Skip to content

Commit 932b5aa

Browse files
committed
Allow folding icmp eq (add X, C2), C when there is more than one-use when we can compute the range
If there are multiple uses of an add, we can fold a comparison anyway if we can compute a constant range for it, which should happen in cases such as saturated add.
1 parent 3382338 commit 932b5aa

File tree

2 files changed

+6
-3
lines changed

2 files changed

+6
-3
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3160,7 +3160,7 @@ Instruction *InstCombinerImpl::foldICmpAddConstant(ICmpInst &Cmp,
31603160
return replaceInstUsesWith(Cmp, Cond);
31613161
}
31623162
const APInt *C2;
3163-
if (Cmp.isEquality() || !match(Y, m_APInt(C2)))
3163+
if (!match(Y, m_APInt(C2)))
31643164
return nullptr;
31653165

31663166
// Fold icmp pred (add X, C2), C.
@@ -3184,7 +3184,7 @@ Instruction *InstCombinerImpl::foldICmpAddConstant(ICmpInst &Cmp,
31843184
return new ICmpInst(Pred, X, ConstantInt::get(Ty, NewC));
31853185
}
31863186

3187-
if (ICmpInst::isUnsigned(Pred) && Add->hasNoSignedWrap() &&
3187+
if (!Cmp.isEquality() && ICmpInst::isUnsigned(Pred) && Add->hasNoSignedWrap() &&
31883188
C.isNonNegative() && (C - *C2).isNonNegative() &&
31893189
computeConstantRange(X, /*ForSigned=*/true).add(*C2).isAllNonNegative())
31903190
return new ICmpInst(ICmpInst::getSignedPredicate(Pred), X,
@@ -3205,6 +3205,9 @@ Instruction *InstCombinerImpl::foldICmpAddConstant(ICmpInst &Cmp,
32053205
return new ICmpInst(ICmpInst::ICMP_UGE, X, ConstantInt::get(Ty, Lower));
32063206
}
32073207

3208+
if (Cmp.isEquality())
3209+
return nullptr;
3210+
32083211
// This set of folds is intentionally placed after folds that use no-wrapping
32093212
// flags because those folds are likely better for later analysis/codegen.
32103213
const APInt SMax = APInt::getSignedMaxValue(Ty->getScalarSizeInBits());

llvm/test/Transforms/InstCombine/uaddo.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ define i1 @uaddo_1(i8 %x, ptr %p) {
158158
; CHECK-LABEL: @uaddo_1(
159159
; CHECK-NEXT: [[A:%.*]] = add i8 [[X:%.*]], 1
160160
; CHECK-NEXT: store i8 [[A]], ptr [[P:%.*]], align 1
161-
; CHECK-NEXT: [[C:%.*]] = icmp eq i8 [[A]], 0
161+
; CHECK-NEXT: [[C:%.*]] = icmp eq i8 [[X]], -1
162162
; CHECK-NEXT: ret i1 [[C]]
163163
;
164164
%a = add i8 %x, 1

0 commit comments

Comments
 (0)