001/* 002 * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved. 003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 004 * 005 * This code is free software; you can redistribute it and/or modify it 006 * under the terms of the GNU General Public License version 2 only, as 007 * published by the Free Software Foundation. 008 * 009 * This code is distributed in the hope that it will be useful, but WITHOUT 010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 011 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 012 * version 2 for more details (a copy is included in the LICENSE file that 013 * accompanied this code). 014 * 015 * You should have received a copy of the GNU General Public License version 016 * 2 along with this work; if not, write to the Free Software Foundation, 017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 018 * 019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 020 * or visit www.oracle.com if you need additional information or have any 021 * questions. 022 */ 023package com.oracle.graal.compiler.test; 024 025import org.junit.*; 026 027import com.oracle.graal.nodes.*; 028import com.oracle.graal.nodes.StructuredGraph.AllowAssumptions; 029import com.oracle.graal.nodes.calc.*; 030import com.oracle.graal.phases.common.*; 031import com.oracle.graal.phases.tiers.*; 032 033public class CompareCanonicalizerTest extends GraalCompilerTest { 034 035 private StructuredGraph getCanonicalizedGraph(String name) { 036 StructuredGraph graph = parseEager(name, AllowAssumptions.YES); 037 new CanonicalizerPhase().apply(graph, new PhaseContext(getProviders())); 038 return graph; 039 } 040 041 private static ValueNode getResult(StructuredGraph graph) { 042 assertTrue(graph.start().next() instanceof ReturnNode); 043 ReturnNode ret = (ReturnNode) graph.start().next(); 044 return ret.result(); 045 } 046 047 @Test 048 public void testCanonicalComparison() { 049 StructuredGraph referenceGraph = parseEager("referenceCanonicalComparison", AllowAssumptions.NO); 050 for (int i = 1; i < 4; i++) { 051 StructuredGraph graph = parseEager("canonicalCompare" + i, AllowAssumptions.NO); 052 assertEquals(referenceGraph, graph); 053 } 054 new CanonicalizerPhase().apply(referenceGraph, new PhaseContext(getProviders())); 055 for (int i = 1; i < 4; i++) { 056 StructuredGraph graph = getCanonicalizedGraph("canonicalCompare" + i); 057 assertEquals(referenceGraph, graph); 058 } 059 } 060 061 public static int referenceCanonicalComparison(int a, int b) { 062 if (a < b) { 063 return 1; 064 } else { 065 return 2; 066 } 067 } 068 069 public static int canonicalCompare1(int a, int b) { 070 if (a >= b) { 071 return 2; 072 } else { 073 return 1; 074 } 075 } 076 077 public static int canonicalCompare2(int a, int b) { 078 if (b > a) { 079 return 1; 080 } else { 081 return 2; 082 } 083 } 084 085 public static int canonicalCompare3(int a, int b) { 086 if (b <= a) { 087 return 2; 088 } else { 089 return 1; 090 } 091 } 092 093 @Test 094 public void testIntegerTest() { 095 for (int i = 1; i <= 4; i++) { 096 StructuredGraph graph = getCanonicalizedGraph("integerTest" + i); 097 098 ReturnNode returnNode = (ReturnNode) graph.start().next(); 099 ConditionalNode conditional = (ConditionalNode) returnNode.result(); 100 IntegerTestNode test = (IntegerTestNode) conditional.condition(); 101 ParameterNode param0 = graph.getParameter(0); 102 ParameterNode param1 = graph.getParameter(1); 103 assertTrue((test.getX() == param0 && test.getY() == param1) || (test.getX() == param1 && test.getY() == param0)); 104 } 105 } 106 107 public static boolean integerTest1(int x, int y) { 108 return (x & y) == 0; 109 } 110 111 public static boolean integerTest2(long x, long y) { 112 return 0 == (x & y); 113 } 114 115 public static boolean integerTest3(long x, long y) { 116 int c = 5; 117 return (c - 5) == (x & y); 118 } 119 120 public static boolean integerTest4(int x, int y) { 121 int c = 10; 122 return (x & y) == (10 - c); 123 } 124 125 @Test 126 public void testIntegerTestCanonicalization() { 127 ValueNode result = getResult(getCanonicalizedGraph("integerTestCanonicalization1")); 128 assertTrue(result.isConstant() && result.asJavaConstant().asLong() == 1); 129 result = getResult(getCanonicalizedGraph("integerTestCanonicalization2")); 130 assertTrue(result.isConstant() && result.asJavaConstant().asLong() == 1); 131 StructuredGraph graph = getCanonicalizedGraph("integerTestCanonicalization3"); 132 assertDeepEquals(1, graph.getNodes(ReturnNode.TYPE).count()); 133 assertTrue(graph.getNodes(ReturnNode.TYPE).first().result() instanceof ConditionalNode); 134 } 135 136 public static int integerTestCanonicalization1(boolean b) { 137 int x = b ? 128 : 256; 138 if ((x & 8) == 0) { 139 return 1; 140 } else { 141 return 2; 142 } 143 } 144 145 public static int integerTestCanonicalization2(boolean b) { 146 int x = b ? 128 : 256; 147 int y = b ? 32 : 64; 148 if ((x & y) == 0) { 149 return 1; 150 } else { 151 return 2; 152 } 153 } 154 155 public static int integerTestCanonicalization3(boolean b) { 156 int x = b ? 128 : 64; 157 int y = b ? 32 : 64; 158 if ((x & y) == 0) { 159 return 1; 160 } else { 161 return 2; 162 } 163 } 164 165}