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"
28 // Forward declarations
32 /// This structure is used to contain the output of the Bytecode Analysis
33 /// library. It simply contains fields to hold each item of the analysis
35 /// @brief Bytecode Analysis results structure
36 struct BytecodeAnalysis {
37 std::string ModuleId; ///< Identification of the module
38 unsigned version; ///< The version number of the bytecode file
39 unsigned byteSize; ///< The size of the bytecode file in bytes
40 unsigned numTypes; ///< The number of types
41 unsigned numValues; ///< The number of values
42 unsigned numBlocks; ///< The number of *bytecode* blocks
43 unsigned numFunctions; ///< The number of functions defined
44 unsigned numConstants; ///< The number of constants
45 unsigned numGlobalVars; ///< The number of global variables
46 unsigned numInstructions; ///< The number of instructions in all functions
47 unsigned numBasicBlocks; ///< The number of BBs in all functions
48 unsigned numOperands; ///< The number of BBs in all functions
49 unsigned numCmpctnTables; ///< The number of compaction tables
50 unsigned numSymTab; ///< The number of symbol tables
51 unsigned numAlignment; ///< The number of alignment bytes
52 unsigned numLibraries; ///< The number of dependent libraries
53 unsigned libSize; ///< Number of bytes taken by dep libs.
54 unsigned maxTypeSlot; ///< The maximum slot number for types
55 unsigned maxValueSlot; ///< The maximum slot number for values
56 double fileDensity; ///< Density of file (bytes/definition)
57 ///< This is the density of the bytecode file. It is the ratio of
58 ///< the number of bytes to the number of definitions in the file. Smaller
59 ///< numbers mean the file is more compact (denser). Larger numbers mean
60 ///< the file is more sparse.
61 double globalsDensity; ///< density of global defs (bytes/definition)
62 double functionDensity; ///< Average density of functions (bytes/function)
63 unsigned instructionSize; ///< Size of instructions in bytes
64 unsigned longInstructions;///< Number of instructions > 4 bytes
65 unsigned vbrCount32; ///< Number of 32-bit vbr values
66 unsigned vbrCount64; ///< Number of 64-bit vbr values
67 unsigned vbrCompBytes; ///< Number of vbr bytes (compressed)
68 unsigned vbrExpdBytes; ///< Number of vbr bytes (expanded)
70 typedef std::map<BytecodeFormat::CompressedBytecodeBlockIdentifiers,unsigned>
72 BlockSizeMap BlockSizes;
74 /// A structure that contains various pieces of information related to
75 /// an analysis of a single function.
76 struct BytecodeFunctionInfo {
77 std::string description; ///< Function type description
78 std::string name; ///< Name of function if it has one
79 unsigned byteSize; ///< The size of the function in bytecode bytes
80 unsigned numInstructions; ///< The number of instructions in the function
81 unsigned numBasicBlocks; ///< The number of basic blocks in the function
82 unsigned numPhis; ///< Number of Phi Nodes in Instructions
83 unsigned numOperands; ///< The number of operands in the function
84 double density; ///< Density of function
85 unsigned instructionSize; ///< Size of instructions in bytes
86 unsigned longInstructions;///< Number of instructions > 4 bytes
87 unsigned vbrCount32; ///< Number of 32-bit vbr values
88 unsigned vbrCount64; ///< Number of 64-bit vbr values
89 unsigned vbrCompBytes; ///< Number of vbr bytes (compressed)
90 unsigned vbrExpdBytes; ///< Number of vbr bytes (expanded)
93 /// A mapping of function slot numbers to the collected information about
95 std::map<const Function*,BytecodeFunctionInfo> FunctionInfo;
97 /// The content of the progressive verification
98 std::string VerifyInfo;
100 /// Flags for what should be done
101 bool detailedResults; ///< If true, FunctionInfo has contents
102 bool progressiveVerify; ///< If true, VerifyInfo has contents
105 /// This function is the main entry point into the bytecode analysis library. It
106 /// allows you to simply provide a \p filename and storage for the \p Results
107 /// that will be filled in with the analysis results.
108 /// @brief Analyze contents of a bytecode File
109 Module* AnalyzeBytecodeFile(
110 const std::string& Filename, ///< The name of the bytecode file to read
111 BytecodeAnalysis& Results, ///< The results of the analysis
112 std::string* ErrorStr = 0, ///< Errors, if any.
113 std::ostream* output = 0 ///< Stream for dump output, if wanted
116 /// This function is an alternate entry point into the bytecode analysis
117 /// library. It allows you to provide an arbitrary memory buffer which is
118 /// assumed to contain a complete bytecode file. The \p Buffer is analyzed and
119 /// the \p Results are filled in.
120 /// @brief Analyze contents of a bytecode buffer.
121 Module* AnalyzeBytecodeBuffer(
122 const unsigned char* Buffer, ///< Pointer to start of bytecode buffer
123 unsigned BufferSize, ///< Size of the bytecode buffer
124 const std::string& ModuleID, ///< Identifier for the module
125 BytecodeAnalysis& Results, ///< The results of the analysis
126 std::string* ErrorStr = 0, ///< Errors, if any.
127 std::ostream* output = 0 ///< Stream for dump output, if wanted
130 /// This function prints the contents of rhe BytecodeAnalysis structure in
131 /// a human legible form.
132 /// @brief Print BytecodeAnalysis structure to an ostream
133 void PrintBytecodeAnalysis(BytecodeAnalysis& bca, std::ostream& Out );
135 /// @brief std::ostream inserter for BytecodeAnalysis structure
136 inline std::ostream& operator<<(std::ostream& Out, BytecodeAnalysis& bca ) {
137 PrintBytecodeAnalysis(bca,Out);
141 } // End llvm namespace