1. Make more use of APInt::getHighBitsSet/getLowBitsSet.

2. Let APInt variable do the binary operation stuff instead of using
   ConstantExpr::getXXX.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@35450 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Zhou Sheng 2007-03-29 08:15:12 +00:00
parent b0b6c76ffe
commit 290bec5c15

View File

@ -1921,9 +1921,8 @@ Instruction *InstCombiner::visitAdd(BinaryOperator &I) {
if ((RHSVal == CFF80Val && XorRHS->getValue() == C0080Val) || if ((RHSVal == CFF80Val && XorRHS->getValue() == C0080Val) ||
(RHSVal == C0080Val && XorRHS->getValue() == CFF80Val)) { (RHSVal == C0080Val && XorRHS->getValue() == CFF80Val)) {
// This is a sign extend if the top bits are known zero. // This is a sign extend if the top bits are known zero.
APInt Mask(APInt::getAllOnesValue(TySizeBits)); if (!MaskedValueIsZero(XorLHS,
Mask <<= Size; APInt::getHighBitsSet(TySizeBits, TySizeBits - Size)))
if (!MaskedValueIsZero(XorLHS, Mask))
Size = 0; // Not a sign ext, but can't be any others either. Size = 0; // Not a sign ext, but can't be any others either.
break; break;
} }
@ -2984,11 +2983,14 @@ Instruction *InstCombiner::OptAndOp(Instruction *Op,
// We know that the AND will not produce any of the bits shifted in, so if // We know that the AND will not produce any of the bits shifted in, so if
// the anded constant includes them, clear them now! // the anded constant includes them, clear them now!
// //
Constant *AllOne = ConstantInt::getAllOnesValue(AndRHS->getType()); uint32_t BitWidth = AndRHS->getType()->getBitWidth();
Constant *ShlMask = ConstantExpr::getShl(AllOne, OpRHS); uint32_t OpRHSVal = OpRHS->getValue().getActiveBits() > 64 ?
Constant *CI = ConstantExpr::getAnd(AndRHS, ShlMask); BitWidth : OpRHS->getZExtValue();
APInt ShlMask(APInt::getHighBitsSet(BitWidth, BitWidth-OpRHSVal));
ConstantInt *CI = ConstantInt::get(AndRHS->getValue() & ShlMask);
if (CI == ShlMask) { // Masking out bits that the shift already masks if (CI->getValue() == ShlMask) {
// Masking out bits that the shift already masks
return ReplaceInstUsesWith(TheAnd, Op); // No need for the and. return ReplaceInstUsesWith(TheAnd, Op); // No need for the and.
} else if (CI != AndRHS) { // Reducing bits set in and. } else if (CI != AndRHS) { // Reducing bits set in and.
TheAnd.setOperand(1, CI); TheAnd.setOperand(1, CI);
@ -3002,11 +3004,14 @@ Instruction *InstCombiner::OptAndOp(Instruction *Op,
// the anded constant includes them, clear them now! This only applies to // the anded constant includes them, clear them now! This only applies to
// unsigned shifts, because a signed shr may bring in set bits! // unsigned shifts, because a signed shr may bring in set bits!
// //
Constant *AllOne = ConstantInt::getAllOnesValue(AndRHS->getType()); uint32_t BitWidth = AndRHS->getType()->getBitWidth();
Constant *ShrMask = ConstantExpr::getLShr(AllOne, OpRHS); uint32_t OpRHSVal = OpRHS->getValue().getActiveBits() > 64 ?
Constant *CI = ConstantExpr::getAnd(AndRHS, ShrMask); BitWidth : OpRHS->getZExtValue();
APInt ShrMask(APInt::getLowBitsSet(BitWidth, BitWidth - OpRHSVal));
ConstantInt *CI = ConstantInt::get(AndRHS->getValue() & ShrMask);
if (CI == ShrMask) { // Masking out bits that the shift already masks. if (CI->getValue() == ShrMask) {
// Masking out bits that the shift already masks.
return ReplaceInstUsesWith(TheAnd, Op); return ReplaceInstUsesWith(TheAnd, Op);
} else if (CI != AndRHS) { } else if (CI != AndRHS) {
TheAnd.setOperand(1, CI); // Reduce bits set in and cst. TheAnd.setOperand(1, CI); // Reduce bits set in and cst.
@ -3019,9 +3024,11 @@ Instruction *InstCombiner::OptAndOp(Instruction *Op,
// See if this is shifting in some sign extension, then masking it out // See if this is shifting in some sign extension, then masking it out
// with an and. // with an and.
if (Op->hasOneUse()) { if (Op->hasOneUse()) {
Constant *AllOne = ConstantInt::getAllOnesValue(AndRHS->getType()); uint32_t BitWidth = AndRHS->getType()->getBitWidth();
Constant *ShrMask = ConstantExpr::getLShr(AllOne, OpRHS); uint32_t OpRHSVal = OpRHS->getValue().getActiveBits() > 64 ?
Constant *C = ConstantExpr::getAnd(AndRHS, ShrMask); BitWidth : OpRHS->getZExtValue();
APInt ShrMask(APInt::getLowBitsSet(BitWidth, BitWidth - OpRHSVal));
Constant *C = ConstantInt::get(AndRHS->getValue() & ShrMask);
if (C == AndRHS) { // Masking out bits shifted in. if (C == AndRHS) { // Masking out bits shifted in.
// (Val ashr C1) & C2 -> (Val lshr C1) & C2 // (Val ashr C1) & C2 -> (Val lshr C1) & C2
// Make the argument unsigned. // Make the argument unsigned.
@ -3140,8 +3147,7 @@ Value *InstCombiner::FoldLogicalPlusAnd(Value *LHS, Value *RHS,
unsigned MB = 0, ME = 0; unsigned MB = 0, ME = 0;
if (isRunOfOnes(Mask, MB, ME)) { // begin/end bit of run, inclusive if (isRunOfOnes(Mask, MB, ME)) { // begin/end bit of run, inclusive
uint32_t BitWidth = cast<IntegerType>(RHS->getType())->getBitWidth(); uint32_t BitWidth = cast<IntegerType>(RHS->getType())->getBitWidth();
APInt Mask(APInt::getAllOnesValue(BitWidth)); APInt Mask(APInt::getLowBitsSet(BitWidth, MB-1));
Mask = Mask.lshr(BitWidth-MB+1);
if (MaskedValueIsZero(RHS, Mask)) if (MaskedValueIsZero(RHS, Mask))
break; break;
} }
@ -4786,11 +4792,9 @@ Instruction *InstCombiner::visitICmpInst(ICmpInst &I) {
int ShAmtVal = Ty->getPrimitiveSizeInBits()-ShAmt->getZExtValue(); int ShAmtVal = Ty->getPrimitiveSizeInBits()-ShAmt->getZExtValue();
if (ShAmtVal < 0) ShAmtVal = 0; // Out of range shift. if (ShAmtVal < 0) ShAmtVal = 0; // Out of range shift.
Constant *OShAmt = ConstantInt::get(AndTy, ShAmtVal); uint32_t BitWidth = AndTy->getPrimitiveSizeInBits();
Constant *ShVal = if ((APInt::getHighBitsSet(BitWidth, BitWidth-ShAmtVal) &
ConstantExpr::getShl(ConstantInt::getAllOnesValue(AndTy), AndCST->getValue()) == 0)
OShAmt);
if (ConstantExpr::getAnd(ShVal, AndCST)->isNullValue())
CanFold = true; CanFold = true;
} }
@ -4925,7 +4929,7 @@ Instruction *InstCombiner::visitICmpInst(ICmpInst &I) {
unsigned ShAmtVal = (unsigned)ShAmt->getZExtValue(); unsigned ShAmtVal = (unsigned)ShAmt->getZExtValue();
// Otherwise strength reduce the shift into an and. // Otherwise strength reduce the shift into an and.
APInt Val(APInt::getAllOnesValue(TypeBits).shl(ShAmtVal)); APInt Val(APInt::getHighBitsSet(TypeBits, TypeBits - ShAmtVal));
Constant *Mask = ConstantInt::get(Val); Constant *Mask = ConstantInt::get(Val);
Instruction *AndI = Instruction *AndI =