blob: a16094be20f67935f28ef957ccc8a920fcbfbed2 [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
19void ProfileSummary::addRecord(const InstrProfRecord &R) {
20 NumFunctions++;
21 if (R.Counts[0] > MaxFunctionCount)
22 MaxFunctionCount = R.Counts[0];
23
24 for (size_t I = 0, E = R.Counts.size(); I < E; ++I)
25 addCount(R.Counts[I], (I == 0));
26}
27
28// The argument to this method is a vector of cutoff percentages and the return
29// value is a vector of (Cutoff, MinBlockCount, NumBlocks) triplets.
30void ProfileSummary::computeDetailedSummary() {
31 if (DetailedSummaryCutoffs.empty())
32 return;
33 auto Iter = CountFrequencies.begin();
34 auto End = CountFrequencies.end();
35 std::sort(DetailedSummaryCutoffs.begin(), DetailedSummaryCutoffs.end());
36
37 uint32_t BlocksSeen = 0;
38 uint64_t CurrSum = 0, Count = 0;
39
40 for (uint32_t Cutoff : DetailedSummaryCutoffs) {
41 assert(Cutoff <= 999999);
42 APInt Temp(128, TotalCount);
43 APInt N(128, Cutoff);
44 APInt D(128, ProfileSummary::Scale);
45 Temp *= N;
46 Temp = Temp.sdiv(D);
47 uint64_t DesiredCount = Temp.getZExtValue();
48 assert(DesiredCount <= TotalCount);
49 while (CurrSum < DesiredCount && Iter != End) {
50 Count = Iter->first;
51 uint32_t Freq = Iter->second;
52 CurrSum += (Count * Freq);
53 BlocksSeen += Freq;
54 Iter++;
55 }
56 assert(CurrSum >= DesiredCount);
57 ProfileSummaryEntry PSE = {Cutoff, Count, BlocksSeen};
58 DetailedSummary.push_back(PSE);
59 }
60}
61
62ProfileSummary::ProfileSummary(const IndexedInstrProf::Summary &S)
63 : TotalCount(S.get(IndexedInstrProf::Summary::TotalBlockCount)),
64 MaxBlockCount(S.get(IndexedInstrProf::Summary::MaxBlockCount)),
65 MaxInternalBlockCount(
66 S.get(IndexedInstrProf::Summary::MaxInternalBlockCount)),
67 MaxFunctionCount(S.get(IndexedInstrProf::Summary::MaxFunctionCount)),
68 NumBlocks(S.get(IndexedInstrProf::Summary::TotalNumBlocks)),
69 NumFunctions(S.get(IndexedInstrProf::Summary::TotalNumFunctions)) {
70 for (unsigned I = 0; I < S.NumCutoffEntries; I++) {
71 const IndexedInstrProf::Summary::Entry &Ent = S.getEntry(I);
72 DetailedSummary.emplace_back((uint32_t)Ent.Cutoff, Ent.MinBlockCount,
73 Ent.NumBlocks);
74 }
75}