Skip to content

[SelectionDAGBuilder] Use address width when lowering ptrtoaddr #139423

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: users/arichardson/spr/main.selectiondagbuilder-use-address-width-when-lowering-ptrtoaddr
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3884,8 +3884,15 @@ 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);
EVT AddrVT = TLI.getValueType(DL, DL.getAddressType(Op0->getType()));
N = DAG.getPtrExtOrTrunc(N, getCurSDLoc(), AddrVT);
Copy link
Contributor

Choose a reason for hiding this comment

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

The LangRef says that it's always zero extend, but DAG.getPtrExtOrTrunc's comment suggests it may decide to be another extension some day?

N = DAG.getZExtOrTrunc(N, getCurSDLoc(), TLI.getValueType(DL, I.getType()));
setValue(&I, N);
}

void SelectionDAGBuilder::visitPtrToInt(const User &I) {
Expand Down
11 changes: 5 additions & 6 deletions llvm/test/CodeGen/AMDGPU/ptrtoint-ptrtoaddr-p8.ll
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ define <2 x i64> @ptrtoaddr_vec(ptr addrspace(8) %ignored, <2 x ptr addrspace(8)
; CHECK-LABEL: ptrtoaddr_vec:
; CHECK: ; %bb.0:
; CHECK-NEXT: s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
; CHECK-NEXT: v_mov_b32_e32 v3, v9
; CHECK-NEXT: v_mov_b32_e32 v2, v8
; CHECK-NEXT: v_mov_b32_e32 v1, v5
; CHECK-NEXT: v_and_b32_e32 v1, 0xffff, v5
; CHECK-NEXT: v_and_b32_e32 v3, 0xffff, v9
; CHECK-NEXT: v_mov_b32_e32 v0, v4
; CHECK-NEXT: s_setpc_b64 s[30:31]
%ret = ptrtoaddr <2 x ptr addrspace(8)> %ptr to <2 x i64>
Expand All @@ -78,15 +78,14 @@ define zeroext i256 @ptrtoint_ext(ptr addrspace(8) %ignored, ptr addrspace(8) %p
}

;; Check that we extend the offset to i256 instead of reinterpreting all bits.
;; FIXME: this is wrong, we are removing the trunc to i48:
define zeroext i256 @ptrtoaddr_ext(ptr addrspace(8) %ignored, ptr addrspace(8) %ptr) {
; CHECK-LABEL: ptrtoaddr_ext:
; CHECK: ; %bb.0:
; CHECK-NEXT: s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
; CHECK-NEXT: v_mov_b32_e32 v3, v7
; CHECK-NEXT: v_mov_b32_e32 v2, v6
; CHECK-NEXT: v_mov_b32_e32 v1, v5
; CHECK-NEXT: v_mov_b32_e32 v0, v4
; CHECK-NEXT: v_and_b32_e32 v1, 0xffff, v5
; CHECK-NEXT: v_mov_b32_e32 v2, 0
; CHECK-NEXT: v_mov_b32_e32 v3, 0
; CHECK-NEXT: v_mov_b32_e32 v4, 0
; CHECK-NEXT: v_mov_b32_e32 v5, 0
; CHECK-NEXT: v_mov_b32_e32 v6, 0
Expand Down
Loading