Index: llvm/lib/Analysis/InstructionSimplify.cpp =================================================================== --- llvm/lib/Analysis/InstructionSimplify.cpp +++ llvm/lib/Analysis/InstructionSimplify.cpp @@ -2826,44 +2826,6 @@ return nullptr; } -static Value *simplifyICmpWithAbsNabs(CmpInst::Predicate Pred, Value *Op0, - Value *Op1) { - // We need a comparison with a constant. - const APInt *C; - if (!match(Op1, m_APInt(C))) - return nullptr; - - // matchSelectPattern returns the negation part of an abs pattern in SP1. - // If the negate has an NSW flag, abs(INT_MIN) is undefined. Without that - // constraint, we can't make a contiguous range for the result of abs. - ICmpInst::Predicate AbsPred = ICmpInst::BAD_ICMP_PREDICATE; - Value *SP0, *SP1; - SelectPatternFlavor SPF = matchSelectPattern(Op0, SP0, SP1).Flavor; - if (SPF == SelectPatternFlavor::SPF_ABS && - cast(SP1)->hasNoSignedWrap()) - // The result of abs(X) is >= 0 (with nsw). - AbsPred = ICmpInst::ICMP_SGE; - if (SPF == SelectPatternFlavor::SPF_NABS) - // The result of -abs(X) is <= 0. - AbsPred = ICmpInst::ICMP_SLE; - - if (AbsPred == ICmpInst::BAD_ICMP_PREDICATE) - return nullptr; - - // If there is no intersection between abs/nabs and the range of this icmp, - // the icmp must be false. If the abs/nabs range is a subset of the icmp - // range, the icmp must be true. - APInt Zero = APInt::getNullValue(C->getBitWidth()); - ConstantRange AbsRange = ConstantRange::makeExactICmpRegion(AbsPred, Zero); - ConstantRange CmpRange = ConstantRange::makeExactICmpRegion(Pred, *C); - if (AbsRange.intersectWith(CmpRange).isEmptySet()) - return getFalse(GetCompareTy(Op0)); - if (CmpRange.contains(AbsRange)) - return getTrue(GetCompareTy(Op0)); - - return nullptr; -} - /// Simplify integer comparisons where at least one operand of the compare /// matches an integer min/max idiom. static Value *simplifyICmpWithMinMax(CmpInst::Predicate Pred, Value *LHS, @@ -3295,9 +3257,6 @@ if (Value *V = simplifyICmpWithMinMax(Pred, LHS, RHS, Q, MaxRecurse)) return V; - if (Value *V = simplifyICmpWithAbsNabs(Pred, LHS, RHS)) - return V; - // Simplify comparisons of related pointers using a powerful, recursive // GEP-walk when we have target data available.. if (LHS->getType()->isPointerTy()) Index: llvm/lib/Analysis/ValueTracking.cpp =================================================================== --- llvm/lib/Analysis/ValueTracking.cpp +++ llvm/lib/Analysis/ValueTracking.cpp @@ -5653,6 +5653,57 @@ } } +static void setLimitsForSelect(const SelectInst &SI, APInt &Lower, + APInt &Upper) { + const Value *LHS, *RHS; + SelectPatternResult R = matchSelectPattern(&SI, LHS, RHS); + if (R.Flavor == SPF_UNKNOWN) + return; + + unsigned BitWidth = SI.getType()->getScalarSizeInBits(); + + // matchSelectPattern() returns the negation part of an abs pattern in RHS. + // If the negate has an NSW flag, abs(INT_MIN) is undefined. Without that + // constraint, we can't make a contiguous range for the result of abs. + if (R.Flavor == SelectPatternFlavor::SPF_ABS && + cast(RHS)->hasNoSignedWrap()) { + // The result of abs(X) is >= 0 (with nsw). + Lower = APInt::getNullValue(BitWidth); + Upper = APInt::getSignedMaxValue(BitWidth) + 1; + return; + } + + if (R.Flavor == SelectPatternFlavor::SPF_NABS) { + // The result of -abs(X) is <= 0. + Lower = APInt::getSignedMinValue(BitWidth); + Upper = APInt(BitWidth, 1); + return; + } + + const APInt *C; + if (!match(LHS, m_APInt(C)) && !match(RHS, m_APInt(C))) + return; + + switch (R.Flavor) { + case SPF_UMIN: + Upper = *C + 1; + break; + case SPF_UMAX: + Lower = *C; + break; + case SPF_SMIN: + Lower = APInt::getSignedMinValue(BitWidth); + Upper = *C + 1; + break; + case SPF_SMAX: + Lower = *C; + Upper = APInt::getSignedMaxValue(BitWidth) + 1; + break; + default: + break; + } +} + ConstantRange llvm::computeConstantRange(const Value *V, bool UseInstrInfo) { assert(V->getType()->isIntOrIntVectorTy() && "Expected integer instruction"); @@ -5664,6 +5715,8 @@ setLimitsForBinOp(*BO, Lower, Upper, IIQ); else if (auto *II = dyn_cast(V)) setLimitsForIntrinsic(*II, Lower, Upper); + else if (auto *SI = dyn_cast(V)) + setLimitsForSelect(*SI, Lower, Upper); ConstantRange CR = Lower != Upper ? ConstantRange(Lower, Upper) : ConstantRange(BitWidth, true); Index: llvm/test/Transforms/InstCombine/minmax-fold.ll =================================================================== --- llvm/test/Transforms/InstCombine/minmax-fold.ll +++ llvm/test/Transforms/InstCombine/minmax-fold.ll @@ -533,6 +533,37 @@ ret i32 %res } +; Check that there is no infinite loop because of reverse cmp transformation: +; (icmp slt smax(PositiveA, B) 2) -> (icmp eq B 1) +define i32 @clamp_check_for_no_infinite_loop3(i32 %i) { +; CHECK-LABEL: @clamp_check_for_no_infinite_loop3( +; CHECK-NEXT: [[I2:%.*]] = icmp sgt i32 [[I:%.*]], 1 +; CHECK-NEXT: [[I3:%.*]] = select i1 [[I2]], i32 [[I]], i32 1 +; CHECK-NEXT: br i1 true, label [[TRUELABEL:%.*]], label [[FALSELABEL:%.*]] +; CHECK: truelabel: +; CHECK-NEXT: [[I5:%.*]] = icmp slt i32 [[I3]], 2 +; CHECK-NEXT: [[I6:%.*]] = select i1 [[I5]], i32 [[I3]], i32 2 +; CHECK-NEXT: [[I7:%.*]] = shl nuw nsw i32 [[I6]], 2 +; CHECK-NEXT: ret i32 [[I7]] +; CHECK: falselabel: +; CHECK-NEXT: ret i32 0 +; + + %i2 = icmp sgt i32 %i, 1 + %i3 = select i1 %i2, i32 %i, i32 1 + %i4 = icmp sgt i32 %i3, 0 + br i1 %i4, label %truelabel, label %falselabel + +truelabel: ; %i<=1, %i3>0 + %i5 = icmp slt i32 %i3, 2 + %i6 = select i1 %i5, i32 %i3, i32 2 + %i7 = shl nuw nsw i32 %i6, 2 + ret i32 %i7 + +falselabel: + ret i32 0 +} + ; The next 3 min tests should canonicalize to the same form...and not infinite loop. define double @PR31751_umin1(i32 %x) { Index: llvm/test/Transforms/InstSimplify/cmp_of_min_max.ll =================================================================== --- llvm/test/Transforms/InstSimplify/cmp_of_min_max.ll +++ llvm/test/Transforms/InstSimplify/cmp_of_min_max.ll @@ -3,10 +3,7 @@ define i1 @test_umin1(i32 %n) { ; CHECK-LABEL: @test_umin1( -; CHECK-NEXT: [[C1:%.*]] = icmp ugt i32 [[N:%.*]], 10 -; CHECK-NEXT: [[S:%.*]] = select i1 [[C1]], i32 [[N]], i32 10 -; CHECK-NEXT: [[C2:%.*]] = icmp ugt i32 [[S]], 9 -; CHECK-NEXT: ret i1 [[C2]] +; CHECK-NEXT: ret i1 true ; %c1 = icmp ugt i32 %n, 10 %s = select i1 %c1, i32 %n, i32 10 @@ -40,10 +37,7 @@ define i1 @test_umax1(i32 %n) { ; CHECK-LABEL: @test_umax1( -; CHECK-NEXT: [[C1:%.*]] = icmp ult i32 [[N:%.*]], 10 -; CHECK-NEXT: [[S:%.*]] = select i1 [[C1]], i32 [[N]], i32 10 -; CHECK-NEXT: [[C2:%.*]] = icmp ult i32 [[S]], 11 -; CHECK-NEXT: ret i1 [[C2]] +; CHECK-NEXT: ret i1 true ; %c1 = icmp ult i32 %n, 10 %s = select i1 %c1, i32 %n, i32 10 @@ -77,10 +71,7 @@ define i1 @test_smin1(i32 %n) { ; CHECK-LABEL: @test_smin1( -; CHECK-NEXT: [[C1:%.*]] = icmp sgt i32 [[N:%.*]], -10 -; CHECK-NEXT: [[S:%.*]] = select i1 [[C1]], i32 [[N]], i32 -10 -; CHECK-NEXT: [[C2:%.*]] = icmp sgt i32 [[S]], -11 -; CHECK-NEXT: ret i1 [[C2]] +; CHECK-NEXT: ret i1 true ; %c1 = icmp sgt i32 %n, -10 %s = select i1 %c1, i32 %n, i32 -10 @@ -114,10 +105,7 @@ define i1 @test_smax1(i32 %n) { ; CHECK-LABEL: @test_smax1( -; CHECK-NEXT: [[C1:%.*]] = icmp slt i32 [[N:%.*]], 10 -; CHECK-NEXT: [[S:%.*]] = select i1 [[C1]], i32 [[N]], i32 10 -; CHECK-NEXT: [[C2:%.*]] = icmp slt i32 [[S]], 11 -; CHECK-NEXT: ret i1 [[C2]] +; CHECK-NEXT: ret i1 true ; %c1 = icmp slt i32 %n, 10 %s = select i1 %c1, i32 %n, i32 10