33import static org .prlprg .fir .ir .abstraction .AbstractionCopier .copy ;
44
55import com .google .common .collect .ImmutableList ;
6- import org .jspecify .annotations .NonNull ;
6+ import java .util .Objects ;
7+ import org .jspecify .annotations .Nullable ;
78import org .prlprg .fir .feedback .AbstractionFeedback ;
89import org .prlprg .fir .ir .abstraction .Abstraction ;
910import org .prlprg .fir .ir .callee .StaticFnCallee ;
1011import org .prlprg .fir .ir .expression .Call ;
1112import org .prlprg .fir .ir .instruction .Statement ;
1213import org .prlprg .fir .ir .module .Function ;
13- import org .prlprg .fir .ir .type .Type ;
1414import org .prlprg .fir .opt .specialize .OptimizeCallee ;
15+ import org .prlprg .util .Lists ;
1516
1617/// If there's a call whose best guaranteed version
1718/// ([Function#guess(org.prlprg.fir.ir.type.Signature)]) does not exactly match its arguments'
1819/// static types, copy that version into a new one and narrow the parameter types to match
1920public record CreateBestVersion (int versionLimit ) implements AbstractionOptimization {
2021 public boolean runWithoutRecording (
21- Function function , AbstractionFeedback feedback , Abstraction version ) {
22+ @ Nullable Function function , AbstractionFeedback feedback , Abstraction version ) {
2223 var calls =
2324 version
2425 .streamCfgs ()
@@ -44,16 +45,15 @@ private boolean run(AbstractionFeedback feedback, Abstraction scope, Call call)
4445 }
4546
4647 var callArguments = call .callArguments ();
47- var argumentTypes =
48- callArguments .stream ().map (scope ::typeOf ).collect (ImmutableList .toImmutableList ());
49- if (argumentTypes .contains (null )) {
48+ if (callArguments .stream ().anyMatch (a -> scope .typeOf (a ) == null )) {
5049 // Invalid, null type
5150 return false ;
5251 }
53- @ SuppressWarnings ("RedundantCast" )
54- var argumentTypes1 = (ImmutableList <@ NonNull Type >) argumentTypes ;
52+ var argumentTypes =
53+ ImmutableList .copyOf (
54+ Lists .mapStrict (callArguments , a -> Objects .requireNonNull (scope .typeOf (a ))));
5555
56- var bestSignature = OptimizeCallee .bestSignature (callee , argumentTypes1 );
56+ var bestSignature = OptimizeCallee .bestSignature (callee , argumentTypes );
5757 var bestVersion = calleeFun .guess (bestSignature );
5858 if (bestVersion == null ) {
5959 // Invalid, there should always be a possible version
@@ -65,7 +65,7 @@ private boolean run(AbstractionFeedback feedback, Abstraction scope, Call call)
6565 }
6666
6767 // Create a new version with the exact parameter types
68- copy (feedback .module (), calleeFun , bestVersion , argumentTypes1 );
68+ copy (feedback .module (), calleeFun , bestVersion , argumentTypes );
6969 return true ;
7070 }
7171}
0 commit comments