blob: 044cbe0df20eb481cc6b909645b91ea634764937 [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
Reid Kleckner0738a9c2015-05-05 17:44:16 +000011// backend wants for functions using a personality function from a runtime
12// provided by MSVC. Functions with other personality functions are left alone
13// and may be prepared by other passes. In particular, all supported MSVC
14// personality functions require cleanup code to be outlined, and the C++
15// personality requires catch handler code to be outlined.
Andrew Kaylor1476e6d2015-02-24 20:49:35 +000016//
17//===----------------------------------------------------------------------===//
18
19#include "llvm/CodeGen/Passes.h"
20#include "llvm/ADT/MapVector.h"
Andrew Kaylor6b67d422015-03-11 23:22:06 +000021#include "llvm/ADT/STLExtras.h"
Benjamin Kramera8d61b12015-03-23 18:57:17 +000022#include "llvm/ADT/SmallSet.h"
Reid Klecknerfd7df282015-04-22 21:05:21 +000023#include "llvm/ADT/SetVector.h"
Reid Klecknerbcda1cd2015-04-29 22:49:54 +000024#include "llvm/ADT/Triple.h"
Andrew Kaylor1476e6d2015-02-24 20:49:35 +000025#include "llvm/ADT/TinyPtrVector.h"
David Majnemerfd9f4772015-08-11 01:15:26 +000026#include "llvm/Analysis/CFG.h"
Andrew Kaylor1476e6d2015-02-24 20:49:35 +000027#include "llvm/Analysis/LibCallSemantics.h"
Reid Klecknerf12c0302015-06-09 21:42:19 +000028#include "llvm/Analysis/TargetLibraryInfo.h"
David Majnemercde33032015-03-30 22:58:10 +000029#include "llvm/CodeGen/WinEHFuncInfo.h"
Andrew Kaylor64622aa2015-04-01 17:21:25 +000030#include "llvm/IR/Dominators.h"
Andrew Kaylor1476e6d2015-02-24 20:49:35 +000031#include "llvm/IR/Function.h"
32#include "llvm/IR/IRBuilder.h"
33#include "llvm/IR/Instructions.h"
34#include "llvm/IR/IntrinsicInst.h"
35#include "llvm/IR/Module.h"
36#include "llvm/IR/PatternMatch.h"
37#include "llvm/Pass.h"
Andrew Kaylor6b67d422015-03-11 23:22:06 +000038#include "llvm/Support/Debug.h"
Benjamin Kramera8d61b12015-03-23 18:57:17 +000039#include "llvm/Support/raw_ostream.h"
Andrew Kaylor6b67d422015-03-11 23:22:06 +000040#include "llvm/Transforms/Utils/BasicBlockUtils.h"
Andrew Kaylor1476e6d2015-02-24 20:49:35 +000041#include "llvm/Transforms/Utils/Cloning.h"
42#include "llvm/Transforms/Utils/Local.h"
Andrew Kaylor64622aa2015-04-01 17:21:25 +000043#include "llvm/Transforms/Utils/PromoteMemToReg.h"
Andrew Kaylor1476e6d2015-02-24 20:49:35 +000044#include <memory>
45
46using namespace llvm;
47using namespace llvm::PatternMatch;
48
49#define DEBUG_TYPE "winehprepare"
50
51namespace {
52
Andrew Kaylor1476e6d2015-02-24 20:49:35 +000053// This map is used to model frame variable usage during outlining, to
54// construct a structure type to hold the frame variables in a frame
55// allocation block, and to remap the frame variable allocas (including
56// spill locations as needed) to GEPs that get the variable from the
57// frame allocation structure.
Reid Klecknercfb9ce52015-03-05 18:26:34 +000058typedef MapVector<Value *, TinyPtrVector<AllocaInst *>> FrameVarInfoMap;
Andrew Kaylor1476e6d2015-02-24 20:49:35 +000059
Reid Kleckner3567d272015-04-02 21:13:31 +000060// TinyPtrVector cannot hold nullptr, so we need our own sentinel that isn't
61// quite null.
62AllocaInst *getCatchObjectSentinel() {
63 return static_cast<AllocaInst *>(nullptr) + 1;
64}
65
Andrew Kaylor6b67d422015-03-11 23:22:06 +000066typedef SmallSet<BasicBlock *, 4> VisitedBlockSet;
67
Andrew Kaylor6b67d422015-03-11 23:22:06 +000068class LandingPadActions;
Andrew Kaylor6b67d422015-03-11 23:22:06 +000069class LandingPadMap;
70
71typedef DenseMap<const BasicBlock *, CatchHandler *> CatchHandlerMapTy;
72typedef DenseMap<const BasicBlock *, CleanupHandler *> CleanupHandlerMapTy;
73
Andrew Kaylor1476e6d2015-02-24 20:49:35 +000074class WinEHPrepare : public FunctionPass {
Andrew Kaylor1476e6d2015-02-24 20:49:35 +000075public:
76 static char ID; // Pass identification, replacement for typeid.
77 WinEHPrepare(const TargetMachine *TM = nullptr)
Reid Kleckner582786b2015-04-30 18:17:12 +000078 : FunctionPass(ID) {
Reid Klecknerbcda1cd2015-04-29 22:49:54 +000079 if (TM)
Daniel Sanders110bf6d2015-06-24 13:25:57 +000080 TheTriple = TM->getTargetTriple();
Reid Klecknerbcda1cd2015-04-29 22:49:54 +000081 }
Andrew Kaylor1476e6d2015-02-24 20:49:35 +000082
83 bool runOnFunction(Function &Fn) override;
84
85 bool doFinalization(Module &M) override;
86
87 void getAnalysisUsage(AnalysisUsage &AU) const override;
88
89 const char *getPassName() const override {
90 return "Windows exception handling preparation";
91 }
92
93private:
Reid Kleckner0f9e27a2015-03-18 20:26:53 +000094 bool prepareExceptionHandlers(Function &F,
95 SmallVectorImpl<LandingPadInst *> &LPads);
Andrew Kaylora6c5b962015-05-20 23:22:24 +000096 void identifyEHBlocks(Function &F, SmallVectorImpl<LandingPadInst *> &LPads);
Andrew Kaylor64622aa2015-04-01 17:21:25 +000097 void promoteLandingPadValues(LandingPadInst *LPad);
Reid Klecknerfd7df282015-04-22 21:05:21 +000098 void demoteValuesLiveAcrossHandlers(Function &F,
99 SmallVectorImpl<LandingPadInst *> &LPads);
Andrew Kaylor046f7b42015-04-28 21:54:14 +0000100 void findSEHEHReturnPoints(Function &F,
101 SetVector<BasicBlock *> &EHReturnBlocks);
102 void findCXXEHReturnPoints(Function &F,
103 SetVector<BasicBlock *> &EHReturnBlocks);
Andrew Kaylorcc14f382015-05-11 23:06:02 +0000104 void getPossibleReturnTargets(Function *ParentF, Function *HandlerF,
105 SetVector<BasicBlock*> &Targets);
Andrew Kayloraa92ab02015-04-03 19:37:50 +0000106 void completeNestedLandingPad(Function *ParentFn,
107 LandingPadInst *OutlinedLPad,
108 const LandingPadInst *OriginalLPad,
109 FrameVarInfoMap &VarInfo);
Reid Kleckner6511c8b2015-07-01 20:59:25 +0000110 Function *createHandlerFunc(Function *ParentFn, Type *RetTy,
111 const Twine &Name, Module *M, Value *&ParentFP);
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000112 bool outlineHandler(ActionHandler *Action, Function *SrcFn,
113 LandingPadInst *LPad, BasicBlock *StartBB,
Reid Klecknercfb9ce52015-03-05 18:26:34 +0000114 FrameVarInfoMap &VarInfo);
David Majnemer7fddecc2015-06-17 20:52:32 +0000115 void addStubInvokeToHandlerIfNeeded(Function *Handler);
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000116
117 void mapLandingPadBlocks(LandingPadInst *LPad, LandingPadActions &Actions);
118 CatchHandler *findCatchHandler(BasicBlock *BB, BasicBlock *&NextBB,
119 VisitedBlockSet &VisitedBlocks);
Reid Kleckner9405ef02015-04-10 23:12:29 +0000120 void findCleanupHandlers(LandingPadActions &Actions, BasicBlock *StartBB,
121 BasicBlock *EndBB);
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000122
Reid Kleckner0f9e27a2015-03-18 20:26:53 +0000123 void processSEHCatchHandler(CatchHandler *Handler, BasicBlock *StartBB);
Joseph Tremouletc9ff9142015-08-13 14:30:10 +0000124 void insertPHIStores(PHINode *OriginalPHI, AllocaInst *SpillSlot);
125 void
126 insertPHIStore(BasicBlock *PredBlock, Value *PredVal, AllocaInst *SpillSlot,
127 SmallVectorImpl<std::pair<BasicBlock *, Value *>> &Worklist);
128 AllocaInst *insertPHILoads(PHINode *PN, Function &F);
129 void replaceUseWithLoad(Value *V, Use &U, AllocaInst *&SpillSlot,
130 DenseMap<BasicBlock *, Value *> &Loads, Function &F);
131 void demoteNonlocalUses(Value *V, std::set<BasicBlock *> &ColorsForBB,
132 Function &F);
Joseph Tremouletec182852015-08-28 01:12:35 +0000133 bool prepareExplicitEH(Function &F,
134 SmallVectorImpl<BasicBlock *> &EntryBlocks);
135 void colorFunclets(Function &F, SmallVectorImpl<BasicBlock *> &EntryBlocks);
David Majnemerb3d9b962015-09-16 18:40:24 +0000136 void demotePHIsOnFunclets(Function &F);
137 void demoteUsesBetweenFunclets(Function &F);
138 void demoteArgumentUses(Function &F);
139 void cloneCommonBlocks(Function &F,
140 SmallVectorImpl<BasicBlock *> &EntryBlocks);
141 void removeImplausibleTerminators(Function &F);
142 void cleanupPreparedFunclets(Function &F);
143 void verifyPreparedFunclets(Function &F);
David Majnemerfd9f4772015-08-11 01:15:26 +0000144
Reid Klecknerbcda1cd2015-04-29 22:49:54 +0000145 Triple TheTriple;
146
Reid Kleckner0f9e27a2015-03-18 20:26:53 +0000147 // All fields are reset by runOnFunction.
Reid Kleckner582786b2015-04-30 18:17:12 +0000148 DominatorTree *DT = nullptr;
Reid Klecknerf12c0302015-06-09 21:42:19 +0000149 const TargetLibraryInfo *LibInfo = nullptr;
Reid Kleckner582786b2015-04-30 18:17:12 +0000150 EHPersonality Personality = EHPersonality::Unknown;
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000151 CatchHandlerMapTy CatchHandlerMap;
152 CleanupHandlerMapTy CleanupHandlerMap;
Andrew Kaylor64622aa2015-04-01 17:21:25 +0000153 DenseMap<const LandingPadInst *, LandingPadMap> LPadMaps;
Andrew Kaylora6c5b962015-05-20 23:22:24 +0000154 SmallPtrSet<BasicBlock *, 4> NormalBlocks;
155 SmallPtrSet<BasicBlock *, 4> EHBlocks;
156 SetVector<BasicBlock *> EHReturnBlocks;
Andrew Kayloraa92ab02015-04-03 19:37:50 +0000157
158 // This maps landing pad instructions found in outlined handlers to
159 // the landing pad instruction in the parent function from which they
160 // were cloned. The cloned/nested landing pad is used as the key
161 // because the landing pad may be cloned into multiple handlers.
162 // This map will be used to add the llvm.eh.actions call to the nested
163 // landing pads after all handlers have been outlined.
164 DenseMap<LandingPadInst *, const LandingPadInst *> NestedLPtoOriginalLP;
165
166 // This maps blocks in the parent function which are destinations of
167 // catch handlers to cloned blocks in (other) outlined handlers. This
168 // handles the case where a nested landing pads has a catch handler that
169 // returns to a handler function rather than the parent function.
170 // The original block is used as the key here because there should only
171 // ever be one handler function from which the cloned block is not pruned.
172 // The original block will be pruned from the parent function after all
173 // handlers have been outlined. This map will be used to adjust the
174 // return instructions of handlers which return to the block that was
175 // outlined into a handler. This is done after all handlers have been
176 // outlined but before the outlined code is pruned from the parent function.
177 DenseMap<const BasicBlock *, BasicBlock *> LPadTargetBlocks;
Reid Klecknercfbfe6f2015-04-24 20:25:05 +0000178
Reid Klecknerd5afc62f2015-07-07 23:23:03 +0000179 // Map from outlined handler to call to parent local address. Only used for
Reid Klecknerbcda1cd2015-04-29 22:49:54 +0000180 // 32-bit EH.
181 DenseMap<Function *, Value *> HandlerToParentFP;
182
Reid Kleckner582786b2015-04-30 18:17:12 +0000183 AllocaInst *SEHExceptionCodeSlot = nullptr;
David Majnemerfd9f4772015-08-11 01:15:26 +0000184
185 std::map<BasicBlock *, std::set<BasicBlock *>> BlockColors;
186 std::map<BasicBlock *, std::set<BasicBlock *>> FuncletBlocks;
Joseph Tremouletec182852015-08-28 01:12:35 +0000187 std::map<BasicBlock *, std::set<BasicBlock *>> FuncletChildren;
Andrew Kaylor1476e6d2015-02-24 20:49:35 +0000188};
189
190class WinEHFrameVariableMaterializer : public ValueMaterializer {
191public:
Reid Klecknerbcda1cd2015-04-29 22:49:54 +0000192 WinEHFrameVariableMaterializer(Function *OutlinedFn, Value *ParentFP,
Andrew Kaylor1476e6d2015-02-24 20:49:35 +0000193 FrameVarInfoMap &FrameVarInfo);
Alexander Kornienkof817c1c2015-04-11 02:11:45 +0000194 ~WinEHFrameVariableMaterializer() override {}
Andrew Kaylor1476e6d2015-02-24 20:49:35 +0000195
Alexander Kornienkof817c1c2015-04-11 02:11:45 +0000196 Value *materializeValueFor(Value *V) override;
Andrew Kaylor1476e6d2015-02-24 20:49:35 +0000197
Reid Kleckner3567d272015-04-02 21:13:31 +0000198 void escapeCatchObject(Value *V);
199
Andrew Kaylor1476e6d2015-02-24 20:49:35 +0000200private:
201 FrameVarInfoMap &FrameVarInfo;
202 IRBuilder<> Builder;
203};
204
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000205class LandingPadMap {
206public:
207 LandingPadMap() : OriginLPad(nullptr) {}
208 void mapLandingPad(const LandingPadInst *LPad);
209
210 bool isInitialized() { return OriginLPad != nullptr; }
211
Andrew Kaylorf7118ae2015-03-27 22:31:12 +0000212 bool isOriginLandingPadBlock(const BasicBlock *BB) const;
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000213 bool isLandingPadSpecificInst(const Instruction *Inst) const;
214
Andrew Kaylor64622aa2015-04-01 17:21:25 +0000215 void remapEHValues(ValueToValueMapTy &VMap, Value *EHPtrValue,
216 Value *SelectorValue) const;
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000217
218private:
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000219 const LandingPadInst *OriginLPad;
220 // We will normally only see one of each of these instructions, but
221 // if more than one occurs for some reason we can handle that.
222 TinyPtrVector<const ExtractValueInst *> ExtractedEHPtrs;
223 TinyPtrVector<const ExtractValueInst *> ExtractedSelectors;
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000224};
225
Andrew Kaylorf0f5e462015-03-03 20:00:16 +0000226class WinEHCloningDirectorBase : public CloningDirector {
Andrew Kaylor1476e6d2015-02-24 20:49:35 +0000227public:
Reid Klecknerbcda1cd2015-04-29 22:49:54 +0000228 WinEHCloningDirectorBase(Function *HandlerFn, Value *ParentFP,
229 FrameVarInfoMap &VarInfo, LandingPadMap &LPadMap)
230 : Materializer(HandlerFn, ParentFP, VarInfo),
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000231 SelectorIDType(Type::getInt32Ty(HandlerFn->getContext())),
232 Int8PtrType(Type::getInt8PtrTy(HandlerFn->getContext())),
Reid Klecknerbcda1cd2015-04-29 22:49:54 +0000233 LPadMap(LPadMap), ParentFP(ParentFP) {}
Andrew Kaylor1476e6d2015-02-24 20:49:35 +0000234
235 CloningAction handleInstruction(ValueToValueMapTy &VMap,
236 const Instruction *Inst,
237 BasicBlock *NewBB) override;
238
Andrew Kaylorf0f5e462015-03-03 20:00:16 +0000239 virtual CloningAction handleBeginCatch(ValueToValueMapTy &VMap,
240 const Instruction *Inst,
241 BasicBlock *NewBB) = 0;
242 virtual CloningAction handleEndCatch(ValueToValueMapTy &VMap,
243 const Instruction *Inst,
244 BasicBlock *NewBB) = 0;
245 virtual CloningAction handleTypeIdFor(ValueToValueMapTy &VMap,
246 const Instruction *Inst,
247 BasicBlock *NewBB) = 0;
Andrew Kaylora6c5b962015-05-20 23:22:24 +0000248 virtual CloningAction handleIndirectBr(ValueToValueMapTy &VMap,
249 const IndirectBrInst *IBr,
250 BasicBlock *NewBB) = 0;
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000251 virtual CloningAction handleInvoke(ValueToValueMapTy &VMap,
252 const InvokeInst *Invoke,
253 BasicBlock *NewBB) = 0;
Andrew Kaylorf0f5e462015-03-03 20:00:16 +0000254 virtual CloningAction handleResume(ValueToValueMapTy &VMap,
255 const ResumeInst *Resume,
256 BasicBlock *NewBB) = 0;
Andrew Kaylorea8df612015-04-17 23:05:43 +0000257 virtual CloningAction handleCompare(ValueToValueMapTy &VMap,
258 const CmpInst *Compare,
259 BasicBlock *NewBB) = 0;
Andrew Kayloraa92ab02015-04-03 19:37:50 +0000260 virtual CloningAction handleLandingPad(ValueToValueMapTy &VMap,
261 const LandingPadInst *LPad,
262 BasicBlock *NewBB) = 0;
Andrew Kaylorf0f5e462015-03-03 20:00:16 +0000263
Andrew Kaylor1476e6d2015-02-24 20:49:35 +0000264 ValueMaterializer *getValueMaterializer() override { return &Materializer; }
265
Andrew Kaylorf0f5e462015-03-03 20:00:16 +0000266protected:
Andrew Kaylor1476e6d2015-02-24 20:49:35 +0000267 WinEHFrameVariableMaterializer Materializer;
268 Type *SelectorIDType;
269 Type *Int8PtrType;
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000270 LandingPadMap &LPadMap;
Reid Klecknerf14787d2015-04-22 00:07:52 +0000271
272 /// The value representing the parent frame pointer.
Reid Klecknerbcda1cd2015-04-29 22:49:54 +0000273 Value *ParentFP;
Andrew Kaylor1476e6d2015-02-24 20:49:35 +0000274};
Andrew Kaylorf0f5e462015-03-03 20:00:16 +0000275
276class WinEHCatchDirector : public WinEHCloningDirectorBase {
277public:
Andrew Kayloraa92ab02015-04-03 19:37:50 +0000278 WinEHCatchDirector(
Reid Klecknerbcda1cd2015-04-29 22:49:54 +0000279 Function *CatchFn, Value *ParentFP, Value *Selector,
280 FrameVarInfoMap &VarInfo, LandingPadMap &LPadMap,
Andrew Kaylora6c5b962015-05-20 23:22:24 +0000281 DenseMap<LandingPadInst *, const LandingPadInst *> &NestedLPads,
282 DominatorTree *DT, SmallPtrSetImpl<BasicBlock *> &EHBlocks)
Reid Klecknerbcda1cd2015-04-29 22:49:54 +0000283 : WinEHCloningDirectorBase(CatchFn, ParentFP, VarInfo, LPadMap),
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000284 CurrentSelector(Selector->stripPointerCasts()),
Andrew Kaylora6c5b962015-05-20 23:22:24 +0000285 ExceptionObjectVar(nullptr), NestedLPtoOriginalLP(NestedLPads),
286 DT(DT), EHBlocks(EHBlocks) {}
Andrew Kaylorf0f5e462015-03-03 20:00:16 +0000287
288 CloningAction handleBeginCatch(ValueToValueMapTy &VMap,
289 const Instruction *Inst,
290 BasicBlock *NewBB) override;
291 CloningAction handleEndCatch(ValueToValueMapTy &VMap, const Instruction *Inst,
292 BasicBlock *NewBB) override;
293 CloningAction handleTypeIdFor(ValueToValueMapTy &VMap,
294 const Instruction *Inst,
295 BasicBlock *NewBB) override;
Andrew Kaylora6c5b962015-05-20 23:22:24 +0000296 CloningAction handleIndirectBr(ValueToValueMapTy &VMap,
297 const IndirectBrInst *IBr,
298 BasicBlock *NewBB) override;
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000299 CloningAction handleInvoke(ValueToValueMapTy &VMap, const InvokeInst *Invoke,
300 BasicBlock *NewBB) override;
Andrew Kaylorf0f5e462015-03-03 20:00:16 +0000301 CloningAction handleResume(ValueToValueMapTy &VMap, const ResumeInst *Resume,
302 BasicBlock *NewBB) override;
Andrew Kaylor91307432015-04-28 22:01:51 +0000303 CloningAction handleCompare(ValueToValueMapTy &VMap, const CmpInst *Compare,
304 BasicBlock *NewBB) override;
Andrew Kayloraa92ab02015-04-03 19:37:50 +0000305 CloningAction handleLandingPad(ValueToValueMapTy &VMap,
306 const LandingPadInst *LPad,
307 BasicBlock *NewBB) override;
Andrew Kaylorf0f5e462015-03-03 20:00:16 +0000308
Reid Kleckner3567d272015-04-02 21:13:31 +0000309 Value *getExceptionVar() { return ExceptionObjectVar; }
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000310 TinyPtrVector<BasicBlock *> &getReturnTargets() { return ReturnTargets; }
311
Andrew Kaylorf0f5e462015-03-03 20:00:16 +0000312private:
313 Value *CurrentSelector;
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000314
Reid Kleckner3567d272015-04-02 21:13:31 +0000315 Value *ExceptionObjectVar;
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000316 TinyPtrVector<BasicBlock *> ReturnTargets;
Andrew Kayloraa92ab02015-04-03 19:37:50 +0000317
318 // This will be a reference to the field of the same name in the WinEHPrepare
319 // object which instantiates this WinEHCatchDirector object.
320 DenseMap<LandingPadInst *, const LandingPadInst *> &NestedLPtoOriginalLP;
Andrew Kaylora6c5b962015-05-20 23:22:24 +0000321 DominatorTree *DT;
322 SmallPtrSetImpl<BasicBlock *> &EHBlocks;
Andrew Kaylorf0f5e462015-03-03 20:00:16 +0000323};
324
325class WinEHCleanupDirector : public WinEHCloningDirectorBase {
326public:
Reid Klecknerbcda1cd2015-04-29 22:49:54 +0000327 WinEHCleanupDirector(Function *CleanupFn, Value *ParentFP,
328 FrameVarInfoMap &VarInfo, LandingPadMap &LPadMap)
329 : WinEHCloningDirectorBase(CleanupFn, ParentFP, VarInfo,
330 LPadMap) {}
Andrew Kaylorf0f5e462015-03-03 20:00:16 +0000331
332 CloningAction handleBeginCatch(ValueToValueMapTy &VMap,
333 const Instruction *Inst,
334 BasicBlock *NewBB) override;
335 CloningAction handleEndCatch(ValueToValueMapTy &VMap, const Instruction *Inst,
336 BasicBlock *NewBB) override;
337 CloningAction handleTypeIdFor(ValueToValueMapTy &VMap,
338 const Instruction *Inst,
339 BasicBlock *NewBB) override;
Andrew Kaylora6c5b962015-05-20 23:22:24 +0000340 CloningAction handleIndirectBr(ValueToValueMapTy &VMap,
341 const IndirectBrInst *IBr,
342 BasicBlock *NewBB) override;
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000343 CloningAction handleInvoke(ValueToValueMapTy &VMap, const InvokeInst *Invoke,
344 BasicBlock *NewBB) override;
Andrew Kaylorf0f5e462015-03-03 20:00:16 +0000345 CloningAction handleResume(ValueToValueMapTy &VMap, const ResumeInst *Resume,
346 BasicBlock *NewBB) override;
Andrew Kaylor91307432015-04-28 22:01:51 +0000347 CloningAction handleCompare(ValueToValueMapTy &VMap, const CmpInst *Compare,
348 BasicBlock *NewBB) override;
Andrew Kayloraa92ab02015-04-03 19:37:50 +0000349 CloningAction handleLandingPad(ValueToValueMapTy &VMap,
350 const LandingPadInst *LPad,
351 BasicBlock *NewBB) override;
Andrew Kaylorf0f5e462015-03-03 20:00:16 +0000352};
353
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000354class LandingPadActions {
355public:
356 LandingPadActions() : HasCleanupHandlers(false) {}
357
358 void insertCatchHandler(CatchHandler *Action) { Actions.push_back(Action); }
359 void insertCleanupHandler(CleanupHandler *Action) {
360 Actions.push_back(Action);
361 HasCleanupHandlers = true;
362 }
363
364 bool includesCleanup() const { return HasCleanupHandlers; }
365
David Majnemercde33032015-03-30 22:58:10 +0000366 SmallVectorImpl<ActionHandler *> &actions() { return Actions; }
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000367 SmallVectorImpl<ActionHandler *>::iterator begin() { return Actions.begin(); }
368 SmallVectorImpl<ActionHandler *>::iterator end() { return Actions.end(); }
369
370private:
371 // Note that this class does not own the ActionHandler objects in this vector.
372 // The ActionHandlers are owned by the CatchHandlerMap and CleanupHandlerMap
373 // in the WinEHPrepare class.
374 SmallVector<ActionHandler *, 4> Actions;
375 bool HasCleanupHandlers;
376};
377
Andrew Kaylor1476e6d2015-02-24 20:49:35 +0000378} // end anonymous namespace
379
380char WinEHPrepare::ID = 0;
Reid Kleckner47c8e7a2015-03-12 00:36:20 +0000381INITIALIZE_TM_PASS(WinEHPrepare, "winehprepare", "Prepare Windows exceptions",
382 false, false)
Andrew Kaylor1476e6d2015-02-24 20:49:35 +0000383
384FunctionPass *llvm::createWinEHPass(const TargetMachine *TM) {
385 return new WinEHPrepare(TM);
386}
387
Andrew Kaylor1476e6d2015-02-24 20:49:35 +0000388bool WinEHPrepare::runOnFunction(Function &Fn) {
David Majnemerfd9f4772015-08-11 01:15:26 +0000389 if (!Fn.hasPersonalityFn())
Andrew Kaylor1476e6d2015-02-24 20:49:35 +0000390 return false;
391
David Majnemerfd9f4772015-08-11 01:15:26 +0000392 // No need to prepare outlined handlers.
393 if (Fn.hasFnAttribute("wineh-parent"))
David Majnemer09e1fdb2015-08-06 21:13:51 +0000394 return false;
395
Andrew Kaylor1476e6d2015-02-24 20:49:35 +0000396 // Classify the personality to see what kind of preparation we need.
David Majnemer7fddecc2015-06-17 20:52:32 +0000397 Personality = classifyEHPersonality(Fn.getPersonalityFn());
Andrew Kaylor1476e6d2015-02-24 20:49:35 +0000398
Reid Kleckner47c8e7a2015-03-12 00:36:20 +0000399 // Do nothing if this is not an MSVC personality.
Reid Kleckner0f9e27a2015-03-18 20:26:53 +0000400 if (!isMSVCEHPersonality(Personality))
Reid Kleckner47c8e7a2015-03-12 00:36:20 +0000401 return false;
Andrew Kaylor1476e6d2015-02-24 20:49:35 +0000402
David Majnemerfd9f4772015-08-11 01:15:26 +0000403 SmallVector<LandingPadInst *, 4> LPads;
404 SmallVector<ResumeInst *, 4> Resumes;
Joseph Tremouletec182852015-08-28 01:12:35 +0000405 SmallVector<BasicBlock *, 4> EntryBlocks;
David Majnemerfd9f4772015-08-11 01:15:26 +0000406 bool ForExplicitEH = false;
407 for (BasicBlock &BB : Fn) {
Joseph Tremouletec182852015-08-28 01:12:35 +0000408 Instruction *First = BB.getFirstNonPHI();
409 if (auto *LP = dyn_cast<LandingPadInst>(First)) {
David Majnemerfd9f4772015-08-11 01:15:26 +0000410 LPads.push_back(LP);
Joseph Tremouletec182852015-08-28 01:12:35 +0000411 } else if (First->isEHPad()) {
412 if (!ForExplicitEH)
413 EntryBlocks.push_back(&Fn.getEntryBlock());
Joseph Tremoulet9ce71f72015-09-03 09:09:43 +0000414 if (!isa<CatchEndPadInst>(First) && !isa<CleanupEndPadInst>(First))
Joseph Tremouletec182852015-08-28 01:12:35 +0000415 EntryBlocks.push_back(&BB);
David Majnemerfd9f4772015-08-11 01:15:26 +0000416 ForExplicitEH = true;
David Majnemerfd9f4772015-08-11 01:15:26 +0000417 }
418 if (auto *Resume = dyn_cast<ResumeInst>(BB.getTerminator()))
419 Resumes.push_back(Resume);
420 }
421
422 if (ForExplicitEH)
Joseph Tremouletec182852015-08-28 01:12:35 +0000423 return prepareExplicitEH(Fn, EntryBlocks);
David Majnemerfd9f4772015-08-11 01:15:26 +0000424
425 // No need to prepare functions that lack landing pads.
426 if (LPads.empty())
427 return false;
428
Andrew Kaylor64622aa2015-04-01 17:21:25 +0000429 DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
Reid Klecknerf12c0302015-06-09 21:42:19 +0000430 LibInfo = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
Andrew Kaylor64622aa2015-04-01 17:21:25 +0000431
Reid Kleckner0f9e27a2015-03-18 20:26:53 +0000432 // If there were any landing pads, prepareExceptionHandlers will make changes.
433 prepareExceptionHandlers(Fn, LPads);
Andrew Kaylor1476e6d2015-02-24 20:49:35 +0000434 return true;
435}
436
Andrew Kayloraa92ab02015-04-03 19:37:50 +0000437bool WinEHPrepare::doFinalization(Module &M) { return false; }
Andrew Kaylor1476e6d2015-02-24 20:49:35 +0000438
Andrew Kaylor64622aa2015-04-01 17:21:25 +0000439void WinEHPrepare::getAnalysisUsage(AnalysisUsage &AU) const {
440 AU.addRequired<DominatorTreeWrapperPass>();
Reid Klecknerf12c0302015-06-09 21:42:19 +0000441 AU.addRequired<TargetLibraryInfoWrapperPass>();
Andrew Kaylor64622aa2015-04-01 17:21:25 +0000442}
Andrew Kaylor1476e6d2015-02-24 20:49:35 +0000443
Reid Klecknerfd7df282015-04-22 21:05:21 +0000444static bool isSelectorDispatch(BasicBlock *BB, BasicBlock *&CatchHandler,
445 Constant *&Selector, BasicBlock *&NextBB);
446
447// Finds blocks reachable from the starting set Worklist. Does not follow unwind
448// edges or blocks listed in StopPoints.
449static void findReachableBlocks(SmallPtrSetImpl<BasicBlock *> &ReachableBBs,
450 SetVector<BasicBlock *> &Worklist,
451 const SetVector<BasicBlock *> *StopPoints) {
452 while (!Worklist.empty()) {
453 BasicBlock *BB = Worklist.pop_back_val();
454
455 // Don't cross blocks that we should stop at.
456 if (StopPoints && StopPoints->count(BB))
457 continue;
458
459 if (!ReachableBBs.insert(BB).second)
460 continue; // Already visited.
461
462 // Don't follow unwind edges of invokes.
463 if (auto *II = dyn_cast<InvokeInst>(BB->getTerminator())) {
464 Worklist.insert(II->getNormalDest());
465 continue;
466 }
467
468 // Otherwise, follow all successors.
469 Worklist.insert(succ_begin(BB), succ_end(BB));
470 }
471}
472
Andrew Kaylor046f7b42015-04-28 21:54:14 +0000473// Attempt to find an instruction where a block can be split before
474// a call to llvm.eh.begincatch and its operands. If the block
475// begins with the begincatch call or one of its adjacent operands
476// the block will not be split.
477static Instruction *findBeginCatchSplitPoint(BasicBlock *BB,
478 IntrinsicInst *II) {
479 // If the begincatch call is already the first instruction in the block,
480 // don't split.
481 Instruction *FirstNonPHI = BB->getFirstNonPHI();
482 if (II == FirstNonPHI)
483 return nullptr;
484
485 // If either operand is in the same basic block as the instruction and
486 // isn't used by another instruction before the begincatch call, include it
487 // in the split block.
488 auto *Op0 = dyn_cast<Instruction>(II->getOperand(0));
489 auto *Op1 = dyn_cast<Instruction>(II->getOperand(1));
490
491 Instruction *I = II->getPrevNode();
492 Instruction *LastI = II;
493
494 while (I == Op0 || I == Op1) {
495 // If the block begins with one of the operands and there are no other
496 // instructions between the operand and the begincatch call, don't split.
497 if (I == FirstNonPHI)
498 return nullptr;
499
500 LastI = I;
501 I = I->getPrevNode();
502 }
503
504 // If there is at least one instruction in the block before the begincatch
505 // call and its operands, split the block at either the begincatch or
506 // its operand.
507 return LastI;
508}
509
Reid Klecknerfd7df282015-04-22 21:05:21 +0000510/// Find all points where exceptional control rejoins normal control flow via
511/// llvm.eh.endcatch. Add them to the normal bb reachability worklist.
Andrew Kaylor046f7b42015-04-28 21:54:14 +0000512void WinEHPrepare::findCXXEHReturnPoints(
513 Function &F, SetVector<BasicBlock *> &EHReturnBlocks) {
Reid Klecknerfd7df282015-04-22 21:05:21 +0000514 for (auto BBI = F.begin(), BBE = F.end(); BBI != BBE; ++BBI) {
515 BasicBlock *BB = BBI;
516 for (Instruction &I : *BB) {
Andrew Kaylor046f7b42015-04-28 21:54:14 +0000517 if (match(&I, m_Intrinsic<Intrinsic::eh_begincatch>())) {
Andrew Kaylor91307432015-04-28 22:01:51 +0000518 Instruction *SplitPt =
Andrew Kaylor046f7b42015-04-28 21:54:14 +0000519 findBeginCatchSplitPoint(BB, cast<IntrinsicInst>(&I));
520 if (SplitPt) {
521 // Split the block before the llvm.eh.begincatch call to allow
522 // cleanup and catch code to be distinguished later.
523 // Do not update BBI because we still need to process the
524 // portion of the block that we are splitting off.
Andrew Kaylora33f1592015-04-29 17:21:26 +0000525 SplitBlock(BB, SplitPt, DT);
Andrew Kaylor046f7b42015-04-28 21:54:14 +0000526 break;
527 }
528 }
Reid Klecknerfd7df282015-04-22 21:05:21 +0000529 if (match(&I, m_Intrinsic<Intrinsic::eh_endcatch>())) {
530 // Split the block after the call to llvm.eh.endcatch if there is
531 // anything other than an unconditional branch, or if the successor
532 // starts with a phi.
533 auto *Br = dyn_cast<BranchInst>(I.getNextNode());
534 if (!Br || !Br->isUnconditional() ||
535 isa<PHINode>(Br->getSuccessor(0)->begin())) {
536 DEBUG(dbgs() << "splitting block " << BB->getName()
537 << " with llvm.eh.endcatch\n");
Andrew Kaylor046f7b42015-04-28 21:54:14 +0000538 BBI = SplitBlock(BB, I.getNextNode(), DT);
Reid Klecknerfd7df282015-04-22 21:05:21 +0000539 }
540 // The next BB is normal control flow.
541 EHReturnBlocks.insert(BB->getTerminator()->getSuccessor(0));
542 break;
543 }
544 }
545 }
546}
547
548static bool isCatchAllLandingPad(const BasicBlock *BB) {
549 const LandingPadInst *LP = BB->getLandingPadInst();
550 if (!LP)
551 return false;
552 unsigned N = LP->getNumClauses();
553 return (N > 0 && LP->isCatch(N - 1) &&
554 isa<ConstantPointerNull>(LP->getClause(N - 1)));
555}
556
557/// Find all points where exceptions control rejoins normal control flow via
558/// selector dispatch.
Andrew Kaylor046f7b42015-04-28 21:54:14 +0000559void WinEHPrepare::findSEHEHReturnPoints(
560 Function &F, SetVector<BasicBlock *> &EHReturnBlocks) {
Reid Klecknerfd7df282015-04-22 21:05:21 +0000561 for (auto BBI = F.begin(), BBE = F.end(); BBI != BBE; ++BBI) {
562 BasicBlock *BB = BBI;
563 // If the landingpad is a catch-all, treat the whole lpad as if it is
564 // reachable from normal control flow.
565 // FIXME: This is imprecise. We need a better way of identifying where a
566 // catch-all starts and cleanups stop. As far as LLVM is concerned, there
567 // is no difference.
568 if (isCatchAllLandingPad(BB)) {
569 EHReturnBlocks.insert(BB);
570 continue;
571 }
572
573 BasicBlock *CatchHandler;
574 BasicBlock *NextBB;
575 Constant *Selector;
576 if (isSelectorDispatch(BB, CatchHandler, Selector, NextBB)) {
Reid Klecknered012db2015-07-08 18:08:52 +0000577 // Split the edge if there are multiple predecessors. This creates a place
578 // where we can insert EH recovery code.
579 if (!CatchHandler->getSinglePredecessor()) {
Reid Klecknerfd7df282015-04-22 21:05:21 +0000580 DEBUG(dbgs() << "splitting EH return edge from " << BB->getName()
581 << " to " << CatchHandler->getName() << '\n');
582 BBI = CatchHandler = SplitCriticalEdge(
583 BB, std::find(succ_begin(BB), succ_end(BB), CatchHandler));
584 }
585 EHReturnBlocks.insert(CatchHandler);
586 }
587 }
588}
589
Andrew Kaylora6c5b962015-05-20 23:22:24 +0000590void WinEHPrepare::identifyEHBlocks(Function &F,
591 SmallVectorImpl<LandingPadInst *> &LPads) {
Reid Klecknerfd7df282015-04-22 21:05:21 +0000592 DEBUG(dbgs() << "Demoting values live across exception handlers in function "
593 << F.getName() << '\n');
594
595 // Build a set of all non-exceptional blocks and exceptional blocks.
596 // - Non-exceptional blocks are blocks reachable from the entry block while
597 // not following invoke unwind edges.
598 // - Exceptional blocks are blocks reachable from landingpads. Analysis does
599 // not follow llvm.eh.endcatch blocks, which mark a transition from
600 // exceptional to normal control.
Reid Klecknerfd7df282015-04-22 21:05:21 +0000601
602 if (Personality == EHPersonality::MSVC_CXX)
603 findCXXEHReturnPoints(F, EHReturnBlocks);
604 else
605 findSEHEHReturnPoints(F, EHReturnBlocks);
606
607 DEBUG({
608 dbgs() << "identified the following blocks as EH return points:\n";
609 for (BasicBlock *BB : EHReturnBlocks)
610 dbgs() << " " << BB->getName() << '\n';
611 });
612
Andrew Kaylor91307432015-04-28 22:01:51 +0000613// Join points should not have phis at this point, unless they are a
614// landingpad, in which case we will demote their phis later.
Reid Klecknerfd7df282015-04-22 21:05:21 +0000615#ifndef NDEBUG
616 for (BasicBlock *BB : EHReturnBlocks)
617 assert((BB->isLandingPad() || !isa<PHINode>(BB->begin())) &&
618 "non-lpad EH return block has phi");
619#endif
620
621 // Normal blocks are the blocks reachable from the entry block and all EH
622 // return points.
Andrew Kaylora6c5b962015-05-20 23:22:24 +0000623 SetVector<BasicBlock *> Worklist;
Reid Klecknerfd7df282015-04-22 21:05:21 +0000624 Worklist = EHReturnBlocks;
625 Worklist.insert(&F.getEntryBlock());
626 findReachableBlocks(NormalBlocks, Worklist, nullptr);
627 DEBUG({
628 dbgs() << "marked the following blocks as normal:\n";
629 for (BasicBlock *BB : NormalBlocks)
630 dbgs() << " " << BB->getName() << '\n';
631 });
632
633 // Exceptional blocks are the blocks reachable from landingpads that don't
634 // cross EH return points.
635 Worklist.clear();
636 for (auto *LPI : LPads)
637 Worklist.insert(LPI->getParent());
638 findReachableBlocks(EHBlocks, Worklist, &EHReturnBlocks);
639 DEBUG({
640 dbgs() << "marked the following blocks as exceptional:\n";
641 for (BasicBlock *BB : EHBlocks)
642 dbgs() << " " << BB->getName() << '\n';
643 });
644
Andrew Kaylora6c5b962015-05-20 23:22:24 +0000645}
646
647/// Ensure that all values live into and out of exception handlers are stored
648/// in memory.
649/// FIXME: This falls down when values are defined in one handler and live into
650/// another handler. For example, a cleanup defines a value used only by a
651/// catch handler.
652void WinEHPrepare::demoteValuesLiveAcrossHandlers(
653 Function &F, SmallVectorImpl<LandingPadInst *> &LPads) {
654 DEBUG(dbgs() << "Demoting values live across exception handlers in function "
655 << F.getName() << '\n');
656
657 // identifyEHBlocks() should have been called before this function.
658 assert(!NormalBlocks.empty());
659
Reid Kleckner7ea77082015-07-10 22:21:54 +0000660 // Try to avoid demoting EH pointer and selector values. They get in the way
661 // of our pattern matching.
662 SmallPtrSet<Instruction *, 10> EHVals;
663 for (BasicBlock &BB : F) {
664 LandingPadInst *LP = BB.getLandingPadInst();
665 if (!LP)
666 continue;
667 EHVals.insert(LP);
668 for (User *U : LP->users()) {
669 auto *EI = dyn_cast<ExtractValueInst>(U);
670 if (!EI)
671 continue;
672 EHVals.insert(EI);
673 for (User *U2 : EI->users()) {
674 if (auto *PN = dyn_cast<PHINode>(U2))
675 EHVals.insert(PN);
676 }
677 }
678 }
679
Reid Klecknerfd7df282015-04-22 21:05:21 +0000680 SetVector<Argument *> ArgsToDemote;
681 SetVector<Instruction *> InstrsToDemote;
682 for (BasicBlock &BB : F) {
683 bool IsNormalBB = NormalBlocks.count(&BB);
684 bool IsEHBB = EHBlocks.count(&BB);
685 if (!IsNormalBB && !IsEHBB)
686 continue; // Blocks that are neither normal nor EH are unreachable.
687 for (Instruction &I : BB) {
688 for (Value *Op : I.operands()) {
689 // Don't demote static allocas, constants, and labels.
690 if (isa<Constant>(Op) || isa<BasicBlock>(Op) || isa<InlineAsm>(Op))
691 continue;
692 auto *AI = dyn_cast<AllocaInst>(Op);
693 if (AI && AI->isStaticAlloca())
694 continue;
695
696 if (auto *Arg = dyn_cast<Argument>(Op)) {
697 if (IsEHBB) {
698 DEBUG(dbgs() << "Demoting argument " << *Arg
699 << " used by EH instr: " << I << "\n");
700 ArgsToDemote.insert(Arg);
701 }
702 continue;
703 }
704
Reid Kleckner7ea77082015-07-10 22:21:54 +0000705 // Don't demote EH values.
Reid Klecknerfd7df282015-04-22 21:05:21 +0000706 auto *OpI = cast<Instruction>(Op);
Reid Kleckner7ea77082015-07-10 22:21:54 +0000707 if (EHVals.count(OpI))
708 continue;
709
Reid Klecknerfd7df282015-04-22 21:05:21 +0000710 BasicBlock *OpBB = OpI->getParent();
711 // If a value is produced and consumed in the same BB, we don't need to
712 // demote it.
713 if (OpBB == &BB)
714 continue;
715 bool IsOpNormalBB = NormalBlocks.count(OpBB);
716 bool IsOpEHBB = EHBlocks.count(OpBB);
717 if (IsNormalBB != IsOpNormalBB || IsEHBB != IsOpEHBB) {
718 DEBUG({
719 dbgs() << "Demoting instruction live in-out from EH:\n";
720 dbgs() << "Instr: " << *OpI << '\n';
721 dbgs() << "User: " << I << '\n';
722 });
723 InstrsToDemote.insert(OpI);
724 }
725 }
726 }
727 }
728
729 // Demote values live into and out of handlers.
730 // FIXME: This demotion is inefficient. We should insert spills at the point
731 // of definition, insert one reload in each handler that uses the value, and
732 // insert reloads in the BB used to rejoin normal control flow.
733 Instruction *AllocaInsertPt = F.getEntryBlock().getFirstInsertionPt();
734 for (Instruction *I : InstrsToDemote)
735 DemoteRegToStack(*I, false, AllocaInsertPt);
736
737 // Demote arguments separately, and only for uses in EH blocks.
738 for (Argument *Arg : ArgsToDemote) {
739 auto *Slot = new AllocaInst(Arg->getType(), nullptr,
740 Arg->getName() + ".reg2mem", AllocaInsertPt);
741 SmallVector<User *, 4> Users(Arg->user_begin(), Arg->user_end());
742 for (User *U : Users) {
743 auto *I = dyn_cast<Instruction>(U);
744 if (I && EHBlocks.count(I->getParent())) {
745 auto *Reload = new LoadInst(Slot, Arg->getName() + ".reload", false, I);
746 U->replaceUsesOfWith(Arg, Reload);
747 }
748 }
749 new StoreInst(Arg, Slot, AllocaInsertPt);
750 }
751
752 // Demote landingpad phis, as the landingpad will be removed from the machine
753 // CFG.
754 for (LandingPadInst *LPI : LPads) {
755 BasicBlock *BB = LPI->getParent();
756 while (auto *Phi = dyn_cast<PHINode>(BB->begin()))
757 DemotePHIToStack(Phi, AllocaInsertPt);
758 }
759
760 DEBUG(dbgs() << "Demoted " << InstrsToDemote.size() << " instructions and "
761 << ArgsToDemote.size() << " arguments for WinEHPrepare\n\n");
762}
763
Reid Kleckner0f9e27a2015-03-18 20:26:53 +0000764bool WinEHPrepare::prepareExceptionHandlers(
Andrew Kaylor1476e6d2015-02-24 20:49:35 +0000765 Function &F, SmallVectorImpl<LandingPadInst *> &LPads) {
Reid Klecknerfd7df282015-04-22 21:05:21 +0000766 // Don't run on functions that are already prepared.
767 for (LandingPadInst *LPad : LPads) {
768 BasicBlock *LPadBB = LPad->getParent();
769 for (Instruction &Inst : *LPadBB)
770 if (match(&Inst, m_Intrinsic<Intrinsic::eh_actions>()))
771 return false;
772 }
773
Andrew Kaylora6c5b962015-05-20 23:22:24 +0000774 identifyEHBlocks(F, LPads);
Reid Klecknerfd7df282015-04-22 21:05:21 +0000775 demoteValuesLiveAcrossHandlers(F, LPads);
776
Andrew Kaylor1476e6d2015-02-24 20:49:35 +0000777 // These containers are used to re-map frame variables that are used in
778 // outlined catch and cleanup handlers. They will be populated as the
779 // handlers are outlined.
780 FrameVarInfoMap FrameVarInfo;
Andrew Kaylor1476e6d2015-02-24 20:49:35 +0000781
782 bool HandlersOutlined = false;
783
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000784 Module *M = F.getParent();
785 LLVMContext &Context = M->getContext();
786
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000787 // Create a new function to receive the handler contents.
788 PointerType *Int8PtrType = Type::getInt8PtrTy(Context);
789 Type *Int32Type = Type::getInt32Ty(Context);
Reid Kleckner52b07792015-03-12 01:45:37 +0000790 Function *ActionIntrin = Intrinsic::getDeclaration(M, Intrinsic::eh_actions);
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000791
Reid Klecknercfbfe6f2015-04-24 20:25:05 +0000792 if (isAsynchronousEHPersonality(Personality)) {
793 // FIXME: Switch the ehptr type to i32 and then switch this.
794 SEHExceptionCodeSlot =
795 new AllocaInst(Int8PtrType, nullptr, "seh_exception_code",
796 F.getEntryBlock().getFirstInsertionPt());
797 }
798
Andrew Kaylora6c5b962015-05-20 23:22:24 +0000799 // In order to handle the case where one outlined catch handler returns
800 // to a block within another outlined catch handler that would otherwise
801 // be unreachable, we need to outline the nested landing pad before we
802 // outline the landing pad which encloses it.
Manuel Klimekb00d42c2015-05-21 15:38:25 +0000803 if (!isAsynchronousEHPersonality(Personality))
804 std::sort(LPads.begin(), LPads.end(),
805 [this](LandingPadInst *const &L, LandingPadInst *const &R) {
806 return DT->properlyDominates(R->getParent(), L->getParent());
Andrew Kaylora6c5b962015-05-20 23:22:24 +0000807 });
808
Andrew Kaylorcc14f382015-05-11 23:06:02 +0000809 // This container stores the llvm.eh.recover and IndirectBr instructions
810 // that make up the body of each landing pad after it has been outlined.
811 // We need to defer the population of the target list for the indirectbr
812 // until all landing pads have been outlined so that we can handle the
813 // case of blocks in the target that are reached only from nested
814 // landing pads.
815 SmallVector<std::pair<CallInst*, IndirectBrInst *>, 4> LPadImpls;
816
Andrew Kaylor1476e6d2015-02-24 20:49:35 +0000817 for (LandingPadInst *LPad : LPads) {
818 // Look for evidence that this landingpad has already been processed.
819 bool LPadHasActionList = false;
820 BasicBlock *LPadBB = LPad->getParent();
Reid Klecknerc759fe92015-03-19 22:31:02 +0000821 for (Instruction &Inst : *LPadBB) {
Reid Klecknerfd7df282015-04-22 21:05:21 +0000822 if (match(&Inst, m_Intrinsic<Intrinsic::eh_actions>())) {
823 LPadHasActionList = true;
824 break;
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000825 }
Andrew Kaylor1476e6d2015-02-24 20:49:35 +0000826 }
827
828 // If we've already outlined the handlers for this landingpad,
829 // there's nothing more to do here.
830 if (LPadHasActionList)
831 continue;
832
Andrew Kaylor64622aa2015-04-01 17:21:25 +0000833 // If either of the values in the aggregate returned by the landing pad is
834 // extracted and stored to memory, promote the stored value to a register.
835 promoteLandingPadValues(LPad);
836
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000837 LandingPadActions Actions;
838 mapLandingPadBlocks(LPad, Actions);
Andrew Kaylorf0f5e462015-03-03 20:00:16 +0000839
Reid Kleckner9405ef02015-04-10 23:12:29 +0000840 HandlersOutlined |= !Actions.actions().empty();
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000841 for (ActionHandler *Action : Actions) {
Reid Kleckner0f9e27a2015-03-18 20:26:53 +0000842 if (Action->hasBeenProcessed())
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000843 continue;
844 BasicBlock *StartBB = Action->getStartBlock();
Reid Kleckner0f9e27a2015-03-18 20:26:53 +0000845
846 // SEH doesn't do any outlining for catches. Instead, pass the handler
847 // basic block addr to llvm.eh.actions and list the block as a return
848 // target.
849 if (isAsynchronousEHPersonality(Personality)) {
850 if (auto *CatchAction = dyn_cast<CatchHandler>(Action)) {
851 processSEHCatchHandler(CatchAction, StartBB);
Reid Kleckner0f9e27a2015-03-18 20:26:53 +0000852 continue;
853 }
854 }
855
Reid Kleckner9405ef02015-04-10 23:12:29 +0000856 outlineHandler(Action, &F, LPad, StartBB, FrameVarInfo);
857 }
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000858
Reid Kleckner2c3ccaa2015-04-24 16:22:19 +0000859 // Split the block after the landingpad instruction so that it is just a
860 // call to llvm.eh.actions followed by indirectbr.
861 assert(!isa<PHINode>(LPadBB->begin()) && "lpad phi not removed");
Andrew Kaylor046f7b42015-04-28 21:54:14 +0000862 SplitBlock(LPadBB, LPad->getNextNode(), DT);
Reid Kleckner2c3ccaa2015-04-24 16:22:19 +0000863 // Erase the branch inserted by the split so we can insert indirectbr.
864 LPadBB->getTerminator()->eraseFromParent();
Andrew Kayloraa92ab02015-04-03 19:37:50 +0000865
Reid Klecknere3af86e2015-04-23 21:22:30 +0000866 // Replace all extracted values with undef and ultimately replace the
867 // landingpad with undef.
Reid Klecknercfbfe6f2015-04-24 20:25:05 +0000868 SmallVector<Instruction *, 4> SEHCodeUses;
869 SmallVector<Instruction *, 4> EHUndefs;
Reid Klecknere3af86e2015-04-23 21:22:30 +0000870 for (User *U : LPad->users()) {
871 auto *E = dyn_cast<ExtractValueInst>(U);
872 if (!E)
873 continue;
874 assert(E->getNumIndices() == 1 &&
875 "Unexpected operation: extracting both landing pad values");
Reid Klecknercfbfe6f2015-04-24 20:25:05 +0000876 unsigned Idx = *E->idx_begin();
877 assert((Idx == 0 || Idx == 1) && "unexpected index");
878 if (Idx == 0 && isAsynchronousEHPersonality(Personality))
879 SEHCodeUses.push_back(E);
880 else
881 EHUndefs.push_back(E);
Reid Klecknere3af86e2015-04-23 21:22:30 +0000882 }
Reid Klecknercfbfe6f2015-04-24 20:25:05 +0000883 for (Instruction *E : EHUndefs) {
Reid Klecknere3af86e2015-04-23 21:22:30 +0000884 E->replaceAllUsesWith(UndefValue::get(E->getType()));
885 E->eraseFromParent();
886 }
887 LPad->replaceAllUsesWith(UndefValue::get(LPad->getType()));
Reid Kleckner0f9e27a2015-03-18 20:26:53 +0000888
Reid Klecknercfbfe6f2015-04-24 20:25:05 +0000889 // Rewrite uses of the exception pointer to loads of an alloca.
Reid Kleckner7ea77082015-07-10 22:21:54 +0000890 while (!SEHCodeUses.empty()) {
891 Instruction *E = SEHCodeUses.pop_back_val();
Reid Klecknercfbfe6f2015-04-24 20:25:05 +0000892 SmallVector<Use *, 4> Uses;
893 for (Use &U : E->uses())
894 Uses.push_back(&U);
895 for (Use *U : Uses) {
896 auto *I = cast<Instruction>(U->getUser());
897 if (isa<ResumeInst>(I))
898 continue;
Reid Klecknercfbfe6f2015-04-24 20:25:05 +0000899 if (auto *Phi = dyn_cast<PHINode>(I))
Reid Kleckner7ea77082015-07-10 22:21:54 +0000900 SEHCodeUses.push_back(Phi);
Reid Klecknercfbfe6f2015-04-24 20:25:05 +0000901 else
Reid Kleckner7ea77082015-07-10 22:21:54 +0000902 U->set(new LoadInst(SEHExceptionCodeSlot, "sehcode", false, I));
Reid Klecknercfbfe6f2015-04-24 20:25:05 +0000903 }
904 E->replaceAllUsesWith(UndefValue::get(E->getType()));
905 E->eraseFromParent();
906 }
907
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000908 // Add a call to describe the actions for this landing pad.
909 std::vector<Value *> ActionArgs;
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000910 for (ActionHandler *Action : Actions) {
Reid Klecknerc759fe92015-03-19 22:31:02 +0000911 // Action codes from docs are: 0 cleanup, 1 catch.
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000912 if (auto *CatchAction = dyn_cast<CatchHandler>(Action)) {
Reid Klecknerc759fe92015-03-19 22:31:02 +0000913 ActionArgs.push_back(ConstantInt::get(Int32Type, 1));
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000914 ActionArgs.push_back(CatchAction->getSelector());
Reid Kleckner3567d272015-04-02 21:13:31 +0000915 // Find the frame escape index of the exception object alloca in the
916 // parent.
917 int FrameEscapeIdx = -1;
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000918 Value *EHObj = const_cast<Value *>(CatchAction->getExceptionVar());
Reid Kleckner3567d272015-04-02 21:13:31 +0000919 if (EHObj && !isa<ConstantPointerNull>(EHObj)) {
920 auto I = FrameVarInfo.find(EHObj);
921 assert(I != FrameVarInfo.end() &&
922 "failed to map llvm.eh.begincatch var");
923 FrameEscapeIdx = std::distance(FrameVarInfo.begin(), I);
924 }
925 ActionArgs.push_back(ConstantInt::get(Int32Type, FrameEscapeIdx));
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000926 } else {
Reid Klecknerc759fe92015-03-19 22:31:02 +0000927 ActionArgs.push_back(ConstantInt::get(Int32Type, 0));
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000928 }
Reid Klecknerc759fe92015-03-19 22:31:02 +0000929 ActionArgs.push_back(Action->getHandlerBlockOrFunc());
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000930 }
931 CallInst *Recover =
Reid Kleckner2c3ccaa2015-04-24 16:22:19 +0000932 CallInst::Create(ActionIntrin, ActionArgs, "recover", LPadBB);
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000933
Andrew Kaylora6c5b962015-05-20 23:22:24 +0000934 SetVector<BasicBlock *> ReturnTargets;
935 for (ActionHandler *Action : Actions) {
936 if (auto *CatchAction = dyn_cast<CatchHandler>(Action)) {
937 const auto &CatchTargets = CatchAction->getReturnTargets();
938 ReturnTargets.insert(CatchTargets.begin(), CatchTargets.end());
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000939 }
Andrew Kaylora6c5b962015-05-20 23:22:24 +0000940 }
941 IndirectBrInst *Branch =
942 IndirectBrInst::Create(Recover, ReturnTargets.size(), LPadBB);
943 for (BasicBlock *Target : ReturnTargets)
944 Branch->addDestination(Target);
Andrew Kaylorcc14f382015-05-11 23:06:02 +0000945
Andrew Kaylora6c5b962015-05-20 23:22:24 +0000946 if (!isAsynchronousEHPersonality(Personality)) {
947 // C++ EH must repopulate the targets later to handle the case of
948 // targets that are reached indirectly through nested landing pads.
Andrew Kaylorcc14f382015-05-11 23:06:02 +0000949 LPadImpls.push_back(std::make_pair(Recover, Branch));
Andrew Kaylorf0f5e462015-03-03 20:00:16 +0000950 }
Andrew Kaylora6c5b962015-05-20 23:22:24 +0000951
Andrew Kaylorf0f5e462015-03-03 20:00:16 +0000952 } // End for each landingpad
Andrew Kaylor1476e6d2015-02-24 20:49:35 +0000953
954 // If nothing got outlined, there is no more processing to be done.
955 if (!HandlersOutlined)
956 return false;
957
Andrew Kayloraa92ab02015-04-03 19:37:50 +0000958 // Replace any nested landing pad stubs with the correct action handler.
959 // This must be done before we remove unreachable blocks because it
960 // cleans up references to outlined blocks that will be deleted.
961 for (auto &LPadPair : NestedLPtoOriginalLP)
962 completeNestedLandingPad(&F, LPadPair.first, LPadPair.second, FrameVarInfo);
Andrew Kaylor67d3c032015-04-08 20:57:22 +0000963 NestedLPtoOriginalLP.clear();
Andrew Kayloraa92ab02015-04-03 19:37:50 +0000964
Andrew Kaylora6c5b962015-05-20 23:22:24 +0000965 // Update the indirectbr instructions' target lists if necessary.
Andrew Kaylorcc14f382015-05-11 23:06:02 +0000966 SetVector<BasicBlock*> CheckedTargets;
Benjamin Kramera48e0652015-05-16 15:40:03 +0000967 SmallVector<std::unique_ptr<ActionHandler>, 4> ActionList;
Andrew Kaylorcc14f382015-05-11 23:06:02 +0000968 for (auto &LPadImplPair : LPadImpls) {
969 IntrinsicInst *Recover = cast<IntrinsicInst>(LPadImplPair.first);
970 IndirectBrInst *Branch = LPadImplPair.second;
971
972 // Get a list of handlers called by
Andrew Kaylorcc14f382015-05-11 23:06:02 +0000973 parseEHActions(Recover, ActionList);
974
975 // Add an indirect branch listing possible successors of the catch handlers.
976 SetVector<BasicBlock *> ReturnTargets;
Benjamin Kramera48e0652015-05-16 15:40:03 +0000977 for (const auto &Action : ActionList) {
978 if (auto *CA = dyn_cast<CatchHandler>(Action.get())) {
Andrew Kaylorcc14f382015-05-11 23:06:02 +0000979 Function *Handler = cast<Function>(CA->getHandlerBlockOrFunc());
980 getPossibleReturnTargets(&F, Handler, ReturnTargets);
981 }
982 }
Andrew Kaylor0ddaf2b2015-05-12 00:13:51 +0000983 ActionList.clear();
Andrew Kaylora6c5b962015-05-20 23:22:24 +0000984 // Clear any targets we already knew about.
985 for (unsigned int I = 0, E = Branch->getNumDestinations(); I < E; ++I) {
986 BasicBlock *KnownTarget = Branch->getDestination(I);
987 if (ReturnTargets.count(KnownTarget))
988 ReturnTargets.remove(KnownTarget);
989 }
Andrew Kaylorcc14f382015-05-11 23:06:02 +0000990 for (BasicBlock *Target : ReturnTargets) {
991 Branch->addDestination(Target);
992 // The target may be a block that we excepted to get pruned.
993 // If it is, it may contain a call to llvm.eh.endcatch.
994 if (CheckedTargets.insert(Target)) {
995 // Earlier preparations guarantee that all calls to llvm.eh.endcatch
996 // will be followed by an unconditional branch.
997 auto *Br = dyn_cast<BranchInst>(Target->getTerminator());
998 if (Br && Br->isUnconditional() &&
999 Br != Target->getFirstNonPHIOrDbgOrLifetime()) {
1000 Instruction *Prev = Br->getPrevNode();
1001 if (match(cast<Value>(Prev), m_Intrinsic<Intrinsic::eh_endcatch>()))
1002 Prev->eraseFromParent();
1003 }
1004 }
1005 }
1006 }
1007 LPadImpls.clear();
1008
David Majnemercde33032015-03-30 22:58:10 +00001009 F.addFnAttr("wineh-parent", F.getName());
1010
Andrew Kaylor6b67d422015-03-11 23:22:06 +00001011 // Delete any blocks that were only used by handlers that were outlined above.
1012 removeUnreachableBlocks(F);
Andrew Kaylor1476e6d2015-02-24 20:49:35 +00001013
Andrew Kaylor1476e6d2015-02-24 20:49:35 +00001014 BasicBlock *Entry = &F.getEntryBlock();
Reid Klecknercfb9ce52015-03-05 18:26:34 +00001015 IRBuilder<> Builder(F.getParent()->getContext());
Andrew Kaylor1476e6d2015-02-24 20:49:35 +00001016 Builder.SetInsertPoint(Entry->getFirstInsertionPt());
Andrew Kaylor1476e6d2015-02-24 20:49:35 +00001017
Reid Klecknercfb9ce52015-03-05 18:26:34 +00001018 Function *FrameEscapeFn =
Reid Kleckner60381792015-07-07 22:25:32 +00001019 Intrinsic::getDeclaration(M, Intrinsic::localescape);
Reid Klecknercfb9ce52015-03-05 18:26:34 +00001020 Function *RecoverFrameFn =
Reid Kleckner60381792015-07-07 22:25:32 +00001021 Intrinsic::getDeclaration(M, Intrinsic::localrecover);
Reid Klecknerf14787d2015-04-22 00:07:52 +00001022 SmallVector<Value *, 8> AllocasToEscape;
1023
Reid Kleckner60381792015-07-07 22:25:32 +00001024 // Scan the entry block for an existing call to llvm.localescape. We need to
Reid Klecknerf14787d2015-04-22 00:07:52 +00001025 // keep escaping those objects.
1026 for (Instruction &I : F.front()) {
1027 auto *II = dyn_cast<IntrinsicInst>(&I);
Reid Kleckner60381792015-07-07 22:25:32 +00001028 if (II && II->getIntrinsicID() == Intrinsic::localescape) {
Reid Klecknerf14787d2015-04-22 00:07:52 +00001029 auto Args = II->arg_operands();
1030 AllocasToEscape.append(Args.begin(), Args.end());
1031 II->eraseFromParent();
1032 break;
1033 }
1034 }
Andrew Kaylor1476e6d2015-02-24 20:49:35 +00001035
Andrew Kaylor1476e6d2015-02-24 20:49:35 +00001036 // Finally, replace all of the temporary allocas for frame variables used in
Reid Kleckner60381792015-07-07 22:25:32 +00001037 // the outlined handlers with calls to llvm.localrecover.
Andrew Kaylor1476e6d2015-02-24 20:49:35 +00001038 for (auto &VarInfoEntry : FrameVarInfo) {
Andrew Kaylor72029c62015-03-03 00:41:03 +00001039 Value *ParentVal = VarInfoEntry.first;
Reid Klecknercfb9ce52015-03-05 18:26:34 +00001040 TinyPtrVector<AllocaInst *> &Allocas = VarInfoEntry.second;
Reid Klecknerfd7df282015-04-22 21:05:21 +00001041 AllocaInst *ParentAlloca = cast<AllocaInst>(ParentVal);
Andrew Kaylor72029c62015-03-03 00:41:03 +00001042
Reid Klecknerb4019412015-04-06 18:50:38 +00001043 // FIXME: We should try to sink unescaped allocas from the parent frame into
1044 // the child frame. If the alloca is escaped, we have to use the lifetime
1045 // markers to ensure that the alloca is only live within the child frame.
Andrew Kaylor1476e6d2015-02-24 20:49:35 +00001046
Reid Klecknercfb9ce52015-03-05 18:26:34 +00001047 // Add this alloca to the list of things to escape.
1048 AllocasToEscape.push_back(ParentAlloca);
Andrew Kaylor1476e6d2015-02-24 20:49:35 +00001049
Reid Klecknercfb9ce52015-03-05 18:26:34 +00001050 // Next replace all outlined allocas that are mapped to it.
1051 for (AllocaInst *TempAlloca : Allocas) {
Reid Kleckner3567d272015-04-02 21:13:31 +00001052 if (TempAlloca == getCatchObjectSentinel())
1053 continue; // Skip catch parameter sentinels.
Reid Klecknercfb9ce52015-03-05 18:26:34 +00001054 Function *HandlerFn = TempAlloca->getParent()->getParent();
Reid Klecknerbcda1cd2015-04-29 22:49:54 +00001055 llvm::Value *FP = HandlerToParentFP[HandlerFn];
1056 assert(FP);
1057
Reid Kleckner60381792015-07-07 22:25:32 +00001058 // FIXME: Sink this localrecover into the blocks where it is used.
Reid Klecknercfb9ce52015-03-05 18:26:34 +00001059 Builder.SetInsertPoint(TempAlloca);
1060 Builder.SetCurrentDebugLocation(TempAlloca->getDebugLoc());
1061 Value *RecoverArgs[] = {
Reid Klecknerbcda1cd2015-04-29 22:49:54 +00001062 Builder.CreateBitCast(&F, Int8PtrType, ""), FP,
Reid Klecknercfb9ce52015-03-05 18:26:34 +00001063 llvm::ConstantInt::get(Int32Type, AllocasToEscape.size() - 1)};
Reid Klecknerbcda1cd2015-04-29 22:49:54 +00001064 Instruction *RecoveredAlloca =
1065 Builder.CreateCall(RecoverFrameFn, RecoverArgs);
1066
Reid Klecknercfb9ce52015-03-05 18:26:34 +00001067 // Add a pointer bitcast if the alloca wasn't an i8.
1068 if (RecoveredAlloca->getType() != TempAlloca->getType()) {
1069 RecoveredAlloca->setName(Twine(TempAlloca->getName()) + ".i8");
Reid Klecknerbcda1cd2015-04-29 22:49:54 +00001070 RecoveredAlloca = cast<Instruction>(
1071 Builder.CreateBitCast(RecoveredAlloca, TempAlloca->getType()));
Andrew Kaylor1476e6d2015-02-24 20:49:35 +00001072 }
Reid Klecknercfb9ce52015-03-05 18:26:34 +00001073 TempAlloca->replaceAllUsesWith(RecoveredAlloca);
1074 TempAlloca->removeFromParent();
1075 RecoveredAlloca->takeName(TempAlloca);
1076 delete TempAlloca;
1077 }
Andrew Kaylor6b67d422015-03-11 23:22:06 +00001078 } // End for each FrameVarInfo entry.
Andrew Kaylor1476e6d2015-02-24 20:49:35 +00001079
Reid Kleckner60381792015-07-07 22:25:32 +00001080 // Insert 'call void (...)* @llvm.localescape(...)' at the end of the entry
Reid Klecknercfb9ce52015-03-05 18:26:34 +00001081 // block.
1082 Builder.SetInsertPoint(&F.getEntryBlock().back());
1083 Builder.CreateCall(FrameEscapeFn, AllocasToEscape);
1084
Reid Klecknercfbfe6f2015-04-24 20:25:05 +00001085 if (SEHExceptionCodeSlot) {
Reid Klecknerf12c0302015-06-09 21:42:19 +00001086 if (isAllocaPromotable(SEHExceptionCodeSlot)) {
1087 SmallPtrSet<BasicBlock *, 4> UserBlocks;
1088 for (User *U : SEHExceptionCodeSlot->users()) {
1089 if (auto *Inst = dyn_cast<Instruction>(U))
1090 UserBlocks.insert(Inst->getParent());
1091 }
Reid Klecknercfbfe6f2015-04-24 20:25:05 +00001092 PromoteMemToReg(SEHExceptionCodeSlot, *DT);
Reid Klecknerf12c0302015-06-09 21:42:19 +00001093 // After the promotion, kill off dead instructions.
1094 for (BasicBlock *BB : UserBlocks)
1095 SimplifyInstructionsInBlock(BB, LibInfo);
1096 }
Reid Klecknercfbfe6f2015-04-24 20:25:05 +00001097 }
1098
Andrew Kaylor6b67d422015-03-11 23:22:06 +00001099 // Clean up the handler action maps we created for this function
1100 DeleteContainerSeconds(CatchHandlerMap);
1101 CatchHandlerMap.clear();
1102 DeleteContainerSeconds(CleanupHandlerMap);
1103 CleanupHandlerMap.clear();
Reid Klecknerbcda1cd2015-04-29 22:49:54 +00001104 HandlerToParentFP.clear();
1105 DT = nullptr;
Reid Klecknerf12c0302015-06-09 21:42:19 +00001106 LibInfo = nullptr;
Ahmed Bougachaed363c52015-05-06 01:28:58 +00001107 SEHExceptionCodeSlot = nullptr;
Andrew Kaylora6c5b962015-05-20 23:22:24 +00001108 EHBlocks.clear();
1109 NormalBlocks.clear();
1110 EHReturnBlocks.clear();
Andrew Kaylor6b67d422015-03-11 23:22:06 +00001111
Andrew Kaylor1476e6d2015-02-24 20:49:35 +00001112 return HandlersOutlined;
1113}
1114
Andrew Kaylor64622aa2015-04-01 17:21:25 +00001115void WinEHPrepare::promoteLandingPadValues(LandingPadInst *LPad) {
1116 // If the return values of the landing pad instruction are extracted and
1117 // stored to memory, we want to promote the store locations to reg values.
1118 SmallVector<AllocaInst *, 2> EHAllocas;
1119
1120 // The landingpad instruction returns an aggregate value. Typically, its
1121 // value will be passed to a pair of extract value instructions and the
1122 // results of those extracts are often passed to store instructions.
1123 // In unoptimized code the stored value will often be loaded and then stored
1124 // again.
1125 for (auto *U : LPad->users()) {
1126 ExtractValueInst *Extract = dyn_cast<ExtractValueInst>(U);
1127 if (!Extract)
1128 continue;
1129
1130 for (auto *EU : Extract->users()) {
1131 if (auto *Store = dyn_cast<StoreInst>(EU)) {
1132 auto *AV = cast<AllocaInst>(Store->getPointerOperand());
1133 EHAllocas.push_back(AV);
1134 }
1135 }
1136 }
1137
1138 // We can't do this without a dominator tree.
1139 assert(DT);
1140
1141 if (!EHAllocas.empty()) {
1142 PromoteMemToReg(EHAllocas, *DT);
1143 EHAllocas.clear();
1144 }
Reid Kleckner86762142015-04-16 00:02:04 +00001145
1146 // After promotion, some extracts may be trivially dead. Remove them.
1147 SmallVector<Value *, 4> Users(LPad->user_begin(), LPad->user_end());
1148 for (auto *U : Users)
1149 RecursivelyDeleteTriviallyDeadInstructions(U);
Andrew Kaylor64622aa2015-04-01 17:21:25 +00001150}
1151
Andrew Kaylorcc14f382015-05-11 23:06:02 +00001152void WinEHPrepare::getPossibleReturnTargets(Function *ParentF,
1153 Function *HandlerF,
1154 SetVector<BasicBlock*> &Targets) {
1155 for (BasicBlock &BB : *HandlerF) {
1156 // If the handler contains landing pads, check for any
1157 // handlers that may return directly to a block in the
1158 // parent function.
1159 if (auto *LPI = BB.getLandingPadInst()) {
1160 IntrinsicInst *Recover = cast<IntrinsicInst>(LPI->getNextNode());
Benjamin Kramera48e0652015-05-16 15:40:03 +00001161 SmallVector<std::unique_ptr<ActionHandler>, 4> ActionList;
Andrew Kaylorcc14f382015-05-11 23:06:02 +00001162 parseEHActions(Recover, ActionList);
Benjamin Kramera48e0652015-05-16 15:40:03 +00001163 for (const auto &Action : ActionList) {
1164 if (auto *CH = dyn_cast<CatchHandler>(Action.get())) {
Andrew Kaylorcc14f382015-05-11 23:06:02 +00001165 Function *NestedF = cast<Function>(CH->getHandlerBlockOrFunc());
1166 getPossibleReturnTargets(ParentF, NestedF, Targets);
1167 }
1168 }
1169 }
1170
1171 auto *Ret = dyn_cast<ReturnInst>(BB.getTerminator());
1172 if (!Ret)
1173 continue;
1174
1175 // Handler functions must always return a block address.
1176 BlockAddress *BA = cast<BlockAddress>(Ret->getReturnValue());
1177
1178 // If this is the handler for a nested landing pad, the
1179 // return address may have been remapped to a block in the
1180 // parent handler. We're not interested in those.
1181 if (BA->getFunction() != ParentF)
1182 continue;
1183
1184 Targets.insert(BA->getBasicBlock());
1185 }
1186}
1187
Andrew Kayloraa92ab02015-04-03 19:37:50 +00001188void WinEHPrepare::completeNestedLandingPad(Function *ParentFn,
1189 LandingPadInst *OutlinedLPad,
1190 const LandingPadInst *OriginalLPad,
1191 FrameVarInfoMap &FrameVarInfo) {
1192 // Get the nested block and erase the unreachable instruction that was
1193 // temporarily inserted as its terminator.
1194 LLVMContext &Context = ParentFn->getContext();
1195 BasicBlock *OutlinedBB = OutlinedLPad->getParent();
Andrew Kaylora6c5b962015-05-20 23:22:24 +00001196 // If the nested landing pad was outlined before the landing pad that enclosed
1197 // it, it will already be in outlined form. In that case, we just need to see
1198 // if the returns and the enclosing branch instruction need to be updated.
1199 IndirectBrInst *Branch =
1200 dyn_cast<IndirectBrInst>(OutlinedBB->getTerminator());
1201 if (!Branch) {
1202 // If the landing pad wasn't in outlined form, it should be a stub with
1203 // an unreachable terminator.
1204 assert(isa<UnreachableInst>(OutlinedBB->getTerminator()));
1205 OutlinedBB->getTerminator()->eraseFromParent();
1206 // That should leave OutlinedLPad as the last instruction in its block.
1207 assert(&OutlinedBB->back() == OutlinedLPad);
1208 }
Andrew Kayloraa92ab02015-04-03 19:37:50 +00001209
1210 // The original landing pad will have already had its action intrinsic
1211 // built by the outlining loop. We need to clone that into the outlined
1212 // location. It may also be necessary to add references to the exception
1213 // variables to the outlined handler in which this landing pad is nested
1214 // and remap return instructions in the nested handlers that should return
1215 // to an address in the outlined handler.
1216 Function *OutlinedHandlerFn = OutlinedBB->getParent();
Andrew Kayloraa92ab02015-04-03 19:37:50 +00001217 BasicBlock::const_iterator II = OriginalLPad;
1218 ++II;
1219 // The instruction after the landing pad should now be a call to eh.actions.
1220 const Instruction *Recover = II;
Andrew Kaylora6c5b962015-05-20 23:22:24 +00001221 const IntrinsicInst *EHActions = cast<IntrinsicInst>(Recover);
Andrew Kayloraa92ab02015-04-03 19:37:50 +00001222
Andrew Kaylora6c5b962015-05-20 23:22:24 +00001223 // Remap the return target in the nested handler.
Andrew Kayloraa92ab02015-04-03 19:37:50 +00001224 SmallVector<BlockAddress *, 4> ActionTargets;
Benjamin Kramera48e0652015-05-16 15:40:03 +00001225 SmallVector<std::unique_ptr<ActionHandler>, 4> ActionList;
Andrew Kayloraa92ab02015-04-03 19:37:50 +00001226 parseEHActions(EHActions, ActionList);
Benjamin Kramera48e0652015-05-16 15:40:03 +00001227 for (const auto &Action : ActionList) {
1228 auto *Catch = dyn_cast<CatchHandler>(Action.get());
Andrew Kayloraa92ab02015-04-03 19:37:50 +00001229 if (!Catch)
1230 continue;
1231 // The dyn_cast to function here selects C++ catch handlers and skips
1232 // SEH catch handlers.
1233 auto *Handler = dyn_cast<Function>(Catch->getHandlerBlockOrFunc());
1234 if (!Handler)
1235 continue;
1236 // Visit all the return instructions, looking for places that return
1237 // to a location within OutlinedHandlerFn.
1238 for (BasicBlock &NestedHandlerBB : *Handler) {
1239 auto *Ret = dyn_cast<ReturnInst>(NestedHandlerBB.getTerminator());
1240 if (!Ret)
1241 continue;
1242
1243 // Handler functions must always return a block address.
1244 BlockAddress *BA = cast<BlockAddress>(Ret->getReturnValue());
1245 // The original target will have been in the main parent function,
1246 // but if it is the address of a block that has been outlined, it
1247 // should be a block that was outlined into OutlinedHandlerFn.
1248 assert(BA->getFunction() == ParentFn);
1249
Andrew Kaylora6c5b962015-05-20 23:22:24 +00001250 // Ignore targets that aren't part of an outlined handler function.
Andrew Kayloraa92ab02015-04-03 19:37:50 +00001251 if (!LPadTargetBlocks.count(BA->getBasicBlock()))
1252 continue;
1253
1254 // If the return value is the address ofF a block that we
1255 // previously outlined into the parent handler function, replace
1256 // the return instruction and add the mapped target to the list
1257 // of possible return addresses.
1258 BasicBlock *MappedBB = LPadTargetBlocks[BA->getBasicBlock()];
1259 assert(MappedBB->getParent() == OutlinedHandlerFn);
1260 BlockAddress *NewBA = BlockAddress::get(OutlinedHandlerFn, MappedBB);
1261 Ret->eraseFromParent();
1262 ReturnInst::Create(Context, NewBA, &NestedHandlerBB);
1263 ActionTargets.push_back(NewBA);
1264 }
1265 }
Andrew Kaylor7a0cec32015-04-03 21:44:17 +00001266 ActionList.clear();
Andrew Kayloraa92ab02015-04-03 19:37:50 +00001267
Andrew Kaylora6c5b962015-05-20 23:22:24 +00001268 if (Branch) {
1269 // If the landing pad was already in outlined form, just update its targets.
1270 for (unsigned int I = Branch->getNumDestinations(); I > 0; --I)
1271 Branch->removeDestination(I);
1272 // Add the previously collected action targets.
1273 for (auto *Target : ActionTargets)
1274 Branch->addDestination(Target->getBasicBlock());
1275 } else {
1276 // If the landing pad was previously stubbed out, fill in its outlined form.
1277 IntrinsicInst *NewEHActions = cast<IntrinsicInst>(EHActions->clone());
1278 OutlinedBB->getInstList().push_back(NewEHActions);
1279
1280 // Insert an indirect branch into the outlined landing pad BB.
1281 IndirectBrInst *IBr = IndirectBrInst::Create(NewEHActions, 0, OutlinedBB);
1282 // Add the previously collected action targets.
1283 for (auto *Target : ActionTargets)
1284 IBr->addDestination(Target->getBasicBlock());
1285 }
Andrew Kayloraa92ab02015-04-03 19:37:50 +00001286}
1287
Andrew Kaylor6b67d422015-03-11 23:22:06 +00001288// This function examines a block to determine whether the block ends with a
1289// conditional branch to a catch handler based on a selector comparison.
1290// This function is used both by the WinEHPrepare::findSelectorComparison() and
1291// WinEHCleanupDirector::handleTypeIdFor().
1292static bool isSelectorDispatch(BasicBlock *BB, BasicBlock *&CatchHandler,
1293 Constant *&Selector, BasicBlock *&NextBB) {
1294 ICmpInst::Predicate Pred;
1295 BasicBlock *TBB, *FBB;
1296 Value *LHS, *RHS;
1297
1298 if (!match(BB->getTerminator(),
1299 m_Br(m_ICmp(Pred, m_Value(LHS), m_Value(RHS)), TBB, FBB)))
1300 return false;
1301
1302 if (!match(LHS,
1303 m_Intrinsic<Intrinsic::eh_typeid_for>(m_Constant(Selector))) &&
1304 !match(RHS, m_Intrinsic<Intrinsic::eh_typeid_for>(m_Constant(Selector))))
1305 return false;
1306
1307 if (Pred == CmpInst::ICMP_EQ) {
1308 CatchHandler = TBB;
1309 NextBB = FBB;
1310 return true;
1311 }
1312
1313 if (Pred == CmpInst::ICMP_NE) {
1314 CatchHandler = FBB;
1315 NextBB = TBB;
1316 return true;
1317 }
1318
1319 return false;
1320}
1321
Andrew Kaylor41758512015-04-20 22:04:09 +00001322static bool isCatchBlock(BasicBlock *BB) {
1323 for (BasicBlock::iterator II = BB->getFirstNonPHIOrDbg(), IE = BB->end();
1324 II != IE; ++II) {
1325 if (match(cast<Value>(II), m_Intrinsic<Intrinsic::eh_begincatch>()))
1326 return true;
1327 }
1328 return false;
1329}
1330
David Majnemer7fddecc2015-06-17 20:52:32 +00001331static BasicBlock *createStubLandingPad(Function *Handler) {
Andrew Kaylorbb111322015-04-07 21:30:23 +00001332 // FIXME: Finish this!
1333 LLVMContext &Context = Handler->getContext();
1334 BasicBlock *StubBB = BasicBlock::Create(Context, "stub");
1335 Handler->getBasicBlockList().push_back(StubBB);
1336 IRBuilder<> Builder(StubBB);
1337 LandingPadInst *LPad = Builder.CreateLandingPad(
1338 llvm::StructType::get(Type::getInt8PtrTy(Context),
1339 Type::getInt32Ty(Context), nullptr),
David Majnemer7fddecc2015-06-17 20:52:32 +00001340 0);
Andrew Kaylor43e1d762015-04-23 00:20:44 +00001341 // Insert a call to llvm.eh.actions so that we don't try to outline this lpad.
Andrew Kaylor91307432015-04-28 22:01:51 +00001342 Function *ActionIntrin =
1343 Intrinsic::getDeclaration(Handler->getParent(), Intrinsic::eh_actions);
David Blaikieff6409d2015-05-18 22:13:54 +00001344 Builder.CreateCall(ActionIntrin, {}, "recover");
Andrew Kaylorbb111322015-04-07 21:30:23 +00001345 LPad->setCleanup(true);
1346 Builder.CreateUnreachable();
1347 return StubBB;
1348}
1349
1350// Cycles through the blocks in an outlined handler function looking for an
1351// invoke instruction and inserts an invoke of llvm.donothing with an empty
1352// landing pad if none is found. The code that generates the .xdata tables for
1353// the handler needs at least one landing pad to identify the parent function's
1354// personality.
David Majnemer7fddecc2015-06-17 20:52:32 +00001355void WinEHPrepare::addStubInvokeToHandlerIfNeeded(Function *Handler) {
Andrew Kaylorbb111322015-04-07 21:30:23 +00001356 ReturnInst *Ret = nullptr;
Andrew Kaylor5f715522015-04-23 18:37:39 +00001357 UnreachableInst *Unreached = nullptr;
Andrew Kaylorbb111322015-04-07 21:30:23 +00001358 for (BasicBlock &BB : *Handler) {
1359 TerminatorInst *Terminator = BB.getTerminator();
1360 // If we find an invoke, there is nothing to be done.
1361 auto *II = dyn_cast<InvokeInst>(Terminator);
1362 if (II)
1363 return;
1364 // If we've already recorded a return instruction, keep looking for invokes.
Andrew Kaylor5f715522015-04-23 18:37:39 +00001365 if (!Ret)
1366 Ret = dyn_cast<ReturnInst>(Terminator);
1367 // If we haven't recorded an unreachable instruction, try this terminator.
1368 if (!Unreached)
1369 Unreached = dyn_cast<UnreachableInst>(Terminator);
Andrew Kaylorbb111322015-04-07 21:30:23 +00001370 }
1371
1372 // If we got this far, the handler contains no invokes. We should have seen
Andrew Kaylor5f715522015-04-23 18:37:39 +00001373 // at least one return or unreachable instruction. We'll insert an invoke of
1374 // llvm.donothing ahead of that instruction.
1375 assert(Ret || Unreached);
1376 TerminatorInst *Term;
1377 if (Ret)
1378 Term = Ret;
1379 else
1380 Term = Unreached;
1381 BasicBlock *OldRetBB = Term->getParent();
Andrew Kaylor046f7b42015-04-28 21:54:14 +00001382 BasicBlock *NewRetBB = SplitBlock(OldRetBB, Term, DT);
Andrew Kaylorbb111322015-04-07 21:30:23 +00001383 // SplitBlock adds an unconditional branch instruction at the end of the
1384 // parent block. We want to replace that with an invoke call, so we can
1385 // erase it now.
1386 OldRetBB->getTerminator()->eraseFromParent();
David Majnemer7fddecc2015-06-17 20:52:32 +00001387 BasicBlock *StubLandingPad = createStubLandingPad(Handler);
Andrew Kaylorbb111322015-04-07 21:30:23 +00001388 Function *F =
1389 Intrinsic::getDeclaration(Handler->getParent(), Intrinsic::donothing);
1390 InvokeInst::Create(F, NewRetBB, StubLandingPad, None, "", OldRetBB);
1391}
1392
Reid Klecknerbcda1cd2015-04-29 22:49:54 +00001393// FIXME: Consider sinking this into lib/Target/X86 somehow. TargetLowering
1394// usually doesn't build LLVM IR, so that's probably the wrong place.
Reid Kleckner6511c8b2015-07-01 20:59:25 +00001395Function *WinEHPrepare::createHandlerFunc(Function *ParentFn, Type *RetTy,
1396 const Twine &Name, Module *M,
1397 Value *&ParentFP) {
Reid Klecknerbcda1cd2015-04-29 22:49:54 +00001398 // x64 uses a two-argument prototype where the parent FP is the second
1399 // argument. x86 uses no arguments, just the incoming EBP value.
1400 LLVMContext &Context = M->getContext();
Reid Kleckner6511c8b2015-07-01 20:59:25 +00001401 Type *Int8PtrType = Type::getInt8PtrTy(Context);
Reid Klecknerbcda1cd2015-04-29 22:49:54 +00001402 FunctionType *FnType;
1403 if (TheTriple.getArch() == Triple::x86_64) {
Reid Klecknerbcda1cd2015-04-29 22:49:54 +00001404 Type *ArgTys[2] = {Int8PtrType, Int8PtrType};
1405 FnType = FunctionType::get(RetTy, ArgTys, false);
1406 } else {
1407 FnType = FunctionType::get(RetTy, None, false);
1408 }
1409
1410 Function *Handler =
1411 Function::Create(FnType, GlobalVariable::InternalLinkage, Name, M);
1412 BasicBlock *Entry = BasicBlock::Create(Context, "entry");
1413 Handler->getBasicBlockList().push_front(Entry);
1414 if (TheTriple.getArch() == Triple::x86_64) {
1415 ParentFP = &(Handler->getArgumentList().back());
1416 } else {
1417 assert(M);
1418 Function *FrameAddressFn =
1419 Intrinsic::getDeclaration(M, Intrinsic::frameaddress);
Reid Kleckner6511c8b2015-07-01 20:59:25 +00001420 Function *RecoverFPFn =
1421 Intrinsic::getDeclaration(M, Intrinsic::x86_seh_recoverfp);
1422 IRBuilder<> Builder(&Handler->getEntryBlock());
1423 Value *EBP =
1424 Builder.CreateCall(FrameAddressFn, {Builder.getInt32(1)}, "ebp");
1425 Value *ParentI8Fn = Builder.CreateBitCast(ParentFn, Int8PtrType);
1426 ParentFP = Builder.CreateCall(RecoverFPFn, {ParentI8Fn, EBP});
Reid Klecknerbcda1cd2015-04-29 22:49:54 +00001427 }
1428 return Handler;
1429}
1430
Andrew Kaylor6b67d422015-03-11 23:22:06 +00001431bool WinEHPrepare::outlineHandler(ActionHandler *Action, Function *SrcFn,
1432 LandingPadInst *LPad, BasicBlock *StartBB,
Andrew Kaylorf0f5e462015-03-03 20:00:16 +00001433 FrameVarInfoMap &VarInfo) {
Andrew Kaylor1476e6d2015-02-24 20:49:35 +00001434 Module *M = SrcFn->getParent();
1435 LLVMContext &Context = M->getContext();
Reid Klecknerbcda1cd2015-04-29 22:49:54 +00001436 Type *Int8PtrType = Type::getInt8PtrTy(Context);
Andrew Kaylor1476e6d2015-02-24 20:49:35 +00001437
1438 // Create a new function to receive the handler contents.
Reid Klecknerbcda1cd2015-04-29 22:49:54 +00001439 Value *ParentFP;
Andrew Kaylorf0f5e462015-03-03 20:00:16 +00001440 Function *Handler;
Andrew Kaylor6b67d422015-03-11 23:22:06 +00001441 if (Action->getType() == Catch) {
Reid Kleckner6511c8b2015-07-01 20:59:25 +00001442 Handler = createHandlerFunc(SrcFn, Int8PtrType, SrcFn->getName() + ".catch", M,
Reid Klecknerbcda1cd2015-04-29 22:49:54 +00001443 ParentFP);
Andrew Kaylorf0f5e462015-03-03 20:00:16 +00001444 } else {
Reid Kleckner6511c8b2015-07-01 20:59:25 +00001445 Handler = createHandlerFunc(SrcFn, Type::getVoidTy(Context),
Reid Klecknerbcda1cd2015-04-29 22:49:54 +00001446 SrcFn->getName() + ".cleanup", M, ParentFP);
Andrew Kaylorf0f5e462015-03-03 20:00:16 +00001447 }
David Majnemer7fddecc2015-06-17 20:52:32 +00001448 Handler->setPersonalityFn(SrcFn->getPersonalityFn());
Reid Klecknerbcda1cd2015-04-29 22:49:54 +00001449 HandlerToParentFP[Handler] = ParentFP;
David Majnemercde33032015-03-30 22:58:10 +00001450 Handler->addFnAttr("wineh-parent", SrcFn->getName());
Reid Klecknerbcda1cd2015-04-29 22:49:54 +00001451 BasicBlock *Entry = &Handler->getEntryBlock();
David Majnemercde33032015-03-30 22:58:10 +00001452
Andrew Kaylor1476e6d2015-02-24 20:49:35 +00001453 // Generate a standard prolog to setup the frame recovery structure.
1454 IRBuilder<> Builder(Context);
Andrew Kaylor1476e6d2015-02-24 20:49:35 +00001455 Builder.SetInsertPoint(Entry);
1456 Builder.SetCurrentDebugLocation(LPad->getDebugLoc());
1457
Andrew Kaylorf0f5e462015-03-03 20:00:16 +00001458 std::unique_ptr<WinEHCloningDirectorBase> Director;
Andrew Kaylor1476e6d2015-02-24 20:49:35 +00001459
Andrew Kaylor1476e6d2015-02-24 20:49:35 +00001460 ValueToValueMapTy VMap;
1461
Andrew Kaylor6b67d422015-03-11 23:22:06 +00001462 LandingPadMap &LPadMap = LPadMaps[LPad];
1463 if (!LPadMap.isInitialized())
1464 LPadMap.mapLandingPad(LPad);
Reid Kleckner0f9e27a2015-03-18 20:26:53 +00001465 if (auto *CatchAction = dyn_cast<CatchHandler>(Action)) {
1466 Constant *Sel = CatchAction->getSelector();
Andrew Kaylora6c5b962015-05-20 23:22:24 +00001467 Director.reset(new WinEHCatchDirector(Handler, ParentFP, Sel, VarInfo,
1468 LPadMap, NestedLPtoOriginalLP, DT,
1469 EHBlocks));
Andrew Kaylor64622aa2015-04-01 17:21:25 +00001470 LPadMap.remapEHValues(VMap, UndefValue::get(Int8PtrType),
1471 ConstantInt::get(Type::getInt32Ty(Context), 1));
Andrew Kaylor6b67d422015-03-11 23:22:06 +00001472 } else {
Reid Klecknerbcda1cd2015-04-29 22:49:54 +00001473 Director.reset(
1474 new WinEHCleanupDirector(Handler, ParentFP, VarInfo, LPadMap));
Andrew Kaylor64622aa2015-04-01 17:21:25 +00001475 LPadMap.remapEHValues(VMap, UndefValue::get(Int8PtrType),
1476 UndefValue::get(Type::getInt32Ty(Context)));
Andrew Kaylor6b67d422015-03-11 23:22:06 +00001477 }
Andrew Kaylor1476e6d2015-02-24 20:49:35 +00001478
Andrew Kaylor1476e6d2015-02-24 20:49:35 +00001479 SmallVector<ReturnInst *, 8> Returns;
Andrew Kaylor6b67d422015-03-11 23:22:06 +00001480 ClonedCodeInfo OutlinedFunctionInfo;
Andrew Kaylor1476e6d2015-02-24 20:49:35 +00001481
Andrew Kaylor3170e562015-03-20 21:42:54 +00001482 // If the start block contains PHI nodes, we need to map them.
1483 BasicBlock::iterator II = StartBB->begin();
1484 while (auto *PN = dyn_cast<PHINode>(II)) {
1485 bool Mapped = false;
1486 // Look for PHI values that we have already mapped (such as the selector).
1487 for (Value *Val : PN->incoming_values()) {
1488 if (VMap.count(Val)) {
1489 VMap[PN] = VMap[Val];
1490 Mapped = true;
1491 }
1492 }
1493 // If we didn't find a match for this value, map it as an undef.
1494 if (!Mapped) {
1495 VMap[PN] = UndefValue::get(PN->getType());
1496 }
1497 ++II;
1498 }
1499
Andrew Kaylor00e5d9e2015-04-20 22:53:42 +00001500 // The landing pad value may be used by PHI nodes. It will ultimately be
1501 // eliminated, but we need it in the map for intermediate handling.
1502 VMap[LPad] = UndefValue::get(LPad->getType());
1503
Andrew Kaylor6b67d422015-03-11 23:22:06 +00001504 // Skip over PHIs and, if applicable, landingpad instructions.
Andrew Kaylor3170e562015-03-20 21:42:54 +00001505 II = StartBB->getFirstInsertionPt();
Andrew Kaylor1476e6d2015-02-24 20:49:35 +00001506
Andrew Kaylor6b67d422015-03-11 23:22:06 +00001507 CloneAndPruneIntoFromInst(Handler, SrcFn, II, VMap,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001508 /*ModuleLevelChanges=*/false, Returns, "",
Andrew Kaylor6b67d422015-03-11 23:22:06 +00001509 &OutlinedFunctionInfo, Director.get());
Andrew Kaylor1476e6d2015-02-24 20:49:35 +00001510
Andrew Kaylor5dacfd82015-04-24 23:10:38 +00001511 // Move all the instructions in the cloned "entry" block into our entry block.
1512 // Depending on how the parent function was laid out, the block that will
1513 // correspond to the outlined entry block may not be the first block in the
1514 // list. We can recognize it, however, as the cloned block which has no
1515 // predecessors. Any other block wouldn't have been cloned if it didn't
1516 // have a predecessor which was also cloned.
Andrew Kaylor91307432015-04-28 22:01:51 +00001517 Function::iterator ClonedIt = std::next(Function::iterator(Entry));
Andrew Kaylor5dacfd82015-04-24 23:10:38 +00001518 while (!pred_empty(ClonedIt))
1519 ++ClonedIt;
1520 BasicBlock *ClonedEntryBB = ClonedIt;
1521 assert(ClonedEntryBB);
1522 Entry->getInstList().splice(Entry->end(), ClonedEntryBB->getInstList());
1523 ClonedEntryBB->eraseFromParent();
Andrew Kaylor1476e6d2015-02-24 20:49:35 +00001524
Andrew Kaylorbb111322015-04-07 21:30:23 +00001525 // Make sure we can identify the handler's personality later.
David Majnemer7fddecc2015-06-17 20:52:32 +00001526 addStubInvokeToHandlerIfNeeded(Handler);
Andrew Kaylorbb111322015-04-07 21:30:23 +00001527
Andrew Kaylor6b67d422015-03-11 23:22:06 +00001528 if (auto *CatchAction = dyn_cast<CatchHandler>(Action)) {
1529 WinEHCatchDirector *CatchDirector =
1530 reinterpret_cast<WinEHCatchDirector *>(Director.get());
1531 CatchAction->setExceptionVar(CatchDirector->getExceptionVar());
1532 CatchAction->setReturnTargets(CatchDirector->getReturnTargets());
Andrew Kayloraa92ab02015-04-03 19:37:50 +00001533
1534 // Look for blocks that are not part of the landing pad that we just
1535 // outlined but terminate with a call to llvm.eh.endcatch and a
1536 // branch to a block that is in the handler we just outlined.
1537 // These blocks will be part of a nested landing pad that intends to
1538 // return to an address in this handler. This case is best handled
1539 // after both landing pads have been outlined, so for now we'll just
1540 // save the association of the blocks in LPadTargetBlocks. The
1541 // return instructions which are created from these branches will be
1542 // replaced after all landing pads have been outlined.
Richard Trieu6b1aa5f2015-04-15 01:21:15 +00001543 for (const auto MapEntry : VMap) {
Andrew Kayloraa92ab02015-04-03 19:37:50 +00001544 // VMap maps all values and blocks that were just cloned, but dead
1545 // blocks which were pruned will map to nullptr.
1546 if (!isa<BasicBlock>(MapEntry.first) || MapEntry.second == nullptr)
1547 continue;
1548 const BasicBlock *MappedBB = cast<BasicBlock>(MapEntry.first);
1549 for (auto *Pred : predecessors(const_cast<BasicBlock *>(MappedBB))) {
1550 auto *Branch = dyn_cast<BranchInst>(Pred->getTerminator());
1551 if (!Branch || !Branch->isUnconditional() || Pred->size() <= 1)
1552 continue;
1553 BasicBlock::iterator II = const_cast<BranchInst *>(Branch);
1554 --II;
1555 if (match(cast<Value>(II), m_Intrinsic<Intrinsic::eh_endcatch>())) {
1556 // This would indicate that a nested landing pad wants to return
1557 // to a block that is outlined into two different handlers.
1558 assert(!LPadTargetBlocks.count(MappedBB));
1559 LPadTargetBlocks[MappedBB] = cast<BasicBlock>(MapEntry.second);
1560 }
1561 }
1562 }
1563 } // End if (CatchAction)
Andrew Kaylor6b67d422015-03-11 23:22:06 +00001564
Reid Kleckner0f9e27a2015-03-18 20:26:53 +00001565 Action->setHandlerBlockOrFunc(Handler);
Andrew Kaylor6b67d422015-03-11 23:22:06 +00001566
Andrew Kaylor1476e6d2015-02-24 20:49:35 +00001567 return true;
1568}
1569
Reid Kleckner0f9e27a2015-03-18 20:26:53 +00001570/// This BB must end in a selector dispatch. All we need to do is pass the
1571/// handler block to llvm.eh.actions and list it as a possible indirectbr
1572/// target.
1573void WinEHPrepare::processSEHCatchHandler(CatchHandler *CatchAction,
1574 BasicBlock *StartBB) {
1575 BasicBlock *HandlerBB;
1576 BasicBlock *NextBB;
1577 Constant *Selector;
1578 bool Res = isSelectorDispatch(StartBB, HandlerBB, Selector, NextBB);
1579 if (Res) {
1580 // If this was EH dispatch, this must be a conditional branch to the handler
1581 // block.
1582 // FIXME: Handle instructions in the dispatch block. Currently we drop them,
1583 // leading to crashes if some optimization hoists stuff here.
1584 assert(CatchAction->getSelector() && HandlerBB &&
1585 "expected catch EH dispatch");
1586 } else {
1587 // This must be a catch-all. Split the block after the landingpad.
1588 assert(CatchAction->getSelector()->isNullValue() && "expected catch-all");
Andrew Kaylor046f7b42015-04-28 21:54:14 +00001589 HandlerBB = SplitBlock(StartBB, StartBB->getFirstInsertionPt(), DT);
Reid Kleckner0f9e27a2015-03-18 20:26:53 +00001590 }
Reid Klecknercfbfe6f2015-04-24 20:25:05 +00001591 IRBuilder<> Builder(HandlerBB->getFirstInsertionPt());
1592 Function *EHCodeFn = Intrinsic::getDeclaration(
1593 StartBB->getParent()->getParent(), Intrinsic::eh_exceptioncode);
David Blaikieff6409d2015-05-18 22:13:54 +00001594 Value *Code = Builder.CreateCall(EHCodeFn, {}, "sehcode");
Reid Klecknercfbfe6f2015-04-24 20:25:05 +00001595 Code = Builder.CreateIntToPtr(Code, SEHExceptionCodeSlot->getAllocatedType());
1596 Builder.CreateStore(Code, SEHExceptionCodeSlot);
Reid Kleckner0f9e27a2015-03-18 20:26:53 +00001597 CatchAction->setHandlerBlockOrFunc(BlockAddress::get(HandlerBB));
1598 TinyPtrVector<BasicBlock *> Targets(HandlerBB);
1599 CatchAction->setReturnTargets(Targets);
1600}
1601
Andrew Kaylor6b67d422015-03-11 23:22:06 +00001602void LandingPadMap::mapLandingPad(const LandingPadInst *LPad) {
1603 // Each instance of this class should only ever be used to map a single
1604 // landing pad.
1605 assert(OriginLPad == nullptr || OriginLPad == LPad);
1606
1607 // If the landing pad has already been mapped, there's nothing more to do.
1608 if (OriginLPad == LPad)
1609 return;
1610
1611 OriginLPad = LPad;
1612
1613 // The landingpad instruction returns an aggregate value. Typically, its
1614 // value will be passed to a pair of extract value instructions and the
Andrew Kaylor64622aa2015-04-01 17:21:25 +00001615 // results of those extracts will have been promoted to reg values before
1616 // this routine is called.
Andrew Kaylor6b67d422015-03-11 23:22:06 +00001617 for (auto *U : LPad->users()) {
1618 const ExtractValueInst *Extract = dyn_cast<ExtractValueInst>(U);
1619 if (!Extract)
1620 continue;
1621 assert(Extract->getNumIndices() == 1 &&
1622 "Unexpected operation: extracting both landing pad values");
1623 unsigned int Idx = *(Extract->idx_begin());
1624 assert((Idx == 0 || Idx == 1) &&
1625 "Unexpected operation: extracting an unknown landing pad element");
1626 if (Idx == 0) {
Andrew Kaylor6b67d422015-03-11 23:22:06 +00001627 ExtractedEHPtrs.push_back(Extract);
1628 } else if (Idx == 1) {
Andrew Kaylor6b67d422015-03-11 23:22:06 +00001629 ExtractedSelectors.push_back(Extract);
1630 }
Andrew Kaylor6b67d422015-03-11 23:22:06 +00001631 }
1632}
1633
Andrew Kaylorf7118ae2015-03-27 22:31:12 +00001634bool LandingPadMap::isOriginLandingPadBlock(const BasicBlock *BB) const {
1635 return BB->getLandingPadInst() == OriginLPad;
1636}
1637
Andrew Kaylor6b67d422015-03-11 23:22:06 +00001638bool LandingPadMap::isLandingPadSpecificInst(const Instruction *Inst) const {
1639 if (Inst == OriginLPad)
1640 return true;
1641 for (auto *Extract : ExtractedEHPtrs) {
1642 if (Inst == Extract)
1643 return true;
1644 }
1645 for (auto *Extract : ExtractedSelectors) {
1646 if (Inst == Extract)
1647 return true;
1648 }
Andrew Kaylor6b67d422015-03-11 23:22:06 +00001649 return false;
1650}
1651
Andrew Kaylor64622aa2015-04-01 17:21:25 +00001652void LandingPadMap::remapEHValues(ValueToValueMapTy &VMap, Value *EHPtrValue,
1653 Value *SelectorValue) const {
1654 // Remap all landing pad extract instructions to the specified values.
1655 for (auto *Extract : ExtractedEHPtrs)
1656 VMap[Extract] = EHPtrValue;
Andrew Kaylor6b67d422015-03-11 23:22:06 +00001657 for (auto *Extract : ExtractedSelectors)
Andrew Kaylor64622aa2015-04-01 17:21:25 +00001658 VMap[Extract] = SelectorValue;
Andrew Kaylor6b67d422015-03-11 23:22:06 +00001659}
1660
Reid Klecknerd5afc62f2015-07-07 23:23:03 +00001661static bool isLocalAddressCall(const Value *V) {
1662 return match(const_cast<Value *>(V), m_Intrinsic<Intrinsic::localaddress>());
Reid Klecknerf14787d2015-04-22 00:07:52 +00001663}
1664
Andrew Kaylorf0f5e462015-03-03 20:00:16 +00001665CloningDirector::CloningAction WinEHCloningDirectorBase::handleInstruction(
Andrew Kaylor1476e6d2015-02-24 20:49:35 +00001666 ValueToValueMapTy &VMap, const Instruction *Inst, BasicBlock *NewBB) {
Andrew Kaylor6b67d422015-03-11 23:22:06 +00001667 // If this is one of the boilerplate landing pad instructions, skip it.
1668 // The instruction will have already been remapped in VMap.
1669 if (LPadMap.isLandingPadSpecificInst(Inst))
1670 return CloningDirector::SkipInstruction;
Andrew Kaylor1476e6d2015-02-24 20:49:35 +00001671
Andrew Kaylora6c5b962015-05-20 23:22:24 +00001672 // Nested landing pads that have not already been outlined will be cloned as
1673 // stubs, with just the landingpad instruction and an unreachable instruction.
1674 // When all landingpads have been outlined, we'll replace this with the
1675 // llvm.eh.actions call and indirect branch created when the landing pad was
1676 // outlined.
Andrew Kayloraa92ab02015-04-03 19:37:50 +00001677 if (auto *LPad = dyn_cast<LandingPadInst>(Inst)) {
1678 return handleLandingPad(VMap, LPad, NewBB);
Andrew Kaylor6b67d422015-03-11 23:22:06 +00001679 }
1680
Andrew Kaylora6c5b962015-05-20 23:22:24 +00001681 // Nested landing pads that have already been outlined will be cloned in their
1682 // outlined form, but we need to intercept the ibr instruction to filter out
1683 // targets that do not return to the handler we are outlining.
1684 if (auto *IBr = dyn_cast<IndirectBrInst>(Inst)) {
1685 return handleIndirectBr(VMap, IBr, NewBB);
1686 }
1687
Andrew Kaylor6b67d422015-03-11 23:22:06 +00001688 if (auto *Invoke = dyn_cast<InvokeInst>(Inst))
1689 return handleInvoke(VMap, Invoke, NewBB);
1690
Andrew Kaylorf0f5e462015-03-03 20:00:16 +00001691 if (auto *Resume = dyn_cast<ResumeInst>(Inst))
1692 return handleResume(VMap, Resume, NewBB);
Andrew Kaylor1476e6d2015-02-24 20:49:35 +00001693
Andrew Kaylorea8df612015-04-17 23:05:43 +00001694 if (auto *Cmp = dyn_cast<CmpInst>(Inst))
1695 return handleCompare(VMap, Cmp, NewBB);
1696
Andrew Kaylorf0f5e462015-03-03 20:00:16 +00001697 if (match(Inst, m_Intrinsic<Intrinsic::eh_begincatch>()))
1698 return handleBeginCatch(VMap, Inst, NewBB);
1699 if (match(Inst, m_Intrinsic<Intrinsic::eh_endcatch>()))
1700 return handleEndCatch(VMap, Inst, NewBB);
1701 if (match(Inst, m_Intrinsic<Intrinsic::eh_typeid_for>()))
1702 return handleTypeIdFor(VMap, Inst, NewBB);
Andrew Kaylor1476e6d2015-02-24 20:49:35 +00001703
Reid Klecknerd5afc62f2015-07-07 23:23:03 +00001704 // When outlining llvm.localaddress(), remap that to the second argument,
Reid Klecknerf14787d2015-04-22 00:07:52 +00001705 // which is the FP of the parent.
Reid Klecknerd5afc62f2015-07-07 23:23:03 +00001706 if (isLocalAddressCall(Inst)) {
Reid Klecknerbcda1cd2015-04-29 22:49:54 +00001707 VMap[Inst] = ParentFP;
Reid Klecknerf14787d2015-04-22 00:07:52 +00001708 return CloningDirector::SkipInstruction;
1709 }
1710
Andrew Kaylor1476e6d2015-02-24 20:49:35 +00001711 // Continue with the default cloning behavior.
1712 return CloningDirector::CloneInstruction;
1713}
1714
Andrew Kayloraa92ab02015-04-03 19:37:50 +00001715CloningDirector::CloningAction WinEHCatchDirector::handleLandingPad(
1716 ValueToValueMapTy &VMap, const LandingPadInst *LPad, BasicBlock *NewBB) {
Andrew Kaylora6c5b962015-05-20 23:22:24 +00001717 // If the instruction after the landing pad is a call to llvm.eh.actions
1718 // the landing pad has already been outlined. In this case, we should
1719 // clone it because it may return to a block in the handler we are
1720 // outlining now that would otherwise be unreachable. The landing pads
1721 // are sorted before outlining begins to enable this case to work
1722 // properly.
1723 const Instruction *NextI = LPad->getNextNode();
1724 if (match(NextI, m_Intrinsic<Intrinsic::eh_actions>()))
1725 return CloningDirector::CloneInstruction;
1726
1727 // If the landing pad hasn't been outlined yet, the landing pad we are
1728 // outlining now does not dominate it and so it cannot return to a block
1729 // in this handler. In that case, we can just insert a stub landing
1730 // pad now and patch it up later.
Andrew Kayloraa92ab02015-04-03 19:37:50 +00001731 Instruction *NewInst = LPad->clone();
1732 if (LPad->hasName())
1733 NewInst->setName(LPad->getName());
1734 // Save this correlation for later processing.
1735 NestedLPtoOriginalLP[cast<LandingPadInst>(NewInst)] = LPad;
1736 VMap[LPad] = NewInst;
1737 BasicBlock::InstListType &InstList = NewBB->getInstList();
1738 InstList.push_back(NewInst);
1739 InstList.push_back(new UnreachableInst(NewBB->getContext()));
1740 return CloningDirector::StopCloningBB;
1741}
1742
Andrew Kaylorf0f5e462015-03-03 20:00:16 +00001743CloningDirector::CloningAction WinEHCatchDirector::handleBeginCatch(
1744 ValueToValueMapTy &VMap, const Instruction *Inst, BasicBlock *NewBB) {
1745 // The argument to the call is some form of the first element of the
1746 // landingpad aggregate value, but that doesn't matter. It isn't used
1747 // here.
Reid Kleckner42366532015-03-03 23:20:30 +00001748 // The second argument is an outparameter where the exception object will be
1749 // stored. Typically the exception object is a scalar, but it can be an
1750 // aggregate when catching by value.
1751 // FIXME: Leave something behind to indicate where the exception object lives
1752 // for this handler. Should it be part of llvm.eh.actions?
Andrew Kaylor6b67d422015-03-11 23:22:06 +00001753 assert(ExceptionObjectVar == nullptr && "Multiple calls to "
1754 "llvm.eh.begincatch found while "
1755 "outlining catch handler.");
1756 ExceptionObjectVar = Inst->getOperand(1)->stripPointerCasts();
Reid Kleckner3567d272015-04-02 21:13:31 +00001757 if (isa<ConstantPointerNull>(ExceptionObjectVar))
1758 return CloningDirector::SkipInstruction;
Reid Kleckneraab30e12015-04-03 18:18:06 +00001759 assert(cast<AllocaInst>(ExceptionObjectVar)->isStaticAlloca() &&
1760 "catch parameter is not static alloca");
Reid Kleckner3567d272015-04-02 21:13:31 +00001761 Materializer.escapeCatchObject(ExceptionObjectVar);
Andrew Kaylorf0f5e462015-03-03 20:00:16 +00001762 return CloningDirector::SkipInstruction;
1763}
1764
1765CloningDirector::CloningAction
1766WinEHCatchDirector::handleEndCatch(ValueToValueMapTy &VMap,
1767 const Instruction *Inst, BasicBlock *NewBB) {
1768 auto *IntrinCall = dyn_cast<IntrinsicInst>(Inst);
1769 // It might be interesting to track whether or not we are inside a catch
1770 // function, but that might make the algorithm more brittle than it needs
1771 // to be.
1772
1773 // The end catch call can occur in one of two places: either in a
Andrew Kaylor6b67d422015-03-11 23:22:06 +00001774 // landingpad block that is part of the catch handlers exception mechanism,
Andrew Kaylorf7118ae2015-03-27 22:31:12 +00001775 // or at the end of the catch block. However, a catch-all handler may call
1776 // end catch from the original landing pad. If the call occurs in a nested
1777 // landing pad block, we must skip it and continue so that the landing pad
1778 // gets cloned.
1779 auto *ParentBB = IntrinCall->getParent();
1780 if (ParentBB->isLandingPad() && !LPadMap.isOriginLandingPadBlock(ParentBB))
Andrew Kaylorf0f5e462015-03-03 20:00:16 +00001781 return CloningDirector::SkipInstruction;
1782
Andrew Kaylor64622aa2015-04-01 17:21:25 +00001783 // If an end catch occurs anywhere else we want to terminate the handler
1784 // with a return to the code that follows the endcatch call. If the
1785 // next instruction is not an unconditional branch, we need to split the
1786 // block to provide a clear target for the return instruction.
1787 BasicBlock *ContinueBB;
1788 auto Next = std::next(BasicBlock::const_iterator(IntrinCall));
1789 const BranchInst *Branch = dyn_cast<BranchInst>(Next);
1790 if (!Branch || !Branch->isUnconditional()) {
1791 // We're interrupting the cloning process at this location, so the
1792 // const_cast we're doing here will not cause a problem.
1793 ContinueBB = SplitBlock(const_cast<BasicBlock *>(ParentBB),
1794 const_cast<Instruction *>(cast<Instruction>(Next)));
1795 } else {
1796 ContinueBB = Branch->getSuccessor(0);
1797 }
Andrew Kaylorf0f5e462015-03-03 20:00:16 +00001798
Andrew Kaylor64622aa2015-04-01 17:21:25 +00001799 ReturnInst::Create(NewBB->getContext(), BlockAddress::get(ContinueBB), NewBB);
1800 ReturnTargets.push_back(ContinueBB);
Andrew Kaylorf0f5e462015-03-03 20:00:16 +00001801
1802 // We just added a terminator to the cloned block.
1803 // Tell the caller to stop processing the current basic block so that
1804 // the branch instruction will be skipped.
1805 return CloningDirector::StopCloningBB;
1806}
1807
1808CloningDirector::CloningAction WinEHCatchDirector::handleTypeIdFor(
1809 ValueToValueMapTy &VMap, const Instruction *Inst, BasicBlock *NewBB) {
1810 auto *IntrinCall = dyn_cast<IntrinsicInst>(Inst);
1811 Value *Selector = IntrinCall->getArgOperand(0)->stripPointerCasts();
1812 // This causes a replacement that will collapse the landing pad CFG based
1813 // on the filter function we intend to match.
1814 if (Selector == CurrentSelector)
1815 VMap[Inst] = ConstantInt::get(SelectorIDType, 1);
1816 else
1817 VMap[Inst] = ConstantInt::get(SelectorIDType, 0);
1818 // Tell the caller not to clone this instruction.
1819 return CloningDirector::SkipInstruction;
1820}
1821
Andrew Kaylora6c5b962015-05-20 23:22:24 +00001822CloningDirector::CloningAction WinEHCatchDirector::handleIndirectBr(
1823 ValueToValueMapTy &VMap,
1824 const IndirectBrInst *IBr,
1825 BasicBlock *NewBB) {
1826 // If this indirect branch is not part of a landing pad block, just clone it.
1827 const BasicBlock *ParentBB = IBr->getParent();
1828 if (!ParentBB->isLandingPad())
1829 return CloningDirector::CloneInstruction;
1830
1831 // If it is part of a landing pad, we want to filter out target blocks
1832 // that are not part of the handler we are outlining.
1833 const LandingPadInst *LPad = ParentBB->getLandingPadInst();
1834
1835 // Save this correlation for later processing.
1836 NestedLPtoOriginalLP[cast<LandingPadInst>(VMap[LPad])] = LPad;
1837
1838 // We should only get here for landing pads that have already been outlined.
1839 assert(match(LPad->getNextNode(), m_Intrinsic<Intrinsic::eh_actions>()));
1840
1841 // Copy the indirectbr, but only include targets that were previously
1842 // identified as EH blocks and are dominated by the nested landing pad.
1843 SetVector<const BasicBlock *> ReturnTargets;
1844 for (int I = 0, E = IBr->getNumDestinations(); I < E; ++I) {
1845 auto *TargetBB = IBr->getDestination(I);
1846 if (EHBlocks.count(const_cast<BasicBlock*>(TargetBB)) &&
1847 DT->dominates(ParentBB, TargetBB)) {
1848 DEBUG(dbgs() << " Adding destination " << TargetBB->getName() << "\n");
1849 ReturnTargets.insert(TargetBB);
1850 }
1851 }
1852 IndirectBrInst *NewBranch =
1853 IndirectBrInst::Create(const_cast<Value *>(IBr->getAddress()),
1854 ReturnTargets.size(), NewBB);
1855 for (auto *Target : ReturnTargets)
1856 NewBranch->addDestination(const_cast<BasicBlock*>(Target));
1857
1858 // The operands and targets of the branch instruction are remapped later
1859 // because it is a terminator. Tell the cloning code to clone the
1860 // blocks we just added to the target list.
1861 return CloningDirector::CloneSuccessors;
1862}
1863
Andrew Kaylorf0f5e462015-03-03 20:00:16 +00001864CloningDirector::CloningAction
Andrew Kaylor6b67d422015-03-11 23:22:06 +00001865WinEHCatchDirector::handleInvoke(ValueToValueMapTy &VMap,
1866 const InvokeInst *Invoke, BasicBlock *NewBB) {
1867 return CloningDirector::CloneInstruction;
1868}
1869
1870CloningDirector::CloningAction
Andrew Kaylorf0f5e462015-03-03 20:00:16 +00001871WinEHCatchDirector::handleResume(ValueToValueMapTy &VMap,
1872 const ResumeInst *Resume, BasicBlock *NewBB) {
1873 // Resume instructions shouldn't be reachable from catch handlers.
1874 // We still need to handle it, but it will be pruned.
1875 BasicBlock::InstListType &InstList = NewBB->getInstList();
1876 InstList.push_back(new UnreachableInst(NewBB->getContext()));
1877 return CloningDirector::StopCloningBB;
1878}
1879
Andrew Kaylorea8df612015-04-17 23:05:43 +00001880CloningDirector::CloningAction
1881WinEHCatchDirector::handleCompare(ValueToValueMapTy &VMap,
1882 const CmpInst *Compare, BasicBlock *NewBB) {
1883 const IntrinsicInst *IntrinCall = nullptr;
1884 if (match(Compare->getOperand(0), m_Intrinsic<Intrinsic::eh_typeid_for>())) {
1885 IntrinCall = dyn_cast<IntrinsicInst>(Compare->getOperand(0));
Andrew Kaylor91307432015-04-28 22:01:51 +00001886 } else if (match(Compare->getOperand(1),
1887 m_Intrinsic<Intrinsic::eh_typeid_for>())) {
Andrew Kaylorea8df612015-04-17 23:05:43 +00001888 IntrinCall = dyn_cast<IntrinsicInst>(Compare->getOperand(1));
1889 }
1890 if (IntrinCall) {
1891 Value *Selector = IntrinCall->getArgOperand(0)->stripPointerCasts();
1892 // This causes a replacement that will collapse the landing pad CFG based
1893 // on the filter function we intend to match.
1894 if (Selector == CurrentSelector->stripPointerCasts()) {
1895 VMap[Compare] = ConstantInt::get(SelectorIDType, 1);
Andrew Kaylor91307432015-04-28 22:01:51 +00001896 } else {
Andrew Kaylorea8df612015-04-17 23:05:43 +00001897 VMap[Compare] = ConstantInt::get(SelectorIDType, 0);
1898 }
1899 return CloningDirector::SkipInstruction;
1900 }
1901 return CloningDirector::CloneInstruction;
1902}
1903
Andrew Kayloraa92ab02015-04-03 19:37:50 +00001904CloningDirector::CloningAction WinEHCleanupDirector::handleLandingPad(
1905 ValueToValueMapTy &VMap, const LandingPadInst *LPad, BasicBlock *NewBB) {
1906 // The MS runtime will terminate the process if an exception occurs in a
1907 // cleanup handler, so we shouldn't encounter landing pads in the actual
1908 // cleanup code, but they may appear in catch blocks. Depending on where
1909 // we started cloning we may see one, but it will get dropped during dead
1910 // block pruning.
1911 Instruction *NewInst = new UnreachableInst(NewBB->getContext());
1912 VMap[LPad] = NewInst;
1913 BasicBlock::InstListType &InstList = NewBB->getInstList();
1914 InstList.push_back(NewInst);
1915 return CloningDirector::StopCloningBB;
1916}
1917
Andrew Kaylorf0f5e462015-03-03 20:00:16 +00001918CloningDirector::CloningAction WinEHCleanupDirector::handleBeginCatch(
1919 ValueToValueMapTy &VMap, const Instruction *Inst, BasicBlock *NewBB) {
Andrew Kaylorea8df612015-04-17 23:05:43 +00001920 // Cleanup code may flow into catch blocks or the catch block may be part
1921 // of a branch that will be optimized away. We'll insert a return
1922 // instruction now, but it may be pruned before the cloning process is
1923 // complete.
1924 ReturnInst::Create(NewBB->getContext(), nullptr, NewBB);
Andrew Kaylorf0f5e462015-03-03 20:00:16 +00001925 return CloningDirector::StopCloningBB;
1926}
1927
1928CloningDirector::CloningAction WinEHCleanupDirector::handleEndCatch(
1929 ValueToValueMapTy &VMap, const Instruction *Inst, BasicBlock *NewBB) {
Andrew Kaylorbb111322015-04-07 21:30:23 +00001930 // Cleanup handlers nested within catch handlers may begin with a call to
1931 // eh.endcatch. We can just ignore that instruction.
1932 return CloningDirector::SkipInstruction;
Andrew Kaylorf0f5e462015-03-03 20:00:16 +00001933}
1934
1935CloningDirector::CloningAction WinEHCleanupDirector::handleTypeIdFor(
1936 ValueToValueMapTy &VMap, const Instruction *Inst, BasicBlock *NewBB) {
Andrew Kaylor6b67d422015-03-11 23:22:06 +00001937 // If we encounter a selector comparison while cloning a cleanup handler,
1938 // we want to stop cloning immediately. Anything after the dispatch
1939 // will be outlined into a different handler.
1940 BasicBlock *CatchHandler;
1941 Constant *Selector;
1942 BasicBlock *NextBB;
1943 if (isSelectorDispatch(const_cast<BasicBlock *>(Inst->getParent()),
1944 CatchHandler, Selector, NextBB)) {
1945 ReturnInst::Create(NewBB->getContext(), nullptr, NewBB);
1946 return CloningDirector::StopCloningBB;
1947 }
1948 // If eg.typeid.for is called for any other reason, it can be ignored.
Andrew Kaylorf0f5e462015-03-03 20:00:16 +00001949 VMap[Inst] = ConstantInt::get(SelectorIDType, 0);
Andrew Kaylorf0f5e462015-03-03 20:00:16 +00001950 return CloningDirector::SkipInstruction;
1951}
1952
Andrew Kaylora6c5b962015-05-20 23:22:24 +00001953CloningDirector::CloningAction WinEHCleanupDirector::handleIndirectBr(
1954 ValueToValueMapTy &VMap,
1955 const IndirectBrInst *IBr,
1956 BasicBlock *NewBB) {
1957 // No special handling is required for cleanup cloning.
1958 return CloningDirector::CloneInstruction;
1959}
1960
Andrew Kaylor6b67d422015-03-11 23:22:06 +00001961CloningDirector::CloningAction WinEHCleanupDirector::handleInvoke(
1962 ValueToValueMapTy &VMap, const InvokeInst *Invoke, BasicBlock *NewBB) {
1963 // All invokes in cleanup handlers can be replaced with calls.
1964 SmallVector<Value *, 16> CallArgs(Invoke->op_begin(), Invoke->op_end() - 3);
1965 // Insert a normal call instruction...
1966 CallInst *NewCall =
1967 CallInst::Create(const_cast<Value *>(Invoke->getCalledValue()), CallArgs,
1968 Invoke->getName(), NewBB);
1969 NewCall->setCallingConv(Invoke->getCallingConv());
1970 NewCall->setAttributes(Invoke->getAttributes());
1971 NewCall->setDebugLoc(Invoke->getDebugLoc());
1972 VMap[Invoke] = NewCall;
1973
Reid Kleckner6e48a822015-04-10 16:26:42 +00001974 // Remap the operands.
1975 llvm::RemapInstruction(NewCall, VMap, RF_None, nullptr, &Materializer);
1976
Andrew Kaylor6b67d422015-03-11 23:22:06 +00001977 // Insert an unconditional branch to the normal destination.
1978 BranchInst::Create(Invoke->getNormalDest(), NewBB);
1979
1980 // The unwind destination won't be cloned into the new function, so
1981 // we don't need to clean up its phi nodes.
1982
1983 // We just added a terminator to the cloned block.
1984 // Tell the caller to stop processing the current basic block.
Reid Kleckner6e48a822015-04-10 16:26:42 +00001985 return CloningDirector::CloneSuccessors;
Andrew Kaylor6b67d422015-03-11 23:22:06 +00001986}
1987
Andrew Kaylorf0f5e462015-03-03 20:00:16 +00001988CloningDirector::CloningAction WinEHCleanupDirector::handleResume(
1989 ValueToValueMapTy &VMap, const ResumeInst *Resume, BasicBlock *NewBB) {
1990 ReturnInst::Create(NewBB->getContext(), nullptr, NewBB);
1991
1992 // We just added a terminator to the cloned block.
1993 // Tell the caller to stop processing the current basic block so that
1994 // the branch instruction will be skipped.
1995 return CloningDirector::StopCloningBB;
1996}
1997
Andrew Kaylorea8df612015-04-17 23:05:43 +00001998CloningDirector::CloningAction
1999WinEHCleanupDirector::handleCompare(ValueToValueMapTy &VMap,
2000 const CmpInst *Compare, BasicBlock *NewBB) {
Andrew Kaylorea8df612015-04-17 23:05:43 +00002001 if (match(Compare->getOperand(0), m_Intrinsic<Intrinsic::eh_typeid_for>()) ||
2002 match(Compare->getOperand(1), m_Intrinsic<Intrinsic::eh_typeid_for>())) {
2003 VMap[Compare] = ConstantInt::get(SelectorIDType, 1);
2004 return CloningDirector::SkipInstruction;
2005 }
2006 return CloningDirector::CloneInstruction;
Andrew Kaylorea8df612015-04-17 23:05:43 +00002007}
2008
Andrew Kaylor1476e6d2015-02-24 20:49:35 +00002009WinEHFrameVariableMaterializer::WinEHFrameVariableMaterializer(
Reid Klecknerbcda1cd2015-04-29 22:49:54 +00002010 Function *OutlinedFn, Value *ParentFP, FrameVarInfoMap &FrameVarInfo)
Andrew Kaylor1476e6d2015-02-24 20:49:35 +00002011 : FrameVarInfo(FrameVarInfo), Builder(OutlinedFn->getContext()) {
Andrew Kayloraa92ab02015-04-03 19:37:50 +00002012 BasicBlock *EntryBB = &OutlinedFn->getEntryBlock();
Reid Klecknerbcda1cd2015-04-29 22:49:54 +00002013
2014 // New allocas should be inserted in the entry block, but after the parent FP
2015 // is established if it is an instruction.
2016 Instruction *InsertPoint = EntryBB->getFirstInsertionPt();
2017 if (auto *FPInst = dyn_cast<Instruction>(ParentFP))
2018 InsertPoint = FPInst->getNextNode();
2019 Builder.SetInsertPoint(EntryBB, InsertPoint);
Andrew Kaylor1476e6d2015-02-24 20:49:35 +00002020}
2021
2022Value *WinEHFrameVariableMaterializer::materializeValueFor(Value *V) {
Reid Klecknerfd7df282015-04-22 21:05:21 +00002023 // If we're asked to materialize a static alloca, we temporarily create an
2024 // alloca in the outlined function and add this to the FrameVarInfo map. When
2025 // all the outlining is complete, we'll replace these temporary allocas with
Reid Kleckner60381792015-07-07 22:25:32 +00002026 // calls to llvm.localrecover.
Andrew Kaylor1476e6d2015-02-24 20:49:35 +00002027 if (auto *AV = dyn_cast<AllocaInst>(V)) {
Reid Klecknerfd7df282015-04-22 21:05:21 +00002028 assert(AV->isStaticAlloca() &&
2029 "cannot materialize un-demoted dynamic alloca");
Andrew Kaylor72029c62015-03-03 00:41:03 +00002030 AllocaInst *NewAlloca = dyn_cast<AllocaInst>(AV->clone());
2031 Builder.Insert(NewAlloca, AV->getName());
Reid Klecknercfb9ce52015-03-05 18:26:34 +00002032 FrameVarInfo[AV].push_back(NewAlloca);
Andrew Kaylor1476e6d2015-02-24 20:49:35 +00002033 return NewAlloca;
2034 }
2035
Andrew Kaylor72029c62015-03-03 00:41:03 +00002036 if (isa<Instruction>(V) || isa<Argument>(V)) {
Reid Klecknerd1b38c42015-05-06 18:45:24 +00002037 Function *Parent = isa<Instruction>(V)
2038 ? cast<Instruction>(V)->getParent()->getParent()
2039 : cast<Argument>(V)->getParent();
2040 errs()
2041 << "Failed to demote instruction used in exception handler of function "
2042 << GlobalValue::getRealLinkageName(Parent->getName()) << ":\n";
Reid Klecknerfd7df282015-04-22 21:05:21 +00002043 errs() << " " << *V << '\n';
2044 report_fatal_error("WinEHPrepare failed to demote instruction");
Andrew Kaylor1476e6d2015-02-24 20:49:35 +00002045 }
Andrew Kaylor1476e6d2015-02-24 20:49:35 +00002046
Andrew Kaylor72029c62015-03-03 00:41:03 +00002047 // Don't materialize other values.
Andrew Kaylor1476e6d2015-02-24 20:49:35 +00002048 return nullptr;
2049}
Andrew Kaylor6b67d422015-03-11 23:22:06 +00002050
Reid Kleckner3567d272015-04-02 21:13:31 +00002051void WinEHFrameVariableMaterializer::escapeCatchObject(Value *V) {
2052 // Catch parameter objects have to live in the parent frame. When we see a use
2053 // of a catch parameter, add a sentinel to the multimap to indicate that it's
2054 // used from another handler. This will prevent us from trying to sink the
2055 // alloca into the handler and ensure that the catch parameter is present in
Reid Kleckner60381792015-07-07 22:25:32 +00002056 // the call to llvm.localescape.
Reid Kleckner3567d272015-04-02 21:13:31 +00002057 FrameVarInfo[V].push_back(getCatchObjectSentinel());
2058}
2059
Andrew Kaylor6b67d422015-03-11 23:22:06 +00002060// This function maps the catch and cleanup handlers that are reachable from the
2061// specified landing pad. The landing pad sequence will have this basic shape:
2062//
2063// <cleanup handler>
2064// <selector comparison>
2065// <catch handler>
2066// <cleanup handler>
2067// <selector comparison>
2068// <catch handler>
2069// <cleanup handler>
2070// ...
2071//
2072// Any of the cleanup slots may be absent. The cleanup slots may be occupied by
2073// any arbitrary control flow, but all paths through the cleanup code must
2074// eventually reach the next selector comparison and no path can skip to a
2075// different selector comparisons, though some paths may terminate abnormally.
2076// Therefore, we will use a depth first search from the start of any given
2077// cleanup block and stop searching when we find the next selector comparison.
2078//
2079// If the landingpad instruction does not have a catch clause, we will assume
2080// that any instructions other than selector comparisons and catch handlers can
2081// be ignored. In practice, these will only be the boilerplate instructions.
2082//
2083// The catch handlers may also have any control structure, but we are only
2084// interested in the start of the catch handlers, so we don't need to actually
2085// follow the flow of the catch handlers. The start of the catch handlers can
2086// be located from the compare instructions, but they can be skipped in the
2087// flow by following the contrary branch.
2088void WinEHPrepare::mapLandingPadBlocks(LandingPadInst *LPad,
2089 LandingPadActions &Actions) {
2090 unsigned int NumClauses = LPad->getNumClauses();
2091 unsigned int HandlersFound = 0;
2092 BasicBlock *BB = LPad->getParent();
2093
2094 DEBUG(dbgs() << "Mapping landing pad: " << BB->getName() << "\n");
2095
2096 if (NumClauses == 0) {
Reid Kleckner9405ef02015-04-10 23:12:29 +00002097 findCleanupHandlers(Actions, BB, nullptr);
Andrew Kaylor6b67d422015-03-11 23:22:06 +00002098 return;
2099 }
2100
2101 VisitedBlockSet VisitedBlocks;
2102
2103 while (HandlersFound != NumClauses) {
Andrew Kaylor6b67d422015-03-11 23:22:06 +00002104 BasicBlock *NextBB = nullptr;
2105
Andrew Kaylor20ae2a32015-04-23 22:38:36 +00002106 // Skip over filter clauses.
2107 if (LPad->isFilter(HandlersFound)) {
2108 ++HandlersFound;
2109 continue;
2110 }
2111
Andrew Kaylor6b67d422015-03-11 23:22:06 +00002112 // See if the clause we're looking for is a catch-all.
2113 // If so, the catch begins immediately.
Andrew Kaylor91307432015-04-28 22:01:51 +00002114 Constant *ExpectedSelector =
2115 LPad->getClause(HandlersFound)->stripPointerCasts();
Andrew Kaylorea8df612015-04-17 23:05:43 +00002116 if (isa<ConstantPointerNull>(ExpectedSelector)) {
Andrew Kaylor6b67d422015-03-11 23:22:06 +00002117 // The catch all must occur last.
2118 assert(HandlersFound == NumClauses - 1);
2119
Andrew Kaylorea8df612015-04-17 23:05:43 +00002120 // There can be additional selector dispatches in the call chain that we
2121 // need to ignore.
2122 BasicBlock *CatchBlock = nullptr;
2123 Constant *Selector;
2124 while (BB && isSelectorDispatch(BB, CatchBlock, Selector, NextBB)) {
2125 DEBUG(dbgs() << " Found extra catch dispatch in block "
Andrew Kaylor91307432015-04-28 22:01:51 +00002126 << CatchBlock->getName() << "\n");
Andrew Kaylorea8df612015-04-17 23:05:43 +00002127 BB = NextBB;
2128 }
2129
Andrew Kaylor6b67d422015-03-11 23:22:06 +00002130 // Add the catch handler to the action list.
Andrew Kaylorf18771b2015-04-20 18:48:45 +00002131 CatchHandler *Action = nullptr;
2132 if (CatchHandlerMap.count(BB) && CatchHandlerMap[BB] != nullptr) {
2133 // If the CatchHandlerMap already has an entry for this BB, re-use it.
2134 Action = CatchHandlerMap[BB];
2135 assert(Action->getSelector() == ExpectedSelector);
2136 } else {
Andrew Kaylor046f7b42015-04-28 21:54:14 +00002137 // We don't expect a selector dispatch, but there may be a call to
2138 // llvm.eh.begincatch, which separates catch handling code from
2139 // cleanup code in the same control flow. This call looks for the
2140 // begincatch intrinsic.
2141 Action = findCatchHandler(BB, NextBB, VisitedBlocks);
2142 if (Action) {
2143 // For C++ EH, check if there is any interesting cleanup code before
2144 // we begin the catch. This is important because cleanups cannot
2145 // rethrow exceptions but code called from catches can. For SEH, it
2146 // isn't important if some finally code before a catch-all is executed
2147 // out of line or after recovering from the exception.
2148 if (Personality == EHPersonality::MSVC_CXX)
2149 findCleanupHandlers(Actions, BB, BB);
2150 } else {
2151 // If an action was not found, it means that the control flows
2152 // directly into the catch-all handler and there is no cleanup code.
2153 // That's an expected situation and we must create a catch action.
2154 // Since this is a catch-all handler, the selector won't actually
2155 // appear in the code anywhere. ExpectedSelector here is the constant
2156 // null ptr that we got from the landing pad instruction.
2157 Action = new CatchHandler(BB, ExpectedSelector, nullptr);
2158 CatchHandlerMap[BB] = Action;
2159 }
Andrew Kaylorf18771b2015-04-20 18:48:45 +00002160 }
Andrew Kaylor6b67d422015-03-11 23:22:06 +00002161 Actions.insertCatchHandler(Action);
2162 DEBUG(dbgs() << " Catch all handler at block " << BB->getName() << "\n");
2163 ++HandlersFound;
Reid Kleckner0f9e27a2015-03-18 20:26:53 +00002164
2165 // Once we reach a catch-all, don't expect to hit a resume instruction.
2166 BB = nullptr;
2167 break;
Andrew Kaylor6b67d422015-03-11 23:22:06 +00002168 }
2169
2170 CatchHandler *CatchAction = findCatchHandler(BB, NextBB, VisitedBlocks);
Andrew Kaylor41758512015-04-20 22:04:09 +00002171 assert(CatchAction);
2172
Andrew Kaylor6b67d422015-03-11 23:22:06 +00002173 // See if there is any interesting code executed before the dispatch.
Reid Kleckner9405ef02015-04-10 23:12:29 +00002174 findCleanupHandlers(Actions, BB, CatchAction->getStartBlock());
Andrew Kaylor6b67d422015-03-11 23:22:06 +00002175
Andrew Kaylorea8df612015-04-17 23:05:43 +00002176 // When the source program contains multiple nested try blocks the catch
2177 // handlers can get strung together in such a way that we can encounter
2178 // a dispatch for a selector that we've already had a handler for.
2179 if (CatchAction->getSelector()->stripPointerCasts() == ExpectedSelector) {
2180 ++HandlersFound;
2181
2182 // Add the catch handler to the action list.
2183 DEBUG(dbgs() << " Found catch dispatch in block "
2184 << CatchAction->getStartBlock()->getName() << "\n");
2185 Actions.insertCatchHandler(CatchAction);
2186 } else {
Andrew Kaylor41758512015-04-20 22:04:09 +00002187 // Under some circumstances optimized IR will flow unconditionally into a
2188 // handler block without checking the selector. This can only happen if
2189 // the landing pad has a catch-all handler and the handler for the
Benjamin Kramerdf005cb2015-08-08 18:27:36 +00002190 // preceding catch clause is identical to the catch-call handler
Andrew Kaylor41758512015-04-20 22:04:09 +00002191 // (typically an empty catch). In this case, the handler must be shared
2192 // by all remaining clauses.
2193 if (isa<ConstantPointerNull>(
2194 CatchAction->getSelector()->stripPointerCasts())) {
2195 DEBUG(dbgs() << " Applying early catch-all handler in block "
2196 << CatchAction->getStartBlock()->getName()
2197 << " to all remaining clauses.\n");
2198 Actions.insertCatchHandler(CatchAction);
2199 return;
2200 }
2201
Andrew Kaylorea8df612015-04-17 23:05:43 +00002202 DEBUG(dbgs() << " Found extra catch dispatch in block "
2203 << CatchAction->getStartBlock()->getName() << "\n");
2204 }
Andrew Kaylor6b67d422015-03-11 23:22:06 +00002205
2206 // Move on to the block after the catch handler.
2207 BB = NextBB;
2208 }
2209
Reid Kleckner0f9e27a2015-03-18 20:26:53 +00002210 // If we didn't wind up in a catch-all, see if there is any interesting code
2211 // executed before the resume.
Reid Kleckner9405ef02015-04-10 23:12:29 +00002212 findCleanupHandlers(Actions, BB, BB);
Andrew Kaylor6b67d422015-03-11 23:22:06 +00002213
2214 // It's possible that some optimization moved code into a landingpad that
2215 // wasn't
2216 // previously being used for cleanup. If that happens, we need to execute
2217 // that
2218 // extra code from a cleanup handler.
2219 if (Actions.includesCleanup() && !LPad->isCleanup())
2220 LPad->setCleanup(true);
2221}
2222
2223// This function searches starting with the input block for the next
2224// block that terminates with a branch whose condition is based on a selector
2225// comparison. This may be the input block. See the mapLandingPadBlocks
2226// comments for a discussion of control flow assumptions.
2227//
2228CatchHandler *WinEHPrepare::findCatchHandler(BasicBlock *BB,
2229 BasicBlock *&NextBB,
2230 VisitedBlockSet &VisitedBlocks) {
2231 // See if we've already found a catch handler use it.
2232 // Call count() first to avoid creating a null entry for blocks
2233 // we haven't seen before.
2234 if (CatchHandlerMap.count(BB) && CatchHandlerMap[BB] != nullptr) {
2235 CatchHandler *Action = cast<CatchHandler>(CatchHandlerMap[BB]);
2236 NextBB = Action->getNextBB();
2237 return Action;
2238 }
2239
2240 // VisitedBlocks applies only to the current search. We still
2241 // need to consider blocks that we've visited while mapping other
2242 // landing pads.
2243 VisitedBlocks.insert(BB);
2244
2245 BasicBlock *CatchBlock = nullptr;
2246 Constant *Selector = nullptr;
2247
2248 // If this is the first time we've visited this block from any landing pad
2249 // look to see if it is a selector dispatch block.
2250 if (!CatchHandlerMap.count(BB)) {
2251 if (isSelectorDispatch(BB, CatchBlock, Selector, NextBB)) {
2252 CatchHandler *Action = new CatchHandler(BB, Selector, NextBB);
2253 CatchHandlerMap[BB] = Action;
2254 return Action;
2255 }
Andrew Kaylor41758512015-04-20 22:04:09 +00002256 // If we encounter a block containing an llvm.eh.begincatch before we
2257 // find a selector dispatch block, the handler is assumed to be
2258 // reached unconditionally. This happens for catch-all blocks, but
2259 // it can also happen for other catch handlers that have been combined
2260 // with the catch-all handler during optimization.
2261 if (isCatchBlock(BB)) {
2262 PointerType *Int8PtrTy = Type::getInt8PtrTy(BB->getContext());
2263 Constant *NullSelector = ConstantPointerNull::get(Int8PtrTy);
2264 CatchHandler *Action = new CatchHandler(BB, NullSelector, nullptr);
2265 CatchHandlerMap[BB] = Action;
2266 return Action;
2267 }
Andrew Kaylor6b67d422015-03-11 23:22:06 +00002268 }
2269
2270 // Visit each successor, looking for the dispatch.
2271 // FIXME: We expect to find the dispatch quickly, so this will probably
2272 // work better as a breadth first search.
2273 for (BasicBlock *Succ : successors(BB)) {
2274 if (VisitedBlocks.count(Succ))
2275 continue;
2276
2277 CatchHandler *Action = findCatchHandler(Succ, NextBB, VisitedBlocks);
2278 if (Action)
2279 return Action;
2280 }
2281 return nullptr;
2282}
2283
Reid Kleckner9405ef02015-04-10 23:12:29 +00002284// These are helper functions to combine repeated code from findCleanupHandlers.
2285static void createCleanupHandler(LandingPadActions &Actions,
2286 CleanupHandlerMapTy &CleanupHandlerMap,
2287 BasicBlock *BB) {
Andrew Kaylor6b67d422015-03-11 23:22:06 +00002288 CleanupHandler *Action = new CleanupHandler(BB);
2289 CleanupHandlerMap[BB] = Action;
Reid Kleckner9405ef02015-04-10 23:12:29 +00002290 Actions.insertCleanupHandler(Action);
2291 DEBUG(dbgs() << " Found cleanup code in block "
2292 << Action->getStartBlock()->getName() << "\n");
2293}
2294
Reid Kleckner9405ef02015-04-10 23:12:29 +00002295static CallSite matchOutlinedFinallyCall(BasicBlock *BB,
2296 Instruction *MaybeCall) {
2297 // Look for finally blocks that Clang has already outlined for us.
Reid Klecknerd5afc62f2015-07-07 23:23:03 +00002298 // %fp = call i8* @llvm.localaddress()
Reid Kleckner9405ef02015-04-10 23:12:29 +00002299 // call void @"fin$parent"(iN 1, i8* %fp)
Reid Klecknerd5afc62f2015-07-07 23:23:03 +00002300 if (isLocalAddressCall(MaybeCall) && MaybeCall != BB->getTerminator())
Reid Kleckner9405ef02015-04-10 23:12:29 +00002301 MaybeCall = MaybeCall->getNextNode();
2302 CallSite FinallyCall(MaybeCall);
2303 if (!FinallyCall || FinallyCall.arg_size() != 2)
2304 return CallSite();
2305 if (!match(FinallyCall.getArgument(0), m_SpecificInt(1)))
2306 return CallSite();
Reid Klecknerd5afc62f2015-07-07 23:23:03 +00002307 if (!isLocalAddressCall(FinallyCall.getArgument(1)))
Reid Kleckner9405ef02015-04-10 23:12:29 +00002308 return CallSite();
2309 return FinallyCall;
2310}
2311
2312static BasicBlock *followSingleUnconditionalBranches(BasicBlock *BB) {
2313 // Skip single ubr blocks.
2314 while (BB->getFirstNonPHIOrDbg() == BB->getTerminator()) {
2315 auto *Br = dyn_cast<BranchInst>(BB->getTerminator());
2316 if (Br && Br->isUnconditional())
2317 BB = Br->getSuccessor(0);
2318 else
2319 return BB;
2320 }
2321 return BB;
Andrew Kaylor6b67d422015-03-11 23:22:06 +00002322}
2323
2324// This function searches starting with the input block for the next block that
2325// contains code that is not part of a catch handler and would not be eliminated
2326// during handler outlining.
2327//
Reid Kleckner9405ef02015-04-10 23:12:29 +00002328void WinEHPrepare::findCleanupHandlers(LandingPadActions &Actions,
2329 BasicBlock *StartBB, BasicBlock *EndBB) {
Andrew Kaylor6b67d422015-03-11 23:22:06 +00002330 // Here we will skip over the following:
2331 //
2332 // landing pad prolog:
2333 //
2334 // Unconditional branches
2335 //
2336 // Selector dispatch
2337 //
2338 // Resume pattern
2339 //
2340 // Anything else marks the start of an interesting block
2341
2342 BasicBlock *BB = StartBB;
2343 // Anything other than an unconditional branch will kick us out of this loop
2344 // one way or another.
2345 while (BB) {
Reid Kleckner9405ef02015-04-10 23:12:29 +00002346 BB = followSingleUnconditionalBranches(BB);
Andrew Kaylor6b67d422015-03-11 23:22:06 +00002347 // If we've already scanned this block, don't scan it again. If it is
2348 // a cleanup block, there will be an action in the CleanupHandlerMap.
2349 // If we've scanned it and it is not a cleanup block, there will be a
2350 // nullptr in the CleanupHandlerMap. If we have not scanned it, there will
2351 // be no entry in the CleanupHandlerMap. We must call count() first to
2352 // avoid creating a null entry for blocks we haven't scanned.
2353 if (CleanupHandlerMap.count(BB)) {
2354 if (auto *Action = CleanupHandlerMap[BB]) {
Reid Kleckner9405ef02015-04-10 23:12:29 +00002355 Actions.insertCleanupHandler(Action);
2356 DEBUG(dbgs() << " Found cleanup code in block "
Andrew Kaylor91307432015-04-28 22:01:51 +00002357 << Action->getStartBlock()->getName() << "\n");
Reid Kleckner9405ef02015-04-10 23:12:29 +00002358 // FIXME: This cleanup might chain into another, and we need to discover
2359 // that.
2360 return;
Andrew Kaylor6b67d422015-03-11 23:22:06 +00002361 } else {
2362 // Here we handle the case where the cleanup handler map contains a
2363 // value for this block but the value is a nullptr. This means that
2364 // we have previously analyzed the block and determined that it did
2365 // not contain any cleanup code. Based on the earlier analysis, we
Eric Christopher572e03a2015-06-19 01:53:21 +00002366 // know the block must end in either an unconditional branch, a
Andrew Kaylor6b67d422015-03-11 23:22:06 +00002367 // resume or a conditional branch that is predicated on a comparison
2368 // with a selector. Either the resume or the selector dispatch
2369 // would terminate the search for cleanup code, so the unconditional
2370 // branch is the only case for which we might need to continue
2371 // searching.
Reid Kleckner9405ef02015-04-10 23:12:29 +00002372 BasicBlock *SuccBB = followSingleUnconditionalBranches(BB);
2373 if (SuccBB == BB || SuccBB == EndBB)
2374 return;
Andrew Kaylor6b67d422015-03-11 23:22:06 +00002375 BB = SuccBB;
2376 continue;
2377 }
2378 }
2379
2380 // Create an entry in the cleanup handler map for this block. Initially
2381 // we create an entry that says this isn't a cleanup block. If we find
2382 // cleanup code, the caller will replace this entry.
2383 CleanupHandlerMap[BB] = nullptr;
2384
2385 TerminatorInst *Terminator = BB->getTerminator();
2386
2387 // Landing pad blocks have extra instructions we need to accept.
2388 LandingPadMap *LPadMap = nullptr;
2389 if (BB->isLandingPad()) {
2390 LandingPadInst *LPad = BB->getLandingPadInst();
2391 LPadMap = &LPadMaps[LPad];
2392 if (!LPadMap->isInitialized())
2393 LPadMap->mapLandingPad(LPad);
2394 }
2395
2396 // Look for the bare resume pattern:
Andrew Kaylor64622aa2015-04-01 17:21:25 +00002397 // %lpad.val1 = insertvalue { i8*, i32 } undef, i8* %exn, 0
2398 // %lpad.val2 = insertvalue { i8*, i32 } %lpad.val1, i32 %sel, 1
Andrew Kaylor6b67d422015-03-11 23:22:06 +00002399 // resume { i8*, i32 } %lpad.val2
2400 if (auto *Resume = dyn_cast<ResumeInst>(Terminator)) {
2401 InsertValueInst *Insert1 = nullptr;
2402 InsertValueInst *Insert2 = nullptr;
Reid Kleckner0f9e27a2015-03-18 20:26:53 +00002403 Value *ResumeVal = Resume->getOperand(0);
Reid Kleckner1c130bb2015-04-16 17:02:23 +00002404 // If the resume value isn't a phi or landingpad value, it should be a
2405 // series of insertions. Identify them so we can avoid them when scanning
2406 // for cleanups.
2407 if (!isa<PHINode>(ResumeVal) && !isa<LandingPadInst>(ResumeVal)) {
Reid Kleckner0f9e27a2015-03-18 20:26:53 +00002408 Insert2 = dyn_cast<InsertValueInst>(ResumeVal);
Andrew Kaylor6b67d422015-03-11 23:22:06 +00002409 if (!Insert2)
Reid Kleckner9405ef02015-04-10 23:12:29 +00002410 return createCleanupHandler(Actions, CleanupHandlerMap, BB);
Andrew Kaylor6b67d422015-03-11 23:22:06 +00002411 Insert1 = dyn_cast<InsertValueInst>(Insert2->getAggregateOperand());
2412 if (!Insert1)
Reid Kleckner9405ef02015-04-10 23:12:29 +00002413 return createCleanupHandler(Actions, CleanupHandlerMap, BB);
Andrew Kaylor6b67d422015-03-11 23:22:06 +00002414 }
2415 for (BasicBlock::iterator II = BB->getFirstNonPHIOrDbg(), IE = BB->end();
2416 II != IE; ++II) {
2417 Instruction *Inst = II;
2418 if (LPadMap && LPadMap->isLandingPadSpecificInst(Inst))
2419 continue;
2420 if (Inst == Insert1 || Inst == Insert2 || Inst == Resume)
2421 continue;
2422 if (!Inst->hasOneUse() ||
2423 (Inst->user_back() != Insert1 && Inst->user_back() != Insert2)) {
Reid Kleckner9405ef02015-04-10 23:12:29 +00002424 return createCleanupHandler(Actions, CleanupHandlerMap, BB);
Andrew Kaylor6b67d422015-03-11 23:22:06 +00002425 }
2426 }
Reid Kleckner9405ef02015-04-10 23:12:29 +00002427 return;
Andrew Kaylor6b67d422015-03-11 23:22:06 +00002428 }
2429
2430 BranchInst *Branch = dyn_cast<BranchInst>(Terminator);
Andrew Kaylor64622aa2015-04-01 17:21:25 +00002431 if (Branch && Branch->isConditional()) {
2432 // Look for the selector dispatch.
2433 // %2 = call i32 @llvm.eh.typeid.for(i8* bitcast (i8** @_ZTIf to i8*))
2434 // %matches = icmp eq i32 %sel, %2
2435 // br i1 %matches, label %catch14, label %eh.resume
2436 CmpInst *Compare = dyn_cast<CmpInst>(Branch->getCondition());
2437 if (!Compare || !Compare->isEquality())
Reid Kleckner9405ef02015-04-10 23:12:29 +00002438 return createCleanupHandler(Actions, CleanupHandlerMap, BB);
Andrew Kaylorbb111322015-04-07 21:30:23 +00002439 for (BasicBlock::iterator II = BB->getFirstNonPHIOrDbg(), IE = BB->end();
2440 II != IE; ++II) {
Andrew Kaylor64622aa2015-04-01 17:21:25 +00002441 Instruction *Inst = II;
2442 if (LPadMap && LPadMap->isLandingPadSpecificInst(Inst))
2443 continue;
2444 if (Inst == Compare || Inst == Branch)
2445 continue;
2446 if (match(Inst, m_Intrinsic<Intrinsic::eh_typeid_for>()))
2447 continue;
Reid Kleckner9405ef02015-04-10 23:12:29 +00002448 return createCleanupHandler(Actions, CleanupHandlerMap, BB);
Andrew Kaylor64622aa2015-04-01 17:21:25 +00002449 }
2450 // The selector dispatch block should always terminate our search.
2451 assert(BB == EndBB);
Reid Kleckner9405ef02015-04-10 23:12:29 +00002452 return;
2453 }
2454
2455 if (isAsynchronousEHPersonality(Personality)) {
2456 // If this is a landingpad block, split the block at the first non-landing
2457 // pad instruction.
2458 Instruction *MaybeCall = BB->getFirstNonPHIOrDbg();
2459 if (LPadMap) {
2460 while (MaybeCall != BB->getTerminator() &&
2461 LPadMap->isLandingPadSpecificInst(MaybeCall))
2462 MaybeCall = MaybeCall->getNextNode();
2463 }
2464
Reid Kleckner6511c8b2015-07-01 20:59:25 +00002465 // Look for outlined finally calls on x64, since those happen to match the
2466 // prototype provided by the runtime.
2467 if (TheTriple.getArch() == Triple::x86_64) {
2468 if (CallSite FinallyCall = matchOutlinedFinallyCall(BB, MaybeCall)) {
2469 Function *Fin = FinallyCall.getCalledFunction();
2470 assert(Fin && "outlined finally call should be direct");
2471 auto *Action = new CleanupHandler(BB);
2472 Action->setHandlerBlockOrFunc(Fin);
2473 Actions.insertCleanupHandler(Action);
2474 CleanupHandlerMap[BB] = Action;
2475 DEBUG(dbgs() << " Found frontend-outlined finally call to "
2476 << Fin->getName() << " in block "
2477 << Action->getStartBlock()->getName() << "\n");
Reid Kleckner9405ef02015-04-10 23:12:29 +00002478
Reid Kleckner6511c8b2015-07-01 20:59:25 +00002479 // Split the block if there were more interesting instructions and
2480 // look for finally calls in the normal successor block.
2481 BasicBlock *SuccBB = BB;
2482 if (FinallyCall.getInstruction() != BB->getTerminator() &&
2483 FinallyCall.getInstruction()->getNextNode() !=
2484 BB->getTerminator()) {
Andrew Kaylor046f7b42015-04-28 21:54:14 +00002485 SuccBB =
Reid Kleckner6511c8b2015-07-01 20:59:25 +00002486 SplitBlock(BB, FinallyCall.getInstruction()->getNextNode(), DT);
Reid Kleckner9405ef02015-04-10 23:12:29 +00002487 } else {
Reid Kleckner6511c8b2015-07-01 20:59:25 +00002488 if (FinallyCall.isInvoke()) {
2489 SuccBB = cast<InvokeInst>(FinallyCall.getInstruction())
2490 ->getNormalDest();
2491 } else {
2492 SuccBB = BB->getUniqueSuccessor();
2493 assert(SuccBB &&
2494 "splitOutlinedFinallyCalls didn't insert a branch");
2495 }
Reid Kleckner9405ef02015-04-10 23:12:29 +00002496 }
Reid Kleckner6511c8b2015-07-01 20:59:25 +00002497 BB = SuccBB;
2498 if (BB == EndBB)
2499 return;
2500 continue;
Reid Kleckner9405ef02015-04-10 23:12:29 +00002501 }
Reid Kleckner9405ef02015-04-10 23:12:29 +00002502 }
Andrew Kaylor64622aa2015-04-01 17:21:25 +00002503 }
Andrew Kaylor6b67d422015-03-11 23:22:06 +00002504
Andrew Kaylor64622aa2015-04-01 17:21:25 +00002505 // Anything else is either a catch block or interesting cleanup code.
Andrew Kaylorbb111322015-04-07 21:30:23 +00002506 for (BasicBlock::iterator II = BB->getFirstNonPHIOrDbg(), IE = BB->end();
2507 II != IE; ++II) {
Andrew Kaylor64622aa2015-04-01 17:21:25 +00002508 Instruction *Inst = II;
2509 if (LPadMap && LPadMap->isLandingPadSpecificInst(Inst))
2510 continue;
2511 // Unconditional branches fall through to this loop.
2512 if (Inst == Branch)
2513 continue;
2514 // If this is a catch block, there is no cleanup code to be found.
2515 if (match(Inst, m_Intrinsic<Intrinsic::eh_begincatch>()))
Reid Kleckner9405ef02015-04-10 23:12:29 +00002516 return;
Andrew Kayloraa92ab02015-04-03 19:37:50 +00002517 // If this a nested landing pad, it may contain an endcatch call.
2518 if (match(Inst, m_Intrinsic<Intrinsic::eh_endcatch>()))
Reid Kleckner9405ef02015-04-10 23:12:29 +00002519 return;
Andrew Kaylor64622aa2015-04-01 17:21:25 +00002520 // Anything else makes this interesting cleanup code.
Reid Kleckner9405ef02015-04-10 23:12:29 +00002521 return createCleanupHandler(Actions, CleanupHandlerMap, BB);
Andrew Kaylor64622aa2015-04-01 17:21:25 +00002522 }
2523
2524 // Only unconditional branches in empty blocks should get this far.
2525 assert(Branch && Branch->isUnconditional());
2526 if (BB == EndBB)
Reid Kleckner9405ef02015-04-10 23:12:29 +00002527 return;
Andrew Kaylor64622aa2015-04-01 17:21:25 +00002528 BB = Branch->getSuccessor(0);
Andrew Kaylor6b67d422015-03-11 23:22:06 +00002529 }
Andrew Kaylor6b67d422015-03-11 23:22:06 +00002530}
Andrew Kayloraa92ab02015-04-03 19:37:50 +00002531
2532// This is a public function, declared in WinEHFuncInfo.h and is also
2533// referenced by WinEHNumbering in FunctionLoweringInfo.cpp.
Benjamin Kramera48e0652015-05-16 15:40:03 +00002534void llvm::parseEHActions(
2535 const IntrinsicInst *II,
2536 SmallVectorImpl<std::unique_ptr<ActionHandler>> &Actions) {
Reid Klecknerf12c0302015-06-09 21:42:19 +00002537 assert(II->getIntrinsicID() == Intrinsic::eh_actions &&
2538 "attempted to parse non eh.actions intrinsic");
Andrew Kayloraa92ab02015-04-03 19:37:50 +00002539 for (unsigned I = 0, E = II->getNumArgOperands(); I != E;) {
2540 uint64_t ActionKind =
Andrew Kaylorbb111322015-04-07 21:30:23 +00002541 cast<ConstantInt>(II->getArgOperand(I))->getZExtValue();
Andrew Kayloraa92ab02015-04-03 19:37:50 +00002542 if (ActionKind == /*catch=*/1) {
2543 auto *Selector = cast<Constant>(II->getArgOperand(I + 1));
2544 ConstantInt *EHObjIndex = cast<ConstantInt>(II->getArgOperand(I + 2));
2545 int64_t EHObjIndexVal = EHObjIndex->getSExtValue();
2546 Constant *Handler = cast<Constant>(II->getArgOperand(I + 3));
2547 I += 4;
Benjamin Kramera48e0652015-05-16 15:40:03 +00002548 auto CH = make_unique<CatchHandler>(/*BB=*/nullptr, Selector,
2549 /*NextBB=*/nullptr);
Andrew Kayloraa92ab02015-04-03 19:37:50 +00002550 CH->setHandlerBlockOrFunc(Handler);
2551 CH->setExceptionVarIndex(EHObjIndexVal);
Benjamin Kramera48e0652015-05-16 15:40:03 +00002552 Actions.push_back(std::move(CH));
David Majnemer69132a72015-04-03 22:49:05 +00002553 } else if (ActionKind == 0) {
Andrew Kayloraa92ab02015-04-03 19:37:50 +00002554 Constant *Handler = cast<Constant>(II->getArgOperand(I + 1));
2555 I += 2;
Benjamin Kramera48e0652015-05-16 15:40:03 +00002556 auto CH = make_unique<CleanupHandler>(/*BB=*/nullptr);
Andrew Kayloraa92ab02015-04-03 19:37:50 +00002557 CH->setHandlerBlockOrFunc(Handler);
Benjamin Kramera48e0652015-05-16 15:40:03 +00002558 Actions.push_back(std::move(CH));
David Majnemer69132a72015-04-03 22:49:05 +00002559 } else {
2560 llvm_unreachable("Expected either a catch or cleanup handler!");
Andrew Kayloraa92ab02015-04-03 19:37:50 +00002561 }
2562 }
2563 std::reverse(Actions.begin(), Actions.end());
2564}
Reid Klecknerfe4d4912015-05-28 22:00:24 +00002565
2566namespace {
2567struct WinEHNumbering {
2568 WinEHNumbering(WinEHFuncInfo &FuncInfo) : FuncInfo(FuncInfo),
2569 CurrentBaseState(-1), NextState(0) {}
2570
2571 WinEHFuncInfo &FuncInfo;
2572 int CurrentBaseState;
2573 int NextState;
2574
2575 SmallVector<std::unique_ptr<ActionHandler>, 4> HandlerStack;
2576 SmallPtrSet<const Function *, 4> VisitedHandlers;
2577
2578 int currentEHNumber() const {
2579 return HandlerStack.empty() ? CurrentBaseState : HandlerStack.back()->getEHState();
2580 }
2581
2582 void createUnwindMapEntry(int ToState, ActionHandler *AH);
2583 void createTryBlockMapEntry(int TryLow, int TryHigh,
2584 ArrayRef<CatchHandler *> Handlers);
2585 void processCallSite(MutableArrayRef<std::unique_ptr<ActionHandler>> Actions,
2586 ImmutableCallSite CS);
2587 void popUnmatchedActions(int FirstMismatch);
2588 void calculateStateNumbers(const Function &F);
2589 void findActionRootLPads(const Function &F);
2590};
Alexander Kornienkof00654e2015-06-23 09:49:53 +00002591}
Reid Klecknerfe4d4912015-05-28 22:00:24 +00002592
David Majnemer0ad363e2015-08-18 19:07:12 +00002593static int addUnwindMapEntry(WinEHFuncInfo &FuncInfo, int ToState,
2594 const Value *V) {
Reid Klecknerfe4d4912015-05-28 22:00:24 +00002595 WinEHUnwindMapEntry UME;
2596 UME.ToState = ToState;
David Majnemer0ad363e2015-08-18 19:07:12 +00002597 UME.Cleanup = V;
Reid Klecknerfe4d4912015-05-28 22:00:24 +00002598 FuncInfo.UnwindMap.push_back(UME);
David Majnemer0ad363e2015-08-18 19:07:12 +00002599 return FuncInfo.getLastStateNumber();
2600}
2601
2602static void addTryBlockMapEntry(WinEHFuncInfo &FuncInfo, int TryLow,
2603 int TryHigh, int CatchHigh,
2604 ArrayRef<const CatchPadInst *> Handlers) {
2605 WinEHTryBlockMapEntry TBME;
2606 TBME.TryLow = TryLow;
2607 TBME.TryHigh = TryHigh;
2608 TBME.CatchHigh = CatchHigh;
2609 assert(TBME.TryLow <= TBME.TryHigh);
2610 for (const CatchPadInst *CPI : Handlers) {
2611 WinEHHandlerType HT;
2612 Constant *TypeInfo = cast<Constant>(CPI->getArgOperand(0));
2613 if (TypeInfo->isNullValue()) {
2614 HT.Adjectives = 0x40;
2615 HT.TypeDescriptor = nullptr;
2616 } else {
2617 auto *GV = cast<GlobalVariable>(TypeInfo->stripPointerCasts());
2618 // Selectors are always pointers to GlobalVariables with 'struct' type.
2619 // The struct has two fields, adjectives and a type descriptor.
2620 auto *CS = cast<ConstantStruct>(GV->getInitializer());
2621 HT.Adjectives =
2622 cast<ConstantInt>(CS->getAggregateElement(0U))->getZExtValue();
2623 HT.TypeDescriptor =
2624 cast<GlobalVariable>(CS->getAggregateElement(1)->stripPointerCasts());
2625 }
Reid Kleckner0e288232015-08-27 23:27:47 +00002626 HT.Handler = CPI->getNormalDest();
David Majnemer0ad363e2015-08-18 19:07:12 +00002627 // FIXME: Pass CPI->getArgOperand(1).
2628 HT.CatchObjRecoverIdx = -1;
2629 TBME.HandlerArray.push_back(HT);
2630 }
2631 FuncInfo.TryBlockMap.push_back(TBME);
2632}
2633
2634void WinEHNumbering::createUnwindMapEntry(int ToState, ActionHandler *AH) {
2635 Value *V = nullptr;
2636 if (auto *CH = dyn_cast_or_null<CleanupHandler>(AH))
2637 V = cast<Function>(CH->getHandlerBlockOrFunc());
2638 addUnwindMapEntry(FuncInfo, ToState, V);
Reid Klecknerfe4d4912015-05-28 22:00:24 +00002639}
2640
2641void WinEHNumbering::createTryBlockMapEntry(int TryLow, int TryHigh,
2642 ArrayRef<CatchHandler *> Handlers) {
2643 // See if we already have an entry for this set of handlers.
2644 // This is using iterators rather than a range-based for loop because
2645 // if we find the entry we're looking for we'll need the iterator to erase it.
2646 int NumHandlers = Handlers.size();
2647 auto I = FuncInfo.TryBlockMap.begin();
2648 auto E = FuncInfo.TryBlockMap.end();
2649 for ( ; I != E; ++I) {
2650 auto &Entry = *I;
2651 if (Entry.HandlerArray.size() != (size_t)NumHandlers)
2652 continue;
2653 int N;
2654 for (N = 0; N < NumHandlers; ++N) {
Reid Kleckner94b704c2015-09-09 21:10:03 +00002655 if (Entry.HandlerArray[N].Handler.get<const Value *>() !=
2656 Handlers[N]->getHandlerBlockOrFunc())
Reid Klecknerfe4d4912015-05-28 22:00:24 +00002657 break; // breaks out of inner loop
2658 }
2659 // If all the handlers match, this is what we were looking for.
2660 if (N == NumHandlers) {
2661 break;
2662 }
2663 }
2664
2665 // If we found an existing entry for this set of handlers, extend the range
2666 // but move the entry to the end of the map vector. The order of entries
2667 // in the map is critical to the way that the runtime finds handlers.
2668 // FIXME: Depending on what has happened with block ordering, this may
2669 // incorrectly combine entries that should remain separate.
2670 if (I != E) {
2671 // Copy the existing entry.
2672 WinEHTryBlockMapEntry Entry = *I;
2673 Entry.TryLow = std::min(TryLow, Entry.TryLow);
2674 Entry.TryHigh = std::max(TryHigh, Entry.TryHigh);
2675 assert(Entry.TryLow <= Entry.TryHigh);
2676 // Erase the old entry and add this one to the back.
2677 FuncInfo.TryBlockMap.erase(I);
2678 FuncInfo.TryBlockMap.push_back(Entry);
2679 return;
2680 }
2681
2682 // If we didn't find an entry, create a new one.
2683 WinEHTryBlockMapEntry TBME;
2684 TBME.TryLow = TryLow;
2685 TBME.TryHigh = TryHigh;
2686 assert(TBME.TryLow <= TBME.TryHigh);
2687 for (CatchHandler *CH : Handlers) {
2688 WinEHHandlerType HT;
2689 if (CH->getSelector()->isNullValue()) {
2690 HT.Adjectives = 0x40;
2691 HT.TypeDescriptor = nullptr;
2692 } else {
2693 auto *GV = cast<GlobalVariable>(CH->getSelector()->stripPointerCasts());
2694 // Selectors are always pointers to GlobalVariables with 'struct' type.
2695 // The struct has two fields, adjectives and a type descriptor.
2696 auto *CS = cast<ConstantStruct>(GV->getInitializer());
2697 HT.Adjectives =
2698 cast<ConstantInt>(CS->getAggregateElement(0U))->getZExtValue();
2699 HT.TypeDescriptor =
2700 cast<GlobalVariable>(CS->getAggregateElement(1)->stripPointerCasts());
2701 }
2702 HT.Handler = cast<Function>(CH->getHandlerBlockOrFunc());
2703 HT.CatchObjRecoverIdx = CH->getExceptionVarIndex();
2704 TBME.HandlerArray.push_back(HT);
2705 }
2706 FuncInfo.TryBlockMap.push_back(TBME);
2707}
2708
2709static void print_name(const Value *V) {
2710#ifndef NDEBUG
2711 if (!V) {
2712 DEBUG(dbgs() << "null");
2713 return;
2714 }
2715
2716 if (const auto *F = dyn_cast<Function>(V))
2717 DEBUG(dbgs() << F->getName());
2718 else
2719 DEBUG(V->dump());
2720#endif
2721}
2722
2723void WinEHNumbering::processCallSite(
2724 MutableArrayRef<std::unique_ptr<ActionHandler>> Actions,
2725 ImmutableCallSite CS) {
2726 DEBUG(dbgs() << "processCallSite (EH state = " << currentEHNumber()
2727 << ") for: ");
2728 print_name(CS ? CS.getCalledValue() : nullptr);
2729 DEBUG(dbgs() << '\n');
2730
2731 DEBUG(dbgs() << "HandlerStack: \n");
2732 for (int I = 0, E = HandlerStack.size(); I < E; ++I) {
2733 DEBUG(dbgs() << " ");
2734 print_name(HandlerStack[I]->getHandlerBlockOrFunc());
2735 DEBUG(dbgs() << '\n');
2736 }
2737 DEBUG(dbgs() << "Actions: \n");
2738 for (int I = 0, E = Actions.size(); I < E; ++I) {
2739 DEBUG(dbgs() << " ");
2740 print_name(Actions[I]->getHandlerBlockOrFunc());
2741 DEBUG(dbgs() << '\n');
2742 }
2743 int FirstMismatch = 0;
2744 for (int E = std::min(HandlerStack.size(), Actions.size()); FirstMismatch < E;
2745 ++FirstMismatch) {
2746 if (HandlerStack[FirstMismatch]->getHandlerBlockOrFunc() !=
2747 Actions[FirstMismatch]->getHandlerBlockOrFunc())
2748 break;
2749 }
2750
2751 // Remove unmatched actions from the stack and process their EH states.
2752 popUnmatchedActions(FirstMismatch);
2753
2754 DEBUG(dbgs() << "Pushing actions for CallSite: ");
2755 print_name(CS ? CS.getCalledValue() : nullptr);
2756 DEBUG(dbgs() << '\n');
2757
2758 bool LastActionWasCatch = false;
2759 const LandingPadInst *LastRootLPad = nullptr;
2760 for (size_t I = FirstMismatch; I != Actions.size(); ++I) {
2761 // We can reuse eh states when pushing two catches for the same invoke.
2762 bool CurrActionIsCatch = isa<CatchHandler>(Actions[I].get());
2763 auto *Handler = cast<Function>(Actions[I]->getHandlerBlockOrFunc());
2764 // Various conditions can lead to a handler being popped from the
2765 // stack and re-pushed later. That shouldn't create a new state.
2766 // FIXME: Can code optimization lead to re-used handlers?
2767 if (FuncInfo.HandlerEnclosedState.count(Handler)) {
2768 // If we already assigned the state enclosed by this handler re-use it.
2769 Actions[I]->setEHState(FuncInfo.HandlerEnclosedState[Handler]);
2770 continue;
2771 }
2772 const LandingPadInst* RootLPad = FuncInfo.RootLPad[Handler];
2773 if (CurrActionIsCatch && LastActionWasCatch && RootLPad == LastRootLPad) {
2774 DEBUG(dbgs() << "setEHState for handler to " << currentEHNumber() << "\n");
2775 Actions[I]->setEHState(currentEHNumber());
2776 } else {
2777 DEBUG(dbgs() << "createUnwindMapEntry(" << currentEHNumber() << ", ");
2778 print_name(Actions[I]->getHandlerBlockOrFunc());
2779 DEBUG(dbgs() << ") with EH state " << NextState << "\n");
2780 createUnwindMapEntry(currentEHNumber(), Actions[I].get());
2781 DEBUG(dbgs() << "setEHState for handler to " << NextState << "\n");
2782 Actions[I]->setEHState(NextState);
2783 NextState++;
2784 }
2785 HandlerStack.push_back(std::move(Actions[I]));
2786 LastActionWasCatch = CurrActionIsCatch;
2787 LastRootLPad = RootLPad;
2788 }
2789
2790 // This is used to defer numbering states for a handler until after the
2791 // last time it appears in an invoke action list.
2792 if (CS.isInvoke()) {
2793 for (int I = 0, E = HandlerStack.size(); I < E; ++I) {
2794 auto *Handler = cast<Function>(HandlerStack[I]->getHandlerBlockOrFunc());
2795 if (FuncInfo.LastInvoke[Handler] != cast<InvokeInst>(CS.getInstruction()))
2796 continue;
2797 FuncInfo.LastInvokeVisited[Handler] = true;
2798 DEBUG(dbgs() << "Last invoke of ");
2799 print_name(Handler);
2800 DEBUG(dbgs() << " has been visited.\n");
2801 }
2802 }
2803
2804 DEBUG(dbgs() << "In EHState " << currentEHNumber() << " for CallSite: ");
2805 print_name(CS ? CS.getCalledValue() : nullptr);
2806 DEBUG(dbgs() << '\n');
2807}
2808
2809void WinEHNumbering::popUnmatchedActions(int FirstMismatch) {
2810 // Don't recurse while we are looping over the handler stack. Instead, defer
2811 // the numbering of the catch handlers until we are done popping.
2812 SmallVector<CatchHandler *, 4> PoppedCatches;
2813 for (int I = HandlerStack.size() - 1; I >= FirstMismatch; --I) {
2814 std::unique_ptr<ActionHandler> Handler = HandlerStack.pop_back_val();
2815 if (isa<CatchHandler>(Handler.get()))
2816 PoppedCatches.push_back(cast<CatchHandler>(Handler.release()));
2817 }
2818
2819 int TryHigh = NextState - 1;
2820 int LastTryLowIdx = 0;
2821 for (int I = 0, E = PoppedCatches.size(); I != E; ++I) {
2822 CatchHandler *CH = PoppedCatches[I];
2823 DEBUG(dbgs() << "Popped handler with state " << CH->getEHState() << "\n");
2824 if (I + 1 == E || CH->getEHState() != PoppedCatches[I + 1]->getEHState()) {
2825 int TryLow = CH->getEHState();
2826 auto Handlers =
2827 makeArrayRef(&PoppedCatches[LastTryLowIdx], I - LastTryLowIdx + 1);
2828 DEBUG(dbgs() << "createTryBlockMapEntry(" << TryLow << ", " << TryHigh);
2829 for (size_t J = 0; J < Handlers.size(); ++J) {
2830 DEBUG(dbgs() << ", ");
2831 print_name(Handlers[J]->getHandlerBlockOrFunc());
2832 }
2833 DEBUG(dbgs() << ")\n");
2834 createTryBlockMapEntry(TryLow, TryHigh, Handlers);
2835 LastTryLowIdx = I + 1;
2836 }
2837 }
2838
2839 for (CatchHandler *CH : PoppedCatches) {
2840 if (auto *F = dyn_cast<Function>(CH->getHandlerBlockOrFunc())) {
2841 if (FuncInfo.LastInvokeVisited[F]) {
2842 DEBUG(dbgs() << "Assigning base state " << NextState << " to ");
2843 print_name(F);
2844 DEBUG(dbgs() << '\n');
2845 FuncInfo.HandlerBaseState[F] = NextState;
2846 DEBUG(dbgs() << "createUnwindMapEntry(" << currentEHNumber()
2847 << ", null)\n");
2848 createUnwindMapEntry(currentEHNumber(), nullptr);
2849 ++NextState;
2850 calculateStateNumbers(*F);
2851 }
2852 else {
2853 DEBUG(dbgs() << "Deferring handling of ");
2854 print_name(F);
2855 DEBUG(dbgs() << " until last invoke visited.\n");
2856 }
2857 }
2858 delete CH;
2859 }
2860}
2861
2862void WinEHNumbering::calculateStateNumbers(const Function &F) {
2863 auto I = VisitedHandlers.insert(&F);
2864 if (!I.second)
2865 return; // We've already visited this handler, don't renumber it.
2866
2867 int OldBaseState = CurrentBaseState;
2868 if (FuncInfo.HandlerBaseState.count(&F)) {
2869 CurrentBaseState = FuncInfo.HandlerBaseState[&F];
2870 }
2871
2872 size_t SavedHandlerStackSize = HandlerStack.size();
2873
2874 DEBUG(dbgs() << "Calculating state numbers for: " << F.getName() << '\n');
2875 SmallVector<std::unique_ptr<ActionHandler>, 4> ActionList;
2876 for (const BasicBlock &BB : F) {
2877 for (const Instruction &I : BB) {
2878 const auto *CI = dyn_cast<CallInst>(&I);
2879 if (!CI || CI->doesNotThrow())
2880 continue;
2881 processCallSite(None, CI);
2882 }
2883 const auto *II = dyn_cast<InvokeInst>(BB.getTerminator());
2884 if (!II)
2885 continue;
2886 const LandingPadInst *LPI = II->getLandingPadInst();
2887 auto *ActionsCall = dyn_cast<IntrinsicInst>(LPI->getNextNode());
2888 if (!ActionsCall)
2889 continue;
Reid Klecknerfe4d4912015-05-28 22:00:24 +00002890 parseEHActions(ActionsCall, ActionList);
2891 if (ActionList.empty())
2892 continue;
2893 processCallSite(ActionList, II);
2894 ActionList.clear();
David Majnemer0ad363e2015-08-18 19:07:12 +00002895 FuncInfo.EHPadStateMap[LPI] = currentEHNumber();
Reid Klecknerfe4d4912015-05-28 22:00:24 +00002896 DEBUG(dbgs() << "Assigning state " << currentEHNumber()
2897 << " to landing pad at " << LPI->getParent()->getName()
2898 << '\n');
2899 }
2900
2901 // Pop any actions that were pushed on the stack for this function.
2902 popUnmatchedActions(SavedHandlerStackSize);
2903
2904 DEBUG(dbgs() << "Assigning max state " << NextState - 1
2905 << " to " << F.getName() << '\n');
2906 FuncInfo.CatchHandlerMaxState[&F] = NextState - 1;
2907
2908 CurrentBaseState = OldBaseState;
2909}
2910
2911// This function follows the same basic traversal as calculateStateNumbers
2912// but it is necessary to identify the root landing pad associated
2913// with each action before we start assigning state numbers.
2914void WinEHNumbering::findActionRootLPads(const Function &F) {
2915 auto I = VisitedHandlers.insert(&F);
2916 if (!I.second)
2917 return; // We've already visited this handler, don't revisit it.
2918
2919 SmallVector<std::unique_ptr<ActionHandler>, 4> ActionList;
2920 for (const BasicBlock &BB : F) {
2921 const auto *II = dyn_cast<InvokeInst>(BB.getTerminator());
2922 if (!II)
2923 continue;
2924 const LandingPadInst *LPI = II->getLandingPadInst();
2925 auto *ActionsCall = dyn_cast<IntrinsicInst>(LPI->getNextNode());
2926 if (!ActionsCall)
2927 continue;
2928
2929 assert(ActionsCall->getIntrinsicID() == Intrinsic::eh_actions);
2930 parseEHActions(ActionsCall, ActionList);
2931 if (ActionList.empty())
2932 continue;
2933 for (int I = 0, E = ActionList.size(); I < E; ++I) {
2934 if (auto *Handler
2935 = dyn_cast<Function>(ActionList[I]->getHandlerBlockOrFunc())) {
2936 FuncInfo.LastInvoke[Handler] = II;
2937 // Don't replace the root landing pad if we previously saw this
2938 // handler in a different function.
2939 if (FuncInfo.RootLPad.count(Handler) &&
2940 FuncInfo.RootLPad[Handler]->getParent()->getParent() != &F)
2941 continue;
2942 DEBUG(dbgs() << "Setting root lpad for ");
2943 print_name(Handler);
2944 DEBUG(dbgs() << " to " << LPI->getParent()->getName() << '\n');
2945 FuncInfo.RootLPad[Handler] = LPI;
2946 }
2947 }
2948 // Walk the actions again and look for nested handlers. This has to
2949 // happen after all of the actions have been processed in the current
2950 // function.
2951 for (int I = 0, E = ActionList.size(); I < E; ++I)
2952 if (auto *Handler
2953 = dyn_cast<Function>(ActionList[I]->getHandlerBlockOrFunc()))
2954 findActionRootLPads(*Handler);
2955 ActionList.clear();
2956 }
2957}
2958
Reid Kleckner94b704c2015-09-09 21:10:03 +00002959static const CatchPadInst *getSingleCatchPadPredecessor(const BasicBlock *BB) {
2960 for (const BasicBlock *PredBlock : predecessors(BB))
2961 if (auto *CPI = dyn_cast<CatchPadInst>(PredBlock->getFirstNonPHI()))
2962 return CPI;
David Majnemer0ad363e2015-08-18 19:07:12 +00002963 return nullptr;
2964}
2965
Reid Kleckner94b704c2015-09-09 21:10:03 +00002966/// Find all the catchpads that feed directly into the catchendpad. Frontends
2967/// using this personality should ensure that each catchendpad and catchpad has
2968/// one or zero catchpad predecessors.
2969///
2970/// The following C++ generates the IR after it:
2971/// try {
2972/// } catch (A) {
2973/// } catch (B) {
2974/// }
2975///
2976/// IR:
2977/// %catchpad.A
2978/// catchpad [i8* A typeinfo]
2979/// to label %catch.A unwind label %catchpad.B
2980/// %catchpad.B
2981/// catchpad [i8* B typeinfo]
2982/// to label %catch.B unwind label %endcatches
2983/// %endcatches
2984/// catchendblock unwind to caller
2985void findCatchPadsForCatchEndPad(
2986 const BasicBlock *CatchEndBB,
2987 SmallVectorImpl<const CatchPadInst *> &Handlers) {
2988 const CatchPadInst *CPI = getSingleCatchPadPredecessor(CatchEndBB);
2989 while (CPI) {
2990 Handlers.push_back(CPI);
2991 CPI = getSingleCatchPadPredecessor(CPI->getParent());
2992 }
2993 // We've pushed these back into reverse source order. Reverse them to get
2994 // the list back into source order.
2995 std::reverse(Handlers.begin(), Handlers.end());
2996}
2997
David Majnemer0ad363e2015-08-18 19:07:12 +00002998// Given BB which ends in an unwind edge, return the EHPad that this BB belongs
2999// to. If the unwind edge came from an invoke, return null.
3000static const BasicBlock *getEHPadFromPredecessor(const BasicBlock *BB) {
3001 const TerminatorInst *TI = BB->getTerminator();
3002 if (isa<InvokeInst>(TI))
3003 return nullptr;
Reid Kleckner7bb20bd2015-09-10 21:46:36 +00003004 if (TI->isEHPad())
David Majnemer0ad363e2015-08-18 19:07:12 +00003005 return BB;
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +00003006 return cast<CleanupReturnInst>(TI)->getCleanupPad()->getParent();
David Majnemer0ad363e2015-08-18 19:07:12 +00003007}
3008
Reid Kleckner94b704c2015-09-09 21:10:03 +00003009static void calculateExplicitCXXStateNumbers(WinEHFuncInfo &FuncInfo,
3010 const BasicBlock &BB,
3011 int ParentState) {
David Majnemer0ad363e2015-08-18 19:07:12 +00003012 assert(BB.isEHPad());
3013 const Instruction *FirstNonPHI = BB.getFirstNonPHI();
3014 // All catchpad instructions will be handled when we process their
3015 // respective catchendpad instruction.
3016 if (isa<CatchPadInst>(FirstNonPHI))
3017 return;
3018
3019 if (isa<CatchEndPadInst>(FirstNonPHI)) {
David Majnemer0ad363e2015-08-18 19:07:12 +00003020 SmallVector<const CatchPadInst *, 2> Handlers;
Reid Kleckner94b704c2015-09-09 21:10:03 +00003021 findCatchPadsForCatchEndPad(&BB, Handlers);
3022 const BasicBlock *FirstTryPad = Handlers.front()->getParent();
David Majnemer0ad363e2015-08-18 19:07:12 +00003023 int TryLow = addUnwindMapEntry(FuncInfo, ParentState, nullptr);
3024 FuncInfo.EHPadStateMap[Handlers.front()] = TryLow;
Reid Kleckner94b704c2015-09-09 21:10:03 +00003025 for (const BasicBlock *PredBlock : predecessors(FirstTryPad))
David Majnemer0ad363e2015-08-18 19:07:12 +00003026 if ((PredBlock = getEHPadFromPredecessor(PredBlock)))
Reid Kleckner94b704c2015-09-09 21:10:03 +00003027 calculateExplicitCXXStateNumbers(FuncInfo, *PredBlock, TryLow);
David Majnemer0ad363e2015-08-18 19:07:12 +00003028 int CatchLow = addUnwindMapEntry(FuncInfo, ParentState, nullptr);
Reid Kleckner94b704c2015-09-09 21:10:03 +00003029
3030 // catchpads are separate funclets in C++ EH due to the way rethrow works.
3031 // In SEH, they aren't, so no invokes will unwind to the catchendpad.
David Majnemer0ad363e2015-08-18 19:07:12 +00003032 FuncInfo.EHPadStateMap[FirstNonPHI] = CatchLow;
3033 int TryHigh = CatchLow - 1;
3034 for (const BasicBlock *PredBlock : predecessors(&BB))
3035 if ((PredBlock = getEHPadFromPredecessor(PredBlock)))
Reid Kleckner94b704c2015-09-09 21:10:03 +00003036 calculateExplicitCXXStateNumbers(FuncInfo, *PredBlock, CatchLow);
David Majnemer0ad363e2015-08-18 19:07:12 +00003037 int CatchHigh = FuncInfo.getLastStateNumber();
3038 addTryBlockMapEntry(FuncInfo, TryLow, TryHigh, CatchHigh, Handlers);
Reid Kleckner94b704c2015-09-09 21:10:03 +00003039 DEBUG(dbgs() << "TryLow[" << FirstTryPad->getName() << "]: " << TryLow
David Majnemer0ad363e2015-08-18 19:07:12 +00003040 << '\n');
Reid Kleckner94b704c2015-09-09 21:10:03 +00003041 DEBUG(dbgs() << "TryHigh[" << FirstTryPad->getName() << "]: " << TryHigh
David Majnemer0ad363e2015-08-18 19:07:12 +00003042 << '\n');
Reid Kleckner94b704c2015-09-09 21:10:03 +00003043 DEBUG(dbgs() << "CatchHigh[" << FirstTryPad->getName() << "]: " << CatchHigh
David Majnemer0ad363e2015-08-18 19:07:12 +00003044 << '\n');
3045 } else if (isa<CleanupPadInst>(FirstNonPHI)) {
3046 int CleanupState = addUnwindMapEntry(FuncInfo, ParentState, &BB);
3047 FuncInfo.EHPadStateMap[FirstNonPHI] = CleanupState;
3048 DEBUG(dbgs() << "Assigning state #" << CleanupState << " to BB "
3049 << BB.getName() << '\n');
3050 for (const BasicBlock *PredBlock : predecessors(&BB))
3051 if ((PredBlock = getEHPadFromPredecessor(PredBlock)))
Reid Kleckner94b704c2015-09-09 21:10:03 +00003052 calculateExplicitCXXStateNumbers(FuncInfo, *PredBlock, CleanupState);
David Majnemer0ad363e2015-08-18 19:07:12 +00003053 } else if (isa<TerminatePadInst>(FirstNonPHI)) {
3054 report_fatal_error("Not yet implemented!");
3055 } else {
3056 llvm_unreachable("unexpected EH Pad!");
3057 }
3058}
3059
Reid Kleckner94b704c2015-09-09 21:10:03 +00003060static int addSEHHandler(WinEHFuncInfo &FuncInfo, int ParentState,
Reid Kleckner78783912015-09-10 00:25:23 +00003061 const Function *Filter, const BasicBlock *Handler) {
Reid Kleckner94b704c2015-09-09 21:10:03 +00003062 SEHUnwindMapEntry Entry;
3063 Entry.ToState = ParentState;
3064 Entry.Filter = Filter;
3065 Entry.Handler = Handler;
3066 FuncInfo.SEHUnwindMap.push_back(Entry);
3067 return FuncInfo.SEHUnwindMap.size() - 1;
3068}
3069
3070static void calculateExplicitSEHStateNumbers(WinEHFuncInfo &FuncInfo,
3071 const BasicBlock &BB,
3072 int ParentState) {
3073 assert(BB.isEHPad());
3074 const Instruction *FirstNonPHI = BB.getFirstNonPHI();
3075 // All catchpad instructions will be handled when we process their
3076 // respective catchendpad instruction.
3077 if (isa<CatchPadInst>(FirstNonPHI))
3078 return;
3079
3080 if (isa<CatchEndPadInst>(FirstNonPHI)) {
3081 // Extract the filter function and the __except basic block and create a
3082 // state for them.
3083 SmallVector<const CatchPadInst *, 1> Handlers;
3084 findCatchPadsForCatchEndPad(&BB, Handlers);
3085 assert(Handlers.size() == 1 &&
3086 "SEH doesn't have multiple handlers per __try");
3087 const CatchPadInst *CPI = Handlers.front();
3088 const BasicBlock *CatchPadBB = CPI->getParent();
3089 const Function *Filter =
3090 cast<Function>(CPI->getArgOperand(0)->stripPointerCasts());
3091 int TryState =
3092 addSEHHandler(FuncInfo, ParentState, Filter, CPI->getNormalDest());
3093
3094 // Everything in the __try block uses TryState as its parent state.
3095 FuncInfo.EHPadStateMap[CPI] = TryState;
3096 DEBUG(dbgs() << "Assigning state #" << TryState << " to BB "
3097 << CatchPadBB->getName() << '\n');
3098 for (const BasicBlock *PredBlock : predecessors(CatchPadBB))
3099 if ((PredBlock = getEHPadFromPredecessor(PredBlock)))
3100 calculateExplicitSEHStateNumbers(FuncInfo, *PredBlock, TryState);
3101
3102 // Everything in the __except block unwinds to ParentState, just like code
3103 // outside the __try.
3104 FuncInfo.EHPadStateMap[FirstNonPHI] = ParentState;
3105 DEBUG(dbgs() << "Assigning state #" << ParentState << " to BB "
3106 << BB.getName() << '\n');
3107 for (const BasicBlock *PredBlock : predecessors(&BB))
3108 if ((PredBlock = getEHPadFromPredecessor(PredBlock)))
3109 calculateExplicitSEHStateNumbers(FuncInfo, *PredBlock, ParentState);
3110 } else if (isa<CleanupPadInst>(FirstNonPHI)) {
3111 int CleanupState =
3112 addSEHHandler(FuncInfo, ParentState, /*Filter=*/nullptr, &BB);
3113 FuncInfo.EHPadStateMap[FirstNonPHI] = CleanupState;
3114 DEBUG(dbgs() << "Assigning state #" << CleanupState << " to BB "
3115 << BB.getName() << '\n');
3116 for (const BasicBlock *PredBlock : predecessors(&BB))
3117 if ((PredBlock = getEHPadFromPredecessor(PredBlock)))
3118 calculateExplicitSEHStateNumbers(FuncInfo, *PredBlock, CleanupState);
Reid Kleckner7bb20bd2015-09-10 21:46:36 +00003119 } else if (isa<CleanupEndPadInst>(FirstNonPHI)) {
3120 // Anything unwinding through CleanupEndPadInst is in ParentState.
3121 FuncInfo.EHPadStateMap[FirstNonPHI] = ParentState;
3122 DEBUG(dbgs() << "Assigning state #" << ParentState << " to BB "
3123 << BB.getName() << '\n');
3124 for (const BasicBlock *PredBlock : predecessors(&BB))
3125 if ((PredBlock = getEHPadFromPredecessor(PredBlock)))
3126 calculateExplicitSEHStateNumbers(FuncInfo, *PredBlock, ParentState);
Reid Kleckner94b704c2015-09-09 21:10:03 +00003127 } else if (isa<TerminatePadInst>(FirstNonPHI)) {
3128 report_fatal_error("Not yet implemented!");
3129 } else {
3130 llvm_unreachable("unexpected EH Pad!");
3131 }
3132}
3133
3134/// Check if the EH Pad unwinds to caller. Cleanups are a little bit of a
3135/// special case because we have to look at the cleanupret instruction that uses
3136/// the cleanuppad.
3137static bool doesEHPadUnwindToCaller(const Instruction *EHPad) {
3138 auto *CPI = dyn_cast<CleanupPadInst>(EHPad);
3139 if (!CPI)
3140 return EHPad->mayThrow();
3141
3142 // This cleanup does not return or unwind, so we say it unwinds to caller.
3143 if (CPI->use_empty())
3144 return true;
3145
3146 const Instruction *User = CPI->user_back();
3147 if (auto *CRI = dyn_cast<CleanupReturnInst>(User))
3148 return CRI->unwindsToCaller();
3149 return cast<CleanupEndPadInst>(User)->unwindsToCaller();
3150}
3151
3152void llvm::calculateSEHStateNumbers(const Function *ParentFn,
3153 WinEHFuncInfo &FuncInfo) {
3154 // Don't compute state numbers twice.
3155 if (!FuncInfo.SEHUnwindMap.empty())
3156 return;
3157
3158 for (const BasicBlock &BB : *ParentFn) {
3159 if (!BB.isEHPad() || !doesEHPadUnwindToCaller(BB.getFirstNonPHI()))
3160 continue;
3161 calculateExplicitSEHStateNumbers(FuncInfo, BB, -1);
3162 }
3163}
3164
Reid Klecknerfe4d4912015-05-28 22:00:24 +00003165void llvm::calculateWinCXXEHStateNumbers(const Function *ParentFn,
3166 WinEHFuncInfo &FuncInfo) {
3167 // Return if it's already been done.
David Majnemer0ad363e2015-08-18 19:07:12 +00003168 if (!FuncInfo.EHPadStateMap.empty())
3169 return;
3170
3171 bool IsExplicit = false;
3172 for (const BasicBlock &BB : *ParentFn) {
3173 if (!BB.isEHPad())
3174 continue;
Joseph Tremoulet9ce71f72015-09-03 09:09:43 +00003175 const Instruction *FirstNonPHI = BB.getFirstNonPHI();
3176 // Skip cleanupendpads; they are exits, not entries.
3177 if (isa<CleanupEndPadInst>(FirstNonPHI))
3178 continue;
Reid Kleckner94b704c2015-09-09 21:10:03 +00003179 if (!doesEHPadUnwindToCaller(FirstNonPHI))
David Majnemer0ad363e2015-08-18 19:07:12 +00003180 continue;
Reid Kleckner94b704c2015-09-09 21:10:03 +00003181 calculateExplicitCXXStateNumbers(FuncInfo, BB, -1);
David Majnemer0ad363e2015-08-18 19:07:12 +00003182 IsExplicit = true;
3183 }
3184
3185 if (IsExplicit)
Reid Klecknerfe4d4912015-05-28 22:00:24 +00003186 return;
3187
3188 WinEHNumbering Num(FuncInfo);
3189 Num.findActionRootLPads(*ParentFn);
3190 // The VisitedHandlers list is used by both findActionRootLPads and
3191 // calculateStateNumbers, but both functions need to visit all handlers.
3192 Num.VisitedHandlers.clear();
3193 Num.calculateStateNumbers(*ParentFn);
3194 // Pop everything on the handler stack.
3195 // It may be necessary to call this more than once because a handler can
3196 // be pushed on the stack as a result of clearing the stack.
3197 while (!Num.HandlerStack.empty())
3198 Num.processCallSite(None, ImmutableCallSite());
3199}
David Majnemerfd9f4772015-08-11 01:15:26 +00003200
Joseph Tremouletec182852015-08-28 01:12:35 +00003201void WinEHPrepare::colorFunclets(Function &F,
3202 SmallVectorImpl<BasicBlock *> &EntryBlocks) {
3203 SmallVector<std::pair<BasicBlock *, BasicBlock *>, 16> Worklist;
3204 BasicBlock *EntryBlock = &F.getEntryBlock();
David Majnemerfd9f4772015-08-11 01:15:26 +00003205
Joseph Tremouletec182852015-08-28 01:12:35 +00003206 // Build up the color map, which maps each block to its set of 'colors'.
3207 // For any block B, the "colors" of B are the set of funclets F (possibly
3208 // including a root "funclet" representing the main function), such that
3209 // F will need to directly contain B or a copy of B (where the term "directly
3210 // contain" is used to distinguish from being "transitively contained" in
3211 // a nested funclet).
3212 // Use a CFG walk driven by a worklist of (block, color) pairs. The "color"
3213 // sets attached during this processing to a block which is the entry of some
3214 // funclet F is actually the set of F's parents -- i.e. the union of colors
3215 // of all predecessors of F's entry. For all other blocks, the color sets
3216 // are as defined above. A post-pass fixes up the block color map to reflect
3217 // the same sense of "color" for funclet entries as for other blocks.
3218
3219 Worklist.push_back({EntryBlock, EntryBlock});
David Majnemerfd9f4772015-08-11 01:15:26 +00003220
3221 while (!Worklist.empty()) {
Joseph Tremouletec182852015-08-28 01:12:35 +00003222 BasicBlock *Visiting;
3223 BasicBlock *Color;
3224 std::tie(Visiting, Color) = Worklist.pop_back_val();
3225 Instruction *VisitingHead = Visiting->getFirstNonPHI();
Joseph Tremoulet9ce71f72015-09-03 09:09:43 +00003226 if (VisitingHead->isEHPad() && !isa<CatchEndPadInst>(VisitingHead) &&
3227 !isa<CleanupEndPadInst>(VisitingHead)) {
Joseph Tremouletec182852015-08-28 01:12:35 +00003228 // Mark this as a funclet head as a member of itself.
3229 FuncletBlocks[Visiting].insert(Visiting);
3230 // Queue exits with the parent color.
3231 for (User *Exit : VisitingHead->users()) {
3232 for (BasicBlock *Succ :
3233 successors(cast<Instruction>(Exit)->getParent())) {
3234 if (BlockColors[Succ].insert(Color).second) {
3235 Worklist.push_back({Succ, Color});
3236 }
3237 }
3238 }
3239 // Handle CatchPad specially since its successors need different colors.
3240 if (CatchPadInst *CatchPad = dyn_cast<CatchPadInst>(VisitingHead)) {
3241 // Visit the normal successor with the color of the new EH pad, and
3242 // visit the unwind successor with the color of the parent.
3243 BasicBlock *NormalSucc = CatchPad->getNormalDest();
3244 if (BlockColors[NormalSucc].insert(Visiting).second) {
3245 Worklist.push_back({NormalSucc, Visiting});
3246 }
3247 BasicBlock *UnwindSucc = CatchPad->getUnwindDest();
3248 if (BlockColors[UnwindSucc].insert(Color).second) {
3249 Worklist.push_back({UnwindSucc, Color});
3250 }
3251 continue;
3252 }
3253 // Switch color to the current node, except for terminate pads which
3254 // have no bodies and only unwind successors and so need their successors
3255 // visited with the color of the parent.
3256 if (!isa<TerminatePadInst>(VisitingHead))
3257 Color = Visiting;
3258 } else {
3259 // Note that this is a member of the given color.
3260 FuncletBlocks[Color].insert(Visiting);
Joseph Tremouletf3aff312015-09-10 16:51:25 +00003261 }
3262
3263 TerminatorInst *Terminator = Visiting->getTerminator();
3264 if (isa<CleanupReturnInst>(Terminator) ||
3265 isa<CatchReturnInst>(Terminator) ||
3266 isa<CleanupEndPadInst>(Terminator)) {
3267 // These blocks' successors have already been queued with the parent
3268 // color.
3269 continue;
David Majnemerfd9f4772015-08-11 01:15:26 +00003270 }
Joseph Tremouletec182852015-08-28 01:12:35 +00003271 for (BasicBlock *Succ : successors(Visiting)) {
3272 if (isa<CatchEndPadInst>(Succ->getFirstNonPHI())) {
3273 // The catchendpad needs to be visited with the parent's color, not
3274 // the current color. This will happen in the code above that visits
3275 // any catchpad unwind successor with the parent color, so we can
3276 // safely skip this successor here.
3277 continue;
3278 }
3279 if (BlockColors[Succ].insert(Color).second) {
3280 Worklist.push_back({Succ, Color});
3281 }
3282 }
3283 }
David Majnemerfd9f4772015-08-11 01:15:26 +00003284
Joseph Tremouletec182852015-08-28 01:12:35 +00003285 // The processing above actually accumulated the parent set for this
3286 // funclet into the color set for its entry; use the parent set to
3287 // populate the children map, and reset the color set to include just
3288 // the funclet itself (no instruction can target a funclet entry except on
3289 // that transitions to the child funclet).
3290 for (BasicBlock *FuncletEntry : EntryBlocks) {
3291 std::set<BasicBlock *> &ColorMapItem = BlockColors[FuncletEntry];
3292 for (BasicBlock *Parent : ColorMapItem)
3293 FuncletChildren[Parent].insert(FuncletEntry);
3294 ColorMapItem.clear();
3295 ColorMapItem.insert(FuncletEntry);
David Majnemerfd9f4772015-08-11 01:15:26 +00003296 }
3297}
3298
David Majnemerb3d9b962015-09-16 18:40:24 +00003299void WinEHPrepare::demotePHIsOnFunclets(Function &F) {
Joseph Tremouletc9ff9142015-08-13 14:30:10 +00003300 // Strip PHI nodes off of EH pads.
3301 SmallVector<PHINode *, 16> PHINodes;
David Majnemerfd9f4772015-08-11 01:15:26 +00003302 for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE;) {
3303 BasicBlock *BB = FI++;
David Majnemerfd9f4772015-08-11 01:15:26 +00003304 if (!BB->isEHPad())
3305 continue;
David Majnemerfd9f4772015-08-11 01:15:26 +00003306 for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE;) {
3307 Instruction *I = BI++;
3308 auto *PN = dyn_cast<PHINode>(I);
3309 // Stop at the first non-PHI.
3310 if (!PN)
3311 break;
David Majnemerfd9f4772015-08-11 01:15:26 +00003312
Joseph Tremouletc9ff9142015-08-13 14:30:10 +00003313 AllocaInst *SpillSlot = insertPHILoads(PN, F);
3314 if (SpillSlot)
3315 insertPHIStores(PN, SpillSlot);
3316
3317 PHINodes.push_back(PN);
David Majnemerfd9f4772015-08-11 01:15:26 +00003318 }
3319 }
3320
Joseph Tremouletc9ff9142015-08-13 14:30:10 +00003321 for (auto *PN : PHINodes) {
3322 // There may be lingering uses on other EH PHIs being removed
3323 PN->replaceAllUsesWith(UndefValue::get(PN->getType()));
3324 PN->eraseFromParent();
David Majnemerfd9f4772015-08-11 01:15:26 +00003325 }
David Majnemerb3d9b962015-09-16 18:40:24 +00003326}
David Majnemerfd9f4772015-08-11 01:15:26 +00003327
David Majnemerb3d9b962015-09-16 18:40:24 +00003328void WinEHPrepare::demoteUsesBetweenFunclets(Function &F) {
David Majnemerfd9f4772015-08-11 01:15:26 +00003329 // Turn all inter-funclet uses of a Value into loads and stores.
3330 for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE;) {
3331 BasicBlock *BB = FI++;
3332 std::set<BasicBlock *> &ColorsForBB = BlockColors[BB];
3333 for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE;) {
3334 Instruction *I = BI++;
3335 // Funclets are permitted to use static allocas.
3336 if (auto *AI = dyn_cast<AllocaInst>(I))
3337 if (AI->isStaticAlloca())
3338 continue;
3339
Joseph Tremouletc9ff9142015-08-13 14:30:10 +00003340 demoteNonlocalUses(I, ColorsForBB, F);
David Majnemerfd9f4772015-08-11 01:15:26 +00003341 }
3342 }
David Majnemerb3d9b962015-09-16 18:40:24 +00003343}
3344
3345void WinEHPrepare::demoteArgumentUses(Function &F) {
Joseph Tremouletc9ff9142015-08-13 14:30:10 +00003346 // Also demote function parameters used in funclets.
3347 std::set<BasicBlock *> &ColorsForEntry = BlockColors[&F.getEntryBlock()];
3348 for (Argument &Arg : F.args())
3349 demoteNonlocalUses(&Arg, ColorsForEntry, F);
David Majnemerb3d9b962015-09-16 18:40:24 +00003350}
David Majnemerfd9f4772015-08-11 01:15:26 +00003351
David Majnemerb3d9b962015-09-16 18:40:24 +00003352void WinEHPrepare::cloneCommonBlocks(
3353 Function &F, SmallVectorImpl<BasicBlock *> &EntryBlocks) {
David Majnemerfd9f4772015-08-11 01:15:26 +00003354 // We need to clone all blocks which belong to multiple funclets. Values are
3355 // remapped throughout the funclet to propogate both the new instructions
3356 // *and* the new basic blocks themselves.
Joseph Tremouletec182852015-08-28 01:12:35 +00003357 for (BasicBlock *FuncletPadBB : EntryBlocks) {
3358 std::set<BasicBlock *> &BlocksInFunclet = FuncletBlocks[FuncletPadBB];
David Majnemerfd9f4772015-08-11 01:15:26 +00003359
3360 std::map<BasicBlock *, BasicBlock *> Orig2Clone;
3361 ValueToValueMapTy VMap;
3362 for (BasicBlock *BB : BlocksInFunclet) {
3363 std::set<BasicBlock *> &ColorsForBB = BlockColors[BB];
3364 // We don't need to do anything if the block is monochromatic.
3365 size_t NumColorsForBB = ColorsForBB.size();
3366 if (NumColorsForBB == 1)
3367 continue;
3368
David Majnemerfd9f4772015-08-11 01:15:26 +00003369 // Create a new basic block and copy instructions into it!
Joseph Tremouletec182852015-08-28 01:12:35 +00003370 BasicBlock *CBB =
3371 CloneBasicBlock(BB, VMap, Twine(".for.", FuncletPadBB->getName()));
3372 // Insert the clone immediately after the original to ensure determinism
3373 // and to keep the same relative ordering of any funclet's blocks.
3374 CBB->insertInto(&F, BB->getNextNode());
David Majnemerfd9f4772015-08-11 01:15:26 +00003375
3376 // Add basic block mapping.
3377 VMap[BB] = CBB;
3378
3379 // Record delta operations that we need to perform to our color mappings.
3380 Orig2Clone[BB] = CBB;
3381 }
3382
3383 // Update our color mappings to reflect that one block has lost a color and
3384 // another has gained a color.
3385 for (auto &BBMapping : Orig2Clone) {
3386 BasicBlock *OldBlock = BBMapping.first;
3387 BasicBlock *NewBlock = BBMapping.second;
3388
3389 BlocksInFunclet.insert(NewBlock);
3390 BlockColors[NewBlock].insert(FuncletPadBB);
3391
3392 BlocksInFunclet.erase(OldBlock);
3393 BlockColors[OldBlock].erase(FuncletPadBB);
3394 }
3395
3396 // Loop over all of the instructions in the function, fixing up operand
3397 // references as we go. This uses VMap to do all the hard work.
3398 for (BasicBlock *BB : BlocksInFunclet)
3399 // Loop over all instructions, fixing each one as we find it...
3400 for (Instruction &I : *BB)
3401 RemapInstruction(&I, VMap, RF_IgnoreMissingEntries);
3402 }
David Majnemerb3d9b962015-09-16 18:40:24 +00003403}
David Majnemerfd9f4772015-08-11 01:15:26 +00003404
David Majnemerb3d9b962015-09-16 18:40:24 +00003405void WinEHPrepare::removeImplausibleTerminators(Function &F) {
David Majnemer83f4bb22015-08-17 20:56:39 +00003406 // Remove implausible terminators and replace them with UnreachableInst.
3407 for (auto &Funclet : FuncletBlocks) {
3408 BasicBlock *FuncletPadBB = Funclet.first;
3409 std::set<BasicBlock *> &BlocksInFunclet = Funclet.second;
3410 Instruction *FirstNonPHI = FuncletPadBB->getFirstNonPHI();
3411 auto *CatchPad = dyn_cast<CatchPadInst>(FirstNonPHI);
3412 auto *CleanupPad = dyn_cast<CleanupPadInst>(FirstNonPHI);
3413
3414 for (BasicBlock *BB : BlocksInFunclet) {
3415 TerminatorInst *TI = BB->getTerminator();
3416 // CatchPadInst and CleanupPadInst can't transfer control to a ReturnInst.
3417 bool IsUnreachableRet = isa<ReturnInst>(TI) && (CatchPad || CleanupPad);
3418 // The token consumed by a CatchReturnInst must match the funclet token.
3419 bool IsUnreachableCatchret = false;
3420 if (auto *CRI = dyn_cast<CatchReturnInst>(TI))
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +00003421 IsUnreachableCatchret = CRI->getCatchPad() != CatchPad;
Joseph Tremoulet9ce71f72015-09-03 09:09:43 +00003422 // The token consumed by a CleanupReturnInst must match the funclet token.
David Majnemer83f4bb22015-08-17 20:56:39 +00003423 bool IsUnreachableCleanupret = false;
3424 if (auto *CRI = dyn_cast<CleanupReturnInst>(TI))
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +00003425 IsUnreachableCleanupret = CRI->getCleanupPad() != CleanupPad;
Joseph Tremoulet9ce71f72015-09-03 09:09:43 +00003426 // The token consumed by a CleanupEndPadInst must match the funclet token.
3427 bool IsUnreachableCleanupendpad = false;
3428 if (auto *CEPI = dyn_cast<CleanupEndPadInst>(TI))
3429 IsUnreachableCleanupendpad = CEPI->getCleanupPad() != CleanupPad;
3430 if (IsUnreachableRet || IsUnreachableCatchret ||
3431 IsUnreachableCleanupret || IsUnreachableCleanupendpad) {
David Majnemer83f4bb22015-08-17 20:56:39 +00003432 new UnreachableInst(BB->getContext(), TI);
3433 TI->eraseFromParent();
3434 }
3435 }
3436 }
David Majnemerb3d9b962015-09-16 18:40:24 +00003437}
David Majnemer83f4bb22015-08-17 20:56:39 +00003438
David Majnemerb3d9b962015-09-16 18:40:24 +00003439void WinEHPrepare::cleanupPreparedFunclets(Function &F) {
David Majnemerfd9f4772015-08-11 01:15:26 +00003440 // Clean-up some of the mess we made by removing useles PHI nodes, trivial
3441 // branches, etc.
3442 for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE;) {
3443 BasicBlock *BB = FI++;
3444 SimplifyInstructionsInBlock(BB);
3445 ConstantFoldTerminator(BB, /*DeleteDeadConditions=*/true);
3446 MergeBlockIntoPredecessor(BB);
3447 }
3448
David Majnemerfd9f4772015-08-11 01:15:26 +00003449 // We might have some unreachable blocks after cleaning up some impossible
3450 // control flow.
3451 removeUnreachableBlocks(F);
David Majnemerb3d9b962015-09-16 18:40:24 +00003452}
David Majnemerfd9f4772015-08-11 01:15:26 +00003453
David Majnemerb3d9b962015-09-16 18:40:24 +00003454void WinEHPrepare::verifyPreparedFunclets(Function &F) {
David Majnemerfd9f4772015-08-11 01:15:26 +00003455 // Recolor the CFG to verify that all is well.
3456 for (BasicBlock &BB : F) {
3457 size_t NumColors = BlockColors[&BB].size();
3458 assert(NumColors == 1 && "Expected monochromatic BB!");
3459 if (NumColors == 0)
3460 report_fatal_error("Uncolored BB!");
3461 if (NumColors > 1)
3462 report_fatal_error("Multicolor BB!");
3463 bool EHPadHasPHI = BB.isEHPad() && isa<PHINode>(BB.begin());
3464 assert(!EHPadHasPHI && "EH Pad still has a PHI!");
3465 if (EHPadHasPHI)
3466 report_fatal_error("EH Pad still has a PHI!");
3467 }
David Majnemerb3d9b962015-09-16 18:40:24 +00003468}
3469
3470bool WinEHPrepare::prepareExplicitEH(
3471 Function &F, SmallVectorImpl<BasicBlock *> &EntryBlocks) {
3472 // Remove unreachable blocks. It is not valuable to assign them a color and
3473 // their existence can trick us into thinking values are alive when they are
3474 // not.
3475 removeUnreachableBlocks(F);
3476
3477 // Determine which blocks are reachable from which funclet entries.
3478 colorFunclets(F, EntryBlocks);
3479
3480 demotePHIsOnFunclets(F);
3481
3482 demoteUsesBetweenFunclets(F);
3483
3484 demoteArgumentUses(F);
3485
3486 cloneCommonBlocks(F, EntryBlocks);
3487
3488 removeImplausibleTerminators(F);
3489
3490 cleanupPreparedFunclets(F);
3491
3492 verifyPreparedFunclets(F);
David Majnemerfd9f4772015-08-11 01:15:26 +00003493
3494 BlockColors.clear();
3495 FuncletBlocks.clear();
Joseph Tremouletec182852015-08-28 01:12:35 +00003496 FuncletChildren.clear();
David Majnemer0ad363e2015-08-18 19:07:12 +00003497
David Majnemerfd9f4772015-08-11 01:15:26 +00003498 return true;
3499}
Joseph Tremouletc9ff9142015-08-13 14:30:10 +00003500
3501// TODO: Share loads when one use dominates another, or when a catchpad exit
3502// dominates uses (needs dominators).
3503AllocaInst *WinEHPrepare::insertPHILoads(PHINode *PN, Function &F) {
3504 BasicBlock *PHIBlock = PN->getParent();
3505 AllocaInst *SpillSlot = nullptr;
3506
3507 if (isa<CleanupPadInst>(PHIBlock->getFirstNonPHI())) {
3508 // Insert a load in place of the PHI and replace all uses.
3509 SpillSlot = new AllocaInst(PN->getType(), nullptr,
3510 Twine(PN->getName(), ".wineh.spillslot"),
3511 F.getEntryBlock().begin());
3512 Value *V = new LoadInst(SpillSlot, Twine(PN->getName(), ".wineh.reload"),
3513 PHIBlock->getFirstInsertionPt());
3514 PN->replaceAllUsesWith(V);
3515 return SpillSlot;
3516 }
3517
3518 DenseMap<BasicBlock *, Value *> Loads;
3519 for (Value::use_iterator UI = PN->use_begin(), UE = PN->use_end();
3520 UI != UE;) {
3521 Use &U = *UI++;
3522 auto *UsingInst = cast<Instruction>(U.getUser());
3523 BasicBlock *UsingBB = UsingInst->getParent();
3524 if (UsingBB->isEHPad()) {
3525 // Use is on an EH pad phi. Leave it alone; we'll insert loads and
3526 // stores for it separately.
3527 assert(isa<PHINode>(UsingInst));
3528 continue;
3529 }
3530 replaceUseWithLoad(PN, U, SpillSlot, Loads, F);
3531 }
3532 return SpillSlot;
3533}
3534
3535// TODO: improve store placement. Inserting at def is probably good, but need
3536// to be careful not to introduce interfering stores (needs liveness analysis).
3537// TODO: identify related phi nodes that can share spill slots, and share them
3538// (also needs liveness).
3539void WinEHPrepare::insertPHIStores(PHINode *OriginalPHI,
3540 AllocaInst *SpillSlot) {
3541 // Use a worklist of (Block, Value) pairs -- the given Value needs to be
3542 // stored to the spill slot by the end of the given Block.
3543 SmallVector<std::pair<BasicBlock *, Value *>, 4> Worklist;
3544
3545 Worklist.push_back({OriginalPHI->getParent(), OriginalPHI});
3546
3547 while (!Worklist.empty()) {
3548 BasicBlock *EHBlock;
3549 Value *InVal;
3550 std::tie(EHBlock, InVal) = Worklist.pop_back_val();
3551
3552 PHINode *PN = dyn_cast<PHINode>(InVal);
3553 if (PN && PN->getParent() == EHBlock) {
3554 // The value is defined by another PHI we need to remove, with no room to
3555 // insert a store after the PHI, so each predecessor needs to store its
3556 // incoming value.
3557 for (unsigned i = 0, e = PN->getNumIncomingValues(); i < e; ++i) {
3558 Value *PredVal = PN->getIncomingValue(i);
3559
3560 // Undef can safely be skipped.
3561 if (isa<UndefValue>(PredVal))
3562 continue;
3563
3564 insertPHIStore(PN->getIncomingBlock(i), PredVal, SpillSlot, Worklist);
3565 }
3566 } else {
3567 // We need to store InVal, which dominates EHBlock, but can't put a store
3568 // in EHBlock, so need to put stores in each predecessor.
3569 for (BasicBlock *PredBlock : predecessors(EHBlock)) {
3570 insertPHIStore(PredBlock, InVal, SpillSlot, Worklist);
3571 }
3572 }
3573 }
3574}
3575
3576void WinEHPrepare::insertPHIStore(
3577 BasicBlock *PredBlock, Value *PredVal, AllocaInst *SpillSlot,
3578 SmallVectorImpl<std::pair<BasicBlock *, Value *>> &Worklist) {
3579
3580 if (PredBlock->isEHPad() &&
3581 !isa<CleanupPadInst>(PredBlock->getFirstNonPHI())) {
3582 // Pred is unsplittable, so we need to queue it on the worklist.
3583 Worklist.push_back({PredBlock, PredVal});
3584 return;
3585 }
3586
3587 // Otherwise, insert the store at the end of the basic block.
3588 new StoreInst(PredVal, SpillSlot, PredBlock->getTerminator());
3589}
3590
3591// TODO: Share loads for same-funclet uses (requires dominators if funclets
3592// aren't properly nested).
3593void WinEHPrepare::demoteNonlocalUses(Value *V,
3594 std::set<BasicBlock *> &ColorsForBB,
3595 Function &F) {
David Majnemer83f4bb22015-08-17 20:56:39 +00003596 // Tokens can only be used non-locally due to control flow involving
3597 // unreachable edges. Don't try to demote the token usage, we'll simply
3598 // delete the cloned user later.
3599 if (isa<CatchPadInst>(V) || isa<CleanupPadInst>(V))
3600 return;
3601
Joseph Tremouletc9ff9142015-08-13 14:30:10 +00003602 DenseMap<BasicBlock *, Value *> Loads;
3603 AllocaInst *SpillSlot = nullptr;
3604 for (Value::use_iterator UI = V->use_begin(), UE = V->use_end(); UI != UE;) {
3605 Use &U = *UI++;
3606 auto *UsingInst = cast<Instruction>(U.getUser());
3607 BasicBlock *UsingBB = UsingInst->getParent();
3608
Joseph Tremouletec182852015-08-28 01:12:35 +00003609 // Is the Use inside a block which is colored the same as the Def?
Joseph Tremouletc9ff9142015-08-13 14:30:10 +00003610 // If so, we don't need to escape the Def because we will clone
3611 // ourselves our own private copy.
3612 std::set<BasicBlock *> &ColorsForUsingBB = BlockColors[UsingBB];
Joseph Tremouletec182852015-08-28 01:12:35 +00003613 if (ColorsForUsingBB == ColorsForBB)
Joseph Tremouletc9ff9142015-08-13 14:30:10 +00003614 continue;
3615
3616 replaceUseWithLoad(V, U, SpillSlot, Loads, F);
3617 }
3618 if (SpillSlot) {
3619 // Insert stores of the computed value into the stack slot.
3620 // We have to be careful if I is an invoke instruction,
3621 // because we can't insert the store AFTER the terminator instruction.
3622 BasicBlock::iterator InsertPt;
3623 if (isa<Argument>(V)) {
3624 InsertPt = F.getEntryBlock().getTerminator();
3625 } else if (isa<TerminatorInst>(V)) {
3626 auto *II = cast<InvokeInst>(V);
3627 // We cannot demote invoke instructions to the stack if their normal
3628 // edge is critical. Therefore, split the critical edge and create a
3629 // basic block into which the store can be inserted.
3630 if (!II->getNormalDest()->getSinglePredecessor()) {
3631 unsigned SuccNum =
3632 GetSuccessorNumber(II->getParent(), II->getNormalDest());
3633 assert(isCriticalEdge(II, SuccNum) && "Expected a critical edge!");
3634 BasicBlock *NewBlock = SplitCriticalEdge(II, SuccNum);
3635 assert(NewBlock && "Unable to split critical edge.");
3636 // Update the color mapping for the newly split edge.
3637 std::set<BasicBlock *> &ColorsForUsingBB = BlockColors[II->getParent()];
3638 BlockColors[NewBlock] = ColorsForUsingBB;
3639 for (BasicBlock *FuncletPad : ColorsForUsingBB)
3640 FuncletBlocks[FuncletPad].insert(NewBlock);
3641 }
3642 InsertPt = II->getNormalDest()->getFirstInsertionPt();
3643 } else {
3644 InsertPt = cast<Instruction>(V);
3645 ++InsertPt;
3646 // Don't insert before PHI nodes or EH pad instrs.
3647 for (; isa<PHINode>(InsertPt) || InsertPt->isEHPad(); ++InsertPt)
3648 ;
3649 }
3650 new StoreInst(V, SpillSlot, InsertPt);
3651 }
3652}
3653
3654void WinEHPrepare::replaceUseWithLoad(Value *V, Use &U, AllocaInst *&SpillSlot,
3655 DenseMap<BasicBlock *, Value *> &Loads,
3656 Function &F) {
3657 // Lazilly create the spill slot.
3658 if (!SpillSlot)
3659 SpillSlot = new AllocaInst(V->getType(), nullptr,
3660 Twine(V->getName(), ".wineh.spillslot"),
3661 F.getEntryBlock().begin());
3662
3663 auto *UsingInst = cast<Instruction>(U.getUser());
3664 if (auto *UsingPHI = dyn_cast<PHINode>(UsingInst)) {
3665 // If this is a PHI node, we can't insert a load of the value before
3666 // the use. Instead insert the load in the predecessor block
3667 // corresponding to the incoming value.
3668 //
3669 // Note that if there are multiple edges from a basic block to this
3670 // PHI node that we cannot have multiple loads. The problem is that
3671 // the resulting PHI node will have multiple values (from each load)
3672 // coming in from the same block, which is illegal SSA form.
3673 // For this reason, we keep track of and reuse loads we insert.
3674 BasicBlock *IncomingBlock = UsingPHI->getIncomingBlock(U);
Joseph Tremoulet7031c9f2015-08-17 13:51:37 +00003675 if (auto *CatchRet =
3676 dyn_cast<CatchReturnInst>(IncomingBlock->getTerminator())) {
3677 // Putting a load above a catchret and use on the phi would still leave
3678 // a cross-funclet def/use. We need to split the edge, change the
3679 // catchret to target the new block, and put the load there.
3680 BasicBlock *PHIBlock = UsingInst->getParent();
3681 BasicBlock *NewBlock = SplitEdge(IncomingBlock, PHIBlock);
3682 // SplitEdge gives us:
3683 // IncomingBlock:
3684 // ...
3685 // br label %NewBlock
3686 // NewBlock:
3687 // catchret label %PHIBlock
3688 // But we need:
3689 // IncomingBlock:
3690 // ...
3691 // catchret label %NewBlock
3692 // NewBlock:
3693 // br label %PHIBlock
3694 // So move the terminators to each others' blocks and swap their
3695 // successors.
3696 BranchInst *Goto = cast<BranchInst>(IncomingBlock->getTerminator());
3697 Goto->removeFromParent();
3698 CatchRet->removeFromParent();
3699 IncomingBlock->getInstList().push_back(CatchRet);
3700 NewBlock->getInstList().push_back(Goto);
3701 Goto->setSuccessor(0, PHIBlock);
3702 CatchRet->setSuccessor(NewBlock);
3703 // Update the color mapping for the newly split edge.
3704 std::set<BasicBlock *> &ColorsForPHIBlock = BlockColors[PHIBlock];
3705 BlockColors[NewBlock] = ColorsForPHIBlock;
3706 for (BasicBlock *FuncletPad : ColorsForPHIBlock)
3707 FuncletBlocks[FuncletPad].insert(NewBlock);
3708 // Treat the new block as incoming for load insertion.
3709 IncomingBlock = NewBlock;
3710 }
Joseph Tremouletc9ff9142015-08-13 14:30:10 +00003711 Value *&Load = Loads[IncomingBlock];
3712 // Insert the load into the predecessor block
3713 if (!Load)
3714 Load = new LoadInst(SpillSlot, Twine(V->getName(), ".wineh.reload"),
3715 /*Volatile=*/false, IncomingBlock->getTerminator());
3716
3717 U.set(Load);
3718 } else {
3719 // Reload right before the old use.
3720 auto *Load = new LoadInst(SpillSlot, Twine(V->getName(), ".wineh.reload"),
3721 /*Volatile=*/false, UsingInst);
3722 U.set(Load);
3723 }
3724}