1 /*
  2  * Copyright (c) 2024, 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 org.testng.Assert;
 25 import org.testng.annotations.Test;
 26 
 27 import java.lang.reflect.code.OpTransformer;
 28 import java.lang.reflect.code.op.CoreOp;
 29 import java.lang.reflect.code.Op;
 30 import java.lang.reflect.code.analysis.SSA;
 31 import java.lang.reflect.code.interpreter.Interpreter;
 32 import java.lang.reflect.Method;
 33 import java.lang.runtime.CodeReflection;
 34 import java.util.Optional;
 35 import java.util.stream.Stream;
 36 
 37 /*
 38  * @test
 39  * @run testng TestSSA
 40  */
 41 
 42 public class TestSSA {
 43 
 44     @CodeReflection
 45     static int ifelse(int a, int b, int n) {
 46         if (n < 10) {
 47             a += 1;
 48         } else {
 49             b += 2;
 50         }
 51         return a + b;
 52     }
 53 
 54     @Test
 55     public void testIfelse() throws Throwable {
 56         CoreOp.FuncOp f = getFuncOp("ifelse");
 57 
 58         CoreOp.FuncOp lf = generate(f);
 59 
 60         Assert.assertEquals((int) Interpreter.invoke(lf, 0, 0, 1), ifelse(0, 0, 1));
 61         Assert.assertEquals((int) Interpreter.invoke(lf, 0, 0, 11), ifelse(0, 0, 11));
 62     }
 63 
 64     @CodeReflection
 65     static int ifelseNested(int a, int b, int c, int d, int n) {
 66         if (n < 20) {
 67             if (n < 10) {
 68                 a += 1;
 69             } else {
 70                 b += 2;
 71             }
 72             c += 3;
 73         } else {
 74             if (n > 20) {
 75                 a += 4;
 76             } else {
 77                 b += 5;
 78             }
 79             d += 6;
 80         }
 81         return a + b + c + d;
 82     }
 83 
 84     @Test
 85     public void testIfelseNested() throws Throwable {
 86         CoreOp.FuncOp f = getFuncOp("ifelseNested");
 87 
 88         CoreOp.FuncOp lf = generate(f);
 89 
 90         for (int i : new int[]{1, 11, 20, 21}) {
 91             Assert.assertEquals((int) Interpreter.invoke(lf, 0, 0, 0, 0, i), ifelseNested(0, 0, 0, 0, i));
 92         }
 93     }
 94 
 95     @CodeReflection
 96     static int loop(int n) {
 97         int sum = 0;
 98         for (int i = 0; i < n; i++) {
 99             sum = sum + i;
100         }
101         return sum;
102     }
103 
104     @Test
105     public void testLoop() throws Throwable {
106         CoreOp.FuncOp f = getFuncOp("loop");
107 
108         CoreOp.FuncOp lf = generate(f);
109 
110         Assert.assertEquals((int) Interpreter.invoke(lf, 10), loop(10));
111     }
112 
113     @CodeReflection
114     static int nestedLoop(int n) {
115         int sum = 0;
116         for (int i = 0; i < n; i++) {
117             for (int j = 0; j < n; j++) {
118                 sum = sum + i + j;
119             }
120         }
121         return sum;
122     }
123 
124     @Test
125     public void testNestedLoop() throws Throwable {
126         CoreOp.FuncOp f = getFuncOp("nestedLoop");
127 
128         CoreOp.FuncOp lf = generate(f);
129 
130         Assert.assertEquals((int) Interpreter.invoke(lf, 10), nestedLoop(10));
131     }
132 
133     static CoreOp.FuncOp generate(CoreOp.FuncOp f) {
134         f.writeTo(System.out);
135 
136         CoreOp.FuncOp lf = f.transform(OpTransformer.LOWERING_TRANSFORMER);
137         lf.writeTo(System.out);
138 
139         lf = SSA.transform(lf);
140         lf.writeTo(System.out);
141         return lf;
142     }
143 
144     static CoreOp.FuncOp getFuncOp(String name) {
145         Optional<Method> om = Stream.of(TestSSA.class.getDeclaredMethods())
146                 .filter(m -> m.getName().equals(name))
147                 .findFirst();
148 
149         Method m = om.get();
150         return m.getCodeModel().get();
151     }
152 }