Skip to content

Commit 900d46f

Browse files
committedMay 15, 2014
Don't insert lifetime.end markers between a musttail call and ret
The allocas going out of scope are immediately killed by the return instruction. This is a resend of r208912, which was committed accidentally. Reviewers: chandlerc Differential Revision: http://reviews.llvm.org/D3792 llvm-svn: 208920
1 parent 61fdef6 commit 900d46f

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed
 

‎llvm/lib/Transforms/Utils/InlineFunction.cpp

+12-2
Original file line numberDiff line numberDiff line change
@@ -755,8 +755,13 @@ bool llvm::InlineFunction(CallSite CS, InlineFunctionInfo &IFI,
755755
}
756756

757757
builder.CreateLifetimeStart(AI, AllocaSize);
758-
for (ReturnInst *RI : Returns)
758+
for (ReturnInst *RI : Returns) {
759+
// Don't insert llvm.lifetime.end calls between a musttail call and a
760+
// return. The return kills all local allocas.
761+
if (InlinedMustTailCalls && getPrecedingMustTailCall(RI))
762+
continue;
759763
IRBuilder<>(RI).CreateLifetimeEnd(AI, AllocaSize);
764+
}
760765
}
761766
}
762767

@@ -774,8 +779,13 @@ bool llvm::InlineFunction(CallSite CS, InlineFunctionInfo &IFI,
774779

775780
// Insert a call to llvm.stackrestore before any return instructions in the
776781
// inlined function.
777-
for (ReturnInst *RI : Returns)
782+
for (ReturnInst *RI : Returns) {
783+
// Don't insert llvm.stackrestore calls between a musttail call and a
784+
// return. The return will restore the stack pointer.
785+
if (InlinedMustTailCalls && getPrecedingMustTailCall(RI))
786+
continue;
778787
IRBuilder<>(RI).CreateCall(StackRestore, SavedPtr);
788+
}
779789
}
780790

781791
// If we are inlining for an invoke instruction, we must make sure to rewrite

‎llvm/test/Transforms/Inline/inline-tail.ll

+36
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,42 @@ define void @test_musttail_basic_a(i32* %p) {
4949
ret void
5050
}
5151

52+
; Don't insert lifetime end markers here, the lifetime is trivially over due
53+
; the return.
54+
; CHECK: define void @test_byval_a(
55+
; CHECK: musttail call void @test_byval_c(
56+
; CHECK-NEXT: ret void
57+
58+
declare void @test_byval_c(i32* byval %p)
59+
define internal void @test_byval_b(i32* byval %p) {
60+
musttail call void @test_byval_c(i32* byval %p)
61+
ret void
62+
}
63+
define void @test_byval_a(i32* byval %p) {
64+
musttail call void @test_byval_b(i32* byval %p)
65+
ret void
66+
}
67+
68+
; Don't insert a stack restore, we're about to return.
69+
; CHECK: define void @test_dynalloca_a(
70+
; CHECK: call i8* @llvm.stacksave(
71+
; CHECK: alloca i8, i32 %n
72+
; CHECK: musttail call void @test_dynalloca_c(
73+
; CHECK-NEXT: ret void
74+
75+
declare void @escape(i8* %buf)
76+
declare void @test_dynalloca_c(i32* byval %p, i32 %n)
77+
define internal void @test_dynalloca_b(i32* byval %p, i32 %n) alwaysinline {
78+
%buf = alloca i8, i32 %n ; dynamic alloca
79+
call void @escape(i8* %buf) ; escape it
80+
musttail call void @test_dynalloca_c(i32* byval %p, i32 %n)
81+
ret void
82+
}
83+
define void @test_dynalloca_a(i32* byval %p, i32 %n) {
84+
musttail call void @test_dynalloca_b(i32* byval %p, i32 %n)
85+
ret void
86+
}
87+
5288
; We can't merge the returns.
5389
; CHECK: define void @test_multiret_a(
5490
; CHECK: musttail call void @test_multiret_c(

0 commit comments

Comments
 (0)
Please sign in to comment.