blob: 62db61be252a8a7ad13083ec4ff4238a8b12fdb9 [file] [log] [blame]
Teresa Johnson1e44b5d2016-07-12 21:13:44 +00001//===-- IndirectCallPromotionAnalysis.cpp - Find promotion candidates ===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// Helper methods for identifying profitable indirect call promotion
11// candidates for an instruction when the indirect-call value profile metadata
12// is available.
13//
14//===----------------------------------------------------------------------===//
15
16#include "llvm/Analysis/IndirectCallPromotionAnalysis.h"
17#include "llvm/ADT/STLExtras.h"
18#include "llvm/Analysis/IndirectCallSiteVisitor.h"
19#include "llvm/IR/CallSite.h"
20#include "llvm/IR/DiagnosticInfo.h"
21#include "llvm/IR/InstIterator.h"
22#include "llvm/IR/InstVisitor.h"
23#include "llvm/IR/Instructions.h"
24#include "llvm/IR/IntrinsicInst.h"
25#include "llvm/ProfileData/InstrProf.h"
26#include "llvm/Support/Debug.h"
27#include <string>
28#include <utility>
29#include <vector>
30
31using namespace llvm;
32
33#define DEBUG_TYPE "pgo-icall-prom-analysis"
34
35// The minimum call count for the direct-call target to be considered as the
36// promotion candidate.
37static cl::opt<unsigned>
38 ICPCountThreshold("icp-count-threshold", cl::Hidden, cl::ZeroOrMore,
39 cl::init(1000),
40 cl::desc("The minimum count to the direct call target "
41 "for the promotion"));
42
43// The percent threshold for the direct-call target (this call site vs the
Dehao Chenf4240b52017-07-28 01:02:54 +000044// remaining call count) for it to be considered as the promotion target.
45static cl::opt<unsigned> ICPRemainingPercentThreshold(
46 "icp-remaining-percent-threshold", cl::init(30), cl::Hidden, cl::ZeroOrMore,
47 cl::desc("The percentage threshold against remaining unpromoted indirect "
48 "call count for the promotion"));
49
50// The percent threshold for the direct-call target (this call site vs the
Teresa Johnson1e44b5d2016-07-12 21:13:44 +000051// total call count) for it to be considered as the promotion target.
52static cl::opt<unsigned>
Dehao Chenf4240b52017-07-28 01:02:54 +000053 ICPTotalPercentThreshold("icp-total-percent-threshold", cl::init(5),
54 cl::Hidden, cl::ZeroOrMore,
55 cl::desc("The percentage threshold against total "
56 "count for the promotion"));
Teresa Johnson1e44b5d2016-07-12 21:13:44 +000057
58// Set the maximum number of targets to promote for a single indirect-call
59// callsite.
60static cl::opt<unsigned>
Dehao Chene70a4722017-07-28 01:03:10 +000061 MaxNumPromotions("icp-max-prom", cl::init(3), cl::Hidden, cl::ZeroOrMore,
Teresa Johnson1e44b5d2016-07-12 21:13:44 +000062 cl::desc("Max number of promotions for a single indirect "
63 "call callsite"));
64
65ICallPromotionAnalysis::ICallPromotionAnalysis() {
66 ValueDataArray = llvm::make_unique<InstrProfValueData[]>(MaxNumPromotions);
67}
68
69bool ICallPromotionAnalysis::isPromotionProfitable(uint64_t Count,
Dehao Chenf4240b52017-07-28 01:02:54 +000070 uint64_t TotalCount,
71 uint64_t RemainingCount) {
72 return Count >= ICPCountThreshold &&
73 Count * 100 >= ICPRemainingPercentThreshold * RemainingCount &&
74 Count * 100 >= ICPTotalPercentThreshold * TotalCount;
Teresa Johnson1e44b5d2016-07-12 21:13:44 +000075}
76
77// Indirect-call promotion heuristic. The direct targets are sorted based on
78// the count. Stop at the first target that is not promoted. Returns the
79// number of candidates deemed profitable.
80uint32_t ICallPromotionAnalysis::getProfitablePromotionCandidates(
81 const Instruction *Inst, uint32_t NumVals, uint64_t TotalCount) {
82 ArrayRef<InstrProfValueData> ValueDataRef(ValueDataArray.get(), NumVals);
83
84 DEBUG(dbgs() << " \nWork on callsite " << *Inst << " Num_targets: " << NumVals
85 << "\n");
86
87 uint32_t I = 0;
Dehao Chenf4240b52017-07-28 01:02:54 +000088 uint64_t RemainingCount = TotalCount;
Teresa Johnson1e44b5d2016-07-12 21:13:44 +000089 for (; I < MaxNumPromotions && I < NumVals; I++) {
90 uint64_t Count = ValueDataRef[I].Count;
Dehao Chenf4240b52017-07-28 01:02:54 +000091 assert(Count <= RemainingCount);
Teresa Johnson1e44b5d2016-07-12 21:13:44 +000092 DEBUG(dbgs() << " Candidate " << I << " Count=" << Count
Teresa Johnson835df562016-07-12 23:49:17 +000093 << " Target_func: " << ValueDataRef[I].Value << "\n");
Teresa Johnson1e44b5d2016-07-12 21:13:44 +000094
Dehao Chenf4240b52017-07-28 01:02:54 +000095 if (!isPromotionProfitable(Count, TotalCount, RemainingCount)) {
Teresa Johnson1e44b5d2016-07-12 21:13:44 +000096 DEBUG(dbgs() << " Not promote: Cold target.\n");
97 return I;
98 }
Dehao Chenf4240b52017-07-28 01:02:54 +000099 RemainingCount -= Count;
Teresa Johnson1e44b5d2016-07-12 21:13:44 +0000100 }
101 return I;
102}
103
104ArrayRef<InstrProfValueData>
105ICallPromotionAnalysis::getPromotionCandidatesForInstruction(
106 const Instruction *I, uint32_t &NumVals, uint64_t &TotalCount,
107 uint32_t &NumCandidates) {
108 bool Res =
109 getValueProfDataFromInst(*I, IPVK_IndirectCallTarget, MaxNumPromotions,
110 ValueDataArray.get(), NumVals, TotalCount);
111 if (!Res) {
112 NumCandidates = 0;
113 return ArrayRef<InstrProfValueData>();
114 }
115 NumCandidates = getProfitablePromotionCandidates(I, NumVals, TotalCount);
116 return ArrayRef<InstrProfValueData>(ValueDataArray.get(), NumVals);
117}