Skip to content
10 changes: 8 additions & 2 deletions llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If these are the same type, why do you need the truncate?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While most targets will have them be the same, the Op0 type can be wider. This is the case e.g. for the AMD GPU buffer pointers.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added a comment, hopefully makes sense now.

setValue(&I, N);
}

void SelectionDAGBuilder::visitPtrToInt(const User &I) {
Expand Down
Loading