LLVM 24.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 "AnalysisOptions.h"
17#include "llvm/IR/Instruction.h"
20#include "llvm/Support/Debug.h"
21
22using namespace llvm;
23
24#define DEBUG_TYPE "pgo-icall-prom-analysis"
25
26namespace llvm {
27
29 "icp-max-num-vtables", cl::init(6), cl::Hidden,
30 cl::desc("Max number of vtables annotated for a vtable load instruction."));
31
32} // end namespace llvm
33
34bool ICallPromotionAnalysis::isPromotionProfitable(const AnalysisOptions &Opts,
36 uint64_t TotalCount,
37 uint64_t RemainingCount) {
38 return Count >= Opts.icp_minimum_count_threshold &&
39 Count * 100 >= Opts.icp_remaining_percent_threshold * RemainingCount &&
40 Count * 100 >= Opts.icp_total_percent_threshold * TotalCount;
41}
42
43// Indirect-call promotion heuristic. The direct targets are sorted based on
44// the count. Stop at the first target that is not promoted. Returns the
45// number of candidates deemed profitable.
46uint32_t ICallPromotionAnalysis::getProfitablePromotionCandidates(
47 const AnalysisOptions &Opts, const Instruction *Inst, uint64_t TotalCount) {
48 LLVM_DEBUG(dbgs() << " \nWork on callsite " << *Inst
49 << " Num_targets: " << ValueDataArray.size() << "\n");
50
51 uint32_t I = 0;
52 uint64_t RemainingCount = TotalCount;
53 for (; I < Opts.icp_max_prom && I < ValueDataArray.size(); I++) {
54 uint64_t Count = ValueDataArray[I].Count;
55 assert(Count <= RemainingCount);
56 LLVM_DEBUG(dbgs() << " Candidate " << I << " Count=" << Count
57 << " Target_func: " << ValueDataArray[I].Value << "\n");
58
59 if (!isPromotionProfitable(Opts, Count, TotalCount, RemainingCount)) {
60 LLVM_DEBUG(dbgs() << " Not promote: Cold target.\n");
61 return I;
62 }
63 RemainingCount -= Count;
64 }
65 return I;
66}
67
70 const Instruction *I, uint64_t &TotalCount, uint32_t &NumCandidates,
71 unsigned MaxNumValueData) {
72 const AnalysisOptions &Opts = getAnalysisOptions(*I->getFunction());
73 // Use the max of the values specified by -icp-max-prom and the provided
74 // MaxNumValueData parameter.
75 if (Opts.icp_max_prom > MaxNumValueData)
76 MaxNumValueData = Opts.icp_max_prom;
77 ValueDataArray = getValueProfDataFromInst(*I, IPVK_IndirectCallTarget,
78 MaxNumValueData, TotalCount);
79 if (ValueDataArray.empty()) {
80 NumCandidates = 0;
82 }
83 NumCandidates = getProfitablePromotionCandidates(Opts, I, TotalCount);
84 return ValueDataArray;
85}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
uint64_t uint64_t
Interface to identify indirect call promotion candidates.
#define I(x, y, z)
Definition MD5.cpp:57
#define LLVM_DEBUG(...)
Definition Debug.h:119
LLVM_ABI MutableArrayRef< InstrProfValueData > getPromotionCandidatesForInstruction(const Instruction *I, uint64_t &TotalCount, uint32_t &NumCandidates, unsigned MaxNumValueData=0)
Returns reference to array of InstrProfValueData for the given instruction I.
Represent a mutable reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:294
initializer< Ty > init(const Ty &Val)
This is an optimization pass for GlobalISel generic memory operations.
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."))
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Value
Definition InstrProf.h:143
const AnalysisOptions & getAnalysisOptions(const LLVMContext &C)
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
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 >
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Count
Definition InstrProf.h:145