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 #include <memory> 21 22 using namespace llvm; 23 24 #define DEBUG_TYPE "pgo-icall-prom-analysis" 25 26 // The percent threshold for the direct-call target (this call site vs the 27 // remaining call count) for it to be considered as the promotion target. 28 static cl::opt<unsigned> ICPRemainingPercentThreshold( 29 "icp-remaining-percent-threshold", cl::init(30), cl::Hidden, 30 cl::desc("The percentage threshold against remaining unpromoted indirect " 31 "call count for the promotion")); 32 33 // The percent threshold for the direct-call target (this call site vs the 34 // total call count) for it to be considered as the promotion target. 35 static cl::opt<unsigned> 36 ICPTotalPercentThreshold("icp-total-percent-threshold", cl::init(5), 37 cl::Hidden, 38 cl::desc("The percentage threshold against total " 39 "count for the promotion")); 40 41 // Set the maximum number of targets to promote for a single indirect-call 42 // callsite. 43 static cl::opt<unsigned> 44 MaxNumPromotions("icp-max-prom", cl::init(3), cl::Hidden, 45 cl::desc("Max number of promotions for a single indirect " 46 "call callsite")); 47 48 ICallPromotionAnalysis::ICallPromotionAnalysis() { 49 ValueDataArray = std::make_unique<InstrProfValueData[]>(MaxNumPromotions); 50 } 51 52 bool ICallPromotionAnalysis::isPromotionProfitable(uint64_t Count, 53 uint64_t TotalCount, 54 uint64_t RemainingCount) { 55 return Count * 100 >= ICPRemainingPercentThreshold * RemainingCount && 56 Count * 100 >= ICPTotalPercentThreshold * TotalCount; 57 } 58 59 // Indirect-call promotion heuristic. The direct targets are sorted based on 60 // the count. Stop at the first target that is not promoted. Returns the 61 // number of candidates deemed profitable. 62 uint32_t ICallPromotionAnalysis::getProfitablePromotionCandidates( 63 const Instruction *Inst, uint32_t NumVals, uint64_t TotalCount) { 64 ArrayRef<InstrProfValueData> ValueDataRef(ValueDataArray.get(), NumVals); 65 66 LLVM_DEBUG(dbgs() << " \nWork on callsite " << *Inst 67 << " Num_targets: " << NumVals << "\n"); 68 69 uint32_t I = 0; 70 uint64_t RemainingCount = TotalCount; 71 for (; I < MaxNumPromotions && I < NumVals; I++) { 72 uint64_t Count = ValueDataRef[I].Count; 73 assert(Count <= RemainingCount); 74 LLVM_DEBUG(dbgs() << " Candidate " << I << " Count=" << Count 75 << " Target_func: " << ValueDataRef[I].Value << "\n"); 76 77 if (!isPromotionProfitable(Count, TotalCount, RemainingCount)) { 78 LLVM_DEBUG(dbgs() << " Not promote: Cold target.\n"); 79 return I; 80 } 81 RemainingCount -= Count; 82 } 83 return I; 84 } 85 86 ArrayRef<InstrProfValueData> 87 ICallPromotionAnalysis::getPromotionCandidatesForInstruction( 88 const Instruction *I, uint32_t &NumVals, uint64_t &TotalCount, 89 uint32_t &NumCandidates) { 90 bool Res = 91 getValueProfDataFromInst(*I, IPVK_IndirectCallTarget, MaxNumPromotions, 92 ValueDataArray.get(), NumVals, TotalCount); 93 if (!Res) { 94 NumCandidates = 0; 95 return ArrayRef<InstrProfValueData>(); 96 } 97 NumCandidates = getProfitablePromotionCandidates(I, NumVals, TotalCount); 98 return ArrayRef<InstrProfValueData>(ValueDataArray.get(), NumVals); 99 } 100