f905af655c624265a317f0b659f2c5dc614280ac
[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 GlobalValue;
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   void setReturnTargets(TinyPtrVector<BasicBlock *> &Targets) {
79     ReturnTargets = Targets;
80   }
81
82 private:
83   Constant *Selector;
84   BasicBlock *NextBB;
85   // While catch handlers are being outlined the ExceptionObjectVar field will
86   // be populated with the instruction in the parent frame that corresponds
87   // to the exception object (or nullptr if the catch does not use an
88   // exception object) and the ExceptionObjectIndex field will be -1.
89   // When the parseEHActions function is called to populate a vector of
90   // instances of this class, the ExceptionObjectVar field will be nullptr
91   // and the ExceptionObjectIndex will be the index of the exception object in
92   // the parent function's frameescape block.
93   const Value *ExceptionObjectVar;
94   int ExceptionObjectIndex;
95   TinyPtrVector<BasicBlock *> ReturnTargets;
96 };
97
98 class CleanupHandler : public ActionHandler {
99 public:
100   CleanupHandler(BasicBlock *BB) : ActionHandler(BB, ActionType::Cleanup) {}
101
102   // Method for support type inquiry through isa, cast, and dyn_cast:
103   static inline bool classof(const ActionHandler *H) {
104     return H->getType() == ActionType::Cleanup;
105   }
106 };
107
108 void parseEHActions(const IntrinsicInst *II,
109   SmallVectorImpl<ActionHandler *> &Actions);
110
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 CatchObjIdx;
124   int CatchObjOffset;
125   Function *Handler;
126 };
127
128 struct WinEHTryBlockMapEntry {
129   int TryLow;
130   int TryHigh;
131   int CatchHigh;
132   SmallVector<WinEHHandlerType, 1> HandlerArray;
133 };
134
135 struct WinEHFuncInfo {
136   DenseMap<const LandingPadInst *, int> LandingPadStateMap;
137   DenseMap<const Function *, int> CatchHandlerParentFrameObjIdx;
138   DenseMap<const Function *, int> CatchHandlerParentFrameObjOffset;
139   SmallVector<WinEHUnwindMapEntry, 4> UnwindMap;
140   SmallVector<WinEHTryBlockMapEntry, 4> TryBlockMap;
141   SmallVector<std::pair<MCSymbol *, int>, 4> IPToStateList;
142   int UnwindHelpFrameIdx;
143   int UnwindHelpFrameOffset;
144
145   WinEHFuncInfo() : UnwindHelpFrameIdx(INT_MAX), UnwindHelpFrameOffset(-1) {}
146 };
147
148 }
149 #endif // LLVM_CODEGEN_WINEHFUNCINFO_H