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 
  24 
  25 package org.graalvm.compiler.core.test;
  26 
  27 import org.graalvm.compiler.debug.DebugContext;
  28 import org.graalvm.compiler.nodes.StructuredGraph;
  29 import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions;
  30 import org.graalvm.compiler.phases.common.CanonicalizerPhase;
  31 import org.graalvm.compiler.phases.tiers.PhaseContext;
  32 import org.junit.Test;
  33 
  34 public class StraighteningTest extends GraalCompilerTest {
  35 
  36     private static final String REFERENCE_SNIPPET = "ref";
  37 
  38     public static boolean ref(int a, int b) {
  39         return a == b;
  40     }
  41 
  42     public static boolean test1Snippet(int a, int b) {
  43         int c = a;
  44         if (c == b) {
  45             c = 0x55;
  46         }
  47         if (c != 0x55) {
  48             return false;
  49         }
  50         return true;
  51     }
  52 
  53     public static boolean test3Snippet(int a, int b) {
  54         int val = (int) System.currentTimeMillis();
  55         int c = val + 1;
  56         if (a == b) {
  57             c = val;
  58         }
  59         if (c != val) {
  60             return false;
  61         }
  62         return true;
  63     }
  64 
  65     public static boolean test2Snippet(int a, int b) {
  66         int c;
  67         if (a == b) {
  68             c = 1;
  69         } else {
  70             c = 0;
  71         }
  72         return c == 1;
  73     }
  74 
  75     @Test(expected = AssertionError.class)
  76     public void test1() {
  77         test("test1Snippet");
  78     }
  79 
  80     public void test2() {
  81         test("test2Snippet");
  82     }
  83 
  84     @Test(expected = AssertionError.class)
  85     public void test3() {
  86         test("test3Snippet");
  87     }
  88 
  89     private void test(final String snippet) {
  90         // No debug scope to reduce console noise for @Test(expected = ...) tests
  91         StructuredGraph graph = parseEager(snippet, AllowAssumptions.YES);
  92         DebugContext debug = graph.getDebug();
  93         debug.dump(DebugContext.BASIC_LEVEL, graph, "Graph");
  94         new CanonicalizerPhase().apply(graph, new PhaseContext(getProviders()));
  95         StructuredGraph referenceGraph = parseEager(REFERENCE_SNIPPET, AllowAssumptions.YES);
  96         assertEquals(referenceGraph, graph);
  97     }
  98 }