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