[llvm] [SelectionDAG] Introduce ISD::PTRADD (PR #140017)
Fabian Ritter via llvm-commits
llvm-commits at lists.llvm.org
Thu May 15 05:36:47 PDT 2025
================
@@ -2617,6 +2620,100 @@ 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.
+/// This function was adapted from DAGCombiner::visitPTRADD() from the Morello
+/// project, which is based on CHERI.
+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);
+
+ // fold (ptradd undef, y) -> undef
+ if (N0.isUndef())
+ return N0;
+
+ // fold (ptradd x, undef) -> undef
+ if (N1.isUndef())
+ return DAG.getUNDEF(PtrVT);
+
+ // fold (ptradd x, 0) -> x
+ if (isNullConstant(N1))
+ return N0;
+
+ if (N0.getOpcode() == ISD::PTRADD &&
+ !reassociationCanBreakAddressingModePattern(ISD::PTRADD, DL, N, N0, N1)) {
+ SDValue X = N0.getOperand(0);
----------------
ritter-x2a wrote:
@rgwott provided explanations for changes in the DAGCombiner in the original PR, from which this is extracted:
- https://github.com/llvm/llvm-project/pull/105669#discussion_r1727485653
- https://github.com/llvm/llvm-project/pull/105669#discussion_r1727494906
Or, if you're referring to the use of `AddToWorklist(Add.getNode())` to let the worklist algorithm handle the new node instead of handling it manually: That way seems to be more consistent with the other DAG combines, where `visit` is almost never called directly (see here for previous discussion: https://github.com/llvm/llvm-project/pull/105669/files#r2065675292 ).
https://github.com/llvm/llvm-project/pull/140017
More information about the llvm-commits
mailing list