LLVM  6.0.0svn
ProfileSummaryBuilder.cpp
Go to the documentation of this file.
1 //=-- ProfilesummaryBuilder.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/IR/Attributes.h"
15 #include "llvm/IR/Constants.h"
16 #include "llvm/IR/Function.h"
17 #include "llvm/IR/Metadata.h"
18 #include "llvm/IR/Type.h"
22 #include "llvm/Support/Casting.h"
23 
24 using namespace llvm;
25 
26 // A set of cutoff values. Each value, when divided by ProfileSummary::Scale
27 // (which is 1000000) is a desired percentile of total counts.
28 static const uint32_t DefaultCutoffsData[] = {
29  10000, /* 1% */
30  100000, /* 10% */
31  200000, 300000, 400000, 500000, 600000, 500000, 600000, 700000,
32  800000, 900000, 950000, 990000, 999000, 999900, 999990, 999999};
35 
37  // The first counter is not necessarily an entry count for IR
38  // instrumentation profiles.
39  // Eventually MaxFunctionCount will become obsolete and this can be
40  // removed.
41  addEntryCount(R.Counts[0]);
42  for (size_t I = 1, E = R.Counts.size(); I < E; ++I)
43  addInternalCount(R.Counts[I]);
44 }
45 
46 // To compute the detailed summary, we consider each line containing samples as
47 // equivalent to a block with a count in the instrumented profile.
49  const sampleprof::FunctionSamples &FS) {
50  NumFunctions++;
53  for (const auto &I : FS.getBodySamples())
54  addCount(I.second.getSamples());
55 }
56 
57 // The argument to this method is a vector of cutoff percentages and the return
58 // value is a vector of (Cutoff, MinCount, NumCounts) triplets.
60  if (DetailedSummaryCutoffs.empty())
61  return;
62  std::sort(DetailedSummaryCutoffs.begin(), DetailedSummaryCutoffs.end());
63  auto Iter = CountFrequencies.begin();
64  const auto End = CountFrequencies.end();
65 
66  uint32_t CountsSeen = 0;
67  uint64_t CurrSum = 0, Count = 0;
68 
69  for (const uint32_t Cutoff : DetailedSummaryCutoffs) {
70  assert(Cutoff <= 999999);
71  APInt Temp(128, TotalCount);
72  APInt N(128, Cutoff);
74  Temp *= N;
75  Temp = Temp.sdiv(D);
76  uint64_t DesiredCount = Temp.getZExtValue();
77  assert(DesiredCount <= TotalCount);
78  while (CurrSum < DesiredCount && Iter != End) {
79  Count = Iter->first;
80  uint32_t Freq = Iter->second;
81  CurrSum += (Count * Freq);
82  CountsSeen += Freq;
83  Iter++;
84  }
85  assert(CurrSum >= DesiredCount);
86  ProfileSummaryEntry PSE = {Cutoff, Count, CountsSeen};
87  DetailedSummary.push_back(PSE);
88  }
89 }
90 
91 std::unique_ptr<ProfileSummary> SampleProfileSummaryBuilder::getSummary() {
93  return llvm::make_unique<ProfileSummary>(
96 }
97 
98 std::unique_ptr<ProfileSummary> InstrProfSummaryBuilder::getSummary() {
100  return llvm::make_unique<ProfileSummary>(
102  MaxInternalBlockCount, MaxFunctionCount, NumCounts, NumFunctions);
103 }
104 
105 void InstrProfSummaryBuilder::addEntryCount(uint64_t Count) {
106  addCount(Count);
107  NumFunctions++;
108  if (Count > MaxFunctionCount)
109  MaxFunctionCount = Count;
110 }
111 
112 void InstrProfSummaryBuilder::addInternalCount(uint64_t Count) {
113  addCount(Count);
114  if (Count > MaxInternalBlockCount)
115  MaxInternalBlockCount = Count;
116 }
uint64_t getZExtValue() const
Get zero extended value.
Definition: APInt.h:1542
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
APInt sdiv(const APInt &RHS) const
Signed division function for APInt.
Definition: APInt.cpp:1590
This file contains the declarations for metadata subclasses.
static const ArrayRef< uint32_t > DefaultCutoffs
A vector of useful cutoff values for detailed summary.
Definition: ProfileCommon.h:65
void addCount(uint64_t Count)
This is called when a count is seen in the profile.
Definition: ProfileCommon.h:92
void addRecord(const InstrProfRecord &)
Representation of the samples collected for a function.
Definition: SampleProf.h:196
static const int Scale
This file contains the simple types necessary to represent the attributes associated with functions a...
uint64_t getHeadSamples() const
Return the total number of branch samples that have the function as the branch target.
Definition: SampleProf.h:304
const BodySampleMap & getBodySamples() const
Return all the samples collected in the body of the function.
Definition: SampleProf.h:327
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
static const unsigned End
std::unique_ptr< ProfileSummary > getSummary()
void addRecord(const sampleprof::FunctionSamples &FS)
SummaryEntryVector DetailedSummary
Definition: ProfileCommon.h:49
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
Class for arbitrary precision integers.
Definition: APInt.h:69
std::unique_ptr< ProfileSummary > getSummary()
std::vector< uint64_t > Counts
Definition: InstrProf.h:593
Profiling information for a single function.
Definition: InstrProf.h:592
#define I(x, y, z)
Definition: MD5.cpp:58
#define N
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static const uint32_t DefaultCutoffsData[]
void sort(Policy policy, RandomAccessIterator Start, RandomAccessIterator End, const Comparator &Comp=Comparator())
Definition: Parallel.h:199