1. Avoid unnecessary APInt construction if possible.

2. Use isStrictlyPositive() instead of isPositive() in two places where
   they need APInt value > 0 not only >=0.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@35333 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Zhou Sheng 2007-03-25 05:01:29 +00:00
parent 5df99b376f
commit 0fc5095d0e

View File

@ -2443,10 +2443,9 @@ Instruction *InstCombiner::visitUDiv(BinaryOperator &I) {
// Check to see if this is an unsigned division with an exact power of 2, // Check to see if this is an unsigned division with an exact power of 2,
// if so, convert to a right shift. // if so, convert to a right shift.
if (ConstantInt *C = dyn_cast<ConstantInt>(Op1)) { if (ConstantInt *C = dyn_cast<ConstantInt>(Op1)) {
APInt Val(C->getValue()); if (!C->isZero() && C->getValue().isPowerOf2()) // Don't break X / 0
if (Val != 0 && Val.isPowerOf2()) // Don't break X / 0
return BinaryOperator::createLShr(Op0, return BinaryOperator::createLShr(Op0,
ConstantInt::get(Op0->getType(), Val.logBase2())); ConstantInt::get(Op0->getType(), C->getValue().logBase2()));
} }
// X udiv (C1 << N), where C1 is "1<<C2" --> X >> (N+C2) // X udiv (C1 << N), where C1 is "1<<C2" --> X >> (N+C2)
@ -2673,8 +2672,7 @@ Instruction *InstCombiner::visitURem(BinaryOperator &I) {
// Turn A % (C << N), where C is 2^k, into A & ((C << N)-1) // Turn A % (C << N), where C is 2^k, into A & ((C << N)-1)
if (RHSI->getOpcode() == Instruction::Shl && if (RHSI->getOpcode() == Instruction::Shl &&
isa<ConstantInt>(RHSI->getOperand(0))) { isa<ConstantInt>(RHSI->getOperand(0))) {
APInt C1(cast<ConstantInt>(RHSI->getOperand(0))->getValue()); if (cast<ConstantInt>(RHSI->getOperand(0))->getValue().isPowerOf2()) {
if (C1.isPowerOf2()) {
Constant *N1 = ConstantInt::getAllOnesValue(I.getType()); Constant *N1 = ConstantInt::getAllOnesValue(I.getType());
Value *Add = InsertNewInstBefore(BinaryOperator::createAdd(RHSI, N1, Value *Add = InsertNewInstBefore(BinaryOperator::createAdd(RHSI, N1,
"tmp"), I); "tmp"), I);
@ -2711,7 +2709,7 @@ Instruction *InstCombiner::visitSRem(BinaryOperator &I) {
if (Value *RHSNeg = dyn_castNegVal(Op1)) if (Value *RHSNeg = dyn_castNegVal(Op1))
if (!isa<ConstantInt>(RHSNeg) || if (!isa<ConstantInt>(RHSNeg) ||
cast<ConstantInt>(RHSNeg)->getValue().isPositive()) { cast<ConstantInt>(RHSNeg)->getValue().isStrictlyPositive()) {
// X % -Y -> X % Y // X % -Y -> X % Y
AddUsesToWorkList(I); AddUsesToWorkList(I);
I.setOperand(1, RHSNeg); I.setOperand(1, RHSNeg);
@ -7466,7 +7464,7 @@ bool InstCombiner::transformConstExprCastCall(CallSite CS) {
(ParamTy->isInteger() && ActTy->isInteger() && (ParamTy->isInteger() && ActTy->isInteger() &&
ParamTy->getPrimitiveSizeInBits() >= ActTy->getPrimitiveSizeInBits()) || ParamTy->getPrimitiveSizeInBits() >= ActTy->getPrimitiveSizeInBits()) ||
(c && ParamTy->getPrimitiveSizeInBits() >= ActTy->getPrimitiveSizeInBits() (c && ParamTy->getPrimitiveSizeInBits() >= ActTy->getPrimitiveSizeInBits()
&& c->getValue().isPositive()); && c->getValue().isStrictlyPositive());
if (Callee->isDeclaration() && !isConvertible) return false; if (Callee->isDeclaration() && !isConvertible) return false;
} }