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"
21 ArgumentInfo = 1, // The command line argument block
22 FunctionInfo = 2, // Function profiling information
23 BlockInfo = 3, // Block profiling information
26 // ByteSwap - Byteswap 'Var' if 'Really' is true.
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);
36 static void ReadProfilingBlock(const char *ToolName, FILE *F,
38 std::vector<unsigned> &Data) {
39 // Read the number of entries...
41 if (fread(&NumEntries, sizeof(unsigned), 1, F) != 1) {
42 std::cerr << ToolName << ": data packet truncated!\n";
46 NumEntries = ByteSwap(NumEntries, ShouldByteSwap);
49 std::vector<unsigned> TempSpace(NumEntries);
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";
58 // Make sure we have enough space...
59 if (Data.size() < NumEntries)
60 Data.resize(NumEntries);
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];
67 for (unsigned i = 0; i != NumEntries; ++i)
68 Data[i] += ByteSwap(TempSpace[i], true);
72 // ProfileInfo ctor - Read the specified profiling data file, exiting the
73 // program if the file is invalid or broken.
75 ProfileInfo::ProfileInfo(const char *ToolName, const std::string &Filename,
76 Module &TheModule) : M(TheModule) {
77 FILE *F = fopen(Filename.c_str(), "r");
79 std::cerr << ToolName << ": Error opening '" << Filename << ": ";
84 // Keep reading packets until we run out of them.
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
90 bool ShouldByteSwap = (char)PacketType == 0;
91 PacketType = ByteSwap(PacketType, ShouldByteSwap);
96 if (fread(&ArgLength, sizeof(unsigned), 1, F) != 1) {
97 std::cerr << ToolName << ": arguments packet truncated!\n";
101 ArgLength = ByteSwap(ArgLength, ShouldByteSwap);
103 // Read in the arguments...
104 std::vector<char> Chars(ArgLength+4);
107 if (fread(&Chars[0], (ArgLength+3) & ~3, 1, F) != 1) {
108 std::cerr << ToolName << ": arguments packet truncated!\n";
112 CommandLines.push_back(std::string(&Chars[0], &Chars[ArgLength]));
117 ReadProfilingBlock(ToolName, F, ShouldByteSwap, FunctionCounts);
121 ReadProfilingBlock(ToolName, F, ShouldByteSwap, BlockCounts);
125 std::cerr << ToolName << ": Unknown packet type #" << PacketType << "!\n";
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.
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);
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));
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++]));
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.
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";
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())