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 package org.graalvm.compiler.core.test; 24 25 import org.junit.Test; 26 27 import org.graalvm.compiler.debug.Debug; 28 import org.graalvm.compiler.debug.Debug.Scope; 29 import org.graalvm.compiler.debug.DebugDumpScope; 30 import org.graalvm.compiler.nodes.StructuredGraph; 31 import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions; 32 import org.graalvm.compiler.phases.common.CanonicalizerPhase; 33 import org.graalvm.compiler.phases.common.inlining.InliningPhase; 34 import org.graalvm.compiler.phases.tiers.HighTierContext; 35 36 /** 37 * In the following tests, the usages of local variable "a" are replaced with the integer constant 38 * 0. Then canonicalization is applied and it is verified that the resulting graph is equal to the 39 * graph of the method that just has a "return 1" statement in it. 40 */ 41 public class DegeneratedLoopsTest extends GraalCompilerTest { 42 43 private static final String REFERENCE_SNIPPET = "referenceSnippet"; 44 45 @SuppressWarnings("all") 46 public static int referenceSnippet(int a) { 47 return a; 48 } 49 50 @Test 51 public void test1() { 52 test("test1Snippet"); 53 } 54 64 } 65 66 @SuppressWarnings("all") 67 public static int test1Snippet(int a) { 68 for (;;) { 69 try { 70 test(); 71 break; 72 } catch (UnresolvedException e) { 73 } 74 } 75 return a; 76 } 77 78 private static void test() { 79 80 } 81 82 @SuppressWarnings("try") 83 private void test(final String snippet) { 84 try (Scope s = Debug.scope("DegeneratedLoopsTest", new DebugDumpScope(snippet))) { 85 StructuredGraph graph = parseEager(snippet, AllowAssumptions.YES); 86 HighTierContext context = getDefaultHighTierContext(); 87 new InliningPhase(new CanonicalizerPhase()).apply(graph, context); 88 new CanonicalizerPhase().apply(graph, context); 89 Debug.dump(Debug.BASIC_LEVEL, graph, "Graph"); 90 StructuredGraph referenceGraph = parseEager(REFERENCE_SNIPPET, AllowAssumptions.YES); 91 Debug.dump(Debug.BASIC_LEVEL, referenceGraph, "ReferenceGraph"); 92 assertEquals(referenceGraph, graph); 93 } catch (Throwable e) { 94 throw Debug.handle(e); 95 } 96 } 97 } | 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 package org.graalvm.compiler.core.test; 24 25 import org.graalvm.compiler.debug.DebugContext; 26 import org.graalvm.compiler.debug.DebugDumpScope; 27 import org.graalvm.compiler.nodes.StructuredGraph; 28 import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions; 29 import org.graalvm.compiler.phases.common.CanonicalizerPhase; 30 import org.graalvm.compiler.phases.common.inlining.InliningPhase; 31 import org.graalvm.compiler.phases.tiers.HighTierContext; 32 import org.junit.Test; 33 34 /** 35 * In the following tests, the usages of local variable "a" are replaced with the integer constant 36 * 0. Then canonicalization is applied and it is verified that the resulting graph is equal to the 37 * graph of the method that just has a "return 1" statement in it. 38 */ 39 public class DegeneratedLoopsTest extends GraalCompilerTest { 40 41 private static final String REFERENCE_SNIPPET = "referenceSnippet"; 42 43 @SuppressWarnings("all") 44 public static int referenceSnippet(int a) { 45 return a; 46 } 47 48 @Test 49 public void test1() { 50 test("test1Snippet"); 51 } 52 62 } 63 64 @SuppressWarnings("all") 65 public static int test1Snippet(int a) { 66 for (;;) { 67 try { 68 test(); 69 break; 70 } catch (UnresolvedException e) { 71 } 72 } 73 return a; 74 } 75 76 private static void test() { 77 78 } 79 80 @SuppressWarnings("try") 81 private void test(final String snippet) { 82 DebugContext debug = getDebugContext(); 83 try (DebugContext.Scope s = debug.scope("DegeneratedLoopsTest", new DebugDumpScope(snippet))) { 84 StructuredGraph graph = parseEager(snippet, AllowAssumptions.YES); 85 HighTierContext context = getDefaultHighTierContext(); 86 new InliningPhase(new CanonicalizerPhase()).apply(graph, context); 87 new CanonicalizerPhase().apply(graph, context); 88 debug.dump(DebugContext.BASIC_LEVEL, graph, "Graph"); 89 StructuredGraph referenceGraph = parseEager(REFERENCE_SNIPPET, AllowAssumptions.YES); 90 debug.dump(DebugContext.BASIC_LEVEL, referenceGraph, "ReferenceGraph"); 91 assertEquals(referenceGraph, graph); 92 } catch (Throwable e) { 93 throw debug.handle(e); 94 } 95 } 96 } |