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