DwarfDebug: Prevent DebugLocEntry merging from coalescing two different
[oota-llvm.git] / lib / CodeGen / AsmPrinter / DwarfDebug.h
1 //===-- llvm/CodeGen/DwarfDebug.h - Dwarf Debug Framework ------*- 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 // This file contains support for writing dwarf debug info into asm files.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef CODEGEN_ASMPRINTER_DWARFDEBUG_H__
15 #define CODEGEN_ASMPRINTER_DWARFDEBUG_H__
16
17 #include "AsmPrinterHandler.h"
18 #include "DIE.h"
19 #include "llvm/ADT/DenseMap.h"
20 #include "llvm/ADT/MapVector.h"
21 #include "llvm/ADT/SmallPtrSet.h"
22 #include "llvm/ADT/StringMap.h"
23 #include "llvm/ADT/FoldingSet.h"
24 #include "llvm/CodeGen/LexicalScopes.h"
25 #include "llvm/IR/DebugInfo.h"
26 #include "llvm/IR/DebugLoc.h"
27 #include "llvm/MC/MachineLocation.h"
28 #include "llvm/MC/MCDwarf.h"
29 #include "llvm/Support/Allocator.h"
30
31 namespace llvm {
32
33 class AsmPrinter;
34 class ByteStreamer;
35 class ConstantInt;
36 class ConstantFP;
37 class DwarfCompileUnit;
38 class DwarfDebug;
39 class DwarfTypeUnit;
40 class DwarfUnit;
41 class MachineModuleInfo;
42
43 //===----------------------------------------------------------------------===//
44 /// \brief This class is used to record source line correspondence.
45 class SrcLineInfo {
46   unsigned Line;     // Source line number.
47   unsigned Column;   // Source column.
48   unsigned SourceID; // Source ID number.
49   MCSymbol *Label;   // Label in code ID number.
50 public:
51   SrcLineInfo(unsigned L, unsigned C, unsigned S, MCSymbol *label)
52       : Line(L), Column(C), SourceID(S), Label(label) {}
53
54   // Accessors
55   unsigned getLine() const { return Line; }
56   unsigned getColumn() const { return Column; }
57   unsigned getSourceID() const { return SourceID; }
58   MCSymbol *getLabel() const { return Label; }
59 };
60
61 /// \brief This struct describes location entries emitted in the .debug_loc
62 /// section.
63 class DebugLocEntry {
64   // Begin and end symbols for the address range that this location is valid.
65   const MCSymbol *Begin;
66   const MCSymbol *End;
67
68   // Type of entry that this represents.
69   enum EntryType { E_Location, E_Integer, E_ConstantFP, E_ConstantInt };
70   enum EntryType EntryKind;
71
72   union {
73     int64_t Int;
74     const ConstantFP *CFP;
75     const ConstantInt *CIP;
76   } Constants;
77
78   // The location in the machine frame.
79   MachineLocation Loc;
80
81   // The variable to which this location entry corresponds.
82   const MDNode *Variable;
83
84   // The compile unit to which this location entry is referenced by.
85   const DwarfCompileUnit *Unit;
86
87 public:
88   DebugLocEntry() : Begin(0), End(0), Variable(0), Unit(0) {
89     Constants.Int = 0;
90   }
91   DebugLocEntry(const MCSymbol *B, const MCSymbol *E, MachineLocation &L,
92                 const MDNode *V, const DwarfCompileUnit *U)
93       : Begin(B), End(E), Loc(L), Variable(V), Unit(U) {
94     Constants.Int = 0;
95     EntryKind = E_Location;
96   }
97   DebugLocEntry(const MCSymbol *B, const MCSymbol *E, int64_t i,
98                 const DwarfCompileUnit *U)
99       : Begin(B), End(E), Variable(0), Unit(U) {
100     Constants.Int = i;
101     EntryKind = E_Integer;
102   }
103   DebugLocEntry(const MCSymbol *B, const MCSymbol *E, const ConstantFP *FPtr,
104                 const DwarfCompileUnit *U)
105       : Begin(B), End(E), Variable(0), Unit(U) {
106     Constants.CFP = FPtr;
107     EntryKind = E_ConstantFP;
108   }
109   DebugLocEntry(const MCSymbol *B, const MCSymbol *E, const ConstantInt *IPtr,
110                 const DwarfCompileUnit *U)
111       : Begin(B), End(E), Variable(0), Unit(U) {
112     Constants.CIP = IPtr;
113     EntryKind = E_ConstantInt;
114   }
115
116   /// \brief Empty entries are also used as a trigger to emit temp label. Such
117   /// labels are referenced is used to find debug_loc offset for a given DIE.
118   bool isEmpty() const { return Begin == 0 && End == 0; }
119   bool Merge(const DebugLocEntry &Next) {
120     if (Begin &&
121         Loc == Next.Loc &&
122         EntryKind == Next.EntryKind &&
123         (!isInt() || getInt() == Next.getInt()) &&
124         (!isConstantInt() || getConstantInt() == Next.getConstantInt()) &&
125         (!isConstantFP() || getConstantFP() == Next.getConstantFP()) &&
126         End == Next.Begin) {
127     End = Next.End;
128     return true;
129   }
130   return false;
131   }
132   bool isLocation() const { return EntryKind == E_Location; }
133   bool isInt() const { return EntryKind == E_Integer; }
134   bool isConstantFP() const { return EntryKind == E_ConstantFP; }
135   bool isConstantInt() const { return EntryKind == E_ConstantInt; }
136   int64_t getInt() const { return Constants.Int; }
137   const ConstantFP *getConstantFP() const { return Constants.CFP; }
138   const ConstantInt *getConstantInt() const { return Constants.CIP; }
139   const MDNode *getVariable() const { return Variable; }
140   const MCSymbol *getBeginSym() const { return Begin; }
141   const MCSymbol *getEndSym() const { return End; }
142   const DwarfCompileUnit *getCU() const { return Unit; }
143   MachineLocation getLoc() const { return Loc; }
144 };
145
146 //===----------------------------------------------------------------------===//
147 /// \brief This class is used to track local variable information.
148 class DbgVariable {
149   DIVariable Var;             // Variable Descriptor.
150   DIE *TheDIE;                // Variable DIE.
151   unsigned DotDebugLocOffset; // Offset in DotDebugLocEntries.
152   DbgVariable *AbsVar;        // Corresponding Abstract variable, if any.
153   const MachineInstr *MInsn;  // DBG_VALUE instruction of the variable.
154   int FrameIndex;
155   DwarfDebug *DD;
156
157 public:
158   // AbsVar may be NULL.
159   DbgVariable(DIVariable V, DbgVariable *AV, DwarfDebug *DD)
160       : Var(V), TheDIE(0), DotDebugLocOffset(~0U), AbsVar(AV), MInsn(0),
161         FrameIndex(~0), DD(DD) {}
162
163   // Accessors.
164   DIVariable getVariable() const { return Var; }
165   void setDIE(DIE *D) { TheDIE = D; }
166   DIE *getDIE() const { return TheDIE; }
167   void setDotDebugLocOffset(unsigned O) { DotDebugLocOffset = O; }
168   unsigned getDotDebugLocOffset() const { return DotDebugLocOffset; }
169   StringRef getName() const { return Var.getName(); }
170   DbgVariable *getAbstractVariable() const { return AbsVar; }
171   const MachineInstr *getMInsn() const { return MInsn; }
172   void setMInsn(const MachineInstr *M) { MInsn = M; }
173   int getFrameIndex() const { return FrameIndex; }
174   void setFrameIndex(int FI) { FrameIndex = FI; }
175   // Translate tag to proper Dwarf tag.
176   uint16_t getTag() const {
177     if (Var.getTag() == dwarf::DW_TAG_arg_variable)
178       return dwarf::DW_TAG_formal_parameter;
179
180     return dwarf::DW_TAG_variable;
181   }
182   /// \brief Return true if DbgVariable is artificial.
183   bool isArtificial() const {
184     if (Var.isArtificial())
185       return true;
186     if (getType().isArtificial())
187       return true;
188     return false;
189   }
190
191   bool isObjectPointer() const {
192     if (Var.isObjectPointer())
193       return true;
194     if (getType().isObjectPointer())
195       return true;
196     return false;
197   }
198
199   bool variableHasComplexAddress() const {
200     assert(Var.isVariable() && "Invalid complex DbgVariable!");
201     return Var.hasComplexAddress();
202   }
203   bool isBlockByrefVariable() const;
204   unsigned getNumAddrElements() const {
205     assert(Var.isVariable() && "Invalid complex DbgVariable!");
206     return Var.getNumAddrElements();
207   }
208   uint64_t getAddrElement(unsigned i) const { return Var.getAddrElement(i); }
209   DIType getType() const;
210
211 private:
212   /// resolve - Look in the DwarfDebug map for the MDNode that
213   /// corresponds to the reference.
214   template <typename T> T resolve(DIRef<T> Ref) const;
215 };
216
217 /// \brief Collects and handles information specific to a particular
218 /// collection of units. This collection represents all of the units
219 /// that will be ultimately output into a single object file.
220 class DwarfFile {
221   // Target of Dwarf emission, used for sizing of abbreviations.
222   AsmPrinter *Asm;
223
224   // Used to uniquely define abbreviations.
225   FoldingSet<DIEAbbrev> AbbreviationsSet;
226
227   // A list of all the unique abbreviations in use.
228   std::vector<DIEAbbrev *> Abbreviations;
229
230   // A pointer to all units in the section.
231   SmallVector<DwarfUnit *, 1> CUs;
232
233   // Collection of strings for this unit and assorted symbols.
234   // A String->Symbol mapping of strings used by indirect
235   // references.
236   typedef StringMap<std::pair<MCSymbol *, unsigned>, BumpPtrAllocator &>
237   StrPool;
238   StrPool StringPool;
239   unsigned NextStringPoolNumber;
240   std::string StringPref;
241
242   struct AddressPoolEntry {
243     unsigned Number;
244     bool TLS;
245     AddressPoolEntry(unsigned Number, bool TLS) : Number(Number), TLS(TLS) {}
246   };
247   // Collection of addresses for this unit and assorted labels.
248   // A Symbol->unsigned mapping of addresses used by indirect
249   // references.
250   typedef DenseMap<const MCSymbol *, AddressPoolEntry> AddrPool;
251   AddrPool AddressPool;
252   unsigned NextAddrPoolNumber;
253
254 public:
255   DwarfFile(AsmPrinter *AP, const char *Pref, BumpPtrAllocator &DA)
256       : Asm(AP), StringPool(DA), NextStringPoolNumber(0), StringPref(Pref),
257         AddressPool(), NextAddrPoolNumber(0) {}
258
259   ~DwarfFile();
260
261   const SmallVectorImpl<DwarfUnit *> &getUnits() { return CUs; }
262
263   /// \brief Compute the size and offset of a DIE given an incoming Offset.
264   unsigned computeSizeAndOffset(DIE *Die, unsigned Offset);
265
266   /// \brief Compute the size and offset of all the DIEs.
267   void computeSizeAndOffsets();
268
269   /// \brief Define a unique number for the abbreviation.
270   void assignAbbrevNumber(DIEAbbrev &Abbrev);
271
272   /// \brief Add a unit to the list of CUs.
273   void addUnit(DwarfUnit *CU) { CUs.push_back(CU); }
274
275   /// \brief Emit all of the units to the section listed with the given
276   /// abbreviation section.
277   void emitUnits(DwarfDebug *DD, const MCSymbol *ASectionSym);
278
279   /// \brief Emit a set of abbreviations to the specific section.
280   void emitAbbrevs(const MCSection *);
281
282   /// \brief Emit all of the strings to the section given.
283   void emitStrings(const MCSection *StrSection, const MCSection *OffsetSection,
284                    const MCSymbol *StrSecSym);
285
286   /// \brief Emit all of the addresses to the section given.
287   void emitAddresses(const MCSection *AddrSection);
288
289   /// \brief Returns the entry into the start of the pool.
290   MCSymbol *getStringPoolSym();
291
292   /// \brief Returns an entry into the string pool with the given
293   /// string text.
294   MCSymbol *getStringPoolEntry(StringRef Str);
295
296   /// \brief Returns the index into the string pool with the given
297   /// string text.
298   unsigned getStringPoolIndex(StringRef Str);
299
300   /// \brief Returns the string pool.
301   StrPool *getStringPool() { return &StringPool; }
302
303   /// \brief Returns the index into the address pool with the given
304   /// label/symbol.
305   unsigned getAddrPoolIndex(const MCSymbol *Sym, bool TLS = false);
306
307   /// \brief Returns the address pool.
308   AddrPool *getAddrPool() { return &AddressPool; }
309 };
310
311 /// \brief Helper used to pair up a symbol and its DWARF compile unit.
312 struct SymbolCU {
313   SymbolCU(DwarfCompileUnit *CU, const MCSymbol *Sym) : Sym(Sym), CU(CU) {}
314   const MCSymbol *Sym;
315   DwarfCompileUnit *CU;
316 };
317
318 /// \brief Collects and handles dwarf debug information.
319 class DwarfDebug : public AsmPrinterHandler {
320   // Target of Dwarf emission.
321   AsmPrinter *Asm;
322
323   // Collected machine module information.
324   MachineModuleInfo *MMI;
325
326   // All DIEValues are allocated through this allocator.
327   BumpPtrAllocator DIEValueAllocator;
328
329   // Handle to the compile unit used for the inline extension handling,
330   // this is just so that the DIEValue allocator has a place to store
331   // the particular elements.
332   // FIXME: Store these off of DwarfDebug instead?
333   DwarfCompileUnit *FirstCU;
334
335   // Maps MDNode with its corresponding DwarfCompileUnit.
336   MapVector<const MDNode *, DwarfCompileUnit *> CUMap;
337
338   // Maps subprogram MDNode with its corresponding DwarfCompileUnit.
339   DenseMap<const MDNode *, DwarfCompileUnit *> SPMap;
340
341   // Maps a CU DIE with its corresponding DwarfCompileUnit.
342   DenseMap<const DIE *, DwarfCompileUnit *> CUDieMap;
343
344   /// Maps MDNodes for type system with the corresponding DIEs. These DIEs can
345   /// be shared across CUs, that is why we keep the map here instead
346   /// of in DwarfCompileUnit.
347   DenseMap<const MDNode *, DIE *> MDTypeNodeToDieMap;
348
349   // List of all labels used in aranges generation.
350   std::vector<SymbolCU> ArangeLabels;
351
352   // Size of each symbol emitted (for those symbols that have a specific size).
353   DenseMap<const MCSymbol *, uint64_t> SymSize;
354
355   // Provides a unique id per text section.
356   typedef DenseMap<const MCSection *, SmallVector<SymbolCU, 8> > SectionMapType;
357   SectionMapType SectionMap;
358
359   // List of arguments for current function.
360   SmallVector<DbgVariable *, 8> CurrentFnArguments;
361
362   LexicalScopes LScopes;
363
364   // Collection of abstract subprogram DIEs.
365   DenseMap<const MDNode *, DIE *> AbstractSPDies;
366
367   // Collection of dbg variables of a scope.
368   typedef DenseMap<LexicalScope *, SmallVector<DbgVariable *, 8> >
369   ScopeVariablesMap;
370   ScopeVariablesMap ScopeVariables;
371
372   // Collection of abstract variables.
373   DenseMap<const MDNode *, DbgVariable *> AbstractVariables;
374
375   // Collection of DebugLocEntry.
376   SmallVector<SmallVector<DebugLocEntry, 4>, 4> DotDebugLocEntries;
377
378   // Collection of subprogram DIEs that are marked (at the end of the module)
379   // as DW_AT_inline.
380   SmallPtrSet<DIE *, 4> InlinedSubprogramDIEs;
381
382   // This is a collection of subprogram MDNodes that are processed to
383   // create DIEs.
384   SmallPtrSet<const MDNode *, 16> ProcessedSPNodes;
385
386   // Maps instruction with label emitted before instruction.
387   DenseMap<const MachineInstr *, MCSymbol *> LabelsBeforeInsn;
388
389   // Maps instruction with label emitted after instruction.
390   DenseMap<const MachineInstr *, MCSymbol *> LabelsAfterInsn;
391
392   // Every user variable mentioned by a DBG_VALUE instruction in order of
393   // appearance.
394   SmallVector<const MDNode *, 8> UserVariables;
395
396   // For each user variable, keep a list of DBG_VALUE instructions in order.
397   // The list can also contain normal instructions that clobber the previous
398   // DBG_VALUE.
399   typedef DenseMap<const MDNode *, SmallVector<const MachineInstr *, 4> >
400   DbgValueHistoryMap;
401   DbgValueHistoryMap DbgValues;
402
403   // Previous instruction's location information. This is used to determine
404   // label location to indicate scope boundries in dwarf debug info.
405   DebugLoc PrevInstLoc;
406   MCSymbol *PrevLabel;
407
408   // This location indicates end of function prologue and beginning of function
409   // body.
410   DebugLoc PrologEndLoc;
411
412   // If nonnull, stores the current machine function we're processing.
413   const MachineFunction *CurFn;
414
415   // If nonnull, stores the current machine instruction we're processing.
416   const MachineInstr *CurMI;
417
418   // If nonnull, stores the section that the previous function was allocated to
419   // emitting.
420   const MCSection *PrevSection;
421
422   // If nonnull, stores the CU in which the previous subprogram was contained.
423   const DwarfCompileUnit *PrevCU;
424
425   // Section Symbols: these are assembler temporary labels that are emitted at
426   // the beginning of each supported dwarf section.  These are used to form
427   // section offsets and are created by EmitSectionLabels.
428   MCSymbol *DwarfInfoSectionSym, *DwarfAbbrevSectionSym;
429   MCSymbol *DwarfStrSectionSym, *TextSectionSym, *DwarfDebugRangeSectionSym;
430   MCSymbol *DwarfDebugLocSectionSym, *DwarfLineSectionSym, *DwarfAddrSectionSym;
431   MCSymbol *FunctionBeginSym, *FunctionEndSym;
432   MCSymbol *DwarfInfoDWOSectionSym, *DwarfAbbrevDWOSectionSym;
433   MCSymbol *DwarfStrDWOSectionSym;
434   MCSymbol *DwarfGnuPubNamesSectionSym, *DwarfGnuPubTypesSectionSym;
435
436   // As an optimization, there is no need to emit an entry in the directory
437   // table for the same directory as DW_AT_comp_dir.
438   StringRef CompilationDir;
439
440   // Counter for assigning globally unique IDs for ranges.
441   unsigned GlobalRangeCount;
442
443   // Holder for the file specific debug information.
444   DwarfFile InfoHolder;
445
446   // Holders for the various debug information flags that we might need to
447   // have exposed. See accessor functions below for description.
448
449   // Holder for imported entities.
450   typedef SmallVector<std::pair<const MDNode *, const MDNode *>, 32>
451   ImportedEntityMap;
452   ImportedEntityMap ScopesWithImportedEntities;
453
454   // Map from MDNodes for user-defined types to the type units that describe
455   // them.
456   DenseMap<const MDNode *, const DwarfTypeUnit *> DwarfTypeUnits;
457
458   // Whether to emit the pubnames/pubtypes sections.
459   bool HasDwarfPubSections;
460
461   // Whether or not to use AT_ranges for compilation units.
462   bool HasCURanges;
463
464   // Whether we emitted a function into a section other than the default
465   // text.
466   bool UsedNonDefaultText;
467
468   // Version of dwarf we're emitting.
469   unsigned DwarfVersion;
470
471   // Maps from a type identifier to the actual MDNode.
472   DITypeIdentifierMap TypeIdentifierMap;
473
474   // DWARF5 Experimental Options
475   bool HasDwarfAccelTables;
476   bool HasSplitDwarf;
477
478   // Separated Dwarf Variables
479   // In general these will all be for bits that are left in the
480   // original object file, rather than things that are meant
481   // to be in the .dwo sections.
482
483   // Holder for the skeleton information.
484   DwarfFile SkeletonHolder;
485
486   /// Store file names for type units under fission in a line table header that
487   /// will be emitted into debug_line.dwo.
488   // FIXME: replace this with a map from comp_dir to table so that we can emit
489   // multiple tables during LTO each of which uses directory 0, referencing the
490   // comp_dir of all the type units that use it.
491   MCDwarfDwoLineTable SplitTypeUnitFileTable;
492
493   // True iff there are multiple CUs in this module.
494   bool SingleCU;
495
496   MCDwarfDwoLineTable *getDwoLineTable(const DwarfCompileUnit &);
497
498   void addScopeVariable(LexicalScope *LS, DbgVariable *Var);
499
500   const SmallVectorImpl<DwarfUnit *> &getUnits() {
501     return InfoHolder.getUnits();
502   }
503
504   /// \brief Find abstract variable associated with Var.
505   DbgVariable *findAbstractVariable(DIVariable &Var, DebugLoc Loc);
506
507   /// \brief Find DIE for the given subprogram and attach appropriate
508   /// DW_AT_low_pc and DW_AT_high_pc attributes. If there are global
509   /// variables in this scope then create and insert DIEs for these
510   /// variables.
511   DIE *updateSubprogramScopeDIE(DwarfCompileUnit *SPCU, DISubprogram SP);
512
513   /// \brief A helper function to check whether the DIE for a given Scope is
514   /// going to be null.
515   bool isLexicalScopeDIENull(LexicalScope *Scope);
516
517   /// \brief A helper function to construct a RangeSpanList for a given
518   /// lexical scope.
519   void addScopeRangeList(DwarfCompileUnit *TheCU, DIE *ScopeDIE,
520                          const SmallVectorImpl<InsnRange> &Range);
521
522   /// \brief Construct new DW_TAG_lexical_block for this scope and
523   /// attach DW_AT_low_pc/DW_AT_high_pc labels.
524   DIE *constructLexicalScopeDIE(DwarfCompileUnit *TheCU, LexicalScope *Scope);
525
526   /// \brief This scope represents inlined body of a function. Construct
527   /// DIE to represent this concrete inlined copy of the function.
528   DIE *constructInlinedScopeDIE(DwarfCompileUnit *TheCU, LexicalScope *Scope);
529
530   /// \brief Construct a DIE for this scope.
531   DIE *constructScopeDIE(DwarfCompileUnit *TheCU, LexicalScope *Scope);
532   /// A helper function to create children of a Scope DIE.
533   DIE *createScopeChildrenDIE(DwarfCompileUnit *TheCU, LexicalScope *Scope,
534                               SmallVectorImpl<DIE *> &Children);
535
536   /// \brief Emit initial Dwarf sections with a label at the start of each one.
537   void emitSectionLabels();
538
539   /// \brief Compute the size and offset of a DIE given an incoming Offset.
540   unsigned computeSizeAndOffset(DIE *Die, unsigned Offset);
541
542   /// \brief Compute the size and offset of all the DIEs.
543   void computeSizeAndOffsets();
544
545   /// \brief Attach DW_AT_inline attribute with inlined subprogram DIEs.
546   void computeInlinedDIEs();
547
548   /// \brief Collect info for variables that were optimized out.
549   void collectDeadVariables();
550
551   /// \brief Finish off debug information after all functions have been
552   /// processed.
553   void finalizeModuleInfo();
554
555   /// \brief Emit labels to close any remaining sections that have been left
556   /// open.
557   void endSections();
558
559   /// \brief Emit the debug info section.
560   void emitDebugInfo();
561
562   /// \brief Emit the abbreviation section.
563   void emitAbbreviations();
564
565   /// \brief Emit the last address of the section and the end of
566   /// the line matrix.
567   void emitEndOfLineMatrix(unsigned SectionEnd);
568
569   /// \brief Emit visible names into a hashed accelerator table section.
570   void emitAccelNames();
571
572   /// \brief Emit objective C classes and categories into a hashed
573   /// accelerator table section.
574   void emitAccelObjC();
575
576   /// \brief Emit namespace dies into a hashed accelerator table.
577   void emitAccelNamespaces();
578
579   /// \brief Emit type dies into a hashed accelerator table.
580   void emitAccelTypes();
581
582   /// \brief Emit visible names into a debug pubnames section.
583   /// \param GnuStyle determines whether or not we want to emit
584   /// additional information into the table ala newer gcc for gdb
585   /// index.
586   void emitDebugPubNames(bool GnuStyle = false);
587
588   /// \brief Emit visible types into a debug pubtypes section.
589   /// \param GnuStyle determines whether or not we want to emit
590   /// additional information into the table ala newer gcc for gdb
591   /// index.
592   void emitDebugPubTypes(bool GnuStyle = false);
593
594   void
595   emitDebugPubSection(bool GnuStyle, const MCSection *PSec, StringRef Name,
596                       const StringMap<const DIE *> &(DwarfUnit::*Accessor)()
597                       const);
598
599   /// \brief Emit visible names into a debug str section.
600   void emitDebugStr();
601
602   /// \brief Emit visible names into a debug loc section.
603   void emitDebugLoc();
604
605   /// \brief Emit visible names into a debug aranges section.
606   void emitDebugARanges();
607
608   /// \brief Emit visible names into a debug ranges section.
609   void emitDebugRanges();
610
611   /// \brief Emit inline info using custom format.
612   void emitDebugInlineInfo();
613
614   /// DWARF 5 Experimental Split Dwarf Emitters
615
616   /// \brief Initialize common features of skeleton units.
617   void initSkeletonUnit(const DwarfUnit *U, DIE *Die, DwarfUnit *NewU);
618
619   /// \brief Construct the split debug info compile unit for the debug info
620   /// section.
621   DwarfCompileUnit *constructSkeletonCU(const DwarfCompileUnit *CU);
622
623   /// \brief Construct the split debug info compile unit for the debug info
624   /// section.
625   DwarfTypeUnit *constructSkeletonTU(DwarfTypeUnit *TU);
626
627   /// \brief Emit the debug info dwo section.
628   void emitDebugInfoDWO();
629
630   /// \brief Emit the debug abbrev dwo section.
631   void emitDebugAbbrevDWO();
632
633   /// \brief Emit the debug line dwo section.
634   void emitDebugLineDWO();
635
636   /// \brief Emit the debug str dwo section.
637   void emitDebugStrDWO();
638
639   /// Flags to let the linker know we have emitted new style pubnames. Only
640   /// emit it here if we don't have a skeleton CU for split dwarf.
641   void addGnuPubAttributes(DwarfUnit *U, DIE *D) const;
642
643   /// \brief Create new DwarfCompileUnit for the given metadata node with tag
644   /// DW_TAG_compile_unit.
645   DwarfCompileUnit *constructDwarfCompileUnit(DICompileUnit DIUnit);
646
647   /// \brief Construct subprogram DIE.
648   void constructSubprogramDIE(DwarfCompileUnit *TheCU, const MDNode *N);
649
650   /// \brief Construct imported_module or imported_declaration DIE.
651   void constructImportedEntityDIE(DwarfCompileUnit *TheCU, const MDNode *N);
652
653   /// \brief Construct import_module DIE.
654   void constructImportedEntityDIE(DwarfCompileUnit *TheCU, const MDNode *N,
655                                   DIE *Context);
656
657   /// \brief Construct import_module DIE.
658   void constructImportedEntityDIE(DwarfCompileUnit *TheCU,
659                                   const DIImportedEntity &Module, DIE *Context);
660
661   /// \brief Register a source line with debug info. Returns the unique
662   /// label that was emitted and which provides correspondence to the
663   /// source line list.
664   void recordSourceLine(unsigned Line, unsigned Col, const MDNode *Scope,
665                         unsigned Flags);
666
667   /// \brief Indentify instructions that are marking the beginning of or
668   /// ending of a scope.
669   void identifyScopeMarkers();
670
671   /// \brief If Var is an current function argument that add it in
672   /// CurrentFnArguments list.
673   bool addCurrentFnArgument(DbgVariable *Var, LexicalScope *Scope);
674
675   /// \brief Populate LexicalScope entries with variables' info.
676   void collectVariableInfo(SmallPtrSet<const MDNode *, 16> &ProcessedVars);
677
678   /// \brief Collect variable information from the side table maintained
679   /// by MMI.
680   void collectVariableInfoFromMMITable(SmallPtrSet<const MDNode *, 16> &P);
681
682   /// \brief Ensure that a label will be emitted before MI.
683   void requestLabelBeforeInsn(const MachineInstr *MI) {
684     LabelsBeforeInsn.insert(std::make_pair(MI, (MCSymbol *)0));
685   }
686
687   /// \brief Return Label preceding the instruction.
688   MCSymbol *getLabelBeforeInsn(const MachineInstr *MI);
689
690   /// \brief Ensure that a label will be emitted after MI.
691   void requestLabelAfterInsn(const MachineInstr *MI) {
692     LabelsAfterInsn.insert(std::make_pair(MI, (MCSymbol *)0));
693   }
694
695   /// \brief Return Label immediately following the instruction.
696   MCSymbol *getLabelAfterInsn(const MachineInstr *MI);
697
698   void attachLowHighPC(DwarfCompileUnit *Unit, DIE *D, MCSymbol *Begin,
699                        MCSymbol *End);
700
701 public:
702   //===--------------------------------------------------------------------===//
703   // Main entry points.
704   //
705   DwarfDebug(AsmPrinter *A, Module *M);
706
707   void insertDIE(const MDNode *TypeMD, DIE *Die) {
708     MDTypeNodeToDieMap.insert(std::make_pair(TypeMD, Die));
709   }
710   DIE *getDIE(const MDNode *TypeMD) {
711     return MDTypeNodeToDieMap.lookup(TypeMD);
712   }
713
714   /// \brief Emit all Dwarf sections that should come prior to the
715   /// content.
716   void beginModule();
717
718   /// \brief Emit all Dwarf sections that should come after the content.
719   void endModule() override;
720
721   /// \brief Gather pre-function debug information.
722   void beginFunction(const MachineFunction *MF) override;
723
724   /// \brief Gather and emit post-function debug information.
725   void endFunction(const MachineFunction *MF) override;
726
727   /// \brief Process beginning of an instruction.
728   void beginInstruction(const MachineInstr *MI) override;
729
730   /// \brief Process end of an instruction.
731   void endInstruction() override;
732
733   /// \brief Add a DIE to the set of types that we're going to pull into
734   /// type units.
735   void addDwarfTypeUnitType(DwarfCompileUnit &CU, StringRef Identifier,
736                             DIE *Die, DICompositeType CTy);
737
738   /// \brief Add a label so that arange data can be generated for it.
739   void addArangeLabel(SymbolCU SCU) { ArangeLabels.push_back(SCU); }
740
741   /// \brief For symbols that have a size designated (e.g. common symbols),
742   /// this tracks that size.
743   void setSymbolSize(const MCSymbol *Sym, uint64_t Size) override {
744     SymSize[Sym] = Size;
745   }
746
747   /// \brief Recursively Emits a debug information entry.
748   void emitDIE(DIE *Die);
749
750   // Experimental DWARF5 features.
751
752   /// \brief Returns whether or not to emit tables that dwarf consumers can
753   /// use to accelerate lookup.
754   bool useDwarfAccelTables() const { return HasDwarfAccelTables; }
755
756   /// \brief Returns whether or not to change the current debug info for the
757   /// split dwarf proposal support.
758   bool useSplitDwarf() const { return HasSplitDwarf; }
759
760   /// Returns the Dwarf Version.
761   unsigned getDwarfVersion() const { return DwarfVersion; }
762
763   /// Returns the section symbol for the .debug_loc section.
764   MCSymbol *getDebugLocSym() const { return DwarfDebugLocSectionSym; }
765
766   /// Returns the previous section that was emitted into.
767   const MCSection *getPrevSection() const { return PrevSection; }
768
769   /// Returns the previous CU that was being updated
770   const DwarfCompileUnit *getPrevCU() const { return PrevCU; }
771
772   /// Returns the entries for the .debug_loc section.
773   const SmallVectorImpl<SmallVector<DebugLocEntry, 4>> &
774   getDebugLocEntries() const {
775     return DotDebugLocEntries;
776   }
777
778   /// \brief Emit an entry for the debug loc section. This can be used to
779   /// handle an entry that's going to be emitted into the debug loc section.
780   void emitDebugLocEntry(ByteStreamer &Streamer, const DebugLocEntry &Entry);
781
782   /// Emit the location for a debug loc entry, including the size header.
783   void emitDebugLocEntryLocation(const DebugLocEntry &Entry);
784
785   /// Find the MDNode for the given reference.
786   template <typename T> T resolve(DIRef<T> Ref) const {
787     return Ref.resolve(TypeIdentifierMap);
788   }
789
790   /// \brief Return the TypeIdentifierMap.
791   const DITypeIdentifierMap &getTypeIdentifierMap() const {
792     return TypeIdentifierMap;
793   }
794
795   /// Find the DwarfCompileUnit for the given CU Die.
796   DwarfCompileUnit *lookupUnit(const DIE *CU) const {
797     return CUDieMap.lookup(CU);
798   }
799   /// isSubprogramContext - Return true if Context is either a subprogram
800   /// or another context nested inside a subprogram.
801   bool isSubprogramContext(const MDNode *Context);
802 };
803 } // End of namespace llvm
804
805 #endif