Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 33 additions & 3 deletions llvm/lib/Analysis/ValueTracking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9576,15 +9576,45 @@ static void setLimitsForBinOp(const BinaryOperator &BO, APInt &Lower,
unsigned Width = Lower.getBitWidth();
const APInt *C;
switch (BO.getOpcode()) {
case Instruction::Add:
if (match(BO.getOperand(1), m_APInt(C)) && !C->isZero()) {
case Instruction::Sub:
if (match(BO.getOperand(0), m_APInt(C))) {
bool HasNSW = IIQ.hasNoSignedWrap(&BO);
bool HasNUW = IIQ.hasNoUnsignedWrap(&BO);

// If the caller expects a signed compare, then try to use a signed range.
// Otherwise if both no-wraps are set, use the unsigned range because it
// is never larger than the signed range. Example:
// "add nuw nsw i8 X, -2" is unsigned [254,255] vs. signed [-128, 125].
// "sub nuw nsw i8 -2, x" is unsigned [0, 254] vs. signed [-128, 126].
// "sub nuw nsw i8 2, x" is unsigned [0, 2] vs. signed [-125, 127].
if (PreferSignedRange && HasNSW && HasNUW)
HasNUW = false;

if (HasNUW) {
// 'sub nuw c, x' produces [0, C].
Upper = *C + 1;
} else if (HasNSW) {
if (C->isNegative()) {
// 'sub nsw -C, x' produces [SINT_MIN, -C - SINT_MIN].
Lower = APInt::getSignedMinValue(Width);
Upper = *C - APInt::getSignedMaxValue(Width);
} else {
// Note that sub 0, INT_MIN is not NSW. It techically is a signed wrap
// 'sub nsw C, x' produces [C - SINT_MAX, SINT_MAX].
Lower = *C - APInt::getSignedMaxValue(Width);
Upper = APInt::getSignedMinValue(Width);
}
}
}
break;
case Instruction::Add:
if (match(BO.getOperand(1), m_APInt(C)) && !C->isZero()) {
bool HasNSW = IIQ.hasNoSignedWrap(&BO);
bool HasNUW = IIQ.hasNoUnsignedWrap(&BO);

// If the caller expects a signed compare, then try to use a signed
// range. Otherwise if both no-wraps are set, use the unsigned range
// because it is never larger than the signed range. Example: "add nuw
// nsw i8 X, -2" is unsigned [254,255] vs. signed [-128, 125].
if (PreferSignedRange && HasNSW && HasNUW)
HasNUW = false;

Expand Down
8 changes: 2 additions & 6 deletions llvm/test/Transforms/InstCombine/div.ll
Original file line number Diff line number Diff line change
Expand Up @@ -494,9 +494,7 @@ define <2 x i8> @sdiv_exact_negated_dividend_constant_divisor_vec_splat(<2 x i8>

define i8 @sdiv_negated_dividend_constant_divisor_smin(i8 %x) {
; CHECK-LABEL: @sdiv_negated_dividend_constant_divisor_smin(
; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i8 [[X:%.*]], -128
; CHECK-NEXT: [[D:%.*]] = zext i1 [[TMP1]] to i8
; CHECK-NEXT: ret i8 [[D]]
; CHECK-NEXT: ret i8 0
;
%neg = sub nsw i8 0, %x
%d = sdiv i8 %neg, -128
Expand All @@ -505,9 +503,7 @@ define i8 @sdiv_negated_dividend_constant_divisor_smin(i8 %x) {

define <2 x i8> @sdiv_negated_dividend_constant_divisor_vec_splat_smin(<2 x i8> %x) {
; CHECK-LABEL: @sdiv_negated_dividend_constant_divisor_vec_splat_smin(
; CHECK-NEXT: [[TMP1:%.*]] = icmp eq <2 x i8> [[X:%.*]], splat (i8 -128)
; CHECK-NEXT: [[D:%.*]] = zext <2 x i1> [[TMP1]] to <2 x i8>
; CHECK-NEXT: ret <2 x i8> [[D]]
; CHECK-NEXT: ret <2 x i8> zeroinitializer
;
%neg = sub nsw <2 x i8> zeroinitializer, %x
%d = sdiv <2 x i8> %neg, <i8 -128, i8 -128>
Expand Down
3 changes: 1 addition & 2 deletions llvm/test/Transforms/InstCombine/icmp-sub.ll
Original file line number Diff line number Diff line change
Expand Up @@ -290,8 +290,7 @@ define i1 @subC_nsw_ne(i32 %x) {
; CHECK-LABEL: @subC_nsw_ne(
; CHECK-NEXT: [[SUBX:%.*]] = sub nsw i32 -2147483647, [[X:%.*]]
; CHECK-NEXT: call void @use(i32 [[SUBX]])
; CHECK-NEXT: [[R:%.*]] = icmp ne i32 [[X]], 2147483603
; CHECK-NEXT: ret i1 [[R]]
; CHECK-NEXT: ret i1 true
;
%subx = sub nsw i32 -2147483647, %x
call void @use(i32 %subx)
Expand Down