blob: 5738f1a733d85d9835b0180d5247fe18ca1ec10a [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"
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>
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
Reid Spencer911ec6d2004-08-21 20:58:19 +000036 std::ostream* os; ///< A convenience for osing data.
Reid Spencerf41aa732004-06-29 23:23:12 +000037 /// @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 Spencer911ec6d2004-08-21 20:58:19 +000047 AnalyzerHandler(BytecodeAnalysis& TheBca, std::ostream* output)
Reid Spencercbb22e22004-06-10 22:00:54 +000048 : bca(TheBca)
Reid Spencer911ec6d2004-08-21 20:58:19 +000049 , os(output)
Reid Spencercbb22e22004-06-10 22:00:54 +000050 , currFunc(0)
Reid Spencer911ec6d2004-08-21 20:58:19 +000051 { }
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 ) {
Reid Spencer911ec6d2004-08-21 20:58:19 +000058 if (os)
59 *os << "ERROR: " << str << "\n";
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;
Reid Spencer911ec6d2004-08-21 20:58:19 +000064 if (os)
65 *os << "Bytecode {\n";
Reid Spencerf41aa732004-06-29 23:23:12 +000066 bca.byteSize = theSize;
Reid Spencer649ee572004-06-09 06:16:43 +000067 bca.ModuleId.clear();
Reid Spencer00c28a72004-06-10 08:09:13 +000068 bca.numBlocks = 0;
Reid Spencer649ee572004-06-09 06:16:43 +000069 bca.numTypes = 0;
70 bca.numValues = 0;
71 bca.numFunctions = 0;
72 bca.numConstants = 0;
73 bca.numGlobalVars = 0;
74 bca.numInstructions = 0;
75 bca.numBasicBlocks = 0;
76 bca.numOperands = 0;
77 bca.numCmpctnTables = 0;
78 bca.numSymTab = 0;
Reid Spencer911ec6d2004-08-21 20:58:19 +000079 bca.numLibraries = 0;
80 bca.libSize = 0;
Reid Spencer649ee572004-06-09 06:16:43 +000081 bca.maxTypeSlot = 0;
82 bca.maxValueSlot = 0;
Reid Spencer00c28a72004-06-10 08:09:13 +000083 bca.numAlignment = 0;
84 bca.fileDensity = 0.0;
85 bca.globalsDensity = 0.0;
86 bca.functionDensity = 0.0;
Reid Spencer1cf50242004-06-11 15:10:38 +000087 bca.instructionSize = 0;
88 bca.longInstructions = 0;
Reid Spencer00c28a72004-06-10 08:09:13 +000089 bca.vbrCount32 = 0;
90 bca.vbrCount64 = 0;
91 bca.vbrCompBytes = 0;
92 bca.vbrExpdBytes = 0;
Reid Spencer649ee572004-06-09 06:16:43 +000093 bca.FunctionInfo.clear();
Reid Spencer911ec6d2004-08-21 20:58:19 +000094 bca.BlockSizes[BytecodeFormat::Reserved_DoNotUse] = 0;
95 bca.BlockSizes[BytecodeFormat::ModuleBlockID] = theSize;
96 bca.BlockSizes[BytecodeFormat::FunctionBlockID] = 0;
97 bca.BlockSizes[BytecodeFormat::ConstantPoolBlockID] = 0;
98 bca.BlockSizes[BytecodeFormat::SymbolTableBlockID] = 0;
99 bca.BlockSizes[BytecodeFormat::ModuleGlobalInfoBlockID] = 0;
100 bca.BlockSizes[BytecodeFormat::GlobalTypePlaneBlockID] = 0;
101 bca.BlockSizes[BytecodeFormat::InstructionListBlockID] = 0;
102 bca.BlockSizes[BytecodeFormat::CompactionTableBlockID] = 0;
Reid Spencerdac69c82004-06-07 17:53:43 +0000103 }
104
Reid Spencercbb22e22004-06-10 22:00:54 +0000105 virtual void handleFinish() {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000106 if (os)
107 *os << "} End Bytecode\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000108
Reid Spencer00c28a72004-06-10 08:09:13 +0000109 bca.fileDensity = double(bca.byteSize) / double( bca.numTypes + bca.numValues );
110 double globalSize = 0.0;
Reid Spencer911ec6d2004-08-21 20:58:19 +0000111 globalSize += double(bca.BlockSizes[BytecodeFormat::ConstantPoolBlockID]);
112 globalSize += double(bca.BlockSizes[BytecodeFormat::ModuleGlobalInfoBlockID]);
113 globalSize += double(bca.BlockSizes[BytecodeFormat::GlobalTypePlaneBlockID]);
Reid Spencer00c28a72004-06-10 08:09:13 +0000114 bca.globalsDensity = globalSize / double( bca.numTypes + bca.numConstants +
115 bca.numGlobalVars );
Reid Spencer911ec6d2004-08-21 20:58:19 +0000116 bca.functionDensity = double(bca.BlockSizes[BytecodeFormat::FunctionBlockID]) /
Reid Spencer00c28a72004-06-10 08:09:13 +0000117 double(bca.numFunctions);
Reid Spencerf41aa732004-06-29 23:23:12 +0000118
119 if ( bca.progressiveVerify ) {
120 try {
Reid Spencerb61cdb72004-07-04 11:00:39 +0000121 verifyModule(*M, ThrowExceptionAction);
Reid Spencerf41aa732004-06-29 23:23:12 +0000122 } catch ( std::string& msg ) {
Reid Spencerb61cdb72004-07-04 11:00:39 +0000123 bca.VerifyInfo += "Verify@Finish: " + msg + "\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000124 }
125 }
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
Reid Spencerf41aa732004-06-29 23:23:12 +0000134 virtual void handleModuleEnd(const std::string& id) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000135 if (os)
136 *os << " } End Module " << id << "\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000137 if ( bca.progressiveVerify ) {
138 try {
Reid Spencerb61cdb72004-07-04 11:00:39 +0000139 verifyModule(*M, ThrowExceptionAction);
Reid Spencerf41aa732004-06-29 23:23:12 +0000140 } catch ( std::string& msg ) {
Reid Spencerb61cdb72004-07-04 11:00:39 +0000141 bca.VerifyInfo += "Verify@EndModule: " + msg + "\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000142 }
143 }
144 }
Reid Spencerdac69c82004-06-07 17:53:43 +0000145
Reid Spencercbb22e22004-06-10 22:00:54 +0000146 virtual void handleVersionInfo(
Reid Spencerdac69c82004-06-07 17:53:43 +0000147 unsigned char RevisionNum, ///< Byte code revision number
148 Module::Endianness Endianness, ///< Endianness indicator
149 Module::PointerSize PointerSize ///< PointerSize indicator
Reid Spencerf41aa732004-06-29 23:23:12 +0000150 ) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000151 if (os)
152 *os << " RevisionNum: " << int(RevisionNum)
Reid Spencerb61cdb72004-07-04 11:00:39 +0000153 << " Endianness: " << Endianness
154 << " PointerSize: " << PointerSize << "\n";
Reid Spencer911ec6d2004-08-21 20:58:19 +0000155 bca.version = RevisionNum;
Reid Spencerf41aa732004-06-29 23:23:12 +0000156 }
Reid Spencerdac69c82004-06-07 17:53:43 +0000157
Reid Spencerf41aa732004-06-29 23:23:12 +0000158 virtual void handleModuleGlobalsBegin() {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000159 if (os)
160 *os << " BLOCK: ModuleGlobalInfo {\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000161 }
Reid Spencerdac69c82004-06-07 17:53:43 +0000162
Reid Spencercbb22e22004-06-10 22:00:54 +0000163 virtual void handleGlobalVariable(
Reid Spencerf41aa732004-06-29 23:23:12 +0000164 const Type* ElemType,
165 bool isConstant,
166 GlobalValue::LinkageTypes Linkage,
167 unsigned SlotNum,
168 unsigned initSlot
Reid Spencercbb22e22004-06-10 22:00:54 +0000169 ) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000170 if (os) {
171 *os << " GV: "
172 << ( initSlot == 0 ? "Uni" : "I" ) << "nitialized, "
173 << ( isConstant? "Constant, " : "Variable, ")
174 << " Linkage=" << Linkage << " Type=";
175 WriteTypeSymbolic(*os, ElemType, M);
176 *os << " Slot=" << SlotNum << " InitSlot=" << initSlot
177 << "\n";
178 }
179
Reid Spencer649ee572004-06-09 06:16:43 +0000180 bca.numGlobalVars++;
Reid Spencer00c28a72004-06-10 08:09:13 +0000181 bca.numValues++;
Reid Spencer911ec6d2004-08-21 20:58:19 +0000182 if (SlotNum > bca.maxValueSlot)
183 bca.maxValueSlot = SlotNum;
184 if (initSlot > bca.maxValueSlot)
185 bca.maxValueSlot = initSlot;
Reid Spencerf41aa732004-06-29 23:23:12 +0000186
Reid Spencer911ec6d2004-08-21 20:58:19 +0000187 }
188
189 virtual void handleTypeList(unsigned numEntries) {
190 bca.maxTypeSlot = numEntries - 1;
Reid Spencerdac69c82004-06-07 17:53:43 +0000191 }
192
Reid Spencerf41aa732004-06-29 23:23:12 +0000193 virtual void handleType( const Type* Ty ) {
194 bca.numTypes++;
Reid Spencer911ec6d2004-08-21 20:58:19 +0000195 if (os) {
196 *os << " Type: ";
197 WriteTypeSymbolic(*os,Ty,M);
198 *os << "\n";
199 }
Reid Spencerdac69c82004-06-07 17:53:43 +0000200 }
201
Reid Spencercbb22e22004-06-10 22:00:54 +0000202 virtual void handleFunctionDeclaration(
Reid Spencerb61cdb72004-07-04 11:00:39 +0000203 Function* Func ///< The function
Reid Spencercbb22e22004-06-10 22:00:54 +0000204 ) {
Reid Spencer649ee572004-06-09 06:16:43 +0000205 bca.numFunctions++;
Reid Spencer00c28a72004-06-10 08:09:13 +0000206 bca.numValues++;
Reid Spencer911ec6d2004-08-21 20:58:19 +0000207 if (os) {
208 *os << " Function Decl: ";
209 WriteTypeSymbolic(*os,Func->getType(),M);
210 *os << "\n";
211 }
Reid Spencerdac69c82004-06-07 17:53:43 +0000212 }
213
Reid Spencerf41aa732004-06-29 23:23:12 +0000214 virtual void handleGlobalInitializer(GlobalVariable* GV, Constant* CV) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000215 if (os) {
216 *os << " Initializer: GV=";
217 GV->print(*os);
218 *os << " CV=";
219 CV->print(*os);
220 *os << "\n";
221 }
222 }
223
224 virtual void handleDependentLibrary(const std::string& libName) {
225 bca.numLibraries++;
226 bca.libSize += libName.size() + (libName.size() < 128 ? 1 : 2);
Reid Spencer3ee8eed2004-09-11 04:14:07 +0000227 if (os)
228 *os << " Library: '" << libName << "'\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000229 }
Reid Spencerdac69c82004-06-07 17:53:43 +0000230
Reid Spencerf41aa732004-06-29 23:23:12 +0000231 virtual void handleModuleGlobalsEnd() {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000232 if (os)
233 *os << " } END BLOCK: ModuleGlobalInfo\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000234 if ( bca.progressiveVerify ) {
235 try {
Reid Spencerb61cdb72004-07-04 11:00:39 +0000236 verifyModule(*M, ThrowExceptionAction);
Reid Spencerf41aa732004-06-29 23:23:12 +0000237 } catch ( std::string& msg ) {
Reid Spencerb61cdb72004-07-04 11:00:39 +0000238 bca.VerifyInfo += "Verify@EndModuleGlobalInfo: " + msg + "\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000239 }
240 }
241 }
242
243 virtual void handleCompactionTableBegin() {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000244 if (os)
245 *os << " BLOCK: CompactionTable {\n";
Reid Spencer488d73a2004-08-27 00:43:51 +0000246 bca.numCmpctnTables++;
Reid Spencerf41aa732004-06-29 23:23:12 +0000247 }
Reid Spencerdac69c82004-06-07 17:53:43 +0000248
Reid Spencercbb22e22004-06-10 22:00:54 +0000249 virtual void handleCompactionTablePlane( unsigned Ty, unsigned NumEntries) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000250 if (os)
251 *os << " Plane: Ty=" << Ty << " Size=" << NumEntries << "\n";
Reid Spencerdac69c82004-06-07 17:53:43 +0000252 }
253
Reid Spencercbb22e22004-06-10 22:00:54 +0000254 virtual void handleCompactionTableType( unsigned i, unsigned TypSlot,
Reid Spencerf41aa732004-06-29 23:23:12 +0000255 const Type* Ty ) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000256 if (os) {
257 *os << " Type: " << i << " Slot:" << TypSlot << " is ";
258 WriteTypeSymbolic(*os,Ty,M);
259 *os << "\n";
260 }
Reid Spencerf41aa732004-06-29 23:23:12 +0000261 }
Reid Spencerdac69c82004-06-07 17:53:43 +0000262
Chris Lattner2c6c14d2004-08-04 00:19:23 +0000263 virtual void handleCompactionTableValue(unsigned i, unsigned TypSlot,
264 unsigned ValSlot) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000265 if (os)
266 *os << " Value: " << i << " TypSlot: " << TypSlot
Chris Lattner2c6c14d2004-08-04 00:19:23 +0000267 << " ValSlot:" << ValSlot << "\n";
Reid Spencer911ec6d2004-08-21 20:58:19 +0000268 if (ValSlot > bca.maxValueSlot)
269 bca.maxValueSlot = ValSlot;
Reid Spencerf41aa732004-06-29 23:23:12 +0000270 }
Reid Spencercbb22e22004-06-10 22:00:54 +0000271
Reid Spencerf41aa732004-06-29 23:23:12 +0000272 virtual void handleCompactionTableEnd() {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000273 if (os)
274 *os << " } END BLOCK: CompactionTable\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000275 }
Reid Spencercbb22e22004-06-10 22:00:54 +0000276
Reid Spencerf41aa732004-06-29 23:23:12 +0000277 virtual void handleSymbolTableBegin(Function* CF, SymbolTable* ST) {
278 bca.numSymTab++;
Reid Spencer911ec6d2004-08-21 20:58:19 +0000279 if (os)
280 *os << " BLOCK: SymbolTable {\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000281 }
Reid Spencercbb22e22004-06-10 22:00:54 +0000282
Reid Spencerf41aa732004-06-29 23:23:12 +0000283 virtual void handleSymbolTablePlane(unsigned Ty, unsigned NumEntries,
284 const Type* Typ) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000285 if (os) {
286 *os << " Plane: Ty=" << Ty << " Size=" << NumEntries << " Type: ";
287 WriteTypeSymbolic(*os,Typ,M);
288 *os << "\n";
289 }
Reid Spencerf41aa732004-06-29 23:23:12 +0000290 }
Reid Spencercbb22e22004-06-10 22:00:54 +0000291
Reid Spencer911ec6d2004-08-21 20:58:19 +0000292 virtual void handleSymbolTableType(unsigned i, unsigned TypSlot,
Reid Spencerf41aa732004-06-29 23:23:12 +0000293 const std::string& name ) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000294 if (os)
295 *os << " Type " << i << " Slot=" << TypSlot
296 << " Name: " << name << "\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000297 }
Reid Spencercbb22e22004-06-10 22:00:54 +0000298
Reid Spencer911ec6d2004-08-21 20:58:19 +0000299 virtual void handleSymbolTableValue(unsigned i, unsigned ValSlot,
Reid Spencerf41aa732004-06-29 23:23:12 +0000300 const std::string& name ) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000301 if (os)
302 *os << " Value " << i << " Slot=" << ValSlot
303 << " Name: " << name << "\n";
304 if (ValSlot > bca.maxValueSlot)
305 bca.maxValueSlot = ValSlot;
Reid Spencerf41aa732004-06-29 23:23:12 +0000306 }
Reid Spencercbb22e22004-06-10 22:00:54 +0000307
Reid Spencerf41aa732004-06-29 23:23:12 +0000308 virtual void handleSymbolTableEnd() {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000309 if (os)
310 *os << " } END BLOCK: SymbolTable\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000311 }
Reid Spencercbb22e22004-06-10 22:00:54 +0000312
Reid Spencerf41aa732004-06-29 23:23:12 +0000313 virtual void handleFunctionBegin(Function* Func, unsigned Size) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000314 if (os) {
315 *os << " BLOCK: Function {\n"
316 << " Linkage: " << Func->getLinkage() << "\n"
317 << " Type: ";
318 WriteTypeSymbolic(*os,Func->getType(),M);
319 *os << "\n";
320 }
321
Reid Spencercbb22e22004-06-10 22:00:54 +0000322 currFunc = &bca.FunctionInfo[Func];
Reid Spencer911ec6d2004-08-21 20:58:19 +0000323 std::ostringstream tmp;
324 WriteTypeSymbolic(tmp,Func->getType(),M);
325 currFunc->description = tmp.str();
Reid Spencercbb22e22004-06-10 22:00:54 +0000326 currFunc->name = Func->getName();
327 currFunc->byteSize = Size;
328 currFunc->numInstructions = 0;
329 currFunc->numBasicBlocks = 0;
330 currFunc->numPhis = 0;
331 currFunc->numOperands = 0;
332 currFunc->density = 0.0;
Reid Spencer1cf50242004-06-11 15:10:38 +0000333 currFunc->instructionSize = 0;
334 currFunc->longInstructions = 0;
Reid Spencercbb22e22004-06-10 22:00:54 +0000335 currFunc->vbrCount32 = 0;
336 currFunc->vbrCount64 = 0;
337 currFunc->vbrCompBytes = 0;
338 currFunc->vbrExpdBytes = 0;
Reid Spencerf41aa732004-06-29 23:23:12 +0000339
Reid Spencerdac69c82004-06-07 17:53:43 +0000340 }
341
Reid Spencercbb22e22004-06-10 22:00:54 +0000342 virtual void handleFunctionEnd( Function* Func) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000343 if (os)
344 *os << " } END BLOCK: Function\n";
Reid Spencercbb22e22004-06-10 22:00:54 +0000345 currFunc->density = double(currFunc->byteSize) /
Reid Spencer3120e712004-08-24 22:45:32 +0000346 double(currFunc->numInstructions);
Reid Spencerf41aa732004-06-29 23:23:12 +0000347
348 if ( bca.progressiveVerify ) {
349 try {
Reid Spencerb61cdb72004-07-04 11:00:39 +0000350 verifyModule(*M, ThrowExceptionAction);
Reid Spencerf41aa732004-06-29 23:23:12 +0000351 } catch ( std::string& msg ) {
Reid Spencerb61cdb72004-07-04 11:00:39 +0000352 bca.VerifyInfo += "Verify@EndFunction: " + msg + "\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000353 }
354 }
Reid Spencerdac69c82004-06-07 17:53:43 +0000355 }
356
Reid Spencercbb22e22004-06-10 22:00:54 +0000357 virtual void handleBasicBlockBegin( unsigned blocknum) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000358 if (os)
359 *os << " BLOCK: BasicBlock #" << blocknum << "{\n";
Reid Spencer649ee572004-06-09 06:16:43 +0000360 bca.numBasicBlocks++;
Reid Spencer00c28a72004-06-10 08:09:13 +0000361 bca.numValues++;
Reid Spencercbb22e22004-06-10 22:00:54 +0000362 if ( currFunc ) currFunc->numBasicBlocks++;
Reid Spencerdac69c82004-06-07 17:53:43 +0000363 }
364
Reid Spencercbb22e22004-06-10 22:00:54 +0000365 virtual bool handleInstruction( unsigned Opcode, const Type* iType,
Reid Spencerb61cdb72004-07-04 11:00:39 +0000366 std::vector<unsigned>& Operands, unsigned Size){
Reid Spencer911ec6d2004-08-21 20:58:19 +0000367 if (os) {
368 *os << " INST: OpCode="
369 << Instruction::getOpcodeName(Opcode) << " Type=\"";
370 WriteTypeSymbolic(*os,iType,M);
371 *os << "\"";
372 for ( unsigned i = 0; i < Operands.size(); ++i )
373 *os << " Op(" << i << ")=Slot(" << Operands[i] << ")";
374 *os << "\n";
375 }
Reid Spencerf41aa732004-06-29 23:23:12 +0000376
Reid Spencer649ee572004-06-09 06:16:43 +0000377 bca.numInstructions++;
Reid Spencer00c28a72004-06-10 08:09:13 +0000378 bca.numValues++;
Reid Spencer1cf50242004-06-11 15:10:38 +0000379 bca.instructionSize += Size;
380 if (Size > 4 ) bca.longInstructions++;
Reid Spencer00c28a72004-06-10 08:09:13 +0000381 bca.numOperands += Operands.size();
Reid Spencer911ec6d2004-08-21 20:58:19 +0000382 for (unsigned i = 0; i < Operands.size(); ++i )
383 if (Operands[i] > bca.maxValueSlot)
384 bca.maxValueSlot = Operands[i];
Reid Spencercbb22e22004-06-10 22:00:54 +0000385 if ( currFunc ) {
386 currFunc->numInstructions++;
Reid Spencer1cf50242004-06-11 15:10:38 +0000387 currFunc->instructionSize += Size;
388 if (Size > 4 ) currFunc->longInstructions++;
Reid Spencer8a9a3702004-06-11 03:06:43 +0000389 if ( Opcode == Instruction::PHI ) currFunc->numPhis++;
Reid Spencercbb22e22004-06-10 22:00:54 +0000390 }
Reid Spencer649ee572004-06-09 06:16:43 +0000391 return Instruction::isTerminator(Opcode);
Reid Spencerdac69c82004-06-07 17:53:43 +0000392 }
393
Reid Spencerf41aa732004-06-29 23:23:12 +0000394 virtual void handleBasicBlockEnd(unsigned blocknum) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000395 if (os)
396 *os << " } END BLOCK: BasicBlock #" << blocknum << "{\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000397 }
Reid Spencerdac69c82004-06-07 17:53:43 +0000398
Reid Spencerf41aa732004-06-29 23:23:12 +0000399 virtual void handleGlobalConstantsBegin() {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000400 if (os)
401 *os << " BLOCK: GlobalConstants {\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000402 }
Reid Spencerdac69c82004-06-07 17:53:43 +0000403
Reid Spencerf41aa732004-06-29 23:23:12 +0000404 virtual void handleConstantExpression( unsigned Opcode,
405 std::vector<Constant*> ArgVec, Constant* C ) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000406 if (os) {
407 *os << " EXPR: " << Instruction::getOpcodeName(Opcode) << "\n";
408 for ( unsigned i = 0; i < ArgVec.size(); ++i ) {
409 *os << " Arg#" << i << " "; ArgVec[i]->print(*os);
410 *os << "\n";
411 }
412 *os << " Value=";
413 C->print(*os);
414 *os << "\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000415 }
Reid Spencer649ee572004-06-09 06:16:43 +0000416 bca.numConstants++;
Reid Spencer00c28a72004-06-10 08:09:13 +0000417 bca.numValues++;
Reid Spencerdac69c82004-06-07 17:53:43 +0000418 }
419
Reid Spencercbb22e22004-06-10 22:00:54 +0000420 virtual void handleConstantValue( Constant * c ) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000421 if (os) {
422 *os << " VALUE: ";
423 c->print(*os);
424 *os << "\n";
425 }
Reid Spencer649ee572004-06-09 06:16:43 +0000426 bca.numConstants++;
Reid Spencer00c28a72004-06-10 08:09:13 +0000427 bca.numValues++;
Reid Spencerdac69c82004-06-07 17:53:43 +0000428 }
429
Reid Spencercbb22e22004-06-10 22:00:54 +0000430 virtual void handleConstantArray( const ArrayType* AT,
Reid Spencerf41aa732004-06-29 23:23:12 +0000431 std::vector<Constant*>& Elements,
Reid Spencerb61cdb72004-07-04 11:00:39 +0000432 unsigned TypeSlot,
433 Constant* ArrayVal ) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000434 if (os) {
435 *os << " ARRAY: ";
436 WriteTypeSymbolic(*os,AT,M);
437 *os << " TypeSlot=" << TypeSlot << "\n";
438 for ( unsigned i = 0; i < Elements.size(); ++i ) {
439 *os << " #" << i;
440 Elements[i]->print(*os);
441 *os << "\n";
442 }
443 *os << " Value=";
444 ArrayVal->print(*os);
445 *os << "\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000446 }
Reid Spencerf41aa732004-06-29 23:23:12 +0000447
Reid Spencer649ee572004-06-09 06:16:43 +0000448 bca.numConstants++;
Reid Spencer00c28a72004-06-10 08:09:13 +0000449 bca.numValues++;
Reid Spencerdac69c82004-06-07 17:53:43 +0000450 }
451
Reid Spencercbb22e22004-06-10 22:00:54 +0000452 virtual void handleConstantStruct(
Reid Spencer00c28a72004-06-10 08:09:13 +0000453 const StructType* ST,
Reid Spencerf41aa732004-06-29 23:23:12 +0000454 std::vector<Constant*>& Elements,
Reid Spencerb61cdb72004-07-04 11:00:39 +0000455 Constant* StructVal)
Reid Spencerdac69c82004-06-07 17:53:43 +0000456 {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000457 if (os) {
458 *os << " STRUC: ";
459 WriteTypeSymbolic(*os,ST,M);
460 *os << "\n";
461 for ( unsigned i = 0; i < Elements.size(); ++i ) {
462 *os << " #" << i << " "; Elements[i]->print(*os);
463 *os << "\n";
464 }
465 *os << " Value=";
466 StructVal->print(*os);
467 *os << "\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000468 }
Reid Spencer649ee572004-06-09 06:16:43 +0000469 bca.numConstants++;
Reid Spencer00c28a72004-06-10 08:09:13 +0000470 bca.numValues++;
Reid Spencerdac69c82004-06-07 17:53:43 +0000471 }
472
Brian Gaeke715c90b2004-08-20 06:00:58 +0000473 virtual void handleConstantPacked(
474 const PackedType* PT,
475 std::vector<Constant*>& Elements,
476 unsigned TypeSlot,
477 Constant* PackedVal)
478 {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000479 if (os) {
480 *os << " PACKD: ";
481 WriteTypeSymbolic(*os,PT,M);
482 *os << " TypeSlot=" << TypeSlot << "\n";
483 for ( unsigned i = 0; i < Elements.size(); ++i ) {
484 *os << " #" << i;
485 Elements[i]->print(*os);
486 *os << "\n";
487 }
488 *os << " Value=";
489 PackedVal->print(*os);
490 *os << "\n";
Brian Gaeke715c90b2004-08-20 06:00:58 +0000491 }
Brian Gaeke715c90b2004-08-20 06:00:58 +0000492
493 bca.numConstants++;
494 bca.numValues++;
495 }
496
Reid Spencerf41aa732004-06-29 23:23:12 +0000497 virtual void handleConstantPointer( const PointerType* PT,
Reid Spencer3c90f9f2004-07-18 00:10:36 +0000498 unsigned Slot, GlobalValue* GV ) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000499 if (os) {
500 *os << " PNTR: ";
501 WriteTypeSymbolic(*os,PT,M);
502 *os << " Slot=" << Slot << " GlobalValue=";
503 GV->print(*os);
504 *os << "\n";
505 }
Reid Spencer649ee572004-06-09 06:16:43 +0000506 bca.numConstants++;
Reid Spencer00c28a72004-06-10 08:09:13 +0000507 bca.numValues++;
Reid Spencerdac69c82004-06-07 17:53:43 +0000508 }
509
Reid Spencercbb22e22004-06-10 22:00:54 +0000510 virtual void handleConstantString( const ConstantArray* CA ) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000511 if (os) {
512 *os << " STRNG: ";
513 CA->print(*os);
514 *os << "\n";
515 }
Reid Spencer649ee572004-06-09 06:16:43 +0000516 bca.numConstants++;
Reid Spencer00c28a72004-06-10 08:09:13 +0000517 bca.numValues++;
Reid Spencerdac69c82004-06-07 17:53:43 +0000518 }
519
Reid Spencerf41aa732004-06-29 23:23:12 +0000520 virtual void handleGlobalConstantsEnd() {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000521 if (os)
522 *os << " } END BLOCK: GlobalConstants\n";
523
Reid Spencerf41aa732004-06-29 23:23:12 +0000524 if ( bca.progressiveVerify ) {
525 try {
Reid Spencerb61cdb72004-07-04 11:00:39 +0000526 verifyModule(*M, ThrowExceptionAction);
Reid Spencerf41aa732004-06-29 23:23:12 +0000527 } catch ( std::string& msg ) {
Reid Spencerb61cdb72004-07-04 11:00:39 +0000528 bca.VerifyInfo += "Verify@EndGlobalConstants: " + msg + "\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000529 }
530 }
531 }
Reid Spencerdac69c82004-06-07 17:53:43 +0000532
Reid Spencercbb22e22004-06-10 22:00:54 +0000533 virtual void handleAlignment(unsigned numBytes) {
Reid Spencer00c28a72004-06-10 08:09:13 +0000534 bca.numAlignment += numBytes;
Reid Spencerdac69c82004-06-07 17:53:43 +0000535 }
536
Reid Spencercbb22e22004-06-10 22:00:54 +0000537 virtual void handleBlock(
Reid Spencer00c28a72004-06-10 08:09:13 +0000538 unsigned BType, const unsigned char* StartPtr, unsigned Size) {
539 bca.numBlocks++;
Reid Spencer911ec6d2004-08-21 20:58:19 +0000540 assert(BType >= BytecodeFormat::ModuleBlockID);
541 assert(BType < BytecodeFormat::NumberOfBlockIDs);
542 bca.BlockSizes[
543 llvm::BytecodeFormat::CompressedBytecodeBlockIdentifiers(BType)] += Size;
544
545 if (bca.version < 3) // Check for long block headers versions
546 bca.BlockSizes[llvm::BytecodeFormat::Reserved_DoNotUse] += 8;
547 else
548 bca.BlockSizes[llvm::BytecodeFormat::Reserved_DoNotUse] += 4;
Reid Spencer00c28a72004-06-10 08:09:13 +0000549 }
550
551 virtual void handleVBR32(unsigned Size ) {
552 bca.vbrCount32++;
553 bca.vbrCompBytes += Size;
554 bca.vbrExpdBytes += sizeof(uint32_t);
Reid Spencercbb22e22004-06-10 22:00:54 +0000555 if (currFunc) {
556 currFunc->vbrCount32++;
557 currFunc->vbrCompBytes += Size;
558 currFunc->vbrExpdBytes += sizeof(uint32_t);
559 }
Reid Spencer00c28a72004-06-10 08:09:13 +0000560 }
Reid Spencercbb22e22004-06-10 22:00:54 +0000561
Reid Spencer00c28a72004-06-10 08:09:13 +0000562 virtual void handleVBR64(unsigned Size ) {
563 bca.vbrCount64++;
564 bca.vbrCompBytes += Size;
565 bca.vbrExpdBytes += sizeof(uint64_t);
Reid Spencercbb22e22004-06-10 22:00:54 +0000566 if ( currFunc ) {
567 currFunc->vbrCount64++;
568 currFunc->vbrCompBytes += Size;
569 currFunc->vbrExpdBytes += sizeof(uint64_t);
570 }
Reid Spencer00c28a72004-06-10 08:09:13 +0000571 }
Reid Spencerdac69c82004-06-07 17:53:43 +0000572};
573
Reid Spencerf41aa732004-06-29 23:23:12 +0000574
575/// @brief Utility for printing a titled unsigned value with
576/// an aligned colon.
577inline static void print(std::ostream& Out, const char*title,
578 unsigned val, bool nl = true ) {
579 Out << std::setw(30) << std::right << title
580 << std::setw(0) << ": "
581 << std::setw(9) << val << "\n";
Reid Spencerdac69c82004-06-07 17:53:43 +0000582}
583
Reid Spencerf41aa732004-06-29 23:23:12 +0000584/// @brief Utility for printing a titled double value with an
585/// aligned colon
586inline static void print(std::ostream&Out, const char*title,
587 double val ) {
588 Out << std::setw(30) << std::right << title
589 << std::setw(0) << ": "
590 << std::setw(9) << std::setprecision(6) << val << "\n" ;
591}
592
593/// @brief Utility for printing a titled double value with a
594/// percentage and aligned colon.
595inline static void print(std::ostream&Out, const char*title,
596 double top, double bot ) {
597 Out << std::setw(30) << std::right << title
598 << std::setw(0) << ": "
599 << std::setw(9) << std::setprecision(6) << top
600 << " (" << std::left << std::setw(0) << std::setprecision(4)
601 << (top/bot)*100.0 << "%)\n";
602}
603
604/// @brief Utility for printing a titled string value with
605/// an aligned colon.
606inline static void print(std::ostream&Out, const char*title,
607 std::string val, bool nl = true) {
608 Out << std::setw(30) << std::right << title
609 << std::setw(0) << ": "
610 << std::left << val << (nl ? "\n" : "");
611}
612
613}
614
615namespace llvm {
616
617/// This function prints the contents of rhe BytecodeAnalysis structure in
618/// a human legible form.
619/// @brief Print BytecodeAnalysis structure to an ostream
620void PrintBytecodeAnalysis(BytecodeAnalysis& bca, std::ostream& Out )
Reid Spencerdac69c82004-06-07 17:53:43 +0000621{
Reid Spencer911ec6d2004-08-21 20:58:19 +0000622 Out << "\nSummary Analysis Of " << bca.ModuleId << ": \n\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000623 print(Out, "Bytecode Analysis Of Module", bca.ModuleId);
Reid Spencer911ec6d2004-08-21 20:58:19 +0000624 print(Out, "Bytecode Version Number", bca.version);
Reid Spencerf41aa732004-06-29 23:23:12 +0000625 print(Out, "File Size", bca.byteSize);
Reid Spencer911ec6d2004-08-21 20:58:19 +0000626 print(Out, "Module Bytes",
627 double(bca.BlockSizes[BytecodeFormat::ModuleBlockID]),
628 double(bca.byteSize));
629 print(Out, "Function Bytes",
630 double(bca.BlockSizes[BytecodeFormat::FunctionBlockID]),
631 double(bca.byteSize));
632 print(Out, "Global Types Bytes",
633 double(bca.BlockSizes[BytecodeFormat::GlobalTypePlaneBlockID]),
634 double(bca.byteSize));
635 print(Out, "Constant Pool Bytes",
636 double(bca.BlockSizes[BytecodeFormat::ConstantPoolBlockID]),
637 double(bca.byteSize));
638 print(Out, "Module Globals Bytes",
639 double(bca.BlockSizes[BytecodeFormat::ModuleGlobalInfoBlockID]),
640 double(bca.byteSize));
641 print(Out, "Instruction List Bytes",
642 double(bca.BlockSizes[BytecodeFormat::InstructionListBlockID]),
643 double(bca.byteSize));
644 print(Out, "Compaction Table Bytes",
645 double(bca.BlockSizes[BytecodeFormat::CompactionTableBlockID]),
646 double(bca.byteSize));
647 print(Out, "Symbol Table Bytes",
648 double(bca.BlockSizes[BytecodeFormat::SymbolTableBlockID]),
649 double(bca.byteSize));
650 print(Out, "Alignment Bytes",
651 double(bca.numAlignment), double(bca.byteSize));
652 print(Out, "Block Header Bytes",
653 double(bca.BlockSizes[BytecodeFormat::Reserved_DoNotUse]),
654 double(bca.byteSize));
655 print(Out, "Dependent Libraries Bytes", double(bca.libSize),
656 double(bca.byteSize));
Reid Spencerf41aa732004-06-29 23:23:12 +0000657 print(Out, "Number Of Bytecode Blocks", bca.numBlocks);
Reid Spencer911ec6d2004-08-21 20:58:19 +0000658 print(Out, "Number Of Functions", bca.numFunctions);
Reid Spencerf41aa732004-06-29 23:23:12 +0000659 print(Out, "Number Of Types", bca.numTypes);
Reid Spencerf41aa732004-06-29 23:23:12 +0000660 print(Out, "Number Of Constants", bca.numConstants);
661 print(Out, "Number Of Global Variables", bca.numGlobalVars);
Reid Spencer911ec6d2004-08-21 20:58:19 +0000662 print(Out, "Number Of Values", bca.numValues);
Reid Spencerf41aa732004-06-29 23:23:12 +0000663 print(Out, "Number Of Basic Blocks", bca.numBasicBlocks);
664 print(Out, "Number Of Instructions", bca.numInstructions);
Reid Spencer911ec6d2004-08-21 20:58:19 +0000665 print(Out, "Number Of Long Instructions", bca.longInstructions);
Reid Spencerf41aa732004-06-29 23:23:12 +0000666 print(Out, "Number Of Operands", bca.numOperands);
667 print(Out, "Number Of Compaction Tables", bca.numCmpctnTables);
668 print(Out, "Number Of Symbol Tables", bca.numSymTab);
Reid Spencer911ec6d2004-08-21 20:58:19 +0000669 print(Out, "Number Of Dependent Libs", bca.numLibraries);
670 print(Out, "Total Instruction Size", bca.instructionSize);
Reid Spencerf41aa732004-06-29 23:23:12 +0000671 print(Out, "Average Instruction Size",
Reid Spencer911ec6d2004-08-21 20:58:19 +0000672 double(bca.instructionSize)/double(bca.numInstructions));
673
Reid Spencerf41aa732004-06-29 23:23:12 +0000674 print(Out, "Maximum Type Slot Number", bca.maxTypeSlot);
675 print(Out, "Maximum Value Slot Number", bca.maxValueSlot);
Reid Spencer3120e712004-08-24 22:45:32 +0000676 print(Out, "Bytes Per Value ", bca.fileDensity);
677 print(Out, "Bytes Per Global", bca.globalsDensity);
678 print(Out, "Bytes Per Function", bca.functionDensity);
679 print(Out, "# of VBR 32-bit Integers", bca.vbrCount32);
680 print(Out, "# of VBR 64-bit Integers", bca.vbrCount64);
681 print(Out, "# of VBR Compressed Bytes", bca.vbrCompBytes);
682 print(Out, "# of VBR Expanded Bytes", bca.vbrExpdBytes);
683 print(Out, "Bytes Saved With VBR",
Reid Spencer911ec6d2004-08-21 20:58:19 +0000684 double(bca.vbrExpdBytes)-double(bca.vbrCompBytes),
685 double(bca.vbrExpdBytes));
Reid Spencerf41aa732004-06-29 23:23:12 +0000686
Reid Spencer911ec6d2004-08-21 20:58:19 +0000687 if (bca.detailedResults) {
688 Out << "\nDetailed Analysis Of " << bca.ModuleId << " Functions:\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000689
690 std::map<const Function*,BytecodeAnalysis::BytecodeFunctionInfo>::iterator I =
691 bca.FunctionInfo.begin();
692 std::map<const Function*,BytecodeAnalysis::BytecodeFunctionInfo>::iterator E =
693 bca.FunctionInfo.end();
694
695 while ( I != E ) {
Chris Lattner4a8167f2004-10-15 19:40:31 +0000696 Out << std::left << std::setw(0) << "\n";
697 if (I->second.numBasicBlocks == 0) Out << "External ";
698 Out << "Function: " << I->second.name << "\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000699 print(Out, "Type:", I->second.description);
700 print(Out, "Byte Size", I->second.byteSize);
Chris Lattner4a8167f2004-10-15 19:40:31 +0000701 if (I->second.numBasicBlocks) {
702 print(Out, "Basic Blocks", I->second.numBasicBlocks);
703 print(Out, "Instructions", I->second.numInstructions);
704 print(Out, "Long Instructions", I->second.longInstructions);
705 print(Out, "Operands", I->second.numOperands);
706 print(Out, "Instruction Size", I->second.instructionSize);
707 print(Out, "Average Instruction Size",
708 double(I->second.instructionSize) / I->second.numInstructions);
709 print(Out, "Bytes Per Instruction", I->second.density);
710 print(Out, "# of VBR 32-bit Integers", I->second.vbrCount32);
711 print(Out, "# of VBR 64-bit Integers", I->second.vbrCount64);
712 print(Out, "# of VBR Compressed Bytes", I->second.vbrCompBytes);
713 print(Out, "# of VBR Expanded Bytes", I->second.vbrExpdBytes);
714 print(Out, "Bytes Saved With VBR",
715 double(I->second.vbrExpdBytes) - I->second.vbrCompBytes),
716 double(I->second.vbrExpdBytes);
717 }
Reid Spencerf41aa732004-06-29 23:23:12 +0000718 ++I;
719 }
720 }
721
Reid Spencerf41aa732004-06-29 23:23:12 +0000722 if ( bca.progressiveVerify )
723 Out << bca.VerifyInfo;
724}
725
Reid Spencer911ec6d2004-08-21 20:58:19 +0000726BytecodeHandler* createBytecodeAnalyzerHandler(BytecodeAnalysis& bca,
727 std::ostream* output)
Reid Spencerf41aa732004-06-29 23:23:12 +0000728{
Reid Spencer911ec6d2004-08-21 20:58:19 +0000729 return new AnalyzerHandler(bca,output);
Reid Spencerf41aa732004-06-29 23:23:12 +0000730}
731
Reid Spencerdac69c82004-06-07 17:53:43 +0000732}
733
734// vim: sw=2