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