1 /* 2 * Copyright (c) 2012, 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.nodes.ParameterNode; 28 import org.graalvm.compiler.nodes.ReturnNode; 29 import org.graalvm.compiler.nodes.StructuredGraph; 30 import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions; 31 import org.graalvm.compiler.nodes.ValueNode; 32 import org.graalvm.compiler.nodes.calc.ConditionalNode; 33 import org.graalvm.compiler.nodes.calc.IntegerTestNode; 34 import org.graalvm.compiler.phases.common.CanonicalizerPhase; 35 import org.junit.Test; 36 37 public class CompareCanonicalizerTest extends GraalCompilerTest { 38 39 private StructuredGraph getCanonicalizedGraph(String name) { 40 StructuredGraph graph = parseEager(name, AllowAssumptions.YES); 41 new CanonicalizerPhase().apply(graph, getProviders()); 42 return graph; 43 } 44 45 private static ValueNode getResult(StructuredGraph graph) { 46 assertTrue(graph.start().next() instanceof ReturnNode); 47 ReturnNode ret = (ReturnNode) graph.start().next(); 48 return ret.result(); 49 } 50 51 @Test 52 public void testCanonicalComparison() { 53 StructuredGraph referenceGraph = parseEager("referenceCanonicalComparison", AllowAssumptions.NO); 54 for (int i = 1; i < 4; i++) { 55 StructuredGraph graph = parseEager("canonicalCompare" + i, AllowAssumptions.NO); 56 assertEquals(referenceGraph, graph); 57 } 58 new CanonicalizerPhase().apply(referenceGraph, getProviders()); 59 for (int i = 1; i < 4; i++) { 60 StructuredGraph graph = getCanonicalizedGraph("canonicalCompare" + i); 61 assertEquals(referenceGraph, graph); 62 } 63 } 64 65 public static int referenceCanonicalComparison(int a, int b) { 66 if (a < b) { 67 return 1; 68 } else { 69 return 2; 70 } 71 } 72 73 public static int canonicalCompare1(int a, int b) { 74 if (a >= b) { 75 return 2; 76 } else { 77 return 1; 78 } 79 } 80 81 public static int canonicalCompare2(int a, int b) { 82 if (b > a) { 83 return 1; 84 } else { 85 return 2; 86 } 87 } 88 89 public static int canonicalCompare3(int a, int b) { 90 if (b <= a) { 91 return 2; 92 } else { 93 return 1; 94 } 95 } 96 97 @Test 98 public void testIntegerTest() { 99 for (int i = 1; i <= 4; i++) { 100 StructuredGraph graph = getCanonicalizedGraph("integerTest" + i); 101 102 ReturnNode returnNode = (ReturnNode) graph.start().next(); 103 ConditionalNode conditional = (ConditionalNode) returnNode.result(); 104 IntegerTestNode test = (IntegerTestNode) conditional.condition(); 105 ParameterNode param0 = graph.getParameter(0); 106 ParameterNode param1 = graph.getParameter(1); 107 assertTrue((test.getX() == param0 && test.getY() == param1) || (test.getX() == param1 && test.getY() == param0)); 108 } 109 } 110 111 public static boolean integerTest1(int x, int y) { 112 return (x & y) == 0; 113 } 114 115 public static boolean integerTest2(long x, long y) { 116 return 0 == (x & y); 117 } 118 119 public static boolean integerTest3(long x, long y) { 120 int c = 5; 121 return (c - 5) == (x & y); 122 } 123 124 public static boolean integerTest4(int x, int y) { 125 int c = 10; 126 return (x & y) == (10 - c); 127 } 128 129 @Test 130 public void testIntegerTestCanonicalization() { 131 ValueNode result = getResult(getCanonicalizedGraph("integerTestCanonicalization1")); 132 assertTrue(result.isConstant() && result.asJavaConstant().asLong() == 1); 133 result = getResult(getCanonicalizedGraph("integerTestCanonicalization2")); 134 assertTrue(result.isConstant() && result.asJavaConstant().asLong() == 1); 135 StructuredGraph graph = getCanonicalizedGraph("integerTestCanonicalization3"); 136 assertDeepEquals(1, graph.getNodes(ReturnNode.TYPE).count()); 137 assertTrue(graph.getNodes(ReturnNode.TYPE).first().result() instanceof ConditionalNode); 138 } 139 140 public static int integerTestCanonicalization1(boolean b) { 141 int x = b ? 128 : 256; 142 if ((x & 8) == 0) { 143 return 1; 144 } else { 145 return 2; 146 } 147 } 148 149 public static int integerTestCanonicalization2(boolean b) { 150 int x = b ? 128 : 256; 151 int y = b ? 32 : 64; 152 if ((x & y) == 0) { 153 return 1; 154 } else { 155 return 2; 156 } 157 } 158 159 public static int integerTestCanonicalization3(boolean b) { 160 int x = b ? 128 : 64; 161 int y = b ? 32 : 64; 162 if ((x & y) == 0) { 163 return 1; 164 } else { 165 return 2; 166 } 167 } 168 169 }