1//===- SjLjEHPrepare.cpp - Eliminate Invoke & Unwind instructions ---------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8//
9// This transformation is designed for use by code generators which use SjLj
10// based exception handling.
11//
12//===----------------------------------------------------------------------===//
13
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/Transforms/Utils/Local.h"
32using namespace llvm;
33
34#define DEBUG_TYPE "sjlj-eh-prepare"
35
36STATISTIC(NumInvokes, "Number of invokes replaced");
37STATISTIC(NumSpilled, "Number of registers live across unwind edges");
38
39namespace {
40class SjLjEHPrepareImpl {
41 IntegerType *DataTy = nullptr;
42 Type *doubleUnderDataTy = nullptr;
43 Type *doubleUnderJBufTy = nullptr;
44 StructType *FunctionContextTy = nullptr;
45 FunctionCallee RegisterFn;
46 FunctionCallee UnregisterFn;
47 Function *BuiltinSetupDispatchFn = nullptr;
48 Function *FrameAddrFn = nullptr;
49 Function *StackAddrFn = nullptr;
50 Function *StackRestoreFn = nullptr;
51 Function *LSDAAddrFn = nullptr;
52 Function *CallSiteFn = nullptr;
53 Function *FuncCtxFn = nullptr;
54 AllocaInst *FuncCtx = nullptr;
55
56 // The module's "exception-model" flag.
57 ExceptionHandling ExceptionModel = ExceptionHandling::Default;
58
59public:
60 bool doInitialization(Module &M);
61 bool runOnFunction(Function &F);
62
63private:
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);
70};
71
72class SjLjEHPrepare : public FunctionPass {
73 SjLjEHPrepareImpl Impl;
74
75public:
76 static char ID; // Pass identification, replacement for typeid
77 SjLjEHPrepare() : FunctionPass(ID) {}
78 bool doInitialization(Module &M) override { return Impl.doInitialization(M); }
79 bool runOnFunction(Function &F) override { return Impl.runOnFunction(F); };
80
81 StringRef getPassName() const override {
82 return "SJLJ Exception Handling preparation";
83 }
84};
85
86} // end anonymous namespace
87
88PreservedAnalyses SjLjEHPreparePass::run(Function &F,
89 FunctionAnalysisManager &FAM) {
90 SjLjEHPrepareImpl Impl;
91 Impl.doInitialization(M&: *F.getParent());
92 bool Changed = Impl.runOnFunction(F);
93 return Changed ? PreservedAnalyses::none() : PreservedAnalyses::all();
94}
95
96char SjLjEHPrepare::ID = 0;
97INITIALIZE_PASS(SjLjEHPrepare, DEBUG_TYPE, "Prepare SjLj exceptions",
98 false, false)
99
100// Public Interface To the SjLjEHPrepare pass.
101FunctionPass *llvm::createSjLjEHPreparePass() { return new SjLjEHPrepare(); }
102
103StructType *llvm::getSjLjFunctionContextType(const Module &M) {
104 LLVMContext &Ctx = M.getContext();
105 unsigned DataBits = M.getTargetTriple().getArchPointerBitWidth();
106 if (DataBits == 0)
107 DataBits = M.getDataLayout().getPointerSizeInBits();
108 Type *DataTy = Type::getIntNTy(C&: Ctx, N: DataBits);
109 // builtin_setjmp uses a five word jbuf
110 Type *VoidPtrTy = PointerType::getUnqual(C&: Ctx);
111 return StructType::get(elt1: VoidPtrTy, // __prev
112 elts: Type::getInt32Ty(C&: Ctx), // call_site
113 elts: ArrayType::get(ElementType: DataTy, NumElements: 4), // __data
114 elts: VoidPtrTy, // __personality
115 elts: VoidPtrTy, // __lsda
116 elts: ArrayType::get(ElementType: VoidPtrTy, NumElements: 5) // __jbuf
117 );
118}
119
120// doInitialization - Set up decalarations and types needed to process
121// exceptions.
122bool SjLjEHPrepareImpl::doInitialization(Module &M) {
123 ExceptionModel = M.getExceptionModel();
124
125 FunctionContextTy = getSjLjFunctionContextType(M);
126 doubleUnderDataTy = FunctionContextTy->getElementType(N: 2);
127 doubleUnderJBufTy = FunctionContextTy->getElementType(N: 5);
128 DataTy = cast<IntegerType>(Val: doubleUnderDataTy->getArrayElementType());
129
130 return false;
131}
132
133/// insertCallSiteStore - Insert a store of the call-site value to the
134/// function context
135void SjLjEHPrepareImpl::insertCallSiteStore(Instruction *I, int Number) {
136 IRBuilder<> Builder(I);
137 Builder.disableGEPCanonicalization();
138
139 // Get a reference to the call_site field.
140 Type *Int32Ty = Type::getInt32Ty(C&: I->getContext());
141 Value *Zero = ConstantInt::get(Ty: Int32Ty, V: 0);
142 Value *One = ConstantInt::get(Ty: Int32Ty, V: 1);
143 Value *Idxs[2] = { Zero, One };
144 Value *CallSite =
145 Builder.CreateGEP(Ty: FunctionContextTy, Ptr: FuncCtx, IdxList: Idxs, Name: "call_site");
146
147 // Insert a store of the call-site number
148 ConstantInt *CallSiteNoC = Builder.getInt32(C: Number);
149 Builder.CreateStore(Val: CallSiteNoC, Ptr: CallSite, isVolatile: true /*volatile*/);
150}
151
152/// MarkBlocksLiveIn - Insert BB and all of its predecessors into LiveBBs until
153/// we reach blocks we've already seen.
154static void MarkBlocksLiveIn(BasicBlock *BB,
155 SmallPtrSetImpl<BasicBlock *> &LiveBBs) {
156 if (!LiveBBs.insert(Ptr: BB).second)
157 return; // already been here.
158
159 LiveBBs.insert_range(R: inverse_depth_first(G: BB));
160}
161
162/// substituteLPadValues - Substitute the values returned by the landingpad
163/// instruction with those returned by the personality function.
164void SjLjEHPrepareImpl::substituteLPadValues(LandingPadInst *LPI, Value *ExnVal,
165 Value *SelVal) {
166 SmallVector<Value *, 8> UseWorkList(LPI->users());
167 while (!UseWorkList.empty()) {
168 Value *Val = UseWorkList.pop_back_val();
169 auto *EVI = dyn_cast<ExtractValueInst>(Val);
170 if (!EVI)
171 continue;
172 if (EVI->getNumIndices() != 1)
173 continue;
174 if (*EVI->idx_begin() == 0)
175 EVI->replaceAllUsesWith(V: ExnVal);
176 else if (*EVI->idx_begin() == 1)
177 EVI->replaceAllUsesWith(V: SelVal);
178 if (EVI->use_empty())
179 EVI->eraseFromParent();
180 }
181
182 if (LPI->use_empty())
183 return;
184
185 // There are still some uses of LPI. Construct an aggregate with the exception
186 // values and replace the LPI with that aggregate.
187 Type *LPadType = LPI->getType();
188 Value *LPadVal = PoisonValue::get(T: LPadType);
189 auto *SelI = cast<Instruction>(Val: SelVal);
190 IRBuilder<> Builder(std::next(x: SelI->getIterator()));
191 LPadVal = Builder.CreateInsertValue(Agg: LPadVal, Val: ExnVal, Idxs: 0, Name: "lpad.val");
192 LPadVal = Builder.CreateInsertValue(Agg: LPadVal, Val: SelVal, Idxs: 1, Name: "lpad.val");
193
194 LPI->replaceAllUsesWith(V: LPadVal);
195}
196
197/// setupFunctionContext - Allocate the function context on the stack and fill
198/// it with all of the data that we know at this point.
199Value *
200SjLjEHPrepareImpl::setupFunctionContext(Function &F,
201 ArrayRef<LandingPadInst *> LPads) {
202 BasicBlock *EntryBB = &F.front();
203
204 // Create an alloca for the incoming jump buffer ptr and the new jump buffer
205 // that needs to be restored on all exits from the function. This is an alloca
206 // because the value needs to be added to the global context list.
207 auto &DL = F.getDataLayout();
208 const Align Alignment = DL.getPrefTypeAlign(Ty: FunctionContextTy);
209 FuncCtx = new AllocaInst(FunctionContextTy, DL.getAllocaAddrSpace(), nullptr,
210 Alignment, "fn_context", EntryBB->begin());
211
212 // Fill in the function context structure.
213 for (LandingPadInst *LPI : LPads) {
214 IRBuilder<> Builder(LPI->getParent()->getFirstInsertionPt());
215 Builder.disableGEPCanonicalization();
216
217 // Reference the __data field.
218 Value *FCData =
219 Builder.CreateConstGEP2_32(Ty: FunctionContextTy, Ptr: FuncCtx, Idx0: 0, Idx1: 2, Name: "__data");
220
221 // The exception values come back in context->__data[0].
222 Value *ExceptionAddr = Builder.CreateConstGEP2_32(Ty: doubleUnderDataTy, Ptr: FCData,
223 Idx0: 0, Idx1: 0, Name: "exception_gep");
224 Value *ExnVal = Builder.CreateLoad(Ty: DataTy, Ptr: ExceptionAddr, isVolatile: true, Name: "exn_val");
225 ExnVal = Builder.CreateIntToPtr(V: ExnVal, DestTy: Builder.getPtrTy());
226
227 Value *SelectorAddr = Builder.CreateConstGEP2_32(Ty: doubleUnderDataTy, Ptr: FCData,
228 Idx0: 0, Idx1: 1, Name: "exn_selector_gep");
229 Value *SelVal =
230 Builder.CreateLoad(Ty: DataTy, Ptr: SelectorAddr, isVolatile: true, Name: "exn_selector_val");
231
232 // SelVal must be Int32Ty, so trunc it
233 SelVal = Builder.CreateTrunc(V: SelVal, DestTy: Type::getInt32Ty(C&: F.getContext()));
234
235 substituteLPadValues(LPI, ExnVal, SelVal);
236 }
237
238 // Personality function
239 IRBuilder<> Builder(EntryBB->getTerminator());
240 Builder.disableGEPCanonicalization();
241 Value *PersonalityFn = F.getPersonalityFn();
242 Value *PersonalityFieldPtr = Builder.CreateConstGEP2_32(
243 Ty: FunctionContextTy, Ptr: FuncCtx, Idx0: 0, Idx1: 3, Name: "pers_fn_gep");
244 Builder.CreateStore(Val: PersonalityFn, Ptr: PersonalityFieldPtr, /*isVolatile=*/true);
245
246 // LSDA address
247 Value *LSDA = Builder.CreateCall(Callee: LSDAAddrFn, Args: {}, Name: "lsda_addr");
248 Value *LSDAFieldPtr =
249 Builder.CreateConstGEP2_32(Ty: FunctionContextTy, Ptr: FuncCtx, Idx0: 0, Idx1: 4, Name: "lsda_gep");
250 Builder.CreateStore(Val: LSDA, Ptr: LSDAFieldPtr, /*isVolatile=*/true);
251
252 return FuncCtx;
253}
254
255/// lowerIncomingArguments - To avoid having to handle incoming arguments
256/// specially, we lower each arg to a copy instruction in the entry block. This
257/// ensures that the argument value itself cannot be live out of the entry
258/// block.
259void SjLjEHPrepareImpl::lowerIncomingArguments(Function &F) {
260 BasicBlock::iterator AfterAllocaInsPt = F.begin()->begin();
261 while (isa<AllocaInst>(Val: AfterAllocaInsPt) &&
262 cast<AllocaInst>(Val&: AfterAllocaInsPt)->isStaticAlloca())
263 ++AfterAllocaInsPt;
264 assert(AfterAllocaInsPt != F.front().end());
265
266 for (auto &AI : F.args()) {
267 // Swift error really is a register that we model as memory -- instruction
268 // selection will perform mem-to-reg for us and spill/reload appropriately
269 // around calls that clobber it. There is no need to spill this
270 // value to the stack and doing so would not be allowed.
271 if (AI.isSwiftError())
272 continue;
273
274 Type *Ty = AI.getType();
275
276 // Use 'select i8 true, %arg, poison' to simulate a 'no-op' instruction.
277 Value *TrueValue = ConstantInt::getTrue(Context&: F.getContext());
278 Value *PoisonValue = PoisonValue::get(T: Ty);
279 Instruction *SI = SelectInst::Create(
280 C: TrueValue, S1: &AI, S2: PoisonValue, NameStr: AI.getName() + ".tmp", InsertBefore: AfterAllocaInsPt);
281 AI.replaceAllUsesWith(V: SI);
282
283 // Reset the operand, because it was clobbered by the RAUW above.
284 SI->setOperand(i: 1, Val: &AI);
285 }
286}
287
288/// lowerAcrossUnwindEdges - Find all variables which are alive across an unwind
289/// edge and spill them.
290void SjLjEHPrepareImpl::lowerAcrossUnwindEdges(Function &F,
291 ArrayRef<InvokeInst *> Invokes) {
292 // Finally, scan the code looking for instructions with bad live ranges.
293 for (BasicBlock &BB : F) {
294 for (Instruction &Inst : BB) {
295 // Ignore obvious cases we don't have to handle. In particular, most
296 // instructions either have no uses or only have a single use inside the
297 // current block. Ignore them quickly.
298 if (Inst.use_empty())
299 continue;
300 if (Inst.hasOneUse() &&
301 cast<Instruction>(Val: Inst.user_back())->getParent() == &BB &&
302 !isa<PHINode>(Val: Inst.user_back()))
303 continue;
304
305 // If this is an alloca in the entry block, it's not a real register
306 // value.
307 if (auto *AI = dyn_cast<AllocaInst>(Val: &Inst))
308 if (AI->isStaticAlloca())
309 continue;
310
311 // Avoid iterator invalidation by copying users to a temporary vector.
312 SmallVector<Instruction *, 16> Users;
313 for (User *U : Inst.users()) {
314 Instruction *UI = cast<Instruction>(Val: U);
315 if (UI->getParent() != &BB || isa<PHINode>(Val: UI))
316 Users.push_back(Elt: UI);
317 }
318
319 // Find all of the blocks that this value is live in.
320 SmallPtrSet<BasicBlock *, 32> LiveBBs;
321 LiveBBs.insert(Ptr: &BB);
322 while (!Users.empty()) {
323 Instruction *U = Users.pop_back_val();
324
325 if (!isa<PHINode>(Val: U)) {
326 MarkBlocksLiveIn(BB: U->getParent(), LiveBBs);
327 } else {
328 // Uses for a PHI node occur in their predecessor block.
329 PHINode *PN = cast<PHINode>(Val: U);
330 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
331 if (PN->getIncomingValue(i) == &Inst)
332 MarkBlocksLiveIn(BB: PN->getIncomingBlock(i), LiveBBs);
333 }
334 }
335
336 // Now that we know all of the blocks that this thing is live in, see if
337 // it includes any of the unwind locations.
338 bool NeedsSpill = false;
339 for (InvokeInst *Invoke : Invokes) {
340 BasicBlock *UnwindBlock = Invoke->getUnwindDest();
341 if (UnwindBlock != &BB && LiveBBs.count(Ptr: UnwindBlock)) {
342 LLVM_DEBUG(dbgs() << "SJLJ Spill: " << Inst << " around "
343 << UnwindBlock->getName() << "\n");
344 NeedsSpill = true;
345 break;
346 }
347 }
348
349 // If we decided we need a spill, do it.
350 // FIXME: Spilling this way is overkill, as it forces all uses of
351 // the value to be reloaded from the stack slot, even those that aren't
352 // in the unwind blocks. We should be more selective.
353 if (NeedsSpill) {
354 DemoteRegToStack(X&: Inst, VolatileLoads: true);
355 ++NumSpilled;
356 }
357 }
358 }
359
360 // Go through the landing pads and remove any PHIs there.
361 for (InvokeInst *Invoke : Invokes) {
362 BasicBlock *UnwindBlock = Invoke->getUnwindDest();
363 LandingPadInst *LPI = UnwindBlock->getLandingPadInst();
364
365 // Place PHIs into a set to avoid invalidating the iterator.
366 SmallPtrSet<PHINode *, 8> PHIsToDemote;
367 for (BasicBlock::iterator PN = UnwindBlock->begin(); isa<PHINode>(Val: PN); ++PN)
368 PHIsToDemote.insert(Ptr: cast<PHINode>(Val&: PN));
369 if (PHIsToDemote.empty())
370 continue;
371
372 // Demote the PHIs to the stack.
373 for (PHINode *PN : PHIsToDemote)
374 DemotePHIToStack(P: PN);
375
376 // Move the landingpad instruction back to the top of the landing pad block.
377 LPI->moveBefore(InsertPos: UnwindBlock->begin());
378 }
379}
380
381/// setupEntryBlockAndCallSites - Setup the entry block by creating and filling
382/// the function context and marking the call sites with the appropriate
383/// values. These values are used by the DWARF EH emitter.
384bool SjLjEHPrepareImpl::setupEntryBlockAndCallSites(Function &F) {
385 SmallVector<ReturnInst *, 16> Returns;
386 SmallVector<InvokeInst *, 16> Invokes;
387 SmallSetVector<LandingPadInst *, 16> LPads;
388
389 // Look through the terminators of the basic blocks to find invokes.
390 for (BasicBlock &BB : F)
391 if (auto *II = dyn_cast<InvokeInst>(Val: BB.getTerminator())) {
392 if (Function *Callee = II->getCalledFunction())
393 if (Callee->getIntrinsicID() == Intrinsic::donothing) {
394 // Remove the NOP invoke.
395 UncondBrInst::Create(Target: II->getNormalDest(), InsertBefore: II->getIterator());
396 II->eraseFromParent();
397 continue;
398 }
399
400 Invokes.push_back(Elt: II);
401 LPads.insert(X: II->getUnwindDest()->getLandingPadInst());
402 } else if (auto *RI = dyn_cast<ReturnInst>(Val: BB.getTerminator())) {
403 Returns.push_back(Elt: RI);
404 }
405
406 if (Invokes.empty())
407 return false;
408
409 NumInvokes += Invokes.size();
410
411 lowerIncomingArguments(F);
412 lowerAcrossUnwindEdges(F, Invokes);
413
414 Value *FuncCtx =
415 setupFunctionContext(F, LPads: ArrayRef(LPads.begin(), LPads.end()));
416 BasicBlock *EntryBB = &F.front();
417 IRBuilder<> Builder(EntryBB->getTerminator());
418 Builder.disableGEPCanonicalization();
419
420 // Get a reference to the jump buffer.
421 Value *JBufPtr =
422 Builder.CreateConstGEP2_32(Ty: FunctionContextTy, Ptr: FuncCtx, Idx0: 0, Idx1: 5, Name: "jbuf_gep");
423
424 // Save the frame pointer.
425 Value *FramePtr = Builder.CreateConstGEP2_32(Ty: doubleUnderJBufTy, Ptr: JBufPtr, Idx0: 0, Idx1: 0,
426 Name: "jbuf_fp_gep");
427
428 Value *Val = Builder.CreateCall(Callee: FrameAddrFn, Args: Builder.getInt32(C: 0), Name: "fp");
429 Builder.CreateStore(Val, Ptr: FramePtr, /*isVolatile=*/true);
430
431 // Save the stack pointer.
432 Value *StackPtr = Builder.CreateConstGEP2_32(Ty: doubleUnderJBufTy, Ptr: JBufPtr, Idx0: 0, Idx1: 2,
433 Name: "jbuf_sp_gep");
434
435 Val = Builder.CreateCall(Callee: StackAddrFn, Args: {}, Name: "sp");
436 Builder.CreateStore(Val, Ptr: StackPtr, /*isVolatile=*/true);
437
438 // Call the setup_dispatch intrinsic. It fills in the rest of the jmpbuf.
439 Builder.CreateCall(Callee: BuiltinSetupDispatchFn, Args: {});
440
441 // Store a pointer to the function context so that the back-end will know
442 // where to look for it.
443 Builder.CreateCall(Callee: FuncCtxFn, Args: FuncCtx);
444
445 // Register the function context and make sure it's known to not throw.
446 CallInst *Register = Builder.CreateCall(Callee: RegisterFn, Args: FuncCtx, Name: "");
447 Register->setDoesNotThrow();
448
449 // At this point, we are all set up, update the invoke instructions to mark
450 // their call_site values.
451 for (unsigned I = 0, E = Invokes.size(); I != E; ++I) {
452 insertCallSiteStore(I: Invokes[I], Number: I + 1);
453
454 ConstantInt *CallSiteNum =
455 ConstantInt::get(Ty: Type::getInt32Ty(C&: F.getContext()), V: I + 1);
456
457 // Record the call site value for the back end so it stays associated with
458 // the invoke.
459 CallInst::Create(Func: CallSiteFn, Args: CallSiteNum, NameStr: "", InsertBefore: Invokes[I]->getIterator());
460 }
461
462 // Mark call instructions that aren't nounwind as no-action (call_site ==
463 // -1). Skip the entry block, as prior to then, no function context has been
464 // created for this function and any unexpected exceptions thrown will go
465 // directly to the caller's context, which is what we want anyway, so no need
466 // to do anything here.
467 for (BasicBlock &BB : F) {
468 if (&BB == &F.front())
469 continue;
470 for (Instruction &I : BB)
471 if (!isa<InvokeInst>(Val: I) && I.mayThrow())
472 insertCallSiteStore(I: &I, Number: -1);
473 }
474
475 // Following any allocas not in the entry block, update the saved SP in the
476 // jmpbuf to the new value.
477 for (BasicBlock &BB : F) {
478 if (&BB == &F.front())
479 continue;
480 for (Instruction &I : BB) {
481 if (auto *CI = dyn_cast<CallInst>(Val: &I)) {
482 if (CI->getCalledFunction() != StackRestoreFn)
483 continue;
484 } else if (!isa<AllocaInst>(Val: &I)) {
485 continue;
486 }
487 Instruction *StackAddr = CallInst::Create(Func: StackAddrFn, NameStr: "sp");
488 StackAddr->insertAfter(InsertPos: I.getIterator());
489 new StoreInst(StackAddr, StackPtr, true,
490 std::next(x: StackAddr->getIterator()));
491 }
492 }
493
494 // Finally, for any returns from this function, if this function contains an
495 // invoke, add a call to unregister the function context.
496 for (ReturnInst *Return : Returns) {
497 Instruction *InsertPoint = Return;
498 if (CallInst *CI = Return->getParent()->getTerminatingMustTailCall())
499 InsertPoint = CI;
500 CallInst::Create(Func: UnregisterFn, Args: FuncCtx, NameStr: "", InsertBefore: InsertPoint->getIterator());
501 }
502
503 return true;
504}
505
506bool SjLjEHPrepareImpl::runOnFunction(Function &F) {
507 if (ExceptionModel != ExceptionHandling::SjLj)
508 return false;
509
510 Module &M = *F.getParent();
511 RegisterFn = M.getOrInsertFunction(
512 Name: "_Unwind_SjLj_Register", RetTy: Type::getVoidTy(C&: M.getContext()),
513 Args: PointerType::getUnqual(C&: FunctionContextTy->getContext()));
514 UnregisterFn = M.getOrInsertFunction(
515 Name: "_Unwind_SjLj_Unregister", RetTy: Type::getVoidTy(C&: M.getContext()),
516 Args: PointerType::getUnqual(C&: FunctionContextTy->getContext()));
517
518 PointerType *AllocaPtrTy = M.getDataLayout().getAllocaPtrType(Ctx&: M.getContext());
519
520 FrameAddrFn = Intrinsic::getOrInsertDeclaration(M: &M, id: Intrinsic::frameaddress,
521 OverloadTys: {AllocaPtrTy});
522 StackAddrFn = Intrinsic::getOrInsertDeclaration(M: &M, id: Intrinsic::stacksave,
523 OverloadTys: {AllocaPtrTy});
524 StackRestoreFn = Intrinsic::getOrInsertDeclaration(
525 M: &M, id: Intrinsic::stackrestore, OverloadTys: {AllocaPtrTy});
526 BuiltinSetupDispatchFn =
527 Intrinsic::getOrInsertDeclaration(M: &M, id: Intrinsic::eh_sjlj_setup_dispatch);
528 LSDAAddrFn = Intrinsic::getOrInsertDeclaration(M: &M, id: Intrinsic::eh_sjlj_lsda);
529 CallSiteFn =
530 Intrinsic::getOrInsertDeclaration(M: &M, id: Intrinsic::eh_sjlj_callsite);
531 FuncCtxFn =
532 Intrinsic::getOrInsertDeclaration(M: &M, id: Intrinsic::eh_sjlj_functioncontext);
533
534 bool Res = setupEntryBlockAndCallSites(F);
535 return Res;
536}
537