LLVM 22.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/IR/Instruction.h"
19#include "llvm/Support/Debug.h"
20
21using namespace llvm;
22
23#define DEBUG_TYPE "pgo-icall-prom-analysis"
24
25namespace llvm {
26
27// The percent threshold for the direct-call target (this call site vs the
28// remaining call count) for it to be considered as the promotion target.
30 "icp-remaining-percent-threshold", cl::init(30), cl::Hidden,
31 cl::desc("The percentage threshold against remaining unpromoted indirect "
32 "call count for the promotion"));
33
34// The percent threshold for the direct-call target (this call site vs the
35// total call count) for it to be considered as the promotion target.
37 ICPTotalPercentThreshold("icp-total-percent-threshold", cl::init(5),
39 cl::desc("The percentage threshold against total "
40 "count for the promotion"));
41
42// Set the minimum absolute count threshold for indirect call promotion.
43// Candidates with counts below this threshold will not be promoted.
45 "icp-minimum-count-threshold", cl::init(0), cl::Hidden,
46 cl::desc("Minimum absolute count for promotion candidate"));
47
48// Set the maximum number of targets to promote for a single indirect-call
49// callsite.
52 cl::desc("Max number of promotions for a single indirect "
53 "call callsite"));
54
56 "icp-max-num-vtables", cl::init(6), cl::Hidden,
57 cl::desc("Max number of vtables annotated for a vtable load instruction."));
58
59} // end namespace llvm
60
61bool ICallPromotionAnalysis::isPromotionProfitable(uint64_t Count,
62 uint64_t TotalCount,
63 uint64_t RemainingCount) {
65 Count * 100 >= ICPRemainingPercentThreshold * RemainingCount &&
66 Count * 100 >= ICPTotalPercentThreshold * TotalCount;
67}
68
69// Indirect-call promotion heuristic. The direct targets are sorted based on
70// the count. Stop at the first target that is not promoted. Returns the
71// number of candidates deemed profitable.
72uint32_t ICallPromotionAnalysis::getProfitablePromotionCandidates(
73 const Instruction *Inst, uint64_t TotalCount) {
74 LLVM_DEBUG(dbgs() << " \nWork on callsite " << *Inst
75 << " Num_targets: " << ValueDataArray.size() << "\n");
76
77 uint32_t I = 0;
78 uint64_t RemainingCount = TotalCount;
79 for (; I < MaxNumPromotions && I < ValueDataArray.size(); I++) {
80 uint64_t Count = ValueDataArray[I].Count;
81 assert(Count <= RemainingCount);
82 LLVM_DEBUG(dbgs() << " Candidate " << I << " Count=" << Count
83 << " Target_func: " << ValueDataArray[I].Value << "\n");
84
85 if (!isPromotionProfitable(Count, TotalCount, RemainingCount)) {
86 LLVM_DEBUG(dbgs() << " Not promote: Cold target.\n");
87 return I;
88 }
89 RemainingCount -= Count;
90 }
91 return I;
92}
93
96 const Instruction *I, uint64_t &TotalCount, uint32_t &NumCandidates) {
97 ValueDataArray = getValueProfDataFromInst(*I, IPVK_IndirectCallTarget,
98 MaxNumPromotions, TotalCount);
99 if (ValueDataArray.empty()) {
100 NumCandidates = 0;
102 }
103 NumCandidates = getProfitablePromotionCandidates(I, TotalCount);
104 return ValueDataArray;
105}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
Interface to identify indirect call promotion candidates.
#define I(x, y, z)
Definition MD5.cpp:58
#define LLVM_DEBUG(...)
Definition Debug.h:114
MutableArrayRef< InstrProfValueData > getPromotionCandidatesForInstruction(const Instruction *I, uint64_t &TotalCount, uint32_t &NumCandidates)
Returns reference to array of InstrProfValueData for the given instruction I.
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
Definition ArrayRef.h:303
initializer< Ty > init(const Ty &Val)
This is an optimization pass for GlobalISel generic memory operations.
FunctionAddr VTableAddr Value
Definition InstrProf.h:137
cl::opt< unsigned > MaxNumVTableAnnotations("icp-max-num-vtables", cl::init(6), cl::Hidden, cl::desc("Max number of vtables annotated for a vtable load instruction."))
static cl::opt< unsigned > ICPMinimumCountThreshold("icp-minimum-count-threshold", cl::init(0), cl::Hidden, cl::desc("Minimum absolute count for promotion candidate"))
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:207
FunctionAddr VTableAddr Count
Definition InstrProf.h:139
LLVM_ABI SmallVector< InstrProfValueData, 4 > getValueProfDataFromInst(const Instruction &Inst, InstrProfValueKind ValueKind, uint32_t MaxNumValueData, uint64_t &TotalC, bool GetNoICPValue=false)
Extract the value profile data from Inst and returns them if Inst is annotated with value profile dat...
MutableArrayRef(T &OneElt) -> MutableArrayRef< T >
static cl::opt< unsigned > ICPRemainingPercentThreshold("icp-remaining-percent-threshold", cl::init(30), cl::Hidden, cl::desc("The percentage threshold against remaining unpromoted indirect " "call count for the promotion"))
static cl::opt< unsigned > MaxNumPromotions("icp-max-prom", cl::init(3), cl::Hidden, cl::desc("Max number of promotions for a single indirect " "call callsite"))
static cl::opt< uint64_t > ICPTotalPercentThreshold("icp-total-percent-threshold", cl::init(5), cl::Hidden, cl::desc("The percentage threshold against total " "count for the promotion"))