[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;
+
+          APInt Mask = CV->getValue();
+          if ((Mask | ~Demanded).isAllOnes()) {
+            clearAssumptionsOfUsers(And, DB);
+            U.set(And->getOperand(0));
+            Worklist.push_back(And);
+            Changed = true;
----------------
dtcxzyw wrote:

```suggestion
            ++NumSimplified;
            Changed = true;
```

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


More information about the llvm-commits mailing list