WinEHFuncInfo.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. //===-- llvm/CodeGen/WinEHFuncInfo.h ----------------------------*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // Data structures and associated state for Windows exception handling schemes.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_CODEGEN_WINEHFUNCINFO_H
  14. #define LLVM_CODEGEN_WINEHFUNCINFO_H
  15. #include "llvm/ADT/SmallVector.h"
  16. #include "llvm/ADT/TinyPtrVector.h"
  17. #include "llvm/ADT/DenseMap.h"
  18. namespace llvm {
  19. class BasicBlock;
  20. class Constant;
  21. class Function;
  22. class GlobalVariable;
  23. class InvokeInst;
  24. class IntrinsicInst;
  25. class LandingPadInst;
  26. class MCSymbol;
  27. class Value;
  28. enum ActionType { Catch, Cleanup };
  29. class ActionHandler {
  30. public:
  31. ActionHandler(BasicBlock *BB, ActionType Type)
  32. : StartBB(BB), Type(Type), EHState(-1), HandlerBlockOrFunc(nullptr) {}
  33. ActionType getType() const { return Type; }
  34. BasicBlock *getStartBlock() const { return StartBB; }
  35. bool hasBeenProcessed() { return HandlerBlockOrFunc != nullptr; }
  36. void setHandlerBlockOrFunc(Constant *F) { HandlerBlockOrFunc = F; }
  37. Constant *getHandlerBlockOrFunc() { return HandlerBlockOrFunc; }
  38. void setEHState(int State) { EHState = State; }
  39. int getEHState() const { return EHState; }
  40. private:
  41. BasicBlock *StartBB;
  42. ActionType Type;
  43. int EHState;
  44. // Can be either a BlockAddress or a Function depending on the EH personality.
  45. Constant *HandlerBlockOrFunc;
  46. };
  47. class CatchHandler : public ActionHandler {
  48. public:
  49. CatchHandler(BasicBlock *BB, Constant *Selector, BasicBlock *NextBB)
  50. : ActionHandler(BB, ActionType::Catch), Selector(Selector),
  51. NextBB(NextBB), ExceptionObjectVar(nullptr),
  52. ExceptionObjectIndex(-1) {}
  53. // Method for support type inquiry through isa, cast, and dyn_cast:
  54. static inline bool classof(const ActionHandler *H) {
  55. return H->getType() == ActionType::Catch;
  56. }
  57. Constant *getSelector() const { return Selector; }
  58. BasicBlock *getNextBB() const { return NextBB; }
  59. const Value *getExceptionVar() { return ExceptionObjectVar; }
  60. TinyPtrVector<BasicBlock *> &getReturnTargets() { return ReturnTargets; }
  61. void setExceptionVar(const Value *Val) { ExceptionObjectVar = Val; }
  62. void setExceptionVarIndex(int Index) { ExceptionObjectIndex = Index; }
  63. int getExceptionVarIndex() const { return ExceptionObjectIndex; }
  64. void setReturnTargets(TinyPtrVector<BasicBlock *> &Targets) {
  65. ReturnTargets = Targets;
  66. }
  67. private:
  68. Constant *Selector;
  69. BasicBlock *NextBB;
  70. // While catch handlers are being outlined the ExceptionObjectVar field will
  71. // be populated with the instruction in the parent frame that corresponds
  72. // to the exception object (or nullptr if the catch does not use an
  73. // exception object) and the ExceptionObjectIndex field will be -1.
  74. // When the parseEHActions function is called to populate a vector of
  75. // instances of this class, the ExceptionObjectVar field will be nullptr
  76. // and the ExceptionObjectIndex will be the index of the exception object in
  77. // the parent function's localescape block.
  78. const Value *ExceptionObjectVar;
  79. int ExceptionObjectIndex;
  80. TinyPtrVector<BasicBlock *> ReturnTargets;
  81. };
  82. class CleanupHandler : public ActionHandler {
  83. public:
  84. CleanupHandler(BasicBlock *BB) : ActionHandler(BB, ActionType::Cleanup) {}
  85. // Method for support type inquiry through isa, cast, and dyn_cast:
  86. static inline bool classof(const ActionHandler *H) {
  87. return H->getType() == ActionType::Cleanup;
  88. }
  89. };
  90. void parseEHActions(const IntrinsicInst *II,
  91. SmallVectorImpl<std::unique_ptr<ActionHandler>> &Actions);
  92. // The following structs respresent the .xdata for functions using C++
  93. // exceptions on Windows.
  94. struct WinEHUnwindMapEntry {
  95. int ToState;
  96. Function *Cleanup;
  97. };
  98. struct WinEHHandlerType {
  99. int Adjectives;
  100. GlobalVariable *TypeDescriptor;
  101. int CatchObjRecoverIdx;
  102. Function *Handler;
  103. };
  104. struct WinEHTryBlockMapEntry {
  105. int TryLow;
  106. int TryHigh;
  107. SmallVector<WinEHHandlerType, 1> HandlerArray;
  108. };
  109. struct WinEHFuncInfo {
  110. DenseMap<const Function *, const LandingPadInst *> RootLPad;
  111. DenseMap<const Function *, const InvokeInst *> LastInvoke;
  112. DenseMap<const Function *, int> HandlerEnclosedState;
  113. DenseMap<const Function *, bool> LastInvokeVisited;
  114. DenseMap<const LandingPadInst *, int> LandingPadStateMap;
  115. DenseMap<const Function *, int> CatchHandlerParentFrameObjIdx;
  116. DenseMap<const Function *, int> CatchHandlerParentFrameObjOffset;
  117. DenseMap<const Function *, int> CatchHandlerMaxState;
  118. DenseMap<const Function *, int> HandlerBaseState;
  119. SmallVector<WinEHUnwindMapEntry, 4> UnwindMap;
  120. SmallVector<WinEHTryBlockMapEntry, 4> TryBlockMap;
  121. SmallVector<std::pair<MCSymbol *, int>, 4> IPToStateList;
  122. int UnwindHelpFrameIdx = INT_MAX;
  123. int UnwindHelpFrameOffset = -1;
  124. unsigned NumIPToStateFuncsVisited = 0;
  125. /// localescape index of the 32-bit EH registration node. Set by
  126. /// WinEHStatePass and used indirectly by SEH filter functions of the parent.
  127. int EHRegNodeEscapeIndex = INT_MAX;
  128. WinEHFuncInfo() {}
  129. };
  130. /// Analyze the IR in ParentFn and it's handlers to build WinEHFuncInfo, which
  131. /// describes the state numbers and tables used by __CxxFrameHandler3. This
  132. /// analysis assumes that WinEHPrepare has already been run.
  133. void calculateWinCXXEHStateNumbers(const Function *ParentFn,
  134. WinEHFuncInfo &FuncInfo);
  135. }
  136. #endif // LLVM_CODEGEN_WINEHFUNCINFO_H