106 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8//9// Helper methods for identifying profitable indirect call promotion10// candidates for an instruction when the indirect-call value profile metadata11// 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 21using namespace llvm;22 23#define DEBUG_TYPE "pgo-icall-prom-analysis"24 25namespace llvm {26 27// The percent threshold for the direct-call target (this call site vs the28// remaining call count) for it to be considered as the promotion target.29static cl::opt<unsigned> ICPRemainingPercentThreshold(30 "icp-remaining-percent-threshold", cl::init(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 the35// total call count) for it to be considered as the promotion target.36static cl::opt<uint64_t>37 ICPTotalPercentThreshold("icp-total-percent-threshold", cl::init(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.44static cl::opt<unsigned> ICPMinimumCountThreshold(45 "icp-minimum-count-threshold", cl::init(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-call49// callsite.50static cl::opt<unsigned>51 MaxNumPromotions("icp-max-prom", cl::init(3), cl::Hidden,52 cl::desc("Max number of promotions for a single indirect "53 "call callsite"));54 55cl::opt<unsigned> MaxNumVTableAnnotations(56 "icp-max-num-vtables", cl::init(6), cl::Hidden,57 cl::desc("Max number of vtables annotated for a vtable load instruction."));58 59} // end namespace llvm60 61bool 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 on70// the count. Stop at the first target that is not promoted. Returns the71// number of candidates deemed profitable.72uint32_t ICallPromotionAnalysis::getProfitablePromotionCandidates(73 const Instruction *Inst, uint64_t TotalCount) {74 LLVM_DEBUG(dbgs() << " \nWork on callsite " << *Inst75 << " 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=" << Count83 << " 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 94MutableArrayRef<InstrProfValueData>95ICallPromotionAnalysis::getPromotionCandidatesForInstruction(96 const Instruction *I, uint64_t &TotalCount, uint32_t &NumCandidates) {97 ValueDataArray = getValueProfDataFromInst(*I, IPVK_IndirectCallTarget,98 MaxNumPromotions, TotalCount);99 if (ValueDataArray.empty()) {100 NumCandidates = 0;101 return MutableArrayRef<InstrProfValueData>();102 }103 NumCandidates = getProfitablePromotionCandidates(I, TotalCount);104 return ValueDataArray;105}106