Skip to content

Commit 9416b19

Browse files
authored
[InstCombine] Add missing constant check (#170068)
`cast<Constant>` is not guarded by a type check during canonicalization of predicates. This patch adds a type check in the outer if to avoid the crash. `dyn_cast` may introduce another nested if, so I just use `isa<Constant>` instead. Address the crash reported in #153053 (comment).
1 parent 036279a commit 9416b19

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1163,6 +1163,7 @@ static Value *canonicalizeSaturatedAddSigned(ICmpInst *Cmp, Value *TVal,
11631163
// (X >= Y) ? INT_MAX : (X + C) --> sadd.sat(X, C)
11641164
// where Y is INT_MAX - C or INT_MAX - C - 1, and C > 0
11651165
if ((Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SGE) &&
1166+
isa<Constant>(Cmp1) &&
11661167
match(FVal, m_Add(m_Specific(Cmp0), m_StrictlyPositive(C)))) {
11671168
APInt IntMax =
11681169
APInt::getSignedMaxValue(Cmp1->getType()->getScalarSizeInBits());

llvm/test/Transforms/InstCombine/saturating-add-sub.ll

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2671,3 +2671,19 @@ define i8 @neg_neg_constant(i8 %x, i8 %y) {
26712671
%s = select i1 %cmp, i8 127, i8 %d
26722672
ret i8 %s
26732673
}
2674+
2675+
; Make sure we don't crash in this case.
2676+
define i32 @pr153053_strict_pred_with_nonconstant_rhs(i32 %x, i32 %y) {
2677+
; CHECK-LABEL: @pr153053_strict_pred_with_nonconstant_rhs(
2678+
; CHECK-NEXT: entry:
2679+
; CHECK-NEXT: [[CMP:%.*]] = icmp slt i32 [[X:%.*]], [[Y:%.*]]
2680+
; CHECK-NEXT: [[ADD:%.*]] = add i32 [[X]], 1
2681+
; CHECK-NEXT: [[RES:%.*]] = select i1 [[CMP]], i32 [[ADD]], i32 2147483647
2682+
; CHECK-NEXT: ret i32 [[RES]]
2683+
;
2684+
entry:
2685+
%cmp = icmp slt i32 %x, %y
2686+
%add = add i32 %x, 1
2687+
%res = select i1 %cmp, i32 %add, i32 2147483647
2688+
ret i32 %res
2689+
}

0 commit comments

Comments
 (0)