1 /* 2 * Copyright (c) 2015, 2016, 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.sparc; 24 25 import org.graalvm.compiler.core.common.type.IntegerStamp; 26 import org.graalvm.compiler.core.common.type.Stamp; 27 import org.graalvm.compiler.debug.GraalError; 28 import org.graalvm.compiler.graph.Node; 29 import org.graalvm.compiler.nodes.ConstantNode; 30 import org.graalvm.compiler.nodes.StructuredGraph; 31 import org.graalvm.compiler.nodes.ValueNode; 32 import org.graalvm.compiler.nodes.calc.CompareNode; 33 import org.graalvm.compiler.nodes.calc.SignExtendNode; 34 import org.graalvm.compiler.phases.Phase; 35 36 import jdk.vm.ci.code.CodeUtil; 37 import jdk.vm.ci.meta.JavaConstant; 38 39 /** 40 * SPARC only supports 32 and 64 bit integer compare. 41 * 42 * This phase turns {@link CompareNode}s which have {@link IntegerStamp} as input and its bit-width 43 * is not 32 or 64 bit into either a 32 or 64 bit compare by sign extending the input values. 44 * 45 * Why we do this in the HIR instead in the LIR? This enables the pattern matcher 46 * {@link SPARCNodeMatchRules#signExtend(SignExtendNode, org.graalvm.compiler.nodes.memory.Access)} 47 * to do it's job and replace loads with sign extending ones. 48 */ 49 public class SPARCIntegerCompareCanonicalizationPhase extends Phase { 50 @Override 51 protected void run(StructuredGraph graph) { 52 for (Node n : graph.getNodes()) { 53 if (n instanceof CompareNode) { 54 CompareNode enode = (CompareNode) n; 55 min32(enode, enode.getX()); 56 min32(enode, enode.getY()); 57 } 58 } 59 } 60 61 private static void min32(CompareNode enode, ValueNode v) { 62 Stamp s = v.stamp(); 63 if (s instanceof IntegerStamp) { 64 int bits = ((IntegerStamp) s).getBits(); 65 if (bits != 32 && bits != 64) { 66 if (bits <= 32) { 67 bits = 32; 68 } else { 69 bits = 64; 70 } 71 ValueNode replacement; 72 if (v instanceof ConstantNode) { 73 JavaConstant newConst; 74 if (bits == 32) { 75 newConst = JavaConstant.forInt(v.asJavaConstant().asInt()); 76 } else if (bits == 64) { 77 newConst = JavaConstant.forLong(v.asJavaConstant().asLong()); 78 } else { 79 throw GraalError.shouldNotReachHere(); 80 } 81 long mask = CodeUtil.mask(bits); 82 replacement = v.graph().addOrUnique(new ConstantNode(newConst, IntegerStamp.stampForMask(bits, newConst.asLong() & mask, newConst.asLong() & mask))); 83 } else { 84 replacement = v.graph().addOrUnique(new SignExtendNode(v, bits)); 85 } 86 v.replaceAtUsages(replacement, x -> x == enode); 87 } 88 } 89 } 90 }