From 87fb724da0e6a24713687abf9e87c512a10d3b14 Mon Sep 17 00:00:00 2001 From: Veera Date: Fri, 28 Mar 2025 04:28:24 +0000 Subject: [PATCH 1/2] Add Test --- .../Transforms/InstCombine/icmp-of-trunc-ext.ll | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/llvm/test/Transforms/InstCombine/icmp-of-trunc-ext.ll b/llvm/test/Transforms/InstCombine/icmp-of-trunc-ext.ll index 2c2de5dbf09f6..4ff9f8339be1a 100644 --- a/llvm/test/Transforms/InstCombine/icmp-of-trunc-ext.ll +++ b/llvm/test/Transforms/InstCombine/icmp-of-trunc-ext.ll @@ -409,6 +409,19 @@ define i1 @trunc_equality_either(i16 %x, i16 %y) { ret i1 %c } +define i1 @trunc_equality_bool(i8 %a, i8 %b) { +; CHECK-LABEL: @trunc_equality_bool( +; CHECK-NEXT: [[TMP1:%.*]] = xor i8 [[A:%.*]], [[B:%.*]] +; CHECK-NEXT: [[TMP2:%.*]] = trunc i8 [[TMP1]] to i1 +; CHECK-NEXT: [[EQ:%.*]] = xor i1 [[TMP2]], true +; CHECK-NEXT: ret i1 [[EQ]] +; + %at = trunc nuw i8 %a to i1 + %bt = trunc nuw i8 %b to i1 + %eq = icmp eq i1 %at, %bt + ret i1 %eq +} + define i1 @trunc_unsigned_nuw_zext(i32 %x, i8 %y) { ; CHECK-LABEL: @trunc_unsigned_nuw_zext( ; CHECK-NEXT: [[TMP1:%.*]] = zext i8 [[Y:%.*]] to i32 From ed615d224cc4726d1f685c63bf728ca13262f03f Mon Sep 17 00:00:00 2001 From: Veera Date: Fri, 28 Mar 2025 04:42:27 +0000 Subject: [PATCH 2/2] [InstCombine] Fold `(trunc nuw A to i1) == (trunc nuw B to i1)` to `A == B` --- llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp | 6 +++--- llvm/test/Transforms/InstCombine/icmp-of-trunc-ext.ll | 4 +--- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp index 76020d2b1dbf4..e75b4026d5424 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp @@ -7451,6 +7451,9 @@ Instruction *InstCombinerImpl::visitICmpInst(ICmpInst &I) { } } + if (Instruction *Res = foldICmpTruncWithTruncOrExt(I, Q)) + return Res; + if (Op0->getType()->isIntOrIntVectorTy(1)) if (Instruction *Res = canonicalizeICmpBool(I, Builder)) return Res; @@ -7473,9 +7476,6 @@ Instruction *InstCombinerImpl::visitICmpInst(ICmpInst &I) { if (Instruction *Res = foldICmpUsingKnownBits(I)) return Res; - if (Instruction *Res = foldICmpTruncWithTruncOrExt(I, Q)) - return Res; - // Test if the ICmpInst instruction is used exclusively by a select as // part of a minimum or maximum operation. If so, refrain from doing // any other folding. This helps out other analyses which understand diff --git a/llvm/test/Transforms/InstCombine/icmp-of-trunc-ext.ll b/llvm/test/Transforms/InstCombine/icmp-of-trunc-ext.ll index 4ff9f8339be1a..aeb70a636682f 100644 --- a/llvm/test/Transforms/InstCombine/icmp-of-trunc-ext.ll +++ b/llvm/test/Transforms/InstCombine/icmp-of-trunc-ext.ll @@ -411,9 +411,7 @@ define i1 @trunc_equality_either(i16 %x, i16 %y) { define i1 @trunc_equality_bool(i8 %a, i8 %b) { ; CHECK-LABEL: @trunc_equality_bool( -; CHECK-NEXT: [[TMP1:%.*]] = xor i8 [[A:%.*]], [[B:%.*]] -; CHECK-NEXT: [[TMP2:%.*]] = trunc i8 [[TMP1]] to i1 -; CHECK-NEXT: [[EQ:%.*]] = xor i1 [[TMP2]], true +; CHECK-NEXT: [[EQ:%.*]] = icmp eq i8 [[A:%.*]], [[B:%.*]] ; CHECK-NEXT: ret i1 [[EQ]] ; %at = trunc nuw i8 %a to i1