1*0b57cec5SDimitry Andric //===-- IndirectCallPromotionAnalysis.cpp - Find promotion candidates ===//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //
7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric //
9*0b57cec5SDimitry Andric // Helper methods for identifying profitable indirect call promotion
10*0b57cec5SDimitry Andric // candidates for an instruction when the indirect-call value profile metadata
11*0b57cec5SDimitry Andric // is available.
12*0b57cec5SDimitry Andric //
13*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
14*0b57cec5SDimitry Andric 
15*0b57cec5SDimitry Andric #include "llvm/Analysis/IndirectCallPromotionAnalysis.h"
16*0b57cec5SDimitry Andric #include "llvm/IR/Instruction.h"
17*0b57cec5SDimitry Andric #include "llvm/ProfileData/InstrProf.h"
18*0b57cec5SDimitry Andric #include "llvm/Support/CommandLine.h"
19*0b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
20*0b57cec5SDimitry Andric #include <memory>
21*0b57cec5SDimitry Andric 
22*0b57cec5SDimitry Andric using namespace llvm;
23*0b57cec5SDimitry Andric 
24*0b57cec5SDimitry Andric #define DEBUG_TYPE "pgo-icall-prom-analysis"
25*0b57cec5SDimitry Andric 
26*0b57cec5SDimitry Andric // The percent threshold for the direct-call target (this call site vs the
27*0b57cec5SDimitry Andric // remaining call count) for it to be considered as the promotion target.
28*0b57cec5SDimitry Andric static cl::opt<unsigned> ICPRemainingPercentThreshold(
29*0b57cec5SDimitry Andric     "icp-remaining-percent-threshold", cl::init(30), cl::Hidden,
30*0b57cec5SDimitry Andric     cl::desc("The percentage threshold against remaining unpromoted indirect "
31*0b57cec5SDimitry Andric              "call count for the promotion"));
32*0b57cec5SDimitry Andric 
33*0b57cec5SDimitry Andric // The percent threshold for the direct-call target (this call site vs the
34*0b57cec5SDimitry Andric // total call count) for it to be considered as the promotion target.
35*0b57cec5SDimitry Andric static cl::opt<unsigned>
36*0b57cec5SDimitry Andric     ICPTotalPercentThreshold("icp-total-percent-threshold", cl::init(5),
37*0b57cec5SDimitry Andric                              cl::Hidden,
38*0b57cec5SDimitry Andric                              cl::desc("The percentage threshold against total "
39*0b57cec5SDimitry Andric                                       "count for the promotion"));
40*0b57cec5SDimitry Andric 
41*0b57cec5SDimitry Andric // Set the maximum number of targets to promote for a single indirect-call
42*0b57cec5SDimitry Andric // callsite.
43*0b57cec5SDimitry Andric static cl::opt<unsigned>
44*0b57cec5SDimitry Andric     MaxNumPromotions("icp-max-prom", cl::init(3), cl::Hidden,
45*0b57cec5SDimitry Andric                      cl::desc("Max number of promotions for a single indirect "
46*0b57cec5SDimitry Andric                               "call callsite"));
47*0b57cec5SDimitry Andric 
ICallPromotionAnalysis()48*0b57cec5SDimitry Andric ICallPromotionAnalysis::ICallPromotionAnalysis() {
49*0b57cec5SDimitry Andric   ValueDataArray = std::make_unique<InstrProfValueData[]>(MaxNumPromotions);
50*0b57cec5SDimitry Andric }
51*0b57cec5SDimitry Andric 
isPromotionProfitable(uint64_t Count,uint64_t TotalCount,uint64_t RemainingCount)52*0b57cec5SDimitry Andric bool ICallPromotionAnalysis::isPromotionProfitable(uint64_t Count,
53*0b57cec5SDimitry Andric                                                    uint64_t TotalCount,
54*0b57cec5SDimitry Andric                                                    uint64_t RemainingCount) {
55*0b57cec5SDimitry Andric   return Count * 100 >= ICPRemainingPercentThreshold * RemainingCount &&
56*0b57cec5SDimitry Andric          Count * 100 >= ICPTotalPercentThreshold * TotalCount;
57*0b57cec5SDimitry Andric }
58*0b57cec5SDimitry Andric 
59*0b57cec5SDimitry Andric // Indirect-call promotion heuristic. The direct targets are sorted based on
60*0b57cec5SDimitry Andric // the count. Stop at the first target that is not promoted. Returns the
61*0b57cec5SDimitry Andric // number of candidates deemed profitable.
getProfitablePromotionCandidates(const Instruction * Inst,uint32_t NumVals,uint64_t TotalCount)62*0b57cec5SDimitry Andric uint32_t ICallPromotionAnalysis::getProfitablePromotionCandidates(
63*0b57cec5SDimitry Andric     const Instruction *Inst, uint32_t NumVals, uint64_t TotalCount) {
64*0b57cec5SDimitry Andric   ArrayRef<InstrProfValueData> ValueDataRef(ValueDataArray.get(), NumVals);
65*0b57cec5SDimitry Andric 
66*0b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << " \nWork on callsite " << *Inst
67*0b57cec5SDimitry Andric                     << " Num_targets: " << NumVals << "\n");
68*0b57cec5SDimitry Andric 
69*0b57cec5SDimitry Andric   uint32_t I = 0;
70*0b57cec5SDimitry Andric   uint64_t RemainingCount = TotalCount;
71*0b57cec5SDimitry Andric   for (; I < MaxNumPromotions && I < NumVals; I++) {
72*0b57cec5SDimitry Andric     uint64_t Count = ValueDataRef[I].Count;
73*0b57cec5SDimitry Andric     assert(Count <= RemainingCount);
74*0b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << " Candidate " << I << " Count=" << Count
75*0b57cec5SDimitry Andric                       << "  Target_func: " << ValueDataRef[I].Value << "\n");
76*0b57cec5SDimitry Andric 
77*0b57cec5SDimitry Andric     if (!isPromotionProfitable(Count, TotalCount, RemainingCount)) {
78*0b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << " Not promote: Cold target.\n");
79*0b57cec5SDimitry Andric       return I;
80*0b57cec5SDimitry Andric     }
81*0b57cec5SDimitry Andric     RemainingCount -= Count;
82*0b57cec5SDimitry Andric   }
83*0b57cec5SDimitry Andric   return I;
84*0b57cec5SDimitry Andric }
85*0b57cec5SDimitry Andric 
86*0b57cec5SDimitry Andric ArrayRef<InstrProfValueData>
getPromotionCandidatesForInstruction(const Instruction * I,uint32_t & NumVals,uint64_t & TotalCount,uint32_t & NumCandidates)87*0b57cec5SDimitry Andric ICallPromotionAnalysis::getPromotionCandidatesForInstruction(
88*0b57cec5SDimitry Andric     const Instruction *I, uint32_t &NumVals, uint64_t &TotalCount,
89*0b57cec5SDimitry Andric     uint32_t &NumCandidates) {
90*0b57cec5SDimitry Andric   bool Res =
91*0b57cec5SDimitry Andric       getValueProfDataFromInst(*I, IPVK_IndirectCallTarget, MaxNumPromotions,
92*0b57cec5SDimitry Andric                                ValueDataArray.get(), NumVals, TotalCount);
93*0b57cec5SDimitry Andric   if (!Res) {
94*0b57cec5SDimitry Andric     NumCandidates = 0;
95*0b57cec5SDimitry Andric     return ArrayRef<InstrProfValueData>();
96*0b57cec5SDimitry Andric   }
97*0b57cec5SDimitry Andric   NumCandidates = getProfitablePromotionCandidates(I, NumVals, TotalCount);
98*0b57cec5SDimitry Andric   return ArrayRef<InstrProfValueData>(ValueDataArray.get(), NumVals);
99*0b57cec5SDimitry Andric }
100*0b57cec5SDimitry Andric