blob: d6e6e76af03c567af10bf792cb5ba2c3742bca89 [file] [log] [blame]
Teresa Johnsone0c5cea2016-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"
Chandler Carruth9d8b7a02019-01-07 07:15:51 +000018#include "llvm/Analysis/IndirectCallVisitor.h"
Teresa Johnsone0c5cea2016-07-12 21:13:44 +000019#include "llvm/IR/CallSite.h"
Teresa Johnsone0c5cea2016-07-12 21:13:44 +000020#include "llvm/IR/InstIterator.h"
21#include "llvm/IR/InstVisitor.h"
22#include "llvm/IR/Instructions.h"
23#include "llvm/IR/IntrinsicInst.h"
24#include "llvm/ProfileData/InstrProf.h"
25#include "llvm/Support/Debug.h"
26#include <string>
27#include <utility>
28#include <vector>
29
30using namespace llvm;
31
32#define DEBUG_TYPE "pgo-icall-prom-analysis"
33
Teresa Johnsone0c5cea2016-07-12 21:13:44 +000034// The percent threshold for the direct-call target (this call site vs the
Dehao Chen87452e82017-07-28 01:02:54 +000035// remaining call count) for it to be considered as the promotion target.
36static cl::opt<unsigned> ICPRemainingPercentThreshold(
37 "icp-remaining-percent-threshold", cl::init(30), cl::Hidden, cl::ZeroOrMore,
38 cl::desc("The percentage threshold against remaining unpromoted indirect "
39 "call count for the promotion"));
40
41// The percent threshold for the direct-call target (this call site vs the
Teresa Johnsone0c5cea2016-07-12 21:13:44 +000042// total call count) for it to be considered as the promotion target.
43static cl::opt<unsigned>
Dehao Chen87452e82017-07-28 01:02:54 +000044 ICPTotalPercentThreshold("icp-total-percent-threshold", cl::init(5),
45 cl::Hidden, cl::ZeroOrMore,
46 cl::desc("The percentage threshold against total "
47 "count for the promotion"));
Teresa Johnsone0c5cea2016-07-12 21:13:44 +000048
49// Set the maximum number of targets to promote for a single indirect-call
50// callsite.
51static cl::opt<unsigned>
Dehao Chen4e663792017-07-28 01:03:10 +000052 MaxNumPromotions("icp-max-prom", cl::init(3), cl::Hidden, cl::ZeroOrMore,
Teresa Johnsone0c5cea2016-07-12 21:13:44 +000053 cl::desc("Max number of promotions for a single indirect "
54 "call callsite"));
55
56ICallPromotionAnalysis::ICallPromotionAnalysis() {
57 ValueDataArray = llvm::make_unique<InstrProfValueData[]>(MaxNumPromotions);
58}
59
60bool ICallPromotionAnalysis::isPromotionProfitable(uint64_t Count,
Dehao Chen87452e82017-07-28 01:02:54 +000061 uint64_t TotalCount,
62 uint64_t RemainingCount) {
Dehao Chen8a7a8e62017-08-08 20:57:33 +000063 return Count * 100 >= ICPRemainingPercentThreshold * RemainingCount &&
Dehao Chen87452e82017-07-28 01:02:54 +000064 Count * 100 >= ICPTotalPercentThreshold * TotalCount;
Teresa Johnsone0c5cea2016-07-12 21:13:44 +000065}
66
67// Indirect-call promotion heuristic. The direct targets are sorted based on
68// the count. Stop at the first target that is not promoted. Returns the
69// number of candidates deemed profitable.
70uint32_t ICallPromotionAnalysis::getProfitablePromotionCandidates(
71 const Instruction *Inst, uint32_t NumVals, uint64_t TotalCount) {
72 ArrayRef<InstrProfValueData> ValueDataRef(ValueDataArray.get(), NumVals);
73
Nicola Zaghen0818e782018-05-14 12:53:11 +000074 LLVM_DEBUG(dbgs() << " \nWork on callsite " << *Inst
75 << " Num_targets: " << NumVals << "\n");
Teresa Johnsone0c5cea2016-07-12 21:13:44 +000076
77 uint32_t I = 0;
Dehao Chen87452e82017-07-28 01:02:54 +000078 uint64_t RemainingCount = TotalCount;
Teresa Johnsone0c5cea2016-07-12 21:13:44 +000079 for (; I < MaxNumPromotions && I < NumVals; I++) {
80 uint64_t Count = ValueDataRef[I].Count;
Dehao Chen87452e82017-07-28 01:02:54 +000081 assert(Count <= RemainingCount);
Nicola Zaghen0818e782018-05-14 12:53:11 +000082 LLVM_DEBUG(dbgs() << " Candidate " << I << " Count=" << Count
83 << " Target_func: " << ValueDataRef[I].Value << "\n");
Teresa Johnsone0c5cea2016-07-12 21:13:44 +000084
Dehao Chen87452e82017-07-28 01:02:54 +000085 if (!isPromotionProfitable(Count, TotalCount, RemainingCount)) {
Nicola Zaghen0818e782018-05-14 12:53:11 +000086 LLVM_DEBUG(dbgs() << " Not promote: Cold target.\n");
Teresa Johnsone0c5cea2016-07-12 21:13:44 +000087 return I;
88 }
Dehao Chen87452e82017-07-28 01:02:54 +000089 RemainingCount -= Count;
Teresa Johnsone0c5cea2016-07-12 21:13:44 +000090 }
91 return I;
92}
93
94ArrayRef<InstrProfValueData>
95ICallPromotionAnalysis::getPromotionCandidatesForInstruction(
96 const Instruction *I, uint32_t &NumVals, uint64_t &TotalCount,
97 uint32_t &NumCandidates) {
98 bool Res =
99 getValueProfDataFromInst(*I, IPVK_IndirectCallTarget, MaxNumPromotions,
100 ValueDataArray.get(), NumVals, TotalCount);
101 if (!Res) {
102 NumCandidates = 0;
103 return ArrayRef<InstrProfValueData>();
104 }
105 NumCandidates = getProfitablePromotionCandidates(I, NumVals, TotalCount);
106 return ArrayRef<InstrProfValueData>(ValueDataArray.get(), NumVals);
107}