several major improvements to the sparc backend: support for weak linkage
[llvm/avr.git] / lib / Analysis / ProfileInfoLoader.cpp
bloba6c55dfd5f7a6e45f17e322627ef8f32acc52236
1 //===- ProfileInfoLoad.cpp - Load profile information from disk -----------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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"
20 #include <cstdio>
21 #include <cstdlib>
22 #include <map>
23 using namespace llvm;
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 == ~0U) return B;
38 if (B == ~0U) return A;
39 return A + B;
42 static 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) {
48 errs() << ToolName << ": data packet truncated!\n";
49 perror(0);
50 exit(1);
52 NumEntries = ByteSwap(NumEntries, ShouldByteSwap);
54 // Read the counts...
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";
60 perror(0);
61 exit(1);
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, ~0U);
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]);
74 } else {
75 for (unsigned i = 0; i != NumEntries; ++i) {
76 Data[i] = AddCounts(ByteSwap(TempSpace[i], true), Data[i]);
81 // ProfileInfoLoader ctor - Read the specified profiling data file, exiting the
82 // program if the file is invalid or broken.
84 ProfileInfoLoader::ProfileInfoLoader(const char *ToolName,
85 const std::string &Filename,
86 Module &TheModule) :
87 Filename(Filename),
88 M(TheModule), Warned(false) {
89 FILE *F = fopen(Filename.c_str(), "rb");
90 if (F == 0) {
91 errs() << ToolName << ": Error opening '" << Filename << "': ";
92 perror(0);
93 exit(1);
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);
105 switch (PacketType) {
106 case ArgumentInfo: {
107 unsigned ArgLength;
108 if (fread(&ArgLength, sizeof(unsigned), 1, F) != 1) {
109 errs() << ToolName << ": arguments packet truncated!\n";
110 perror(0);
111 exit(1);
113 ArgLength = ByteSwap(ArgLength, ShouldByteSwap);
115 // Read in the arguments...
116 std::vector<char> Chars(ArgLength+4);
118 if (ArgLength)
119 if (fread(&Chars[0], (ArgLength+3) & ~3, 1, F) != 1) {
120 errs() << ToolName << ": arguments packet truncated!\n";
121 perror(0);
122 exit(1);
124 CommandLines.push_back(std::string(&Chars[0], &Chars[ArgLength]));
125 break;
128 case FunctionInfo:
129 ReadProfilingBlock(ToolName, F, ShouldByteSwap, FunctionCounts);
130 break;
132 case BlockInfo:
133 ReadProfilingBlock(ToolName, F, ShouldByteSwap, BlockCounts);
134 break;
136 case EdgeInfo:
137 ReadProfilingBlock(ToolName, F, ShouldByteSwap, EdgeCounts);
138 break;
140 case OptEdgeInfo:
141 ReadProfilingBlock(ToolName, F, ShouldByteSwap, OptimalEdgeCounts);
142 break;
144 case BBTraceInfo:
145 ReadProfilingBlock(ToolName, F, ShouldByteSwap, BBTrace);
146 break;
148 default:
149 errs() << ToolName << ": Unknown packet type #" << PacketType << "!\n";
150 exit(1);
154 fclose(F);