[PATCH] D59563: [ValueTracking] Compute range for abs without nsw

Nikita Popov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Mar 19 14:19:56 PDT 2019


nikic created this revision.
nikic added reviewers: lebedev.ri, spatel.
Herald added subscribers: llvm-commits, jdoerfert, hiraditya.
Herald added a project: LLVM.

This is a small followup to D59511 <https://reviews.llvm.org/D59511>. The code that was moved into computeConstantRange() there is a bit overly conversative: If the abs is not nsw, it does not compute any range. However, abs without nsw still has a well-defined contiguous unsigned range from 0 to SIGNED_MIN (inclusive). This is a lot less useful than the usual 0 to SIGNED_MAX (inclusive) range, but if we're already here we might as well specify it...


Repository:
  rL LLVM

https://reviews.llvm.org/D59563

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
@@ -150,11 +150,7 @@
 ; Even if we don't have nsw, the range is still limited in the unsigned domain.
 define i1 @abs_positive_or_signed_min(i32 %x) {
 ; CHECK-LABEL: @abs_positive_or_signed_min(
-; CHECK-NEXT:    [[CMP:%.*]] = icmp slt i32 [[X:%.*]], 0
-; CHECK-NEXT:    [[NEGX:%.*]] = sub i32 0, [[X]]
-; CHECK-NEXT:    [[ABS:%.*]] = select i1 [[CMP]], i32 [[NEGX]], i32 [[X]]
-; CHECK-NEXT:    [[R:%.*]] = icmp ult i32 [[ABS]], -2147483647
-; CHECK-NEXT:    ret i1 [[R]]
+; CHECK-NEXT:    ret i1 true
 ;
   %cmp = icmp slt i32 %x, 0
   %negx = sub i32 0, %x
Index: llvm/lib/Analysis/ValueTracking.cpp
===================================================================
--- llvm/lib/Analysis/ValueTracking.cpp
+++ llvm/lib/Analysis/ValueTracking.cpp
@@ -5673,14 +5673,14 @@
 
   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<Instruction>(RHS)->hasNoSignedWrap()) {
-    // The result of abs(X) is >= 0 (with nsw).
+  if (R.Flavor == SelectPatternFlavor::SPF_ABS) {
+    // The result of abs(X) is >= 0, or SignedMin if the negation part of the
+    // abs (in RHS) has no NSW flag.
     Lower = APInt::getNullValue(BitWidth);
-    Upper = APInt::getSignedMaxValue(BitWidth) + 1;
+    if (cast<Instruction>(RHS)->hasNoSignedWrap())
+      Upper = APInt::getSignedMaxValue(BitWidth) + 1;
+    else
+      Upper = APInt::getSignedMinValue(BitWidth) + 1;
     return;
   }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D59563.191394.patch
Type: text/x-patch
Size: 1915 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190319/b3377e3e/attachment.bin>


More information about the llvm-commits mailing list