LLVM  6.0.0svn
IndirectCallPromotionAnalysis.cpp
Go to the documentation of this file.
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 
17 #include "llvm/ADT/STLExtras.h"
19 #include "llvm/IR/CallSite.h"
20 #include "llvm/IR/DiagnosticInfo.h"
21 #include "llvm/IR/InstIterator.h"
22 #include "llvm/IR/InstVisitor.h"
23 #include "llvm/IR/Instructions.h"
24 #include "llvm/IR/IntrinsicInst.h"
26 #include "llvm/Support/Debug.h"
27 #include <string>
28 #include <utility>
29 #include <vector>
30 
31 using namespace llvm;
32 
33 #define DEBUG_TYPE "pgo-icall-prom-analysis"
34 
35 // The percent threshold for the direct-call target (this call site vs the
36 // remaining call count) for it to be considered as the promotion target.
38  "icp-remaining-percent-threshold", cl::init(30), cl::Hidden, cl::ZeroOrMore,
39  cl::desc("The percentage threshold against remaining unpromoted indirect "
40  "call count for the promotion"));
41 
42 // The percent threshold for the direct-call target (this call site vs the
43 // total call count) for it to be considered as the promotion target.
44 static cl::opt<unsigned>
45  ICPTotalPercentThreshold("icp-total-percent-threshold", cl::init(5),
47  cl::desc("The percentage threshold against total "
48  "count for the promotion"));
49 
50 // Set the maximum number of targets to promote for a single indirect-call
51 // callsite.
52 static cl::opt<unsigned>
54  cl::desc("Max number of promotions for a single indirect "
55  "call callsite"));
56 
58  ValueDataArray = llvm::make_unique<InstrProfValueData[]>(MaxNumPromotions);
59 }
60 
61 bool ICallPromotionAnalysis::isPromotionProfitable(uint64_t Count,
62  uint64_t TotalCount,
63  uint64_t RemainingCount) {
64  return Count * 100 >= ICPRemainingPercentThreshold * RemainingCount &&
65  Count * 100 >= ICPTotalPercentThreshold * TotalCount;
66 }
67 
68 // Indirect-call promotion heuristic. The direct targets are sorted based on
69 // the count. Stop at the first target that is not promoted. Returns the
70 // number of candidates deemed profitable.
71 uint32_t ICallPromotionAnalysis::getProfitablePromotionCandidates(
72  const Instruction *Inst, uint32_t NumVals, uint64_t TotalCount) {
73  ArrayRef<InstrProfValueData> ValueDataRef(ValueDataArray.get(), NumVals);
74 
75  DEBUG(dbgs() << " \nWork on callsite " << *Inst << " Num_targets: " << NumVals
76  << "\n");
77 
78  uint32_t I = 0;
79  uint64_t RemainingCount = TotalCount;
80  for (; I < MaxNumPromotions && I < NumVals; I++) {
81  uint64_t Count = ValueDataRef[I].Count;
82  assert(Count <= RemainingCount);
83  DEBUG(dbgs() << " Candidate " << I << " Count=" << Count
84  << " Target_func: " << ValueDataRef[I].Value << "\n");
85 
86  if (!isPromotionProfitable(Count, TotalCount, RemainingCount)) {
87  DEBUG(dbgs() << " Not promote: Cold target.\n");
88  return I;
89  }
90  RemainingCount -= Count;
91  }
92  return I;
93 }
94 
97  const Instruction *I, uint32_t &NumVals, uint64_t &TotalCount,
98  uint32_t &NumCandidates) {
99  bool Res =
100  getValueProfDataFromInst(*I, IPVK_IndirectCallTarget, MaxNumPromotions,
101  ValueDataArray.get(), NumVals, TotalCount);
102  if (!Res) {
103  NumCandidates = 0;
105  }
106  NumCandidates = getProfitablePromotionCandidates(I, NumVals, TotalCount);
107  return ArrayRef<InstrProfValueData>(ValueDataArray.get(), NumVals);
108 }
ArrayRef< InstrProfValueData > getPromotionCandidatesForInstruction(const Instruction *I, uint32_t &NumVals, uint64_t &TotalCount, uint32_t &NumCandidates)
Returns reference to array of InstrProfValueData for the given instruction I.
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
static cl::opt< unsigned > ICPTotalPercentThreshold("icp-total-percent-threshold", cl::init(5), cl::Hidden, cl::ZeroOrMore, cl::desc("The percentage threshold against total " "count for the promotion"))
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:33
Interface to identify indirect call promotion candidates.
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:406
bool getValueProfDataFromInst(const Instruction &Inst, InstrProfValueKind ValueKind, uint32_t MaxNumValueData, InstrProfValueData ValueData[], uint32_t &ActualNumValueData, uint64_t &TotalC)
Extract the value profile data from Inst which is annotated with value profile meta data...
Definition: InstrProf.cpp:856
static cl::opt< unsigned > ICPRemainingPercentThreshold("icp-remaining-percent-threshold", cl::init(30), cl::Hidden, cl::ZeroOrMore, cl::desc("The percentage threshold against remaining unpromoted indirect " "call count for the promotion"))
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
static cl::opt< unsigned > MaxNumPromotions("icp-max-prom", cl::init(3), cl::Hidden, cl::ZeroOrMore, cl::desc("Max number of promotions for a single indirect " "call callsite"))
#define I(x, y, z)
Definition: MD5.cpp:58
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
Definition: Value.h:73
#define DEBUG(X)
Definition: Debug.h:118