[llvm] [InstCombine] Fold `(op x, ({z,s}ext (icmp eq x, C)))` to select (PR #89020)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Thu Apr 18 02:58:04 PDT 2024


================
@@ -4734,6 +4734,89 @@ void InstCombinerImpl::tryToSinkInstructionDbgValues(
   }
 }
 
+// If we have:
+//  `(op X, (zext/sext (icmp eq X, C)))`
+// We can transform it to:
+//  `(select (icmp eq X, C), (op C, (zext/sext 1)), (op X, 0))`
+// We do so if the `zext/sext` is one use and `(op X, 0)` simplifies.
+Value *InstCombinerImpl::foldOpOfXWithXEqC(Value *Op, const SimplifyQuery &SQ) {
----------------
nikic wrote:

FWIW, I think the additional complexity is actually quite significant. Doing this for just add is something like:

```
  if (match(I, m_c_Add(m_Value(X),
                       m_ZExt(m_CombineAnd(
                           m_Value(Cond),
                           m_ICmp(Pred, m_Deferred(X), m_ImmConstant(C)))))) &&
      Pred == ICmpInst::ICMP_EQ) 
    return Builder.CreateSelect(
        Cond, Builder.CreateAdd(C, ConstantInt::get(C->getType(), 1)), X);
```

Generalizing it to other operations is a hundred lines of code here, plus a dozen call sites elsewhere.

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


More information about the llvm-commits mailing list