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