[llvm] [DAG] select m, sub/add(X, C), X --> sub/add (X, and(C, m)) (PR #82441)

via llvm-commits llvm-commits at lists.llvm.org
Sat Feb 24 10:36:10 PST 2024


================
@@ -11660,6 +11660,24 @@ SDValue DAGCombiner::visitSELECT(SDNode *N) {
     }
   }
 
+  // select m, sub(X, C), X --> sub (X, and(C, m))
+  if (N1.getOpcode() == ISD::SUB && N1.getOperand(0) == N2 && N1->hasOneUse() &&
+      DAG.isConstantIntBuildVectorOrConstantInt(N1.getOperand(1)) &&
+      N0.getScalarValueSizeInBits() == N1.getScalarValueSizeInBits()) {
+    return DAG.getNode(ISD::SUB, DL, N1.getValueType(), N2,
+                       DAG.getNode(ISD::AND, DL, N0.getValueType(), N1.getOperand(1),
+                                   N0));
+  }
+
+  // select (sext m), (add X, C), X --> (add X, (and C, (sext m))))
+  if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N2 && N1->hasOneUse() &&
+      DAG.isConstantIntBuildVectorOrConstantInt(N1.getOperand(1)) && 
+      N0.getScalarValueSizeInBits() == N1.getScalarValueSizeInBits()) {
+    return DAG.getNode(ISD::ADD, DL, N1.getValueType(), N2,
+                       DAG.getNode(ISD::AND, DL, N0.getValueType(), N1.getOperand(1),
+                                   N0));
+  }
----------------
goldsteinn wrote:

The sub case could be `select m, (sub C, X), (sub 0, X)`

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


More information about the llvm-commits mailing list