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