| 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 | namespace llvm { |
| 26 | |
| 27 | // The percent threshold for the direct-call target (this call site vs the |
| 28 | // remaining call count) for it to be considered as the promotion target. |
| 29 | static cl::opt<unsigned> ICPRemainingPercentThreshold( |
| 30 | "icp-remaining-percent-threshold" , cl::init(Val: 30), cl::Hidden, |
| 31 | cl::desc("The percentage threshold against remaining unpromoted indirect " |
| 32 | "call count for the promotion" )); |
| 33 | |
| 34 | // The percent threshold for the direct-call target (this call site vs the |
| 35 | // total call count) for it to be considered as the promotion target. |
| 36 | static cl::opt<uint64_t> |
| 37 | ICPTotalPercentThreshold("icp-total-percent-threshold" , cl::init(Val: 5), |
| 38 | cl::Hidden, |
| 39 | cl::desc("The percentage threshold against total " |
| 40 | "count for the promotion" )); |
| 41 | |
| 42 | // Set the minimum absolute count threshold for indirect call promotion. |
| 43 | // Candidates with counts below this threshold will not be promoted. |
| 44 | static cl::opt<unsigned> ICPMinimumCountThreshold( |
| 45 | "icp-minimum-count-threshold" , cl::init(Val: 0), cl::Hidden, |
| 46 | cl::desc("Minimum absolute count for promotion candidate" )); |
| 47 | |
| 48 | // Set the maximum number of targets to promote for a single indirect-call |
| 49 | // callsite. |
| 50 | static cl::opt<unsigned> |
| 51 | MaxNumPromotions("icp-max-prom" , cl::init(Val: 3), cl::Hidden, |
| 52 | cl::desc("Max number of promotions for a single indirect " |
| 53 | "call callsite" )); |
| 54 | |
| 55 | cl::opt<unsigned> MaxNumVTableAnnotations( |
| 56 | "icp-max-num-vtables" , cl::init(Val: 6), cl::Hidden, |
| 57 | cl::desc("Max number of vtables annotated for a vtable load instruction." )); |
| 58 | |
| 59 | } // end namespace llvm |
| 60 | |
| 61 | bool ICallPromotionAnalysis::isPromotionProfitable(uint64_t Count, |
| 62 | uint64_t TotalCount, |
| 63 | uint64_t RemainingCount) { |
| 64 | return Count >= ICPMinimumCountThreshold && |
| 65 | Count * 100 >= ICPRemainingPercentThreshold * RemainingCount && |
| 66 | Count * 100 >= ICPTotalPercentThreshold * TotalCount; |
| 67 | } |
| 68 | |
| 69 | // Indirect-call promotion heuristic. The direct targets are sorted based on |
| 70 | // the count. Stop at the first target that is not promoted. Returns the |
| 71 | // number of candidates deemed profitable. |
| 72 | uint32_t ICallPromotionAnalysis::getProfitablePromotionCandidates( |
| 73 | const Instruction *Inst, uint64_t TotalCount) { |
| 74 | LLVM_DEBUG(dbgs() << " \nWork on callsite " << *Inst |
| 75 | << " Num_targets: " << ValueDataArray.size() << "\n" ); |
| 76 | |
| 77 | uint32_t I = 0; |
| 78 | uint64_t RemainingCount = TotalCount; |
| 79 | for (; I < MaxNumPromotions && I < ValueDataArray.size(); I++) { |
| 80 | uint64_t Count = ValueDataArray[I].Count; |
| 81 | assert(Count <= RemainingCount); |
| 82 | LLVM_DEBUG(dbgs() << " Candidate " << I << " Count=" << Count |
| 83 | << " Target_func: " << ValueDataArray[I].Value << "\n" ); |
| 84 | |
| 85 | if (!isPromotionProfitable(Count, TotalCount, RemainingCount)) { |
| 86 | LLVM_DEBUG(dbgs() << " Not promote: Cold target.\n" ); |
| 87 | return I; |
| 88 | } |
| 89 | RemainingCount -= Count; |
| 90 | } |
| 91 | return I; |
| 92 | } |
| 93 | |
| 94 | MutableArrayRef<InstrProfValueData> |
| 95 | ICallPromotionAnalysis::getPromotionCandidatesForInstruction( |
| 96 | const Instruction *I, uint64_t &TotalCount, uint32_t &NumCandidates, |
| 97 | unsigned MaxNumValueData) { |
| 98 | // Use the max of the values specified by -icp-max-prom and the provided |
| 99 | // MaxNumValueData parameter. |
| 100 | if (MaxNumPromotions > MaxNumValueData) |
| 101 | MaxNumValueData = MaxNumPromotions; |
| 102 | ValueDataArray = getValueProfDataFromInst(Inst: *I, ValueKind: IPVK_IndirectCallTarget, |
| 103 | MaxNumValueData, TotalC&: TotalCount); |
| 104 | if (ValueDataArray.empty()) { |
| 105 | NumCandidates = 0; |
| 106 | return MutableArrayRef<InstrProfValueData>(); |
| 107 | } |
| 108 | NumCandidates = getProfitablePromotionCandidates(Inst: I, TotalCount); |
| 109 | return ValueDataArray; |
| 110 | } |
| 111 | |