1 //===- ProfileInfoLoaderPass.cpp - LLVM Pass to load profile info ---------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements a concrete implementation of profiling information that
11 // loads the information from a profile dump file.
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/CFG.h"
24 #include "llvm/Support/Debug.h"
25 #include "llvm/Support/raw_ostream.h"
26 #include "llvm/Support/Format.h"
27 #include "llvm/ADT/Statistic.h"
28 #include "llvm/ADT/SmallSet.h"
32 STATISTIC(NumEdgesRead, "The # of edges read.");
34 static cl::opt<std::string>
35 ProfileInfoFilename("profile-info-file", cl::init("llvmprof.out"),
36 cl::value_desc("filename"),
37 cl::desc("Profile file loaded by -profile-loader"));
40 class LoaderPass : public ModulePass, public ProfileInfo {
42 std::set<Edge> SpanningTree;
43 std::set<const BasicBlock*> BBisUnvisited;
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;
52 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
56 virtual const char *getPassName() const {
57 return "Profiling information loader";
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 &, double &);
64 virtual void readEdge(ProfileInfo::Edge, std::vector<unsigned>&);
66 /// run - Load the profile information from the specified file.
67 virtual bool runOnModule(Module &M);
69 } // End of anonymous namespace
71 char LoaderPass::ID = 0;
72 static RegisterPass<LoaderPass>
73 X("profile-loader", "Load profile information from llvmprof.out", false, true);
75 static RegisterAnalysisGroup<ProfileInfo> Y(X);
77 const PassInfo *llvm::ProfileLoaderPassID = &X;
79 ModulePass *llvm::createProfileLoaderPass() { return new LoaderPass(); }
81 /// createProfileLoaderPass - This function returns a Pass that loads the
82 /// profiling information for the module from the specified filename, making it
83 /// available to the optimizers.
84 Pass *llvm::createProfileLoaderPass(const std::string &Filename) {
85 return new LoaderPass(Filename);
88 void LoaderPass::readEdgeOrRemember(Edge edge, Edge &tocalc,
89 unsigned &uncalc, double &count) {
91 if ((w = getEdgeWeight(edge)) == MissingValue) {
99 // recurseBasicBlock - Visits all neighbours of a block and then tries to
100 // calculate the missing edge values.
101 void LoaderPass::recurseBasicBlock(const BasicBlock *BB) {
103 // break recursion if already visited
104 if (BBisUnvisited.find(BB) == BBisUnvisited.end()) return;
105 BBisUnvisited.erase(BB);
108 for (succ_const_iterator bbi = succ_begin(BB), bbe = succ_end(BB);
110 recurseBasicBlock(*bbi);
112 for (pred_const_iterator bbi = pred_begin(BB), bbe = pred_end(BB);
114 recurseBasicBlock(*bbi);
118 if (CalculateMissingEdge(BB, tocalc)) {
119 SpanningTree.erase(tocalc);
123 void LoaderPass::readEdge(ProfileInfo::Edge e,
124 std::vector<unsigned> &ECs) {
125 if (ReadCount < ECs.size()) {
126 double weight = ECs[ReadCount++];
127 if (weight != ProfileInfoLoader::Uncounted) {
128 // Here the data realm changes from the unsigned of the file to the
129 // double of the ProfileInfo. This conversion is save because we know
130 // that everything thats representable in unsinged is also representable
132 EdgeInformation[getFunction(e)][e] += (double)weight;
134 DEBUG(errs() << "--Read Edge Counter for " << e
135 << " (# "<< (ReadCount-1) << "): "
136 << (unsigned)getEdgeWeight(e) << "\n");
138 // This happens only if reading optimal profiling information, not when
139 // reading regular profiling information.
140 SpanningTree.insert(e);
145 bool LoaderPass::runOnModule(Module &M) {
146 ProfileInfoLoader PIL("profile-loader", Filename, M);
148 EdgeInformation.clear();
149 std::vector<unsigned> Counters = PIL.getRawEdgeCounts();
150 if (Counters.size() > 0) {
152 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
153 if (F->isDeclaration()) continue;
154 DEBUG(errs()<<"Working on "<<F->getNameStr()<<"\n");
155 readEdge(getEdge(0,&F->getEntryBlock()), Counters);
156 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
157 TerminatorInst *TI = BB->getTerminator();
158 for (unsigned s = 0, e = TI->getNumSuccessors(); s != e; ++s) {
159 readEdge(getEdge(BB,TI->getSuccessor(s)), Counters);
163 if (ReadCount != Counters.size()) {
164 errs() << "WARNING: profile information is inconsistent with "
165 << "the current program!\n";
167 NumEdgesRead = ReadCount;
170 Counters = PIL.getRawOptimalEdgeCounts();
171 if (Counters.size() > 0) {
173 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
174 if (F->isDeclaration()) continue;
175 DEBUG(errs()<<"Working on "<<F->getNameStr()<<"\n");
176 readEdge(getEdge(0,&F->getEntryBlock()), Counters);
177 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
178 TerminatorInst *TI = BB->getTerminator();
179 if (TI->getNumSuccessors() == 0) {
180 readEdge(getEdge(BB,0), Counters);
182 for (unsigned s = 0, e = TI->getNumSuccessors(); s != e; ++s) {
183 readEdge(getEdge(BB,TI->getSuccessor(s)), Counters);
186 while (SpanningTree.size() > 0) {
188 unsigned size = SpanningTree.size();
190 BBisUnvisited.clear();
191 for (std::set<Edge>::iterator ei = SpanningTree.begin(),
192 ee = SpanningTree.end(); ei != ee; ++ei) {
193 BBisUnvisited.insert(ei->first);
194 BBisUnvisited.insert(ei->second);
196 while (BBisUnvisited.size() > 0) {
197 recurseBasicBlock(*BBisUnvisited.begin());
200 if (SpanningTree.size() == size) {
202 for (std::set<Edge>::iterator ei = SpanningTree.begin(),
203 ee = SpanningTree.end(); ei != ee; ++ei) {
204 DEBUG(errs()<< *ei <<",");
206 assert(0 && "No edge calculated!");
211 if (ReadCount != Counters.size()) {
212 errs() << "WARNING: profile information is inconsistent with "
213 << "the current program!\n";
215 NumEdgesRead = ReadCount;
218 BlockInformation.clear();
219 Counters = PIL.getRawBlockCounts();
220 if (Counters.size() > 0) {
222 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
223 if (F->isDeclaration()) continue;
224 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
225 if (ReadCount < Counters.size())
226 // Here the data realm changes from the unsigned of the file to the
227 // double of the ProfileInfo. This conversion is save because we know
228 // that everything thats representable in unsinged is also
229 // representable in double.
230 BlockInformation[F][BB] = (double)Counters[ReadCount++];
232 if (ReadCount != Counters.size()) {
233 errs() << "WARNING: profile information is inconsistent with "
234 << "the current program!\n";
238 FunctionInformation.clear();
239 Counters = PIL.getRawFunctionCounts();
240 if (Counters.size() > 0) {
242 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
243 if (F->isDeclaration()) continue;
244 if (ReadCount < Counters.size())
245 // Here the data realm changes from the unsigned of the file to the
246 // double of the ProfileInfo. This conversion is save because we know
247 // that everything thats representable in unsinged is also
248 // representable in double.
249 FunctionInformation[F] = (double)Counters[ReadCount++];
251 if (ReadCount != Counters.size()) {
252 errs() << "WARNING: profile information is inconsistent with "
253 << "the current program!\n";