1 /*
   2  * Copyright (c) 2012, 2014, 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 
  24 
  25 package org.graalvm.compiler.core.test;
  26 
  27 import org.graalvm.compiler.nodes.StructuredGraph;
  28 import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions;
  29 import org.graalvm.compiler.nodes.ValueNode;
  30 import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderContext;
  31 import org.graalvm.compiler.nodes.graphbuilderconf.InlineInvokePlugin;
  32 import org.graalvm.compiler.phases.OptimisticOptimizations;
  33 import org.graalvm.compiler.phases.common.CanonicalizerPhase;
  34 import org.graalvm.compiler.phases.common.FrameStateAssignmentPhase;
  35 import org.graalvm.compiler.phases.common.GuardLoweringPhase;
  36 import org.graalvm.compiler.phases.tiers.MidTierContext;
  37 import org.graalvm.compiler.phases.tiers.PhaseContext;
  38 import org.junit.Ignore;
  39 import org.junit.Test;
  40 
  41 import jdk.vm.ci.meta.ResolvedJavaMethod;
  42 
  43 public class CompareCanonicalizerTest3 extends GraalCompilerTest {
  44 
  45     @SuppressWarnings("unused") private static int sink0;
  46     @SuppressWarnings("unused") private static int sink1;
  47 
  48     @Ignore("Subword input cannot be trusted.")
  49     @Test
  50     public void test00() {
  51         assertCanonicallyEqual("integerTestCanonicalization00", "referenceSnippet00");
  52     }
  53 
  54     public static void integerTestCanonicalization00(char a) {
  55         if (a - 1 < a) {
  56             sink1 = 0;
  57         } else {
  58             sink0 = -1;
  59         }
  60     }
  61 
  62     @SuppressWarnings("unused")
  63     public static void referenceSnippet00(char a) {
  64         sink1 = 0;
  65     }
  66 
  67     @Ignore("Needs better stamp support for unsigned ranges")
  68     @Test
  69     public void test01() {
  70         assertCanonicallyEqual("integerTestCanonicalization01", "referenceSnippet01");
  71     }
  72 
  73     public static void integerTestCanonicalization01(char a) {
  74         if (Integer.compareUnsigned(a - 1, a) < 0) {
  75             sink1 = 0;
  76         } else {
  77             sink0 = -1;
  78         }
  79     }
  80 
  81     public static void referenceSnippet01(char a) {
  82         if (a != 0) {
  83             sink1 = 0;
  84         } else {
  85             sink0 = -1;
  86         }
  87     }
  88 
  89     @Ignore("Needs better stamp support for unsigned ranges")
  90     @Test
  91     public void test1() {
  92         assertCanonicallyEqual("integerTestCanonicalization1", "referenceSnippet1");
  93     }
  94 
  95     public static void integerTestCanonicalization1(char a) {
  96         if (Integer.compareUnsigned(a - 2, a) < 0) {
  97             sink1 = 0;
  98         } else {
  99             sink0 = -1;
 100         }
 101     }
 102 
 103     public static void referenceSnippet1(char a) {
 104         if (Integer.compareUnsigned(a, 2) >= 0) {
 105             sink1 = 0;
 106         } else {
 107             sink0 = -1;
 108         }
 109     }
 110 
 111     @Test
 112     public void test2() {
 113         assertCanonicallyEqual("integerTestCanonicalization2", "referenceSnippet2");
 114     }
 115 
 116     public static void integerTestCanonicalization2(int a) {
 117         if (a - 1 < a) {
 118             sink1 = 0;
 119         } else {
 120             sink0 = -1;
 121         }
 122     }
 123 
 124     public static void referenceSnippet2(int a) {
 125         if (a != Integer.MIN_VALUE) {
 126             sink1 = 0;
 127         } else {
 128             sink0 = -1;
 129         }
 130     }
 131 
 132     @Test
 133     public void test3() {
 134         assertCanonicallyEqual("integerTestCanonicalization3", "referenceSnippet3");
 135     }
 136 
 137     public static void integerTestCanonicalization3(int a) {
 138         if (a - 2 < a) {
 139             sink1 = 0;
 140         } else {
 141             sink0 = -1;
 142         }
 143     }
 144 
 145     public static void referenceSnippet3(int a) {
 146         if (a >= Integer.MIN_VALUE + 2) {
 147             sink1 = 0;
 148         } else {
 149             sink0 = -1;
 150         }
 151     }
 152 
 153     @Test
 154     public void test4() {
 155         assertCanonicallyEqual("integerTestCanonicalization4", "referenceSnippet4");
 156     }
 157 
 158     public static void integerTestCanonicalization4(int a) {
 159         if (a + 1 < a) {
 160             sink1 = 0;
 161         } else {
 162             sink0 = -1;
 163         }
 164     }
 165 
 166     public static void referenceSnippet4(int a) {
 167         if (a == Integer.MAX_VALUE) {
 168             sink1 = 0;
 169         } else {
 170             sink0 = -1;
 171         }
 172     }
 173 
 174     @Test
 175     public void test5() {
 176         assertCanonicallyEqual("integerTestCanonicalization5", "referenceSnippet5");
 177     }
 178 
 179     public static void integerTestCanonicalization5(int a) {
 180         if (a + 2 < a) {
 181             sink1 = 0;
 182         } else {
 183             sink0 = -1;
 184         }
 185     }
 186 
 187     public static void referenceSnippet5(int a) {
 188         if (a > Integer.MAX_VALUE - 2) {
 189             sink1 = 0;
 190         } else {
 191             sink0 = -1;
 192         }
 193     }
 194 
 195     @Test
 196     public void test6() {
 197         assertCanonicallyEqual("integerTestCanonicalization6", "referenceSnippet6");
 198     }
 199 
 200     public static void integerTestCanonicalization6(int a) {
 201         if (a < a + 1) {
 202             sink1 = 0;
 203         } else {
 204             sink0 = -1;
 205         }
 206     }
 207 
 208     public static void referenceSnippet6(int a) {
 209         if (a != Integer.MAX_VALUE) {
 210             sink1 = 0;
 211         } else {
 212             sink0 = -1;
 213         }
 214     }
 215 
 216     @Test
 217     public void test7() {
 218         assertCanonicallyEqual("integerTestCanonicalization7", "referenceSnippet7");
 219     }
 220 
 221     public static void integerTestCanonicalization7(int a) {
 222         if (a < a + 2) {
 223             sink1 = 0;
 224         } else {
 225             sink0 = -1;
 226         }
 227     }
 228 
 229     public static void referenceSnippet7(int a) {
 230         if (a <= Integer.MAX_VALUE - 2) {
 231             sink1 = 0;
 232         } else {
 233             sink0 = -1;
 234         }
 235     }
 236 
 237     protected void assertCanonicallyEqual(String snippet, String reference) {
 238         StructuredGraph graph = parseEager(snippet, AllowAssumptions.YES);
 239         PhaseContext context = new PhaseContext(getProviders());
 240         CanonicalizerPhase canonicalizer = new CanonicalizerPhase();
 241         canonicalizer.apply(graph, context);
 242         new GuardLoweringPhase().apply(graph, new MidTierContext(getProviders(), getTargetProvider(), OptimisticOptimizations.ALL, graph.getProfilingInfo()));
 243         new FrameStateAssignmentPhase().apply(graph);
 244         canonicalizer.apply(graph, context);
 245         StructuredGraph referenceGraph = parseEager(reference, AllowAssumptions.YES);
 246         canonicalizer.apply(referenceGraph, context);
 247         canonicalizer.apply(referenceGraph, context);
 248         assertEquals(referenceGraph, graph, true, true);
 249     }
 250 
 251     @Override
 252     protected InlineInvokePlugin.InlineInfo bytecodeParserShouldInlineInvoke(GraphBuilderContext b, ResolvedJavaMethod method, ValueNode[] args) {
 253         return InlineInvokePlugin.InlineInfo.createStandardInlineInfo(method);
 254     }
 255 }