8803 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9#include "llvm/Bitcode/BitcodeReader.h"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/STLExtras.h"17#include "llvm/ADT/SmallString.h"18#include "llvm/ADT/SmallVector.h"19#include "llvm/ADT/StringRef.h"20#include "llvm/ADT/Twine.h"21#include "llvm/Bitcode/BitcodeCommon.h"22#include "llvm/Bitcode/LLVMBitCodes.h"23#include "llvm/Bitstream/BitstreamReader.h"24#include "llvm/Config/llvm-config.h"25#include "llvm/IR/Argument.h"26#include "llvm/IR/AttributeMask.h"27#include "llvm/IR/Attributes.h"28#include "llvm/IR/AutoUpgrade.h"29#include "llvm/IR/BasicBlock.h"30#include "llvm/IR/CallingConv.h"31#include "llvm/IR/Comdat.h"32#include "llvm/IR/Constant.h"33#include "llvm/IR/ConstantRangeList.h"34#include "llvm/IR/Constants.h"35#include "llvm/IR/DataLayout.h"36#include "llvm/IR/DebugInfo.h"37#include "llvm/IR/DebugInfoMetadata.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/GetElementPtrTypeIterator.h"43#include "llvm/IR/GlobalAlias.h"44#include "llvm/IR/GlobalIFunc.h"45#include "llvm/IR/GlobalObject.h"46#include "llvm/IR/GlobalValue.h"47#include "llvm/IR/GlobalVariable.h"48#include "llvm/IR/InlineAsm.h"49#include "llvm/IR/InstIterator.h"50#include "llvm/IR/InstrTypes.h"51#include "llvm/IR/Instruction.h"52#include "llvm/IR/Instructions.h"53#include "llvm/IR/Intrinsics.h"54#include "llvm/IR/IntrinsicsAArch64.h"55#include "llvm/IR/IntrinsicsARM.h"56#include "llvm/IR/LLVMContext.h"57#include "llvm/IR/Metadata.h"58#include "llvm/IR/Module.h"59#include "llvm/IR/ModuleSummaryIndex.h"60#include "llvm/IR/Operator.h"61#include "llvm/IR/ProfDataUtils.h"62#include "llvm/IR/Type.h"63#include "llvm/IR/Value.h"64#include "llvm/IR/Verifier.h"65#include "llvm/Support/AtomicOrdering.h"66#include "llvm/Support/Casting.h"67#include "llvm/Support/CommandLine.h"68#include "llvm/Support/Compiler.h"69#include "llvm/Support/Debug.h"70#include "llvm/Support/Error.h"71#include "llvm/Support/ErrorHandling.h"72#include "llvm/Support/ErrorOr.h"73#include "llvm/Support/MathExtras.h"74#include "llvm/Support/MemoryBuffer.h"75#include "llvm/Support/ModRef.h"76#include "llvm/Support/raw_ostream.h"77#include "llvm/TargetParser/Triple.h"78#include <algorithm>79#include <cassert>80#include <cstddef>81#include <cstdint>82#include <deque>83#include <map>84#include <memory>85#include <optional>86#include <string>87#include <system_error>88#include <tuple>89#include <utility>90#include <vector>91 92using namespace llvm;93 94static cl::opt<bool> PrintSummaryGUIDs(95 "print-summary-global-ids", cl::init(false), cl::Hidden,96 cl::desc(97 "Print the global id for each value when reading the module summary"));98 99static cl::opt<bool> ExpandConstantExprs(100 "expand-constant-exprs", cl::Hidden,101 cl::desc(102 "Expand constant expressions to instructions for testing purposes"));103 104namespace {105 106enum {107 SWITCH_INST_MAGIC = 0x4B5 // May 2012 => 1205 => Hex108};109 110} // end anonymous namespace111 112static Error error(const Twine &Message) {113 return make_error<StringError>(114 Message, make_error_code(BitcodeError::CorruptedBitcode));115}116 117static Error hasInvalidBitcodeHeader(BitstreamCursor &Stream) {118 if (!Stream.canSkipToPos(4))119 return createStringError(std::errc::illegal_byte_sequence,120 "file too small to contain bitcode header");121 for (unsigned C : {'B', 'C'})122 if (Expected<SimpleBitstreamCursor::word_t> Res = Stream.Read(8)) {123 if (Res.get() != C)124 return createStringError(std::errc::illegal_byte_sequence,125 "file doesn't start with bitcode header");126 } else127 return Res.takeError();128 for (unsigned C : {0x0, 0xC, 0xE, 0xD})129 if (Expected<SimpleBitstreamCursor::word_t> Res = Stream.Read(4)) {130 if (Res.get() != C)131 return createStringError(std::errc::illegal_byte_sequence,132 "file doesn't start with bitcode header");133 } else134 return Res.takeError();135 return Error::success();136}137 138static Expected<BitstreamCursor> initStream(MemoryBufferRef Buffer) {139 const unsigned char *BufPtr = (const unsigned char *)Buffer.getBufferStart();140 const unsigned char *BufEnd = BufPtr + Buffer.getBufferSize();141 142 if (Buffer.getBufferSize() & 3)143 return error("Invalid bitcode signature");144 145 // If we have a wrapper header, parse it and ignore the non-bc file contents.146 // The magic number is 0x0B17C0DE stored in little endian.147 if (isBitcodeWrapper(BufPtr, BufEnd))148 if (SkipBitcodeWrapperHeader(BufPtr, BufEnd, true))149 return error("Invalid bitcode wrapper header");150 151 BitstreamCursor Stream(ArrayRef<uint8_t>(BufPtr, BufEnd));152 if (Error Err = hasInvalidBitcodeHeader(Stream))153 return std::move(Err);154 155 return std::move(Stream);156}157 158/// Convert a string from a record into an std::string, return true on failure.159template <typename StrTy>160static bool convertToString(ArrayRef<uint64_t> Record, unsigned Idx,161 StrTy &Result) {162 if (Idx > Record.size())163 return true;164 165 Result.append(Record.begin() + Idx, Record.end());166 return false;167}168 169// Strip all the TBAA attachment for the module.170static void stripTBAA(Module *M) {171 for (auto &F : *M) {172 if (F.isMaterializable())173 continue;174 for (auto &I : instructions(F))175 I.setMetadata(LLVMContext::MD_tbaa, nullptr);176 }177}178 179/// Read the "IDENTIFICATION_BLOCK_ID" block, do some basic enforcement on the180/// "epoch" encoded in the bitcode, and return the producer name if any.181static Expected<std::string> readIdentificationBlock(BitstreamCursor &Stream) {182 if (Error Err = Stream.EnterSubBlock(bitc::IDENTIFICATION_BLOCK_ID))183 return std::move(Err);184 185 // Read all the records.186 SmallVector<uint64_t, 64> Record;187 188 std::string ProducerIdentification;189 190 while (true) {191 BitstreamEntry Entry;192 if (Error E = Stream.advance().moveInto(Entry))193 return std::move(E);194 195 switch (Entry.Kind) {196 default:197 case BitstreamEntry::Error:198 return error("Malformed block");199 case BitstreamEntry::EndBlock:200 return ProducerIdentification;201 case BitstreamEntry::Record:202 // The interesting case.203 break;204 }205 206 // Read a record.207 Record.clear();208 Expected<unsigned> MaybeBitCode = Stream.readRecord(Entry.ID, Record);209 if (!MaybeBitCode)210 return MaybeBitCode.takeError();211 switch (MaybeBitCode.get()) {212 default: // Default behavior: reject213 return error("Invalid value");214 case bitc::IDENTIFICATION_CODE_STRING: // IDENTIFICATION: [strchr x N]215 convertToString(Record, 0, ProducerIdentification);216 break;217 case bitc::IDENTIFICATION_CODE_EPOCH: { // EPOCH: [epoch#]218 unsigned epoch = (unsigned)Record[0];219 if (epoch != bitc::BITCODE_CURRENT_EPOCH) {220 return error(221 Twine("Incompatible epoch: Bitcode '") + Twine(epoch) +222 "' vs current: '" + Twine(bitc::BITCODE_CURRENT_EPOCH) + "'");223 }224 }225 }226 }227}228 229static Expected<std::string> readIdentificationCode(BitstreamCursor &Stream) {230 // We expect a number of well-defined blocks, though we don't necessarily231 // need to understand them all.232 while (true) {233 if (Stream.AtEndOfStream())234 return "";235 236 BitstreamEntry Entry;237 if (Error E = Stream.advance().moveInto(Entry))238 return std::move(E);239 240 switch (Entry.Kind) {241 case BitstreamEntry::EndBlock:242 case BitstreamEntry::Error:243 return error("Malformed block");244 245 case BitstreamEntry::SubBlock:246 if (Entry.ID == bitc::IDENTIFICATION_BLOCK_ID)247 return readIdentificationBlock(Stream);248 249 // Ignore other sub-blocks.250 if (Error Err = Stream.SkipBlock())251 return std::move(Err);252 continue;253 case BitstreamEntry::Record:254 if (Error E = Stream.skipRecord(Entry.ID).takeError())255 return std::move(E);256 continue;257 }258 }259}260 261static Expected<bool> hasObjCCategoryInModule(BitstreamCursor &Stream) {262 if (Error Err = Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID))263 return std::move(Err);264 265 SmallVector<uint64_t, 64> Record;266 // Read all the records for this module.267 268 while (true) {269 Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks();270 if (!MaybeEntry)271 return MaybeEntry.takeError();272 BitstreamEntry Entry = MaybeEntry.get();273 274 switch (Entry.Kind) {275 case BitstreamEntry::SubBlock: // Handled for us already.276 case BitstreamEntry::Error:277 return error("Malformed block");278 case BitstreamEntry::EndBlock:279 return false;280 case BitstreamEntry::Record:281 // The interesting case.282 break;283 }284 285 // Read a record.286 Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record);287 if (!MaybeRecord)288 return MaybeRecord.takeError();289 switch (MaybeRecord.get()) {290 default:291 break; // Default behavior, ignore unknown content.292 case bitc::MODULE_CODE_SECTIONNAME: { // SECTIONNAME: [strchr x N]293 std::string S;294 if (convertToString(Record, 0, S))295 return error("Invalid section name record");296 297 // Check for the i386 and other (x86_64, ARM) conventions298 299 auto [Segment, Section] = StringRef(S).split(",");300 Segment = Segment.trim();301 Section = Section.trim();302 303 if (Segment == "__DATA" && Section.starts_with("__objc_catlist"))304 return true;305 if (Segment == "__OBJC" && Section.starts_with("__category"))306 return true;307 if (Segment == "__TEXT" && Section.starts_with("__swift"))308 return true;309 break;310 }311 }312 Record.clear();313 }314 llvm_unreachable("Exit infinite loop");315}316 317static Expected<bool> hasObjCCategory(BitstreamCursor &Stream) {318 // We expect a number of well-defined blocks, though we don't necessarily319 // need to understand them all.320 while (true) {321 BitstreamEntry Entry;322 if (Error E = Stream.advance().moveInto(Entry))323 return std::move(E);324 325 switch (Entry.Kind) {326 case BitstreamEntry::Error:327 return error("Malformed block");328 case BitstreamEntry::EndBlock:329 return false;330 331 case BitstreamEntry::SubBlock:332 if (Entry.ID == bitc::MODULE_BLOCK_ID)333 return hasObjCCategoryInModule(Stream);334 335 // Ignore other sub-blocks.336 if (Error Err = Stream.SkipBlock())337 return std::move(Err);338 continue;339 340 case BitstreamEntry::Record:341 if (Error E = Stream.skipRecord(Entry.ID).takeError())342 return std::move(E);343 continue;344 }345 }346}347 348static Expected<std::string> readModuleTriple(BitstreamCursor &Stream) {349 if (Error Err = Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID))350 return std::move(Err);351 352 SmallVector<uint64_t, 64> Record;353 354 std::string Triple;355 356 // Read all the records for this module.357 while (true) {358 Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks();359 if (!MaybeEntry)360 return MaybeEntry.takeError();361 BitstreamEntry Entry = MaybeEntry.get();362 363 switch (Entry.Kind) {364 case BitstreamEntry::SubBlock: // Handled for us already.365 case BitstreamEntry::Error:366 return error("Malformed block");367 case BitstreamEntry::EndBlock:368 return Triple;369 case BitstreamEntry::Record:370 // The interesting case.371 break;372 }373 374 // Read a record.375 Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record);376 if (!MaybeRecord)377 return MaybeRecord.takeError();378 switch (MaybeRecord.get()) {379 default: break; // Default behavior, ignore unknown content.380 case bitc::MODULE_CODE_TRIPLE: { // TRIPLE: [strchr x N]381 std::string S;382 if (convertToString(Record, 0, S))383 return error("Invalid triple record");384 Triple = S;385 break;386 }387 }388 Record.clear();389 }390 llvm_unreachable("Exit infinite loop");391}392 393static Expected<std::string> readTriple(BitstreamCursor &Stream) {394 // We expect a number of well-defined blocks, though we don't necessarily395 // need to understand them all.396 while (true) {397 Expected<BitstreamEntry> MaybeEntry = Stream.advance();398 if (!MaybeEntry)399 return MaybeEntry.takeError();400 BitstreamEntry Entry = MaybeEntry.get();401 402 switch (Entry.Kind) {403 case BitstreamEntry::Error:404 return error("Malformed block");405 case BitstreamEntry::EndBlock:406 return "";407 408 case BitstreamEntry::SubBlock:409 if (Entry.ID == bitc::MODULE_BLOCK_ID)410 return readModuleTriple(Stream);411 412 // Ignore other sub-blocks.413 if (Error Err = Stream.SkipBlock())414 return std::move(Err);415 continue;416 417 case BitstreamEntry::Record:418 if (llvm::Expected<unsigned> Skipped = Stream.skipRecord(Entry.ID))419 continue;420 else421 return Skipped.takeError();422 }423 }424}425 426namespace {427 428class BitcodeReaderBase {429protected:430 BitcodeReaderBase(BitstreamCursor Stream, StringRef Strtab)431 : Stream(std::move(Stream)), Strtab(Strtab) {432 this->Stream.setBlockInfo(&BlockInfo);433 }434 435 BitstreamBlockInfo BlockInfo;436 BitstreamCursor Stream;437 StringRef Strtab;438 439 /// In version 2 of the bitcode we store names of global values and comdats in440 /// a string table rather than in the VST.441 bool UseStrtab = false;442 443 Expected<unsigned> parseVersionRecord(ArrayRef<uint64_t> Record);444 445 /// If this module uses a string table, pop the reference to the string table446 /// and return the referenced string and the rest of the record. Otherwise447 /// just return the record itself.448 std::pair<StringRef, ArrayRef<uint64_t>>449 readNameFromStrtab(ArrayRef<uint64_t> Record);450 451 Error readBlockInfo();452 453 // Contains an arbitrary and optional string identifying the bitcode producer454 std::string ProducerIdentification;455 456 Error error(const Twine &Message);457};458 459} // end anonymous namespace460 461Error BitcodeReaderBase::error(const Twine &Message) {462 std::string FullMsg = Message.str();463 if (!ProducerIdentification.empty())464 FullMsg += " (Producer: '" + ProducerIdentification + "' Reader: 'LLVM " +465 LLVM_VERSION_STRING "')";466 return ::error(FullMsg);467}468 469Expected<unsigned>470BitcodeReaderBase::parseVersionRecord(ArrayRef<uint64_t> Record) {471 if (Record.empty())472 return error("Invalid version record");473 unsigned ModuleVersion = Record[0];474 if (ModuleVersion > 2)475 return error("Invalid value");476 UseStrtab = ModuleVersion >= 2;477 return ModuleVersion;478}479 480std::pair<StringRef, ArrayRef<uint64_t>>481BitcodeReaderBase::readNameFromStrtab(ArrayRef<uint64_t> Record) {482 if (!UseStrtab)483 return {"", Record};484 // Invalid reference. Let the caller complain about the record being empty.485 if (Record[0] + Record[1] > Strtab.size())486 return {"", {}};487 return {StringRef(Strtab.data() + Record[0], Record[1]), Record.slice(2)};488}489 490namespace {491 492/// This represents a constant expression or constant aggregate using a custom493/// structure internal to the bitcode reader. Later, this structure will be494/// expanded by materializeValue() either into a constant expression/aggregate,495/// or into an instruction sequence at the point of use. This allows us to496/// upgrade bitcode using constant expressions even if this kind of constant497/// expression is no longer supported.498class BitcodeConstant final : public Value,499 TrailingObjects<BitcodeConstant, unsigned> {500 friend TrailingObjects;501 502 // Value subclass ID: Pick largest possible value to avoid any clashes.503 static constexpr uint8_t SubclassID = 255;504 505public:506 // Opcodes used for non-expressions. This includes constant aggregates507 // (struct, array, vector) that might need expansion, as well as non-leaf508 // constants that don't need expansion (no_cfi, dso_local, blockaddress),509 // but still go through BitcodeConstant to avoid different uselist orders510 // between the two cases.511 static constexpr uint8_t ConstantStructOpcode = 255;512 static constexpr uint8_t ConstantArrayOpcode = 254;513 static constexpr uint8_t ConstantVectorOpcode = 253;514 static constexpr uint8_t NoCFIOpcode = 252;515 static constexpr uint8_t DSOLocalEquivalentOpcode = 251;516 static constexpr uint8_t BlockAddressOpcode = 250;517 static constexpr uint8_t ConstantPtrAuthOpcode = 249;518 static constexpr uint8_t FirstSpecialOpcode = ConstantPtrAuthOpcode;519 520 // Separate struct to make passing different number of parameters to521 // BitcodeConstant::create() more convenient.522 struct ExtraInfo {523 uint8_t Opcode;524 uint8_t Flags;525 unsigned BlockAddressBB = 0;526 Type *SrcElemTy = nullptr;527 std::optional<ConstantRange> InRange;528 529 ExtraInfo(uint8_t Opcode, uint8_t Flags = 0, Type *SrcElemTy = nullptr,530 std::optional<ConstantRange> InRange = std::nullopt)531 : Opcode(Opcode), Flags(Flags), SrcElemTy(SrcElemTy),532 InRange(std::move(InRange)) {}533 534 ExtraInfo(uint8_t Opcode, uint8_t Flags, unsigned BlockAddressBB)535 : Opcode(Opcode), Flags(Flags), BlockAddressBB(BlockAddressBB) {}536 };537 538 uint8_t Opcode;539 uint8_t Flags;540 unsigned NumOperands;541 unsigned BlockAddressBB;542 Type *SrcElemTy; // GEP source element type.543 std::optional<ConstantRange> InRange; // GEP inrange attribute.544 545private:546 BitcodeConstant(Type *Ty, const ExtraInfo &Info, ArrayRef<unsigned> OpIDs)547 : Value(Ty, SubclassID), Opcode(Info.Opcode), Flags(Info.Flags),548 NumOperands(OpIDs.size()), BlockAddressBB(Info.BlockAddressBB),549 SrcElemTy(Info.SrcElemTy), InRange(Info.InRange) {550 llvm::uninitialized_copy(OpIDs, getTrailingObjects());551 }552 553 BitcodeConstant &operator=(const BitcodeConstant &) = delete;554 555public:556 static BitcodeConstant *create(BumpPtrAllocator &A, Type *Ty,557 const ExtraInfo &Info,558 ArrayRef<unsigned> OpIDs) {559 void *Mem = A.Allocate(totalSizeToAlloc<unsigned>(OpIDs.size()),560 alignof(BitcodeConstant));561 return new (Mem) BitcodeConstant(Ty, Info, OpIDs);562 }563 564 static bool classof(const Value *V) { return V->getValueID() == SubclassID; }565 566 ArrayRef<unsigned> getOperandIDs() const {567 return ArrayRef(getTrailingObjects(), NumOperands);568 }569 570 std::optional<ConstantRange> getInRange() const {571 assert(Opcode == Instruction::GetElementPtr);572 return InRange;573 }574 575 const char *getOpcodeName() const {576 return Instruction::getOpcodeName(Opcode);577 }578};579 580class BitcodeReader : public BitcodeReaderBase, public GVMaterializer {581 LLVMContext &Context;582 Module *TheModule = nullptr;583 // Next offset to start scanning for lazy parsing of function bodies.584 uint64_t NextUnreadBit = 0;585 // Last function offset found in the VST.586 uint64_t LastFunctionBlockBit = 0;587 bool SeenValueSymbolTable = false;588 uint64_t VSTOffset = 0;589 590 std::vector<std::string> SectionTable;591 std::vector<std::string> GCTable;592 593 std::vector<Type *> TypeList;594 /// Track type IDs of contained types. Order is the same as the contained595 /// types of a Type*. This is used during upgrades of typed pointer IR in596 /// opaque pointer mode.597 DenseMap<unsigned, SmallVector<unsigned, 1>> ContainedTypeIDs;598 /// In some cases, we need to create a type ID for a type that was not599 /// explicitly encoded in the bitcode, or we don't know about at the current600 /// point. For example, a global may explicitly encode the value type ID, but601 /// not have a type ID for the pointer to value type, for which we create a602 /// virtual type ID instead. This map stores the new type ID that was created603 /// for the given pair of Type and contained type ID.604 DenseMap<std::pair<Type *, unsigned>, unsigned> VirtualTypeIDs;605 DenseMap<Function *, unsigned> FunctionTypeIDs;606 /// Allocator for BitcodeConstants. This should come before ValueList,607 /// because the ValueList might hold ValueHandles to these constants, so608 /// ValueList must be destroyed before Alloc.609 BumpPtrAllocator Alloc;610 BitcodeReaderValueList ValueList;611 std::optional<MetadataLoader> MDLoader;612 std::vector<Comdat *> ComdatList;613 DenseSet<GlobalObject *> ImplicitComdatObjects;614 SmallVector<Instruction *, 64> InstructionList;615 616 std::vector<std::pair<GlobalVariable *, unsigned>> GlobalInits;617 std::vector<std::pair<GlobalValue *, unsigned>> IndirectSymbolInits;618 619 struct FunctionOperandInfo {620 Function *F;621 unsigned PersonalityFn;622 unsigned Prefix;623 unsigned Prologue;624 };625 std::vector<FunctionOperandInfo> FunctionOperands;626 627 /// The set of attributes by index. Index zero in the file is for null, and628 /// is thus not represented here. As such all indices are off by one.629 std::vector<AttributeList> MAttributes;630 631 /// The set of attribute groups.632 std::map<unsigned, AttributeList> MAttributeGroups;633 634 /// While parsing a function body, this is a list of the basic blocks for the635 /// function.636 std::vector<BasicBlock*> FunctionBBs;637 638 // When reading the module header, this list is populated with functions that639 // have bodies later in the file.640 std::vector<Function*> FunctionsWithBodies;641 642 // When intrinsic functions are encountered which require upgrading they are643 // stored here with their replacement function.644 using UpdatedIntrinsicMap = DenseMap<Function *, Function *>;645 UpdatedIntrinsicMap UpgradedIntrinsics;646 647 // Several operations happen after the module header has been read, but648 // before function bodies are processed. This keeps track of whether649 // we've done this yet.650 bool SeenFirstFunctionBody = false;651 652 /// When function bodies are initially scanned, this map contains info about653 /// where to find deferred function body in the stream.654 DenseMap<Function*, uint64_t> DeferredFunctionInfo;655 656 /// When Metadata block is initially scanned when parsing the module, we may657 /// choose to defer parsing of the metadata. This vector contains info about658 /// which Metadata blocks are deferred.659 std::vector<uint64_t> DeferredMetadataInfo;660 661 /// These are basic blocks forward-referenced by block addresses. They are662 /// inserted lazily into functions when they're loaded. The basic block ID is663 /// its index into the vector.664 DenseMap<Function *, std::vector<BasicBlock *>> BasicBlockFwdRefs;665 std::deque<Function *> BasicBlockFwdRefQueue;666 667 /// These are Functions that contain BlockAddresses which refer a different668 /// Function. When parsing the different Function, queue Functions that refer669 /// to the different Function. Those Functions must be materialized in order670 /// to resolve their BlockAddress constants before the different Function671 /// gets moved into another Module.672 std::vector<Function *> BackwardRefFunctions;673 674 /// Indicates that we are using a new encoding for instruction operands where675 /// most operands in the current FUNCTION_BLOCK are encoded relative to the676 /// instruction number, for a more compact encoding. Some instruction677 /// operands are not relative to the instruction ID: basic block numbers, and678 /// types. Once the old style function blocks have been phased out, we would679 /// not need this flag.680 bool UseRelativeIDs = false;681 682 /// True if all functions will be materialized, negating the need to process683 /// (e.g.) blockaddress forward references.684 bool WillMaterializeAllForwardRefs = false;685 686 /// Tracks whether we have seen debug intrinsics or records in this bitcode;687 /// seeing both in a single module is currently a fatal error.688 bool SeenDebugIntrinsic = false;689 bool SeenDebugRecord = false;690 691 bool StripDebugInfo = false;692 TBAAVerifier TBAAVerifyHelper;693 694 std::vector<std::string> BundleTags;695 SmallVector<SyncScope::ID, 8> SSIDs;696 697 std::optional<ValueTypeCallbackTy> ValueTypeCallback;698 699public:700 BitcodeReader(BitstreamCursor Stream, StringRef Strtab,701 StringRef ProducerIdentification, LLVMContext &Context);702 703 Error materializeForwardReferencedFunctions();704 705 Error materialize(GlobalValue *GV) override;706 Error materializeModule() override;707 std::vector<StructType *> getIdentifiedStructTypes() const override;708 709 /// Main interface to parsing a bitcode buffer.710 /// \returns true if an error occurred.711 Error parseBitcodeInto(Module *M, bool ShouldLazyLoadMetadata,712 bool IsImporting, ParserCallbacks Callbacks = {});713 714 static uint64_t decodeSignRotatedValue(uint64_t V);715 716 /// Materialize any deferred Metadata block.717 Error materializeMetadata() override;718 719 void setStripDebugInfo() override;720 721private:722 std::vector<StructType *> IdentifiedStructTypes;723 StructType *createIdentifiedStructType(LLVMContext &Context, StringRef Name);724 StructType *createIdentifiedStructType(LLVMContext &Context);725 726 static constexpr unsigned InvalidTypeID = ~0u;727 728 Type *getTypeByID(unsigned ID);729 Type *getPtrElementTypeByID(unsigned ID);730 unsigned getContainedTypeID(unsigned ID, unsigned Idx = 0);731 unsigned getVirtualTypeID(Type *Ty, ArrayRef<unsigned> ContainedTypeIDs = {});732 733 void callValueTypeCallback(Value *F, unsigned TypeID);734 Expected<Value *> materializeValue(unsigned ValID, BasicBlock *InsertBB);735 Expected<Constant *> getValueForInitializer(unsigned ID);736 737 Value *getFnValueByID(unsigned ID, Type *Ty, unsigned TyID,738 BasicBlock *ConstExprInsertBB) {739 if (Ty && Ty->isMetadataTy())740 return MetadataAsValue::get(Ty->getContext(), getFnMetadataByID(ID));741 return ValueList.getValueFwdRef(ID, Ty, TyID, ConstExprInsertBB);742 }743 744 Metadata *getFnMetadataByID(unsigned ID) {745 return MDLoader->getMetadataFwdRefOrLoad(ID);746 }747 748 BasicBlock *getBasicBlock(unsigned ID) const {749 if (ID >= FunctionBBs.size()) return nullptr; // Invalid ID750 return FunctionBBs[ID];751 }752 753 AttributeList getAttributes(unsigned i) const {754 if (i-1 < MAttributes.size())755 return MAttributes[i-1];756 return AttributeList();757 }758 759 /// Read a value/type pair out of the specified record from slot 'Slot'.760 /// Increment Slot past the number of slots used in the record. Return true on761 /// failure.762 bool getValueTypePair(const SmallVectorImpl<uint64_t> &Record, unsigned &Slot,763 unsigned InstNum, Value *&ResVal, unsigned &TypeID,764 BasicBlock *ConstExprInsertBB) {765 if (Slot == Record.size()) return true;766 unsigned ValNo = (unsigned)Record[Slot++];767 // Adjust the ValNo, if it was encoded relative to the InstNum.768 if (UseRelativeIDs)769 ValNo = InstNum - ValNo;770 if (ValNo < InstNum) {771 // If this is not a forward reference, just return the value we already772 // have.773 TypeID = ValueList.getTypeID(ValNo);774 ResVal = getFnValueByID(ValNo, nullptr, TypeID, ConstExprInsertBB);775 assert((!ResVal || ResVal->getType() == getTypeByID(TypeID)) &&776 "Incorrect type ID stored for value");777 return ResVal == nullptr;778 }779 if (Slot == Record.size())780 return true;781 782 TypeID = (unsigned)Record[Slot++];783 ResVal = getFnValueByID(ValNo, getTypeByID(TypeID), TypeID,784 ConstExprInsertBB);785 return ResVal == nullptr;786 }787 788 bool getValueOrMetadata(const SmallVectorImpl<uint64_t> &Record,789 unsigned &Slot, unsigned InstNum, Value *&ResVal,790 BasicBlock *ConstExprInsertBB) {791 if (Slot == Record.size())792 return true;793 unsigned ValID = Record[Slot++];794 if (ValID != static_cast<unsigned>(bitc::OB_METADATA)) {795 unsigned TypeId;796 return getValueTypePair(Record, --Slot, InstNum, ResVal, TypeId,797 ConstExprInsertBB);798 }799 if (Slot == Record.size())800 return true;801 unsigned ValNo = InstNum - (unsigned)Record[Slot++];802 ResVal = MetadataAsValue::get(Context, getFnMetadataByID(ValNo));803 return false;804 }805 806 /// Read a value out of the specified record from slot 'Slot'. Increment Slot807 /// past the number of slots used by the value in the record. Return true if808 /// there is an error.809 bool popValue(const SmallVectorImpl<uint64_t> &Record, unsigned &Slot,810 unsigned InstNum, Type *Ty, unsigned TyID, Value *&ResVal,811 BasicBlock *ConstExprInsertBB) {812 if (getValue(Record, Slot, InstNum, Ty, TyID, ResVal, ConstExprInsertBB))813 return true;814 // All values currently take a single record slot.815 ++Slot;816 return false;817 }818 819 /// Like popValue, but does not increment the Slot number.820 bool getValue(const SmallVectorImpl<uint64_t> &Record, unsigned Slot,821 unsigned InstNum, Type *Ty, unsigned TyID, Value *&ResVal,822 BasicBlock *ConstExprInsertBB) {823 ResVal = getValue(Record, Slot, InstNum, Ty, TyID, ConstExprInsertBB);824 return ResVal == nullptr;825 }826 827 /// Version of getValue that returns ResVal directly, or 0 if there is an828 /// error.829 Value *getValue(const SmallVectorImpl<uint64_t> &Record, unsigned Slot,830 unsigned InstNum, Type *Ty, unsigned TyID,831 BasicBlock *ConstExprInsertBB) {832 if (Slot == Record.size()) return nullptr;833 unsigned ValNo = (unsigned)Record[Slot];834 // Adjust the ValNo, if it was encoded relative to the InstNum.835 if (UseRelativeIDs)836 ValNo = InstNum - ValNo;837 return getFnValueByID(ValNo, Ty, TyID, ConstExprInsertBB);838 }839 840 /// Like getValue, but decodes signed VBRs.841 Value *getValueSigned(const SmallVectorImpl<uint64_t> &Record, unsigned Slot,842 unsigned InstNum, Type *Ty, unsigned TyID,843 BasicBlock *ConstExprInsertBB) {844 if (Slot == Record.size()) return nullptr;845 unsigned ValNo = (unsigned)decodeSignRotatedValue(Record[Slot]);846 // Adjust the ValNo, if it was encoded relative to the InstNum.847 if (UseRelativeIDs)848 ValNo = InstNum - ValNo;849 return getFnValueByID(ValNo, Ty, TyID, ConstExprInsertBB);850 }851 852 Expected<ConstantRange> readConstantRange(ArrayRef<uint64_t> Record,853 unsigned &OpNum,854 unsigned BitWidth) {855 if (Record.size() - OpNum < 2)856 return error("Too few records for range");857 if (BitWidth > 64) {858 unsigned LowerActiveWords = Record[OpNum];859 unsigned UpperActiveWords = Record[OpNum++] >> 32;860 if (Record.size() - OpNum < LowerActiveWords + UpperActiveWords)861 return error("Too few records for range");862 APInt Lower =863 readWideAPInt(ArrayRef(&Record[OpNum], LowerActiveWords), BitWidth);864 OpNum += LowerActiveWords;865 APInt Upper =866 readWideAPInt(ArrayRef(&Record[OpNum], UpperActiveWords), BitWidth);867 OpNum += UpperActiveWords;868 return ConstantRange(Lower, Upper);869 } else {870 int64_t Start = BitcodeReader::decodeSignRotatedValue(Record[OpNum++]);871 int64_t End = BitcodeReader::decodeSignRotatedValue(Record[OpNum++]);872 return ConstantRange(APInt(BitWidth, Start, true),873 APInt(BitWidth, End, true));874 }875 }876 877 Expected<ConstantRange>878 readBitWidthAndConstantRange(ArrayRef<uint64_t> Record, unsigned &OpNum) {879 if (Record.size() - OpNum < 1)880 return error("Too few records for range");881 unsigned BitWidth = Record[OpNum++];882 return readConstantRange(Record, OpNum, BitWidth);883 }884 885 /// Upgrades old-style typeless byval/sret/inalloca attributes by adding the886 /// corresponding argument's pointee type. Also upgrades intrinsics that now887 /// require an elementtype attribute.888 Error propagateAttributeTypes(CallBase *CB, ArrayRef<unsigned> ArgsTys);889 890 /// Converts alignment exponent (i.e. power of two (or zero)) to the891 /// corresponding alignment to use. If alignment is too large, returns892 /// a corresponding error code.893 Error parseAlignmentValue(uint64_t Exponent, MaybeAlign &Alignment);894 Error parseAttrKind(uint64_t Code, Attribute::AttrKind *Kind);895 Error parseModule(uint64_t ResumeBit, bool ShouldLazyLoadMetadata = false,896 ParserCallbacks Callbacks = {});897 898 Error parseComdatRecord(ArrayRef<uint64_t> Record);899 Error parseGlobalVarRecord(ArrayRef<uint64_t> Record);900 Error parseFunctionRecord(ArrayRef<uint64_t> Record);901 Error parseGlobalIndirectSymbolRecord(unsigned BitCode,902 ArrayRef<uint64_t> Record);903 904 Error parseAttributeBlock();905 Error parseAttributeGroupBlock();906 Error parseTypeTable();907 Error parseTypeTableBody();908 Error parseOperandBundleTags();909 Error parseSyncScopeNames();910 911 Expected<Value *> recordValue(SmallVectorImpl<uint64_t> &Record,912 unsigned NameIndex, Triple &TT);913 void setDeferredFunctionInfo(unsigned FuncBitcodeOffsetDelta, Function *F,914 ArrayRef<uint64_t> Record);915 Error parseValueSymbolTable(uint64_t Offset = 0);916 Error parseGlobalValueSymbolTable();917 Error parseConstants();918 Error rememberAndSkipFunctionBodies();919 Error rememberAndSkipFunctionBody();920 /// Save the positions of the Metadata blocks and skip parsing the blocks.921 Error rememberAndSkipMetadata();922 Error typeCheckLoadStoreInst(Type *ValType, Type *PtrType);923 Error parseFunctionBody(Function *F);924 Error globalCleanup();925 Error resolveGlobalAndIndirectSymbolInits();926 Error parseUseLists();927 Error findFunctionInStream(928 Function *F,929 DenseMap<Function *, uint64_t>::iterator DeferredFunctionInfoIterator);930 931 SyncScope::ID getDecodedSyncScopeID(unsigned Val);932};933 934/// Class to manage reading and parsing function summary index bitcode935/// files/sections.936class ModuleSummaryIndexBitcodeReader : public BitcodeReaderBase {937 /// The module index built during parsing.938 ModuleSummaryIndex &TheIndex;939 940 /// Indicates whether we have encountered a global value summary section941 /// yet during parsing.942 bool SeenGlobalValSummary = false;943 944 /// Indicates whether we have already parsed the VST, used for error checking.945 bool SeenValueSymbolTable = false;946 947 /// Set to the offset of the VST recorded in the MODULE_CODE_VSTOFFSET record.948 /// Used to enable on-demand parsing of the VST.949 uint64_t VSTOffset = 0;950 951 // Map to save ValueId to ValueInfo association that was recorded in the952 // ValueSymbolTable. It is used after the VST is parsed to convert953 // call graph edges read from the function summary from referencing954 // callees by their ValueId to using the ValueInfo instead, which is how955 // they are recorded in the summary index being built.956 // We save a GUID which refers to the same global as the ValueInfo, but957 // ignoring the linkage, i.e. for values other than local linkage they are958 // identical (this is the second member). ValueInfo has the real GUID.959 DenseMap<unsigned, std::pair<ValueInfo, GlobalValue::GUID>>960 ValueIdToValueInfoMap;961 962 /// Map populated during module path string table parsing, from the963 /// module ID to a string reference owned by the index's module964 /// path string table, used to correlate with combined index965 /// summary records.966 DenseMap<uint64_t, StringRef> ModuleIdMap;967 968 /// Original source file name recorded in a bitcode record.969 std::string SourceFileName;970 971 /// The string identifier given to this module by the client, normally the972 /// path to the bitcode file.973 StringRef ModulePath;974 975 /// Callback to ask whether a symbol is the prevailing copy when invoked976 /// during combined index building.977 std::function<bool(GlobalValue::GUID)> IsPrevailing;978 979 /// Saves the stack ids from the STACK_IDS record to consult when adding stack980 /// ids from the lists in the callsite and alloc entries to the index.981 std::vector<uint64_t> StackIds;982 983 /// Linearized radix tree of allocation contexts. See the description above984 /// the CallStackRadixTreeBuilder class in ProfileData/MemProf.h for format.985 std::vector<uint64_t> RadixArray;986 987public:988 ModuleSummaryIndexBitcodeReader(989 BitstreamCursor Stream, StringRef Strtab, ModuleSummaryIndex &TheIndex,990 StringRef ModulePath,991 std::function<bool(GlobalValue::GUID)> IsPrevailing = nullptr);992 993 Error parseModule();994 995private:996 void setValueGUID(uint64_t ValueID, StringRef ValueName,997 GlobalValue::LinkageTypes Linkage,998 StringRef SourceFileName);999 Error parseValueSymbolTable(1000 uint64_t Offset,1001 DenseMap<unsigned, GlobalValue::LinkageTypes> &ValueIdToLinkageMap);1002 SmallVector<ValueInfo, 0> makeRefList(ArrayRef<uint64_t> Record);1003 SmallVector<FunctionSummary::EdgeTy, 0>1004 makeCallList(ArrayRef<uint64_t> Record, bool IsOldProfileFormat,1005 bool HasProfile, bool HasRelBF);1006 Error parseEntireSummary(unsigned ID);1007 Error parseModuleStringTable();1008 void parseTypeIdCompatibleVtableSummaryRecord(ArrayRef<uint64_t> Record);1009 void parseTypeIdCompatibleVtableInfo(ArrayRef<uint64_t> Record, size_t &Slot,1010 TypeIdCompatibleVtableInfo &TypeId);1011 std::vector<FunctionSummary::ParamAccess>1012 parseParamAccesses(ArrayRef<uint64_t> Record);1013 SmallVector<unsigned> parseAllocInfoContext(ArrayRef<uint64_t> Record,1014 unsigned &I);1015 1016 template <bool AllowNullValueInfo = false>1017 std::pair<ValueInfo, GlobalValue::GUID>1018 getValueInfoFromValueId(unsigned ValueId);1019 1020 void addThisModule();1021 ModuleSummaryIndex::ModuleInfo *getThisModule();1022};1023 1024} // end anonymous namespace1025 1026std::error_code llvm::errorToErrorCodeAndEmitErrors(LLVMContext &Ctx,1027 Error Err) {1028 if (Err) {1029 std::error_code EC;1030 handleAllErrors(std::move(Err), [&](ErrorInfoBase &EIB) {1031 EC = EIB.convertToErrorCode();1032 Ctx.emitError(EIB.message());1033 });1034 return EC;1035 }1036 return std::error_code();1037}1038 1039BitcodeReader::BitcodeReader(BitstreamCursor Stream, StringRef Strtab,1040 StringRef ProducerIdentification,1041 LLVMContext &Context)1042 : BitcodeReaderBase(std::move(Stream), Strtab), Context(Context),1043 ValueList(this->Stream.SizeInBytes(),1044 [this](unsigned ValID, BasicBlock *InsertBB) {1045 return materializeValue(ValID, InsertBB);1046 }) {1047 this->ProducerIdentification = std::string(ProducerIdentification);1048}1049 1050Error BitcodeReader::materializeForwardReferencedFunctions() {1051 if (WillMaterializeAllForwardRefs)1052 return Error::success();1053 1054 // Prevent recursion.1055 WillMaterializeAllForwardRefs = true;1056 1057 while (!BasicBlockFwdRefQueue.empty()) {1058 Function *F = BasicBlockFwdRefQueue.front();1059 BasicBlockFwdRefQueue.pop_front();1060 assert(F && "Expected valid function");1061 if (!BasicBlockFwdRefs.count(F))1062 // Already materialized.1063 continue;1064 1065 // Check for a function that isn't materializable to prevent an infinite1066 // loop. When parsing a blockaddress stored in a global variable, there1067 // isn't a trivial way to check if a function will have a body without a1068 // linear search through FunctionsWithBodies, so just check it here.1069 if (!F->isMaterializable())1070 return error("Never resolved function from blockaddress");1071 1072 // Try to materialize F.1073 if (Error Err = materialize(F))1074 return Err;1075 }1076 assert(BasicBlockFwdRefs.empty() && "Function missing from queue");1077 1078 for (Function *F : BackwardRefFunctions)1079 if (Error Err = materialize(F))1080 return Err;1081 BackwardRefFunctions.clear();1082 1083 // Reset state.1084 WillMaterializeAllForwardRefs = false;1085 return Error::success();1086}1087 1088//===----------------------------------------------------------------------===//1089// Helper functions to implement forward reference resolution, etc.1090//===----------------------------------------------------------------------===//1091 1092static bool hasImplicitComdat(size_t Val) {1093 switch (Val) {1094 default:1095 return false;1096 case 1: // Old WeakAnyLinkage1097 case 4: // Old LinkOnceAnyLinkage1098 case 10: // Old WeakODRLinkage1099 case 11: // Old LinkOnceODRLinkage1100 return true;1101 }1102}1103 1104static GlobalValue::LinkageTypes getDecodedLinkage(unsigned Val) {1105 switch (Val) {1106 default: // Map unknown/new linkages to external1107 case 0:1108 return GlobalValue::ExternalLinkage;1109 case 2:1110 return GlobalValue::AppendingLinkage;1111 case 3:1112 return GlobalValue::InternalLinkage;1113 case 5:1114 return GlobalValue::ExternalLinkage; // Obsolete DLLImportLinkage1115 case 6:1116 return GlobalValue::ExternalLinkage; // Obsolete DLLExportLinkage1117 case 7:1118 return GlobalValue::ExternalWeakLinkage;1119 case 8:1120 return GlobalValue::CommonLinkage;1121 case 9:1122 return GlobalValue::PrivateLinkage;1123 case 12:1124 return GlobalValue::AvailableExternallyLinkage;1125 case 13:1126 return GlobalValue::PrivateLinkage; // Obsolete LinkerPrivateLinkage1127 case 14:1128 return GlobalValue::PrivateLinkage; // Obsolete LinkerPrivateWeakLinkage1129 case 15:1130 return GlobalValue::ExternalLinkage; // Obsolete LinkOnceODRAutoHideLinkage1131 case 1: // Old value with implicit comdat.1132 case 16:1133 return GlobalValue::WeakAnyLinkage;1134 case 10: // Old value with implicit comdat.1135 case 17:1136 return GlobalValue::WeakODRLinkage;1137 case 4: // Old value with implicit comdat.1138 case 18:1139 return GlobalValue::LinkOnceAnyLinkage;1140 case 11: // Old value with implicit comdat.1141 case 19:1142 return GlobalValue::LinkOnceODRLinkage;1143 }1144}1145 1146static FunctionSummary::FFlags getDecodedFFlags(uint64_t RawFlags) {1147 FunctionSummary::FFlags Flags;1148 Flags.ReadNone = RawFlags & 0x1;1149 Flags.ReadOnly = (RawFlags >> 1) & 0x1;1150 Flags.NoRecurse = (RawFlags >> 2) & 0x1;1151 Flags.ReturnDoesNotAlias = (RawFlags >> 3) & 0x1;1152 Flags.NoInline = (RawFlags >> 4) & 0x1;1153 Flags.AlwaysInline = (RawFlags >> 5) & 0x1;1154 Flags.NoUnwind = (RawFlags >> 6) & 0x1;1155 Flags.MayThrow = (RawFlags >> 7) & 0x1;1156 Flags.HasUnknownCall = (RawFlags >> 8) & 0x1;1157 Flags.MustBeUnreachable = (RawFlags >> 9) & 0x1;1158 return Flags;1159}1160 1161// Decode the flags for GlobalValue in the summary. The bits for each attribute:1162//1163// linkage: [0,4), notEligibleToImport: 4, live: 5, local: 6, canAutoHide: 7,1164// visibility: [8, 10).1165static GlobalValueSummary::GVFlags getDecodedGVSummaryFlags(uint64_t RawFlags,1166 uint64_t Version) {1167 // Summary were not emitted before LLVM 3.9, we don't need to upgrade Linkage1168 // like getDecodedLinkage() above. Any future change to the linkage enum and1169 // to getDecodedLinkage() will need to be taken into account here as above.1170 auto Linkage = GlobalValue::LinkageTypes(RawFlags & 0xF); // 4 bits1171 auto Visibility = GlobalValue::VisibilityTypes((RawFlags >> 8) & 3); // 2 bits1172 auto IK = GlobalValueSummary::ImportKind((RawFlags >> 10) & 1); // 1 bit1173 RawFlags = RawFlags >> 4;1174 bool NotEligibleToImport = (RawFlags & 0x1) || Version < 3;1175 // The Live flag wasn't introduced until version 3. For dead stripping1176 // to work correctly on earlier versions, we must conservatively treat all1177 // values as live.1178 bool Live = (RawFlags & 0x2) || Version < 3;1179 bool Local = (RawFlags & 0x4);1180 bool AutoHide = (RawFlags & 0x8);1181 1182 return GlobalValueSummary::GVFlags(Linkage, Visibility, NotEligibleToImport,1183 Live, Local, AutoHide, IK);1184}1185 1186// Decode the flags for GlobalVariable in the summary1187static GlobalVarSummary::GVarFlags getDecodedGVarFlags(uint64_t RawFlags) {1188 return GlobalVarSummary::GVarFlags(1189 (RawFlags & 0x1) ? true : false, (RawFlags & 0x2) ? true : false,1190 (RawFlags & 0x4) ? true : false,1191 (GlobalObject::VCallVisibility)(RawFlags >> 3));1192}1193 1194static std::pair<CalleeInfo::HotnessType, bool>1195getDecodedHotnessCallEdgeInfo(uint64_t RawFlags) {1196 CalleeInfo::HotnessType Hotness =1197 static_cast<CalleeInfo::HotnessType>(RawFlags & 0x7); // 3 bits1198 bool HasTailCall = (RawFlags & 0x8); // 1 bit1199 return {Hotness, HasTailCall};1200}1201 1202static void getDecodedRelBFCallEdgeInfo(uint64_t RawFlags, uint64_t &RelBF,1203 bool &HasTailCall) {1204 static constexpr uint64_t RelBlockFreqMask =1205 (1 << CalleeInfo::RelBlockFreqBits) - 1;1206 RelBF = RawFlags & RelBlockFreqMask; // RelBlockFreqBits bits1207 HasTailCall = (RawFlags & (1 << CalleeInfo::RelBlockFreqBits)); // 1 bit1208}1209 1210static GlobalValue::VisibilityTypes getDecodedVisibility(unsigned Val) {1211 switch (Val) {1212 default: // Map unknown visibilities to default.1213 case 0: return GlobalValue::DefaultVisibility;1214 case 1: return GlobalValue::HiddenVisibility;1215 case 2: return GlobalValue::ProtectedVisibility;1216 }1217}1218 1219static GlobalValue::DLLStorageClassTypes1220getDecodedDLLStorageClass(unsigned Val) {1221 switch (Val) {1222 default: // Map unknown values to default.1223 case 0: return GlobalValue::DefaultStorageClass;1224 case 1: return GlobalValue::DLLImportStorageClass;1225 case 2: return GlobalValue::DLLExportStorageClass;1226 }1227}1228 1229static bool getDecodedDSOLocal(unsigned Val) {1230 switch(Val) {1231 default: // Map unknown values to preemptable.1232 case 0: return false;1233 case 1: return true;1234 }1235}1236 1237static std::optional<CodeModel::Model> getDecodedCodeModel(unsigned Val) {1238 switch (Val) {1239 case 1:1240 return CodeModel::Tiny;1241 case 2:1242 return CodeModel::Small;1243 case 3:1244 return CodeModel::Kernel;1245 case 4:1246 return CodeModel::Medium;1247 case 5:1248 return CodeModel::Large;1249 }1250 1251 return {};1252}1253 1254static GlobalVariable::ThreadLocalMode getDecodedThreadLocalMode(unsigned Val) {1255 switch (Val) {1256 case 0: return GlobalVariable::NotThreadLocal;1257 default: // Map unknown non-zero value to general dynamic.1258 case 1: return GlobalVariable::GeneralDynamicTLSModel;1259 case 2: return GlobalVariable::LocalDynamicTLSModel;1260 case 3: return GlobalVariable::InitialExecTLSModel;1261 case 4: return GlobalVariable::LocalExecTLSModel;1262 }1263}1264 1265static GlobalVariable::UnnamedAddr getDecodedUnnamedAddrType(unsigned Val) {1266 switch (Val) {1267 default: // Map unknown to UnnamedAddr::None.1268 case 0: return GlobalVariable::UnnamedAddr::None;1269 case 1: return GlobalVariable::UnnamedAddr::Global;1270 case 2: return GlobalVariable::UnnamedAddr::Local;1271 }1272}1273 1274static int getDecodedCastOpcode(unsigned Val) {1275 switch (Val) {1276 default: return -1;1277 case bitc::CAST_TRUNC : return Instruction::Trunc;1278 case bitc::CAST_ZEXT : return Instruction::ZExt;1279 case bitc::CAST_SEXT : return Instruction::SExt;1280 case bitc::CAST_FPTOUI : return Instruction::FPToUI;1281 case bitc::CAST_FPTOSI : return Instruction::FPToSI;1282 case bitc::CAST_UITOFP : return Instruction::UIToFP;1283 case bitc::CAST_SITOFP : return Instruction::SIToFP;1284 case bitc::CAST_FPTRUNC : return Instruction::FPTrunc;1285 case bitc::CAST_FPEXT : return Instruction::FPExt;1286 case bitc::CAST_PTRTOADDR: return Instruction::PtrToAddr;1287 case bitc::CAST_PTRTOINT: return Instruction::PtrToInt;1288 case bitc::CAST_INTTOPTR: return Instruction::IntToPtr;1289 case bitc::CAST_BITCAST : return Instruction::BitCast;1290 case bitc::CAST_ADDRSPACECAST: return Instruction::AddrSpaceCast;1291 }1292}1293 1294static int getDecodedUnaryOpcode(unsigned Val, Type *Ty) {1295 bool IsFP = Ty->isFPOrFPVectorTy();1296 // UnOps are only valid for int/fp or vector of int/fp types1297 if (!IsFP && !Ty->isIntOrIntVectorTy())1298 return -1;1299 1300 switch (Val) {1301 default:1302 return -1;1303 case bitc::UNOP_FNEG:1304 return IsFP ? Instruction::FNeg : -1;1305 }1306}1307 1308static int getDecodedBinaryOpcode(unsigned Val, Type *Ty) {1309 bool IsFP = Ty->isFPOrFPVectorTy();1310 // BinOps are only valid for int/fp or vector of int/fp types1311 if (!IsFP && !Ty->isIntOrIntVectorTy())1312 return -1;1313 1314 switch (Val) {1315 default:1316 return -1;1317 case bitc::BINOP_ADD:1318 return IsFP ? Instruction::FAdd : Instruction::Add;1319 case bitc::BINOP_SUB:1320 return IsFP ? Instruction::FSub : Instruction::Sub;1321 case bitc::BINOP_MUL:1322 return IsFP ? Instruction::FMul : Instruction::Mul;1323 case bitc::BINOP_UDIV:1324 return IsFP ? -1 : Instruction::UDiv;1325 case bitc::BINOP_SDIV:1326 return IsFP ? Instruction::FDiv : Instruction::SDiv;1327 case bitc::BINOP_UREM:1328 return IsFP ? -1 : Instruction::URem;1329 case bitc::BINOP_SREM:1330 return IsFP ? Instruction::FRem : Instruction::SRem;1331 case bitc::BINOP_SHL:1332 return IsFP ? -1 : Instruction::Shl;1333 case bitc::BINOP_LSHR:1334 return IsFP ? -1 : Instruction::LShr;1335 case bitc::BINOP_ASHR:1336 return IsFP ? -1 : Instruction::AShr;1337 case bitc::BINOP_AND:1338 return IsFP ? -1 : Instruction::And;1339 case bitc::BINOP_OR:1340 return IsFP ? -1 : Instruction::Or;1341 case bitc::BINOP_XOR:1342 return IsFP ? -1 : Instruction::Xor;1343 }1344}1345 1346static AtomicRMWInst::BinOp getDecodedRMWOperation(unsigned Val) {1347 switch (Val) {1348 default: return AtomicRMWInst::BAD_BINOP;1349 case bitc::RMW_XCHG: return AtomicRMWInst::Xchg;1350 case bitc::RMW_ADD: return AtomicRMWInst::Add;1351 case bitc::RMW_SUB: return AtomicRMWInst::Sub;1352 case bitc::RMW_AND: return AtomicRMWInst::And;1353 case bitc::RMW_NAND: return AtomicRMWInst::Nand;1354 case bitc::RMW_OR: return AtomicRMWInst::Or;1355 case bitc::RMW_XOR: return AtomicRMWInst::Xor;1356 case bitc::RMW_MAX: return AtomicRMWInst::Max;1357 case bitc::RMW_MIN: return AtomicRMWInst::Min;1358 case bitc::RMW_UMAX: return AtomicRMWInst::UMax;1359 case bitc::RMW_UMIN: return AtomicRMWInst::UMin;1360 case bitc::RMW_FADD: return AtomicRMWInst::FAdd;1361 case bitc::RMW_FSUB: return AtomicRMWInst::FSub;1362 case bitc::RMW_FMAX: return AtomicRMWInst::FMax;1363 case bitc::RMW_FMIN: return AtomicRMWInst::FMin;1364 case bitc::RMW_FMAXIMUM:1365 return AtomicRMWInst::FMaximum;1366 case bitc::RMW_FMINIMUM:1367 return AtomicRMWInst::FMinimum;1368 case bitc::RMW_UINC_WRAP:1369 return AtomicRMWInst::UIncWrap;1370 case bitc::RMW_UDEC_WRAP:1371 return AtomicRMWInst::UDecWrap;1372 case bitc::RMW_USUB_COND:1373 return AtomicRMWInst::USubCond;1374 case bitc::RMW_USUB_SAT:1375 return AtomicRMWInst::USubSat;1376 }1377}1378 1379static AtomicOrdering getDecodedOrdering(unsigned Val) {1380 switch (Val) {1381 case bitc::ORDERING_NOTATOMIC: return AtomicOrdering::NotAtomic;1382 case bitc::ORDERING_UNORDERED: return AtomicOrdering::Unordered;1383 case bitc::ORDERING_MONOTONIC: return AtomicOrdering::Monotonic;1384 case bitc::ORDERING_ACQUIRE: return AtomicOrdering::Acquire;1385 case bitc::ORDERING_RELEASE: return AtomicOrdering::Release;1386 case bitc::ORDERING_ACQREL: return AtomicOrdering::AcquireRelease;1387 default: // Map unknown orderings to sequentially-consistent.1388 case bitc::ORDERING_SEQCST: return AtomicOrdering::SequentiallyConsistent;1389 }1390}1391 1392static Comdat::SelectionKind getDecodedComdatSelectionKind(unsigned Val) {1393 switch (Val) {1394 default: // Map unknown selection kinds to any.1395 case bitc::COMDAT_SELECTION_KIND_ANY:1396 return Comdat::Any;1397 case bitc::COMDAT_SELECTION_KIND_EXACT_MATCH:1398 return Comdat::ExactMatch;1399 case bitc::COMDAT_SELECTION_KIND_LARGEST:1400 return Comdat::Largest;1401 case bitc::COMDAT_SELECTION_KIND_NO_DUPLICATES:1402 return Comdat::NoDeduplicate;1403 case bitc::COMDAT_SELECTION_KIND_SAME_SIZE:1404 return Comdat::SameSize;1405 }1406}1407 1408static FastMathFlags getDecodedFastMathFlags(unsigned Val) {1409 FastMathFlags FMF;1410 if (0 != (Val & bitc::UnsafeAlgebra))1411 FMF.setFast();1412 if (0 != (Val & bitc::AllowReassoc))1413 FMF.setAllowReassoc();1414 if (0 != (Val & bitc::NoNaNs))1415 FMF.setNoNaNs();1416 if (0 != (Val & bitc::NoInfs))1417 FMF.setNoInfs();1418 if (0 != (Val & bitc::NoSignedZeros))1419 FMF.setNoSignedZeros();1420 if (0 != (Val & bitc::AllowReciprocal))1421 FMF.setAllowReciprocal();1422 if (0 != (Val & bitc::AllowContract))1423 FMF.setAllowContract(true);1424 if (0 != (Val & bitc::ApproxFunc))1425 FMF.setApproxFunc();1426 return FMF;1427}1428 1429static void upgradeDLLImportExportLinkage(GlobalValue *GV, unsigned Val) {1430 // A GlobalValue with local linkage cannot have a DLL storage class.1431 if (GV->hasLocalLinkage())1432 return;1433 switch (Val) {1434 case 5: GV->setDLLStorageClass(GlobalValue::DLLImportStorageClass); break;1435 case 6: GV->setDLLStorageClass(GlobalValue::DLLExportStorageClass); break;1436 }1437}1438 1439Type *BitcodeReader::getTypeByID(unsigned ID) {1440 // The type table size is always specified correctly.1441 if (ID >= TypeList.size())1442 return nullptr;1443 1444 if (Type *Ty = TypeList[ID])1445 return Ty;1446 1447 // If we have a forward reference, the only possible case is when it is to a1448 // named struct. Just create a placeholder for now.1449 return TypeList[ID] = createIdentifiedStructType(Context);1450}1451 1452unsigned BitcodeReader::getContainedTypeID(unsigned ID, unsigned Idx) {1453 auto It = ContainedTypeIDs.find(ID);1454 if (It == ContainedTypeIDs.end())1455 return InvalidTypeID;1456 1457 if (Idx >= It->second.size())1458 return InvalidTypeID;1459 1460 return It->second[Idx];1461}1462 1463Type *BitcodeReader::getPtrElementTypeByID(unsigned ID) {1464 if (ID >= TypeList.size())1465 return nullptr;1466 1467 Type *Ty = TypeList[ID];1468 if (!Ty->isPointerTy())1469 return nullptr;1470 1471 return getTypeByID(getContainedTypeID(ID, 0));1472}1473 1474unsigned BitcodeReader::getVirtualTypeID(Type *Ty,1475 ArrayRef<unsigned> ChildTypeIDs) {1476 unsigned ChildTypeID = ChildTypeIDs.empty() ? InvalidTypeID : ChildTypeIDs[0];1477 auto CacheKey = std::make_pair(Ty, ChildTypeID);1478 auto It = VirtualTypeIDs.find(CacheKey);1479 if (It != VirtualTypeIDs.end()) {1480 // The cmpxchg return value is the only place we need more than one1481 // contained type ID, however the second one will always be the same (i1),1482 // so we don't need to include it in the cache key. This asserts that the1483 // contained types are indeed as expected and there are no collisions.1484 assert((ChildTypeIDs.empty() ||1485 ContainedTypeIDs[It->second] == ChildTypeIDs) &&1486 "Incorrect cached contained type IDs");1487 return It->second;1488 }1489 1490 unsigned TypeID = TypeList.size();1491 TypeList.push_back(Ty);1492 if (!ChildTypeIDs.empty())1493 append_range(ContainedTypeIDs[TypeID], ChildTypeIDs);1494 VirtualTypeIDs.insert({CacheKey, TypeID});1495 return TypeID;1496}1497 1498static GEPNoWrapFlags toGEPNoWrapFlags(uint64_t Flags) {1499 GEPNoWrapFlags NW;1500 if (Flags & (1 << bitc::GEP_INBOUNDS))1501 NW |= GEPNoWrapFlags::inBounds();1502 if (Flags & (1 << bitc::GEP_NUSW))1503 NW |= GEPNoWrapFlags::noUnsignedSignedWrap();1504 if (Flags & (1 << bitc::GEP_NUW))1505 NW |= GEPNoWrapFlags::noUnsignedWrap();1506 return NW;1507}1508 1509static bool isConstExprSupported(const BitcodeConstant *BC) {1510 uint8_t Opcode = BC->Opcode;1511 1512 // These are not real constant expressions, always consider them supported.1513 if (Opcode >= BitcodeConstant::FirstSpecialOpcode)1514 return true;1515 1516 // If -expand-constant-exprs is set, we want to consider all expressions1517 // as unsupported.1518 if (ExpandConstantExprs)1519 return false;1520 1521 if (Instruction::isBinaryOp(Opcode))1522 return ConstantExpr::isSupportedBinOp(Opcode);1523 1524 if (Instruction::isCast(Opcode))1525 return ConstantExpr::isSupportedCastOp(Opcode);1526 1527 if (Opcode == Instruction::GetElementPtr)1528 return ConstantExpr::isSupportedGetElementPtr(BC->SrcElemTy);1529 1530 switch (Opcode) {1531 case Instruction::FNeg:1532 case Instruction::Select:1533 case Instruction::ICmp:1534 case Instruction::FCmp:1535 return false;1536 default:1537 return true;1538 }1539}1540 1541Expected<Value *> BitcodeReader::materializeValue(unsigned StartValID,1542 BasicBlock *InsertBB) {1543 // Quickly handle the case where there is no BitcodeConstant to resolve.1544 if (StartValID < ValueList.size() && ValueList[StartValID] &&1545 !isa<BitcodeConstant>(ValueList[StartValID]))1546 return ValueList[StartValID];1547 1548 SmallDenseMap<unsigned, Value *> MaterializedValues;1549 SmallVector<unsigned> Worklist;1550 Worklist.push_back(StartValID);1551 while (!Worklist.empty()) {1552 unsigned ValID = Worklist.back();1553 if (MaterializedValues.count(ValID)) {1554 // Duplicate expression that was already handled.1555 Worklist.pop_back();1556 continue;1557 }1558 1559 if (ValID >= ValueList.size() || !ValueList[ValID])1560 return error("Invalid value ID");1561 1562 Value *V = ValueList[ValID];1563 auto *BC = dyn_cast<BitcodeConstant>(V);1564 if (!BC) {1565 MaterializedValues.insert({ValID, V});1566 Worklist.pop_back();1567 continue;1568 }1569 1570 // Iterate in reverse, so values will get popped from the worklist in1571 // expected order.1572 SmallVector<Value *> Ops;1573 for (unsigned OpID : reverse(BC->getOperandIDs())) {1574 auto It = MaterializedValues.find(OpID);1575 if (It != MaterializedValues.end())1576 Ops.push_back(It->second);1577 else1578 Worklist.push_back(OpID);1579 }1580 1581 // Some expressions have not been resolved yet, handle them first and then1582 // revisit this one.1583 if (Ops.size() != BC->getOperandIDs().size())1584 continue;1585 std::reverse(Ops.begin(), Ops.end());1586 1587 SmallVector<Constant *> ConstOps;1588 for (Value *Op : Ops)1589 if (auto *C = dyn_cast<Constant>(Op))1590 ConstOps.push_back(C);1591 1592 // Materialize as constant expression if possible.1593 if (isConstExprSupported(BC) && ConstOps.size() == Ops.size()) {1594 Constant *C;1595 if (Instruction::isCast(BC->Opcode)) {1596 C = UpgradeBitCastExpr(BC->Opcode, ConstOps[0], BC->getType());1597 if (!C)1598 C = ConstantExpr::getCast(BC->Opcode, ConstOps[0], BC->getType());1599 } else if (Instruction::isBinaryOp(BC->Opcode)) {1600 C = ConstantExpr::get(BC->Opcode, ConstOps[0], ConstOps[1], BC->Flags);1601 } else {1602 switch (BC->Opcode) {1603 case BitcodeConstant::ConstantPtrAuthOpcode: {1604 auto *Key = dyn_cast<ConstantInt>(ConstOps[1]);1605 if (!Key)1606 return error("ptrauth key operand must be ConstantInt");1607 1608 auto *Disc = dyn_cast<ConstantInt>(ConstOps[2]);1609 if (!Disc)1610 return error("ptrauth disc operand must be ConstantInt");1611 1612 Constant *DeactivationSymbol =1613 ConstOps.size() > 4 ? ConstOps[4]1614 : ConstantPointerNull::get(cast<PointerType>(1615 ConstOps[3]->getType()));1616 if (!DeactivationSymbol->getType()->isPointerTy())1617 return error(1618 "ptrauth deactivation symbol operand must be a pointer");1619 1620 C = ConstantPtrAuth::get(ConstOps[0], Key, Disc, ConstOps[3],1621 DeactivationSymbol);1622 break;1623 }1624 case BitcodeConstant::NoCFIOpcode: {1625 auto *GV = dyn_cast<GlobalValue>(ConstOps[0]);1626 if (!GV)1627 return error("no_cfi operand must be GlobalValue");1628 C = NoCFIValue::get(GV);1629 break;1630 }1631 case BitcodeConstant::DSOLocalEquivalentOpcode: {1632 auto *GV = dyn_cast<GlobalValue>(ConstOps[0]);1633 if (!GV)1634 return error("dso_local operand must be GlobalValue");1635 C = DSOLocalEquivalent::get(GV);1636 break;1637 }1638 case BitcodeConstant::BlockAddressOpcode: {1639 Function *Fn = dyn_cast<Function>(ConstOps[0]);1640 if (!Fn)1641 return error("blockaddress operand must be a function");1642 1643 // If the function is already parsed we can insert the block address1644 // right away.1645 BasicBlock *BB;1646 unsigned BBID = BC->BlockAddressBB;1647 if (!BBID)1648 // Invalid reference to entry block.1649 return error("Invalid ID");1650 if (!Fn->empty()) {1651 Function::iterator BBI = Fn->begin(), BBE = Fn->end();1652 for (size_t I = 0, E = BBID; I != E; ++I) {1653 if (BBI == BBE)1654 return error("Invalid ID");1655 ++BBI;1656 }1657 BB = &*BBI;1658 } else {1659 // Otherwise insert a placeholder and remember it so it can be1660 // inserted when the function is parsed.1661 auto &FwdBBs = BasicBlockFwdRefs[Fn];1662 if (FwdBBs.empty())1663 BasicBlockFwdRefQueue.push_back(Fn);1664 if (FwdBBs.size() < BBID + 1)1665 FwdBBs.resize(BBID + 1);1666 if (!FwdBBs[BBID])1667 FwdBBs[BBID] = BasicBlock::Create(Context);1668 BB = FwdBBs[BBID];1669 }1670 C = BlockAddress::get(Fn->getType(), BB);1671 break;1672 }1673 case BitcodeConstant::ConstantStructOpcode: {1674 auto *ST = cast<StructType>(BC->getType());1675 if (ST->getNumElements() != ConstOps.size())1676 return error("Invalid number of elements in struct initializer");1677 1678 for (const auto [Ty, Op] : zip(ST->elements(), ConstOps))1679 if (Op->getType() != Ty)1680 return error("Incorrect type in struct initializer");1681 1682 C = ConstantStruct::get(ST, ConstOps);1683 break;1684 }1685 case BitcodeConstant::ConstantArrayOpcode: {1686 auto *AT = cast<ArrayType>(BC->getType());1687 if (AT->getNumElements() != ConstOps.size())1688 return error("Invalid number of elements in array initializer");1689 1690 for (Constant *Op : ConstOps)1691 if (Op->getType() != AT->getElementType())1692 return error("Incorrect type in array initializer");1693 1694 C = ConstantArray::get(AT, ConstOps);1695 break;1696 }1697 case BitcodeConstant::ConstantVectorOpcode: {1698 auto *VT = cast<FixedVectorType>(BC->getType());1699 if (VT->getNumElements() != ConstOps.size())1700 return error("Invalid number of elements in vector initializer");1701 1702 for (Constant *Op : ConstOps)1703 if (Op->getType() != VT->getElementType())1704 return error("Incorrect type in vector initializer");1705 1706 C = ConstantVector::get(ConstOps);1707 break;1708 }1709 case Instruction::GetElementPtr:1710 C = ConstantExpr::getGetElementPtr(1711 BC->SrcElemTy, ConstOps[0], ArrayRef(ConstOps).drop_front(),1712 toGEPNoWrapFlags(BC->Flags), BC->getInRange());1713 break;1714 case Instruction::ExtractElement:1715 C = ConstantExpr::getExtractElement(ConstOps[0], ConstOps[1]);1716 break;1717 case Instruction::InsertElement:1718 C = ConstantExpr::getInsertElement(ConstOps[0], ConstOps[1],1719 ConstOps[2]);1720 break;1721 case Instruction::ShuffleVector: {1722 SmallVector<int, 16> Mask;1723 ShuffleVectorInst::getShuffleMask(ConstOps[2], Mask);1724 C = ConstantExpr::getShuffleVector(ConstOps[0], ConstOps[1], Mask);1725 break;1726 }1727 default:1728 llvm_unreachable("Unhandled bitcode constant");1729 }1730 }1731 1732 // Cache resolved constant.1733 ValueList.replaceValueWithoutRAUW(ValID, C);1734 MaterializedValues.insert({ValID, C});1735 Worklist.pop_back();1736 continue;1737 }1738 1739 if (!InsertBB)1740 return error(Twine("Value referenced by initializer is an unsupported "1741 "constant expression of type ") +1742 BC->getOpcodeName());1743 1744 // Materialize as instructions if necessary.1745 Instruction *I;1746 if (Instruction::isCast(BC->Opcode)) {1747 I = CastInst::Create((Instruction::CastOps)BC->Opcode, Ops[0],1748 BC->getType(), "constexpr", InsertBB);1749 } else if (Instruction::isUnaryOp(BC->Opcode)) {1750 I = UnaryOperator::Create((Instruction::UnaryOps)BC->Opcode, Ops[0],1751 "constexpr", InsertBB);1752 } else if (Instruction::isBinaryOp(BC->Opcode)) {1753 I = BinaryOperator::Create((Instruction::BinaryOps)BC->Opcode, Ops[0],1754 Ops[1], "constexpr", InsertBB);1755 if (isa<OverflowingBinaryOperator>(I)) {1756 if (BC->Flags & OverflowingBinaryOperator::NoSignedWrap)1757 I->setHasNoSignedWrap();1758 if (BC->Flags & OverflowingBinaryOperator::NoUnsignedWrap)1759 I->setHasNoUnsignedWrap();1760 }1761 if (isa<PossiblyExactOperator>(I) &&1762 (BC->Flags & PossiblyExactOperator::IsExact))1763 I->setIsExact();1764 } else {1765 switch (BC->Opcode) {1766 case BitcodeConstant::ConstantVectorOpcode: {1767 Type *IdxTy = Type::getInt32Ty(BC->getContext());1768 Value *V = PoisonValue::get(BC->getType());1769 for (auto Pair : enumerate(Ops)) {1770 Value *Idx = ConstantInt::get(IdxTy, Pair.index());1771 V = InsertElementInst::Create(V, Pair.value(), Idx, "constexpr.ins",1772 InsertBB);1773 }1774 I = cast<Instruction>(V);1775 break;1776 }1777 case BitcodeConstant::ConstantStructOpcode:1778 case BitcodeConstant::ConstantArrayOpcode: {1779 Value *V = PoisonValue::get(BC->getType());1780 for (auto Pair : enumerate(Ops))1781 V = InsertValueInst::Create(V, Pair.value(), Pair.index(),1782 "constexpr.ins", InsertBB);1783 I = cast<Instruction>(V);1784 break;1785 }1786 case Instruction::ICmp:1787 case Instruction::FCmp:1788 I = CmpInst::Create((Instruction::OtherOps)BC->Opcode,1789 (CmpInst::Predicate)BC->Flags, Ops[0], Ops[1],1790 "constexpr", InsertBB);1791 break;1792 case Instruction::GetElementPtr:1793 I = GetElementPtrInst::Create(BC->SrcElemTy, Ops[0],1794 ArrayRef(Ops).drop_front(), "constexpr",1795 InsertBB);1796 cast<GetElementPtrInst>(I)->setNoWrapFlags(toGEPNoWrapFlags(BC->Flags));1797 break;1798 case Instruction::Select:1799 I = SelectInst::Create(Ops[0], Ops[1], Ops[2], "constexpr", InsertBB);1800 break;1801 case Instruction::ExtractElement:1802 I = ExtractElementInst::Create(Ops[0], Ops[1], "constexpr", InsertBB);1803 break;1804 case Instruction::InsertElement:1805 I = InsertElementInst::Create(Ops[0], Ops[1], Ops[2], "constexpr",1806 InsertBB);1807 break;1808 case Instruction::ShuffleVector:1809 I = new ShuffleVectorInst(Ops[0], Ops[1], Ops[2], "constexpr",1810 InsertBB);1811 break;1812 default:1813 llvm_unreachable("Unhandled bitcode constant");1814 }1815 }1816 1817 MaterializedValues.insert({ValID, I});1818 Worklist.pop_back();1819 }1820 1821 return MaterializedValues[StartValID];1822}1823 1824Expected<Constant *> BitcodeReader::getValueForInitializer(unsigned ID) {1825 Expected<Value *> MaybeV = materializeValue(ID, /* InsertBB */ nullptr);1826 if (!MaybeV)1827 return MaybeV.takeError();1828 1829 // Result must be Constant if InsertBB is nullptr.1830 return cast<Constant>(MaybeV.get());1831}1832 1833StructType *BitcodeReader::createIdentifiedStructType(LLVMContext &Context,1834 StringRef Name) {1835 auto *Ret = StructType::create(Context, Name);1836 IdentifiedStructTypes.push_back(Ret);1837 return Ret;1838}1839 1840StructType *BitcodeReader::createIdentifiedStructType(LLVMContext &Context) {1841 auto *Ret = StructType::create(Context);1842 IdentifiedStructTypes.push_back(Ret);1843 return Ret;1844}1845 1846//===----------------------------------------------------------------------===//1847// Functions for parsing blocks from the bitcode file1848//===----------------------------------------------------------------------===//1849 1850static uint64_t getRawAttributeMask(Attribute::AttrKind Val) {1851 switch (Val) {1852 case Attribute::EndAttrKinds:1853 case Attribute::EmptyKey:1854 case Attribute::TombstoneKey:1855 llvm_unreachable("Synthetic enumerators which should never get here");1856 1857 case Attribute::None: return 0;1858 case Attribute::ZExt: return 1 << 0;1859 case Attribute::SExt: return 1 << 1;1860 case Attribute::NoReturn: return 1 << 2;1861 case Attribute::InReg: return 1 << 3;1862 case Attribute::StructRet: return 1 << 4;1863 case Attribute::NoUnwind: return 1 << 5;1864 case Attribute::NoAlias: return 1 << 6;1865 case Attribute::ByVal: return 1 << 7;1866 case Attribute::Nest: return 1 << 8;1867 case Attribute::ReadNone: return 1 << 9;1868 case Attribute::ReadOnly: return 1 << 10;1869 case Attribute::NoInline: return 1 << 11;1870 case Attribute::AlwaysInline: return 1 << 12;1871 case Attribute::OptimizeForSize: return 1 << 13;1872 case Attribute::StackProtect: return 1 << 14;1873 case Attribute::StackProtectReq: return 1 << 15;1874 case Attribute::Alignment: return 31 << 16;1875 // 1ULL << 21 is NoCapture, which is upgraded separately.1876 case Attribute::NoRedZone: return 1 << 22;1877 case Attribute::NoImplicitFloat: return 1 << 23;1878 case Attribute::Naked: return 1 << 24;1879 case Attribute::InlineHint: return 1 << 25;1880 case Attribute::StackAlignment: return 7 << 26;1881 case Attribute::ReturnsTwice: return 1 << 29;1882 case Attribute::UWTable: return 1 << 30;1883 case Attribute::NonLazyBind: return 1U << 31;1884 case Attribute::SanitizeAddress: return 1ULL << 32;1885 case Attribute::MinSize: return 1ULL << 33;1886 case Attribute::NoDuplicate: return 1ULL << 34;1887 case Attribute::StackProtectStrong: return 1ULL << 35;1888 case Attribute::SanitizeThread: return 1ULL << 36;1889 case Attribute::SanitizeMemory: return 1ULL << 37;1890 case Attribute::NoBuiltin: return 1ULL << 38;1891 case Attribute::Returned: return 1ULL << 39;1892 case Attribute::Cold: return 1ULL << 40;1893 case Attribute::Builtin: return 1ULL << 41;1894 case Attribute::OptimizeNone: return 1ULL << 42;1895 case Attribute::InAlloca: return 1ULL << 43;1896 case Attribute::NonNull: return 1ULL << 44;1897 case Attribute::JumpTable: return 1ULL << 45;1898 case Attribute::Convergent: return 1ULL << 46;1899 case Attribute::SafeStack: return 1ULL << 47;1900 case Attribute::NoRecurse: return 1ULL << 48;1901 // 1ULL << 49 is InaccessibleMemOnly, which is upgraded separately.1902 // 1ULL << 50 is InaccessibleMemOrArgMemOnly, which is upgraded separately.1903 case Attribute::SwiftSelf: return 1ULL << 51;1904 case Attribute::SwiftError: return 1ULL << 52;1905 case Attribute::WriteOnly: return 1ULL << 53;1906 case Attribute::Speculatable: return 1ULL << 54;1907 case Attribute::StrictFP: return 1ULL << 55;1908 case Attribute::SanitizeHWAddress: return 1ULL << 56;1909 case Attribute::NoCfCheck: return 1ULL << 57;1910 case Attribute::OptForFuzzing: return 1ULL << 58;1911 case Attribute::ShadowCallStack: return 1ULL << 59;1912 case Attribute::SpeculativeLoadHardening:1913 return 1ULL << 60;1914 case Attribute::ImmArg:1915 return 1ULL << 61;1916 case Attribute::WillReturn:1917 return 1ULL << 62;1918 case Attribute::NoFree:1919 return 1ULL << 63;1920 default:1921 // Other attributes are not supported in the raw format,1922 // as we ran out of space.1923 return 0;1924 }1925 llvm_unreachable("Unsupported attribute type");1926}1927 1928static void addRawAttributeValue(AttrBuilder &B, uint64_t Val) {1929 if (!Val) return;1930 1931 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;1932 I = Attribute::AttrKind(I + 1)) {1933 if (uint64_t A = (Val & getRawAttributeMask(I))) {1934 if (I == Attribute::Alignment)1935 B.addAlignmentAttr(1ULL << ((A >> 16) - 1));1936 else if (I == Attribute::StackAlignment)1937 B.addStackAlignmentAttr(1ULL << ((A >> 26)-1));1938 else if (Attribute::isTypeAttrKind(I))1939 B.addTypeAttr(I, nullptr); // Type will be auto-upgraded.1940 else1941 B.addAttribute(I);1942 }1943 }1944}1945 1946/// This fills an AttrBuilder object with the LLVM attributes that have1947/// been decoded from the given integer.1948static void decodeLLVMAttributesForBitcode(AttrBuilder &B,1949 uint64_t EncodedAttrs,1950 uint64_t AttrIdx) {1951 // The alignment is stored as a 16-bit raw value from bits 31--16. We shift1952 // the bits above 31 down by 11 bits.1953 unsigned Alignment = (EncodedAttrs & (0xffffULL << 16)) >> 16;1954 assert((!Alignment || isPowerOf2_32(Alignment)) &&1955 "Alignment must be a power of two.");1956 1957 if (Alignment)1958 B.addAlignmentAttr(Alignment);1959 1960 uint64_t Attrs = ((EncodedAttrs & (0xfffffULL << 32)) >> 11) |1961 (EncodedAttrs & 0xffff);1962 1963 if (AttrIdx == AttributeList::FunctionIndex) {1964 // Upgrade old memory attributes.1965 MemoryEffects ME = MemoryEffects::unknown();1966 if (Attrs & (1ULL << 9)) {1967 // ReadNone1968 Attrs &= ~(1ULL << 9);1969 ME &= MemoryEffects::none();1970 }1971 if (Attrs & (1ULL << 10)) {1972 // ReadOnly1973 Attrs &= ~(1ULL << 10);1974 ME &= MemoryEffects::readOnly();1975 }1976 if (Attrs & (1ULL << 49)) {1977 // InaccessibleMemOnly1978 Attrs &= ~(1ULL << 49);1979 ME &= MemoryEffects::inaccessibleMemOnly();1980 }1981 if (Attrs & (1ULL << 50)) {1982 // InaccessibleMemOrArgMemOnly1983 Attrs &= ~(1ULL << 50);1984 ME &= MemoryEffects::inaccessibleOrArgMemOnly();1985 }1986 if (Attrs & (1ULL << 53)) {1987 // WriteOnly1988 Attrs &= ~(1ULL << 53);1989 ME &= MemoryEffects::writeOnly();1990 }1991 if (ME != MemoryEffects::unknown())1992 B.addMemoryAttr(ME);1993 }1994 1995 // Upgrade nocapture to captures(none).1996 if (Attrs & (1ULL << 21)) {1997 Attrs &= ~(1ULL << 21);1998 B.addCapturesAttr(CaptureInfo::none());1999 }2000 2001 addRawAttributeValue(B, Attrs);2002}2003 2004Error BitcodeReader::parseAttributeBlock() {2005 if (Error Err = Stream.EnterSubBlock(bitc::PARAMATTR_BLOCK_ID))2006 return Err;2007 2008 if (!MAttributes.empty())2009 return error("Invalid multiple blocks");2010 2011 SmallVector<uint64_t, 64> Record;2012 2013 SmallVector<AttributeList, 8> Attrs;2014 2015 // Read all the records.2016 while (true) {2017 Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks();2018 if (!MaybeEntry)2019 return MaybeEntry.takeError();2020 BitstreamEntry Entry = MaybeEntry.get();2021 2022 switch (Entry.Kind) {2023 case BitstreamEntry::SubBlock: // Handled for us already.2024 case BitstreamEntry::Error:2025 return error("Malformed block");2026 case BitstreamEntry::EndBlock:2027 return Error::success();2028 case BitstreamEntry::Record:2029 // The interesting case.2030 break;2031 }2032 2033 // Read a record.2034 Record.clear();2035 Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record);2036 if (!MaybeRecord)2037 return MaybeRecord.takeError();2038 switch (MaybeRecord.get()) {2039 default: // Default behavior: ignore.2040 break;2041 case bitc::PARAMATTR_CODE_ENTRY_OLD: // ENTRY: [paramidx0, attr0, ...]2042 // Deprecated, but still needed to read old bitcode files.2043 if (Record.size() & 1)2044 return error("Invalid parameter attribute record");2045 2046 for (unsigned i = 0, e = Record.size(); i != e; i += 2) {2047 AttrBuilder B(Context);2048 decodeLLVMAttributesForBitcode(B, Record[i+1], Record[i]);2049 Attrs.push_back(AttributeList::get(Context, Record[i], B));2050 }2051 2052 MAttributes.push_back(AttributeList::get(Context, Attrs));2053 Attrs.clear();2054 break;2055 case bitc::PARAMATTR_CODE_ENTRY: // ENTRY: [attrgrp0, attrgrp1, ...]2056 for (uint64_t Val : Record)2057 Attrs.push_back(MAttributeGroups[Val]);2058 2059 MAttributes.push_back(AttributeList::get(Context, Attrs));2060 Attrs.clear();2061 break;2062 }2063 }2064}2065 2066// Returns Attribute::None on unrecognized codes.2067static Attribute::AttrKind getAttrFromCode(uint64_t Code) {2068 switch (Code) {2069 default:2070 return Attribute::None;2071 case bitc::ATTR_KIND_ALIGNMENT:2072 return Attribute::Alignment;2073 case bitc::ATTR_KIND_ALWAYS_INLINE:2074 return Attribute::AlwaysInline;2075 case bitc::ATTR_KIND_BUILTIN:2076 return Attribute::Builtin;2077 case bitc::ATTR_KIND_BY_VAL:2078 return Attribute::ByVal;2079 case bitc::ATTR_KIND_IN_ALLOCA:2080 return Attribute::InAlloca;2081 case bitc::ATTR_KIND_COLD:2082 return Attribute::Cold;2083 case bitc::ATTR_KIND_CONVERGENT:2084 return Attribute::Convergent;2085 case bitc::ATTR_KIND_DISABLE_SANITIZER_INSTRUMENTATION:2086 return Attribute::DisableSanitizerInstrumentation;2087 case bitc::ATTR_KIND_ELEMENTTYPE:2088 return Attribute::ElementType;2089 case bitc::ATTR_KIND_FNRETTHUNK_EXTERN:2090 return Attribute::FnRetThunkExtern;2091 case bitc::ATTR_KIND_INLINE_HINT:2092 return Attribute::InlineHint;2093 case bitc::ATTR_KIND_IN_REG:2094 return Attribute::InReg;2095 case bitc::ATTR_KIND_JUMP_TABLE:2096 return Attribute::JumpTable;2097 case bitc::ATTR_KIND_MEMORY:2098 return Attribute::Memory;2099 case bitc::ATTR_KIND_NOFPCLASS:2100 return Attribute::NoFPClass;2101 case bitc::ATTR_KIND_MIN_SIZE:2102 return Attribute::MinSize;2103 case bitc::ATTR_KIND_NAKED:2104 return Attribute::Naked;2105 case bitc::ATTR_KIND_NEST:2106 return Attribute::Nest;2107 case bitc::ATTR_KIND_NO_ALIAS:2108 return Attribute::NoAlias;2109 case bitc::ATTR_KIND_NO_BUILTIN:2110 return Attribute::NoBuiltin;2111 case bitc::ATTR_KIND_NO_CALLBACK:2112 return Attribute::NoCallback;2113 case bitc::ATTR_KIND_NO_DIVERGENCE_SOURCE:2114 return Attribute::NoDivergenceSource;2115 case bitc::ATTR_KIND_NO_DUPLICATE:2116 return Attribute::NoDuplicate;2117 case bitc::ATTR_KIND_NOFREE:2118 return Attribute::NoFree;2119 case bitc::ATTR_KIND_NO_IMPLICIT_FLOAT:2120 return Attribute::NoImplicitFloat;2121 case bitc::ATTR_KIND_NO_INLINE:2122 return Attribute::NoInline;2123 case bitc::ATTR_KIND_NO_RECURSE:2124 return Attribute::NoRecurse;2125 case bitc::ATTR_KIND_NO_MERGE:2126 return Attribute::NoMerge;2127 case bitc::ATTR_KIND_NON_LAZY_BIND:2128 return Attribute::NonLazyBind;2129 case bitc::ATTR_KIND_NON_NULL:2130 return Attribute::NonNull;2131 case bitc::ATTR_KIND_DEREFERENCEABLE:2132 return Attribute::Dereferenceable;2133 case bitc::ATTR_KIND_DEREFERENCEABLE_OR_NULL:2134 return Attribute::DereferenceableOrNull;2135 case bitc::ATTR_KIND_ALLOC_ALIGN:2136 return Attribute::AllocAlign;2137 case bitc::ATTR_KIND_ALLOC_KIND:2138 return Attribute::AllocKind;2139 case bitc::ATTR_KIND_ALLOC_SIZE:2140 return Attribute::AllocSize;2141 case bitc::ATTR_KIND_ALLOCATED_POINTER:2142 return Attribute::AllocatedPointer;2143 case bitc::ATTR_KIND_NO_RED_ZONE:2144 return Attribute::NoRedZone;2145 case bitc::ATTR_KIND_NO_RETURN:2146 return Attribute::NoReturn;2147 case bitc::ATTR_KIND_NOSYNC:2148 return Attribute::NoSync;2149 case bitc::ATTR_KIND_NOCF_CHECK:2150 return Attribute::NoCfCheck;2151 case bitc::ATTR_KIND_NO_PROFILE:2152 return Attribute::NoProfile;2153 case bitc::ATTR_KIND_SKIP_PROFILE:2154 return Attribute::SkipProfile;2155 case bitc::ATTR_KIND_NO_UNWIND:2156 return Attribute::NoUnwind;2157 case bitc::ATTR_KIND_NO_SANITIZE_BOUNDS:2158 return Attribute::NoSanitizeBounds;2159 case bitc::ATTR_KIND_NO_SANITIZE_COVERAGE:2160 return Attribute::NoSanitizeCoverage;2161 case bitc::ATTR_KIND_NULL_POINTER_IS_VALID:2162 return Attribute::NullPointerIsValid;2163 case bitc::ATTR_KIND_OPTIMIZE_FOR_DEBUGGING:2164 return Attribute::OptimizeForDebugging;2165 case bitc::ATTR_KIND_OPT_FOR_FUZZING:2166 return Attribute::OptForFuzzing;2167 case bitc::ATTR_KIND_OPTIMIZE_FOR_SIZE:2168 return Attribute::OptimizeForSize;2169 case bitc::ATTR_KIND_OPTIMIZE_NONE:2170 return Attribute::OptimizeNone;2171 case bitc::ATTR_KIND_READ_NONE:2172 return Attribute::ReadNone;2173 case bitc::ATTR_KIND_READ_ONLY:2174 return Attribute::ReadOnly;2175 case bitc::ATTR_KIND_RETURNED:2176 return Attribute::Returned;2177 case bitc::ATTR_KIND_RETURNS_TWICE:2178 return Attribute::ReturnsTwice;2179 case bitc::ATTR_KIND_S_EXT:2180 return Attribute::SExt;2181 case bitc::ATTR_KIND_SPECULATABLE:2182 return Attribute::Speculatable;2183 case bitc::ATTR_KIND_STACK_ALIGNMENT:2184 return Attribute::StackAlignment;2185 case bitc::ATTR_KIND_STACK_PROTECT:2186 return Attribute::StackProtect;2187 case bitc::ATTR_KIND_STACK_PROTECT_REQ:2188 return Attribute::StackProtectReq;2189 case bitc::ATTR_KIND_STACK_PROTECT_STRONG:2190 return Attribute::StackProtectStrong;2191 case bitc::ATTR_KIND_SAFESTACK:2192 return Attribute::SafeStack;2193 case bitc::ATTR_KIND_SHADOWCALLSTACK:2194 return Attribute::ShadowCallStack;2195 case bitc::ATTR_KIND_STRICT_FP:2196 return Attribute::StrictFP;2197 case bitc::ATTR_KIND_STRUCT_RET:2198 return Attribute::StructRet;2199 case bitc::ATTR_KIND_SANITIZE_ADDRESS:2200 return Attribute::SanitizeAddress;2201 case bitc::ATTR_KIND_SANITIZE_HWADDRESS:2202 return Attribute::SanitizeHWAddress;2203 case bitc::ATTR_KIND_SANITIZE_THREAD:2204 return Attribute::SanitizeThread;2205 case bitc::ATTR_KIND_SANITIZE_TYPE:2206 return Attribute::SanitizeType;2207 case bitc::ATTR_KIND_SANITIZE_MEMORY:2208 return Attribute::SanitizeMemory;2209 case bitc::ATTR_KIND_SANITIZE_NUMERICAL_STABILITY:2210 return Attribute::SanitizeNumericalStability;2211 case bitc::ATTR_KIND_SANITIZE_REALTIME:2212 return Attribute::SanitizeRealtime;2213 case bitc::ATTR_KIND_SANITIZE_REALTIME_BLOCKING:2214 return Attribute::SanitizeRealtimeBlocking;2215 case bitc::ATTR_KIND_SANITIZE_ALLOC_TOKEN:2216 return Attribute::SanitizeAllocToken;2217 case bitc::ATTR_KIND_SPECULATIVE_LOAD_HARDENING:2218 return Attribute::SpeculativeLoadHardening;2219 case bitc::ATTR_KIND_SWIFT_ERROR:2220 return Attribute::SwiftError;2221 case bitc::ATTR_KIND_SWIFT_SELF:2222 return Attribute::SwiftSelf;2223 case bitc::ATTR_KIND_SWIFT_ASYNC:2224 return Attribute::SwiftAsync;2225 case bitc::ATTR_KIND_UW_TABLE:2226 return Attribute::UWTable;2227 case bitc::ATTR_KIND_VSCALE_RANGE:2228 return Attribute::VScaleRange;2229 case bitc::ATTR_KIND_WILLRETURN:2230 return Attribute::WillReturn;2231 case bitc::ATTR_KIND_WRITEONLY:2232 return Attribute::WriteOnly;2233 case bitc::ATTR_KIND_Z_EXT:2234 return Attribute::ZExt;2235 case bitc::ATTR_KIND_IMMARG:2236 return Attribute::ImmArg;2237 case bitc::ATTR_KIND_SANITIZE_MEMTAG:2238 return Attribute::SanitizeMemTag;2239 case bitc::ATTR_KIND_PREALLOCATED:2240 return Attribute::Preallocated;2241 case bitc::ATTR_KIND_NOUNDEF:2242 return Attribute::NoUndef;2243 case bitc::ATTR_KIND_BYREF:2244 return Attribute::ByRef;2245 case bitc::ATTR_KIND_MUSTPROGRESS:2246 return Attribute::MustProgress;2247 case bitc::ATTR_KIND_HOT:2248 return Attribute::Hot;2249 case bitc::ATTR_KIND_PRESPLIT_COROUTINE:2250 return Attribute::PresplitCoroutine;2251 case bitc::ATTR_KIND_WRITABLE:2252 return Attribute::Writable;2253 case bitc::ATTR_KIND_CORO_ONLY_DESTROY_WHEN_COMPLETE:2254 return Attribute::CoroDestroyOnlyWhenComplete;2255 case bitc::ATTR_KIND_DEAD_ON_UNWIND:2256 return Attribute::DeadOnUnwind;2257 case bitc::ATTR_KIND_RANGE:2258 return Attribute::Range;2259 case bitc::ATTR_KIND_INITIALIZES:2260 return Attribute::Initializes;2261 case bitc::ATTR_KIND_CORO_ELIDE_SAFE:2262 return Attribute::CoroElideSafe;2263 case bitc::ATTR_KIND_NO_EXT:2264 return Attribute::NoExt;2265 case bitc::ATTR_KIND_CAPTURES:2266 return Attribute::Captures;2267 case bitc::ATTR_KIND_DEAD_ON_RETURN:2268 return Attribute::DeadOnReturn;2269 case bitc::ATTR_KIND_NO_CREATE_UNDEF_OR_POISON:2270 return Attribute::NoCreateUndefOrPoison;2271 }2272}2273 2274Error BitcodeReader::parseAlignmentValue(uint64_t Exponent,2275 MaybeAlign &Alignment) {2276 // Note: Alignment in bitcode files is incremented by 1, so that zero2277 // can be used for default alignment.2278 if (Exponent > Value::MaxAlignmentExponent + 1)2279 return error("Invalid alignment value");2280 Alignment = decodeMaybeAlign(Exponent);2281 return Error::success();2282}2283 2284Error BitcodeReader::parseAttrKind(uint64_t Code, Attribute::AttrKind *Kind) {2285 *Kind = getAttrFromCode(Code);2286 if (*Kind == Attribute::None)2287 return error("Unknown attribute kind (" + Twine(Code) + ")");2288 return Error::success();2289}2290 2291static bool upgradeOldMemoryAttribute(MemoryEffects &ME, uint64_t EncodedKind) {2292 switch (EncodedKind) {2293 case bitc::ATTR_KIND_READ_NONE:2294 ME &= MemoryEffects::none();2295 return true;2296 case bitc::ATTR_KIND_READ_ONLY:2297 ME &= MemoryEffects::readOnly();2298 return true;2299 case bitc::ATTR_KIND_WRITEONLY:2300 ME &= MemoryEffects::writeOnly();2301 return true;2302 case bitc::ATTR_KIND_ARGMEMONLY:2303 ME &= MemoryEffects::argMemOnly();2304 return true;2305 case bitc::ATTR_KIND_INACCESSIBLEMEM_ONLY:2306 ME &= MemoryEffects::inaccessibleMemOnly();2307 return true;2308 case bitc::ATTR_KIND_INACCESSIBLEMEM_OR_ARGMEMONLY:2309 ME &= MemoryEffects::inaccessibleOrArgMemOnly();2310 return true;2311 default:2312 return false;2313 }2314}2315 2316Error BitcodeReader::parseAttributeGroupBlock() {2317 if (Error Err = Stream.EnterSubBlock(bitc::PARAMATTR_GROUP_BLOCK_ID))2318 return Err;2319 2320 if (!MAttributeGroups.empty())2321 return error("Invalid multiple blocks");2322 2323 SmallVector<uint64_t, 64> Record;2324 2325 // Read all the records.2326 while (true) {2327 Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks();2328 if (!MaybeEntry)2329 return MaybeEntry.takeError();2330 BitstreamEntry Entry = MaybeEntry.get();2331 2332 switch (Entry.Kind) {2333 case BitstreamEntry::SubBlock: // Handled for us already.2334 case BitstreamEntry::Error:2335 return error("Malformed block");2336 case BitstreamEntry::EndBlock:2337 return Error::success();2338 case BitstreamEntry::Record:2339 // The interesting case.2340 break;2341 }2342 2343 // Read a record.2344 Record.clear();2345 Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record);2346 if (!MaybeRecord)2347 return MaybeRecord.takeError();2348 switch (MaybeRecord.get()) {2349 default: // Default behavior: ignore.2350 break;2351 case bitc::PARAMATTR_GRP_CODE_ENTRY: { // ENTRY: [grpid, idx, a0, a1, ...]2352 if (Record.size() < 3)2353 return error("Invalid grp record");2354 2355 uint64_t GrpID = Record[0];2356 uint64_t Idx = Record[1]; // Index of the object this attribute refers to.2357 2358 AttrBuilder B(Context);2359 MemoryEffects ME = MemoryEffects::unknown();2360 for (unsigned i = 2, e = Record.size(); i != e; ++i) {2361 if (Record[i] == 0) { // Enum attribute2362 Attribute::AttrKind Kind;2363 uint64_t EncodedKind = Record[++i];2364 if (Idx == AttributeList::FunctionIndex &&2365 upgradeOldMemoryAttribute(ME, EncodedKind))2366 continue;2367 2368 if (EncodedKind == bitc::ATTR_KIND_NO_CAPTURE) {2369 B.addCapturesAttr(CaptureInfo::none());2370 continue;2371 }2372 2373 if (Error Err = parseAttrKind(EncodedKind, &Kind))2374 return Err;2375 2376 // Upgrade old-style byval attribute to one with a type, even if it's2377 // nullptr. We will have to insert the real type when we associate2378 // this AttributeList with a function.2379 if (Kind == Attribute::ByVal)2380 B.addByValAttr(nullptr);2381 else if (Kind == Attribute::StructRet)2382 B.addStructRetAttr(nullptr);2383 else if (Kind == Attribute::InAlloca)2384 B.addInAllocaAttr(nullptr);2385 else if (Kind == Attribute::UWTable)2386 B.addUWTableAttr(UWTableKind::Default);2387 else if (Attribute::isEnumAttrKind(Kind))2388 B.addAttribute(Kind);2389 else2390 return error("Not an enum attribute");2391 } else if (Record[i] == 1) { // Integer attribute2392 Attribute::AttrKind Kind;2393 if (Error Err = parseAttrKind(Record[++i], &Kind))2394 return Err;2395 if (!Attribute::isIntAttrKind(Kind))2396 return error("Not an int attribute");2397 if (Kind == Attribute::Alignment)2398 B.addAlignmentAttr(Record[++i]);2399 else if (Kind == Attribute::StackAlignment)2400 B.addStackAlignmentAttr(Record[++i]);2401 else if (Kind == Attribute::Dereferenceable)2402 B.addDereferenceableAttr(Record[++i]);2403 else if (Kind == Attribute::DereferenceableOrNull)2404 B.addDereferenceableOrNullAttr(Record[++i]);2405 else if (Kind == Attribute::AllocSize)2406 B.addAllocSizeAttrFromRawRepr(Record[++i]);2407 else if (Kind == Attribute::VScaleRange)2408 B.addVScaleRangeAttrFromRawRepr(Record[++i]);2409 else if (Kind == Attribute::UWTable)2410 B.addUWTableAttr(UWTableKind(Record[++i]));2411 else if (Kind == Attribute::AllocKind)2412 B.addAllocKindAttr(static_cast<AllocFnKind>(Record[++i]));2413 else if (Kind == Attribute::Memory) {2414 uint64_t EncodedME = Record[++i];2415 const uint8_t Version = (EncodedME >> 56);2416 if (Version == 0) {2417 // Errno memory location was previously encompassed into default2418 // memory. Ensure this is taken into account while reconstructing2419 // the memory attribute prior to its introduction.2420 ModRefInfo ArgMem = ModRefInfo((EncodedME >> 0) & 3);2421 ModRefInfo InaccessibleMem = ModRefInfo((EncodedME >> 2) & 3);2422 ModRefInfo OtherMem = ModRefInfo((EncodedME >> 4) & 3);2423 auto ME = MemoryEffects::inaccessibleMemOnly(InaccessibleMem) |2424 MemoryEffects::argMemOnly(ArgMem) |2425 MemoryEffects::errnoMemOnly(OtherMem) |2426 MemoryEffects::otherMemOnly(OtherMem);2427 B.addMemoryAttr(ME);2428 } else {2429 // Construct the memory attribute directly from the encoded base2430 // on newer versions.2431 B.addMemoryAttr(MemoryEffects::createFromIntValue(2432 EncodedME & 0x00FFFFFFFFFFFFFFULL));2433 }2434 } else if (Kind == Attribute::Captures)2435 B.addCapturesAttr(CaptureInfo::createFromIntValue(Record[++i]));2436 else if (Kind == Attribute::NoFPClass)2437 B.addNoFPClassAttr(2438 static_cast<FPClassTest>(Record[++i] & fcAllFlags));2439 } else if (Record[i] == 3 || Record[i] == 4) { // String attribute2440 bool HasValue = (Record[i++] == 4);2441 SmallString<64> KindStr;2442 SmallString<64> ValStr;2443 2444 while (Record[i] != 0 && i != e)2445 KindStr += Record[i++];2446 assert(Record[i] == 0 && "Kind string not null terminated");2447 2448 if (HasValue) {2449 // Has a value associated with it.2450 ++i; // Skip the '0' that terminates the "kind" string.2451 while (Record[i] != 0 && i != e)2452 ValStr += Record[i++];2453 assert(Record[i] == 0 && "Value string not null terminated");2454 }2455 2456 B.addAttribute(KindStr.str(), ValStr.str());2457 } else if (Record[i] == 5 || Record[i] == 6) {2458 bool HasType = Record[i] == 6;2459 Attribute::AttrKind Kind;2460 if (Error Err = parseAttrKind(Record[++i], &Kind))2461 return Err;2462 if (!Attribute::isTypeAttrKind(Kind))2463 return error("Not a type attribute");2464 2465 B.addTypeAttr(Kind, HasType ? getTypeByID(Record[++i]) : nullptr);2466 } else if (Record[i] == 7) {2467 Attribute::AttrKind Kind;2468 2469 i++;2470 if (Error Err = parseAttrKind(Record[i++], &Kind))2471 return Err;2472 if (!Attribute::isConstantRangeAttrKind(Kind))2473 return error("Not a ConstantRange attribute");2474 2475 Expected<ConstantRange> MaybeCR =2476 readBitWidthAndConstantRange(Record, i);2477 if (!MaybeCR)2478 return MaybeCR.takeError();2479 i--;2480 2481 B.addConstantRangeAttr(Kind, MaybeCR.get());2482 } else if (Record[i] == 8) {2483 Attribute::AttrKind Kind;2484 2485 i++;2486 if (Error Err = parseAttrKind(Record[i++], &Kind))2487 return Err;2488 if (!Attribute::isConstantRangeListAttrKind(Kind))2489 return error("Not a constant range list attribute");2490 2491 SmallVector<ConstantRange, 2> Val;2492 if (i + 2 > e)2493 return error("Too few records for constant range list");2494 unsigned RangeSize = Record[i++];2495 unsigned BitWidth = Record[i++];2496 for (unsigned Idx = 0; Idx < RangeSize; ++Idx) {2497 Expected<ConstantRange> MaybeCR =2498 readConstantRange(Record, i, BitWidth);2499 if (!MaybeCR)2500 return MaybeCR.takeError();2501 Val.push_back(MaybeCR.get());2502 }2503 i--;2504 2505 if (!ConstantRangeList::isOrderedRanges(Val))2506 return error("Invalid (unordered or overlapping) range list");2507 B.addConstantRangeListAttr(Kind, Val);2508 } else {2509 return error("Invalid attribute group entry");2510 }2511 }2512 2513 if (ME != MemoryEffects::unknown())2514 B.addMemoryAttr(ME);2515 2516 UpgradeAttributes(B);2517 MAttributeGroups[GrpID] = AttributeList::get(Context, Idx, B);2518 break;2519 }2520 }2521 }2522}2523 2524Error BitcodeReader::parseTypeTable() {2525 if (Error Err = Stream.EnterSubBlock(bitc::TYPE_BLOCK_ID_NEW))2526 return Err;2527 2528 return parseTypeTableBody();2529}2530 2531Error BitcodeReader::parseTypeTableBody() {2532 if (!TypeList.empty())2533 return error("Invalid multiple blocks");2534 2535 SmallVector<uint64_t, 64> Record;2536 unsigned NumRecords = 0;2537 2538 SmallString<64> TypeName;2539 2540 // Read all the records for this type table.2541 while (true) {2542 Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks();2543 if (!MaybeEntry)2544 return MaybeEntry.takeError();2545 BitstreamEntry Entry = MaybeEntry.get();2546 2547 switch (Entry.Kind) {2548 case BitstreamEntry::SubBlock: // Handled for us already.2549 case BitstreamEntry::Error:2550 return error("Malformed block");2551 case BitstreamEntry::EndBlock:2552 if (NumRecords != TypeList.size())2553 return error("Malformed block");2554 return Error::success();2555 case BitstreamEntry::Record:2556 // The interesting case.2557 break;2558 }2559 2560 // Read a record.2561 Record.clear();2562 Type *ResultTy = nullptr;2563 SmallVector<unsigned> ContainedIDs;2564 Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record);2565 if (!MaybeRecord)2566 return MaybeRecord.takeError();2567 switch (MaybeRecord.get()) {2568 default:2569 return error("Invalid value");2570 case bitc::TYPE_CODE_NUMENTRY: // TYPE_CODE_NUMENTRY: [numentries]2571 // TYPE_CODE_NUMENTRY contains a count of the number of types in the2572 // type list. This allows us to reserve space.2573 if (Record.empty())2574 return error("Invalid numentry record");2575 TypeList.resize(Record[0]);2576 continue;2577 case bitc::TYPE_CODE_VOID: // VOID2578 ResultTy = Type::getVoidTy(Context);2579 break;2580 case bitc::TYPE_CODE_HALF: // HALF2581 ResultTy = Type::getHalfTy(Context);2582 break;2583 case bitc::TYPE_CODE_BFLOAT: // BFLOAT2584 ResultTy = Type::getBFloatTy(Context);2585 break;2586 case bitc::TYPE_CODE_FLOAT: // FLOAT2587 ResultTy = Type::getFloatTy(Context);2588 break;2589 case bitc::TYPE_CODE_DOUBLE: // DOUBLE2590 ResultTy = Type::getDoubleTy(Context);2591 break;2592 case bitc::TYPE_CODE_X86_FP80: // X86_FP802593 ResultTy = Type::getX86_FP80Ty(Context);2594 break;2595 case bitc::TYPE_CODE_FP128: // FP1282596 ResultTy = Type::getFP128Ty(Context);2597 break;2598 case bitc::TYPE_CODE_PPC_FP128: // PPC_FP1282599 ResultTy = Type::getPPC_FP128Ty(Context);2600 break;2601 case bitc::TYPE_CODE_LABEL: // LABEL2602 ResultTy = Type::getLabelTy(Context);2603 break;2604 case bitc::TYPE_CODE_METADATA: // METADATA2605 ResultTy = Type::getMetadataTy(Context);2606 break;2607 case bitc::TYPE_CODE_X86_MMX: // X86_MMX2608 // Deprecated: decodes as <1 x i64>2609 ResultTy =2610 llvm::FixedVectorType::get(llvm::IntegerType::get(Context, 64), 1);2611 break;2612 case bitc::TYPE_CODE_X86_AMX: // X86_AMX2613 ResultTy = Type::getX86_AMXTy(Context);2614 break;2615 case bitc::TYPE_CODE_TOKEN: // TOKEN2616 ResultTy = Type::getTokenTy(Context);2617 break;2618 case bitc::TYPE_CODE_INTEGER: { // INTEGER: [width]2619 if (Record.empty())2620 return error("Invalid integer record");2621 2622 uint64_t NumBits = Record[0];2623 if (NumBits < IntegerType::MIN_INT_BITS ||2624 NumBits > IntegerType::MAX_INT_BITS)2625 return error("Bitwidth for integer type out of range");2626 ResultTy = IntegerType::get(Context, NumBits);2627 break;2628 }2629 case bitc::TYPE_CODE_POINTER: { // POINTER: [pointee type] or2630 // [pointee type, address space]2631 if (Record.empty())2632 return error("Invalid pointer record");2633 unsigned AddressSpace = 0;2634 if (Record.size() == 2)2635 AddressSpace = Record[1];2636 ResultTy = getTypeByID(Record[0]);2637 if (!ResultTy ||2638 !PointerType::isValidElementType(ResultTy))2639 return error("Invalid type");2640 ContainedIDs.push_back(Record[0]);2641 ResultTy = PointerType::get(ResultTy->getContext(), AddressSpace);2642 break;2643 }2644 case bitc::TYPE_CODE_OPAQUE_POINTER: { // OPAQUE_POINTER: [addrspace]2645 if (Record.size() != 1)2646 return error("Invalid opaque pointer record");2647 unsigned AddressSpace = Record[0];2648 ResultTy = PointerType::get(Context, AddressSpace);2649 break;2650 }2651 case bitc::TYPE_CODE_FUNCTION_OLD: {2652 // Deprecated, but still needed to read old bitcode files.2653 // FUNCTION: [vararg, attrid, retty, paramty x N]2654 if (Record.size() < 3)2655 return error("Invalid function record");2656 SmallVector<Type*, 8> ArgTys;2657 for (unsigned i = 3, e = Record.size(); i != e; ++i) {2658 if (Type *T = getTypeByID(Record[i]))2659 ArgTys.push_back(T);2660 else2661 break;2662 }2663 2664 ResultTy = getTypeByID(Record[2]);2665 if (!ResultTy || ArgTys.size() < Record.size()-3)2666 return error("Invalid type");2667 2668 ContainedIDs.append(Record.begin() + 2, Record.end());2669 ResultTy = FunctionType::get(ResultTy, ArgTys, Record[0]);2670 break;2671 }2672 case bitc::TYPE_CODE_FUNCTION: {2673 // FUNCTION: [vararg, retty, paramty x N]2674 if (Record.size() < 2)2675 return error("Invalid function record");2676 SmallVector<Type*, 8> ArgTys;2677 for (unsigned i = 2, e = Record.size(); i != e; ++i) {2678 if (Type *T = getTypeByID(Record[i])) {2679 if (!FunctionType::isValidArgumentType(T))2680 return error("Invalid function argument type");2681 ArgTys.push_back(T);2682 }2683 else2684 break;2685 }2686 2687 ResultTy = getTypeByID(Record[1]);2688 if (!ResultTy || ArgTys.size() < Record.size()-2)2689 return error("Invalid type");2690 2691 ContainedIDs.append(Record.begin() + 1, Record.end());2692 ResultTy = FunctionType::get(ResultTy, ArgTys, Record[0]);2693 break;2694 }2695 case bitc::TYPE_CODE_STRUCT_ANON: { // STRUCT: [ispacked, eltty x N]2696 if (Record.empty())2697 return error("Invalid anon struct record");2698 SmallVector<Type*, 8> EltTys;2699 for (unsigned i = 1, e = Record.size(); i != e; ++i) {2700 if (Type *T = getTypeByID(Record[i]))2701 EltTys.push_back(T);2702 else2703 break;2704 }2705 if (EltTys.size() != Record.size()-1)2706 return error("Invalid type");2707 ContainedIDs.append(Record.begin() + 1, Record.end());2708 ResultTy = StructType::get(Context, EltTys, Record[0]);2709 break;2710 }2711 case bitc::TYPE_CODE_STRUCT_NAME: // STRUCT_NAME: [strchr x N]2712 if (convertToString(Record, 0, TypeName))2713 return error("Invalid struct name record");2714 continue;2715 2716 case bitc::TYPE_CODE_STRUCT_NAMED: { // STRUCT: [ispacked, eltty x N]2717 if (Record.empty())2718 return error("Invalid named struct record");2719 2720 if (NumRecords >= TypeList.size())2721 return error("Invalid TYPE table");2722 2723 // Check to see if this was forward referenced, if so fill in the temp.2724 StructType *Res = cast_or_null<StructType>(TypeList[NumRecords]);2725 if (Res) {2726 Res->setName(TypeName);2727 TypeList[NumRecords] = nullptr;2728 } else // Otherwise, create a new struct.2729 Res = createIdentifiedStructType(Context, TypeName);2730 TypeName.clear();2731 2732 SmallVector<Type*, 8> EltTys;2733 for (unsigned i = 1, e = Record.size(); i != e; ++i) {2734 if (Type *T = getTypeByID(Record[i]))2735 EltTys.push_back(T);2736 else2737 break;2738 }2739 if (EltTys.size() != Record.size()-1)2740 return error("Invalid named struct record");2741 if (auto E = Res->setBodyOrError(EltTys, Record[0]))2742 return E;2743 ContainedIDs.append(Record.begin() + 1, Record.end());2744 ResultTy = Res;2745 break;2746 }2747 case bitc::TYPE_CODE_OPAQUE: { // OPAQUE: []2748 if (Record.size() != 1)2749 return error("Invalid opaque type record");2750 2751 if (NumRecords >= TypeList.size())2752 return error("Invalid TYPE table");2753 2754 // Check to see if this was forward referenced, if so fill in the temp.2755 StructType *Res = cast_or_null<StructType>(TypeList[NumRecords]);2756 if (Res) {2757 Res->setName(TypeName);2758 TypeList[NumRecords] = nullptr;2759 } else // Otherwise, create a new struct with no body.2760 Res = createIdentifiedStructType(Context, TypeName);2761 TypeName.clear();2762 ResultTy = Res;2763 break;2764 }2765 case bitc::TYPE_CODE_TARGET_TYPE: { // TARGET_TYPE: [NumTy, Tys..., Ints...]2766 if (Record.size() < 1)2767 return error("Invalid target extension type record");2768 2769 if (NumRecords >= TypeList.size())2770 return error("Invalid TYPE table");2771 2772 if (Record[0] >= Record.size())2773 return error("Too many type parameters");2774 2775 unsigned NumTys = Record[0];2776 SmallVector<Type *, 4> TypeParams;2777 SmallVector<unsigned, 8> IntParams;2778 for (unsigned i = 0; i < NumTys; i++) {2779 if (Type *T = getTypeByID(Record[i + 1]))2780 TypeParams.push_back(T);2781 else2782 return error("Invalid type");2783 }2784 2785 for (unsigned i = NumTys + 1, e = Record.size(); i < e; i++) {2786 if (Record[i] > UINT_MAX)2787 return error("Integer parameter too large");2788 IntParams.push_back(Record[i]);2789 }2790 auto TTy =2791 TargetExtType::getOrError(Context, TypeName, TypeParams, IntParams);2792 if (auto E = TTy.takeError())2793 return E;2794 ResultTy = *TTy;2795 TypeName.clear();2796 break;2797 }2798 case bitc::TYPE_CODE_ARRAY: // ARRAY: [numelts, eltty]2799 if (Record.size() < 2)2800 return error("Invalid array type record");2801 ResultTy = getTypeByID(Record[1]);2802 if (!ResultTy || !ArrayType::isValidElementType(ResultTy))2803 return error("Invalid type");2804 ContainedIDs.push_back(Record[1]);2805 ResultTy = ArrayType::get(ResultTy, Record[0]);2806 break;2807 case bitc::TYPE_CODE_VECTOR: // VECTOR: [numelts, eltty] or2808 // [numelts, eltty, scalable]2809 if (Record.size() < 2)2810 return error("Invalid vector type record");2811 if (Record[0] == 0)2812 return error("Invalid vector length");2813 ResultTy = getTypeByID(Record[1]);2814 if (!ResultTy || !VectorType::isValidElementType(ResultTy))2815 return error("Invalid type");2816 bool Scalable = Record.size() > 2 ? Record[2] : false;2817 ContainedIDs.push_back(Record[1]);2818 ResultTy = VectorType::get(ResultTy, Record[0], Scalable);2819 break;2820 }2821 2822 if (NumRecords >= TypeList.size())2823 return error("Invalid TYPE table");2824 if (TypeList[NumRecords])2825 return error(2826 "Invalid TYPE table: Only named structs can be forward referenced");2827 assert(ResultTy && "Didn't read a type?");2828 TypeList[NumRecords] = ResultTy;2829 if (!ContainedIDs.empty())2830 ContainedTypeIDs[NumRecords] = std::move(ContainedIDs);2831 ++NumRecords;2832 }2833}2834 2835Error BitcodeReader::parseOperandBundleTags() {2836 if (Error Err = Stream.EnterSubBlock(bitc::OPERAND_BUNDLE_TAGS_BLOCK_ID))2837 return Err;2838 2839 if (!BundleTags.empty())2840 return error("Invalid multiple blocks");2841 2842 SmallVector<uint64_t, 64> Record;2843 2844 while (true) {2845 Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks();2846 if (!MaybeEntry)2847 return MaybeEntry.takeError();2848 BitstreamEntry Entry = MaybeEntry.get();2849 2850 switch (Entry.Kind) {2851 case BitstreamEntry::SubBlock: // Handled for us already.2852 case BitstreamEntry::Error:2853 return error("Malformed block");2854 case BitstreamEntry::EndBlock:2855 return Error::success();2856 case BitstreamEntry::Record:2857 // The interesting case.2858 break;2859 }2860 2861 // Tags are implicitly mapped to integers by their order.2862 2863 Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record);2864 if (!MaybeRecord)2865 return MaybeRecord.takeError();2866 if (MaybeRecord.get() != bitc::OPERAND_BUNDLE_TAG)2867 return error("Invalid operand bundle record");2868 2869 // OPERAND_BUNDLE_TAG: [strchr x N]2870 BundleTags.emplace_back();2871 if (convertToString(Record, 0, BundleTags.back()))2872 return error("Invalid operand bundle record");2873 Record.clear();2874 }2875}2876 2877Error BitcodeReader::parseSyncScopeNames() {2878 if (Error Err = Stream.EnterSubBlock(bitc::SYNC_SCOPE_NAMES_BLOCK_ID))2879 return Err;2880 2881 if (!SSIDs.empty())2882 return error("Invalid multiple synchronization scope names blocks");2883 2884 SmallVector<uint64_t, 64> Record;2885 while (true) {2886 Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks();2887 if (!MaybeEntry)2888 return MaybeEntry.takeError();2889 BitstreamEntry Entry = MaybeEntry.get();2890 2891 switch (Entry.Kind) {2892 case BitstreamEntry::SubBlock: // Handled for us already.2893 case BitstreamEntry::Error:2894 return error("Malformed block");2895 case BitstreamEntry::EndBlock:2896 if (SSIDs.empty())2897 return error("Invalid empty synchronization scope names block");2898 return Error::success();2899 case BitstreamEntry::Record:2900 // The interesting case.2901 break;2902 }2903 2904 // Synchronization scope names are implicitly mapped to synchronization2905 // scope IDs by their order.2906 2907 Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record);2908 if (!MaybeRecord)2909 return MaybeRecord.takeError();2910 if (MaybeRecord.get() != bitc::SYNC_SCOPE_NAME)2911 return error("Invalid sync scope record");2912 2913 SmallString<16> SSN;2914 if (convertToString(Record, 0, SSN))2915 return error("Invalid sync scope record");2916 2917 SSIDs.push_back(Context.getOrInsertSyncScopeID(SSN));2918 Record.clear();2919 }2920}2921 2922/// Associate a value with its name from the given index in the provided record.2923Expected<Value *> BitcodeReader::recordValue(SmallVectorImpl<uint64_t> &Record,2924 unsigned NameIndex, Triple &TT) {2925 SmallString<128> ValueName;2926 if (convertToString(Record, NameIndex, ValueName))2927 return error("Invalid record");2928 unsigned ValueID = Record[0];2929 if (ValueID >= ValueList.size() || !ValueList[ValueID])2930 return error("Invalid record");2931 Value *V = ValueList[ValueID];2932 2933 StringRef NameStr(ValueName.data(), ValueName.size());2934 if (NameStr.contains(0))2935 return error("Invalid value name");2936 V->setName(NameStr);2937 auto *GO = dyn_cast<GlobalObject>(V);2938 if (GO && ImplicitComdatObjects.contains(GO) && TT.supportsCOMDAT())2939 GO->setComdat(TheModule->getOrInsertComdat(V->getName()));2940 return V;2941}2942 2943/// Helper to note and return the current location, and jump to the given2944/// offset.2945static Expected<uint64_t> jumpToValueSymbolTable(uint64_t Offset,2946 BitstreamCursor &Stream) {2947 // Save the current parsing location so we can jump back at the end2948 // of the VST read.2949 uint64_t CurrentBit = Stream.GetCurrentBitNo();2950 if (Error JumpFailed = Stream.JumpToBit(Offset * 32))2951 return std::move(JumpFailed);2952 Expected<BitstreamEntry> MaybeEntry = Stream.advance();2953 if (!MaybeEntry)2954 return MaybeEntry.takeError();2955 if (MaybeEntry.get().Kind != BitstreamEntry::SubBlock ||2956 MaybeEntry.get().ID != bitc::VALUE_SYMTAB_BLOCK_ID)2957 return error("Expected value symbol table subblock");2958 return CurrentBit;2959}2960 2961void BitcodeReader::setDeferredFunctionInfo(unsigned FuncBitcodeOffsetDelta,2962 Function *F,2963 ArrayRef<uint64_t> Record) {2964 // Note that we subtract 1 here because the offset is relative to one word2965 // before the start of the identification or module block, which was2966 // historically always the start of the regular bitcode header.2967 uint64_t FuncWordOffset = Record[1] - 1;2968 uint64_t FuncBitOffset = FuncWordOffset * 32;2969 DeferredFunctionInfo[F] = FuncBitOffset + FuncBitcodeOffsetDelta;2970 // Set the LastFunctionBlockBit to point to the last function block.2971 // Later when parsing is resumed after function materialization,2972 // we can simply skip that last function block.2973 if (FuncBitOffset > LastFunctionBlockBit)2974 LastFunctionBlockBit = FuncBitOffset;2975}2976 2977/// Read a new-style GlobalValue symbol table.2978Error BitcodeReader::parseGlobalValueSymbolTable() {2979 unsigned FuncBitcodeOffsetDelta =2980 Stream.getAbbrevIDWidth() + bitc::BlockIDWidth;2981 2982 if (Error Err = Stream.EnterSubBlock(bitc::VALUE_SYMTAB_BLOCK_ID))2983 return Err;2984 2985 SmallVector<uint64_t, 64> Record;2986 while (true) {2987 Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks();2988 if (!MaybeEntry)2989 return MaybeEntry.takeError();2990 BitstreamEntry Entry = MaybeEntry.get();2991 2992 switch (Entry.Kind) {2993 case BitstreamEntry::SubBlock:2994 case BitstreamEntry::Error:2995 return error("Malformed block");2996 case BitstreamEntry::EndBlock:2997 return Error::success();2998 case BitstreamEntry::Record:2999 break;3000 }3001 3002 Record.clear();3003 Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record);3004 if (!MaybeRecord)3005 return MaybeRecord.takeError();3006 switch (MaybeRecord.get()) {3007 case bitc::VST_CODE_FNENTRY: { // [valueid, offset]3008 unsigned ValueID = Record[0];3009 if (ValueID >= ValueList.size() || !ValueList[ValueID])3010 return error("Invalid value reference in symbol table");3011 setDeferredFunctionInfo(FuncBitcodeOffsetDelta,3012 cast<Function>(ValueList[ValueID]), Record);3013 break;3014 }3015 }3016 }3017}3018 3019/// Parse the value symbol table at either the current parsing location or3020/// at the given bit offset if provided.3021Error BitcodeReader::parseValueSymbolTable(uint64_t Offset) {3022 uint64_t CurrentBit;3023 // Pass in the Offset to distinguish between calling for the module-level3024 // VST (where we want to jump to the VST offset) and the function-level3025 // VST (where we don't).3026 if (Offset > 0) {3027 Expected<uint64_t> MaybeCurrentBit = jumpToValueSymbolTable(Offset, Stream);3028 if (!MaybeCurrentBit)3029 return MaybeCurrentBit.takeError();3030 CurrentBit = MaybeCurrentBit.get();3031 // If this module uses a string table, read this as a module-level VST.3032 if (UseStrtab) {3033 if (Error Err = parseGlobalValueSymbolTable())3034 return Err;3035 if (Error JumpFailed = Stream.JumpToBit(CurrentBit))3036 return JumpFailed;3037 return Error::success();3038 }3039 // Otherwise, the VST will be in a similar format to a function-level VST,3040 // and will contain symbol names.3041 }3042 3043 // Compute the delta between the bitcode indices in the VST (the word offset3044 // to the word-aligned ENTER_SUBBLOCK for the function block, and that3045 // expected by the lazy reader. The reader's EnterSubBlock expects to have3046 // already read the ENTER_SUBBLOCK code (size getAbbrevIDWidth) and BlockID3047 // (size BlockIDWidth). Note that we access the stream's AbbrevID width here3048 // just before entering the VST subblock because: 1) the EnterSubBlock3049 // changes the AbbrevID width; 2) the VST block is nested within the same3050 // outer MODULE_BLOCK as the FUNCTION_BLOCKs and therefore have the same3051 // AbbrevID width before calling EnterSubBlock; and 3) when we want to3052 // jump to the FUNCTION_BLOCK using this offset later, we don't want3053 // to rely on the stream's AbbrevID width being that of the MODULE_BLOCK.3054 unsigned FuncBitcodeOffsetDelta =3055 Stream.getAbbrevIDWidth() + bitc::BlockIDWidth;3056 3057 if (Error Err = Stream.EnterSubBlock(bitc::VALUE_SYMTAB_BLOCK_ID))3058 return Err;3059 3060 SmallVector<uint64_t, 64> Record;3061 3062 Triple TT(TheModule->getTargetTriple());3063 3064 // Read all the records for this value table.3065 SmallString<128> ValueName;3066 3067 while (true) {3068 Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks();3069 if (!MaybeEntry)3070 return MaybeEntry.takeError();3071 BitstreamEntry Entry = MaybeEntry.get();3072 3073 switch (Entry.Kind) {3074 case BitstreamEntry::SubBlock: // Handled for us already.3075 case BitstreamEntry::Error:3076 return error("Malformed block");3077 case BitstreamEntry::EndBlock:3078 if (Offset > 0)3079 if (Error JumpFailed = Stream.JumpToBit(CurrentBit))3080 return JumpFailed;3081 return Error::success();3082 case BitstreamEntry::Record:3083 // The interesting case.3084 break;3085 }3086 3087 // Read a record.3088 Record.clear();3089 Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record);3090 if (!MaybeRecord)3091 return MaybeRecord.takeError();3092 switch (MaybeRecord.get()) {3093 default: // Default behavior: unknown type.3094 break;3095 case bitc::VST_CODE_ENTRY: { // VST_CODE_ENTRY: [valueid, namechar x N]3096 Expected<Value *> ValOrErr = recordValue(Record, 1, TT);3097 if (Error Err = ValOrErr.takeError())3098 return Err;3099 ValOrErr.get();3100 break;3101 }3102 case bitc::VST_CODE_FNENTRY: {3103 // VST_CODE_FNENTRY: [valueid, offset, namechar x N]3104 Expected<Value *> ValOrErr = recordValue(Record, 2, TT);3105 if (Error Err = ValOrErr.takeError())3106 return Err;3107 Value *V = ValOrErr.get();3108 3109 // Ignore function offsets emitted for aliases of functions in older3110 // versions of LLVM.3111 if (auto *F = dyn_cast<Function>(V))3112 setDeferredFunctionInfo(FuncBitcodeOffsetDelta, F, Record);3113 break;3114 }3115 case bitc::VST_CODE_BBENTRY: {3116 if (convertToString(Record, 1, ValueName))3117 return error("Invalid bbentry record");3118 BasicBlock *BB = getBasicBlock(Record[0]);3119 if (!BB)3120 return error("Invalid bbentry record");3121 3122 BB->setName(ValueName.str());3123 ValueName.clear();3124 break;3125 }3126 }3127 }3128}3129 3130/// Decode a signed value stored with the sign bit in the LSB for dense VBR3131/// encoding.3132uint64_t BitcodeReader::decodeSignRotatedValue(uint64_t V) {3133 if ((V & 1) == 0)3134 return V >> 1;3135 if (V != 1)3136 return -(V >> 1);3137 // There is no such thing as -0 with integers. "-0" really means MININT.3138 return 1ULL << 63;3139}3140 3141/// Resolve all of the initializers for global values and aliases that we can.3142Error BitcodeReader::resolveGlobalAndIndirectSymbolInits() {3143 std::vector<std::pair<GlobalVariable *, unsigned>> GlobalInitWorklist;3144 std::vector<std::pair<GlobalValue *, unsigned>> IndirectSymbolInitWorklist;3145 std::vector<FunctionOperandInfo> FunctionOperandWorklist;3146 3147 GlobalInitWorklist.swap(GlobalInits);3148 IndirectSymbolInitWorklist.swap(IndirectSymbolInits);3149 FunctionOperandWorklist.swap(FunctionOperands);3150 3151 while (!GlobalInitWorklist.empty()) {3152 unsigned ValID = GlobalInitWorklist.back().second;3153 if (ValID >= ValueList.size()) {3154 // Not ready to resolve this yet, it requires something later in the file.3155 GlobalInits.push_back(GlobalInitWorklist.back());3156 } else {3157 Expected<Constant *> MaybeC = getValueForInitializer(ValID);3158 if (!MaybeC)3159 return MaybeC.takeError();3160 GlobalInitWorklist.back().first->setInitializer(MaybeC.get());3161 }3162 GlobalInitWorklist.pop_back();3163 }3164 3165 while (!IndirectSymbolInitWorklist.empty()) {3166 unsigned ValID = IndirectSymbolInitWorklist.back().second;3167 if (ValID >= ValueList.size()) {3168 IndirectSymbolInits.push_back(IndirectSymbolInitWorklist.back());3169 } else {3170 Expected<Constant *> MaybeC = getValueForInitializer(ValID);3171 if (!MaybeC)3172 return MaybeC.takeError();3173 Constant *C = MaybeC.get();3174 GlobalValue *GV = IndirectSymbolInitWorklist.back().first;3175 if (auto *GA = dyn_cast<GlobalAlias>(GV)) {3176 if (C->getType() != GV->getType())3177 return error("Alias and aliasee types don't match");3178 GA->setAliasee(C);3179 } else if (auto *GI = dyn_cast<GlobalIFunc>(GV)) {3180 GI->setResolver(C);3181 } else {3182 return error("Expected an alias or an ifunc");3183 }3184 }3185 IndirectSymbolInitWorklist.pop_back();3186 }3187 3188 while (!FunctionOperandWorklist.empty()) {3189 FunctionOperandInfo &Info = FunctionOperandWorklist.back();3190 if (Info.PersonalityFn) {3191 unsigned ValID = Info.PersonalityFn - 1;3192 if (ValID < ValueList.size()) {3193 Expected<Constant *> MaybeC = getValueForInitializer(ValID);3194 if (!MaybeC)3195 return MaybeC.takeError();3196 Info.F->setPersonalityFn(MaybeC.get());3197 Info.PersonalityFn = 0;3198 }3199 }3200 if (Info.Prefix) {3201 unsigned ValID = Info.Prefix - 1;3202 if (ValID < ValueList.size()) {3203 Expected<Constant *> MaybeC = getValueForInitializer(ValID);3204 if (!MaybeC)3205 return MaybeC.takeError();3206 Info.F->setPrefixData(MaybeC.get());3207 Info.Prefix = 0;3208 }3209 }3210 if (Info.Prologue) {3211 unsigned ValID = Info.Prologue - 1;3212 if (ValID < ValueList.size()) {3213 Expected<Constant *> MaybeC = getValueForInitializer(ValID);3214 if (!MaybeC)3215 return MaybeC.takeError();3216 Info.F->setPrologueData(MaybeC.get());3217 Info.Prologue = 0;3218 }3219 }3220 if (Info.PersonalityFn || Info.Prefix || Info.Prologue)3221 FunctionOperands.push_back(Info);3222 FunctionOperandWorklist.pop_back();3223 }3224 3225 return Error::success();3226}3227 3228APInt llvm::readWideAPInt(ArrayRef<uint64_t> Vals, unsigned TypeBits) {3229 SmallVector<uint64_t, 8> Words(Vals.size());3230 transform(Vals, Words.begin(),3231 BitcodeReader::decodeSignRotatedValue);3232 3233 return APInt(TypeBits, Words);3234}3235 3236Error BitcodeReader::parseConstants() {3237 if (Error Err = Stream.EnterSubBlock(bitc::CONSTANTS_BLOCK_ID))3238 return Err;3239 3240 SmallVector<uint64_t, 64> Record;3241 3242 // Read all the records for this value table.3243 Type *CurTy = Type::getInt32Ty(Context);3244 unsigned Int32TyID = getVirtualTypeID(CurTy);3245 unsigned CurTyID = Int32TyID;3246 Type *CurElemTy = nullptr;3247 unsigned NextCstNo = ValueList.size();3248 3249 while (true) {3250 Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks();3251 if (!MaybeEntry)3252 return MaybeEntry.takeError();3253 BitstreamEntry Entry = MaybeEntry.get();3254 3255 switch (Entry.Kind) {3256 case BitstreamEntry::SubBlock: // Handled for us already.3257 case BitstreamEntry::Error:3258 return error("Malformed block");3259 case BitstreamEntry::EndBlock:3260 if (NextCstNo != ValueList.size())3261 return error("Invalid constant reference");3262 return Error::success();3263 case BitstreamEntry::Record:3264 // The interesting case.3265 break;3266 }3267 3268 // Read a record.3269 Record.clear();3270 Type *VoidType = Type::getVoidTy(Context);3271 Value *V = nullptr;3272 Expected<unsigned> MaybeBitCode = Stream.readRecord(Entry.ID, Record);3273 if (!MaybeBitCode)3274 return MaybeBitCode.takeError();3275 switch (unsigned BitCode = MaybeBitCode.get()) {3276 default: // Default behavior: unknown constant3277 case bitc::CST_CODE_UNDEF: // UNDEF3278 V = UndefValue::get(CurTy);3279 break;3280 case bitc::CST_CODE_POISON: // POISON3281 V = PoisonValue::get(CurTy);3282 break;3283 case bitc::CST_CODE_SETTYPE: // SETTYPE: [typeid]3284 if (Record.empty())3285 return error("Invalid settype record");3286 if (Record[0] >= TypeList.size() || !TypeList[Record[0]])3287 return error("Invalid settype record");3288 if (TypeList[Record[0]] == VoidType)3289 return error("Invalid constant type");3290 CurTyID = Record[0];3291 CurTy = TypeList[CurTyID];3292 CurElemTy = getPtrElementTypeByID(CurTyID);3293 continue; // Skip the ValueList manipulation.3294 case bitc::CST_CODE_NULL: // NULL3295 if (CurTy->isVoidTy() || CurTy->isFunctionTy() || CurTy->isLabelTy())3296 return error("Invalid type for a constant null value");3297 if (auto *TETy = dyn_cast<TargetExtType>(CurTy))3298 if (!TETy->hasProperty(TargetExtType::HasZeroInit))3299 return error("Invalid type for a constant null value");3300 V = Constant::getNullValue(CurTy);3301 break;3302 case bitc::CST_CODE_INTEGER: // INTEGER: [intval]3303 if (!CurTy->isIntOrIntVectorTy() || Record.empty())3304 return error("Invalid integer const record");3305 V = ConstantInt::get(CurTy, decodeSignRotatedValue(Record[0]));3306 break;3307 case bitc::CST_CODE_WIDE_INTEGER: {// WIDE_INTEGER: [n x intval]3308 if (!CurTy->isIntOrIntVectorTy() || Record.empty())3309 return error("Invalid wide integer const record");3310 3311 auto *ScalarTy = cast<IntegerType>(CurTy->getScalarType());3312 APInt VInt = readWideAPInt(Record, ScalarTy->getBitWidth());3313 V = ConstantInt::get(CurTy, VInt);3314 break;3315 }3316 case bitc::CST_CODE_FLOAT: { // FLOAT: [fpval]3317 if (Record.empty())3318 return error("Invalid float const record");3319 3320 auto *ScalarTy = CurTy->getScalarType();3321 if (ScalarTy->isHalfTy())3322 V = ConstantFP::get(CurTy, APFloat(APFloat::IEEEhalf(),3323 APInt(16, (uint16_t)Record[0])));3324 else if (ScalarTy->isBFloatTy())3325 V = ConstantFP::get(3326 CurTy, APFloat(APFloat::BFloat(), APInt(16, (uint32_t)Record[0])));3327 else if (ScalarTy->isFloatTy())3328 V = ConstantFP::get(CurTy, APFloat(APFloat::IEEEsingle(),3329 APInt(32, (uint32_t)Record[0])));3330 else if (ScalarTy->isDoubleTy())3331 V = ConstantFP::get(3332 CurTy, APFloat(APFloat::IEEEdouble(), APInt(64, Record[0])));3333 else if (ScalarTy->isX86_FP80Ty()) {3334 // Bits are not stored the same way as a normal i80 APInt, compensate.3335 uint64_t Rearrange[2];3336 Rearrange[0] = (Record[1] & 0xffffLL) | (Record[0] << 16);3337 Rearrange[1] = Record[0] >> 48;3338 V = ConstantFP::get(3339 CurTy, APFloat(APFloat::x87DoubleExtended(), APInt(80, Rearrange)));3340 } else if (ScalarTy->isFP128Ty())3341 V = ConstantFP::get(CurTy,3342 APFloat(APFloat::IEEEquad(), APInt(128, Record)));3343 else if (ScalarTy->isPPC_FP128Ty())3344 V = ConstantFP::get(3345 CurTy, APFloat(APFloat::PPCDoubleDouble(), APInt(128, Record)));3346 else3347 V = PoisonValue::get(CurTy);3348 break;3349 }3350 3351 case bitc::CST_CODE_AGGREGATE: {// AGGREGATE: [n x value number]3352 if (Record.empty())3353 return error("Invalid aggregate record");3354 3355 SmallVector<unsigned, 16> Elts;3356 llvm::append_range(Elts, Record);3357 3358 if (isa<StructType>(CurTy)) {3359 V = BitcodeConstant::create(3360 Alloc, CurTy, BitcodeConstant::ConstantStructOpcode, Elts);3361 } else if (isa<ArrayType>(CurTy)) {3362 V = BitcodeConstant::create(Alloc, CurTy,3363 BitcodeConstant::ConstantArrayOpcode, Elts);3364 } else if (isa<VectorType>(CurTy)) {3365 V = BitcodeConstant::create(3366 Alloc, CurTy, BitcodeConstant::ConstantVectorOpcode, Elts);3367 } else {3368 V = PoisonValue::get(CurTy);3369 }3370 break;3371 }3372 case bitc::CST_CODE_STRING: // STRING: [values]3373 case bitc::CST_CODE_CSTRING: { // CSTRING: [values]3374 if (Record.empty())3375 return error("Invalid string record");3376 3377 SmallString<16> Elts(Record.begin(), Record.end());3378 V = ConstantDataArray::getString(Context, Elts,3379 BitCode == bitc::CST_CODE_CSTRING);3380 break;3381 }3382 case bitc::CST_CODE_DATA: {// DATA: [n x value]3383 if (Record.empty())3384 return error("Invalid data record");3385 3386 Type *EltTy;3387 if (auto *Array = dyn_cast<ArrayType>(CurTy))3388 EltTy = Array->getElementType();3389 else3390 EltTy = cast<VectorType>(CurTy)->getElementType();3391 if (EltTy->isIntegerTy(8)) {3392 SmallVector<uint8_t, 16> Elts(Record.begin(), Record.end());3393 if (isa<VectorType>(CurTy))3394 V = ConstantDataVector::get(Context, Elts);3395 else3396 V = ConstantDataArray::get(Context, Elts);3397 } else if (EltTy->isIntegerTy(16)) {3398 SmallVector<uint16_t, 16> Elts(Record.begin(), Record.end());3399 if (isa<VectorType>(CurTy))3400 V = ConstantDataVector::get(Context, Elts);3401 else3402 V = ConstantDataArray::get(Context, Elts);3403 } else if (EltTy->isIntegerTy(32)) {3404 SmallVector<uint32_t, 16> Elts(Record.begin(), Record.end());3405 if (isa<VectorType>(CurTy))3406 V = ConstantDataVector::get(Context, Elts);3407 else3408 V = ConstantDataArray::get(Context, Elts);3409 } else if (EltTy->isIntegerTy(64)) {3410 SmallVector<uint64_t, 16> Elts(Record.begin(), Record.end());3411 if (isa<VectorType>(CurTy))3412 V = ConstantDataVector::get(Context, Elts);3413 else3414 V = ConstantDataArray::get(Context, Elts);3415 } else if (EltTy->isHalfTy()) {3416 SmallVector<uint16_t, 16> Elts(Record.begin(), Record.end());3417 if (isa<VectorType>(CurTy))3418 V = ConstantDataVector::getFP(EltTy, Elts);3419 else3420 V = ConstantDataArray::getFP(EltTy, Elts);3421 } else if (EltTy->isBFloatTy()) {3422 SmallVector<uint16_t, 16> Elts(Record.begin(), Record.end());3423 if (isa<VectorType>(CurTy))3424 V = ConstantDataVector::getFP(EltTy, Elts);3425 else3426 V = ConstantDataArray::getFP(EltTy, Elts);3427 } else if (EltTy->isFloatTy()) {3428 SmallVector<uint32_t, 16> Elts(Record.begin(), Record.end());3429 if (isa<VectorType>(CurTy))3430 V = ConstantDataVector::getFP(EltTy, Elts);3431 else3432 V = ConstantDataArray::getFP(EltTy, Elts);3433 } else if (EltTy->isDoubleTy()) {3434 SmallVector<uint64_t, 16> Elts(Record.begin(), Record.end());3435 if (isa<VectorType>(CurTy))3436 V = ConstantDataVector::getFP(EltTy, Elts);3437 else3438 V = ConstantDataArray::getFP(EltTy, Elts);3439 } else {3440 return error("Invalid type for value");3441 }3442 break;3443 }3444 case bitc::CST_CODE_CE_UNOP: { // CE_UNOP: [opcode, opval]3445 if (Record.size() < 2)3446 return error("Invalid unary op constexpr record");3447 int Opc = getDecodedUnaryOpcode(Record[0], CurTy);3448 if (Opc < 0) {3449 V = PoisonValue::get(CurTy); // Unknown unop.3450 } else {3451 V = BitcodeConstant::create(Alloc, CurTy, Opc, (unsigned)Record[1]);3452 }3453 break;3454 }3455 case bitc::CST_CODE_CE_BINOP: { // CE_BINOP: [opcode, opval, opval]3456 if (Record.size() < 3)3457 return error("Invalid binary op constexpr record");3458 int Opc = getDecodedBinaryOpcode(Record[0], CurTy);3459 if (Opc < 0) {3460 V = PoisonValue::get(CurTy); // Unknown binop.3461 } else {3462 uint8_t Flags = 0;3463 if (Record.size() >= 4) {3464 if (Opc == Instruction::Add ||3465 Opc == Instruction::Sub ||3466 Opc == Instruction::Mul ||3467 Opc == Instruction::Shl) {3468 if (Record[3] & (1 << bitc::OBO_NO_SIGNED_WRAP))3469 Flags |= OverflowingBinaryOperator::NoSignedWrap;3470 if (Record[3] & (1 << bitc::OBO_NO_UNSIGNED_WRAP))3471 Flags |= OverflowingBinaryOperator::NoUnsignedWrap;3472 } else if (Opc == Instruction::SDiv ||3473 Opc == Instruction::UDiv ||3474 Opc == Instruction::LShr ||3475 Opc == Instruction::AShr) {3476 if (Record[3] & (1 << bitc::PEO_EXACT))3477 Flags |= PossiblyExactOperator::IsExact;3478 }3479 }3480 V = BitcodeConstant::create(Alloc, CurTy, {(uint8_t)Opc, Flags},3481 {(unsigned)Record[1], (unsigned)Record[2]});3482 }3483 break;3484 }3485 case bitc::CST_CODE_CE_CAST: { // CE_CAST: [opcode, opty, opval]3486 if (Record.size() < 3)3487 return error("Invalid cast constexpr record");3488 int Opc = getDecodedCastOpcode(Record[0]);3489 if (Opc < 0) {3490 V = PoisonValue::get(CurTy); // Unknown cast.3491 } else {3492 unsigned OpTyID = Record[1];3493 Type *OpTy = getTypeByID(OpTyID);3494 if (!OpTy)3495 return error("Invalid cast constexpr record");3496 V = BitcodeConstant::create(Alloc, CurTy, Opc, (unsigned)Record[2]);3497 }3498 break;3499 }3500 case bitc::CST_CODE_CE_INBOUNDS_GEP: // [ty, n x operands]3501 case bitc::CST_CODE_CE_GEP_OLD: // [ty, n x operands]3502 case bitc::CST_CODE_CE_GEP_WITH_INRANGE_INDEX_OLD: // [ty, flags, n x3503 // operands]3504 case bitc::CST_CODE_CE_GEP: // [ty, flags, n x operands]3505 case bitc::CST_CODE_CE_GEP_WITH_INRANGE: { // [ty, flags, start, end, n x3506 // operands]3507 if (Record.size() < 2)3508 return error("Constant GEP record must have at least two elements");3509 unsigned OpNum = 0;3510 Type *PointeeType = nullptr;3511 if (BitCode == bitc::CST_CODE_CE_GEP_WITH_INRANGE_INDEX_OLD ||3512 BitCode == bitc::CST_CODE_CE_GEP_WITH_INRANGE ||3513 BitCode == bitc::CST_CODE_CE_GEP || Record.size() % 2)3514 PointeeType = getTypeByID(Record[OpNum++]);3515 3516 uint64_t Flags = 0;3517 std::optional<ConstantRange> InRange;3518 if (BitCode == bitc::CST_CODE_CE_GEP_WITH_INRANGE_INDEX_OLD) {3519 uint64_t Op = Record[OpNum++];3520 Flags = Op & 1; // inbounds3521 unsigned InRangeIndex = Op >> 1;3522 // "Upgrade" inrange by dropping it. The feature is too niche to3523 // bother.3524 (void)InRangeIndex;3525 } else if (BitCode == bitc::CST_CODE_CE_GEP_WITH_INRANGE) {3526 Flags = Record[OpNum++];3527 Expected<ConstantRange> MaybeInRange =3528 readBitWidthAndConstantRange(Record, OpNum);3529 if (!MaybeInRange)3530 return MaybeInRange.takeError();3531 InRange = MaybeInRange.get();3532 } else if (BitCode == bitc::CST_CODE_CE_GEP) {3533 Flags = Record[OpNum++];3534 } else if (BitCode == bitc::CST_CODE_CE_INBOUNDS_GEP)3535 Flags = (1 << bitc::GEP_INBOUNDS);3536 3537 SmallVector<unsigned, 16> Elts;3538 unsigned BaseTypeID = Record[OpNum];3539 while (OpNum != Record.size()) {3540 unsigned ElTyID = Record[OpNum++];3541 Type *ElTy = getTypeByID(ElTyID);3542 if (!ElTy)3543 return error("Invalid getelementptr constexpr record");3544 Elts.push_back(Record[OpNum++]);3545 }3546 3547 if (Elts.size() < 1)3548 return error("Invalid gep with no operands");3549 3550 Type *BaseType = getTypeByID(BaseTypeID);3551 if (isa<VectorType>(BaseType)) {3552 BaseTypeID = getContainedTypeID(BaseTypeID, 0);3553 BaseType = getTypeByID(BaseTypeID);3554 }3555 3556 PointerType *OrigPtrTy = dyn_cast_or_null<PointerType>(BaseType);3557 if (!OrigPtrTy)3558 return error("GEP base operand must be pointer or vector of pointer");3559 3560 if (!PointeeType) {3561 PointeeType = getPtrElementTypeByID(BaseTypeID);3562 if (!PointeeType)3563 return error("Missing element type for old-style constant GEP");3564 }3565 3566 V = BitcodeConstant::create(3567 Alloc, CurTy,3568 {Instruction::GetElementPtr, uint8_t(Flags), PointeeType, InRange},3569 Elts);3570 break;3571 }3572 case bitc::CST_CODE_CE_SELECT: { // CE_SELECT: [opval#, opval#, opval#]3573 if (Record.size() < 3)3574 return error("Invalid select constexpr record");3575 3576 V = BitcodeConstant::create(3577 Alloc, CurTy, Instruction::Select,3578 {(unsigned)Record[0], (unsigned)Record[1], (unsigned)Record[2]});3579 break;3580 }3581 case bitc::CST_CODE_CE_EXTRACTELT3582 : { // CE_EXTRACTELT: [opty, opval, opty, opval]3583 if (Record.size() < 3)3584 return error("Invalid extractelement constexpr record");3585 unsigned OpTyID = Record[0];3586 VectorType *OpTy =3587 dyn_cast_or_null<VectorType>(getTypeByID(OpTyID));3588 if (!OpTy)3589 return error("Invalid extractelement constexpr record");3590 unsigned IdxRecord;3591 if (Record.size() == 4) {3592 unsigned IdxTyID = Record[2];3593 Type *IdxTy = getTypeByID(IdxTyID);3594 if (!IdxTy)3595 return error("Invalid extractelement constexpr record");3596 IdxRecord = Record[3];3597 } else {3598 // Deprecated, but still needed to read old bitcode files.3599 IdxRecord = Record[2];3600 }3601 V = BitcodeConstant::create(Alloc, CurTy, Instruction::ExtractElement,3602 {(unsigned)Record[1], IdxRecord});3603 break;3604 }3605 case bitc::CST_CODE_CE_INSERTELT3606 : { // CE_INSERTELT: [opval, opval, opty, opval]3607 VectorType *OpTy = dyn_cast<VectorType>(CurTy);3608 if (Record.size() < 3 || !OpTy)3609 return error("Invalid insertelement constexpr record");3610 unsigned IdxRecord;3611 if (Record.size() == 4) {3612 unsigned IdxTyID = Record[2];3613 Type *IdxTy = getTypeByID(IdxTyID);3614 if (!IdxTy)3615 return error("Invalid insertelement constexpr record");3616 IdxRecord = Record[3];3617 } else {3618 // Deprecated, but still needed to read old bitcode files.3619 IdxRecord = Record[2];3620 }3621 V = BitcodeConstant::create(3622 Alloc, CurTy, Instruction::InsertElement,3623 {(unsigned)Record[0], (unsigned)Record[1], IdxRecord});3624 break;3625 }3626 case bitc::CST_CODE_CE_SHUFFLEVEC: { // CE_SHUFFLEVEC: [opval, opval, opval]3627 VectorType *OpTy = dyn_cast<VectorType>(CurTy);3628 if (Record.size() < 3 || !OpTy)3629 return error("Invalid shufflevector constexpr record");3630 V = BitcodeConstant::create(3631 Alloc, CurTy, Instruction::ShuffleVector,3632 {(unsigned)Record[0], (unsigned)Record[1], (unsigned)Record[2]});3633 break;3634 }3635 case bitc::CST_CODE_CE_SHUFVEC_EX: { // [opty, opval, opval, opval]3636 VectorType *RTy = dyn_cast<VectorType>(CurTy);3637 VectorType *OpTy =3638 dyn_cast_or_null<VectorType>(getTypeByID(Record[0]));3639 if (Record.size() < 4 || !RTy || !OpTy)3640 return error("Invalid shufflevector constexpr record");3641 V = BitcodeConstant::create(3642 Alloc, CurTy, Instruction::ShuffleVector,3643 {(unsigned)Record[1], (unsigned)Record[2], (unsigned)Record[3]});3644 break;3645 }3646 case bitc::CST_CODE_CE_CMP: { // CE_CMP: [opty, opval, opval, pred]3647 if (Record.size() < 4)3648 return error("Invalid cmp constexpt record");3649 unsigned OpTyID = Record[0];3650 Type *OpTy = getTypeByID(OpTyID);3651 if (!OpTy)3652 return error("Invalid cmp constexpr record");3653 V = BitcodeConstant::create(3654 Alloc, CurTy,3655 {(uint8_t)(OpTy->isFPOrFPVectorTy() ? Instruction::FCmp3656 : Instruction::ICmp),3657 (uint8_t)Record[3]},3658 {(unsigned)Record[1], (unsigned)Record[2]});3659 break;3660 }3661 // This maintains backward compatibility, pre-asm dialect keywords.3662 // Deprecated, but still needed to read old bitcode files.3663 case bitc::CST_CODE_INLINEASM_OLD: {3664 if (Record.size() < 2)3665 return error("Invalid inlineasm record");3666 std::string AsmStr, ConstrStr;3667 bool HasSideEffects = Record[0] & 1;3668 bool IsAlignStack = Record[0] >> 1;3669 unsigned AsmStrSize = Record[1];3670 if (2+AsmStrSize >= Record.size())3671 return error("Invalid inlineasm record");3672 unsigned ConstStrSize = Record[2+AsmStrSize];3673 if (3+AsmStrSize+ConstStrSize > Record.size())3674 return error("Invalid inlineasm record");3675 3676 for (unsigned i = 0; i != AsmStrSize; ++i)3677 AsmStr += (char)Record[2+i];3678 for (unsigned i = 0; i != ConstStrSize; ++i)3679 ConstrStr += (char)Record[3+AsmStrSize+i];3680 UpgradeInlineAsmString(&AsmStr);3681 if (!CurElemTy)3682 return error("Missing element type for old-style inlineasm");3683 V = InlineAsm::get(cast<FunctionType>(CurElemTy), AsmStr, ConstrStr,3684 HasSideEffects, IsAlignStack);3685 break;3686 }3687 // This version adds support for the asm dialect keywords (e.g.,3688 // inteldialect).3689 case bitc::CST_CODE_INLINEASM_OLD2: {3690 if (Record.size() < 2)3691 return error("Invalid inlineasm record");3692 std::string AsmStr, ConstrStr;3693 bool HasSideEffects = Record[0] & 1;3694 bool IsAlignStack = (Record[0] >> 1) & 1;3695 unsigned AsmDialect = Record[0] >> 2;3696 unsigned AsmStrSize = Record[1];3697 if (2+AsmStrSize >= Record.size())3698 return error("Invalid inlineasm record");3699 unsigned ConstStrSize = Record[2+AsmStrSize];3700 if (3+AsmStrSize+ConstStrSize > Record.size())3701 return error("Invalid inlineasm record");3702 3703 for (unsigned i = 0; i != AsmStrSize; ++i)3704 AsmStr += (char)Record[2+i];3705 for (unsigned i = 0; i != ConstStrSize; ++i)3706 ConstrStr += (char)Record[3+AsmStrSize+i];3707 UpgradeInlineAsmString(&AsmStr);3708 if (!CurElemTy)3709 return error("Missing element type for old-style inlineasm");3710 V = InlineAsm::get(cast<FunctionType>(CurElemTy), AsmStr, ConstrStr,3711 HasSideEffects, IsAlignStack,3712 InlineAsm::AsmDialect(AsmDialect));3713 break;3714 }3715 // This version adds support for the unwind keyword.3716 case bitc::CST_CODE_INLINEASM_OLD3: {3717 if (Record.size() < 2)3718 return error("Invalid inlineasm record");3719 unsigned OpNum = 0;3720 std::string AsmStr, ConstrStr;3721 bool HasSideEffects = Record[OpNum] & 1;3722 bool IsAlignStack = (Record[OpNum] >> 1) & 1;3723 unsigned AsmDialect = (Record[OpNum] >> 2) & 1;3724 bool CanThrow = (Record[OpNum] >> 3) & 1;3725 ++OpNum;3726 unsigned AsmStrSize = Record[OpNum];3727 ++OpNum;3728 if (OpNum + AsmStrSize >= Record.size())3729 return error("Invalid inlineasm record");3730 unsigned ConstStrSize = Record[OpNum + AsmStrSize];3731 if (OpNum + 1 + AsmStrSize + ConstStrSize > Record.size())3732 return error("Invalid inlineasm record");3733 3734 for (unsigned i = 0; i != AsmStrSize; ++i)3735 AsmStr += (char)Record[OpNum + i];3736 ++OpNum;3737 for (unsigned i = 0; i != ConstStrSize; ++i)3738 ConstrStr += (char)Record[OpNum + AsmStrSize + i];3739 UpgradeInlineAsmString(&AsmStr);3740 if (!CurElemTy)3741 return error("Missing element type for old-style inlineasm");3742 V = InlineAsm::get(cast<FunctionType>(CurElemTy), AsmStr, ConstrStr,3743 HasSideEffects, IsAlignStack,3744 InlineAsm::AsmDialect(AsmDialect), CanThrow);3745 break;3746 }3747 // This version adds explicit function type.3748 case bitc::CST_CODE_INLINEASM: {3749 if (Record.size() < 3)3750 return error("Invalid inlineasm record");3751 unsigned OpNum = 0;3752 auto *FnTy = dyn_cast_or_null<FunctionType>(getTypeByID(Record[OpNum]));3753 ++OpNum;3754 if (!FnTy)3755 return error("Invalid inlineasm record");3756 std::string AsmStr, ConstrStr;3757 bool HasSideEffects = Record[OpNum] & 1;3758 bool IsAlignStack = (Record[OpNum] >> 1) & 1;3759 unsigned AsmDialect = (Record[OpNum] >> 2) & 1;3760 bool CanThrow = (Record[OpNum] >> 3) & 1;3761 ++OpNum;3762 unsigned AsmStrSize = Record[OpNum];3763 ++OpNum;3764 if (OpNum + AsmStrSize >= Record.size())3765 return error("Invalid inlineasm record");3766 unsigned ConstStrSize = Record[OpNum + AsmStrSize];3767 if (OpNum + 1 + AsmStrSize + ConstStrSize > Record.size())3768 return error("Invalid inlineasm record");3769 3770 for (unsigned i = 0; i != AsmStrSize; ++i)3771 AsmStr += (char)Record[OpNum + i];3772 ++OpNum;3773 for (unsigned i = 0; i != ConstStrSize; ++i)3774 ConstrStr += (char)Record[OpNum + AsmStrSize + i];3775 UpgradeInlineAsmString(&AsmStr);3776 V = InlineAsm::get(FnTy, AsmStr, ConstrStr, HasSideEffects, IsAlignStack,3777 InlineAsm::AsmDialect(AsmDialect), CanThrow);3778 break;3779 }3780 case bitc::CST_CODE_BLOCKADDRESS:{3781 if (Record.size() < 3)3782 return error("Invalid blockaddress record");3783 unsigned FnTyID = Record[0];3784 Type *FnTy = getTypeByID(FnTyID);3785 if (!FnTy)3786 return error("Invalid blockaddress record");3787 V = BitcodeConstant::create(3788 Alloc, CurTy,3789 {BitcodeConstant::BlockAddressOpcode, 0, (unsigned)Record[2]},3790 Record[1]);3791 break;3792 }3793 case bitc::CST_CODE_DSO_LOCAL_EQUIVALENT: {3794 if (Record.size() < 2)3795 return error("Invalid dso_local record");3796 unsigned GVTyID = Record[0];3797 Type *GVTy = getTypeByID(GVTyID);3798 if (!GVTy)3799 return error("Invalid dso_local record");3800 V = BitcodeConstant::create(3801 Alloc, CurTy, BitcodeConstant::DSOLocalEquivalentOpcode, Record[1]);3802 break;3803 }3804 case bitc::CST_CODE_NO_CFI_VALUE: {3805 if (Record.size() < 2)3806 return error("Invalid no_cfi record");3807 unsigned GVTyID = Record[0];3808 Type *GVTy = getTypeByID(GVTyID);3809 if (!GVTy)3810 return error("Invalid no_cfi record");3811 V = BitcodeConstant::create(Alloc, CurTy, BitcodeConstant::NoCFIOpcode,3812 Record[1]);3813 break;3814 }3815 case bitc::CST_CODE_PTRAUTH: {3816 if (Record.size() < 4)3817 return error("Invalid ptrauth record");3818 // Ptr, Key, Disc, AddrDisc3819 V = BitcodeConstant::create(Alloc, CurTy,3820 BitcodeConstant::ConstantPtrAuthOpcode,3821 {(unsigned)Record[0], (unsigned)Record[1],3822 (unsigned)Record[2], (unsigned)Record[3]});3823 break;3824 }3825 case bitc::CST_CODE_PTRAUTH2: {3826 if (Record.size() < 5)3827 return error("Invalid ptrauth record");3828 // Ptr, Key, Disc, AddrDisc, DeactivationSymbol3829 V = BitcodeConstant::create(3830 Alloc, CurTy, BitcodeConstant::ConstantPtrAuthOpcode,3831 {(unsigned)Record[0], (unsigned)Record[1], (unsigned)Record[2],3832 (unsigned)Record[3], (unsigned)Record[4]});3833 break;3834 }3835 }3836 3837 assert(V->getType() == getTypeByID(CurTyID) && "Incorrect result type ID");3838 if (Error Err = ValueList.assignValue(NextCstNo, V, CurTyID))3839 return Err;3840 ++NextCstNo;3841 }3842}3843 3844Error BitcodeReader::parseUseLists() {3845 if (Error Err = Stream.EnterSubBlock(bitc::USELIST_BLOCK_ID))3846 return Err;3847 3848 // Read all the records.3849 SmallVector<uint64_t, 64> Record;3850 3851 while (true) {3852 Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks();3853 if (!MaybeEntry)3854 return MaybeEntry.takeError();3855 BitstreamEntry Entry = MaybeEntry.get();3856 3857 switch (Entry.Kind) {3858 case BitstreamEntry::SubBlock: // Handled for us already.3859 case BitstreamEntry::Error:3860 return error("Malformed block");3861 case BitstreamEntry::EndBlock:3862 return Error::success();3863 case BitstreamEntry::Record:3864 // The interesting case.3865 break;3866 }3867 3868 // Read a use list record.3869 Record.clear();3870 bool IsBB = false;3871 Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record);3872 if (!MaybeRecord)3873 return MaybeRecord.takeError();3874 switch (MaybeRecord.get()) {3875 default: // Default behavior: unknown type.3876 break;3877 case bitc::USELIST_CODE_BB:3878 IsBB = true;3879 [[fallthrough]];3880 case bitc::USELIST_CODE_DEFAULT: {3881 unsigned RecordLength = Record.size();3882 if (RecordLength < 3)3883 // Records should have at least an ID and two indexes.3884 return error("Invalid record");3885 unsigned ID = Record.pop_back_val();3886 3887 Value *V;3888 if (IsBB) {3889 assert(ID < FunctionBBs.size() && "Basic block not found");3890 V = FunctionBBs[ID];3891 } else3892 V = ValueList[ID];3893 3894 if (!V->hasUseList())3895 break;3896 3897 unsigned NumUses = 0;3898 SmallDenseMap<const Use *, unsigned, 16> Order;3899 for (const Use &U : V->materialized_uses()) {3900 if (++NumUses > Record.size())3901 break;3902 Order[&U] = Record[NumUses - 1];3903 }3904 if (Order.size() != Record.size() || NumUses > Record.size())3905 // Mismatches can happen if the functions are being materialized lazily3906 // (out-of-order), or a value has been upgraded.3907 break;3908 3909 V->sortUseList([&](const Use &L, const Use &R) {3910 return Order.lookup(&L) < Order.lookup(&R);3911 });3912 break;3913 }3914 }3915 }3916}3917 3918/// When we see the block for metadata, remember where it is and then skip it.3919/// This lets us lazily deserialize the metadata.3920Error BitcodeReader::rememberAndSkipMetadata() {3921 // Save the current stream state.3922 uint64_t CurBit = Stream.GetCurrentBitNo();3923 DeferredMetadataInfo.push_back(CurBit);3924 3925 // Skip over the block for now.3926 if (Error Err = Stream.SkipBlock())3927 return Err;3928 return Error::success();3929}3930 3931Error BitcodeReader::materializeMetadata() {3932 for (uint64_t BitPos : DeferredMetadataInfo) {3933 // Move the bit stream to the saved position.3934 if (Error JumpFailed = Stream.JumpToBit(BitPos))3935 return JumpFailed;3936 if (Error Err = MDLoader->parseModuleMetadata())3937 return Err;3938 }3939 3940 // Upgrade "Linker Options" module flag to "llvm.linker.options" module-level3941 // metadata. Only upgrade if the new option doesn't exist to avoid upgrade3942 // multiple times.3943 if (!TheModule->getNamedMetadata("llvm.linker.options")) {3944 if (Metadata *Val = TheModule->getModuleFlag("Linker Options")) {3945 NamedMDNode *LinkerOpts =3946 TheModule->getOrInsertNamedMetadata("llvm.linker.options");3947 for (const MDOperand &MDOptions : cast<MDNode>(Val)->operands())3948 LinkerOpts->addOperand(cast<MDNode>(MDOptions));3949 }3950 }3951 3952 DeferredMetadataInfo.clear();3953 return Error::success();3954}3955 3956void BitcodeReader::setStripDebugInfo() { StripDebugInfo = true; }3957 3958/// When we see the block for a function body, remember where it is and then3959/// skip it. This lets us lazily deserialize the functions.3960Error BitcodeReader::rememberAndSkipFunctionBody() {3961 // Get the function we are talking about.3962 if (FunctionsWithBodies.empty())3963 return error("Insufficient function protos");3964 3965 Function *Fn = FunctionsWithBodies.back();3966 FunctionsWithBodies.pop_back();3967 3968 // Save the current stream state.3969 uint64_t CurBit = Stream.GetCurrentBitNo();3970 assert(3971 (DeferredFunctionInfo[Fn] == 0 || DeferredFunctionInfo[Fn] == CurBit) &&3972 "Mismatch between VST and scanned function offsets");3973 DeferredFunctionInfo[Fn] = CurBit;3974 3975 // Skip over the function block for now.3976 if (Error Err = Stream.SkipBlock())3977 return Err;3978 return Error::success();3979}3980 3981Error BitcodeReader::globalCleanup() {3982 // Patch the initializers for globals and aliases up.3983 if (Error Err = resolveGlobalAndIndirectSymbolInits())3984 return Err;3985 if (!GlobalInits.empty() || !IndirectSymbolInits.empty())3986 return error("Malformed global initializer set");3987 3988 // Look for intrinsic functions which need to be upgraded at some point3989 // and functions that need to have their function attributes upgraded.3990 for (Function &F : *TheModule) {3991 MDLoader->upgradeDebugIntrinsics(F);3992 Function *NewFn;3993 if (UpgradeIntrinsicFunction(&F, NewFn))3994 UpgradedIntrinsics[&F] = NewFn;3995 // Look for functions that rely on old function attribute behavior.3996 UpgradeFunctionAttributes(F);3997 }3998 3999 // Look for global variables which need to be renamed.4000 std::vector<std::pair<GlobalVariable *, GlobalVariable *>> UpgradedVariables;4001 for (GlobalVariable &GV : TheModule->globals())4002 if (GlobalVariable *Upgraded = UpgradeGlobalVariable(&GV))4003 UpgradedVariables.emplace_back(&GV, Upgraded);4004 for (auto &Pair : UpgradedVariables) {4005 Pair.first->eraseFromParent();4006 TheModule->insertGlobalVariable(Pair.second);4007 }4008 4009 // Force deallocation of memory for these vectors to favor the client that4010 // want lazy deserialization.4011 std::vector<std::pair<GlobalVariable *, unsigned>>().swap(GlobalInits);4012 std::vector<std::pair<GlobalValue *, unsigned>>().swap(IndirectSymbolInits);4013 return Error::success();4014}4015 4016/// Support for lazy parsing of function bodies. This is required if we4017/// either have an old bitcode file without a VST forward declaration record,4018/// or if we have an anonymous function being materialized, since anonymous4019/// functions do not have a name and are therefore not in the VST.4020Error BitcodeReader::rememberAndSkipFunctionBodies() {4021 if (Error JumpFailed = Stream.JumpToBit(NextUnreadBit))4022 return JumpFailed;4023 4024 if (Stream.AtEndOfStream())4025 return error("Could not find function in stream");4026 4027 if (!SeenFirstFunctionBody)4028 return error("Trying to materialize functions before seeing function blocks");4029 4030 // An old bitcode file with the symbol table at the end would have4031 // finished the parse greedily.4032 assert(SeenValueSymbolTable);4033 4034 while (true) {4035 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();4036 if (!MaybeEntry)4037 return MaybeEntry.takeError();4038 llvm::BitstreamEntry Entry = MaybeEntry.get();4039 4040 switch (Entry.Kind) {4041 default:4042 return error("Expect SubBlock");4043 case BitstreamEntry::SubBlock:4044 switch (Entry.ID) {4045 default:4046 return error("Expect function block");4047 case bitc::FUNCTION_BLOCK_ID:4048 if (Error Err = rememberAndSkipFunctionBody())4049 return Err;4050 NextUnreadBit = Stream.GetCurrentBitNo();4051 return Error::success();4052 }4053 }4054 }4055}4056 4057Error BitcodeReaderBase::readBlockInfo() {4058 Expected<std::optional<BitstreamBlockInfo>> MaybeNewBlockInfo =4059 Stream.ReadBlockInfoBlock();4060 if (!MaybeNewBlockInfo)4061 return MaybeNewBlockInfo.takeError();4062 std::optional<BitstreamBlockInfo> NewBlockInfo =4063 std::move(MaybeNewBlockInfo.get());4064 if (!NewBlockInfo)4065 return error("Malformed block");4066 BlockInfo = std::move(*NewBlockInfo);4067 return Error::success();4068}4069 4070Error BitcodeReader::parseComdatRecord(ArrayRef<uint64_t> Record) {4071 // v1: [selection_kind, name]4072 // v2: [strtab_offset, strtab_size, selection_kind]4073 StringRef Name;4074 std::tie(Name, Record) = readNameFromStrtab(Record);4075 4076 if (Record.empty())4077 return error("Invalid record");4078 Comdat::SelectionKind SK = getDecodedComdatSelectionKind(Record[0]);4079 std::string OldFormatName;4080 if (!UseStrtab) {4081 if (Record.size() < 2)4082 return error("Invalid record");4083 unsigned ComdatNameSize = Record[1];4084 if (ComdatNameSize > Record.size() - 2)4085 return error("Comdat name size too large");4086 OldFormatName.reserve(ComdatNameSize);4087 for (unsigned i = 0; i != ComdatNameSize; ++i)4088 OldFormatName += (char)Record[2 + i];4089 Name = OldFormatName;4090 }4091 Comdat *C = TheModule->getOrInsertComdat(Name);4092 C->setSelectionKind(SK);4093 ComdatList.push_back(C);4094 return Error::success();4095}4096 4097static void inferDSOLocal(GlobalValue *GV) {4098 // infer dso_local from linkage and visibility if it is not encoded.4099 if (GV->hasLocalLinkage() ||4100 (!GV->hasDefaultVisibility() && !GV->hasExternalWeakLinkage()))4101 GV->setDSOLocal(true);4102}4103 4104GlobalValue::SanitizerMetadata deserializeSanitizerMetadata(unsigned V) {4105 GlobalValue::SanitizerMetadata Meta;4106 if (V & (1 << 0))4107 Meta.NoAddress = true;4108 if (V & (1 << 1))4109 Meta.NoHWAddress = true;4110 if (V & (1 << 2))4111 Meta.Memtag = true;4112 if (V & (1 << 3))4113 Meta.IsDynInit = true;4114 return Meta;4115}4116 4117Error BitcodeReader::parseGlobalVarRecord(ArrayRef<uint64_t> Record) {4118 // v1: [pointer type, isconst, initid, linkage, alignment, section,4119 // visibility, threadlocal, unnamed_addr, externally_initialized,4120 // dllstorageclass, comdat, attributes, preemption specifier,4121 // partition strtab offset, partition strtab size] (name in VST)4122 // v2: [strtab_offset, strtab_size, v1]4123 // v3: [v2, code_model]4124 StringRef Name;4125 std::tie(Name, Record) = readNameFromStrtab(Record);4126 4127 if (Record.size() < 6)4128 return error("Invalid record");4129 unsigned TyID = Record[0];4130 Type *Ty = getTypeByID(TyID);4131 if (!Ty)4132 return error("Invalid record");4133 bool isConstant = Record[1] & 1;4134 bool explicitType = Record[1] & 2;4135 unsigned AddressSpace;4136 if (explicitType) {4137 AddressSpace = Record[1] >> 2;4138 } else {4139 if (!Ty->isPointerTy())4140 return error("Invalid type for value");4141 AddressSpace = cast<PointerType>(Ty)->getAddressSpace();4142 TyID = getContainedTypeID(TyID);4143 Ty = getTypeByID(TyID);4144 if (!Ty)4145 return error("Missing element type for old-style global");4146 }4147 4148 uint64_t RawLinkage = Record[3];4149 GlobalValue::LinkageTypes Linkage = getDecodedLinkage(RawLinkage);4150 MaybeAlign Alignment;4151 if (Error Err = parseAlignmentValue(Record[4], Alignment))4152 return Err;4153 std::string Section;4154 if (Record[5]) {4155 if (Record[5] - 1 >= SectionTable.size())4156 return error("Invalid ID");4157 Section = SectionTable[Record[5] - 1];4158 }4159 GlobalValue::VisibilityTypes Visibility = GlobalValue::DefaultVisibility;4160 // Local linkage must have default visibility.4161 // auto-upgrade `hidden` and `protected` for old bitcode.4162 if (Record.size() > 6 && !GlobalValue::isLocalLinkage(Linkage))4163 Visibility = getDecodedVisibility(Record[6]);4164 4165 GlobalVariable::ThreadLocalMode TLM = GlobalVariable::NotThreadLocal;4166 if (Record.size() > 7)4167 TLM = getDecodedThreadLocalMode(Record[7]);4168 4169 GlobalValue::UnnamedAddr UnnamedAddr = GlobalValue::UnnamedAddr::None;4170 if (Record.size() > 8)4171 UnnamedAddr = getDecodedUnnamedAddrType(Record[8]);4172 4173 bool ExternallyInitialized = false;4174 if (Record.size() > 9)4175 ExternallyInitialized = Record[9];4176 4177 GlobalVariable *NewGV =4178 new GlobalVariable(*TheModule, Ty, isConstant, Linkage, nullptr, Name,4179 nullptr, TLM, AddressSpace, ExternallyInitialized);4180 if (Alignment)4181 NewGV->setAlignment(*Alignment);4182 if (!Section.empty())4183 NewGV->setSection(Section);4184 NewGV->setVisibility(Visibility);4185 NewGV->setUnnamedAddr(UnnamedAddr);4186 4187 if (Record.size() > 10) {4188 // A GlobalValue with local linkage cannot have a DLL storage class.4189 if (!NewGV->hasLocalLinkage()) {4190 NewGV->setDLLStorageClass(getDecodedDLLStorageClass(Record[10]));4191 }4192 } else {4193 upgradeDLLImportExportLinkage(NewGV, RawLinkage);4194 }4195 4196 ValueList.push_back(NewGV, getVirtualTypeID(NewGV->getType(), TyID));4197 4198 // Remember which value to use for the global initializer.4199 if (unsigned InitID = Record[2])4200 GlobalInits.push_back(std::make_pair(NewGV, InitID - 1));4201 4202 if (Record.size() > 11) {4203 if (unsigned ComdatID = Record[11]) {4204 if (ComdatID > ComdatList.size())4205 return error("Invalid global variable comdat ID");4206 NewGV->setComdat(ComdatList[ComdatID - 1]);4207 }4208 } else if (hasImplicitComdat(RawLinkage)) {4209 ImplicitComdatObjects.insert(NewGV);4210 }4211 4212 if (Record.size() > 12) {4213 auto AS = getAttributes(Record[12]).getFnAttrs();4214 NewGV->setAttributes(AS);4215 }4216 4217 if (Record.size() > 13) {4218 NewGV->setDSOLocal(getDecodedDSOLocal(Record[13]));4219 }4220 inferDSOLocal(NewGV);4221 4222 // Check whether we have enough values to read a partition name.4223 if (Record.size() > 15)4224 NewGV->setPartition(StringRef(Strtab.data() + Record[14], Record[15]));4225 4226 if (Record.size() > 16 && Record[16]) {4227 llvm::GlobalValue::SanitizerMetadata Meta =4228 deserializeSanitizerMetadata(Record[16]);4229 NewGV->setSanitizerMetadata(Meta);4230 }4231 4232 if (Record.size() > 17 && Record[17]) {4233 if (auto CM = getDecodedCodeModel(Record[17]))4234 NewGV->setCodeModel(*CM);4235 else4236 return error("Invalid global variable code model");4237 }4238 4239 return Error::success();4240}4241 4242void BitcodeReader::callValueTypeCallback(Value *F, unsigned TypeID) {4243 if (ValueTypeCallback) {4244 (*ValueTypeCallback)(4245 F, TypeID, [this](unsigned I) { return getTypeByID(I); },4246 [this](unsigned I, unsigned J) { return getContainedTypeID(I, J); });4247 }4248}4249 4250Error BitcodeReader::parseFunctionRecord(ArrayRef<uint64_t> Record) {4251 // v1: [type, callingconv, isproto, linkage, paramattr, alignment, section,4252 // visibility, gc, unnamed_addr, prologuedata, dllstorageclass, comdat,4253 // prefixdata, personalityfn, preemption specifier, addrspace] (name in VST)4254 // v2: [strtab_offset, strtab_size, v1]4255 StringRef Name;4256 std::tie(Name, Record) = readNameFromStrtab(Record);4257 4258 if (Record.size() < 8)4259 return error("Invalid record");4260 unsigned FTyID = Record[0];4261 Type *FTy = getTypeByID(FTyID);4262 if (!FTy)4263 return error("Invalid record");4264 if (isa<PointerType>(FTy)) {4265 FTyID = getContainedTypeID(FTyID, 0);4266 FTy = getTypeByID(FTyID);4267 if (!FTy)4268 return error("Missing element type for old-style function");4269 }4270 4271 if (!isa<FunctionType>(FTy))4272 return error("Invalid type for value");4273 auto CC = static_cast<CallingConv::ID>(Record[1]);4274 if (CC & ~CallingConv::MaxID)4275 return error("Invalid calling convention ID");4276 4277 unsigned AddrSpace = TheModule->getDataLayout().getProgramAddressSpace();4278 if (Record.size() > 16)4279 AddrSpace = Record[16];4280 4281 Function *Func =4282 Function::Create(cast<FunctionType>(FTy), GlobalValue::ExternalLinkage,4283 AddrSpace, Name, TheModule);4284 4285 assert(Func->getFunctionType() == FTy &&4286 "Incorrect fully specified type provided for function");4287 FunctionTypeIDs[Func] = FTyID;4288 4289 Func->setCallingConv(CC);4290 bool isProto = Record[2];4291 uint64_t RawLinkage = Record[3];4292 Func->setLinkage(getDecodedLinkage(RawLinkage));4293 Func->setAttributes(getAttributes(Record[4]));4294 callValueTypeCallback(Func, FTyID);4295 4296 // Upgrade any old-style byval or sret without a type by propagating the4297 // argument's pointee type. There should be no opaque pointers where the byval4298 // type is implicit.4299 for (unsigned i = 0; i != Func->arg_size(); ++i) {4300 for (Attribute::AttrKind Kind : {Attribute::ByVal, Attribute::StructRet,4301 Attribute::InAlloca}) {4302 if (!Func->hasParamAttribute(i, Kind))4303 continue;4304 4305 if (Func->getParamAttribute(i, Kind).getValueAsType())4306 continue;4307 4308 Func->removeParamAttr(i, Kind);4309 4310 unsigned ParamTypeID = getContainedTypeID(FTyID, i + 1);4311 Type *PtrEltTy = getPtrElementTypeByID(ParamTypeID);4312 if (!PtrEltTy)4313 return error("Missing param element type for attribute upgrade");4314 4315 Attribute NewAttr;4316 switch (Kind) {4317 case Attribute::ByVal:4318 NewAttr = Attribute::getWithByValType(Context, PtrEltTy);4319 break;4320 case Attribute::StructRet:4321 NewAttr = Attribute::getWithStructRetType(Context, PtrEltTy);4322 break;4323 case Attribute::InAlloca:4324 NewAttr = Attribute::getWithInAllocaType(Context, PtrEltTy);4325 break;4326 default:4327 llvm_unreachable("not an upgraded type attribute");4328 }4329 4330 Func->addParamAttr(i, NewAttr);4331 }4332 }4333 4334 if (Func->getCallingConv() == CallingConv::X86_INTR &&4335 !Func->arg_empty() && !Func->hasParamAttribute(0, Attribute::ByVal)) {4336 unsigned ParamTypeID = getContainedTypeID(FTyID, 1);4337 Type *ByValTy = getPtrElementTypeByID(ParamTypeID);4338 if (!ByValTy)4339 return error("Missing param element type for x86_intrcc upgrade");4340 Attribute NewAttr = Attribute::getWithByValType(Context, ByValTy);4341 Func->addParamAttr(0, NewAttr);4342 }4343 4344 MaybeAlign Alignment;4345 if (Error Err = parseAlignmentValue(Record[5], Alignment))4346 return Err;4347 if (Alignment)4348 Func->setAlignment(*Alignment);4349 if (Record[6]) {4350 if (Record[6] - 1 >= SectionTable.size())4351 return error("Invalid ID");4352 Func->setSection(SectionTable[Record[6] - 1]);4353 }4354 // Local linkage must have default visibility.4355 // auto-upgrade `hidden` and `protected` for old bitcode.4356 if (!Func->hasLocalLinkage())4357 Func->setVisibility(getDecodedVisibility(Record[7]));4358 if (Record.size() > 8 && Record[8]) {4359 if (Record[8] - 1 >= GCTable.size())4360 return error("Invalid ID");4361 Func->setGC(GCTable[Record[8] - 1]);4362 }4363 GlobalValue::UnnamedAddr UnnamedAddr = GlobalValue::UnnamedAddr::None;4364 if (Record.size() > 9)4365 UnnamedAddr = getDecodedUnnamedAddrType(Record[9]);4366 Func->setUnnamedAddr(UnnamedAddr);4367 4368 FunctionOperandInfo OperandInfo = {Func, 0, 0, 0};4369 if (Record.size() > 10)4370 OperandInfo.Prologue = Record[10];4371 4372 if (Record.size() > 11) {4373 // A GlobalValue with local linkage cannot have a DLL storage class.4374 if (!Func->hasLocalLinkage()) {4375 Func->setDLLStorageClass(getDecodedDLLStorageClass(Record[11]));4376 }4377 } else {4378 upgradeDLLImportExportLinkage(Func, RawLinkage);4379 }4380 4381 if (Record.size() > 12) {4382 if (unsigned ComdatID = Record[12]) {4383 if (ComdatID > ComdatList.size())4384 return error("Invalid function comdat ID");4385 Func->setComdat(ComdatList[ComdatID - 1]);4386 }4387 } else if (hasImplicitComdat(RawLinkage)) {4388 ImplicitComdatObjects.insert(Func);4389 }4390 4391 if (Record.size() > 13)4392 OperandInfo.Prefix = Record[13];4393 4394 if (Record.size() > 14)4395 OperandInfo.PersonalityFn = Record[14];4396 4397 if (Record.size() > 15) {4398 Func->setDSOLocal(getDecodedDSOLocal(Record[15]));4399 }4400 inferDSOLocal(Func);4401 4402 // Record[16] is the address space number.4403 4404 // Check whether we have enough values to read a partition name. Also make4405 // sure Strtab has enough values.4406 if (Record.size() > 18 && Strtab.data() &&4407 Record[17] + Record[18] <= Strtab.size()) {4408 Func->setPartition(StringRef(Strtab.data() + Record[17], Record[18]));4409 }4410 4411 ValueList.push_back(Func, getVirtualTypeID(Func->getType(), FTyID));4412 4413 if (OperandInfo.PersonalityFn || OperandInfo.Prefix || OperandInfo.Prologue)4414 FunctionOperands.push_back(OperandInfo);4415 4416 // If this is a function with a body, remember the prototype we are4417 // creating now, so that we can match up the body with them later.4418 if (!isProto) {4419 Func->setIsMaterializable(true);4420 FunctionsWithBodies.push_back(Func);4421 DeferredFunctionInfo[Func] = 0;4422 }4423 return Error::success();4424}4425 4426Error BitcodeReader::parseGlobalIndirectSymbolRecord(4427 unsigned BitCode, ArrayRef<uint64_t> Record) {4428 // v1 ALIAS_OLD: [alias type, aliasee val#, linkage] (name in VST)4429 // v1 ALIAS: [alias type, addrspace, aliasee val#, linkage, visibility,4430 // dllstorageclass, threadlocal, unnamed_addr,4431 // preemption specifier] (name in VST)4432 // v1 IFUNC: [alias type, addrspace, aliasee val#, linkage,4433 // visibility, dllstorageclass, threadlocal, unnamed_addr,4434 // preemption specifier] (name in VST)4435 // v2: [strtab_offset, strtab_size, v1]4436 StringRef Name;4437 std::tie(Name, Record) = readNameFromStrtab(Record);4438 4439 bool NewRecord = BitCode != bitc::MODULE_CODE_ALIAS_OLD;4440 if (Record.size() < (3 + (unsigned)NewRecord))4441 return error("Invalid record");4442 unsigned OpNum = 0;4443 unsigned TypeID = Record[OpNum++];4444 Type *Ty = getTypeByID(TypeID);4445 if (!Ty)4446 return error("Invalid record");4447 4448 unsigned AddrSpace;4449 if (!NewRecord) {4450 auto *PTy = dyn_cast<PointerType>(Ty);4451 if (!PTy)4452 return error("Invalid type for value");4453 AddrSpace = PTy->getAddressSpace();4454 TypeID = getContainedTypeID(TypeID);4455 Ty = getTypeByID(TypeID);4456 if (!Ty)4457 return error("Missing element type for old-style indirect symbol");4458 } else {4459 AddrSpace = Record[OpNum++];4460 }4461 4462 auto Val = Record[OpNum++];4463 auto Linkage = Record[OpNum++];4464 GlobalValue *NewGA;4465 if (BitCode == bitc::MODULE_CODE_ALIAS ||4466 BitCode == bitc::MODULE_CODE_ALIAS_OLD)4467 NewGA = GlobalAlias::create(Ty, AddrSpace, getDecodedLinkage(Linkage), Name,4468 TheModule);4469 else4470 NewGA = GlobalIFunc::create(Ty, AddrSpace, getDecodedLinkage(Linkage), Name,4471 nullptr, TheModule);4472 4473 // Local linkage must have default visibility.4474 // auto-upgrade `hidden` and `protected` for old bitcode.4475 if (OpNum != Record.size()) {4476 auto VisInd = OpNum++;4477 if (!NewGA->hasLocalLinkage())4478 NewGA->setVisibility(getDecodedVisibility(Record[VisInd]));4479 }4480 if (BitCode == bitc::MODULE_CODE_ALIAS ||4481 BitCode == bitc::MODULE_CODE_ALIAS_OLD) {4482 if (OpNum != Record.size()) {4483 auto S = Record[OpNum++];4484 // A GlobalValue with local linkage cannot have a DLL storage class.4485 if (!NewGA->hasLocalLinkage())4486 NewGA->setDLLStorageClass(getDecodedDLLStorageClass(S));4487 }4488 else4489 upgradeDLLImportExportLinkage(NewGA, Linkage);4490 if (OpNum != Record.size())4491 NewGA->setThreadLocalMode(getDecodedThreadLocalMode(Record[OpNum++]));4492 if (OpNum != Record.size())4493 NewGA->setUnnamedAddr(getDecodedUnnamedAddrType(Record[OpNum++]));4494 }4495 if (OpNum != Record.size())4496 NewGA->setDSOLocal(getDecodedDSOLocal(Record[OpNum++]));4497 inferDSOLocal(NewGA);4498 4499 // Check whether we have enough values to read a partition name.4500 if (OpNum + 1 < Record.size()) {4501 // Check Strtab has enough values for the partition.4502 if (Record[OpNum] + Record[OpNum + 1] > Strtab.size())4503 return error("Malformed partition, too large.");4504 NewGA->setPartition(4505 StringRef(Strtab.data() + Record[OpNum], Record[OpNum + 1]));4506 }4507 4508 ValueList.push_back(NewGA, getVirtualTypeID(NewGA->getType(), TypeID));4509 IndirectSymbolInits.push_back(std::make_pair(NewGA, Val));4510 return Error::success();4511}4512 4513Error BitcodeReader::parseModule(uint64_t ResumeBit,4514 bool ShouldLazyLoadMetadata,4515 ParserCallbacks Callbacks) {4516 this->ValueTypeCallback = std::move(Callbacks.ValueType);4517 if (ResumeBit) {4518 if (Error JumpFailed = Stream.JumpToBit(ResumeBit))4519 return JumpFailed;4520 } else if (Error Err = Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID))4521 return Err;4522 4523 SmallVector<uint64_t, 64> Record;4524 4525 // Parts of bitcode parsing depend on the datalayout. Make sure we4526 // finalize the datalayout before we run any of that code.4527 bool ResolvedDataLayout = false;4528 // In order to support importing modules with illegal data layout strings,4529 // delay parsing the data layout string until after upgrades and overrides4530 // have been applied, allowing to fix illegal data layout strings.4531 // Initialize to the current module's layout string in case none is specified.4532 std::string TentativeDataLayoutStr = TheModule->getDataLayoutStr();4533 4534 auto ResolveDataLayout = [&]() -> Error {4535 if (ResolvedDataLayout)4536 return Error::success();4537 4538 // Datalayout and triple can't be parsed after this point.4539 ResolvedDataLayout = true;4540 4541 // Auto-upgrade the layout string4542 TentativeDataLayoutStr = llvm::UpgradeDataLayoutString(4543 TentativeDataLayoutStr, TheModule->getTargetTriple().str());4544 4545 // Apply override4546 if (Callbacks.DataLayout) {4547 if (auto LayoutOverride = (*Callbacks.DataLayout)(4548 TheModule->getTargetTriple().str(), TentativeDataLayoutStr))4549 TentativeDataLayoutStr = *LayoutOverride;4550 }4551 4552 // Now the layout string is finalized in TentativeDataLayoutStr. Parse it.4553 Expected<DataLayout> MaybeDL = DataLayout::parse(TentativeDataLayoutStr);4554 if (!MaybeDL)4555 return MaybeDL.takeError();4556 4557 TheModule->setDataLayout(MaybeDL.get());4558 return Error::success();4559 };4560 4561 // Read all the records for this module.4562 while (true) {4563 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();4564 if (!MaybeEntry)4565 return MaybeEntry.takeError();4566 llvm::BitstreamEntry Entry = MaybeEntry.get();4567 4568 switch (Entry.Kind) {4569 case BitstreamEntry::Error:4570 return error("Malformed block");4571 case BitstreamEntry::EndBlock:4572 if (Error Err = ResolveDataLayout())4573 return Err;4574 return globalCleanup();4575 4576 case BitstreamEntry::SubBlock:4577 switch (Entry.ID) {4578 default: // Skip unknown content.4579 if (Error Err = Stream.SkipBlock())4580 return Err;4581 break;4582 case bitc::BLOCKINFO_BLOCK_ID:4583 if (Error Err = readBlockInfo())4584 return Err;4585 break;4586 case bitc::PARAMATTR_BLOCK_ID:4587 if (Error Err = parseAttributeBlock())4588 return Err;4589 break;4590 case bitc::PARAMATTR_GROUP_BLOCK_ID:4591 if (Error Err = parseAttributeGroupBlock())4592 return Err;4593 break;4594 case bitc::TYPE_BLOCK_ID_NEW:4595 if (Error Err = parseTypeTable())4596 return Err;4597 break;4598 case bitc::VALUE_SYMTAB_BLOCK_ID:4599 if (!SeenValueSymbolTable) {4600 // Either this is an old form VST without function index and an4601 // associated VST forward declaration record (which would have caused4602 // the VST to be jumped to and parsed before it was encountered4603 // normally in the stream), or there were no function blocks to4604 // trigger an earlier parsing of the VST.4605 assert(VSTOffset == 0 || FunctionsWithBodies.empty());4606 if (Error Err = parseValueSymbolTable())4607 return Err;4608 SeenValueSymbolTable = true;4609 } else {4610 // We must have had a VST forward declaration record, which caused4611 // the parser to jump to and parse the VST earlier.4612 assert(VSTOffset > 0);4613 if (Error Err = Stream.SkipBlock())4614 return Err;4615 }4616 break;4617 case bitc::CONSTANTS_BLOCK_ID:4618 if (Error Err = parseConstants())4619 return Err;4620 if (Error Err = resolveGlobalAndIndirectSymbolInits())4621 return Err;4622 break;4623 case bitc::METADATA_BLOCK_ID:4624 if (ShouldLazyLoadMetadata) {4625 if (Error Err = rememberAndSkipMetadata())4626 return Err;4627 break;4628 }4629 assert(DeferredMetadataInfo.empty() && "Unexpected deferred metadata");4630 if (Error Err = MDLoader->parseModuleMetadata())4631 return Err;4632 break;4633 case bitc::METADATA_KIND_BLOCK_ID:4634 if (Error Err = MDLoader->parseMetadataKinds())4635 return Err;4636 break;4637 case bitc::FUNCTION_BLOCK_ID:4638 if (Error Err = ResolveDataLayout())4639 return Err;4640 4641 // If this is the first function body we've seen, reverse the4642 // FunctionsWithBodies list.4643 if (!SeenFirstFunctionBody) {4644 std::reverse(FunctionsWithBodies.begin(), FunctionsWithBodies.end());4645 if (Error Err = globalCleanup())4646 return Err;4647 SeenFirstFunctionBody = true;4648 }4649 4650 if (VSTOffset > 0) {4651 // If we have a VST forward declaration record, make sure we4652 // parse the VST now if we haven't already. It is needed to4653 // set up the DeferredFunctionInfo vector for lazy reading.4654 if (!SeenValueSymbolTable) {4655 if (Error Err = BitcodeReader::parseValueSymbolTable(VSTOffset))4656 return Err;4657 SeenValueSymbolTable = true;4658 // Fall through so that we record the NextUnreadBit below.4659 // This is necessary in case we have an anonymous function that4660 // is later materialized. Since it will not have a VST entry we4661 // need to fall back to the lazy parse to find its offset.4662 } else {4663 // If we have a VST forward declaration record, but have already4664 // parsed the VST (just above, when the first function body was4665 // encountered here), then we are resuming the parse after4666 // materializing functions. The ResumeBit points to the4667 // start of the last function block recorded in the4668 // DeferredFunctionInfo map. Skip it.4669 if (Error Err = Stream.SkipBlock())4670 return Err;4671 continue;4672 }4673 }4674 4675 // Support older bitcode files that did not have the function4676 // index in the VST, nor a VST forward declaration record, as4677 // well as anonymous functions that do not have VST entries.4678 // Build the DeferredFunctionInfo vector on the fly.4679 if (Error Err = rememberAndSkipFunctionBody())4680 return Err;4681 4682 // Suspend parsing when we reach the function bodies. Subsequent4683 // materialization calls will resume it when necessary. If the bitcode4684 // file is old, the symbol table will be at the end instead and will not4685 // have been seen yet. In this case, just finish the parse now.4686 if (SeenValueSymbolTable) {4687 NextUnreadBit = Stream.GetCurrentBitNo();4688 // After the VST has been parsed, we need to make sure intrinsic name4689 // are auto-upgraded.4690 return globalCleanup();4691 }4692 break;4693 case bitc::USELIST_BLOCK_ID:4694 if (Error Err = parseUseLists())4695 return Err;4696 break;4697 case bitc::OPERAND_BUNDLE_TAGS_BLOCK_ID:4698 if (Error Err = parseOperandBundleTags())4699 return Err;4700 break;4701 case bitc::SYNC_SCOPE_NAMES_BLOCK_ID:4702 if (Error Err = parseSyncScopeNames())4703 return Err;4704 break;4705 }4706 continue;4707 4708 case BitstreamEntry::Record:4709 // The interesting case.4710 break;4711 }4712 4713 // Read a record.4714 Expected<unsigned> MaybeBitCode = Stream.readRecord(Entry.ID, Record);4715 if (!MaybeBitCode)4716 return MaybeBitCode.takeError();4717 switch (unsigned BitCode = MaybeBitCode.get()) {4718 default: break; // Default behavior, ignore unknown content.4719 case bitc::MODULE_CODE_VERSION: {4720 Expected<unsigned> VersionOrErr = parseVersionRecord(Record);4721 if (!VersionOrErr)4722 return VersionOrErr.takeError();4723 UseRelativeIDs = *VersionOrErr >= 1;4724 break;4725 }4726 case bitc::MODULE_CODE_TRIPLE: { // TRIPLE: [strchr x N]4727 if (ResolvedDataLayout)4728 return error("target triple too late in module");4729 std::string S;4730 if (convertToString(Record, 0, S))4731 return error("Invalid record");4732 TheModule->setTargetTriple(Triple(std::move(S)));4733 break;4734 }4735 case bitc::MODULE_CODE_DATALAYOUT: { // DATALAYOUT: [strchr x N]4736 if (ResolvedDataLayout)4737 return error("datalayout too late in module");4738 if (convertToString(Record, 0, TentativeDataLayoutStr))4739 return error("Invalid record");4740 break;4741 }4742 case bitc::MODULE_CODE_ASM: { // ASM: [strchr x N]4743 std::string S;4744 if (convertToString(Record, 0, S))4745 return error("Invalid record");4746 TheModule->setModuleInlineAsm(S);4747 break;4748 }4749 case bitc::MODULE_CODE_DEPLIB: { // DEPLIB: [strchr x N]4750 // Deprecated, but still needed to read old bitcode files.4751 std::string S;4752 if (convertToString(Record, 0, S))4753 return error("Invalid record");4754 // Ignore value.4755 break;4756 }4757 case bitc::MODULE_CODE_SECTIONNAME: { // SECTIONNAME: [strchr x N]4758 std::string S;4759 if (convertToString(Record, 0, S))4760 return error("Invalid record");4761 SectionTable.push_back(S);4762 break;4763 }4764 case bitc::MODULE_CODE_GCNAME: { // SECTIONNAME: [strchr x N]4765 std::string S;4766 if (convertToString(Record, 0, S))4767 return error("Invalid record");4768 GCTable.push_back(S);4769 break;4770 }4771 case bitc::MODULE_CODE_COMDAT:4772 if (Error Err = parseComdatRecord(Record))4773 return Err;4774 break;4775 // FIXME: BitcodeReader should handle {GLOBALVAR, FUNCTION, ALIAS, IFUNC}4776 // written by ThinLinkBitcodeWriter. See4777 // `ThinLinkBitcodeWriter::writeSimplifiedModuleInfo` for the format of each4778 // record4779 // (https://github.com/llvm/llvm-project/blob/b6a93967d9c11e79802b5e75cec1584d6c8aa472/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp#L4714)4780 case bitc::MODULE_CODE_GLOBALVAR:4781 if (Error Err = parseGlobalVarRecord(Record))4782 return Err;4783 break;4784 case bitc::MODULE_CODE_FUNCTION:4785 if (Error Err = ResolveDataLayout())4786 return Err;4787 if (Error Err = parseFunctionRecord(Record))4788 return Err;4789 break;4790 case bitc::MODULE_CODE_IFUNC:4791 case bitc::MODULE_CODE_ALIAS:4792 case bitc::MODULE_CODE_ALIAS_OLD:4793 if (Error Err = parseGlobalIndirectSymbolRecord(BitCode, Record))4794 return Err;4795 break;4796 /// MODULE_CODE_VSTOFFSET: [offset]4797 case bitc::MODULE_CODE_VSTOFFSET:4798 if (Record.empty())4799 return error("Invalid record");4800 // Note that we subtract 1 here because the offset is relative to one word4801 // before the start of the identification or module block, which was4802 // historically always the start of the regular bitcode header.4803 VSTOffset = Record[0] - 1;4804 break;4805 /// MODULE_CODE_SOURCE_FILENAME: [namechar x N]4806 case bitc::MODULE_CODE_SOURCE_FILENAME:4807 SmallString<128> ValueName;4808 if (convertToString(Record, 0, ValueName))4809 return error("Invalid record");4810 TheModule->setSourceFileName(ValueName);4811 break;4812 }4813 Record.clear();4814 }4815 this->ValueTypeCallback = std::nullopt;4816 return Error::success();4817}4818 4819Error BitcodeReader::parseBitcodeInto(Module *M, bool ShouldLazyLoadMetadata,4820 bool IsImporting,4821 ParserCallbacks Callbacks) {4822 TheModule = M;4823 MetadataLoaderCallbacks MDCallbacks;4824 MDCallbacks.GetTypeByID = [&](unsigned ID) { return getTypeByID(ID); };4825 MDCallbacks.GetContainedTypeID = [&](unsigned I, unsigned J) {4826 return getContainedTypeID(I, J);4827 };4828 MDCallbacks.MDType = Callbacks.MDType;4829 MDLoader = MetadataLoader(Stream, *M, ValueList, IsImporting, MDCallbacks);4830 return parseModule(0, ShouldLazyLoadMetadata, Callbacks);4831}4832 4833Error BitcodeReader::typeCheckLoadStoreInst(Type *ValType, Type *PtrType) {4834 if (!isa<PointerType>(PtrType))4835 return error("Load/Store operand is not a pointer type");4836 if (!PointerType::isLoadableOrStorableType(ValType))4837 return error("Cannot load/store from pointer");4838 return Error::success();4839}4840 4841Error BitcodeReader::propagateAttributeTypes(CallBase *CB,4842 ArrayRef<unsigned> ArgTyIDs) {4843 AttributeList Attrs = CB->getAttributes();4844 for (unsigned i = 0; i != CB->arg_size(); ++i) {4845 for (Attribute::AttrKind Kind : {Attribute::ByVal, Attribute::StructRet,4846 Attribute::InAlloca}) {4847 if (!Attrs.hasParamAttr(i, Kind) ||4848 Attrs.getParamAttr(i, Kind).getValueAsType())4849 continue;4850 4851 Type *PtrEltTy = getPtrElementTypeByID(ArgTyIDs[i]);4852 if (!PtrEltTy)4853 return error("Missing element type for typed attribute upgrade");4854 4855 Attribute NewAttr;4856 switch (Kind) {4857 case Attribute::ByVal:4858 NewAttr = Attribute::getWithByValType(Context, PtrEltTy);4859 break;4860 case Attribute::StructRet:4861 NewAttr = Attribute::getWithStructRetType(Context, PtrEltTy);4862 break;4863 case Attribute::InAlloca:4864 NewAttr = Attribute::getWithInAllocaType(Context, PtrEltTy);4865 break;4866 default:4867 llvm_unreachable("not an upgraded type attribute");4868 }4869 4870 Attrs = Attrs.addParamAttribute(Context, i, NewAttr);4871 }4872 }4873 4874 if (CB->isInlineAsm()) {4875 const InlineAsm *IA = cast<InlineAsm>(CB->getCalledOperand());4876 unsigned ArgNo = 0;4877 for (const InlineAsm::ConstraintInfo &CI : IA->ParseConstraints()) {4878 if (!CI.hasArg())4879 continue;4880 4881 if (CI.isIndirect && !Attrs.getParamElementType(ArgNo)) {4882 Type *ElemTy = getPtrElementTypeByID(ArgTyIDs[ArgNo]);4883 if (!ElemTy)4884 return error("Missing element type for inline asm upgrade");4885 Attrs = Attrs.addParamAttribute(4886 Context, ArgNo,4887 Attribute::get(Context, Attribute::ElementType, ElemTy));4888 }4889 4890 ArgNo++;4891 }4892 }4893 4894 switch (CB->getIntrinsicID()) {4895 case Intrinsic::preserve_array_access_index:4896 case Intrinsic::preserve_struct_access_index:4897 case Intrinsic::aarch64_ldaxr:4898 case Intrinsic::aarch64_ldxr:4899 case Intrinsic::aarch64_stlxr:4900 case Intrinsic::aarch64_stxr:4901 case Intrinsic::arm_ldaex:4902 case Intrinsic::arm_ldrex:4903 case Intrinsic::arm_stlex:4904 case Intrinsic::arm_strex: {4905 unsigned ArgNo;4906 switch (CB->getIntrinsicID()) {4907 case Intrinsic::aarch64_stlxr:4908 case Intrinsic::aarch64_stxr:4909 case Intrinsic::arm_stlex:4910 case Intrinsic::arm_strex:4911 ArgNo = 1;4912 break;4913 default:4914 ArgNo = 0;4915 break;4916 }4917 if (!Attrs.getParamElementType(ArgNo)) {4918 Type *ElTy = getPtrElementTypeByID(ArgTyIDs[ArgNo]);4919 if (!ElTy)4920 return error("Missing element type for elementtype upgrade");4921 Attribute NewAttr = Attribute::get(Context, Attribute::ElementType, ElTy);4922 Attrs = Attrs.addParamAttribute(Context, ArgNo, NewAttr);4923 }4924 break;4925 }4926 default:4927 break;4928 }4929 4930 CB->setAttributes(Attrs);4931 return Error::success();4932}4933 4934/// Lazily parse the specified function body block.4935Error BitcodeReader::parseFunctionBody(Function *F) {4936 if (Error Err = Stream.EnterSubBlock(bitc::FUNCTION_BLOCK_ID))4937 return Err;4938 4939 // Unexpected unresolved metadata when parsing function.4940 if (MDLoader->hasFwdRefs())4941 return error("Invalid function metadata: incoming forward references");4942 4943 InstructionList.clear();4944 unsigned ModuleValueListSize = ValueList.size();4945 unsigned ModuleMDLoaderSize = MDLoader->size();4946 4947 // Add all the function arguments to the value table.4948 unsigned ArgNo = 0;4949 unsigned FTyID = FunctionTypeIDs[F];4950 for (Argument &I : F->args()) {4951 unsigned ArgTyID = getContainedTypeID(FTyID, ArgNo + 1);4952 assert(I.getType() == getTypeByID(ArgTyID) &&4953 "Incorrect fully specified type for Function Argument");4954 ValueList.push_back(&I, ArgTyID);4955 ++ArgNo;4956 }4957 unsigned NextValueNo = ValueList.size();4958 BasicBlock *CurBB = nullptr;4959 unsigned CurBBNo = 0;4960 // Block into which constant expressions from phi nodes are materialized.4961 BasicBlock *PhiConstExprBB = nullptr;4962 // Edge blocks for phi nodes into which constant expressions have been4963 // expanded.4964 SmallMapVector<std::pair<BasicBlock *, BasicBlock *>, BasicBlock *, 4>4965 ConstExprEdgeBBs;4966 4967 DebugLoc LastLoc;4968 auto getLastInstruction = [&]() -> Instruction * {4969 if (CurBB && !CurBB->empty())4970 return &CurBB->back();4971 else if (CurBBNo && FunctionBBs[CurBBNo - 1] &&4972 !FunctionBBs[CurBBNo - 1]->empty())4973 return &FunctionBBs[CurBBNo - 1]->back();4974 return nullptr;4975 };4976 4977 std::vector<OperandBundleDef> OperandBundles;4978 4979 // Read all the records.4980 SmallVector<uint64_t, 64> Record;4981 4982 while (true) {4983 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();4984 if (!MaybeEntry)4985 return MaybeEntry.takeError();4986 llvm::BitstreamEntry Entry = MaybeEntry.get();4987 4988 switch (Entry.Kind) {4989 case BitstreamEntry::Error:4990 return error("Malformed block");4991 case BitstreamEntry::EndBlock:4992 goto OutOfRecordLoop;4993 4994 case BitstreamEntry::SubBlock:4995 switch (Entry.ID) {4996 default: // Skip unknown content.4997 if (Error Err = Stream.SkipBlock())4998 return Err;4999 break;5000 case bitc::CONSTANTS_BLOCK_ID:5001 if (Error Err = parseConstants())5002 return Err;5003 NextValueNo = ValueList.size();5004 break;5005 case bitc::VALUE_SYMTAB_BLOCK_ID:5006 if (Error Err = parseValueSymbolTable())5007 return Err;5008 break;5009 case bitc::METADATA_ATTACHMENT_ID:5010 if (Error Err = MDLoader->parseMetadataAttachment(*F, InstructionList))5011 return Err;5012 break;5013 case bitc::METADATA_BLOCK_ID:5014 assert(DeferredMetadataInfo.empty() &&5015 "Must read all module-level metadata before function-level");5016 if (Error Err = MDLoader->parseFunctionMetadata())5017 return Err;5018 break;5019 case bitc::USELIST_BLOCK_ID:5020 if (Error Err = parseUseLists())5021 return Err;5022 break;5023 }5024 continue;5025 5026 case BitstreamEntry::Record:5027 // The interesting case.5028 break;5029 }5030 5031 // Read a record.5032 Record.clear();5033 Instruction *I = nullptr;5034 unsigned ResTypeID = InvalidTypeID;5035 Expected<unsigned> MaybeBitCode = Stream.readRecord(Entry.ID, Record);5036 if (!MaybeBitCode)5037 return MaybeBitCode.takeError();5038 switch (unsigned BitCode = MaybeBitCode.get()) {5039 default: // Default behavior: reject5040 return error("Invalid value");5041 case bitc::FUNC_CODE_DECLAREBLOCKS: { // DECLAREBLOCKS: [nblocks]5042 if (Record.empty() || Record[0] == 0)5043 return error("Invalid record");5044 // Create all the basic blocks for the function.5045 FunctionBBs.resize(Record[0]);5046 5047 // See if anything took the address of blocks in this function.5048 auto BBFRI = BasicBlockFwdRefs.find(F);5049 if (BBFRI == BasicBlockFwdRefs.end()) {5050 for (BasicBlock *&BB : FunctionBBs)5051 BB = BasicBlock::Create(Context, "", F);5052 } else {5053 auto &BBRefs = BBFRI->second;5054 // Check for invalid basic block references.5055 if (BBRefs.size() > FunctionBBs.size())5056 return error("Invalid ID");5057 assert(!BBRefs.empty() && "Unexpected empty array");5058 assert(!BBRefs.front() && "Invalid reference to entry block");5059 for (unsigned I = 0, E = FunctionBBs.size(), RE = BBRefs.size(); I != E;5060 ++I)5061 if (I < RE && BBRefs[I]) {5062 BBRefs[I]->insertInto(F);5063 FunctionBBs[I] = BBRefs[I];5064 } else {5065 FunctionBBs[I] = BasicBlock::Create(Context, "", F);5066 }5067 5068 // Erase from the table.5069 BasicBlockFwdRefs.erase(BBFRI);5070 }5071 5072 CurBB = FunctionBBs[0];5073 continue;5074 }5075 5076 case bitc::FUNC_CODE_BLOCKADDR_USERS: // BLOCKADDR_USERS: [vals...]5077 // The record should not be emitted if it's an empty list.5078 if (Record.empty())5079 return error("Invalid record");5080 // When we have the RARE case of a BlockAddress Constant that is not5081 // scoped to the Function it refers to, we need to conservatively5082 // materialize the referred to Function, regardless of whether or not5083 // that Function will ultimately be linked, otherwise users of5084 // BitcodeReader might start splicing out Function bodies such that we5085 // might no longer be able to materialize the BlockAddress since the5086 // BasicBlock (and entire body of the Function) the BlockAddress refers5087 // to may have been moved. In the case that the user of BitcodeReader5088 // decides ultimately not to link the Function body, materializing here5089 // could be considered wasteful, but it's better than a deserialization5090 // failure as described. This keeps BitcodeReader unaware of complex5091 // linkage policy decisions such as those use by LTO, leaving those5092 // decisions "one layer up."5093 for (uint64_t ValID : Record)5094 if (auto *F = dyn_cast<Function>(ValueList[ValID]))5095 BackwardRefFunctions.push_back(F);5096 else5097 return error("Invalid record");5098 5099 continue;5100 5101 case bitc::FUNC_CODE_DEBUG_LOC_AGAIN: // DEBUG_LOC_AGAIN5102 // This record indicates that the last instruction is at the same5103 // location as the previous instruction with a location.5104 I = getLastInstruction();5105 5106 if (!I)5107 return error("Invalid record");5108 I->setDebugLoc(LastLoc);5109 I = nullptr;5110 continue;5111 5112 case bitc::FUNC_CODE_DEBUG_LOC: { // DEBUG_LOC: [line, col, scope, ia]5113 I = getLastInstruction();5114 if (!I || Record.size() < 4)5115 return error("Invalid record");5116 5117 unsigned Line = Record[0], Col = Record[1];5118 unsigned ScopeID = Record[2], IAID = Record[3];5119 bool isImplicitCode = Record.size() >= 5 && Record[4];5120 uint64_t AtomGroup = Record.size() == 7 ? Record[5] : 0;5121 uint8_t AtomRank = Record.size() == 7 ? Record[6] : 0;5122 5123 MDNode *Scope = nullptr, *IA = nullptr;5124 if (ScopeID) {5125 Scope = dyn_cast_or_null<MDNode>(5126 MDLoader->getMetadataFwdRefOrLoad(ScopeID - 1));5127 if (!Scope)5128 return error("Invalid record");5129 }5130 if (IAID) {5131 IA = dyn_cast_or_null<MDNode>(5132 MDLoader->getMetadataFwdRefOrLoad(IAID - 1));5133 if (!IA)5134 return error("Invalid record");5135 }5136 5137 LastLoc = DILocation::get(Scope->getContext(), Line, Col, Scope, IA,5138 isImplicitCode, AtomGroup, AtomRank);5139 I->setDebugLoc(LastLoc);5140 I = nullptr;5141 continue;5142 }5143 case bitc::FUNC_CODE_INST_UNOP: { // UNOP: [opval, ty, opcode]5144 unsigned OpNum = 0;5145 Value *LHS;5146 unsigned TypeID;5147 if (getValueTypePair(Record, OpNum, NextValueNo, LHS, TypeID, CurBB) ||5148 OpNum+1 > Record.size())5149 return error("Invalid record");5150 5151 int Opc = getDecodedUnaryOpcode(Record[OpNum++], LHS->getType());5152 if (Opc == -1)5153 return error("Invalid record");5154 I = UnaryOperator::Create((Instruction::UnaryOps)Opc, LHS);5155 ResTypeID = TypeID;5156 InstructionList.push_back(I);5157 if (OpNum < Record.size()) {5158 if (isa<FPMathOperator>(I)) {5159 FastMathFlags FMF = getDecodedFastMathFlags(Record[OpNum]);5160 if (FMF.any())5161 I->setFastMathFlags(FMF);5162 }5163 }5164 break;5165 }5166 case bitc::FUNC_CODE_INST_BINOP: { // BINOP: [opval, ty, opval, opcode]5167 unsigned OpNum = 0;5168 Value *LHS, *RHS;5169 unsigned TypeID;5170 if (getValueTypePair(Record, OpNum, NextValueNo, LHS, TypeID, CurBB) ||5171 popValue(Record, OpNum, NextValueNo, LHS->getType(), TypeID, RHS,5172 CurBB) ||5173 OpNum+1 > Record.size())5174 return error("Invalid record");5175 5176 int Opc = getDecodedBinaryOpcode(Record[OpNum++], LHS->getType());5177 if (Opc == -1)5178 return error("Invalid record");5179 I = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS);5180 ResTypeID = TypeID;5181 InstructionList.push_back(I);5182 if (OpNum < Record.size()) {5183 if (Opc == Instruction::Add ||5184 Opc == Instruction::Sub ||5185 Opc == Instruction::Mul ||5186 Opc == Instruction::Shl) {5187 if (Record[OpNum] & (1 << bitc::OBO_NO_SIGNED_WRAP))5188 cast<BinaryOperator>(I)->setHasNoSignedWrap(true);5189 if (Record[OpNum] & (1 << bitc::OBO_NO_UNSIGNED_WRAP))5190 cast<BinaryOperator>(I)->setHasNoUnsignedWrap(true);5191 } else if (Opc == Instruction::SDiv ||5192 Opc == Instruction::UDiv ||5193 Opc == Instruction::LShr ||5194 Opc == Instruction::AShr) {5195 if (Record[OpNum] & (1 << bitc::PEO_EXACT))5196 cast<BinaryOperator>(I)->setIsExact(true);5197 } else if (Opc == Instruction::Or) {5198 if (Record[OpNum] & (1 << bitc::PDI_DISJOINT))5199 cast<PossiblyDisjointInst>(I)->setIsDisjoint(true);5200 } else if (isa<FPMathOperator>(I)) {5201 FastMathFlags FMF = getDecodedFastMathFlags(Record[OpNum]);5202 if (FMF.any())5203 I->setFastMathFlags(FMF);5204 }5205 }5206 break;5207 }5208 case bitc::FUNC_CODE_INST_CAST: { // CAST: [opval, opty, destty, castopc]5209 unsigned OpNum = 0;5210 Value *Op;5211 unsigned OpTypeID;5212 if (getValueTypePair(Record, OpNum, NextValueNo, Op, OpTypeID, CurBB) ||5213 OpNum + 1 > Record.size())5214 return error("Invalid record");5215 5216 ResTypeID = Record[OpNum++];5217 Type *ResTy = getTypeByID(ResTypeID);5218 int Opc = getDecodedCastOpcode(Record[OpNum++]);5219 5220 if (Opc == -1 || !ResTy)5221 return error("Invalid record");5222 Instruction *Temp = nullptr;5223 if ((I = UpgradeBitCastInst(Opc, Op, ResTy, Temp))) {5224 if (Temp) {5225 InstructionList.push_back(Temp);5226 assert(CurBB && "No current BB?");5227 Temp->insertInto(CurBB, CurBB->end());5228 }5229 } else {5230 auto CastOp = (Instruction::CastOps)Opc;5231 if (!CastInst::castIsValid(CastOp, Op, ResTy))5232 return error("Invalid cast");5233 I = CastInst::Create(CastOp, Op, ResTy);5234 }5235 5236 if (OpNum < Record.size()) {5237 if (Opc == Instruction::ZExt || Opc == Instruction::UIToFP) {5238 if (Record[OpNum] & (1 << bitc::PNNI_NON_NEG))5239 cast<PossiblyNonNegInst>(I)->setNonNeg(true);5240 } else if (Opc == Instruction::Trunc) {5241 if (Record[OpNum] & (1 << bitc::TIO_NO_UNSIGNED_WRAP))5242 cast<TruncInst>(I)->setHasNoUnsignedWrap(true);5243 if (Record[OpNum] & (1 << bitc::TIO_NO_SIGNED_WRAP))5244 cast<TruncInst>(I)->setHasNoSignedWrap(true);5245 }5246 if (isa<FPMathOperator>(I)) {5247 FastMathFlags FMF = getDecodedFastMathFlags(Record[OpNum]);5248 if (FMF.any())5249 I->setFastMathFlags(FMF);5250 }5251 }5252 5253 InstructionList.push_back(I);5254 break;5255 }5256 case bitc::FUNC_CODE_INST_INBOUNDS_GEP_OLD:5257 case bitc::FUNC_CODE_INST_GEP_OLD:5258 case bitc::FUNC_CODE_INST_GEP: { // GEP: type, [n x operands]5259 unsigned OpNum = 0;5260 5261 unsigned TyID;5262 Type *Ty;5263 GEPNoWrapFlags NW;5264 5265 if (BitCode == bitc::FUNC_CODE_INST_GEP) {5266 NW = toGEPNoWrapFlags(Record[OpNum++]);5267 TyID = Record[OpNum++];5268 Ty = getTypeByID(TyID);5269 } else {5270 if (BitCode == bitc::FUNC_CODE_INST_INBOUNDS_GEP_OLD)5271 NW = GEPNoWrapFlags::inBounds();5272 TyID = InvalidTypeID;5273 Ty = nullptr;5274 }5275 5276 Value *BasePtr;5277 unsigned BasePtrTypeID;5278 if (getValueTypePair(Record, OpNum, NextValueNo, BasePtr, BasePtrTypeID,5279 CurBB))5280 return error("Invalid record");5281 5282 if (!Ty) {5283 TyID = getContainedTypeID(BasePtrTypeID);5284 if (BasePtr->getType()->isVectorTy())5285 TyID = getContainedTypeID(TyID);5286 Ty = getTypeByID(TyID);5287 }5288 5289 SmallVector<Value*, 16> GEPIdx;5290 while (OpNum != Record.size()) {5291 Value *Op;5292 unsigned OpTypeID;5293 if (getValueTypePair(Record, OpNum, NextValueNo, Op, OpTypeID, CurBB))5294 return error("Invalid record");5295 GEPIdx.push_back(Op);5296 }5297 5298 auto *GEP = GetElementPtrInst::Create(Ty, BasePtr, GEPIdx);5299 I = GEP;5300 5301 ResTypeID = TyID;5302 if (cast<GEPOperator>(I)->getNumIndices() != 0) {5303 auto GTI = std::next(gep_type_begin(I));5304 for (Value *Idx : drop_begin(cast<GEPOperator>(I)->indices())) {5305 unsigned SubType = 0;5306 if (GTI.isStruct()) {5307 ConstantInt *IdxC =5308 Idx->getType()->isVectorTy()5309 ? cast<ConstantInt>(cast<Constant>(Idx)->getSplatValue())5310 : cast<ConstantInt>(Idx);5311 SubType = IdxC->getZExtValue();5312 }5313 ResTypeID = getContainedTypeID(ResTypeID, SubType);5314 ++GTI;5315 }5316 }5317 5318 // At this point ResTypeID is the result element type. We need a pointer5319 // or vector of pointer to it.5320 ResTypeID = getVirtualTypeID(I->getType()->getScalarType(), ResTypeID);5321 if (I->getType()->isVectorTy())5322 ResTypeID = getVirtualTypeID(I->getType(), ResTypeID);5323 5324 InstructionList.push_back(I);5325 GEP->setNoWrapFlags(NW);5326 break;5327 }5328 5329 case bitc::FUNC_CODE_INST_EXTRACTVAL: {5330 // EXTRACTVAL: [opty, opval, n x indices]5331 unsigned OpNum = 0;5332 Value *Agg;5333 unsigned AggTypeID;5334 if (getValueTypePair(Record, OpNum, NextValueNo, Agg, AggTypeID, CurBB))5335 return error("Invalid record");5336 Type *Ty = Agg->getType();5337 5338 unsigned RecSize = Record.size();5339 if (OpNum == RecSize)5340 return error("EXTRACTVAL: Invalid instruction with 0 indices");5341 5342 SmallVector<unsigned, 4> EXTRACTVALIdx;5343 ResTypeID = AggTypeID;5344 for (; OpNum != RecSize; ++OpNum) {5345 bool IsArray = Ty->isArrayTy();5346 bool IsStruct = Ty->isStructTy();5347 uint64_t Index = Record[OpNum];5348 5349 if (!IsStruct && !IsArray)5350 return error("EXTRACTVAL: Invalid type");5351 if ((unsigned)Index != Index)5352 return error("Invalid value");5353 if (IsStruct && Index >= Ty->getStructNumElements())5354 return error("EXTRACTVAL: Invalid struct index");5355 if (IsArray && Index >= Ty->getArrayNumElements())5356 return error("EXTRACTVAL: Invalid array index");5357 EXTRACTVALIdx.push_back((unsigned)Index);5358 5359 if (IsStruct) {5360 Ty = Ty->getStructElementType(Index);5361 ResTypeID = getContainedTypeID(ResTypeID, Index);5362 } else {5363 Ty = Ty->getArrayElementType();5364 ResTypeID = getContainedTypeID(ResTypeID);5365 }5366 }5367 5368 I = ExtractValueInst::Create(Agg, EXTRACTVALIdx);5369 InstructionList.push_back(I);5370 break;5371 }5372 5373 case bitc::FUNC_CODE_INST_INSERTVAL: {5374 // INSERTVAL: [opty, opval, opty, opval, n x indices]5375 unsigned OpNum = 0;5376 Value *Agg;5377 unsigned AggTypeID;5378 if (getValueTypePair(Record, OpNum, NextValueNo, Agg, AggTypeID, CurBB))5379 return error("Invalid record");5380 Value *Val;5381 unsigned ValTypeID;5382 if (getValueTypePair(Record, OpNum, NextValueNo, Val, ValTypeID, CurBB))5383 return error("Invalid record");5384 5385 unsigned RecSize = Record.size();5386 if (OpNum == RecSize)5387 return error("INSERTVAL: Invalid instruction with 0 indices");5388 5389 SmallVector<unsigned, 4> INSERTVALIdx;5390 Type *CurTy = Agg->getType();5391 for (; OpNum != RecSize; ++OpNum) {5392 bool IsArray = CurTy->isArrayTy();5393 bool IsStruct = CurTy->isStructTy();5394 uint64_t Index = Record[OpNum];5395 5396 if (!IsStruct && !IsArray)5397 return error("INSERTVAL: Invalid type");5398 if ((unsigned)Index != Index)5399 return error("Invalid value");5400 if (IsStruct && Index >= CurTy->getStructNumElements())5401 return error("INSERTVAL: Invalid struct index");5402 if (IsArray && Index >= CurTy->getArrayNumElements())5403 return error("INSERTVAL: Invalid array index");5404 5405 INSERTVALIdx.push_back((unsigned)Index);5406 if (IsStruct)5407 CurTy = CurTy->getStructElementType(Index);5408 else5409 CurTy = CurTy->getArrayElementType();5410 }5411 5412 if (CurTy != Val->getType())5413 return error("Inserted value type doesn't match aggregate type");5414 5415 I = InsertValueInst::Create(Agg, Val, INSERTVALIdx);5416 ResTypeID = AggTypeID;5417 InstructionList.push_back(I);5418 break;5419 }5420 5421 case bitc::FUNC_CODE_INST_SELECT: { // SELECT: [opval, ty, opval, opval]5422 // obsolete form of select5423 // handles select i1 ... in old bitcode5424 unsigned OpNum = 0;5425 Value *TrueVal, *FalseVal, *Cond;5426 unsigned TypeID;5427 Type *CondType = Type::getInt1Ty(Context);5428 if (getValueTypePair(Record, OpNum, NextValueNo, TrueVal, TypeID,5429 CurBB) ||5430 popValue(Record, OpNum, NextValueNo, TrueVal->getType(), TypeID,5431 FalseVal, CurBB) ||5432 popValue(Record, OpNum, NextValueNo, CondType,5433 getVirtualTypeID(CondType), Cond, CurBB))5434 return error("Invalid record");5435 5436 I = SelectInst::Create(Cond, TrueVal, FalseVal);5437 ResTypeID = TypeID;5438 InstructionList.push_back(I);5439 break;5440 }5441 5442 case bitc::FUNC_CODE_INST_VSELECT: {// VSELECT: [ty,opval,opval,predty,pred]5443 // new form of select5444 // handles select i1 or select [N x i1]5445 unsigned OpNum = 0;5446 Value *TrueVal, *FalseVal, *Cond;5447 unsigned ValTypeID, CondTypeID;5448 if (getValueTypePair(Record, OpNum, NextValueNo, TrueVal, ValTypeID,5449 CurBB) ||5450 popValue(Record, OpNum, NextValueNo, TrueVal->getType(), ValTypeID,5451 FalseVal, CurBB) ||5452 getValueTypePair(Record, OpNum, NextValueNo, Cond, CondTypeID, CurBB))5453 return error("Invalid record");5454 5455 // select condition can be either i1 or [N x i1]5456 if (VectorType* vector_type =5457 dyn_cast<VectorType>(Cond->getType())) {5458 // expect <n x i1>5459 if (vector_type->getElementType() != Type::getInt1Ty(Context))5460 return error("Invalid type for value");5461 } else {5462 // expect i15463 if (Cond->getType() != Type::getInt1Ty(Context))5464 return error("Invalid type for value");5465 }5466 5467 I = SelectInst::Create(Cond, TrueVal, FalseVal);5468 ResTypeID = ValTypeID;5469 InstructionList.push_back(I);5470 if (OpNum < Record.size() && isa<FPMathOperator>(I)) {5471 FastMathFlags FMF = getDecodedFastMathFlags(Record[OpNum]);5472 if (FMF.any())5473 I->setFastMathFlags(FMF);5474 }5475 break;5476 }5477 5478 case bitc::FUNC_CODE_INST_EXTRACTELT: { // EXTRACTELT: [opty, opval, opval]5479 unsigned OpNum = 0;5480 Value *Vec, *Idx;5481 unsigned VecTypeID, IdxTypeID;5482 if (getValueTypePair(Record, OpNum, NextValueNo, Vec, VecTypeID, CurBB) ||5483 getValueTypePair(Record, OpNum, NextValueNo, Idx, IdxTypeID, CurBB))5484 return error("Invalid record");5485 if (!Vec->getType()->isVectorTy())5486 return error("Invalid type for value");5487 I = ExtractElementInst::Create(Vec, Idx);5488 ResTypeID = getContainedTypeID(VecTypeID);5489 InstructionList.push_back(I);5490 break;5491 }5492 5493 case bitc::FUNC_CODE_INST_INSERTELT: { // INSERTELT: [ty, opval,opval,opval]5494 unsigned OpNum = 0;5495 Value *Vec, *Elt, *Idx;5496 unsigned VecTypeID, IdxTypeID;5497 if (getValueTypePair(Record, OpNum, NextValueNo, Vec, VecTypeID, CurBB))5498 return error("Invalid record");5499 if (!Vec->getType()->isVectorTy())5500 return error("Invalid type for value");5501 if (popValue(Record, OpNum, NextValueNo,5502 cast<VectorType>(Vec->getType())->getElementType(),5503 getContainedTypeID(VecTypeID), Elt, CurBB) ||5504 getValueTypePair(Record, OpNum, NextValueNo, Idx, IdxTypeID, CurBB))5505 return error("Invalid record");5506 I = InsertElementInst::Create(Vec, Elt, Idx);5507 ResTypeID = VecTypeID;5508 InstructionList.push_back(I);5509 break;5510 }5511 5512 case bitc::FUNC_CODE_INST_SHUFFLEVEC: {// SHUFFLEVEC: [opval,ty,opval,opval]5513 unsigned OpNum = 0;5514 Value *Vec1, *Vec2, *Mask;5515 unsigned Vec1TypeID;5516 if (getValueTypePair(Record, OpNum, NextValueNo, Vec1, Vec1TypeID,5517 CurBB) ||5518 popValue(Record, OpNum, NextValueNo, Vec1->getType(), Vec1TypeID,5519 Vec2, CurBB))5520 return error("Invalid record");5521 5522 unsigned MaskTypeID;5523 if (getValueTypePair(Record, OpNum, NextValueNo, Mask, MaskTypeID, CurBB))5524 return error("Invalid record");5525 if (!Vec1->getType()->isVectorTy() || !Vec2->getType()->isVectorTy())5526 return error("Invalid type for value");5527 5528 I = new ShuffleVectorInst(Vec1, Vec2, Mask);5529 ResTypeID =5530 getVirtualTypeID(I->getType(), getContainedTypeID(Vec1TypeID));5531 InstructionList.push_back(I);5532 break;5533 }5534 5535 case bitc::FUNC_CODE_INST_CMP: // CMP: [opty, opval, opval, pred]5536 // Old form of ICmp/FCmp returning bool5537 // Existed to differentiate between icmp/fcmp and vicmp/vfcmp which were5538 // both legal on vectors but had different behaviour.5539 case bitc::FUNC_CODE_INST_CMP2: { // CMP2: [opty, opval, opval, pred]5540 // FCmp/ICmp returning bool or vector of bool5541 5542 unsigned OpNum = 0;5543 Value *LHS, *RHS;5544 unsigned LHSTypeID;5545 if (getValueTypePair(Record, OpNum, NextValueNo, LHS, LHSTypeID, CurBB) ||5546 popValue(Record, OpNum, NextValueNo, LHS->getType(), LHSTypeID, RHS,5547 CurBB))5548 return error("Invalid record");5549 5550 if (OpNum >= Record.size())5551 return error(5552 "Invalid record: operand number exceeded available operands");5553 5554 CmpInst::Predicate PredVal = CmpInst::Predicate(Record[OpNum]);5555 bool IsFP = LHS->getType()->isFPOrFPVectorTy();5556 FastMathFlags FMF;5557 if (IsFP && Record.size() > OpNum+1)5558 FMF = getDecodedFastMathFlags(Record[++OpNum]);5559 5560 if (IsFP) {5561 if (!CmpInst::isFPPredicate(PredVal))5562 return error("Invalid fcmp predicate");5563 I = new FCmpInst(PredVal, LHS, RHS);5564 } else {5565 if (!CmpInst::isIntPredicate(PredVal))5566 return error("Invalid icmp predicate");5567 I = new ICmpInst(PredVal, LHS, RHS);5568 if (Record.size() > OpNum + 1 &&5569 (Record[++OpNum] & (1 << bitc::ICMP_SAME_SIGN)))5570 cast<ICmpInst>(I)->setSameSign();5571 }5572 5573 if (OpNum + 1 != Record.size())5574 return error("Invalid record");5575 5576 ResTypeID = getVirtualTypeID(I->getType()->getScalarType());5577 if (LHS->getType()->isVectorTy())5578 ResTypeID = getVirtualTypeID(I->getType(), ResTypeID);5579 5580 if (FMF.any())5581 I->setFastMathFlags(FMF);5582 InstructionList.push_back(I);5583 break;5584 }5585 5586 case bitc::FUNC_CODE_INST_RET: // RET: [opty,opval<optional>]5587 {5588 unsigned Size = Record.size();5589 if (Size == 0) {5590 I = ReturnInst::Create(Context);5591 InstructionList.push_back(I);5592 break;5593 }5594 5595 unsigned OpNum = 0;5596 Value *Op = nullptr;5597 unsigned OpTypeID;5598 if (getValueTypePair(Record, OpNum, NextValueNo, Op, OpTypeID, CurBB))5599 return error("Invalid record");5600 if (OpNum != Record.size())5601 return error("Invalid record");5602 5603 I = ReturnInst::Create(Context, Op);5604 InstructionList.push_back(I);5605 break;5606 }5607 case bitc::FUNC_CODE_INST_BR: { // BR: [bb#, bb#, opval] or [bb#]5608 if (Record.size() != 1 && Record.size() != 3)5609 return error("Invalid record");5610 BasicBlock *TrueDest = getBasicBlock(Record[0]);5611 if (!TrueDest)5612 return error("Invalid record");5613 5614 if (Record.size() == 1) {5615 I = BranchInst::Create(TrueDest);5616 InstructionList.push_back(I);5617 }5618 else {5619 BasicBlock *FalseDest = getBasicBlock(Record[1]);5620 Type *CondType = Type::getInt1Ty(Context);5621 Value *Cond = getValue(Record, 2, NextValueNo, CondType,5622 getVirtualTypeID(CondType), CurBB);5623 if (!FalseDest || !Cond)5624 return error("Invalid record");5625 I = BranchInst::Create(TrueDest, FalseDest, Cond);5626 InstructionList.push_back(I);5627 }5628 break;5629 }5630 case bitc::FUNC_CODE_INST_CLEANUPRET: { // CLEANUPRET: [val] or [val,bb#]5631 if (Record.size() != 1 && Record.size() != 2)5632 return error("Invalid record");5633 unsigned Idx = 0;5634 Type *TokenTy = Type::getTokenTy(Context);5635 Value *CleanupPad = getValue(Record, Idx++, NextValueNo, TokenTy,5636 getVirtualTypeID(TokenTy), CurBB);5637 if (!CleanupPad)5638 return error("Invalid record");5639 BasicBlock *UnwindDest = nullptr;5640 if (Record.size() == 2) {5641 UnwindDest = getBasicBlock(Record[Idx++]);5642 if (!UnwindDest)5643 return error("Invalid record");5644 }5645 5646 I = CleanupReturnInst::Create(CleanupPad, UnwindDest);5647 InstructionList.push_back(I);5648 break;5649 }5650 case bitc::FUNC_CODE_INST_CATCHRET: { // CATCHRET: [val,bb#]5651 if (Record.size() != 2)5652 return error("Invalid record");5653 unsigned Idx = 0;5654 Type *TokenTy = Type::getTokenTy(Context);5655 Value *CatchPad = getValue(Record, Idx++, NextValueNo, TokenTy,5656 getVirtualTypeID(TokenTy), CurBB);5657 if (!CatchPad)5658 return error("Invalid record");5659 BasicBlock *BB = getBasicBlock(Record[Idx++]);5660 if (!BB)5661 return error("Invalid record");5662 5663 I = CatchReturnInst::Create(CatchPad, BB);5664 InstructionList.push_back(I);5665 break;5666 }5667 case bitc::FUNC_CODE_INST_CATCHSWITCH: { // CATCHSWITCH: [tok,num,(bb)*,bb?]5668 // We must have, at minimum, the outer scope and the number of arguments.5669 if (Record.size() < 2)5670 return error("Invalid record");5671 5672 unsigned Idx = 0;5673 5674 Type *TokenTy = Type::getTokenTy(Context);5675 Value *ParentPad = getValue(Record, Idx++, NextValueNo, TokenTy,5676 getVirtualTypeID(TokenTy), CurBB);5677 if (!ParentPad)5678 return error("Invalid record");5679 5680 unsigned NumHandlers = Record[Idx++];5681 5682 SmallVector<BasicBlock *, 2> Handlers;5683 for (unsigned Op = 0; Op != NumHandlers; ++Op) {5684 BasicBlock *BB = getBasicBlock(Record[Idx++]);5685 if (!BB)5686 return error("Invalid record");5687 Handlers.push_back(BB);5688 }5689 5690 BasicBlock *UnwindDest = nullptr;5691 if (Idx + 1 == Record.size()) {5692 UnwindDest = getBasicBlock(Record[Idx++]);5693 if (!UnwindDest)5694 return error("Invalid record");5695 }5696 5697 if (Record.size() != Idx)5698 return error("Invalid record");5699 5700 auto *CatchSwitch =5701 CatchSwitchInst::Create(ParentPad, UnwindDest, NumHandlers);5702 for (BasicBlock *Handler : Handlers)5703 CatchSwitch->addHandler(Handler);5704 I = CatchSwitch;5705 ResTypeID = getVirtualTypeID(I->getType());5706 InstructionList.push_back(I);5707 break;5708 }5709 case bitc::FUNC_CODE_INST_CATCHPAD:5710 case bitc::FUNC_CODE_INST_CLEANUPPAD: { // [tok,num,(ty,val)*]5711 // We must have, at minimum, the outer scope and the number of arguments.5712 if (Record.size() < 2)5713 return error("Invalid record");5714 5715 unsigned Idx = 0;5716 5717 Type *TokenTy = Type::getTokenTy(Context);5718 Value *ParentPad = getValue(Record, Idx++, NextValueNo, TokenTy,5719 getVirtualTypeID(TokenTy), CurBB);5720 if (!ParentPad)5721 return error("Invald record");5722 5723 unsigned NumArgOperands = Record[Idx++];5724 5725 SmallVector<Value *, 2> Args;5726 for (unsigned Op = 0; Op != NumArgOperands; ++Op) {5727 Value *Val;5728 unsigned ValTypeID;5729 if (getValueTypePair(Record, Idx, NextValueNo, Val, ValTypeID, nullptr))5730 return error("Invalid record");5731 Args.push_back(Val);5732 }5733 5734 if (Record.size() != Idx)5735 return error("Invalid record");5736 5737 if (BitCode == bitc::FUNC_CODE_INST_CLEANUPPAD)5738 I = CleanupPadInst::Create(ParentPad, Args);5739 else5740 I = CatchPadInst::Create(ParentPad, Args);5741 ResTypeID = getVirtualTypeID(I->getType());5742 InstructionList.push_back(I);5743 break;5744 }5745 case bitc::FUNC_CODE_INST_SWITCH: { // SWITCH: [opty, op0, op1, ...]5746 // Check magic5747 if ((Record[0] >> 16) == SWITCH_INST_MAGIC) {5748 // "New" SwitchInst format with case ranges. The changes to write this5749 // format were reverted but we still recognize bitcode that uses it.5750 // Hopefully someday we will have support for case ranges and can use5751 // this format again.5752 5753 unsigned OpTyID = Record[1];5754 Type *OpTy = getTypeByID(OpTyID);5755 unsigned ValueBitWidth = cast<IntegerType>(OpTy)->getBitWidth();5756 5757 Value *Cond = getValue(Record, 2, NextValueNo, OpTy, OpTyID, CurBB);5758 BasicBlock *Default = getBasicBlock(Record[3]);5759 if (!OpTy || !Cond || !Default)5760 return error("Invalid record");5761 5762 unsigned NumCases = Record[4];5763 5764 SwitchInst *SI = SwitchInst::Create(Cond, Default, NumCases);5765 InstructionList.push_back(SI);5766 5767 unsigned CurIdx = 5;5768 for (unsigned i = 0; i != NumCases; ++i) {5769 SmallVector<ConstantInt*, 1> CaseVals;5770 unsigned NumItems = Record[CurIdx++];5771 for (unsigned ci = 0; ci != NumItems; ++ci) {5772 bool isSingleNumber = Record[CurIdx++];5773 5774 APInt Low;5775 unsigned ActiveWords = 1;5776 if (ValueBitWidth > 64)5777 ActiveWords = Record[CurIdx++];5778 Low = readWideAPInt(ArrayRef(&Record[CurIdx], ActiveWords),5779 ValueBitWidth);5780 CurIdx += ActiveWords;5781 5782 if (!isSingleNumber) {5783 ActiveWords = 1;5784 if (ValueBitWidth > 64)5785 ActiveWords = Record[CurIdx++];5786 APInt High = readWideAPInt(ArrayRef(&Record[CurIdx], ActiveWords),5787 ValueBitWidth);5788 CurIdx += ActiveWords;5789 5790 // FIXME: It is not clear whether values in the range should be5791 // compared as signed or unsigned values. The partially5792 // implemented changes that used this format in the past used5793 // unsigned comparisons.5794 for ( ; Low.ule(High); ++Low)5795 CaseVals.push_back(ConstantInt::get(Context, Low));5796 } else5797 CaseVals.push_back(ConstantInt::get(Context, Low));5798 }5799 BasicBlock *DestBB = getBasicBlock(Record[CurIdx++]);5800 for (ConstantInt *Cst : CaseVals)5801 SI->addCase(Cst, DestBB);5802 }5803 I = SI;5804 break;5805 }5806 5807 // Old SwitchInst format without case ranges.5808 5809 if (Record.size() < 3 || (Record.size() & 1) == 0)5810 return error("Invalid record");5811 unsigned OpTyID = Record[0];5812 Type *OpTy = getTypeByID(OpTyID);5813 Value *Cond = getValue(Record, 1, NextValueNo, OpTy, OpTyID, CurBB);5814 BasicBlock *Default = getBasicBlock(Record[2]);5815 if (!OpTy || !Cond || !Default)5816 return error("Invalid record");5817 unsigned NumCases = (Record.size()-3)/2;5818 SwitchInst *SI = SwitchInst::Create(Cond, Default, NumCases);5819 InstructionList.push_back(SI);5820 for (unsigned i = 0, e = NumCases; i != e; ++i) {5821 ConstantInt *CaseVal = dyn_cast_or_null<ConstantInt>(5822 getFnValueByID(Record[3+i*2], OpTy, OpTyID, nullptr));5823 BasicBlock *DestBB = getBasicBlock(Record[1+3+i*2]);5824 if (!CaseVal || !DestBB) {5825 delete SI;5826 return error("Invalid record");5827 }5828 SI->addCase(CaseVal, DestBB);5829 }5830 I = SI;5831 break;5832 }5833 case bitc::FUNC_CODE_INST_INDIRECTBR: { // INDIRECTBR: [opty, op0, op1, ...]5834 if (Record.size() < 2)5835 return error("Invalid record");5836 unsigned OpTyID = Record[0];5837 Type *OpTy = getTypeByID(OpTyID);5838 Value *Address = getValue(Record, 1, NextValueNo, OpTy, OpTyID, CurBB);5839 if (!OpTy || !Address)5840 return error("Invalid record");5841 unsigned NumDests = Record.size()-2;5842 IndirectBrInst *IBI = IndirectBrInst::Create(Address, NumDests);5843 InstructionList.push_back(IBI);5844 for (unsigned i = 0, e = NumDests; i != e; ++i) {5845 if (BasicBlock *DestBB = getBasicBlock(Record[2+i])) {5846 IBI->addDestination(DestBB);5847 } else {5848 delete IBI;5849 return error("Invalid record");5850 }5851 }5852 I = IBI;5853 break;5854 }5855 5856 case bitc::FUNC_CODE_INST_INVOKE: {5857 // INVOKE: [attrs, cc, normBB, unwindBB, fnty, op0,op1,op2, ...]5858 if (Record.size() < 4)5859 return error("Invalid record");5860 unsigned OpNum = 0;5861 AttributeList PAL = getAttributes(Record[OpNum++]);5862 unsigned CCInfo = Record[OpNum++];5863 BasicBlock *NormalBB = getBasicBlock(Record[OpNum++]);5864 BasicBlock *UnwindBB = getBasicBlock(Record[OpNum++]);5865 5866 unsigned FTyID = InvalidTypeID;5867 FunctionType *FTy = nullptr;5868 if ((CCInfo >> 13) & 1) {5869 FTyID = Record[OpNum++];5870 FTy = dyn_cast<FunctionType>(getTypeByID(FTyID));5871 if (!FTy)5872 return error("Explicit invoke type is not a function type");5873 }5874 5875 Value *Callee;5876 unsigned CalleeTypeID;5877 if (getValueTypePair(Record, OpNum, NextValueNo, Callee, CalleeTypeID,5878 CurBB))5879 return error("Invalid record");5880 5881 PointerType *CalleeTy = dyn_cast<PointerType>(Callee->getType());5882 if (!CalleeTy)5883 return error("Callee is not a pointer");5884 if (!FTy) {5885 FTyID = getContainedTypeID(CalleeTypeID);5886 FTy = dyn_cast_or_null<FunctionType>(getTypeByID(FTyID));5887 if (!FTy)5888 return error("Callee is not of pointer to function type");5889 }5890 if (Record.size() < FTy->getNumParams() + OpNum)5891 return error("Insufficient operands to call");5892 5893 SmallVector<Value*, 16> Ops;5894 SmallVector<unsigned, 16> ArgTyIDs;5895 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i, ++OpNum) {5896 unsigned ArgTyID = getContainedTypeID(FTyID, i + 1);5897 Ops.push_back(getValue(Record, OpNum, NextValueNo, FTy->getParamType(i),5898 ArgTyID, CurBB));5899 ArgTyIDs.push_back(ArgTyID);5900 if (!Ops.back())5901 return error("Invalid record");5902 }5903 5904 if (!FTy->isVarArg()) {5905 if (Record.size() != OpNum)5906 return error("Invalid record");5907 } else {5908 // Read type/value pairs for varargs params.5909 while (OpNum != Record.size()) {5910 Value *Op;5911 unsigned OpTypeID;5912 if (getValueTypePair(Record, OpNum, NextValueNo, Op, OpTypeID, CurBB))5913 return error("Invalid record");5914 Ops.push_back(Op);5915 ArgTyIDs.push_back(OpTypeID);5916 }5917 }5918 5919 // Upgrade the bundles if needed.5920 if (!OperandBundles.empty())5921 UpgradeOperandBundles(OperandBundles);5922 5923 I = InvokeInst::Create(FTy, Callee, NormalBB, UnwindBB, Ops,5924 OperandBundles);5925 ResTypeID = getContainedTypeID(FTyID);5926 OperandBundles.clear();5927 InstructionList.push_back(I);5928 cast<InvokeInst>(I)->setCallingConv(5929 static_cast<CallingConv::ID>(CallingConv::MaxID & CCInfo));5930 cast<InvokeInst>(I)->setAttributes(PAL);5931 if (Error Err = propagateAttributeTypes(cast<CallBase>(I), ArgTyIDs)) {5932 I->deleteValue();5933 return Err;5934 }5935 5936 break;5937 }5938 case bitc::FUNC_CODE_INST_RESUME: { // RESUME: [opval]5939 unsigned Idx = 0;5940 Value *Val = nullptr;5941 unsigned ValTypeID;5942 if (getValueTypePair(Record, Idx, NextValueNo, Val, ValTypeID, CurBB))5943 return error("Invalid record");5944 I = ResumeInst::Create(Val);5945 InstructionList.push_back(I);5946 break;5947 }5948 case bitc::FUNC_CODE_INST_CALLBR: {5949 // CALLBR: [attr, cc, norm, transfs, fty, fnid, args]5950 unsigned OpNum = 0;5951 AttributeList PAL = getAttributes(Record[OpNum++]);5952 unsigned CCInfo = Record[OpNum++];5953 5954 BasicBlock *DefaultDest = getBasicBlock(Record[OpNum++]);5955 unsigned NumIndirectDests = Record[OpNum++];5956 SmallVector<BasicBlock *, 16> IndirectDests;5957 for (unsigned i = 0, e = NumIndirectDests; i != e; ++i)5958 IndirectDests.push_back(getBasicBlock(Record[OpNum++]));5959 5960 unsigned FTyID = InvalidTypeID;5961 FunctionType *FTy = nullptr;5962 if ((CCInfo >> bitc::CALL_EXPLICIT_TYPE) & 1) {5963 FTyID = Record[OpNum++];5964 FTy = dyn_cast_or_null<FunctionType>(getTypeByID(FTyID));5965 if (!FTy)5966 return error("Explicit call type is not a function type");5967 }5968 5969 Value *Callee;5970 unsigned CalleeTypeID;5971 if (getValueTypePair(Record, OpNum, NextValueNo, Callee, CalleeTypeID,5972 CurBB))5973 return error("Invalid record");5974 5975 PointerType *OpTy = dyn_cast<PointerType>(Callee->getType());5976 if (!OpTy)5977 return error("Callee is not a pointer type");5978 if (!FTy) {5979 FTyID = getContainedTypeID(CalleeTypeID);5980 FTy = dyn_cast_or_null<FunctionType>(getTypeByID(FTyID));5981 if (!FTy)5982 return error("Callee is not of pointer to function type");5983 }5984 if (Record.size() < FTy->getNumParams() + OpNum)5985 return error("Insufficient operands to call");5986 5987 SmallVector<Value*, 16> Args;5988 SmallVector<unsigned, 16> ArgTyIDs;5989 // Read the fixed params.5990 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i, ++OpNum) {5991 Value *Arg;5992 unsigned ArgTyID = getContainedTypeID(FTyID, i + 1);5993 if (FTy->getParamType(i)->isLabelTy())5994 Arg = getBasicBlock(Record[OpNum]);5995 else5996 Arg = getValue(Record, OpNum, NextValueNo, FTy->getParamType(i),5997 ArgTyID, CurBB);5998 if (!Arg)5999 return error("Invalid record");6000 Args.push_back(Arg);6001 ArgTyIDs.push_back(ArgTyID);6002 }6003 6004 // Read type/value pairs for varargs params.6005 if (!FTy->isVarArg()) {6006 if (OpNum != Record.size())6007 return error("Invalid record");6008 } else {6009 while (OpNum != Record.size()) {6010 Value *Op;6011 unsigned OpTypeID;6012 if (getValueTypePair(Record, OpNum, NextValueNo, Op, OpTypeID, CurBB))6013 return error("Invalid record");6014 Args.push_back(Op);6015 ArgTyIDs.push_back(OpTypeID);6016 }6017 }6018 6019 // Upgrade the bundles if needed.6020 if (!OperandBundles.empty())6021 UpgradeOperandBundles(OperandBundles);6022 6023 if (auto *IA = dyn_cast<InlineAsm>(Callee)) {6024 InlineAsm::ConstraintInfoVector ConstraintInfo = IA->ParseConstraints();6025 auto IsLabelConstraint = [](const InlineAsm::ConstraintInfo &CI) {6026 return CI.Type == InlineAsm::isLabel;6027 };6028 if (none_of(ConstraintInfo, IsLabelConstraint)) {6029 // Upgrade explicit blockaddress arguments to label constraints.6030 // Verify that the last arguments are blockaddress arguments that6031 // match the indirect destinations. Clang always generates callbr6032 // in this form. We could support reordering with more effort.6033 unsigned FirstBlockArg = Args.size() - IndirectDests.size();6034 for (unsigned ArgNo = FirstBlockArg; ArgNo < Args.size(); ++ArgNo) {6035 unsigned LabelNo = ArgNo - FirstBlockArg;6036 auto *BA = dyn_cast<BlockAddress>(Args[ArgNo]);6037 if (!BA || BA->getFunction() != F ||6038 LabelNo > IndirectDests.size() ||6039 BA->getBasicBlock() != IndirectDests[LabelNo])6040 return error("callbr argument does not match indirect dest");6041 }6042 6043 // Remove blockaddress arguments.6044 Args.erase(Args.begin() + FirstBlockArg, Args.end());6045 ArgTyIDs.erase(ArgTyIDs.begin() + FirstBlockArg, ArgTyIDs.end());6046 6047 // Recreate the function type with less arguments.6048 SmallVector<Type *> ArgTys;6049 for (Value *Arg : Args)6050 ArgTys.push_back(Arg->getType());6051 FTy =6052 FunctionType::get(FTy->getReturnType(), ArgTys, FTy->isVarArg());6053 6054 // Update constraint string to use label constraints.6055 std::string Constraints = IA->getConstraintString().str();6056 unsigned ArgNo = 0;6057 size_t Pos = 0;6058 for (const auto &CI : ConstraintInfo) {6059 if (CI.hasArg()) {6060 if (ArgNo >= FirstBlockArg)6061 Constraints.insert(Pos, "!");6062 ++ArgNo;6063 }6064 6065 // Go to next constraint in string.6066 Pos = Constraints.find(',', Pos);6067 if (Pos == std::string::npos)6068 break;6069 ++Pos;6070 }6071 6072 Callee = InlineAsm::get(FTy, IA->getAsmString(), Constraints,6073 IA->hasSideEffects(), IA->isAlignStack(),6074 IA->getDialect(), IA->canThrow());6075 }6076 }6077 6078 I = CallBrInst::Create(FTy, Callee, DefaultDest, IndirectDests, Args,6079 OperandBundles);6080 ResTypeID = getContainedTypeID(FTyID);6081 OperandBundles.clear();6082 InstructionList.push_back(I);6083 cast<CallBrInst>(I)->setCallingConv(6084 static_cast<CallingConv::ID>((0x7ff & CCInfo) >> bitc::CALL_CCONV));6085 cast<CallBrInst>(I)->setAttributes(PAL);6086 if (Error Err = propagateAttributeTypes(cast<CallBase>(I), ArgTyIDs)) {6087 I->deleteValue();6088 return Err;6089 }6090 break;6091 }6092 case bitc::FUNC_CODE_INST_UNREACHABLE: // UNREACHABLE6093 I = new UnreachableInst(Context);6094 InstructionList.push_back(I);6095 break;6096 case bitc::FUNC_CODE_INST_PHI: { // PHI: [ty, val0,bb0, ...]6097 if (Record.empty())6098 return error("Invalid phi record");6099 // The first record specifies the type.6100 unsigned TyID = Record[0];6101 Type *Ty = getTypeByID(TyID);6102 if (!Ty)6103 return error("Invalid phi record");6104 6105 // Phi arguments are pairs of records of [value, basic block].6106 // There is an optional final record for fast-math-flags if this phi has a6107 // floating-point type.6108 size_t NumArgs = (Record.size() - 1) / 2;6109 PHINode *PN = PHINode::Create(Ty, NumArgs);6110 if ((Record.size() - 1) % 2 == 1 && !isa<FPMathOperator>(PN)) {6111 PN->deleteValue();6112 return error("Invalid phi record");6113 }6114 InstructionList.push_back(PN);6115 6116 SmallDenseMap<BasicBlock *, Value *> Args;6117 for (unsigned i = 0; i != NumArgs; i++) {6118 BasicBlock *BB = getBasicBlock(Record[i * 2 + 2]);6119 if (!BB) {6120 PN->deleteValue();6121 return error("Invalid phi BB");6122 }6123 6124 // Phi nodes may contain the same predecessor multiple times, in which6125 // case the incoming value must be identical. Directly reuse the already6126 // seen value here, to avoid expanding a constant expression multiple6127 // times.6128 auto It = Args.find(BB);6129 BasicBlock *EdgeBB = ConstExprEdgeBBs.lookup({BB, CurBB});6130 if (It != Args.end()) {6131 // If this predecessor was also replaced with a constexpr basic6132 // block, it must be de-duplicated.6133 if (!EdgeBB) {6134 PN->addIncoming(It->second, BB);6135 }6136 continue;6137 }6138 6139 // If there already is a block for this edge (from a different phi),6140 // use it.6141 if (!EdgeBB) {6142 // Otherwise, use a temporary block (that we will discard if it6143 // turns out to be unnecessary).6144 if (!PhiConstExprBB)6145 PhiConstExprBB = BasicBlock::Create(Context, "phi.constexpr", F);6146 EdgeBB = PhiConstExprBB;6147 }6148 6149 // With the new function encoding, it is possible that operands have6150 // negative IDs (for forward references). Use a signed VBR6151 // representation to keep the encoding small.6152 Value *V;6153 if (UseRelativeIDs)6154 V = getValueSigned(Record, i * 2 + 1, NextValueNo, Ty, TyID, EdgeBB);6155 else6156 V = getValue(Record, i * 2 + 1, NextValueNo, Ty, TyID, EdgeBB);6157 if (!V) {6158 PN->deleteValue();6159 PhiConstExprBB->eraseFromParent();6160 return error("Invalid phi record");6161 }6162 6163 if (EdgeBB == PhiConstExprBB && !EdgeBB->empty()) {6164 ConstExprEdgeBBs.insert({{BB, CurBB}, EdgeBB});6165 PhiConstExprBB = nullptr;6166 }6167 PN->addIncoming(V, BB);6168 Args.insert({BB, V});6169 }6170 I = PN;6171 ResTypeID = TyID;6172 6173 // If there are an even number of records, the final record must be FMF.6174 if (Record.size() % 2 == 0) {6175 assert(isa<FPMathOperator>(I) && "Unexpected phi type");6176 FastMathFlags FMF = getDecodedFastMathFlags(Record[Record.size() - 1]);6177 if (FMF.any())6178 I->setFastMathFlags(FMF);6179 }6180 6181 break;6182 }6183 6184 case bitc::FUNC_CODE_INST_LANDINGPAD:6185 case bitc::FUNC_CODE_INST_LANDINGPAD_OLD: {6186 // LANDINGPAD: [ty, val, val, num, (id0,val0 ...)?]6187 unsigned Idx = 0;6188 if (BitCode == bitc::FUNC_CODE_INST_LANDINGPAD) {6189 if (Record.size() < 3)6190 return error("Invalid record");6191 } else {6192 assert(BitCode == bitc::FUNC_CODE_INST_LANDINGPAD_OLD);6193 if (Record.size() < 4)6194 return error("Invalid record");6195 }6196 ResTypeID = Record[Idx++];6197 Type *Ty = getTypeByID(ResTypeID);6198 if (!Ty)6199 return error("Invalid record");6200 if (BitCode == bitc::FUNC_CODE_INST_LANDINGPAD_OLD) {6201 Value *PersFn = nullptr;6202 unsigned PersFnTypeID;6203 if (getValueTypePair(Record, Idx, NextValueNo, PersFn, PersFnTypeID,6204 nullptr))6205 return error("Invalid record");6206 6207 if (!F->hasPersonalityFn())6208 F->setPersonalityFn(cast<Constant>(PersFn));6209 else if (F->getPersonalityFn() != cast<Constant>(PersFn))6210 return error("Personality function mismatch");6211 }6212 6213 bool IsCleanup = !!Record[Idx++];6214 unsigned NumClauses = Record[Idx++];6215 LandingPadInst *LP = LandingPadInst::Create(Ty, NumClauses);6216 LP->setCleanup(IsCleanup);6217 for (unsigned J = 0; J != NumClauses; ++J) {6218 LandingPadInst::ClauseType CT =6219 LandingPadInst::ClauseType(Record[Idx++]); (void)CT;6220 Value *Val;6221 unsigned ValTypeID;6222 6223 if (getValueTypePair(Record, Idx, NextValueNo, Val, ValTypeID,6224 nullptr)) {6225 delete LP;6226 return error("Invalid record");6227 }6228 6229 assert((CT != LandingPadInst::Catch ||6230 !isa<ArrayType>(Val->getType())) &&6231 "Catch clause has a invalid type!");6232 assert((CT != LandingPadInst::Filter ||6233 isa<ArrayType>(Val->getType())) &&6234 "Filter clause has invalid type!");6235 LP->addClause(cast<Constant>(Val));6236 }6237 6238 I = LP;6239 InstructionList.push_back(I);6240 break;6241 }6242 6243 case bitc::FUNC_CODE_INST_ALLOCA: { // ALLOCA: [instty, opty, op, align]6244 if (Record.size() != 4 && Record.size() != 5)6245 return error("Invalid record");6246 using APV = AllocaPackedValues;6247 const uint64_t Rec = Record[3];6248 const bool InAlloca = Bitfield::get<APV::UsedWithInAlloca>(Rec);6249 const bool SwiftError = Bitfield::get<APV::SwiftError>(Rec);6250 unsigned TyID = Record[0];6251 Type *Ty = getTypeByID(TyID);6252 if (!Bitfield::get<APV::ExplicitType>(Rec)) {6253 TyID = getContainedTypeID(TyID);6254 Ty = getTypeByID(TyID);6255 if (!Ty)6256 return error("Missing element type for old-style alloca");6257 }6258 unsigned OpTyID = Record[1];6259 Type *OpTy = getTypeByID(OpTyID);6260 Value *Size = getFnValueByID(Record[2], OpTy, OpTyID, CurBB);6261 MaybeAlign Align;6262 uint64_t AlignExp =6263 Bitfield::get<APV::AlignLower>(Rec) |6264 (Bitfield::get<APV::AlignUpper>(Rec) << APV::AlignLower::Bits);6265 if (Error Err = parseAlignmentValue(AlignExp, Align)) {6266 return Err;6267 }6268 if (!Ty || !Size)6269 return error("Invalid record");6270 6271 const DataLayout &DL = TheModule->getDataLayout();6272 unsigned AS = Record.size() == 5 ? Record[4] : DL.getAllocaAddrSpace();6273 6274 SmallPtrSet<Type *, 4> Visited;6275 if (!Align && !Ty->isSized(&Visited))6276 return error("alloca of unsized type");6277 if (!Align)6278 Align = DL.getPrefTypeAlign(Ty);6279 6280 if (!Size->getType()->isIntegerTy())6281 return error("alloca element count must have integer type");6282 6283 AllocaInst *AI = new AllocaInst(Ty, AS, Size, *Align);6284 AI->setUsedWithInAlloca(InAlloca);6285 AI->setSwiftError(SwiftError);6286 I = AI;6287 ResTypeID = getVirtualTypeID(AI->getType(), TyID);6288 InstructionList.push_back(I);6289 break;6290 }6291 case bitc::FUNC_CODE_INST_LOAD: { // LOAD: [opty, op, align, vol]6292 unsigned OpNum = 0;6293 Value *Op;6294 unsigned OpTypeID;6295 if (getValueTypePair(Record, OpNum, NextValueNo, Op, OpTypeID, CurBB) ||6296 (OpNum + 2 != Record.size() && OpNum + 3 != Record.size()))6297 return error("Invalid record");6298 6299 if (!isa<PointerType>(Op->getType()))6300 return error("Load operand is not a pointer type");6301 6302 Type *Ty = nullptr;6303 if (OpNum + 3 == Record.size()) {6304 ResTypeID = Record[OpNum++];6305 Ty = getTypeByID(ResTypeID);6306 } else {6307 ResTypeID = getContainedTypeID(OpTypeID);6308 Ty = getTypeByID(ResTypeID);6309 }6310 6311 if (!Ty)6312 return error("Missing load type");6313 6314 if (Error Err = typeCheckLoadStoreInst(Ty, Op->getType()))6315 return Err;6316 6317 MaybeAlign Align;6318 if (Error Err = parseAlignmentValue(Record[OpNum], Align))6319 return Err;6320 SmallPtrSet<Type *, 4> Visited;6321 if (!Align && !Ty->isSized(&Visited))6322 return error("load of unsized type");6323 if (!Align)6324 Align = TheModule->getDataLayout().getABITypeAlign(Ty);6325 I = new LoadInst(Ty, Op, "", Record[OpNum + 1], *Align);6326 InstructionList.push_back(I);6327 break;6328 }6329 case bitc::FUNC_CODE_INST_LOADATOMIC: {6330 // LOADATOMIC: [opty, op, align, vol, ordering, ssid]6331 unsigned OpNum = 0;6332 Value *Op;6333 unsigned OpTypeID;6334 if (getValueTypePair(Record, OpNum, NextValueNo, Op, OpTypeID, CurBB) ||6335 (OpNum + 4 != Record.size() && OpNum + 5 != Record.size()))6336 return error("Invalid record");6337 6338 if (!isa<PointerType>(Op->getType()))6339 return error("Load operand is not a pointer type");6340 6341 Type *Ty = nullptr;6342 if (OpNum + 5 == Record.size()) {6343 ResTypeID = Record[OpNum++];6344 Ty = getTypeByID(ResTypeID);6345 } else {6346 ResTypeID = getContainedTypeID(OpTypeID);6347 Ty = getTypeByID(ResTypeID);6348 }6349 6350 if (!Ty)6351 return error("Missing atomic load type");6352 6353 if (Error Err = typeCheckLoadStoreInst(Ty, Op->getType()))6354 return Err;6355 6356 AtomicOrdering Ordering = getDecodedOrdering(Record[OpNum + 2]);6357 if (Ordering == AtomicOrdering::NotAtomic ||6358 Ordering == AtomicOrdering::Release ||6359 Ordering == AtomicOrdering::AcquireRelease)6360 return error("Invalid record");6361 if (Ordering != AtomicOrdering::NotAtomic && Record[OpNum] == 0)6362 return error("Invalid record");6363 SyncScope::ID SSID = getDecodedSyncScopeID(Record[OpNum + 3]);6364 6365 MaybeAlign Align;6366 if (Error Err = parseAlignmentValue(Record[OpNum], Align))6367 return Err;6368 if (!Align)6369 return error("Alignment missing from atomic load");6370 I = new LoadInst(Ty, Op, "", Record[OpNum + 1], *Align, Ordering, SSID);6371 InstructionList.push_back(I);6372 break;6373 }6374 case bitc::FUNC_CODE_INST_STORE:6375 case bitc::FUNC_CODE_INST_STORE_OLD: { // STORE2:[ptrty, ptr, val, align, vol]6376 unsigned OpNum = 0;6377 Value *Val, *Ptr;6378 unsigned PtrTypeID, ValTypeID;6379 if (getValueTypePair(Record, OpNum, NextValueNo, Ptr, PtrTypeID, CurBB))6380 return error("Invalid record");6381 6382 if (BitCode == bitc::FUNC_CODE_INST_STORE) {6383 if (getValueTypePair(Record, OpNum, NextValueNo, Val, ValTypeID, CurBB))6384 return error("Invalid record");6385 } else {6386 ValTypeID = getContainedTypeID(PtrTypeID);6387 if (popValue(Record, OpNum, NextValueNo, getTypeByID(ValTypeID),6388 ValTypeID, Val, CurBB))6389 return error("Invalid record");6390 }6391 6392 if (OpNum + 2 != Record.size())6393 return error("Invalid record");6394 6395 if (Error Err = typeCheckLoadStoreInst(Val->getType(), Ptr->getType()))6396 return Err;6397 MaybeAlign Align;6398 if (Error Err = parseAlignmentValue(Record[OpNum], Align))6399 return Err;6400 SmallPtrSet<Type *, 4> Visited;6401 if (!Align && !Val->getType()->isSized(&Visited))6402 return error("store of unsized type");6403 if (!Align)6404 Align = TheModule->getDataLayout().getABITypeAlign(Val->getType());6405 I = new StoreInst(Val, Ptr, Record[OpNum + 1], *Align);6406 InstructionList.push_back(I);6407 break;6408 }6409 case bitc::FUNC_CODE_INST_STOREATOMIC:6410 case bitc::FUNC_CODE_INST_STOREATOMIC_OLD: {6411 // STOREATOMIC: [ptrty, ptr, val, align, vol, ordering, ssid]6412 unsigned OpNum = 0;6413 Value *Val, *Ptr;6414 unsigned PtrTypeID, ValTypeID;6415 if (getValueTypePair(Record, OpNum, NextValueNo, Ptr, PtrTypeID, CurBB) ||6416 !isa<PointerType>(Ptr->getType()))6417 return error("Invalid record");6418 if (BitCode == bitc::FUNC_CODE_INST_STOREATOMIC) {6419 if (getValueTypePair(Record, OpNum, NextValueNo, Val, ValTypeID, CurBB))6420 return error("Invalid record");6421 } else {6422 ValTypeID = getContainedTypeID(PtrTypeID);6423 if (popValue(Record, OpNum, NextValueNo, getTypeByID(ValTypeID),6424 ValTypeID, Val, CurBB))6425 return error("Invalid record");6426 }6427 6428 if (OpNum + 4 != Record.size())6429 return error("Invalid record");6430 6431 if (Error Err = typeCheckLoadStoreInst(Val->getType(), Ptr->getType()))6432 return Err;6433 AtomicOrdering Ordering = getDecodedOrdering(Record[OpNum + 2]);6434 if (Ordering == AtomicOrdering::NotAtomic ||6435 Ordering == AtomicOrdering::Acquire ||6436 Ordering == AtomicOrdering::AcquireRelease)6437 return error("Invalid record");6438 SyncScope::ID SSID = getDecodedSyncScopeID(Record[OpNum + 3]);6439 if (Ordering != AtomicOrdering::NotAtomic && Record[OpNum] == 0)6440 return error("Invalid record");6441 6442 MaybeAlign Align;6443 if (Error Err = parseAlignmentValue(Record[OpNum], Align))6444 return Err;6445 if (!Align)6446 return error("Alignment missing from atomic store");6447 I = new StoreInst(Val, Ptr, Record[OpNum + 1], *Align, Ordering, SSID);6448 InstructionList.push_back(I);6449 break;6450 }6451 case bitc::FUNC_CODE_INST_CMPXCHG_OLD: {6452 // CMPXCHG_OLD: [ptrty, ptr, cmp, val, vol, ordering, syncscope,6453 // failure_ordering?, weak?]6454 const size_t NumRecords = Record.size();6455 unsigned OpNum = 0;6456 Value *Ptr = nullptr;6457 unsigned PtrTypeID;6458 if (getValueTypePair(Record, OpNum, NextValueNo, Ptr, PtrTypeID, CurBB))6459 return error("Invalid record");6460 6461 if (!isa<PointerType>(Ptr->getType()))6462 return error("Cmpxchg operand is not a pointer type");6463 6464 Value *Cmp = nullptr;6465 unsigned CmpTypeID = getContainedTypeID(PtrTypeID);6466 if (popValue(Record, OpNum, NextValueNo, getTypeByID(CmpTypeID),6467 CmpTypeID, Cmp, CurBB))6468 return error("Invalid record");6469 6470 Value *New = nullptr;6471 if (popValue(Record, OpNum, NextValueNo, Cmp->getType(), CmpTypeID,6472 New, CurBB) ||6473 NumRecords < OpNum + 3 || NumRecords > OpNum + 5)6474 return error("Invalid record");6475 6476 const AtomicOrdering SuccessOrdering =6477 getDecodedOrdering(Record[OpNum + 1]);6478 if (SuccessOrdering == AtomicOrdering::NotAtomic ||6479 SuccessOrdering == AtomicOrdering::Unordered)6480 return error("Invalid record");6481 6482 const SyncScope::ID SSID = getDecodedSyncScopeID(Record[OpNum + 2]);6483 6484 if (Error Err = typeCheckLoadStoreInst(Cmp->getType(), Ptr->getType()))6485 return Err;6486 6487 const AtomicOrdering FailureOrdering =6488 NumRecords < 76489 ? AtomicCmpXchgInst::getStrongestFailureOrdering(SuccessOrdering)6490 : getDecodedOrdering(Record[OpNum + 3]);6491 6492 if (FailureOrdering == AtomicOrdering::NotAtomic ||6493 FailureOrdering == AtomicOrdering::Unordered)6494 return error("Invalid record");6495 6496 const Align Alignment(6497 TheModule->getDataLayout().getTypeStoreSize(Cmp->getType()));6498 6499 I = new AtomicCmpXchgInst(Ptr, Cmp, New, Alignment, SuccessOrdering,6500 FailureOrdering, SSID);6501 cast<AtomicCmpXchgInst>(I)->setVolatile(Record[OpNum]);6502 6503 if (NumRecords < 8) {6504 // Before weak cmpxchgs existed, the instruction simply returned the6505 // value loaded from memory, so bitcode files from that era will be6506 // expecting the first component of a modern cmpxchg.6507 I->insertInto(CurBB, CurBB->end());6508 I = ExtractValueInst::Create(I, 0);6509 ResTypeID = CmpTypeID;6510 } else {6511 cast<AtomicCmpXchgInst>(I)->setWeak(Record[OpNum + 4]);6512 unsigned I1TypeID = getVirtualTypeID(Type::getInt1Ty(Context));6513 ResTypeID = getVirtualTypeID(I->getType(), {CmpTypeID, I1TypeID});6514 }6515 6516 InstructionList.push_back(I);6517 break;6518 }6519 case bitc::FUNC_CODE_INST_CMPXCHG: {6520 // CMPXCHG: [ptrty, ptr, cmp, val, vol, success_ordering, syncscope,6521 // failure_ordering, weak, align?]6522 const size_t NumRecords = Record.size();6523 unsigned OpNum = 0;6524 Value *Ptr = nullptr;6525 unsigned PtrTypeID;6526 if (getValueTypePair(Record, OpNum, NextValueNo, Ptr, PtrTypeID, CurBB))6527 return error("Invalid record");6528 6529 if (!isa<PointerType>(Ptr->getType()))6530 return error("Cmpxchg operand is not a pointer type");6531 6532 Value *Cmp = nullptr;6533 unsigned CmpTypeID;6534 if (getValueTypePair(Record, OpNum, NextValueNo, Cmp, CmpTypeID, CurBB))6535 return error("Invalid record");6536 6537 Value *Val = nullptr;6538 if (popValue(Record, OpNum, NextValueNo, Cmp->getType(), CmpTypeID, Val,6539 CurBB))6540 return error("Invalid record");6541 6542 if (NumRecords < OpNum + 3 || NumRecords > OpNum + 6)6543 return error("Invalid record");6544 6545 const bool IsVol = Record[OpNum];6546 6547 const AtomicOrdering SuccessOrdering =6548 getDecodedOrdering(Record[OpNum + 1]);6549 if (!AtomicCmpXchgInst::isValidSuccessOrdering(SuccessOrdering))6550 return error("Invalid cmpxchg success ordering");6551 6552 const SyncScope::ID SSID = getDecodedSyncScopeID(Record[OpNum + 2]);6553 6554 if (Error Err = typeCheckLoadStoreInst(Cmp->getType(), Ptr->getType()))6555 return Err;6556 6557 const AtomicOrdering FailureOrdering =6558 getDecodedOrdering(Record[OpNum + 3]);6559 if (!AtomicCmpXchgInst::isValidFailureOrdering(FailureOrdering))6560 return error("Invalid cmpxchg failure ordering");6561 6562 const bool IsWeak = Record[OpNum + 4];6563 6564 MaybeAlign Alignment;6565 6566 if (NumRecords == (OpNum + 6)) {6567 if (Error Err = parseAlignmentValue(Record[OpNum + 5], Alignment))6568 return Err;6569 }6570 if (!Alignment)6571 Alignment =6572 Align(TheModule->getDataLayout().getTypeStoreSize(Cmp->getType()));6573 6574 I = new AtomicCmpXchgInst(Ptr, Cmp, Val, *Alignment, SuccessOrdering,6575 FailureOrdering, SSID);6576 cast<AtomicCmpXchgInst>(I)->setVolatile(IsVol);6577 cast<AtomicCmpXchgInst>(I)->setWeak(IsWeak);6578 6579 unsigned I1TypeID = getVirtualTypeID(Type::getInt1Ty(Context));6580 ResTypeID = getVirtualTypeID(I->getType(), {CmpTypeID, I1TypeID});6581 6582 InstructionList.push_back(I);6583 break;6584 }6585 case bitc::FUNC_CODE_INST_ATOMICRMW_OLD:6586 case bitc::FUNC_CODE_INST_ATOMICRMW: {6587 // ATOMICRMW_OLD: [ptrty, ptr, val, op, vol, ordering, ssid, align?]6588 // ATOMICRMW: [ptrty, ptr, valty, val, op, vol, ordering, ssid, align?]6589 const size_t NumRecords = Record.size();6590 unsigned OpNum = 0;6591 6592 Value *Ptr = nullptr;6593 unsigned PtrTypeID;6594 if (getValueTypePair(Record, OpNum, NextValueNo, Ptr, PtrTypeID, CurBB))6595 return error("Invalid record");6596 6597 if (!isa<PointerType>(Ptr->getType()))6598 return error("Invalid record");6599 6600 Value *Val = nullptr;6601 unsigned ValTypeID = InvalidTypeID;6602 if (BitCode == bitc::FUNC_CODE_INST_ATOMICRMW_OLD) {6603 ValTypeID = getContainedTypeID(PtrTypeID);6604 if (popValue(Record, OpNum, NextValueNo,6605 getTypeByID(ValTypeID), ValTypeID, Val, CurBB))6606 return error("Invalid record");6607 } else {6608 if (getValueTypePair(Record, OpNum, NextValueNo, Val, ValTypeID, CurBB))6609 return error("Invalid record");6610 }6611 6612 if (!(NumRecords == (OpNum + 4) || NumRecords == (OpNum + 5)))6613 return error("Invalid record");6614 6615 const AtomicRMWInst::BinOp Operation =6616 getDecodedRMWOperation(Record[OpNum]);6617 if (Operation < AtomicRMWInst::FIRST_BINOP ||6618 Operation > AtomicRMWInst::LAST_BINOP)6619 return error("Invalid record");6620 6621 const bool IsVol = Record[OpNum + 1];6622 6623 const AtomicOrdering Ordering = getDecodedOrdering(Record[OpNum + 2]);6624 if (Ordering == AtomicOrdering::NotAtomic ||6625 Ordering == AtomicOrdering::Unordered)6626 return error("Invalid record");6627 6628 const SyncScope::ID SSID = getDecodedSyncScopeID(Record[OpNum + 3]);6629 6630 MaybeAlign Alignment;6631 6632 if (NumRecords == (OpNum + 5)) {6633 if (Error Err = parseAlignmentValue(Record[OpNum + 4], Alignment))6634 return Err;6635 }6636 6637 if (!Alignment)6638 Alignment =6639 Align(TheModule->getDataLayout().getTypeStoreSize(Val->getType()));6640 6641 I = new AtomicRMWInst(Operation, Ptr, Val, *Alignment, Ordering, SSID);6642 ResTypeID = ValTypeID;6643 cast<AtomicRMWInst>(I)->setVolatile(IsVol);6644 6645 InstructionList.push_back(I);6646 break;6647 }6648 case bitc::FUNC_CODE_INST_FENCE: { // FENCE:[ordering, ssid]6649 if (2 != Record.size())6650 return error("Invalid record");6651 AtomicOrdering Ordering = getDecodedOrdering(Record[0]);6652 if (Ordering == AtomicOrdering::NotAtomic ||6653 Ordering == AtomicOrdering::Unordered ||6654 Ordering == AtomicOrdering::Monotonic)6655 return error("Invalid record");6656 SyncScope::ID SSID = getDecodedSyncScopeID(Record[1]);6657 I = new FenceInst(Context, Ordering, SSID);6658 InstructionList.push_back(I);6659 break;6660 }6661 case bitc::FUNC_CODE_DEBUG_RECORD_LABEL: {6662 // DbgLabelRecords are placed after the Instructions that they are6663 // attached to.6664 SeenDebugRecord = true;6665 Instruction *Inst = getLastInstruction();6666 if (!Inst)6667 return error("Invalid dbg record: missing instruction");6668 DILocation *DIL = cast<DILocation>(getFnMetadataByID(Record[0]));6669 DILabel *Label = cast<DILabel>(getFnMetadataByID(Record[1]));6670 Inst->getParent()->insertDbgRecordBefore(6671 new DbgLabelRecord(Label, DebugLoc(DIL)), Inst->getIterator());6672 continue; // This isn't an instruction.6673 }6674 case bitc::FUNC_CODE_DEBUG_RECORD_VALUE_SIMPLE:6675 case bitc::FUNC_CODE_DEBUG_RECORD_VALUE:6676 case bitc::FUNC_CODE_DEBUG_RECORD_DECLARE:6677 case bitc::FUNC_CODE_DEBUG_RECORD_DECLARE_VALUE:6678 case bitc::FUNC_CODE_DEBUG_RECORD_ASSIGN: {6679 // DbgVariableRecords are placed after the Instructions that they are6680 // attached to.6681 SeenDebugRecord = true;6682 Instruction *Inst = getLastInstruction();6683 if (!Inst)6684 return error("Invalid dbg record: missing instruction");6685 6686 // First 3 fields are common to all kinds:6687 // DILocation, DILocalVariable, DIExpression6688 // dbg_value (FUNC_CODE_DEBUG_RECORD_VALUE)6689 // ..., LocationMetadata6690 // dbg_value (FUNC_CODE_DEBUG_RECORD_VALUE_SIMPLE - abbrev'd)6691 // ..., Value6692 // dbg_declare (FUNC_CODE_DEBUG_RECORD_DECLARE)6693 // ..., LocationMetadata6694 // dbg_declare_value (FUNC_CODE_DEBUG_RECORD_DECLARE_VALUE)6695 // ..., LocationMetadata6696 // dbg_assign (FUNC_CODE_DEBUG_RECORD_ASSIGN)6697 // ..., LocationMetadata, DIAssignID, DIExpression, LocationMetadata6698 unsigned Slot = 0;6699 // Common fields (0-2).6700 DILocation *DIL = cast<DILocation>(getFnMetadataByID(Record[Slot++]));6701 DILocalVariable *Var =6702 cast<DILocalVariable>(getFnMetadataByID(Record[Slot++]));6703 DIExpression *Expr =6704 cast<DIExpression>(getFnMetadataByID(Record[Slot++]));6705 6706 // Union field (3: LocationMetadata | Value).6707 Metadata *RawLocation = nullptr;6708 if (BitCode == bitc::FUNC_CODE_DEBUG_RECORD_VALUE_SIMPLE) {6709 Value *V = nullptr;6710 unsigned TyID = 0;6711 // We never expect to see a fwd reference value here because6712 // use-before-defs are encoded with the standard non-abbrev record6713 // type (they'd require encoding the type too, and they're rare). As a6714 // result, getValueTypePair only ever increments Slot by one here (once6715 // for the value, never twice for value and type).6716 unsigned SlotBefore = Slot;6717 if (getValueTypePair(Record, Slot, NextValueNo, V, TyID, CurBB))6718 return error("Invalid dbg record: invalid value");6719 (void)SlotBefore;6720 assert((SlotBefore == Slot - 1) && "unexpected fwd ref");6721 RawLocation = ValueAsMetadata::get(V);6722 } else {6723 RawLocation = getFnMetadataByID(Record[Slot++]);6724 }6725 6726 DbgVariableRecord *DVR = nullptr;6727 switch (BitCode) {6728 case bitc::FUNC_CODE_DEBUG_RECORD_VALUE:6729 case bitc::FUNC_CODE_DEBUG_RECORD_VALUE_SIMPLE:6730 DVR = new DbgVariableRecord(RawLocation, Var, Expr, DIL,6731 DbgVariableRecord::LocationType::Value);6732 break;6733 case bitc::FUNC_CODE_DEBUG_RECORD_DECLARE:6734 DVR = new DbgVariableRecord(RawLocation, Var, Expr, DIL,6735 DbgVariableRecord::LocationType::Declare);6736 break;6737 case bitc::FUNC_CODE_DEBUG_RECORD_DECLARE_VALUE:6738 DVR = new DbgVariableRecord(6739 RawLocation, Var, Expr, DIL,6740 DbgVariableRecord::LocationType::DeclareValue);6741 break;6742 case bitc::FUNC_CODE_DEBUG_RECORD_ASSIGN: {6743 DIAssignID *ID = cast<DIAssignID>(getFnMetadataByID(Record[Slot++]));6744 DIExpression *AddrExpr =6745 cast<DIExpression>(getFnMetadataByID(Record[Slot++]));6746 Metadata *Addr = getFnMetadataByID(Record[Slot++]);6747 DVR = new DbgVariableRecord(RawLocation, Var, Expr, ID, Addr, AddrExpr,6748 DIL);6749 break;6750 }6751 default:6752 llvm_unreachable("Unknown DbgVariableRecord bitcode");6753 }6754 Inst->getParent()->insertDbgRecordBefore(DVR, Inst->getIterator());6755 continue; // This isn't an instruction.6756 }6757 case bitc::FUNC_CODE_INST_CALL: {6758 // CALL: [paramattrs, cc, fmf, fnty, fnid, arg0, arg1...]6759 if (Record.size() < 3)6760 return error("Invalid record");6761 6762 unsigned OpNum = 0;6763 AttributeList PAL = getAttributes(Record[OpNum++]);6764 unsigned CCInfo = Record[OpNum++];6765 6766 FastMathFlags FMF;6767 if ((CCInfo >> bitc::CALL_FMF) & 1) {6768 FMF = getDecodedFastMathFlags(Record[OpNum++]);6769 if (!FMF.any())6770 return error("Fast math flags indicator set for call with no FMF");6771 }6772 6773 unsigned FTyID = InvalidTypeID;6774 FunctionType *FTy = nullptr;6775 if ((CCInfo >> bitc::CALL_EXPLICIT_TYPE) & 1) {6776 FTyID = Record[OpNum++];6777 FTy = dyn_cast_or_null<FunctionType>(getTypeByID(FTyID));6778 if (!FTy)6779 return error("Explicit call type is not a function type");6780 }6781 6782 Value *Callee;6783 unsigned CalleeTypeID;6784 if (getValueTypePair(Record, OpNum, NextValueNo, Callee, CalleeTypeID,6785 CurBB))6786 return error("Invalid record");6787 6788 PointerType *OpTy = dyn_cast<PointerType>(Callee->getType());6789 if (!OpTy)6790 return error("Callee is not a pointer type");6791 if (!FTy) {6792 FTyID = getContainedTypeID(CalleeTypeID);6793 FTy = dyn_cast_or_null<FunctionType>(getTypeByID(FTyID));6794 if (!FTy)6795 return error("Callee is not of pointer to function type");6796 }6797 if (Record.size() < FTy->getNumParams() + OpNum)6798 return error("Insufficient operands to call");6799 6800 SmallVector<Value*, 16> Args;6801 SmallVector<unsigned, 16> ArgTyIDs;6802 // Read the fixed params.6803 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i, ++OpNum) {6804 unsigned ArgTyID = getContainedTypeID(FTyID, i + 1);6805 if (FTy->getParamType(i)->isLabelTy())6806 Args.push_back(getBasicBlock(Record[OpNum]));6807 else6808 Args.push_back(getValue(Record, OpNum, NextValueNo,6809 FTy->getParamType(i), ArgTyID, CurBB));6810 ArgTyIDs.push_back(ArgTyID);6811 if (!Args.back())6812 return error("Invalid record");6813 }6814 6815 // Read type/value pairs for varargs params.6816 if (!FTy->isVarArg()) {6817 if (OpNum != Record.size())6818 return error("Invalid record");6819 } else {6820 while (OpNum != Record.size()) {6821 Value *Op;6822 unsigned OpTypeID;6823 if (getValueTypePair(Record, OpNum, NextValueNo, Op, OpTypeID, CurBB))6824 return error("Invalid record");6825 Args.push_back(Op);6826 ArgTyIDs.push_back(OpTypeID);6827 }6828 }6829 6830 // Upgrade the bundles if needed.6831 if (!OperandBundles.empty())6832 UpgradeOperandBundles(OperandBundles);6833 6834 I = CallInst::Create(FTy, Callee, Args, OperandBundles);6835 ResTypeID = getContainedTypeID(FTyID);6836 OperandBundles.clear();6837 InstructionList.push_back(I);6838 cast<CallInst>(I)->setCallingConv(6839 static_cast<CallingConv::ID>((0x7ff & CCInfo) >> bitc::CALL_CCONV));6840 CallInst::TailCallKind TCK = CallInst::TCK_None;6841 if (CCInfo & (1 << bitc::CALL_TAIL))6842 TCK = CallInst::TCK_Tail;6843 if (CCInfo & (1 << bitc::CALL_MUSTTAIL))6844 TCK = CallInst::TCK_MustTail;6845 if (CCInfo & (1 << bitc::CALL_NOTAIL))6846 TCK = CallInst::TCK_NoTail;6847 cast<CallInst>(I)->setTailCallKind(TCK);6848 cast<CallInst>(I)->setAttributes(PAL);6849 if (isa<DbgInfoIntrinsic>(I))6850 SeenDebugIntrinsic = true;6851 if (Error Err = propagateAttributeTypes(cast<CallBase>(I), ArgTyIDs)) {6852 I->deleteValue();6853 return Err;6854 }6855 if (FMF.any()) {6856 if (!isa<FPMathOperator>(I))6857 return error("Fast-math-flags specified for call without "6858 "floating-point scalar or vector return type");6859 I->setFastMathFlags(FMF);6860 }6861 break;6862 }6863 case bitc::FUNC_CODE_INST_VAARG: { // VAARG: [valistty, valist, instty]6864 if (Record.size() < 3)6865 return error("Invalid record");6866 unsigned OpTyID = Record[0];6867 Type *OpTy = getTypeByID(OpTyID);6868 Value *Op = getValue(Record, 1, NextValueNo, OpTy, OpTyID, CurBB);6869 ResTypeID = Record[2];6870 Type *ResTy = getTypeByID(ResTypeID);6871 if (!OpTy || !Op || !ResTy)6872 return error("Invalid record");6873 I = new VAArgInst(Op, ResTy);6874 InstructionList.push_back(I);6875 break;6876 }6877 6878 case bitc::FUNC_CODE_OPERAND_BUNDLE: {6879 // A call or an invoke can be optionally prefixed with some variable6880 // number of operand bundle blocks. These blocks are read into6881 // OperandBundles and consumed at the next call or invoke instruction.6882 6883 if (Record.empty() || Record[0] >= BundleTags.size())6884 return error("Invalid record");6885 6886 std::vector<Value *> Inputs;6887 6888 unsigned OpNum = 1;6889 while (OpNum != Record.size()) {6890 Value *Op;6891 if (getValueOrMetadata(Record, OpNum, NextValueNo, Op, CurBB))6892 return error("Invalid record");6893 Inputs.push_back(Op);6894 }6895 6896 OperandBundles.emplace_back(BundleTags[Record[0]], std::move(Inputs));6897 continue;6898 }6899 6900 case bitc::FUNC_CODE_INST_FREEZE: { // FREEZE: [opty,opval]6901 unsigned OpNum = 0;6902 Value *Op = nullptr;6903 unsigned OpTypeID;6904 if (getValueTypePair(Record, OpNum, NextValueNo, Op, OpTypeID, CurBB))6905 return error("Invalid record");6906 if (OpNum != Record.size())6907 return error("Invalid record");6908 6909 I = new FreezeInst(Op);6910 ResTypeID = OpTypeID;6911 InstructionList.push_back(I);6912 break;6913 }6914 }6915 6916 // Add instruction to end of current BB. If there is no current BB, reject6917 // this file.6918 if (!CurBB) {6919 I->deleteValue();6920 return error("Invalid instruction with no BB");6921 }6922 if (!OperandBundles.empty()) {6923 I->deleteValue();6924 return error("Operand bundles found with no consumer");6925 }6926 I->insertInto(CurBB, CurBB->end());6927 6928 // If this was a terminator instruction, move to the next block.6929 if (I->isTerminator()) {6930 ++CurBBNo;6931 CurBB = CurBBNo < FunctionBBs.size() ? FunctionBBs[CurBBNo] : nullptr;6932 }6933 6934 // Non-void values get registered in the value table for future use.6935 if (!I->getType()->isVoidTy()) {6936 assert(I->getType() == getTypeByID(ResTypeID) &&6937 "Incorrect result type ID");6938 if (Error Err = ValueList.assignValue(NextValueNo++, I, ResTypeID))6939 return Err;6940 }6941 }6942 6943OutOfRecordLoop:6944 6945 if (!OperandBundles.empty())6946 return error("Operand bundles found with no consumer");6947 6948 // Check the function list for unresolved values.6949 if (Argument *A = dyn_cast<Argument>(ValueList.back())) {6950 if (!A->getParent()) {6951 // We found at least one unresolved value. Nuke them all to avoid leaks.6952 for (unsigned i = ModuleValueListSize, e = ValueList.size(); i != e; ++i){6953 if ((A = dyn_cast_or_null<Argument>(ValueList[i])) && !A->getParent()) {6954 A->replaceAllUsesWith(PoisonValue::get(A->getType()));6955 delete A;6956 }6957 }6958 return error("Never resolved value found in function");6959 }6960 }6961 6962 // Unexpected unresolved metadata about to be dropped.6963 if (MDLoader->hasFwdRefs())6964 return error("Invalid function metadata: outgoing forward refs");6965 6966 if (PhiConstExprBB)6967 PhiConstExprBB->eraseFromParent();6968 6969 for (const auto &Pair : ConstExprEdgeBBs) {6970 BasicBlock *From = Pair.first.first;6971 BasicBlock *To = Pair.first.second;6972 BasicBlock *EdgeBB = Pair.second;6973 BranchInst::Create(To, EdgeBB);6974 From->getTerminator()->replaceSuccessorWith(To, EdgeBB);6975 To->replacePhiUsesWith(From, EdgeBB);6976 EdgeBB->moveBefore(To);6977 }6978 6979 // Trim the value list down to the size it was before we parsed this function.6980 ValueList.shrinkTo(ModuleValueListSize);6981 MDLoader->shrinkTo(ModuleMDLoaderSize);6982 std::vector<BasicBlock*>().swap(FunctionBBs);6983 return Error::success();6984}6985 6986/// Find the function body in the bitcode stream6987Error BitcodeReader::findFunctionInStream(6988 Function *F,6989 DenseMap<Function *, uint64_t>::iterator DeferredFunctionInfoIterator) {6990 while (DeferredFunctionInfoIterator->second == 0) {6991 // This is the fallback handling for the old format bitcode that6992 // didn't contain the function index in the VST, or when we have6993 // an anonymous function which would not have a VST entry.6994 // Assert that we have one of those two cases.6995 assert(VSTOffset == 0 || !F->hasName());6996 // Parse the next body in the stream and set its position in the6997 // DeferredFunctionInfo map.6998 if (Error Err = rememberAndSkipFunctionBodies())6999 return Err;7000 }7001 return Error::success();7002}7003 7004SyncScope::ID BitcodeReader::getDecodedSyncScopeID(unsigned Val) {7005 if (Val == SyncScope::SingleThread || Val == SyncScope::System)7006 return SyncScope::ID(Val);7007 if (Val >= SSIDs.size())7008 return SyncScope::System; // Map unknown synchronization scopes to system.7009 return SSIDs[Val];7010}7011 7012//===----------------------------------------------------------------------===//7013// GVMaterializer implementation7014//===----------------------------------------------------------------------===//7015 7016Error BitcodeReader::materialize(GlobalValue *GV) {7017 Function *F = dyn_cast<Function>(GV);7018 // If it's not a function or is already material, ignore the request.7019 if (!F || !F->isMaterializable())7020 return Error::success();7021 7022 DenseMap<Function*, uint64_t>::iterator DFII = DeferredFunctionInfo.find(F);7023 assert(DFII != DeferredFunctionInfo.end() && "Deferred function not found!");7024 // If its position is recorded as 0, its body is somewhere in the stream7025 // but we haven't seen it yet.7026 if (DFII->second == 0)7027 if (Error Err = findFunctionInStream(F, DFII))7028 return Err;7029 7030 // Materialize metadata before parsing any function bodies.7031 if (Error Err = materializeMetadata())7032 return Err;7033 7034 // Move the bit stream to the saved position of the deferred function body.7035 if (Error JumpFailed = Stream.JumpToBit(DFII->second))7036 return JumpFailed;7037 7038 if (Error Err = parseFunctionBody(F))7039 return Err;7040 F->setIsMaterializable(false);7041 7042 // All parsed Functions should load into the debug info format dictated by the7043 // Module.7044 if (SeenDebugIntrinsic && SeenDebugRecord)7045 return error("Mixed debug intrinsics and debug records in bitcode module!");7046 7047 if (StripDebugInfo)7048 stripDebugInfo(*F);7049 7050 // Finish fn->subprogram upgrade for materialized functions.7051 if (DISubprogram *SP = MDLoader->lookupSubprogramForFunction(F))7052 F->setSubprogram(SP);7053 7054 // Check if the TBAA Metadata are valid, otherwise we will need to strip them.7055 if (!MDLoader->isStrippingTBAA()) {7056 for (auto &I : instructions(F)) {7057 MDNode *TBAA = I.getMetadata(LLVMContext::MD_tbaa);7058 if (!TBAA || TBAAVerifyHelper.visitTBAAMetadata(&I, TBAA))7059 continue;7060 MDLoader->setStripTBAA(true);7061 stripTBAA(F->getParent());7062 }7063 }7064 7065 for (auto &I : make_early_inc_range(instructions(F))) {7066 // "Upgrade" older incorrect branch weights by dropping them.7067 if (auto *MD = I.getMetadata(LLVMContext::MD_prof)) {7068 if (MD->getOperand(0) != nullptr && isa<MDString>(MD->getOperand(0))) {7069 MDString *MDS = cast<MDString>(MD->getOperand(0));7070 StringRef ProfName = MDS->getString();7071 // Check consistency of !prof branch_weights metadata.7072 if (ProfName != MDProfLabels::BranchWeights)7073 continue;7074 unsigned ExpectedNumOperands = 0;7075 if (BranchInst *BI = dyn_cast<BranchInst>(&I))7076 ExpectedNumOperands = BI->getNumSuccessors();7077 else if (SwitchInst *SI = dyn_cast<SwitchInst>(&I))7078 ExpectedNumOperands = SI->getNumSuccessors();7079 else if (isa<CallInst>(&I))7080 ExpectedNumOperands = 1;7081 else if (IndirectBrInst *IBI = dyn_cast<IndirectBrInst>(&I))7082 ExpectedNumOperands = IBI->getNumDestinations();7083 else if (isa<SelectInst>(&I))7084 ExpectedNumOperands = 2;7085 else7086 continue; // ignore and continue.7087 7088 unsigned Offset = getBranchWeightOffset(MD);7089 7090 // If branch weight doesn't match, just strip branch weight.7091 if (MD->getNumOperands() != Offset + ExpectedNumOperands)7092 I.setMetadata(LLVMContext::MD_prof, nullptr);7093 }7094 }7095 7096 if (auto *CI = dyn_cast<CallBase>(&I)) {7097 // Remove incompatible attributes on function calls.7098 CI->removeRetAttrs(AttributeFuncs::typeIncompatible(7099 CI->getFunctionType()->getReturnType(), CI->getRetAttributes()));7100 7101 for (unsigned ArgNo = 0; ArgNo < CI->arg_size(); ++ArgNo)7102 CI->removeParamAttrs(ArgNo, AttributeFuncs::typeIncompatible(7103 CI->getArgOperand(ArgNo)->getType(),7104 CI->getParamAttributes(ArgNo)));7105 7106 // Upgrade intrinsics.7107 if (Function *OldFn = CI->getCalledFunction()) {7108 auto It = UpgradedIntrinsics.find(OldFn);7109 if (It != UpgradedIntrinsics.end())7110 UpgradeIntrinsicCall(CI, It->second);7111 }7112 }7113 }7114 7115 // Look for functions that rely on old function attribute behavior.7116 UpgradeFunctionAttributes(*F);7117 7118 // Bring in any functions that this function forward-referenced via7119 // blockaddresses.7120 return materializeForwardReferencedFunctions();7121}7122 7123Error BitcodeReader::materializeModule() {7124 if (Error Err = materializeMetadata())7125 return Err;7126 7127 // Promise to materialize all forward references.7128 WillMaterializeAllForwardRefs = true;7129 7130 // Iterate over the module, deserializing any functions that are still on7131 // disk.7132 for (Function &F : *TheModule) {7133 if (Error Err = materialize(&F))7134 return Err;7135 }7136 // At this point, if there are any function bodies, parse the rest of7137 // the bits in the module past the last function block we have recorded7138 // through either lazy scanning or the VST.7139 if (LastFunctionBlockBit || NextUnreadBit)7140 if (Error Err = parseModule(LastFunctionBlockBit > NextUnreadBit7141 ? LastFunctionBlockBit7142 : NextUnreadBit))7143 return Err;7144 7145 // Check that all block address forward references got resolved (as we7146 // promised above).7147 if (!BasicBlockFwdRefs.empty())7148 return error("Never resolved function from blockaddress");7149 7150 // Upgrade any intrinsic calls that slipped through (should not happen!) and7151 // delete the old functions to clean up. We can't do this unless the entire7152 // module is materialized because there could always be another function body7153 // with calls to the old function.7154 for (auto &I : UpgradedIntrinsics) {7155 for (auto *U : I.first->users()) {7156 if (CallInst *CI = dyn_cast<CallInst>(U))7157 UpgradeIntrinsicCall(CI, I.second);7158 }7159 if (I.first != I.second) {7160 if (!I.first->use_empty())7161 I.first->replaceAllUsesWith(I.second);7162 I.first->eraseFromParent();7163 }7164 }7165 UpgradedIntrinsics.clear();7166 7167 UpgradeDebugInfo(*TheModule);7168 7169 UpgradeModuleFlags(*TheModule);7170 7171 UpgradeNVVMAnnotations(*TheModule);7172 7173 UpgradeARCRuntime(*TheModule);7174 7175 copyModuleAttrToFunctions(*TheModule);7176 7177 return Error::success();7178}7179 7180std::vector<StructType *> BitcodeReader::getIdentifiedStructTypes() const {7181 return IdentifiedStructTypes;7182}7183 7184ModuleSummaryIndexBitcodeReader::ModuleSummaryIndexBitcodeReader(7185 BitstreamCursor Cursor, StringRef Strtab, ModuleSummaryIndex &TheIndex,7186 StringRef ModulePath, std::function<bool(GlobalValue::GUID)> IsPrevailing)7187 : BitcodeReaderBase(std::move(Cursor), Strtab), TheIndex(TheIndex),7188 ModulePath(ModulePath), IsPrevailing(IsPrevailing) {}7189 7190void ModuleSummaryIndexBitcodeReader::addThisModule() {7191 TheIndex.addModule(ModulePath);7192}7193 7194ModuleSummaryIndex::ModuleInfo *7195ModuleSummaryIndexBitcodeReader::getThisModule() {7196 return TheIndex.getModule(ModulePath);7197}7198 7199template <bool AllowNullValueInfo>7200std::pair<ValueInfo, GlobalValue::GUID>7201ModuleSummaryIndexBitcodeReader::getValueInfoFromValueId(unsigned ValueId) {7202 auto VGI = ValueIdToValueInfoMap[ValueId];7203 // We can have a null value info for memprof callsite info records in7204 // distributed ThinLTO index files when the callee function summary is not7205 // included in the index. The bitcode writer records 0 in that case,7206 // and the caller of this helper will set AllowNullValueInfo to true.7207 assert(AllowNullValueInfo || std::get<0>(VGI));7208 return VGI;7209}7210 7211void ModuleSummaryIndexBitcodeReader::setValueGUID(7212 uint64_t ValueID, StringRef ValueName, GlobalValue::LinkageTypes Linkage,7213 StringRef SourceFileName) {7214 std::string GlobalId =7215 GlobalValue::getGlobalIdentifier(ValueName, Linkage, SourceFileName);7216 auto ValueGUID = GlobalValue::getGUIDAssumingExternalLinkage(GlobalId);7217 auto OriginalNameID = ValueGUID;7218 if (GlobalValue::isLocalLinkage(Linkage))7219 OriginalNameID = GlobalValue::getGUIDAssumingExternalLinkage(ValueName);7220 if (PrintSummaryGUIDs)7221 dbgs() << "GUID " << ValueGUID << "(" << OriginalNameID << ") is "7222 << ValueName << "\n";7223 7224 // UseStrtab is false for legacy summary formats and value names are7225 // created on stack. In that case we save the name in a string saver in7226 // the index so that the value name can be recorded.7227 ValueIdToValueInfoMap[ValueID] = std::make_pair(7228 TheIndex.getOrInsertValueInfo(7229 ValueGUID, UseStrtab ? ValueName : TheIndex.saveString(ValueName)),7230 OriginalNameID);7231}7232 7233// Specialized value symbol table parser used when reading module index7234// blocks where we don't actually create global values. The parsed information7235// is saved in the bitcode reader for use when later parsing summaries.7236Error ModuleSummaryIndexBitcodeReader::parseValueSymbolTable(7237 uint64_t Offset,7238 DenseMap<unsigned, GlobalValue::LinkageTypes> &ValueIdToLinkageMap) {7239 // With a strtab the VST is not required to parse the summary.7240 if (UseStrtab)7241 return Error::success();7242 7243 assert(Offset > 0 && "Expected non-zero VST offset");7244 Expected<uint64_t> MaybeCurrentBit = jumpToValueSymbolTable(Offset, Stream);7245 if (!MaybeCurrentBit)7246 return MaybeCurrentBit.takeError();7247 uint64_t CurrentBit = MaybeCurrentBit.get();7248 7249 if (Error Err = Stream.EnterSubBlock(bitc::VALUE_SYMTAB_BLOCK_ID))7250 return Err;7251 7252 SmallVector<uint64_t, 64> Record;7253 7254 // Read all the records for this value table.7255 SmallString<128> ValueName;7256 7257 while (true) {7258 Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks();7259 if (!MaybeEntry)7260 return MaybeEntry.takeError();7261 BitstreamEntry Entry = MaybeEntry.get();7262 7263 switch (Entry.Kind) {7264 case BitstreamEntry::SubBlock: // Handled for us already.7265 case BitstreamEntry::Error:7266 return error("Malformed block");7267 case BitstreamEntry::EndBlock:7268 // Done parsing VST, jump back to wherever we came from.7269 if (Error JumpFailed = Stream.JumpToBit(CurrentBit))7270 return JumpFailed;7271 return Error::success();7272 case BitstreamEntry::Record:7273 // The interesting case.7274 break;7275 }7276 7277 // Read a record.7278 Record.clear();7279 Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record);7280 if (!MaybeRecord)7281 return MaybeRecord.takeError();7282 switch (MaybeRecord.get()) {7283 default: // Default behavior: ignore (e.g. VST_CODE_BBENTRY records).7284 break;7285 case bitc::VST_CODE_ENTRY: { // VST_CODE_ENTRY: [valueid, namechar x N]7286 if (convertToString(Record, 1, ValueName))7287 return error("Invalid record");7288 unsigned ValueID = Record[0];7289 assert(!SourceFileName.empty());7290 auto VLI = ValueIdToLinkageMap.find(ValueID);7291 assert(VLI != ValueIdToLinkageMap.end() &&7292 "No linkage found for VST entry?");7293 auto Linkage = VLI->second;7294 setValueGUID(ValueID, ValueName, Linkage, SourceFileName);7295 ValueName.clear();7296 break;7297 }7298 case bitc::VST_CODE_FNENTRY: {7299 // VST_CODE_FNENTRY: [valueid, offset, namechar x N]7300 if (convertToString(Record, 2, ValueName))7301 return error("Invalid record");7302 unsigned ValueID = Record[0];7303 assert(!SourceFileName.empty());7304 auto VLI = ValueIdToLinkageMap.find(ValueID);7305 assert(VLI != ValueIdToLinkageMap.end() &&7306 "No linkage found for VST entry?");7307 auto Linkage = VLI->second;7308 setValueGUID(ValueID, ValueName, Linkage, SourceFileName);7309 ValueName.clear();7310 break;7311 }7312 case bitc::VST_CODE_COMBINED_ENTRY: {7313 // VST_CODE_COMBINED_ENTRY: [valueid, refguid]7314 unsigned ValueID = Record[0];7315 GlobalValue::GUID RefGUID = Record[1];7316 // The "original name", which is the second value of the pair will be7317 // overriden later by a FS_COMBINED_ORIGINAL_NAME in the combined index.7318 ValueIdToValueInfoMap[ValueID] =7319 std::make_pair(TheIndex.getOrInsertValueInfo(RefGUID), RefGUID);7320 break;7321 }7322 }7323 }7324}7325 7326// Parse just the blocks needed for building the index out of the module.7327// At the end of this routine the module Index is populated with a map7328// from global value id to GlobalValueSummary objects.7329Error ModuleSummaryIndexBitcodeReader::parseModule() {7330 if (Error Err = Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID))7331 return Err;7332 7333 SmallVector<uint64_t, 64> Record;7334 DenseMap<unsigned, GlobalValue::LinkageTypes> ValueIdToLinkageMap;7335 unsigned ValueId = 0;7336 7337 // Read the index for this module.7338 while (true) {7339 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();7340 if (!MaybeEntry)7341 return MaybeEntry.takeError();7342 llvm::BitstreamEntry Entry = MaybeEntry.get();7343 7344 switch (Entry.Kind) {7345 case BitstreamEntry::Error:7346 return error("Malformed block");7347 case BitstreamEntry::EndBlock:7348 return Error::success();7349 7350 case BitstreamEntry::SubBlock:7351 switch (Entry.ID) {7352 default: // Skip unknown content.7353 if (Error Err = Stream.SkipBlock())7354 return Err;7355 break;7356 case bitc::BLOCKINFO_BLOCK_ID:7357 // Need to parse these to get abbrev ids (e.g. for VST)7358 if (Error Err = readBlockInfo())7359 return Err;7360 break;7361 case bitc::VALUE_SYMTAB_BLOCK_ID:7362 // Should have been parsed earlier via VSTOffset, unless there7363 // is no summary section.7364 assert(((SeenValueSymbolTable && VSTOffset > 0) ||7365 !SeenGlobalValSummary) &&7366 "Expected early VST parse via VSTOffset record");7367 if (Error Err = Stream.SkipBlock())7368 return Err;7369 break;7370 case bitc::GLOBALVAL_SUMMARY_BLOCK_ID:7371 case bitc::FULL_LTO_GLOBALVAL_SUMMARY_BLOCK_ID:7372 // Add the module if it is a per-module index (has a source file name).7373 if (!SourceFileName.empty())7374 addThisModule();7375 assert(!SeenValueSymbolTable &&7376 "Already read VST when parsing summary block?");7377 // We might not have a VST if there were no values in the7378 // summary. An empty summary block generated when we are7379 // performing ThinLTO compiles so we don't later invoke7380 // the regular LTO process on them.7381 if (VSTOffset > 0) {7382 if (Error Err = parseValueSymbolTable(VSTOffset, ValueIdToLinkageMap))7383 return Err;7384 SeenValueSymbolTable = true;7385 }7386 SeenGlobalValSummary = true;7387 if (Error Err = parseEntireSummary(Entry.ID))7388 return Err;7389 break;7390 case bitc::MODULE_STRTAB_BLOCK_ID:7391 if (Error Err = parseModuleStringTable())7392 return Err;7393 break;7394 }7395 continue;7396 7397 case BitstreamEntry::Record: {7398 Record.clear();7399 Expected<unsigned> MaybeBitCode = Stream.readRecord(Entry.ID, Record);7400 if (!MaybeBitCode)7401 return MaybeBitCode.takeError();7402 switch (MaybeBitCode.get()) {7403 default:7404 break; // Default behavior, ignore unknown content.7405 case bitc::MODULE_CODE_VERSION: {7406 if (Error Err = parseVersionRecord(Record).takeError())7407 return Err;7408 break;7409 }7410 /// MODULE_CODE_SOURCE_FILENAME: [namechar x N]7411 case bitc::MODULE_CODE_SOURCE_FILENAME: {7412 SmallString<128> ValueName;7413 if (convertToString(Record, 0, ValueName))7414 return error("Invalid record");7415 SourceFileName = ValueName.c_str();7416 break;7417 }7418 /// MODULE_CODE_HASH: [5*i32]7419 case bitc::MODULE_CODE_HASH: {7420 if (Record.size() != 5)7421 return error("Invalid hash length " + Twine(Record.size()).str());7422 auto &Hash = getThisModule()->second;7423 int Pos = 0;7424 for (auto &Val : Record) {7425 assert(!(Val >> 32) && "Unexpected high bits set");7426 Hash[Pos++] = Val;7427 }7428 break;7429 }7430 /// MODULE_CODE_VSTOFFSET: [offset]7431 case bitc::MODULE_CODE_VSTOFFSET:7432 if (Record.empty())7433 return error("Invalid record");7434 // Note that we subtract 1 here because the offset is relative to one7435 // word before the start of the identification or module block, which7436 // was historically always the start of the regular bitcode header.7437 VSTOffset = Record[0] - 1;7438 break;7439 // v1 GLOBALVAR: [pointer type, isconst, initid, linkage, ...]7440 // v1 FUNCTION: [type, callingconv, isproto, linkage, ...]7441 // v1 ALIAS: [alias type, addrspace, aliasee val#, linkage, ...]7442 // v2: [strtab offset, strtab size, v1]7443 case bitc::MODULE_CODE_GLOBALVAR:7444 case bitc::MODULE_CODE_FUNCTION:7445 case bitc::MODULE_CODE_ALIAS: {7446 StringRef Name;7447 ArrayRef<uint64_t> GVRecord;7448 std::tie(Name, GVRecord) = readNameFromStrtab(Record);7449 if (GVRecord.size() <= 3)7450 return error("Invalid record");7451 uint64_t RawLinkage = GVRecord[3];7452 GlobalValue::LinkageTypes Linkage = getDecodedLinkage(RawLinkage);7453 if (!UseStrtab) {7454 ValueIdToLinkageMap[ValueId++] = Linkage;7455 break;7456 }7457 7458 setValueGUID(ValueId++, Name, Linkage, SourceFileName);7459 break;7460 }7461 }7462 }7463 continue;7464 }7465 }7466}7467 7468SmallVector<ValueInfo, 0>7469ModuleSummaryIndexBitcodeReader::makeRefList(ArrayRef<uint64_t> Record) {7470 SmallVector<ValueInfo, 0> Ret;7471 Ret.reserve(Record.size());7472 for (uint64_t RefValueId : Record)7473 Ret.push_back(std::get<0>(getValueInfoFromValueId(RefValueId)));7474 return Ret;7475}7476 7477SmallVector<FunctionSummary::EdgeTy, 0>7478ModuleSummaryIndexBitcodeReader::makeCallList(ArrayRef<uint64_t> Record,7479 bool IsOldProfileFormat,7480 bool HasProfile, bool HasRelBF) {7481 SmallVector<FunctionSummary::EdgeTy, 0> Ret;7482 // In the case of new profile formats, there are two Record entries per7483 // Edge. Otherwise, conservatively reserve up to Record.size.7484 if (!IsOldProfileFormat && (HasProfile || HasRelBF))7485 Ret.reserve(Record.size() / 2);7486 else7487 Ret.reserve(Record.size());7488 7489 for (unsigned I = 0, E = Record.size(); I != E; ++I) {7490 CalleeInfo::HotnessType Hotness = CalleeInfo::HotnessType::Unknown;7491 bool HasTailCall = false;7492 uint64_t RelBF = 0;7493 ValueInfo Callee = std::get<0>(getValueInfoFromValueId(Record[I]));7494 if (IsOldProfileFormat) {7495 I += 1; // Skip old callsitecount field7496 if (HasProfile)7497 I += 1; // Skip old profilecount field7498 } else if (HasProfile)7499 std::tie(Hotness, HasTailCall) =7500 getDecodedHotnessCallEdgeInfo(Record[++I]);7501 else if (HasRelBF)7502 getDecodedRelBFCallEdgeInfo(Record[++I], RelBF, HasTailCall);7503 Ret.push_back(FunctionSummary::EdgeTy{7504 Callee, CalleeInfo(Hotness, HasTailCall, RelBF)});7505 }7506 return Ret;7507}7508 7509static void7510parseWholeProgramDevirtResolutionByArg(ArrayRef<uint64_t> Record, size_t &Slot,7511 WholeProgramDevirtResolution &Wpd) {7512 uint64_t ArgNum = Record[Slot++];7513 WholeProgramDevirtResolution::ByArg &B =7514 Wpd.ResByArg[{Record.begin() + Slot, Record.begin() + Slot + ArgNum}];7515 Slot += ArgNum;7516 7517 B.TheKind =7518 static_cast<WholeProgramDevirtResolution::ByArg::Kind>(Record[Slot++]);7519 B.Info = Record[Slot++];7520 B.Byte = Record[Slot++];7521 B.Bit = Record[Slot++];7522}7523 7524static void parseWholeProgramDevirtResolution(ArrayRef<uint64_t> Record,7525 StringRef Strtab, size_t &Slot,7526 TypeIdSummary &TypeId) {7527 uint64_t Id = Record[Slot++];7528 WholeProgramDevirtResolution &Wpd = TypeId.WPDRes[Id];7529 7530 Wpd.TheKind = static_cast<WholeProgramDevirtResolution::Kind>(Record[Slot++]);7531 Wpd.SingleImplName = {Strtab.data() + Record[Slot],7532 static_cast<size_t>(Record[Slot + 1])};7533 Slot += 2;7534 7535 uint64_t ResByArgNum = Record[Slot++];7536 for (uint64_t I = 0; I != ResByArgNum; ++I)7537 parseWholeProgramDevirtResolutionByArg(Record, Slot, Wpd);7538}7539 7540static void parseTypeIdSummaryRecord(ArrayRef<uint64_t> Record,7541 StringRef Strtab,7542 ModuleSummaryIndex &TheIndex) {7543 size_t Slot = 0;7544 TypeIdSummary &TypeId = TheIndex.getOrInsertTypeIdSummary(7545 {Strtab.data() + Record[Slot], static_cast<size_t>(Record[Slot + 1])});7546 Slot += 2;7547 7548 TypeId.TTRes.TheKind = static_cast<TypeTestResolution::Kind>(Record[Slot++]);7549 TypeId.TTRes.SizeM1BitWidth = Record[Slot++];7550 TypeId.TTRes.AlignLog2 = Record[Slot++];7551 TypeId.TTRes.SizeM1 = Record[Slot++];7552 TypeId.TTRes.BitMask = Record[Slot++];7553 TypeId.TTRes.InlineBits = Record[Slot++];7554 7555 while (Slot < Record.size())7556 parseWholeProgramDevirtResolution(Record, Strtab, Slot, TypeId);7557}7558 7559std::vector<FunctionSummary::ParamAccess>7560ModuleSummaryIndexBitcodeReader::parseParamAccesses(ArrayRef<uint64_t> Record) {7561 auto ReadRange = [&]() {7562 APInt Lower(FunctionSummary::ParamAccess::RangeWidth,7563 BitcodeReader::decodeSignRotatedValue(Record.consume_front()));7564 APInt Upper(FunctionSummary::ParamAccess::RangeWidth,7565 BitcodeReader::decodeSignRotatedValue(Record.consume_front()));7566 ConstantRange Range{Lower, Upper};7567 assert(!Range.isFullSet());7568 assert(!Range.isUpperSignWrapped());7569 return Range;7570 };7571 7572 std::vector<FunctionSummary::ParamAccess> PendingParamAccesses;7573 while (!Record.empty()) {7574 PendingParamAccesses.emplace_back();7575 FunctionSummary::ParamAccess &ParamAccess = PendingParamAccesses.back();7576 ParamAccess.ParamNo = Record.consume_front();7577 ParamAccess.Use = ReadRange();7578 ParamAccess.Calls.resize(Record.consume_front());7579 for (auto &Call : ParamAccess.Calls) {7580 Call.ParamNo = Record.consume_front();7581 Call.Callee =7582 std::get<0>(getValueInfoFromValueId(Record.consume_front()));7583 Call.Offsets = ReadRange();7584 }7585 }7586 return PendingParamAccesses;7587}7588 7589void ModuleSummaryIndexBitcodeReader::parseTypeIdCompatibleVtableInfo(7590 ArrayRef<uint64_t> Record, size_t &Slot,7591 TypeIdCompatibleVtableInfo &TypeId) {7592 uint64_t Offset = Record[Slot++];7593 ValueInfo Callee = std::get<0>(getValueInfoFromValueId(Record[Slot++]));7594 TypeId.push_back({Offset, Callee});7595}7596 7597void ModuleSummaryIndexBitcodeReader::parseTypeIdCompatibleVtableSummaryRecord(7598 ArrayRef<uint64_t> Record) {7599 size_t Slot = 0;7600 TypeIdCompatibleVtableInfo &TypeId =7601 TheIndex.getOrInsertTypeIdCompatibleVtableSummary(7602 {Strtab.data() + Record[Slot],7603 static_cast<size_t>(Record[Slot + 1])});7604 Slot += 2;7605 7606 while (Slot < Record.size())7607 parseTypeIdCompatibleVtableInfo(Record, Slot, TypeId);7608}7609 7610SmallVector<unsigned> ModuleSummaryIndexBitcodeReader::parseAllocInfoContext(7611 ArrayRef<uint64_t> Record, unsigned &I) {7612 SmallVector<unsigned> StackIdList;7613 // For backwards compatibility with old format before radix tree was7614 // used, simply see if we found a radix tree array record (and thus if7615 // the RadixArray is non-empty).7616 if (RadixArray.empty()) {7617 unsigned NumStackEntries = Record[I++];7618 assert(Record.size() - I >= NumStackEntries);7619 StackIdList.reserve(NumStackEntries);7620 for (unsigned J = 0; J < NumStackEntries; J++) {7621 assert(Record[I] < StackIds.size());7622 StackIdList.push_back(7623 TheIndex.addOrGetStackIdIndex(StackIds[Record[I++]]));7624 }7625 } else {7626 unsigned RadixIndex = Record[I++];7627 // See the comments above CallStackRadixTreeBuilder in ProfileData/MemProf.h7628 // for a detailed description of the radix tree array format. Briefly, the7629 // first entry will be the number of frames, any negative values are the7630 // negative of the offset of the next frame, and otherwise the frames are in7631 // increasing linear order.7632 assert(RadixIndex < RadixArray.size());7633 unsigned NumStackIds = RadixArray[RadixIndex++];7634 StackIdList.reserve(NumStackIds);7635 while (NumStackIds--) {7636 assert(RadixIndex < RadixArray.size());7637 unsigned Elem = RadixArray[RadixIndex];7638 if (static_cast<std::make_signed_t<unsigned>>(Elem) < 0) {7639 RadixIndex = RadixIndex - Elem;7640 assert(RadixIndex < RadixArray.size());7641 Elem = RadixArray[RadixIndex];7642 // We shouldn't encounter a second offset in a row.7643 assert(static_cast<std::make_signed_t<unsigned>>(Elem) >= 0);7644 }7645 RadixIndex++;7646 StackIdList.push_back(TheIndex.addOrGetStackIdIndex(StackIds[Elem]));7647 }7648 }7649 return StackIdList;7650}7651 7652static void setSpecialRefs(SmallVectorImpl<ValueInfo> &Refs, unsigned ROCnt,7653 unsigned WOCnt) {7654 // Readonly and writeonly refs are in the end of the refs list.7655 assert(ROCnt + WOCnt <= Refs.size());7656 unsigned FirstWORef = Refs.size() - WOCnt;7657 unsigned RefNo = FirstWORef - ROCnt;7658 for (; RefNo < FirstWORef; ++RefNo)7659 Refs[RefNo].setReadOnly();7660 for (; RefNo < Refs.size(); ++RefNo)7661 Refs[RefNo].setWriteOnly();7662}7663 7664// Eagerly parse the entire summary block. This populates the GlobalValueSummary7665// objects in the index.7666Error ModuleSummaryIndexBitcodeReader::parseEntireSummary(unsigned ID) {7667 if (Error Err = Stream.EnterSubBlock(ID))7668 return Err;7669 SmallVector<uint64_t, 64> Record;7670 7671 // Parse version7672 {7673 Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks();7674 if (!MaybeEntry)7675 return MaybeEntry.takeError();7676 BitstreamEntry Entry = MaybeEntry.get();7677 7678 if (Entry.Kind != BitstreamEntry::Record)7679 return error("Invalid Summary Block: record for version expected");7680 Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record);7681 if (!MaybeRecord)7682 return MaybeRecord.takeError();7683 if (MaybeRecord.get() != bitc::FS_VERSION)7684 return error("Invalid Summary Block: version expected");7685 }7686 const uint64_t Version = Record[0];7687 const bool IsOldProfileFormat = Version == 1;7688 if (Version < 1 || Version > ModuleSummaryIndex::BitcodeSummaryVersion)7689 return error("Invalid summary version " + Twine(Version) +7690 ". Version should be in the range [1-" +7691 Twine(ModuleSummaryIndex::BitcodeSummaryVersion) +7692 "].");7693 Record.clear();7694 7695 // Keep around the last seen summary to be used when we see an optional7696 // "OriginalName" attachement.7697 GlobalValueSummary *LastSeenSummary = nullptr;7698 GlobalValue::GUID LastSeenGUID = 0;7699 7700 // We can expect to see any number of type ID information records before7701 // each function summary records; these variables store the information7702 // collected so far so that it can be used to create the summary object.7703 std::vector<GlobalValue::GUID> PendingTypeTests;7704 std::vector<FunctionSummary::VFuncId> PendingTypeTestAssumeVCalls,7705 PendingTypeCheckedLoadVCalls;7706 std::vector<FunctionSummary::ConstVCall> PendingTypeTestAssumeConstVCalls,7707 PendingTypeCheckedLoadConstVCalls;7708 std::vector<FunctionSummary::ParamAccess> PendingParamAccesses;7709 7710 std::vector<CallsiteInfo> PendingCallsites;7711 std::vector<AllocInfo> PendingAllocs;7712 std::vector<uint64_t> PendingContextIds;7713 7714 while (true) {7715 Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks();7716 if (!MaybeEntry)7717 return MaybeEntry.takeError();7718 BitstreamEntry Entry = MaybeEntry.get();7719 7720 switch (Entry.Kind) {7721 case BitstreamEntry::SubBlock: // Handled for us already.7722 case BitstreamEntry::Error:7723 return error("Malformed block");7724 case BitstreamEntry::EndBlock:7725 return Error::success();7726 case BitstreamEntry::Record:7727 // The interesting case.7728 break;7729 }7730 7731 // Read a record. The record format depends on whether this7732 // is a per-module index or a combined index file. In the per-module7733 // case the records contain the associated value's ID for correlation7734 // with VST entries. In the combined index the correlation is done7735 // via the bitcode offset of the summary records (which were saved7736 // in the combined index VST entries). The records also contain7737 // information used for ThinLTO renaming and importing.7738 Record.clear();7739 Expected<unsigned> MaybeBitCode = Stream.readRecord(Entry.ID, Record);7740 if (!MaybeBitCode)7741 return MaybeBitCode.takeError();7742 switch (unsigned BitCode = MaybeBitCode.get()) {7743 default: // Default behavior: ignore.7744 break;7745 case bitc::FS_FLAGS: { // [flags]7746 TheIndex.setFlags(Record[0]);7747 break;7748 }7749 case bitc::FS_VALUE_GUID: { // [valueid, refguid_upper32, refguid_lower32]7750 uint64_t ValueID = Record[0];7751 GlobalValue::GUID RefGUID;7752 if (Version >= 11) {7753 RefGUID = Record[1] << 32 | Record[2];7754 } else {7755 RefGUID = Record[1];7756 }7757 ValueIdToValueInfoMap[ValueID] =7758 std::make_pair(TheIndex.getOrInsertValueInfo(RefGUID), RefGUID);7759 break;7760 }7761 // FS_PERMODULE is legacy and does not have support for the tail call flag.7762 // FS_PERMODULE: [valueid, flags, instcount, fflags, numrefs,7763 // numrefs x valueid, n x (valueid)]7764 // FS_PERMODULE_PROFILE: [valueid, flags, instcount, fflags, numrefs,7765 // numrefs x valueid,7766 // n x (valueid, hotness+tailcall flags)]7767 // FS_PERMODULE_RELBF: [valueid, flags, instcount, fflags, numrefs,7768 // numrefs x valueid,7769 // n x (valueid, relblockfreq+tailcall)]7770 case bitc::FS_PERMODULE:7771 case bitc::FS_PERMODULE_RELBF:7772 case bitc::FS_PERMODULE_PROFILE: {7773 unsigned ValueID = Record[0];7774 uint64_t RawFlags = Record[1];7775 unsigned InstCount = Record[2];7776 uint64_t RawFunFlags = 0;7777 unsigned NumRefs = Record[3];7778 unsigned NumRORefs = 0, NumWORefs = 0;7779 int RefListStartIndex = 4;7780 if (Version >= 4) {7781 RawFunFlags = Record[3];7782 NumRefs = Record[4];7783 RefListStartIndex = 5;7784 if (Version >= 5) {7785 NumRORefs = Record[5];7786 RefListStartIndex = 6;7787 if (Version >= 7) {7788 NumWORefs = Record[6];7789 RefListStartIndex = 7;7790 }7791 }7792 }7793 7794 auto Flags = getDecodedGVSummaryFlags(RawFlags, Version);7795 // The module path string ref set in the summary must be owned by the7796 // index's module string table. Since we don't have a module path7797 // string table section in the per-module index, we create a single7798 // module path string table entry with an empty (0) ID to take7799 // ownership.7800 int CallGraphEdgeStartIndex = RefListStartIndex + NumRefs;7801 assert(Record.size() >= RefListStartIndex + NumRefs &&7802 "Record size inconsistent with number of references");7803 SmallVector<ValueInfo, 0> Refs = makeRefList(7804 ArrayRef<uint64_t>(Record).slice(RefListStartIndex, NumRefs));7805 bool HasProfile = (BitCode == bitc::FS_PERMODULE_PROFILE);7806 bool HasRelBF = (BitCode == bitc::FS_PERMODULE_RELBF);7807 SmallVector<FunctionSummary::EdgeTy, 0> Calls = makeCallList(7808 ArrayRef<uint64_t>(Record).slice(CallGraphEdgeStartIndex),7809 IsOldProfileFormat, HasProfile, HasRelBF);7810 setSpecialRefs(Refs, NumRORefs, NumWORefs);7811 auto VIAndOriginalGUID = getValueInfoFromValueId(ValueID);7812 // In order to save memory, only record the memprof summaries if this is7813 // the prevailing copy of a symbol. The linker doesn't resolve local7814 // linkage values so don't check whether those are prevailing.7815 auto LT = (GlobalValue::LinkageTypes)Flags.Linkage;7816 if (IsPrevailing && !GlobalValue::isLocalLinkage(LT) &&7817 !IsPrevailing(VIAndOriginalGUID.first.getGUID())) {7818 PendingCallsites.clear();7819 PendingAllocs.clear();7820 }7821 auto FS = std::make_unique<FunctionSummary>(7822 Flags, InstCount, getDecodedFFlags(RawFunFlags), std::move(Refs),7823 std::move(Calls), std::move(PendingTypeTests),7824 std::move(PendingTypeTestAssumeVCalls),7825 std::move(PendingTypeCheckedLoadVCalls),7826 std::move(PendingTypeTestAssumeConstVCalls),7827 std::move(PendingTypeCheckedLoadConstVCalls),7828 std::move(PendingParamAccesses), std::move(PendingCallsites),7829 std::move(PendingAllocs));7830 FS->setModulePath(getThisModule()->first());7831 FS->setOriginalName(std::get<1>(VIAndOriginalGUID));7832 TheIndex.addGlobalValueSummary(std::get<0>(VIAndOriginalGUID),7833 std::move(FS));7834 break;7835 }7836 // FS_ALIAS: [valueid, flags, valueid]7837 // Aliases must be emitted (and parsed) after all FS_PERMODULE entries, as7838 // they expect all aliasee summaries to be available.7839 case bitc::FS_ALIAS: {7840 unsigned ValueID = Record[0];7841 uint64_t RawFlags = Record[1];7842 unsigned AliaseeID = Record[2];7843 auto Flags = getDecodedGVSummaryFlags(RawFlags, Version);7844 auto AS = std::make_unique<AliasSummary>(Flags);7845 // The module path string ref set in the summary must be owned by the7846 // index's module string table. Since we don't have a module path7847 // string table section in the per-module index, we create a single7848 // module path string table entry with an empty (0) ID to take7849 // ownership.7850 AS->setModulePath(getThisModule()->first());7851 7852 auto AliaseeVI = std::get<0>(getValueInfoFromValueId(AliaseeID));7853 auto AliaseeInModule = TheIndex.findSummaryInModule(AliaseeVI, ModulePath);7854 if (!AliaseeInModule)7855 return error("Alias expects aliasee summary to be parsed");7856 AS->setAliasee(AliaseeVI, AliaseeInModule);7857 7858 auto GUID = getValueInfoFromValueId(ValueID);7859 AS->setOriginalName(std::get<1>(GUID));7860 TheIndex.addGlobalValueSummary(std::get<0>(GUID), std::move(AS));7861 break;7862 }7863 // FS_PERMODULE_GLOBALVAR_INIT_REFS: [valueid, flags, varflags, n x valueid]7864 case bitc::FS_PERMODULE_GLOBALVAR_INIT_REFS: {7865 unsigned ValueID = Record[0];7866 uint64_t RawFlags = Record[1];7867 unsigned RefArrayStart = 2;7868 GlobalVarSummary::GVarFlags GVF(/* ReadOnly */ false,7869 /* WriteOnly */ false,7870 /* Constant */ false,7871 GlobalObject::VCallVisibilityPublic);7872 auto Flags = getDecodedGVSummaryFlags(RawFlags, Version);7873 if (Version >= 5) {7874 GVF = getDecodedGVarFlags(Record[2]);7875 RefArrayStart = 3;7876 }7877 SmallVector<ValueInfo, 0> Refs =7878 makeRefList(ArrayRef<uint64_t>(Record).slice(RefArrayStart));7879 auto FS =7880 std::make_unique<GlobalVarSummary>(Flags, GVF, std::move(Refs));7881 FS->setModulePath(getThisModule()->first());7882 auto GUID = getValueInfoFromValueId(ValueID);7883 FS->setOriginalName(std::get<1>(GUID));7884 TheIndex.addGlobalValueSummary(std::get<0>(GUID), std::move(FS));7885 break;7886 }7887 // FS_PERMODULE_VTABLE_GLOBALVAR_INIT_REFS: [valueid, flags, varflags,7888 // numrefs, numrefs x valueid,7889 // n x (valueid, offset)]7890 case bitc::FS_PERMODULE_VTABLE_GLOBALVAR_INIT_REFS: {7891 unsigned ValueID = Record[0];7892 uint64_t RawFlags = Record[1];7893 GlobalVarSummary::GVarFlags GVF = getDecodedGVarFlags(Record[2]);7894 unsigned NumRefs = Record[3];7895 unsigned RefListStartIndex = 4;7896 unsigned VTableListStartIndex = RefListStartIndex + NumRefs;7897 auto Flags = getDecodedGVSummaryFlags(RawFlags, Version);7898 SmallVector<ValueInfo, 0> Refs = makeRefList(7899 ArrayRef<uint64_t>(Record).slice(RefListStartIndex, NumRefs));7900 VTableFuncList VTableFuncs;7901 for (unsigned I = VTableListStartIndex, E = Record.size(); I != E; ++I) {7902 ValueInfo Callee = std::get<0>(getValueInfoFromValueId(Record[I]));7903 uint64_t Offset = Record[++I];7904 VTableFuncs.push_back({Callee, Offset});7905 }7906 auto VS =7907 std::make_unique<GlobalVarSummary>(Flags, GVF, std::move(Refs));7908 VS->setModulePath(getThisModule()->first());7909 VS->setVTableFuncs(VTableFuncs);7910 auto GUID = getValueInfoFromValueId(ValueID);7911 VS->setOriginalName(std::get<1>(GUID));7912 TheIndex.addGlobalValueSummary(std::get<0>(GUID), std::move(VS));7913 break;7914 }7915 // FS_COMBINED is legacy and does not have support for the tail call flag.7916 // FS_COMBINED: [valueid, modid, flags, instcount, fflags, numrefs,7917 // numrefs x valueid, n x (valueid)]7918 // FS_COMBINED_PROFILE: [valueid, modid, flags, instcount, fflags, numrefs,7919 // numrefs x valueid,7920 // n x (valueid, hotness+tailcall flags)]7921 case bitc::FS_COMBINED:7922 case bitc::FS_COMBINED_PROFILE: {7923 unsigned ValueID = Record[0];7924 uint64_t ModuleId = Record[1];7925 uint64_t RawFlags = Record[2];7926 unsigned InstCount = Record[3];7927 uint64_t RawFunFlags = 0;7928 unsigned NumRefs = Record[4];7929 unsigned NumRORefs = 0, NumWORefs = 0;7930 int RefListStartIndex = 5;7931 7932 if (Version >= 4) {7933 RawFunFlags = Record[4];7934 RefListStartIndex = 6;7935 size_t NumRefsIndex = 5;7936 if (Version >= 5) {7937 unsigned NumRORefsOffset = 1;7938 RefListStartIndex = 7;7939 if (Version >= 6) {7940 NumRefsIndex = 6;7941 RefListStartIndex = 8;7942 if (Version >= 7) {7943 RefListStartIndex = 9;7944 NumWORefs = Record[8];7945 NumRORefsOffset = 2;7946 }7947 }7948 NumRORefs = Record[RefListStartIndex - NumRORefsOffset];7949 }7950 NumRefs = Record[NumRefsIndex];7951 }7952 7953 auto Flags = getDecodedGVSummaryFlags(RawFlags, Version);7954 int CallGraphEdgeStartIndex = RefListStartIndex + NumRefs;7955 assert(Record.size() >= RefListStartIndex + NumRefs &&7956 "Record size inconsistent with number of references");7957 SmallVector<ValueInfo, 0> Refs = makeRefList(7958 ArrayRef<uint64_t>(Record).slice(RefListStartIndex, NumRefs));7959 bool HasProfile = (BitCode == bitc::FS_COMBINED_PROFILE);7960 SmallVector<FunctionSummary::EdgeTy, 0> Edges = makeCallList(7961 ArrayRef<uint64_t>(Record).slice(CallGraphEdgeStartIndex),7962 IsOldProfileFormat, HasProfile, false);7963 ValueInfo VI = std::get<0>(getValueInfoFromValueId(ValueID));7964 setSpecialRefs(Refs, NumRORefs, NumWORefs);7965 auto FS = std::make_unique<FunctionSummary>(7966 Flags, InstCount, getDecodedFFlags(RawFunFlags), std::move(Refs),7967 std::move(Edges), std::move(PendingTypeTests),7968 std::move(PendingTypeTestAssumeVCalls),7969 std::move(PendingTypeCheckedLoadVCalls),7970 std::move(PendingTypeTestAssumeConstVCalls),7971 std::move(PendingTypeCheckedLoadConstVCalls),7972 std::move(PendingParamAccesses), std::move(PendingCallsites),7973 std::move(PendingAllocs));7974 LastSeenSummary = FS.get();7975 LastSeenGUID = VI.getGUID();7976 FS->setModulePath(ModuleIdMap[ModuleId]);7977 TheIndex.addGlobalValueSummary(VI, std::move(FS));7978 break;7979 }7980 // FS_COMBINED_ALIAS: [valueid, modid, flags, valueid]7981 // Aliases must be emitted (and parsed) after all FS_COMBINED entries, as7982 // they expect all aliasee summaries to be available.7983 case bitc::FS_COMBINED_ALIAS: {7984 unsigned ValueID = Record[0];7985 uint64_t ModuleId = Record[1];7986 uint64_t RawFlags = Record[2];7987 unsigned AliaseeValueId = Record[3];7988 auto Flags = getDecodedGVSummaryFlags(RawFlags, Version);7989 auto AS = std::make_unique<AliasSummary>(Flags);7990 LastSeenSummary = AS.get();7991 AS->setModulePath(ModuleIdMap[ModuleId]);7992 7993 auto AliaseeVI = std::get<0>(getValueInfoFromValueId(AliaseeValueId));7994 auto AliaseeInModule = TheIndex.findSummaryInModule(AliaseeVI, AS->modulePath());7995 AS->setAliasee(AliaseeVI, AliaseeInModule);7996 7997 ValueInfo VI = std::get<0>(getValueInfoFromValueId(ValueID));7998 LastSeenGUID = VI.getGUID();7999 TheIndex.addGlobalValueSummary(VI, std::move(AS));8000 break;8001 }8002 // FS_COMBINED_GLOBALVAR_INIT_REFS: [valueid, modid, flags, n x valueid]8003 case bitc::FS_COMBINED_GLOBALVAR_INIT_REFS: {8004 unsigned ValueID = Record[0];8005 uint64_t ModuleId = Record[1];8006 uint64_t RawFlags = Record[2];8007 unsigned RefArrayStart = 3;8008 GlobalVarSummary::GVarFlags GVF(/* ReadOnly */ false,8009 /* WriteOnly */ false,8010 /* Constant */ false,8011 GlobalObject::VCallVisibilityPublic);8012 auto Flags = getDecodedGVSummaryFlags(RawFlags, Version);8013 if (Version >= 5) {8014 GVF = getDecodedGVarFlags(Record[3]);8015 RefArrayStart = 4;8016 }8017 SmallVector<ValueInfo, 0> Refs =8018 makeRefList(ArrayRef<uint64_t>(Record).slice(RefArrayStart));8019 auto FS =8020 std::make_unique<GlobalVarSummary>(Flags, GVF, std::move(Refs));8021 LastSeenSummary = FS.get();8022 FS->setModulePath(ModuleIdMap[ModuleId]);8023 ValueInfo VI = std::get<0>(getValueInfoFromValueId(ValueID));8024 LastSeenGUID = VI.getGUID();8025 TheIndex.addGlobalValueSummary(VI, std::move(FS));8026 break;8027 }8028 // FS_COMBINED_ORIGINAL_NAME: [original_name]8029 case bitc::FS_COMBINED_ORIGINAL_NAME: {8030 uint64_t OriginalName = Record[0];8031 if (!LastSeenSummary)8032 return error("Name attachment that does not follow a combined record");8033 LastSeenSummary->setOriginalName(OriginalName);8034 TheIndex.addOriginalName(LastSeenGUID, OriginalName);8035 // Reset the LastSeenSummary8036 LastSeenSummary = nullptr;8037 LastSeenGUID = 0;8038 break;8039 }8040 case bitc::FS_TYPE_TESTS:8041 assert(PendingTypeTests.empty());8042 llvm::append_range(PendingTypeTests, Record);8043 break;8044 8045 case bitc::FS_TYPE_TEST_ASSUME_VCALLS:8046 assert(PendingTypeTestAssumeVCalls.empty());8047 for (unsigned I = 0; I != Record.size(); I += 2)8048 PendingTypeTestAssumeVCalls.push_back({Record[I], Record[I+1]});8049 break;8050 8051 case bitc::FS_TYPE_CHECKED_LOAD_VCALLS:8052 assert(PendingTypeCheckedLoadVCalls.empty());8053 for (unsigned I = 0; I != Record.size(); I += 2)8054 PendingTypeCheckedLoadVCalls.push_back({Record[I], Record[I+1]});8055 break;8056 8057 case bitc::FS_TYPE_TEST_ASSUME_CONST_VCALL:8058 PendingTypeTestAssumeConstVCalls.push_back(8059 {{Record[0], Record[1]}, {Record.begin() + 2, Record.end()}});8060 break;8061 8062 case bitc::FS_TYPE_CHECKED_LOAD_CONST_VCALL:8063 PendingTypeCheckedLoadConstVCalls.push_back(8064 {{Record[0], Record[1]}, {Record.begin() + 2, Record.end()}});8065 break;8066 8067 case bitc::FS_CFI_FUNCTION_DEFS: {8068 auto &CfiFunctionDefs = TheIndex.cfiFunctionDefs();8069 for (unsigned I = 0; I != Record.size(); I += 2)8070 CfiFunctionDefs.emplace(Strtab.data() + Record[I],8071 static_cast<size_t>(Record[I + 1]));8072 break;8073 }8074 8075 case bitc::FS_CFI_FUNCTION_DECLS: {8076 auto &CfiFunctionDecls = TheIndex.cfiFunctionDecls();8077 for (unsigned I = 0; I != Record.size(); I += 2)8078 CfiFunctionDecls.emplace(Strtab.data() + Record[I],8079 static_cast<size_t>(Record[I + 1]));8080 break;8081 }8082 8083 case bitc::FS_TYPE_ID:8084 parseTypeIdSummaryRecord(Record, Strtab, TheIndex);8085 break;8086 8087 case bitc::FS_TYPE_ID_METADATA:8088 parseTypeIdCompatibleVtableSummaryRecord(Record);8089 break;8090 8091 case bitc::FS_BLOCK_COUNT:8092 TheIndex.addBlockCount(Record[0]);8093 break;8094 8095 case bitc::FS_PARAM_ACCESS: {8096 PendingParamAccesses = parseParamAccesses(Record);8097 break;8098 }8099 8100 case bitc::FS_STACK_IDS: { // [n x stackid]8101 // Save stack ids in the reader to consult when adding stack ids from the8102 // lists in the stack node and alloc node entries.8103 if (Version <= 11) {8104 StackIds = ArrayRef<uint64_t>(Record);8105 break;8106 }8107 // This is an array of 32-bit fixed-width values, holding each 64-bit8108 // context id as a pair of adjacent (most significant first) 32-bit words.8109 assert(Record.size() % 2 == 0);8110 StackIds.reserve(Record.size() / 2);8111 for (auto R = Record.begin(); R != Record.end(); R += 2)8112 StackIds.push_back(*R << 32 | *(R + 1));8113 break;8114 }8115 8116 case bitc::FS_CONTEXT_RADIX_TREE_ARRAY: { // [n x entry]8117 RadixArray = ArrayRef<uint64_t>(Record);8118 break;8119 }8120 8121 case bitc::FS_PERMODULE_CALLSITE_INFO: {8122 unsigned ValueID = Record[0];8123 SmallVector<unsigned> StackIdList;8124 for (uint64_t R : drop_begin(Record)) {8125 assert(R < StackIds.size());8126 StackIdList.push_back(TheIndex.addOrGetStackIdIndex(StackIds[R]));8127 }8128 ValueInfo VI = std::get<0>(getValueInfoFromValueId(ValueID));8129 PendingCallsites.push_back(CallsiteInfo({VI, std::move(StackIdList)}));8130 break;8131 }8132 8133 case bitc::FS_COMBINED_CALLSITE_INFO: {8134 auto RecordIter = Record.begin();8135 unsigned ValueID = *RecordIter++;8136 unsigned NumStackIds = *RecordIter++;8137 unsigned NumVersions = *RecordIter++;8138 assert(Record.size() == 3 + NumStackIds + NumVersions);8139 SmallVector<unsigned> StackIdList;8140 for (unsigned J = 0; J < NumStackIds; J++) {8141 assert(*RecordIter < StackIds.size());8142 StackIdList.push_back(8143 TheIndex.addOrGetStackIdIndex(StackIds[*RecordIter++]));8144 }8145 SmallVector<unsigned> Versions;8146 for (unsigned J = 0; J < NumVersions; J++)8147 Versions.push_back(*RecordIter++);8148 ValueInfo VI = std::get<0>(8149 getValueInfoFromValueId</*AllowNullValueInfo*/ true>(ValueID));8150 PendingCallsites.push_back(8151 CallsiteInfo({VI, std::move(Versions), std::move(StackIdList)}));8152 break;8153 }8154 8155 case bitc::FS_ALLOC_CONTEXT_IDS: {8156 // This is an array of 32-bit fixed-width values, holding each 64-bit8157 // context id as a pair of adjacent (most significant first) 32-bit words.8158 assert(Record.size() % 2 == 0);8159 PendingContextIds.reserve(Record.size() / 2);8160 for (auto R = Record.begin(); R != Record.end(); R += 2)8161 PendingContextIds.push_back(*R << 32 | *(R + 1));8162 break;8163 }8164 8165 case bitc::FS_PERMODULE_ALLOC_INFO: {8166 unsigned I = 0;8167 std::vector<MIBInfo> MIBs;8168 unsigned NumMIBs = 0;8169 if (Version >= 10)8170 NumMIBs = Record[I++];8171 unsigned MIBsRead = 0;8172 while ((Version >= 10 && MIBsRead++ < NumMIBs) ||8173 (Version < 10 && I < Record.size())) {8174 assert(Record.size() - I >= 2);8175 AllocationType AllocType = (AllocationType)Record[I++];8176 auto StackIdList = parseAllocInfoContext(Record, I);8177 MIBs.push_back(MIBInfo(AllocType, std::move(StackIdList)));8178 }8179 // We either have nothing left or at least NumMIBs context size info8180 // indices left (for the total sizes included when reporting of hinted8181 // bytes is enabled).8182 assert(I == Record.size() || Record.size() - I >= NumMIBs);8183 std::vector<std::vector<ContextTotalSize>> AllContextSizes;8184 if (I < Record.size()) {8185 assert(!PendingContextIds.empty() &&8186 "Missing context ids for alloc sizes");8187 unsigned ContextIdIndex = 0;8188 MIBsRead = 0;8189 // The sizes are a linearized array of sizes, where for each MIB there8190 // is 1 or more sizes (due to context trimming, each MIB in the metadata8191 // and summarized here can correspond to more than one original context8192 // from the profile).8193 while (MIBsRead++ < NumMIBs) {8194 // First read the number of contexts recorded for this MIB.8195 unsigned NumContextSizeInfoEntries = Record[I++];8196 assert(Record.size() - I >= NumContextSizeInfoEntries);8197 std::vector<ContextTotalSize> ContextSizes;8198 ContextSizes.reserve(NumContextSizeInfoEntries);8199 for (unsigned J = 0; J < NumContextSizeInfoEntries; J++) {8200 assert(ContextIdIndex < PendingContextIds.size());8201 // Skip any 0 entries for MIBs without the context size info.8202 if (PendingContextIds[ContextIdIndex] == 0) {8203 // The size should also be 0 if the context was 0.8204 assert(!Record[I]);8205 ContextIdIndex++;8206 I++;8207 continue;8208 }8209 // PendingContextIds read from the preceding FS_ALLOC_CONTEXT_IDS8210 // should be in the same order as the total sizes.8211 ContextSizes.push_back(8212 {PendingContextIds[ContextIdIndex++], Record[I++]});8213 }8214 AllContextSizes.push_back(std::move(ContextSizes));8215 }8216 PendingContextIds.clear();8217 }8218 PendingAllocs.push_back(AllocInfo(std::move(MIBs)));8219 if (!AllContextSizes.empty()) {8220 assert(PendingAllocs.back().MIBs.size() == AllContextSizes.size());8221 PendingAllocs.back().ContextSizeInfos = std::move(AllContextSizes);8222 }8223 break;8224 }8225 8226 case bitc::FS_COMBINED_ALLOC_INFO:8227 case bitc::FS_COMBINED_ALLOC_INFO_NO_CONTEXT: {8228 unsigned I = 0;8229 std::vector<MIBInfo> MIBs;8230 unsigned NumMIBs = Record[I++];8231 unsigned NumVersions = Record[I++];8232 unsigned MIBsRead = 0;8233 while (MIBsRead++ < NumMIBs) {8234 assert(Record.size() - I >= 2);8235 AllocationType AllocType = (AllocationType)Record[I++];8236 SmallVector<unsigned> StackIdList;8237 if (BitCode == bitc::FS_COMBINED_ALLOC_INFO)8238 StackIdList = parseAllocInfoContext(Record, I);8239 MIBs.push_back(MIBInfo(AllocType, std::move(StackIdList)));8240 }8241 assert(Record.size() - I >= NumVersions);8242 SmallVector<uint8_t> Versions;8243 for (unsigned J = 0; J < NumVersions; J++)8244 Versions.push_back(Record[I++]);8245 assert(I == Record.size());8246 PendingAllocs.push_back(AllocInfo(std::move(Versions), std::move(MIBs)));8247 break;8248 }8249 }8250 }8251 llvm_unreachable("Exit infinite loop");8252}8253 8254// Parse the module string table block into the Index.8255// This populates the ModulePathStringTable map in the index.8256Error ModuleSummaryIndexBitcodeReader::parseModuleStringTable() {8257 if (Error Err = Stream.EnterSubBlock(bitc::MODULE_STRTAB_BLOCK_ID))8258 return Err;8259 8260 SmallVector<uint64_t, 64> Record;8261 8262 SmallString<128> ModulePath;8263 ModuleSummaryIndex::ModuleInfo *LastSeenModule = nullptr;8264 8265 while (true) {8266 Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks();8267 if (!MaybeEntry)8268 return MaybeEntry.takeError();8269 BitstreamEntry Entry = MaybeEntry.get();8270 8271 switch (Entry.Kind) {8272 case BitstreamEntry::SubBlock: // Handled for us already.8273 case BitstreamEntry::Error:8274 return error("Malformed block");8275 case BitstreamEntry::EndBlock:8276 return Error::success();8277 case BitstreamEntry::Record:8278 // The interesting case.8279 break;8280 }8281 8282 Record.clear();8283 Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record);8284 if (!MaybeRecord)8285 return MaybeRecord.takeError();8286 switch (MaybeRecord.get()) {8287 default: // Default behavior: ignore.8288 break;8289 case bitc::MST_CODE_ENTRY: {8290 // MST_ENTRY: [modid, namechar x N]8291 uint64_t ModuleId = Record[0];8292 8293 if (convertToString(Record, 1, ModulePath))8294 return error("Invalid record");8295 8296 LastSeenModule = TheIndex.addModule(ModulePath);8297 ModuleIdMap[ModuleId] = LastSeenModule->first();8298 8299 ModulePath.clear();8300 break;8301 }8302 /// MST_CODE_HASH: [5*i32]8303 case bitc::MST_CODE_HASH: {8304 if (Record.size() != 5)8305 return error("Invalid hash length " + Twine(Record.size()).str());8306 if (!LastSeenModule)8307 return error("Invalid hash that does not follow a module path");8308 int Pos = 0;8309 for (auto &Val : Record) {8310 assert(!(Val >> 32) && "Unexpected high bits set");8311 LastSeenModule->second[Pos++] = Val;8312 }8313 // Reset LastSeenModule to avoid overriding the hash unexpectedly.8314 LastSeenModule = nullptr;8315 break;8316 }8317 }8318 }8319 llvm_unreachable("Exit infinite loop");8320}8321 8322namespace {8323 8324// FIXME: This class is only here to support the transition to llvm::Error. It8325// will be removed once this transition is complete. Clients should prefer to8326// deal with the Error value directly, rather than converting to error_code.8327class BitcodeErrorCategoryType : public std::error_category {8328 const char *name() const noexcept override {8329 return "llvm.bitcode";8330 }8331 8332 std::string message(int IE) const override {8333 BitcodeError E = static_cast<BitcodeError>(IE);8334 switch (E) {8335 case BitcodeError::CorruptedBitcode:8336 return "Corrupted bitcode";8337 }8338 llvm_unreachable("Unknown error type!");8339 }8340};8341 8342} // end anonymous namespace8343 8344const std::error_category &llvm::BitcodeErrorCategory() {8345 static BitcodeErrorCategoryType ErrorCategory;8346 return ErrorCategory;8347}8348 8349static Expected<StringRef> readBlobInRecord(BitstreamCursor &Stream,8350 unsigned Block, unsigned RecordID) {8351 if (Error Err = Stream.EnterSubBlock(Block))8352 return std::move(Err);8353 8354 StringRef Strtab;8355 while (true) {8356 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();8357 if (!MaybeEntry)8358 return MaybeEntry.takeError();8359 llvm::BitstreamEntry Entry = MaybeEntry.get();8360 8361 switch (Entry.Kind) {8362 case BitstreamEntry::EndBlock:8363 return Strtab;8364 8365 case BitstreamEntry::Error:8366 return error("Malformed block");8367 8368 case BitstreamEntry::SubBlock:8369 if (Error Err = Stream.SkipBlock())8370 return std::move(Err);8371 break;8372 8373 case BitstreamEntry::Record:8374 StringRef Blob;8375 SmallVector<uint64_t, 1> Record;8376 Expected<unsigned> MaybeRecord =8377 Stream.readRecord(Entry.ID, Record, &Blob);8378 if (!MaybeRecord)8379 return MaybeRecord.takeError();8380 if (MaybeRecord.get() == RecordID)8381 Strtab = Blob;8382 break;8383 }8384 }8385}8386 8387//===----------------------------------------------------------------------===//8388// External interface8389//===----------------------------------------------------------------------===//8390 8391Expected<std::vector<BitcodeModule>>8392llvm::getBitcodeModuleList(MemoryBufferRef Buffer) {8393 auto FOrErr = getBitcodeFileContents(Buffer);8394 if (!FOrErr)8395 return FOrErr.takeError();8396 return std::move(FOrErr->Mods);8397}8398 8399Expected<BitcodeFileContents>8400llvm::getBitcodeFileContents(MemoryBufferRef Buffer) {8401 Expected<BitstreamCursor> StreamOrErr = initStream(Buffer);8402 if (!StreamOrErr)8403 return StreamOrErr.takeError();8404 BitstreamCursor &Stream = *StreamOrErr;8405 8406 BitcodeFileContents F;8407 while (true) {8408 uint64_t BCBegin = Stream.getCurrentByteNo();8409 8410 // We may be consuming bitcode from a client that leaves garbage at the end8411 // of the bitcode stream (e.g. Apple's ar tool). If we are close enough to8412 // the end that there cannot possibly be another module, stop looking.8413 if (BCBegin + 8 >= Stream.getBitcodeBytes().size())8414 return F;8415 8416 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();8417 if (!MaybeEntry)8418 return MaybeEntry.takeError();8419 llvm::BitstreamEntry Entry = MaybeEntry.get();8420 8421 switch (Entry.Kind) {8422 case BitstreamEntry::EndBlock:8423 case BitstreamEntry::Error:8424 return error("Malformed block");8425 8426 case BitstreamEntry::SubBlock: {8427 uint64_t IdentificationBit = -1ull;8428 if (Entry.ID == bitc::IDENTIFICATION_BLOCK_ID) {8429 IdentificationBit = Stream.GetCurrentBitNo() - BCBegin * 8;8430 if (Error Err = Stream.SkipBlock())8431 return std::move(Err);8432 8433 {8434 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();8435 if (!MaybeEntry)8436 return MaybeEntry.takeError();8437 Entry = MaybeEntry.get();8438 }8439 8440 if (Entry.Kind != BitstreamEntry::SubBlock ||8441 Entry.ID != bitc::MODULE_BLOCK_ID)8442 return error("Malformed block");8443 }8444 8445 if (Entry.ID == bitc::MODULE_BLOCK_ID) {8446 uint64_t ModuleBit = Stream.GetCurrentBitNo() - BCBegin * 8;8447 if (Error Err = Stream.SkipBlock())8448 return std::move(Err);8449 8450 F.Mods.push_back({Stream.getBitcodeBytes().slice(8451 BCBegin, Stream.getCurrentByteNo() - BCBegin),8452 Buffer.getBufferIdentifier(), IdentificationBit,8453 ModuleBit});8454 continue;8455 }8456 8457 if (Entry.ID == bitc::STRTAB_BLOCK_ID) {8458 Expected<StringRef> Strtab =8459 readBlobInRecord(Stream, bitc::STRTAB_BLOCK_ID, bitc::STRTAB_BLOB);8460 if (!Strtab)8461 return Strtab.takeError();8462 // This string table is used by every preceding bitcode module that does8463 // not have its own string table. A bitcode file may have multiple8464 // string tables if it was created by binary concatenation, for example8465 // with "llvm-cat -b".8466 for (BitcodeModule &I : llvm::reverse(F.Mods)) {8467 if (!I.Strtab.empty())8468 break;8469 I.Strtab = *Strtab;8470 }8471 // Similarly, the string table is used by every preceding symbol table;8472 // normally there will be just one unless the bitcode file was created8473 // by binary concatenation.8474 if (!F.Symtab.empty() && F.StrtabForSymtab.empty())8475 F.StrtabForSymtab = *Strtab;8476 continue;8477 }8478 8479 if (Entry.ID == bitc::SYMTAB_BLOCK_ID) {8480 Expected<StringRef> SymtabOrErr =8481 readBlobInRecord(Stream, bitc::SYMTAB_BLOCK_ID, bitc::SYMTAB_BLOB);8482 if (!SymtabOrErr)8483 return SymtabOrErr.takeError();8484 8485 // We can expect the bitcode file to have multiple symbol tables if it8486 // was created by binary concatenation. In that case we silently8487 // ignore any subsequent symbol tables, which is fine because this is a8488 // low level function. The client is expected to notice that the number8489 // of modules in the symbol table does not match the number of modules8490 // in the input file and regenerate the symbol table.8491 if (F.Symtab.empty())8492 F.Symtab = *SymtabOrErr;8493 continue;8494 }8495 8496 if (Error Err = Stream.SkipBlock())8497 return std::move(Err);8498 continue;8499 }8500 case BitstreamEntry::Record:8501 if (Error E = Stream.skipRecord(Entry.ID).takeError())8502 return std::move(E);8503 continue;8504 }8505 }8506}8507 8508/// Get a lazy one-at-time loading module from bitcode.8509///8510/// This isn't always used in a lazy context. In particular, it's also used by8511/// \a parseModule(). If this is truly lazy, then we need to eagerly pull8512/// in forward-referenced functions from block address references.8513///8514/// \param[in] MaterializeAll Set to \c true if we should materialize8515/// everything.8516Expected<std::unique_ptr<Module>>8517BitcodeModule::getModuleImpl(LLVMContext &Context, bool MaterializeAll,8518 bool ShouldLazyLoadMetadata, bool IsImporting,8519 ParserCallbacks Callbacks) {8520 BitstreamCursor Stream(Buffer);8521 8522 std::string ProducerIdentification;8523 if (IdentificationBit != -1ull) {8524 if (Error JumpFailed = Stream.JumpToBit(IdentificationBit))8525 return std::move(JumpFailed);8526 if (Error E =8527 readIdentificationBlock(Stream).moveInto(ProducerIdentification))8528 return std::move(E);8529 }8530 8531 if (Error JumpFailed = Stream.JumpToBit(ModuleBit))8532 return std::move(JumpFailed);8533 auto *R = new BitcodeReader(std::move(Stream), Strtab, ProducerIdentification,8534 Context);8535 8536 std::unique_ptr<Module> M =8537 std::make_unique<Module>(ModuleIdentifier, Context);8538 M->setMaterializer(R);8539 8540 // Delay parsing Metadata if ShouldLazyLoadMetadata is true.8541 if (Error Err = R->parseBitcodeInto(M.get(), ShouldLazyLoadMetadata,8542 IsImporting, Callbacks))8543 return std::move(Err);8544 8545 if (MaterializeAll) {8546 // Read in the entire module, and destroy the BitcodeReader.8547 if (Error Err = M->materializeAll())8548 return std::move(Err);8549 } else {8550 // Resolve forward references from blockaddresses.8551 if (Error Err = R->materializeForwardReferencedFunctions())8552 return std::move(Err);8553 }8554 8555 return std::move(M);8556}8557 8558Expected<std::unique_ptr<Module>>8559BitcodeModule::getLazyModule(LLVMContext &Context, bool ShouldLazyLoadMetadata,8560 bool IsImporting, ParserCallbacks Callbacks) {8561 return getModuleImpl(Context, false, ShouldLazyLoadMetadata, IsImporting,8562 Callbacks);8563}8564 8565// Parse the specified bitcode buffer and merge the index into CombinedIndex.8566// We don't use ModuleIdentifier here because the client may need to control the8567// module path used in the combined summary (e.g. when reading summaries for8568// regular LTO modules).8569Error BitcodeModule::readSummary(8570 ModuleSummaryIndex &CombinedIndex, StringRef ModulePath,8571 std::function<bool(GlobalValue::GUID)> IsPrevailing) {8572 BitstreamCursor Stream(Buffer);8573 if (Error JumpFailed = Stream.JumpToBit(ModuleBit))8574 return JumpFailed;8575 8576 ModuleSummaryIndexBitcodeReader R(std::move(Stream), Strtab, CombinedIndex,8577 ModulePath, IsPrevailing);8578 return R.parseModule();8579}8580 8581// Parse the specified bitcode buffer, returning the function info index.8582Expected<std::unique_ptr<ModuleSummaryIndex>> BitcodeModule::getSummary() {8583 BitstreamCursor Stream(Buffer);8584 if (Error JumpFailed = Stream.JumpToBit(ModuleBit))8585 return std::move(JumpFailed);8586 8587 auto Index = std::make_unique<ModuleSummaryIndex>(/*HaveGVs=*/false);8588 ModuleSummaryIndexBitcodeReader R(std::move(Stream), Strtab, *Index,8589 ModuleIdentifier, 0);8590 8591 if (Error Err = R.parseModule())8592 return std::move(Err);8593 8594 return std::move(Index);8595}8596 8597static Expected<std::pair<bool, bool>>8598getEnableSplitLTOUnitAndUnifiedFlag(BitstreamCursor &Stream, unsigned ID) {8599 if (Error Err = Stream.EnterSubBlock(ID))8600 return std::move(Err);8601 8602 SmallVector<uint64_t, 64> Record;8603 while (true) {8604 BitstreamEntry Entry;8605 if (Error E = Stream.advanceSkippingSubblocks().moveInto(Entry))8606 return std::move(E);8607 8608 switch (Entry.Kind) {8609 case BitstreamEntry::SubBlock: // Handled for us already.8610 case BitstreamEntry::Error:8611 return error("Malformed block");8612 case BitstreamEntry::EndBlock: {8613 // If no flags record found, return both flags as false.8614 return std::make_pair(false, false);8615 }8616 case BitstreamEntry::Record:8617 // The interesting case.8618 break;8619 }8620 8621 // Look for the FS_FLAGS record.8622 Record.clear();8623 Expected<unsigned> MaybeBitCode = Stream.readRecord(Entry.ID, Record);8624 if (!MaybeBitCode)8625 return MaybeBitCode.takeError();8626 switch (MaybeBitCode.get()) {8627 default: // Default behavior: ignore.8628 break;8629 case bitc::FS_FLAGS: { // [flags]8630 uint64_t Flags = Record[0];8631 // Scan flags.8632 assert(Flags <= 0x7ff && "Unexpected bits in flag");8633 8634 bool EnableSplitLTOUnit = Flags & 0x8;8635 bool UnifiedLTO = Flags & 0x200;8636 return std::make_pair(EnableSplitLTOUnit, UnifiedLTO);8637 }8638 }8639 }8640 llvm_unreachable("Exit infinite loop");8641}8642 8643// Check if the given bitcode buffer contains a global value summary block.8644Expected<BitcodeLTOInfo> BitcodeModule::getLTOInfo() {8645 BitstreamCursor Stream(Buffer);8646 if (Error JumpFailed = Stream.JumpToBit(ModuleBit))8647 return std::move(JumpFailed);8648 8649 if (Error Err = Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID))8650 return std::move(Err);8651 8652 while (true) {8653 llvm::BitstreamEntry Entry;8654 if (Error E = Stream.advance().moveInto(Entry))8655 return std::move(E);8656 8657 switch (Entry.Kind) {8658 case BitstreamEntry::Error:8659 return error("Malformed block");8660 case BitstreamEntry::EndBlock:8661 return BitcodeLTOInfo{/*IsThinLTO=*/false, /*HasSummary=*/false,8662 /*EnableSplitLTOUnit=*/false, /*UnifiedLTO=*/false};8663 8664 case BitstreamEntry::SubBlock:8665 if (Entry.ID == bitc::GLOBALVAL_SUMMARY_BLOCK_ID ||8666 Entry.ID == bitc::FULL_LTO_GLOBALVAL_SUMMARY_BLOCK_ID) {8667 Expected<std::pair<bool, bool>> Flags =8668 getEnableSplitLTOUnitAndUnifiedFlag(Stream, Entry.ID);8669 if (!Flags)8670 return Flags.takeError();8671 BitcodeLTOInfo LTOInfo;8672 std::tie(LTOInfo.EnableSplitLTOUnit, LTOInfo.UnifiedLTO) = Flags.get();8673 LTOInfo.IsThinLTO = (Entry.ID == bitc::GLOBALVAL_SUMMARY_BLOCK_ID);8674 LTOInfo.HasSummary = true;8675 return LTOInfo;8676 }8677 8678 // Ignore other sub-blocks.8679 if (Error Err = Stream.SkipBlock())8680 return std::move(Err);8681 continue;8682 8683 case BitstreamEntry::Record:8684 if (Expected<unsigned> StreamFailed = Stream.skipRecord(Entry.ID))8685 continue;8686 else8687 return StreamFailed.takeError();8688 }8689 }8690}8691 8692static Expected<BitcodeModule> getSingleModule(MemoryBufferRef Buffer) {8693 Expected<std::vector<BitcodeModule>> MsOrErr = getBitcodeModuleList(Buffer);8694 if (!MsOrErr)8695 return MsOrErr.takeError();8696 8697 if (MsOrErr->size() != 1)8698 return error("Expected a single module");8699 8700 return (*MsOrErr)[0];8701}8702 8703Expected<std::unique_ptr<Module>>8704llvm::getLazyBitcodeModule(MemoryBufferRef Buffer, LLVMContext &Context,8705 bool ShouldLazyLoadMetadata, bool IsImporting,8706 ParserCallbacks Callbacks) {8707 Expected<BitcodeModule> BM = getSingleModule(Buffer);8708 if (!BM)8709 return BM.takeError();8710 8711 return BM->getLazyModule(Context, ShouldLazyLoadMetadata, IsImporting,8712 Callbacks);8713}8714 8715Expected<std::unique_ptr<Module>> llvm::getOwningLazyBitcodeModule(8716 std::unique_ptr<MemoryBuffer> &&Buffer, LLVMContext &Context,8717 bool ShouldLazyLoadMetadata, bool IsImporting, ParserCallbacks Callbacks) {8718 auto MOrErr = getLazyBitcodeModule(*Buffer, Context, ShouldLazyLoadMetadata,8719 IsImporting, Callbacks);8720 if (MOrErr)8721 (*MOrErr)->setOwnedMemoryBuffer(std::move(Buffer));8722 return MOrErr;8723}8724 8725Expected<std::unique_ptr<Module>>8726BitcodeModule::parseModule(LLVMContext &Context, ParserCallbacks Callbacks) {8727 return getModuleImpl(Context, true, false, false, Callbacks);8728 // TODO: Restore the use-lists to the in-memory state when the bitcode was8729 // written. We must defer until the Module has been fully materialized.8730}8731 8732Expected<std::unique_ptr<Module>>8733llvm::parseBitcodeFile(MemoryBufferRef Buffer, LLVMContext &Context,8734 ParserCallbacks Callbacks) {8735 Expected<BitcodeModule> BM = getSingleModule(Buffer);8736 if (!BM)8737 return BM.takeError();8738 8739 return BM->parseModule(Context, Callbacks);8740}8741 8742Expected<std::string> llvm::getBitcodeTargetTriple(MemoryBufferRef Buffer) {8743 Expected<BitstreamCursor> StreamOrErr = initStream(Buffer);8744 if (!StreamOrErr)8745 return StreamOrErr.takeError();8746 8747 return readTriple(*StreamOrErr);8748}8749 8750Expected<bool> llvm::isBitcodeContainingObjCCategory(MemoryBufferRef Buffer) {8751 Expected<BitstreamCursor> StreamOrErr = initStream(Buffer);8752 if (!StreamOrErr)8753 return StreamOrErr.takeError();8754 8755 return hasObjCCategory(*StreamOrErr);8756}8757 8758Expected<std::string> llvm::getBitcodeProducerString(MemoryBufferRef Buffer) {8759 Expected<BitstreamCursor> StreamOrErr = initStream(Buffer);8760 if (!StreamOrErr)8761 return StreamOrErr.takeError();8762 8763 return readIdentificationCode(*StreamOrErr);8764}8765 8766Error llvm::readModuleSummaryIndex(MemoryBufferRef Buffer,8767 ModuleSummaryIndex &CombinedIndex) {8768 Expected<BitcodeModule> BM = getSingleModule(Buffer);8769 if (!BM)8770 return BM.takeError();8771 8772 return BM->readSummary(CombinedIndex, BM->getModuleIdentifier());8773}8774 8775Expected<std::unique_ptr<ModuleSummaryIndex>>8776llvm::getModuleSummaryIndex(MemoryBufferRef Buffer) {8777 Expected<BitcodeModule> BM = getSingleModule(Buffer);8778 if (!BM)8779 return BM.takeError();8780 8781 return BM->getSummary();8782}8783 8784Expected<BitcodeLTOInfo> llvm::getBitcodeLTOInfo(MemoryBufferRef Buffer) {8785 Expected<BitcodeModule> BM = getSingleModule(Buffer);8786 if (!BM)8787 return BM.takeError();8788 8789 return BM->getLTOInfo();8790}8791 8792Expected<std::unique_ptr<ModuleSummaryIndex>>8793llvm::getModuleSummaryIndexForFile(StringRef Path,8794 bool IgnoreEmptyThinLTOIndexFile) {8795 ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =8796 MemoryBuffer::getFileOrSTDIN(Path);8797 if (!FileOrErr)8798 return errorCodeToError(FileOrErr.getError());8799 if (IgnoreEmptyThinLTOIndexFile && !(*FileOrErr)->getBufferSize())8800 return nullptr;8801 return getModuleSummaryIndex(**FileOrErr);8802}8803