OSDN Git Service

[llvm-dlltool] Set a proper machine type for weak symbol object files
[android-x86/external-llvm.git] / lib / ProfileData / ProfileSummaryBuilder.cpp
1 //=-- ProfilesummaryBuilder.cpp - Profile summary computation ---------------=//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file contains support for computing profile summary data.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #include "llvm/IR/Attributes.h"
14 #include "llvm/IR/Function.h"
15 #include "llvm/IR/Metadata.h"
16 #include "llvm/IR/Type.h"
17 #include "llvm/ProfileData/InstrProf.h"
18 #include "llvm/ProfileData/ProfileCommon.h"
19 #include "llvm/ProfileData/SampleProf.h"
20 #include "llvm/Support/Casting.h"
21
22 using namespace llvm;
23
24 // A set of cutoff values. Each value, when divided by ProfileSummary::Scale
25 // (which is 1000000) is a desired percentile of total counts.
26 static const uint32_t DefaultCutoffsData[] = {
27     10000,  /*  1% */
28     100000, /* 10% */
29     200000, 300000, 400000, 500000, 600000, 700000, 800000,
30     900000, 950000, 990000, 999000, 999900, 999990, 999999};
31 const ArrayRef<uint32_t> ProfileSummaryBuilder::DefaultCutoffs =
32     DefaultCutoffsData;
33
34 void InstrProfSummaryBuilder::addRecord(const InstrProfRecord &R) {
35   // The first counter is not necessarily an entry count for IR
36   // instrumentation profiles.
37   // Eventually MaxFunctionCount will become obsolete and this can be
38   // removed.
39   addEntryCount(R.Counts[0]);
40   for (size_t I = 1, E = R.Counts.size(); I < E; ++I)
41     addInternalCount(R.Counts[I]);
42 }
43
44 // To compute the detailed summary, we consider each line containing samples as
45 // equivalent to a block with a count in the instrumented profile.
46 void SampleProfileSummaryBuilder::addRecord(
47     const sampleprof::FunctionSamples &FS) {
48   NumFunctions++;
49   if (FS.getHeadSamples() > MaxFunctionCount)
50     MaxFunctionCount = FS.getHeadSamples();
51   for (const auto &I : FS.getBodySamples())
52     addCount(I.second.getSamples());
53 }
54
55 // The argument to this method is a vector of cutoff percentages and the return
56 // value is a vector of (Cutoff, MinCount, NumCounts) triplets.
57 void ProfileSummaryBuilder::computeDetailedSummary() {
58   if (DetailedSummaryCutoffs.empty())
59     return;
60   llvm::sort(DetailedSummaryCutoffs);
61   auto Iter = CountFrequencies.begin();
62   const auto End = CountFrequencies.end();
63
64   uint32_t CountsSeen = 0;
65   uint64_t CurrSum = 0, Count = 0;
66
67   for (const uint32_t Cutoff : DetailedSummaryCutoffs) {
68     assert(Cutoff <= 999999);
69     APInt Temp(128, TotalCount);
70     APInt N(128, Cutoff);
71     APInt D(128, ProfileSummary::Scale);
72     Temp *= N;
73     Temp = Temp.sdiv(D);
74     uint64_t DesiredCount = Temp.getZExtValue();
75     assert(DesiredCount <= TotalCount);
76     while (CurrSum < DesiredCount && Iter != End) {
77       Count = Iter->first;
78       uint32_t Freq = Iter->second;
79       CurrSum += (Count * Freq);
80       CountsSeen += Freq;
81       Iter++;
82     }
83     assert(CurrSum >= DesiredCount);
84     ProfileSummaryEntry PSE = {Cutoff, Count, CountsSeen};
85     DetailedSummary.push_back(PSE);
86   }
87 }
88
89 std::unique_ptr<ProfileSummary> SampleProfileSummaryBuilder::getSummary() {
90   computeDetailedSummary();
91   return llvm::make_unique<ProfileSummary>(
92       ProfileSummary::PSK_Sample, DetailedSummary, TotalCount, MaxCount, 0,
93       MaxFunctionCount, NumCounts, NumFunctions);
94 }
95
96 std::unique_ptr<ProfileSummary> InstrProfSummaryBuilder::getSummary() {
97   computeDetailedSummary();
98   return llvm::make_unique<ProfileSummary>(
99       ProfileSummary::PSK_Instr, DetailedSummary, TotalCount, MaxCount,
100       MaxInternalBlockCount, MaxFunctionCount, NumCounts, NumFunctions);
101 }
102
103 void InstrProfSummaryBuilder::addEntryCount(uint64_t Count) {
104   addCount(Count);
105   NumFunctions++;
106   if (Count > MaxFunctionCount)
107     MaxFunctionCount = Count;
108 }
109
110 void InstrProfSummaryBuilder::addInternalCount(uint64_t Count) {
111   addCount(Count);
112   if (Count > MaxInternalBlockCount)
113     MaxInternalBlockCount = Count;
114 }