1 //===-- llvm/CodeGen/WinEHFuncInfo.h ----------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Data structures and associated state for Windows exception handling schemes.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CODEGEN_WINEHFUNCINFO_H
15 #define LLVM_CODEGEN_WINEHFUNCINFO_H
17 #include "llvm/ADT/DenseMap.h"
18 #include "llvm/ADT/PointerUnion.h"
19 #include "llvm/ADT/SmallVector.h"
20 #include "llvm/ADT/TinyPtrVector.h"
32 class MachineBasicBlock;
35 enum ActionType { Catch, Cleanup };
39 ActionHandler(BasicBlock *BB, ActionType Type)
40 : StartBB(BB), Type(Type), EHState(-1), HandlerBlockOrFunc(nullptr) {}
42 ActionType getType() const { return Type; }
43 BasicBlock *getStartBlock() const { return StartBB; }
45 bool hasBeenProcessed() { return HandlerBlockOrFunc != nullptr; }
47 void setHandlerBlockOrFunc(Constant *F) { HandlerBlockOrFunc = F; }
48 Constant *getHandlerBlockOrFunc() { return HandlerBlockOrFunc; }
50 void setEHState(int State) { EHState = State; }
51 int getEHState() const { return EHState; }
58 // Can be either a BlockAddress or a Function depending on the EH personality.
59 Constant *HandlerBlockOrFunc;
62 class CatchHandler : public ActionHandler {
64 CatchHandler(BasicBlock *BB, Constant *Selector, BasicBlock *NextBB)
65 : ActionHandler(BB, ActionType::Catch), Selector(Selector),
66 NextBB(NextBB), ExceptionObjectVar(nullptr),
67 ExceptionObjectIndex(-1) {}
69 // Method for support type inquiry through isa, cast, and dyn_cast:
70 static inline bool classof(const ActionHandler *H) {
71 return H->getType() == ActionType::Catch;
74 Constant *getSelector() const { return Selector; }
75 BasicBlock *getNextBB() const { return NextBB; }
77 const Value *getExceptionVar() { return ExceptionObjectVar; }
78 TinyPtrVector<BasicBlock *> &getReturnTargets() { return ReturnTargets; }
80 void setExceptionVar(const Value *Val) { ExceptionObjectVar = Val; }
81 void setExceptionVarIndex(int Index) { ExceptionObjectIndex = Index; }
82 int getExceptionVarIndex() const { return ExceptionObjectIndex; }
83 void setReturnTargets(TinyPtrVector<BasicBlock *> &Targets) {
84 ReturnTargets = Targets;
90 // While catch handlers are being outlined the ExceptionObjectVar field will
91 // be populated with the instruction in the parent frame that corresponds
92 // to the exception object (or nullptr if the catch does not use an
93 // exception object) and the ExceptionObjectIndex field will be -1.
94 // When the parseEHActions function is called to populate a vector of
95 // instances of this class, the ExceptionObjectVar field will be nullptr
96 // and the ExceptionObjectIndex will be the index of the exception object in
97 // the parent function's localescape block.
98 const Value *ExceptionObjectVar;
99 int ExceptionObjectIndex;
100 TinyPtrVector<BasicBlock *> ReturnTargets;
103 class CleanupHandler : public ActionHandler {
105 CleanupHandler(BasicBlock *BB) : ActionHandler(BB, ActionType::Cleanup) {}
107 // Method for support type inquiry through isa, cast, and dyn_cast:
108 static inline bool classof(const ActionHandler *H) {
109 return H->getType() == ActionType::Cleanup;
113 void parseEHActions(const IntrinsicInst *II,
114 SmallVectorImpl<std::unique_ptr<ActionHandler>> &Actions);
116 // The following structs respresent the .xdata for functions using C++
117 // exceptions on Windows.
119 struct WinEHUnwindMapEntry {
121 const Value *Cleanup;
124 typedef PointerUnion<const BasicBlock *, MachineBasicBlock *> MBBOrBasicBlock;
125 typedef PointerUnion<const Value *, MachineBasicBlock *> ValueOrMBB;
127 /// Similar to WinEHUnwindMapEntry, but supports SEH filters.
128 struct SEHUnwindMapEntry {
129 /// If unwinding continues through this handler, transition to the handler at
130 /// this state. This indexes into SEHUnwindMap.
133 /// Holds the filter expression function.
134 const Function *Filter = nullptr;
136 /// Holds the __except or __finally basic block.
137 MBBOrBasicBlock Handler;
140 struct WinEHHandlerType {
142 GlobalVariable *TypeDescriptor;
143 int CatchObjRecoverIdx;
147 struct WinEHTryBlockMapEntry {
151 SmallVector<WinEHHandlerType, 1> HandlerArray;
154 struct WinEHFuncInfo {
155 DenseMap<const Function *, const LandingPadInst *> RootLPad;
156 DenseMap<const Function *, const InvokeInst *> LastInvoke;
157 DenseMap<const Function *, int> HandlerEnclosedState;
158 DenseMap<const Function *, bool> LastInvokeVisited;
159 DenseMap<const Instruction *, int> EHPadStateMap;
160 DenseMap<const Function *, int> CatchHandlerParentFrameObjIdx;
161 DenseMap<const Function *, int> CatchHandlerParentFrameObjOffset;
162 DenseMap<const Function *, int> CatchHandlerMaxState;
163 DenseMap<const Function *, int> HandlerBaseState;
164 SmallVector<WinEHUnwindMapEntry, 4> UnwindMap;
165 SmallVector<WinEHTryBlockMapEntry, 4> TryBlockMap;
166 SmallVector<SEHUnwindMapEntry, 4> SEHUnwindMap;
167 SmallVector<std::pair<MCSymbol *, int>, 4> IPToStateList;
168 int UnwindHelpFrameIdx = INT_MAX;
169 int UnwindHelpFrameOffset = -1;
170 unsigned NumIPToStateFuncsVisited = 0;
172 int getLastStateNumber() const { return UnwindMap.size() - 1; }
174 /// localescape index of the 32-bit EH registration node. Set by
175 /// WinEHStatePass and used indirectly by SEH filter functions of the parent.
176 int EHRegNodeEscapeIndex = INT_MAX;
177 const AllocaInst *EHRegNode = nullptr;
178 int EHRegNodeFrameIndex = INT_MAX;
183 /// Analyze the IR in ParentFn and it's handlers to build WinEHFuncInfo, which
184 /// describes the state numbers and tables used by __CxxFrameHandler3. This
185 /// analysis assumes that WinEHPrepare has already been run.
186 void calculateWinCXXEHStateNumbers(const Function *ParentFn,
187 WinEHFuncInfo &FuncInfo);
189 void calculateSEHStateNumbers(const Function *ParentFn,
190 WinEHFuncInfo &FuncInfo);
192 #endif // LLVM_CODEGEN_WINEHFUNCINFO_H