[WinEH] C++ EH state numbering fixes
[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 IntrinsicInst;
27 class LandingPadInst;
28 class MCSymbol;
29 class Value;
30
31 enum ActionType { Catch, Cleanup };
32
33 class ActionHandler {
34 public:
35   ActionHandler(BasicBlock *BB, ActionType Type)
36       : StartBB(BB), Type(Type), EHState(-1), HandlerBlockOrFunc(nullptr) {}
37
38   ActionType getType() const { return Type; }
39   BasicBlock *getStartBlock() const { return StartBB; }
40
41   bool hasBeenProcessed() { return HandlerBlockOrFunc != nullptr; }
42
43   void setHandlerBlockOrFunc(Constant *F) { HandlerBlockOrFunc = F; }
44   Constant *getHandlerBlockOrFunc() { return HandlerBlockOrFunc; }
45
46   void setEHState(int State) { EHState = State; }
47   int getEHState() const { return EHState; }
48
49 private:
50   BasicBlock *StartBB;
51   ActionType Type;
52   int EHState;
53
54   // Can be either a BlockAddress or a Function depending on the EH personality.
55   Constant *HandlerBlockOrFunc;
56 };
57
58 class CatchHandler : public ActionHandler {
59 public:
60   CatchHandler(BasicBlock *BB, Constant *Selector, BasicBlock *NextBB)
61       : ActionHandler(BB, ActionType::Catch), Selector(Selector),
62       NextBB(NextBB), ExceptionObjectVar(nullptr),
63       ExceptionObjectIndex(-1) {}
64
65   // Method for support type inquiry through isa, cast, and dyn_cast:
66   static inline bool classof(const ActionHandler *H) {
67     return H->getType() == ActionType::Catch;
68   }
69
70   Constant *getSelector() const { return Selector; }
71   BasicBlock *getNextBB() const { return NextBB; }
72
73   const Value *getExceptionVar() { return ExceptionObjectVar; }
74   TinyPtrVector<BasicBlock *> &getReturnTargets() { return ReturnTargets; }
75
76   void setExceptionVar(const Value *Val) { ExceptionObjectVar = Val; }
77   void setExceptionVarIndex(int Index) { ExceptionObjectIndex = Index;  }
78   int getExceptionVarIndex() const { return ExceptionObjectIndex; }
79   void setReturnTargets(TinyPtrVector<BasicBlock *> &Targets) {
80     ReturnTargets = Targets;
81   }
82
83 private:
84   Constant *Selector;
85   BasicBlock *NextBB;
86   // While catch handlers are being outlined the ExceptionObjectVar field will
87   // be populated with the instruction in the parent frame that corresponds
88   // to the exception object (or nullptr if the catch does not use an
89   // exception object) and the ExceptionObjectIndex field will be -1.
90   // When the parseEHActions function is called to populate a vector of
91   // instances of this class, the ExceptionObjectVar field will be nullptr
92   // and the ExceptionObjectIndex will be the index of the exception object in
93   // the parent function's frameescape block.
94   const Value *ExceptionObjectVar;
95   int ExceptionObjectIndex;
96   TinyPtrVector<BasicBlock *> ReturnTargets;
97 };
98
99 class CleanupHandler : public ActionHandler {
100 public:
101   CleanupHandler(BasicBlock *BB) : ActionHandler(BB, ActionType::Cleanup) {}
102
103   // Method for support type inquiry through isa, cast, and dyn_cast:
104   static inline bool classof(const ActionHandler *H) {
105     return H->getType() == ActionType::Cleanup;
106   }
107 };
108
109 void parseEHActions(const IntrinsicInst *II,
110                     SmallVectorImpl<std::unique_ptr<ActionHandler>> &Actions);
111
112 // The following structs respresent the .xdata for functions using C++
113 // exceptions on Windows.
114
115 struct WinEHUnwindMapEntry {
116   int ToState;
117   Function *Cleanup;
118 };
119
120 struct WinEHHandlerType {
121   int Adjectives;
122   GlobalVariable *TypeDescriptor;
123   int CatchObjRecoverIdx;
124   Function *Handler;
125 };
126
127 struct WinEHTryBlockMapEntry {
128   int TryLow;
129   int TryHigh;
130   SmallVector<WinEHHandlerType, 1> HandlerArray;
131 };
132
133 struct WinEHFuncInfo {
134   DenseMap<const Function *, const LandingPadInst *> RootLPad;
135   DenseMap<const Function *, const InvokeInst *> LastInvoke;
136   DenseMap<const Function *, int> HandlerEnclosedState;
137   DenseMap<const Function *, bool> LastInvokeVisited;
138   DenseMap<const LandingPadInst *, int> LandingPadStateMap;
139   DenseMap<const Function *, int> CatchHandlerParentFrameObjIdx;
140   DenseMap<const Function *, int> CatchHandlerParentFrameObjOffset;
141   DenseMap<const Function *, int> CatchHandlerMaxState;
142   DenseMap<const Function *, int> HandlerBaseState;
143   SmallVector<WinEHUnwindMapEntry, 4> UnwindMap;
144   SmallVector<WinEHTryBlockMapEntry, 4> TryBlockMap;
145   SmallVector<std::pair<MCSymbol *, int>, 4> IPToStateList;
146   int UnwindHelpFrameIdx;
147   int UnwindHelpFrameOffset;
148
149   unsigned NumIPToStateFuncsVisited;
150
151   WinEHFuncInfo()
152       : UnwindHelpFrameIdx(INT_MAX), UnwindHelpFrameOffset(-1),
153         NumIPToStateFuncsVisited(0) {}
154 };
155
156 }
157 #endif // LLVM_CODEGEN_WINEHFUNCINFO_H