Fix unused variable warning in non-debug builds.
[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 typedef PointerUnion<const BasicBlock *, MachineBasicBlock *> MBBOrBasicBlock;
120 typedef PointerUnion<const Value *, MachineBasicBlock *> ValueOrMBB;
121
122 struct WinEHUnwindMapEntry {
123   int ToState;
124   ValueOrMBB Cleanup;
125 };
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   int CatchObjRecoverIdx;
143   /// The CatchObj starts out life as an LLVM alloca, is turned into a frame
144   /// index, and after PEI, becomes a raw offset.
145   union {
146     const AllocaInst *Alloca;
147     int FrameOffset;
148     int FrameIndex;
149   } CatchObj = {};
150   GlobalVariable *TypeDescriptor;
151   ValueOrMBB Handler;
152 };
153
154 struct WinEHTryBlockMapEntry {
155   int TryLow = -1;
156   int TryHigh = -1;
157   int CatchHigh = -1;
158   SmallVector<WinEHHandlerType, 1> HandlerArray;
159 };
160
161 struct WinEHFuncInfo {
162   DenseMap<const Instruction *, int> EHPadStateMap;
163   SmallVector<WinEHUnwindMapEntry, 4> UnwindMap;
164   SmallVector<WinEHTryBlockMapEntry, 4> TryBlockMap;
165   SmallVector<SEHUnwindMapEntry, 4> SEHUnwindMap;
166   SmallVector<std::pair<MCSymbol *, int>, 4> IPToStateList;
167   int UnwindHelpFrameIdx = INT_MAX;
168   int UnwindHelpFrameOffset = -1;
169
170   int getLastStateNumber() const { return UnwindMap.size() - 1; }
171
172   /// localescape index of the 32-bit EH registration node. Set by
173   /// WinEHStatePass and used indirectly by SEH filter functions of the parent.
174   int EHRegNodeEscapeIndex = INT_MAX;
175   const AllocaInst *EHRegNode = nullptr;
176   int EHRegNodeFrameIndex = INT_MAX;
177   int EHRegNodeEndOffset = INT_MAX;
178
179   WinEHFuncInfo() {}
180 };
181
182 /// Analyze the IR in ParentFn and it's handlers to build WinEHFuncInfo, which
183 /// describes the state numbers and tables used by __CxxFrameHandler3. This
184 /// analysis assumes that WinEHPrepare has already been run.
185 void calculateWinCXXEHStateNumbers(const Function *ParentFn,
186                                    WinEHFuncInfo &FuncInfo);
187
188 void calculateSEHStateNumbers(const Function *ParentFn,
189                               WinEHFuncInfo &FuncInfo);
190 }
191 #endif // LLVM_CODEGEN_WINEHFUNCINFO_H