xref: /aosp_15_r20/external/llvm/lib/Analysis/IndirectCallPromotionAnalysis.cpp (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker //===-- IndirectCallPromotionAnalysis.cpp - Find promotion candidates ===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker //                      The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker // Helper methods for identifying profitable indirect call promotion
11*9880d681SAndroid Build Coastguard Worker // candidates for an instruction when the indirect-call value profile metadata
12*9880d681SAndroid Build Coastguard Worker // is available.
13*9880d681SAndroid Build Coastguard Worker //
14*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
15*9880d681SAndroid Build Coastguard Worker 
16*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/IndirectCallPromotionAnalysis.h"
17*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/STLExtras.h"
18*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/IndirectCallSiteVisitor.h"
19*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/CallSite.h"
20*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/DiagnosticInfo.h"
21*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/InstIterator.h"
22*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/InstVisitor.h"
23*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Instructions.h"
24*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/IntrinsicInst.h"
25*9880d681SAndroid Build Coastguard Worker #include "llvm/ProfileData/InstrProf.h"
26*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/Debug.h"
27*9880d681SAndroid Build Coastguard Worker #include <string>
28*9880d681SAndroid Build Coastguard Worker #include <utility>
29*9880d681SAndroid Build Coastguard Worker #include <vector>
30*9880d681SAndroid Build Coastguard Worker 
31*9880d681SAndroid Build Coastguard Worker using namespace llvm;
32*9880d681SAndroid Build Coastguard Worker 
33*9880d681SAndroid Build Coastguard Worker #define DEBUG_TYPE "pgo-icall-prom-analysis"
34*9880d681SAndroid Build Coastguard Worker 
35*9880d681SAndroid Build Coastguard Worker // The minimum call count for the direct-call target to be considered as the
36*9880d681SAndroid Build Coastguard Worker // promotion candidate.
37*9880d681SAndroid Build Coastguard Worker static cl::opt<unsigned>
38*9880d681SAndroid Build Coastguard Worker     ICPCountThreshold("icp-count-threshold", cl::Hidden, cl::ZeroOrMore,
39*9880d681SAndroid Build Coastguard Worker                       cl::init(1000),
40*9880d681SAndroid Build Coastguard Worker                       cl::desc("The minimum count to the direct call target "
41*9880d681SAndroid Build Coastguard Worker                                "for the promotion"));
42*9880d681SAndroid Build Coastguard Worker 
43*9880d681SAndroid Build Coastguard Worker // The percent threshold for the direct-call target (this call site vs the
44*9880d681SAndroid Build Coastguard Worker // total call count) for it to be considered as the promotion target.
45*9880d681SAndroid Build Coastguard Worker static cl::opt<unsigned>
46*9880d681SAndroid Build Coastguard Worker     ICPPercentThreshold("icp-percent-threshold", cl::init(33), cl::Hidden,
47*9880d681SAndroid Build Coastguard Worker                         cl::ZeroOrMore,
48*9880d681SAndroid Build Coastguard Worker                         cl::desc("The percentage threshold for the promotion"));
49*9880d681SAndroid Build Coastguard Worker 
50*9880d681SAndroid Build Coastguard Worker // Set the maximum number of targets to promote for a single indirect-call
51*9880d681SAndroid Build Coastguard Worker // callsite.
52*9880d681SAndroid Build Coastguard Worker static cl::opt<unsigned>
53*9880d681SAndroid Build Coastguard Worker     MaxNumPromotions("icp-max-prom", cl::init(2), cl::Hidden, cl::ZeroOrMore,
54*9880d681SAndroid Build Coastguard Worker                      cl::desc("Max number of promotions for a single indirect "
55*9880d681SAndroid Build Coastguard Worker                               "call callsite"));
56*9880d681SAndroid Build Coastguard Worker 
ICallPromotionAnalysis()57*9880d681SAndroid Build Coastguard Worker ICallPromotionAnalysis::ICallPromotionAnalysis() {
58*9880d681SAndroid Build Coastguard Worker   ValueDataArray = llvm::make_unique<InstrProfValueData[]>(MaxNumPromotions);
59*9880d681SAndroid Build Coastguard Worker }
60*9880d681SAndroid Build Coastguard Worker 
isPromotionProfitable(uint64_t Count,uint64_t TotalCount)61*9880d681SAndroid Build Coastguard Worker bool ICallPromotionAnalysis::isPromotionProfitable(uint64_t Count,
62*9880d681SAndroid Build Coastguard Worker                                                    uint64_t TotalCount) {
63*9880d681SAndroid Build Coastguard Worker   if (Count < ICPCountThreshold)
64*9880d681SAndroid Build Coastguard Worker     return false;
65*9880d681SAndroid Build Coastguard Worker 
66*9880d681SAndroid Build Coastguard Worker   unsigned Percentage = (Count * 100) / TotalCount;
67*9880d681SAndroid Build Coastguard Worker   return (Percentage >= ICPPercentThreshold);
68*9880d681SAndroid Build Coastguard Worker }
69*9880d681SAndroid Build Coastguard Worker 
70*9880d681SAndroid Build Coastguard Worker // Indirect-call promotion heuristic. The direct targets are sorted based on
71*9880d681SAndroid Build Coastguard Worker // the count. Stop at the first target that is not promoted. Returns the
72*9880d681SAndroid Build Coastguard Worker // number of candidates deemed profitable.
getProfitablePromotionCandidates(const Instruction * Inst,uint32_t NumVals,uint64_t TotalCount)73*9880d681SAndroid Build Coastguard Worker uint32_t ICallPromotionAnalysis::getProfitablePromotionCandidates(
74*9880d681SAndroid Build Coastguard Worker     const Instruction *Inst, uint32_t NumVals, uint64_t TotalCount) {
75*9880d681SAndroid Build Coastguard Worker   ArrayRef<InstrProfValueData> ValueDataRef(ValueDataArray.get(), NumVals);
76*9880d681SAndroid Build Coastguard Worker 
77*9880d681SAndroid Build Coastguard Worker   DEBUG(dbgs() << " \nWork on callsite " << *Inst << " Num_targets: " << NumVals
78*9880d681SAndroid Build Coastguard Worker                << "\n");
79*9880d681SAndroid Build Coastguard Worker 
80*9880d681SAndroid Build Coastguard Worker   uint32_t I = 0;
81*9880d681SAndroid Build Coastguard Worker   for (; I < MaxNumPromotions && I < NumVals; I++) {
82*9880d681SAndroid Build Coastguard Worker     uint64_t Count = ValueDataRef[I].Count;
83*9880d681SAndroid Build Coastguard Worker     assert(Count <= TotalCount);
84*9880d681SAndroid Build Coastguard Worker     DEBUG(dbgs() << " Candidate " << I << " Count=" << Count
85*9880d681SAndroid Build Coastguard Worker                  << "  Target_func: " << ValueDataRef[I].Value << "\n");
86*9880d681SAndroid Build Coastguard Worker 
87*9880d681SAndroid Build Coastguard Worker     if (!isPromotionProfitable(Count, TotalCount)) {
88*9880d681SAndroid Build Coastguard Worker       DEBUG(dbgs() << " Not promote: Cold target.\n");
89*9880d681SAndroid Build Coastguard Worker       return I;
90*9880d681SAndroid Build Coastguard Worker     }
91*9880d681SAndroid Build Coastguard Worker     TotalCount -= Count;
92*9880d681SAndroid Build Coastguard Worker   }
93*9880d681SAndroid Build Coastguard Worker   return I;
94*9880d681SAndroid Build Coastguard Worker }
95*9880d681SAndroid Build Coastguard Worker 
96*9880d681SAndroid Build Coastguard Worker ArrayRef<InstrProfValueData>
getPromotionCandidatesForInstruction(const Instruction * I,uint32_t & NumVals,uint64_t & TotalCount,uint32_t & NumCandidates)97*9880d681SAndroid Build Coastguard Worker ICallPromotionAnalysis::getPromotionCandidatesForInstruction(
98*9880d681SAndroid Build Coastguard Worker     const Instruction *I, uint32_t &NumVals, uint64_t &TotalCount,
99*9880d681SAndroid Build Coastguard Worker     uint32_t &NumCandidates) {
100*9880d681SAndroid Build Coastguard Worker   bool Res =
101*9880d681SAndroid Build Coastguard Worker       getValueProfDataFromInst(*I, IPVK_IndirectCallTarget, MaxNumPromotions,
102*9880d681SAndroid Build Coastguard Worker                                ValueDataArray.get(), NumVals, TotalCount);
103*9880d681SAndroid Build Coastguard Worker   if (!Res) {
104*9880d681SAndroid Build Coastguard Worker     NumCandidates = 0;
105*9880d681SAndroid Build Coastguard Worker     return ArrayRef<InstrProfValueData>();
106*9880d681SAndroid Build Coastguard Worker   }
107*9880d681SAndroid Build Coastguard Worker   NumCandidates = getProfitablePromotionCandidates(I, NumVals, TotalCount);
108*9880d681SAndroid Build Coastguard Worker   return ArrayRef<InstrProfValueData>(ValueDataArray.get(), NumVals);
109*9880d681SAndroid Build Coastguard Worker }
110