89a750582472d82994f0e001cfab6edca13b76a2
[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 typedef PointerUnion<const Value *, const MachineBasicBlock *> ValueOrMBB;
41
42 struct CxxUnwindMapEntry {
43   int ToState;
44   ValueOrMBB 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   int CatchObjRecoverIdx;
65   /// The CatchObj starts out life as an LLVM alloca, is turned into a frame
66   /// index, and after PEI, becomes a raw offset.
67   union {
68     const AllocaInst *Alloca;
69     int FrameIndex;
70   } CatchObj = {};
71   GlobalVariable *TypeDescriptor;
72   ValueOrMBB Handler;
73 };
74
75 struct WinEHTryBlockMapEntry {
76   int TryLow = -1;
77   int TryHigh = -1;
78   int CatchHigh = -1;
79   SmallVector<WinEHHandlerType, 1> HandlerArray;
80 };
81
82 enum class ClrHandlerType { Catch, Finally, Fault, Filter };
83
84 struct ClrEHUnwindMapEntry {
85   MBBOrBasicBlock Handler;
86   uint32_t TypeToken;
87   int Parent;
88   ClrHandlerType HandlerType;
89 };
90
91 struct WinEHFuncInfo {
92   DenseMap<const Instruction *, int> EHPadStateMap;
93   DenseMap<const CatchReturnInst *, const BasicBlock *>
94       CatchRetSuccessorColorMap;
95   DenseMap<MCSymbol *, std::pair<int, MCSymbol *>> InvokeToStateMap;
96   SmallVector<CxxUnwindMapEntry, 4> CxxUnwindMap;
97   SmallVector<WinEHTryBlockMapEntry, 4> TryBlockMap;
98   SmallVector<SEHUnwindMapEntry, 4> SEHUnwindMap;
99   SmallVector<ClrEHUnwindMapEntry, 4> ClrEHUnwindMap;
100   int UnwindHelpFrameIdx = INT_MAX;
101
102   int getLastStateNumber() const { return CxxUnwindMap.size() - 1; }
103
104   void addIPToStateRange(const BasicBlock *PadBB, MCSymbol *InvokeBegin,
105                          MCSymbol *InvokeEnd);
106
107   /// localescape index of the 32-bit EH registration node. Set by
108   /// WinEHStatePass and used indirectly by SEH filter functions of the parent.
109   int EHRegNodeEscapeIndex = INT_MAX;
110   const AllocaInst *EHRegNode = nullptr;
111   int EHRegNodeFrameIndex = INT_MAX;
112   int EHRegNodeEndOffset = INT_MAX;
113
114   WinEHFuncInfo() {}
115 };
116
117 /// Analyze the IR in ParentFn and it's handlers to build WinEHFuncInfo, which
118 /// describes the state numbers and tables used by __CxxFrameHandler3. This
119 /// analysis assumes that WinEHPrepare has already been run.
120 void calculateWinCXXEHStateNumbers(const Function *ParentFn,
121                                    WinEHFuncInfo &FuncInfo);
122
123 void calculateSEHStateNumbers(const Function *ParentFn,
124                               WinEHFuncInfo &FuncInfo);
125
126 void calculateClrEHStateNumbers(const Function *Fn, WinEHFuncInfo &FuncInfo);
127
128 void calculateCatchReturnSuccessorColors(const Function *Fn,
129                                          WinEHFuncInfo &FuncInfo);
130 }
131 #endif // LLVM_CODEGEN_WINEHFUNCINFO_H