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"
32 enum ActionType { Catch, Cleanup };
36 ActionHandler(BasicBlock *BB, ActionType Type)
37 : StartBB(BB), Type(Type), EHState(-1), HandlerBlockOrFunc(nullptr) {}
39 ActionType getType() const { return Type; }
40 BasicBlock *getStartBlock() const { return StartBB; }
42 bool hasBeenProcessed() { return HandlerBlockOrFunc != nullptr; }
44 void setHandlerBlockOrFunc(Constant *F) { HandlerBlockOrFunc = F; }
45 Constant *getHandlerBlockOrFunc() { return HandlerBlockOrFunc; }
47 void setEHState(int State) { EHState = State; }
48 int getEHState() const { return EHState; }
55 // Can be either a BlockAddress or a Function depending on the EH personality.
56 Constant *HandlerBlockOrFunc;
59 class CatchHandler : public ActionHandler {
61 CatchHandler(BasicBlock *BB, Constant *Selector, BasicBlock *NextBB)
62 : ActionHandler(BB, ActionType::Catch), Selector(Selector),
63 NextBB(NextBB), ExceptionObjectVar(nullptr),
64 ExceptionObjectIndex(-1) {}
66 // Method for support type inquiry through isa, cast, and dyn_cast:
67 static inline bool classof(const ActionHandler *H) {
68 return H->getType() == ActionType::Catch;
71 Constant *getSelector() const { return Selector; }
72 BasicBlock *getNextBB() const { return NextBB; }
74 const Value *getExceptionVar() { return ExceptionObjectVar; }
75 TinyPtrVector<BasicBlock *> &getReturnTargets() { return ReturnTargets; }
77 void setExceptionVar(const Value *Val) { ExceptionObjectVar = Val; }
78 void setExceptionVarIndex(int Index) { ExceptionObjectIndex = Index; }
79 int getExceptionVarIndex() const { return ExceptionObjectIndex; }
80 void setReturnTargets(TinyPtrVector<BasicBlock *> &Targets) {
81 ReturnTargets = Targets;
87 // While catch handlers are being outlined the ExceptionObjectVar field will
88 // be populated with the instruction in the parent frame that corresponds
89 // to the exception object (or nullptr if the catch does not use an
90 // exception object) and the ExceptionObjectIndex field will be -1.
91 // When the parseEHActions function is called to populate a vector of
92 // instances of this class, the ExceptionObjectVar field will be nullptr
93 // and the ExceptionObjectIndex will be the index of the exception object in
94 // the parent function's localescape block.
95 const Value *ExceptionObjectVar;
96 int ExceptionObjectIndex;
97 TinyPtrVector<BasicBlock *> ReturnTargets;
100 class CleanupHandler : public ActionHandler {
102 CleanupHandler(BasicBlock *BB) : ActionHandler(BB, ActionType::Cleanup) {}
104 // Method for support type inquiry through isa, cast, and dyn_cast:
105 static inline bool classof(const ActionHandler *H) {
106 return H->getType() == ActionType::Cleanup;
110 void parseEHActions(const IntrinsicInst *II,
111 SmallVectorImpl<std::unique_ptr<ActionHandler>> &Actions);
113 // The following structs respresent the .xdata for functions using C++
114 // exceptions on Windows.
116 struct WinEHUnwindMapEntry {
118 const Value *Cleanup;
121 struct WinEHHandlerType {
123 GlobalVariable *TypeDescriptor;
124 int CatchObjRecoverIdx;
125 const Value *Handler;
128 struct WinEHTryBlockMapEntry {
132 SmallVector<WinEHHandlerType, 1> HandlerArray;
135 struct WinEHFuncInfo {
136 DenseMap<const Function *, const LandingPadInst *> RootLPad;
137 DenseMap<const Function *, const InvokeInst *> LastInvoke;
138 DenseMap<const Function *, int> HandlerEnclosedState;
139 DenseMap<const Function *, bool> LastInvokeVisited;
140 DenseMap<const Instruction *, int> EHPadStateMap;
141 DenseMap<const Function *, int> CatchHandlerParentFrameObjIdx;
142 DenseMap<const Function *, int> CatchHandlerParentFrameObjOffset;
143 DenseMap<const Function *, int> CatchHandlerMaxState;
144 DenseMap<const Function *, int> HandlerBaseState;
145 SmallVector<WinEHUnwindMapEntry, 4> UnwindMap;
146 SmallVector<WinEHTryBlockMapEntry, 4> TryBlockMap;
147 SmallVector<std::pair<MCSymbol *, int>, 4> IPToStateList;
148 int UnwindHelpFrameIdx = INT_MAX;
149 int UnwindHelpFrameOffset = -1;
150 unsigned NumIPToStateFuncsVisited = 0;
152 int getLastStateNumber() const { return UnwindMap.size() - 1; }
154 /// localescape index of the 32-bit EH registration node. Set by
155 /// WinEHStatePass and used indirectly by SEH filter functions of the parent.
156 int EHRegNodeEscapeIndex = INT_MAX;
161 /// Analyze the IR in ParentFn and it's handlers to build WinEHFuncInfo, which
162 /// describes the state numbers and tables used by __CxxFrameHandler3. This
163 /// analysis assumes that WinEHPrepare has already been run.
164 void calculateWinCXXEHStateNumbers(const Function *ParentFn,
165 WinEHFuncInfo &FuncInfo);
168 #endif // LLVM_CODEGEN_WINEHFUNCINFO_H