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