Skip to content

Commit bda8d1a

Browse files
committed
[PatternMatch] Do not match constant expression trunc
Similar to the change previously made for binops, make m_Trunc() only match instructions, not constant expressions. This is more likely to cause a crash than do something useful. Fixes crash reported at: #92885 (comment)
1 parent 4812e9a commit bda8d1a

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

llvm/include/llvm/IR/PatternMatch.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1948,8 +1948,8 @@ m_IntToPtr(const OpTy &Op) {
19481948

19491949
/// Matches Trunc.
19501950
template <typename OpTy>
1951-
inline CastOperator_match<OpTy, Instruction::Trunc> m_Trunc(const OpTy &Op) {
1952-
return CastOperator_match<OpTy, Instruction::Trunc>(Op);
1951+
inline CastInst_match<OpTy, TruncInst> m_Trunc(const OpTy &Op) {
1952+
return CastInst_match<OpTy, TruncInst>(Op);
19531953
}
19541954

19551955
/// Matches trunc nuw.
@@ -1967,7 +1967,7 @@ m_NSWTrunc(const OpTy &Op) {
19671967
}
19681968

19691969
template <typename OpTy>
1970-
inline match_combine_or<CastOperator_match<OpTy, Instruction::Trunc>, OpTy>
1970+
inline match_combine_or<CastInst_match<OpTy, TruncInst>, OpTy>
19711971
m_TruncOrSelf(const OpTy &Op) {
19721972
return m_CombineOr(m_Trunc(Op), Op);
19731973
}

llvm/test/Transforms/InstCombine/icmp-of-trunc-ext.ll

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -642,3 +642,17 @@ define i1 @test_slt_nuw(i32 %x, i16 %y) {
642642
%cond = icmp slt i8 %conv1, %conv2
643643
ret i1 %cond
644644
}
645+
646+
@foo = external global i8
647+
@bar = external global i8
648+
649+
define i1 @constexpr_trunc() {
650+
; CHECK-LABEL: @constexpr_trunc(
651+
; CHECK-NEXT: [[CMP5:%.*]] = icmp ne i16 trunc (i64 sub (i64 sub (i64 ptrtoint (ptr @bar to i64), i64 ptrtoint (ptr @foo to i64)), i64 8) to i16), trunc (i64 sub (i64 sub (i64 0, i64 ptrtoint (ptr @foo to i64)), i64 8) to i16)
652+
; CHECK-NEXT: ret i1 [[CMP5]]
653+
;
654+
%conv = zext i16 trunc (i64 sub (i64 sub nuw nsw (i64 ptrtoint (ptr @bar to i64), i64 ptrtoint (ptr @foo to i64)), i64 8) to i16) to i32
655+
%conv1 = zext i16 trunc (i64 sub (i64 sub nuw nsw (i64 0, i64 ptrtoint (ptr @foo to i64)), i64 8) to i16) to i32
656+
%cmp5 = icmp ne i32 %conv, %conv1
657+
ret i1 %cmp5
658+
}

0 commit comments

Comments
 (0)