diff --git a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp index e7dc366b13798..c9f51e4b294b1 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp @@ -1163,6 +1163,7 @@ static Value *canonicalizeSaturatedAddSigned(ICmpInst *Cmp, Value *TVal, // (X >= Y) ? INT_MAX : (X + C) --> sadd.sat(X, C) // where Y is INT_MAX - C or INT_MAX - C - 1, and C > 0 if ((Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SGE) && + isa(Cmp1) && match(FVal, m_Add(m_Specific(Cmp0), m_StrictlyPositive(C)))) { APInt IntMax = APInt::getSignedMaxValue(Cmp1->getType()->getScalarSizeInBits()); diff --git a/llvm/test/Transforms/InstCombine/saturating-add-sub.ll b/llvm/test/Transforms/InstCombine/saturating-add-sub.ll index c0ad5818e448a..1294f867f07c0 100644 --- a/llvm/test/Transforms/InstCombine/saturating-add-sub.ll +++ b/llvm/test/Transforms/InstCombine/saturating-add-sub.ll @@ -2671,3 +2671,19 @@ define i8 @neg_neg_constant(i8 %x, i8 %y) { %s = select i1 %cmp, i8 127, i8 %d ret i8 %s } + +; Make sure we don't crash in this case. +define i32 @pr153053_strict_pred_with_nonconstant_rhs(i32 %x, i32 %y) { +; CHECK-LABEL: @pr153053_strict_pred_with_nonconstant_rhs( +; CHECK-NEXT: entry: +; CHECK-NEXT: [[CMP:%.*]] = icmp slt i32 [[X:%.*]], [[Y:%.*]] +; CHECK-NEXT: [[ADD:%.*]] = add i32 [[X]], 1 +; CHECK-NEXT: [[RES:%.*]] = select i1 [[CMP]], i32 [[ADD]], i32 2147483647 +; CHECK-NEXT: ret i32 [[RES]] +; +entry: + %cmp = icmp slt i32 %x, %y + %add = add i32 %x, 1 + %res = select i1 %cmp, i32 %add, i32 2147483647 + ret i32 %res +}