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 org.junit.Test;
  26 
  27 import org.graalvm.compiler.graph.IterableNodeType;
  28 import org.graalvm.compiler.graph.Node;
  29 import org.graalvm.compiler.nodes.StructuredGraph;
  30 import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions;
  31 import org.graalvm.compiler.phases.common.CanonicalizerPhase;
  32 import org.graalvm.compiler.phases.tiers.PhaseContext;
  33 
  34 public class ReassociateAndCanonicalTest extends GraalCompilerTest {
  35 
  36     public static int rnd = (int) (Math.random() * 100);
  37 
  38     @Test
  39     public void test1() {
  40         test("test1Snippet", "ref1Snippet");
  41     }
  42 
  43     public static int test1Snippet() {
  44         return 1 + (rnd + 2);
  45     }
  46 
  47     public static int ref1Snippet() {
  48         return rnd + 3;
  49     }
  50 
  51     @Test
  52     public void test2() {
  53         test("test2Snippet", "ref2Snippet");
  54     }
  55 
  56     public static int test2Snippet() {
  57         return rnd + 3;
  58     }
  59 
  60     public static int ref2Snippet() {
  61         return (rnd + 2) + 1;
  62     }
  63 
  64     @Test
  65     public void test3() {
  66         test("test3Snippet", "ref3Snippet");
  67     }
  68 
  69     public static int test3Snippet() {
  70         return rnd + 3;
  71     }
  72 
  73     public static int ref3Snippet() {
  74         return 1 + (2 + rnd);
  75     }
  76 
  77     @Test
  78     public void test4() {
  79         test("test4Snippet", "ref4Snippet");
  80     }
  81 
  82     public static int test4Snippet() {
  83         return rnd + 3;
  84     }
  85 
  86     public static int ref4Snippet() {
  87         return (2 + rnd) + 1;
  88     }
  89 
  90     @Test
  91     public void test5() {
  92         test("test5Snippet", "ref5Snippet");
  93     }
  94 
  95     public static int test5Snippet() {
  96         return -1 - rnd;
  97     }
  98 
  99     public static int ref5Snippet() {
 100         return 1 - (rnd + 2);
 101     }
 102 
 103     @Test
 104     public void test6() {
 105         test("test6Snippet", "ref6Snippet");
 106     }
 107 
 108     public static int test6Snippet() {
 109         return rnd + 1;
 110     }
 111 
 112     public static int ref6Snippet() {
 113         return (rnd + 2) - 1;
 114     }
 115 
 116     @Test
 117     public void test7() {
 118         test("test7Snippet", "ref7Snippet");
 119     }
 120 
 121     public static int test7Snippet() {
 122         return -1 - rnd;
 123     }
 124 
 125     public static int ref7Snippet() {
 126         return 1 - (2 + rnd);
 127     }
 128 
 129     @Test
 130     public void test8() {
 131         test("test8Snippet", "ref8Snippet");
 132     }
 133 
 134     public static int test8Snippet() {
 135         return rnd + 1;
 136     }
 137 
 138     public static int ref8Snippet() {
 139         return (2 + rnd) - 1;
 140     }
 141 
 142     @Test
 143     public void test9() {
 144         test("test9Snippet", "ref9Snippet");
 145     }
 146 
 147     public static int test9Snippet() {
 148         return rnd - 1;
 149     }
 150 
 151     public static int ref9Snippet() {
 152         return 1 + (rnd - 2);
 153     }
 154 
 155     @Test
 156     public void test10() {
 157         test("test10Snippet", "ref10Snippet");
 158     }
 159 
 160     public static int test10Snippet() {
 161         return rnd - 1;
 162     }
 163 
 164     public static int ref10Snippet() {
 165         return (rnd - 2) + 1;
 166     }
 167 
 168     @Test
 169     public void test11() {
 170         test("test11Snippet", "ref11Snippet");
 171     }
 172 
 173     public static int test11Snippet() {
 174         return -rnd + 3;
 175     }
 176 
 177     public static int ref11Snippet() {
 178         return 1 + (2 - rnd);
 179     }
 180 
 181     @Test
 182     public void test12() {
 183         test("test12Snippet", "ref12Snippet");
 184     }
 185 
 186     public static int test12Snippet() {
 187         return -rnd + 3;
 188     }
 189 
 190     public static int ref12Snippet() {
 191         return (2 - rnd) + 1;
 192     }
 193 
 194     @Test
 195     public void test13() {
 196         test("test13Snippet", "ref13Snippet");
 197     }
 198 
 199     public static int test13Snippet() {
 200         return -rnd + 3;
 201     }
 202 
 203     public static int ref13Snippet() {
 204         return 1 - (rnd - 2);
 205     }
 206 
 207     @Test
 208     public void test14() {
 209         test("test14Snippet", "ref14Snippet");
 210     }
 211 
 212     public static int test14Snippet() {
 213         return rnd - 3;
 214     }
 215 
 216     public static int ref14Snippet() {
 217         return (rnd - 2) - 1;
 218     }
 219 
 220     @Test
 221     public void test15() {
 222         test("test15Snippet", "ref15Snippet");
 223     }
 224 
 225     public static int test15Snippet() {
 226         return rnd + -1;
 227     }
 228 
 229     public static int ref15Snippet() {
 230         return 1 - (2 - rnd);
 231     }
 232 
 233     @Test
 234     public void test16() {
 235         test("test16Snippet", "ref16Snippet");
 236     }
 237 
 238     public static int test16Snippet() {
 239         return -rnd + 1;
 240     }
 241 
 242     public static int ref16Snippet() {
 243         return (2 - rnd) - 1;
 244     }
 245 
 246     private <T extends Node & IterableNodeType> void test(String test, String ref) {
 247         StructuredGraph testGraph = parseEager(test, AllowAssumptions.NO);
 248         new CanonicalizerPhase().apply(testGraph, new PhaseContext(getProviders()));
 249         StructuredGraph refGraph = parseEager(ref, AllowAssumptions.NO);
 250         new CanonicalizerPhase().apply(refGraph, new PhaseContext(getProviders()));
 251         assertEquals(testGraph, refGraph);
 252     }
 253 }