LLVM 21.0.0git
WinEHPrepare.cpp
Go to the documentation of this file.
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
19#include "llvm/ADT/DenseMap.h"
20#include "llvm/ADT/MapVector.h"
21#include "llvm/ADT/STLExtras.h"
23#include "llvm/CodeGen/Passes.h"
25#include "llvm/IR/Constants.h"
28#include "llvm/IR/Module.h"
29#include "llvm/IR/Verifier.h"
31#include "llvm/Pass.h"
33#include "llvm/Support/Debug.h"
40
41using namespace llvm;
42
43#define DEBUG_TYPE "win-eh-prepare"
44
46 "disable-demotion", cl::Hidden,
48 "Clone multicolor basic blocks but do not demote cross scopes"),
49 cl::init(false));
50
52 "disable-cleanups", cl::Hidden,
53 cl::desc("Do not remove implausible terminators or other similar cleanups"),
54 cl::init(false));
55
56// TODO: Remove this option when we fully migrate to new pass manager
58 "demote-catchswitch-only", cl::Hidden,
59 cl::desc("Demote catchswitch BBs only (for wasm EH)"), cl::init(false));
60
61namespace {
62
63class WinEHPrepareImpl {
64public:
65 WinEHPrepareImpl(bool DemoteCatchSwitchPHIOnly)
66 : DemoteCatchSwitchPHIOnly(DemoteCatchSwitchPHIOnly) {}
67
68 bool runOnFunction(Function &Fn);
69
70private:
71 void insertPHIStores(PHINode *OriginalPHI, AllocaInst *SpillSlot);
72 void
73 insertPHIStore(BasicBlock *PredBlock, Value *PredVal, AllocaInst *SpillSlot,
74 SmallVectorImpl<std::pair<BasicBlock *, Value *>> &Worklist);
75 AllocaInst *insertPHILoads(PHINode *PN, Function &F);
76 void replaceUseWithLoad(Value *V, Use &U, AllocaInst *&SpillSlot,
78 bool prepareExplicitEH(Function &F);
79 void colorFunclets(Function &F);
80
81 void demotePHIsOnFunclets(Function &F, bool DemoteCatchSwitchPHIOnly);
82 void cloneCommonBlocks(Function &F);
83 void removeImplausibleInstructions(Function &F);
84 void cleanupPreparedFunclets(Function &F);
85 void verifyPreparedFunclets(Function &F);
86
87 bool DemoteCatchSwitchPHIOnly;
88
89 // All fields are reset by runOnFunction.
90 EHPersonality Personality = EHPersonality::Unknown;
91
92 const DataLayout *DL = nullptr;
95};
96
97class WinEHPrepare : public FunctionPass {
98 bool DemoteCatchSwitchPHIOnly;
99
100public:
101 static char ID; // Pass identification, replacement for typeid.
102
103 WinEHPrepare(bool DemoteCatchSwitchPHIOnly = false)
104 : FunctionPass(ID), DemoteCatchSwitchPHIOnly(DemoteCatchSwitchPHIOnly) {}
105
106 StringRef getPassName() const override {
107 return "Windows exception handling preparation";
108 }
109
110 bool runOnFunction(Function &Fn) override {
111 return WinEHPrepareImpl(DemoteCatchSwitchPHIOnly).runOnFunction(Fn);
112 }
113};
114
115} // end anonymous namespace
116
119 bool Changed = WinEHPrepareImpl(DemoteCatchSwitchPHIOnly).runOnFunction(F);
120 return Changed ? PreservedAnalyses::none() : PreservedAnalyses::all();
121}
122
123char WinEHPrepare::ID = 0;
124INITIALIZE_PASS(WinEHPrepare, DEBUG_TYPE, "Prepare Windows exceptions", false,
125 false)
126
127FunctionPass *llvm::createWinEHPass(bool DemoteCatchSwitchPHIOnly) {
128 return new WinEHPrepare(DemoteCatchSwitchPHIOnly);
129}
130
131bool WinEHPrepareImpl::runOnFunction(Function &Fn) {
132 if (!Fn.hasPersonalityFn())
133 return false;
134
135 // Classify the personality to see what kind of preparation we need.
136 Personality = classifyEHPersonality(Fn.getPersonalityFn());
137
138 // Do nothing if this is not a scope-based personality.
139 if (!isScopedEHPersonality(Personality))
140 return false;
141
142 DL = &Fn.getDataLayout();
143 return prepareExplicitEH(Fn);
144}
145
146static int addUnwindMapEntry(WinEHFuncInfo &FuncInfo, int ToState,
147 const BasicBlock *BB) {
149 UME.ToState = ToState;
150 UME.Cleanup = BB;
151 FuncInfo.CxxUnwindMap.push_back(UME);
152 return FuncInfo.getLastStateNumber();
153}
154
155static void addTryBlockMapEntry(WinEHFuncInfo &FuncInfo, int TryLow,
156 int TryHigh, int CatchHigh,
159 TBME.TryLow = TryLow;
160 TBME.TryHigh = TryHigh;
161 TBME.CatchHigh = CatchHigh;
162 assert(TBME.TryLow <= TBME.TryHigh);
163 for (const CatchPadInst *CPI : Handlers) {
165 Constant *TypeInfo = cast<Constant>(CPI->getArgOperand(0));
166 if (TypeInfo->isNullValue())
167 HT.TypeDescriptor = nullptr;
168 else
169 HT.TypeDescriptor = cast<GlobalVariable>(TypeInfo->stripPointerCasts());
170 HT.Adjectives = cast<ConstantInt>(CPI->getArgOperand(1))->getZExtValue();
171 HT.Handler = CPI->getParent();
172 if (auto *AI =
173 dyn_cast<AllocaInst>(CPI->getArgOperand(2)->stripPointerCasts()))
174 HT.CatchObj.Alloca = AI;
175 else
176 HT.CatchObj.Alloca = nullptr;
177 TBME.HandlerArray.push_back(HT);
178 }
179 FuncInfo.TryBlockMap.push_back(TBME);
180}
181
183 for (const User *U : CleanupPad->users())
184 if (const auto *CRI = dyn_cast<CleanupReturnInst>(U))
185 return CRI->getUnwindDest();
186 return nullptr;
187}
188
190 WinEHFuncInfo &FuncInfo) {
191 auto *F = const_cast<Function *>(Fn);
193 for (BasicBlock &BB : *F) {
194 auto *II = dyn_cast<InvokeInst>(BB.getTerminator());
195 if (!II)
196 continue;
197
198 auto &BBColors = BlockColors[&BB];
199 assert(BBColors.size() == 1 && "multi-color BB not removed by preparation");
200 BasicBlock *FuncletEntryBB = BBColors.front();
201
202 BasicBlock *FuncletUnwindDest;
203 auto *FuncletPad =
204 dyn_cast<FuncletPadInst>(FuncletEntryBB->getFirstNonPHIIt());
205 assert(FuncletPad || FuncletEntryBB == &Fn->getEntryBlock());
206 if (!FuncletPad)
207 FuncletUnwindDest = nullptr;
208 else if (auto *CatchPad = dyn_cast<CatchPadInst>(FuncletPad))
209 FuncletUnwindDest = CatchPad->getCatchSwitch()->getUnwindDest();
210 else if (auto *CleanupPad = dyn_cast<CleanupPadInst>(FuncletPad))
211 FuncletUnwindDest = getCleanupRetUnwindDest(CleanupPad);
212 else
213 llvm_unreachable("unexpected funclet pad!");
214
215 BasicBlock *InvokeUnwindDest = II->getUnwindDest();
216 int BaseState = -1;
217 if (FuncletUnwindDest == InvokeUnwindDest) {
218 auto BaseStateI = FuncInfo.FuncletBaseStateMap.find(FuncletPad);
219 if (BaseStateI != FuncInfo.FuncletBaseStateMap.end())
220 BaseState = BaseStateI->second;
221 }
222
223 if (BaseState != -1) {
224 FuncInfo.InvokeStateMap[II] = BaseState;
225 } else {
226 Instruction *PadInst = &*InvokeUnwindDest->getFirstNonPHIIt();
227 assert(FuncInfo.EHPadStateMap.count(PadInst) && "EH Pad has no state!");
228 FuncInfo.InvokeStateMap[II] = FuncInfo.EHPadStateMap[PadInst];
229 }
230 }
231}
232
233// See comments below for calculateSEHStateForAsynchEH().
234// State - incoming State of normal paths
235struct WorkItem {
237 int State;
238 WorkItem(const BasicBlock *BB, int St) {
239 Block = BB;
240 State = St;
241 }
242};
244 WinEHFuncInfo &EHInfo) {
246 struct WorkItem *WI = new WorkItem(BB, State);
247 WorkList.push_back(WI);
248
249 while (!WorkList.empty()) {
250 WI = WorkList.pop_back_val();
251 const BasicBlock *BB = WI->Block;
252 int State = WI->State;
253 delete WI;
254 if (auto It = EHInfo.BlockToStateMap.find(BB);
255 It != EHInfo.BlockToStateMap.end() && It->second <= State)
256 continue; // skip blocks already visited by lower State
257
259 const llvm::Instruction *TI = BB->getTerminator();
260 if (It->isEHPad())
261 State = EHInfo.EHPadStateMap[&*It];
262 EHInfo.BlockToStateMap[BB] = State; // Record state, also flag visiting
263
264 if ((isa<CleanupReturnInst>(TI) || isa<CatchReturnInst>(TI)) && State > 0) {
265 // Retrive the new State
266 State = EHInfo.CxxUnwindMap[State].ToState; // Retrive next State
267 } else if (isa<InvokeInst>(TI)) {
268 auto *Call = cast<CallBase>(TI);
269 const Function *Fn = Call->getCalledFunction();
270 if (Fn && Fn->isIntrinsic() &&
271 (Fn->getIntrinsicID() == Intrinsic::seh_scope_begin ||
272 Fn->getIntrinsicID() == Intrinsic::seh_try_begin))
273 // Retrive the new State from seh_scope_begin
274 State = EHInfo.InvokeStateMap[cast<InvokeInst>(TI)];
275 else if (Fn && Fn->isIntrinsic() &&
276 (Fn->getIntrinsicID() == Intrinsic::seh_scope_end ||
277 Fn->getIntrinsicID() == Intrinsic::seh_try_end)) {
278 // In case of conditional ctor, let's retrieve State from Invoke
279 State = EHInfo.InvokeStateMap[cast<InvokeInst>(TI)];
280 // end of current state, retrive new state from UnwindMap
281 State = EHInfo.CxxUnwindMap[State].ToState;
282 }
283 }
284 // Continue push successors into worklist
285 for (auto *SuccBB : successors(BB)) {
286 WI = new WorkItem(SuccBB, State);
287 WorkList.push_back(WI);
288 }
289 }
290}
291
292// The central theory of this routine is based on the following:
293// A _try scope is always a SEME (Single Entry Multiple Exits) region
294// as jumping into a _try is not allowed
295// The single entry must start with a seh_try_begin() invoke with a
296// correct State number that is the initial state of the SEME.
297// Through control-flow, state number is propagated into all blocks.
298// Side exits marked by seh_try_end() will unwind to parent state via
299// existing SEHUnwindMap[].
300// Side exits can ONLY jump into parent scopes (lower state number).
301// Thus, when a block succeeds various states from its predecessors,
302// the lowest State trumphs others.
303// If some exits flow to unreachable, propagation on those paths terminate,
304// not affecting remaining blocks.
306 WinEHFuncInfo &EHInfo) {
308 struct WorkItem *WI = new WorkItem(BB, State);
309 WorkList.push_back(WI);
310
311 while (!WorkList.empty()) {
312 WI = WorkList.pop_back_val();
313 const BasicBlock *BB = WI->Block;
314 int State = WI->State;
315 delete WI;
316 if (auto It = EHInfo.BlockToStateMap.find(BB);
317 It != EHInfo.BlockToStateMap.end() && It->second <= State)
318 continue; // skip blocks already visited by lower State
319
321 const llvm::Instruction *TI = BB->getTerminator();
322 if (It->isEHPad())
323 State = EHInfo.EHPadStateMap[&*It];
324 EHInfo.BlockToStateMap[BB] = State; // Record state
325
326 if (isa<CatchPadInst>(It) && isa<CatchReturnInst>(TI)) {
327 const Constant *FilterOrNull = cast<Constant>(
328 cast<CatchPadInst>(It)->getArgOperand(0)->stripPointerCasts());
329 const Function *Filter = dyn_cast<Function>(FilterOrNull);
330 if (!Filter || !Filter->getName().starts_with("__IsLocalUnwind"))
331 State = EHInfo.SEHUnwindMap[State].ToState; // Retrive next State
332 } else if ((isa<CleanupReturnInst>(TI) || isa<CatchReturnInst>(TI)) &&
333 State > 0) {
334 // Retrive the new State.
335 State = EHInfo.SEHUnwindMap[State].ToState; // Retrive next State
336 } else if (isa<InvokeInst>(TI)) {
337 auto *Call = cast<CallBase>(TI);
338 const Function *Fn = Call->getCalledFunction();
339 if (Fn && Fn->isIntrinsic() &&
340 Fn->getIntrinsicID() == Intrinsic::seh_try_begin)
341 // Retrive the new State from seh_try_begin
342 State = EHInfo.InvokeStateMap[cast<InvokeInst>(TI)];
343 else if (Fn && Fn->isIntrinsic() &&
344 Fn->getIntrinsicID() == Intrinsic::seh_try_end)
345 // end of current state, retrive new state from UnwindMap
346 State = EHInfo.SEHUnwindMap[State].ToState;
347 }
348 // Continue push successors into worklist
349 for (auto *SuccBB : successors(BB)) {
350 WI = new WorkItem(SuccBB, State);
351 WorkList.push_back(WI);
352 }
353 }
354}
355
356// Given BB which ends in an unwind edge, return the EHPad that this BB belongs
357// to. If the unwind edge came from an invoke, return null.
359 Value *ParentPad) {
360 const Instruction *TI = BB->getTerminator();
361 if (isa<InvokeInst>(TI))
362 return nullptr;
363 if (auto *CatchSwitch = dyn_cast<CatchSwitchInst>(TI)) {
364 if (CatchSwitch->getParentPad() != ParentPad)
365 return nullptr;
366 return BB;
367 }
368 assert(!TI->isEHPad() && "unexpected EHPad!");
369 auto *CleanupPad = cast<CleanupReturnInst>(TI)->getCleanupPad();
370 if (CleanupPad->getParentPad() != ParentPad)
371 return nullptr;
372 return CleanupPad->getParent();
373}
374
375// Starting from a EHPad, Backward walk through control-flow graph
376// to produce two primary outputs:
377// FuncInfo.EHPadStateMap[] and FuncInfo.CxxUnwindMap[]
379 const Instruction *FirstNonPHI,
380 int ParentState) {
381 const BasicBlock *BB = FirstNonPHI->getParent();
382 assert(BB->isEHPad() && "not a funclet!");
383
384 if (auto *CatchSwitch = dyn_cast<CatchSwitchInst>(FirstNonPHI)) {
385 assert(FuncInfo.EHPadStateMap.count(CatchSwitch) == 0 &&
386 "shouldn't revist catch funclets!");
387
389 for (const BasicBlock *CatchPadBB : CatchSwitch->handlers()) {
390 auto *CatchPad = cast<CatchPadInst>(CatchPadBB->getFirstNonPHIIt());
391 Handlers.push_back(CatchPad);
392 }
393 int TryLow = addUnwindMapEntry(FuncInfo, ParentState, nullptr);
394 FuncInfo.EHPadStateMap[CatchSwitch] = TryLow;
395 for (const BasicBlock *PredBlock : predecessors(BB))
396 if ((PredBlock = getEHPadFromPredecessor(PredBlock,
397 CatchSwitch->getParentPad())))
398 calculateCXXStateNumbers(FuncInfo, &*PredBlock->getFirstNonPHIIt(),
399 TryLow);
400 int CatchLow = addUnwindMapEntry(FuncInfo, ParentState, nullptr);
401
402 // catchpads are separate funclets in C++ EH due to the way rethrow works.
403 int TryHigh = CatchLow - 1;
404
405 // MSVC FrameHandler3/4 on x64&Arm64 expect Catch Handlers in $tryMap$
406 // stored in pre-order (outer first, inner next), not post-order
407 // Add to map here. Fix the CatchHigh after children are processed
408 const Module *Mod = BB->getParent()->getParent();
409 bool IsPreOrder = Triple(Mod->getTargetTriple()).isArch64Bit();
410 if (IsPreOrder)
411 addTryBlockMapEntry(FuncInfo, TryLow, TryHigh, CatchLow, Handlers);
412 unsigned TBMEIdx = FuncInfo.TryBlockMap.size() - 1;
413
414 for (const auto *CatchPad : Handlers) {
415 FuncInfo.FuncletBaseStateMap[CatchPad] = CatchLow;
416 FuncInfo.EHPadStateMap[CatchPad] = CatchLow;
417 for (const User *U : CatchPad->users()) {
418 const auto *UserI = cast<Instruction>(U);
419 if (auto *InnerCatchSwitch = dyn_cast<CatchSwitchInst>(UserI)) {
420 BasicBlock *UnwindDest = InnerCatchSwitch->getUnwindDest();
421 if (!UnwindDest || UnwindDest == CatchSwitch->getUnwindDest())
422 calculateCXXStateNumbers(FuncInfo, UserI, CatchLow);
423 }
424 if (auto *InnerCleanupPad = dyn_cast<CleanupPadInst>(UserI)) {
425 BasicBlock *UnwindDest = getCleanupRetUnwindDest(InnerCleanupPad);
426 // If a nested cleanup pad reports a null unwind destination and the
427 // enclosing catch pad doesn't it must be post-dominated by an
428 // unreachable instruction.
429 if (!UnwindDest || UnwindDest == CatchSwitch->getUnwindDest())
430 calculateCXXStateNumbers(FuncInfo, UserI, CatchLow);
431 }
432 }
433 }
434 int CatchHigh = FuncInfo.getLastStateNumber();
435 // Now child Catches are processed, update CatchHigh
436 if (IsPreOrder)
437 FuncInfo.TryBlockMap[TBMEIdx].CatchHigh = CatchHigh;
438 else // PostOrder
439 addTryBlockMapEntry(FuncInfo, TryLow, TryHigh, CatchHigh, Handlers);
440
441 LLVM_DEBUG(dbgs() << "TryLow[" << BB->getName() << "]: " << TryLow << '\n');
442 LLVM_DEBUG(dbgs() << "TryHigh[" << BB->getName() << "]: " << TryHigh
443 << '\n');
444 LLVM_DEBUG(dbgs() << "CatchHigh[" << BB->getName() << "]: " << CatchHigh
445 << '\n');
446 } else {
447 auto *CleanupPad = cast<CleanupPadInst>(FirstNonPHI);
448
449 // It's possible for a cleanup to be visited twice: it might have multiple
450 // cleanupret instructions.
451 if (FuncInfo.EHPadStateMap.count(CleanupPad))
452 return;
453
454 int CleanupState = addUnwindMapEntry(FuncInfo, ParentState, BB);
455 FuncInfo.EHPadStateMap[CleanupPad] = CleanupState;
456 LLVM_DEBUG(dbgs() << "Assigning state #" << CleanupState << " to BB "
457 << BB->getName() << '\n');
458 for (const BasicBlock *PredBlock : predecessors(BB)) {
459 if ((PredBlock = getEHPadFromPredecessor(PredBlock,
460 CleanupPad->getParentPad()))) {
461 calculateCXXStateNumbers(FuncInfo, &*PredBlock->getFirstNonPHIIt(),
462 CleanupState);
463 }
464 }
465 for (const User *U : CleanupPad->users()) {
466 const auto *UserI = cast<Instruction>(U);
467 if (UserI->isEHPad())
468 report_fatal_error("Cleanup funclets for the MSVC++ personality cannot "
469 "contain exceptional actions");
470 }
471 }
472}
473
474static int addSEHExcept(WinEHFuncInfo &FuncInfo, int ParentState,
475 const Function *Filter, const BasicBlock *Handler) {
476 SEHUnwindMapEntry Entry;
477 Entry.ToState = ParentState;
478 Entry.IsFinally = false;
479 Entry.Filter = Filter;
480 Entry.Handler = Handler;
481 FuncInfo.SEHUnwindMap.push_back(Entry);
482 return FuncInfo.SEHUnwindMap.size() - 1;
483}
484
485static int addSEHFinally(WinEHFuncInfo &FuncInfo, int ParentState,
486 const BasicBlock *Handler) {
487 SEHUnwindMapEntry Entry;
488 Entry.ToState = ParentState;
489 Entry.IsFinally = true;
490 Entry.Filter = nullptr;
491 Entry.Handler = Handler;
492 FuncInfo.SEHUnwindMap.push_back(Entry);
493 return FuncInfo.SEHUnwindMap.size() - 1;
494}
495
496// Starting from a EHPad, Backward walk through control-flow graph
497// to produce two primary outputs:
498// FuncInfo.EHPadStateMap[] and FuncInfo.SEHUnwindMap[]
500 const Instruction *FirstNonPHI,
501 int ParentState) {
502 const BasicBlock *BB = FirstNonPHI->getParent();
503 assert(BB->isEHPad() && "no a funclet!");
504
505 if (auto *CatchSwitch = dyn_cast<CatchSwitchInst>(FirstNonPHI)) {
506 assert(FuncInfo.EHPadStateMap.count(CatchSwitch) == 0 &&
507 "shouldn't revist catch funclets!");
508
509 // Extract the filter function and the __except basic block and create a
510 // state for them.
511 assert(CatchSwitch->getNumHandlers() == 1 &&
512 "SEH doesn't have multiple handlers per __try");
513 const auto *CatchPad =
514 cast<CatchPadInst>((*CatchSwitch->handler_begin())->getFirstNonPHIIt());
515 const BasicBlock *CatchPadBB = CatchPad->getParent();
516 const Constant *FilterOrNull =
517 cast<Constant>(CatchPad->getArgOperand(0)->stripPointerCasts());
518 const Function *Filter = dyn_cast<Function>(FilterOrNull);
519 assert((Filter || FilterOrNull->isNullValue()) &&
520 "unexpected filter value");
521 int TryState = addSEHExcept(FuncInfo, ParentState, Filter, CatchPadBB);
522
523 // Everything in the __try block uses TryState as its parent state.
524 FuncInfo.EHPadStateMap[CatchSwitch] = TryState;
525 FuncInfo.EHPadStateMap[CatchPad] = TryState;
526 LLVM_DEBUG(dbgs() << "Assigning state #" << TryState << " to BB "
527 << CatchPadBB->getName() << '\n');
528 for (const BasicBlock *PredBlock : predecessors(BB))
529 if ((PredBlock = getEHPadFromPredecessor(PredBlock,
530 CatchSwitch->getParentPad())))
531 calculateSEHStateNumbers(FuncInfo, &*PredBlock->getFirstNonPHIIt(),
532 TryState);
533
534 // Everything in the __except block unwinds to ParentState, just like code
535 // outside the __try.
536 for (const User *U : CatchPad->users()) {
537 const auto *UserI = cast<Instruction>(U);
538 if (auto *InnerCatchSwitch = dyn_cast<CatchSwitchInst>(UserI)) {
539 BasicBlock *UnwindDest = InnerCatchSwitch->getUnwindDest();
540 if (!UnwindDest || UnwindDest == CatchSwitch->getUnwindDest())
541 calculateSEHStateNumbers(FuncInfo, UserI, ParentState);
542 }
543 if (auto *InnerCleanupPad = dyn_cast<CleanupPadInst>(UserI)) {
544 BasicBlock *UnwindDest = getCleanupRetUnwindDest(InnerCleanupPad);
545 // If a nested cleanup pad reports a null unwind destination and the
546 // enclosing catch pad doesn't it must be post-dominated by an
547 // unreachable instruction.
548 if (!UnwindDest || UnwindDest == CatchSwitch->getUnwindDest())
549 calculateSEHStateNumbers(FuncInfo, UserI, ParentState);
550 }
551 }
552 } else {
553 auto *CleanupPad = cast<CleanupPadInst>(FirstNonPHI);
554
555 // It's possible for a cleanup to be visited twice: it might have multiple
556 // cleanupret instructions.
557 if (FuncInfo.EHPadStateMap.count(CleanupPad))
558 return;
559
560 int CleanupState = addSEHFinally(FuncInfo, ParentState, BB);
561 FuncInfo.EHPadStateMap[CleanupPad] = CleanupState;
562 LLVM_DEBUG(dbgs() << "Assigning state #" << CleanupState << " to BB "
563 << BB->getName() << '\n');
564 for (const BasicBlock *PredBlock : predecessors(BB))
565 if ((PredBlock =
566 getEHPadFromPredecessor(PredBlock, CleanupPad->getParentPad())))
567 calculateSEHStateNumbers(FuncInfo, &*PredBlock->getFirstNonPHIIt(),
568 CleanupState);
569 for (const User *U : CleanupPad->users()) {
570 const auto *UserI = cast<Instruction>(U);
571 if (UserI->isEHPad())
572 report_fatal_error("Cleanup funclets for the SEH personality cannot "
573 "contain exceptional actions");
574 }
575 }
576}
577
578static bool isTopLevelPadForMSVC(const Instruction *EHPad) {
579 if (auto *CatchSwitch = dyn_cast<CatchSwitchInst>(EHPad))
580 return isa<ConstantTokenNone>(CatchSwitch->getParentPad()) &&
581 CatchSwitch->unwindsToCaller();
582 if (auto *CleanupPad = dyn_cast<CleanupPadInst>(EHPad))
583 return isa<ConstantTokenNone>(CleanupPad->getParentPad()) &&
584 getCleanupRetUnwindDest(CleanupPad) == nullptr;
585 if (isa<CatchPadInst>(EHPad))
586 return false;
587 llvm_unreachable("unexpected EHPad!");
588}
589
591 WinEHFuncInfo &FuncInfo) {
592 // Don't compute state numbers twice.
593 if (!FuncInfo.SEHUnwindMap.empty())
594 return;
595
596 for (const BasicBlock &BB : *Fn) {
597 if (!BB.isEHPad())
598 continue;
599 const Instruction *FirstNonPHI = &*BB.getFirstNonPHIIt();
600 if (!isTopLevelPadForMSVC(FirstNonPHI))
601 continue;
602 ::calculateSEHStateNumbers(FuncInfo, FirstNonPHI, -1);
603 }
604
606
607 bool IsEHa = Fn->getParent()->getModuleFlag("eh-asynch");
608 if (IsEHa) {
609 const BasicBlock *EntryBB = &(Fn->getEntryBlock());
610 calculateSEHStateForAsynchEH(EntryBB, -1, FuncInfo);
611 }
612}
613
615 WinEHFuncInfo &FuncInfo) {
616 // Return if it's already been done.
617 if (!FuncInfo.EHPadStateMap.empty())
618 return;
619
620 for (const BasicBlock &BB : *Fn) {
621 if (!BB.isEHPad())
622 continue;
623 const Instruction *FirstNonPHI = &*BB.getFirstNonPHIIt();
624 if (!isTopLevelPadForMSVC(FirstNonPHI))
625 continue;
626 calculateCXXStateNumbers(FuncInfo, FirstNonPHI, -1);
627 }
628
630
631 bool IsEHa = Fn->getParent()->getModuleFlag("eh-asynch");
632 if (IsEHa) {
633 const BasicBlock *EntryBB = &(Fn->getEntryBlock());
634 calculateCXXStateForAsynchEH(EntryBB, -1, FuncInfo);
635 }
636}
637
638static int addClrEHHandler(WinEHFuncInfo &FuncInfo, int HandlerParentState,
639 int TryParentState, ClrHandlerType HandlerType,
640 uint32_t TypeToken, const BasicBlock *Handler) {
642 Entry.HandlerParentState = HandlerParentState;
643 Entry.TryParentState = TryParentState;
644 Entry.Handler = Handler;
645 Entry.HandlerType = HandlerType;
646 Entry.TypeToken = TypeToken;
647 FuncInfo.ClrEHUnwindMap.push_back(Entry);
648 return FuncInfo.ClrEHUnwindMap.size() - 1;
649}
650
652 WinEHFuncInfo &FuncInfo) {
653 // Return if it's already been done.
654 if (!FuncInfo.EHPadStateMap.empty())
655 return;
656
657 // This numbering assigns one state number to each catchpad and cleanuppad.
658 // It also computes two tree-like relations over states:
659 // 1) Each state has a "HandlerParentState", which is the state of the next
660 // outer handler enclosing this state's handler (same as nearest ancestor
661 // per the ParentPad linkage on EH pads, but skipping over catchswitches).
662 // 2) Each state has a "TryParentState", which:
663 // a) for a catchpad that's not the last handler on its catchswitch, is
664 // the state of the next catchpad on that catchswitch
665 // b) for all other pads, is the state of the pad whose try region is the
666 // next outer try region enclosing this state's try region. The "try
667 // regions are not present as such in the IR, but will be inferred
668 // based on the placement of invokes and pads which reach each other
669 // by exceptional exits
670 // Catchswitches do not get their own states, but each gets mapped to the
671 // state of its first catchpad.
672
673 // Step one: walk down from outermost to innermost funclets, assigning each
674 // catchpad and cleanuppad a state number. Add an entry to the
675 // ClrEHUnwindMap for each state, recording its HandlerParentState and
676 // handler attributes. Record the TryParentState as well for each catchpad
677 // that's not the last on its catchswitch, but initialize all other entries'
678 // TryParentStates to a sentinel -1 value that the next pass will update.
679
680 // Seed a worklist with pads that have no parent.
682 for (const BasicBlock &BB : *Fn) {
683 const Instruction *FirstNonPHI = &*BB.getFirstNonPHIIt();
684 const Value *ParentPad;
685 if (const auto *CPI = dyn_cast<CleanupPadInst>(FirstNonPHI))
686 ParentPad = CPI->getParentPad();
687 else if (const auto *CSI = dyn_cast<CatchSwitchInst>(FirstNonPHI))
688 ParentPad = CSI->getParentPad();
689 else
690 continue;
691 if (isa<ConstantTokenNone>(ParentPad))
692 Worklist.emplace_back(FirstNonPHI, -1);
693 }
694
695 // Use the worklist to visit all pads, from outer to inner. Record
696 // HandlerParentState for all pads. Record TryParentState only for catchpads
697 // that aren't the last on their catchswitch (setting all other entries'
698 // TryParentStates to an initial value of -1). This loop is also responsible
699 // for setting the EHPadStateMap entry for all catchpads, cleanuppads, and
700 // catchswitches.
701 while (!Worklist.empty()) {
702 const Instruction *Pad;
703 int HandlerParentState;
704 std::tie(Pad, HandlerParentState) = Worklist.pop_back_val();
705
706 if (const auto *Cleanup = dyn_cast<CleanupPadInst>(Pad)) {
707 // Create the entry for this cleanup with the appropriate handler
708 // properties. Finally and fault handlers are distinguished by arity.
709 ClrHandlerType HandlerType =
710 (Cleanup->arg_size() ? ClrHandlerType::Fault
712 int CleanupState = addClrEHHandler(FuncInfo, HandlerParentState, -1,
713 HandlerType, 0, Pad->getParent());
714 // Queue any child EH pads on the worklist.
715 for (const User *U : Cleanup->users())
716 if (const auto *I = dyn_cast<Instruction>(U))
717 if (I->isEHPad())
718 Worklist.emplace_back(I, CleanupState);
719 // Remember this pad's state.
720 FuncInfo.EHPadStateMap[Cleanup] = CleanupState;
721 } else {
722 // Walk the handlers of this catchswitch in reverse order since all but
723 // the last need to set the following one as its TryParentState.
724 const auto *CatchSwitch = cast<CatchSwitchInst>(Pad);
725 int CatchState = -1, FollowerState = -1;
726 SmallVector<const BasicBlock *, 4> CatchBlocks(CatchSwitch->handlers());
727 for (const BasicBlock *CatchBlock : llvm::reverse(CatchBlocks)) {
728 // Create the entry for this catch with the appropriate handler
729 // properties.
730 const auto *Catch = cast<CatchPadInst>(CatchBlock->getFirstNonPHIIt());
731 uint32_t TypeToken = static_cast<uint32_t>(
732 cast<ConstantInt>(Catch->getArgOperand(0))->getZExtValue());
733 CatchState =
734 addClrEHHandler(FuncInfo, HandlerParentState, FollowerState,
735 ClrHandlerType::Catch, TypeToken, CatchBlock);
736 // Queue any child EH pads on the worklist.
737 for (const User *U : Catch->users())
738 if (const auto *I = dyn_cast<Instruction>(U))
739 if (I->isEHPad())
740 Worklist.emplace_back(I, CatchState);
741 // Remember this catch's state.
742 FuncInfo.EHPadStateMap[Catch] = CatchState;
743 FollowerState = CatchState;
744 }
745 // Associate the catchswitch with the state of its first catch.
746 assert(CatchSwitch->getNumHandlers());
747 FuncInfo.EHPadStateMap[CatchSwitch] = CatchState;
748 }
749 }
750
751 // Step two: record the TryParentState of each state. For cleanuppads that
752 // don't have cleanuprets, we may need to infer this from their child pads,
753 // so visit pads in descendant-most to ancestor-most order.
754 for (ClrEHUnwindMapEntry &Entry : llvm::reverse(FuncInfo.ClrEHUnwindMap)) {
755 const Instruction *Pad =
756 &*cast<const BasicBlock *>(Entry.Handler)->getFirstNonPHIIt();
757 // For most pads, the TryParentState is the state associated with the
758 // unwind dest of exceptional exits from it.
759 const BasicBlock *UnwindDest;
760 if (const auto *Catch = dyn_cast<CatchPadInst>(Pad)) {
761 // If a catch is not the last in its catchswitch, its TryParentState is
762 // the state associated with the next catch in the switch, even though
763 // that's not the unwind dest of exceptions escaping the catch. Those
764 // cases were already assigned a TryParentState in the first pass, so
765 // skip them.
766 if (Entry.TryParentState != -1)
767 continue;
768 // Otherwise, get the unwind dest from the catchswitch.
769 UnwindDest = Catch->getCatchSwitch()->getUnwindDest();
770 } else {
771 const auto *Cleanup = cast<CleanupPadInst>(Pad);
772 UnwindDest = nullptr;
773 for (const User *U : Cleanup->users()) {
774 if (auto *CleanupRet = dyn_cast<CleanupReturnInst>(U)) {
775 // Common and unambiguous case -- cleanupret indicates cleanup's
776 // unwind dest.
777 UnwindDest = CleanupRet->getUnwindDest();
778 break;
779 }
780
781 // Get an unwind dest for the user
782 const BasicBlock *UserUnwindDest = nullptr;
783 if (auto *Invoke = dyn_cast<InvokeInst>(U)) {
784 UserUnwindDest = Invoke->getUnwindDest();
785 } else if (auto *CatchSwitch = dyn_cast<CatchSwitchInst>(U)) {
786 UserUnwindDest = CatchSwitch->getUnwindDest();
787 } else if (auto *ChildCleanup = dyn_cast<CleanupPadInst>(U)) {
788 int UserState = FuncInfo.EHPadStateMap[ChildCleanup];
789 int UserUnwindState =
790 FuncInfo.ClrEHUnwindMap[UserState].TryParentState;
791 if (UserUnwindState != -1)
792 UserUnwindDest = cast<const BasicBlock *>(
793 FuncInfo.ClrEHUnwindMap[UserUnwindState].Handler);
794 }
795
796 // Not having an unwind dest for this user might indicate that it
797 // doesn't unwind, so can't be taken as proof that the cleanup itself
798 // may unwind to caller (see e.g. SimplifyUnreachable and
799 // RemoveUnwindEdge).
800 if (!UserUnwindDest)
801 continue;
802
803 // Now we have an unwind dest for the user, but we need to see if it
804 // unwinds all the way out of the cleanup or if it stays within it.
805 const Instruction *UserUnwindPad = &*UserUnwindDest->getFirstNonPHIIt();
806 const Value *UserUnwindParent;
807 if (auto *CSI = dyn_cast<CatchSwitchInst>(UserUnwindPad))
808 UserUnwindParent = CSI->getParentPad();
809 else
810 UserUnwindParent =
811 cast<CleanupPadInst>(UserUnwindPad)->getParentPad();
812
813 // The unwind stays within the cleanup iff it targets a child of the
814 // cleanup.
815 if (UserUnwindParent == Cleanup)
816 continue;
817
818 // This unwind exits the cleanup, so its dest is the cleanup's dest.
819 UnwindDest = UserUnwindDest;
820 break;
821 }
822 }
823
824 // Record the state of the unwind dest as the TryParentState.
825 int UnwindDestState;
826
827 // If UnwindDest is null at this point, either the pad in question can
828 // be exited by unwind to caller, or it cannot be exited by unwind. In
829 // either case, reporting such cases as unwinding to caller is correct.
830 // This can lead to EH tables that "look strange" -- if this pad's is in
831 // a parent funclet which has other children that do unwind to an enclosing
832 // pad, the try region for this pad will be missing the "duplicate" EH
833 // clause entries that you'd expect to see covering the whole parent. That
834 // should be benign, since the unwind never actually happens. If it were
835 // an issue, we could add a subsequent pass that pushes unwind dests down
836 // from parents that have them to children that appear to unwind to caller.
837 if (!UnwindDest) {
838 UnwindDestState = -1;
839 } else {
840 UnwindDestState =
841 FuncInfo.EHPadStateMap[&*UnwindDest->getFirstNonPHIIt()];
842 }
843
844 Entry.TryParentState = UnwindDestState;
845 }
846
847 // Step three: transfer information from pads to invokes.
849}
850
851void WinEHPrepareImpl::colorFunclets(Function &F) {
852 BlockColors = colorEHFunclets(F);
853
854 // Invert the map from BB to colors to color to BBs.
855 for (BasicBlock &BB : F) {
856 ColorVector &Colors = BlockColors[&BB];
857 for (BasicBlock *Color : Colors)
858 FuncletBlocks[Color].push_back(&BB);
859 }
860}
861
862void WinEHPrepareImpl::demotePHIsOnFunclets(Function &F,
863 bool DemoteCatchSwitchPHIOnly) {
864 // Strip PHI nodes off of EH pads.
866 for (BasicBlock &BB : make_early_inc_range(F)) {
867 if (!BB.isEHPad())
868 continue;
869 if (DemoteCatchSwitchPHIOnly &&
870 !isa<CatchSwitchInst>(BB.getFirstNonPHIIt()))
871 continue;
872
873 for (Instruction &I : make_early_inc_range(BB)) {
874 auto *PN = dyn_cast<PHINode>(&I);
875 // Stop at the first non-PHI.
876 if (!PN)
877 break;
878
879 AllocaInst *SpillSlot = insertPHILoads(PN, F);
880 if (SpillSlot)
881 insertPHIStores(PN, SpillSlot);
882
883 PHINodes.push_back(PN);
884 }
885 }
886
887 for (auto *PN : PHINodes) {
888 // There may be lingering uses on other EH PHIs being removed
889 PN->replaceAllUsesWith(PoisonValue::get(PN->getType()));
890 PN->eraseFromParent();
891 }
892}
893
894void WinEHPrepareImpl::cloneCommonBlocks(Function &F) {
895 // We need to clone all blocks which belong to multiple funclets. Values are
896 // remapped throughout the funclet to propagate both the new instructions
897 // *and* the new basic blocks themselves.
898 for (auto &Funclets : FuncletBlocks) {
899 BasicBlock *FuncletPadBB = Funclets.first;
900 std::vector<BasicBlock *> &BlocksInFunclet = Funclets.second;
901 Value *FuncletToken;
902 if (FuncletPadBB == &F.getEntryBlock())
903 FuncletToken = ConstantTokenNone::get(F.getContext());
904 else
905 FuncletToken = &*FuncletPadBB->getFirstNonPHIIt();
906
907 std::vector<std::pair<BasicBlock *, BasicBlock *>> Orig2Clone;
909 for (BasicBlock *BB : BlocksInFunclet) {
910 ColorVector &ColorsForBB = BlockColors[BB];
911 // We don't need to do anything if the block is monochromatic.
912 size_t NumColorsForBB = ColorsForBB.size();
913 if (NumColorsForBB == 1)
914 continue;
915
916 DEBUG_WITH_TYPE("win-eh-prepare-coloring",
917 dbgs() << " Cloning block \'" << BB->getName()
918 << "\' for funclet \'" << FuncletPadBB->getName()
919 << "\'.\n");
920
921 // Create a new basic block and copy instructions into it!
922 BasicBlock *CBB =
923 CloneBasicBlock(BB, VMap, Twine(".for.", FuncletPadBB->getName()));
924 // Insert the clone immediately after the original to ensure determinism
925 // and to keep the same relative ordering of any funclet's blocks.
926 CBB->insertInto(&F, BB->getNextNode());
927
928 // Add basic block mapping.
929 VMap[BB] = CBB;
930
931 // Record delta operations that we need to perform to our color mappings.
932 Orig2Clone.emplace_back(BB, CBB);
933 }
934
935 // If nothing was cloned, we're done cloning in this funclet.
936 if (Orig2Clone.empty())
937 continue;
938
939 // Update our color mappings to reflect that one block has lost a color and
940 // another has gained a color.
941 for (auto &BBMapping : Orig2Clone) {
942 BasicBlock *OldBlock = BBMapping.first;
943 BasicBlock *NewBlock = BBMapping.second;
944
945 BlocksInFunclet.push_back(NewBlock);
946 ColorVector &NewColors = BlockColors[NewBlock];
947 assert(NewColors.empty() && "A new block should only have one color!");
948 NewColors.push_back(FuncletPadBB);
949
950 DEBUG_WITH_TYPE("win-eh-prepare-coloring",
951 dbgs() << " Assigned color \'" << FuncletPadBB->getName()
952 << "\' to block \'" << NewBlock->getName()
953 << "\'.\n");
954
955 llvm::erase(BlocksInFunclet, OldBlock);
956 ColorVector &OldColors = BlockColors[OldBlock];
957 llvm::erase(OldColors, FuncletPadBB);
958
959 DEBUG_WITH_TYPE("win-eh-prepare-coloring",
960 dbgs() << " Removed color \'" << FuncletPadBB->getName()
961 << "\' from block \'" << OldBlock->getName()
962 << "\'.\n");
963 }
964
965 // Loop over all of the instructions in this funclet, fixing up operand
966 // references as we go. This uses VMap to do all the hard work.
967 for (BasicBlock *BB : BlocksInFunclet)
968 // Loop over all instructions, fixing each one as we find it...
969 for (Instruction &I : *BB)
970 RemapInstruction(&I, VMap,
972
973 // Catchrets targeting cloned blocks need to be updated separately from
974 // the loop above because they are not in the current funclet.
976 for (auto &BBMapping : Orig2Clone) {
977 BasicBlock *OldBlock = BBMapping.first;
978 BasicBlock *NewBlock = BBMapping.second;
979
980 FixupCatchrets.clear();
981 for (BasicBlock *Pred : predecessors(OldBlock))
982 if (auto *CatchRet = dyn_cast<CatchReturnInst>(Pred->getTerminator()))
983 if (CatchRet->getCatchSwitchParentPad() == FuncletToken)
984 FixupCatchrets.push_back(CatchRet);
985
986 for (CatchReturnInst *CatchRet : FixupCatchrets)
987 CatchRet->setSuccessor(NewBlock);
988 }
989
990 auto UpdatePHIOnClonedBlock = [&](PHINode *PN, bool IsForOldBlock) {
991 unsigned NumPreds = PN->getNumIncomingValues();
992 for (unsigned PredIdx = 0, PredEnd = NumPreds; PredIdx != PredEnd;
993 ++PredIdx) {
994 BasicBlock *IncomingBlock = PN->getIncomingBlock(PredIdx);
995 bool EdgeTargetsFunclet;
996 if (auto *CRI =
997 dyn_cast<CatchReturnInst>(IncomingBlock->getTerminator())) {
998 EdgeTargetsFunclet = (CRI->getCatchSwitchParentPad() == FuncletToken);
999 } else {
1000 ColorVector &IncomingColors = BlockColors[IncomingBlock];
1001 assert(!IncomingColors.empty() && "Block not colored!");
1002 assert((IncomingColors.size() == 1 ||
1003 !llvm::is_contained(IncomingColors, FuncletPadBB)) &&
1004 "Cloning should leave this funclet's blocks monochromatic");
1005 EdgeTargetsFunclet = (IncomingColors.front() == FuncletPadBB);
1006 }
1007 if (IsForOldBlock != EdgeTargetsFunclet)
1008 continue;
1009 PN->removeIncomingValue(IncomingBlock, /*DeletePHIIfEmpty=*/false);
1010 // Revisit the next entry.
1011 --PredIdx;
1012 --PredEnd;
1013 }
1014 };
1015
1016 for (auto &BBMapping : Orig2Clone) {
1017 BasicBlock *OldBlock = BBMapping.first;
1018 BasicBlock *NewBlock = BBMapping.second;
1019 for (PHINode &OldPN : OldBlock->phis()) {
1020 UpdatePHIOnClonedBlock(&OldPN, /*IsForOldBlock=*/true);
1021 }
1022 for (PHINode &NewPN : NewBlock->phis()) {
1023 UpdatePHIOnClonedBlock(&NewPN, /*IsForOldBlock=*/false);
1024 }
1025 }
1026
1027 // Check to see if SuccBB has PHI nodes. If so, we need to add entries to
1028 // the PHI nodes for NewBB now.
1029 for (auto &BBMapping : Orig2Clone) {
1030 BasicBlock *OldBlock = BBMapping.first;
1031 BasicBlock *NewBlock = BBMapping.second;
1032 for (BasicBlock *SuccBB : successors(NewBlock)) {
1033 for (PHINode &SuccPN : SuccBB->phis()) {
1034 // Ok, we have a PHI node. Figure out what the incoming value was for
1035 // the OldBlock.
1036 int OldBlockIdx = SuccPN.getBasicBlockIndex(OldBlock);
1037 if (OldBlockIdx == -1)
1038 break;
1039 Value *IV = SuccPN.getIncomingValue(OldBlockIdx);
1040
1041 // Remap the value if necessary.
1042 if (auto *Inst = dyn_cast<Instruction>(IV)) {
1043 ValueToValueMapTy::iterator I = VMap.find(Inst);
1044 if (I != VMap.end())
1045 IV = I->second;
1046 }
1047
1048 SuccPN.addIncoming(IV, NewBlock);
1049 }
1050 }
1051 }
1052
1053 for (ValueToValueMapTy::value_type VT : VMap) {
1054 // If there were values defined in BB that are used outside the funclet,
1055 // then we now have to update all uses of the value to use either the
1056 // original value, the cloned value, or some PHI derived value. This can
1057 // require arbitrary PHI insertion, of which we are prepared to do, clean
1058 // these up now.
1059 SmallVector<Use *, 16> UsesToRename;
1060
1061 auto *OldI = dyn_cast<Instruction>(const_cast<Value *>(VT.first));
1062 if (!OldI)
1063 continue;
1064 auto *NewI = cast<Instruction>(VT.second);
1065 // Scan all uses of this instruction to see if it is used outside of its
1066 // funclet, and if so, record them in UsesToRename.
1067 for (Use &U : OldI->uses()) {
1068 Instruction *UserI = cast<Instruction>(U.getUser());
1069 BasicBlock *UserBB = UserI->getParent();
1070 ColorVector &ColorsForUserBB = BlockColors[UserBB];
1071 assert(!ColorsForUserBB.empty());
1072 if (ColorsForUserBB.size() > 1 ||
1073 *ColorsForUserBB.begin() != FuncletPadBB)
1074 UsesToRename.push_back(&U);
1075 }
1076
1077 // If there are no uses outside the block, we're done with this
1078 // instruction.
1079 if (UsesToRename.empty())
1080 continue;
1081
1082 // We found a use of OldI outside of the funclet. Rename all uses of OldI
1083 // that are outside its funclet to be uses of the appropriate PHI node
1084 // etc.
1085 SSAUpdater SSAUpdate;
1086 SSAUpdate.Initialize(OldI->getType(), OldI->getName());
1087 SSAUpdate.AddAvailableValue(OldI->getParent(), OldI);
1088 SSAUpdate.AddAvailableValue(NewI->getParent(), NewI);
1089
1090 while (!UsesToRename.empty())
1091 SSAUpdate.RewriteUseAfterInsertions(*UsesToRename.pop_back_val());
1092 }
1093 }
1094}
1095
1096void WinEHPrepareImpl::removeImplausibleInstructions(Function &F) {
1097 // Remove implausible terminators and replace them with UnreachableInst.
1098 for (auto &Funclet : FuncletBlocks) {
1099 BasicBlock *FuncletPadBB = Funclet.first;
1100 std::vector<BasicBlock *> &BlocksInFunclet = Funclet.second;
1101 Instruction *FirstNonPHI = &*FuncletPadBB->getFirstNonPHIIt();
1102 auto *FuncletPad = dyn_cast<FuncletPadInst>(FirstNonPHI);
1103 auto *CatchPad = dyn_cast_or_null<CatchPadInst>(FuncletPad);
1104 auto *CleanupPad = dyn_cast_or_null<CleanupPadInst>(FuncletPad);
1105
1106 for (BasicBlock *BB : BlocksInFunclet) {
1107 for (Instruction &I : *BB) {
1108 auto *CB = dyn_cast<CallBase>(&I);
1109 if (!CB)
1110 continue;
1111
1112 Value *FuncletBundleOperand = nullptr;
1113 if (auto BU = CB->getOperandBundle(LLVMContext::OB_funclet))
1114 FuncletBundleOperand = BU->Inputs.front();
1115
1116 if (FuncletBundleOperand == FuncletPad)
1117 continue;
1118
1119 // Skip call sites which are nounwind intrinsics or inline asm.
1120 auto *CalledFn =
1121 dyn_cast<Function>(CB->getCalledOperand()->stripPointerCasts());
1122 if (CalledFn && ((CalledFn->isIntrinsic() && CB->doesNotThrow()) ||
1123 CB->isInlineAsm()))
1124 continue;
1125
1126 // This call site was not part of this funclet, remove it.
1127 if (isa<InvokeInst>(CB)) {
1128 // Remove the unwind edge if it was an invoke.
1129 removeUnwindEdge(BB);
1130 // Get a pointer to the new call.
1131 BasicBlock::iterator CallI =
1132 std::prev(BB->getTerminator()->getIterator());
1133 auto *CI = cast<CallInst>(&*CallI);
1135 } else {
1137 }
1138
1139 // There are no more instructions in the block (except for unreachable),
1140 // we are done.
1141 break;
1142 }
1143
1144 Instruction *TI = BB->getTerminator();
1145 // CatchPadInst and CleanupPadInst can't transfer control to a ReturnInst.
1146 bool IsUnreachableRet = isa<ReturnInst>(TI) && FuncletPad;
1147 // The token consumed by a CatchReturnInst must match the funclet token.
1148 bool IsUnreachableCatchret = false;
1149 if (auto *CRI = dyn_cast<CatchReturnInst>(TI))
1150 IsUnreachableCatchret = CRI->getCatchPad() != CatchPad;
1151 // The token consumed by a CleanupReturnInst must match the funclet token.
1152 bool IsUnreachableCleanupret = false;
1153 if (auto *CRI = dyn_cast<CleanupReturnInst>(TI))
1154 IsUnreachableCleanupret = CRI->getCleanupPad() != CleanupPad;
1155 if (IsUnreachableRet || IsUnreachableCatchret ||
1156 IsUnreachableCleanupret) {
1158 } else if (isa<InvokeInst>(TI)) {
1159 if (Personality == EHPersonality::MSVC_CXX && CleanupPad) {
1160 // Invokes within a cleanuppad for the MSVC++ personality never
1161 // transfer control to their unwind edge: the personality will
1162 // terminate the program.
1163 removeUnwindEdge(BB);
1164 }
1165 }
1166 }
1167 }
1168}
1169
1170void WinEHPrepareImpl::cleanupPreparedFunclets(Function &F) {
1171 // Clean-up some of the mess we made by removing useles PHI nodes, trivial
1172 // branches, etc.
1175 ConstantFoldTerminator(&BB, /*DeleteDeadConditions=*/true);
1177 }
1178
1179 // We might have some unreachable blocks after cleaning up some impossible
1180 // control flow.
1182}
1183
1184#ifndef NDEBUG
1185void WinEHPrepareImpl::verifyPreparedFunclets(Function &F) {
1186 for (BasicBlock &BB : F) {
1187 size_t NumColors = BlockColors[&BB].size();
1188 assert(NumColors == 1 && "Expected monochromatic BB!");
1189 if (NumColors == 0)
1190 report_fatal_error("Uncolored BB!");
1191 if (NumColors > 1)
1192 report_fatal_error("Multicolor BB!");
1193 assert((DisableDemotion || !(BB.isEHPad() && isa<PHINode>(BB.begin()))) &&
1194 "EH Pad still has a PHI!");
1195 }
1196}
1197#endif
1198
1199bool WinEHPrepareImpl::prepareExplicitEH(Function &F) {
1200 // Remove unreachable blocks. It is not valuable to assign them a color and
1201 // their existence can trick us into thinking values are alive when they are
1202 // not.
1204
1205 // Determine which blocks are reachable from which funclet entries.
1206 colorFunclets(F);
1207
1208 cloneCommonBlocks(F);
1209
1210 if (!DisableDemotion)
1211 demotePHIsOnFunclets(F, DemoteCatchSwitchPHIOnly ||
1213
1214 if (!DisableCleanups) {
1215 assert(!verifyFunction(F, &dbgs()));
1216 removeImplausibleInstructions(F);
1217
1218 assert(!verifyFunction(F, &dbgs()));
1219 cleanupPreparedFunclets(F);
1220 }
1221
1222 LLVM_DEBUG(verifyPreparedFunclets(F));
1223 // Recolor the CFG to verify that all is well.
1224 LLVM_DEBUG(colorFunclets(F));
1225 LLVM_DEBUG(verifyPreparedFunclets(F));
1226
1227 return true;
1228}
1229
1230// TODO: Share loads when one use dominates another, or when a catchpad exit
1231// dominates uses (needs dominators).
1232AllocaInst *WinEHPrepareImpl::insertPHILoads(PHINode *PN, Function &F) {
1233 BasicBlock *PHIBlock = PN->getParent();
1234 AllocaInst *SpillSlot = nullptr;
1235 Instruction *EHPad = &*PHIBlock->getFirstNonPHIIt();
1236
1237 if (!EHPad->isTerminator()) {
1238 // If the EHPad isn't a terminator, then we can insert a load in this block
1239 // that will dominate all uses.
1240 SpillSlot = new AllocaInst(PN->getType(), DL->getAllocaAddrSpace(), nullptr,
1241 Twine(PN->getName(), ".wineh.spillslot"),
1242 F.getEntryBlock().begin());
1243 Value *V = new LoadInst(PN->getType(), SpillSlot,
1244 Twine(PN->getName(), ".wineh.reload"),
1245 PHIBlock->getFirstInsertionPt());
1246 PN->replaceAllUsesWith(V);
1247 return SpillSlot;
1248 }
1249
1250 // Otherwise, we have a PHI on a terminator EHPad, and we give up and insert
1251 // loads of the slot before every use.
1253 for (Use &U : llvm::make_early_inc_range(PN->uses())) {
1254 auto *UsingInst = cast<Instruction>(U.getUser());
1255 if (isa<PHINode>(UsingInst) && UsingInst->getParent()->isEHPad()) {
1256 // Use is on an EH pad phi. Leave it alone; we'll insert loads and
1257 // stores for it separately.
1258 continue;
1259 }
1260 replaceUseWithLoad(PN, U, SpillSlot, Loads, F);
1261 }
1262 return SpillSlot;
1263}
1264
1265// TODO: improve store placement. Inserting at def is probably good, but need
1266// to be careful not to introduce interfering stores (needs liveness analysis).
1267// TODO: identify related phi nodes that can share spill slots, and share them
1268// (also needs liveness).
1269void WinEHPrepareImpl::insertPHIStores(PHINode *OriginalPHI,
1270 AllocaInst *SpillSlot) {
1271 // Use a worklist of (Block, Value) pairs -- the given Value needs to be
1272 // stored to the spill slot by the end of the given Block.
1274
1275 Worklist.push_back({OriginalPHI->getParent(), OriginalPHI});
1276
1277 while (!Worklist.empty()) {
1278 BasicBlock *EHBlock;
1279 Value *InVal;
1280 std::tie(EHBlock, InVal) = Worklist.pop_back_val();
1281
1282 PHINode *PN = dyn_cast<PHINode>(InVal);
1283 if (PN && PN->getParent() == EHBlock) {
1284 // The value is defined by another PHI we need to remove, with no room to
1285 // insert a store after the PHI, so each predecessor needs to store its
1286 // incoming value.
1287 for (unsigned i = 0, e = PN->getNumIncomingValues(); i < e; ++i) {
1288 Value *PredVal = PN->getIncomingValue(i);
1289
1290 // Undef can safely be skipped.
1291 if (isa<UndefValue>(PredVal))
1292 continue;
1293
1294 insertPHIStore(PN->getIncomingBlock(i), PredVal, SpillSlot, Worklist);
1295 }
1296 } else {
1297 // We need to store InVal, which dominates EHBlock, but can't put a store
1298 // in EHBlock, so need to put stores in each predecessor.
1299 for (BasicBlock *PredBlock : predecessors(EHBlock)) {
1300 insertPHIStore(PredBlock, InVal, SpillSlot, Worklist);
1301 }
1302 }
1303 }
1304}
1305
1306void WinEHPrepareImpl::insertPHIStore(
1307 BasicBlock *PredBlock, Value *PredVal, AllocaInst *SpillSlot,
1308 SmallVectorImpl<std::pair<BasicBlock *, Value *>> &Worklist) {
1309
1310 if (PredBlock->isEHPad() && PredBlock->getFirstNonPHIIt()->isTerminator()) {
1311 // Pred is unsplittable, so we need to queue it on the worklist.
1312 Worklist.push_back({PredBlock, PredVal});
1313 return;
1314 }
1315
1316 // Otherwise, insert the store at the end of the basic block.
1317 new StoreInst(PredVal, SpillSlot, PredBlock->getTerminator()->getIterator());
1318}
1319
1320void WinEHPrepareImpl::replaceUseWithLoad(
1321 Value *V, Use &U, AllocaInst *&SpillSlot,
1323 // Lazilly create the spill slot.
1324 if (!SpillSlot)
1325 SpillSlot = new AllocaInst(V->getType(), DL->getAllocaAddrSpace(), nullptr,
1326 Twine(V->getName(), ".wineh.spillslot"),
1327 F.getEntryBlock().begin());
1328
1329 auto *UsingInst = cast<Instruction>(U.getUser());
1330 if (auto *UsingPHI = dyn_cast<PHINode>(UsingInst)) {
1331 // If this is a PHI node, we can't insert a load of the value before
1332 // the use. Instead insert the load in the predecessor block
1333 // corresponding to the incoming value.
1334 //
1335 // Note that if there are multiple edges from a basic block to this
1336 // PHI node that we cannot have multiple loads. The problem is that
1337 // the resulting PHI node will have multiple values (from each load)
1338 // coming in from the same block, which is illegal SSA form.
1339 // For this reason, we keep track of and reuse loads we insert.
1340 BasicBlock *IncomingBlock = UsingPHI->getIncomingBlock(U);
1341 if (auto *CatchRet =
1342 dyn_cast<CatchReturnInst>(IncomingBlock->getTerminator())) {
1343 // Putting a load above a catchret and use on the phi would still leave
1344 // a cross-funclet def/use. We need to split the edge, change the
1345 // catchret to target the new block, and put the load there.
1346 BasicBlock *PHIBlock = UsingInst->getParent();
1347 BasicBlock *NewBlock = SplitEdge(IncomingBlock, PHIBlock);
1348 // SplitEdge gives us:
1349 // IncomingBlock:
1350 // ...
1351 // br label %NewBlock
1352 // NewBlock:
1353 // catchret label %PHIBlock
1354 // But we need:
1355 // IncomingBlock:
1356 // ...
1357 // catchret label %NewBlock
1358 // NewBlock:
1359 // br label %PHIBlock
1360 // So move the terminators to each others' blocks and swap their
1361 // successors.
1362 BranchInst *Goto = cast<BranchInst>(IncomingBlock->getTerminator());
1363 Goto->removeFromParent();
1364 CatchRet->removeFromParent();
1365 CatchRet->insertInto(IncomingBlock, IncomingBlock->end());
1366 Goto->insertInto(NewBlock, NewBlock->end());
1367 Goto->setSuccessor(0, PHIBlock);
1368 CatchRet->setSuccessor(NewBlock);
1369 // Update the color mapping for the newly split edge.
1370 // Grab a reference to the ColorVector to be inserted before getting the
1371 // reference to the vector we are copying because inserting the new
1372 // element in BlockColors might cause the map to be reallocated.
1373 ColorVector &ColorsForNewBlock = BlockColors[NewBlock];
1374 ColorVector &ColorsForPHIBlock = BlockColors[PHIBlock];
1375 ColorsForNewBlock = ColorsForPHIBlock;
1376 for (BasicBlock *FuncletPad : ColorsForPHIBlock)
1377 FuncletBlocks[FuncletPad].push_back(NewBlock);
1378 // Treat the new block as incoming for load insertion.
1379 IncomingBlock = NewBlock;
1380 }
1381 Value *&Load = Loads[IncomingBlock];
1382 // Insert the load into the predecessor block
1383 if (!Load)
1384 Load = new LoadInst(
1385 V->getType(), SpillSlot, Twine(V->getName(), ".wineh.reload"),
1386 /*isVolatile=*/false, IncomingBlock->getTerminator()->getIterator());
1387
1388 U.set(Load);
1389 } else {
1390 // Reload right before the old use.
1391 auto *Load = new LoadInst(V->getType(), SpillSlot,
1392 Twine(V->getName(), ".wineh.reload"),
1393 /*isVolatile=*/false, UsingInst->getIterator());
1394 U.set(Load);
1395 }
1396}
1397
1399 MCSymbol *InvokeBegin,
1400 MCSymbol *InvokeEnd) {
1401 assert(InvokeStateMap.count(II) &&
1402 "should get invoke with precomputed state");
1403 LabelToStateMap[InvokeBegin] = std::make_pair(InvokeStateMap[II], InvokeEnd);
1404}
1405
1406void WinEHFuncInfo::addIPToStateRange(int State, MCSymbol* InvokeBegin,
1407 MCSymbol* InvokeEnd) {
1408 LabelToStateMap[InvokeBegin] = std::make_pair(State, InvokeEnd);
1409}
1410
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
This file contains the declarations for the subclasses of Constant, which represent the different fla...
#define LLVM_DEBUG(...)
Definition: Debug.h:106
#define DEBUG_WITH_TYPE(TYPE,...)
DEBUG_WITH_TYPE macro - This macro should be used by passes to emit debug information.
Definition: Debug.h:64
This file defines the DenseMap class.
static bool runOnFunction(Function &F, bool PostInlining)
#define DEBUG_TYPE
static const HTTPClientCleanup Cleanup
Definition: HTTPClient.cpp:42
Module.h This file contains the declarations for the Module class.
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
This file implements a map that provides insertion order iteration.
uint64_t IntrinsicInst * II
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:38
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file contains some templates that are useful if you are working with the STL at all.
static cl::opt< bool > DisableDemotion("disable-demotion", cl::Hidden, cl::desc("Clone multicolor basic blocks but do not demote cross scopes"), cl::init(false))
static int addUnwindMapEntry(WinEHFuncInfo &FuncInfo, int ToState, const BasicBlock *BB)
static void calculateStateNumbersForInvokes(const Function *Fn, WinEHFuncInfo &FuncInfo)
static BasicBlock * getCleanupRetUnwindDest(const CleanupPadInst *CleanupPad)
static cl::opt< bool > DisableCleanups("disable-cleanups", cl::Hidden, cl::desc("Do not remove implausible terminators or other similar cleanups"), cl::init(false))
static int addSEHFinally(WinEHFuncInfo &FuncInfo, int ParentState, const BasicBlock *Handler)
static const BasicBlock * getEHPadFromPredecessor(const BasicBlock *BB, Value *ParentPad)
static int addClrEHHandler(WinEHFuncInfo &FuncInfo, int HandlerParentState, int TryParentState, ClrHandlerType HandlerType, uint32_t TypeToken, const BasicBlock *Handler)
static void calculateCXXStateNumbers(WinEHFuncInfo &FuncInfo, const Instruction *FirstNonPHI, int ParentState)
static cl::opt< bool > DemoteCatchSwitchPHIOnlyOpt("demote-catchswitch-only", cl::Hidden, cl::desc("Demote catchswitch BBs only (for wasm EH)"), cl::init(false))
static void addTryBlockMapEntry(WinEHFuncInfo &FuncInfo, int TryLow, int TryHigh, int CatchHigh, ArrayRef< const CatchPadInst * > Handlers)
static bool isTopLevelPadForMSVC(const Instruction *EHPad)
static int addSEHExcept(WinEHFuncInfo &FuncInfo, int ParentState, const Function *Filter, const BasicBlock *Handler)
static const uint32_t IV[8]
Definition: blake3_impl.h:78
an instruction to allocate memory on the stack
Definition: Instructions.h:63
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:253
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
LLVM Basic Block Representation.
Definition: BasicBlock.h:61
iterator end()
Definition: BasicBlock.h:474
iterator_range< const_phi_iterator > phis() const
Returns a range that iterates over the phis in the basic block.
Definition: BasicBlock.h:530
const_iterator getFirstInsertionPt() const
Returns an iterator to the first instruction in this block that is suitable for inserting a non-PHI i...
Definition: BasicBlock.cpp:437
InstListType::const_iterator getFirstNonPHIIt() const
Returns an iterator to the first instruction in this block that is not a PHINode instruction.
Definition: BasicBlock.cpp:381
InstListType::const_iterator const_iterator
Definition: BasicBlock.h:178
const Instruction & front() const
Definition: BasicBlock.h:484
const Function * getParent() const
Return the enclosing method, or null if none.
Definition: BasicBlock.h:220
void insertInto(Function *Parent, BasicBlock *InsertBefore=nullptr)
Insert unlinked basic block into a function.
Definition: BasicBlock.cpp:198
InstListType::iterator iterator
Instruction iterators...
Definition: BasicBlock.h:177
bool isEHPad() const
Return true if this basic block is an exception handling block.
Definition: BasicBlock.h:688
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
Definition: BasicBlock.h:240
Conditional or Unconditional Branch instruction.
void setSuccessor(unsigned idx, BasicBlock *NewSucc)
static ConstantTokenNone * get(LLVMContext &Context)
Return the ConstantTokenNone.
Definition: Constants.cpp:1522
This is an important base class in LLVM.
Definition: Constant.h:42
const Constant * stripPointerCasts() const
Definition: Constant.h:218
bool isNullValue() const
Return true if this is the value that would be returned by getNullValue.
Definition: Constants.cpp:90
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:63
iterator find(const_arg_type_t< KeyT > Val)
Definition: DenseMap.h:156
iterator end()
Definition: DenseMap.h:84
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:310
virtual bool runOnFunction(Function &F)=0
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass.
const BasicBlock & getEntryBlock() const
Definition: Function.h:821
const DataLayout & getDataLayout() const
Get the data layout of the module this function belongs to.
Definition: Function.cpp:373
Intrinsic::ID getIntrinsicID() const LLVM_READONLY
getIntrinsicID - This method returns the ID number of the specified function, or Intrinsic::not_intri...
Definition: Function.h:251
bool hasPersonalityFn() const
Check whether this function has a personality function.
Definition: Function.h:917
Constant * getPersonalityFn() const
Get the personality function associated with this function.
Definition: Function.cpp:1048
bool isIntrinsic() const
isIntrinsic - Returns true if the function's name starts with "llvm.".
Definition: Function.h:256
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:657
void removeFromParent()
This method unlinks 'this' from the containing basic block, but does not delete it.
Definition: Instruction.cpp:80
bool isEHPad() const
Return true if the instruction is a variety of EH-block.
Definition: Instruction.h:869
bool isTerminator() const
Definition: Instruction.h:313
InstListType::iterator insertInto(BasicBlock *ParentBB, InstListType::iterator It)
Inserts an unlinked instruction into ParentBB at position It and returns the iterator of the inserted...
Invoke instruction.
An instruction for reading from memory.
Definition: Instructions.h:176
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
This class implements a map that also provides access to all stored values in a deterministic order.
Definition: MapVector.h:36
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
Metadata * getModuleFlag(StringRef Key) const
Return the corresponding value if Key appears in module flags, otherwise return null.
Definition: Module.cpp:354
Value * removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty=true)
Remove an incoming value.
BasicBlock * getIncomingBlock(unsigned i) const
Return incoming basic block number i.
Value * getIncomingValue(unsigned i) const
Return incoming value number x.
unsigned getNumIncomingValues() const
Return the number of incoming edges.
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
Definition: Pass.cpp:81
static PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
Definition: Constants.cpp:1878
A set of analyses that are preserved following a run of a transformation pass.
Definition: Analysis.h:111
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
Definition: Analysis.h:114
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: Analysis.h:117
Helper class for SSA formation on a set of values defined in multiple blocks.
Definition: SSAUpdater.h:40
void RewriteUseAfterInsertions(Use &U)
Rewrite a use like RewriteUse but handling in-block definitions.
Definition: SSAUpdater.cpp:247
void Initialize(Type *Ty, StringRef Name)
Reset this object to get ready for a new set of SSA updates with type 'Ty'.
Definition: SSAUpdater.cpp:52
void AddAvailableValue(BasicBlock *BB, Value *V)
Indicate that a rewritten value is available in the specified block with the specified value.
Definition: SSAUpdater.cpp:69
bool empty() const
Definition: SmallVector.h:81
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:573
reference emplace_back(ArgTypes &&... Args)
Definition: SmallVector.h:937
void push_back(const T &Elt)
Definition: SmallVector.h:413
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1196
An instruction for storing to memory.
Definition: Instructions.h:292
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
TinyPtrVector - This class is specialized for cases where there are normally 0 or 1 element in a vect...
Definition: TinyPtrVector.h:29
void push_back(EltTy NewVal)
EltTy front() const
bool empty() const
unsigned size() const
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
bool isArch64Bit() const
Test whether the architecture is 64-bit.
Definition: Triple.cpp:1734
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
A Use represents the edge between a Value definition and its users.
Definition: Use.h:35
std::pair< const Value *, WeakTrackingVH > value_type
Definition: ValueMap.h:99
iterator find(const KeyT &Val)
Definition: ValueMap.h:155
iterator end()
Definition: ValueMap.h:135
LLVM Value Representation.
Definition: Value.h:74
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:255
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition: Value.cpp:534
iterator_range< user_iterator > users()
Definition: Value.h:421
iterator_range< use_iterator > uses()
Definition: Value.h:376
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:309
PreservedAnalyses run(Function &F, FunctionAnalysisManager &FAM)
const ParentTy * getParent() const
Definition: ilist_node.h:32
self_iterator getIterator()
Definition: ilist_node.h:132
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:443
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
bool ConstantFoldTerminator(BasicBlock *BB, bool DeleteDeadConditions=false, const TargetLibraryInfo *TLI=nullptr, DomTreeUpdater *DTU=nullptr)
If a terminator instruction is predicated on a constant value, convert it into an unconditional branc...
Definition: Local.cpp:136
bool verifyFunction(const Function &F, raw_ostream *OS=nullptr)
Check a function for errors, useful for use when debugging a pass.
Definition: Verifier.cpp:7301
auto successors(const MachineBasicBlock *BB)
DenseMap< BasicBlock *, ColorVector > colorEHFunclets(Function &F)
If an EH funclet personality is in use (see isFuncletEHPersonality), this will recompute which blocks...
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
Definition: STLExtras.h:657
bool SimplifyInstructionsInBlock(BasicBlock *BB, const TargetLibraryInfo *TLI=nullptr)
Scan the specified basic block and try to simplify any instructions in it and recursively delete dead...
Definition: Local.cpp:734
void calculateWinCXXEHStateNumbers(const Function *ParentFn, WinEHFuncInfo &FuncInfo)
Analyze the IR in ParentFn and it's handlers to build WinEHFuncInfo, which describes the state number...
bool isScopedEHPersonality(EHPersonality Pers)
Returns true if this personality uses scope-style EH IR instructions: catchswitch,...
void erase(Container &C, ValueType V)
Wrapper function to remove a value from a container:
Definition: STLExtras.h:2107
auto reverse(ContainerTy &&C)
Definition: STLExtras.h:420
@ RF_IgnoreMissingLocals
If this flag is set, the remapper ignores missing function-local entries (Argument,...
Definition: ValueMapper.h:96
@ RF_NoModuleLevelChanges
If this flag is set, the remapper knows that only local values within a function (such as an instruct...
Definition: ValueMapper.h:78
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
void RemapInstruction(Instruction *I, ValueToValueMapTy &VM, RemapFlags Flags=RF_None, ValueMapTypeRemapper *TypeMapper=nullptr, ValueMaterializer *Materializer=nullptr, const MetadataSetTy *IdentityMD=nullptr)
Convert the instruction operands from referencing the current values into those specified by VM.
Definition: ValueMapper.h:277
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:167
Instruction * removeUnwindEdge(BasicBlock *BB, DomTreeUpdater *DTU=nullptr)
Replace 'BB's terminator with one that does not have an unwind successor block.
Definition: Local.cpp:3233
void calculateSEHStateForAsynchEH(const BasicBlock *BB, int State, WinEHFuncInfo &FuncInfo)
EHPersonality classifyEHPersonality(const Value *Pers)
See if the given exception handling personality function is one that we understand.
unsigned changeToUnreachable(Instruction *I, bool PreserveLCSSA=false, DomTreeUpdater *DTU=nullptr, MemorySSAUpdater *MSSAU=nullptr)
Insert an unreachable instruction before the specified instruction, making it and the rest of the cod...
Definition: Local.cpp:2906
void calculateCXXStateForAsynchEH(const BasicBlock *BB, int State, WinEHFuncInfo &FuncInfo)
@ Mod
The access may modify the value stored in memory.
BasicBlock * CloneBasicBlock(const BasicBlock *BB, ValueToValueMapTy &VMap, const Twine &NameSuffix="", Function *F=nullptr, ClonedCodeInfo *CodeInfo=nullptr)
Return a copy of the specified basic block, but without embedding the block into a particular functio...
void calculateSEHStateNumbers(const Function *ParentFn, WinEHFuncInfo &FuncInfo)
bool MergeBlockIntoPredecessor(BasicBlock *BB, DomTreeUpdater *DTU=nullptr, LoopInfo *LI=nullptr, MemorySSAUpdater *MSSAU=nullptr, MemoryDependenceResults *MemDep=nullptr, bool PredecessorWithTwoSuccessors=false, DominatorTree *DT=nullptr)
Attempts to merge a block into its predecessor, if possible.
ClrHandlerType
Definition: WinEHFuncInfo.h:79
auto predecessors(const MachineBasicBlock *BB)
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition: STLExtras.h:1903
BasicBlock * SplitEdge(BasicBlock *From, BasicBlock *To, DominatorTree *DT=nullptr, LoopInfo *LI=nullptr, MemorySSAUpdater *MSSAU=nullptr, const Twine &BBName="")
Split the edge connecting the specified blocks, and return the newly created basic block between From...
bool removeUnreachableBlocks(Function &F, DomTreeUpdater *DTU=nullptr, MemorySSAUpdater *MSSAU=nullptr)
Remove all blocks that can not be reached from the function's entry.
Definition: Local.cpp:3271
FunctionPass * createWinEHPass(bool DemoteCatchSwitchPHIOnly=false)
createWinEHPass - Prepares personality functions used by MSVC on Windows, in addition to the Itanium ...
void calculateClrEHStateNumbers(const Function *Fn, WinEHFuncInfo &FuncInfo)
const BasicBlock * Block
WorkItem(const BasicBlock *BB, int St)
MBBOrBasicBlock Cleanup
Definition: WinEHFuncInfo.h:42
Similar to CxxUnwindMapEntry, but supports SEH filters.
Definition: WinEHFuncInfo.h:46
void addIPToStateRange(const InvokeInst *II, MCSymbol *InvokeBegin, MCSymbol *InvokeEnd)
SmallVector< SEHUnwindMapEntry, 4 > SEHUnwindMap
Definition: WinEHFuncInfo.h:98
SmallVector< ClrEHUnwindMapEntry, 4 > ClrEHUnwindMap
Definition: WinEHFuncInfo.h:99
DenseMap< const FuncletPadInst *, int > FuncletBaseStateMap
Definition: WinEHFuncInfo.h:92
DenseMap< const BasicBlock *, int > BlockToStateMap
Definition: WinEHFuncInfo.h:95
DenseMap< const InvokeInst *, int > InvokeStateMap
Definition: WinEHFuncInfo.h:93
SmallVector< WinEHTryBlockMapEntry, 4 > TryBlockMap
Definition: WinEHFuncInfo.h:97
DenseMap< const Instruction *, int > EHPadStateMap
Definition: WinEHFuncInfo.h:91
DenseMap< MCSymbol *, std::pair< int, MCSymbol * > > LabelToStateMap
Definition: WinEHFuncInfo.h:94
SmallVector< CxxUnwindMapEntry, 4 > CxxUnwindMap
Definition: WinEHFuncInfo.h:96
int getLastStateNumber() const
union llvm::WinEHHandlerType::@253 CatchObj
The CatchObj starts out life as an LLVM alloca and is eventually turned frame index.
GlobalVariable * TypeDescriptor
Definition: WinEHFuncInfo.h:68
const AllocaInst * Alloca
Definition: WinEHFuncInfo.h:65
MBBOrBasicBlock Handler
Definition: WinEHFuncInfo.h:69
SmallVector< WinEHHandlerType, 1 > HandlerArray
Definition: WinEHFuncInfo.h:76