Path: blob/main/contrib/llvm-project/llvm/lib/Analysis/IndirectCallPromotionAnalysis.cpp
35233 views
//===-- IndirectCallPromotionAnalysis.cpp - Find promotion candidates ===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// Helper methods for identifying profitable indirect call promotion9// candidates for an instruction when the indirect-call value profile metadata10// is available.11//12//===----------------------------------------------------------------------===//1314#include "llvm/Analysis/IndirectCallPromotionAnalysis.h"15#include "llvm/IR/Instruction.h"16#include "llvm/ProfileData/InstrProf.h"17#include "llvm/Support/CommandLine.h"18#include "llvm/Support/Debug.h"19#include <memory>2021using namespace llvm;2223#define DEBUG_TYPE "pgo-icall-prom-analysis"2425// The percent threshold for the direct-call target (this call site vs the26// remaining call count) for it to be considered as the promotion target.27static cl::opt<unsigned> ICPRemainingPercentThreshold(28"icp-remaining-percent-threshold", cl::init(30), cl::Hidden,29cl::desc("The percentage threshold against remaining unpromoted indirect "30"call count for the promotion"));3132// The percent threshold for the direct-call target (this call site vs the33// total call count) for it to be considered as the promotion target.34static cl::opt<unsigned>35ICPTotalPercentThreshold("icp-total-percent-threshold", cl::init(5),36cl::Hidden,37cl::desc("The percentage threshold against total "38"count for the promotion"));3940// Set the maximum number of targets to promote for a single indirect-call41// callsite.42static cl::opt<unsigned>43MaxNumPromotions("icp-max-prom", cl::init(3), cl::Hidden,44cl::desc("Max number of promotions for a single indirect "45"call callsite"));4647cl::opt<unsigned> MaxNumVTableAnnotations(48"icp-max-num-vtables", cl::init(6), cl::Hidden,49cl::desc("Max number of vtables annotated for a vtable load instruction."));5051bool ICallPromotionAnalysis::isPromotionProfitable(uint64_t Count,52uint64_t TotalCount,53uint64_t RemainingCount) {54return Count * 100 >= ICPRemainingPercentThreshold * RemainingCount &&55Count * 100 >= ICPTotalPercentThreshold * TotalCount;56}5758// Indirect-call promotion heuristic. The direct targets are sorted based on59// the count. Stop at the first target that is not promoted. Returns the60// number of candidates deemed profitable.61uint32_t ICallPromotionAnalysis::getProfitablePromotionCandidates(62const Instruction *Inst, uint64_t TotalCount) {63LLVM_DEBUG(dbgs() << " \nWork on callsite " << *Inst64<< " Num_targets: " << ValueDataArray.size() << "\n");6566uint32_t I = 0;67uint64_t RemainingCount = TotalCount;68for (; I < MaxNumPromotions && I < ValueDataArray.size(); I++) {69uint64_t Count = ValueDataArray[I].Count;70assert(Count <= RemainingCount);71LLVM_DEBUG(dbgs() << " Candidate " << I << " Count=" << Count72<< " Target_func: " << ValueDataArray[I].Value << "\n");7374if (!isPromotionProfitable(Count, TotalCount, RemainingCount)) {75LLVM_DEBUG(dbgs() << " Not promote: Cold target.\n");76return I;77}78RemainingCount -= Count;79}80return I;81}8283MutableArrayRef<InstrProfValueData>84ICallPromotionAnalysis::getPromotionCandidatesForInstruction(85const Instruction *I, uint64_t &TotalCount, uint32_t &NumCandidates) {86ValueDataArray = getValueProfDataFromInst(*I, IPVK_IndirectCallTarget,87MaxNumPromotions, TotalCount);88if (ValueDataArray.empty()) {89NumCandidates = 0;90return MutableArrayRef<InstrProfValueData>();91}92NumCandidates = getProfitablePromotionCandidates(I, TotalCount);93return ValueDataArray;94}959697