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 & (255<< 0)) << 24) |
30 ((Var & (255<< 8)) << 8) |
31 ((Var & (255<<16)) >> 8) |
32 ((Var & (255<<24)) >> 24);
35 static void ReadProfilingBlock(const char *ToolName, FILE *F,
37 std::vector<unsigned> &Data) {
38 // Read the number of entries...
40 if (fread(&NumEntries, sizeof(unsigned), 1, F) != 1) {
41 errs() << ToolName << ": data packet truncated!\n";
45 NumEntries = ByteSwap(NumEntries, ShouldByteSwap);
48 std::vector<unsigned> TempSpace(NumEntries);
50 // Read in the block of data...
51 if (fread(&TempSpace[0], sizeof(unsigned)*NumEntries, 1, F) != 1) {
52 errs() << ToolName << ": data packet truncated!\n";
57 // Make sure we have enough space... The space is initialised to -1 to
58 // facitiltate the loading of missing values for OptimalEdgeProfiling.
59 if (Data.size() < NumEntries)
60 Data.resize(NumEntries, ~0U);
62 // Accumulate the data we just read into the data.
63 if (!ShouldByteSwap) {
64 for (unsigned i = 0; i != NumEntries; ++i) {
65 unsigned data = TempSpace[i];
66 if (data != (unsigned)-1) { // only load data if its not MissingVal
67 if (Data[i] == (unsigned)-1) {
68 Data[i] = data; // if data is still initialised
75 for (unsigned i = 0; i != NumEntries; ++i) {
76 unsigned data = ByteSwap(TempSpace[i], true);
77 if (data != (unsigned)-1) { // only load data if its not MissingVal
78 if (Data[i] == (unsigned)-1) {
88 // ProfileInfoLoader ctor - Read the specified profiling data file, exiting the
89 // program if the file is invalid or broken.
91 ProfileInfoLoader::ProfileInfoLoader(const char *ToolName,
92 const std::string &Filename,
95 M(TheModule), Warned(false) {
96 FILE *F = fopen(Filename.c_str(), "rb");
98 errs() << ToolName << ": Error opening '" << Filename << "': ";
103 // Keep reading packets until we run out of them.
105 while (fread(&PacketType, sizeof(unsigned), 1, F) == 1) {
106 // If the low eight bits of the packet are zero, we must be dealing with an
107 // endianness mismatch. Byteswap all words read from the profiling
109 bool ShouldByteSwap = (char)PacketType == 0;
110 PacketType = ByteSwap(PacketType, ShouldByteSwap);
112 switch (PacketType) {
115 if (fread(&ArgLength, sizeof(unsigned), 1, F) != 1) {
116 errs() << ToolName << ": arguments packet truncated!\n";
120 ArgLength = ByteSwap(ArgLength, ShouldByteSwap);
122 // Read in the arguments...
123 std::vector<char> Chars(ArgLength+4);
126 if (fread(&Chars[0], (ArgLength+3) & ~3, 1, F) != 1) {
127 errs() << ToolName << ": arguments packet truncated!\n";
131 CommandLines.push_back(std::string(&Chars[0], &Chars[ArgLength]));
136 ReadProfilingBlock(ToolName, F, ShouldByteSwap, FunctionCounts);
140 ReadProfilingBlock(ToolName, F, ShouldByteSwap, BlockCounts);
144 ReadProfilingBlock(ToolName, F, ShouldByteSwap, EdgeCounts);
148 ReadProfilingBlock(ToolName, F, ShouldByteSwap, OptimalEdgeCounts);
152 ReadProfilingBlock(ToolName, F, ShouldByteSwap, BBTrace);
156 errs() << ToolName << ": Unknown packet type #" << PacketType << "!\n";