LLVM  4.0.0
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 minimum call count for the direct-call target to be considered as the
36 // promotion candidate.
37 static cl::opt<unsigned>
38  ICPCountThreshold("icp-count-threshold", cl::Hidden, cl::ZeroOrMore,
39  cl::init(1000),
40  cl::desc("The minimum count to the direct call target "
41  "for the promotion"));
42 
43 // The percent threshold for the direct-call target (this call site vs the
44 // total call count) for it to be considered as the promotion target.
45 static cl::opt<unsigned>
46  ICPPercentThreshold("icp-percent-threshold", cl::init(33), cl::Hidden,
48  cl::desc("The percentage threshold 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  if (Count < ICPCountThreshold)
64  return false;
65 
66  unsigned Percentage = (Count * 100) / TotalCount;
67  return (Percentage >= ICPPercentThreshold);
68 }
69 
70 // Indirect-call promotion heuristic. The direct targets are sorted based on
71 // the count. Stop at the first target that is not promoted. Returns the
72 // number of candidates deemed profitable.
73 uint32_t ICallPromotionAnalysis::getProfitablePromotionCandidates(
74  const Instruction *Inst, uint32_t NumVals, uint64_t TotalCount) {
75  ArrayRef<InstrProfValueData> ValueDataRef(ValueDataArray.get(), NumVals);
76 
77  DEBUG(dbgs() << " \nWork on callsite " << *Inst << " Num_targets: " << NumVals
78  << "\n");
79 
80  uint32_t I = 0;
81  for (; I < MaxNumPromotions && I < NumVals; I++) {
82  uint64_t Count = ValueDataRef[I].Count;
83  assert(Count <= TotalCount);
84  DEBUG(dbgs() << " Candidate " << I << " Count=" << Count
85  << " Target_func: " << ValueDataRef[I].Value << "\n");
86 
87  if (!isPromotionProfitable(Count, TotalCount)) {
88  DEBUG(dbgs() << " Not promote: Cold target.\n");
89  return I;
90  }
91  TotalCount -= Count;
92  }
93  return I;
94 }
95 
98  const Instruction *I, uint32_t &NumVals, uint64_t &TotalCount,
99  uint32_t &NumCandidates) {
100  bool Res =
101  getValueProfDataFromInst(*I, IPVK_IndirectCallTarget, MaxNumPromotions,
102  ValueDataArray.get(), NumVals, TotalCount);
103  if (!Res) {
104  NumCandidates = 0;
106  }
107  NumCandidates = getProfitablePromotionCandidates(I, NumVals, TotalCount);
108  return ArrayRef<InstrProfValueData>(ValueDataArray.get(), NumVals);
109 }
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.
static cl::opt< unsigned > MaxNumPromotions("icp-max-prom", cl::init(2), cl::Hidden, cl::ZeroOrMore, cl::desc("Max number of promotions for a single indirect ""call callsite"))
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:395
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:721
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
static cl::opt< unsigned > ICPPercentThreshold("icp-percent-threshold", cl::init(33), cl::Hidden, cl::ZeroOrMore, cl::desc("The percentage threshold for the promotion"))
#define I(x, y, z)
Definition: MD5.cpp:54
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
Definition: Value.h:71
#define DEBUG(X)
Definition: Debug.h:100
static cl::opt< unsigned > ICPCountThreshold("icp-count-threshold", cl::Hidden, cl::ZeroOrMore, cl::init(1000), cl::desc("The minimum count to the direct call target ""for the promotion"))