1 /*
  2  * Copyright (c) 2024, 2026, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */
 23 
 24 import jdk.incubator.code.*;
 25 import jdk.incubator.code.Reflect;
 26 import jdk.incubator.code.dialect.core.SSA;
 27 import org.junit.jupiter.api.Assertions;
 28 import org.junit.jupiter.api.Test;
 29 
 30 import java.lang.invoke.MethodHandles;
 31 import java.lang.reflect.Method;
 32 import java.util.Optional;
 33 import java.util.function.Predicate;
 34 import java.util.stream.Stream;
 35 
 36 import static jdk.incubator.code.dialect.core.CoreOp.FuncOp;
 37 import static jdk.incubator.code.dialect.core.CoreOp.VarAccessOp.VarLoadOp;
 38 import static jdk.incubator.code.dialect.core.CoreOp.VarAccessOp.VarStoreOp;
 39 import static jdk.incubator.code.dialect.core.CoreOp.VarOp;
 40 
 41 /*
 42  * @test
 43  * @modules jdk.incubator.code
 44  * @library lib
 45  * @run junit TestRemoveFinalVars
 46  */
 47 
 48 public class TestRemoveFinalVars {
 49 
 50     @Reflect
 51     static boolean f() {
 52         final int x = 8; // final var
 53         int y = x + 2; // final var
 54         int z = y + 3; // non final var
 55         z++;
 56         return x == 8 && y == 10 && z == 14;
 57     }
 58 
 59     @Test
 60     void test() {
 61         FuncOp f = getFuncOp(this.getClass(),"f");
 62         System.out.println(f.toText());
 63         FuncOp lf = lower(f);
 64         System.out.println(lf.toText());
 65 
 66         FuncOp f2 = f.transform(TestRemoveFinalVars::rmFinalVars);
 67         System.out.println(f2.toText());
 68         FuncOp lf2 = lower(f2);
 69         System.out.println(lf2.toText());
 70 
 71         Assertions.assertEquals(Interpreter.invoke(MethodHandles.lookup(), lf2), Interpreter.invoke(MethodHandles.lookup(), lf));
 72 
 73         Op op = SSA.transform(lower(f));
 74         System.out.println(op.toText());
 75     }
 76 
 77     static FuncOp lower(FuncOp funcOp) {
 78         return funcOp.transform(CodeTransformer.LOWERING_TRANSFORMER);
 79     }
 80 
 81     static Block.Builder rmFinalVars(Block.Builder block, Op op) {
 82         if (op instanceof VarOp varOp) {
 83             // Is the variable stored to? If not we can remove it
 84             // otherwise, it's not considered final and we copy it
 85             if (isValueUsedWithOp(varOp.result(), o -> o instanceof VarStoreOp)) {
 86                 block.add(varOp);
 87             }
 88         } else if (op instanceof VarLoadOp varLoadOp) {
 89             // If the variable is not stored to
 90             if (!isValueUsedWithOp(varLoadOp.varOp().result(), o -> o instanceof VarStoreOp)) {
 91                 // Map result of load from variable to the value that initialized the variable
 92                 // Subsequently encountered input operations using the result will be copied
 93                 // to output operations using the mapped value
 94                 CodeContext cc = block.context();
 95                 cc.mapValue(varLoadOp.result(), cc.getValue(varLoadOp.varOp().operands().get(0)));
 96             } else {
 97                 block.add(varLoadOp);
 98             }
 99         } else {
100             block.add(op);
101         }
102         return block;
103     }
104 
105     private static boolean isValueUsedWithOp(Value value, Predicate<Op> opPredicate) {
106         for (Op.Result user : value.uses()) {
107             if (opPredicate.test(user.op())) {
108                 return true;
109             }
110         }
111         return false;
112     }
113 
114     static FuncOp getFuncOp(Class<?> c, String name) {
115         Optional<Method> om = Stream.of(c.getDeclaredMethods())
116                 .filter(m -> m.getName().equals(name))
117                 .findFirst();
118 
119         Method m = om.get();
120         return Op.ofMethod(m).get();
121     }
122 }