Skip to content

Commit dd19f34

Browse files
committed
[AggressiveInstCombine] guard against applying instruction flags with constant folding
This is a minimized version of a crash reported in: D108201
1 parent 88962ce commit dd19f34

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

llvm/lib/Transforms/AggressiveInstCombine/TruncInstCombine.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,8 @@ void TruncInstCombine::ReduceExpressionDag(Type *SclTy) {
396396
Res = Builder.CreateBinOp((Instruction::BinaryOps)Opc, LHS, RHS);
397397
// Preserve `exact` flag since truncation doesn't change exactness
398398
if (Opc == Instruction::LShr)
399-
cast<Instruction>(Res)->setIsExact(I->isExact());
399+
if (auto *ResI = dyn_cast<Instruction>(Res))
400+
ResI->setIsExact(I->isExact());
400401
break;
401402
}
402403
case Instruction::Select: {

llvm/test/Transforms/AggressiveInstCombine/trunc_lshr.ll

+14
Original file line numberDiff line numberDiff line change
@@ -234,3 +234,17 @@ define i16 @lshr_negative_operand_but_short(i16 %x) {
234234
%trunc = trunc i32 %lshr2 to i16
235235
ret i16 %trunc
236236
}
237+
238+
; We may encounter unoptimized IR as below,
239+
; so don't crash by assuming that we can
240+
; apply instruction flags (exact) if there
241+
; is no instruction.
242+
243+
define i8 @non_canonical_crash() {
244+
; CHECK-LABEL: @non_canonical_crash(
245+
; CHECK-NEXT: ret i8 8
246+
;
247+
%sh = lshr i32 33, 2
248+
%tr = trunc i32 %sh to i8
249+
ret i8 %tr
250+
}

0 commit comments

Comments
 (0)