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"
25 #include "llvm/Assembly/Writer.h"
34 /// @brief Bytecode reading handler for analyzing bytecode.
35 class AnalyzerHandler : public BytecodeHandler {
36 BytecodeAnalysis& bca; ///< The structure in which data is recorded
37 std::ostream* os; ///< A convenience for osing data.
38 /// @brief Keeps track of current function
39 BytecodeAnalysis::BytecodeFunctionInfo* currFunc;
40 Module* M; ///< Keeps track of current module
45 /// The only way to construct an AnalyzerHandler. All that is needed is a
46 /// reference to the BytecodeAnalysis structure where the output will be
48 AnalyzerHandler(BytecodeAnalysis& TheBca, std::ostream* output)
55 /// @name BytecodeHandler Implementations
58 virtual void handleError(const std::string& str ) {
60 *os << "ERROR: " << str << "\n";
63 virtual void handleStart( Module* Mod, unsigned theSize ) {
66 *os << "Bytecode {\n";
67 bca.byteSize = theSize;
74 bca.numGlobalVars = 0;
75 bca.numInstructions = 0;
76 bca.numBasicBlocks = 0;
78 bca.numCmpctnTables = 0;
85 bca.fileDensity = 0.0;
86 bca.globalsDensity = 0.0;
87 bca.functionDensity = 0.0;
88 bca.instructionSize = 0;
89 bca.longInstructions = 0;
94 bca.FunctionInfo.clear();
95 bca.BlockSizes[BytecodeFormat::Reserved_DoNotUse] = 0;
96 bca.BlockSizes[BytecodeFormat::ModuleBlockID] = theSize;
97 bca.BlockSizes[BytecodeFormat::FunctionBlockID] = 0;
98 bca.BlockSizes[BytecodeFormat::ConstantPoolBlockID] = 0;
99 bca.BlockSizes[BytecodeFormat::ValueSymbolTableBlockID] = 0;
100 bca.BlockSizes[BytecodeFormat::ModuleGlobalInfoBlockID] = 0;
101 bca.BlockSizes[BytecodeFormat::GlobalTypePlaneBlockID] = 0;
102 bca.BlockSizes[BytecodeFormat::InstructionListBlockID] = 0;
103 bca.BlockSizes[BytecodeFormat::TypeSymbolTableBlockID] = 0;
106 virtual void handleFinish() {
108 *os << "} End Bytecode\n";
110 bca.fileDensity = double(bca.byteSize) / double( bca.numTypes + bca.numValues );
111 double globalSize = 0.0;
112 globalSize += double(bca.BlockSizes[BytecodeFormat::ConstantPoolBlockID]);
113 globalSize += double(bca.BlockSizes[BytecodeFormat::ModuleGlobalInfoBlockID]);
114 globalSize += double(bca.BlockSizes[BytecodeFormat::GlobalTypePlaneBlockID]);
115 bca.globalsDensity = globalSize / double( bca.numTypes + bca.numConstants +
117 bca.functionDensity = double(bca.BlockSizes[BytecodeFormat::FunctionBlockID]) /
118 double(bca.numFunctions);
120 if (bca.progressiveVerify) {
122 if (verifyModule(*M, ReturnStatusAction, &msg))
123 bca.VerifyInfo += "Verify@Finish: " + msg + "\n";
127 virtual void handleModuleBegin(const std::string& id) {
129 *os << " Module " << id << " {\n";
133 virtual void handleModuleEnd(const std::string& id) {
135 *os << " } End Module " << id << "\n";
136 if (bca.progressiveVerify) {
138 if (verifyModule(*M, ReturnStatusAction, &msg))
139 bca.VerifyInfo += "Verify@EndModule: " + msg + "\n";
143 virtual void handleVersionInfo(
144 unsigned char RevisionNum ///< Byte code revision number
147 *os << " RevisionNum: " << int(RevisionNum) << "\n";
148 bca.version = RevisionNum;
151 virtual void handleModuleGlobalsBegin() {
153 *os << " BLOCK: ModuleGlobalInfo {\n";
156 virtual void handleGlobalVariable(
157 const Type* ElemType,
159 GlobalValue::LinkageTypes Linkage,
160 GlobalValue::VisibilityTypes Visibility,
166 << ( initSlot == 0 ? "Uni" : "I" ) << "nitialized, "
167 << ( isConstant? "Constant, " : "Variable, ")
168 << " Linkage=" << Linkage
169 << " Visibility="<< Visibility
171 WriteTypeSymbolic(*os, ElemType, M);
172 *os << " Slot=" << SlotNum << " InitSlot=" << initSlot
178 if (SlotNum > bca.maxValueSlot)
179 bca.maxValueSlot = SlotNum;
180 if (initSlot > bca.maxValueSlot)
181 bca.maxValueSlot = initSlot;
185 virtual void handleTypeList(unsigned numEntries) {
186 bca.maxTypeSlot = numEntries - 1;
189 virtual void handleType( const Type* Ty ) {
193 WriteTypeSymbolic(*os,Ty,M);
198 virtual void handleFunctionDeclaration(
199 Function* Func ///< The function
204 *os << " Function Decl: ";
205 WriteTypeSymbolic(*os,Func->getType(),M);
206 *os <<", Linkage=" << Func->getLinkage();
207 *os <<", Visibility=" << Func->getVisibility();
212 virtual void handleGlobalInitializer(GlobalVariable* GV, Constant* CV) {
214 *os << " Initializer: GV=";
222 virtual void handleDependentLibrary(const std::string& libName) {
224 bca.libSize += libName.size() + (libName.size() < 128 ? 1 : 2);
226 *os << " Library: '" << libName << "'\n";
229 virtual void handleModuleGlobalsEnd() {
231 *os << " } END BLOCK: ModuleGlobalInfo\n";
232 if (bca.progressiveVerify) {
234 if (verifyModule(*M, ReturnStatusAction, &msg))
235 bca.VerifyInfo += "Verify@EndModuleGlobalInfo: " + msg + "\n";
239 virtual void handleCompactionTableBegin() {
241 *os << " BLOCK: CompactionTable {\n";
242 bca.numCmpctnTables++;
245 virtual void handleCompactionTablePlane( unsigned Ty, unsigned NumEntries) {
247 *os << " Plane: Ty=" << Ty << " Size=" << NumEntries << "\n";
250 virtual void handleCompactionTableType( unsigned i, unsigned TypSlot,
253 *os << " Type: " << i << " Slot:" << TypSlot << " is ";
254 WriteTypeSymbolic(*os,Ty,M);
259 virtual void handleCompactionTableValue(unsigned i, unsigned TypSlot,
262 *os << " Value: " << i << " TypSlot: " << TypSlot
263 << " ValSlot:" << ValSlot << "\n";
264 if (ValSlot > bca.maxValueSlot)
265 bca.maxValueSlot = ValSlot;
268 virtual void handleCompactionTableEnd() {
270 *os << " } END BLOCK: CompactionTable\n";
273 virtual void handleTypeSymbolTableBegin(TypeSymbolTable* ST) {
276 *os << " BLOCK: TypeSymbolTable {\n";
278 virtual void handleValueSymbolTableBegin(Function* CF, ValueSymbolTable* ST) {
281 *os << " BLOCK: ValueSymbolTable {\n";
284 virtual void handleSymbolTableType(unsigned i, unsigned TypSlot,
285 const std::string& name ) {
287 *os << " Type " << i << " Slot=" << TypSlot
288 << " Name: " << name << "\n";
291 virtual void handleSymbolTableValue(unsigned TySlot, unsigned ValSlot,
292 const std::string& name) {
294 *os << " Value " << TySlot << " Slot=" << ValSlot
295 << " Name: " << name << "\n";
296 if (ValSlot > bca.maxValueSlot)
297 bca.maxValueSlot = ValSlot;
300 virtual void handleValueSymbolTableEnd() {
302 *os << " } END BLOCK: ValueSymbolTable\n";
305 virtual void handleTypeSymbolTableEnd() {
307 *os << " } END BLOCK: TypeSymbolTable\n";
310 virtual void handleFunctionBegin(Function* Func, unsigned Size) {
312 *os << " BLOCK: Function {\n"
313 << " Linkage: " << Func->getLinkage() << "\n"
314 << " Visibility: " << Func->getVisibility() << "\n"
316 WriteTypeSymbolic(*os,Func->getType(),M);
320 currFunc = &bca.FunctionInfo[Func];
321 std::ostringstream tmp;
322 WriteTypeSymbolic(tmp,Func->getType(),M);
323 currFunc->description = tmp.str();
324 currFunc->name = Func->getName();
325 currFunc->byteSize = Size;
326 currFunc->numInstructions = 0;
327 currFunc->numBasicBlocks = 0;
328 currFunc->numPhis = 0;
329 currFunc->numOperands = 0;
330 currFunc->density = 0.0;
331 currFunc->instructionSize = 0;
332 currFunc->longInstructions = 0;
333 currFunc->vbrCount32 = 0;
334 currFunc->vbrCount64 = 0;
335 currFunc->vbrCompBytes = 0;
336 currFunc->vbrExpdBytes = 0;
340 virtual void handleFunctionEnd( Function* Func) {
342 *os << " } END BLOCK: Function\n";
343 currFunc->density = double(currFunc->byteSize) /
344 double(currFunc->numInstructions);
346 if (bca.progressiveVerify) {
348 if (verifyModule(*M, ReturnStatusAction, &msg))
349 bca.VerifyInfo += "Verify@EndFunction: " + msg + "\n";
353 virtual void handleBasicBlockBegin( unsigned blocknum) {
355 *os << " BLOCK: BasicBlock #" << blocknum << "{\n";
356 bca.numBasicBlocks++;
358 if ( currFunc ) currFunc->numBasicBlocks++;
361 virtual bool handleInstruction( unsigned Opcode, const Type* iType,
362 std::vector<unsigned>& Operands,
366 *os << " INST: OpCode="
367 << Instruction::getOpcodeName(Opcode);
368 for ( unsigned i = 0; i < Operands.size(); ++i )
369 *os << " Op(" << Operands[i] << ")";
373 bca.numInstructions++;
375 bca.instructionSize += Size;
376 if (Size > 4 ) bca.longInstructions++;
377 bca.numOperands += Operands.size();
378 for (unsigned i = 0; i < Operands.size(); ++i )
379 if (Operands[i] > bca.maxValueSlot)
380 bca.maxValueSlot = Operands[i];
382 currFunc->numInstructions++;
383 currFunc->instructionSize += Size;
384 if (Size > 4 ) currFunc->longInstructions++;
385 if ( Opcode == Instruction::PHI ) currFunc->numPhis++;
387 return Instruction::isTerminator(Opcode);
390 virtual void handleBasicBlockEnd(unsigned blocknum) {
392 *os << " } END BLOCK: BasicBlock #" << blocknum << "\n";
395 virtual void handleGlobalConstantsBegin() {
397 *os << " BLOCK: GlobalConstants {\n";
400 virtual void handleConstantExpression( unsigned Opcode,
401 std::vector<Constant*> ArgVec, Constant* C ) {
403 *os << " EXPR: " << Instruction::getOpcodeName(Opcode) << "\n";
404 for ( unsigned i = 0; i < ArgVec.size(); ++i ) {
405 *os << " Arg#" << i << " "; ArgVec[i]->print(*os);
416 virtual void handleConstantValue( Constant * c ) {
426 virtual void handleConstantArray( const ArrayType* AT,
427 std::vector<Constant*>& Elements,
429 Constant* ArrayVal ) {
432 WriteTypeSymbolic(*os,AT,M);
433 *os << " TypeSlot=" << TypeSlot << "\n";
434 for ( unsigned i = 0; i < Elements.size(); ++i ) {
436 Elements[i]->print(*os);
440 ArrayVal->print(*os);
448 virtual void handleConstantStruct(
449 const StructType* ST,
450 std::vector<Constant*>& Elements,
455 WriteTypeSymbolic(*os,ST,M);
457 for ( unsigned i = 0; i < Elements.size(); ++i ) {
458 *os << " #" << i << " "; Elements[i]->print(*os);
462 StructVal->print(*os);
469 virtual void handleConstantPacked(
470 const PackedType* PT,
471 std::vector<Constant*>& Elements,
477 WriteTypeSymbolic(*os,PT,M);
478 *os << " TypeSlot=" << TypeSlot << "\n";
479 for ( unsigned i = 0; i < Elements.size(); ++i ) {
481 Elements[i]->print(*os);
485 PackedVal->print(*os);
493 virtual void handleConstantPointer( const PointerType* PT,
494 unsigned Slot, GlobalValue* GV ) {
497 WriteTypeSymbolic(*os,PT,M);
498 *os << " Slot=" << Slot << " GlobalValue=";
506 virtual void handleConstantString( const ConstantArray* CA ) {
516 virtual void handleGlobalConstantsEnd() {
518 *os << " } END BLOCK: GlobalConstants\n";
520 if (bca.progressiveVerify) {
522 if (verifyModule(*M, ReturnStatusAction, &msg))
523 bca.VerifyInfo += "Verify@EndGlobalConstants: " + msg + "\n";
527 virtual void handleAlignment(unsigned numBytes) {
528 bca.numAlignment += numBytes;
531 virtual void handleBlock(
532 unsigned BType, const unsigned char* StartPtr, unsigned Size) {
534 assert(BType >= BytecodeFormat::ModuleBlockID);
535 assert(BType < BytecodeFormat::NumberOfBlockIDs);
537 llvm::BytecodeFormat::BytecodeBlockIdentifiers(BType)] += Size;
539 if (bca.version < 3) // Check for long block headers versions
540 bca.BlockSizes[llvm::BytecodeFormat::Reserved_DoNotUse] += 8;
542 bca.BlockSizes[llvm::BytecodeFormat::Reserved_DoNotUse] += 4;
545 virtual void handleVBR32(unsigned Size ) {
547 bca.vbrCompBytes += Size;
548 bca.vbrExpdBytes += sizeof(uint32_t);
550 currFunc->vbrCount32++;
551 currFunc->vbrCompBytes += Size;
552 currFunc->vbrExpdBytes += sizeof(uint32_t);
556 virtual void handleVBR64(unsigned Size ) {
558 bca.vbrCompBytes += Size;
559 bca.vbrExpdBytes += sizeof(uint64_t);
561 currFunc->vbrCount64++;
562 currFunc->vbrCompBytes += Size;
563 currFunc->vbrExpdBytes += sizeof(uint64_t);
569 /// @brief Utility for printing a titled unsigned value with
570 /// an aligned colon.
571 inline static void print(std::ostream& Out, const char*title,
572 unsigned val, bool nl = true ) {
573 Out << std::setw(30) << std::right << title
574 << std::setw(0) << ": "
575 << std::setw(9) << val << "\n";
578 /// @brief Utility for printing a titled double value with an
580 inline static void print(std::ostream&Out, const char*title,
582 Out << std::setw(30) << std::right << title
583 << std::setw(0) << ": "
584 << std::setw(9) << std::setprecision(6) << val << "\n" ;
587 /// @brief Utility for printing a titled double value with a
588 /// percentage and aligned colon.
589 inline static void print(std::ostream&Out, const char*title,
590 double top, double bot ) {
591 Out << std::setw(30) << std::right << title
592 << std::setw(0) << ": "
593 << std::setw(9) << std::setprecision(6) << top
594 << " (" << std::left << std::setw(0) << std::setprecision(4)
595 << (top/bot)*100.0 << "%)\n";
598 /// @brief Utility for printing a titled string value with
599 /// an aligned colon.
600 inline static void print(std::ostream&Out, const char*title,
601 std::string val, bool nl = true) {
602 Out << std::setw(30) << std::right << title
603 << std::setw(0) << ": "
604 << std::left << val << (nl ? "\n" : "");
611 /// This function prints the contents of rhe BytecodeAnalysis structure in
612 /// a human legible form.
613 /// @brief Print BytecodeAnalysis structure to an ostream
614 void PrintBytecodeAnalysis(BytecodeAnalysis& bca, std::ostream& Out )
616 Out << "\nSummary Analysis Of " << bca.ModuleId << ": \n\n";
617 print(Out, "Bytecode Analysis Of Module", bca.ModuleId);
618 print(Out, "Bytecode Version Number", bca.version);
619 print(Out, "File Size", bca.byteSize);
620 print(Out, "Module Bytes",
621 double(bca.BlockSizes[BytecodeFormat::ModuleBlockID]),
622 double(bca.byteSize));
623 print(Out, "Function Bytes",
624 double(bca.BlockSizes[BytecodeFormat::FunctionBlockID]),
625 double(bca.byteSize));
626 print(Out, "Global Types Bytes",
627 double(bca.BlockSizes[BytecodeFormat::GlobalTypePlaneBlockID]),
628 double(bca.byteSize));
629 print(Out, "Constant Pool Bytes",
630 double(bca.BlockSizes[BytecodeFormat::ConstantPoolBlockID]),
631 double(bca.byteSize));
632 print(Out, "Module Globals Bytes",
633 double(bca.BlockSizes[BytecodeFormat::ModuleGlobalInfoBlockID]),
634 double(bca.byteSize));
635 print(Out, "Instruction List Bytes",
636 double(bca.BlockSizes[BytecodeFormat::InstructionListBlockID]),
637 double(bca.byteSize));
638 print(Out, "Value Symbol Table Bytes",
639 double(bca.BlockSizes[BytecodeFormat::ValueSymbolTableBlockID]),
640 double(bca.byteSize));
641 print(Out, "Type Symbol Table Bytes",
642 double(bca.BlockSizes[BytecodeFormat::TypeSymbolTableBlockID]),
643 double(bca.byteSize));
644 print(Out, "Alignment Bytes",
645 double(bca.numAlignment), double(bca.byteSize));
646 print(Out, "Block Header Bytes",
647 double(bca.BlockSizes[BytecodeFormat::Reserved_DoNotUse]),
648 double(bca.byteSize));
649 print(Out, "Dependent Libraries Bytes", double(bca.libSize),
650 double(bca.byteSize));
651 print(Out, "Number Of Bytecode Blocks", bca.numBlocks);
652 print(Out, "Number Of Functions", bca.numFunctions);
653 print(Out, "Number Of Types", bca.numTypes);
654 print(Out, "Number Of Constants", bca.numConstants);
655 print(Out, "Number Of Global Variables", bca.numGlobalVars);
656 print(Out, "Number Of Values", bca.numValues);
657 print(Out, "Number Of Basic Blocks", bca.numBasicBlocks);
658 print(Out, "Number Of Instructions", bca.numInstructions);
659 print(Out, "Number Of Long Instructions", bca.longInstructions);
660 print(Out, "Number Of Operands", bca.numOperands);
661 print(Out, "Number Of Compaction Tables", bca.numCmpctnTables);
662 print(Out, "Number Of Symbol Tables", bca.numSymTab);
663 print(Out, "Number Of Dependent Libs", bca.numLibraries);
664 print(Out, "Total Instruction Size", bca.instructionSize);
665 print(Out, "Average Instruction Size",
666 double(bca.instructionSize)/double(bca.numInstructions));
668 print(Out, "Maximum Type Slot Number", bca.maxTypeSlot);
669 print(Out, "Maximum Value Slot Number", bca.maxValueSlot);
670 print(Out, "Bytes Per Value ", bca.fileDensity);
671 print(Out, "Bytes Per Global", bca.globalsDensity);
672 print(Out, "Bytes Per Function", bca.functionDensity);
673 print(Out, "# of VBR 32-bit Integers", bca.vbrCount32);
674 print(Out, "# of VBR 64-bit Integers", bca.vbrCount64);
675 print(Out, "# of VBR Compressed Bytes", bca.vbrCompBytes);
676 print(Out, "# of VBR Expanded Bytes", bca.vbrExpdBytes);
677 print(Out, "Bytes Saved With VBR",
678 double(bca.vbrExpdBytes)-double(bca.vbrCompBytes),
679 double(bca.vbrExpdBytes));
681 if (bca.detailedResults) {
682 Out << "\nDetailed Analysis Of " << bca.ModuleId << " Functions:\n";
684 std::map<const Function*,BytecodeAnalysis::BytecodeFunctionInfo>::iterator I =
685 bca.FunctionInfo.begin();
686 std::map<const Function*,BytecodeAnalysis::BytecodeFunctionInfo>::iterator E =
687 bca.FunctionInfo.end();
690 Out << std::left << std::setw(0) << "\n";
691 if (I->second.numBasicBlocks == 0) Out << "External ";
692 Out << "Function: " << I->second.name << "\n";
693 print(Out, "Type:", I->second.description);
694 print(Out, "Byte Size", I->second.byteSize);
695 if (I->second.numBasicBlocks) {
696 print(Out, "Basic Blocks", I->second.numBasicBlocks);
697 print(Out, "Instructions", I->second.numInstructions);
698 print(Out, "Long Instructions", I->second.longInstructions);
699 print(Out, "Operands", I->second.numOperands);
700 print(Out, "Instruction Size", I->second.instructionSize);
701 print(Out, "Average Instruction Size",
702 double(I->second.instructionSize) / I->second.numInstructions);
703 print(Out, "Bytes Per Instruction", I->second.density);
704 print(Out, "# of VBR 32-bit Integers", I->second.vbrCount32);
705 print(Out, "# of VBR 64-bit Integers", I->second.vbrCount64);
706 print(Out, "# of VBR Compressed Bytes", I->second.vbrCompBytes);
707 print(Out, "# of VBR Expanded Bytes", I->second.vbrExpdBytes);
708 print(Out, "Bytes Saved With VBR",
709 double(I->second.vbrExpdBytes) - I->second.vbrCompBytes);
715 if ( bca.progressiveVerify )
716 Out << bca.VerifyInfo;
719 BytecodeHandler* createBytecodeAnalyzerHandler(BytecodeAnalysis& bca,
720 std::ostream* output)
722 return new AnalyzerHandler(bca,output);