Chris Lattner | bc44aa6 | 2004-02-11 05:54:25 +0000 | [diff] [blame] | 1 | //===- ProfileInfoLoad.cpp - Load profile information from disk -----------===// |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 2 | // |
Chris Lattner | bc44aa6 | 2004-02-11 05:54:25 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 7 | // |
Chris Lattner | bc44aa6 | 2004-02-11 05:54:25 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 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 Gaeke | 660ef70 | 2004-05-04 16:53:07 +0000 | [diff] [blame] | 16 | #include "llvm/Analysis/ProfileInfoTypes.h" |
Chris Lattner | bc44aa6 | 2004-02-11 05:54:25 +0000 | [diff] [blame] | 17 | #include "llvm/Module.h" |
Chris Lattner | 01945c1 | 2004-03-08 18:20:18 +0000 | [diff] [blame] | 18 | #include "llvm/InstrTypes.h" |
Chris Lattner | a81d29b | 2009-08-23 07:33:14 +0000 | [diff] [blame] | 19 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | bc44aa6 | 2004-02-11 05:54:25 +0000 | [diff] [blame] | 20 | #include <cstdio> |
Dan Gohman | d68a076 | 2009-01-05 17:59:02 +0000 | [diff] [blame] | 21 | #include <cstdlib> |
Chris Lattner | dbbbfef | 2004-03-08 20:03:52 +0000 | [diff] [blame] | 22 | #include <map> |
Chris Lattner | bc44aa6 | 2004-02-11 05:54:25 +0000 | [diff] [blame] | 23 | using namespace llvm; |
| 24 | |
Chris Lattner | bc44aa6 | 2004-02-11 05:54:25 +0000 | [diff] [blame] | 25 | // ByteSwap - Byteswap 'Var' if 'Really' is true. |
| 26 | // |
| 27 | static inline unsigned ByteSwap(unsigned Var, bool Really) { |
| 28 | if (!Really) return Var; |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 29 | return ((Var & (255<< 0)) << 24) | |
| 30 | ((Var & (255<< 8)) << 8) | |
| 31 | ((Var & (255<<16)) >> 8) | |
Chris Lattner | bc44aa6 | 2004-02-11 05:54:25 +0000 | [diff] [blame] | 32 | ((Var & (255<<24)) >> 24); |
| 33 | } |
| 34 | |
| 35 | static 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 Lattner | a81d29b | 2009-08-23 07:33:14 +0000 | [diff] [blame] | 41 | errs() << ToolName << ": data packet truncated!\n"; |
Chris Lattner | bc44aa6 | 2004-02-11 05:54:25 +0000 | [diff] [blame] | 42 | 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 Lattner | a81d29b | 2009-08-23 07:33:14 +0000 | [diff] [blame] | 52 | errs() << ToolName << ": data packet truncated!\n"; |
Chris Lattner | bc44aa6 | 2004-02-11 05:54:25 +0000 | [diff] [blame] | 53 | perror(0); |
| 54 | exit(1); |
| 55 | } |
| 56 | |
| 57 | // Make sure we have enough space... |
| 58 | if (Data.size() < NumEntries) |
| 59 | Data.resize(NumEntries); |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 60 | |
Chris Lattner | bc44aa6 | 2004-02-11 05:54:25 +0000 | [diff] [blame] | 61 | // Accumulate the data we just read into the data. |
| 62 | if (!ShouldByteSwap) { |
| 63 | for (unsigned i = 0; i != NumEntries; ++i) |
| 64 | Data[i] += TempSpace[i]; |
| 65 | } else { |
| 66 | for (unsigned i = 0; i != NumEntries; ++i) |
| 67 | Data[i] += ByteSwap(TempSpace[i], true); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | // ProfileInfoLoader ctor - Read the specified profiling data file, exiting the |
| 72 | // program if the file is invalid or broken. |
| 73 | // |
| 74 | ProfileInfoLoader::ProfileInfoLoader(const char *ToolName, |
| 75 | const std::string &Filename, |
Daniel Dunbar | ee16638 | 2009-08-05 15:55:56 +0000 | [diff] [blame] | 76 | Module &TheModule) : |
| 77 | Filename(Filename), |
| 78 | M(TheModule), Warned(false) { |
Andreas Neustifter | cf48efc | 2009-08-25 12:53:27 +0000 | [diff] [blame^] | 79 | FILE *F = fopen(Filename.c_str(), "rb"); |
Chris Lattner | bc44aa6 | 2004-02-11 05:54:25 +0000 | [diff] [blame] | 80 | if (F == 0) { |
Chris Lattner | a81d29b | 2009-08-23 07:33:14 +0000 | [diff] [blame] | 81 | errs() << ToolName << ": Error opening '" << Filename << "': "; |
Chris Lattner | bc44aa6 | 2004-02-11 05:54:25 +0000 | [diff] [blame] | 82 | perror(0); |
| 83 | exit(1); |
| 84 | } |
| 85 | |
| 86 | // Keep reading packets until we run out of them. |
| 87 | unsigned PacketType; |
| 88 | while (fread(&PacketType, sizeof(unsigned), 1, F) == 1) { |
| 89 | // If the low eight bits of the packet are zero, we must be dealing with an |
| 90 | // endianness mismatch. Byteswap all words read from the profiling |
| 91 | // information. |
| 92 | bool ShouldByteSwap = (char)PacketType == 0; |
| 93 | PacketType = ByteSwap(PacketType, ShouldByteSwap); |
| 94 | |
| 95 | switch (PacketType) { |
| 96 | case ArgumentInfo: { |
| 97 | unsigned ArgLength; |
| 98 | if (fread(&ArgLength, sizeof(unsigned), 1, F) != 1) { |
Chris Lattner | a81d29b | 2009-08-23 07:33:14 +0000 | [diff] [blame] | 99 | errs() << ToolName << ": arguments packet truncated!\n"; |
Chris Lattner | bc44aa6 | 2004-02-11 05:54:25 +0000 | [diff] [blame] | 100 | perror(0); |
| 101 | exit(1); |
| 102 | } |
| 103 | ArgLength = ByteSwap(ArgLength, ShouldByteSwap); |
| 104 | |
| 105 | // Read in the arguments... |
| 106 | std::vector<char> Chars(ArgLength+4); |
| 107 | |
| 108 | if (ArgLength) |
| 109 | if (fread(&Chars[0], (ArgLength+3) & ~3, 1, F) != 1) { |
Chris Lattner | a81d29b | 2009-08-23 07:33:14 +0000 | [diff] [blame] | 110 | errs() << ToolName << ": arguments packet truncated!\n"; |
Chris Lattner | bc44aa6 | 2004-02-11 05:54:25 +0000 | [diff] [blame] | 111 | perror(0); |
| 112 | exit(1); |
| 113 | } |
| 114 | CommandLines.push_back(std::string(&Chars[0], &Chars[ArgLength])); |
| 115 | break; |
| 116 | } |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 117 | |
Chris Lattner | bc44aa6 | 2004-02-11 05:54:25 +0000 | [diff] [blame] | 118 | case FunctionInfo: |
| 119 | ReadProfilingBlock(ToolName, F, ShouldByteSwap, FunctionCounts); |
| 120 | break; |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 121 | |
Chris Lattner | bc44aa6 | 2004-02-11 05:54:25 +0000 | [diff] [blame] | 122 | case BlockInfo: |
| 123 | ReadProfilingBlock(ToolName, F, ShouldByteSwap, BlockCounts); |
| 124 | break; |
| 125 | |
Chris Lattner | 01945c1 | 2004-03-08 18:20:18 +0000 | [diff] [blame] | 126 | case EdgeInfo: |
| 127 | ReadProfilingBlock(ToolName, F, ShouldByteSwap, EdgeCounts); |
| 128 | break; |
| 129 | |
Brian Gaeke | b171d79 | 2004-05-04 17:11:14 +0000 | [diff] [blame] | 130 | case BBTraceInfo: |
| 131 | ReadProfilingBlock(ToolName, F, ShouldByteSwap, BBTrace); |
| 132 | break; |
| 133 | |
Chris Lattner | bc44aa6 | 2004-02-11 05:54:25 +0000 | [diff] [blame] | 134 | default: |
Chris Lattner | a81d29b | 2009-08-23 07:33:14 +0000 | [diff] [blame] | 135 | errs() << ToolName << ": Unknown packet type #" << PacketType << "!\n"; |
Chris Lattner | bc44aa6 | 2004-02-11 05:54:25 +0000 | [diff] [blame] | 136 | exit(1); |
| 137 | } |
| 138 | } |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 139 | |
Chris Lattner | bc44aa6 | 2004-02-11 05:54:25 +0000 | [diff] [blame] | 140 | fclose(F); |
| 141 | } |
| 142 | |