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; |
Andreas Neustifter | 8a58c18 | 2009-09-11 08:39:33 +0000 | [diff] [blame] | 37 | std::set<BasicBlock*> BBToVisit; |
Daniel Dunbar | 55e354a | 2009-08-08 18:44:18 +0000 | [diff] [blame] | 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 | 8a58c18 | 2009-09-11 08:39:33 +0000 | [diff] [blame] | 58 | virtual void 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 | 8a58c18 | 2009-09-11 08:39:33 +0000 | [diff] [blame] | 89 | static void inline printEdgeError(ProfileInfo::Edge e, const char *M) { |
| 90 | DEBUG(errs() << "-- Edge " << e << " is not calculated, " << M << "\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 | 8a58c18 | 2009-09-11 08:39:33 +0000 | [diff] [blame] | 100 | // The algorithm preserves the flow condition, meaning that the sum of the |
| 101 | // weight of the incoming edges must be equal the block weight which must in |
| 102 | // turn be equal to the sume of the weights of the outgoing edges. |
| 103 | // Since the flow of an block is deterimined from the current state of the |
| 104 | // flow, once an edge has a flow assigned this flow is never changed again, |
| 105 | // otherwise it would be possible to violate the flow condition in another |
| 106 | // block. |
| 107 | void ProfileEstimatorPass::recurseBasicBlock(BasicBlock *BB) { |
Daniel Dunbar | 55e354a | 2009-08-08 18:44:18 +0000 | [diff] [blame] | 108 | |
Andreas Neustifter | ff271e1 | 2009-08-26 15:13:44 +0000 | [diff] [blame] | 109 | // Break the recursion if this BasicBlock was already visited. |
Andreas Neustifter | 8a58c18 | 2009-09-11 08:39:33 +0000 | [diff] [blame] | 110 | if (BBToVisit.find(BB) == BBToVisit.end()) return; |
Daniel Dunbar | 55e354a | 2009-08-08 18:44:18 +0000 | [diff] [blame] | 111 | |
Andreas Neustifter | 8a58c18 | 2009-09-11 08:39:33 +0000 | [diff] [blame] | 112 | // Read the LoopInfo for this block. |
Daniel Dunbar | 55e354a | 2009-08-08 18:44:18 +0000 | [diff] [blame] | 113 | bool BBisHeader = LI->isLoopHeader(BB); |
| 114 | Loop* BBLoop = LI->getLoopFor(BB); |
| 115 | |
Andreas Neustifter | 8a58c18 | 2009-09-11 08:39:33 +0000 | [diff] [blame] | 116 | // To get the block weight, read all incoming edges. |
Daniel Dunbar | 55e354a | 2009-08-08 18:44:18 +0000 | [diff] [blame] | 117 | double BBWeight = 0; |
| 118 | std::set<BasicBlock*> ProcessedPreds; |
| 119 | for ( pred_iterator bbi = pred_begin(BB), bbe = pred_end(BB); |
| 120 | bbi != bbe; ++bbi ) { |
Andreas Neustifter | 8a58c18 | 2009-09-11 08:39:33 +0000 | [diff] [blame] | 121 | // If this block was not considered already, add weight. |
Andreas Neustifter | 0c0de66 | 2009-09-10 16:30:38 +0000 | [diff] [blame] | 122 | Edge edge = getEdge(*bbi,BB); |
| 123 | double w = getEdgeWeight(edge); |
Daniel Dunbar | 55e354a | 2009-08-08 18:44:18 +0000 | [diff] [blame] | 124 | if (ProcessedPreds.insert(*bbi).second) { |
Andreas Neustifter | 0c0de66 | 2009-09-10 16:30:38 +0000 | [diff] [blame] | 125 | BBWeight += ignoreMissing(w); |
Daniel Dunbar | 55e354a | 2009-08-08 18:44:18 +0000 | [diff] [blame] | 126 | } |
Andreas Neustifter | 8a58c18 | 2009-09-11 08:39:33 +0000 | [diff] [blame] | 127 | // If this block is a loop header and the predecessor is contained in this |
| 128 | // loop, thus the edge is a backedge, continue and do not check if the |
| 129 | // value is valid. |
Andreas Neustifter | 0c0de66 | 2009-09-10 16:30:38 +0000 | [diff] [blame] | 130 | if (BBisHeader && BBLoop->contains(*bbi)) { |
Andreas Neustifter | 8a58c18 | 2009-09-11 08:39:33 +0000 | [diff] [blame] | 131 | printEdgeError(edge, "but is backedge, continueing"); |
Daniel Dunbar | 55e354a | 2009-08-08 18:44:18 +0000 | [diff] [blame] | 132 | continue; |
| 133 | } |
Andreas Neustifter | 8a58c18 | 2009-09-11 08:39:33 +0000 | [diff] [blame] | 134 | // If the edges value is missing (and this is no loop header, and this is |
| 135 | // no backedge) return, this block is currently non estimatable. |
Andreas Neustifter | 0c0de66 | 2009-09-10 16:30:38 +0000 | [diff] [blame] | 136 | if (w == MissingValue) { |
Andreas Neustifter | 8a58c18 | 2009-09-11 08:39:33 +0000 | [diff] [blame] | 137 | printEdgeError(edge, "returning"); |
| 138 | return; |
Daniel Dunbar | 55e354a | 2009-08-08 18:44:18 +0000 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | if (getExecutionCount(BB) != MissingValue) { |
| 142 | BBWeight = getExecutionCount(BB); |
| 143 | } |
| 144 | |
Andreas Neustifter | ff271e1 | 2009-08-26 15:13:44 +0000 | [diff] [blame] | 145 | // Fetch all necessary information for current block. |
Daniel Dunbar | 55e354a | 2009-08-08 18:44:18 +0000 | [diff] [blame] | 146 | SmallVector<Edge, 8> ExitEdges; |
| 147 | SmallVector<Edge, 8> Edges; |
| 148 | if (BBLoop) { |
| 149 | BBLoop->getExitEdges(ExitEdges); |
| 150 | } |
| 151 | |
Andreas Neustifter | 8a58c18 | 2009-09-11 08:39:33 +0000 | [diff] [blame] | 152 | // If this is a loop header, consider the following: |
| 153 | // Exactly the flow that is entering this block, must exit this block too. So |
| 154 | // do the following: |
| 155 | // *) get all the exit edges, read the flow that is already leaving this |
| 156 | // loop, remember the edges that do not have any flow on them right now. |
| 157 | // (The edges that have already flow on them are most likely exiting edges of |
| 158 | // other loops, do not touch those flows because the previously caclulated |
| 159 | // loopheaders would not be exact anymore.) |
| 160 | // *) In case there is not a single exiting edge left, create one at the loop |
| 161 | // latch to prevent the flow from building up in the loop. |
| 162 | // *) Take the flow that is not leaving the loop already and distribute it on |
| 163 | // the remaining exiting edges. |
| 164 | // (This ensures that all flow that enters the loop also leaves it.) |
| 165 | // *) Increase the flow into the loop by increasing the weight of this block. |
| 166 | // There is at least one incoming backedge that will bring us this flow later |
| 167 | // on. (So that the flow condition in this node is valid again.) |
Daniel Dunbar | 55e354a | 2009-08-08 18:44:18 +0000 | [diff] [blame] | 168 | if (BBisHeader) { |
| 169 | double incoming = BBWeight; |
Andreas Neustifter | ff271e1 | 2009-08-26 15:13:44 +0000 | [diff] [blame] | 170 | // Subtract the flow leaving the loop. |
Andreas Neustifter | 0c0de66 | 2009-09-10 16:30:38 +0000 | [diff] [blame] | 171 | std::set<Edge> ProcessedExits; |
Daniel Dunbar | 55e354a | 2009-08-08 18:44:18 +0000 | [diff] [blame] | 172 | for (SmallVector<Edge, 8>::iterator ei = ExitEdges.begin(), |
| 173 | ee = ExitEdges.end(); ei != ee; ++ei) { |
Andreas Neustifter | 0c0de66 | 2009-09-10 16:30:38 +0000 | [diff] [blame] | 174 | if (ProcessedExits.insert(*ei).second) { |
| 175 | double w = getEdgeWeight(*ei); |
| 176 | if (w == MissingValue) { |
| 177 | Edges.push_back(*ei); |
| 178 | } else { |
| 179 | incoming -= w; |
| 180 | } |
Daniel Dunbar | 55e354a | 2009-08-08 18:44:18 +0000 | [diff] [blame] | 181 | } |
| 182 | } |
Andreas Neustifter | 0c0de66 | 2009-09-10 16:30:38 +0000 | [diff] [blame] | 183 | // If no exit edges, create one: |
| 184 | if (Edges.size() == 0) { |
| 185 | BasicBlock *Latch = BBLoop->getLoopLatch(); |
| 186 | if (Latch) { |
| 187 | Edge edge = getEdge(Latch,0); |
| 188 | EdgeInformation[BB->getParent()][edge] = BBWeight; |
| 189 | printEdgeWeight(edge); |
| 190 | edge = getEdge(Latch, BB); |
| 191 | EdgeInformation[BB->getParent()][edge] = BBWeight * ExecCount; |
| 192 | printEdgeWeight(edge); |
| 193 | } |
| 194 | } |
Andreas Neustifter | ff271e1 | 2009-08-26 15:13:44 +0000 | [diff] [blame] | 195 | // Distribute remaining weight onto the exit edges. |
Daniel Dunbar | 55e354a | 2009-08-08 18:44:18 +0000 | [diff] [blame] | 196 | for (SmallVector<Edge, 8>::iterator ei = Edges.begin(), ee = Edges.end(); |
| 197 | ei != ee; ++ei) { |
| 198 | EdgeInformation[BB->getParent()][*ei] += incoming/Edges.size(); |
Andreas Neustifter | ff271e1 | 2009-08-26 15:13:44 +0000 | [diff] [blame] | 199 | printEdgeWeight(*ei); |
Daniel Dunbar | 55e354a | 2009-08-08 18:44:18 +0000 | [diff] [blame] | 200 | } |
Andreas Neustifter | ff271e1 | 2009-08-26 15:13:44 +0000 | [diff] [blame] | 201 | // Increase flow into the loop. |
Daniel Dunbar | 55e354a | 2009-08-08 18:44:18 +0000 | [diff] [blame] | 202 | BBWeight *= (ExecCount+1); |
| 203 | } |
| 204 | |
Andreas Neustifter | 8a58c18 | 2009-09-11 08:39:33 +0000 | [diff] [blame] | 205 | BlockInformation[BB->getParent()][BB] = BBWeight; |
| 206 | // Up until now we considered only the loop exiting edges, now we have a |
| 207 | // definite block weight and must ditribute this onto the outgoing edges. |
| 208 | // Since there may be already flow attached to some of the edges, read this |
| 209 | // flow first and remember the edges that have still now flow attached. |
Daniel Dunbar | 55e354a | 2009-08-08 18:44:18 +0000 | [diff] [blame] | 210 | Edges.clear(); |
| 211 | std::set<BasicBlock*> ProcessedSuccs; |
Andreas Neustifter | ff271e1 | 2009-08-26 15:13:44 +0000 | [diff] [blame] | 212 | |
Andreas Neustifter | e885af9 | 2009-09-01 10:06:05 +0000 | [diff] [blame] | 213 | succ_iterator bbi = succ_begin(BB), bbe = succ_end(BB); |
Andreas Neustifter | 8a58c18 | 2009-09-11 08:39:33 +0000 | [diff] [blame] | 214 | // Also check for (BB,0) edges that may already contain some flow. (But only |
| 215 | // in case there are no successors.) |
Andreas Neustifter | e885af9 | 2009-09-01 10:06:05 +0000 | [diff] [blame] | 216 | if (bbi == bbe) { |
| 217 | Edge edge = getEdge(BB,0); |
| 218 | EdgeInformation[BB->getParent()][edge] = BBWeight; |
| 219 | printEdgeWeight(edge); |
| 220 | } |
Andreas Neustifter | 19531d1 | 2009-09-01 19:01:59 +0000 | [diff] [blame] | 221 | for ( ; bbi != bbe; ++bbi ) { |
Daniel Dunbar | 55e354a | 2009-08-08 18:44:18 +0000 | [diff] [blame] | 222 | if (ProcessedSuccs.insert(*bbi).second) { |
| 223 | Edge edge = getEdge(BB,*bbi); |
| 224 | double w = getEdgeWeight(edge); |
| 225 | if (w != MissingValue) { |
| 226 | BBWeight -= getEdgeWeight(edge); |
| 227 | } else { |
| 228 | Edges.push_back(edge); |
| 229 | } |
| 230 | } |
| 231 | } |
| 232 | |
Andreas Neustifter | 8a58c18 | 2009-09-11 08:39:33 +0000 | [diff] [blame] | 233 | // Finally we know what flow is still not leaving the block, distribute this |
| 234 | // flow onto the empty edges. |
Daniel Dunbar | 55e354a | 2009-08-08 18:44:18 +0000 | [diff] [blame] | 235 | for (SmallVector<Edge, 8>::iterator ei = Edges.begin(), ee = Edges.end(); |
| 236 | ei != ee; ++ei) { |
| 237 | EdgeInformation[BB->getParent()][*ei] += BBWeight/Edges.size(); |
Andreas Neustifter | ff271e1 | 2009-08-26 15:13:44 +0000 | [diff] [blame] | 238 | printEdgeWeight(*ei); |
Daniel Dunbar | 55e354a | 2009-08-08 18:44:18 +0000 | [diff] [blame] | 239 | } |
| 240 | |
Andreas Neustifter | 8a58c18 | 2009-09-11 08:39:33 +0000 | [diff] [blame] | 241 | // This block is visited, mark this before the recursion. |
| 242 | BBToVisit.erase(BB); |
| 243 | |
| 244 | // Recurse into successors. |
| 245 | for (succ_iterator bbi = succ_begin(BB), bbe = succ_end(BB); |
| 246 | bbi != bbe; ++bbi) { |
| 247 | recurseBasicBlock(*bbi); |
Daniel Dunbar | 55e354a | 2009-08-08 18:44:18 +0000 | [diff] [blame] | 248 | } |
| 249 | } |
| 250 | |
| 251 | bool ProfileEstimatorPass::runOnFunction(Function &F) { |
| 252 | if (F.isDeclaration()) return false; |
| 253 | |
Andreas Neustifter | 8a58c18 | 2009-09-11 08:39:33 +0000 | [diff] [blame] | 254 | // Fetch LoopInfo and clear ProfileInfo for this function. |
Daniel Dunbar | 55e354a | 2009-08-08 18:44:18 +0000 | [diff] [blame] | 255 | LI = &getAnalysis<LoopInfo>(); |
| 256 | FunctionInformation.erase(&F); |
| 257 | BlockInformation[&F].clear(); |
| 258 | EdgeInformation[&F].clear(); |
Andreas Neustifter | 8a58c18 | 2009-09-11 08:39:33 +0000 | [diff] [blame] | 259 | |
| 260 | // Mark all blocks as to visit. |
| 261 | for (Function::iterator bi = F.begin(), be = F.end(); bi != be; ++bi) |
| 262 | BBToVisit.insert(bi); |
Daniel Dunbar | 55e354a | 2009-08-08 18:44:18 +0000 | [diff] [blame] | 263 | |
Andreas Neustifter | ff271e1 | 2009-08-26 15:13:44 +0000 | [diff] [blame] | 264 | DEBUG(errs() << "Working on function " << F.getNameStr() << "\n"); |
Daniel Dunbar | 55e354a | 2009-08-08 18:44:18 +0000 | [diff] [blame] | 265 | |
Andreas Neustifter | ff271e1 | 2009-08-26 15:13:44 +0000 | [diff] [blame] | 266 | // Since the entry block is the first one and has no predecessors, the edge |
| 267 | // (0,entry) is inserted with the starting weight of 1. |
Daniel Dunbar | 55e354a | 2009-08-08 18:44:18 +0000 | [diff] [blame] | 268 | BasicBlock *entry = &F.getEntryBlock(); |
| 269 | BlockInformation[&F][entry] = 1; |
Daniel Dunbar | 55e354a | 2009-08-08 18:44:18 +0000 | [diff] [blame] | 270 | Edge edge = getEdge(0,entry); |
Andreas Neustifter | 8a58c18 | 2009-09-11 08:39:33 +0000 | [diff] [blame] | 271 | EdgeInformation[&F][edge] = 1; |
| 272 | printEdgeWeight(edge); |
| 273 | |
| 274 | // Since recurseBasicBlock() maybe returns with a block which was not fully |
| 275 | // estimated, use recurseBasicBlock() until everything is calculated. |
| 276 | recurseBasicBlock(entry); |
| 277 | while (BBToVisit.size() > 0) { |
| 278 | // Remember number of open blocks, this is later used to check if progress |
| 279 | // was made. |
| 280 | unsigned size = BBToVisit.size(); |
| 281 | |
| 282 | // Try to calculate all blocks in turn. |
| 283 | for (std::set<BasicBlock*>::iterator bi = BBToVisit.begin(), |
| 284 | be = BBToVisit.end(); bi != be; ++bi) { |
| 285 | recurseBasicBlock(*bi); |
| 286 | // If at least one block was finished, break because iterator may be |
| 287 | // invalid. |
| 288 | if (BBToVisit.size() < size) break; |
| 289 | } |
| 290 | |
| 291 | // If there was not a single block resovled, make some assumptions. |
| 292 | if (BBToVisit.size() == size) { |
| 293 | BasicBlock *BB = *(BBToVisit.begin()); |
| 294 | // Since this BB was not calculated because of missing incoming edges, |
| 295 | // set these edges to zero. |
Daniel Dunbar | 55e354a | 2009-08-08 18:44:18 +0000 | [diff] [blame] | 296 | for (pred_iterator bbi = pred_begin(BB), bbe = pred_end(BB); |
| 297 | bbi != bbe; ++bbi) { |
| 298 | Edge e = getEdge(*bbi,BB); |
Andreas Neustifter | 0c0de66 | 2009-09-10 16:30:38 +0000 | [diff] [blame] | 299 | double w = getEdgeWeight(e); |
| 300 | if (w == MissingValue) { |
| 301 | EdgeInformation[&F][e] = 0; |
Andreas Neustifter | 8a58c18 | 2009-09-11 08:39:33 +0000 | [diff] [blame] | 302 | DEBUG(errs() << "Assuming edge weight: "); |
Andreas Neustifter | 0c0de66 | 2009-09-10 16:30:38 +0000 | [diff] [blame] | 303 | printEdgeWeight(e); |
| 304 | } |
Daniel Dunbar | 55e354a | 2009-08-08 18:44:18 +0000 | [diff] [blame] | 305 | } |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | return false; |
| 310 | } |