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

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 13 01:54:34 PDT 2025


================
@@ -3572,6 +3572,28 @@ Instruction *InstCombinerImpl::foldSelectToCmp(SelectInst &SI) {
   if (!LHS->getType()->isIntOrIntVectorTy())
     return nullptr;
 
+  // If there is no -1, 0 or 1 at TV, then invert the select statement and try
+  // to canonicalize to one of the forms above
+  if (!match(TV, m_AllOnes()) && !match(TV, m_One()) && !match(TV, m_Zero())) {
+    if (!match(FV, m_AllOnes()) && !match(FV, m_One()) && !match(FV, m_Zero()))
+      return nullptr;
+    Pred = ICmpInst::getInverseCmpPredicate(Pred);
+    std::swap(TV, FV);
+  }
+
+  if (ICmpInst::isGE(Pred) || ICmpInst::isLE(Pred)) {
----------------
dtcxzyw wrote:

```suggestion
  if (ICmpInst::isNonStrictPredicate(Pred)) {
```

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


More information about the llvm-commits mailing list