[llvm] [InstCombine] Optimize (select %x, op(%x), 0) to op(%x) for operations where op(0) == 0 (PR #147605)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 14 02:11:36 PDT 2025


================
@@ -900,10 +904,25 @@ static Instruction *foldSelectZeroOrMul(SelectInst &SI, InstCombinerImpl &IC) {
   // non-zero elements that are masked by undef elements in the compare
   // constant.
   auto *TrueValC = dyn_cast<Constant>(TrueVal);
-  if (TrueValC == nullptr ||
-      !match(FalseVal, m_c_Mul(m_Specific(X), m_Value(Y))) ||
-      !isa<Instruction>(FalseVal))
+  if (TrueValC == nullptr || !isa<Instruction>(FalseVal))
+    return nullptr;
+
+  bool FreezeY;
+  if (match(FalseVal, m_c_Mul(m_Specific(X), m_Value(Y))) ||
+      match(FalseVal, m_c_And(m_Specific(X), m_Value(Y))) ||
+      match(FalseVal, m_FShl(m_Specific(X), m_Specific(X), m_Value(Y))) ||
+      match(FalseVal, m_FShr(m_Specific(X), m_Specific(X), m_Value(Y))) ||
+      match(FalseVal,
+            m_c_Intrinsic<Intrinsic::umin>(m_Specific(X), m_Value(Y)))) {
+    FreezeY = true;
+  } else if (match(FalseVal, m_SDiv(m_Specific(X), m_Value(Y))) ||
+             match(FalseVal, m_UDiv(m_Specific(X), m_Value(Y))) ||
+             match(FalseVal, m_SRem(m_Specific(X), m_Value(Y))) ||
+             match(FalseVal, m_URem(m_Specific(X), m_Value(Y)))) {
----------------
dtcxzyw wrote:

```suggestion
  } else if (match(FalseVal, m_IDiv(m_Specific(X), m_Value(Y))) ||
             match(FalseVal, m_IRem(m_Specific(X), m_Value(Y)))) {
```

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


More information about the llvm-commits mailing list