Chris Lattner | 927fec3 | 2004-02-11 06:10:05 +0000 | [diff] [blame] | 1 | //===- ProfileInfoLoaderPass.cpp - LLVM Pass to load profile info ---------===// |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 2 | // |
Chris Lattner | 927fec3 | 2004-02-11 06:10:05 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 7 | // |
Chris Lattner | 927fec3 | 2004-02-11 06:10:05 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements a concrete implementation of profiling information that |
| 11 | // loads the information from a profile dump file. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
Andreas Neustifter | da5ea94 | 2009-09-01 19:08:51 +0000 | [diff] [blame] | 14 | #define DEBUG_TYPE "profile-loader" |
Chris Lattner | 96ab5ca | 2004-03-08 22:04:08 +0000 | [diff] [blame] | 15 | #include "llvm/BasicBlock.h" |
| 16 | #include "llvm/InstrTypes.h" |
Daniel Dunbar | ee16638 | 2009-08-05 15:55:56 +0000 | [diff] [blame] | 17 | #include "llvm/Module.h" |
Chris Lattner | 927fec3 | 2004-02-11 06:10:05 +0000 | [diff] [blame] | 18 | #include "llvm/Pass.h" |
Jeff Cohen | 534927d | 2005-01-08 22:01:16 +0000 | [diff] [blame] | 19 | #include "llvm/Analysis/Passes.h" |
Chris Lattner | 927fec3 | 2004-02-11 06:10:05 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/ProfileInfo.h" |
| 21 | #include "llvm/Analysis/ProfileInfoLoader.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 22 | #include "llvm/Support/CommandLine.h" |
Reid Spencer | d7d83db | 2007-02-05 23:42:17 +0000 | [diff] [blame] | 23 | #include "llvm/Support/Compiler.h" |
Andreas Neustifter | da5ea94 | 2009-09-01 19:08:51 +0000 | [diff] [blame] | 24 | #include "llvm/Support/CFG.h" |
| 25 | #include "llvm/Support/Debug.h" |
Chris Lattner | a81d29b | 2009-08-23 07:33:14 +0000 | [diff] [blame] | 26 | #include "llvm/Support/raw_ostream.h" |
Andreas Neustifter | da5ea94 | 2009-09-01 19:08:51 +0000 | [diff] [blame] | 27 | #include "llvm/Support/Format.h" |
| 28 | #include "llvm/ADT/Statistic.h" |
| 29 | #include "llvm/ADT/SmallSet.h" |
| 30 | #include <set> |
Chris Lattner | 927fec3 | 2004-02-11 06:10:05 +0000 | [diff] [blame] | 31 | using namespace llvm; |
| 32 | |
Andreas Neustifter | da5ea94 | 2009-09-01 19:08:51 +0000 | [diff] [blame] | 33 | STATISTIC(NumEdgesRead, "The # of edges read."); |
| 34 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 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")); |
Chris Lattner | 945871d | 2004-02-11 18:21:05 +0000 | [diff] [blame] | 39 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 40 | namespace { |
Reid Spencer | d7d83db | 2007-02-05 23:42:17 +0000 | [diff] [blame] | 41 | class VISIBILITY_HIDDEN LoaderPass : public ModulePass, public ProfileInfo { |
Chris Lattner | 927fec3 | 2004-02-11 06:10:05 +0000 | [diff] [blame] | 42 | std::string Filename; |
Andreas Neustifter | da5ea94 | 2009-09-01 19:08:51 +0000 | [diff] [blame] | 43 | std::set<Edge> SpanningTree; |
| 44 | std::set<const BasicBlock*> BBisUnvisited; |
Andreas Neustifter | b4b1c9f | 2009-09-10 07:12:35 +0000 | [diff] [blame^] | 45 | unsigned ReadCount; |
Chris Lattner | 927fec3 | 2004-02-11 06:10:05 +0000 | [diff] [blame] | 46 | public: |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 47 | static char ID; // Class identification, replacement for typeinfo |
Dan Gohman | c2bbfc1 | 2007-08-01 15:32:29 +0000 | [diff] [blame] | 48 | explicit LoaderPass(const std::string &filename = "") |
Dan Gohman | ae73dc1 | 2008-09-04 17:05:41 +0000 | [diff] [blame] | 49 | : ModulePass(&ID), Filename(filename) { |
Chris Lattner | 945871d | 2004-02-11 18:21:05 +0000 | [diff] [blame] | 50 | if (filename.empty()) Filename = ProfileInfoFilename; |
| 51 | } |
Chris Lattner | 927fec3 | 2004-02-11 06:10:05 +0000 | [diff] [blame] | 52 | |
| 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 Neustifter | da5ea94 | 2009-09-01 19:08:51 +0000 | [diff] [blame] | 61 | // 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 Neustifter | b4b1c9f | 2009-09-10 07:12:35 +0000 | [diff] [blame^] | 65 | virtual void readEdge(ProfileInfo::Edge, std::vector<unsigned>&); |
Andreas Neustifter | da5ea94 | 2009-09-01 19:08:51 +0000 | [diff] [blame] | 66 | |
Chris Lattner | 927fec3 | 2004-02-11 06:10:05 +0000 | [diff] [blame] | 67 | /// run - Load the profile information from the specified file. |
Chris Lattner | b12914b | 2004-09-20 04:48:05 +0000 | [diff] [blame] | 68 | virtual bool runOnModule(Module &M); |
Chris Lattner | 927fec3 | 2004-02-11 06:10:05 +0000 | [diff] [blame] | 69 | }; |
Chris Lattner | 927fec3 | 2004-02-11 06:10:05 +0000 | [diff] [blame] | 70 | } // End of anonymous namespace |
| 71 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 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 | |
Jeff Cohen | 6e400f7 | 2005-01-10 03:56:27 +0000 | [diff] [blame] | 78 | ModulePass *llvm::createProfileLoaderPass() { return new LoaderPass(); } |
Chris Lattner | 927fec3 | 2004-02-11 06:10:05 +0000 | [diff] [blame] | 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 | |
Andreas Neustifter | da5ea94 | 2009-09-01 19:08:51 +0000 | [diff] [blame] | 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 | |
Andreas Neustifter | b4b1c9f | 2009-09-10 07:12:35 +0000 | [diff] [blame^] | 159 | void LoaderPass::readEdge(ProfileInfo::Edge e, |
| 160 | std::vector<unsigned> &ECs) { |
| 161 | if (ReadCount < ECs.size()) { |
| 162 | double weight = ECs[ReadCount++]; |
| 163 | if (weight != ~0U) { |
| 164 | EdgeInformation[getFunction(e)][e] += weight; |
| 165 | DEBUG(errs() << "--Read Edge Counter for " << e |
| 166 | << " (# "<< (ReadCount-1) << "): " |
| 167 | << (unsigned)getEdgeWeight(e) << "\n"); |
| 168 | } else { |
| 169 | // This happens only if reading optimal profiling information, not when |
| 170 | // reading regular profiling information. |
| 171 | SpanningTree.insert(e); |
| 172 | } |
Andreas Neustifter | da5ea94 | 2009-09-01 19:08:51 +0000 | [diff] [blame] | 173 | } |
| 174 | } |
| 175 | |
Chris Lattner | b12914b | 2004-09-20 04:48:05 +0000 | [diff] [blame] | 176 | bool LoaderPass::runOnModule(Module &M) { |
Chris Lattner | 62e84f3 | 2004-03-08 21:30:35 +0000 | [diff] [blame] | 177 | ProfileInfoLoader PIL("profile-loader", Filename, M); |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 178 | |
Daniel Dunbar | caaa493 | 2009-08-08 17:43:09 +0000 | [diff] [blame] | 179 | EdgeInformation.clear(); |
Andreas Neustifter | b4b1c9f | 2009-09-10 07:12:35 +0000 | [diff] [blame^] | 180 | std::vector<unsigned> Counters = PIL.getRawEdgeCounts(); |
| 181 | if (Counters.size() > 0) { |
| 182 | ReadCount = 0; |
Daniel Dunbar | caaa493 | 2009-08-08 17:43:09 +0000 | [diff] [blame] | 183 | for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) { |
| 184 | if (F->isDeclaration()) continue; |
Andreas Neustifter | b4b1c9f | 2009-09-10 07:12:35 +0000 | [diff] [blame^] | 185 | DEBUG(errs()<<"Working on "<<F->getNameStr()<<"\n"); |
| 186 | readEdge(getEdge(0,&F->getEntryBlock()), Counters); |
Daniel Dunbar | caaa493 | 2009-08-08 17:43:09 +0000 | [diff] [blame] | 187 | for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) { |
| 188 | // Okay, we have to add a counter of each outgoing edge. If the |
| 189 | // outgoing edge is not critical don't split it, just insert the counter |
| 190 | // in the source or destination of the edge. |
| 191 | TerminatorInst *TI = BB->getTerminator(); |
| 192 | for (unsigned s = 0, e = TI->getNumSuccessors(); s != e; ++s) { |
Andreas Neustifter | b4b1c9f | 2009-09-10 07:12:35 +0000 | [diff] [blame^] | 193 | readEdge(getEdge(BB,TI->getSuccessor(s)), Counters); |
Daniel Dunbar | caaa493 | 2009-08-08 17:43:09 +0000 | [diff] [blame] | 194 | } |
Chris Lattner | 96ab5ca | 2004-03-08 22:04:08 +0000 | [diff] [blame] | 195 | } |
Chris Lattner | 96ab5ca | 2004-03-08 22:04:08 +0000 | [diff] [blame] | 196 | } |
Andreas Neustifter | b4b1c9f | 2009-09-10 07:12:35 +0000 | [diff] [blame^] | 197 | if (ReadCount != Counters.size()) { |
Chris Lattner | a81d29b | 2009-08-23 07:33:14 +0000 | [diff] [blame] | 198 | errs() << "WARNING: profile information is inconsistent with " |
| 199 | << "the current program!\n"; |
Daniel Dunbar | caaa493 | 2009-08-08 17:43:09 +0000 | [diff] [blame] | 200 | } |
Andreas Neustifter | b4b1c9f | 2009-09-10 07:12:35 +0000 | [diff] [blame^] | 201 | NumEdgesRead = ReadCount; |
Andreas Neustifter | da5ea94 | 2009-09-01 19:08:51 +0000 | [diff] [blame] | 202 | } |
| 203 | |
Andreas Neustifter | b4b1c9f | 2009-09-10 07:12:35 +0000 | [diff] [blame^] | 204 | Counters = PIL.getRawOptimalEdgeCounts(); |
| 205 | if (Counters.size() > 0) { |
| 206 | ReadCount = 0; |
Andreas Neustifter | da5ea94 | 2009-09-01 19:08:51 +0000 | [diff] [blame] | 207 | for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) { |
| 208 | if (F->isDeclaration()) continue; |
| 209 | DEBUG(errs()<<"Working on "<<F->getNameStr()<<"\n"); |
Andreas Neustifter | b4b1c9f | 2009-09-10 07:12:35 +0000 | [diff] [blame^] | 210 | readEdge(getEdge(0,&F->getEntryBlock()), Counters); |
Andreas Neustifter | da5ea94 | 2009-09-01 19:08:51 +0000 | [diff] [blame] | 211 | for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) { |
| 212 | TerminatorInst *TI = BB->getTerminator(); |
| 213 | if (TI->getNumSuccessors() == 0) { |
Andreas Neustifter | b4b1c9f | 2009-09-10 07:12:35 +0000 | [diff] [blame^] | 214 | readEdge(getEdge(BB,0), Counters); |
Andreas Neustifter | da5ea94 | 2009-09-01 19:08:51 +0000 | [diff] [blame] | 215 | } |
| 216 | for (unsigned s = 0, e = TI->getNumSuccessors(); s != e; ++s) { |
Andreas Neustifter | b4b1c9f | 2009-09-10 07:12:35 +0000 | [diff] [blame^] | 217 | readEdge(getEdge(BB,TI->getSuccessor(s)), Counters); |
Andreas Neustifter | da5ea94 | 2009-09-01 19:08:51 +0000 | [diff] [blame] | 218 | } |
| 219 | } |
| 220 | while (SpanningTree.size() > 0) { |
| 221 | #if 0 |
| 222 | unsigned size = SpanningTree.size(); |
| 223 | #endif |
| 224 | BBisUnvisited.clear(); |
| 225 | for (std::set<Edge>::iterator ei = SpanningTree.begin(), |
| 226 | ee = SpanningTree.end(); ei != ee; ++ei) { |
| 227 | BBisUnvisited.insert(ei->first); |
| 228 | BBisUnvisited.insert(ei->second); |
| 229 | } |
| 230 | while (BBisUnvisited.size() > 0) { |
| 231 | recurseBasicBlock(*BBisUnvisited.begin()); |
| 232 | } |
| 233 | #if 0 |
| 234 | if (SpanningTree.size() == size) { |
| 235 | DEBUG(errs()<<"{"); |
| 236 | for (std::set<Edge>::iterator ei = SpanningTree.begin(), |
| 237 | ee = SpanningTree.end(); ei != ee; ++ei) { |
| 238 | DEBUG(errs()<<"("<<(ei->first?ei->first->getName():"0")<<"," |
| 239 | <<(ei->second?ei->second->getName():"0")<<"),"); |
| 240 | } |
| 241 | assert(0 && "No edge calculated!"); |
| 242 | } |
| 243 | #endif |
| 244 | } |
| 245 | } |
Andreas Neustifter | b4b1c9f | 2009-09-10 07:12:35 +0000 | [diff] [blame^] | 246 | if (ReadCount != Counters.size()) { |
Andreas Neustifter | da5ea94 | 2009-09-01 19:08:51 +0000 | [diff] [blame] | 247 | errs() << "WARNING: profile information is inconsistent with " |
| 248 | << "the current program!\n"; |
| 249 | } |
Andreas Neustifter | b4b1c9f | 2009-09-10 07:12:35 +0000 | [diff] [blame^] | 250 | NumEdgesRead = ReadCount; |
Daniel Dunbar | c9008c5 | 2009-08-05 21:51:16 +0000 | [diff] [blame] | 251 | } |
Daniel Dunbar | caaa493 | 2009-08-08 17:43:09 +0000 | [diff] [blame] | 252 | |
| 253 | BlockInformation.clear(); |
Andreas Neustifter | b4b1c9f | 2009-09-10 07:12:35 +0000 | [diff] [blame^] | 254 | Counters = PIL.getRawBlockCounts(); |
| 255 | if (Counters.size() > 0) { |
| 256 | ReadCount = 0; |
Daniel Dunbar | caaa493 | 2009-08-08 17:43:09 +0000 | [diff] [blame] | 257 | for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) { |
| 258 | if (F->isDeclaration()) continue; |
| 259 | for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) |
Andreas Neustifter | b4b1c9f | 2009-09-10 07:12:35 +0000 | [diff] [blame^] | 260 | if (ReadCount < Counters.size()) |
| 261 | BlockInformation[F][BB] = Counters[ReadCount++]; |
Daniel Dunbar | caaa493 | 2009-08-08 17:43:09 +0000 | [diff] [blame] | 262 | } |
Andreas Neustifter | b4b1c9f | 2009-09-10 07:12:35 +0000 | [diff] [blame^] | 263 | if (ReadCount != Counters.size()) { |
Chris Lattner | a81d29b | 2009-08-23 07:33:14 +0000 | [diff] [blame] | 264 | errs() << "WARNING: profile information is inconsistent with " |
| 265 | << "the current program!\n"; |
Daniel Dunbar | caaa493 | 2009-08-08 17:43:09 +0000 | [diff] [blame] | 266 | } |
| 267 | } |
| 268 | |
| 269 | FunctionInformation.clear(); |
Andreas Neustifter | b4b1c9f | 2009-09-10 07:12:35 +0000 | [diff] [blame^] | 270 | Counters = PIL.getRawFunctionCounts(); |
| 271 | if (Counters.size() > 0) { |
| 272 | ReadCount = 0; |
Daniel Dunbar | caaa493 | 2009-08-08 17:43:09 +0000 | [diff] [blame] | 273 | for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) { |
| 274 | if (F->isDeclaration()) continue; |
Andreas Neustifter | b4b1c9f | 2009-09-10 07:12:35 +0000 | [diff] [blame^] | 275 | if (ReadCount < Counters.size()) |
| 276 | FunctionInformation[F] = Counters[ReadCount++]; |
Daniel Dunbar | caaa493 | 2009-08-08 17:43:09 +0000 | [diff] [blame] | 277 | } |
Andreas Neustifter | b4b1c9f | 2009-09-10 07:12:35 +0000 | [diff] [blame^] | 278 | if (ReadCount != Counters.size()) { |
Chris Lattner | a81d29b | 2009-08-23 07:33:14 +0000 | [diff] [blame] | 279 | errs() << "WARNING: profile information is inconsistent with " |
| 280 | << "the current program!\n"; |
Daniel Dunbar | caaa493 | 2009-08-08 17:43:09 +0000 | [diff] [blame] | 281 | } |
Chris Lattner | 945871d | 2004-02-11 18:21:05 +0000 | [diff] [blame] | 282 | } |
Chris Lattner | 96ab5ca | 2004-03-08 22:04:08 +0000 | [diff] [blame] | 283 | |
Chris Lattner | 927fec3 | 2004-02-11 06:10:05 +0000 | [diff] [blame] | 284 | return false; |
| 285 | } |