blob: 5c7c97cad1e5a8e99ea5130be39da3e715c1702d [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 Lattnerbc44aa62004-02-11 05:54:25 +000022using namespace llvm;
23
Chris Lattnerbc44aa62004-02-11 05:54:25 +000024// ByteSwap - Byteswap 'Var' if 'Really' is true.
25//
26static inline unsigned ByteSwap(unsigned Var, bool Really) {
27 if (!Really) return Var;
Duncan Sands18f13c62009-09-06 12:56:52 +000028 return ((Var & (255U<< 0U)) << 24U) |
29 ((Var & (255U<< 8U)) << 8U) |
30 ((Var & (255U<<16U)) >> 8U) |
31 ((Var & (255U<<24U)) >> 24U);
Chris Lattnerbc44aa62004-02-11 05:54:25 +000032}
33
Andreas Neustifterf0d568d2009-09-03 09:11:10 +000034static unsigned AddCounts(unsigned A, unsigned B) {
Andreas Neustifter4c2c5332009-09-03 08:41:05 +000035 // If either value is undefined, use the other.
Andreas Neustifter92332722009-09-16 11:35:50 +000036 if (A == ProfileInfoLoader::Uncounted) return B;
37 if (B == ProfileInfoLoader::Uncounted) return A;
Andreas Neustifter4c2c5332009-09-03 08:41:05 +000038 return A + B;
39}
40
Chris Lattnerbc44aa62004-02-11 05:54:25 +000041static void ReadProfilingBlock(const char *ToolName, FILE *F,
42 bool ShouldByteSwap,
43 std::vector<unsigned> &Data) {
44 // Read the number of entries...
45 unsigned NumEntries;
46 if (fread(&NumEntries, sizeof(unsigned), 1, F) != 1) {
Chris Lattnera81d29b2009-08-23 07:33:14 +000047 errs() << ToolName << ": data packet truncated!\n";
Chris Lattnerbc44aa62004-02-11 05:54:25 +000048 perror(0);
49 exit(1);
50 }
51 NumEntries = ByteSwap(NumEntries, ShouldByteSwap);
52
53 // Read the counts...
54 std::vector<unsigned> TempSpace(NumEntries);
55
56 // Read in the block of data...
57 if (fread(&TempSpace[0], sizeof(unsigned)*NumEntries, 1, F) != 1) {
Chris Lattnera81d29b2009-08-23 07:33:14 +000058 errs() << ToolName << ": data packet truncated!\n";
Chris Lattnerbc44aa62004-02-11 05:54:25 +000059 perror(0);
60 exit(1);
61 }
62
Andreas Neustifterda5ea942009-09-01 19:08:51 +000063 // Make sure we have enough space... The space is initialised to -1 to
64 // facitiltate the loading of missing values for OptimalEdgeProfiling.
Chris Lattnerbc44aa62004-02-11 05:54:25 +000065 if (Data.size() < NumEntries)
Andreas Neustifter92332722009-09-16 11:35:50 +000066 Data.resize(NumEntries, ProfileInfoLoader::Uncounted);
Misha Brukman2b37d7c2005-04-21 21:13:18 +000067
Chris Lattnerbc44aa62004-02-11 05:54:25 +000068 // Accumulate the data we just read into the data.
69 if (!ShouldByteSwap) {
Andreas Neustifterda5ea942009-09-01 19:08:51 +000070 for (unsigned i = 0; i != NumEntries; ++i) {
Andreas Neustifter4c2c5332009-09-03 08:41:05 +000071 Data[i] = AddCounts(TempSpace[i], Data[i]);
Andreas Neustifterda5ea942009-09-01 19:08:51 +000072 }
Chris Lattnerbc44aa62004-02-11 05:54:25 +000073 } else {
Andreas Neustifterda5ea942009-09-01 19:08:51 +000074 for (unsigned i = 0; i != NumEntries; ++i) {
Andreas Neustifter4c2c5332009-09-03 08:41:05 +000075 Data[i] = AddCounts(ByteSwap(TempSpace[i], true), Data[i]);
Andreas Neustifterda5ea942009-09-01 19:08:51 +000076 }
Chris Lattnerbc44aa62004-02-11 05:54:25 +000077 }
78}
79
Andreas Neustifter92332722009-09-16 11:35:50 +000080const unsigned ProfileInfoLoader::Uncounted = ~0U;
81
Chris Lattnerbc44aa62004-02-11 05:54:25 +000082// ProfileInfoLoader ctor - Read the specified profiling data file, exiting the
83// program if the file is invalid or broken.
84//
85ProfileInfoLoader::ProfileInfoLoader(const char *ToolName,
Benjamin Kramer95a9d932012-06-06 19:47:08 +000086 const std::string &Filename)
Benjamin Kramere288cd12012-07-20 22:05:57 +000087 : Filename(Filename) {
Andreas Neustiftercf48efc2009-08-25 12:53:27 +000088 FILE *F = fopen(Filename.c_str(), "rb");
Chris Lattnerbc44aa62004-02-11 05:54:25 +000089 if (F == 0) {
Chris Lattnera81d29b2009-08-23 07:33:14 +000090 errs() << ToolName << ": Error opening '" << Filename << "': ";
Chris Lattnerbc44aa62004-02-11 05:54:25 +000091 perror(0);
92 exit(1);
93 }
94
95 // Keep reading packets until we run out of them.
96 unsigned PacketType;
97 while (fread(&PacketType, sizeof(unsigned), 1, F) == 1) {
98 // If the low eight bits of the packet are zero, we must be dealing with an
99 // endianness mismatch. Byteswap all words read from the profiling
100 // information.
101 bool ShouldByteSwap = (char)PacketType == 0;
102 PacketType = ByteSwap(PacketType, ShouldByteSwap);
103
104 switch (PacketType) {
105 case ArgumentInfo: {
106 unsigned ArgLength;
107 if (fread(&ArgLength, sizeof(unsigned), 1, F) != 1) {
Chris Lattnera81d29b2009-08-23 07:33:14 +0000108 errs() << ToolName << ": arguments packet truncated!\n";
Chris Lattnerbc44aa62004-02-11 05:54:25 +0000109 perror(0);
110 exit(1);
111 }
112 ArgLength = ByteSwap(ArgLength, ShouldByteSwap);
113
114 // Read in the arguments...
115 std::vector<char> Chars(ArgLength+4);
116
117 if (ArgLength)
118 if (fread(&Chars[0], (ArgLength+3) & ~3, 1, F) != 1) {
Chris Lattnera81d29b2009-08-23 07:33:14 +0000119 errs() << ToolName << ": arguments packet truncated!\n";
Chris Lattnerbc44aa62004-02-11 05:54:25 +0000120 perror(0);
121 exit(1);
122 }
123 CommandLines.push_back(std::string(&Chars[0], &Chars[ArgLength]));
124 break;
125 }
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000126
Chris Lattnerbc44aa62004-02-11 05:54:25 +0000127 case FunctionInfo:
128 ReadProfilingBlock(ToolName, F, ShouldByteSwap, FunctionCounts);
129 break;
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000130
Chris Lattnerbc44aa62004-02-11 05:54:25 +0000131 case BlockInfo:
132 ReadProfilingBlock(ToolName, F, ShouldByteSwap, BlockCounts);
133 break;
134
Chris Lattner01945c12004-03-08 18:20:18 +0000135 case EdgeInfo:
136 ReadProfilingBlock(ToolName, F, ShouldByteSwap, EdgeCounts);
137 break;
138
Andreas Neustifterda5ea942009-09-01 19:08:51 +0000139 case OptEdgeInfo:
140 ReadProfilingBlock(ToolName, F, ShouldByteSwap, OptimalEdgeCounts);
141 break;
142
Brian Gaekeb171d792004-05-04 17:11:14 +0000143 case BBTraceInfo:
144 ReadProfilingBlock(ToolName, F, ShouldByteSwap, BBTrace);
145 break;
146
Chris Lattnerbc44aa62004-02-11 05:54:25 +0000147 default:
Chris Lattnera81d29b2009-08-23 07:33:14 +0000148 errs() << ToolName << ": Unknown packet type #" << PacketType << "!\n";
Chris Lattnerbc44aa62004-02-11 05:54:25 +0000149 exit(1);
150 }
151 }
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000152
Chris Lattnerbc44aa62004-02-11 05:54:25 +0000153 fclose(F);
154}
155