package org.graalvm.compiler.core.test;
import org.graalvm.compiler.core.common.cfg.Loop;
import org.graalvm.compiler.debug.DebugContext;
import org.graalvm.compiler.graph.Node;
import org.graalvm.compiler.nodes.Invoke;
import org.graalvm.compiler.nodes.StructuredGraph;
import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions;
import org.graalvm.compiler.nodes.cfg.Block;
import org.graalvm.compiler.nodes.cfg.ControlFlowGraph;
import org.graalvm.compiler.nodes.java.MethodCallTargetNode;
import org.junit.Assert;
import org.junit.Test;
public class NestedLoopTest extends GraalCompilerTest {
@Test
public void test1() {
test("test1Snippet", 1, 2, 2);
}
@Test
public void test2() {
test("test2Snippet", 1, 2, 2);
}
@Test
public void test3() {
test("test3Snippet", 1, 2, 2);
}
@Test
public void test4() {
test("test4Snippet", 1, 3, 2);
}
@SuppressWarnings("all")
public static void test1Snippet(int a) {
while (a()) {
m1: while (b()) {
while (c()) {
if (d()) {
break m1;
}
}
}
}
}
@SuppressWarnings("all")
public static void test2Snippet(int a) {
while (a()) {
try {
m1: while (b()) {
while (c()) {
if (d()) {
break m1;
}
}
}
} catch (Throwable t) {
}
}
}
@SuppressWarnings("all")
public static void test3Snippet(int a) {
while (a == 0) {
try {
m1: while (b()) {
while (c()) {
if (d()) {
a();
break m1;
}
}
}
} catch (Throwable t) {
}
}
}
public static void test4Snippet(int a) {
while (a != 0) {
try {
m1: while (a != 0) {
b();
while (c()) {
if (d()) {
break m1;
}
}
if (a != 2) {
a();
throw new Exception();
}
}
} catch (Throwable t) {
}
}
}
private static native boolean a();
private static native boolean b();
private static native boolean c();
private static native boolean d();
private static Invoke getInvoke(String name, StructuredGraph graph) {
for (MethodCallTargetNode callTarget : graph.getNodes(MethodCallTargetNode.TYPE)) {
if (callTarget.targetMethod().getName().equals(name)) {
return callTarget.invoke();
}
}
return null;
}
private void test(String snippet, int rootExits, int nestedExits, int innerExits) {
StructuredGraph graph = parseEager(snippet, AllowAssumptions.YES);
DebugContext debug = graph.getDebug();
debug.dump(DebugContext.BASIC_LEVEL, graph, "Graph");
ControlFlowGraph cfg = ControlFlowGraph.compute(graph, true, true, true, true);
Assert.assertEquals(3, cfg.getLoops().size());
Loop<Block> rootLoop = cfg.getLoops().get(0);
Loop<Block> nestedLoop = cfg.getLoops().get(1);
Loop<Block> innerMostLoop = cfg.getLoops().get(2);
Invoke a = getInvoke("a", graph);
Invoke b = getInvoke("b", graph);
Invoke c = getInvoke("c", graph);
Invoke d = getInvoke("d", graph);
Assert.assertTrue(containsDirect(rootLoop, a, cfg));
Assert.assertTrue(containsDirect(nestedLoop, b, cfg));
Assert.assertTrue(containsDirect(innerMostLoop, c, cfg));
Assert.assertTrue(containsDirect(innerMostLoop, d, cfg));
Assert.assertTrue(contains(rootLoop, d, cfg));
Assert.assertTrue(contains(nestedLoop, d, cfg));
Assert.assertEquals(rootExits, rootLoop.getExits().size());
Assert.assertEquals(nestedExits, nestedLoop.getExits().size());
Assert.assertEquals(innerExits, innerMostLoop.getExits().size());
debug.dump(DebugContext.BASIC_LEVEL, graph, "Graph");
}
private static boolean contains(Loop<Block> loop, Invoke node, ControlFlowGraph cfg) {
Block block = cfg.blockFor((Node) node);
Assert.assertNotNull(block);
return loop.getBlocks().contains(block);
}
private static boolean containsDirect(Loop<Block> loop, Invoke node, ControlFlowGraph cfg) {
for (Loop<Block> child : loop.getChildren()) {
if (contains(child, node, cfg)) {
return false;
}
}
return contains(loop, node, cfg);
}
}