LCOV - code coverage report
Current view: top level - lib/ProfileData - ProfileSummaryBuilder.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 39 39 100.0 %
Date: 2018-02-23 05:02:05 Functions: 5 5 100.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.13