8786b303881763dfa0b6ff6411ce92d58648f07f
[oota-llvm.git] / tools / llvm-prof / ProfileInfo.cpp
1 //===- ProfileInfo.cpp - Represents profile information -------------------===//
2 // 
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.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 // The ProfileInfo class is used to represent profiling information read in from
11 // the dump file.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "ProfileInfo.h"
16 #include "llvm/Module.h"
17 #include <cstdio>
18 using namespace llvm;
19
20 enum ProfilingType {
21   ArgumentInfo = 1,   // The command line argument block
22   FunctionInfo = 2,   // Function profiling information
23   BlockInfo    = 3,   // Block profiling information
24 };
25
26 // ByteSwap - Byteswap 'Var' if 'Really' is true.
27 //
28 static inline unsigned ByteSwap(unsigned Var, bool Really) {
29   if (!Really) return Var;
30   return ((Var & (255<< 0)) << 24) | 
31          ((Var & (255<< 8)) <<  8) | 
32          ((Var & (255<<16)) >>  8) | 
33          ((Var & (255<<24)) >> 24);
34 }
35
36 static void ReadProfilingBlock(const char *ToolName, FILE *F,
37                                bool ShouldByteSwap,
38                                std::vector<unsigned> &Data) {
39   // Read the number of entries...
40   unsigned NumEntries;
41   if (fread(&NumEntries, sizeof(unsigned), 1, F) != 1) {
42     std::cerr << ToolName << ": data packet truncated!\n";
43     perror(0);
44     exit(1);
45   }
46   NumEntries = ByteSwap(NumEntries, ShouldByteSwap);
47
48   // Read the counts...
49   std::vector<unsigned> TempSpace(NumEntries);
50
51   // Read in the block of data...
52   if (fread(&TempSpace[0], sizeof(unsigned)*NumEntries, 1, F) != 1) {
53     std::cerr << ToolName << ": data packet truncated!\n";
54     perror(0);
55     exit(1);
56   }
57
58   // Make sure we have enough space...
59   if (Data.size() < NumEntries)
60     Data.resize(NumEntries);
61   
62   // Accumulate the data we just read into the data.
63   if (!ShouldByteSwap) {
64     for (unsigned i = 0; i != NumEntries; ++i)
65       Data[i] += TempSpace[i];
66   } else {
67     for (unsigned i = 0; i != NumEntries; ++i)
68       Data[i] += ByteSwap(TempSpace[i], true);
69   }
70 }
71
72 // ProfileInfo ctor - Read the specified profiling data file, exiting the
73 // program if the file is invalid or broken.
74 //
75 ProfileInfo::ProfileInfo(const char *ToolName, const std::string &Filename,
76                          Module &TheModule) : M(TheModule) {
77   FILE *F = fopen(Filename.c_str(), "r");
78   if (F == 0) {
79     std::cerr << ToolName << ": Error opening '" << Filename << ": ";
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     }
115       
116     case FunctionInfo:
117       ReadProfilingBlock(ToolName, F, ShouldByteSwap, FunctionCounts);
118       break;
119       
120     case BlockInfo:
121       ReadProfilingBlock(ToolName, F, ShouldByteSwap, BlockCounts);
122       break;
123
124     default:
125       std::cerr << ToolName << ": Unknown packet type #" << PacketType << "!\n";
126       exit(1);
127     }
128   }
129   
130   fclose(F);
131 }
132
133
134 // getFunctionCounts - This method is used by consumers of function counting
135 // information.  If we do not directly have function count information, we
136 // compute it from other, more refined, types of profile information.
137 //
138 void ProfileInfo::getFunctionCounts(std::vector<std::pair<Function*,
139                                                          unsigned> > &Counts) {
140   if (FunctionCounts.empty()) {
141     // Synthesize function frequency information from the number of times their
142     // entry blocks were executed.
143     std::vector<std::pair<BasicBlock*, unsigned> > BlockCounts;
144     getBlockCounts(BlockCounts);
145
146     for (unsigned i = 0, e = BlockCounts.size(); i != e; ++i)
147       if (&BlockCounts[i].first->getParent()->front() == BlockCounts[i].first)
148         Counts.push_back(std::make_pair(BlockCounts[i].first->getParent(),
149                                         BlockCounts[i].second));
150     return;
151   }
152   
153   unsigned Counter = 0;
154   for (Module::iterator I = M.begin(), E = M.end();
155        I != E && Counter != FunctionCounts.size(); ++I)
156     if (!I->isExternal())
157       Counts.push_back(std::make_pair(I, FunctionCounts[Counter++]));
158 }
159
160 // getBlockCounts - This method is used by consumers of block counting
161 // information.  If we do not directly have block count information, we
162 // compute it from other, more refined, types of profile information.
163 //
164 void ProfileInfo::getBlockCounts(std::vector<std::pair<BasicBlock*,
165                                                        unsigned> > &Counts) {
166   if (BlockCounts.empty()) {
167     std::cerr << "Block counts not available, and no synthesis "
168               << "is implemented yet!\n";
169     return;
170   }
171
172   unsigned Counter = 0;
173   for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F)
174     for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
175       Counts.push_back(std::make_pair(BB, BlockCounts[Counter++]));
176       if (Counter == BlockCounts.size())
177         return;
178     }
179 }