Coverage Report

Created: 2017-10-03 07:32

/Users/buildslave/jenkins/sharedspace/clang-stage2-coverage-R@2/llvm/lib/IR/DebugInfoMetadata.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- DebugInfoMetadata.cpp - Implement debug info metadata --------------===//
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 implements the debug info Metadata classes.
11
//
12
//===----------------------------------------------------------------------===//
13
14
#include "llvm/IR/DebugInfoMetadata.h"
15
#include "LLVMContextImpl.h"
16
#include "MetadataImpl.h"
17
#include "llvm/ADT/StringSwitch.h"
18
#include "llvm/IR/DIBuilder.h"
19
#include "llvm/IR/Function.h"
20
21
using namespace llvm;
22
23
DILocation::DILocation(LLVMContext &C, StorageType Storage, unsigned Line,
24
                       unsigned Column, ArrayRef<Metadata *> MDs)
25
632k
    : MDNode(C, DILocationKind, Storage, MDs) {
26
632k
  assert((MDs.size() == 1 || MDs.size() == 2) &&
27
632k
         "Expected a scope and optional inlined-at");
28
632k
29
632k
  // Set line and column.
30
632k
  assert(Column < (1u << 16) && "Expected 16-bit column");
31
632k
32
632k
  SubclassData32 = Line;
33
632k
  SubclassData16 = Column;
34
632k
}
35
36
1.46M
static void adjustColumn(unsigned &Column) {
37
1.46M
  // Set to unknown on overflow.  We only have 16 bits to play with here.
38
1.46M
  if (Column >= (1u << 16))
39
4
    Column = 0;
40
1.46M
}
41
42
DILocation *DILocation::getImpl(LLVMContext &Context, unsigned Line,
43
                                unsigned Column, Metadata *Scope,
44
                                Metadata *InlinedAt, StorageType Storage,
45
1.46M
                                bool ShouldCreate) {
46
1.46M
  // Fixup column.
47
1.46M
  adjustColumn(Column);
48
1.46M
49
1.46M
  if (
Storage == Uniqued1.46M
) {
50
1.40M
    if (auto *N =
51
1.40M
            getUniqued(Context.pImpl->DILocations,
52
1.40M
                       DILocationInfo::KeyTy(Line, Column, Scope, InlinedAt)))
53
830k
      return N;
54
574k
    
if (574k
!ShouldCreate574k
)
55
0
      return nullptr;
56
58.0k
  } else {
57
58.0k
    assert(ShouldCreate && "Expected non-uniqued nodes to always be created");
58
58.0k
  }
59
1.46M
60
632k
  SmallVector<Metadata *, 2> Ops;
61
632k
  Ops.push_back(Scope);
62
632k
  if (InlinedAt)
63
188k
    Ops.push_back(InlinedAt);
64
632k
  return storeImpl(new (Ops.size())
65
632k
                       DILocation(Context, Storage, Line, Column, Ops),
66
632k
                   Storage, Context.pImpl->DILocations);
67
1.46M
}
68
69
2.64k
DINode::DIFlags DINode::getFlag(StringRef Flag) {
70
2.64k
  return StringSwitch<DIFlags>(Flag)
71
68.6k
#define HANDLE_DI_FLAG(ID, NAME) .Case("DIFlag" #NAME, Flag##NAME)
72
2.64k
#include "llvm/IR/DebugInfoFlags.def"
73
2.64k
      .Default(DINode::FlagZero);
74
2.64k
}
75
76
7.22k
StringRef DINode::getFlagString(DIFlags Flag) {
77
7.22k
  switch (Flag) {
78
7.22k
#define HANDLE_DI_FLAG(ID, NAME)                                               \
79
7.22k
  case Flag##NAME:                                                             \
80
7.22k
    return "DIFlag" #NAME;
81
0
#include "llvm/IR/DebugInfoFlags.def"
82
7.22k
  }
83
3
  return "";
84
7.22k
}
85
86
DINode::DIFlags DINode::splitFlags(DIFlags Flags,
87
5.49k
                                   SmallVectorImpl<DIFlags> &SplitFlags) {
88
5.49k
  // Flags that are packed together need to be specially handled, so
89
5.49k
  // that, for example, we emit "DIFlagPublic" and not
90
5.49k
  // "DIFlagPrivate | DIFlagProtected".
91
5.49k
  if (DIFlags 
A5.49k
= Flags & FlagAccessibility) {
92
727
    if (A == FlagPrivate)
93
40
      SplitFlags.push_back(FlagPrivate);
94
687
    else 
if (687
A == FlagProtected687
)
95
56
      SplitFlags.push_back(FlagProtected);
96
687
    else
97
631
      SplitFlags.push_back(FlagPublic);
98
727
    Flags &= ~A;
99
727
  }
100
5.49k
  if (DIFlags 
R5.49k
= Flags & FlagPtrToMemberRep) {
101
10
    if (R == FlagSingleInheritance)
102
6
      SplitFlags.push_back(FlagSingleInheritance);
103
4
    else 
if (4
R == FlagMultipleInheritance4
)
104
2
      SplitFlags.push_back(FlagMultipleInheritance);
105
4
    else
106
2
      SplitFlags.push_back(FlagVirtualInheritance);
107
10
    Flags &= ~R;
108
10
  }
109
5.49k
  if (
(Flags & FlagIndirectVirtualBase) == FlagIndirectVirtualBase5.49k
) {
110
1
    Flags &= ~FlagIndirectVirtualBase;
111
1
    SplitFlags.push_back(FlagIndirectVirtualBase);
112
1
  }
113
5.49k
114
5.49k
#define HANDLE_DI_FLAG(ID, NAME)                                               \
115
142k
  
if (DIFlags 142k
Bit142k
= Flags & Flag##NAME) { \
116
6.48k
    SplitFlags.push_back(Bit);                                                 \
117
6.48k
    Flags &= ~Bit;                                                             \
118
6.48k
  }
119
5.49k
#include "llvm/IR/DebugInfoFlags.def"
120
5.49k
  return Flags;
121
5.49k
}
122
123
752
DIScopeRef DIScope::getScope() const {
124
752
  if (auto *T = dyn_cast<DIType>(this))
125
239
    return T->getScope();
126
513
127
513
  
if (auto *513
SP513
= dyn_cast<DISubprogram>(this))
128
11
    return SP->getScope();
129
502
130
502
  
if (auto *502
LB502
= dyn_cast<DILexicalBlockBase>(this))
131
160
    return LB->getScope();
132
342
133
342
  
if (auto *342
NS342
= dyn_cast<DINamespace>(this))
134
121
    return NS->getScope();
135
221
136
221
  
if (auto *221
M221
= dyn_cast<DIModule>(this))
137
0
    return M->getScope();
138
221
139
221
  assert((isa<DIFile>(this) || isa<DICompileUnit>(this)) &&
140
221
         "Unhandled type of scope.");
141
221
  return nullptr;
142
221
}
143
144
624
StringRef DIScope::getName() const {
145
624
  if (auto *T = dyn_cast<DIType>(this))
146
301
    return T->getName();
147
323
  
if (auto *323
SP323
= dyn_cast<DISubprogram>(this))
148
9
    return SP->getName();
149
314
  
if (auto *314
NS314
= dyn_cast<DINamespace>(this))
150
97
    return NS->getName();
151
217
  
if (auto *217
M217
= dyn_cast<DIModule>(this))
152
0
    return M->getName();
153
217
  assert((isa<DILexicalBlockBase>(this) || isa<DIFile>(this) ||
154
217
          isa<DICompileUnit>(this)) &&
155
217
         "Unhandled type of scope.");
156
217
  return "";
157
217
}
158
159
#ifndef NDEBUG
160
static bool isCanonical(const MDString *S) {
161
  return !S || !S->getString().empty();
162
}
163
#endif
164
165
GenericDINode *GenericDINode::getImpl(LLVMContext &Context, unsigned Tag,
166
                                      MDString *Header,
167
                                      ArrayRef<Metadata *> DwarfOps,
168
61
                                      StorageType Storage, bool ShouldCreate) {
169
61
  unsigned Hash = 0;
170
61
  if (
Storage == Uniqued61
) {
171
49
    GenericDINodeInfo::KeyTy Key(Tag, Header, DwarfOps);
172
49
    if (auto *N = getUniqued(Context.pImpl->GenericDINodes, Key))
173
13
      return N;
174
36
    
if (36
!ShouldCreate36
)
175
0
      return nullptr;
176
36
    Hash = Key.getHash();
177
61
  } else {
178
12
    assert(ShouldCreate && "Expected non-uniqued nodes to always be created");
179
12
  }
180
61
181
61
  // Use a nullptr for empty headers.
182
48
  assert(isCanonical(Header) && "Expected canonical MDString");
183
48
  Metadata *PreOps[] = {Header};
184
48
  return storeImpl(new (DwarfOps.size() + 1) GenericDINode(
185
48
                       Context, Storage, Hash, Tag, PreOps, DwarfOps),
186
48
                   Storage, Context.pImpl->GenericDINodes);
187
61
}
188
189
15
void GenericDINode::recalculateHash() {
190
15
  setHash(GenericDINodeInfo::KeyTy::calculateHash(this));
191
15
}
192
193
65.0k
#define UNWRAP_ARGS_IMPL(...) __VA_ARGS__
194
65.0k
#define UNWRAP_ARGS(ARGS) UNWRAP_ARGS_IMPL ARGS
195
#define DEFINE_GETIMPL_LOOKUP(CLASS, ARGS)                                     \
196
96.0k
  
