1 //===-- Analyzer.cpp - Analysis and Dumping of Bytecode ---------*- 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"
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::ostream* os; ///< A convenience for osing 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, std::ostream* output)
54 /// @name BytecodeHandler Implementations
57 virtual void handleError(const std::string& str ) {
59 *os << "ERROR: " << str << "\n";
62 virtual void handleStart( Module* Mod, unsigned theSize ) {
65 *os << "Bytecode {\n";
66 bca.byteSize = theSize;
73 bca.numGlobalVars = 0;
74 bca.numInstructions = 0;
75 bca.numBasicBlocks = 0;
77 bca.numCmpctnTables = 0;
84 bca.fileDensity = 0.0;
85 bca.globalsDensity = 0.0;
86 bca.functionDensity = 0.0;
87 bca.instructionSize = 0;
88 bca.longInstructions = 0;
89 bca.FunctionInfo.clear();
90 bca.BlockSizes[BytecodeFormat::Reserved_DoNotUse] = 0;
91 bca.BlockSizes[BytecodeFormat::ModuleBlockID] = theSize;
92 bca.BlockSizes[BytecodeFormat::FunctionBlockID] = 0;
93 bca.BlockSizes[BytecodeFormat::ConstantPoolBlockID] = 0;
94 bca.BlockSizes[BytecodeFormat::ValueSymbolTableBlockID] = 0;
95 bca.BlockSizes[BytecodeFormat::ModuleGlobalInfoBlockID] = 0;
96 bca.BlockSizes[BytecodeFormat::GlobalTypePlaneBlockID] = 0;
97 bca.BlockSizes[BytecodeFormat::InstructionListBlockID] = 0;
98 bca.BlockSizes[BytecodeFormat::TypeSymbolTableBlockID] = 0;
101 virtual void handleFinish() {
103 *os << "} End Bytecode\n";
105 bca.fileDensity = double(bca.byteSize) / double( bca.numTypes + bca.numValues );
106 double globalSize = 0.0;
107 globalSize += double(bca.BlockSizes[BytecodeFormat::ConstantPoolBlockID]);
108 globalSize += double(bca.BlockSizes[BytecodeFormat::ModuleGlobalInfoBlockID]);
109 globalSize += double(bca.BlockSizes[BytecodeFormat::GlobalTypePlaneBlockID]);
110 bca.globalsDensity = globalSize / double( bca.numTypes + bca.numConstants +
112 bca.functionDensity = double(bca.BlockSizes[BytecodeFormat::FunctionBlockID]) /
113 double(bca.numFunctions);
115 if (bca.progressiveVerify) {
117 if (verifyModule(*M, ReturnStatusAction, &msg))
118 bca.VerifyInfo += "Verify@Finish: " + msg + "\n";
122 virtual void handleModuleBegin(const std::string& id) {
124 *os << " Module " << id << " {\n";
128 virtual void handleModuleEnd(const std::string& id) {
130 *os << " } End Module " << id << "\n";
131 if (bca.progressiveVerify) {
133 if (verifyModule(*M, ReturnStatusAction, &msg))
134 bca.VerifyInfo += "Verify@EndModule: " + msg + "\n";
138 virtual void handleVersionInfo(
139 unsigned char RevisionNum ///< Byte code revision number
142 *os << " RevisionNum: " << int(RevisionNum) << "\n";
143 bca.version = RevisionNum;
146 virtual void handleModuleGlobalsBegin() {
148 *os << " BLOCK: ModuleGlobalInfo {\n";
151 virtual void handleGlobalVariable(
152 const Type* ElemType,
154 GlobalValue::LinkageTypes Linkage,
155 GlobalValue::VisibilityTypes Visibility,
161 << ( initSlot == 0 ? "Uni" : "I" ) << "nitialized, "
162 << ( isConstant? "Constant, " : "Variable, ")
163 << " Linkage=" << Linkage
164 << " Visibility="<< Visibility
166 WriteTypeSymbolic(*os, ElemType, M);
167 *os << " Slot=" << SlotNum << " InitSlot=" << initSlot
173 if (SlotNum > bca.maxValueSlot)
174 bca.maxValueSlot = SlotNum;
175 if (initSlot > bca.maxValueSlot)
176 bca.maxValueSlot = initSlot;
180 virtual void handleTypeList(unsigned numEntries) {
181 bca.maxTypeSlot = numEntries - 1;
184 virtual void handleType( const Type* Ty ) {
188 WriteTypeSymbolic(*os,Ty,M);
193 virtual void handleFunctionDeclaration(
194 Function* Func ///< The function
199 *os << " Function Decl: ";
200 WriteTypeSymbolic(*os,Func->getType(),M);
201 *os <<", Linkage=" << Func->getLinkage();
202 *os <<", Visibility=" << Func->getVisibility();
207 virtual void handleGlobalInitializer(GlobalVariable* GV, Constant* CV) {
209 *os << " Initializer: GV=";
217 virtual void handleDependentLibrary(const std::string& libName) {
219 bca.libSize += libName.size() + (libName.size() < 128 ? 1 : 2);
221 *os << " Library: '" << libName << "'\n";
224 virtual void handleModuleGlobalsEnd() {
226 *os << " } END BLOCK: ModuleGlobalInfo\n";
227 if (bca.progressiveVerify) {
229 if (verifyModule(*M, ReturnStatusAction, &msg))
230 bca.VerifyInfo += "Verify@EndModuleGlobalInfo: " + msg + "\n";
234 virtual void handleCompactionTableBegin() {
236 *os << " BLOCK: CompactionTable {\n";
237 bca.numCmpctnTables++;
240 virtual void handleCompactionTablePlane( unsigned Ty, unsigned NumEntries) {
242 *os << " Plane: Ty=" << Ty << " Size=" << NumEntries << "\n";
245 virtual void handleCompactionTableType( unsigned i, unsigned TypSlot,
248 *os << " Type: " << i << " Slot:" << TypSlot << " is ";
249 WriteTypeSymbolic(*os,Ty,M);
254 virtual void handleCompactionTableValue(unsigned i, unsigned TypSlot,
257 *os << " Value: " << i << " TypSlot: " << TypSlot
258 << " ValSlot:" << ValSlot << "\n";
259 if (ValSlot > bca.maxValueSlot)
260 bca.maxValueSlot = ValSlot;
263 virtual void handleCompactionTableEnd() {
265 *os << " } END BLOCK: CompactionTable\n";
268 virtual void handleTypeSymbolTableBegin(TypeSymbolTable* ST) {
271 *os << " BLOCK: TypeSymbolTable {\n";
273 virtual void handleValueSymbolTableBegin(Function* CF, ValueSymbolTable* ST) {
276 *os << " BLOCK: ValueSymbolTable {\n";
279 virtual void handleSymbolTableType(unsigned i, unsigned TypSlot,
280 const std::string& name ) {
282 *os << " Type " << i << " Slot=" << TypSlot
283 << " Name: " << name << "\n";
286 virtual void handleSymbolTableValue(unsigned TySlot, unsigned ValSlot,
287 const std::string& name) {
289 *os << " Value " << TySlot << " Slot=" << ValSlot
290 << " Name: " << name << "\n";
291 if (ValSlot > bca.maxValueSlot)
292 bca.maxValueSlot = ValSlot;
295 virtual void handleValueSymbolTableEnd() {
297 *os << " } END BLOCK: ValueSymbolTable\n";
300 virtual void handleTypeSymbolTableEnd() {
302 *os << " } END BLOCK: TypeSymbolTable\n";
305 virtual void handleFunctionBegin(Function* Func, unsigned Size) {
307 *os << " BLOCK: Function {\n"
308 << " Linkage: " << Func->getLinkage() << "\n"
309 << " Visibility: " << Func->getVisibility() << "\n"
311 WriteTypeSymbolic(*os,Func->getType(),M);
315 currFunc = &bca.FunctionInfo[Func];
316 std::ostringstream tmp;
317 WriteTypeSymbolic(tmp,Func->getType(),M);
318 currFunc->description = tmp.str();
319 currFunc->name = Func->getName();
320 currFunc->byteSize = Size;
321 currFunc->numInstructions = 0;
322 currFunc->numBasicBlocks = 0;
323 currFunc->numPhis = 0;
324 currFunc->numOperands = 0;
325 currFunc->density = 0.0;
326 currFunc->instructionSize = 0;
327 currFunc->longInstructions = 0;
331 virtual void handleFunctionEnd( Function* Func) {
333 *os << " } END BLOCK: Function\n";
334 currFunc->density = double(currFunc->byteSize) /
335 double(currFunc->numInstructions);
337 if (bca.progressiveVerify) {
339 if (verifyModule(*M, ReturnStatusAction, &msg))
340 bca.VerifyInfo += "Verify@EndFunction: " + msg + "\n";
344 virtual void handleBasicBlockBegin( unsigned blocknum) {
346 *os << " BLOCK: BasicBlock #" << blocknum << "{\n";
347 bca.numBasicBlocks++;
349 if ( currFunc ) currFunc->numBasicBlocks++;
352 virtual bool handleInstruction( unsigned Opcode, const Type* iType,
353 unsigned *Operands, unsigned NumOps,
357 *os << " INST: OpCode="
358 << Instruction::getOpcodeName(Opcode);
359 for (unsigned i = 0; i != NumOps; ++i)
360 *os << " Op(" << Operands[i] << ")";
364 bca.numInstructions++;
366 bca.instructionSize += Size;
367 if (Size > 4 ) bca.longInstructions++;
368 bca.numOperands += NumOps;
369 for (unsigned i = 0; i != NumOps; ++i)
370 if (Operands[i] > bca.maxValueSlot)
371 bca.maxValueSlot = Operands[i];
373 currFunc->numInstructions++;
374 currFunc->instructionSize += Size;
375 if (Size > 4 ) currFunc->longInstructions++;
376 if (Opcode == Instruction::PHI) currFunc->numPhis++;
378 return Instruction::isTerminator(Opcode);
381 virtual void handleBasicBlockEnd(unsigned blocknum) {
383 *os << " } END BLOCK: BasicBlock #" << blocknum << "\n";
386 virtual void handleGlobalConstantsBegin() {
388 *os << " BLOCK: GlobalConstants {\n";
391 virtual void handleConstantExpression(unsigned Opcode,
392 Constant**ArgVec, unsigned NumArgs, Constant* C) {
394 *os << " EXPR: " << Instruction::getOpcodeName(Opcode) << "\n";
395 for ( unsigned i = 0; i != NumArgs; ++i ) {
396 *os << " Arg#" << i << " "; ArgVec[i]->print(*os);
407 virtual void handleConstantValue( Constant * c ) {
417 virtual void handleConstantArray( const ArrayType* AT,
418 Constant**Elements, unsigned NumElts,
420 Constant* ArrayVal ) {
423 WriteTypeSymbolic(*os,AT,M);
424 *os << " TypeSlot=" << TypeSlot << "\n";
425 for (unsigned i = 0; i != NumElts; ++i) {
427 Elements[i]->print(*os);
431 ArrayVal->print(*os);
439 virtual void handleConstantStruct(
440 const StructType* ST,
441 Constant**Elements, unsigned NumElts,
446 WriteTypeSymbolic(*os,ST,M);
448 for ( unsigned i = 0; i != NumElts; ++i) {
449 *os << " #" << i << " "; Elements[i]->print(*os);
453 StructVal->print(*os);
460 virtual void handleConstantPacked(
461 const PackedType* PT,
462 Constant**Elements, unsigned NumElts,
468 WriteTypeSymbolic(*os,PT,M);
469 *os << " TypeSlot=" << TypeSlot << "\n";
470 for ( unsigned i = 0; i != NumElts; ++i ) {
472 Elements[i]->print(*os);
476 PackedVal->print(*os);
484 virtual void handleConstantPointer( const PointerType* PT,
485 unsigned Slot, GlobalValue* GV ) {
488 WriteTypeSymbolic(*os,PT,M);
489 *os << " Slot=" << Slot << " GlobalValue=";
497 virtual void handleConstantString( const ConstantArray* CA ) {
507 virtual void handleGlobalConstantsEnd() {
509 *os << " } END BLOCK: GlobalConstants\n";
511 if (bca.progressiveVerify) {
513 if (verifyModule(*M, ReturnStatusAction, &msg))
514 bca.VerifyInfo += "Verify@EndGlobalConstants: " + msg + "\n";
518 virtual void handleAlignment(unsigned numBytes) {
519 bca.numAlignment += numBytes;
522 virtual void handleBlock(
523 unsigned BType, const unsigned char* StartPtr, unsigned Size) {
525 assert(BType >= BytecodeFormat::ModuleBlockID);
526 assert(BType < BytecodeFormat::NumberOfBlockIDs);
528 llvm::BytecodeFormat::BytecodeBlockIdentifiers(BType)] += Size;
530 if (bca.version < 3) // Check for long block headers versions
531 bca.BlockSizes[llvm::BytecodeFormat::Reserved_DoNotUse] += 8;
533 bca.BlockSizes[llvm::BytecodeFormat::Reserved_DoNotUse] += 4;
537 } // end anonymous namespace
539 /// @brief Utility for printing a titled unsigned value with
540 /// an aligned colon.
541 inline static void print(std::ostream& Out, const char*title,
542 unsigned val, bool nl = true ) {
543 Out << std::setw(30) << std::right << title
544 << std::setw(0) << ": "
545 << std::setw(9) << val << "\n";
548 /// @brief Utility for printing a titled double value with an
550 inline static void print(std::ostream&Out, const char*title,
552 Out << std::setw(30) << std::right << title
553 << std::setw(0) << ": "
554 << std::setw(9) << std::setprecision(6) << val << "\n" ;
557 /// @brief Utility for printing a titled double value with a
558 /// percentage and aligned colon.
559 inline static void print(std::ostream&Out, const char*title,
560 double top, double bot ) {
561 Out << std::setw(30) << std::right << title
562 << std::setw(0) << ": "
563 << std::setw(9) << std::setprecision(6) << top
564 << " (" << std::left << std::setw(0) << std::setprecision(4)
565 << (top/bot)*100.0 << "%)\n";
568 /// @brief Utility for printing a titled string value with
569 /// an aligned colon.
570 inline static void print(std::ostream&Out, const char*title,
571 std::string val, bool nl = true) {
572 Out << std::setw(30) << std::right << title
573 << std::setw(0) << ": "
574 << std::left << val << (nl ? "\n" : "");
577 /// This function prints the contents of rhe BytecodeAnalysis structure in
578 /// a human legible form.
579 /// @brief Print BytecodeAnalysis structure to an ostream
580 void llvm::PrintBytecodeAnalysis(BytecodeAnalysis& bca, std::ostream& Out )
582 Out << "\nSummary Analysis Of " << bca.ModuleId << ": \n\n";
583 print(Out, "Bytecode Analysis Of Module", bca.ModuleId);
584 print(Out, "Bytecode Version Number", bca.version);
585 print(Out, "File Size", bca.byteSize);
586 print(Out, "Module Bytes",
587 double(bca.BlockSizes[BytecodeFormat::ModuleBlockID]),
588 double(bca.byteSize));
589 print(Out, "Function Bytes",
590 double(bca.BlockSizes[BytecodeFormat::FunctionBlockID]),
591 double(bca.byteSize));
592 print(Out, "Global Types Bytes",
593 double(bca.BlockSizes[BytecodeFormat::GlobalTypePlaneBlockID]),
594 double(bca.byteSize));
595 print(Out, "Constant Pool Bytes",
596 double(bca.BlockSizes[BytecodeFormat::ConstantPoolBlockID]),
597 double(bca.byteSize));
598 print(Out, "Module Globals Bytes",
599 double(bca.BlockSizes[BytecodeFormat::ModuleGlobalInfoBlockID]),
600 double(bca.byteSize));
601 print(Out, "Instruction List Bytes",
602 double(bca.BlockSizes[BytecodeFormat::InstructionListBlockID]),
603 double(bca.byteSize));
604 print(Out, "Value Symbol Table Bytes",
605 double(bca.BlockSizes[BytecodeFormat::ValueSymbolTableBlockID]),
606 double(bca.byteSize));
607 print(Out, "Type Symbol Table Bytes",
608 double(bca.BlockSizes[BytecodeFormat::TypeSymbolTableBlockID]),
609 double(bca.byteSize));
610 print(Out, "Alignment Bytes",
611 double(bca.numAlignment), double(bca.byteSize));
612 print(Out, "Block Header Bytes",
613 double(bca.BlockSizes[BytecodeFormat::Reserved_DoNotUse]),
614 double(bca.byteSize));
615 print(Out, "Dependent Libraries Bytes", double(bca.libSize),
616 double(bca.byteSize));
617 print(Out, "Number Of Bytecode Blocks", bca.numBlocks);
618 print(Out, "Number Of Functions", bca.numFunctions);
619 print(Out, "Number Of Types", bca.numTypes);
620 print(Out, "Number Of Constants", bca.numConstants);
621 print(Out, "Number Of Global Variables", bca.numGlobalVars);
622 print(Out, "Number Of Values", bca.numValues);
623 print(Out, "Number Of Basic Blocks", bca.numBasicBlocks);
624 print(Out, "Number Of Instructions", bca.numInstructions);
625 print(Out, "Number Of Long Instructions", bca.longInstructions);
626 print(Out, "Number Of Operands", bca.numOperands);
627 print(Out, "Number Of Compaction Tables", bca.numCmpctnTables);
628 print(Out, "Number Of Symbol Tables", bca.numSymTab);
629 print(Out, "Number Of Dependent Libs", bca.numLibraries);
630 print(Out, "Total Instruction Size", bca.instructionSize);
631 print(Out, "Average Instruction Size",
632 double(bca.instructionSize)/double(bca.numInstructions));
634 print(Out, "Maximum Type Slot Number", bca.maxTypeSlot);
635 print(Out, "Maximum Value Slot Number", bca.maxValueSlot);
636 print(Out, "Bytes Per Value ", bca.fileDensity);
637 print(Out, "Bytes Per Global", bca.globalsDensity);
638 print(Out, "Bytes Per Function", bca.functionDensity);
640 if (bca.detailedResults) {
641 Out << "\nDetailed Analysis Of " << bca.ModuleId << " Functions:\n";
643 std::map<const Function*,BytecodeAnalysis::BytecodeFunctionInfo>::iterator I =
644 bca.FunctionInfo.begin();
645 std::map<const Function*,BytecodeAnalysis::BytecodeFunctionInfo>::iterator E =
646 bca.FunctionInfo.end();
649 Out << std::left << std::setw(0) << "\n";
650 if (I->second.numBasicBlocks == 0) Out << "External ";
651 Out << "Function: " << I->second.name << "\n";
652 print(Out, "Type:", I->second.description);
653 print(Out, "Byte Size", I->second.byteSize);
654 if (I->second.numBasicBlocks) {
655 print(Out, "Basic Blocks", I->second.numBasicBlocks);
656 print(Out, "Instructions", I->second.numInstructions);
657 print(Out, "Long Instructions", I->second.longInstructions);
658 print(Out, "Operands", I->second.numOperands);
659 print(Out, "Instruction Size", I->second.instructionSize);
660 print(Out, "Average Instruction Size",
661 double(I->second.instructionSize) / I->second.numInstructions);
662 print(Out, "Bytes Per Instruction", I->second.density);
668 if ( bca.progressiveVerify )
669 Out << bca.VerifyInfo;
672 // AnalyzeBytecodeFile - analyze one file
673 Module* llvm::AnalyzeBytecodeFile(const std::string &Filename, ///< File to analyze
674 BytecodeAnalysis& bca, ///< Statistical output
675 BCDecompressor_t *BCDC,
676 std::string *ErrMsg, ///< Error output
677 std::ostream* output ///< Dump output
679 BytecodeHandler* AH = new AnalyzerHandler(bca, output);
680 ModuleProvider* MP = getBytecodeModuleProvider(Filename, BCDC, ErrMsg, AH);
682 Module *M = MP->releaseModule(ErrMsg);