blob: 899a53427274cb7b9cddc2ed0047b50c69fce165 [file] [log] [blame]
Reid Spencerf41aa732004-06-29 23:23:12 +00001//===-- Analyzer.cpp - Analysis and Dumping of Bytecode 000000---*- C++ -*-===//
Misha Brukman8a96c532005-04-21 21:44:41 +00002//
Reid Spencerdac69c82004-06-07 17:53:43 +00003// The LLVM Compiler Infrastructure
4//
Misha Brukman8a96c532005-04-21 21:44:41 +00005// This file was developed by Reid Spencer and is distributed under the
Reid Spencerdac69c82004-06-07 17:53:43 +00006// University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukman8a96c532005-04-21 21:44:41 +00007//
Reid Spencerdac69c82004-06-07 17:53:43 +00008//===----------------------------------------------------------------------===//
9//
Reid Spencerf41aa732004-06-29 23:23:12 +000010// 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
Reid Spencerdac69c82004-06-07 17:53:43 +000016//
17//===----------------------------------------------------------------------===//
18
Reid Spencerf41aa732004-06-29 23:23:12 +000019#include "Reader.h"
20#include "llvm/Constants.h"
21#include "llvm/DerivedTypes.h"
22#include "llvm/Module.h"
23#include "llvm/Analysis/Verifier.h"
Reid Spencerf41aa732004-06-29 23:23:12 +000024#include "llvm/Bytecode/BytecodeHandler.h"
Reid Spencer911ec6d2004-08-21 20:58:19 +000025#include "llvm/Assembly/Writer.h"
Reid Spencerf41aa732004-06-29 23:23:12 +000026#include <iomanip>
27#include <sstream>
Duraid Madina0f7bfba2005-12-26 14:23:22 +000028#include <ios>
Reid Spencerdac69c82004-06-07 17:53:43 +000029
30using namespace llvm;
31
Reid Spencerdac69c82004-06-07 17:53:43 +000032namespace {
33
Reid Spencerf41aa732004-06-29 23:23:12 +000034/// @brief Bytecode reading handler for analyzing bytecode.
Reid Spencerdac69c82004-06-07 17:53:43 +000035class AnalyzerHandler : public BytecodeHandler {
Reid Spencerf41aa732004-06-29 23:23:12 +000036 BytecodeAnalysis& bca; ///< The structure in which data is recorded
Reid Spencer911ec6d2004-08-21 20:58:19 +000037 std::ostream* os; ///< A convenience for osing data.
Reid Spencerf41aa732004-06-29 23:23:12 +000038 /// @brief Keeps track of current function
Misha Brukman8a96c532005-04-21 21:44:41 +000039 BytecodeAnalysis::BytecodeFunctionInfo* currFunc;
Reid Spencerf41aa732004-06-29 23:23:12 +000040 Module* M; ///< Keeps track of current module
41
42/// @name Constructor
43/// @{
Reid Spencerdac69c82004-06-07 17:53:43 +000044public:
Reid Spencerf41aa732004-06-29 23:23:12 +000045 /// The only way to construct an AnalyzerHandler. All that is needed is a
46 /// reference to the BytecodeAnalysis structure where the output will be
47 /// placed.
Misha Brukman8a96c532005-04-21 21:44:41 +000048 AnalyzerHandler(BytecodeAnalysis& TheBca, std::ostream* output)
49 : bca(TheBca)
Reid Spencer911ec6d2004-08-21 20:58:19 +000050 , os(output)
Reid Spencercbb22e22004-06-10 22:00:54 +000051 , currFunc(0)
Reid Spencer911ec6d2004-08-21 20:58:19 +000052 { }
Reid Spencer649ee572004-06-09 06:16:43 +000053
Reid Spencerf41aa732004-06-29 23:23:12 +000054/// @}
55/// @name BytecodeHandler Implementations
56/// @{
57public:
Misha Brukman8a96c532005-04-21 21:44:41 +000058 virtual void handleError(const std::string& str ) {
Reid Spencer911ec6d2004-08-21 20:58:19 +000059 if (os)
60 *os << "ERROR: " << str << "\n";
Reid Spencerdac69c82004-06-07 17:53:43 +000061 }
62
Reid Spencerf41aa732004-06-29 23:23:12 +000063 virtual void handleStart( Module* Mod, unsigned theSize ) {
64 M = Mod;
Reid Spencer911ec6d2004-08-21 20:58:19 +000065 if (os)
66 *os << "Bytecode {\n";
Reid Spencerf41aa732004-06-29 23:23:12 +000067 bca.byteSize = theSize;
Reid Spencer649ee572004-06-09 06:16:43 +000068 bca.ModuleId.clear();
Reid Spencer00c28a72004-06-10 08:09:13 +000069 bca.numBlocks = 0;
Reid Spencer649ee572004-06-09 06:16:43 +000070 bca.numTypes = 0;
71 bca.numValues = 0;
72 bca.numFunctions = 0;
73 bca.numConstants = 0;
74 bca.numGlobalVars = 0;
75 bca.numInstructions = 0;
76 bca.numBasicBlocks = 0;
77 bca.numOperands = 0;
78 bca.numCmpctnTables = 0;
79 bca.numSymTab = 0;
Reid Spencer911ec6d2004-08-21 20:58:19 +000080 bca.numLibraries = 0;
81 bca.libSize = 0;
Reid Spencer649ee572004-06-09 06:16:43 +000082 bca.maxTypeSlot = 0;
83 bca.maxValueSlot = 0;
Reid Spencer00c28a72004-06-10 08:09:13 +000084 bca.numAlignment = 0;
85 bca.fileDensity = 0.0;
86 bca.globalsDensity = 0.0;
87 bca.functionDensity = 0.0;
Reid Spencer1cf50242004-06-11 15:10:38 +000088 bca.instructionSize = 0;
89 bca.longInstructions = 0;
Reid Spencer00c28a72004-06-10 08:09:13 +000090 bca.vbrCount32 = 0;
91 bca.vbrCount64 = 0;
92 bca.vbrCompBytes = 0;
93 bca.vbrExpdBytes = 0;
Reid Spencer649ee572004-06-09 06:16:43 +000094 bca.FunctionInfo.clear();
Reid Spencer911ec6d2004-08-21 20:58:19 +000095 bca.BlockSizes[BytecodeFormat::Reserved_DoNotUse] = 0;
96 bca.BlockSizes[BytecodeFormat::ModuleBlockID] = theSize;
97 bca.BlockSizes[BytecodeFormat::FunctionBlockID] = 0;
98 bca.BlockSizes[BytecodeFormat::ConstantPoolBlockID] = 0;
Reid Spencer78d033e2007-01-06 07:24:44 +000099 bca.BlockSizes[BytecodeFormat::ValueSymbolTableBlockID] = 0;
Reid Spencer911ec6d2004-08-21 20:58:19 +0000100 bca.BlockSizes[BytecodeFormat::ModuleGlobalInfoBlockID] = 0;
101 bca.BlockSizes[BytecodeFormat::GlobalTypePlaneBlockID] = 0;
102 bca.BlockSizes[BytecodeFormat::InstructionListBlockID] = 0;
103 bca.BlockSizes[BytecodeFormat::CompactionTableBlockID] = 0;
Reid Spencer78d033e2007-01-06 07:24:44 +0000104 bca.BlockSizes[BytecodeFormat::TypeSymbolTableBlockID] = 0;
Reid Spencerdac69c82004-06-07 17:53:43 +0000105 }
106
Reid Spencercbb22e22004-06-10 22:00:54 +0000107 virtual void handleFinish() {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000108 if (os)
Misha Brukman8a96c532005-04-21 21:44:41 +0000109 *os << "} End Bytecode\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000110
Reid Spencer00c28a72004-06-10 08:09:13 +0000111 bca.fileDensity = double(bca.byteSize) / double( bca.numTypes + bca.numValues );
112 double globalSize = 0.0;
Reid Spencer911ec6d2004-08-21 20:58:19 +0000113 globalSize += double(bca.BlockSizes[BytecodeFormat::ConstantPoolBlockID]);
114 globalSize += double(bca.BlockSizes[BytecodeFormat::ModuleGlobalInfoBlockID]);
115 globalSize += double(bca.BlockSizes[BytecodeFormat::GlobalTypePlaneBlockID]);
Misha Brukman8a96c532005-04-21 21:44:41 +0000116 bca.globalsDensity = globalSize / double( bca.numTypes + bca.numConstants +
Reid Spencer00c28a72004-06-10 08:09:13 +0000117 bca.numGlobalVars );
Misha Brukman8a96c532005-04-21 21:44:41 +0000118 bca.functionDensity = double(bca.BlockSizes[BytecodeFormat::FunctionBlockID]) /
Reid Spencer00c28a72004-06-10 08:09:13 +0000119 double(bca.numFunctions);
Reid Spencerf41aa732004-06-29 23:23:12 +0000120
Chris Lattner05ac92c2006-07-06 18:02:27 +0000121 if (bca.progressiveVerify) {
122 std::string msg;
123 if (verifyModule(*M, ReturnStatusAction, &msg))
Reid Spencerb61cdb72004-07-04 11:00:39 +0000124 bca.VerifyInfo += "Verify@Finish: " + msg + "\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000125 }
Reid Spencerdac69c82004-06-07 17:53:43 +0000126 }
127
Reid Spencercbb22e22004-06-10 22:00:54 +0000128 virtual void handleModuleBegin(const std::string& id) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000129 if (os)
130 *os << " Module " << id << " {\n";
Reid Spencer649ee572004-06-09 06:16:43 +0000131 bca.ModuleId = id;
Reid Spencerdac69c82004-06-07 17:53:43 +0000132 }
133
Misha Brukman8a96c532005-04-21 21:44:41 +0000134 virtual void handleModuleEnd(const std::string& id) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000135 if (os)
136 *os << " } End Module " << id << "\n";
Chris Lattner05ac92c2006-07-06 18:02:27 +0000137 if (bca.progressiveVerify) {
138 std::string msg;
139 if (verifyModule(*M, ReturnStatusAction, &msg))
Reid Spencerb61cdb72004-07-04 11:00:39 +0000140 bca.VerifyInfo += "Verify@EndModule: " + msg + "\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000141 }
142 }
Reid Spencerdac69c82004-06-07 17:53:43 +0000143
Reid Spencercbb22e22004-06-10 22:00:54 +0000144 virtual void handleVersionInfo(
Reid Spencerdac69c82004-06-07 17:53:43 +0000145 unsigned char RevisionNum, ///< Byte code revision number
146 Module::Endianness Endianness, ///< Endianness indicator
147 Module::PointerSize PointerSize ///< PointerSize indicator
Misha Brukman8a96c532005-04-21 21:44:41 +0000148 ) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000149 if (os)
Misha Brukman8a96c532005-04-21 21:44:41 +0000150 *os << " RevisionNum: " << int(RevisionNum)
Reid Spencerb61cdb72004-07-04 11:00:39 +0000151 << " Endianness: " << Endianness
152 << " PointerSize: " << PointerSize << "\n";
Reid Spencer911ec6d2004-08-21 20:58:19 +0000153 bca.version = RevisionNum;
Reid Spencerf41aa732004-06-29 23:23:12 +0000154 }
Reid Spencerdac69c82004-06-07 17:53:43 +0000155
Misha Brukman8a96c532005-04-21 21:44:41 +0000156 virtual void handleModuleGlobalsBegin() {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000157 if (os)
158 *os << " BLOCK: ModuleGlobalInfo {\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000159 }
Reid Spencerdac69c82004-06-07 17:53:43 +0000160
Misha Brukman8a96c532005-04-21 21:44:41 +0000161 virtual void handleGlobalVariable(
162 const Type* ElemType,
163 bool isConstant,
Reid Spencerf41aa732004-06-29 23:23:12 +0000164 GlobalValue::LinkageTypes Linkage,
165 unsigned SlotNum,
166 unsigned initSlot
Reid Spencercbb22e22004-06-10 22:00:54 +0000167 ) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000168 if (os) {
169 *os << " GV: "
170 << ( initSlot == 0 ? "Uni" : "I" ) << "nitialized, "
171 << ( isConstant? "Constant, " : "Variable, ")
172 << " Linkage=" << Linkage << " Type=";
173 WriteTypeSymbolic(*os, ElemType, M);
Misha Brukman8a96c532005-04-21 21:44:41 +0000174 *os << " Slot=" << SlotNum << " InitSlot=" << initSlot
Reid Spencer911ec6d2004-08-21 20:58:19 +0000175 << "\n";
176 }
177
Reid Spencer649ee572004-06-09 06:16:43 +0000178 bca.numGlobalVars++;
Reid Spencer00c28a72004-06-10 08:09:13 +0000179 bca.numValues++;
Reid Spencer911ec6d2004-08-21 20:58:19 +0000180 if (SlotNum > bca.maxValueSlot)
181 bca.maxValueSlot = SlotNum;
182 if (initSlot > bca.maxValueSlot)
183 bca.maxValueSlot = initSlot;
Reid Spencerf41aa732004-06-29 23:23:12 +0000184
Reid Spencer911ec6d2004-08-21 20:58:19 +0000185 }
186
187 virtual void handleTypeList(unsigned numEntries) {
188 bca.maxTypeSlot = numEntries - 1;
Reid Spencerdac69c82004-06-07 17:53:43 +0000189 }
190
Misha Brukman8a96c532005-04-21 21:44:41 +0000191 virtual void handleType( const Type* Ty ) {
192 bca.numTypes++;
Reid Spencer911ec6d2004-08-21 20:58:19 +0000193 if (os) {
194 *os << " Type: ";
195 WriteTypeSymbolic(*os,Ty,M);
196 *os << "\n";
197 }
Reid Spencerdac69c82004-06-07 17:53:43 +0000198 }
199
Misha Brukman8a96c532005-04-21 21:44:41 +0000200 virtual void handleFunctionDeclaration(
Reid Spencerb61cdb72004-07-04 11:00:39 +0000201 Function* Func ///< The function
Reid Spencercbb22e22004-06-10 22:00:54 +0000202 ) {
Reid Spencer649ee572004-06-09 06:16:43 +0000203 bca.numFunctions++;
Reid Spencer00c28a72004-06-10 08:09:13 +0000204 bca.numValues++;
Reid Spencer911ec6d2004-08-21 20:58:19 +0000205 if (os) {
206 *os << " Function Decl: ";
207 WriteTypeSymbolic(*os,Func->getType(),M);
Anton Korobeynikov93c2b372006-09-17 13:06:18 +0000208 *os <<", Linkage=" << Func->getLinkage();
Reid Spencer911ec6d2004-08-21 20:58:19 +0000209 *os << "\n";
210 }
Reid Spencerdac69c82004-06-07 17:53:43 +0000211 }
212
Reid Spencerf41aa732004-06-29 23:23:12 +0000213 virtual void handleGlobalInitializer(GlobalVariable* GV, Constant* CV) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000214 if (os) {
215 *os << " Initializer: GV=";
216 GV->print(*os);
217 *os << " CV=";
218 CV->print(*os);
219 *os << "\n";
220 }
221 }
222
223 virtual void handleDependentLibrary(const std::string& libName) {
224 bca.numLibraries++;
225 bca.libSize += libName.size() + (libName.size() < 128 ? 1 : 2);
Reid Spencer3ee8eed2004-09-11 04:14:07 +0000226 if (os)
227 *os << " Library: '" << libName << "'\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000228 }
Reid Spencerdac69c82004-06-07 17:53:43 +0000229
Misha Brukman8a96c532005-04-21 21:44:41 +0000230 virtual void handleModuleGlobalsEnd() {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000231 if (os)
232 *os << " } END BLOCK: ModuleGlobalInfo\n";
Chris Lattner05ac92c2006-07-06 18:02:27 +0000233 if (bca.progressiveVerify) {
234 std::string msg;
235 if (verifyModule(*M, ReturnStatusAction, &msg))
Reid Spencerb61cdb72004-07-04 11:00:39 +0000236 bca.VerifyInfo += "Verify@EndModuleGlobalInfo: " + msg + "\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000237 }
238 }
239
Misha Brukman8a96c532005-04-21 21:44:41 +0000240 virtual void handleCompactionTableBegin() {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000241 if (os)
242 *os << " BLOCK: CompactionTable {\n";
Reid Spencer488d73a2004-08-27 00:43:51 +0000243 bca.numCmpctnTables++;
Reid Spencerf41aa732004-06-29 23:23:12 +0000244 }
Reid Spencerdac69c82004-06-07 17:53:43 +0000245
Reid Spencercbb22e22004-06-10 22:00:54 +0000246 virtual void handleCompactionTablePlane( unsigned Ty, unsigned NumEntries) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000247 if (os)
248 *os << " Plane: Ty=" << Ty << " Size=" << NumEntries << "\n";
Reid Spencerdac69c82004-06-07 17:53:43 +0000249 }
250
Misha Brukman8a96c532005-04-21 21:44:41 +0000251 virtual void handleCompactionTableType( unsigned i, unsigned TypSlot,
Reid Spencerf41aa732004-06-29 23:23:12 +0000252 const Type* Ty ) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000253 if (os) {
254 *os << " Type: " << i << " Slot:" << TypSlot << " is ";
255 WriteTypeSymbolic(*os,Ty,M);
Misha Brukman8a96c532005-04-21 21:44:41 +0000256 *os << "\n";
Reid Spencer911ec6d2004-08-21 20:58:19 +0000257 }
Reid Spencerf41aa732004-06-29 23:23:12 +0000258 }
Reid Spencerdac69c82004-06-07 17:53:43 +0000259
Chris Lattner2c6c14d2004-08-04 00:19:23 +0000260 virtual void handleCompactionTableValue(unsigned i, unsigned TypSlot,
Misha Brukman8a96c532005-04-21 21:44:41 +0000261 unsigned ValSlot) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000262 if (os)
Misha Brukman8a96c532005-04-21 21:44:41 +0000263 *os << " Value: " << i << " TypSlot: " << TypSlot
Chris Lattner2c6c14d2004-08-04 00:19:23 +0000264 << " ValSlot:" << ValSlot << "\n";
Reid Spencer911ec6d2004-08-21 20:58:19 +0000265 if (ValSlot > bca.maxValueSlot)
266 bca.maxValueSlot = ValSlot;
Reid Spencerf41aa732004-06-29 23:23:12 +0000267 }
Reid Spencercbb22e22004-06-10 22:00:54 +0000268
Misha Brukman8a96c532005-04-21 21:44:41 +0000269 virtual void handleCompactionTableEnd() {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000270 if (os)
271 *os << " } END BLOCK: CompactionTable\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000272 }
Reid Spencercbb22e22004-06-10 22:00:54 +0000273
Misha Brukman8a96c532005-04-21 21:44:41 +0000274 virtual void handleSymbolTableBegin(Function* CF, SymbolTable* ST) {
275 bca.numSymTab++;
Reid Spencer911ec6d2004-08-21 20:58:19 +0000276 if (os)
277 *os << " BLOCK: SymbolTable {\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000278 }
Reid Spencercbb22e22004-06-10 22:00:54 +0000279
Misha Brukman8a96c532005-04-21 21:44:41 +0000280 virtual void handleSymbolTablePlane(unsigned Ty, unsigned NumEntries,
281 const Type* Typ) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000282 if (os) {
283 *os << " Plane: Ty=" << Ty << " Size=" << NumEntries << " Type: ";
284 WriteTypeSymbolic(*os,Typ,M);
Misha Brukman8a96c532005-04-21 21:44:41 +0000285 *os << "\n";
Reid Spencer911ec6d2004-08-21 20:58:19 +0000286 }
Reid Spencerf41aa732004-06-29 23:23:12 +0000287 }
Reid Spencercbb22e22004-06-10 22:00:54 +0000288
Misha Brukman8a96c532005-04-21 21:44:41 +0000289 virtual void handleSymbolTableType(unsigned i, unsigned TypSlot,
290 const std::string& name ) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000291 if (os)
292 *os << " Type " << i << " Slot=" << TypSlot
Misha Brukman8a96c532005-04-21 21:44:41 +0000293 << " Name: " << name << "\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000294 }
Reid Spencercbb22e22004-06-10 22:00:54 +0000295
Misha Brukman8a96c532005-04-21 21:44:41 +0000296 virtual void handleSymbolTableValue(unsigned i, unsigned ValSlot,
297 const std::string& name ) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000298 if (os)
299 *os << " Value " << i << " Slot=" << ValSlot
300 << " Name: " << name << "\n";
301 if (ValSlot > bca.maxValueSlot)
302 bca.maxValueSlot = ValSlot;
Reid Spencerf41aa732004-06-29 23:23:12 +0000303 }
Reid Spencercbb22e22004-06-10 22:00:54 +0000304
Misha Brukman8a96c532005-04-21 21:44:41 +0000305 virtual void handleSymbolTableEnd() {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000306 if (os)
307 *os << " } END BLOCK: SymbolTable\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000308 }
Reid Spencercbb22e22004-06-10 22:00:54 +0000309
Reid Spencerf41aa732004-06-29 23:23:12 +0000310 virtual void handleFunctionBegin(Function* Func, unsigned Size) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000311 if (os) {
312 *os << " BLOCK: Function {\n"
313 << " Linkage: " << Func->getLinkage() << "\n"
Misha Brukman8a96c532005-04-21 21:44:41 +0000314 << " Type: ";
Reid Spencer911ec6d2004-08-21 20:58:19 +0000315 WriteTypeSymbolic(*os,Func->getType(),M);
316 *os << "\n";
317 }
318
Reid Spencercbb22e22004-06-10 22:00:54 +0000319 currFunc = &bca.FunctionInfo[Func];
Reid Spencer911ec6d2004-08-21 20:58:19 +0000320 std::ostringstream tmp;
321 WriteTypeSymbolic(tmp,Func->getType(),M);
322 currFunc->description = tmp.str();
Reid Spencercbb22e22004-06-10 22:00:54 +0000323 currFunc->name = Func->getName();
324 currFunc->byteSize = Size;
325 currFunc->numInstructions = 0;
326 currFunc->numBasicBlocks = 0;
327 currFunc->numPhis = 0;
328 currFunc->numOperands = 0;
329 currFunc->density = 0.0;
Reid Spencer1cf50242004-06-11 15:10:38 +0000330 currFunc->instructionSize = 0;
331 currFunc->longInstructions = 0;
Reid Spencercbb22e22004-06-10 22:00:54 +0000332 currFunc->vbrCount32 = 0;
333 currFunc->vbrCount64 = 0;
334 currFunc->vbrCompBytes = 0;
335 currFunc->vbrExpdBytes = 0;
Reid Spencerf41aa732004-06-29 23:23:12 +0000336
Reid Spencerdac69c82004-06-07 17:53:43 +0000337 }
338
Reid Spencercbb22e22004-06-10 22:00:54 +0000339 virtual void handleFunctionEnd( Function* Func) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000340 if (os)
341 *os << " } END BLOCK: Function\n";
Reid Spencercbb22e22004-06-10 22:00:54 +0000342 currFunc->density = double(currFunc->byteSize) /
Reid Spencer3120e712004-08-24 22:45:32 +0000343 double(currFunc->numInstructions);
Reid Spencerf41aa732004-06-29 23:23:12 +0000344
Chris Lattner05ac92c2006-07-06 18:02:27 +0000345 if (bca.progressiveVerify) {
346 std::string msg;
347 if (verifyModule(*M, ReturnStatusAction, &msg))
Reid Spencerb61cdb72004-07-04 11:00:39 +0000348 bca.VerifyInfo += "Verify@EndFunction: " + msg + "\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000349 }
Reid Spencerdac69c82004-06-07 17:53:43 +0000350 }
351
Reid Spencercbb22e22004-06-10 22:00:54 +0000352 virtual void handleBasicBlockBegin( unsigned blocknum) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000353 if (os)
354 *os << " BLOCK: BasicBlock #" << blocknum << "{\n";
Reid Spencer649ee572004-06-09 06:16:43 +0000355 bca.numBasicBlocks++;
Reid Spencer00c28a72004-06-10 08:09:13 +0000356 bca.numValues++;
Reid Spencercbb22e22004-06-10 22:00:54 +0000357 if ( currFunc ) currFunc->numBasicBlocks++;
Reid Spencerdac69c82004-06-07 17:53:43 +0000358 }
359
Misha Brukman8a96c532005-04-21 21:44:41 +0000360 virtual bool handleInstruction( unsigned Opcode, const Type* iType,
Reid Spencerb61cdb72004-07-04 11:00:39 +0000361 std::vector<unsigned>& Operands, unsigned Size){
Reid Spencer911ec6d2004-08-21 20:58:19 +0000362 if (os) {
Misha Brukman8a96c532005-04-21 21:44:41 +0000363 *os << " INST: OpCode="
Reid Spencer911ec6d2004-08-21 20:58:19 +0000364 << Instruction::getOpcodeName(Opcode) << " Type=\"";
365 WriteTypeSymbolic(*os,iType,M);
366 *os << "\"";
Misha Brukman8a96c532005-04-21 21:44:41 +0000367 for ( unsigned i = 0; i < Operands.size(); ++i )
Reid Spencer911ec6d2004-08-21 20:58:19 +0000368 *os << " Op(" << i << ")=Slot(" << Operands[i] << ")";
369 *os << "\n";
370 }
Reid Spencerf41aa732004-06-29 23:23:12 +0000371
Reid Spencer649ee572004-06-09 06:16:43 +0000372 bca.numInstructions++;
Reid Spencer00c28a72004-06-10 08:09:13 +0000373 bca.numValues++;
Reid Spencer1cf50242004-06-11 15:10:38 +0000374 bca.instructionSize += Size;
375 if (Size > 4 ) bca.longInstructions++;
Reid Spencer00c28a72004-06-10 08:09:13 +0000376 bca.numOperands += Operands.size();
Reid Spencer911ec6d2004-08-21 20:58:19 +0000377 for (unsigned i = 0; i < Operands.size(); ++i )
378 if (Operands[i] > bca.maxValueSlot)
379 bca.maxValueSlot = Operands[i];
Reid Spencercbb22e22004-06-10 22:00:54 +0000380 if ( currFunc ) {
381 currFunc->numInstructions++;
Reid Spencer1cf50242004-06-11 15:10:38 +0000382 currFunc->instructionSize += Size;
383 if (Size > 4 ) currFunc->longInstructions++;
Reid Spencer8a9a3702004-06-11 03:06:43 +0000384 if ( Opcode == Instruction::PHI ) currFunc->numPhis++;
Reid Spencercbb22e22004-06-10 22:00:54 +0000385 }
Misha Brukman8a96c532005-04-21 21:44:41 +0000386 return Instruction::isTerminator(Opcode);
Reid Spencerdac69c82004-06-07 17:53:43 +0000387 }
388
Misha Brukman8a96c532005-04-21 21:44:41 +0000389 virtual void handleBasicBlockEnd(unsigned blocknum) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000390 if (os)
Reid Spencerc6d416a2006-12-15 21:46:37 +0000391 *os << " } END BLOCK: BasicBlock #" << blocknum << "\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000392 }
Reid Spencerdac69c82004-06-07 17:53:43 +0000393
Misha Brukman8a96c532005-04-21 21:44:41 +0000394 virtual void handleGlobalConstantsBegin() {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000395 if (os)
396 *os << " BLOCK: GlobalConstants {\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000397 }
Reid Spencerdac69c82004-06-07 17:53:43 +0000398
Misha Brukman8a96c532005-04-21 21:44:41 +0000399 virtual void handleConstantExpression( unsigned Opcode,
Reid Spencerf41aa732004-06-29 23:23:12 +0000400 std::vector<Constant*> ArgVec, Constant* C ) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000401 if (os) {
402 *os << " EXPR: " << Instruction::getOpcodeName(Opcode) << "\n";
403 for ( unsigned i = 0; i < ArgVec.size(); ++i ) {
Misha Brukman8a96c532005-04-21 21:44:41 +0000404 *os << " Arg#" << i << " "; ArgVec[i]->print(*os);
Reid Spencer911ec6d2004-08-21 20:58:19 +0000405 *os << "\n";
406 }
407 *os << " Value=";
408 C->print(*os);
409 *os << "\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000410 }
Reid Spencer649ee572004-06-09 06:16:43 +0000411 bca.numConstants++;
Reid Spencer00c28a72004-06-10 08:09:13 +0000412 bca.numValues++;
Reid Spencerdac69c82004-06-07 17:53:43 +0000413 }
414
Reid Spencercbb22e22004-06-10 22:00:54 +0000415 virtual void handleConstantValue( Constant * c ) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000416 if (os) {
417 *os << " VALUE: ";
418 c->print(*os);
419 *os << "\n";
420 }
Reid Spencer649ee572004-06-09 06:16:43 +0000421 bca.numConstants++;
Reid Spencer00c28a72004-06-10 08:09:13 +0000422 bca.numValues++;
Reid Spencerdac69c82004-06-07 17:53:43 +0000423 }
424
Misha Brukman8a96c532005-04-21 21:44:41 +0000425 virtual void handleConstantArray( const ArrayType* AT,
Reid Spencerf41aa732004-06-29 23:23:12 +0000426 std::vector<Constant*>& Elements,
Reid Spencerb61cdb72004-07-04 11:00:39 +0000427 unsigned TypeSlot,
428 Constant* ArrayVal ) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000429 if (os) {
430 *os << " ARRAY: ";
Misha Brukman8a96c532005-04-21 21:44:41 +0000431 WriteTypeSymbolic(*os,AT,M);
Reid Spencer911ec6d2004-08-21 20:58:19 +0000432 *os << " TypeSlot=" << TypeSlot << "\n";
433 for ( unsigned i = 0; i < Elements.size(); ++i ) {
434 *os << " #" << i;
435 Elements[i]->print(*os);
436 *os << "\n";
437 }
438 *os << " Value=";
439 ArrayVal->print(*os);
440 *os << "\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000441 }
Reid Spencerf41aa732004-06-29 23:23:12 +0000442
Reid Spencer649ee572004-06-09 06:16:43 +0000443 bca.numConstants++;
Reid Spencer00c28a72004-06-10 08:09:13 +0000444 bca.numValues++;
Reid Spencerdac69c82004-06-07 17:53:43 +0000445 }
446
Reid Spencercbb22e22004-06-10 22:00:54 +0000447 virtual void handleConstantStruct(
Reid Spencer00c28a72004-06-10 08:09:13 +0000448 const StructType* ST,
Reid Spencerf41aa732004-06-29 23:23:12 +0000449 std::vector<Constant*>& Elements,
Reid Spencerb61cdb72004-07-04 11:00:39 +0000450 Constant* StructVal)
Reid Spencerdac69c82004-06-07 17:53:43 +0000451 {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000452 if (os) {
453 *os << " STRUC: ";
454 WriteTypeSymbolic(*os,ST,M);
455 *os << "\n";
456 for ( unsigned i = 0; i < Elements.size(); ++i ) {
Misha Brukman8a96c532005-04-21 21:44:41 +0000457 *os << " #" << i << " "; Elements[i]->print(*os);
Reid Spencer911ec6d2004-08-21 20:58:19 +0000458 *os << "\n";
459 }
460 *os << " Value=";
461 StructVal->print(*os);
462 *os << "\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000463 }
Reid Spencer649ee572004-06-09 06:16:43 +0000464 bca.numConstants++;
Reid Spencer00c28a72004-06-10 08:09:13 +0000465 bca.numValues++;
Reid Spencerdac69c82004-06-07 17:53:43 +0000466 }
467
Misha Brukman8a96c532005-04-21 21:44:41 +0000468 virtual void handleConstantPacked(
469 const PackedType* PT,
Brian Gaeke715c90b2004-08-20 06:00:58 +0000470 std::vector<Constant*>& Elements,
Misha Brukman8a96c532005-04-21 21:44:41 +0000471 unsigned TypeSlot,
472 Constant* PackedVal)
Brian Gaeke715c90b2004-08-20 06:00:58 +0000473 {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000474 if (os) {
475 *os << " PACKD: ";
476 WriteTypeSymbolic(*os,PT,M);
477 *os << " TypeSlot=" << TypeSlot << "\n";
478 for ( unsigned i = 0; i < Elements.size(); ++i ) {
479 *os << " #" << i;
480 Elements[i]->print(*os);
481 *os << "\n";
482 }
483 *os << " Value=";
484 PackedVal->print(*os);
485 *os << "\n";
Brian Gaeke715c90b2004-08-20 06:00:58 +0000486 }
Brian Gaeke715c90b2004-08-20 06:00:58 +0000487
488 bca.numConstants++;
489 bca.numValues++;
490 }
491
Misha Brukman8a96c532005-04-21 21:44:41 +0000492 virtual void handleConstantPointer( const PointerType* PT,
Reid Spencer3c90f9f2004-07-18 00:10:36 +0000493 unsigned Slot, GlobalValue* GV ) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000494 if (os) {
495 *os << " PNTR: ";
496 WriteTypeSymbolic(*os,PT,M);
497 *os << " Slot=" << Slot << " GlobalValue=";
498 GV->print(*os);
499 *os << "\n";
500 }
Reid Spencer649ee572004-06-09 06:16:43 +0000501 bca.numConstants++;
Reid Spencer00c28a72004-06-10 08:09:13 +0000502 bca.numValues++;
Reid Spencerdac69c82004-06-07 17:53:43 +0000503 }
504
Reid Spencercbb22e22004-06-10 22:00:54 +0000505 virtual void handleConstantString( const ConstantArray* CA ) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000506 if (os) {
507 *os << " STRNG: ";
Misha Brukman8a96c532005-04-21 21:44:41 +0000508 CA->print(*os);
Reid Spencer911ec6d2004-08-21 20:58:19 +0000509 *os << "\n";
510 }
Reid Spencer649ee572004-06-09 06:16:43 +0000511 bca.numConstants++;
Reid Spencer00c28a72004-06-10 08:09:13 +0000512 bca.numValues++;
Reid Spencerdac69c82004-06-07 17:53:43 +0000513 }
514
Misha Brukman8a96c532005-04-21 21:44:41 +0000515 virtual void handleGlobalConstantsEnd() {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000516 if (os)
517 *os << " } END BLOCK: GlobalConstants\n";
518
Chris Lattner05ac92c2006-07-06 18:02:27 +0000519 if (bca.progressiveVerify) {
520 std::string msg;
521 if (verifyModule(*M, ReturnStatusAction, &msg))
Reid Spencerb61cdb72004-07-04 11:00:39 +0000522 bca.VerifyInfo += "Verify@EndGlobalConstants: " + msg + "\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000523 }
524 }
Reid Spencerdac69c82004-06-07 17:53:43 +0000525
Reid Spencercbb22e22004-06-10 22:00:54 +0000526 virtual void handleAlignment(unsigned numBytes) {
Reid Spencer00c28a72004-06-10 08:09:13 +0000527 bca.numAlignment += numBytes;
Reid Spencerdac69c82004-06-07 17:53:43 +0000528 }
529
Reid Spencercbb22e22004-06-10 22:00:54 +0000530 virtual void handleBlock(
Reid Spencer00c28a72004-06-10 08:09:13 +0000531 unsigned BType, const unsigned char* StartPtr, unsigned Size) {
532 bca.numBlocks++;
Reid Spencer911ec6d2004-08-21 20:58:19 +0000533 assert(BType >= BytecodeFormat::ModuleBlockID);
534 assert(BType < BytecodeFormat::NumberOfBlockIDs);
535 bca.BlockSizes[
Reid Spencerd798a512006-11-14 04:47:22 +0000536 llvm::BytecodeFormat::BytecodeBlockIdentifiers(BType)] += Size;
Reid Spencer911ec6d2004-08-21 20:58:19 +0000537
538 if (bca.version < 3) // Check for long block headers versions
539 bca.BlockSizes[llvm::BytecodeFormat::Reserved_DoNotUse] += 8;
540 else
541 bca.BlockSizes[llvm::BytecodeFormat::Reserved_DoNotUse] += 4;
Reid Spencer00c28a72004-06-10 08:09:13 +0000542 }
543
544 virtual void handleVBR32(unsigned Size ) {
545 bca.vbrCount32++;
546 bca.vbrCompBytes += Size;
547 bca.vbrExpdBytes += sizeof(uint32_t);
Reid Spencercbb22e22004-06-10 22:00:54 +0000548 if (currFunc) {
549 currFunc->vbrCount32++;
550 currFunc->vbrCompBytes += Size;
551 currFunc->vbrExpdBytes += sizeof(uint32_t);
552 }
Reid Spencer00c28a72004-06-10 08:09:13 +0000553 }
Reid Spencercbb22e22004-06-10 22:00:54 +0000554
Reid Spencer00c28a72004-06-10 08:09:13 +0000555 virtual void handleVBR64(unsigned Size ) {
556 bca.vbrCount64++;
557 bca.vbrCompBytes += Size;
558 bca.vbrExpdBytes += sizeof(uint64_t);
Reid Spencercbb22e22004-06-10 22:00:54 +0000559 if ( currFunc ) {
560 currFunc->vbrCount64++;
561 currFunc->vbrCompBytes += Size;
562 currFunc->vbrExpdBytes += sizeof(uint64_t);
563 }
Reid Spencer00c28a72004-06-10 08:09:13 +0000564 }
Reid Spencerdac69c82004-06-07 17:53:43 +0000565};
566
Reid Spencerf41aa732004-06-29 23:23:12 +0000567
568/// @brief Utility for printing a titled unsigned value with
569/// an aligned colon.
Misha Brukman8a96c532005-04-21 21:44:41 +0000570inline static void print(std::ostream& Out, const char*title,
Reid Spencerf41aa732004-06-29 23:23:12 +0000571 unsigned val, bool nl = true ) {
Misha Brukman8a96c532005-04-21 21:44:41 +0000572 Out << std::setw(30) << std::right << title
Reid Spencerf41aa732004-06-29 23:23:12 +0000573 << std::setw(0) << ": "
574 << std::setw(9) << val << "\n";
Reid Spencerdac69c82004-06-07 17:53:43 +0000575}
576
Reid Spencerf41aa732004-06-29 23:23:12 +0000577/// @brief Utility for printing a titled double value with an
578/// aligned colon
Misha Brukman8a96c532005-04-21 21:44:41 +0000579inline static void print(std::ostream&Out, const char*title,
Reid Spencerf41aa732004-06-29 23:23:12 +0000580 double val ) {
Misha Brukman8a96c532005-04-21 21:44:41 +0000581 Out << std::setw(30) << std::right << title
Reid Spencerf41aa732004-06-29 23:23:12 +0000582 << std::setw(0) << ": "
583 << std::setw(9) << std::setprecision(6) << val << "\n" ;
584}
585
586/// @brief Utility for printing a titled double value with a
587/// percentage and aligned colon.
Misha Brukman8a96c532005-04-21 21:44:41 +0000588inline static void print(std::ostream&Out, const char*title,
Reid Spencerf41aa732004-06-29 23:23:12 +0000589 double top, double bot ) {
Misha Brukman8a96c532005-04-21 21:44:41 +0000590 Out << std::setw(30) << std::right << title
Reid Spencerf41aa732004-06-29 23:23:12 +0000591 << std::setw(0) << ": "
Misha Brukman8a96c532005-04-21 21:44:41 +0000592 << std::setw(9) << std::setprecision(6) << top
593 << " (" << std::left << std::setw(0) << std::setprecision(4)
Reid Spencerf41aa732004-06-29 23:23:12 +0000594 << (top/bot)*100.0 << "%)\n";
595}
596
597/// @brief Utility for printing a titled string value with
598/// an aligned colon.
Misha Brukman8a96c532005-04-21 21:44:41 +0000599inline static void print(std::ostream&Out, const char*title,
Reid Spencerf41aa732004-06-29 23:23:12 +0000600 std::string val, bool nl = true) {
Misha Brukman8a96c532005-04-21 21:44:41 +0000601 Out << std::setw(30) << std::right << title
Reid Spencerf41aa732004-06-29 23:23:12 +0000602 << std::setw(0) << ": "
603 << std::left << val << (nl ? "\n" : "");
604}
605
606}
607
608namespace llvm {
609
610/// This function prints the contents of rhe BytecodeAnalysis structure in
611/// a human legible form.
612/// @brief Print BytecodeAnalysis structure to an ostream
613void PrintBytecodeAnalysis(BytecodeAnalysis& bca, std::ostream& Out )
Reid Spencerdac69c82004-06-07 17:53:43 +0000614{
Reid Spencer911ec6d2004-08-21 20:58:19 +0000615 Out << "\nSummary Analysis Of " << bca.ModuleId << ": \n\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000616 print(Out, "Bytecode Analysis Of Module", bca.ModuleId);
Reid Spencer911ec6d2004-08-21 20:58:19 +0000617 print(Out, "Bytecode Version Number", bca.version);
Reid Spencerf41aa732004-06-29 23:23:12 +0000618 print(Out, "File Size", bca.byteSize);
Reid Spencer911ec6d2004-08-21 20:58:19 +0000619 print(Out, "Module Bytes",
620 double(bca.BlockSizes[BytecodeFormat::ModuleBlockID]),
621 double(bca.byteSize));
Misha Brukman8a96c532005-04-21 21:44:41 +0000622 print(Out, "Function Bytes",
Reid Spencer911ec6d2004-08-21 20:58:19 +0000623 double(bca.BlockSizes[BytecodeFormat::FunctionBlockID]),
624 double(bca.byteSize));
Misha Brukman8a96c532005-04-21 21:44:41 +0000625 print(Out, "Global Types Bytes",
Reid Spencer911ec6d2004-08-21 20:58:19 +0000626 double(bca.BlockSizes[BytecodeFormat::GlobalTypePlaneBlockID]),
627 double(bca.byteSize));
Misha Brukman8a96c532005-04-21 21:44:41 +0000628 print(Out, "Constant Pool Bytes",
Reid Spencer911ec6d2004-08-21 20:58:19 +0000629 double(bca.BlockSizes[BytecodeFormat::ConstantPoolBlockID]),
630 double(bca.byteSize));
Misha Brukman8a96c532005-04-21 21:44:41 +0000631 print(Out, "Module Globals Bytes",
Reid Spencer911ec6d2004-08-21 20:58:19 +0000632 double(bca.BlockSizes[BytecodeFormat::ModuleGlobalInfoBlockID]),
633 double(bca.byteSize));
Misha Brukman8a96c532005-04-21 21:44:41 +0000634 print(Out, "Instruction List Bytes",
Reid Spencer911ec6d2004-08-21 20:58:19 +0000635 double(bca.BlockSizes[BytecodeFormat::InstructionListBlockID]),
636 double(bca.byteSize));
Misha Brukman8a96c532005-04-21 21:44:41 +0000637 print(Out, "Compaction Table Bytes",
Reid Spencer911ec6d2004-08-21 20:58:19 +0000638 double(bca.BlockSizes[BytecodeFormat::CompactionTableBlockID]),
639 double(bca.byteSize));
Reid Spencer78d033e2007-01-06 07:24:44 +0000640 print(Out, "Value Symbol Table Bytes",
641 double(bca.BlockSizes[BytecodeFormat::ValueSymbolTableBlockID]),
642 double(bca.byteSize));
643 print(Out, "Type Symbol Table Bytes",
644 double(bca.BlockSizes[BytecodeFormat::TypeSymbolTableBlockID]),
Reid Spencer911ec6d2004-08-21 20:58:19 +0000645 double(bca.byteSize));
Misha Brukman8a96c532005-04-21 21:44:41 +0000646 print(Out, "Alignment Bytes",
Reid Spencer911ec6d2004-08-21 20:58:19 +0000647 double(bca.numAlignment), double(bca.byteSize));
Misha Brukman8a96c532005-04-21 21:44:41 +0000648 print(Out, "Block Header Bytes",
Reid Spencer911ec6d2004-08-21 20:58:19 +0000649 double(bca.BlockSizes[BytecodeFormat::Reserved_DoNotUse]),
650 double(bca.byteSize));
Misha Brukman8a96c532005-04-21 21:44:41 +0000651 print(Out, "Dependent Libraries Bytes", double(bca.libSize),
Reid Spencer911ec6d2004-08-21 20:58:19 +0000652 double(bca.byteSize));
Reid Spencerf41aa732004-06-29 23:23:12 +0000653 print(Out, "Number Of Bytecode Blocks", bca.numBlocks);
Reid Spencer911ec6d2004-08-21 20:58:19 +0000654 print(Out, "Number Of Functions", bca.numFunctions);
Reid Spencerf41aa732004-06-29 23:23:12 +0000655 print(Out, "Number Of Types", bca.numTypes);
Reid Spencerf41aa732004-06-29 23:23:12 +0000656 print(Out, "Number Of Constants", bca.numConstants);
657 print(Out, "Number Of Global Variables", bca.numGlobalVars);
Reid Spencer911ec6d2004-08-21 20:58:19 +0000658 print(Out, "Number Of Values", bca.numValues);
Reid Spencerf41aa732004-06-29 23:23:12 +0000659 print(Out, "Number Of Basic Blocks", bca.numBasicBlocks);
660 print(Out, "Number Of Instructions", bca.numInstructions);
Reid Spencer911ec6d2004-08-21 20:58:19 +0000661 print(Out, "Number Of Long Instructions", bca.longInstructions);
Reid Spencerf41aa732004-06-29 23:23:12 +0000662 print(Out, "Number Of Operands", bca.numOperands);
663 print(Out, "Number Of Compaction Tables", bca.numCmpctnTables);
664 print(Out, "Number Of Symbol Tables", bca.numSymTab);
Reid Spencer911ec6d2004-08-21 20:58:19 +0000665 print(Out, "Number Of Dependent Libs", bca.numLibraries);
666 print(Out, "Total Instruction Size", bca.instructionSize);
Misha Brukman8a96c532005-04-21 21:44:41 +0000667 print(Out, "Average Instruction Size",
Reid Spencer911ec6d2004-08-21 20:58:19 +0000668 double(bca.instructionSize)/double(bca.numInstructions));
669
Reid Spencerf41aa732004-06-29 23:23:12 +0000670 print(Out, "Maximum Type Slot Number", bca.maxTypeSlot);
671 print(Out, "Maximum Value Slot Number", bca.maxValueSlot);
Reid Spencer3120e712004-08-24 22:45:32 +0000672 print(Out, "Bytes Per Value ", bca.fileDensity);
673 print(Out, "Bytes Per Global", bca.globalsDensity);
674 print(Out, "Bytes Per Function", bca.functionDensity);
675 print(Out, "# of VBR 32-bit Integers", bca.vbrCount32);
676 print(Out, "# of VBR 64-bit Integers", bca.vbrCount64);
677 print(Out, "# of VBR Compressed Bytes", bca.vbrCompBytes);
678 print(Out, "# of VBR Expanded Bytes", bca.vbrExpdBytes);
Misha Brukman8a96c532005-04-21 21:44:41 +0000679 print(Out, "Bytes Saved With VBR",
Reid Spencer911ec6d2004-08-21 20:58:19 +0000680 double(bca.vbrExpdBytes)-double(bca.vbrCompBytes),
681 double(bca.vbrExpdBytes));
Reid Spencerf41aa732004-06-29 23:23:12 +0000682
Reid Spencer911ec6d2004-08-21 20:58:19 +0000683 if (bca.detailedResults) {
684 Out << "\nDetailed Analysis Of " << bca.ModuleId << " Functions:\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000685
Misha Brukman8a96c532005-04-21 21:44:41 +0000686 std::map<const Function*,BytecodeAnalysis::BytecodeFunctionInfo>::iterator I =
Reid Spencerf41aa732004-06-29 23:23:12 +0000687 bca.FunctionInfo.begin();
Misha Brukman8a96c532005-04-21 21:44:41 +0000688 std::map<const Function*,BytecodeAnalysis::BytecodeFunctionInfo>::iterator E =
Reid Spencerf41aa732004-06-29 23:23:12 +0000689 bca.FunctionInfo.end();
690
691 while ( I != E ) {
Chris Lattner4a8167f2004-10-15 19:40:31 +0000692 Out << std::left << std::setw(0) << "\n";
693 if (I->second.numBasicBlocks == 0) Out << "External ";
694 Out << "Function: " << I->second.name << "\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000695 print(Out, "Type:", I->second.description);
696 print(Out, "Byte Size", I->second.byteSize);
Chris Lattner4a8167f2004-10-15 19:40:31 +0000697 if (I->second.numBasicBlocks) {
698 print(Out, "Basic Blocks", I->second.numBasicBlocks);
699 print(Out, "Instructions", I->second.numInstructions);
700 print(Out, "Long Instructions", I->second.longInstructions);
701 print(Out, "Operands", I->second.numOperands);
702 print(Out, "Instruction Size", I->second.instructionSize);
Misha Brukman8a96c532005-04-21 21:44:41 +0000703 print(Out, "Average Instruction Size",
Chris Lattner4a8167f2004-10-15 19:40:31 +0000704 double(I->second.instructionSize) / I->second.numInstructions);
705 print(Out, "Bytes Per Instruction", I->second.density);
706 print(Out, "# of VBR 32-bit Integers", I->second.vbrCount32);
707 print(Out, "# of VBR 64-bit Integers", I->second.vbrCount64);
708 print(Out, "# of VBR Compressed Bytes", I->second.vbrCompBytes);
709 print(Out, "# of VBR Expanded Bytes", I->second.vbrExpdBytes);
Misha Brukman8a96c532005-04-21 21:44:41 +0000710 print(Out, "Bytes Saved With VBR",
Reid Spencere03f09c2006-11-03 01:44:51 +0000711 double(I->second.vbrExpdBytes) - I->second.vbrCompBytes);
Chris Lattner4a8167f2004-10-15 19:40:31 +0000712 }
Reid Spencerf41aa732004-06-29 23:23:12 +0000713 ++I;
714 }
715 }
716
Reid Spencerf41aa732004-06-29 23:23:12 +0000717 if ( bca.progressiveVerify )
718 Out << bca.VerifyInfo;
719}
720
Reid Spencer911ec6d2004-08-21 20:58:19 +0000721BytecodeHandler* createBytecodeAnalyzerHandler(BytecodeAnalysis& bca,
722 std::ostream* output)
Reid Spencerf41aa732004-06-29 23:23:12 +0000723{
Reid Spencer911ec6d2004-08-21 20:58:19 +0000724 return new AnalyzerHandler(bca,output);
Reid Spencerf41aa732004-06-29 23:23:12 +0000725}
726
Reid Spencerdac69c82004-06-07 17:53:43 +0000727}
728