LCOV - code coverage report
Current view: top level - lib/Analysis - IndirectCallPromotionAnalysis.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 29 29 100.0 %
Date: 2018-02-25 19:55:18 Functions: 6 6 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===-- IndirectCallPromotionAnalysis.cpp - Find promotion candidates ===//
       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             : // Helper methods for identifying profitable indirect call promotion
      11             : // candidates for an instruction when the indirect-call value profile metadata
      12             : // is available.
      13             : //
      14             : //===----------------------------------------------------------------------===//
      15             : 
      16             : #include "llvm/Analysis/IndirectCallPromotionAnalysis.h"
      17             : #include "llvm/ADT/STLExtras.h"
      18             : #include "llvm/Analysis/IndirectCallSiteVisitor.h"
      19             : #include "llvm/IR/CallSite.h"
      20             : #include "llvm/IR/InstIterator.h"
      21             : #include "llvm/IR/InstVisitor.h"
      22             : #include "llvm/IR/Instructions.h"
      23             : #include "llvm/IR/IntrinsicInst.h"
      24             : #include "llvm/ProfileData/InstrProf.h"
      25             : #include "llvm/Support/Debug.h"
      26             : #include <string>
      27             : #include <utility>
      28             : #include <vector>
      29             : 
      30             : using namespace llvm;
      31             : 
      32             : #define DEBUG_TYPE "pgo-icall-prom-analysis"
      33             : 
      34             : // The percent threshold for the direct-call target (this call site vs the
      35             : // remaining call count) for it to be considered as the promotion target.
      36       81774 : static cl::opt<unsigned> ICPRemainingPercentThreshold(
      37      163548 :     "icp-remaining-percent-threshold", cl::init(30), cl::Hidden, cl::ZeroOrMore,
      38       81774 :     cl::desc("The percentage threshold against remaining unpromoted indirect "
      39      245322 :              "call count for the promotion"));
      40             : 
      41             : // The percent threshold for the direct-call target (this call site vs the
      42             : // total call count) for it to be considered as the promotion target.
      43             : static cl::opt<unsigned>
      44      245322 :     ICPTotalPercentThreshold("icp-total-percent-threshold", cl::init(5),
      45             :                              cl::Hidden, cl::ZeroOrMore,
      46       81774 :                              cl::desc("The percentage threshold against total "
      47      245322 :                                       "count for the promotion"));
      48             : 
      49             : // Set the maximum number of targets to promote for a single indirect-call
      50             : // callsite.
      51             : static cl::opt<unsigned>
      52      245322 :     MaxNumPromotions("icp-max-prom", cl::init(3), cl::Hidden, cl::ZeroOrMore,
      53       81774 :                      cl::desc("Max number of promotions for a single indirect "
      54      245322 :                               "call callsite"));
      55             : 
      56        1400 : ICallPromotionAnalysis::ICallPromotionAnalysis() {
      57        2800 :   ValueDataArray = llvm::make_unique<InstrProfValueData[]>(MaxNumPromotions);
      58        1400 : }
      59             : 
      60          61 : bool ICallPromotionAnalysis::isPromotionProfitable(uint64_t Count,
      61             :                                                    uint64_t TotalCount,
      62             :                                                    uint64_t RemainingCount) {
      63         183 :   return Count * 100 >= ICPRemainingPercentThreshold * RemainingCount &&
      64         122 :          Count * 100 >= ICPTotalPercentThreshold * TotalCount;
      65             : }
      66             : 
      67             : // Indirect-call promotion heuristic. The direct targets are sorted based on
      68             : // the count. Stop at the first target that is not promoted. Returns the
      69             : // number of candidates deemed profitable.
      70          46 : uint32_t ICallPromotionAnalysis::getProfitablePromotionCandidates(
      71             :     const Instruction *Inst, uint32_t NumVals, uint64_t TotalCount) {
      72             :   ArrayRef<InstrProfValueData> ValueDataRef(ValueDataArray.get(), NumVals);
      73             : 
      74             :   DEBUG(dbgs() << " \nWork on callsite " << *Inst << " Num_targets: " << NumVals
      75             :                << "\n");
      76             : 
      77             :   uint32_t I = 0;
      78             :   uint64_t RemainingCount = TotalCount;
      79         160 :   for (; I < MaxNumPromotions && I < NumVals; I++) {
      80         122 :     uint64_t Count = ValueDataRef[I].Count;
      81             :     assert(Count <= RemainingCount);
      82             :     DEBUG(dbgs() << " Candidate " << I << " Count=" << Count
      83             :                  << "  Target_func: " << ValueDataRef[I].Value << "\n");
      84             : 
      85          61 :     if (!isPromotionProfitable(Count, TotalCount, RemainingCount)) {
      86             :       DEBUG(dbgs() << " Not promote: Cold target.\n");
      87             :       return I;
      88             :     }
      89          57 :     RemainingCount -= Count;
      90             :   }
      91             :   return I;
      92             : }
      93             : 
      94             : ArrayRef<InstrProfValueData>
      95          81 : ICallPromotionAnalysis::getPromotionCandidatesForInstruction(
      96             :     const Instruction *I, uint32_t &NumVals, uint64_t &TotalCount,
      97             :     uint32_t &NumCandidates) {
      98             :   bool Res =
      99             :       getValueProfDataFromInst(*I, IPVK_IndirectCallTarget, MaxNumPromotions,
     100          81 :                                ValueDataArray.get(), NumVals, TotalCount);
     101          81 :   if (!Res) {
     102          35 :     NumCandidates = 0;
     103          35 :     return ArrayRef<InstrProfValueData>();
     104             :   }
     105          46 :   NumCandidates = getProfitablePromotionCandidates(I, NumVals, TotalCount);
     106          92 :   return ArrayRef<InstrProfValueData>(ValueDataArray.get(), NumVals);
     107      245322 : }

Generated by: LCOV version 1.13