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 static org.graalvm.compiler.core.common.CompilationIdentifier.INVALID_COMPILATION_ID;
26
27 import org.junit.Assert;
28 import org.junit.Test;
29
30 import org.graalvm.compiler.debug.Debug;
31 import org.graalvm.compiler.nodes.AbstractBeginNode;
32 import org.graalvm.compiler.nodes.AbstractMergeNode;
33 import org.graalvm.compiler.nodes.BeginNode;
34 import org.graalvm.compiler.nodes.EndNode;
35 import org.graalvm.compiler.nodes.IfNode;
36 import org.graalvm.compiler.nodes.MergeNode;
37 import org.graalvm.compiler.nodes.ReturnNode;
38 import org.graalvm.compiler.nodes.StructuredGraph;
39 import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions;
40 import org.graalvm.compiler.nodes.cfg.Block;
41 import org.graalvm.compiler.nodes.cfg.ControlFlowGraph;
42
43 public class SimpleCFGTest extends GraalCompilerTest {
44
45 private static void dumpGraph(final StructuredGraph graph) {
46 Debug.dump(Debug.BASIC_LOG_LEVEL, graph, "Graph");
47 }
48
49 @Test
50 public void testImplies() {
51 StructuredGraph graph = new StructuredGraph(AllowAssumptions.YES, INVALID_COMPILATION_ID);
52
53 EndNode trueEnd = graph.add(new EndNode());
54 EndNode falseEnd = graph.add(new EndNode());
55
56 AbstractBeginNode trueBegin = graph.add(new BeginNode());
57 trueBegin.setNext(trueEnd);
58 AbstractBeginNode falseBegin = graph.add(new BeginNode());
59 falseBegin.setNext(falseEnd);
60
61 IfNode ifNode = graph.add(new IfNode(null, trueBegin, falseBegin, 0.5));
62 graph.start().setNext(ifNode);
63
64 AbstractMergeNode merge = graph.add(new MergeNode());
65 merge.addForwardEnd(trueEnd);
66 merge.addForwardEnd(falseEnd);
67 ReturnNode returnNode = graph.add(new ReturnNode(null));
68 merge.setNext(returnNode);
69
70 dumpGraph(graph);
71
92 assertDominator(blocks[3], blocks[0]);
93
94 // check dominated
95 assertDominatedSize(blocks[0], 3);
96 assertDominatedSize(blocks[1], 0);
97 assertDominatedSize(blocks[2], 0);
98 assertDominatedSize(blocks[3], 0);
99
100 // check postdominators
101 assertPostdominator(blocks[0], blocks[3]);
102 assertPostdominator(blocks[1], blocks[3]);
103 assertPostdominator(blocks[2], blocks[3]);
104 assertPostdominator(blocks[3], null);
105 }
106
107 public static void assertDominator(Block block, Block expectedDominator) {
108 Assert.assertEquals("dominator of " + block, expectedDominator, block.getDominator());
109 }
110
111 public static void assertDominatedSize(Block block, int size) {
112 Assert.assertEquals("number of dominated blocks of " + block, size, block.getDominated().size());
113 }
114
115 public static void assertPostdominator(Block block, Block expectedPostdominator) {
116 Assert.assertEquals("postdominator of " + block, expectedPostdominator, block.getPostdominator());
117 }
118
119 }
|
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.debug.Debug;
29 import org.graalvm.compiler.nodes.AbstractBeginNode;
30 import org.graalvm.compiler.nodes.AbstractMergeNode;
31 import org.graalvm.compiler.nodes.BeginNode;
32 import org.graalvm.compiler.nodes.EndNode;
33 import org.graalvm.compiler.nodes.IfNode;
34 import org.graalvm.compiler.nodes.MergeNode;
35 import org.graalvm.compiler.nodes.ReturnNode;
36 import org.graalvm.compiler.nodes.StructuredGraph;
37 import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions;
38 import org.graalvm.compiler.nodes.cfg.Block;
39 import org.graalvm.compiler.nodes.cfg.ControlFlowGraph;
40
41 public class SimpleCFGTest extends GraalCompilerTest {
42
43 private static void dumpGraph(final StructuredGraph graph) {
44 Debug.dump(Debug.BASIC_LOG_LEVEL, graph, "Graph");
45 }
46
47 @Test
48 public void testImplies() {
49 StructuredGraph graph = new StructuredGraph.Builder(getInitialOptions(), AllowAssumptions.YES).build();
50
51 EndNode trueEnd = graph.add(new EndNode());
52 EndNode falseEnd = graph.add(new EndNode());
53
54 AbstractBeginNode trueBegin = graph.add(new BeginNode());
55 trueBegin.setNext(trueEnd);
56 AbstractBeginNode falseBegin = graph.add(new BeginNode());
57 falseBegin.setNext(falseEnd);
58
59 IfNode ifNode = graph.add(new IfNode(null, trueBegin, falseBegin, 0.5));
60 graph.start().setNext(ifNode);
61
62 AbstractMergeNode merge = graph.add(new MergeNode());
63 merge.addForwardEnd(trueEnd);
64 merge.addForwardEnd(falseEnd);
65 ReturnNode returnNode = graph.add(new ReturnNode(null));
66 merge.setNext(returnNode);
67
68 dumpGraph(graph);
69
90 assertDominator(blocks[3], blocks[0]);
91
92 // check dominated
93 assertDominatedSize(blocks[0], 3);
94 assertDominatedSize(blocks[1], 0);
95 assertDominatedSize(blocks[2], 0);
96 assertDominatedSize(blocks[3], 0);
97
98 // check postdominators
99 assertPostdominator(blocks[0], blocks[3]);
100 assertPostdominator(blocks[1], blocks[3]);
101 assertPostdominator(blocks[2], blocks[3]);
102 assertPostdominator(blocks[3], null);
103 }
104
105 public static void assertDominator(Block block, Block expectedDominator) {
106 Assert.assertEquals("dominator of " + block, expectedDominator, block.getDominator());
107 }
108
109 public static void assertDominatedSize(Block block, int size) {
110 int count = 0;
111 Block domChild = block.getFirstDominated();
112 while (domChild != null) {
113 count++;
114 domChild = domChild.getDominatedSibling();
115 }
116 Assert.assertEquals("number of dominated blocks of " + block, size, count);
117 }
118
119 public static void assertPostdominator(Block block, Block expectedPostdominator) {
120 Assert.assertEquals("postdominator of " + block, expectedPostdominator, block.getPostdominator());
121 }
122
123 }
|