Bytecode Analyzer cleanup:
[oota-llvm.git] / include / llvm / Bytecode / Analyzer.h
1 //===-- llvm/Bytecode/Analyzer.h - Analyzer for Bytecode files --*- C++ -*-===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
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.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
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 
15 // bytecode files.
16 //
17 //===----------------------------------------------------------------------===//
18
19 #ifndef LLVM_BYTECODE_ANALYZER_H
20 #define LLVM_BYTECODE_ANALYZER_H
21
22 #include "llvm/Bytecode/Format.h"
23 #include <string>
24 #include <map>
25
26 namespace llvm {
27
28 // Forward declarations
29 class Function;
30 class Module;
31
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 
34 /// results.
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)
69
70   typedef std::map<BytecodeFormat::CompressedBytecodeBlockIdentifiers,unsigned> 
71       BlockSizeMap;
72   BlockSizeMap BlockSizes;
73
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)
91   };
92
93   /// A mapping of function slot numbers to the collected information about 
94   /// the function.
95   std::map<const Function*,BytecodeFunctionInfo> FunctionInfo; 
96
97   /// The content of the progressive verification
98   std::string VerifyInfo;
99
100   /// Flags for what should be done
101   bool detailedResults;       ///< If true, FunctionInfo has contents 
102   bool progressiveVerify;     ///< If true, VerifyInfo has contents
103 };
104
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
114     );
115
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
128      );
129
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 );
134
135 /// @brief std::ostream inserter for BytecodeAnalysis structure
136 inline std::ostream& operator<<(std::ostream& Out, BytecodeAnalysis& bca ) {
137     PrintBytecodeAnalysis(bca,Out);
138     return Out;
139 }
140
141 } // End llvm namespace
142
143 #endif