Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/IR/Metadata.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- Metadata.cpp - Implement Metadata classes --------------------------===//
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 implements the Metadata classes.
10
//
11
//===----------------------------------------------------------------------===//
12
13
#include "LLVMContextImpl.h"
14
#include "MetadataImpl.h"
15
#include "SymbolTableListTraitsImpl.h"
16
#include "llvm/ADT/APFloat.h"
17
#include "llvm/ADT/APInt.h"
18
#include "llvm/ADT/ArrayRef.h"
19
#include "llvm/ADT/DenseSet.h"
20
#include "llvm/ADT/None.h"
21
#include "llvm/ADT/STLExtras.h"
22
#include "llvm/ADT/SetVector.h"
23
#include "llvm/ADT/SmallPtrSet.h"
24
#include "llvm/ADT/SmallSet.h"
25
#include "llvm/ADT/SmallVector.h"
26
#include "llvm/ADT/StringMap.h"
27
#include "llvm/ADT/StringRef.h"
28
#include "llvm/ADT/Twine.h"
29
#include "llvm/IR/Argument.h"
30
#include "llvm/IR/BasicBlock.h"
31
#include "llvm/IR/Constant.h"
32
#include "llvm/IR/ConstantRange.h"
33
#include "llvm/IR/Constants.h"
34
#include "llvm/IR/DebugInfoMetadata.h"
35
#include "llvm/IR/DebugLoc.h"
36
#include "llvm/IR/Function.h"
37
#include "llvm/IR/GlobalObject.h"
38
#include "llvm/IR/GlobalVariable.h"
39
#include "llvm/IR/Instruction.h"
40
#include "llvm/IR/LLVMContext.h"
41
#include "llvm/IR/Metadata.h"
42
#include "llvm/IR/Module.h"
43
#include "llvm/IR/TrackingMDRef.h"
44
#include "llvm/IR/Type.h"
45
#include "llvm/IR/Value.h"
46
#include "llvm/IR/ValueHandle.h"
47
#include "llvm/Support/Casting.h"
48
#include "llvm/Support/ErrorHandling.h"
49
#include "llvm/Support/MathExtras.h"
50
#include <algorithm>
51
#include <cassert>
52
#include <cstddef>
53
#include <cstdint>
54
#include <iterator>
55
#include <tuple>
56
#include <type_traits>
57
#include <utility>
58
#include <vector>
59
60
using namespace llvm;
61
62
MetadataAsValue::MetadataAsValue(Type *Ty, Metadata *MD)
63
51.5k
    : Value(Ty, MetadataAsValueVal), MD(MD) {
64
51.5k
  track();
65
51.5k
}
66
67
39.0k
MetadataAsValue::~MetadataAsValue() {
68
39.0k
  getType()->getContext().pImpl->MetadataAsValues.erase(MD);
69
39.0k
  untrack();
70
39.0k
}
71
72
/// Canonicalize metadata arguments to intrinsics.
73
///
74
/// To support bitcode upgrades (and assembly semantic sugar) for \a
75
/// MetadataAsValue, we need to canonicalize certain metadata.
76
///
77
///   - nullptr is replaced by an empty MDNode.
78
///   - An MDNode with a single null operand is replaced by an empty MDNode.
79
///   - An MDNode whose only operand is a \a ConstantAsMetadata gets skipped.
80
///
81
/// This maintains readability of bitcode from when metadata was a type of
82
/// value, and these bridges were unnecessary.
83
static Metadata *canonicalizeMetadataForValue(LLVMContext &Context,
84
105k
                                              Metadata *MD) {
85
105k
  if (!MD)
86
13.2k
    // !{}
87
13.2k
    return MDNode::get(Context, None);
88
92.6k
89
92.6k
  // Return early if this isn't a single-operand MDNode.
90
92.6k
  auto *N = dyn_cast<MDNode>(MD);
91
92.6k
  if (!N || 
N->getNumOperands() != 136.6k
)
92
91.8k
    return MD;
93
821
94
821
  if (!N->getOperand(0))
95
10
    // !{}
96
10
    return MDNode::get(Context, None);
97
811
98
811
  if (auto *C = dyn_cast<ConstantAsMetadata>(N->getOperand(0)))
99
246
    // Look through the MDNode.
100
246
    return C;
101
565
102
565
  return MD;
103
565
}
104
105
76.9k
MetadataAsValue *MetadataAsValue::get(LLVMContext &Context, Metadata *MD) {
106
76.9k
  MD = canonicalizeMetadataForValue(Context, MD);
107
76.9k
  auto *&Entry = Context.pImpl->MetadataAsValues[MD];
108
76.9k
  if (!Entry)
109
51.5k
    Entry = new MetadataAsValue(Type::getMetadataTy(Context), MD);
110
76.9k
  return Entry;
111
76.9k
}
112
113
MetadataAsValue *MetadataAsValue::getIfExists(LLVMContext &Context,
114
4.22k
                                              Metadata *MD) {
115
4.22k
  MD = canonicalizeMetadataForValue(Context, MD);
116
4.22k
  auto &Store = Context.pImpl->MetadataAsValues;
117
4.22k
  return Store.lookup(MD);
118
4.22k
}
119
120
24.7k
void MetadataAsValue::handleChangedMetadata(Metadata *MD) {
121
24.7k
  LLVMContext &Context = getContext();
122
24.7k
  MD = canonicalizeMetadataForValue(Context, MD);
123
24.7k
  auto &Store = Context.pImpl->MetadataAsValues;
124
24.7k
125
24.7k
  // Stop tracking the old metadata.
126
24.7k
  Store.erase(this->MD);
127
24.7k
  untrack();
128
24.7k
  this->MD = nullptr;
129
24.7k
130
24.7k
  // Start tracking MD, or RAUW if necessary.
131
24.7k
  auto *&Entry = Store[MD];
132
24.7k
  if (Entry) {
133
17.5k
    replaceAllUsesWith(Entry);
134
17.5k
    delete this;
135
17.5k
    return;
136
17.5k
  }
137
7.17k
138
7.17k
  this->MD = MD;
139
7.17k
  track();
140
7.17k
  Entry = this;
141
7.17k
}
142
143
58.6k
void MetadataAsValue::track() {
144
58.6k
  if (MD)
145
58.6k
    MetadataTracking::track(&MD, *MD, *this);
146
58.6k
}
147
148
63.7k
void MetadataAsValue::untrack() {
149
63.7k
  if (MD)
150
24.7k
    MetadataTracking::untrack(MD);
151
63.7k
}
152
153
178M
bool MetadataTracking::track(void *Ref, Metadata &MD, OwnerTy Owner) {
154
178M
  assert(Ref && "Expected live reference");
155
178M
  assert((Owner || *static_cast<Metadata **>(Ref) == &MD) &&
156
178M
         "Reference without owner must be direct");
157
178M
  if (auto *R = ReplaceableMetadataImpl::getOrCreate(MD)) {
158
1.81M
    R->addRef(Ref, Owner);
159
1.81M
    return true;
160
1.81M
  }
161
176M
  if (auto *PH = dyn_cast<DistinctMDOperandPlaceholder>(&MD)) {
162
38.4k
    assert(!PH->Use && "Placeholders can only be used once");
163
38.4k
    assert(!Owner && "Unexpected callback to owner");
164
38.4k
    PH->Use = static_cast<Metadata **>(Ref);
165
38.4k
    return true;
166
38.4k
  }
167
176M
  return false;
168
176M
}
169
170
151M
void MetadataTracking::untrack(void *Ref, Metadata &MD) {
171
151M
  assert(Ref && "Expected live reference");
172
151M
  if (auto *R = ReplaceableMetadataImpl::getIfExists(MD))
173
445k
    R->dropRef(Ref);
174
151M
  else if (auto *PH = dyn_cast<DistinctMDOperandPlaceholder>(&MD))
175
38.4k
    PH->Use = nullptr;
176
151M
}
177
178
123M
bool MetadataTracking::retrack(void *Ref, Metadata &MD, void *New) {
179
123M
  assert(Ref && "Expected live reference");
180
123M
  assert(New && "Expected live reference");
181
123M
  assert(Ref != New && "Expected change");
182
123M
  if (auto *R = ReplaceableMetadataImpl::getIfExists(MD)) {
183
160k
    R->moveRef(Ref, New, MD);
184
160k
    return true;
185
160k
  }
186
122M
  assert(!isa<DistinctMDOperandPlaceholder>(MD) &&
187
122M
         "Unexpected move of an MDOperand");
188
122M
  assert(!isReplaceable(MD) &&
189
122M
         "Expected un-replaceable metadata, since we didn't move a reference");
190
122M
  return false;
191
122M
}
192
193
0
bool MetadataTracking::isReplaceable(const Metadata &MD) {
194
0
  return ReplaceableMetadataImpl::isReplaceable(MD);
195
0
}
196
197
1.81M
void ReplaceableMetadataImpl::addRef(void *Ref, OwnerTy Owner) {
198
1.81M
  bool WasInserted =
199
1.81M
      UseMap.insert(std::make_pair(Ref, std::make_pair(Owner, NextIndex)))
200
1.81M
          .second;
201
1.81M
  (void)WasInserted;
202
1.81M
  assert(WasInserted && "Expected to add a reference");
203
1.81M
204
1.81M
  ++NextIndex;
205
1.81M
  assert(NextIndex != 0 && "Unexpected overflow");
206
1.81M
}
207
208
445k
void ReplaceableMetadataImpl::dropRef(void *Ref) {
209
445k
  bool WasErased = UseMap.erase(Ref);
210
445k
  (void)WasErased;
211
445k
  assert(WasErased && "Expected to drop a reference");
212
445k
}
213
214
void ReplaceableMetadataImpl::moveRef(void *Ref, void *New,
215
160k
                                      const Metadata &MD) {
216
160k
  auto I = UseMap.find(Ref);
217
160k
  assert(I != UseMap.end() && "Expected to move a reference");
218
160k
  auto OwnerAndIndex = I->second;
219
160k
  UseMap.erase(I);
220
160k
  bool WasInserted = UseMap.insert(std::make_pair(New, OwnerAndIndex)).second;
221
160k
  (void)WasInserted;
222
160k
  assert(WasInserted && "Expected to add a reference");
223
160k
224
160k
  // Check that the references are direct if there's no owner.
225
160k
  (void)MD;
226
160k
  assert((OwnerAndIndex.first || *static_cast<Metadata **>(Ref) == &MD) &&
227
160k
         "Reference without owner must be direct");
228
160k
  assert((OwnerAndIndex.first || *static_cast<Metadata **>(New) == &MD) &&
229
160k
         "Reference without owner must be direct");
230
160k
}
231
232
677k
void ReplaceableMetadataImpl::replaceAllUsesWith(Metadata *MD) {
233
677k
  if (UseMap.empty())
234
420k
    return;
235
256k
236
256k
  // Copy out uses since UseMap will get touched below.
237
256k
  using UseTy = std::pair<void *, std::pair<OwnerTy, uint64_t>>;
238
256k
  SmallVector<UseTy, 8> Uses(UseMap.begin(), UseMap.end());
239
328k
  llvm::sort(Uses, [](const UseTy &L, const UseTy &R) {
240
328k
    return L.second.second < R.second.second;
241
328k
  });
242
418k
  for (const auto &Pair : Uses) {
243
418k
    // Check that this Ref hasn't disappeared after RAUW (when updating a
244
418k
    // previous Ref).
245
418k
    if (!UseMap.count(Pair.first))
246
0
      continue;
247
418k
248
418k
    OwnerTy Owner = Pair.second.first;
249
418k
    if (!Owner) {
250
301k
      // Update unowned tracking references directly.
251
301k
      Metadata *&Ref = *static_cast<Metadata **>(Pair.first);
252
301k
      Ref = MD;
253
301k
      if (MD)
254
301k
        MetadataTracking::track(Ref);
255
301k
      UseMap.erase(Pair.first);
256
301k
      continue;
257
301k
    }
258
117k
259
117k
    // Check for MetadataAsValue.
260
117k
    if (Owner.is<MetadataAsValue *>()) {
261
24.7k
      Owner.get<MetadataAsValue *>()->handleChangedMetadata(MD);
262
24.7k
      continue;
263
24.7k
    }
264
92.5k
265
92.5k
    // There's a Metadata owner -- dispatch.
266
92.5k
    Metadata *OwnerMD = Owner.get<Metadata *>();
267
92.5k
    switch (OwnerMD->getMetadataID()) {
268
92.5k
#define HANDLE_METADATA_LEAF(CLASS)                                            \
269
92.5k
  case Metadata::CLASS##Kind:                                                  \
270
92.5k
    cast<CLASS>(OwnerMD)->handleChangedOperand(Pair.first, MD);                \
271
92.5k
    continue;
272
92.5k
#include "llvm/IR/Metadata.def"
273
92.5k
    default:
274
0
      llvm_unreachable("Invalid metadata subclass");
275
92.5k
    }
276
92.5k
  }
277
256k
  assert(UseMap.empty() && "Expected all uses to be replaced");
278
256k
}
279
280
494k
void ReplaceableMetadataImpl::resolveAllUses(bool ResolveUsers) {
281
494k
  if (UseMap.empty())
282
416k
    return;
283
78.4k
284
78.4k
  if (!ResolveUsers) {
285
4.48k
    UseMap.clear();
286
4.48k
    return;
287
4.48k
  }
288
73.9k
289
73.9k
  // Copy out uses since UseMap could get touched below.
290
73.9k
  using UseTy = std::pair<void *, std::pair<OwnerTy, uint64_t>>;
291
73.9k
  SmallVector<UseTy, 8> Uses(UseMap.begin(), UseMap.end());
292
151k
  llvm::sort(Uses, [](const UseTy &L, const UseTy &R) {
293
151k
    return L.second.second < R.second.second;
294
151k
  });
295
73.9k
  UseMap.clear();
296
158k
  for (const auto &Pair : Uses) {
297
158k
    auto Owner = Pair.second.first;
298
158k
    if (!Owner)
299
70.2k
      continue;
300
88.2k
    if (Owner.is<MetadataAsValue *>())
301
1.39k
      continue;
302
86.8k
303
86.8k
    // Resolve MDNodes that point at this.
304
86.8k
    auto *OwnerMD = dyn_cast<MDNode>(Owner.get<Metadata *>());
305
86.8k
    if (!OwnerMD)
306
0
      continue;
307
86.8k
    if (OwnerMD->isResolved())
308
48.1k
      continue;
309
38.7k
    OwnerMD->decrementUnresolvedOperandCount();
310
38.7k
  }
311
73.9k
}
312
313
178M
ReplaceableMetadataImpl *ReplaceableMetadataImpl::getOrCreate(Metadata &MD) {
314
178M
  if (auto *N = dyn_cast<MDNode>(&MD))
315
176M
    return N->isResolved() ? 
nullptr175M
:
N->Context.getOrCreateReplaceableUses()659k
;
316
1.83M
  return dyn_cast<ValueAsMetadata>(&MD);
317
1.83M
}
318
319
274M
ReplaceableMetadataImpl *ReplaceableMetadataImpl::getIfExists(Metadata &MD) {
320
274M
  if (auto *N = dyn_cast<MDNode>(&MD))
321
274M
    return N->isResolved() ? 
nullptr273M
:
N->Context.getReplaceableUses()241k
;
322
898k
  return dyn_cast<ValueAsMetadata>(&MD);
323
898k
}
324
325
0
bool ReplaceableMetadataImpl::isReplaceable(const Metadata &MD) {
326
0
  if (auto *N = dyn_cast<MDNode>(&MD))
327
0
    return !N->isResolved();
328
0
  return dyn_cast<ValueAsMetadata>(&MD);
329
0
}
330
331
2.31k
static DISubprogram *getLocalFunctionMetadata(Value *V) {
332
2.31k
  assert(V && "Expected value");
333
2.31k
  if (auto *A = dyn_cast<Argument>(V)) {
334
673
    if (auto *Fn = A->getParent())
335
42
      return Fn->getSubprogram();
336
631
    return nullptr;
337
631
  }
338
1.64k
339
1.64k
  if (BasicBlock *BB = cast<Instruction>(V)->getParent()) {
340
1.56k
    if (auto *Fn = BB->getParent())
341
1.56k
      return Fn->getSubprogram();
342
0
    return nullptr;
343
0
  }
344
78
345
78
  return nullptr;
346
78
}
347
348
1.08M
ValueAsMetadata *ValueAsMetadata::get(Value *V) {
349
1.08M
  assert(V && "Unexpected null Value");
350
1.08M
351
1.08M
  auto &Context = V->getContext();
352
1.08M
  auto *&Entry = Context.pImpl->ValuesAsMetadata[V];
353
1.08M
  if (!Entry) {
354
382k
    assert((isa<Constant>(V) || isa<Argument>(V) || isa<Instruction>(V)) &&
355
382k
           "Expected constant or function-local value");
356
382k
    assert(!V->IsUsedByMD && "Expected this to be the only metadata use");
357
382k
    V->IsUsedByMD = true;
358
382k
    if (auto *C = dyn_cast<Constant>(V))
359
351k
      Entry = new ConstantAsMetadata(C);
360
30.9k
    else
361
30.9k
      Entry = new LocalAsMetadata(V);
362
382k
  }
363
1.08M
364
1.08M
  return Entry;
365
1.08M
}
366
367
5.22k
ValueAsMetadata *ValueAsMetadata::getIfExists(Value *V) {
368
5.22k
  assert(V && "Unexpected null Value");
369
5.22k
  return V->getContext().pImpl->ValuesAsMetadata.lookup(V);
370
5.22k
}
371
372
97.5k
void ValueAsMetadata::handleDeletion(Value *V) {
373
97.5k
  assert(V && "Expected valid value");
374
97.5k
375
97.5k
  auto &Store = V->getType()->getContext().pImpl->ValuesAsMetadata;
376
97.5k
  auto I = Store.find(V);
377
97.5k
  if (I == Store.end())
378
1
    return;
379
97.5k
380
97.5k
  // Remove old entry from the map.
381
97.5k
  ValueAsMetadata *MD = I->second;
382
97.5k
  assert(MD && "Expected valid metadata");
383
97.5k
  assert(MD->getValue() == V && "Expected valid mapping");
384
97.5k
  Store.erase(I);
385
97.5k
386
97.5k
  // Delete the metadata.
387
97.5k
  MD->replaceAllUsesWith(nullptr);
388
97.5k
  delete MD;
389
97.5k
}
390
391
9.89k
void ValueAsMetadata::handleRAUW(Value *From, Value *To) {
392
9.89k
  assert(From && "Expected valid value");
393
9.89k
  assert(To && "Expected valid value");
394
9.89k
  assert(From != To && "Expected changed value");
395
9.89k
  assert(From->getType() == To->getType() && "Unexpected type change");
396
9.89k
397
9.89k
  LLVMContext &Context = From->getType()->getContext();
398
9.89k
  auto &Store = Context.pImpl->ValuesAsMetadata;
399
9.89k
  auto I = Store.find(From);
400
9.89k
  if (I == Store.end()) {
401
0
    assert(!From->IsUsedByMD && "Expected From not to be used by metadata");
402
0
    return;
403
0
  }
404
9.89k
405
9.89k
  // Remove old entry from the map.
406
9.89k
  assert(From->IsUsedByMD && "Expected From to be used by metadata");
407
9.89k
  From->IsUsedByMD = false;
408
9.89k
  ValueAsMetadata *MD = I->second;
409
9.89k
  assert(MD && "Expected valid metadata");
410
9.89k
  assert(MD->getValue() == From && "Expected valid mapping");
411
9.89k
  Store.erase(I);
412
9.89k
413
9.89k
  if (isa<LocalAsMetadata>(MD)) {
414
9.73k
    if (auto *C = dyn_cast<Constant>(To)) {
415
8.15k
      // Local became a constant.
416
8.15k
      MD->replaceAllUsesWith(ConstantAsMetadata::get(C));
417
8.15k
      delete MD;
418
8.15k
      return;
419
8.15k
    }
420
1.58k
    if (getLocalFunctionMetadata(From) && 
getLocalFunctionMetadata(To)297
&&
421
1.58k
        
getLocalFunctionMetadata(From) != getLocalFunctionMetadata(To)216
) {
422
0
      // DISubprogram changed.
423
0
      MD->replaceAllUsesWith(nullptr);
424
0
      delete MD;
425
0
      return;
426
0
    }
427
162
  } else if (!isa<Constant>(To)) {
428
1
    // Changed to function-local value.
429
1
    MD->replaceAllUsesWith(nullptr);
430
1
    delete MD;
431
1
    return;
432
1
  }
433
1.74k
434
1.74k
  auto *&Entry = Store[To];
435
1.74k
  if (Entry) {
436
58
    // The target already exists.
437
58
    MD->replaceAllUsesWith(Entry);
438
58
    delete MD;
439
58
    return;
440
58
  }
441
1.68k
442
1.68k
  // Update MD in place (and update the map entry).
443
1.68k
  assert(!To->IsUsedByMD && "Expected this to be the only metadata use");
444
1.68k
  To->IsUsedByMD = true;
445
1.68k
  MD->V = To;
446
1.68k
  Entry = MD;
447
1.68k
}
448
449
//===----------------------------------------------------------------------===//
450
// MDString implementation.
451
//
452
453
717k
MDString *MDString::get(LLVMContext &Context, StringRef Str) {
454
717k
  auto &Store = Context.pImpl->MDStringCache;
455
717k
  auto I = Store.try_emplace(Str);
456
717k
  auto &MapEntry = I.first->getValue();
457
717k
  if (!I.second)
458
277k
    return &MapEntry;
459
439k
  MapEntry.Entry = &*I.first;
460
439k
  return &MapEntry;
461
439k
}
462
463
63.1M
StringRef MDString::getString() const {
464
63.1M
  assert(Entry && "Expected to find string map entry");
465
63.1M
  return Entry->first();
466
63.1M
}
467
468
//===----------------------------------------------------------------------===//
469
// MDNode implementation.
470
//
471
472
// Assert that the MDNode types will not be unaligned by the objects
473
// prepended to them.
474
#define HANDLE_MDNODE_LEAF(CLASS)                                              \
475
  static_assert(                                                               \
476
      alignof(uint64_t) >= alignof(CLASS),                                     \
477
      "Alignment is insufficient after objects prepended to " #CLASS);
478
#include "llvm/IR/Metadata.def"
479
480
5.17M
void *MDNode::operator new(size_t Size, unsigned NumOps) {
481
5.17M
  size_t OpSize = NumOps * sizeof(MDOperand);
482
5.17M
  // uint64_t is the most aligned type we need support (ensured by static_assert
483
5.17M
  // above)
484
5.17M
  OpSize = alignTo(OpSize, alignof(uint64_t));
485
5.17M
  void *Ptr = reinterpret_cast<char *>(::operator new(OpSize + Size)) + OpSize;
486
5.17M
  MDOperand *O = static_cast<MDOperand *>(Ptr);
487
15.0M
  for (MDOperand *E = O - NumOps; O != E; 
--O9.85M
)
488
9.85M
    (void)new (O - 1) MDOperand;
489
5.17M
  return Ptr;
490
5.17M
}
491
492
595k
void MDNode::operator delete(void *Mem) {
493
595k
  MDNode *N = static_cast<MDNode *>(Mem);
494
595k
  size_t OpSize = N->NumOperands * sizeof(MDOperand);
495
595k
  OpSize = alignTo(OpSize, alignof(uint64_t));
496
595k
497
595k
  MDOperand *O = static_cast<MDOperand *>(Mem);
498
1.28M
  for (MDOperand *E = O - N->NumOperands; O != E; 
--O686k
)
499
686k
    (O - 1)->~MDOperand();
500
595k
  ::operator delete(reinterpret_cast<char *>(Mem) - OpSize);
501
595k
}
502
503
MDNode::MDNode(LLVMContext &Context, unsigned ID, StorageType Storage,
504
               ArrayRef<Metadata *> Ops1, ArrayRef<Metadata *> Ops2)
505
    : Metadata(ID, Storage), NumOperands(Ops1.size() + Ops2.size()),
506
5.17M
      NumUnresolved(0), Context(Context) {
507
5.17M
  unsigned Op = 0;
508
5.17M
  for (Metadata *MD : Ops1)
509
9.85M
    setOperand(Op++, MD);
510
5.17M
  for (Metadata *MD : Ops2)
511
35
    setOperand(Op++, MD);
512
5.17M
513
5.17M
  if (!isUniqued())
514
1.21M
    return;
515
3.95M
516
3.95M
  // Count the unresolved operands.  If there are any, RAUW support will be
517
3.95M
  // added lazily on first reference.
518
3.95M
  countUnresolvedOperands();
519
3.95M
}
520
521
2.62k
TempMDNode MDNode::clone() const {
522
2.62k
  switch (getMetadataID()) {
523
2.62k
  default:
524
0
    llvm_unreachable("Invalid MDNode subclass");
525
2.62k
#define HANDLE_MDNODE_LEAF(CLASS)                                              \
526
2.62k
  case CLASS##Kind:                                                            \
527
2.62k
    return cast<CLASS>(this)->cloneImpl();
528
2.62k
#include 
"llvm/IR/Metadata.def"50
529
2.62k
  }
530
2.62k
}
531
532
7.58M
static bool isOperandUnresolved(Metadata *Op) {
533
7.58M
  if (auto *N = dyn_cast_or_null<MDNode>(Op))
534
6.03M
    return !N->isResolved();
535
1.55M
  return false;
536
1.55M
}
537
538
3.95M
void MDNode::countUnresolvedOperands() {
539
3.95M
  assert(NumUnresolved == 0 && "Expected unresolved ops to be uncounted");
540
3.95M
  assert(isUniqued() && "Expected this to be uniqued");
541
3.95M
  NumUnresolved = count_if(operands(), isOperandUnresolved);
542
3.95M
}
543
544
1.16k
void MDNode::makeUniqued() {
545
1.16k
  assert(isTemporary() && "Expected this to be temporary");
546
1.16k
  assert(!isResolved() && "Expected this to be unresolved");
547
1.16k
548
1.16k
  // Enable uniquing callbacks.
549
1.16k
  for (auto &Op : mutable_operands())
550
6.04k
    Op.reset(Op.get(), this);
551
1.16k
552
1.16k
  // Make this 'uniqued'.
553
1.16k
  Storage = Uniqued;
554
1.16k
  countUnresolvedOperands();
555
1.16k
  if (!NumUnresolved) {
556
1.04k
    dropReplaceableUses();
557
1.04k
    assert(isResolved() && "Expected this to be resolved");
558
1.04k
  }
559
1.16k
560
1.16k
  assert(isUniqued() && "Expected this to be uniqued");
561
1.16k
}
562
563
1.17k
void MDNode::makeDistinct() {
564
1.17k
  assert(isTemporary() && "Expected this to be temporary");
565
1.17k
  assert(!isResolved() && "Expected this to be unresolved");
566
1.17k
567
1.17k
  // Drop RAUW support and store as a distinct node.
568
1.17k
  dropReplaceableUses();
569
1.17k
  storeDistinctInContext();
570
1.17k
571
1.17k
  assert(isDistinct() && "Expected this to be distinct");
572
1.17k
  assert(isResolved() && "Expected this to be resolved");
573
1.17k
}
574
575
48.0k
void MDNode::resolve() {
576
48.0k
  assert(isUniqued() && "Expected this to be uniqued");
577
48.0k
  assert(!isResolved() && "Expected this to be unresolved");
578
48.0k
579
48.0k
  NumUnresolved = 0;
580
48.0k
  dropReplaceableUses();
581
48.0k
582
48.0k
  assert(isResolved() && "Expected this to be resolved");
583
48.0k
}
584
585
74.9k
void MDNode::dropReplaceableUses() {
586
74.9k
  assert(!NumUnresolved && "Unexpected unresolved operand");
587
74.9k
588
74.9k
  // Drop any RAUW support.
589
74.9k
  if (Context.hasReplaceableUses())
590
73.9k
    Context.takeReplaceableUses()->resolveAllUses();
591
74.9k
}
592
593
59.9k
void MDNode::resolveAfterOperandChange(Metadata *Old, Metadata *New) {
594
59.9k
  assert(isUniqued() && "Expected this to be uniqued");
595
59.9k
  assert(NumUnresolved != 0 && "Expected unresolved operands");
596
59.9k
597
59.9k
  // Check if an operand was resolved.
598
59.9k
  if (!isOperandUnresolved(Old)) {
599
2
    if (isOperandUnresolved(New))
600
1
      // An operand was un-resolved!
601
1
      ++NumUnresolved;
602
59.9k
  } else if (!isOperandUnresolved(New))
603
25.5k
    decrementUnresolvedOperandCount();
604
59.9k
}
605
606
64.2k
void MDNode::decrementUnresolvedOperandCount() {
607
64.2k
  assert(!isResolved() && "Expected this to be unresolved");
608
64.2k
  if (isTemporary())
609
0
    return;
610
64.2k
611
64.2k
  assert(isUniqued() && "Expected this to be uniqued");
612
64.2k
  if (--NumUnresolved)
613
39.5k
    return;
614
24.6k
615
24.6k
  // Last unresolved operand has just been resolved.
616
24.6k
  dropReplaceableUses();
617
24.6k
  assert(isResolved() && "Expected this to become resolved");
618
24.6k
}
619
620
2.04k
void MDNode::resolveCycles() {
621
2.04k
  if (isResolved())
622
1.39k
    return;
623
651
624
651
  // Resolve this node immediately.
625
651
  resolve();
626
651
627
651
  // Resolve all operands.
628
4.11k
  for (const auto &Op : operands()) {
629
4.11k
    auto *N = dyn_cast_or_null<MDNode>(Op);
630
4.11k
    if (!N)
631
2.65k
      continue;
632
1.46k
633
1.46k
    assert(!N->isTemporary() &&
634
1.46k
           "Expected all forward declarations to be resolved");
635
1.46k
    if (!N->isResolved())
636
441
      N->resolveCycles();
637
1.46k
  }
638
651
}
639
640
3
static bool hasSelfReference(MDNode *N) {
641
3
  for (Metadata *MD : N->operands())
642
3
    if (MD == N)
643
1
      return true;
644
3
  
return false2
;
645
3
}
646
647
4
MDNode *MDNode::replaceWithPermanentImpl() {
648
4
  switch (getMetadataID()) {
649
4
  default:
650
1
    // If this type isn't uniquable, replace with a distinct node.
651
1
    return replaceWithDistinctImpl();
652
4
653
4
#define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS)                                    \
654
4
  case CLASS##Kind:                                                            \
655
3
    break;
656
4
#include 
"llvm/IR/Metadata.def"3
657
4
  }
658
4
659
4
  // Even if this type is uniquable, self-references have to be distinct.
660
4
  
if (3
hasSelfReference(this)3
)
661
1
    return replaceWithDistinctImpl();
662
2
  return replaceWithUniquedImpl();
663
2
}
664
665
2.76k
MDNode *MDNode::replaceWithUniquedImpl() {
666
2.76k
  // Try to uniquify in place.
667
2.76k
  MDNode *UniquedNode = uniquify();
668
2.76k
669
2.76k
  if (UniquedNode == this) {
670
1.16k
    makeUniqued();
671
1.16k
    return this;
672
1.16k
  }
673
1.59k
674
1.59k
  // Collision, so RAUW instead.
675
1.59k
  replaceAllUsesWith(UniquedNode);
676
1.59k
  deleteAsSubclass();
677
1.59k
  return UniquedNode;
678
1.59k
}
679
680
1.17k
MDNode *MDNode::replaceWithDistinctImpl() {
681
1.17k
  makeDistinct();
682
1.17k
  return this;
683
1.17k
}
684
685
50.2k
void MDTuple::recalculateHash() {
686
50.2k
  setHash(MDTupleInfo::KeyTy::calculateHash(this));
687
50.2k
}
688
689
750k
void MDNode::dropAllReferences() {
690
1.80M
  for (unsigned I = 0, E = NumOperands; I != E; 
++I1.05M
)
691
1.05M
    setOperand(I, nullptr);
692
750k
  if (Context.hasReplaceableUses()) {
693
337k
    Context.getReplaceableUses()->resolveAllUses(/* ResolveUsers */ false);
694
337k
    (void)Context.takeReplaceableUses();
695
337k
  }
696
750k
}
697
698
132k
void MDNode::handleChangedOperand(void *Ref, Metadata *New) {
699
132k
  unsigned Op = static_cast<MDOperand *>(Ref) - op_begin();
700
132k
  assert(Op < getNumOperands() && "Expected valid operand");
701
132k
702
132k
  if (!isUniqued()) {
703
10.3k
    // This node is not uniqued.  Just set the operand and be done with it.
704
10.3k
    setOperand(Op, New);
705
10.3k
    return;
706
10.3k
  }
707
121k
708
121k
  // This node is uniqued.
709
121k
  eraseFromStore();
710
121k
711
121k
  Metadata *Old = getOperand(Op);
712
121k
  setOperand(Op, New);
713
121k
714
121k
  // Drop uniquing for self-reference cycles and deleted constants.
715
121k
  if (New == this || 
(61.6k
!New61.6k
&&
Old1.45k
&&
isa<ConstantAsMetadata>(Old)1.45k
)) {
716
61.6k
    if (!isResolved())
717
47.4k
      resolve();
718
61.6k
    storeDistinctInContext();
719
61.6k
    return;
720
61.6k
  }
721
60.2k
722
60.2k
  // Re-unique the node.
723
60.2k
  auto *Uniqued = uniquify();
724
60.2k
  if (Uniqued == this) {
725
60.1k
    if (!isResolved())
726
59.9k
      resolveAfterOperandChange(Old, New);
727
60.1k
    return;
728
60.1k
  }
729
87
730
87
  // Collision.
731
87
  if (!isResolved()) {
732
85
    // Still unresolved, so RAUW.
733
85
    //
734
85
    // First, clear out all operands to prevent any recursion (similar to
735
85
    // dropAllReferences(), but we still need the use-list).
736
344
    for (unsigned O = 0, E = getNumOperands(); O != E; 
++O259
)
737
259
      setOperand(O, nullptr);
738
85
    if (Context.hasReplaceableUses())
739
85
      Context.getReplaceableUses()->replaceAllUsesWith(Uniqued);
740
85
    deleteAsSubclass();
741
85
    return;
742
85
  }
743
2
744
2
  // Store in non-uniqued form if RAUW isn't possible.
745
2
  storeDistinctInContext();
746
2
}
747
748
408k
void MDNode::deleteAsSubclass() {
749
408k
  switch (getMetadataID()) {
750
408k
  default:
751
0
    llvm_unreachable("Invalid subclass of MDNode");
752
408k
#define HANDLE_MDNODE_LEAF(CLASS)                                              \
753
408k
  case CLASS##Kind:                                                            \
754
408k
    delete cast<CLASS>(this);                                                  \
755
408k
    break;
756
408k
#include 
"llvm/IR/Metadata.def"389k
757
408k
  }
758
408k
}
759
760
template <class T, class InfoT>
761
62.9k
static T *uniquifyImpl(T *N, DenseSet<T *, InfoT> &Store) {
762
62.9k
  if (T *U = getUniqued(Store, N))
763
1.68k
    return U;
764
61.3k
765
61.3k
  Store.insert(N);
766
61.3k
  return N;
767
61.3k
}
Metadata.cpp:llvm::MDTuple* uniquifyImpl<llvm::MDTuple, llvm::MDNodeInfo<llvm::MDTuple> >(llvm::MDTuple*, llvm::DenseSet<llvm::MDTuple*, llvm::MDNodeInfo<llvm::MDTuple> >&)
Line
Count
Source
761
50.2k
static T *uniquifyImpl(T *N, DenseSet<T *, InfoT> &Store) {
762
50.2k
  if (T *U = getUniqued(Store, N))
763
58
    return U;
764
50.2k
765
50.2k
  Store.insert(N);
766
50.2k
  return N;
767
50.2k
}
Metadata.cpp:llvm::DILocation* uniquifyImpl<llvm::DILocation, llvm::MDNodeInfo<llvm::DILocation> >(llvm::DILocation*, llvm::DenseSet<llvm::DILocation*, llvm::MDNodeInfo<llvm::DILocation> >&)
Line
Count
Source
761
1.46k
static T *uniquifyImpl(T *N, DenseSet<T *, InfoT> &Store) {
762
1.46k
  if (T *U = getUniqued(Store, N))
763
0
    return U;
764
1.46k
765
1.46k
  Store.insert(N);
766
1.46k
  return N;
767
1.46k
}
Metadata.cpp:llvm::DIExpression* uniquifyImpl<llvm::DIExpression, llvm::MDNodeInfo<llvm::DIExpression> >(llvm::DIExpression*, llvm::DenseSet<llvm::DIExpression*, llvm::MDNodeInfo<llvm::DIExpression> >&)
Line
Count
Source
761
1
static T *uniquifyImpl(T *N, DenseSet<T *, InfoT> &Store) {
762
1
  if (T *U = getUniqued(Store, N))
763
1
    return U;
764
0
765
0
  Store.insert(N);
766
0
  return N;
767
0
}
Metadata.cpp:llvm::DIGlobalVariableExpression* uniquifyImpl<llvm::DIGlobalVariableExpression, llvm::MDNodeInfo<llvm::DIGlobalVariableExpression> >(llvm::DIGlobalVariableExpression*, llvm::DenseSet<llvm::DIGlobalVariableExpression*, llvm::MDNodeInfo<llvm::DIGlobalVariableExpression> >&)
Line
Count
Source
761
1.07k
static T *uniquifyImpl(T *N, DenseSet<T *, InfoT> &Store) {
762
1.07k
  if (T *U = getUniqued(Store, N))
763
1
    return U;
764
1.07k
765
1.07k
  Store.insert(N);
766
1.07k
  return N;
767
1.07k
}
Metadata.cpp:llvm::GenericDINode* uniquifyImpl<llvm::GenericDINode, llvm::MDNodeInfo<llvm::GenericDINode> >(llvm::GenericDINode*, llvm::DenseSet<llvm::GenericDINode*, llvm::MDNodeInfo<llvm::GenericDINode> >&)
Line
Count
Source
761
15
static T *uniquifyImpl(T *N, DenseSet<T *, InfoT> &Store) {
762
15
  if (T *U = getUniqued(Store, N))
763
1
    return U;
764
14
765
14
  Store.insert(N);
766
14
  return N;
767
14
}
Metadata.cpp:llvm::DISubrange* uniquifyImpl<llvm::DISubrange, llvm::MDNodeInfo<llvm::DISubrange> >(llvm::DISubrange*, llvm::DenseSet<llvm::DISubrange*, llvm::MDNodeInfo<llvm::DISubrange> >&)
Line
Count
Source
761
11
static T *uniquifyImpl(T *N, DenseSet<T *, InfoT> &Store) {
762
11
  if (T *U = getUniqued(Store, N))
763
1
    return U;
764
10
765
10
  Store.insert(N);
766
10
  return N;
767
10
}
Metadata.cpp:llvm::DIEnumerator* uniquifyImpl<llvm::DIEnumerator, llvm::MDNodeInfo<llvm::DIEnumerator> >(llvm::DIEnumerator*, llvm::DenseSet<llvm::DIEnumerator*, llvm::MDNodeInfo<llvm::DIEnumerator> >&)
Line
Count
Source
761
1
static T *uniquifyImpl(T *N, DenseSet<T *, InfoT> &Store) {
762
1
  if (T *U = getUniqued(Store, N))
763
1
    return U;
764
0
765
0
  Store.insert(N);
766
0
  return N;
767
0
}
Metadata.cpp:llvm::DIBasicType* uniquifyImpl<llvm::DIBasicType, llvm::MDNodeInfo<llvm::DIBasicType> >(llvm::DIBasicType*, llvm::DenseSet<llvm::DIBasicType*, llvm::MDNodeInfo<llvm::DIBasicType> >&)
Line
Count
Source
761
2
static T *uniquifyImpl(T *N, DenseSet<T *, InfoT> &Store) {
762
2
  if (T *U = getUniqued(Store, N))
763
2
    return U;
764
0
765
0
  Store.insert(N);
766
0
  return N;
767
0
}
Metadata.cpp:llvm::DIDerivedType* uniquifyImpl<llvm::DIDerivedType, llvm::MDNodeInfo<llvm::DIDerivedType> >(llvm::DIDerivedType*, llvm::DenseSet<llvm::DIDerivedType*, llvm::MDNodeInfo<llvm::DIDerivedType> >&)
Line
Count
Source
761
4.08k
static T *uniquifyImpl(T *N, DenseSet<T *, InfoT> &Store) {
762
4.08k
  if (T *U = getUniqued(Store, N))
763
1.57k
    return U;
764
2.51k
765
2.51k
  Store.insert(N);
766
2.51k
  return N;
767
2.51k
}
Metadata.cpp:llvm::DICompositeType* uniquifyImpl<llvm::DICompositeType, llvm::MDNodeInfo<llvm::DICompositeType> >(llvm::DICompositeType*, llvm::DenseSet<llvm::DICompositeType*, llvm::MDNodeInfo<llvm::DICompositeType> >&)
Line
Count
Source
761
1.29k
static T *uniquifyImpl(T *N, DenseSet<T *, InfoT> &Store) {
762
1.29k
  if (T *U = getUniqued(Store, N))
763
11
    return U;
764
1.28k
765
1.28k
  Store.insert(N);
766
1.28k
  return N;
767
1.28k
}
Metadata.cpp:llvm::DISubroutineType* uniquifyImpl<llvm::DISubroutineType, llvm::MDNodeInfo<llvm::DISubroutineType> >(llvm::DISubroutineType*, llvm::DenseSet<llvm::DISubroutineType*, llvm::MDNodeInfo<llvm::DISubroutineType> >&)
Line
Count
Source
761
2.00k
static T *uniquifyImpl(T *N, DenseSet<T *, InfoT> &Store) {
762
2.00k
  if (T *U = getUniqued(Store, N))
763
22
    return U;
764
1.98k
765
1.98k
  Store.insert(N);
766
1.98k
  return N;
767
1.98k
}
Metadata.cpp:llvm::DIFile* uniquifyImpl<llvm::DIFile, llvm::MDNodeInfo<llvm::DIFile> >(llvm::DIFile*, llvm::DenseSet<llvm::DIFile*, llvm::MDNodeInfo<llvm::DIFile> >&)
Line
Count
Source
761
1
static T *uniquifyImpl(T *N, DenseSet<T *, InfoT> &Store) {
762
1
  if (T *U = getUniqued(Store, N))
763
1
    return U;
764
0
765
0
  Store.insert(N);
766
0
  return N;
767
0
}
Metadata.cpp:llvm::DISubprogram* uniquifyImpl<llvm::DISubprogram, llvm::MDNodeInfo<llvm::DISubprogram> >(llvm::DISubprogram*, llvm::DenseSet<llvm::DISubprogram*, llvm::MDNodeInfo<llvm::DISubprogram> >&)
Line
Count
Source
761
571
static T *uniquifyImpl(T *N, DenseSet<T *, InfoT> &Store) {
762
571
  if (T *U = getUniqued(Store, N))
763
2
    return U;
764
569
765
569
  Store.insert(N);
766
569
  return N;
767
569
}
Metadata.cpp:llvm::DILexicalBlock* uniquifyImpl<llvm::DILexicalBlock, llvm::MDNodeInfo<llvm::DILexicalBlock> >(llvm::DILexicalBlock*, llvm::DenseSet<llvm::DILexicalBlock*, llvm::MDNodeInfo<llvm::DILexicalBlock> >&)
Line
Count
Source
761
1
static T *uniquifyImpl(T *N, DenseSet<T *, InfoT> &Store) {
762
1
  if (T *U = getUniqued(Store, N))
763
1
    return U;
764
0
765
0
  Store.insert(N);
766
0
  return N;
767
0
}
Metadata.cpp:llvm::DILexicalBlockFile* uniquifyImpl<llvm::DILexicalBlockFile, llvm::MDNodeInfo<llvm::DILexicalBlockFile> >(llvm::DILexicalBlockFile*, llvm::DenseSet<llvm::DILexicalBlockFile*, llvm::MDNodeInfo<llvm::DILexicalBlockFile> >&)
Line
Count
Source
761
68
static T *uniquifyImpl(T *N, DenseSet<T *, InfoT> &Store) {
762
68
  if (T *U = getUniqued(Store, N))
763
1
    return U;
764
67
765
67
  Store.insert(N);
766
67
  return N;
767
67
}
Metadata.cpp:llvm::DINamespace* uniquifyImpl<llvm::DINamespace, llvm::MDNodeInfo<llvm::DINamespace> >(llvm::DINamespace*, llvm::DenseSet<llvm::DINamespace*, llvm::MDNodeInfo<llvm::DINamespace> >&)
Line
Count
Source
761
13
static T *uniquifyImpl(T *N, DenseSet<T *, InfoT> &Store) {
762
13
  if (T *U = getUniqued(Store, N))
763
1
    return U;
764
12
765
12
  Store.insert(N);
766
12
  return N;
767
12
}
Metadata.cpp:llvm::DIModule* uniquifyImpl<llvm::DIModule, llvm::MDNodeInfo<llvm::DIModule> >(llvm::DIModule*, llvm::DenseSet<llvm::DIModule*, llvm::MDNodeInfo<llvm::DIModule> >&)
Line
Count
Source
761
1
static T *uniquifyImpl(T *N, DenseSet<T *, InfoT> &Store) {
762
1
  if (T *U = getUniqued(Store, N))
763
1
    return U;
764
0
765
0
  Store.insert(N);
766
0
  return N;
767
0
}
Metadata.cpp:llvm::DITemplateTypeParameter* uniquifyImpl<llvm::DITemplateTypeParameter, llvm::MDNodeInfo<llvm::DITemplateTypeParameter> >(llvm::DITemplateTypeParameter*, llvm::DenseSet<llvm::DITemplateTypeParameter*, llvm::MDNodeInfo<llvm::DITemplateTypeParameter> >&)
Line
Count
Source
761
12
static T *uniquifyImpl(T *N, DenseSet<T *, InfoT> &Store) {
762
12
  if (T *U = getUniqued(Store, N))
763
1
    return U;
764
11
765
11
  Store.insert(N);
766
11
  return N;
767
11
}
Metadata.cpp:llvm::DITemplateValueParameter* uniquifyImpl<llvm::DITemplateValueParameter, llvm::MDNodeInfo<llvm::DITemplateValueParameter> >(llvm::DITemplateValueParameter*, llvm::DenseSet<llvm::DITemplateValueParameter*, llvm::MDNodeInfo<llvm::DITemplateValueParameter> >&)
Line
Count
Source
761
25
static T *uniquifyImpl(T *N, DenseSet<T *, InfoT> &Store) {
762
25
  if (T *U = getUniqued(Store, N))
763
1
    return U;
764
24
765
24
  Store.insert(N);
766
24
  return N;
767
24
}
Metadata.cpp:llvm::DIGlobalVariable* uniquifyImpl<llvm::DIGlobalVariable, llvm::MDNodeInfo<llvm::DIGlobalVariable> >(llvm::DIGlobalVariable*, llvm::DenseSet<llvm::DIGlobalVariable*, llvm::MDNodeInfo<llvm::DIGlobalVariable> >&)
Line
Count
Source
761
652
static T *uniquifyImpl(T *N, DenseSet<T *, InfoT> &Store) {
762
652
  if (T *U = getUniqued(Store, N))
763
1
    return U;
764
651
765
651
  Store.insert(N);
766
651
  return N;
767
651
}
Metadata.cpp:llvm::DILocalVariable* uniquifyImpl<llvm::DILocalVariable, llvm::MDNodeInfo<llvm::DILocalVariable> >(llvm::DILocalVariable*, llvm::DenseSet<llvm::DILocalVariable*, llvm::MDNodeInfo<llvm::DILocalVariable> >&)
Line
Count
Source
761
1.15k
static T *uniquifyImpl(T *N, DenseSet<T *, InfoT> &Store) {
762
1.15k
  if (T *U = getUniqued(Store, N))
763
1
    return U;
764
1.15k
765
1.15k
  Store.insert(N);
766
1.15k
  return N;
767
1.15k
}
Metadata.cpp:llvm::DILabel* uniquifyImpl<llvm::DILabel, llvm::MDNodeInfo<llvm::DILabel> >(llvm::DILabel*, llvm::DenseSet<llvm::DILabel*, llvm::MDNodeInfo<llvm::DILabel> >&)
Line
Count
Source
761
3
static T *uniquifyImpl(T *N, DenseSet<T *, InfoT> &Store) {
762
3
  if (T *U = getUniqued(Store, N))
763
0
    return U;
764
3
765
3
  Store.insert(N);
766
3
  return N;
767
3
}
Metadata.cpp:llvm::DIObjCProperty* uniquifyImpl<llvm::DIObjCProperty, llvm::MDNodeInfo<llvm::DIObjCProperty> >(llvm::DIObjCProperty*, llvm::DenseSet<llvm::DIObjCProperty*, llvm::MDNodeInfo<llvm::DIObjCProperty> >&)
Line
Count
Source
761
1
static T *uniquifyImpl(T *N, DenseSet<T *, InfoT> &Store) {
762
1
  if (T *U = getUniqued(Store, N))
763
1
    return U;
764
0
765
0
  Store.insert(N);
766
0
  return N;
767
0
}
Metadata.cpp:llvm::DIImportedEntity* uniquifyImpl<llvm::DIImportedEntity, llvm::MDNodeInfo<llvm::DIImportedEntity> >(llvm::DIImportedEntity*, llvm::DenseSet<llvm::DIImportedEntity*, llvm::MDNodeInfo<llvm::DIImportedEntity> >&)
Line
Count
Source
761
239
static T *uniquifyImpl(T *N, DenseSet<T *, InfoT> &Store) {
762
239
  if (T *U = getUniqued(Store, N))
763
1
    return U;
764
238
765
238
  Store.insert(N);
766
238
  return N;
767
238
}
Unexecuted instantiation: Metadata.cpp:llvm::DIMacro* uniquifyImpl<llvm::DIMacro, llvm::MDNodeInfo<llvm::DIMacro> >(llvm::DIMacro*, llvm::DenseSet<llvm::DIMacro*, llvm::MDNodeInfo<llvm::DIMacro> >&)
Metadata.cpp:llvm::DIMacroFile* uniquifyImpl<llvm::DIMacroFile, llvm::MDNodeInfo<llvm::DIMacroFile> >(llvm::DIMacroFile*, llvm::DenseSet<llvm::DIMacroFile*, llvm::MDNodeInfo<llvm::DIMacroFile> >&)
Line
Count
Source
761
14
static T *uniquifyImpl(T *N, DenseSet<T *, InfoT> &Store) {
762
14
  if (T *U = getUniqued(Store, N))
763
1
    return U;
764
13
765
13
  Store.insert(N);
766
13
  return N;
767
13
}
Metadata.cpp:llvm::DICommonBlock* uniquifyImpl<llvm::DICommonBlock, llvm::MDNodeInfo<llvm::DICommonBlock> >(llvm::DICommonBlock*, llvm::DenseSet<llvm::DICommonBlock*, llvm::MDNodeInfo<llvm::DICommonBlock> >&)
Line
Count
Source
761
4
static T *uniquifyImpl(T *N, DenseSet<T *, InfoT> &Store) {
762
4
  if (T *U = getUniqued(Store, N))
763
0
    return U;
764
4
765
4
  Store.insert(N);
766
4
  return N;
767
4
}
768
769
template <class NodeTy> struct MDNode::HasCachedHash {
770
  using Yes = char[1];
771
  using No = char[2];
772
  template <class U, U Val> struct SFINAE {};
773
774
  template <class U>
775
  static Yes &check(SFINAE<void (U::*)(unsigned), &U::setHash> *);
776
  template <class U> static No &check(...);
777
778
  static const bool value = sizeof(check<NodeTy>(nullptr)) == sizeof(Yes);
779
};
780
781
62.9k
MDNode *MDNode::uniquify() {
782
62.9k
  assert(!hasSelfReference(this) && "Cannot uniquify a self-referencing node");
783
62.9k
784
62.9k
  // Try to insert into uniquing store.
785
62.9k
  switch (getMetadataID()) {
786
62.9k
  default:
787
0
    llvm_unreachable("Invalid or non-uniquable subclass of MDNode");
788
62.9k
#define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS)                                    \
789
62.9k
  case CLASS##Kind: {                                                          \
790
62.9k
    CLASS *SubclassThis = cast<CLASS>(this);                                   \
791
62.9k
    std::integral_constant<bool, HasCachedHash<CLASS>::value>                  \
792
62.9k
        ShouldRecalculateHash;                                                 \
793
62.9k
    dispatchRecalculateHash(SubclassThis, ShouldRecalculateHash);              \
794
62.9k
    return uniquifyImpl(SubclassThis, getContext().pImpl->CLASS##s);           \
795
62.9k
  }
796
62.9k
#include 
"llvm/IR/Metadata.def"50.2k
797
62.9k
  }
798
62.9k
}
799
800
121k
void MDNode::eraseFromStore() {
801
121k
  switch (getMetadataID()) {
802
121k
  default:
803
0
    llvm_unreachable("Invalid or non-uniquable subclass of MDNode");
804
121k
#define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS)                                    \
805
121k
  case CLASS##Kind:                                                            \
806
121k
    getContext().pImpl->CLASS##s.erase(cast<CLASS>(this));                     \
807
121k
    break;
808
121k
#include 
"llvm/IR/Metadata.def"111k
809
121k
  }
810
121k
}
811
812
MDTuple *MDTuple::getImpl(LLVMContext &Context, ArrayRef<Metadata *> MDs,
813
1.98M
                          StorageType Storage, bool ShouldCreate) {
814
1.98M
  unsigned Hash = 0;
815
1.98M
  if (Storage == Uniqued) {
816
1.50M
    MDTupleInfo::KeyTy Key(MDs);
817
1.50M
    if (auto *N = getUniqued(Context.pImpl->MDTuples, Key))
818
820k
      return N;
819
689k
    if (!ShouldCreate)
820
1
      return nullptr;
821
689k
    Hash = Key.getHash();
822
689k
  } else {
823
473k
    assert(ShouldCreate && "Expected non-uniqued nodes to always be created");
824
473k
  }
825
1.98M
826
1.98M
  return storeImpl(new (MDs.size()) MDTuple(Context, Storage, Hash, MDs),
827
1.16M
                   Storage, Context.pImpl->MDTuples);
828
1.98M
}
829
830
363k
void MDNode::deleteTemporary(MDNode *N) {
831
363k
  assert(N->isTemporary() && "Expected temporary node");
832
363k
  N->replaceAllUsesWith(nullptr);
833
363k
  N->deleteAsSubclass();
834
363k
}
835
836
914k
void MDNode::storeDistinctInContext() {
837
914k
  assert(!Context.hasReplaceableUses() && "Unexpected replaceable uses");
838
914k
  assert(!NumUnresolved && "Unexpected unresolved nodes");
839
914k
  Storage = Distinct;
840
914k
  assert(isResolved() && "Expected this to be resolved");
841
914k
842
914k
  // Reset the hash.
843
914k
  switch (getMetadataID()) {
844
914k
  default:
845
0
    llvm_unreachable("Invalid subclass of MDNode");
846
914k
#define HANDLE_MDNODE_LEAF(CLASS)                                              \
847
914k
  case CLASS##Kind: {                                                          \
848
914k
    std::integral_constant<bool, HasCachedHash<CLASS>::value> ShouldResetHash; \
849
914k
    dispatchResetHash(cast<CLASS>(this), ShouldResetHash);                     \
850
914k
    break;                                                                     \
851
914k
  }
852
914k
#include 
"llvm/IR/Metadata.def"171k
853
914k
  }
854
914k
855
914k
  getContext().pImpl->DistinctMDNodes.push_back(this);
856
914k
}
857
858
133k
void MDNode::replaceOperandWith(unsigned I, Metadata *New) {
859
133k
  if (getOperand(I) == New)
860
756
    return;
861
132k
862
132k
  if (!isUniqued()) {
863
93.0k
    setOperand(I, New);
864
93.0k
    return;
865
93.0k
  }
866
39.7k
867
39.7k
  handleChangedOperand(mutable_begin() + I, New);
868
39.7k
}
869
870
11.1M
void MDNode::setOperand(unsigned I, Metadata *New) {
871
11.1M
  assert(I < NumOperands);
872
11.1M
  mutable_begin()[I].reset(New, isUniqued() ? 
this8.39M
:
nullptr2.73M
);
873
11.1M
}
874
875
/// Get a node or a self-reference that looks like it.
876
///
877
/// Special handling for finding self-references, for use by \a
878
/// MDNode::concatenate() and \a MDNode::intersect() to maintain behaviour from
879
/// when self-referencing nodes were still uniqued.  If the first operand has
880
/// the same operands as \c Ops, return the first operand instead.
881
static MDNode *getOrSelfReference(LLVMContext &Context,
882
42.5k
                                  ArrayRef<Metadata *> Ops) {
883
42.5k
  if (!Ops.empty())
884
29.3k
    if (MDNode *N = dyn_cast_or_null<MDNode>(Ops[0]))
885
29.3k
      if (N->getNumOperands() == Ops.size() && 
N == N->getOperand(0)7.12k
) {
886
7.12k
        for (unsigned I = 1, E = Ops.size(); I != E; 
++I0
)
887
7.12k
          if (Ops[I] != N->getOperand(I))
888
7.12k
            return MDNode::get(Context, Ops);
889
7.12k
        
return N2
;
890
35.4k
      }
891
35.4k
892
35.4k
  return MDNode::get(Context, Ops);
893
35.4k
}
894
895
77.7k
MDNode *MDNode::concatenate(MDNode *A, MDNode *B) {
896
77.7k
  if (!A)
897
50.3k
    return B;
898
27.4k
  if (!B)
899
2
    return A;
900
27.4k
901
27.4k
  SmallSetVector<Metadata *, 4> MDs(A->op_begin(), A->op_end());
902
27.4k
  MDs.insert(B->op_begin(), B->op_end());
903
27.4k
904
27.4k
  // FIXME: This preserves long-standing behaviour, but is it really the right
905
27.4k
  // behaviour?  Or was that an unintended side-effect of node uniquing?
906
27.4k
  return getOrSelfReference(A->getContext(), MDs.getArrayRef());
907
27.4k
}
908
909
21.6k
MDNode *MDNode::intersect(MDNode *A, MDNode *B) {
910
21.6k
  if (!A || 
!B15.1k
)
911
6.44k
    return nullptr;
912
15.1k
913
15.1k
  SmallSetVector<Metadata *, 4> MDs(A->op_begin(), A->op_end());
914
15.1k
  SmallPtrSet<Metadata *, 4> BSet(B->op_begin(), B->op_end());
915
15.1k
  MDs.remove_if([&](Metadata *MD) 
{ return !is_contained(BSet, MD); }5.31k
);
916
15.1k
917
15.1k
  // FIXME: This preserves long-standing behaviour, but is it really the right
918
15.1k
  // behaviour?  Or was that an unintended side-effect of node uniquing?
919
15.1k
  return getOrSelfReference(A->getContext(), MDs.getArrayRef());
920
15.1k
}
921
922
8.01k
MDNode *MDNode::getMostGenericAliasScope(MDNode *A, MDNode *B) {
923
8.01k
  if (!A || 
!B1.81k
)
924
6.20k
    return nullptr;
925
1.81k
926
1.81k
  return concatenate(A, B);
927
1.81k
}
928
929
12
MDNode *MDNode::getMostGenericFPMath(MDNode *A, MDNode *B) {
930
12
  if (!A || 
!B10
)
931
2
    return nullptr;
932
10
933
10
  APFloat AVal = mdconst::extract<ConstantFP>(A->getOperand(0))->getValueAPF();
934
10
  APFloat BVal = mdconst::extract<ConstantFP>(B->getOperand(0))->getValueAPF();
935
10
  if (AVal.compare(BVal) == APFloat::cmpLessThan)
936
4
    return A;
937
6
  return B;
938
6
}
939
940
2
static bool isContiguous(const ConstantRange &A, const ConstantRange &B) {
941
2
  return A.getUpper() == B.getLower() || A.getLower() == B.getUpper();
942
2
}
943
944
3
static bool canBeMerged(const ConstantRange &A, const ConstantRange &B) {
945
3
  return !A.intersectWith(B).isEmptySet() || 
isContiguous(A, B)2
;
946
3
}
947
948
static bool tryMergeRange(SmallVectorImpl<ConstantInt *> &EndPoints,
949
3
                          ConstantInt *Low, ConstantInt *High) {
950
3
  ConstantRange NewRange(Low->getValue(), High->getValue());
951
3
  unsigned Size = EndPoints.size();
952
3
  APInt LB = EndPoints[Size - 2]->getValue();
953
3
  APInt LE = EndPoints[Size - 1]->getValue();
954
3
  ConstantRange LastRange(LB, LE);
955
3
  if (canBeMerged(NewRange, LastRange)) {
956
1
    ConstantRange Union = LastRange.unionWith(NewRange);
957
1
    Type *Ty = High->getType();
958
1
    EndPoints[Size - 2] =
959
1
        cast<ConstantInt>(ConstantInt::get(Ty, Union.getLower()));
960
1
    EndPoints[Size - 1] =
961
1
        cast<ConstantInt>(ConstantInt::get(Ty, Union.getUpper()));
962
1
    return true;
963
1
  }
964
2
  return false;
965
2
}
966
967
static void addRange(SmallVectorImpl<ConstantInt *> &EndPoints,
968
6
                     ConstantInt *Low, ConstantInt *High) {
969
6
  if (!EndPoints.empty())
970
3
    if (tryMergeRange(EndPoints, Low, High))
971
1
      return;
972
5
973
5
  EndPoints.push_back(Low);
974
5
  EndPoints.push_back(High);
975
5
}
976
977
407
MDNode *MDNode::getMostGenericRange(MDNode *A, MDNode *B) {
978
407
  // Given two ranges, we want to compute the union of the ranges. This
979
407
  // is slightly complicated by having to combine the intervals and merge
980
407
  // the ones that overlap.
981
407
982
407
  if (!A || 
!B405
)
983
2
    return nullptr;
984
405
985
405
  if (A == B)
986
402
    return A;
987
3
988
3
  // First, walk both lists in order of the lower boundary of each interval.
989
3
  // At each step, try to merge the new interval to the last one we adedd.
990
3
  SmallVector<ConstantInt *, 4> EndPoints;
991
3
  int AI = 0;
992
3
  int BI = 0;
993
3
  int AN = A->getNumOperands() / 2;
994
3
  int BN = B->getNumOperands() / 2;
995
6
  while (AI < AN && BI < BN) {
996
3
    ConstantInt *ALow = mdconst::extract<ConstantInt>(A->getOperand(2 * AI));
997
3
    ConstantInt *BLow = mdconst::extract<ConstantInt>(B->getOperand(2 * BI));
998
3
999
3
    if (ALow->getValue().slt(BLow->getValue())) {
1000
0
      addRange(EndPoints, ALow,
1001
0
               mdconst::extract<ConstantInt>(A->getOperand(2 * AI + 1)));
1002
0
      ++AI;
1003
3
    } else {
1004
3
      addRange(EndPoints, BLow,
1005
3
               mdconst::extract<ConstantInt>(B->getOperand(2 * BI + 1)));
1006
3
      ++BI;
1007
3
    }
1008
3
  }
1009
6
  while (AI < AN) {
1010
3
    addRange(EndPoints, mdconst::extract<ConstantInt>(A->getOperand(2 * AI)),
1011
3
             mdconst::extract<ConstantInt>(A->getOperand(2 * AI + 1)));
1012
3
    ++AI;
1013
3
  }
1014
3
  while (BI < BN) {
1015
0
    addRange(EndPoints, mdconst::extract<ConstantInt>(B->getOperand(2 * BI)),
1016
0
             mdconst::extract<ConstantInt>(B->getOperand(2 * BI + 1)));
1017
0
    ++BI;
1018
0
  }
1019
3
1020
3
  // If we have more than 2 ranges (4 endpoints) we have to try to merge
1021
3
  // the last and first ones.
1022
3
  unsigned Size = EndPoints.size();
1023
3
  if (Size > 4) {
1024
0
    ConstantInt *FB = EndPoints[0];
1025
0
    ConstantInt *FE = EndPoints[1];
1026
0
    if (tryMergeRange(EndPoints, FB, FE)) {
1027
0
      for (unsigned i = 0; i < Size - 2; ++i) {
1028
0
        EndPoints[i] = EndPoints[i + 2];
1029
0
      }
1030
0
      EndPoints.resize(Size - 2);
1031
0
    }
1032
0
  }
1033
3
1034
3
  // If in the end we have a single range, it is possible that it is now the
1035
3
  // full range. Just drop the metadata in that case.
1036
3
  if (EndPoints.size() == 2) {
1037
1
    ConstantRange Range(EndPoints[0]->getValue(), EndPoints[1]->getValue());
1038
1
    if (Range.isFullSet())
1039
0
      return nullptr;
1040
3
  }
1041
3
1042
3
  SmallVector<Metadata *, 4> MDs;
1043
3
  MDs.reserve(EndPoints.size());
1044
3
  for (auto *I : EndPoints)
1045
10
    MDs.push_back(ConstantAsMetadata::get(I));
1046
3
  return MDNode::get(A->getContext(), MDs);
1047
3
}
1048
1049
9
MDNode *MDNode::getMostGenericAlignmentOrDereferenceable(MDNode *A, MDNode *B) {
1050
9
  if (!A || !B)
1051
0
    return nullptr;
1052
9
1053
9
  ConstantInt *AVal = mdconst::extract<ConstantInt>(A->getOperand(0));
1054
9
  ConstantInt *BVal = mdconst::extract<ConstantInt>(B->getOperand(0));
1055
9
  if (AVal->getZExtValue() < BVal->getZExtValue())
1056
0
    return A;
1057
9
  return B;
1058
9
}
1059
1060
//===----------------------------------------------------------------------===//
1061
// NamedMDNode implementation.
1062
//
1063
1064
73.9M
static SmallVector<TrackingMDRef, 4> &getNMDOps(void *Operands) {
1065
73.9M
  return *(SmallVector<TrackingMDRef, 4> *)Operands;
1066
73.9M
}
1067
1068
NamedMDNode::NamedMDNode(const Twine &N)
1069
59.7k
    : Name(N.str()), Operands(new SmallVector<TrackingMDRef, 4>()) {}
