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