blob: 89d90bca2166d57a7b79be8342b5868e94bd029a [file] [log] [blame]
Chris Lattner927fec32004-02-11 06:10:05 +00001//===- ProfileInfoLoaderPass.cpp - LLVM Pass to load profile info ---------===//
Misha Brukman2b37d7c2005-04-21 21:13:18 +00002//
Chris Lattner927fec32004-02-11 06:10:05 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman2b37d7c2005-04-21 21:13:18 +00007//
Chris Lattner927fec32004-02-11 06:10:05 +00008//===----------------------------------------------------------------------===//
9//
10// This file implements a concrete implementation of profiling information that
11// loads the information from a profile dump file.
12//
13//===----------------------------------------------------------------------===//
Andreas Neustifterda5ea942009-09-01 19:08:51 +000014#define DEBUG_TYPE "profile-loader"
Chris Lattner96ab5ca2004-03-08 22:04:08 +000015#include "llvm/BasicBlock.h"
16#include "llvm/InstrTypes.h"
Daniel Dunbaree166382009-08-05 15:55:56 +000017#include "llvm/Module.h"
Chris Lattner927fec32004-02-11 06:10:05 +000018#include "llvm/Pass.h"
Jeff Cohen534927d2005-01-08 22:01:16 +000019#include "llvm/Analysis/Passes.h"
Chris Lattner927fec32004-02-11 06:10:05 +000020#include "llvm/Analysis/ProfileInfo.h"
21#include "llvm/Analysis/ProfileInfoLoader.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000022#include "llvm/Support/CommandLine.h"
Reid Spencerd7d83db2007-02-05 23:42:17 +000023#include "llvm/Support/Compiler.h"
Andreas Neustifterda5ea942009-09-01 19:08:51 +000024#include "llvm/Support/CFG.h"
25#include "llvm/Support/Debug.h"
Chris Lattnera81d29b2009-08-23 07:33:14 +000026#include "llvm/Support/raw_ostream.h"
Andreas Neustifterda5ea942009-09-01 19:08:51 +000027#include "llvm/Support/Format.h"
28#include "llvm/ADT/Statistic.h"
29#include "llvm/ADT/SmallSet.h"
30#include <set>
Chris Lattner927fec32004-02-11 06:10:05 +000031using namespace llvm;
32
Andreas Neustifterda5ea942009-09-01 19:08:51 +000033STATISTIC(NumEdgesRead, "The # of edges read.");
34
Dan Gohman844731a2008-05-13 00:00:25 +000035static cl::opt<std::string>
36ProfileInfoFilename("profile-info-file", cl::init("llvmprof.out"),
37 cl::value_desc("filename"),
38 cl::desc("Profile file loaded by -profile-loader"));
Chris Lattner945871d2004-02-11 18:21:05 +000039
Dan Gohman844731a2008-05-13 00:00:25 +000040namespace {
Reid Spencerd7d83db2007-02-05 23:42:17 +000041 class VISIBILITY_HIDDEN LoaderPass : public ModulePass, public ProfileInfo {
Chris Lattner927fec32004-02-11 06:10:05 +000042 std::string Filename;
Andreas Neustifterda5ea942009-09-01 19:08:51 +000043 std::set<Edge> SpanningTree;
44 std::set<const BasicBlock*> BBisUnvisited;
Andreas Neustifterb4b1c9f2009-09-10 07:12:35 +000045 unsigned ReadCount;
Chris Lattner927fec32004-02-11 06:10:05 +000046 public:
Devang Patel19974732007-05-03 01:11:54 +000047 static char ID; // Class identification, replacement for typeinfo
Dan Gohmanc2bbfc12007-08-01 15:32:29 +000048 explicit LoaderPass(const std::string &filename = "")
Dan Gohmanae73dc12008-09-04 17:05:41 +000049 : ModulePass(&ID), Filename(filename) {
Chris Lattner945871d2004-02-11 18:21:05 +000050 if (filename.empty()) Filename = ProfileInfoFilename;
51 }
Chris Lattner927fec32004-02-11 06:10:05 +000052
53 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
54 AU.setPreservesAll();
55 }
56
57 virtual const char *getPassName() const {
58 return "Profiling information loader";
59 }
60
Andreas Neustifterda5ea942009-09-01 19:08:51 +000061 // recurseBasicBlock() - Calculates the edge weights for as much basic
62 // blocks as possbile.
63 virtual void recurseBasicBlock(const BasicBlock *BB);
64 virtual void readEdgeOrRemember(Edge, Edge&, unsigned &, unsigned &);
Andreas Neustifterb4b1c9f2009-09-10 07:12:35 +000065 virtual void readEdge(ProfileInfo::Edge, std::vector<unsigned>&);
Andreas Neustifterda5ea942009-09-01 19:08:51 +000066
Chris Lattner927fec32004-02-11 06:10:05 +000067 /// run - Load the profile information from the specified file.
Chris Lattnerb12914b2004-09-20 04:48:05 +000068 virtual bool runOnModule(Module &M);
Chris Lattner927fec32004-02-11 06:10:05 +000069 };
Chris Lattner927fec32004-02-11 06:10:05 +000070} // End of anonymous namespace
71
Dan Gohman844731a2008-05-13 00:00:25 +000072char LoaderPass::ID = 0;
73static RegisterPass<LoaderPass>
74X("profile-loader", "Load profile information from llvmprof.out", false, true);
75
76static RegisterAnalysisGroup<ProfileInfo> Y(X);
77
Jeff Cohen6e400f72005-01-10 03:56:27 +000078ModulePass *llvm::createProfileLoaderPass() { return new LoaderPass(); }
Chris Lattner927fec32004-02-11 06:10:05 +000079
80/// createProfileLoaderPass - This function returns a Pass that loads the
81/// profiling information for the module from the specified filename, making it
82/// available to the optimizers.
83Pass *llvm::createProfileLoaderPass(const std::string &Filename) {
84 return new LoaderPass(Filename);
85}
86
Andreas Neustifterda5ea942009-09-01 19:08:51 +000087void LoaderPass::readEdgeOrRemember(Edge edge, Edge &tocalc,
88 unsigned &uncalc, unsigned &count) {
89 double w;
90 if ((w = getEdgeWeight(edge)) == MissingValue) {
91 tocalc = edge;
92 uncalc++;
93 } else {
94 count+=w;
95 }
96}
97
98// recurseBasicBlock - Visits all neighbours of a block and then tries to
99// calculate the missing edge values.
100void LoaderPass::recurseBasicBlock(const BasicBlock *BB) {
101
102 // break recursion if already visited
103 if (BBisUnvisited.find(BB) == BBisUnvisited.end()) return;
104 BBisUnvisited.erase(BB);
105 if (!BB) return;
106
107 for (succ_const_iterator bbi = succ_begin(BB), bbe = succ_end(BB);
108 bbi != bbe; ++bbi) {
109 recurseBasicBlock(*bbi);
110 }
111 for (pred_const_iterator bbi = pred_begin(BB), bbe = pred_end(BB);
112 bbi != bbe; ++bbi) {
113 recurseBasicBlock(*bbi);
114 }
115
116 Edge edgetocalc;
117 unsigned uncalculated = 0;
118
119 // collect weights of all incoming and outgoing edges, rememer edges that
120 // have no value
121 unsigned incount = 0;
122 SmallSet<const BasicBlock*,8> pred_visited;
123 pred_const_iterator bbi = pred_begin(BB), bbe = pred_end(BB);
124 if (bbi==bbe) {
125 readEdgeOrRemember(getEdge(0, BB),edgetocalc,uncalculated,incount);
126 }
127 for (;bbi != bbe; ++bbi) {
128 if (pred_visited.insert(*bbi)) {
129 readEdgeOrRemember(getEdge(*bbi, BB),edgetocalc,uncalculated,incount);
130 }
131 }
132
133 unsigned outcount = 0;
134 SmallSet<const BasicBlock*,8> succ_visited;
135 succ_const_iterator sbbi = succ_begin(BB), sbbe = succ_end(BB);
136 if (sbbi==sbbe) {
137 readEdgeOrRemember(getEdge(BB, 0),edgetocalc,uncalculated,outcount);
138 }
139 for (;sbbi != sbbe; ++sbbi) {
140 if (succ_visited.insert(*sbbi)) {
141 readEdgeOrRemember(getEdge(BB, *sbbi),edgetocalc,uncalculated,outcount);
142 }
143 }
144
145 // if exactly one edge weight was missing, calculate it and remove it from
146 // spanning tree
147 if (uncalculated == 1) {
148 if (incount < outcount) {
149 EdgeInformation[BB->getParent()][edgetocalc] = outcount-incount;
150 } else {
151 EdgeInformation[BB->getParent()][edgetocalc] = incount-outcount;
152 }
153 DEBUG(errs() << "--Calc Edge Counter for " << edgetocalc << ": "
154 << format("%g", getEdgeWeight(edgetocalc)) << "\n");
155 SpanningTree.erase(edgetocalc);
156 }
157}
158
Andreas Neustifterb4b1c9f2009-09-10 07:12:35 +0000159void LoaderPass::readEdge(ProfileInfo::Edge e,
160 std::vector<unsigned> &ECs) {
161 if (ReadCount < ECs.size()) {
162 double weight = ECs[ReadCount++];
Andreas Neustifter92332722009-09-16 11:35:50 +0000163 if (weight != ProfileInfoLoader::Uncounted) {
164 // Here the data realm changes from the unsigned of the file to the
165 // double of the ProfileInfo. This conversion is save because we know
166 // that everything thats representable in unsinged is also representable
167 // in double.
168 EdgeInformation[getFunction(e)][e] += (double)weight;
169
Andreas Neustifterb4b1c9f2009-09-10 07:12:35 +0000170 DEBUG(errs() << "--Read Edge Counter for " << e
171 << " (# "<< (ReadCount-1) << "): "
172 << (unsigned)getEdgeWeight(e) << "\n");
173 } else {
174 // This happens only if reading optimal profiling information, not when
175 // reading regular profiling information.
176 SpanningTree.insert(e);
177 }
Andreas Neustifterda5ea942009-09-01 19:08:51 +0000178 }
179}
180
Chris Lattnerb12914b2004-09-20 04:48:05 +0000181bool LoaderPass::runOnModule(Module &M) {
Chris Lattner62e84f32004-03-08 21:30:35 +0000182 ProfileInfoLoader PIL("profile-loader", Filename, M);
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000183
Daniel Dunbarcaaa4932009-08-08 17:43:09 +0000184 EdgeInformation.clear();
Andreas Neustifterb4b1c9f2009-09-10 07:12:35 +0000185 std::vector<unsigned> Counters = PIL.getRawEdgeCounts();
186 if (Counters.size() > 0) {
187 ReadCount = 0;
Daniel Dunbarcaaa4932009-08-08 17:43:09 +0000188 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
189 if (F->isDeclaration()) continue;
Andreas Neustifterb4b1c9f2009-09-10 07:12:35 +0000190 DEBUG(errs()<<"Working on "<<F->getNameStr()<<"\n");
191 readEdge(getEdge(0,&F->getEntryBlock()), Counters);
Daniel Dunbarcaaa4932009-08-08 17:43:09 +0000192 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
Daniel Dunbarcaaa4932009-08-08 17:43:09 +0000193 TerminatorInst *TI = BB->getTerminator();
194 for (unsigned s = 0, e = TI->getNumSuccessors(); s != e; ++s) {
Andreas Neustifterb4b1c9f2009-09-10 07:12:35 +0000195 readEdge(getEdge(BB,TI->getSuccessor(s)), Counters);
Daniel Dunbarcaaa4932009-08-08 17:43:09 +0000196 }
Chris Lattner96ab5ca2004-03-08 22:04:08 +0000197 }
Chris Lattner96ab5ca2004-03-08 22:04:08 +0000198 }
Andreas Neustifterb4b1c9f2009-09-10 07:12:35 +0000199 if (ReadCount != Counters.size()) {
Chris Lattnera81d29b2009-08-23 07:33:14 +0000200 errs() << "WARNING: profile information is inconsistent with "
201 << "the current program!\n";
Daniel Dunbarcaaa4932009-08-08 17:43:09 +0000202 }
Andreas Neustifterb4b1c9f2009-09-10 07:12:35 +0000203 NumEdgesRead = ReadCount;
Andreas Neustifterda5ea942009-09-01 19:08:51 +0000204 }
205
Andreas Neustifterb4b1c9f2009-09-10 07:12:35 +0000206 Counters = PIL.getRawOptimalEdgeCounts();
207 if (Counters.size() > 0) {
208 ReadCount = 0;
Andreas Neustifterda5ea942009-09-01 19:08:51 +0000209 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
210 if (F->isDeclaration()) continue;
211 DEBUG(errs()<<"Working on "<<F->getNameStr()<<"\n");
Andreas Neustifterb4b1c9f2009-09-10 07:12:35 +0000212 readEdge(getEdge(0,&F->getEntryBlock()), Counters);
Andreas Neustifterda5ea942009-09-01 19:08:51 +0000213 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
214 TerminatorInst *TI = BB->getTerminator();
215 if (TI->getNumSuccessors() == 0) {
Andreas Neustifterb4b1c9f2009-09-10 07:12:35 +0000216 readEdge(getEdge(BB,0), Counters);
Andreas Neustifterda5ea942009-09-01 19:08:51 +0000217 }
218 for (unsigned s = 0, e = TI->getNumSuccessors(); s != e; ++s) {
Andreas Neustifterb4b1c9f2009-09-10 07:12:35 +0000219 readEdge(getEdge(BB,TI->getSuccessor(s)), Counters);
Andreas Neustifterda5ea942009-09-01 19:08:51 +0000220 }
221 }
222 while (SpanningTree.size() > 0) {
223#if 0
224 unsigned size = SpanningTree.size();
225#endif
226 BBisUnvisited.clear();
227 for (std::set<Edge>::iterator ei = SpanningTree.begin(),
228 ee = SpanningTree.end(); ei != ee; ++ei) {
229 BBisUnvisited.insert(ei->first);
230 BBisUnvisited.insert(ei->second);
231 }
232 while (BBisUnvisited.size() > 0) {
233 recurseBasicBlock(*BBisUnvisited.begin());
234 }
235#if 0
236 if (SpanningTree.size() == size) {
237 DEBUG(errs()<<"{");
238 for (std::set<Edge>::iterator ei = SpanningTree.begin(),
239 ee = SpanningTree.end(); ei != ee; ++ei) {
240 DEBUG(errs()<<"("<<(ei->first?ei->first->getName():"0")<<","
241 <<(ei->second?ei->second->getName():"0")<<"),");
242 }
243 assert(0 && "No edge calculated!");
244 }
245#endif
246 }
247 }
Andreas Neustifterb4b1c9f2009-09-10 07:12:35 +0000248 if (ReadCount != Counters.size()) {
Andreas Neustifterda5ea942009-09-01 19:08:51 +0000249 errs() << "WARNING: profile information is inconsistent with "
250 << "the current program!\n";
251 }
Andreas Neustifterb4b1c9f2009-09-10 07:12:35 +0000252 NumEdgesRead = ReadCount;
Daniel Dunbarc9008c52009-08-05 21:51:16 +0000253 }
Daniel Dunbarcaaa4932009-08-08 17:43:09 +0000254
255 BlockInformation.clear();
Andreas Neustifterb4b1c9f2009-09-10 07:12:35 +0000256 Counters = PIL.getRawBlockCounts();
257 if (Counters.size() > 0) {
258 ReadCount = 0;
Daniel Dunbarcaaa4932009-08-08 17:43:09 +0000259 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
260 if (F->isDeclaration()) continue;
261 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
Andreas Neustifterb4b1c9f2009-09-10 07:12:35 +0000262 if (ReadCount < Counters.size())
Andreas Neustifter92332722009-09-16 11:35:50 +0000263 // Here the data realm changes from the unsigned of the file to the
264 // double of the ProfileInfo. This conversion is save because we know
265 // that everything thats representable in unsinged is also
266 // representable in double.
267 BlockInformation[F][BB] = (double)Counters[ReadCount++];
Daniel Dunbarcaaa4932009-08-08 17:43:09 +0000268 }
Andreas Neustifterb4b1c9f2009-09-10 07:12:35 +0000269 if (ReadCount != Counters.size()) {
Chris Lattnera81d29b2009-08-23 07:33:14 +0000270 errs() << "WARNING: profile information is inconsistent with "
271 << "the current program!\n";
Daniel Dunbarcaaa4932009-08-08 17:43:09 +0000272 }
273 }
274
275 FunctionInformation.clear();
Andreas Neustifterb4b1c9f2009-09-10 07:12:35 +0000276 Counters = PIL.getRawFunctionCounts();
277 if (Counters.size() > 0) {
278 ReadCount = 0;
Daniel Dunbarcaaa4932009-08-08 17:43:09 +0000279 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
280 if (F->isDeclaration()) continue;
Andreas Neustifterb4b1c9f2009-09-10 07:12:35 +0000281 if (ReadCount < Counters.size())
Andreas Neustifter92332722009-09-16 11:35:50 +0000282 // Here the data realm changes from the unsigned of the file to the
283 // double of the ProfileInfo. This conversion is save because we know
284 // that everything thats representable in unsinged is also
285 // representable in double.
286 FunctionInformation[F] = (double)Counters[ReadCount++];
Daniel Dunbarcaaa4932009-08-08 17:43:09 +0000287 }
Andreas Neustifterb4b1c9f2009-09-10 07:12:35 +0000288 if (ReadCount != Counters.size()) {
Chris Lattnera81d29b2009-08-23 07:33:14 +0000289 errs() << "WARNING: profile information is inconsistent with "
290 << "the current program!\n";
Daniel Dunbarcaaa4932009-08-08 17:43:09 +0000291 }
Chris Lattner945871d2004-02-11 18:21:05 +0000292 }
Chris Lattner96ab5ca2004-03-08 22:04:08 +0000293
Chris Lattner927fec32004-02-11 06:10:05 +0000294 return false;
295}