1 //===- ProfileInfo.cpp - Represents profile information -------------------===//
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 ProfileInfo class is used to represent profiling information read in from
13 //===----------------------------------------------------------------------===//
15 #include "ProfileInfo.h"
16 #include "llvm/Module.h"
18 #include <sys/types.h>
26 ArgumentInfo = 1, // The command line argument block
27 FunctionInfo = 2, // Function profiling information
28 BlockInfo = 3, // Block profiling information
31 // ByteSwap - Byteswap 'Var' if 'Really' is true.
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);
41 static void ReadProfilingBlock(const char *ToolName, FILE *F,
43 std::vector<unsigned> &Data) {
44 // Read the number of entries...
46 if (fread(&NumEntries, sizeof(unsigned), 1, F) != 1) {
47 std::cerr << ToolName << ": data packet truncated!\n";
51 NumEntries = ByteSwap(NumEntries, ShouldByteSwap);
54 std::vector<unsigned> TempSpace(NumEntries);
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";
63 // Make sure we have enough space...
64 if (Data.size() < NumEntries)
65 Data.resize(NumEntries);
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];
72 for (unsigned i = 0; i != NumEntries; ++i)
73 Data[i] += ByteSwap(TempSpace[i], true);
77 // ProfileInfo ctor - Read the specified profiling data file, exiting the
78 // program if the file is invalid or broken.
80 ProfileInfo::ProfileInfo(const char *ToolName, const std::string &Filename,
81 Module &TheModule) : M(TheModule) {
82 FILE *F = fopen(Filename.c_str(), "r");
84 std::cerr << ToolName << ": Error opening '" << Filename << ": ";
89 // Keep reading packets until we run out of them.
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
95 bool ShouldByteSwap = (char)PacketType == 0;
96 PacketType = ByteSwap(PacketType, ShouldByteSwap);
101 if (fread(&ArgLength, sizeof(unsigned), 1, F) != 1) {
102 std::cerr << ToolName << ": arguments packet truncated!\n";
106 ArgLength = ByteSwap(ArgLength, ShouldByteSwap);
108 // Read in the arguments...
109 std::vector<char> Chars(ArgLength+4);
112 if (fread(&Chars[0], (ArgLength+3) & ~3, 1, F) != 1) {
113 std::cerr << ToolName << ": arguments packet truncated!\n";
117 CommandLines.push_back(std::string(&Chars[0], &Chars[ArgLength]));
122 ReadProfilingBlock(ToolName, F, ShouldByteSwap, FunctionCounts);
126 ReadProfilingBlock(ToolName, F, ShouldByteSwap, BlockCounts);
130 std::cerr << ToolName << ": Unknown packet type #" << PacketType << "!\n";
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.
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);
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));
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++]));
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.
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";
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())