[llvm-branch-commits] [llvm] [AMDGPU][SDAG] Add ISD::PTRADD DAG combines (PR #142739)

Fabian Ritter via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Fri Jun 13 05:29:11 PDT 2025


================
@@ -2628,6 +2630,87 @@ SDValue DAGCombiner::foldSubToAvg(SDNode *N, const SDLoc &DL) {
   return SDValue();
 }
 
+/// Try to fold a pointer arithmetic node.
+/// This needs to be done separately from normal addition, because pointer
+/// addition is not commutative.
+SDValue DAGCombiner::visitPTRADD(SDNode *N) {
+  SDValue N0 = N->getOperand(0);
+  SDValue N1 = N->getOperand(1);
+  EVT PtrVT = N0.getValueType();
+  EVT IntVT = N1.getValueType();
+  SDLoc DL(N);
+
+  // This is already ensured by an assert in SelectionDAG::getNode(). Several
+  // combines here depend on this assumption.
+  assert(PtrVT == IntVT &&
+         "PTRADD with different operand types is not supported");
+
+  // fold (ptradd x, 0) -> x
+  if (isNullConstant(N1))
+    return N0;
+
+  // fold (ptradd 0, x) -> x
+  if (isNullConstant(N0) && PtrVT == IntVT)
+    return N1;
----------------
ritter-x2a wrote:

Yes, @arichardson argued for adding it redundantly here in [a thread below](https://app.graphite.dev/github/pr/llvm/llvm-project/142739/%5BAMDGPU%5D%5BSDAG%5D-Add-ISD-PTRADD-DAG-combines#comment-PRRC_kwDOBITxeM5_IBA7), to highlight this constraint more explicitly for when the assertion might be relaxed in the future.

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


More information about the llvm-branch-commits mailing list