LLVM  14.0.0git
IndirectCallPromotionAnalysis.cpp
Go to the documentation of this file.
1 //===-- IndirectCallPromotionAnalysis.cpp - Find promotion candidates ===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Helper methods for identifying profitable indirect call promotion
10 // candidates for an instruction when the indirect-call value profile metadata
11 // is available.
12 //
13 //===----------------------------------------------------------------------===//
14 
16 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/IR/InstIterator.h"
19 #include "llvm/IR/InstVisitor.h"
20 #include "llvm/IR/Instructions.h"
21 #include "llvm/IR/IntrinsicInst.h"
24 #include "llvm/Support/Debug.h"
25 #include <memory>
26 
27 using namespace llvm;
28 
29 #define DEBUG_TYPE "pgo-icall-prom-analysis"
30 
31 // The percent threshold for the direct-call target (this call site vs the
32 // remaining call count) for it to be considered as the promotion target.
34  "icp-remaining-percent-threshold", cl::init(30), cl::Hidden, cl::ZeroOrMore,
35  cl::desc("The percentage threshold against remaining unpromoted indirect "
36  "call count for the promotion"));
37 
38 // The percent threshold for the direct-call target (this call site vs the
39 // total call count) for it to be considered as the promotion target.
40 static cl::opt<unsigned>
41  ICPTotalPercentThreshold("icp-total-percent-threshold", cl::init(5),
43  cl::desc("The percentage threshold against total "
44  "count for the promotion"));
45 
46 // Set the maximum number of targets to promote for a single indirect-call
47 // callsite.
48 static cl::opt<unsigned>
50  cl::desc("Max number of promotions for a single indirect "
51  "call callsite"));
52 
54  ValueDataArray = std::make_unique<InstrProfValueData[]>(MaxNumPromotions);
55 }
56 
57 bool ICallPromotionAnalysis::isPromotionProfitable(uint64_t Count,
58  uint64_t TotalCount,
59  uint64_t RemainingCount) {
60  return Count * 100 >= ICPRemainingPercentThreshold * RemainingCount &&
61  Count * 100 >= ICPTotalPercentThreshold * TotalCount;
62 }
63 
64 // Indirect-call promotion heuristic. The direct targets are sorted based on
65 // the count. Stop at the first target that is not promoted. Returns the
66 // number of candidates deemed profitable.
67 uint32_t ICallPromotionAnalysis::getProfitablePromotionCandidates(
68  const Instruction *Inst, uint32_t NumVals, uint64_t TotalCount) {
69  ArrayRef<InstrProfValueData> ValueDataRef(ValueDataArray.get(), NumVals);
70 
71  LLVM_DEBUG(dbgs() << " \nWork on callsite " << *Inst
72  << " Num_targets: " << NumVals << "\n");
73 
74  uint32_t I = 0;
75  uint64_t RemainingCount = TotalCount;
76  for (; I < MaxNumPromotions && I < NumVals; I++) {
77  uint64_t Count = ValueDataRef[I].Count;
78  assert(Count <= RemainingCount);
79  LLVM_DEBUG(dbgs() << " Candidate " << I << " Count=" << Count
80  << " Target_func: " << ValueDataRef[I].Value << "\n");
81 
82  if (!isPromotionProfitable(Count, TotalCount, RemainingCount)) {
83  LLVM_DEBUG(dbgs() << " Not promote: Cold target.\n");
84  return I;
85  }
86  RemainingCount -= Count;
87  }
88  return I;
89 }
90 
93  const Instruction *I, uint32_t &NumVals, uint64_t &TotalCount,
94  uint32_t &NumCandidates) {
95  bool Res =
96  getValueProfDataFromInst(*I, IPVK_IndirectCallTarget, MaxNumPromotions,
97  ValueDataArray.get(), NumVals, TotalCount);
98  if (!Res) {
99  NumCandidates = 0;
101  }
102  NumCandidates = getProfitablePromotionCandidates(I, NumVals, TotalCount);
103  return ArrayRef<InstrProfValueData>(ValueDataArray.get(), NumVals);
104 }
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
IntrinsicInst.h
InstIterator.h
llvm::cl::Hidden
@ Hidden
Definition: CommandLine.h:143
STLExtras.h
ICPTotalPercentThreshold
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"))
LLVM_DEBUG
#define LLVM_DEBUG(X)
Definition: Debug.h:101
IndirectCallPromotionAnalysis.h
llvm::dbgs
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
CommandLine.h
IndirectCallVisitor.h
llvm::Instruction
Definition: Instruction.h:45
InstrProf.h
llvm::cl::ZeroOrMore
@ ZeroOrMore
Definition: CommandLine.h:120
llvm::cl::opt
Definition: CommandLine.h:1434
uint64_t
ICPRemainingPercentThreshold
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"))
I
#define I(x, y, z)
Definition: MD5.cpp:59
llvm::cl::init
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:443
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::ArrayRef
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: APInt.h:32
InstVisitor.h
uint32_t
llvm::ICallPromotionAnalysis::getPromotionCandidatesForInstruction
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.
Definition: IndirectCallPromotionAnalysis.cpp:92
llvm::ICallPromotionAnalysis::ICallPromotionAnalysis
ICallPromotionAnalysis()
Definition: IndirectCallPromotionAnalysis.cpp:53
Instructions.h
llvm::getValueProfDataFromInst
bool getValueProfDataFromInst(const Instruction &Inst, InstrProfValueKind ValueKind, uint32_t MaxNumValueData, InstrProfValueData ValueData[], uint32_t &ActualNumValueData, uint64_t &TotalC, bool GetNoICPValue=false)
Extract the value profile data from Inst which is annotated with value profile meta data.
Definition: InstrProf.cpp:1000
llvm::cl::desc
Definition: CommandLine.h:414
MaxNumPromotions
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"))
llvm::Value
LLVM Value Representation.
Definition: Value.h:75
Debug.h