Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 1 | //===- ProfileSummary.h - Profile summary data structure. -------*- C++ -*-===// |
| 2 | // |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame^] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file defines the profile summary data structure. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #ifndef LLVM_IR_PROFILESUMMARY_H |
| 14 | #define LLVM_IR_PROFILESUMMARY_H |
| 15 | |
| 16 | #include <algorithm> |
| 17 | #include <cstdint> |
| 18 | #include <vector> |
| 19 | |
| 20 | namespace llvm { |
| 21 | |
| 22 | class LLVMContext; |
| 23 | class Metadata; |
| 24 | |
| 25 | // The profile summary is one or more (Cutoff, MinCount, NumCounts) triplets. |
| 26 | // The semantics of counts depend on the type of profile. For instrumentation |
| 27 | // profile, counts are block counts and for sample profile, counts are |
| 28 | // per-line samples. Given a target counts percentile, we compute the minimum |
| 29 | // number of counts needed to reach this target and the minimum among these |
| 30 | // counts. |
| 31 | struct ProfileSummaryEntry { |
| 32 | uint32_t Cutoff; ///< The required percentile of counts. |
| 33 | uint64_t MinCount; ///< The minimum count for this percentile. |
| 34 | uint64_t NumCounts; ///< Number of counts >= the minimum count. |
| 35 | |
| 36 | ProfileSummaryEntry(uint32_t TheCutoff, uint64_t TheMinCount, |
| 37 | uint64_t TheNumCounts) |
| 38 | : Cutoff(TheCutoff), MinCount(TheMinCount), NumCounts(TheNumCounts) {} |
| 39 | }; |
| 40 | |
| 41 | using SummaryEntryVector = std::vector<ProfileSummaryEntry>; |
| 42 | |
| 43 | class ProfileSummary { |
| 44 | public: |
| 45 | enum Kind { PSK_Instr, PSK_Sample }; |
| 46 | |
| 47 | private: |
| 48 | const Kind PSK; |
| 49 | static const char *KindStr[2]; |
| 50 | SummaryEntryVector DetailedSummary; |
| 51 | uint64_t TotalCount, MaxCount, MaxInternalCount, MaxFunctionCount; |
| 52 | uint32_t NumCounts, NumFunctions; |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 53 | /// Return detailed summary as metadata. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 54 | Metadata *getDetailedSummaryMD(LLVMContext &Context); |
| 55 | |
| 56 | public: |
| 57 | static const int Scale = 1000000; |
| 58 | |
| 59 | ProfileSummary(Kind K, SummaryEntryVector DetailedSummary, |
| 60 | uint64_t TotalCount, uint64_t MaxCount, |
| 61 | uint64_t MaxInternalCount, uint64_t MaxFunctionCount, |
| 62 | uint32_t NumCounts, uint32_t NumFunctions) |
| 63 | : PSK(K), DetailedSummary(std::move(DetailedSummary)), |
| 64 | TotalCount(TotalCount), MaxCount(MaxCount), |
| 65 | MaxInternalCount(MaxInternalCount), MaxFunctionCount(MaxFunctionCount), |
| 66 | NumCounts(NumCounts), NumFunctions(NumFunctions) {} |
| 67 | |
| 68 | Kind getKind() const { return PSK; } |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 69 | /// Return summary information as metadata. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 70 | Metadata *getMD(LLVMContext &Context); |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 71 | /// Construct profile summary from metdata. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 72 | static ProfileSummary *getFromMD(Metadata *MD); |
| 73 | SummaryEntryVector &getDetailedSummary() { return DetailedSummary; } |
| 74 | uint32_t getNumFunctions() { return NumFunctions; } |
| 75 | uint64_t getMaxFunctionCount() { return MaxFunctionCount; } |
| 76 | uint32_t getNumCounts() { return NumCounts; } |
| 77 | uint64_t getTotalCount() { return TotalCount; } |
| 78 | uint64_t getMaxCount() { return MaxCount; } |
| 79 | uint64_t getMaxInternalCount() { return MaxInternalCount; } |
| 80 | }; |
| 81 | |
| 82 | } // end namespace llvm |
| 83 | |
| 84 | #endif // LLVM_IR_PROFILESUMMARY_H |