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"
31 enum ActionType { Catch, Cleanup };
35 ActionHandler(BasicBlock *BB, ActionType Type)
36 : StartBB(BB), Type(Type), EHState(-1), HandlerBlockOrFunc(nullptr) {}
38 ActionType getType() const { return Type; }
39 BasicBlock *getStartBlock() const { return StartBB; }
41 bool hasBeenProcessed() { return HandlerBlockOrFunc != nullptr; }
43 void setHandlerBlockOrFunc(Constant *F) { HandlerBlockOrFunc = F; }
44 Constant *getHandlerBlockOrFunc() { return HandlerBlockOrFunc; }
46 void setEHState(int State) { EHState = State; }
47 int getEHState() const { return EHState; }
54 // Can be either a BlockAddress or a Function depending on the EH personality.
55 Constant *HandlerBlockOrFunc;
58 class CatchHandler : public ActionHandler {
60 CatchHandler(BasicBlock *BB, Constant *Selector, BasicBlock *NextBB)
61 : ActionHandler(BB, ActionType::Catch), Selector(Selector),
62 NextBB(NextBB), ExceptionObjectVar(nullptr),
63 ExceptionObjectIndex(-1) {}
65 // Method for support type inquiry through isa, cast, and dyn_cast:
66 static inline bool classof(const ActionHandler *H) {
67 return H->getType() == ActionType::Catch;
70 Constant *getSelector() const { return Selector; }
71 BasicBlock *getNextBB() const { return NextBB; }
73 const Value *getExceptionVar() { return ExceptionObjectVar; }
74 TinyPtrVector<BasicBlock *> &getReturnTargets() { return ReturnTargets; }
76 void setExceptionVar(const Value *Val) { ExceptionObjectVar = Val; }
77 void setExceptionVarIndex(int Index) { ExceptionObjectIndex = Index; }
78 int getExceptionVarIndex() const { return ExceptionObjectIndex; }
79 void setReturnTargets(TinyPtrVector<BasicBlock *> &Targets) {
80 ReturnTargets = Targets;
86 // While catch handlers are being outlined the ExceptionObjectVar field will
87 // be populated with the instruction in the parent frame that corresponds
88 // to the exception object (or nullptr if the catch does not use an
89 // exception object) and the ExceptionObjectIndex field will be -1.
90 // When the parseEHActions function is called to populate a vector of
91 // instances of this class, the ExceptionObjectVar field will be nullptr
92 // and the ExceptionObjectIndex will be the index of the exception object in
93 // the parent function's frameescape block.
94 const Value *ExceptionObjectVar;
95 int ExceptionObjectIndex;
96 TinyPtrVector<BasicBlock *> ReturnTargets;
99 class CleanupHandler : public ActionHandler {
101 CleanupHandler(BasicBlock *BB) : ActionHandler(BB, ActionType::Cleanup) {}
103 // Method for support type inquiry through isa, cast, and dyn_cast:
104 static inline bool classof(const ActionHandler *H) {
105 return H->getType() == ActionType::Cleanup;
109 void parseEHActions(const IntrinsicInst *II,
110 SmallVectorImpl<std::unique_ptr<ActionHandler>> &Actions);
112 // The following structs respresent the .xdata for functions using C++
113 // exceptions on Windows.
115 struct WinEHUnwindMapEntry {
120 struct WinEHHandlerType {
122 GlobalVariable *TypeDescriptor;
123 int CatchObjRecoverIdx;
127 struct WinEHTryBlockMapEntry {
130 SmallVector<WinEHHandlerType, 1> HandlerArray;
133 struct WinEHFuncInfo {
134 DenseMap<const LandingPadInst *, int> LandingPadStateMap;
135 DenseMap<const Function *, int> CatchHandlerParentFrameObjIdx;
136 DenseMap<const Function *, int> CatchHandlerParentFrameObjOffset;
137 DenseMap<const Function *, int> CatchHandlerMaxState;
138 DenseMap<const Function *, int> HandlerBaseState;
139 SmallVector<WinEHUnwindMapEntry, 4> UnwindMap;
140 SmallVector<WinEHTryBlockMapEntry, 4> TryBlockMap;
141 SmallVector<std::pair<MCSymbol *, int>, 4> IPToStateList;
142 int UnwindHelpFrameIdx;
143 int UnwindHelpFrameOffset;
145 unsigned NumIPToStateFuncsVisited;
148 : UnwindHelpFrameIdx(INT_MAX), UnwindHelpFrameOffset(-1),
149 NumIPToStateFuncsVisited(0) {}
153 #endif // LLVM_CODEGEN_WINEHFUNCINFO_H