summaryrefslogtreecommitdiffstats
path: root/llvm/lib/Analysis/InstructionSimplify.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'llvm/lib/Analysis/InstructionSimplify.cpp')
-rw-r--r--llvm/lib/Analysis/InstructionSimplify.cpp239
1 files changed, 1 insertions, 238 deletions
diff --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp
index baf72a0bf37..047f84f3ef0 100644
--- a/llvm/lib/Analysis/InstructionSimplify.cpp
+++ b/llvm/lib/Analysis/InstructionSimplify.cpp
@@ -2473,228 +2473,6 @@ static Value *simplifyICmpWithZero(CmpInst::Predicate Pred, Value *LHS,
return nullptr;
}
-/// Many binary operators with a constant operand have an easy-to-compute
-/// range of outputs. This can be used to fold a comparison to always true or
-/// always false.
-static void setLimitsForBinOp(BinaryOperator &BO, APInt &Lower, APInt &Upper,
- const InstrInfoQuery &IIQ) {
- unsigned Width = Lower.getBitWidth();
- const APInt *C;
- switch (BO.getOpcode()) {
- case Instruction::Add:
- if (match(BO.getOperand(1), m_APInt(C)) && !C->isNullValue()) {
- // FIXME: If we have both nuw and nsw, we should reduce the range further.
- if (IIQ.hasNoUnsignedWrap(cast<OverflowingBinaryOperator>(&BO))) {
- // 'add nuw x, C' produces [C, UINT_MAX].
- Lower = *C;
- } else if (IIQ.hasNoSignedWrap(cast<OverflowingBinaryOperator>(&BO))) {
- if (C->isNegative()) {
- // 'add nsw x, -C' produces [SINT_MIN, SINT_MAX - C].
- Lower = APInt::getSignedMinValue(Width);
- Upper = APInt::getSignedMaxValue(Width) + *C + 1;
- } else {
- // 'add nsw x, +C' produces [SINT_MIN + C, SINT_MAX].
- Lower = APInt::getSignedMinValue(Width) + *C;
- Upper = APInt::getSignedMaxValue(Width) + 1;
- }
- }
- }
- break;
-
- case Instruction::And:
- if (match(BO.getOperand(1), m_APInt(C)))
- // 'and x, C' produces [0, C].
- Upper = *C + 1;
- break;
-
- case Instruction::Or:
- if (match(BO.getOperand(1), m_APInt(C)))
- // 'or x, C' produces [C, UINT_MAX].
- Lower = *C;
- break;
-
- case Instruction::AShr:
- if (match(BO.getOperand(1), m_APInt(C)) && C->ult(Width)) {
- // 'ashr x, C' produces [INT_MIN >> C, INT_MAX >> C].
- Lower = APInt::getSignedMinValue(Width).ashr(*C);
- Upper = APInt::getSignedMaxValue(Width).ashr(*C) + 1;
- } else if (match(BO.getOperand(0), m_APInt(C))) {
- unsigned ShiftAmount = Width - 1;
- if (!C->isNullValue() && IIQ.isExact(&BO))
- ShiftAmount = C->countTrailingZeros();
- if (C->isNegative()) {
- // 'ashr C, x' produces [C, C >> (Width-1)]
- Lower = *C;
- Upper = C->ashr(ShiftAmount) + 1;
- } else {
- // 'ashr C, x' produces [C >> (Width-1), C]
- Lower = C->ashr(ShiftAmount);
- Upper = *C + 1;
- }
- }
- break;
-
- case Instruction::LShr:
- if (match(BO.getOperand(1), m_APInt(C)) && C->ult(Width)) {
- // 'lshr x, C' produces [0, UINT_MAX >> C].
- Upper = APInt::getAllOnesValue(Width).lshr(*C) + 1;
- } else if (match(BO.getOperand(0), m_APInt(C))) {
- // 'lshr C, x' produces [C >> (Width-1), C].
- unsigned ShiftAmount = Width - 1;
- if (!C->isNullValue() && IIQ.isExact(&BO))
- ShiftAmount = C->countTrailingZeros();
- Lower = C->lshr(ShiftAmount);
- Upper = *C + 1;
- }
- break;
-
- case Instruction::Shl:
- if (match(BO.getOperand(0), m_APInt(C))) {
- if (IIQ.hasNoUnsignedWrap(&BO)) {
- // 'shl nuw C, x' produces [C, C << CLZ(C)]
- Lower = *C;
- Upper = Lower.shl(Lower.countLeadingZeros()) + 1;
- } else if (BO.hasNoSignedWrap()) { // TODO: What if both nuw+nsw?
- if (C->isNegative()) {
- // 'shl nsw C, x' produces [C << CLO(C)-1, C]
- unsigned ShiftAmount = C->countLeadingOnes() - 1;
- Lower = C->shl(ShiftAmount);
- Upper = *C + 1;
- } else {
- // 'shl nsw C, x' produces [C, C << CLZ(C)-1]
- unsigned ShiftAmount = C->countLeadingZeros() - 1;
- Lower = *C;
- Upper = C->shl(ShiftAmount) + 1;
- }
- }
- }
- break;
-
- case Instruction::SDiv:
- if (match(BO.getOperand(1), m_APInt(C))) {
- APInt IntMin = APInt::getSignedMinValue(Width);
- APInt IntMax = APInt::getSignedMaxValue(Width);
- if (C->isAllOnesValue()) {
- // 'sdiv x, -1' produces [INT_MIN + 1, INT_MAX]
- // where C != -1 and C != 0 and C != 1
- Lower = IntMin + 1;
- Upper = IntMax + 1;
- } else if (C->countLeadingZeros() < Width - 1) {
- // 'sdiv x, C' produces [INT_MIN / C, INT_MAX / C]
- // where C != -1 and C != 0 and C != 1
- Lower = IntMin.sdiv(*C);
- Upper = IntMax.sdiv(*C);
- if (Lower.sgt(Upper))
- std::swap(Lower, Upper);
- Upper = Upper + 1;
- assert(Upper != Lower && "Upper part of range has wrapped!");
- }
- } else if (match(BO.getOperand(0), m_APInt(C))) {
- if (C->isMinSignedValue()) {
- // 'sdiv INT_MIN, x' produces [INT_MIN, INT_MIN / -2].
- Lower = *C;
- Upper = Lower.lshr(1) + 1;
- } else {
- // 'sdiv C, x' produces [-|C|, |C|].
- Upper = C->abs() + 1;
- Lower = (-Upper) + 1;
- }
- }
- break;
-
- case Instruction::UDiv:
- if (match(BO.getOperand(1), m_APInt(C)) && !C->isNullValue()) {
- // 'udiv x, C' produces [0, UINT_MAX / C].
- Upper = APInt::getMaxValue(Width).udiv(*C) + 1;
- } else if (match(BO.getOperand(0), m_APInt(C))) {
- // 'udiv C, x' produces [0, C].
- Upper = *C + 1;
- }
- break;
-
- case Instruction::SRem:
- if (match(BO.getOperand(1), m_APInt(C))) {
- // 'srem x, C' produces (-|C|, |C|).
- Upper = C->abs();
- Lower = (-Upper) + 1;
- }
- break;
-
- case Instruction::URem:
- if (match(BO.getOperand(1), m_APInt(C)))
- // 'urem x, C' produces [0, C).
- Upper = *C;
- break;
-
- default:
- break;
- }
-}
-
-/// Some intrinsics with a constant operand have an easy-to-compute range of
-/// outputs. This can be used to fold a comparison to always true or always
-/// false.
-static void setLimitsForIntrinsic(IntrinsicInst &II, APInt &Lower,
- APInt &Upper) {
- unsigned Width = Lower.getBitWidth();
- const APInt *C;
- switch (II.getIntrinsicID()) {
- case Intrinsic::uadd_sat:
- // uadd.sat(x, C) produces [C, UINT_MAX].
- if (match(II.getOperand(0), m_APInt(C)) ||
- match(II.getOperand(1), m_APInt(C)))
- Lower = *C;
- break;
- case Intrinsic::sadd_sat:
- if (match(II.getOperand(0), m_APInt(C)) ||
- match(II.getOperand(1), m_APInt(C))) {
- if (C->isNegative()) {
- // sadd.sat(x, -C) produces [SINT_MIN, SINT_MAX + (-C)].
- Lower = APInt::getSignedMinValue(Width);
- Upper = APInt::getSignedMaxValue(Width) + *C + 1;
- } else {
- // sadd.sat(x, +C) produces [SINT_MIN + C, SINT_MAX].
- Lower = APInt::getSignedMinValue(Width) + *C;
- Upper = APInt::getSignedMaxValue(Width) + 1;
- }
- }
- break;
- case Intrinsic::usub_sat:
- // usub.sat(C, x) produces [0, C].
- if (match(II.getOperand(0), m_APInt(C)))
- Upper = *C + 1;
- // usub.sat(x, C) produces [0, UINT_MAX - C].
- else if (match(II.getOperand(1), m_APInt(C)))
- Upper = APInt::getMaxValue(Width) - *C + 1;
- break;
- case Intrinsic::ssub_sat:
- if (match(II.getOperand(0), m_APInt(C))) {
- if (C->isNegative()) {
- // ssub.sat(-C, x) produces [SINT_MIN, -SINT_MIN + (-C)].
- Lower = APInt::getSignedMinValue(Width);
- Upper = *C - APInt::getSignedMinValue(Width) + 1;
- } else {
- // ssub.sat(+C, x) produces [-SINT_MAX + C, SINT_MAX].
- Lower = *C - APInt::getSignedMaxValue(Width);
- Upper = APInt::getSignedMaxValue(Width) + 1;
- }
- } else if (match(II.getOperand(1), m_APInt(C))) {
- if (C->isNegative()) {
- // ssub.sat(x, -C) produces [SINT_MIN - (-C), SINT_MAX]:
- Lower = APInt::getSignedMinValue(Width) - *C;
- Upper = APInt::getSignedMaxValue(Width) + 1;
- } else {
- // ssub.sat(x, +C) produces [SINT_MIN, SINT_MAX - C].
- Lower = APInt::getSignedMinValue(Width);
- Upper = APInt::getSignedMaxValue(Width) - *C + 1;
- }
- }
- break;
- default:
- break;
- }
-}
-
static Value *simplifyICmpWithConstant(CmpInst::Predicate Pred, Value *LHS,
Value *RHS, const InstrInfoQuery &IIQ) {
Type *ITy = GetCompareTy(RHS); // The return type.
@@ -2722,22 +2500,7 @@ static Value *simplifyICmpWithConstant(CmpInst::Predicate Pred, Value *LHS,
if (RHS_CR.isFullSet())
return ConstantInt::getTrue(ITy);
- // Find the range of possible values for binary operators.
- unsigned Width = C->getBitWidth();
- APInt Lower = APInt(Width, 0);
- APInt Upper = APInt(Width, 0);
- if (auto *BO = dyn_cast<BinaryOperator>(LHS))
- setLimitsForBinOp(*BO, Lower, Upper, IIQ);
- else if (auto *II = dyn_cast<IntrinsicInst>(LHS))
- setLimitsForIntrinsic(*II, Lower, Upper);
-
- ConstantRange LHS_CR =
- Lower != Upper ? ConstantRange(Lower, Upper) : ConstantRange(Width, true);
-
- if (auto *I = dyn_cast<Instruction>(LHS))
- if (auto *Ranges = IIQ.getMetadata(I, LLVMContext::MD_range))
- LHS_CR = LHS_CR.intersectWith(getConstantRangeFromMetadata(*Ranges));
-
+ ConstantRange LHS_CR = computeConstantRange(LHS, IIQ.UseInstrInfo);
if (!LHS_CR.isFullSet()) {
if (RHS_CR.contains(LHS_CR))
return ConstantInt::getTrue(ITy);
OpenPOWER on IntegriCloud