Skip to content

Commit 4cdcf3b

Browse files
[InstCombine] Fold (trunc nuw A to i1) == (trunc nuw B to i1) to A == B (#133368)
Fixes #133344 Proof: https://alive2.llvm.org/ce/z/X3Uh23 InstCombine couldn't optimize `i1` because `canonicalizeICmpBool()` was transforming the comparison into bitwise operations before `foldICmpTruncWithTruncOrExt()` was called. This PR solves the ordering issue by placing `foldICmpTruncWithTruncOrExt()` before `canonicalizeICmpBool()`. I believe this will not cause any regressions since all tests are passing.
1 parent f76254d commit 4cdcf3b

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -7451,6 +7451,9 @@ Instruction *InstCombinerImpl::visitICmpInst(ICmpInst &I) {
74517451
}
74527452
}
74537453

7454+
if (Instruction *Res = foldICmpTruncWithTruncOrExt(I, Q))
7455+
return Res;
7456+
74547457
if (Op0->getType()->isIntOrIntVectorTy(1))
74557458
if (Instruction *Res = canonicalizeICmpBool(I, Builder))
74567459
return Res;
@@ -7473,9 +7476,6 @@ Instruction *InstCombinerImpl::visitICmpInst(ICmpInst &I) {
74737476
if (Instruction *Res = foldICmpUsingKnownBits(I))
74747477
return Res;
74757478

7476-
if (Instruction *Res = foldICmpTruncWithTruncOrExt(I, Q))
7477-
return Res;
7478-
74797479
// Test if the ICmpInst instruction is used exclusively by a select as
74807480
// part of a minimum or maximum operation. If so, refrain from doing
74817481
// any other folding. This helps out other analyses which understand

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

+11
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,17 @@ define i1 @trunc_equality_either(i16 %x, i16 %y) {
409409
ret i1 %c
410410
}
411411

412+
define i1 @trunc_equality_bool(i8 %a, i8 %b) {
413+
; CHECK-LABEL: @trunc_equality_bool(
414+
; CHECK-NEXT: [[EQ:%.*]] = icmp eq i8 [[A:%.*]], [[B:%.*]]
415+
; CHECK-NEXT: ret i1 [[EQ]]
416+
;
417+
%at = trunc nuw i8 %a to i1
418+
%bt = trunc nuw i8 %b to i1
419+
%eq = icmp eq i1 %at, %bt
420+
ret i1 %eq
421+
}
422+
412423
define i1 @trunc_unsigned_nuw_zext(i32 %x, i8 %y) {
413424
; CHECK-LABEL: @trunc_unsigned_nuw_zext(
414425
; CHECK-NEXT: [[TMP1:%.*]] = zext i8 [[Y:%.*]] to i32

0 commit comments

Comments
 (0)