[llvm] [InstSimplify] Canonicalize `X uge 1` to `X ne 0` and `X sle -1` to `X slt 0` in `simplifyICmpInst` (PR #151642)

Iris Shi via llvm-commits llvm-commits at lists.llvm.org
Fri Aug 1 21:26:16 PDT 2025


================
@@ -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);
+    }
+  }
----------------
el-ev wrote:

It seems that the current implementation provides special handling only for comparisons against zero. I'm not sure if flipping other constant and predicate pairs is profitable. For instance, converting `x sgt 0` to `x sge -1` causes regressions.

https://github.com/llvm/llvm-project/pull/151642


More information about the llvm-commits mailing list