1070
1071
32.5k
NamedMDNode::~NamedMDNode() {
1072
32.5k
  dropAllReferences();
1073
32.5k
  delete &getNMDOps(Operands);
1074
32.5k
}
1075
1076
16.4M
unsigned NamedMDNode::getNumOperands() const {
1077
16.4M
  return (unsigned)getNMDOps(Operands).size();
1078
16.4M
}
1079
1080
57.2M
MDNode *NamedMDNode::getOperand(unsigned i) const {
1081
57.2M
  assert(i < getNumOperands() && "Invalid Operand number!");
1082
57.2M
  auto *N = getNMDOps(Operands)[i].get();
1083
57.2M
  return cast_or_null<MDNode>(N);
1084
57.2M
}
1085
1086
145k
void NamedMDNode::addOperand(MDNode *M) { getNMDOps(Operands).emplace_back(M); }
1087
1088
358
void NamedMDNode::setOperand(unsigned I, MDNode *New) {
1089
358
  assert(I < getNumOperands() && "Invalid operand number");
1090
358
  getNMDOps(Operands)[I].reset(New);
1091
358
}
1092
1093
752
void NamedMDNode::eraseFromParent() { getParent()->eraseNamedMetadata(this); }
1094
1095
32.6k
void NamedMDNode::clearOperands() { getNMDOps(Operands).clear(); }
1096
1097
184k
StringRef NamedMDNode::getName() const { return StringRef(Name); }
1098
1099
//===----------------------------------------------------------------------===//
1100
// Instruction Metadata method implementations.
1101
//
1102
8.77M
void MDAttachmentMap::set(unsigned ID, MDNode &MD) {
1103
8.77M
  for (auto &I : Attachments)
1104
525k
    if (I.first == ID) {
1105
221k
      I.second.reset(&MD);
1106
221k
      return;
1107
221k
    }
1108
8.77M
  Attachments.emplace_back(std::piecewise_construct, std::make_tuple(ID),
1109
8.55M
                           std::make_tuple(&MD));
1110
8.55M
}
1111
1112
10.6M
bool MDAttachmentMap::erase(unsigned ID) {
1113
10.6M
  if (empty())
1114
0
    return false;
1115
10.6M
1116
10.6M
  // Common case is one/last value.
1117
10.6M
  if (Attachments.back().first == ID) {
1118
1.13k
    Attachments.pop_back();
1119
1.13k
    return true;
1120
1.13k
  }
1121
10.6M
1122
10.8M
  
for (auto I = Attachments.begin(), E = std::prev(Attachments.end()); 10.6M
I != E;
1123
10.6M
       
++I268k
)
1124
268k
    if (I->first == ID) {
1125
26
      *I = std::move(Attachments.back());
1126
26
      Attachments.pop_back();
1127
26
      return true;
1128
26
    }
1129
10.6M
1130
10.6M
  
return false10.6M
;
1131
10.6M
}
1132
1133
315M
MDNode *MDAttachmentMap::lookup(unsigned ID) const {
1134
315M
  for (const auto &I : Attachments)
1135
326M
    if (I.first == ID)
1136
51.6M
      return I.second;
1137
315M
  
return nullptr263M
;
1138
315M
}
1139
1140
void MDAttachmentMap::getAll(
1141
4.46M
    SmallVectorImpl<std::pair<unsigned, MDNode *>> &Result) const {
1142
4.46M
  Result.append(Attachments.begin(), Attachments.end());
1143
4.46M
1144
4.46M
  // Sort the resulting array so it is stable.
1145
4.46M
  if (Result.size() > 1)
1146
617k
    array_pod_sort(Result.begin(), Result.end());
1147
4.46M
}
1148
1149
134k
void MDGlobalAttachmentMap::insert(unsigned ID, MDNode &MD) {
1150
134k
  Attachments.push_back({ID, TrackingMDNodeRef(&MD)});
1151
134k
}
1152
1153
4.23M
MDNode *MDGlobalAttachmentMap::lookup(unsigned ID) const {
1154
4.23M
  for (const auto &A : Attachments)
1155
4.27M
    if (A.MDKind == ID)
1156
3.54M
      return A.Node;
1157
4.23M
  
return nullptr696k
;
1158
4.23M
}
1159
1160
void MDGlobalAttachmentMap::get(unsigned ID,
1161
7.05k
                                SmallVectorImpl<MDNode *> &Result) const {
1162
7.05k
  for (const auto &A : Attachments)
1163
8.12k
    if (A.MDKind == ID)
1164
6.34k
      Result.push_back(A.Node);
1165
7.05k
}
1166
1167
3.87k
bool MDGlobalAttachmentMap::erase(unsigned ID) {
1168
3.87k
  auto I = std::remove_if(Attachments.begin(), Attachments.end(),
1169
6.92k
                          [ID](const Attachment &A) { return A.MDKind == ID; });
1170
3.87k
  bool Changed = I != Attachments.end();
1171
3.87k
  Attachments.erase(I, Attachments.end());
1172
3.87k
  return Changed;
1173
3.87k
}
1174
1175
void MDGlobalAttachmentMap::getAll(
1176
36.4k
    SmallVectorImpl<std::pair<unsigned, MDNode *>> &Result) const {
1177
36.4k
  for (const auto &A : Attachments)
1178
48.9k
    Result.emplace_back(A.MDKind, A.Node);
1179
36.4k
1180
36.4k
  // Sort the resulting array so it is stable with respect to metadata IDs. We
1181
36.4k
  // need to preserve the original insertion order though.
1182
36.4k
  llvm::stable_sort(Result, less_first());
1183
36.4k
}
1184
1185
6.55M
void Instruction::setMetadata(StringRef Kind, MDNode *Node) {
1186
6.55M
  if (!Node && 
!hasMetadata()6.44M
)
1187
6.44M
    return;
1188
110k
  setMetadata(getContext().getMDKindID(Kind), Node);
1189
110k
}
1190
1191
21.0k
MDNode *Instruction::getMetadataImpl(StringRef Kind) const {
1192
21.0k
  return getMetadataImpl(getContext().getMDKindID(Kind));
1193
21.0k
}
1194
1195
340k
void Instruction::dropUnknownNonDebugMetadata(ArrayRef<unsigned> KnownIDs) {
1196
340k
  if (!hasMetadataHashEntry())
1197
253k
    return; // Nothing to remove!
1198
86.5k
1199
86.5k
  auto &InstructionMetadata = getContext().pImpl->InstructionMetadata;
1200
86.5k
1201
86.5k
  SmallSet<unsigned, 4> KnownSet;
1202
86.5k
  KnownSet.insert(KnownIDs.begin(), KnownIDs.end());
1203
86.5k
  if (KnownSet.empty()) {
1204
21.9k
    // Just drop our entry at the store.
1205
21.9k
    InstructionMetadata.erase(this);
1206
21.9k
    setHasMetadataHashEntry(false);
1207
21.9k
    return;
1208
21.9k
  }
1209
64.5k
1210
64.5k
  auto &Info = InstructionMetadata[this];
1211
66.7k
  Info.remove_if([&KnownSet](const std::pair<unsigned, TrackingMDNodeRef> &I) {
1212
66.7k
    return !KnownSet.count(I.first);
1213
66.7k
  });
1214
64.5k
1215
64.5k
  if (Info.empty()) {
1216
2.81k
    // Drop our entry at the store.
1217
2.81k
    InstructionMetadata.erase(this);
1218
2.81k
    setHasMetadataHashEntry(false);
1219
2.81k
  }
1220
64.5k
}
1221
1222
21.6M
void Instruction::setMetadata(unsigned KindID, MDNode *Node) {
1223
21.6M
  if (!Node && 
!hasMetadata()12.7M
)
1224
2.11M
    return;
1225
19.5M
1226
19.5M
  // Handle 'dbg' as a special case since it is not stored in the hash table.
1227
19.5M
  if (KindID == LLVMContext::MD_dbg) {
1228
40.9k
    DbgLoc = DebugLoc(Node);
1229
40.9k
    return;
1230
40.9k
  }
1231
19.4M
1232
19.4M
  // Handle the case when we're adding/updating metadata on an instruction.
1233
19.4M
  if (Node) {
1234
8.77M
    auto &Info = getContext().pImpl->InstructionMetadata[this];
1235
8.77M
    assert(!Info.empty() == hasMetadataHashEntry() &&
1236
8.77M
           "HasMetadata bit is wonked");
1237
8.77M
    if (Info.empty())
1238
8.32M
      setHasMetadataHashEntry(true);
1239
8.77M
    Info.set(KindID, *Node);
1240
8.77M
    return;
1241
8.77M
  }
1242
10.6M
1243
10.6M
  // Otherwise, we're removing metadata from an instruction.
1244
10.6M
  assert((hasMetadataHashEntry() ==
1245
10.6M
          (getContext().pImpl->InstructionMetadata.count(this) > 0)) &&
1246
10.6M
         "HasMetadata bit out of date!");
1247
10.6M
  if (!hasMetadataHashEntry())
1248
59.3k
    return; // Nothing to remove!
1249
10.6M
  auto &Info = getContext().pImpl->InstructionMetadata[this];
1250
10.6M
1251
10.6M
  // Handle removal of an existing value.
1252
10.6M
  Info.erase(KindID);
1253
10.6M
1254
10.6M
  if (!Info.empty())
1255
10.6M
    return;
1256
817
1257
817
  getContext().pImpl->InstructionMetadata.erase(this);
1258
817
  setHasMetadataHashEntry(false);
1259
817
}
1260
1261
1.80M
void Instruction::setAAMetadata(const AAMDNodes &N) {
1262
1.80M
  setMetadata(LLVMContext::MD_tbaa, N.TBAA);
1263
1.80M
  setMetadata(LLVMContext::MD_alias_scope, N.Scope);
1264
1.80M
  setMetadata(LLVMContext::MD_noalias, N.NoAlias);
1265
1.80M
}
1266
1267
345M
MDNode *Instruction::getMetadataImpl(unsigned KindID) const {
1268
345M
  // Handle 'dbg' as a special case since it is not stored in the hash table.
1269
345M
  if (KindID == LLVMContext::MD_dbg)
1270
0
    return DbgLoc.getAsMDNode();
1271
345M
1272
345M
  if (!hasMetadataHashEntry())
1273
30.2M
    return nullptr;
1274
315M
  auto &Info = getContext().pImpl->InstructionMetadata[this];
1275
315M
  assert(!Info.empty() && "bit out of sync with hash table");
1276
315M
1277
315M
  return Info.lookup(KindID);
1278
315M
}
1279
1280
void Instruction::getAllMetadataImpl(
1281
3.18M
    SmallVectorImpl<std::pair<unsigned, MDNode *>> &Result) const {
1282
3.18M
  Result.clear();
1283
3.18M
1284
3.18M
  // Handle 'dbg' as a special case since it is not stored in the hash table.
1285
3.18M
  if (DbgLoc) {
1286
2.12M
    Result.push_back(
1287
2.12M
        std::make_pair((unsigned)LLVMContext::MD_dbg, DbgLoc.getAsMDNode()));
1288
2.12M
    if (!hasMetadataHashEntry())
1289
1.62M
      return;
1290
1.55M
  }
1291
1.55M
1292
1.55M
  assert(hasMetadataHashEntry() &&
1293
1.55M
         getContext().pImpl->InstructionMetadata.count(this) &&
1294
1.55M
         "Shouldn't have called this");
1295
1.55M
  const auto &Info = getContext().pImpl->InstructionMetadata.find(this)->second;
1296
1.55M
  assert(!Info.empty() && "Shouldn't have called this");
1297
1.55M
  Info.getAll(Result);
1298
1.55M
}
1299
1300
void Instruction::getAllMetadataOtherThanDebugLocImpl(
1301
2.90M
    SmallVectorImpl<std::pair<unsigned, MDNode *>> &Result) const {
1302
2.90M
  Result.clear();
1303
2.90M
  assert(hasMetadataHashEntry() &&
1304
2.90M
         getContext().pImpl->InstructionMetadata.count(this) &&
1305
2.90M
         "Shouldn't have called this");
1306
2.90M
  const auto &Info = getContext().pImpl->InstructionMetadata.find(this)->second;
1307
2.90M
  assert(!Info.empty() && "Shouldn't have called this");
1308
2.90M
  Info.getAll(Result);
1309
2.90M
}
1310
1311
bool Instruction::extractProfMetadata(uint64_t &TrueVal,
1312
112k
                                      uint64_t &FalseVal) const {
1313
112k
  assert(
1314
112k
      (getOpcode() == Instruction::Br || getOpcode() == Instruction::Select) &&
1315
112k
      "Looking for branch weights on something besides branch or select");
1316
112k
1317
112k
  auto *ProfileData = getMetadata(LLVMContext::MD_prof);
1318
112k
  if (!ProfileData || 
ProfileData->getNumOperands() != 32.77k
)
1319
109k
    return false;
1320
2.77k
1321
2.77k
  auto *ProfDataName = dyn_cast<MDString>(ProfileData->getOperand(0));
1322
2.77k
  if (!ProfDataName || !ProfDataName->getString().equals("branch_weights"))
1323
1
    return false;
1324
2.77k
1325
2.77k
  auto *CITrue = mdconst::dyn_extract<ConstantInt>(ProfileData->getOperand(1));
1326
2.77k
  auto *CIFalse = mdconst::dyn_extract<ConstantInt>(ProfileData->getOperand(2));
1327
2.77k
  if (!CITrue || !CIFalse)
1328
0
    return false;
1329
2.77k
1330
2.77k
  TrueVal = CITrue->getValue().getZExtValue();
1331
2.77k
  FalseVal = CIFalse->getValue().getZExtValue();
1332
2.77k
1333
2.77k
  return true;
1334
2.77k
}
1335
1336
22.1k
bool Instruction::extractProfTotalWeight(uint64_t &TotalVal) const {
1337
22.1k
  assert((getOpcode() == Instruction::Br ||
1338
22.1k
          getOpcode() == Instruction::Select ||
1339
22.1k
          getOpcode() == Instruction::Call ||
1340
22.1k
          getOpcode() == Instruction::Invoke ||
1341
22.1k
          getOpcode() == Instruction::Switch) &&
1342
22.1k
         "Looking for branch weights on something besides branch");
1343
22.1k
1344
22.1k
  TotalVal = 0;
1345
22.1k
  auto *ProfileData = getMetadata(LLVMContext::MD_prof);
1346
22.1k
  if (!ProfileData)
1347
22.0k
    return false;
1348
59
1349
59
  auto *ProfDataName = dyn_cast<MDString>(ProfileData->getOperand(0));
1350
59
  if (!ProfDataName)
1351
0
    return false;
1352
59
1353
59
  if (ProfDataName->getString().equals("branch_weights")) {
1354
56
    TotalVal = 0;
1355
121
    for (unsigned i = 1; i < ProfileData->getNumOperands(); 
i++65
) {
1356
65
      auto *V = mdconst::dyn_extract<ConstantInt>(ProfileData->getOperand(i));
1357
65
      if (!V)
1358
0
        return false;
1359
65
      TotalVal += V->getValue().getZExtValue();
1360
65
    }
1361
56
    return true;
1362
3
  } else if (ProfDataName->getString().equals("VP") &&
1363
3
             ProfileData->getNumOperands() > 3) {
1364
3
    TotalVal = mdconst::dyn_extract<ConstantInt>(ProfileData->getOperand(2))
1365
3
                   ->getValue()
1366
3
                   .getZExtValue();
1367
3
    return true;
1368
3
  }
1369
0
  return false;
1370
0
}
1371
1372
5.85M
void Instruction::clearMetadataHashEntries() {
1373
5.85M
  assert(hasMetadataHashEntry() && "Caller should check");
1374
5.85M
  getContext().pImpl->InstructionMetadata.erase(this);
1375
5.85M
  setHasMetadataHashEntry(false);
1376
5.85M
}
1377
1378
void GlobalObject::getMetadata(unsigned KindID,
1379
637k
                               SmallVectorImpl<MDNode *> &MDs) const {
1380
637k
  if (hasMetadata())
1381
7.05k
    getContext().pImpl->GlobalObjectMetadata[this].get(KindID, MDs);
1382
637k
}
1383
1384
void GlobalObject::getMetadata(StringRef Kind,
1385
0
                               SmallVectorImpl<MDNode *> &MDs) const {
1386
0
  if (hasMetadata())
1387
0
    getMetadata(getContext().getMDKindID(Kind), MDs);
1388
0
}
1389
1390
134k
void GlobalObject::addMetadata(unsigned KindID, MDNode &MD) {
1391
134k
  if (!hasMetadata())
1392
129k
    setHasMetadataHashEntry(true);
1393
134k
1394
134k
  getContext().pImpl->GlobalObjectMetadata[this].insert(KindID, MD);
1395
134k
}
1396
1397
0
void GlobalObject::addMetadata(StringRef Kind, MDNode &MD) {
1398
0
  addMetadata(getContext().getMDKindID(Kind), MD);
1399
0
}
1400
1401
475k
bool GlobalObject::eraseMetadata(unsigned KindID) {
1402
475k
  // Nothing to unset.
1403
475k
  if (!hasMetadata())
1404
471k
    return false;
1405
3.87k
1406
3.87k
  auto &Store = getContext().pImpl->GlobalObjectMetadata[this];
1407
3.87k
  bool Changed = Store.erase(KindID);
1408
3.87k
  if (Store.empty())
1409
1.36k
    clearMetadata();
1410
3.87k
  return Changed;
1411
3.87k
}
1412
1413
void GlobalObject::getAllMetadata(
1414
2.64M
    SmallVectorImpl<std::pair<unsigned, MDNode *>> &MDs) const {
1415
2.64M
  MDs.clear();
1416
2.64M
1417
2.64M
  if (!hasMetadata())
1418
2.61M
    return;
1419
36.4k
1420
36.4k
  getContext().pImpl->GlobalObjectMetadata[this].getAll(MDs);
1421
36.4k
}
1422
1423
2.66M
void GlobalObject::clearMetadata() {
1424
2.66M
  if (!hasMetadata())
1425
2.56M
    return;
1426
95.0k
  getContext().pImpl->GlobalObjectMetadata.erase(this);
1427
95.0k
  setHasMetadataHashEntry(false);
1428
95.0k
}
1429
1430
125k
void GlobalObject::setMetadata(unsigned KindID, MDNode *N) {
1431
125k
  eraseMetadata(KindID);
1432
125k
  if (N)
1433
121k
    addMetadata(KindID, *N);
1434
125k
}
1435
1436
2.08k
void GlobalObject::setMetadata(StringRef Kind, MDNode *N) {
1437
2.08k
  setMetadata(getContext().getMDKindID(Kind), N);
1438
2.08k
}
1439
1440
13.0M
MDNode *GlobalObject::getMetadata(unsigned KindID) const {
1441
13.0M
  if (hasMetadata())
1442
4.23M
    return getContext().pImpl->GlobalObjectMetadata[this].lookup(KindID);
1443
8.77M
  return nullptr;
1444
8.77M
}
1445
1446
61.6k
MDNode *GlobalObject::getMetadata(StringRef Kind) const {
1447
61.6k
  return getMetadata(getContext().getMDKindID(Kind));
1448
61.6k
}
1449
1450
8.02k
void GlobalObject::copyMetadata(const GlobalObject *Other, unsigned Offset) {
1451
8.02k
  SmallVector<std::pair<unsigned, MDNode *>, 8> MDs;
1452
8.02k
  Other->getAllMetadata(MDs);
1453
8.02k
  for (auto &MD : MDs) {
1454
262
    // We need to adjust the type metadata offset.
1455
262
    if (Offset != 0 && 
MD.first == LLVMContext::MD_type33
) {
1456
28
      auto *OffsetConst = cast<ConstantInt>(
1457
28
          cast<ConstantAsMetadata>(MD.second->getOperand(0))->getValue());
1458
28
      Metadata *TypeId = MD.second->getOperand(1);
1459
28
      auto *NewOffsetMD = ConstantAsMetadata::get(ConstantInt::get(
1460
28
          OffsetConst->getType(), OffsetConst->getValue() + Offset));
1461
28
      addMetadata(LLVMContext::MD_type,
1462
28
                  *MDNode::get(getContext(), {NewOffsetMD, TypeId}));
1463
28
      continue;
1464
28
    }
1465
234
    // If an offset adjustment was specified we need to modify the DIExpression
1466
234
    // to prepend the adjustment:
1467
234
    // !DIExpression(DW_OP_plus, Offset, [original expr])
1468
234
    auto *Attachment = MD.second;
1469
234
    if (Offset != 0 && 
MD.first == LLVMContext::MD_dbg5
) {
1470
5
      DIGlobalVariable *GV = dyn_cast<DIGlobalVariable>(Attachment);
1471
5
      DIExpression *E = nullptr;
1472
5
      if (!GV) {
1473
5
        auto *GVE = cast<DIGlobalVariableExpression>(Attachment);
1474
5
        GV = GVE->getVariable();
1475
5
        E = GVE->getExpression();
1476
5
      }
1477
5
      ArrayRef<uint64_t> OrigElements;
1478
5
      if (E)
1479
5
        OrigElements = E->getElements();
1480
5
      std::vector<uint64_t> Elements(OrigElements.size() + 2);
1481
5
      Elements[0] = dwarf::DW_OP_plus_uconst;
1482
5
      Elements[1] = Offset;
1483
5
      llvm::copy(OrigElements, Elements.begin() + 2);
1484
5
      E = DIExpression::get(getContext(), Elements);
1485
5
      Attachment = DIGlobalVariableExpression::get(getContext(), GV, E);
1486
5
    }
1487
234
    addMetadata(MD.first, *Attachment);
1488
234
  }
1489
8.02k
}
1490
1491
442
void GlobalObject::addTypeMetadata(unsigned Offset, Metadata *TypeID) {
1492
442
  addMetadata(
1493
442
      LLVMContext::MD_type,
1494
442
      *MDTuple::get(getContext(),
1495
442
                    {ConstantAsMetadata::get(ConstantInt::get(
1496
442
                         Type::getInt64Ty(getContext()), Offset)),
1497
442
                     TypeID}));
1498
442
}
1499
1500
122k
void Function::setSubprogram(DISubprogram *SP) {
1501
122k
  setMetadata(LLVMContext::MD_dbg, SP);
1502
122k
}
1503
1504
7.30M
DISubprogram *Function::getSubprogram() const {
1505
7.30M
  return cast_or_null<DISubprogram>(getMetadata(LLVMContext::MD_dbg));
1506
7.30M
}
1507
1508
20.1k
bool Function::isDebugInfoForProfiling() const {
1509
20.1k
  if (DISubprogram *SP = getSubprogram()) {
1510
11.6k
    if (DICompileUnit *CU = SP->getUnit()) {
1511
11.6k
      return CU->getDebugInfoForProfiling();
1512
11.6k
    }
1513
8.45k
  }
1514
8.45k
  return false;
1515
8.45k
}
1516
1517
608
void GlobalVariable::addDebugInfo(DIGlobalVariableExpression *GV) {
1518
608
  addMetadata(LLVMContext::MD_dbg, *GV);
1519
608
}
1520
1521
void GlobalVariable::getDebugInfo(
1522
464k
    SmallVectorImpl<DIGlobalVariableExpression *> &GVs) const {
1523
464k
  SmallVector<MDNode *, 1> MDs;
1524
464k
  getMetadata(LLVMContext::MD_dbg, MDs);
1525
464k
  for (MDNode *MD : MDs)
1526
1.04k
    GVs.push_back(cast<DIGlobalVariableExpression>(MD));
1527
464k
}