Daniel Dunbar | 55e354a | 2009-08-08 18:44:18 +0000 | [diff] [blame] | 1 | //===- ProfileEstimatorPass.cpp - LLVM Pass to estimate 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 | // estimates the profiling information in a very crude and unimaginative way. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | #define DEBUG_TYPE "profile-estimator" |
| 15 | #include "llvm/Pass.h" |
| 16 | #include "llvm/Analysis/Passes.h" |
| 17 | #include "llvm/Analysis/ProfileInfo.h" |
| 18 | #include "llvm/Analysis/LoopInfo.h" |
| 19 | #include "llvm/Support/CommandLine.h" |
| 20 | #include "llvm/Support/Debug.h" |
| 21 | #include "llvm/Support/raw_ostream.h" |
Andreas Neustifter | ff271e1 | 2009-08-26 15:13:44 +0000 | [diff] [blame] | 22 | #include "llvm/Support/Format.h" |
Daniel Dunbar | 55e354a | 2009-08-08 18:44:18 +0000 | [diff] [blame] | 23 | using namespace llvm; |
| 24 | |
| 25 | static cl::opt<double> |
Andreas Neustifter | ff271e1 | 2009-08-26 15:13:44 +0000 | [diff] [blame] | 26 | LoopWeight( |
Daniel Dunbar | 55e354a | 2009-08-08 18:44:18 +0000 | [diff] [blame] | 27 | "profile-estimator-loop-weight", cl::init(10), |
| 28 | cl::value_desc("loop-weight"), |
| 29 | cl::desc("Number of loop executions used for profile-estimator") |
| 30 | ); |
| 31 | |
| 32 | namespace { |
| 33 | class VISIBILITY_HIDDEN ProfileEstimatorPass : |
| 34 | public FunctionPass, public ProfileInfo { |
| 35 | double ExecCount; |
| 36 | LoopInfo *LI; |
| 37 | std::set<BasicBlock*> BBisVisited; |
| 38 | std::map<Loop*,double> LoopExitWeights; |
| 39 | public: |
| 40 | static char ID; // Class identification, replacement for typeinfo |
| 41 | explicit ProfileEstimatorPass(const double execcount = 0) |
| 42 | : FunctionPass(&ID), ExecCount(execcount) { |
Andreas Neustifter | ff271e1 | 2009-08-26 15:13:44 +0000 | [diff] [blame] | 43 | if (execcount == 0) ExecCount = LoopWeight; |
Daniel Dunbar | 55e354a | 2009-08-08 18:44:18 +0000 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 47 | AU.setPreservesAll(); |
| 48 | AU.addRequired<LoopInfo>(); |
| 49 | } |
| 50 | |
| 51 | virtual const char *getPassName() const { |
| 52 | return "Profiling information estimator"; |
| 53 | } |
| 54 | |
| 55 | /// run - Estimate the profile information from the specified file. |
| 56 | virtual bool runOnFunction(Function &F); |
| 57 | |
Andreas Neustifter | 0c0de66 | 2009-09-10 16:30:38 +0000 | [diff] [blame^] | 58 | BasicBlock *recurseBasicBlock(BasicBlock *BB); |
Andreas Neustifter | ff271e1 | 2009-08-26 15:13:44 +0000 | [diff] [blame] | 59 | |
| 60 | void inline printEdgeWeight(Edge); |
Daniel Dunbar | 55e354a | 2009-08-08 18:44:18 +0000 | [diff] [blame] | 61 | }; |
| 62 | } // End of anonymous namespace |
| 63 | |
| 64 | char ProfileEstimatorPass::ID = 0; |
| 65 | static RegisterPass<ProfileEstimatorPass> |
| 66 | X("profile-estimator", "Estimate profiling information", false, true); |
| 67 | |
| 68 | static RegisterAnalysisGroup<ProfileInfo> Y(X); |
| 69 | |
| 70 | namespace llvm { |
| 71 | const PassInfo *ProfileEstimatorPassID = &X; |
| 72 | |
| 73 | FunctionPass *createProfileEstimatorPass() { |
| 74 | return new ProfileEstimatorPass(); |
| 75 | } |
| 76 | |
Andreas Neustifter | ff271e1 | 2009-08-26 15:13:44 +0000 | [diff] [blame] | 77 | /// createProfileEstimatorPass - This function returns a Pass that estimates |
| 78 | /// profiling information using the given loop execution count. |
Daniel Dunbar | 55e354a | 2009-08-08 18:44:18 +0000 | [diff] [blame] | 79 | Pass *createProfileEstimatorPass(const unsigned execcount) { |
| 80 | return new ProfileEstimatorPass(execcount); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | static double ignoreMissing(double w) { |
| 85 | if (w == ProfileInfo::MissingValue) return 0; |
| 86 | return w; |
| 87 | } |
| 88 | |
Andreas Neustifter | 0c0de66 | 2009-09-10 16:30:38 +0000 | [diff] [blame^] | 89 | static void inline printEdgeError(ProfileInfo::Edge e) { |
| 90 | DEBUG(errs() << "-- Edge " << e << " is not calculated, returning\n"); |
Andreas Neustifter | ff271e1 | 2009-08-26 15:13:44 +0000 | [diff] [blame] | 91 | } |
Daniel Dunbar | 55e354a | 2009-08-08 18:44:18 +0000 | [diff] [blame] | 92 | |
Andreas Neustifter | ff271e1 | 2009-08-26 15:13:44 +0000 | [diff] [blame] | 93 | void inline ProfileEstimatorPass::printEdgeWeight(Edge E) { |
| 94 | DEBUG(errs() << "-- Weight of Edge " << E << ":" |
| 95 | << format("%g", getEdgeWeight(E)) << "\n"); |
| 96 | } |
Daniel Dunbar | 55e354a | 2009-08-08 18:44:18 +0000 | [diff] [blame] | 97 | |
| 98 | // recurseBasicBlock() - This calculates the ProfileInfo estimation for a |
| 99 | // single block and then recurses into the successors. |
Andreas Neustifter | 0c0de66 | 2009-09-10 16:30:38 +0000 | [diff] [blame^] | 100 | BasicBlock* ProfileEstimatorPass::recurseBasicBlock(BasicBlock *BB) { |
Daniel Dunbar | 55e354a | 2009-08-08 18:44:18 +0000 | [diff] [blame] | 101 | |
Andreas Neustifter | ff271e1 | 2009-08-26 15:13:44 +0000 | [diff] [blame] | 102 | // Break the recursion if this BasicBlock was already visited. |
Andreas Neustifter | 0c0de66 | 2009-09-10 16:30:38 +0000 | [diff] [blame^] | 103 | if (BBisVisited.find(BB) != BBisVisited.end()) return 0; |
Daniel Dunbar | 55e354a | 2009-08-08 18:44:18 +0000 | [diff] [blame] | 104 | |
Andreas Neustifter | ff271e1 | 2009-08-26 15:13:44 +0000 | [diff] [blame] | 105 | // Check if incoming edges are calculated already, if BB is header allow |
| 106 | // backedges that are uncalculated for now. |
Daniel Dunbar | 55e354a | 2009-08-08 18:44:18 +0000 | [diff] [blame] | 107 | bool BBisHeader = LI->isLoopHeader(BB); |
| 108 | Loop* BBLoop = LI->getLoopFor(BB); |
| 109 | |
| 110 | double BBWeight = 0; |
| 111 | std::set<BasicBlock*> ProcessedPreds; |
| 112 | for ( pred_iterator bbi = pred_begin(BB), bbe = pred_end(BB); |
| 113 | bbi != bbe; ++bbi ) { |
Andreas Neustifter | 0c0de66 | 2009-09-10 16:30:38 +0000 | [diff] [blame^] | 114 | Edge edge = getEdge(*bbi,BB); |
| 115 | double w = getEdgeWeight(edge); |
Daniel Dunbar | 55e354a | 2009-08-08 18:44:18 +0000 | [diff] [blame] | 116 | if (ProcessedPreds.insert(*bbi).second) { |
Andreas Neustifter | 0c0de66 | 2009-09-10 16:30:38 +0000 | [diff] [blame^] | 117 | BBWeight += ignoreMissing(w); |
Daniel Dunbar | 55e354a | 2009-08-08 18:44:18 +0000 | [diff] [blame] | 118 | } |
Andreas Neustifter | 0c0de66 | 2009-09-10 16:30:38 +0000 | [diff] [blame^] | 119 | if (BBisHeader && BBLoop->contains(*bbi)) { |
| 120 | printEdgeError(edge); |
Daniel Dunbar | 55e354a | 2009-08-08 18:44:18 +0000 | [diff] [blame] | 121 | continue; |
| 122 | } |
Andreas Neustifter | 0c0de66 | 2009-09-10 16:30:38 +0000 | [diff] [blame^] | 123 | if (w == MissingValue) { |
| 124 | printEdgeError(edge); |
| 125 | return BB; |
Daniel Dunbar | 55e354a | 2009-08-08 18:44:18 +0000 | [diff] [blame] | 126 | } |
| 127 | } |
| 128 | if (getExecutionCount(BB) != MissingValue) { |
| 129 | BBWeight = getExecutionCount(BB); |
| 130 | } |
| 131 | |
Andreas Neustifter | ff271e1 | 2009-08-26 15:13:44 +0000 | [diff] [blame] | 132 | // Fetch all necessary information for current block. |
Daniel Dunbar | 55e354a | 2009-08-08 18:44:18 +0000 | [diff] [blame] | 133 | SmallVector<Edge, 8> ExitEdges; |
| 134 | SmallVector<Edge, 8> Edges; |
| 135 | if (BBLoop) { |
| 136 | BBLoop->getExitEdges(ExitEdges); |
| 137 | } |
| 138 | |
Andreas Neustifter | ff271e1 | 2009-08-26 15:13:44 +0000 | [diff] [blame] | 139 | // If block is an loop header, first subtract all weights from edges that |
Daniel Dunbar | 55e354a | 2009-08-08 18:44:18 +0000 | [diff] [blame] | 140 | // exit this loop, then distribute remaining weight on to the edges exiting |
Andreas Neustifter | ff271e1 | 2009-08-26 15:13:44 +0000 | [diff] [blame] | 141 | // this loop. Finally the weight of the block is increased, to simulate |
| 142 | // several executions of this loop. |
Daniel Dunbar | 55e354a | 2009-08-08 18:44:18 +0000 | [diff] [blame] | 143 | if (BBisHeader) { |
| 144 | double incoming = BBWeight; |
Andreas Neustifter | ff271e1 | 2009-08-26 15:13:44 +0000 | [diff] [blame] | 145 | // Subtract the flow leaving the loop. |
Andreas Neustifter | 0c0de66 | 2009-09-10 16:30:38 +0000 | [diff] [blame^] | 146 | std::set<Edge> ProcessedExits; |
Daniel Dunbar | 55e354a | 2009-08-08 18:44:18 +0000 | [diff] [blame] | 147 | for (SmallVector<Edge, 8>::iterator ei = ExitEdges.begin(), |
| 148 | ee = ExitEdges.end(); ei != ee; ++ei) { |
Andreas Neustifter | 0c0de66 | 2009-09-10 16:30:38 +0000 | [diff] [blame^] | 149 | if (ProcessedExits.insert(*ei).second) { |
| 150 | double w = getEdgeWeight(*ei); |
| 151 | if (w == MissingValue) { |
| 152 | Edges.push_back(*ei); |
| 153 | } else { |
| 154 | incoming -= w; |
| 155 | } |
Daniel Dunbar | 55e354a | 2009-08-08 18:44:18 +0000 | [diff] [blame] | 156 | } |
| 157 | } |
Andreas Neustifter | 0c0de66 | 2009-09-10 16:30:38 +0000 | [diff] [blame^] | 158 | // If no exit edges, create one: |
| 159 | if (Edges.size() == 0) { |
| 160 | BasicBlock *Latch = BBLoop->getLoopLatch(); |
| 161 | if (Latch) { |
| 162 | Edge edge = getEdge(Latch,0); |
| 163 | EdgeInformation[BB->getParent()][edge] = BBWeight; |
| 164 | printEdgeWeight(edge); |
| 165 | edge = getEdge(Latch, BB); |
| 166 | EdgeInformation[BB->getParent()][edge] = BBWeight * ExecCount; |
| 167 | printEdgeWeight(edge); |
| 168 | } |
| 169 | } |
| 170 | |
Andreas Neustifter | ff271e1 | 2009-08-26 15:13:44 +0000 | [diff] [blame] | 171 | // Distribute remaining weight onto the exit edges. |
Daniel Dunbar | 55e354a | 2009-08-08 18:44:18 +0000 | [diff] [blame] | 172 | for (SmallVector<Edge, 8>::iterator ei = Edges.begin(), ee = Edges.end(); |
| 173 | ei != ee; ++ei) { |
| 174 | EdgeInformation[BB->getParent()][*ei] += incoming/Edges.size(); |
Andreas Neustifter | ff271e1 | 2009-08-26 15:13:44 +0000 | [diff] [blame] | 175 | printEdgeWeight(*ei); |
Daniel Dunbar | 55e354a | 2009-08-08 18:44:18 +0000 | [diff] [blame] | 176 | } |
Andreas Neustifter | ff271e1 | 2009-08-26 15:13:44 +0000 | [diff] [blame] | 177 | // Increase flow into the loop. |
Daniel Dunbar | 55e354a | 2009-08-08 18:44:18 +0000 | [diff] [blame] | 178 | BBWeight *= (ExecCount+1); |
| 179 | } |
| 180 | |
Andreas Neustifter | ff271e1 | 2009-08-26 15:13:44 +0000 | [diff] [blame] | 181 | // Remove from current flow of block all the successor edges that already |
| 182 | // have some flow on them. |
Daniel Dunbar | 55e354a | 2009-08-08 18:44:18 +0000 | [diff] [blame] | 183 | Edges.clear(); |
| 184 | std::set<BasicBlock*> ProcessedSuccs; |
Andreas Neustifter | ff271e1 | 2009-08-26 15:13:44 +0000 | [diff] [blame] | 185 | |
| 186 | // Otherwise consider weight of outgoing edges and store them for |
Andreas Neustifter | e885af9 | 2009-09-01 10:06:05 +0000 | [diff] [blame] | 187 | // distribution of remaining weight. In case the block has no successors |
| 188 | // create a (BB,0) edge. |
| 189 | succ_iterator bbi = succ_begin(BB), bbe = succ_end(BB); |
| 190 | if (bbi == bbe) { |
| 191 | Edge edge = getEdge(BB,0); |
| 192 | EdgeInformation[BB->getParent()][edge] = BBWeight; |
| 193 | printEdgeWeight(edge); |
| 194 | } |
Andreas Neustifter | 19531d1 | 2009-09-01 19:01:59 +0000 | [diff] [blame] | 195 | for ( ; bbi != bbe; ++bbi ) { |
Daniel Dunbar | 55e354a | 2009-08-08 18:44:18 +0000 | [diff] [blame] | 196 | if (ProcessedSuccs.insert(*bbi).second) { |
| 197 | Edge edge = getEdge(BB,*bbi); |
| 198 | double w = getEdgeWeight(edge); |
| 199 | if (w != MissingValue) { |
| 200 | BBWeight -= getEdgeWeight(edge); |
| 201 | } else { |
| 202 | Edges.push_back(edge); |
| 203 | } |
| 204 | } |
| 205 | } |
| 206 | |
Andreas Neustifter | ff271e1 | 2009-08-26 15:13:44 +0000 | [diff] [blame] | 207 | // Distribute remaining flow onto the outgoing edges. |
Daniel Dunbar | 55e354a | 2009-08-08 18:44:18 +0000 | [diff] [blame] | 208 | for (SmallVector<Edge, 8>::iterator ei = Edges.begin(), ee = Edges.end(); |
| 209 | ei != ee; ++ei) { |
| 210 | EdgeInformation[BB->getParent()][*ei] += BBWeight/Edges.size(); |
Andreas Neustifter | ff271e1 | 2009-08-26 15:13:44 +0000 | [diff] [blame] | 211 | printEdgeWeight(*ei); |
Daniel Dunbar | 55e354a | 2009-08-08 18:44:18 +0000 | [diff] [blame] | 212 | } |
| 213 | |
Andreas Neustifter | ff271e1 | 2009-08-26 15:13:44 +0000 | [diff] [blame] | 214 | // Mark this Block visited and recurse into successors. |
Daniel Dunbar | 55e354a | 2009-08-08 18:44:18 +0000 | [diff] [blame] | 215 | BBisVisited.insert(BB); |
Andreas Neustifter | 0c0de66 | 2009-09-10 16:30:38 +0000 | [diff] [blame^] | 216 | BasicBlock *Uncalculated = 0; |
Daniel Dunbar | 55e354a | 2009-08-08 18:44:18 +0000 | [diff] [blame] | 217 | for ( succ_iterator bbi = succ_begin(BB), bbe = succ_end(BB); |
Andreas Neustifter | 0c0de66 | 2009-09-10 16:30:38 +0000 | [diff] [blame^] | 218 | bbi != bbe; ++bbi ) { |
| 219 | BasicBlock* ret = recurseBasicBlock(*bbi); |
| 220 | if (!Uncalculated) |
| 221 | Uncalculated = ret; |
Daniel Dunbar | 55e354a | 2009-08-08 18:44:18 +0000 | [diff] [blame] | 222 | } |
Andreas Neustifter | 0c0de66 | 2009-09-10 16:30:38 +0000 | [diff] [blame^] | 223 | if (BBisVisited.find(Uncalculated) != BBisVisited.end()) |
| 224 | return 0; |
| 225 | return Uncalculated; |
Daniel Dunbar | 55e354a | 2009-08-08 18:44:18 +0000 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | bool ProfileEstimatorPass::runOnFunction(Function &F) { |
| 229 | if (F.isDeclaration()) return false; |
| 230 | |
| 231 | LI = &getAnalysis<LoopInfo>(); |
| 232 | FunctionInformation.erase(&F); |
| 233 | BlockInformation[&F].clear(); |
| 234 | EdgeInformation[&F].clear(); |
| 235 | BBisVisited.clear(); |
| 236 | |
Andreas Neustifter | ff271e1 | 2009-08-26 15:13:44 +0000 | [diff] [blame] | 237 | DEBUG(errs() << "Working on function " << F.getNameStr() << "\n"); |
Daniel Dunbar | 55e354a | 2009-08-08 18:44:18 +0000 | [diff] [blame] | 238 | |
Andreas Neustifter | ff271e1 | 2009-08-26 15:13:44 +0000 | [diff] [blame] | 239 | // Since the entry block is the first one and has no predecessors, the edge |
| 240 | // (0,entry) is inserted with the starting weight of 1. |
Daniel Dunbar | 55e354a | 2009-08-08 18:44:18 +0000 | [diff] [blame] | 241 | BasicBlock *entry = &F.getEntryBlock(); |
| 242 | BlockInformation[&F][entry] = 1; |
| 243 | |
| 244 | Edge edge = getEdge(0,entry); |
Andreas Neustifter | ff271e1 | 2009-08-26 15:13:44 +0000 | [diff] [blame] | 245 | EdgeInformation[&F][edge] = 1; printEdgeWeight(edge); |
Andreas Neustifter | 0c0de66 | 2009-09-10 16:30:38 +0000 | [diff] [blame^] | 246 | BasicBlock *BB = entry; |
| 247 | while (BB) { |
| 248 | BB = recurseBasicBlock(BB); |
| 249 | if (BB) { |
Daniel Dunbar | 55e354a | 2009-08-08 18:44:18 +0000 | [diff] [blame] | 250 | for (pred_iterator bbi = pred_begin(BB), bbe = pred_end(BB); |
| 251 | bbi != bbe; ++bbi) { |
| 252 | Edge e = getEdge(*bbi,BB); |
Andreas Neustifter | 0c0de66 | 2009-09-10 16:30:38 +0000 | [diff] [blame^] | 253 | double w = getEdgeWeight(e); |
| 254 | if (w == MissingValue) { |
| 255 | EdgeInformation[&F][e] = 0; |
| 256 | errs() << "Assuming edge weight: "; |
| 257 | printEdgeWeight(e); |
| 258 | } |
Daniel Dunbar | 55e354a | 2009-08-08 18:44:18 +0000 | [diff] [blame] | 259 | } |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | return false; |
| 264 | } |