1 /*
   2  * Copyright (c) 2011, 2015, 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 org.graalvm.compiler.debug.DebugContext;
  26 import org.graalvm.compiler.graph.Node;
  27 import org.graalvm.compiler.nodes.ConstantNode;
  28 import org.graalvm.compiler.nodes.FrameState;
  29 import org.graalvm.compiler.nodes.IfNode;
  30 import org.graalvm.compiler.nodes.ParameterNode;
  31 import org.graalvm.compiler.nodes.StructuredGraph;
  32 import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions;
  33 import org.graalvm.compiler.nodes.spi.LoweringTool;
  34 import org.graalvm.compiler.phases.OptimisticOptimizations;
  35 import org.graalvm.compiler.phases.common.CanonicalizerPhase;
  36 import org.graalvm.compiler.phases.common.FloatingReadPhase;
  37 import org.graalvm.compiler.phases.common.GuardLoweringPhase;
  38 import org.graalvm.compiler.phases.common.LoweringPhase;
  39 import org.graalvm.compiler.phases.tiers.MidTierContext;
  40 import org.graalvm.compiler.phases.tiers.PhaseContext;
  41 import org.junit.Test;
  42 
  43 /**
  44  * In the following tests, the usages of local variable "a" are replaced with the integer constant
  45  * 0. Then canonicalization is applied and it is verified that the resulting graph is equal to the
  46  * graph of the method that just has a "return 1" statement in it.
  47  */
  48 public class IfCanonicalizerTest extends GraalCompilerTest {
  49 
  50     private static final String REFERENCE_SNIPPET = "referenceSnippet";
  51 
  52     @SuppressWarnings("all")
  53     public static int referenceSnippet(int a) {
  54         return 1;
  55     }
  56 
  57     @Test
  58     public void test1() {
  59         test("test1Snippet");
  60     }
  61 
  62     @SuppressWarnings("all")
  63     public static int test1Snippet(int a) {
  64         if (a == 0) {
  65             return 1;
  66         } else {
  67             return 2;
  68         }
  69     }
  70 
  71     @Test
  72     public void test2() {
  73         test("test2Snippet");
  74     }
  75 
  76     @SuppressWarnings("all")
  77     public static int test2Snippet(int a) {
  78         if (a == 0) {
  79             if (a == 0) {
  80                 if (a == 0) {
  81                     return 1;
  82                 }
  83             }
  84         } else {
  85             return 2;
  86         }
  87         return 3;
  88     }
  89 
  90     @Test
  91     public void test3() {
  92         test("test3Snippet");
  93     }
  94 
  95     @SuppressWarnings("all")
  96     public static int test3Snippet(int a) {
  97         if (a == 0) {
  98             if (a != 1) {
  99                 if (a == 1) {
 100                     return 3;
 101                 } else {
 102                     if (a >= 0) {
 103                         if (a <= 0) {
 104                             if (a > -1) {
 105                                 if (a < 1) {
 106                                     return 1;
 107                                 }
 108                             }
 109                         }
 110                     }
 111                 }
 112             }
 113         } else {
 114             return 2;
 115         }
 116         return 3;
 117     }
 118 
 119     @Test
 120     public void test4() {
 121         test("test4Snippet");
 122     }
 123 
 124     public static int test4Snippet(int a) {
 125         if (a == 0) {
 126             return 1;
 127         }
 128         return 1;
 129     }
 130 
 131     @Test
 132     public void test5() {
 133         test("test5Snippet");
 134     }
 135 
 136     public static int test5Snippet(int a) {
 137         int val = 2;
 138         if (a == 0) {
 139             val = 1;
 140         }
 141         if (a * (3 + val) == 0) {
 142             return 1;
 143         }
 144         return 1;
 145     }
 146 
 147     @Test
 148     public void test6() {
 149         testCombinedIf("test6Snippet", 4);
 150         test("test6Snippet", new int[]{0});
 151     }
 152 
 153     public static int test6Snippet(int[] a) {
 154         int i = a[0];
 155         if (i >= 0 && i < a.length) {
 156             return a[i];
 157         }
 158         return 1;
 159     }
 160 
 161     @Test
 162     public void test7() {
 163         testCombinedIf("test7Snippet", 1);
 164         test("test7Snippet", -1);
 165     }
 166 
 167     public static int test7Snippet(int v) {
 168         if (v >= 0 && v < 1024) {
 169             return v + 1;
 170         }
 171         return v - 1;
 172     }
 173 
 174     @Test
 175     public void test8() {
 176         testCombinedIf("test8Snippet", 1);
 177         test("test8Snippet", -1);
 178     }
 179 
 180     public static int test8Snippet(int v) {
 181         if (v >= 0 && v <= 1024) {
 182             return v + 1;
 183         }
 184         return v - 1;
 185     }
 186 
 187     @Test
 188     public void test9() {
 189         testCombinedIf("test9Snippet", 2);
 190         test("test9Snippet", -1);
 191         test("test9Snippet", 1025);
 192     }
 193 
 194     public static int test9Snippet(int n) {
 195         return (n < 0) ? 1 : (n >= 1024) ? 1024 : n + 1;
 196     }
 197 
 198     @Test
 199     public void test10() {
 200         // Exercise NormalizeCompareNode with long values
 201         test("test10Snippet", 0, 1);
 202     }
 203 
 204     public static long test10Snippet(int x, int y) {
 205         return (x < y) ? -1L : ((x == y) ? 0L : 1L);
 206     }
 207 
 208     @Test
 209     public void test11() {
 210         test("test11Snippet", 0, 1);
 211     }
 212 
 213     public static long test11Snippet(int x, int y) {
 214         long normalizeCompare = normalizeCompareLong(x, y);
 215         if (normalizeCompare == 0) {
 216             return 5;
 217         }
 218         return 1;
 219     }
 220 
 221     private static Long normalizeCompareLong(int x, int y) {
 222         return (x < y) ? -1L : ((x == y) ? 0L : 1L);
 223     }
 224 
 225     private void testCombinedIf(String snippet, int count) {
 226         StructuredGraph graph = parseEager(snippet, AllowAssumptions.YES);
 227         PhaseContext context = new PhaseContext(getProviders());
 228         new LoweringPhase(new CanonicalizerPhase(), LoweringTool.StandardLoweringStage.HIGH_TIER).apply(graph, context);
 229         new FloatingReadPhase().apply(graph);
 230         MidTierContext midContext = new MidTierContext(getProviders(), getTargetProvider(), OptimisticOptimizations.ALL, graph.getProfilingInfo());
 231         new GuardLoweringPhase().apply(graph, midContext);
 232         new LoweringPhase(new CanonicalizerPhase(), LoweringTool.StandardLoweringStage.MID_TIER).apply(graph, midContext);
 233         new CanonicalizerPhase().apply(graph, context);
 234         assertDeepEquals(count, graph.getNodes().filter(IfNode.class).count());
 235     }
 236 
 237     private void test(String snippet) {
 238         StructuredGraph graph = parseEager(snippet, AllowAssumptions.YES);
 239         DebugContext debug = graph.getDebug();
 240         ParameterNode param = graph.getNodes(ParameterNode.TYPE).iterator().next();
 241         ConstantNode constant = ConstantNode.forInt(0, graph);
 242         for (Node n : param.usages().snapshot()) {
 243             if (!(n instanceof FrameState)) {
 244                 n.replaceFirstInput(param, constant);
 245             }
 246         }
 247         debug.dump(DebugContext.BASIC_LEVEL, graph, "Graph");
 248         new CanonicalizerPhase().apply(graph, new PhaseContext(getProviders()));
 249         for (FrameState fs : param.usages().filter(FrameState.class).snapshot()) {
 250             fs.replaceFirstInput(param, null);
 251             param.safeDelete();
 252         }
 253         StructuredGraph referenceGraph = parseEager(REFERENCE_SNIPPET, AllowAssumptions.YES);
 254         assertEquals(referenceGraph, graph);
 255     }
 256 }