Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 1 | //===- AnalyzerWrappers.cpp - Analyze bytecode from file or buffer -------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by Reid Spencer and is distributed under the |
| 6 | // University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements loading and analysis of a bytecode file and analyzing a |
| 11 | // bytecode buffer. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "llvm/Bytecode/Analyzer.h" |
| 16 | #include "AnalyzerInternals.h" |
| 17 | #include "Support/FileUtilities.h" |
| 18 | #include "Support/StringExtras.h" |
| 19 | #include "Config/unistd.h" |
| 20 | #include <cerrno> |
Reid Spencer | 00c28a7 | 2004-06-10 08:09:13 +0000 | [diff] [blame^] | 21 | #include <iomanip> |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 22 | |
| 23 | using namespace llvm; |
| 24 | |
| 25 | //===----------------------------------------------------------------------===// |
| 26 | // BytecodeFileAnalyzer - Analyze from an mmap'able file descriptor. |
| 27 | // |
| 28 | |
| 29 | namespace { |
| 30 | /// BytecodeFileAnalyzer - parses a bytecode file from a file |
| 31 | class BytecodeFileAnalyzer : public BytecodeAnalyzer { |
| 32 | private: |
| 33 | unsigned char *Buffer; |
| 34 | unsigned Length; |
| 35 | |
| 36 | BytecodeFileAnalyzer(const BytecodeFileAnalyzer&); // Do not implement |
| 37 | void operator=(const BytecodeFileAnalyzer &BFR); // Do not implement |
| 38 | |
| 39 | public: |
| 40 | BytecodeFileAnalyzer(const std::string &Filename, BytecodeAnalysis& bca); |
| 41 | ~BytecodeFileAnalyzer(); |
| 42 | }; |
| 43 | } |
| 44 | |
| 45 | static std::string ErrnoMessage (int savedErrNum, std::string descr) { |
| 46 | return ::strerror(savedErrNum) + std::string(", while trying to ") + descr; |
| 47 | } |
| 48 | |
| 49 | BytecodeFileAnalyzer::BytecodeFileAnalyzer(const std::string &Filename, |
Reid Spencer | 00c28a7 | 2004-06-10 08:09:13 +0000 | [diff] [blame^] | 50 | BytecodeAnalysis& bca) { |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 51 | Buffer = (unsigned char*)ReadFileIntoAddressSpace(Filename, Length); |
| 52 | if (Buffer == 0) |
| 53 | throw "Error reading file '" + Filename + "'."; |
| 54 | |
| 55 | try { |
| 56 | // Parse the bytecode we mmapped in |
| 57 | if ( bca.dumpBytecode ) |
| 58 | DumpBytecode(Buffer, Length, bca, Filename); |
| 59 | AnalyzeBytecode(Buffer, Length, bca, Filename); |
| 60 | } catch (...) { |
| 61 | UnmapFileFromAddressSpace(Buffer, Length); |
| 62 | throw; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | BytecodeFileAnalyzer::~BytecodeFileAnalyzer() { |
| 67 | // Unmmap the bytecode... |
| 68 | UnmapFileFromAddressSpace(Buffer, Length); |
| 69 | } |
| 70 | |
| 71 | //===----------------------------------------------------------------------===// |
| 72 | // BytecodeBufferAnalyzer - Read from a memory buffer |
| 73 | // |
| 74 | |
| 75 | namespace { |
| 76 | /// BytecodeBufferAnalyzer - parses a bytecode file from a buffer |
| 77 | /// |
| 78 | class BytecodeBufferAnalyzer : public BytecodeAnalyzer { |
| 79 | private: |
| 80 | const unsigned char *Buffer; |
| 81 | bool MustDelete; |
| 82 | |
| 83 | BytecodeBufferAnalyzer(const BytecodeBufferAnalyzer&); // Do not implement |
| 84 | void operator=(const BytecodeBufferAnalyzer &BFR); // Do not implement |
| 85 | |
| 86 | public: |
| 87 | BytecodeBufferAnalyzer(const unsigned char *Buf, unsigned Length, |
Reid Spencer | 00c28a7 | 2004-06-10 08:09:13 +0000 | [diff] [blame^] | 88 | BytecodeAnalysis& bca, const std::string &ModuleID); |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 89 | ~BytecodeBufferAnalyzer(); |
| 90 | |
| 91 | }; |
| 92 | } |
| 93 | |
| 94 | BytecodeBufferAnalyzer::BytecodeBufferAnalyzer(const unsigned char *Buf, |
Reid Spencer | 00c28a7 | 2004-06-10 08:09:13 +0000 | [diff] [blame^] | 95 | unsigned Length, |
| 96 | BytecodeAnalysis& bca, |
| 97 | const std::string &ModuleID) { |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 98 | // If not aligned, allocate a new buffer to hold the bytecode... |
| 99 | const unsigned char *ParseBegin = 0; |
| 100 | if ((intptr_t)Buf & 3) { |
| 101 | Buffer = new unsigned char[Length+4]; |
| 102 | unsigned Offset = 4 - ((intptr_t)Buffer & 3); // Make sure it's aligned |
| 103 | ParseBegin = Buffer + Offset; |
| 104 | memcpy((unsigned char*)ParseBegin, Buf, Length); // Copy it over |
| 105 | MustDelete = true; |
| 106 | } else { |
| 107 | // If we don't need to copy it over, just use the caller's copy |
| 108 | ParseBegin = Buffer = Buf; |
| 109 | MustDelete = false; |
| 110 | } |
| 111 | try { |
| 112 | if ( bca.dumpBytecode ) |
| 113 | DumpBytecode(ParseBegin, Length, bca, ModuleID); |
| 114 | AnalyzeBytecode(ParseBegin, Length, bca, ModuleID); |
| 115 | } catch (...) { |
| 116 | if (MustDelete) delete [] Buffer; |
| 117 | throw; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | BytecodeBufferAnalyzer::~BytecodeBufferAnalyzer() { |
| 122 | if (MustDelete) delete [] Buffer; |
| 123 | } |
| 124 | |
| 125 | //===----------------------------------------------------------------------===// |
| 126 | // BytecodeStdinAnalyzer - Read bytecode from Standard Input |
| 127 | // |
| 128 | |
| 129 | namespace { |
| 130 | /// BytecodeStdinAnalyzer - parses a bytecode file from stdin |
| 131 | /// |
| 132 | class BytecodeStdinAnalyzer : public BytecodeAnalyzer { |
| 133 | private: |
| 134 | std::vector<unsigned char> FileData; |
| 135 | unsigned char *FileBuf; |
| 136 | |
| 137 | BytecodeStdinAnalyzer(const BytecodeStdinAnalyzer&); // Do not implement |
| 138 | void operator=(const BytecodeStdinAnalyzer &BFR); // Do not implement |
| 139 | |
| 140 | public: |
| 141 | BytecodeStdinAnalyzer(BytecodeAnalysis& bca); |
| 142 | }; |
| 143 | } |
| 144 | |
| 145 | BytecodeStdinAnalyzer::BytecodeStdinAnalyzer(BytecodeAnalysis& bca ) { |
| 146 | int BlockSize; |
| 147 | unsigned char Buffer[4096*4]; |
| 148 | |
| 149 | // Read in all of the data from stdin, we cannot mmap stdin... |
| 150 | while ((BlockSize = ::read(0 /*stdin*/, Buffer, 4096*4))) { |
| 151 | if (BlockSize == -1) |
| 152 | throw ErrnoMessage(errno, "read from standard input"); |
| 153 | |
| 154 | FileData.insert(FileData.end(), Buffer, Buffer+BlockSize); |
| 155 | } |
| 156 | |
| 157 | if (FileData.empty()) |
| 158 | throw std::string("Standard Input empty!"); |
| 159 | |
| 160 | FileBuf = &FileData[0]; |
| 161 | if (bca.dumpBytecode) |
| 162 | DumpBytecode(&FileData[0], FileData.size(), bca, "<stdin>"); |
| 163 | AnalyzeBytecode(FileBuf, FileData.size(), bca, "<stdin>"); |
| 164 | } |
| 165 | |
| 166 | //===----------------------------------------------------------------------===// |
| 167 | // Wrapper functions |
| 168 | //===----------------------------------------------------------------------===// |
| 169 | |
| 170 | // AnalyzeBytecodeFile - analyze one file |
| 171 | void llvm::AnalyzeBytecodeFile(const std::string &Filename, |
| 172 | BytecodeAnalysis& bca, |
| 173 | std::string *ErrorStr) |
| 174 | { |
| 175 | try { |
| 176 | if ( Filename != "-" ) |
| 177 | BytecodeFileAnalyzer bfa(Filename,bca); |
| 178 | else |
| 179 | BytecodeStdinAnalyzer bsa(bca); |
| 180 | } catch (std::string &err) { |
| 181 | if (ErrorStr) *ErrorStr = err; |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | // AnalyzeBytecodeBuffer - analyze a buffer |
| 186 | void llvm::AnalyzeBytecodeBuffer( |
| 187 | const unsigned char* Buffer, ///< Pointer to start of bytecode buffer |
| 188 | unsigned BufferSize, ///< Size of the bytecode buffer |
| 189 | BytecodeAnalysis& Results, ///< The results of the analysis |
| 190 | std::string* ErrorStr ///< Errors, if any. |
| 191 | ) |
| 192 | { |
| 193 | try { |
| 194 | BytecodeBufferAnalyzer(Buffer, BufferSize, Results, "<buffer>" ); |
| 195 | } catch (std::string& err ) { |
| 196 | if ( ErrorStr) *ErrorStr = err; |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | |
| 201 | /// This function prints the contents of rhe BytecodeAnalysis structure in |
| 202 | /// a human legible form. |
| 203 | /// @brief Print BytecodeAnalysis structure to an ostream |
Reid Spencer | 00c28a7 | 2004-06-10 08:09:13 +0000 | [diff] [blame^] | 204 | namespace { |
| 205 | inline static void print(std::ostream& Out, const char*title, |
| 206 | unsigned val, bool nl = true ) { |
| 207 | Out << std::setw(30) << std::right << title |
| 208 | << std::setw(0) << ": " |
| 209 | << std::setw(9) << val << "\n"; |
| 210 | } |
| 211 | |
| 212 | inline static void print(std::ostream&Out, const char*title, |
| 213 | double val ) { |
| 214 | Out << std::setw(30) << std::right << title |
| 215 | << std::setw(0) << ": " |
| 216 | << std::setw(9) << std::setprecision(6) << val << "\n" ; |
| 217 | } |
| 218 | |
| 219 | inline static void print(std::ostream&Out, const char*title, |
| 220 | double top, double bot ) { |
| 221 | Out << std::setw(30) << std::right << title |
| 222 | << std::setw(0) << ": " |
| 223 | << std::setw(9) << std::setprecision(6) << top |
| 224 | << " (" << std::left << std::setw(0) << std::setprecision(4) |
| 225 | << (top/bot)*100.0 << "%)\n"; |
| 226 | } |
| 227 | inline static void print(std::ostream&Out, const char*title, |
| 228 | std::string val, bool nl = true) { |
| 229 | Out << std::setw(30) << std::right << title |
| 230 | << std::setw(0) << ": " |
| 231 | << std::left << val << (nl ? "\n" : ""); |
| 232 | } |
| 233 | |
| 234 | } |
| 235 | |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 236 | void llvm::PrintBytecodeAnalysis(BytecodeAnalysis& bca, std::ostream& Out ) |
| 237 | { |
Reid Spencer | 00c28a7 | 2004-06-10 08:09:13 +0000 | [diff] [blame^] | 238 | print(Out, "Bytecode Analysis Of Module", bca.ModuleId); |
| 239 | print(Out, "File Size", bca.byteSize); |
| 240 | print(Out, "Bytecode Compression Index",std::string("TBD")); |
| 241 | print(Out, "Number Of Bytecode Blocks", bca.numBlocks); |
| 242 | print(Out, "Number Of Types", bca.numTypes); |
| 243 | print(Out, "Number Of Values", bca.numValues); |
| 244 | print(Out, "Number Of Constants", bca.numConstants); |
| 245 | print(Out, "Number Of Global Variables", bca.numGlobalVars); |
| 246 | print(Out, "Number Of Functions", bca.numFunctions); |
| 247 | print(Out, "Number Of Basic Blocks", bca.numBasicBlocks); |
| 248 | print(Out, "Number Of Instructions", bca.numInstructions); |
| 249 | print(Out, "Number Of Operands", bca.numOperands); |
| 250 | print(Out, "Number Of Compaction Tables", bca.numCmpctnTables); |
| 251 | print(Out, "Number Of Symbol Tables", bca.numSymTab); |
| 252 | print(Out, "Maximum Type Slot Number", bca.maxTypeSlot); |
| 253 | print(Out, "Maximum Value Slot Number", bca.maxValueSlot); |
| 254 | print(Out, "Bytes Thrown To Alignment", double(bca.numAlignment), |
| 255 | double(bca.byteSize)); |
| 256 | print(Out, "File Density (bytes/def)", bca.fileDensity); |
| 257 | print(Out, "Globals Density (bytes/def)", bca.globalsDensity); |
| 258 | print(Out, "Function Density (bytes/func)", bca.functionDensity); |
| 259 | print(Out, "Number of VBR 32-bit Integers", bca.vbrCount32); |
| 260 | print(Out, "Number of VBR 64-bit Integers", bca.vbrCount64); |
| 261 | print(Out, "Number of VBR Compressed Bytes", bca.vbrCompBytes); |
| 262 | print(Out, "Number of VBR Expanded Bytes", bca.vbrExpdBytes); |
| 263 | print(Out, "VBR Savings", |
| 264 | double(bca.vbrExpdBytes)-double(bca.vbrCompBytes), |
| 265 | double(bca.byteSize)); |
Reid Spencer | edc3b58 | 2004-06-09 06:17:58 +0000 | [diff] [blame] | 266 | |
Reid Spencer | 00c28a7 | 2004-06-10 08:09:13 +0000 | [diff] [blame^] | 267 | if ( bca.detailedResults ) { |
| 268 | print(Out, "Module Bytes", |
| 269 | double(bca.BlockSizes[BytecodeFormat::Module]), |
| 270 | double(bca.byteSize)); |
| 271 | print(Out, "Function Bytes", |
| 272 | double(bca.BlockSizes[BytecodeFormat::Function]), |
| 273 | double(bca.byteSize)); |
| 274 | print(Out, "Constant Pool Bytes", |
| 275 | double(bca.BlockSizes[BytecodeFormat::ConstantPool]), |
| 276 | double(bca.byteSize)); |
| 277 | print(Out, "Symbol Table Bytes", |
| 278 | double(bca.BlockSizes[BytecodeFormat::SymbolTable]), |
| 279 | double(bca.byteSize)); |
| 280 | print(Out, "Module Global Info Bytes", |
| 281 | double(bca.BlockSizes[BytecodeFormat::ModuleGlobalInfo]), |
| 282 | double(bca.byteSize)); |
| 283 | print(Out, "Global Type Plane Bytes", |
| 284 | double(bca.BlockSizes[BytecodeFormat::GlobalTypePlane]), |
| 285 | double(bca.byteSize)); |
| 286 | print(Out, "Basic Block Bytes", |
| 287 | double(bca.BlockSizes[BytecodeFormat::BasicBlock]), |
| 288 | double(bca.byteSize)); |
| 289 | print(Out, "Instruction List Bytes", |
| 290 | double(bca.BlockSizes[BytecodeFormat::InstructionList]), |
| 291 | double(bca.byteSize)); |
| 292 | print(Out, "Compaction Table Bytes", |
| 293 | double(bca.BlockSizes[BytecodeFormat::CompactionTable]), |
| 294 | double(bca.byteSize)); |
| 295 | |
| 296 | std::map<unsigned,BytecodeAnalysis::BytecodeFunctionInfo>::iterator I = |
| 297 | bca.FunctionInfo.begin(); |
| 298 | std::map<unsigned,BytecodeAnalysis::BytecodeFunctionInfo>::iterator E = |
| 299 | bca.FunctionInfo.end(); |
| 300 | |
| 301 | while ( I != E ) { |
| 302 | Out << std::left << std::setw(0); |
| 303 | Out << "Function: " << I->second.name << " Slot=" << I->first << "\n"; |
| 304 | print(Out,"Type:", I->second.description); |
| 305 | print(Out,"Byte Size", I->second.byteSize); |
| 306 | print(Out,"Instructions", I->second.numInstructions); |
| 307 | print(Out,"Basic Blocks", I->second.numBasicBlocks); |
| 308 | print(Out,"Operand", I->second.numOperands); |
| 309 | print(Out,"Function Density", I->second.density); |
| 310 | print(Out,"VBR Effectiveness", I->second.vbrEffectiveness); |
| 311 | ++I; |
| 312 | } |
| 313 | } |
Reid Spencer | edc3b58 | 2004-06-09 06:17:58 +0000 | [diff] [blame] | 314 | |
| 315 | if ( bca.dumpBytecode ) |
| 316 | Out << bca.BytecodeDump; |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 317 | } |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 318 | // vim: sw=2 |