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

Simon Pilgrim via llvm-commits llvm-commits at lists.llvm.org
Wed Feb 21 03:22:31 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));
+  }
----------------
RKSimon wrote:

You should be able to merge the ADD/SUB folds? However, unless we have test coverage for the SUB case you'll need to drop it and just keep the ADD case.

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


More information about the llvm-commits mailing list