do 96.0k
{ \
197
96.0k
    if (
Storage == Uniqued96.0k
) { \
198
65.7k
      if (auto *N = getUniqued(Context.pImpl->CLASS##s,                        \
199
65.7k
                               CLASS##Info::KeyTy(UNWRAP_ARGS(ARGS))))         \
200
29.5k
        return N;                                                              \
201
36.1k
      
if (36.1k
!ShouldCreate36.1k
) \
202
0
        return nullptr;                                                        \
203
96.0k
    } else {                                                                   \
204
30.3k
      assert(ShouldCreate &&                                                   \
205
30.3k
             "Expected non-uniqued nodes to always be created");               \
206
30.3k
    }                                                                          \
207
96.0k
  } while (false)
208
#define DEFINE_GETIMPL_STORE(CLASS, ARGS, OPS)                                 \
209
36.9k
  return storeImpl(new (array_lengthof(OPS))                                   \
210
36.9k
                       CLASS(Context, Storage, UNWRAP_ARGS(ARGS), OPS),        \
211
36.9k
                   Storage, Context.pImpl->CLASS##s)
212
#define DEFINE_GETIMPL_STORE_NO_OPS(CLASS, ARGS)                               \
213
2.01k
  
return storeImpl(new (0u) CLASS(Context, Storage, 2.01k
UNWRAP_ARGS2.01k
(ARGS)), \
214
2.01k
                   Storage, Context.pImpl->CLASS##s)
215
#define DEFINE_GETIMPL_STORE_NO_CONSTRUCTOR_ARGS(CLASS, OPS)                   \
216
1.49k
  return storeImpl(new (array_lengthof(OPS)) CLASS(Context, Storage, OPS),     \
217
1.49k
                   Storage, Context.pImpl->CLASS##s)
218
#define DEFINE_GETIMPL_STORE_N(CLASS, ARGS, OPS, NUM_OPS)                      \
219
26.0k
  return storeImpl(new (NUM_OPS)                                               \
220
26.0k
                       CLASS(Context, Storage, UNWRAP_ARGS(ARGS), OPS),        \
221
26.0k
                   Storage, Context.pImpl->CLASS##s)
222
223
DISubrange *DISubrange::getImpl(LLVMContext &Context, int64_t Count, int64_t Lo,
224
405
                                StorageType Storage, bool ShouldCreate) {
225
405
  DEFINE_GETIMPL_LOOKUP(DISubrange, (Count, Lo));
226
378
  
DEFINE_GETIMPL_STORE_NO_OPS378
(DISubrange, (Count, Lo));
227
405
}
228
229
DIEnumerator *DIEnumerator::getImpl(LLVMContext &Context, int64_t Value,
230
                                    MDString *Name, StorageType Storage,
231
5.86k
                                    bool ShouldCreate) {
232
5.86k
  assert(isCanonical(Name) && "Expected canonical MDString");
233
5.86k
  DEFINE_GETIMPL_LOOKUP(DIEnumerator, (Value, Name));
234
5.86k
  Metadata *Ops[] = {Name};
235
5.86k
  DEFINE_GETIMPL_STORE(DIEnumerator, (Value), Ops);
236
5.86k
}
237
238
DIBasicType *DIBasicType::getImpl(LLVMContext &Context, unsigned Tag,
239
                                  MDString *Name, uint64_t SizeInBits,
240
                                  uint32_t AlignInBits, unsigned Encoding,
241
1.89k
                                  StorageType Storage, bool ShouldCreate) {
242
1.89k
  assert(isCanonical(Name) && "Expected canonical MDString");
243
1.89k
  DEFINE_GETIMPL_LOOKUP(DIBasicType,
244
1.89k
                        (Tag, Name, SizeInBits, AlignInBits, Encoding));
245
1.85k
  Metadata *Ops[] = {nullptr, nullptr, Name};
246
1.85k
  DEFINE_GETIMPL_STORE(DIBasicType, (Tag, SizeInBits, AlignInBits, Encoding),
247
1.89k
                       Ops);
248
1.89k
}
249
250
DIDerivedType *DIDerivedType::getImpl(
251
    LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *File,
252
    unsigned Line, Metadata *Scope, Metadata *BaseType, uint64_t SizeInBits,
253
    uint32_t AlignInBits, uint64_t OffsetInBits,
254
    Optional<unsigned> DWARFAddressSpace, DIFlags Flags, Metadata *ExtraData,
255
7.14k
    StorageType Storage, bool ShouldCreate) {
256
7.14k
  assert(isCanonical(Name) && "Expected canonical MDString");
257
7.14k
  DEFINE_GETIMPL_LOOKUP(DIDerivedType,
258
7.14k
                        (Tag, Name, File, Line, Scope, BaseType, SizeInBits,
259
7.14k
                         AlignInBits, OffsetInBits, DWARFAddressSpace, Flags,
260
7.14k
                         ExtraData));
261
6.56k
  Metadata *Ops[] = {File, Scope, Name, BaseType, ExtraData};
262
6.56k
  DEFINE_GETIMPL_STORE(
263
7.14k
      DIDerivedType, (Tag, Line, SizeInBits, AlignInBits, OffsetInBits,
264
7.14k
                      DWARFAddressSpace, Flags), Ops);
265
7.14k
}
266
267
DICompositeType *DICompositeType::getImpl(
268
    LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *File,
269
    unsigned Line, Metadata *Scope, Metadata *BaseType, uint64_t SizeInBits,
270
    uint32_t AlignInBits, uint64_t OffsetInBits, DIFlags Flags,
271
    Metadata *Elements, unsigned RuntimeLang, Metadata *VTableHolder,
272
    Metadata *TemplateParams, MDString *Identifier, StorageType Storage,
273
2.77k
    bool ShouldCreate) {
274
2.77k
  assert(isCanonical(Name) && "Expected canonical MDString");
275
2.77k
276
2.77k
  // Keep this in sync with buildODRType.
277
2.77k
  DEFINE_GETIMPL_LOOKUP(
278
2.77k
      DICompositeType, (Tag, Name, File, Line, Scope, BaseType, SizeInBits,
279
2.77k
                        AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
280
2.77k
                        VTableHolder, TemplateParams, Identifier));
281
2.75k
  Metadata *Ops[] = {File,     Scope,        Name,           BaseType,
282
2.75k
                     Elements, VTableHolder, TemplateParams, Identifier};
283
2.75k
  DEFINE_GETIMPL_STORE(DICompositeType, (Tag, Line, RuntimeLang, SizeInBits,
284
2.77k
                                         AlignInBits, OffsetInBits, Flags),
285
2.77k
                       Ops);
286
2.77k
}
287
288
DICompositeType *DICompositeType::buildODRType(
289
    LLVMContext &Context, MDString &Identifier, unsigned Tag, MDString *Name,
290
    Metadata *File, unsigned Line, Metadata *Scope, Metadata *BaseType,
291
    uint64_t SizeInBits, uint32_t AlignInBits, uint64_t OffsetInBits,
292
    DIFlags Flags, Metadata *Elements, unsigned RuntimeLang,
293
469
    Metadata *VTableHolder, Metadata *TemplateParams) {
294
469
  assert(!Identifier.getString().empty() && "Expected valid identifier");
295
469
  if (!Context.isODRUniquingDebugTypes())
296
348
    return nullptr;
297
121
  auto *&CT = (*Context.pImpl->DITypeMap)[&Identifier];
298
121
  if (!CT)
299
89
    return CT = DICompositeType::getDistinct(
300
89
               Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
301
89
               AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
302
89
               VTableHolder, TemplateParams, &Identifier);
303
32
304
32
  // Only mutate CT if it's a forward declaration and the new operands aren't.
305
121
  assert(CT->getRawIdentifier() == &Identifier && "Wrong ODR identifier?");
306
32
  if (
!CT->isForwardDecl() || 32
(Flags & DINode::FlagFwdDecl)11
)
307
28
    return CT;
308
4
309
4
  // Mutate CT in place.  Keep this in sync with getImpl.
310
4
  CT->mutate(Tag, Line, RuntimeLang, SizeInBits, AlignInBits, OffsetInBits,
311
4
             Flags);
312
4
  Metadata *Ops[] = {File,     Scope,        Name,           BaseType,
313
4
                     Elements, VTableHolder, TemplateParams, &Identifier};
314
4
  assert((std::end(Ops) - std::begin(Ops)) == (int)CT->getNumOperands() &&
315
4
         "Mismatched number of operands");
316
36
  for (unsigned I = 0, E = CT->getNumOperands(); 
I != E36
;
++I32
)
317
32
    
if (32
Ops[I] != CT->getOperand(I)32
)
318
9
      CT->setOperand(I, Ops[I]);
319
469
  return CT;
320
469
}
321
322
DICompositeType *DICompositeType::getODRType(
323
    LLVMContext &Context, MDString &Identifier, unsigned Tag, MDString *Name,
324
    Metadata *File, unsigned Line, Metadata *Scope, Metadata *BaseType,
325
    uint64_t SizeInBits, uint32_t AlignInBits, uint64_t OffsetInBits,
326
    DIFlags Flags, Metadata *Elements, unsigned RuntimeLang,
327
4
    Metadata *VTableHolder, Metadata *TemplateParams) {
328
4
  assert(!Identifier.getString().empty() && "Expected valid identifier");
329
4
  if (!Context.isODRUniquingDebugTypes())
330
1
    return nullptr;
331
3
  auto *&CT = (*Context.pImpl->DITypeMap)[&Identifier];
332
3
  if (!CT)
333
1
    CT = DICompositeType::getDistinct(
334
1
        Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
335
1
        AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang, VTableHolder,
336
1
        TemplateParams, &Identifier);
337
4
  return CT;
338
4
}
339
340
DICompositeType *DICompositeType::getODRTypeIfExists(LLVMContext &Context,
341
5
                                                     MDString &Identifier) {
342
5
  assert(!Identifier.getString().empty() && "Expected valid identifier");
343
5
  if (!Context.isODRUniquingDebugTypes())
344
1
    return nullptr;
345
4
  return Context.pImpl->DITypeMap->lookup(&Identifier);
346
4
}
347
348
DISubroutineType *DISubroutineType::getImpl(LLVMContext &Context, DIFlags Flags,
349
                                            uint8_t CC, Metadata *TypeArray,
350
                                            StorageType Storage,
351
25.2k
                                            bool ShouldCreate) {
352
25.2k
  DEFINE_GETIMPL_LOOKUP(DISubroutineType, (Flags, CC, TypeArray));
353
3.76k
  Metadata *Ops[] = {nullptr, nullptr, nullptr, TypeArray};
354
3.76k
  DEFINE_GETIMPL_STORE(DISubroutineType, (Flags, CC), Ops);
355
25.2k
}
356
357
// FIXME: Implement this string-enum correspondence with a .def file and macros,
358
// so that the association is explicit rather than implied.
359
static const char *ChecksumKindName[DIFile::CSK_Last + 1] = {
360
  "CSK_None",
361
  "CSK_MD5",
362
  "CSK_SHA1"
363
};
364
365
63
DIFile::ChecksumKind DIFile::getChecksumKind(StringRef CSKindStr) {
366
63
  return StringSwitch<DIFile::ChecksumKind>(CSKindStr)
367
63
      .Case("CSK_MD5", DIFile::CSK_MD5)
368
63
      .Case("CSK_SHA1", DIFile::CSK_SHA1)
369
63
      .Default(DIFile::CSK_None);
370
63
}
371
372
34
StringRef DIFile::getChecksumKindAsString() const {
373
34
  assert(CSKind <= DIFile::CSK_Last && "Invalid checksum kind");
374
34
  return ChecksumKindName[CSKind];
375
34
}
376
377
DIFile *DIFile::getImpl(LLVMContext &Context, MDString *Filename,
378
                        MDString *Directory, DIFile::ChecksumKind CSKind,
379
                        MDString *Checksum, StorageType Storage,
380
7.82k
                        bool ShouldCreate) {
381
7.82k
  assert(isCanonical(Filename) && "Expected canonical MDString");
382
7.82k
  assert(isCanonical(Directory) && "Expected canonical MDString");
383
7.82k
  assert(isCanonical(Checksum) && "Expected canonical MDString");
384
7.82k
  DEFINE_GETIMPL_LOOKUP(DIFile, (Filename, Directory, CSKind, Checksum));
385
5.74k
  Metadata *Ops[] = {Filename, Directory, Checksum};
386
5.74k
  DEFINE_GETIMPL_STORE(DIFile, (CSKind), Ops);
387
7.82k
}
388
389
DICompileUnit *DICompileUnit::getImpl(
390
    LLVMContext &Context, unsigned SourceLanguage, Metadata *File,
391
    MDString *Producer, bool IsOptimized, MDString *Flags,
392
    unsigned RuntimeVersion, MDString *SplitDebugFilename,
393
    unsigned EmissionKind, Metadata *EnumTypes, Metadata *RetainedTypes,
394
    Metadata *GlobalVariables, Metadata *ImportedEntities, Metadata *Macros,
395
    uint64_t DWOId, bool SplitDebugInlining, bool DebugInfoForProfiling,
396
3.30k
    bool GnuPubnames, StorageType Storage, bool ShouldCreate) {
397
3.30k
  assert(Storage != Uniqued && "Cannot unique DICompileUnit");
398
3.30k
  assert(isCanonical(Producer) && "Expected canonical MDString");
399
3.30k
  assert(isCanonical(Flags) && "Expected canonical MDString");
400
3.30k
  assert(isCanonical(SplitDebugFilename) && "Expected canonical MDString");
401
3.30k
402
3.30k
  Metadata *Ops[] = {
403
3.30k
      File,      Producer,      Flags,           SplitDebugFilename,
404
3.30k
      EnumTypes, RetainedTypes, GlobalVariables, ImportedEntities,
405
3.30k
      Macros};
406
3.30k
  return storeImpl(new (array_lengthof(Ops)) DICompileUnit(
407
3.30k
                       Context, Storage, SourceLanguage, IsOptimized,
408
3.30k
                       RuntimeVersion, EmissionKind, DWOId, SplitDebugInlining,
409
3.30k
                       DebugInfoForProfiling, GnuPubnames, Ops),
410
3.30k
                   Storage);
411
3.30k
}
412
413
Optional<DICompileUnit::DebugEmissionKind>
414
1.26k
DICompileUnit::getEmissionKind(StringRef Str) {
415
1.26k
  return StringSwitch<Optional<DebugEmissionKind>>(Str)
416
1.26k
      .Case("NoDebug", NoDebug)
417
1.26k
      .Case("FullDebug", FullDebug)
418
1.26k
      .Case("LineTablesOnly", LineTablesOnly)
419
1.26k
      .Default(None);
420
1.26k
}
421
422
875
const char *DICompileUnit::EmissionKindString(DebugEmissionKind EK) {
423
875
  switch (EK) {
424
102
  case NoDebug:        return "NoDebug";
425
595
  case FullDebug:      return "FullDebug";
426
178
  case LineTablesOnly: return "LineTablesOnly";
427
0
  }
428
0
  return nullptr;
429
0
}
430
431
357k
DISubprogram *DILocalScope::getSubprogram() const {
432
357k
  if (auto *Block = dyn_cast<DILexicalBlockBase>(this))
433
7.36k
    return Block->getScope()->getSubprogram();
434
349k
  return const_cast<DISubprogram *>(cast<DISubprogram>(this));
435
349k
}
436
437
882k
DILocalScope *DILocalScope::getNonLexicalBlockFileScope() const {
438
882k
  if (auto *File = dyn_cast<DILexicalBlockFile>(this))
439
10.3k
    return File->getScope()->getNonLexicalBlockFileScope();
440
872k
  return const_cast<DILocalScope *>(this);
441
872k
}
442
443
DISubprogram *DISubprogram::getImpl(
444
    LLVMContext &Context, Metadata *Scope, MDString *Name,
445
    MDString *LinkageName, Metadata *File, unsigned Line, Metadata *Type,
446
    bool IsLocalToUnit, bool IsDefinition, unsigned ScopeLine,
447
    Metadata *ContainingType, unsigned Virtuality, unsigned VirtualIndex,
448
    int ThisAdjustment, DIFlags Flags, bool IsOptimized, Metadata *Unit,
449
    Metadata *TemplateParams, Metadata *Declaration, Metadata *Variables,
450
26.0k
    Metadata *ThrownTypes, StorageType Storage, bool ShouldCreate) {
451
26.0k
  assert(isCanonical(Name) && "Expected canonical MDString");
452
26.0k
  assert(isCanonical(LinkageName) && "Expected canonical MDString");
453
26.0k
  DEFINE_GETIMPL_LOOKUP(
454
26.0k
      DISubprogram, (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
455
26.0k
                     IsDefinition, ScopeLine, ContainingType, Virtuality,
456
26.0k
                     VirtualIndex, ThisAdjustment, Flags, IsOptimized, Unit,
457
26.0k
                     TemplateParams, Declaration, Variables, ThrownTypes));
458
26.0k
  SmallVector<Metadata *, 11> Ops = {
459
26.0k
      File,        Scope,     Name,           LinkageName,    Type,       Unit,
460
26.0k
      Declaration, Variables, ContainingType, TemplateParams, ThrownTypes};
461
26.0k
  if (
!ThrownTypes26.0k
) {
462
26.0k
    Ops.pop_back();
463
26.0k
    if (
!TemplateParams26.0k
) {
464
25.6k
      Ops.pop_back();
465
25.6k
      if (!ContainingType)
466
25.2k
        Ops.pop_back();
467
25.6k
    }
468
26.0k
  }
469
26.0k
  DEFINE_GETIMPL_STORE_N(DISubprogram,
470
26.0k
                         (Line, ScopeLine, Virtuality, VirtualIndex,
471
26.0k
                          ThisAdjustment, Flags, IsLocalToUnit, IsDefinition,
472
26.0k
                          IsOptimized),
473
26.0k
                         Ops, Ops.size());
474
26.0k
}
475
476
6.02k
bool DISubprogram::describes(const Function *F) const {
477
6.02k
  assert(F && "Invalid function");
478
6.02k
  if (F->getSubprogram() == this)
479
5.96k
    return true;
480
63
  StringRef Name = getLinkageName();
481
63
  if (Name.empty())
482
21
    Name = getName();
483
6.02k
  return F->getName() == Name;
484
6.02k
}
485
486
DILexicalBlock *DILexicalBlock::getImpl(LLVMContext &Context, Metadata *Scope,
487
                                        Metadata *File, unsigned Line,
488
                                        unsigned Column, StorageType Storage,
489
1.59k
                                        bool ShouldCreate) {
490
1.59k
  // Fixup column.
491
1.59k
  adjustColumn(Column);
492
1.59k
493
1.59k
  assert(Scope && "Expected scope");
494
1.59k
  DEFINE_GETIMPL_LOOKUP(DILexicalBlock, (Scope, File, Line, Column));
495
1.58k
  Metadata *Ops[] = {File, Scope};
496
1.58k
  DEFINE_GETIMPL_STORE(DILexicalBlock, (Line, Column), Ops);
497
1.59k
}
498
499
DILexicalBlockFile *DILexicalBlockFile::getImpl(LLVMContext &Context,
500
                                                Metadata *Scope, Metadata *File,
501
                                                unsigned Discriminator,
502
                                                StorageType Storage,
503
918
                                                bool ShouldCreate) {
504
918
  assert(Scope && "Expected scope");
505
918
  DEFINE_GETIMPL_LOOKUP(DILexicalBlockFile, (Scope, File, Discriminator));
506
534
  Metadata *Ops[] = {File, Scope};
507
534
  DEFINE_GETIMPL_STORE(DILexicalBlockFile, (Discriminator), Ops);
508
918
}
509
510
DINamespace *DINamespace::getImpl(LLVMContext &Context, Metadata *Scope,
511
                                  MDString *Name, bool ExportSymbols,
512
244
                                  StorageType Storage, bool ShouldCreate) {
513
244
  assert(isCanonical(Name) && "Expected canonical MDString");
514
244
  DEFINE_GETIMPL_LOOKUP(DINamespace, (Scope, Name, ExportSymbols));
515
244
  // The nullptr is for DIScope's File operand. This should be refactored.
516
222
  Metadata *Ops[] = {nullptr, Scope, Name};
517
222
  DEFINE_GETIMPL_STORE(DINamespace, (ExportSymbols), Ops);
518
244
}
519
520
DIModule *DIModule::getImpl(LLVMContext &Context, Metadata *Scope,
521
                            MDString *Name, MDString *ConfigurationMacros,
522
                            MDString *IncludePath, MDString *ISysRoot,
523
92
                            StorageType Storage, bool ShouldCreate) {
524
92
  assert(isCanonical(Name) && "Expected canonical MDString");
525
92
  DEFINE_GETIMPL_LOOKUP(
526
92
      DIModule, (Scope, Name, ConfigurationMacros, IncludePath, ISysRoot));
527
85
  Metadata *Ops[] = {Scope, Name, ConfigurationMacros, IncludePath, ISysRoot};
528
85
  DEFINE_GETIMPL_STORE_NO_CONSTRUCTOR_ARGS(DIModule, Ops);
529
92
}
530
531
DITemplateTypeParameter *DITemplateTypeParameter::getImpl(LLVMContext &Context,
532
                                                          MDString *Name,
533
                                                          Metadata *Type,
534
                                                          StorageType Storage,
535
391
                                                          bool ShouldCreate) {
536
391
  assert(isCanonical(Name) && "Expected canonical MDString");
537
391
  DEFINE_GETIMPL_LOOKUP(DITemplateTypeParameter, (Name, Type));
538
297
  Metadata *Ops[] = {Name, Type};
539
297
  DEFINE_GETIMPL_STORE_NO_CONSTRUCTOR_ARGS(DITemplateTypeParameter, Ops);
540
391
}
541
542
DITemplateValueParameter *DITemplateValueParameter::getImpl(
543
    LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *Type,
544
496
    Metadata *Value, StorageType Storage, bool ShouldCreate) {
545
496
  assert(isCanonical(Name) && "Expected canonical MDString");
546
496
  DEFINE_GETIMPL_LOOKUP(DITemplateValueParameter, (Tag, Name, Type, Value));
547
349
  Metadata *Ops[] = {Name, Type, Value};
548
349
  DEFINE_GETIMPL_STORE(DITemplateValueParameter, (Tag), Ops);
549
496
}
550
551
DIGlobalVariable *
552
DIGlobalVariable::getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name,
553
                          MDString *LinkageName, Metadata *File, unsigned Line,
554
                          Metadata *Type, bool IsLocalToUnit, bool IsDefinition,
555
                          Metadata *StaticDataMemberDeclaration,
556
                          uint32_t AlignInBits, StorageType Storage,
557
1.13k
                          bool ShouldCreate) {
558
1.13k
  assert(isCanonical(Name) && "Expected canonical MDString");
559
1.13k
  assert(isCanonical(LinkageName) && "Expected canonical MDString");
560
1.13k
  DEFINE_GETIMPL_LOOKUP(DIGlobalVariable,
561
1.13k
                        (Scope, Name, LinkageName, File, Line, Type,
562
1.13k
                         IsLocalToUnit, IsDefinition,
563
1.13k
                         StaticDataMemberDeclaration, AlignInBits));
564
1.13k
  Metadata *Ops[] = {
565
1.13k
      Scope, Name, File, Type, Name, LinkageName, StaticDataMemberDeclaration};
566
1.13k
  DEFINE_GETIMPL_STORE(DIGlobalVariable,
567
1.13k
                       (Line, IsLocalToUnit, IsDefinition, AlignInBits),
568
1.13k
                       Ops);
569
1.13k
}
570
571
DILocalVariable *DILocalVariable::getImpl(LLVMContext &Context, Metadata *Scope,
572
                                          MDString *Name, Metadata *File,
573
                                          unsigned Line, Metadata *Type,
574
                                          unsigned Arg, DIFlags Flags,
575
                                          uint32_t AlignInBits,
576
                                          StorageType Storage,
577
5.22k
                                          bool ShouldCreate) {
578
5.22k
  // 64K ought to be enough for any frontend.
579
5.22k
  assert(Arg <= UINT16_MAX && "Expected argument number to fit in 16-bits");
580
5.22k
581
5.22k
  assert(Scope && "Expected scope");
582
5.22k
  assert(isCanonical(Name) && "Expected canonical MDString");
583
5.22k
  DEFINE_GETIMPL_LOOKUP(DILocalVariable,
584
5.22k
                        (Scope, Name, File, Line, Type, Arg, Flags,
585
5.22k
                         AlignInBits));
586
5.14k
  Metadata *Ops[] = {Scope, Name, File, Type};
587
5.14k
  DEFINE_GETIMPL_STORE(DILocalVariable, (Line, Arg, Flags, AlignInBits), Ops);
588
5.22k
}
589
590
DIExpression *DIExpression::getImpl(LLVMContext &Context,
591
                                    ArrayRef<uint64_t> Elements,
592
6.08k
                                    StorageType Storage, bool ShouldCreate) {
593
6.08k
  DEFINE_GETIMPL_LOOKUP(DIExpression, (Elements));
594
1.63k
  
DEFINE_GETIMPL_STORE_NO_OPS1.63k
(DIExpression, (Elements));
595
6.08k
}
596
597
12.0k
unsigned DIExpression::ExprOperand::getSize() const {
598
12.0k
  switch (getOp()) {
599
1.74k
  case dwarf::DW_OP_LLVM_fragment:
600
1.74k
    return 3;
601
4.60k
  case dwarf::DW_OP_constu:
602
4.60k
  case dwarf::DW_OP_plus_uconst:
603
4.60k
    return 2;
604
5.75k
  default:
605
5.75k
    return 1;
606
0
  }
607
0
}
608
609
18.1k
bool DIExpression::isValid() const {
610
20.1k
  for (auto I = expr_op_begin(), E = expr_op_end(); 
I != E20.1k
;
++I2.00k
) {
611
2.68k
    // Check that there's space for the operand.
612
2.68k
    if (I->get() + I->getSize() > E->get())
613
3
      return false;
614
2.67k
615
2.67k
    // Check that the operand is valid.
616
2.67k
    switch (I->getOp()) {
617
5
    default:
618
5
      return false;
619
661
    case dwarf::DW_OP_LLVM_fragment:
620
661
      // A fragment operator must appear at the end.
621
661
      return I->get() + I->getSize() == E->get();
622
210
    case dwarf::DW_OP_stack_value: {
623
210
      // Must be the last one or followed by a DW_OP_LLVM_fragment.
624
210
      if (I->get() + I->getSize() == E->get())
625
156
        break;
626
54
      auto J = I;
627
54
      if ((++J)->getOp() != dwarf::DW_OP_LLVM_fragment)
628
0
        return false;
629
54
      break;
630
54
    }
631
163
    case dwarf::DW_OP_swap: {
632
163
      // Must be more than one implicit element on the stack.
633
163
634
163
      // FIXME: A better way to implement this would be to add a local variable
635
163
      // that keeps track of the stack depth and introduce something like a
636
163
      // DW_LLVM_OP_implicit_location as a placeholder for the location this
637
163
      // DIExpression is attached to, or else pass the number of implicit stack
638
163
      // elements into isValid.
639
163
      if (getNumElements() == 1)
640
2
        return false;
641
161
      break;
642
161
    }
643
1.63k
    case dwarf::DW_OP_constu:
644
1.63k
    case dwarf::DW_OP_plus_uconst:
645
1.63k
    case dwarf::DW_OP_plus:
646
1.63k
    case dwarf::DW_OP_minus:
647
1.63k
    case dwarf::DW_OP_mul:
648
1.63k
    case dwarf::DW_OP_deref:
649
1.63k
    case dwarf::DW_OP_xderef:
650
1.63k
      break;
651
2.68k
    }
652
2.68k
  }
653
17.4k
  return true;
654
18.1k
}
655
656
Optional<DIExpression::FragmentInfo>
657
16.5k
DIExpression::getFragmentInfo(expr_op_iterator Start, expr_op_iterator End) {
658
19.6k
  for (auto I = Start; 
I != End19.6k
;
++I3.04k
)
659
4.20k
    
if (4.20k
I->getOp() == dwarf::DW_OP_LLVM_fragment4.20k
) {
660
1.15k
      DIExpression::FragmentInfo Info = {I->getArg(1), I->getArg(0)};
661
1.15k
      return Info;
662
1.15k
    }
663
15.4k
  return None;
664
16.5k
}
665
666
void DIExpression::appendOffset(SmallVectorImpl<uint64_t> &Ops,
667
239
                                int64_t Offset) {
668
239
  if (
Offset > 0239
) {
669
80
    Ops.push_back(dwarf::DW_OP_plus_uconst);
670
80
    Ops.push_back(Offset);
671
239
  } else 
if (159
Offset < 0159
) {
672
38
    Ops.push_back(dwarf::DW_OP_constu);
673
38
    Ops.push_back(-Offset);
674
38
    Ops.push_back(dwarf::DW_OP_minus);
675
38
  }
676
239
}
677
678
95
bool DIExpression::extractIfOffset(int64_t &Offset) const {
679
95
  if (
getNumElements() == 095
) {
680
89
    Offset = 0;
681
89
    return true;
682
89
  }
683
6
684
6
  
if (6
getNumElements() == 2 && 6
Elements[0] == dwarf::DW_OP_plus_uconst6
) {
685
6
    Offset = Elements[1];
686
6
    return true;
687
6
  }
688
0
689
0
  
if (0
getNumElements() == 3 && 0
Elements[0] == dwarf::DW_OP_constu0
) {
690
0
    if (
Elements[2] == dwarf::DW_OP_plus0
) {
691
0
      Offset = Elements[1];
692
0
      return true;
693
0
    }
694
0
    
if (0
Elements[2] == dwarf::DW_OP_minus0
) {
695
0
      Offset = -Elements[1];
696
0
      return true;
697
0
    }
698
0
  }
699
0
700
0
  return false;
701
0
}
702
703
DIExpression *DIExpression::prepend(const DIExpression *Expr, bool Deref,
704
237
                                    int64_t Offset, bool StackValue) {
705
237
  SmallVector<uint64_t, 8> Ops;
706
237
  appendOffset(Ops, Offset);
707
237
  if (Deref)
708
35
    Ops.push_back(dwarf::DW_OP_deref);
709
237
  if (Expr)
710
237
    
for (auto Op : Expr->expr_ops()) 237
{
711
139
      // A DW_OP_stack_value comes at the end, but before a DW_OP_LLVM_fragment.
712
139
      if (
StackValue139
) {
713
3
        if (Op.getOp() == dwarf::DW_OP_stack_value)
714
1
          StackValue = false;
715
2
        else 
if (2
Op.getOp() == dwarf::DW_OP_LLVM_fragment2
) {
716
1
          Ops.push_back(dwarf::DW_OP_stack_value);
717
1
          StackValue = false;
718
1
        }
719
3
      }
720
139
      Ops.push_back(Op.getOp());
721
188
      for (unsigned I = 0; 
I < Op.getNumArgs()188
;
++I49
)
722
49
        Ops.push_back(Op.getArg(I));
723
237
    }
724
237
  if (StackValue)
725
2
    Ops.push_back(dwarf::DW_OP_stack_value);
726
237
  return DIExpression::get(Expr->getContext(), Ops);
727
237
}
728
729
DIExpression *DIExpression::createFragmentExpression(const DIExpression *Expr,
730
                                                     unsigned OffsetInBits,
731
70
                                                     unsigned SizeInBits) {
732
70
  SmallVector<uint64_t, 8> Ops;
733
70
  // Copy over the expression, but leave off any trailing DW_OP_LLVM_fragment.
734
70
  if (
Expr70
) {
735
15
    for (auto Op : Expr->expr_ops()) {
736
15
      if (
Op.getOp() == dwarf::DW_OP_LLVM_fragment15
) {
737
15
        // Make the new offset point into the existing fragment.
738
15
        uint64_t FragmentOffsetInBits = Op.getArg(0);
739
15
        // Op.getArg(0) is FragmentOffsetInBits.
740
15
        // Op.getArg(1) is FragmentSizeInBits.
741
15
        assert((OffsetInBits + SizeInBits <= Op.getArg(0) + Op.getArg(1)) &&
742
15
               "new fragment outside of original fragment");
743
15
        OffsetInBits += FragmentOffsetInBits;
744
15
        break;
745
15
      }
746
0
      Ops.push_back(Op.getOp());
747
0
      for (unsigned I = 0; 
I < Op.getNumArgs()0
;
++I0
)
748
0
        Ops.push_back(Op.getArg(I));
749
15
    }
750
70
  }
751
70
  Ops.push_back(dwarf::DW_OP_LLVM_fragment);
752
70
  Ops.push_back(OffsetInBits);
753
70
  Ops.push_back(SizeInBits);
754
70
  return DIExpression::get(Expr->getContext(), Ops);
755
70
}
756
757
829
bool DIExpression::isConstant() const {
758
829
  // Recognize DW_OP_constu C DW_OP_stack_value (DW_OP_LLVM_fragment Len Ofs)?.
759
829
  if (
getNumElements() != 3 && 829
getNumElements() != 6823
)
760
812
    return false;
761
17
  
if (17
getElement(0) != dwarf::DW_OP_constu ||
762
14
      getElement(2) != dwarf::DW_OP_stack_value)
763
3
    return false;
764
14
  
if (14
getNumElements() == 6 && 14
getElement(3) != dwarf::DW_OP_LLVM_fragment11
)
765
0
    return false;
766
14
  return true;
767
14
}
768
769
DIGlobalVariableExpression *
770
DIGlobalVariableExpression::getImpl(LLVMContext &Context, Metadata *Variable,
771
                                    Metadata *Expression, StorageType Storage,
772
1.11k
                                    bool ShouldCreate) {
773
1.11k
  DEFINE_GETIMPL_LOOKUP(DIGlobalVariableExpression, (Variable, Expression));
774
1.11k
  Metadata *Ops[] = {Variable, Expression};
775
1.11k
  DEFINE_GETIMPL_STORE_NO_CONSTRUCTOR_ARGS(DIGlobalVariableExpression, Ops);
776
1.11k
}
777
778
DIObjCProperty *DIObjCProperty::getImpl(
779
    LLVMContext &Context, MDString *Name, Metadata *File, unsigned Line,
780
    MDString *GetterName, MDString *SetterName, unsigned Attributes,
781
127
    Metadata *Type, StorageType Storage, bool ShouldCreate) {
782
127
  assert(isCanonical(Name) && "Expected canonical MDString");
783
127
  assert(isCanonical(GetterName) && "Expected canonical MDString");
784
127
  assert(isCanonical(SetterName) && "Expected canonical MDString");
785
127
  DEFINE_GETIMPL_LOOKUP(DIObjCProperty, (Name, File, Line, GetterName,
786
127
                                         SetterName, Attributes, Type));
787
94
  Metadata *Ops[] = {Name, File, GetterName, SetterName, Type};
788
94
  DEFINE_GETIMPL_STORE(DIObjCProperty, (Line, Attributes), Ops);
789
127
}
790
791
DIImportedEntity *DIImportedEntity::getImpl(LLVMContext &Context, unsigned Tag,
792
                                            Metadata *Scope, Metadata *Entity,
793
                                            Metadata *File, unsigned Line,
794
                                            MDString *Name, StorageType Storage,
795
155
                                            bool ShouldCreate) {
796
155
  assert(isCanonical(Name) && "Expected canonical MDString");
797
155
  DEFINE_GETIMPL_LOOKUP(DIImportedEntity,
798
155
                        (Tag, Scope, Entity, File, Line, Name));
799
149
  Metadata *Ops[] = {Scope, Entity, Name, File};
800
149
  DEFINE_GETIMPL_STORE(DIImportedEntity, (Tag, Line), Ops);
801
155
}
802
803
DIMacro *DIMacro::getImpl(LLVMContext &Context, unsigned MIType,
804
                          unsigned Line, MDString *Name, MDString *Value,
805
1.10k
                          StorageType Storage, bool ShouldCreate) {
806
1.10k
  assert(isCanonical(Name) && "Expected canonical MDString");
807
1.10k
  DEFINE_GETIMPL_LOOKUP(DIMacro, (MIType, Line, Name, Value));
808
1.10k
  Metadata *Ops[] = { Name, Value };
809
1.10k
  DEFINE_GETIMPL_STORE(DIMacro, (MIType, Line), Ops);
810
1.10k
}
811
812
DIMacroFile *DIMacroFile::getImpl(LLVMContext &Context, unsigned MIType,
813
                                  unsigned Line, Metadata *File,
814
                                  Metadata *Elements, StorageType Storage,
815
84
                                  bool ShouldCreate) {
816
84
  DEFINE_GETIMPL_LOOKUP(DIMacroFile,
817
84
                        (MIType, Line, File, Elements));
818
82
  Metadata *Ops[] = { File, Elements };
819
82
  DEFINE_GETIMPL_STORE(DIMacroFile, (MIType, Line), Ops);
820
84
}
821