[llvm] [llvm][InstCombine] Fold signum(x) into scmp(x, 0) (PR #143445)

Yash Solanki via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 13 01:55:28 PDT 2025


================
@@ -3632,6 +3632,55 @@ Instruction *InstCombinerImpl::foldSelectToCmp(SelectInst &SI) {
   if (Replace)
     return replaceInstUsesWith(
         SI, Builder.CreateIntrinsic(SI.getType(), IID, {LHS, RHS}));
+
+  Value *EXT_RHS;
+
+  // For constant y, inequalities aren't always canonical so we need to check
+  // the following manually: (x > y - 1) ? zext(x != y) : -1 (x > y - 1) ?
+  // zext(x > y) : -1
+  if (ICmpInst::isGT(Pred) && match(FV, m_AllOnes()) &&
+      match(TV, m_ZExt(m_c_ICmp(ExtendedCmpPredicate, m_Specific(LHS),
+                                m_Value(EXT_RHS)))) &&
+      (ExtendedCmpPredicate == ICmpInst::ICMP_NE ||
+       ICmpInst::isGT(ExtendedCmpPredicate))) {
----------------
yashnator wrote:

I have modified the code according to Nikita's suggestion, and added this as a test case

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


More information about the llvm-commits mailing list