From ace8b1223e478a5107875fdbc36f0c9c7259b927 Mon Sep 17 00:00:00 2001 From: Iris Shi <0.0@owo.li> Date: Fri, 1 Aug 2025 13:23:51 +0800 Subject: [PATCH 1/3] [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 5907e21065331..3e54fce46b8da 100644 --- a/llvm/lib/Analysis/InstructionSimplify.cpp +++ b/llvm/lib/Analysis/InstructionSimplify.cpp @@ -3025,9 +3025,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; } @@ -3774,6 +3771,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 64c0d1d7553fe..0f29998e3fd19 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 2e74abde9d05adeec193b3dedd1c539096aa47dd Mon Sep 17 00:00:00 2001 From: Iris Shi <0.0@owo.li> Date: Thu, 7 Aug 2025 15:17:05 +0800 Subject: [PATCH 2/3] 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 3e54fce46b8da..d76c6dbfb494f 100644 --- a/llvm/lib/Analysis/InstructionSimplify.cpp +++ b/llvm/lib/Analysis/InstructionSimplify.cpp @@ -3772,13 +3772,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 387267a6ecd614bdf5a42bccad2de9504904e8e9 Mon Sep 17 00:00:00 2001 From: Iris Shi <0.0@owo.li> Date: Tue, 12 Aug 2025 10:34:59 +0800 Subject: [PATCH 3/3] 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 97ac8f2ea47ea..2f827f2a8ad23 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> , [[V:%.*]] +; CHECK-NEXT: [[CMP:%.*]] = icmp ule <2 x i8> [[SHL]], +; CHECK-NEXT: ret <2 x i1> [[CMP]] ; %shl = shl <2 x i8> , %V %cmp = icmp ule <2 x i8> %shl,