MetadataLoader.cpp revision 311544
1//===- MetadataLoader.cpp - Internal BitcodeReader implementation ---------===//
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#include "MetadataLoader.h"
11#include "ValueList.h"
12
13#include "llvm/ADT/APFloat.h"
14#include "llvm/ADT/APInt.h"
15#include "llvm/ADT/ArrayRef.h"
16#include "llvm/ADT/DenseMap.h"
17#include "llvm/ADT/DenseSet.h"
18#include "llvm/ADT/None.h"
19#include "llvm/ADT/STLExtras.h"
20#include "llvm/ADT/SmallString.h"
21#include "llvm/ADT/SmallVector.h"
22#include "llvm/ADT/Statistic.h"
23#include "llvm/ADT/StringRef.h"
24#include "llvm/ADT/Triple.h"
25#include "llvm/ADT/Twine.h"
26#include "llvm/Bitcode/BitcodeReader.h"
27#include "llvm/Bitcode/BitstreamReader.h"
28#include "llvm/Bitcode/LLVMBitCodes.h"
29#include "llvm/IR/Argument.h"
30#include "llvm/IR/Attributes.h"
31#include "llvm/IR/AutoUpgrade.h"
32#include "llvm/IR/BasicBlock.h"
33#include "llvm/IR/CallSite.h"
34#include "llvm/IR/CallingConv.h"
35#include "llvm/IR/Comdat.h"
36#include "llvm/IR/Constant.h"
37#include "llvm/IR/Constants.h"
38#include "llvm/IR/DebugInfo.h"
39#include "llvm/IR/DebugInfoMetadata.h"
40#include "llvm/IR/DebugLoc.h"
41#include "llvm/IR/DerivedTypes.h"
42#include "llvm/IR/DiagnosticInfo.h"
43#include "llvm/IR/DiagnosticPrinter.h"
44#include "llvm/IR/Function.h"
45#include "llvm/IR/GVMaterializer.h"
46#include "llvm/IR/GlobalAlias.h"
47#include "llvm/IR/GlobalIFunc.h"
48#include "llvm/IR/GlobalIndirectSymbol.h"
49#include "llvm/IR/GlobalObject.h"
50#include "llvm/IR/GlobalValue.h"
51#include "llvm/IR/GlobalVariable.h"
52#include "llvm/IR/InlineAsm.h"
53#include "llvm/IR/InstrTypes.h"
54#include "llvm/IR/Instruction.h"
55#include "llvm/IR/Instructions.h"
56#include "llvm/IR/Intrinsics.h"
57#include "llvm/IR/LLVMContext.h"
58#include "llvm/IR/Module.h"
59#include "llvm/IR/ModuleSummaryIndex.h"
60#include "llvm/IR/OperandTraits.h"
61#include "llvm/IR/Operator.h"
62#include "llvm/IR/TrackingMDRef.h"
63#include "llvm/IR/Type.h"
64#include "llvm/IR/ValueHandle.h"
65#include "llvm/Support/AtomicOrdering.h"
66#include "llvm/Support/Casting.h"
67#include "llvm/Support/CommandLine.h"
68#include "llvm/Support/Compiler.h"
69#include "llvm/Support/Debug.h"
70#include "llvm/Support/Error.h"
71#include "llvm/Support/ErrorHandling.h"
72#include "llvm/Support/ManagedStatic.h"
73#include "llvm/Support/MemoryBuffer.h"
74#include "llvm/Support/raw_ostream.h"
75#include <algorithm>
76#include <cassert>
77#include <cstddef>
78#include <cstdint>
79#include <deque>
80#include <limits>
81#include <map>
82#include <memory>
83#include <string>
84#include <system_error>
85#include <tuple>
86#include <utility>
87#include <vector>
88
89using namespace llvm;
90
91#define DEBUG_TYPE "bitcode-reader"
92
93STATISTIC(NumMDStringLoaded, "Number of MDStrings loaded");
94STATISTIC(NumMDNodeTemporary, "Number of MDNode::Temporary created");
95STATISTIC(NumMDRecordLoaded, "Number of Metadata records loaded");
96
97/// Flag whether we need to import full type definitions for ThinLTO.
98/// Currently needed for Darwin and LLDB.
99static cl::opt<bool> ImportFullTypeDefinitions(
100    "import-full-type-definitions", cl::init(false), cl::Hidden,
101    cl::desc("Import full type definitions for ThinLTO."));
102
103static cl::opt<bool> DisableLazyLoading(
104    "disable-ondemand-mds-loading", cl::init(false), cl::Hidden,
105    cl::desc("Force disable the lazy-loading on-demand of metadata when "
106             "loading bitcode for importing."));
107
108namespace {
109
110static int64_t unrotateSign(uint64_t U) { return U & 1 ? ~(U >> 1) : U >> 1; }
111
112class BitcodeReaderMetadataList {
113  /// Array of metadata references.
114  ///
115  /// Don't use std::vector here.  Some versions of libc++ copy (instead of
116  /// move) on resize, and TrackingMDRef is very expensive to copy.
117  SmallVector<TrackingMDRef, 1> MetadataPtrs;
118
119  /// The set of indices in MetadataPtrs above of forward references that were
120  /// generated.
121  SmallDenseSet<unsigned, 1> ForwardReference;
122
123  /// The set of indices in MetadataPtrs above of Metadata that need to be
124  /// resolved.
125  SmallDenseSet<unsigned, 1> UnresolvedNodes;
126
127  /// Structures for resolving old type refs.
128  struct {
129    SmallDenseMap<MDString *, TempMDTuple, 1> Unknown;
130    SmallDenseMap<MDString *, DICompositeType *, 1> Final;
131    SmallDenseMap<MDString *, DICompositeType *, 1> FwdDecls;
132    SmallVector<std::pair<TrackingMDRef, TempMDTuple>, 1> Arrays;
133  } OldTypeRefs;
134
135  LLVMContext &Context;
136
137public:
138  BitcodeReaderMetadataList(LLVMContext &C) : Context(C) {}
139
140  // vector compatibility methods
141  unsigned size() const { return MetadataPtrs.size(); }
142  void resize(unsigned N) { MetadataPtrs.resize(N); }
143  void push_back(Metadata *MD) { MetadataPtrs.emplace_back(MD); }
144  void clear() { MetadataPtrs.clear(); }
145  Metadata *back() const { return MetadataPtrs.back(); }
146  void pop_back() { MetadataPtrs.pop_back(); }
147  bool empty() const { return MetadataPtrs.empty(); }
148
149  Metadata *operator[](unsigned i) const {
150    assert(i < MetadataPtrs.size());
151    return MetadataPtrs[i];
152  }
153
154  Metadata *lookup(unsigned I) const {
155    if (I < MetadataPtrs.size())
156      return MetadataPtrs[I];
157    return nullptr;
158  }
159
160  void shrinkTo(unsigned N) {
161    assert(N <= size() && "Invalid shrinkTo request!");
162    assert(ForwardReference.empty() && "Unexpected forward refs");
163    assert(UnresolvedNodes.empty() && "Unexpected unresolved node");
164    MetadataPtrs.resize(N);
165  }
166
167  /// Return the given metadata, creating a replaceable forward reference if
168  /// necessary.
169  Metadata *getMetadataFwdRef(unsigned Idx);
170
171  /// Return the the given metadata only if it is fully resolved.
172  ///
173  /// Gives the same result as \a lookup(), unless \a MDNode::isResolved()
174  /// would give \c false.
175  Metadata *getMetadataIfResolved(unsigned Idx);
176
177  MDNode *getMDNodeFwdRefOrNull(unsigned Idx);
178  void assignValue(Metadata *MD, unsigned Idx);
179  void tryToResolveCycles();
180  bool hasFwdRefs() const { return !ForwardReference.empty(); }
181  int getNextFwdRef() {
182    assert(hasFwdRefs());
183    return *ForwardReference.begin();
184  }
185
186  /// Upgrade a type that had an MDString reference.
187  void addTypeRef(MDString &UUID, DICompositeType &CT);
188
189  /// Upgrade a type that had an MDString reference.
190  Metadata *upgradeTypeRef(Metadata *MaybeUUID);
191
192  /// Upgrade a type ref array that may have MDString references.
193  Metadata *upgradeTypeRefArray(Metadata *MaybeTuple);
194
195private:
196  Metadata *resolveTypeRefArray(Metadata *MaybeTuple);
197};
198
199void BitcodeReaderMetadataList::assignValue(Metadata *MD, unsigned Idx) {
200  if (auto *MDN = dyn_cast<MDNode>(MD))
201    if (!MDN->isResolved())
202      UnresolvedNodes.insert(Idx);
203
204  if (Idx == size()) {
205    push_back(MD);
206    return;
207  }
208
209  if (Idx >= size())
210    resize(Idx + 1);
211
212  TrackingMDRef &OldMD = MetadataPtrs[Idx];
213  if (!OldMD) {
214    OldMD.reset(MD);
215    return;
216  }
217
218  // If there was a forward reference to this value, replace it.
219  TempMDTuple PrevMD(cast<MDTuple>(OldMD.get()));
220  PrevMD->replaceAllUsesWith(MD);
221  ForwardReference.erase(Idx);
222}
223
224Metadata *BitcodeReaderMetadataList::getMetadataFwdRef(unsigned Idx) {
225  if (Idx >= size())
226    resize(Idx + 1);
227
228  if (Metadata *MD = MetadataPtrs[Idx])
229    return MD;
230
231  // Track forward refs to be resolved later.
232  ForwardReference.insert(Idx);
233
234  // Create and return a placeholder, which will later be RAUW'd.
235  ++NumMDNodeTemporary;
236  Metadata *MD = MDNode::getTemporary(Context, None).release();
237  MetadataPtrs[Idx].reset(MD);
238  return MD;
239}
240
241Metadata *BitcodeReaderMetadataList::getMetadataIfResolved(unsigned Idx) {
242  Metadata *MD = lookup(Idx);
243  if (auto *N = dyn_cast_or_null<MDNode>(MD))
244    if (!N->isResolved())
245      return nullptr;
246  return MD;
247}
248
249MDNode *BitcodeReaderMetadataList::getMDNodeFwdRefOrNull(unsigned Idx) {
250  return dyn_cast_or_null<MDNode>(getMetadataFwdRef(Idx));
251}
252
253void BitcodeReaderMetadataList::tryToResolveCycles() {
254  if (!ForwardReference.empty())
255    // Still forward references... can't resolve cycles.
256    return;
257
258  // Give up on finding a full definition for any forward decls that remain.
259  for (const auto &Ref : OldTypeRefs.FwdDecls)
260    OldTypeRefs.Final.insert(Ref);
261  OldTypeRefs.FwdDecls.clear();
262
263  // Upgrade from old type ref arrays.  In strange cases, this could add to
264  // OldTypeRefs.Unknown.
265  for (const auto &Array : OldTypeRefs.Arrays)
266    Array.second->replaceAllUsesWith(resolveTypeRefArray(Array.first.get()));
267  OldTypeRefs.Arrays.clear();
268
269  // Replace old string-based type refs with the resolved node, if possible.
270  // If we haven't seen the node, leave it to the verifier to complain about
271  // the invalid string reference.
272  for (const auto &Ref : OldTypeRefs.Unknown) {
273    if (DICompositeType *CT = OldTypeRefs.Final.lookup(Ref.first))
274      Ref.second->replaceAllUsesWith(CT);
275    else
276      Ref.second->replaceAllUsesWith(Ref.first);
277  }
278  OldTypeRefs.Unknown.clear();
279
280  if (UnresolvedNodes.empty())
281    // Nothing to do.
282    return;
283
284  // Resolve any cycles.
285  for (unsigned I : UnresolvedNodes) {
286    auto &MD = MetadataPtrs[I];
287    auto *N = dyn_cast_or_null<MDNode>(MD);
288    if (!N)
289      continue;
290
291    assert(!N->isTemporary() && "Unexpected forward reference");
292    N->resolveCycles();
293  }
294
295  // Make sure we return early again until there's another unresolved ref.
296  UnresolvedNodes.clear();
297}
298
299void BitcodeReaderMetadataList::addTypeRef(MDString &UUID,
300                                           DICompositeType &CT) {
301  assert(CT.getRawIdentifier() == &UUID && "Mismatched UUID");
302  if (CT.isForwardDecl())
303    OldTypeRefs.FwdDecls.insert(std::make_pair(&UUID, &CT));
304  else
305    OldTypeRefs.Final.insert(std::make_pair(&UUID, &CT));
306}
307
308Metadata *BitcodeReaderMetadataList::upgradeTypeRef(Metadata *MaybeUUID) {
309  auto *UUID = dyn_cast_or_null<MDString>(MaybeUUID);
310  if (LLVM_LIKELY(!UUID))
311    return MaybeUUID;
312
313  if (auto *CT = OldTypeRefs.Final.lookup(UUID))
314    return CT;
315
316  auto &Ref = OldTypeRefs.Unknown[UUID];
317  if (!Ref)
318    Ref = MDNode::getTemporary(Context, None);
319  return Ref.get();
320}
321
322Metadata *BitcodeReaderMetadataList::upgradeTypeRefArray(Metadata *MaybeTuple) {
323  auto *Tuple = dyn_cast_or_null<MDTuple>(MaybeTuple);
324  if (!Tuple || Tuple->isDistinct())
325    return MaybeTuple;
326
327  // Look through the array immediately if possible.
328  if (!Tuple->isTemporary())
329    return resolveTypeRefArray(Tuple);
330
331  // Create and return a placeholder to use for now.  Eventually
332  // resolveTypeRefArrays() will be resolve this forward reference.
333  OldTypeRefs.Arrays.emplace_back(
334      std::piecewise_construct, std::forward_as_tuple(Tuple),
335      std::forward_as_tuple(MDTuple::getTemporary(Context, None)));
336  return OldTypeRefs.Arrays.back().second.get();
337}
338
339Metadata *BitcodeReaderMetadataList::resolveTypeRefArray(Metadata *MaybeTuple) {
340  auto *Tuple = dyn_cast_or_null<MDTuple>(MaybeTuple);
341  if (!Tuple || Tuple->isDistinct())
342    return MaybeTuple;
343
344  // Look through the DITypeRefArray, upgrading each DITypeRef.
345  SmallVector<Metadata *, 32> Ops;
346  Ops.reserve(Tuple->getNumOperands());
347  for (Metadata *MD : Tuple->operands())
348    Ops.push_back(upgradeTypeRef(MD));
349
350  return MDTuple::get(Context, Ops);
351}
352
353namespace {
354
355class PlaceholderQueue {
356  // Placeholders would thrash around when moved, so store in a std::deque
357  // instead of some sort of vector.
358  std::deque<DistinctMDOperandPlaceholder> PHs;
359
360public:
361  bool empty() { return PHs.empty(); }
362  DistinctMDOperandPlaceholder &getPlaceholderOp(unsigned ID);
363  void flush(BitcodeReaderMetadataList &MetadataList);
364
365  /// Return the list of temporaries nodes in the queue, these need to be
366  /// loaded before we can flush the queue.
367  void getTemporaries(BitcodeReaderMetadataList &MetadataList,
368                      DenseSet<unsigned> &Temporaries) {
369    for (auto &PH : PHs) {
370      auto ID = PH.getID();
371      auto *MD = MetadataList.lookup(ID);
372      if (!MD) {
373        Temporaries.insert(ID);
374        continue;
375      }
376      auto *N = dyn_cast_or_null<MDNode>(MD);
377      if (N && N->isTemporary())
378        Temporaries.insert(ID);
379    }
380  }
381};
382
383} // end anonymous namespace
384
385DistinctMDOperandPlaceholder &PlaceholderQueue::getPlaceholderOp(unsigned ID) {
386  PHs.emplace_back(ID);
387  return PHs.back();
388}
389
390void PlaceholderQueue::flush(BitcodeReaderMetadataList &MetadataList) {
391  while (!PHs.empty()) {
392    auto *MD = MetadataList.lookup(PHs.front().getID());
393    assert(MD && "Flushing placeholder on unassigned MD");
394#ifndef NDEBUG
395    if (auto *MDN = dyn_cast<MDNode>(MD))
396      assert(MDN->isResolved() &&
397             "Flushing Placeholder while cycles aren't resolved");
398#endif
399    PHs.front().replaceUseWith(MD);
400    PHs.pop_front();
401  }
402}
403
404} // anonynous namespace
405
406class MetadataLoader::MetadataLoaderImpl {
407  BitcodeReaderMetadataList MetadataList;
408  BitcodeReaderValueList &ValueList;
409  BitstreamCursor &Stream;
410  LLVMContext &Context;
411  Module &TheModule;
412  std::function<Type *(unsigned)> getTypeByID;
413
414  /// Cursor associated with the lazy-loading of Metadata. This is the easy way
415  /// to keep around the right "context" (Abbrev list) to be able to jump in
416  /// the middle of the metadata block and load any record.
417  BitstreamCursor IndexCursor;
418
419  /// Index that keeps track of MDString values.
420  std::vector<StringRef> MDStringRef;
421
422  /// On-demand loading of a single MDString. Requires the index above to be
423  /// populated.
424  MDString *lazyLoadOneMDString(unsigned Idx);
425
426  /// Index that keeps track of where to find a metadata record in the stream.
427  std::vector<uint64_t> GlobalMetadataBitPosIndex;
428
429  /// Populate the index above to enable lazily loading of metadata, and load
430  /// the named metadata as well as the transitively referenced global
431  /// Metadata.
432  Expected<bool> lazyLoadModuleMetadataBlock(PlaceholderQueue &Placeholders);
433
434  /// On-demand loading of a single metadata. Requires the index above to be
435  /// populated.
436  void lazyLoadOneMetadata(unsigned Idx, PlaceholderQueue &Placeholders);
437
438  // Keep mapping of seens pair of old-style CU <-> SP, and update pointers to
439  // point from SP to CU after a block is completly parsed.
440  std::vector<std::pair<DICompileUnit *, Metadata *>> CUSubprograms;
441
442  /// Functions that need to be matched with subprograms when upgrading old
443  /// metadata.
444  SmallDenseMap<Function *, DISubprogram *, 16> FunctionsWithSPs;
445
446  // Map the bitcode's custom MDKind ID to the Module's MDKind ID.
447  DenseMap<unsigned, unsigned> MDKindMap;
448
449  bool StripTBAA = false;
450  bool HasSeenOldLoopTags = false;
451
452  /// True if metadata is being parsed for a module being ThinLTO imported.
453  bool IsImporting = false;
454
455  Error parseOneMetadata(SmallVectorImpl<uint64_t> &Record, unsigned Code,
456                         PlaceholderQueue &Placeholders, StringRef Blob,
457                         unsigned &NextMetadataNo);
458  Error parseMetadataStrings(ArrayRef<uint64_t> Record, StringRef Blob,
459                             std::function<void(StringRef)> CallBack);
460  Error parseGlobalObjectAttachment(GlobalObject &GO,
461                                    ArrayRef<uint64_t> Record);
462  Error parseMetadataKindRecord(SmallVectorImpl<uint64_t> &Record);
463
464  void resolveForwardRefsAndPlaceholders(PlaceholderQueue &Placeholders);
465
466  /// Upgrade old-style CU <-> SP pointers to point from SP to CU.
467  void upgradeCUSubprograms() {
468    for (auto CU_SP : CUSubprograms)
469      if (auto *SPs = dyn_cast_or_null<MDTuple>(CU_SP.second))
470        for (auto &Op : SPs->operands())
471          if (auto *SP = dyn_cast_or_null<MDNode>(Op))
472            SP->replaceOperandWith(7, CU_SP.first);
473    CUSubprograms.clear();
474  }
475
476public:
477  MetadataLoaderImpl(BitstreamCursor &Stream, Module &TheModule,
478                     BitcodeReaderValueList &ValueList,
479                     std::function<Type *(unsigned)> getTypeByID,
480                     bool IsImporting)
481      : MetadataList(TheModule.getContext()), ValueList(ValueList),
482        Stream(Stream), Context(TheModule.getContext()), TheModule(TheModule),
483        getTypeByID(getTypeByID), IsImporting(IsImporting) {}
484
485  Error parseMetadata(bool ModuleLevel);
486
487  bool hasFwdRefs() const { return MetadataList.hasFwdRefs(); }
488  Metadata *getMetadataFwdRef(unsigned Idx) {
489    return MetadataList.getMetadataFwdRef(Idx);
490  }
491
492  MDNode *getMDNodeFwdRefOrNull(unsigned Idx) {
493    return MetadataList.getMDNodeFwdRefOrNull(Idx);
494  }
495
496  DISubprogram *lookupSubprogramForFunction(Function *F) {
497    return FunctionsWithSPs.lookup(F);
498  }
499
500  bool hasSeenOldLoopTags() { return HasSeenOldLoopTags; }
501
502  Error parseMetadataAttachment(
503      Function &F, const SmallVectorImpl<Instruction *> &InstructionList);
504
505  Error parseMetadataKinds();
506
507  void setStripTBAA(bool Value) { StripTBAA = Value; }
508  bool isStrippingTBAA() { return StripTBAA; }
509
510  unsigned size() const { return MetadataList.size(); }
511  void shrinkTo(unsigned N) { MetadataList.shrinkTo(N); }
512};
513
514Error error(const Twine &Message) {
515  return make_error<StringError>(
516      Message, make_error_code(BitcodeError::CorruptedBitcode));
517}
518
519Expected<bool> MetadataLoader::MetadataLoaderImpl::lazyLoadModuleMetadataBlock(
520    PlaceholderQueue &Placeholders) {
521  IndexCursor = Stream;
522  SmallVector<uint64_t, 64> Record;
523  // Get the abbrevs, and preload record positions to make them lazy-loadable.
524  while (true) {
525    BitstreamEntry Entry = IndexCursor.advanceSkippingSubblocks(
526        BitstreamCursor::AF_DontPopBlockAtEnd);
527    switch (Entry.Kind) {
528    case BitstreamEntry::SubBlock: // Handled for us already.
529    case BitstreamEntry::Error:
530      return error("Malformed block");
531    case BitstreamEntry::EndBlock: {
532      return true;
533    }
534    case BitstreamEntry::Record: {
535      // The interesting case.
536      ++NumMDRecordLoaded;
537      uint64_t CurrentPos = IndexCursor.GetCurrentBitNo();
538      auto Code = IndexCursor.skipRecord(Entry.ID);
539      switch (Code) {
540      case bitc::METADATA_STRINGS: {
541        // Rewind and parse the strings.
542        IndexCursor.JumpToBit(CurrentPos);
543        StringRef Blob;
544        Record.clear();
545        IndexCursor.readRecord(Entry.ID, Record, &Blob);
546        unsigned NumStrings = Record[0];
547        MDStringRef.reserve(NumStrings);
548        auto IndexNextMDString = [&](StringRef Str) {
549          MDStringRef.push_back(Str);
550        };
551        if (auto Err = parseMetadataStrings(Record, Blob, IndexNextMDString))
552          return std::move(Err);
553        break;
554      }
555      case bitc::METADATA_INDEX_OFFSET: {
556        // This is the offset to the index, when we see this we skip all the
557        // records and load only an index to these.
558        IndexCursor.JumpToBit(CurrentPos);
559        Record.clear();
560        IndexCursor.readRecord(Entry.ID, Record);
561        if (Record.size() != 2)
562          return error("Invalid record");
563        auto Offset = Record[0] + (Record[1] << 32);
564        auto BeginPos = IndexCursor.GetCurrentBitNo();
565        IndexCursor.JumpToBit(BeginPos + Offset);
566        Entry = IndexCursor.advanceSkippingSubblocks(
567            BitstreamCursor::AF_DontPopBlockAtEnd);
568        assert(Entry.Kind == BitstreamEntry::Record &&
569               "Corrupted bitcode: Expected `Record` when trying to find the "
570               "Metadata index");
571        Record.clear();
572        auto Code = IndexCursor.readRecord(Entry.ID, Record);
573        (void)Code;
574        assert(Code == bitc::METADATA_INDEX && "Corrupted bitcode: Expected "
575                                               "`METADATA_INDEX` when trying "
576                                               "to find the Metadata index");
577
578        // Delta unpack
579        auto CurrentValue = BeginPos;
580        GlobalMetadataBitPosIndex.reserve(Record.size());
581        for (auto &Elt : Record) {
582          CurrentValue += Elt;
583          GlobalMetadataBitPosIndex.push_back(CurrentValue);
584        }
585        break;
586      }
587      case bitc::METADATA_INDEX:
588        // We don't expect to get there, the Index is loaded when we encounter
589        // the offset.
590        return error("Corrupted Metadata block");
591      case bitc::METADATA_NAME: {
592        // Named metadata need to be materialized now and aren't deferred.
593        IndexCursor.JumpToBit(CurrentPos);
594        Record.clear();
595        unsigned Code = IndexCursor.readRecord(Entry.ID, Record);
596        assert(Code == bitc::METADATA_NAME);
597
598        // Read name of the named metadata.
599        SmallString<8> Name(Record.begin(), Record.end());
600        Code = IndexCursor.ReadCode();
601
602        // Named Metadata comes in two parts, we expect the name to be followed
603        // by the node
604        Record.clear();
605        unsigned NextBitCode = IndexCursor.readRecord(Code, Record);
606        assert(NextBitCode == bitc::METADATA_NAMED_NODE);
607        (void)NextBitCode;
608
609        // Read named metadata elements.
610        unsigned Size = Record.size();
611        NamedMDNode *NMD = TheModule.getOrInsertNamedMetadata(Name);
612        for (unsigned i = 0; i != Size; ++i) {
613          // FIXME: We could use a placeholder here, however NamedMDNode are
614          // taking MDNode as operand and not using the Metadata infrastructure.
615          // It is acknowledged by 'TODO: Inherit from Metadata' in the
616          // NamedMDNode class definition.
617          MDNode *MD = MetadataList.getMDNodeFwdRefOrNull(Record[i]);
618          assert(MD && "Invalid record");
619          NMD->addOperand(MD);
620        }
621        break;
622      }
623      case bitc::METADATA_GLOBAL_DECL_ATTACHMENT: {
624        // FIXME: we need to do this early because we don't materialize global
625        // value explicitly.
626        IndexCursor.JumpToBit(CurrentPos);
627        Record.clear();
628        IndexCursor.readRecord(Entry.ID, Record);
629        if (Record.size() % 2 == 0)
630          return error("Invalid record");
631        unsigned ValueID = Record[0];
632        if (ValueID >= ValueList.size())
633          return error("Invalid record");
634        if (auto *GO = dyn_cast<GlobalObject>(ValueList[ValueID]))
635          if (Error Err = parseGlobalObjectAttachment(
636                  *GO, ArrayRef<uint64_t>(Record).slice(1)))
637            return std::move(Err);
638        break;
639      }
640      case bitc::METADATA_KIND:
641      case bitc::METADATA_STRING_OLD:
642      case bitc::METADATA_OLD_FN_NODE:
643      case bitc::METADATA_OLD_NODE:
644      case bitc::METADATA_VALUE:
645      case bitc::METADATA_DISTINCT_NODE:
646      case bitc::METADATA_NODE:
647      case bitc::METADATA_LOCATION:
648      case bitc::METADATA_GENERIC_DEBUG:
649      case bitc::METADATA_SUBRANGE:
650      case bitc::METADATA_ENUMERATOR:
651      case bitc::METADATA_BASIC_TYPE:
652      case bitc::METADATA_DERIVED_TYPE:
653      case bitc::METADATA_COMPOSITE_TYPE:
654      case bitc::METADATA_SUBROUTINE_TYPE:
655      case bitc::METADATA_MODULE:
656      case bitc::METADATA_FILE:
657      case bitc::METADATA_COMPILE_UNIT:
658      case bitc::METADATA_SUBPROGRAM:
659      case bitc::METADATA_LEXICAL_BLOCK:
660      case bitc::METADATA_LEXICAL_BLOCK_FILE:
661      case bitc::METADATA_NAMESPACE:
662      case bitc::METADATA_MACRO:
663      case bitc::METADATA_MACRO_FILE:
664      case bitc::METADATA_TEMPLATE_TYPE:
665      case bitc::METADATA_TEMPLATE_VALUE:
666      case bitc::METADATA_GLOBAL_VAR:
667      case bitc::METADATA_LOCAL_VAR:
668      case bitc::METADATA_EXPRESSION:
669      case bitc::METADATA_OBJC_PROPERTY:
670      case bitc::METADATA_IMPORTED_ENTITY:
671      case bitc::METADATA_GLOBAL_VAR_EXPR:
672        // We don't expect to see any of these, if we see one, give up on
673        // lazy-loading and fallback.
674        MDStringRef.clear();
675        GlobalMetadataBitPosIndex.clear();
676        return false;
677      }
678      break;
679    }
680    }
681  }
682}
683
684/// Parse a METADATA_BLOCK. If ModuleLevel is true then we are parsing
685/// module level metadata.
686Error MetadataLoader::MetadataLoaderImpl::parseMetadata(bool ModuleLevel) {
687  if (!ModuleLevel && MetadataList.hasFwdRefs())
688    return error("Invalid metadata: fwd refs into function blocks");
689
690  // Record the entry position so that we can jump back here and efficiently
691  // skip the whole block in case we lazy-load.
692  auto EntryPos = Stream.GetCurrentBitNo();
693
694  if (Stream.EnterSubBlock(bitc::METADATA_BLOCK_ID))
695    return error("Invalid record");
696
697  SmallVector<uint64_t, 64> Record;
698  PlaceholderQueue Placeholders;
699
700  // We lazy-load module-level metadata: we build an index for each record, and
701  // then load individual record as needed, starting with the named metadata.
702  if (ModuleLevel && IsImporting && MetadataList.empty() &&
703      !DisableLazyLoading) {
704    auto SuccessOrErr = lazyLoadModuleMetadataBlock(Placeholders);
705    if (!SuccessOrErr)
706      return SuccessOrErr.takeError();
707    if (SuccessOrErr.get()) {
708      // An index was successfully created and we will be able to load metadata
709      // on-demand.
710      MetadataList.resize(MDStringRef.size() +
711                          GlobalMetadataBitPosIndex.size());
712
713      // Reading the named metadata created forward references and/or
714      // placeholders, that we flush here.
715      resolveForwardRefsAndPlaceholders(Placeholders);
716      upgradeCUSubprograms();
717      // Return at the beginning of the block, since it is easy to skip it
718      // entirely from there.
719      Stream.ReadBlockEnd(); // Pop the abbrev block context.
720      Stream.JumpToBit(EntryPos);
721      if (Stream.SkipBlock())
722        return error("Invalid record");
723      return Error::success();
724    }
725    // Couldn't load an index, fallback to loading all the block "old-style".
726  }
727
728  unsigned NextMetadataNo = MetadataList.size();
729
730  // Read all the records.
731  while (true) {
732    BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
733
734    switch (Entry.Kind) {
735    case BitstreamEntry::SubBlock: // Handled for us already.
736    case BitstreamEntry::Error:
737      return error("Malformed block");
738    case BitstreamEntry::EndBlock:
739      resolveForwardRefsAndPlaceholders(Placeholders);
740      upgradeCUSubprograms();
741      return Error::success();
742    case BitstreamEntry::Record:
743      // The interesting case.
744      break;
745    }
746
747    // Read a record.
748    Record.clear();
749    StringRef Blob;
750    ++NumMDRecordLoaded;
751    unsigned Code = Stream.readRecord(Entry.ID, Record, &Blob);
752    if (Error Err =
753            parseOneMetadata(Record, Code, Placeholders, Blob, NextMetadataNo))
754      return Err;
755  }
756}
757
758MDString *MetadataLoader::MetadataLoaderImpl::lazyLoadOneMDString(unsigned ID) {
759  ++NumMDStringLoaded;
760  if (Metadata *MD = MetadataList.lookup(ID))
761    return cast<MDString>(MD);
762  auto MDS = MDString::get(Context, MDStringRef[ID]);
763  MetadataList.assignValue(MDS, ID);
764  return MDS;
765}
766
767void MetadataLoader::MetadataLoaderImpl::lazyLoadOneMetadata(
768    unsigned ID, PlaceholderQueue &Placeholders) {
769  assert(ID < (MDStringRef.size()) + GlobalMetadataBitPosIndex.size());
770  assert(ID >= MDStringRef.size() && "Unexpected lazy-loading of MDString");
771#ifndef NDEBUG
772  // Lookup first if the metadata hasn't already been loaded.
773  if (auto *MD = MetadataList.lookup(ID)) {
774    auto *N = dyn_cast_or_null<MDNode>(MD);
775    assert(N && N->isTemporary() && "Lazy loading an already loaded metadata");
776  }
777#endif
778  SmallVector<uint64_t, 64> Record;
779  StringRef Blob;
780  IndexCursor.JumpToBit(GlobalMetadataBitPosIndex[ID - MDStringRef.size()]);
781  auto Entry = IndexCursor.advanceSkippingSubblocks();
782  ++NumMDRecordLoaded;
783  unsigned Code = IndexCursor.readRecord(Entry.ID, Record, &Blob);
784  if (Error Err = parseOneMetadata(Record, Code, Placeholders, Blob, ID))
785    report_fatal_error("Can't lazyload MD");
786}
787
788/// Ensure that all forward-references and placeholders are resolved.
789/// Iteratively lazy-loading metadata on-demand if needed.
790void MetadataLoader::MetadataLoaderImpl::resolveForwardRefsAndPlaceholders(
791    PlaceholderQueue &Placeholders) {
792  DenseSet<unsigned> Temporaries;
793  while (1) {
794    // Populate Temporaries with the placeholders that haven't been loaded yet.
795    Placeholders.getTemporaries(MetadataList, Temporaries);
796
797    // If we don't have any temporary, or FwdReference, we're done!
798    if (Temporaries.empty() && !MetadataList.hasFwdRefs())
799      break;
800
801    // First, load all the temporaries. This can add new placeholders or
802    // forward references.
803    for (auto ID : Temporaries)
804      lazyLoadOneMetadata(ID, Placeholders);
805    Temporaries.clear();
806
807    // Second, load the forward-references. This can also add new placeholders
808    // or forward references.
809    while (MetadataList.hasFwdRefs())
810      lazyLoadOneMetadata(MetadataList.getNextFwdRef(), Placeholders);
811  }
812  // At this point we don't have any forward reference remaining, or temporary
813  // that haven't been loaded. We can safely drop RAUW support and mark cycles
814  // as resolved.
815  MetadataList.tryToResolveCycles();
816
817  // Finally, everything is in place, we can replace the placeholders operands
818  // with the final node they refer to.
819  Placeholders.flush(MetadataList);
820}
821
822Error MetadataLoader::MetadataLoaderImpl::parseOneMetadata(
823    SmallVectorImpl<uint64_t> &Record, unsigned Code,
824    PlaceholderQueue &Placeholders, StringRef Blob, unsigned &NextMetadataNo) {
825
826  bool IsDistinct = false;
827  auto getMD = [&](unsigned ID) -> Metadata * {
828    if (ID < MDStringRef.size())
829      return lazyLoadOneMDString(ID);
830    if (!IsDistinct)
831      return MetadataList.getMetadataFwdRef(ID);
832    if (auto *MD = MetadataList.getMetadataIfResolved(ID))
833      return MD;
834    return &Placeholders.getPlaceholderOp(ID);
835  };
836  auto getMDOrNull = [&](unsigned ID) -> Metadata * {
837    if (ID)
838      return getMD(ID - 1);
839    return nullptr;
840  };
841  auto getMDOrNullWithoutPlaceholders = [&](unsigned ID) -> Metadata * {
842    if (ID)
843      return MetadataList.getMetadataFwdRef(ID - 1);
844    return nullptr;
845  };
846  auto getMDString = [&](unsigned ID) -> MDString * {
847    // This requires that the ID is not really a forward reference.  In
848    // particular, the MDString must already have been resolved.
849    auto MDS = getMDOrNull(ID);
850    return cast_or_null<MDString>(MDS);
851  };
852
853  // Support for old type refs.
854  auto getDITypeRefOrNull = [&](unsigned ID) {
855    return MetadataList.upgradeTypeRef(getMDOrNull(ID));
856  };
857
858#define GET_OR_DISTINCT(CLASS, ARGS)                                           \
859  (IsDistinct ? CLASS::getDistinct ARGS : CLASS::get ARGS)
860
861  switch (Code) {
862  default: // Default behavior: ignore.
863    break;
864  case bitc::METADATA_NAME: {
865    // Read name of the named metadata.
866    SmallString<8> Name(Record.begin(), Record.end());
867    Record.clear();
868    Code = Stream.ReadCode();
869
870    ++NumMDRecordLoaded;
871    unsigned NextBitCode = Stream.readRecord(Code, Record);
872    if (NextBitCode != bitc::METADATA_NAMED_NODE)
873      return error("METADATA_NAME not followed by METADATA_NAMED_NODE");
874
875    // Read named metadata elements.
876    unsigned Size = Record.size();
877    NamedMDNode *NMD = TheModule.getOrInsertNamedMetadata(Name);
878    for (unsigned i = 0; i != Size; ++i) {
879      MDNode *MD = MetadataList.getMDNodeFwdRefOrNull(Record[i]);
880      if (!MD)
881        return error("Invalid record");
882      NMD->addOperand(MD);
883    }
884    break;
885  }
886  case bitc::METADATA_OLD_FN_NODE: {
887    // FIXME: Remove in 4.0.
888    // This is a LocalAsMetadata record, the only type of function-local
889    // metadata.
890    if (Record.size() % 2 == 1)
891      return error("Invalid record");
892
893    // If this isn't a LocalAsMetadata record, we're dropping it.  This used
894    // to be legal, but there's no upgrade path.
895    auto dropRecord = [&] {
896      MetadataList.assignValue(MDNode::get(Context, None), NextMetadataNo++);
897    };
898    if (Record.size() != 2) {
899      dropRecord();
900      break;
901    }
902
903    Type *Ty = getTypeByID(Record[0]);
904    if (Ty->isMetadataTy() || Ty->isVoidTy()) {
905      dropRecord();
906      break;
907    }
908
909    MetadataList.assignValue(
910        LocalAsMetadata::get(ValueList.getValueFwdRef(Record[1], Ty)),
911        NextMetadataNo++);
912    break;
913  }
914  case bitc::METADATA_OLD_NODE: {
915    // FIXME: Remove in 4.0.
916    if (Record.size() % 2 == 1)
917      return error("Invalid record");
918
919    unsigned Size = Record.size();
920    SmallVector<Metadata *, 8> Elts;
921    for (unsigned i = 0; i != Size; i += 2) {
922      Type *Ty = getTypeByID(Record[i]);
923      if (!Ty)
924        return error("Invalid record");
925      if (Ty->isMetadataTy())
926        Elts.push_back(getMD(Record[i + 1]));
927      else if (!Ty->isVoidTy()) {
928        auto *MD =
929            ValueAsMetadata::get(ValueList.getValueFwdRef(Record[i + 1], Ty));
930        assert(isa<ConstantAsMetadata>(MD) &&
931               "Expected non-function-local metadata");
932        Elts.push_back(MD);
933      } else
934        Elts.push_back(nullptr);
935    }
936    MetadataList.assignValue(MDNode::get(Context, Elts), NextMetadataNo++);
937    break;
938  }
939  case bitc::METADATA_VALUE: {
940    if (Record.size() != 2)
941      return error("Invalid record");
942
943    Type *Ty = getTypeByID(Record[0]);
944    if (Ty->isMetadataTy() || Ty->isVoidTy())
945      return error("Invalid record");
946
947    MetadataList.assignValue(
948        ValueAsMetadata::get(ValueList.getValueFwdRef(Record[1], Ty)),
949        NextMetadataNo++);
950    break;
951  }
952  case bitc::METADATA_DISTINCT_NODE:
953    IsDistinct = true;
954    LLVM_FALLTHROUGH;
955  case bitc::METADATA_NODE: {
956    SmallVector<Metadata *, 8> Elts;
957    Elts.reserve(Record.size());
958    for (unsigned ID : Record)
959      Elts.push_back(getMDOrNull(ID));
960    MetadataList.assignValue(IsDistinct ? MDNode::getDistinct(Context, Elts)
961                                        : MDNode::get(Context, Elts),
962                             NextMetadataNo++);
963    break;
964  }
965  case bitc::METADATA_LOCATION: {
966    if (Record.size() != 5)
967      return error("Invalid record");
968
969    IsDistinct = Record[0];
970    unsigned Line = Record[1];
971    unsigned Column = Record[2];
972    Metadata *Scope = getMD(Record[3]);
973    Metadata *InlinedAt = getMDOrNull(Record[4]);
974    MetadataList.assignValue(
975        GET_OR_DISTINCT(DILocation, (Context, Line, Column, Scope, InlinedAt)),
976        NextMetadataNo++);
977    break;
978  }
979  case bitc::METADATA_GENERIC_DEBUG: {
980    if (Record.size() < 4)
981      return error("Invalid record");
982
983    IsDistinct = Record[0];
984    unsigned Tag = Record[1];
985    unsigned Version = Record[2];
986
987    if (Tag >= 1u << 16 || Version != 0)
988      return error("Invalid record");
989
990    auto *Header = getMDString(Record[3]);
991    SmallVector<Metadata *, 8> DwarfOps;
992    for (unsigned I = 4, E = Record.size(); I != E; ++I)
993      DwarfOps.push_back(getMDOrNull(Record[I]));
994    MetadataList.assignValue(
995        GET_OR_DISTINCT(GenericDINode, (Context, Tag, Header, DwarfOps)),
996        NextMetadataNo++);
997    break;
998  }
999  case bitc::METADATA_SUBRANGE: {
1000    if (Record.size() != 3)
1001      return error("Invalid record");
1002
1003    IsDistinct = Record[0];
1004    MetadataList.assignValue(
1005        GET_OR_DISTINCT(DISubrange,
1006                        (Context, Record[1], unrotateSign(Record[2]))),
1007        NextMetadataNo++);
1008    break;
1009  }
1010  case bitc::METADATA_ENUMERATOR: {
1011    if (Record.size() != 3)
1012      return error("Invalid record");
1013
1014    IsDistinct = Record[0];
1015    MetadataList.assignValue(
1016        GET_OR_DISTINCT(DIEnumerator, (Context, unrotateSign(Record[1]),
1017                                       getMDString(Record[2]))),
1018        NextMetadataNo++);
1019    break;
1020  }
1021  case bitc::METADATA_BASIC_TYPE: {
1022    if (Record.size() != 6)
1023      return error("Invalid record");
1024
1025    IsDistinct = Record[0];
1026    MetadataList.assignValue(
1027        GET_OR_DISTINCT(DIBasicType,
1028                        (Context, Record[1], getMDString(Record[2]), Record[3],
1029                         Record[4], Record[5])),
1030        NextMetadataNo++);
1031    break;
1032  }
1033  case bitc::METADATA_DERIVED_TYPE: {
1034    if (Record.size() != 12)
1035      return error("Invalid record");
1036
1037    IsDistinct = Record[0];
1038    DINode::DIFlags Flags = static_cast<DINode::DIFlags>(Record[10]);
1039    MetadataList.assignValue(
1040        GET_OR_DISTINCT(DIDerivedType,
1041                        (Context, Record[1], getMDString(Record[2]),
1042                         getMDOrNull(Record[3]), Record[4],
1043                         getDITypeRefOrNull(Record[5]),
1044                         getDITypeRefOrNull(Record[6]), Record[7], Record[8],
1045                         Record[9], Flags, getDITypeRefOrNull(Record[11]))),
1046        NextMetadataNo++);
1047    break;
1048  }
1049  case bitc::METADATA_COMPOSITE_TYPE: {
1050    if (Record.size() != 16)
1051      return error("Invalid record");
1052
1053    // If we have a UUID and this is not a forward declaration, lookup the
1054    // mapping.
1055    IsDistinct = Record[0] & 0x1;
1056    bool IsNotUsedInTypeRef = Record[0] >= 2;
1057    unsigned Tag = Record[1];
1058    MDString *Name = getMDString(Record[2]);
1059    Metadata *File = getMDOrNull(Record[3]);
1060    unsigned Line = Record[4];
1061    Metadata *Scope = getDITypeRefOrNull(Record[5]);
1062    Metadata *BaseType = nullptr;
1063    uint64_t SizeInBits = Record[7];
1064    if (Record[8] > (uint64_t)std::numeric_limits<uint32_t>::max())
1065      return error("Alignment value is too large");
1066    uint32_t AlignInBits = Record[8];
1067    uint64_t OffsetInBits = 0;
1068    DINode::DIFlags Flags = static_cast<DINode::DIFlags>(Record[10]);
1069    Metadata *Elements = nullptr;
1070    unsigned RuntimeLang = Record[12];
1071    Metadata *VTableHolder = nullptr;
1072    Metadata *TemplateParams = nullptr;
1073    auto *Identifier = getMDString(Record[15]);
1074    // If this module is being parsed so that it can be ThinLTO imported
1075    // into another module, composite types only need to be imported
1076    // as type declarations (unless full type definitions requested).
1077    // Create type declarations up front to save memory. Also, buildODRType
1078    // handles the case where this is type ODRed with a definition needed
1079    // by the importing module, in which case the existing definition is
1080    // used.
1081    if (IsImporting && !ImportFullTypeDefinitions && Identifier &&
1082        (Tag == dwarf::DW_TAG_enumeration_type ||
1083         Tag == dwarf::DW_TAG_class_type ||
1084         Tag == dwarf::DW_TAG_structure_type ||
1085         Tag == dwarf::DW_TAG_union_type)) {
1086      Flags = Flags | DINode::FlagFwdDecl;
1087    } else {
1088      BaseType = getDITypeRefOrNull(Record[6]);
1089      OffsetInBits = Record[9];
1090      Elements = getMDOrNull(Record[11]);
1091      VTableHolder = getDITypeRefOrNull(Record[13]);
1092      TemplateParams = getMDOrNull(Record[14]);
1093    }
1094    DICompositeType *CT = nullptr;
1095    if (Identifier)
1096      CT = DICompositeType::buildODRType(
1097          Context, *Identifier, Tag, Name, File, Line, Scope, BaseType,
1098          SizeInBits, AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
1099          VTableHolder, TemplateParams);
1100
1101    // Create a node if we didn't get a lazy ODR type.
1102    if (!CT)
1103      CT = GET_OR_DISTINCT(DICompositeType,
1104                           (Context, Tag, Name, File, Line, Scope, BaseType,
1105                            SizeInBits, AlignInBits, OffsetInBits, Flags,
1106                            Elements, RuntimeLang, VTableHolder, TemplateParams,
1107                            Identifier));
1108    if (!IsNotUsedInTypeRef && Identifier)
1109      MetadataList.addTypeRef(*Identifier, *cast<DICompositeType>(CT));
1110
1111    MetadataList.assignValue(CT, NextMetadataNo++);
1112    break;
1113  }
1114  case bitc::METADATA_SUBROUTINE_TYPE: {
1115    if (Record.size() < 3 || Record.size() > 4)
1116      return error("Invalid record");
1117    bool IsOldTypeRefArray = Record[0] < 2;
1118    unsigned CC = (Record.size() > 3) ? Record[3] : 0;
1119
1120    IsDistinct = Record[0] & 0x1;
1121    DINode::DIFlags Flags = static_cast<DINode::DIFlags>(Record[1]);
1122    Metadata *Types = getMDOrNull(Record[2]);
1123    if (LLVM_UNLIKELY(IsOldTypeRefArray))
1124      Types = MetadataList.upgradeTypeRefArray(Types);
1125
1126    MetadataList.assignValue(
1127        GET_OR_DISTINCT(DISubroutineType, (Context, Flags, CC, Types)),
1128        NextMetadataNo++);
1129    break;
1130  }
1131
1132  case bitc::METADATA_MODULE: {
1133    if (Record.size() != 6)
1134      return error("Invalid record");
1135
1136    IsDistinct = Record[0];
1137    MetadataList.assignValue(
1138        GET_OR_DISTINCT(DIModule,
1139                        (Context, getMDOrNull(Record[1]),
1140                         getMDString(Record[2]), getMDString(Record[3]),
1141                         getMDString(Record[4]), getMDString(Record[5]))),
1142        NextMetadataNo++);
1143    break;
1144  }
1145
1146  case bitc::METADATA_FILE: {
1147    if (Record.size() != 3 && Record.size() != 5)
1148      return error("Invalid record");
1149
1150    IsDistinct = Record[0];
1151    MetadataList.assignValue(
1152        GET_OR_DISTINCT(
1153            DIFile,
1154            (Context, getMDString(Record[1]), getMDString(Record[2]),
1155             Record.size() == 3 ? DIFile::CSK_None
1156                                : static_cast<DIFile::ChecksumKind>(Record[3]),
1157             Record.size() == 3 ? nullptr : getMDString(Record[4]))),
1158        NextMetadataNo++);
1159    break;
1160  }
1161  case bitc::METADATA_COMPILE_UNIT: {
1162    if (Record.size() < 14 || Record.size() > 17)
1163      return error("Invalid record");
1164
1165    // Ignore Record[0], which indicates whether this compile unit is
1166    // distinct.  It's always distinct.
1167    IsDistinct = true;
1168    auto *CU = DICompileUnit::getDistinct(
1169        Context, Record[1], getMDOrNull(Record[2]), getMDString(Record[3]),
1170        Record[4], getMDString(Record[5]), Record[6], getMDString(Record[7]),
1171        Record[8], getMDOrNull(Record[9]), getMDOrNull(Record[10]),
1172        getMDOrNull(Record[12]), getMDOrNull(Record[13]),
1173        Record.size() <= 15 ? nullptr : getMDOrNull(Record[15]),
1174        Record.size() <= 14 ? 0 : Record[14],
1175        Record.size() <= 16 ? true : Record[16]);
1176
1177    MetadataList.assignValue(CU, NextMetadataNo++);
1178
1179    // Move the Upgrade the list of subprograms.
1180    if (Metadata *SPs = getMDOrNullWithoutPlaceholders(Record[11]))
1181      CUSubprograms.push_back({CU, SPs});
1182    break;
1183  }
1184  case bitc::METADATA_SUBPROGRAM: {
1185    if (Record.size() < 18 || Record.size() > 20)
1186      return error("Invalid record");
1187
1188    IsDistinct =
1189        (Record[0] & 1) || Record[8]; // All definitions should be distinct.
1190    // Version 1 has a Function as Record[15].
1191    // Version 2 has removed Record[15].
1192    // Version 3 has the Unit as Record[15].
1193    // Version 4 added thisAdjustment.
1194    bool HasUnit = Record[0] >= 2;
1195    if (HasUnit && Record.size() < 19)
1196      return error("Invalid record");
1197    Metadata *CUorFn = getMDOrNull(Record[15]);
1198    unsigned Offset = Record.size() >= 19 ? 1 : 0;
1199    bool HasFn = Offset && !HasUnit;
1200    bool HasThisAdj = Record.size() >= 20;
1201    DISubprogram *SP = GET_OR_DISTINCT(
1202        DISubprogram, (Context,
1203                       getDITypeRefOrNull(Record[1]),          // scope
1204                       getMDString(Record[2]),                 // name
1205                       getMDString(Record[3]),                 // linkageName
1206                       getMDOrNull(Record[4]),                 // file
1207                       Record[5],                              // line
1208                       getMDOrNull(Record[6]),                 // type
1209                       Record[7],                              // isLocal
1210                       Record[8],                              // isDefinition
1211                       Record[9],                              // scopeLine
1212                       getDITypeRefOrNull(Record[10]),         // containingType
1213                       Record[11],                             // virtuality
1214                       Record[12],                             // virtualIndex
1215                       HasThisAdj ? Record[19] : 0,            // thisAdjustment
1216                       static_cast<DINode::DIFlags>(Record[13] // flags
1217                                                    ),
1218                       Record[14],                       // isOptimized
1219                       HasUnit ? CUorFn : nullptr,       // unit
1220                       getMDOrNull(Record[15 + Offset]), // templateParams
1221                       getMDOrNull(Record[16 + Offset]), // declaration
1222                       getMDOrNull(Record[17 + Offset])  // variables
1223                       ));
1224    MetadataList.assignValue(SP, NextMetadataNo++);
1225
1226    // Upgrade sp->function mapping to function->sp mapping.
1227    if (HasFn) {
1228      if (auto *CMD = dyn_cast_or_null<ConstantAsMetadata>(CUorFn))
1229        if (auto *F = dyn_cast<Function>(CMD->getValue())) {
1230          if (F->isMaterializable())
1231            // Defer until materialized; unmaterialized functions may not have
1232            // metadata.
1233            FunctionsWithSPs[F] = SP;
1234          else if (!F->empty())
1235            F->setSubprogram(SP);
1236        }
1237    }
1238    break;
1239  }
1240  case bitc::METADATA_LEXICAL_BLOCK: {
1241    if (Record.size() != 5)
1242      return error("Invalid record");
1243
1244    IsDistinct = Record[0];
1245    MetadataList.assignValue(
1246        GET_OR_DISTINCT(DILexicalBlock,
1247                        (Context, getMDOrNull(Record[1]),
1248                         getMDOrNull(Record[2]), Record[3], Record[4])),
1249        NextMetadataNo++);
1250    break;
1251  }
1252  case bitc::METADATA_LEXICAL_BLOCK_FILE: {
1253    if (Record.size() != 4)
1254      return error("Invalid record");
1255
1256    IsDistinct = Record[0];
1257    MetadataList.assignValue(
1258        GET_OR_DISTINCT(DILexicalBlockFile,
1259                        (Context, getMDOrNull(Record[1]),
1260                         getMDOrNull(Record[2]), Record[3])),
1261        NextMetadataNo++);
1262    break;
1263  }
1264  case bitc::METADATA_NAMESPACE: {
1265    if (Record.size() != 5)
1266      return error("Invalid record");
1267
1268    IsDistinct = Record[0] & 1;
1269    bool ExportSymbols = Record[0] & 2;
1270    MetadataList.assignValue(
1271        GET_OR_DISTINCT(DINamespace,
1272                        (Context, getMDOrNull(Record[1]),
1273                         getMDOrNull(Record[2]), getMDString(Record[3]),
1274                         Record[4], ExportSymbols)),
1275        NextMetadataNo++);
1276    break;
1277  }
1278  case bitc::METADATA_MACRO: {
1279    if (Record.size() != 5)
1280      return error("Invalid record");
1281
1282    IsDistinct = Record[0];
1283    MetadataList.assignValue(
1284        GET_OR_DISTINCT(DIMacro,
1285                        (Context, Record[1], Record[2], getMDString(Record[3]),
1286                         getMDString(Record[4]))),
1287        NextMetadataNo++);
1288    break;
1289  }
1290  case bitc::METADATA_MACRO_FILE: {
1291    if (Record.size() != 5)
1292      return error("Invalid record");
1293
1294    IsDistinct = Record[0];
1295    MetadataList.assignValue(
1296        GET_OR_DISTINCT(DIMacroFile,
1297                        (Context, Record[1], Record[2], getMDOrNull(Record[3]),
1298                         getMDOrNull(Record[4]))),
1299        NextMetadataNo++);
1300    break;
1301  }
1302  case bitc::METADATA_TEMPLATE_TYPE: {
1303    if (Record.size() != 3)
1304      return error("Invalid record");
1305
1306    IsDistinct = Record[0];
1307    MetadataList.assignValue(GET_OR_DISTINCT(DITemplateTypeParameter,
1308                                             (Context, getMDString(Record[1]),
1309                                              getDITypeRefOrNull(Record[2]))),
1310                             NextMetadataNo++);
1311    break;
1312  }
1313  case bitc::METADATA_TEMPLATE_VALUE: {
1314    if (Record.size() != 5)
1315      return error("Invalid record");
1316
1317    IsDistinct = Record[0];
1318    MetadataList.assignValue(
1319        GET_OR_DISTINCT(DITemplateValueParameter,
1320                        (Context, Record[1], getMDString(Record[2]),
1321                         getDITypeRefOrNull(Record[3]),
1322                         getMDOrNull(Record[4]))),
1323        NextMetadataNo++);
1324    break;
1325  }
1326  case bitc::METADATA_GLOBAL_VAR: {
1327    if (Record.size() < 11 || Record.size() > 12)
1328      return error("Invalid record");
1329
1330    IsDistinct = Record[0] & 1;
1331    unsigned Version = Record[0] >> 1;
1332
1333    if (Version == 1) {
1334      MetadataList.assignValue(
1335          GET_OR_DISTINCT(DIGlobalVariable,
1336                          (Context, getMDOrNull(Record[1]),
1337                           getMDString(Record[2]), getMDString(Record[3]),
1338                           getMDOrNull(Record[4]), Record[5],
1339                           getDITypeRefOrNull(Record[6]), Record[7], Record[8],
1340                           getMDOrNull(Record[10]), Record[11])),
1341          NextMetadataNo++);
1342    } else if (Version == 0) {
1343      // Upgrade old metadata, which stored a global variable reference or a
1344      // ConstantInt here.
1345      Metadata *Expr = getMDOrNull(Record[9]);
1346      uint32_t AlignInBits = 0;
1347      if (Record.size() > 11) {
1348        if (Record[11] > (uint64_t)std::numeric_limits<uint32_t>::max())
1349          return error("Alignment value is too large");
1350        AlignInBits = Record[11];
1351      }
1352      GlobalVariable *Attach = nullptr;
1353      if (auto *CMD = dyn_cast_or_null<ConstantAsMetadata>(Expr)) {
1354        if (auto *GV = dyn_cast<GlobalVariable>(CMD->getValue())) {
1355          Attach = GV;
1356          Expr = nullptr;
1357        } else if (auto *CI = dyn_cast<ConstantInt>(CMD->getValue())) {
1358          Expr = DIExpression::get(Context,
1359                                   {dwarf::DW_OP_constu, CI->getZExtValue(),
1360                                    dwarf::DW_OP_stack_value});
1361        } else {
1362          Expr = nullptr;
1363        }
1364      }
1365      DIGlobalVariable *DGV = GET_OR_DISTINCT(
1366          DIGlobalVariable,
1367          (Context, getMDOrNull(Record[1]), getMDString(Record[2]),
1368           getMDString(Record[3]), getMDOrNull(Record[4]), Record[5],
1369           getDITypeRefOrNull(Record[6]), Record[7], Record[8],
1370           getMDOrNull(Record[10]), AlignInBits));
1371
1372      auto *DGVE = DIGlobalVariableExpression::getDistinct(Context, DGV, Expr);
1373      MetadataList.assignValue(DGVE, NextMetadataNo++);
1374      if (Attach)
1375        Attach->addDebugInfo(DGVE);
1376    } else
1377      return error("Invalid record");
1378
1379    break;
1380  }
1381  case bitc::METADATA_LOCAL_VAR: {
1382    // 10th field is for the obseleted 'inlinedAt:' field.
1383    if (Record.size() < 8 || Record.size() > 10)
1384      return error("Invalid record");
1385
1386    IsDistinct = Record[0] & 1;
1387    bool HasAlignment = Record[0] & 2;
1388    // 2nd field used to be an artificial tag, either DW_TAG_auto_variable or
1389    // DW_TAG_arg_variable, if we have alignment flag encoded it means, that
1390    // this is newer version of record which doesn't have artifical tag.
1391    bool HasTag = !HasAlignment && Record.size() > 8;
1392    DINode::DIFlags Flags = static_cast<DINode::DIFlags>(Record[7 + HasTag]);
1393    uint32_t AlignInBits = 0;
1394    if (HasAlignment) {
1395      if (Record[8 + HasTag] > (uint64_t)std::numeric_limits<uint32_t>::max())
1396        return error("Alignment value is too large");
1397      AlignInBits = Record[8 + HasTag];
1398    }
1399    MetadataList.assignValue(
1400        GET_OR_DISTINCT(DILocalVariable,
1401                        (Context, getMDOrNull(Record[1 + HasTag]),
1402                         getMDString(Record[2 + HasTag]),
1403                         getMDOrNull(Record[3 + HasTag]), Record[4 + HasTag],
1404                         getDITypeRefOrNull(Record[5 + HasTag]),
1405                         Record[6 + HasTag], Flags, AlignInBits)),
1406        NextMetadataNo++);
1407    break;
1408  }
1409  case bitc::METADATA_EXPRESSION: {
1410    if (Record.size() < 1)
1411      return error("Invalid record");
1412
1413    IsDistinct = Record[0] & 1;
1414    bool HasOpFragment = Record[0] & 2;
1415    auto Elts = MutableArrayRef<uint64_t>(Record).slice(1);
1416    if (!HasOpFragment)
1417      if (unsigned N = Elts.size())
1418        if (N >= 3 && Elts[N - 3] == dwarf::DW_OP_bit_piece)
1419          Elts[N - 3] = dwarf::DW_OP_LLVM_fragment;
1420
1421    MetadataList.assignValue(
1422        GET_OR_DISTINCT(DIExpression, (Context, makeArrayRef(Record).slice(1))),
1423        NextMetadataNo++);
1424    break;
1425  }
1426  case bitc::METADATA_GLOBAL_VAR_EXPR: {
1427    if (Record.size() != 3)
1428      return error("Invalid record");
1429
1430    IsDistinct = Record[0];
1431    MetadataList.assignValue(GET_OR_DISTINCT(DIGlobalVariableExpression,
1432                                             (Context, getMDOrNull(Record[1]),
1433                                              getMDOrNull(Record[2]))),
1434                             NextMetadataNo++);
1435    break;
1436  }
1437  case bitc::METADATA_OBJC_PROPERTY: {
1438    if (Record.size() != 8)
1439      return error("Invalid record");
1440
1441    IsDistinct = Record[0];
1442    MetadataList.assignValue(
1443        GET_OR_DISTINCT(DIObjCProperty,
1444                        (Context, getMDString(Record[1]),
1445                         getMDOrNull(Record[2]), Record[3],
1446                         getMDString(Record[4]), getMDString(Record[5]),
1447                         Record[6], getDITypeRefOrNull(Record[7]))),
1448        NextMetadataNo++);
1449    break;
1450  }
1451  case bitc::METADATA_IMPORTED_ENTITY: {
1452    if (Record.size() != 6)
1453      return error("Invalid record");
1454
1455    IsDistinct = Record[0];
1456    MetadataList.assignValue(
1457        GET_OR_DISTINCT(DIImportedEntity,
1458                        (Context, Record[1], getMDOrNull(Record[2]),
1459                         getDITypeRefOrNull(Record[3]), Record[4],
1460                         getMDString(Record[5]))),
1461        NextMetadataNo++);
1462    break;
1463  }
1464  case bitc::METADATA_STRING_OLD: {
1465    std::string String(Record.begin(), Record.end());
1466
1467    // Test for upgrading !llvm.loop.
1468    HasSeenOldLoopTags |= mayBeOldLoopAttachmentTag(String);
1469    ++NumMDStringLoaded;
1470    Metadata *MD = MDString::get(Context, String);
1471    MetadataList.assignValue(MD, NextMetadataNo++);
1472    break;
1473  }
1474  case bitc::METADATA_STRINGS: {
1475    auto CreateNextMDString = [&](StringRef Str) {
1476      ++NumMDStringLoaded;
1477      MetadataList.assignValue(MDString::get(Context, Str), NextMetadataNo++);
1478    };
1479    if (Error Err = parseMetadataStrings(Record, Blob, CreateNextMDString))
1480      return Err;
1481    break;
1482  }
1483  case bitc::METADATA_GLOBAL_DECL_ATTACHMENT: {
1484    if (Record.size() % 2 == 0)
1485      return error("Invalid record");
1486    unsigned ValueID = Record[0];
1487    if (ValueID >= ValueList.size())
1488      return error("Invalid record");
1489    if (auto *GO = dyn_cast<GlobalObject>(ValueList[ValueID]))
1490      if (Error Err = parseGlobalObjectAttachment(
1491              *GO, ArrayRef<uint64_t>(Record).slice(1)))
1492        return Err;
1493    break;
1494  }
1495  case bitc::METADATA_KIND: {
1496    // Support older bitcode files that had METADATA_KIND records in a
1497    // block with METADATA_BLOCK_ID.
1498    if (Error Err = parseMetadataKindRecord(Record))
1499      return Err;
1500    break;
1501  }
1502  }
1503  return Error::success();
1504#undef GET_OR_DISTINCT
1505}
1506
1507Error MetadataLoader::MetadataLoaderImpl::parseMetadataStrings(
1508    ArrayRef<uint64_t> Record, StringRef Blob,
1509    std::function<void(StringRef)> CallBack) {
1510  // All the MDStrings in the block are emitted together in a single
1511  // record.  The strings are concatenated and stored in a blob along with
1512  // their sizes.
1513  if (Record.size() != 2)
1514    return error("Invalid record: metadata strings layout");
1515
1516  unsigned NumStrings = Record[0];
1517  unsigned StringsOffset = Record[1];
1518  if (!NumStrings)
1519    return error("Invalid record: metadata strings with no strings");
1520  if (StringsOffset > Blob.size())
1521    return error("Invalid record: metadata strings corrupt offset");
1522
1523  StringRef Lengths = Blob.slice(0, StringsOffset);
1524  SimpleBitstreamCursor R(Lengths);
1525
1526  StringRef Strings = Blob.drop_front(StringsOffset);
1527  do {
1528    if (R.AtEndOfStream())
1529      return error("Invalid record: metadata strings bad length");
1530
1531    unsigned Size = R.ReadVBR(6);
1532    if (Strings.size() < Size)
1533      return error("Invalid record: metadata strings truncated chars");
1534
1535    CallBack(Strings.slice(0, Size));
1536    Strings = Strings.drop_front(Size);
1537  } while (--NumStrings);
1538
1539  return Error::success();
1540}
1541
1542Error MetadataLoader::MetadataLoaderImpl::parseGlobalObjectAttachment(
1543    GlobalObject &GO, ArrayRef<uint64_t> Record) {
1544  assert(Record.size() % 2 == 0);
1545  for (unsigned I = 0, E = Record.size(); I != E; I += 2) {
1546    auto K = MDKindMap.find(Record[I]);
1547    if (K == MDKindMap.end())
1548      return error("Invalid ID");
1549    MDNode *MD = MetadataList.getMDNodeFwdRefOrNull(Record[I + 1]);
1550    if (!MD)
1551      return error("Invalid metadata attachment");
1552    GO.addMetadata(K->second, *MD);
1553  }
1554  return Error::success();
1555}
1556
1557/// Parse metadata attachments.
1558Error MetadataLoader::MetadataLoaderImpl::parseMetadataAttachment(
1559    Function &F, const SmallVectorImpl<Instruction *> &InstructionList) {
1560  if (Stream.EnterSubBlock(bitc::METADATA_ATTACHMENT_ID))
1561    return error("Invalid record");
1562
1563  SmallVector<uint64_t, 64> Record;
1564
1565  PlaceholderQueue Placeholders;
1566
1567  while (true) {
1568    BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
1569
1570    switch (Entry.Kind) {
1571    case BitstreamEntry::SubBlock: // Handled for us already.
1572    case BitstreamEntry::Error:
1573      return error("Malformed block");
1574    case BitstreamEntry::EndBlock:
1575      resolveForwardRefsAndPlaceholders(Placeholders);
1576      return Error::success();
1577    case BitstreamEntry::Record:
1578      // The interesting case.
1579      break;
1580    }
1581
1582    // Read a metadata attachment record.
1583    Record.clear();
1584    ++NumMDRecordLoaded;
1585    switch (Stream.readRecord(Entry.ID, Record)) {
1586    default: // Default behavior: ignore.
1587      break;
1588    case bitc::METADATA_ATTACHMENT: {
1589      unsigned RecordLength = Record.size();
1590      if (Record.empty())
1591        return error("Invalid record");
1592      if (RecordLength % 2 == 0) {
1593        // A function attachment.
1594        if (Error Err = parseGlobalObjectAttachment(F, Record))
1595          return Err;
1596        continue;
1597      }
1598
1599      // An instruction attachment.
1600      Instruction *Inst = InstructionList[Record[0]];
1601      for (unsigned i = 1; i != RecordLength; i = i + 2) {
1602        unsigned Kind = Record[i];
1603        DenseMap<unsigned, unsigned>::iterator I = MDKindMap.find(Kind);
1604        if (I == MDKindMap.end())
1605          return error("Invalid ID");
1606        if (I->second == LLVMContext::MD_tbaa && StripTBAA)
1607          continue;
1608
1609        auto Idx = Record[i + 1];
1610        if (Idx < (MDStringRef.size() + GlobalMetadataBitPosIndex.size()) &&
1611            !MetadataList.lookup(Idx))
1612          // Load the attachment if it is in the lazy-loadable range and hasn't
1613          // been loaded yet.
1614          lazyLoadOneMetadata(Idx, Placeholders);
1615
1616        Metadata *Node = MetadataList.getMetadataFwdRef(Idx);
1617        if (isa<LocalAsMetadata>(Node))
1618          // Drop the attachment.  This used to be legal, but there's no
1619          // upgrade path.
1620          break;
1621        MDNode *MD = dyn_cast_or_null<MDNode>(Node);
1622        if (!MD)
1623          return error("Invalid metadata attachment");
1624
1625        if (HasSeenOldLoopTags && I->second == LLVMContext::MD_loop)
1626          MD = upgradeInstructionLoopAttachment(*MD);
1627
1628        if (I->second == LLVMContext::MD_tbaa) {
1629          assert(!MD->isTemporary() && "should load MDs before attachments");
1630          MD = UpgradeTBAANode(*MD);
1631        }
1632        Inst->setMetadata(I->second, MD);
1633      }
1634      break;
1635    }
1636    }
1637  }
1638}
1639
1640/// Parse a single METADATA_KIND record, inserting result in MDKindMap.
1641Error MetadataLoader::MetadataLoaderImpl::parseMetadataKindRecord(
1642    SmallVectorImpl<uint64_t> &Record) {
1643  if (Record.size() < 2)
1644    return error("Invalid record");
1645
1646  unsigned Kind = Record[0];
1647  SmallString<8> Name(Record.begin() + 1, Record.end());
1648
1649  unsigned NewKind = TheModule.getMDKindID(Name.str());
1650  if (!MDKindMap.insert(std::make_pair(Kind, NewKind)).second)
1651    return error("Conflicting METADATA_KIND records");
1652  return Error::success();
1653}
1654
1655/// Parse the metadata kinds out of the METADATA_KIND_BLOCK.
1656Error MetadataLoader::MetadataLoaderImpl::parseMetadataKinds() {
1657  if (Stream.EnterSubBlock(bitc::METADATA_KIND_BLOCK_ID))
1658    return error("Invalid record");
1659
1660  SmallVector<uint64_t, 64> Record;
1661
1662  // Read all the records.
1663  while (true) {
1664    BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
1665
1666    switch (Entry.Kind) {
1667    case BitstreamEntry::SubBlock: // Handled for us already.
1668    case BitstreamEntry::Error:
1669      return error("Malformed block");
1670    case BitstreamEntry::EndBlock:
1671      return Error::success();
1672    case BitstreamEntry::Record:
1673      // The interesting case.
1674      break;
1675    }
1676
1677    // Read a record.
1678    Record.clear();
1679    ++NumMDRecordLoaded;
1680    unsigned Code = Stream.readRecord(Entry.ID, Record);
1681    switch (Code) {
1682    default: // Default behavior: ignore.
1683      break;
1684    case bitc::METADATA_KIND: {
1685      if (Error Err = parseMetadataKindRecord(Record))
1686        return Err;
1687      break;
1688    }
1689    }
1690  }
1691}
1692
1693MetadataLoader &MetadataLoader::operator=(MetadataLoader &&RHS) {
1694  Pimpl = std::move(RHS.Pimpl);
1695  return *this;
1696}
1697MetadataLoader::MetadataLoader(MetadataLoader &&RHS)
1698    : Pimpl(std::move(RHS.Pimpl)) {}
1699
1700MetadataLoader::~MetadataLoader() = default;
1701MetadataLoader::MetadataLoader(BitstreamCursor &Stream, Module &TheModule,
1702                               BitcodeReaderValueList &ValueList,
1703                               bool IsImporting,
1704                               std::function<Type *(unsigned)> getTypeByID)
1705    : Pimpl(llvm::make_unique<MetadataLoaderImpl>(Stream, TheModule, ValueList,
1706                                                  getTypeByID, IsImporting)) {}
1707
1708Error MetadataLoader::parseMetadata(bool ModuleLevel) {
1709  return Pimpl->parseMetadata(ModuleLevel);
1710}
1711
1712bool MetadataLoader::hasFwdRefs() const { return Pimpl->hasFwdRefs(); }
1713
1714/// Return the given metadata, creating a replaceable forward reference if
1715/// necessary.
1716Metadata *MetadataLoader::getMetadataFwdRef(unsigned Idx) {
1717  return Pimpl->getMetadataFwdRef(Idx);
1718}
1719
1720MDNode *MetadataLoader::getMDNodeFwdRefOrNull(unsigned Idx) {
1721  return Pimpl->getMDNodeFwdRefOrNull(Idx);
1722}
1723
1724DISubprogram *MetadataLoader::lookupSubprogramForFunction(Function *F) {
1725  return Pimpl->lookupSubprogramForFunction(F);
1726}
1727
1728Error MetadataLoader::parseMetadataAttachment(
1729    Function &F, const SmallVectorImpl<Instruction *> &InstructionList) {
1730  return Pimpl->parseMetadataAttachment(F, InstructionList);
1731}
1732
1733Error MetadataLoader::parseMetadataKinds() {
1734  return Pimpl->parseMetadataKinds();
1735}
1736
1737void MetadataLoader::setStripTBAA(bool StripTBAA) {
1738  return Pimpl->setStripTBAA(StripTBAA);
1739}
1740
1741bool MetadataLoader::isStrippingTBAA() { return Pimpl->isStrippingTBAA(); }
1742
1743unsigned MetadataLoader::size() const { return Pimpl->size(); }
1744void MetadataLoader::shrinkTo(unsigned N) { return Pimpl->shrinkTo(N); }
1745