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/SmallVector.h"
18 #include "llvm/ADT/TinyPtrVector.h"
19 #include "llvm/ADT/DenseMap.h"
30 class MachineBasicBlock;
33 enum ActionType { Catch, Cleanup };
37 ActionHandler(BasicBlock *BB, ActionType Type)
38 : StartBB(BB), Type(Type), EHState(-1), HandlerBlockOrFunc(nullptr) {}
40 ActionType getType() const { return Type; }
41 BasicBlock *getStartBlock() const { return StartBB; }
43 bool hasBeenProcessed() { return HandlerBlockOrFunc != nullptr; }
45 void setHandlerBlockOrFunc(Constant *F) { HandlerBlockOrFunc = F; }
46 Constant *getHandlerBlockOrFunc() { return HandlerBlockOrFunc; }
48 void setEHState(int State) { EHState = State; }
49 int getEHState() const { return EHState; }
56 // Can be either a BlockAddress or a Function depending on the EH personality.
57 Constant *HandlerBlockOrFunc;
60 class CatchHandler : public ActionHandler {
62 CatchHandler(BasicBlock *BB, Constant *Selector, BasicBlock *NextBB)
63 : ActionHandler(BB, ActionType::Catch), Selector(Selector),
64 NextBB(NextBB), ExceptionObjectVar(nullptr),
65 ExceptionObjectIndex(-1) {}
67 // Method for support type inquiry through isa, cast, and dyn_cast:
68 static inline bool classof(const ActionHandler *H) {
69 return H->getType() == ActionType::Catch;
72 Constant *getSelector() const { return Selector; }
73 BasicBlock *getNextBB() const { return NextBB; }
75 const Value *getExceptionVar() { return ExceptionObjectVar; }
76 TinyPtrVector<BasicBlock *> &getReturnTargets() { return ReturnTargets; }
78 void setExceptionVar(const Value *Val) { ExceptionObjectVar = Val; }
79 void setExceptionVarIndex(int Index) { ExceptionObjectIndex = Index; }
80 int getExceptionVarIndex() const { return ExceptionObjectIndex; }
81 void setReturnTargets(TinyPtrVector<BasicBlock *> &Targets) {
82 ReturnTargets = Targets;
88 // While catch handlers are being outlined the ExceptionObjectVar field will
89 // be populated with the instruction in the parent frame that corresponds
90 // to the exception object (or nullptr if the catch does not use an
91 // exception object) and the ExceptionObjectIndex field will be -1.
92 // When the parseEHActions function is called to populate a vector of
93 // instances of this class, the ExceptionObjectVar field will be nullptr
94 // and the ExceptionObjectIndex will be the index of the exception object in
95 // the parent function's localescape block.
96 const Value *ExceptionObjectVar;
97 int ExceptionObjectIndex;
98 TinyPtrVector<BasicBlock *> ReturnTargets;
101 class CleanupHandler : public ActionHandler {
103 CleanupHandler(BasicBlock *BB) : ActionHandler(BB, ActionType::Cleanup) {}
105 // Method for support type inquiry through isa, cast, and dyn_cast:
106 static inline bool classof(const ActionHandler *H) {
107 return H->getType() == ActionType::Cleanup;
111 void parseEHActions(const IntrinsicInst *II,
112 SmallVectorImpl<std::unique_ptr<ActionHandler>> &Actions);
114 // The following structs respresent the .xdata for functions using C++
115 // exceptions on Windows.
117 struct WinEHUnwindMapEntry {
119 const Value *Cleanup;
122 struct WinEHHandlerType {
124 GlobalVariable *TypeDescriptor;
125 int CatchObjRecoverIdx;
126 const Value *Handler;
127 MachineBasicBlock *HandlerMBB;
130 struct WinEHTryBlockMapEntry {
134 SmallVector<WinEHHandlerType, 1> HandlerArray;
137 struct WinEHFuncInfo {
138 DenseMap<const Function *, const LandingPadInst *> RootLPad;
139 DenseMap<const Function *, const InvokeInst *> LastInvoke;
140 DenseMap<const Function *, int> HandlerEnclosedState;
141 DenseMap<const Function *, bool> LastInvokeVisited;
142 DenseMap<const Instruction *, int> EHPadStateMap;
143 DenseMap<const Function *, int> CatchHandlerParentFrameObjIdx;
144 DenseMap<const Function *, int> CatchHandlerParentFrameObjOffset;
145 DenseMap<const Function *, int> CatchHandlerMaxState;
146 DenseMap<const Function *, int> HandlerBaseState;
147 SmallVector<WinEHUnwindMapEntry, 4> UnwindMap;
148 SmallVector<WinEHTryBlockMapEntry, 4> TryBlockMap;
149 SmallVector<std::pair<MCSymbol *, int>, 4> IPToStateList;
150 int UnwindHelpFrameIdx = INT_MAX;
151 int UnwindHelpFrameOffset = -1;
152 unsigned NumIPToStateFuncsVisited = 0;
154 int getLastStateNumber() const { return UnwindMap.size() - 1; }
156 /// localescape index of the 32-bit EH registration node. Set by
157 /// WinEHStatePass and used indirectly by SEH filter functions of the parent.
158 int EHRegNodeEscapeIndex = INT_MAX;
163 /// Analyze the IR in ParentFn and it's handlers to build WinEHFuncInfo, which
164 /// describes the state numbers and tables used by __CxxFrameHandler3. This
165 /// analysis assumes that WinEHPrepare has already been run.
166 void calculateWinCXXEHStateNumbers(const Function *ParentFn,
167 WinEHFuncInfo &FuncInfo);
170 #endif // LLVM_CODEGEN_WINEHFUNCINFO_H