55 public ValueNode canonical(CanonicalizerTool tool, ValueNode forX, ValueNode forY) { 56 if (forX.isConstant() && forY.isConstant()) { 57 @SuppressWarnings("hiding") 58 long y = forY.asJavaConstant().asLong(); 59 if (y == 0) { 60 return this; // this will trap, can not canonicalize 61 } 62 return ConstantNode.forIntegerStamp(stamp(), forX.asJavaConstant().asLong() % y); 63 } else if (forY.isConstant() && forX.stamp() instanceof IntegerStamp && forY.stamp() instanceof IntegerStamp) { 64 long constY = forY.asJavaConstant().asLong(); 65 IntegerStamp xStamp = (IntegerStamp) forX.stamp(); 66 IntegerStamp yStamp = (IntegerStamp) forY.stamp(); 67 if (constY < 0 && constY != CodeUtil.minValue(yStamp.getBits())) { 68 return new SignedRemNode(forX, ConstantNode.forIntegerStamp(yStamp, -constY)).canonical(tool); 69 } 70 71 if (constY == 1) { 72 return ConstantNode.forIntegerStamp(stamp(), 0); 73 } else if (CodeUtil.isPowerOf2(constY)) { 74 if (xStamp.isPositive()) { 75 return new AndNode(forX, ConstantNode.forIntegerStamp(stamp(), constY - 1)); 76 } else if (xStamp.isNegative()) { 77 return new NegateNode(new AndNode(new NegateNode(forX), ConstantNode.forIntegerStamp(stamp(), constY - 1))); 78 } else { 79 return new ConditionalNode(IntegerLessThanNode.create(forX, ConstantNode.forIntegerStamp(forX.stamp(), 0)), 80 new NegateNode(new AndNode(new NegateNode(forX), ConstantNode.forIntegerStamp(stamp(), constY - 1))), 81 new AndNode(forX, ConstantNode.forIntegerStamp(stamp(), constY - 1))); 82 } 83 } 84 } 85 return this; 86 } 87 88 @Override 89 public void generate(NodeLIRBuilderTool gen) { 90 gen.setResult(this, gen.getLIRGeneratorTool().getArithmetic().emitRem(gen.operand(getX()), gen.operand(getY()), gen.state(this))); 91 } 92 } | 55 public ValueNode canonical(CanonicalizerTool tool, ValueNode forX, ValueNode forY) { 56 if (forX.isConstant() && forY.isConstant()) { 57 @SuppressWarnings("hiding") 58 long y = forY.asJavaConstant().asLong(); 59 if (y == 0) { 60 return this; // this will trap, can not canonicalize 61 } 62 return ConstantNode.forIntegerStamp(stamp(), forX.asJavaConstant().asLong() % y); 63 } else if (forY.isConstant() && forX.stamp() instanceof IntegerStamp && forY.stamp() instanceof IntegerStamp) { 64 long constY = forY.asJavaConstant().asLong(); 65 IntegerStamp xStamp = (IntegerStamp) forX.stamp(); 66 IntegerStamp yStamp = (IntegerStamp) forY.stamp(); 67 if (constY < 0 && constY != CodeUtil.minValue(yStamp.getBits())) { 68 return new SignedRemNode(forX, ConstantNode.forIntegerStamp(yStamp, -constY)).canonical(tool); 69 } 70 71 if (constY == 1) { 72 return ConstantNode.forIntegerStamp(stamp(), 0); 73 } else if (CodeUtil.isPowerOf2(constY)) { 74 if (xStamp.isPositive()) { 75 // x & (y - 1) 76 return new AndNode(forX, ConstantNode.forIntegerStamp(stamp(), constY - 1)); 77 } else if (xStamp.isNegative()) { 78 // -((-x) & (y - 1)) 79 return new NegateNode(new AndNode(new NegateNode(forX), ConstantNode.forIntegerStamp(stamp(), constY - 1))); 80 } else { 81 // x - ((x / y) << log2(y)) 82 return SubNode.create(forX, LeftShiftNode.create(SignedDivNode.canonical(forX, constY), ConstantNode.forInt(CodeUtil.log2(constY)))); 83 } 84 } 85 } 86 return this; 87 } 88 89 @Override 90 public void generate(NodeLIRBuilderTool gen) { 91 gen.setResult(this, gen.getLIRGeneratorTool().getArithmetic().emitRem(gen.operand(getX()), gen.operand(getY()), gen.state(this))); 92 } 93 } |