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