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