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/ADT/SetVector.h"
15 #include "llvm/ADT/SmallPtrSet.h"
16 #include "llvm/ADT/SmallVector.h"
17 #include "llvm/ADT/Statistic.h"
18 #include "llvm/CodeGen/Passes.h"
19 #include "llvm/IR/Constants.h"
20 #include "llvm/IR/DataLayout.h"
21 #include "llvm/IR/DerivedTypes.h"
22 #include "llvm/IR/IRBuilder.h"
23 #include "llvm/IR/Instructions.h"
24 #include "llvm/IR/Intrinsics.h"
25 #include "llvm/IR/Module.h"
26 #include "llvm/InitializePasses.h"
27 #include "llvm/Pass.h"
28 #include "llvm/Support/Debug.h"
29 #include "llvm/Support/raw_ostream.h"
30 #include "llvm/Target/TargetMachine.h"
31 #include "llvm/Transforms/Utils/Local.h"
34 #define DEBUG_TYPE "sjljehprepare"
36 STATISTIC(NumInvokes
, "Number of invokes replaced");
37 STATISTIC(NumSpilled
, "Number of registers live across unwind edges");
40 class SjLjEHPrepare
: public FunctionPass
{
42 Type
*doubleUnderDataTy
;
43 Type
*doubleUnderJBufTy
;
44 Type
*FunctionContextTy
;
45 FunctionCallee RegisterFn
;
46 FunctionCallee UnregisterFn
;
47 Function
*BuiltinSetupDispatchFn
;
48 Function
*FrameAddrFn
;
49 Function
*StackAddrFn
;
50 Function
*StackRestoreFn
;
55 const TargetMachine
*TM
;
58 static char ID
; // Pass identification, replacement for typeid
59 explicit SjLjEHPrepare(const TargetMachine
*TM
= nullptr)
60 : FunctionPass(ID
), TM(TM
) {}
61 bool doInitialization(Module
&M
) override
;
62 bool runOnFunction(Function
&F
) override
;
64 void getAnalysisUsage(AnalysisUsage
&AU
) const override
{}
65 StringRef
getPassName() const override
{
66 return "SJLJ Exception Handling preparation";
70 bool setupEntryBlockAndCallSites(Function
&F
);
71 void substituteLPadValues(LandingPadInst
*LPI
, Value
*ExnVal
, Value
*SelVal
);
72 Value
*setupFunctionContext(Function
&F
, ArrayRef
<LandingPadInst
*> LPads
);
73 void lowerIncomingArguments(Function
&F
);
74 void lowerAcrossUnwindEdges(Function
&F
, ArrayRef
<InvokeInst
*> Invokes
);
75 void insertCallSiteStore(Instruction
*I
, int Number
);
77 } // end anonymous namespace
79 char SjLjEHPrepare::ID
= 0;
80 INITIALIZE_PASS(SjLjEHPrepare
, DEBUG_TYPE
, "Prepare SjLj exceptions",
83 // Public Interface To the SjLjEHPrepare pass.
84 FunctionPass
*llvm::createSjLjEHPreparePass(const TargetMachine
*TM
) {
85 return new SjLjEHPrepare(TM
);
88 // doInitialization - Set up decalarations and types needed to process
90 bool SjLjEHPrepare::doInitialization(Module
&M
) {
91 // Build the function context structure.
92 // builtin_setjmp uses a five word jbuf
93 Type
*VoidPtrTy
= Type::getInt8PtrTy(M
.getContext());
95 TM
? TM
->getSjLjDataSize() : TargetMachine::DefaultSjLjDataSize
;
96 DataTy
= Type::getIntNTy(M
.getContext(), DataBits
);
97 doubleUnderDataTy
= ArrayType::get(DataTy
, 4);
98 doubleUnderJBufTy
= ArrayType::get(VoidPtrTy
, 5);
99 FunctionContextTy
= StructType::get(VoidPtrTy
, // __prev
101 doubleUnderDataTy
, // __data
102 VoidPtrTy
, // __personality
104 doubleUnderJBufTy
// __jbuf
110 /// insertCallSiteStore - Insert a store of the call-site value to the
112 void SjLjEHPrepare::insertCallSiteStore(Instruction
*I
, int Number
) {
113 IRBuilder
<> Builder(I
);
115 // Get a reference to the call_site field.
116 Type
*Int32Ty
= Type::getInt32Ty(I
->getContext());
117 Value
*Zero
= ConstantInt::get(Int32Ty
, 0);
118 Value
*One
= ConstantInt::get(Int32Ty
, 1);
119 Value
*Idxs
[2] = { Zero
, One
};
121 Builder
.CreateGEP(FunctionContextTy
, FuncCtx
, Idxs
, "call_site");
123 // Insert a store of the call-site number
124 ConstantInt
*CallSiteNoC
= ConstantInt::get(DataTy
, Number
);
125 Builder
.CreateStore(CallSiteNoC
, CallSite
, true /*volatile*/);
128 /// MarkBlocksLiveIn - Insert BB and all of its predecessors into LiveBBs until
129 /// we reach blocks we've already seen.
130 static void MarkBlocksLiveIn(BasicBlock
*BB
,
131 SmallPtrSetImpl
<BasicBlock
*> &LiveBBs
) {
132 if (!LiveBBs
.insert(BB
).second
)
133 return; // already been here.
135 df_iterator_default_set
<BasicBlock
*> Visited
;
137 for (BasicBlock
*B
: inverse_depth_first_ext(BB
, Visited
))
141 /// substituteLPadValues - Substitute the values returned by the landingpad
142 /// instruction with those returned by the personality function.
143 void SjLjEHPrepare::substituteLPadValues(LandingPadInst
*LPI
, Value
*ExnVal
,
145 SmallVector
<Value
*, 8> UseWorkList(LPI
->users());
146 while (!UseWorkList
.empty()) {
147 Value
*Val
= UseWorkList
.pop_back_val();
148 auto *EVI
= dyn_cast
<ExtractValueInst
>(Val
);
151 if (EVI
->getNumIndices() != 1)
153 if (*EVI
->idx_begin() == 0)
154 EVI
->replaceAllUsesWith(ExnVal
);
155 else if (*EVI
->idx_begin() == 1)
156 EVI
->replaceAllUsesWith(SelVal
);
157 if (EVI
->use_empty())
158 EVI
->eraseFromParent();
161 if (LPI
->use_empty())
164 // There are still some uses of LPI. Construct an aggregate with the exception
165 // values and replace the LPI with that aggregate.
166 Type
*LPadType
= LPI
->getType();
167 Value
*LPadVal
= UndefValue::get(LPadType
);
168 auto *SelI
= cast
<Instruction
>(SelVal
);
169 IRBuilder
<> Builder(SelI
->getParent(), std::next(SelI
->getIterator()));
170 LPadVal
= Builder
.CreateInsertValue(LPadVal
, ExnVal
, 0, "lpad.val");
171 LPadVal
= Builder
.CreateInsertValue(LPadVal
, SelVal
, 1, "lpad.val");
173 LPI
->replaceAllUsesWith(LPadVal
);
176 /// setupFunctionContext - Allocate the function context on the stack and fill
177 /// it with all of the data that we know at this point.
178 Value
*SjLjEHPrepare::setupFunctionContext(Function
&F
,
179 ArrayRef
<LandingPadInst
*> LPads
) {
180 BasicBlock
*EntryBB
= &F
.front();
182 // Create an alloca for the incoming jump buffer ptr and the new jump buffer
183 // that needs to be restored on all exits from the function. This is an alloca
184 // because the value needs to be added to the global context list.
185 auto &DL
= F
.getParent()->getDataLayout();
186 const Align
Alignment(DL
.getPrefTypeAlignment(FunctionContextTy
));
187 FuncCtx
= new AllocaInst(FunctionContextTy
, DL
.getAllocaAddrSpace(), nullptr,
188 Alignment
, "fn_context", &EntryBB
->front());
190 // Fill in the function context structure.
191 for (LandingPadInst
*LPI
: LPads
) {
192 IRBuilder
<> Builder(LPI
->getParent(),
193 LPI
->getParent()->getFirstInsertionPt());
195 // Reference the __data field.
197 Builder
.CreateConstGEP2_32(FunctionContextTy
, FuncCtx
, 0, 2, "__data");
199 // The exception values come back in context->__data[0].
200 Value
*ExceptionAddr
= Builder
.CreateConstGEP2_32(doubleUnderDataTy
, FCData
,
201 0, 0, "exception_gep");
202 Value
*ExnVal
= Builder
.CreateLoad(DataTy
, ExceptionAddr
, true, "exn_val");
203 ExnVal
= Builder
.CreateIntToPtr(ExnVal
, Builder
.getInt8PtrTy());
205 Value
*SelectorAddr
= Builder
.CreateConstGEP2_32(doubleUnderDataTy
, FCData
,
206 0, 1, "exn_selector_gep");
208 Builder
.CreateLoad(DataTy
, SelectorAddr
, true, "exn_selector_val");
210 // SelVal must be Int32Ty, so trunc it
211 SelVal
= Builder
.CreateTrunc(SelVal
, Type::getInt32Ty(F
.getContext()));
213 substituteLPadValues(LPI
, ExnVal
, SelVal
);
216 // Personality function
217 IRBuilder
<> Builder(EntryBB
->getTerminator());
218 Value
*PersonalityFn
= F
.getPersonalityFn();
219 Value
*PersonalityFieldPtr
= Builder
.CreateConstGEP2_32(
220 FunctionContextTy
, FuncCtx
, 0, 3, "pers_fn_gep");
222 Builder
.CreateBitCast(PersonalityFn
, Builder
.getInt8PtrTy()),
223 PersonalityFieldPtr
, /*isVolatile=*/true);
226 Value
*LSDA
= Builder
.CreateCall(LSDAAddrFn
, {}, "lsda_addr");
227 Value
*LSDAFieldPtr
=
228 Builder
.CreateConstGEP2_32(FunctionContextTy
, FuncCtx
, 0, 4, "lsda_gep");
229 Builder
.CreateStore(LSDA
, LSDAFieldPtr
, /*isVolatile=*/true);
234 /// lowerIncomingArguments - To avoid having to handle incoming arguments
235 /// specially, we lower each arg to a copy instruction in the entry block. This
236 /// ensures that the argument value itself cannot be live out of the entry
238 void SjLjEHPrepare::lowerIncomingArguments(Function
&F
) {
239 BasicBlock::iterator AfterAllocaInsPt
= F
.begin()->begin();
240 while (isa
<AllocaInst
>(AfterAllocaInsPt
) &&
241 cast
<AllocaInst
>(AfterAllocaInsPt
)->isStaticAlloca())
243 assert(AfterAllocaInsPt
!= F
.front().end());
245 for (auto &AI
: F
.args()) {
246 // Swift error really is a register that we model as memory -- instruction
247 // selection will perform mem-to-reg for us and spill/reload appropriately
248 // around calls that clobber it. There is no need to spill this
249 // value to the stack and doing so would not be allowed.
250 if (AI
.isSwiftError())
253 Type
*Ty
= AI
.getType();
255 // Use 'select i8 true, %arg, undef' to simulate a 'no-op' instruction.
256 Value
*TrueValue
= ConstantInt::getTrue(F
.getContext());
257 Value
*UndefValue
= UndefValue::get(Ty
);
258 Instruction
*SI
= SelectInst::Create(
259 TrueValue
, &AI
, UndefValue
, AI
.getName() + ".tmp", &*AfterAllocaInsPt
);
260 AI
.replaceAllUsesWith(SI
);
262 // Reset the operand, because it was clobbered by the RAUW above.
263 SI
->setOperand(1, &AI
);
267 /// lowerAcrossUnwindEdges - Find all variables which are alive across an unwind
268 /// edge and spill them.
269 void SjLjEHPrepare::lowerAcrossUnwindEdges(Function
&F
,
270 ArrayRef
<InvokeInst
*> Invokes
) {
271 // Finally, scan the code looking for instructions with bad live ranges.
272 for (BasicBlock
&BB
: F
) {
273 for (Instruction
&Inst
: BB
) {
274 // Ignore obvious cases we don't have to handle. In particular, most
275 // instructions either have no uses or only have a single use inside the
276 // current block. Ignore them quickly.
277 if (Inst
.use_empty())
279 if (Inst
.hasOneUse() &&
280 cast
<Instruction
>(Inst
.user_back())->getParent() == &BB
&&
281 !isa
<PHINode
>(Inst
.user_back()))
284 // If this is an alloca in the entry block, it's not a real register
286 if (auto *AI
= dyn_cast
<AllocaInst
>(&Inst
))
287 if (AI
->isStaticAlloca())
290 // Avoid iterator invalidation by copying users to a temporary vector.
291 SmallVector
<Instruction
*, 16> Users
;
292 for (User
*U
: Inst
.users()) {
293 Instruction
*UI
= cast
<Instruction
>(U
);
294 if (UI
->getParent() != &BB
|| isa
<PHINode
>(UI
))
298 // Find all of the blocks that this value is live in.
299 SmallPtrSet
<BasicBlock
*, 32> LiveBBs
;
301 while (!Users
.empty()) {
302 Instruction
*U
= Users
.pop_back_val();
304 if (!isa
<PHINode
>(U
)) {
305 MarkBlocksLiveIn(U
->getParent(), LiveBBs
);
307 // Uses for a PHI node occur in their predecessor block.
308 PHINode
*PN
= cast
<PHINode
>(U
);
309 for (unsigned i
= 0, e
= PN
->getNumIncomingValues(); i
!= e
; ++i
)
310 if (PN
->getIncomingValue(i
) == &Inst
)
311 MarkBlocksLiveIn(PN
->getIncomingBlock(i
), LiveBBs
);
315 // Now that we know all of the blocks that this thing is live in, see if
316 // it includes any of the unwind locations.
317 bool NeedsSpill
= false;
318 for (InvokeInst
*Invoke
: Invokes
) {
319 BasicBlock
*UnwindBlock
= Invoke
->getUnwindDest();
320 if (UnwindBlock
!= &BB
&& LiveBBs
.count(UnwindBlock
)) {
321 LLVM_DEBUG(dbgs() << "SJLJ Spill: " << Inst
<< " around "
322 << UnwindBlock
->getName() << "\n");
328 // If we decided we need a spill, do it.
329 // FIXME: Spilling this way is overkill, as it forces all uses of
330 // the value to be reloaded from the stack slot, even those that aren't
331 // in the unwind blocks. We should be more selective.
333 DemoteRegToStack(Inst
, true);
339 // Go through the landing pads and remove any PHIs there.
340 for (InvokeInst
*Invoke
: Invokes
) {
341 BasicBlock
*UnwindBlock
= Invoke
->getUnwindDest();
342 LandingPadInst
*LPI
= UnwindBlock
->getLandingPadInst();
344 // Place PHIs into a set to avoid invalidating the iterator.
345 SmallPtrSet
<PHINode
*, 8> PHIsToDemote
;
346 for (BasicBlock::iterator PN
= UnwindBlock
->begin(); isa
<PHINode
>(PN
); ++PN
)
347 PHIsToDemote
.insert(cast
<PHINode
>(PN
));
348 if (PHIsToDemote
.empty())
351 // Demote the PHIs to the stack.
352 for (PHINode
*PN
: PHIsToDemote
)
353 DemotePHIToStack(PN
);
355 // Move the landingpad instruction back to the top of the landing pad block.
356 LPI
->moveBefore(&UnwindBlock
->front());
360 /// setupEntryBlockAndCallSites - Setup the entry block by creating and filling
361 /// the function context and marking the call sites with the appropriate
362 /// values. These values are used by the DWARF EH emitter.
363 bool SjLjEHPrepare::setupEntryBlockAndCallSites(Function
&F
) {
364 SmallVector
<ReturnInst
*, 16> Returns
;
365 SmallVector
<InvokeInst
*, 16> Invokes
;
366 SmallSetVector
<LandingPadInst
*, 16> LPads
;
368 // Look through the terminators of the basic blocks to find invokes.
369 for (BasicBlock
&BB
: F
)
370 if (auto *II
= dyn_cast
<InvokeInst
>(BB
.getTerminator())) {
371 if (Function
*Callee
= II
->getCalledFunction())
372 if (Callee
->getIntrinsicID() == Intrinsic::donothing
) {
373 // Remove the NOP invoke.
374 BranchInst::Create(II
->getNormalDest(), II
);
375 II
->eraseFromParent();
379 Invokes
.push_back(II
);
380 LPads
.insert(II
->getUnwindDest()->getLandingPadInst());
381 } else if (auto *RI
= dyn_cast
<ReturnInst
>(BB
.getTerminator())) {
382 Returns
.push_back(RI
);
388 NumInvokes
+= Invokes
.size();
390 lowerIncomingArguments(F
);
391 lowerAcrossUnwindEdges(F
, Invokes
);
394 setupFunctionContext(F
, makeArrayRef(LPads
.begin(), LPads
.end()));
395 BasicBlock
*EntryBB
= &F
.front();
396 IRBuilder
<> Builder(EntryBB
->getTerminator());
398 // Get a reference to the jump buffer.
400 Builder
.CreateConstGEP2_32(FunctionContextTy
, FuncCtx
, 0, 5, "jbuf_gep");
402 // Save the frame pointer.
403 Value
*FramePtr
= Builder
.CreateConstGEP2_32(doubleUnderJBufTy
, JBufPtr
, 0, 0,
406 Value
*Val
= Builder
.CreateCall(FrameAddrFn
, Builder
.getInt32(0), "fp");
407 Builder
.CreateStore(Val
, FramePtr
, /*isVolatile=*/true);
409 // Save the stack pointer.
410 Value
*StackPtr
= Builder
.CreateConstGEP2_32(doubleUnderJBufTy
, JBufPtr
, 0, 2,
413 Val
= Builder
.CreateCall(StackAddrFn
, {}, "sp");
414 Builder
.CreateStore(Val
, StackPtr
, /*isVolatile=*/true);
416 // Call the setup_dispatch instrinsic. It fills in the rest of the jmpbuf.
417 Builder
.CreateCall(BuiltinSetupDispatchFn
, {});
419 // Store a pointer to the function context so that the back-end will know
420 // where to look for it.
421 Value
*FuncCtxArg
= Builder
.CreateBitCast(FuncCtx
, Builder
.getInt8PtrTy());
422 Builder
.CreateCall(FuncCtxFn
, FuncCtxArg
);
424 // At this point, we are all set up, update the invoke instructions to mark
425 // their call_site values.
426 for (unsigned I
= 0, E
= Invokes
.size(); I
!= E
; ++I
) {
427 insertCallSiteStore(Invokes
[I
], I
+ 1);
429 ConstantInt
*CallSiteNum
=
430 ConstantInt::get(Type::getInt32Ty(F
.getContext()), I
+ 1);
432 // Record the call site value for the back end so it stays associated with
434 CallInst::Create(CallSiteFn
, CallSiteNum
, "", Invokes
[I
]);
437 // Mark call instructions that aren't nounwind as no-action (call_site ==
438 // -1). Skip the entry block, as prior to then, no function context has been
439 // created for this function and any unexpected exceptions thrown will go
440 // directly to the caller's context, which is what we want anyway, so no need
441 // to do anything here.
442 for (BasicBlock
&BB
: F
) {
443 if (&BB
== &F
.front())
445 for (Instruction
&I
: BB
)
447 insertCallSiteStore(&I
, -1);
450 // Register the function context and make sure it's known to not throw
452 CallInst::Create(RegisterFn
, FuncCtx
, "", EntryBB
->getTerminator());
453 Register
->setDoesNotThrow();
455 // Following any allocas not in the entry block, update the saved SP in the
456 // jmpbuf to the new value.
457 for (BasicBlock
&BB
: F
) {
458 if (&BB
== &F
.front())
460 for (Instruction
&I
: BB
) {
461 if (auto *CI
= dyn_cast
<CallInst
>(&I
)) {
462 if (CI
->getCalledFunction() != StackRestoreFn
)
464 } else if (!isa
<AllocaInst
>(&I
)) {
467 Instruction
*StackAddr
= CallInst::Create(StackAddrFn
, "sp");
468 StackAddr
->insertAfter(&I
);
469 new StoreInst(StackAddr
, StackPtr
, true, StackAddr
->getNextNode());
473 // Finally, for any returns from this function, if this function contains an
474 // invoke, add a call to unregister the function context.
475 for (ReturnInst
*Return
: Returns
) {
476 Instruction
*InsertPoint
= Return
;
477 if (CallInst
*CI
= Return
->getParent()->getTerminatingMustTailCall())
479 CallInst::Create(UnregisterFn
, FuncCtx
, "", InsertPoint
);
485 bool SjLjEHPrepare::runOnFunction(Function
&F
) {
486 Module
&M
= *F
.getParent();
487 RegisterFn
= M
.getOrInsertFunction(
488 "_Unwind_SjLj_Register", Type::getVoidTy(M
.getContext()),
489 PointerType::getUnqual(FunctionContextTy
));
490 UnregisterFn
= M
.getOrInsertFunction(
491 "_Unwind_SjLj_Unregister", Type::getVoidTy(M
.getContext()),
492 PointerType::getUnqual(FunctionContextTy
));
493 FrameAddrFn
= Intrinsic::getDeclaration(
494 &M
, Intrinsic::frameaddress
,
495 {Type::getInt8PtrTy(M
.getContext(),
496 M
.getDataLayout().getAllocaAddrSpace())});
497 StackAddrFn
= Intrinsic::getDeclaration(&M
, Intrinsic::stacksave
);
498 StackRestoreFn
= Intrinsic::getDeclaration(&M
, Intrinsic::stackrestore
);
499 BuiltinSetupDispatchFn
=
500 Intrinsic::getDeclaration(&M
, Intrinsic::eh_sjlj_setup_dispatch
);
501 LSDAAddrFn
= Intrinsic::getDeclaration(&M
, Intrinsic::eh_sjlj_lsda
);
502 CallSiteFn
= Intrinsic::getDeclaration(&M
, Intrinsic::eh_sjlj_callsite
);
503 FuncCtxFn
= Intrinsic::getDeclaration(&M
, Intrinsic::eh_sjlj_functioncontext
);
505 bool Res
= setupEntryBlockAndCallSites(F
);