[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.
Craig Topper via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Fri Aug 2 23:36:34 PDT 2019
craig.topper created this revision.
craig.topper added reviewers: RKSimon, spatel.
Herald added a subscriber: hiraditya.
Herald added a project: LLVM.
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.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D65692
Files:
llvm/lib/Analysis/ValueTracking.cpp
llvm/test/Transforms/InstSimplify/icmp-abs-nabs.ll
Index: llvm/test/Transforms/InstSimplify/icmp-abs-nabs.ll
===================================================================
--- llvm/test/Transforms/InstSimplify/icmp-abs-nabs.ll
+++ llvm/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
Index: llvm/lib/Analysis/ValueTracking.cpp
===================================================================
--- llvm/lib/Analysis/ValueTracking.cpp
+++ llvm/lib/Analysis/ValueTracking.cpp
@@ -1095,7 +1095,7 @@
// 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_NSWSub(m_ZeroInt(), m_Specific(LHS))))
MaxHighZeros = 1;
}
@@ -5624,7 +5624,7 @@
// 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_NSWSub(m_ZeroInt(), m_Specific(LHS))))
Upper = APInt::getSignedMaxValue(BitWidth) + 1;
else
Upper = APInt::getSignedMinValue(BitWidth) + 1;
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D65692.213181.patch
Type: text/x-patch
Size: 1831 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190803/6b6a3787/attachment.bin>
More information about the llvm-commits
mailing list