[llvm] [BDCE] Handle multi-use `and`/`or` on demanded bits (PR #79688)

via llvm-commits llvm-commits at lists.llvm.org
Sat Jan 27 19:24:22 PST 2024


================
@@ -125,6 +125,28 @@ static bool bitTrackingDCE(Function &F, DemandedBits &DB) {
       }
     }
 
+    // Simplify `and` or `or` when their mask does not affect the demanded bits.
+    if (auto *BO = dyn_cast<BinaryOperator>(&I)) {
+      unsigned Opc = BO->getOpcode();
+      if (Opc == Instruction::And || Opc == Instruction::Or) {
+        APInt Demanded = DB.getDemandedBits(BO);
+        if (!Demanded.isAllOnes()) {
+          if (auto *CV = dyn_cast<ConstantInt>(BO->getOperand(1))) {
+            APInt Mask = CV->getValue();
+            if ((Opc == Instruction::And && Demanded.isSubsetOf(Mask)) ||
+                (Opc == Instruction::Or && !Demanded.intersects(Mask))) {
----------------
XChy wrote:

```suggestion
          const APInt *Mask;
          if (match(BO->getOperand(1), m_APInt(Mask))) {
            if ((Opc == Instruction::And && Demanded.isSubsetOf(*Mask)) ||
                (Opc == Instruction::Or && !Demanded.intersects(*Mask))) {
```
Match m_APInt to handle the splat.

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


More information about the llvm-commits mailing list