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