diff options
Diffstat (limited to 'contrib/llvm/lib/Bitcode/Reader/BitcodeReader.cpp')
-rw-r--r-- | contrib/llvm/lib/Bitcode/Reader/BitcodeReader.cpp | 177 |
1 files changed, 150 insertions, 27 deletions
diff --git a/contrib/llvm/lib/Bitcode/Reader/BitcodeReader.cpp b/contrib/llvm/lib/Bitcode/Reader/BitcodeReader.cpp index 945ac4515368..c45b441238bc 100644 --- a/contrib/llvm/lib/Bitcode/Reader/BitcodeReader.cpp +++ b/contrib/llvm/lib/Bitcode/Reader/BitcodeReader.cpp @@ -23,6 +23,7 @@ #include "llvm/ADT/Twine.h" #include "llvm/Bitcode/BitstreamReader.h" #include "llvm/Bitcode/LLVMBitCodes.h" +#include "llvm/Config/llvm-config.h" #include "llvm/IR/Argument.h" #include "llvm/IR/Attributes.h" #include "llvm/IR/AutoUpgrade.h" @@ -532,7 +533,7 @@ public: Error materializeModule() override; std::vector<StructType *> getIdentifiedStructTypes() const override; - /// \brief Main interface to parsing a bitcode buffer. + /// Main interface to parsing a bitcode buffer. /// \returns true if an error occurred. Error parseBitcodeInto(Module *M, bool ShouldLazyLoadMetadata = false, bool IsImporting = false); @@ -743,14 +744,16 @@ private: std::vector<ValueInfo> makeRefList(ArrayRef<uint64_t> Record); std::vector<FunctionSummary::EdgeTy> makeCallList(ArrayRef<uint64_t> Record, bool IsOldProfileFormat, - bool HasProfile); + bool HasProfile, + bool HasRelBF); Error parseEntireSummary(unsigned ID); Error parseModuleStringTable(); std::pair<ValueInfo, GlobalValue::GUID> getValueInfoFromValueId(unsigned ValueId); - ModuleSummaryIndex::ModuleInfo *addThisModule(); + void addThisModule(); + ModuleSummaryIndex::ModuleInfo *getThisModule(); }; } // end anonymous namespace @@ -1159,6 +1162,9 @@ static uint64_t getRawAttributeMask(Attribute::AttrKind Val) { case Attribute::Speculatable: return 1ULL << 54; case Attribute::StrictFP: return 1ULL << 55; case Attribute::SanitizeHWAddress: return 1ULL << 56; + case Attribute::NoCfCheck: return 1ULL << 57; + case Attribute::OptForFuzzing: return 1ULL << 58; + case Attribute::ShadowCallStack: return 1ULL << 59; case Attribute::Dereferenceable: llvm_unreachable("dereferenceable attribute not supported in raw format"); break; @@ -1197,7 +1203,7 @@ static void addRawAttributeValue(AttrBuilder &B, uint64_t Val) { } } -/// \brief This fills an AttrBuilder object with the LLVM attributes that have +/// This fills an AttrBuilder object with the LLVM attributes that have /// been decoded from the given integer. This function must stay in sync with /// 'encodeLLVMAttributesForBitcode'. static void decodeLLVMAttributesForBitcode(AttrBuilder &B, @@ -1337,8 +1343,12 @@ static Attribute::AttrKind getAttrFromCode(uint64_t Code) { return Attribute::NoRedZone; case bitc::ATTR_KIND_NO_RETURN: return Attribute::NoReturn; + case bitc::ATTR_KIND_NOCF_CHECK: + return Attribute::NoCfCheck; case bitc::ATTR_KIND_NO_UNWIND: return Attribute::NoUnwind; + case bitc::ATTR_KIND_OPT_FOR_FUZZING: + return Attribute::OptForFuzzing; case bitc::ATTR_KIND_OPTIMIZE_FOR_SIZE: return Attribute::OptimizeForSize; case bitc::ATTR_KIND_OPTIMIZE_NONE: @@ -1365,6 +1375,8 @@ static Attribute::AttrKind getAttrFromCode(uint64_t Code) { return Attribute::StackProtectStrong; case bitc::ATTR_KIND_SAFESTACK: return Attribute::SafeStack; + case bitc::ATTR_KIND_SHADOWCALLSTACK: + return Attribute::ShadowCallStack; case bitc::ATTR_KIND_STRICT_FP: return Attribute::StrictFP; case bitc::ATTR_KIND_STRUCT_RET: @@ -2512,6 +2524,7 @@ Error BitcodeReader::parseConstants() { for (unsigned i = 0; i != ConstStrSize; ++i) ConstrStr += (char)Record[3+AsmStrSize+i]; PointerType *PTy = cast<PointerType>(CurTy); + UpgradeInlineAsmString(&AsmStr); V = InlineAsm::get(cast<FunctionType>(PTy->getElementType()), AsmStr, ConstrStr, HasSideEffects, IsAlignStack); break; @@ -2537,6 +2550,7 @@ Error BitcodeReader::parseConstants() { for (unsigned i = 0; i != ConstStrSize; ++i) ConstrStr += (char)Record[3+AsmStrSize+i]; PointerType *PTy = cast<PointerType>(CurTy); + UpgradeInlineAsmString(&AsmStr); V = InlineAsm::get(cast<FunctionType>(PTy->getElementType()), AsmStr, ConstrStr, HasSideEffects, IsAlignStack, InlineAsm::AsmDialect(AsmDialect)); @@ -2817,6 +2831,13 @@ Error BitcodeReader::parseComdatRecord(ArrayRef<uint64_t> Record) { return Error::success(); } +static void inferDSOLocal(GlobalValue *GV) { + // infer dso_local from linkage and visibility if it is not encoded. + if (GV->hasLocalLinkage() || + (!GV->hasDefaultVisibility() && !GV->hasExternalWeakLinkage())) + GV->setDSOLocal(true); +} + Error BitcodeReader::parseGlobalVarRecord(ArrayRef<uint64_t> Record) { // v1: [pointer type, isconst, initid, linkage, alignment, section, // visibility, threadlocal, unnamed_addr, externally_initialized, @@ -2909,6 +2930,7 @@ Error BitcodeReader::parseGlobalVarRecord(ArrayRef<uint64_t> Record) { if (Record.size() > 13) { NewGV->setDSOLocal(getDecodedDSOLocal(Record[13])); } + inferDSOLocal(NewGV); return Error::success(); } @@ -2993,6 +3015,7 @@ Error BitcodeReader::parseFunctionRecord(ArrayRef<uint64_t> Record) { if (Record.size() > 15) { Func->setDSOLocal(getDecodedDSOLocal(Record[15])); } + inferDSOLocal(Func); ValueList.push_back(Func); @@ -3056,16 +3079,21 @@ Error BitcodeReader::parseGlobalIndirectSymbolRecord( // FIXME: Change to an error if non-default in 4.0. NewGA->setVisibility(getDecodedVisibility(Record[VisInd])); } - if (OpNum != Record.size()) - NewGA->setDLLStorageClass(getDecodedDLLStorageClass(Record[OpNum++])); - else - upgradeDLLImportExportLinkage(NewGA, Linkage); - if (OpNum != Record.size()) - NewGA->setThreadLocalMode(getDecodedThreadLocalMode(Record[OpNum++])); - if (OpNum != Record.size()) - NewGA->setUnnamedAddr(getDecodedUnnamedAddrType(Record[OpNum++])); + if (BitCode == bitc::MODULE_CODE_ALIAS || + BitCode == bitc::MODULE_CODE_ALIAS_OLD) { + if (OpNum != Record.size()) + NewGA->setDLLStorageClass(getDecodedDLLStorageClass(Record[OpNum++])); + else + upgradeDLLImportExportLinkage(NewGA, Linkage); + if (OpNum != Record.size()) + NewGA->setThreadLocalMode(getDecodedThreadLocalMode(Record[OpNum++])); + if (OpNum != Record.size()) + NewGA->setUnnamedAddr(getDecodedUnnamedAddrType(Record[OpNum++])); + } if (OpNum != Record.size()) NewGA->setDSOLocal(getDecodedDSOLocal(Record[OpNum++])); + inferDSOLocal(NewGA); + ValueList.push_back(NewGA); IndirectSymbolInits.push_back(std::make_pair(NewGA, Val)); return Error::success(); @@ -4775,6 +4803,9 @@ Error BitcodeReader::materializeModule() { UpgradeDebugInfo(*TheModule); UpgradeModuleFlags(*TheModule); + + UpgradeRetainReleaseMarker(*TheModule); + return Error::success(); } @@ -4788,9 +4819,13 @@ ModuleSummaryIndexBitcodeReader::ModuleSummaryIndexBitcodeReader( : BitcodeReaderBase(std::move(Cursor), Strtab), TheIndex(TheIndex), ModulePath(ModulePath), ModuleId(ModuleId) {} +void ModuleSummaryIndexBitcodeReader::addThisModule() { + TheIndex.addModule(ModulePath, ModuleId); +} + ModuleSummaryIndex::ModuleInfo * -ModuleSummaryIndexBitcodeReader::addThisModule() { - return TheIndex.addModule(ModulePath, ModuleId); +ModuleSummaryIndexBitcodeReader::getThisModule() { + return TheIndex.getModule(ModulePath); } std::pair<ValueInfo, GlobalValue::GUID> @@ -4812,8 +4847,15 @@ void ModuleSummaryIndexBitcodeReader::setValueGUID( if (PrintSummaryGUIDs) dbgs() << "GUID " << ValueGUID << "(" << OriginalNameID << ") is " << ValueName << "\n"; - ValueIdToValueInfoMap[ValueID] = - std::make_pair(TheIndex.getOrInsertValueInfo(ValueGUID), OriginalNameID); + + // UseStrtab is false for legacy summary formats and value names are + // created on stack. In that case we save the name in a string saver in + // the index so that the value name can be recorded. + ValueIdToValueInfoMap[ValueID] = std::make_pair( + TheIndex.getOrInsertValueInfo( + ValueGUID, + UseStrtab ? ValueName : TheIndex.saveString(ValueName.str())), + OriginalNameID); } // Specialized value symbol table parser used when reading module index @@ -4942,6 +4984,9 @@ Error ModuleSummaryIndexBitcodeReader::parseModule() { break; case bitc::GLOBALVAL_SUMMARY_BLOCK_ID: case bitc::FULL_LTO_GLOBALVAL_SUMMARY_BLOCK_ID: + // Add the module if it is a per-module index (has a source file name). + if (!SourceFileName.empty()) + addThisModule(); assert(!SeenValueSymbolTable && "Already read VST when parsing summary block?"); // We might not have a VST if there were no values in the @@ -4987,7 +5032,7 @@ Error ModuleSummaryIndexBitcodeReader::parseModule() { case bitc::MODULE_CODE_HASH: { if (Record.size() != 5) return error("Invalid hash length " + Twine(Record.size()).str()); - auto &Hash = addThisModule()->second.second; + auto &Hash = getThisModule()->second.second; int Pos = 0; for (auto &Val : Record) { assert(!(Val >> 32) && "Unexpected high bits set"); @@ -5042,12 +5087,15 @@ ModuleSummaryIndexBitcodeReader::makeRefList(ArrayRef<uint64_t> Record) { return Ret; } -std::vector<FunctionSummary::EdgeTy> ModuleSummaryIndexBitcodeReader::makeCallList( - ArrayRef<uint64_t> Record, bool IsOldProfileFormat, bool HasProfile) { +std::vector<FunctionSummary::EdgeTy> +ModuleSummaryIndexBitcodeReader::makeCallList(ArrayRef<uint64_t> Record, + bool IsOldProfileFormat, + bool HasProfile, bool HasRelBF) { std::vector<FunctionSummary::EdgeTy> Ret; Ret.reserve(Record.size()); for (unsigned I = 0, E = Record.size(); I != E; ++I) { CalleeInfo::HotnessType Hotness = CalleeInfo::HotnessType::Unknown; + uint64_t RelBF = 0; ValueInfo Callee = getValueInfoFromValueId(Record[I]).first; if (IsOldProfileFormat) { I += 1; // Skip old callsitecount field @@ -5055,11 +5103,63 @@ std::vector<FunctionSummary::EdgeTy> ModuleSummaryIndexBitcodeReader::makeCallLi I += 1; // Skip old profilecount field } else if (HasProfile) Hotness = static_cast<CalleeInfo::HotnessType>(Record[++I]); - Ret.push_back(FunctionSummary::EdgeTy{Callee, CalleeInfo{Hotness}}); + else if (HasRelBF) + RelBF = Record[++I]; + Ret.push_back(FunctionSummary::EdgeTy{Callee, CalleeInfo(Hotness, RelBF)}); } return Ret; } +static void +parseWholeProgramDevirtResolutionByArg(ArrayRef<uint64_t> Record, size_t &Slot, + WholeProgramDevirtResolution &Wpd) { + uint64_t ArgNum = Record[Slot++]; + WholeProgramDevirtResolution::ByArg &B = + Wpd.ResByArg[{Record.begin() + Slot, Record.begin() + Slot + ArgNum}]; + Slot += ArgNum; + + B.TheKind = + static_cast<WholeProgramDevirtResolution::ByArg::Kind>(Record[Slot++]); + B.Info = Record[Slot++]; + B.Byte = Record[Slot++]; + B.Bit = Record[Slot++]; +} + +static void parseWholeProgramDevirtResolution(ArrayRef<uint64_t> Record, + StringRef Strtab, size_t &Slot, + TypeIdSummary &TypeId) { + uint64_t Id = Record[Slot++]; + WholeProgramDevirtResolution &Wpd = TypeId.WPDRes[Id]; + + Wpd.TheKind = static_cast<WholeProgramDevirtResolution::Kind>(Record[Slot++]); + Wpd.SingleImplName = {Strtab.data() + Record[Slot], + static_cast<size_t>(Record[Slot + 1])}; + Slot += 2; + + uint64_t ResByArgNum = Record[Slot++]; + for (uint64_t I = 0; I != ResByArgNum; ++I) + parseWholeProgramDevirtResolutionByArg(Record, Slot, Wpd); +} + +static void parseTypeIdSummaryRecord(ArrayRef<uint64_t> Record, + StringRef Strtab, + ModuleSummaryIndex &TheIndex) { + size_t Slot = 0; + TypeIdSummary &TypeId = TheIndex.getOrInsertTypeIdSummary( + {Strtab.data() + Record[Slot], static_cast<size_t>(Record[Slot + 1])}); + Slot += 2; + + TypeId.TTRes.TheKind = static_cast<TypeTestResolution::Kind>(Record[Slot++]); + TypeId.TTRes.SizeM1BitWidth = Record[Slot++]; + TypeId.TTRes.AlignLog2 = Record[Slot++]; + TypeId.TTRes.SizeM1 = Record[Slot++]; + TypeId.TTRes.BitMask = Record[Slot++]; + TypeId.TTRes.InlineBits = Record[Slot++]; + + while (Slot < Record.size()) + parseWholeProgramDevirtResolution(Record, Strtab, Slot, TypeId); +} + // Eagerly parse the entire summary block. This populates the GlobalValueSummary // objects in the index. Error ModuleSummaryIndexBitcodeReader::parseEntireSummary(unsigned ID) { @@ -5122,6 +5222,19 @@ Error ModuleSummaryIndexBitcodeReader::parseEntireSummary(unsigned ID) { switch (BitCode) { default: // Default behavior: ignore. break; + case bitc::FS_FLAGS: { // [flags] + uint64_t Flags = Record[0]; + // Scan flags (set only on the combined index). + assert(Flags <= 0x3 && "Unexpected bits in flag"); + + // 1 bit: WithGlobalValueDeadStripping flag. + if (Flags & 0x1) + TheIndex.setWithGlobalValueDeadStripping(); + // 1 bit: SkipModuleByDistributedBackend flag. + if (Flags & 0x2) + TheIndex.setSkipModuleByDistributedBackend(); + break; + } case bitc::FS_VALUE_GUID: { // [valueid, refguid] uint64_t ValueID = Record[0]; GlobalValue::GUID RefGUID = Record[1]; @@ -5134,7 +5247,11 @@ Error ModuleSummaryIndexBitcodeReader::parseEntireSummary(unsigned ID) { // FS_PERMODULE_PROFILE: [valueid, flags, instcount, fflags, numrefs, // numrefs x valueid, // n x (valueid, hotness)] + // FS_PERMODULE_RELBF: [valueid, flags, instcount, fflags, numrefs, + // numrefs x valueid, + // n x (valueid, relblockfreq)] case bitc::FS_PERMODULE: + case bitc::FS_PERMODULE_RELBF: case bitc::FS_PERMODULE_PROFILE: { unsigned ValueID = Record[0]; uint64_t RawFlags = Record[1]; @@ -5160,9 +5277,10 @@ Error ModuleSummaryIndexBitcodeReader::parseEntireSummary(unsigned ID) { std::vector<ValueInfo> Refs = makeRefList( ArrayRef<uint64_t>(Record).slice(RefListStartIndex, NumRefs)); bool HasProfile = (BitCode == bitc::FS_PERMODULE_PROFILE); + bool HasRelBF = (BitCode == bitc::FS_PERMODULE_RELBF); std::vector<FunctionSummary::EdgeTy> Calls = makeCallList( ArrayRef<uint64_t>(Record).slice(CallGraphEdgeStartIndex), - IsOldProfileFormat, HasProfile); + IsOldProfileFormat, HasProfile, HasRelBF); auto FS = llvm::make_unique<FunctionSummary>( Flags, InstCount, getDecodedFFlags(RawFunFlags), std::move(Refs), std::move(Calls), std::move(PendingTypeTests), @@ -5176,7 +5294,7 @@ Error ModuleSummaryIndexBitcodeReader::parseEntireSummary(unsigned ID) { PendingTypeTestAssumeConstVCalls.clear(); PendingTypeCheckedLoadConstVCalls.clear(); auto VIAndOriginalGUID = getValueInfoFromValueId(ValueID); - FS->setModulePath(addThisModule()->first()); + FS->setModulePath(getThisModule()->first()); FS->setOriginalName(VIAndOriginalGUID.second); TheIndex.addGlobalValueSummary(VIAndOriginalGUID.first, std::move(FS)); break; @@ -5195,7 +5313,7 @@ Error ModuleSummaryIndexBitcodeReader::parseEntireSummary(unsigned ID) { // string table section in the per-module index, we create a single // module path string table entry with an empty (0) ID to take // ownership. - AS->setModulePath(addThisModule()->first()); + AS->setModulePath(getThisModule()->first()); GlobalValue::GUID AliaseeGUID = getValueInfoFromValueId(AliaseeID).first.getGUID(); @@ -5219,7 +5337,7 @@ Error ModuleSummaryIndexBitcodeReader::parseEntireSummary(unsigned ID) { std::vector<ValueInfo> Refs = makeRefList(ArrayRef<uint64_t>(Record).slice(2)); auto FS = llvm::make_unique<GlobalVarSummary>(Flags, std::move(Refs)); - FS->setModulePath(addThisModule()->first()); + FS->setModulePath(getThisModule()->first()); auto GUID = getValueInfoFromValueId(ValueID); FS->setOriginalName(GUID.second); TheIndex.addGlobalValueSummary(GUID.first, std::move(FS)); @@ -5254,7 +5372,7 @@ Error ModuleSummaryIndexBitcodeReader::parseEntireSummary(unsigned ID) { bool HasProfile = (BitCode == bitc::FS_COMBINED_PROFILE); std::vector<FunctionSummary::EdgeTy> Edges = makeCallList( ArrayRef<uint64_t>(Record).slice(CallGraphEdgeStartIndex), - IsOldProfileFormat, HasProfile); + IsOldProfileFormat, HasProfile, false); ValueInfo VI = getValueInfoFromValueId(ValueID).first; auto FS = llvm::make_unique<FunctionSummary>( Flags, InstCount, getDecodedFFlags(RawFunFlags), std::move(Refs), @@ -5362,6 +5480,7 @@ Error ModuleSummaryIndexBitcodeReader::parseEntireSummary(unsigned ID) { {Strtab.data() + Record[I], static_cast<size_t>(Record[I + 1])}); break; } + case bitc::FS_CFI_FUNCTION_DECLS: { std::set<std::string> &CfiFunctionDecls = TheIndex.cfiFunctionDecls(); for (unsigned I = 0; I != Record.size(); I += 2) @@ -5369,6 +5488,10 @@ Error ModuleSummaryIndexBitcodeReader::parseEntireSummary(unsigned ID) { {Strtab.data() + Record[I], static_cast<size_t>(Record[I + 1])}); break; } + + case bitc::FS_TYPE_ID: + parseTypeIdSummaryRecord(Record, Strtab, TheIndex); + break; } } llvm_unreachable("Exit infinite loop"); @@ -5604,7 +5727,7 @@ llvm::getBitcodeFileContents(MemoryBufferRef Buffer) { } } -/// \brief Get a lazy one-at-time loading module from bitcode. +/// Get a lazy one-at-time loading module from bitcode. /// /// This isn't always used in a lazy context. In particular, it's also used by /// \a parseModule(). If this is truly lazy, then we need to eagerly pull @@ -5678,7 +5801,7 @@ Expected<std::unique_ptr<ModuleSummaryIndex>> BitcodeModule::getSummary() { BitstreamCursor Stream(Buffer); Stream.JumpToBit(ModuleBit); - auto Index = llvm::make_unique<ModuleSummaryIndex>(); + auto Index = llvm::make_unique<ModuleSummaryIndex>(/*HaveGVs=*/false); ModuleSummaryIndexBitcodeReader R(std::move(Stream), Strtab, *Index, ModuleIdentifier, 0); |