1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-11-26 12:43:36 +01:00
llvm-mirror/test/Transforms/GlobalOpt/preallocated.ll
Arthur Eubanks edf99b5e43 [GlobalOpt] Remove preallocated calls when possible
When possible (e.g. internal linkage), strip preallocated attribute off
parameters/arguments.
This requires removing the "preallocated" operand bundle from the call
site, replacing @llvm.call.preallocated.arg() with an alloca and a
bitcast to i8*, and removing the @llvm.call.preallocated.setup(). Since
@llvm.call.preallocated.arg() can be called multiple times with the same
arg index, we create an alloca per arg index.
We add a @llvm.stacksave() where the @llvm.call.preallocated.setup() was
and a @llvm.stackrestore() after the preallocated call to prevent the
stack from blowing up. This is valid because the argument would normally
not exist on the stack after the call before the transformation.

This does not currently handle all possible preallocated calls. We will
need to figure out where to put @llvm.stackrestore() in the cases where
there is no obvious place to put it, for example conditional
preallocated calls, invokes.

This sort of transformation may need to be moved to somewhere more
accessible to accomodate similar transformations (like inlining) in the
future.

Reviewers: efriedma, hans

Subscribers: hiraditya, llvm-commits

Tags: #llvm

Differential Revision: https://reviews.llvm.org/D80951
2020-06-18 09:56:13 -07:00

89 lines
3.5 KiB
LLVM

; RUN: opt < %s -globalopt -S | FileCheck %s
declare token @llvm.call.preallocated.setup(i32)
declare i8* @llvm.call.preallocated.arg(token, i32)
declare i32 @__CxxFrameHandler3(...)
; Don't touch functions with any musttail calls
define internal i32 @preallocated_musttail(i32* preallocated(i32) %p) {
; CHECK-LABEL: define internal i32 @preallocated_musttail(i32* preallocated(i32) %p)
%rv = load i32, i32* %p
ret i32 %rv
}
define i32 @call_preallocated_musttail(i32* preallocated(i32) %a) {
%r = musttail call i32 @preallocated_musttail(i32* preallocated(i32) %a)
ret i32 %r
}
; CHECK-LABEL: define i32 @call_preallocated_musttail(i32* preallocated(i32) %a)
; CHECK: musttail call i32 @preallocated_musttail(i32* preallocated(i32) %a)
define i32 @call_preallocated_musttail_without_musttail() {
%c = call token @llvm.call.preallocated.setup(i32 1)
%N = call i8* @llvm.call.preallocated.arg(token %c, i32 0) preallocated(i32)
%n = bitcast i8* %N to i32*
%r = call i32 @preallocated_musttail(i32* preallocated(i32) %n) ["preallocated"(token %c)]
ret i32 %r
}
; CHECK-LABEL: define i32 @call_preallocated_musttail_without_musttail()
; CHECK: call i32 @preallocated_musttail(i32* preallocated(i32) %n)
; Check that only one alloca per preallocated arg
define internal i32 @preallocated(i32* preallocated(i32) %a) {
; CHECK-LABEL: define internal fastcc i32 @preallocated(i32* %a)
%rv = load i32, i32* %a
ret i32 %rv
}
declare void @foo(i8*)
define i32 @call_preallocated_multiple_args() {
; CHECK-LABEL: define i32 @call_preallocated_multiple_args()
; CHECK-NEXT: [[SS:%[0-9a-zA-Z_]+]] = call i8* @llvm.stacksave()
; CHECK-NEXT: [[ARG0:%[0-9a-zA-Z_]+]] = alloca i32
; CHECK-NEXT: [[ARG1:%[0-9a-zA-Z_]+]] = bitcast i32* [[ARG0]] to i8*
; CHECK-NEXT: call void @foo(i8* [[ARG1]])
; CHECK-NEXT: call void @foo(i8* [[ARG1]])
; CHECK-NEXT: call void @foo(i8* [[ARG1]])
; CHECK-NEXT: [[ARG2:%[0-9a-zA-Z_]+]] = bitcast i8* [[ARG1]] to i32*
; CHECK-NEXT: call fastcc i32 @preallocated(i32* [[ARG2]])
; CHECK-NEXT: call void @llvm.stackrestore(i8* [[SS]])
; CHECK-NEXT: ret
%c = call token @llvm.call.preallocated.setup(i32 1)
%a1 = call i8* @llvm.call.preallocated.arg(token %c, i32 0) preallocated(i32)
call void @foo(i8* %a1)
%a2 = call i8* @llvm.call.preallocated.arg(token %c, i32 0) preallocated(i32)
call void @foo(i8* %a2)
%a3 = call i8* @llvm.call.preallocated.arg(token %c, i32 0) preallocated(i32)
call void @foo(i8* %a3)
%b = bitcast i8* %a3 to i32*
%r = call i32 @preallocated(i32* preallocated(i32) %b) ["preallocated"(token %c)]
ret i32 %r
}
; Don't touch functions with any invokes
define internal i32 @preallocated_invoke(i32* preallocated(i32) %p) {
; CHECK-LABEL: define internal i32 @preallocated_invoke(i32* preallocated(i32) %p)
%rv = load i32, i32* %p
ret i32 %rv
}
define i32 @call_preallocated_invoke() personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*) {
%c = call token @llvm.call.preallocated.setup(i32 1)
%a = call i8* @llvm.call.preallocated.arg(token %c, i32 0) preallocated(i32)
%b = bitcast i8* %a to i32*
%r = invoke i32 @preallocated_invoke(i32* preallocated(i32) %b) ["preallocated"(token %c)]
to label %conta unwind label %contb
conta:
ret i32 %r
contb:
%s = catchswitch within none [label %catch] unwind to caller
catch:
%p = catchpad within %s []
catchret from %p to label %cont
cont:
ret i32 42
}
; CHECK-LABEL: define i32 @call_preallocated_invoke()
; CHECK: invoke i32 @preallocated_invoke(i32* preallocated(i32) %b)