--- Reverse-merging r80908 into '.':
[oota-llvm.git] / lib / Analysis / ProfileInfoLoaderPass.cpp
1 //===- ProfileInfoLoaderPass.cpp - LLVM Pass to load 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 // loads the information from a profile dump file.
12 //
13 //===----------------------------------------------------------------------===//
14 #define DEBUG_TYPE "profile-loader"
15 #include "llvm/BasicBlock.h"
16 #include "llvm/InstrTypes.h"
17 #include "llvm/Module.h"
18 #include "llvm/Pass.h"
19 #include "llvm/Analysis/Passes.h"
20 #include "llvm/Analysis/ProfileInfo.h"
21 #include "llvm/Analysis/ProfileInfoLoader.h"
22 #include "llvm/Support/CommandLine.h"
23 #include "llvm/Support/Compiler.h"
24 #include "llvm/Support/CFG.h"
25 #include "llvm/Support/Debug.h"
26 #include "llvm/Support/raw_ostream.h"
27 #include "llvm/Support/Format.h"
28 #include "llvm/ADT/Statistic.h"
29 #include "llvm/ADT/SmallSet.h"
30 #include <set>
31 using namespace llvm;
32
33 STATISTIC(NumEdgesRead, "The # of edges read.");
34
35 static cl::opt<std::string>
36 ProfileInfoFilename("profile-info-file", cl::init("llvmprof.out"),
37                     cl::value_desc("filename"),
38                     cl::desc("Profile file loaded by -profile-loader"));
39
40 namespace {
41   class VISIBILITY_HIDDEN LoaderPass : public ModulePass, public ProfileInfo {
42     std::string Filename;
43     std::set<Edge> SpanningTree;
44     std::set<const BasicBlock*> BBisUnvisited;
45   public:
46     static char ID; // Class identification, replacement for typeinfo
47     explicit LoaderPass(const std::string &filename = "")
48       : ModulePass(&ID), Filename(filename) {
49       if (filename.empty()) Filename = ProfileInfoFilename;
50     }
51
52     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
53       AU.setPreservesAll();
54     }
55
56     virtual const char *getPassName() const {
57       return "Profiling information loader";
58     }
59
60     // recurseBasicBlock() - Calculates the edge weights for as much basic
61     // blocks as possbile.
62     virtual void recurseBasicBlock(const BasicBlock *BB);
63     virtual void readEdgeOrRemember(Edge, Edge&, unsigned &, unsigned &);
64     virtual void readOrRememberEdge(ProfileInfo::Edge, unsigned,
65                                     unsigned, Function*);
66
67     /// run - Load the profile information from the specified file.
68     virtual bool runOnModule(Module &M);
69   };
70 }  // End of anonymous namespace
71
72 char LoaderPass::ID = 0;
73 static RegisterPass<LoaderPass>
74 X("profile-loader", "Load profile information from llvmprof.out", false, true);
75
76 static RegisterAnalysisGroup<ProfileInfo> Y(X);
77
78 ModulePass *llvm::createProfileLoaderPass() { return new LoaderPass(); }
79
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.
83 Pass *llvm::createProfileLoaderPass(const std::string &Filename) {
84   return new LoaderPass(Filename);
85 }
86
87 void 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.
100 void 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
159 void LoaderPass::readOrRememberEdge(ProfileInfo::Edge e,
160                                     unsigned weight, unsigned ei,
161                                     Function *F) {
162   if (weight != (unsigned)MissingValue) {
163     EdgeInformation[F][e] += weight;
164     DEBUG(errs()<<"--Read Edge Counter for " << e 
165                 <<" (# "<<ei<<"): "<<(unsigned)getEdgeWeight(e)<<"\n");
166   } else {
167     SpanningTree.insert(e);
168   }
169 }
170
171 bool LoaderPass::runOnModule(Module &M) {
172   ProfileInfoLoader PIL("profile-loader", Filename, M);
173
174   EdgeInformation.clear();
175   std::vector<unsigned> ECs = PIL.getRawEdgeCounts();
176   if (ECs.size() > 0) {
177     unsigned ei = 0;
178     for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
179       if (F->isDeclaration()) continue;
180       if (ei < ECs.size())
181         EdgeInformation[F][ProfileInfo::getEdge(0, &F->getEntryBlock())] +=
182           ECs[ei++];
183       for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
184         // Okay, we have to add a counter of each outgoing edge.  If the
185         // outgoing edge is not critical don't split it, just insert the counter
186         // in the source or destination of the edge.
187         TerminatorInst *TI = BB->getTerminator();
188         for (unsigned s = 0, e = TI->getNumSuccessors(); s != e; ++s) {
189           if (ei < ECs.size())
190             EdgeInformation[F][ProfileInfo::getEdge(BB, TI->getSuccessor(s))] +=
191               ECs[ei++];
192         }
193       }
194     }
195     if (ei != ECs.size()) {
196       errs() << "WARNING: profile information is inconsistent with "
197              << "the current program!\n";
198     }
199     NumEdgesRead = ei;
200   }
201
202   ECs = PIL.getRawOptimalEdgeCounts();
203   if (ECs.size() > 0) {
204     unsigned ei = 0;
205     for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
206       if (F->isDeclaration()) continue;
207       DEBUG(errs()<<"Working on "<<F->getNameStr()<<"\n");
208       if (ei < ECs.size()) {
209         readOrRememberEdge(getEdge(0,&F->getEntryBlock()), ECs[ei], ei, F); 
210         ei++;
211       }
212       for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
213         TerminatorInst *TI = BB->getTerminator();
214         if (TI->getNumSuccessors() == 0) {
215           if (ei < ECs.size()) {
216             readOrRememberEdge(getEdge(BB,0), ECs[ei], ei, F); ei++;
217           }
218         }
219         for (unsigned s = 0, e = TI->getNumSuccessors(); s != e; ++s) {
220           if (ei < ECs.size()) {
221             readOrRememberEdge(getEdge(BB,TI->getSuccessor(s)), ECs[ei], ei, F);
222             ei++;
223           }
224         }
225       }
226       while (SpanningTree.size() > 0) {
227 #if 0
228         unsigned size = SpanningTree.size();
229 #endif
230         BBisUnvisited.clear();
231         for (std::set<Edge>::iterator ei = SpanningTree.begin(),
232              ee = SpanningTree.end(); ei != ee; ++ei) {
233           BBisUnvisited.insert(ei->first);
234           BBisUnvisited.insert(ei->second);
235         }
236         while (BBisUnvisited.size() > 0) {
237           recurseBasicBlock(*BBisUnvisited.begin());
238         }
239 #if 0
240         if (SpanningTree.size() == size) {
241           DEBUG(errs()<<"{");
242           for (std::set<Edge>::iterator ei = SpanningTree.begin(),
243                ee = SpanningTree.end(); ei != ee; ++ei) {
244             DEBUG(errs()<<"("<<(ei->first?ei->first->getName():"0")<<","
245                         <<(ei->second?ei->second->getName():"0")<<"),");
246           }
247           assert(0 && "No edge calculated!");
248         }
249 #endif
250       }
251     }
252     if (ei != ECs.size()) {
253       errs() << "WARNING: profile information is inconsistent with "
254              << "the current program!\n";
255     }
256     NumEdgesRead = ei;
257   }
258
259   BlockInformation.clear();
260   std::vector<unsigned> BCs = PIL.getRawBlockCounts();
261   if (BCs.size() > 0) {
262     unsigned bi = 0;
263     for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
264       if (F->isDeclaration()) continue;
265       for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
266         if (bi < BCs.size())
267           BlockInformation[F][BB] = BCs[bi++];
268     }
269     if (bi != BCs.size()) {
270       errs() << "WARNING: profile information is inconsistent with "
271              << "the current program!\n";
272     }
273   }
274
275   FunctionInformation.clear();
276   std::vector<unsigned> FCs = PIL.getRawFunctionCounts();
277   if (FCs.size() > 0) {
278     unsigned fi = 0;
279     for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
280       if (F->isDeclaration()) continue;
281       if (fi < FCs.size())
282         FunctionInformation[F] = FCs[fi++];
283     }
284     if (fi != FCs.size()) {
285       errs() << "WARNING: profile information is inconsistent with "
286              << "the current program!\n";
287     }
288   }
289
290   return false;
291 }