Skip to content

Commit 61a2364

Browse files
authored
[SjLjEHPrepare] Configure call sites correctly (#117656)
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 d9c4e9f commit 61a2364

7 files changed

+65
-20
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: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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 } catch ptr @type_info
32+
resume { ptr, i32 } %lp
33+
34+
try.cont:
35+
; CHECK: store volatile i32 -1
36+
; CHECK-NEXT: call void @may_throw
37+
call void @may_throw()
38+
ret void
39+
}
40+
41+
@type_info = external constant ptr
42+
43+
declare void @may_throw()
44+
declare i32 @__gxx_personality_sj0(...)

llvm/test/CodeGen/VE/Scalar/builtin_sjlj_callsite.ll

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,13 @@ define void @test_callsite() personality ptr @__gxx_personality_sj0 {
5252
; CHECK-NEXT: and %s0, %s0, (32)0
5353
; CHECK-NEXT: lea.sl %s0, .LBB0_3@hi(, %s0)
5454
; CHECK-NEXT: st %s0, -32(, %s9)
55-
; CHECK-NEXT: or %s0, 1, (0)1
56-
; CHECK-NEXT: st %s0, -96(, %s9)
5755
; CHECK-NEXT: lea %s0, _Unwind_SjLj_Register@lo
5856
; CHECK-NEXT: and %s0, %s0, (32)0
5957
; CHECK-NEXT: lea.sl %s12, _Unwind_SjLj_Register@hi(, %s0)
6058
; CHECK-NEXT: lea %s0, -104(, %s9)
6159
; CHECK-NEXT: bsic %s10, (, %s12)
60+
; CHECK-NEXT: or %s0, 1, (0)1
61+
; CHECK-NEXT: st %s0, -96(, %s9)
6262
; CHECK-NEXT: .Ltmp0:
6363
; CHECK-NEXT: lea %s0, f@lo
6464
; CHECK-NEXT: and %s0, %s0, (32)0
@@ -172,14 +172,14 @@ define void @test_callsite() personality ptr @__gxx_personality_sj0 {
172172
; PIC-NEXT: and %s0, %s0, (32)0
173173
; PIC-NEXT: lea.sl %s0, .LBB0_3@gotoff_hi(%s0, %s15)
174174
; PIC-NEXT: st %s0, -32(, %s9)
175-
; PIC-NEXT: or %s0, 1, (0)1
176-
; PIC-NEXT: st %s0, -96(, %s9)
177175
; PIC-NEXT: lea %s12, _Unwind_SjLj_Register@plt_lo(-24)
178176
; PIC-NEXT: and %s12, %s12, (32)0
179177
; PIC-NEXT: sic %s16
180178
; PIC-NEXT: lea.sl %s12, _Unwind_SjLj_Register@plt_hi(%s16, %s12)
181179
; PIC-NEXT: lea %s0, -104(, %s9)
182180
; PIC-NEXT: bsic %s10, (, %s12)
181+
; PIC-NEXT: or %s0, 1, (0)1
182+
; PIC-NEXT: st %s0, -96(, %s9)
183183
; PIC-NEXT: .Ltmp0:
184184
; PIC-NEXT: lea %s12, f@plt_lo(-24)
185185
; PIC-NEXT: and %s12, %s12, (32)0

llvm/test/CodeGen/VE/Scalar/builtin_sjlj_landingpad.ll

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,13 @@ define dso_local i32 @foo(i32 %arg) local_unnamed_addr personality ptr @__gxx_pe
5353
; CHECK-NEXT: and %s0, %s0, (32)0
5454
; CHECK-NEXT: lea.sl %s0, .LBB0_3@hi(, %s0)
5555
; CHECK-NEXT: st %s0, -32(, %s9)
56-
; CHECK-NEXT: or %s0, 1, (0)1
57-
; CHECK-NEXT: st %s0, -96(, %s9)
5856
; CHECK-NEXT: lea %s0, _Unwind_SjLj_Register@lo
5957
; CHECK-NEXT: and %s0, %s0, (32)0
6058
; CHECK-NEXT: lea.sl %s12, _Unwind_SjLj_Register@hi(, %s0)
6159
; CHECK-NEXT: lea %s0, -104(, %s9)
6260
; CHECK-NEXT: bsic %s10, (, %s12)
61+
; CHECK-NEXT: or %s0, 1, (0)1
62+
; CHECK-NEXT: st %s0, -96(, %s9)
6363
; CHECK-NEXT: .Ltmp0:
6464
; CHECK-NEXT: lea %s0, errorbar@lo
6565
; CHECK-NEXT: and %s0, %s0, (32)0
@@ -175,14 +175,14 @@ define dso_local i32 @foo(i32 %arg) local_unnamed_addr personality ptr @__gxx_pe
175175
; PIC-NEXT: and %s0, %s0, (32)0
176176
; PIC-NEXT: lea.sl %s0, .LBB0_3@gotoff_hi(%s0, %s15)
177177
; PIC-NEXT: st %s0, -32(, %s9)
178-
; PIC-NEXT: or %s0, 1, (0)1
179-
; PIC-NEXT: st %s0, -96(, %s9)
180178
; PIC-NEXT: lea %s12, _Unwind_SjLj_Register@plt_lo(-24)
181179
; PIC-NEXT: and %s12, %s12, (32)0
182180
; PIC-NEXT: sic %s16
183181
; PIC-NEXT: lea.sl %s12, _Unwind_SjLj_Register@plt_hi(%s16, %s12)
184182
; PIC-NEXT: lea %s0, -104(, %s9)
185183
; PIC-NEXT: bsic %s10, (, %s12)
184+
; PIC-NEXT: or %s0, 1, (0)1
185+
; PIC-NEXT: st %s0, -96(, %s9)
186186
; PIC-NEXT: .Ltmp0:
187187
; PIC-NEXT: lea %s12, errorbar@plt_lo(-24)
188188
; PIC-NEXT: and %s12, %s12, (32)0

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)