1 //===- SjLjEHPrepare.cpp - Eliminate Invoke & Unwind instructions ---------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This transformation is designed for use by code generators which use SjLj
10 // based exception handling.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/CodeGen/SjLjEHPrepare.h"
15 #include "llvm/ADT/SetVector.h"
16 #include "llvm/ADT/SmallPtrSet.h"
17 #include "llvm/ADT/SmallVector.h"
18 #include "llvm/ADT/Statistic.h"
19 #include "llvm/CodeGen/Passes.h"
20 #include "llvm/IR/Constants.h"
21 #include "llvm/IR/DataLayout.h"
22 #include "llvm/IR/DerivedTypes.h"
23 #include "llvm/IR/IRBuilder.h"
24 #include "llvm/IR/Instructions.h"
25 #include "llvm/IR/Intrinsics.h"
26 #include "llvm/IR/Module.h"
27 #include "llvm/InitializePasses.h"
28 #include "llvm/Pass.h"
29 #include "llvm/Support/Debug.h"
30 #include "llvm/Support/raw_ostream.h"
31 #include "llvm/Target/TargetMachine.h"
32 #include "llvm/Transforms/Utils/Local.h"
35 #define DEBUG_TYPE "sjlj-eh-prepare"
37 STATISTIC(NumInvokes
, "Number of invokes replaced");
38 STATISTIC(NumSpilled
, "Number of registers live across unwind edges");
41 class SjLjEHPrepareImpl
{
42 IntegerType
*DataTy
= nullptr;
43 Type
*doubleUnderDataTy
= nullptr;
44 Type
*doubleUnderJBufTy
= nullptr;
45 Type
*FunctionContextTy
= nullptr;
46 FunctionCallee RegisterFn
;
47 FunctionCallee UnregisterFn
;
48 Function
*BuiltinSetupDispatchFn
= nullptr;
49 Function
*FrameAddrFn
= nullptr;
50 Function
*StackAddrFn
= nullptr;
51 Function
*StackRestoreFn
= nullptr;
52 Function
*LSDAAddrFn
= nullptr;
53 Function
*CallSiteFn
= nullptr;
54 Function
*FuncCtxFn
= nullptr;
55 AllocaInst
*FuncCtx
= nullptr;
56 const TargetMachine
*TM
= nullptr;
59 explicit SjLjEHPrepareImpl(const TargetMachine
*TM
= nullptr) : TM(TM
) {}
60 bool doInitialization(Module
&M
);
61 bool runOnFunction(Function
&F
);
64 bool setupEntryBlockAndCallSites(Function
&F
);
65 void substituteLPadValues(LandingPadInst
*LPI
, Value
*ExnVal
, Value
*SelVal
);
66 Value
*setupFunctionContext(Function
&F
, ArrayRef
<LandingPadInst
*> LPads
);
67 void lowerIncomingArguments(Function
&F
);
68 void lowerAcrossUnwindEdges(Function
&F
, ArrayRef
<InvokeInst
*> Invokes
);
69 void insertCallSiteStore(Instruction
*I
, int Number
);
72 class SjLjEHPrepare
: public FunctionPass
{
73 SjLjEHPrepareImpl Impl
;
76 static char ID
; // Pass identification, replacement for typeid
77 explicit SjLjEHPrepare(const TargetMachine
*TM
= nullptr)
78 : FunctionPass(ID
), Impl(TM
) {}
79 bool doInitialization(Module
&M
) override
{ return Impl
.doInitialization(M
); }
80 bool runOnFunction(Function
&F
) override
{ return Impl
.runOnFunction(F
); };
82 StringRef
getPassName() const override
{
83 return "SJLJ Exception Handling preparation";
87 } // end anonymous namespace
89 PreservedAnalyses
SjLjEHPreparePass::run(Function
&F
,
90 FunctionAnalysisManager
&FAM
) {
91 SjLjEHPrepareImpl
Impl(TM
);
92 Impl
.doInitialization(*F
.getParent());
93 bool Changed
= Impl
.runOnFunction(F
);
94 return Changed
? PreservedAnalyses::none() : PreservedAnalyses::all();
97 char SjLjEHPrepare::ID
= 0;
98 INITIALIZE_PASS(SjLjEHPrepare
, DEBUG_TYPE
, "Prepare SjLj exceptions",
101 // Public Interface To the SjLjEHPrepare pass.
102 FunctionPass
*llvm::createSjLjEHPreparePass(const TargetMachine
*TM
) {
103 return new SjLjEHPrepare(TM
);
106 // doInitialization - Set up decalarations and types needed to process
108 bool SjLjEHPrepareImpl::doInitialization(Module
&M
) {
109 // Build the function context structure.
110 // builtin_setjmp uses a five word jbuf
111 Type
*VoidPtrTy
= PointerType::getUnqual(M
.getContext());
113 TM
? TM
->getSjLjDataSize() : TargetMachine::DefaultSjLjDataSize
;
114 DataTy
= Type::getIntNTy(M
.getContext(), DataBits
);
115 doubleUnderDataTy
= ArrayType::get(DataTy
, 4);
116 doubleUnderJBufTy
= ArrayType::get(VoidPtrTy
, 5);
117 FunctionContextTy
= StructType::get(VoidPtrTy
, // __prev
119 doubleUnderDataTy
, // __data
120 VoidPtrTy
, // __personality
122 doubleUnderJBufTy
// __jbuf
128 /// insertCallSiteStore - Insert a store of the call-site value to the
130 void SjLjEHPrepareImpl::insertCallSiteStore(Instruction
*I
, int Number
) {
131 IRBuilder
<> Builder(I
);
133 // Get a reference to the call_site field.
134 Type
*Int32Ty
= Type::getInt32Ty(I
->getContext());
135 Value
*Zero
= ConstantInt::get(Int32Ty
, 0);
136 Value
*One
= ConstantInt::get(Int32Ty
, 1);
137 Value
*Idxs
[2] = { Zero
, One
};
139 Builder
.CreateGEP(FunctionContextTy
, FuncCtx
, Idxs
, "call_site");
141 // Insert a store of the call-site number
142 ConstantInt
*CallSiteNoC
= ConstantInt::get(DataTy
, Number
);
143 Builder
.CreateStore(CallSiteNoC
, CallSite
, true /*volatile*/);
146 /// MarkBlocksLiveIn - Insert BB and all of its predecessors into LiveBBs until
147 /// we reach blocks we've already seen.
148 static void MarkBlocksLiveIn(BasicBlock
*BB
,
149 SmallPtrSetImpl
<BasicBlock
*> &LiveBBs
) {
150 if (!LiveBBs
.insert(BB
).second
)
151 return; // already been here.
153 for (BasicBlock
*B
: inverse_depth_first(BB
))
157 /// substituteLPadValues - Substitute the values returned by the landingpad
158 /// instruction with those returned by the personality function.
159 void SjLjEHPrepareImpl::substituteLPadValues(LandingPadInst
*LPI
, Value
*ExnVal
,
161 SmallVector
<Value
*, 8> UseWorkList(LPI
->users());
162 while (!UseWorkList
.empty()) {
163 Value
*Val
= UseWorkList
.pop_back_val();
164 auto *EVI
= dyn_cast
<ExtractValueInst
>(Val
);
167 if (EVI
->getNumIndices() != 1)
169 if (*EVI
->idx_begin() == 0)
170 EVI
->replaceAllUsesWith(ExnVal
);
171 else if (*EVI
->idx_begin() == 1)
172 EVI
->replaceAllUsesWith(SelVal
);
173 if (EVI
->use_empty())
174 EVI
->eraseFromParent();
177 if (LPI
->use_empty())
180 // There are still some uses of LPI. Construct an aggregate with the exception
181 // values and replace the LPI with that aggregate.
182 Type
*LPadType
= LPI
->getType();
183 Value
*LPadVal
= PoisonValue::get(LPadType
);
184 auto *SelI
= cast
<Instruction
>(SelVal
);
185 IRBuilder
<> Builder(SelI
->getParent(), std::next(SelI
->getIterator()));
186 LPadVal
= Builder
.CreateInsertValue(LPadVal
, ExnVal
, 0, "lpad.val");
187 LPadVal
= Builder
.CreateInsertValue(LPadVal
, SelVal
, 1, "lpad.val");
189 LPI
->replaceAllUsesWith(LPadVal
);
192 /// setupFunctionContext - Allocate the function context on the stack and fill
193 /// it with all of the data that we know at this point.
195 SjLjEHPrepareImpl::setupFunctionContext(Function
&F
,
196 ArrayRef
<LandingPadInst
*> LPads
) {
197 BasicBlock
*EntryBB
= &F
.front();
199 // Create an alloca for the incoming jump buffer ptr and the new jump buffer
200 // that needs to be restored on all exits from the function. This is an alloca
201 // because the value needs to be added to the global context list.
202 auto &DL
= F
.getDataLayout();
203 const Align Alignment
= DL
.getPrefTypeAlign(FunctionContextTy
);
204 FuncCtx
= new AllocaInst(FunctionContextTy
, DL
.getAllocaAddrSpace(), nullptr,
205 Alignment
, "fn_context", EntryBB
->begin());
207 // Fill in the function context structure.
208 for (LandingPadInst
*LPI
: LPads
) {
209 IRBuilder
<> Builder(LPI
->getParent(),
210 LPI
->getParent()->getFirstInsertionPt());
212 // Reference the __data field.
214 Builder
.CreateConstGEP2_32(FunctionContextTy
, FuncCtx
, 0, 2, "__data");
216 // The exception values come back in context->__data[0].
217 Value
*ExceptionAddr
= Builder
.CreateConstGEP2_32(doubleUnderDataTy
, FCData
,
218 0, 0, "exception_gep");
219 Value
*ExnVal
= Builder
.CreateLoad(DataTy
, ExceptionAddr
, true, "exn_val");
220 ExnVal
= Builder
.CreateIntToPtr(ExnVal
, Builder
.getPtrTy());
222 Value
*SelectorAddr
= Builder
.CreateConstGEP2_32(doubleUnderDataTy
, FCData
,
223 0, 1, "exn_selector_gep");
225 Builder
.CreateLoad(DataTy
, SelectorAddr
, true, "exn_selector_val");
227 // SelVal must be Int32Ty, so trunc it
228 SelVal
= Builder
.CreateTrunc(SelVal
, Type::getInt32Ty(F
.getContext()));
230 substituteLPadValues(LPI
, ExnVal
, SelVal
);
233 // Personality function
234 IRBuilder
<> Builder(EntryBB
->getTerminator());
235 Value
*PersonalityFn
= F
.getPersonalityFn();
236 Value
*PersonalityFieldPtr
= Builder
.CreateConstGEP2_32(
237 FunctionContextTy
, FuncCtx
, 0, 3, "pers_fn_gep");
238 Builder
.CreateStore(PersonalityFn
, PersonalityFieldPtr
, /*isVolatile=*/true);
241 Value
*LSDA
= Builder
.CreateCall(LSDAAddrFn
, {}, "lsda_addr");
242 Value
*LSDAFieldPtr
=
243 Builder
.CreateConstGEP2_32(FunctionContextTy
, FuncCtx
, 0, 4, "lsda_gep");
244 Builder
.CreateStore(LSDA
, LSDAFieldPtr
, /*isVolatile=*/true);
249 /// lowerIncomingArguments - To avoid having to handle incoming arguments
250 /// specially, we lower each arg to a copy instruction in the entry block. This
251 /// ensures that the argument value itself cannot be live out of the entry
253 void SjLjEHPrepareImpl::lowerIncomingArguments(Function
&F
) {
254 BasicBlock::iterator AfterAllocaInsPt
= F
.begin()->begin();
255 while (isa
<AllocaInst
>(AfterAllocaInsPt
) &&
256 cast
<AllocaInst
>(AfterAllocaInsPt
)->isStaticAlloca())
258 assert(AfterAllocaInsPt
!= F
.front().end());
260 for (auto &AI
: F
.args()) {
261 // Swift error really is a register that we model as memory -- instruction
262 // selection will perform mem-to-reg for us and spill/reload appropriately
263 // around calls that clobber it. There is no need to spill this
264 // value to the stack and doing so would not be allowed.
265 if (AI
.isSwiftError())
268 Type
*Ty
= AI
.getType();
270 // Use 'select i8 true, %arg, undef' to simulate a 'no-op' instruction.
271 Value
*TrueValue
= ConstantInt::getTrue(F
.getContext());
272 Value
*UndefValue
= UndefValue::get(Ty
);
273 Instruction
*SI
= SelectInst::Create(
274 TrueValue
, &AI
, UndefValue
, AI
.getName() + ".tmp", AfterAllocaInsPt
);
275 AI
.replaceAllUsesWith(SI
);
277 // Reset the operand, because it was clobbered by the RAUW above.
278 SI
->setOperand(1, &AI
);
282 /// lowerAcrossUnwindEdges - Find all variables which are alive across an unwind
283 /// edge and spill them.
284 void SjLjEHPrepareImpl::lowerAcrossUnwindEdges(Function
&F
,
285 ArrayRef
<InvokeInst
*> Invokes
) {
286 // Finally, scan the code looking for instructions with bad live ranges.
287 for (BasicBlock
&BB
: F
) {
288 for (Instruction
&Inst
: BB
) {
289 // Ignore obvious cases we don't have to handle. In particular, most
290 // instructions either have no uses or only have a single use inside the
291 // current block. Ignore them quickly.
292 if (Inst
.use_empty())
294 if (Inst
.hasOneUse() &&
295 cast
<Instruction
>(Inst
.user_back())->getParent() == &BB
&&
296 !isa
<PHINode
>(Inst
.user_back()))
299 // If this is an alloca in the entry block, it's not a real register
301 if (auto *AI
= dyn_cast
<AllocaInst
>(&Inst
))
302 if (AI
->isStaticAlloca())
305 // Avoid iterator invalidation by copying users to a temporary vector.
306 SmallVector
<Instruction
*, 16> Users
;
307 for (User
*U
: Inst
.users()) {
308 Instruction
*UI
= cast
<Instruction
>(U
);
309 if (UI
->getParent() != &BB
|| isa
<PHINode
>(UI
))
313 // Find all of the blocks that this value is live in.
314 SmallPtrSet
<BasicBlock
*, 32> LiveBBs
;
316 while (!Users
.empty()) {
317 Instruction
*U
= Users
.pop_back_val();
319 if (!isa
<PHINode
>(U
)) {
320 MarkBlocksLiveIn(U
->getParent(), LiveBBs
);
322 // Uses for a PHI node occur in their predecessor block.
323 PHINode
*PN
= cast
<PHINode
>(U
);
324 for (unsigned i
= 0, e
= PN
->getNumIncomingValues(); i
!= e
; ++i
)
325 if (PN
->getIncomingValue(i
) == &Inst
)
326 MarkBlocksLiveIn(PN
->getIncomingBlock(i
), LiveBBs
);
330 // Now that we know all of the blocks that this thing is live in, see if
331 // it includes any of the unwind locations.
332 bool NeedsSpill
= false;
333 for (InvokeInst
*Invoke
: Invokes
) {
334 BasicBlock
*UnwindBlock
= Invoke
->getUnwindDest();
335 if (UnwindBlock
!= &BB
&& LiveBBs
.count(UnwindBlock
)) {
336 LLVM_DEBUG(dbgs() << "SJLJ Spill: " << Inst
<< " around "
337 << UnwindBlock
->getName() << "\n");
343 // If we decided we need a spill, do it.
344 // FIXME: Spilling this way is overkill, as it forces all uses of
345 // the value to be reloaded from the stack slot, even those that aren't
346 // in the unwind blocks. We should be more selective.
348 DemoteRegToStack(Inst
, true);
354 // Go through the landing pads and remove any PHIs there.
355 for (InvokeInst
*Invoke
: Invokes
) {
356 BasicBlock
*UnwindBlock
= Invoke
->getUnwindDest();
357 LandingPadInst
*LPI
= UnwindBlock
->getLandingPadInst();
359 // Place PHIs into a set to avoid invalidating the iterator.
360 SmallPtrSet
<PHINode
*, 8> PHIsToDemote
;
361 for (BasicBlock::iterator PN
= UnwindBlock
->begin(); isa
<PHINode
>(PN
); ++PN
)
362 PHIsToDemote
.insert(cast
<PHINode
>(PN
));
363 if (PHIsToDemote
.empty())
366 // Demote the PHIs to the stack.
367 for (PHINode
*PN
: PHIsToDemote
)
368 DemotePHIToStack(PN
);
370 // Move the landingpad instruction back to the top of the landing pad block.
371 LPI
->moveBefore(&UnwindBlock
->front());
375 /// setupEntryBlockAndCallSites - Setup the entry block by creating and filling
376 /// the function context and marking the call sites with the appropriate
377 /// values. These values are used by the DWARF EH emitter.
378 bool SjLjEHPrepareImpl::setupEntryBlockAndCallSites(Function
&F
) {
379 SmallVector
<ReturnInst
*, 16> Returns
;
380 SmallVector
<InvokeInst
*, 16> Invokes
;
381 SmallSetVector
<LandingPadInst
*, 16> LPads
;
383 // Look through the terminators of the basic blocks to find invokes.
384 for (BasicBlock
&BB
: F
)
385 if (auto *II
= dyn_cast
<InvokeInst
>(BB
.getTerminator())) {
386 if (Function
*Callee
= II
->getCalledFunction())
387 if (Callee
->getIntrinsicID() == Intrinsic::donothing
) {
388 // Remove the NOP invoke.
389 BranchInst::Create(II
->getNormalDest(), II
->getIterator());
390 II
->eraseFromParent();
394 Invokes
.push_back(II
);
395 LPads
.insert(II
->getUnwindDest()->getLandingPadInst());
396 } else if (auto *RI
= dyn_cast
<ReturnInst
>(BB
.getTerminator())) {
397 Returns
.push_back(RI
);
403 NumInvokes
+= Invokes
.size();
405 lowerIncomingArguments(F
);
406 lowerAcrossUnwindEdges(F
, Invokes
);
409 setupFunctionContext(F
, ArrayRef(LPads
.begin(), LPads
.end()));
410 BasicBlock
*EntryBB
= &F
.front();
411 IRBuilder
<> Builder(EntryBB
->getTerminator());
413 // Get a reference to the jump buffer.
415 Builder
.CreateConstGEP2_32(FunctionContextTy
, FuncCtx
, 0, 5, "jbuf_gep");
417 // Save the frame pointer.
418 Value
*FramePtr
= Builder
.CreateConstGEP2_32(doubleUnderJBufTy
, JBufPtr
, 0, 0,
421 Value
*Val
= Builder
.CreateCall(FrameAddrFn
, Builder
.getInt32(0), "fp");
422 Builder
.CreateStore(Val
, FramePtr
, /*isVolatile=*/true);
424 // Save the stack pointer.
425 Value
*StackPtr
= Builder
.CreateConstGEP2_32(doubleUnderJBufTy
, JBufPtr
, 0, 2,
428 Val
= Builder
.CreateCall(StackAddrFn
, {}, "sp");
429 Builder
.CreateStore(Val
, StackPtr
, /*isVolatile=*/true);
431 // Call the setup_dispatch intrinsic. It fills in the rest of the jmpbuf.
432 Builder
.CreateCall(BuiltinSetupDispatchFn
, {});
434 // Store a pointer to the function context so that the back-end will know
435 // where to look for it.
436 Builder
.CreateCall(FuncCtxFn
, FuncCtx
);
438 // At this point, we are all set up, update the invoke instructions to mark
439 // their call_site values.
440 for (unsigned I
= 0, E
= Invokes
.size(); I
!= E
; ++I
) {
441 insertCallSiteStore(Invokes
[I
], I
+ 1);
443 ConstantInt
*CallSiteNum
=
444 ConstantInt::get(Type::getInt32Ty(F
.getContext()), I
+ 1);
446 // Record the call site value for the back end so it stays associated with
448 CallInst::Create(CallSiteFn
, CallSiteNum
, "", Invokes
[I
]->getIterator());
451 // Mark call instructions that aren't nounwind as no-action (call_site ==
452 // -1). Skip the entry block, as prior to then, no function context has been
453 // created for this function and any unexpected exceptions thrown will go
454 // directly to the caller's context, which is what we want anyway, so no need
455 // to do anything here.
456 for (BasicBlock
&BB
: F
) {
457 if (&BB
== &F
.front())
459 for (Instruction
&I
: BB
)
461 insertCallSiteStore(&I
, -1);
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();
469 // Following any allocas not in the entry block, update the saved SP in the
470 // jmpbuf to the new value.
471 for (BasicBlock
&BB
: F
) {
472 if (&BB
== &F
.front())
474 for (Instruction
&I
: BB
) {
475 if (auto *CI
= dyn_cast
<CallInst
>(&I
)) {
476 if (CI
->getCalledFunction() != StackRestoreFn
)
478 } else if (!isa
<AllocaInst
>(&I
)) {
481 Instruction
*StackAddr
= CallInst::Create(StackAddrFn
, "sp");
482 StackAddr
->insertAfter(&I
);
483 new StoreInst(StackAddr
, StackPtr
, true,
484 std::next(StackAddr
->getIterator()));
488 // Finally, for any returns from this function, if this function contains an
489 // invoke, add a call to unregister the function context.
490 for (ReturnInst
*Return
: Returns
) {
491 Instruction
*InsertPoint
= Return
;
492 if (CallInst
*CI
= Return
->getParent()->getTerminatingMustTailCall())
494 CallInst::Create(UnregisterFn
, FuncCtx
, "", InsertPoint
->getIterator());
500 bool SjLjEHPrepareImpl::runOnFunction(Function
&F
) {
501 Module
&M
= *F
.getParent();
502 RegisterFn
= M
.getOrInsertFunction(
503 "_Unwind_SjLj_Register", Type::getVoidTy(M
.getContext()),
504 PointerType::getUnqual(FunctionContextTy
));
505 UnregisterFn
= M
.getOrInsertFunction(
506 "_Unwind_SjLj_Unregister", Type::getVoidTy(M
.getContext()),
507 PointerType::getUnqual(FunctionContextTy
));
509 PointerType
*AllocaPtrTy
= M
.getDataLayout().getAllocaPtrType(M
.getContext());
512 Intrinsic::getDeclaration(&M
, Intrinsic::frameaddress
, {AllocaPtrTy
});
514 Intrinsic::getDeclaration(&M
, Intrinsic::stacksave
, {AllocaPtrTy
});
516 Intrinsic::getDeclaration(&M
, Intrinsic::stackrestore
, {AllocaPtrTy
});
517 BuiltinSetupDispatchFn
=
518 Intrinsic::getDeclaration(&M
, Intrinsic::eh_sjlj_setup_dispatch
);
519 LSDAAddrFn
= Intrinsic::getDeclaration(&M
, Intrinsic::eh_sjlj_lsda
);
520 CallSiteFn
= Intrinsic::getDeclaration(&M
, Intrinsic::eh_sjlj_callsite
);
521 FuncCtxFn
= Intrinsic::getDeclaration(&M
, Intrinsic::eh_sjlj_functioncontext
);
523 bool Res
= setupEntryBlockAndCallSites(F
);