https://github.com/el-ev updated https://github.com/llvm/llvm-project/pull/196238
>From b773a978d023777233885eac210d34e051c72761 Mon Sep 17 00:00:00 2001 From: Iris Shi <[email protected]> Date: Fri, 1 Aug 2025 13:23:51 +0800 Subject: [PATCH 1/6] [InstSimplify] Canonicalize `X uge 1` to `X ne 0` and `X sle -1` to `X slt 0` in `simplifyICmpInst` --- llvm/lib/Analysis/InstructionSimplify.cpp | 14 +++++++++++--- llvm/test/Transforms/InstSimplify/select-icmp.ll | 6 +----- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp index af21e46563f94..82c8fcf5dde4a 100644 --- a/llvm/lib/Analysis/InstructionSimplify.cpp +++ b/llvm/lib/Analysis/InstructionSimplify.cpp @@ -3126,9 +3126,6 @@ static Value *simplifyICmpWithConstant(CmpPredicate Pred, Value *LHS, *MulC != 0 && C->srem(*MulC) != 0))) return ConstantInt::get(ITy, Pred == ICmpInst::ICMP_NE); - if (Pred == ICmpInst::ICMP_UGE && C->isOne() && isKnownNonZero(LHS, Q)) - return ConstantInt::getTrue(ITy); - return nullptr; } @@ -3875,6 +3872,17 @@ static Value *simplifyICmpInst(CmpPredicate Pred, Value *LHS, Value *RHS, if (Value *V = simplifyICmpOfBools(Pred, LHS, RHS, Q)) return V; + const APInt *C; + if (match(RHS, m_APIntAllowPoison(C))) { + if (Pred == ICmpInst::ICMP_UGE && C->isOne()) { + Pred = ICmpInst::ICMP_NE; + RHS = ConstantInt::get(RHS->getType(), 0); + } else if (Pred == ICmpInst::ICMP_SLE && C->isAllOnes()) { + Pred = ICmpInst::ICMP_SLT; + RHS = ConstantInt::get(RHS->getType(), 0); + } + } + // TODO: Sink/common this with other potentially expensive calls that use // ValueTracking? See comment below for isKnownNonEqual(). if (Value *V = simplifyICmpWithZero(Pred, LHS, RHS, Q)) diff --git a/llvm/test/Transforms/InstSimplify/select-icmp.ll b/llvm/test/Transforms/InstSimplify/select-icmp.ll index 5ee10bb954766..5bb6d8d1ea04a 100755 --- a/llvm/test/Transforms/InstSimplify/select-icmp.ll +++ b/llvm/test/Transforms/InstSimplify/select-icmp.ll @@ -1,7 +1,6 @@ ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py ; RUN: opt < %s -passes=instsimplify -S | FileCheck %s -; TODO: https://alive2.llvm.org/ce/z/3ybZRl define i32 @pr54735_slt(i32 %x, i32 %y) { ; CHECK-LABEL: @pr54735_slt( ; CHECK-NEXT: entry: @@ -9,11 +8,8 @@ define i32 @pr54735_slt(i32 %x, i32 %y) { ; CHECK-NEXT: br i1 [[CMP]], label [[COND_TRUE:%.*]], label [[COND_END:%.*]] ; CHECK: cond.true: ; CHECK-NEXT: [[SUB:%.*]] = sub nsw i32 [[X]], [[Y]] -; CHECK-NEXT: [[ADD:%.*]] = add nsw i32 [[SUB]], 1 ; CHECK-NEXT: [[NEG:%.*]] = xor i32 [[SUB]], -1 -; CHECK-NEXT: [[ABSCOND:%.*]] = icmp sle i32 [[SUB]], -1 -; CHECK-NEXT: [[ABS:%.*]] = select i1 [[ABSCOND]], i32 [[NEG]], i32 [[ADD]] -; CHECK-NEXT: ret i32 [[ABS]] +; CHECK-NEXT: ret i32 [[NEG]] ; CHECK: cond.end: ; CHECK-NEXT: ret i32 0 ; >From 3fb1fe73de0812a60b9426f9fd5d93d717da8ad0 Mon Sep 17 00:00:00 2001 From: Iris Shi <[email protected]> Date: Thu, 7 Aug 2025 15:17:05 +0800 Subject: [PATCH 2/6] invert non-strict predicates --- llvm/lib/Analysis/InstructionSimplify.cpp | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp index 82c8fcf5dde4a..34711c0b38c99 100644 --- a/llvm/lib/Analysis/InstructionSimplify.cpp +++ b/llvm/lib/Analysis/InstructionSimplify.cpp @@ -3873,13 +3873,12 @@ static Value *simplifyICmpInst(CmpPredicate Pred, Value *LHS, Value *RHS, return V; const APInt *C; - if (match(RHS, m_APIntAllowPoison(C))) { - if (Pred == ICmpInst::ICMP_UGE && C->isOne()) { - Pred = ICmpInst::ICMP_NE; - RHS = ConstantInt::get(RHS->getType(), 0); - } else if (Pred == ICmpInst::ICMP_SLE && C->isAllOnes()) { - Pred = ICmpInst::ICMP_SLT; - RHS = ConstantInt::get(RHS->getType(), 0); + if (match(RHS, m_APIntAllowPoison(C)) && + ICmpInst::isNonStrictPredicate(Pred) && !C->isZero()) { + if (auto Flipped = getFlippedStrictnessPredicateAndConstant( + Pred, ConstantInt::get(LHS->getType(), *C))) { + Pred = Flipped->first; + RHS = Flipped->second; } } >From 6f18f55432ebe32a6e12e99381471657460204e5 Mon Sep 17 00:00:00 2001 From: Iris Shi <[email protected]> Date: Thu, 7 May 2026 12:25:16 +0800 Subject: [PATCH 3/6] update test --- llvm/test/Transforms/InstSimplify/compare.ll | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/llvm/test/Transforms/InstSimplify/compare.ll b/llvm/test/Transforms/InstSimplify/compare.ll index ba10ea532a34a..db09b9223cf6e 100644 --- a/llvm/test/Transforms/InstSimplify/compare.ll +++ b/llvm/test/Transforms/InstSimplify/compare.ll @@ -1895,7 +1895,9 @@ define <2 x i1> @icmp_shl_1_ule_signmask_poison(<2 x i8> %V) { define <2 x i1> @icmp_shl_1_ule_signmask_poison2(<2 x i8> %V) { ; CHECK-LABEL: @icmp_shl_1_ule_signmask_poison2( -; CHECK-NEXT: ret <2 x i1> splat (i1 true) +; CHECK-NEXT: [[SHL:%.*]] = shl <2 x i8> <i8 1, i8 poison>, [[V:%.*]] +; CHECK-NEXT: [[CMP:%.*]] = icmp ule <2 x i8> [[SHL]], <i8 poison, i8 -128> +; CHECK-NEXT: ret <2 x i1> [[CMP]] ; %shl = shl <2 x i8> <i8 1, i8 poison>, %V %cmp = icmp ule <2 x i8> %shl, <i8 poison, i8 128> >From 5274cc8b20dd1bcbc60a42e293b3687982cc62a2 Mon Sep 17 00:00:00 2001 From: Iris Shi <[email protected]> Date: Thu, 7 May 2026 12:40:13 +0800 Subject: [PATCH 4/6] drop zero check --- llvm/lib/Analysis/InstructionSimplify.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp index 34711c0b38c99..51dbf07b1fe17 100644 --- a/llvm/lib/Analysis/InstructionSimplify.cpp +++ b/llvm/lib/Analysis/InstructionSimplify.cpp @@ -3874,7 +3874,7 @@ static Value *simplifyICmpInst(CmpPredicate Pred, Value *LHS, Value *RHS, const APInt *C; if (match(RHS, m_APIntAllowPoison(C)) && - ICmpInst::isNonStrictPredicate(Pred) && !C->isZero()) { + ICmpInst::isNonStrictPredicate(Pred)) { if (auto Flipped = getFlippedStrictnessPredicateAndConstant( Pred, ConstantInt::get(LHS->getType(), *C))) { Pred = Flipped->first; >From 9e9e4e176f2fab91613a12c631a44add2f774079 Mon Sep 17 00:00:00 2001 From: Iris Shi <[email protected]> Date: Thu, 7 May 2026 13:54:13 +0800 Subject: [PATCH 5/6] fix crash --- llvm/lib/Analysis/ValueTracking.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp index 8f10bbae3d462..7cd183a8dcfe2 100644 --- a/llvm/lib/Analysis/ValueTracking.cpp +++ b/llvm/lib/Analysis/ValueTracking.cpp @@ -8802,6 +8802,9 @@ llvm::getFlippedStrictnessPredicateAndConstant(CmpPredicate Pred, Constant *C) { return std::nullopt; Type *Type = C->getType(); + + if (Type->getScalarSizeInBits() == 1) + return std::nullopt; bool IsSigned = ICmpInst::isSigned(Pred); CmpInst::Predicate UnsignedPred = ICmpInst::getUnsignedPredicate(Pred); >From 23590a84c534ed85bd1edbc8a2aba0fb80e5d2f8 Mon Sep 17 00:00:00 2001 From: Iris Shi <[email protected]> Date: Fri, 8 May 2026 18:03:02 +0800 Subject: [PATCH 6/6] update test --- llvm/test/Transforms/InstSimplify/compare.ll | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/llvm/test/Transforms/InstSimplify/compare.ll b/llvm/test/Transforms/InstSimplify/compare.ll index 9542d933f34fc..3895dace4209a 100644 --- a/llvm/test/Transforms/InstSimplify/compare.ll +++ b/llvm/test/Transforms/InstSimplify/compare.ll @@ -1895,9 +1895,7 @@ define <2 x i1> @icmp_shl_1_ule_signmask_poison(<2 x i8> %V) { define <2 x i1> @icmp_shl_1_ule_signmask_poison2(<2 x i8> %V) { ; CHECK-LABEL: @icmp_shl_1_ule_signmask_poison2( -; CHECK-NEXT: [[SHL:%.*]] = shl <2 x i8> <i8 1, i8 poison>, [[V:%.*]] -; CHECK-NEXT: [[CMP:%.*]] = icmp ule <2 x i8> [[SHL]], <i8 poison, i8 -128> -; CHECK-NEXT: ret <2 x i1> [[CMP]] +; CHECK-NEXT: ret <2 x i1> splat (i1 true) ; %shl = shl <2 x i8> <i8 1, i8 poison>, %V %cmp = icmp ule <2 x i8> %shl, <i8 poison, i8 128> _______________________________________________ llvm-branch-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
