[llvm] c422881 - [SelectionDAGBuilder] Use address width when lowering ptrtoaddr

via llvm-commits llvm-commits at lists.llvm.org
Wed Oct 15 15:49:42 PDT 2025


Author: Alexander Richardson
Date: 2025-10-15T15:49:38-07:00
New Revision: c422881670faf30b545362b3af2c310a6800cde5

URL: https://github.com/llvm/llvm-project/commit/c422881670faf30b545362b3af2c310a6800cde5
DIFF: https://github.com/llvm/llvm-project/commit/c422881670faf30b545362b3af2c310a6800cde5.diff

LOG: [SelectionDAGBuilder] Use address width when lowering ptrtoaddr

Instead of just deferring to ptrtoint, we should truncate to the index
width and then perform the ZextOrTrunc.
This is effectively NFC since ptrtoint ends up doing the same thing, but
handling it explicitly is cleaner and will make it easier to eventually
upstream the changes needed for CHERI support.

Reviewed By: nikic, arsenm

Pull Request: https://github.com/llvm/llvm-project/pull/139423

Added: 
    

Modified: 
    llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
index 0f2b5188fc10a..cb0038c54f8c7 100644
--- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
@@ -3971,8 +3971,14 @@ void SelectionDAGBuilder::visitSIToFP(const User &I) {
 }
 
 void SelectionDAGBuilder::visitPtrToAddr(const User &I) {
-  // FIXME: this is not correct for pointers with addr width != pointer width
-  visitPtrToInt(I);
+  SDValue N = getValue(I.getOperand(0));
+  // By definition the type of the ptrtoaddr must be equal to the address type.
+  const auto &TLI = DAG.getTargetLoweringInfo();
+  EVT AddrVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
+  // The address width must be smaller or equal to the pointer representation
+  // width, so we lower ptrtoaddr as a truncate (possibly folded to a no-op).
+  N = DAG.getNode(ISD::TRUNCATE, getCurSDLoc(), AddrVT, N);
+  setValue(&I, N);
 }
 
 void SelectionDAGBuilder::visitPtrToInt(const User &I) {


        


More information about the llvm-commits mailing list