@@ -90,13 +90,13 @@ public static boolean simplifyStackVarsStatement(
9090 }
9191 }
9292 } else {
93- res = simplifyStackVarsExprents (expressions , cl , firstInvocation );
93+ res = simplifyStackVarsExprents (expressions , cl , stat , firstInvocation );
9494 }
9595
9696 return res ;
9797 }
9898
99- private static boolean simplifyStackVarsExprents (List <Exprent > list , StructClass cl , boolean firstInvocation ) {
99+ private static boolean simplifyStackVarsExprents (List <Exprent > list , StructClass cl , Statement stat , boolean firstInvocation ) {
100100 boolean res = false ;
101101
102102 int index = 0 ;
@@ -149,7 +149,7 @@ private static boolean simplifyStackVarsExprents(List<Exprent> list, StructClass
149149 }
150150 }
151151
152- if (isAssignmentReturn (current , next )) {
152+ if (isAssignmentReturn (current , next , stat )) {
153153 list .remove (index );
154154 res = true ;
155155 continue ;
@@ -396,7 +396,7 @@ private static int isArrayInitializer(List<Exprent> list, int index) {
396396 * Note that this is transformation will result into java that is less like the original.
397397 * TODO: put this behind a compiler option.
398398 */
399- private static boolean isAssignmentReturn (Exprent first , Exprent second ) {
399+ private static boolean isAssignmentReturn (Exprent first , Exprent second , Statement stat ) {
400400 //If assignment then exit.
401401 if (first instanceof AssignmentExprent
402402 && second instanceof ExitExprent ) {
@@ -406,12 +406,20 @@ private static boolean isAssignmentReturn(Exprent first, Exprent second) {
406406 if (assignment .getCondType () == null
407407 && exit .getExitType () == ExitExprent .Type .RETURN
408408 && exit .getValue () != null
409- && assignment .getLeft () instanceof VarExprent
410- && exit .getValue () instanceof VarExprent ) {
411- VarExprent assignmentLeft = (VarExprent ) assignment .getLeft ();
412- VarExprent exitValue = (VarExprent ) exit .getValue ();
409+ && assignment .getLeft () instanceof VarExprent assignmentLeft
410+ && exit .getValue () instanceof VarExprent exitValue ) {
413411 //If the assignment before the return is immediately used in the return, inline it.
414412 if (assignmentLeft .equals (exitValue ) && !assignmentLeft .isStack () && !exitValue .isStack ()) {
413+ // Avoid doing this transform for potential pattern matches, as they should be processed by the pattern matcher first.
414+ if (stat .getTopParent ().mt .getBytecodeVersion ().hasIfPatternMatching ()
415+ && stat .getParent () instanceof IfStatement ifst && !ifst .isPatternMatched () && stat .getExprents ().indexOf (first ) == 0
416+ && assignment .getRight () instanceof FunctionExprent func && func .getFuncType () == FunctionType .CAST
417+ // Most expensive, do it last
418+ && ifst .getHeadexprent ().getAllExprents (true , false ).stream ().anyMatch (e -> e instanceof FunctionExprent f && f .getFuncType () == FunctionType .INSTANCEOF )
419+ ) {
420+ return false ;
421+ }
422+
415423 exit .replaceExprent (exitValue , assignment .getRight ());
416424 return true ;
417425 }
0 commit comments