1 //===- ProfileInfoLoad.cpp - Load profile information from disk -----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // 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"
19 #include "llvm/Support/raw_ostream.h"
25 // ByteSwap - Byteswap 'Var' if 'Really' is true.
27 static inline unsigned ByteSwap(unsigned Var, bool Really) {
28 if (!Really) return Var;
29 return ((Var & (255U<< 0U)) << 24U) |
30 ((Var & (255U<< 8U)) << 8U) |
31 ((Var & (255U<<16U)) >> 8U) |
32 ((Var & (255U<<24U)) >> 24U);
35 static unsigned AddCounts(unsigned A, unsigned B) {
36 // If either value is undefined, use the other.
37 if (A == ProfileInfoLoader::Uncounted) return B;
38 if (B == ProfileInfoLoader::Uncounted) return A;
42 static void ReadProfilingBlock(const char *ToolName, FILE *F,
44 std::vector<unsigned> &Data) {
45 // Read the number of entries...
47 if (fread(&NumEntries, sizeof(unsigned), 1, F) != 1) {
48 errs() << ToolName << ": data packet truncated!\n";
52 NumEntries = ByteSwap(NumEntries, ShouldByteSwap);
55 std::vector<unsigned> TempSpace(NumEntries);
57 // Read in the block of data...
58 if (fread(&TempSpace[0], sizeof(unsigned)*NumEntries, 1, F) != 1) {
59 errs() << ToolName << ": data packet truncated!\n";
64 // Make sure we have enough space... The space is initialised to -1 to
65 // facitiltate the loading of missing values for OptimalEdgeProfiling.
66 if (Data.size() < NumEntries)
67 Data.resize(NumEntries, ProfileInfoLoader::Uncounted);
69 // Accumulate the data we just read into the data.
70 if (!ShouldByteSwap) {
71 for (unsigned i = 0; i != NumEntries; ++i) {
72 Data[i] = AddCounts(TempSpace[i], Data[i]);
75 for (unsigned i = 0; i != NumEntries; ++i) {
76 Data[i] = AddCounts(ByteSwap(TempSpace[i], true), Data[i]);
81 const unsigned ProfileInfoLoader::Uncounted = ~0U;
83 // ProfileInfoLoader ctor - Read the specified profiling data file, exiting the
84 // program if the file is invalid or broken.
86 ProfileInfoLoader::ProfileInfoLoader(const char *ToolName,
87 const std::string &Filename,
90 M(TheModule), Warned(false) {
91 FILE *F = fopen(Filename.c_str(), "rb");
93 errs() << ToolName << ": Error opening '" << Filename << "': ";
98 // Keep reading packets until we run out of them.
100 while (fread(&PacketType, sizeof(unsigned), 1, F) == 1) {
101 // If the low eight bits of the packet are zero, we must be dealing with an
102 // endianness mismatch. Byteswap all words read from the profiling
104 bool ShouldByteSwap = (char)PacketType == 0;
105 PacketType = ByteSwap(PacketType, ShouldByteSwap);
107 switch (PacketType) {
110 if (fread(&ArgLength, sizeof(unsigned), 1, F) != 1) {
111 errs() << ToolName << ": arguments packet truncated!\n";
115 ArgLength = ByteSwap(ArgLength, ShouldByteSwap);
117 // Read in the arguments...
118 std::vector<char> Chars(ArgLength+4);
121 if (fread(&Chars[0], (ArgLength+3) & ~3, 1, F) != 1) {
122 errs() << ToolName << ": arguments packet truncated!\n";
126 CommandLines.push_back(std::string(&Chars[0], &Chars[ArgLength]));
131 ReadProfilingBlock(ToolName, F, ShouldByteSwap, FunctionCounts);
135 ReadProfilingBlock(ToolName, F, ShouldByteSwap, BlockCounts);
139 ReadProfilingBlock(ToolName, F, ShouldByteSwap, EdgeCounts);
143 ReadProfilingBlock(ToolName, F, ShouldByteSwap, OptimalEdgeCounts);
147 ReadProfilingBlock(ToolName, F, ShouldByteSwap, BBTrace);
151 errs() << ToolName << ": Unknown packet type #" << PacketType << "!\n";