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 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 52 * the relation between the different conditions. 53 */ 54 public class TypeSystemTest extends GraalCompilerTest { 55 56 @Test 57 public void test3() { 58 test("test3Snippet", "referenceSnippet3"); 59 } 60 61 public static int referenceSnippet3(Object o) { 62 if (o == null) { 63 return 1; 64 } else { 65 return 2; 66 } 67 } 68 69 @SuppressWarnings("unused") 70 public static int test3Snippet(Object o) { 71 if (o == null) { 72 if (o != null) { 73 return 3; 74 } else { 75 return 1; 76 } 77 } else { 78 return 2; 79 } 80 } 81 82 @Test 83 public void test4() { 84 test("test4Snippet", "referenceSnippet3"); 85 } 86 87 @SuppressWarnings("unused") 88 public static int test4Snippet(Object o) { 89 if (o == null) { 90 Object o2 = Integer.class; 91 if (o == o2) { 92 return 3; 93 } else { 94 return 1; 95 } 96 } else { 97 return 2; 98 } 99 } 100 101 @Test 102 @Ignore 103 public void test5() { 104 test("test5Snippet", "referenceSnippet5"); 105 } 106 107 public static int referenceSnippet5(Object o, Object a) { 108 if (o == null) { 109 if (a == Integer.class) { 110 return 1; 111 } 112 } else { 113 if (a == Double.class) { 114 return 11; 115 } 116 } 117 if (a == Integer.class) { 118 return 3; 119 } 120 return 5; 121 } 122 123 @SuppressWarnings("unused") 124 public static int test5Snippet(Object o, Object a) { 125 if (o == null) { 126 if (a == Integer.class) { 127 if (a == null) { 128 return 10; 129 } 130 return 1; 131 } 132 } else { 133 if (a == Double.class) { 134 if (a != null) { 135 return 11; 136 } 137 return 2; 138 } 139 } 140 if (a == Integer.class) { 141 return 3; 142 } 143 return 5; 144 } 145 146 @Test 147 public void test6() { 148 testHelper("test6Snippet", InstanceOfNode.class); 149 } 150 151 public static int test6Snippet(int i) throws IOException { 152 Object o = null; 153 154 if (i == 5) { 155 o = new FileInputStream("asdf"); 156 } 157 if (i < 10) { 158 o = new ByteArrayInputStream(new byte[]{1, 2, 3}); 159 } 160 if (i > 0) { 161 o = new BufferedInputStream(null); 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)) { 224 outputNode(node); 225 } 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 }