Add some new fields for bytecode analysis.
[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/Analysis library.
11 // This library 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 <string>
23 #include <map>
24
25 namespace llvm {
26
27 /// This structure is used to contain the output of the Bytecode Analysis 
28 /// library. It simply contains fields to hold each item of the analysis 
29 /// results.
30 /// @brief Bytecode Analysis results structure
31 struct BytecodeAnalysis {
32   std::string ModuleId;     ///< Identification of the module
33   unsigned byteSize;        ///< The size of the bytecode file in bytes
34   unsigned numTypes;        ///< The number of types
35   unsigned numValues;       ///< The number of values
36   unsigned numFunctions;    ///< The number of functions defined
37   unsigned numConstants;    ///< The number of constants
38   unsigned numGlobalVars;   ///< The number of global variables
39   unsigned numInstructions; ///< The number of instructions in all functions
40   unsigned numBasicBlocks;  ///< The number of BBs in all functions
41   unsigned numOperands;     ///< The number of BBs in all functions
42   unsigned numCmpctnTables; ///< The number of compaction tables
43   unsigned numSymTab;       ///< The number of symbol tables
44   unsigned maxTypeSlot;     ///< The maximum slot number for types
45   unsigned maxValueSlot;    ///< The maximum slot number for values
46   double   density;         ///< Density of file (bytes/defs) 
47
48   /// A structure that contains various pieces of information related to
49   /// an analysis of a single function.
50   struct BytecodeFunctionInfo {
51     unsigned byteSize;        ///< The size of the function in bytecode bytes
52     unsigned numInstructions; ///< The number of instructions in the function
53     unsigned numBasicBlocks;  ///< The number of basic blocks in the function
54     unsigned numOperands;     ///< The number of operands in the function
55     double density;           ///< Density of function
56     double vbrEffectiveness;  ///< Effectiveness of variable bit rate encoding.
57     ///< This is the average number of bytes per unsigned value written in the
58     ///< vbr encoding. A "perfect" score of 1.0 means all vbr values were 
59     ///< encoded in one byte. A score between 1.0 and 4.0 means that some
60     ///< savings were achieved. A score of 4.0 means vbr didn't help. A score
61     ///< greater than 4.0 means vbr negatively impacted size of the file.
62   };
63
64   /// A mapping of function names to the collected information about the 
65   /// function.
66   std::map<std::string,BytecodeFunctionInfo> FunctionInfo; 
67
68   /// The content of the bytecode dump
69   std::string BytecodeDump;
70
71   /// Flags for what should be done
72   bool dumpBytecode;     ///< If true, BytecodeDump has contents
73   bool detailedResults;  ///< If true, FunctionInfo has contents 
74 };
75
76 /// This function is the main entry point into the bytecode analysis library. It
77 /// allows you to simply provide a \p filename and storage for the \p Results 
78 /// that will be filled in with the analysis results.
79 /// @brief Analyze contents of a bytecode File
80 void AnalyzeBytecodeFile(
81       const std::string& Filename, ///< The name of the bytecode file to read
82       BytecodeAnalysis& Results,   ///< The results of the analysis
83       std::string* ErrorStr = 0    ///< Errors, if any.
84     );
85
86 /// This function is an alternate entry point into the bytecode analysis
87 /// library. It allows you to provide an arbitrary memory buffer which is
88 /// assumed to contain a complete bytecode file. The \p Buffer is analyzed and
89 /// the \p Results are filled in.
90 /// @brief Analyze contents of a bytecode buffer.
91 void AnalyzeBytecodeBuffer(
92        const unsigned char* Buffer, ///< Pointer to start of bytecode buffer
93        unsigned BufferSize,         ///< Size of the bytecode buffer
94        BytecodeAnalysis& Results,   ///< The results of the analysis
95        std::string* ErrorStr = 0    ///< Errors, if any.
96      );
97
98 /// This function prints the contents of rhe BytecodeAnalysis structure in
99 /// a human legible form.
100 /// @brief Print BytecodeAnalysis structure to an ostream
101 void PrintBytecodeAnalysis(BytecodeAnalysis& bca, std::ostream& Out );
102
103 /// @brief std::ostream inserter for BytecodeAnalysis structure
104 inline std::ostream& operator<<(std::ostream& Out, BytecodeAnalysis& bca ) {
105     PrintBytecodeAnalysis(bca,Out);
106     return Out;
107 }
108
109 } // End llvm namespace
110
111 #endif