OSDN Git Service

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