llvm.org GIT mirror
[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).
 1094 1094 // RHS from matchSelectPattern returns the negation part of abs pattern. 1095 1095 // If the negate has an NSW flag we can assume the sign bit of the result 1096 1096 // 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))) 1098 1099 MaxHighZeros = 1; 1099 1100 } 1100 1101 5610 5611 } 5611 5612 5612 5613 static void setLimitsForSelectPattern(const SelectInst &SI, APInt &Lower, 5613 APInt &Upper) {⏎ 5614 APInt &Upper, const InstrInfoQuery &IIQ) {⏎ 5614 5615 const Value *LHS, *RHS; 5615 5616 SelectPatternResult R = matchSelectPattern(&SI, LHS, RHS); 5616 5617 if (R.Flavor == SPF_UNKNOWN) 5623 5624 // then the result of abs(X) is [0..SIGNED_MAX], 5624 5625 // otherwise it is [0..SIGNED_MIN], as -SIGNED_MIN == SIGNED_MIN. 5625 5626 Lower = APInt::getNullValue(BitWidth); 5626 if (cast(RHS)->hasNoSignedWrap())⏎ 5627 if (match(RHS, m_Neg(m_Specific(LHS))) &&⏎ 5628 IIQ.hasNoSignedWrap(cast(RHS))) 5627 5629 Upper = APInt::getSignedMaxValue(BitWidth) + 1; 5628 5630 else 5629 5631 Upper = APInt::getSignedMinValue(BitWidth) + 1; 5677 5679 else if (auto *II = dyn_cast(V)) 5678 5680 setLimitsForIntrinsic(*II, Lower, Upper); 5679 5681 else if (auto *SI = dyn_cast(V)) 5680 setLimitsForSelectPattern(*SI, Lower, Upper);⏎ 5682 setLimitsForSelectPattern(*SI, Lower, Upper, IIQ);⏎ 5681 5683 5682 5684 ConstantRange CR = ConstantRange::getNonEmpty(Lower, Upper); 5683 5685
 403 403 ; We can't fold this to false unless both subs have nsw. 404 404 define i1 @abs_sub_sub_missing_nsw(i32 %x, i32 %y) { 405 405 ; 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]] 407 412 ; 408 413 %a = sub i32 %x, %y 409 414 %b = sub nsw i32 %y, %x