diff --git a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp index 7aa1fadd10dfc..6fcfb5165a061 100644 --- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp +++ b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp @@ -3980,8 +3980,16 @@ 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); + const auto &TLI = DAG.getTargetLoweringInfo(); + const DataLayout &DL = DAG.getDataLayout(); + // ptrtoaddr is equivalent to a truncate of ptrtoint to address/index width + auto Op0 = I.getOperand(0); + SDValue N = getValue(Op0); + // By definition the type of the ptrtoaddr must be equal to the address type. + assert(I.getType() == DL.getAddressType(Op0->getType())); + EVT AddrVT = TLI.getValueType(DL, I.getType()); + N = DAG.getNode(ISD::TRUNCATE, getCurSDLoc(), AddrVT, N); + setValue(&I, N); } void SelectionDAGBuilder::visitPtrToInt(const User &I) {