blob: 68153de8219f506204b565b85b333c91cb208b80 [file] [log] [blame]
Teresa Johnson1e44b5d2016-07-12 21:13:44 +00001//===-- IndirectCallPromotionAnalysis.cpp - Find promotion candidates ===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Teresa Johnson1e44b5d2016-07-12 21:13:44 +00006//
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 "llvm/ADT/STLExtras.h"
Chandler Carruth57578aa2019-01-07 07:15:51 +000017#include "llvm/Analysis/IndirectCallVisitor.h"
Teresa Johnson1e44b5d2016-07-12 21:13:44 +000018#include "llvm/IR/CallSite.h"
Teresa Johnson1e44b5d2016-07-12 21:13:44 +000019#include "llvm/IR/InstIterator.h"
20#include "llvm/IR/InstVisitor.h"
21#include "llvm/IR/Instructions.h"
22#include "llvm/IR/IntrinsicInst.h"
23#include "llvm/ProfileData/InstrProf.h"
24#include "llvm/Support/Debug.h"
25#include <string>
26#include <utility>
27#include <vector>
28
29using namespace llvm;
30
31#define DEBUG_TYPE "pgo-icall-prom-analysis"
32
Teresa Johnson1e44b5d2016-07-12 21:13:44 +000033// The percent threshold for the direct-call target (this call site vs the
Dehao Chenf4240b52017-07-28 01:02:54 +000034// remaining call count) for it to be considered as the promotion target.
35static cl::opt<unsigned> ICPRemainingPercentThreshold(
36 "icp-remaining-percent-threshold", cl::init(30), cl::Hidden, cl::ZeroOrMore,
37 cl::desc("The percentage threshold against remaining unpromoted indirect "
38 "call count for the promotion"));
39
40// The percent threshold for the direct-call target (this call site vs the
Teresa Johnson1e44b5d2016-07-12 21:13:44 +000041// total call count) for it to be considered as the promotion target.
42static cl::opt<unsigned>
Dehao Chenf4240b52017-07-28 01:02:54 +000043 ICPTotalPercentThreshold("icp-total-percent-threshold", cl::init(5),
44 cl::Hidden, cl::ZeroOrMore,
45 cl::desc("The percentage threshold against total "
46 "count for the promotion"));
Teresa Johnson1e44b5d2016-07-12 21:13:44 +000047
48// Set the maximum number of targets to promote for a single indirect-call
49// callsite.
50static cl::opt<unsigned>
Dehao Chene70a4722017-07-28 01:03:10 +000051 MaxNumPromotions("icp-max-prom", cl::init(3), cl::Hidden, cl::ZeroOrMore,
Teresa Johnson1e44b5d2016-07-12 21:13:44 +000052 cl::desc("Max number of promotions for a single indirect "
53 "call callsite"));
54
55ICallPromotionAnalysis::ICallPromotionAnalysis() {
Jonas Devlieghere0eaee542019-08-15 15:54:37 +000056 ValueDataArray = std::make_unique<InstrProfValueData[]>(MaxNumPromotions);
Teresa Johnson1e44b5d2016-07-12 21:13:44 +000057}
58
59bool ICallPromotionAnalysis::isPromotionProfitable(uint64_t Count,
Dehao Chenf4240b52017-07-28 01:02:54 +000060 uint64_t TotalCount,
61 uint64_t RemainingCount) {
Dehao Chen34cfcb22017-08-08 20:57:33 +000062 return Count * 100 >= ICPRemainingPercentThreshold * RemainingCount &&
Dehao Chenf4240b52017-07-28 01:02:54 +000063 Count * 100 >= ICPTotalPercentThreshold * TotalCount;
Teresa Johnson1e44b5d2016-07-12 21:13:44 +000064}
65
66// Indirect-call promotion heuristic. The direct targets are sorted based on
67// the count. Stop at the first target that is not promoted. Returns the
68// number of candidates deemed profitable.
69uint32_t ICallPromotionAnalysis::getProfitablePromotionCandidates(
70 const Instruction *Inst, uint32_t NumVals, uint64_t TotalCount) {
71 ArrayRef<InstrProfValueData> ValueDataRef(ValueDataArray.get(), NumVals);
72
Nicola Zaghend34e60c2018-05-14 12:53:11 +000073 LLVM_DEBUG(dbgs() << " \nWork on callsite " << *Inst
74 << " Num_targets: " << NumVals << "\n");
Teresa Johnson1e44b5d2016-07-12 21:13:44 +000075
76 uint32_t I = 0;
Dehao Chenf4240b52017-07-28 01:02:54 +000077 uint64_t RemainingCount = TotalCount;
Teresa Johnson1e44b5d2016-07-12 21:13:44 +000078 for (; I < MaxNumPromotions && I < NumVals; I++) {
79 uint64_t Count = ValueDataRef[I].Count;
Dehao Chenf4240b52017-07-28 01:02:54 +000080 assert(Count <= RemainingCount);
Nicola Zaghend34e60c2018-05-14 12:53:11 +000081 LLVM_DEBUG(dbgs() << " Candidate " << I << " Count=" << Count
82 << " Target_func: " << ValueDataRef[I].Value << "\n");
Teresa Johnson1e44b5d2016-07-12 21:13:44 +000083
Dehao Chenf4240b52017-07-28 01:02:54 +000084 if (!isPromotionProfitable(Count, TotalCount, RemainingCount)) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +000085 LLVM_DEBUG(dbgs() << " Not promote: Cold target.\n");
Teresa Johnson1e44b5d2016-07-12 21:13:44 +000086 return I;
87 }
Dehao Chenf4240b52017-07-28 01:02:54 +000088 RemainingCount -= Count;
Teresa Johnson1e44b5d2016-07-12 21:13:44 +000089 }
90 return I;
91}
92
93ArrayRef<InstrProfValueData>
94ICallPromotionAnalysis::getPromotionCandidatesForInstruction(
95 const Instruction *I, uint32_t &NumVals, uint64_t &TotalCount,
96 uint32_t &NumCandidates) {
97 bool Res =
98 getValueProfDataFromInst(*I, IPVK_IndirectCallTarget, MaxNumPromotions,
99 ValueDataArray.get(), NumVals, TotalCount);
100 if (!Res) {
101 NumCandidates = 0;
102 return ArrayRef<InstrProfValueData>();
103 }
104 NumCandidates = getProfitablePromotionCandidates(I, NumVals, TotalCount);
105 return ArrayRef<InstrProfValueData>(ValueDataArray.get(), NumVals);
106}