llvm.org GIT mirror llvm / 4c45b6f
[ValueTracking] When calculating known bits for integer abs, make sure we're looking at a negate and not just any instruction with the nsw flag set. The matchSelectPattern code can match patterns like (x >= 0) ? x : -x for absolute value. But it can also match ((x-y) >= 0) ? (x-y) : (y-x). If the latter form was matched we can only use the nsw flag if its set on both subtracts. This match makes sure we're looking at the former case only. Differential Revision: https://reviews.llvm.org/D65692 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@368195 91177308-0d34-0410-b5e6-96231b3b80d8 Craig Topper a month ago
2 changed file(s) with 12 addition(s) and 5 deletion(s). Raw diff Collapse all Expand all
10941094 // RHS from matchSelectPattern returns the negation part of abs pattern.
10951095 // If the negate has an NSW flag we can assume the sign bit of the result
10961096 // will be 0 because that makes abs(INT_MIN) undefined.
1097 if (Q.IIQ.hasNoSignedWrap(cast(RHS)))
1097 if (match(RHS, m_Neg(m_Specific(LHS))) &&
1098 Q.IIQ.hasNoSignedWrap(cast(RHS)))
10981099 MaxHighZeros = 1;
10991100 }
11001101
56105611 }
56115612
56125613 static void setLimitsForSelectPattern(const SelectInst &SI, APInt &Lower,
5613 APInt &Upper) {
5614 APInt &Upper, const InstrInfoQuery &IIQ) {
56145615 const Value *LHS, *RHS;
56155616 SelectPatternResult R = matchSelectPattern(&SI, LHS, RHS);
56165617 if (R.Flavor == SPF_UNKNOWN)
56235624 // then the result of abs(X) is [0..SIGNED_MAX],
56245625 // otherwise it is [0..SIGNED_MIN], as -SIGNED_MIN == SIGNED_MIN.
56255626 Lower = APInt::getNullValue(BitWidth);
5626 if (cast(RHS)->hasNoSignedWrap())
5627 if (match(RHS, m_Neg(m_Specific(LHS))) &&
5628 IIQ.hasNoSignedWrap(cast(RHS)))
56275629 Upper = APInt::getSignedMaxValue(BitWidth) + 1;
56285630 else
56295631 Upper = APInt::getSignedMinValue(BitWidth) + 1;
56775679 else if (auto *II = dyn_cast(V))
56785680 setLimitsForIntrinsic(*II, Lower, Upper);
56795681 else if (auto *SI = dyn_cast(V))
5680 setLimitsForSelectPattern(*SI, Lower, Upper);
5682 setLimitsForSelectPattern(*SI, Lower, Upper, IIQ);
56815683
56825684 ConstantRange CR = ConstantRange::getNonEmpty(Lower, Upper);
56835685
403403 ; We can't fold this to false unless both subs have nsw.
404404 define i1 @abs_sub_sub_missing_nsw(i32 %x, i32 %y) {
405405 ; CHECK-LABEL: @abs_sub_sub_missing_nsw(
406 ; CHECK-NEXT: ret i1 false
406 ; CHECK-NEXT: [[A:%.*]] = sub i32 [[X:%.*]], [[Y:%.*]]
407 ; CHECK-NEXT: [[B:%.*]] = sub nsw i32 [[Y]], [[X]]
408 ; CHECK-NEXT: [[C:%.*]] = icmp sgt i32 [[A]], -1
409 ; CHECK-NEXT: [[D:%.*]] = select i1 [[C]], i32 [[A]], i32 [[B]]
410 ; CHECK-NEXT: [[E:%.*]] = icmp slt i32 [[D]], 0
411 ; CHECK-NEXT: ret i1 [[E]]
407412 ;
408413 %a = sub i32 %x, %y
409414 %b = sub nsw i32 %y, %x