1 //===- AnalyzerWrappers.cpp - Analyze bytecode from file or buffer -------===//
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 file implements loading and analysis of a bytecode file and analyzing a
13 //===----------------------------------------------------------------------===//
15 #include "llvm/Bytecode/Analyzer.h"
16 #include "AnalyzerInternals.h"
17 #include "Support/FileUtilities.h"
18 #include "Support/StringExtras.h"
19 #include "Config/unistd.h"
25 //===----------------------------------------------------------------------===//
26 // BytecodeFileAnalyzer - Analyze from an mmap'able file descriptor.
30 /// BytecodeFileAnalyzer - parses a bytecode file from a file
31 class BytecodeFileAnalyzer : public BytecodeAnalyzer {
33 unsigned char *Buffer;
36 BytecodeFileAnalyzer(const BytecodeFileAnalyzer&); // Do not implement
37 void operator=(const BytecodeFileAnalyzer &BFR); // Do not implement
40 BytecodeFileAnalyzer(const std::string &Filename, BytecodeAnalysis& bca);
41 ~BytecodeFileAnalyzer();
45 static std::string ErrnoMessage (int savedErrNum, std::string descr) {
46 return ::strerror(savedErrNum) + std::string(", while trying to ") + descr;
49 BytecodeFileAnalyzer::BytecodeFileAnalyzer(const std::string &Filename,
50 BytecodeAnalysis& bca) {
51 Buffer = (unsigned char*)ReadFileIntoAddressSpace(Filename, Length);
53 throw "Error reading file '" + Filename + "'.";
56 // Parse the bytecode we mmapped in
57 if ( bca.dumpBytecode )
58 DumpBytecode(Buffer, Length, bca, Filename);
59 AnalyzeBytecode(Buffer, Length, bca, Filename);
61 UnmapFileFromAddressSpace(Buffer, Length);
66 BytecodeFileAnalyzer::~BytecodeFileAnalyzer() {
67 // Unmmap the bytecode...
68 UnmapFileFromAddressSpace(Buffer, Length);
71 //===----------------------------------------------------------------------===//
72 // BytecodeBufferAnalyzer - Read from a memory buffer
76 /// BytecodeBufferAnalyzer - parses a bytecode file from a buffer
78 class BytecodeBufferAnalyzer : public BytecodeAnalyzer {
80 const unsigned char *Buffer;
83 BytecodeBufferAnalyzer(const BytecodeBufferAnalyzer&); // Do not implement
84 void operator=(const BytecodeBufferAnalyzer &BFR); // Do not implement
87 BytecodeBufferAnalyzer(const unsigned char *Buf, unsigned Length,
88 BytecodeAnalysis& bca, const std::string &ModuleID);
89 ~BytecodeBufferAnalyzer();
94 BytecodeBufferAnalyzer::BytecodeBufferAnalyzer(const unsigned char *Buf,
96 BytecodeAnalysis& bca,
97 const std::string &ModuleID) {
98 // If not aligned, allocate a new buffer to hold the bytecode...
99 const unsigned char *ParseBegin = 0;
100 if ((intptr_t)Buf & 3) {
101 Buffer = new unsigned char[Length+4];
102 unsigned Offset = 4 - ((intptr_t)Buffer & 3); // Make sure it's aligned
103 ParseBegin = Buffer + Offset;
104 memcpy((unsigned char*)ParseBegin, Buf, Length); // Copy it over
107 // If we don't need to copy it over, just use the caller's copy
108 ParseBegin = Buffer = Buf;
112 if ( bca.dumpBytecode )
113 DumpBytecode(ParseBegin, Length, bca, ModuleID);
114 AnalyzeBytecode(ParseBegin, Length, bca, ModuleID);
116 if (MustDelete) delete [] Buffer;
121 BytecodeBufferAnalyzer::~BytecodeBufferAnalyzer() {
122 if (MustDelete) delete [] Buffer;
125 //===----------------------------------------------------------------------===//
126 // BytecodeStdinAnalyzer - Read bytecode from Standard Input
130 /// BytecodeStdinAnalyzer - parses a bytecode file from stdin
132 class BytecodeStdinAnalyzer : public BytecodeAnalyzer {
134 std::vector<unsigned char> FileData;
135 unsigned char *FileBuf;
137 BytecodeStdinAnalyzer(const BytecodeStdinAnalyzer&); // Do not implement
138 void operator=(const BytecodeStdinAnalyzer &BFR); // Do not implement
141 BytecodeStdinAnalyzer(BytecodeAnalysis& bca);
145 BytecodeStdinAnalyzer::BytecodeStdinAnalyzer(BytecodeAnalysis& bca ) {
147 unsigned char Buffer[4096*4];
149 // Read in all of the data from stdin, we cannot mmap stdin...
150 while ((BlockSize = ::read(0 /*stdin*/, Buffer, 4096*4))) {
152 throw ErrnoMessage(errno, "read from standard input");
154 FileData.insert(FileData.end(), Buffer, Buffer+BlockSize);
157 if (FileData.empty())
158 throw std::string("Standard Input empty!");
160 FileBuf = &FileData[0];
161 if (bca.dumpBytecode)
162 DumpBytecode(&FileData[0], FileData.size(), bca, "<stdin>");
163 AnalyzeBytecode(FileBuf, FileData.size(), bca, "<stdin>");
166 //===----------------------------------------------------------------------===//
168 //===----------------------------------------------------------------------===//
170 // AnalyzeBytecodeFile - analyze one file
171 void llvm::AnalyzeBytecodeFile(const std::string &Filename,
172 BytecodeAnalysis& bca,
173 std::string *ErrorStr)
176 if ( Filename != "-" )
177 BytecodeFileAnalyzer bfa(Filename,bca);
179 BytecodeStdinAnalyzer bsa(bca);
180 } catch (std::string &err) {
181 if (ErrorStr) *ErrorStr = err;
185 // AnalyzeBytecodeBuffer - analyze a buffer
186 void llvm::AnalyzeBytecodeBuffer(
187 const unsigned char* Buffer, ///< Pointer to start of bytecode buffer
188 unsigned BufferSize, ///< Size of the bytecode buffer
189 BytecodeAnalysis& Results, ///< The results of the analysis
190 std::string* ErrorStr ///< Errors, if any.
194 BytecodeBufferAnalyzer(Buffer, BufferSize, Results, "<buffer>" );
195 } catch (std::string& err ) {
196 if ( ErrorStr) *ErrorStr = err;
201 /// This function prints the contents of rhe BytecodeAnalysis structure in
202 /// a human legible form.
203 /// @brief Print BytecodeAnalysis structure to an ostream
205 inline static void print(std::ostream& Out, const char*title,
206 unsigned val, bool nl = true ) {
207 Out << std::setw(30) << std::right << title
208 << std::setw(0) << ": "
209 << std::setw(9) << val << "\n";
212 inline static void print(std::ostream&Out, const char*title,
214 Out << std::setw(30) << std::right << title
215 << std::setw(0) << ": "
216 << std::setw(9) << std::setprecision(6) << val << "\n" ;
219 inline static void print(std::ostream&Out, const char*title,
220 double top, double bot ) {
221 Out << std::setw(30) << std::right << title
222 << std::setw(0) << ": "
223 << std::setw(9) << std::setprecision(6) << top
224 << " (" << std::left << std::setw(0) << std::setprecision(4)
225 << (top/bot)*100.0 << "%)\n";
227 inline static void print(std::ostream&Out, const char*title,
228 std::string val, bool nl = true) {
229 Out << std::setw(30) << std::right << title
230 << std::setw(0) << ": "
231 << std::left << val << (nl ? "\n" : "");
236 void llvm::PrintBytecodeAnalysis(BytecodeAnalysis& bca, std::ostream& Out )
238 print(Out, "Bytecode Analysis Of Module", bca.ModuleId);
239 print(Out, "File Size", bca.byteSize);
240 print(Out, "Bytecode Compression Index",std::string("TBD"));
241 print(Out, "Number Of Bytecode Blocks", bca.numBlocks);
242 print(Out, "Number Of Types", bca.numTypes);
243 print(Out, "Number Of Values", bca.numValues);
244 print(Out, "Number Of Constants", bca.numConstants);
245 print(Out, "Number Of Global Variables", bca.numGlobalVars);
246 print(Out, "Number Of Functions", bca.numFunctions);
247 print(Out, "Number Of Basic Blocks", bca.numBasicBlocks);
248 print(Out, "Number Of Instructions", bca.numInstructions);
249 print(Out, "Number Of Operands", bca.numOperands);
250 print(Out, "Number Of Compaction Tables", bca.numCmpctnTables);
251 print(Out, "Number Of Symbol Tables", bca.numSymTab);
252 print(Out, "Maximum Type Slot Number", bca.maxTypeSlot);
253 print(Out, "Maximum Value Slot Number", bca.maxValueSlot);
254 print(Out, "Bytes Thrown To Alignment", double(bca.numAlignment),
255 double(bca.byteSize));
256 print(Out, "File Density (bytes/def)", bca.fileDensity);
257 print(Out, "Globals Density (bytes/def)", bca.globalsDensity);
258 print(Out, "Function Density (bytes/func)", bca.functionDensity);
259 print(Out, "Number of VBR 32-bit Integers", bca.vbrCount32);
260 print(Out, "Number of VBR 64-bit Integers", bca.vbrCount64);
261 print(Out, "Number of VBR Compressed Bytes", bca.vbrCompBytes);
262 print(Out, "Number of VBR Expanded Bytes", bca.vbrExpdBytes);
263 print(Out, "VBR Savings",
264 double(bca.vbrExpdBytes)-double(bca.vbrCompBytes),
265 double(bca.byteSize));
267 if ( bca.detailedResults ) {
268 print(Out, "Module Bytes",
269 double(bca.BlockSizes[BytecodeFormat::Module]),
270 double(bca.byteSize));
271 print(Out, "Function Bytes",
272 double(bca.BlockSizes[BytecodeFormat::Function]),
273 double(bca.byteSize));
274 print(Out, "Constant Pool Bytes",
275 double(bca.BlockSizes[BytecodeFormat::ConstantPool]),
276 double(bca.byteSize));
277 print(Out, "Symbol Table Bytes",
278 double(bca.BlockSizes[BytecodeFormat::SymbolTable]),
279 double(bca.byteSize));
280 print(Out, "Module Global Info Bytes",
281 double(bca.BlockSizes[BytecodeFormat::ModuleGlobalInfo]),
282 double(bca.byteSize));
283 print(Out, "Global Type Plane Bytes",
284 double(bca.BlockSizes[BytecodeFormat::GlobalTypePlane]),
285 double(bca.byteSize));
286 print(Out, "Basic Block Bytes",
287 double(bca.BlockSizes[BytecodeFormat::BasicBlock]),
288 double(bca.byteSize));
289 print(Out, "Instruction List Bytes",
290 double(bca.BlockSizes[BytecodeFormat::InstructionList]),
291 double(bca.byteSize));
292 print(Out, "Compaction Table Bytes",
293 double(bca.BlockSizes[BytecodeFormat::CompactionTable]),
294 double(bca.byteSize));
296 std::map<unsigned,BytecodeAnalysis::BytecodeFunctionInfo>::iterator I =
297 bca.FunctionInfo.begin();
298 std::map<unsigned,BytecodeAnalysis::BytecodeFunctionInfo>::iterator E =
299 bca.FunctionInfo.end();
302 Out << std::left << std::setw(0);
303 Out << "Function: " << I->second.name << " Slot=" << I->first << "\n";
304 print(Out,"Type:", I->second.description);
305 print(Out,"Byte Size", I->second.byteSize);
306 print(Out,"Instructions", I->second.numInstructions);
307 print(Out,"Basic Blocks", I->second.numBasicBlocks);
308 print(Out,"Operand", I->second.numOperands);
309 print(Out,"Function Density", I->second.density);
310 print(Out,"VBR Effectiveness", I->second.vbrEffectiveness);
315 if ( bca.dumpBytecode )
316 Out << bca.BytecodeDump;