xref: /freebsd/contrib/llvm-project/llvm/lib/Analysis/IndirectCallPromotionAnalysis.cpp (revision 770cf0a5f02dc8983a89c6568d741fbc25baa999)
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 
15 #include "llvm/Analysis/IndirectCallPromotionAnalysis.h"
16 #include "llvm/IR/Instruction.h"
17 #include "llvm/ProfileData/InstrProf.h"
18 #include "llvm/Support/CommandLine.h"
19 #include "llvm/Support/Debug.h"
20 
21 using 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.
27 static cl::opt<unsigned> ICPRemainingPercentThreshold(
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.
34 static cl::opt<uint64_t>
35     ICPTotalPercentThreshold("icp-total-percent-threshold", cl::init(5),
36                              cl::Hidden,
37                              cl::desc("The percentage threshold against total "
38                                       "count for the promotion"));
39 
40 // Set the minimum absolute count threshold for indirect call promotion.
41 // Candidates with counts below this threshold will not be promoted.
42 static cl::opt<unsigned> ICPMinimumCountThreshold(
43     "icp-minimum-count-threshold", cl::init(0), cl::Hidden,
44     cl::desc("Minimum absolute count for promotion candidate"));
45 
46 // Set the maximum number of targets to promote for a single indirect-call
47 // callsite.
48 static cl::opt<unsigned>
49     MaxNumPromotions("icp-max-prom", cl::init(3), cl::Hidden,
50                      cl::desc("Max number of promotions for a single indirect "
51                               "call callsite"));
52 
53 cl::opt<unsigned> MaxNumVTableAnnotations(
54     "icp-max-num-vtables", cl::init(6), cl::Hidden,
55     cl::desc("Max number of vtables annotated for a vtable load instruction."));
56 
57 bool ICallPromotionAnalysis::isPromotionProfitable(uint64_t Count,
58                                                    uint64_t TotalCount,
59                                                    uint64_t RemainingCount) {
60   return Count >= ICPMinimumCountThreshold &&
61          Count * 100 >= ICPRemainingPercentThreshold * RemainingCount &&
62          Count * 100 >= ICPTotalPercentThreshold * TotalCount;
63 }
64 
65 // Indirect-call promotion heuristic. The direct targets are sorted based on
66 // the count. Stop at the first target that is not promoted. Returns the
67 // number of candidates deemed profitable.
68 uint32_t ICallPromotionAnalysis::getProfitablePromotionCandidates(
69     const Instruction *Inst, uint64_t TotalCount) {
70   LLVM_DEBUG(dbgs() << " \nWork on callsite " << *Inst
71                     << " Num_targets: " << ValueDataArray.size() << "\n");
72 
73   uint32_t I = 0;
74   uint64_t RemainingCount = TotalCount;
75   for (; I < MaxNumPromotions && I < ValueDataArray.size(); I++) {
76     uint64_t Count = ValueDataArray[I].Count;
77     assert(Count <= RemainingCount);
78     LLVM_DEBUG(dbgs() << " Candidate " << I << " Count=" << Count
79                       << "  Target_func: " << ValueDataArray[I].Value << "\n");
80 
81     if (!isPromotionProfitable(Count, TotalCount, RemainingCount)) {
82       LLVM_DEBUG(dbgs() << " Not promote: Cold target.\n");
83       return I;
84     }
85     RemainingCount -= Count;
86   }
87   return I;
88 }
89 
90 MutableArrayRef<InstrProfValueData>
91 ICallPromotionAnalysis::getPromotionCandidatesForInstruction(
92     const Instruction *I, uint64_t &TotalCount, uint32_t &NumCandidates) {
93   ValueDataArray = getValueProfDataFromInst(*I, IPVK_IndirectCallTarget,
94                                             MaxNumPromotions, TotalCount);
95   if (ValueDataArray.empty()) {
96     NumCandidates = 0;
97     return MutableArrayRef<InstrProfValueData>();
98   }
99   NumCandidates = getProfitablePromotionCandidates(I, TotalCount);
100   return ValueDataArray;
101 }
102