Implemented the bulk of the functionality. Cleaned up the code.
[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 "llvm/Bytecode/Format.h"
23 #include <string>
24 #include <map>
25
26 namespace llvm {
27
28 /// This structure is used to contain the output of the Bytecode Analysis 
29 /// library. It simply contains fields to hold each item of the analysis 
30 /// results.
31 /// @brief Bytecode Analysis results structure
32 struct BytecodeAnalysis {
33   std::string ModuleId;     ///< Identification of the module
34   unsigned byteSize;        ///< The size of the bytecode file in bytes
35   unsigned numTypes;        ///< The number of types
36   unsigned numValues;       ///< The number of values
37   unsigned numBlocks;       ///< The number of *bytecode* blocks
38   unsigned numFunctions;    ///< The number of functions defined
39   unsigned numConstants;    ///< The number of constants
40   unsigned numGlobalVars;   ///< The number of global variables
41   unsigned numInstructions; ///< The number of instructions in all functions
42   unsigned numBasicBlocks;  ///< The number of BBs in all functions
43   unsigned numOperands;     ///< The number of BBs in all functions
44   unsigned numCmpctnTables; ///< The number of compaction tables
45   unsigned numSymTab;       ///< The number of symbol tables
46   unsigned numAlignment;    ///< The number of alignment bytes
47   unsigned maxTypeSlot;     ///< The maximum slot number for types
48   unsigned maxValueSlot;    ///< The maximum slot number for values
49   double   fileDensity;     ///< Density of file (bytes/definition)
50     ///< This is the density of the bytecode file. It is the ratio of
51     ///< the number of bytes to the number of definitions in the file. Smaller
52     ///< numbers mean the file is more compact (denser). Larger numbers mean
53     ///< the file is more sparse.
54   double   globalsDensity;  ///< density of global defs (bytes/definition)
55   double   functionDensity; ///< Average density of functions (bytes/function)
56   unsigned vbrCount32;      ///< Number of 32-bit vbr values
57   unsigned vbrCount64;      ///< Number of 64-bit vbr values
58   unsigned vbrCompBytes;    ///< Number of vbr bytes (compressed)
59   unsigned vbrExpdBytes;    ///< Number of vbr bytes (expanded)
60
61   typedef std::map<BytecodeFormat::FileBlockIDs,unsigned> BlockSizeMap;
62   BlockSizeMap BlockSizes;
63
64   /// A structure that contains various pieces of information related to
65   /// an analysis of a single function.
66   struct BytecodeFunctionInfo {
67     std::string description;  ///< Function type description
68     std::string name;         ///< Name of function if it has one
69     unsigned byteSize;        ///< The size of the function in bytecode bytes
70     unsigned numInstructions; ///< The number of instructions in the function
71     unsigned numBasicBlocks;  ///< The number of basic blocks in the function
72     unsigned numOperands;     ///< The number of operands in the function
73     double density;           ///< Density of function
74     double vbrEffectiveness;  ///< Effectiveness of variable bit rate encoding.
75   };
76
77   /// A mapping of function slot numbers to the collected information about 
78   /// the function.
79   std::map<unsigned,BytecodeFunctionInfo> FunctionInfo; 
80
81   /// The content of the bytecode dump
82   std::string BytecodeDump;
83
84   /// Flags for what should be done
85   bool dumpBytecode;     ///< If true, BytecodeDump has contents
86   bool detailedResults;  ///< If true, FunctionInfo has contents 
87 };
88
89 /// This function is the main entry point into the bytecode analysis library. It
90 /// allows you to simply provide a \p filename and storage for the \p Results 
91 /// that will be filled in with the analysis results.
92 /// @brief Analyze contents of a bytecode File
93 void AnalyzeBytecodeFile(
94       const std::string& Filename, ///< The name of the bytecode file to read
95       BytecodeAnalysis& Results,   ///< The results of the analysis
96       std::string* ErrorStr = 0    ///< Errors, if any.
97     );
98
99 /// This function is an alternate entry point into the bytecode analysis
100 /// library. It allows you to provide an arbitrary memory buffer which is
101 /// assumed to contain a complete bytecode file. The \p Buffer is analyzed and
102 /// the \p Results are filled in.
103 /// @brief Analyze contents of a bytecode buffer.
104 void AnalyzeBytecodeBuffer(
105        const unsigned char* Buffer, ///< Pointer to start of bytecode buffer
106        unsigned BufferSize,         ///< Size of the bytecode buffer
107        BytecodeAnalysis& Results,   ///< The results of the analysis
108        std::string* ErrorStr = 0    ///< Errors, if any.
109      );
110
111 /// This function prints the contents of rhe BytecodeAnalysis structure in
112 /// a human legible form.
113 /// @brief Print BytecodeAnalysis structure to an ostream
114 void PrintBytecodeAnalysis(BytecodeAnalysis& bca, std::ostream& Out );
115
116 /// @brief std::ostream inserter for BytecodeAnalysis structure
117 inline std::ostream& operator<<(std::ostream& Out, BytecodeAnalysis& bca ) {
118     PrintBytecodeAnalysis(bca,Out);
119     return Out;
120 }
121
122 } // End llvm namespace
123
124 #endif