blob: b85e3006a2418bf968e7ff5794e7d1589fd2481e [file] [log] [blame]
Reid Spencerf41aa732004-06-29 23:23:12 +00001//===-- Analyzer.cpp - Analysis and Dumping of Bytecode 000000---*- C++ -*-===//
Reid Spencerdac69c82004-06-07 17:53:43 +00002//
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//
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"
24#include "llvm/Bytecode/Analyzer.h"
25#include "llvm/Bytecode/BytecodeHandler.h"
26#include <iomanip>
27#include <sstream>
Reid Spencerdac69c82004-06-07 17:53:43 +000028
29using namespace llvm;
30
Reid Spencerdac69c82004-06-07 17:53:43 +000031namespace {
32
Reid Spencerf41aa732004-06-29 23:23:12 +000033/// @brief Bytecode reading handler for analyzing bytecode.
Reid Spencerdac69c82004-06-07 17:53:43 +000034class AnalyzerHandler : public BytecodeHandler {
Reid Spencerf41aa732004-06-29 23:23:12 +000035 BytecodeAnalysis& bca; ///< The structure in which data is recorded
36 std::ostringstream dump; ///< A convenience for dumping data.
37 /// @brief Keeps track of current function
38 BytecodeAnalysis::BytecodeFunctionInfo* currFunc;
39 Module* M; ///< Keeps track of current module
40
41/// @name Constructor
42/// @{
Reid Spencerdac69c82004-06-07 17:53:43 +000043public:
Reid Spencerf41aa732004-06-29 23:23:12 +000044 /// 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.
Reid Spencer649ee572004-06-09 06:16:43 +000047 AnalyzerHandler(BytecodeAnalysis& TheBca)
Reid Spencercbb22e22004-06-10 22:00:54 +000048 : bca(TheBca)
Reid Spencerf41aa732004-06-29 23:23:12 +000049 , dump()
Reid Spencercbb22e22004-06-10 22:00:54 +000050 , currFunc(0)
51 { }
Reid Spencer649ee572004-06-09 06:16:43 +000052
Reid Spencerf41aa732004-06-29 23:23:12 +000053/// @}
54/// @name BytecodeHandler Implementations
55/// @{
56public:
57 virtual void handleError(const std::string& str ) {
58 dump << "ERROR: " << str << "\n";
59 bca.BytecodeDump = dump.str() ;
Reid Spencerdac69c82004-06-07 17:53:43 +000060 }
61
Reid Spencerf41aa732004-06-29 23:23:12 +000062 virtual void handleStart( Module* Mod, unsigned theSize ) {
63 M = Mod;
64 dump << "Bytecode {\n";
65 bca.byteSize = theSize;
Reid Spencer649ee572004-06-09 06:16:43 +000066 bca.ModuleId.clear();
Reid Spencer00c28a72004-06-10 08:09:13 +000067 bca.numBlocks = 0;
Reid Spencer649ee572004-06-09 06:16:43 +000068 bca.numTypes = 0;
69 bca.numValues = 0;
70 bca.numFunctions = 0;
71 bca.numConstants = 0;
72 bca.numGlobalVars = 0;
73 bca.numInstructions = 0;
74 bca.numBasicBlocks = 0;
75 bca.numOperands = 0;
76 bca.numCmpctnTables = 0;
77 bca.numSymTab = 0;
78 bca.maxTypeSlot = 0;
79 bca.maxValueSlot = 0;
Reid Spencer00c28a72004-06-10 08:09:13 +000080 bca.numAlignment = 0;
81 bca.fileDensity = 0.0;
82 bca.globalsDensity = 0.0;
83 bca.functionDensity = 0.0;
Reid Spencer1cf50242004-06-11 15:10:38 +000084 bca.instructionSize = 0;
85 bca.longInstructions = 0;
Reid Spencer00c28a72004-06-10 08:09:13 +000086 bca.vbrCount32 = 0;
87 bca.vbrCount64 = 0;
88 bca.vbrCompBytes = 0;
89 bca.vbrExpdBytes = 0;
Reid Spencer649ee572004-06-09 06:16:43 +000090 bca.FunctionInfo.clear();
91 bca.BytecodeDump.clear();
Reid Spencer00c28a72004-06-10 08:09:13 +000092 bca.BlockSizes[BytecodeFormat::Module] = 0;
93 bca.BlockSizes[BytecodeFormat::Function] = 0;
94 bca.BlockSizes[BytecodeFormat::ConstantPool] = 0;
95 bca.BlockSizes[BytecodeFormat::SymbolTable] = 0;
96 bca.BlockSizes[BytecodeFormat::ModuleGlobalInfo] = 0;
97 bca.BlockSizes[BytecodeFormat::GlobalTypePlane] = 0;
98 bca.BlockSizes[BytecodeFormat::BasicBlock] = 0;
99 bca.BlockSizes[BytecodeFormat::InstructionList] = 0;
100 bca.BlockSizes[BytecodeFormat::CompactionTable] = 0;
Reid Spencerdac69c82004-06-07 17:53:43 +0000101 }
102
Reid Spencercbb22e22004-06-10 22:00:54 +0000103 virtual void handleFinish() {
Reid Spencerf41aa732004-06-29 23:23:12 +0000104 dump << "} End Bytecode\n";
105 bca.BytecodeDump = dump.str() ;
106
Reid Spencer00c28a72004-06-10 08:09:13 +0000107 bca.fileDensity = double(bca.byteSize) / double( bca.numTypes + bca.numValues );
108 double globalSize = 0.0;
109 globalSize += double(bca.BlockSizes[BytecodeFormat::ConstantPool]);
110 globalSize += double(bca.BlockSizes[BytecodeFormat::ModuleGlobalInfo]);
111 globalSize += double(bca.BlockSizes[BytecodeFormat::GlobalTypePlane]);
112 bca.globalsDensity = globalSize / double( bca.numTypes + bca.numConstants +
113 bca.numGlobalVars );
114 bca.functionDensity = double(bca.BlockSizes[BytecodeFormat::Function]) /
115 double(bca.numFunctions);
Reid Spencerf41aa732004-06-29 23:23:12 +0000116
117 if ( bca.progressiveVerify ) {
118 try {
119 verifyModule(*M, ThrowExceptionAction);
120 } catch ( std::string& msg ) {
121 bca.VerifyInfo += "Verify@Finish: " + msg + "\n";
122 }
123 }
Reid Spencerdac69c82004-06-07 17:53:43 +0000124 }
125
Reid Spencercbb22e22004-06-10 22:00:54 +0000126 virtual void handleModuleBegin(const std::string& id) {
Reid Spencerf41aa732004-06-29 23:23:12 +0000127 dump << " Module " << id << " {\n";
Reid Spencer649ee572004-06-09 06:16:43 +0000128 bca.ModuleId = id;
Reid Spencerdac69c82004-06-07 17:53:43 +0000129 }
130
Reid Spencerf41aa732004-06-29 23:23:12 +0000131 virtual void handleModuleEnd(const std::string& id) {
132 dump << " } End Module " << id << "\n";
133 if ( bca.progressiveVerify ) {
134 try {
135 verifyModule(*M, ThrowExceptionAction);
136 } catch ( std::string& msg ) {
137 bca.VerifyInfo += "Verify@EndModule: " + msg + "\n";
138 }
139 }
140 }
Reid Spencerdac69c82004-06-07 17:53:43 +0000141
Reid Spencercbb22e22004-06-10 22:00:54 +0000142 virtual void handleVersionInfo(
Reid Spencerdac69c82004-06-07 17:53:43 +0000143 unsigned char RevisionNum, ///< Byte code revision number
144 Module::Endianness Endianness, ///< Endianness indicator
145 Module::PointerSize PointerSize ///< PointerSize indicator
Reid Spencerf41aa732004-06-29 23:23:12 +0000146 ) {
147 dump << " RevisionNum: " << int(RevisionNum)
148 << " Endianness: " << Endianness
149 << " PointerSize: " << PointerSize << "\n";
150 }
Reid Spencerdac69c82004-06-07 17:53:43 +0000151
Reid Spencerf41aa732004-06-29 23:23:12 +0000152 virtual void handleModuleGlobalsBegin() {
153 dump << " BLOCK: ModuleGlobalInfo {\n";
154 }
Reid Spencerdac69c82004-06-07 17:53:43 +0000155
Reid Spencercbb22e22004-06-10 22:00:54 +0000156 virtual void handleGlobalVariable(
Reid Spencerf41aa732004-06-29 23:23:12 +0000157 const Type* ElemType,
158 bool isConstant,
159 GlobalValue::LinkageTypes Linkage,
160 unsigned SlotNum,
161 unsigned initSlot
Reid Spencercbb22e22004-06-10 22:00:54 +0000162 ) {
Reid Spencer649ee572004-06-09 06:16:43 +0000163 bca.numGlobalVars++;
Reid Spencer00c28a72004-06-10 08:09:13 +0000164 bca.numValues++;
Reid Spencerf41aa732004-06-29 23:23:12 +0000165
166 dump << " GV: "
167 << ( initSlot == 0 ? "Uni" : "I" ) << "nitialized, "
168 << ( isConstant? "Constant, " : "Variable, ")
169 << " Linkage=" << Linkage << " Type="
170 << ElemType->getDescription()
171 << " Slot=" << SlotNum << " InitSlot=" << initSlot
172 << "\n";
Reid Spencerdac69c82004-06-07 17:53:43 +0000173 }
174
Reid Spencerf41aa732004-06-29 23:23:12 +0000175 virtual void handleType( const Type* Ty ) {
176 bca.numTypes++;
177 dump << " Type: " << Ty->getDescription() << "\n";
Reid Spencerdac69c82004-06-07 17:53:43 +0000178 }
179
Reid Spencercbb22e22004-06-10 22:00:54 +0000180 virtual void handleFunctionDeclaration(
Reid Spencerf41aa732004-06-29 23:23:12 +0000181 Function* Func ///< The function
Reid Spencercbb22e22004-06-10 22:00:54 +0000182 ) {
Reid Spencer649ee572004-06-09 06:16:43 +0000183 bca.numFunctions++;
Reid Spencer00c28a72004-06-10 08:09:13 +0000184 bca.numValues++;
Reid Spencerf41aa732004-06-29 23:23:12 +0000185 dump << " Function Decl: " << Func->getType()->getDescription() << "\n";
Reid Spencerdac69c82004-06-07 17:53:43 +0000186 }
187
Reid Spencerf41aa732004-06-29 23:23:12 +0000188 virtual void handleGlobalInitializer(GlobalVariable* GV, Constant* CV) {
189 dump << " Initializer: GV=";
190 GV->print(dump);
191 dump << " CV=";
192 CV->print(dump);
193 dump << "\n";
194 }
Reid Spencerdac69c82004-06-07 17:53:43 +0000195
Reid Spencerf41aa732004-06-29 23:23:12 +0000196 virtual void handleModuleGlobalsEnd() {
197 dump << " } END BLOCK: ModuleGlobalInfo\n";
198 if ( bca.progressiveVerify ) {
199 try {
200 verifyModule(*M, ThrowExceptionAction);
201 } catch ( std::string& msg ) {
202 bca.VerifyInfo += "Verify@EndModuleGlobalInfo: " + msg + "\n";
203 }
204 }
205 }
206
207 virtual void handleCompactionTableBegin() {
208 dump << " BLOCK: CompactionTable {\n";
209 }
Reid Spencerdac69c82004-06-07 17:53:43 +0000210
Reid Spencercbb22e22004-06-10 22:00:54 +0000211 virtual void handleCompactionTablePlane( unsigned Ty, unsigned NumEntries) {
Reid Spencer649ee572004-06-09 06:16:43 +0000212 bca.numCmpctnTables++;
Reid Spencerf41aa732004-06-29 23:23:12 +0000213 dump << " Plane: Ty=" << Ty << " Size=" << NumEntries << "\n";
Reid Spencerdac69c82004-06-07 17:53:43 +0000214 }
215
Reid Spencercbb22e22004-06-10 22:00:54 +0000216 virtual void handleCompactionTableType( unsigned i, unsigned TypSlot,
Reid Spencerf41aa732004-06-29 23:23:12 +0000217 const Type* Ty ) {
218 dump << " Type: " << i << " Slot:" << TypSlot
219 << " is " << Ty->getDescription() << "\n";
220 }
Reid Spencerdac69c82004-06-07 17:53:43 +0000221
Reid Spencercbb22e22004-06-10 22:00:54 +0000222 virtual void handleCompactionTableValue(
Reid Spencerdac69c82004-06-07 17:53:43 +0000223 unsigned i,
Reid Spencerf41aa732004-06-29 23:23:12 +0000224 unsigned TypSlot,
Reid Spencerdac69c82004-06-07 17:53:43 +0000225 unsigned ValSlot,
Reid Spencerf41aa732004-06-29 23:23:12 +0000226 const Type* Ty ) {
227 dump << " Value: " << i << " TypSlot: " << TypSlot
228 << " ValSlot:" << ValSlot << " is " << Ty->getDescription()
229 << "\n";
230 }
Reid Spencercbb22e22004-06-10 22:00:54 +0000231
Reid Spencerf41aa732004-06-29 23:23:12 +0000232 virtual void handleCompactionTableEnd() {
233 dump << " } END BLOCK: CompactionTable\n";
234 }
Reid Spencercbb22e22004-06-10 22:00:54 +0000235
Reid Spencerf41aa732004-06-29 23:23:12 +0000236 virtual void handleSymbolTableBegin(Function* CF, SymbolTable* ST) {
237 bca.numSymTab++;
238 dump << " BLOCK: SymbolTable {\n";
239 }
Reid Spencercbb22e22004-06-10 22:00:54 +0000240
Reid Spencerf41aa732004-06-29 23:23:12 +0000241 virtual void handleSymbolTablePlane(unsigned Ty, unsigned NumEntries,
242 const Type* Typ) {
243 dump << " Plane: Ty=" << Ty << " Size=" << NumEntries
244 << " Type: " << Typ->getDescription() << "\n";
245 }
Reid Spencercbb22e22004-06-10 22:00:54 +0000246
Reid Spencerf41aa732004-06-29 23:23:12 +0000247 virtual void handleSymbolTableType(unsigned i, unsigned slot,
248 const std::string& name ) {
249 dump << " Type " << i << " Slot=" << slot
250 << " Name: " << name << "\n";
251 }
Reid Spencercbb22e22004-06-10 22:00:54 +0000252
Reid Spencerf41aa732004-06-29 23:23:12 +0000253 virtual void handleSymbolTableValue(unsigned i, unsigned slot,
254 const std::string& name ) {
255 dump << " Value " << i << " Slot=" << slot
256 << " Name: " << name << "\n";
257 }
Reid Spencercbb22e22004-06-10 22:00:54 +0000258
Reid Spencerf41aa732004-06-29 23:23:12 +0000259 virtual void handleSymbolTableEnd() {
260 dump << " } END BLOCK: SymbolTable\n";
261 }
Reid Spencercbb22e22004-06-10 22:00:54 +0000262
Reid Spencerf41aa732004-06-29 23:23:12 +0000263 virtual void handleFunctionBegin(Function* Func, unsigned Size) {
264 dump << "BLOCK: Function {\n";
265 dump << " Linkage: " << Func->getLinkage() << "\n";
266 dump << " Type: " << Func->getType()->getDescription() << "\n";
Reid Spencercbb22e22004-06-10 22:00:54 +0000267 const FunctionType* FType =
268 cast<FunctionType>(Func->getType()->getElementType());
269 currFunc = &bca.FunctionInfo[Func];
270 currFunc->description = FType->getDescription();
271 currFunc->name = Func->getName();
272 currFunc->byteSize = Size;
273 currFunc->numInstructions = 0;
274 currFunc->numBasicBlocks = 0;
275 currFunc->numPhis = 0;
276 currFunc->numOperands = 0;
277 currFunc->density = 0.0;
Reid Spencer1cf50242004-06-11 15:10:38 +0000278 currFunc->instructionSize = 0;
279 currFunc->longInstructions = 0;
Reid Spencercbb22e22004-06-10 22:00:54 +0000280 currFunc->vbrCount32 = 0;
281 currFunc->vbrCount64 = 0;
282 currFunc->vbrCompBytes = 0;
283 currFunc->vbrExpdBytes = 0;
Reid Spencerf41aa732004-06-29 23:23:12 +0000284
Reid Spencerdac69c82004-06-07 17:53:43 +0000285 }
286
Reid Spencercbb22e22004-06-10 22:00:54 +0000287 virtual void handleFunctionEnd( Function* Func) {
Reid Spencerf41aa732004-06-29 23:23:12 +0000288 dump << "} END BLOCK: Function\n";
Reid Spencercbb22e22004-06-10 22:00:54 +0000289 currFunc->density = double(currFunc->byteSize) /
290 double(currFunc->numInstructions+currFunc->numBasicBlocks);
Reid Spencerf41aa732004-06-29 23:23:12 +0000291
292 if ( bca.progressiveVerify ) {
293 try {
294 verifyModule(*M, ThrowExceptionAction);
295 } catch ( std::string& msg ) {
296 bca.VerifyInfo += "Verify@EndFunction: " + msg + "\n";
297 }
298 }
Reid Spencerdac69c82004-06-07 17:53:43 +0000299 }
300
Reid Spencercbb22e22004-06-10 22:00:54 +0000301 virtual void handleBasicBlockBegin( unsigned blocknum) {
Reid Spencerf41aa732004-06-29 23:23:12 +0000302 dump << " BLOCK: BasicBlock #" << blocknum << "{\n";
Reid Spencer649ee572004-06-09 06:16:43 +0000303 bca.numBasicBlocks++;
Reid Spencer00c28a72004-06-10 08:09:13 +0000304 bca.numValues++;
Reid Spencercbb22e22004-06-10 22:00:54 +0000305 if ( currFunc ) currFunc->numBasicBlocks++;
Reid Spencerdac69c82004-06-07 17:53:43 +0000306 }
307
Reid Spencercbb22e22004-06-10 22:00:54 +0000308 virtual bool handleInstruction( unsigned Opcode, const Type* iType,
Reid Spencerf41aa732004-06-29 23:23:12 +0000309 std::vector<unsigned>& Operands, unsigned Size){
310 dump << " INST: OpCode="
311 << Instruction::getOpcodeName(Opcode) << " Type="
312 << iType->getDescription() << "\n";
313 for ( unsigned i = 0; i < Operands.size(); ++i )
314 dump << " Op#" << i << " Slot=" << Operands[i] << "\n";
315
Reid Spencer649ee572004-06-09 06:16:43 +0000316 bca.numInstructions++;
Reid Spencer00c28a72004-06-10 08:09:13 +0000317 bca.numValues++;
Reid Spencer1cf50242004-06-11 15:10:38 +0000318 bca.instructionSize += Size;
319 if (Size > 4 ) bca.longInstructions++;
Reid Spencer00c28a72004-06-10 08:09:13 +0000320 bca.numOperands += Operands.size();
Reid Spencercbb22e22004-06-10 22:00:54 +0000321 if ( currFunc ) {
322 currFunc->numInstructions++;
Reid Spencer1cf50242004-06-11 15:10:38 +0000323 currFunc->instructionSize += Size;
324 if (Size > 4 ) currFunc->longInstructions++;
Reid Spencer8a9a3702004-06-11 03:06:43 +0000325 if ( Opcode == Instruction::PHI ) currFunc->numPhis++;
Reid Spencercbb22e22004-06-10 22:00:54 +0000326 }
Reid Spencer649ee572004-06-09 06:16:43 +0000327 return Instruction::isTerminator(Opcode);
Reid Spencerdac69c82004-06-07 17:53:43 +0000328 }
329
Reid Spencerf41aa732004-06-29 23:23:12 +0000330 virtual void handleBasicBlockEnd(unsigned blocknum) {
331 dump << " } END BLOCK: BasicBlock #" << blocknum << "{\n";
332 }
Reid Spencerdac69c82004-06-07 17:53:43 +0000333
Reid Spencerf41aa732004-06-29 23:23:12 +0000334 virtual void handleGlobalConstantsBegin() {
335 dump << " BLOCK: GlobalConstants {\n";
336 }
Reid Spencerdac69c82004-06-07 17:53:43 +0000337
Reid Spencerf41aa732004-06-29 23:23:12 +0000338 virtual void handleConstantExpression( unsigned Opcode,
339 std::vector<Constant*> ArgVec, Constant* C ) {
340 dump << " EXPR: " << Instruction::getOpcodeName(Opcode) << "\n";
341 for ( unsigned i = 0; i < ArgVec.size(); ++i ) {
342 dump << " Arg#" << i << " "; ArgVec[i]->print(dump); dump << "\n";
343 }
344 dump << " Value=";
345 C->print(dump);
346 dump << "\n";
Reid Spencer649ee572004-06-09 06:16:43 +0000347 bca.numConstants++;
Reid Spencer00c28a72004-06-10 08:09:13 +0000348 bca.numValues++;
Reid Spencerdac69c82004-06-07 17:53:43 +0000349 }
350
Reid Spencercbb22e22004-06-10 22:00:54 +0000351 virtual void handleConstantValue( Constant * c ) {
Reid Spencerf41aa732004-06-29 23:23:12 +0000352 dump << " VALUE: ";
353 c->print(dump);
354 dump << "\n";
Reid Spencer649ee572004-06-09 06:16:43 +0000355 bca.numConstants++;
Reid Spencer00c28a72004-06-10 08:09:13 +0000356 bca.numValues++;
Reid Spencerdac69c82004-06-07 17:53:43 +0000357 }
358
Reid Spencercbb22e22004-06-10 22:00:54 +0000359 virtual void handleConstantArray( const ArrayType* AT,
Reid Spencerf41aa732004-06-29 23:23:12 +0000360 std::vector<Constant*>& Elements,
361 unsigned TypeSlot,
362 Constant* ArrayVal ) {
363 dump << " ARRAY: " << AT->getDescription()
364 << " TypeSlot=" << TypeSlot << "\n";
365 for ( unsigned i = 0; i < Elements.size(); ++i ) {
366 dump << " #" << i;
367 Elements[i]->print(dump);
368 dump << "\n";
369 }
370 dump << " Value=";
371 ArrayVal->print(dump);
372 dump << "\n";
373
Reid Spencer649ee572004-06-09 06:16:43 +0000374 bca.numConstants++;
Reid Spencer00c28a72004-06-10 08:09:13 +0000375 bca.numValues++;
Reid Spencerdac69c82004-06-07 17:53:43 +0000376 }
377
Reid Spencercbb22e22004-06-10 22:00:54 +0000378 virtual void handleConstantStruct(
Reid Spencer00c28a72004-06-10 08:09:13 +0000379 const StructType* ST,
Reid Spencerf41aa732004-06-29 23:23:12 +0000380 std::vector<Constant*>& Elements,
381 Constant* StructVal)
Reid Spencerdac69c82004-06-07 17:53:43 +0000382 {
Reid Spencerf41aa732004-06-29 23:23:12 +0000383 dump << " STRUC: " << ST->getDescription() << "\n";
384 for ( unsigned i = 0; i < Elements.size(); ++i ) {
385 dump << " #" << i << " "; Elements[i]->print(dump); dump << "\n";
386 }
387 dump << " Value=";
388 StructVal->print(dump);
389 dump << "\n";
Reid Spencer649ee572004-06-09 06:16:43 +0000390 bca.numConstants++;
Reid Spencer00c28a72004-06-10 08:09:13 +0000391 bca.numValues++;
Reid Spencerdac69c82004-06-07 17:53:43 +0000392 }
393
Reid Spencerf41aa732004-06-29 23:23:12 +0000394 virtual void handleConstantPointer( const PointerType* PT,
395 unsigned Slot, GlobalValue* GV, Constant* PtrVal) {
396 dump << " PNTR: " << PT->getDescription()
397 << " Slot=" << Slot << " GlobalValue=";
398 GV->print(dump);
399 dump << "\n Value=";
400 PtrVal->print(dump);
401 dump << "\n";
Reid Spencer649ee572004-06-09 06:16:43 +0000402 bca.numConstants++;
Reid Spencer00c28a72004-06-10 08:09:13 +0000403 bca.numValues++;
Reid Spencerdac69c82004-06-07 17:53:43 +0000404 }
405
Reid Spencercbb22e22004-06-10 22:00:54 +0000406 virtual void handleConstantString( const ConstantArray* CA ) {
Reid Spencerf41aa732004-06-29 23:23:12 +0000407 dump << " STRNG: ";
408 CA->print(dump);
409 dump << "\n";
Reid Spencer649ee572004-06-09 06:16:43 +0000410 bca.numConstants++;
Reid Spencer00c28a72004-06-10 08:09:13 +0000411 bca.numValues++;
Reid Spencerdac69c82004-06-07 17:53:43 +0000412 }
413
Reid Spencerf41aa732004-06-29 23:23:12 +0000414 virtual void handleGlobalConstantsEnd() {
415 dump << " } END BLOCK: GlobalConstants\n";
416 if ( bca.progressiveVerify ) {
417 try {
418 verifyModule(*M, ThrowExceptionAction);
419 } catch ( std::string& msg ) {
420 bca.VerifyInfo += "Verify@EndGlobalConstants: " + msg + "\n";
421 }
422 }
423 }
Reid Spencerdac69c82004-06-07 17:53:43 +0000424
Reid Spencercbb22e22004-06-10 22:00:54 +0000425 virtual void handleAlignment(unsigned numBytes) {
Reid Spencer00c28a72004-06-10 08:09:13 +0000426 bca.numAlignment += numBytes;
Reid Spencerdac69c82004-06-07 17:53:43 +0000427 }
428
Reid Spencercbb22e22004-06-10 22:00:54 +0000429 virtual void handleBlock(
Reid Spencer00c28a72004-06-10 08:09:13 +0000430 unsigned BType, const unsigned char* StartPtr, unsigned Size) {
431 bca.numBlocks++;
432 bca.BlockSizes[llvm::BytecodeFormat::FileBlockIDs(BType)] += Size;
433 }
434
435 virtual void handleVBR32(unsigned Size ) {
436 bca.vbrCount32++;
437 bca.vbrCompBytes += Size;
438 bca.vbrExpdBytes += sizeof(uint32_t);
Reid Spencercbb22e22004-06-10 22:00:54 +0000439 if (currFunc) {
440 currFunc->vbrCount32++;
441 currFunc->vbrCompBytes += Size;
442 currFunc->vbrExpdBytes += sizeof(uint32_t);
443 }
Reid Spencer00c28a72004-06-10 08:09:13 +0000444 }
Reid Spencercbb22e22004-06-10 22:00:54 +0000445
Reid Spencer00c28a72004-06-10 08:09:13 +0000446 virtual void handleVBR64(unsigned Size ) {
447 bca.vbrCount64++;
448 bca.vbrCompBytes += Size;
449 bca.vbrExpdBytes += sizeof(uint64_t);
Reid Spencercbb22e22004-06-10 22:00:54 +0000450 if ( currFunc ) {
451 currFunc->vbrCount64++;
452 currFunc->vbrCompBytes += Size;
453 currFunc->vbrExpdBytes += sizeof(uint64_t);
454 }
Reid Spencer00c28a72004-06-10 08:09:13 +0000455 }
Reid Spencerdac69c82004-06-07 17:53:43 +0000456};
457
Reid Spencerf41aa732004-06-29 23:23:12 +0000458
459/// @brief Utility for printing a titled unsigned value with
460/// an aligned colon.
461inline static void print(std::ostream& Out, const char*title,
462 unsigned val, bool nl = true ) {
463 Out << std::setw(30) << std::right << title
464 << std::setw(0) << ": "
465 << std::setw(9) << val << "\n";
Reid Spencerdac69c82004-06-07 17:53:43 +0000466}
467
Reid Spencerf41aa732004-06-29 23:23:12 +0000468/// @brief Utility for printing a titled double value with an
469/// aligned colon
470inline static void print(std::ostream&Out, const char*title,
471 double val ) {
472 Out << std::setw(30) << std::right << title
473 << std::setw(0) << ": "
474 << std::setw(9) << std::setprecision(6) << val << "\n" ;
475}
476
477/// @brief Utility for printing a titled double value with a
478/// percentage and aligned colon.
479inline static void print(std::ostream&Out, const char*title,
480 double top, double bot ) {
481 Out << std::setw(30) << std::right << title
482 << std::setw(0) << ": "
483 << std::setw(9) << std::setprecision(6) << top
484 << " (" << std::left << std::setw(0) << std::setprecision(4)
485 << (top/bot)*100.0 << "%)\n";
486}
487
488/// @brief Utility for printing a titled string value with
489/// an aligned colon.
490inline static void print(std::ostream&Out, const char*title,
491 std::string val, bool nl = true) {
492 Out << std::setw(30) << std::right << title
493 << std::setw(0) << ": "
494 << std::left << val << (nl ? "\n" : "");
495}
496
497}
498
499namespace llvm {
500
501/// This function prints the contents of rhe BytecodeAnalysis structure in
502/// a human legible form.
503/// @brief Print BytecodeAnalysis structure to an ostream
504void PrintBytecodeAnalysis(BytecodeAnalysis& bca, std::ostream& Out )
Reid Spencerdac69c82004-06-07 17:53:43 +0000505{
Reid Spencerf41aa732004-06-29 23:23:12 +0000506 print(Out, "Bytecode Analysis Of Module", bca.ModuleId);
507 print(Out, "File Size", bca.byteSize);
508 print(Out, "Bytecode Compression Index",std::string("TBD"));
509 print(Out, "Number Of Bytecode Blocks", bca.numBlocks);
510 print(Out, "Number Of Types", bca.numTypes);
511 print(Out, "Number Of Values", bca.numValues);
512 print(Out, "Number Of Constants", bca.numConstants);
513 print(Out, "Number Of Global Variables", bca.numGlobalVars);
514 print(Out, "Number Of Functions", bca.numFunctions);
515 print(Out, "Number Of Basic Blocks", bca.numBasicBlocks);
516 print(Out, "Number Of Instructions", bca.numInstructions);
517 print(Out, "Number Of Operands", bca.numOperands);
518 print(Out, "Number Of Compaction Tables", bca.numCmpctnTables);
519 print(Out, "Number Of Symbol Tables", bca.numSymTab);
520 print(Out, "Long Instructions", bca.longInstructions);
521 print(Out, "Instruction Size", bca.instructionSize);
522 print(Out, "Average Instruction Size",
523 double(bca.instructionSize)/double(bca.numInstructions));
524 print(Out, "Maximum Type Slot Number", bca.maxTypeSlot);
525 print(Out, "Maximum Value Slot Number", bca.maxValueSlot);
526 print(Out, "Bytes Thrown To Alignment", double(bca.numAlignment),
527 double(bca.byteSize));
528 print(Out, "File Density (bytes/def)", bca.fileDensity);
529 print(Out, "Globals Density (bytes/def)", bca.globalsDensity);
530 print(Out, "Function Density (bytes/func)", bca.functionDensity);
531 print(Out, "Number of VBR 32-bit Integers", bca.vbrCount32);
532 print(Out, "Number of VBR 64-bit Integers", bca.vbrCount64);
533 print(Out, "Number of VBR Compressed Bytes", bca.vbrCompBytes);
534 print(Out, "Number of VBR Expanded Bytes", bca.vbrExpdBytes);
535 print(Out, "VBR Savings",
536 double(bca.vbrExpdBytes)-double(bca.vbrCompBytes),
537 double(bca.byteSize));
538
539 if ( bca.detailedResults ) {
540 print(Out, "Module Bytes",
541 double(bca.BlockSizes[BytecodeFormat::Module]),
542 double(bca.byteSize));
543 print(Out, "Function Bytes",
544 double(bca.BlockSizes[BytecodeFormat::Function]),
545 double(bca.byteSize));
546 print(Out, "Constant Pool Bytes",
547 double(bca.BlockSizes[BytecodeFormat::ConstantPool]),
548 double(bca.byteSize));
549 print(Out, "Symbol Table Bytes",
550 double(bca.BlockSizes[BytecodeFormat::SymbolTable]),
551 double(bca.byteSize));
552 print(Out, "Module Global Info Bytes",
553 double(bca.BlockSizes[BytecodeFormat::ModuleGlobalInfo]),
554 double(bca.byteSize));
555 print(Out, "Global Type Plane Bytes",
556 double(bca.BlockSizes[BytecodeFormat::GlobalTypePlane]),
557 double(bca.byteSize));
558 print(Out, "Basic Block Bytes",
559 double(bca.BlockSizes[BytecodeFormat::BasicBlock]),
560 double(bca.byteSize));
561 print(Out, "Instruction List Bytes",
562 double(bca.BlockSizes[BytecodeFormat::InstructionList]),
563 double(bca.byteSize));
564 print(Out, "Compaction Table Bytes",
565 double(bca.BlockSizes[BytecodeFormat::CompactionTable]),
566 double(bca.byteSize));
567
568 std::map<const Function*,BytecodeAnalysis::BytecodeFunctionInfo>::iterator I =
569 bca.FunctionInfo.begin();
570 std::map<const Function*,BytecodeAnalysis::BytecodeFunctionInfo>::iterator E =
571 bca.FunctionInfo.end();
572
573 while ( I != E ) {
574 Out << std::left << std::setw(0);
575 Out << "Function: " << I->second.name << "\n";
576 print(Out, "Type:", I->second.description);
577 print(Out, "Byte Size", I->second.byteSize);
578 print(Out, "Instructions", I->second.numInstructions);
579 print(Out, "Long Instructions", I->second.longInstructions);
580 print(Out, "Instruction Size", I->second.instructionSize);
581 print(Out, "Average Instruction Size",
582 double(I->second.instructionSize)/double(I->second.numInstructions));
583 print(Out, "Basic Blocks", I->second.numBasicBlocks);
584 print(Out, "Operand", I->second.numOperands);
585 print(Out, "Function Density", I->second.density);
586 print(Out, "Number of VBR 32-bit Integers", I->second.vbrCount32);
587 print(Out, "Number of VBR 64-bit Integers", I->second.vbrCount64);
588 print(Out, "Number of VBR Compressed Bytes", I->second.vbrCompBytes);
589 print(Out, "Number of VBR Expanded Bytes", I->second.vbrExpdBytes);
590 print(Out, "VBR Savings",
591 double(I->second.vbrExpdBytes)-double(I->second.vbrCompBytes),
592 double(I->second.byteSize));
593 ++I;
594 }
595 }
596
597 if ( bca.dumpBytecode )
598 Out << bca.BytecodeDump;
599
600 if ( bca.progressiveVerify )
601 Out << bca.VerifyInfo;
602}
603
604BytecodeHandler* createBytecodeAnalyzerHandler(BytecodeAnalysis& bca)
605{
606 return new AnalyzerHandler(bca);
607}
608
Reid Spencerdac69c82004-06-07 17:53:43 +0000609}
610
611// vim: sw=2