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