Index: include/llvm/ProfileData/SampleProfReader.h =================================================================== --- include/llvm/ProfileData/SampleProfReader.h +++ include/llvm/ProfileData/SampleProfReader.h @@ -232,7 +232,7 @@ class SampleProfileReader { public: SampleProfileReader(std::unique_ptr B, LLVMContext &C) - : Profiles(0), Ctx(C), Buffer(std::move(B)) {} + : Profiles(0), MaxFunctionCount(0), Ctx(C), Buffer(std::move(B)) {} virtual ~SampleProfileReader() {} @@ -262,6 +262,9 @@ LineNumber, Msg)); } + /// \brief Return maximum function entry count; + uint64_t getMaxFunctionCount() const { return MaxFunctionCount; } + /// \brief Create a sample profile reader appropriate to the file format. static ErrorOr> create(StringRef Filename, LLVMContext &C); @@ -278,6 +281,9 @@ /// to their corresponding profiles. StringMap Profiles; + /// \brief Max entry count of all functions. + uint64_t MaxFunctionCount; + /// \brief LLVM context used to emit diagnostics. LLVMContext &Ctx; Index: lib/ProfileData/SampleProfReader.cpp =================================================================== --- lib/ProfileData/SampleProfReader.cpp +++ lib/ProfileData/SampleProfReader.cpp @@ -182,6 +182,9 @@ FunctionSamples &FProfile = Profiles[FName]; MergeResult(Result, FProfile.addTotalSamples(NumSamples)); MergeResult(Result, FProfile.addHeadSamples(NumHeadSamples)); + if (NumHeadSamples > MaxFunctionCount) { + MaxFunctionCount = NumHeadSamples; + } InlineStack.clear(); InlineStack.push_back(&FProfile); } else { @@ -374,6 +377,9 @@ FunctionSamples &FProfile = Profiles[*FName]; FProfile.addHeadSamples(*NumHeadSamples); + if (*NumHeadSamples > MaxFunctionCount) { + MaxFunctionCount = *NumHeadSamples; + } if (std::error_code EC = readProfile(FProfile)) return EC; @@ -552,6 +558,10 @@ // the profile of the original function. FProfile = &Profiles[Name]; FProfile->addHeadSamples(HeadCount); + if (HeadCount > MaxFunctionCount) { + MaxFunctionCount = HeadCount; + } + if (FProfile->getTotalSamples() > 0) Update = false; } else { Index: lib/Transforms/IPO/SampleProfile.cpp =================================================================== --- lib/Transforms/IPO/SampleProfile.cpp +++ lib/Transforms/IPO/SampleProfile.cpp @@ -1243,6 +1243,8 @@ if (!ProfileIsValid) return false; + M.setMaximumFunctionCount(Reader->getMaxFunctionCount()); + // Compute the total number of samples collected in this profile. for (const auto &I : Reader->getProfiles()) TotalCollectedSamples += I.second.getTotalSamples();