1 //===-- IndirectCallPromotionAnalysis.cpp - Find promotion candidates ===//
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
7 //===----------------------------------------------------------------------===//
9 // Helper methods for identifying profitable indirect call promotion
10 // candidates for an instruction when the indirect-call value profile metadata
13 //===----------------------------------------------------------------------===//
15 #include "llvm/Analysis/IndirectCallPromotionAnalysis.h"
16 #include "llvm/ADT/STLExtras.h"
17 #include "llvm/Analysis/IndirectCallVisitor.h"
18 #include "llvm/IR/InstIterator.h"
19 #include "llvm/IR/InstVisitor.h"
20 #include "llvm/IR/Instructions.h"
21 #include "llvm/IR/IntrinsicInst.h"
22 #include "llvm/ProfileData/InstrProf.h"
23 #include "llvm/Support/CommandLine.h"
24 #include "llvm/Support/Debug.h"
29 #define DEBUG_TYPE "pgo-icall-prom-analysis"
31 // The percent threshold for the direct-call target (this call site vs the
32 // remaining call count) for it to be considered as the promotion target.
33 static cl::opt
<unsigned> ICPRemainingPercentThreshold(
34 "icp-remaining-percent-threshold", cl::init(30), cl::Hidden
, cl::ZeroOrMore
,
35 cl::desc("The percentage threshold against remaining unpromoted indirect "
36 "call count for the promotion"));
38 // The percent threshold for the direct-call target (this call site vs the
39 // total call count) for it to be considered as the promotion target.
40 static cl::opt
<unsigned>
41 ICPTotalPercentThreshold("icp-total-percent-threshold", cl::init(5),
42 cl::Hidden
, cl::ZeroOrMore
,
43 cl::desc("The percentage threshold against total "
44 "count for the promotion"));
46 // Set the maximum number of targets to promote for a single indirect-call
48 static cl::opt
<unsigned>
49 MaxNumPromotions("icp-max-prom", cl::init(3), cl::Hidden
, cl::ZeroOrMore
,
50 cl::desc("Max number of promotions for a single indirect "
53 ICallPromotionAnalysis::ICallPromotionAnalysis() {
54 ValueDataArray
= std::make_unique
<InstrProfValueData
[]>(MaxNumPromotions
);
57 bool ICallPromotionAnalysis::isPromotionProfitable(uint64_t Count
,
59 uint64_t RemainingCount
) {
60 return Count
* 100 >= ICPRemainingPercentThreshold
* RemainingCount
&&
61 Count
* 100 >= ICPTotalPercentThreshold
* TotalCount
;
64 // Indirect-call promotion heuristic. The direct targets are sorted based on
65 // the count. Stop at the first target that is not promoted. Returns the
66 // number of candidates deemed profitable.
67 uint32_t ICallPromotionAnalysis::getProfitablePromotionCandidates(
68 const Instruction
*Inst
, uint32_t NumVals
, uint64_t TotalCount
) {
69 ArrayRef
<InstrProfValueData
> ValueDataRef(ValueDataArray
.get(), NumVals
);
71 LLVM_DEBUG(dbgs() << " \nWork on callsite " << *Inst
72 << " Num_targets: " << NumVals
<< "\n");
75 uint64_t RemainingCount
= TotalCount
;
76 for (; I
< MaxNumPromotions
&& I
< NumVals
; I
++) {
77 uint64_t Count
= ValueDataRef
[I
].Count
;
78 assert(Count
<= RemainingCount
);
79 LLVM_DEBUG(dbgs() << " Candidate " << I
<< " Count=" << Count
80 << " Target_func: " << ValueDataRef
[I
].Value
<< "\n");
82 if (!isPromotionProfitable(Count
, TotalCount
, RemainingCount
)) {
83 LLVM_DEBUG(dbgs() << " Not promote: Cold target.\n");
86 RemainingCount
-= Count
;
91 ArrayRef
<InstrProfValueData
>
92 ICallPromotionAnalysis::getPromotionCandidatesForInstruction(
93 const Instruction
*I
, uint32_t &NumVals
, uint64_t &TotalCount
,
94 uint32_t &NumCandidates
) {
96 getValueProfDataFromInst(*I
, IPVK_IndirectCallTarget
, MaxNumPromotions
,
97 ValueDataArray
.get(), NumVals
, TotalCount
);
100 return ArrayRef
<InstrProfValueData
>();
102 NumCandidates
= getProfitablePromotionCandidates(I
, NumVals
, TotalCount
);
103 return ArrayRef
<InstrProfValueData
>(ValueDataArray
.get(), NumVals
);