llvm-project/llvm/test/Bitcode/ptrtoaddr.ll
Alexander Richardson 3a4b351ba1
[IR] Introduce the ptrtoaddr instruction
This introduces a new `ptrtoaddr` instruction which is similar to
`ptrtoint` but has two differences:

1) Unlike `ptrtoint`, `ptrtoaddr` does not capture provenance
2) `ptrtoaddr` only extracts (and then extends/truncates) the low
   index-width bits of the pointer

For most architectures, difference 2) does not matter since index (address)
width and pointer representation width are the same, but this does make a
difference for architectures that have pointers that aren't just plain
integer addresses such as AMDGPU fat pointers or CHERI capabilities.

This commit introduces textual and bitcode IR support as well as basic code
generation, but optimization passes do not handle the new instruction yet
so it may result in worse code than using ptrtoint. Follow-up changes will
update capture tracking, etc. for the new instruction.

RFC: https://discourse.llvm.org/t/clarifiying-the-semantics-of-ptrtoint/83987/54

Reviewed By: nikic

Pull Request: https://github.com/llvm/llvm-project/pull/139357
2025-08-08 10:12:39 -07:00

28 lines
951 B
LLVM

; RUN: llvm-as < %s | llvm-dis | llvm-as | llvm-dis | FileCheck %s
target datalayout = "p1:64:64:64:32"
@i_as0 = global i32 0
@global_cast_as0 = global i64 ptrtoaddr (ptr @i_as0 to i64)
; CHECK: @global_cast_as0 = global i64 ptrtoaddr (ptr @i_as0 to i64)
@i_as1 = addrspace(1) global i32 0
@global_cast_as1 = global i32 ptrtoaddr (ptr addrspace(1) @i_as1 to i32)
; CHECK: @global_cast_as1 = global i32 ptrtoaddr (ptr addrspace(1) @i_as1 to i32)
define i64 @test_as0(ptr %p) {
%addr = ptrtoaddr ptr %p to i64
; CHECK: %addr = ptrtoaddr ptr %p to i64
ret i64 %addr
}
define i32 @test_as1(ptr addrspace(1) %p) {
%addr = ptrtoaddr ptr addrspace(1) %p to i32
; CHECK: %addr = ptrtoaddr ptr addrspace(1) %p to i32
ret i32 %addr
}
define <2 x i32> @test_vec_as1(<2 x ptr addrspace(1)> %p) {
%addr = ptrtoaddr <2 x ptr addrspace(1)> %p to <2 x i32>
; CHECK: %addr = ptrtoaddr <2 x ptr addrspace(1)> %p to <2 x i32>
ret <2 x i32> %addr
}