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"
19#include "llvm/Support/Debug.h"
20
21using namespace llvm;
22
23#define DEBUG_TYPE "pgo-icall-prom-analysis"
24
26 return getAnalysisOptions(C).icp_max_num_vtables;
27}
28
29bool ICallPromotionAnalysis::isPromotionProfitable(const AnalysisOptions &Opts,
31 uint64_t TotalCount,
32 uint64_t RemainingCount) {
33 return Count >= Opts.icp_minimum_count_threshold &&
34 Count * 100 >= Opts.icp_remaining_percent_threshold * RemainingCount &&
35 Count * 100 >= Opts.icp_total_percent_threshold * TotalCount;
36}
37
38// Indirect-call promotion heuristic. The direct targets are sorted based on
39// the count. Stop at the first target that is not promoted. Returns the
40// number of candidates deemed profitable.
41uint32_t ICallPromotionAnalysis::getProfitablePromotionCandidates(
42 const AnalysisOptions &Opts, const Instruction *Inst, uint64_t TotalCount) {
43 LLVM_DEBUG(dbgs() << " \nWork on callsite " << *Inst
44 << " Num_targets: " << ValueDataArray.size() << "\n");
45
46 uint32_t I = 0;
47 uint64_t RemainingCount = TotalCount;
48 for (; I < Opts.icp_max_prom && I < ValueDataArray.size(); I++) {
49 uint64_t Count = ValueDataArray[I].Count;
50 assert(Count <= RemainingCount);
51 LLVM_DEBUG(dbgs() << " Candidate " << I << " Count=" << Count
52 << " Target_func: " << ValueDataArray[I].Value << "\n");
53
54 if (!isPromotionProfitable(Opts, Count, TotalCount, RemainingCount)) {
55 LLVM_DEBUG(dbgs() << " Not promote: Cold target.\n");
56 return I;
57 }
58 RemainingCount -= Count;
59 }
60 return I;
61}
62
65 const Instruction *I, uint64_t &TotalCount, uint32_t &NumCandidates,
66 unsigned MaxNumValueData) {
67 const AnalysisOptions &Opts = getAnalysisOptions(*I->getFunction());
68 // Use the max of the values specified by -icp-max-prom and the provided
69 // MaxNumValueData parameter.
70 if (Opts.icp_max_prom > MaxNumValueData)
71 MaxNumValueData = Opts.icp_max_prom;
72 ValueDataArray = getValueProfDataFromInst(*I, IPVK_IndirectCallTarget,
73 MaxNumValueData, TotalCount);
74 if (ValueDataArray.empty()) {
75 NumCandidates = 0;
77 }
78 NumCandidates = getProfitablePromotionCandidates(Opts, I, TotalCount);
79 return ValueDataArray;
80}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
uint64_t uint64_t
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
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.
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:76
Represent a mutable reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:294
This is an optimization pass for GlobalISel generic memory operations.
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
LLVM_ABI unsigned getMaxNumVTableAnnotations(const LLVMContext &C)
Return -icp-max-num-vtables.