[SEH] Emit 32-bit SEH tables for the new EH IR
[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/DenseMap.h"
18 #include "llvm/ADT/PointerUnion.h"
19 #include "llvm/ADT/SmallVector.h"
20 #include "llvm/ADT/TinyPtrVector.h"
21
22 namespace llvm {
23 class AllocaInst;
24 class BasicBlock;
25 class Constant;
26 class Function;
27 class GlobalVariable;
28 class InvokeInst;
29 class IntrinsicInst;
30 class LandingPadInst;
31 class MCSymbol;
32 class MachineBasicBlock;
33 class Value;
34
35 enum ActionType { Catch, Cleanup };
36
37 class ActionHandler {
38 public:
39   ActionHandler(BasicBlock *BB, ActionType Type)
40       : StartBB(BB), Type(Type), EHState(-1), HandlerBlockOrFunc(nullptr) {}
41
42   ActionType getType() const { return Type; }
43   BasicBlock *getStartBlock() const { return StartBB; }
44
45   bool hasBeenProcessed() { return HandlerBlockOrFunc != nullptr; }
46
47   void setHandlerBlockOrFunc(Constant *F) { HandlerBlockOrFunc = F; }
48   Constant *getHandlerBlockOrFunc() { return HandlerBlockOrFunc; }
49
50   void setEHState(int State) { EHState = State; }
51   int getEHState() const { return EHState; }
52
53 private:
54   BasicBlock *StartBB;
55   ActionType Type;
56   int EHState;
57
58   // Can be either a BlockAddress or a Function depending on the EH personality.
59   Constant *HandlerBlockOrFunc;
60 };
61
62 class CatchHandler : public ActionHandler {
63 public:
64   CatchHandler(BasicBlock *BB, Constant *Selector, BasicBlock *NextBB)
65       : ActionHandler(BB, ActionType::Catch), Selector(Selector),
66       NextBB(NextBB), ExceptionObjectVar(nullptr),
67       ExceptionObjectIndex(-1) {}
68
69   // Method for support type inquiry through isa, cast, and dyn_cast:
70   static inline bool classof(const ActionHandler *H) {
71     return H->getType() == ActionType::Catch;
72   }
73
74   Constant *getSelector() const { return Selector; }
75   BasicBlock *getNextBB() const { return NextBB; }
76
77   const Value *getExceptionVar() { return ExceptionObjectVar; }
78   TinyPtrVector<BasicBlock *> &getReturnTargets() { return ReturnTargets; }
79
80   void setExceptionVar(const Value *Val) { ExceptionObjectVar = Val; }
81   void setExceptionVarIndex(int Index) { ExceptionObjectIndex = Index;  }
82   int getExceptionVarIndex() const { return ExceptionObjectIndex; }
83   void setReturnTargets(TinyPtrVector<BasicBlock *> &Targets) {
84     ReturnTargets = Targets;
85   }
86
87 private:
88   Constant *Selector;
89   BasicBlock *NextBB;
90   // While catch handlers are being outlined the ExceptionObjectVar field will
91   // be populated with the instruction in the parent frame that corresponds
92   // to the exception object (or nullptr if the catch does not use an
93   // exception object) and the ExceptionObjectIndex field will be -1.
94   // When the parseEHActions function is called to populate a vector of
95   // instances of this class, the ExceptionObjectVar field will be nullptr
96   // and the ExceptionObjectIndex will be the index of the exception object in
97   // the parent function's localescape block.
98   const Value *ExceptionObjectVar;
99   int ExceptionObjectIndex;
100   TinyPtrVector<BasicBlock *> ReturnTargets;
101 };
102
103 class CleanupHandler : public ActionHandler {
104 public:
105   CleanupHandler(BasicBlock *BB) : ActionHandler(BB, ActionType::Cleanup) {}
106
107   // Method for support type inquiry through isa, cast, and dyn_cast:
108   static inline bool classof(const ActionHandler *H) {
109     return H->getType() == ActionType::Cleanup;
110   }
111 };
112
113 void parseEHActions(const IntrinsicInst *II,
114                     SmallVectorImpl<std::unique_ptr<ActionHandler>> &Actions);
115
116 // The following structs respresent the .xdata for functions using C++
117 // exceptions on Windows.
118
119 struct WinEHUnwindMapEntry {
120   int ToState;
121   const Value *Cleanup;
122 };
123
124 typedef PointerUnion<const BasicBlock *, MachineBasicBlock *> MBBOrBasicBlock;
125 typedef PointerUnion<const Value *, MachineBasicBlock *> ValueOrMBB;
126
127 /// Similar to WinEHUnwindMapEntry, but supports SEH filters.
128 struct SEHUnwindMapEntry {
129   /// If unwinding continues through this handler, transition to the handler at
130   /// this state. This indexes into SEHUnwindMap.
131   int ToState = -1;
132
133   /// Holds the filter expression function.
134   const Function *Filter = nullptr;
135
136   /// Holds the __except or __finally basic block.
137   MBBOrBasicBlock Handler;
138 };
139
140 struct WinEHHandlerType {
141   int Adjectives;
142   GlobalVariable *TypeDescriptor;
143   int CatchObjRecoverIdx;
144   ValueOrMBB Handler;
145 };
146
147 struct WinEHTryBlockMapEntry {
148   int TryLow = -1;
149   int TryHigh = -1;
150   int CatchHigh = -1;
151   SmallVector<WinEHHandlerType, 1> HandlerArray;
152 };
153
154 struct WinEHFuncInfo {
155   DenseMap<const Function *, const LandingPadInst *> RootLPad;
156   DenseMap<const Function *, const InvokeInst *> LastInvoke;
157   DenseMap<const Function *, int> HandlerEnclosedState;
158   DenseMap<const Function *, bool> LastInvokeVisited;
159   DenseMap<const Instruction *, int> EHPadStateMap;
160   DenseMap<const Function *, int> CatchHandlerParentFrameObjIdx;
161   DenseMap<const Function *, int> CatchHandlerParentFrameObjOffset;
162   DenseMap<const Function *, int> CatchHandlerMaxState;
163   DenseMap<const Function *, int> HandlerBaseState;
164   SmallVector<WinEHUnwindMapEntry, 4> UnwindMap;
165   SmallVector<WinEHTryBlockMapEntry, 4> TryBlockMap;
166   SmallVector<SEHUnwindMapEntry, 4> SEHUnwindMap;
167   SmallVector<std::pair<MCSymbol *, int>, 4> IPToStateList;
168   int UnwindHelpFrameIdx = INT_MAX;
169   int UnwindHelpFrameOffset = -1;
170   unsigned NumIPToStateFuncsVisited = 0;
171
172   int getLastStateNumber() const { return UnwindMap.size() - 1; }
173
174   /// localescape index of the 32-bit EH registration node. Set by
175   /// WinEHStatePass and used indirectly by SEH filter functions of the parent.
176   int EHRegNodeEscapeIndex = INT_MAX;
177   const AllocaInst *EHRegNode = nullptr;
178   int EHRegNodeFrameIndex = INT_MAX;
179
180   WinEHFuncInfo() {}
181 };
182
183 /// Analyze the IR in ParentFn and it's handlers to build WinEHFuncInfo, which
184 /// describes the state numbers and tables used by __CxxFrameHandler3. This
185 /// analysis assumes that WinEHPrepare has already been run.
186 void calculateWinCXXEHStateNumbers(const Function *ParentFn,
187                                    WinEHFuncInfo &FuncInfo);
188
189 void calculateSEHStateNumbers(const Function *ParentFn,
190                               WinEHFuncInfo &FuncInfo);
191 }
192 #endif // LLVM_CODEGEN_WINEHFUNCINFO_H