1//===-- WinEHPrepare - Prepare exception handling for code generation ---===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This pass lowers LLVM IR exception handling into something closer to what the
10// backend wants for functions using a personality function from a runtime
11// provided by MSVC. Functions with other personality functions are left alone
12// and may be prepared by other passes. In particular, all supported MSVC
13// personality functions require cleanup code to be outlined, and the C++
14// personality requires catch handler code to be outlined.
15//
16//===----------------------------------------------------------------------===//
17
18#include "llvm/CodeGen/WinEHPrepare.h"
19#include "CodeGenOptions.h"
20#include "llvm/ADT/DenseMap.h"
21#include "llvm/ADT/MapVector.h"
22#include "llvm/ADT/STLExtras.h"
23#include "llvm/CodeGen/MachineBasicBlock.h"
24#include "llvm/CodeGen/Passes.h"
25#include "llvm/CodeGen/WinEHFuncInfo.h"
26#include "llvm/IR/Constants.h"
27#include "llvm/IR/DiagnosticInfo.h"
28#include "llvm/IR/EHPersonalities.h"
29#include "llvm/IR/IRBuilder.h"
30#include "llvm/IR/Instructions.h"
31#include "llvm/IR/Module.h"
32#include "llvm/IR/Verifier.h"
33#include "llvm/InitializePasses.h"
34#include "llvm/Pass.h"
35#include "llvm/Support/Debug.h"
36#include "llvm/Support/raw_ostream.h"
37#include "llvm/TargetParser/Triple.h"
38#include "llvm/Transforms/Utils/BasicBlockUtils.h"
39#include "llvm/Transforms/Utils/Cloning.h"
40#include "llvm/Transforms/Utils/Local.h"
41#include "llvm/Transforms/Utils/SSAUpdater.h"
42
43using namespace llvm;
44
45#define DEBUG_TYPE "win-eh-prepare"
46
47static bool isMalformedCatchpad(const CatchPadInst *CPI,
48 EHPersonality Personality) {
49 switch (Personality) {
50 case EHPersonality::MSVC_CXX: {
51 if (CPI->arg_size() != 3)
52 return true;
53
54 Constant *TypeInfo = dyn_cast<Constant>(Val: CPI->getArgOperand(i: 0));
55 if (!TypeInfo)
56 return true;
57 if (!TypeInfo->isNullValue() &&
58 !isa<GlobalVariable>(Val: TypeInfo->stripPointerCasts()))
59 return true;
60
61 if (!isa<ConstantInt>(Val: CPI->getArgOperand(i: 1)))
62 return true;
63
64 return false;
65 }
66 case EHPersonality::MSVC_X86SEH:
67 case EHPersonality::MSVC_TableSEH: {
68 if (CPI->arg_size() == 0)
69 return true;
70
71 Constant *FilterOrNull = dyn_cast<Constant>(Val: CPI->getArgOperand(i: 0));
72 if (!FilterOrNull)
73 return true;
74
75 Constant *Filter = FilterOrNull->stripPointerCasts();
76 if (!Filter->isNullValue() && !isa<Function>(Val: Filter))
77 return true;
78
79 return false;
80 }
81 case EHPersonality::CoreCLR: {
82 if (CPI->arg_size() == 0)
83 return true;
84
85 if (!isa<ConstantInt>(Val: CPI->getArgOperand(i: 0)))
86 return true;
87
88 return false;
89 }
90 case EHPersonality::Wasm_CXX:
91 case EHPersonality::Wasm_D:
92 if (CPI->arg_size() == 1 && !isa<Constant>(Val: CPI->getArgOperand(i: 0)))
93 return true;
94
95 return false;
96 default:
97 llvm_unreachable("Unsupported Personality for WinEH");
98 }
99}
100
101namespace {
102
103class WinEHPrepareImpl {
104public:
105 explicit WinEHPrepareImpl(const CodeGenOptions &Opts) : Opts(Opts) {}
106 bool runOnFunction(Function &Fn);
107
108private:
109 void insertPHIStores(PHINode *OriginalPHI, AllocaInst *SpillSlot);
110 void
111 insertPHIStore(BasicBlock *PredBlock, Value *PredVal, AllocaInst *SpillSlot,
112 SmallVectorImpl<std::pair<BasicBlock *, Value *>> &Worklist);
113 AllocaInst *insertPHILoads(PHINode *PN, Function &F);
114 void replaceUseWithLoad(Value *V, Use &U, AllocaInst *&SpillSlot,
115 DenseMap<BasicBlock *, Value *> &Loads, Function &F);
116 bool prepareExplicitEH(Function &F);
117 void colorFunclets(Function &F);
118
119 bool demotePHIsOnFunclets(Function &F, bool DemoteCatchSwitchPHIOnly);
120 bool cloneCommonBlocks(Function &F);
121 bool removeMalformedCatchswitch(Value *FuncletToken);
122 bool removeImplausibleInstructions(Function &F);
123 bool cleanupPreparedFunclets(Function &F);
124 void verifyPreparedFunclets(Function &F);
125
126 const CodeGenOptions &Opts;
127
128 // True for Wasm C++ personalities.
129 bool DemoteCatchSwitchPHIOnly = false;
130
131 // All fields are reset by runOnFunction.
132 EHPersonality Personality = EHPersonality::Unknown;
133
134 const DataLayout *DL = nullptr;
135 DenseMap<BasicBlock *, ColorVector> BlockColors;
136 MapVector<BasicBlock *, std::vector<BasicBlock *>> FuncletBlocks;
137};
138
139class WinEHPrepare : public FunctionPass {
140public:
141 static char ID; // Pass identification, replacement for typeid.
142
143 WinEHPrepare() : FunctionPass(ID) {}
144
145 StringRef getPassName() const override {
146 return "Windows exception handling preparation";
147 }
148
149 bool runOnFunction(Function &Fn) override {
150 return WinEHPrepareImpl(getCodeGenOptions(F: Fn)).runOnFunction(Fn);
151 }
152};
153
154} // end anonymous namespace
155
156PreservedAnalyses WinEHPreparePass::run(Function &F,
157 FunctionAnalysisManager &) {
158 bool Changed = WinEHPrepareImpl(getCodeGenOptions(F)).runOnFunction(Fn&: F);
159 return Changed ? PreservedAnalyses::none() : PreservedAnalyses::all();
160}
161
162char WinEHPrepare::ID = 0;
163INITIALIZE_PASS(WinEHPrepare, DEBUG_TYPE, "Prepare Windows exceptions", false,
164 false)
165
166FunctionPass *llvm::createWinEHPass() { return new WinEHPrepare(); }
167
168bool WinEHPrepareImpl::runOnFunction(Function &Fn) {
169 if (!Fn.hasPersonalityFn())
170 return false;
171
172 // Classify the personality to see what kind of preparation we need.
173 Personality = classifyEHPersonality(Pers: Fn.getPersonalityFn());
174
175 // Do nothing if this is not a scope-based personality.
176 if (!isScopedEHPersonality(Pers: Personality))
177 return false;
178
179 // Funclet personalities outline catch/cleanup bodies, so every funclet PHI
180 // must be demoted. A scoped-but-non-funclet personality (Wasm) keeps its pads
181 // inline and only needs the catchswitch dispatch PHIs demoted.
182 DemoteCatchSwitchPHIOnly = !isFuncletEHPersonality(Pers: Personality);
183
184 DL = &Fn.getDataLayout();
185 return prepareExplicitEH(F&: Fn);
186}
187
188static int addUnwindMapEntry(WinEHFuncInfo &FuncInfo, int ToState,
189 const BasicBlock *BB) {
190 CxxUnwindMapEntry UME;
191 UME.ToState = ToState;
192 UME.Cleanup = BB;
193 FuncInfo.CxxUnwindMap.push_back(Elt: UME);
194 return FuncInfo.getLastStateNumber();
195}
196
197static void addTryBlockMapEntry(WinEHFuncInfo &FuncInfo, int TryLow,
198 int TryHigh, int CatchHigh,
199 ArrayRef<const CatchPadInst *> Handlers) {
200 WinEHTryBlockMapEntry TBME;
201 TBME.TryLow = TryLow;
202 TBME.TryHigh = TryHigh;
203 TBME.CatchHigh = CatchHigh;
204 assert(TBME.TryLow <= TBME.TryHigh);
205 for (const CatchPadInst *CPI : Handlers) {
206 WinEHHandlerType HT;
207 assert(!isMalformedCatchpad(CPI, EHPersonality::MSVC_CXX) &&
208 "Malformed CatchPadInst not caught by win-eh-prepare");
209 Constant *TypeInfo = cast<Constant>(Val: CPI->getArgOperand(i: 0));
210 if (TypeInfo->isNullValue())
211 HT.TypeDescriptor = nullptr;
212 else
213 HT.TypeDescriptor = cast<GlobalVariable>(Val: TypeInfo->stripPointerCasts());
214 HT.Adjectives = cast<ConstantInt>(Val: CPI->getArgOperand(i: 1))->getZExtValue();
215 HT.Handler = CPI->getParent();
216 if (auto *AI =
217 dyn_cast<AllocaInst>(Val: CPI->getArgOperand(i: 2)->stripPointerCasts()))
218 HT.CatchObj.Alloca = AI;
219 else
220 HT.CatchObj.Alloca = nullptr;
221 TBME.HandlerArray.push_back(Elt: HT);
222 }
223 FuncInfo.TryBlockMap.push_back(Elt: TBME);
224}
225
226static BasicBlock *getCleanupRetUnwindDest(const CleanupPadInst *CleanupPad) {
227 for (const User *U : CleanupPad->users())
228 if (const auto *CRI = dyn_cast<CleanupReturnInst>(Val: U))
229 return CRI->getUnwindDest();
230 return nullptr;
231}
232
233static void calculateStateNumbersForInvokes(const Function *Fn,
234 WinEHFuncInfo &FuncInfo) {
235 auto *F = const_cast<Function *>(Fn);
236 DenseMap<BasicBlock *, ColorVector> BlockColors = colorEHFunclets(F&: *F);
237 for (BasicBlock &BB : *F) {
238 auto *II = dyn_cast<InvokeInst>(Val: BB.getTerminator());
239 if (!II)
240 continue;
241
242 auto &BBColors = BlockColors[&BB];
243 assert(BBColors.size() == 1 && "multi-color BB not removed by preparation");
244 BasicBlock *FuncletEntryBB = BBColors.front();
245
246 BasicBlock *FuncletUnwindDest;
247 auto *FuncletPad =
248 dyn_cast<FuncletPadInst>(Val: FuncletEntryBB->getFirstNonPHIIt());
249 assert(FuncletPad || FuncletEntryBB == &Fn->getEntryBlock());
250 if (!FuncletPad)
251 FuncletUnwindDest = nullptr;
252 else if (auto *CatchPad = dyn_cast<CatchPadInst>(Val: FuncletPad))
253 FuncletUnwindDest = CatchPad->getCatchSwitch()->getUnwindDest();
254 else if (auto *CleanupPad = dyn_cast<CleanupPadInst>(Val: FuncletPad))
255 FuncletUnwindDest = getCleanupRetUnwindDest(CleanupPad);
256 else
257 llvm_unreachable("unexpected funclet pad!");
258
259 BasicBlock *InvokeUnwindDest = II->getUnwindDest();
260 int BaseState = -1;
261 if (FuncletUnwindDest == InvokeUnwindDest) {
262 auto BaseStateI = FuncInfo.FuncletBaseStateMap.find(Val: FuncletPad);
263 if (BaseStateI != FuncInfo.FuncletBaseStateMap.end())
264 BaseState = BaseStateI->second;
265 }
266
267 if (BaseState != -1) {
268 FuncInfo.InvokeStateMap[II] = BaseState;
269 } else {
270 Instruction *PadInst = &*InvokeUnwindDest->getFirstNonPHIIt();
271 assert(FuncInfo.EHPadStateMap.count(PadInst) && "EH Pad has no state!");
272 FuncInfo.InvokeStateMap[II] = FuncInfo.EHPadStateMap[PadInst];
273 }
274 }
275}
276
277// See comments below for calculateSEHStateForAsynchEH().
278// State - incoming State of normal paths
279struct WorkItem {
280 const BasicBlock *Block;
281 int State;
282 WorkItem(const BasicBlock *BB, int St) {
283 Block = BB;
284 State = St;
285 }
286};
287void llvm::calculateCXXStateForAsynchEH(const BasicBlock *BB, int State,
288 WinEHFuncInfo &EHInfo) {
289 SmallVector<struct WorkItem *, 8> WorkList;
290 struct WorkItem *WI = new WorkItem(BB, State);
291 WorkList.push_back(Elt: WI);
292
293 while (!WorkList.empty()) {
294 WI = WorkList.pop_back_val();
295 const BasicBlock *BB = WI->Block;
296 int State = WI->State;
297 delete WI;
298 auto [StateIt, Inserted] = EHInfo.BlockToStateMap.try_emplace(Key: BB);
299 if (!Inserted && StateIt->second <= State)
300 continue; // skip blocks already visited by lower State
301
302 BasicBlock::const_iterator It = BB->getFirstNonPHIIt();
303 const llvm::Instruction *TI = BB->getTerminator();
304 if (It->isEHPad())
305 State = EHInfo.EHPadStateMap[&*It];
306 StateIt->second = State; // Record state, also flag visiting
307
308 if ((isa<CleanupReturnInst>(Val: TI) || isa<CatchReturnInst>(Val: TI)) && State > 0) {
309 // Retrive the new State
310 State = EHInfo.CxxUnwindMap[State].ToState; // Retrive next State
311 } else if (isa<InvokeInst>(Val: TI)) {
312 auto *Call = cast<CallBase>(Val: TI);
313 const Function *Fn = Call->getCalledFunction();
314 if (Fn && Fn->isIntrinsic() &&
315 (Fn->getIntrinsicID() == Intrinsic::seh_scope_begin ||
316 Fn->getIntrinsicID() == Intrinsic::seh_try_begin))
317 // Retrive the new State from seh_scope_begin
318 State = EHInfo.InvokeStateMap[cast<InvokeInst>(Val: TI)];
319 else if (Fn && Fn->isIntrinsic() &&
320 (Fn->getIntrinsicID() == Intrinsic::seh_scope_end ||
321 Fn->getIntrinsicID() == Intrinsic::seh_try_end)) {
322 // In case of conditional ctor, let's retrieve State from Invoke
323 State = EHInfo.InvokeStateMap[cast<InvokeInst>(Val: TI)];
324 // end of current state, retrive new state from UnwindMap
325 State = EHInfo.CxxUnwindMap[State].ToState;
326 }
327 }
328 // Continue push successors into worklist
329 for (auto *SuccBB : successors(BB)) {
330 WI = new WorkItem(SuccBB, State);
331 WorkList.push_back(Elt: WI);
332 }
333 }
334}
335
336// The central theory of this routine is based on the following:
337// A _try scope is always a SEME (Single Entry Multiple Exits) region
338// as jumping into a _try is not allowed
339// The single entry must start with a seh_try_begin() invoke with a
340// correct State number that is the initial state of the SEME.
341// Through control-flow, state number is propagated into all blocks.
342// Side exits marked by seh_try_end() will unwind to parent state via
343// existing SEHUnwindMap[].
344// Side exits can ONLY jump into parent scopes (lower state number).
345// Thus, when a block succeeds various states from its predecessors,
346// the lowest State trumphs others.
347// If some exits flow to unreachable, propagation on those paths terminate,
348// not affecting remaining blocks.
349void llvm::calculateSEHStateForAsynchEH(const BasicBlock *BB, int State,
350 WinEHFuncInfo &EHInfo) {
351 SmallVector<struct WorkItem *, 8> WorkList;
352 struct WorkItem *WI = new WorkItem(BB, State);
353 WorkList.push_back(Elt: WI);
354
355 while (!WorkList.empty()) {
356 WI = WorkList.pop_back_val();
357 const BasicBlock *BB = WI->Block;
358 int State = WI->State;
359 delete WI;
360 if (auto It = EHInfo.BlockToStateMap.find(Val: BB);
361 It != EHInfo.BlockToStateMap.end() && It->second <= State)
362 continue; // skip blocks already visited by lower State
363
364 BasicBlock::const_iterator It = BB->getFirstNonPHIIt();
365 const llvm::Instruction *TI = BB->getTerminator();
366 if (It->isEHPad())
367 State = EHInfo.EHPadStateMap[&*It];
368 EHInfo.BlockToStateMap[BB] = State; // Record state
369
370 if (isa<CatchPadInst>(Val: It) && isa<CatchReturnInst>(Val: TI)) {
371 assert(!isMalformedCatchpad(cast<CatchPadInst>(It),
372 EHPersonality::MSVC_X86SEH) &&
373 "Malformed CatchPadInst not caught by win-eh-prepare");
374 const Constant *FilterOrNull = cast<Constant>(
375 Val: cast<CatchPadInst>(Val&: It)->getArgOperand(i: 0)->stripPointerCasts());
376 const Function *Filter = dyn_cast<Function>(Val: FilterOrNull);
377 if (!Filter || !Filter->getName().starts_with(Prefix: "__IsLocalUnwind"))
378 State = EHInfo.SEHUnwindMap[State].ToState; // Retrive next State
379 } else if ((isa<CleanupReturnInst>(Val: TI) || isa<CatchReturnInst>(Val: TI)) &&
380 State > 0) {
381 // Retrive the new State.
382 State = EHInfo.SEHUnwindMap[State].ToState; // Retrive next State
383 } else if (isa<InvokeInst>(Val: TI)) {
384 auto *Call = cast<CallBase>(Val: TI);
385 const Function *Fn = Call->getCalledFunction();
386 if (Fn && Fn->isIntrinsic() &&
387 Fn->getIntrinsicID() == Intrinsic::seh_try_begin)
388 // Retrive the new State from seh_try_begin
389 State = EHInfo.InvokeStateMap[cast<InvokeInst>(Val: TI)];
390 else if (Fn && Fn->isIntrinsic() &&
391 Fn->getIntrinsicID() == Intrinsic::seh_try_end)
392 // end of current state, retrive new state from UnwindMap
393 State = EHInfo.SEHUnwindMap[State].ToState;
394 }
395 // Continue push successors into worklist
396 for (auto *SuccBB : successors(BB)) {
397 WI = new WorkItem(SuccBB, State);
398 WorkList.push_back(Elt: WI);
399 }
400 }
401}
402
403// Given BB which ends in an unwind edge, return the EHPad that this BB belongs
404// to. If the unwind edge came from an invoke, return null.
405static const BasicBlock *getEHPadFromPredecessor(const BasicBlock *BB,
406 Value *ParentPad) {
407 const Instruction *TI = BB->getTerminator();
408 if (isa<InvokeInst>(Val: TI))
409 return nullptr;
410 if (auto *CatchSwitch = dyn_cast<CatchSwitchInst>(Val: TI)) {
411 if (CatchSwitch->getParentPad() != ParentPad)
412 return nullptr;
413 return BB;
414 }
415 assert(!TI->isEHPad() && "unexpected EHPad!");
416 auto *CleanupPad = cast<CleanupReturnInst>(Val: TI)->getCleanupPad();
417 if (CleanupPad->getParentPad() != ParentPad)
418 return nullptr;
419 return CleanupPad->getParent();
420}
421
422// Starting from a EHPad, Backward walk through control-flow graph
423// to produce two primary outputs:
424// FuncInfo.EHPadStateMap[] and FuncInfo.CxxUnwindMap[]
425static void calculateCXXStateNumbers(WinEHFuncInfo &FuncInfo,
426 const Instruction *FirstNonPHI,
427 int ParentState) {
428 const BasicBlock *BB = FirstNonPHI->getParent();
429 assert(BB->isEHPad() && "not a funclet!");
430
431 if (auto *CatchSwitch = dyn_cast<CatchSwitchInst>(Val: FirstNonPHI)) {
432 assert(FuncInfo.EHPadStateMap.count(CatchSwitch) == 0 &&
433 "shouldn't revist catch funclets!");
434
435 SmallVector<const CatchPadInst *, 2> Handlers;
436 for (const BasicBlock *CatchPadBB : CatchSwitch->handlers()) {
437 auto *CatchPad = cast<CatchPadInst>(Val: CatchPadBB->getFirstNonPHIIt());
438 Handlers.push_back(Elt: CatchPad);
439 }
440 int TryLow = addUnwindMapEntry(FuncInfo, ToState: ParentState, BB: nullptr);
441 FuncInfo.EHPadStateMap[CatchSwitch] = TryLow;
442 for (const BasicBlock *PredBlock : predecessors(BB))
443 if ((PredBlock = getEHPadFromPredecessor(BB: PredBlock,
444 ParentPad: CatchSwitch->getParentPad())))
445 calculateCXXStateNumbers(FuncInfo, FirstNonPHI: &*PredBlock->getFirstNonPHIIt(),
446 ParentState: TryLow);
447 int CatchLow = addUnwindMapEntry(FuncInfo, ToState: ParentState, BB: nullptr);
448
449 // catchpads are separate funclets in C++ EH due to the way rethrow works.
450 int TryHigh = CatchLow - 1;
451
452 // MSVC FrameHandler3/4 on x64&Arm64 expect Catch Handlers in $tryMap$
453 // stored in pre-order (outer first, inner next), not post-order
454 // Add to map here. Fix the CatchHigh after children are processed
455 const Module *Mod = BB->getParent()->getParent();
456 bool IsPreOrder = Mod->getTargetTriple().isArch64Bit();
457 if (IsPreOrder)
458 addTryBlockMapEntry(FuncInfo, TryLow, TryHigh, CatchHigh: CatchLow, Handlers);
459 unsigned TBMEIdx = FuncInfo.TryBlockMap.size() - 1;
460
461 for (const auto *CatchPad : Handlers) {
462 FuncInfo.FuncletBaseStateMap[CatchPad] = CatchLow;
463 FuncInfo.EHPadStateMap[CatchPad] = CatchLow;
464 for (const User *U : CatchPad->users()) {
465 const auto *UserI = cast<Instruction>(Val: U);
466 if (auto *InnerCatchSwitch = dyn_cast<CatchSwitchInst>(Val: UserI)) {
467 BasicBlock *UnwindDest = InnerCatchSwitch->getUnwindDest();
468 if (!UnwindDest || UnwindDest == CatchSwitch->getUnwindDest())
469 calculateCXXStateNumbers(FuncInfo, FirstNonPHI: UserI, ParentState: CatchLow);
470 }
471 if (auto *InnerCleanupPad = dyn_cast<CleanupPadInst>(Val: UserI)) {
472 BasicBlock *UnwindDest = getCleanupRetUnwindDest(CleanupPad: InnerCleanupPad);
473 // If a nested cleanup pad reports a null unwind destination and the
474 // enclosing catch pad doesn't it must be post-dominated by an
475 // unreachable instruction.
476 if (!UnwindDest || UnwindDest == CatchSwitch->getUnwindDest())
477 calculateCXXStateNumbers(FuncInfo, FirstNonPHI: UserI, ParentState: CatchLow);
478 }
479 }
480 }
481 int CatchHigh = FuncInfo.getLastStateNumber();
482 // Now child Catches are processed, update CatchHigh
483 if (IsPreOrder)
484 FuncInfo.TryBlockMap[TBMEIdx].CatchHigh = CatchHigh;
485 else // PostOrder
486 addTryBlockMapEntry(FuncInfo, TryLow, TryHigh, CatchHigh, Handlers);
487
488 LLVM_DEBUG(dbgs() << "TryLow[" << BB->getName() << "]: " << TryLow << '\n');
489 LLVM_DEBUG(dbgs() << "TryHigh[" << BB->getName() << "]: " << TryHigh
490 << '\n');
491 LLVM_DEBUG(dbgs() << "CatchHigh[" << BB->getName() << "]: " << CatchHigh
492 << '\n');
493 } else {
494 auto *CleanupPad = cast<CleanupPadInst>(Val: FirstNonPHI);
495
496 // It's possible for a cleanup to be visited twice: it might have multiple
497 // cleanupret instructions.
498 auto [It, Inserted] = FuncInfo.EHPadStateMap.try_emplace(Key: CleanupPad);
499 if (!Inserted)
500 return;
501
502 int CleanupState = addUnwindMapEntry(FuncInfo, ToState: ParentState, BB);
503 It->second = CleanupState;
504 LLVM_DEBUG(dbgs() << "Assigning state #" << CleanupState << " to BB "
505 << BB->getName() << '\n');
506 for (const BasicBlock *PredBlock : predecessors(BB)) {
507 if ((PredBlock = getEHPadFromPredecessor(BB: PredBlock,
508 ParentPad: CleanupPad->getParentPad()))) {
509 calculateCXXStateNumbers(FuncInfo, FirstNonPHI: &*PredBlock->getFirstNonPHIIt(),
510 ParentState: CleanupState);
511 }
512 }
513 for (const User *U : CleanupPad->users()) {
514 const auto *UserI = cast<Instruction>(Val: U);
515 if (UserI->isEHPad())
516 report_fatal_error(reason: "Cleanup funclets for the MSVC++ personality cannot "
517 "contain exceptional actions");
518 }
519 }
520}
521
522static int addSEHExcept(WinEHFuncInfo &FuncInfo, int ParentState,
523 const Function *Filter, const BasicBlock *Handler) {
524 SEHUnwindMapEntry Entry;
525 Entry.ToState = ParentState;
526 Entry.IsFinally = false;
527 Entry.Filter = Filter;
528 Entry.Handler = Handler;
529 FuncInfo.SEHUnwindMap.push_back(Elt: Entry);
530 return FuncInfo.SEHUnwindMap.size() - 1;
531}
532
533static int addSEHFinally(WinEHFuncInfo &FuncInfo, int ParentState,
534 const BasicBlock *Handler) {
535 SEHUnwindMapEntry Entry;
536 Entry.ToState = ParentState;
537 Entry.IsFinally = true;
538 Entry.Filter = nullptr;
539 Entry.Handler = Handler;
540 FuncInfo.SEHUnwindMap.push_back(Elt: Entry);
541 return FuncInfo.SEHUnwindMap.size() - 1;
542}
543
544// Starting from a EHPad, Backward walk through control-flow graph
545// to produce two primary outputs:
546// FuncInfo.EHPadStateMap[] and FuncInfo.SEHUnwindMap[]
547static void calculateSEHStateNumbers(WinEHFuncInfo &FuncInfo,
548 const Instruction *FirstNonPHI,
549 int ParentState) {
550 const BasicBlock *BB = FirstNonPHI->getParent();
551 assert(BB->isEHPad() && "no a funclet!");
552
553 if (auto *CatchSwitch = dyn_cast<CatchSwitchInst>(Val: FirstNonPHI)) {
554 assert(FuncInfo.EHPadStateMap.count(CatchSwitch) == 0 &&
555 "shouldn't revist catch funclets!");
556
557 // Extract the filter function and the __except basic block and create a
558 // state for them.
559 assert(CatchSwitch->getNumHandlers() == 1 &&
560 "SEH doesn't have multiple handlers per __try");
561 const auto *CatchPad =
562 cast<CatchPadInst>(Val: (*CatchSwitch->handler_begin())->getFirstNonPHIIt());
563 const BasicBlock *CatchPadBB = CatchPad->getParent();
564 assert(!isMalformedCatchpad(CatchPad, EHPersonality::MSVC_X86SEH) &&
565 "Malformed CatchPadInst not caught by win-eh-prepare");
566 const Constant *FilterOrNull =
567 cast<Constant>(Val: CatchPad->getArgOperand(i: 0)->stripPointerCasts());
568 const Function *Filter = dyn_cast<Function>(Val: FilterOrNull);
569 assert((Filter || FilterOrNull->isNullValue()) &&
570 "unexpected filter value");
571 int TryState = addSEHExcept(FuncInfo, ParentState, Filter, Handler: CatchPadBB);
572
573 // Everything in the __try block uses TryState as its parent state.
574 FuncInfo.EHPadStateMap[CatchSwitch] = TryState;
575 FuncInfo.EHPadStateMap[CatchPad] = TryState;
576 LLVM_DEBUG(dbgs() << "Assigning state #" << TryState << " to BB "
577 << CatchPadBB->getName() << '\n');
578 for (const BasicBlock *PredBlock : predecessors(BB))
579 if ((PredBlock = getEHPadFromPredecessor(BB: PredBlock,
580 ParentPad: CatchSwitch->getParentPad())))
581 calculateSEHStateNumbers(FuncInfo, FirstNonPHI: &*PredBlock->getFirstNonPHIIt(),
582 ParentState: TryState);
583
584 // Everything in the __except block unwinds to ParentState, just like code
585 // outside the __try.
586 for (const User *U : CatchPad->users()) {
587 const auto *UserI = cast<Instruction>(Val: U);
588 if (auto *InnerCatchSwitch = dyn_cast<CatchSwitchInst>(Val: UserI)) {
589 BasicBlock *UnwindDest = InnerCatchSwitch->getUnwindDest();
590 if (!UnwindDest || UnwindDest == CatchSwitch->getUnwindDest())
591 calculateSEHStateNumbers(FuncInfo, FirstNonPHI: UserI, ParentState);
592 }
593 if (auto *InnerCleanupPad = dyn_cast<CleanupPadInst>(Val: UserI)) {
594 BasicBlock *UnwindDest = getCleanupRetUnwindDest(CleanupPad: InnerCleanupPad);
595 // If a nested cleanup pad reports a null unwind destination and the
596 // enclosing catch pad doesn't it must be post-dominated by an
597 // unreachable instruction.
598 if (!UnwindDest || UnwindDest == CatchSwitch->getUnwindDest())
599 calculateSEHStateNumbers(FuncInfo, FirstNonPHI: UserI, ParentState);
600 }
601 }
602 } else {
603 auto *CleanupPad = cast<CleanupPadInst>(Val: FirstNonPHI);
604
605 // It's possible for a cleanup to be visited twice: it might have multiple
606 // cleanupret instructions.
607 auto [It, Inserted] = FuncInfo.EHPadStateMap.try_emplace(Key: CleanupPad);
608 if (!Inserted)
609 return;
610
611 int CleanupState = addSEHFinally(FuncInfo, ParentState, Handler: BB);
612 It->second = CleanupState;
613 LLVM_DEBUG(dbgs() << "Assigning state #" << CleanupState << " to BB "
614 << BB->getName() << '\n');
615 for (const BasicBlock *PredBlock : predecessors(BB))
616 if ((PredBlock =
617 getEHPadFromPredecessor(BB: PredBlock, ParentPad: CleanupPad->getParentPad())))
618 calculateSEHStateNumbers(FuncInfo, FirstNonPHI: &*PredBlock->getFirstNonPHIIt(),
619 ParentState: CleanupState);
620 for (const User *U : CleanupPad->users()) {
621 const auto *UserI = cast<Instruction>(Val: U);
622 if (UserI->isEHPad())
623 report_fatal_error(reason: "Cleanup funclets for the SEH personality cannot "
624 "contain exceptional actions");
625 }
626 }
627}
628
629static bool isTopLevelPadForMSVC(const Instruction *EHPad) {
630 if (auto *CatchSwitch = dyn_cast<CatchSwitchInst>(Val: EHPad))
631 return isa<ConstantTokenNone>(Val: CatchSwitch->getParentPad()) &&
632 CatchSwitch->unwindsToCaller();
633 if (auto *CleanupPad = dyn_cast<CleanupPadInst>(Val: EHPad))
634 return isa<ConstantTokenNone>(Val: CleanupPad->getParentPad()) &&
635 getCleanupRetUnwindDest(CleanupPad) == nullptr;
636 if (isa<CatchPadInst>(Val: EHPad))
637 return false;
638 llvm_unreachable("unexpected EHPad!");
639}
640
641void llvm::calculateSEHStateNumbers(const Function *Fn,
642 WinEHFuncInfo &FuncInfo) {
643 // Don't compute state numbers twice.
644 if (!FuncInfo.SEHUnwindMap.empty())
645 return;
646
647 for (const BasicBlock &BB : *Fn) {
648 if (!BB.isEHPad())
649 continue;
650 const Instruction *FirstNonPHI = &*BB.getFirstNonPHIIt();
651 if (!isTopLevelPadForMSVC(EHPad: FirstNonPHI))
652 continue;
653 ::calculateSEHStateNumbers(FuncInfo, FirstNonPHI, ParentState: -1);
654 }
655
656 calculateStateNumbersForInvokes(Fn, FuncInfo);
657
658 bool IsEHa = Fn->getParent()->getModuleFlag(Key: "eh-asynch");
659 if (IsEHa) {
660 const BasicBlock *EntryBB = &(Fn->getEntryBlock());
661 calculateSEHStateForAsynchEH(BB: EntryBB, State: -1, EHInfo&: FuncInfo);
662 }
663}
664
665void llvm::calculateWinCXXEHStateNumbers(const Function *Fn,
666 WinEHFuncInfo &FuncInfo) {
667 // Return if it's already been done.
668 if (!FuncInfo.EHPadStateMap.empty())
669 return;
670
671 for (const BasicBlock &BB : *Fn) {
672 if (!BB.isEHPad())
673 continue;
674 const Instruction *FirstNonPHI = &*BB.getFirstNonPHIIt();
675 if (!isTopLevelPadForMSVC(EHPad: FirstNonPHI))
676 continue;
677 calculateCXXStateNumbers(FuncInfo, FirstNonPHI, ParentState: -1);
678 }
679
680 calculateStateNumbersForInvokes(Fn, FuncInfo);
681
682 bool IsEHa = Fn->getParent()->getModuleFlag(Key: "eh-asynch");
683 if (IsEHa) {
684 const BasicBlock *EntryBB = &(Fn->getEntryBlock());
685 calculateCXXStateForAsynchEH(BB: EntryBB, State: -1, EHInfo&: FuncInfo);
686 }
687}
688
689static int addClrEHHandler(WinEHFuncInfo &FuncInfo, int HandlerParentState,
690 int TryParentState, ClrHandlerType HandlerType,
691 uint32_t TypeToken, const BasicBlock *Handler) {
692 ClrEHUnwindMapEntry Entry;
693 Entry.HandlerParentState = HandlerParentState;
694 Entry.TryParentState = TryParentState;
695 Entry.Handler = Handler;
696 Entry.HandlerType = HandlerType;
697 Entry.TypeToken = TypeToken;
698 FuncInfo.ClrEHUnwindMap.push_back(Elt: Entry);
699 return FuncInfo.ClrEHUnwindMap.size() - 1;
700}
701
702void llvm::calculateClrEHStateNumbers(const Function *Fn,
703 WinEHFuncInfo &FuncInfo) {
704 // Return if it's already been done.
705 if (!FuncInfo.EHPadStateMap.empty())
706 return;
707
708 // This numbering assigns one state number to each catchpad and cleanuppad.
709 // It also computes two tree-like relations over states:
710 // 1) Each state has a "HandlerParentState", which is the state of the next
711 // outer handler enclosing this state's handler (same as nearest ancestor
712 // per the ParentPad linkage on EH pads, but skipping over catchswitches).
713 // 2) Each state has a "TryParentState", which:
714 // a) for a catchpad that's not the last handler on its catchswitch, is
715 // the state of the next catchpad on that catchswitch
716 // b) for all other pads, is the state of the pad whose try region is the
717 // next outer try region enclosing this state's try region. The "try
718 // regions are not present as such in the IR, but will be inferred
719 // based on the placement of invokes and pads which reach each other
720 // by exceptional exits
721 // Catchswitches do not get their own states, but each gets mapped to the
722 // state of its first catchpad.
723
724 // Step one: walk down from outermost to innermost funclets, assigning each
725 // catchpad and cleanuppad a state number. Add an entry to the
726 // ClrEHUnwindMap for each state, recording its HandlerParentState and
727 // handler attributes. Record the TryParentState as well for each catchpad
728 // that's not the last on its catchswitch, but initialize all other entries'
729 // TryParentStates to a sentinel -1 value that the next pass will update.
730
731 // Seed a worklist with pads that have no parent.
732 SmallVector<std::pair<const Instruction *, int>, 8> Worklist;
733 for (const BasicBlock &BB : *Fn) {
734 const Instruction *FirstNonPHI = &*BB.getFirstNonPHIIt();
735 const Value *ParentPad;
736 if (const auto *CPI = dyn_cast<CleanupPadInst>(Val: FirstNonPHI))
737 ParentPad = CPI->getParentPad();
738 else if (const auto *CSI = dyn_cast<CatchSwitchInst>(Val: FirstNonPHI))
739 ParentPad = CSI->getParentPad();
740 else
741 continue;
742 if (isa<ConstantTokenNone>(Val: ParentPad))
743 Worklist.emplace_back(Args&: FirstNonPHI, Args: -1);
744 }
745
746 // Use the worklist to visit all pads, from outer to inner. Record
747 // HandlerParentState for all pads. Record TryParentState only for catchpads
748 // that aren't the last on their catchswitch (setting all other entries'
749 // TryParentStates to an initial value of -1). This loop is also responsible
750 // for setting the EHPadStateMap entry for all catchpads, cleanuppads, and
751 // catchswitches.
752 while (!Worklist.empty()) {
753 const Instruction *Pad;
754 int HandlerParentState;
755 std::tie(args&: Pad, args&: HandlerParentState) = Worklist.pop_back_val();
756
757 if (const auto *Cleanup = dyn_cast<CleanupPadInst>(Val: Pad)) {
758 // Create the entry for this cleanup with the appropriate handler
759 // properties. Finally and fault handlers are distinguished by arity.
760 ClrHandlerType HandlerType =
761 (Cleanup->arg_size() ? ClrHandlerType::Fault
762 : ClrHandlerType::Finally);
763 int CleanupState = addClrEHHandler(FuncInfo, HandlerParentState, TryParentState: -1,
764 HandlerType, TypeToken: 0, Handler: Pad->getParent());
765 // Queue any child EH pads on the worklist.
766 for (const User *U : Cleanup->users())
767 if (const auto *I = dyn_cast<Instruction>(Val: U))
768 if (I->isEHPad())
769 Worklist.emplace_back(Args&: I, Args&: CleanupState);
770 // Remember this pad's state.
771 FuncInfo.EHPadStateMap[Cleanup] = CleanupState;
772 } else {
773 // Walk the handlers of this catchswitch in reverse order since all but
774 // the last need to set the following one as its TryParentState.
775 const auto *CatchSwitch = cast<CatchSwitchInst>(Val: Pad);
776 int CatchState = -1, FollowerState = -1;
777 SmallVector<const BasicBlock *, 4> CatchBlocks(CatchSwitch->handlers());
778 for (const BasicBlock *CatchBlock : llvm::reverse(C&: CatchBlocks)) {
779 // Create the entry for this catch with the appropriate handler
780 // properties.
781 const auto *Catch = cast<CatchPadInst>(Val: CatchBlock->getFirstNonPHIIt());
782 assert(!isMalformedCatchpad(Catch, EHPersonality::CoreCLR) &&
783 "Malformed CatchPadInst not caught by win-eh-prepare");
784 uint32_t TypeToken = static_cast<uint32_t>(
785 cast<ConstantInt>(Val: Catch->getArgOperand(i: 0))->getZExtValue());
786 CatchState =
787 addClrEHHandler(FuncInfo, HandlerParentState, TryParentState: FollowerState,
788 HandlerType: ClrHandlerType::Catch, TypeToken, Handler: CatchBlock);
789 // Queue any child EH pads on the worklist.
790 for (const User *U : Catch->users())
791 if (const auto *I = dyn_cast<Instruction>(Val: U))
792 if (I->isEHPad())
793 Worklist.emplace_back(Args&: I, Args&: CatchState);
794 // Remember this catch's state.
795 FuncInfo.EHPadStateMap[Catch] = CatchState;
796 FollowerState = CatchState;
797 }
798 // Associate the catchswitch with the state of its first catch.
799 assert(CatchSwitch->getNumHandlers());
800 FuncInfo.EHPadStateMap[CatchSwitch] = CatchState;
801 }
802 }
803
804 // Step two: record the TryParentState of each state. For cleanuppads that
805 // don't have cleanuprets, we may need to infer this from their child pads,
806 // so visit pads in descendant-most to ancestor-most order.
807 for (ClrEHUnwindMapEntry &Entry : llvm::reverse(C&: FuncInfo.ClrEHUnwindMap)) {
808 const Instruction *Pad =
809 &*cast<const BasicBlock *>(Val&: Entry.Handler)->getFirstNonPHIIt();
810 // For most pads, the TryParentState is the state associated with the
811 // unwind dest of exceptional exits from it.
812 const BasicBlock *UnwindDest;
813 if (const auto *Catch = dyn_cast<CatchPadInst>(Val: Pad)) {
814 // If a catch is not the last in its catchswitch, its TryParentState is
815 // the state associated with the next catch in the switch, even though
816 // that's not the unwind dest of exceptions escaping the catch. Those
817 // cases were already assigned a TryParentState in the first pass, so
818 // skip them.
819 if (Entry.TryParentState != -1)
820 continue;
821 // Otherwise, get the unwind dest from the catchswitch.
822 UnwindDest = Catch->getCatchSwitch()->getUnwindDest();
823 } else {
824 const auto *Cleanup = cast<CleanupPadInst>(Val: Pad);
825 UnwindDest = nullptr;
826 for (const User *U : Cleanup->users()) {
827 if (auto *CleanupRet = dyn_cast<CleanupReturnInst>(Val: U)) {
828 // Common and unambiguous case -- cleanupret indicates cleanup's
829 // unwind dest.
830 UnwindDest = CleanupRet->getUnwindDest();
831 break;
832 }
833
834 // Get an unwind dest for the user
835 const BasicBlock *UserUnwindDest = nullptr;
836 if (auto *Invoke = dyn_cast<InvokeInst>(Val: U)) {
837 UserUnwindDest = Invoke->getUnwindDest();
838 } else if (auto *CatchSwitch = dyn_cast<CatchSwitchInst>(Val: U)) {
839 UserUnwindDest = CatchSwitch->getUnwindDest();
840 } else if (auto *ChildCleanup = dyn_cast<CleanupPadInst>(Val: U)) {
841 int UserState = FuncInfo.EHPadStateMap[ChildCleanup];
842 int UserUnwindState =
843 FuncInfo.ClrEHUnwindMap[UserState].TryParentState;
844 if (UserUnwindState != -1)
845 UserUnwindDest = cast<const BasicBlock *>(
846 Val&: FuncInfo.ClrEHUnwindMap[UserUnwindState].Handler);
847 }
848
849 // Not having an unwind dest for this user might indicate that it
850 // doesn't unwind, so can't be taken as proof that the cleanup itself
851 // may unwind to caller (see e.g. SimplifyUnreachable and
852 // RemoveUnwindEdge).
853 if (!UserUnwindDest)
854 continue;
855
856 // Now we have an unwind dest for the user, but we need to see if it
857 // unwinds all the way out of the cleanup or if it stays within it.
858 const Instruction *UserUnwindPad = &*UserUnwindDest->getFirstNonPHIIt();
859 const Value *UserUnwindParent;
860 if (auto *CSI = dyn_cast<CatchSwitchInst>(Val: UserUnwindPad))
861 UserUnwindParent = CSI->getParentPad();
862 else
863 UserUnwindParent =
864 cast<CleanupPadInst>(Val: UserUnwindPad)->getParentPad();
865
866 // The unwind stays within the cleanup iff it targets a child of the
867 // cleanup.
868 if (UserUnwindParent == Cleanup)
869 continue;
870
871 // This unwind exits the cleanup, so its dest is the cleanup's dest.
872 UnwindDest = UserUnwindDest;
873 break;
874 }
875 }
876
877 // Record the state of the unwind dest as the TryParentState.
878 int UnwindDestState;
879
880 // If UnwindDest is null at this point, either the pad in question can
881 // be exited by unwind to caller, or it cannot be exited by unwind. In
882 // either case, reporting such cases as unwinding to caller is correct.
883 // This can lead to EH tables that "look strange" -- if this pad's is in
884 // a parent funclet which has other children that do unwind to an enclosing
885 // pad, the try region for this pad will be missing the "duplicate" EH
886 // clause entries that you'd expect to see covering the whole parent. That
887 // should be benign, since the unwind never actually happens. If it were
888 // an issue, we could add a subsequent pass that pushes unwind dests down
889 // from parents that have them to children that appear to unwind to caller.
890 if (!UnwindDest) {
891 UnwindDestState = -1;
892 } else {
893 UnwindDestState =
894 FuncInfo.EHPadStateMap[&*UnwindDest->getFirstNonPHIIt()];
895 }
896
897 Entry.TryParentState = UnwindDestState;
898 }
899
900 // Step three: transfer information from pads to invokes.
901 calculateStateNumbersForInvokes(Fn, FuncInfo);
902}
903
904void WinEHPrepareImpl::colorFunclets(Function &F) {
905 BlockColors = colorEHFunclets(F);
906
907 // Invert the map from BB to colors to color to BBs.
908 for (BasicBlock &BB : F) {
909 ColorVector &Colors = BlockColors[&BB];
910 for (BasicBlock *Color : Colors)
911 FuncletBlocks[Color].push_back(x: &BB);
912 }
913}
914
915bool WinEHPrepareImpl::demotePHIsOnFunclets(Function &F,
916 bool DemoteCatchSwitchPHIOnly) {
917 bool Changed = false;
918
919 // Strip PHI nodes off of EH pads.
920 SmallVector<PHINode *, 16> PHINodes;
921 for (BasicBlock &BB : make_early_inc_range(Range&: F)) {
922 if (!BB.isEHPad())
923 continue;
924
925 for (Instruction &I : make_early_inc_range(Range&: BB)) {
926 auto *PN = dyn_cast<PHINode>(Val: &I);
927 // Stop at the first non-PHI.
928 if (!PN)
929 break;
930
931 // If DemoteCatchSwitchPHIOnly is true, we only demote a PHI when
932 // 1. The PHI is within a catchswitch BB
933 // 2. The PHI has a catchswitch BB has one of its incoming blocks
934 if (DemoteCatchSwitchPHIOnly) {
935 bool IsCatchSwitchBB = isa<CatchSwitchInst>(Val: BB.getFirstNonPHIIt());
936 bool HasIncomingCatchSwitchBB = false;
937 for (unsigned I = 0, E = PN->getNumIncomingValues(); I < E; ++I) {
938 if (isa<CatchSwitchInst>(
939 Val: PN->getIncomingBlock(i: I)->getFirstNonPHIIt())) {
940 HasIncomingCatchSwitchBB = true;
941 break;
942 }
943 }
944 if (!IsCatchSwitchBB && !HasIncomingCatchSwitchBB)
945 break;
946 }
947
948 Changed = true;
949
950 AllocaInst *SpillSlot = insertPHILoads(PN, F);
951 if (SpillSlot)
952 insertPHIStores(OriginalPHI: PN, SpillSlot);
953
954 PHINodes.push_back(Elt: PN);
955 }
956 }
957
958 for (auto *PN : PHINodes) {
959 // There may be lingering uses on other EH PHIs being removed
960 PN->replaceAllUsesWith(V: PoisonValue::get(T: PN->getType()));
961 PN->eraseFromParent();
962 }
963
964 return Changed;
965}
966
967bool WinEHPrepareImpl::removeMalformedCatchswitch(Value *FuncletToken) {
968 // If a catchpad is malformed, the whole catchswitch is invalidated
969 // therefore, make all of its catchpads unreachable
970 CatchPadInst *CatchPad = dyn_cast<CatchPadInst>(Val: FuncletToken);
971
972 if (!CatchPad)
973 return false;
974
975 if (!isMalformedCatchpad(CPI: CatchPad, Personality))
976 return false;
977
978 CatchPad->getContext().diagnose(DI: DiagnosticInfoGenericWithLoc(
979 "catchpad with unexpected arguments", *CatchPad->getParent()->getParent(),
980 CatchPad->getDebugLoc()));
981
982 CatchSwitchInst *CatchSwitch = CatchPad->getCatchSwitch();
983 for (BasicBlock *Handler : CatchSwitch->handlers()) {
984 if (CatchPadInst *CPI =
985 dyn_cast<CatchPadInst>(Val: Handler->getFirstNonPHIIt())) {
986 LLVMContext &CTX = CPI->getContext();
987 IRBuilder<> Builder(CPI);
988
989 // default values for the CatchPad's args
990 SmallVector<Value *, 3> args;
991 Value *nullPtr = ConstantPointerNull::get(T: PointerType::getUnqual(C&: CTX));
992 Value *constantZero = ConstantInt::get(Ty: Type::getInt32Ty(C&: CTX), V: 0);
993 switch (Personality) {
994 case EHPersonality::MSVC_CXX:
995 args = {nullPtr, constantZero, nullPtr};
996 break;
997 case EHPersonality::MSVC_X86SEH:
998 case EHPersonality::MSVC_TableSEH:
999 case EHPersonality::Wasm_CXX:
1000 case EHPersonality::Wasm_D:
1001 args = {nullPtr};
1002 break;
1003 case EHPersonality::CoreCLR:
1004 args = {constantZero};
1005 break;
1006 default:
1007 llvm_unreachable("Unsupported Personality for WinEH");
1008 };
1009 Value *NewCatchPad =
1010 Builder.CreateCatchPad(ParentPad: CPI->getParentPad(), Args: args, Name: CPI->getName());
1011 CPI->replaceAllUsesWith(V: NewCatchPad);
1012 changeToUnreachable(I: CPI);
1013 }
1014 }
1015
1016 return true;
1017}
1018
1019bool WinEHPrepareImpl::cloneCommonBlocks(Function &F) {
1020 bool Changed = false;
1021
1022 // We need to clone all blocks which belong to multiple funclets. Values are
1023 // remapped throughout the funclet to propagate both the new instructions
1024 // *and* the new basic blocks themselves.
1025 for (auto &Funclets : FuncletBlocks) {
1026 BasicBlock *FuncletPadBB = Funclets.first;
1027 std::vector<BasicBlock *> &BlocksInFunclet = Funclets.second;
1028 Value *FuncletToken;
1029 if (FuncletPadBB == &F.getEntryBlock())
1030 FuncletToken = ConstantTokenNone::get(Context&: F.getContext());
1031 else {
1032 FuncletToken = &*FuncletPadBB->getFirstNonPHIIt();
1033 Changed |= removeMalformedCatchswitch(FuncletToken);
1034 }
1035
1036 std::vector<std::pair<BasicBlock *, BasicBlock *>> Orig2Clone;
1037 ValueToValueMapTy VMap;
1038 for (BasicBlock *BB : BlocksInFunclet) {
1039 ColorVector &ColorsForBB = BlockColors[BB];
1040 // We don't need to do anything if the block is monochromatic.
1041 size_t NumColorsForBB = ColorsForBB.size();
1042 if (NumColorsForBB == 1)
1043 continue;
1044
1045 DEBUG_WITH_TYPE("win-eh-prepare-coloring",
1046 dbgs() << " Cloning block \'" << BB->getName()
1047 << "\' for funclet \'" << FuncletPadBB->getName()
1048 << "\'.\n");
1049
1050 // Create a new basic block and copy instructions into it!
1051 BasicBlock *CBB =
1052 CloneBasicBlock(BB, VMap, NameSuffix: Twine(".for.", FuncletPadBB->getName()));
1053 // Insert the clone immediately after the original to ensure determinism
1054 // and to keep the same relative ordering of any funclet's blocks.
1055 CBB->insertInto(Parent: &F, InsertBefore: BB->getNextNode());
1056
1057 // Add basic block mapping.
1058 VMap[BB] = CBB;
1059
1060 // Record delta operations that we need to perform to our color mappings.
1061 Orig2Clone.emplace_back(args&: BB, args&: CBB);
1062 }
1063
1064 // If nothing was cloned, we're done cloning in this funclet.
1065 if (Orig2Clone.empty())
1066 continue;
1067
1068 Changed = true;
1069
1070 // Update our color mappings to reflect that one block has lost a color and
1071 // another has gained a color.
1072 for (auto &BBMapping : Orig2Clone) {
1073 BasicBlock *OldBlock = BBMapping.first;
1074 BasicBlock *NewBlock = BBMapping.second;
1075
1076 BlocksInFunclet.push_back(x: NewBlock);
1077 ColorVector &NewColors = BlockColors[NewBlock];
1078 assert(NewColors.empty() && "A new block should only have one color!");
1079 NewColors.push_back(NewVal: FuncletPadBB);
1080
1081 DEBUG_WITH_TYPE("win-eh-prepare-coloring",
1082 dbgs() << " Assigned color \'" << FuncletPadBB->getName()
1083 << "\' to block \'" << NewBlock->getName()
1084 << "\'.\n");
1085
1086 llvm::erase(C&: BlocksInFunclet, V: OldBlock);
1087 ColorVector &OldColors = BlockColors[OldBlock];
1088 llvm::erase(C&: OldColors, V: FuncletPadBB);
1089
1090 DEBUG_WITH_TYPE("win-eh-prepare-coloring",
1091 dbgs() << " Removed color \'" << FuncletPadBB->getName()
1092 << "\' from block \'" << OldBlock->getName()
1093 << "\'.\n");
1094 }
1095
1096 // Loop over all of the instructions in this funclet, fixing up operand
1097 // references as we go. This uses VMap to do all the hard work.
1098 for (BasicBlock *BB : BlocksInFunclet)
1099 // Loop over all instructions, fixing each one as we find it...
1100 for (Instruction &I : *BB)
1101 RemapInstruction(I: &I, VM&: VMap,
1102 Flags: RF_IgnoreMissingLocals | RF_NoModuleLevelChanges);
1103
1104 // Catchrets targeting cloned blocks need to be updated separately from
1105 // the loop above because they are not in the current funclet.
1106 SmallVector<CatchReturnInst *, 2> FixupCatchrets;
1107 for (auto &BBMapping : Orig2Clone) {
1108 BasicBlock *OldBlock = BBMapping.first;
1109 BasicBlock *NewBlock = BBMapping.second;
1110
1111 FixupCatchrets.clear();
1112 for (BasicBlock *Pred : predecessors(BB: OldBlock))
1113 if (auto *CatchRet = dyn_cast<CatchReturnInst>(Val: Pred->getTerminator()))
1114 if (CatchRet->getCatchSwitchParentPad() == FuncletToken)
1115 FixupCatchrets.push_back(Elt: CatchRet);
1116
1117 for (CatchReturnInst *CatchRet : FixupCatchrets)
1118 CatchRet->setSuccessor(NewBlock);
1119 }
1120
1121 auto UpdatePHIOnClonedBlock = [&](PHINode *PN, bool IsForOldBlock) {
1122 PN->removeIncomingValueIf(
1123 Predicate: [&](unsigned Idx) {
1124 BasicBlock *IncomingBlock = PN->getIncomingBlock(i: Idx);
1125 bool EdgeTargetsFunclet;
1126 if (auto *CRI =
1127 dyn_cast<CatchReturnInst>(Val: IncomingBlock->getTerminator())) {
1128 EdgeTargetsFunclet =
1129 (CRI->getCatchSwitchParentPad() == FuncletToken);
1130 } else {
1131 ColorVector &IncomingColors = BlockColors[IncomingBlock];
1132 assert(!IncomingColors.empty() && "Block not colored!");
1133 assert(
1134 (IncomingColors.size() == 1 ||
1135 !llvm::is_contained(IncomingColors, FuncletPadBB)) &&
1136 "Cloning should leave this funclet's blocks monochromatic");
1137 EdgeTargetsFunclet = (IncomingColors.front() == FuncletPadBB);
1138 }
1139 return IsForOldBlock == EdgeTargetsFunclet;
1140 },
1141 /*DeletePHIIfEmpty=*/false);
1142 };
1143
1144 for (auto &BBMapping : Orig2Clone) {
1145 BasicBlock *OldBlock = BBMapping.first;
1146 BasicBlock *NewBlock = BBMapping.second;
1147 for (PHINode &OldPN : OldBlock->phis()) {
1148 UpdatePHIOnClonedBlock(&OldPN, /*IsForOldBlock=*/true);
1149 }
1150 for (PHINode &NewPN : NewBlock->phis()) {
1151 UpdatePHIOnClonedBlock(&NewPN, /*IsForOldBlock=*/false);
1152 }
1153 }
1154
1155 // Check to see if SuccBB has PHI nodes. If so, we need to add entries to
1156 // the PHI nodes for NewBB now.
1157 for (auto &BBMapping : Orig2Clone) {
1158 BasicBlock *OldBlock = BBMapping.first;
1159 BasicBlock *NewBlock = BBMapping.second;
1160 for (BasicBlock *SuccBB : successors(BB: NewBlock)) {
1161 for (PHINode &SuccPN : SuccBB->phis()) {
1162 // Ok, we have a PHI node. Figure out what the incoming value was for
1163 // the OldBlock.
1164 int OldBlockIdx = SuccPN.getBasicBlockIndex(BB: OldBlock);
1165 if (OldBlockIdx == -1)
1166 break;
1167 Value *IV = SuccPN.getIncomingValue(i: OldBlockIdx);
1168
1169 // Remap the value if necessary.
1170 if (auto *Inst = dyn_cast<Instruction>(Val: IV)) {
1171 ValueToValueMapTy::iterator I = VMap.find(Val: Inst);
1172 if (I != VMap.end())
1173 IV = I->second;
1174 }
1175
1176 SuccPN.addIncoming(V: IV, BB: NewBlock);
1177 }
1178 }
1179 }
1180
1181 for (ValueToValueMapTy::value_type VT : VMap) {
1182 // If there were values defined in BB that are used outside the funclet,
1183 // then we now have to update all uses of the value to use either the
1184 // original value, the cloned value, or some PHI derived value. This can
1185 // require arbitrary PHI insertion, of which we are prepared to do, clean
1186 // these up now.
1187 SmallVector<Use *, 16> UsesToRename;
1188
1189 auto *OldI = dyn_cast<Instruction>(Val: const_cast<Value *>(VT.first));
1190 if (!OldI)
1191 continue;
1192 auto *NewI = cast<Instruction>(Val&: VT.second);
1193 // Scan all uses of this instruction to see if it is used outside of its
1194 // funclet, and if so, record them in UsesToRename.
1195 for (Use &U : OldI->uses()) {
1196 Instruction *UserI = cast<Instruction>(Val: U.getUser());
1197 BasicBlock *UserBB = UserI->getParent();
1198 ColorVector &ColorsForUserBB = BlockColors[UserBB];
1199 assert(!ColorsForUserBB.empty());
1200 if (ColorsForUserBB.size() > 1 ||
1201 *ColorsForUserBB.begin() != FuncletPadBB)
1202 UsesToRename.push_back(Elt: &U);
1203 }
1204
1205 // If there are no uses outside the block, we're done with this
1206 // instruction.
1207 if (UsesToRename.empty())
1208 continue;
1209
1210 // We found a use of OldI outside of the funclet. Rename all uses of OldI
1211 // that are outside its funclet to be uses of the appropriate PHI node
1212 // etc.
1213 SSAUpdater SSAUpdate;
1214 SSAUpdate.Initialize(Ty: OldI->getType(), Name: OldI->getName());
1215 SSAUpdate.AddAvailableValue(BB: OldI->getParent(), V: OldI);
1216 SSAUpdate.AddAvailableValue(BB: NewI->getParent(), V: NewI);
1217
1218 while (!UsesToRename.empty())
1219 SSAUpdate.RewriteUseAfterInsertions(U&: *UsesToRename.pop_back_val());
1220 }
1221 }
1222
1223 return Changed;
1224}
1225
1226bool WinEHPrepareImpl::removeImplausibleInstructions(Function &F) {
1227 bool Changed = false;
1228
1229 // Remove implausible terminators and replace them with UnreachableInst.
1230 for (auto &Funclet : FuncletBlocks) {
1231 BasicBlock *FuncletPadBB = Funclet.first;
1232 std::vector<BasicBlock *> &BlocksInFunclet = Funclet.second;
1233 Instruction *FirstNonPHI = &*FuncletPadBB->getFirstNonPHIIt();
1234 auto *FuncletPad = dyn_cast<FuncletPadInst>(Val: FirstNonPHI);
1235 auto *CatchPad = dyn_cast_or_null<CatchPadInst>(Val: FuncletPad);
1236 auto *CleanupPad = dyn_cast_or_null<CleanupPadInst>(Val: FuncletPad);
1237
1238 for (BasicBlock *BB : BlocksInFunclet) {
1239 for (Instruction &I : *BB) {
1240 auto *CB = dyn_cast<CallBase>(Val: &I);
1241 if (!CB)
1242 continue;
1243
1244 Value *FuncletBundleOperand = nullptr;
1245 if (auto BU = CB->getOperandBundle(ID: LLVMContext::OB_funclet))
1246 FuncletBundleOperand = BU->Inputs.front();
1247
1248 if (FuncletBundleOperand == FuncletPad)
1249 continue;
1250
1251 // Skip call sites which are nounwind intrinsics or inline asm.
1252 auto *CalledFn =
1253 dyn_cast<Function>(Val: CB->getCalledOperand()->stripPointerCasts());
1254 if (CB->isInlineAsm() ||
1255 (CalledFn && CalledFn->isIntrinsic() && CB->doesNotThrow()))
1256 continue;
1257
1258 Changed = true;
1259
1260 // This call site was not part of this funclet, remove it.
1261 if (isa<InvokeInst>(Val: CB)) {
1262 // Remove the unwind edge if it was an invoke.
1263 removeUnwindEdge(BB);
1264 // Get a pointer to the new call.
1265 BasicBlock::iterator CallI =
1266 std::prev(x: BB->getTerminator()->getIterator());
1267 auto *CI = cast<CallInst>(Val: &*CallI);
1268 changeToUnreachable(I: CI);
1269 } else {
1270 changeToUnreachable(I: &I);
1271 }
1272
1273 // There are no more instructions in the block (except for unreachable),
1274 // we are done.
1275 break;
1276 }
1277
1278 Instruction *TI = BB->getTerminator();
1279 // CatchPadInst and CleanupPadInst can't transfer control to a ReturnInst.
1280 bool IsUnreachableRet = isa<ReturnInst>(Val: TI) && FuncletPad;
1281 // The token consumed by a CatchReturnInst must match the funclet token.
1282 bool IsUnreachableCatchret = false;
1283 if (auto *CRI = dyn_cast<CatchReturnInst>(Val: TI))
1284 IsUnreachableCatchret = CRI->getCatchPad() != CatchPad;
1285 // The token consumed by a CleanupReturnInst must match the funclet token.
1286 bool IsUnreachableCleanupret = false;
1287 if (auto *CRI = dyn_cast<CleanupReturnInst>(Val: TI))
1288 IsUnreachableCleanupret = CRI->getCleanupPad() != CleanupPad;
1289 if (IsUnreachableRet || IsUnreachableCatchret ||
1290 IsUnreachableCleanupret) {
1291 Changed = true;
1292 changeToUnreachable(I: TI);
1293 } else if (isa<InvokeInst>(Val: TI)) {
1294 if (Personality == EHPersonality::MSVC_CXX && CleanupPad) {
1295 Changed = true;
1296 // Invokes within a cleanuppad for the MSVC++ personality never
1297 // transfer control to their unwind edge: the personality will
1298 // terminate the program.
1299 removeUnwindEdge(BB);
1300 }
1301 }
1302 }
1303 }
1304
1305 return Changed;
1306}
1307
1308bool WinEHPrepareImpl::cleanupPreparedFunclets(Function &F) {
1309 bool Changed = false;
1310
1311 // Clean-up some of the mess we made by removing useles PHI nodes, trivial
1312 // branches, etc.
1313 for (BasicBlock &BB : llvm::make_early_inc_range(Range&: F)) {
1314 Changed |= SimplifyInstructionsInBlock(BB: &BB);
1315 Changed |= ConstantFoldTerminator(BB: &BB, /*DeleteDeadConditions=*/true);
1316 Changed |= MergeBlockIntoPredecessor(BB: &BB);
1317 }
1318
1319 // We might have some unreachable blocks after cleaning up some impossible
1320 // control flow.
1321 Changed |= removeUnreachableBlocks(F);
1322
1323 return Changed;
1324}
1325
1326#ifndef NDEBUG
1327void WinEHPrepareImpl::verifyPreparedFunclets(Function &F) {
1328 for (BasicBlock &BB : F) {
1329 size_t NumColors = BlockColors[&BB].size();
1330 assert(NumColors == 1 && "Expected monochromatic BB!");
1331 if (NumColors == 0)
1332 report_fatal_error("Uncolored BB!");
1333 if (NumColors > 1)
1334 report_fatal_error("Multicolor BB!");
1335 assert((!Opts.win_eh_prepare_demotion ||
1336 !(BB.isEHPad() && isa<PHINode>(BB.begin()))) &&
1337 "EH Pad still has a PHI!");
1338 }
1339}
1340#endif
1341
1342bool WinEHPrepareImpl::prepareExplicitEH(Function &F) {
1343 // Remove unreachable blocks. It is not valuable to assign them a color and
1344 // their existence can trick us into thinking values are alive when they are
1345 // not.
1346 bool Changed = removeUnreachableBlocks(F);
1347
1348 // Determine which blocks are reachable from which funclet entries.
1349 colorFunclets(F);
1350
1351 Changed |= cloneCommonBlocks(F);
1352
1353 if (Opts.win_eh_prepare_demotion)
1354 Changed |= demotePHIsOnFunclets(F, DemoteCatchSwitchPHIOnly);
1355
1356 if (Opts.win_eh_prepare_cleanups) {
1357 assert(!verifyFunction(F, &dbgs()));
1358 Changed |= removeImplausibleInstructions(F);
1359
1360 assert(!verifyFunction(F, &dbgs()));
1361 Changed |= cleanupPreparedFunclets(F);
1362 }
1363
1364 LLVM_DEBUG(verifyPreparedFunclets(F));
1365 // Recolor the CFG to verify that all is well.
1366 LLVM_DEBUG(colorFunclets(F));
1367 LLVM_DEBUG(verifyPreparedFunclets(F));
1368
1369 return Changed;
1370}
1371
1372// TODO: Share loads when one use dominates another, or when a catchpad exit
1373// dominates uses (needs dominators).
1374AllocaInst *WinEHPrepareImpl::insertPHILoads(PHINode *PN, Function &F) {
1375 BasicBlock *PHIBlock = PN->getParent();
1376 AllocaInst *SpillSlot = nullptr;
1377 Instruction *EHPad = &*PHIBlock->getFirstNonPHIIt();
1378
1379 if (!EHPad->isTerminator()) {
1380 // If the EHPad isn't a terminator, then we can insert a load in this block
1381 // that will dominate all uses.
1382 SpillSlot = new AllocaInst(PN->getType(), DL->getAllocaAddrSpace(), nullptr,
1383 Twine(PN->getName(), ".wineh.spillslot"),
1384 F.getEntryBlock().begin());
1385 Value *V = new LoadInst(PN->getType(), SpillSlot,
1386 Twine(PN->getName(), ".wineh.reload"),
1387 PHIBlock->getFirstInsertionPt());
1388 PN->replaceAllUsesWith(V);
1389 return SpillSlot;
1390 }
1391
1392 // Otherwise, we have a PHI on a terminator EHPad, and we give up and insert
1393 // loads of the slot before every use.
1394 DenseMap<BasicBlock *, Value *> Loads;
1395 for (Use &U : llvm::make_early_inc_range(Range: PN->uses())) {
1396 auto *UsingInst = cast<Instruction>(Val: U.getUser());
1397 if (isa<PHINode>(Val: UsingInst) && UsingInst->getParent()->isEHPad()) {
1398 // Use is on an EH pad phi. Leave it alone; we'll insert loads and
1399 // stores for it separately.
1400 continue;
1401 }
1402 replaceUseWithLoad(V: PN, U, SpillSlot, Loads, F);
1403 }
1404 return SpillSlot;
1405}
1406
1407// TODO: improve store placement. Inserting at def is probably good, but need
1408// to be careful not to introduce interfering stores (needs liveness analysis).
1409// TODO: identify related phi nodes that can share spill slots, and share them
1410// (also needs liveness).
1411void WinEHPrepareImpl::insertPHIStores(PHINode *OriginalPHI,
1412 AllocaInst *SpillSlot) {
1413 // Use a worklist of (Block, Value) pairs -- the given Value needs to be
1414 // stored to the spill slot by the end of the given Block.
1415 SmallVector<std::pair<BasicBlock *, Value *>, 4> Worklist;
1416
1417 Worklist.push_back(Elt: {OriginalPHI->getParent(), OriginalPHI});
1418
1419 while (!Worklist.empty()) {
1420 BasicBlock *EHBlock;
1421 Value *InVal;
1422 std::tie(args&: EHBlock, args&: InVal) = Worklist.pop_back_val();
1423
1424 PHINode *PN = dyn_cast<PHINode>(Val: InVal);
1425 if (PN && PN->getParent() == EHBlock) {
1426 // The value is defined by another PHI we need to remove, with no room to
1427 // insert a store after the PHI, so each predecessor needs to store its
1428 // incoming value.
1429 for (unsigned i = 0, e = PN->getNumIncomingValues(); i < e; ++i) {
1430 Value *PredVal = PN->getIncomingValue(i);
1431
1432 // Undef can safely be skipped.
1433 if (isa<UndefValue>(Val: PredVal))
1434 continue;
1435
1436 insertPHIStore(PredBlock: PN->getIncomingBlock(i), PredVal, SpillSlot, Worklist);
1437 }
1438 } else {
1439 // We need to store InVal, which dominates EHBlock, but can't put a store
1440 // in EHBlock, so need to put stores in each predecessor.
1441 for (BasicBlock *PredBlock : predecessors(BB: EHBlock)) {
1442 insertPHIStore(PredBlock, PredVal: InVal, SpillSlot, Worklist);
1443 }
1444 }
1445 }
1446}
1447
1448void WinEHPrepareImpl::insertPHIStore(
1449 BasicBlock *PredBlock, Value *PredVal, AllocaInst *SpillSlot,
1450 SmallVectorImpl<std::pair<BasicBlock *, Value *>> &Worklist) {
1451
1452 if (PredBlock->isEHPad() && PredBlock->getFirstNonPHIIt()->isTerminator()) {
1453 // Pred is unsplittable, so we need to queue it on the worklist.
1454 Worklist.push_back(Elt: {PredBlock, PredVal});
1455 return;
1456 }
1457
1458 // Otherwise, insert the store at the end of the basic block.
1459 new StoreInst(PredVal, SpillSlot, PredBlock->getTerminator()->getIterator());
1460}
1461
1462void WinEHPrepareImpl::replaceUseWithLoad(
1463 Value *V, Use &U, AllocaInst *&SpillSlot,
1464 DenseMap<BasicBlock *, Value *> &Loads, Function &F) {
1465 // Lazilly create the spill slot.
1466 if (!SpillSlot)
1467 SpillSlot = new AllocaInst(V->getType(), DL->getAllocaAddrSpace(), nullptr,
1468 Twine(V->getName(), ".wineh.spillslot"),
1469 F.getEntryBlock().begin());
1470
1471 auto *UsingInst = cast<Instruction>(Val: U.getUser());
1472 if (auto *UsingPHI = dyn_cast<PHINode>(Val: UsingInst)) {
1473 // If this is a PHI node, we can't insert a load of the value before
1474 // the use. Instead insert the load in the predecessor block
1475 // corresponding to the incoming value.
1476 //
1477 // Note that if there are multiple edges from a basic block to this
1478 // PHI node that we cannot have multiple loads. The problem is that
1479 // the resulting PHI node will have multiple values (from each load)
1480 // coming in from the same block, which is illegal SSA form.
1481 // For this reason, we keep track of and reuse loads we insert.
1482 BasicBlock *IncomingBlock = UsingPHI->getIncomingBlock(U);
1483 if (auto *CatchRet =
1484 dyn_cast<CatchReturnInst>(Val: IncomingBlock->getTerminator())) {
1485 // Putting a load above a catchret and use on the phi would still leave
1486 // a cross-funclet def/use. We need to split the edge, change the
1487 // catchret to target the new block, and put the load there.
1488 BasicBlock *PHIBlock = UsingInst->getParent();
1489 BasicBlock *NewBlock = SplitEdge(From: IncomingBlock, To: PHIBlock);
1490 // SplitEdge gives us:
1491 // IncomingBlock:
1492 // ...
1493 // br label %NewBlock
1494 // NewBlock:
1495 // catchret label %PHIBlock
1496 // But we need:
1497 // IncomingBlock:
1498 // ...
1499 // catchret label %NewBlock
1500 // NewBlock:
1501 // br label %PHIBlock
1502 // So move the terminators to each others' blocks and swap their
1503 // successors.
1504 UncondBrInst *Goto = cast<UncondBrInst>(Val: IncomingBlock->getTerminator());
1505 Goto->removeFromParent();
1506 CatchRet->removeFromParent();
1507 CatchRet->insertInto(ParentBB: IncomingBlock, It: IncomingBlock->end());
1508 Goto->insertInto(ParentBB: NewBlock, It: NewBlock->end());
1509 Goto->setSuccessor(PHIBlock);
1510 CatchRet->setSuccessor(NewBlock);
1511 // Update the color mapping for the newly split edge.
1512 // Grab a reference to the ColorVector to be inserted before getting the
1513 // reference to the vector we are copying because inserting the new
1514 // element in BlockColors might cause the map to be reallocated.
1515 ColorVector &ColorsForNewBlock = BlockColors[NewBlock];
1516 ColorVector &ColorsForPHIBlock = BlockColors[PHIBlock];
1517 ColorsForNewBlock = ColorsForPHIBlock;
1518 for (BasicBlock *FuncletPad : ColorsForPHIBlock)
1519 FuncletBlocks[FuncletPad].push_back(x: NewBlock);
1520 // Treat the new block as incoming for load insertion.
1521 IncomingBlock = NewBlock;
1522 }
1523 Value *&Load = Loads[IncomingBlock];
1524 // Insert the load into the predecessor block
1525 if (!Load)
1526 Load = new LoadInst(
1527 V->getType(), SpillSlot, Twine(V->getName(), ".wineh.reload"),
1528 /*isVolatile=*/false, IncomingBlock->getTerminator()->getIterator());
1529
1530 U.set(Load);
1531 } else {
1532 // Reload right before the old use.
1533 auto *Load = new LoadInst(V->getType(), SpillSlot,
1534 Twine(V->getName(), ".wineh.reload"),
1535 /*isVolatile=*/false, UsingInst->getIterator());
1536 U.set(Load);
1537 }
1538}
1539
1540void WinEHFuncInfo::addIPToStateRange(const InvokeInst *II,
1541 MCSymbol *InvokeBegin,
1542 MCSymbol *InvokeEnd) {
1543 assert(InvokeStateMap.count(II) &&
1544 "should get invoke with precomputed state");
1545 LabelToStateMap[InvokeBegin] = std::make_pair(x&: InvokeStateMap[II], y&: InvokeEnd);
1546}
1547
1548void WinEHFuncInfo::addIPToStateRange(int State, MCSymbol* InvokeBegin,
1549 MCSymbol* InvokeEnd) {
1550 LabelToStateMap[InvokeBegin] = std::make_pair(x&: State, y&: InvokeEnd);
1551}
1552
1553WinEHFuncInfo::WinEHFuncInfo() = default;
1554