[WinEHPrepare] Don't rely on the order of IR
[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   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<ActionHandler *> &Actions);
111
112
113 // The following structs respresent the .xdata for functions using C++
114 // exceptions on Windows.
115
116 struct WinEHUnwindMapEntry {
117   int ToState;
118   Function *Cleanup;
119 };
120
121 struct WinEHHandlerType {
122   int Adjectives;
123   GlobalVariable *TypeDescriptor;
124   int CatchObjRecoverIdx;
125   Function *Handler;
126 };
127
128 struct WinEHTryBlockMapEntry {
129   int TryLow;
130   int TryHigh;
131   SmallVector<WinEHHandlerType, 1> HandlerArray;
132 };
133
134 struct WinEHFuncInfo {
135   DenseMap<const LandingPadInst *, int> LandingPadStateMap;
136   DenseMap<const Function *, int> CatchHandlerParentFrameObjIdx;
137   DenseMap<const Function *, int> CatchHandlerParentFrameObjOffset;
138   DenseMap<const Function *, int> CatchHandlerMaxState;
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   unsigned NumIPToStateFuncsVisited;
146
147   WinEHFuncInfo()
148       : UnwindHelpFrameIdx(INT_MAX), UnwindHelpFrameOffset(-1),
149         NumIPToStateFuncsVisited(0) {}
150 };
151
152 }
153 #endif // LLVM_CODEGEN_WINEHFUNCINFO_H