Chris Lattner | bc44aa6 | 2004-02-11 05:54:25 +0000 | [diff] [blame] | 1 | //===- ProfileInfoLoad.cpp - Load profile information from disk -----------===// |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 2 | // |
Chris Lattner | bc44aa6 | 2004-02-11 05:54:25 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 7 | // |
Chris Lattner | bc44aa6 | 2004-02-11 05:54:25 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // The ProfileInfoLoader class is used to load and represent profiling |
| 11 | // information read in from the dump file. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "llvm/Analysis/ProfileInfoLoader.h" |
Brian Gaeke | 660ef70 | 2004-05-04 16:53:07 +0000 | [diff] [blame] | 16 | #include "llvm/Analysis/ProfileInfoTypes.h" |
Chris Lattner | bc44aa6 | 2004-02-11 05:54:25 +0000 | [diff] [blame] | 17 | #include "llvm/Module.h" |
Chris Lattner | 01945c1 | 2004-03-08 18:20:18 +0000 | [diff] [blame] | 18 | #include "llvm/InstrTypes.h" |
Bill Wendling | 6f81b51 | 2006-11-28 22:46:12 +0000 | [diff] [blame^] | 19 | #include "llvm/Support/Streams.h" |
Chris Lattner | bc44aa6 | 2004-02-11 05:54:25 +0000 | [diff] [blame] | 20 | #include <cstdio> |
Chris Lattner | dbbbfef | 2004-03-08 20:03:52 +0000 | [diff] [blame] | 21 | #include <map> |
Chris Lattner | bc44aa6 | 2004-02-11 05:54:25 +0000 | [diff] [blame] | 22 | using namespace llvm; |
| 23 | |
Chris Lattner | bc44aa6 | 2004-02-11 05:54:25 +0000 | [diff] [blame] | 24 | // ByteSwap - Byteswap 'Var' if 'Really' is true. |
| 25 | // |
| 26 | static inline unsigned ByteSwap(unsigned Var, bool Really) { |
| 27 | if (!Really) return Var; |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 28 | return ((Var & (255<< 0)) << 24) | |
| 29 | ((Var & (255<< 8)) << 8) | |
| 30 | ((Var & (255<<16)) >> 8) | |
Chris Lattner | bc44aa6 | 2004-02-11 05:54:25 +0000 | [diff] [blame] | 31 | ((Var & (255<<24)) >> 24); |
| 32 | } |
| 33 | |
| 34 | static void ReadProfilingBlock(const char *ToolName, FILE *F, |
| 35 | bool ShouldByteSwap, |
| 36 | std::vector<unsigned> &Data) { |
| 37 | // Read the number of entries... |
| 38 | unsigned NumEntries; |
| 39 | if (fread(&NumEntries, sizeof(unsigned), 1, F) != 1) { |
Bill Wendling | 6f81b51 | 2006-11-28 22:46:12 +0000 | [diff] [blame^] | 40 | llvm_cerr << ToolName << ": data packet truncated!\n"; |
Chris Lattner | bc44aa6 | 2004-02-11 05:54:25 +0000 | [diff] [blame] | 41 | perror(0); |
| 42 | exit(1); |
| 43 | } |
| 44 | NumEntries = ByteSwap(NumEntries, ShouldByteSwap); |
| 45 | |
| 46 | // Read the counts... |
| 47 | std::vector<unsigned> TempSpace(NumEntries); |
| 48 | |
| 49 | // Read in the block of data... |
| 50 | if (fread(&TempSpace[0], sizeof(unsigned)*NumEntries, 1, F) != 1) { |
Bill Wendling | 6f81b51 | 2006-11-28 22:46:12 +0000 | [diff] [blame^] | 51 | llvm_cerr << ToolName << ": data packet truncated!\n"; |
Chris Lattner | bc44aa6 | 2004-02-11 05:54:25 +0000 | [diff] [blame] | 52 | perror(0); |
| 53 | exit(1); |
| 54 | } |
| 55 | |
| 56 | // Make sure we have enough space... |
| 57 | if (Data.size() < NumEntries) |
| 58 | Data.resize(NumEntries); |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 59 | |
Chris Lattner | bc44aa6 | 2004-02-11 05:54:25 +0000 | [diff] [blame] | 60 | // Accumulate the data we just read into the data. |
| 61 | if (!ShouldByteSwap) { |
| 62 | for (unsigned i = 0; i != NumEntries; ++i) |
| 63 | Data[i] += TempSpace[i]; |
| 64 | } else { |
| 65 | for (unsigned i = 0; i != NumEntries; ++i) |
| 66 | Data[i] += ByteSwap(TempSpace[i], true); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | // ProfileInfoLoader ctor - Read the specified profiling data file, exiting the |
| 71 | // program if the file is invalid or broken. |
| 72 | // |
| 73 | ProfileInfoLoader::ProfileInfoLoader(const char *ToolName, |
| 74 | const std::string &Filename, |
| 75 | Module &TheModule) : M(TheModule) { |
| 76 | FILE *F = fopen(Filename.c_str(), "r"); |
| 77 | if (F == 0) { |
Bill Wendling | 6f81b51 | 2006-11-28 22:46:12 +0000 | [diff] [blame^] | 78 | llvm_cerr << ToolName << ": Error opening '" << Filename << "': "; |
Chris Lattner | bc44aa6 | 2004-02-11 05:54:25 +0000 | [diff] [blame] | 79 | perror(0); |
| 80 | exit(1); |
| 81 | } |
| 82 | |
| 83 | // Keep reading packets until we run out of them. |
| 84 | unsigned PacketType; |
| 85 | while (fread(&PacketType, sizeof(unsigned), 1, F) == 1) { |
| 86 | // If the low eight bits of the packet are zero, we must be dealing with an |
| 87 | // endianness mismatch. Byteswap all words read from the profiling |
| 88 | // information. |
| 89 | bool ShouldByteSwap = (char)PacketType == 0; |
| 90 | PacketType = ByteSwap(PacketType, ShouldByteSwap); |
| 91 | |
| 92 | switch (PacketType) { |
| 93 | case ArgumentInfo: { |
| 94 | unsigned ArgLength; |
| 95 | if (fread(&ArgLength, sizeof(unsigned), 1, F) != 1) { |
Bill Wendling | 6f81b51 | 2006-11-28 22:46:12 +0000 | [diff] [blame^] | 96 | llvm_cerr << ToolName << ": arguments packet truncated!\n"; |
Chris Lattner | bc44aa6 | 2004-02-11 05:54:25 +0000 | [diff] [blame] | 97 | perror(0); |
| 98 | exit(1); |
| 99 | } |
| 100 | ArgLength = ByteSwap(ArgLength, ShouldByteSwap); |
| 101 | |
| 102 | // Read in the arguments... |
| 103 | std::vector<char> Chars(ArgLength+4); |
| 104 | |
| 105 | if (ArgLength) |
| 106 | if (fread(&Chars[0], (ArgLength+3) & ~3, 1, F) != 1) { |
Bill Wendling | 6f81b51 | 2006-11-28 22:46:12 +0000 | [diff] [blame^] | 107 | llvm_cerr << ToolName << ": arguments packet truncated!\n"; |
Chris Lattner | bc44aa6 | 2004-02-11 05:54:25 +0000 | [diff] [blame] | 108 | perror(0); |
| 109 | exit(1); |
| 110 | } |
| 111 | CommandLines.push_back(std::string(&Chars[0], &Chars[ArgLength])); |
| 112 | break; |
| 113 | } |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 114 | |
Chris Lattner | bc44aa6 | 2004-02-11 05:54:25 +0000 | [diff] [blame] | 115 | case FunctionInfo: |
| 116 | ReadProfilingBlock(ToolName, F, ShouldByteSwap, FunctionCounts); |
| 117 | break; |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 118 | |
Chris Lattner | bc44aa6 | 2004-02-11 05:54:25 +0000 | [diff] [blame] | 119 | case BlockInfo: |
| 120 | ReadProfilingBlock(ToolName, F, ShouldByteSwap, BlockCounts); |
| 121 | break; |
| 122 | |
Chris Lattner | 01945c1 | 2004-03-08 18:20:18 +0000 | [diff] [blame] | 123 | case EdgeInfo: |
| 124 | ReadProfilingBlock(ToolName, F, ShouldByteSwap, EdgeCounts); |
| 125 | break; |
| 126 | |
Brian Gaeke | b171d79 | 2004-05-04 17:11:14 +0000 | [diff] [blame] | 127 | case BBTraceInfo: |
| 128 | ReadProfilingBlock(ToolName, F, ShouldByteSwap, BBTrace); |
| 129 | break; |
| 130 | |
Chris Lattner | bc44aa6 | 2004-02-11 05:54:25 +0000 | [diff] [blame] | 131 | default: |
Bill Wendling | 6f81b51 | 2006-11-28 22:46:12 +0000 | [diff] [blame^] | 132 | llvm_cerr << ToolName << ": Unknown packet type #" << PacketType << "!\n"; |
Chris Lattner | bc44aa6 | 2004-02-11 05:54:25 +0000 | [diff] [blame] | 133 | exit(1); |
| 134 | } |
| 135 | } |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 136 | |
Chris Lattner | bc44aa6 | 2004-02-11 05:54:25 +0000 | [diff] [blame] | 137 | fclose(F); |
| 138 | } |
| 139 | |
| 140 | |
| 141 | // getFunctionCounts - This method is used by consumers of function counting |
| 142 | // information. If we do not directly have function count information, we |
| 143 | // compute it from other, more refined, types of profile information. |
| 144 | // |
| 145 | void ProfileInfoLoader::getFunctionCounts(std::vector<std::pair<Function*, |
| 146 | unsigned> > &Counts) { |
| 147 | if (FunctionCounts.empty()) { |
Chris Lattner | dbbbfef | 2004-03-08 20:03:52 +0000 | [diff] [blame] | 148 | if (hasAccurateBlockCounts()) { |
| 149 | // Synthesize function frequency information from the number of times |
| 150 | // their entry blocks were executed. |
| 151 | std::vector<std::pair<BasicBlock*, unsigned> > BlockCounts; |
| 152 | getBlockCounts(BlockCounts); |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 153 | |
Chris Lattner | dbbbfef | 2004-03-08 20:03:52 +0000 | [diff] [blame] | 154 | for (unsigned i = 0, e = BlockCounts.size(); i != e; ++i) |
| 155 | if (&BlockCounts[i].first->getParent()->front() == BlockCounts[i].first) |
| 156 | Counts.push_back(std::make_pair(BlockCounts[i].first->getParent(), |
| 157 | BlockCounts[i].second)); |
| 158 | } else { |
Bill Wendling | 6f81b51 | 2006-11-28 22:46:12 +0000 | [diff] [blame^] | 159 | llvm_cerr << "Function counts are not available!\n"; |
Chris Lattner | dbbbfef | 2004-03-08 20:03:52 +0000 | [diff] [blame] | 160 | } |
Chris Lattner | bc44aa6 | 2004-02-11 05:54:25 +0000 | [diff] [blame] | 161 | return; |
| 162 | } |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 163 | |
Chris Lattner | bc44aa6 | 2004-02-11 05:54:25 +0000 | [diff] [blame] | 164 | unsigned Counter = 0; |
| 165 | for (Module::iterator I = M.begin(), E = M.end(); |
| 166 | I != E && Counter != FunctionCounts.size(); ++I) |
| 167 | if (!I->isExternal()) |
| 168 | Counts.push_back(std::make_pair(I, FunctionCounts[Counter++])); |
| 169 | } |
| 170 | |
| 171 | // getBlockCounts - This method is used by consumers of block counting |
| 172 | // information. If we do not directly have block count information, we |
| 173 | // compute it from other, more refined, types of profile information. |
| 174 | // |
| 175 | void ProfileInfoLoader::getBlockCounts(std::vector<std::pair<BasicBlock*, |
| 176 | unsigned> > &Counts) { |
| 177 | if (BlockCounts.empty()) { |
Chris Lattner | dbbbfef | 2004-03-08 20:03:52 +0000 | [diff] [blame] | 178 | if (hasAccurateEdgeCounts()) { |
| 179 | // Synthesize block count information from edge frequency information. |
| 180 | // The block execution frequency is equal to the sum of the execution |
| 181 | // frequency of all outgoing edges from a block. |
| 182 | // |
| 183 | // If a block has no successors, this will not be correct, so we have to |
| 184 | // special case it. :( |
| 185 | std::vector<std::pair<Edge, unsigned> > EdgeCounts; |
| 186 | getEdgeCounts(EdgeCounts); |
| 187 | |
| 188 | std::map<BasicBlock*, unsigned> InEdgeFreqs; |
| 189 | |
| 190 | BasicBlock *LastBlock = 0; |
| 191 | TerminatorInst *TI = 0; |
| 192 | for (unsigned i = 0, e = EdgeCounts.size(); i != e; ++i) { |
| 193 | if (EdgeCounts[i].first.first != LastBlock) { |
| 194 | LastBlock = EdgeCounts[i].first.first; |
| 195 | TI = LastBlock->getTerminator(); |
| 196 | Counts.push_back(std::make_pair(LastBlock, 0)); |
| 197 | } |
| 198 | Counts.back().second += EdgeCounts[i].second; |
| 199 | unsigned SuccNum = EdgeCounts[i].first.second; |
| 200 | if (SuccNum >= TI->getNumSuccessors()) { |
| 201 | static bool Warned = false; |
| 202 | if (!Warned) { |
Bill Wendling | 6f81b51 | 2006-11-28 22:46:12 +0000 | [diff] [blame^] | 203 | llvm_cerr << "WARNING: profile info doesn't seem to match" |
Chris Lattner | dbbbfef | 2004-03-08 20:03:52 +0000 | [diff] [blame] | 204 | << " the program!\n"; |
| 205 | Warned = true; |
| 206 | } |
| 207 | } else { |
| 208 | // If this successor has no successors of its own, we will never |
| 209 | // compute an execution count for that block. Remember the incoming |
| 210 | // edge frequencies to add later. |
| 211 | BasicBlock *Succ = TI->getSuccessor(SuccNum); |
| 212 | if (Succ->getTerminator()->getNumSuccessors() == 0) |
| 213 | InEdgeFreqs[Succ] += EdgeCounts[i].second; |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | // Now we have to accumulate information for those blocks without |
| 218 | // successors into our table. |
| 219 | for (std::map<BasicBlock*, unsigned>::iterator I = InEdgeFreqs.begin(), |
| 220 | E = InEdgeFreqs.end(); I != E; ++I) { |
| 221 | unsigned i = 0; |
| 222 | for (; i != Counts.size() && Counts[i].first != I->first; ++i) |
| 223 | /*empty*/; |
| 224 | if (i == Counts.size()) Counts.push_back(std::make_pair(I->first, 0)); |
| 225 | Counts[i].second += I->second; |
| 226 | } |
| 227 | |
| 228 | } else { |
Bill Wendling | 6f81b51 | 2006-11-28 22:46:12 +0000 | [diff] [blame^] | 229 | llvm_cerr << "Block counts are not available!\n"; |
Chris Lattner | dbbbfef | 2004-03-08 20:03:52 +0000 | [diff] [blame] | 230 | } |
Chris Lattner | bc44aa6 | 2004-02-11 05:54:25 +0000 | [diff] [blame] | 231 | return; |
| 232 | } |
| 233 | |
| 234 | unsigned Counter = 0; |
| 235 | for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) |
| 236 | for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) { |
| 237 | Counts.push_back(std::make_pair(BB, BlockCounts[Counter++])); |
| 238 | if (Counter == BlockCounts.size()) |
| 239 | return; |
| 240 | } |
| 241 | } |
Chris Lattner | 01945c1 | 2004-03-08 18:20:18 +0000 | [diff] [blame] | 242 | |
| 243 | // getEdgeCounts - This method is used by consumers of edge counting |
| 244 | // information. If we do not directly have edge count information, we compute |
| 245 | // it from other, more refined, types of profile information. |
| 246 | // |
| 247 | void ProfileInfoLoader::getEdgeCounts(std::vector<std::pair<Edge, |
| 248 | unsigned> > &Counts) { |
| 249 | if (EdgeCounts.empty()) { |
Bill Wendling | 6f81b51 | 2006-11-28 22:46:12 +0000 | [diff] [blame^] | 250 | llvm_cerr << "Edge counts not available, and no synthesis " |
Chris Lattner | 01945c1 | 2004-03-08 18:20:18 +0000 | [diff] [blame] | 251 | << "is implemented yet!\n"; |
| 252 | return; |
| 253 | } |
| 254 | |
| 255 | unsigned Counter = 0; |
| 256 | for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) |
| 257 | for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) |
| 258 | for (unsigned i = 0, e = BB->getTerminator()->getNumSuccessors(); |
| 259 | i != e; ++i) { |
| 260 | Counts.push_back(std::make_pair(Edge(BB, i), EdgeCounts[Counter++])); |
| 261 | if (Counter == EdgeCounts.size()) |
| 262 | return; |
| 263 | } |
| 264 | } |
Brian Gaeke | b171d79 | 2004-05-04 17:11:14 +0000 | [diff] [blame] | 265 | |
| 266 | // getBBTrace - This method is used by consumers of basic-block trace |
| 267 | // information. |
| 268 | // |
| 269 | void ProfileInfoLoader::getBBTrace(std::vector<BasicBlock *> &Trace) { |
| 270 | if (BBTrace.empty ()) { |
Bill Wendling | 6f81b51 | 2006-11-28 22:46:12 +0000 | [diff] [blame^] | 271 | llvm_cerr << "Basic block trace is not available!\n"; |
Brian Gaeke | b171d79 | 2004-05-04 17:11:14 +0000 | [diff] [blame] | 272 | return; |
| 273 | } |
Bill Wendling | 6f81b51 | 2006-11-28 22:46:12 +0000 | [diff] [blame^] | 274 | llvm_cerr << "Basic block trace loading is not implemented yet!\n"; |
Brian Gaeke | b171d79 | 2004-05-04 17:11:14 +0000 | [diff] [blame] | 275 | } |
| 276 | |