[WinEH] Remove more dead code
[oota-llvm.git] / include / llvm / CodeGen / MachineModuleInfo.h
1 //===-- llvm/CodeGen/MachineModuleInfo.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 // Collect meta information for a module.  This information should be in a
11 // neutral form that can be used by different debugging and exception handling
12 // schemes.
13 //
14 // The organization of information is primarily clustered around the source
15 // compile units.  The main exception is source line correspondence where
16 // inlining may interleave code from various compile units.
17 //
18 // The following information can be retrieved from the MachineModuleInfo.
19 //
20 //  -- Source directories - Directories are uniqued based on their canonical
21 //     string and assigned a sequential numeric ID (base 1.)
22 //  -- Source files - Files are also uniqued based on their name and directory
23 //     ID.  A file ID is sequential number (base 1.)
24 //  -- Source line correspondence - A vector of file ID, line#, column# triples.
25 //     A DEBUG_LOCATION instruction is generated  by the DAG Legalizer
26 //     corresponding to each entry in the source line list.  This allows a debug
27 //     emitter to generate labels referenced by debug information tables.
28 //
29 //===----------------------------------------------------------------------===//
30
31 #ifndef LLVM_CODEGEN_MACHINEMODULEINFO_H
32 #define LLVM_CODEGEN_MACHINEMODULEINFO_H
33
34 #include "llvm/ADT/DenseMap.h"
35 #include "llvm/ADT/PointerIntPair.h"
36 #include "llvm/ADT/SmallPtrSet.h"
37 #include "llvm/ADT/SmallVector.h"
38 #include "llvm/Analysis/LibCallSemantics.h"
39 #include "llvm/IR/DebugLoc.h"
40 #include "llvm/IR/Metadata.h"
41 #include "llvm/IR/ValueHandle.h"
42 #include "llvm/MC/MCContext.h"
43 #include "llvm/MC/MachineLocation.h"
44 #include "llvm/Pass.h"
45 #include "llvm/Support/DataTypes.h"
46 #include "llvm/Support/Dwarf.h"
47
48 namespace llvm {
49
50 //===----------------------------------------------------------------------===//
51 // Forward declarations.
52 class Constant;
53 class GlobalVariable;
54 class BlockAddress;
55 class MDNode;
56 class MMIAddrLabelMap;
57 class MachineBasicBlock;
58 class MachineFunction;
59 class Module;
60 class PointerType;
61 class StructType;
62 struct WinEHFuncInfo;
63
64 struct SEHHandler {
65   // Filter or finally function. Null indicates a catch-all.
66   const Function *FilterOrFinally;
67
68   // Address of block to recover at. Null for a finally handler.
69   const BlockAddress *RecoverBA;
70 };
71
72 //===----------------------------------------------------------------------===//
73 /// LandingPadInfo - This structure is used to retain landing pad info for
74 /// the current function.
75 ///
76 struct LandingPadInfo {
77   MachineBasicBlock *LandingPadBlock;      // Landing pad block.
78   SmallVector<MCSymbol *, 1> BeginLabels;  // Labels prior to invoke.
79   SmallVector<MCSymbol *, 1> EndLabels;    // Labels after invoke.
80   SmallVector<SEHHandler, 1> SEHHandlers;  // SEH handlers active at this lpad.
81   MCSymbol *LandingPadLabel;               // Label at beginning of landing pad.
82   std::vector<int> TypeIds;               // List of type ids (filters negative).
83   int WinEHState;                         // WinEH specific state number.
84
85   explicit LandingPadInfo(MachineBasicBlock *MBB)
86       : LandingPadBlock(MBB), LandingPadLabel(nullptr),
87         WinEHState(-1) {}
88 };
89
90 //===----------------------------------------------------------------------===//
91 /// MachineModuleInfoImpl - This class can be derived from and used by targets
92 /// to hold private target-specific information for each Module.  Objects of
93 /// type are accessed/created with MMI::getInfo and destroyed when the
94 /// MachineModuleInfo is destroyed.
95 /// 
96 class MachineModuleInfoImpl {
97 public:
98   typedef PointerIntPair<MCSymbol*, 1, bool> StubValueTy;
99   virtual ~MachineModuleInfoImpl();
100   typedef std::vector<std::pair<MCSymbol*, StubValueTy> > SymbolListTy;
101 protected:
102
103   /// Return the entries from a DenseMap in a deterministic sorted orer.
104   /// Clears the map.
105   static SymbolListTy getSortedStubs(DenseMap<MCSymbol*, StubValueTy>&);
106 };
107
108 //===----------------------------------------------------------------------===//
109 /// MachineModuleInfo - This class contains meta information specific to a
110 /// module.  Queries can be made by different debugging and exception handling
111 /// schemes and reformated for specific use.
112 ///
113 class MachineModuleInfo : public ImmutablePass {
114   /// Context - This is the MCContext used for the entire code generator.
115   MCContext Context;
116
117   /// TheModule - This is the LLVM Module being worked on.
118   const Module *TheModule;
119
120   /// ObjFileMMI - This is the object-file-format-specific implementation of
121   /// MachineModuleInfoImpl, which lets targets accumulate whatever info they
122   /// want.
123   MachineModuleInfoImpl *ObjFileMMI;
124
125   /// List of moves done by a function's prolog.  Used to construct frame maps
126   /// by debug and exception handling consumers.
127   std::vector<MCCFIInstruction> FrameInstructions;
128
129   /// LandingPads - List of LandingPadInfo describing the landing pad
130   /// information in the current function.
131   std::vector<LandingPadInfo> LandingPads;
132
133   /// LPadToCallSiteMap - Map a landing pad's EH symbol to the call site
134   /// indexes.
135   DenseMap<MCSymbol*, SmallVector<unsigned, 4> > LPadToCallSiteMap;
136
137   /// CallSiteMap - Map of invoke call site index values to associated begin
138   /// EH_LABEL for the current function.
139   DenseMap<MCSymbol*, unsigned> CallSiteMap;
140
141   /// CurCallSite - The current call site index being processed, if any. 0 if
142   /// none.
143   unsigned CurCallSite;
144
145   /// TypeInfos - List of C++ TypeInfo used in the current function.
146   std::vector<const GlobalValue *> TypeInfos;
147
148   /// FilterIds - List of typeids encoding filters used in the current function.
149   std::vector<unsigned> FilterIds;
150
151   /// FilterEnds - List of the indices in FilterIds corresponding to filter
152   /// terminators.
153   std::vector<unsigned> FilterEnds;
154
155   /// Personalities - Vector of all personality functions ever seen. Used to
156   /// emit common EH frames.
157   std::vector<const Function *> Personalities;
158
159   /// AddrLabelSymbols - This map keeps track of which symbol is being used for
160   /// the specified basic block's address of label.
161   MMIAddrLabelMap *AddrLabelSymbols;
162
163   bool CallsEHReturn;
164   bool CallsUnwindInit;
165   bool HasEHFunclets;
166
167   /// DbgInfoAvailable - True if debugging information is available
168   /// in this module.
169   bool DbgInfoAvailable;
170
171   /// UsesVAFloatArgument - True if this module calls VarArg function with
172   /// floating-point arguments.  This is used to emit an undefined reference
173   /// to _fltused on Windows targets.
174   bool UsesVAFloatArgument;
175
176   /// UsesMorestackAddr - True if the module calls the __morestack function
177   /// indirectly, as is required under the large code model on x86. This is used
178   /// to emit a definition of a symbol, __morestack_addr, containing the
179   /// address. See comments in lib/Target/X86/X86FrameLowering.cpp for more
180   /// details.
181   bool UsesMorestackAddr;
182
183   EHPersonality PersonalityTypeCache;
184
185   DenseMap<const Function *, std::unique_ptr<WinEHFuncInfo>> FuncInfoMap;
186
187 public:
188   static char ID; // Pass identification, replacement for typeid
189
190   struct VariableDbgInfo {
191     const DILocalVariable *Var;
192     const DIExpression *Expr;
193     unsigned Slot;
194     const DILocation *Loc;
195
196     VariableDbgInfo(const DILocalVariable *Var, const DIExpression *Expr,
197                     unsigned Slot, const DILocation *Loc)
198         : Var(Var), Expr(Expr), Slot(Slot), Loc(Loc) {}
199   };
200   typedef SmallVector<VariableDbgInfo, 4> VariableDbgInfoMapTy;
201   VariableDbgInfoMapTy VariableDbgInfos;
202
203   MachineModuleInfo();  // DUMMY CONSTRUCTOR, DO NOT CALL.
204   // Real constructor.
205   MachineModuleInfo(const MCAsmInfo &MAI, const MCRegisterInfo &MRI,
206                     const MCObjectFileInfo *MOFI);
207   ~MachineModuleInfo() override;
208
209   // Initialization and Finalization
210   bool doInitialization(Module &) override;
211   bool doFinalization(Module &) override;
212
213   /// EndFunction - Discard function meta information.
214   ///
215   void EndFunction();
216
217   const MCContext &getContext() const { return Context; }
218   MCContext &getContext() { return Context; }
219
220   void setModule(const Module *M) { TheModule = M; }
221   const Module *getModule() const { return TheModule; }
222
223   WinEHFuncInfo &getWinEHFuncInfo(const Function *F);
224   bool hasWinEHFuncInfo(const Function *F) const {
225     return FuncInfoMap.count(F) > 0;
226   }
227
228   /// getInfo - Keep track of various per-function pieces of information for
229   /// backends that would like to do so.
230   ///
231   template<typename Ty>
232   Ty &getObjFileInfo() {
233     if (ObjFileMMI == nullptr)
234       ObjFileMMI = new Ty(*this);
235     return *static_cast<Ty*>(ObjFileMMI);
236   }
237
238   template<typename Ty>
239   const Ty &getObjFileInfo() const {
240     return const_cast<MachineModuleInfo*>(this)->getObjFileInfo<Ty>();
241   }
242
243   /// hasDebugInfo - Returns true if valid debug info is present.
244   ///
245   bool hasDebugInfo() const { return DbgInfoAvailable; }
246   void setDebugInfoAvailability(bool avail) { DbgInfoAvailable = avail; }
247
248   bool callsEHReturn() const { return CallsEHReturn; }
249   void setCallsEHReturn(bool b) { CallsEHReturn = b; }
250
251   bool callsUnwindInit() const { return CallsUnwindInit; }
252   void setCallsUnwindInit(bool b) { CallsUnwindInit = b; }
253
254   bool hasEHFunclets() const { return HasEHFunclets; }
255   void setHasEHFunclets(bool V) { HasEHFunclets = V; }
256
257   bool usesVAFloatArgument() const {
258     return UsesVAFloatArgument;
259   }
260
261   void setUsesVAFloatArgument(bool b) {
262     UsesVAFloatArgument = b;
263   }
264
265   bool usesMorestackAddr() const {
266     return UsesMorestackAddr;
267   }
268
269   void setUsesMorestackAddr(bool b) {
270     UsesMorestackAddr = b;
271   }
272
273   /// \brief Returns a reference to a list of cfi instructions in the current
274   /// function's prologue.  Used to construct frame maps for debug and exception
275   /// handling comsumers.
276   const std::vector<MCCFIInstruction> &getFrameInstructions() const {
277     return FrameInstructions;
278   }
279
280   unsigned LLVM_ATTRIBUTE_UNUSED_RESULT
281   addFrameInst(const MCCFIInstruction &Inst) {
282     FrameInstructions.push_back(Inst);
283     return FrameInstructions.size() - 1;
284   }
285
286   /// getAddrLabelSymbol - Return the symbol to be used for the specified basic
287   /// block when its address is taken.  This cannot be its normal LBB label
288   /// because the block may be accessed outside its containing function.
289   MCSymbol *getAddrLabelSymbol(const BasicBlock *BB) {
290     return getAddrLabelSymbolToEmit(BB).front();
291   }
292
293   /// getAddrLabelSymbolToEmit - Return the symbol to be used for the specified
294   /// basic block when its address is taken.  If other blocks were RAUW'd to
295   /// this one, we may have to emit them as well, return the whole set.
296   ArrayRef<MCSymbol *> getAddrLabelSymbolToEmit(const BasicBlock *BB);
297
298   /// takeDeletedSymbolsForFunction - If the specified function has had any
299   /// references to address-taken blocks generated, but the block got deleted,
300   /// return the symbol now so we can emit it.  This prevents emitting a
301   /// reference to a symbol that has no definition.
302   void takeDeletedSymbolsForFunction(const Function *F,
303                                      std::vector<MCSymbol*> &Result);
304
305
306   //===- EH ---------------------------------------------------------------===//
307
308   /// getOrCreateLandingPadInfo - Find or create an LandingPadInfo for the
309   /// specified MachineBasicBlock.
310   LandingPadInfo &getOrCreateLandingPadInfo(MachineBasicBlock *LandingPad);
311
312   /// addInvoke - Provide the begin and end labels of an invoke style call and
313   /// associate it with a try landing pad block.
314   void addInvoke(MachineBasicBlock *LandingPad,
315                  MCSymbol *BeginLabel, MCSymbol *EndLabel);
316
317   /// addLandingPad - Add a new panding pad.  Returns the label ID for the
318   /// landing pad entry.
319   MCSymbol *addLandingPad(MachineBasicBlock *LandingPad);
320
321   /// addPersonality - Provide the personality function for the exception
322   /// information.
323   void addPersonality(const Function *Personality);
324
325   void addWinEHState(MachineBasicBlock *LandingPad, int State);
326
327   /// getPersonalities - Return array of personality functions ever seen.
328   const std::vector<const Function *>& getPersonalities() const {
329     return Personalities;
330   }
331
332   /// addCatchTypeInfo - Provide the catch typeinfo for a landing pad.
333   ///
334   void addCatchTypeInfo(MachineBasicBlock *LandingPad,
335                         ArrayRef<const GlobalValue *> TyInfo);
336
337   /// addFilterTypeInfo - Provide the filter typeinfo for a landing pad.
338   ///
339   void addFilterTypeInfo(MachineBasicBlock *LandingPad,
340                          ArrayRef<const GlobalValue *> TyInfo);
341
342   /// addCleanup - Add a cleanup action for a landing pad.
343   ///
344   void addCleanup(MachineBasicBlock *LandingPad);
345
346   void addSEHCatchHandler(MachineBasicBlock *LandingPad, const Function *Filter,
347                           const BlockAddress *RecoverLabel);
348
349   void addSEHCleanupHandler(MachineBasicBlock *LandingPad,
350                             const Function *Cleanup);
351
352   /// getTypeIDFor - Return the type id for the specified typeinfo.  This is
353   /// function wide.
354   unsigned getTypeIDFor(const GlobalValue *TI);
355
356   /// getFilterIDFor - Return the id of the filter encoded by TyIds.  This is
357   /// function wide.
358   int getFilterIDFor(std::vector<unsigned> &TyIds);
359
360   /// TidyLandingPads - Remap landing pad labels and remove any deleted landing
361   /// pads.
362   void TidyLandingPads(DenseMap<MCSymbol*, uintptr_t> *LPMap = nullptr);
363
364   /// getLandingPads - Return a reference to the landing pad info for the
365   /// current function.
366   const std::vector<LandingPadInfo> &getLandingPads() const {
367     return LandingPads;
368   }
369
370   /// setCallSiteLandingPad - Map the landing pad's EH symbol to the call
371   /// site indexes.
372   void setCallSiteLandingPad(MCSymbol *Sym, ArrayRef<unsigned> Sites);
373
374   /// getCallSiteLandingPad - Get the call site indexes for a landing pad EH
375   /// symbol.
376   SmallVectorImpl<unsigned> &getCallSiteLandingPad(MCSymbol *Sym) {
377     assert(hasCallSiteLandingPad(Sym) &&
378            "missing call site number for landing pad!");
379     return LPadToCallSiteMap[Sym];
380   }
381
382   /// hasCallSiteLandingPad - Return true if the landing pad Eh symbol has an
383   /// associated call site.
384   bool hasCallSiteLandingPad(MCSymbol *Sym) {
385     return !LPadToCallSiteMap[Sym].empty();
386   }
387
388   /// setCallSiteBeginLabel - Map the begin label for a call site.
389   void setCallSiteBeginLabel(MCSymbol *BeginLabel, unsigned Site) {
390     CallSiteMap[BeginLabel] = Site;
391   }
392
393   /// getCallSiteBeginLabel - Get the call site number for a begin label.
394   unsigned getCallSiteBeginLabel(MCSymbol *BeginLabel) {
395     assert(hasCallSiteBeginLabel(BeginLabel) &&
396            "Missing call site number for EH_LABEL!");
397     return CallSiteMap[BeginLabel];
398   }
399
400   /// hasCallSiteBeginLabel - Return true if the begin label has a call site
401   /// number associated with it.
402   bool hasCallSiteBeginLabel(MCSymbol *BeginLabel) {
403     return CallSiteMap[BeginLabel] != 0;
404   }
405
406   /// setCurrentCallSite - Set the call site currently being processed.
407   void setCurrentCallSite(unsigned Site) { CurCallSite = Site; }
408
409   /// getCurrentCallSite - Get the call site currently being processed, if any.
410   /// return zero if none.
411   unsigned getCurrentCallSite() { return CurCallSite; }
412
413   /// getTypeInfos - Return a reference to the C++ typeinfo for the current
414   /// function.
415   const std::vector<const GlobalValue *> &getTypeInfos() const {
416     return TypeInfos;
417   }
418
419   /// getFilterIds - Return a reference to the typeids encoding filters used in
420   /// the current function.
421   const std::vector<unsigned> &getFilterIds() const {
422     return FilterIds;
423   }
424
425   /// setVariableDbgInfo - Collect information used to emit debugging
426   /// information of a variable.
427   void setVariableDbgInfo(const DILocalVariable *Var, const DIExpression *Expr,
428                           unsigned Slot, const DILocation *Loc) {
429     VariableDbgInfos.emplace_back(Var, Expr, Slot, Loc);
430   }
431
432   VariableDbgInfoMapTy &getVariableDbgInfo() { return VariableDbgInfos; }
433
434 }; // End class MachineModuleInfo
435
436 } // End llvm namespace
437
438 #endif