1 //===-- llvm/CodeGen/WinEHFuncInfo.h ----------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Data structures and associated state for Windows exception handling schemes.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CODEGEN_WINEHFUNCINFO_H
15 #define LLVM_CODEGEN_WINEHFUNCINFO_H
17 #include "llvm/ADT/DenseMap.h"
18 #include "llvm/ADT/PointerUnion.h"
19 #include "llvm/ADT/SmallVector.h"
20 #include "llvm/ADT/TinyPtrVector.h"
33 class MachineBasicBlock;
36 enum ActionType { Catch, Cleanup };
40 ActionHandler(BasicBlock *BB, ActionType Type)
41 : StartBB(BB), Type(Type), EHState(-1), HandlerBlockOrFunc(nullptr) {}
43 ActionType getType() const { return Type; }
44 BasicBlock *getStartBlock() const { return StartBB; }
46 bool hasBeenProcessed() { return HandlerBlockOrFunc != nullptr; }
48 void setHandlerBlockOrFunc(Constant *F) { HandlerBlockOrFunc = F; }
49 Constant *getHandlerBlockOrFunc() { return HandlerBlockOrFunc; }
51 void setEHState(int State) { EHState = State; }
52 int getEHState() const { return EHState; }
59 // Can be either a BlockAddress or a Function depending on the EH personality.
60 Constant *HandlerBlockOrFunc;
63 class CatchHandler : public ActionHandler {
65 CatchHandler(BasicBlock *BB, Constant *Selector, BasicBlock *NextBB)
66 : ActionHandler(BB, ActionType::Catch), Selector(Selector),
67 NextBB(NextBB), ExceptionObjectVar(nullptr),
68 ExceptionObjectIndex(-1) {}
70 // Method for support type inquiry through isa, cast, and dyn_cast:
71 static inline bool classof(const ActionHandler *H) {
72 return H->getType() == ActionType::Catch;
75 Constant *getSelector() const { return Selector; }
76 BasicBlock *getNextBB() const { return NextBB; }
78 const Value *getExceptionVar() { return ExceptionObjectVar; }
79 TinyPtrVector<BasicBlock *> &getReturnTargets() { return ReturnTargets; }
81 void setExceptionVar(const Value *Val) { ExceptionObjectVar = Val; }
82 void setExceptionVarIndex(int Index) { ExceptionObjectIndex = Index; }
83 int getExceptionVarIndex() const { return ExceptionObjectIndex; }
84 void setReturnTargets(TinyPtrVector<BasicBlock *> &Targets) {
85 ReturnTargets = Targets;
91 // While catch handlers are being outlined the ExceptionObjectVar field will
92 // be populated with the instruction in the parent frame that corresponds
93 // to the exception object (or nullptr if the catch does not use an
94 // exception object) and the ExceptionObjectIndex field will be -1.
95 // When the parseEHActions function is called to populate a vector of
96 // instances of this class, the ExceptionObjectVar field will be nullptr
97 // and the ExceptionObjectIndex will be the index of the exception object in
98 // the parent function's localescape block.
99 const Value *ExceptionObjectVar;
100 int ExceptionObjectIndex;
101 TinyPtrVector<BasicBlock *> ReturnTargets;
104 class CleanupHandler : public ActionHandler {
106 CleanupHandler(BasicBlock *BB) : ActionHandler(BB, ActionType::Cleanup) {}
108 // Method for support type inquiry through isa, cast, and dyn_cast:
109 static inline bool classof(const ActionHandler *H) {
110 return H->getType() == ActionType::Cleanup;
114 void parseEHActions(const IntrinsicInst *II,
115 SmallVectorImpl<std::unique_ptr<ActionHandler>> &Actions);
117 // The following structs respresent the .xdata for functions using C++
118 // exceptions on Windows.
120 typedef PointerUnion<const BasicBlock *, MachineBasicBlock *> MBBOrBasicBlock;
121 typedef PointerUnion<const Value *, const MachineBasicBlock *> ValueOrMBB;
123 struct WinEHUnwindMapEntry {
128 /// Similar to WinEHUnwindMapEntry, but supports SEH filters.
129 struct SEHUnwindMapEntry {
130 /// If unwinding continues through this handler, transition to the handler at
131 /// this state. This indexes into SEHUnwindMap.
134 bool IsFinally = false;
136 /// Holds the filter expression function.
137 const Function *Filter = nullptr;
139 /// Holds the __except or __finally basic block.
140 MBBOrBasicBlock Handler;
143 struct WinEHHandlerType {
145 int CatchObjRecoverIdx;
146 /// The CatchObj starts out life as an LLVM alloca, is turned into a frame
147 /// index, and after PEI, becomes a raw offset.
149 const AllocaInst *Alloca;
153 GlobalVariable *TypeDescriptor;
157 struct WinEHTryBlockMapEntry {
161 SmallVector<WinEHHandlerType, 1> HandlerArray;
164 struct WinEHFuncInfo {
165 DenseMap<const Instruction *, int> EHPadStateMap;
166 DenseMap<const CatchReturnInst *, const BasicBlock *>
167 CatchRetSuccessorColorMap;
168 DenseMap<MCSymbol *, std::pair<int, MCSymbol *>> InvokeToStateMap;
169 SmallVector<WinEHUnwindMapEntry, 4> UnwindMap;
170 SmallVector<WinEHTryBlockMapEntry, 4> TryBlockMap;
171 SmallVector<SEHUnwindMapEntry, 4> SEHUnwindMap;
172 int UnwindHelpFrameIdx = INT_MAX;
173 int UnwindHelpFrameOffset = -1;
175 int getLastStateNumber() const { return UnwindMap.size() - 1; }
177 void addIPToStateRange(const BasicBlock *PadBB, MCSymbol *InvokeBegin,
178 MCSymbol *InvokeEnd);
180 /// localescape index of the 32-bit EH registration node. Set by
181 /// WinEHStatePass and used indirectly by SEH filter functions of the parent.
182 int EHRegNodeEscapeIndex = INT_MAX;
183 const AllocaInst *EHRegNode = nullptr;
184 int EHRegNodeFrameIndex = INT_MAX;
185 int EHRegNodeEndOffset = INT_MAX;
190 /// Analyze the IR in ParentFn and it's handlers to build WinEHFuncInfo, which
191 /// describes the state numbers and tables used by __CxxFrameHandler3. This
192 /// analysis assumes that WinEHPrepare has already been run.
193 void calculateWinCXXEHStateNumbers(const Function *ParentFn,
194 WinEHFuncInfo &FuncInfo);
196 void calculateSEHStateNumbers(const Function *ParentFn,
197 WinEHFuncInfo &FuncInfo);
199 void calculateCatchReturnSuccessorColors(const Function *Fn,
200 WinEHFuncInfo &FuncInfo);
202 #endif // LLVM_CODEGEN_WINEHFUNCINFO_H