Index: include/llvm/IR/ModuleSummaryIndex.h =================================================================== --- include/llvm/IR/ModuleSummaryIndex.h +++ include/llvm/IR/ModuleSummaryIndex.h @@ -25,6 +25,7 @@ #include "llvm/ADT/StringRef.h" #include "llvm/IR/GlobalValue.h" #include "llvm/IR/Module.h" +#include "llvm/Transforms/IPO/FunctionAttrs.h" #include #include #include @@ -56,8 +57,13 @@ }; HotnessType Hotness = HotnessType::Unknown; + // Tracks the memory access for a callee in a function. Used to propagate + // readonly/readnone/write flags to caller. + MemoryAccessKind MemoryAccess = MemoryAccessKind::MAK_MayWrite; + CalleeInfo() = default; - explicit CalleeInfo(HotnessType Hotness) : Hotness(Hotness) {} + explicit CalleeInfo(HotnessType Hotness, MemoryAccessKind A) + : Hotness(Hotness), MemoryAccess(A) {} void updateHotness(const HotnessType OtherHotness) { Hotness = std::max(Hotness, OtherHotness); @@ -193,6 +199,7 @@ /// Returns the hash of the original name, it is identical to the GUID for /// externally visible symbols, but not for local ones. GlobalValue::GUID getOriginalName() { return OriginalName; } + GlobalValue::GUID getOriginalName() const { return OriginalName; } /// Initialize the original name hash in this summary. void setOriginalName(GlobalValue::GUID Name) { OriginalName = Name; } @@ -287,11 +294,22 @@ std::vector Args; }; + /// Function attribute flags. Used to track if a function accesses memory, + /// recurses or aliases. + struct FFlags { + unsigned ReadNone : 1; + unsigned ReadOnly : 1; + unsigned NoRecurse : 1; + unsigned NoAlias : 1; + }; + private: /// Number of instructions (ignoring debug instructions, e.g.) computed /// during the initial compile step when the summary index is first built. unsigned InstCount; + FFlags FunFlags; + /// List of call edge pairs from this function. std::vector CallGraphEdgeList; @@ -317,15 +335,16 @@ std::unique_ptr TIdInfo; public: - FunctionSummary(GVFlags Flags, unsigned NumInsts, std::vector Refs, - std::vector CGEdges, + FunctionSummary(GVFlags Flags, unsigned NumInsts, FFlags FunFlags, + std::vector Refs, std::vector CGEdges, std::vector TypeTests, std::vector TypeTestAssumeVCalls, std::vector TypeCheckedLoadVCalls, std::vector TypeTestAssumeConstVCalls, std::vector TypeCheckedLoadConstVCalls) : GlobalValueSummary(FunctionKind, Flags, std::move(Refs)), - InstCount(NumInsts), CallGraphEdgeList(std::move(CGEdges)) { + InstCount(NumInsts), FunFlags(FunFlags), + CallGraphEdgeList(std::move(CGEdges)) { if (!TypeTests.empty() || !TypeTestAssumeVCalls.empty() || !TypeCheckedLoadVCalls.empty() || !TypeTestAssumeConstVCalls.empty() || !TypeCheckedLoadConstVCalls.empty()) @@ -341,12 +360,70 @@ return GVS->getSummaryKind() == FunctionKind; } + /// Get function attribute flags. + FFlags &fflags() { return FunFlags; } + /// Get the instruction count recorded for this function. unsigned instCount() const { return InstCount; } /// Return the list of pairs. ArrayRef calls() const { return CallGraphEdgeList; } + /// Iterator for a function summaries calls. + class const_iterator + : public std::iterator { + std::vector::const_iterator I; + const FunctionSummary *fsumFromEdge(const EdgeTy &P) { + if (P.first.Ref && P.first.getSummaryList().size()) + return cast(P.first.getSummaryList().front().get()); + + // External function + // FIXME: constructing a new functionsumary just for external functions is + // probably wrong + auto F = llvm::make_unique( + GVFlags(GlobalValue::LinkageTypes::AvailableExternallyLinkage, true, + false), + 0, FFlags{}, std::vector(), std::vector(), + std::vector(), std::vector(), + std::vector(), std::vector(), + std::vector()); + F->setOriginalName(P.first.Ref ? P.first.getGUID() : 0); + return F.get(); + } + + public: + const_iterator(std::vector::const_iterator I) : I(I){}; + const_iterator operator++(int) { + I++; + return *this; + } + bool operator==(const const_iterator &rhs) const { return I == rhs.I; } + bool operator!=(const const_iterator &rhs) const { return I != rhs.I; } + const FunctionSummary *operator*() { return fsumFromEdge(*I); } + }; + + const_iterator call_summaries_begin() const { + return const_iterator(CallGraphEdgeList.begin()); + } + const_iterator call_summaries_end() const { + return const_iterator(CallGraphEdgeList.end()); + } + + /// Sets the MemoryAccessKind for a call edge in the function. Returns false + /// if CalleeGUID isn't found. + bool updateCalleeMemAccessKind(GlobalValue::GUID CalleeGUID, + MemoryAccessKind M) { + auto E = std::find_if(CallGraphEdgeList.begin(), CallGraphEdgeList.end(), + [&CalleeGUID](FunctionSummary::EdgeTy &E) -> bool { + return E.first.getGUID() == CalleeGUID; + }); + if (E != CallGraphEdgeList.end()) { + E->second.MemoryAccess = M; + return true; + } + return false; + } + /// Returns the list of type identifiers used by this function in /// llvm.type.test intrinsics other than by an llvm.assume intrinsic, /// represented as GUIDs. @@ -754,6 +831,38 @@ StringMap &ModuleToDefinedGVSummaries) const; }; +template <> struct GraphTraits { + typedef const FunctionSummary *NodeRef; // Function or Alias + + typedef FunctionSummary::const_iterator ChildIteratorType; + + static NodeRef getEntryNode(const FunctionSummary *F) { + if (F->call_summaries_begin() != F->call_summaries_end()) + return *(F->call_summaries_begin()); + + // use a dummy functionsummary + auto S = llvm::make_unique( + FunctionSummary::GVFlags( + GlobalValue::LinkageTypes::AvailableExternallyLinkage, true, false), + 0, FunctionSummary::FFlags{}, std::vector(), + std::vector(), + std::vector(), + std::vector(), + std::vector(), + std::vector(), + std::vector()); + return S.get(); + } + + static ChildIteratorType child_begin(NodeRef N) { + return N->call_summaries_begin(); + } + + static ChildIteratorType child_end(NodeRef N) { + return N->call_summaries_end(); + } +}; + } // end namespace llvm #endif // LLVM_IR_MODULESUMMARYINDEX_H Index: include/llvm/IR/ModuleSummaryIndexYAML.h =================================================================== --- include/llvm/IR/ModuleSummaryIndexYAML.h +++ include/llvm/IR/ModuleSummaryIndexYAML.h @@ -206,8 +206,9 @@ GlobalValueSummary::GVFlags( static_cast(FSum.Linkage), FSum.NotEligibleToImport, FSum.Live), - 0, ArrayRef{}, ArrayRef{}, - std::move(FSum.TypeTests), std::move(FSum.TypeTestAssumeVCalls), + 0, FunctionSummary::FFlags{}, ArrayRef{}, + ArrayRef{}, std::move(FSum.TypeTests), + std::move(FSum.TypeTestAssumeVCalls), std::move(FSum.TypeCheckedLoadVCalls), std::move(FSum.TypeTestAssumeConstVCalls), std::move(FSum.TypeCheckedLoadConstVCalls))); Index: lib/Analysis/ModuleSummaryAnalysis.cpp =================================================================== --- lib/Analysis/ModuleSummaryAnalysis.cpp +++ lib/Analysis/ModuleSummaryAnalysis.cpp @@ -276,10 +276,16 @@ F.isVarArg(); GlobalValueSummary::GVFlags Flags(F.getLinkage(), NotEligibleForImport, /* Live = */ false); + FunctionSummary::FFlags FunFlags{ + F.hasFnAttribute(Attribute::ReadNone), + F.hasFnAttribute(Attribute::ReadOnly), + F.hasFnAttribute(Attribute::NoRecurse), + F.hasFnAttribute(Attribute::NoAlias), + }; auto FuncSummary = llvm::make_unique( - Flags, NumInsts, RefEdges.takeVector(), CallGraphEdges.takeVector(), - TypeTests.takeVector(), TypeTestAssumeVCalls.takeVector(), - TypeCheckedLoadVCalls.takeVector(), + Flags, NumInsts, FunFlags, RefEdges.takeVector(), + CallGraphEdges.takeVector(), TypeTests.takeVector(), + TypeTestAssumeVCalls.takeVector(), TypeCheckedLoadVCalls.takeVector(), TypeTestAssumeConstVCalls.takeVector(), TypeCheckedLoadConstVCalls.takeVector()); if (NonRenamableLocal) @@ -427,11 +433,16 @@ /* Live = */ true); CantBePromoted.insert(GlobalValue::getGUID(Name)); // Create the appropriate summary type. - if (isa(GV)) { + if (Function *F = dyn_cast(GV)) { std::unique_ptr Summary = llvm::make_unique( - GVFlags, 0, ArrayRef{}, - ArrayRef{}, + GVFlags, 0, + FunctionSummary::FFlags{ + F->hasFnAttribute(Attribute::ReadNone), + F->hasFnAttribute(Attribute::ReadOnly), + F->hasFnAttribute(Attribute::NoRecurse), + F->hasFnAttribute(Attribute::NoAlias)}, + ArrayRef{}, ArrayRef{}, ArrayRef{}, ArrayRef{}, ArrayRef{}, Index: lib/Bitcode/Reader/BitcodeReader.cpp =================================================================== --- lib/Bitcode/Reader/BitcodeReader.cpp +++ lib/Bitcode/Reader/BitcodeReader.cpp @@ -860,6 +860,15 @@ } } +static FunctionSummary::FFlags getDecodedFFlags(uint64_t RawFlags) { + FunctionSummary::FFlags Flags; + Flags.ReadNone = RawFlags & 0x1; + Flags.ReadOnly = (RawFlags >> 1) & 0x1; + Flags.NoRecurse = (RawFlags >> 2) & 0x1; + Flags.NoAlias = (RawFlags >> 3) & 0x1; + return Flags; +} + /// Decode the flags for GlobalValue in the summary. static GlobalValueSummary::GVFlags getDecodedGVSummaryFlags(uint64_t RawFlags, uint64_t Version) { @@ -5008,13 +5017,14 @@ for (unsigned I = 0, E = Record.size(); I != E; ++I) { CalleeInfo::HotnessType Hotness = CalleeInfo::HotnessType::Unknown; ValueInfo Callee = getValueInfoFromValueId(Record[I]).first; + MemoryAccessKind MA = static_cast(Record[++I]); if (IsOldProfileFormat) { I += 1; // Skip old callsitecount field if (HasProfile) I += 1; // Skip old profilecount field } else if (HasProfile) Hotness = static_cast(Record[++I]); - Ret.push_back(FunctionSummary::EdgeTy{Callee, CalleeInfo{Hotness}}); + Ret.push_back(FunctionSummary::EdgeTy{Callee, CalleeInfo{Hotness, MA}}); } return Ret; } @@ -5098,14 +5108,15 @@ unsigned ValueID = Record[0]; uint64_t RawFlags = Record[1]; unsigned InstCount = Record[2]; - unsigned NumRefs = Record[3]; + uint64_t RawFunFlags = Record[3]; + unsigned NumRefs = Record[4]; auto Flags = getDecodedGVSummaryFlags(RawFlags, Version); // The module path string ref set in the summary must be owned by the // index's module string table. Since we don't have a module path // 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. - static int RefListStartIndex = 4; + static int RefListStartIndex = 5; int CallGraphEdgeStartIndex = RefListStartIndex + NumRefs; assert(Record.size() >= RefListStartIndex + NumRefs && "Record size inconsistent with number of references"); @@ -5116,8 +5127,9 @@ ArrayRef(Record).slice(CallGraphEdgeStartIndex), IsOldProfileFormat, HasProfile); auto FS = llvm::make_unique( - Flags, InstCount, std::move(Refs), std::move(Calls), - std::move(PendingTypeTests), std::move(PendingTypeTestAssumeVCalls), + Flags, InstCount, getDecodedFFlags(RawFunFlags), std::move(Refs), + std::move(Calls), std::move(PendingTypeTests), + std::move(PendingTypeTestAssumeVCalls), std::move(PendingTypeCheckedLoadVCalls), std::move(PendingTypeTestAssumeConstVCalls), std::move(PendingTypeCheckedLoadConstVCalls)); @@ -5186,9 +5198,10 @@ uint64_t ModuleId = Record[1]; uint64_t RawFlags = Record[2]; unsigned InstCount = Record[3]; - unsigned NumRefs = Record[4]; + uint64_t RawFunFlags = Record[4]; + unsigned NumRefs = Record[5]; auto Flags = getDecodedGVSummaryFlags(RawFlags, Version); - static int RefListStartIndex = 5; + static int RefListStartIndex = 6; int CallGraphEdgeStartIndex = RefListStartIndex + NumRefs; assert(Record.size() >= RefListStartIndex + NumRefs && "Record size inconsistent with number of references"); @@ -5200,8 +5213,9 @@ IsOldProfileFormat, HasProfile); ValueInfo VI = getValueInfoFromValueId(ValueID).first; auto FS = llvm::make_unique( - Flags, InstCount, std::move(Refs), std::move(Edges), - std::move(PendingTypeTests), std::move(PendingTypeTestAssumeVCalls), + Flags, InstCount, getDecodedFFlags(RawFunFlags), std::move(Refs), + std::move(Edges), std::move(PendingTypeTests), + std::move(PendingTypeTestAssumeVCalls), std::move(PendingTypeCheckedLoadVCalls), std::move(PendingTypeTestAssumeConstVCalls), std::move(PendingTypeCheckedLoadConstVCalls)); Index: lib/Bitcode/Writer/BitcodeWriter.cpp =================================================================== --- lib/Bitcode/Writer/BitcodeWriter.cpp +++ lib/Bitcode/Writer/BitcodeWriter.cpp @@ -895,6 +895,15 @@ return getEncodedLinkage(GV.getLinkage()); } +static uint64_t getEncodedFFlags(FunctionSummary::FFlags Flags) { + uint64_t RawFlags = 0; + RawFlags |= Flags.ReadNone; + RawFlags |= (Flags.ReadOnly << 1); + RawFlags |= (Flags.NoRecurse << 2); + RawFlags |= (Flags.NoAlias << 3); + return RawFlags; +} + // Decode the flags for GlobalValue in the summary static uint64_t getEncodedGVSummaryFlags(GlobalValueSummary::GVFlags Flags) { uint64_t RawFlags = 0; @@ -1703,7 +1712,7 @@ Record.push_back(N->isDistinct()); Record.push_back(VE.getMetadataOrNullID(N->getVariable())); Record.push_back(VE.getMetadataOrNullID(N->getExpression())); - + Stream.EmitRecord(bitc::METADATA_GLOBAL_VAR_EXPR, Record, Abbrev); Record.clear(); } @@ -3293,6 +3302,7 @@ NameVals.push_back(getEncodedGVSummaryFlags(FS->flags())); NameVals.push_back(FS->instCount()); + NameVals.push_back(getEncodedFFlags(FS->fflags())); NameVals.push_back(FS->refs().size()); for (auto &RI : FS->refs()) @@ -3301,6 +3311,7 @@ bool HasProfileData = F.getEntryCount().hasValue(); for (auto &ECI : FS->calls()) { NameVals.push_back(getValueId(ECI.first)); + NameVals.push_back(static_cast(ECI.second.MemoryAccess)); if (HasProfileData) NameVals.push_back(static_cast(ECI.second.Hotness)); } @@ -3574,6 +3585,7 @@ NameVals.push_back(Index.getModuleId(FS->modulePath())); NameVals.push_back(getEncodedGVSummaryFlags(FS->flags())); NameVals.push_back(FS->instCount()); + NameVals.push_back(getEncodedFFlags(FS->fflags())); // Fill in below NameVals.push_back(0); @@ -3585,7 +3597,7 @@ NameVals.push_back(*RefValueId); Count++; } - NameVals[4] = Count; + NameVals[5] = Count; bool HasProfileData = false; for (auto &EI : FS->calls()) { @@ -3611,6 +3623,7 @@ continue; } NameVals.push_back(*CallValueId); + NameVals.push_back(static_cast(EI.second.MemoryAccess)); if (HasProfileData) NameVals.push_back(static_cast(EI.second.Hotness)); }