1 //===-- Analyzer.cpp - Analysis and Dumping of Bytecode 000000---*- C++ -*-===//
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 the AnalyzerHandler class and PrintBytecodeAnalysis
11 // function which together comprise the basic functionality of the llmv-abcd
12 // tool. The AnalyzerHandler collects information about the bytecode file into
13 // the BytecodeAnalysis structure. The PrintBytecodeAnalysis function prints
14 // out the content of that structure.
15 // @see include/llvm/Bytecode/Analysis.h
17 //===----------------------------------------------------------------------===//
20 #include "llvm/Constants.h"
21 #include "llvm/DerivedTypes.h"
22 #include "llvm/Module.h"
23 #include "llvm/Analysis/Verifier.h"
24 #include "llvm/Bytecode/BytecodeHandler.h"
32 /// @brief Bytecode reading handler for analyzing bytecode.
33 class AnalyzerHandler : public BytecodeHandler {
34 BytecodeAnalysis& bca; ///< The structure in which data is recorded
35 std::ostringstream dump; ///< A convenience for dumping data.
36 /// @brief Keeps track of current function
37 BytecodeAnalysis::BytecodeFunctionInfo* currFunc;
38 Module* M; ///< Keeps track of current module
43 /// The only way to construct an AnalyzerHandler. All that is needed is a
44 /// reference to the BytecodeAnalysis structure where the output will be
46 AnalyzerHandler(BytecodeAnalysis& TheBca)
53 /// @name BytecodeHandler Implementations
56 virtual void handleError(const std::string& str ) {
57 dump << "ERROR: " << str << "\n";
58 bca.BytecodeDump = dump.str() ;
61 virtual void handleStart( Module* Mod, unsigned theSize ) {
63 dump << "Bytecode {\n";
64 bca.byteSize = theSize;
71 bca.numGlobalVars = 0;
72 bca.numInstructions = 0;
73 bca.numBasicBlocks = 0;
75 bca.numCmpctnTables = 0;
80 bca.fileDensity = 0.0;
81 bca.globalsDensity = 0.0;
82 bca.functionDensity = 0.0;
83 bca.instructionSize = 0;
84 bca.longInstructions = 0;
89 bca.FunctionInfo.clear();
90 bca.BytecodeDump.clear();
91 bca.BlockSizes[BytecodeFormat::Module] = 0;
92 bca.BlockSizes[BytecodeFormat::Function] = 0;
93 bca.BlockSizes[BytecodeFormat::ConstantPool] = 0;
94 bca.BlockSizes[BytecodeFormat::SymbolTable] = 0;
95 bca.BlockSizes[BytecodeFormat::ModuleGlobalInfo] = 0;
96 bca.BlockSizes[BytecodeFormat::GlobalTypePlane] = 0;
97 bca.BlockSizes[BytecodeFormat::BasicBlock] = 0;
98 bca.BlockSizes[BytecodeFormat::InstructionList] = 0;
99 bca.BlockSizes[BytecodeFormat::CompactionTable] = 0;
102 virtual void handleFinish() {
103 dump << "} End Bytecode\n";
104 bca.BytecodeDump = dump.str() ;
106 bca.fileDensity = double(bca.byteSize) / double( bca.numTypes + bca.numValues );
107 double globalSize = 0.0;
108 globalSize += double(bca.BlockSizes[BytecodeFormat::ConstantPool]);
109 globalSize += double(bca.BlockSizes[BytecodeFormat::ModuleGlobalInfo]);
110 globalSize += double(bca.BlockSizes[BytecodeFormat::GlobalTypePlane]);
111 bca.globalsDensity = globalSize / double( bca.numTypes + bca.numConstants +
113 bca.functionDensity = double(bca.BlockSizes[BytecodeFormat::Function]) /
114 double(bca.numFunctions);
116 if ( bca.progressiveVerify ) {
118 verifyModule(*M, ThrowExceptionAction);
119 } catch ( std::string& msg ) {
120 bca.VerifyInfo += "Verify@Finish: " + msg + "\n";
125 virtual void handleModuleBegin(const std::string& id) {
126 dump << " Module " << id << " {\n";
130 virtual void handleModuleEnd(const std::string& id) {
131 dump << " } End Module " << id << "\n";
132 if ( bca.progressiveVerify ) {
134 verifyModule(*M, ThrowExceptionAction);
135 } catch ( std::string& msg ) {
136 bca.VerifyInfo += "Verify@EndModule: " + msg + "\n";
141 virtual void handleVersionInfo(
142 unsigned char RevisionNum, ///< Byte code revision number
143 Module::Endianness Endianness, ///< Endianness indicator
144 Module::PointerSize PointerSize ///< PointerSize indicator
146 dump << " RevisionNum: " << int(RevisionNum)
147 << " Endianness: " << Endianness
148 << " PointerSize: " << PointerSize << "\n";
151 virtual void handleModuleGlobalsBegin() {
152 dump << " BLOCK: ModuleGlobalInfo {\n";
155 virtual void handleGlobalVariable(
156 const Type* ElemType,
158 GlobalValue::LinkageTypes Linkage,
166 << ( initSlot == 0 ? "Uni" : "I" ) << "nitialized, "
167 << ( isConstant? "Constant, " : "Variable, ")
168 << " Linkage=" << Linkage << " Type="
169 << ElemType->getDescription()
170 << " Slot=" << SlotNum << " InitSlot=" << initSlot
174 virtual void handleType( const Type* Ty ) {
176 dump << " Type: " << Ty->getDescription() << "\n";
179 virtual void handleFunctionDeclaration(
180 Function* Func ///< The function
184 dump << " Function Decl: " << Func->getType()->getDescription() << "\n";
187 virtual void handleGlobalInitializer(GlobalVariable* GV, Constant* CV) {
188 dump << " Initializer: GV=";
195 virtual void handleModuleGlobalsEnd() {
196 dump << " } END BLOCK: ModuleGlobalInfo\n";
197 if ( bca.progressiveVerify ) {
199 verifyModule(*M, ThrowExceptionAction);
200 } catch ( std::string& msg ) {
201 bca.VerifyInfo += "Verify@EndModuleGlobalInfo: " + msg + "\n";
206 virtual void handleCompactionTableBegin() {
207 dump << " BLOCK: CompactionTable {\n";
210 virtual void handleCompactionTablePlane( unsigned Ty, unsigned NumEntries) {
211 bca.numCmpctnTables++;
212 dump << " Plane: Ty=" << Ty << " Size=" << NumEntries << "\n";
215 virtual void handleCompactionTableType( unsigned i, unsigned TypSlot,
217 dump << " Type: " << i << " Slot:" << TypSlot
218 << " is " << Ty->getDescription() << "\n";
221 virtual void handleCompactionTableValue(
226 dump << " Value: " << i << " TypSlot: " << TypSlot
227 << " ValSlot:" << ValSlot << " is " << Ty->getDescription()
231 virtual void handleCompactionTableEnd() {
232 dump << " } END BLOCK: CompactionTable\n";
235 virtual void handleSymbolTableBegin(Function* CF, SymbolTable* ST) {
237 dump << " BLOCK: SymbolTable {\n";
240 virtual void handleSymbolTablePlane(unsigned Ty, unsigned NumEntries,
242 dump << " Plane: Ty=" << Ty << " Size=" << NumEntries
243 << " Type: " << Typ->getDescription() << "\n";
246 virtual void handleSymbolTableType(unsigned i, unsigned slot,
247 const std::string& name ) {
248 dump << " Type " << i << " Slot=" << slot
249 << " Name: " << name << "\n";
252 virtual void handleSymbolTableValue(unsigned i, unsigned slot,
253 const std::string& name ) {
254 dump << " Value " << i << " Slot=" << slot
255 << " Name: " << name << "\n";
258 virtual void handleSymbolTableEnd() {
259 dump << " } END BLOCK: SymbolTable\n";
262 virtual void handleFunctionBegin(Function* Func, unsigned Size) {
263 dump << " BLOCK: Function {\n";
264 dump << " Linkage: " << Func->getLinkage() << "\n";
265 dump << " Type: " << Func->getType()->getDescription() << "\n";
266 const FunctionType* FType =
267 cast<FunctionType>(Func->getType()->getElementType());
268 currFunc = &bca.FunctionInfo[Func];
269 currFunc->description = FType->getDescription();
270 currFunc->name = Func->getName();
271 currFunc->byteSize = Size;
272 currFunc->numInstructions = 0;
273 currFunc->numBasicBlocks = 0;
274 currFunc->numPhis = 0;
275 currFunc->numOperands = 0;
276 currFunc->density = 0.0;
277 currFunc->instructionSize = 0;
278 currFunc->longInstructions = 0;
279 currFunc->vbrCount32 = 0;
280 currFunc->vbrCount64 = 0;
281 currFunc->vbrCompBytes = 0;
282 currFunc->vbrExpdBytes = 0;
286 virtual void handleFunctionEnd( Function* Func) {
287 dump << " } END BLOCK: Function\n";
288 currFunc->density = double(currFunc->byteSize) /
289 double(currFunc->numInstructions+currFunc->numBasicBlocks);
291 if ( bca.progressiveVerify ) {
293 verifyModule(*M, ThrowExceptionAction);
294 } catch ( std::string& msg ) {
295 bca.VerifyInfo += "Verify@EndFunction: " + msg + "\n";
300 virtual void handleBasicBlockBegin( unsigned blocknum) {
301 dump << " BLOCK: BasicBlock #" << blocknum << "{\n";
302 bca.numBasicBlocks++;
304 if ( currFunc ) currFunc->numBasicBlocks++;
307 virtual bool handleInstruction( unsigned Opcode, const Type* iType,
308 std::vector<unsigned>& Operands, unsigned Size){
309 dump << " INST: OpCode="
310 << Instruction::getOpcodeName(Opcode) << " Type=\""
311 << iType->getDescription() << "\"";
312 for ( unsigned i = 0; i < Operands.size(); ++i )
313 dump << " Op(" << i << ")=Slot(" << Operands[i] << ")";
316 bca.numInstructions++;
318 bca.instructionSize += Size;
319 if (Size > 4 ) bca.longInstructions++;
320 bca.numOperands += Operands.size();
322 currFunc->numInstructions++;
323 currFunc->instructionSize += Size;
324 if (Size > 4 ) currFunc->longInstructions++;
325 if ( Opcode == Instruction::PHI ) currFunc->numPhis++;
327 return Instruction::isTerminator(Opcode);
330 virtual void handleBasicBlockEnd(unsigned blocknum) {
331 dump << " } END BLOCK: BasicBlock #" << blocknum << "{\n";
334 virtual void handleGlobalConstantsBegin() {
335 dump << " BLOCK: GlobalConstants {\n";
338 virtual void handleConstantExpression( unsigned Opcode,
339 std::vector<Constant*> ArgVec, Constant* C ) {
340 dump << " EXPR: " << Instruction::getOpcodeName(Opcode) << "\n";
341 for ( unsigned i = 0; i < ArgVec.size(); ++i ) {
342 dump << " Arg#" << i << " "; ArgVec[i]->print(dump); dump << "\n";
351 virtual void handleConstantValue( Constant * c ) {
359 virtual void handleConstantArray( const ArrayType* AT,
360 std::vector<Constant*>& Elements,
362 Constant* ArrayVal ) {
363 dump << " ARRAY: " << AT->getDescription()
364 << " TypeSlot=" << TypeSlot << "\n";
365 for ( unsigned i = 0; i < Elements.size(); ++i ) {
367 Elements[i]->print(dump);
371 ArrayVal->print(dump);
378 virtual void handleConstantStruct(
379 const StructType* ST,
380 std::vector<Constant*>& Elements,
383 dump << " STRUC: " << ST->getDescription() << "\n";
384 for ( unsigned i = 0; i < Elements.size(); ++i ) {
385 dump << " #" << i << " "; Elements[i]->print(dump); dump << "\n";
388 StructVal->print(dump);
394 virtual void handleConstantPointer( const PointerType* PT,
395 unsigned Slot, GlobalValue* GV, Constant* PtrVal) {
396 dump << " PNTR: " << PT->getDescription()
397 << " Slot=" << Slot << " GlobalValue=";
406 virtual void handleConstantString( const ConstantArray* CA ) {
414 virtual void handleGlobalConstantsEnd() {
415 dump << " } END BLOCK: GlobalConstants\n";
416 if ( bca.progressiveVerify ) {
418 verifyModule(*M, ThrowExceptionAction);
419 } catch ( std::string& msg ) {
420 bca.VerifyInfo += "Verify@EndGlobalConstants: " + msg + "\n";
425 virtual void handleAlignment(unsigned numBytes) {
426 bca.numAlignment += numBytes;
429 virtual void handleBlock(
430 unsigned BType, const unsigned char* StartPtr, unsigned Size) {
432 bca.BlockSizes[llvm::BytecodeFormat::FileBlockIDs(BType)] += Size;
435 virtual void handleVBR32(unsigned Size ) {
437 bca.vbrCompBytes += Size;
438 bca.vbrExpdBytes += sizeof(uint32_t);
440 currFunc->vbrCount32++;
441 currFunc->vbrCompBytes += Size;
442 currFunc->vbrExpdBytes += sizeof(uint32_t);
446 virtual void handleVBR64(unsigned Size ) {
448 bca.vbrCompBytes += Size;
449 bca.vbrExpdBytes += sizeof(uint64_t);
451 currFunc->vbrCount64++;
452 currFunc->vbrCompBytes += Size;
453 currFunc->vbrExpdBytes += sizeof(uint64_t);
459 /// @brief Utility for printing a titled unsigned value with
460 /// an aligned colon.
461 inline static void print(std::ostream& Out, const char*title,
462 unsigned val, bool nl = true ) {
463 Out << std::setw(30) << std::right << title
464 << std::setw(0) << ": "
465 << std::setw(9) << val << "\n";
468 /// @brief Utility for printing a titled double value with an
470 inline static void print(std::ostream&Out, const char*title,
472 Out << std::setw(30) << std::right << title
473 << std::setw(0) << ": "
474 << std::setw(9) << std::setprecision(6) << val << "\n" ;
477 /// @brief Utility for printing a titled double value with a
478 /// percentage and aligned colon.
479 inline static void print(std::ostream&Out, const char*title,
480 double top, double bot ) {
481 Out << std::setw(30) << std::right << title
482 << std::setw(0) << ": "
483 << std::setw(9) << std::setprecision(6) << top
484 << " (" << std::left << std::setw(0) << std::setprecision(4)
485 << (top/bot)*100.0 << "%)\n";
488 /// @brief Utility for printing a titled string value with
489 /// an aligned colon.
490 inline static void print(std::ostream&Out, const char*title,
491 std::string val, bool nl = true) {
492 Out << std::setw(30) << std::right << title
493 << std::setw(0) << ": "
494 << std::left << val << (nl ? "\n" : "");
501 /// This function prints the contents of rhe BytecodeAnalysis structure in
502 /// a human legible form.
503 /// @brief Print BytecodeAnalysis structure to an ostream
504 void PrintBytecodeAnalysis(BytecodeAnalysis& bca, std::ostream& Out )
506 print(Out, "Bytecode Analysis Of Module", bca.ModuleId);
507 print(Out, "File Size", bca.byteSize);
508 print(Out, "Number Of Bytecode Blocks", bca.numBlocks);
509 print(Out, "Number Of Types", bca.numTypes);
510 print(Out, "Number Of Values", bca.numValues);
511 print(Out, "Number Of Constants", bca.numConstants);
512 print(Out, "Number Of Global Variables", bca.numGlobalVars);
513 print(Out, "Number Of Functions", bca.numFunctions);
514 print(Out, "Number Of Basic Blocks", bca.numBasicBlocks);
515 print(Out, "Number Of Instructions", bca.numInstructions);
516 print(Out, "Number Of Operands", bca.numOperands);
517 print(Out, "Number Of Compaction Tables", bca.numCmpctnTables);
518 print(Out, "Number Of Symbol Tables", bca.numSymTab);
519 print(Out, "Long Instructions", bca.longInstructions);
520 print(Out, "Instruction Size", bca.instructionSize);
521 print(Out, "Average Instruction Size",
522 double(bca.instructionSize)/double(bca.numInstructions));
523 print(Out, "Maximum Type Slot Number", bca.maxTypeSlot);
524 print(Out, "Maximum Value Slot Number", bca.maxValueSlot);
525 print(Out, "Bytes Thrown To Alignment", double(bca.numAlignment),
526 double(bca.byteSize));
527 print(Out, "File Density (bytes/def)", bca.fileDensity);
528 print(Out, "Globals Density (bytes/def)", bca.globalsDensity);
529 print(Out, "Function Density (bytes/func)", bca.functionDensity);
530 print(Out, "Number of VBR 32-bit Integers", bca.vbrCount32);
531 print(Out, "Number of VBR 64-bit Integers", bca.vbrCount64);
532 print(Out, "Number of VBR Compressed Bytes", bca.vbrCompBytes);
533 print(Out, "Number of VBR Expanded Bytes", bca.vbrExpdBytes);
534 print(Out, "VBR Savings",
535 double(bca.vbrExpdBytes)-double(bca.vbrCompBytes),
536 double(bca.vbrExpdBytes));
538 if ( bca.detailedResults ) {
539 print(Out, "Module Bytes",
540 double(bca.BlockSizes[BytecodeFormat::Module]),
541 double(bca.byteSize));
542 print(Out, "Function Bytes",
543 double(bca.BlockSizes[BytecodeFormat::Function]),
544 double(bca.byteSize));
545 print(Out, "Constant Pool Bytes",
546 double(bca.BlockSizes[BytecodeFormat::ConstantPool]),
547 double(bca.byteSize));
548 print(Out, "Symbol Table Bytes",
549 double(bca.BlockSizes[BytecodeFormat::SymbolTable]),
550 double(bca.byteSize));
551 print(Out, "Module Global Info Bytes",
552 double(bca.BlockSizes[BytecodeFormat::ModuleGlobalInfo]),
553 double(bca.byteSize));
554 print(Out, "Global Type Plane Bytes",
555 double(bca.BlockSizes[BytecodeFormat::GlobalTypePlane]),
556 double(bca.byteSize));
557 print(Out, "Basic Block Bytes",
558 double(bca.BlockSizes[BytecodeFormat::BasicBlock]),
559 double(bca.byteSize));
560 print(Out, "Instruction List Bytes",
561 double(bca.BlockSizes[BytecodeFormat::InstructionList]),
562 double(bca.byteSize));
563 print(Out, "Compaction Table Bytes",
564 double(bca.BlockSizes[BytecodeFormat::CompactionTable]),
565 double(bca.byteSize));
567 std::map<const Function*,BytecodeAnalysis::BytecodeFunctionInfo>::iterator I =
568 bca.FunctionInfo.begin();
569 std::map<const Function*,BytecodeAnalysis::BytecodeFunctionInfo>::iterator E =
570 bca.FunctionInfo.end();
573 Out << std::left << std::setw(0);
574 Out << "Function: " << I->second.name << "\n";
575 print(Out, "Type:", I->second.description);
576 print(Out, "Byte Size", I->second.byteSize);
577 print(Out, "Instructions", I->second.numInstructions);
578 print(Out, "Long Instructions", I->second.longInstructions);
579 print(Out, "Instruction Size", I->second.instructionSize);
580 print(Out, "Average Instruction Size",
581 double(I->second.instructionSize)/double(I->second.numInstructions));
582 print(Out, "Basic Blocks", I->second.numBasicBlocks);
583 print(Out, "Operand", I->second.numOperands);
584 print(Out, "Function Density", I->second.density);
585 print(Out, "Number of VBR 32-bit Integers", I->second.vbrCount32);
586 print(Out, "Number of VBR 64-bit Integers", I->second.vbrCount64);
587 print(Out, "Number of VBR Compressed Bytes", I->second.vbrCompBytes);
588 print(Out, "Number of VBR Expanded Bytes", I->second.vbrExpdBytes);
589 print(Out, "VBR Savings",
590 double(I->second.vbrExpdBytes)-double(I->second.vbrCompBytes),
591 double(I->second.vbrExpdBytes));
596 if ( bca.dumpBytecode )
597 Out << bca.BytecodeDump;
599 if ( bca.progressiveVerify )
600 Out << bca.VerifyInfo;
603 BytecodeHandler* createBytecodeAnalyzerHandler(BytecodeAnalysis& bca)
605 return new AnalyzerHandler(bca);