< prev index next >

src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.nodes/src/org/graalvm/compiler/nodes/calc/SignedDivNode.java

Print this page




  46     protected SignedDivNode(NodeClass<? extends SignedDivNode> c, ValueNode x, ValueNode y) {
  47         super(c, IntegerStamp.OPS.getDiv().foldStamp(x.stamp(), y.stamp()), Op.DIV, Type.SIGNED, x, y);
  48     }
  49 
  50     @Override
  51     public boolean inferStamp() {
  52         return updateStamp(IntegerStamp.OPS.getDiv().foldStamp(getX().stamp(), getY().stamp()));
  53     }
  54 
  55     @Override
  56     public ValueNode canonical(CanonicalizerTool tool, ValueNode forX, ValueNode forY) {
  57         if (forX.isConstant() && forY.isConstant()) {
  58             @SuppressWarnings("hiding")
  59             long y = forY.asJavaConstant().asLong();
  60             if (y == 0) {
  61                 return this; // this will trap, can not canonicalize
  62             }
  63             return ConstantNode.forIntegerStamp(stamp(), forX.asJavaConstant().asLong() / y);
  64         } else if (forY.isConstant()) {
  65             long c = forY.asJavaConstant().asLong();
  66             if (c == 1) {
  67                 return forX;
  68             }
  69             if (c == -1) {
  70                 return NegateNode.create(forX);
  71             }
  72             long abs = Math.abs(c);
  73             if (CodeUtil.isPowerOf2(abs) && forX.stamp() instanceof IntegerStamp) {
  74                 ValueNode dividend = forX;
  75                 IntegerStamp stampX = (IntegerStamp) forX.stamp();
  76                 int log2 = CodeUtil.log2(abs);
  77                 // no rounding if dividend is positive or if its low bits are always 0
  78                 if (stampX.canBeNegative() || (stampX.upMask() & (abs - 1)) != 0) {
  79                     int bits = PrimitiveStamp.getBits(stamp());
  80                     RightShiftNode sign = new RightShiftNode(forX, ConstantNode.forInt(bits - 1));
  81                     UnsignedRightShiftNode round = new UnsignedRightShiftNode(sign, ConstantNode.forInt(bits - log2));
  82                     dividend = BinaryArithmeticNode.add(dividend, round);
  83                 }
  84                 RightShiftNode shift = new RightShiftNode(dividend, ConstantNode.forInt(log2));
  85                 if (c < 0) {
  86                     return NegateNode.create(shift);
  87                 }
  88                 return shift;
  89             }
  90         }
  91 
  92         // Convert the expression ((a - a % b) / b) into (a / b).
  93         if (forX instanceof SubNode) {
  94             SubNode integerSubNode = (SubNode) forX;
  95             if (integerSubNode.getY() instanceof SignedRemNode) {
  96                 SignedRemNode integerRemNode = (SignedRemNode) integerSubNode.getY();
  97                 if (integerSubNode.stamp().isCompatible(this.stamp()) && integerRemNode.stamp().isCompatible(this.stamp()) && integerSubNode.getX() == integerRemNode.getX() &&
  98                                 forY == integerRemNode.getY()) {
  99                     SignedDivNode sd = new SignedDivNode(integerSubNode.getX(), forY);
 100                     sd.stateBefore = this.stateBefore;
 101                     return sd;
 102                 }
 103             }
 104         }
 105 
 106         if (next() instanceof SignedDivNode) {
 107             NodeClass<?> nodeClass = getNodeClass();
 108             if (next().getClass() == this.getClass() && nodeClass.equalInputs(this, next()) && valueEquals(next())) {
 109                 return next();
 110             }
 111         }
 112 
 113         return this;




























 114     }
 115 
 116     @Override
 117     public void generate(NodeLIRBuilderTool gen) {
 118         gen.setResult(this, gen.getLIRGeneratorTool().getArithmetic().emitDiv(gen.operand(getX()), gen.operand(getY()), gen.state(this)));
 119     }
 120 }


  46     protected SignedDivNode(NodeClass<? extends SignedDivNode> c, ValueNode x, ValueNode y) {
  47         super(c, IntegerStamp.OPS.getDiv().foldStamp(x.stamp(), y.stamp()), Op.DIV, Type.SIGNED, x, y);
  48     }
  49 
  50     @Override
  51     public boolean inferStamp() {
  52         return updateStamp(IntegerStamp.OPS.getDiv().foldStamp(getX().stamp(), getY().stamp()));
  53     }
  54 
  55     @Override
  56     public ValueNode canonical(CanonicalizerTool tool, ValueNode forX, ValueNode forY) {
  57         if (forX.isConstant() && forY.isConstant()) {
  58             @SuppressWarnings("hiding")
  59             long y = forY.asJavaConstant().asLong();
  60             if (y == 0) {
  61                 return this; // this will trap, can not canonicalize
  62             }
  63             return ConstantNode.forIntegerStamp(stamp(), forX.asJavaConstant().asLong() / y);
  64         } else if (forY.isConstant()) {
  65             long c = forY.asJavaConstant().asLong();
  66             ValueNode v = canonical(forX, c);
  67             if (v != null) {
  68                 return v;




















  69             }
  70         }
  71 
  72         // Convert the expression ((a - a % b) / b) into (a / b).
  73         if (forX instanceof SubNode) {
  74             SubNode integerSubNode = (SubNode) forX;
  75             if (integerSubNode.getY() instanceof SignedRemNode) {
  76                 SignedRemNode integerRemNode = (SignedRemNode) integerSubNode.getY();
  77                 if (integerSubNode.stamp().isCompatible(this.stamp()) && integerRemNode.stamp().isCompatible(this.stamp()) && integerSubNode.getX() == integerRemNode.getX() &&
  78                                 forY == integerRemNode.getY()) {
  79                     SignedDivNode sd = new SignedDivNode(integerSubNode.getX(), forY);
  80                     sd.stateBefore = this.stateBefore;
  81                     return sd;
  82                 }
  83             }
  84         }
  85 
  86         if (next() instanceof SignedDivNode) {
  87             NodeClass<?> nodeClass = getNodeClass();
  88             if (next().getClass() == this.getClass() && nodeClass.equalInputs(this, next()) && valueEquals(next())) {
  89                 return next();
  90             }
  91         }
  92 
  93         return this;
  94     }
  95 
  96     public static ValueNode canonical(ValueNode forX, long c) {
  97         if (c == 1) {
  98             return forX;
  99         }
 100         if (c == -1) {
 101             return NegateNode.create(forX);
 102         }
 103         long abs = Math.abs(c);
 104         if (CodeUtil.isPowerOf2(abs) && forX.stamp() instanceof IntegerStamp) {
 105             ValueNode dividend = forX;
 106             IntegerStamp stampX = (IntegerStamp) forX.stamp();
 107             int log2 = CodeUtil.log2(abs);
 108             // no rounding if dividend is positive or if its low bits are always 0
 109             if (stampX.canBeNegative() || (stampX.upMask() & (abs - 1)) != 0) {
 110                 int bits = PrimitiveStamp.getBits(forX.stamp());
 111                 RightShiftNode sign = new RightShiftNode(forX, ConstantNode.forInt(bits - 1));
 112                 UnsignedRightShiftNode round = new UnsignedRightShiftNode(sign, ConstantNode.forInt(bits - log2));
 113                 dividend = BinaryArithmeticNode.add(dividend, round);
 114             }
 115             RightShiftNode shift = new RightShiftNode(dividend, ConstantNode.forInt(log2));
 116             if (c < 0) {
 117                 return NegateNode.create(shift);
 118             }
 119             return shift;
 120         }
 121         return null;
 122     }
 123 
 124     @Override
 125     public void generate(NodeLIRBuilderTool gen) {
 126         gen.setResult(this, gen.getLIRGeneratorTool().getArithmetic().emitDiv(gen.operand(getX()), gen.operand(getY()), gen.state(this)));
 127     }
 128 }
< prev index next >