[WinEH] Add some support for code generating catchpad
[oota-llvm.git] / include / llvm / CodeGen / WinEHFuncInfo.h
1 //===-- llvm/CodeGen/WinEHFuncInfo.h ----------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Data structures and associated state for Windows exception handling schemes.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_CODEGEN_WINEHFUNCINFO_H
15 #define LLVM_CODEGEN_WINEHFUNCINFO_H
16
17 #include "llvm/ADT/SmallVector.h"
18 #include "llvm/ADT/TinyPtrVector.h"
19 #include "llvm/ADT/DenseMap.h"
20
21 namespace llvm {
22 class BasicBlock;
23 class Constant;
24 class Function;
25 class GlobalVariable;
26 class InvokeInst;
27 class IntrinsicInst;
28 class LandingPadInst;
29 class MCSymbol;
30 class MachineBasicBlock;
31 class Value;
32
33 enum ActionType { Catch, Cleanup };
34
35 class ActionHandler {
36 public:
37   ActionHandler(BasicBlock *BB, ActionType Type)
38       : StartBB(BB), Type(Type), EHState(-1), HandlerBlockOrFunc(nullptr) {}
39
40   ActionType getType() const { return Type; }
41   BasicBlock *getStartBlock() const { return StartBB; }
42
43   bool hasBeenProcessed() { return HandlerBlockOrFunc != nullptr; }
44
45   void setHandlerBlockOrFunc(Constant *F) { HandlerBlockOrFunc = F; }
46   Constant *getHandlerBlockOrFunc() { return HandlerBlockOrFunc; }
47
48   void setEHState(int State) { EHState = State; }
49   int getEHState() const { return EHState; }
50
51 private:
52   BasicBlock *StartBB;
53   ActionType Type;
54   int EHState;
55
56   // Can be either a BlockAddress or a Function depending on the EH personality.
57   Constant *HandlerBlockOrFunc;
58 };
59
60 class CatchHandler : public ActionHandler {
61 public:
62   CatchHandler(BasicBlock *BB, Constant *Selector, BasicBlock *NextBB)
63       : ActionHandler(BB, ActionType::Catch), Selector(Selector),
64       NextBB(NextBB), ExceptionObjectVar(nullptr),
65       ExceptionObjectIndex(-1) {}
66
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;
70   }
71
72   Constant *getSelector() const { return Selector; }
73   BasicBlock *getNextBB() const { return NextBB; }
74
75   const Value *getExceptionVar() { return ExceptionObjectVar; }
76   TinyPtrVector<BasicBlock *> &getReturnTargets() { return ReturnTargets; }
77
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;
83   }
84
85 private:
86   Constant *Selector;
87   BasicBlock *NextBB;
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;
99 };
100
101 class CleanupHandler : public ActionHandler {
102 public:
103   CleanupHandler(BasicBlock *BB) : ActionHandler(BB, ActionType::Cleanup) {}
104
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;
108   }
109 };
110
111 void parseEHActions(const IntrinsicInst *II,
112                     SmallVectorImpl<std::unique_ptr<ActionHandler>> &Actions);
113
114 // The following structs respresent the .xdata for functions using C++
115 // exceptions on Windows.
116
117 struct WinEHUnwindMapEntry {
118   int ToState;
119   const Value *Cleanup;
120 };
121
122 struct WinEHHandlerType {
123   int Adjectives;
124   GlobalVariable *TypeDescriptor;
125   int CatchObjRecoverIdx;
126   const Value *Handler;
127   MachineBasicBlock *HandlerMBB;
128 };
129
130 struct WinEHTryBlockMapEntry {
131   int TryLow;
132   int TryHigh;
133   int CatchHigh = -1;
134   SmallVector<WinEHHandlerType, 1> HandlerArray;
135 };
136
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;
153
154   int getLastStateNumber() const { return UnwindMap.size() - 1; }
155
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;
159
160   WinEHFuncInfo() {}
161 };
162
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);
168
169 }
170 #endif // LLVM_CODEGEN_WINEHFUNCINFO_H