Skip to content

Commit c422881

Browse files
authored
[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: llvm/llvm-project#139423
1 parent dc4cef8 commit c422881

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3971,8 +3971,14 @@ void SelectionDAGBuilder::visitSIToFP(const User &I) {
39713971
}
39723972

39733973
void SelectionDAGBuilder::visitPtrToAddr(const User &I) {
3974-
// FIXME: this is not correct for pointers with addr width != pointer width
3975-
visitPtrToInt(I);
3974+
SDValue N = getValue(I.getOperand(0));
3975+
// By definition the type of the ptrtoaddr must be equal to the address type.
3976+
const auto &TLI = DAG.getTargetLoweringInfo();
3977+
EVT AddrVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
3978+
// The address width must be smaller or equal to the pointer representation
3979+
// width, so we lower ptrtoaddr as a truncate (possibly folded to a no-op).
3980+
N = DAG.getNode(ISD::TRUNCATE, getCurSDLoc(), AddrVT, N);
3981+
setValue(&I, N);
39763982
}
39773983

39783984
void SelectionDAGBuilder::visitPtrToInt(const User &I) {

0 commit comments

Comments
 (0)