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

164 lines
4.4 KiB
LLVM

; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 4
; RUN: opt -S -passes='normalize<no-rename-all>' -verify-each < %s | FileCheck %s
define void @foo() {
; CHECK-LABEL: define void @foo() {
; CHECK-NEXT: bb17254:
; CHECK-NEXT: ret void
;
ret void
}
define void @empty_basic_block() {
; CHECK-LABEL: define void @empty_basic_block() {
; CHECK-NEXT: exit:
; CHECK-NEXT: ret void
;
exit:
ret void
}
declare void @effecting()
; Place dead instruction(s) before the terminator
define void @call_effecting() {
; CHECK-LABEL: define void @call_effecting() {
; CHECK-NEXT: bb14885:
; CHECK-NEXT: call void @effecting()
; CHECK-NEXT: [[TMP0:%.*]] = add i32 0, 1
; CHECK-NEXT: ret void
;
%1 = add i32 0, 1
call void @effecting()
ret void
}
define void @dont_move_above_phi() {
; CHECK-LABEL: define void @dont_move_above_phi() {
; CHECK-NEXT: bb76951:
; CHECK-NEXT: br label [[EXIT:%.*]]
; CHECK: exit:
; CHECK-NEXT: [[TMP0:%.*]] = phi i32 [ 0, [[BB76951:%.*]] ]
; CHECK-NEXT: call void @effecting()
; CHECK-NEXT: ret void
;
br label %exit
exit:
%1 = phi i32 [0, %0]
call void @effecting()
ret void
}
define void @dont_move_above_alloca() {
; CHECK-LABEL: define void @dont_move_above_alloca() {
; CHECK-NEXT: bb14885:
; CHECK-NEXT: [[TMP0:%.*]] = alloca i32, align 4
; CHECK-NEXT: call void @effecting()
; CHECK-NEXT: ret void
;
%1 = alloca i32
call void @effecting()
ret void
}
declare void @effecting1()
define void @dont_reorder_effecting() {
; CHECK-LABEL: define void @dont_reorder_effecting() {
; CHECK-NEXT: bb45003:
; CHECK-NEXT: call void @effecting()
; CHECK-NEXT: call void @effecting1()
; CHECK-NEXT: ret void
;
call void @effecting()
call void @effecting1()
ret void
}
declare void @effecting2(i32)
define void @dont_reorder_effecting1() {
; CHECK-LABEL: define void @dont_reorder_effecting1() {
; CHECK-NEXT: bb45003:
; CHECK-NEXT: [[ONE:%.*]] = add i32 1, 1
; CHECK-NEXT: call void @effecting2(i32 [[ONE]])
; CHECK-NEXT: [[TWO:%.*]] = add i32 2, 2
; CHECK-NEXT: call void @effecting2(i32 [[TWO]])
; CHECK-NEXT: ret void
;
%one = add i32 1, 1
%two = add i32 2, 2
call void @effecting2(i32 %one)
call void @effecting2(i32 %two)
ret void
}
define void @dont_reorder_across_blocks() {
; CHECK-LABEL: define void @dont_reorder_across_blocks() {
; CHECK-NEXT: bb76951:
; CHECK-NEXT: [[ONE:%.*]] = add i32 1, 1
; CHECK-NEXT: br label [[EXIT:%.*]]
; CHECK: exit:
; CHECK-NEXT: call void @effecting2(i32 [[ONE]])
; CHECK-NEXT: ret void
;
%one = add i32 1, 1
br label %exit
exit:
call void @effecting2(i32 %one)
ret void
}
define void @independentldst(ptr %a, ptr %b) {
; CHECK-LABEL: define void @independentldst(
; CHECK-SAME: ptr [[A:%.*]], ptr [[B:%.*]]) {
; CHECK-NEXT: bb10495:
; CHECK-NEXT: %"vl12961([[B]])" = load i32, ptr [[B]], align 4
; CHECK-NEXT: store i32 %"vl12961([[B]])", ptr [[A]], align 4
; CHECK-NEXT: %"vl89528([[A]])" = load i32, ptr [[A]], align 4
; CHECK-NEXT: store i32 %"vl89528([[A]])", ptr [[B]], align 4
; CHECK-NEXT: ret void
;
%2 = load i32, ptr %a
%3 = load i32, ptr %b
store i32 %3, ptr %a
store i32 %2, ptr %b
ret void
}
define void @multiple_use_ld(ptr %a, ptr %b) {
; CHECK-LABEL: define void @multiple_use_ld(
; CHECK-SAME: ptr [[A:%.*]], ptr [[B:%.*]]) {
; CHECK-NEXT: bb14927:
; CHECK-NEXT: %"vl16793([[A]])" = load i32, ptr [[A]], align 4
; CHECK-NEXT: store i32 %"vl16793([[A]])", ptr [[A]], align 4
; CHECK-NEXT: %"vl89528([[B]])" = load i32, ptr [[B]], align 4
; CHECK-NEXT: store i32 %"vl89528([[B]])", ptr [[B]], align 4
; CHECK-NEXT: store i32 %"vl16793([[A]])", ptr [[A]], align 4
; CHECK-NEXT: ret void
;
%2 = load i32, ptr %a
store i32 %2, ptr %a
%3 = load i32, ptr %b
store i32 %3, ptr %b
store i32 %2, ptr %a
ret void
}
; This is an incorrect transformation. Moving the store above the load could
; change the loaded value. To do this, the pointers would need to be `noalias`.
define void @undef_st(ptr %a, ptr %b) {
; CHECK-LABEL: define void @undef_st(
; CHECK-SAME: ptr [[A:%.*]], ptr [[B:%.*]]) {
; CHECK-NEXT: bb10495:
; CHECK-NEXT: store i32 undef, ptr [[B]], align 4
; CHECK-NEXT: %"vl16028([[A]])" = load i32, ptr [[A]], align 4
; CHECK-NEXT: store i32 %"vl16028([[A]])", ptr [[A]], align 4
; CHECK-NEXT: ret void
;
%2 = load i32, ptr %a
store i32 undef, ptr %b
store i32 %2, ptr %a
ret void
}