blob: cb7ae1a10c3a92ade0723ae5b882ba2b87794201 [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
Andreas Neustifterf0d568d2009-09-03 09:11:10 +000035static unsigned AddCounts(unsigned A, unsigned B) {
Andreas Neustifter4c2c5332009-09-03 08:41:05 +000036 // If either value is undefined, use the other.
37 if (A == ~0U) return B;
38 if (B == ~0U) return A;
39 return A + B;
40}
41
Chris Lattnerbc44aa62004-02-11 05:54:25 +000042static void ReadProfilingBlock(const char *ToolName, FILE *F,
43 bool ShouldByteSwap,
44 std::vector<unsigned> &Data) {
45 // Read the number of entries...
46 unsigned NumEntries;
47 if (fread(&NumEntries, sizeof(unsigned), 1, F) != 1) {
Chris Lattnera81d29b2009-08-23 07:33:14 +000048 errs() << ToolName << ": data packet truncated!\n";
Chris Lattnerbc44aa62004-02-11 05:54:25 +000049 perror(0);
50 exit(1);
51 }
52 NumEntries = ByteSwap(NumEntries, ShouldByteSwap);
53
54 // Read the counts...
55 std::vector<unsigned> TempSpace(NumEntries);
56
57 // Read in the block of data...
58 if (fread(&TempSpace[0], sizeof(unsigned)*NumEntries, 1, F) != 1) {
Chris Lattnera81d29b2009-08-23 07:33:14 +000059 errs() << ToolName << ": data packet truncated!\n";
Chris Lattnerbc44aa62004-02-11 05:54:25 +000060 perror(0);
61 exit(1);
62 }
63
Andreas Neustifterda5ea942009-09-01 19:08:51 +000064 // Make sure we have enough space... The space is initialised to -1 to
65 // facitiltate the loading of missing values for OptimalEdgeProfiling.
Chris Lattnerbc44aa62004-02-11 05:54:25 +000066 if (Data.size() < NumEntries)
Daniel Dunbar6b382d52009-09-01 22:07:12 +000067 Data.resize(NumEntries, ~0U);
Misha Brukman2b37d7c2005-04-21 21:13:18 +000068
Chris Lattnerbc44aa62004-02-11 05:54:25 +000069 // Accumulate the data we just read into the data.
70 if (!ShouldByteSwap) {
Andreas Neustifterda5ea942009-09-01 19:08:51 +000071 for (unsigned i = 0; i != NumEntries; ++i) {
Andreas Neustifter4c2c5332009-09-03 08:41:05 +000072 Data[i] = AddCounts(TempSpace[i], Data[i]);
Andreas Neustifterda5ea942009-09-01 19:08:51 +000073 }
Chris Lattnerbc44aa62004-02-11 05:54:25 +000074 } else {
Andreas Neustifterda5ea942009-09-01 19:08:51 +000075 for (unsigned i = 0; i != NumEntries; ++i) {
Andreas Neustifter4c2c5332009-09-03 08:41:05 +000076 Data[i] = AddCounts(ByteSwap(TempSpace[i], true), Data[i]);
Andreas Neustifterda5ea942009-09-01 19:08:51 +000077 }
Chris Lattnerbc44aa62004-02-11 05:54:25 +000078 }
79}
80
81// ProfileInfoLoader ctor - Read the specified profiling data file, exiting the
82// program if the file is invalid or broken.
83//
84ProfileInfoLoader::ProfileInfoLoader(const char *ToolName,
85 const std::string &Filename,
Daniel Dunbaree166382009-08-05 15:55:56 +000086 Module &TheModule) :
87 Filename(Filename),
88 M(TheModule), Warned(false) {
Andreas Neustiftercf48efc2009-08-25 12:53:27 +000089 FILE *F = fopen(Filename.c_str(), "rb");
Chris Lattnerbc44aa62004-02-11 05:54:25 +000090 if (F == 0) {
Chris Lattnera81d29b2009-08-23 07:33:14 +000091 errs() << ToolName << ": Error opening '" << Filename << "': ";
Chris Lattnerbc44aa62004-02-11 05:54:25 +000092 perror(0);
93 exit(1);
94 }
95
96 // Keep reading packets until we run out of them.
97 unsigned PacketType;
98 while (fread(&PacketType, sizeof(unsigned), 1, F) == 1) {
99 // If the low eight bits of the packet are zero, we must be dealing with an
100 // endianness mismatch. Byteswap all words read from the profiling
101 // information.
102 bool ShouldByteSwap = (char)PacketType == 0;
103 PacketType = ByteSwap(PacketType, ShouldByteSwap);
104
105 switch (PacketType) {
106 case ArgumentInfo: {
107 unsigned ArgLength;
108 if (fread(&ArgLength, sizeof(unsigned), 1, F) != 1) {
Chris Lattnera81d29b2009-08-23 07:33:14 +0000109 errs() << ToolName << ": arguments packet truncated!\n";
Chris Lattnerbc44aa62004-02-11 05:54:25 +0000110 perror(0);
111 exit(1);
112 }
113 ArgLength = ByteSwap(ArgLength, ShouldByteSwap);
114
115 // Read in the arguments...
116 std::vector<char> Chars(ArgLength+4);
117
118 if (ArgLength)
119 if (fread(&Chars[0], (ArgLength+3) & ~3, 1, F) != 1) {
Chris Lattnera81d29b2009-08-23 07:33:14 +0000120 errs() << ToolName << ": arguments packet truncated!\n";
Chris Lattnerbc44aa62004-02-11 05:54:25 +0000121 perror(0);
122 exit(1);
123 }
124 CommandLines.push_back(std::string(&Chars[0], &Chars[ArgLength]));
125 break;
126 }
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000127
Chris Lattnerbc44aa62004-02-11 05:54:25 +0000128 case FunctionInfo:
129 ReadProfilingBlock(ToolName, F, ShouldByteSwap, FunctionCounts);
130 break;
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000131
Chris Lattnerbc44aa62004-02-11 05:54:25 +0000132 case BlockInfo:
133 ReadProfilingBlock(ToolName, F, ShouldByteSwap, BlockCounts);
134 break;
135
Chris Lattner01945c12004-03-08 18:20:18 +0000136 case EdgeInfo:
137 ReadProfilingBlock(ToolName, F, ShouldByteSwap, EdgeCounts);
138 break;
139
Andreas Neustifterda5ea942009-09-01 19:08:51 +0000140 case OptEdgeInfo:
141 ReadProfilingBlock(ToolName, F, ShouldByteSwap, OptimalEdgeCounts);
142 break;
143
Brian Gaekeb171d792004-05-04 17:11:14 +0000144 case BBTraceInfo:
145 ReadProfilingBlock(ToolName, F, ShouldByteSwap, BBTrace);
146 break;
147
Chris Lattnerbc44aa62004-02-11 05:54:25 +0000148 default:
Chris Lattnera81d29b2009-08-23 07:33:14 +0000149 errs() << ToolName << ": Unknown packet type #" << PacketType << "!\n";
Chris Lattnerbc44aa62004-02-11 05:54:25 +0000150 exit(1);
151 }
152 }
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000153
Chris Lattnerbc44aa62004-02-11 05:54:25 +0000154 fclose(F);
155}
156