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