blob: 3af7eba94e3c9888207caf6738016464e17c37cd [file] [log] [blame]
Daniel Dunbar55e354a2009-08-08 18:44:18 +00001//===- ProfileEstimatorPass.cpp - LLVM Pass to estimate profile info ------===//
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// This file implements a concrete implementation of profiling information that
11// estimates the profiling information in a very crude and unimaginative way.
12//
13//===----------------------------------------------------------------------===//
14#define DEBUG_TYPE "profile-estimator"
15#include "llvm/Pass.h"
16#include "llvm/Analysis/Passes.h"
17#include "llvm/Analysis/ProfileInfo.h"
18#include "llvm/Analysis/LoopInfo.h"
19#include "llvm/Support/CommandLine.h"
20#include "llvm/Support/Debug.h"
21#include "llvm/Support/raw_ostream.h"
Andreas Neustifterff271e12009-08-26 15:13:44 +000022#include "llvm/Support/Format.h"
Daniel Dunbar55e354a2009-08-08 18:44:18 +000023using namespace llvm;
24
25static cl::opt<double>
Andreas Neustifterff271e12009-08-26 15:13:44 +000026LoopWeight(
Daniel Dunbar55e354a2009-08-08 18:44:18 +000027 "profile-estimator-loop-weight", cl::init(10),
28 cl::value_desc("loop-weight"),
29 cl::desc("Number of loop executions used for profile-estimator")
30);
31
32namespace {
33 class VISIBILITY_HIDDEN ProfileEstimatorPass :
34 public FunctionPass, public ProfileInfo {
35 double ExecCount;
36 LoopInfo *LI;
37 std::set<BasicBlock*> BBisVisited;
38 std::map<Loop*,double> LoopExitWeights;
39 public:
40 static char ID; // Class identification, replacement for typeinfo
41 explicit ProfileEstimatorPass(const double execcount = 0)
42 : FunctionPass(&ID), ExecCount(execcount) {
Andreas Neustifterff271e12009-08-26 15:13:44 +000043 if (execcount == 0) ExecCount = LoopWeight;
Daniel Dunbar55e354a2009-08-08 18:44:18 +000044 }
45
46 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
47 AU.setPreservesAll();
48 AU.addRequired<LoopInfo>();
49 }
50
51 virtual const char *getPassName() const {
52 return "Profiling information estimator";
53 }
54
55 /// run - Estimate the profile information from the specified file.
56 virtual bool runOnFunction(Function &F);
57
58 virtual void recurseBasicBlock(BasicBlock *BB);
Andreas Neustifterff271e12009-08-26 15:13:44 +000059
60 void inline printEdgeWeight(Edge);
Daniel Dunbar55e354a2009-08-08 18:44:18 +000061 };
62} // End of anonymous namespace
63
64char ProfileEstimatorPass::ID = 0;
65static RegisterPass<ProfileEstimatorPass>
66X("profile-estimator", "Estimate profiling information", false, true);
67
68static RegisterAnalysisGroup<ProfileInfo> Y(X);
69
70namespace llvm {
71 const PassInfo *ProfileEstimatorPassID = &X;
72
73 FunctionPass *createProfileEstimatorPass() {
74 return new ProfileEstimatorPass();
75 }
76
Andreas Neustifterff271e12009-08-26 15:13:44 +000077 /// createProfileEstimatorPass - This function returns a Pass that estimates
78 /// profiling information using the given loop execution count.
Daniel Dunbar55e354a2009-08-08 18:44:18 +000079 Pass *createProfileEstimatorPass(const unsigned execcount) {
80 return new ProfileEstimatorPass(execcount);
81 }
82}
83
84static double ignoreMissing(double w) {
85 if (w == ProfileInfo::MissingValue) return 0;
86 return w;
87}
88
Andreas Neustifterff271e12009-08-26 15:13:44 +000089static void inline printEdgeError(BasicBlock *V1, BasicBlock *V2) {
Daniel Dunbar55e354a2009-08-08 18:44:18 +000090 DEBUG(errs() << "-- Edge (" <<(V1)->getName() << "," << (V2)->getName() \
Andreas Neustifterff271e12009-08-26 15:13:44 +000091 << ") is not calculated, returning\n");
92}
Daniel Dunbar55e354a2009-08-08 18:44:18 +000093
Andreas Neustifterff271e12009-08-26 15:13:44 +000094void inline ProfileEstimatorPass::printEdgeWeight(Edge E) {
95 DEBUG(errs() << "-- Weight of Edge " << E << ":"
96 << format("%g", getEdgeWeight(E)) << "\n");
97}
Daniel Dunbar55e354a2009-08-08 18:44:18 +000098
99// recurseBasicBlock() - This calculates the ProfileInfo estimation for a
100// single block and then recurses into the successors.
101void ProfileEstimatorPass::recurseBasicBlock(BasicBlock *BB) {
102
Andreas Neustifterff271e12009-08-26 15:13:44 +0000103 // Break the recursion if this BasicBlock was already visited.
Daniel Dunbar55e354a2009-08-08 18:44:18 +0000104 if (BBisVisited.find(BB) != BBisVisited.end()) return;
105
Andreas Neustifterff271e12009-08-26 15:13:44 +0000106 // Check if incoming edges are calculated already, if BB is header allow
107 // backedges that are uncalculated for now.
Daniel Dunbar55e354a2009-08-08 18:44:18 +0000108 bool BBisHeader = LI->isLoopHeader(BB);
109 Loop* BBLoop = LI->getLoopFor(BB);
110
111 double BBWeight = 0;
112 std::set<BasicBlock*> ProcessedPreds;
113 for ( pred_iterator bbi = pred_begin(BB), bbe = pred_end(BB);
114 bbi != bbe; ++bbi ) {
115 if (ProcessedPreds.insert(*bbi).second) {
116 Edge edge = getEdge(*bbi,BB);
117 BBWeight += ignoreMissing(getEdgeWeight(edge));
118 }
119 if (BBisHeader && BBLoop == LI->getLoopFor(*bbi)) {
Andreas Neustifterff271e12009-08-26 15:13:44 +0000120 printEdgeError(*bbi,BB);
Daniel Dunbar55e354a2009-08-08 18:44:18 +0000121 continue;
122 }
123 if (BBisVisited.find(*bbi) == BBisVisited.end()) {
Andreas Neustifterff271e12009-08-26 15:13:44 +0000124 printEdgeError(*bbi,BB);
Daniel Dunbar55e354a2009-08-08 18:44:18 +0000125 return;
126 }
127 }
128 if (getExecutionCount(BB) != MissingValue) {
129 BBWeight = getExecutionCount(BB);
130 }
131
Andreas Neustifterff271e12009-08-26 15:13:44 +0000132 // Fetch all necessary information for current block.
Daniel Dunbar55e354a2009-08-08 18:44:18 +0000133 SmallVector<Edge, 8> ExitEdges;
134 SmallVector<Edge, 8> Edges;
135 if (BBLoop) {
136 BBLoop->getExitEdges(ExitEdges);
137 }
138
Andreas Neustifterff271e12009-08-26 15:13:44 +0000139 // If block is an loop header, first subtract all weights from edges that
Daniel Dunbar55e354a2009-08-08 18:44:18 +0000140 // exit this loop, then distribute remaining weight on to the edges exiting
Andreas Neustifterff271e12009-08-26 15:13:44 +0000141 // this loop. Finally the weight of the block is increased, to simulate
142 // several executions of this loop.
Daniel Dunbar55e354a2009-08-08 18:44:18 +0000143 if (BBisHeader) {
144 double incoming = BBWeight;
Andreas Neustifterff271e12009-08-26 15:13:44 +0000145 // Subtract the flow leaving the loop.
Daniel Dunbar55e354a2009-08-08 18:44:18 +0000146 for (SmallVector<Edge, 8>::iterator ei = ExitEdges.begin(),
147 ee = ExitEdges.end(); ei != ee; ++ei) {
148 double w = getEdgeWeight(*ei);
149 if (w == MissingValue) {
150 Edges.push_back(*ei);
151 } else {
152 incoming -= w;
153 }
154 }
Andreas Neustifterff271e12009-08-26 15:13:44 +0000155 // Distribute remaining weight onto the exit edges.
Daniel Dunbar55e354a2009-08-08 18:44:18 +0000156 for (SmallVector<Edge, 8>::iterator ei = Edges.begin(), ee = Edges.end();
157 ei != ee; ++ei) {
158 EdgeInformation[BB->getParent()][*ei] += incoming/Edges.size();
Andreas Neustifterff271e12009-08-26 15:13:44 +0000159 printEdgeWeight(*ei);
Daniel Dunbar55e354a2009-08-08 18:44:18 +0000160 }
Andreas Neustifterff271e12009-08-26 15:13:44 +0000161 // Increase flow into the loop.
Daniel Dunbar55e354a2009-08-08 18:44:18 +0000162 BBWeight *= (ExecCount+1);
163 }
164
Andreas Neustifterff271e12009-08-26 15:13:44 +0000165 // Remove from current flow of block all the successor edges that already
166 // have some flow on them.
Daniel Dunbar55e354a2009-08-08 18:44:18 +0000167 Edges.clear();
168 std::set<BasicBlock*> ProcessedSuccs;
Andreas Neustifterff271e12009-08-26 15:13:44 +0000169
170 // Otherwise consider weight of outgoing edges and store them for
Andreas Neustiftere885af92009-09-01 10:06:05 +0000171 // distribution of remaining weight. In case the block has no successors
172 // create a (BB,0) edge.
173 succ_iterator bbi = succ_begin(BB), bbe = succ_end(BB);
174 if (bbi == bbe) {
175 Edge edge = getEdge(BB,0);
176 EdgeInformation[BB->getParent()][edge] = BBWeight;
177 printEdgeWeight(edge);
178 }
Daniel Dunbar55e354a2009-08-08 18:44:18 +0000179 for ( succ_iterator bbi = succ_begin(BB), bbe = succ_end(BB);
180 bbi != bbe; ++bbi ) {
181 if (ProcessedSuccs.insert(*bbi).second) {
182 Edge edge = getEdge(BB,*bbi);
183 double w = getEdgeWeight(edge);
184 if (w != MissingValue) {
185 BBWeight -= getEdgeWeight(edge);
186 } else {
187 Edges.push_back(edge);
188 }
189 }
190 }
191
Andreas Neustifterff271e12009-08-26 15:13:44 +0000192 // Distribute remaining flow onto the outgoing edges.
Daniel Dunbar55e354a2009-08-08 18:44:18 +0000193 for (SmallVector<Edge, 8>::iterator ei = Edges.begin(), ee = Edges.end();
194 ei != ee; ++ei) {
195 EdgeInformation[BB->getParent()][*ei] += BBWeight/Edges.size();
Andreas Neustifterff271e12009-08-26 15:13:44 +0000196 printEdgeWeight(*ei);
Daniel Dunbar55e354a2009-08-08 18:44:18 +0000197 }
198
Andreas Neustifterff271e12009-08-26 15:13:44 +0000199 // Mark this Block visited and recurse into successors.
Daniel Dunbar55e354a2009-08-08 18:44:18 +0000200 BBisVisited.insert(BB);
201 for ( succ_iterator bbi = succ_begin(BB), bbe = succ_end(BB);
202 bbi != bbe;
203 ++bbi ) {
204 recurseBasicBlock(*bbi);
205 }
206}
207
208bool ProfileEstimatorPass::runOnFunction(Function &F) {
209 if (F.isDeclaration()) return false;
210
211 LI = &getAnalysis<LoopInfo>();
212 FunctionInformation.erase(&F);
213 BlockInformation[&F].clear();
214 EdgeInformation[&F].clear();
215 BBisVisited.clear();
216
Andreas Neustifterff271e12009-08-26 15:13:44 +0000217 DEBUG(errs() << "Working on function " << F.getNameStr() << "\n");
Daniel Dunbar55e354a2009-08-08 18:44:18 +0000218
Andreas Neustifterff271e12009-08-26 15:13:44 +0000219 // Since the entry block is the first one and has no predecessors, the edge
220 // (0,entry) is inserted with the starting weight of 1.
Daniel Dunbar55e354a2009-08-08 18:44:18 +0000221 BasicBlock *entry = &F.getEntryBlock();
222 BlockInformation[&F][entry] = 1;
223
224 Edge edge = getEdge(0,entry);
Andreas Neustifterff271e12009-08-26 15:13:44 +0000225 EdgeInformation[&F][edge] = 1; printEdgeWeight(edge);
Daniel Dunbar55e354a2009-08-08 18:44:18 +0000226 recurseBasicBlock(entry);
227
Andreas Neustifterff271e12009-08-26 15:13:44 +0000228 // In case something went wrong, clear all results, not profiling info is
229 // available.
Daniel Dunbar55e354a2009-08-08 18:44:18 +0000230 if (BBisVisited.size() != F.size()) {
231 DEBUG(errs() << "-- could not estimate profile, using default profile\n");
232 FunctionInformation.erase(&F);
233 BlockInformation[&F].clear();
234 for (Function::iterator BB = F.begin(), BBE = F.end(); BB != BBE; ++BB) {
235 for (pred_iterator bbi = pred_begin(BB), bbe = pred_end(BB);
236 bbi != bbe; ++bbi) {
237 Edge e = getEdge(*bbi,BB);
Andreas Neustifterff271e12009-08-26 15:13:44 +0000238 EdgeInformation[&F][e] = 1;
239 printEdgeWeight(e);
Daniel Dunbar55e354a2009-08-08 18:44:18 +0000240 }
241 for (succ_iterator bbi = succ_begin(BB), bbe = succ_end(BB);
242 bbi != bbe; ++bbi) {
243 Edge e = getEdge(BB,*bbi);
Andreas Neustifterff271e12009-08-26 15:13:44 +0000244 EdgeInformation[&F][e] = 1;
245 printEdgeWeight(e);
Daniel Dunbar55e354a2009-08-08 18:44:18 +0000246 }
247 }
248 }
249
250 return false;
251}