blob: 716c39e43541172b6485363df42262ad7a6c615c [file] [log] [blame]
Easwaran Ramand68aae22016-02-04 23:34:31 +00001//=-- Profilesummary.cpp - Profile summary computation ----------------------=//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains support for computing profile summary data.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/ProfileData/ProfileCommon.h"
15#include "llvm/ProfileData/InstrProf.h"
16
17using namespace llvm;
18
Easwaran Raman43095702016-02-17 18:18:47 +000019void InstrProfSummary::addRecord(const InstrProfRecord &R) {
20 addEntryCount(R.Counts[0]);
21 for (size_t I = 1, E = R.Counts.size(); I < E; ++I)
22 addInternalCount(R.Counts[I]);
Easwaran Ramand68aae22016-02-04 23:34:31 +000023}
24
25// The argument to this method is a vector of cutoff percentages and the return
Easwaran Raman43095702016-02-17 18:18:47 +000026// value is a vector of (Cutoff, MinCount, NumCounts) triplets.
Easwaran Ramand68aae22016-02-04 23:34:31 +000027void ProfileSummary::computeDetailedSummary() {
28 if (DetailedSummaryCutoffs.empty())
29 return;
30 auto Iter = CountFrequencies.begin();
31 auto End = CountFrequencies.end();
32 std::sort(DetailedSummaryCutoffs.begin(), DetailedSummaryCutoffs.end());
33
Easwaran Raman43095702016-02-17 18:18:47 +000034 uint32_t CountsSeen = 0;
Easwaran Ramand68aae22016-02-04 23:34:31 +000035 uint64_t CurrSum = 0, Count = 0;
36
37 for (uint32_t Cutoff : DetailedSummaryCutoffs) {
38 assert(Cutoff <= 999999);
39 APInt Temp(128, TotalCount);
40 APInt N(128, Cutoff);
41 APInt D(128, ProfileSummary::Scale);
42 Temp *= N;
43 Temp = Temp.sdiv(D);
44 uint64_t DesiredCount = Temp.getZExtValue();
45 assert(DesiredCount <= TotalCount);
46 while (CurrSum < DesiredCount && Iter != End) {
47 Count = Iter->first;
48 uint32_t Freq = Iter->second;
49 CurrSum += (Count * Freq);
Easwaran Raman43095702016-02-17 18:18:47 +000050 CountsSeen += Freq;
Easwaran Ramand68aae22016-02-04 23:34:31 +000051 Iter++;
52 }
53 assert(CurrSum >= DesiredCount);
Easwaran Raman43095702016-02-17 18:18:47 +000054 ProfileSummaryEntry PSE = {Cutoff, Count, CountsSeen};
Easwaran Ramand68aae22016-02-04 23:34:31 +000055 DetailedSummary.push_back(PSE);
56 }
57}
58
Easwaran Raman43095702016-02-17 18:18:47 +000059InstrProfSummary::InstrProfSummary(const IndexedInstrProf::Summary &S)
60 : ProfileSummary(), MaxInternalBlockCount(S.get(
61 IndexedInstrProf::Summary::MaxInternalBlockCount)),
Easwaran Ramand68aae22016-02-04 23:34:31 +000062 MaxFunctionCount(S.get(IndexedInstrProf::Summary::MaxFunctionCount)),
Easwaran Ramand68aae22016-02-04 23:34:31 +000063 NumFunctions(S.get(IndexedInstrProf::Summary::TotalNumFunctions)) {
Easwaran Raman43095702016-02-17 18:18:47 +000064
65 TotalCount = S.get(IndexedInstrProf::Summary::TotalBlockCount);
66 MaxCount = S.get(IndexedInstrProf::Summary::MaxBlockCount);
67 NumCounts = S.get(IndexedInstrProf::Summary::TotalNumBlocks);
68
Easwaran Ramand68aae22016-02-04 23:34:31 +000069 for (unsigned I = 0; I < S.NumCutoffEntries; I++) {
70 const IndexedInstrProf::Summary::Entry &Ent = S.getEntry(I);
71 DetailedSummary.emplace_back((uint32_t)Ent.Cutoff, Ent.MinBlockCount,
72 Ent.NumBlocks);
73 }
74}
Easwaran Raman43095702016-02-17 18:18:47 +000075void InstrProfSummary::addEntryCount(uint64_t Count) {
76 addCount(Count);
77 NumFunctions++;
78 if (Count > MaxFunctionCount)
79 MaxFunctionCount = Count;
80}
81
82void InstrProfSummary::addInternalCount(uint64_t Count) {
83 addCount(Count);
84 if (Count > MaxInternalBlockCount)
85 MaxInternalBlockCount = Count;
86}