LLVM 24.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 "CodeGenOptions.h"
20#include "llvm/ADT/DenseMap.h"
21#include "llvm/ADT/MapVector.h"
22#include "llvm/ADT/STLExtras.h"
24#include "llvm/CodeGen/Passes.h"
26#include "llvm/IR/Constants.h"
29#include "llvm/IR/IRBuilder.h"
31#include "llvm/IR/Module.h"
32#include "llvm/IR/Verifier.h"
34#include "llvm/Pass.h"
35#include "llvm/Support/Debug.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) {
51 if (CPI->arg_size() != 3)
52 return true;
53
54 Constant *TypeInfo = dyn_cast<Constant>(CPI->getArgOperand(0));
55 if (!TypeInfo)
56 return true;
57 if (!TypeInfo->isNullValue() &&
59 return true;
60
61 if (!isa<ConstantInt>(CPI->getArgOperand(1)))
62 return true;
63
64 return false;
65 }
68 if (CPI->arg_size() == 0)
69 return true;
70
71 Constant *FilterOrNull = dyn_cast<Constant>(CPI->getArgOperand(0));
72 if (!FilterOrNull)
73 return true;
74
75 Constant *Filter = FilterOrNull->stripPointerCasts();
76 if (!Filter->isNullValue() && !isa<Function>(Filter))
77 return true;
78
79 return false;
80 }
82 if (CPI->arg_size() == 0)
83 return true;
84
85 if (!isa<ConstantInt>(CPI->getArgOperand(0)))
86 return true;
87
88 return false;
89 }
92 if (CPI->arg_size() == 1 && !isa<Constant>(CPI->getArgOperand(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(Fn)).runOnFunction(Fn);
151 }
152};
153
154} // end anonymous namespace
155
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(Fn.getPersonalityFn());
174
175 // Do nothing if this is not a scope-based personality.
176 if (!isScopedEHPersonality(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(Personality);
183
184 DL = &Fn.getDataLayout();
185 return prepareExplicitEH(Fn);
186}
187
188static int addUnwindMapEntry(WinEHFuncInfo &FuncInfo, int ToState,
189 const BasicBlock *BB) {
191 UME.ToState = ToState;
192 UME.Cleanup = BB;
193 FuncInfo.CxxUnwindMap.push_back(UME);
194 return FuncInfo.getLastStateNumber();
195}
196
197static void addTryBlockMapEntry(WinEHFuncInfo &FuncInfo, int TryLow,
198 int TryHigh, int CatchHigh,
201 TBME.TryLow = TryLow;
202 TBME.TryHigh = TryHigh;
203 TBME.CatchHigh = CatchHigh;
204 assert(TBME.TryLow <= TBME.TryHigh);
205 for (const CatchPadInst *CPI : Handlers) {
208 "Malformed CatchPadInst not caught by win-eh-prepare");
209 Constant *TypeInfo = cast<Constant>(CPI->getArgOperand(0));
210 if (TypeInfo->isNullValue())
211 HT.TypeDescriptor = nullptr;
212 else
214 HT.Adjectives = cast<ConstantInt>(CPI->getArgOperand(1))->getZExtValue();
215 HT.Handler = CPI->getParent();
216 if (auto *AI =
217 dyn_cast<AllocaInst>(CPI->getArgOperand(2)->stripPointerCasts()))
218 HT.CatchObj.Alloca = AI;
219 else
220 HT.CatchObj.Alloca = nullptr;
221 TBME.HandlerArray.push_back(HT);
222 }
223 FuncInfo.TryBlockMap.push_back(TBME);
224}
225
227 for (const User *U : CleanupPad->users())
228 if (const auto *CRI = dyn_cast<CleanupReturnInst>(U))
229 return CRI->getUnwindDest();
230 return nullptr;
231}
232
234 WinEHFuncInfo &FuncInfo) {
235 auto *F = const_cast<Function *>(Fn);
237 for (BasicBlock &BB : *F) {
238 auto *II = dyn_cast<InvokeInst>(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 =
249 assert(FuncletPad || FuncletEntryBB == &Fn->getEntryBlock());
250 if (!FuncletPad)
251 FuncletUnwindDest = nullptr;
252 else if (auto *CatchPad = dyn_cast<CatchPadInst>(FuncletPad))
253 FuncletUnwindDest = CatchPad->getCatchSwitch()->getUnwindDest();
254 else if (auto *CleanupPad = dyn_cast<CleanupPadInst>(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(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 {
281 int State;
282 WorkItem(const BasicBlock *BB, int St) {
283 Block = BB;
284 State = St;
285 }
286};
288 WinEHFuncInfo &EHInfo) {
290 struct WorkItem *WI = new WorkItem(BB, State);
291 WorkList.push_back(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(BB);
299 if (!Inserted && StateIt->second <= State)
300 continue; // skip blocks already visited by lower State
301
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>(TI) || isa<CatchReturnInst>(TI)) && State > 0) {
309 // Retrive the new State
310 State = EHInfo.CxxUnwindMap[State].ToState; // Retrive next State
311 } else if (isa<InvokeInst>(TI)) {
312 auto *Call = cast<CallBase>(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>(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>(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(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.
350 WinEHFuncInfo &EHInfo) {
352 struct WorkItem *WI = new WorkItem(BB, State);
353 WorkList.push_back(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(BB);
361 It != EHInfo.BlockToStateMap.end() && It->second <= State)
362 continue; // skip blocks already visited by lower State
363
365 const llvm::Instruction *TI = BB->getTerminator();
366 if (It->isEHPad())
367 State = EHInfo.EHPadStateMap[&*It];
368 EHInfo.BlockToStateMap[BB] = State; // Record state
369
373 "Malformed CatchPadInst not caught by win-eh-prepare");
374 const Constant *FilterOrNull = cast<Constant>(
375 cast<CatchPadInst>(It)->getArgOperand(0)->stripPointerCasts());
376 const Function *Filter = dyn_cast<Function>(FilterOrNull);
377 if (!Filter || !Filter->getName().starts_with("__IsLocalUnwind"))
378 State = EHInfo.SEHUnwindMap[State].ToState; // Retrive next State
379 } else if ((isa<CleanupReturnInst>(TI) || isa<CatchReturnInst>(TI)) &&
380 State > 0) {
381 // Retrive the new State.
382 State = EHInfo.SEHUnwindMap[State].ToState; // Retrive next State
383 } else if (isa<InvokeInst>(TI)) {
384 auto *Call = cast<CallBase>(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>(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(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.
406 Value *ParentPad) {
407 const Instruction *TI = BB->getTerminator();
408 if (isa<InvokeInst>(TI))
409 return nullptr;
410 if (auto *CatchSwitch = dyn_cast<CatchSwitchInst>(TI)) {
411 if (CatchSwitch->getParentPad() != ParentPad)
412 return nullptr;
413 return BB;
414 }
415 assert(!TI->isEHPad() && "unexpected EHPad!");
416 auto *CleanupPad = cast<CleanupReturnInst>(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[]
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>(FirstNonPHI)) {
432 assert(FuncInfo.EHPadStateMap.count(CatchSwitch) == 0 &&
433 "shouldn't revist catch funclets!");
434
436 for (const BasicBlock *CatchPadBB : CatchSwitch->handlers()) {
437 auto *CatchPad = cast<CatchPadInst>(CatchPadBB->getFirstNonPHIIt());
438 Handlers.push_back(CatchPad);
439 }
440 int TryLow = addUnwindMapEntry(FuncInfo, ParentState, nullptr);
441 FuncInfo.EHPadStateMap[CatchSwitch] = TryLow;
442 for (const BasicBlock *PredBlock : predecessors(BB))
443 if ((PredBlock = getEHPadFromPredecessor(PredBlock,
444 CatchSwitch->getParentPad())))
445 calculateCXXStateNumbers(FuncInfo, &*PredBlock->getFirstNonPHIIt(),
446 TryLow);
447 int CatchLow = addUnwindMapEntry(FuncInfo, ParentState, 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, 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>(U);
466 if (auto *InnerCatchSwitch = dyn_cast<CatchSwitchInst>(UserI)) {
467 BasicBlock *UnwindDest = InnerCatchSwitch->getUnwindDest();
468 if (!UnwindDest || UnwindDest == CatchSwitch->getUnwindDest())
469 calculateCXXStateNumbers(FuncInfo, UserI, CatchLow);
470 }
471 if (auto *InnerCleanupPad = dyn_cast<CleanupPadInst>(UserI)) {
472 BasicBlock *UnwindDest = getCleanupRetUnwindDest(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, UserI, 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>(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(CleanupPad);
499 if (!Inserted)
500 return;
501
502 int CleanupState = addUnwindMapEntry(FuncInfo, 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(PredBlock,
508 CleanupPad->getParentPad()))) {
509 calculateCXXStateNumbers(FuncInfo, &*PredBlock->getFirstNonPHIIt(),
510 CleanupState);
511 }
512 }
513 for (const User *U : CleanupPad->users()) {
514 const auto *UserI = cast<Instruction>(U);
515 if (UserI->isEHPad())
516 report_fatal_error("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(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(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[]
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>(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>((*CatchSwitch->handler_begin())->getFirstNonPHIIt());
563 const BasicBlock *CatchPadBB = CatchPad->getParent();
565 "Malformed CatchPadInst not caught by win-eh-prepare");
566 const Constant *FilterOrNull =
567 cast<Constant>(CatchPad->getArgOperand(0)->stripPointerCasts());
568 const Function *Filter = dyn_cast<Function>(FilterOrNull);
569 assert((Filter || FilterOrNull->isNullValue()) &&
570 "unexpected filter value");
571 int TryState = addSEHExcept(FuncInfo, ParentState, Filter, 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(PredBlock,
580 CatchSwitch->getParentPad())))
581 calculateSEHStateNumbers(FuncInfo, &*PredBlock->getFirstNonPHIIt(),
582 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>(U);
588 if (auto *InnerCatchSwitch = dyn_cast<CatchSwitchInst>(UserI)) {
589 BasicBlock *UnwindDest = InnerCatchSwitch->getUnwindDest();
590 if (!UnwindDest || UnwindDest == CatchSwitch->getUnwindDest())
591 calculateSEHStateNumbers(FuncInfo, UserI, ParentState);
592 }
593 if (auto *InnerCleanupPad = dyn_cast<CleanupPadInst>(UserI)) {
594 BasicBlock *UnwindDest = getCleanupRetUnwindDest(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, UserI, ParentState);
600 }
601 }
602 } else {
603 auto *CleanupPad = cast<CleanupPadInst>(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(CleanupPad);
608 if (!Inserted)
609 return;
610
611 int CleanupState = addSEHFinally(FuncInfo, ParentState, 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(PredBlock, CleanupPad->getParentPad())))
618 calculateSEHStateNumbers(FuncInfo, &*PredBlock->getFirstNonPHIIt(),
619 CleanupState);
620 for (const User *U : CleanupPad->users()) {
621 const auto *UserI = cast<Instruction>(U);
622 if (UserI->isEHPad())
623 report_fatal_error("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>(EHPad))
631 return isa<ConstantTokenNone>(CatchSwitch->getParentPad()) &&
632 CatchSwitch->unwindsToCaller();
633 if (auto *CleanupPad = dyn_cast<CleanupPadInst>(EHPad))
634 return isa<ConstantTokenNone>(CleanupPad->getParentPad()) &&
635 getCleanupRetUnwindDest(CleanupPad) == nullptr;
636 if (isa<CatchPadInst>(EHPad))
637 return false;
638 llvm_unreachable("unexpected EHPad!");
639}
640
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(FirstNonPHI))
652 continue;
653 ::calculateSEHStateNumbers(FuncInfo, FirstNonPHI, -1);
654 }
655
657
658 bool IsEHa = Fn->getParent()->getModuleFlag("eh-asynch");
659 if (IsEHa) {
660 const BasicBlock *EntryBB = &(Fn->getEntryBlock());
661 calculateSEHStateForAsynchEH(EntryBB, -1, FuncInfo);
662 }
663}
664
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(FirstNonPHI))
676 continue;
677 calculateCXXStateNumbers(FuncInfo, FirstNonPHI, -1);
678 }
679
681
682 bool IsEHa = Fn->getParent()->getModuleFlag("eh-asynch");
683 if (IsEHa) {
684 const BasicBlock *EntryBB = &(Fn->getEntryBlock());
685 calculateCXXStateForAsynchEH(EntryBB, -1, FuncInfo);
686 }
687}
688
689static int addClrEHHandler(WinEHFuncInfo &FuncInfo, int HandlerParentState,
690 int TryParentState, ClrHandlerType HandlerType,
691 uint32_t TypeToken, const BasicBlock *Handler) {
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(Entry);
699 return FuncInfo.ClrEHUnwindMap.size() - 1;
700}
701
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.
733 for (const BasicBlock &BB : *Fn) {
734 const Instruction *FirstNonPHI = &*BB.getFirstNonPHIIt();
735 const Value *ParentPad;
736 if (const auto *CPI = dyn_cast<CleanupPadInst>(FirstNonPHI))
737 ParentPad = CPI->getParentPad();
738 else if (const auto *CSI = dyn_cast<CatchSwitchInst>(FirstNonPHI))
739 ParentPad = CSI->getParentPad();
740 else
741 continue;
742 if (isa<ConstantTokenNone>(ParentPad))
743 Worklist.emplace_back(FirstNonPHI, -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(Pad, HandlerParentState) = Worklist.pop_back_val();
756
757 if (const auto *Cleanup = dyn_cast<CleanupPadInst>(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
763 int CleanupState = addClrEHHandler(FuncInfo, HandlerParentState, -1,
764 HandlerType, 0, 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>(U))
768 if (I->isEHPad())
769 Worklist.emplace_back(I, 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>(Pad);
776 int CatchState = -1, FollowerState = -1;
777 SmallVector<const BasicBlock *, 4> CatchBlocks(CatchSwitch->handlers());
778 for (const BasicBlock *CatchBlock : llvm::reverse(CatchBlocks)) {
779 // Create the entry for this catch with the appropriate handler
780 // properties.
781 const auto *Catch = cast<CatchPadInst>(CatchBlock->getFirstNonPHIIt());
783 "Malformed CatchPadInst not caught by win-eh-prepare");
784 uint32_t TypeToken = static_cast<uint32_t>(
785 cast<ConstantInt>(Catch->getArgOperand(0))->getZExtValue());
786 CatchState =
787 addClrEHHandler(FuncInfo, HandlerParentState, FollowerState,
788 ClrHandlerType::Catch, TypeToken, 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>(U))
792 if (I->isEHPad())
793 Worklist.emplace_back(I, 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(FuncInfo.ClrEHUnwindMap)) {
808 const Instruction *Pad =
809 &*cast<const BasicBlock *>(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>(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>(Pad);
825 UnwindDest = nullptr;
826 for (const User *U : Cleanup->users()) {
827 if (auto *CleanupRet = dyn_cast<CleanupReturnInst>(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>(U)) {
837 UserUnwindDest = Invoke->getUnwindDest();
838 } else if (auto *CatchSwitch = dyn_cast<CatchSwitchInst>(U)) {
839 UserUnwindDest = CatchSwitch->getUnwindDest();
840 } else if (auto *ChildCleanup = dyn_cast<CleanupPadInst>(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 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>(UserUnwindPad))
861 UserUnwindParent = CSI->getParentPad();
862 else
863 UserUnwindParent =
864 cast<CleanupPadInst>(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.
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(&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.
921 for (BasicBlock &BB : make_early_inc_range(F)) {
922 if (!BB.isEHPad())
923 continue;
924
925 for (Instruction &I : make_early_inc_range(BB)) {
926 auto *PN = dyn_cast<PHINode>(&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>(BB.getFirstNonPHIIt());
936 bool HasIncomingCatchSwitchBB = false;
937 for (unsigned I = 0, E = PN->getNumIncomingValues(); I < E; ++I) {
939 PN->getIncomingBlock(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(PN, SpillSlot);
953
954 PHINodes.push_back(PN);
955 }
956 }
957
958 for (auto *PN : PHINodes) {
959 // There may be lingering uses on other EH PHIs being removed
960 PN->replaceAllUsesWith(PoisonValue::get(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>(FuncletToken);
971
972 if (!CatchPad)
973 return false;
974
975 if (!isMalformedCatchpad(CatchPad, Personality))
976 return false;
977
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>(Handler->getFirstNonPHIIt())) {
986 LLVMContext &CTX = CPI->getContext();
987 IRBuilder<> Builder(CPI);
988
989 // default values for the CatchPad's args
992 Value *constantZero = ConstantInt::get(Type::getInt32Ty(CTX), 0);
993 switch (Personality) {
995 args = {nullPtr, constantZero, nullPtr};
996 break;
1001 args = {nullPtr};
1002 break;
1004 args = {constantZero};
1005 break;
1006 default:
1007 llvm_unreachable("Unsupported Personality for WinEH");
1008 };
1009 Value *NewCatchPad =
1010 Builder.CreateCatchPad(CPI->getParentPad(), args, CPI->getName());
1011 CPI->replaceAllUsesWith(NewCatchPad);
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(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, 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(&F, 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(BB, 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(NewBlock);
1077 ColorVector &NewColors = BlockColors[NewBlock];
1078 assert(NewColors.empty() && "A new block should only have one color!");
1079 NewColors.push_back(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(BlocksInFunclet, OldBlock);
1087 ColorVector &OldColors = BlockColors[OldBlock];
1088 llvm::erase(OldColors, 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, VMap,
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(OldBlock))
1113 if (auto *CatchRet = dyn_cast<CatchReturnInst>(Pred->getTerminator()))
1114 if (CatchRet->getCatchSwitchParentPad() == FuncletToken)
1115 FixupCatchrets.push_back(CatchRet);
1116
1117 for (CatchReturnInst *CatchRet : FixupCatchrets)
1118 CatchRet->setSuccessor(NewBlock);
1119 }
1120
1121 auto UpdatePHIOnClonedBlock = [&](PHINode *PN, bool IsForOldBlock) {
1123 [&](unsigned Idx) {
1124 BasicBlock *IncomingBlock = PN->getIncomingBlock(Idx);
1125 bool EdgeTargetsFunclet;
1126 if (auto *CRI =
1127 dyn_cast<CatchReturnInst>(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(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(OldBlock);
1165 if (OldBlockIdx == -1)
1166 break;
1167 Value *IV = SuccPN.getIncomingValue(OldBlockIdx);
1168
1169 // Remap the value if necessary.
1170 if (auto *Inst = dyn_cast<Instruction>(IV)) {
1171 ValueToValueMapTy::iterator I = VMap.find(Inst);
1172 if (I != VMap.end())
1173 IV = I->second;
1174 }
1175
1176 SuccPN.addIncoming(IV, 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>(const_cast<Value *>(VT.first));
1190 if (!OldI)
1191 continue;
1192 auto *NewI = cast<Instruction>(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>(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(&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(OldI->getType(), OldI->getName());
1215 SSAUpdate.AddAvailableValue(OldI->getParent(), OldI);
1216 SSAUpdate.AddAvailableValue(NewI->getParent(), NewI);
1217
1218 while (!UsesToRename.empty())
1219 SSAUpdate.RewriteUseAfterInsertions(*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>(FirstNonPHI);
1235 auto *CatchPad = dyn_cast_or_null<CatchPadInst>(FuncletPad);
1236 auto *CleanupPad = dyn_cast_or_null<CleanupPadInst>(FuncletPad);
1237
1238 for (BasicBlock *BB : BlocksInFunclet) {
1239 for (Instruction &I : *BB) {
1240 auto *CB = dyn_cast<CallBase>(&I);
1241 if (!CB)
1242 continue;
1243
1244 Value *FuncletBundleOperand = nullptr;
1245 if (auto BU = CB->getOperandBundle(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>(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>(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(BB->getTerminator()->getIterator());
1267 auto *CI = cast<CallInst>(&*CallI);
1269 } else {
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>(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>(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>(TI))
1288 IsUnreachableCleanupret = CRI->getCleanupPad() != CleanupPad;
1289 if (IsUnreachableRet || IsUnreachableCatchret ||
1290 IsUnreachableCleanupret) {
1291 Changed = true;
1293 } else if (isa<InvokeInst>(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.
1315 Changed |= ConstantFoldTerminator(&BB, /*DeleteDeadConditions=*/true);
1317 }
1318
1319 // We might have some unreachable blocks after cleaning up some impossible
1320 // control flow.
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.
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.
1395 for (Use &U : llvm::make_early_inc_range(PN->uses())) {
1396 auto *UsingInst = cast<Instruction>(U.getUser());
1397 if (isa<PHINode>(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(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.
1416
1417 Worklist.push_back({OriginalPHI->getParent(), OriginalPHI});
1418
1419 while (!Worklist.empty()) {
1420 BasicBlock *EHBlock;
1421 Value *InVal;
1422 std::tie(EHBlock, InVal) = Worklist.pop_back_val();
1423
1424 PHINode *PN = dyn_cast<PHINode>(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>(PredVal))
1434 continue;
1435
1436 insertPHIStore(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(EHBlock)) {
1442 insertPHIStore(PredBlock, 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({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,
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>(U.getUser());
1472 if (auto *UsingPHI = dyn_cast<PHINode>(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>(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(IncomingBlock, 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>(IncomingBlock->getTerminator());
1505 Goto->removeFromParent();
1506 CatchRet->removeFromParent();
1507 CatchRet->insertInto(IncomingBlock, IncomingBlock->end());
1508 Goto->insertInto(NewBlock, 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(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
1541 MCSymbol *InvokeBegin,
1542 MCSymbol *InvokeEnd) {
1543 assert(InvokeStateMap.count(II) &&
1544 "should get invoke with precomputed state");
1545 LabelToStateMap[InvokeBegin] = std::make_pair(InvokeStateMap[II], InvokeEnd);
1546}
1547
1548void WinEHFuncInfo::addIPToStateRange(int State, MCSymbol* InvokeBegin,
1549 MCSymbol* InvokeEnd) {
1550 LabelToStateMap[InvokeBegin] = std::make_pair(State, InvokeEnd);
1551}
1552
SDValue Pred
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
This file defines the DenseMap class.
static bool runOnFunction(Function &F, bool PostInlining)
#define DEBUG_TYPE
ManagedStatic< HTTPClientCleanup > Cleanup
Module.h This file contains the declarations for the Module class.
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
This file implements a map that provides insertion order iteration.
nvptx lower args
uint64_t IntrinsicInst * II
if(auto Err=PB.parsePassPipeline(MPM, Passes)) return wrap(std MPM run * Mod
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
This file contains some templates that are useful if you are working with the STL at all.
#define LLVM_DEBUG(...)
Definition Debug.h:119
#define DEBUG_WITH_TYPE(TYPE,...)
DEBUG_WITH_TYPE macro - This macro should be used by passes to emit debug information.
Definition Debug.h:72
static bool isMalformedCatchpad(const CatchPadInst *CPI, EHPersonality Personality)
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 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 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:83
an instruction to allocate memory on the stack
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
LLVM Basic Block Representation.
Definition BasicBlock.h:62
iterator end()
Definition BasicBlock.h:453
iterator_range< const_phi_iterator > phis() const
Returns a range that iterates over the phis in the basic block.
Definition BasicBlock.h:509
LLVM_ABI const_iterator getFirstInsertionPt() const
Returns an iterator to the first instruction in this block that is suitable for inserting a non-PHI i...
const Function * getParent() const
Return the enclosing method, or null if none.
Definition BasicBlock.h:213
LLVM_ABI InstListType::const_iterator getFirstNonPHIIt() const
Returns an iterator to the first instruction in this block that is not a PHINode instruction.
InstListType::const_iterator const_iterator
Definition BasicBlock.h:171
const Instruction & front() const
Definition BasicBlock.h:463
LLVM_ABI void insertInto(Function *Parent, BasicBlock *InsertBefore=nullptr)
Insert unlinked basic block into a function.
InstListType::iterator iterator
Instruction iterators...
Definition BasicBlock.h:170
bool isEHPad() const
Return true if this basic block is an exception handling block.
Definition BasicBlock.h:683
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction; assumes that the block is well-formed.
Definition BasicBlock.h:237
CatchSwitchInst * getCatchSwitch() const
Convenience accessors.
handler_range handlers()
iteration adapter for range-for loops.
static LLVM_ABI ConstantPointerNull * get(PointerType *T)
Static factory methods - Return objects of the specified value.
static LLVM_ABI ConstantTokenNone * get(LLVMContext &Context)
Return the ConstantTokenNone.
This is an important base class in LLVM.
Definition Constant.h:44
bool isNullValue() const
Return true if this is the value that would be returned by getNullValue.
Definition Constant.h:65
const Constant * stripPointerCasts() const
Definition Constant.h:243
unsigned arg_size() const
arg_size - Return the number of funcletpad arguments.
Value * getArgOperand(unsigned i) const
getArgOperand/setArgOperand - Return/set the i-th funcletpad argument.
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:312
const BasicBlock & getEntryBlock() const
Definition Function.h:803
const DataLayout & getDataLayout() const
Get the data layout of the module this function belongs to.
Definition Function.cpp:356
Intrinsic::ID getIntrinsicID() const LLVM_READONLY
getIntrinsicID - This method returns the ID number of the specified function, or Intrinsic::not_intri...
Definition Function.h:247
bool hasPersonalityFn() const
Check whether this function has a personality function.
Definition Function.h:899
Constant * getPersonalityFn() const
Get the personality function associated with this function.
bool isIntrinsic() const
isIntrinsic - Returns true if the function's name starts with "llvm.".
Definition Function.h:252
Module * getParent()
Get the module that this global value is contained inside of...
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition IRBuilder.h:2990
LLVM_ABI void removeFromParent()
This method unlinks 'this' from the containing basic block, but does not delete it.
const DebugLoc & getDebugLoc() const
Return the debug location for this node as a DebugLoc.
bool isEHPad() const
Return true if the instruction is a variety of EH-block.
bool isTerminator() const
iterator_range< user_iterator > users()
LLVM_ABI 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.
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:76
LLVM_ABI void diagnose(const DiagnosticInfo &DI)
Report a message to the currently installed diagnostic handler.
An instruction for reading from memory.
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition MCSymbol.h:42
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:68
Metadata * getModuleFlag(StringRef Key) const
Return the corresponding value if Key appears in module flags, otherwise return null.
Definition Module.cpp:377
LLVM_ABI void removeIncomingValueIf(function_ref< bool(unsigned)> Predicate, bool DeletePHIIfEmpty=true)
Remove all incoming values for which the predicate returns true.
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.
static PointerType * getUnqual(LLVMContext &C)
This constructs an opaque pointer to an object in the default address space (address space zero).
static LLVM_ABI PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
Definition Analysis.h:115
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
Helper class for SSA formation on a set of values defined in multiple blocks.
Definition SSAUpdater.h:39
LLVM_ABI void RewriteUseAfterInsertions(Use &U)
Rewrite a use like RewriteUse but handling in-block definitions.
LLVM_ABI void Initialize(Type *Ty, StringRef Name)
Reset this object to get ready for a new set of SSA updates with type 'Ty'.
LLVM_ABI void AddAvailableValue(BasicBlock *BB, Value *V)
Indicate that a rewritten value is available in the specified block with the specified value.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
reference emplace_back(ArgTypes &&... Args)
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
An instruction for storing to memory.
void push_back(EltTy NewVal)
EltTy front() const
unsigned size() const
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
static LLVM_ABI IntegerType * getInt32Ty(LLVMContext &C)
Definition Type.cpp:294
Unconditional Branch instruction.
void setSuccessor(BasicBlock *NewSucc)
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:101
iterator find(const KeyT &Val)
Definition ValueMap.h:160
iterator end()
Definition ValueMap.h:139
ValueMapIteratorImpl< MapT, const Value *, false > iterator
Definition ValueMap.h:135
LLVM Value Representation.
Definition Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:257
LLVM_ABI void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition Value.cpp:548
LLVMContext & getContext() const
All values hold a context through their type.
Definition Value.h:260
iterator_range< user_iterator > users()
Definition Value.h:428
iterator_range< use_iterator > uses()
Definition Value.h:382
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
Definition Value.cpp:314
LLVM_ABI PreservedAnalyses run(Function &F, FunctionAnalysisManager &FAM)
const ParentTy * getParent() const
Definition ilist_node.h:34
self_iterator getIterator()
Definition ilist_node.h:123
CallInst * Call
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI FunctionPass * createWinEHPass()
createWinEHPass - Prepares personality functions used by MSVC on Windows, in addition to the Itanium ...
LLVM_ABI 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:133
LLVM_ABI BasicBlock * CloneBasicBlock(const BasicBlock *BB, ValueToValueMapTy &VMap, const Twine &NameSuffix="", Function *F=nullptr, ClonedCodeInfo *CodeInfo=nullptr, bool MapAtoms=true)
Return a copy of the specified basic block, but without embedding the block into a particular functio...
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
LLVM_ABI bool verifyFunction(const Function &F, raw_ostream *OS=nullptr)
Check a function for errors, useful for use when debugging a pass.
auto successors(const MachineBasicBlock *BB)
@ Load
The value being inserted comes from a load (InsertElement only).
LLVM_ABI 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:649
LLVM_ABI 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:715
LLVM_ABI 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,...
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Value
Definition InstrProf.h:143
LLVM_ABI bool removeUnreachableBlocks(Function &F, DomTreeUpdater *DTU=nullptr, MemorySSAUpdater *MSSAU=nullptr, bool FoldInstsToUnreachable=true)
Remove all blocks that can not be reached from the function's entry.
Definition Local.cpp:2961
auto dyn_cast_or_null(const Y &Val)
Definition Casting.h:753
void erase(Container &C, ValueType V)
Wrapper function to remove a value from a container:
Definition STLExtras.h:2220
auto reverse(ContainerTy &&C)
Definition STLExtras.h:408
@ RF_IgnoreMissingLocals
If this flag is set, the remapper ignores missing function-local entries (Argument,...
Definition ValueMapper.h:98
@ RF_NoModuleLevelChanges
If this flag is set, the remapper knows that only local values within a function (such as an instruct...
Definition ValueMapper.h:80
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:163
LLVM_ABI Instruction * removeUnwindEdge(BasicBlock *BB, DomTreeUpdater *DTU=nullptr)
Replace 'BB's terminator with one that does not have an unwind successor block.
Definition Local.cpp:2923
LLVM_ABI void calculateSEHStateForAsynchEH(const BasicBlock *BB, int State, WinEHFuncInfo &FuncInfo)
LLVM_ABI EHPersonality classifyEHPersonality(const Value *Pers)
See if the given exception handling personality function is one that we understand.
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
LLVM_ABI 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:2592
LLVM_ABI void calculateCXXStateForAsynchEH(const BasicBlock *BB, int State, WinEHFuncInfo &FuncInfo)
bool isFuncletEHPersonality(EHPersonality Pers)
Returns true if this is a personality function that invokes handler funclets (which must return to it...
LLVM_ABI void calculateSEHStateNumbers(const Function *ParentFn, WinEHFuncInfo &FuncInfo)
LLVM_ABI 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.
void RemapInstruction(Instruction *I, ValueToValueMapTy &VM, RemapFlags Flags=RF_None, ValueMapTypeRemapper *TypeMapper=nullptr, ValueMaterializer *Materializer=nullptr, const MetadataPredicate *IdentityMD=nullptr)
Convert the instruction operands from referencing the current values into those specified by VM.
ValueMap< const Value *, WeakTrackingVH > ValueToValueMapTy
TinyPtrVector< BasicBlock * > ColorVector
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
auto predecessors(const MachineBasicBlock *BB)
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition STLExtras.h:1963
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
LLVM_ABI 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...
const CodeGenOptions & getCodeGenOptions(const LLVMContext &C)
LLVM_ABI void calculateClrEHStateNumbers(const Function *Fn, WinEHFuncInfo &FuncInfo)
const BasicBlock * Block
WorkItem(const BasicBlock *BB, int St)
int HandlerParentState
Outer handler enclosing this entry's handler.
MBBOrBasicBlock Cleanup
Similar to CxxUnwindMapEntry, but supports SEH filters.
int ToState
If unwinding continues through this handler, transition to the handler at this state.
LLVM_ABI void addIPToStateRange(const InvokeInst *II, MCSymbol *InvokeBegin, MCSymbol *InvokeEnd)
SmallVector< SEHUnwindMapEntry, 4 > SEHUnwindMap
SmallVector< ClrEHUnwindMapEntry, 4 > ClrEHUnwindMap
DenseMap< const FuncletPadInst *, int > FuncletBaseStateMap
DenseMap< const BasicBlock *, int > BlockToStateMap
DenseMap< const InvokeInst *, int > InvokeStateMap
SmallVector< WinEHTryBlockMapEntry, 4 > TryBlockMap
DenseMap< const Instruction *, int > EHPadStateMap
LLVM_ABI WinEHFuncInfo()
DenseMap< MCSymbol *, std::pair< int, MCSymbol * > > LabelToStateMap
SmallVector< CxxUnwindMapEntry, 4 > CxxUnwindMap
int getLastStateNumber() const
GlobalVariable * TypeDescriptor
union llvm::WinEHHandlerType::@246205307012256373115155017221207221353102114334 CatchObj
The CatchObj starts out life as an LLVM alloca and is eventually turned frame index.
const AllocaInst * Alloca
MBBOrBasicBlock Handler
SmallVector< WinEHHandlerType, 1 > HandlerArray