1 //===-- llvm/Bytecode/Analyzer.h - Analyzer for Bytecode files --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This functionality is implemented by the lib/Bytecode/Reader library.
11 // It is used to read VM bytecode files from a file or memory buffer
12 // and print out a diagnostic analysis of the contents of the file. It is
13 // intended for three uses: (a) understanding the bytecode format, (b) ensuring
14 // correctness of bytecode format, (c) statistical analysis of generated
17 //===----------------------------------------------------------------------===//
19 #ifndef LLVM_BYTECODE_ANALYZER_H
20 #define LLVM_BYTECODE_ANALYZER_H
22 #include "llvm/Bytecode/Format.h"
29 // Forward declarations
33 /// This structure is used to contain the output of the Bytecode Analysis
34 /// library. It simply contains fields to hold each item of the analysis
36 /// @brief Bytecode Analysis results structure
37 struct BytecodeAnalysis {
38 std::string ModuleId; ///< Identification of the module
39 unsigned version; ///< The version number of the bytecode file
40 unsigned byteSize; ///< The size of the bytecode file in bytes
41 unsigned numTypes; ///< The number of types
42 unsigned numValues; ///< The number of values
43 unsigned numBlocks; ///< The number of *bytecode* blocks
44 unsigned numFunctions; ///< The number of functions defined
45 unsigned numConstants; ///< The number of constants
46 unsigned numGlobalVars; ///< The number of global variables
47 unsigned numInstructions; ///< The number of instructions in all functions
48 unsigned numBasicBlocks; ///< The number of BBs in all functions
49 unsigned numOperands; ///< The number of BBs in all functions
50 unsigned numCmpctnTables; ///< The number of compaction tables
51 unsigned numSymTab; ///< The number of symbol tables
52 unsigned numAlignment; ///< The number of alignment bytes
53 unsigned numLibraries; ///< The number of dependent libraries
54 unsigned libSize; ///< Number of bytes taken by dep libs.
55 unsigned maxTypeSlot; ///< The maximum slot number for types
56 unsigned maxValueSlot; ///< The maximum slot number for values
57 double fileDensity; ///< Density of file (bytes/definition)
58 ///< This is the density of the bytecode file. It is the ratio of
59 ///< the number of bytes to the number of definitions in the file. Smaller
60 ///< numbers mean the file is more compact (denser). Larger numbers mean
61 ///< the file is more sparse.
62 double globalsDensity; ///< density of global defs (bytes/definition)
63 double functionDensity; ///< Average density of functions (bytes/function)
64 unsigned instructionSize; ///< Size of instructions in bytes
65 unsigned longInstructions;///< Number of instructions > 4 bytes
67 typedef std::map<BytecodeFormat::BytecodeBlockIdentifiers,unsigned>
69 BlockSizeMap BlockSizes;
71 /// A structure that contains various pieces of information related to
72 /// an analysis of a single function.
73 struct BytecodeFunctionInfo {
74 std::string description; ///< Function type description
75 std::string name; ///< Name of function if it has one
76 unsigned byteSize; ///< The size of the function in bytecode bytes
77 unsigned numInstructions; ///< The number of instructions in the function
78 unsigned numBasicBlocks; ///< The number of basic blocks in the function
79 unsigned numPhis; ///< Number of Phi Nodes in Instructions
80 unsigned numOperands; ///< The number of operands in the function
81 double density; ///< Density of function
82 unsigned instructionSize; ///< Size of instructions in bytes
83 unsigned longInstructions;///< Number of instructions > 4 bytes
86 /// A mapping of function slot numbers to the collected information about
88 std::map<const Function*,BytecodeFunctionInfo> FunctionInfo;
90 /// The content of the progressive verification
91 std::string VerifyInfo;
93 /// Flags for what should be done
94 bool detailedResults; ///< If true, FunctionInfo has contents
95 bool progressiveVerify; ///< If true, VerifyInfo has contents
98 /// This function is the main entry point into the bytecode analysis library. It
99 /// allows you to simply provide a \p filename and storage for the \p Results
100 /// that will be filled in with the analysis results.
101 /// @brief Analyze contents of a bytecode File
102 Module* AnalyzeBytecodeFile(
103 const std::string& Filename, ///< The name of the bytecode file to read
104 BytecodeAnalysis& Results, ///< The results of the analysis
105 std::string* ErrorStr = 0, ///< Errors, if any.
106 std::ostream* output = 0 ///< Stream for dump output, if wanted
109 /// This function is an alternate entry point into the bytecode analysis
110 /// library. It allows you to provide an arbitrary memory buffer which is
111 /// assumed to contain a complete bytecode file. The \p Buffer is analyzed and
112 /// the \p Results are filled in.
113 /// @brief Analyze contents of a bytecode buffer.
114 Module* AnalyzeBytecodeBuffer(
115 const unsigned char* Buffer, ///< Pointer to start of bytecode buffer
116 unsigned BufferSize, ///< Size of the bytecode buffer
117 const std::string& ModuleID, ///< Identifier for the module
118 BytecodeAnalysis& Results, ///< The results of the analysis
119 std::string* ErrorStr = 0, ///< Errors, if any.
120 std::ostream* output = 0 ///< Stream for dump output, if wanted
123 /// This function prints the contents of rhe BytecodeAnalysis structure in
124 /// a human legible form.
125 /// @brief Print BytecodeAnalysis structure to an ostream
126 void PrintBytecodeAnalysis(BytecodeAnalysis& bca, std::ostream& Out );
128 /// @brief std::ostream inserter for BytecodeAnalysis structure
129 inline std::ostream& operator<<(std::ostream& Out, BytecodeAnalysis& bca ) {
130 PrintBytecodeAnalysis(bca,Out);
134 } // End llvm namespace