[llvm] [DAG] Fold pow2 masked bittest comparisons (PR #216302)

Simon Pilgrim via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 18 10:00:06 PDT 2026


================
@@ -6778,6 +6778,26 @@ SDValue DAGCombiner::foldLogicOfSetCCs(bool IsAnd, SDValue N0, SDValue N1,
     }
   }
 
+  // (and (setne (and X, LL1), 0), (setne (and X, RL1), 0))
+  // --> (seteq (and X, (LL1|RL1)), (LL1|RL1))
+  // (or  (seteq (and X, LL1), 0), (seteq (and X, RL1), 0))
+  // --> (setne (and X, (LL1|RL1)), (LL1|RL1))
+  if (LL.getOpcode() == ISD::AND && RL.getOpcode() == ISD::AND &&
+      isNullConstant(LR) && isNullConstant(RR) && CC0 == CC1 &&
+      (CC0 == ISD::SETNE || CC0 == ISD::SETEQ)) {
+    SDValue LL0, LL1, RL0, RL1;
+    LL0 = LL.getOperand(0);
+    RL0 = RL.getOperand(0);
+    LL1 = LL.getOperand(1);
+    RL1 = RL.getOperand(1);
+    if (DAG.isKnownToBeAPowerOfTwo(LL1) && DAG.isKnownToBeAPowerOfTwo(RL1) &&
+        LL0 == RL0) {
----------------
RKSimon wrote:

better to test `LL0 == RL0` first as the isKnownToBeAPowerOfTwo calls will be expensive 

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


More information about the llvm-commits mailing list