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:
ActionHandler(BasicBlock * BB,ActionType Type)35   ActionHandler(BasicBlock *BB, ActionType Type)
36       : StartBB(BB), Type(Type), EHState(-1), HandlerBlockOrFunc(nullptr) {}
37 
getType()38   ActionType getType() const { return Type; }
getStartBlock()39   BasicBlock *getStartBlock() const { return StartBB; }
40 
hasBeenProcessed()41   bool hasBeenProcessed() { return HandlerBlockOrFunc != nullptr; }
42 
setHandlerBlockOrFunc(Constant * F)43   void setHandlerBlockOrFunc(Constant *F) { HandlerBlockOrFunc = F; }
getHandlerBlockOrFunc()44   Constant *getHandlerBlockOrFunc() { return HandlerBlockOrFunc; }
45 
setEHState(int State)46   void setEHState(int State) { EHState = State; }
getEHState()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:
CatchHandler(BasicBlock * BB,Constant * Selector,BasicBlock * NextBB)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:
classof(const ActionHandler * H)66   static inline bool classof(const ActionHandler *H) {
67     return H->getType() == ActionType::Catch;
68   }
69 
getSelector()70   Constant *getSelector() const { return Selector; }
getNextBB()71   BasicBlock *getNextBB() const { return NextBB; }
72 
getExceptionVar()73   const Value *getExceptionVar() { return ExceptionObjectVar; }
getReturnTargets()74   TinyPtrVector<BasicBlock *> &getReturnTargets() { return ReturnTargets; }
75 
setExceptionVar(const Value * Val)76   void setExceptionVar(const Value *Val) { ExceptionObjectVar = Val; }
setExceptionVarIndex(int Index)77   void setExceptionVarIndex(int Index) { ExceptionObjectIndex = Index;  }
getExceptionVarIndex()78   int getExceptionVarIndex() const { return ExceptionObjectIndex; }
setReturnTargets(TinyPtrVector<BasicBlock * > & Targets)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:
CleanupHandler(BasicBlock * BB)101   CleanupHandler(BasicBlock *BB) : ActionHandler(BB, ActionType::Cleanup) {}
102 
103   // Method for support type inquiry through isa, cast, and dyn_cast:
classof(const ActionHandler * H)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 
WinEHFuncInfoWinEHFuncInfo147   WinEHFuncInfo()
148       : UnwindHelpFrameIdx(INT_MAX), UnwindHelpFrameOffset(-1),
149         NumIPToStateFuncsVisited(0) {}
150 };
151 
152 }
153 #endif // LLVM_CODEGEN_WINEHFUNCINFO_H
154