
lifetime.start and lifetime.end are primarily intended for use on allocas, to enable stack coloring and other liveness optimizations. This is necessary because all (static) allocas are hoisted into the entry block, so lifetime markers are the only way to convey the actual lifetimes. However, lifetime.start and lifetime.end are currently *allowed* to be used on non-alloca pointers. We don't actually do this in practice, but just the mere fact that this is possible breaks the core purpose of the lifetime markers, which is stack coloring of allocas. Stack coloring can only work correctly if all lifetime markers for an alloca are analyzable. * If a lifetime marker may operate on multiple allocas via a select/phi, we don't know which lifetime actually starts/ends and handle it incorrectly (https://github.com/llvm/llvm-project/issues/104776). * Stack coloring operates on the assumption that all lifetime markers are visible, and not, for example, hidden behind a function call or escaped pointer. It's not possible to change this, as part of the purpose of lifetime markers is that they work even in the presence of escaped pointers, where simple use analysis is insufficient. I don't think there is any way to have coherent semantics for lifetime markers on allocas, while also permitting them on arbitrary pointer values. This PR restricts lifetimes to operate on allocas only. As a followup, I will also drop the size argument, which is superfluous if we always operate on an alloca. (This change also renders various code handling lifetime markers on non-alloca dead. I plan to clean up that kind of code after dropping the size argument as well.) In practice, I've only found a few places that currently produce lifetimes on non-allocas: * CoroEarly replaces the promise alloca with the result of an intrinsic, which will later be replaced back with an alloca. I think this is the only place where there is some legitimate loss of functionality, but I don't think this is particularly important (I don't think we'd expect the promise in a coroutine to admit useful lifetime optimization.) * SafeStack moves unsafe allocas onto a separate frame. We can safely drop lifetimes here, as SafeStack performs its own stack coloring. * Similar for AddressSanitizer, it also moves allocas into separate memory. * LSR sometimes replaces the lifetime argument with a GEP chain of the alloca (where the offsets ultimately cancel out). This is just unnecessary. (Fixed separately in https://github.com/llvm/llvm-project/pull/149492.) * InferAddrSpaces sometimes makes lifetimes operate on an addrspacecast of an alloca. I don't think this is necessary.
119 lines
5.1 KiB
LLVM
119 lines
5.1 KiB
LLVM
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
|
|
; RUN: opt < %s -passes=memcpyopt -S -verify-memoryssa | FileCheck %s
|
|
|
|
; performCallSlotOptzn in MemCpy should not exchange the calls to
|
|
; @llvm.lifetime.start and @llvm.memcpy.
|
|
|
|
declare void @llvm.memcpy.p0.p0.i64(ptr nocapture, ptr nocapture readonly, i64, i1)
|
|
declare void @llvm.lifetime.start.p0(i64, ptr nocapture)
|
|
declare void @llvm.lifetime.end.p0(i64, ptr nocapture)
|
|
|
|
define void @call_slot(ptr nocapture dereferenceable(16) %arg1) {
|
|
; CHECK-LABEL: @call_slot(
|
|
; CHECK-NEXT: bb:
|
|
; CHECK-NEXT: [[TMP:%.*]] = alloca [8 x i8], align 8
|
|
; CHECK-NEXT: call void @llvm.lifetime.start.p0(i64 16, ptr [[TMP]])
|
|
; CHECK-NEXT: [[TMP10:%.*]] = getelementptr inbounds i8, ptr [[TMP]], i64 7
|
|
; CHECK-NEXT: store i8 0, ptr [[TMP10]], align 1
|
|
; CHECK-NEXT: call void @llvm.lifetime.end.p0(i64 16, ptr [[TMP]])
|
|
; CHECK-NEXT: ret void
|
|
;
|
|
bb:
|
|
%tmp = alloca [8 x i8], align 8
|
|
call void @llvm.lifetime.start.p0(i64 16, ptr %tmp)
|
|
%tmp10 = getelementptr inbounds i8, ptr %tmp, i64 7
|
|
store i8 0, ptr %tmp10, align 1
|
|
call void @llvm.memcpy.p0.p0.i64(ptr align 8 %arg1, ptr align 8 %tmp, i64 16, i1 false)
|
|
call void @llvm.lifetime.end.p0(i64 16, ptr %tmp)
|
|
ret void
|
|
}
|
|
|
|
define void @memcpy_memcpy_across_lifetime(ptr noalias %p1, ptr noalias %p2, ptr noalias %p3) {
|
|
; CHECK-LABEL: @memcpy_memcpy_across_lifetime(
|
|
; CHECK-NEXT: [[A:%.*]] = alloca [16 x i8], align 1
|
|
; CHECK-NEXT: call void @llvm.lifetime.start.p0(i64 16, ptr [[A]])
|
|
; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr [[A]], ptr [[P1:%.*]], i64 16, i1 false)
|
|
; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr [[P1]], ptr [[P2:%.*]], i64 16, i1 false)
|
|
; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr [[P2]], ptr [[A]], i64 16, i1 false)
|
|
; CHECK-NEXT: call void @llvm.lifetime.end.p0(i64 16, ptr [[A]])
|
|
; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr [[P3:%.*]], ptr [[P2]], i64 16, i1 false)
|
|
; CHECK-NEXT: ret void
|
|
;
|
|
%a = alloca [16 x i8]
|
|
call void @llvm.lifetime.start.p0(i64 16, ptr %a)
|
|
call void @llvm.memcpy.p0.p0.i64(ptr %a, ptr %p1, i64 16, i1 false)
|
|
call void @llvm.memcpy.p0.p0.i64(ptr %p1, ptr %p2, i64 16, i1 false)
|
|
call void @llvm.memcpy.p0.p0.i64(ptr %p2, ptr %a, i64 16, i1 false)
|
|
call void @llvm.lifetime.end.p0(i64 16, ptr %a)
|
|
call void @llvm.memcpy.p0.p0.i64(ptr %p3, ptr %p2, i64 16, i1 false)
|
|
ret void
|
|
}
|
|
|
|
declare void @call(ptr)
|
|
|
|
define i32 @call_slot_move_lifetime_start() {
|
|
; CHECK-LABEL: @call_slot_move_lifetime_start(
|
|
; CHECK-NEXT: [[TMP:%.*]] = alloca i32, align 4
|
|
; CHECK-NEXT: [[DST:%.*]] = alloca i32, align 4
|
|
; CHECK-NEXT: call void @llvm.lifetime.start.p0(i64 4, ptr [[DST]])
|
|
; CHECK-NEXT: call void @call(ptr [[DST]])
|
|
; CHECK-NEXT: call void @llvm.lifetime.end.p0(i64 4, ptr [[DST]])
|
|
; CHECK-NEXT: [[V:%.*]] = load i32, ptr [[DST]], align 4
|
|
; CHECK-NEXT: ret i32 [[V]]
|
|
;
|
|
%tmp = alloca i32
|
|
%dst = alloca i32
|
|
call void @call(ptr %tmp)
|
|
call void @llvm.lifetime.start.p0(i64 4, ptr %dst)
|
|
call void @llvm.memcpy.p0.p0.i64(ptr align 4 %dst, ptr align 4 %tmp, i64 4, i1 false)
|
|
call void @llvm.lifetime.end.p0(i64 4, ptr %dst)
|
|
%v = load i32, ptr %dst
|
|
ret i32 %v
|
|
}
|
|
|
|
define i32 @call_slot_two_lifetime_starts() {
|
|
; CHECK-LABEL: @call_slot_two_lifetime_starts(
|
|
; CHECK-NEXT: [[TMP:%.*]] = alloca i32, align 4
|
|
; CHECK-NEXT: [[DST:%.*]] = alloca i32, align 4
|
|
; CHECK-NEXT: call void @call(ptr [[TMP]])
|
|
; CHECK-NEXT: call void @llvm.lifetime.start.p0(i64 4, ptr [[DST]])
|
|
; CHECK-NEXT: call void @llvm.lifetime.start.p0(i64 4, ptr [[DST]])
|
|
; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 4 [[DST]], ptr align 4 [[TMP]], i64 4, i1 false)
|
|
; CHECK-NEXT: call void @llvm.lifetime.end.p0(i64 4, ptr [[DST]])
|
|
; CHECK-NEXT: [[V:%.*]] = load i32, ptr [[DST]], align 4
|
|
; CHECK-NEXT: ret i32 [[V]]
|
|
;
|
|
%tmp = alloca i32
|
|
%dst = alloca i32
|
|
call void @call(ptr %tmp)
|
|
call void @llvm.lifetime.start.p0(i64 4, ptr %dst)
|
|
call void @llvm.lifetime.start.p0(i64 4, ptr %dst)
|
|
call void @llvm.memcpy.p0.p0.i64(ptr align 4 %dst, ptr align 4 %tmp, i64 4, i1 false)
|
|
call void @llvm.lifetime.end.p0(i64 4, ptr %dst)
|
|
%v = load i32, ptr %dst
|
|
ret i32 %v
|
|
}
|
|
|
|
define i32 @call_slot_clobber_before_lifetime_start() {
|
|
; CHECK-LABEL: @call_slot_clobber_before_lifetime_start(
|
|
; CHECK-NEXT: [[TMP:%.*]] = alloca i32, align 4
|
|
; CHECK-NEXT: [[DST:%.*]] = alloca i32, align 4
|
|
; CHECK-NEXT: call void @call(ptr [[TMP]])
|
|
; CHECK-NEXT: store i32 0, ptr [[DST]], align 4
|
|
; CHECK-NEXT: call void @llvm.lifetime.start.p0(i64 4, ptr [[DST]])
|
|
; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 4 [[DST]], ptr align 4 [[TMP]], i64 4, i1 false)
|
|
; CHECK-NEXT: call void @llvm.lifetime.end.p0(i64 4, ptr [[DST]])
|
|
; CHECK-NEXT: [[V:%.*]] = load i32, ptr [[DST]], align 4
|
|
; CHECK-NEXT: ret i32 [[V]]
|
|
;
|
|
%tmp = alloca i32
|
|
%dst = alloca i32
|
|
call void @call(ptr %tmp)
|
|
store i32 0, ptr %dst
|
|
call void @llvm.lifetime.start.p0(i64 4, ptr %dst)
|
|
call void @llvm.memcpy.p0.p0.i64(ptr align 4 %dst, ptr align 4 %tmp, i64 4, i1 false)
|
|
call void @llvm.lifetime.end.p0(i64 4, ptr %dst)
|
|
%v = load i32, ptr %dst
|
|
ret i32 %v
|
|
}
|