1 //===- ProfileInfoLoad.cpp - Load profile information from disk -----------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // The ProfileInfoLoader class is used to load and represent profiling
11 // information read in from the dump file.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/Analysis/ProfileInfoLoader.h"
16 #include "llvm/Analysis/ProfileInfoTypes.h"
17 #include "llvm/Module.h"
18 #include "llvm/InstrTypes.h"
23 // ByteSwap - Byteswap 'Var' if 'Really' is true.
25 static inline unsigned ByteSwap(unsigned Var, bool Really) {
26 if (!Really) return Var;
27 return ((Var & (255<< 0)) << 24) |
28 ((Var & (255<< 8)) << 8) |
29 ((Var & (255<<16)) >> 8) |
30 ((Var & (255<<24)) >> 24);
33 static void ReadProfilingBlock(const char *ToolName, FILE *F,
35 std::vector<unsigned> &Data) {
36 // Read the number of entries...
38 if (fread(&NumEntries, sizeof(unsigned), 1, F) != 1) {
39 std::cerr << ToolName << ": data packet truncated!\n";
43 NumEntries = ByteSwap(NumEntries, ShouldByteSwap);
46 std::vector<unsigned> TempSpace(NumEntries);
48 // Read in the block of data...
49 if (fread(&TempSpace[0], sizeof(unsigned)*NumEntries, 1, F) != 1) {
50 std::cerr << ToolName << ": data packet truncated!\n";
55 // Make sure we have enough space...
56 if (Data.size() < NumEntries)
57 Data.resize(NumEntries);
59 // Accumulate the data we just read into the data.
60 if (!ShouldByteSwap) {
61 for (unsigned i = 0; i != NumEntries; ++i)
62 Data[i] += TempSpace[i];
64 for (unsigned i = 0; i != NumEntries; ++i)
65 Data[i] += ByteSwap(TempSpace[i], true);
69 // ProfileInfoLoader ctor - Read the specified profiling data file, exiting the
70 // program if the file is invalid or broken.
72 ProfileInfoLoader::ProfileInfoLoader(const char *ToolName,
73 const std::string &Filename,
74 Module &TheModule) : M(TheModule) {
75 FILE *F = fopen(Filename.c_str(), "r");
77 std::cerr << ToolName << ": Error opening '" << Filename << "': ";
82 // Keep reading packets until we run out of them.
84 while (fread(&PacketType, sizeof(unsigned), 1, F) == 1) {
85 // If the low eight bits of the packet are zero, we must be dealing with an
86 // endianness mismatch. Byteswap all words read from the profiling
88 bool ShouldByteSwap = (char)PacketType == 0;
89 PacketType = ByteSwap(PacketType, ShouldByteSwap);
94 if (fread(&ArgLength, sizeof(unsigned), 1, F) != 1) {
95 std::cerr << ToolName << ": arguments packet truncated!\n";
99 ArgLength = ByteSwap(ArgLength, ShouldByteSwap);
101 // Read in the arguments...
102 std::vector<char> Chars(ArgLength+4);
105 if (fread(&Chars[0], (ArgLength+3) & ~3, 1, F) != 1) {
106 std::cerr << ToolName << ": arguments packet truncated!\n";
110 CommandLines.push_back(std::string(&Chars[0], &Chars[ArgLength]));
115 ReadProfilingBlock(ToolName, F, ShouldByteSwap, FunctionCounts);
119 ReadProfilingBlock(ToolName, F, ShouldByteSwap, BlockCounts);
123 ReadProfilingBlock(ToolName, F, ShouldByteSwap, EdgeCounts);
127 ReadProfilingBlock(ToolName, F, ShouldByteSwap, BBTrace);
131 std::cerr << ToolName << ": Unknown packet type #" << PacketType << "!\n";
140 // getFunctionCounts - This method is used by consumers of function counting
141 // information. If we do not directly have function count information, we
142 // compute it from other, more refined, types of profile information.
144 void ProfileInfoLoader::getFunctionCounts(std::vector<std::pair<Function*,
145 unsigned> > &Counts) {
146 if (FunctionCounts.empty()) {
147 if (hasAccurateBlockCounts()) {
148 // Synthesize function frequency information from the number of times
149 // their entry blocks were executed.
150 std::vector<std::pair<BasicBlock*, unsigned> > BlockCounts;
151 getBlockCounts(BlockCounts);
153 for (unsigned i = 0, e = BlockCounts.size(); i != e; ++i)
154 if (&BlockCounts[i].first->getParent()->front() == BlockCounts[i].first)
155 Counts.push_back(std::make_pair(BlockCounts[i].first->getParent(),
156 BlockCounts[i].second));
158 std::cerr << "Function counts are not available!\n";
163 unsigned Counter = 0;
164 for (Module::iterator I = M.begin(), E = M.end();
165 I != E && Counter != FunctionCounts.size(); ++I)
166 if (!I->isExternal())
167 Counts.push_back(std::make_pair(I, FunctionCounts[Counter++]));
170 // getBlockCounts - This method is used by consumers of block counting
171 // information. If we do not directly have block count information, we
172 // compute it from other, more refined, types of profile information.
174 void ProfileInfoLoader::getBlockCounts(std::vector<std::pair<BasicBlock*,
175 unsigned> > &Counts) {
176 if (BlockCounts.empty()) {
177 if (hasAccurateEdgeCounts()) {
178 // Synthesize block count information from edge frequency information.
179 // The block execution frequency is equal to the sum of the execution
180 // frequency of all outgoing edges from a block.
182 // If a block has no successors, this will not be correct, so we have to
183 // special case it. :(
184 std::vector<std::pair<Edge, unsigned> > EdgeCounts;
185 getEdgeCounts(EdgeCounts);
187 std::map<BasicBlock*, unsigned> InEdgeFreqs;
189 BasicBlock *LastBlock = 0;
190 TerminatorInst *TI = 0;
191 for (unsigned i = 0, e = EdgeCounts.size(); i != e; ++i) {
192 if (EdgeCounts[i].first.first != LastBlock) {
193 LastBlock = EdgeCounts[i].first.first;
194 TI = LastBlock->getTerminator();
195 Counts.push_back(std::make_pair(LastBlock, 0));
197 Counts.back().second += EdgeCounts[i].second;
198 unsigned SuccNum = EdgeCounts[i].first.second;
199 if (SuccNum >= TI->getNumSuccessors()) {
200 static bool Warned = false;
202 std::cerr << "WARNING: profile info doesn't seem to match"
203 << " the program!\n";
207 // If this successor has no successors of its own, we will never
208 // compute an execution count for that block. Remember the incoming
209 // edge frequencies to add later.
210 BasicBlock *Succ = TI->getSuccessor(SuccNum);
211 if (Succ->getTerminator()->getNumSuccessors() == 0)
212 InEdgeFreqs[Succ] += EdgeCounts[i].second;
216 // Now we have to accumulate information for those blocks without
217 // successors into our table.
218 for (std::map<BasicBlock*, unsigned>::iterator I = InEdgeFreqs.begin(),
219 E = InEdgeFreqs.end(); I != E; ++I) {
221 for (; i != Counts.size() && Counts[i].first != I->first; ++i)
223 if (i == Counts.size()) Counts.push_back(std::make_pair(I->first, 0));
224 Counts[i].second += I->second;
228 std::cerr << "Block counts are not available!\n";
233 unsigned Counter = 0;
234 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F)
235 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
236 Counts.push_back(std::make_pair(BB, BlockCounts[Counter++]));
237 if (Counter == BlockCounts.size())
242 // getEdgeCounts - This method is used by consumers of edge counting
243 // information. If we do not directly have edge count information, we compute
244 // it from other, more refined, types of profile information.
246 void ProfileInfoLoader::getEdgeCounts(std::vector<std::pair<Edge,
247 unsigned> > &Counts) {
248 if (EdgeCounts.empty()) {
249 std::cerr << "Edge counts not available, and no synthesis "
250 << "is implemented yet!\n";
254 unsigned Counter = 0;
255 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F)
256 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
257 for (unsigned i = 0, e = BB->getTerminator()->getNumSuccessors();
259 Counts.push_back(std::make_pair(Edge(BB, i), EdgeCounts[Counter++]));
260 if (Counter == EdgeCounts.size())
265 // getBBTrace - This method is used by consumers of basic-block trace
268 void ProfileInfoLoader::getBBTrace(std::vector<BasicBlock *> &Trace) {
269 if (BBTrace.empty ()) {
270 std::cerr << "Basic block trace is not available!\n";
273 std::cerr << "Basic block trace loading is not implemented yet!\n";