Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/CodeGen/AsmPrinter/DwarfCompileUnit.h
Line
Count
Source
1
//===- llvm/CodeGen/DwarfCompileUnit.h - Dwarf Compile Unit -----*- C++ -*-===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
//
9
// This file contains support for writing dwarf compile unit.
10
//
11
//===----------------------------------------------------------------------===//
12
13
#ifndef LLVM_LIB_CODEGEN_ASMPRINTER_DWARFCOMPILEUNIT_H
14
#define LLVM_LIB_CODEGEN_ASMPRINTER_DWARFCOMPILEUNIT_H
15
16
#include "DwarfDebug.h"
17
#include "DwarfUnit.h"
18
#include "llvm/ADT/ArrayRef.h"
19
#include "llvm/ADT/DenseMap.h"
20
#include "llvm/ADT/SmallVector.h"
21
#include "llvm/ADT/StringMap.h"
22
#include "llvm/ADT/StringRef.h"
23
#include "llvm/BinaryFormat/Dwarf.h"
24
#include "llvm/CodeGen/DbgEntityHistoryCalculator.h"
25
#include "llvm/CodeGen/DIE.h"
26
#include "llvm/CodeGen/LexicalScopes.h"
27
#include "llvm/IR/DebugInfoMetadata.h"
28
#include "llvm/Support/Casting.h"
29
#include <algorithm>
30
#include <cassert>
31
#include <cstdint>
32
#include <memory>
33
34
namespace llvm {
35
36
class AsmPrinter;
37
class DwarfFile;
38
class GlobalVariable;
39
class MCExpr;
40
class MCSymbol;
41
class MDNode;
42
43
class DwarfCompileUnit final : public DwarfUnit {
44
  /// A numeric ID unique among all CUs in the module
45
  unsigned UniqueID;
46
  bool HasRangeLists = false;
47
48
  /// The attribute index of DW_AT_stmt_list in the compile unit DIE, avoiding
49
  /// the need to search for it in applyStmtList.
50
  DIE::value_iterator StmtListValue;
51
52
  /// Skeleton unit associated with this unit.
53
  DwarfCompileUnit *Skeleton = nullptr;
54
55
  /// The start of the unit within its section.
56
  MCSymbol *LabelBegin;
57
58
  /// The start of the unit macro info within macro section.
59
  MCSymbol *MacroLabelBegin;
60
61
  using ImportedEntityList = SmallVector<const MDNode *, 8>;
62
  using ImportedEntityMap = DenseMap<const MDNode *, ImportedEntityList>;
63
64
  ImportedEntityMap ImportedEntities;
65
66
  /// GlobalNames - A map of globally visible named entities for this unit.
67
  StringMap<const DIE *> GlobalNames;
68
69
  /// GlobalTypes - A map of globally visible types for this unit.
70
  StringMap<const DIE *> GlobalTypes;
71
72
  // List of ranges for a given compile unit.
73
  SmallVector<RangeSpan, 2> CURanges;
74
75
  // The base address of this unit, if any. Used for relative references in
76
  // ranges/locs.
77
  const MCSymbol *BaseAddress = nullptr;
78
79
  DenseMap<const MDNode *, DIE *> AbstractSPDies;
80
  DenseMap<const DINode *, std::unique_ptr<DbgEntity>> AbstractEntities;
81
82
  /// DWO ID for correlating skeleton and split units.
83
  uint64_t DWOId = 0;
84
85
  /// Construct a DIE for the given DbgVariable without initializing the
86
  /// DbgVariable's DIE reference.
87
  DIE *constructVariableDIEImpl(const DbgVariable &DV, bool Abstract);
88
89
  bool isDwoUnit() const override;
90
91
480k
  DenseMap<const MDNode *, DIE *> &getAbstractSPDies() {
92
480k
    if (isDwoUnit() && 
!DD->shareAcrossDWOCUs()70
)
93
64
      return AbstractSPDies;
94
480k
    return DU->getAbstractSPDies();
95
480k
  }
96
97
2.62k
  DenseMap<const DINode *, std::unique_ptr<DbgEntity>> &getAbstractEntities() {
98
2.62k
    if (isDwoUnit() && 
!DD->shareAcrossDWOCUs()95
)
99
86
      return AbstractEntities;
100
2.53k
    return DU->getAbstractEntities();
101
2.53k
  }
102
103
  void finishNonUnitTypeDIE(DIE& D, const DICompositeType *CTy) override;
104
105
public:
106
  DwarfCompileUnit(unsigned UID, const DICompileUnit *Node, AsmPrinter *A,
107
                   DwarfDebug *DW, DwarfFile *DWU);
108
109
65
  bool hasRangeLists() const { return HasRangeLists; }
110
1.28M
  unsigned getUniqueID() const { return UniqueID; }
111
112
245k
  DwarfCompileUnit *getSkeleton() const {
113
245k
    return Skeleton;
114
245k
  }
115
116
  bool includeMinimalInlineScopes() const;
117
118
  void initStmtList();
119
120
  /// Apply the DW_AT_stmt_list from this compile unit to the specified DIE.
121
  void applyStmtList(DIE &D);
122
123
  /// A pair of GlobalVariable and DIExpression.
124
  struct GlobalExpr {
125
    const GlobalVariable *Var;
126
    const DIExpression *Expr;
127
  };
128
129
  struct BaseTypeRef {
130
    BaseTypeRef(unsigned BitSize, dwarf::TypeKind Encoding) :
131
8
      BitSize(BitSize), Encoding(Encoding) {}
132
    unsigned BitSize;
133
    dwarf::TypeKind Encoding;
134
    DIE *Die = nullptr;
135
  };
136
137
  std::vector<BaseTypeRef> ExprRefedBaseTypes;
138
139
  /// Get or create global variable DIE.
140
  DIE *
141
  getOrCreateGlobalVariableDIE(const DIGlobalVariable *GV,
142
                               ArrayRef<GlobalExpr> GlobalExprs);
143
144
  DIE *getOrCreateCommonBlock(const DICommonBlock *CB,
145
                              ArrayRef<GlobalExpr> GlobalExprs);
146
147
  void addLocationAttribute(DIE *ToDIE, const DIGlobalVariable *GV,
148
                            ArrayRef<GlobalExpr> GlobalExprs);
149
150
  /// addLabelAddress - Add a dwarf label attribute data and value using
151
  /// either DW_FORM_addr or DW_FORM_GNU_addr_index.
152
  void addLabelAddress(DIE &Die, dwarf::Attribute Attribute,
153
                       const MCSymbol *Label);
154
155
  /// addLocalLabelAddress - Add a dwarf label attribute data and value using
156
  /// DW_FORM_addr only.
157
  void addLocalLabelAddress(DIE &Die, dwarf::Attribute Attribute,
158
                            const MCSymbol *Label);
159
160
43
  DwarfCompileUnit &getCU() override { return *this; }
161
162
  unsigned getOrCreateSourceID(const DIFile *File) override;
163
164
235
  void addImportedEntity(const DIImportedEntity* IE) {
165
235
    DIScope *Scope = IE->getScope();
166
235
    assert(Scope && "Invalid Scope encoding!");
167
235
    if (!isa<DILocalScope>(Scope))
168
218
      // No need to add imported enities that are not local declaration.
169
218
      return;
170
17
171
17
    auto *LocalScope = cast<DILocalScope>(Scope)->getNonLexicalBlockFileScope();
172
17
    ImportedEntities[LocalScope].push_back(IE);
173
17
  }
174
175
  /// addRange - Add an address range to the list of ranges for this unit.
176
  void addRange(RangeSpan Range);
177
178
  void attachLowHighPC(DIE &D, const MCSymbol *Begin, const MCSymbol *End);
179
180
  /// Find DIE for the given subprogram and attach appropriate
181
  /// DW_AT_low_pc and DW_AT_high_pc attributes. If there are global
182
  /// variables in this scope then create and insert DIEs for these
183
  /// variables.
184
  DIE &updateSubprogramScopeDIE(const DISubprogram *SP);
185
186
  void constructScopeDIE(LexicalScope *Scope,
187
                         SmallVectorImpl<DIE *> &FinalChildren);
188
189
  /// A helper function to construct a RangeSpanList for a given
190
  /// lexical scope.
191
  void addScopeRangeList(DIE &ScopeDIE, SmallVector<RangeSpan, 2> Range);
192
193
  void attachRangesOrLowHighPC(DIE &D, SmallVector<RangeSpan, 2> Ranges);
194
195
  void attachRangesOrLowHighPC(DIE &D,
196
                               const SmallVectorImpl<InsnRange> &Ranges);
197
198
  /// This scope represents inlined body of a function. Construct
199
  /// DIE to represent this concrete inlined copy of the function.
200
  DIE *constructInlinedScopeDIE(LexicalScope *Scope);
201
202
  /// Construct new DW_TAG_lexical_block for this scope and
203
  /// attach DW_AT_low_pc/DW_AT_high_pc labels.
204
  DIE *constructLexicalScopeDIE(LexicalScope *Scope);
205
206
  /// constructVariableDIE - Construct a DIE for the given DbgVariable.
207
  DIE *constructVariableDIE(DbgVariable &DV, bool Abstract = false);
208
209
  DIE *constructVariableDIE(DbgVariable &DV, const LexicalScope &Scope,
210
                            DIE *&ObjectPointer);
211
212
  /// Construct a DIE for the given DbgLabel.
213
  DIE *constructLabelDIE(DbgLabel &DL, const LexicalScope &Scope);
214
215
  /// A helper function to create children of a Scope DIE.
216
  DIE *createScopeChildrenDIE(LexicalScope *Scope,
217
                              SmallVectorImpl<DIE *> &Children,
218
                              bool *HasNonScopeChildren = nullptr);
219
220
  void createBaseTypeDIEs();
221
222
  /// Construct a DIE for this subprogram scope.
223
  DIE &constructSubprogramScopeDIE(const DISubprogram *Sub,
224
                                   LexicalScope *Scope);
225
226
  DIE *createAndAddScopeChildren(LexicalScope *Scope, DIE &ScopeDIE);
227
228
  void constructAbstractSubprogramScopeDIE(LexicalScope *Scope);
229
230
  /// Construct a call site entry DIE describing a call within \p Scope to a
231
  /// callee described by \p CalleeSP. \p IsTail specifies whether the call is
232
  /// a tail call. \p PCOffset must be non-zero for non-tail calls or be the
233
  /// function-local offset to PC value after the call instruction.
234
  DIE &constructCallSiteEntryDIE(DIE &ScopeDIE, const DISubprogram &CalleeSP,
235
                                 bool IsTail, const MCExpr *PCOffset);
236
237
  /// Construct import_module DIE.
238
  DIE *constructImportedEntityDIE(const DIImportedEntity *Module);
239
240
  void finishSubprogramDefinition(const DISubprogram *SP);
241
  void finishEntityDefinition(const DbgEntity *Entity);
242
243
  /// Find abstract variable associated with Var.
244
  using InlinedEntity = DbgValueHistoryMap::InlinedEntity;
245
  DbgEntity *getExistingAbstractEntity(const DINode *Node);
246
  void createAbstractEntity(const DINode *Node, LexicalScope *Scope);
247
248
  /// Set the skeleton unit associated with this unit.
249
55
  void setSkeleton(DwarfCompileUnit &Skel) { Skeleton = &Skel; }
250
251
6.22k
  unsigned getHeaderSize() const override {
252
6.22k
    // DWARF v5 added the DWO ID to the header for split/skeleton units.
253
6.22k
    unsigned DWOIdSize =
254
6.22k
        DD->getDwarfVersion() >= 5 && 
DD->useSplitDwarf()78
?
sizeof(uint64_t)20
255
6.22k
                                                          : 
06.20k
;
256
6.22k
    return DwarfUnit::getHeaderSize() + DWOIdSize;
257
6.22k
  }
258
3.48k
  unsigned getLength() {
259
3.48k
    return sizeof(uint32_t) + // Length field
260
3.48k
        getHeaderSize() + getUnitDie().getSize();
261
3.48k
  }
262
263
  void emitHeader(bool UseOffsets) override;
264
265
  /// Add the DW_AT_addr_base attribute to the unit DIE.
266
  void addAddrTableBase();
267
268
3.81k
  MCSymbol *getLabelBegin() const {
269
3.81k
    assert(getSection());
270
3.81k
    return LabelBegin;
271
3.81k
  }
272
273
4
  MCSymbol *getMacroLabelBegin() const {
274
4
    return MacroLabelBegin;
275
4
  }
276
277
  /// Add a new global name to the compile unit.
278
  void addGlobalName(StringRef Name, const DIE &Die,
279
                     const DIScope *Context) override;
280
281
  /// Add a new global name present in a type unit to this compile unit.
282
  void addGlobalNameForTypeUnit(StringRef Name, const DIScope *Context);
283
284
  /// Add a new global type to the compile unit.
285
  void addGlobalType(const DIType *Ty, const DIE &Die,
286
                     const DIScope *Context) override;
287
288
  /// Add a new global type present in a type unit to this compile unit.
289
  void addGlobalTypeUnitType(const DIType *Ty, const DIScope *Context);
290
291
1.74k
  const StringMap<const DIE *> &getGlobalNames() const { return GlobalNames; }
292
1.74k
  const StringMap<const DIE *> &getGlobalTypes() const { return GlobalTypes; }
293
294
  /// Add DW_AT_location attribute for a DbgVariable based on provided
295
  /// MachineLocation.
296
  void addVariableAddress(const DbgVariable &DV, DIE &Die,
297
                          MachineLocation Location);
298
  /// Add an address attribute to a die based on the location provided.
299
  void addAddress(DIE &Die, dwarf::Attribute Attribute,
300
                  const MachineLocation &Location);
301
302
  /// Start with the address based on the location provided, and generate the
303
  /// DWARF information necessary to find the actual variable (navigating the
304
  /// extra location information encoded in the type) based on the starting
305
  /// location.  Add the DWARF information to the die.
306
  void addComplexAddress(const DbgVariable &DV, DIE &Die,
307
                         dwarf::Attribute Attribute,
308
                         const MachineLocation &Location);
309
310
  /// Add a Dwarf loclistptr attribute data and value.
311
  void addLocationList(DIE &Die, dwarf::Attribute Attribute, unsigned Index);
312
  void applyVariableAttributes(const DbgVariable &Var, DIE &VariableDie);
313
314
  /// Add a Dwarf expression attribute data and value.
315
  void addExpr(DIELoc &Die, dwarf::Form Form, const MCExpr *Expr);
316
317
  /// Add an attribute containing an address expression to \p Die.
318
  void addAddressExpr(DIE &Die, dwarf::Attribute Attribute, const MCExpr *Expr);
319
320
  void applySubprogramAttributesToDefinition(const DISubprogram *SP,
321
                                             DIE &SPDie);
322
323
  void applyLabelAttributes(const DbgLabel &Label, DIE &LabelDie);
324
325
  /// getRanges - Get the list of ranges for this unit.
326
4.71k
  const SmallVectorImpl<RangeSpan> &getRanges() const { return CURanges; }
327
2.14k
  SmallVector<RangeSpan, 2> takeRanges() { return std::move(CURanges); }
328
329
2.05k
  void setBaseAddress(const MCSymbol *Base) { BaseAddress = Base; }
330
57.3k
  const MCSymbol *getBaseAddress() const { return BaseAddress; }
331
332
18
  uint64_t getDWOId() const { return DWOId; }
333
18
  void setDWOId(uint64_t DwoId) { DWOId = DwoId; }
334
335
  bool hasDwarfPubSections() const;
336
337
  void addBaseTypeRef(DIEValueList &Die, int64_t Idx);
338
};
339
340
} // end namespace llvm
341
342
#endif // LLVM_LIB_CODEGEN_ASMPRINTER_DWARFCOMPILEUNIT_H