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 {
121 struct WinEHHandlerType {
123 GlobalVariable *TypeDescriptor;
124 int CatchObjRecoverIdx;
128 struct WinEHTryBlockMapEntry {
131 SmallVector<WinEHHandlerType, 1> HandlerArray;
134 struct WinEHFuncInfo {
135 DenseMap<const Function *, const LandingPadInst *> RootLPad;
136 DenseMap<const Function *, const InvokeInst *> LastInvoke;
137 DenseMap<const Function *, int> HandlerEnclosedState;
138 DenseMap<const Function *, bool> LastInvokeVisited;
139 DenseMap<const LandingPadInst *, int> LandingPadStateMap;
140 DenseMap<const Function *, int> CatchHandlerParentFrameObjIdx;
141 DenseMap<const Function *, int> CatchHandlerParentFrameObjOffset;
142 DenseMap<const Function *, int> CatchHandlerMaxState;
143 DenseMap<const Function *, int> HandlerBaseState;
144 SmallVector<WinEHUnwindMapEntry, 4> UnwindMap;
145 SmallVector<WinEHTryBlockMapEntry, 4> TryBlockMap;
146 SmallVector<std::pair<MCSymbol *, int>, 4> IPToStateList;
147 int UnwindHelpFrameIdx = INT_MAX;
148 int UnwindHelpFrameOffset = -1;
149 unsigned NumIPToStateFuncsVisited = 0;
151 /// localescape index of the 32-bit EH registration node. Set by
152 /// WinEHStatePass and used indirectly by SEH filter functions of the parent.
153 int EHRegNodeEscapeIndex = INT_MAX;
158 /// Analyze the IR in ParentFn and it's handlers to build WinEHFuncInfo, which
159 /// describes the state numbers and tables used by __CxxFrameHandler3. This
160 /// analysis assumes that WinEHPrepare has already been run.
161 void calculateWinCXXEHStateNumbers(const Function *ParentFn,
162 WinEHFuncInfo &FuncInfo);
165 #endif // LLVM_CODEGEN_WINEHFUNCINFO_H