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.junit.Assert;
  32 import org.junit.Ignore;
  33 import org.junit.Test;
  34 
  35 import org.graalvm.compiler.debug.Debug;
  36 import org.graalvm.compiler.debug.TTY;
  37 import org.graalvm.compiler.graph.Node;
  38 import org.graalvm.compiler.nodeinfo.Verbosity;
  39 import org.graalvm.compiler.nodes.AbstractMergeNode;
  40 import org.graalvm.compiler.nodes.PhiNode;
  41 import org.graalvm.compiler.nodes.StructuredGraph;
  42 import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions;
  43 import org.graalvm.compiler.nodes.StructuredGraph.ScheduleResult;
  44 import org.graalvm.compiler.nodes.cfg.Block;
  45 import org.graalvm.compiler.nodes.java.InstanceOfNode;
  46 import org.graalvm.compiler.phases.common.CanonicalizerPhase;
  47 import org.graalvm.compiler.phases.common.DominatorConditionalEliminationPhase;
  48 import org.graalvm.compiler.phases.schedule.SchedulePhase;
  49 import org.graalvm.compiler.phases.tiers.PhaseContext;
  50 
  51 /**
  52  * In the following tests, the scalar type system of the compiler should be complete enough to see
  53  * the relation between the different conditions.
  54  */
  55 public class TypeSystemTest extends GraalCompilerTest {
  56 
  57     @Test
  58     public void test3() {
  59         test("test3Snippet", "referenceSnippet3");
  60     }
  61 
  62     public static int referenceSnippet3(Object o) {
  63         if (o == null) {
  64             return 1;
  65         } else {
  66             return 2;
  67         }
  68     }
  69 
  70     @SuppressWarnings("unused")
  71     public static int test3Snippet(Object o) {
  72         if (o == null) {
  73             if (o != null) {
  74                 return 3;
  75             } else {
  76                 return 1;
  77             }
  78         } else {
  79             return 2;
  80         }
  81     }
  82 
  83     @Test
  84     public void test4() {
  85         test("test4Snippet", "referenceSnippet3");
  86     }
  87 
  88     @SuppressWarnings("unused")
  89     public static int test4Snippet(Object o) {
  90         if (o == null) {
  91             Object o2 = Integer.class;
  92             if (o == o2) {
  93                 return 3;
  94             } else {
  95                 return 1;
  96             }
  97         } else {
  98             return 2;
  99         }
 100     }
 101 
 102     @Test
 103     @Ignore
 104     public void test5() {
 105         test("test5Snippet", "referenceSnippet5");
 106     }
 107 
 108     public static int referenceSnippet5(Object o, Object a) {
 109         if (o == null) {
 110             if (a == Integer.class) {
 111                 return 1;
 112             }
 113         } else {
 114             if (a == Double.class) {
 115                 return 11;
 116             }
 117         }
 118         if (a == Integer.class) {
 119             return 3;
 120         }
 121         return 5;
 122     }
 123 
 124     @SuppressWarnings("unused")
 125     public static int test5Snippet(Object o, Object a) {
 126         if (o == null) {
 127             if (a == Integer.class) {
 128                 if (a == null) {
 129                     return 10;
 130                 }
 131                 return 1;
 132             }
 133         } else {
 134             if (a == Double.class) {
 135                 if (a != null) {
 136                     return 11;
 137                 }
 138                 return 2;
 139             }
 140         }
 141         if (a == Integer.class) {
 142             return 3;
 143         }
 144         return 5;
 145     }
 146 
 147     @Test
 148     public void test6() {
 149         testHelper("test6Snippet", InstanceOfNode.class);
 150     }
 151 
 152     public static int test6Snippet(int i) throws IOException {
 153         Object o = null;
 154 
 155         if (i == 5) {
 156             o = new FileInputStream("asdf");
 157         }
 158         if (i < 10) {
 159             o = new ByteArrayInputStream(new byte[]{1, 2, 3});
 160         }
 161         if (i > 0) {
 162             o = new BufferedInputStream(null);
 163         }
 164 
 165         return ((InputStream) o).available();
 166     }
 167 
 168     @Test
 169     public void test7() {
 170         test("test7Snippet", "referenceSnippet7");
 171     }
 172 
 173     public static int test7Snippet(int x) {
 174         return ((x & 0xff) << 10) == ((x & 0x1f) + 1) ? 0 : x;
 175     }
 176 
 177     public static int referenceSnippet7(int x) {
 178         return x;
 179     }
 180 
 181     private void test(String snippet, String referenceSnippet) {
 182         StructuredGraph graph = parseEager(snippet, AllowAssumptions.NO);
 183         Debug.dump(Debug.BASIC_LOG_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 DominatorConditionalEliminationPhase(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 DominatorConditionalEliminationPhase(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         if (getNodeCountExcludingUnusedConstants(expected) != getNodeCountExcludingUnusedConstants(graph)) {
 203             Debug.dump(Debug.BASIC_LOG_LEVEL, expected, "expected (node count)");
 204             Debug.dump(Debug.BASIC_LOG_LEVEL, graph, "graph (node count)");
 205             Assert.fail("Graphs do not have the same number of nodes: " + expected.getNodeCount() + " vs. " + graph.getNodeCount());
 206         }
 207     }
 208 
 209     public static void outputGraph(StructuredGraph graph, String message) {
 210         TTY.println("========================= " + message);
 211         SchedulePhase schedulePhase = new SchedulePhase();
 212         schedulePhase.apply(graph);
 213         ScheduleResult schedule = graph.getLastSchedule();
 214         for (Block block : schedule.getCFG().getBlocks()) {
 215             TTY.print("Block " + block + " ");
 216             if (block == schedule.getCFG().getStartBlock()) {
 217                 TTY.print("* ");
 218             }
 219             TTY.print("-> ");
 220             for (Block succ : block.getSuccessors()) {
 221                 TTY.print(succ + " ");
 222             }
 223             TTY.println();
 224             for (Node node : schedule.getBlockToNodesMap().get(block)) {
 225                 outputNode(node);
 226             }
 227         }
 228     }
 229 
 230     private static void outputNode(Node node) {
 231         TTY.print("  " + node + "    (usage count: " + node.getUsageCount() + ") (inputs:");
 232         for (Node input : node.inputs()) {
 233             TTY.print(" " + input.toString(Verbosity.Id));
 234         }
 235         TTY.println(")");
 236         if (node instanceof AbstractMergeNode) {
 237             for (PhiNode phi : ((AbstractMergeNode) node).phis()) {
 238                 outputNode(phi);
 239             }
 240         }
 241     }
 242 
 243     private <T extends Node> void testHelper(String snippet, Class<T> clazz) {
 244         StructuredGraph graph = parseEager(snippet, AllowAssumptions.NO);
 245         new CanonicalizerPhase().apply(graph, new PhaseContext(getProviders()));
 246         new CanonicalizerPhase().apply(graph, new PhaseContext(getProviders()));
 247         Debug.dump(Debug.BASIC_LOG_LEVEL, graph, "Graph " + snippet);
 248         Assert.assertFalse("shouldn't have nodes of type " + clazz, graph.getNodes().filter(clazz).iterator().hasNext());
 249     }
 250 }