[WinEH] Teach AsmPrinter about funclets
[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   /// Holds the filter expression function.
135   const Function *Filter = nullptr;
136
137   /// Holds the __except or __finally basic block.
138   MBBOrBasicBlock Handler;
139 };
140
141 struct WinEHHandlerType {
142   int Adjectives;
143   int CatchObjRecoverIdx;
144   /// The CatchObj starts out life as an LLVM alloca, is turned into a frame
145   /// index, and after PEI, becomes a raw offset.
146   union {
147     const AllocaInst *Alloca;
148     int FrameOffset;
149     int FrameIndex;
150   } CatchObj = {};
151   GlobalVariable *TypeDescriptor;
152   ValueOrMBB Handler;
153 };
154
155 struct WinEHTryBlockMapEntry {
156   int TryLow = -1;
157   int TryHigh = -1;
158   int CatchHigh = -1;
159   SmallVector<WinEHHandlerType, 1> HandlerArray;
160 };
161
162 struct WinEHFuncInfo {
163   DenseMap<const Instruction *, int> EHPadStateMap;
164   DenseMap<MCSymbol *, std::pair<int, MCSymbol *>> InvokeToStateMap;
165   SmallVector<WinEHUnwindMapEntry, 4> UnwindMap;
166   SmallVector<WinEHTryBlockMapEntry, 4> TryBlockMap;
167   SmallVector<SEHUnwindMapEntry, 4> SEHUnwindMap;
168   int UnwindHelpFrameIdx = INT_MAX;
169   int UnwindHelpFrameOffset = -1;
170
171   int getLastStateNumber() const { return UnwindMap.size() - 1; }
172
173   void addIPToStateRange(const BasicBlock *PadBB, MCSymbol *InvokeBegin,
174                          MCSymbol *InvokeEnd);
175
176   /// localescape index of the 32-bit EH registration node. Set by
177   /// WinEHStatePass and used indirectly by SEH filter functions of the parent.
178   int EHRegNodeEscapeIndex = INT_MAX;
179   const AllocaInst *EHRegNode = nullptr;
180   int EHRegNodeFrameIndex = INT_MAX;
181   int EHRegNodeEndOffset = INT_MAX;
182
183   WinEHFuncInfo() {}
184 };
185
186 /// Analyze the IR in ParentFn and it's handlers to build WinEHFuncInfo, which
187 /// describes the state numbers and tables used by __CxxFrameHandler3. This
188 /// analysis assumes that WinEHPrepare has already been run.
189 void calculateWinCXXEHStateNumbers(const Function *ParentFn,
190                                    WinEHFuncInfo &FuncInfo);
191
192 void calculateSEHStateNumbers(const Function *ParentFn,
193                               WinEHFuncInfo &FuncInfo);
194 }
195 #endif // LLVM_CODEGEN_WINEHFUNCINFO_H