LCOV - code coverage report
Current view: top level - lib/ProfileData - ProfileSummaryBuilder.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 47 47 100.0 %
Date: 2017-09-14 15:23:50 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/Constants.h"
      16             : #include "llvm/IR/Function.h"
      17             : #include "llvm/IR/Metadata.h"
      18             : #include "llvm/IR/Type.h"
      19             : #include "llvm/ProfileData/InstrProf.h"
      20             : #include "llvm/ProfileData/ProfileCommon.h"
      21             : #include "llvm/ProfileData/SampleProf.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};
      33             : const ArrayRef<uint32_t> ProfileSummaryBuilder::DefaultCutoffs =
      34             :     DefaultCutoffsData;
      35             : 
      36         495 : void InstrProfSummaryBuilder::addRecord(const InstrProfRecord &R) {
      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         990 :   addEntryCount(R.Counts[0]);
      42        2282 :   for (size_t I = 1, E = R.Counts.size(); I < E; ++I)
      43        3876 :     addInternalCount(R.Counts[I]);
      44         495 : }
      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.
      48         148 : void SampleProfileSummaryBuilder::addRecord(
      49             :     const sampleprof::FunctionSamples &FS) {
      50         148 :   NumFunctions++;
      51         148 :   if (FS.getHeadSamples() > MaxFunctionCount)
      52          74 :     MaxFunctionCount = FS.getHeadSamples();
      53         894 :   for (const auto &I : FS.getBodySamples())
      54         450 :     addCount(I.second.getSamples());
      55         148 : }
      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.
      59         386 : void ProfileSummaryBuilder::computeDetailedSummary() {
      60         772 :   if (DetailedSummaryCutoffs.empty())
      61             :     return;
      62        1008 :   std::sort(DetailedSummaryCutoffs.begin(), DetailedSummaryCutoffs.end());
      63         672 :   auto Iter = CountFrequencies.begin();
      64         672 :   const auto End = CountFrequencies.end();
      65             : 
      66         336 :   uint32_t CountsSeen = 0;
      67         336 :   uint64_t CurrSum = 0, Count = 0;
      68             : 
      69        7349 :   for (const uint32_t Cutoff : DetailedSummaryCutoffs) {
      70             :     assert(Cutoff <= 999999);
      71       18015 :     APInt Temp(128, TotalCount);
      72       18015 :     APInt N(128, Cutoff);
      73       12010 :     APInt D(128, ProfileSummary::Scale);
      74        6005 :     Temp *= N;
      75       18015 :     Temp = Temp.sdiv(D);
      76        6005 :     uint64_t DesiredCount = Temp.getZExtValue();
      77             :     assert(DesiredCount <= TotalCount);
      78        6650 :     while (CurrSum < DesiredCount && Iter != End) {
      79         645 :       Count = Iter->first;
      80         645 :       uint32_t Freq = Iter->second;
      81         645 :       CurrSum += (Count * Freq);
      82         645 :       CountsSeen += Freq;
      83         645 :       Iter++;
      84             :     }
      85             :     assert(CurrSum >= DesiredCount);
      86       12010 :     ProfileSummaryEntry PSE = {Cutoff, Count, CountsSeen};
      87        6005 :     DetailedSummary.push_back(PSE);
      88             :   }
      89             : }
      90             : 
      91          94 : std::unique_ptr<ProfileSummary> SampleProfileSummaryBuilder::getSummary() {
      92          94 :   computeDetailedSummary();
      93             :   return llvm::make_unique<ProfileSummary>(
      94             :       ProfileSummary::PSK_Sample, DetailedSummary, TotalCount, MaxCount, 0,
      95          94 :       MaxFunctionCount, NumCounts, NumFunctions);
      96             : }
      97             : 
      98         292 : std::unique_ptr<ProfileSummary> InstrProfSummaryBuilder::getSummary() {
      99         292 :   computeDetailedSummary();
     100             :   return llvm::make_unique<ProfileSummary>(
     101             :       ProfileSummary::PSK_Instr, DetailedSummary, TotalCount, MaxCount,
     102         292 :       MaxInternalBlockCount, MaxFunctionCount, NumCounts, NumFunctions);
     103             : }
     104             : 
     105             : void InstrProfSummaryBuilder::addEntryCount(uint64_t Count) {
     106         495 :   addCount(Count);
     107         495 :   NumFunctions++;
     108         495 :   if (Count > MaxFunctionCount)
     109         290 :     MaxFunctionCount = Count;
     110             : }
     111             : 
     112             : void InstrProfSummaryBuilder::addInternalCount(uint64_t Count) {
     113        1292 :   addCount(Count);
     114        1292 :   if (Count > MaxInternalBlockCount)
     115         288 :     MaxInternalBlockCount = Count;
     116             : }

Generated by: LCOV version 1.13