Update file header
[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 <iostream>
18 #include <sys/types.h>
19 #include <sys/stat.h>
20 #include <fcntl.h>
21 #include <stdio.h>
22
23 using namespace llvm;
24
25 enum ProfilingType {
26   ArgumentInfo = 1,   // The command line argument block
27   FunctionInfo = 2,   // Function profiling information
28   BlockInfo    = 3,   // Block profiling information
29 };
30
31 // ByteSwap - Byteswap 'Var' if 'Really' is true.
32 //
33 static inline unsigned ByteSwap(unsigned Var, bool Really) {
34   if (!Really) return Var;
35   return ((Var & (255<< 0)) << 24) | 
36          ((Var & (255<< 8)) <<  8) | 
37          ((Var & (255<<16)) >>  8) | 
38          ((Var & (255<<24)) >> 24);
39 }
40
41 static void ReadProfilingBlock(const char *ToolName, FILE *F,
42                                bool ShouldByteSwap,
43                                std::vector<unsigned> &Data) {
44   // Read the number of entries...
45   unsigned NumEntries;
46   if (fread(&NumEntries, sizeof(unsigned), 1, F) != 1) {
47     std::cerr << ToolName << ": data packet truncated!\n";
48     perror(0);
49     exit(1);
50   }
51   NumEntries = ByteSwap(NumEntries, ShouldByteSwap);
52
53   // Read the counts...
54   std::vector<unsigned> TempSpace(NumEntries);
55
56   // Read in the block of data...
57   if (fread(&TempSpace[0], sizeof(unsigned)*NumEntries, 1, F) != 1) {
58     std::cerr << ToolName << ": data packet truncated!\n";
59     perror(0);
60     exit(1);
61   }
62
63   // Make sure we have enough space...
64   if (Data.size() < NumEntries)
65     Data.resize(NumEntries);
66   
67   // Accumulate the data we just read into the data.
68   if (!ShouldByteSwap) {
69     for (unsigned i = 0; i != NumEntries; ++i)
70       Data[i] += TempSpace[i];
71   } else {
72     for (unsigned i = 0; i != NumEntries; ++i)
73       Data[i] += ByteSwap(TempSpace[i], true);
74   }
75 }
76
77 // ProfileInfo ctor - Read the specified profiling data file, exiting the
78 // program if the file is invalid or broken.
79 //
80 ProfileInfo::ProfileInfo(const char *ToolName, const std::string &Filename,
81                          Module &TheModule) : M(TheModule) {
82   FILE *F = fopen(Filename.c_str(), "r");
83   if (F == 0) {
84     std::cerr << ToolName << ": Error opening '" << Filename << ": ";
85     perror(0);
86     exit(1);
87   }
88
89   // Keep reading packets until we run out of them.
90   unsigned PacketType;
91   while (fread(&PacketType, sizeof(unsigned), 1, F) == 1) {
92     // If the low eight bits of the packet are zero, we must be dealing with an
93     // endianness mismatch.  Byteswap all words read from the profiling
94     // information.
95     bool ShouldByteSwap = (char)PacketType == 0;
96     PacketType = ByteSwap(PacketType, ShouldByteSwap);
97
98     switch (PacketType) {
99     case ArgumentInfo: {
100       unsigned ArgLength;
101       if (fread(&ArgLength, sizeof(unsigned), 1, F) != 1) {
102         std::cerr << ToolName << ": arguments packet truncated!\n";
103         perror(0);
104         exit(1);
105       }
106       ArgLength = ByteSwap(ArgLength, ShouldByteSwap);
107
108       // Read in the arguments...
109       std::vector<char> Chars(ArgLength+4);
110
111       if (ArgLength)
112         if (fread(&Chars[0], (ArgLength+3) & ~3, 1, F) != 1) {
113           std::cerr << ToolName << ": arguments packet truncated!\n";
114           perror(0);
115           exit(1);
116         }
117       CommandLines.push_back(std::string(&Chars[0], &Chars[ArgLength]));
118       break;
119     }
120       
121     case FunctionInfo:
122       ReadProfilingBlock(ToolName, F, ShouldByteSwap, FunctionCounts);
123       break;
124       
125     case BlockInfo:
126       ReadProfilingBlock(ToolName, F, ShouldByteSwap, BlockCounts);
127       break;
128
129     default:
130       std::cerr << ToolName << ": Unknown packet type #" << PacketType << "!\n";
131       exit(1);
132     }
133   }
134   
135   fclose(F);
136 }
137
138
139 // getFunctionCounts - This method is used by consumers of function counting
140 // information.  If we do not directly have function count information, we
141 // compute it from other, more refined, types of profile information.
142 //
143 void ProfileInfo::getFunctionCounts(std::vector<std::pair<Function*,
144                                                          unsigned> > &Counts) {
145   if (FunctionCounts.empty()) {
146     // Synthesize function frequency information from the number of times their
147     // entry blocks were executed.
148     std::vector<std::pair<BasicBlock*, unsigned> > BlockCounts;
149     getBlockCounts(BlockCounts);
150
151     for (unsigned i = 0, e = BlockCounts.size(); i != e; ++i)
152       if (&BlockCounts[i].first->getParent()->front() == BlockCounts[i].first)
153         Counts.push_back(std::make_pair(BlockCounts[i].first->getParent(),
154                                         BlockCounts[i].second));
155     return;
156   }
157   
158   unsigned Counter = 0;
159   for (Module::iterator I = M.begin(), E = M.end();
160        I != E && Counter != FunctionCounts.size(); ++I)
161     if (!I->isExternal())
162       Counts.push_back(std::make_pair(I, FunctionCounts[Counter++]));
163 }
164
165 // getBlockCounts - This method is used by consumers of block counting
166 // information.  If we do not directly have block count information, we
167 // compute it from other, more refined, types of profile information.
168 //
169 void ProfileInfo::getBlockCounts(std::vector<std::pair<BasicBlock*,
170                                                        unsigned> > &Counts) {
171   if (BlockCounts.empty()) {
172     std::cerr << "Block counts not available, and no synthesis "
173               << "is implemented yet!\n";
174     return;
175   }
176
177   unsigned Counter = 0;
178   for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F)
179     for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
180       Counts.push_back(std::make_pair(BB, BlockCounts[Counter++]));
181       if (Counter == BlockCounts.size())
182         return;
183     }
184 }