blob: f6739ae3c6fbe3803bcd6799d84b9effcff18347 [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"
Reid Spencerf41aa732004-06-29 23:23:12 +000024#include "llvm/Bytecode/BytecodeHandler.h"
25#include <iomanip>
26#include <sstream>
Reid Spencerdac69c82004-06-07 17:53:43 +000027
28using namespace llvm;
29
Reid Spencerdac69c82004-06-07 17:53:43 +000030namespace {
31
Reid Spencerf41aa732004-06-29 23:23:12 +000032/// @brief Bytecode reading handler for analyzing bytecode.
Reid Spencerdac69c82004-06-07 17:53:43 +000033class AnalyzerHandler : public BytecodeHandler {
Reid Spencerf41aa732004-06-29 23:23:12 +000034 BytecodeAnalysis& bca; ///< The structure in which data is recorded
35 std::ostringstream dump; ///< A convenience for dumping data.
36 /// @brief Keeps track of current function
37 BytecodeAnalysis::BytecodeFunctionInfo* currFunc;
38 Module* M; ///< Keeps track of current module
39
40/// @name Constructor
41/// @{
Reid Spencerdac69c82004-06-07 17:53:43 +000042public:
Reid Spencerf41aa732004-06-29 23:23:12 +000043 /// The only way to construct an AnalyzerHandler. All that is needed is a
44 /// reference to the BytecodeAnalysis structure where the output will be
45 /// placed.
Reid Spencer649ee572004-06-09 06:16:43 +000046 AnalyzerHandler(BytecodeAnalysis& TheBca)
Reid Spencercbb22e22004-06-10 22:00:54 +000047 : bca(TheBca)
Reid Spencerf41aa732004-06-29 23:23:12 +000048 , dump()
Reid Spencercbb22e22004-06-10 22:00:54 +000049 , currFunc(0)
50 { }
Reid Spencer649ee572004-06-09 06:16:43 +000051
Reid Spencerf41aa732004-06-29 23:23:12 +000052/// @}
53/// @name BytecodeHandler Implementations
54/// @{
55public:
56 virtual void handleError(const std::string& str ) {
57 dump << "ERROR: " << str << "\n";
58 bca.BytecodeDump = dump.str() ;
Reid Spencerdac69c82004-06-07 17:53:43 +000059 }
60
Reid Spencerf41aa732004-06-29 23:23:12 +000061 virtual void handleStart( Module* Mod, unsigned theSize ) {
62 M = Mod;
63 dump << "Bytecode {\n";
64 bca.byteSize = theSize;
Reid Spencer649ee572004-06-09 06:16:43 +000065 bca.ModuleId.clear();
Reid Spencer00c28a72004-06-10 08:09:13 +000066 bca.numBlocks = 0;
Reid Spencer649ee572004-06-09 06:16:43 +000067 bca.numTypes = 0;
68 bca.numValues = 0;
69 bca.numFunctions = 0;
70 bca.numConstants = 0;
71 bca.numGlobalVars = 0;
72 bca.numInstructions = 0;
73 bca.numBasicBlocks = 0;
74 bca.numOperands = 0;
75 bca.numCmpctnTables = 0;
76 bca.numSymTab = 0;
77 bca.maxTypeSlot = 0;
78 bca.maxValueSlot = 0;
Reid Spencer00c28a72004-06-10 08:09:13 +000079 bca.numAlignment = 0;
80 bca.fileDensity = 0.0;
81 bca.globalsDensity = 0.0;
82 bca.functionDensity = 0.0;
Reid Spencer1cf50242004-06-11 15:10:38 +000083 bca.instructionSize = 0;
84 bca.longInstructions = 0;
Reid Spencer00c28a72004-06-10 08:09:13 +000085 bca.vbrCount32 = 0;
86 bca.vbrCount64 = 0;
87 bca.vbrCompBytes = 0;
88 bca.vbrExpdBytes = 0;
Reid Spencer649ee572004-06-09 06:16:43 +000089 bca.FunctionInfo.clear();
90 bca.BytecodeDump.clear();
Reid Spencer00c28a72004-06-10 08:09:13 +000091 bca.BlockSizes[BytecodeFormat::Module] = 0;
92 bca.BlockSizes[BytecodeFormat::Function] = 0;
93 bca.BlockSizes[BytecodeFormat::ConstantPool] = 0;
94 bca.BlockSizes[BytecodeFormat::SymbolTable] = 0;
95 bca.BlockSizes[BytecodeFormat::ModuleGlobalInfo] = 0;
96 bca.BlockSizes[BytecodeFormat::GlobalTypePlane] = 0;
97 bca.BlockSizes[BytecodeFormat::BasicBlock] = 0;
98 bca.BlockSizes[BytecodeFormat::InstructionList] = 0;
99 bca.BlockSizes[BytecodeFormat::CompactionTable] = 0;
Reid Spencerdac69c82004-06-07 17:53:43 +0000100 }
101
Reid Spencercbb22e22004-06-10 22:00:54 +0000102 virtual void handleFinish() {
Reid Spencerf41aa732004-06-29 23:23:12 +0000103 dump << "} End Bytecode\n";
104 bca.BytecodeDump = dump.str() ;
105
Reid Spencer00c28a72004-06-10 08:09:13 +0000106 bca.fileDensity = double(bca.byteSize) / double( bca.numTypes + bca.numValues );
107 double globalSize = 0.0;
108 globalSize += double(bca.BlockSizes[BytecodeFormat::ConstantPool]);
109 globalSize += double(bca.BlockSizes[BytecodeFormat::ModuleGlobalInfo]);
110 globalSize += double(bca.BlockSizes[BytecodeFormat::GlobalTypePlane]);
111 bca.globalsDensity = globalSize / double( bca.numTypes + bca.numConstants +
112 bca.numGlobalVars );
113 bca.functionDensity = double(bca.BlockSizes[BytecodeFormat::Function]) /
114 double(bca.numFunctions);
Reid Spencerf41aa732004-06-29 23:23:12 +0000115
116 if ( bca.progressiveVerify ) {
117 try {
Reid Spencerb61cdb72004-07-04 11:00:39 +0000118 verifyModule(*M, ThrowExceptionAction);
Reid Spencerf41aa732004-06-29 23:23:12 +0000119 } catch ( std::string& msg ) {
Reid Spencerb61cdb72004-07-04 11:00:39 +0000120 bca.VerifyInfo += "Verify@Finish: " + msg + "\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000121 }
122 }
Reid Spencerdac69c82004-06-07 17:53:43 +0000123 }
124
Reid Spencercbb22e22004-06-10 22:00:54 +0000125 virtual void handleModuleBegin(const std::string& id) {
Reid Spencerf41aa732004-06-29 23:23:12 +0000126 dump << " Module " << id << " {\n";
Reid Spencer649ee572004-06-09 06:16:43 +0000127 bca.ModuleId = id;
Reid Spencerdac69c82004-06-07 17:53:43 +0000128 }
129
Reid Spencerf41aa732004-06-29 23:23:12 +0000130 virtual void handleModuleEnd(const std::string& id) {
131 dump << " } End Module " << id << "\n";
132 if ( bca.progressiveVerify ) {
133 try {
Reid Spencerb61cdb72004-07-04 11:00:39 +0000134 verifyModule(*M, ThrowExceptionAction);
Reid Spencerf41aa732004-06-29 23:23:12 +0000135 } catch ( std::string& msg ) {
Reid Spencerb61cdb72004-07-04 11:00:39 +0000136 bca.VerifyInfo += "Verify@EndModule: " + msg + "\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000137 }
138 }
139 }
Reid Spencerdac69c82004-06-07 17:53:43 +0000140
Reid Spencercbb22e22004-06-10 22:00:54 +0000141 virtual void handleVersionInfo(
Reid Spencerdac69c82004-06-07 17:53:43 +0000142 unsigned char RevisionNum, ///< Byte code revision number
143 Module::Endianness Endianness, ///< Endianness indicator
144 Module::PointerSize PointerSize ///< PointerSize indicator
Reid Spencerf41aa732004-06-29 23:23:12 +0000145 ) {
146 dump << " RevisionNum: " << int(RevisionNum)
Reid Spencerb61cdb72004-07-04 11:00:39 +0000147 << " Endianness: " << Endianness
148 << " PointerSize: " << PointerSize << "\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000149 }
Reid Spencerdac69c82004-06-07 17:53:43 +0000150
Reid Spencerf41aa732004-06-29 23:23:12 +0000151 virtual void handleModuleGlobalsBegin() {
152 dump << " BLOCK: ModuleGlobalInfo {\n";
153 }
Reid Spencerdac69c82004-06-07 17:53:43 +0000154
Reid Spencercbb22e22004-06-10 22:00:54 +0000155 virtual void handleGlobalVariable(
Reid Spencerf41aa732004-06-29 23:23:12 +0000156 const Type* ElemType,
157 bool isConstant,
158 GlobalValue::LinkageTypes Linkage,
159 unsigned SlotNum,
160 unsigned initSlot
Reid Spencercbb22e22004-06-10 22:00:54 +0000161 ) {
Reid Spencer649ee572004-06-09 06:16:43 +0000162 bca.numGlobalVars++;
Reid Spencer00c28a72004-06-10 08:09:13 +0000163 bca.numValues++;
Reid Spencerf41aa732004-06-29 23:23:12 +0000164
165 dump << " GV: "
166 << ( initSlot == 0 ? "Uni" : "I" ) << "nitialized, "
Reid Spencerb61cdb72004-07-04 11:00:39 +0000167 << ( isConstant? "Constant, " : "Variable, ")
168 << " Linkage=" << Linkage << " Type="
169 << ElemType->getDescription()
170 << " Slot=" << SlotNum << " InitSlot=" << initSlot
171 << "\n";
Reid Spencerdac69c82004-06-07 17:53:43 +0000172 }
173
Reid Spencerf41aa732004-06-29 23:23:12 +0000174 virtual void handleType( const Type* Ty ) {
175 bca.numTypes++;
176 dump << " Type: " << Ty->getDescription() << "\n";
Reid Spencerdac69c82004-06-07 17:53:43 +0000177 }
178
Reid Spencercbb22e22004-06-10 22:00:54 +0000179 virtual void handleFunctionDeclaration(
Reid Spencerb61cdb72004-07-04 11:00:39 +0000180 Function* Func ///< The function
Reid Spencercbb22e22004-06-10 22:00:54 +0000181 ) {
Reid Spencer649ee572004-06-09 06:16:43 +0000182 bca.numFunctions++;
Reid Spencer00c28a72004-06-10 08:09:13 +0000183 bca.numValues++;
Reid Spencerf41aa732004-06-29 23:23:12 +0000184 dump << " Function Decl: " << Func->getType()->getDescription() << "\n";
Reid Spencerdac69c82004-06-07 17:53:43 +0000185 }
186
Reid Spencerf41aa732004-06-29 23:23:12 +0000187 virtual void handleGlobalInitializer(GlobalVariable* GV, Constant* CV) {
Reid Spencer5c15fe52004-07-05 00:57:50 +0000188 dump << " Initializer: GV=";
Reid Spencerf41aa732004-06-29 23:23:12 +0000189 GV->print(dump);
Reid Spencer5c15fe52004-07-05 00:57:50 +0000190 dump << " CV=";
Reid Spencerf41aa732004-06-29 23:23:12 +0000191 CV->print(dump);
192 dump << "\n";
193 }
Reid Spencerdac69c82004-06-07 17:53:43 +0000194
Reid Spencerf41aa732004-06-29 23:23:12 +0000195 virtual void handleModuleGlobalsEnd() {
196 dump << " } END BLOCK: ModuleGlobalInfo\n";
197 if ( bca.progressiveVerify ) {
198 try {
Reid Spencerb61cdb72004-07-04 11:00:39 +0000199 verifyModule(*M, ThrowExceptionAction);
Reid Spencerf41aa732004-06-29 23:23:12 +0000200 } catch ( std::string& msg ) {
Reid Spencerb61cdb72004-07-04 11:00:39 +0000201 bca.VerifyInfo += "Verify@EndModuleGlobalInfo: " + msg + "\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000202 }
203 }
204 }
205
206 virtual void handleCompactionTableBegin() {
Reid Spencer5c15fe52004-07-05 00:57:50 +0000207 dump << " BLOCK: CompactionTable {\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000208 }
Reid Spencerdac69c82004-06-07 17:53:43 +0000209
Reid Spencercbb22e22004-06-10 22:00:54 +0000210 virtual void handleCompactionTablePlane( unsigned Ty, unsigned NumEntries) {
Reid Spencer649ee572004-06-09 06:16:43 +0000211 bca.numCmpctnTables++;
Reid Spencer5c15fe52004-07-05 00:57:50 +0000212 dump << " Plane: Ty=" << Ty << " Size=" << NumEntries << "\n";
Reid Spencerdac69c82004-06-07 17:53:43 +0000213 }
214
Reid Spencercbb22e22004-06-10 22:00:54 +0000215 virtual void handleCompactionTableType( unsigned i, unsigned TypSlot,
Reid Spencerf41aa732004-06-29 23:23:12 +0000216 const Type* Ty ) {
Reid Spencer5c15fe52004-07-05 00:57:50 +0000217 dump << " Type: " << i << " Slot:" << TypSlot
Reid Spencerb61cdb72004-07-04 11:00:39 +0000218 << " is " << Ty->getDescription() << "\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000219 }
Reid Spencerdac69c82004-06-07 17:53:43 +0000220
Chris Lattner2c6c14d2004-08-04 00:19:23 +0000221 virtual void handleCompactionTableValue(unsigned i, unsigned TypSlot,
222 unsigned ValSlot) {
Reid Spencer5c15fe52004-07-05 00:57:50 +0000223 dump << " Value: " << i << " TypSlot: " << TypSlot
Chris Lattner2c6c14d2004-08-04 00:19:23 +0000224 << " ValSlot:" << ValSlot << "\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000225 }
Reid Spencercbb22e22004-06-10 22:00:54 +0000226
Reid Spencerf41aa732004-06-29 23:23:12 +0000227 virtual void handleCompactionTableEnd() {
Reid Spencer5c15fe52004-07-05 00:57:50 +0000228 dump << " } END BLOCK: CompactionTable\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000229 }
Reid Spencercbb22e22004-06-10 22:00:54 +0000230
Reid Spencerf41aa732004-06-29 23:23:12 +0000231 virtual void handleSymbolTableBegin(Function* CF, SymbolTable* ST) {
232 bca.numSymTab++;
233 dump << " BLOCK: SymbolTable {\n";
234 }
Reid Spencercbb22e22004-06-10 22:00:54 +0000235
Reid Spencerf41aa732004-06-29 23:23:12 +0000236 virtual void handleSymbolTablePlane(unsigned Ty, unsigned NumEntries,
237 const Type* Typ) {
238 dump << " Plane: Ty=" << Ty << " Size=" << NumEntries
Reid Spencerb61cdb72004-07-04 11:00:39 +0000239 << " Type: " << Typ->getDescription() << "\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000240 }
Reid Spencercbb22e22004-06-10 22:00:54 +0000241
Reid Spencerf41aa732004-06-29 23:23:12 +0000242 virtual void handleSymbolTableType(unsigned i, unsigned slot,
243 const std::string& name ) {
244 dump << " Type " << i << " Slot=" << slot
Reid Spencerb61cdb72004-07-04 11:00:39 +0000245 << " Name: " << name << "\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000246 }
Reid Spencercbb22e22004-06-10 22:00:54 +0000247
Reid Spencerf41aa732004-06-29 23:23:12 +0000248 virtual void handleSymbolTableValue(unsigned i, unsigned slot,
249 const std::string& name ) {
250 dump << " Value " << i << " Slot=" << slot
Reid Spencerb61cdb72004-07-04 11:00:39 +0000251 << " Name: " << name << "\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000252 }
Reid Spencercbb22e22004-06-10 22:00:54 +0000253
Reid Spencerf41aa732004-06-29 23:23:12 +0000254 virtual void handleSymbolTableEnd() {
255 dump << " } END BLOCK: SymbolTable\n";
256 }
Reid Spencercbb22e22004-06-10 22:00:54 +0000257
Reid Spencerf41aa732004-06-29 23:23:12 +0000258 virtual void handleFunctionBegin(Function* Func, unsigned Size) {
Reid Spencer5c15fe52004-07-05 00:57:50 +0000259 dump << " BLOCK: Function {\n";
260 dump << " Linkage: " << Func->getLinkage() << "\n";
261 dump << " Type: " << Func->getType()->getDescription() << "\n";
Reid Spencercbb22e22004-06-10 22:00:54 +0000262 const FunctionType* FType =
263 cast<FunctionType>(Func->getType()->getElementType());
264 currFunc = &bca.FunctionInfo[Func];
265 currFunc->description = FType->getDescription();
266 currFunc->name = Func->getName();
267 currFunc->byteSize = Size;
268 currFunc->numInstructions = 0;
269 currFunc->numBasicBlocks = 0;
270 currFunc->numPhis = 0;
271 currFunc->numOperands = 0;
272 currFunc->density = 0.0;
Reid Spencer1cf50242004-06-11 15:10:38 +0000273 currFunc->instructionSize = 0;
274 currFunc->longInstructions = 0;
Reid Spencercbb22e22004-06-10 22:00:54 +0000275 currFunc->vbrCount32 = 0;
276 currFunc->vbrCount64 = 0;
277 currFunc->vbrCompBytes = 0;
278 currFunc->vbrExpdBytes = 0;
Reid Spencerf41aa732004-06-29 23:23:12 +0000279
Reid Spencerdac69c82004-06-07 17:53:43 +0000280 }
281
Reid Spencercbb22e22004-06-10 22:00:54 +0000282 virtual void handleFunctionEnd( Function* Func) {
Reid Spencer5c15fe52004-07-05 00:57:50 +0000283 dump << " } END BLOCK: Function\n";
Reid Spencercbb22e22004-06-10 22:00:54 +0000284 currFunc->density = double(currFunc->byteSize) /
285 double(currFunc->numInstructions+currFunc->numBasicBlocks);
Reid Spencerf41aa732004-06-29 23:23:12 +0000286
287 if ( bca.progressiveVerify ) {
288 try {
Reid Spencerb61cdb72004-07-04 11:00:39 +0000289 verifyModule(*M, ThrowExceptionAction);
Reid Spencerf41aa732004-06-29 23:23:12 +0000290 } catch ( std::string& msg ) {
Reid Spencerb61cdb72004-07-04 11:00:39 +0000291 bca.VerifyInfo += "Verify@EndFunction: " + msg + "\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000292 }
293 }
Reid Spencerdac69c82004-06-07 17:53:43 +0000294 }
295
Reid Spencercbb22e22004-06-10 22:00:54 +0000296 virtual void handleBasicBlockBegin( unsigned blocknum) {
Reid Spencer5c15fe52004-07-05 00:57:50 +0000297 dump << " BLOCK: BasicBlock #" << blocknum << "{\n";
Reid Spencer649ee572004-06-09 06:16:43 +0000298 bca.numBasicBlocks++;
Reid Spencer00c28a72004-06-10 08:09:13 +0000299 bca.numValues++;
Reid Spencercbb22e22004-06-10 22:00:54 +0000300 if ( currFunc ) currFunc->numBasicBlocks++;
Reid Spencerdac69c82004-06-07 17:53:43 +0000301 }
302
Reid Spencercbb22e22004-06-10 22:00:54 +0000303 virtual bool handleInstruction( unsigned Opcode, const Type* iType,
Reid Spencerb61cdb72004-07-04 11:00:39 +0000304 std::vector<unsigned>& Operands, unsigned Size){
Reid Spencer5c15fe52004-07-05 00:57:50 +0000305 dump << " INST: OpCode="
306 << Instruction::getOpcodeName(Opcode) << " Type=\""
307 << iType->getDescription() << "\"";
Reid Spencerf41aa732004-06-29 23:23:12 +0000308 for ( unsigned i = 0; i < Operands.size(); ++i )
Reid Spencer5c15fe52004-07-05 00:57:50 +0000309 dump << " Op(" << i << ")=Slot(" << Operands[i] << ")";
310 dump << "\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000311
Reid Spencer649ee572004-06-09 06:16:43 +0000312 bca.numInstructions++;
Reid Spencer00c28a72004-06-10 08:09:13 +0000313 bca.numValues++;
Reid Spencer1cf50242004-06-11 15:10:38 +0000314 bca.instructionSize += Size;
315 if (Size > 4 ) bca.longInstructions++;
Reid Spencer00c28a72004-06-10 08:09:13 +0000316 bca.numOperands += Operands.size();
Reid Spencercbb22e22004-06-10 22:00:54 +0000317 if ( currFunc ) {
318 currFunc->numInstructions++;
Reid Spencer1cf50242004-06-11 15:10:38 +0000319 currFunc->instructionSize += Size;
320 if (Size > 4 ) currFunc->longInstructions++;
Reid Spencer8a9a3702004-06-11 03:06:43 +0000321 if ( Opcode == Instruction::PHI ) currFunc->numPhis++;
Reid Spencercbb22e22004-06-10 22:00:54 +0000322 }
Reid Spencer649ee572004-06-09 06:16:43 +0000323 return Instruction::isTerminator(Opcode);
Reid Spencerdac69c82004-06-07 17:53:43 +0000324 }
325
Reid Spencerf41aa732004-06-29 23:23:12 +0000326 virtual void handleBasicBlockEnd(unsigned blocknum) {
Reid Spencer5c15fe52004-07-05 00:57:50 +0000327 dump << " } END BLOCK: BasicBlock #" << blocknum << "{\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000328 }
Reid Spencerdac69c82004-06-07 17:53:43 +0000329
Reid Spencerf41aa732004-06-29 23:23:12 +0000330 virtual void handleGlobalConstantsBegin() {
331 dump << " BLOCK: GlobalConstants {\n";
332 }
Reid Spencerdac69c82004-06-07 17:53:43 +0000333
Reid Spencerf41aa732004-06-29 23:23:12 +0000334 virtual void handleConstantExpression( unsigned Opcode,
335 std::vector<Constant*> ArgVec, Constant* C ) {
336 dump << " EXPR: " << Instruction::getOpcodeName(Opcode) << "\n";
337 for ( unsigned i = 0; i < ArgVec.size(); ++i ) {
338 dump << " Arg#" << i << " "; ArgVec[i]->print(dump); dump << "\n";
339 }
340 dump << " Value=";
341 C->print(dump);
342 dump << "\n";
Reid Spencer649ee572004-06-09 06:16:43 +0000343 bca.numConstants++;
Reid Spencer00c28a72004-06-10 08:09:13 +0000344 bca.numValues++;
Reid Spencerdac69c82004-06-07 17:53:43 +0000345 }
346
Reid Spencercbb22e22004-06-10 22:00:54 +0000347 virtual void handleConstantValue( Constant * c ) {
Reid Spencerf41aa732004-06-29 23:23:12 +0000348 dump << " VALUE: ";
349 c->print(dump);
350 dump << "\n";
Reid Spencer649ee572004-06-09 06:16:43 +0000351 bca.numConstants++;
Reid Spencer00c28a72004-06-10 08:09:13 +0000352 bca.numValues++;
Reid Spencerdac69c82004-06-07 17:53:43 +0000353 }
354
Reid Spencercbb22e22004-06-10 22:00:54 +0000355 virtual void handleConstantArray( const ArrayType* AT,
Reid Spencerf41aa732004-06-29 23:23:12 +0000356 std::vector<Constant*>& Elements,
Reid Spencerb61cdb72004-07-04 11:00:39 +0000357 unsigned TypeSlot,
358 Constant* ArrayVal ) {
Reid Spencerf41aa732004-06-29 23:23:12 +0000359 dump << " ARRAY: " << AT->getDescription()
360 << " TypeSlot=" << TypeSlot << "\n";
361 for ( unsigned i = 0; i < Elements.size(); ++i ) {
362 dump << " #" << i;
363 Elements[i]->print(dump);
364 dump << "\n";
365 }
366 dump << " Value=";
367 ArrayVal->print(dump);
368 dump << "\n";
369
Reid Spencer649ee572004-06-09 06:16:43 +0000370 bca.numConstants++;
Reid Spencer00c28a72004-06-10 08:09:13 +0000371 bca.numValues++;
Reid Spencerdac69c82004-06-07 17:53:43 +0000372 }
373
Reid Spencercbb22e22004-06-10 22:00:54 +0000374 virtual void handleConstantStruct(
Reid Spencer00c28a72004-06-10 08:09:13 +0000375 const StructType* ST,
Reid Spencerf41aa732004-06-29 23:23:12 +0000376 std::vector<Constant*>& Elements,
Reid Spencerb61cdb72004-07-04 11:00:39 +0000377 Constant* StructVal)
Reid Spencerdac69c82004-06-07 17:53:43 +0000378 {
Reid Spencerf41aa732004-06-29 23:23:12 +0000379 dump << " STRUC: " << ST->getDescription() << "\n";
380 for ( unsigned i = 0; i < Elements.size(); ++i ) {
381 dump << " #" << i << " "; Elements[i]->print(dump); dump << "\n";
382 }
383 dump << " Value=";
384 StructVal->print(dump);
385 dump << "\n";
Reid Spencer649ee572004-06-09 06:16:43 +0000386 bca.numConstants++;
Reid Spencer00c28a72004-06-10 08:09:13 +0000387 bca.numValues++;
Reid Spencerdac69c82004-06-07 17:53:43 +0000388 }
389
Reid Spencerf41aa732004-06-29 23:23:12 +0000390 virtual void handleConstantPointer( const PointerType* PT,
Reid Spencer3c90f9f2004-07-18 00:10:36 +0000391 unsigned Slot, GlobalValue* GV ) {
Reid Spencerf41aa732004-06-29 23:23:12 +0000392 dump << " PNTR: " << PT->getDescription()
Reid Spencerb61cdb72004-07-04 11:00:39 +0000393 << " Slot=" << Slot << " GlobalValue=";
Reid Spencerf41aa732004-06-29 23:23:12 +0000394 GV->print(dump);
Reid Spencerf41aa732004-06-29 23:23:12 +0000395 dump << "\n";
Reid Spencer649ee572004-06-09 06:16:43 +0000396 bca.numConstants++;
Reid Spencer00c28a72004-06-10 08:09:13 +0000397 bca.numValues++;
Reid Spencerdac69c82004-06-07 17:53:43 +0000398 }
399
Reid Spencercbb22e22004-06-10 22:00:54 +0000400 virtual void handleConstantString( const ConstantArray* CA ) {
Reid Spencerf41aa732004-06-29 23:23:12 +0000401 dump << " STRNG: ";
402 CA->print(dump);
403 dump << "\n";
Reid Spencer649ee572004-06-09 06:16:43 +0000404 bca.numConstants++;
Reid Spencer00c28a72004-06-10 08:09:13 +0000405 bca.numValues++;
Reid Spencerdac69c82004-06-07 17:53:43 +0000406 }
407
Reid Spencerf41aa732004-06-29 23:23:12 +0000408 virtual void handleGlobalConstantsEnd() {
409 dump << " } END BLOCK: GlobalConstants\n";
410 if ( bca.progressiveVerify ) {
411 try {
Reid Spencerb61cdb72004-07-04 11:00:39 +0000412 verifyModule(*M, ThrowExceptionAction);
Reid Spencerf41aa732004-06-29 23:23:12 +0000413 } catch ( std::string& msg ) {
Reid Spencerb61cdb72004-07-04 11:00:39 +0000414 bca.VerifyInfo += "Verify@EndGlobalConstants: " + msg + "\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000415 }
416 }
417 }
Reid Spencerdac69c82004-06-07 17:53:43 +0000418
Reid Spencercbb22e22004-06-10 22:00:54 +0000419 virtual void handleAlignment(unsigned numBytes) {
Reid Spencer00c28a72004-06-10 08:09:13 +0000420 bca.numAlignment += numBytes;
Reid Spencerdac69c82004-06-07 17:53:43 +0000421 }
422
Reid Spencercbb22e22004-06-10 22:00:54 +0000423 virtual void handleBlock(
Reid Spencer00c28a72004-06-10 08:09:13 +0000424 unsigned BType, const unsigned char* StartPtr, unsigned Size) {
425 bca.numBlocks++;
426 bca.BlockSizes[llvm::BytecodeFormat::FileBlockIDs(BType)] += Size;
427 }
428
429 virtual void handleVBR32(unsigned Size ) {
430 bca.vbrCount32++;
431 bca.vbrCompBytes += Size;
432 bca.vbrExpdBytes += sizeof(uint32_t);
Reid Spencercbb22e22004-06-10 22:00:54 +0000433 if (currFunc) {
434 currFunc->vbrCount32++;
435 currFunc->vbrCompBytes += Size;
436 currFunc->vbrExpdBytes += sizeof(uint32_t);
437 }
Reid Spencer00c28a72004-06-10 08:09:13 +0000438 }
Reid Spencercbb22e22004-06-10 22:00:54 +0000439
Reid Spencer00c28a72004-06-10 08:09:13 +0000440 virtual void handleVBR64(unsigned Size ) {
441 bca.vbrCount64++;
442 bca.vbrCompBytes += Size;
443 bca.vbrExpdBytes += sizeof(uint64_t);
Reid Spencercbb22e22004-06-10 22:00:54 +0000444 if ( currFunc ) {
445 currFunc->vbrCount64++;
446 currFunc->vbrCompBytes += Size;
447 currFunc->vbrExpdBytes += sizeof(uint64_t);
448 }
Reid Spencer00c28a72004-06-10 08:09:13 +0000449 }
Reid Spencerdac69c82004-06-07 17:53:43 +0000450};
451
Reid Spencerf41aa732004-06-29 23:23:12 +0000452
453/// @brief Utility for printing a titled unsigned value with
454/// an aligned colon.
455inline static void print(std::ostream& Out, const char*title,
456 unsigned val, bool nl = true ) {
457 Out << std::setw(30) << std::right << title
458 << std::setw(0) << ": "
459 << std::setw(9) << val << "\n";
Reid Spencerdac69c82004-06-07 17:53:43 +0000460}
461
Reid Spencerf41aa732004-06-29 23:23:12 +0000462/// @brief Utility for printing a titled double value with an
463/// aligned colon
464inline static void print(std::ostream&Out, const char*title,
465 double val ) {
466 Out << std::setw(30) << std::right << title
467 << std::setw(0) << ": "
468 << std::setw(9) << std::setprecision(6) << val << "\n" ;
469}
470
471/// @brief Utility for printing a titled double value with a
472/// percentage and aligned colon.
473inline static void print(std::ostream&Out, const char*title,
474 double top, double bot ) {
475 Out << std::setw(30) << std::right << title
476 << std::setw(0) << ": "
477 << std::setw(9) << std::setprecision(6) << top
478 << " (" << std::left << std::setw(0) << std::setprecision(4)
479 << (top/bot)*100.0 << "%)\n";
480}
481
482/// @brief Utility for printing a titled string value with
483/// an aligned colon.
484inline static void print(std::ostream&Out, const char*title,
485 std::string val, bool nl = true) {
486 Out << std::setw(30) << std::right << title
487 << std::setw(0) << ": "
488 << std::left << val << (nl ? "\n" : "");
489}
490
491}
492
493namespace llvm {
494
495/// This function prints the contents of rhe BytecodeAnalysis structure in
496/// a human legible form.
497/// @brief Print BytecodeAnalysis structure to an ostream
498void PrintBytecodeAnalysis(BytecodeAnalysis& bca, std::ostream& Out )
Reid Spencerdac69c82004-06-07 17:53:43 +0000499{
Reid Spencerf41aa732004-06-29 23:23:12 +0000500 print(Out, "Bytecode Analysis Of Module", bca.ModuleId);
501 print(Out, "File Size", bca.byteSize);
Reid Spencerf41aa732004-06-29 23:23:12 +0000502 print(Out, "Number Of Bytecode Blocks", bca.numBlocks);
503 print(Out, "Number Of Types", bca.numTypes);
504 print(Out, "Number Of Values", bca.numValues);
505 print(Out, "Number Of Constants", bca.numConstants);
506 print(Out, "Number Of Global Variables", bca.numGlobalVars);
507 print(Out, "Number Of Functions", bca.numFunctions);
508 print(Out, "Number Of Basic Blocks", bca.numBasicBlocks);
509 print(Out, "Number Of Instructions", bca.numInstructions);
510 print(Out, "Number Of Operands", bca.numOperands);
511 print(Out, "Number Of Compaction Tables", bca.numCmpctnTables);
512 print(Out, "Number Of Symbol Tables", bca.numSymTab);
513 print(Out, "Long Instructions", bca.longInstructions);
514 print(Out, "Instruction Size", bca.instructionSize);
515 print(Out, "Average Instruction Size",
516 double(bca.instructionSize)/double(bca.numInstructions));
517 print(Out, "Maximum Type Slot Number", bca.maxTypeSlot);
518 print(Out, "Maximum Value Slot Number", bca.maxValueSlot);
519 print(Out, "Bytes Thrown To Alignment", double(bca.numAlignment),
520 double(bca.byteSize));
521 print(Out, "File Density (bytes/def)", bca.fileDensity);
522 print(Out, "Globals Density (bytes/def)", bca.globalsDensity);
523 print(Out, "Function Density (bytes/func)", bca.functionDensity);
524 print(Out, "Number of VBR 32-bit Integers", bca.vbrCount32);
525 print(Out, "Number of VBR 64-bit Integers", bca.vbrCount64);
526 print(Out, "Number of VBR Compressed Bytes", bca.vbrCompBytes);
527 print(Out, "Number of VBR Expanded Bytes", bca.vbrExpdBytes);
528 print(Out, "VBR Savings",
529 double(bca.vbrExpdBytes)-double(bca.vbrCompBytes),
Reid Spencer9cb52422004-07-10 08:04:13 +0000530 double(bca.vbrExpdBytes));
Reid Spencerf41aa732004-06-29 23:23:12 +0000531
532 if ( bca.detailedResults ) {
533 print(Out, "Module Bytes",
534 double(bca.BlockSizes[BytecodeFormat::Module]),
535 double(bca.byteSize));
536 print(Out, "Function Bytes",
537 double(bca.BlockSizes[BytecodeFormat::Function]),
538 double(bca.byteSize));
539 print(Out, "Constant Pool Bytes",
540 double(bca.BlockSizes[BytecodeFormat::ConstantPool]),
541 double(bca.byteSize));
542 print(Out, "Symbol Table Bytes",
543 double(bca.BlockSizes[BytecodeFormat::SymbolTable]),
544 double(bca.byteSize));
545 print(Out, "Module Global Info Bytes",
546 double(bca.BlockSizes[BytecodeFormat::ModuleGlobalInfo]),
547 double(bca.byteSize));
548 print(Out, "Global Type Plane Bytes",
549 double(bca.BlockSizes[BytecodeFormat::GlobalTypePlane]),
550 double(bca.byteSize));
551 print(Out, "Basic Block Bytes",
552 double(bca.BlockSizes[BytecodeFormat::BasicBlock]),
553 double(bca.byteSize));
554 print(Out, "Instruction List Bytes",
555 double(bca.BlockSizes[BytecodeFormat::InstructionList]),
556 double(bca.byteSize));
557 print(Out, "Compaction Table Bytes",
558 double(bca.BlockSizes[BytecodeFormat::CompactionTable]),
559 double(bca.byteSize));
560
561 std::map<const Function*,BytecodeAnalysis::BytecodeFunctionInfo>::iterator I =
562 bca.FunctionInfo.begin();
563 std::map<const Function*,BytecodeAnalysis::BytecodeFunctionInfo>::iterator E =
564 bca.FunctionInfo.end();
565
566 while ( I != E ) {
567 Out << std::left << std::setw(0);
568 Out << "Function: " << I->second.name << "\n";
569 print(Out, "Type:", I->second.description);
570 print(Out, "Byte Size", I->second.byteSize);
571 print(Out, "Instructions", I->second.numInstructions);
572 print(Out, "Long Instructions", I->second.longInstructions);
573 print(Out, "Instruction Size", I->second.instructionSize);
574 print(Out, "Average Instruction Size",
575 double(I->second.instructionSize)/double(I->second.numInstructions));
576 print(Out, "Basic Blocks", I->second.numBasicBlocks);
577 print(Out, "Operand", I->second.numOperands);
578 print(Out, "Function Density", I->second.density);
579 print(Out, "Number of VBR 32-bit Integers", I->second.vbrCount32);
580 print(Out, "Number of VBR 64-bit Integers", I->second.vbrCount64);
581 print(Out, "Number of VBR Compressed Bytes", I->second.vbrCompBytes);
582 print(Out, "Number of VBR Expanded Bytes", I->second.vbrExpdBytes);
583 print(Out, "VBR Savings",
584 double(I->second.vbrExpdBytes)-double(I->second.vbrCompBytes),
Reid Spencer9cb52422004-07-10 08:04:13 +0000585 double(I->second.vbrExpdBytes));
Reid Spencerf41aa732004-06-29 23:23:12 +0000586 ++I;
587 }
588 }
589
590 if ( bca.dumpBytecode )
591 Out << bca.BytecodeDump;
592
593 if ( bca.progressiveVerify )
594 Out << bca.VerifyInfo;
595}
596
597BytecodeHandler* createBytecodeAnalyzerHandler(BytecodeAnalysis& bca)
598{
599 return new AnalyzerHandler(bca);
600}
601
Reid Spencerdac69c82004-06-07 17:53:43 +0000602}
603
604// vim: sw=2