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