1 //===-- WinEHPrepare - Prepare exception handling for code generation ---===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This pass lowers LLVM IR exception handling into something closer to what the
11 // backend wants. It snifs the personality function to see which kind of
12 // preparation is necessary. If the personality function uses the Itanium LSDA,
13 // this pass delegates to the DWARF EH preparation pass.
14 //
15 //===----------------------------------------------------------------------===//
16
17 #include "llvm/CodeGen/Passes.h"
18 #include "llvm/ADT/MapVector.h"
19 #include "llvm/ADT/STLExtras.h"
20 #include "llvm/ADT/SmallSet.h"
21 #include "llvm/ADT/TinyPtrVector.h"
22 #include "llvm/Analysis/LibCallSemantics.h"
23 #include "llvm/CodeGen/WinEHFuncInfo.h"
24 #include "llvm/IR/Dominators.h"
25 #include "llvm/IR/Function.h"
26 #include "llvm/IR/IRBuilder.h"
27 #include "llvm/IR/Instructions.h"
28 #include "llvm/IR/IntrinsicInst.h"
29 #include "llvm/IR/Module.h"
30 #include "llvm/IR/PatternMatch.h"
31 #include "llvm/Pass.h"
32 #include "llvm/Support/CommandLine.h"
33 #include "llvm/Support/Debug.h"
34 #include "llvm/Support/raw_ostream.h"
35 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
36 #include "llvm/Transforms/Utils/Cloning.h"
37 #include "llvm/Transforms/Utils/Local.h"
38 #include "llvm/Transforms/Utils/PromoteMemToReg.h"
39 #include <memory>
40
41 using namespace llvm;
42 using namespace llvm::PatternMatch;
43
44 #define DEBUG_TYPE "winehprepare"
45
46 namespace {
47
48 // This map is used to model frame variable usage during outlining, to
49 // construct a structure type to hold the frame variables in a frame
50 // allocation block, and to remap the frame variable allocas (including
51 // spill locations as needed) to GEPs that get the variable from the
52 // frame allocation structure.
53 typedef MapVector<Value *, TinyPtrVector<AllocaInst *>> FrameVarInfoMap;
54
55 // TinyPtrVector cannot hold nullptr, so we need our own sentinel that isn't
56 // quite null.
getCatchObjectSentinel()57 AllocaInst *getCatchObjectSentinel() {
58 return static_cast<AllocaInst *>(nullptr) + 1;
59 }
60
61 typedef SmallSet<BasicBlock *, 4> VisitedBlockSet;
62
63 class LandingPadActions;
64 class LandingPadMap;
65
66 typedef DenseMap<const BasicBlock *, CatchHandler *> CatchHandlerMapTy;
67 typedef DenseMap<const BasicBlock *, CleanupHandler *> CleanupHandlerMapTy;
68
69 class WinEHPrepare : public FunctionPass {
70 public:
71 static char ID; // Pass identification, replacement for typeid.
WinEHPrepare(const TargetMachine * TM=nullptr)72 WinEHPrepare(const TargetMachine *TM = nullptr)
73 : FunctionPass(ID), DT(nullptr) {}
74
75 bool runOnFunction(Function &Fn) override;
76
77 bool doFinalization(Module &M) override;
78
79 void getAnalysisUsage(AnalysisUsage &AU) const override;
80
getPassName() const81 const char *getPassName() const override {
82 return "Windows exception handling preparation";
83 }
84
85 private:
86 bool prepareExceptionHandlers(Function &F,
87 SmallVectorImpl<LandingPadInst *> &LPads);
88 void promoteLandingPadValues(LandingPadInst *LPad);
89 void completeNestedLandingPad(Function *ParentFn,
90 LandingPadInst *OutlinedLPad,
91 const LandingPadInst *OriginalLPad,
92 FrameVarInfoMap &VarInfo);
93 bool outlineHandler(ActionHandler *Action, Function *SrcFn,
94 LandingPadInst *LPad, BasicBlock *StartBB,
95 FrameVarInfoMap &VarInfo);
96 void addStubInvokeToHandlerIfNeeded(Function *Handler, Value *PersonalityFn);
97
98 void mapLandingPadBlocks(LandingPadInst *LPad, LandingPadActions &Actions);
99 CatchHandler *findCatchHandler(BasicBlock *BB, BasicBlock *&NextBB,
100 VisitedBlockSet &VisitedBlocks);
101 void findCleanupHandlers(LandingPadActions &Actions, BasicBlock *StartBB,
102 BasicBlock *EndBB);
103
104 void processSEHCatchHandler(CatchHandler *Handler, BasicBlock *StartBB);
105
106 // All fields are reset by runOnFunction.
107 DominatorTree *DT;
108 EHPersonality Personality;
109 CatchHandlerMapTy CatchHandlerMap;
110 CleanupHandlerMapTy CleanupHandlerMap;
111 DenseMap<const LandingPadInst *, LandingPadMap> LPadMaps;
112
113 // This maps landing pad instructions found in outlined handlers to
114 // the landing pad instruction in the parent function from which they
115 // were cloned. The cloned/nested landing pad is used as the key
116 // because the landing pad may be cloned into multiple handlers.
117 // This map will be used to add the llvm.eh.actions call to the nested
118 // landing pads after all handlers have been outlined.
119 DenseMap<LandingPadInst *, const LandingPadInst *> NestedLPtoOriginalLP;
120
121 // This maps blocks in the parent function which are destinations of
122 // catch handlers to cloned blocks in (other) outlined handlers. This
123 // handles the case where a nested landing pads has a catch handler that
124 // returns to a handler function rather than the parent function.
125 // The original block is used as the key here because there should only
126 // ever be one handler function from which the cloned block is not pruned.
127 // The original block will be pruned from the parent function after all
128 // handlers have been outlined. This map will be used to adjust the
129 // return instructions of handlers which return to the block that was
130 // outlined into a handler. This is done after all handlers have been
131 // outlined but before the outlined code is pruned from the parent function.
132 DenseMap<const BasicBlock *, BasicBlock *> LPadTargetBlocks;
133 };
134
135 class WinEHFrameVariableMaterializer : public ValueMaterializer {
136 public:
137 WinEHFrameVariableMaterializer(Function *OutlinedFn,
138 FrameVarInfoMap &FrameVarInfo);
~WinEHFrameVariableMaterializer()139 ~WinEHFrameVariableMaterializer() override {}
140
141 Value *materializeValueFor(Value *V) override;
142
143 void escapeCatchObject(Value *V);
144
145 private:
146 FrameVarInfoMap &FrameVarInfo;
147 IRBuilder<> Builder;
148 };
149
150 class LandingPadMap {
151 public:
LandingPadMap()152 LandingPadMap() : OriginLPad(nullptr) {}
153 void mapLandingPad(const LandingPadInst *LPad);
154
isInitialized()155 bool isInitialized() { return OriginLPad != nullptr; }
156
157 bool isOriginLandingPadBlock(const BasicBlock *BB) const;
158 bool isLandingPadSpecificInst(const Instruction *Inst) const;
159
160 void remapEHValues(ValueToValueMapTy &VMap, Value *EHPtrValue,
161 Value *SelectorValue) const;
162
163 private:
164 const LandingPadInst *OriginLPad;
165 // We will normally only see one of each of these instructions, but
166 // if more than one occurs for some reason we can handle that.
167 TinyPtrVector<const ExtractValueInst *> ExtractedEHPtrs;
168 TinyPtrVector<const ExtractValueInst *> ExtractedSelectors;
169 };
170
171 class WinEHCloningDirectorBase : public CloningDirector {
172 public:
WinEHCloningDirectorBase(Function * HandlerFn,FrameVarInfoMap & VarInfo,LandingPadMap & LPadMap)173 WinEHCloningDirectorBase(Function *HandlerFn, FrameVarInfoMap &VarInfo,
174 LandingPadMap &LPadMap)
175 : Materializer(HandlerFn, VarInfo),
176 SelectorIDType(Type::getInt32Ty(HandlerFn->getContext())),
177 Int8PtrType(Type::getInt8PtrTy(HandlerFn->getContext())),
178 LPadMap(LPadMap) {}
179
180 CloningAction handleInstruction(ValueToValueMapTy &VMap,
181 const Instruction *Inst,
182 BasicBlock *NewBB) override;
183
184 virtual CloningAction handleBeginCatch(ValueToValueMapTy &VMap,
185 const Instruction *Inst,
186 BasicBlock *NewBB) = 0;
187 virtual CloningAction handleEndCatch(ValueToValueMapTy &VMap,
188 const Instruction *Inst,
189 BasicBlock *NewBB) = 0;
190 virtual CloningAction handleTypeIdFor(ValueToValueMapTy &VMap,
191 const Instruction *Inst,
192 BasicBlock *NewBB) = 0;
193 virtual CloningAction handleInvoke(ValueToValueMapTy &VMap,
194 const InvokeInst *Invoke,
195 BasicBlock *NewBB) = 0;
196 virtual CloningAction handleResume(ValueToValueMapTy &VMap,
197 const ResumeInst *Resume,
198 BasicBlock *NewBB) = 0;
199 virtual CloningAction handleLandingPad(ValueToValueMapTy &VMap,
200 const LandingPadInst *LPad,
201 BasicBlock *NewBB) = 0;
202
getValueMaterializer()203 ValueMaterializer *getValueMaterializer() override { return &Materializer; }
204
205 protected:
206 WinEHFrameVariableMaterializer Materializer;
207 Type *SelectorIDType;
208 Type *Int8PtrType;
209 LandingPadMap &LPadMap;
210 };
211
212 class WinEHCatchDirector : public WinEHCloningDirectorBase {
213 public:
WinEHCatchDirector(Function * CatchFn,Value * Selector,FrameVarInfoMap & VarInfo,LandingPadMap & LPadMap,DenseMap<LandingPadInst *,const LandingPadInst * > & NestedLPads)214 WinEHCatchDirector(
215 Function *CatchFn, Value *Selector, FrameVarInfoMap &VarInfo,
216 LandingPadMap &LPadMap,
217 DenseMap<LandingPadInst *, const LandingPadInst *> &NestedLPads)
218 : WinEHCloningDirectorBase(CatchFn, VarInfo, LPadMap),
219 CurrentSelector(Selector->stripPointerCasts()),
220 ExceptionObjectVar(nullptr), NestedLPtoOriginalLP(NestedLPads) {}
221
222 CloningAction handleBeginCatch(ValueToValueMapTy &VMap,
223 const Instruction *Inst,
224 BasicBlock *NewBB) override;
225 CloningAction handleEndCatch(ValueToValueMapTy &VMap, const Instruction *Inst,
226 BasicBlock *NewBB) override;
227 CloningAction handleTypeIdFor(ValueToValueMapTy &VMap,
228 const Instruction *Inst,
229 BasicBlock *NewBB) override;
230 CloningAction handleInvoke(ValueToValueMapTy &VMap, const InvokeInst *Invoke,
231 BasicBlock *NewBB) override;
232 CloningAction handleResume(ValueToValueMapTy &VMap, const ResumeInst *Resume,
233 BasicBlock *NewBB) override;
234 CloningAction handleLandingPad(ValueToValueMapTy &VMap,
235 const LandingPadInst *LPad,
236 BasicBlock *NewBB) override;
237
getExceptionVar()238 Value *getExceptionVar() { return ExceptionObjectVar; }
getReturnTargets()239 TinyPtrVector<BasicBlock *> &getReturnTargets() { return ReturnTargets; }
240
241 private:
242 Value *CurrentSelector;
243
244 Value *ExceptionObjectVar;
245 TinyPtrVector<BasicBlock *> ReturnTargets;
246
247 // This will be a reference to the field of the same name in the WinEHPrepare
248 // object which instantiates this WinEHCatchDirector object.
249 DenseMap<LandingPadInst *, const LandingPadInst *> &NestedLPtoOriginalLP;
250 };
251
252 class WinEHCleanupDirector : public WinEHCloningDirectorBase {
253 public:
WinEHCleanupDirector(Function * CleanupFn,FrameVarInfoMap & VarInfo,LandingPadMap & LPadMap)254 WinEHCleanupDirector(Function *CleanupFn, FrameVarInfoMap &VarInfo,
255 LandingPadMap &LPadMap)
256 : WinEHCloningDirectorBase(CleanupFn, VarInfo, LPadMap) {}
257
258 CloningAction handleBeginCatch(ValueToValueMapTy &VMap,
259 const Instruction *Inst,
260 BasicBlock *NewBB) override;
261 CloningAction handleEndCatch(ValueToValueMapTy &VMap, const Instruction *Inst,
262 BasicBlock *NewBB) override;
263 CloningAction handleTypeIdFor(ValueToValueMapTy &VMap,
264 const Instruction *Inst,
265 BasicBlock *NewBB) override;
266 CloningAction handleInvoke(ValueToValueMapTy &VMap, const InvokeInst *Invoke,
267 BasicBlock *NewBB) override;
268 CloningAction handleResume(ValueToValueMapTy &VMap, const ResumeInst *Resume,
269 BasicBlock *NewBB) override;
270 CloningAction handleLandingPad(ValueToValueMapTy &VMap,
271 const LandingPadInst *LPad,
272 BasicBlock *NewBB) override;
273 };
274
275 class LandingPadActions {
276 public:
LandingPadActions()277 LandingPadActions() : HasCleanupHandlers(false) {}
278
insertCatchHandler(CatchHandler * Action)279 void insertCatchHandler(CatchHandler *Action) { Actions.push_back(Action); }
insertCleanupHandler(CleanupHandler * Action)280 void insertCleanupHandler(CleanupHandler *Action) {
281 Actions.push_back(Action);
282 HasCleanupHandlers = true;
283 }
284
includesCleanup() const285 bool includesCleanup() const { return HasCleanupHandlers; }
286
actions()287 SmallVectorImpl<ActionHandler *> &actions() { return Actions; }
begin()288 SmallVectorImpl<ActionHandler *>::iterator begin() { return Actions.begin(); }
end()289 SmallVectorImpl<ActionHandler *>::iterator end() { return Actions.end(); }
290
291 private:
292 // Note that this class does not own the ActionHandler objects in this vector.
293 // The ActionHandlers are owned by the CatchHandlerMap and CleanupHandlerMap
294 // in the WinEHPrepare class.
295 SmallVector<ActionHandler *, 4> Actions;
296 bool HasCleanupHandlers;
297 };
298
299 } // end anonymous namespace
300
301 char WinEHPrepare::ID = 0;
302 INITIALIZE_TM_PASS(WinEHPrepare, "winehprepare", "Prepare Windows exceptions",
303 false, false)
304
createWinEHPass(const TargetMachine * TM)305 FunctionPass *llvm::createWinEHPass(const TargetMachine *TM) {
306 return new WinEHPrepare(TM);
307 }
308
309 // FIXME: Remove this once the backend can handle the prepared IR.
310 static cl::opt<bool>
311 SEHPrepare("sehprepare", cl::Hidden,
312 cl::desc("Prepare functions with SEH personalities"));
313
runOnFunction(Function & Fn)314 bool WinEHPrepare::runOnFunction(Function &Fn) {
315 SmallVector<LandingPadInst *, 4> LPads;
316 SmallVector<ResumeInst *, 4> Resumes;
317 for (BasicBlock &BB : Fn) {
318 if (auto *LP = BB.getLandingPadInst())
319 LPads.push_back(LP);
320 if (auto *Resume = dyn_cast<ResumeInst>(BB.getTerminator()))
321 Resumes.push_back(Resume);
322 }
323
324 // No need to prepare functions that lack landing pads.
325 if (LPads.empty())
326 return false;
327
328 // Classify the personality to see what kind of preparation we need.
329 Personality = classifyEHPersonality(LPads.back()->getPersonalityFn());
330
331 // Do nothing if this is not an MSVC personality.
332 if (!isMSVCEHPersonality(Personality))
333 return false;
334
335 DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
336
337 if (isAsynchronousEHPersonality(Personality) && !SEHPrepare) {
338 // Replace all resume instructions with unreachable.
339 // FIXME: Remove this once the backend can handle the prepared IR.
340 for (ResumeInst *Resume : Resumes) {
341 IRBuilder<>(Resume).CreateUnreachable();
342 Resume->eraseFromParent();
343 }
344 return true;
345 }
346
347 // If there were any landing pads, prepareExceptionHandlers will make changes.
348 prepareExceptionHandlers(Fn, LPads);
349 return true;
350 }
351
doFinalization(Module & M)352 bool WinEHPrepare::doFinalization(Module &M) { return false; }
353
getAnalysisUsage(AnalysisUsage & AU) const354 void WinEHPrepare::getAnalysisUsage(AnalysisUsage &AU) const {
355 AU.addRequired<DominatorTreeWrapperPass>();
356 }
357
prepareExceptionHandlers(Function & F,SmallVectorImpl<LandingPadInst * > & LPads)358 bool WinEHPrepare::prepareExceptionHandlers(
359 Function &F, SmallVectorImpl<LandingPadInst *> &LPads) {
360 // These containers are used to re-map frame variables that are used in
361 // outlined catch and cleanup handlers. They will be populated as the
362 // handlers are outlined.
363 FrameVarInfoMap FrameVarInfo;
364
365 bool HandlersOutlined = false;
366
367 Module *M = F.getParent();
368 LLVMContext &Context = M->getContext();
369
370 // Create a new function to receive the handler contents.
371 PointerType *Int8PtrType = Type::getInt8PtrTy(Context);
372 Type *Int32Type = Type::getInt32Ty(Context);
373 Function *ActionIntrin = Intrinsic::getDeclaration(M, Intrinsic::eh_actions);
374
375 for (LandingPadInst *LPad : LPads) {
376 // Look for evidence that this landingpad has already been processed.
377 bool LPadHasActionList = false;
378 BasicBlock *LPadBB = LPad->getParent();
379 for (Instruction &Inst : *LPadBB) {
380 if (auto *IntrinCall = dyn_cast<IntrinsicInst>(&Inst)) {
381 if (IntrinCall->getIntrinsicID() == Intrinsic::eh_actions) {
382 LPadHasActionList = true;
383 break;
384 }
385 }
386 // FIXME: This is here to help with the development of nested landing pad
387 // outlining. It should be removed when that is finished.
388 if (isa<UnreachableInst>(Inst)) {
389 LPadHasActionList = true;
390 break;
391 }
392 }
393
394 // If we've already outlined the handlers for this landingpad,
395 // there's nothing more to do here.
396 if (LPadHasActionList)
397 continue;
398
399 // If either of the values in the aggregate returned by the landing pad is
400 // extracted and stored to memory, promote the stored value to a register.
401 promoteLandingPadValues(LPad);
402
403 LandingPadActions Actions;
404 mapLandingPadBlocks(LPad, Actions);
405
406 HandlersOutlined |= !Actions.actions().empty();
407 for (ActionHandler *Action : Actions) {
408 if (Action->hasBeenProcessed())
409 continue;
410 BasicBlock *StartBB = Action->getStartBlock();
411
412 // SEH doesn't do any outlining for catches. Instead, pass the handler
413 // basic block addr to llvm.eh.actions and list the block as a return
414 // target.
415 if (isAsynchronousEHPersonality(Personality)) {
416 if (auto *CatchAction = dyn_cast<CatchHandler>(Action)) {
417 processSEHCatchHandler(CatchAction, StartBB);
418 continue;
419 }
420 }
421
422 outlineHandler(Action, &F, LPad, StartBB, FrameVarInfo);
423 }
424
425 // Replace the landing pad with a new llvm.eh.action based landing pad.
426 BasicBlock *NewLPadBB = BasicBlock::Create(Context, "lpad", &F, LPadBB);
427 assert(!isa<PHINode>(LPadBB->begin()));
428 auto *NewLPad = cast<LandingPadInst>(LPad->clone());
429 NewLPadBB->getInstList().push_back(NewLPad);
430 while (!pred_empty(LPadBB)) {
431 auto *pred = *pred_begin(LPadBB);
432 InvokeInst *Invoke = cast<InvokeInst>(pred->getTerminator());
433 Invoke->setUnwindDest(NewLPadBB);
434 }
435
436 // If anyone is still using the old landingpad value, just give them undef
437 // instead. The eh pointer and selector values are not real.
438 LPad->replaceAllUsesWith(UndefValue::get(LPad->getType()));
439
440 // Replace the mapping of any nested landing pad that previously mapped
441 // to this landing pad with a referenced to the cloned version.
442 for (auto &LPadPair : NestedLPtoOriginalLP) {
443 const LandingPadInst *OriginalLPad = LPadPair.second;
444 if (OriginalLPad == LPad) {
445 LPadPair.second = NewLPad;
446 }
447 }
448
449 // Replace uses of the old lpad in phis with this block and delete the old
450 // block.
451 LPadBB->replaceSuccessorsPhiUsesWith(NewLPadBB);
452 LPadBB->getTerminator()->eraseFromParent();
453 new UnreachableInst(LPadBB->getContext(), LPadBB);
454
455 // Add a call to describe the actions for this landing pad.
456 std::vector<Value *> ActionArgs;
457 for (ActionHandler *Action : Actions) {
458 // Action codes from docs are: 0 cleanup, 1 catch.
459 if (auto *CatchAction = dyn_cast<CatchHandler>(Action)) {
460 ActionArgs.push_back(ConstantInt::get(Int32Type, 1));
461 ActionArgs.push_back(CatchAction->getSelector());
462 // Find the frame escape index of the exception object alloca in the
463 // parent.
464 int FrameEscapeIdx = -1;
465 Value *EHObj = const_cast<Value *>(CatchAction->getExceptionVar());
466 if (EHObj && !isa<ConstantPointerNull>(EHObj)) {
467 auto I = FrameVarInfo.find(EHObj);
468 assert(I != FrameVarInfo.end() &&
469 "failed to map llvm.eh.begincatch var");
470 FrameEscapeIdx = std::distance(FrameVarInfo.begin(), I);
471 }
472 ActionArgs.push_back(ConstantInt::get(Int32Type, FrameEscapeIdx));
473 } else {
474 ActionArgs.push_back(ConstantInt::get(Int32Type, 0));
475 }
476 ActionArgs.push_back(Action->getHandlerBlockOrFunc());
477 }
478 CallInst *Recover =
479 CallInst::Create(ActionIntrin, ActionArgs, "recover", NewLPadBB);
480
481 // Add an indirect branch listing possible successors of the catch handlers.
482 IndirectBrInst *Branch = IndirectBrInst::Create(Recover, 0, NewLPadBB);
483 for (ActionHandler *Action : Actions) {
484 if (auto *CatchAction = dyn_cast<CatchHandler>(Action)) {
485 for (auto *Target : CatchAction->getReturnTargets()) {
486 Branch->addDestination(Target);
487 }
488 }
489 }
490 } // End for each landingpad
491
492 // If nothing got outlined, there is no more processing to be done.
493 if (!HandlersOutlined)
494 return false;
495
496 // Replace any nested landing pad stubs with the correct action handler.
497 // This must be done before we remove unreachable blocks because it
498 // cleans up references to outlined blocks that will be deleted.
499 for (auto &LPadPair : NestedLPtoOriginalLP)
500 completeNestedLandingPad(&F, LPadPair.first, LPadPair.second, FrameVarInfo);
501 NestedLPtoOriginalLP.clear();
502
503 F.addFnAttr("wineh-parent", F.getName());
504
505 // Delete any blocks that were only used by handlers that were outlined above.
506 removeUnreachableBlocks(F);
507
508 BasicBlock *Entry = &F.getEntryBlock();
509 IRBuilder<> Builder(F.getParent()->getContext());
510 Builder.SetInsertPoint(Entry->getFirstInsertionPt());
511
512 Function *FrameEscapeFn =
513 Intrinsic::getDeclaration(M, Intrinsic::frameescape);
514 Function *RecoverFrameFn =
515 Intrinsic::getDeclaration(M, Intrinsic::framerecover);
516
517 // Finally, replace all of the temporary allocas for frame variables used in
518 // the outlined handlers with calls to llvm.framerecover.
519 BasicBlock::iterator II = Entry->getFirstInsertionPt();
520 Instruction *AllocaInsertPt = II;
521 SmallVector<Value *, 8> AllocasToEscape;
522 for (auto &VarInfoEntry : FrameVarInfo) {
523 Value *ParentVal = VarInfoEntry.first;
524 TinyPtrVector<AllocaInst *> &Allocas = VarInfoEntry.second;
525
526 // If the mapped value isn't already an alloca, we need to spill it if it
527 // is a computed value or copy it if it is an argument.
528 AllocaInst *ParentAlloca = dyn_cast<AllocaInst>(ParentVal);
529 if (!ParentAlloca) {
530 if (auto *Arg = dyn_cast<Argument>(ParentVal)) {
531 // Lower this argument to a copy and then demote that to the stack.
532 // We can't just use the argument location because the handler needs
533 // it to be in the frame allocation block.
534 // Use 'select i8 true, %arg, undef' to simulate a 'no-op' instruction.
535 Value *TrueValue = ConstantInt::getTrue(Context);
536 Value *UndefValue = UndefValue::get(Arg->getType());
537 Instruction *SI =
538 SelectInst::Create(TrueValue, Arg, UndefValue,
539 Arg->getName() + ".tmp", AllocaInsertPt);
540 Arg->replaceAllUsesWith(SI);
541 // Reset the select operand, because it was clobbered by the RAUW above.
542 SI->setOperand(1, Arg);
543 ParentAlloca = DemoteRegToStack(*SI, true, SI);
544 } else if (auto *PN = dyn_cast<PHINode>(ParentVal)) {
545 ParentAlloca = DemotePHIToStack(PN, AllocaInsertPt);
546 } else {
547 Instruction *ParentInst = cast<Instruction>(ParentVal);
548 // FIXME: This is a work-around to temporarily handle the case where an
549 // instruction that is only used in handlers is not sunk.
550 // Without uses, DemoteRegToStack would just eliminate the value.
551 // This will fail if ParentInst is an invoke.
552 if (ParentInst->getNumUses() == 0) {
553 BasicBlock::iterator InsertPt = ParentInst;
554 ++InsertPt;
555 ParentAlloca =
556 new AllocaInst(ParentInst->getType(), nullptr,
557 ParentInst->getName() + ".reg2mem",
558 AllocaInsertPt);
559 new StoreInst(ParentInst, ParentAlloca, InsertPt);
560 } else {
561 ParentAlloca = DemoteRegToStack(*ParentInst, true, AllocaInsertPt);
562 }
563 }
564 }
565
566 // FIXME: We should try to sink unescaped allocas from the parent frame into
567 // the child frame. If the alloca is escaped, we have to use the lifetime
568 // markers to ensure that the alloca is only live within the child frame.
569
570 // Add this alloca to the list of things to escape.
571 AllocasToEscape.push_back(ParentAlloca);
572
573 // Next replace all outlined allocas that are mapped to it.
574 for (AllocaInst *TempAlloca : Allocas) {
575 if (TempAlloca == getCatchObjectSentinel())
576 continue; // Skip catch parameter sentinels.
577 Function *HandlerFn = TempAlloca->getParent()->getParent();
578 // FIXME: Sink this GEP into the blocks where it is used.
579 Builder.SetInsertPoint(TempAlloca);
580 Builder.SetCurrentDebugLocation(TempAlloca->getDebugLoc());
581 Value *RecoverArgs[] = {
582 Builder.CreateBitCast(&F, Int8PtrType, ""),
583 &(HandlerFn->getArgumentList().back()),
584 llvm::ConstantInt::get(Int32Type, AllocasToEscape.size() - 1)};
585 Value *RecoveredAlloca = Builder.CreateCall(RecoverFrameFn, RecoverArgs);
586 // Add a pointer bitcast if the alloca wasn't an i8.
587 if (RecoveredAlloca->getType() != TempAlloca->getType()) {
588 RecoveredAlloca->setName(Twine(TempAlloca->getName()) + ".i8");
589 RecoveredAlloca =
590 Builder.CreateBitCast(RecoveredAlloca, TempAlloca->getType());
591 }
592 TempAlloca->replaceAllUsesWith(RecoveredAlloca);
593 TempAlloca->removeFromParent();
594 RecoveredAlloca->takeName(TempAlloca);
595 delete TempAlloca;
596 }
597 } // End for each FrameVarInfo entry.
598
599 // Insert 'call void (...)* @llvm.frameescape(...)' at the end of the entry
600 // block.
601 Builder.SetInsertPoint(&F.getEntryBlock().back());
602 Builder.CreateCall(FrameEscapeFn, AllocasToEscape);
603
604 // Clean up the handler action maps we created for this function
605 DeleteContainerSeconds(CatchHandlerMap);
606 CatchHandlerMap.clear();
607 DeleteContainerSeconds(CleanupHandlerMap);
608 CleanupHandlerMap.clear();
609
610 return HandlersOutlined;
611 }
612
promoteLandingPadValues(LandingPadInst * LPad)613 void WinEHPrepare::promoteLandingPadValues(LandingPadInst *LPad) {
614 // If the return values of the landing pad instruction are extracted and
615 // stored to memory, we want to promote the store locations to reg values.
616 SmallVector<AllocaInst *, 2> EHAllocas;
617
618 // The landingpad instruction returns an aggregate value. Typically, its
619 // value will be passed to a pair of extract value instructions and the
620 // results of those extracts are often passed to store instructions.
621 // In unoptimized code the stored value will often be loaded and then stored
622 // again.
623 for (auto *U : LPad->users()) {
624 ExtractValueInst *Extract = dyn_cast<ExtractValueInst>(U);
625 if (!Extract)
626 continue;
627
628 for (auto *EU : Extract->users()) {
629 if (auto *Store = dyn_cast<StoreInst>(EU)) {
630 auto *AV = cast<AllocaInst>(Store->getPointerOperand());
631 EHAllocas.push_back(AV);
632 }
633 }
634 }
635
636 // We can't do this without a dominator tree.
637 assert(DT);
638
639 if (!EHAllocas.empty()) {
640 PromoteMemToReg(EHAllocas, *DT);
641 EHAllocas.clear();
642 }
643
644 // After promotion, some extracts may be trivially dead. Remove them.
645 SmallVector<Value *, 4> Users(LPad->user_begin(), LPad->user_end());
646 for (auto *U : Users)
647 RecursivelyDeleteTriviallyDeadInstructions(U);
648 }
649
completeNestedLandingPad(Function * ParentFn,LandingPadInst * OutlinedLPad,const LandingPadInst * OriginalLPad,FrameVarInfoMap & FrameVarInfo)650 void WinEHPrepare::completeNestedLandingPad(Function *ParentFn,
651 LandingPadInst *OutlinedLPad,
652 const LandingPadInst *OriginalLPad,
653 FrameVarInfoMap &FrameVarInfo) {
654 // Get the nested block and erase the unreachable instruction that was
655 // temporarily inserted as its terminator.
656 LLVMContext &Context = ParentFn->getContext();
657 BasicBlock *OutlinedBB = OutlinedLPad->getParent();
658 assert(isa<UnreachableInst>(OutlinedBB->getTerminator()));
659 OutlinedBB->getTerminator()->eraseFromParent();
660 // That should leave OutlinedLPad as the last instruction in its block.
661 assert(&OutlinedBB->back() == OutlinedLPad);
662
663 // The original landing pad will have already had its action intrinsic
664 // built by the outlining loop. We need to clone that into the outlined
665 // location. It may also be necessary to add references to the exception
666 // variables to the outlined handler in which this landing pad is nested
667 // and remap return instructions in the nested handlers that should return
668 // to an address in the outlined handler.
669 Function *OutlinedHandlerFn = OutlinedBB->getParent();
670 BasicBlock::const_iterator II = OriginalLPad;
671 ++II;
672 // The instruction after the landing pad should now be a call to eh.actions.
673 const Instruction *Recover = II;
674 assert(match(Recover, m_Intrinsic<Intrinsic::eh_actions>()));
675 IntrinsicInst *EHActions = cast<IntrinsicInst>(Recover->clone());
676
677 // Remap the exception variables into the outlined function.
678 WinEHFrameVariableMaterializer Materializer(OutlinedHandlerFn, FrameVarInfo);
679 SmallVector<BlockAddress *, 4> ActionTargets;
680 SmallVector<ActionHandler *, 4> ActionList;
681 parseEHActions(EHActions, ActionList);
682 for (auto *Action : ActionList) {
683 auto *Catch = dyn_cast<CatchHandler>(Action);
684 if (!Catch)
685 continue;
686 // The dyn_cast to function here selects C++ catch handlers and skips
687 // SEH catch handlers.
688 auto *Handler = dyn_cast<Function>(Catch->getHandlerBlockOrFunc());
689 if (!Handler)
690 continue;
691 // Visit all the return instructions, looking for places that return
692 // to a location within OutlinedHandlerFn.
693 for (BasicBlock &NestedHandlerBB : *Handler) {
694 auto *Ret = dyn_cast<ReturnInst>(NestedHandlerBB.getTerminator());
695 if (!Ret)
696 continue;
697
698 // Handler functions must always return a block address.
699 BlockAddress *BA = cast<BlockAddress>(Ret->getReturnValue());
700 // The original target will have been in the main parent function,
701 // but if it is the address of a block that has been outlined, it
702 // should be a block that was outlined into OutlinedHandlerFn.
703 assert(BA->getFunction() == ParentFn);
704
705 // Ignore targets that aren't part of OutlinedHandlerFn.
706 if (!LPadTargetBlocks.count(BA->getBasicBlock()))
707 continue;
708
709 // If the return value is the address ofF a block that we
710 // previously outlined into the parent handler function, replace
711 // the return instruction and add the mapped target to the list
712 // of possible return addresses.
713 BasicBlock *MappedBB = LPadTargetBlocks[BA->getBasicBlock()];
714 assert(MappedBB->getParent() == OutlinedHandlerFn);
715 BlockAddress *NewBA = BlockAddress::get(OutlinedHandlerFn, MappedBB);
716 Ret->eraseFromParent();
717 ReturnInst::Create(Context, NewBA, &NestedHandlerBB);
718 ActionTargets.push_back(NewBA);
719 }
720 }
721 DeleteContainerPointers(ActionList);
722 ActionList.clear();
723 OutlinedBB->getInstList().push_back(EHActions);
724
725 // Insert an indirect branch into the outlined landing pad BB.
726 IndirectBrInst *IBr = IndirectBrInst::Create(EHActions, 0, OutlinedBB);
727 // Add the previously collected action targets.
728 for (auto *Target : ActionTargets)
729 IBr->addDestination(Target->getBasicBlock());
730 }
731
732 // This function examines a block to determine whether the block ends with a
733 // conditional branch to a catch handler based on a selector comparison.
734 // This function is used both by the WinEHPrepare::findSelectorComparison() and
735 // WinEHCleanupDirector::handleTypeIdFor().
isSelectorDispatch(BasicBlock * BB,BasicBlock * & CatchHandler,Constant * & Selector,BasicBlock * & NextBB)736 static bool isSelectorDispatch(BasicBlock *BB, BasicBlock *&CatchHandler,
737 Constant *&Selector, BasicBlock *&NextBB) {
738 ICmpInst::Predicate Pred;
739 BasicBlock *TBB, *FBB;
740 Value *LHS, *RHS;
741
742 if (!match(BB->getTerminator(),
743 m_Br(m_ICmp(Pred, m_Value(LHS), m_Value(RHS)), TBB, FBB)))
744 return false;
745
746 if (!match(LHS,
747 m_Intrinsic<Intrinsic::eh_typeid_for>(m_Constant(Selector))) &&
748 !match(RHS, m_Intrinsic<Intrinsic::eh_typeid_for>(m_Constant(Selector))))
749 return false;
750
751 if (Pred == CmpInst::ICMP_EQ) {
752 CatchHandler = TBB;
753 NextBB = FBB;
754 return true;
755 }
756
757 if (Pred == CmpInst::ICMP_NE) {
758 CatchHandler = FBB;
759 NextBB = TBB;
760 return true;
761 }
762
763 return false;
764 }
765
createStubLandingPad(Function * Handler,Value * PersonalityFn)766 static BasicBlock *createStubLandingPad(Function *Handler,
767 Value *PersonalityFn) {
768 // FIXME: Finish this!
769 LLVMContext &Context = Handler->getContext();
770 BasicBlock *StubBB = BasicBlock::Create(Context, "stub");
771 Handler->getBasicBlockList().push_back(StubBB);
772 IRBuilder<> Builder(StubBB);
773 LandingPadInst *LPad = Builder.CreateLandingPad(
774 llvm::StructType::get(Type::getInt8PtrTy(Context),
775 Type::getInt32Ty(Context), nullptr),
776 PersonalityFn, 0);
777 LPad->setCleanup(true);
778 Builder.CreateUnreachable();
779 return StubBB;
780 }
781
782 // Cycles through the blocks in an outlined handler function looking for an
783 // invoke instruction and inserts an invoke of llvm.donothing with an empty
784 // landing pad if none is found. The code that generates the .xdata tables for
785 // the handler needs at least one landing pad to identify the parent function's
786 // personality.
addStubInvokeToHandlerIfNeeded(Function * Handler,Value * PersonalityFn)787 void WinEHPrepare::addStubInvokeToHandlerIfNeeded(Function *Handler,
788 Value *PersonalityFn) {
789 ReturnInst *Ret = nullptr;
790 for (BasicBlock &BB : *Handler) {
791 TerminatorInst *Terminator = BB.getTerminator();
792 // If we find an invoke, there is nothing to be done.
793 auto *II = dyn_cast<InvokeInst>(Terminator);
794 if (II)
795 return;
796 // If we've already recorded a return instruction, keep looking for invokes.
797 if (Ret)
798 continue;
799 // If we haven't recorded a return instruction yet, try this terminator.
800 Ret = dyn_cast<ReturnInst>(Terminator);
801 }
802
803 // If we got this far, the handler contains no invokes. We should have seen
804 // at least one return. We'll insert an invoke of llvm.donothing ahead of
805 // that return.
806 assert(Ret);
807 BasicBlock *OldRetBB = Ret->getParent();
808 BasicBlock *NewRetBB = SplitBlock(OldRetBB, Ret);
809 // SplitBlock adds an unconditional branch instruction at the end of the
810 // parent block. We want to replace that with an invoke call, so we can
811 // erase it now.
812 OldRetBB->getTerminator()->eraseFromParent();
813 BasicBlock *StubLandingPad = createStubLandingPad(Handler, PersonalityFn);
814 Function *F =
815 Intrinsic::getDeclaration(Handler->getParent(), Intrinsic::donothing);
816 InvokeInst::Create(F, NewRetBB, StubLandingPad, None, "", OldRetBB);
817 }
818
outlineHandler(ActionHandler * Action,Function * SrcFn,LandingPadInst * LPad,BasicBlock * StartBB,FrameVarInfoMap & VarInfo)819 bool WinEHPrepare::outlineHandler(ActionHandler *Action, Function *SrcFn,
820 LandingPadInst *LPad, BasicBlock *StartBB,
821 FrameVarInfoMap &VarInfo) {
822 Module *M = SrcFn->getParent();
823 LLVMContext &Context = M->getContext();
824
825 // Create a new function to receive the handler contents.
826 Type *Int8PtrType = Type::getInt8PtrTy(Context);
827 std::vector<Type *> ArgTys;
828 ArgTys.push_back(Int8PtrType);
829 ArgTys.push_back(Int8PtrType);
830 Function *Handler;
831 if (Action->getType() == Catch) {
832 FunctionType *FnType = FunctionType::get(Int8PtrType, ArgTys, false);
833 Handler = Function::Create(FnType, GlobalVariable::InternalLinkage,
834 SrcFn->getName() + ".catch", M);
835 } else {
836 FunctionType *FnType =
837 FunctionType::get(Type::getVoidTy(Context), ArgTys, false);
838 Handler = Function::Create(FnType, GlobalVariable::InternalLinkage,
839 SrcFn->getName() + ".cleanup", M);
840 }
841
842 Handler->addFnAttr("wineh-parent", SrcFn->getName());
843
844 // Generate a standard prolog to setup the frame recovery structure.
845 IRBuilder<> Builder(Context);
846 BasicBlock *Entry = BasicBlock::Create(Context, "entry");
847 Handler->getBasicBlockList().push_front(Entry);
848 Builder.SetInsertPoint(Entry);
849 Builder.SetCurrentDebugLocation(LPad->getDebugLoc());
850
851 std::unique_ptr<WinEHCloningDirectorBase> Director;
852
853 ValueToValueMapTy VMap;
854
855 LandingPadMap &LPadMap = LPadMaps[LPad];
856 if (!LPadMap.isInitialized())
857 LPadMap.mapLandingPad(LPad);
858 if (auto *CatchAction = dyn_cast<CatchHandler>(Action)) {
859 Constant *Sel = CatchAction->getSelector();
860 Director.reset(new WinEHCatchDirector(Handler, Sel, VarInfo, LPadMap,
861 NestedLPtoOriginalLP));
862 LPadMap.remapEHValues(VMap, UndefValue::get(Int8PtrType),
863 ConstantInt::get(Type::getInt32Ty(Context), 1));
864 } else {
865 Director.reset(new WinEHCleanupDirector(Handler, VarInfo, LPadMap));
866 LPadMap.remapEHValues(VMap, UndefValue::get(Int8PtrType),
867 UndefValue::get(Type::getInt32Ty(Context)));
868 }
869
870 SmallVector<ReturnInst *, 8> Returns;
871 ClonedCodeInfo OutlinedFunctionInfo;
872
873 // If the start block contains PHI nodes, we need to map them.
874 BasicBlock::iterator II = StartBB->begin();
875 while (auto *PN = dyn_cast<PHINode>(II)) {
876 bool Mapped = false;
877 // Look for PHI values that we have already mapped (such as the selector).
878 for (Value *Val : PN->incoming_values()) {
879 if (VMap.count(Val)) {
880 VMap[PN] = VMap[Val];
881 Mapped = true;
882 }
883 }
884 // If we didn't find a match for this value, map it as an undef.
885 if (!Mapped) {
886 VMap[PN] = UndefValue::get(PN->getType());
887 }
888 ++II;
889 }
890
891 // Skip over PHIs and, if applicable, landingpad instructions.
892 II = StartBB->getFirstInsertionPt();
893
894 CloneAndPruneIntoFromInst(Handler, SrcFn, II, VMap,
895 /*ModuleLevelChanges=*/false, Returns, "",
896 &OutlinedFunctionInfo, Director.get());
897
898 // Move all the instructions in the first cloned block into our entry block.
899 BasicBlock *FirstClonedBB = std::next(Function::iterator(Entry));
900 Entry->getInstList().splice(Entry->end(), FirstClonedBB->getInstList());
901 FirstClonedBB->eraseFromParent();
902
903 // Make sure we can identify the handler's personality later.
904 addStubInvokeToHandlerIfNeeded(Handler, LPad->getPersonalityFn());
905
906 if (auto *CatchAction = dyn_cast<CatchHandler>(Action)) {
907 WinEHCatchDirector *CatchDirector =
908 reinterpret_cast<WinEHCatchDirector *>(Director.get());
909 CatchAction->setExceptionVar(CatchDirector->getExceptionVar());
910 CatchAction->setReturnTargets(CatchDirector->getReturnTargets());
911
912 // Look for blocks that are not part of the landing pad that we just
913 // outlined but terminate with a call to llvm.eh.endcatch and a
914 // branch to a block that is in the handler we just outlined.
915 // These blocks will be part of a nested landing pad that intends to
916 // return to an address in this handler. This case is best handled
917 // after both landing pads have been outlined, so for now we'll just
918 // save the association of the blocks in LPadTargetBlocks. The
919 // return instructions which are created from these branches will be
920 // replaced after all landing pads have been outlined.
921 for (const auto MapEntry : VMap) {
922 // VMap maps all values and blocks that were just cloned, but dead
923 // blocks which were pruned will map to nullptr.
924 if (!isa<BasicBlock>(MapEntry.first) || MapEntry.second == nullptr)
925 continue;
926 const BasicBlock *MappedBB = cast<BasicBlock>(MapEntry.first);
927 for (auto *Pred : predecessors(const_cast<BasicBlock *>(MappedBB))) {
928 auto *Branch = dyn_cast<BranchInst>(Pred->getTerminator());
929 if (!Branch || !Branch->isUnconditional() || Pred->size() <= 1)
930 continue;
931 BasicBlock::iterator II = const_cast<BranchInst *>(Branch);
932 --II;
933 if (match(cast<Value>(II), m_Intrinsic<Intrinsic::eh_endcatch>())) {
934 // This would indicate that a nested landing pad wants to return
935 // to a block that is outlined into two different handlers.
936 assert(!LPadTargetBlocks.count(MappedBB));
937 LPadTargetBlocks[MappedBB] = cast<BasicBlock>(MapEntry.second);
938 }
939 }
940 }
941 } // End if (CatchAction)
942
943 Action->setHandlerBlockOrFunc(Handler);
944
945 return true;
946 }
947
948 /// This BB must end in a selector dispatch. All we need to do is pass the
949 /// handler block to llvm.eh.actions and list it as a possible indirectbr
950 /// target.
processSEHCatchHandler(CatchHandler * CatchAction,BasicBlock * StartBB)951 void WinEHPrepare::processSEHCatchHandler(CatchHandler *CatchAction,
952 BasicBlock *StartBB) {
953 BasicBlock *HandlerBB;
954 BasicBlock *NextBB;
955 Constant *Selector;
956 bool Res = isSelectorDispatch(StartBB, HandlerBB, Selector, NextBB);
957 if (Res) {
958 // If this was EH dispatch, this must be a conditional branch to the handler
959 // block.
960 // FIXME: Handle instructions in the dispatch block. Currently we drop them,
961 // leading to crashes if some optimization hoists stuff here.
962 assert(CatchAction->getSelector() && HandlerBB &&
963 "expected catch EH dispatch");
964 } else {
965 // This must be a catch-all. Split the block after the landingpad.
966 assert(CatchAction->getSelector()->isNullValue() && "expected catch-all");
967 HandlerBB =
968 StartBB->splitBasicBlock(StartBB->getFirstInsertionPt(), "catch.all");
969 }
970 CatchAction->setHandlerBlockOrFunc(BlockAddress::get(HandlerBB));
971 TinyPtrVector<BasicBlock *> Targets(HandlerBB);
972 CatchAction->setReturnTargets(Targets);
973 }
974
mapLandingPad(const LandingPadInst * LPad)975 void LandingPadMap::mapLandingPad(const LandingPadInst *LPad) {
976 // Each instance of this class should only ever be used to map a single
977 // landing pad.
978 assert(OriginLPad == nullptr || OriginLPad == LPad);
979
980 // If the landing pad has already been mapped, there's nothing more to do.
981 if (OriginLPad == LPad)
982 return;
983
984 OriginLPad = LPad;
985
986 // The landingpad instruction returns an aggregate value. Typically, its
987 // value will be passed to a pair of extract value instructions and the
988 // results of those extracts will have been promoted to reg values before
989 // this routine is called.
990 for (auto *U : LPad->users()) {
991 const ExtractValueInst *Extract = dyn_cast<ExtractValueInst>(U);
992 if (!Extract)
993 continue;
994 assert(Extract->getNumIndices() == 1 &&
995 "Unexpected operation: extracting both landing pad values");
996 unsigned int Idx = *(Extract->idx_begin());
997 assert((Idx == 0 || Idx == 1) &&
998 "Unexpected operation: extracting an unknown landing pad element");
999 if (Idx == 0) {
1000 ExtractedEHPtrs.push_back(Extract);
1001 } else if (Idx == 1) {
1002 ExtractedSelectors.push_back(Extract);
1003 }
1004 }
1005 }
1006
isOriginLandingPadBlock(const BasicBlock * BB) const1007 bool LandingPadMap::isOriginLandingPadBlock(const BasicBlock *BB) const {
1008 return BB->getLandingPadInst() == OriginLPad;
1009 }
1010
isLandingPadSpecificInst(const Instruction * Inst) const1011 bool LandingPadMap::isLandingPadSpecificInst(const Instruction *Inst) const {
1012 if (Inst == OriginLPad)
1013 return true;
1014 for (auto *Extract : ExtractedEHPtrs) {
1015 if (Inst == Extract)
1016 return true;
1017 }
1018 for (auto *Extract : ExtractedSelectors) {
1019 if (Inst == Extract)
1020 return true;
1021 }
1022 return false;
1023 }
1024
remapEHValues(ValueToValueMapTy & VMap,Value * EHPtrValue,Value * SelectorValue) const1025 void LandingPadMap::remapEHValues(ValueToValueMapTy &VMap, Value *EHPtrValue,
1026 Value *SelectorValue) const {
1027 // Remap all landing pad extract instructions to the specified values.
1028 for (auto *Extract : ExtractedEHPtrs)
1029 VMap[Extract] = EHPtrValue;
1030 for (auto *Extract : ExtractedSelectors)
1031 VMap[Extract] = SelectorValue;
1032 }
1033
handleInstruction(ValueToValueMapTy & VMap,const Instruction * Inst,BasicBlock * NewBB)1034 CloningDirector::CloningAction WinEHCloningDirectorBase::handleInstruction(
1035 ValueToValueMapTy &VMap, const Instruction *Inst, BasicBlock *NewBB) {
1036 // If this is one of the boilerplate landing pad instructions, skip it.
1037 // The instruction will have already been remapped in VMap.
1038 if (LPadMap.isLandingPadSpecificInst(Inst))
1039 return CloningDirector::SkipInstruction;
1040
1041 // Nested landing pads will be cloned as stubs, with just the
1042 // landingpad instruction and an unreachable instruction. When
1043 // all landingpads have been outlined, we'll replace this with the
1044 // llvm.eh.actions call and indirect branch created when the
1045 // landing pad was outlined.
1046 if (auto *LPad = dyn_cast<LandingPadInst>(Inst)) {
1047 return handleLandingPad(VMap, LPad, NewBB);
1048 }
1049
1050 if (auto *Invoke = dyn_cast<InvokeInst>(Inst))
1051 return handleInvoke(VMap, Invoke, NewBB);
1052
1053 if (auto *Resume = dyn_cast<ResumeInst>(Inst))
1054 return handleResume(VMap, Resume, NewBB);
1055
1056 if (match(Inst, m_Intrinsic<Intrinsic::eh_begincatch>()))
1057 return handleBeginCatch(VMap, Inst, NewBB);
1058 if (match(Inst, m_Intrinsic<Intrinsic::eh_endcatch>()))
1059 return handleEndCatch(VMap, Inst, NewBB);
1060 if (match(Inst, m_Intrinsic<Intrinsic::eh_typeid_for>()))
1061 return handleTypeIdFor(VMap, Inst, NewBB);
1062
1063 // Continue with the default cloning behavior.
1064 return CloningDirector::CloneInstruction;
1065 }
1066
handleLandingPad(ValueToValueMapTy & VMap,const LandingPadInst * LPad,BasicBlock * NewBB)1067 CloningDirector::CloningAction WinEHCatchDirector::handleLandingPad(
1068 ValueToValueMapTy &VMap, const LandingPadInst *LPad, BasicBlock *NewBB) {
1069 Instruction *NewInst = LPad->clone();
1070 if (LPad->hasName())
1071 NewInst->setName(LPad->getName());
1072 // Save this correlation for later processing.
1073 NestedLPtoOriginalLP[cast<LandingPadInst>(NewInst)] = LPad;
1074 VMap[LPad] = NewInst;
1075 BasicBlock::InstListType &InstList = NewBB->getInstList();
1076 InstList.push_back(NewInst);
1077 InstList.push_back(new UnreachableInst(NewBB->getContext()));
1078 return CloningDirector::StopCloningBB;
1079 }
1080
handleBeginCatch(ValueToValueMapTy & VMap,const Instruction * Inst,BasicBlock * NewBB)1081 CloningDirector::CloningAction WinEHCatchDirector::handleBeginCatch(
1082 ValueToValueMapTy &VMap, const Instruction *Inst, BasicBlock *NewBB) {
1083 // The argument to the call is some form of the first element of the
1084 // landingpad aggregate value, but that doesn't matter. It isn't used
1085 // here.
1086 // The second argument is an outparameter where the exception object will be
1087 // stored. Typically the exception object is a scalar, but it can be an
1088 // aggregate when catching by value.
1089 // FIXME: Leave something behind to indicate where the exception object lives
1090 // for this handler. Should it be part of llvm.eh.actions?
1091 assert(ExceptionObjectVar == nullptr && "Multiple calls to "
1092 "llvm.eh.begincatch found while "
1093 "outlining catch handler.");
1094 ExceptionObjectVar = Inst->getOperand(1)->stripPointerCasts();
1095 if (isa<ConstantPointerNull>(ExceptionObjectVar))
1096 return CloningDirector::SkipInstruction;
1097 assert(cast<AllocaInst>(ExceptionObjectVar)->isStaticAlloca() &&
1098 "catch parameter is not static alloca");
1099 Materializer.escapeCatchObject(ExceptionObjectVar);
1100 return CloningDirector::SkipInstruction;
1101 }
1102
1103 CloningDirector::CloningAction
handleEndCatch(ValueToValueMapTy & VMap,const Instruction * Inst,BasicBlock * NewBB)1104 WinEHCatchDirector::handleEndCatch(ValueToValueMapTy &VMap,
1105 const Instruction *Inst, BasicBlock *NewBB) {
1106 auto *IntrinCall = dyn_cast<IntrinsicInst>(Inst);
1107 // It might be interesting to track whether or not we are inside a catch
1108 // function, but that might make the algorithm more brittle than it needs
1109 // to be.
1110
1111 // The end catch call can occur in one of two places: either in a
1112 // landingpad block that is part of the catch handlers exception mechanism,
1113 // or at the end of the catch block. However, a catch-all handler may call
1114 // end catch from the original landing pad. If the call occurs in a nested
1115 // landing pad block, we must skip it and continue so that the landing pad
1116 // gets cloned.
1117 auto *ParentBB = IntrinCall->getParent();
1118 if (ParentBB->isLandingPad() && !LPadMap.isOriginLandingPadBlock(ParentBB))
1119 return CloningDirector::SkipInstruction;
1120
1121 // If an end catch occurs anywhere else we want to terminate the handler
1122 // with a return to the code that follows the endcatch call. If the
1123 // next instruction is not an unconditional branch, we need to split the
1124 // block to provide a clear target for the return instruction.
1125 BasicBlock *ContinueBB;
1126 auto Next = std::next(BasicBlock::const_iterator(IntrinCall));
1127 const BranchInst *Branch = dyn_cast<BranchInst>(Next);
1128 if (!Branch || !Branch->isUnconditional()) {
1129 // We're interrupting the cloning process at this location, so the
1130 // const_cast we're doing here will not cause a problem.
1131 ContinueBB = SplitBlock(const_cast<BasicBlock *>(ParentBB),
1132 const_cast<Instruction *>(cast<Instruction>(Next)));
1133 } else {
1134 ContinueBB = Branch->getSuccessor(0);
1135 }
1136
1137 ReturnInst::Create(NewBB->getContext(), BlockAddress::get(ContinueBB), NewBB);
1138 ReturnTargets.push_back(ContinueBB);
1139
1140 // We just added a terminator to the cloned block.
1141 // Tell the caller to stop processing the current basic block so that
1142 // the branch instruction will be skipped.
1143 return CloningDirector::StopCloningBB;
1144 }
1145
handleTypeIdFor(ValueToValueMapTy & VMap,const Instruction * Inst,BasicBlock * NewBB)1146 CloningDirector::CloningAction WinEHCatchDirector::handleTypeIdFor(
1147 ValueToValueMapTy &VMap, const Instruction *Inst, BasicBlock *NewBB) {
1148 auto *IntrinCall = dyn_cast<IntrinsicInst>(Inst);
1149 Value *Selector = IntrinCall->getArgOperand(0)->stripPointerCasts();
1150 // This causes a replacement that will collapse the landing pad CFG based
1151 // on the filter function we intend to match.
1152 if (Selector == CurrentSelector)
1153 VMap[Inst] = ConstantInt::get(SelectorIDType, 1);
1154 else
1155 VMap[Inst] = ConstantInt::get(SelectorIDType, 0);
1156 // Tell the caller not to clone this instruction.
1157 return CloningDirector::SkipInstruction;
1158 }
1159
1160 CloningDirector::CloningAction
handleInvoke(ValueToValueMapTy & VMap,const InvokeInst * Invoke,BasicBlock * NewBB)1161 WinEHCatchDirector::handleInvoke(ValueToValueMapTy &VMap,
1162 const InvokeInst *Invoke, BasicBlock *NewBB) {
1163 return CloningDirector::CloneInstruction;
1164 }
1165
1166 CloningDirector::CloningAction
handleResume(ValueToValueMapTy & VMap,const ResumeInst * Resume,BasicBlock * NewBB)1167 WinEHCatchDirector::handleResume(ValueToValueMapTy &VMap,
1168 const ResumeInst *Resume, BasicBlock *NewBB) {
1169 // Resume instructions shouldn't be reachable from catch handlers.
1170 // We still need to handle it, but it will be pruned.
1171 BasicBlock::InstListType &InstList = NewBB->getInstList();
1172 InstList.push_back(new UnreachableInst(NewBB->getContext()));
1173 return CloningDirector::StopCloningBB;
1174 }
1175
handleLandingPad(ValueToValueMapTy & VMap,const LandingPadInst * LPad,BasicBlock * NewBB)1176 CloningDirector::CloningAction WinEHCleanupDirector::handleLandingPad(
1177 ValueToValueMapTy &VMap, const LandingPadInst *LPad, BasicBlock *NewBB) {
1178 // The MS runtime will terminate the process if an exception occurs in a
1179 // cleanup handler, so we shouldn't encounter landing pads in the actual
1180 // cleanup code, but they may appear in catch blocks. Depending on where
1181 // we started cloning we may see one, but it will get dropped during dead
1182 // block pruning.
1183 Instruction *NewInst = new UnreachableInst(NewBB->getContext());
1184 VMap[LPad] = NewInst;
1185 BasicBlock::InstListType &InstList = NewBB->getInstList();
1186 InstList.push_back(NewInst);
1187 return CloningDirector::StopCloningBB;
1188 }
1189
handleBeginCatch(ValueToValueMapTy & VMap,const Instruction * Inst,BasicBlock * NewBB)1190 CloningDirector::CloningAction WinEHCleanupDirector::handleBeginCatch(
1191 ValueToValueMapTy &VMap, const Instruction *Inst, BasicBlock *NewBB) {
1192 // Catch blocks within cleanup handlers will always be unreachable.
1193 // We'll insert an unreachable instruction now, but it will be pruned
1194 // before the cloning process is complete.
1195 BasicBlock::InstListType &InstList = NewBB->getInstList();
1196 InstList.push_back(new UnreachableInst(NewBB->getContext()));
1197 return CloningDirector::StopCloningBB;
1198 }
1199
handleEndCatch(ValueToValueMapTy & VMap,const Instruction * Inst,BasicBlock * NewBB)1200 CloningDirector::CloningAction WinEHCleanupDirector::handleEndCatch(
1201 ValueToValueMapTy &VMap, const Instruction *Inst, BasicBlock *NewBB) {
1202 // Cleanup handlers nested within catch handlers may begin with a call to
1203 // eh.endcatch. We can just ignore that instruction.
1204 return CloningDirector::SkipInstruction;
1205 }
1206
handleTypeIdFor(ValueToValueMapTy & VMap,const Instruction * Inst,BasicBlock * NewBB)1207 CloningDirector::CloningAction WinEHCleanupDirector::handleTypeIdFor(
1208 ValueToValueMapTy &VMap, const Instruction *Inst, BasicBlock *NewBB) {
1209 // If we encounter a selector comparison while cloning a cleanup handler,
1210 // we want to stop cloning immediately. Anything after the dispatch
1211 // will be outlined into a different handler.
1212 BasicBlock *CatchHandler;
1213 Constant *Selector;
1214 BasicBlock *NextBB;
1215 if (isSelectorDispatch(const_cast<BasicBlock *>(Inst->getParent()),
1216 CatchHandler, Selector, NextBB)) {
1217 ReturnInst::Create(NewBB->getContext(), nullptr, NewBB);
1218 return CloningDirector::StopCloningBB;
1219 }
1220 // If eg.typeid.for is called for any other reason, it can be ignored.
1221 VMap[Inst] = ConstantInt::get(SelectorIDType, 0);
1222 return CloningDirector::SkipInstruction;
1223 }
1224
handleInvoke(ValueToValueMapTy & VMap,const InvokeInst * Invoke,BasicBlock * NewBB)1225 CloningDirector::CloningAction WinEHCleanupDirector::handleInvoke(
1226 ValueToValueMapTy &VMap, const InvokeInst *Invoke, BasicBlock *NewBB) {
1227 // All invokes in cleanup handlers can be replaced with calls.
1228 SmallVector<Value *, 16> CallArgs(Invoke->op_begin(), Invoke->op_end() - 3);
1229 // Insert a normal call instruction...
1230 CallInst *NewCall =
1231 CallInst::Create(const_cast<Value *>(Invoke->getCalledValue()), CallArgs,
1232 Invoke->getName(), NewBB);
1233 NewCall->setCallingConv(Invoke->getCallingConv());
1234 NewCall->setAttributes(Invoke->getAttributes());
1235 NewCall->setDebugLoc(Invoke->getDebugLoc());
1236 VMap[Invoke] = NewCall;
1237
1238 // Remap the operands.
1239 llvm::RemapInstruction(NewCall, VMap, RF_None, nullptr, &Materializer);
1240
1241 // Insert an unconditional branch to the normal destination.
1242 BranchInst::Create(Invoke->getNormalDest(), NewBB);
1243
1244 // The unwind destination won't be cloned into the new function, so
1245 // we don't need to clean up its phi nodes.
1246
1247 // We just added a terminator to the cloned block.
1248 // Tell the caller to stop processing the current basic block.
1249 return CloningDirector::CloneSuccessors;
1250 }
1251
handleResume(ValueToValueMapTy & VMap,const ResumeInst * Resume,BasicBlock * NewBB)1252 CloningDirector::CloningAction WinEHCleanupDirector::handleResume(
1253 ValueToValueMapTy &VMap, const ResumeInst *Resume, BasicBlock *NewBB) {
1254 ReturnInst::Create(NewBB->getContext(), nullptr, NewBB);
1255
1256 // We just added a terminator to the cloned block.
1257 // Tell the caller to stop processing the current basic block so that
1258 // the branch instruction will be skipped.
1259 return CloningDirector::StopCloningBB;
1260 }
1261
WinEHFrameVariableMaterializer(Function * OutlinedFn,FrameVarInfoMap & FrameVarInfo)1262 WinEHFrameVariableMaterializer::WinEHFrameVariableMaterializer(
1263 Function *OutlinedFn, FrameVarInfoMap &FrameVarInfo)
1264 : FrameVarInfo(FrameVarInfo), Builder(OutlinedFn->getContext()) {
1265 BasicBlock *EntryBB = &OutlinedFn->getEntryBlock();
1266 Builder.SetInsertPoint(EntryBB, EntryBB->getFirstInsertionPt());
1267 }
1268
materializeValueFor(Value * V)1269 Value *WinEHFrameVariableMaterializer::materializeValueFor(Value *V) {
1270 // If we're asked to materialize a value that is an instruction, we
1271 // temporarily create an alloca in the outlined function and add this
1272 // to the FrameVarInfo map. When all the outlining is complete, we'll
1273 // collect these into a structure, spilling non-alloca values in the
1274 // parent frame as necessary, and replace these temporary allocas with
1275 // GEPs referencing the frame allocation block.
1276
1277 // If the value is an alloca, the mapping is direct.
1278 if (auto *AV = dyn_cast<AllocaInst>(V)) {
1279 AllocaInst *NewAlloca = dyn_cast<AllocaInst>(AV->clone());
1280 Builder.Insert(NewAlloca, AV->getName());
1281 FrameVarInfo[AV].push_back(NewAlloca);
1282 return NewAlloca;
1283 }
1284
1285 // For other types of instructions or arguments, we need an alloca based on
1286 // the value's type and a load of the alloca. The alloca will be replaced
1287 // by a GEP, but the load will stay. In the parent function, the value will
1288 // be spilled to a location in the frame allocation block.
1289 if (isa<Instruction>(V) || isa<Argument>(V)) {
1290 AllocaInst *NewAlloca =
1291 Builder.CreateAlloca(V->getType(), nullptr, "eh.temp.alloca");
1292 FrameVarInfo[V].push_back(NewAlloca);
1293 LoadInst *NewLoad = Builder.CreateLoad(NewAlloca, V->getName() + ".reload");
1294 return NewLoad;
1295 }
1296
1297 // Don't materialize other values.
1298 return nullptr;
1299 }
1300
escapeCatchObject(Value * V)1301 void WinEHFrameVariableMaterializer::escapeCatchObject(Value *V) {
1302 // Catch parameter objects have to live in the parent frame. When we see a use
1303 // of a catch parameter, add a sentinel to the multimap to indicate that it's
1304 // used from another handler. This will prevent us from trying to sink the
1305 // alloca into the handler and ensure that the catch parameter is present in
1306 // the call to llvm.frameescape.
1307 FrameVarInfo[V].push_back(getCatchObjectSentinel());
1308 }
1309
1310 // This function maps the catch and cleanup handlers that are reachable from the
1311 // specified landing pad. The landing pad sequence will have this basic shape:
1312 //
1313 // <cleanup handler>
1314 // <selector comparison>
1315 // <catch handler>
1316 // <cleanup handler>
1317 // <selector comparison>
1318 // <catch handler>
1319 // <cleanup handler>
1320 // ...
1321 //
1322 // Any of the cleanup slots may be absent. The cleanup slots may be occupied by
1323 // any arbitrary control flow, but all paths through the cleanup code must
1324 // eventually reach the next selector comparison and no path can skip to a
1325 // different selector comparisons, though some paths may terminate abnormally.
1326 // Therefore, we will use a depth first search from the start of any given
1327 // cleanup block and stop searching when we find the next selector comparison.
1328 //
1329 // If the landingpad instruction does not have a catch clause, we will assume
1330 // that any instructions other than selector comparisons and catch handlers can
1331 // be ignored. In practice, these will only be the boilerplate instructions.
1332 //
1333 // The catch handlers may also have any control structure, but we are only
1334 // interested in the start of the catch handlers, so we don't need to actually
1335 // follow the flow of the catch handlers. The start of the catch handlers can
1336 // be located from the compare instructions, but they can be skipped in the
1337 // flow by following the contrary branch.
mapLandingPadBlocks(LandingPadInst * LPad,LandingPadActions & Actions)1338 void WinEHPrepare::mapLandingPadBlocks(LandingPadInst *LPad,
1339 LandingPadActions &Actions) {
1340 unsigned int NumClauses = LPad->getNumClauses();
1341 unsigned int HandlersFound = 0;
1342 BasicBlock *BB = LPad->getParent();
1343
1344 DEBUG(dbgs() << "Mapping landing pad: " << BB->getName() << "\n");
1345
1346 if (NumClauses == 0) {
1347 findCleanupHandlers(Actions, BB, nullptr);
1348 return;
1349 }
1350
1351 VisitedBlockSet VisitedBlocks;
1352
1353 while (HandlersFound != NumClauses) {
1354 BasicBlock *NextBB = nullptr;
1355
1356 // See if the clause we're looking for is a catch-all.
1357 // If so, the catch begins immediately.
1358 if (isa<ConstantPointerNull>(LPad->getClause(HandlersFound))) {
1359 // The catch all must occur last.
1360 assert(HandlersFound == NumClauses - 1);
1361
1362 // For C++ EH, check if there is any interesting cleanup code before we
1363 // begin the catch. This is important because cleanups cannot rethrow
1364 // exceptions but code called from catches can. For SEH, it isn't
1365 // important if some finally code before a catch-all is executed out of
1366 // line or after recovering from the exception.
1367 if (Personality == EHPersonality::MSVC_CXX)
1368 findCleanupHandlers(Actions, BB, BB);
1369
1370 // Add the catch handler to the action list.
1371 CatchHandler *Action =
1372 new CatchHandler(BB, LPad->getClause(HandlersFound), nullptr);
1373 CatchHandlerMap[BB] = Action;
1374 Actions.insertCatchHandler(Action);
1375 DEBUG(dbgs() << " Catch all handler at block " << BB->getName() << "\n");
1376 ++HandlersFound;
1377
1378 // Once we reach a catch-all, don't expect to hit a resume instruction.
1379 BB = nullptr;
1380 break;
1381 }
1382
1383 CatchHandler *CatchAction = findCatchHandler(BB, NextBB, VisitedBlocks);
1384 // See if there is any interesting code executed before the dispatch.
1385 findCleanupHandlers(Actions, BB, CatchAction->getStartBlock());
1386
1387 assert(CatchAction);
1388 ++HandlersFound;
1389
1390 // Add the catch handler to the action list.
1391 Actions.insertCatchHandler(CatchAction);
1392 DEBUG(dbgs() << " Found catch dispatch in block "
1393 << CatchAction->getStartBlock()->getName() << "\n");
1394
1395 // Move on to the block after the catch handler.
1396 BB = NextBB;
1397 }
1398
1399 // If we didn't wind up in a catch-all, see if there is any interesting code
1400 // executed before the resume.
1401 findCleanupHandlers(Actions, BB, BB);
1402
1403 // It's possible that some optimization moved code into a landingpad that
1404 // wasn't
1405 // previously being used for cleanup. If that happens, we need to execute
1406 // that
1407 // extra code from a cleanup handler.
1408 if (Actions.includesCleanup() && !LPad->isCleanup())
1409 LPad->setCleanup(true);
1410 }
1411
1412 // This function searches starting with the input block for the next
1413 // block that terminates with a branch whose condition is based on a selector
1414 // comparison. This may be the input block. See the mapLandingPadBlocks
1415 // comments for a discussion of control flow assumptions.
1416 //
findCatchHandler(BasicBlock * BB,BasicBlock * & NextBB,VisitedBlockSet & VisitedBlocks)1417 CatchHandler *WinEHPrepare::findCatchHandler(BasicBlock *BB,
1418 BasicBlock *&NextBB,
1419 VisitedBlockSet &VisitedBlocks) {
1420 // See if we've already found a catch handler use it.
1421 // Call count() first to avoid creating a null entry for blocks
1422 // we haven't seen before.
1423 if (CatchHandlerMap.count(BB) && CatchHandlerMap[BB] != nullptr) {
1424 CatchHandler *Action = cast<CatchHandler>(CatchHandlerMap[BB]);
1425 NextBB = Action->getNextBB();
1426 return Action;
1427 }
1428
1429 // VisitedBlocks applies only to the current search. We still
1430 // need to consider blocks that we've visited while mapping other
1431 // landing pads.
1432 VisitedBlocks.insert(BB);
1433
1434 BasicBlock *CatchBlock = nullptr;
1435 Constant *Selector = nullptr;
1436
1437 // If this is the first time we've visited this block from any landing pad
1438 // look to see if it is a selector dispatch block.
1439 if (!CatchHandlerMap.count(BB)) {
1440 if (isSelectorDispatch(BB, CatchBlock, Selector, NextBB)) {
1441 CatchHandler *Action = new CatchHandler(BB, Selector, NextBB);
1442 CatchHandlerMap[BB] = Action;
1443 return Action;
1444 }
1445 }
1446
1447 // Visit each successor, looking for the dispatch.
1448 // FIXME: We expect to find the dispatch quickly, so this will probably
1449 // work better as a breadth first search.
1450 for (BasicBlock *Succ : successors(BB)) {
1451 if (VisitedBlocks.count(Succ))
1452 continue;
1453
1454 CatchHandler *Action = findCatchHandler(Succ, NextBB, VisitedBlocks);
1455 if (Action)
1456 return Action;
1457 }
1458 return nullptr;
1459 }
1460
1461 // These are helper functions to combine repeated code from findCleanupHandlers.
createCleanupHandler(LandingPadActions & Actions,CleanupHandlerMapTy & CleanupHandlerMap,BasicBlock * BB)1462 static void createCleanupHandler(LandingPadActions &Actions,
1463 CleanupHandlerMapTy &CleanupHandlerMap,
1464 BasicBlock *BB) {
1465 CleanupHandler *Action = new CleanupHandler(BB);
1466 CleanupHandlerMap[BB] = Action;
1467 Actions.insertCleanupHandler(Action);
1468 DEBUG(dbgs() << " Found cleanup code in block "
1469 << Action->getStartBlock()->getName() << "\n");
1470 }
1471
isFrameAddressCall(Value * V)1472 static bool isFrameAddressCall(Value *V) {
1473 return match(V, m_Intrinsic<Intrinsic::frameaddress>(m_SpecificInt(0)));
1474 }
1475
matchOutlinedFinallyCall(BasicBlock * BB,Instruction * MaybeCall)1476 static CallSite matchOutlinedFinallyCall(BasicBlock *BB,
1477 Instruction *MaybeCall) {
1478 // Look for finally blocks that Clang has already outlined for us.
1479 // %fp = call i8* @llvm.frameaddress(i32 0)
1480 // call void @"fin$parent"(iN 1, i8* %fp)
1481 if (isFrameAddressCall(MaybeCall) && MaybeCall != BB->getTerminator())
1482 MaybeCall = MaybeCall->getNextNode();
1483 CallSite FinallyCall(MaybeCall);
1484 if (!FinallyCall || FinallyCall.arg_size() != 2)
1485 return CallSite();
1486 if (!match(FinallyCall.getArgument(0), m_SpecificInt(1)))
1487 return CallSite();
1488 if (!isFrameAddressCall(FinallyCall.getArgument(1)))
1489 return CallSite();
1490 return FinallyCall;
1491 }
1492
followSingleUnconditionalBranches(BasicBlock * BB)1493 static BasicBlock *followSingleUnconditionalBranches(BasicBlock *BB) {
1494 // Skip single ubr blocks.
1495 while (BB->getFirstNonPHIOrDbg() == BB->getTerminator()) {
1496 auto *Br = dyn_cast<BranchInst>(BB->getTerminator());
1497 if (Br && Br->isUnconditional())
1498 BB = Br->getSuccessor(0);
1499 else
1500 return BB;
1501 }
1502 return BB;
1503 }
1504
1505 // This function searches starting with the input block for the next block that
1506 // contains code that is not part of a catch handler and would not be eliminated
1507 // during handler outlining.
1508 //
findCleanupHandlers(LandingPadActions & Actions,BasicBlock * StartBB,BasicBlock * EndBB)1509 void WinEHPrepare::findCleanupHandlers(LandingPadActions &Actions,
1510 BasicBlock *StartBB, BasicBlock *EndBB) {
1511 // Here we will skip over the following:
1512 //
1513 // landing pad prolog:
1514 //
1515 // Unconditional branches
1516 //
1517 // Selector dispatch
1518 //
1519 // Resume pattern
1520 //
1521 // Anything else marks the start of an interesting block
1522
1523 BasicBlock *BB = StartBB;
1524 // Anything other than an unconditional branch will kick us out of this loop
1525 // one way or another.
1526 while (BB) {
1527 BB = followSingleUnconditionalBranches(BB);
1528 // If we've already scanned this block, don't scan it again. If it is
1529 // a cleanup block, there will be an action in the CleanupHandlerMap.
1530 // If we've scanned it and it is not a cleanup block, there will be a
1531 // nullptr in the CleanupHandlerMap. If we have not scanned it, there will
1532 // be no entry in the CleanupHandlerMap. We must call count() first to
1533 // avoid creating a null entry for blocks we haven't scanned.
1534 if (CleanupHandlerMap.count(BB)) {
1535 if (auto *Action = CleanupHandlerMap[BB]) {
1536 Actions.insertCleanupHandler(Action);
1537 DEBUG(dbgs() << " Found cleanup code in block "
1538 << Action->getStartBlock()->getName() << "\n");
1539 // FIXME: This cleanup might chain into another, and we need to discover
1540 // that.
1541 return;
1542 } else {
1543 // Here we handle the case where the cleanup handler map contains a
1544 // value for this block but the value is a nullptr. This means that
1545 // we have previously analyzed the block and determined that it did
1546 // not contain any cleanup code. Based on the earlier analysis, we
1547 // know the the block must end in either an unconditional branch, a
1548 // resume or a conditional branch that is predicated on a comparison
1549 // with a selector. Either the resume or the selector dispatch
1550 // would terminate the search for cleanup code, so the unconditional
1551 // branch is the only case for which we might need to continue
1552 // searching.
1553 BasicBlock *SuccBB = followSingleUnconditionalBranches(BB);
1554 if (SuccBB == BB || SuccBB == EndBB)
1555 return;
1556 BB = SuccBB;
1557 continue;
1558 }
1559 }
1560
1561 // Create an entry in the cleanup handler map for this block. Initially
1562 // we create an entry that says this isn't a cleanup block. If we find
1563 // cleanup code, the caller will replace this entry.
1564 CleanupHandlerMap[BB] = nullptr;
1565
1566 TerminatorInst *Terminator = BB->getTerminator();
1567
1568 // Landing pad blocks have extra instructions we need to accept.
1569 LandingPadMap *LPadMap = nullptr;
1570 if (BB->isLandingPad()) {
1571 LandingPadInst *LPad = BB->getLandingPadInst();
1572 LPadMap = &LPadMaps[LPad];
1573 if (!LPadMap->isInitialized())
1574 LPadMap->mapLandingPad(LPad);
1575 }
1576
1577 // Look for the bare resume pattern:
1578 // %lpad.val1 = insertvalue { i8*, i32 } undef, i8* %exn, 0
1579 // %lpad.val2 = insertvalue { i8*, i32 } %lpad.val1, i32 %sel, 1
1580 // resume { i8*, i32 } %lpad.val2
1581 if (auto *Resume = dyn_cast<ResumeInst>(Terminator)) {
1582 InsertValueInst *Insert1 = nullptr;
1583 InsertValueInst *Insert2 = nullptr;
1584 Value *ResumeVal = Resume->getOperand(0);
1585 // If the resume value isn't a phi or landingpad value, it should be a
1586 // series of insertions. Identify them so we can avoid them when scanning
1587 // for cleanups.
1588 if (!isa<PHINode>(ResumeVal) && !isa<LandingPadInst>(ResumeVal)) {
1589 Insert2 = dyn_cast<InsertValueInst>(ResumeVal);
1590 if (!Insert2)
1591 return createCleanupHandler(Actions, CleanupHandlerMap, BB);
1592 Insert1 = dyn_cast<InsertValueInst>(Insert2->getAggregateOperand());
1593 if (!Insert1)
1594 return createCleanupHandler(Actions, CleanupHandlerMap, BB);
1595 }
1596 for (BasicBlock::iterator II = BB->getFirstNonPHIOrDbg(), IE = BB->end();
1597 II != IE; ++II) {
1598 Instruction *Inst = II;
1599 if (LPadMap && LPadMap->isLandingPadSpecificInst(Inst))
1600 continue;
1601 if (Inst == Insert1 || Inst == Insert2 || Inst == Resume)
1602 continue;
1603 if (!Inst->hasOneUse() ||
1604 (Inst->user_back() != Insert1 && Inst->user_back() != Insert2)) {
1605 return createCleanupHandler(Actions, CleanupHandlerMap, BB);
1606 }
1607 }
1608 return;
1609 }
1610
1611 BranchInst *Branch = dyn_cast<BranchInst>(Terminator);
1612 if (Branch && Branch->isConditional()) {
1613 // Look for the selector dispatch.
1614 // %2 = call i32 @llvm.eh.typeid.for(i8* bitcast (i8** @_ZTIf to i8*))
1615 // %matches = icmp eq i32 %sel, %2
1616 // br i1 %matches, label %catch14, label %eh.resume
1617 CmpInst *Compare = dyn_cast<CmpInst>(Branch->getCondition());
1618 if (!Compare || !Compare->isEquality())
1619 return createCleanupHandler(Actions, CleanupHandlerMap, BB);
1620 for (BasicBlock::iterator II = BB->getFirstNonPHIOrDbg(), IE = BB->end();
1621 II != IE; ++II) {
1622 Instruction *Inst = II;
1623 if (LPadMap && LPadMap->isLandingPadSpecificInst(Inst))
1624 continue;
1625 if (Inst == Compare || Inst == Branch)
1626 continue;
1627 if (match(Inst, m_Intrinsic<Intrinsic::eh_typeid_for>()))
1628 continue;
1629 return createCleanupHandler(Actions, CleanupHandlerMap, BB);
1630 }
1631 // The selector dispatch block should always terminate our search.
1632 assert(BB == EndBB);
1633 return;
1634 }
1635
1636 if (isAsynchronousEHPersonality(Personality)) {
1637 // If this is a landingpad block, split the block at the first non-landing
1638 // pad instruction.
1639 Instruction *MaybeCall = BB->getFirstNonPHIOrDbg();
1640 if (LPadMap) {
1641 while (MaybeCall != BB->getTerminator() &&
1642 LPadMap->isLandingPadSpecificInst(MaybeCall))
1643 MaybeCall = MaybeCall->getNextNode();
1644 }
1645
1646 // Look for outlined finally calls.
1647 if (CallSite FinallyCall = matchOutlinedFinallyCall(BB, MaybeCall)) {
1648 Function *Fin = FinallyCall.getCalledFunction();
1649 assert(Fin && "outlined finally call should be direct");
1650 auto *Action = new CleanupHandler(BB);
1651 Action->setHandlerBlockOrFunc(Fin);
1652 Actions.insertCleanupHandler(Action);
1653 CleanupHandlerMap[BB] = Action;
1654 DEBUG(dbgs() << " Found frontend-outlined finally call to "
1655 << Fin->getName() << " in block "
1656 << Action->getStartBlock()->getName() << "\n");
1657
1658 // Split the block if there were more interesting instructions and look
1659 // for finally calls in the normal successor block.
1660 BasicBlock *SuccBB = BB;
1661 if (FinallyCall.getInstruction() != BB->getTerminator() &&
1662 FinallyCall.getInstruction()->getNextNode() != BB->getTerminator()) {
1663 SuccBB = BB->splitBasicBlock(FinallyCall.getInstruction()->getNextNode());
1664 } else {
1665 if (FinallyCall.isInvoke()) {
1666 SuccBB = cast<InvokeInst>(FinallyCall.getInstruction())->getNormalDest();
1667 } else {
1668 SuccBB = BB->getUniqueSuccessor();
1669 assert(SuccBB && "splitOutlinedFinallyCalls didn't insert a branch");
1670 }
1671 }
1672 BB = SuccBB;
1673 if (BB == EndBB)
1674 return;
1675 continue;
1676 }
1677 }
1678
1679 // Anything else is either a catch block or interesting cleanup code.
1680 for (BasicBlock::iterator II = BB->getFirstNonPHIOrDbg(), IE = BB->end();
1681 II != IE; ++II) {
1682 Instruction *Inst = II;
1683 if (LPadMap && LPadMap->isLandingPadSpecificInst(Inst))
1684 continue;
1685 // Unconditional branches fall through to this loop.
1686 if (Inst == Branch)
1687 continue;
1688 // If this is a catch block, there is no cleanup code to be found.
1689 if (match(Inst, m_Intrinsic<Intrinsic::eh_begincatch>()))
1690 return;
1691 // If this a nested landing pad, it may contain an endcatch call.
1692 if (match(Inst, m_Intrinsic<Intrinsic::eh_endcatch>()))
1693 return;
1694 // Anything else makes this interesting cleanup code.
1695 return createCleanupHandler(Actions, CleanupHandlerMap, BB);
1696 }
1697
1698 // Only unconditional branches in empty blocks should get this far.
1699 assert(Branch && Branch->isUnconditional());
1700 if (BB == EndBB)
1701 return;
1702 BB = Branch->getSuccessor(0);
1703 }
1704 }
1705
1706 // This is a public function, declared in WinEHFuncInfo.h and is also
1707 // referenced by WinEHNumbering in FunctionLoweringInfo.cpp.
parseEHActions(const IntrinsicInst * II,SmallVectorImpl<ActionHandler * > & Actions)1708 void llvm::parseEHActions(const IntrinsicInst *II,
1709 SmallVectorImpl<ActionHandler *> &Actions) {
1710 for (unsigned I = 0, E = II->getNumArgOperands(); I != E;) {
1711 uint64_t ActionKind =
1712 cast<ConstantInt>(II->getArgOperand(I))->getZExtValue();
1713 if (ActionKind == /*catch=*/1) {
1714 auto *Selector = cast<Constant>(II->getArgOperand(I + 1));
1715 ConstantInt *EHObjIndex = cast<ConstantInt>(II->getArgOperand(I + 2));
1716 int64_t EHObjIndexVal = EHObjIndex->getSExtValue();
1717 Constant *Handler = cast<Constant>(II->getArgOperand(I + 3));
1718 I += 4;
1719 auto *CH = new CatchHandler(/*BB=*/nullptr, Selector, /*NextBB=*/nullptr);
1720 CH->setHandlerBlockOrFunc(Handler);
1721 CH->setExceptionVarIndex(EHObjIndexVal);
1722 Actions.push_back(CH);
1723 } else if (ActionKind == 0) {
1724 Constant *Handler = cast<Constant>(II->getArgOperand(I + 1));
1725 I += 2;
1726 auto *CH = new CleanupHandler(/*BB=*/nullptr);
1727 CH->setHandlerBlockOrFunc(Handler);
1728 Actions.push_back(CH);
1729 } else {
1730 llvm_unreachable("Expected either a catch or cleanup handler!");
1731 }
1732 }
1733 std::reverse(Actions.begin(), Actions.end());
1734 }
1735