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/Analysis library.
11 // This library is used to read VM bytecode files from an iostream and print
12 // out a diagnostic analysis of the contents of the file. It is intended for
13 // three uses: (a) understanding the bytecode format, (b) ensuring correctness
14 // of bytecode format, (c) statistical analysis of generated bytecode files.
16 //===----------------------------------------------------------------------===//
18 #ifndef LLVM_BYTECODE_ANALYZER_H
19 #define LLVM_BYTECODE_ANALYZER_H
26 /// This structure is used to contain the output of the Bytecode Analysis
27 /// library. It simply contains fields to hold each item of the analysis
29 /// @brief Bytecode Analysis results structure
30 struct BytecodeAnalysis {
31 unsigned byteSize; ///< The size of the bytecode file in bytes
32 unsigned numTypes; ///< The number of types
33 unsigned numValues; ///< The number of values
34 unsigned numFunctions; ///< The number of functions defined
35 unsigned numConstants; ///< The number of constants
36 unsigned numGlobalVars; ///< The number of global variables
37 unsigned numInstructions; ///< The number of instructions in all functions
38 unsigned numBasicBlocks; ///< The number of BBs in all functions
39 unsigned numOperands; ///< The number of BBs in all functions
40 unsigned maxTypeSlot; ///< The maximum slot number for types
41 unsigned maxValueSlot; ///< The maximum slot number for values
42 double density; ///< Density of file (bytes/defs)
44 /// A structure that contains various pieces of information related to
45 /// an analysis of a single function.
46 struct BytecodeFunctionInfo {
47 unsigned byteSize; ///< The size of the function in bytecode bytes
48 unsigned numInstructions; ///< The number of instructions in the function
49 unsigned numBasicBlocks; ///< The number of basic blocks in the function
50 unsigned numOperands; ///< The number of operands in the function
51 double density; ///< Density of function
52 double vbrEffectiveness; ///< Effectiveness of variable bit rate encoding.
53 ///< This is the average number of bytes per unsigned value written in the
54 ///< vbr encoding. A "perfect" score of 1.0 means all vbr values were
55 ///< encoded in one byte. A score between 1.0 and 4.0 means that some
56 ///< savings were achieved. A score of 4.0 means vbr didn't help. A score
57 ///< greater than 4.0 means vbr negatively impacted size of the file.
60 /// A mapping of function names to the collected information about the
62 std::map<std::string,BytecodeFunctionInfo> FunctionInfo;
64 /// Flags for what should be done
68 /// This function is the main entry point into the bytecode analysis library. It
69 /// allows you to simply provide a \P filename and storage for the \P Results
70 /// that will be filled in with the analysis results.
71 /// @brief Analyze contents of a bytecode File
72 void AnalyzeBytecodeFile(
73 const std::string& Filename, ///< The name of the bytecode file to read
74 BytecodeAnalysis& Results, ///< The results of the analysis
75 std::string* ErrorStr = 0 ///< Errors, if any.
78 /// This function is an alternate entry point into the bytecode analysis
79 /// library. It allows you to provide an arbitrary memory buffer which is
80 /// assumed to contain a complete bytecode file. The \P Buffer is analyzed and
81 /// the \P Results are filled in.
82 /// @brief Analyze contents of a bytecode buffer.
83 void AnalyzeBytecodeBuffer(
84 const unsigned char* Buffer, ///< Pointer to start of bytecode buffer
85 unsigned BufferSize, ///< Size of the bytecode buffer
86 BytecodeAnalysis& Results, ///< The results of the analysis
87 std::string* ErrorStr = 0 ///< Errors, if any.
90 /// This function prints the contents of rhe BytecodeAnalysis structure in
91 /// a human legible form.
92 /// @brief Print BytecodeAnalysis structure to an ostream
93 void PrintBytecodeAnalysis(BytecodeAnalysis& bca, std::ostream& Out );
95 } // End llvm namespace