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