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/Analyzer.h"
25 #include "llvm/Bytecode/BytecodeHandler.h"
33 /// @brief Bytecode reading handler for analyzing bytecode.
34 class AnalyzerHandler : public BytecodeHandler {
35 BytecodeAnalysis& bca; ///< The structure in which data is recorded
36 std::ostringstream dump; ///< A convenience for dumping data.
37 /// @brief Keeps track of current function
38 BytecodeAnalysis::BytecodeFunctionInfo* currFunc;
39 Module* M; ///< Keeps track of current module
44 /// The only way to construct an AnalyzerHandler. All that is needed is a
45 /// reference to the BytecodeAnalysis structure where the output will be
47 AnalyzerHandler(BytecodeAnalysis& TheBca)
54 /// @name BytecodeHandler Implementations
57 virtual void handleError(const std::string& str ) {
58 dump << "ERROR: " << str << "\n";
59 bca.BytecodeDump = dump.str() ;
62 virtual void handleStart( Module* Mod, unsigned theSize ) {
64 dump << "Bytecode {\n";
65 bca.byteSize = theSize;
72 bca.numGlobalVars = 0;
73 bca.numInstructions = 0;
74 bca.numBasicBlocks = 0;
76 bca.numCmpctnTables = 0;
81 bca.fileDensity = 0.0;
82 bca.globalsDensity = 0.0;
83 bca.functionDensity = 0.0;
84 bca.instructionSize = 0;
85 bca.longInstructions = 0;
90 bca.FunctionInfo.clear();
91 bca.BytecodeDump.clear();
92 bca.BlockSizes[BytecodeFormat::Module] = 0;
93 bca.BlockSizes[BytecodeFormat::Function] = 0;
94 bca.BlockSizes[BytecodeFormat::ConstantPool] = 0;
95 bca.BlockSizes[BytecodeFormat::SymbolTable] = 0;
96 bca.BlockSizes[BytecodeFormat::ModuleGlobalInfo] = 0;
97 bca.BlockSizes[BytecodeFormat::GlobalTypePlane] = 0;
98 bca.BlockSizes[BytecodeFormat::BasicBlock] = 0;
99 bca.BlockSizes[BytecodeFormat::InstructionList] = 0;
100 bca.BlockSizes[BytecodeFormat::CompactionTable] = 0;
103 virtual void handleFinish() {
104 dump << "} End Bytecode\n";
105 bca.BytecodeDump = dump.str() ;
107 bca.fileDensity = double(bca.byteSize) / double( bca.numTypes + bca.numValues );
108 double globalSize = 0.0;
109 globalSize += double(bca.BlockSizes[BytecodeFormat::ConstantPool]);
110 globalSize += double(bca.BlockSizes[BytecodeFormat::ModuleGlobalInfo]);
111 globalSize += double(bca.BlockSizes[BytecodeFormat::GlobalTypePlane]);
112 bca.globalsDensity = globalSize / double( bca.numTypes + bca.numConstants +
114 bca.functionDensity = double(bca.BlockSizes[BytecodeFormat::Function]) /
115 double(bca.numFunctions);
117 if ( bca.progressiveVerify ) {
119 verifyModule(*M, ThrowExceptionAction);
120 } catch ( std::string& msg ) {
121 bca.VerifyInfo += "Verify@Finish: " + msg + "\n";
126 virtual void handleModuleBegin(const std::string& id) {
127 dump << " Module " << id << " {\n";
131 virtual void handleModuleEnd(const std::string& id) {
132 dump << " } End Module " << id << "\n";
133 if ( bca.progressiveVerify ) {
135 verifyModule(*M, ThrowExceptionAction);
136 } catch ( std::string& msg ) {
137 bca.VerifyInfo += "Verify@EndModule: " + msg + "\n";
142 virtual void handleVersionInfo(
143 unsigned char RevisionNum, ///< Byte code revision number
144 Module::Endianness Endianness, ///< Endianness indicator
145 Module::PointerSize PointerSize ///< PointerSize indicator
147 dump << " RevisionNum: " << int(RevisionNum)
148 << " Endianness: " << Endianness
149 << " PointerSize: " << PointerSize << "\n";
152 virtual void handleModuleGlobalsBegin() {
153 dump << " BLOCK: ModuleGlobalInfo {\n";
156 virtual void handleGlobalVariable(
157 const Type* ElemType,
159 GlobalValue::LinkageTypes Linkage,
167 << ( initSlot == 0 ? "Uni" : "I" ) << "nitialized, "
168 << ( isConstant? "Constant, " : "Variable, ")
169 << " Linkage=" << Linkage << " Type="
170 << ElemType->getDescription()
171 << " Slot=" << SlotNum << " InitSlot=" << initSlot
175 virtual void handleType( const Type* Ty ) {
177 dump << " Type: " << Ty->getDescription() << "\n";
180 virtual void handleFunctionDeclaration(
181 Function* Func ///< The function
185 dump << " Function Decl: " << Func->getType()->getDescription() << "\n";
188 virtual void handleGlobalInitializer(GlobalVariable* GV, Constant* CV) {
189 dump << " Initializer: GV=";
196 virtual void handleModuleGlobalsEnd() {
197 dump << " } END BLOCK: ModuleGlobalInfo\n";
198 if ( bca.progressiveVerify ) {
200 verifyModule(*M, ThrowExceptionAction);
201 } catch ( std::string& msg ) {
202 bca.VerifyInfo += "Verify@EndModuleGlobalInfo: " + msg + "\n";
207 virtual void handleCompactionTableBegin() {
208 dump << " BLOCK: CompactionTable {\n";
211 virtual void handleCompactionTablePlane( unsigned Ty, unsigned NumEntries) {
212 bca.numCmpctnTables++;
213 dump << " Plane: Ty=" << Ty << " Size=" << NumEntries << "\n";
216 virtual void handleCompactionTableType( unsigned i, unsigned TypSlot,
218 dump << " Type: " << i << " Slot:" << TypSlot
219 << " is " << Ty->getDescription() << "\n";
222 virtual void handleCompactionTableValue(
227 dump << " Value: " << i << " TypSlot: " << TypSlot
228 << " ValSlot:" << ValSlot << " is " << Ty->getDescription()
232 virtual void handleCompactionTableEnd() {
233 dump << " } END BLOCK: CompactionTable\n";
236 virtual void handleSymbolTableBegin(Function* CF, SymbolTable* ST) {
238 dump << " BLOCK: SymbolTable {\n";
241 virtual void handleSymbolTablePlane(unsigned Ty, unsigned NumEntries,
243 dump << " Plane: Ty=" << Ty << " Size=" << NumEntries
244 << " Type: " << Typ->getDescription() << "\n";
247 virtual void handleSymbolTableType(unsigned i, unsigned slot,
248 const std::string& name ) {
249 dump << " Type " << i << " Slot=" << slot
250 << " Name: " << name << "\n";
253 virtual void handleSymbolTableValue(unsigned i, unsigned slot,
254 const std::string& name ) {
255 dump << " Value " << i << " Slot=" << slot
256 << " Name: " << name << "\n";
259 virtual void handleSymbolTableEnd() {
260 dump << " } END BLOCK: SymbolTable\n";
263 virtual void handleFunctionBegin(Function* Func, unsigned Size) {
264 dump << "BLOCK: Function {\n";
265 dump << " Linkage: " << Func->getLinkage() << "\n";
266 dump << " Type: " << Func->getType()->getDescription() << "\n";
267 const FunctionType* FType =
268 cast<FunctionType>(Func->getType()->getElementType());
269 currFunc = &bca.FunctionInfo[Func];
270 currFunc->description = FType->getDescription();
271 currFunc->name = Func->getName();
272 currFunc->byteSize = Size;
273 currFunc->numInstructions = 0;
274 currFunc->numBasicBlocks = 0;
275 currFunc->numPhis = 0;
276 currFunc->numOperands = 0;
277 currFunc->density = 0.0;
278 currFunc->instructionSize = 0;
279 currFunc->longInstructions = 0;
280 currFunc->vbrCount32 = 0;
281 currFunc->vbrCount64 = 0;
282 currFunc->vbrCompBytes = 0;
283 currFunc->vbrExpdBytes = 0;
287 virtual void handleFunctionEnd( Function* Func) {
288 dump << "} END BLOCK: Function\n";
289 currFunc->density = double(currFunc->byteSize) /
290 double(currFunc->numInstructions+currFunc->numBasicBlocks);
292 if ( bca.progressiveVerify ) {
294 verifyModule(*M, ThrowExceptionAction);
295 } catch ( std::string& msg ) {
296 bca.VerifyInfo += "Verify@EndFunction: " + msg + "\n";
301 virtual void handleBasicBlockBegin( unsigned blocknum) {
302 dump << " BLOCK: BasicBlock #" << blocknum << "{\n";
303 bca.numBasicBlocks++;
305 if ( currFunc ) currFunc->numBasicBlocks++;
308 virtual bool handleInstruction( unsigned Opcode, const Type* iType,
309 std::vector<unsigned>& Operands, unsigned Size){
310 dump << " INST: OpCode="
311 << Instruction::getOpcodeName(Opcode) << " Type="
312 << iType->getDescription() << "\n";
313 for ( unsigned i = 0; i < Operands.size(); ++i )
314 dump << " Op#" << i << " Slot=" << Operands[i] << "\n";
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, "Bytecode Compression Index",std::string("TBD"));
509 print(Out, "Number Of Bytecode Blocks", bca.numBlocks);
510 print(Out, "Number Of Types", bca.numTypes);
511 print(Out, "Number Of Values", bca.numValues);
512 print(Out, "Number Of Constants", bca.numConstants);
513 print(Out, "Number Of Global Variables", bca.numGlobalVars);
514 print(Out, "Number Of Functions", bca.numFunctions);
515 print(Out, "Number Of Basic Blocks", bca.numBasicBlocks);
516 print(Out, "Number Of Instructions", bca.numInstructions);
517 print(Out, "Number Of Operands", bca.numOperands);
518 print(Out, "Number Of Compaction Tables", bca.numCmpctnTables);
519 print(Out, "Number Of Symbol Tables", bca.numSymTab);
520 print(Out, "Long Instructions", bca.longInstructions);
521 print(Out, "Instruction Size", bca.instructionSize);
522 print(Out, "Average Instruction Size",
523 double(bca.instructionSize)/double(bca.numInstructions));
524 print(Out, "Maximum Type Slot Number", bca.maxTypeSlot);
525 print(Out, "Maximum Value Slot Number", bca.maxValueSlot);
526 print(Out, "Bytes Thrown To Alignment", double(bca.numAlignment),
527 double(bca.byteSize));
528 print(Out, "File Density (bytes/def)", bca.fileDensity);
529 print(Out, "Globals Density (bytes/def)", bca.globalsDensity);
530 print(Out, "Function Density (bytes/func)", bca.functionDensity);
531 print(Out, "Number of VBR 32-bit Integers", bca.vbrCount32);
532 print(Out, "Number of VBR 64-bit Integers", bca.vbrCount64);
533 print(Out, "Number of VBR Compressed Bytes", bca.vbrCompBytes);
534 print(Out, "Number of VBR Expanded Bytes", bca.vbrExpdBytes);
535 print(Out, "VBR Savings",
536 double(bca.vbrExpdBytes)-double(bca.vbrCompBytes),
537 double(bca.byteSize));
539 if ( bca.detailedResults ) {
540 print(Out, "Module Bytes",
541 double(bca.BlockSizes[BytecodeFormat::Module]),
542 double(bca.byteSize));
543 print(Out, "Function Bytes",
544 double(bca.BlockSizes[BytecodeFormat::Function]),
545 double(bca.byteSize));
546 print(Out, "Constant Pool Bytes",
547 double(bca.BlockSizes[BytecodeFormat::ConstantPool]),
548 double(bca.byteSize));
549 print(Out, "Symbol Table Bytes",
550 double(bca.BlockSizes[BytecodeFormat::SymbolTable]),
551 double(bca.byteSize));
552 print(Out, "Module Global Info Bytes",
553 double(bca.BlockSizes[BytecodeFormat::ModuleGlobalInfo]),
554 double(bca.byteSize));
555 print(Out, "Global Type Plane Bytes",
556 double(bca.BlockSizes[BytecodeFormat::GlobalTypePlane]),
557 double(bca.byteSize));
558 print(Out, "Basic Block Bytes",
559 double(bca.BlockSizes[BytecodeFormat::BasicBlock]),
560 double(bca.byteSize));
561 print(Out, "Instruction List Bytes",
562 double(bca.BlockSizes[BytecodeFormat::InstructionList]),
563 double(bca.byteSize));
564 print(Out, "Compaction Table Bytes",
565 double(bca.BlockSizes[BytecodeFormat::CompactionTable]),
566 double(bca.byteSize));
568 std::map<const Function*,BytecodeAnalysis::BytecodeFunctionInfo>::iterator I =
569 bca.FunctionInfo.begin();
570 std::map<const Function*,BytecodeAnalysis::BytecodeFunctionInfo>::iterator E =
571 bca.FunctionInfo.end();
574 Out << std::left << std::setw(0);
575 Out << "Function: " << I->second.name << "\n";
576 print(Out, "Type:", I->second.description);
577 print(Out, "Byte Size", I->second.byteSize);
578 print(Out, "Instructions", I->second.numInstructions);
579 print(Out, "Long Instructions", I->second.longInstructions);
580 print(Out, "Instruction Size", I->second.instructionSize);
581 print(Out, "Average Instruction Size",
582 double(I->second.instructionSize)/double(I->second.numInstructions));
583 print(Out, "Basic Blocks", I->second.numBasicBlocks);
584 print(Out, "Operand", I->second.numOperands);
585 print(Out, "Function Density", I->second.density);
586 print(Out, "Number of VBR 32-bit Integers", I->second.vbrCount32);
587 print(Out, "Number of VBR 64-bit Integers", I->second.vbrCount64);
588 print(Out, "Number of VBR Compressed Bytes", I->second.vbrCompBytes);
589 print(Out, "Number of VBR Expanded Bytes", I->second.vbrExpdBytes);
590 print(Out, "VBR Savings",
591 double(I->second.vbrExpdBytes)-double(I->second.vbrCompBytes),
592 double(I->second.byteSize));
597 if ( bca.dumpBytecode )
598 Out << bca.BytecodeDump;
600 if ( bca.progressiveVerify )
601 Out << bca.VerifyInfo;
604 BytecodeHandler* createBytecodeAnalyzerHandler(BytecodeAnalysis& bca)
606 return new AnalyzerHandler(bca);