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