Skip to content

Commit a89eed9

Browse files
committed
[SjLjEHPrepare] Configure call sites correctly
After 9fe78db, the pass inserts `store volatile i32 -1, ptr %call_site` before all invoke instruction except the one in the entry block, which has the effect of bypassing landing pads on exceptions. When configuring the call site for a potentially throwing instruction check that it is not `InvokeInst` -- they are handled by earlier code.
1 parent 1ea7ced commit a89eed9

File tree

5 files changed

+53
-12
lines changed

5 files changed

+53
-12
lines changed

llvm/lib/CodeGen/SjLjEHPrepare.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,10 @@ bool SjLjEHPrepareImpl::setupEntryBlockAndCallSites(Function &F) {
435435
// where to look for it.
436436
Builder.CreateCall(FuncCtxFn, FuncCtx);
437437

438+
// Register the function context and make sure it's known to not throw.
439+
CallInst *Register = Builder.CreateCall(RegisterFn, FuncCtx, "");
440+
Register->setDoesNotThrow();
441+
438442
// At this point, we are all set up, update the invoke instructions to mark
439443
// their call_site values.
440444
for (unsigned I = 0, E = Invokes.size(); I != E; ++I) {
@@ -457,15 +461,10 @@ bool SjLjEHPrepareImpl::setupEntryBlockAndCallSites(Function &F) {
457461
if (&BB == &F.front())
458462
continue;
459463
for (Instruction &I : BB)
460-
if (I.mayThrow())
464+
if (!isa<InvokeInst>(I) && I.mayThrow())
461465
insertCallSiteStore(&I, -1);
462466
}
463467

464-
// Register the function context and make sure it's known to not throw
465-
CallInst *Register = CallInst::Create(
466-
RegisterFn, FuncCtx, "", EntryBB->getTerminator()->getIterator());
467-
Register->setDoesNotThrow();
468-
469468
// Following any allocas not in the entry block, update the saved SP in the
470469
// jmpbuf to the new value.
471470
for (BasicBlock &BB : F) {

llvm/test/CodeGen/ARM/sjljehprepare-lower-empty-struct.ll

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,15 @@
1616
define ptr @foo(i8 %a, {} %c) personality ptr @baz {
1717
entry:
1818
; CHECK: bl __Unwind_SjLj_Register
19+
; CHECK-NEXT: mov r0, #1
20+
; CHECK-NEXT: str r0, [sp, #{{[0-9]+}}]
1921
; CHECK-NEXT: {{[A-Z][a-zA-Z0-9]*}}:
2022
; CHECK-NEXT: bl _bar
2123
; CHECK: bl __Unwind_SjLj_Resume
2224

2325
; CHECK-LINUX: bl _Unwind_SjLj_Register
26+
; CHECK-LINUX-NEXT: mov r0, #1
27+
; CHECK-LINUX-NEXT: str r0, [sp, #{{[0-9]+}}]
2428
; CHECK-LINUX-NEXT: .{{[A-Z][a-zA-Z0-9]*}}:
2529
; CHECK-LINUX-NEXT: bl bar
2630
; CHECK-LINUX: bl _Unwind_SjLj_Resume
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
; RUN: opt -p sjlj-eh-prepare %s -S -o - | FileCheck %s
2+
3+
; Check that callsites are set up correctly:
4+
; 1. Throwing call in the entry block does not set call_site
5+
; (function context hasn't been configured yet).
6+
; 2. Throwing call not in the entry block sets call_site to -1
7+
; (reset to the initial state).
8+
; 3. Invoke instructions set call_site to the correct call site number.
9+
; 4. Resume instruction sets call_site to -1 (reset to the initial state).
10+
11+
define void @test_call_sites() personality ptr @__gxx_personality_sj0 {
12+
entry:
13+
; CHECK-NOT: store volatile
14+
; CHECK: call void @may_throw()
15+
call void @may_throw()
16+
17+
; CHECK: store volatile i32 1
18+
; CHECK-NEXT: call void @llvm.eh.sjlj.callsite(i32 1)
19+
; CHECK-NEXT: invoke void @may_throw()
20+
invoke void @may_throw() to label %invoke.cont unwind label %lpad
21+
22+
invoke.cont:
23+
; CHECK: store volatile i32 2
24+
; CHECK-NEXT: call void @llvm.eh.sjlj.callsite(i32 2)
25+
; CHECK-NEXT: invoke void @may_throw()
26+
invoke void @may_throw() to label %try.cont unwind label %lpad
27+
28+
lpad:
29+
; CHECK: store volatile i32 -1
30+
; CHECK-NEXT: resume
31+
%lp = landingpad { ptr, i32 } cleanup
32+
resume { ptr, i32 } %lp
33+
34+
try.cont:
35+
call void @may_throw()
36+
ret void
37+
}
38+
39+
declare void @may_throw()
40+
declare i32 @__gxx_personality_sj0(...)

llvm/test/CodeGen/X86/indirect-branch-tracking-eh2.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ define dso_local i32 @main() #0 personality ptr @__gxx_personality_sj0 {
2424
; NUM-NEXT: movq %rbp, -104(%rbp)
2525
; NUM-NEXT: movq %rsp, -88(%rbp)
2626
; NUM-NEXT: movq $.LBB0_9, -96(%rbp)
27-
; NUM-NEXT: movl $1, -144(%rbp)
2827
; NUM-NEXT: leaq -152(%rbp), %rdi
2928
; NUM-NEXT: callq _Unwind_SjLj_Register@PLT
29+
; NUM-NEXT: movl $1, -144(%rbp)
3030
; NUM-NEXT: .Ltmp0:
3131
; NUM-NEXT: callq _Z3foov
3232
; NUM-NEXT: .Ltmp1:
@@ -110,9 +110,9 @@ define dso_local i32 @main() #0 personality ptr @__gxx_personality_sj0 {
110110
; SJLJ-NEXT: movq %rbp, -104(%rbp)
111111
; SJLJ-NEXT: movq %rsp, -88(%rbp)
112112
; SJLJ-NEXT: movq $.LBB0_9, -96(%rbp)
113-
; SJLJ-NEXT: movl $1, -144(%rbp)
114113
; SJLJ-NEXT: leaq -152(%rbp), %rdi
115114
; SJLJ-NEXT: callq _Unwind_SjLj_Register@PLT
115+
; SJLJ-NEXT: movl $1, -144(%rbp)
116116
; SJLJ-NEXT: .Ltmp0:
117117
; SJLJ-NEXT: callq _Z3foov
118118
; SJLJ-NEXT: .Ltmp1:

llvm/test/CodeGen/X86/sjlj-eh.ll

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,13 @@ try.cont:
4848
; CHECK: movl %esp, -24(%ebp)
4949
; UFC.__jbuf[1] = $EIP
5050
; CHECK: movl $[[RESUME:LBB[0-9]+_[0-9]+]], -28(%ebp)
51-
; UFC.__callsite = 1
52-
; CHECK: movl $1, -60(%ebp)
5351
; _Unwind_SjLj_Register(&UFC);
5452
; CHECK: leal -64(%ebp), %eax
5553
; CHECK: pushl %eax
5654
; CHECK: calll __Unwind_SjLj_Register
5755
; CHECK: addl $4, %esp
5856
; function_that_throws();
57+
; CHECK: movl $1, -60(%ebp)
5958
; CHECK: calll __Z20function_that_throwsv
6059
; _Unwind_SjLj_Unregister(&UFC);
6160
; CHECK: leal -64(%ebp), %eax
@@ -99,12 +98,11 @@ try.cont:
9998
; UFC.__jbuf[1] = $RIP
10099
; CHECK-X64: leaq .[[RESUME:LBB[0-9]+_[0-9]+]](%rip), %rax
101100
; CHECK-X64: movq %rax, -256(%rbp)
102-
; UFC.__callsite = 1
103-
; CHECK-X64: movl $1, -304(%rbp)
104101
; _Unwind_SjLj_Register(&UFC);
105102
; CHECK-X64: leaq -312(%rbp), %rcx
106103
; CHECK-X64: callq _Unwind_SjLj_Register
107104
; function_that_throws();
105+
; CHECK-X64: movl $1, -304(%rbp)
108106
; CHECK-X64: callq _Z20function_that_throwsv
109107
; _Unwind_SjLj_Unregister(&UFC);
110108
; CHECK-X64: leaq -312(%rbp), %rcx

0 commit comments

Comments
 (0)