f6ad7a8572ab0ff560df9d3e170a5d7816f07eca
[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/DenseMap.h"
18 #include "llvm/ADT/PointerUnion.h"
19 #include "llvm/ADT/SmallVector.h"
20 #include "llvm/ADT/TinyPtrVector.h"
21
22 namespace llvm {
23 class AllocaInst;
24 class BasicBlock;
25 class CatchReturnInst;
26 class Constant;
27 class Function;
28 class GlobalVariable;
29 class InvokeInst;
30 class IntrinsicInst;
31 class LandingPadInst;
32 class MCExpr;
33 class MCSymbol;
34 class MachineBasicBlock;
35 class Value;
36
37 // The following structs respresent the .xdata tables for various
38 // Windows-related EH personalities.
39
40 typedef PointerUnion<const BasicBlock *, MachineBasicBlock *> MBBOrBasicBlock;
41
42 struct CxxUnwindMapEntry {
43   int ToState;
44   MBBOrBasicBlock Cleanup;
45 };
46
47 /// Similar to CxxUnwindMapEntry, but supports SEH filters.
48 struct SEHUnwindMapEntry {
49   /// If unwinding continues through this handler, transition to the handler at
50   /// this state. This indexes into SEHUnwindMap.
51   int ToState = -1;
52
53   bool IsFinally = false;
54
55   /// Holds the filter expression function.
56   const Function *Filter = nullptr;
57
58   /// Holds the __except or __finally basic block.
59   MBBOrBasicBlock Handler;
60 };
61
62 struct WinEHHandlerType {
63   int Adjectives;
64   /// The CatchObj starts out life as an LLVM alloca and is eventually turned
65   /// frame index.
66   union {
67     const AllocaInst *Alloca;
68     int FrameIndex;
69   } CatchObj = {};
70   GlobalVariable *TypeDescriptor;
71   MBBOrBasicBlock Handler;
72 };
73
74 struct WinEHTryBlockMapEntry {
75   int TryLow = -1;
76   int TryHigh = -1;
77   int CatchHigh = -1;
78   SmallVector<WinEHHandlerType, 1> HandlerArray;
79 };
80
81 enum class ClrHandlerType { Catch, Finally, Fault, Filter };
82
83 struct ClrEHUnwindMapEntry {
84   MBBOrBasicBlock Handler;
85   uint32_t TypeToken;
86   int HandlerParentState; ///< Outer handler enclosing this entry's handler
87   int TryParentState; ///< Outer try region enclosing this entry's try region,
88                       ///< treating later catches on same try as "outer"
89   ClrHandlerType HandlerType;
90 };
91
92 struct WinEHFuncInfo {
93   DenseMap<const Instruction *, int> EHPadStateMap;
94   DenseMap<const FuncletPadInst *, int> FuncletBaseStateMap;
95   DenseMap<const InvokeInst *, int> InvokeStateMap;
96   DenseMap<const CatchReturnInst *, const BasicBlock *>
97       CatchRetSuccessorColorMap;
98   DenseMap<MCSymbol *, std::pair<int, MCSymbol *>> LabelToStateMap;
99   SmallVector<CxxUnwindMapEntry, 4> CxxUnwindMap;
100   SmallVector<WinEHTryBlockMapEntry, 4> TryBlockMap;
101   SmallVector<SEHUnwindMapEntry, 4> SEHUnwindMap;
102   SmallVector<ClrEHUnwindMapEntry, 4> ClrEHUnwindMap;
103   int UnwindHelpFrameIdx = INT_MAX;
104   int PSPSymFrameIdx = INT_MAX;
105
106   int getLastStateNumber() const { return CxxUnwindMap.size() - 1; }
107
108   void addIPToStateRange(const InvokeInst *II, MCSymbol *InvokeBegin,
109                          MCSymbol *InvokeEnd);
110
111   int EHRegNodeFrameIndex = INT_MAX;
112   int EHRegNodeEndOffset = INT_MAX;
113   int SEHSetFrameOffset = INT_MAX;
114
115   WinEHFuncInfo();
116 };
117
118 /// Analyze the IR in ParentFn and it's handlers to build WinEHFuncInfo, which
119 /// describes the state numbers and tables used by __CxxFrameHandler3. This
120 /// analysis assumes that WinEHPrepare has already been run.
121 void calculateWinCXXEHStateNumbers(const Function *ParentFn,
122                                    WinEHFuncInfo &FuncInfo);
123
124 void calculateSEHStateNumbers(const Function *ParentFn,
125                               WinEHFuncInfo &FuncInfo);
126
127 void calculateClrEHStateNumbers(const Function *Fn, WinEHFuncInfo &FuncInfo);
128
129 void calculateCatchReturnSuccessorColors(const Function *Fn,
130                                          WinEHFuncInfo &FuncInfo);
131 }
132 #endif // LLVM_CODEGEN_WINEHFUNCINFO_H