LLVM  15.0.0git
BitcodeReader.cpp
Go to the documentation of this file.
1 //===- BitcodeReader.cpp - Internal BitcodeReader implementation ----------===//
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 
10 #include "MetadataLoader.h"
11 #include "ValueList.h"
12 #include "llvm/ADT/APFloat.h"
13 #include "llvm/ADT/APInt.h"
14 #include "llvm/ADT/ArrayRef.h"
15 #include "llvm/ADT/DenseMap.h"
16 #include "llvm/ADT/Optional.h"
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/ADT/SmallString.h"
19 #include "llvm/ADT/SmallVector.h"
20 #include "llvm/ADT/StringRef.h"
21 #include "llvm/ADT/Triple.h"
22 #include "llvm/ADT/Twine.h"
26 #include "llvm/Config/llvm-config.h"
27 #include "llvm/IR/Argument.h"
28 #include "llvm/IR/Attributes.h"
29 #include "llvm/IR/AutoUpgrade.h"
30 #include "llvm/IR/BasicBlock.h"
31 #include "llvm/IR/CallingConv.h"
32 #include "llvm/IR/Comdat.h"
33 #include "llvm/IR/Constant.h"
34 #include "llvm/IR/Constants.h"
35 #include "llvm/IR/DataLayout.h"
36 #include "llvm/IR/DebugInfo.h"
38 #include "llvm/IR/DebugLoc.h"
39 #include "llvm/IR/DerivedTypes.h"
40 #include "llvm/IR/Function.h"
41 #include "llvm/IR/GVMaterializer.h"
43 #include "llvm/IR/GlobalAlias.h"
44 #include "llvm/IR/GlobalIFunc.h"
45 #include "llvm/IR/GlobalObject.h"
46 #include "llvm/IR/GlobalValue.h"
47 #include "llvm/IR/GlobalVariable.h"
48 #include "llvm/IR/InlineAsm.h"
49 #include "llvm/IR/InstIterator.h"
50 #include "llvm/IR/InstrTypes.h"
51 #include "llvm/IR/Instruction.h"
52 #include "llvm/IR/Instructions.h"
53 #include "llvm/IR/Intrinsics.h"
54 #include "llvm/IR/IntrinsicsAArch64.h"
55 #include "llvm/IR/IntrinsicsARM.h"
56 #include "llvm/IR/LLVMContext.h"
57 #include "llvm/IR/Metadata.h"
58 #include "llvm/IR/Module.h"
60 #include "llvm/IR/Operator.h"
61 #include "llvm/IR/Type.h"
62 #include "llvm/IR/Value.h"
63 #include "llvm/IR/Verifier.h"
65 #include "llvm/Support/Casting.h"
67 #include "llvm/Support/Compiler.h"
68 #include "llvm/Support/Debug.h"
69 #include "llvm/Support/Error.h"
71 #include "llvm/Support/ErrorOr.h"
76 #include <algorithm>
77 #include <cassert>
78 #include <cstddef>
79 #include <cstdint>
80 #include <deque>
81 #include <map>
82 #include <memory>
83 #include <set>
84 #include <string>
85 #include <system_error>
86 #include <tuple>
87 #include <utility>
88 #include <vector>
89 
90 using namespace llvm;
91 
93  "print-summary-global-ids", cl::init(false), cl::Hidden,
94  cl::desc(
95  "Print the global id for each value when reading the module summary"));
96 
97 namespace {
98 
99 enum {
100  SWITCH_INST_MAGIC = 0x4B5 // May 2012 => 1205 => Hex
101 };
102 
103 } // end anonymous namespace
104 
105 static Error error(const Twine &Message) {
106  return make_error<StringError>(
108 }
109 
111  if (!Stream.canSkipToPos(4))
112  return createStringError(std::errc::illegal_byte_sequence,
113  "file too small to contain bitcode header");
114  for (unsigned C : {'B', 'C'})
115  if (Expected<SimpleBitstreamCursor::word_t> Res = Stream.Read(8)) {
116  if (Res.get() != C)
117  return createStringError(std::errc::illegal_byte_sequence,
118  "file doesn't start with bitcode header");
119  } else
120  return Res.takeError();
121  for (unsigned C : {0x0, 0xC, 0xE, 0xD})
122  if (Expected<SimpleBitstreamCursor::word_t> Res = Stream.Read(4)) {
123  if (Res.get() != C)
124  return createStringError(std::errc::illegal_byte_sequence,
125  "file doesn't start with bitcode header");
126  } else
127  return Res.takeError();
128  return Error::success();
129 }
130 
132  const unsigned char *BufPtr = (const unsigned char *)Buffer.getBufferStart();
133  const unsigned char *BufEnd = BufPtr + Buffer.getBufferSize();
134 
135  if (Buffer.getBufferSize() & 3)
136  return error("Invalid bitcode signature");
137 
138  // If we have a wrapper header, parse it and ignore the non-bc file contents.
139  // The magic number is 0x0B17C0DE stored in little endian.
140  if (isBitcodeWrapper(BufPtr, BufEnd))
141  if (SkipBitcodeWrapperHeader(BufPtr, BufEnd, true))
142  return error("Invalid bitcode wrapper header");
143 
144  BitstreamCursor Stream(ArrayRef<uint8_t>(BufPtr, BufEnd));
145  if (Error Err = hasInvalidBitcodeHeader(Stream))
146  return std::move(Err);
147 
148  return std::move(Stream);
149 }
150 
151 /// Convert a string from a record into an std::string, return true on failure.
152 template <typename StrTy>
153 static bool convertToString(ArrayRef<uint64_t> Record, unsigned Idx,
154  StrTy &Result) {
155  if (Idx > Record.size())
156  return true;
157 
158  Result.append(Record.begin() + Idx, Record.end());
159  return false;
160 }
161 
162 // Strip all the TBAA attachment for the module.
163 static void stripTBAA(Module *M) {
164  for (auto &F : *M) {
165  if (F.isMaterializable())
166  continue;
167  for (auto &I : instructions(F))
168  I.setMetadata(LLVMContext::MD_tbaa, nullptr);
169  }
170 }
171 
172 /// Read the "IDENTIFICATION_BLOCK_ID" block, do some basic enforcement on the
173 /// "epoch" encoded in the bitcode, and return the producer name if any.
175  if (Error Err = Stream.EnterSubBlock(bitc::IDENTIFICATION_BLOCK_ID))
176  return std::move(Err);
177 
178  // Read all the records.
180 
181  std::string ProducerIdentification;
182 
183  while (true) {
184  BitstreamEntry Entry;
185  if (Error E = Stream.advance().moveInto(Entry))
186  return std::move(E);
187 
188  switch (Entry.Kind) {
189  default:
191  return error("Malformed block");
193  return ProducerIdentification;
195  // The interesting case.
196  break;
197  }
198 
199  // Read a record.
200  Record.clear();
201  Expected<unsigned> MaybeBitCode = Stream.readRecord(Entry.ID, Record);
202  if (!MaybeBitCode)
203  return MaybeBitCode.takeError();
204  switch (MaybeBitCode.get()) {
205  default: // Default behavior: reject
206  return error("Invalid value");
207  case bitc::IDENTIFICATION_CODE_STRING: // IDENTIFICATION: [strchr x N]
208  convertToString(Record, 0, ProducerIdentification);
209  break;
210  case bitc::IDENTIFICATION_CODE_EPOCH: { // EPOCH: [epoch#]
211  unsigned epoch = (unsigned)Record[0];
212  if (epoch != bitc::BITCODE_CURRENT_EPOCH) {
213  return error(
214  Twine("Incompatible epoch: Bitcode '") + Twine(epoch) +
215  "' vs current: '" + Twine(bitc::BITCODE_CURRENT_EPOCH) + "'");
216  }
217  }
218  }
219  }
220 }
221 
223  // We expect a number of well-defined blocks, though we don't necessarily
224  // need to understand them all.
225  while (true) {
226  if (Stream.AtEndOfStream())
227  return "";
228 
229  BitstreamEntry Entry;
230  if (Error E = Stream.advance().moveInto(Entry))
231  return std::move(E);
232 
233  switch (Entry.Kind) {
236  return error("Malformed block");
237 
239  if (Entry.ID == bitc::IDENTIFICATION_BLOCK_ID)
240  return readIdentificationBlock(Stream);
241 
242  // Ignore other sub-blocks.
243  if (Error Err = Stream.SkipBlock())
244  return std::move(Err);
245  continue;
247  if (Error E = Stream.skipRecord(Entry.ID).takeError())
248  return std::move(E);
249  continue;
250  }
251  }
252 }
253 
255  if (Error Err = Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID))
256  return std::move(Err);
257 
259  // Read all the records for this module.
260 
261  while (true) {
262  Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks();
263  if (!MaybeEntry)
264  return MaybeEntry.takeError();
265  BitstreamEntry Entry = MaybeEntry.get();
266 
267  switch (Entry.Kind) {
268  case BitstreamEntry::SubBlock: // Handled for us already.
270  return error("Malformed block");
272  return false;
274  // The interesting case.
275  break;
276  }
277 
278  // Read a record.
279  Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record);
280  if (!MaybeRecord)
281  return MaybeRecord.takeError();
282  switch (MaybeRecord.get()) {
283  default:
284  break; // Default behavior, ignore unknown content.
285  case bitc::MODULE_CODE_SECTIONNAME: { // SECTIONNAME: [strchr x N]
286  std::string S;
287  if (convertToString(Record, 0, S))
288  return error("Invalid section name record");
289  // Check for the i386 and other (x86_64, ARM) conventions
290  if (S.find("__DATA,__objc_catlist") != std::string::npos ||
291  S.find("__OBJC,__category") != std::string::npos)
292  return true;
293  break;
294  }
295  }
296  Record.clear();
297  }
298  llvm_unreachable("Exit infinite loop");
299 }
300 
302  // We expect a number of well-defined blocks, though we don't necessarily
303  // need to understand them all.
304  while (true) {
305  BitstreamEntry Entry;
306  if (Error E = Stream.advance().moveInto(Entry))
307  return std::move(E);
308 
309  switch (Entry.Kind) {
311  return error("Malformed block");
313  return false;
314 
316  if (Entry.ID == bitc::MODULE_BLOCK_ID)
317  return hasObjCCategoryInModule(Stream);
318 
319  // Ignore other sub-blocks.
320  if (Error Err = Stream.SkipBlock())
321  return std::move(Err);
322  continue;
323 
325  if (Error E = Stream.skipRecord(Entry.ID).takeError())
326  return std::move(E);
327  continue;
328  }
329  }
330 }
331 
333  if (Error Err = Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID))
334  return std::move(Err);
335 
337 
338  std::string Triple;
339 
340  // Read all the records for this module.
341  while (true) {
342  Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks();
343  if (!MaybeEntry)
344  return MaybeEntry.takeError();
345  BitstreamEntry Entry = MaybeEntry.get();
346 
347  switch (Entry.Kind) {
348  case BitstreamEntry::SubBlock: // Handled for us already.
350  return error("Malformed block");
352  return Triple;
354  // The interesting case.
355  break;
356  }
357 
358  // Read a record.
359  Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record);
360  if (!MaybeRecord)
361  return MaybeRecord.takeError();
362  switch (MaybeRecord.get()) {
363  default: break; // Default behavior, ignore unknown content.
364  case bitc::MODULE_CODE_TRIPLE: { // TRIPLE: [strchr x N]
365  std::string S;
366  if (convertToString(Record, 0, S))
367  return error("Invalid triple record");
368  Triple = S;
369  break;
370  }
371  }
372  Record.clear();
373  }
374  llvm_unreachable("Exit infinite loop");
375 }
376 
378  // We expect a number of well-defined blocks, though we don't necessarily
379  // need to understand them all.
380  while (true) {
381  Expected<BitstreamEntry> MaybeEntry = Stream.advance();
382  if (!MaybeEntry)
383  return MaybeEntry.takeError();
384  BitstreamEntry Entry = MaybeEntry.get();
385 
386  switch (Entry.Kind) {
388  return error("Malformed block");
390  return "";
391 
393  if (Entry.ID == bitc::MODULE_BLOCK_ID)
394  return readModuleTriple(Stream);
395 
396  // Ignore other sub-blocks.
397  if (Error Err = Stream.SkipBlock())
398  return std::move(Err);
399  continue;
400 
402  if (llvm::Expected<unsigned> Skipped = Stream.skipRecord(Entry.ID))
403  continue;
404  else
405  return Skipped.takeError();
406  }
407  }
408 }
409 
410 namespace {
411 
412 class BitcodeReaderBase {
413 protected:
414  BitcodeReaderBase(BitstreamCursor Stream, StringRef Strtab)
415  : Stream(std::move(Stream)), Strtab(Strtab) {
416  this->Stream.setBlockInfo(&BlockInfo);
417  }
418 
419  BitstreamBlockInfo BlockInfo;
421  StringRef Strtab;
422 
423  /// In version 2 of the bitcode we store names of global values and comdats in
424  /// a string table rather than in the VST.
425  bool UseStrtab = false;
426 
427  Expected<unsigned> parseVersionRecord(ArrayRef<uint64_t> Record);
428 
429  /// If this module uses a string table, pop the reference to the string table
430  /// and return the referenced string and the rest of the record. Otherwise
431  /// just return the record itself.
432  std::pair<StringRef, ArrayRef<uint64_t>>
433  readNameFromStrtab(ArrayRef<uint64_t> Record);
434 
435  Error readBlockInfo();
436 
437  // Contains an arbitrary and optional string identifying the bitcode producer
438  std::string ProducerIdentification;
439 
440  Error error(const Twine &Message);
441 };
442 
443 } // end anonymous namespace
444 
445 Error BitcodeReaderBase::error(const Twine &Message) {
446  std::string FullMsg = Message.str();
447  if (!ProducerIdentification.empty())
448  FullMsg += " (Producer: '" + ProducerIdentification + "' Reader: 'LLVM " +
449  LLVM_VERSION_STRING "')";
450  return ::error(FullMsg);
451 }
452 
454 BitcodeReaderBase::parseVersionRecord(ArrayRef<uint64_t> Record) {
455  if (Record.empty())
456  return error("Invalid version record");
457  unsigned ModuleVersion = Record[0];
458  if (ModuleVersion > 2)
459  return error("Invalid value");
460  UseStrtab = ModuleVersion >= 2;
461  return ModuleVersion;
462 }
463 
464 std::pair<StringRef, ArrayRef<uint64_t>>
465 BitcodeReaderBase::readNameFromStrtab(ArrayRef<uint64_t> Record) {
466  if (!UseStrtab)
467  return {"", Record};
468  // Invalid reference. Let the caller complain about the record being empty.
469  if (Record[0] + Record[1] > Strtab.size())
470  return {"", {}};
471  return {StringRef(Strtab.data() + Record[0], Record[1]), Record.slice(2)};
472 }
473 
474 namespace {
475 
476 class BitcodeReader : public BitcodeReaderBase, public GVMaterializer {
478  Module *TheModule = nullptr;
479  // Next offset to start scanning for lazy parsing of function bodies.
480  uint64_t NextUnreadBit = 0;
481  // Last function offset found in the VST.
482  uint64_t LastFunctionBlockBit = 0;
483  bool SeenValueSymbolTable = false;
484  uint64_t VSTOffset = 0;
485 
486  std::vector<std::string> SectionTable;
487  std::vector<std::string> GCTable;
488 
489  std::vector<Type *> TypeList;
490  /// Track type IDs of contained types. Order is the same as the contained
491  /// types of a Type*. This is used during upgrades of typed pointer IR in
492  /// opaque pointer mode.
494  /// In some cases, we need to create a type ID for a type that was not
495  /// explicitly encoded in the bitcode, or we don't know about at the current
496  /// point. For example, a global may explicitly encode the value type ID, but
497  /// not have a type ID for the pointer to value type, for which we create a
498  /// virtual type ID instead. This map stores the new type ID that was created
499  /// for the given pair of Type and contained type ID.
500  DenseMap<std::pair<Type *, unsigned>, unsigned> VirtualTypeIDs;
501  DenseMap<Function *, unsigned> FunctionTypeIDs;
502  BitcodeReaderValueList ValueList;
503  Optional<MetadataLoader> MDLoader;
504  std::vector<Comdat *> ComdatList;
505  DenseSet<GlobalObject *> ImplicitComdatObjects;
506  SmallVector<Instruction *, 64> InstructionList;
507 
508  std::vector<std::pair<GlobalVariable *, unsigned>> GlobalInits;
509  std::vector<std::pair<GlobalValue *, unsigned>> IndirectSymbolInits;
510 
511  struct FunctionOperandInfo {
512  Function *F;
513  unsigned PersonalityFn;
514  unsigned Prefix;
515  unsigned Prologue;
516  };
517  std::vector<FunctionOperandInfo> FunctionOperands;
518 
519  /// The set of attributes by index. Index zero in the file is for null, and
520  /// is thus not represented here. As such all indices are off by one.
521  std::vector<AttributeList> MAttributes;
522 
523  /// The set of attribute groups.
524  std::map<unsigned, AttributeList> MAttributeGroups;
525 
526  /// While parsing a function body, this is a list of the basic blocks for the
527  /// function.
528  std::vector<BasicBlock*> FunctionBBs;
529 
530  // When reading the module header, this list is populated with functions that
531  // have bodies later in the file.
532  std::vector<Function*> FunctionsWithBodies;
533 
534  // When intrinsic functions are encountered which require upgrading they are
535  // stored here with their replacement function.
536  using UpdatedIntrinsicMap = DenseMap<Function *, Function *>;
537  UpdatedIntrinsicMap UpgradedIntrinsics;
538  // Intrinsics which were remangled because of types rename
539  UpdatedIntrinsicMap RemangledIntrinsics;
540 
541  // Several operations happen after the module header has been read, but
542  // before function bodies are processed. This keeps track of whether
543  // we've done this yet.
544  bool SeenFirstFunctionBody = false;
545 
546  /// When function bodies are initially scanned, this map contains info about
547  /// where to find deferred function body in the stream.
548  DenseMap<Function*, uint64_t> DeferredFunctionInfo;
549 
550  /// When Metadata block is initially scanned when parsing the module, we may
551  /// choose to defer parsing of the metadata. This vector contains info about
552  /// which Metadata blocks are deferred.
553  std::vector<uint64_t> DeferredMetadataInfo;
554 
555  /// These are basic blocks forward-referenced by block addresses. They are
556  /// inserted lazily into functions when they're loaded. The basic block ID is
557  /// its index into the vector.
559  std::deque<Function *> BasicBlockFwdRefQueue;
560 
561  /// These are Functions that contain BlockAddresses which refer a different
562  /// Function. When parsing the different Function, queue Functions that refer
563  /// to the different Function. Those Functions must be materialized in order
564  /// to resolve their BlockAddress constants before the different Function
565  /// gets moved into another Module.
566  std::vector<Function *> BackwardRefFunctions;
567 
568  /// Indicates that we are using a new encoding for instruction operands where
569  /// most operands in the current FUNCTION_BLOCK are encoded relative to the
570  /// instruction number, for a more compact encoding. Some instruction
571  /// operands are not relative to the instruction ID: basic block numbers, and
572  /// types. Once the old style function blocks have been phased out, we would
573  /// not need this flag.
574  bool UseRelativeIDs = false;
575 
576  /// True if all functions will be materialized, negating the need to process
577  /// (e.g.) blockaddress forward references.
578  bool WillMaterializeAllForwardRefs = false;
579 
580  bool StripDebugInfo = false;
581  TBAAVerifier TBAAVerifyHelper;
582 
583  std::vector<std::string> BundleTags;
585 
586 public:
587  BitcodeReader(BitstreamCursor Stream, StringRef Strtab,
588  StringRef ProducerIdentification, LLVMContext &Context);
589 
590  Error materializeForwardReferencedFunctions();
591 
592  Error materialize(GlobalValue *GV) override;
593  Error materializeModule() override;
594  std::vector<StructType *> getIdentifiedStructTypes() const override;
595 
596  /// Main interface to parsing a bitcode buffer.
597  /// \returns true if an error occurred.
598  Error parseBitcodeInto(
599  Module *M, bool ShouldLazyLoadMetadata = false, bool IsImporting = false,
600  DataLayoutCallbackTy DataLayoutCallback = [](StringRef) { return None; });
601 
603 
604  /// Materialize any deferred Metadata block.
605  Error materializeMetadata() override;
606 
607  void setStripDebugInfo() override;
608 
609 private:
610  std::vector<StructType *> IdentifiedStructTypes;
611  StructType *createIdentifiedStructType(LLVMContext &Context, StringRef Name);
612  StructType *createIdentifiedStructType(LLVMContext &Context);
613 
614  static constexpr unsigned InvalidTypeID = ~0u;
615 
616  Type *getTypeByID(unsigned ID);
617  Type *getPtrElementTypeByID(unsigned ID);
618  unsigned getContainedTypeID(unsigned ID, unsigned Idx = 0);
619  unsigned getVirtualTypeID(Type *Ty, ArrayRef<unsigned> ContainedTypeIDs = {});
620 
621  Value *getFnValueByID(unsigned ID, Type *Ty, unsigned TyID) {
622  if (Ty && Ty->isMetadataTy())
623  return MetadataAsValue::get(Ty->getContext(), getFnMetadataByID(ID));
624  return ValueList.getValueFwdRef(ID, Ty, TyID);
625  }
626 
627  Metadata *getFnMetadataByID(unsigned ID) {
628  return MDLoader->getMetadataFwdRefOrLoad(ID);
629  }
630 
631  BasicBlock *getBasicBlock(unsigned ID) const {
632  if (ID >= FunctionBBs.size()) return nullptr; // Invalid ID
633  return FunctionBBs[ID];
634  }
635 
636  AttributeList getAttributes(unsigned i) const {
637  if (i-1 < MAttributes.size())
638  return MAttributes[i-1];
639  return AttributeList();
640  }
641 
642  /// Read a value/type pair out of the specified record from slot 'Slot'.
643  /// Increment Slot past the number of slots used in the record. Return true on
644  /// failure.
645  bool getValueTypePair(const SmallVectorImpl<uint64_t> &Record, unsigned &Slot,
646  unsigned InstNum, Value *&ResVal, unsigned &TypeID) {
647  if (Slot == Record.size()) return true;
648  unsigned ValNo = (unsigned)Record[Slot++];
649  // Adjust the ValNo, if it was encoded relative to the InstNum.
650  if (UseRelativeIDs)
651  ValNo = InstNum - ValNo;
652  if (ValNo < InstNum) {
653  // If this is not a forward reference, just return the value we already
654  // have.
655  TypeID = ValueList.getTypeID(ValNo);
656  ResVal = getFnValueByID(ValNo, nullptr, TypeID);
657  assert((!ResVal || ResVal->getType() == getTypeByID(TypeID)) &&
658  "Incorrect type ID stored for value");
659  return ResVal == nullptr;
660  }
661  if (Slot == Record.size())
662  return true;
663 
664  TypeID = (unsigned)Record[Slot++];
665  ResVal = getFnValueByID(ValNo, getTypeByID(TypeID), TypeID);
666  return ResVal == nullptr;
667  }
668 
669  /// Read a value out of the specified record from slot 'Slot'. Increment Slot
670  /// past the number of slots used by the value in the record. Return true if
671  /// there is an error.
672  bool popValue(const SmallVectorImpl<uint64_t> &Record, unsigned &Slot,
673  unsigned InstNum, Type *Ty, unsigned TyID, Value *&ResVal) {
674  if (getValue(Record, Slot, InstNum, Ty, TyID, ResVal))
675  return true;
676  // All values currently take a single record slot.
677  ++Slot;
678  return false;
679  }
680 
681  /// Like popValue, but does not increment the Slot number.
682  bool getValue(const SmallVectorImpl<uint64_t> &Record, unsigned Slot,
683  unsigned InstNum, Type *Ty, unsigned TyID, Value *&ResVal) {
684  ResVal = getValue(Record, Slot, InstNum, Ty, TyID);
685  return ResVal == nullptr;
686  }
687 
688  /// Version of getValue that returns ResVal directly, or 0 if there is an
689  /// error.
690  Value *getValue(const SmallVectorImpl<uint64_t> &Record, unsigned Slot,
691  unsigned InstNum, Type *Ty, unsigned TyID) {
692  if (Slot == Record.size()) return nullptr;
693  unsigned ValNo = (unsigned)Record[Slot];
694  // Adjust the ValNo, if it was encoded relative to the InstNum.
695  if (UseRelativeIDs)
696  ValNo = InstNum - ValNo;
697  return getFnValueByID(ValNo, Ty, TyID);
698  }
699 
700  /// Like getValue, but decodes signed VBRs.
701  Value *getValueSigned(const SmallVectorImpl<uint64_t> &Record, unsigned Slot,
702  unsigned InstNum, Type *Ty, unsigned TyID) {
703  if (Slot == Record.size()) return nullptr;
704  unsigned ValNo = (unsigned)decodeSignRotatedValue(Record[Slot]);
705  // Adjust the ValNo, if it was encoded relative to the InstNum.
706  if (UseRelativeIDs)
707  ValNo = InstNum - ValNo;
708  return getFnValueByID(ValNo, Ty, TyID);
709  }
710 
711  /// Upgrades old-style typeless byval/sret/inalloca attributes by adding the
712  /// corresponding argument's pointee type. Also upgrades intrinsics that now
713  /// require an elementtype attribute.
714  Error propagateAttributeTypes(CallBase *CB, ArrayRef<unsigned> ArgsTys);
715 
716  /// Converts alignment exponent (i.e. power of two (or zero)) to the
717  /// corresponding alignment to use. If alignment is too large, returns
718  /// a corresponding error code.
719  Error parseAlignmentValue(uint64_t Exponent, MaybeAlign &Alignment);
720  Error parseAttrKind(uint64_t Code, Attribute::AttrKind *Kind);
722  uint64_t ResumeBit, bool ShouldLazyLoadMetadata = false,
723  DataLayoutCallbackTy DataLayoutCallback = [](StringRef) { return None; });
724 
725  Error parseComdatRecord(ArrayRef<uint64_t> Record);
726  Error parseGlobalVarRecord(ArrayRef<uint64_t> Record);
727  Error parseFunctionRecord(ArrayRef<uint64_t> Record);
728  Error parseGlobalIndirectSymbolRecord(unsigned BitCode,
730 
731  Error parseAttributeBlock();
732  Error parseAttributeGroupBlock();
733  Error parseTypeTable();
734  Error parseTypeTableBody();
735  Error parseOperandBundleTags();
736  Error parseSyncScopeNames();
737 
739  unsigned NameIndex, Triple &TT);
740  void setDeferredFunctionInfo(unsigned FuncBitcodeOffsetDelta, Function *F,
742  Error parseValueSymbolTable(uint64_t Offset = 0);
743  Error parseGlobalValueSymbolTable();
744  Error parseConstants();
745  Error rememberAndSkipFunctionBodies();
746  Error rememberAndSkipFunctionBody();
747  /// Save the positions of the Metadata blocks and skip parsing the blocks.
748  Error rememberAndSkipMetadata();
749  Error typeCheckLoadStoreInst(Type *ValType, Type *PtrType);
750  Error parseFunctionBody(Function *F);
751  Error globalCleanup();
752  Error resolveGlobalAndIndirectSymbolInits();
753  Error parseUseLists();
754  Error findFunctionInStream(
755  Function *F,
756  DenseMap<Function *, uint64_t>::iterator DeferredFunctionInfoIterator);
757 
758  SyncScope::ID getDecodedSyncScopeID(unsigned Val);
759 };
760 
761 /// Class to manage reading and parsing function summary index bitcode
762 /// files/sections.
763 class ModuleSummaryIndexBitcodeReader : public BitcodeReaderBase {
764  /// The module index built during parsing.
765  ModuleSummaryIndex &TheIndex;
766 
767  /// Indicates whether we have encountered a global value summary section
768  /// yet during parsing.
769  bool SeenGlobalValSummary = false;
770 
771  /// Indicates whether we have already parsed the VST, used for error checking.
772  bool SeenValueSymbolTable = false;
773 
774  /// Set to the offset of the VST recorded in the MODULE_CODE_VSTOFFSET record.
775  /// Used to enable on-demand parsing of the VST.
776  uint64_t VSTOffset = 0;
777 
778  // Map to save ValueId to ValueInfo association that was recorded in the
779  // ValueSymbolTable. It is used after the VST is parsed to convert
780  // call graph edges read from the function summary from referencing
781  // callees by their ValueId to using the ValueInfo instead, which is how
782  // they are recorded in the summary index being built.
783  // We save a GUID which refers to the same global as the ValueInfo, but
784  // ignoring the linkage, i.e. for values other than local linkage they are
785  // identical.
787  ValueIdToValueInfoMap;
788 
789  /// Map populated during module path string table parsing, from the
790  /// module ID to a string reference owned by the index's module
791  /// path string table, used to correlate with combined index
792  /// summary records.
793  DenseMap<uint64_t, StringRef> ModuleIdMap;
794 
795  /// Original source file name recorded in a bitcode record.
796  std::string SourceFileName;
797 
798  /// The string identifier given to this module by the client, normally the
799  /// path to the bitcode file.
800  StringRef ModulePath;
801 
802  /// For per-module summary indexes, the unique numerical identifier given to
803  /// this module by the client.
804  unsigned ModuleId;
805 
806 public:
807  ModuleSummaryIndexBitcodeReader(BitstreamCursor Stream, StringRef Strtab,
808  ModuleSummaryIndex &TheIndex,
809  StringRef ModulePath, unsigned ModuleId);
810 
811  Error parseModule();
812 
813 private:
814  void setValueGUID(uint64_t ValueID, StringRef ValueName,
816  StringRef SourceFileName);
817  Error parseValueSymbolTable(
818  uint64_t Offset,
819  DenseMap<unsigned, GlobalValue::LinkageTypes> &ValueIdToLinkageMap);
820  std::vector<ValueInfo> makeRefList(ArrayRef<uint64_t> Record);
821  std::vector<FunctionSummary::EdgeTy> makeCallList(ArrayRef<uint64_t> Record,
822  bool IsOldProfileFormat,
823  bool HasProfile,
824  bool HasRelBF);
825  Error parseEntireSummary(unsigned ID);
826  Error parseModuleStringTable();
827  void parseTypeIdCompatibleVtableSummaryRecord(ArrayRef<uint64_t> Record);
828  void parseTypeIdCompatibleVtableInfo(ArrayRef<uint64_t> Record, size_t &Slot,
830  std::vector<FunctionSummary::ParamAccess>
831  parseParamAccesses(ArrayRef<uint64_t> Record);
832 
833  std::pair<ValueInfo, GlobalValue::GUID>
834  getValueInfoFromValueId(unsigned ValueId);
835 
836  void addThisModule();
837  ModuleSummaryIndex::ModuleInfo *getThisModule();
838 };
839 
840 } // end anonymous namespace
841 
843  Error Err) {
844  if (Err) {
845  std::error_code EC;
846  handleAllErrors(std::move(Err), [&](ErrorInfoBase &EIB) {
847  EC = EIB.convertToErrorCode();
848  Ctx.emitError(EIB.message());
849  });
850  return EC;
851  }
852  return std::error_code();
853 }
854 
855 BitcodeReader::BitcodeReader(BitstreamCursor Stream, StringRef Strtab,
856  StringRef ProducerIdentification,
858  : BitcodeReaderBase(std::move(Stream), Strtab), Context(Context),
859  ValueList(Context, this->Stream.SizeInBytes()) {
860  this->ProducerIdentification = std::string(ProducerIdentification);
861 }
862 
863 Error BitcodeReader::materializeForwardReferencedFunctions() {
864  if (WillMaterializeAllForwardRefs)
865  return Error::success();
866 
867  // Prevent recursion.
868  WillMaterializeAllForwardRefs = true;
869 
870  while (!BasicBlockFwdRefQueue.empty()) {
871  Function *F = BasicBlockFwdRefQueue.front();
872  BasicBlockFwdRefQueue.pop_front();
873  assert(F && "Expected valid function");
874  if (!BasicBlockFwdRefs.count(F))
875  // Already materialized.
876  continue;
877 
878  // Check for a function that isn't materializable to prevent an infinite
879  // loop. When parsing a blockaddress stored in a global variable, there
880  // isn't a trivial way to check if a function will have a body without a
881  // linear search through FunctionsWithBodies, so just check it here.
882  if (!F->isMaterializable())
883  return error("Never resolved function from blockaddress");
884 
885  // Try to materialize F.
886  if (Error Err = materialize(F))
887  return Err;
888  }
889  assert(BasicBlockFwdRefs.empty() && "Function missing from queue");
890 
891  for (Function *F : BackwardRefFunctions)
892  if (Error Err = materialize(F))
893  return Err;
894  BackwardRefFunctions.clear();
895 
896  // Reset state.
897  WillMaterializeAllForwardRefs = false;
898  return Error::success();
899 }
900 
901 //===----------------------------------------------------------------------===//
902 // Helper functions to implement forward reference resolution, etc.
903 //===----------------------------------------------------------------------===//
904 
905 static bool hasImplicitComdat(size_t Val) {
906  switch (Val) {
907  default:
908  return false;
909  case 1: // Old WeakAnyLinkage
910  case 4: // Old LinkOnceAnyLinkage
911  case 10: // Old WeakODRLinkage
912  case 11: // Old LinkOnceODRLinkage
913  return true;
914  }
915 }
916 
918  switch (Val) {
919  default: // Map unknown/new linkages to external
920  case 0:
921  return GlobalValue::ExternalLinkage;
922  case 2:
923  return GlobalValue::AppendingLinkage;
924  case 3:
925  return GlobalValue::InternalLinkage;
926  case 5:
927  return GlobalValue::ExternalLinkage; // Obsolete DLLImportLinkage
928  case 6:
929  return GlobalValue::ExternalLinkage; // Obsolete DLLExportLinkage
930  case 7:
931  return GlobalValue::ExternalWeakLinkage;
932  case 8:
933  return GlobalValue::CommonLinkage;
934  case 9:
935  return GlobalValue::PrivateLinkage;
936  case 12:
937  return GlobalValue::AvailableExternallyLinkage;
938  case 13:
939  return GlobalValue::PrivateLinkage; // Obsolete LinkerPrivateLinkage
940  case 14:
941  return GlobalValue::PrivateLinkage; // Obsolete LinkerPrivateWeakLinkage
942  case 15:
943  return GlobalValue::ExternalLinkage; // Obsolete LinkOnceODRAutoHideLinkage
944  case 1: // Old value with implicit comdat.
945  case 16:
946  return GlobalValue::WeakAnyLinkage;
947  case 10: // Old value with implicit comdat.
948  case 17:
949  return GlobalValue::WeakODRLinkage;
950  case 4: // Old value with implicit comdat.
951  case 18:
952  return GlobalValue::LinkOnceAnyLinkage;
953  case 11: // Old value with implicit comdat.
954  case 19:
955  return GlobalValue::LinkOnceODRLinkage;
956  }
957 }
958 
961  Flags.ReadNone = RawFlags & 0x1;
962  Flags.ReadOnly = (RawFlags >> 1) & 0x1;
963  Flags.NoRecurse = (RawFlags >> 2) & 0x1;
964  Flags.ReturnDoesNotAlias = (RawFlags >> 3) & 0x1;
965  Flags.NoInline = (RawFlags >> 4) & 0x1;
966  Flags.AlwaysInline = (RawFlags >> 5) & 0x1;
967  Flags.NoUnwind = (RawFlags >> 6) & 0x1;
968  Flags.MayThrow = (RawFlags >> 7) & 0x1;
969  Flags.HasUnknownCall = (RawFlags >> 8) & 0x1;
970  Flags.MustBeUnreachable = (RawFlags >> 9) & 0x1;
971  return Flags;
972 }
973 
974 // Decode the flags for GlobalValue in the summary. The bits for each attribute:
975 //
976 // linkage: [0,4), notEligibleToImport: 4, live: 5, local: 6, canAutoHide: 7,
977 // visibility: [8, 10).
979  uint64_t Version) {
980  // Summary were not emitted before LLVM 3.9, we don't need to upgrade Linkage
981  // like getDecodedLinkage() above. Any future change to the linkage enum and
982  // to getDecodedLinkage() will need to be taken into account here as above.
983  auto Linkage = GlobalValue::LinkageTypes(RawFlags & 0xF); // 4 bits
984  auto Visibility = GlobalValue::VisibilityTypes((RawFlags >> 8) & 3); // 2 bits
985  RawFlags = RawFlags >> 4;
986  bool NotEligibleToImport = (RawFlags & 0x1) || Version < 3;
987  // The Live flag wasn't introduced until version 3. For dead stripping
988  // to work correctly on earlier versions, we must conservatively treat all
989  // values as live.
990  bool Live = (RawFlags & 0x2) || Version < 3;
991  bool Local = (RawFlags & 0x4);
992  bool AutoHide = (RawFlags & 0x8);
993 
994  return GlobalValueSummary::GVFlags(Linkage, Visibility, NotEligibleToImport,
995  Live, Local, AutoHide);
996 }
997 
998 // Decode the flags for GlobalVariable in the summary
1001  (RawFlags & 0x1) ? true : false, (RawFlags & 0x2) ? true : false,
1002  (RawFlags & 0x4) ? true : false,
1003  (GlobalObject::VCallVisibility)(RawFlags >> 3));
1004 }
1005 
1007  switch (Val) {
1008  default: // Map unknown visibilities to default.
1009  case 0: return GlobalValue::DefaultVisibility;
1010  case 1: return GlobalValue::HiddenVisibility;
1011  case 2: return GlobalValue::ProtectedVisibility;
1012  }
1013 }
1014 
1017  switch (Val) {
1018  default: // Map unknown values to default.
1019  case 0: return GlobalValue::DefaultStorageClass;
1020  case 1: return GlobalValue::DLLImportStorageClass;
1021  case 2: return GlobalValue::DLLExportStorageClass;
1022  }
1023 }
1024 
1025 static bool getDecodedDSOLocal(unsigned Val) {
1026  switch(Val) {
1027  default: // Map unknown values to preemptable.
1028  case 0: return false;
1029  case 1: return true;
1030  }
1031 }
1032 
1033 static GlobalVariable::ThreadLocalMode getDecodedThreadLocalMode(unsigned Val) {
1034  switch (Val) {
1035  case 0: return GlobalVariable::NotThreadLocal;
1036  default: // Map unknown non-zero value to general dynamic.
1037  case 1: return GlobalVariable::GeneralDynamicTLSModel;
1038  case 2: return GlobalVariable::LocalDynamicTLSModel;
1039  case 3: return GlobalVariable::InitialExecTLSModel;
1040  case 4: return GlobalVariable::LocalExecTLSModel;
1041  }
1042 }
1043 
1044 static GlobalVariable::UnnamedAddr getDecodedUnnamedAddrType(unsigned Val) {
1045  switch (Val) {
1046  default: // Map unknown to UnnamedAddr::None.
1047  case 0: return GlobalVariable::UnnamedAddr::None;
1048  case 1: return GlobalVariable::UnnamedAddr::Global;
1049  case 2: return GlobalVariable::UnnamedAddr::Local;
1050  }
1051 }
1052 
1053 static int getDecodedCastOpcode(unsigned Val) {
1054  switch (Val) {
1055  default: return -1;
1056  case bitc::CAST_TRUNC : return Instruction::Trunc;
1057  case bitc::CAST_ZEXT : return Instruction::ZExt;
1058  case bitc::CAST_SEXT : return Instruction::SExt;
1059  case bitc::CAST_FPTOUI : return Instruction::FPToUI;
1060  case bitc::CAST_FPTOSI : return Instruction::FPToSI;
1061  case bitc::CAST_UITOFP : return Instruction::UIToFP;
1062  case bitc::CAST_SITOFP : return Instruction::SIToFP;
1063  case bitc::CAST_FPTRUNC : return Instruction::FPTrunc;
1064  case bitc::CAST_FPEXT : return Instruction::FPExt;
1065  case bitc::CAST_PTRTOINT: return Instruction::PtrToInt;
1066  case bitc::CAST_INTTOPTR: return Instruction::IntToPtr;
1067  case bitc::CAST_BITCAST : return Instruction::BitCast;
1068  case bitc::CAST_ADDRSPACECAST: return Instruction::AddrSpaceCast;
1069  }
1070 }
1071 
1072 static int getDecodedUnaryOpcode(unsigned Val, Type *Ty) {
1073  bool IsFP = Ty->isFPOrFPVectorTy();
1074  // UnOps are only valid for int/fp or vector of int/fp types
1075  if (!IsFP && !Ty->isIntOrIntVectorTy())
1076  return -1;
1077 
1078  switch (Val) {
1079  default:
1080  return -1;
1081  case bitc::UNOP_FNEG:
1082  return IsFP ? Instruction::FNeg : -1;
1083  }
1084 }
1085 
1086 static int getDecodedBinaryOpcode(unsigned Val, Type *Ty) {
1087  bool IsFP = Ty->isFPOrFPVectorTy();
1088  // BinOps are only valid for int/fp or vector of int/fp types
1089  if (!IsFP && !Ty->isIntOrIntVectorTy())
1090  return -1;
1091 
1092  switch (Val) {
1093  default:
1094  return -1;
1095  case bitc::BINOP_ADD:
1096  return IsFP ? Instruction::FAdd : Instruction::Add;
1097  case bitc::BINOP_SUB:
1098  return IsFP ? Instruction::FSub : Instruction::Sub;
1099  case bitc::BINOP_MUL:
1100  return IsFP ? Instruction::FMul : Instruction::Mul;
1101  case bitc::BINOP_UDIV:
1102  return IsFP ? -1 : Instruction::UDiv;
1103  case bitc::BINOP_SDIV:
1104  return IsFP ? Instruction::FDiv : Instruction::SDiv;
1105  case bitc::BINOP_UREM:
1106  return IsFP ? -1 : Instruction::URem;
1107  case bitc::BINOP_SREM:
1108  return IsFP ? Instruction::FRem : Instruction::SRem;
1109  case bitc::BINOP_SHL:
1110  return IsFP ? -1 : Instruction::Shl;
1111  case bitc::BINOP_LSHR:
1112  return IsFP ? -1 : Instruction::LShr;
1113  case bitc::BINOP_ASHR:
1114  return IsFP ? -1 : Instruction::AShr;
1115  case bitc::BINOP_AND:
1116  return IsFP ? -1 : Instruction::And;
1117  case bitc::BINOP_OR:
1118  return IsFP ? -1 : Instruction::Or;
1119  case bitc::BINOP_XOR:
1120  return IsFP ? -1 : Instruction::Xor;
1121  }
1122 }
1123 
1125  switch (Val) {
1126  default: return AtomicRMWInst::BAD_BINOP;
1127  case bitc::RMW_XCHG: return AtomicRMWInst::Xchg;
1128  case bitc::RMW_ADD: return AtomicRMWInst::Add;
1129  case bitc::RMW_SUB: return AtomicRMWInst::Sub;
1130  case bitc::RMW_AND: return AtomicRMWInst::And;
1131  case bitc::RMW_NAND: return AtomicRMWInst::Nand;
1132  case bitc::RMW_OR: return AtomicRMWInst::Or;
1133  case bitc::RMW_XOR: return AtomicRMWInst::Xor;
1134  case bitc::RMW_MAX: return AtomicRMWInst::Max;
1135  case bitc::RMW_MIN: return AtomicRMWInst::Min;
1136  case bitc::RMW_UMAX: return AtomicRMWInst::UMax;
1137  case bitc::RMW_UMIN: return AtomicRMWInst::UMin;
1138  case bitc::RMW_FADD: return AtomicRMWInst::FAdd;
1139  case bitc::RMW_FSUB: return AtomicRMWInst::FSub;
1140  }
1141 }
1142 
1143 static AtomicOrdering getDecodedOrdering(unsigned Val) {
1144  switch (Val) {
1145  case bitc::ORDERING_NOTATOMIC: return AtomicOrdering::NotAtomic;
1147  case bitc::ORDERING_MONOTONIC: return AtomicOrdering::Monotonic;
1148  case bitc::ORDERING_ACQUIRE: return AtomicOrdering::Acquire;
1149  case bitc::ORDERING_RELEASE: return AtomicOrdering::Release;
1150  case bitc::ORDERING_ACQREL: return AtomicOrdering::AcquireRelease;
1151  default: // Map unknown orderings to sequentially-consistent.
1152  case bitc::ORDERING_SEQCST: return AtomicOrdering::SequentiallyConsistent;
1153  }
1154 }
1155 
1157  switch (Val) {
1158  default: // Map unknown selection kinds to any.
1160  return Comdat::Any;
1162  return Comdat::ExactMatch;
1164  return Comdat::Largest;
1166  return Comdat::NoDeduplicate;
1168  return Comdat::SameSize;
1169  }
1170 }
1171 
1173  FastMathFlags FMF;
1174  if (0 != (Val & bitc::UnsafeAlgebra))
1175  FMF.setFast();
1176  if (0 != (Val & bitc::AllowReassoc))
1177  FMF.setAllowReassoc();
1178  if (0 != (Val & bitc::NoNaNs))
1179  FMF.setNoNaNs();
1180  if (0 != (Val & bitc::NoInfs))
1181  FMF.setNoInfs();
1182  if (0 != (Val & bitc::NoSignedZeros))
1183  FMF.setNoSignedZeros();
1184  if (0 != (Val & bitc::AllowReciprocal))
1185  FMF.setAllowReciprocal();
1186  if (0 != (Val & bitc::AllowContract))
1187  FMF.setAllowContract(true);
1188  if (0 != (Val & bitc::ApproxFunc))
1189  FMF.setApproxFunc();
1190  return FMF;
1191 }
1192 
1193 static void upgradeDLLImportExportLinkage(GlobalValue *GV, unsigned Val) {
1194  switch (Val) {
1195  case 5: GV->setDLLStorageClass(GlobalValue::DLLImportStorageClass); break;
1196  case 6: GV->setDLLStorageClass(GlobalValue::DLLExportStorageClass); break;
1197  }
1198 }
1199 
1200 Type *BitcodeReader::getTypeByID(unsigned ID) {
1201  // The type table size is always specified correctly.
1202  if (ID >= TypeList.size())
1203  return nullptr;
1204 
1205  if (Type *Ty = TypeList[ID])
1206  return Ty;
1207 
1208  // If we have a forward reference, the only possible case is when it is to a
1209  // named struct. Just create a placeholder for now.
1210  return TypeList[ID] = createIdentifiedStructType(Context);
1211 }
1212 
1213 unsigned BitcodeReader::getContainedTypeID(unsigned ID, unsigned Idx) {
1214  auto It = ContainedTypeIDs.find(ID);
1215  if (It == ContainedTypeIDs.end())
1216  return InvalidTypeID;
1217 
1218  if (Idx >= It->second.size())
1219  return InvalidTypeID;
1220 
1221  return It->second[Idx];
1222 }
1223 
1224 Type *BitcodeReader::getPtrElementTypeByID(unsigned ID) {
1225  if (ID >= TypeList.size())
1226  return nullptr;
1227 
1228  Type *Ty = TypeList[ID];
1229  if (!Ty->isPointerTy())
1230  return nullptr;
1231 
1232  Type *ElemTy = getTypeByID(getContainedTypeID(ID, 0));
1233  if (!ElemTy)
1234  return nullptr;
1235 
1236  assert(cast<PointerType>(Ty)->isOpaqueOrPointeeTypeMatches(ElemTy) &&
1237  "Incorrect element type");
1238  return ElemTy;
1239 }
1240 
1241 unsigned BitcodeReader::getVirtualTypeID(Type *Ty,
1242  ArrayRef<unsigned> ChildTypeIDs) {
1243  unsigned ChildTypeID = ChildTypeIDs.empty() ? InvalidTypeID : ChildTypeIDs[0];
1244  auto CacheKey = std::make_pair(Ty, ChildTypeID);
1245  auto It = VirtualTypeIDs.find(CacheKey);
1246  if (It != VirtualTypeIDs.end()) {
1247  // The cmpxchg return value is the only place we need more than one
1248  // contained type ID, however the second one will always be the same (i1),
1249  // so we don't need to include it in the cache key. This asserts that the
1250  // contained types are indeed as expected and there are no collisions.
1251  assert((ChildTypeIDs.empty() ||
1252  ContainedTypeIDs[It->second] == ChildTypeIDs) &&
1253  "Incorrect cached contained type IDs");
1254  return It->second;
1255  }
1256 
1257 #ifndef NDEBUG
1258  if (!Ty->isOpaquePointerTy()) {
1259  assert(Ty->getNumContainedTypes() == ChildTypeIDs.size() &&
1260  "Wrong number of contained types");
1261  for (auto Pair : zip(Ty->subtypes(), ChildTypeIDs)) {
1262  assert(std::get<0>(Pair) == getTypeByID(std::get<1>(Pair)) &&
1263  "Incorrect contained type ID");
1264  }
1265  }
1266 #endif
1267 
1268  unsigned TypeID = TypeList.size();
1269  TypeList.push_back(Ty);
1270  if (!ChildTypeIDs.empty())
1271  append_range(ContainedTypeIDs[TypeID], ChildTypeIDs);
1272  VirtualTypeIDs.insert({CacheKey, TypeID});
1273  return TypeID;
1274 }
1275 
1276 StructType *BitcodeReader::createIdentifiedStructType(LLVMContext &Context,
1277  StringRef Name) {
1278  auto *Ret = StructType::create(Context, Name);
1279  IdentifiedStructTypes.push_back(Ret);
1280  return Ret;
1281 }
1282 
1283 StructType *BitcodeReader::createIdentifiedStructType(LLVMContext &Context) {
1284  auto *Ret = StructType::create(Context);
1285  IdentifiedStructTypes.push_back(Ret);
1286  return Ret;
1287 }
1288 
1289 //===----------------------------------------------------------------------===//
1290 // Functions for parsing blocks from the bitcode file
1291 //===----------------------------------------------------------------------===//
1292 
1294  switch (Val) {
1295  case Attribute::EndAttrKinds:
1296  case Attribute::EmptyKey:
1297  case Attribute::TombstoneKey:
1298  llvm_unreachable("Synthetic enumerators which should never get here");
1299 
1300  case Attribute::None: return 0;
1301  case Attribute::ZExt: return 1 << 0;
1302  case Attribute::SExt: return 1 << 1;
1303  case Attribute::NoReturn: return 1 << 2;
1304  case Attribute::InReg: return 1 << 3;
1305  case Attribute::StructRet: return 1 << 4;
1306  case Attribute::NoUnwind: return 1 << 5;
1307  case Attribute::NoAlias: return 1 << 6;
1308  case Attribute::ByVal: return 1 << 7;
1309  case Attribute::Nest: return 1 << 8;
1310  case Attribute::ReadNone: return 1 << 9;
1311  case Attribute::ReadOnly: return 1 << 10;
1312  case Attribute::NoInline: return 1 << 11;
1313  case Attribute::AlwaysInline: return 1 << 12;
1314  case Attribute::OptimizeForSize: return 1 << 13;
1315  case Attribute::StackProtect: return 1 << 14;
1316  case Attribute::StackProtectReq: return 1 << 15;
1317  case Attribute::Alignment: return 31 << 16;
1318  case Attribute::NoCapture: return 1 << 21;
1319  case Attribute::NoRedZone: return 1 << 22;
1320  case Attribute::NoImplicitFloat: return 1 << 23;
1321  case Attribute::Naked: return 1 << 24;
1322  case Attribute::InlineHint: return 1 << 25;
1323  case Attribute::StackAlignment: return 7 << 26;
1324  case Attribute::ReturnsTwice: return 1 << 29;
1325  case Attribute::UWTable: return 1 << 30;
1326  case Attribute::NonLazyBind: return 1U << 31;
1327  case Attribute::SanitizeAddress: return 1ULL << 32;
1328  case Attribute::MinSize: return 1ULL << 33;
1329  case Attribute::NoDuplicate: return 1ULL << 34;
1330  case Attribute::StackProtectStrong: return 1ULL << 35;
1331  case Attribute::SanitizeThread: return 1ULL << 36;
1332  case Attribute::SanitizeMemory: return 1ULL << 37;
1333  case Attribute::NoBuiltin: return 1ULL << 38;
1334  case Attribute::Returned: return 1ULL << 39;
1335  case Attribute::Cold: return 1ULL << 40;
1336  case Attribute::Builtin: return 1ULL << 41;
1337  case Attribute::OptimizeNone: return 1ULL << 42;
1338  case Attribute::InAlloca: return 1ULL << 43;
1339  case Attribute::NonNull: return 1ULL << 44;
1340  case Attribute::JumpTable: return 1ULL << 45;
1341  case Attribute::Convergent: return 1ULL << 46;
1342  case Attribute::SafeStack: return 1ULL << 47;
1343  case Attribute::NoRecurse: return 1ULL << 48;
1344  case Attribute::InaccessibleMemOnly: return 1ULL << 49;
1345  case Attribute::InaccessibleMemOrArgMemOnly: return 1ULL << 50;
1346  case Attribute::SwiftSelf: return 1ULL << 51;
1347  case Attribute::SwiftError: return 1ULL << 52;
1348  case Attribute::WriteOnly: return 1ULL << 53;
1349  case Attribute::Speculatable: return 1ULL << 54;
1350  case Attribute::StrictFP: return 1ULL << 55;
1351  case Attribute::SanitizeHWAddress: return 1ULL << 56;
1352  case Attribute::NoCfCheck: return 1ULL << 57;
1353  case Attribute::OptForFuzzing: return 1ULL << 58;
1354  case Attribute::ShadowCallStack: return 1ULL << 59;
1355  case Attribute::SpeculativeLoadHardening:
1356  return 1ULL << 60;
1357  case Attribute::ImmArg:
1358  return 1ULL << 61;
1359  case Attribute::WillReturn:
1360  return 1ULL << 62;
1361  case Attribute::NoFree:
1362  return 1ULL << 63;
1363  default:
1364  // Other attributes are not supported in the raw format,
1365  // as we ran out of space.
1366  return 0;
1367  }
1368  llvm_unreachable("Unsupported attribute type");
1369 }
1370 
1372  if (!Val) return;
1373 
1374  for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
1375  I = Attribute::AttrKind(I + 1)) {
1376  if (uint64_t A = (Val & getRawAttributeMask(I))) {
1377  if (I == Attribute::Alignment)
1378  B.addAlignmentAttr(1ULL << ((A >> 16) - 1));
1379  else if (I == Attribute::StackAlignment)
1380  B.addStackAlignmentAttr(1ULL << ((A >> 26)-1));
1381  else if (Attribute::isTypeAttrKind(I))
1382  B.addTypeAttr(I, nullptr); // Type will be auto-upgraded.
1383  else
1384  B.addAttribute(I);
1385  }
1386  }
1387 }
1388 
1389 /// This fills an AttrBuilder object with the LLVM attributes that have
1390 /// been decoded from the given integer. This function must stay in sync with
1391 /// 'encodeLLVMAttributesForBitcode'.
1393  uint64_t EncodedAttrs) {
1394  // The alignment is stored as a 16-bit raw value from bits 31--16. We shift
1395  // the bits above 31 down by 11 bits.
1396  unsigned Alignment = (EncodedAttrs & (0xffffULL << 16)) >> 16;
1397  assert((!Alignment || isPowerOf2_32(Alignment)) &&
1398  "Alignment must be a power of two.");
1399 
1400  if (Alignment)
1401  B.addAlignmentAttr(Alignment);
1402  addRawAttributeValue(B, ((EncodedAttrs & (0xfffffULL << 32)) >> 11) |
1403  (EncodedAttrs & 0xffff));
1404 }
1405 
1406 Error BitcodeReader::parseAttributeBlock() {
1407  if (Error Err = Stream.EnterSubBlock(bitc::PARAMATTR_BLOCK_ID))
1408  return Err;
1409 
1410  if (!MAttributes.empty())
1411  return error("Invalid multiple blocks");
1412 
1414 
1416 
1417  // Read all the records.
1418  while (true) {
1419  Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks();
1420  if (!MaybeEntry)
1421  return MaybeEntry.takeError();
1422  BitstreamEntry Entry = MaybeEntry.get();
1423 
1424  switch (Entry.Kind) {
1425  case BitstreamEntry::SubBlock: // Handled for us already.
1426  case BitstreamEntry::Error:
1427  return error("Malformed block");
1428  case BitstreamEntry::EndBlock:
1429  return Error::success();
1430  case BitstreamEntry::Record:
1431  // The interesting case.
1432  break;
1433  }
1434 
1435  // Read a record.
1436  Record.clear();
1437  Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record);
1438  if (!MaybeRecord)
1439  return MaybeRecord.takeError();
1440  switch (MaybeRecord.get()) {
1441  default: // Default behavior: ignore.
1442  break;
1443  case bitc::PARAMATTR_CODE_ENTRY_OLD: // ENTRY: [paramidx0, attr0, ...]
1444  // Deprecated, but still needed to read old bitcode files.
1445  if (Record.size() & 1)
1446  return error("Invalid parameter attribute record");
1447 
1448  for (unsigned i = 0, e = Record.size(); i != e; i += 2) {
1451  Attrs.push_back(AttributeList::get(Context, Record[i], B));
1452  }
1453 
1454  MAttributes.push_back(AttributeList::get(Context, Attrs));
1455  Attrs.clear();
1456  break;
1457  case bitc::PARAMATTR_CODE_ENTRY: // ENTRY: [attrgrp0, attrgrp1, ...]
1458  for (unsigned i = 0, e = Record.size(); i != e; ++i)
1459  Attrs.push_back(MAttributeGroups[Record[i]]);
1460 
1461  MAttributes.push_back(AttributeList::get(Context, Attrs));
1462  Attrs.clear();
1463  break;
1464  }
1465  }
1466 }
1467 
1468 // Returns Attribute::None on unrecognized codes.
1470  switch (Code) {
1471  default:
1472  return Attribute::None;
1474  return Attribute::Alignment;
1476  return Attribute::AlwaysInline;
1478  return Attribute::ArgMemOnly;
1480  return Attribute::Builtin;
1482  return Attribute::ByVal;
1484  return Attribute::InAlloca;
1485  case bitc::ATTR_KIND_COLD:
1486  return Attribute::Cold;
1488  return Attribute::Convergent;
1490  return Attribute::DisableSanitizerInstrumentation;
1492  return Attribute::ElementType;
1494  return Attribute::InaccessibleMemOnly;
1496  return Attribute::InaccessibleMemOrArgMemOnly;
1498  return Attribute::InlineHint;
1500  return Attribute::InReg;
1502  return Attribute::JumpTable;
1504  return Attribute::MinSize;
1505  case bitc::ATTR_KIND_NAKED:
1506  return Attribute::Naked;
1507  case bitc::ATTR_KIND_NEST:
1508  return Attribute::Nest;
1510  return Attribute::NoAlias;
1512  return Attribute::NoBuiltin;
1514  return Attribute::NoCallback;
1516  return Attribute::NoCapture;
1518  return Attribute::NoDuplicate;
1520  return Attribute::NoFree;
1522  return Attribute::NoImplicitFloat;
1524  return Attribute::NoInline;
1526  return Attribute::NoRecurse;
1528  return Attribute::NoMerge;
1530  return Attribute::NonLazyBind;
1532  return Attribute::NonNull;
1534  return Attribute::Dereferenceable;
1536  return Attribute::DereferenceableOrNull;
1538  return Attribute::AllocAlign;
1540  return Attribute::AllocSize;
1542  return Attribute::AllocatedPointer;
1544  return Attribute::NoRedZone;
1546  return Attribute::NoReturn;
1548  return Attribute::NoSync;
1550  return Attribute::NoCfCheck;
1552  return Attribute::NoProfile;
1554  return Attribute::NoUnwind;
1556  return Attribute::NoSanitizeBounds;
1558  return Attribute::NoSanitizeCoverage;
1560  return Attribute::NullPointerIsValid;
1562  return Attribute::OptForFuzzing;
1564  return Attribute::OptimizeForSize;
1566  return Attribute::OptimizeNone;
1568  return Attribute::ReadNone;
1570  return Attribute::ReadOnly;
1572  return Attribute::Returned;
1574  return Attribute::ReturnsTwice;
1575  case bitc::ATTR_KIND_S_EXT:
1576  return Attribute::SExt;
1578  return Attribute::Speculatable;
1580  return Attribute::StackAlignment;
1582  return Attribute::StackProtect;
1584  return Attribute::StackProtectReq;
1586  return Attribute::StackProtectStrong;
1588  return Attribute::SafeStack;
1590  return Attribute::ShadowCallStack;
1592  return Attribute::StrictFP;
1594  return Attribute::StructRet;
1596  return Attribute::SanitizeAddress;
1598  return Attribute::SanitizeHWAddress;
1600  return Attribute::SanitizeThread;
1602  return Attribute::SanitizeMemory;
1604  return Attribute::SpeculativeLoadHardening;
1606  return Attribute::SwiftError;
1608  return Attribute::SwiftSelf;
1610  return Attribute::SwiftAsync;
1612  return Attribute::UWTable;
1614  return Attribute::VScaleRange;
1616  return Attribute::WillReturn;
1618  return Attribute::WriteOnly;
1619  case bitc::ATTR_KIND_Z_EXT:
1620  return Attribute::ZExt;
1622  return Attribute::ImmArg;
1624  return Attribute::SanitizeMemTag;
1626  return Attribute::Preallocated;
1628  return Attribute::NoUndef;
1629  case bitc::ATTR_KIND_BYREF:
1630  return Attribute::ByRef;
1632  return Attribute::MustProgress;
1633  case bitc::ATTR_KIND_HOT:
1634  return Attribute::Hot;
1635  }
1636 }
1637 
1638 Error BitcodeReader::parseAlignmentValue(uint64_t Exponent,
1639  MaybeAlign &Alignment) {
1640  // Note: Alignment in bitcode files is incremented by 1, so that zero
1641  // can be used for default alignment.
1642  if (Exponent > Value::MaxAlignmentExponent + 1)
1643  return error("Invalid alignment value");
1644  Alignment = decodeMaybeAlign(Exponent);
1645  return Error::success();
1646 }
1647 
1648 Error BitcodeReader::parseAttrKind(uint64_t Code, Attribute::AttrKind *Kind) {
1650  if (*Kind == Attribute::None)
1651  return error("Unknown attribute kind (" + Twine(Code) + ")");
1652  return Error::success();
1653 }
1654 
1655 Error BitcodeReader::parseAttributeGroupBlock() {
1656  if (Error Err = Stream.EnterSubBlock(bitc::PARAMATTR_GROUP_BLOCK_ID))
1657  return Err;
1658 
1659  if (!MAttributeGroups.empty())
1660  return error("Invalid multiple blocks");
1661 
1663 
1664  // Read all the records.
1665  while (true) {
1666  Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks();
1667  if (!MaybeEntry)
1668  return MaybeEntry.takeError();
1669  BitstreamEntry Entry = MaybeEntry.get();
1670 
1671  switch (Entry.Kind) {
1672  case BitstreamEntry::SubBlock: // Handled for us already.
1673  case BitstreamEntry::Error:
1674  return error("Malformed block");
1675  case BitstreamEntry::EndBlock:
1676  return Error::success();
1677  case BitstreamEntry::Record:
1678  // The interesting case.
1679  break;
1680  }
1681 
1682  // Read a record.
1683  Record.clear();
1684  Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record);
1685  if (!MaybeRecord)
1686  return MaybeRecord.takeError();
1687  switch (MaybeRecord.get()) {
1688  default: // Default behavior: ignore.
1689  break;
1690  case bitc::PARAMATTR_GRP_CODE_ENTRY: { // ENTRY: [grpid, idx, a0, a1, ...]
1691  if (Record.size() < 3)
1692  return error("Invalid grp record");
1693 
1694  uint64_t GrpID = Record[0];
1695  uint64_t Idx = Record[1]; // Index of the object this attribute refers to.
1696 
1698  for (unsigned i = 2, e = Record.size(); i != e; ++i) {
1699  if (Record[i] == 0) { // Enum attribute
1701  if (Error Err = parseAttrKind(Record[++i], &Kind))
1702  return Err;
1703 
1704  // Upgrade old-style byval attribute to one with a type, even if it's
1705  // nullptr. We will have to insert the real type when we associate
1706  // this AttributeList with a function.
1707  if (Kind == Attribute::ByVal)
1708  B.addByValAttr(nullptr);
1709  else if (Kind == Attribute::StructRet)
1710  B.addStructRetAttr(nullptr);
1711  else if (Kind == Attribute::InAlloca)
1712  B.addInAllocaAttr(nullptr);
1713  else if (Kind == Attribute::UWTable)
1714  B.addUWTableAttr(UWTableKind::Default);
1715  else if (Attribute::isEnumAttrKind(Kind))
1716  B.addAttribute(Kind);
1717  else
1718  return error("Not an enum attribute");
1719  } else if (Record[i] == 1) { // Integer attribute
1721  if (Error Err = parseAttrKind(Record[++i], &Kind))
1722  return Err;
1723  if (!Attribute::isIntAttrKind(Kind))
1724  return error("Not an int attribute");
1725  if (Kind == Attribute::Alignment)
1726  B.addAlignmentAttr(Record[++i]);
1727  else if (Kind == Attribute::StackAlignment)
1728  B.addStackAlignmentAttr(Record[++i]);
1729  else if (Kind == Attribute::Dereferenceable)
1730  B.addDereferenceableAttr(Record[++i]);
1731  else if (Kind == Attribute::DereferenceableOrNull)
1732  B.addDereferenceableOrNullAttr(Record[++i]);
1733  else if (Kind == Attribute::AllocSize)
1734  B.addAllocSizeAttrFromRawRepr(Record[++i]);
1735  else if (Kind == Attribute::VScaleRange)
1736  B.addVScaleRangeAttrFromRawRepr(Record[++i]);
1737  else if (Kind == Attribute::UWTable)
1738  B.addUWTableAttr(UWTableKind(Record[++i]));
1739  } else if (Record[i] == 3 || Record[i] == 4) { // String attribute
1740  bool HasValue = (Record[i++] == 4);
1741  SmallString<64> KindStr;
1742  SmallString<64> ValStr;
1743 
1744  while (Record[i] != 0 && i != e)
1745  KindStr += Record[i++];
1746  assert(Record[i] == 0 && "Kind string not null terminated");
1747 
1748  if (HasValue) {
1749  // Has a value associated with it.
1750  ++i; // Skip the '0' that terminates the "kind" string.
1751  while (Record[i] != 0 && i != e)
1752  ValStr += Record[i++];
1753  assert(Record[i] == 0 && "Value string not null terminated");
1754  }
1755 
1756  B.addAttribute(KindStr.str(), ValStr.str());
1757  } else if (Record[i] == 5 || Record[i] == 6) {
1758  bool HasType = Record[i] == 6;
1760  if (Error Err = parseAttrKind(Record[++i], &Kind))
1761  return Err;
1762  if (!Attribute::isTypeAttrKind(Kind))
1763  return error("Not a type attribute");
1764 
1765  B.addTypeAttr(Kind, HasType ? getTypeByID(Record[++i]) : nullptr);
1766  } else {
1767  return error("Invalid attribute group entry");
1768  }
1769  }
1770 
1772  MAttributeGroups[GrpID] = AttributeList::get(Context, Idx, B);
1773  break;
1774  }
1775  }
1776  }
1777 }
1778 
1779 Error BitcodeReader::parseTypeTable() {
1780  if (Error Err = Stream.EnterSubBlock(bitc::TYPE_BLOCK_ID_NEW))
1781  return Err;
1782 
1783  return parseTypeTableBody();
1784 }
1785 
1786 Error BitcodeReader::parseTypeTableBody() {
1787  if (!TypeList.empty())
1788  return error("Invalid multiple blocks");
1789 
1791  unsigned NumRecords = 0;
1792 
1794 
1795  // Read all the records for this type table.
1796  while (true) {
1797  Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks();
1798  if (!MaybeEntry)
1799  return MaybeEntry.takeError();
1800  BitstreamEntry Entry = MaybeEntry.get();
1801 
1802  switch (Entry.Kind) {
1803  case BitstreamEntry::SubBlock: // Handled for us already.
1804  case BitstreamEntry::Error:
1805  return error("Malformed block");
1806  case BitstreamEntry::EndBlock:
1807  if (NumRecords != TypeList.size())
1808  return error("Malformed block");
1809  return Error::success();
1810  case BitstreamEntry::Record:
1811  // The interesting case.
1812  break;
1813  }
1814 
1815  // Read a record.
1816  Record.clear();
1817  Type *ResultTy = nullptr;
1818  SmallVector<unsigned> ContainedIDs;
1819  Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record);
1820  if (!MaybeRecord)
1821  return MaybeRecord.takeError();
1822  switch (MaybeRecord.get()) {
1823  default:
1824  return error("Invalid value");
1825  case bitc::TYPE_CODE_NUMENTRY: // TYPE_CODE_NUMENTRY: [numentries]
1826  // TYPE_CODE_NUMENTRY contains a count of the number of types in the
1827  // type list. This allows us to reserve space.
1828  if (Record.empty())
1829  return error("Invalid numentry record");
1830  TypeList.resize(Record[0]);
1831  continue;
1832  case bitc::TYPE_CODE_VOID: // VOID
1833  ResultTy = Type::getVoidTy(Context);
1834  break;
1835  case bitc::TYPE_CODE_HALF: // HALF
1836  ResultTy = Type::getHalfTy(Context);
1837  break;
1838  case bitc::TYPE_CODE_BFLOAT: // BFLOAT
1839  ResultTy = Type::getBFloatTy(Context);
1840  break;
1841  case bitc::TYPE_CODE_FLOAT: // FLOAT
1842  ResultTy = Type::getFloatTy(Context);
1843  break;
1844  case bitc::TYPE_CODE_DOUBLE: // DOUBLE
1845  ResultTy = Type::getDoubleTy(Context);
1846  break;
1847  case bitc::TYPE_CODE_X86_FP80: // X86_FP80
1848  ResultTy = Type::getX86_FP80Ty(Context);
1849  break;
1850  case bitc::TYPE_CODE_FP128: // FP128
1851  ResultTy = Type::getFP128Ty(Context);
1852  break;
1853  case bitc::TYPE_CODE_PPC_FP128: // PPC_FP128
1854  ResultTy = Type::getPPC_FP128Ty(Context);
1855  break;
1856  case bitc::TYPE_CODE_LABEL: // LABEL
1857  ResultTy = Type::getLabelTy(Context);
1858  break;
1859  case bitc::TYPE_CODE_METADATA: // METADATA
1860  ResultTy = Type::getMetadataTy(Context);
1861  break;
1862  case bitc::TYPE_CODE_X86_MMX: // X86_MMX
1863  ResultTy = Type::getX86_MMXTy(Context);
1864  break;
1865  case bitc::TYPE_CODE_X86_AMX: // X86_AMX
1866  ResultTy = Type::getX86_AMXTy(Context);
1867  break;
1868  case bitc::TYPE_CODE_TOKEN: // TOKEN
1869  ResultTy = Type::getTokenTy(Context);
1870  break;
1871  case bitc::TYPE_CODE_INTEGER: { // INTEGER: [width]
1872  if (Record.empty())
1873  return error("Invalid integer record");
1874 
1875  uint64_t NumBits = Record[0];
1876  if (NumBits < IntegerType::MIN_INT_BITS ||
1877  NumBits > IntegerType::MAX_INT_BITS)
1878  return error("Bitwidth for integer type out of range");
1879  ResultTy = IntegerType::get(Context, NumBits);
1880  break;
1881  }
1882  case bitc::TYPE_CODE_POINTER: { // POINTER: [pointee type] or
1883  // [pointee type, address space]
1884  if (Record.empty())
1885  return error("Invalid pointer record");
1886  unsigned AddressSpace = 0;
1887  if (Record.size() == 2)
1888  AddressSpace = Record[1];
1889  ResultTy = getTypeByID(Record[0]);
1890  if (!ResultTy ||
1892  return error("Invalid type");
1894  Context.setOpaquePointers(false);
1895  ContainedIDs.push_back(Record[0]);
1896  ResultTy = PointerType::get(ResultTy, AddressSpace);
1897  break;
1898  }
1899  case bitc::TYPE_CODE_OPAQUE_POINTER: { // OPAQUE_POINTER: [addrspace]
1900  if (Record.size() != 1)
1901  return error("Invalid opaque pointer record");
1903  Context.setOpaquePointers(true);
1904  } else if (Context.supportsTypedPointers())
1905  return error(
1906  "Opaque pointers are only supported in -opaque-pointers mode");
1907  unsigned AddressSpace = Record[0];
1908  ResultTy = PointerType::get(Context, AddressSpace);
1909  break;
1910  }
1912  // Deprecated, but still needed to read old bitcode files.
1913  // FUNCTION: [vararg, attrid, retty, paramty x N]
1914  if (Record.size() < 3)
1915  return error("Invalid function record");
1916  SmallVector<Type*, 8> ArgTys;
1917  for (unsigned i = 3, e = Record.size(); i != e; ++i) {
1918  if (Type *T = getTypeByID(Record[i]))
1919  ArgTys.push_back(T);
1920  else
1921  break;
1922  }
1923 
1924  ResultTy = getTypeByID(Record[2]);
1925  if (!ResultTy || ArgTys.size() < Record.size()-3)
1926  return error("Invalid type");
1927 
1928  ContainedIDs.append(Record.begin() + 2, Record.end());
1929  ResultTy = FunctionType::get(ResultTy, ArgTys, Record[0]);
1930  break;
1931  }
1932  case bitc::TYPE_CODE_FUNCTION: {
1933  // FUNCTION: [vararg, retty, paramty x N]
1934  if (Record.size() < 2)
1935  return error("Invalid function record");
1936  SmallVector<Type*, 8> ArgTys;
1937  for (unsigned i = 2, e = Record.size(); i != e; ++i) {
1938  if (Type *T = getTypeByID(Record[i])) {
1939  if (!FunctionType::isValidArgumentType(T))
1940  return error("Invalid function argument type");
1941  ArgTys.push_back(T);
1942  }
1943  else
1944  break;
1945  }
1946 
1947  ResultTy = getTypeByID(Record[1]);
1948  if (!ResultTy || ArgTys.size() < Record.size()-2)
1949  return error("Invalid type");
1950 
1951  ContainedIDs.append(Record.begin() + 1, Record.end());
1952  ResultTy = FunctionType::get(ResultTy, ArgTys, Record[0]);
1953  break;
1954  }
1955  case bitc::TYPE_CODE_STRUCT_ANON: { // STRUCT: [ispacked, eltty x N]
1956  if (Record.empty())
1957  return error("Invalid anon struct record");
1958  SmallVector<Type*, 8> EltTys;
1959  for (unsigned i = 1, e = Record.size(); i != e; ++i) {
1960  if (Type *T = getTypeByID(Record[i]))
1961  EltTys.push_back(T);
1962  else
1963  break;
1964  }
1965  if (EltTys.size() != Record.size()-1)
1966  return error("Invalid type");
1967  ContainedIDs.append(Record.begin() + 1, Record.end());
1968  ResultTy = StructType::get(Context, EltTys, Record[0]);
1969  break;
1970  }
1971  case bitc::TYPE_CODE_STRUCT_NAME: // STRUCT_NAME: [strchr x N]
1972  if (convertToString(Record, 0, TypeName))
1973  return error("Invalid struct name record");
1974  continue;
1975 
1976  case bitc::TYPE_CODE_STRUCT_NAMED: { // STRUCT: [ispacked, eltty x N]
1977  if (Record.empty())
1978  return error("Invalid named struct record");
1979 
1980  if (NumRecords >= TypeList.size())
1981  return error("Invalid TYPE table");
1982 
1983  // Check to see if this was forward referenced, if so fill in the temp.
1984  StructType *Res = cast_or_null<StructType>(TypeList[NumRecords]);
1985  if (Res) {
1986  Res->setName(TypeName);
1987  TypeList[NumRecords] = nullptr;
1988  } else // Otherwise, create a new struct.
1989  Res = createIdentifiedStructType(Context, TypeName);
1990  TypeName.clear();
1991 
1992  SmallVector<Type*, 8> EltTys;
1993  for (unsigned i = 1, e = Record.size(); i != e; ++i) {
1994  if (Type *T = getTypeByID(Record[i]))
1995  EltTys.push_back(T);
1996  else
1997  break;
1998  }
1999  if (EltTys.size() != Record.size()-1)
2000  return error("Invalid named struct record");
2001  Res->setBody(EltTys, Record[0]);
2002  ContainedIDs.append(Record.begin() + 1, Record.end());
2003  ResultTy = Res;
2004  break;
2005  }
2006  case bitc::TYPE_CODE_OPAQUE: { // OPAQUE: []
2007  if (Record.size() != 1)
2008  return error("Invalid opaque type record");
2009 
2010  if (NumRecords >= TypeList.size())
2011  return error("Invalid TYPE table");
2012 
2013  // Check to see if this was forward referenced, if so fill in the temp.
2014  StructType *Res = cast_or_null<StructType>(TypeList[NumRecords]);
2015  if (Res) {
2016  Res->setName(TypeName);
2017  TypeList[NumRecords] = nullptr;
2018  } else // Otherwise, create a new struct with no body.
2019  Res = createIdentifiedStructType(Context, TypeName);
2020  TypeName.clear();
2021  ResultTy = Res;
2022  break;
2023  }
2024  case bitc::TYPE_CODE_ARRAY: // ARRAY: [numelts, eltty]
2025  if (Record.size() < 2)
2026  return error("Invalid array type record");
2027  ResultTy = getTypeByID(Record[1]);
2028  if (!ResultTy || !ArrayType::isValidElementType(ResultTy))
2029  return error("Invalid type");
2030  ContainedIDs.push_back(Record[1]);
2031  ResultTy = ArrayType::get(ResultTy, Record[0]);
2032  break;
2033  case bitc::TYPE_CODE_VECTOR: // VECTOR: [numelts, eltty] or
2034  // [numelts, eltty, scalable]
2035  if (Record.size() < 2)
2036  return error("Invalid vector type record");
2037  if (Record[0] == 0)
2038  return error("Invalid vector length");
2039  ResultTy = getTypeByID(Record[1]);
2040  if (!ResultTy || !VectorType::isValidElementType(ResultTy))
2041  return error("Invalid type");
2042  bool Scalable = Record.size() > 2 ? Record[2] : false;
2043  ContainedIDs.push_back(Record[1]);
2044  ResultTy = VectorType::get(ResultTy, Record[0], Scalable);
2045  break;
2046  }
2047 
2048  if (NumRecords >= TypeList.size())
2049  return error("Invalid TYPE table");
2050  if (TypeList[NumRecords])
2051  return error(
2052  "Invalid TYPE table: Only named structs can be forward referenced");
2053  assert(ResultTy && "Didn't read a type?");
2054  TypeList[NumRecords] = ResultTy;
2055  if (!ContainedIDs.empty())
2056  ContainedTypeIDs[NumRecords] = std::move(ContainedIDs);
2057  ++NumRecords;
2058  }
2059 }
2060 
2061 Error BitcodeReader::parseOperandBundleTags() {
2062  if (Error Err = Stream.EnterSubBlock(bitc::OPERAND_BUNDLE_TAGS_BLOCK_ID))
2063  return Err;
2064 
2065  if (!BundleTags.empty())
2066  return error("Invalid multiple blocks");
2067 
2069 
2070  while (true) {
2071  Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks();
2072  if (!MaybeEntry)
2073  return MaybeEntry.takeError();
2074  BitstreamEntry Entry = MaybeEntry.get();
2075 
2076  switch (Entry.Kind) {
2077  case BitstreamEntry::SubBlock: // Handled for us already.
2078  case BitstreamEntry::Error:
2079  return error("Malformed block");
2080  case BitstreamEntry::EndBlock:
2081  return Error::success();
2082  case BitstreamEntry::Record:
2083  // The interesting case.
2084  break;
2085  }
2086 
2087  // Tags are implicitly mapped to integers by their order.
2088 
2089  Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record);
2090  if (!MaybeRecord)
2091  return MaybeRecord.takeError();
2092  if (MaybeRecord.get() != bitc::OPERAND_BUNDLE_TAG)
2093  return error("Invalid operand bundle record");
2094 
2095  // OPERAND_BUNDLE_TAG: [strchr x N]
2096  BundleTags.emplace_back();
2097  if (convertToString(Record, 0, BundleTags.back()))
2098  return error("Invalid operand bundle record");
2099  Record.clear();
2100  }
2101 }
2102 
2103 Error BitcodeReader::parseSyncScopeNames() {
2104  if (Error Err = Stream.EnterSubBlock(bitc::SYNC_SCOPE_NAMES_BLOCK_ID))
2105  return Err;
2106 
2107  if (!SSIDs.empty())
2108  return error("Invalid multiple synchronization scope names blocks");
2109 
2111  while (true) {
2112  Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks();
2113  if (!MaybeEntry)
2114  return MaybeEntry.takeError();
2115  BitstreamEntry Entry = MaybeEntry.get();
2116 
2117  switch (Entry.Kind) {
2118  case BitstreamEntry::SubBlock: // Handled for us already.
2119  case BitstreamEntry::Error:
2120  return error("Malformed block");
2121  case BitstreamEntry::EndBlock:
2122  if (SSIDs.empty())
2123  return error("Invalid empty synchronization scope names block");
2124  return Error::success();
2125  case BitstreamEntry::Record:
2126  // The interesting case.
2127  break;
2128  }
2129 
2130  // Synchronization scope names are implicitly mapped to synchronization
2131  // scope IDs by their order.
2132 
2133  Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record);
2134  if (!MaybeRecord)
2135  return MaybeRecord.takeError();
2136  if (MaybeRecord.get() != bitc::SYNC_SCOPE_NAME)
2137  return error("Invalid sync scope record");
2138 
2139  SmallString<16> SSN;
2140  if (convertToString(Record, 0, SSN))
2141  return error("Invalid sync scope record");
2142 
2143  SSIDs.push_back(Context.getOrInsertSyncScopeID(SSN));
2144  Record.clear();
2145  }
2146 }
2147 
2148 /// Associate a value with its name from the given index in the provided record.
2149 Expected<Value *> BitcodeReader::recordValue(SmallVectorImpl<uint64_t> &Record,
2150  unsigned NameIndex, Triple &TT) {
2152  if (convertToString(Record, NameIndex, ValueName))
2153  return error("Invalid record");
2154  unsigned ValueID = Record[0];
2155  if (ValueID >= ValueList.size() || !ValueList[ValueID])
2156  return error("Invalid record");
2157  Value *V = ValueList[ValueID];
2158 
2159  StringRef NameStr(ValueName.data(), ValueName.size());
2160  if (NameStr.find_first_of(0) != StringRef::npos)
2161  return error("Invalid value name");
2162  V->setName(NameStr);
2163  auto *GO = dyn_cast<GlobalObject>(V);
2164  if (GO && ImplicitComdatObjects.contains(GO) && TT.supportsCOMDAT())
2165  GO->setComdat(TheModule->getOrInsertComdat(V->getName()));
2166  return V;
2167 }
2168 
2169 /// Helper to note and return the current location, and jump to the given
2170 /// offset.
2172  BitstreamCursor &Stream) {
2173  // Save the current parsing location so we can jump back at the end
2174  // of the VST read.
2175  uint64_t CurrentBit = Stream.GetCurrentBitNo();
2176  if (Error JumpFailed = Stream.JumpToBit(Offset * 32))
2177  return std::move(JumpFailed);
2178  Expected<BitstreamEntry> MaybeEntry = Stream.advance();
2179  if (!MaybeEntry)
2180  return MaybeEntry.takeError();
2181  if (MaybeEntry.get().Kind != BitstreamEntry::SubBlock ||
2182  MaybeEntry.get().ID != bitc::VALUE_SYMTAB_BLOCK_ID)
2183  return error("Expected value symbol table subblock");
2184  return CurrentBit;
2185 }
2186 
2187 void BitcodeReader::setDeferredFunctionInfo(unsigned FuncBitcodeOffsetDelta,
2188  Function *F,
2190  // Note that we subtract 1 here because the offset is relative to one word
2191  // before the start of the identification or module block, which was
2192  // historically always the start of the regular bitcode header.
2193  uint64_t FuncWordOffset = Record[1] - 1;
2194  uint64_t FuncBitOffset = FuncWordOffset * 32;
2195  DeferredFunctionInfo[F] = FuncBitOffset + FuncBitcodeOffsetDelta;
2196  // Set the LastFunctionBlockBit to point to the last function block.
2197  // Later when parsing is resumed after function materialization,
2198  // we can simply skip that last function block.
2199  if (FuncBitOffset > LastFunctionBlockBit)
2200  LastFunctionBlockBit = FuncBitOffset;
2201 }
2202 
2203 /// Read a new-style GlobalValue symbol table.
2204 Error BitcodeReader::parseGlobalValueSymbolTable() {
2205  unsigned FuncBitcodeOffsetDelta =
2206  Stream.getAbbrevIDWidth() + bitc::BlockIDWidth;
2207 
2208  if (Error Err = Stream.EnterSubBlock(bitc::VALUE_SYMTAB_BLOCK_ID))
2209  return Err;
2210 
2212  while (true) {
2213  Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks();
2214  if (!MaybeEntry)
2215  return MaybeEntry.takeError();
2216  BitstreamEntry Entry = MaybeEntry.get();
2217 
2218  switch (Entry.Kind) {
2219  case BitstreamEntry::SubBlock:
2220  case BitstreamEntry::Error:
2221  return error("Malformed block");
2222  case BitstreamEntry::EndBlock:
2223  return Error::success();
2224  case BitstreamEntry::Record:
2225  break;
2226  }
2227 
2228  Record.clear();
2229  Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record);
2230  if (!MaybeRecord)
2231  return MaybeRecord.takeError();
2232  switch (MaybeRecord.get()) {
2233  case bitc::VST_CODE_FNENTRY: { // [valueid, offset]
2234  unsigned ValueID = Record[0];
2235  if (ValueID >= ValueList.size() || !ValueList[ValueID])
2236  return error("Invalid value reference in symbol table");
2237  setDeferredFunctionInfo(FuncBitcodeOffsetDelta,
2238  cast<Function>(ValueList[ValueID]), Record);
2239  break;
2240  }
2241  }
2242  }
2243 }
2244 
2245 /// Parse the value symbol table at either the current parsing location or
2246 /// at the given bit offset if provided.
2247 Error BitcodeReader::parseValueSymbolTable(uint64_t Offset) {
2248  uint64_t CurrentBit;
2249  // Pass in the Offset to distinguish between calling for the module-level
2250  // VST (where we want to jump to the VST offset) and the function-level
2251  // VST (where we don't).
2252  if (Offset > 0) {
2253  Expected<uint64_t> MaybeCurrentBit = jumpToValueSymbolTable(Offset, Stream);
2254  if (!MaybeCurrentBit)
2255  return MaybeCurrentBit.takeError();
2256  CurrentBit = MaybeCurrentBit.get();
2257  // If this module uses a string table, read this as a module-level VST.
2258  if (UseStrtab) {
2259  if (Error Err = parseGlobalValueSymbolTable())
2260  return Err;
2261  if (Error JumpFailed = Stream.JumpToBit(CurrentBit))
2262  return JumpFailed;
2263  return Error::success();
2264  }
2265  // Otherwise, the VST will be in a similar format to a function-level VST,
2266  // and will contain symbol names.
2267  }
2268 
2269  // Compute the delta between the bitcode indices in the VST (the word offset
2270  // to the word-aligned ENTER_SUBBLOCK for the function block, and that
2271  // expected by the lazy reader. The reader's EnterSubBlock expects to have
2272  // already read the ENTER_SUBBLOCK code (size getAbbrevIDWidth) and BlockID
2273  // (size BlockIDWidth). Note that we access the stream's AbbrevID width here
2274  // just before entering the VST subblock because: 1) the EnterSubBlock
2275  // changes the AbbrevID width; 2) the VST block is nested within the same
2276  // outer MODULE_BLOCK as the FUNCTION_BLOCKs and therefore have the same
2277  // AbbrevID width before calling EnterSubBlock; and 3) when we want to
2278  // jump to the FUNCTION_BLOCK using this offset later, we don't want
2279  // to rely on the stream's AbbrevID width being that of the MODULE_BLOCK.
2280  unsigned FuncBitcodeOffsetDelta =
2281  Stream.getAbbrevIDWidth() + bitc::BlockIDWidth;
2282 
2283  if (Error Err = Stream.EnterSubBlock(bitc::VALUE_SYMTAB_BLOCK_ID))
2284  return Err;
2285 
2287 
2288  Triple TT(TheModule->getTargetTriple());
2289 
2290  // Read all the records for this value table.
2292 
2293  while (true) {
2294  Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks();
2295  if (!MaybeEntry)
2296  return MaybeEntry.takeError();
2297  BitstreamEntry Entry = MaybeEntry.get();
2298 
2299  switch (Entry.Kind) {
2300  case BitstreamEntry::SubBlock: // Handled for us already.
2301  case BitstreamEntry::Error:
2302  return error("Malformed block");
2303  case BitstreamEntry::EndBlock:
2304  if (Offset > 0)
2305  if (Error JumpFailed = Stream.JumpToBit(CurrentBit))
2306  return JumpFailed;
2307  return Error::success();
2308  case BitstreamEntry::Record:
2309  // The interesting case.
2310  break;
2311  }
2312 
2313  // Read a record.
2314  Record.clear();
2315  Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record);
2316  if (!MaybeRecord)
2317  return MaybeRecord.takeError();
2318  switch (MaybeRecord.get()) {
2319  default: // Default behavior: unknown type.
2320  break;
2321  case bitc::VST_CODE_ENTRY: { // VST_CODE_ENTRY: [valueid, namechar x N]
2322  Expected<Value *> ValOrErr = recordValue(Record, 1, TT);
2323  if (Error Err = ValOrErr.takeError())
2324  return Err;
2325  ValOrErr.get();
2326  break;
2327  }
2328  case bitc::VST_CODE_FNENTRY: {
2329  // VST_CODE_FNENTRY: [valueid, offset, namechar x N]
2330  Expected<Value *> ValOrErr = recordValue(Record, 2, TT);
2331  if (Error Err = ValOrErr.takeError())
2332  return Err;
2333  Value *V = ValOrErr.get();
2334 
2335  // Ignore function offsets emitted for aliases of functions in older
2336  // versions of LLVM.
2337  if (auto *F = dyn_cast<Function>(V))
2338  setDeferredFunctionInfo(FuncBitcodeOffsetDelta, F, Record);
2339  break;
2340  }
2341  case bitc::VST_CODE_BBENTRY: {
2342  if (convertToString(Record, 1, ValueName))
2343  return error("Invalid bbentry record");
2344  BasicBlock *BB = getBasicBlock(Record[0]);
2345  if (!BB)
2346  return error("Invalid bbentry record");
2347 
2348  BB->setName(StringRef(ValueName.data(), ValueName.size()));
2349  ValueName.clear();
2350  break;
2351  }
2352  }
2353  }
2354 }
2355 
2356 /// Decode a signed value stored with the sign bit in the LSB for dense VBR
2357 /// encoding.
2359  if ((V & 1) == 0)
2360  return V >> 1;
2361  if (V != 1)
2362  return -(V >> 1);
2363  // There is no such thing as -0 with integers. "-0" really means MININT.
2364  return 1ULL << 63;
2365 }
2366 
2367 /// Resolve all of the initializers for global values and aliases that we can.
2368 Error BitcodeReader::resolveGlobalAndIndirectSymbolInits() {
2369  std::vector<std::pair<GlobalVariable *, unsigned>> GlobalInitWorklist;
2370  std::vector<std::pair<GlobalValue *, unsigned>> IndirectSymbolInitWorklist;
2371  std::vector<FunctionOperandInfo> FunctionOperandWorklist;
2372 
2373  GlobalInitWorklist.swap(GlobalInits);
2374  IndirectSymbolInitWorklist.swap(IndirectSymbolInits);
2375  FunctionOperandWorklist.swap(FunctionOperands);
2376 
2377  while (!GlobalInitWorklist.empty()) {
2378  unsigned ValID = GlobalInitWorklist.back().second;
2379  if (ValID >= ValueList.size()) {
2380  // Not ready to resolve this yet, it requires something later in the file.
2381  GlobalInits.push_back(GlobalInitWorklist.back());
2382  } else {
2383  if (Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID]))
2384  GlobalInitWorklist.back().first->setInitializer(C);
2385  else
2386  return error("Expected a constant");
2387  }
2388  GlobalInitWorklist.pop_back();
2389  }
2390 
2391  while (!IndirectSymbolInitWorklist.empty()) {
2392  unsigned ValID = IndirectSymbolInitWorklist.back().second;
2393  if (ValID >= ValueList.size()) {
2394  IndirectSymbolInits.push_back(IndirectSymbolInitWorklist.back());
2395  } else {
2396  Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID]);
2397  if (!C)
2398  return error("Expected a constant");
2399  GlobalValue *GV = IndirectSymbolInitWorklist.back().first;
2400  if (auto *GA = dyn_cast<GlobalAlias>(GV)) {
2401  if (C->getType() != GV->getType())
2402  return error("Alias and aliasee types don't match");
2403  GA->setAliasee(C);
2404  } else if (auto *GI = dyn_cast<GlobalIFunc>(GV)) {
2405  Type *ResolverFTy =
2406  GlobalIFunc::getResolverFunctionType(GI->getValueType());
2407  // Transparently fix up the type for compatiblity with older bitcode
2408  GI->setResolver(
2409  ConstantExpr::getBitCast(C, ResolverFTy->getPointerTo()));
2410  } else {
2411  return error("Expected an alias or an ifunc");
2412  }
2413  }
2414  IndirectSymbolInitWorklist.pop_back();
2415  }
2416 
2417  while (!FunctionOperandWorklist.empty()) {
2418  FunctionOperandInfo &Info = FunctionOperandWorklist.back();
2419  if (Info.PersonalityFn) {
2420  unsigned ValID = Info.PersonalityFn - 1;
2421  if (ValID < ValueList.size()) {
2422  if (Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID]))
2423  Info.F->setPersonalityFn(C);
2424  else
2425  return error("Expected a constant");
2426  Info.PersonalityFn = 0;
2427  }
2428  }
2429  if (Info.Prefix) {
2430  unsigned ValID = Info.Prefix - 1;
2431  if (ValID < ValueList.size()) {
2432  if (Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID]))
2433  Info.F->setPrefixData(C);
2434  else
2435  return error("Expected a constant");
2436  Info.Prefix = 0;
2437  }
2438  }
2439  if (Info.Prologue) {
2440  unsigned ValID = Info.Prologue - 1;
2441  if (ValID < ValueList.size()) {
2442  if (Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID]))
2443  Info.F->setPrologueData(C);
2444  else
2445  return error("Expected a constant");
2446  Info.Prologue = 0;
2447  }
2448  }
2449  if (Info.PersonalityFn || Info.Prefix || Info.Prologue)
2450  FunctionOperands.push_back(Info);
2451  FunctionOperandWorklist.pop_back();
2452  }
2453 
2454  return Error::success();
2455 }
2456 
2458  SmallVector<uint64_t, 8> Words(Vals.size());
2459  transform(Vals, Words.begin(),
2461 
2462  return APInt(TypeBits, Words);
2463 }
2464 
2465 Error BitcodeReader::parseConstants() {
2466  if (Error Err = Stream.EnterSubBlock(bitc::CONSTANTS_BLOCK_ID))
2467  return Err;
2468 
2470 
2471  // Read all the records for this value table.
2472  Type *CurTy = Type::getInt32Ty(Context);
2473  unsigned Int32TyID = getVirtualTypeID(CurTy);
2474  unsigned CurTyID = Int32TyID;
2475  Type *CurElemTy = nullptr;
2476  unsigned NextCstNo = ValueList.size();
2477 
2478  struct DelayedShufTy {
2479  VectorType *OpTy;
2480  unsigned OpTyID;
2481  VectorType *RTy;
2482  uint64_t Op0Idx;
2483  uint64_t Op1Idx;
2484  uint64_t Op2Idx;
2485  unsigned CstNo;
2486  };
2487  std::vector<DelayedShufTy> DelayedShuffles;
2488  struct DelayedSelTy {
2489  Type *OpTy;
2490  unsigned OpTyID;
2491  uint64_t Op0Idx;
2492  uint64_t Op1Idx;
2493  uint64_t Op2Idx;
2494  unsigned CstNo;
2495  };
2496  std::vector<DelayedSelTy> DelayedSelectors;
2497 
2498  while (true) {
2499  Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks();
2500  if (!MaybeEntry)
2501  return MaybeEntry.takeError();
2502  BitstreamEntry Entry = MaybeEntry.get();
2503 
2504  switch (Entry.Kind) {
2505  case BitstreamEntry::SubBlock: // Handled for us already.
2506  case BitstreamEntry::Error:
2507  return error("Malformed block");
2508  case BitstreamEntry::EndBlock:
2509  // Once all the constants have been read, go through and resolve forward
2510  // references.
2511  //
2512  // We have to treat shuffles specially because they don't have three
2513  // operands anymore. We need to convert the shuffle mask into an array,
2514  // and we can't convert a forward reference.
2515  for (auto &DelayedShuffle : DelayedShuffles) {
2516  VectorType *OpTy = DelayedShuffle.OpTy;
2517  unsigned OpTyID = DelayedShuffle.OpTyID;
2518  VectorType *RTy = DelayedShuffle.RTy;
2519  uint64_t Op0Idx = DelayedShuffle.Op0Idx;
2520  uint64_t Op1Idx = DelayedShuffle.Op1Idx;
2521  uint64_t Op2Idx = DelayedShuffle.Op2Idx;
2522  uint64_t CstNo = DelayedShuffle.CstNo;
2523  Constant *Op0 = ValueList.getConstantFwdRef(Op0Idx, OpTy, OpTyID);
2524  Constant *Op1 = ValueList.getConstantFwdRef(Op1Idx, OpTy, OpTyID);
2525  Type *ShufTy =
2526  VectorType::get(Type::getInt32Ty(Context), RTy->getElementCount());
2527  Constant *Op2 = ValueList.getConstantFwdRef(
2528  Op2Idx, ShufTy, getVirtualTypeID(ShufTy, Int32TyID));
2529  if (!ShuffleVectorInst::isValidOperands(Op0, Op1, Op2))
2530  return error("Invalid shufflevector operands");
2532  ShuffleVectorInst::getShuffleMask(Op2, Mask);
2533  Value *V = ConstantExpr::getShuffleVector(Op0, Op1, Mask);
2534  if (Error Err = ValueList.assignValue(
2535  CstNo, V,
2536  getVirtualTypeID(V->getType(), getContainedTypeID(OpTyID))))
2537  return Err;
2538  }
2539  for (auto &DelayedSelector : DelayedSelectors) {
2540  Type *OpTy = DelayedSelector.OpTy;
2541  unsigned OpTyID = DelayedSelector.OpTyID;
2542  Type *SelectorTy = Type::getInt1Ty(Context);
2543  unsigned SelectorTyID = getVirtualTypeID(SelectorTy);
2544  uint64_t Op0Idx = DelayedSelector.Op0Idx;
2545  uint64_t Op1Idx = DelayedSelector.Op1Idx;
2546  uint64_t Op2Idx = DelayedSelector.Op2Idx;
2547  uint64_t CstNo = DelayedSelector.CstNo;
2548  Constant *Op1 = ValueList.getConstantFwdRef(Op1Idx, OpTy, OpTyID);
2549  Constant *Op2 = ValueList.getConstantFwdRef(Op2Idx, OpTy, OpTyID);
2550  // The selector might be an i1 or an <n x i1>
2551  // Get the type from the ValueList before getting a forward ref.
2552  if (VectorType *VTy = dyn_cast<VectorType>(OpTy)) {
2553  Value *V = ValueList[Op0Idx];
2554  assert(V);
2555  if (SelectorTy != V->getType()) {
2556  SelectorTy = VectorType::get(SelectorTy, VTy->getElementCount());
2557  SelectorTyID = getVirtualTypeID(SelectorTy, SelectorTyID);
2558  }
2559  }
2560  Constant *Op0 =
2561  ValueList.getConstantFwdRef(Op0Idx, SelectorTy, SelectorTyID);
2562  Value *V = ConstantExpr::getSelect(Op0, Op1, Op2);
2563  if (Error Err = ValueList.assignValue(CstNo, V, OpTyID))
2564  return Err;
2565  }
2566 
2567  if (NextCstNo != ValueList.size())
2568  return error("Invalid constant reference");
2569 
2570  ValueList.resolveConstantForwardRefs();
2571  return Error::success();
2572  case BitstreamEntry::Record:
2573  // The interesting case.
2574  break;
2575  }
2576 
2577  // Read a record.
2578  Record.clear();
2579  Type *VoidType = Type::getVoidTy(Context);
2580  Value *V = nullptr;
2581  Expected<unsigned> MaybeBitCode = Stream.readRecord(Entry.ID, Record);
2582  if (!MaybeBitCode)
2583  return MaybeBitCode.takeError();
2584  switch (unsigned BitCode = MaybeBitCode.get()) {
2585  default: // Default behavior: unknown constant
2586  case bitc::CST_CODE_UNDEF: // UNDEF
2587  V = UndefValue::get(CurTy);
2588  break;
2589  case bitc::CST_CODE_POISON: // POISON
2590  V = PoisonValue::get(CurTy);
2591  break;
2592  case bitc::CST_CODE_SETTYPE: // SETTYPE: [typeid]
2593  if (Record.empty())
2594  return error("Invalid settype record");
2595  if (Record[0] >= TypeList.size() || !TypeList[Record[0]])
2596  return error("Invalid settype record");
2597  if (TypeList[Record[0]] == VoidType)
2598  return error("Invalid constant type");
2599  CurTyID = Record[0];
2600  CurTy = TypeList[CurTyID];
2601  CurElemTy = getPtrElementTypeByID(CurTyID);
2602  continue; // Skip the ValueList manipulation.
2603  case bitc::CST_CODE_NULL: // NULL
2604  if (CurTy->isVoidTy() || CurTy->isFunctionTy() || CurTy->isLabelTy())
2605  return error("Invalid type for a constant null value");
2606  V = Constant::getNullValue(CurTy);
2607  break;
2608  case bitc::CST_CODE_INTEGER: // INTEGER: [intval]
2609  if (!CurTy->isIntegerTy() || Record.empty())
2610  return error("Invalid integer const record");
2612  break;
2613  case bitc::CST_CODE_WIDE_INTEGER: {// WIDE_INTEGER: [n x intval]
2614  if (!CurTy->isIntegerTy() || Record.empty())
2615  return error("Invalid wide integer const record");
2616 
2617  APInt VInt =
2618  readWideAPInt(Record, cast<IntegerType>(CurTy)->getBitWidth());
2619  V = ConstantInt::get(Context, VInt);
2620 
2621  break;
2622  }
2623  case bitc::CST_CODE_FLOAT: { // FLOAT: [fpval]
2624  if (Record.empty())
2625  return error("Invalid float const record");
2626  if (CurTy->isHalfTy())
2627  V = ConstantFP::get(Context, APFloat(APFloat::IEEEhalf(),
2628  APInt(16, (uint16_t)Record[0])));
2629  else if (CurTy->isBFloatTy())
2630  V = ConstantFP::get(Context, APFloat(APFloat::BFloat(),
2631  APInt(16, (uint32_t)Record[0])));
2632  else if (CurTy->isFloatTy())
2633  V = ConstantFP::get(Context, APFloat(APFloat::IEEEsingle(),
2634  APInt(32, (uint32_t)Record[0])));
2635  else if (CurTy->isDoubleTy())
2636  V = ConstantFP::get(Context, APFloat(APFloat::IEEEdouble(),
2637  APInt(64, Record[0])));
2638  else if (CurTy->isX86_FP80Ty()) {
2639  // Bits are not stored the same way as a normal i80 APInt, compensate.
2640  uint64_t Rearrange[2];
2641  Rearrange[0] = (Record[1] & 0xffffLL) | (Record[0] << 16);
2642  Rearrange[1] = Record[0] >> 48;
2643  V = ConstantFP::get(Context, APFloat(APFloat::x87DoubleExtended(),
2644  APInt(80, Rearrange)));
2645  } else if (CurTy->isFP128Ty())
2646  V = ConstantFP::get(Context, APFloat(APFloat::IEEEquad(),
2647  APInt(128, Record)));
2648  else if (CurTy->isPPC_FP128Ty())
2649  V = ConstantFP::get(Context, APFloat(APFloat::PPCDoubleDouble(),
2650  APInt(128, Record)));
2651  else
2652  V = UndefValue::get(CurTy);
2653  break;
2654  }
2655 
2656  case bitc::CST_CODE_AGGREGATE: {// AGGREGATE: [n x value number]
2657  if (Record.empty())
2658  return error("Invalid aggregate record");
2659 
2660  unsigned Size = Record.size();
2662 
2663  if (StructType *STy = dyn_cast<StructType>(CurTy)) {
2664  for (unsigned i = 0; i != Size; ++i)
2665  Elts.push_back(ValueList.getConstantFwdRef(
2666  Record[i], STy->getElementType(i),
2667  getContainedTypeID(CurTyID, i)));
2668  V = ConstantStruct::get(STy, Elts);
2669  } else if (ArrayType *ATy = dyn_cast<ArrayType>(CurTy)) {
2670  Type *EltTy = ATy->getElementType();
2671  unsigned EltTyID = getContainedTypeID(CurTyID);
2672  for (unsigned i = 0; i != Size; ++i)
2673  Elts.push_back(ValueList.getConstantFwdRef(Record[i], EltTy,
2674  EltTyID));
2675  V = ConstantArray::get(ATy, Elts);
2676  } else if (VectorType *VTy = dyn_cast<VectorType>(CurTy)) {
2677  Type *EltTy = VTy->getElementType();
2678  unsigned EltTyID = getContainedTypeID(CurTyID);
2679  for (unsigned i = 0; i != Size; ++i)
2680  Elts.push_back(ValueList.getConstantFwdRef(Record[i], EltTy,
2681  EltTyID));
2682  V = ConstantVector::get(Elts);
2683  } else {
2684  V = UndefValue::get(CurTy);
2685  }
2686  break;
2687  }
2688  case bitc::CST_CODE_STRING: // STRING: [values]
2689  case bitc::CST_CODE_CSTRING: { // CSTRING: [values]
2690  if (Record.empty())
2691  return error("Invalid string record");
2692 
2693  SmallString<16> Elts(Record.begin(), Record.end());
2694  V = ConstantDataArray::getString(Context, Elts,
2695  BitCode == bitc::CST_CODE_CSTRING);
2696  break;
2697  }
2698  case bitc::CST_CODE_DATA: {// DATA: [n x value]
2699  if (Record.empty())
2700  return error("Invalid data record");
2701 
2702  Type *EltTy;
2703  if (auto *Array = dyn_cast<ArrayType>(CurTy))
2704  EltTy = Array->getElementType();
2705  else
2706  EltTy = cast<VectorType>(CurTy)->getElementType();
2707  if (EltTy->isIntegerTy(8)) {
2708  SmallVector<uint8_t, 16> Elts(Record.begin(), Record.end());
2709  if (isa<VectorType>(CurTy))
2710  V = ConstantDataVector::get(Context, Elts);
2711  else
2712  V = ConstantDataArray::get(Context, Elts);
2713  } else if (EltTy->isIntegerTy(16)) {
2714  SmallVector<uint16_t, 16> Elts(Record.begin(), Record.end());
2715  if (isa<VectorType>(CurTy))
2716  V = ConstantDataVector::get(Context, Elts);
2717  else
2718  V = ConstantDataArray::get(Context, Elts);
2719  } else if (EltTy->isIntegerTy(32)) {
2720  SmallVector<uint32_t, 16> Elts(Record.begin(), Record.end());
2721  if (isa<VectorType>(CurTy))
2722  V = ConstantDataVector::get(Context, Elts);
2723  else
2724  V = ConstantDataArray::get(Context, Elts);
2725  } else if (EltTy->isIntegerTy(64)) {
2726  SmallVector<uint64_t, 16> Elts(Record.begin(), Record.end());
2727  if (isa<VectorType>(CurTy))
2728  V = ConstantDataVector::get(Context, Elts);
2729  else
2730  V = ConstantDataArray::get(Context, Elts);
2731  } else if (EltTy->isHalfTy()) {
2732  SmallVector<uint16_t, 16> Elts(Record.begin(), Record.end());
2733  if (isa<VectorType>(CurTy))
2734  V = ConstantDataVector::getFP(EltTy, Elts);
2735  else
2736  V = ConstantDataArray::getFP(EltTy, Elts);
2737  } else if (EltTy->isBFloatTy()) {
2738  SmallVector<uint16_t, 16> Elts(Record.begin(), Record.end());
2739  if (isa<VectorType>(CurTy))
2740  V = ConstantDataVector::getFP(EltTy, Elts);
2741  else
2742  V = ConstantDataArray::getFP(EltTy, Elts);
2743  } else if (EltTy->isFloatTy()) {
2744  SmallVector<uint32_t, 16> Elts(Record.begin(), Record.end());
2745  if (isa<VectorType>(CurTy))
2746  V = ConstantDataVector::getFP(EltTy, Elts);
2747  else
2748  V = ConstantDataArray::getFP(EltTy, Elts);
2749  } else if (EltTy->isDoubleTy()) {
2750  SmallVector<uint64_t, 16> Elts(Record.begin(), Record.end());
2751  if (isa<VectorType>(CurTy))
2752  V = ConstantDataVector::getFP(EltTy, Elts);
2753  else
2754  V = ConstantDataArray::getFP(EltTy, Elts);
2755  } else {
2756  return error("Invalid type for value");
2757  }
2758  break;
2759  }
2760  case bitc::CST_CODE_CE_UNOP: { // CE_UNOP: [opcode, opval]
2761  if (Record.size() < 2)
2762  return error("Invalid unary op constexpr record");
2763  int Opc = getDecodedUnaryOpcode(Record[0], CurTy);
2764  if (Opc < 0) {
2765  V = UndefValue::get(CurTy); // Unknown unop.
2766  } else {
2767  Constant *LHS = ValueList.getConstantFwdRef(Record[1], CurTy, CurTyID);
2768  unsigned Flags = 0;
2769  V = ConstantExpr::get(Opc, LHS, Flags);
2770  }
2771  break;
2772  }
2773  case bitc::CST_CODE_CE_BINOP: { // CE_BINOP: [opcode, opval, opval]
2774  if (Record.size() < 3)
2775  return error("Invalid binary op constexpr record");
2776  int Opc = getDecodedBinaryOpcode(Record[0], CurTy);
2777  if (Opc < 0) {
2778  V = UndefValue::get(CurTy); // Unknown binop.
2779  } else {
2780  Constant *LHS = ValueList.getConstantFwdRef(Record[1], CurTy, CurTyID);
2781  Constant *RHS = ValueList.getConstantFwdRef(Record[2], CurTy, CurTyID);
2782  unsigned Flags = 0;
2783  if (Record.size() >= 4) {
2784  if (Opc == Instruction::Add ||
2785  Opc == Instruction::Sub ||
2786  Opc == Instruction::Mul ||
2787  Opc == Instruction::Shl) {
2788  if (Record[3] & (1 << bitc::OBO_NO_SIGNED_WRAP))
2789  Flags |= OverflowingBinaryOperator::NoSignedWrap;
2790  if (Record[3] & (1 << bitc::OBO_NO_UNSIGNED_WRAP))
2791  Flags |= OverflowingBinaryOperator::NoUnsignedWrap;
2792  } else if (Opc == Instruction::SDiv ||
2793  Opc == Instruction::UDiv ||
2794  Opc == Instruction::LShr ||
2795  Opc == Instruction::AShr) {
2796  if (Record[3] & (1 << bitc::PEO_EXACT))
2797  Flags |= SDivOperator::IsExact;
2798  }
2799  }
2800  V = ConstantExpr::get(Opc, LHS, RHS, Flags);
2801  }
2802  break;
2803  }
2804  case bitc::CST_CODE_CE_CAST: { // CE_CAST: [opcode, opty, opval]
2805  if (Record.size() < 3)
2806  return error("Invalid cast constexpr record");
2807  int Opc = getDecodedCastOpcode(Record[0]);
2808  if (Opc < 0) {
2809  V = UndefValue::get(CurTy); // Unknown cast.
2810  } else {
2811  unsigned OpTyID = Record[1];
2812  Type *OpTy = getTypeByID(OpTyID);
2813  if (!OpTy)
2814  return error("Invalid cast constexpr record");
2815  Constant *Op = ValueList.getConstantFwdRef(Record[2], OpTy, OpTyID);
2816  V = UpgradeBitCastExpr(Opc, Op, CurTy);
2817  if (!V) V = ConstantExpr::getCast(Opc, Op, CurTy);
2818  }
2819  break;
2820  }
2821  case bitc::CST_CODE_CE_INBOUNDS_GEP: // [ty, n x operands]
2822  case bitc::CST_CODE_CE_GEP: // [ty, n x operands]
2823  case bitc::CST_CODE_CE_GEP_WITH_INRANGE_INDEX: { // [ty, flags, n x
2824  // operands]
2825  if (Record.size() < 2)
2826  return error("Constant GEP record must have at least two elements");
2827  unsigned OpNum = 0;
2828  Type *PointeeType = nullptr;
2830  Record.size() % 2)
2831  PointeeType = getTypeByID(Record[OpNum++]);
2832 
2833  bool InBounds = false;
2834  Optional<unsigned> InRangeIndex;
2836  uint64_t Op = Record[OpNum++];
2837  InBounds = Op & 1;
2838  InRangeIndex = Op >> 1;
2839  } else if (BitCode == bitc::CST_CODE_CE_INBOUNDS_GEP)
2840  InBounds = true;
2841 
2843  unsigned BaseTypeID = Record[OpNum];
2844  while (OpNum != Record.size()) {
2845  unsigned ElTyID = Record[OpNum++];
2846  Type *ElTy = getTypeByID(ElTyID);
2847  if (!ElTy)
2848  return error("Invalid getelementptr constexpr record");
2849  Elts.push_back(ValueList.getConstantFwdRef(Record[OpNum++], ElTy,
2850  ElTyID));
2851  }
2852 
2853  if (Elts.size() < 1)
2854  return error("Invalid gep with no operands");
2855 
2856  Type *BaseType = getTypeByID(BaseTypeID);
2857  if (isa<VectorType>(BaseType)) {
2858  BaseTypeID = getContainedTypeID(BaseTypeID, 0);
2859  BaseType = getTypeByID(BaseTypeID);
2860  }
2861 
2862  PointerType *OrigPtrTy = dyn_cast_or_null<PointerType>(BaseType);
2863  if (!OrigPtrTy)
2864  return error("GEP base operand must be pointer or vector of pointer");
2865 
2866  if (!PointeeType) {
2867  PointeeType = getPtrElementTypeByID(BaseTypeID);
2868  if (!PointeeType)
2869  return error("Missing element type for old-style constant GEP");
2870  } else if (!OrigPtrTy->isOpaqueOrPointeeTypeMatches(PointeeType))
2871  return error("Explicit gep operator type does not match pointee type "
2872  "of pointer operand");
2873 
2874  ArrayRef<Constant *> Indices(Elts.begin() + 1, Elts.end());
2875  V = ConstantExpr::getGetElementPtr(PointeeType, Elts[0], Indices,
2876  InBounds, InRangeIndex);
2877  break;
2878  }
2879  case bitc::CST_CODE_CE_SELECT: { // CE_SELECT: [opval#, opval#, opval#]
2880  if (Record.size() < 3)
2881  return error("Invalid select constexpr record");
2882 
2883  DelayedSelectors.push_back(
2884  {CurTy, CurTyID, Record[0], Record[1], Record[2], NextCstNo});
2885  (void)ValueList.getConstantFwdRef(NextCstNo, CurTy, CurTyID);
2886  ++NextCstNo;
2887  continue;
2888  }
2890  : { // CE_EXTRACTELT: [opty, opval, opty, opval]
2891  if (Record.size() < 3)
2892  return error("Invalid extractelement constexpr record");
2893  unsigned OpTyID = Record[0];
2894  VectorType *OpTy =
2895  dyn_cast_or_null<VectorType>(getTypeByID(OpTyID));
2896  if (!OpTy)
2897  return error("Invalid extractelement constexpr record");
2898  Constant *Op0 = ValueList.getConstantFwdRef(Record[1], OpTy, OpTyID);
2899  Constant *Op1 = nullptr;
2900  if (Record.size() == 4) {
2901  unsigned IdxTyID = Record[2];
2902  Type *IdxTy = getTypeByID(IdxTyID);
2903  if (!IdxTy)
2904  return error("Invalid extractelement constexpr record");
2905  Op1 = ValueList.getConstantFwdRef(Record[3], IdxTy, IdxTyID);
2906  } else {
2907  // Deprecated, but still needed to read old bitcode files.
2908  Op1 = ValueList.getConstantFwdRef(Record[2], Type::getInt32Ty(Context),
2909  Int32TyID);
2910  }
2911  if (!Op1)
2912  return error("Invalid extractelement constexpr record");
2913  V = ConstantExpr::getExtractElement(Op0, Op1);
2914  break;
2915  }
2917  : { // CE_INSERTELT: [opval, opval, opty, opval]
2918  VectorType *OpTy = dyn_cast<VectorType>(CurTy);
2919  if (Record.size() < 3 || !OpTy)
2920  return error("Invalid insertelement constexpr record");
2921  Constant *Op0 = ValueList.getConstantFwdRef(Record[0], OpTy, CurTyID);
2922  Constant *Op1 = ValueList.getConstantFwdRef(Record[1],
2923  OpTy->getElementType(),
2924  getContainedTypeID(CurTyID));
2925  Constant *Op2 = nullptr;
2926  if (Record.size() == 4) {
2927  unsigned IdxTyID = Record[2];
2928  Type *IdxTy = getTypeByID(IdxTyID);
2929  if (!IdxTy)
2930  return error("Invalid insertelement constexpr record");
2931  Op2 = ValueList.getConstantFwdRef(Record[3], IdxTy, IdxTyID);
2932  } else {
2933  // Deprecated, but still needed to read old bitcode files.
2934  Op2 = ValueList.getConstantFwdRef(Record[2], Type::getInt32Ty(Context),
2935  Int32TyID);
2936  }
2937  if (!Op2)
2938  return error("Invalid insertelement constexpr record");
2939  V = ConstantExpr::getInsertElement(Op0, Op1, Op2);
2940  break;
2941  }
2942  case bitc::CST_CODE_CE_SHUFFLEVEC: { // CE_SHUFFLEVEC: [opval, opval, opval]
2943  VectorType *OpTy = dyn_cast<VectorType>(CurTy);
2944  if (Record.size() < 3 || !OpTy)
2945  return error("Invalid shufflevector constexpr record");
2946  DelayedShuffles.push_back(
2947  {OpTy, CurTyID, OpTy, Record[0], Record[1], Record[2], NextCstNo});
2948  ++NextCstNo;
2949  continue;
2950  }
2951  case bitc::CST_CODE_CE_SHUFVEC_EX: { // [opty, opval, opval, opval]
2952  VectorType *RTy = dyn_cast<VectorType>(CurTy);
2953  VectorType *OpTy =
2954  dyn_cast_or_null<VectorType>(getTypeByID(Record[0]));
2955  if (Record.size() < 4 || !RTy || !OpTy)
2956  return error("Invalid shufflevector constexpr record");
2957  DelayedShuffles.push_back(
2958  {OpTy, CurTyID, RTy, Record[1], Record[2], Record[3], NextCstNo});
2959  ++NextCstNo;
2960  continue;
2961  }
2962  case bitc::CST_CODE_CE_CMP: { // CE_CMP: [opty, opval, opval, pred]
2963  if (Record.size() < 4)
2964  return error("Invalid cmp constexpt record");
2965  unsigned OpTyID = Record[0];
2966  Type *OpTy = getTypeByID(OpTyID);
2967  if (!OpTy)
2968  return error("Invalid cmp constexpr record");
2969  Constant *Op0 = ValueList.getConstantFwdRef(Record[1], OpTy, OpTyID);
2970  Constant *Op1 = ValueList.getConstantFwdRef(Record[2], OpTy, OpTyID);
2971 
2972  if (OpTy->isFPOrFPVectorTy())
2973  V = ConstantExpr::getFCmp(Record[3], Op0, Op1);
2974  else
2975  V = ConstantExpr::getICmp(Record[3], Op0, Op1);
2976  break;
2977  }
2978  // This maintains backward compatibility, pre-asm dialect keywords.
2979  // Deprecated, but still needed to read old bitcode files.
2981  if (Record.size() < 2)
2982  return error("Invalid inlineasm record");
2983  std::string AsmStr, ConstrStr;
2984  bool HasSideEffects = Record[0] & 1;
2985  bool IsAlignStack = Record[0] >> 1;
2986  unsigned AsmStrSize = Record[1];
2987  if (2+AsmStrSize >= Record.size())
2988  return error("Invalid inlineasm record");
2989  unsigned ConstStrSize = Record[2+AsmStrSize];
2990  if (3+AsmStrSize+ConstStrSize > Record.size())
2991  return error("Invalid inlineasm record");
2992 
2993  for (unsigned i = 0; i != AsmStrSize; ++i)
2994  AsmStr += (char)Record[2+i];
2995  for (unsigned i = 0; i != ConstStrSize; ++i)
2996  ConstrStr += (char)Record[3+AsmStrSize+i];
2997  UpgradeInlineAsmString(&AsmStr);
2998  if (!CurElemTy)
2999  return error("Missing element type for old-style inlineasm");
3000  V = InlineAsm::get(cast<FunctionType>(CurElemTy), AsmStr, ConstrStr,
3001  HasSideEffects, IsAlignStack);
3002  break;
3003  }
3004  // This version adds support for the asm dialect keywords (e.g.,
3005  // inteldialect).
3007  if (Record.size() < 2)
3008  return error("Invalid inlineasm record");
3009  std::string AsmStr, ConstrStr;
3010  bool HasSideEffects = Record[0] & 1;
3011  bool IsAlignStack = (Record[0] >> 1) & 1;
3012  unsigned AsmDialect = Record[0] >> 2;
3013  unsigned AsmStrSize = Record[1];
3014  if (2+AsmStrSize >= Record.size())
3015  return error("Invalid inlineasm record");
3016  unsigned ConstStrSize = Record[2+AsmStrSize];
3017  if (3+AsmStrSize+ConstStrSize > Record.size())
3018  return error("Invalid inlineasm record");
3019 
3020  for (unsigned i = 0; i != AsmStrSize; ++i)
3021  AsmStr += (char)Record[2+i];
3022  for (unsigned i = 0; i != ConstStrSize; ++i)
3023  ConstrStr += (char)Record[3+AsmStrSize+i];
3024  UpgradeInlineAsmString(&AsmStr);
3025  if (!CurElemTy)
3026  return error("Missing element type for old-style inlineasm");
3027  V = InlineAsm::get(cast<FunctionType>(CurElemTy), AsmStr, ConstrStr,
3028  HasSideEffects, IsAlignStack,
3029  InlineAsm::AsmDialect(AsmDialect));
3030  break;
3031  }
3032  // This version adds support for the unwind keyword.
3034  if (Record.size() < 2)
3035  return error("Invalid inlineasm record");
3036  unsigned OpNum = 0;
3037  std::string AsmStr, ConstrStr;
3038  bool HasSideEffects = Record[OpNum] & 1;
3039  bool IsAlignStack = (Record[OpNum] >> 1) & 1;
3040  unsigned AsmDialect = (Record[OpNum] >> 2) & 1;
3041  bool CanThrow = (Record[OpNum] >> 3) & 1;
3042  ++OpNum;
3043  unsigned AsmStrSize = Record[OpNum];
3044  ++OpNum;
3045  if (OpNum + AsmStrSize >= Record.size())
3046  return error("Invalid inlineasm record");
3047  unsigned ConstStrSize = Record[OpNum + AsmStrSize];
3048  if (OpNum + 1 + AsmStrSize + ConstStrSize > Record.size())
3049  return error("Invalid inlineasm record");
3050 
3051  for (unsigned i = 0; i != AsmStrSize; ++i)
3052  AsmStr += (char)Record[OpNum + i];
3053  ++OpNum;
3054  for (unsigned i = 0; i != ConstStrSize; ++i)
3055  ConstrStr += (char)Record[OpNum + AsmStrSize + i];
3056  UpgradeInlineAsmString(&AsmStr);
3057  if (!CurElemTy)
3058  return error("Missing element type for old-style inlineasm");
3059  V = InlineAsm::get(cast<FunctionType>(CurElemTy), AsmStr, ConstrStr,
3060  HasSideEffects, IsAlignStack,
3061  InlineAsm::AsmDialect(AsmDialect), CanThrow);
3062  break;
3063  }
3064  // This version adds explicit function type.
3065  case bitc::CST_CODE_INLINEASM: {
3066  if (Record.size() < 3)
3067  return error("Invalid inlineasm record");
3068  unsigned OpNum = 0;
3069  auto *FnTy = dyn_cast_or_null<FunctionType>(getTypeByID(Record[OpNum]));
3070  ++OpNum;
3071  if (!FnTy)
3072  return error("Invalid inlineasm record");
3073  std::string AsmStr, ConstrStr;
3074  bool HasSideEffects = Record[OpNum] & 1;
3075  bool IsAlignStack = (Record[OpNum] >> 1) & 1;
3076  unsigned AsmDialect = (Record[OpNum] >> 2) & 1;
3077  bool CanThrow = (Record[OpNum] >> 3) & 1;
3078  ++OpNum;
3079  unsigned AsmStrSize = Record[OpNum];
3080  ++OpNum;
3081  if (OpNum + AsmStrSize >= Record.size())
3082  return error("Invalid inlineasm record");
3083  unsigned ConstStrSize = Record[OpNum + AsmStrSize];
3084  if (OpNum + 1 + AsmStrSize + ConstStrSize > Record.size())
3085  return error("Invalid inlineasm record");
3086 
3087  for (unsigned i = 0; i != AsmStrSize; ++i)
3088  AsmStr += (char)Record[OpNum + i];
3089  ++OpNum;
3090  for (unsigned i = 0; i != ConstStrSize; ++i)
3091  ConstrStr += (char)Record[OpNum + AsmStrSize + i];
3092  UpgradeInlineAsmString(&AsmStr);
3093  V = InlineAsm::get(FnTy, AsmStr, ConstrStr, HasSideEffects, IsAlignStack,
3094  InlineAsm::AsmDialect(AsmDialect), CanThrow);
3095  break;
3096  }
3098  if (Record.size() < 3)
3099  return error("Invalid blockaddress record");
3100  unsigned FnTyID = Record[0];
3101  Type *FnTy = getTypeByID(FnTyID);
3102  if (!FnTy)
3103  return error("Invalid blockaddress record");
3104  Function *Fn = dyn_cast_or_null<Function>(
3105  ValueList.getConstantFwdRef(Record[1], FnTy, FnTyID));
3106  if (!Fn)
3107  return error("Invalid blockaddress record");
3108 
3109  // If the function is already parsed we can insert the block address right
3110  // away.
3111  BasicBlock *BB;
3112  unsigned BBID = Record[2];
3113  if (!BBID)
3114  // Invalid reference to entry block.
3115  return error("Invalid ID");
3116  if (!Fn->empty()) {
3117  Function::iterator BBI = Fn->begin(), BBE = Fn->end();
3118  for (size_t I = 0, E = BBID; I != E; ++I) {
3119  if (BBI == BBE)
3120  return error("Invalid ID");
3121  ++BBI;
3122  }
3123  BB = &*BBI;
3124  } else {
3125  // Otherwise insert a placeholder and remember it so it can be inserted
3126  // when the function is parsed.
3127  auto &FwdBBs = BasicBlockFwdRefs[Fn];
3128  if (FwdBBs.empty())
3129  BasicBlockFwdRefQueue.push_back(Fn);
3130  if (FwdBBs.size() < BBID + 1)
3131  FwdBBs.resize(BBID + 1);
3132  if (!FwdBBs[BBID])
3133  FwdBBs[BBID] = BasicBlock::Create(Context);
3134  BB = FwdBBs[BBID];
3135  }
3136  V = BlockAddress::get(Fn, BB);
3137  break;
3138  }
3140  if (Record.size() < 2)
3141  return error("Invalid dso_local record");
3142  unsigned GVTyID = Record[0];
3143  Type *GVTy = getTypeByID(GVTyID);
3144  if (!GVTy)
3145  return error("Invalid dso_local record");
3146  GlobalValue *GV = dyn_cast_or_null<GlobalValue>(
3147  ValueList.getConstantFwdRef(Record[1], GVTy, GVTyID));
3148  if (!GV)
3149  return error("Invalid dso_local record");
3150 
3151  V = DSOLocalEquivalent::get(GV);
3152  break;
3153  }
3155  if (Record.size() < 2)
3156  return error("Invalid no_cfi record");
3157  unsigned GVTyID = Record[0];
3158  Type *GVTy = getTypeByID(GVTyID);
3159  if (!GVTy)
3160  return error("Invalid no_cfi record");
3161  GlobalValue *GV = dyn_cast_or_null<GlobalValue>(
3162  ValueList.getConstantFwdRef(Record[1], GVTy, GVTyID));
3163  if (!GV)
3164  return error("Invalid no_cfi record");
3165  V = NoCFIValue::get(GV);
3166  break;
3167  }
3168  }
3169 
3170  assert(V->getType() == getTypeByID(CurTyID) && "Incorrect result type ID");
3171  if (Error Err = ValueList.assignValue(NextCstNo, V, CurTyID))
3172  return Err;
3173  ++NextCstNo;
3174  }
3175 }
3176 
3177 Error BitcodeReader::parseUseLists() {
3178  if (Error Err = Stream.EnterSubBlock(bitc::USELIST_BLOCK_ID))
3179  return Err;
3180 
3181  // Read all the records.
3183 
3184  while (true) {
3185  Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks();
3186  if (!MaybeEntry)
3187  return MaybeEntry.takeError();
3188  BitstreamEntry Entry = MaybeEntry.get();
3189 
3190  switch (Entry.Kind) {
3191  case BitstreamEntry::SubBlock: // Handled for us already.
3192  case BitstreamEntry::Error:
3193  return error("Malformed block");
3194  case BitstreamEntry::EndBlock:
3195  return Error::success();
3196  case BitstreamEntry::Record:
3197  // The interesting case.
3198  break;
3199  }
3200 
3201  // Read a use list record.
3202  Record.clear();
3203  bool IsBB = false;
3204  Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record);
3205  if (!MaybeRecord)
3206  return MaybeRecord.takeError();
3207  switch (MaybeRecord.get()) {
3208  default: // Default behavior: unknown type.
3209  break;
3210  case bitc::USELIST_CODE_BB:
3211  IsBB = true;
3214  unsigned RecordLength = Record.size();
3215  if (RecordLength < 3)
3216  // Records should have at least an ID and two indexes.
3217  return error("Invalid record");
3218  unsigned ID = Record.pop_back_val();
3219 
3220  Value *V;
3221  if (IsBB) {
3222  assert(ID < FunctionBBs.size() && "Basic block not found");
3223  V = FunctionBBs[ID];
3224  } else
3225  V = ValueList[ID];
3226  unsigned NumUses = 0;
3228  for (const Use &U : V->materialized_uses()) {
3229  if (++NumUses > Record.size())
3230  break;
3231  Order[&U] = Record[NumUses - 1];
3232  }
3233  if (Order.size() != Record.size() || NumUses > Record.size())
3234  // Mismatches can happen if the functions are being materialized lazily
3235  // (out-of-order), or a value has been upgraded.
3236  break;
3237 
3238  V->sortUseList([&](const Use &L, const Use &R) {
3239  return Order.lookup(&L) < Order.lookup(&R);
3240  });
3241  break;
3242  }
3243  }
3244  }
3245 }
3246 
3247 /// When we see the block for metadata, remember where it is and then skip it.
3248 /// This lets us lazily deserialize the metadata.
3249 Error BitcodeReader::rememberAndSkipMetadata() {
3250  // Save the current stream state.
3251  uint64_t CurBit = Stream.GetCurrentBitNo();
3252  DeferredMetadataInfo.push_back(CurBit);
3253 
3254  // Skip over the block for now.
3255  if (Error Err = Stream.SkipBlock())
3256  return Err;
3257  return Error::success();
3258 }
3259 
3260 Error BitcodeReader::materializeMetadata() {
3261  for (uint64_t BitPos : DeferredMetadataInfo) {
3262  // Move the bit stream to the saved position.
3263  if (Error JumpFailed = Stream.JumpToBit(BitPos))
3264  return JumpFailed;
3265  if (Error Err = MDLoader->parseModuleMetadata())
3266  return Err;
3267  }
3268 
3269  // Upgrade "Linker Options" module flag to "llvm.linker.options" module-level
3270  // metadata. Only upgrade if the new option doesn't exist to avoid upgrade
3271  // multiple times.
3272  if (!TheModule->getNamedMetadata("llvm.linker.options")) {
3273  if (Metadata *Val = TheModule->getModuleFlag("Linker Options")) {
3274  NamedMDNode *LinkerOpts =
3275  TheModule->getOrInsertNamedMetadata("llvm.linker.options");
3276  for (const MDOperand &MDOptions : cast<MDNode>(Val)->operands())
3277  LinkerOpts->addOperand(cast<MDNode>(MDOptions));
3278  }
3279  }
3280 
3281  DeferredMetadataInfo.clear();
3282  return Error::success();
3283 }
3284 
3285 void BitcodeReader::setStripDebugInfo() { StripDebugInfo = true; }
3286 
3287 /// When we see the block for a function body, remember where it is and then
3288 /// skip it. This lets us lazily deserialize the functions.
3289 Error BitcodeReader::rememberAndSkipFunctionBody() {
3290  // Get the function we are talking about.
3291  if (FunctionsWithBodies.empty())
3292  return error("Insufficient function protos");
3293 
3294  Function *Fn = FunctionsWithBodies.back();
3295  FunctionsWithBodies.pop_back();
3296 
3297  // Save the current stream state.
3298  uint64_t CurBit = Stream.GetCurrentBitNo();
3299  assert(
3300  (DeferredFunctionInfo[Fn] == 0 || DeferredFunctionInfo[Fn] == CurBit) &&
3301  "Mismatch between VST and scanned function offsets");
3302  DeferredFunctionInfo[Fn] = CurBit;
3303 
3304  // Skip over the function block for now.
3305  if (Error Err = Stream.SkipBlock())
3306  return Err;
3307  return Error::success();
3308 }
3309 
3310 Error BitcodeReader::globalCleanup() {
3311  // Patch the initializers for globals and aliases up.
3312  if (Error Err = resolveGlobalAndIndirectSymbolInits())
3313  return Err;
3314  if (!GlobalInits.empty() || !IndirectSymbolInits.empty())
3315  return error("Malformed global initializer set");
3316 
3317  // Look for intrinsic functions which need to be upgraded at some point
3318  // and functions that need to have their function attributes upgraded.
3319  for (Function &F : *TheModule) {
3320  MDLoader->upgradeDebugIntrinsics(F);
3321  Function *NewFn;
3322  if (UpgradeIntrinsicFunction(&F, NewFn))
3323  UpgradedIntrinsics[&F] = NewFn;
3324  else if (auto Remangled = Intrinsic::remangleIntrinsicFunction(&F))
3325  // Some types could be renamed during loading if several modules are
3326  // loaded in the same LLVMContext (LTO scenario). In this case we should
3327  // remangle intrinsics names as well.
3328  RemangledIntrinsics[&F] = Remangled.getValue();
3329  // Look for functions that rely on old function attribute behavior.
3331  }
3332 
3333  // Look for global variables which need to be renamed.
3334  std::vector<std::pair<GlobalVariable *, GlobalVariable *>> UpgradedVariables;
3335  for (GlobalVariable &GV : TheModule->globals())
3336  if (GlobalVariable *Upgraded = UpgradeGlobalVariable(&GV))
3337  UpgradedVariables.emplace_back(&GV, Upgraded);
3338  for (auto &Pair : UpgradedVariables) {
3339  Pair.first->eraseFromParent();
3340  TheModule->getGlobalList().push_back(Pair.second);
3341  }
3342 
3343  // Force deallocation of memory for these vectors to favor the client that
3344  // want lazy deserialization.
3345  std::vector<std::pair<GlobalVariable *, unsigned>>().swap(GlobalInits);
3346  std::vector<std::pair<GlobalValue *, unsigned>>().swap(IndirectSymbolInits);
3347  return Error::success();
3348 }
3349 
3350 /// Support for lazy parsing of function bodies. This is required if we
3351 /// either have an old bitcode file without a VST forward declaration record,
3352 /// or if we have an anonymous function being materialized, since anonymous
3353 /// functions do not have a name and are therefore not in the VST.
3354 Error BitcodeReader::rememberAndSkipFunctionBodies() {
3355  if (Error JumpFailed = Stream.JumpToBit(NextUnreadBit))
3356  return JumpFailed;
3357 
3358  if (Stream.AtEndOfStream())
3359  return error("Could not find function in stream");
3360 
3361  if (!SeenFirstFunctionBody)
3362  return error("Trying to materialize functions before seeing function blocks");
3363 
3364  // An old bitcode file with the symbol table at the end would have
3365  // finished the parse greedily.
3366  assert(SeenValueSymbolTable);
3367 
3369 
3370  while (true) {
3371  Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
3372  if (!MaybeEntry)
3373  return MaybeEntry.takeError();
3374  llvm::BitstreamEntry Entry = MaybeEntry.get();
3375 
3376  switch (Entry.Kind) {
3377  default:
3378  return error("Expect SubBlock");
3379  case BitstreamEntry::SubBlock:
3380  switch (Entry.ID) {
3381  default:
3382  return error("Expect function block");
3384  if (Error Err = rememberAndSkipFunctionBody())
3385  return Err;
3386  NextUnreadBit = Stream.GetCurrentBitNo();
3387  return Error::success();
3388  }
3389  }
3390  }
3391 }
3392 
3393 Error BitcodeReaderBase::readBlockInfo() {
3394  Expected<Optional<BitstreamBlockInfo>> MaybeNewBlockInfo =
3395  Stream.ReadBlockInfoBlock();
3396  if (!MaybeNewBlockInfo)
3397  return MaybeNewBlockInfo.takeError();
3398  Optional<BitstreamBlockInfo> NewBlockInfo =
3399  std::move(MaybeNewBlockInfo.get());
3400  if (!NewBlockInfo)
3401  return error("Malformed block");
3402  BlockInfo = std::move(*NewBlockInfo);
3403  return Error::success();
3404 }
3405 
3406 Error BitcodeReader::parseComdatRecord(ArrayRef<uint64_t> Record) {
3407  // v1: [selection_kind, name]
3408  // v2: [strtab_offset, strtab_size, selection_kind]
3409  StringRef Name;
3410  std::tie(Name, Record) = readNameFromStrtab(Record);
3411 
3412  if (Record.empty())
3413  return error("Invalid record");
3415  std::string OldFormatName;
3416  if (!UseStrtab) {
3417  if (Record.size() < 2)
3418  return error("Invalid record");
3419  unsigned ComdatNameSize = Record[1];
3420  if (ComdatNameSize > Record.size() - 2)
3421  return error("Comdat name size too large");
3422  OldFormatName.reserve(ComdatNameSize);
3423  for (unsigned i = 0; i != ComdatNameSize; ++i)
3424  OldFormatName += (char)Record[2 + i];
3425  Name = OldFormatName;
3426  }
3427  Comdat *C = TheModule->getOrInsertComdat(Name);
3428  C->setSelectionKind(SK);
3429  ComdatList.push_back(C);
3430  return Error::success();
3431 }
3432 
3433 static void inferDSOLocal(GlobalValue *GV) {
3434  // infer dso_local from linkage and visibility if it is not encoded.
3435  if (GV->hasLocalLinkage() ||
3436  (!GV->hasDefaultVisibility() && !GV->hasExternalWeakLinkage()))
3437  GV->setDSOLocal(true);
3438 }
3439 
3440 Error BitcodeReader::parseGlobalVarRecord(ArrayRef<uint64_t> Record) {
3441  // v1: [pointer type, isconst, initid, linkage, alignment, section,
3442  // visibility, threadlocal, unnamed_addr, externally_initialized,
3443  // dllstorageclass, comdat, attributes, preemption specifier,
3444  // partition strtab offset, partition strtab size] (name in VST)
3445  // v2: [strtab_offset, strtab_size, v1]
3446  StringRef Name;
3447  std::tie(Name, Record) = readNameFromStrtab(Record);
3448 
3449  if (Record.size() < 6)
3450  return error("Invalid record");
3451  unsigned TyID = Record[0];
3452  Type *Ty = getTypeByID(TyID);
3453  if (!Ty)
3454  return error("Invalid record");
3455  bool isConstant = Record[1] & 1;
3456  bool explicitType = Record[1] & 2;
3457  unsigned AddressSpace;
3458  if (explicitType) {
3459  AddressSpace = Record[1] >> 2;
3460  } else {
3461  if (!Ty->isPointerTy())
3462  return error("Invalid type for value");
3463  AddressSpace = cast<PointerType>(Ty)->getAddressSpace();
3464  TyID = getContainedTypeID(TyID);
3465  Ty = getTypeByID(TyID);
3466  if (!Ty)
3467  return error("Missing element type for old-style global");
3468  }
3469 
3470  uint64_t RawLinkage = Record[3];
3473  if (Error Err = parseAlignmentValue(Record[4], Alignment))
3474  return Err;
3475  std::string Section;
3476  if (Record[5]) {
3477  if (Record[5] - 1 >= SectionTable.size())
3478  return error("Invalid ID");
3479  Section = SectionTable[Record[5] - 1];
3480  }
3481  GlobalValue::VisibilityTypes Visibility = GlobalValue::DefaultVisibility;
3482  // Local linkage must have default visibility.
3483  // auto-upgrade `hidden` and `protected` for old bitcode.
3484  if (Record.size() > 6 && !GlobalValue::isLocalLinkage(Linkage))
3485  Visibility = getDecodedVisibility(Record[6]);
3486 
3487  GlobalVariable::ThreadLocalMode TLM = GlobalVariable::NotThreadLocal;
3488  if (Record.size() > 7)
3490 
3492  if (Record.size() > 8)
3493  UnnamedAddr = getDecodedUnnamedAddrType(Record[8]);
3494 
3495  bool ExternallyInitialized = false;
3496  if (Record.size() > 9)
3497  ExternallyInitialized = Record[9];
3498 
3499  GlobalVariable *NewGV =
3500  new GlobalVariable(*TheModule, Ty, isConstant, Linkage, nullptr, Name,
3501  nullptr, TLM, AddressSpace, ExternallyInitialized);
3502  NewGV->setAlignment(Alignment);
3503  if (!Section.empty())
3504  NewGV->setSection(Section);
3505  NewGV->setVisibility(Visibility);
3506  NewGV->setUnnamedAddr(UnnamedAddr);
3507 
3508  if (Record.size() > 10)
3510  else
3511  upgradeDLLImportExportLinkage(NewGV, RawLinkage);
3512 
3513  ValueList.push_back(NewGV, getVirtualTypeID(NewGV->getType(), TyID));
3514 
3515  // Remember which value to use for the global initializer.
3516  if (unsigned InitID = Record[2])
3517  GlobalInits.push_back(std::make_pair(NewGV, InitID - 1));
3518 
3519  if (Record.size() > 11) {
3520  if (unsigned ComdatID = Record[11]) {
3521  if (ComdatID > ComdatList.size())
3522  return error("Invalid global variable comdat ID");
3523  NewGV->setComdat(ComdatList[ComdatID - 1]);
3524  }
3525  } else if (hasImplicitComdat(RawLinkage)) {
3526  ImplicitComdatObjects.insert(NewGV);
3527  }
3528 
3529  if (Record.size() > 12) {
3530  auto AS = getAttributes(Record[12]).getFnAttrs();
3531  NewGV->setAttributes(AS);
3532  }
3533 
3534  if (Record.size() > 13) {
3535  NewGV->setDSOLocal(getDecodedDSOLocal(Record[13]));
3536  }
3537  inferDSOLocal(NewGV);
3538 
3539  // Check whether we have enough values to read a partition name.
3540  if (Record.size() > 15)
3541  NewGV->setPartition(StringRef(Strtab.data() + Record[14], Record[15]));
3542 
3543  return Error::success();
3544 }
3545 
3546 Error BitcodeReader::parseFunctionRecord(ArrayRef<uint64_t> Record) {
3547  // v1: [type, callingconv, isproto, linkage, paramattr, alignment, section,
3548  // visibility, gc, unnamed_addr, prologuedata, dllstorageclass, comdat,
3549  // prefixdata, personalityfn, preemption specifier, addrspace] (name in VST)
3550  // v2: [strtab_offset, strtab_size, v1]
3551  StringRef Name;
3552  std::tie(Name, Record) = readNameFromStrtab(Record);
3553 
3554  if (Record.size() < 8)
3555  return error("Invalid record");
3556  unsigned FTyID = Record[0];
3557  Type *FTy = getTypeByID(FTyID);
3558  if (!FTy)
3559  return error("Invalid record");
3560  if (isa<PointerType>(FTy)) {
3561  FTyID = getContainedTypeID(FTyID, 0);
3562  FTy = getTypeByID(FTyID);
3563  if (!FTy)
3564  return error("Missing element type for old-style function");
3565  }
3566 
3567  if (!isa<FunctionType>(FTy))
3568  return error("Invalid type for value");
3569  auto CC = static_cast<CallingConv::ID>(Record[1]);
3570  if (CC & ~CallingConv::MaxID)
3571  return error("Invalid calling convention ID");
3572 
3573  unsigned AddrSpace = TheModule->getDataLayout().getProgramAddressSpace();
3574  if (Record.size() > 16)
3575  AddrSpace = Record[16];
3576 
3577  Function *Func =
3578  Function::Create(cast<FunctionType>(FTy), GlobalValue::ExternalLinkage,
3579  AddrSpace, Name, TheModule);
3580 
3581  assert(Func->getFunctionType() == FTy &&
3582  "Incorrect fully specified type provided for function");
3583  FunctionTypeIDs[Func] = FTyID;
3584 
3585  Func->setCallingConv(CC);
3586  bool isProto = Record[2];
3587  uint64_t RawLinkage = Record[3];
3588  Func->setLinkage(getDecodedLinkage(RawLinkage));
3589  Func->setAttributes(getAttributes(Record[4]));
3590 
3591  // Upgrade any old-style byval or sret without a type by propagating the
3592  // argument's pointee type. There should be no opaque pointers where the byval
3593  // type is implicit.
3594  for (unsigned i = 0; i != Func->arg_size(); ++i) {
3595  for (Attribute::AttrKind Kind : {Attribute::ByVal, Attribute::StructRet,
3596  Attribute::InAlloca}) {
3597  if (!Func->hasParamAttribute(i, Kind))
3598  continue;
3599 
3600  if (Func->getParamAttribute(i, Kind).getValueAsType())
3601  continue;
3602 
3603  Func->removeParamAttr(i, Kind);
3604 
3605  unsigned ParamTypeID = getContainedTypeID(FTyID, i + 1);
3606  Type *PtrEltTy = getPtrElementTypeByID(ParamTypeID);
3607  if (!PtrEltTy)
3608  return error("Missing param element type for attribute upgrade");
3609 
3610  Attribute NewAttr;
3611  switch (Kind) {
3612  case Attribute::ByVal:
3613  NewAttr = Attribute::getWithByValType(Context, PtrEltTy);
3614  break;
3615  case Attribute::StructRet:
3616  NewAttr = Attribute::getWithStructRetType(Context, PtrEltTy);
3617  break;
3618  case Attribute::InAlloca:
3619  NewAttr = Attribute::getWithInAllocaType(Context, PtrEltTy);
3620  break;
3621  default:
3622  llvm_unreachable("not an upgraded type attribute");
3623  }
3624 
3625  Func->addParamAttr(i, NewAttr);
3626  }
3627  }
3628 
3629  if (Func->getCallingConv() == CallingConv::X86_INTR &&
3630  !Func->arg_empty() && !Func->hasParamAttribute(0, Attribute::ByVal)) {
3631  unsigned ParamTypeID = getContainedTypeID(FTyID, 1);
3632  Type *ByValTy = getPtrElementTypeByID(ParamTypeID);
3633  if (!ByValTy)
3634  return error("Missing param element type for x86_intrcc upgrade");
3635  Attribute NewAttr = Attribute::getWithByValType(Context, ByValTy);
3636  Func->addParamAttr(0, NewAttr);
3637  }
3638 
3640  if (Error Err = parseAlignmentValue(Record[5], Alignment))
3641  return Err;
3642  Func->setAlignment(Alignment);
3643  if (Record[6]) {
3644  if (Record[6] - 1 >= SectionTable.size())
3645  return error("Invalid ID");
3646  Func->setSection(SectionTable[Record[6] - 1]);
3647  }
3648  // Local linkage must have default visibility.
3649  // auto-upgrade `hidden` and `protected` for old bitcode.
3650  if (!Func->hasLocalLinkage())
3651  Func->setVisibility(getDecodedVisibility(Record[7]));
3652  if (Record.size() > 8 && Record[8]) {
3653  if (Record[8] - 1 >= GCTable.size())
3654  return error("Invalid ID");
3655  Func->setGC(GCTable[Record[8] - 1]);
3656  }
3658  if (Record.size() > 9)
3659  UnnamedAddr = getDecodedUnnamedAddrType(Record[9]);
3660  Func->setUnnamedAddr(UnnamedAddr);
3661 
3662  FunctionOperandInfo OperandInfo = {Func, 0, 0, 0};
3663  if (Record.size() > 10)
3664  OperandInfo.Prologue = Record[10];
3665 
3666  if (Record.size() > 11)
3667  Func->setDLLStorageClass(getDecodedDLLStorageClass(Record[11]));
3668  else
3669  upgradeDLLImportExportLinkage(Func, RawLinkage);
3670 
3671  if (Record.size() > 12) {
3672  if (unsigned ComdatID = Record[12]) {
3673  if (ComdatID > ComdatList.size())
3674  return error("Invalid function comdat ID");
3675  Func->setComdat(ComdatList[ComdatID - 1]);
3676  }
3677  } else if (hasImplicitComdat(RawLinkage)) {
3678  ImplicitComdatObjects.insert(Func);
3679  }
3680 
3681  if (Record.size() > 13)
3682  OperandInfo.Prefix = Record[13];
3683 
3684  if (Record.size() > 14)
3685  OperandInfo.PersonalityFn = Record[14];
3686 
3687  if (Record.size() > 15) {
3688  Func->setDSOLocal(getDecodedDSOLocal(Record[15]));
3689  }
3690  inferDSOLocal(Func);
3691 
3692  // Record[16] is the address space number.
3693 
3694  // Check whether we have enough values to read a partition name. Also make
3695  // sure Strtab has enough values.
3696  if (Record.size() > 18 && Strtab.data() &&
3697  Record[17] + Record[18] <= Strtab.size()) {
3698  Func->setPartition(StringRef(Strtab.data() + Record[17], Record[18]));
3699  }
3700 
3701  ValueList.push_back(Func, getVirtualTypeID(Func->getType(), FTyID));
3702 
3703  if (OperandInfo.PersonalityFn || OperandInfo.Prefix || OperandInfo.Prologue)
3704  FunctionOperands.push_back(OperandInfo);
3705 
3706  // If this is a function with a body, remember the prototype we are
3707  // creating now, so that we can match up the body with them later.
3708  if (!isProto) {
3709  Func->setIsMaterializable(true);
3710  FunctionsWithBodies.push_back(Func);
3711  DeferredFunctionInfo[Func] = 0;
3712  }
3713  return Error::success();
3714 }
3715 
3716 Error BitcodeReader::parseGlobalIndirectSymbolRecord(
3717  unsigned BitCode, ArrayRef<uint64_t> Record) {
3718  // v1 ALIAS_OLD: [alias type, aliasee val#, linkage] (name in VST)
3719  // v1 ALIAS: [alias type, addrspace, aliasee val#, linkage, visibility,
3720  // dllstorageclass, threadlocal, unnamed_addr,
3721  // preemption specifier] (name in VST)
3722  // v1 IFUNC: [alias type, addrspace, aliasee val#, linkage,
3723  // visibility, dllstorageclass, threadlocal, unnamed_addr,
3724  // preemption specifier] (name in VST)
3725  // v2: [strtab_offset, strtab_size, v1]
3726  StringRef Name;
3727  std::tie(Name, Record) = readNameFromStrtab(Record);
3728 
3729  bool NewRecord = BitCode != bitc::MODULE_CODE_ALIAS_OLD;
3730  if (Record.size() < (3 + (unsigned)NewRecord))
3731  return error("Invalid record");
3732  unsigned OpNum = 0;
3733  unsigned TypeID = Record[OpNum++];
3734  Type *Ty = getTypeByID(TypeID);
3735  if (!Ty)
3736  return error("Invalid record");
3737 
3738  unsigned AddrSpace;
3739  if (!NewRecord) {
3740  auto *PTy = dyn_cast<PointerType>(Ty);
3741  if (!PTy)
3742  return error("Invalid type for value");
3743  AddrSpace = PTy->getAddressSpace();
3744  TypeID = getContainedTypeID(TypeID);
3745  Ty = getTypeByID(TypeID);
3746  if (!Ty)
3747  return error("Missing element type for old-style indirect symbol");
3748  } else {
3749  AddrSpace = Record[OpNum++];
3750  }
3751 
3752  auto Val = Record[OpNum++];
3753  auto Linkage = Record[OpNum++];
3754  GlobalValue *NewGA;
3755  if (BitCode == bitc::MODULE_CODE_ALIAS ||
3756  BitCode == bitc::MODULE_CODE_ALIAS_OLD)
3757  NewGA = GlobalAlias::create(Ty, AddrSpace, getDecodedLinkage(Linkage), Name,
3758  TheModule);
3759  else
3760  NewGA = GlobalIFunc::create(Ty, AddrSpace, getDecodedLinkage(Linkage), Name,
3761  nullptr, TheModule);
3762 
3763  // Local linkage must have default visibility.
3764  // auto-upgrade `hidden` and `protected` for old bitcode.
3765  if (OpNum != Record.size()) {
3766  auto VisInd = OpNum++;
3767  if (!NewGA->hasLocalLinkage())
3768  NewGA->setVisibility(getDecodedVisibility(Record[VisInd]));
3769  }
3770  if (BitCode == bitc::MODULE_CODE_ALIAS ||
3771  BitCode == bitc::MODULE_CODE_ALIAS_OLD) {
3772  if (OpNum != Record.size())
3774  else
3776  if (OpNum != Record.size())
3778  if (OpNum != Record.size())
3780  }
3781  if (OpNum != Record.size())
3782  NewGA->setDSOLocal(getDecodedDSOLocal(Record[OpNum++]));
3783  inferDSOLocal(NewGA);
3784 
3785  // Check whether we have enough values to read a partition name.
3786  if (OpNum + 1 < Record.size()) {
3787  NewGA->setPartition(
3788  StringRef(Strtab.data() + Record[OpNum], Record[OpNum + 1]));
3789  OpNum += 2;
3790  }
3791 
3792  ValueList.push_back(NewGA, getVirtualTypeID(NewGA->getType(), TypeID));
3793  IndirectSymbolInits.push_back(std::make_pair(NewGA, Val));
3794  return Error::success();
3795 }
3796 
3798  bool ShouldLazyLoadMetadata,
3799  DataLayoutCallbackTy DataLayoutCallback) {
3800  if (ResumeBit) {
3801  if (Error JumpFailed = Stream.JumpToBit(ResumeBit))
3802  return JumpFailed;
3803  } else if (Error Err = Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID))
3804  return Err;
3805 
3807 
3808  // Parts of bitcode parsing depend on the datalayout. Make sure we
3809  // finalize the datalayout before we run any of that code.
3810  bool ResolvedDataLayout = false;
3811  auto ResolveDataLayout = [&] {
3812  if (ResolvedDataLayout)
3813  return;
3814 
3815  // datalayout and triple can't be parsed after this point.
3816  ResolvedDataLayout = true;
3817 
3818  // Upgrade data layout string.
3819  std::string DL = llvm::UpgradeDataLayoutString(
3820  TheModule->getDataLayoutStr(), TheModule->getTargetTriple());
3821  TheModule->setDataLayout(DL);
3822 
3823  if (auto LayoutOverride =
3824  DataLayoutCallback(TheModule->getTargetTriple()))
3825  TheModule->setDataLayout(*LayoutOverride);
3826  };
3827 
3828  // Read all the records for this module.
3829  while (true) {
3830  Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
3831  if (!MaybeEntry)
3832  return MaybeEntry.takeError();
3833  llvm::BitstreamEntry Entry = MaybeEntry.get();
3834 
3835  switch (Entry.Kind) {
3836  case BitstreamEntry::Error:
3837  return error("Malformed block");
3838  case BitstreamEntry::EndBlock:
3839  ResolveDataLayout();
3840  return globalCleanup();
3841 
3842  case BitstreamEntry::SubBlock:
3843  switch (Entry.ID) {
3844  default: // Skip unknown content.
3845  if (Error Err = Stream.SkipBlock())
3846  return Err;
3847  break;
3849  if (Error Err = readBlockInfo())
3850  return Err;
3851  break;
3853  if (Error Err = parseAttributeBlock())
3854  return Err;
3855  break;
3857  if (Error Err = parseAttributeGroupBlock())
3858  return Err;
3859  break;
3861  if (Error Err = parseTypeTable())
3862  return Err;
3863  break;
3865  if (!SeenValueSymbolTable) {
3866  // Either this is an old form VST without function index and an
3867  // associated VST forward declaration record (which would have caused
3868  // the VST to be jumped to and parsed before it was encountered
3869  // normally in the stream), or there were no function blocks to
3870  // trigger an earlier parsing of the VST.
3871  assert(VSTOffset == 0 || FunctionsWithBodies.empty());
3872  if (Error Err = parseValueSymbolTable())
3873  return Err;
3874  SeenValueSymbolTable = true;
3875  } else {
3876  // We must have had a VST forward declaration record, which caused
3877  // the parser to jump to and parse the VST earlier.
3878  assert(VSTOffset > 0);
3879  if (Error Err = Stream.SkipBlock())
3880  return Err;
3881  }
3882  break;
3884  if (Error Err = parseConstants())
3885  return Err;
3886  if (Error Err = resolveGlobalAndIndirectSymbolInits())
3887  return Err;
3888  break;
3890  if (ShouldLazyLoadMetadata) {
3891  if (Error Err = rememberAndSkipMetadata())
3892  return Err;
3893  break;
3894  }
3895  assert(DeferredMetadataInfo.empty() && "Unexpected deferred metadata");
3896  if (Error Err = MDLoader->parseModuleMetadata())
3897  return Err;
3898  break;
3900  if (Error Err = MDLoader->parseMetadataKinds())
3901  return Err;
3902  break;
3904  ResolveDataLayout();
3905 
3906  // If this is the first function body we've seen, reverse the
3907  // FunctionsWithBodies list.
3908  if (!SeenFirstFunctionBody) {
3909  std::reverse(FunctionsWithBodies.begin(), FunctionsWithBodies.end());
3910  if (Error Err = globalCleanup())
3911  return Err;
3912  SeenFirstFunctionBody = true;
3913  }
3914 
3915  if (VSTOffset > 0) {
3916  // If we have a VST forward declaration record, make sure we
3917  // parse the VST now if we haven't already. It is needed to
3918  // set up the DeferredFunctionInfo vector for lazy reading.
3919  if (!SeenValueSymbolTable) {
3920  if (Error Err = BitcodeReader::parseValueSymbolTable(VSTOffset))
3921  return Err;
3922  SeenValueSymbolTable = true;
3923  // Fall through so that we record the NextUnreadBit below.
3924  // This is necessary in case we have an anonymous function that
3925  // is later materialized. Since it will not have a VST entry we
3926  // need to fall back to the lazy parse to find its offset.
3927  } else {
3928  // If we have a VST forward declaration record, but have already
3929  // parsed the VST (just above, when the first function body was
3930  // encountered here), then we are resuming the parse after
3931  // materializing functions. The ResumeBit points to the
3932  // start of the last function block recorded in the
3933  // DeferredFunctionInfo map. Skip it.
3934  if (Error Err = Stream.SkipBlock())
3935  return Err;
3936  continue;
3937  }
3938  }
3939 
3940  // Support older bitcode files that did not have the function
3941  // index in the VST, nor a VST forward declaration record, as
3942  // well as anonymous functions that do not have VST entries.
3943  // Build the DeferredFunctionInfo vector on the fly.
3944  if (Error Err = rememberAndSkipFunctionBody())
3945  return Err;
3946 
3947  // Suspend parsing when we reach the function bodies. Subsequent
3948  // materialization calls will resume it when necessary. If the bitcode
3949  // file is old, the symbol table will be at the end instead and will not
3950  // have been seen yet. In this case, just finish the parse now.
3951  if (SeenValueSymbolTable) {
3952  NextUnreadBit = Stream.GetCurrentBitNo();
3953  // After the VST has been parsed, we need to make sure intrinsic name
3954  // are auto-upgraded.
3955  return globalCleanup();
3956  }
3957  break;
3959  if (Error Err = parseUseLists())
3960  return Err;
3961  break;
3963  if (Error Err = parseOperandBundleTags())
3964  return Err;
3965  break;
3967  if (Error Err = parseSyncScopeNames())
3968  return Err;
3969  break;
3970  }
3971  continue;
3972 
3973  case BitstreamEntry::Record:
3974  // The interesting case.
3975  break;
3976  }
3977 
3978  // Read a record.
3979  Expected<unsigned> MaybeBitCode = Stream.readRecord(Entry.ID, Record);
3980  if (!MaybeBitCode)
3981  return MaybeBitCode.takeError();
3982  switch (unsigned BitCode = MaybeBitCode.get()) {
3983  default: break; // Default behavior, ignore unknown content.
3985  Expected<unsigned> VersionOrErr = parseVersionRecord(Record);
3986  if (!VersionOrErr)
3987  return VersionOrErr.takeError();
3988  UseRelativeIDs = *VersionOrErr >= 1;
3989  break;
3990  }
3991  case bitc::MODULE_CODE_TRIPLE: { // TRIPLE: [strchr x N]
3992  if (ResolvedDataLayout)
3993  return error("target triple too late in module");
3994  std::string S;
3995  if (convertToString(Record, 0, S))
3996  return error("Invalid record");
3997  TheModule->setTargetTriple(S);
3998  break;
3999  }
4000  case bitc::MODULE_CODE_DATALAYOUT: { // DATALAYOUT: [strchr x N]
4001  if (ResolvedDataLayout)
4002  return error("datalayout too late in module");
4003  std::string S;
4004  if (convertToString(Record, 0, S))
4005  return error("Invalid record");
4007  if (!MaybeDL)
4008  return MaybeDL.takeError();
4009  TheModule->setDataLayout(MaybeDL.get());
4010  break;
4011  }
4012  case bitc::MODULE_CODE_ASM: { // ASM: [strchr x N]
4013  std::string S;
4014  if (convertToString(Record, 0, S))
4015  return error("Invalid record");
4016  TheModule->setModuleInlineAsm(S);
4017  break;
4018  }
4019  case bitc::MODULE_CODE_DEPLIB: { // DEPLIB: [strchr x N]
4020  // Deprecated, but still needed to read old bitcode files.
4021  std::string S;
4022  if (convertToString(Record, 0, S))
4023  return error("Invalid record");
4024  // Ignore value.
4025  break;
4026  }
4027  case bitc::MODULE_CODE_SECTIONNAME: { // SECTIONNAME: [strchr x N]
4028  std::string S;
4029  if (convertToString(Record, 0, S))
4030  return error("Invalid record");
4031  SectionTable.push_back(S);
4032  break;
4033  }
4034  case bitc::MODULE_CODE_GCNAME: { // SECTIONNAME: [strchr x N]
4035  std::string S;
4036  if (convertToString(Record, 0, S))
4037  return error("Invalid record");
4038  GCTable.push_back(S);
4039  break;
4040  }
4042  if (Error Err = parseComdatRecord(Record))
4043  return Err;
4044  break;
4045  // FIXME: BitcodeReader should handle {GLOBALVAR, FUNCTION, ALIAS, IFUNC}
4046  // written by ThinLinkBitcodeWriter. See
4047  // `ThinLinkBitcodeWriter::writeSimplifiedModuleInfo` for the format of each
4048  // record
4049  // (https://github.com/llvm/llvm-project/blob/b6a93967d9c11e79802b5e75cec1584d6c8aa472/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp#L4714)
4051  if (Error Err = parseGlobalVarRecord(Record))
4052  return Err;
4053  break;
4055  ResolveDataLayout();
4056  if (Error Err = parseFunctionRecord(Record))
4057  return Err;
4058  break;
4062  if (Error Err = parseGlobalIndirectSymbolRecord(BitCode, Record))
4063  return Err;
4064  break;
4065  /// MODULE_CODE_VSTOFFSET: [offset]
4067  if (Record.empty())
4068  return error("Invalid record");
4069  // Note that we subtract 1 here because the offset is relative to one word
4070  // before the start of the identification or module block, which was
4071  // historically always the start of the regular bitcode header.
4072  VSTOffset = Record[0] - 1;
4073  break;
4074  /// MODULE_CODE_SOURCE_FILENAME: [namechar x N]
4077  if (convertToString(Record, 0, ValueName))
4078  return error("Invalid record");
4079  TheModule->setSourceFileName(ValueName);
4080  break;
4081  }
4082  Record.clear();
4083  }
4084 }
4085 
4086 Error BitcodeReader::parseBitcodeInto(Module *M, bool ShouldLazyLoadMetadata,
4087  bool IsImporting,
4088  DataLayoutCallbackTy DataLayoutCallback) {
4089  TheModule = M;
4090  MDLoader = MetadataLoader(Stream, *M, ValueList, IsImporting,
4091  [&](unsigned ID) { return getTypeByID(ID); });
4092  return parseModule(0, ShouldLazyLoadMetadata, DataLayoutCallback);
4093 }
4094 
4095 Error BitcodeReader::typeCheckLoadStoreInst(Type *ValType, Type *PtrType) {
4096  if (!isa<PointerType>(PtrType))
4097  return error("Load/Store operand is not a pointer type");
4098 
4099  if (!cast<PointerType>(PtrType)->isOpaqueOrPointeeTypeMatches(ValType))
4100  return error("Explicit load/store type does not match pointee "
4101  "type of pointer operand");
4102  if (!PointerType::isLoadableOrStorableType(ValType))
4103  return error("Cannot load/store from pointer");
4104  return Error::success();
4105 }
4106 
4107 Error BitcodeReader::propagateAttributeTypes(CallBase *CB,
4108  ArrayRef<unsigned> ArgTyIDs) {
4110  for (unsigned i = 0; i != CB->arg_size(); ++i) {
4111  for (Attribute::AttrKind Kind : {Attribute::ByVal, Attribute::StructRet,
4112  Attribute::InAlloca}) {
4113  if (!Attrs.hasParamAttr(i, Kind) ||
4114  Attrs.getParamAttr(i, Kind).getValueAsType())
4115  continue;
4116 
4117  Type *PtrEltTy = getPtrElementTypeByID(ArgTyIDs[i]);
4118  if (!PtrEltTy)
4119  return error("Missing element type for typed attribute upgrade");
4120 
4121  Attribute NewAttr;
4122  switch (Kind) {
4123  case Attribute::ByVal:
4124  NewAttr = Attribute::getWithByValType(Context, PtrEltTy);
4125  break;
4126  case Attribute::StructRet:
4127  NewAttr = Attribute::getWithStructRetType(Context, PtrEltTy);
4128  break;
4129  case Attribute::InAlloca:
4130  NewAttr = Attribute::getWithInAllocaType(Context, PtrEltTy);
4131  break;
4132  default:
4133  llvm_unreachable("not an upgraded type attribute");
4134  }
4135 
4136  Attrs = Attrs.addParamAttribute(Context, i, NewAttr);
4137  }
4138  }
4139 
4140  if (CB->isInlineAsm()) {
4141  const InlineAsm *IA = cast<InlineAsm>(CB->getCalledOperand());
4142  unsigned ArgNo = 0;
4143  for (const InlineAsm::ConstraintInfo &CI : IA->ParseConstraints()) {
4144  if (!CI.hasArg())
4145  continue;
4146 
4147  if (CI.isIndirect && !Attrs.getParamElementType(ArgNo)) {
4148  Type *ElemTy = getPtrElementTypeByID(ArgTyIDs[ArgNo]);
4149  if (!ElemTy)
4150  return error("Missing element type for inline asm upgrade");
4151  Attrs = Attrs.addParamAttribute(
4152  Context, ArgNo,
4153  Attribute::get(Context, Attribute::ElementType, ElemTy));
4154  }
4155 
4156  ArgNo++;
4157  }
4158  }
4159 
4160  switch (CB->getIntrinsicID()) {
4161  case Intrinsic::preserve_array_access_index:
4162  case Intrinsic::preserve_struct_access_index:
4163  case Intrinsic::aarch64_ldaxr:
4164  case Intrinsic::aarch64_ldxr:
4165  case Intrinsic::aarch64_stlxr:
4166  case Intrinsic::aarch64_stxr:
4167  case Intrinsic::arm_ldaex:
4168  case Intrinsic::arm_ldrex:
4169  case Intrinsic::arm_stlex:
4170  case Intrinsic::arm_strex: {
4171  unsigned ArgNo;
4172  switch (CB->getIntrinsicID()) {
4173  case Intrinsic::aarch64_stlxr:
4174  case Intrinsic::aarch64_stxr:
4175  case Intrinsic::arm_stlex:
4176  case Intrinsic::arm_strex:
4177  ArgNo = 1;
4178  break;
4179  default:
4180  ArgNo = 0;
4181  break;
4182  }
4183  if (!Attrs.getParamElementType(ArgNo)) {
4184  Type *ElTy = getPtrElementTypeByID(ArgTyIDs[ArgNo]);
4185  if (!ElTy)
4186  return error("Missing element type for elementtype upgrade");
4187  Attribute NewAttr = Attribute::get(Context, Attribute::ElementType, ElTy);
4188  Attrs = Attrs.addParamAttribute(Context, ArgNo, NewAttr);
4189  }
4190  break;
4191  }
4192  default:
4193  break;
4194  }
4195 
4196  CB->setAttributes(Attrs);
4197  return Error::success();
4198 }
4199 
4200 /// Lazily parse the specified function body block.
4201 Error BitcodeReader::parseFunctionBody(Function *F) {
4202  if (Error Err = Stream.EnterSubBlock(bitc::FUNCTION_BLOCK_ID))
4203  return Err;
4204 
4205  // Unexpected unresolved metadata when parsing function.
4206  if (MDLoader->hasFwdRefs())
4207  return error("Invalid function metadata: incoming forward references");
4208 
4209  InstructionList.clear();
4210  unsigned ModuleValueListSize = ValueList.size();
4211  unsigned ModuleMDLoaderSize = MDLoader->size();
4212 
4213  // Add all the function arguments to the value table.
4214  unsigned ArgNo = 0;
4215  unsigned FTyID = FunctionTypeIDs[F];
4216  for (Argument &I : F->args()) {
4217  unsigned ArgTyID = getContainedTypeID(FTyID, ArgNo + 1);
4218  assert(I.getType() == getTypeByID(ArgTyID) &&
4219  "Incorrect fully specified type for Function Argument");
4220  ValueList.push_back(&I, ArgTyID);
4221  ++ArgNo;
4222  }
4223  unsigned NextValueNo = ValueList.size();
4224  BasicBlock *CurBB = nullptr;
4225  unsigned CurBBNo = 0;
4226 
4227  DebugLoc LastLoc;
4228  auto getLastInstruction = [&]() -> Instruction * {
4229  if (CurBB && !CurBB->empty())
4230  return &CurBB->back();
4231  else if (CurBBNo && FunctionBBs[CurBBNo - 1] &&
4232  !FunctionBBs[CurBBNo - 1]->empty())
4233  return &FunctionBBs[CurBBNo - 1]->back();
4234  return nullptr;
4235  };
4236 
4237  std::vector<OperandBundleDef> OperandBundles;
4238 
4239  // Read all the records.
4241 
4242  while (true) {
4243  Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4244  if (!MaybeEntry)
4245  return MaybeEntry.takeError();
4246  llvm::BitstreamEntry Entry = MaybeEntry.get();
4247 
4248  switch (Entry.Kind) {
4249  case BitstreamEntry::Error:
4250  return error("Malformed block");
4251  case BitstreamEntry::EndBlock:
4252  goto OutOfRecordLoop;
4253 
4254  case BitstreamEntry::SubBlock:
4255  switch (Entry.ID) {
4256  default: // Skip unknown content.
4257  if (Error Err = Stream.SkipBlock())
4258  return Err;
4259  break;
4261  if (Error Err = parseConstants())
4262  return Err;
4263  NextValueNo = ValueList.size();
4264  break;
4266  if (Error Err = parseValueSymbolTable())
4267  return Err;
4268  break;
4270  if (Error Err = MDLoader->parseMetadataAttachment(*F, InstructionList))
4271  return Err;
4272  break;
4274  assert(DeferredMetadataInfo.empty() &&
4275  "Must read all module-level metadata before function-level");
4276  if (Error Err = MDLoader->parseFunctionMetadata())
4277  return Err;
4278  break;
4280  if (Error Err = parseUseLists())
4281  return Err;
4282  break;
4283  }
4284  continue;
4285 
4286  case BitstreamEntry::Record:
4287  // The interesting case.
4288  break;
4289  }
4290 
4291  // Read a record.
4292  Record.clear();
4293  Instruction *I = nullptr;
4294  unsigned ResTypeID = InvalidTypeID;
4295  Expected<unsigned> MaybeBitCode = Stream.readRecord(Entry.ID, Record);
4296  if (!MaybeBitCode)
4297  return MaybeBitCode.takeError();
4298  switch (unsigned BitCode = MaybeBitCode.get()) {
4299  default: // Default behavior: reject
4300  return error("Invalid value");
4301  case bitc::FUNC_CODE_DECLAREBLOCKS: { // DECLAREBLOCKS: [nblocks]
4302  if (Record.empty() || Record[0] == 0)
4303  return error("Invalid record");
4304  // Create all the basic blocks for the function.
4305  FunctionBBs.resize(Record[0]);
4306 
4307  // See if anything took the address of blocks in this function.
4308  auto BBFRI = BasicBlockFwdRefs.find(F);
4309  if (BBFRI == BasicBlockFwdRefs.end()) {
4310  for (BasicBlock *&BB : FunctionBBs)
4311  BB = BasicBlock::Create(Context, "", F);
4312  } else {
4313  auto &BBRefs = BBFRI->second;
4314  // Check for invalid basic block references.
4315  if (BBRefs.size() > FunctionBBs.size())
4316  return error("Invalid ID");
4317  assert(!BBRefs.empty() && "Unexpected empty array");
4318  assert(!BBRefs.front() && "Invalid reference to entry block");
4319  for (unsigned I = 0, E = FunctionBBs.size(), RE = BBRefs.size(); I != E;
4320  ++I)
4321  if (I < RE && BBRefs[I]) {
4322  BBRefs[I]->insertInto(F);
4323  FunctionBBs[I] = BBRefs[I];
4324  } else {
4325  FunctionBBs[I] = BasicBlock::Create(Context, "", F);
4326  }
4327 
4328  // Erase from the table.
4329  BasicBlockFwdRefs.erase(BBFRI);
4330  }
4331 
4332  CurBB = FunctionBBs[0];
4333  continue;
4334  }
4335 
4336  case bitc::FUNC_CODE_BLOCKADDR_USERS: // BLOCKADDR_USERS: [vals...]
4337  // The record should not be emitted if it's an empty list.
4338  if (Record.empty())
4339  return error("Invalid record");
4340  // When we have the RARE case of a BlockAddress Constant that is not
4341  // scoped to the Function it refers to, we need to conservatively
4342  // materialize the referred to Function, regardless of whether or not
4343  // that Function will ultimately be linked, otherwise users of
4344  // BitcodeReader might start splicing out Function bodies such that we
4345  // might no longer be able to materialize the BlockAddress since the
4346  // BasicBlock (and entire body of the Function) the BlockAddress refers
4347  // to may have been moved. In the case that the user of BitcodeReader
4348  // decides ultimately not to link the Function body, materializing here
4349  // could be considered wasteful, but it's better than a deserialization
4350  // failure as described. This keeps BitcodeReader unaware of complex
4351  // linkage policy decisions such as those use by LTO, leaving those
4352  // decisions "one layer up."
4353  for (uint64_t ValID : Record)
4354  if (auto *F = dyn_cast<Function>(ValueList[ValID]))
4355  BackwardRefFunctions.push_back(F);
4356  else
4357  return error("Invalid record");
4358 
4359  continue;
4360 
4361  case bitc::FUNC_CODE_DEBUG_LOC_AGAIN: // DEBUG_LOC_AGAIN
4362  // This record indicates that the last instruction is at the same
4363  // location as the previous instruction with a location.
4364  I = getLastInstruction();
4365 
4366  if (!I)
4367  return error("Invalid record");
4368  I->setDebugLoc(LastLoc);
4369  I = nullptr;
4370  continue;
4371 
4372  case bitc::FUNC_CODE_DEBUG_LOC: { // DEBUG_LOC: [line, col, scope, ia]
4373  I = getLastInstruction();
4374  if (!I || Record.size() < 4)
4375  return error("Invalid record");
4376 
4377  unsigned Line = Record[0], Col = Record[1];
4378  unsigned ScopeID = Record[2], IAID = Record[3];
4379  bool isImplicitCode = Record.size() == 5 && Record[4];
4380 
4381  MDNode *Scope = nullptr, *IA = nullptr;
4382  if (ScopeID) {
4383  Scope = dyn_cast_or_null<MDNode>(
4384  MDLoader->getMetadataFwdRefOrLoad(ScopeID - 1));
4385  if (!Scope)
4386  return error("Invalid record");
4387  }
4388  if (IAID) {
4389  IA = dyn_cast_or_null<MDNode>(
4390  MDLoader->getMetadataFwdRefOrLoad(IAID - 1));
4391  if (!IA)
4392  return error("Invalid record");
4393  }
4394  LastLoc = DILocation::get(Scope->getContext(), Line, Col, Scope, IA,
4395  isImplicitCode);
4396  I->setDebugLoc(LastLoc);
4397  I = nullptr;
4398  continue;
4399  }
4400  case bitc::FUNC_CODE_INST_UNOP: { // UNOP: [opval, ty, opcode]
4401  unsigned OpNum = 0;
4402  Value *LHS;
4403  unsigned TypeID;
4404  if (getValueTypePair(Record, OpNum, NextValueNo, LHS, TypeID) ||
4405  OpNum+1 > Record.size())
4406  return error("Invalid record");
4407 
4408  int Opc = getDecodedUnaryOpcode(Record[OpNum++], LHS->getType());
4409  if (Opc == -1)
4410  return error("Invalid record");
4411  I = UnaryOperator::Create((Instruction::UnaryOps)Opc, LHS);
4412  ResTypeID = TypeID;
4413  InstructionList.push_back(I);
4414  if (OpNum < Record.size()) {
4415  if (isa<FPMathOperator>(I)) {
4417  if (FMF.any())
4418  I->setFastMathFlags(FMF);
4419  }
4420  }
4421  break;
4422  }
4423  case bitc::FUNC_CODE_INST_BINOP: { // BINOP: [opval, ty, opval, opcode]
4424  unsigned OpNum = 0;
4425  Value *LHS, *RHS;
4426  unsigned TypeID;
4427  if (getValueTypePair(Record, OpNum, NextValueNo, LHS, TypeID) ||
4428  popValue(Record, OpNum, NextValueNo, LHS->getType(), TypeID, RHS) ||
4429  OpNum+1 > Record.size())
4430  return error("Invalid record");
4431 
4432  int Opc = getDecodedBinaryOpcode(Record[OpNum++], LHS->getType());
4433  if (Opc == -1)
4434  return error("Invalid record");
4435  I = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS);
4436  ResTypeID = TypeID;
4437  InstructionList.push_back(I);
4438  if (OpNum < Record.size()) {
4439  if (Opc == Instruction::Add ||
4440  Opc == Instruction::Sub ||
4441  Opc == Instruction::Mul ||
4442  Opc == Instruction::Shl) {
4443  if (Record[OpNum] & (1 << bitc::OBO_NO_SIGNED_WRAP))
4444  cast<BinaryOperator>(I)->setHasNoSignedWrap(true);
4445  if (Record[OpNum] & (1 << bitc::OBO_NO_UNSIGNED_WRAP))
4446  cast<BinaryOperator>(I)->setHasNoUnsignedWrap(true);
4447  } else if (Opc == Instruction::SDiv ||
4448  Opc == Instruction::UDiv ||
4449  Opc == Instruction::LShr ||
4450  Opc == Instruction::AShr) {
4451  if (Record[OpNum] & (1 << bitc::PEO_EXACT))
4452  cast<BinaryOperator>(I)->setIsExact(true);
4453  } else if (isa<FPMathOperator>(I)) {
4455  if (FMF.any())
4456  I->setFastMathFlags(FMF);
4457  }
4458 
4459  }
4460  break;
4461  }
4462  case bitc::FUNC_CODE_INST_CAST: { // CAST: [opval, opty, destty, castopc]
4463  unsigned OpNum = 0;
4464  Value *Op;
4465  unsigned OpTypeID;
4466  if (getValueTypePair(Record, OpNum, NextValueNo, Op, OpTypeID) ||
4467  OpNum+2 != Record.size())
4468  return error("Invalid record");
4469 
4470  ResTypeID = Record[OpNum];
4471  Type *ResTy = getTypeByID(ResTypeID);
4472  int Opc = getDecodedCastOpcode(Record[OpNum + 1]);
4473  if (Opc == -1 || !ResTy)
4474  return error("Invalid record");
4475  Instruction *Temp = nullptr;
4476  if ((I = UpgradeBitCastInst(Opc, Op, ResTy, Temp))) {
4477  if (Temp) {
4478  InstructionList.push_back(Temp);
4479  assert(CurBB && "No current BB?");
4480  CurBB->getInstList().push_back(Temp);
4481  }
4482  } else {
4483  auto CastOp = (Instruction::CastOps)Opc;
4484  if (!CastInst::castIsValid(CastOp, Op, ResTy))
4485  return error("Invalid cast");
4486  I = CastInst::Create(CastOp, Op, ResTy);
4487  }
4488  InstructionList.push_back(I);
4489  break;
4490  }
4493  case bitc::FUNC_CODE_INST_GEP: { // GEP: type, [n x operands]
4494  unsigned OpNum = 0;
4495 
4496  unsigned TyID;
4497  Type *Ty;
4498  bool InBounds;
4499 
4500  if (BitCode == bitc::FUNC_CODE_INST_GEP) {
4501  InBounds = Record[OpNum++];
4502  TyID = Record[OpNum++];
4503  Ty = getTypeByID(TyID);
4504  } else {
4505  InBounds = BitCode == bitc::FUNC_CODE_INST_INBOUNDS_GEP_OLD;
4506  TyID = InvalidTypeID;
4507  Ty = nullptr;
4508  }
4509 
4510  Value *BasePtr;
4511  unsigned BasePtrTypeID;
4512  if (getValueTypePair(Record, OpNum, NextValueNo, BasePtr, BasePtrTypeID))
4513  return error("Invalid record");
4514 
4515  if (!Ty) {
4516  TyID = getContainedTypeID(BasePtrTypeID);
4517  if (BasePtr->getType()->isVectorTy())
4518  TyID = getContainedTypeID(TyID);
4519  Ty = getTypeByID(TyID);
4520  } else if (!cast<PointerType>(BasePtr->getType()->getScalarType())
4521  ->isOpaqueOrPointeeTypeMatches(Ty)) {
4522  return error(
4523  "Explicit gep type does not match pointee type of pointer operand");
4524  }
4525 
4526  SmallVector<Value*, 16> GEPIdx;
4527  while (OpNum != Record.size()) {
4528  Value *Op;
4529  unsigned OpTypeID;
4530  if (getValueTypePair(Record, OpNum, NextValueNo, Op, OpTypeID))
4531  return error("Invalid record");
4532  GEPIdx.push_back(Op);
4533  }
4534 
4535  I = GetElementPtrInst::Create(Ty, BasePtr, GEPIdx);
4536 
4537  ResTypeID = TyID;
4538  if (cast<GEPOperator>(I)->getNumIndices() != 0) {
4539  auto GTI = std::next(gep_type_begin(I));
4540  for (Value *Idx : drop_begin(cast<GEPOperator>(I)->indices())) {
4541  unsigned SubType = 0;
4542  if (GTI.isStruct()) {
4543  ConstantInt *IdxC =
4544  Idx->getType()->isVectorTy()
4545  ? cast<ConstantInt>(cast<Constant>(Idx)->getSplatValue())
4546  : cast<ConstantInt>(Idx);
4547  SubType = IdxC->getZExtValue();
4548  }
4549  ResTypeID = getContainedTypeID(ResTypeID, SubType);
4550  ++GTI;
4551  }
4552  }
4553 
4554  // At this point ResTypeID is the result element type. We need a pointer
4555  // or vector of pointer to it.
4556  ResTypeID = getVirtualTypeID(I->getType()->getScalarType(), ResTypeID);
4557  if (I->getType()->isVectorTy())
4558  ResTypeID = getVirtualTypeID(I->getType(), ResTypeID);
4559 
4560  InstructionList.push_back(I);
4561  if (InBounds)
4562  cast<GetElementPtrInst>(I)->setIsInBounds(true);
4563  break;
4564  }
4565 
4567  // EXTRACTVAL: [opty, opval, n x indices]
4568  unsigned OpNum = 0;
4569  Value *Agg;
4570  unsigned AggTypeID;
4571  if (getValueTypePair(Record, OpNum, NextValueNo, Agg, AggTypeID))
4572  return error("Invalid record");
4573  Type *Ty = Agg->getType();
4574 
4575  unsigned RecSize = Record.size();
4576  if (OpNum == RecSize)
4577  return error("EXTRACTVAL: Invalid instruction with 0 indices");
4578 
4579  SmallVector<unsigned, 4> EXTRACTVALIdx;
4580  ResTypeID = AggTypeID;
4581  for (; OpNum != RecSize; ++OpNum) {
4582  bool IsArray = Ty->isArrayTy();
4583  bool IsStruct = Ty->isStructTy();
4584  uint64_t Index = Record[OpNum];
4585 
4586  if (!IsStruct && !IsArray)
4587  return error("EXTRACTVAL: Invalid type");
4588  if ((unsigned)Index != Index)
4589  return error("Invalid value");
4590  if (IsStruct && Index >= Ty->getStructNumElements())
4591  return error("EXTRACTVAL: Invalid struct index");
4592  if (IsArray && Index >= Ty->getArrayNumElements())
4593  return error("EXTRACTVAL: Invalid array index");
4594  EXTRACTVALIdx.push_back((unsigned)Index);
4595 
4596  if (IsStruct) {
4597  Ty = Ty->getStructElementType(Index);
4598  ResTypeID = getContainedTypeID(ResTypeID, Index);
4599  } else {
4600  Ty = Ty->getArrayElementType();
4601  ResTypeID = getContainedTypeID(ResTypeID);
4602  }
4603  }
4604 
4605  I = ExtractValueInst::Create(Agg, EXTRACTVALIdx);
4606  InstructionList.push_back(I);
4607  break;
4608  }
4609 
4611  // INSERTVAL: [opty, opval, opty, opval, n x indices]
4612  unsigned OpNum = 0;
4613  Value *Agg;
4614  unsigned AggTypeID;
4615  if (getValueTypePair(Record, OpNum, NextValueNo, Agg, AggTypeID))
4616  return error("Invalid record");
4617  Value *Val;
4618  unsigned ValTypeID;
4619  if (getValueTypePair(Record, OpNum, NextValueNo, Val, ValTypeID))
4620  return error("Invalid record");
4621 
4622  unsigned RecSize = Record.size();
4623  if (OpNum == RecSize)
4624  return error("INSERTVAL: Invalid instruction with 0 indices");
4625 
4626  SmallVector<unsigned, 4> INSERTVALIdx;
4627  Type *CurTy = Agg->getType();
4628  for (; OpNum != RecSize; ++OpNum) {
4629  bool IsArray = CurTy->isArrayTy();
4630  bool IsStruct = CurTy->isStructTy();
4631  uint64_t Index = Record[OpNum];
4632 
4633  if (!IsStruct && !IsArray)
4634  return error("INSERTVAL: Invalid type");
4635  if ((unsigned)Index != Index)
4636  return error("Invalid value");
4637  if (IsStruct && Index >= CurTy->getStructNumElements())
4638  return error("INSERTVAL: Invalid struct index");
4639  if (IsArray && Index >= CurTy->getArrayNumElements())
4640  return error("INSERTVAL: Invalid array index");
4641 
4642  INSERTVALIdx.push_back((unsigned)Index);
4643  if (IsStruct)
4644  CurTy = CurTy->getStructElementType(Index);
4645  else
4646  CurTy = CurTy->getArrayElementType();
4647  }
4648 
4649  if (CurTy != Val->getType())
4650  return error("Inserted value type doesn't match aggregate type");
4651 
4652  I = InsertValueInst::Create(Agg, Val, INSERTVALIdx);
4653  ResTypeID = AggTypeID;
4654  InstructionList.push_back(I);
4655  break;
4656  }
4657 
4658  case bitc::FUNC_CODE_INST_SELECT: { // SELECT: [opval, ty, opval, opval]
4659  // obsolete form of select
4660  // handles select i1 ... in old bitcode
4661  unsigned OpNum = 0;
4662  Value *TrueVal, *FalseVal, *Cond;
4663  unsigned TypeID;
4664  Type *CondType = Type::getInt1Ty(Context);
4665  if (getValueTypePair(Record, OpNum, NextValueNo, TrueVal, TypeID) ||
4666  popValue(Record, OpNum, NextValueNo, TrueVal->getType(), TypeID,
4667  FalseVal) ||
4668  popValue(Record, OpNum, NextValueNo, CondType,
4669  getVirtualTypeID(CondType), Cond))
4670  return error("Invalid record");
4671 
4672  I = SelectInst::Create(Cond, TrueVal, FalseVal);
4673  ResTypeID = TypeID;
4674  InstructionList.push_back(I);
4675  break;
4676  }
4677 
4678  case bitc::FUNC_CODE_INST_VSELECT: {// VSELECT: [ty,opval,opval,predty,pred]
4679  // new form of select
4680  // handles select i1 or select [N x i1]
4681  unsigned OpNum = 0;
4682  Value *TrueVal, *FalseVal, *Cond;
4683  unsigned ValTypeID, CondTypeID;
4684  if (getValueTypePair(Record, OpNum, NextValueNo, TrueVal, ValTypeID) ||
4685  popValue(Record, OpNum, NextValueNo, TrueVal->getType(), ValTypeID,
4686  FalseVal) ||
4687  getValueTypePair(Record, OpNum, NextValueNo, Cond, CondTypeID))
4688  return error("Invalid record");
4689 
4690  // select condition can be either i1 or [N x i1]
4691  if (VectorType* vector_type =
4692  dyn_cast<VectorType>(Cond->getType())) {
4693  // expect <n x i1>
4694  if (vector_type->getElementType() != Type::getInt1Ty(Context))
4695  return error("Invalid type for value");
4696  } else {
4697  // expect i1
4698  if (Cond->getType() != Type::getInt1Ty(Context))
4699  return error("Invalid type for value");
4700  }
4701 
4702  I = SelectInst::Create(Cond, TrueVal, FalseVal);
4703  ResTypeID = ValTypeID;
4704  InstructionList.push_back(I);
4705  if (OpNum < Record.size() && isa<FPMathOperator>(I)) {
4707  if (FMF.any())
4708  I->setFastMathFlags(FMF);
4709  }
4710  break;
4711  }
4712 
4713  case bitc::FUNC_CODE_INST_EXTRACTELT: { // EXTRACTELT: [opty, opval, opval]
4714  unsigned OpNum = 0;
4715  Value *Vec, *Idx;
4716  unsigned VecTypeID, IdxTypeID;
4717  if (getValueTypePair(Record, OpNum, NextValueNo, Vec, VecTypeID) ||
4718  getValueTypePair(Record, OpNum, NextValueNo, Idx, IdxTypeID))
4719  return error("Invalid record");
4720  if (!Vec->getType()->isVectorTy())
4721  return error("Invalid type for value");
4722  I = ExtractElementInst::Create(Vec, Idx);
4723  ResTypeID = getContainedTypeID(VecTypeID);
4724  InstructionList.push_back(I);
4725  break;
4726  }
4727 
4728  case bitc::FUNC_CODE_INST_INSERTELT: { // INSERTELT: [ty, opval,opval,opval]
4729  unsigned OpNum = 0;
4730  Value *Vec, *Elt, *Idx;
4731  unsigned VecTypeID, IdxTypeID;
4732  if (getValueTypePair(Record, OpNum, NextValueNo, Vec, VecTypeID))
4733  return error("Invalid record");
4734  if (!Vec->getType()->isVectorTy())
4735  return error("Invalid type for value");
4736  if (popValue(Record, OpNum, NextValueNo,
4737  cast<VectorType>(Vec->getType())->getElementType(),
4738  getContainedTypeID(VecTypeID), Elt) ||
4739  getValueTypePair(Record, OpNum, NextValueNo, Idx, IdxTypeID))
4740  return error("Invalid record");
4741  I = InsertElementInst::Create(Vec, Elt, Idx);
4742  ResTypeID = VecTypeID;
4743  InstructionList.push_back(I);
4744  break;
4745  }
4746 
4747  case bitc::FUNC_CODE_INST_SHUFFLEVEC: {// SHUFFLEVEC: [opval,ty,opval,opval]
4748  unsigned OpNum = 0;
4749  Value *Vec1, *Vec2, *Mask;
4750  unsigned Vec1TypeID;
4751  if (getValueTypePair(Record, OpNum, NextValueNo, Vec1, Vec1TypeID) ||
4752  popValue(Record, OpNum, NextValueNo, Vec1->getType(), Vec1TypeID,
4753  Vec2))
4754  return error("Invalid record");
4755 
4756  unsigned MaskTypeID;
4757  if (getValueTypePair(Record, OpNum, NextValueNo, Mask, MaskTypeID))
4758  return error("Invalid record");
4759  if (!Vec1->getType()->isVectorTy() || !Vec2->getType()->isVectorTy())
4760  return error("Invalid type for value");
4761 
4762  I = new ShuffleVectorInst(Vec1, Vec2, Mask);
4763  ResTypeID =
4764  getVirtualTypeID(I->getType(), getContainedTypeID(Vec1TypeID));
4765  InstructionList.push_back(I);
4766  break;
4767  }
4768 
4769  case bitc::FUNC_CODE_INST_CMP: // CMP: [opty, opval, opval, pred]
4770  // Old form of ICmp/FCmp returning bool
4771  // Existed to differentiate between icmp/fcmp and vicmp/vfcmp which were
4772  // both legal on vectors but had different behaviour.
4773  case bitc::FUNC_CODE_INST_CMP2: { // CMP2: [opty, opval, opval, pred]
4774  // FCmp/ICmp returning bool or vector of bool
4775 
4776  unsigned OpNum = 0;
4777  Value *LHS, *RHS;
4778  unsigned LHSTypeID;
4779  if (getValueTypePair(Record, OpNum, NextValueNo, LHS, LHSTypeID) ||
4780  popValue(Record, OpNum, NextValueNo, LHS->getType(), LHSTypeID, RHS))
4781  return error("Invalid record");
4782 
4783  if (OpNum >= Record.size())
4784  return error(
4785  "Invalid record: operand number exceeded available operands");
4786 
4787  unsigned PredVal = Record[OpNum];
4788  bool IsFP = LHS->getType()->isFPOrFPVectorTy();
4789  FastMathFlags FMF;
4790  if (IsFP && Record.size() > OpNum+1)
4791  FMF = getDecodedFastMathFlags(Record[++OpNum]);
4792 
4793  if (OpNum+1 != Record.size())
4794  return error("Invalid record");
4795 
4796  if (LHS->getType()->isFPOrFPVectorTy())
4797  I = new FCmpInst((FCmpInst::Predicate)PredVal, LHS, RHS);
4798  else
4799  I = new ICmpInst((ICmpInst::Predicate)PredVal, LHS, RHS);
4800 
4801  ResTypeID = getVirtualTypeID(I->getType()->getScalarType());
4802  if (LHS->getType()->isVectorTy())
4803  ResTypeID = getVirtualTypeID(I->getType(), ResTypeID);
4804 
4805  if (FMF.any())
4806  I->setFastMathFlags(FMF);
4807  InstructionList.push_back(I);
4808  break;
4809  }
4810 
4811  case bitc::FUNC_CODE_INST_RET: // RET: [opty,opval<optional>]
4812  {
4813  unsigned Size = Record.size();
4814  if (Size == 0) {
4815  I = ReturnInst::Create(Context);
4816  InstructionList.push_back(I);
4817  break;
4818  }
4819 
4820  unsigned OpNum = 0;
4821  Value *Op = nullptr;
4822  unsigned OpTypeID;
4823  if (getValueTypePair(Record, OpNum, NextValueNo, Op, OpTypeID))
4824  return error("Invalid record");
4825  if (OpNum != Record.size())
4826  return error("Invalid record");
4827 
4828  I = ReturnInst::Create(Context, Op);
4829  InstructionList.push_back(I);
4830  break;
4831  }
4832  case bitc::FUNC_CODE_INST_BR: { // BR: [bb#, bb#, opval] or [bb#]
4833  if (Record.size() != 1 && Record.size() != 3)
4834  return error("Invalid record");
4835  BasicBlock *TrueDest = getBasicBlock(Record[0]);
4836  if (!TrueDest)
4837  return error("Invalid record");
4838 
4839  if (Record.size() == 1) {
4840  I = BranchInst::Create(TrueDest);
4841  InstructionList.push_back(I);
4842  }
4843  else {
4844  BasicBlock *FalseDest = getBasicBlock(Record[1]);
4845  Type *CondType = Type::getInt1Ty(Context);
4846  Value *Cond = getValue(Record, 2, NextValueNo, CondType,
4847  getVirtualTypeID(CondType));
4848  if (!FalseDest || !Cond)
4849  return error("Invalid record");
4850  I = BranchInst::Create(TrueDest, FalseDest, Cond);
4851  InstructionList.push_back(I);
4852  }
4853  break;
4854  }
4855  case bitc::FUNC_CODE_INST_CLEANUPRET: { // CLEANUPRET: [val] or [val,bb#]
4856  if (Record.size() != 1 && Record.size() != 2)
4857  return error("Invalid record");
4858  unsigned Idx = 0;
4859  Type *TokenTy = Type::getTokenTy(Context);
4860  Value *CleanupPad = getValue(Record, Idx++, NextValueNo, TokenTy,
4861  getVirtualTypeID(TokenTy));
4862  if (!CleanupPad)
4863  return error("Invalid record");
4864  BasicBlock *UnwindDest = nullptr;
4865  if (Record.size() == 2) {
4866  UnwindDest = getBasicBlock(Record[Idx++]);
4867  if (!UnwindDest)
4868  return error("Invalid record");
4869  }
4870 
4871  I = CleanupReturnInst::Create(CleanupPad, UnwindDest);
4872  InstructionList.push_back(I);
4873  break;
4874  }
4875  case bitc::FUNC_CODE_INST_CATCHRET: { // CATCHRET: [val,bb#]
4876  if (Record.size() != 2)
4877  return error("Invalid record");
4878  unsigned Idx = 0;
4879  Type *TokenTy = Type::getTokenTy(Context);
4880  Value *CatchPad = getValue(Record, Idx++, NextValueNo, TokenTy,
4881  getVirtualTypeID(TokenTy));
4882  if (!CatchPad)
4883  return error("Invalid record");
4884  BasicBlock *BB = getBasicBlock(Record[Idx++]);
4885  if (!BB)
4886  return error("Invalid record");
4887 
4888  I = CatchReturnInst::Create(CatchPad, BB);
4889  InstructionList.push_back(I);
4890  break;
4891  }
4892  case bitc::FUNC_CODE_INST_CATCHSWITCH: { // CATCHSWITCH: [tok,num,(bb)*,bb?]
4893  // We must have, at minimum, the outer scope and the number of arguments.
4894  if (Record.size() < 2)
4895  return error("Invalid record");
4896 
4897  unsigned Idx = 0;
4898 
4899  Type *TokenTy = Type::getTokenTy(Context);
4900  Value *ParentPad = getValue(Record, Idx++, NextValueNo, TokenTy,
4901  getVirtualTypeID(TokenTy));
4902 
4903  unsigned NumHandlers = Record[Idx++];
4904 
4906  for (unsigned Op = 0; Op != NumHandlers; ++Op) {
4907  BasicBlock *BB = getBasicBlock(Record[Idx++]);
4908  if (!BB)
4909  return error("Invalid record");
4910  Handlers.push_back(BB);
4911  }
4912 
4913  BasicBlock *UnwindDest = nullptr;
4914  if (Idx + 1 == Record.size()) {
4915  UnwindDest = getBasicBlock(Record[Idx++]);
4916  if (!UnwindDest)
4917  return error("Invalid record");
4918  }
4919 
4920  if (Record.size() != Idx)
4921  return error("Invalid record");
4922 
4923  auto *CatchSwitch =
4924  CatchSwitchInst::Create(ParentPad, UnwindDest, NumHandlers);
4925  for (BasicBlock *Handler : Handlers)
4926  CatchSwitch->addHandler(Handler);
4927  I = CatchSwitch;
4928  ResTypeID = getVirtualTypeID(I->getType());
4929  InstructionList.push_back(I);
4930  break;
4931  }
4933  case bitc::FUNC_CODE_INST_CLEANUPPAD: { // [tok,num,(ty,val)*]
4934  // We must have, at minimum, the outer scope and the number of arguments.
4935  if (Record.size() < 2)
4936  return error("Invalid record");
4937 
4938  unsigned Idx = 0;
4939 
4940  Type *TokenTy = Type::getTokenTy(Context);
4941  Value *ParentPad = getValue(Record, Idx++, NextValueNo, TokenTy,
4942  getVirtualTypeID(TokenTy));
4943 
4944  unsigned NumArgOperands = Record[Idx++];
4945 
4947  for (unsigned Op = 0; Op != NumArgOperands; ++Op) {
4948  Value *Val;
4949  unsigned ValTypeID;
4950  if (getValueTypePair(Record, Idx, NextValueNo, Val, ValTypeID))
4951  return error("Invalid record");
4952  Args.push_back(Val);
4953  }
4954 
4955  if (Record.size() != Idx)
4956  return error("Invalid record");
4957 
4958  if (BitCode == bitc::FUNC_CODE_INST_CLEANUPPAD)
4959  I = CleanupPadInst::Create(ParentPad, Args);
4960  else
4961  I = CatchPadInst::Create(ParentPad, Args);
4962  ResTypeID = getVirtualTypeID(I->getType());
4963  InstructionList.push_back(I);
4964  break;
4965  }
4966  case bitc::FUNC_CODE_INST_SWITCH: { // SWITCH: [opty, op0, op1, ...]
4967  // Check magic
4968  if ((Record[0] >> 16) == SWITCH_INST_MAGIC) {
4969  // "New" SwitchInst format with case ranges. The changes to write this
4970  // format were reverted but we still recognize bitcode that uses it.
4971  // Hopefully someday we will have support for case ranges and can use
4972  // this format again.
4973 
4974  unsigned OpTyID = Record[1];
4975  Type *OpTy = getTypeByID(OpTyID);
4976  unsigned ValueBitWidth = cast<IntegerType>(OpTy)->getBitWidth();
4977 
4978  Value *Cond = getValue(Record, 2, NextValueNo, OpTy, OpTyID);
4979  BasicBlock *Default = getBasicBlock(Record[3]);
4980  if (!OpTy || !Cond || !Default)
4981  return error("Invalid record");
4982 
4983  unsigned NumCases = Record[4];
4984 
4985  SwitchInst *SI = SwitchInst::Create(Cond, Default, NumCases);
4986  InstructionList.push_back(SI);
4987 
4988  unsigned CurIdx = 5;
4989  for (unsigned i = 0; i != NumCases; ++i) {
4991  unsigned NumItems = Record[CurIdx++];
4992  for (unsigned ci = 0; ci != NumItems; ++ci) {
4993  bool isSingleNumber = Record[CurIdx++];
4994 
4995  APInt Low;
4996  unsigned ActiveWords = 1;
4997  if (ValueBitWidth > 64)
4998  ActiveWords = Record[CurIdx++];
4999  Low = readWideAPInt(makeArrayRef(&Record[CurIdx], ActiveWords),
5000  ValueBitWidth);
5001  CurIdx += ActiveWords;
5002 
5003  if (!isSingleNumber) {
5004  ActiveWords = 1;
5005  if (ValueBitWidth > 64)
5006  ActiveWords = Record[CurIdx++];
5008  makeArrayRef(&Record[CurIdx], ActiveWords), ValueBitWidth);
5009  CurIdx += ActiveWords;
5010 
5011  // FIXME: It is not clear whether values in the range should be
5012  // compared as signed or unsigned values. The partially
5013  // implemented changes that used this format in the past used
5014  // unsigned comparisons.
5015  for ( ; Low.ule(High); ++Low)
5016  CaseVals.push_back(ConstantInt::get(Context, Low));
5017  } else
5018  CaseVals.push_back(ConstantInt::get(Context, Low));
5019  }
5020  BasicBlock *DestBB = getBasicBlock(Record[CurIdx++]);
5021  for (ConstantInt *Cst : CaseVals)
5022  SI->addCase(Cst, DestBB);
5023  }
5024  I = SI;
5025  break;
5026  }
5027 
5028  // Old SwitchInst format without case ranges.
5029 
5030  if (Record.size() < 3 || (Record.size() & 1) == 0)
5031  return error("Invalid record");
5032  unsigned OpTyID = Record[0];
5033  Type *OpTy = getTypeByID(OpTyID);
5034  Value *Cond = getValue(Record, 1, NextValueNo, OpTy, OpTyID);
5035  BasicBlock *Default = getBasicBlock(Record[2]);
5036  if (!OpTy || !Cond || !Default)
5037  return error("Invalid record");
5038  unsigned NumCases = (Record.size()-3)/2;
5039  SwitchInst *SI = SwitchInst::Create(Cond, Default, NumCases);
5040  InstructionList.push_back(SI);
5041  for (unsigned i = 0, e = NumCases; i != e; ++i) {
5042  ConstantInt *CaseVal = dyn_cast_or_null<ConstantInt>(
5043  getFnValueByID(Record[3+i*2], OpTy, OpTyID));
5044  BasicBlock *DestBB = getBasicBlock(Record[1+3+i*2]);
5045  if (!CaseVal || !DestBB) {
5046  delete SI;
5047  return error("Invalid record");
5048  }
5049  SI->addCase(CaseVal, DestBB);
5050  }
5051  I = SI;
5052  break;
5053  }
5054  case bitc::FUNC_CODE_INST_INDIRECTBR: { // INDIRECTBR: [opty, op0, op1, ...]
5055  if (Record.size() < 2)
5056  return error("Invalid record");
5057  unsigned OpTyID = Record[0];
5058  Type *OpTy = getTypeByID(OpTyID);
5059  Value *Address = getValue(Record, 1, NextValueNo, OpTy, OpTyID);
5060  if (!OpTy || !Address)
5061  return error("Invalid record");
5062  unsigned NumDests = Record.size()-2;
5063  IndirectBrInst *IBI = IndirectBrInst::Create(Address, NumDests);
5064  InstructionList.push_back(IBI);
5065  for (unsigned i = 0, e = NumDests; i != e; ++i) {
5066  if (BasicBlock *DestBB = getBasicBlock(Record[2+i])) {
5067  IBI->addDestination(DestBB);
5068  } else {
5069  delete IBI;
5070  return error("Invalid record");
5071  }
5072  }
5073  I = IBI;
5074  break;
5075  }
5076 
5078  // INVOKE: [attrs, cc, normBB, unwindBB, fnty, op0,op1,op2, ...]
5079  if (Record.size() < 4)
5080  return error("Invalid record");
5081  unsigned OpNum = 0;
5082  AttributeList PAL = getAttributes(Record[OpNum++]);
5083  unsigned CCInfo = Record[OpNum++];
5084  BasicBlock *NormalBB = getBasicBlock(Record[OpNum++]);
5085  BasicBlock *UnwindBB = getBasicBlock(Record[OpNum++]);
5086 
5087  unsigned FTyID = InvalidTypeID;
5088  FunctionType *FTy = nullptr;
5089  if ((CCInfo >> 13) & 1) {
5090  FTyID = Record[OpNum++];
5091  FTy = dyn_cast<FunctionType>(getTypeByID(FTyID));
5092  if (!FTy)
5093  return error("Explicit invoke type is not a function type");
5094  }
5095 
5096  Value *Callee;
5097  unsigned CalleeTypeID;
5098  if (getValueTypePair(Record, OpNum, NextValueNo, Callee, CalleeTypeID))
5099  return error("Invalid record");
5100 
5101  PointerType *CalleeTy = dyn_cast<PointerType>(Callee->getType());
5102  if (!CalleeTy)
5103  return error("Callee is not a pointer");
5104  if (!FTy) {
5105  FTyID = getContainedTypeID(CalleeTypeID);
5106  FTy = dyn_cast_or_null<FunctionType>(getTypeByID(FTyID));
5107  if (!FTy)
5108  return error("Callee is not of pointer to function type");
5109  } else if (!CalleeTy->isOpaqueOrPointeeTypeMatches(FTy))
5110  return error("Explicit invoke type does not match pointee type of "
5111  "callee operand");
5112  if (Record.size() < FTy->getNumParams() + OpNum)
5113  return error("Insufficient operands to call");
5114 
5116  SmallVector<unsigned, 16> ArgTyIDs;
5117  <