Manman Ren | d262004 | 2012-08-28 22:21:25 +0000 | [diff] [blame] | 1 | //===- ProfileDataLoader.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 ProfileDataLoader class is used to load raw profiling data from the dump |
| 11 | // file. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Bill Wendling | f91e400 | 2012-08-31 05:18:31 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/ArrayRef.h" |
| 16 | #include "llvm/ADT/OwningPtr.h" |
Manman Ren | d262004 | 2012-08-28 22:21:25 +0000 | [diff] [blame] | 17 | #include "llvm/Module.h" |
| 18 | #include "llvm/InstrTypes.h" |
| 19 | #include "llvm/Analysis/ProfileDataLoader.h" |
| 20 | #include "llvm/Analysis/ProfileDataTypes.h" |
| 21 | #include "llvm/Support/raw_ostream.h" |
Bill Wendling | f91e400 | 2012-08-31 05:18:31 +0000 | [diff] [blame] | 22 | #include "llvm/Support/system_error.h" |
Manman Ren | d262004 | 2012-08-28 22:21:25 +0000 | [diff] [blame] | 23 | #include <cstdio> |
| 24 | #include <cstdlib> |
| 25 | using namespace llvm; |
| 26 | |
Benjamin Kramer | cb5f63d | 2012-08-31 12:43:07 +0000 | [diff] [blame] | 27 | raw_ostream &llvm::operator<<(raw_ostream &O, std::pair<const BasicBlock *, |
| 28 | const BasicBlock *> E) { |
Manman Ren | d262004 | 2012-08-28 22:21:25 +0000 | [diff] [blame] | 29 | O << "("; |
| 30 | |
| 31 | if (E.first) |
Benjamin Kramer | cb5f63d | 2012-08-31 12:43:07 +0000 | [diff] [blame] | 32 | O << E.first->getName(); |
Manman Ren | d262004 | 2012-08-28 22:21:25 +0000 | [diff] [blame] | 33 | else |
| 34 | O << "0"; |
| 35 | |
| 36 | O << ","; |
| 37 | |
| 38 | if (E.second) |
Benjamin Kramer | cb5f63d | 2012-08-31 12:43:07 +0000 | [diff] [blame] | 39 | O << E.second->getName(); |
Manman Ren | d262004 | 2012-08-28 22:21:25 +0000 | [diff] [blame] | 40 | else |
| 41 | O << "0"; |
| 42 | |
| 43 | return O << ")"; |
| 44 | } |
| 45 | |
Manman Ren | d262004 | 2012-08-28 22:21:25 +0000 | [diff] [blame] | 46 | /// AddCounts - Add 'A' and 'B', accounting for the fact that the value of one |
| 47 | /// (or both) may not be defined. |
| 48 | static unsigned AddCounts(unsigned A, unsigned B) { |
| 49 | // If either value is undefined, use the other. |
| 50 | // Undefined + undefined = undefined. |
| 51 | if (A == ProfileDataLoader::Uncounted) return B; |
| 52 | if (B == ProfileDataLoader::Uncounted) return A; |
| 53 | |
| 54 | // Saturate to the maximum storable value. This could change taken/nottaken |
| 55 | // ratios, but is presumably better than wrapping and thus potentially |
| 56 | // inverting ratios. |
Bill Wendling | f91e400 | 2012-08-31 05:18:31 +0000 | [diff] [blame] | 57 | uint64_t tmp = (uint64_t)A + (uint64_t)B; |
| 58 | if (tmp > (uint64_t)ProfileDataLoader::MaxCount) |
Manman Ren | d262004 | 2012-08-28 22:21:25 +0000 | [diff] [blame] | 59 | tmp = ProfileDataLoader::MaxCount; |
| 60 | return (unsigned)tmp; |
| 61 | } |
| 62 | |
| 63 | /// ReadProfilingData - Load 'NumEntries' items of type 'T' from file 'F' |
Benjamin Kramer | cb5f63d | 2012-08-31 12:43:07 +0000 | [diff] [blame] | 64 | template <typename T> |
Manman Ren | d262004 | 2012-08-28 22:21:25 +0000 | [diff] [blame] | 65 | static void ReadProfilingData(const char *ToolName, FILE *F, |
Benjamin Kramer | cb5f63d | 2012-08-31 12:43:07 +0000 | [diff] [blame] | 66 | T *Data, size_t NumEntries) { |
Manman Ren | d262004 | 2012-08-28 22:21:25 +0000 | [diff] [blame] | 67 | // Read in the block of data... |
Benjamin Kramer | cb5f63d | 2012-08-31 12:43:07 +0000 | [diff] [blame] | 68 | if (fread(Data, sizeof(T), NumEntries, F) != NumEntries) |
| 69 | report_fatal_error(Twine(ToolName) + ": Profiling data truncated"); |
Manman Ren | d262004 | 2012-08-28 22:21:25 +0000 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | /// ReadProfilingNumEntries - Read how many entries are in this profiling data |
| 73 | /// packet. |
| 74 | static unsigned ReadProfilingNumEntries(const char *ToolName, FILE *F, |
| 75 | bool ShouldByteSwap) { |
Benjamin Kramer | cb5f63d | 2012-08-31 12:43:07 +0000 | [diff] [blame] | 76 | unsigned Entry; |
| 77 | ReadProfilingData<unsigned>(ToolName, F, &Entry, 1); |
| 78 | return ShouldByteSwap ? ByteSwap_32(Entry) : Entry; |
Manman Ren | d262004 | 2012-08-28 22:21:25 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | /// ReadProfilingBlock - Read the number of entries in the next profiling data |
| 82 | /// packet and then accumulate the entries into 'Data'. |
| 83 | static void ReadProfilingBlock(const char *ToolName, FILE *F, |
| 84 | bool ShouldByteSwap, |
Bill Wendling | f91e400 | 2012-08-31 05:18:31 +0000 | [diff] [blame] | 85 | SmallVector<unsigned, 32> &Data) { |
Manman Ren | d262004 | 2012-08-28 22:21:25 +0000 | [diff] [blame] | 86 | // Read the number of entries... |
| 87 | unsigned NumEntries = ReadProfilingNumEntries(ToolName, F, ShouldByteSwap); |
| 88 | |
| 89 | // Read in the data. |
Bill Wendling | f91e400 | 2012-08-31 05:18:31 +0000 | [diff] [blame] | 90 | SmallVector<unsigned, 8> TempSpace(NumEntries); |
Benjamin Kramer | cb5f63d | 2012-08-31 12:43:07 +0000 | [diff] [blame] | 91 | ReadProfilingData<unsigned>(ToolName, F, TempSpace.data(), NumEntries); |
Manman Ren | d262004 | 2012-08-28 22:21:25 +0000 | [diff] [blame] | 92 | |
| 93 | // Make sure we have enough space ... |
| 94 | if (Data.size() < NumEntries) |
| 95 | Data.resize(NumEntries, ProfileDataLoader::Uncounted); |
| 96 | |
| 97 | // Accumulate the data we just read into the existing data. |
Benjamin Kramer | cb5f63d | 2012-08-31 12:43:07 +0000 | [diff] [blame] | 98 | for (unsigned i = 0; i < NumEntries; ++i) { |
| 99 | unsigned Entry = ShouldByteSwap ? ByteSwap_32(TempSpace[i]) : TempSpace[i]; |
| 100 | Data[i] = AddCounts(Entry, Data[i]); |
| 101 | } |
Manman Ren | d262004 | 2012-08-28 22:21:25 +0000 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | /// ReadProfilingArgBlock - Read the command line arguments that the progam was |
| 105 | /// run with when the current profiling data packet(s) were generated. |
| 106 | static void ReadProfilingArgBlock(const char *ToolName, FILE *F, |
| 107 | bool ShouldByteSwap, |
Bill Wendling | f91e400 | 2012-08-31 05:18:31 +0000 | [diff] [blame] | 108 | SmallVector<std::string, 1> &CommandLines) { |
Manman Ren | d262004 | 2012-08-28 22:21:25 +0000 | [diff] [blame] | 109 | // Read the number of bytes ... |
| 110 | unsigned ArgLength = ReadProfilingNumEntries(ToolName, F, ShouldByteSwap); |
| 111 | |
| 112 | // Read in the arguments (if there are any to read). Round up the length to |
| 113 | // the nearest 4-byte multiple. |
Bill Wendling | f91e400 | 2012-08-31 05:18:31 +0000 | [diff] [blame] | 114 | SmallVector<char, 8> Args(ArgLength+4); |
Manman Ren | d262004 | 2012-08-28 22:21:25 +0000 | [diff] [blame] | 115 | if (ArgLength) |
Benjamin Kramer | cb5f63d | 2012-08-31 12:43:07 +0000 | [diff] [blame] | 116 | ReadProfilingData<char>(ToolName, F, Args.data(), (ArgLength+3) & ~3); |
Manman Ren | d262004 | 2012-08-28 22:21:25 +0000 | [diff] [blame] | 117 | |
| 118 | // Store the arguments. |
| 119 | CommandLines.push_back(std::string(&Args[0], &Args[ArgLength])); |
| 120 | } |
| 121 | |
| 122 | const unsigned ProfileDataLoader::Uncounted = ~0U; |
| 123 | const unsigned ProfileDataLoader::MaxCount = ~0U - 1U; |
| 124 | |
Bill Wendling | f91e400 | 2012-08-31 05:18:31 +0000 | [diff] [blame] | 125 | /// ProfileDataLoader ctor - Read the specified profiling data file, reporting |
| 126 | /// a fatal error if the file is invalid or broken. |
Manman Ren | d262004 | 2012-08-28 22:21:25 +0000 | [diff] [blame] | 127 | ProfileDataLoader::ProfileDataLoader(const char *ToolName, |
| 128 | const std::string &Filename) |
| 129 | : Filename(Filename) { |
| 130 | FILE *F = fopen(Filename.c_str(), "rb"); |
Bill Wendling | f91e400 | 2012-08-31 05:18:31 +0000 | [diff] [blame] | 131 | if (F == 0) |
Benjamin Kramer | cb5f63d | 2012-08-31 12:43:07 +0000 | [diff] [blame] | 132 | report_fatal_error(Twine(ToolName) + ": Error opening '" + |
Bill Wendling | f91e400 | 2012-08-31 05:18:31 +0000 | [diff] [blame] | 133 | Filename + "': "); |
Manman Ren | d262004 | 2012-08-28 22:21:25 +0000 | [diff] [blame] | 134 | |
| 135 | // Keep reading packets until we run out of them. |
| 136 | unsigned PacketType; |
| 137 | while (fread(&PacketType, sizeof(unsigned), 1, F) == 1) { |
| 138 | // If the low eight bits of the packet are zero, we must be dealing with an |
| 139 | // endianness mismatch. Byteswap all words read from the profiling |
| 140 | // information. This can happen when the compiler host and target have |
| 141 | // different endianness. |
| 142 | bool ShouldByteSwap = (char)PacketType == 0; |
Benjamin Kramer | cb5f63d | 2012-08-31 12:43:07 +0000 | [diff] [blame] | 143 | PacketType = ShouldByteSwap ? ByteSwap_32(PacketType) : PacketType; |
Manman Ren | d262004 | 2012-08-28 22:21:25 +0000 | [diff] [blame] | 144 | |
| 145 | switch (PacketType) { |
| 146 | case ArgumentInfo: |
| 147 | ReadProfilingArgBlock(ToolName, F, ShouldByteSwap, CommandLines); |
| 148 | break; |
| 149 | |
| 150 | case EdgeInfo: |
| 151 | ReadProfilingBlock(ToolName, F, ShouldByteSwap, EdgeCounts); |
| 152 | break; |
| 153 | |
| 154 | default: |
Bill Wendling | f91e400 | 2012-08-31 05:18:31 +0000 | [diff] [blame] | 155 | report_fatal_error(std::string(ToolName) |
| 156 | + ": Unknown profiling packet type"); |
| 157 | break; |
Manman Ren | d262004 | 2012-08-28 22:21:25 +0000 | [diff] [blame] | 158 | } |
| 159 | } |
| 160 | |
| 161 | fclose(F); |
| 162 | } |