LLVM API Documentation
00001 //===-- llvm/Bytecode/Analyzer.h - Analyzer for Bytecode files --*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file was developed by Reid Spencer and is distributed under the 00006 // University of Illinois Open Source License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This functionality is implemented by the lib/Bytecode/Reader library. 00011 // It is used to read VM bytecode files from a file or memory buffer 00012 // and print out a diagnostic analysis of the contents of the file. It is 00013 // intended for three uses: (a) understanding the bytecode format, (b) ensuring 00014 // correctness of bytecode format, (c) statistical analysis of generated 00015 // bytecode files. 00016 // 00017 //===----------------------------------------------------------------------===// 00018 00019 #ifndef LLVM_BYTECODE_ANALYZER_H 00020 #define LLVM_BYTECODE_ANALYZER_H 00021 00022 #include "llvm/Bytecode/Format.h" 00023 #include <string> 00024 #include <map> 00025 #include <iostream> 00026 00027 namespace llvm { 00028 00029 // Forward declarations 00030 class Function; 00031 class Module; 00032 00033 /// This structure is used to contain the output of the Bytecode Analysis 00034 /// library. It simply contains fields to hold each item of the analysis 00035 /// results. 00036 /// @brief Bytecode Analysis results structure 00037 struct BytecodeAnalysis { 00038 std::string ModuleId; ///< Identification of the module 00039 unsigned version; ///< The version number of the bytecode file 00040 unsigned byteSize; ///< The size of the bytecode file in bytes 00041 unsigned numTypes; ///< The number of types 00042 unsigned numValues; ///< The number of values 00043 unsigned numBlocks; ///< The number of *bytecode* blocks 00044 unsigned numFunctions; ///< The number of functions defined 00045 unsigned numConstants; ///< The number of constants 00046 unsigned numGlobalVars; ///< The number of global variables 00047 unsigned numInstructions; ///< The number of instructions in all functions 00048 unsigned numBasicBlocks; ///< The number of BBs in all functions 00049 unsigned numOperands; ///< The number of BBs in all functions 00050 unsigned numCmpctnTables; ///< The number of compaction tables 00051 unsigned numSymTab; ///< The number of symbol tables 00052 unsigned numAlignment; ///< The number of alignment bytes 00053 unsigned numLibraries; ///< The number of dependent libraries 00054 unsigned libSize; ///< Number of bytes taken by dep libs. 00055 unsigned maxTypeSlot; ///< The maximum slot number for types 00056 unsigned maxValueSlot; ///< The maximum slot number for values 00057 double fileDensity; ///< Density of file (bytes/definition) 00058 ///< This is the density of the bytecode file. It is the ratio of 00059 ///< the number of bytes to the number of definitions in the file. Smaller 00060 ///< numbers mean the file is more compact (denser). Larger numbers mean 00061 ///< the file is more sparse. 00062 double globalsDensity; ///< density of global defs (bytes/definition) 00063 double functionDensity; ///< Average density of functions (bytes/function) 00064 unsigned instructionSize; ///< Size of instructions in bytes 00065 unsigned longInstructions;///< Number of instructions > 4 bytes 00066 unsigned vbrCount32; ///< Number of 32-bit vbr values 00067 unsigned vbrCount64; ///< Number of 64-bit vbr values 00068 unsigned vbrCompBytes; ///< Number of vbr bytes (compressed) 00069 unsigned vbrExpdBytes; ///< Number of vbr bytes (expanded) 00070 00071 typedef std::map<BytecodeFormat::CompressedBytecodeBlockIdentifiers,unsigned> 00072 BlockSizeMap; 00073 BlockSizeMap BlockSizes; 00074 00075 /// A structure that contains various pieces of information related to 00076 /// an analysis of a single function. 00077 struct BytecodeFunctionInfo { 00078 std::string description; ///< Function type description 00079 std::string name; ///< Name of function if it has one 00080 unsigned byteSize; ///< The size of the function in bytecode bytes 00081 unsigned numInstructions; ///< The number of instructions in the function 00082 unsigned numBasicBlocks; ///< The number of basic blocks in the function 00083 unsigned numPhis; ///< Number of Phi Nodes in Instructions 00084 unsigned numOperands; ///< The number of operands in the function 00085 double density; ///< Density of function 00086 unsigned instructionSize; ///< Size of instructions in bytes 00087 unsigned longInstructions;///< Number of instructions > 4 bytes 00088 unsigned vbrCount32; ///< Number of 32-bit vbr values 00089 unsigned vbrCount64; ///< Number of 64-bit vbr values 00090 unsigned vbrCompBytes; ///< Number of vbr bytes (compressed) 00091 unsigned vbrExpdBytes; ///< Number of vbr bytes (expanded) 00092 }; 00093 00094 /// A mapping of function slot numbers to the collected information about 00095 /// the function. 00096 std::map<const Function*,BytecodeFunctionInfo> FunctionInfo; 00097 00098 /// The content of the progressive verification 00099 std::string VerifyInfo; 00100 00101 /// Flags for what should be done 00102 bool detailedResults; ///< If true, FunctionInfo has contents 00103 bool progressiveVerify; ///< If true, VerifyInfo has contents 00104 }; 00105 00106 /// This function is the main entry point into the bytecode analysis library. It 00107 /// allows you to simply provide a \p filename and storage for the \p Results 00108 /// that will be filled in with the analysis results. 00109 /// @brief Analyze contents of a bytecode File 00110 Module* AnalyzeBytecodeFile( 00111 const std::string& Filename, ///< The name of the bytecode file to read 00112 BytecodeAnalysis& Results, ///< The results of the analysis 00113 std::string* ErrorStr = 0, ///< Errors, if any. 00114 std::ostream* output = 0 ///< Stream for dump output, if wanted 00115 ); 00116 00117 /// This function is an alternate entry point into the bytecode analysis 00118 /// library. It allows you to provide an arbitrary memory buffer which is 00119 /// assumed to contain a complete bytecode file. The \p Buffer is analyzed and 00120 /// the \p Results are filled in. 00121 /// @brief Analyze contents of a bytecode buffer. 00122 Module* AnalyzeBytecodeBuffer( 00123 const unsigned char* Buffer, ///< Pointer to start of bytecode buffer 00124 unsigned BufferSize, ///< Size of the bytecode buffer 00125 const std::string& ModuleID, ///< Identifier for the module 00126 BytecodeAnalysis& Results, ///< The results of the analysis 00127 std::string* ErrorStr = 0, ///< Errors, if any. 00128 std::ostream* output = 0 ///< Stream for dump output, if wanted 00129 ); 00130 00131 /// This function prints the contents of rhe BytecodeAnalysis structure in 00132 /// a human legible form. 00133 /// @brief Print BytecodeAnalysis structure to an ostream 00134 void PrintBytecodeAnalysis(BytecodeAnalysis& bca, std::ostream& Out ); 00135 00136 /// @brief std::ostream inserter for BytecodeAnalysis structure 00137 inline std::ostream& operator<<(std::ostream& Out, BytecodeAnalysis& bca ) { 00138 PrintBytecodeAnalysis(bca,Out); 00139 return Out; 00140 } 00141 00142 } // End llvm namespace 00143 00144 #endif