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 "AnalysisOptions.h"
17#include "llvm/IR/Instruction.h"
18#include "llvm/ProfileData/InstrProf.h"
19#include "llvm/Support/Debug.h"
20
21using namespace llvm;
22
23#define DEBUG_TYPE "pgo-icall-prom-analysis"
24
25unsigned llvm::getMaxNumVTableAnnotations(const LLVMContext &C) {
26 return getAnalysisOptions(C).icp_max_num_vtables;
27}
28
29bool ICallPromotionAnalysis::isPromotionProfitable(const AnalysisOptions &Opts,
30 uint64_t Count,
31 uint64_t TotalCount,
32 uint64_t RemainingCount) {
33 return Count >= Opts.icp_minimum_count_threshold &&
34 Count * 100 >= Opts.icp_remaining_percent_threshold * RemainingCount &&
35 Count * 100 >= Opts.icp_total_percent_threshold * TotalCount;
36}
37
38// Indirect-call promotion heuristic. The direct targets are sorted based on
39// the count. Stop at the first target that is not promoted. Returns the
40// number of candidates deemed profitable.
41uint32_t ICallPromotionAnalysis::getProfitablePromotionCandidates(
42 const AnalysisOptions &Opts, const Instruction *Inst, uint64_t TotalCount) {
43 LLVM_DEBUG(dbgs() << " \nWork on callsite " << *Inst
44 << " Num_targets: " << ValueDataArray.size() << "\n");
45
46 uint32_t I = 0;
47 uint64_t RemainingCount = TotalCount;
48 for (; I < Opts.icp_max_prom && I < ValueDataArray.size(); I++) {
49 uint64_t Count = ValueDataArray[I].Count;
50 assert(Count <= RemainingCount);
51 LLVM_DEBUG(dbgs() << " Candidate " << I << " Count=" << Count
52 << " Target_func: " << ValueDataArray[I].Value << "\n");
53
54 if (!isPromotionProfitable(Opts, Count, TotalCount, RemainingCount)) {
55 LLVM_DEBUG(dbgs() << " Not promote: Cold target.\n");
56 return I;
57 }
58 RemainingCount -= Count;
59 }
60 return I;
61}
62
63MutableArrayRef<InstrProfValueData>
64ICallPromotionAnalysis::getPromotionCandidatesForInstruction(
65 const Instruction *I, uint64_t &TotalCount, uint32_t &NumCandidates,
66 unsigned MaxNumValueData) {
67 const AnalysisOptions &Opts = getAnalysisOptions(F: *I->getFunction());
68 // Use the max of the values specified by -icp-max-prom and the provided
69 // MaxNumValueData parameter.
70 if (Opts.icp_max_prom > MaxNumValueData)
71 MaxNumValueData = Opts.icp_max_prom;
72 ValueDataArray = getValueProfDataFromInst(Inst: *I, ValueKind: IPVK_IndirectCallTarget,
73 MaxNumValueData, TotalC&: TotalCount);
74 if (ValueDataArray.empty()) {
75 NumCandidates = 0;
76 return MutableArrayRef<InstrProfValueData>();
77 }
78 NumCandidates = getProfitablePromotionCandidates(Opts, Inst: I, TotalCount);
79 return ValueDataArray;
80}
81