Chris Lattner | 171de65 | 2004-02-10 22:11:42 +0000 | [diff] [blame] | 1 | //===- ProfileInfo.cpp - Profile Info Interface ---------------------------===// |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 2 | // |
Chris Lattner | 171de65 | 2004-02-10 22:11:42 +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 | 171de65 | 2004-02-10 22:11:42 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the abstract ProfileInfo interface, and the default |
| 11 | // "no profile" implementation. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Jeff Cohen | 534927d | 2005-01-08 22:01:16 +0000 | [diff] [blame] | 15 | #include "llvm/Analysis/Passes.h" |
Chris Lattner | 171de65 | 2004-02-10 22:11:42 +0000 | [diff] [blame] | 16 | #include "llvm/Analysis/ProfileInfo.h" |
| 17 | #include "llvm/Pass.h" |
Chris Lattner | 96ab5ca | 2004-03-08 22:04:08 +0000 | [diff] [blame] | 18 | #include "llvm/Support/CFG.h" |
Reid Spencer | d7d83db | 2007-02-05 23:42:17 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Compiler.h" |
Andreas Neustifter | 07abe17 | 2009-09-09 17:52:57 +0000 | [diff] [blame^] | 20 | #include "llvm/Support/Debug.h" |
Dan Gohman | 4bac4b9 | 2009-08-26 15:56:38 +0000 | [diff] [blame] | 21 | #include "llvm/Support/raw_ostream.h" |
Andreas Neustifter | 07abe17 | 2009-09-09 17:52:57 +0000 | [diff] [blame^] | 22 | #include "llvm/Support/Format.h" |
Chris Lattner | 96ab5ca | 2004-03-08 22:04:08 +0000 | [diff] [blame] | 23 | #include <set> |
Chris Lattner | 171de65 | 2004-02-10 22:11:42 +0000 | [diff] [blame] | 24 | using namespace llvm; |
| 25 | |
Chris Lattner | ecefc96 | 2004-02-11 06:10:18 +0000 | [diff] [blame] | 26 | // Register the ProfileInfo interface, providing a nice name to refer to. |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 27 | static RegisterAnalysisGroup<ProfileInfo> Z("Profile Information"); |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 28 | char ProfileInfo::ID = 0; |
Chris Lattner | 171de65 | 2004-02-10 22:11:42 +0000 | [diff] [blame] | 29 | |
| 30 | ProfileInfo::~ProfileInfo() {} |
| 31 | |
Andreas Neustifter | 96135b6 | 2009-08-24 21:37:48 +0000 | [diff] [blame] | 32 | const double ProfileInfo::MissingValue = -1; |
| 33 | |
Daniel Dunbar | caaa493 | 2009-08-08 17:43:09 +0000 | [diff] [blame] | 34 | double ProfileInfo::getExecutionCount(const BasicBlock *BB) { |
| 35 | std::map<const Function*, BlockCounts>::iterator J = |
| 36 | BlockInformation.find(BB->getParent()); |
| 37 | if (J != BlockInformation.end()) { |
| 38 | BlockCounts::iterator I = J->second.find(BB); |
| 39 | if (I != J->second.end()) |
| 40 | return I->second; |
| 41 | } |
Daniel Dunbar | c9008c5 | 2009-08-05 21:51:16 +0000 | [diff] [blame] | 42 | |
Daniel Dunbar | 858cb8a | 2009-07-14 06:58:59 +0000 | [diff] [blame] | 43 | pred_const_iterator PI = pred_begin(BB), PE = pred_end(BB); |
Chris Lattner | 96ab5ca | 2004-03-08 22:04:08 +0000 | [diff] [blame] | 44 | |
| 45 | // Are there zero predecessors of this block? |
| 46 | if (PI == PE) { |
| 47 | // If this is the entry block, look for the Null -> Entry edge. |
Dan Gohman | ecb7a77 | 2007-03-22 16:38:57 +0000 | [diff] [blame] | 48 | if (BB == &BB->getParent()->getEntryBlock()) |
Daniel Dunbar | caaa493 | 2009-08-08 17:43:09 +0000 | [diff] [blame] | 49 | return getEdgeWeight(getEdge(0, BB)); |
Chris Lattner | 96ab5ca | 2004-03-08 22:04:08 +0000 | [diff] [blame] | 50 | else |
| 51 | return 0; // Otherwise, this is a dead block. |
| 52 | } |
| 53 | |
| 54 | // Otherwise, if there are predecessors, the execution count of this block is |
Daniel Dunbar | caaa493 | 2009-08-08 17:43:09 +0000 | [diff] [blame] | 55 | // the sum of the edge frequencies from the incoming edges. |
Daniel Dunbar | 858cb8a | 2009-07-14 06:58:59 +0000 | [diff] [blame] | 56 | std::set<const BasicBlock*> ProcessedPreds; |
Daniel Dunbar | caaa493 | 2009-08-08 17:43:09 +0000 | [diff] [blame] | 57 | double Count = 0; |
Chris Lattner | 96ab5ca | 2004-03-08 22:04:08 +0000 | [diff] [blame] | 58 | for (; PI != PE; ++PI) |
Daniel Dunbar | caaa493 | 2009-08-08 17:43:09 +0000 | [diff] [blame] | 59 | if (ProcessedPreds.insert(*PI).second) { |
| 60 | double w = getEdgeWeight(getEdge(*PI, BB)); |
| 61 | if (w == MissingValue) { |
Andreas Neustifter | 0a324aa | 2009-08-19 05:44:39 +0000 | [diff] [blame] | 62 | Count = MissingValue; |
| 63 | break; |
Daniel Dunbar | caaa493 | 2009-08-08 17:43:09 +0000 | [diff] [blame] | 64 | } |
| 65 | Count += w; |
| 66 | } |
| 67 | |
Andreas Neustifter | 96135b6 | 2009-08-24 21:37:48 +0000 | [diff] [blame] | 68 | if (Count != MissingValue) BlockInformation[BB->getParent()][BB] = Count; |
Chris Lattner | 96ab5ca | 2004-03-08 22:04:08 +0000 | [diff] [blame] | 69 | return Count; |
| 70 | } |
| 71 | |
Daniel Dunbar | caaa493 | 2009-08-08 17:43:09 +0000 | [diff] [blame] | 72 | double ProfileInfo::getExecutionCount(const Function *F) { |
| 73 | std::map<const Function*, double>::iterator J = |
| 74 | FunctionInformation.find(F); |
| 75 | if (J != FunctionInformation.end()) |
| 76 | return J->second; |
Daniel Dunbar | c9008c5 | 2009-08-05 21:51:16 +0000 | [diff] [blame] | 77 | |
Andreas Neustifter | 3772fb1 | 2009-08-26 13:33:09 +0000 | [diff] [blame] | 78 | // isDeclaration() is checked here and not at start of function to allow |
| 79 | // functions without a body still to have a execution count. |
| 80 | if (F->isDeclaration()) return MissingValue; |
| 81 | |
Daniel Dunbar | caaa493 | 2009-08-08 17:43:09 +0000 | [diff] [blame] | 82 | double Count = getExecutionCount(&F->getEntryBlock()); |
Andreas Neustifter | 96135b6 | 2009-08-24 21:37:48 +0000 | [diff] [blame] | 83 | if (Count != MissingValue) FunctionInformation[F] = Count; |
Daniel Dunbar | caaa493 | 2009-08-08 17:43:09 +0000 | [diff] [blame] | 84 | return Count; |
Daniel Dunbar | 858cb8a | 2009-07-14 06:58:59 +0000 | [diff] [blame] | 85 | } |
Chris Lattner | 96ab5ca | 2004-03-08 22:04:08 +0000 | [diff] [blame] | 86 | |
Andreas Neustifter | 07abe17 | 2009-09-09 17:52:57 +0000 | [diff] [blame^] | 87 | /// Replaces all occurences of RmBB in the ProfilingInfo with DestBB. |
| 88 | /// This checks all edges of the function the blocks reside in and replaces the |
| 89 | /// occurences of RmBB with DestBB. |
| 90 | void ProfileInfo::replaceAllUses(const BasicBlock *RmBB, |
| 91 | const BasicBlock *DestBB) { |
| 92 | DEBUG(errs() << "Replacing " << RmBB->getNameStr() |
| 93 | << " with " << DestBB->getNameStr() << "\n"); |
| 94 | const Function *F = DestBB->getParent(); |
| 95 | std::map<const Function*, EdgeWeights>::iterator J = |
| 96 | EdgeInformation.find(F); |
| 97 | if (J == EdgeInformation.end()) return; |
| 98 | |
| 99 | for (EdgeWeights::iterator I = J->second.begin(), E = J->second.end(); |
| 100 | I != E; ++I) { |
| 101 | Edge e = I->first; |
| 102 | Edge newedge; bool foundedge = false; |
| 103 | if (e.first == RmBB) { |
| 104 | newedge = getEdge(DestBB, e.second); |
| 105 | foundedge = true; |
| 106 | } |
| 107 | if (e.second == RmBB) { |
| 108 | newedge = getEdge(e.first, DestBB); |
| 109 | foundedge = true; |
| 110 | } |
| 111 | if (foundedge) { |
| 112 | double w = getEdgeWeight(e); |
| 113 | EdgeInformation[F][newedge] = w; |
| 114 | DEBUG(errs() << "Replacing " << e << " with " << newedge << "\n"); |
| 115 | J->second.erase(e); |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | /// Splits an edge in the ProfileInfo and redirects flow over NewBB. |
| 121 | /// Since its possible that there is more than one edge in the CFG from FristBB |
| 122 | /// to SecondBB its necessary to redirect the flow proporionally. |
| 123 | void ProfileInfo::splitEdge(const BasicBlock *FirstBB, |
| 124 | const BasicBlock *SecondBB, |
| 125 | const BasicBlock *NewBB, |
| 126 | bool MergeIdenticalEdges) { |
| 127 | const Function *F = FirstBB->getParent(); |
| 128 | std::map<const Function*, EdgeWeights>::iterator J = |
| 129 | EdgeInformation.find(F); |
| 130 | if (J == EdgeInformation.end()) return; |
| 131 | |
| 132 | // Generate edges and read current weight. |
| 133 | Edge e = getEdge(FirstBB, SecondBB); |
| 134 | Edge n1 = getEdge(FirstBB, NewBB); |
| 135 | Edge n2 = getEdge(NewBB, SecondBB); |
| 136 | EdgeWeights &ECs = J->second; |
| 137 | double w = ECs[e]; |
| 138 | |
| 139 | int succ_count = 0; |
| 140 | if (!MergeIdenticalEdges) { |
| 141 | // First count the edges from FristBB to SecondBB, if there is more than |
| 142 | // one, only slice out a proporional part for NewBB. |
| 143 | for(succ_const_iterator BBI = succ_begin(FirstBB), BBE = succ_end(FirstBB); |
| 144 | BBI != BBE; ++BBI) { |
| 145 | if (*BBI == SecondBB) succ_count++; |
| 146 | } |
| 147 | // When the NewBB is completely new, increment the count by one so that |
| 148 | // the counts are properly distributed. |
| 149 | if (getExecutionCount(NewBB) == ProfileInfo::MissingValue) succ_count++; |
| 150 | } else { |
| 151 | // When the edges are merged anyway, then redirect all flow. |
| 152 | succ_count = 1; |
| 153 | } |
| 154 | |
| 155 | // We know now how many edges there are from FirstBB to SecondBB, reroute a |
| 156 | // proportional part of the edge weight over NewBB. |
| 157 | double neww = w / succ_count; |
| 158 | ECs[n1] += neww; |
| 159 | ECs[n2] += neww; |
| 160 | BlockInformation[F][NewBB] += neww; |
| 161 | if (succ_count == 1) { |
| 162 | ECs.erase(e); |
| 163 | } else { |
| 164 | ECs[e] -= neww; |
| 165 | } |
| 166 | } |
| 167 | |
Dan Gohman | 4bac4b9 | 2009-08-26 15:56:38 +0000 | [diff] [blame] | 168 | raw_ostream& llvm::operator<<(raw_ostream &O, ProfileInfo::Edge E) { |
| 169 | O << "("; |
| 170 | O << (E.first ? E.first->getNameStr() : "0"); |
| 171 | O << ","; |
| 172 | O << (E.second ? E.second->getNameStr() : "0"); |
| 173 | return O << ")"; |
| 174 | } |
Chris Lattner | 171de65 | 2004-02-10 22:11:42 +0000 | [diff] [blame] | 175 | |
| 176 | //===----------------------------------------------------------------------===// |
| 177 | // NoProfile ProfileInfo implementation |
| 178 | // |
| 179 | |
| 180 | namespace { |
Reid Spencer | d7d83db | 2007-02-05 23:42:17 +0000 | [diff] [blame] | 181 | struct VISIBILITY_HIDDEN NoProfileInfo |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 182 | : public ImmutablePass, public ProfileInfo { |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 183 | static char ID; // Class identification, replacement for typeinfo |
Dan Gohman | ae73dc1 | 2008-09-04 17:05:41 +0000 | [diff] [blame] | 184 | NoProfileInfo() : ImmutablePass(&ID) {} |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 185 | }; |
Chris Lattner | 171de65 | 2004-02-10 22:11:42 +0000 | [diff] [blame] | 186 | } // End of anonymous namespace |
Jeff Cohen | 534927d | 2005-01-08 22:01:16 +0000 | [diff] [blame] | 187 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 188 | char NoProfileInfo::ID = 0; |
| 189 | // Register this pass... |
| 190 | static RegisterPass<NoProfileInfo> |
| 191 | X("no-profile", "No Profile Information", false, true); |
| 192 | |
| 193 | // Declare that we implement the ProfileInfo interface |
| 194 | static RegisterAnalysisGroup<ProfileInfo, true> Y(X); |
| 195 | |
Jeff Cohen | 534927d | 2005-01-08 22:01:16 +0000 | [diff] [blame] | 196 | ImmutablePass *llvm::createNoProfileInfoPass() { return new NoProfileInfo(); } |