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/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"
33 STATISTIC(NumEdgesRead, "The # of edges read.");
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"));
41 class VISIBILITY_HIDDEN LoaderPass : public ModulePass, public ProfileInfo {
43 std::set<Edge> SpanningTree;
44 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 &, unsigned &);
64 virtual unsigned readEdge(ProfileInfo::Edge, std::vector<unsigned>,
67 /// run - Load the profile information from the specified file.
68 virtual bool runOnModule(Module &M);
70 } // End of anonymous namespace
72 char LoaderPass::ID = 0;
73 static RegisterPass<LoaderPass>
74 X("profile-loader", "Load profile information from llvmprof.out", false, true);
76 static RegisterAnalysisGroup<ProfileInfo> Y(X);
78 ModulePass *llvm::createProfileLoaderPass() { return new LoaderPass(); }
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);
87 void LoaderPass::readEdgeOrRemember(Edge edge, Edge &tocalc,
88 unsigned &uncalc, unsigned &count) {
90 if ((w = getEdgeWeight(edge)) == MissingValue) {
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) {
102 // break recursion if already visited
103 if (BBisUnvisited.find(BB) == BBisUnvisited.end()) return;
104 BBisUnvisited.erase(BB);
107 for (succ_const_iterator bbi = succ_begin(BB), bbe = succ_end(BB);
109 recurseBasicBlock(*bbi);
111 for (pred_const_iterator bbi = pred_begin(BB), bbe = pred_end(BB);
113 recurseBasicBlock(*bbi);
117 unsigned uncalculated = 0;
119 // collect weights of all incoming and outgoing edges, rememer edges that
121 unsigned incount = 0;
122 SmallSet<const BasicBlock*,8> pred_visited;
123 pred_const_iterator bbi = pred_begin(BB), bbe = pred_end(BB);
125 readEdgeOrRemember(getEdge(0, BB),edgetocalc,uncalculated,incount);
127 for (;bbi != bbe; ++bbi) {
128 if (pred_visited.insert(*bbi)) {
129 readEdgeOrRemember(getEdge(*bbi, BB),edgetocalc,uncalculated,incount);
133 unsigned outcount = 0;
134 SmallSet<const BasicBlock*,8> succ_visited;
135 succ_const_iterator sbbi = succ_begin(BB), sbbe = succ_end(BB);
137 readEdgeOrRemember(getEdge(BB, 0),edgetocalc,uncalculated,outcount);
139 for (;sbbi != sbbe; ++sbbi) {
140 if (succ_visited.insert(*sbbi)) {
141 readEdgeOrRemember(getEdge(BB, *sbbi),edgetocalc,uncalculated,outcount);
145 // if exactly one edge weight was missing, calculate it and remove it from
147 if (uncalculated == 1) {
148 if (incount < outcount) {
149 EdgeInformation[BB->getParent()][edgetocalc] = outcount-incount;
151 EdgeInformation[BB->getParent()][edgetocalc] = incount-outcount;
153 DEBUG(errs() << "--Calc Edge Counter for " << edgetocalc << ": "
154 << format("%g", getEdgeWeight(edgetocalc)) << "\n");
155 SpanningTree.erase(edgetocalc);
159 unsigned LoaderPass::readEdge(ProfileInfo::Edge e, std::vector<unsigned> ECs,
160 unsigned ei, Function *F) {
161 if (ei < ECs.size()) {
162 double weight = ECs[ei];
164 EdgeInformation[F][e] += weight;
165 DEBUG(errs()<<"--Read Edge Counter for " << e
166 <<" (# "<<ei<<"): "<<(unsigned)getEdgeWeight(e)<<"\n");
168 // This happens only when loading edges for optimal edge profiling.
169 SpanningTree.insert(e);
177 bool LoaderPass::runOnModule(Module &M) {
178 ProfileInfoLoader PIL("profile-loader", Filename, M);
180 EdgeInformation.clear();
181 std::vector<unsigned> ECs = PIL.getRawEdgeCounts();
182 if (ECs.size() > 0) {
184 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
185 if (F->isDeclaration()) continue;
186 DEBUG(errs()<<"Working on "<<F->getNameStr()<<"\n");
187 ei = readEdge(getEdge(0,&F->getEntryBlock()), ECs, ei, F);
188 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
189 // Okay, we have to add a counter of each outgoing edge. If the
190 // outgoing edge is not critical don't split it, just insert the counter
191 // in the source or destination of the edge.
192 TerminatorInst *TI = BB->getTerminator();
193 for (unsigned s = 0, e = TI->getNumSuccessors(); s != e; ++s) {
194 ei = readEdge(getEdge(BB,TI->getSuccessor(s)), ECs, ei, F);
198 if (ei != ECs.size()) {
199 errs() << "WARNING: profile information is inconsistent with "
200 << "the current program!\n";
205 ECs = PIL.getRawOptimalEdgeCounts();
206 if (ECs.size() > 0) {
208 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
209 if (F->isDeclaration()) continue;
210 DEBUG(errs()<<"Working on "<<F->getNameStr()<<"\n");
211 ei = readEdge(getEdge(0,&F->getEntryBlock()), ECs, ei, F);
212 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
213 TerminatorInst *TI = BB->getTerminator();
214 if (TI->getNumSuccessors() == 0) {
215 ei = readEdge(getEdge(BB,0), ECs, ei, F);
217 for (unsigned s = 0, e = TI->getNumSuccessors(); s != e; ++s) {
218 ei = readEdge(getEdge(BB,TI->getSuccessor(s)), ECs, ei, F);
221 while (SpanningTree.size() > 0) {
223 unsigned size = SpanningTree.size();
225 BBisUnvisited.clear();
226 for (std::set<Edge>::iterator ei = SpanningTree.begin(),
227 ee = SpanningTree.end(); ei != ee; ++ei) {
228 BBisUnvisited.insert(ei->first);
229 BBisUnvisited.insert(ei->second);
231 while (BBisUnvisited.size() > 0) {
232 recurseBasicBlock(*BBisUnvisited.begin());
235 if (SpanningTree.size() == size) {
237 for (std::set<Edge>::iterator ei = SpanningTree.begin(),
238 ee = SpanningTree.end(); ei != ee; ++ei) {
239 DEBUG(errs()<<"("<<(ei->first?ei->first->getName():"0")<<","
240 <<(ei->second?ei->second->getName():"0")<<"),");
242 assert(0 && "No edge calculated!");
247 if (ei != ECs.size()) {
248 errs() << "WARNING: profile information is inconsistent with "
249 << "the current program!\n";
254 BlockInformation.clear();
255 std::vector<unsigned> BCs = PIL.getRawBlockCounts();
256 if (BCs.size() > 0) {
258 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
259 if (F->isDeclaration()) continue;
260 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
262 BlockInformation[F][BB] = BCs[bi++];
264 if (bi != BCs.size()) {
265 errs() << "WARNING: profile information is inconsistent with "
266 << "the current program!\n";
270 FunctionInformation.clear();
271 std::vector<unsigned> FCs = PIL.getRawFunctionCounts();
272 if (FCs.size() > 0) {
274 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
275 if (F->isDeclaration()) continue;
277 FunctionInformation[F] = FCs[fi++];
279 if (fi != FCs.size()) {
280 errs() << "WARNING: profile information is inconsistent with "
281 << "the current program!\n";