LLVM 20.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
25// The percent threshold for the direct-call target (this call site vs the
26// remaining call count) for it to be considered as the promotion target.
28 "icp-remaining-percent-threshold", cl::init(30), cl::Hidden,
29 cl::desc("The percentage threshold against remaining unpromoted indirect "
30 "call count for the promotion"));
31
32// The percent threshold for the direct-call target (this call site vs the
33// total call count) for it to be considered as the promotion target.
35 ICPTotalPercentThreshold("icp-total-percent-threshold", cl::init(5),
37 cl::desc("The percentage threshold against total "
38 "count for the promotion"));
39
40// Set the maximum number of targets to promote for a single indirect-call
41// callsite.
44 cl::desc("Max number of promotions for a single indirect "
45 "call callsite"));
46
48 "icp-max-num-vtables", cl::init(6), cl::Hidden,
49 cl::desc("Max number of vtables annotated for a vtable load instruction."));
50
51bool ICallPromotionAnalysis::isPromotionProfitable(uint64_t Count,
52 uint64_t TotalCount,
53 uint64_t RemainingCount) {
54 return Count * 100 >= ICPRemainingPercentThreshold * RemainingCount &&
55 Count * 100 >= ICPTotalPercentThreshold * TotalCount;
56}
57
58// Indirect-call promotion heuristic. The direct targets are sorted based on
59// the count. Stop at the first target that is not promoted. Returns the
60// number of candidates deemed profitable.
61uint32_t ICallPromotionAnalysis::getProfitablePromotionCandidates(
62 const Instruction *Inst, uint64_t TotalCount) {
63 LLVM_DEBUG(dbgs() << " \nWork on callsite " << *Inst
64 << " Num_targets: " << ValueDataArray.size() << "\n");
65
66 uint32_t I = 0;
67 uint64_t RemainingCount = TotalCount;
68 for (; I < MaxNumPromotions && I < ValueDataArray.size(); I++) {
69 uint64_t Count = ValueDataArray[I].Count;
70 assert(Count <= RemainingCount);
71 LLVM_DEBUG(dbgs() << " Candidate " << I << " Count=" << Count
72 << " Target_func: " << ValueDataArray[I].Value << "\n");
73
74 if (!isPromotionProfitable(Count, TotalCount, RemainingCount)) {
75 LLVM_DEBUG(dbgs() << " Not promote: Cold target.\n");
76 return I;
77 }
78 RemainingCount -= Count;
79 }
80 return I;
81}
82
85 const Instruction *I, uint64_t &TotalCount, uint32_t &NumCandidates) {
86 ValueDataArray = getValueProfDataFromInst(*I, IPVK_IndirectCallTarget,
87 MaxNumPromotions, TotalCount);
88 if (ValueDataArray.empty()) {
89 NumCandidates = 0;
91 }
92 NumCandidates = getProfitablePromotionCandidates(I, TotalCount);
93 return ValueDataArray;
94}
#define LLVM_DEBUG(...)
Definition: Debug.h:106
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< unsigned > ICPTotalPercentThreshold("icp-total-percent-threshold", cl::init(5), cl::Hidden, cl::desc("The percentage threshold against total " "count for the promotion"))
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."))
Interface to identify indirect call promotion candidates.
#define I(x, y, z)
Definition: MD5.cpp:58
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
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:310
bool empty() const
Definition: SmallVector.h:81
size_t size() const
Definition: SmallVector.h:78
LLVM Value Representation.
Definition: Value.h:74
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:443
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
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...
Definition: InstrProf.cpp:1369