WinEHFuncInfo.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. //===- llvm/CodeGen/WinEHFuncInfo.h -----------------------------*- C++ -*-===//
  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. // Data structures and associated state for Windows exception handling schemes.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #ifndef LLVM_CODEGEN_WINEHFUNCINFO_H
  13. #define LLVM_CODEGEN_WINEHFUNCINFO_H
  14. #include "llvm/ADT/DenseMap.h"
  15. #include "llvm/ADT/PointerUnion.h"
  16. #include "llvm/ADT/SmallVector.h"
  17. #include <cstdint>
  18. #include <limits>
  19. #include <utility>
  20. namespace llvm {
  21. class AllocaInst;
  22. class BasicBlock;
  23. class FuncletPadInst;
  24. class Function;
  25. class GlobalVariable;
  26. class Instruction;
  27. class InvokeInst;
  28. class MachineBasicBlock;
  29. class MCSymbol;
  30. // The following structs respresent the .xdata tables for various
  31. // Windows-related EH personalities.
  32. using MBBOrBasicBlock = PointerUnion<const BasicBlock *, MachineBasicBlock *>;
  33. struct CxxUnwindMapEntry {
  34. int ToState;
  35. MBBOrBasicBlock Cleanup;
  36. };
  37. /// Similar to CxxUnwindMapEntry, but supports SEH filters.
  38. struct SEHUnwindMapEntry {
  39. /// If unwinding continues through this handler, transition to the handler at
  40. /// this state. This indexes into SEHUnwindMap.
  41. int ToState = -1;
  42. bool IsFinally = false;
  43. /// Holds the filter expression function.
  44. const Function *Filter = nullptr;
  45. /// Holds the __except or __finally basic block.
  46. MBBOrBasicBlock Handler;
  47. };
  48. struct WinEHHandlerType {
  49. int Adjectives;
  50. /// The CatchObj starts out life as an LLVM alloca and is eventually turned
  51. /// frame index.
  52. union {
  53. const AllocaInst *Alloca;
  54. int FrameIndex;
  55. } CatchObj = {};
  56. GlobalVariable *TypeDescriptor;
  57. MBBOrBasicBlock Handler;
  58. };
  59. struct WinEHTryBlockMapEntry {
  60. int TryLow = -1;
  61. int TryHigh = -1;
  62. int CatchHigh = -1;
  63. SmallVector<WinEHHandlerType, 1> HandlerArray;
  64. };
  65. enum class ClrHandlerType { Catch, Finally, Fault, Filter };
  66. struct ClrEHUnwindMapEntry {
  67. MBBOrBasicBlock Handler;
  68. uint32_t TypeToken;
  69. int HandlerParentState; ///< Outer handler enclosing this entry's handler
  70. int TryParentState; ///< Outer try region enclosing this entry's try region,
  71. ///< treating later catches on same try as "outer"
  72. ClrHandlerType HandlerType;
  73. };
  74. struct WinEHFuncInfo {
  75. DenseMap<const Instruction *, int> EHPadStateMap;
  76. DenseMap<const FuncletPadInst *, int> FuncletBaseStateMap;
  77. DenseMap<const InvokeInst *, int> InvokeStateMap;
  78. DenseMap<MCSymbol *, std::pair<int, MCSymbol *>> LabelToStateMap;
  79. SmallVector<CxxUnwindMapEntry, 4> CxxUnwindMap;
  80. SmallVector<WinEHTryBlockMapEntry, 4> TryBlockMap;
  81. SmallVector<SEHUnwindMapEntry, 4> SEHUnwindMap;
  82. SmallVector<ClrEHUnwindMapEntry, 4> ClrEHUnwindMap;
  83. int UnwindHelpFrameIdx = std::numeric_limits<int>::max();
  84. int PSPSymFrameIdx = std::numeric_limits<int>::max();
  85. int getLastStateNumber() const { return CxxUnwindMap.size() - 1; }
  86. void addIPToStateRange(const InvokeInst *II, MCSymbol *InvokeBegin,
  87. MCSymbol *InvokeEnd);
  88. int EHRegNodeFrameIndex = std::numeric_limits<int>::max();
  89. int EHRegNodeEndOffset = std::numeric_limits<int>::max();
  90. int EHGuardFrameIndex = std::numeric_limits<int>::max();
  91. int SEHSetFrameOffset = std::numeric_limits<int>::max();
  92. WinEHFuncInfo();
  93. };
  94. /// Analyze the IR in ParentFn and it's handlers to build WinEHFuncInfo, which
  95. /// describes the state numbers and tables used by __CxxFrameHandler3. This
  96. /// analysis assumes that WinEHPrepare has already been run.
  97. void calculateWinCXXEHStateNumbers(const Function *ParentFn,
  98. WinEHFuncInfo &FuncInfo);
  99. void calculateSEHStateNumbers(const Function *ParentFn,
  100. WinEHFuncInfo &FuncInfo);
  101. void calculateClrEHStateNumbers(const Function *Fn, WinEHFuncInfo &FuncInfo);
  102. } // end namespace llvm
  103. #endif // LLVM_CODEGEN_WINEHFUNCINFO_H