blob: dfb79ff875c8786dd9fff9b7ac004b0ab05c5157 [file] [log] [blame]
Chris Lattnerbc44aa62004-02-11 05:54:25 +00001//===- ProfileInfoLoad.cpp - Load profile information from disk -----------===//
Misha Brukman2b37d7c2005-04-21 21:13:18 +00002//
Chris Lattnerbc44aa62004-02-11 05:54:25 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman2b37d7c2005-04-21 21:13:18 +00007//
Chris Lattnerbc44aa62004-02-11 05:54:25 +00008//===----------------------------------------------------------------------===//
9//
10// The ProfileInfoLoader class is used to load and represent profiling
11// information read in from the dump file.
12//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/Analysis/ProfileInfoLoader.h"
Brian Gaeke660ef702004-05-04 16:53:07 +000016#include "llvm/Analysis/ProfileInfoTypes.h"
Chris Lattnerbc44aa62004-02-11 05:54:25 +000017#include "llvm/Module.h"
Chris Lattner01945c12004-03-08 18:20:18 +000018#include "llvm/InstrTypes.h"
Chris Lattnera81d29b2009-08-23 07:33:14 +000019#include "llvm/Support/raw_ostream.h"
Chris Lattnerbc44aa62004-02-11 05:54:25 +000020#include <cstdio>
Dan Gohmand68a0762009-01-05 17:59:02 +000021#include <cstdlib>
Chris Lattnerdbbbfef2004-03-08 20:03:52 +000022#include <map>
Chris Lattnerbc44aa62004-02-11 05:54:25 +000023using namespace llvm;
24
Chris Lattnerbc44aa62004-02-11 05:54:25 +000025// ByteSwap - Byteswap 'Var' if 'Really' is true.
26//
27static inline unsigned ByteSwap(unsigned Var, bool Really) {
28 if (!Really) return Var;
Misha Brukman2b37d7c2005-04-21 21:13:18 +000029 return ((Var & (255<< 0)) << 24) |
30 ((Var & (255<< 8)) << 8) |
31 ((Var & (255<<16)) >> 8) |
Chris Lattnerbc44aa62004-02-11 05:54:25 +000032 ((Var & (255<<24)) >> 24);
33}
34
35static void ReadProfilingBlock(const char *ToolName, FILE *F,
36 bool ShouldByteSwap,
37 std::vector<unsigned> &Data) {
38 // Read the number of entries...
39 unsigned NumEntries;
40 if (fread(&NumEntries, sizeof(unsigned), 1, F) != 1) {
Chris Lattnera81d29b2009-08-23 07:33:14 +000041 errs() << ToolName << ": data packet truncated!\n";
Chris Lattnerbc44aa62004-02-11 05:54:25 +000042 perror(0);
43 exit(1);
44 }
45 NumEntries = ByteSwap(NumEntries, ShouldByteSwap);
46
47 // Read the counts...
48 std::vector<unsigned> TempSpace(NumEntries);
49
50 // Read in the block of data...
51 if (fread(&TempSpace[0], sizeof(unsigned)*NumEntries, 1, F) != 1) {
Chris Lattnera81d29b2009-08-23 07:33:14 +000052 errs() << ToolName << ": data packet truncated!\n";
Chris Lattnerbc44aa62004-02-11 05:54:25 +000053 perror(0);
54 exit(1);
55 }
56
Andreas Neustifterda5ea942009-09-01 19:08:51 +000057 // Make sure we have enough space... The space is initialised to -1 to
58 // facitiltate the loading of missing values for OptimalEdgeProfiling.
Chris Lattnerbc44aa62004-02-11 05:54:25 +000059 if (Data.size() < NumEntries)
Daniel Dunbar6b382d52009-09-01 22:07:12 +000060 Data.resize(NumEntries, ~0U);
Misha Brukman2b37d7c2005-04-21 21:13:18 +000061
Chris Lattnerbc44aa62004-02-11 05:54:25 +000062 // Accumulate the data we just read into the data.
63 if (!ShouldByteSwap) {
Andreas Neustifterda5ea942009-09-01 19:08:51 +000064 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
69 } else {
70 Data[i] += data;
71 }
72 }
73 }
Chris Lattnerbc44aa62004-02-11 05:54:25 +000074 } else {
Andreas Neustifterda5ea942009-09-01 19:08:51 +000075 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) {
79 Data[i] = data;
80 } else {
81 Data[i] += data;
82 }
83 }
84 }
Chris Lattnerbc44aa62004-02-11 05:54:25 +000085 }
86}
87
88// ProfileInfoLoader ctor - Read the specified profiling data file, exiting the
89// program if the file is invalid or broken.
90//
91ProfileInfoLoader::ProfileInfoLoader(const char *ToolName,
92 const std::string &Filename,
Daniel Dunbaree166382009-08-05 15:55:56 +000093 Module &TheModule) :
94 Filename(Filename),
95 M(TheModule), Warned(false) {
Andreas Neustiftercf48efc2009-08-25 12:53:27 +000096 FILE *F = fopen(Filename.c_str(), "rb");
Chris Lattnerbc44aa62004-02-11 05:54:25 +000097 if (F == 0) {
Chris Lattnera81d29b2009-08-23 07:33:14 +000098 errs() << ToolName << ": Error opening '" << Filename << "': ";
Chris Lattnerbc44aa62004-02-11 05:54:25 +000099 perror(0);
100 exit(1);
101 }
102
103 // Keep reading packets until we run out of them.
104 unsigned PacketType;
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
108 // information.
109 bool ShouldByteSwap = (char)PacketType == 0;
110 PacketType = ByteSwap(PacketType, ShouldByteSwap);
111
112 switch (PacketType) {
113 case ArgumentInfo: {
114 unsigned ArgLength;
115 if (fread(&ArgLength, sizeof(unsigned), 1, F) != 1) {
Chris Lattnera81d29b2009-08-23 07:33:14 +0000116 errs() << ToolName << ": arguments packet truncated!\n";
Chris Lattnerbc44aa62004-02-11 05:54:25 +0000117 perror(0);
118 exit(1);
119 }
120 ArgLength = ByteSwap(ArgLength, ShouldByteSwap);
121
122 // Read in the arguments...
123 std::vector<char> Chars(ArgLength+4);
124
125 if (ArgLength)
126 if (fread(&Chars[0], (ArgLength+3) & ~3, 1, F) != 1) {
Chris Lattnera81d29b2009-08-23 07:33:14 +0000127 errs() << ToolName << ": arguments packet truncated!\n";
Chris Lattnerbc44aa62004-02-11 05:54:25 +0000128 perror(0);
129 exit(1);
130 }
131 CommandLines.push_back(std::string(&Chars[0], &Chars[ArgLength]));
132 break;
133 }
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000134
Chris Lattnerbc44aa62004-02-11 05:54:25 +0000135 case FunctionInfo:
136 ReadProfilingBlock(ToolName, F, ShouldByteSwap, FunctionCounts);
137 break;
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000138
Chris Lattnerbc44aa62004-02-11 05:54:25 +0000139 case BlockInfo:
140 ReadProfilingBlock(ToolName, F, ShouldByteSwap, BlockCounts);
141 break;
142
Chris Lattner01945c12004-03-08 18:20:18 +0000143 case EdgeInfo:
144 ReadProfilingBlock(ToolName, F, ShouldByteSwap, EdgeCounts);
145 break;
146
Andreas Neustifterda5ea942009-09-01 19:08:51 +0000147 case OptEdgeInfo:
148 ReadProfilingBlock(ToolName, F, ShouldByteSwap, OptimalEdgeCounts);
149 break;
150
Brian Gaekeb171d792004-05-04 17:11:14 +0000151 case BBTraceInfo:
152 ReadProfilingBlock(ToolName, F, ShouldByteSwap, BBTrace);
153 break;
154
Chris Lattnerbc44aa62004-02-11 05:54:25 +0000155 default:
Chris Lattnera81d29b2009-08-23 07:33:14 +0000156 errs() << ToolName << ": Unknown packet type #" << PacketType << "!\n";
Chris Lattnerbc44aa62004-02-11 05:54:25 +0000157 exit(1);
158 }
159 }
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000160
Chris Lattnerbc44aa62004-02-11 05:54:25 +0000161 fclose(F);
162}
163