[PATCH] D65692: [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.
Phabricator via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Wed Aug 7 11:28:42 PDT 2019
This revision was automatically updated to reflect the committed changes.
Closed by commit rL368195: [ValueTracking] When calculating known bits for integer abs, make sure we're… (authored by ctopper, committed by ).
Changed prior to commit:
https://reviews.llvm.org/D65692?vs=213403&id=213957#toc
Repository:
rL LLVM
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D65692/new/
https://reviews.llvm.org/D65692
Files:
llvm/trunk/lib/Analysis/ValueTracking.cpp
llvm/trunk/test/Transforms/InstSimplify/icmp-abs-nabs.ll
Index: llvm/trunk/lib/Analysis/ValueTracking.cpp
===================================================================
--- llvm/trunk/lib/Analysis/ValueTracking.cpp
+++ llvm/trunk/lib/Analysis/ValueTracking.cpp
@@ -1095,7 +1095,8 @@
// RHS from matchSelectPattern returns the negation part of abs pattern.
// If the negate has an NSW flag we can assume the sign bit of the result
// will be 0 because that makes abs(INT_MIN) undefined.
- if (Q.IIQ.hasNoSignedWrap(cast<Instruction>(RHS)))
+ if (match(RHS, m_Neg(m_Specific(LHS))) &&
+ Q.IIQ.hasNoSignedWrap(cast<Instruction>(RHS)))
MaxHighZeros = 1;
}
@@ -5611,7 +5612,7 @@
}
static void setLimitsForSelectPattern(const SelectInst &SI, APInt &Lower,
- APInt &Upper) {
+ APInt &Upper, const InstrInfoQuery &IIQ) {
const Value *LHS, *RHS;
SelectPatternResult R = matchSelectPattern(&SI, LHS, RHS);
if (R.Flavor == SPF_UNKNOWN)
@@ -5624,7 +5625,8 @@
// then the result of abs(X) is [0..SIGNED_MAX],
// otherwise it is [0..SIGNED_MIN], as -SIGNED_MIN == SIGNED_MIN.
Lower = APInt::getNullValue(BitWidth);
- if (cast<Instruction>(RHS)->hasNoSignedWrap())
+ if (match(RHS, m_Neg(m_Specific(LHS))) &&
+ IIQ.hasNoSignedWrap(cast<Instruction>(RHS)))
Upper = APInt::getSignedMaxValue(BitWidth) + 1;
else
Upper = APInt::getSignedMinValue(BitWidth) + 1;
@@ -5678,7 +5680,7 @@
else if (auto *II = dyn_cast<IntrinsicInst>(V))
setLimitsForIntrinsic(*II, Lower, Upper);
else if (auto *SI = dyn_cast<SelectInst>(V))
- setLimitsForSelectPattern(*SI, Lower, Upper);
+ setLimitsForSelectPattern(*SI, Lower, Upper, IIQ);
ConstantRange CR = ConstantRange::getNonEmpty(Lower, Upper);
Index: llvm/trunk/test/Transforms/InstSimplify/icmp-abs-nabs.ll
===================================================================
--- llvm/trunk/test/Transforms/InstSimplify/icmp-abs-nabs.ll
+++ llvm/trunk/test/Transforms/InstSimplify/icmp-abs-nabs.ll
@@ -404,7 +404,12 @@
; We can't fold this to false unless both subs have nsw.
define i1 @abs_sub_sub_missing_nsw(i32 %x, i32 %y) {
; CHECK-LABEL: @abs_sub_sub_missing_nsw(
-; CHECK-NEXT: ret i1 false
+; CHECK-NEXT: [[A:%.*]] = sub i32 [[X:%.*]], [[Y:%.*]]
+; CHECK-NEXT: [[B:%.*]] = sub nsw i32 [[Y]], [[X]]
+; CHECK-NEXT: [[C:%.*]] = icmp sgt i32 [[A]], -1
+; CHECK-NEXT: [[D:%.*]] = select i1 [[C]], i32 [[A]], i32 [[B]]
+; CHECK-NEXT: [[E:%.*]] = icmp slt i32 [[D]], 0
+; CHECK-NEXT: ret i1 [[E]]
;
%a = sub i32 %x, %y
%b = sub nsw i32 %y, %x
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D65692.213957.patch
Type: text/x-patch
Size: 2654 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190807/058a15de/attachment.bin>
More information about the llvm-commits
mailing list