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