[llvm] [InstSimplify] Simplify `icmp X & C1, X & C2` when `(C1 & C2) == C1/C2` (PR #65905)

via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 12 09:05:41 PDT 2023


================
@@ -3436,6 +3436,32 @@ static Value *simplifyICmpWithBinOp(CmpInst::Predicate Pred, Value *LHS,
       if (Value *V = simplifyICmpInst(Pred, LBO->getOperand(1),
                                       RBO->getOperand(1), Q, MaxRecurse - 1))
         return V;
+      break;
+    }
+    // icmp X & C1, X & C2 where (C1 & C2) == C1/C2
+    // icmp X | C1, X | C2 where (C1 & C2) == C1/C2
+    case Instruction::And:
+    case Instruction::Or: {
+      if (ICmpInst::isUnsigned(Pred)) {
+        const APInt *C1, *C2;
+        if (match(LBO->getOperand(1), m_APInt(C1)) &&
+            match(RBO->getOperand(1), m_APInt(C2))) {
+          if (C1->isSubsetOf(*C2)) {
----------------
goldsteinn wrote:

Think cleaner would be something like:

```
if(!C1->isSubsetOf(*C2)) {
    swap(C1, C2);
    Pred = getSwappedPred(Pred);
}
if(C1->isSubsetOf(*C2)) {
....
}
```

So you don't need to duplicate logic.

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


More information about the llvm-commits mailing list