blob: 7240159b083ef5d6340ea516764e8b9e6543aa74 [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"
Reid Klecknerc71d6272015-09-28 23:56:30 +000037#include "llvm/MC/MCSymbol.h"
Andrew Kaylor1476e6d2015-02-24 20:49:35 +000038#include "llvm/Pass.h"
Andrew Kaylor6b67d422015-03-11 23:22:06 +000039#include "llvm/Support/Debug.h"
Benjamin Kramera8d61b12015-03-23 18:57:17 +000040#include "llvm/Support/raw_ostream.h"
Andrew Kaylor6b67d422015-03-11 23:22:06 +000041#include "llvm/Transforms/Utils/BasicBlockUtils.h"
Andrew Kaylor1476e6d2015-02-24 20:49:35 +000042#include "llvm/Transforms/Utils/Cloning.h"
43#include "llvm/Transforms/Utils/Local.h"
Andrew Kaylor64622aa2015-04-01 17:21:25 +000044#include "llvm/Transforms/Utils/PromoteMemToReg.h"
David Majnemer459a64a2015-09-16 18:40:37 +000045#include "llvm/Transforms/Utils/SSAUpdater.h"
Andrew Kaylor1476e6d2015-02-24 20:49:35 +000046#include <memory>
47
48using namespace llvm;
49using namespace llvm::PatternMatch;
50
51#define DEBUG_TYPE "winehprepare"
52
David Majnemer459a64a2015-09-16 18:40:37 +000053static cl::opt<bool> DisableDemotion(
54 "disable-demotion", cl::Hidden,
55 cl::desc(
56 "Clone multicolor basic blocks but do not demote cross funclet values"),
57 cl::init(false));
58
59static cl::opt<bool> DisableCleanups(
60 "disable-cleanups", cl::Hidden,
61 cl::desc("Do not remove implausible terminators or other similar cleanups"),
62 cl::init(false));
63
Andrew Kaylor1476e6d2015-02-24 20:49:35 +000064namespace {
65
Andrew Kaylor1476e6d2015-02-24 20:49:35 +000066// This map is used to model frame variable usage during outlining, to
67// construct a structure type to hold the frame variables in a frame
68// allocation block, and to remap the frame variable allocas (including
69// spill locations as needed) to GEPs that get the variable from the
70// frame allocation structure.
Reid Klecknercfb9ce52015-03-05 18:26:34 +000071typedef MapVector<Value *, TinyPtrVector<AllocaInst *>> FrameVarInfoMap;
Andrew Kaylor1476e6d2015-02-24 20:49:35 +000072
Reid Kleckner3567d272015-04-02 21:13:31 +000073// TinyPtrVector cannot hold nullptr, so we need our own sentinel that isn't
74// quite null.
75AllocaInst *getCatchObjectSentinel() {
76 return static_cast<AllocaInst *>(nullptr) + 1;
77}
78
Andrew Kaylor6b67d422015-03-11 23:22:06 +000079typedef SmallSet<BasicBlock *, 4> VisitedBlockSet;
80
Andrew Kaylor6b67d422015-03-11 23:22:06 +000081class LandingPadActions;
Andrew Kaylor6b67d422015-03-11 23:22:06 +000082class LandingPadMap;
83
84typedef DenseMap<const BasicBlock *, CatchHandler *> CatchHandlerMapTy;
85typedef DenseMap<const BasicBlock *, CleanupHandler *> CleanupHandlerMapTy;
86
Andrew Kaylor1476e6d2015-02-24 20:49:35 +000087class WinEHPrepare : public FunctionPass {
Andrew Kaylor1476e6d2015-02-24 20:49:35 +000088public:
89 static char ID; // Pass identification, replacement for typeid.
90 WinEHPrepare(const TargetMachine *TM = nullptr)
Reid Kleckner582786b2015-04-30 18:17:12 +000091 : FunctionPass(ID) {
Reid Klecknerbcda1cd2015-04-29 22:49:54 +000092 if (TM)
Daniel Sanders110bf6d2015-06-24 13:25:57 +000093 TheTriple = TM->getTargetTriple();
Reid Klecknerbcda1cd2015-04-29 22:49:54 +000094 }
Andrew Kaylor1476e6d2015-02-24 20:49:35 +000095
96 bool runOnFunction(Function &Fn) override;
97
98 bool doFinalization(Module &M) override;
99
100 void getAnalysisUsage(AnalysisUsage &AU) const override;
101
102 const char *getPassName() const override {
103 return "Windows exception handling preparation";
104 }
105
106private:
Reid Kleckner0f9e27a2015-03-18 20:26:53 +0000107 bool prepareExceptionHandlers(Function &F,
108 SmallVectorImpl<LandingPadInst *> &LPads);
Andrew Kaylora6c5b962015-05-20 23:22:24 +0000109 void identifyEHBlocks(Function &F, SmallVectorImpl<LandingPadInst *> &LPads);
Andrew Kaylor64622aa2015-04-01 17:21:25 +0000110 void promoteLandingPadValues(LandingPadInst *LPad);
Reid Klecknerfd7df282015-04-22 21:05:21 +0000111 void demoteValuesLiveAcrossHandlers(Function &F,
112 SmallVectorImpl<LandingPadInst *> &LPads);
Andrew Kaylor046f7b42015-04-28 21:54:14 +0000113 void findSEHEHReturnPoints(Function &F,
114 SetVector<BasicBlock *> &EHReturnBlocks);
115 void findCXXEHReturnPoints(Function &F,
116 SetVector<BasicBlock *> &EHReturnBlocks);
Andrew Kaylorcc14f382015-05-11 23:06:02 +0000117 void getPossibleReturnTargets(Function *ParentF, Function *HandlerF,
118 SetVector<BasicBlock*> &Targets);
Andrew Kayloraa92ab02015-04-03 19:37:50 +0000119 void completeNestedLandingPad(Function *ParentFn,
120 LandingPadInst *OutlinedLPad,
121 const LandingPadInst *OriginalLPad,
122 FrameVarInfoMap &VarInfo);
Reid Kleckner6511c8b2015-07-01 20:59:25 +0000123 Function *createHandlerFunc(Function *ParentFn, Type *RetTy,
124 const Twine &Name, Module *M, Value *&ParentFP);
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000125 bool outlineHandler(ActionHandler *Action, Function *SrcFn,
126 LandingPadInst *LPad, BasicBlock *StartBB,
Reid Klecknercfb9ce52015-03-05 18:26:34 +0000127 FrameVarInfoMap &VarInfo);
David Majnemer7fddecc2015-06-17 20:52:32 +0000128 void addStubInvokeToHandlerIfNeeded(Function *Handler);
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000129
130 void mapLandingPadBlocks(LandingPadInst *LPad, LandingPadActions &Actions);
131 CatchHandler *findCatchHandler(BasicBlock *BB, BasicBlock *&NextBB,
132 VisitedBlockSet &VisitedBlocks);
Reid Kleckner9405ef02015-04-10 23:12:29 +0000133 void findCleanupHandlers(LandingPadActions &Actions, BasicBlock *StartBB,
134 BasicBlock *EndBB);
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000135
Reid Kleckner0f9e27a2015-03-18 20:26:53 +0000136 void processSEHCatchHandler(CatchHandler *Handler, BasicBlock *StartBB);
Joseph Tremouletc9ff9142015-08-13 14:30:10 +0000137 void insertPHIStores(PHINode *OriginalPHI, AllocaInst *SpillSlot);
138 void
139 insertPHIStore(BasicBlock *PredBlock, Value *PredVal, AllocaInst *SpillSlot,
140 SmallVectorImpl<std::pair<BasicBlock *, Value *>> &Worklist);
141 AllocaInst *insertPHILoads(PHINode *PN, Function &F);
142 void replaceUseWithLoad(Value *V, Use &U, AllocaInst *&SpillSlot,
143 DenseMap<BasicBlock *, Value *> &Loads, Function &F);
144 void demoteNonlocalUses(Value *V, std::set<BasicBlock *> &ColorsForBB,
145 Function &F);
Joseph Tremouletec182852015-08-28 01:12:35 +0000146 bool prepareExplicitEH(Function &F,
147 SmallVectorImpl<BasicBlock *> &EntryBlocks);
David Majnemer67bff0d2015-09-16 20:42:16 +0000148 void replaceTerminatePadWithCleanup(Function &F);
Joseph Tremouletec182852015-08-28 01:12:35 +0000149 void colorFunclets(Function &F, SmallVectorImpl<BasicBlock *> &EntryBlocks);
David Majnemerb3d9b962015-09-16 18:40:24 +0000150 void demotePHIsOnFunclets(Function &F);
151 void demoteUsesBetweenFunclets(Function &F);
152 void demoteArgumentUses(Function &F);
153 void cloneCommonBlocks(Function &F,
154 SmallVectorImpl<BasicBlock *> &EntryBlocks);
155 void removeImplausibleTerminators(Function &F);
156 void cleanupPreparedFunclets(Function &F);
157 void verifyPreparedFunclets(Function &F);
David Majnemerfd9f4772015-08-11 01:15:26 +0000158
Reid Klecknerbcda1cd2015-04-29 22:49:54 +0000159 Triple TheTriple;
160
Reid Kleckner0f9e27a2015-03-18 20:26:53 +0000161 // All fields are reset by runOnFunction.
Reid Kleckner582786b2015-04-30 18:17:12 +0000162 DominatorTree *DT = nullptr;
Reid Klecknerf12c0302015-06-09 21:42:19 +0000163 const TargetLibraryInfo *LibInfo = nullptr;
Reid Kleckner582786b2015-04-30 18:17:12 +0000164 EHPersonality Personality = EHPersonality::Unknown;
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000165 CatchHandlerMapTy CatchHandlerMap;
166 CleanupHandlerMapTy CleanupHandlerMap;
Andrew Kaylor64622aa2015-04-01 17:21:25 +0000167 DenseMap<const LandingPadInst *, LandingPadMap> LPadMaps;
Andrew Kaylora6c5b962015-05-20 23:22:24 +0000168 SmallPtrSet<BasicBlock *, 4> NormalBlocks;
169 SmallPtrSet<BasicBlock *, 4> EHBlocks;
170 SetVector<BasicBlock *> EHReturnBlocks;
Andrew Kayloraa92ab02015-04-03 19:37:50 +0000171
172 // This maps landing pad instructions found in outlined handlers to
173 // the landing pad instruction in the parent function from which they
174 // were cloned. The cloned/nested landing pad is used as the key
175 // because the landing pad may be cloned into multiple handlers.
176 // This map will be used to add the llvm.eh.actions call to the nested
177 // landing pads after all handlers have been outlined.
178 DenseMap<LandingPadInst *, const LandingPadInst *> NestedLPtoOriginalLP;
179
180 // This maps blocks in the parent function which are destinations of
181 // catch handlers to cloned blocks in (other) outlined handlers. This
182 // handles the case where a nested landing pads has a catch handler that
183 // returns to a handler function rather than the parent function.
184 // The original block is used as the key here because there should only
185 // ever be one handler function from which the cloned block is not pruned.
186 // The original block will be pruned from the parent function after all
187 // handlers have been outlined. This map will be used to adjust the
188 // return instructions of handlers which return to the block that was
189 // outlined into a handler. This is done after all handlers have been
190 // outlined but before the outlined code is pruned from the parent function.
191 DenseMap<const BasicBlock *, BasicBlock *> LPadTargetBlocks;
Reid Klecknercfbfe6f2015-04-24 20:25:05 +0000192
Reid Klecknerd5afc62f2015-07-07 23:23:03 +0000193 // Map from outlined handler to call to parent local address. Only used for
Reid Klecknerbcda1cd2015-04-29 22:49:54 +0000194 // 32-bit EH.
195 DenseMap<Function *, Value *> HandlerToParentFP;
196
Reid Kleckner582786b2015-04-30 18:17:12 +0000197 AllocaInst *SEHExceptionCodeSlot = nullptr;
David Majnemerfd9f4772015-08-11 01:15:26 +0000198
199 std::map<BasicBlock *, std::set<BasicBlock *>> BlockColors;
200 std::map<BasicBlock *, std::set<BasicBlock *>> FuncletBlocks;
Joseph Tremouletec182852015-08-28 01:12:35 +0000201 std::map<BasicBlock *, std::set<BasicBlock *>> FuncletChildren;
Andrew Kaylor1476e6d2015-02-24 20:49:35 +0000202};
203
204class WinEHFrameVariableMaterializer : public ValueMaterializer {
205public:
Reid Klecknerbcda1cd2015-04-29 22:49:54 +0000206 WinEHFrameVariableMaterializer(Function *OutlinedFn, Value *ParentFP,
Andrew Kaylor1476e6d2015-02-24 20:49:35 +0000207 FrameVarInfoMap &FrameVarInfo);
Alexander Kornienkof817c1c2015-04-11 02:11:45 +0000208 ~WinEHFrameVariableMaterializer() override {}
Andrew Kaylor1476e6d2015-02-24 20:49:35 +0000209
Alexander Kornienkof817c1c2015-04-11 02:11:45 +0000210 Value *materializeValueFor(Value *V) override;
Andrew Kaylor1476e6d2015-02-24 20:49:35 +0000211
Reid Kleckner3567d272015-04-02 21:13:31 +0000212 void escapeCatchObject(Value *V);
213
Andrew Kaylor1476e6d2015-02-24 20:49:35 +0000214private:
215 FrameVarInfoMap &FrameVarInfo;
216 IRBuilder<> Builder;
217};
218
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000219class LandingPadMap {
220public:
221 LandingPadMap() : OriginLPad(nullptr) {}
222 void mapLandingPad(const LandingPadInst *LPad);
223
224 bool isInitialized() { return OriginLPad != nullptr; }
225
Andrew Kaylorf7118ae2015-03-27 22:31:12 +0000226 bool isOriginLandingPadBlock(const BasicBlock *BB) const;
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000227 bool isLandingPadSpecificInst(const Instruction *Inst) const;
228
Andrew Kaylor64622aa2015-04-01 17:21:25 +0000229 void remapEHValues(ValueToValueMapTy &VMap, Value *EHPtrValue,
230 Value *SelectorValue) const;
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000231
232private:
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000233 const LandingPadInst *OriginLPad;
234 // We will normally only see one of each of these instructions, but
235 // if more than one occurs for some reason we can handle that.
236 TinyPtrVector<const ExtractValueInst *> ExtractedEHPtrs;
237 TinyPtrVector<const ExtractValueInst *> ExtractedSelectors;
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000238};
239
Andrew Kaylorf0f5e462015-03-03 20:00:16 +0000240class WinEHCloningDirectorBase : public CloningDirector {
Andrew Kaylor1476e6d2015-02-24 20:49:35 +0000241public:
Reid Klecknerbcda1cd2015-04-29 22:49:54 +0000242 WinEHCloningDirectorBase(Function *HandlerFn, Value *ParentFP,
243 FrameVarInfoMap &VarInfo, LandingPadMap &LPadMap)
244 : Materializer(HandlerFn, ParentFP, VarInfo),
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000245 SelectorIDType(Type::getInt32Ty(HandlerFn->getContext())),
246 Int8PtrType(Type::getInt8PtrTy(HandlerFn->getContext())),
Reid Klecknerbcda1cd2015-04-29 22:49:54 +0000247 LPadMap(LPadMap), ParentFP(ParentFP) {}
Andrew Kaylor1476e6d2015-02-24 20:49:35 +0000248
249 CloningAction handleInstruction(ValueToValueMapTy &VMap,
250 const Instruction *Inst,
251 BasicBlock *NewBB) override;
252
Andrew Kaylorf0f5e462015-03-03 20:00:16 +0000253 virtual CloningAction handleBeginCatch(ValueToValueMapTy &VMap,
254 const Instruction *Inst,
255 BasicBlock *NewBB) = 0;
256 virtual CloningAction handleEndCatch(ValueToValueMapTy &VMap,
257 const Instruction *Inst,
258 BasicBlock *NewBB) = 0;
259 virtual CloningAction handleTypeIdFor(ValueToValueMapTy &VMap,
260 const Instruction *Inst,
261 BasicBlock *NewBB) = 0;
Andrew Kaylora6c5b962015-05-20 23:22:24 +0000262 virtual CloningAction handleIndirectBr(ValueToValueMapTy &VMap,
263 const IndirectBrInst *IBr,
264 BasicBlock *NewBB) = 0;
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000265 virtual CloningAction handleInvoke(ValueToValueMapTy &VMap,
266 const InvokeInst *Invoke,
267 BasicBlock *NewBB) = 0;
Andrew Kaylorf0f5e462015-03-03 20:00:16 +0000268 virtual CloningAction handleResume(ValueToValueMapTy &VMap,
269 const ResumeInst *Resume,
270 BasicBlock *NewBB) = 0;
Andrew Kaylorea8df612015-04-17 23:05:43 +0000271 virtual CloningAction handleCompare(ValueToValueMapTy &VMap,
272 const CmpInst *Compare,
273 BasicBlock *NewBB) = 0;
Andrew Kayloraa92ab02015-04-03 19:37:50 +0000274 virtual CloningAction handleLandingPad(ValueToValueMapTy &VMap,
275 const LandingPadInst *LPad,
276 BasicBlock *NewBB) = 0;
Andrew Kaylorf0f5e462015-03-03 20:00:16 +0000277
Andrew Kaylor1476e6d2015-02-24 20:49:35 +0000278 ValueMaterializer *getValueMaterializer() override { return &Materializer; }
279
Andrew Kaylorf0f5e462015-03-03 20:00:16 +0000280protected:
Andrew Kaylor1476e6d2015-02-24 20:49:35 +0000281 WinEHFrameVariableMaterializer Materializer;
282 Type *SelectorIDType;
283 Type *Int8PtrType;
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000284 LandingPadMap &LPadMap;
Reid Klecknerf14787d2015-04-22 00:07:52 +0000285
286 /// The value representing the parent frame pointer.
Reid Klecknerbcda1cd2015-04-29 22:49:54 +0000287 Value *ParentFP;
Andrew Kaylor1476e6d2015-02-24 20:49:35 +0000288};
Andrew Kaylorf0f5e462015-03-03 20:00:16 +0000289
290class WinEHCatchDirector : public WinEHCloningDirectorBase {
291public:
Andrew Kayloraa92ab02015-04-03 19:37:50 +0000292 WinEHCatchDirector(
Reid Klecknerbcda1cd2015-04-29 22:49:54 +0000293 Function *CatchFn, Value *ParentFP, Value *Selector,
294 FrameVarInfoMap &VarInfo, LandingPadMap &LPadMap,
Andrew Kaylora6c5b962015-05-20 23:22:24 +0000295 DenseMap<LandingPadInst *, const LandingPadInst *> &NestedLPads,
296 DominatorTree *DT, SmallPtrSetImpl<BasicBlock *> &EHBlocks)
Reid Klecknerbcda1cd2015-04-29 22:49:54 +0000297 : WinEHCloningDirectorBase(CatchFn, ParentFP, VarInfo, LPadMap),
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000298 CurrentSelector(Selector->stripPointerCasts()),
Andrew Kaylora6c5b962015-05-20 23:22:24 +0000299 ExceptionObjectVar(nullptr), NestedLPtoOriginalLP(NestedLPads),
300 DT(DT), EHBlocks(EHBlocks) {}
Andrew Kaylorf0f5e462015-03-03 20:00:16 +0000301
302 CloningAction handleBeginCatch(ValueToValueMapTy &VMap,
303 const Instruction *Inst,
304 BasicBlock *NewBB) override;
305 CloningAction handleEndCatch(ValueToValueMapTy &VMap, const Instruction *Inst,
306 BasicBlock *NewBB) override;
307 CloningAction handleTypeIdFor(ValueToValueMapTy &VMap,
308 const Instruction *Inst,
309 BasicBlock *NewBB) override;
Andrew Kaylora6c5b962015-05-20 23:22:24 +0000310 CloningAction handleIndirectBr(ValueToValueMapTy &VMap,
311 const IndirectBrInst *IBr,
312 BasicBlock *NewBB) override;
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000313 CloningAction handleInvoke(ValueToValueMapTy &VMap, const InvokeInst *Invoke,
314 BasicBlock *NewBB) override;
Andrew Kaylorf0f5e462015-03-03 20:00:16 +0000315 CloningAction handleResume(ValueToValueMapTy &VMap, const ResumeInst *Resume,
316 BasicBlock *NewBB) override;
Andrew Kaylor91307432015-04-28 22:01:51 +0000317 CloningAction handleCompare(ValueToValueMapTy &VMap, const CmpInst *Compare,
318 BasicBlock *NewBB) override;
Andrew Kayloraa92ab02015-04-03 19:37:50 +0000319 CloningAction handleLandingPad(ValueToValueMapTy &VMap,
320 const LandingPadInst *LPad,
321 BasicBlock *NewBB) override;
Andrew Kaylorf0f5e462015-03-03 20:00:16 +0000322
Reid Kleckner3567d272015-04-02 21:13:31 +0000323 Value *getExceptionVar() { return ExceptionObjectVar; }
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000324 TinyPtrVector<BasicBlock *> &getReturnTargets() { return ReturnTargets; }
325
Andrew Kaylorf0f5e462015-03-03 20:00:16 +0000326private:
327 Value *CurrentSelector;
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000328
Reid Kleckner3567d272015-04-02 21:13:31 +0000329 Value *ExceptionObjectVar;
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000330 TinyPtrVector<BasicBlock *> ReturnTargets;
Andrew Kayloraa92ab02015-04-03 19:37:50 +0000331
332 // This will be a reference to the field of the same name in the WinEHPrepare
333 // object which instantiates this WinEHCatchDirector object.
334 DenseMap<LandingPadInst *, const LandingPadInst *> &NestedLPtoOriginalLP;
Andrew Kaylora6c5b962015-05-20 23:22:24 +0000335 DominatorTree *DT;
336 SmallPtrSetImpl<BasicBlock *> &EHBlocks;
Andrew Kaylorf0f5e462015-03-03 20:00:16 +0000337};
338
339class WinEHCleanupDirector : public WinEHCloningDirectorBase {
340public:
Reid Klecknerbcda1cd2015-04-29 22:49:54 +0000341 WinEHCleanupDirector(Function *CleanupFn, Value *ParentFP,
342 FrameVarInfoMap &VarInfo, LandingPadMap &LPadMap)
343 : WinEHCloningDirectorBase(CleanupFn, ParentFP, VarInfo,
344 LPadMap) {}
Andrew Kaylorf0f5e462015-03-03 20:00:16 +0000345
346 CloningAction handleBeginCatch(ValueToValueMapTy &VMap,
347 const Instruction *Inst,
348 BasicBlock *NewBB) override;
349 CloningAction handleEndCatch(ValueToValueMapTy &VMap, const Instruction *Inst,
350 BasicBlock *NewBB) override;
351 CloningAction handleTypeIdFor(ValueToValueMapTy &VMap,
352 const Instruction *Inst,
353 BasicBlock *NewBB) override;
Andrew Kaylora6c5b962015-05-20 23:22:24 +0000354 CloningAction handleIndirectBr(ValueToValueMapTy &VMap,
355 const IndirectBrInst *IBr,
356 BasicBlock *NewBB) override;
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000357 CloningAction handleInvoke(ValueToValueMapTy &VMap, const InvokeInst *Invoke,
358 BasicBlock *NewBB) override;
Andrew Kaylorf0f5e462015-03-03 20:00:16 +0000359 CloningAction handleResume(ValueToValueMapTy &VMap, const ResumeInst *Resume,
360 BasicBlock *NewBB) override;
Andrew Kaylor91307432015-04-28 22:01:51 +0000361 CloningAction handleCompare(ValueToValueMapTy &VMap, const CmpInst *Compare,
362 BasicBlock *NewBB) override;
Andrew Kayloraa92ab02015-04-03 19:37:50 +0000363 CloningAction handleLandingPad(ValueToValueMapTy &VMap,
364 const LandingPadInst *LPad,
365 BasicBlock *NewBB) override;
Andrew Kaylorf0f5e462015-03-03 20:00:16 +0000366};
367
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000368class LandingPadActions {
369public:
370 LandingPadActions() : HasCleanupHandlers(false) {}
371
372 void insertCatchHandler(CatchHandler *Action) { Actions.push_back(Action); }
373 void insertCleanupHandler(CleanupHandler *Action) {
374 Actions.push_back(Action);
375 HasCleanupHandlers = true;
376 }
377
378 bool includesCleanup() const { return HasCleanupHandlers; }
379
David Majnemercde33032015-03-30 22:58:10 +0000380 SmallVectorImpl<ActionHandler *> &actions() { return Actions; }
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000381 SmallVectorImpl<ActionHandler *>::iterator begin() { return Actions.begin(); }
382 SmallVectorImpl<ActionHandler *>::iterator end() { return Actions.end(); }
383
384private:
385 // Note that this class does not own the ActionHandler objects in this vector.
386 // The ActionHandlers are owned by the CatchHandlerMap and CleanupHandlerMap
387 // in the WinEHPrepare class.
388 SmallVector<ActionHandler *, 4> Actions;
389 bool HasCleanupHandlers;
390};
391
Andrew Kaylor1476e6d2015-02-24 20:49:35 +0000392} // end anonymous namespace
393
394char WinEHPrepare::ID = 0;
Reid Kleckner47c8e7a2015-03-12 00:36:20 +0000395INITIALIZE_TM_PASS(WinEHPrepare, "winehprepare", "Prepare Windows exceptions",
396 false, false)
Andrew Kaylor1476e6d2015-02-24 20:49:35 +0000397
398FunctionPass *llvm::createWinEHPass(const TargetMachine *TM) {
399 return new WinEHPrepare(TM);
400}
401
Andrew Kaylor1476e6d2015-02-24 20:49:35 +0000402bool WinEHPrepare::runOnFunction(Function &Fn) {
David Majnemerfd9f4772015-08-11 01:15:26 +0000403 if (!Fn.hasPersonalityFn())
Andrew Kaylor1476e6d2015-02-24 20:49:35 +0000404 return false;
405
David Majnemerfd9f4772015-08-11 01:15:26 +0000406 // No need to prepare outlined handlers.
407 if (Fn.hasFnAttribute("wineh-parent"))
David Majnemer09e1fdb2015-08-06 21:13:51 +0000408 return false;
409
Andrew Kaylor1476e6d2015-02-24 20:49:35 +0000410 // Classify the personality to see what kind of preparation we need.
David Majnemer7fddecc2015-06-17 20:52:32 +0000411 Personality = classifyEHPersonality(Fn.getPersonalityFn());
Andrew Kaylor1476e6d2015-02-24 20:49:35 +0000412
Reid Kleckner47c8e7a2015-03-12 00:36:20 +0000413 // Do nothing if this is not an MSVC personality.
Reid Kleckner0f9e27a2015-03-18 20:26:53 +0000414 if (!isMSVCEHPersonality(Personality))
Reid Kleckner47c8e7a2015-03-12 00:36:20 +0000415 return false;
Andrew Kaylor1476e6d2015-02-24 20:49:35 +0000416
David Majnemerfd9f4772015-08-11 01:15:26 +0000417 SmallVector<LandingPadInst *, 4> LPads;
418 SmallVector<ResumeInst *, 4> Resumes;
Joseph Tremouletec182852015-08-28 01:12:35 +0000419 SmallVector<BasicBlock *, 4> EntryBlocks;
David Majnemerfd9f4772015-08-11 01:15:26 +0000420 bool ForExplicitEH = false;
421 for (BasicBlock &BB : Fn) {
Joseph Tremouletec182852015-08-28 01:12:35 +0000422 Instruction *First = BB.getFirstNonPHI();
423 if (auto *LP = dyn_cast<LandingPadInst>(First)) {
David Majnemerfd9f4772015-08-11 01:15:26 +0000424 LPads.push_back(LP);
Joseph Tremouletec182852015-08-28 01:12:35 +0000425 } else if (First->isEHPad()) {
426 if (!ForExplicitEH)
427 EntryBlocks.push_back(&Fn.getEntryBlock());
Joseph Tremoulet9ce71f72015-09-03 09:09:43 +0000428 if (!isa<CatchEndPadInst>(First) && !isa<CleanupEndPadInst>(First))
Joseph Tremouletec182852015-08-28 01:12:35 +0000429 EntryBlocks.push_back(&BB);
David Majnemerfd9f4772015-08-11 01:15:26 +0000430 ForExplicitEH = true;
David Majnemerfd9f4772015-08-11 01:15:26 +0000431 }
432 if (auto *Resume = dyn_cast<ResumeInst>(BB.getTerminator()))
433 Resumes.push_back(Resume);
434 }
435
436 if (ForExplicitEH)
Joseph Tremouletec182852015-08-28 01:12:35 +0000437 return prepareExplicitEH(Fn, EntryBlocks);
David Majnemerfd9f4772015-08-11 01:15:26 +0000438
439 // No need to prepare functions that lack landing pads.
440 if (LPads.empty())
441 return false;
442
Andrew Kaylor64622aa2015-04-01 17:21:25 +0000443 DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
Reid Klecknerf12c0302015-06-09 21:42:19 +0000444 LibInfo = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
Andrew Kaylor64622aa2015-04-01 17:21:25 +0000445
Reid Kleckner0f9e27a2015-03-18 20:26:53 +0000446 // If there were any landing pads, prepareExceptionHandlers will make changes.
447 prepareExceptionHandlers(Fn, LPads);
Andrew Kaylor1476e6d2015-02-24 20:49:35 +0000448 return true;
449}
450
Andrew Kayloraa92ab02015-04-03 19:37:50 +0000451bool WinEHPrepare::doFinalization(Module &M) { return false; }
Andrew Kaylor1476e6d2015-02-24 20:49:35 +0000452
Andrew Kaylor64622aa2015-04-01 17:21:25 +0000453void WinEHPrepare::getAnalysisUsage(AnalysisUsage &AU) const {
454 AU.addRequired<DominatorTreeWrapperPass>();
Reid Klecknerf12c0302015-06-09 21:42:19 +0000455 AU.addRequired<TargetLibraryInfoWrapperPass>();
Andrew Kaylor64622aa2015-04-01 17:21:25 +0000456}
Andrew Kaylor1476e6d2015-02-24 20:49:35 +0000457
Reid Klecknerfd7df282015-04-22 21:05:21 +0000458static bool isSelectorDispatch(BasicBlock *BB, BasicBlock *&CatchHandler,
459 Constant *&Selector, BasicBlock *&NextBB);
460
461// Finds blocks reachable from the starting set Worklist. Does not follow unwind
462// edges or blocks listed in StopPoints.
463static void findReachableBlocks(SmallPtrSetImpl<BasicBlock *> &ReachableBBs,
464 SetVector<BasicBlock *> &Worklist,
465 const SetVector<BasicBlock *> *StopPoints) {
466 while (!Worklist.empty()) {
467 BasicBlock *BB = Worklist.pop_back_val();
468
469 // Don't cross blocks that we should stop at.
470 if (StopPoints && StopPoints->count(BB))
471 continue;
472
473 if (!ReachableBBs.insert(BB).second)
474 continue; // Already visited.
475
476 // Don't follow unwind edges of invokes.
477 if (auto *II = dyn_cast<InvokeInst>(BB->getTerminator())) {
478 Worklist.insert(II->getNormalDest());
479 continue;
480 }
481
482 // Otherwise, follow all successors.
483 Worklist.insert(succ_begin(BB), succ_end(BB));
484 }
485}
486
Andrew Kaylor046f7b42015-04-28 21:54:14 +0000487// Attempt to find an instruction where a block can be split before
488// a call to llvm.eh.begincatch and its operands. If the block
489// begins with the begincatch call or one of its adjacent operands
490// the block will not be split.
491static Instruction *findBeginCatchSplitPoint(BasicBlock *BB,
492 IntrinsicInst *II) {
493 // If the begincatch call is already the first instruction in the block,
494 // don't split.
495 Instruction *FirstNonPHI = BB->getFirstNonPHI();
496 if (II == FirstNonPHI)
497 return nullptr;
498
499 // If either operand is in the same basic block as the instruction and
500 // isn't used by another instruction before the begincatch call, include it
501 // in the split block.
502 auto *Op0 = dyn_cast<Instruction>(II->getOperand(0));
503 auto *Op1 = dyn_cast<Instruction>(II->getOperand(1));
504
505 Instruction *I = II->getPrevNode();
506 Instruction *LastI = II;
507
508 while (I == Op0 || I == Op1) {
509 // If the block begins with one of the operands and there are no other
510 // instructions between the operand and the begincatch call, don't split.
511 if (I == FirstNonPHI)
512 return nullptr;
513
514 LastI = I;
515 I = I->getPrevNode();
516 }
517
518 // If there is at least one instruction in the block before the begincatch
519 // call and its operands, split the block at either the begincatch or
520 // its operand.
521 return LastI;
522}
523
Reid Klecknerfd7df282015-04-22 21:05:21 +0000524/// Find all points where exceptional control rejoins normal control flow via
525/// llvm.eh.endcatch. Add them to the normal bb reachability worklist.
Andrew Kaylor046f7b42015-04-28 21:54:14 +0000526void WinEHPrepare::findCXXEHReturnPoints(
527 Function &F, SetVector<BasicBlock *> &EHReturnBlocks) {
Reid Klecknerfd7df282015-04-22 21:05:21 +0000528 for (auto BBI = F.begin(), BBE = F.end(); BBI != BBE; ++BBI) {
529 BasicBlock *BB = BBI;
530 for (Instruction &I : *BB) {
Andrew Kaylor046f7b42015-04-28 21:54:14 +0000531 if (match(&I, m_Intrinsic<Intrinsic::eh_begincatch>())) {
Andrew Kaylor91307432015-04-28 22:01:51 +0000532 Instruction *SplitPt =
Andrew Kaylor046f7b42015-04-28 21:54:14 +0000533 findBeginCatchSplitPoint(BB, cast<IntrinsicInst>(&I));
534 if (SplitPt) {
535 // Split the block before the llvm.eh.begincatch call to allow
536 // cleanup and catch code to be distinguished later.
537 // Do not update BBI because we still need to process the
538 // portion of the block that we are splitting off.
Andrew Kaylora33f1592015-04-29 17:21:26 +0000539 SplitBlock(BB, SplitPt, DT);
Andrew Kaylor046f7b42015-04-28 21:54:14 +0000540 break;
541 }
542 }
Reid Klecknerfd7df282015-04-22 21:05:21 +0000543 if (match(&I, m_Intrinsic<Intrinsic::eh_endcatch>())) {
544 // Split the block after the call to llvm.eh.endcatch if there is
545 // anything other than an unconditional branch, or if the successor
546 // starts with a phi.
547 auto *Br = dyn_cast<BranchInst>(I.getNextNode());
548 if (!Br || !Br->isUnconditional() ||
549 isa<PHINode>(Br->getSuccessor(0)->begin())) {
550 DEBUG(dbgs() << "splitting block " << BB->getName()
551 << " with llvm.eh.endcatch\n");
Andrew Kaylor046f7b42015-04-28 21:54:14 +0000552 BBI = SplitBlock(BB, I.getNextNode(), DT);
Reid Klecknerfd7df282015-04-22 21:05:21 +0000553 }
554 // The next BB is normal control flow.
555 EHReturnBlocks.insert(BB->getTerminator()->getSuccessor(0));
556 break;
557 }
558 }
559 }
560}
561
562static bool isCatchAllLandingPad(const BasicBlock *BB) {
563 const LandingPadInst *LP = BB->getLandingPadInst();
564 if (!LP)
565 return false;
566 unsigned N = LP->getNumClauses();
567 return (N > 0 && LP->isCatch(N - 1) &&
568 isa<ConstantPointerNull>(LP->getClause(N - 1)));
569}
570
571/// Find all points where exceptions control rejoins normal control flow via
572/// selector dispatch.
Andrew Kaylor046f7b42015-04-28 21:54:14 +0000573void WinEHPrepare::findSEHEHReturnPoints(
574 Function &F, SetVector<BasicBlock *> &EHReturnBlocks) {
Reid Klecknerfd7df282015-04-22 21:05:21 +0000575 for (auto BBI = F.begin(), BBE = F.end(); BBI != BBE; ++BBI) {
576 BasicBlock *BB = BBI;
577 // If the landingpad is a catch-all, treat the whole lpad as if it is
578 // reachable from normal control flow.
579 // FIXME: This is imprecise. We need a better way of identifying where a
580 // catch-all starts and cleanups stop. As far as LLVM is concerned, there
581 // is no difference.
582 if (isCatchAllLandingPad(BB)) {
583 EHReturnBlocks.insert(BB);
584 continue;
585 }
586
587 BasicBlock *CatchHandler;
588 BasicBlock *NextBB;
589 Constant *Selector;
590 if (isSelectorDispatch(BB, CatchHandler, Selector, NextBB)) {
Reid Klecknered012db2015-07-08 18:08:52 +0000591 // Split the edge if there are multiple predecessors. This creates a place
592 // where we can insert EH recovery code.
593 if (!CatchHandler->getSinglePredecessor()) {
Reid Klecknerfd7df282015-04-22 21:05:21 +0000594 DEBUG(dbgs() << "splitting EH return edge from " << BB->getName()
595 << " to " << CatchHandler->getName() << '\n');
596 BBI = CatchHandler = SplitCriticalEdge(
597 BB, std::find(succ_begin(BB), succ_end(BB), CatchHandler));
598 }
599 EHReturnBlocks.insert(CatchHandler);
600 }
601 }
602}
603
Andrew Kaylora6c5b962015-05-20 23:22:24 +0000604void WinEHPrepare::identifyEHBlocks(Function &F,
605 SmallVectorImpl<LandingPadInst *> &LPads) {
Reid Klecknerfd7df282015-04-22 21:05:21 +0000606 DEBUG(dbgs() << "Demoting values live across exception handlers in function "
607 << F.getName() << '\n');
608
609 // Build a set of all non-exceptional blocks and exceptional blocks.
610 // - Non-exceptional blocks are blocks reachable from the entry block while
611 // not following invoke unwind edges.
612 // - Exceptional blocks are blocks reachable from landingpads. Analysis does
613 // not follow llvm.eh.endcatch blocks, which mark a transition from
614 // exceptional to normal control.
Reid Klecknerfd7df282015-04-22 21:05:21 +0000615
616 if (Personality == EHPersonality::MSVC_CXX)
617 findCXXEHReturnPoints(F, EHReturnBlocks);
618 else
619 findSEHEHReturnPoints(F, EHReturnBlocks);
620
621 DEBUG({
622 dbgs() << "identified the following blocks as EH return points:\n";
623 for (BasicBlock *BB : EHReturnBlocks)
624 dbgs() << " " << BB->getName() << '\n';
625 });
626
Andrew Kaylor91307432015-04-28 22:01:51 +0000627// Join points should not have phis at this point, unless they are a
628// landingpad, in which case we will demote their phis later.
Reid Klecknerfd7df282015-04-22 21:05:21 +0000629#ifndef NDEBUG
630 for (BasicBlock *BB : EHReturnBlocks)
631 assert((BB->isLandingPad() || !isa<PHINode>(BB->begin())) &&
632 "non-lpad EH return block has phi");
633#endif
634
635 // Normal blocks are the blocks reachable from the entry block and all EH
636 // return points.
Andrew Kaylora6c5b962015-05-20 23:22:24 +0000637 SetVector<BasicBlock *> Worklist;
Reid Klecknerfd7df282015-04-22 21:05:21 +0000638 Worklist = EHReturnBlocks;
639 Worklist.insert(&F.getEntryBlock());
640 findReachableBlocks(NormalBlocks, Worklist, nullptr);
641 DEBUG({
642 dbgs() << "marked the following blocks as normal:\n";
643 for (BasicBlock *BB : NormalBlocks)
644 dbgs() << " " << BB->getName() << '\n';
645 });
646
647 // Exceptional blocks are the blocks reachable from landingpads that don't
648 // cross EH return points.
649 Worklist.clear();
650 for (auto *LPI : LPads)
651 Worklist.insert(LPI->getParent());
652 findReachableBlocks(EHBlocks, Worklist, &EHReturnBlocks);
653 DEBUG({
654 dbgs() << "marked the following blocks as exceptional:\n";
655 for (BasicBlock *BB : EHBlocks)
656 dbgs() << " " << BB->getName() << '\n';
657 });
658
Andrew Kaylora6c5b962015-05-20 23:22:24 +0000659}
660
661/// Ensure that all values live into and out of exception handlers are stored
662/// in memory.
663/// FIXME: This falls down when values are defined in one handler and live into
664/// another handler. For example, a cleanup defines a value used only by a
665/// catch handler.
666void WinEHPrepare::demoteValuesLiveAcrossHandlers(
667 Function &F, SmallVectorImpl<LandingPadInst *> &LPads) {
668 DEBUG(dbgs() << "Demoting values live across exception handlers in function "
669 << F.getName() << '\n');
670
671 // identifyEHBlocks() should have been called before this function.
672 assert(!NormalBlocks.empty());
673
Reid Kleckner7ea77082015-07-10 22:21:54 +0000674 // Try to avoid demoting EH pointer and selector values. They get in the way
675 // of our pattern matching.
676 SmallPtrSet<Instruction *, 10> EHVals;
677 for (BasicBlock &BB : F) {
678 LandingPadInst *LP = BB.getLandingPadInst();
679 if (!LP)
680 continue;
681 EHVals.insert(LP);
682 for (User *U : LP->users()) {
683 auto *EI = dyn_cast<ExtractValueInst>(U);
684 if (!EI)
685 continue;
686 EHVals.insert(EI);
687 for (User *U2 : EI->users()) {
688 if (auto *PN = dyn_cast<PHINode>(U2))
689 EHVals.insert(PN);
690 }
691 }
692 }
693
Reid Klecknerfd7df282015-04-22 21:05:21 +0000694 SetVector<Argument *> ArgsToDemote;
695 SetVector<Instruction *> InstrsToDemote;
696 for (BasicBlock &BB : F) {
697 bool IsNormalBB = NormalBlocks.count(&BB);
698 bool IsEHBB = EHBlocks.count(&BB);
699 if (!IsNormalBB && !IsEHBB)
700 continue; // Blocks that are neither normal nor EH are unreachable.
701 for (Instruction &I : BB) {
702 for (Value *Op : I.operands()) {
703 // Don't demote static allocas, constants, and labels.
704 if (isa<Constant>(Op) || isa<BasicBlock>(Op) || isa<InlineAsm>(Op))
705 continue;
706 auto *AI = dyn_cast<AllocaInst>(Op);
707 if (AI && AI->isStaticAlloca())
708 continue;
709
710 if (auto *Arg = dyn_cast<Argument>(Op)) {
711 if (IsEHBB) {
712 DEBUG(dbgs() << "Demoting argument " << *Arg
713 << " used by EH instr: " << I << "\n");
714 ArgsToDemote.insert(Arg);
715 }
716 continue;
717 }
718
Reid Kleckner7ea77082015-07-10 22:21:54 +0000719 // Don't demote EH values.
Reid Klecknerfd7df282015-04-22 21:05:21 +0000720 auto *OpI = cast<Instruction>(Op);
Reid Kleckner7ea77082015-07-10 22:21:54 +0000721 if (EHVals.count(OpI))
722 continue;
723
Reid Klecknerfd7df282015-04-22 21:05:21 +0000724 BasicBlock *OpBB = OpI->getParent();
725 // If a value is produced and consumed in the same BB, we don't need to
726 // demote it.
727 if (OpBB == &BB)
728 continue;
729 bool IsOpNormalBB = NormalBlocks.count(OpBB);
730 bool IsOpEHBB = EHBlocks.count(OpBB);
731 if (IsNormalBB != IsOpNormalBB || IsEHBB != IsOpEHBB) {
732 DEBUG({
733 dbgs() << "Demoting instruction live in-out from EH:\n";
734 dbgs() << "Instr: " << *OpI << '\n';
735 dbgs() << "User: " << I << '\n';
736 });
737 InstrsToDemote.insert(OpI);
738 }
739 }
740 }
741 }
742
743 // Demote values live into and out of handlers.
744 // FIXME: This demotion is inefficient. We should insert spills at the point
745 // of definition, insert one reload in each handler that uses the value, and
746 // insert reloads in the BB used to rejoin normal control flow.
747 Instruction *AllocaInsertPt = F.getEntryBlock().getFirstInsertionPt();
748 for (Instruction *I : InstrsToDemote)
749 DemoteRegToStack(*I, false, AllocaInsertPt);
750
751 // Demote arguments separately, and only for uses in EH blocks.
752 for (Argument *Arg : ArgsToDemote) {
753 auto *Slot = new AllocaInst(Arg->getType(), nullptr,
754 Arg->getName() + ".reg2mem", AllocaInsertPt);
755 SmallVector<User *, 4> Users(Arg->user_begin(), Arg->user_end());
756 for (User *U : Users) {
757 auto *I = dyn_cast<Instruction>(U);
758 if (I && EHBlocks.count(I->getParent())) {
759 auto *Reload = new LoadInst(Slot, Arg->getName() + ".reload", false, I);
760 U->replaceUsesOfWith(Arg, Reload);
761 }
762 }
763 new StoreInst(Arg, Slot, AllocaInsertPt);
764 }
765
766 // Demote landingpad phis, as the landingpad will be removed from the machine
767 // CFG.
768 for (LandingPadInst *LPI : LPads) {
769 BasicBlock *BB = LPI->getParent();
770 while (auto *Phi = dyn_cast<PHINode>(BB->begin()))
771 DemotePHIToStack(Phi, AllocaInsertPt);
772 }
773
774 DEBUG(dbgs() << "Demoted " << InstrsToDemote.size() << " instructions and "
775 << ArgsToDemote.size() << " arguments for WinEHPrepare\n\n");
776}
777
Reid Kleckner0f9e27a2015-03-18 20:26:53 +0000778bool WinEHPrepare::prepareExceptionHandlers(
Andrew Kaylor1476e6d2015-02-24 20:49:35 +0000779 Function &F, SmallVectorImpl<LandingPadInst *> &LPads) {
Reid Klecknerfd7df282015-04-22 21:05:21 +0000780 // Don't run on functions that are already prepared.
781 for (LandingPadInst *LPad : LPads) {
782 BasicBlock *LPadBB = LPad->getParent();
783 for (Instruction &Inst : *LPadBB)
784 if (match(&Inst, m_Intrinsic<Intrinsic::eh_actions>()))
785 return false;
786 }
787
Andrew Kaylora6c5b962015-05-20 23:22:24 +0000788 identifyEHBlocks(F, LPads);
Reid Klecknerfd7df282015-04-22 21:05:21 +0000789 demoteValuesLiveAcrossHandlers(F, LPads);
790
Andrew Kaylor1476e6d2015-02-24 20:49:35 +0000791 // These containers are used to re-map frame variables that are used in
792 // outlined catch and cleanup handlers. They will be populated as the
793 // handlers are outlined.
794 FrameVarInfoMap FrameVarInfo;
Andrew Kaylor1476e6d2015-02-24 20:49:35 +0000795
796 bool HandlersOutlined = false;
797
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000798 Module *M = F.getParent();
799 LLVMContext &Context = M->getContext();
800
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000801 // Create a new function to receive the handler contents.
802 PointerType *Int8PtrType = Type::getInt8PtrTy(Context);
803 Type *Int32Type = Type::getInt32Ty(Context);
Reid Kleckner52b07792015-03-12 01:45:37 +0000804 Function *ActionIntrin = Intrinsic::getDeclaration(M, Intrinsic::eh_actions);
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000805
Reid Klecknercfbfe6f2015-04-24 20:25:05 +0000806 if (isAsynchronousEHPersonality(Personality)) {
807 // FIXME: Switch the ehptr type to i32 and then switch this.
808 SEHExceptionCodeSlot =
809 new AllocaInst(Int8PtrType, nullptr, "seh_exception_code",
810 F.getEntryBlock().getFirstInsertionPt());
811 }
812
Andrew Kaylora6c5b962015-05-20 23:22:24 +0000813 // In order to handle the case where one outlined catch handler returns
814 // to a block within another outlined catch handler that would otherwise
815 // be unreachable, we need to outline the nested landing pad before we
816 // outline the landing pad which encloses it.
Manuel Klimekb00d42c2015-05-21 15:38:25 +0000817 if (!isAsynchronousEHPersonality(Personality))
818 std::sort(LPads.begin(), LPads.end(),
819 [this](LandingPadInst *const &L, LandingPadInst *const &R) {
820 return DT->properlyDominates(R->getParent(), L->getParent());
Andrew Kaylora6c5b962015-05-20 23:22:24 +0000821 });
822
Andrew Kaylorcc14f382015-05-11 23:06:02 +0000823 // This container stores the llvm.eh.recover and IndirectBr instructions
824 // that make up the body of each landing pad after it has been outlined.
825 // We need to defer the population of the target list for the indirectbr
826 // until all landing pads have been outlined so that we can handle the
827 // case of blocks in the target that are reached only from nested
828 // landing pads.
829 SmallVector<std::pair<CallInst*, IndirectBrInst *>, 4> LPadImpls;
830
Andrew Kaylor1476e6d2015-02-24 20:49:35 +0000831 for (LandingPadInst *LPad : LPads) {
832 // Look for evidence that this landingpad has already been processed.
833 bool LPadHasActionList = false;
834 BasicBlock *LPadBB = LPad->getParent();
Reid Klecknerc759fe92015-03-19 22:31:02 +0000835 for (Instruction &Inst : *LPadBB) {
Reid Klecknerfd7df282015-04-22 21:05:21 +0000836 if (match(&Inst, m_Intrinsic<Intrinsic::eh_actions>())) {
837 LPadHasActionList = true;
838 break;
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000839 }
Andrew Kaylor1476e6d2015-02-24 20:49:35 +0000840 }
841
842 // If we've already outlined the handlers for this landingpad,
843 // there's nothing more to do here.
844 if (LPadHasActionList)
845 continue;
846
Andrew Kaylor64622aa2015-04-01 17:21:25 +0000847 // If either of the values in the aggregate returned by the landing pad is
848 // extracted and stored to memory, promote the stored value to a register.
849 promoteLandingPadValues(LPad);
850
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000851 LandingPadActions Actions;
852 mapLandingPadBlocks(LPad, Actions);
Andrew Kaylorf0f5e462015-03-03 20:00:16 +0000853
Reid Kleckner9405ef02015-04-10 23:12:29 +0000854 HandlersOutlined |= !Actions.actions().empty();
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000855 for (ActionHandler *Action : Actions) {
Reid Kleckner0f9e27a2015-03-18 20:26:53 +0000856 if (Action->hasBeenProcessed())
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000857 continue;
858 BasicBlock *StartBB = Action->getStartBlock();
Reid Kleckner0f9e27a2015-03-18 20:26:53 +0000859
860 // SEH doesn't do any outlining for catches. Instead, pass the handler
861 // basic block addr to llvm.eh.actions and list the block as a return
862 // target.
863 if (isAsynchronousEHPersonality(Personality)) {
864 if (auto *CatchAction = dyn_cast<CatchHandler>(Action)) {
865 processSEHCatchHandler(CatchAction, StartBB);
Reid Kleckner0f9e27a2015-03-18 20:26:53 +0000866 continue;
867 }
868 }
869
Reid Kleckner9405ef02015-04-10 23:12:29 +0000870 outlineHandler(Action, &F, LPad, StartBB, FrameVarInfo);
871 }
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000872
Reid Kleckner2c3ccaa2015-04-24 16:22:19 +0000873 // Split the block after the landingpad instruction so that it is just a
874 // call to llvm.eh.actions followed by indirectbr.
875 assert(!isa<PHINode>(LPadBB->begin()) && "lpad phi not removed");
Andrew Kaylor046f7b42015-04-28 21:54:14 +0000876 SplitBlock(LPadBB, LPad->getNextNode(), DT);
Reid Kleckner2c3ccaa2015-04-24 16:22:19 +0000877 // Erase the branch inserted by the split so we can insert indirectbr.
878 LPadBB->getTerminator()->eraseFromParent();
Andrew Kayloraa92ab02015-04-03 19:37:50 +0000879
Reid Klecknere3af86e2015-04-23 21:22:30 +0000880 // Replace all extracted values with undef and ultimately replace the
881 // landingpad with undef.
Reid Klecknercfbfe6f2015-04-24 20:25:05 +0000882 SmallVector<Instruction *, 4> SEHCodeUses;
883 SmallVector<Instruction *, 4> EHUndefs;
Reid Klecknere3af86e2015-04-23 21:22:30 +0000884 for (User *U : LPad->users()) {
885 auto *E = dyn_cast<ExtractValueInst>(U);
886 if (!E)
887 continue;
888 assert(E->getNumIndices() == 1 &&
889 "Unexpected operation: extracting both landing pad values");
Reid Klecknercfbfe6f2015-04-24 20:25:05 +0000890 unsigned Idx = *E->idx_begin();
891 assert((Idx == 0 || Idx == 1) && "unexpected index");
892 if (Idx == 0 && isAsynchronousEHPersonality(Personality))
893 SEHCodeUses.push_back(E);
894 else
895 EHUndefs.push_back(E);
Reid Klecknere3af86e2015-04-23 21:22:30 +0000896 }
Reid Klecknercfbfe6f2015-04-24 20:25:05 +0000897 for (Instruction *E : EHUndefs) {
Reid Klecknere3af86e2015-04-23 21:22:30 +0000898 E->replaceAllUsesWith(UndefValue::get(E->getType()));
899 E->eraseFromParent();
900 }
901 LPad->replaceAllUsesWith(UndefValue::get(LPad->getType()));
Reid Kleckner0f9e27a2015-03-18 20:26:53 +0000902
Reid Klecknercfbfe6f2015-04-24 20:25:05 +0000903 // Rewrite uses of the exception pointer to loads of an alloca.
Reid Kleckner7ea77082015-07-10 22:21:54 +0000904 while (!SEHCodeUses.empty()) {
905 Instruction *E = SEHCodeUses.pop_back_val();
Reid Klecknercfbfe6f2015-04-24 20:25:05 +0000906 SmallVector<Use *, 4> Uses;
907 for (Use &U : E->uses())
908 Uses.push_back(&U);
909 for (Use *U : Uses) {
910 auto *I = cast<Instruction>(U->getUser());
911 if (isa<ResumeInst>(I))
912 continue;
Reid Klecknercfbfe6f2015-04-24 20:25:05 +0000913 if (auto *Phi = dyn_cast<PHINode>(I))
Reid Kleckner7ea77082015-07-10 22:21:54 +0000914 SEHCodeUses.push_back(Phi);
Reid Klecknercfbfe6f2015-04-24 20:25:05 +0000915 else
Reid Kleckner7ea77082015-07-10 22:21:54 +0000916 U->set(new LoadInst(SEHExceptionCodeSlot, "sehcode", false, I));
Reid Klecknercfbfe6f2015-04-24 20:25:05 +0000917 }
918 E->replaceAllUsesWith(UndefValue::get(E->getType()));
919 E->eraseFromParent();
920 }
921
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000922 // Add a call to describe the actions for this landing pad.
923 std::vector<Value *> ActionArgs;
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000924 for (ActionHandler *Action : Actions) {
Reid Klecknerc759fe92015-03-19 22:31:02 +0000925 // Action codes from docs are: 0 cleanup, 1 catch.
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000926 if (auto *CatchAction = dyn_cast<CatchHandler>(Action)) {
Reid Klecknerc759fe92015-03-19 22:31:02 +0000927 ActionArgs.push_back(ConstantInt::get(Int32Type, 1));
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000928 ActionArgs.push_back(CatchAction->getSelector());
Reid Kleckner3567d272015-04-02 21:13:31 +0000929 // Find the frame escape index of the exception object alloca in the
930 // parent.
931 int FrameEscapeIdx = -1;
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000932 Value *EHObj = const_cast<Value *>(CatchAction->getExceptionVar());
Reid Kleckner3567d272015-04-02 21:13:31 +0000933 if (EHObj && !isa<ConstantPointerNull>(EHObj)) {
934 auto I = FrameVarInfo.find(EHObj);
935 assert(I != FrameVarInfo.end() &&
936 "failed to map llvm.eh.begincatch var");
937 FrameEscapeIdx = std::distance(FrameVarInfo.begin(), I);
938 }
939 ActionArgs.push_back(ConstantInt::get(Int32Type, FrameEscapeIdx));
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000940 } else {
Reid Klecknerc759fe92015-03-19 22:31:02 +0000941 ActionArgs.push_back(ConstantInt::get(Int32Type, 0));
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000942 }
Reid Klecknerc759fe92015-03-19 22:31:02 +0000943 ActionArgs.push_back(Action->getHandlerBlockOrFunc());
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000944 }
945 CallInst *Recover =
Reid Kleckner2c3ccaa2015-04-24 16:22:19 +0000946 CallInst::Create(ActionIntrin, ActionArgs, "recover", LPadBB);
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000947
Andrew Kaylora6c5b962015-05-20 23:22:24 +0000948 SetVector<BasicBlock *> ReturnTargets;
949 for (ActionHandler *Action : Actions) {
950 if (auto *CatchAction = dyn_cast<CatchHandler>(Action)) {
951 const auto &CatchTargets = CatchAction->getReturnTargets();
952 ReturnTargets.insert(CatchTargets.begin(), CatchTargets.end());
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000953 }
Andrew Kaylora6c5b962015-05-20 23:22:24 +0000954 }
955 IndirectBrInst *Branch =
956 IndirectBrInst::Create(Recover, ReturnTargets.size(), LPadBB);
957 for (BasicBlock *Target : ReturnTargets)
958 Branch->addDestination(Target);
Andrew Kaylorcc14f382015-05-11 23:06:02 +0000959
Andrew Kaylora6c5b962015-05-20 23:22:24 +0000960 if (!isAsynchronousEHPersonality(Personality)) {
961 // C++ EH must repopulate the targets later to handle the case of
962 // targets that are reached indirectly through nested landing pads.
Andrew Kaylorcc14f382015-05-11 23:06:02 +0000963 LPadImpls.push_back(std::make_pair(Recover, Branch));
Andrew Kaylorf0f5e462015-03-03 20:00:16 +0000964 }
Andrew Kaylora6c5b962015-05-20 23:22:24 +0000965
Andrew Kaylorf0f5e462015-03-03 20:00:16 +0000966 } // End for each landingpad
Andrew Kaylor1476e6d2015-02-24 20:49:35 +0000967
968 // If nothing got outlined, there is no more processing to be done.
969 if (!HandlersOutlined)
970 return false;
971
Andrew Kayloraa92ab02015-04-03 19:37:50 +0000972 // Replace any nested landing pad stubs with the correct action handler.
973 // This must be done before we remove unreachable blocks because it
974 // cleans up references to outlined blocks that will be deleted.
975 for (auto &LPadPair : NestedLPtoOriginalLP)
976 completeNestedLandingPad(&F, LPadPair.first, LPadPair.second, FrameVarInfo);
Andrew Kaylor67d3c032015-04-08 20:57:22 +0000977 NestedLPtoOriginalLP.clear();
Andrew Kayloraa92ab02015-04-03 19:37:50 +0000978
Andrew Kaylora6c5b962015-05-20 23:22:24 +0000979 // Update the indirectbr instructions' target lists if necessary.
Andrew Kaylorcc14f382015-05-11 23:06:02 +0000980 SetVector<BasicBlock*> CheckedTargets;
Benjamin Kramera48e0652015-05-16 15:40:03 +0000981 SmallVector<std::unique_ptr<ActionHandler>, 4> ActionList;
Andrew Kaylorcc14f382015-05-11 23:06:02 +0000982 for (auto &LPadImplPair : LPadImpls) {
983 IntrinsicInst *Recover = cast<IntrinsicInst>(LPadImplPair.first);
984 IndirectBrInst *Branch = LPadImplPair.second;
985
986 // Get a list of handlers called by
Andrew Kaylorcc14f382015-05-11 23:06:02 +0000987 parseEHActions(Recover, ActionList);
988
989 // Add an indirect branch listing possible successors of the catch handlers.
990 SetVector<BasicBlock *> ReturnTargets;
Benjamin Kramera48e0652015-05-16 15:40:03 +0000991 for (const auto &Action : ActionList) {
992 if (auto *CA = dyn_cast<CatchHandler>(Action.get())) {
Andrew Kaylorcc14f382015-05-11 23:06:02 +0000993 Function *Handler = cast<Function>(CA->getHandlerBlockOrFunc());
994 getPossibleReturnTargets(&F, Handler, ReturnTargets);
995 }
996 }
Andrew Kaylor0ddaf2b2015-05-12 00:13:51 +0000997 ActionList.clear();
Andrew Kaylora6c5b962015-05-20 23:22:24 +0000998 // Clear any targets we already knew about.
999 for (unsigned int I = 0, E = Branch->getNumDestinations(); I < E; ++I) {
1000 BasicBlock *KnownTarget = Branch->getDestination(I);
1001 if (ReturnTargets.count(KnownTarget))
1002 ReturnTargets.remove(KnownTarget);
1003 }
Andrew Kaylorcc14f382015-05-11 23:06:02 +00001004 for (BasicBlock *Target : ReturnTargets) {
1005 Branch->addDestination(Target);
1006 // The target may be a block that we excepted to get pruned.
1007 // If it is, it may contain a call to llvm.eh.endcatch.
1008 if (CheckedTargets.insert(Target)) {
1009 // Earlier preparations guarantee that all calls to llvm.eh.endcatch
1010 // will be followed by an unconditional branch.
1011 auto *Br = dyn_cast<BranchInst>(Target->getTerminator());
1012 if (Br && Br->isUnconditional() &&
1013 Br != Target->getFirstNonPHIOrDbgOrLifetime()) {
1014 Instruction *Prev = Br->getPrevNode();
1015 if (match(cast<Value>(Prev), m_Intrinsic<Intrinsic::eh_endcatch>()))
1016 Prev->eraseFromParent();
1017 }
1018 }
1019 }
1020 }
1021 LPadImpls.clear();
1022
David Majnemercde33032015-03-30 22:58:10 +00001023 F.addFnAttr("wineh-parent", F.getName());
1024
Andrew Kaylor6b67d422015-03-11 23:22:06 +00001025 // Delete any blocks that were only used by handlers that were outlined above.
1026 removeUnreachableBlocks(F);
Andrew Kaylor1476e6d2015-02-24 20:49:35 +00001027
Andrew Kaylor1476e6d2015-02-24 20:49:35 +00001028 BasicBlock *Entry = &F.getEntryBlock();
Reid Klecknercfb9ce52015-03-05 18:26:34 +00001029 IRBuilder<> Builder(F.getParent()->getContext());
Andrew Kaylor1476e6d2015-02-24 20:49:35 +00001030 Builder.SetInsertPoint(Entry->getFirstInsertionPt());
Andrew Kaylor1476e6d2015-02-24 20:49:35 +00001031
Reid Klecknercfb9ce52015-03-05 18:26:34 +00001032 Function *FrameEscapeFn =
Reid Kleckner60381792015-07-07 22:25:32 +00001033 Intrinsic::getDeclaration(M, Intrinsic::localescape);
Reid Klecknercfb9ce52015-03-05 18:26:34 +00001034 Function *RecoverFrameFn =
Reid Kleckner60381792015-07-07 22:25:32 +00001035 Intrinsic::getDeclaration(M, Intrinsic::localrecover);
Reid Klecknerf14787d2015-04-22 00:07:52 +00001036 SmallVector<Value *, 8> AllocasToEscape;
1037
Reid Kleckner60381792015-07-07 22:25:32 +00001038 // Scan the entry block for an existing call to llvm.localescape. We need to
Reid Klecknerf14787d2015-04-22 00:07:52 +00001039 // keep escaping those objects.
1040 for (Instruction &I : F.front()) {
1041 auto *II = dyn_cast<IntrinsicInst>(&I);
Reid Kleckner60381792015-07-07 22:25:32 +00001042 if (II && II->getIntrinsicID() == Intrinsic::localescape) {
Reid Klecknerf14787d2015-04-22 00:07:52 +00001043 auto Args = II->arg_operands();
1044 AllocasToEscape.append(Args.begin(), Args.end());
1045 II->eraseFromParent();
1046 break;
1047 }
1048 }
Andrew Kaylor1476e6d2015-02-24 20:49:35 +00001049
Andrew Kaylor1476e6d2015-02-24 20:49:35 +00001050 // Finally, replace all of the temporary allocas for frame variables used in
Reid Kleckner60381792015-07-07 22:25:32 +00001051 // the outlined handlers with calls to llvm.localrecover.
Andrew Kaylor1476e6d2015-02-24 20:49:35 +00001052 for (auto &VarInfoEntry : FrameVarInfo) {
Andrew Kaylor72029c62015-03-03 00:41:03 +00001053 Value *ParentVal = VarInfoEntry.first;
Reid Klecknercfb9ce52015-03-05 18:26:34 +00001054 TinyPtrVector<AllocaInst *> &Allocas = VarInfoEntry.second;
Reid Klecknerfd7df282015-04-22 21:05:21 +00001055 AllocaInst *ParentAlloca = cast<AllocaInst>(ParentVal);
Andrew Kaylor72029c62015-03-03 00:41:03 +00001056
Reid Klecknerb4019412015-04-06 18:50:38 +00001057 // FIXME: We should try to sink unescaped allocas from the parent frame into
1058 // the child frame. If the alloca is escaped, we have to use the lifetime
1059 // markers to ensure that the alloca is only live within the child frame.
Andrew Kaylor1476e6d2015-02-24 20:49:35 +00001060
Reid Klecknercfb9ce52015-03-05 18:26:34 +00001061 // Add this alloca to the list of things to escape.
1062 AllocasToEscape.push_back(ParentAlloca);
Andrew Kaylor1476e6d2015-02-24 20:49:35 +00001063
Reid Klecknercfb9ce52015-03-05 18:26:34 +00001064 // Next replace all outlined allocas that are mapped to it.
1065 for (AllocaInst *TempAlloca : Allocas) {
Reid Kleckner3567d272015-04-02 21:13:31 +00001066 if (TempAlloca == getCatchObjectSentinel())
1067 continue; // Skip catch parameter sentinels.
Reid Klecknercfb9ce52015-03-05 18:26:34 +00001068 Function *HandlerFn = TempAlloca->getParent()->getParent();
Reid Klecknerbcda1cd2015-04-29 22:49:54 +00001069 llvm::Value *FP = HandlerToParentFP[HandlerFn];
1070 assert(FP);
1071
Reid Kleckner60381792015-07-07 22:25:32 +00001072 // FIXME: Sink this localrecover into the blocks where it is used.
Reid Klecknercfb9ce52015-03-05 18:26:34 +00001073 Builder.SetInsertPoint(TempAlloca);
1074 Builder.SetCurrentDebugLocation(TempAlloca->getDebugLoc());
1075 Value *RecoverArgs[] = {
Reid Klecknerbcda1cd2015-04-29 22:49:54 +00001076 Builder.CreateBitCast(&F, Int8PtrType, ""), FP,
Reid Klecknercfb9ce52015-03-05 18:26:34 +00001077 llvm::ConstantInt::get(Int32Type, AllocasToEscape.size() - 1)};
Reid Klecknerbcda1cd2015-04-29 22:49:54 +00001078 Instruction *RecoveredAlloca =
1079 Builder.CreateCall(RecoverFrameFn, RecoverArgs);
1080
Reid Klecknercfb9ce52015-03-05 18:26:34 +00001081 // Add a pointer bitcast if the alloca wasn't an i8.
1082 if (RecoveredAlloca->getType() != TempAlloca->getType()) {
1083 RecoveredAlloca->setName(Twine(TempAlloca->getName()) + ".i8");
Reid Klecknerbcda1cd2015-04-29 22:49:54 +00001084 RecoveredAlloca = cast<Instruction>(
1085 Builder.CreateBitCast(RecoveredAlloca, TempAlloca->getType()));
Andrew Kaylor1476e6d2015-02-24 20:49:35 +00001086 }
Reid Klecknercfb9ce52015-03-05 18:26:34 +00001087 TempAlloca->replaceAllUsesWith(RecoveredAlloca);
1088 TempAlloca->removeFromParent();
1089 RecoveredAlloca->takeName(TempAlloca);
1090 delete TempAlloca;
1091 }
Andrew Kaylor6b67d422015-03-11 23:22:06 +00001092 } // End for each FrameVarInfo entry.
Andrew Kaylor1476e6d2015-02-24 20:49:35 +00001093
Reid Kleckner60381792015-07-07 22:25:32 +00001094 // Insert 'call void (...)* @llvm.localescape(...)' at the end of the entry
Reid Klecknercfb9ce52015-03-05 18:26:34 +00001095 // block.
1096 Builder.SetInsertPoint(&F.getEntryBlock().back());
1097 Builder.CreateCall(FrameEscapeFn, AllocasToEscape);
1098
Reid Klecknercfbfe6f2015-04-24 20:25:05 +00001099 if (SEHExceptionCodeSlot) {
Reid Klecknerf12c0302015-06-09 21:42:19 +00001100 if (isAllocaPromotable(SEHExceptionCodeSlot)) {
1101 SmallPtrSet<BasicBlock *, 4> UserBlocks;
1102 for (User *U : SEHExceptionCodeSlot->users()) {
1103 if (auto *Inst = dyn_cast<Instruction>(U))
1104 UserBlocks.insert(Inst->getParent());
1105 }
Reid Klecknercfbfe6f2015-04-24 20:25:05 +00001106 PromoteMemToReg(SEHExceptionCodeSlot, *DT);
Reid Klecknerf12c0302015-06-09 21:42:19 +00001107 // After the promotion, kill off dead instructions.
1108 for (BasicBlock *BB : UserBlocks)
1109 SimplifyInstructionsInBlock(BB, LibInfo);
1110 }
Reid Klecknercfbfe6f2015-04-24 20:25:05 +00001111 }
1112
Andrew Kaylor6b67d422015-03-11 23:22:06 +00001113 // Clean up the handler action maps we created for this function
1114 DeleteContainerSeconds(CatchHandlerMap);
1115 CatchHandlerMap.clear();
1116 DeleteContainerSeconds(CleanupHandlerMap);
1117 CleanupHandlerMap.clear();
Reid Klecknerbcda1cd2015-04-29 22:49:54 +00001118 HandlerToParentFP.clear();
1119 DT = nullptr;
Reid Klecknerf12c0302015-06-09 21:42:19 +00001120 LibInfo = nullptr;
Ahmed Bougachaed363c52015-05-06 01:28:58 +00001121 SEHExceptionCodeSlot = nullptr;
Andrew Kaylora6c5b962015-05-20 23:22:24 +00001122 EHBlocks.clear();
1123 NormalBlocks.clear();
1124 EHReturnBlocks.clear();
Andrew Kaylor6b67d422015-03-11 23:22:06 +00001125
Andrew Kaylor1476e6d2015-02-24 20:49:35 +00001126 return HandlersOutlined;
1127}
1128
Andrew Kaylor64622aa2015-04-01 17:21:25 +00001129void WinEHPrepare::promoteLandingPadValues(LandingPadInst *LPad) {
1130 // If the return values of the landing pad instruction are extracted and
1131 // stored to memory, we want to promote the store locations to reg values.
1132 SmallVector<AllocaInst *, 2> EHAllocas;
1133
1134 // The landingpad instruction returns an aggregate value. Typically, its
1135 // value will be passed to a pair of extract value instructions and the
1136 // results of those extracts are often passed to store instructions.
1137 // In unoptimized code the stored value will often be loaded and then stored
1138 // again.
1139 for (auto *U : LPad->users()) {
1140 ExtractValueInst *Extract = dyn_cast<ExtractValueInst>(U);
1141 if (!Extract)
1142 continue;
1143
1144 for (auto *EU : Extract->users()) {
1145 if (auto *Store = dyn_cast<StoreInst>(EU)) {
1146 auto *AV = cast<AllocaInst>(Store->getPointerOperand());
1147 EHAllocas.push_back(AV);
1148 }
1149 }
1150 }
1151
1152 // We can't do this without a dominator tree.
1153 assert(DT);
1154
1155 if (!EHAllocas.empty()) {
1156 PromoteMemToReg(EHAllocas, *DT);
1157 EHAllocas.clear();
1158 }
Reid Kleckner86762142015-04-16 00:02:04 +00001159
1160 // After promotion, some extracts may be trivially dead. Remove them.
1161 SmallVector<Value *, 4> Users(LPad->user_begin(), LPad->user_end());
1162 for (auto *U : Users)
1163 RecursivelyDeleteTriviallyDeadInstructions(U);
Andrew Kaylor64622aa2015-04-01 17:21:25 +00001164}
1165
Andrew Kaylorcc14f382015-05-11 23:06:02 +00001166void WinEHPrepare::getPossibleReturnTargets(Function *ParentF,
1167 Function *HandlerF,
1168 SetVector<BasicBlock*> &Targets) {
1169 for (BasicBlock &BB : *HandlerF) {
1170 // If the handler contains landing pads, check for any
1171 // handlers that may return directly to a block in the
1172 // parent function.
1173 if (auto *LPI = BB.getLandingPadInst()) {
1174 IntrinsicInst *Recover = cast<IntrinsicInst>(LPI->getNextNode());
Benjamin Kramera48e0652015-05-16 15:40:03 +00001175 SmallVector<std::unique_ptr<ActionHandler>, 4> ActionList;
Andrew Kaylorcc14f382015-05-11 23:06:02 +00001176 parseEHActions(Recover, ActionList);
Benjamin Kramera48e0652015-05-16 15:40:03 +00001177 for (const auto &Action : ActionList) {
1178 if (auto *CH = dyn_cast<CatchHandler>(Action.get())) {
Andrew Kaylorcc14f382015-05-11 23:06:02 +00001179 Function *NestedF = cast<Function>(CH->getHandlerBlockOrFunc());
1180 getPossibleReturnTargets(ParentF, NestedF, Targets);
1181 }
1182 }
1183 }
1184
1185 auto *Ret = dyn_cast<ReturnInst>(BB.getTerminator());
1186 if (!Ret)
1187 continue;
1188
1189 // Handler functions must always return a block address.
1190 BlockAddress *BA = cast<BlockAddress>(Ret->getReturnValue());
1191
1192 // If this is the handler for a nested landing pad, the
1193 // return address may have been remapped to a block in the
1194 // parent handler. We're not interested in those.
1195 if (BA->getFunction() != ParentF)
1196 continue;
1197
1198 Targets.insert(BA->getBasicBlock());
1199 }
1200}
1201
Andrew Kayloraa92ab02015-04-03 19:37:50 +00001202void WinEHPrepare::completeNestedLandingPad(Function *ParentFn,
1203 LandingPadInst *OutlinedLPad,
1204 const LandingPadInst *OriginalLPad,
1205 FrameVarInfoMap &FrameVarInfo) {
1206 // Get the nested block and erase the unreachable instruction that was
1207 // temporarily inserted as its terminator.
1208 LLVMContext &Context = ParentFn->getContext();
1209 BasicBlock *OutlinedBB = OutlinedLPad->getParent();
Andrew Kaylora6c5b962015-05-20 23:22:24 +00001210 // If the nested landing pad was outlined before the landing pad that enclosed
1211 // it, it will already be in outlined form. In that case, we just need to see
1212 // if the returns and the enclosing branch instruction need to be updated.
1213 IndirectBrInst *Branch =
1214 dyn_cast<IndirectBrInst>(OutlinedBB->getTerminator());
1215 if (!Branch) {
1216 // If the landing pad wasn't in outlined form, it should be a stub with
1217 // an unreachable terminator.
1218 assert(isa<UnreachableInst>(OutlinedBB->getTerminator()));
1219 OutlinedBB->getTerminator()->eraseFromParent();
1220 // That should leave OutlinedLPad as the last instruction in its block.
1221 assert(&OutlinedBB->back() == OutlinedLPad);
1222 }
Andrew Kayloraa92ab02015-04-03 19:37:50 +00001223
1224 // The original landing pad will have already had its action intrinsic
1225 // built by the outlining loop. We need to clone that into the outlined
1226 // location. It may also be necessary to add references to the exception
1227 // variables to the outlined handler in which this landing pad is nested
1228 // and remap return instructions in the nested handlers that should return
1229 // to an address in the outlined handler.
1230 Function *OutlinedHandlerFn = OutlinedBB->getParent();
Andrew Kayloraa92ab02015-04-03 19:37:50 +00001231 BasicBlock::const_iterator II = OriginalLPad;
1232 ++II;
1233 // The instruction after the landing pad should now be a call to eh.actions.
1234 const Instruction *Recover = II;
Andrew Kaylora6c5b962015-05-20 23:22:24 +00001235 const IntrinsicInst *EHActions = cast<IntrinsicInst>(Recover);
Andrew Kayloraa92ab02015-04-03 19:37:50 +00001236
Andrew Kaylora6c5b962015-05-20 23:22:24 +00001237 // Remap the return target in the nested handler.
Andrew Kayloraa92ab02015-04-03 19:37:50 +00001238 SmallVector<BlockAddress *, 4> ActionTargets;
Benjamin Kramera48e0652015-05-16 15:40:03 +00001239 SmallVector<std::unique_ptr<ActionHandler>, 4> ActionList;
Andrew Kayloraa92ab02015-04-03 19:37:50 +00001240 parseEHActions(EHActions, ActionList);
Benjamin Kramera48e0652015-05-16 15:40:03 +00001241 for (const auto &Action : ActionList) {
1242 auto *Catch = dyn_cast<CatchHandler>(Action.get());
Andrew Kayloraa92ab02015-04-03 19:37:50 +00001243 if (!Catch)
1244 continue;
1245 // The dyn_cast to function here selects C++ catch handlers and skips
1246 // SEH catch handlers.
1247 auto *Handler = dyn_cast<Function>(Catch->getHandlerBlockOrFunc());
1248 if (!Handler)
1249 continue;
1250 // Visit all the return instructions, looking for places that return
1251 // to a location within OutlinedHandlerFn.
1252 for (BasicBlock &NestedHandlerBB : *Handler) {
1253 auto *Ret = dyn_cast<ReturnInst>(NestedHandlerBB.getTerminator());
1254 if (!Ret)
1255 continue;
1256
1257 // Handler functions must always return a block address.
1258 BlockAddress *BA = cast<BlockAddress>(Ret->getReturnValue());
1259 // The original target will have been in the main parent function,
1260 // but if it is the address of a block that has been outlined, it
1261 // should be a block that was outlined into OutlinedHandlerFn.
1262 assert(BA->getFunction() == ParentFn);
1263
Andrew Kaylora6c5b962015-05-20 23:22:24 +00001264 // Ignore targets that aren't part of an outlined handler function.
Andrew Kayloraa92ab02015-04-03 19:37:50 +00001265 if (!LPadTargetBlocks.count(BA->getBasicBlock()))
1266 continue;
1267
1268 // If the return value is the address ofF a block that we
1269 // previously outlined into the parent handler function, replace
1270 // the return instruction and add the mapped target to the list
1271 // of possible return addresses.
1272 BasicBlock *MappedBB = LPadTargetBlocks[BA->getBasicBlock()];
1273 assert(MappedBB->getParent() == OutlinedHandlerFn);
1274 BlockAddress *NewBA = BlockAddress::get(OutlinedHandlerFn, MappedBB);
1275 Ret->eraseFromParent();
1276 ReturnInst::Create(Context, NewBA, &NestedHandlerBB);
1277 ActionTargets.push_back(NewBA);
1278 }
1279 }
Andrew Kaylor7a0cec32015-04-03 21:44:17 +00001280 ActionList.clear();
Andrew Kayloraa92ab02015-04-03 19:37:50 +00001281
Andrew Kaylora6c5b962015-05-20 23:22:24 +00001282 if (Branch) {
1283 // If the landing pad was already in outlined form, just update its targets.
1284 for (unsigned int I = Branch->getNumDestinations(); I > 0; --I)
1285 Branch->removeDestination(I);
1286 // Add the previously collected action targets.
1287 for (auto *Target : ActionTargets)
1288 Branch->addDestination(Target->getBasicBlock());
1289 } else {
1290 // If the landing pad was previously stubbed out, fill in its outlined form.
1291 IntrinsicInst *NewEHActions = cast<IntrinsicInst>(EHActions->clone());
1292 OutlinedBB->getInstList().push_back(NewEHActions);
1293
1294 // Insert an indirect branch into the outlined landing pad BB.
1295 IndirectBrInst *IBr = IndirectBrInst::Create(NewEHActions, 0, OutlinedBB);
1296 // Add the previously collected action targets.
1297 for (auto *Target : ActionTargets)
1298 IBr->addDestination(Target->getBasicBlock());
1299 }
Andrew Kayloraa92ab02015-04-03 19:37:50 +00001300}
1301
Andrew Kaylor6b67d422015-03-11 23:22:06 +00001302// This function examines a block to determine whether the block ends with a
1303// conditional branch to a catch handler based on a selector comparison.
1304// This function is used both by the WinEHPrepare::findSelectorComparison() and
1305// WinEHCleanupDirector::handleTypeIdFor().
1306static bool isSelectorDispatch(BasicBlock *BB, BasicBlock *&CatchHandler,
1307 Constant *&Selector, BasicBlock *&NextBB) {
1308 ICmpInst::Predicate Pred;
1309 BasicBlock *TBB, *FBB;
1310 Value *LHS, *RHS;
1311
1312 if (!match(BB->getTerminator(),
1313 m_Br(m_ICmp(Pred, m_Value(LHS), m_Value(RHS)), TBB, FBB)))
1314 return false;
1315
1316 if (!match(LHS,
1317 m_Intrinsic<Intrinsic::eh_typeid_for>(m_Constant(Selector))) &&
1318 !match(RHS, m_Intrinsic<Intrinsic::eh_typeid_for>(m_Constant(Selector))))
1319 return false;
1320
1321 if (Pred == CmpInst::ICMP_EQ) {
1322 CatchHandler = TBB;
1323 NextBB = FBB;
1324 return true;
1325 }
1326
1327 if (Pred == CmpInst::ICMP_NE) {
1328 CatchHandler = FBB;
1329 NextBB = TBB;
1330 return true;
1331 }
1332
1333 return false;
1334}
1335
Andrew Kaylor41758512015-04-20 22:04:09 +00001336static bool isCatchBlock(BasicBlock *BB) {
1337 for (BasicBlock::iterator II = BB->getFirstNonPHIOrDbg(), IE = BB->end();
1338 II != IE; ++II) {
1339 if (match(cast<Value>(II), m_Intrinsic<Intrinsic::eh_begincatch>()))
1340 return true;
1341 }
1342 return false;
1343}
1344
David Majnemer7fddecc2015-06-17 20:52:32 +00001345static BasicBlock *createStubLandingPad(Function *Handler) {
Andrew Kaylorbb111322015-04-07 21:30:23 +00001346 // FIXME: Finish this!
1347 LLVMContext &Context = Handler->getContext();
1348 BasicBlock *StubBB = BasicBlock::Create(Context, "stub");
1349 Handler->getBasicBlockList().push_back(StubBB);
1350 IRBuilder<> Builder(StubBB);
1351 LandingPadInst *LPad = Builder.CreateLandingPad(
1352 llvm::StructType::get(Type::getInt8PtrTy(Context),
1353 Type::getInt32Ty(Context), nullptr),
David Majnemer7fddecc2015-06-17 20:52:32 +00001354 0);
Andrew Kaylor43e1d762015-04-23 00:20:44 +00001355 // Insert a call to llvm.eh.actions so that we don't try to outline this lpad.
Andrew Kaylor91307432015-04-28 22:01:51 +00001356 Function *ActionIntrin =
1357 Intrinsic::getDeclaration(Handler->getParent(), Intrinsic::eh_actions);
David Blaikieff6409d2015-05-18 22:13:54 +00001358 Builder.CreateCall(ActionIntrin, {}, "recover");
Andrew Kaylorbb111322015-04-07 21:30:23 +00001359 LPad->setCleanup(true);
1360 Builder.CreateUnreachable();
1361 return StubBB;
1362}
1363
1364// Cycles through the blocks in an outlined handler function looking for an
1365// invoke instruction and inserts an invoke of llvm.donothing with an empty
1366// landing pad if none is found. The code that generates the .xdata tables for
1367// the handler needs at least one landing pad to identify the parent function's
1368// personality.
David Majnemer7fddecc2015-06-17 20:52:32 +00001369void WinEHPrepare::addStubInvokeToHandlerIfNeeded(Function *Handler) {
Andrew Kaylorbb111322015-04-07 21:30:23 +00001370 ReturnInst *Ret = nullptr;
Andrew Kaylor5f715522015-04-23 18:37:39 +00001371 UnreachableInst *Unreached = nullptr;
Andrew Kaylorbb111322015-04-07 21:30:23 +00001372 for (BasicBlock &BB : *Handler) {
1373 TerminatorInst *Terminator = BB.getTerminator();
1374 // If we find an invoke, there is nothing to be done.
1375 auto *II = dyn_cast<InvokeInst>(Terminator);
1376 if (II)
1377 return;
1378 // If we've already recorded a return instruction, keep looking for invokes.
Andrew Kaylor5f715522015-04-23 18:37:39 +00001379 if (!Ret)
1380 Ret = dyn_cast<ReturnInst>(Terminator);
1381 // If we haven't recorded an unreachable instruction, try this terminator.
1382 if (!Unreached)
1383 Unreached = dyn_cast<UnreachableInst>(Terminator);
Andrew Kaylorbb111322015-04-07 21:30:23 +00001384 }
1385
1386 // If we got this far, the handler contains no invokes. We should have seen
Andrew Kaylor5f715522015-04-23 18:37:39 +00001387 // at least one return or unreachable instruction. We'll insert an invoke of
1388 // llvm.donothing ahead of that instruction.
1389 assert(Ret || Unreached);
1390 TerminatorInst *Term;
1391 if (Ret)
1392 Term = Ret;
1393 else
1394 Term = Unreached;
1395 BasicBlock *OldRetBB = Term->getParent();
Andrew Kaylor046f7b42015-04-28 21:54:14 +00001396 BasicBlock *NewRetBB = SplitBlock(OldRetBB, Term, DT);
Andrew Kaylorbb111322015-04-07 21:30:23 +00001397 // SplitBlock adds an unconditional branch instruction at the end of the
1398 // parent block. We want to replace that with an invoke call, so we can
1399 // erase it now.
1400 OldRetBB->getTerminator()->eraseFromParent();
David Majnemer7fddecc2015-06-17 20:52:32 +00001401 BasicBlock *StubLandingPad = createStubLandingPad(Handler);
Andrew Kaylorbb111322015-04-07 21:30:23 +00001402 Function *F =
1403 Intrinsic::getDeclaration(Handler->getParent(), Intrinsic::donothing);
1404 InvokeInst::Create(F, NewRetBB, StubLandingPad, None, "", OldRetBB);
1405}
1406
Reid Klecknerbcda1cd2015-04-29 22:49:54 +00001407// FIXME: Consider sinking this into lib/Target/X86 somehow. TargetLowering
1408// usually doesn't build LLVM IR, so that's probably the wrong place.
Reid Kleckner6511c8b2015-07-01 20:59:25 +00001409Function *WinEHPrepare::createHandlerFunc(Function *ParentFn, Type *RetTy,
1410 const Twine &Name, Module *M,
1411 Value *&ParentFP) {
Reid Klecknerbcda1cd2015-04-29 22:49:54 +00001412 // x64 uses a two-argument prototype where the parent FP is the second
1413 // argument. x86 uses no arguments, just the incoming EBP value.
1414 LLVMContext &Context = M->getContext();
Reid Kleckner6511c8b2015-07-01 20:59:25 +00001415 Type *Int8PtrType = Type::getInt8PtrTy(Context);
Reid Klecknerbcda1cd2015-04-29 22:49:54 +00001416 FunctionType *FnType;
1417 if (TheTriple.getArch() == Triple::x86_64) {
Reid Klecknerbcda1cd2015-04-29 22:49:54 +00001418 Type *ArgTys[2] = {Int8PtrType, Int8PtrType};
1419 FnType = FunctionType::get(RetTy, ArgTys, false);
1420 } else {
1421 FnType = FunctionType::get(RetTy, None, false);
1422 }
1423
1424 Function *Handler =
1425 Function::Create(FnType, GlobalVariable::InternalLinkage, Name, M);
1426 BasicBlock *Entry = BasicBlock::Create(Context, "entry");
1427 Handler->getBasicBlockList().push_front(Entry);
1428 if (TheTriple.getArch() == Triple::x86_64) {
1429 ParentFP = &(Handler->getArgumentList().back());
1430 } else {
1431 assert(M);
1432 Function *FrameAddressFn =
1433 Intrinsic::getDeclaration(M, Intrinsic::frameaddress);
Reid Kleckner6511c8b2015-07-01 20:59:25 +00001434 Function *RecoverFPFn =
1435 Intrinsic::getDeclaration(M, Intrinsic::x86_seh_recoverfp);
1436 IRBuilder<> Builder(&Handler->getEntryBlock());
1437 Value *EBP =
1438 Builder.CreateCall(FrameAddressFn, {Builder.getInt32(1)}, "ebp");
1439 Value *ParentI8Fn = Builder.CreateBitCast(ParentFn, Int8PtrType);
1440 ParentFP = Builder.CreateCall(RecoverFPFn, {ParentI8Fn, EBP});
Reid Klecknerbcda1cd2015-04-29 22:49:54 +00001441 }
1442 return Handler;
1443}
1444
Andrew Kaylor6b67d422015-03-11 23:22:06 +00001445bool WinEHPrepare::outlineHandler(ActionHandler *Action, Function *SrcFn,
1446 LandingPadInst *LPad, BasicBlock *StartBB,
Andrew Kaylorf0f5e462015-03-03 20:00:16 +00001447 FrameVarInfoMap &VarInfo) {
Andrew Kaylor1476e6d2015-02-24 20:49:35 +00001448 Module *M = SrcFn->getParent();
1449 LLVMContext &Context = M->getContext();
Reid Klecknerbcda1cd2015-04-29 22:49:54 +00001450 Type *Int8PtrType = Type::getInt8PtrTy(Context);
Andrew Kaylor1476e6d2015-02-24 20:49:35 +00001451
1452 // Create a new function to receive the handler contents.
Reid Klecknerbcda1cd2015-04-29 22:49:54 +00001453 Value *ParentFP;
Andrew Kaylorf0f5e462015-03-03 20:00:16 +00001454 Function *Handler;
Andrew Kaylor6b67d422015-03-11 23:22:06 +00001455 if (Action->getType() == Catch) {
Reid Kleckner6511c8b2015-07-01 20:59:25 +00001456 Handler = createHandlerFunc(SrcFn, Int8PtrType, SrcFn->getName() + ".catch", M,
Reid Klecknerbcda1cd2015-04-29 22:49:54 +00001457 ParentFP);
Andrew Kaylorf0f5e462015-03-03 20:00:16 +00001458 } else {
Reid Kleckner6511c8b2015-07-01 20:59:25 +00001459 Handler = createHandlerFunc(SrcFn, Type::getVoidTy(Context),
Reid Klecknerbcda1cd2015-04-29 22:49:54 +00001460 SrcFn->getName() + ".cleanup", M, ParentFP);
Andrew Kaylorf0f5e462015-03-03 20:00:16 +00001461 }
David Majnemer7fddecc2015-06-17 20:52:32 +00001462 Handler->setPersonalityFn(SrcFn->getPersonalityFn());
Reid Klecknerbcda1cd2015-04-29 22:49:54 +00001463 HandlerToParentFP[Handler] = ParentFP;
David Majnemercde33032015-03-30 22:58:10 +00001464 Handler->addFnAttr("wineh-parent", SrcFn->getName());
Reid Klecknerbcda1cd2015-04-29 22:49:54 +00001465 BasicBlock *Entry = &Handler->getEntryBlock();
David Majnemercde33032015-03-30 22:58:10 +00001466
Andrew Kaylor1476e6d2015-02-24 20:49:35 +00001467 // Generate a standard prolog to setup the frame recovery structure.
1468 IRBuilder<> Builder(Context);
Andrew Kaylor1476e6d2015-02-24 20:49:35 +00001469 Builder.SetInsertPoint(Entry);
1470 Builder.SetCurrentDebugLocation(LPad->getDebugLoc());
1471
Andrew Kaylorf0f5e462015-03-03 20:00:16 +00001472 std::unique_ptr<WinEHCloningDirectorBase> Director;
Andrew Kaylor1476e6d2015-02-24 20:49:35 +00001473
Andrew Kaylor1476e6d2015-02-24 20:49:35 +00001474 ValueToValueMapTy VMap;
1475
Andrew Kaylor6b67d422015-03-11 23:22:06 +00001476 LandingPadMap &LPadMap = LPadMaps[LPad];
1477 if (!LPadMap.isInitialized())
1478 LPadMap.mapLandingPad(LPad);
Reid Kleckner0f9e27a2015-03-18 20:26:53 +00001479 if (auto *CatchAction = dyn_cast<CatchHandler>(Action)) {
1480 Constant *Sel = CatchAction->getSelector();
Andrew Kaylora6c5b962015-05-20 23:22:24 +00001481 Director.reset(new WinEHCatchDirector(Handler, ParentFP, Sel, VarInfo,
1482 LPadMap, NestedLPtoOriginalLP, DT,
1483 EHBlocks));
Andrew Kaylor64622aa2015-04-01 17:21:25 +00001484 LPadMap.remapEHValues(VMap, UndefValue::get(Int8PtrType),
1485 ConstantInt::get(Type::getInt32Ty(Context), 1));
Andrew Kaylor6b67d422015-03-11 23:22:06 +00001486 } else {
Reid Klecknerbcda1cd2015-04-29 22:49:54 +00001487 Director.reset(
1488 new WinEHCleanupDirector(Handler, ParentFP, VarInfo, LPadMap));
Andrew Kaylor64622aa2015-04-01 17:21:25 +00001489 LPadMap.remapEHValues(VMap, UndefValue::get(Int8PtrType),
1490 UndefValue::get(Type::getInt32Ty(Context)));
Andrew Kaylor6b67d422015-03-11 23:22:06 +00001491 }
Andrew Kaylor1476e6d2015-02-24 20:49:35 +00001492
Andrew Kaylor1476e6d2015-02-24 20:49:35 +00001493 SmallVector<ReturnInst *, 8> Returns;
Andrew Kaylor6b67d422015-03-11 23:22:06 +00001494 ClonedCodeInfo OutlinedFunctionInfo;
Andrew Kaylor1476e6d2015-02-24 20:49:35 +00001495
Andrew Kaylor3170e562015-03-20 21:42:54 +00001496 // If the start block contains PHI nodes, we need to map them.
1497 BasicBlock::iterator II = StartBB->begin();
1498 while (auto *PN = dyn_cast<PHINode>(II)) {
1499 bool Mapped = false;
1500 // Look for PHI values that we have already mapped (such as the selector).
1501 for (Value *Val : PN->incoming_values()) {
1502 if (VMap.count(Val)) {
1503 VMap[PN] = VMap[Val];
1504 Mapped = true;
1505 }
1506 }
1507 // If we didn't find a match for this value, map it as an undef.
1508 if (!Mapped) {
1509 VMap[PN] = UndefValue::get(PN->getType());
1510 }
1511 ++II;
1512 }
1513
Andrew Kaylor00e5d9e2015-04-20 22:53:42 +00001514 // The landing pad value may be used by PHI nodes. It will ultimately be
1515 // eliminated, but we need it in the map for intermediate handling.
1516 VMap[LPad] = UndefValue::get(LPad->getType());
1517
Andrew Kaylor6b67d422015-03-11 23:22:06 +00001518 // Skip over PHIs and, if applicable, landingpad instructions.
Andrew Kaylor3170e562015-03-20 21:42:54 +00001519 II = StartBB->getFirstInsertionPt();
Andrew Kaylor1476e6d2015-02-24 20:49:35 +00001520
Andrew Kaylor6b67d422015-03-11 23:22:06 +00001521 CloneAndPruneIntoFromInst(Handler, SrcFn, II, VMap,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001522 /*ModuleLevelChanges=*/false, Returns, "",
Andrew Kaylor6b67d422015-03-11 23:22:06 +00001523 &OutlinedFunctionInfo, Director.get());
Andrew Kaylor1476e6d2015-02-24 20:49:35 +00001524
Andrew Kaylor5dacfd82015-04-24 23:10:38 +00001525 // Move all the instructions in the cloned "entry" block into our entry block.
1526 // Depending on how the parent function was laid out, the block that will
1527 // correspond to the outlined entry block may not be the first block in the
1528 // list. We can recognize it, however, as the cloned block which has no
1529 // predecessors. Any other block wouldn't have been cloned if it didn't
1530 // have a predecessor which was also cloned.
Andrew Kaylor91307432015-04-28 22:01:51 +00001531 Function::iterator ClonedIt = std::next(Function::iterator(Entry));
Andrew Kaylor5dacfd82015-04-24 23:10:38 +00001532 while (!pred_empty(ClonedIt))
1533 ++ClonedIt;
1534 BasicBlock *ClonedEntryBB = ClonedIt;
1535 assert(ClonedEntryBB);
1536 Entry->getInstList().splice(Entry->end(), ClonedEntryBB->getInstList());
1537 ClonedEntryBB->eraseFromParent();
Andrew Kaylor1476e6d2015-02-24 20:49:35 +00001538
Andrew Kaylorbb111322015-04-07 21:30:23 +00001539 // Make sure we can identify the handler's personality later.
David Majnemer7fddecc2015-06-17 20:52:32 +00001540 addStubInvokeToHandlerIfNeeded(Handler);
Andrew Kaylorbb111322015-04-07 21:30:23 +00001541
Andrew Kaylor6b67d422015-03-11 23:22:06 +00001542 if (auto *CatchAction = dyn_cast<CatchHandler>(Action)) {
1543 WinEHCatchDirector *CatchDirector =
1544 reinterpret_cast<WinEHCatchDirector *>(Director.get());
1545 CatchAction->setExceptionVar(CatchDirector->getExceptionVar());
1546 CatchAction->setReturnTargets(CatchDirector->getReturnTargets());
Andrew Kayloraa92ab02015-04-03 19:37:50 +00001547
1548 // Look for blocks that are not part of the landing pad that we just
1549 // outlined but terminate with a call to llvm.eh.endcatch and a
1550 // branch to a block that is in the handler we just outlined.
1551 // These blocks will be part of a nested landing pad that intends to
1552 // return to an address in this handler. This case is best handled
1553 // after both landing pads have been outlined, so for now we'll just
1554 // save the association of the blocks in LPadTargetBlocks. The
1555 // return instructions which are created from these branches will be
1556 // replaced after all landing pads have been outlined.
Richard Trieu6b1aa5f2015-04-15 01:21:15 +00001557 for (const auto MapEntry : VMap) {
Andrew Kayloraa92ab02015-04-03 19:37:50 +00001558 // VMap maps all values and blocks that were just cloned, but dead
1559 // blocks which were pruned will map to nullptr.
1560 if (!isa<BasicBlock>(MapEntry.first) || MapEntry.second == nullptr)
1561 continue;
1562 const BasicBlock *MappedBB = cast<BasicBlock>(MapEntry.first);
1563 for (auto *Pred : predecessors(const_cast<BasicBlock *>(MappedBB))) {
1564 auto *Branch = dyn_cast<BranchInst>(Pred->getTerminator());
1565 if (!Branch || !Branch->isUnconditional() || Pred->size() <= 1)
1566 continue;
1567 BasicBlock::iterator II = const_cast<BranchInst *>(Branch);
1568 --II;
1569 if (match(cast<Value>(II), m_Intrinsic<Intrinsic::eh_endcatch>())) {
1570 // This would indicate that a nested landing pad wants to return
1571 // to a block that is outlined into two different handlers.
1572 assert(!LPadTargetBlocks.count(MappedBB));
1573 LPadTargetBlocks[MappedBB] = cast<BasicBlock>(MapEntry.second);
1574 }
1575 }
1576 }
1577 } // End if (CatchAction)
Andrew Kaylor6b67d422015-03-11 23:22:06 +00001578
Reid Kleckner0f9e27a2015-03-18 20:26:53 +00001579 Action->setHandlerBlockOrFunc(Handler);
Andrew Kaylor6b67d422015-03-11 23:22:06 +00001580
Andrew Kaylor1476e6d2015-02-24 20:49:35 +00001581 return true;
1582}
1583
Reid Kleckner0f9e27a2015-03-18 20:26:53 +00001584/// This BB must end in a selector dispatch. All we need to do is pass the
1585/// handler block to llvm.eh.actions and list it as a possible indirectbr
1586/// target.
1587void WinEHPrepare::processSEHCatchHandler(CatchHandler *CatchAction,
1588 BasicBlock *StartBB) {
1589 BasicBlock *HandlerBB;
1590 BasicBlock *NextBB;
1591 Constant *Selector;
1592 bool Res = isSelectorDispatch(StartBB, HandlerBB, Selector, NextBB);
1593 if (Res) {
1594 // If this was EH dispatch, this must be a conditional branch to the handler
1595 // block.
1596 // FIXME: Handle instructions in the dispatch block. Currently we drop them,
1597 // leading to crashes if some optimization hoists stuff here.
1598 assert(CatchAction->getSelector() && HandlerBB &&
1599 "expected catch EH dispatch");
1600 } else {
1601 // This must be a catch-all. Split the block after the landingpad.
1602 assert(CatchAction->getSelector()->isNullValue() && "expected catch-all");
Andrew Kaylor046f7b42015-04-28 21:54:14 +00001603 HandlerBB = SplitBlock(StartBB, StartBB->getFirstInsertionPt(), DT);
Reid Kleckner0f9e27a2015-03-18 20:26:53 +00001604 }
Reid Klecknercfbfe6f2015-04-24 20:25:05 +00001605 IRBuilder<> Builder(HandlerBB->getFirstInsertionPt());
1606 Function *EHCodeFn = Intrinsic::getDeclaration(
1607 StartBB->getParent()->getParent(), Intrinsic::eh_exceptioncode);
David Blaikieff6409d2015-05-18 22:13:54 +00001608 Value *Code = Builder.CreateCall(EHCodeFn, {}, "sehcode");
Reid Klecknercfbfe6f2015-04-24 20:25:05 +00001609 Code = Builder.CreateIntToPtr(Code, SEHExceptionCodeSlot->getAllocatedType());
1610 Builder.CreateStore(Code, SEHExceptionCodeSlot);
Reid Kleckner0f9e27a2015-03-18 20:26:53 +00001611 CatchAction->setHandlerBlockOrFunc(BlockAddress::get(HandlerBB));
1612 TinyPtrVector<BasicBlock *> Targets(HandlerBB);
1613 CatchAction->setReturnTargets(Targets);
1614}
1615
Andrew Kaylor6b67d422015-03-11 23:22:06 +00001616void LandingPadMap::mapLandingPad(const LandingPadInst *LPad) {
1617 // Each instance of this class should only ever be used to map a single
1618 // landing pad.
1619 assert(OriginLPad == nullptr || OriginLPad == LPad);
1620
1621 // If the landing pad has already been mapped, there's nothing more to do.
1622 if (OriginLPad == LPad)
1623 return;
1624
1625 OriginLPad = LPad;
1626
1627 // The landingpad instruction returns an aggregate value. Typically, its
1628 // value will be passed to a pair of extract value instructions and the
Andrew Kaylor64622aa2015-04-01 17:21:25 +00001629 // results of those extracts will have been promoted to reg values before
1630 // this routine is called.
Andrew Kaylor6b67d422015-03-11 23:22:06 +00001631 for (auto *U : LPad->users()) {
1632 const ExtractValueInst *Extract = dyn_cast<ExtractValueInst>(U);
1633 if (!Extract)
1634 continue;
1635 assert(Extract->getNumIndices() == 1 &&
1636 "Unexpected operation: extracting both landing pad values");
1637 unsigned int Idx = *(Extract->idx_begin());
1638 assert((Idx == 0 || Idx == 1) &&
1639 "Unexpected operation: extracting an unknown landing pad element");
1640 if (Idx == 0) {
Andrew Kaylor6b67d422015-03-11 23:22:06 +00001641 ExtractedEHPtrs.push_back(Extract);
1642 } else if (Idx == 1) {
Andrew Kaylor6b67d422015-03-11 23:22:06 +00001643 ExtractedSelectors.push_back(Extract);
1644 }
Andrew Kaylor6b67d422015-03-11 23:22:06 +00001645 }
1646}
1647
Andrew Kaylorf7118ae2015-03-27 22:31:12 +00001648bool LandingPadMap::isOriginLandingPadBlock(const BasicBlock *BB) const {
1649 return BB->getLandingPadInst() == OriginLPad;
1650}
1651
Andrew Kaylor6b67d422015-03-11 23:22:06 +00001652bool LandingPadMap::isLandingPadSpecificInst(const Instruction *Inst) const {
1653 if (Inst == OriginLPad)
1654 return true;
1655 for (auto *Extract : ExtractedEHPtrs) {
1656 if (Inst == Extract)
1657 return true;
1658 }
1659 for (auto *Extract : ExtractedSelectors) {
1660 if (Inst == Extract)
1661 return true;
1662 }
Andrew Kaylor6b67d422015-03-11 23:22:06 +00001663 return false;
1664}
1665
Andrew Kaylor64622aa2015-04-01 17:21:25 +00001666void LandingPadMap::remapEHValues(ValueToValueMapTy &VMap, Value *EHPtrValue,
1667 Value *SelectorValue) const {
1668 // Remap all landing pad extract instructions to the specified values.
1669 for (auto *Extract : ExtractedEHPtrs)
1670 VMap[Extract] = EHPtrValue;
Andrew Kaylor6b67d422015-03-11 23:22:06 +00001671 for (auto *Extract : ExtractedSelectors)
Andrew Kaylor64622aa2015-04-01 17:21:25 +00001672 VMap[Extract] = SelectorValue;
Andrew Kaylor6b67d422015-03-11 23:22:06 +00001673}
1674
Reid Klecknerd5afc62f2015-07-07 23:23:03 +00001675static bool isLocalAddressCall(const Value *V) {
1676 return match(const_cast<Value *>(V), m_Intrinsic<Intrinsic::localaddress>());
Reid Klecknerf14787d2015-04-22 00:07:52 +00001677}
1678
Andrew Kaylorf0f5e462015-03-03 20:00:16 +00001679CloningDirector::CloningAction WinEHCloningDirectorBase::handleInstruction(
Andrew Kaylor1476e6d2015-02-24 20:49:35 +00001680 ValueToValueMapTy &VMap, const Instruction *Inst, BasicBlock *NewBB) {
Andrew Kaylor6b67d422015-03-11 23:22:06 +00001681 // If this is one of the boilerplate landing pad instructions, skip it.
1682 // The instruction will have already been remapped in VMap.
1683 if (LPadMap.isLandingPadSpecificInst(Inst))
1684 return CloningDirector::SkipInstruction;
Andrew Kaylor1476e6d2015-02-24 20:49:35 +00001685
Andrew Kaylora6c5b962015-05-20 23:22:24 +00001686 // Nested landing pads that have not already been outlined will be cloned as
1687 // stubs, with just the landingpad instruction and an unreachable instruction.
1688 // When all landingpads have been outlined, we'll replace this with the
1689 // llvm.eh.actions call and indirect branch created when the landing pad was
1690 // outlined.
Andrew Kayloraa92ab02015-04-03 19:37:50 +00001691 if (auto *LPad = dyn_cast<LandingPadInst>(Inst)) {
1692 return handleLandingPad(VMap, LPad, NewBB);
Andrew Kaylor6b67d422015-03-11 23:22:06 +00001693 }
1694
Andrew Kaylora6c5b962015-05-20 23:22:24 +00001695 // Nested landing pads that have already been outlined will be cloned in their
1696 // outlined form, but we need to intercept the ibr instruction to filter out
1697 // targets that do not return to the handler we are outlining.
1698 if (auto *IBr = dyn_cast<IndirectBrInst>(Inst)) {
1699 return handleIndirectBr(VMap, IBr, NewBB);
1700 }
1701
Andrew Kaylor6b67d422015-03-11 23:22:06 +00001702 if (auto *Invoke = dyn_cast<InvokeInst>(Inst))
1703 return handleInvoke(VMap, Invoke, NewBB);
1704
Andrew Kaylorf0f5e462015-03-03 20:00:16 +00001705 if (auto *Resume = dyn_cast<ResumeInst>(Inst))
1706 return handleResume(VMap, Resume, NewBB);
Andrew Kaylor1476e6d2015-02-24 20:49:35 +00001707
Andrew Kaylorea8df612015-04-17 23:05:43 +00001708 if (auto *Cmp = dyn_cast<CmpInst>(Inst))
1709 return handleCompare(VMap, Cmp, NewBB);
1710
Andrew Kaylorf0f5e462015-03-03 20:00:16 +00001711 if (match(Inst, m_Intrinsic<Intrinsic::eh_begincatch>()))
1712 return handleBeginCatch(VMap, Inst, NewBB);
1713 if (match(Inst, m_Intrinsic<Intrinsic::eh_endcatch>()))
1714 return handleEndCatch(VMap, Inst, NewBB);
1715 if (match(Inst, m_Intrinsic<Intrinsic::eh_typeid_for>()))
1716 return handleTypeIdFor(VMap, Inst, NewBB);
Andrew Kaylor1476e6d2015-02-24 20:49:35 +00001717
Reid Klecknerd5afc62f2015-07-07 23:23:03 +00001718 // When outlining llvm.localaddress(), remap that to the second argument,
Reid Klecknerf14787d2015-04-22 00:07:52 +00001719 // which is the FP of the parent.
Reid Klecknerd5afc62f2015-07-07 23:23:03 +00001720 if (isLocalAddressCall(Inst)) {
Reid Klecknerbcda1cd2015-04-29 22:49:54 +00001721 VMap[Inst] = ParentFP;
Reid Klecknerf14787d2015-04-22 00:07:52 +00001722 return CloningDirector::SkipInstruction;
1723 }
1724
Andrew Kaylor1476e6d2015-02-24 20:49:35 +00001725 // Continue with the default cloning behavior.
1726 return CloningDirector::CloneInstruction;
1727}
1728
Andrew Kayloraa92ab02015-04-03 19:37:50 +00001729CloningDirector::CloningAction WinEHCatchDirector::handleLandingPad(
1730 ValueToValueMapTy &VMap, const LandingPadInst *LPad, BasicBlock *NewBB) {
Andrew Kaylora6c5b962015-05-20 23:22:24 +00001731 // If the instruction after the landing pad is a call to llvm.eh.actions
1732 // the landing pad has already been outlined. In this case, we should
1733 // clone it because it may return to a block in the handler we are
1734 // outlining now that would otherwise be unreachable. The landing pads
1735 // are sorted before outlining begins to enable this case to work
1736 // properly.
1737 const Instruction *NextI = LPad->getNextNode();
1738 if (match(NextI, m_Intrinsic<Intrinsic::eh_actions>()))
1739 return CloningDirector::CloneInstruction;
1740
1741 // If the landing pad hasn't been outlined yet, the landing pad we are
1742 // outlining now does not dominate it and so it cannot return to a block
1743 // in this handler. In that case, we can just insert a stub landing
1744 // pad now and patch it up later.
Andrew Kayloraa92ab02015-04-03 19:37:50 +00001745 Instruction *NewInst = LPad->clone();
1746 if (LPad->hasName())
1747 NewInst->setName(LPad->getName());
1748 // Save this correlation for later processing.
1749 NestedLPtoOriginalLP[cast<LandingPadInst>(NewInst)] = LPad;
1750 VMap[LPad] = NewInst;
1751 BasicBlock::InstListType &InstList = NewBB->getInstList();
1752 InstList.push_back(NewInst);
1753 InstList.push_back(new UnreachableInst(NewBB->getContext()));
1754 return CloningDirector::StopCloningBB;
1755}
1756
Andrew Kaylorf0f5e462015-03-03 20:00:16 +00001757CloningDirector::CloningAction WinEHCatchDirector::handleBeginCatch(
1758 ValueToValueMapTy &VMap, const Instruction *Inst, BasicBlock *NewBB) {
1759 // The argument to the call is some form of the first element of the
1760 // landingpad aggregate value, but that doesn't matter. It isn't used
1761 // here.
Reid Kleckner42366532015-03-03 23:20:30 +00001762 // The second argument is an outparameter where the exception object will be
1763 // stored. Typically the exception object is a scalar, but it can be an
1764 // aggregate when catching by value.
1765 // FIXME: Leave something behind to indicate where the exception object lives
1766 // for this handler. Should it be part of llvm.eh.actions?
Andrew Kaylor6b67d422015-03-11 23:22:06 +00001767 assert(ExceptionObjectVar == nullptr && "Multiple calls to "
1768 "llvm.eh.begincatch found while "
1769 "outlining catch handler.");
1770 ExceptionObjectVar = Inst->getOperand(1)->stripPointerCasts();
Reid Kleckner3567d272015-04-02 21:13:31 +00001771 if (isa<ConstantPointerNull>(ExceptionObjectVar))
1772 return CloningDirector::SkipInstruction;
Reid Kleckneraab30e12015-04-03 18:18:06 +00001773 assert(cast<AllocaInst>(ExceptionObjectVar)->isStaticAlloca() &&
1774 "catch parameter is not static alloca");
Reid Kleckner3567d272015-04-02 21:13:31 +00001775 Materializer.escapeCatchObject(ExceptionObjectVar);
Andrew Kaylorf0f5e462015-03-03 20:00:16 +00001776 return CloningDirector::SkipInstruction;
1777}
1778
1779CloningDirector::CloningAction
1780WinEHCatchDirector::handleEndCatch(ValueToValueMapTy &VMap,
1781 const Instruction *Inst, BasicBlock *NewBB) {
1782 auto *IntrinCall = dyn_cast<IntrinsicInst>(Inst);
1783 // It might be interesting to track whether or not we are inside a catch
1784 // function, but that might make the algorithm more brittle than it needs
1785 // to be.
1786
1787 // The end catch call can occur in one of two places: either in a
Andrew Kaylor6b67d422015-03-11 23:22:06 +00001788 // landingpad block that is part of the catch handlers exception mechanism,
Andrew Kaylorf7118ae2015-03-27 22:31:12 +00001789 // or at the end of the catch block. However, a catch-all handler may call
1790 // end catch from the original landing pad. If the call occurs in a nested
1791 // landing pad block, we must skip it and continue so that the landing pad
1792 // gets cloned.
1793 auto *ParentBB = IntrinCall->getParent();
1794 if (ParentBB->isLandingPad() && !LPadMap.isOriginLandingPadBlock(ParentBB))
Andrew Kaylorf0f5e462015-03-03 20:00:16 +00001795 return CloningDirector::SkipInstruction;
1796
Andrew Kaylor64622aa2015-04-01 17:21:25 +00001797 // If an end catch occurs anywhere else we want to terminate the handler
1798 // with a return to the code that follows the endcatch call. If the
1799 // next instruction is not an unconditional branch, we need to split the
1800 // block to provide a clear target for the return instruction.
1801 BasicBlock *ContinueBB;
1802 auto Next = std::next(BasicBlock::const_iterator(IntrinCall));
1803 const BranchInst *Branch = dyn_cast<BranchInst>(Next);
1804 if (!Branch || !Branch->isUnconditional()) {
1805 // We're interrupting the cloning process at this location, so the
1806 // const_cast we're doing here will not cause a problem.
1807 ContinueBB = SplitBlock(const_cast<BasicBlock *>(ParentBB),
1808 const_cast<Instruction *>(cast<Instruction>(Next)));
1809 } else {
1810 ContinueBB = Branch->getSuccessor(0);
1811 }
Andrew Kaylorf0f5e462015-03-03 20:00:16 +00001812
Andrew Kaylor64622aa2015-04-01 17:21:25 +00001813 ReturnInst::Create(NewBB->getContext(), BlockAddress::get(ContinueBB), NewBB);
1814 ReturnTargets.push_back(ContinueBB);
Andrew Kaylorf0f5e462015-03-03 20:00:16 +00001815
1816 // We just added a terminator to the cloned block.
1817 // Tell the caller to stop processing the current basic block so that
1818 // the branch instruction will be skipped.
1819 return CloningDirector::StopCloningBB;
1820}
1821
1822CloningDirector::CloningAction WinEHCatchDirector::handleTypeIdFor(
1823 ValueToValueMapTy &VMap, const Instruction *Inst, BasicBlock *NewBB) {
1824 auto *IntrinCall = dyn_cast<IntrinsicInst>(Inst);
1825 Value *Selector = IntrinCall->getArgOperand(0)->stripPointerCasts();
1826 // This causes a replacement that will collapse the landing pad CFG based
1827 // on the filter function we intend to match.
1828 if (Selector == CurrentSelector)
1829 VMap[Inst] = ConstantInt::get(SelectorIDType, 1);
1830 else
1831 VMap[Inst] = ConstantInt::get(SelectorIDType, 0);
1832 // Tell the caller not to clone this instruction.
1833 return CloningDirector::SkipInstruction;
1834}
1835
Andrew Kaylora6c5b962015-05-20 23:22:24 +00001836CloningDirector::CloningAction WinEHCatchDirector::handleIndirectBr(
1837 ValueToValueMapTy &VMap,
1838 const IndirectBrInst *IBr,
1839 BasicBlock *NewBB) {
1840 // If this indirect branch is not part of a landing pad block, just clone it.
1841 const BasicBlock *ParentBB = IBr->getParent();
1842 if (!ParentBB->isLandingPad())
1843 return CloningDirector::CloneInstruction;
1844
1845 // If it is part of a landing pad, we want to filter out target blocks
1846 // that are not part of the handler we are outlining.
1847 const LandingPadInst *LPad = ParentBB->getLandingPadInst();
1848
1849 // Save this correlation for later processing.
1850 NestedLPtoOriginalLP[cast<LandingPadInst>(VMap[LPad])] = LPad;
1851
1852 // We should only get here for landing pads that have already been outlined.
1853 assert(match(LPad->getNextNode(), m_Intrinsic<Intrinsic::eh_actions>()));
1854
1855 // Copy the indirectbr, but only include targets that were previously
1856 // identified as EH blocks and are dominated by the nested landing pad.
1857 SetVector<const BasicBlock *> ReturnTargets;
1858 for (int I = 0, E = IBr->getNumDestinations(); I < E; ++I) {
1859 auto *TargetBB = IBr->getDestination(I);
1860 if (EHBlocks.count(const_cast<BasicBlock*>(TargetBB)) &&
1861 DT->dominates(ParentBB, TargetBB)) {
1862 DEBUG(dbgs() << " Adding destination " << TargetBB->getName() << "\n");
1863 ReturnTargets.insert(TargetBB);
1864 }
1865 }
1866 IndirectBrInst *NewBranch =
1867 IndirectBrInst::Create(const_cast<Value *>(IBr->getAddress()),
1868 ReturnTargets.size(), NewBB);
1869 for (auto *Target : ReturnTargets)
1870 NewBranch->addDestination(const_cast<BasicBlock*>(Target));
1871
1872 // The operands and targets of the branch instruction are remapped later
1873 // because it is a terminator. Tell the cloning code to clone the
1874 // blocks we just added to the target list.
1875 return CloningDirector::CloneSuccessors;
1876}
1877
Andrew Kaylorf0f5e462015-03-03 20:00:16 +00001878CloningDirector::CloningAction
Andrew Kaylor6b67d422015-03-11 23:22:06 +00001879WinEHCatchDirector::handleInvoke(ValueToValueMapTy &VMap,
1880 const InvokeInst *Invoke, BasicBlock *NewBB) {
1881 return CloningDirector::CloneInstruction;
1882}
1883
1884CloningDirector::CloningAction
Andrew Kaylorf0f5e462015-03-03 20:00:16 +00001885WinEHCatchDirector::handleResume(ValueToValueMapTy &VMap,
1886 const ResumeInst *Resume, BasicBlock *NewBB) {
1887 // Resume instructions shouldn't be reachable from catch handlers.
1888 // We still need to handle it, but it will be pruned.
1889 BasicBlock::InstListType &InstList = NewBB->getInstList();
1890 InstList.push_back(new UnreachableInst(NewBB->getContext()));
1891 return CloningDirector::StopCloningBB;
1892}
1893
Andrew Kaylorea8df612015-04-17 23:05:43 +00001894CloningDirector::CloningAction
1895WinEHCatchDirector::handleCompare(ValueToValueMapTy &VMap,
1896 const CmpInst *Compare, BasicBlock *NewBB) {
1897 const IntrinsicInst *IntrinCall = nullptr;
1898 if (match(Compare->getOperand(0), m_Intrinsic<Intrinsic::eh_typeid_for>())) {
1899 IntrinCall = dyn_cast<IntrinsicInst>(Compare->getOperand(0));
Andrew Kaylor91307432015-04-28 22:01:51 +00001900 } else if (match(Compare->getOperand(1),
1901 m_Intrinsic<Intrinsic::eh_typeid_for>())) {
Andrew Kaylorea8df612015-04-17 23:05:43 +00001902 IntrinCall = dyn_cast<IntrinsicInst>(Compare->getOperand(1));
1903 }
1904 if (IntrinCall) {
1905 Value *Selector = IntrinCall->getArgOperand(0)->stripPointerCasts();
1906 // This causes a replacement that will collapse the landing pad CFG based
1907 // on the filter function we intend to match.
1908 if (Selector == CurrentSelector->stripPointerCasts()) {
1909 VMap[Compare] = ConstantInt::get(SelectorIDType, 1);
Andrew Kaylor91307432015-04-28 22:01:51 +00001910 } else {
Andrew Kaylorea8df612015-04-17 23:05:43 +00001911 VMap[Compare] = ConstantInt::get(SelectorIDType, 0);
1912 }
1913 return CloningDirector::SkipInstruction;
1914 }
1915 return CloningDirector::CloneInstruction;
1916}
1917
Andrew Kayloraa92ab02015-04-03 19:37:50 +00001918CloningDirector::CloningAction WinEHCleanupDirector::handleLandingPad(
1919 ValueToValueMapTy &VMap, const LandingPadInst *LPad, BasicBlock *NewBB) {
1920 // The MS runtime will terminate the process if an exception occurs in a
1921 // cleanup handler, so we shouldn't encounter landing pads in the actual
1922 // cleanup code, but they may appear in catch blocks. Depending on where
1923 // we started cloning we may see one, but it will get dropped during dead
1924 // block pruning.
1925 Instruction *NewInst = new UnreachableInst(NewBB->getContext());
1926 VMap[LPad] = NewInst;
1927 BasicBlock::InstListType &InstList = NewBB->getInstList();
1928 InstList.push_back(NewInst);
1929 return CloningDirector::StopCloningBB;
1930}
1931
Andrew Kaylorf0f5e462015-03-03 20:00:16 +00001932CloningDirector::CloningAction WinEHCleanupDirector::handleBeginCatch(
1933 ValueToValueMapTy &VMap, const Instruction *Inst, BasicBlock *NewBB) {
Andrew Kaylorea8df612015-04-17 23:05:43 +00001934 // Cleanup code may flow into catch blocks or the catch block may be part
1935 // of a branch that will be optimized away. We'll insert a return
1936 // instruction now, but it may be pruned before the cloning process is
1937 // complete.
1938 ReturnInst::Create(NewBB->getContext(), nullptr, NewBB);
Andrew Kaylorf0f5e462015-03-03 20:00:16 +00001939 return CloningDirector::StopCloningBB;
1940}
1941
1942CloningDirector::CloningAction WinEHCleanupDirector::handleEndCatch(
1943 ValueToValueMapTy &VMap, const Instruction *Inst, BasicBlock *NewBB) {
Andrew Kaylorbb111322015-04-07 21:30:23 +00001944 // Cleanup handlers nested within catch handlers may begin with a call to
1945 // eh.endcatch. We can just ignore that instruction.
1946 return CloningDirector::SkipInstruction;
Andrew Kaylorf0f5e462015-03-03 20:00:16 +00001947}
1948
1949CloningDirector::CloningAction WinEHCleanupDirector::handleTypeIdFor(
1950 ValueToValueMapTy &VMap, const Instruction *Inst, BasicBlock *NewBB) {
Andrew Kaylor6b67d422015-03-11 23:22:06 +00001951 // If we encounter a selector comparison while cloning a cleanup handler,
1952 // we want to stop cloning immediately. Anything after the dispatch
1953 // will be outlined into a different handler.
1954 BasicBlock *CatchHandler;
1955 Constant *Selector;
1956 BasicBlock *NextBB;
1957 if (isSelectorDispatch(const_cast<BasicBlock *>(Inst->getParent()),
1958 CatchHandler, Selector, NextBB)) {
1959 ReturnInst::Create(NewBB->getContext(), nullptr, NewBB);
1960 return CloningDirector::StopCloningBB;
1961 }
1962 // If eg.typeid.for is called for any other reason, it can be ignored.
Andrew Kaylorf0f5e462015-03-03 20:00:16 +00001963 VMap[Inst] = ConstantInt::get(SelectorIDType, 0);
Andrew Kaylorf0f5e462015-03-03 20:00:16 +00001964 return CloningDirector::SkipInstruction;
1965}
1966
Andrew Kaylora6c5b962015-05-20 23:22:24 +00001967CloningDirector::CloningAction WinEHCleanupDirector::handleIndirectBr(
1968 ValueToValueMapTy &VMap,
1969 const IndirectBrInst *IBr,
1970 BasicBlock *NewBB) {
1971 // No special handling is required for cleanup cloning.
1972 return CloningDirector::CloneInstruction;
1973}
1974
Andrew Kaylor6b67d422015-03-11 23:22:06 +00001975CloningDirector::CloningAction WinEHCleanupDirector::handleInvoke(
1976 ValueToValueMapTy &VMap, const InvokeInst *Invoke, BasicBlock *NewBB) {
1977 // All invokes in cleanup handlers can be replaced with calls.
1978 SmallVector<Value *, 16> CallArgs(Invoke->op_begin(), Invoke->op_end() - 3);
1979 // Insert a normal call instruction...
1980 CallInst *NewCall =
1981 CallInst::Create(const_cast<Value *>(Invoke->getCalledValue()), CallArgs,
1982 Invoke->getName(), NewBB);
1983 NewCall->setCallingConv(Invoke->getCallingConv());
1984 NewCall->setAttributes(Invoke->getAttributes());
1985 NewCall->setDebugLoc(Invoke->getDebugLoc());
1986 VMap[Invoke] = NewCall;
1987
Reid Kleckner6e48a822015-04-10 16:26:42 +00001988 // Remap the operands.
1989 llvm::RemapInstruction(NewCall, VMap, RF_None, nullptr, &Materializer);
1990
Andrew Kaylor6b67d422015-03-11 23:22:06 +00001991 // Insert an unconditional branch to the normal destination.
1992 BranchInst::Create(Invoke->getNormalDest(), NewBB);
1993
1994 // The unwind destination won't be cloned into the new function, so
1995 // we don't need to clean up its phi nodes.
1996
1997 // We just added a terminator to the cloned block.
1998 // Tell the caller to stop processing the current basic block.
Reid Kleckner6e48a822015-04-10 16:26:42 +00001999 return CloningDirector::CloneSuccessors;
Andrew Kaylor6b67d422015-03-11 23:22:06 +00002000}
2001
Andrew Kaylorf0f5e462015-03-03 20:00:16 +00002002CloningDirector::CloningAction WinEHCleanupDirector::handleResume(
2003 ValueToValueMapTy &VMap, const ResumeInst *Resume, BasicBlock *NewBB) {
2004 ReturnInst::Create(NewBB->getContext(), nullptr, NewBB);
2005
2006 // We just added a terminator to the cloned block.
2007 // Tell the caller to stop processing the current basic block so that
2008 // the branch instruction will be skipped.
2009 return CloningDirector::StopCloningBB;
2010}
2011
Andrew Kaylorea8df612015-04-17 23:05:43 +00002012CloningDirector::CloningAction
2013WinEHCleanupDirector::handleCompare(ValueToValueMapTy &VMap,
2014 const CmpInst *Compare, BasicBlock *NewBB) {
Andrew Kaylorea8df612015-04-17 23:05:43 +00002015 if (match(Compare->getOperand(0), m_Intrinsic<Intrinsic::eh_typeid_for>()) ||
2016 match(Compare->getOperand(1), m_Intrinsic<Intrinsic::eh_typeid_for>())) {
2017 VMap[Compare] = ConstantInt::get(SelectorIDType, 1);
2018 return CloningDirector::SkipInstruction;
2019 }
2020 return CloningDirector::CloneInstruction;
Andrew Kaylorea8df612015-04-17 23:05:43 +00002021}
2022
Andrew Kaylor1476e6d2015-02-24 20:49:35 +00002023WinEHFrameVariableMaterializer::WinEHFrameVariableMaterializer(
Reid Klecknerbcda1cd2015-04-29 22:49:54 +00002024 Function *OutlinedFn, Value *ParentFP, FrameVarInfoMap &FrameVarInfo)
Andrew Kaylor1476e6d2015-02-24 20:49:35 +00002025 : FrameVarInfo(FrameVarInfo), Builder(OutlinedFn->getContext()) {
Andrew Kayloraa92ab02015-04-03 19:37:50 +00002026 BasicBlock *EntryBB = &OutlinedFn->getEntryBlock();
Reid Klecknerbcda1cd2015-04-29 22:49:54 +00002027
2028 // New allocas should be inserted in the entry block, but after the parent FP
2029 // is established if it is an instruction.
2030 Instruction *InsertPoint = EntryBB->getFirstInsertionPt();
2031 if (auto *FPInst = dyn_cast<Instruction>(ParentFP))
2032 InsertPoint = FPInst->getNextNode();
2033 Builder.SetInsertPoint(EntryBB, InsertPoint);
Andrew Kaylor1476e6d2015-02-24 20:49:35 +00002034}
2035
2036Value *WinEHFrameVariableMaterializer::materializeValueFor(Value *V) {
Reid Klecknerfd7df282015-04-22 21:05:21 +00002037 // If we're asked to materialize a static alloca, we temporarily create an
2038 // alloca in the outlined function and add this to the FrameVarInfo map. When
2039 // all the outlining is complete, we'll replace these temporary allocas with
Reid Kleckner60381792015-07-07 22:25:32 +00002040 // calls to llvm.localrecover.
Andrew Kaylor1476e6d2015-02-24 20:49:35 +00002041 if (auto *AV = dyn_cast<AllocaInst>(V)) {
Reid Klecknerfd7df282015-04-22 21:05:21 +00002042 assert(AV->isStaticAlloca() &&
2043 "cannot materialize un-demoted dynamic alloca");
Andrew Kaylor72029c62015-03-03 00:41:03 +00002044 AllocaInst *NewAlloca = dyn_cast<AllocaInst>(AV->clone());
2045 Builder.Insert(NewAlloca, AV->getName());
Reid Klecknercfb9ce52015-03-05 18:26:34 +00002046 FrameVarInfo[AV].push_back(NewAlloca);
Andrew Kaylor1476e6d2015-02-24 20:49:35 +00002047 return NewAlloca;
2048 }
2049
Andrew Kaylor72029c62015-03-03 00:41:03 +00002050 if (isa<Instruction>(V) || isa<Argument>(V)) {
Reid Klecknerd1b38c42015-05-06 18:45:24 +00002051 Function *Parent = isa<Instruction>(V)
2052 ? cast<Instruction>(V)->getParent()->getParent()
2053 : cast<Argument>(V)->getParent();
2054 errs()
2055 << "Failed to demote instruction used in exception handler of function "
2056 << GlobalValue::getRealLinkageName(Parent->getName()) << ":\n";
Reid Klecknerfd7df282015-04-22 21:05:21 +00002057 errs() << " " << *V << '\n';
2058 report_fatal_error("WinEHPrepare failed to demote instruction");
Andrew Kaylor1476e6d2015-02-24 20:49:35 +00002059 }
Andrew Kaylor1476e6d2015-02-24 20:49:35 +00002060
Andrew Kaylor72029c62015-03-03 00:41:03 +00002061 // Don't materialize other values.
Andrew Kaylor1476e6d2015-02-24 20:49:35 +00002062 return nullptr;
2063}
Andrew Kaylor6b67d422015-03-11 23:22:06 +00002064
Reid Kleckner3567d272015-04-02 21:13:31 +00002065void WinEHFrameVariableMaterializer::escapeCatchObject(Value *V) {
2066 // Catch parameter objects have to live in the parent frame. When we see a use
2067 // of a catch parameter, add a sentinel to the multimap to indicate that it's
2068 // used from another handler. This will prevent us from trying to sink the
2069 // alloca into the handler and ensure that the catch parameter is present in
Reid Kleckner60381792015-07-07 22:25:32 +00002070 // the call to llvm.localescape.
Reid Kleckner3567d272015-04-02 21:13:31 +00002071 FrameVarInfo[V].push_back(getCatchObjectSentinel());
2072}
2073
Andrew Kaylor6b67d422015-03-11 23:22:06 +00002074// This function maps the catch and cleanup handlers that are reachable from the
2075// specified landing pad. The landing pad sequence will have this basic shape:
2076//
2077// <cleanup handler>
2078// <selector comparison>
2079// <catch handler>
2080// <cleanup handler>
2081// <selector comparison>
2082// <catch handler>
2083// <cleanup handler>
2084// ...
2085//
2086// Any of the cleanup slots may be absent. The cleanup slots may be occupied by
2087// any arbitrary control flow, but all paths through the cleanup code must
2088// eventually reach the next selector comparison and no path can skip to a
2089// different selector comparisons, though some paths may terminate abnormally.
2090// Therefore, we will use a depth first search from the start of any given
2091// cleanup block and stop searching when we find the next selector comparison.
2092//
2093// If the landingpad instruction does not have a catch clause, we will assume
2094// that any instructions other than selector comparisons and catch handlers can
2095// be ignored. In practice, these will only be the boilerplate instructions.
2096//
2097// The catch handlers may also have any control structure, but we are only
2098// interested in the start of the catch handlers, so we don't need to actually
2099// follow the flow of the catch handlers. The start of the catch handlers can
2100// be located from the compare instructions, but they can be skipped in the
2101// flow by following the contrary branch.
2102void WinEHPrepare::mapLandingPadBlocks(LandingPadInst *LPad,
2103 LandingPadActions &Actions) {
2104 unsigned int NumClauses = LPad->getNumClauses();
2105 unsigned int HandlersFound = 0;
2106 BasicBlock *BB = LPad->getParent();
2107
2108 DEBUG(dbgs() << "Mapping landing pad: " << BB->getName() << "\n");
2109
2110 if (NumClauses == 0) {
Reid Kleckner9405ef02015-04-10 23:12:29 +00002111 findCleanupHandlers(Actions, BB, nullptr);
Andrew Kaylor6b67d422015-03-11 23:22:06 +00002112 return;
2113 }
2114
2115 VisitedBlockSet VisitedBlocks;
2116
2117 while (HandlersFound != NumClauses) {
Andrew Kaylor6b67d422015-03-11 23:22:06 +00002118 BasicBlock *NextBB = nullptr;
2119
Andrew Kaylor20ae2a32015-04-23 22:38:36 +00002120 // Skip over filter clauses.
2121 if (LPad->isFilter(HandlersFound)) {
2122 ++HandlersFound;
2123 continue;
2124 }
2125
Andrew Kaylor6b67d422015-03-11 23:22:06 +00002126 // See if the clause we're looking for is a catch-all.
2127 // If so, the catch begins immediately.
Andrew Kaylor91307432015-04-28 22:01:51 +00002128 Constant *ExpectedSelector =
2129 LPad->getClause(HandlersFound)->stripPointerCasts();
Andrew Kaylorea8df612015-04-17 23:05:43 +00002130 if (isa<ConstantPointerNull>(ExpectedSelector)) {
Andrew Kaylor6b67d422015-03-11 23:22:06 +00002131 // The catch all must occur last.
2132 assert(HandlersFound == NumClauses - 1);
2133
Andrew Kaylorea8df612015-04-17 23:05:43 +00002134 // There can be additional selector dispatches in the call chain that we
2135 // need to ignore.
2136 BasicBlock *CatchBlock = nullptr;
2137 Constant *Selector;
2138 while (BB && isSelectorDispatch(BB, CatchBlock, Selector, NextBB)) {
2139 DEBUG(dbgs() << " Found extra catch dispatch in block "
Andrew Kaylor91307432015-04-28 22:01:51 +00002140 << CatchBlock->getName() << "\n");
Andrew Kaylorea8df612015-04-17 23:05:43 +00002141 BB = NextBB;
2142 }
2143
Andrew Kaylor6b67d422015-03-11 23:22:06 +00002144 // Add the catch handler to the action list.
Andrew Kaylorf18771b2015-04-20 18:48:45 +00002145 CatchHandler *Action = nullptr;
2146 if (CatchHandlerMap.count(BB) && CatchHandlerMap[BB] != nullptr) {
2147 // If the CatchHandlerMap already has an entry for this BB, re-use it.
2148 Action = CatchHandlerMap[BB];
2149 assert(Action->getSelector() == ExpectedSelector);
2150 } else {
Andrew Kaylor046f7b42015-04-28 21:54:14 +00002151 // We don't expect a selector dispatch, but there may be a call to
2152 // llvm.eh.begincatch, which separates catch handling code from
2153 // cleanup code in the same control flow. This call looks for the
2154 // begincatch intrinsic.
2155 Action = findCatchHandler(BB, NextBB, VisitedBlocks);
2156 if (Action) {
2157 // For C++ EH, check if there is any interesting cleanup code before
2158 // we begin the catch. This is important because cleanups cannot
2159 // rethrow exceptions but code called from catches can. For SEH, it
2160 // isn't important if some finally code before a catch-all is executed
2161 // out of line or after recovering from the exception.
2162 if (Personality == EHPersonality::MSVC_CXX)
2163 findCleanupHandlers(Actions, BB, BB);
2164 } else {
2165 // If an action was not found, it means that the control flows
2166 // directly into the catch-all handler and there is no cleanup code.
2167 // That's an expected situation and we must create a catch action.
2168 // Since this is a catch-all handler, the selector won't actually
2169 // appear in the code anywhere. ExpectedSelector here is the constant
2170 // null ptr that we got from the landing pad instruction.
2171 Action = new CatchHandler(BB, ExpectedSelector, nullptr);
2172 CatchHandlerMap[BB] = Action;
2173 }
Andrew Kaylorf18771b2015-04-20 18:48:45 +00002174 }
Andrew Kaylor6b67d422015-03-11 23:22:06 +00002175 Actions.insertCatchHandler(Action);
2176 DEBUG(dbgs() << " Catch all handler at block " << BB->getName() << "\n");
2177 ++HandlersFound;
Reid Kleckner0f9e27a2015-03-18 20:26:53 +00002178
2179 // Once we reach a catch-all, don't expect to hit a resume instruction.
2180 BB = nullptr;
2181 break;
Andrew Kaylor6b67d422015-03-11 23:22:06 +00002182 }
2183
2184 CatchHandler *CatchAction = findCatchHandler(BB, NextBB, VisitedBlocks);
Andrew Kaylor41758512015-04-20 22:04:09 +00002185 assert(CatchAction);
2186
Andrew Kaylor6b67d422015-03-11 23:22:06 +00002187 // See if there is any interesting code executed before the dispatch.
Reid Kleckner9405ef02015-04-10 23:12:29 +00002188 findCleanupHandlers(Actions, BB, CatchAction->getStartBlock());
Andrew Kaylor6b67d422015-03-11 23:22:06 +00002189
Andrew Kaylorea8df612015-04-17 23:05:43 +00002190 // When the source program contains multiple nested try blocks the catch
2191 // handlers can get strung together in such a way that we can encounter
2192 // a dispatch for a selector that we've already had a handler for.
2193 if (CatchAction->getSelector()->stripPointerCasts() == ExpectedSelector) {
2194 ++HandlersFound;
2195
2196 // Add the catch handler to the action list.
2197 DEBUG(dbgs() << " Found catch dispatch in block "
2198 << CatchAction->getStartBlock()->getName() << "\n");
2199 Actions.insertCatchHandler(CatchAction);
2200 } else {
Andrew Kaylor41758512015-04-20 22:04:09 +00002201 // Under some circumstances optimized IR will flow unconditionally into a
2202 // handler block without checking the selector. This can only happen if
2203 // the landing pad has a catch-all handler and the handler for the
Benjamin Kramerdf005cb2015-08-08 18:27:36 +00002204 // preceding catch clause is identical to the catch-call handler
Andrew Kaylor41758512015-04-20 22:04:09 +00002205 // (typically an empty catch). In this case, the handler must be shared
2206 // by all remaining clauses.
2207 if (isa<ConstantPointerNull>(
2208 CatchAction->getSelector()->stripPointerCasts())) {
2209 DEBUG(dbgs() << " Applying early catch-all handler in block "
2210 << CatchAction->getStartBlock()->getName()
2211 << " to all remaining clauses.\n");
2212 Actions.insertCatchHandler(CatchAction);
2213 return;
2214 }
2215
Andrew Kaylorea8df612015-04-17 23:05:43 +00002216 DEBUG(dbgs() << " Found extra catch dispatch in block "
2217 << CatchAction->getStartBlock()->getName() << "\n");
2218 }
Andrew Kaylor6b67d422015-03-11 23:22:06 +00002219
2220 // Move on to the block after the catch handler.
2221 BB = NextBB;
2222 }
2223
Reid Kleckner0f9e27a2015-03-18 20:26:53 +00002224 // If we didn't wind up in a catch-all, see if there is any interesting code
2225 // executed before the resume.
Reid Kleckner9405ef02015-04-10 23:12:29 +00002226 findCleanupHandlers(Actions, BB, BB);
Andrew Kaylor6b67d422015-03-11 23:22:06 +00002227
2228 // It's possible that some optimization moved code into a landingpad that
2229 // wasn't
2230 // previously being used for cleanup. If that happens, we need to execute
2231 // that
2232 // extra code from a cleanup handler.
2233 if (Actions.includesCleanup() && !LPad->isCleanup())
2234 LPad->setCleanup(true);
2235}
2236
2237// This function searches starting with the input block for the next
2238// block that terminates with a branch whose condition is based on a selector
2239// comparison. This may be the input block. See the mapLandingPadBlocks
2240// comments for a discussion of control flow assumptions.
2241//
2242CatchHandler *WinEHPrepare::findCatchHandler(BasicBlock *BB,
2243 BasicBlock *&NextBB,
2244 VisitedBlockSet &VisitedBlocks) {
2245 // See if we've already found a catch handler use it.
2246 // Call count() first to avoid creating a null entry for blocks
2247 // we haven't seen before.
2248 if (CatchHandlerMap.count(BB) && CatchHandlerMap[BB] != nullptr) {
2249 CatchHandler *Action = cast<CatchHandler>(CatchHandlerMap[BB]);
2250 NextBB = Action->getNextBB();
2251 return Action;
2252 }
2253
2254 // VisitedBlocks applies only to the current search. We still
2255 // need to consider blocks that we've visited while mapping other
2256 // landing pads.
2257 VisitedBlocks.insert(BB);
2258
2259 BasicBlock *CatchBlock = nullptr;
2260 Constant *Selector = nullptr;
2261
2262 // If this is the first time we've visited this block from any landing pad
2263 // look to see if it is a selector dispatch block.
2264 if (!CatchHandlerMap.count(BB)) {
2265 if (isSelectorDispatch(BB, CatchBlock, Selector, NextBB)) {
2266 CatchHandler *Action = new CatchHandler(BB, Selector, NextBB);
2267 CatchHandlerMap[BB] = Action;
2268 return Action;
2269 }
Andrew Kaylor41758512015-04-20 22:04:09 +00002270 // If we encounter a block containing an llvm.eh.begincatch before we
2271 // find a selector dispatch block, the handler is assumed to be
2272 // reached unconditionally. This happens for catch-all blocks, but
2273 // it can also happen for other catch handlers that have been combined
2274 // with the catch-all handler during optimization.
2275 if (isCatchBlock(BB)) {
2276 PointerType *Int8PtrTy = Type::getInt8PtrTy(BB->getContext());
2277 Constant *NullSelector = ConstantPointerNull::get(Int8PtrTy);
2278 CatchHandler *Action = new CatchHandler(BB, NullSelector, nullptr);
2279 CatchHandlerMap[BB] = Action;
2280 return Action;
2281 }
Andrew Kaylor6b67d422015-03-11 23:22:06 +00002282 }
2283
2284 // Visit each successor, looking for the dispatch.
2285 // FIXME: We expect to find the dispatch quickly, so this will probably
2286 // work better as a breadth first search.
2287 for (BasicBlock *Succ : successors(BB)) {
2288 if (VisitedBlocks.count(Succ))
2289 continue;
2290
2291 CatchHandler *Action = findCatchHandler(Succ, NextBB, VisitedBlocks);
2292 if (Action)
2293 return Action;
2294 }
2295 return nullptr;
2296}
2297
Reid Kleckner9405ef02015-04-10 23:12:29 +00002298// These are helper functions to combine repeated code from findCleanupHandlers.
2299static void createCleanupHandler(LandingPadActions &Actions,
2300 CleanupHandlerMapTy &CleanupHandlerMap,
2301 BasicBlock *BB) {
Andrew Kaylor6b67d422015-03-11 23:22:06 +00002302 CleanupHandler *Action = new CleanupHandler(BB);
2303 CleanupHandlerMap[BB] = Action;
Reid Kleckner9405ef02015-04-10 23:12:29 +00002304 Actions.insertCleanupHandler(Action);
2305 DEBUG(dbgs() << " Found cleanup code in block "
2306 << Action->getStartBlock()->getName() << "\n");
2307}
2308
Reid Kleckner9405ef02015-04-10 23:12:29 +00002309static CallSite matchOutlinedFinallyCall(BasicBlock *BB,
2310 Instruction *MaybeCall) {
2311 // Look for finally blocks that Clang has already outlined for us.
Reid Klecknerd5afc62f2015-07-07 23:23:03 +00002312 // %fp = call i8* @llvm.localaddress()
Reid Kleckner9405ef02015-04-10 23:12:29 +00002313 // call void @"fin$parent"(iN 1, i8* %fp)
Reid Klecknerd5afc62f2015-07-07 23:23:03 +00002314 if (isLocalAddressCall(MaybeCall) && MaybeCall != BB->getTerminator())
Reid Kleckner9405ef02015-04-10 23:12:29 +00002315 MaybeCall = MaybeCall->getNextNode();
2316 CallSite FinallyCall(MaybeCall);
2317 if (!FinallyCall || FinallyCall.arg_size() != 2)
2318 return CallSite();
2319 if (!match(FinallyCall.getArgument(0), m_SpecificInt(1)))
2320 return CallSite();
Reid Klecknerd5afc62f2015-07-07 23:23:03 +00002321 if (!isLocalAddressCall(FinallyCall.getArgument(1)))
Reid Kleckner9405ef02015-04-10 23:12:29 +00002322 return CallSite();
2323 return FinallyCall;
2324}
2325
2326static BasicBlock *followSingleUnconditionalBranches(BasicBlock *BB) {
2327 // Skip single ubr blocks.
2328 while (BB->getFirstNonPHIOrDbg() == BB->getTerminator()) {
2329 auto *Br = dyn_cast<BranchInst>(BB->getTerminator());
2330 if (Br && Br->isUnconditional())
2331 BB = Br->getSuccessor(0);
2332 else
2333 return BB;
2334 }
2335 return BB;
Andrew Kaylor6b67d422015-03-11 23:22:06 +00002336}
2337
2338// This function searches starting with the input block for the next block that
2339// contains code that is not part of a catch handler and would not be eliminated
2340// during handler outlining.
2341//
Reid Kleckner9405ef02015-04-10 23:12:29 +00002342void WinEHPrepare::findCleanupHandlers(LandingPadActions &Actions,
2343 BasicBlock *StartBB, BasicBlock *EndBB) {
Andrew Kaylor6b67d422015-03-11 23:22:06 +00002344 // Here we will skip over the following:
2345 //
2346 // landing pad prolog:
2347 //
2348 // Unconditional branches
2349 //
2350 // Selector dispatch
2351 //
2352 // Resume pattern
2353 //
2354 // Anything else marks the start of an interesting block
2355
2356 BasicBlock *BB = StartBB;
2357 // Anything other than an unconditional branch will kick us out of this loop
2358 // one way or another.
2359 while (BB) {
Reid Kleckner9405ef02015-04-10 23:12:29 +00002360 BB = followSingleUnconditionalBranches(BB);
Andrew Kaylor6b67d422015-03-11 23:22:06 +00002361 // If we've already scanned this block, don't scan it again. If it is
2362 // a cleanup block, there will be an action in the CleanupHandlerMap.
2363 // If we've scanned it and it is not a cleanup block, there will be a
2364 // nullptr in the CleanupHandlerMap. If we have not scanned it, there will
2365 // be no entry in the CleanupHandlerMap. We must call count() first to
2366 // avoid creating a null entry for blocks we haven't scanned.
2367 if (CleanupHandlerMap.count(BB)) {
2368 if (auto *Action = CleanupHandlerMap[BB]) {
Reid Kleckner9405ef02015-04-10 23:12:29 +00002369 Actions.insertCleanupHandler(Action);
2370 DEBUG(dbgs() << " Found cleanup code in block "
Andrew Kaylor91307432015-04-28 22:01:51 +00002371 << Action->getStartBlock()->getName() << "\n");
Reid Kleckner9405ef02015-04-10 23:12:29 +00002372 // FIXME: This cleanup might chain into another, and we need to discover
2373 // that.
2374 return;
Andrew Kaylor6b67d422015-03-11 23:22:06 +00002375 } else {
2376 // Here we handle the case where the cleanup handler map contains a
2377 // value for this block but the value is a nullptr. This means that
2378 // we have previously analyzed the block and determined that it did
2379 // not contain any cleanup code. Based on the earlier analysis, we
Eric Christopher572e03a2015-06-19 01:53:21 +00002380 // know the block must end in either an unconditional branch, a
Andrew Kaylor6b67d422015-03-11 23:22:06 +00002381 // resume or a conditional branch that is predicated on a comparison
2382 // with a selector. Either the resume or the selector dispatch
2383 // would terminate the search for cleanup code, so the unconditional
2384 // branch is the only case for which we might need to continue
2385 // searching.
Reid Kleckner9405ef02015-04-10 23:12:29 +00002386 BasicBlock *SuccBB = followSingleUnconditionalBranches(BB);
2387 if (SuccBB == BB || SuccBB == EndBB)
2388 return;
Andrew Kaylor6b67d422015-03-11 23:22:06 +00002389 BB = SuccBB;
2390 continue;
2391 }
2392 }
2393
2394 // Create an entry in the cleanup handler map for this block. Initially
2395 // we create an entry that says this isn't a cleanup block. If we find
2396 // cleanup code, the caller will replace this entry.
2397 CleanupHandlerMap[BB] = nullptr;
2398
2399 TerminatorInst *Terminator = BB->getTerminator();
2400
2401 // Landing pad blocks have extra instructions we need to accept.
2402 LandingPadMap *LPadMap = nullptr;
2403 if (BB->isLandingPad()) {
2404 LandingPadInst *LPad = BB->getLandingPadInst();
2405 LPadMap = &LPadMaps[LPad];
2406 if (!LPadMap->isInitialized())
2407 LPadMap->mapLandingPad(LPad);
2408 }
2409
2410 // Look for the bare resume pattern:
Andrew Kaylor64622aa2015-04-01 17:21:25 +00002411 // %lpad.val1 = insertvalue { i8*, i32 } undef, i8* %exn, 0
2412 // %lpad.val2 = insertvalue { i8*, i32 } %lpad.val1, i32 %sel, 1
Andrew Kaylor6b67d422015-03-11 23:22:06 +00002413 // resume { i8*, i32 } %lpad.val2
2414 if (auto *Resume = dyn_cast<ResumeInst>(Terminator)) {
2415 InsertValueInst *Insert1 = nullptr;
2416 InsertValueInst *Insert2 = nullptr;
Reid Kleckner0f9e27a2015-03-18 20:26:53 +00002417 Value *ResumeVal = Resume->getOperand(0);
Reid Kleckner1c130bb2015-04-16 17:02:23 +00002418 // If the resume value isn't a phi or landingpad value, it should be a
2419 // series of insertions. Identify them so we can avoid them when scanning
2420 // for cleanups.
2421 if (!isa<PHINode>(ResumeVal) && !isa<LandingPadInst>(ResumeVal)) {
Reid Kleckner0f9e27a2015-03-18 20:26:53 +00002422 Insert2 = dyn_cast<InsertValueInst>(ResumeVal);
Andrew Kaylor6b67d422015-03-11 23:22:06 +00002423 if (!Insert2)
Reid Kleckner9405ef02015-04-10 23:12:29 +00002424 return createCleanupHandler(Actions, CleanupHandlerMap, BB);
Andrew Kaylor6b67d422015-03-11 23:22:06 +00002425 Insert1 = dyn_cast<InsertValueInst>(Insert2->getAggregateOperand());
2426 if (!Insert1)
Reid Kleckner9405ef02015-04-10 23:12:29 +00002427 return createCleanupHandler(Actions, CleanupHandlerMap, BB);
Andrew Kaylor6b67d422015-03-11 23:22:06 +00002428 }
2429 for (BasicBlock::iterator II = BB->getFirstNonPHIOrDbg(), IE = BB->end();
2430 II != IE; ++II) {
2431 Instruction *Inst = II;
2432 if (LPadMap && LPadMap->isLandingPadSpecificInst(Inst))
2433 continue;
2434 if (Inst == Insert1 || Inst == Insert2 || Inst == Resume)
2435 continue;
2436 if (!Inst->hasOneUse() ||
2437 (Inst->user_back() != Insert1 && Inst->user_back() != Insert2)) {
Reid Kleckner9405ef02015-04-10 23:12:29 +00002438 return createCleanupHandler(Actions, CleanupHandlerMap, BB);
Andrew Kaylor6b67d422015-03-11 23:22:06 +00002439 }
2440 }
Reid Kleckner9405ef02015-04-10 23:12:29 +00002441 return;
Andrew Kaylor6b67d422015-03-11 23:22:06 +00002442 }
2443
2444 BranchInst *Branch = dyn_cast<BranchInst>(Terminator);
Andrew Kaylor64622aa2015-04-01 17:21:25 +00002445 if (Branch && Branch->isConditional()) {
2446 // Look for the selector dispatch.
2447 // %2 = call i32 @llvm.eh.typeid.for(i8* bitcast (i8** @_ZTIf to i8*))
2448 // %matches = icmp eq i32 %sel, %2
2449 // br i1 %matches, label %catch14, label %eh.resume
2450 CmpInst *Compare = dyn_cast<CmpInst>(Branch->getCondition());
2451 if (!Compare || !Compare->isEquality())
Reid Kleckner9405ef02015-04-10 23:12:29 +00002452 return createCleanupHandler(Actions, CleanupHandlerMap, BB);
Andrew Kaylorbb111322015-04-07 21:30:23 +00002453 for (BasicBlock::iterator II = BB->getFirstNonPHIOrDbg(), IE = BB->end();
2454 II != IE; ++II) {
Andrew Kaylor64622aa2015-04-01 17:21:25 +00002455 Instruction *Inst = II;
2456 if (LPadMap && LPadMap->isLandingPadSpecificInst(Inst))
2457 continue;
2458 if (Inst == Compare || Inst == Branch)
2459 continue;
2460 if (match(Inst, m_Intrinsic<Intrinsic::eh_typeid_for>()))
2461 continue;
Reid Kleckner9405ef02015-04-10 23:12:29 +00002462 return createCleanupHandler(Actions, CleanupHandlerMap, BB);
Andrew Kaylor64622aa2015-04-01 17:21:25 +00002463 }
2464 // The selector dispatch block should always terminate our search.
2465 assert(BB == EndBB);
Reid Kleckner9405ef02015-04-10 23:12:29 +00002466 return;
2467 }
2468
2469 if (isAsynchronousEHPersonality(Personality)) {
2470 // If this is a landingpad block, split the block at the first non-landing
2471 // pad instruction.
2472 Instruction *MaybeCall = BB->getFirstNonPHIOrDbg();
2473 if (LPadMap) {
2474 while (MaybeCall != BB->getTerminator() &&
2475 LPadMap->isLandingPadSpecificInst(MaybeCall))
2476 MaybeCall = MaybeCall->getNextNode();
2477 }
2478
Reid Kleckner6511c8b2015-07-01 20:59:25 +00002479 // Look for outlined finally calls on x64, since those happen to match the
2480 // prototype provided by the runtime.
2481 if (TheTriple.getArch() == Triple::x86_64) {
2482 if (CallSite FinallyCall = matchOutlinedFinallyCall(BB, MaybeCall)) {
2483 Function *Fin = FinallyCall.getCalledFunction();
2484 assert(Fin && "outlined finally call should be direct");
2485 auto *Action = new CleanupHandler(BB);
2486 Action->setHandlerBlockOrFunc(Fin);
2487 Actions.insertCleanupHandler(Action);
2488 CleanupHandlerMap[BB] = Action;
2489 DEBUG(dbgs() << " Found frontend-outlined finally call to "
2490 << Fin->getName() << " in block "
2491 << Action->getStartBlock()->getName() << "\n");
Reid Kleckner9405ef02015-04-10 23:12:29 +00002492
Reid Kleckner6511c8b2015-07-01 20:59:25 +00002493 // Split the block if there were more interesting instructions and
2494 // look for finally calls in the normal successor block.
2495 BasicBlock *SuccBB = BB;
2496 if (FinallyCall.getInstruction() != BB->getTerminator() &&
2497 FinallyCall.getInstruction()->getNextNode() !=
2498 BB->getTerminator()) {
Andrew Kaylor046f7b42015-04-28 21:54:14 +00002499 SuccBB =
Reid Kleckner6511c8b2015-07-01 20:59:25 +00002500 SplitBlock(BB, FinallyCall.getInstruction()->getNextNode(), DT);
Reid Kleckner9405ef02015-04-10 23:12:29 +00002501 } else {
Reid Kleckner6511c8b2015-07-01 20:59:25 +00002502 if (FinallyCall.isInvoke()) {
2503 SuccBB = cast<InvokeInst>(FinallyCall.getInstruction())
2504 ->getNormalDest();
2505 } else {
2506 SuccBB = BB->getUniqueSuccessor();
2507 assert(SuccBB &&
2508 "splitOutlinedFinallyCalls didn't insert a branch");
2509 }
Reid Kleckner9405ef02015-04-10 23:12:29 +00002510 }
Reid Kleckner6511c8b2015-07-01 20:59:25 +00002511 BB = SuccBB;
2512 if (BB == EndBB)
2513 return;
2514 continue;
Reid Kleckner9405ef02015-04-10 23:12:29 +00002515 }
Reid Kleckner9405ef02015-04-10 23:12:29 +00002516 }
Andrew Kaylor64622aa2015-04-01 17:21:25 +00002517 }
Andrew Kaylor6b67d422015-03-11 23:22:06 +00002518
Andrew Kaylor64622aa2015-04-01 17:21:25 +00002519 // Anything else is either a catch block or interesting cleanup code.
Andrew Kaylorbb111322015-04-07 21:30:23 +00002520 for (BasicBlock::iterator II = BB->getFirstNonPHIOrDbg(), IE = BB->end();
2521 II != IE; ++II) {
Andrew Kaylor64622aa2015-04-01 17:21:25 +00002522 Instruction *Inst = II;
2523 if (LPadMap && LPadMap->isLandingPadSpecificInst(Inst))
2524 continue;
2525 // Unconditional branches fall through to this loop.
2526 if (Inst == Branch)
2527 continue;
2528 // If this is a catch block, there is no cleanup code to be found.
2529 if (match(Inst, m_Intrinsic<Intrinsic::eh_begincatch>()))
Reid Kleckner9405ef02015-04-10 23:12:29 +00002530 return;
Andrew Kayloraa92ab02015-04-03 19:37:50 +00002531 // If this a nested landing pad, it may contain an endcatch call.
2532 if (match(Inst, m_Intrinsic<Intrinsic::eh_endcatch>()))
Reid Kleckner9405ef02015-04-10 23:12:29 +00002533 return;
Andrew Kaylor64622aa2015-04-01 17:21:25 +00002534 // Anything else makes this interesting cleanup code.
Reid Kleckner9405ef02015-04-10 23:12:29 +00002535 return createCleanupHandler(Actions, CleanupHandlerMap, BB);
Andrew Kaylor64622aa2015-04-01 17:21:25 +00002536 }
2537
2538 // Only unconditional branches in empty blocks should get this far.
2539 assert(Branch && Branch->isUnconditional());
2540 if (BB == EndBB)
Reid Kleckner9405ef02015-04-10 23:12:29 +00002541 return;
Andrew Kaylor64622aa2015-04-01 17:21:25 +00002542 BB = Branch->getSuccessor(0);
Andrew Kaylor6b67d422015-03-11 23:22:06 +00002543 }
Andrew Kaylor6b67d422015-03-11 23:22:06 +00002544}
Andrew Kayloraa92ab02015-04-03 19:37:50 +00002545
2546// This is a public function, declared in WinEHFuncInfo.h and is also
2547// referenced by WinEHNumbering in FunctionLoweringInfo.cpp.
Benjamin Kramera48e0652015-05-16 15:40:03 +00002548void llvm::parseEHActions(
2549 const IntrinsicInst *II,
2550 SmallVectorImpl<std::unique_ptr<ActionHandler>> &Actions) {
Reid Klecknerf12c0302015-06-09 21:42:19 +00002551 assert(II->getIntrinsicID() == Intrinsic::eh_actions &&
2552 "attempted to parse non eh.actions intrinsic");
Andrew Kayloraa92ab02015-04-03 19:37:50 +00002553 for (unsigned I = 0, E = II->getNumArgOperands(); I != E;) {
2554 uint64_t ActionKind =
Andrew Kaylorbb111322015-04-07 21:30:23 +00002555 cast<ConstantInt>(II->getArgOperand(I))->getZExtValue();
Andrew Kayloraa92ab02015-04-03 19:37:50 +00002556 if (ActionKind == /*catch=*/1) {
2557 auto *Selector = cast<Constant>(II->getArgOperand(I + 1));
2558 ConstantInt *EHObjIndex = cast<ConstantInt>(II->getArgOperand(I + 2));
2559 int64_t EHObjIndexVal = EHObjIndex->getSExtValue();
2560 Constant *Handler = cast<Constant>(II->getArgOperand(I + 3));
2561 I += 4;
Benjamin Kramera48e0652015-05-16 15:40:03 +00002562 auto CH = make_unique<CatchHandler>(/*BB=*/nullptr, Selector,
2563 /*NextBB=*/nullptr);
Andrew Kayloraa92ab02015-04-03 19:37:50 +00002564 CH->setHandlerBlockOrFunc(Handler);
2565 CH->setExceptionVarIndex(EHObjIndexVal);
Benjamin Kramera48e0652015-05-16 15:40:03 +00002566 Actions.push_back(std::move(CH));
David Majnemer69132a72015-04-03 22:49:05 +00002567 } else if (ActionKind == 0) {
Andrew Kayloraa92ab02015-04-03 19:37:50 +00002568 Constant *Handler = cast<Constant>(II->getArgOperand(I + 1));
2569 I += 2;
Benjamin Kramera48e0652015-05-16 15:40:03 +00002570 auto CH = make_unique<CleanupHandler>(/*BB=*/nullptr);
Andrew Kayloraa92ab02015-04-03 19:37:50 +00002571 CH->setHandlerBlockOrFunc(Handler);
Benjamin Kramera48e0652015-05-16 15:40:03 +00002572 Actions.push_back(std::move(CH));
David Majnemer69132a72015-04-03 22:49:05 +00002573 } else {
2574 llvm_unreachable("Expected either a catch or cleanup handler!");
Andrew Kayloraa92ab02015-04-03 19:37:50 +00002575 }
2576 }
2577 std::reverse(Actions.begin(), Actions.end());
2578}
Reid Klecknerfe4d4912015-05-28 22:00:24 +00002579
David Majnemer0ad363e2015-08-18 19:07:12 +00002580static int addUnwindMapEntry(WinEHFuncInfo &FuncInfo, int ToState,
2581 const Value *V) {
Reid Klecknerfe4d4912015-05-28 22:00:24 +00002582 WinEHUnwindMapEntry UME;
2583 UME.ToState = ToState;
David Majnemer0ad363e2015-08-18 19:07:12 +00002584 UME.Cleanup = V;
Reid Klecknerfe4d4912015-05-28 22:00:24 +00002585 FuncInfo.UnwindMap.push_back(UME);
David Majnemer0ad363e2015-08-18 19:07:12 +00002586 return FuncInfo.getLastStateNumber();
2587}
2588
2589static void addTryBlockMapEntry(WinEHFuncInfo &FuncInfo, int TryLow,
2590 int TryHigh, int CatchHigh,
2591 ArrayRef<const CatchPadInst *> Handlers) {
2592 WinEHTryBlockMapEntry TBME;
2593 TBME.TryLow = TryLow;
2594 TBME.TryHigh = TryHigh;
2595 TBME.CatchHigh = CatchHigh;
2596 assert(TBME.TryLow <= TBME.TryHigh);
2597 for (const CatchPadInst *CPI : Handlers) {
2598 WinEHHandlerType HT;
2599 Constant *TypeInfo = cast<Constant>(CPI->getArgOperand(0));
Reid Klecknerb005d282015-09-16 20:16:27 +00002600 if (TypeInfo->isNullValue())
David Majnemer0ad363e2015-08-18 19:07:12 +00002601 HT.TypeDescriptor = nullptr;
Reid Klecknerb005d282015-09-16 20:16:27 +00002602 else
2603 HT.TypeDescriptor = cast<GlobalVariable>(TypeInfo->stripPointerCasts());
2604 HT.Adjectives = cast<ConstantInt>(CPI->getArgOperand(1))->getZExtValue();
Reid Kleckner0e288232015-08-27 23:27:47 +00002605 HT.Handler = CPI->getNormalDest();
Reid Klecknerb005d282015-09-16 20:16:27 +00002606 HT.CatchObjRecoverIdx = -2;
2607 if (isa<ConstantPointerNull>(CPI->getArgOperand(2)))
2608 HT.CatchObj.Alloca = nullptr;
2609 else
2610 HT.CatchObj.Alloca = cast<AllocaInst>(CPI->getArgOperand(2));
David Majnemer0ad363e2015-08-18 19:07:12 +00002611 TBME.HandlerArray.push_back(HT);
2612 }
2613 FuncInfo.TryBlockMap.push_back(TBME);
2614}
2615
Reid Kleckner94b704c2015-09-09 21:10:03 +00002616static const CatchPadInst *getSingleCatchPadPredecessor(const BasicBlock *BB) {
2617 for (const BasicBlock *PredBlock : predecessors(BB))
2618 if (auto *CPI = dyn_cast<CatchPadInst>(PredBlock->getFirstNonPHI()))
2619 return CPI;
David Majnemer0ad363e2015-08-18 19:07:12 +00002620 return nullptr;
2621}
2622
Reid Kleckner94b704c2015-09-09 21:10:03 +00002623/// Find all the catchpads that feed directly into the catchendpad. Frontends
2624/// using this personality should ensure that each catchendpad and catchpad has
2625/// one or zero catchpad predecessors.
2626///
2627/// The following C++ generates the IR after it:
2628/// try {
2629/// } catch (A) {
2630/// } catch (B) {
2631/// }
2632///
2633/// IR:
2634/// %catchpad.A
2635/// catchpad [i8* A typeinfo]
2636/// to label %catch.A unwind label %catchpad.B
2637/// %catchpad.B
2638/// catchpad [i8* B typeinfo]
2639/// to label %catch.B unwind label %endcatches
2640/// %endcatches
2641/// catchendblock unwind to caller
Benjamin Kramer3c96f0a2015-09-22 14:34:57 +00002642static void
2643findCatchPadsForCatchEndPad(const BasicBlock *CatchEndBB,
2644 SmallVectorImpl<const CatchPadInst *> &Handlers) {
Reid Kleckner94b704c2015-09-09 21:10:03 +00002645 const CatchPadInst *CPI = getSingleCatchPadPredecessor(CatchEndBB);
2646 while (CPI) {
2647 Handlers.push_back(CPI);
2648 CPI = getSingleCatchPadPredecessor(CPI->getParent());
2649 }
2650 // We've pushed these back into reverse source order. Reverse them to get
2651 // the list back into source order.
2652 std::reverse(Handlers.begin(), Handlers.end());
2653}
2654
David Majnemer0ad363e2015-08-18 19:07:12 +00002655// Given BB which ends in an unwind edge, return the EHPad that this BB belongs
2656// to. If the unwind edge came from an invoke, return null.
2657static const BasicBlock *getEHPadFromPredecessor(const BasicBlock *BB) {
2658 const TerminatorInst *TI = BB->getTerminator();
2659 if (isa<InvokeInst>(TI))
2660 return nullptr;
Reid Kleckner7bb20bd2015-09-10 21:46:36 +00002661 if (TI->isEHPad())
David Majnemer0ad363e2015-08-18 19:07:12 +00002662 return BB;
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +00002663 return cast<CleanupReturnInst>(TI)->getCleanupPad()->getParent();
David Majnemer0ad363e2015-08-18 19:07:12 +00002664}
2665
Reid Kleckner94b704c2015-09-09 21:10:03 +00002666static void calculateExplicitCXXStateNumbers(WinEHFuncInfo &FuncInfo,
2667 const BasicBlock &BB,
2668 int ParentState) {
David Majnemer0ad363e2015-08-18 19:07:12 +00002669 assert(BB.isEHPad());
2670 const Instruction *FirstNonPHI = BB.getFirstNonPHI();
2671 // All catchpad instructions will be handled when we process their
2672 // respective catchendpad instruction.
2673 if (isa<CatchPadInst>(FirstNonPHI))
2674 return;
2675
2676 if (isa<CatchEndPadInst>(FirstNonPHI)) {
David Majnemer0ad363e2015-08-18 19:07:12 +00002677 SmallVector<const CatchPadInst *, 2> Handlers;
Reid Kleckner94b704c2015-09-09 21:10:03 +00002678 findCatchPadsForCatchEndPad(&BB, Handlers);
2679 const BasicBlock *FirstTryPad = Handlers.front()->getParent();
David Majnemer0ad363e2015-08-18 19:07:12 +00002680 int TryLow = addUnwindMapEntry(FuncInfo, ParentState, nullptr);
2681 FuncInfo.EHPadStateMap[Handlers.front()] = TryLow;
Reid Kleckner94b704c2015-09-09 21:10:03 +00002682 for (const BasicBlock *PredBlock : predecessors(FirstTryPad))
David Majnemer0ad363e2015-08-18 19:07:12 +00002683 if ((PredBlock = getEHPadFromPredecessor(PredBlock)))
Reid Kleckner94b704c2015-09-09 21:10:03 +00002684 calculateExplicitCXXStateNumbers(FuncInfo, *PredBlock, TryLow);
David Majnemer0ad363e2015-08-18 19:07:12 +00002685 int CatchLow = addUnwindMapEntry(FuncInfo, ParentState, nullptr);
Reid Kleckner94b704c2015-09-09 21:10:03 +00002686
2687 // catchpads are separate funclets in C++ EH due to the way rethrow works.
2688 // In SEH, they aren't, so no invokes will unwind to the catchendpad.
David Majnemer0ad363e2015-08-18 19:07:12 +00002689 FuncInfo.EHPadStateMap[FirstNonPHI] = CatchLow;
2690 int TryHigh = CatchLow - 1;
2691 for (const BasicBlock *PredBlock : predecessors(&BB))
2692 if ((PredBlock = getEHPadFromPredecessor(PredBlock)))
Reid Kleckner94b704c2015-09-09 21:10:03 +00002693 calculateExplicitCXXStateNumbers(FuncInfo, *PredBlock, CatchLow);
David Majnemer0ad363e2015-08-18 19:07:12 +00002694 int CatchHigh = FuncInfo.getLastStateNumber();
2695 addTryBlockMapEntry(FuncInfo, TryLow, TryHigh, CatchHigh, Handlers);
Reid Kleckner94b704c2015-09-09 21:10:03 +00002696 DEBUG(dbgs() << "TryLow[" << FirstTryPad->getName() << "]: " << TryLow
David Majnemer0ad363e2015-08-18 19:07:12 +00002697 << '\n');
Reid Kleckner94b704c2015-09-09 21:10:03 +00002698 DEBUG(dbgs() << "TryHigh[" << FirstTryPad->getName() << "]: " << TryHigh
David Majnemer0ad363e2015-08-18 19:07:12 +00002699 << '\n');
Reid Kleckner94b704c2015-09-09 21:10:03 +00002700 DEBUG(dbgs() << "CatchHigh[" << FirstTryPad->getName() << "]: " << CatchHigh
David Majnemer0ad363e2015-08-18 19:07:12 +00002701 << '\n');
2702 } else if (isa<CleanupPadInst>(FirstNonPHI)) {
2703 int CleanupState = addUnwindMapEntry(FuncInfo, ParentState, &BB);
2704 FuncInfo.EHPadStateMap[FirstNonPHI] = CleanupState;
2705 DEBUG(dbgs() << "Assigning state #" << CleanupState << " to BB "
2706 << BB.getName() << '\n');
2707 for (const BasicBlock *PredBlock : predecessors(&BB))
2708 if ((PredBlock = getEHPadFromPredecessor(PredBlock)))
Reid Kleckner94b704c2015-09-09 21:10:03 +00002709 calculateExplicitCXXStateNumbers(FuncInfo, *PredBlock, CleanupState);
David Majnemer0ad363e2015-08-18 19:07:12 +00002710 } else if (isa<TerminatePadInst>(FirstNonPHI)) {
2711 report_fatal_error("Not yet implemented!");
2712 } else {
2713 llvm_unreachable("unexpected EH Pad!");
2714 }
2715}
2716
Reid Kleckner94b704c2015-09-09 21:10:03 +00002717static int addSEHHandler(WinEHFuncInfo &FuncInfo, int ParentState,
Reid Kleckner78783912015-09-10 00:25:23 +00002718 const Function *Filter, const BasicBlock *Handler) {
Reid Kleckner94b704c2015-09-09 21:10:03 +00002719 SEHUnwindMapEntry Entry;
2720 Entry.ToState = ParentState;
2721 Entry.Filter = Filter;
2722 Entry.Handler = Handler;
2723 FuncInfo.SEHUnwindMap.push_back(Entry);
2724 return FuncInfo.SEHUnwindMap.size() - 1;
2725}
2726
2727static void calculateExplicitSEHStateNumbers(WinEHFuncInfo &FuncInfo,
2728 const BasicBlock &BB,
2729 int ParentState) {
2730 assert(BB.isEHPad());
2731 const Instruction *FirstNonPHI = BB.getFirstNonPHI();
2732 // All catchpad instructions will be handled when we process their
2733 // respective catchendpad instruction.
2734 if (isa<CatchPadInst>(FirstNonPHI))
2735 return;
2736
2737 if (isa<CatchEndPadInst>(FirstNonPHI)) {
2738 // Extract the filter function and the __except basic block and create a
2739 // state for them.
2740 SmallVector<const CatchPadInst *, 1> Handlers;
2741 findCatchPadsForCatchEndPad(&BB, Handlers);
2742 assert(Handlers.size() == 1 &&
2743 "SEH doesn't have multiple handlers per __try");
2744 const CatchPadInst *CPI = Handlers.front();
2745 const BasicBlock *CatchPadBB = CPI->getParent();
2746 const Function *Filter =
2747 cast<Function>(CPI->getArgOperand(0)->stripPointerCasts());
2748 int TryState =
2749 addSEHHandler(FuncInfo, ParentState, Filter, CPI->getNormalDest());
2750
2751 // Everything in the __try block uses TryState as its parent state.
2752 FuncInfo.EHPadStateMap[CPI] = TryState;
2753 DEBUG(dbgs() << "Assigning state #" << TryState << " to BB "
2754 << CatchPadBB->getName() << '\n');
2755 for (const BasicBlock *PredBlock : predecessors(CatchPadBB))
2756 if ((PredBlock = getEHPadFromPredecessor(PredBlock)))
2757 calculateExplicitSEHStateNumbers(FuncInfo, *PredBlock, TryState);
2758
2759 // Everything in the __except block unwinds to ParentState, just like code
2760 // outside the __try.
2761 FuncInfo.EHPadStateMap[FirstNonPHI] = ParentState;
2762 DEBUG(dbgs() << "Assigning state #" << ParentState << " to BB "
2763 << BB.getName() << '\n');
2764 for (const BasicBlock *PredBlock : predecessors(&BB))
2765 if ((PredBlock = getEHPadFromPredecessor(PredBlock)))
2766 calculateExplicitSEHStateNumbers(FuncInfo, *PredBlock, ParentState);
2767 } else if (isa<CleanupPadInst>(FirstNonPHI)) {
2768 int CleanupState =
2769 addSEHHandler(FuncInfo, ParentState, /*Filter=*/nullptr, &BB);
2770 FuncInfo.EHPadStateMap[FirstNonPHI] = CleanupState;
2771 DEBUG(dbgs() << "Assigning state #" << CleanupState << " to BB "
2772 << BB.getName() << '\n');
2773 for (const BasicBlock *PredBlock : predecessors(&BB))
2774 if ((PredBlock = getEHPadFromPredecessor(PredBlock)))
2775 calculateExplicitSEHStateNumbers(FuncInfo, *PredBlock, CleanupState);
Reid Kleckner7bb20bd2015-09-10 21:46:36 +00002776 } else if (isa<CleanupEndPadInst>(FirstNonPHI)) {
2777 // Anything unwinding through CleanupEndPadInst is in ParentState.
2778 FuncInfo.EHPadStateMap[FirstNonPHI] = ParentState;
2779 DEBUG(dbgs() << "Assigning state #" << ParentState << " to BB "
2780 << BB.getName() << '\n');
2781 for (const BasicBlock *PredBlock : predecessors(&BB))
2782 if ((PredBlock = getEHPadFromPredecessor(PredBlock)))
2783 calculateExplicitSEHStateNumbers(FuncInfo, *PredBlock, ParentState);
Reid Kleckner94b704c2015-09-09 21:10:03 +00002784 } else if (isa<TerminatePadInst>(FirstNonPHI)) {
2785 report_fatal_error("Not yet implemented!");
2786 } else {
2787 llvm_unreachable("unexpected EH Pad!");
2788 }
2789}
2790
2791/// Check if the EH Pad unwinds to caller. Cleanups are a little bit of a
2792/// special case because we have to look at the cleanupret instruction that uses
2793/// the cleanuppad.
2794static bool doesEHPadUnwindToCaller(const Instruction *EHPad) {
2795 auto *CPI = dyn_cast<CleanupPadInst>(EHPad);
2796 if (!CPI)
2797 return EHPad->mayThrow();
2798
2799 // This cleanup does not return or unwind, so we say it unwinds to caller.
2800 if (CPI->use_empty())
2801 return true;
2802
2803 const Instruction *User = CPI->user_back();
2804 if (auto *CRI = dyn_cast<CleanupReturnInst>(User))
2805 return CRI->unwindsToCaller();
2806 return cast<CleanupEndPadInst>(User)->unwindsToCaller();
2807}
2808
Reid Kleckner813f1b62015-09-16 22:14:46 +00002809void llvm::calculateSEHStateNumbers(const Function *Fn,
Reid Kleckner94b704c2015-09-09 21:10:03 +00002810 WinEHFuncInfo &FuncInfo) {
2811 // Don't compute state numbers twice.
2812 if (!FuncInfo.SEHUnwindMap.empty())
2813 return;
2814
Reid Kleckner813f1b62015-09-16 22:14:46 +00002815 for (const BasicBlock &BB : *Fn) {
Reid Kleckner94b704c2015-09-09 21:10:03 +00002816 if (!BB.isEHPad() || !doesEHPadUnwindToCaller(BB.getFirstNonPHI()))
2817 continue;
2818 calculateExplicitSEHStateNumbers(FuncInfo, BB, -1);
2819 }
2820}
2821
Reid Kleckner813f1b62015-09-16 22:14:46 +00002822void llvm::calculateWinCXXEHStateNumbers(const Function *Fn,
Reid Klecknerfe4d4912015-05-28 22:00:24 +00002823 WinEHFuncInfo &FuncInfo) {
2824 // Return if it's already been done.
David Majnemer0ad363e2015-08-18 19:07:12 +00002825 if (!FuncInfo.EHPadStateMap.empty())
2826 return;
2827
Reid Kleckner813f1b62015-09-16 22:14:46 +00002828 for (const BasicBlock &BB : *Fn) {
David Majnemer0ad363e2015-08-18 19:07:12 +00002829 if (!BB.isEHPad())
2830 continue;
Reid Kleckner813f1b62015-09-16 22:14:46 +00002831 if (BB.isLandingPad())
2832 report_fatal_error("MSVC C++ EH cannot use landingpads");
Joseph Tremoulet9ce71f72015-09-03 09:09:43 +00002833 const Instruction *FirstNonPHI = BB.getFirstNonPHI();
2834 // Skip cleanupendpads; they are exits, not entries.
2835 if (isa<CleanupEndPadInst>(FirstNonPHI))
2836 continue;
Reid Kleckner94b704c2015-09-09 21:10:03 +00002837 if (!doesEHPadUnwindToCaller(FirstNonPHI))
David Majnemer0ad363e2015-08-18 19:07:12 +00002838 continue;
Reid Kleckner94b704c2015-09-09 21:10:03 +00002839 calculateExplicitCXXStateNumbers(FuncInfo, BB, -1);
David Majnemer0ad363e2015-08-18 19:07:12 +00002840 }
Reid Klecknerfe4d4912015-05-28 22:00:24 +00002841}
David Majnemerfd9f4772015-08-11 01:15:26 +00002842
David Majnemer67bff0d2015-09-16 20:42:16 +00002843void WinEHPrepare::replaceTerminatePadWithCleanup(Function &F) {
2844 if (Personality != EHPersonality::MSVC_CXX)
2845 return;
2846 for (BasicBlock &BB : F) {
2847 Instruction *First = BB.getFirstNonPHI();
2848 auto *TPI = dyn_cast<TerminatePadInst>(First);
2849 if (!TPI)
2850 continue;
2851
2852 if (TPI->getNumArgOperands() != 1)
2853 report_fatal_error(
2854 "Expected a unary terminatepad for MSVC C++ personalities!");
2855
2856 auto *TerminateFn = dyn_cast<Function>(TPI->getArgOperand(0));
2857 if (!TerminateFn)
2858 report_fatal_error("Function operand expected in terminatepad for MSVC "
2859 "C++ personalities!");
2860
2861 // Insert the cleanuppad instruction.
2862 auto *CPI = CleanupPadInst::Create(
2863 BB.getContext(), {}, Twine("terminatepad.for.", BB.getName()), &BB);
2864
2865 // Insert the call to the terminate instruction.
2866 auto *CallTerminate = CallInst::Create(TerminateFn, {}, &BB);
2867 CallTerminate->setDoesNotThrow();
2868 CallTerminate->setDoesNotReturn();
2869 CallTerminate->setCallingConv(TerminateFn->getCallingConv());
2870
2871 // Insert a new terminator for the cleanuppad using the same successor as
2872 // the terminatepad.
2873 CleanupReturnInst::Create(CPI, TPI->getUnwindDest(), &BB);
2874
2875 // Let's remove the terminatepad now that we've inserted the new
2876 // instructions.
2877 TPI->eraseFromParent();
2878 }
2879}
2880
Joseph Tremouletec182852015-08-28 01:12:35 +00002881void WinEHPrepare::colorFunclets(Function &F,
2882 SmallVectorImpl<BasicBlock *> &EntryBlocks) {
2883 SmallVector<std::pair<BasicBlock *, BasicBlock *>, 16> Worklist;
2884 BasicBlock *EntryBlock = &F.getEntryBlock();
David Majnemerfd9f4772015-08-11 01:15:26 +00002885
Joseph Tremouletec182852015-08-28 01:12:35 +00002886 // Build up the color map, which maps each block to its set of 'colors'.
2887 // For any block B, the "colors" of B are the set of funclets F (possibly
2888 // including a root "funclet" representing the main function), such that
2889 // F will need to directly contain B or a copy of B (where the term "directly
2890 // contain" is used to distinguish from being "transitively contained" in
2891 // a nested funclet).
2892 // Use a CFG walk driven by a worklist of (block, color) pairs. The "color"
2893 // sets attached during this processing to a block which is the entry of some
2894 // funclet F is actually the set of F's parents -- i.e. the union of colors
2895 // of all predecessors of F's entry. For all other blocks, the color sets
2896 // are as defined above. A post-pass fixes up the block color map to reflect
2897 // the same sense of "color" for funclet entries as for other blocks.
2898
2899 Worklist.push_back({EntryBlock, EntryBlock});
David Majnemerfd9f4772015-08-11 01:15:26 +00002900
2901 while (!Worklist.empty()) {
Joseph Tremouletec182852015-08-28 01:12:35 +00002902 BasicBlock *Visiting;
2903 BasicBlock *Color;
2904 std::tie(Visiting, Color) = Worklist.pop_back_val();
2905 Instruction *VisitingHead = Visiting->getFirstNonPHI();
Joseph Tremoulet9ce71f72015-09-03 09:09:43 +00002906 if (VisitingHead->isEHPad() && !isa<CatchEndPadInst>(VisitingHead) &&
2907 !isa<CleanupEndPadInst>(VisitingHead)) {
Joseph Tremouletec182852015-08-28 01:12:35 +00002908 // Mark this as a funclet head as a member of itself.
2909 FuncletBlocks[Visiting].insert(Visiting);
2910 // Queue exits with the parent color.
2911 for (User *Exit : VisitingHead->users()) {
2912 for (BasicBlock *Succ :
2913 successors(cast<Instruction>(Exit)->getParent())) {
2914 if (BlockColors[Succ].insert(Color).second) {
2915 Worklist.push_back({Succ, Color});
2916 }
2917 }
2918 }
2919 // Handle CatchPad specially since its successors need different colors.
2920 if (CatchPadInst *CatchPad = dyn_cast<CatchPadInst>(VisitingHead)) {
2921 // Visit the normal successor with the color of the new EH pad, and
2922 // visit the unwind successor with the color of the parent.
2923 BasicBlock *NormalSucc = CatchPad->getNormalDest();
2924 if (BlockColors[NormalSucc].insert(Visiting).second) {
2925 Worklist.push_back({NormalSucc, Visiting});
2926 }
2927 BasicBlock *UnwindSucc = CatchPad->getUnwindDest();
2928 if (BlockColors[UnwindSucc].insert(Color).second) {
2929 Worklist.push_back({UnwindSucc, Color});
2930 }
2931 continue;
2932 }
2933 // Switch color to the current node, except for terminate pads which
2934 // have no bodies and only unwind successors and so need their successors
2935 // visited with the color of the parent.
2936 if (!isa<TerminatePadInst>(VisitingHead))
2937 Color = Visiting;
2938 } else {
2939 // Note that this is a member of the given color.
2940 FuncletBlocks[Color].insert(Visiting);
Joseph Tremouletf3aff312015-09-10 16:51:25 +00002941 }
2942
2943 TerminatorInst *Terminator = Visiting->getTerminator();
2944 if (isa<CleanupReturnInst>(Terminator) ||
2945 isa<CatchReturnInst>(Terminator) ||
2946 isa<CleanupEndPadInst>(Terminator)) {
2947 // These blocks' successors have already been queued with the parent
2948 // color.
2949 continue;
David Majnemerfd9f4772015-08-11 01:15:26 +00002950 }
Joseph Tremouletec182852015-08-28 01:12:35 +00002951 for (BasicBlock *Succ : successors(Visiting)) {
2952 if (isa<CatchEndPadInst>(Succ->getFirstNonPHI())) {
2953 // The catchendpad needs to be visited with the parent's color, not
2954 // the current color. This will happen in the code above that visits
2955 // any catchpad unwind successor with the parent color, so we can
2956 // safely skip this successor here.
2957 continue;
2958 }
2959 if (BlockColors[Succ].insert(Color).second) {
2960 Worklist.push_back({Succ, Color});
2961 }
2962 }
2963 }
David Majnemerfd9f4772015-08-11 01:15:26 +00002964
Joseph Tremouletec182852015-08-28 01:12:35 +00002965 // The processing above actually accumulated the parent set for this
2966 // funclet into the color set for its entry; use the parent set to
2967 // populate the children map, and reset the color set to include just
2968 // the funclet itself (no instruction can target a funclet entry except on
2969 // that transitions to the child funclet).
2970 for (BasicBlock *FuncletEntry : EntryBlocks) {
2971 std::set<BasicBlock *> &ColorMapItem = BlockColors[FuncletEntry];
2972 for (BasicBlock *Parent : ColorMapItem)
2973 FuncletChildren[Parent].insert(FuncletEntry);
2974 ColorMapItem.clear();
2975 ColorMapItem.insert(FuncletEntry);
David Majnemerfd9f4772015-08-11 01:15:26 +00002976 }
2977}
2978
David Majnemerb3d9b962015-09-16 18:40:24 +00002979void WinEHPrepare::demotePHIsOnFunclets(Function &F) {
Joseph Tremouletc9ff9142015-08-13 14:30:10 +00002980 // Strip PHI nodes off of EH pads.
2981 SmallVector<PHINode *, 16> PHINodes;
David Majnemerfd9f4772015-08-11 01:15:26 +00002982 for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE;) {
2983 BasicBlock *BB = FI++;
David Majnemerfd9f4772015-08-11 01:15:26 +00002984 if (!BB->isEHPad())
2985 continue;
David Majnemerfd9f4772015-08-11 01:15:26 +00002986 for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE;) {
2987 Instruction *I = BI++;
2988 auto *PN = dyn_cast<PHINode>(I);
2989 // Stop at the first non-PHI.
2990 if (!PN)
2991 break;
David Majnemerfd9f4772015-08-11 01:15:26 +00002992
Joseph Tremouletc9ff9142015-08-13 14:30:10 +00002993 AllocaInst *SpillSlot = insertPHILoads(PN, F);
2994 if (SpillSlot)
2995 insertPHIStores(PN, SpillSlot);
2996
2997 PHINodes.push_back(PN);
David Majnemerfd9f4772015-08-11 01:15:26 +00002998 }
2999 }
3000
Joseph Tremouletc9ff9142015-08-13 14:30:10 +00003001 for (auto *PN : PHINodes) {
3002 // There may be lingering uses on other EH PHIs being removed
3003 PN->replaceAllUsesWith(UndefValue::get(PN->getType()));
3004 PN->eraseFromParent();
David Majnemerfd9f4772015-08-11 01:15:26 +00003005 }
David Majnemerb3d9b962015-09-16 18:40:24 +00003006}
David Majnemerfd9f4772015-08-11 01:15:26 +00003007
David Majnemerb3d9b962015-09-16 18:40:24 +00003008void WinEHPrepare::demoteUsesBetweenFunclets(Function &F) {
David Majnemerfd9f4772015-08-11 01:15:26 +00003009 // Turn all inter-funclet uses of a Value into loads and stores.
3010 for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE;) {
3011 BasicBlock *BB = FI++;
3012 std::set<BasicBlock *> &ColorsForBB = BlockColors[BB];
3013 for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE;) {
3014 Instruction *I = BI++;
3015 // Funclets are permitted to use static allocas.
3016 if (auto *AI = dyn_cast<AllocaInst>(I))
3017 if (AI->isStaticAlloca())
3018 continue;
3019
Joseph Tremouletc9ff9142015-08-13 14:30:10 +00003020 demoteNonlocalUses(I, ColorsForBB, F);
David Majnemerfd9f4772015-08-11 01:15:26 +00003021 }
3022 }
David Majnemerb3d9b962015-09-16 18:40:24 +00003023}
3024
3025void WinEHPrepare::demoteArgumentUses(Function &F) {
Joseph Tremouletc9ff9142015-08-13 14:30:10 +00003026 // Also demote function parameters used in funclets.
3027 std::set<BasicBlock *> &ColorsForEntry = BlockColors[&F.getEntryBlock()];
3028 for (Argument &Arg : F.args())
3029 demoteNonlocalUses(&Arg, ColorsForEntry, F);
David Majnemerb3d9b962015-09-16 18:40:24 +00003030}
David Majnemerfd9f4772015-08-11 01:15:26 +00003031
David Majnemerb3d9b962015-09-16 18:40:24 +00003032void WinEHPrepare::cloneCommonBlocks(
3033 Function &F, SmallVectorImpl<BasicBlock *> &EntryBlocks) {
David Majnemerfd9f4772015-08-11 01:15:26 +00003034 // We need to clone all blocks which belong to multiple funclets. Values are
3035 // remapped throughout the funclet to propogate both the new instructions
3036 // *and* the new basic blocks themselves.
Joseph Tremouletec182852015-08-28 01:12:35 +00003037 for (BasicBlock *FuncletPadBB : EntryBlocks) {
3038 std::set<BasicBlock *> &BlocksInFunclet = FuncletBlocks[FuncletPadBB];
David Majnemerfd9f4772015-08-11 01:15:26 +00003039
3040 std::map<BasicBlock *, BasicBlock *> Orig2Clone;
3041 ValueToValueMapTy VMap;
3042 for (BasicBlock *BB : BlocksInFunclet) {
3043 std::set<BasicBlock *> &ColorsForBB = BlockColors[BB];
3044 // We don't need to do anything if the block is monochromatic.
3045 size_t NumColorsForBB = ColorsForBB.size();
3046 if (NumColorsForBB == 1)
3047 continue;
3048
David Majnemerfd9f4772015-08-11 01:15:26 +00003049 // Create a new basic block and copy instructions into it!
Joseph Tremouletec182852015-08-28 01:12:35 +00003050 BasicBlock *CBB =
3051 CloneBasicBlock(BB, VMap, Twine(".for.", FuncletPadBB->getName()));
3052 // Insert the clone immediately after the original to ensure determinism
3053 // and to keep the same relative ordering of any funclet's blocks.
3054 CBB->insertInto(&F, BB->getNextNode());
David Majnemerfd9f4772015-08-11 01:15:26 +00003055
3056 // Add basic block mapping.
3057 VMap[BB] = CBB;
3058
3059 // Record delta operations that we need to perform to our color mappings.
3060 Orig2Clone[BB] = CBB;
3061 }
3062
3063 // Update our color mappings to reflect that one block has lost a color and
3064 // another has gained a color.
3065 for (auto &BBMapping : Orig2Clone) {
3066 BasicBlock *OldBlock = BBMapping.first;
3067 BasicBlock *NewBlock = BBMapping.second;
3068
3069 BlocksInFunclet.insert(NewBlock);
3070 BlockColors[NewBlock].insert(FuncletPadBB);
3071
3072 BlocksInFunclet.erase(OldBlock);
3073 BlockColors[OldBlock].erase(FuncletPadBB);
3074 }
3075
3076 // Loop over all of the instructions in the function, fixing up operand
3077 // references as we go. This uses VMap to do all the hard work.
3078 for (BasicBlock *BB : BlocksInFunclet)
3079 // Loop over all instructions, fixing each one as we find it...
3080 for (Instruction &I : *BB)
3081 RemapInstruction(&I, VMap, RF_IgnoreMissingEntries);
David Majnemer459a64a2015-09-16 18:40:37 +00003082
3083 // Check to see if SuccBB has PHI nodes. If so, we need to add entries to
3084 // the PHI nodes for NewBB now.
3085 for (auto &BBMapping : Orig2Clone) {
3086 BasicBlock *OldBlock = BBMapping.first;
3087 BasicBlock *NewBlock = BBMapping.second;
3088 for (BasicBlock *SuccBB : successors(NewBlock)) {
3089 for (Instruction &SuccI : *SuccBB) {
3090 auto *SuccPN = dyn_cast<PHINode>(&SuccI);
3091 if (!SuccPN)
3092 break;
3093
3094 // Ok, we have a PHI node. Figure out what the incoming value was for
3095 // the OldBlock.
3096 int OldBlockIdx = SuccPN->getBasicBlockIndex(OldBlock);
3097 if (OldBlockIdx == -1)
3098 break;
3099 Value *IV = SuccPN->getIncomingValue(OldBlockIdx);
3100
3101 // Remap the value if necessary.
3102 if (auto *Inst = dyn_cast<Instruction>(IV)) {
3103 ValueToValueMapTy::iterator I = VMap.find(Inst);
3104 if (I != VMap.end())
3105 IV = I->second;
3106 }
3107
3108 SuccPN->addIncoming(IV, NewBlock);
3109 }
3110 }
3111 }
3112
3113 for (ValueToValueMapTy::value_type VT : VMap) {
3114 // If there were values defined in BB that are used outside the funclet,
3115 // then we now have to update all uses of the value to use either the
3116 // original value, the cloned value, or some PHI derived value. This can
3117 // require arbitrary PHI insertion, of which we are prepared to do, clean
3118 // these up now.
3119 SmallVector<Use *, 16> UsesToRename;
3120
3121 auto *OldI = dyn_cast<Instruction>(const_cast<Value *>(VT.first));
3122 if (!OldI)
3123 continue;
3124 auto *NewI = cast<Instruction>(VT.second);
3125 // Scan all uses of this instruction to see if it is used outside of its
3126 // funclet, and if so, record them in UsesToRename.
3127 for (Use &U : OldI->uses()) {
3128 Instruction *UserI = cast<Instruction>(U.getUser());
3129 BasicBlock *UserBB = UserI->getParent();
3130 std::set<BasicBlock *> &ColorsForUserBB = BlockColors[UserBB];
3131 assert(!ColorsForUserBB.empty());
3132 if (ColorsForUserBB.size() > 1 ||
3133 *ColorsForUserBB.begin() != FuncletPadBB)
3134 UsesToRename.push_back(&U);
3135 }
3136
3137 // If there are no uses outside the block, we're done with this
3138 // instruction.
3139 if (UsesToRename.empty())
3140 continue;
3141
3142 // We found a use of OldI outside of the funclet. Rename all uses of OldI
3143 // that are outside its funclet to be uses of the appropriate PHI node
3144 // etc.
3145 SSAUpdater SSAUpdate;
3146 SSAUpdate.Initialize(OldI->getType(), OldI->getName());
3147 SSAUpdate.AddAvailableValue(OldI->getParent(), OldI);
3148 SSAUpdate.AddAvailableValue(NewI->getParent(), NewI);
3149
3150 while (!UsesToRename.empty())
3151 SSAUpdate.RewriteUseAfterInsertions(*UsesToRename.pop_back_val());
3152 }
David Majnemerfd9f4772015-08-11 01:15:26 +00003153 }
David Majnemerb3d9b962015-09-16 18:40:24 +00003154}
David Majnemerfd9f4772015-08-11 01:15:26 +00003155
David Majnemerb3d9b962015-09-16 18:40:24 +00003156void WinEHPrepare::removeImplausibleTerminators(Function &F) {
David Majnemer83f4bb22015-08-17 20:56:39 +00003157 // Remove implausible terminators and replace them with UnreachableInst.
3158 for (auto &Funclet : FuncletBlocks) {
3159 BasicBlock *FuncletPadBB = Funclet.first;
3160 std::set<BasicBlock *> &BlocksInFunclet = Funclet.second;
3161 Instruction *FirstNonPHI = FuncletPadBB->getFirstNonPHI();
3162 auto *CatchPad = dyn_cast<CatchPadInst>(FirstNonPHI);
3163 auto *CleanupPad = dyn_cast<CleanupPadInst>(FirstNonPHI);
3164
3165 for (BasicBlock *BB : BlocksInFunclet) {
3166 TerminatorInst *TI = BB->getTerminator();
3167 // CatchPadInst and CleanupPadInst can't transfer control to a ReturnInst.
3168 bool IsUnreachableRet = isa<ReturnInst>(TI) && (CatchPad || CleanupPad);
3169 // The token consumed by a CatchReturnInst must match the funclet token.
3170 bool IsUnreachableCatchret = false;
3171 if (auto *CRI = dyn_cast<CatchReturnInst>(TI))
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +00003172 IsUnreachableCatchret = CRI->getCatchPad() != CatchPad;
Joseph Tremoulet9ce71f72015-09-03 09:09:43 +00003173 // The token consumed by a CleanupReturnInst must match the funclet token.
David Majnemer83f4bb22015-08-17 20:56:39 +00003174 bool IsUnreachableCleanupret = false;
3175 if (auto *CRI = dyn_cast<CleanupReturnInst>(TI))
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +00003176 IsUnreachableCleanupret = CRI->getCleanupPad() != CleanupPad;
Joseph Tremoulet9ce71f72015-09-03 09:09:43 +00003177 // The token consumed by a CleanupEndPadInst must match the funclet token.
3178 bool IsUnreachableCleanupendpad = false;
3179 if (auto *CEPI = dyn_cast<CleanupEndPadInst>(TI))
3180 IsUnreachableCleanupendpad = CEPI->getCleanupPad() != CleanupPad;
3181 if (IsUnreachableRet || IsUnreachableCatchret ||
3182 IsUnreachableCleanupret || IsUnreachableCleanupendpad) {
David Majnemer459a64a2015-09-16 18:40:37 +00003183 // Loop through all of our successors and make sure they know that one
3184 // of their predecessors is going away.
3185 for (BasicBlock *SuccBB : TI->successors())
3186 SuccBB->removePredecessor(BB);
3187
Joseph Tremoulet09af67a2015-09-27 01:47:46 +00003188 if (IsUnreachableCleanupendpad) {
3189 // We can't simply replace a cleanupendpad with unreachable, because
3190 // its predecessor edges are EH edges and unreachable is not an EH
3191 // pad. Change all predecessors to the "unwind to caller" form.
3192 for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
3193 PI != PE;) {
3194 BasicBlock *Pred = *PI++;
3195 removeUnwindEdge(Pred);
3196 }
3197 }
3198
David Majnemer83f4bb22015-08-17 20:56:39 +00003199 new UnreachableInst(BB->getContext(), TI);
3200 TI->eraseFromParent();
3201 }
Joseph Tremoulet09af67a2015-09-27 01:47:46 +00003202 // FIXME: Check for invokes/cleanuprets/cleanupendpads which unwind to
3203 // implausible catchendpads (i.e. catchendpad not in immediate parent
3204 // funclet).
David Majnemer83f4bb22015-08-17 20:56:39 +00003205 }
3206 }
David Majnemerb3d9b962015-09-16 18:40:24 +00003207}
David Majnemer83f4bb22015-08-17 20:56:39 +00003208
David Majnemerb3d9b962015-09-16 18:40:24 +00003209void WinEHPrepare::cleanupPreparedFunclets(Function &F) {
David Majnemerfd9f4772015-08-11 01:15:26 +00003210 // Clean-up some of the mess we made by removing useles PHI nodes, trivial
3211 // branches, etc.
3212 for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE;) {
3213 BasicBlock *BB = FI++;
3214 SimplifyInstructionsInBlock(BB);
3215 ConstantFoldTerminator(BB, /*DeleteDeadConditions=*/true);
3216 MergeBlockIntoPredecessor(BB);
3217 }
3218
David Majnemerfd9f4772015-08-11 01:15:26 +00003219 // We might have some unreachable blocks after cleaning up some impossible
3220 // control flow.
3221 removeUnreachableBlocks(F);
David Majnemerb3d9b962015-09-16 18:40:24 +00003222}
David Majnemerfd9f4772015-08-11 01:15:26 +00003223
David Majnemerb3d9b962015-09-16 18:40:24 +00003224void WinEHPrepare::verifyPreparedFunclets(Function &F) {
David Majnemerfd9f4772015-08-11 01:15:26 +00003225 // Recolor the CFG to verify that all is well.
3226 for (BasicBlock &BB : F) {
3227 size_t NumColors = BlockColors[&BB].size();
3228 assert(NumColors == 1 && "Expected monochromatic BB!");
3229 if (NumColors == 0)
3230 report_fatal_error("Uncolored BB!");
3231 if (NumColors > 1)
3232 report_fatal_error("Multicolor BB!");
David Majnemer459a64a2015-09-16 18:40:37 +00003233 if (!DisableDemotion) {
3234 bool EHPadHasPHI = BB.isEHPad() && isa<PHINode>(BB.begin());
3235 assert(!EHPadHasPHI && "EH Pad still has a PHI!");
3236 if (EHPadHasPHI)
3237 report_fatal_error("EH Pad still has a PHI!");
3238 }
David Majnemerfd9f4772015-08-11 01:15:26 +00003239 }
David Majnemerb3d9b962015-09-16 18:40:24 +00003240}
3241
3242bool WinEHPrepare::prepareExplicitEH(
3243 Function &F, SmallVectorImpl<BasicBlock *> &EntryBlocks) {
3244 // Remove unreachable blocks. It is not valuable to assign them a color and
3245 // their existence can trick us into thinking values are alive when they are
3246 // not.
3247 removeUnreachableBlocks(F);
3248
David Majnemer67bff0d2015-09-16 20:42:16 +00003249 replaceTerminatePadWithCleanup(F);
3250
David Majnemerb3d9b962015-09-16 18:40:24 +00003251 // Determine which blocks are reachable from which funclet entries.
3252 colorFunclets(F, EntryBlocks);
3253
David Majnemer459a64a2015-09-16 18:40:37 +00003254 if (!DisableDemotion) {
3255 demotePHIsOnFunclets(F);
David Majnemerb3d9b962015-09-16 18:40:24 +00003256
David Majnemer459a64a2015-09-16 18:40:37 +00003257 demoteUsesBetweenFunclets(F);
David Majnemerb3d9b962015-09-16 18:40:24 +00003258
David Majnemer459a64a2015-09-16 18:40:37 +00003259 demoteArgumentUses(F);
3260 }
David Majnemerb3d9b962015-09-16 18:40:24 +00003261
3262 cloneCommonBlocks(F, EntryBlocks);
3263
David Majnemer459a64a2015-09-16 18:40:37 +00003264 if (!DisableCleanups) {
3265 removeImplausibleTerminators(F);
David Majnemerb3d9b962015-09-16 18:40:24 +00003266
David Majnemer459a64a2015-09-16 18:40:37 +00003267 cleanupPreparedFunclets(F);
3268 }
David Majnemerb3d9b962015-09-16 18:40:24 +00003269
3270 verifyPreparedFunclets(F);
David Majnemerfd9f4772015-08-11 01:15:26 +00003271
3272 BlockColors.clear();
3273 FuncletBlocks.clear();
Joseph Tremouletec182852015-08-28 01:12:35 +00003274 FuncletChildren.clear();
David Majnemer0ad363e2015-08-18 19:07:12 +00003275
David Majnemerfd9f4772015-08-11 01:15:26 +00003276 return true;
3277}
Joseph Tremouletc9ff9142015-08-13 14:30:10 +00003278
3279// TODO: Share loads when one use dominates another, or when a catchpad exit
3280// dominates uses (needs dominators).
3281AllocaInst *WinEHPrepare::insertPHILoads(PHINode *PN, Function &F) {
3282 BasicBlock *PHIBlock = PN->getParent();
3283 AllocaInst *SpillSlot = nullptr;
3284
3285 if (isa<CleanupPadInst>(PHIBlock->getFirstNonPHI())) {
3286 // Insert a load in place of the PHI and replace all uses.
3287 SpillSlot = new AllocaInst(PN->getType(), nullptr,
3288 Twine(PN->getName(), ".wineh.spillslot"),
3289 F.getEntryBlock().begin());
3290 Value *V = new LoadInst(SpillSlot, Twine(PN->getName(), ".wineh.reload"),
3291 PHIBlock->getFirstInsertionPt());
3292 PN->replaceAllUsesWith(V);
3293 return SpillSlot;
3294 }
3295
3296 DenseMap<BasicBlock *, Value *> Loads;
3297 for (Value::use_iterator UI = PN->use_begin(), UE = PN->use_end();
3298 UI != UE;) {
3299 Use &U = *UI++;
3300 auto *UsingInst = cast<Instruction>(U.getUser());
3301 BasicBlock *UsingBB = UsingInst->getParent();
3302 if (UsingBB->isEHPad()) {
3303 // Use is on an EH pad phi. Leave it alone; we'll insert loads and
3304 // stores for it separately.
3305 assert(isa<PHINode>(UsingInst));
3306 continue;
3307 }
3308 replaceUseWithLoad(PN, U, SpillSlot, Loads, F);
3309 }
3310 return SpillSlot;
3311}
3312
3313// TODO: improve store placement. Inserting at def is probably good, but need
3314// to be careful not to introduce interfering stores (needs liveness analysis).
3315// TODO: identify related phi nodes that can share spill slots, and share them
3316// (also needs liveness).
3317void WinEHPrepare::insertPHIStores(PHINode *OriginalPHI,
3318 AllocaInst *SpillSlot) {
3319 // Use a worklist of (Block, Value) pairs -- the given Value needs to be
3320 // stored to the spill slot by the end of the given Block.
3321 SmallVector<std::pair<BasicBlock *, Value *>, 4> Worklist;
3322
3323 Worklist.push_back({OriginalPHI->getParent(), OriginalPHI});
3324
3325 while (!Worklist.empty()) {
3326 BasicBlock *EHBlock;
3327 Value *InVal;
3328 std::tie(EHBlock, InVal) = Worklist.pop_back_val();
3329
3330 PHINode *PN = dyn_cast<PHINode>(InVal);
3331 if (PN && PN->getParent() == EHBlock) {
3332 // The value is defined by another PHI we need to remove, with no room to
3333 // insert a store after the PHI, so each predecessor needs to store its
3334 // incoming value.
3335 for (unsigned i = 0, e = PN->getNumIncomingValues(); i < e; ++i) {
3336 Value *PredVal = PN->getIncomingValue(i);
3337
3338 // Undef can safely be skipped.
3339 if (isa<UndefValue>(PredVal))
3340 continue;
3341
3342 insertPHIStore(PN->getIncomingBlock(i), PredVal, SpillSlot, Worklist);
3343 }
3344 } else {
3345 // We need to store InVal, which dominates EHBlock, but can't put a store
3346 // in EHBlock, so need to put stores in each predecessor.
3347 for (BasicBlock *PredBlock : predecessors(EHBlock)) {
3348 insertPHIStore(PredBlock, InVal, SpillSlot, Worklist);
3349 }
3350 }
3351 }
3352}
3353
3354void WinEHPrepare::insertPHIStore(
3355 BasicBlock *PredBlock, Value *PredVal, AllocaInst *SpillSlot,
3356 SmallVectorImpl<std::pair<BasicBlock *, Value *>> &Worklist) {
3357
3358 if (PredBlock->isEHPad() &&
3359 !isa<CleanupPadInst>(PredBlock->getFirstNonPHI())) {
3360 // Pred is unsplittable, so we need to queue it on the worklist.
3361 Worklist.push_back({PredBlock, PredVal});
3362 return;
3363 }
3364
3365 // Otherwise, insert the store at the end of the basic block.
3366 new StoreInst(PredVal, SpillSlot, PredBlock->getTerminator());
3367}
3368
3369// TODO: Share loads for same-funclet uses (requires dominators if funclets
3370// aren't properly nested).
3371void WinEHPrepare::demoteNonlocalUses(Value *V,
3372 std::set<BasicBlock *> &ColorsForBB,
3373 Function &F) {
David Majnemer83f4bb22015-08-17 20:56:39 +00003374 // Tokens can only be used non-locally due to control flow involving
3375 // unreachable edges. Don't try to demote the token usage, we'll simply
3376 // delete the cloned user later.
3377 if (isa<CatchPadInst>(V) || isa<CleanupPadInst>(V))
3378 return;
3379
Joseph Tremouletc9ff9142015-08-13 14:30:10 +00003380 DenseMap<BasicBlock *, Value *> Loads;
3381 AllocaInst *SpillSlot = nullptr;
3382 for (Value::use_iterator UI = V->use_begin(), UE = V->use_end(); UI != UE;) {
3383 Use &U = *UI++;
3384 auto *UsingInst = cast<Instruction>(U.getUser());
3385 BasicBlock *UsingBB = UsingInst->getParent();
3386
Joseph Tremouletec182852015-08-28 01:12:35 +00003387 // Is the Use inside a block which is colored the same as the Def?
Joseph Tremouletc9ff9142015-08-13 14:30:10 +00003388 // If so, we don't need to escape the Def because we will clone
3389 // ourselves our own private copy.
3390 std::set<BasicBlock *> &ColorsForUsingBB = BlockColors[UsingBB];
Joseph Tremouletec182852015-08-28 01:12:35 +00003391 if (ColorsForUsingBB == ColorsForBB)
Joseph Tremouletc9ff9142015-08-13 14:30:10 +00003392 continue;
3393
3394 replaceUseWithLoad(V, U, SpillSlot, Loads, F);
3395 }
3396 if (SpillSlot) {
3397 // Insert stores of the computed value into the stack slot.
3398 // We have to be careful if I is an invoke instruction,
3399 // because we can't insert the store AFTER the terminator instruction.
3400 BasicBlock::iterator InsertPt;
3401 if (isa<Argument>(V)) {
3402 InsertPt = F.getEntryBlock().getTerminator();
3403 } else if (isa<TerminatorInst>(V)) {
3404 auto *II = cast<InvokeInst>(V);
3405 // We cannot demote invoke instructions to the stack if their normal
3406 // edge is critical. Therefore, split the critical edge and create a
3407 // basic block into which the store can be inserted.
3408 if (!II->getNormalDest()->getSinglePredecessor()) {
3409 unsigned SuccNum =
3410 GetSuccessorNumber(II->getParent(), II->getNormalDest());
3411 assert(isCriticalEdge(II, SuccNum) && "Expected a critical edge!");
3412 BasicBlock *NewBlock = SplitCriticalEdge(II, SuccNum);
3413 assert(NewBlock && "Unable to split critical edge.");
3414 // Update the color mapping for the newly split edge.
3415 std::set<BasicBlock *> &ColorsForUsingBB = BlockColors[II->getParent()];
3416 BlockColors[NewBlock] = ColorsForUsingBB;
3417 for (BasicBlock *FuncletPad : ColorsForUsingBB)
3418 FuncletBlocks[FuncletPad].insert(NewBlock);
3419 }
3420 InsertPt = II->getNormalDest()->getFirstInsertionPt();
3421 } else {
3422 InsertPt = cast<Instruction>(V);
3423 ++InsertPt;
3424 // Don't insert before PHI nodes or EH pad instrs.
3425 for (; isa<PHINode>(InsertPt) || InsertPt->isEHPad(); ++InsertPt)
3426 ;
3427 }
3428 new StoreInst(V, SpillSlot, InsertPt);
3429 }
3430}
3431
3432void WinEHPrepare::replaceUseWithLoad(Value *V, Use &U, AllocaInst *&SpillSlot,
3433 DenseMap<BasicBlock *, Value *> &Loads,
3434 Function &F) {
3435 // Lazilly create the spill slot.
3436 if (!SpillSlot)
3437 SpillSlot = new AllocaInst(V->getType(), nullptr,
3438 Twine(V->getName(), ".wineh.spillslot"),
3439 F.getEntryBlock().begin());
3440
3441 auto *UsingInst = cast<Instruction>(U.getUser());
3442 if (auto *UsingPHI = dyn_cast<PHINode>(UsingInst)) {
3443 // If this is a PHI node, we can't insert a load of the value before
3444 // the use. Instead insert the load in the predecessor block
3445 // corresponding to the incoming value.
3446 //
3447 // Note that if there are multiple edges from a basic block to this
3448 // PHI node that we cannot have multiple loads. The problem is that
3449 // the resulting PHI node will have multiple values (from each load)
3450 // coming in from the same block, which is illegal SSA form.
3451 // For this reason, we keep track of and reuse loads we insert.
3452 BasicBlock *IncomingBlock = UsingPHI->getIncomingBlock(U);
Joseph Tremoulet7031c9f2015-08-17 13:51:37 +00003453 if (auto *CatchRet =
3454 dyn_cast<CatchReturnInst>(IncomingBlock->getTerminator())) {
3455 // Putting a load above a catchret and use on the phi would still leave
3456 // a cross-funclet def/use. We need to split the edge, change the
3457 // catchret to target the new block, and put the load there.
3458 BasicBlock *PHIBlock = UsingInst->getParent();
3459 BasicBlock *NewBlock = SplitEdge(IncomingBlock, PHIBlock);
3460 // SplitEdge gives us:
3461 // IncomingBlock:
3462 // ...
3463 // br label %NewBlock
3464 // NewBlock:
3465 // catchret label %PHIBlock
3466 // But we need:
3467 // IncomingBlock:
3468 // ...
3469 // catchret label %NewBlock
3470 // NewBlock:
3471 // br label %PHIBlock
3472 // So move the terminators to each others' blocks and swap their
3473 // successors.
3474 BranchInst *Goto = cast<BranchInst>(IncomingBlock->getTerminator());
3475 Goto->removeFromParent();
3476 CatchRet->removeFromParent();
3477 IncomingBlock->getInstList().push_back(CatchRet);
3478 NewBlock->getInstList().push_back(Goto);
3479 Goto->setSuccessor(0, PHIBlock);
3480 CatchRet->setSuccessor(NewBlock);
3481 // Update the color mapping for the newly split edge.
3482 std::set<BasicBlock *> &ColorsForPHIBlock = BlockColors[PHIBlock];
3483 BlockColors[NewBlock] = ColorsForPHIBlock;
3484 for (BasicBlock *FuncletPad : ColorsForPHIBlock)
3485 FuncletBlocks[FuncletPad].insert(NewBlock);
3486 // Treat the new block as incoming for load insertion.
3487 IncomingBlock = NewBlock;
3488 }
Joseph Tremouletc9ff9142015-08-13 14:30:10 +00003489 Value *&Load = Loads[IncomingBlock];
3490 // Insert the load into the predecessor block
3491 if (!Load)
3492 Load = new LoadInst(SpillSlot, Twine(V->getName(), ".wineh.reload"),
3493 /*Volatile=*/false, IncomingBlock->getTerminator());
3494
3495 U.set(Load);
3496 } else {
3497 // Reload right before the old use.
3498 auto *Load = new LoadInst(SpillSlot, Twine(V->getName(), ".wineh.reload"),
3499 /*Volatile=*/false, UsingInst);
3500 U.set(Load);
3501 }
3502}
Reid Klecknerc71d6272015-09-28 23:56:30 +00003503
3504void WinEHFuncInfo::addIPToStateRange(const BasicBlock *PadBB,
3505 MCSymbol *InvokeBegin,
3506 MCSymbol *InvokeEnd) {
3507 assert(PadBB->isEHPad() && EHPadStateMap.count(PadBB->getFirstNonPHI()) &&
3508 "should get EH pad BB with precomputed state");
3509 InvokeToStateMap[InvokeBegin] =
3510 std::make_pair(EHPadStateMap[PadBB->getFirstNonPHI()], InvokeEnd);
3511}