llvm.org GIT mirror llvm / 53fbda2
[CodeGen] Mark setjmp/catchret MBBs address-taken Summary: This ensures that BranchFolding (and similar) won't remove these blocks. Also allow AsmPrinter::EmitBasicBlockStart to process MBBs which are address-taken but do not have BBs that are address-taken, since otherwise its call to getAddrLabelSymbolTableToEmit would fail an assertion on such blocks. I audited the other callers of getAddrLabelSymbolTableToEmit (and getAddrLabelSymbol); they all have BBs known to be address-taken except for the call through getAddrLabelSymbol from WinException::create32bitRef; that call is actually now unreachable, so I've removed it and updated the signature of create32bitRef. This fixes PR25168. Reviewers: majnemer, andrew.w.kaylor, rnk Subscribers: pgavlin, llvm-commits Differential Revision: http://reviews.llvm.org/D13774 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@251113 91177308-0d34-0410-b5e6-96231b3b80d8 Joseph Tremoulet 4 years ago
9 changed file(s) with 103 addition(s) and 19 deletion(s). Raw diff Collapse all Expand all
24792479 if (isVerbose())
24802480 OutStreamer->AddComment("Block address taken");
24812481
2482 for (MCSymbol *Sym : MMI->getAddrLabelSymbolToEmit(BB))
2483 OutStreamer->EmitLabel(Sym);
2482 // MBBs can have their address taken as part of CodeGen without having
2483 // their corresponding BB's address taken in IR
2484 if (BB->hasAddressTaken())
2485 for (MCSymbol *Sym : MMI->getAddrLabelSymbolToEmit(BB))
2486 OutStreamer->EmitLabel(Sym);
24842487 }
24852488
24862489 // Print some verbose block comments.
272272 Asm->OutContext);
273273 }
274274
275 const MCExpr *WinException::create32bitRef(const Value *V) {
276 if (!V)
275 const MCExpr *WinException::create32bitRef(const GlobalValue *GV) {
276 if (!GV)
277277 return MCConstantExpr::create(0, Asm->OutContext);
278 if (const auto *GV = dyn_cast(V))
279 return create32bitRef(Asm->getSymbol(GV));
280 return create32bitRef(MMI->getAddrLabelSymbol(cast(V)));
278 return create32bitRef(Asm->getSymbol(GV));
281279 }
282280
283281 const MCExpr *WinException::getLabelPlusOne(const MCSymbol *Label) {
1717
1818 namespace llvm {
1919 class Function;
20 class GlobalValue;
2021 class MachineFunction;
2122 class MCExpr;
2223 class Value;
6566 StringRef FLinkageName);
6667
6768 const MCExpr *create32bitRef(const MCSymbol *Value);
68 const MCExpr *create32bitRef(const Value *V);
69 const MCExpr *create32bitRef(const GlobalValue *GV);
6970 const MCExpr *getLabelPlusOne(const MCSymbol *Label);
7071 const MCExpr *getOffset(const MCSymbol *OffsetOf, const MCSymbol *OffsetFrom);
7172 const MCExpr *getOffsetPlusOne(const MCSymbol *OffsetOf,
11771177 .addReg(ReturnReg)
11781178 .addMBB(RestoreMBB);
11791179 }
1180 // Record that we've taken the address of RestoreMBB and no longer just
1181 // reference it in a terminator.
1182 RestoreMBB->setHasAddressTaken();
11801183 }
11811184
11821185 if (MBBI != MBB.end())
2147021470 // For v = setjmp(buf), we generate
2147121471 //
2147221472 // thisMBB:
21473 // buf[LabelOffset] = restoreMBB
21473 // buf[LabelOffset] = restoreMBB <-- takes address of restoreMBB
2147421474 // SjLjSetup restoreMBB
2147521475 //
2147621476 // mainMBB:
2149021490 MF->insert(I, mainMBB);
2149121491 MF->insert(I, sinkMBB);
2149221492 MF->push_back(restoreMBB);
21493 restoreMBB->setHasAddressTaken();
2149321494
2149421495 MachineInstrBuilder MIB;
2149521496
0 ; RUN: llc -mtriple=x86_64-pc-windows-msvc < %s | FileCheck %s
1
2 ; Repro cases from PR25168
3
4 ; test @catchret - catchret target is not address-taken until PEI
5 ; splits it into lea/mov followed by ret. Make sure the MBB is
6 ; handled, both by tempting BranchFolding to merge it with %early_out
7 ; and delete it, and by checking that we emit a proper reference
8 ; to it in the LEA
9
10 declare void @ProcessCLRException()
11 declare void @f()
12
13 define void @catchret(i1 %b) personality void ()* @ProcessCLRException {
14 entry:
15 br i1 %b, label %body, label %early_out
16 early_out:
17 ret void
18 body:
19 invoke void @f()
20 to label %exit unwind label %catch.pad
21 catch.pad:
22 %catch = catchpad [i32 33554467]
23 to label %catch.body unwind label %catch.end
24 catch.body:
25 catchret %catch to label %exit
26 catch.end:
27 catchendpad unwind to caller
28 exit:
29 ret void
30 }
31 ; CHECK-LABEL: catchret: # @catchret
32 ; CHECK: [[Exit:^[^ :]+]]: # Block address taken
33 ; CHECK-NEXT: # %exit
34 ; CHECK: # %catch.pad
35 ; CHECK: .seh_endprolog
36 ; CHECK: leaq [[Exit]](%rip), %rax
37 ; CHECK: retq # CATCHRET
38
39
40 ; test @setjmp - similar to @catchret, but the MBB in question
41 ; is the one generated when the setjmp's block is split
42
43 @buf = internal global [5 x i8*] zeroinitializer
44 declare i8* @llvm.frameaddress(i32) nounwind readnone
45 declare i8* @llvm.stacksave() nounwind
46 declare i32 @llvm.eh.sjlj.setjmp(i8*) nounwind
47 declare void @llvm.eh.sjlj.longjmp(i8*) nounwind
48
49 define void @setjmp(i1 %b) nounwind {
50 entry:
51 br i1 %b, label %early_out, label %sj
52 early_out:
53 ret void
54 sj:
55 %fp = call i8* @llvm.frameaddress(i32 0)
56 store i8* %fp, i8** getelementptr inbounds ([5 x i8*], [5 x i8*]* @buf, i64 0, i64 0), align 16
57 %sp = call i8* @llvm.stacksave()
58 store i8* %sp, i8** getelementptr inbounds ([5 x i8*], [5 x i8*]* @buf, i64 0, i64 2), align 16
59 call i32 @llvm.eh.sjlj.setjmp(i8* bitcast ([5 x i8*]* @buf to i8*))
60 ret void
61 }
62 ; CHECK-LABEL: setjmp: # @setjmp
63 ; CHECK: # %sj
64 ; CHECK: leaq [[Label:\..+]](%rip), %[[Reg:.+]]{{$}}
65 ; CHECK-NEXT: movq %[[Reg]], buf
66 ; CHECK: {{^}}[[Label]]: # Block address taken
67 ; CHECK-NEXT: # %sj
112112 ; X64: callq useints
113113 ; X64: movl $1, %ecx
114114 ; X64: callq f
115 ; X64: [[contbb:\.LBB0_[0-9]+]]: # %try.cont
115 ; X64: [[contbb:\.LBB0_[0-9]+]]: # Block address taken
116 ; X64-NEXT: # %try.cont
116117 ; X64: addq $40, %rsp
117118 ; X64: popq %rbp
118119 ; X64: retq
187188 ; X64: callq useints
188189 ; X64: movl $1, %ecx
189190 ; X64: callq f
190 ; X64: [[contbb:\.LBB1_[0-9]+]]: # %try.cont
191 ; X64: [[contbb:\.LBB1_[0-9]+]]: # Block address taken
192 ; X64-NEXT: # %try.cont
191193 ; X64: addq $40, %rsp
192194 ; X64-NOT: popq
193195 ; X64: popq %rsi
248250 ; X64: .seh_endprologue
249251 ; X64: movl $1, %ecx
250252 ; X64: callq f
251 ; X64: [[contbb:\.LBB2_[0-9]+]]: # %try.cont
253 ; X64: [[contbb:\.LBB2_[0-9]+]]: # Block address taken
254 ; X64-NEXT: # %try.cont
252255 ; X64: addq $48, %rsp
253256 ; X64-NOT: popq
254257 ; X64: popq %rbp
3030
3131 ; Check the catchret targets
3232 ; CHECK-LABEL: test1: # @test1
33 ; CHECK: [[Exit:^[^: ]+]]: # %exit
34 ; CHECK: [[OuterRet:^[^: ]+]]: # %outer.ret
33 ; CHECK: [[Exit:^[^: ]+]]: # Block address taken
34 ; CHECK-NEXT: # %exit
35 ; CHECK: [[OuterRet:^[^: ]+]]: # Block address taken
36 ; CHECK-NEXT: # %outer.ret
3537 ; CHECK-NEXT: leaq [[Exit]](%rip), %rax
3638 ; CHECK: retq # CATCHRET
3739 ; CHECK: {{^[^: ]+}}: # %inner.pad
7373 ; X86: [[contbb:LBB0_[0-9]+]]: # %try.cont
7474 ; X86: retl
7575
76 ; X86: [[restorebb1:LBB0_[0-9]+]]: # %invoke.cont.2
76 ; X86: [[restorebb1:LBB0_[0-9]+]]: # Block address taken
77 ; X86-NEXT: # %invoke.cont.2
7778 ; X86: movl -16(%ebp), %esp
7879 ; X86: addl $12, %ebp
7980 ; X86: jmp [[contbb]]
8081
8182 ; FIXME: These should be de-duplicated.
82 ; X86: [[restorebb2:LBB0_[0-9]+]]: # %invoke.cont.3
83 ; X86: [[restorebb2:LBB0_[0-9]+]]: # Block address taken
84 ; X86-NEXT: # %invoke.cont.3
8385 ; X86: movl -16(%ebp), %esp
8486 ; X86: addl $12, %ebp
8587 ; X86: jmp [[contbb]]
139141 ; X64-DAG: leaq -[[local_offs:[0-9]+]](%rbp), %rdx
140142 ; X64-DAG: movl $1, %ecx
141143 ; X64: callq f
142 ; X64: [[contbb:\.LBB0_[0-9]+]]: # %try.cont
144 ; X64: [[contbb:\.LBB0_[0-9]+]]: # Block address taken
145 ; X64-NEXT: # %try.cont
143146 ; X64: addq $48, %rsp
144147 ; X64: popq %rbp
145148 ; X64: retq
252255 ; X86: [[contbb:LBB1_[0-9]+]]: # %try.cont
253256 ; X86: retl
254257
255 ; X86: [[restorebb:LBB1_[0-9]+]]: # %catch.done
258 ; X86: [[restorebb:LBB1_[0-9]+]]: # Block address taken
259 ; X86-NEXT: # %catch.done
256260 ; X86: movl -16(%ebp), %esp
257261 ; X86: addl $12, %ebp
258262 ; X86: jmp [[contbb]]
293297 ; X64: .Ltmp[[before_call:[0-9]+]]:
294298 ; X64: callq f
295299 ; X64: .Ltmp[[after_call:[0-9]+]]:
296 ; X64: [[contbb:\.LBB1_[0-9]+]]: # %try.cont
300 ; X64: [[contbb:\.LBB1_[0-9]+]]: # Block address taken
301 ; X64-NEXT: # %try.cont
297302 ; X64: addq $48, %rsp
298303 ; X64: popq %rbp
299304 ; X64: retq