Implement review feedback. Aliasees can be either GlobalValue's or
[oota-llvm.git] / lib / Bytecode / Reader / Analyzer.cpp
1 //===-- Analyzer.cpp - Analysis and Dumping of Bytecode ---------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
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.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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
16 //
17 //===----------------------------------------------------------------------===//
18
19 #include "Reader.h"
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"
26 #include <iomanip>
27 #include <sstream>
28 #include <ios>
29 using namespace llvm;
30
31 namespace {
32
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
40
41 /// @name Constructor
42 /// @{
43 public:
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
46   /// placed.
47   AnalyzerHandler(BytecodeAnalysis& TheBca, std::ostream* output)
48     : bca(TheBca)
49     , os(output)
50     , currFunc(0)
51     { }
52
53 /// @}
54 /// @name BytecodeHandler Implementations
55 /// @{
56 public:
57   virtual void handleError(const std::string& str ) {
58     if (os)
59       *os << "ERROR: " << str << "\n";
60   }
61
62   virtual void handleStart( Module* Mod, unsigned theSize ) {
63     M = Mod;
64     if (os)
65       *os << "Bytecode {\n";
66     bca.byteSize = theSize;
67     bca.ModuleId.clear();
68     bca.numBlocks = 0;
69     bca.numTypes = 0;
70     bca.numValues = 0;
71     bca.numFunctions = 0;
72     bca.numConstants = 0;
73     bca.numGlobalVars = 0;
74     bca.numInstructions = 0;
75     bca.numBasicBlocks = 0;
76     bca.numOperands = 0;
77     bca.numCmpctnTables = 0;
78     bca.numSymTab = 0;
79     bca.numLibraries = 0;
80     bca.libSize = 0;
81     bca.maxTypeSlot = 0;
82     bca.maxValueSlot = 0;
83     bca.numAlignment = 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;
99   }
100
101   virtual void handleFinish() {
102     if (os)
103       *os << "} End Bytecode\n";
104
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 +
111       bca.numGlobalVars );
112     bca.functionDensity = double(bca.BlockSizes[BytecodeFormat::FunctionBlockID]) /
113       double(bca.numFunctions);
114
115     if (bca.progressiveVerify) {
116       std::string msg;
117       if (verifyModule(*M, ReturnStatusAction, &msg))
118         bca.VerifyInfo += "Verify@Finish: " + msg + "\n";
119     }
120   }
121
122   virtual void handleModuleBegin(const std::string& id) {
123     if (os)
124       *os << "  Module " << id << " {\n";
125     bca.ModuleId = id;
126   }
127
128   virtual void handleModuleEnd(const std::string& id) {
129     if (os)
130       *os << "  } End Module " << id << "\n";
131     if (bca.progressiveVerify) {
132       std::string msg;
133       if (verifyModule(*M, ReturnStatusAction, &msg))
134         bca.VerifyInfo += "Verify@EndModule: " + msg + "\n";
135     }
136   }
137
138   virtual void handleVersionInfo(
139     unsigned char RevisionNum        ///< Byte code revision number
140   ) {
141     if (os)
142       *os << "    RevisionNum: " << int(RevisionNum) << "\n";
143     bca.version = RevisionNum;
144   }
145
146   virtual void handleModuleGlobalsBegin() {
147     if (os)
148       *os << "    BLOCK: ModuleGlobalInfo {\n";
149   }
150
151   virtual void handleGlobalVariable(
152     const Type* ElemType,
153     bool isConstant,
154     GlobalValue::LinkageTypes Linkage,
155     GlobalValue::VisibilityTypes Visibility,
156     unsigned SlotNum,
157     unsigned initSlot,
158     bool isThreadLocal
159   ) {
160     if (os) {
161       *os << "      GV: "
162           << ( initSlot == 0 ? "Uni" : "I" ) << "nitialized, "
163           << ( isConstant? "Constant, " : "Variable, ")
164           << " Thread Local = " << ( isThreadLocal? "yes, " : "no, ")
165           << " Linkage=" << Linkage
166           << " Visibility="<< Visibility
167           << " Type=";
168       //WriteTypeSymbolic(*os, ElemType, M);
169       *os << " Slot=" << SlotNum << " InitSlot=" << initSlot
170           << "\n";
171     }
172
173     bca.numGlobalVars++;
174     bca.numValues++;
175     if (SlotNum > bca.maxValueSlot)
176       bca.maxValueSlot = SlotNum;
177     if (initSlot > bca.maxValueSlot)
178       bca.maxValueSlot = initSlot;
179
180   }
181
182   virtual void handleGlobalAlias(
183     const Type* ElemType,
184     GlobalValue::LinkageTypes Linkage,
185     unsigned TypeSlotNum,
186     unsigned AliaseeSlot) {
187     if (os) {
188       *os << "      GA: "
189           << " Linkage=" << Linkage
190           << " Type=";
191       //WriteTypeSymbolic(*os, ElemType, M);
192       *os << " Slot=" << TypeSlotNum << " AliaseeSlot=" << AliaseeSlot
193           << "\n";
194     }
195
196     bca.numValues++;
197     if (TypeSlotNum > bca.maxValueSlot)
198       bca.maxValueSlot = TypeSlotNum;
199     if (AliaseeSlot > bca.maxValueSlot)
200       bca.maxValueSlot = AliaseeSlot;
201   }
202
203   virtual void handleTypeList(unsigned numEntries) {
204     bca.maxTypeSlot = numEntries - 1;
205   }
206
207   virtual void handleType( const Type* Ty ) {
208     bca.numTypes++;
209     if (os) {
210       *os << "      Type: ";
211       //WriteTypeSymbolic(*os,Ty,M);
212       *os << "\n";
213     }
214   }
215
216   virtual void handleFunctionDeclaration(
217     Function* Func            ///< The function
218   ) {
219     bca.numFunctions++;
220     bca.numValues++;
221     if (os) {
222       *os << "      Function Decl: ";
223       //WriteTypeSymbolic(*os,Func->getType(),M);
224       *os <<", Linkage=" << Func->getLinkage();
225       *os <<", Visibility=" << Func->getVisibility();
226       *os << "\n";
227     }
228   }
229
230   virtual void handleGlobalInitializer(GlobalVariable* GV, Constant* CV) {
231     if (os) {
232       *os << "    Initializer: GV=";
233       GV->print(*os);
234       *os << "      CV=";
235       CV->print(*os);
236       *os << "\n";
237     }
238   }
239
240   virtual void handleDependentLibrary(const std::string& libName) {
241     bca.numLibraries++;
242     bca.libSize += libName.size() + (libName.size() < 128 ? 1 : 2);
243     if (os)
244       *os << "      Library: '" << libName << "'\n";
245   }
246
247   virtual void handleModuleGlobalsEnd() {
248     if (os)
249       *os << "    } END BLOCK: ModuleGlobalInfo\n";
250     if (bca.progressiveVerify) {
251       std::string msg;
252       if (verifyModule(*M, ReturnStatusAction, &msg))
253         bca.VerifyInfo += "Verify@EndModuleGlobalInfo: " + msg + "\n";
254     }
255   }
256
257   virtual void handleTypeSymbolTableBegin(TypeSymbolTable* ST) {
258     bca.numSymTab++;
259     if (os)
260       *os << "    BLOCK: TypeSymbolTable {\n";
261   }
262   virtual void handleValueSymbolTableBegin(Function* CF, ValueSymbolTable* ST) {
263     bca.numSymTab++;
264     if (os)
265       *os << "    BLOCK: ValueSymbolTable {\n";
266   }
267
268   virtual void handleSymbolTableType(unsigned i, unsigned TypSlot,
269     const std::string& name ) {
270     if (os)
271       *os << "        Type " << i << " Slot=" << TypSlot
272          << " Name: " << name << "\n";
273   }
274
275   virtual void handleSymbolTableValue(unsigned TySlot, unsigned ValSlot, 
276                                       const char *Name, unsigned NameLen) {
277     if (os)
278       *os << "        Value " << TySlot << " Slot=" << ValSlot
279           << " Name: " << std::string(Name, Name+NameLen) << "\n";
280     if (ValSlot > bca.maxValueSlot)
281       bca.maxValueSlot = ValSlot;
282   }
283
284   virtual void handleValueSymbolTableEnd() {
285     if (os)
286       *os << "    } END BLOCK: ValueSymbolTable\n";
287   }
288
289   virtual void handleTypeSymbolTableEnd() {
290     if (os)
291       *os << "    } END BLOCK: TypeSymbolTable\n";
292   }
293
294   virtual void handleFunctionBegin(Function* Func, unsigned Size) {
295     if (os) {
296       *os << "    BLOCK: Function {\n"
297           << "      Linkage: " << Func->getLinkage() << "\n"
298           << "      Visibility: " << Func->getVisibility() << "\n"
299           << "      Type: ";
300       //WriteTypeSymbolic(*os,Func->getType(),M);
301       *os << "\n";
302     }
303
304     currFunc = &bca.FunctionInfo[Func];
305     std::ostringstream tmp;
306     //WriteTypeSymbolic(tmp,Func->getType(),M);
307     currFunc->description = tmp.str();
308     currFunc->name = Func->getName();
309     currFunc->byteSize = Size;
310     currFunc->numInstructions = 0;
311     currFunc->numBasicBlocks = 0;
312     currFunc->numPhis = 0;
313     currFunc->numOperands = 0;
314     currFunc->density = 0.0;
315     currFunc->instructionSize = 0;
316     currFunc->longInstructions = 0;
317
318   }
319
320   virtual void handleFunctionEnd( Function* Func) {
321     if (os)
322       *os << "    } END BLOCK: Function\n";
323     currFunc->density = double(currFunc->byteSize) /
324       double(currFunc->numInstructions);
325
326     if (bca.progressiveVerify) {
327       std::string msg;
328       if (verifyModule(*M, ReturnStatusAction, &msg))
329         bca.VerifyInfo += "Verify@EndFunction: " + msg + "\n";
330     }
331   }
332
333   virtual void handleBasicBlockBegin( unsigned blocknum) {
334     if (os)
335       *os << "      BLOCK: BasicBlock #" << blocknum << "{\n";
336     bca.numBasicBlocks++;
337     bca.numValues++;
338     if ( currFunc ) currFunc->numBasicBlocks++;
339   }
340
341   virtual bool handleInstruction( unsigned Opcode, const Type* iType,
342                                 unsigned *Operands, unsigned NumOps, 
343                                 Instruction *Inst,
344                                 unsigned Size){
345     if (os) {
346       *os << "        INST: OpCode="
347          << Instruction::getOpcodeName(Opcode);
348       for (unsigned i = 0; i != NumOps; ++i)
349         *os << " Op(" << Operands[i] << ")";
350       *os << *Inst;
351     }
352
353     bca.numInstructions++;
354     bca.numValues++;
355     bca.instructionSize += Size;
356     if (Size > 4 ) bca.longInstructions++;
357     bca.numOperands += NumOps;
358     for (unsigned i = 0; i != NumOps; ++i)
359       if (Operands[i] > bca.maxValueSlot)
360         bca.maxValueSlot = Operands[i];
361     if ( currFunc ) {
362       currFunc->numInstructions++;
363       currFunc->instructionSize += Size;
364       if (Size > 4 ) currFunc->longInstructions++;
365       if (Opcode == Instruction::PHI) currFunc->numPhis++;
366     }
367     return Instruction::isTerminator(Opcode);
368   }
369
370   virtual void handleBasicBlockEnd(unsigned blocknum) {
371     if (os)
372       *os << "      } END BLOCK: BasicBlock #" << blocknum << "\n";
373   }
374
375   virtual void handleGlobalConstantsBegin() {
376     if (os)
377       *os << "    BLOCK: GlobalConstants {\n";
378   }
379
380   virtual void handleConstantExpression(unsigned Opcode,
381       Constant**ArgVec, unsigned NumArgs, Constant* C) {
382     if (os) {
383       *os << "      EXPR: " << Instruction::getOpcodeName(Opcode) << "\n";
384       for ( unsigned i = 0; i != NumArgs; ++i ) {
385         *os << "        Arg#" << i << " "; ArgVec[i]->print(*os);
386         *os << "\n";
387       }
388       *os << "        Value=";
389       C->print(*os);
390       *os << "\n";
391     }
392     bca.numConstants++;
393     bca.numValues++;
394   }
395
396   virtual void handleConstantValue( Constant * c ) {
397     if (os) {
398       *os << "      VALUE: ";
399       c->print(*os);
400       *os << "\n";
401     }
402     bca.numConstants++;
403     bca.numValues++;
404   }
405
406   virtual void handleConstantArray( const ArrayType* AT,
407           Constant**Elements, unsigned NumElts,
408           unsigned TypeSlot,
409           Constant* ArrayVal ) {
410     if (os) {
411       *os << "      ARRAY: ";
412       //WriteTypeSymbolic(*os,AT,M);
413       *os << " TypeSlot=" << TypeSlot << "\n";
414       for (unsigned i = 0; i != NumElts; ++i) {
415         *os << "        #" << i;
416         Elements[i]->print(*os);
417         *os << "\n";
418       }
419       *os << "        Value=";
420       ArrayVal->print(*os);
421       *os << "\n";
422     }
423
424     bca.numConstants++;
425     bca.numValues++;
426   }
427
428   virtual void handleConstantStruct(
429         const StructType* ST,
430         Constant**Elements, unsigned NumElts,
431         Constant* StructVal)
432   {
433     if (os) {
434       *os << "      STRUC: ";
435       //WriteTypeSymbolic(*os,ST,M);
436       *os << "\n";
437       for ( unsigned i = 0; i != NumElts; ++i) {
438         *os << "        #" << i << " "; Elements[i]->print(*os);
439         *os << "\n";
440       }
441       *os << "        Value=";
442       StructVal->print(*os);
443       *os << "\n";
444     }
445     bca.numConstants++;
446     bca.numValues++;
447   }
448
449   virtual void handleConstantVector(
450     const VectorType* PT,
451     Constant**Elements, unsigned NumElts,
452     unsigned TypeSlot,
453     Constant* VectorVal)
454   {
455     if (os) {
456       *os << "      PACKD: ";
457       //WriteTypeSymbolic(*os,PT,M);
458       *os << " TypeSlot=" << TypeSlot << "\n";
459       for ( unsigned i = 0; i != NumElts; ++i ) {
460         *os << "        #" << i;
461         Elements[i]->print(*os);
462         *os << "\n";
463       }
464       *os << "        Value=";
465       VectorVal->print(*os);
466       *os << "\n";
467     }
468
469     bca.numConstants++;
470     bca.numValues++;
471   }
472
473   virtual void handleConstantPointer( const PointerType* PT,
474       unsigned Slot, GlobalValue* GV ) {
475     if (os) {
476       *os << "       PNTR: ";
477       //WriteTypeSymbolic(*os,PT,M);
478       *os << " Slot=" << Slot << " GlobalValue=";
479       GV->print(*os);
480       *os << "\n";
481     }
482     bca.numConstants++;
483     bca.numValues++;
484   }
485
486   virtual void handleConstantString( const ConstantArray* CA ) {
487     if (os) {
488       *os << "      STRNG: ";
489       CA->print(*os);
490       *os << "\n";
491     }
492     bca.numConstants++;
493     bca.numValues++;
494   }
495
496   virtual void handleGlobalConstantsEnd() {
497     if (os)
498       *os << "    } END BLOCK: GlobalConstants\n";
499
500     if (bca.progressiveVerify) {
501       std::string msg;
502       if (verifyModule(*M, ReturnStatusAction, &msg))
503         bca.VerifyInfo += "Verify@EndGlobalConstants: " + msg + "\n";
504     }
505   }
506
507   virtual void handleAlignment(unsigned numBytes) {
508     bca.numAlignment += numBytes;
509   }
510
511   virtual void handleBlock(
512     unsigned BType, const unsigned char* StartPtr, unsigned Size) {
513     bca.numBlocks++;
514     assert(BType >= BytecodeFormat::ModuleBlockID);
515     assert(BType < BytecodeFormat::NumberOfBlockIDs);
516     bca.BlockSizes[
517       llvm::BytecodeFormat::BytecodeBlockIdentifiers(BType)] += Size;
518
519     if (bca.version < 3) // Check for long block headers versions
520       bca.BlockSizes[llvm::BytecodeFormat::Reserved_DoNotUse] += 8;
521     else
522       bca.BlockSizes[llvm::BytecodeFormat::Reserved_DoNotUse] += 4;
523   }
524
525 };
526 } // end anonymous namespace
527
528 /// @brief Utility for printing a titled unsigned value with
529 /// an aligned colon.
530 inline static void print(std::ostream& Out, const char*title,
531   unsigned val, bool nl = true ) {
532   Out << std::setw(30) << std::right << title
533       << std::setw(0) << ": "
534       << std::setw(9) << val << "\n";
535 }
536
537 /// @brief Utility for printing a titled double value with an
538 /// aligned colon
539 inline static void print(std::ostream&Out, const char*title,
540   double val ) {
541   Out << std::setw(30) << std::right << title
542       << std::setw(0) << ": "
543       << std::setw(9) << std::setprecision(6) << val << "\n" ;
544 }
545
546 /// @brief Utility for printing a titled double value with a
547 /// percentage and aligned colon.
548 inline static void print(std::ostream&Out, const char*title,
549   double top, double bot ) {
550   Out << std::setw(30) << std::right << title
551       << std::setw(0) << ": "
552       << std::setw(9) << std::setprecision(6) << top
553       << " (" << std::left << std::setw(0) << std::setprecision(4)
554       << (top/bot)*100.0 << "%)\n";
555 }
556
557 /// @brief Utility for printing a titled string value with
558 /// an aligned colon.
559 inline static void print(std::ostream&Out, const char*title,
560   std::string val, bool nl = true) {
561   Out << std::setw(30) << std::right << title
562       << std::setw(0) << ": "
563       << std::left << val << (nl ? "\n" : "");
564 }
565
566 /// This function prints the contents of rhe BytecodeAnalysis structure in
567 /// a human legible form.
568 /// @brief Print BytecodeAnalysis structure to an ostream
569 void llvm::PrintBytecodeAnalysis(BytecodeAnalysis& bca, std::ostream& Out )
570 {
571   Out << "\nSummary Analysis Of " << bca.ModuleId << ": \n\n";
572   print(Out, "Bytecode Analysis Of Module",     bca.ModuleId);
573   print(Out, "Bytecode Version Number",         bca.version);
574   print(Out, "File Size",                       bca.byteSize);
575   print(Out, "Module Bytes",
576         double(bca.BlockSizes[BytecodeFormat::ModuleBlockID]),
577         double(bca.byteSize));
578   print(Out, "Function Bytes",
579         double(bca.BlockSizes[BytecodeFormat::FunctionBlockID]),
580         double(bca.byteSize));
581   print(Out, "Global Types Bytes",
582         double(bca.BlockSizes[BytecodeFormat::GlobalTypePlaneBlockID]),
583         double(bca.byteSize));
584   print(Out, "Constant Pool Bytes",
585         double(bca.BlockSizes[BytecodeFormat::ConstantPoolBlockID]),
586         double(bca.byteSize));
587   print(Out, "Module Globals Bytes",
588         double(bca.BlockSizes[BytecodeFormat::ModuleGlobalInfoBlockID]),
589         double(bca.byteSize));
590   print(Out, "Instruction List Bytes",
591         double(bca.BlockSizes[BytecodeFormat::InstructionListBlockID]),
592         double(bca.byteSize));
593   print(Out, "Value Symbol Table Bytes",
594         double(bca.BlockSizes[BytecodeFormat::ValueSymbolTableBlockID]),
595         double(bca.byteSize));
596   print(Out, "Type Symbol Table Bytes",
597         double(bca.BlockSizes[BytecodeFormat::TypeSymbolTableBlockID]),
598         double(bca.byteSize));
599   print(Out, "Alignment Bytes",
600         double(bca.numAlignment), double(bca.byteSize));
601   print(Out, "Block Header Bytes",
602         double(bca.BlockSizes[BytecodeFormat::Reserved_DoNotUse]),
603         double(bca.byteSize));
604   print(Out, "Dependent Libraries Bytes", double(bca.libSize),
605         double(bca.byteSize));
606   print(Out, "Number Of Bytecode Blocks",       bca.numBlocks);
607   print(Out, "Number Of Functions",             bca.numFunctions);
608   print(Out, "Number Of Types",                 bca.numTypes);
609   print(Out, "Number Of Constants",             bca.numConstants);
610   print(Out, "Number Of Global Variables",      bca.numGlobalVars);
611   print(Out, "Number Of Values",                bca.numValues);
612   print(Out, "Number Of Basic Blocks",          bca.numBasicBlocks);
613   print(Out, "Number Of Instructions",          bca.numInstructions);
614   print(Out, "Number Of Long Instructions",     bca.longInstructions);
615   print(Out, "Number Of Operands",              bca.numOperands);
616   print(Out, "Number Of Symbol Tables",         bca.numSymTab);
617   print(Out, "Number Of Dependent Libs",        bca.numLibraries);
618   print(Out, "Total Instruction Size",          bca.instructionSize);
619   print(Out, "Average Instruction Size",
620         double(bca.instructionSize)/double(bca.numInstructions));
621
622   print(Out, "Maximum Type Slot Number",        bca.maxTypeSlot);
623   print(Out, "Maximum Value Slot Number",       bca.maxValueSlot);
624   print(Out, "Bytes Per Value ",                bca.fileDensity);
625   print(Out, "Bytes Per Global",                bca.globalsDensity);
626   print(Out, "Bytes Per Function",              bca.functionDensity);
627
628   if (bca.detailedResults) {
629     Out << "\nDetailed Analysis Of " << bca.ModuleId << " Functions:\n";
630
631     std::map<const Function*,BytecodeAnalysis::BytecodeFunctionInfo>::iterator I =
632       bca.FunctionInfo.begin();
633     std::map<const Function*,BytecodeAnalysis::BytecodeFunctionInfo>::iterator E =
634       bca.FunctionInfo.end();
635
636     while ( I != E ) {
637       Out << std::left << std::setw(0) << "\n";
638       if (I->second.numBasicBlocks == 0) Out << "External ";
639       Out << "Function: " << I->second.name << "\n";
640       print(Out, "Type:", I->second.description);
641       print(Out, "Byte Size", I->second.byteSize);
642       if (I->second.numBasicBlocks) {
643         print(Out, "Basic Blocks", I->second.numBasicBlocks);
644         print(Out, "Instructions", I->second.numInstructions);
645         print(Out, "Long Instructions", I->second.longInstructions);
646         print(Out, "Operands", I->second.numOperands);
647         print(Out, "Instruction Size", I->second.instructionSize);
648         print(Out, "Average Instruction Size",
649               double(I->second.instructionSize) / I->second.numInstructions);
650         print(Out, "Bytes Per Instruction", I->second.density);
651       }
652       ++I;
653     }
654   }
655
656   if ( bca.progressiveVerify )
657     Out << bca.VerifyInfo;
658 }
659
660 // AnalyzeBytecodeFile - analyze one file
661 Module* llvm::AnalyzeBytecodeFile(const std::string &Filename,  ///< File to analyze
662                                   BytecodeAnalysis& bca,        ///< Statistical output
663                                   BCDecompressor_t *BCDC,
664                                   std::string *ErrMsg,          ///< Error output
665                                   std::ostream* output          ///< Dump output
666                                   ) {
667   BytecodeHandler* AH = new AnalyzerHandler(bca, output);
668   ModuleProvider* MP = getBytecodeModuleProvider(Filename, BCDC, ErrMsg, AH);
669   if (!MP) return 0;
670   Module *M = MP->releaseModule(ErrMsg);
671   delete MP;
672   return M;
673 }