From 8f1e59217bb7096396541485334cfa40f41f80ae Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Wed, 20 Mar 2019 18:16:02 +0000 Subject: [PATCH] [ValueTracking] Compute range for abs without nsw This is a small followup to 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. This is a lot less useful than the usual 0 to SIGNED_MAX range, but if we're already here we might as well specify it... Differential Revision: https://reviews.llvm.org/D59563 llvm-svn: 356586 --- lib/Analysis/ValueTracking.cpp | 15 ++++++++------- test/Transforms/InstSimplify/icmp-abs-nabs.ll | 6 +----- 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/lib/Analysis/ValueTracking.cpp b/lib/Analysis/ValueTracking.cpp index 99d6010acee..52328b174b4 100644 --- a/lib/Analysis/ValueTracking.cpp +++ b/lib/Analysis/ValueTracking.cpp @@ -5673,14 +5673,15 @@ static void setLimitsForSelectPattern(const SelectInst &SI, APInt &Lower, 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(RHS)->hasNoSignedWrap()) { - // The result of abs(X) is >= 0 (with nsw). + if (R.Flavor == SelectPatternFlavor::SPF_ABS) { + // If the negation part of the abs (in RHS) has the NSW flag, + // 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); - Upper = APInt::getSignedMaxValue(BitWidth) + 1; + if (cast(RHS)->hasNoSignedWrap()) + Upper = APInt::getSignedMaxValue(BitWidth) + 1; + else + Upper = APInt::getSignedMinValue(BitWidth) + 1; return; } diff --git a/test/Transforms/InstSimplify/icmp-abs-nabs.ll b/test/Transforms/InstSimplify/icmp-abs-nabs.ll index 52545e7de0b..be2e7b4d4a6 100644 --- a/test/Transforms/InstSimplify/icmp-abs-nabs.ll +++ b/test/Transforms/InstSimplify/icmp-abs-nabs.ll @@ -150,11 +150,7 @@ define i1 @abs_nsw_is_not_negative_wrong_range(i32 %x) { ; 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