[llvm] dacb629 - [InstSimplify] Simplify and/or of trunc nuw to i1 with op replacement
Andreas Jonson via llvm-commits
llvm-commits at lists.llvm.org
Mon Mar 9 13:46:39 PDT 2026
Author: Andreas Jonson
Date: 2026-03-09T21:46:24+01:00
New Revision: dacb62989db8084cc2865d6b9ef85bbdf34e112d
URL: https://github.com/llvm/llvm-project/commit/dacb62989db8084cc2865d6b9ef85bbdf34e112d
DIFF: https://github.com/llvm/llvm-project/commit/dacb62989db8084cc2865d6b9ef85bbdf34e112d.diff
LOG: [InstSimplify] Simplify and/or of trunc nuw to i1 with op replacement
Added:
Modified:
llvm/lib/Analysis/InstructionSimplify.cpp
llvm/test/Transforms/InstSimplify/and-or-implied-cond.ll
Removed:
################################################################################
diff --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp
index 4da15280029d9..536c25cee736e 100644
--- a/llvm/lib/Analysis/InstructionSimplify.cpp
+++ b/llvm/lib/Analysis/InstructionSimplify.cpp
@@ -1950,8 +1950,12 @@ static Value *simplifyAndOrWithICmpEq(unsigned Opcode, Value *Op0, Value *Op1,
"Must be and/or");
CmpPredicate Pred;
Value *A, *B;
- if (!match(Op0, m_ICmp(Pred, m_Value(A), m_Value(B))) ||
- !ICmpInst::isEquality(Pred))
+ if (Op0->getType()->isIntOrIntVectorTy(1) &&
+ match(Op0, m_NUWTrunc(m_Value(A)))) {
+ B = ConstantInt::getNullValue(A->getType());
+ Pred = ICmpInst::ICMP_NE;
+ } else if (!match(Op0, m_ICmp(Pred, m_Value(A), m_Value(B))) ||
+ !ICmpInst::isEquality(Pred))
return nullptr;
auto Simplify = [&](Value *Res) -> Value * {
diff --git a/llvm/test/Transforms/InstSimplify/and-or-implied-cond.ll b/llvm/test/Transforms/InstSimplify/and-or-implied-cond.ll
index 253edc4b8156c..440499d1bfd30 100644
--- a/llvm/test/Transforms/InstSimplify/and-or-implied-cond.ll
+++ b/llvm/test/Transforms/InstSimplify/and-or-implied-cond.ll
@@ -351,8 +351,7 @@ define i1 @and_trunc_nuw_implied(i8 %x, i8 %y) {
; CHECK-NEXT: [[TRUNC:%.*]] = trunc nuw i8 [[X:%.*]] to i1
; CHECK-NEXT: [[SELECT:%.*]] = select i1 [[TRUNC]], i8 [[Y:%.*]], i8 0
; CHECK-NEXT: [[ICMP:%.*]] = icmp ne i8 [[SELECT]], 0
-; CHECK-NEXT: [[AND:%.*]] = and i1 [[TRUNC]], [[ICMP]]
-; CHECK-NEXT: ret i1 [[AND]]
+; CHECK-NEXT: ret i1 [[ICMP]]
;
%trunc = trunc nuw i8 %x to i1
%select = select i1 %trunc, i8 %y, i8 0
@@ -364,10 +363,7 @@ define i1 @and_trunc_nuw_implied(i8 %x, i8 %y) {
define i1 @or_trunc_nuw_implied(i8 %x, i8 %y) {
; CHECK-LABEL: @or_trunc_nuw_implied(
; CHECK-NEXT: [[TRUNC:%.*]] = trunc nuw i8 [[X:%.*]] to i1
-; CHECK-NEXT: [[SELECT:%.*]] = select i1 [[TRUNC]], i8 [[Y:%.*]], i8 0
-; CHECK-NEXT: [[ICMP:%.*]] = icmp ne i8 [[SELECT]], 0
-; CHECK-NEXT: [[AND:%.*]] = or i1 [[TRUNC]], [[ICMP]]
-; CHECK-NEXT: ret i1 [[AND]]
+; CHECK-NEXT: ret i1 [[TRUNC]]
;
%trunc = trunc nuw i8 %x to i1
%select = select i1 %trunc, i8 %y, i8 0
@@ -378,11 +374,7 @@ define i1 @or_trunc_nuw_implied(i8 %x, i8 %y) {
define i1 @or_trunc_nuw_implied_to_true(i8 %x, i8 %y) {
; CHECK-LABEL: @or_trunc_nuw_implied_to_true(
-; CHECK-NEXT: [[TRUNC:%.*]] = trunc nuw i8 [[X:%.*]] to i1
-; CHECK-NEXT: [[SELECT:%.*]] = select i1 [[TRUNC]], i8 [[Y:%.*]], i8 0
-; CHECK-NEXT: [[ICMP:%.*]] = icmp eq i8 [[SELECT]], 0
-; CHECK-NEXT: [[AND:%.*]] = or i1 [[TRUNC]], [[ICMP]]
-; CHECK-NEXT: ret i1 [[AND]]
+; CHECK-NEXT: ret i1 true
;
%trunc = trunc nuw i8 %x to i1
%select = select i1 %trunc, i8 %y, i8 0
More information about the llvm-commits
mailing list