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 java.io.BufferedInputStream;
26 import java.io.ByteArrayInputStream;
27 import java.io.FileInputStream;
28 import java.io.IOException;
29 import java.io.InputStream;
30
31 import org.graalvm.compiler.debug.Debug;
32 import org.graalvm.compiler.debug.TTY;
33 import org.graalvm.compiler.graph.Node;
34 import org.graalvm.compiler.nodeinfo.Verbosity;
35 import org.graalvm.compiler.nodes.AbstractMergeNode;
36 import org.graalvm.compiler.nodes.PhiNode;
37 import org.graalvm.compiler.nodes.StructuredGraph;
38 import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions;
39 import org.graalvm.compiler.nodes.StructuredGraph.ScheduleResult;
40 import org.graalvm.compiler.nodes.cfg.Block;
41 import org.graalvm.compiler.nodes.java.InstanceOfNode;
42 import org.graalvm.compiler.phases.common.CanonicalizerPhase;
43 import org.graalvm.compiler.phases.common.ConditionalEliminationPhase;
44 import org.graalvm.compiler.phases.schedule.SchedulePhase;
45 import org.graalvm.compiler.phases.tiers.PhaseContext;
46 import org.junit.Assert;
47 import org.junit.Ignore;
48 import org.junit.Test;
49
50 /**
51 * In the following tests, the scalar type system of the compiler should be complete enough to see
162 }
163
164 return ((InputStream) o).available();
165 }
166
167 @Test
168 public void test7() {
169 test("test7Snippet", "referenceSnippet7");
170 }
171
172 public static int test7Snippet(int x) {
173 return ((x & 0xff) << 10) == ((x & 0x1f) + 1) ? 0 : x;
174 }
175
176 public static int referenceSnippet7(int x) {
177 return x;
178 }
179
180 private void test(String snippet, String referenceSnippet) {
181 StructuredGraph graph = parseEager(snippet, AllowAssumptions.NO);
182 Debug.dump(Debug.BASIC_LEVEL, graph, "Graph");
183 /*
184 * When using FlowSensitiveReductionPhase instead of ConditionalEliminationPhase,
185 * tail-duplication gets activated thus resulting in a graph with more nodes than the
186 * reference graph.
187 */
188 new ConditionalEliminationPhase(false).apply(graph, new PhaseContext(getProviders()));
189 new CanonicalizerPhase().apply(graph, new PhaseContext(getProviders()));
190 // a second canonicalizer is needed to process nested MaterializeNodes
191 new CanonicalizerPhase().apply(graph, new PhaseContext(getProviders()));
192 StructuredGraph referenceGraph = parseEager(referenceSnippet, AllowAssumptions.NO);
193 new ConditionalEliminationPhase(false).apply(referenceGraph, new PhaseContext(getProviders()));
194 new CanonicalizerPhase().apply(referenceGraph, new PhaseContext(getProviders()));
195 new CanonicalizerPhase().apply(referenceGraph, new PhaseContext(getProviders()));
196 assertEquals(referenceGraph, graph);
197 }
198
199 @Override
200 protected void assertEquals(StructuredGraph expected, StructuredGraph graph) {
201 if (getNodeCountExcludingUnusedConstants(expected) != getNodeCountExcludingUnusedConstants(graph)) {
202 Debug.dump(Debug.BASIC_LEVEL, expected, "expected (node count)");
203 Debug.dump(Debug.BASIC_LEVEL, graph, "graph (node count)");
204 Assert.fail("Graphs do not have the same number of nodes: " + expected.getNodeCount() + " vs. " + graph.getNodeCount());
205 }
206 }
207
208 public static void outputGraph(StructuredGraph graph, String message) {
209 TTY.println("========================= " + message);
210 SchedulePhase schedulePhase = new SchedulePhase(graph.getOptions());
211 schedulePhase.apply(graph);
212 ScheduleResult schedule = graph.getLastSchedule();
213 for (Block block : schedule.getCFG().getBlocks()) {
214 TTY.print("Block " + block + " ");
215 if (block == schedule.getCFG().getStartBlock()) {
216 TTY.print("* ");
217 }
218 TTY.print("-> ");
219 for (Block succ : block.getSuccessors()) {
220 TTY.print(succ + " ");
221 }
222 TTY.println();
223 for (Node node : schedule.getBlockToNodesMap().get(block)) {
226 }
227 }
228
229 private static void outputNode(Node node) {
230 TTY.print(" " + node + " (usage count: " + node.getUsageCount() + ") (inputs:");
231 for (Node input : node.inputs()) {
232 TTY.print(" " + input.toString(Verbosity.Id));
233 }
234 TTY.println(")");
235 if (node instanceof AbstractMergeNode) {
236 for (PhiNode phi : ((AbstractMergeNode) node).phis()) {
237 outputNode(phi);
238 }
239 }
240 }
241
242 private <T extends Node> void testHelper(String snippet, Class<T> clazz) {
243 StructuredGraph graph = parseEager(snippet, AllowAssumptions.NO);
244 new CanonicalizerPhase().apply(graph, new PhaseContext(getProviders()));
245 new CanonicalizerPhase().apply(graph, new PhaseContext(getProviders()));
246 Debug.dump(Debug.BASIC_LEVEL, graph, "Graph " + snippet);
247 Assert.assertFalse("shouldn't have nodes of type " + clazz, graph.getNodes().filter(clazz).iterator().hasNext());
248 }
249 }
|
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 java.io.BufferedInputStream;
26 import java.io.ByteArrayInputStream;
27 import java.io.FileInputStream;
28 import java.io.IOException;
29 import java.io.InputStream;
30
31 import org.graalvm.compiler.debug.DebugContext;
32 import org.graalvm.compiler.debug.TTY;
33 import org.graalvm.compiler.graph.Node;
34 import org.graalvm.compiler.nodeinfo.Verbosity;
35 import org.graalvm.compiler.nodes.AbstractMergeNode;
36 import org.graalvm.compiler.nodes.PhiNode;
37 import org.graalvm.compiler.nodes.StructuredGraph;
38 import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions;
39 import org.graalvm.compiler.nodes.StructuredGraph.ScheduleResult;
40 import org.graalvm.compiler.nodes.cfg.Block;
41 import org.graalvm.compiler.nodes.java.InstanceOfNode;
42 import org.graalvm.compiler.phases.common.CanonicalizerPhase;
43 import org.graalvm.compiler.phases.common.ConditionalEliminationPhase;
44 import org.graalvm.compiler.phases.schedule.SchedulePhase;
45 import org.graalvm.compiler.phases.tiers.PhaseContext;
46 import org.junit.Assert;
47 import org.junit.Ignore;
48 import org.junit.Test;
49
50 /**
51 * In the following tests, the scalar type system of the compiler should be complete enough to see
162 }
163
164 return ((InputStream) o).available();
165 }
166
167 @Test
168 public void test7() {
169 test("test7Snippet", "referenceSnippet7");
170 }
171
172 public static int test7Snippet(int x) {
173 return ((x & 0xff) << 10) == ((x & 0x1f) + 1) ? 0 : x;
174 }
175
176 public static int referenceSnippet7(int x) {
177 return x;
178 }
179
180 private void test(String snippet, String referenceSnippet) {
181 StructuredGraph graph = parseEager(snippet, AllowAssumptions.NO);
182 DebugContext debug = graph.getDebug();
183 debug.dump(DebugContext.BASIC_LEVEL, graph, "Graph");
184 /*
185 * When using FlowSensitiveReductionPhase instead of ConditionalEliminationPhase,
186 * tail-duplication gets activated thus resulting in a graph with more nodes than the
187 * reference graph.
188 */
189 new ConditionalEliminationPhase(false).apply(graph, new PhaseContext(getProviders()));
190 new CanonicalizerPhase().apply(graph, new PhaseContext(getProviders()));
191 // a second canonicalizer is needed to process nested MaterializeNodes
192 new CanonicalizerPhase().apply(graph, new PhaseContext(getProviders()));
193 StructuredGraph referenceGraph = parseEager(referenceSnippet, AllowAssumptions.NO);
194 new ConditionalEliminationPhase(false).apply(referenceGraph, new PhaseContext(getProviders()));
195 new CanonicalizerPhase().apply(referenceGraph, new PhaseContext(getProviders()));
196 new CanonicalizerPhase().apply(referenceGraph, new PhaseContext(getProviders()));
197 assertEquals(referenceGraph, graph);
198 }
199
200 @Override
201 protected void assertEquals(StructuredGraph expected, StructuredGraph graph) {
202 DebugContext debug = graph.getDebug();
203 if (getNodeCountExcludingUnusedConstants(expected) != getNodeCountExcludingUnusedConstants(graph)) {
204 debug.dump(DebugContext.BASIC_LEVEL, expected, "expected (node count)");
205 debug.dump(DebugContext.BASIC_LEVEL, graph, "graph (node count)");
206 Assert.fail("Graphs do not have the same number of nodes: " + expected.getNodeCount() + " vs. " + graph.getNodeCount());
207 }
208 }
209
210 public static void outputGraph(StructuredGraph graph, String message) {
211 TTY.println("========================= " + message);
212 SchedulePhase schedulePhase = new SchedulePhase(graph.getOptions());
213 schedulePhase.apply(graph);
214 ScheduleResult schedule = graph.getLastSchedule();
215 for (Block block : schedule.getCFG().getBlocks()) {
216 TTY.print("Block " + block + " ");
217 if (block == schedule.getCFG().getStartBlock()) {
218 TTY.print("* ");
219 }
220 TTY.print("-> ");
221 for (Block succ : block.getSuccessors()) {
222 TTY.print(succ + " ");
223 }
224 TTY.println();
225 for (Node node : schedule.getBlockToNodesMap().get(block)) {
228 }
229 }
230
231 private static void outputNode(Node node) {
232 TTY.print(" " + node + " (usage count: " + node.getUsageCount() + ") (inputs:");
233 for (Node input : node.inputs()) {
234 TTY.print(" " + input.toString(Verbosity.Id));
235 }
236 TTY.println(")");
237 if (node instanceof AbstractMergeNode) {
238 for (PhiNode phi : ((AbstractMergeNode) node).phis()) {
239 outputNode(phi);
240 }
241 }
242 }
243
244 private <T extends Node> void testHelper(String snippet, Class<T> clazz) {
245 StructuredGraph graph = parseEager(snippet, AllowAssumptions.NO);
246 new CanonicalizerPhase().apply(graph, new PhaseContext(getProviders()));
247 new CanonicalizerPhase().apply(graph, new PhaseContext(getProviders()));
248 DebugContext debug = graph.getDebug();
249 debug.dump(DebugContext.BASIC_LEVEL, graph, "Graph " + snippet);
250 Assert.assertFalse("shouldn't have nodes of type " + clazz, graph.getNodes().filter(clazz).iterator().hasNext());
251 }
252 }
|