[llvm] [SelectionDAG] Introduce ISD::PTRADD (PR #140017)
Owen Anderson via llvm-commits
llvm-commits at lists.llvm.org
Thu May 15 05:06:11 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);
----------------
resistor wrote:
This code path also diverges from the `cheriot` version. I need to look into whether one is strictly superior to the other: https://github.com/CHERIoT-Platform/llvm-project/blob/ba63f5a2d112f961b921f19656da7ac61fa1fd1c/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp#L2684
https://github.com/llvm/llvm-project/pull/140017
More information about the llvm-commits
mailing list