1 /* 2 * Copyright (c) 2011, 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 package org.graalvm.compiler.core.test; 24 25 import org.graalvm.compiler.core.common.cfg.Loop; 26 import org.graalvm.compiler.debug.DebugContext; 27 import org.graalvm.compiler.graph.Node; 28 import org.graalvm.compiler.nodes.Invoke; 29 import org.graalvm.compiler.nodes.StructuredGraph; 30 import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions; 31 import org.graalvm.compiler.nodes.cfg.Block; 32 import org.graalvm.compiler.nodes.cfg.ControlFlowGraph; 33 import org.graalvm.compiler.nodes.java.MethodCallTargetNode; 34 import org.junit.Assert; 35 import org.junit.Test; 36 37 public class NestedLoopTest extends GraalCompilerTest { 38 39 @Test 40 public void test1() { 41 test("test1Snippet", 1, 2, 2); 42 } 43 44 @Test 45 public void test2() { 46 test("test2Snippet", 1, 2, 2); 47 } 48 49 @Test 50 public void test3() { 51 test("test3Snippet", 1, 2, 2); 52 } 53 54 @Test 55 public void test4() { 56 test("test4Snippet", 1, 3, 2); 57 } 58 59 @SuppressWarnings("all") 60 public static void test1Snippet(int a) { 61 while (a()) { // a() exits root, while() exits root 62 m1: while (b()) { // b() exits nested & root, while() exits nested 63 while (c()) { // c() exits innermost & nested & root, while() exits innermost 64 if (d()) { // d() exits innermost & nested & root 65 break m1; // break exits innermost & nested 66 } 67 } 68 } 69 } 70 }// total : root = 5 exits, nested = 5, innermost = 4 71 72 @SuppressWarnings("all") 73 public static void test2Snippet(int a) { 74 while (a()) { // a() exits root, while() exits root 75 try { 76 m1: while (b()) { // b() exits nested, while() exits nested 77 while (c()) { // c() exits innermost & nested, while() exits innermost 78 if (d()) { // d() exits innermost & nested 79 break m1; // break exits innermost & nested 80 } 81 } 82 } 83 } catch (Throwable t) { 84 } 85 } 86 }// total : root = 2 exits, nested = 5, innermost = 4 87 88 @SuppressWarnings("all") 89 public static void test3Snippet(int a) { 90 while (a == 0) { // while() exits root 91 try { 92 m1: while (b()) { // b() exits nested, while() exits nested 93 while (c()) { // c() exits innermost & nested, while() exits innermost 94 if (d()) { // d() exits innermost & nested 95 a(); // a() exits nothing (already outside innermost & nested) 96 break m1; // break exits innermost & nested 97 } 98 } 99 } 100 } catch (Throwable t) { 101 } 102 } 103 }// total : root = 1 exit, nested = 5, innermost = 4 104 105 public static void test4Snippet(int a) { 106 while (a != 0) { // while() exits root 107 try { 108 m1: while (a != 0) { // while() exits nested 109 b(); // b() exits nested 110 while (c()) { // c() exits innermost & nested, while() exits innermost 111 if (d()) { // d() exits innermost & nested 112 break m1; // break exits innermost & nested 113 } 114 } 115 if (a != 2) { 116 a(); // a() exits nothing (already outside innermost & nested) 117 throw new Exception(); // throw exits nested 118 } 119 } 120 } catch (Throwable t) { 121 } 122 } 123 } // total : root = 1 exit, nested = 6, innermost = 4 124 125 private static native boolean a(); 126 127 private static native boolean b(); 128 129 private static native boolean c(); 130 131 private static native boolean d(); 132 133 private static Invoke getInvoke(String name, StructuredGraph graph) { 134 for (MethodCallTargetNode callTarget : graph.getNodes(MethodCallTargetNode.TYPE)) { 135 if (callTarget.targetMethod().getName().equals(name)) { 136 return callTarget.invoke(); 137 } 138 } 139 return null; 140 } 141 142 private void test(String snippet, int rootExits, int nestedExits, int innerExits) { 143 StructuredGraph graph = parseEager(snippet, AllowAssumptions.YES); 144 DebugContext debug = graph.getDebug(); 145 debug.dump(DebugContext.BASIC_LEVEL, graph, "Graph"); 146 ControlFlowGraph cfg = ControlFlowGraph.compute(graph, true, true, true, true); 147 148 Assert.assertEquals(3, cfg.getLoops().size()); 149 Loop<Block> rootLoop = cfg.getLoops().get(0); 150 Loop<Block> nestedLoop = cfg.getLoops().get(1); 151 Loop<Block> innerMostLoop = cfg.getLoops().get(2); 152 Invoke a = getInvoke("a", graph); 153 Invoke b = getInvoke("b", graph); 154 Invoke c = getInvoke("c", graph); 155 Invoke d = getInvoke("d", graph); 156 Assert.assertTrue(containsDirect(rootLoop, a, cfg)); 157 Assert.assertTrue(containsDirect(nestedLoop, b, cfg)); 158 Assert.assertTrue(containsDirect(innerMostLoop, c, cfg)); 159 Assert.assertTrue(containsDirect(innerMostLoop, d, cfg)); 160 Assert.assertTrue(contains(rootLoop, d, cfg)); 161 Assert.assertTrue(contains(nestedLoop, d, cfg)); 162 Assert.assertEquals(rootExits, rootLoop.getExits().size()); 163 Assert.assertEquals(nestedExits, nestedLoop.getExits().size()); 164 Assert.assertEquals(innerExits, innerMostLoop.getExits().size()); 165 debug.dump(DebugContext.BASIC_LEVEL, graph, "Graph"); 166 } 167 168 private static boolean contains(Loop<Block> loop, Invoke node, ControlFlowGraph cfg) { 169 Block block = cfg.blockFor((Node) node); 170 Assert.assertNotNull(block); 171 return loop.getBlocks().contains(block); 172 } 173 174 private static boolean containsDirect(Loop<Block> loop, Invoke node, ControlFlowGraph cfg) { 175 for (Loop<Block> child : loop.getChildren()) { 176 if (contains(child, node, cfg)) { 177 return false; 178 } 179 } 180 return contains(loop, node, cfg); 181 } 182 }