annotate lib/Analysis/IndirectCallPromotionAnalysis.cpp @ 128:c347d3398279 default tip

fix
author mir3636
date Wed, 06 Dec 2017 14:37:17 +0900
parents 803732b1fca8
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
120
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
1 //===-- IndirectCallPromotionAnalysis.cpp - Find promotion candidates ===//
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
2 //
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
3 // The LLVM Compiler Infrastructure
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
4 //
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
5 // This file is distributed under the University of Illinois Open Source
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
6 // License. See LICENSE.TXT for details.
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
7 //
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
8 //===----------------------------------------------------------------------===//
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
9 //
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
10 // Helper methods for identifying profitable indirect call promotion
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
11 // candidates for an instruction when the indirect-call value profile metadata
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
12 // is available.
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
13 //
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
14 //===----------------------------------------------------------------------===//
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
15
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
16 #include "llvm/Analysis/IndirectCallPromotionAnalysis.h"
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
17 #include "llvm/ADT/STLExtras.h"
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
18 #include "llvm/Analysis/IndirectCallSiteVisitor.h"
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
19 #include "llvm/IR/CallSite.h"
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
20 #include "llvm/IR/DiagnosticInfo.h"
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
21 #include "llvm/IR/InstIterator.h"
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
22 #include "llvm/IR/InstVisitor.h"
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
23 #include "llvm/IR/Instructions.h"
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
24 #include "llvm/IR/IntrinsicInst.h"
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
25 #include "llvm/ProfileData/InstrProf.h"
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
26 #include "llvm/Support/Debug.h"
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
27 #include <string>
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
28 #include <utility>
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
29 #include <vector>
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
30
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
31 using namespace llvm;
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
32
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
33 #define DEBUG_TYPE "pgo-icall-prom-analysis"
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
34
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
35 // The percent threshold for the direct-call target (this call site vs the
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
36 // remaining call count) for it to be considered as the promotion target.
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
37 static cl::opt<unsigned> ICPRemainingPercentThreshold(
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
38 "icp-remaining-percent-threshold", cl::init(30), cl::Hidden, cl::ZeroOrMore,
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
39 cl::desc("The percentage threshold against remaining unpromoted indirect "
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
40 "call count for the promotion"));
120
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
41
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
42 // The percent threshold for the direct-call target (this call site vs the
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
43 // total call count) for it to be considered as the promotion target.
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
44 static cl::opt<unsigned>
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
45 ICPTotalPercentThreshold("icp-total-percent-threshold", cl::init(5),
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
46 cl::Hidden, cl::ZeroOrMore,
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
47 cl::desc("The percentage threshold against total "
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
48 "count for the promotion"));
120
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
49
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
50 // Set the maximum number of targets to promote for a single indirect-call
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
51 // callsite.
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
52 static cl::opt<unsigned>
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
53 MaxNumPromotions("icp-max-prom", cl::init(3), cl::Hidden, cl::ZeroOrMore,
120
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
54 cl::desc("Max number of promotions for a single indirect "
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
55 "call callsite"));
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
56
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
57 ICallPromotionAnalysis::ICallPromotionAnalysis() {
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
58 ValueDataArray = llvm::make_unique<InstrProfValueData[]>(MaxNumPromotions);
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
59 }
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
60
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
61 bool ICallPromotionAnalysis::isPromotionProfitable(uint64_t Count,
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
62 uint64_t TotalCount,
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
63 uint64_t RemainingCount) {
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
64 return Count * 100 >= ICPRemainingPercentThreshold * RemainingCount &&
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
65 Count * 100 >= ICPTotalPercentThreshold * TotalCount;
120
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
66 }
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
67
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
68 // Indirect-call promotion heuristic. The direct targets are sorted based on
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
69 // the count. Stop at the first target that is not promoted. Returns the
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
70 // number of candidates deemed profitable.
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
71 uint32_t ICallPromotionAnalysis::getProfitablePromotionCandidates(
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
72 const Instruction *Inst, uint32_t NumVals, uint64_t TotalCount) {
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
73 ArrayRef<InstrProfValueData> ValueDataRef(ValueDataArray.get(), NumVals);
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
74
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
75 DEBUG(dbgs() << " \nWork on callsite " << *Inst << " Num_targets: " << NumVals
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
76 << "\n");
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
77
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
78 uint32_t I = 0;
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
79 uint64_t RemainingCount = TotalCount;
120
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
80 for (; I < MaxNumPromotions && I < NumVals; I++) {
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
81 uint64_t Count = ValueDataRef[I].Count;
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
82 assert(Count <= RemainingCount);
120
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
83 DEBUG(dbgs() << " Candidate " << I << " Count=" << Count
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
84 << " Target_func: " << ValueDataRef[I].Value << "\n");
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
85
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
86 if (!isPromotionProfitable(Count, TotalCount, RemainingCount)) {
120
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
87 DEBUG(dbgs() << " Not promote: Cold target.\n");
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
88 return I;
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
89 }
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
90 RemainingCount -= Count;
120
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
91 }
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
92 return I;
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
93 }
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
94
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
95 ArrayRef<InstrProfValueData>
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
96 ICallPromotionAnalysis::getPromotionCandidatesForInstruction(
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
97 const Instruction *I, uint32_t &NumVals, uint64_t &TotalCount,
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
98 uint32_t &NumCandidates) {
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
99 bool Res =
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
100 getValueProfDataFromInst(*I, IPVK_IndirectCallTarget, MaxNumPromotions,
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
101 ValueDataArray.get(), NumVals, TotalCount);
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
102 if (!Res) {
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
103 NumCandidates = 0;
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
104 return ArrayRef<InstrProfValueData>();
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
105 }
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
106 NumCandidates = getProfitablePromotionCandidates(I, NumVals, TotalCount);
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
107 return ArrayRef<InstrProfValueData>(ValueDataArray.get(), NumVals);
1172e4bd9c6f update 4.0.0
mir3636
parents:
diff changeset
108 }