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; |
Duncan Sands | 18f13c6 | 2009-09-06 12:56:52 +0000 | [diff] [blame] | 29 | return ((Var & (255U<< 0U)) << 24U) | |
| 30 | ((Var & (255U<< 8U)) << 8U) | |
| 31 | ((Var & (255U<<16U)) >> 8U) | |
| 32 | ((Var & (255U<<24U)) >> 24U); |
Chris Lattner | bc44aa6 | 2004-02-11 05:54:25 +0000 | [diff] [blame] | 33 | } |
| 34 | |
Andreas Neustifter | f0d568d | 2009-09-03 09:11:10 +0000 | [diff] [blame] | 35 | static unsigned AddCounts(unsigned A, unsigned B) { |
Andreas Neustifter | 4c2c533 | 2009-09-03 08:41:05 +0000 | [diff] [blame] | 36 | // If either value is undefined, use the other. |
Andreas Neustifter | 9233272 | 2009-09-16 11:35:50 +0000 | [diff] [blame] | 37 | if (A == ProfileInfoLoader::Uncounted) return B; |
| 38 | if (B == ProfileInfoLoader::Uncounted) return A; |
Andreas Neustifter | 4c2c533 | 2009-09-03 08:41:05 +0000 | [diff] [blame] | 39 | return A + B; |
| 40 | } |
| 41 | |
Chris Lattner | bc44aa6 | 2004-02-11 05:54:25 +0000 | [diff] [blame] | 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) { |
Chris Lattner | a81d29b | 2009-08-23 07:33:14 +0000 | [diff] [blame] | 48 | errs() << ToolName << ": data packet truncated!\n"; |
Chris Lattner | bc44aa6 | 2004-02-11 05:54:25 +0000 | [diff] [blame] | 49 | perror(0); |
| 50 | exit(1); |
| 51 | } |
| 52 | NumEntries = ByteSwap(NumEntries, ShouldByteSwap); |
| 53 | |
| 54 | // Read the counts... |
| 55 | std::vector<unsigned> TempSpace(NumEntries); |
| 56 | |
| 57 | // Read in the block of data... |
| 58 | if (fread(&TempSpace[0], sizeof(unsigned)*NumEntries, 1, F) != 1) { |
Chris Lattner | a81d29b | 2009-08-23 07:33:14 +0000 | [diff] [blame] | 59 | errs() << ToolName << ": data packet truncated!\n"; |
Chris Lattner | bc44aa6 | 2004-02-11 05:54:25 +0000 | [diff] [blame] | 60 | perror(0); |
| 61 | exit(1); |
| 62 | } |
| 63 | |
Andreas Neustifter | da5ea94 | 2009-09-01 19:08:51 +0000 | [diff] [blame] | 64 | // Make sure we have enough space... The space is initialised to -1 to |
| 65 | // facitiltate the loading of missing values for OptimalEdgeProfiling. |
Chris Lattner | bc44aa6 | 2004-02-11 05:54:25 +0000 | [diff] [blame] | 66 | if (Data.size() < NumEntries) |
Andreas Neustifter | 9233272 | 2009-09-16 11:35:50 +0000 | [diff] [blame] | 67 | Data.resize(NumEntries, ProfileInfoLoader::Uncounted); |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 68 | |
Chris Lattner | bc44aa6 | 2004-02-11 05:54:25 +0000 | [diff] [blame] | 69 | // Accumulate the data we just read into the data. |
| 70 | if (!ShouldByteSwap) { |
Andreas Neustifter | da5ea94 | 2009-09-01 19:08:51 +0000 | [diff] [blame] | 71 | for (unsigned i = 0; i != NumEntries; ++i) { |
Andreas Neustifter | 4c2c533 | 2009-09-03 08:41:05 +0000 | [diff] [blame] | 72 | Data[i] = AddCounts(TempSpace[i], Data[i]); |
Andreas Neustifter | da5ea94 | 2009-09-01 19:08:51 +0000 | [diff] [blame] | 73 | } |
Chris Lattner | bc44aa6 | 2004-02-11 05:54:25 +0000 | [diff] [blame] | 74 | } else { |
Andreas Neustifter | da5ea94 | 2009-09-01 19:08:51 +0000 | [diff] [blame] | 75 | for (unsigned i = 0; i != NumEntries; ++i) { |
Andreas Neustifter | 4c2c533 | 2009-09-03 08:41:05 +0000 | [diff] [blame] | 76 | Data[i] = AddCounts(ByteSwap(TempSpace[i], true), Data[i]); |
Andreas Neustifter | da5ea94 | 2009-09-01 19:08:51 +0000 | [diff] [blame] | 77 | } |
Chris Lattner | bc44aa6 | 2004-02-11 05:54:25 +0000 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | |
Andreas Neustifter | 9233272 | 2009-09-16 11:35:50 +0000 | [diff] [blame] | 81 | const unsigned ProfileInfoLoader::Uncounted = ~0U; |
| 82 | |
Chris Lattner | bc44aa6 | 2004-02-11 05:54:25 +0000 | [diff] [blame] | 83 | // ProfileInfoLoader ctor - Read the specified profiling data file, exiting the |
| 84 | // program if the file is invalid or broken. |
| 85 | // |
| 86 | ProfileInfoLoader::ProfileInfoLoader(const char *ToolName, |
| 87 | const std::string &Filename, |
Daniel Dunbar | ee16638 | 2009-08-05 15:55:56 +0000 | [diff] [blame] | 88 | Module &TheModule) : |
| 89 | Filename(Filename), |
| 90 | M(TheModule), Warned(false) { |
Andreas Neustifter | cf48efc | 2009-08-25 12:53:27 +0000 | [diff] [blame] | 91 | FILE *F = fopen(Filename.c_str(), "rb"); |
Chris Lattner | bc44aa6 | 2004-02-11 05:54:25 +0000 | [diff] [blame] | 92 | if (F == 0) { |
Chris Lattner | a81d29b | 2009-08-23 07:33:14 +0000 | [diff] [blame] | 93 | errs() << ToolName << ": Error opening '" << Filename << "': "; |
Chris Lattner | bc44aa6 | 2004-02-11 05:54:25 +0000 | [diff] [blame] | 94 | perror(0); |
| 95 | exit(1); |
| 96 | } |
| 97 | |
| 98 | // Keep reading packets until we run out of them. |
| 99 | unsigned PacketType; |
| 100 | while (fread(&PacketType, sizeof(unsigned), 1, F) == 1) { |
| 101 | // If the low eight bits of the packet are zero, we must be dealing with an |
| 102 | // endianness mismatch. Byteswap all words read from the profiling |
| 103 | // information. |
| 104 | bool ShouldByteSwap = (char)PacketType == 0; |
| 105 | PacketType = ByteSwap(PacketType, ShouldByteSwap); |
| 106 | |
| 107 | switch (PacketType) { |
| 108 | case ArgumentInfo: { |
| 109 | unsigned ArgLength; |
| 110 | if (fread(&ArgLength, sizeof(unsigned), 1, F) != 1) { |
Chris Lattner | a81d29b | 2009-08-23 07:33:14 +0000 | [diff] [blame] | 111 | errs() << ToolName << ": arguments packet truncated!\n"; |
Chris Lattner | bc44aa6 | 2004-02-11 05:54:25 +0000 | [diff] [blame] | 112 | perror(0); |
| 113 | exit(1); |
| 114 | } |
| 115 | ArgLength = ByteSwap(ArgLength, ShouldByteSwap); |
| 116 | |
| 117 | // Read in the arguments... |
| 118 | std::vector<char> Chars(ArgLength+4); |
| 119 | |
| 120 | if (ArgLength) |
| 121 | if (fread(&Chars[0], (ArgLength+3) & ~3, 1, F) != 1) { |
Chris Lattner | a81d29b | 2009-08-23 07:33:14 +0000 | [diff] [blame] | 122 | errs() << ToolName << ": arguments packet truncated!\n"; |
Chris Lattner | bc44aa6 | 2004-02-11 05:54:25 +0000 | [diff] [blame] | 123 | perror(0); |
| 124 | exit(1); |
| 125 | } |
| 126 | CommandLines.push_back(std::string(&Chars[0], &Chars[ArgLength])); |
| 127 | break; |
| 128 | } |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 129 | |
Chris Lattner | bc44aa6 | 2004-02-11 05:54:25 +0000 | [diff] [blame] | 130 | case FunctionInfo: |
| 131 | ReadProfilingBlock(ToolName, F, ShouldByteSwap, FunctionCounts); |
| 132 | break; |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 133 | |
Chris Lattner | bc44aa6 | 2004-02-11 05:54:25 +0000 | [diff] [blame] | 134 | case BlockInfo: |
| 135 | ReadProfilingBlock(ToolName, F, ShouldByteSwap, BlockCounts); |
| 136 | break; |
| 137 | |
Chris Lattner | 01945c1 | 2004-03-08 18:20:18 +0000 | [diff] [blame] | 138 | case EdgeInfo: |
| 139 | ReadProfilingBlock(ToolName, F, ShouldByteSwap, EdgeCounts); |
| 140 | break; |
| 141 | |
Andreas Neustifter | da5ea94 | 2009-09-01 19:08:51 +0000 | [diff] [blame] | 142 | case OptEdgeInfo: |
| 143 | ReadProfilingBlock(ToolName, F, ShouldByteSwap, OptimalEdgeCounts); |
| 144 | break; |
| 145 | |
Brian Gaeke | b171d79 | 2004-05-04 17:11:14 +0000 | [diff] [blame] | 146 | case BBTraceInfo: |
| 147 | ReadProfilingBlock(ToolName, F, ShouldByteSwap, BBTrace); |
| 148 | break; |
| 149 | |
Chris Lattner | bc44aa6 | 2004-02-11 05:54:25 +0000 | [diff] [blame] | 150 | default: |
Chris Lattner | a81d29b | 2009-08-23 07:33:14 +0000 | [diff] [blame] | 151 | errs() << ToolName << ": Unknown packet type #" << PacketType << "!\n"; |
Chris Lattner | bc44aa6 | 2004-02-11 05:54:25 +0000 | [diff] [blame] | 152 | exit(1); |
| 153 | } |
| 154 | } |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 155 | |
Chris Lattner | bc44aa6 | 2004-02-11 05:54:25 +0000 | [diff] [blame] | 156 | fclose(F); |
| 157 | } |
| 158 | |