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