[llvm] [BDCE] Handle multi-use `select` of `and`s on demanded bits (PR #79688)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Sat Jan 27 03:15:14 PST 2024


================
@@ -125,6 +125,33 @@ static bool bitTrackingDCE(Function &F, DemandedBits &DB) {
       }
     }
 
+    // Simplify select of ands when the mask does not affect the demanded bits.
+    if (SelectInst *SI = dyn_cast<SelectInst>(&I)) {
+      APInt Demanded = DB.getDemandedBits(SI);
+      if (!Demanded.isAllOnes()) {
+        for (Use &U : I.operands()) {
+          auto *And = dyn_cast<BinaryOperator>(&U);
+          if (!And || !And->hasOneUse() || And->getOpcode() != Instruction::And)
+            continue;
+
+          auto *CV = dyn_cast<ConstantInt>(And->getOperand(1));
+          if (!CV)
+            continue;
----------------
dtcxzyw wrote:

```suggestion
          const APInt* Mask;
          if (!match(U, m_OneUse(m_And(m_Specific(&I), m_APInt(Mask)))))
            continue;
```


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


More information about the llvm-commits mailing list