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