blob: e35b94f1d369c84bacf563e324a6c9725bc66788 [file] [log] [blame]
Andrew Kaylor1476e6d2015-02-24 20:49:35 +00001//===-- 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"
Andrew Kaylor6b67d422015-03-11 23:22:06 +000019#include "llvm/ADT/STLExtras.h"
Benjamin Kramera8d61b12015-03-23 18:57:17 +000020#include "llvm/ADT/SmallSet.h"
Andrew Kaylor1476e6d2015-02-24 20:49:35 +000021#include "llvm/ADT/TinyPtrVector.h"
22#include "llvm/Analysis/LibCallSemantics.h"
23#include "llvm/IR/Function.h"
24#include "llvm/IR/IRBuilder.h"
25#include "llvm/IR/Instructions.h"
26#include "llvm/IR/IntrinsicInst.h"
27#include "llvm/IR/Module.h"
28#include "llvm/IR/PatternMatch.h"
29#include "llvm/Pass.h"
Reid Kleckner0f9e27a2015-03-18 20:26:53 +000030#include "llvm/Support/CommandLine.h"
Andrew Kaylor6b67d422015-03-11 23:22:06 +000031#include "llvm/Support/Debug.h"
Benjamin Kramera8d61b12015-03-23 18:57:17 +000032#include "llvm/Support/raw_ostream.h"
Andrew Kaylor6b67d422015-03-11 23:22:06 +000033#include "llvm/Transforms/Utils/BasicBlockUtils.h"
Andrew Kaylor1476e6d2015-02-24 20:49:35 +000034#include "llvm/Transforms/Utils/Cloning.h"
35#include "llvm/Transforms/Utils/Local.h"
36#include <memory>
37
38using namespace llvm;
39using namespace llvm::PatternMatch;
40
41#define DEBUG_TYPE "winehprepare"
42
43namespace {
44
Andrew Kaylor1476e6d2015-02-24 20:49:35 +000045// This map is used to model frame variable usage during outlining, to
46// construct a structure type to hold the frame variables in a frame
47// allocation block, and to remap the frame variable allocas (including
48// spill locations as needed) to GEPs that get the variable from the
49// frame allocation structure.
Reid Klecknercfb9ce52015-03-05 18:26:34 +000050typedef MapVector<Value *, TinyPtrVector<AllocaInst *>> FrameVarInfoMap;
Andrew Kaylor1476e6d2015-02-24 20:49:35 +000051
Andrew Kaylor6b67d422015-03-11 23:22:06 +000052typedef SmallSet<BasicBlock *, 4> VisitedBlockSet;
53
54enum ActionType { Catch, Cleanup };
55
56class LandingPadActions;
57class ActionHandler;
58class CatchHandler;
59class CleanupHandler;
60class LandingPadMap;
61
62typedef DenseMap<const BasicBlock *, CatchHandler *> CatchHandlerMapTy;
63typedef DenseMap<const BasicBlock *, CleanupHandler *> CleanupHandlerMapTy;
64
Andrew Kaylor1476e6d2015-02-24 20:49:35 +000065class WinEHPrepare : public FunctionPass {
Andrew Kaylor1476e6d2015-02-24 20:49:35 +000066public:
67 static char ID; // Pass identification, replacement for typeid.
68 WinEHPrepare(const TargetMachine *TM = nullptr)
Reid Kleckner47c8e7a2015-03-12 00:36:20 +000069 : FunctionPass(ID) {}
Andrew Kaylor1476e6d2015-02-24 20:49:35 +000070
71 bool runOnFunction(Function &Fn) override;
72
73 bool doFinalization(Module &M) override;
74
75 void getAnalysisUsage(AnalysisUsage &AU) const override;
76
77 const char *getPassName() const override {
78 return "Windows exception handling preparation";
79 }
80
81private:
Reid Kleckner0f9e27a2015-03-18 20:26:53 +000082 bool prepareExceptionHandlers(Function &F,
83 SmallVectorImpl<LandingPadInst *> &LPads);
Andrew Kaylor6b67d422015-03-11 23:22:06 +000084 bool outlineHandler(ActionHandler *Action, Function *SrcFn,
85 LandingPadInst *LPad, BasicBlock *StartBB,
Reid Klecknercfb9ce52015-03-05 18:26:34 +000086 FrameVarInfoMap &VarInfo);
Andrew Kaylor6b67d422015-03-11 23:22:06 +000087
88 void mapLandingPadBlocks(LandingPadInst *LPad, LandingPadActions &Actions);
89 CatchHandler *findCatchHandler(BasicBlock *BB, BasicBlock *&NextBB,
90 VisitedBlockSet &VisitedBlocks);
91 CleanupHandler *findCleanupHandler(BasicBlock *StartBB, BasicBlock *EndBB);
92
Reid Kleckner0f9e27a2015-03-18 20:26:53 +000093 void processSEHCatchHandler(CatchHandler *Handler, BasicBlock *StartBB);
94
95 // All fields are reset by runOnFunction.
96 EHPersonality Personality;
Andrew Kaylor6b67d422015-03-11 23:22:06 +000097 CatchHandlerMapTy CatchHandlerMap;
98 CleanupHandlerMapTy CleanupHandlerMap;
99 DenseMap<const LandingPadInst *, LandingPadMap> LPadMaps;
Andrew Kaylor1476e6d2015-02-24 20:49:35 +0000100};
101
102class WinEHFrameVariableMaterializer : public ValueMaterializer {
103public:
104 WinEHFrameVariableMaterializer(Function *OutlinedFn,
105 FrameVarInfoMap &FrameVarInfo);
106 ~WinEHFrameVariableMaterializer() {}
107
108 virtual Value *materializeValueFor(Value *V) override;
109
110private:
111 FrameVarInfoMap &FrameVarInfo;
112 IRBuilder<> Builder;
113};
114
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000115class LandingPadMap {
116public:
117 LandingPadMap() : OriginLPad(nullptr) {}
118 void mapLandingPad(const LandingPadInst *LPad);
119
120 bool isInitialized() { return OriginLPad != nullptr; }
121
122 bool mapIfEHPtrLoad(const LoadInst *Load) {
123 return mapIfEHLoad(Load, EHPtrStores, EHPtrStoreAddrs);
124 }
125 bool mapIfSelectorLoad(const LoadInst *Load) {
126 return mapIfEHLoad(Load, SelectorStores, SelectorStoreAddrs);
127 }
128
Andrew Kaylorf7118ae2015-03-27 22:31:12 +0000129 bool isOriginLandingPadBlock(const BasicBlock *BB) const;
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000130 bool isLandingPadSpecificInst(const Instruction *Inst) const;
131
132 void remapSelector(ValueToValueMapTy &VMap, Value *MappedValue) const;
133
134private:
135 bool mapIfEHLoad(const LoadInst *Load,
136 SmallVectorImpl<const StoreInst *> &Stores,
137 SmallVectorImpl<const Value *> &StoreAddrs);
138
139 const LandingPadInst *OriginLPad;
140 // We will normally only see one of each of these instructions, but
141 // if more than one occurs for some reason we can handle that.
142 TinyPtrVector<const ExtractValueInst *> ExtractedEHPtrs;
143 TinyPtrVector<const ExtractValueInst *> ExtractedSelectors;
144
145 // In optimized code, there will typically be at most one instance of
146 // each of the following, but in unoptimized IR it is not uncommon
147 // for the values to be stored, loaded and then stored again. In that
148 // case we will create a second entry for each store and store address.
149 SmallVector<const StoreInst *, 2> EHPtrStores;
150 SmallVector<const StoreInst *, 2> SelectorStores;
151 SmallVector<const Value *, 2> EHPtrStoreAddrs;
152 SmallVector<const Value *, 2> SelectorStoreAddrs;
153};
154
Andrew Kaylorf0f5e462015-03-03 20:00:16 +0000155class WinEHCloningDirectorBase : public CloningDirector {
Andrew Kaylor1476e6d2015-02-24 20:49:35 +0000156public:
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000157 WinEHCloningDirectorBase(Function *HandlerFn,
158 FrameVarInfoMap &VarInfo,
159 LandingPadMap &LPadMap)
160 : Materializer(HandlerFn, VarInfo),
161 SelectorIDType(Type::getInt32Ty(HandlerFn->getContext())),
162 Int8PtrType(Type::getInt8PtrTy(HandlerFn->getContext())),
163 LPadMap(LPadMap) {}
Andrew Kaylor1476e6d2015-02-24 20:49:35 +0000164
165 CloningAction handleInstruction(ValueToValueMapTy &VMap,
166 const Instruction *Inst,
167 BasicBlock *NewBB) override;
168
Andrew Kaylorf0f5e462015-03-03 20:00:16 +0000169 virtual CloningAction handleBeginCatch(ValueToValueMapTy &VMap,
170 const Instruction *Inst,
171 BasicBlock *NewBB) = 0;
172 virtual CloningAction handleEndCatch(ValueToValueMapTy &VMap,
173 const Instruction *Inst,
174 BasicBlock *NewBB) = 0;
175 virtual CloningAction handleTypeIdFor(ValueToValueMapTy &VMap,
176 const Instruction *Inst,
177 BasicBlock *NewBB) = 0;
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000178 virtual CloningAction handleInvoke(ValueToValueMapTy &VMap,
179 const InvokeInst *Invoke,
180 BasicBlock *NewBB) = 0;
Andrew Kaylorf0f5e462015-03-03 20:00:16 +0000181 virtual CloningAction handleResume(ValueToValueMapTy &VMap,
182 const ResumeInst *Resume,
183 BasicBlock *NewBB) = 0;
184
Andrew Kaylor1476e6d2015-02-24 20:49:35 +0000185 ValueMaterializer *getValueMaterializer() override { return &Materializer; }
186
Andrew Kaylorf0f5e462015-03-03 20:00:16 +0000187protected:
Andrew Kaylor1476e6d2015-02-24 20:49:35 +0000188 WinEHFrameVariableMaterializer Materializer;
189 Type *SelectorIDType;
190 Type *Int8PtrType;
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000191 LandingPadMap &LPadMap;
Andrew Kaylor1476e6d2015-02-24 20:49:35 +0000192};
Andrew Kaylorf0f5e462015-03-03 20:00:16 +0000193
194class WinEHCatchDirector : public WinEHCloningDirectorBase {
195public:
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000196 WinEHCatchDirector(Function *CatchFn, Value *Selector,
197 FrameVarInfoMap &VarInfo, LandingPadMap &LPadMap)
198 : WinEHCloningDirectorBase(CatchFn, VarInfo, LPadMap),
199 CurrentSelector(Selector->stripPointerCasts()),
200 ExceptionObjectVar(nullptr) {}
Andrew Kaylorf0f5e462015-03-03 20:00:16 +0000201
202 CloningAction handleBeginCatch(ValueToValueMapTy &VMap,
203 const Instruction *Inst,
204 BasicBlock *NewBB) override;
205 CloningAction handleEndCatch(ValueToValueMapTy &VMap, const Instruction *Inst,
206 BasicBlock *NewBB) override;
207 CloningAction handleTypeIdFor(ValueToValueMapTy &VMap,
208 const Instruction *Inst,
209 BasicBlock *NewBB) override;
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000210 CloningAction handleInvoke(ValueToValueMapTy &VMap, const InvokeInst *Invoke,
211 BasicBlock *NewBB) override;
Andrew Kaylorf0f5e462015-03-03 20:00:16 +0000212 CloningAction handleResume(ValueToValueMapTy &VMap, const ResumeInst *Resume,
213 BasicBlock *NewBB) override;
214
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000215 const Value *getExceptionVar() { return ExceptionObjectVar; }
216 TinyPtrVector<BasicBlock *> &getReturnTargets() { return ReturnTargets; }
217
Andrew Kaylorf0f5e462015-03-03 20:00:16 +0000218private:
219 Value *CurrentSelector;
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000220
221 const Value *ExceptionObjectVar;
222 TinyPtrVector<BasicBlock *> ReturnTargets;
Andrew Kaylorf0f5e462015-03-03 20:00:16 +0000223};
224
225class WinEHCleanupDirector : public WinEHCloningDirectorBase {
226public:
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000227 WinEHCleanupDirector(Function *CleanupFn,
228 FrameVarInfoMap &VarInfo, LandingPadMap &LPadMap)
229 : WinEHCloningDirectorBase(CleanupFn, VarInfo, LPadMap) {}
Andrew Kaylorf0f5e462015-03-03 20:00:16 +0000230
231 CloningAction handleBeginCatch(ValueToValueMapTy &VMap,
232 const Instruction *Inst,
233 BasicBlock *NewBB) override;
234 CloningAction handleEndCatch(ValueToValueMapTy &VMap, const Instruction *Inst,
235 BasicBlock *NewBB) override;
236 CloningAction handleTypeIdFor(ValueToValueMapTy &VMap,
237 const Instruction *Inst,
238 BasicBlock *NewBB) override;
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000239 CloningAction handleInvoke(ValueToValueMapTy &VMap, const InvokeInst *Invoke,
240 BasicBlock *NewBB) override;
Andrew Kaylorf0f5e462015-03-03 20:00:16 +0000241 CloningAction handleResume(ValueToValueMapTy &VMap, const ResumeInst *Resume,
242 BasicBlock *NewBB) override;
243};
244
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000245class ActionHandler {
246public:
247 ActionHandler(BasicBlock *BB, ActionType Type)
Reid Kleckner0f9e27a2015-03-18 20:26:53 +0000248 : StartBB(BB), Type(Type), HandlerBlockOrFunc(nullptr) {}
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000249
250 ActionType getType() const { return Type; }
251 BasicBlock *getStartBlock() const { return StartBB; }
252
Reid Kleckner0f9e27a2015-03-18 20:26:53 +0000253 bool hasBeenProcessed() { return HandlerBlockOrFunc != nullptr; }
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000254
Reid Kleckner0f9e27a2015-03-18 20:26:53 +0000255 void setHandlerBlockOrFunc(Constant *F) { HandlerBlockOrFunc = F; }
256 Constant *getHandlerBlockOrFunc() { return HandlerBlockOrFunc; }
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000257
258private:
259 BasicBlock *StartBB;
260 ActionType Type;
Reid Kleckner0f9e27a2015-03-18 20:26:53 +0000261
262 // Can be either a BlockAddress or a Function depending on the EH personality.
263 Constant *HandlerBlockOrFunc;
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000264};
265
266class CatchHandler : public ActionHandler {
267public:
268 CatchHandler(BasicBlock *BB, Constant *Selector, BasicBlock *NextBB)
Reid Kleckner3c2ea312015-03-11 23:39:36 +0000269 : ActionHandler(BB, ActionType::Catch), Selector(Selector),
270 NextBB(NextBB), ExceptionObjectVar(nullptr) {}
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000271
272 // Method for support type inquiry through isa, cast, and dyn_cast:
273 static inline bool classof(const ActionHandler *H) {
274 return H->getType() == ActionType::Catch;
275 }
276
277 Constant *getSelector() const { return Selector; }
278 BasicBlock *getNextBB() const { return NextBB; }
279
280 const Value *getExceptionVar() { return ExceptionObjectVar; }
281 TinyPtrVector<BasicBlock *> &getReturnTargets() { return ReturnTargets; }
282
283 void setExceptionVar(const Value *Val) { ExceptionObjectVar = Val; }
284 void setReturnTargets(TinyPtrVector<BasicBlock *> &Targets) {
285 ReturnTargets = Targets;
286 }
287
288private:
289 Constant *Selector;
290 BasicBlock *NextBB;
291 const Value *ExceptionObjectVar;
292 TinyPtrVector<BasicBlock *> ReturnTargets;
293};
294
295class CleanupHandler : public ActionHandler {
296public:
297 CleanupHandler(BasicBlock *BB) : ActionHandler(BB, ActionType::Cleanup) {}
298
299 // Method for support type inquiry through isa, cast, and dyn_cast:
300 static inline bool classof(const ActionHandler *H) {
301 return H->getType() == ActionType::Cleanup;
302 }
303};
304
305class LandingPadActions {
306public:
307 LandingPadActions() : HasCleanupHandlers(false) {}
308
309 void insertCatchHandler(CatchHandler *Action) { Actions.push_back(Action); }
310 void insertCleanupHandler(CleanupHandler *Action) {
311 Actions.push_back(Action);
312 HasCleanupHandlers = true;
313 }
314
315 bool includesCleanup() const { return HasCleanupHandlers; }
316
317 SmallVectorImpl<ActionHandler *>::iterator begin() { return Actions.begin(); }
318 SmallVectorImpl<ActionHandler *>::iterator end() { return Actions.end(); }
319
320private:
321 // Note that this class does not own the ActionHandler objects in this vector.
322 // The ActionHandlers are owned by the CatchHandlerMap and CleanupHandlerMap
323 // in the WinEHPrepare class.
324 SmallVector<ActionHandler *, 4> Actions;
325 bool HasCleanupHandlers;
326};
327
Andrew Kaylor1476e6d2015-02-24 20:49:35 +0000328} // end anonymous namespace
329
330char WinEHPrepare::ID = 0;
Reid Kleckner47c8e7a2015-03-12 00:36:20 +0000331INITIALIZE_TM_PASS(WinEHPrepare, "winehprepare", "Prepare Windows exceptions",
332 false, false)
Andrew Kaylor1476e6d2015-02-24 20:49:35 +0000333
334FunctionPass *llvm::createWinEHPass(const TargetMachine *TM) {
335 return new WinEHPrepare(TM);
336}
337
Reid Kleckner0f9e27a2015-03-18 20:26:53 +0000338// FIXME: Remove this once the backend can handle the prepared IR.
339static cl::opt<bool>
340SEHPrepare("sehprepare", cl::Hidden,
341 cl::desc("Prepare functions with SEH personalities"));
342
Andrew Kaylor1476e6d2015-02-24 20:49:35 +0000343bool WinEHPrepare::runOnFunction(Function &Fn) {
344 SmallVector<LandingPadInst *, 4> LPads;
345 SmallVector<ResumeInst *, 4> Resumes;
346 for (BasicBlock &BB : Fn) {
347 if (auto *LP = BB.getLandingPadInst())
348 LPads.push_back(LP);
349 if (auto *Resume = dyn_cast<ResumeInst>(BB.getTerminator()))
350 Resumes.push_back(Resume);
351 }
352
353 // No need to prepare functions that lack landing pads.
354 if (LPads.empty())
355 return false;
356
357 // Classify the personality to see what kind of preparation we need.
Reid Kleckner0f9e27a2015-03-18 20:26:53 +0000358 Personality = classifyEHPersonality(LPads.back()->getPersonalityFn());
Andrew Kaylor1476e6d2015-02-24 20:49:35 +0000359
Reid Kleckner47c8e7a2015-03-12 00:36:20 +0000360 // Do nothing if this is not an MSVC personality.
Reid Kleckner0f9e27a2015-03-18 20:26:53 +0000361 if (!isMSVCEHPersonality(Personality))
Reid Kleckner47c8e7a2015-03-12 00:36:20 +0000362 return false;
Andrew Kaylor1476e6d2015-02-24 20:49:35 +0000363
Reid Kleckner0f9e27a2015-03-18 20:26:53 +0000364 if (isAsynchronousEHPersonality(Personality) && !SEHPrepare) {
365 // Replace all resume instructions with unreachable.
366 // FIXME: Remove this once the backend can handle the prepared IR.
367 for (ResumeInst *Resume : Resumes) {
368 IRBuilder<>(Resume).CreateUnreachable();
369 Resume->eraseFromParent();
370 }
Andrew Kaylor1476e6d2015-02-24 20:49:35 +0000371 return true;
Andrew Kaylor1476e6d2015-02-24 20:49:35 +0000372 }
373
Reid Kleckner0f9e27a2015-03-18 20:26:53 +0000374 // If there were any landing pads, prepareExceptionHandlers will make changes.
375 prepareExceptionHandlers(Fn, LPads);
Andrew Kaylor1476e6d2015-02-24 20:49:35 +0000376 return true;
377}
378
379bool WinEHPrepare::doFinalization(Module &M) {
Reid Kleckner47c8e7a2015-03-12 00:36:20 +0000380 return false;
Andrew Kaylor1476e6d2015-02-24 20:49:35 +0000381}
382
Reid Kleckner47c8e7a2015-03-12 00:36:20 +0000383void WinEHPrepare::getAnalysisUsage(AnalysisUsage &AU) const {}
Andrew Kaylor1476e6d2015-02-24 20:49:35 +0000384
Reid Kleckner0f9e27a2015-03-18 20:26:53 +0000385bool WinEHPrepare::prepareExceptionHandlers(
Andrew Kaylor1476e6d2015-02-24 20:49:35 +0000386 Function &F, SmallVectorImpl<LandingPadInst *> &LPads) {
387 // These containers are used to re-map frame variables that are used in
388 // outlined catch and cleanup handlers. They will be populated as the
389 // handlers are outlined.
390 FrameVarInfoMap FrameVarInfo;
Andrew Kaylor1476e6d2015-02-24 20:49:35 +0000391
392 bool HandlersOutlined = false;
393
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000394 Module *M = F.getParent();
395 LLVMContext &Context = M->getContext();
396
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000397 // Create a new function to receive the handler contents.
398 PointerType *Int8PtrType = Type::getInt8PtrTy(Context);
399 Type *Int32Type = Type::getInt32Ty(Context);
Reid Kleckner52b07792015-03-12 01:45:37 +0000400 Function *ActionIntrin = Intrinsic::getDeclaration(M, Intrinsic::eh_actions);
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000401
Andrew Kaylor1476e6d2015-02-24 20:49:35 +0000402 for (LandingPadInst *LPad : LPads) {
403 // Look for evidence that this landingpad has already been processed.
404 bool LPadHasActionList = false;
405 BasicBlock *LPadBB = LPad->getParent();
Reid Klecknerc759fe92015-03-19 22:31:02 +0000406 for (Instruction &Inst : *LPadBB) {
Reid Kleckner52b07792015-03-12 01:45:37 +0000407 if (auto *IntrinCall = dyn_cast<IntrinsicInst>(&Inst)) {
408 if (IntrinCall->getIntrinsicID() == Intrinsic::eh_actions) {
Andrew Kaylor1476e6d2015-02-24 20:49:35 +0000409 LPadHasActionList = true;
410 break;
411 }
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000412 }
413 // FIXME: This is here to help with the development of nested landing pad
414 // outlining. It should be removed when that is finished.
415 if (isa<UnreachableInst>(Inst)) {
416 LPadHasActionList = true;
417 break;
418 }
Andrew Kaylor1476e6d2015-02-24 20:49:35 +0000419 }
420
421 // If we've already outlined the handlers for this landingpad,
422 // there's nothing more to do here.
423 if (LPadHasActionList)
424 continue;
425
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000426 LandingPadActions Actions;
427 mapLandingPadBlocks(LPad, Actions);
Andrew Kaylorf0f5e462015-03-03 20:00:16 +0000428
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000429 for (ActionHandler *Action : Actions) {
Reid Kleckner0f9e27a2015-03-18 20:26:53 +0000430 if (Action->hasBeenProcessed())
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000431 continue;
432 BasicBlock *StartBB = Action->getStartBlock();
Reid Kleckner0f9e27a2015-03-18 20:26:53 +0000433
434 // SEH doesn't do any outlining for catches. Instead, pass the handler
435 // basic block addr to llvm.eh.actions and list the block as a return
436 // target.
437 if (isAsynchronousEHPersonality(Personality)) {
438 if (auto *CatchAction = dyn_cast<CatchHandler>(Action)) {
439 processSEHCatchHandler(CatchAction, StartBB);
440 HandlersOutlined = true;
441 continue;
442 }
443 }
444
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000445 if (outlineHandler(Action, &F, LPad, StartBB, FrameVarInfo)) {
Andrew Kaylorf0f5e462015-03-03 20:00:16 +0000446 HandlersOutlined = true;
Andrew Kaylorf0f5e462015-03-03 20:00:16 +0000447 }
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000448 } // End for each Action
449
450 // FIXME: We need a guard against partially outlined functions.
451 if (!HandlersOutlined)
452 continue;
453
454 // Replace the landing pad with a new llvm.eh.action based landing pad.
455 BasicBlock *NewLPadBB = BasicBlock::Create(Context, "lpad", &F, LPadBB);
456 assert(!isa<PHINode>(LPadBB->begin()));
457 Instruction *NewLPad = LPad->clone();
458 NewLPadBB->getInstList().push_back(NewLPad);
459 while (!pred_empty(LPadBB)) {
460 auto *pred = *pred_begin(LPadBB);
461 InvokeInst *Invoke = cast<InvokeInst>(pred->getTerminator());
462 Invoke->setUnwindDest(NewLPadBB);
463 }
464
Reid Kleckner0f9e27a2015-03-18 20:26:53 +0000465 // Replace uses of the old lpad in phis with this block and delete the old
466 // block.
467 LPadBB->replaceSuccessorsPhiUsesWith(NewLPadBB);
468 LPadBB->getTerminator()->eraseFromParent();
469 new UnreachableInst(LPadBB->getContext(), LPadBB);
470
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000471 // Add a call to describe the actions for this landing pad.
472 std::vector<Value *> ActionArgs;
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000473 for (ActionHandler *Action : Actions) {
Reid Klecknerc759fe92015-03-19 22:31:02 +0000474 // Action codes from docs are: 0 cleanup, 1 catch.
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000475 if (auto *CatchAction = dyn_cast<CatchHandler>(Action)) {
Reid Klecknerc759fe92015-03-19 22:31:02 +0000476 ActionArgs.push_back(ConstantInt::get(Int32Type, 1));
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000477 ActionArgs.push_back(CatchAction->getSelector());
478 Value *EHObj = const_cast<Value *>(CatchAction->getExceptionVar());
479 if (EHObj)
480 ActionArgs.push_back(EHObj);
481 else
482 ActionArgs.push_back(ConstantPointerNull::get(Int8PtrType));
483 } else {
Reid Klecknerc759fe92015-03-19 22:31:02 +0000484 ActionArgs.push_back(ConstantInt::get(Int32Type, 0));
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000485 }
Reid Klecknerc759fe92015-03-19 22:31:02 +0000486 ActionArgs.push_back(Action->getHandlerBlockOrFunc());
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000487 }
488 CallInst *Recover =
489 CallInst::Create(ActionIntrin, ActionArgs, "recover", NewLPadBB);
490
491 // Add an indirect branch listing possible successors of the catch handlers.
492 IndirectBrInst *Branch = IndirectBrInst::Create(Recover, 0, NewLPadBB);
493 for (ActionHandler *Action : Actions) {
494 if (auto *CatchAction = dyn_cast<CatchHandler>(Action)) {
495 for (auto *Target : CatchAction->getReturnTargets()) {
496 Branch->addDestination(Target);
497 }
498 }
Andrew Kaylorf0f5e462015-03-03 20:00:16 +0000499 }
500 } // End for each landingpad
Andrew Kaylor1476e6d2015-02-24 20:49:35 +0000501
502 // If nothing got outlined, there is no more processing to be done.
503 if (!HandlersOutlined)
504 return false;
505
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000506 // Delete any blocks that were only used by handlers that were outlined above.
507 removeUnreachableBlocks(F);
Andrew Kaylor1476e6d2015-02-24 20:49:35 +0000508
Andrew Kaylor1476e6d2015-02-24 20:49:35 +0000509 BasicBlock *Entry = &F.getEntryBlock();
Reid Klecknercfb9ce52015-03-05 18:26:34 +0000510 IRBuilder<> Builder(F.getParent()->getContext());
Andrew Kaylor1476e6d2015-02-24 20:49:35 +0000511 Builder.SetInsertPoint(Entry->getFirstInsertionPt());
Andrew Kaylor1476e6d2015-02-24 20:49:35 +0000512
Reid Klecknercfb9ce52015-03-05 18:26:34 +0000513 Function *FrameEscapeFn =
514 Intrinsic::getDeclaration(M, Intrinsic::frameescape);
515 Function *RecoverFrameFn =
516 Intrinsic::getDeclaration(M, Intrinsic::framerecover);
Andrew Kaylor1476e6d2015-02-24 20:49:35 +0000517
Andrew Kaylor1476e6d2015-02-24 20:49:35 +0000518 // Finally, replace all of the temporary allocas for frame variables used in
Reid Klecknercfb9ce52015-03-05 18:26:34 +0000519 // the outlined handlers with calls to llvm.framerecover.
520 BasicBlock::iterator II = Entry->getFirstInsertionPt();
Andrew Kaylor72029c62015-03-03 00:41:03 +0000521 Instruction *AllocaInsertPt = II;
Reid Klecknercfb9ce52015-03-05 18:26:34 +0000522 SmallVector<Value *, 8> AllocasToEscape;
Andrew Kaylor1476e6d2015-02-24 20:49:35 +0000523 for (auto &VarInfoEntry : FrameVarInfo) {
Andrew Kaylor72029c62015-03-03 00:41:03 +0000524 Value *ParentVal = VarInfoEntry.first;
Reid Klecknercfb9ce52015-03-05 18:26:34 +0000525 TinyPtrVector<AllocaInst *> &Allocas = VarInfoEntry.second;
Andrew Kaylor1476e6d2015-02-24 20:49:35 +0000526
Andrew Kaylor72029c62015-03-03 00:41:03 +0000527 // If the mapped value isn't already an alloca, we need to spill it if it
528 // is a computed value or copy it if it is an argument.
529 AllocaInst *ParentAlloca = dyn_cast<AllocaInst>(ParentVal);
530 if (!ParentAlloca) {
531 if (auto *Arg = dyn_cast<Argument>(ParentVal)) {
532 // Lower this argument to a copy and then demote that to the stack.
533 // We can't just use the argument location because the handler needs
534 // it to be in the frame allocation block.
535 // Use 'select i8 true, %arg, undef' to simulate a 'no-op' instruction.
536 Value *TrueValue = ConstantInt::getTrue(Context);
537 Value *UndefValue = UndefValue::get(Arg->getType());
538 Instruction *SI =
539 SelectInst::Create(TrueValue, Arg, UndefValue,
540 Arg->getName() + ".tmp", AllocaInsertPt);
541 Arg->replaceAllUsesWith(SI);
542 // Reset the select operand, because it was clobbered by the RAUW above.
543 SI->setOperand(1, Arg);
544 ParentAlloca = DemoteRegToStack(*SI, true, SI);
545 } else if (auto *PN = dyn_cast<PHINode>(ParentVal)) {
546 ParentAlloca = DemotePHIToStack(PN, AllocaInsertPt);
547 } else {
548 Instruction *ParentInst = cast<Instruction>(ParentVal);
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000549 // FIXME: This is a work-around to temporarily handle the case where an
550 // instruction that is only used in handlers is not sunk.
551 // Without uses, DemoteRegToStack would just eliminate the value.
552 // This will fail if ParentInst is an invoke.
553 if (ParentInst->getNumUses() == 0) {
554 BasicBlock::iterator InsertPt = ParentInst;
555 ++InsertPt;
556 ParentAlloca =
557 new AllocaInst(ParentInst->getType(), nullptr,
558 ParentInst->getName() + ".reg2mem", InsertPt);
559 new StoreInst(ParentInst, ParentAlloca, InsertPt);
560 } else {
561 ParentAlloca = DemoteRegToStack(*ParentInst, true, ParentInst);
562 }
Andrew Kaylor72029c62015-03-03 00:41:03 +0000563 }
564 }
565
Reid Klecknercfb9ce52015-03-05 18:26:34 +0000566 // If the parent alloca is no longer used and only one of the handlers used
567 // it, erase the parent and leave the copy in the outlined handler.
568 if (ParentAlloca->getNumUses() == 0 && Allocas.size() == 1) {
Andrew Kaylor1476e6d2015-02-24 20:49:35 +0000569 ParentAlloca->eraseFromParent();
Reid Klecknercfb9ce52015-03-05 18:26:34 +0000570 continue;
571 }
Andrew Kaylor1476e6d2015-02-24 20:49:35 +0000572
Reid Klecknercfb9ce52015-03-05 18:26:34 +0000573 // Add this alloca to the list of things to escape.
574 AllocasToEscape.push_back(ParentAlloca);
Andrew Kaylor1476e6d2015-02-24 20:49:35 +0000575
Reid Klecknercfb9ce52015-03-05 18:26:34 +0000576 // Next replace all outlined allocas that are mapped to it.
577 for (AllocaInst *TempAlloca : Allocas) {
578 Function *HandlerFn = TempAlloca->getParent()->getParent();
579 // FIXME: Sink this GEP into the blocks where it is used.
580 Builder.SetInsertPoint(TempAlloca);
581 Builder.SetCurrentDebugLocation(TempAlloca->getDebugLoc());
582 Value *RecoverArgs[] = {
583 Builder.CreateBitCast(&F, Int8PtrType, ""),
584 &(HandlerFn->getArgumentList().back()),
585 llvm::ConstantInt::get(Int32Type, AllocasToEscape.size() - 1)};
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000586 Value *RecoveredAlloca = Builder.CreateCall(RecoverFrameFn, RecoverArgs);
Reid Klecknercfb9ce52015-03-05 18:26:34 +0000587 // Add a pointer bitcast if the alloca wasn't an i8.
588 if (RecoveredAlloca->getType() != TempAlloca->getType()) {
589 RecoveredAlloca->setName(Twine(TempAlloca->getName()) + ".i8");
590 RecoveredAlloca =
591 Builder.CreateBitCast(RecoveredAlloca, TempAlloca->getType());
Andrew Kaylor1476e6d2015-02-24 20:49:35 +0000592 }
Reid Klecknercfb9ce52015-03-05 18:26:34 +0000593 TempAlloca->replaceAllUsesWith(RecoveredAlloca);
594 TempAlloca->removeFromParent();
595 RecoveredAlloca->takeName(TempAlloca);
596 delete TempAlloca;
597 }
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000598 } // End for each FrameVarInfo entry.
Andrew Kaylor1476e6d2015-02-24 20:49:35 +0000599
Reid Klecknercfb9ce52015-03-05 18:26:34 +0000600 // Insert 'call void (...)* @llvm.frameescape(...)' at the end of the entry
601 // block.
602 Builder.SetInsertPoint(&F.getEntryBlock().back());
603 Builder.CreateCall(FrameEscapeFn, AllocasToEscape);
604
Reid Kleckner7e9546b2015-03-25 20:10:36 +0000605 // Insert an alloca for the EH state in the entry block. On x86, we will also
606 // insert stores to update the EH state, but on other ISAs, the runtime does
607 // it for us.
608 // FIXME: This record is different on x86.
609 Type *UnwindHelpTy = Type::getInt64Ty(Context);
610 AllocaInst *UnwindHelp =
611 new AllocaInst(UnwindHelpTy, "unwindhelp", &F.getEntryBlock().front());
612 Builder.CreateStore(llvm::ConstantInt::get(UnwindHelpTy, -2), UnwindHelp);
613 Function *UnwindHelpFn =
614 Intrinsic::getDeclaration(M, Intrinsic::eh_unwindhelp);
615 Builder.CreateCall(UnwindHelpFn,
616 Builder.CreateBitCast(UnwindHelp, Int8PtrType));
617
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000618 // Clean up the handler action maps we created for this function
619 DeleteContainerSeconds(CatchHandlerMap);
620 CatchHandlerMap.clear();
621 DeleteContainerSeconds(CleanupHandlerMap);
622 CleanupHandlerMap.clear();
623
Andrew Kaylor1476e6d2015-02-24 20:49:35 +0000624 return HandlersOutlined;
625}
626
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000627// This function examines a block to determine whether the block ends with a
628// conditional branch to a catch handler based on a selector comparison.
629// This function is used both by the WinEHPrepare::findSelectorComparison() and
630// WinEHCleanupDirector::handleTypeIdFor().
631static bool isSelectorDispatch(BasicBlock *BB, BasicBlock *&CatchHandler,
632 Constant *&Selector, BasicBlock *&NextBB) {
633 ICmpInst::Predicate Pred;
634 BasicBlock *TBB, *FBB;
635 Value *LHS, *RHS;
636
637 if (!match(BB->getTerminator(),
638 m_Br(m_ICmp(Pred, m_Value(LHS), m_Value(RHS)), TBB, FBB)))
639 return false;
640
641 if (!match(LHS,
642 m_Intrinsic<Intrinsic::eh_typeid_for>(m_Constant(Selector))) &&
643 !match(RHS, m_Intrinsic<Intrinsic::eh_typeid_for>(m_Constant(Selector))))
644 return false;
645
646 if (Pred == CmpInst::ICMP_EQ) {
647 CatchHandler = TBB;
648 NextBB = FBB;
649 return true;
650 }
651
652 if (Pred == CmpInst::ICMP_NE) {
653 CatchHandler = FBB;
654 NextBB = TBB;
655 return true;
656 }
657
658 return false;
659}
660
661bool WinEHPrepare::outlineHandler(ActionHandler *Action, Function *SrcFn,
662 LandingPadInst *LPad, BasicBlock *StartBB,
Andrew Kaylorf0f5e462015-03-03 20:00:16 +0000663 FrameVarInfoMap &VarInfo) {
Andrew Kaylor1476e6d2015-02-24 20:49:35 +0000664 Module *M = SrcFn->getParent();
665 LLVMContext &Context = M->getContext();
666
667 // Create a new function to receive the handler contents.
668 Type *Int8PtrType = Type::getInt8PtrTy(Context);
669 std::vector<Type *> ArgTys;
670 ArgTys.push_back(Int8PtrType);
671 ArgTys.push_back(Int8PtrType);
Andrew Kaylorf0f5e462015-03-03 20:00:16 +0000672 Function *Handler;
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000673 if (Action->getType() == Catch) {
Andrew Kaylorf0f5e462015-03-03 20:00:16 +0000674 FunctionType *FnType = FunctionType::get(Int8PtrType, ArgTys, false);
675 Handler = Function::Create(FnType, GlobalVariable::InternalLinkage,
676 SrcFn->getName() + ".catch", M);
677 } else {
678 FunctionType *FnType =
679 FunctionType::get(Type::getVoidTy(Context), ArgTys, false);
680 Handler = Function::Create(FnType, GlobalVariable::InternalLinkage,
681 SrcFn->getName() + ".cleanup", M);
682 }
Andrew Kaylor1476e6d2015-02-24 20:49:35 +0000683
684 // Generate a standard prolog to setup the frame recovery structure.
685 IRBuilder<> Builder(Context);
Andrew Kaylorf0f5e462015-03-03 20:00:16 +0000686 BasicBlock *Entry = BasicBlock::Create(Context, "entry");
687 Handler->getBasicBlockList().push_front(Entry);
Andrew Kaylor1476e6d2015-02-24 20:49:35 +0000688 Builder.SetInsertPoint(Entry);
689 Builder.SetCurrentDebugLocation(LPad->getDebugLoc());
690
Andrew Kaylorf0f5e462015-03-03 20:00:16 +0000691 std::unique_ptr<WinEHCloningDirectorBase> Director;
Andrew Kaylor1476e6d2015-02-24 20:49:35 +0000692
Andrew Kaylor1476e6d2015-02-24 20:49:35 +0000693 ValueToValueMapTy VMap;
694
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000695 LandingPadMap &LPadMap = LPadMaps[LPad];
696 if (!LPadMap.isInitialized())
697 LPadMap.mapLandingPad(LPad);
Reid Kleckner0f9e27a2015-03-18 20:26:53 +0000698 if (auto *CatchAction = dyn_cast<CatchHandler>(Action)) {
David Majnemerb919dd62015-03-27 04:17:07 +0000699 // Insert an alloca for the object which holds the address of the parent's
700 // frame pointer. The stack offset of this object needs to be encoded in
701 // xdata.
702 AllocaInst *ParentFrame = new AllocaInst(Int8PtrType, "parentframe", Entry);
703 Builder.CreateStore(&Handler->getArgumentList().back(), ParentFrame,
704 /*isStore=*/true);
705 Function *ParentFrameFn =
706 Intrinsic::getDeclaration(M, Intrinsic::eh_parentframe);
707 Builder.CreateCall(ParentFrameFn, ParentFrame);
708
Reid Kleckner0f9e27a2015-03-18 20:26:53 +0000709 Constant *Sel = CatchAction->getSelector();
710 Director.reset(new WinEHCatchDirector(Handler, Sel, VarInfo, LPadMap));
711 LPadMap.remapSelector(VMap, ConstantInt::get(Type::getInt32Ty(Context), 1));
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000712 } else {
713 Director.reset(new WinEHCleanupDirector(Handler, VarInfo, LPadMap));
714 }
Andrew Kaylor1476e6d2015-02-24 20:49:35 +0000715
Andrew Kaylor1476e6d2015-02-24 20:49:35 +0000716 SmallVector<ReturnInst *, 8> Returns;
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000717 ClonedCodeInfo OutlinedFunctionInfo;
Andrew Kaylor1476e6d2015-02-24 20:49:35 +0000718
Andrew Kaylor3170e562015-03-20 21:42:54 +0000719 // If the start block contains PHI nodes, we need to map them.
720 BasicBlock::iterator II = StartBB->begin();
721 while (auto *PN = dyn_cast<PHINode>(II)) {
722 bool Mapped = false;
723 // Look for PHI values that we have already mapped (such as the selector).
724 for (Value *Val : PN->incoming_values()) {
725 if (VMap.count(Val)) {
726 VMap[PN] = VMap[Val];
727 Mapped = true;
728 }
729 }
730 // If we didn't find a match for this value, map it as an undef.
731 if (!Mapped) {
732 VMap[PN] = UndefValue::get(PN->getType());
733 }
734 ++II;
735 }
736
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000737 // Skip over PHIs and, if applicable, landingpad instructions.
Andrew Kaylor3170e562015-03-20 21:42:54 +0000738 II = StartBB->getFirstInsertionPt();
Andrew Kaylor1476e6d2015-02-24 20:49:35 +0000739
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000740 CloneAndPruneIntoFromInst(Handler, SrcFn, II, VMap,
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000741 /*ModuleLevelChanges=*/false, Returns, "",
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000742 &OutlinedFunctionInfo, Director.get());
Andrew Kaylor1476e6d2015-02-24 20:49:35 +0000743
744 // Move all the instructions in the first cloned block into our entry block.
745 BasicBlock *FirstClonedBB = std::next(Function::iterator(Entry));
746 Entry->getInstList().splice(Entry->end(), FirstClonedBB->getInstList());
747 FirstClonedBB->eraseFromParent();
748
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000749 if (auto *CatchAction = dyn_cast<CatchHandler>(Action)) {
750 WinEHCatchDirector *CatchDirector =
751 reinterpret_cast<WinEHCatchDirector *>(Director.get());
752 CatchAction->setExceptionVar(CatchDirector->getExceptionVar());
753 CatchAction->setReturnTargets(CatchDirector->getReturnTargets());
754 }
755
Reid Kleckner0f9e27a2015-03-18 20:26:53 +0000756 Action->setHandlerBlockOrFunc(Handler);
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000757
Andrew Kaylor1476e6d2015-02-24 20:49:35 +0000758 return true;
759}
760
Reid Kleckner0f9e27a2015-03-18 20:26:53 +0000761/// This BB must end in a selector dispatch. All we need to do is pass the
762/// handler block to llvm.eh.actions and list it as a possible indirectbr
763/// target.
764void WinEHPrepare::processSEHCatchHandler(CatchHandler *CatchAction,
765 BasicBlock *StartBB) {
766 BasicBlock *HandlerBB;
767 BasicBlock *NextBB;
768 Constant *Selector;
769 bool Res = isSelectorDispatch(StartBB, HandlerBB, Selector, NextBB);
770 if (Res) {
771 // If this was EH dispatch, this must be a conditional branch to the handler
772 // block.
773 // FIXME: Handle instructions in the dispatch block. Currently we drop them,
774 // leading to crashes if some optimization hoists stuff here.
775 assert(CatchAction->getSelector() && HandlerBB &&
776 "expected catch EH dispatch");
777 } else {
778 // This must be a catch-all. Split the block after the landingpad.
779 assert(CatchAction->getSelector()->isNullValue() && "expected catch-all");
780 HandlerBB =
781 StartBB->splitBasicBlock(StartBB->getFirstInsertionPt(), "catch.all");
782 }
783 CatchAction->setHandlerBlockOrFunc(BlockAddress::get(HandlerBB));
784 TinyPtrVector<BasicBlock *> Targets(HandlerBB);
785 CatchAction->setReturnTargets(Targets);
786}
787
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000788void LandingPadMap::mapLandingPad(const LandingPadInst *LPad) {
789 // Each instance of this class should only ever be used to map a single
790 // landing pad.
791 assert(OriginLPad == nullptr || OriginLPad == LPad);
792
793 // If the landing pad has already been mapped, there's nothing more to do.
794 if (OriginLPad == LPad)
795 return;
796
797 OriginLPad = LPad;
798
799 // The landingpad instruction returns an aggregate value. Typically, its
800 // value will be passed to a pair of extract value instructions and the
801 // results of those extracts are often passed to store instructions.
802 // In unoptimized code the stored value will often be loaded and then stored
803 // again.
804 for (auto *U : LPad->users()) {
805 const ExtractValueInst *Extract = dyn_cast<ExtractValueInst>(U);
806 if (!Extract)
807 continue;
808 assert(Extract->getNumIndices() == 1 &&
809 "Unexpected operation: extracting both landing pad values");
810 unsigned int Idx = *(Extract->idx_begin());
811 assert((Idx == 0 || Idx == 1) &&
812 "Unexpected operation: extracting an unknown landing pad element");
813 if (Idx == 0) {
814 // Element 0 doesn't directly corresponds to anything in the WinEH
815 // scheme.
816 // It will be stored to a memory location, then later loaded and finally
817 // the loaded value will be used as the argument to an
818 // llvm.eh.begincatch
819 // call. We're tracking it here so that we can skip the store and load.
820 ExtractedEHPtrs.push_back(Extract);
821 } else if (Idx == 1) {
822 // Element 1 corresponds to the filter selector. We'll map it to 1 for
823 // matching purposes, but it will also probably be stored to memory and
824 // reloaded, so we need to track the instuction so that we can map the
825 // loaded value too.
826 ExtractedSelectors.push_back(Extract);
827 }
828
829 // Look for stores of the extracted values.
830 for (auto *EU : Extract->users()) {
831 if (auto *Store = dyn_cast<StoreInst>(EU)) {
832 if (Idx == 1) {
833 SelectorStores.push_back(Store);
834 SelectorStoreAddrs.push_back(Store->getPointerOperand());
835 } else {
836 EHPtrStores.push_back(Store);
837 EHPtrStoreAddrs.push_back(Store->getPointerOperand());
838 }
839 }
840 }
841 }
842}
843
Andrew Kaylorf7118ae2015-03-27 22:31:12 +0000844bool LandingPadMap::isOriginLandingPadBlock(const BasicBlock *BB) const {
845 return BB->getLandingPadInst() == OriginLPad;
846}
847
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000848bool LandingPadMap::isLandingPadSpecificInst(const Instruction *Inst) const {
849 if (Inst == OriginLPad)
850 return true;
851 for (auto *Extract : ExtractedEHPtrs) {
852 if (Inst == Extract)
853 return true;
854 }
855 for (auto *Extract : ExtractedSelectors) {
856 if (Inst == Extract)
857 return true;
858 }
859 for (auto *Store : EHPtrStores) {
860 if (Inst == Store)
861 return true;
862 }
863 for (auto *Store : SelectorStores) {
864 if (Inst == Store)
865 return true;
866 }
867
868 return false;
869}
870
871void LandingPadMap::remapSelector(ValueToValueMapTy &VMap,
872 Value *MappedValue) const {
873 // Remap all selector extract instructions to the specified value.
874 for (auto *Extract : ExtractedSelectors)
875 VMap[Extract] = MappedValue;
876}
877
878bool LandingPadMap::mapIfEHLoad(const LoadInst *Load,
879 SmallVectorImpl<const StoreInst *> &Stores,
880 SmallVectorImpl<const Value *> &StoreAddrs) {
881 // This makes the assumption that a store we've previously seen dominates
882 // this load instruction. That might seem like a rather huge assumption,
883 // but given the way that landingpads are constructed its fairly safe.
884 // FIXME: Add debug/assert code that verifies this.
885 const Value *LoadAddr = Load->getPointerOperand();
886 for (auto *StoreAddr : StoreAddrs) {
887 if (LoadAddr == StoreAddr) {
888 // Handle the common debug scenario where this loaded value is stored
889 // to a different location.
890 for (auto *U : Load->users()) {
891 if (auto *Store = dyn_cast<StoreInst>(U)) {
892 Stores.push_back(Store);
893 StoreAddrs.push_back(Store->getPointerOperand());
894 }
895 }
896 return true;
897 }
898 }
899 return false;
900}
901
Andrew Kaylorf0f5e462015-03-03 20:00:16 +0000902CloningDirector::CloningAction WinEHCloningDirectorBase::handleInstruction(
Andrew Kaylor1476e6d2015-02-24 20:49:35 +0000903 ValueToValueMapTy &VMap, const Instruction *Inst, BasicBlock *NewBB) {
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000904 // If this is one of the boilerplate landing pad instructions, skip it.
905 // The instruction will have already been remapped in VMap.
906 if (LPadMap.isLandingPadSpecificInst(Inst))
907 return CloningDirector::SkipInstruction;
Andrew Kaylor1476e6d2015-02-24 20:49:35 +0000908
909 if (auto *Load = dyn_cast<LoadInst>(Inst)) {
910 // Look for loads of (previously suppressed) landingpad values.
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000911 // The EHPtr load can be mapped to an undef value as it should only be used
912 // as an argument to llvm.eh.begincatch, but the selector value needs to be
913 // mapped to a constant value of 1. This value will be used to simplify the
914 // branching to always flow to the current handler.
915 if (LPadMap.mapIfSelectorLoad(Load)) {
916 VMap[Inst] = ConstantInt::get(SelectorIDType, 1);
Andrew Kaylor1476e6d2015-02-24 20:49:35 +0000917 return CloningDirector::SkipInstruction;
918 }
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000919 if (LPadMap.mapIfEHPtrLoad(Load)) {
920 VMap[Inst] = UndefValue::get(Int8PtrType);
Andrew Kaylor1476e6d2015-02-24 20:49:35 +0000921 return CloningDirector::SkipInstruction;
922 }
923
924 // Any other loads just get cloned.
925 return CloningDirector::CloneInstruction;
926 }
927
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000928 // Nested landing pads will be cloned as stubs, with just the
929 // landingpad instruction and an unreachable instruction. When
930 // all landingpads have been outlined, we'll replace this with the
931 // llvm.eh.actions call and indirect branch created when the
932 // landing pad was outlined.
933 if (auto *NestedLPad = dyn_cast<LandingPadInst>(Inst)) {
934 Instruction *NewInst = NestedLPad->clone();
935 if (NestedLPad->hasName())
936 NewInst->setName(NestedLPad->getName());
937 // FIXME: Store this mapping somewhere else also.
938 VMap[NestedLPad] = NewInst;
939 BasicBlock::InstListType &InstList = NewBB->getInstList();
940 InstList.push_back(NewInst);
941 InstList.push_back(new UnreachableInst(NewBB->getContext()));
942 return CloningDirector::StopCloningBB;
943 }
944
945 if (auto *Invoke = dyn_cast<InvokeInst>(Inst))
946 return handleInvoke(VMap, Invoke, NewBB);
947
Andrew Kaylorf0f5e462015-03-03 20:00:16 +0000948 if (auto *Resume = dyn_cast<ResumeInst>(Inst))
949 return handleResume(VMap, Resume, NewBB);
Andrew Kaylor1476e6d2015-02-24 20:49:35 +0000950
Andrew Kaylorf0f5e462015-03-03 20:00:16 +0000951 if (match(Inst, m_Intrinsic<Intrinsic::eh_begincatch>()))
952 return handleBeginCatch(VMap, Inst, NewBB);
953 if (match(Inst, m_Intrinsic<Intrinsic::eh_endcatch>()))
954 return handleEndCatch(VMap, Inst, NewBB);
955 if (match(Inst, m_Intrinsic<Intrinsic::eh_typeid_for>()))
956 return handleTypeIdFor(VMap, Inst, NewBB);
Andrew Kaylor1476e6d2015-02-24 20:49:35 +0000957
958 // Continue with the default cloning behavior.
959 return CloningDirector::CloneInstruction;
960}
961
Andrew Kaylorf0f5e462015-03-03 20:00:16 +0000962CloningDirector::CloningAction WinEHCatchDirector::handleBeginCatch(
963 ValueToValueMapTy &VMap, const Instruction *Inst, BasicBlock *NewBB) {
964 // The argument to the call is some form of the first element of the
965 // landingpad aggregate value, but that doesn't matter. It isn't used
966 // here.
Reid Kleckner42366532015-03-03 23:20:30 +0000967 // The second argument is an outparameter where the exception object will be
968 // stored. Typically the exception object is a scalar, but it can be an
969 // aggregate when catching by value.
970 // FIXME: Leave something behind to indicate where the exception object lives
971 // for this handler. Should it be part of llvm.eh.actions?
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000972 assert(ExceptionObjectVar == nullptr && "Multiple calls to "
973 "llvm.eh.begincatch found while "
974 "outlining catch handler.");
975 ExceptionObjectVar = Inst->getOperand(1)->stripPointerCasts();
Andrew Kaylorf0f5e462015-03-03 20:00:16 +0000976 return CloningDirector::SkipInstruction;
977}
978
979CloningDirector::CloningAction
980WinEHCatchDirector::handleEndCatch(ValueToValueMapTy &VMap,
981 const Instruction *Inst, BasicBlock *NewBB) {
982 auto *IntrinCall = dyn_cast<IntrinsicInst>(Inst);
983 // It might be interesting to track whether or not we are inside a catch
984 // function, but that might make the algorithm more brittle than it needs
985 // to be.
986
987 // The end catch call can occur in one of two places: either in a
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000988 // landingpad block that is part of the catch handlers exception mechanism,
Andrew Kaylorf7118ae2015-03-27 22:31:12 +0000989 // or at the end of the catch block. However, a catch-all handler may call
990 // end catch from the original landing pad. If the call occurs in a nested
991 // landing pad block, we must skip it and continue so that the landing pad
992 // gets cloned.
993 auto *ParentBB = IntrinCall->getParent();
994 if (ParentBB->isLandingPad() && !LPadMap.isOriginLandingPadBlock(ParentBB))
Andrew Kaylorf0f5e462015-03-03 20:00:16 +0000995 return CloningDirector::SkipInstruction;
996
997 // If an end catch occurs anywhere else the next instruction should be an
998 // unconditional branch instruction that we want to replace with a return
999 // to the the address of the branch target.
1000 const BasicBlock *EndCatchBB = IntrinCall->getParent();
1001 const TerminatorInst *Terminator = EndCatchBB->getTerminator();
1002 const BranchInst *Branch = dyn_cast<BranchInst>(Terminator);
1003 assert(Branch && Branch->isUnconditional());
1004 assert(std::next(BasicBlock::const_iterator(IntrinCall)) ==
1005 BasicBlock::const_iterator(Branch));
1006
Andrew Kaylor6b67d422015-03-11 23:22:06 +00001007 BasicBlock *ContinueLabel = Branch->getSuccessor(0);
1008 ReturnInst::Create(NewBB->getContext(), BlockAddress::get(ContinueLabel),
1009 NewBB);
1010 ReturnTargets.push_back(ContinueLabel);
Andrew Kaylorf0f5e462015-03-03 20:00:16 +00001011
1012 // We just added a terminator to the cloned block.
1013 // Tell the caller to stop processing the current basic block so that
1014 // the branch instruction will be skipped.
1015 return CloningDirector::StopCloningBB;
1016}
1017
1018CloningDirector::CloningAction WinEHCatchDirector::handleTypeIdFor(
1019 ValueToValueMapTy &VMap, const Instruction *Inst, BasicBlock *NewBB) {
1020 auto *IntrinCall = dyn_cast<IntrinsicInst>(Inst);
1021 Value *Selector = IntrinCall->getArgOperand(0)->stripPointerCasts();
1022 // This causes a replacement that will collapse the landing pad CFG based
1023 // on the filter function we intend to match.
1024 if (Selector == CurrentSelector)
1025 VMap[Inst] = ConstantInt::get(SelectorIDType, 1);
1026 else
1027 VMap[Inst] = ConstantInt::get(SelectorIDType, 0);
1028 // Tell the caller not to clone this instruction.
1029 return CloningDirector::SkipInstruction;
1030}
1031
1032CloningDirector::CloningAction
Andrew Kaylor6b67d422015-03-11 23:22:06 +00001033WinEHCatchDirector::handleInvoke(ValueToValueMapTy &VMap,
1034 const InvokeInst *Invoke, BasicBlock *NewBB) {
1035 return CloningDirector::CloneInstruction;
1036}
1037
1038CloningDirector::CloningAction
Andrew Kaylorf0f5e462015-03-03 20:00:16 +00001039WinEHCatchDirector::handleResume(ValueToValueMapTy &VMap,
1040 const ResumeInst *Resume, BasicBlock *NewBB) {
1041 // Resume instructions shouldn't be reachable from catch handlers.
1042 // We still need to handle it, but it will be pruned.
1043 BasicBlock::InstListType &InstList = NewBB->getInstList();
1044 InstList.push_back(new UnreachableInst(NewBB->getContext()));
1045 return CloningDirector::StopCloningBB;
1046}
1047
1048CloningDirector::CloningAction WinEHCleanupDirector::handleBeginCatch(
1049 ValueToValueMapTy &VMap, const Instruction *Inst, BasicBlock *NewBB) {
1050 // Catch blocks within cleanup handlers will always be unreachable.
1051 // We'll insert an unreachable instruction now, but it will be pruned
1052 // before the cloning process is complete.
1053 BasicBlock::InstListType &InstList = NewBB->getInstList();
1054 InstList.push_back(new UnreachableInst(NewBB->getContext()));
1055 return CloningDirector::StopCloningBB;
1056}
1057
1058CloningDirector::CloningAction WinEHCleanupDirector::handleEndCatch(
1059 ValueToValueMapTy &VMap, const Instruction *Inst, BasicBlock *NewBB) {
1060 // Catch blocks within cleanup handlers will always be unreachable.
1061 // We'll insert an unreachable instruction now, but it will be pruned
1062 // before the cloning process is complete.
1063 BasicBlock::InstListType &InstList = NewBB->getInstList();
1064 InstList.push_back(new UnreachableInst(NewBB->getContext()));
1065 return CloningDirector::StopCloningBB;
1066}
1067
1068CloningDirector::CloningAction WinEHCleanupDirector::handleTypeIdFor(
1069 ValueToValueMapTy &VMap, const Instruction *Inst, BasicBlock *NewBB) {
Andrew Kaylor6b67d422015-03-11 23:22:06 +00001070 // If we encounter a selector comparison while cloning a cleanup handler,
1071 // we want to stop cloning immediately. Anything after the dispatch
1072 // will be outlined into a different handler.
1073 BasicBlock *CatchHandler;
1074 Constant *Selector;
1075 BasicBlock *NextBB;
1076 if (isSelectorDispatch(const_cast<BasicBlock *>(Inst->getParent()),
1077 CatchHandler, Selector, NextBB)) {
1078 ReturnInst::Create(NewBB->getContext(), nullptr, NewBB);
1079 return CloningDirector::StopCloningBB;
1080 }
1081 // If eg.typeid.for is called for any other reason, it can be ignored.
Andrew Kaylorf0f5e462015-03-03 20:00:16 +00001082 VMap[Inst] = ConstantInt::get(SelectorIDType, 0);
Andrew Kaylorf0f5e462015-03-03 20:00:16 +00001083 return CloningDirector::SkipInstruction;
1084}
1085
Andrew Kaylor6b67d422015-03-11 23:22:06 +00001086CloningDirector::CloningAction WinEHCleanupDirector::handleInvoke(
1087 ValueToValueMapTy &VMap, const InvokeInst *Invoke, BasicBlock *NewBB) {
1088 // All invokes in cleanup handlers can be replaced with calls.
1089 SmallVector<Value *, 16> CallArgs(Invoke->op_begin(), Invoke->op_end() - 3);
1090 // Insert a normal call instruction...
1091 CallInst *NewCall =
1092 CallInst::Create(const_cast<Value *>(Invoke->getCalledValue()), CallArgs,
1093 Invoke->getName(), NewBB);
1094 NewCall->setCallingConv(Invoke->getCallingConv());
1095 NewCall->setAttributes(Invoke->getAttributes());
1096 NewCall->setDebugLoc(Invoke->getDebugLoc());
1097 VMap[Invoke] = NewCall;
1098
1099 // Insert an unconditional branch to the normal destination.
1100 BranchInst::Create(Invoke->getNormalDest(), NewBB);
1101
1102 // The unwind destination won't be cloned into the new function, so
1103 // we don't need to clean up its phi nodes.
1104
1105 // We just added a terminator to the cloned block.
1106 // Tell the caller to stop processing the current basic block.
1107 return CloningDirector::StopCloningBB;
1108}
1109
Andrew Kaylorf0f5e462015-03-03 20:00:16 +00001110CloningDirector::CloningAction WinEHCleanupDirector::handleResume(
1111 ValueToValueMapTy &VMap, const ResumeInst *Resume, BasicBlock *NewBB) {
1112 ReturnInst::Create(NewBB->getContext(), nullptr, NewBB);
1113
1114 // We just added a terminator to the cloned block.
1115 // Tell the caller to stop processing the current basic block so that
1116 // the branch instruction will be skipped.
1117 return CloningDirector::StopCloningBB;
1118}
1119
Andrew Kaylor1476e6d2015-02-24 20:49:35 +00001120WinEHFrameVariableMaterializer::WinEHFrameVariableMaterializer(
1121 Function *OutlinedFn, FrameVarInfoMap &FrameVarInfo)
1122 : FrameVarInfo(FrameVarInfo), Builder(OutlinedFn->getContext()) {
1123 Builder.SetInsertPoint(&OutlinedFn->getEntryBlock());
Andrew Kaylor1476e6d2015-02-24 20:49:35 +00001124}
1125
1126Value *WinEHFrameVariableMaterializer::materializeValueFor(Value *V) {
Andrew Kaylor72029c62015-03-03 00:41:03 +00001127 // If we're asked to materialize a value that is an instruction, we
1128 // temporarily create an alloca in the outlined function and add this
1129 // to the FrameVarInfo map. When all the outlining is complete, we'll
1130 // collect these into a structure, spilling non-alloca values in the
1131 // parent frame as necessary, and replace these temporary allocas with
1132 // GEPs referencing the frame allocation block.
1133
1134 // If the value is an alloca, the mapping is direct.
Andrew Kaylor1476e6d2015-02-24 20:49:35 +00001135 if (auto *AV = dyn_cast<AllocaInst>(V)) {
Andrew Kaylor72029c62015-03-03 00:41:03 +00001136 AllocaInst *NewAlloca = dyn_cast<AllocaInst>(AV->clone());
1137 Builder.Insert(NewAlloca, AV->getName());
Reid Klecknercfb9ce52015-03-05 18:26:34 +00001138 FrameVarInfo[AV].push_back(NewAlloca);
Andrew Kaylor1476e6d2015-02-24 20:49:35 +00001139 return NewAlloca;
1140 }
1141
Andrew Kaylor72029c62015-03-03 00:41:03 +00001142 // For other types of instructions or arguments, we need an alloca based on
1143 // the value's type and a load of the alloca. The alloca will be replaced
1144 // by a GEP, but the load will stay. In the parent function, the value will
1145 // be spilled to a location in the frame allocation block.
1146 if (isa<Instruction>(V) || isa<Argument>(V)) {
1147 AllocaInst *NewAlloca =
1148 Builder.CreateAlloca(V->getType(), nullptr, "eh.temp.alloca");
Reid Klecknercfb9ce52015-03-05 18:26:34 +00001149 FrameVarInfo[V].push_back(NewAlloca);
Andrew Kaylor72029c62015-03-03 00:41:03 +00001150 LoadInst *NewLoad = Builder.CreateLoad(NewAlloca, V->getName() + ".reload");
1151 return NewLoad;
Andrew Kaylor1476e6d2015-02-24 20:49:35 +00001152 }
Andrew Kaylor1476e6d2015-02-24 20:49:35 +00001153
Andrew Kaylor72029c62015-03-03 00:41:03 +00001154 // Don't materialize other values.
Andrew Kaylor1476e6d2015-02-24 20:49:35 +00001155 return nullptr;
1156}
Andrew Kaylor6b67d422015-03-11 23:22:06 +00001157
1158// This function maps the catch and cleanup handlers that are reachable from the
1159// specified landing pad. The landing pad sequence will have this basic shape:
1160//
1161// <cleanup handler>
1162// <selector comparison>
1163// <catch handler>
1164// <cleanup handler>
1165// <selector comparison>
1166// <catch handler>
1167// <cleanup handler>
1168// ...
1169//
1170// Any of the cleanup slots may be absent. The cleanup slots may be occupied by
1171// any arbitrary control flow, but all paths through the cleanup code must
1172// eventually reach the next selector comparison and no path can skip to a
1173// different selector comparisons, though some paths may terminate abnormally.
1174// Therefore, we will use a depth first search from the start of any given
1175// cleanup block and stop searching when we find the next selector comparison.
1176//
1177// If the landingpad instruction does not have a catch clause, we will assume
1178// that any instructions other than selector comparisons and catch handlers can
1179// be ignored. In practice, these will only be the boilerplate instructions.
1180//
1181// The catch handlers may also have any control structure, but we are only
1182// interested in the start of the catch handlers, so we don't need to actually
1183// follow the flow of the catch handlers. The start of the catch handlers can
1184// be located from the compare instructions, but they can be skipped in the
1185// flow by following the contrary branch.
1186void WinEHPrepare::mapLandingPadBlocks(LandingPadInst *LPad,
1187 LandingPadActions &Actions) {
1188 unsigned int NumClauses = LPad->getNumClauses();
1189 unsigned int HandlersFound = 0;
1190 BasicBlock *BB = LPad->getParent();
1191
1192 DEBUG(dbgs() << "Mapping landing pad: " << BB->getName() << "\n");
1193
1194 if (NumClauses == 0) {
1195 // This landing pad contains only cleanup code.
1196 CleanupHandler *Action = new CleanupHandler(BB);
1197 CleanupHandlerMap[BB] = Action;
1198 Actions.insertCleanupHandler(Action);
1199 DEBUG(dbgs() << " Assuming cleanup code in block " << BB->getName()
1200 << "\n");
1201 assert(LPad->isCleanup());
1202 return;
1203 }
1204
1205 VisitedBlockSet VisitedBlocks;
1206
1207 while (HandlersFound != NumClauses) {
Andrew Kaylor6b67d422015-03-11 23:22:06 +00001208 BasicBlock *NextBB = nullptr;
1209
1210 // See if the clause we're looking for is a catch-all.
1211 // If so, the catch begins immediately.
1212 if (isa<ConstantPointerNull>(LPad->getClause(HandlersFound))) {
1213 // The catch all must occur last.
1214 assert(HandlersFound == NumClauses - 1);
1215
Reid Kleckner0f9e27a2015-03-18 20:26:53 +00001216 // For C++ EH, check if there is any interesting cleanup code before we
1217 // begin the catch. This is important because cleanups cannot rethrow
1218 // exceptions but code called from catches can. For SEH, it isn't
1219 // important if some finally code before a catch-all is executed out of
1220 // line or after recovering from the exception.
1221 if (Personality == EHPersonality::MSVC_CXX) {
1222 if (auto *CleanupAction = findCleanupHandler(BB, BB)) {
1223 // Add a cleanup entry to the list
1224 Actions.insertCleanupHandler(CleanupAction);
1225 DEBUG(dbgs() << " Found cleanup code in block "
1226 << CleanupAction->getStartBlock()->getName() << "\n");
1227 }
Andrew Kaylor6b67d422015-03-11 23:22:06 +00001228 }
1229
1230 // Add the catch handler to the action list.
1231 CatchHandler *Action =
1232 new CatchHandler(BB, LPad->getClause(HandlersFound), nullptr);
1233 CatchHandlerMap[BB] = Action;
1234 Actions.insertCatchHandler(Action);
1235 DEBUG(dbgs() << " Catch all handler at block " << BB->getName() << "\n");
1236 ++HandlersFound;
Reid Kleckner0f9e27a2015-03-18 20:26:53 +00001237
1238 // Once we reach a catch-all, don't expect to hit a resume instruction.
1239 BB = nullptr;
1240 break;
Andrew Kaylor6b67d422015-03-11 23:22:06 +00001241 }
1242
1243 CatchHandler *CatchAction = findCatchHandler(BB, NextBB, VisitedBlocks);
1244 // See if there is any interesting code executed before the dispatch.
1245 if (auto *CleanupAction =
1246 findCleanupHandler(BB, CatchAction->getStartBlock())) {
1247 // Add a cleanup entry to the list
1248 Actions.insertCleanupHandler(CleanupAction);
1249 DEBUG(dbgs() << " Found cleanup code in block "
1250 << CleanupAction->getStartBlock()->getName() << "\n");
1251 }
1252
1253 assert(CatchAction);
1254 ++HandlersFound;
1255
1256 // Add the catch handler to the action list.
1257 Actions.insertCatchHandler(CatchAction);
1258 DEBUG(dbgs() << " Found catch dispatch in block "
1259 << CatchAction->getStartBlock()->getName() << "\n");
1260
1261 // Move on to the block after the catch handler.
1262 BB = NextBB;
1263 }
1264
Reid Kleckner0f9e27a2015-03-18 20:26:53 +00001265 // If we didn't wind up in a catch-all, see if there is any interesting code
1266 // executed before the resume.
Andrew Kaylor6b67d422015-03-11 23:22:06 +00001267 if (auto *CleanupAction = findCleanupHandler(BB, BB)) {
1268 // Add a cleanup entry to the list
1269 Actions.insertCleanupHandler(CleanupAction);
1270 DEBUG(dbgs() << " Found cleanup code in block "
1271 << CleanupAction->getStartBlock()->getName() << "\n");
1272 }
1273
1274 // It's possible that some optimization moved code into a landingpad that
1275 // wasn't
1276 // previously being used for cleanup. If that happens, we need to execute
1277 // that
1278 // extra code from a cleanup handler.
1279 if (Actions.includesCleanup() && !LPad->isCleanup())
1280 LPad->setCleanup(true);
1281}
1282
1283// This function searches starting with the input block for the next
1284// block that terminates with a branch whose condition is based on a selector
1285// comparison. This may be the input block. See the mapLandingPadBlocks
1286// comments for a discussion of control flow assumptions.
1287//
1288CatchHandler *WinEHPrepare::findCatchHandler(BasicBlock *BB,
1289 BasicBlock *&NextBB,
1290 VisitedBlockSet &VisitedBlocks) {
1291 // See if we've already found a catch handler use it.
1292 // Call count() first to avoid creating a null entry for blocks
1293 // we haven't seen before.
1294 if (CatchHandlerMap.count(BB) && CatchHandlerMap[BB] != nullptr) {
1295 CatchHandler *Action = cast<CatchHandler>(CatchHandlerMap[BB]);
1296 NextBB = Action->getNextBB();
1297 return Action;
1298 }
1299
1300 // VisitedBlocks applies only to the current search. We still
1301 // need to consider blocks that we've visited while mapping other
1302 // landing pads.
1303 VisitedBlocks.insert(BB);
1304
1305 BasicBlock *CatchBlock = nullptr;
1306 Constant *Selector = nullptr;
1307
1308 // If this is the first time we've visited this block from any landing pad
1309 // look to see if it is a selector dispatch block.
1310 if (!CatchHandlerMap.count(BB)) {
1311 if (isSelectorDispatch(BB, CatchBlock, Selector, NextBB)) {
1312 CatchHandler *Action = new CatchHandler(BB, Selector, NextBB);
1313 CatchHandlerMap[BB] = Action;
1314 return Action;
1315 }
1316 }
1317
1318 // Visit each successor, looking for the dispatch.
1319 // FIXME: We expect to find the dispatch quickly, so this will probably
1320 // work better as a breadth first search.
1321 for (BasicBlock *Succ : successors(BB)) {
1322 if (VisitedBlocks.count(Succ))
1323 continue;
1324
1325 CatchHandler *Action = findCatchHandler(Succ, NextBB, VisitedBlocks);
1326 if (Action)
1327 return Action;
1328 }
1329 return nullptr;
1330}
1331
1332// These are helper functions to combine repeated code from findCleanupHandler.
1333static CleanupHandler *createCleanupHandler(CleanupHandlerMapTy &CleanupHandlerMap,
1334 BasicBlock *BB) {
1335 CleanupHandler *Action = new CleanupHandler(BB);
1336 CleanupHandlerMap[BB] = Action;
1337 return Action;
1338}
1339
1340// This function searches starting with the input block for the next block that
1341// contains code that is not part of a catch handler and would not be eliminated
1342// during handler outlining.
1343//
1344CleanupHandler *WinEHPrepare::findCleanupHandler(BasicBlock *StartBB,
1345 BasicBlock *EndBB) {
1346 // Here we will skip over the following:
1347 //
1348 // landing pad prolog:
1349 //
1350 // Unconditional branches
1351 //
1352 // Selector dispatch
1353 //
1354 // Resume pattern
1355 //
1356 // Anything else marks the start of an interesting block
1357
1358 BasicBlock *BB = StartBB;
1359 // Anything other than an unconditional branch will kick us out of this loop
1360 // one way or another.
1361 while (BB) {
1362 // If we've already scanned this block, don't scan it again. If it is
1363 // a cleanup block, there will be an action in the CleanupHandlerMap.
1364 // If we've scanned it and it is not a cleanup block, there will be a
1365 // nullptr in the CleanupHandlerMap. If we have not scanned it, there will
1366 // be no entry in the CleanupHandlerMap. We must call count() first to
1367 // avoid creating a null entry for blocks we haven't scanned.
1368 if (CleanupHandlerMap.count(BB)) {
1369 if (auto *Action = CleanupHandlerMap[BB]) {
1370 return cast<CleanupHandler>(Action);
1371 } else {
1372 // Here we handle the case where the cleanup handler map contains a
1373 // value for this block but the value is a nullptr. This means that
1374 // we have previously analyzed the block and determined that it did
1375 // not contain any cleanup code. Based on the earlier analysis, we
1376 // know the the block must end in either an unconditional branch, a
1377 // resume or a conditional branch that is predicated on a comparison
1378 // with a selector. Either the resume or the selector dispatch
1379 // would terminate the search for cleanup code, so the unconditional
1380 // branch is the only case for which we might need to continue
1381 // searching.
1382 if (BB == EndBB)
1383 return nullptr;
1384 BasicBlock *SuccBB;
1385 if (!match(BB->getTerminator(), m_UnconditionalBr(SuccBB)))
1386 return nullptr;
1387 BB = SuccBB;
1388 continue;
1389 }
1390 }
1391
1392 // Create an entry in the cleanup handler map for this block. Initially
1393 // we create an entry that says this isn't a cleanup block. If we find
1394 // cleanup code, the caller will replace this entry.
1395 CleanupHandlerMap[BB] = nullptr;
1396
1397 TerminatorInst *Terminator = BB->getTerminator();
1398
1399 // Landing pad blocks have extra instructions we need to accept.
1400 LandingPadMap *LPadMap = nullptr;
1401 if (BB->isLandingPad()) {
1402 LandingPadInst *LPad = BB->getLandingPadInst();
1403 LPadMap = &LPadMaps[LPad];
1404 if (!LPadMap->isInitialized())
1405 LPadMap->mapLandingPad(LPad);
1406 }
1407
1408 // Look for the bare resume pattern:
1409 // %exn2 = load i8** %exn.slot
1410 // %sel2 = load i32* %ehselector.slot
1411 // %lpad.val1 = insertvalue { i8*, i32 } undef, i8* %exn2, 0
1412 // %lpad.val2 = insertvalue { i8*, i32 } %lpad.val1, i32 %sel2, 1
1413 // resume { i8*, i32 } %lpad.val2
1414 if (auto *Resume = dyn_cast<ResumeInst>(Terminator)) {
1415 InsertValueInst *Insert1 = nullptr;
1416 InsertValueInst *Insert2 = nullptr;
Reid Kleckner0f9e27a2015-03-18 20:26:53 +00001417 Value *ResumeVal = Resume->getOperand(0);
1418 // If there is only one landingpad, we may use the lpad directly with no
1419 // insertions.
1420 if (isa<LandingPadInst>(ResumeVal))
1421 return nullptr;
1422 if (!isa<PHINode>(ResumeVal)) {
1423 Insert2 = dyn_cast<InsertValueInst>(ResumeVal);
Andrew Kaylor6b67d422015-03-11 23:22:06 +00001424 if (!Insert2)
1425 return createCleanupHandler(CleanupHandlerMap, BB);
1426 Insert1 = dyn_cast<InsertValueInst>(Insert2->getAggregateOperand());
1427 if (!Insert1)
1428 return createCleanupHandler(CleanupHandlerMap, BB);
1429 }
1430 for (BasicBlock::iterator II = BB->getFirstNonPHIOrDbg(), IE = BB->end();
1431 II != IE; ++II) {
1432 Instruction *Inst = II;
1433 if (LPadMap && LPadMap->isLandingPadSpecificInst(Inst))
1434 continue;
1435 if (Inst == Insert1 || Inst == Insert2 || Inst == Resume)
1436 continue;
1437 if (!Inst->hasOneUse() ||
1438 (Inst->user_back() != Insert1 && Inst->user_back() != Insert2)) {
1439 return createCleanupHandler(CleanupHandlerMap, BB);
1440 }
1441 }
1442 return nullptr;
1443 }
1444
1445 BranchInst *Branch = dyn_cast<BranchInst>(Terminator);
1446 if (Branch) {
1447 if (Branch->isConditional()) {
1448 // Look for the selector dispatch.
1449 // %sel = load i32* %ehselector.slot
1450 // %2 = call i32 @llvm.eh.typeid.for(i8* bitcast (i8** @_ZTIf to i8*))
1451 // %matches = icmp eq i32 %sel12, %2
1452 // br i1 %matches, label %catch14, label %eh.resume
1453 CmpInst *Compare = dyn_cast<CmpInst>(Branch->getCondition());
1454 if (!Compare || !Compare->isEquality())
1455 return createCleanupHandler(CleanupHandlerMap, BB);
1456 for (BasicBlock::iterator II = BB->getFirstNonPHIOrDbg(),
1457 IE = BB->end();
1458 II != IE; ++II) {
1459 Instruction *Inst = II;
1460 if (LPadMap && LPadMap->isLandingPadSpecificInst(Inst))
1461 continue;
1462 if (Inst == Compare || Inst == Branch)
1463 continue;
1464 if (!Inst->hasOneUse() || (Inst->user_back() != Compare))
1465 return createCleanupHandler(CleanupHandlerMap, BB);
1466 if (match(Inst, m_Intrinsic<Intrinsic::eh_typeid_for>()))
1467 continue;
1468 if (!isa<LoadInst>(Inst))
1469 return createCleanupHandler(CleanupHandlerMap, BB);
1470 }
1471 // The selector dispatch block should always terminate our search.
1472 assert(BB == EndBB);
1473 return nullptr;
1474 } else {
1475 // Look for empty blocks with unconditional branches.
1476 for (BasicBlock::iterator II = BB->getFirstNonPHIOrDbg(),
1477 IE = BB->end();
1478 II != IE; ++II) {
1479 Instruction *Inst = II;
1480 if (LPadMap && LPadMap->isLandingPadSpecificInst(Inst))
1481 continue;
1482 if (Inst == Branch)
1483 continue;
Andrew Kaylorf7118ae2015-03-27 22:31:12 +00001484 // This can happen with a catch-all handler.
1485 if (match(Inst, m_Intrinsic<Intrinsic::eh_begincatch>()))
1486 return nullptr;
Andrew Kaylor6b67d422015-03-11 23:22:06 +00001487 if (match(Inst, m_Intrinsic<Intrinsic::eh_endcatch>()))
1488 continue;
1489 // Anything else makes this interesting cleanup code.
1490 return createCleanupHandler(CleanupHandlerMap, BB);
1491 }
1492 if (BB == EndBB)
1493 return nullptr;
1494 // The branch was unconditional.
1495 BB = Branch->getSuccessor(0);
1496 continue;
1497 } // End else of if branch was conditional
1498 } // End if Branch
1499
1500 // Anything else makes this interesting cleanup code.
1501 return createCleanupHandler(CleanupHandlerMap, BB);
1502 }
1503 return nullptr;
1504}