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.junit.Assert;
  26 import org.junit.Test;
  27 
  28 import org.graalvm.compiler.core.common.cfg.Loop;
  29 import org.graalvm.compiler.debug.Debug;
  30 import org.graalvm.compiler.graph.Node;
  31 import org.graalvm.compiler.nodes.Invoke;
  32 import org.graalvm.compiler.nodes.StructuredGraph;
  33 import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions;
  34 import org.graalvm.compiler.nodes.cfg.Block;
  35 import org.graalvm.compiler.nodes.cfg.ControlFlowGraph;
  36 import org.graalvm.compiler.nodes.java.MethodCallTargetNode;
  37 
  38 public class NestedLoopTest extends GraalCompilerTest {
  39 
  40     @Test
  41     public void test1() {
  42         test("test1Snippet", 1, 2, 2);
  43     }
  44 
  45     @Test
  46     public void test2() {
  47         test("test2Snippet", 1, 2, 2);
  48     }
  49 
  50     @Test
  51     public void test3() {
  52         test("test3Snippet", 1, 2, 2);
  53     }
  54 
  55     @Test
  56     public void test4() {
  57         test("test4Snippet", 1, 3, 2);
  58     }
  59 
  60     @SuppressWarnings("all")
  61     public static void test1Snippet(int a) {
  62         while (a()) { // a() exits root, while() exits root
  63             m1: while (b()) { // b() exits nested & root, while() exits nested
  64                 while (c()) { // c() exits innermost & nested & root, while() exits innermost
  65                     if (d()) { // d() exits innermost & nested & root
  66                         break m1; // break exits innermost & nested
  67                     }
  68                 }
  69             }
  70         }
  71     }// total : root = 5 exits, nested = 5, innermost = 4
  72 
  73     @SuppressWarnings("all")
  74     public static void test2Snippet(int a) {
  75         while (a()) { // a() exits root, while() exits root
  76             try {
  77                 m1: while (b()) { // b() exits nested, while() exits nested
  78                     while (c()) { // c() exits innermost & nested, while() exits innermost
  79                         if (d()) { // d() exits innermost & nested
  80                             break m1; // break exits innermost & nested
  81                         }
  82                     }
  83                 }
  84             } catch (Throwable t) {
  85             }
  86         }
  87     }// total : root = 2 exits, nested = 5, innermost = 4
  88 
  89     @SuppressWarnings("all")
  90     public static void test3Snippet(int a) {
  91         while (a == 0) { // while() exits root
  92             try {
  93                 m1: while (b()) { // b() exits nested, while() exits nested
  94                     while (c()) { // c() exits innermost & nested, while() exits innermost
  95                         if (d()) { // d() exits innermost & nested
  96                             a(); // a() exits nothing (already outside innermost & nested)
  97                             break m1; // break exits innermost & nested
  98                         }
  99                     }
 100                 }
 101             } catch (Throwable t) {
 102             }
 103         }
 104     }// total : root = 1 exit, nested = 5, innermost = 4
 105 
 106     public static void test4Snippet(int a) {
 107         while (a != 0) { // while() exits root
 108             try {
 109                 m1: while (a != 0) { // while() exits nested
 110                     b(); // b() exits nested
 111                     while (c()) { // c() exits innermost & nested, while() exits innermost
 112                         if (d()) { // d() exits innermost & nested
 113                             break m1; // break exits innermost & nested
 114                         }
 115                     }
 116                     if (a != 2) {
 117                         a(); // a() exits nothing (already outside innermost & nested)
 118                         throw new Exception(); // throw exits nested
 119                     }
 120                 }
 121             } catch (Throwable t) {
 122             }
 123         }
 124     } // total : root = 1 exit, nested = 6, innermost = 4
 125 
 126     private static native boolean a();
 127 
 128     private static native boolean b();
 129 
 130     private static native boolean c();
 131 
 132     private static native boolean d();
 133 
 134     private static Invoke getInvoke(String name, StructuredGraph graph) {
 135         for (MethodCallTargetNode callTarget : graph.getNodes(MethodCallTargetNode.TYPE)) {
 136             if (callTarget.targetMethod().getName().equals(name)) {
 137                 return callTarget.invoke();
 138             }
 139         }
 140         return null;
 141     }
 142 
 143     private void test(String snippet, int rootExits, int nestedExits, int innerExits) {
 144         StructuredGraph graph = parseEager(snippet, AllowAssumptions.YES);
 145         Debug.dump(Debug.BASIC_LOG_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(Debug.BASIC_LOG_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 }