blob: c86db26a920c7cb9ed6157f69310e967ca6181e6 [file] [log] [blame]
Reid Spencerf41aa732004-06-29 23:23:12 +00001//===-- Analyzer.cpp - Analysis and Dumping of Bytecode 000000---*- C++ -*-===//
Misha Brukman8a96c532005-04-21 21:44:41 +00002//
Reid Spencerdac69c82004-06-07 17:53:43 +00003// The LLVM Compiler Infrastructure
4//
Misha Brukman8a96c532005-04-21 21:44:41 +00005// This file was developed by Reid Spencer and is distributed under the
Reid Spencerdac69c82004-06-07 17:53:43 +00006// University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukman8a96c532005-04-21 21:44:41 +00007//
Reid Spencerdac69c82004-06-07 17:53:43 +00008//===----------------------------------------------------------------------===//
9//
Reid Spencerf41aa732004-06-29 23:23:12 +000010// This file implements the AnalyzerHandler class and PrintBytecodeAnalysis
11// function which together comprise the basic functionality of the llmv-abcd
12// tool. The AnalyzerHandler collects information about the bytecode file into
13// the BytecodeAnalysis structure. The PrintBytecodeAnalysis function prints
14// out the content of that structure.
15// @see include/llvm/Bytecode/Analysis.h
Reid Spencerdac69c82004-06-07 17:53:43 +000016//
17//===----------------------------------------------------------------------===//
18
Reid Spencerf41aa732004-06-29 23:23:12 +000019#include "Reader.h"
20#include "llvm/Constants.h"
21#include "llvm/DerivedTypes.h"
22#include "llvm/Module.h"
23#include "llvm/Analysis/Verifier.h"
Reid Spencerf41aa732004-06-29 23:23:12 +000024#include "llvm/Bytecode/BytecodeHandler.h"
Reid Spencer911ec6d2004-08-21 20:58:19 +000025#include "llvm/Assembly/Writer.h"
Reid Spencerf41aa732004-06-29 23:23:12 +000026#include <iomanip>
27#include <sstream>
Duraid Madina0f7bfba2005-12-26 14:23:22 +000028#include <ios>
Reid Spencerdac69c82004-06-07 17:53:43 +000029
30using namespace llvm;
31
Reid Spencerdac69c82004-06-07 17:53:43 +000032namespace {
33
Reid Spencerf41aa732004-06-29 23:23:12 +000034/// @brief Bytecode reading handler for analyzing bytecode.
Reid Spencerdac69c82004-06-07 17:53:43 +000035class AnalyzerHandler : public BytecodeHandler {
Reid Spencerf41aa732004-06-29 23:23:12 +000036 BytecodeAnalysis& bca; ///< The structure in which data is recorded
Reid Spencer911ec6d2004-08-21 20:58:19 +000037 std::ostream* os; ///< A convenience for osing data.
Reid Spencerf41aa732004-06-29 23:23:12 +000038 /// @brief Keeps track of current function
Misha Brukman8a96c532005-04-21 21:44:41 +000039 BytecodeAnalysis::BytecodeFunctionInfo* currFunc;
Reid Spencerf41aa732004-06-29 23:23:12 +000040 Module* M; ///< Keeps track of current module
41
42/// @name Constructor
43/// @{
Reid Spencerdac69c82004-06-07 17:53:43 +000044public:
Reid Spencerf41aa732004-06-29 23:23:12 +000045 /// The only way to construct an AnalyzerHandler. All that is needed is a
46 /// reference to the BytecodeAnalysis structure where the output will be
47 /// placed.
Misha Brukman8a96c532005-04-21 21:44:41 +000048 AnalyzerHandler(BytecodeAnalysis& TheBca, std::ostream* output)
49 : bca(TheBca)
Reid Spencer911ec6d2004-08-21 20:58:19 +000050 , os(output)
Reid Spencercbb22e22004-06-10 22:00:54 +000051 , currFunc(0)
Reid Spencer911ec6d2004-08-21 20:58:19 +000052 { }
Reid Spencer649ee572004-06-09 06:16:43 +000053
Reid Spencerf41aa732004-06-29 23:23:12 +000054/// @}
55/// @name BytecodeHandler Implementations
56/// @{
57public:
Misha Brukman8a96c532005-04-21 21:44:41 +000058 virtual void handleError(const std::string& str ) {
Reid Spencer911ec6d2004-08-21 20:58:19 +000059 if (os)
60 *os << "ERROR: " << str << "\n";
Reid Spencerdac69c82004-06-07 17:53:43 +000061 }
62
Reid Spencerf41aa732004-06-29 23:23:12 +000063 virtual void handleStart( Module* Mod, unsigned theSize ) {
64 M = Mod;
Reid Spencer911ec6d2004-08-21 20:58:19 +000065 if (os)
66 *os << "Bytecode {\n";
Reid Spencerf41aa732004-06-29 23:23:12 +000067 bca.byteSize = theSize;
Reid Spencer649ee572004-06-09 06:16:43 +000068 bca.ModuleId.clear();
Reid Spencer00c28a72004-06-10 08:09:13 +000069 bca.numBlocks = 0;
Reid Spencer649ee572004-06-09 06:16:43 +000070 bca.numTypes = 0;
71 bca.numValues = 0;
72 bca.numFunctions = 0;
73 bca.numConstants = 0;
74 bca.numGlobalVars = 0;
75 bca.numInstructions = 0;
76 bca.numBasicBlocks = 0;
77 bca.numOperands = 0;
78 bca.numCmpctnTables = 0;
79 bca.numSymTab = 0;
Reid Spencer911ec6d2004-08-21 20:58:19 +000080 bca.numLibraries = 0;
81 bca.libSize = 0;
Reid Spencer649ee572004-06-09 06:16:43 +000082 bca.maxTypeSlot = 0;
83 bca.maxValueSlot = 0;
Reid Spencer00c28a72004-06-10 08:09:13 +000084 bca.numAlignment = 0;
85 bca.fileDensity = 0.0;
86 bca.globalsDensity = 0.0;
87 bca.functionDensity = 0.0;
Reid Spencer1cf50242004-06-11 15:10:38 +000088 bca.instructionSize = 0;
89 bca.longInstructions = 0;
Reid Spencer00c28a72004-06-10 08:09:13 +000090 bca.vbrCount32 = 0;
91 bca.vbrCount64 = 0;
92 bca.vbrCompBytes = 0;
93 bca.vbrExpdBytes = 0;
Reid Spencer649ee572004-06-09 06:16:43 +000094 bca.FunctionInfo.clear();
Reid Spencer911ec6d2004-08-21 20:58:19 +000095 bca.BlockSizes[BytecodeFormat::Reserved_DoNotUse] = 0;
96 bca.BlockSizes[BytecodeFormat::ModuleBlockID] = theSize;
97 bca.BlockSizes[BytecodeFormat::FunctionBlockID] = 0;
98 bca.BlockSizes[BytecodeFormat::ConstantPoolBlockID] = 0;
Reid Spencer78d033e2007-01-06 07:24:44 +000099 bca.BlockSizes[BytecodeFormat::ValueSymbolTableBlockID] = 0;
Reid Spencer911ec6d2004-08-21 20:58:19 +0000100 bca.BlockSizes[BytecodeFormat::ModuleGlobalInfoBlockID] = 0;
101 bca.BlockSizes[BytecodeFormat::GlobalTypePlaneBlockID] = 0;
102 bca.BlockSizes[BytecodeFormat::InstructionListBlockID] = 0;
103 bca.BlockSizes[BytecodeFormat::CompactionTableBlockID] = 0;
Reid Spencer78d033e2007-01-06 07:24:44 +0000104 bca.BlockSizes[BytecodeFormat::TypeSymbolTableBlockID] = 0;
Reid Spencerdac69c82004-06-07 17:53:43 +0000105 }
106
Reid Spencercbb22e22004-06-10 22:00:54 +0000107 virtual void handleFinish() {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000108 if (os)
Misha Brukman8a96c532005-04-21 21:44:41 +0000109 *os << "} End Bytecode\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000110
Reid Spencer00c28a72004-06-10 08:09:13 +0000111 bca.fileDensity = double(bca.byteSize) / double( bca.numTypes + bca.numValues );
112 double globalSize = 0.0;
Reid Spencer911ec6d2004-08-21 20:58:19 +0000113 globalSize += double(bca.BlockSizes[BytecodeFormat::ConstantPoolBlockID]);
114 globalSize += double(bca.BlockSizes[BytecodeFormat::ModuleGlobalInfoBlockID]);
115 globalSize += double(bca.BlockSizes[BytecodeFormat::GlobalTypePlaneBlockID]);
Misha Brukman8a96c532005-04-21 21:44:41 +0000116 bca.globalsDensity = globalSize / double( bca.numTypes + bca.numConstants +
Reid Spencer00c28a72004-06-10 08:09:13 +0000117 bca.numGlobalVars );
Misha Brukman8a96c532005-04-21 21:44:41 +0000118 bca.functionDensity = double(bca.BlockSizes[BytecodeFormat::FunctionBlockID]) /
Reid Spencer00c28a72004-06-10 08:09:13 +0000119 double(bca.numFunctions);
Reid Spencerf41aa732004-06-29 23:23:12 +0000120
Chris Lattner05ac92c2006-07-06 18:02:27 +0000121 if (bca.progressiveVerify) {
122 std::string msg;
123 if (verifyModule(*M, ReturnStatusAction, &msg))
Reid Spencerb61cdb72004-07-04 11:00:39 +0000124 bca.VerifyInfo += "Verify@Finish: " + msg + "\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000125 }
Reid Spencerdac69c82004-06-07 17:53:43 +0000126 }
127
Reid Spencercbb22e22004-06-10 22:00:54 +0000128 virtual void handleModuleBegin(const std::string& id) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000129 if (os)
130 *os << " Module " << id << " {\n";
Reid Spencer649ee572004-06-09 06:16:43 +0000131 bca.ModuleId = id;
Reid Spencerdac69c82004-06-07 17:53:43 +0000132 }
133
Misha Brukman8a96c532005-04-21 21:44:41 +0000134 virtual void handleModuleEnd(const std::string& id) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000135 if (os)
136 *os << " } End Module " << id << "\n";
Chris Lattner05ac92c2006-07-06 18:02:27 +0000137 if (bca.progressiveVerify) {
138 std::string msg;
139 if (verifyModule(*M, ReturnStatusAction, &msg))
Reid Spencerb61cdb72004-07-04 11:00:39 +0000140 bca.VerifyInfo += "Verify@EndModule: " + msg + "\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000141 }
142 }
Reid Spencerdac69c82004-06-07 17:53:43 +0000143
Reid Spencercbb22e22004-06-10 22:00:54 +0000144 virtual void handleVersionInfo(
Reid Spenceraacc35a2007-01-26 08:10:24 +0000145 unsigned char RevisionNum ///< Byte code revision number
Misha Brukman8a96c532005-04-21 21:44:41 +0000146 ) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000147 if (os)
Reid Spenceraacc35a2007-01-26 08:10:24 +0000148 *os << " RevisionNum: " << int(RevisionNum) << "\n";
Reid Spencer911ec6d2004-08-21 20:58:19 +0000149 bca.version = RevisionNum;
Reid Spencerf41aa732004-06-29 23:23:12 +0000150 }
Reid Spencerdac69c82004-06-07 17:53:43 +0000151
Misha Brukman8a96c532005-04-21 21:44:41 +0000152 virtual void handleModuleGlobalsBegin() {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000153 if (os)
154 *os << " BLOCK: ModuleGlobalInfo {\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000155 }
Reid Spencerdac69c82004-06-07 17:53:43 +0000156
Misha Brukman8a96c532005-04-21 21:44:41 +0000157 virtual void handleGlobalVariable(
158 const Type* ElemType,
159 bool isConstant,
Reid Spencerf41aa732004-06-29 23:23:12 +0000160 GlobalValue::LinkageTypes Linkage,
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000161 GlobalValue::VisibilityTypes Visibility,
Reid Spencerf41aa732004-06-29 23:23:12 +0000162 unsigned SlotNum,
163 unsigned initSlot
Reid Spencercbb22e22004-06-10 22:00:54 +0000164 ) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000165 if (os) {
166 *os << " GV: "
167 << ( initSlot == 0 ? "Uni" : "I" ) << "nitialized, "
168 << ( isConstant? "Constant, " : "Variable, ")
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000169 << " Linkage=" << Linkage
170 << " Visibility="<< Visibility
171 << " Type=";
Reid Spencer911ec6d2004-08-21 20:58:19 +0000172 WriteTypeSymbolic(*os, ElemType, M);
Misha Brukman8a96c532005-04-21 21:44:41 +0000173 *os << " Slot=" << SlotNum << " InitSlot=" << initSlot
Reid Spencer911ec6d2004-08-21 20:58:19 +0000174 << "\n";
175 }
176
Reid Spencer649ee572004-06-09 06:16:43 +0000177 bca.numGlobalVars++;
Reid Spencer00c28a72004-06-10 08:09:13 +0000178 bca.numValues++;
Reid Spencer911ec6d2004-08-21 20:58:19 +0000179 if (SlotNum > bca.maxValueSlot)
180 bca.maxValueSlot = SlotNum;
181 if (initSlot > bca.maxValueSlot)
182 bca.maxValueSlot = initSlot;
Reid Spencerf41aa732004-06-29 23:23:12 +0000183
Reid Spencer911ec6d2004-08-21 20:58:19 +0000184 }
185
186 virtual void handleTypeList(unsigned numEntries) {
187 bca.maxTypeSlot = numEntries - 1;
Reid Spencerdac69c82004-06-07 17:53:43 +0000188 }
189
Misha Brukman8a96c532005-04-21 21:44:41 +0000190 virtual void handleType( const Type* Ty ) {
191 bca.numTypes++;
Reid Spencer911ec6d2004-08-21 20:58:19 +0000192 if (os) {
193 *os << " Type: ";
194 WriteTypeSymbolic(*os,Ty,M);
195 *os << "\n";
196 }
Reid Spencerdac69c82004-06-07 17:53:43 +0000197 }
198
Misha Brukman8a96c532005-04-21 21:44:41 +0000199 virtual void handleFunctionDeclaration(
Reid Spencerb61cdb72004-07-04 11:00:39 +0000200 Function* Func ///< The function
Reid Spencercbb22e22004-06-10 22:00:54 +0000201 ) {
Reid Spencer649ee572004-06-09 06:16:43 +0000202 bca.numFunctions++;
Reid Spencer00c28a72004-06-10 08:09:13 +0000203 bca.numValues++;
Reid Spencer911ec6d2004-08-21 20:58:19 +0000204 if (os) {
205 *os << " Function Decl: ";
206 WriteTypeSymbolic(*os,Func->getType(),M);
Anton Korobeynikov93c2b372006-09-17 13:06:18 +0000207 *os <<", Linkage=" << Func->getLinkage();
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000208 *os <<", Visibility=" << Func->getVisibility();
Reid Spencer911ec6d2004-08-21 20:58:19 +0000209 *os << "\n";
210 }
Reid Spencerdac69c82004-06-07 17:53:43 +0000211 }
212
Reid Spencerf41aa732004-06-29 23:23:12 +0000213 virtual void handleGlobalInitializer(GlobalVariable* GV, Constant* CV) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000214 if (os) {
215 *os << " Initializer: GV=";
216 GV->print(*os);
217 *os << " CV=";
218 CV->print(*os);
219 *os << "\n";
220 }
221 }
222
223 virtual void handleDependentLibrary(const std::string& libName) {
224 bca.numLibraries++;
225 bca.libSize += libName.size() + (libName.size() < 128 ? 1 : 2);
Reid Spencer3ee8eed2004-09-11 04:14:07 +0000226 if (os)
227 *os << " Library: '" << libName << "'\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000228 }
Reid Spencerdac69c82004-06-07 17:53:43 +0000229
Misha Brukman8a96c532005-04-21 21:44:41 +0000230 virtual void handleModuleGlobalsEnd() {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000231 if (os)
232 *os << " } END BLOCK: ModuleGlobalInfo\n";
Chris Lattner05ac92c2006-07-06 18:02:27 +0000233 if (bca.progressiveVerify) {
234 std::string msg;
235 if (verifyModule(*M, ReturnStatusAction, &msg))
Reid Spencerb61cdb72004-07-04 11:00:39 +0000236 bca.VerifyInfo += "Verify@EndModuleGlobalInfo: " + msg + "\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000237 }
238 }
239
Misha Brukman8a96c532005-04-21 21:44:41 +0000240 virtual void handleCompactionTableBegin() {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000241 if (os)
242 *os << " BLOCK: CompactionTable {\n";
Reid Spencer488d73a2004-08-27 00:43:51 +0000243 bca.numCmpctnTables++;
Reid Spencerf41aa732004-06-29 23:23:12 +0000244 }
Reid Spencerdac69c82004-06-07 17:53:43 +0000245
Reid Spencercbb22e22004-06-10 22:00:54 +0000246 virtual void handleCompactionTablePlane( unsigned Ty, unsigned NumEntries) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000247 if (os)
248 *os << " Plane: Ty=" << Ty << " Size=" << NumEntries << "\n";
Reid Spencerdac69c82004-06-07 17:53:43 +0000249 }
250
Misha Brukman8a96c532005-04-21 21:44:41 +0000251 virtual void handleCompactionTableType( unsigned i, unsigned TypSlot,
Reid Spencerf41aa732004-06-29 23:23:12 +0000252 const Type* Ty ) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000253 if (os) {
254 *os << " Type: " << i << " Slot:" << TypSlot << " is ";
255 WriteTypeSymbolic(*os,Ty,M);
Misha Brukman8a96c532005-04-21 21:44:41 +0000256 *os << "\n";
Reid Spencer911ec6d2004-08-21 20:58:19 +0000257 }
Reid Spencerf41aa732004-06-29 23:23:12 +0000258 }
Reid Spencerdac69c82004-06-07 17:53:43 +0000259
Chris Lattner2c6c14d2004-08-04 00:19:23 +0000260 virtual void handleCompactionTableValue(unsigned i, unsigned TypSlot,
Misha Brukman8a96c532005-04-21 21:44:41 +0000261 unsigned ValSlot) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000262 if (os)
Misha Brukman8a96c532005-04-21 21:44:41 +0000263 *os << " Value: " << i << " TypSlot: " << TypSlot
Chris Lattner2c6c14d2004-08-04 00:19:23 +0000264 << " ValSlot:" << ValSlot << "\n";
Reid Spencer911ec6d2004-08-21 20:58:19 +0000265 if (ValSlot > bca.maxValueSlot)
266 bca.maxValueSlot = ValSlot;
Reid Spencerf41aa732004-06-29 23:23:12 +0000267 }
Reid Spencercbb22e22004-06-10 22:00:54 +0000268
Misha Brukman8a96c532005-04-21 21:44:41 +0000269 virtual void handleCompactionTableEnd() {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000270 if (os)
271 *os << " } END BLOCK: CompactionTable\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000272 }
Reid Spencercbb22e22004-06-10 22:00:54 +0000273
Misha Brukman8a96c532005-04-21 21:44:41 +0000274 virtual void handleSymbolTableBegin(Function* CF, SymbolTable* ST) {
275 bca.numSymTab++;
Reid Spencer911ec6d2004-08-21 20:58:19 +0000276 if (os)
277 *os << " BLOCK: SymbolTable {\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000278 }
Reid Spencercbb22e22004-06-10 22:00:54 +0000279
Misha Brukman8a96c532005-04-21 21:44:41 +0000280 virtual void handleSymbolTablePlane(unsigned Ty, unsigned NumEntries,
281 const Type* Typ) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000282 if (os) {
283 *os << " Plane: Ty=" << Ty << " Size=" << NumEntries << " Type: ";
284 WriteTypeSymbolic(*os,Typ,M);
Misha Brukman8a96c532005-04-21 21:44:41 +0000285 *os << "\n";
Reid Spencer911ec6d2004-08-21 20:58:19 +0000286 }
Reid Spencerf41aa732004-06-29 23:23:12 +0000287 }
Reid Spencercbb22e22004-06-10 22:00:54 +0000288
Misha Brukman8a96c532005-04-21 21:44:41 +0000289 virtual void handleSymbolTableType(unsigned i, unsigned TypSlot,
290 const std::string& name ) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000291 if (os)
292 *os << " Type " << i << " Slot=" << TypSlot
Misha Brukman8a96c532005-04-21 21:44:41 +0000293 << " Name: " << name << "\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000294 }
Reid Spencercbb22e22004-06-10 22:00:54 +0000295
Misha Brukman8a96c532005-04-21 21:44:41 +0000296 virtual void handleSymbolTableValue(unsigned i, unsigned ValSlot,
297 const std::string& name ) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000298 if (os)
299 *os << " Value " << i << " Slot=" << ValSlot
300 << " Name: " << name << "\n";
301 if (ValSlot > bca.maxValueSlot)
302 bca.maxValueSlot = ValSlot;
Reid Spencerf41aa732004-06-29 23:23:12 +0000303 }
Reid Spencercbb22e22004-06-10 22:00:54 +0000304
Misha Brukman8a96c532005-04-21 21:44:41 +0000305 virtual void handleSymbolTableEnd() {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000306 if (os)
307 *os << " } END BLOCK: SymbolTable\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000308 }
Reid Spencercbb22e22004-06-10 22:00:54 +0000309
Reid Spencerf41aa732004-06-29 23:23:12 +0000310 virtual void handleFunctionBegin(Function* Func, unsigned Size) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000311 if (os) {
312 *os << " BLOCK: Function {\n"
313 << " Linkage: " << Func->getLinkage() << "\n"
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000314 << " Visibility: " << Func->getVisibility() << "\n"
Misha Brukman8a96c532005-04-21 21:44:41 +0000315 << " Type: ";
Reid Spencer911ec6d2004-08-21 20:58:19 +0000316 WriteTypeSymbolic(*os,Func->getType(),M);
317 *os << "\n";
318 }
319
Reid Spencercbb22e22004-06-10 22:00:54 +0000320 currFunc = &bca.FunctionInfo[Func];
Reid Spencer911ec6d2004-08-21 20:58:19 +0000321 std::ostringstream tmp;
322 WriteTypeSymbolic(tmp,Func->getType(),M);
323 currFunc->description = tmp.str();
Reid Spencercbb22e22004-06-10 22:00:54 +0000324 currFunc->name = Func->getName();
325 currFunc->byteSize = Size;
326 currFunc->numInstructions = 0;
327 currFunc->numBasicBlocks = 0;
328 currFunc->numPhis = 0;
329 currFunc->numOperands = 0;
330 currFunc->density = 0.0;
Reid Spencer1cf50242004-06-11 15:10:38 +0000331 currFunc->instructionSize = 0;
332 currFunc->longInstructions = 0;
Reid Spencercbb22e22004-06-10 22:00:54 +0000333 currFunc->vbrCount32 = 0;
334 currFunc->vbrCount64 = 0;
335 currFunc->vbrCompBytes = 0;
336 currFunc->vbrExpdBytes = 0;
Reid Spencerf41aa732004-06-29 23:23:12 +0000337
Reid Spencerdac69c82004-06-07 17:53:43 +0000338 }
339
Reid Spencercbb22e22004-06-10 22:00:54 +0000340 virtual void handleFunctionEnd( Function* Func) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000341 if (os)
342 *os << " } END BLOCK: Function\n";
Reid Spencercbb22e22004-06-10 22:00:54 +0000343 currFunc->density = double(currFunc->byteSize) /
Reid Spencer3120e712004-08-24 22:45:32 +0000344 double(currFunc->numInstructions);
Reid Spencerf41aa732004-06-29 23:23:12 +0000345
Chris Lattner05ac92c2006-07-06 18:02:27 +0000346 if (bca.progressiveVerify) {
347 std::string msg;
348 if (verifyModule(*M, ReturnStatusAction, &msg))
Reid Spencerb61cdb72004-07-04 11:00:39 +0000349 bca.VerifyInfo += "Verify@EndFunction: " + msg + "\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000350 }
Reid Spencerdac69c82004-06-07 17:53:43 +0000351 }
352
Reid Spencercbb22e22004-06-10 22:00:54 +0000353 virtual void handleBasicBlockBegin( unsigned blocknum) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000354 if (os)
355 *os << " BLOCK: BasicBlock #" << blocknum << "{\n";
Reid Spencer649ee572004-06-09 06:16:43 +0000356 bca.numBasicBlocks++;
Reid Spencer00c28a72004-06-10 08:09:13 +0000357 bca.numValues++;
Reid Spencercbb22e22004-06-10 22:00:54 +0000358 if ( currFunc ) currFunc->numBasicBlocks++;
Reid Spencerdac69c82004-06-07 17:53:43 +0000359 }
360
Misha Brukman8a96c532005-04-21 21:44:41 +0000361 virtual bool handleInstruction( unsigned Opcode, const Type* iType,
Reid Spencerb61cdb72004-07-04 11:00:39 +0000362 std::vector<unsigned>& Operands, unsigned Size){
Reid Spencer911ec6d2004-08-21 20:58:19 +0000363 if (os) {
Misha Brukman8a96c532005-04-21 21:44:41 +0000364 *os << " INST: OpCode="
Reid Spencer911ec6d2004-08-21 20:58:19 +0000365 << Instruction::getOpcodeName(Opcode) << " Type=\"";
366 WriteTypeSymbolic(*os,iType,M);
367 *os << "\"";
Misha Brukman8a96c532005-04-21 21:44:41 +0000368 for ( unsigned i = 0; i < Operands.size(); ++i )
Reid Spencer911ec6d2004-08-21 20:58:19 +0000369 *os << " Op(" << i << ")=Slot(" << Operands[i] << ")";
370 *os << "\n";
371 }
Reid Spencerf41aa732004-06-29 23:23:12 +0000372
Reid Spencer649ee572004-06-09 06:16:43 +0000373 bca.numInstructions++;
Reid Spencer00c28a72004-06-10 08:09:13 +0000374 bca.numValues++;
Reid Spencer1cf50242004-06-11 15:10:38 +0000375 bca.instructionSize += Size;
376 if (Size > 4 ) bca.longInstructions++;
Reid Spencer00c28a72004-06-10 08:09:13 +0000377 bca.numOperands += Operands.size();
Reid Spencer911ec6d2004-08-21 20:58:19 +0000378 for (unsigned i = 0; i < Operands.size(); ++i )
379 if (Operands[i] > bca.maxValueSlot)
380 bca.maxValueSlot = Operands[i];
Reid Spencercbb22e22004-06-10 22:00:54 +0000381 if ( currFunc ) {
382 currFunc->numInstructions++;
Reid Spencer1cf50242004-06-11 15:10:38 +0000383 currFunc->instructionSize += Size;
384 if (Size > 4 ) currFunc->longInstructions++;
Reid Spencer8a9a3702004-06-11 03:06:43 +0000385 if ( Opcode == Instruction::PHI ) currFunc->numPhis++;
Reid Spencercbb22e22004-06-10 22:00:54 +0000386 }
Misha Brukman8a96c532005-04-21 21:44:41 +0000387 return Instruction::isTerminator(Opcode);
Reid Spencerdac69c82004-06-07 17:53:43 +0000388 }
389
Misha Brukman8a96c532005-04-21 21:44:41 +0000390 virtual void handleBasicBlockEnd(unsigned blocknum) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000391 if (os)
Reid Spencerc6d416a2006-12-15 21:46:37 +0000392 *os << " } END BLOCK: BasicBlock #" << blocknum << "\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000393 }
Reid Spencerdac69c82004-06-07 17:53:43 +0000394
Misha Brukman8a96c532005-04-21 21:44:41 +0000395 virtual void handleGlobalConstantsBegin() {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000396 if (os)
397 *os << " BLOCK: GlobalConstants {\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000398 }
Reid Spencerdac69c82004-06-07 17:53:43 +0000399
Misha Brukman8a96c532005-04-21 21:44:41 +0000400 virtual void handleConstantExpression( unsigned Opcode,
Reid Spencerf41aa732004-06-29 23:23:12 +0000401 std::vector<Constant*> ArgVec, Constant* C ) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000402 if (os) {
403 *os << " EXPR: " << Instruction::getOpcodeName(Opcode) << "\n";
404 for ( unsigned i = 0; i < ArgVec.size(); ++i ) {
Misha Brukman8a96c532005-04-21 21:44:41 +0000405 *os << " Arg#" << i << " "; ArgVec[i]->print(*os);
Reid Spencer911ec6d2004-08-21 20:58:19 +0000406 *os << "\n";
407 }
408 *os << " Value=";
409 C->print(*os);
410 *os << "\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000411 }
Reid Spencer649ee572004-06-09 06:16:43 +0000412 bca.numConstants++;
Reid Spencer00c28a72004-06-10 08:09:13 +0000413 bca.numValues++;
Reid Spencerdac69c82004-06-07 17:53:43 +0000414 }
415
Reid Spencercbb22e22004-06-10 22:00:54 +0000416 virtual void handleConstantValue( Constant * c ) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000417 if (os) {
418 *os << " VALUE: ";
419 c->print(*os);
420 *os << "\n";
421 }
Reid Spencer649ee572004-06-09 06:16:43 +0000422 bca.numConstants++;
Reid Spencer00c28a72004-06-10 08:09:13 +0000423 bca.numValues++;
Reid Spencerdac69c82004-06-07 17:53:43 +0000424 }
425
Misha Brukman8a96c532005-04-21 21:44:41 +0000426 virtual void handleConstantArray( const ArrayType* AT,
Reid Spencerf41aa732004-06-29 23:23:12 +0000427 std::vector<Constant*>& Elements,
Reid Spencerb61cdb72004-07-04 11:00:39 +0000428 unsigned TypeSlot,
429 Constant* ArrayVal ) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000430 if (os) {
431 *os << " ARRAY: ";
Misha Brukman8a96c532005-04-21 21:44:41 +0000432 WriteTypeSymbolic(*os,AT,M);
Reid Spencer911ec6d2004-08-21 20:58:19 +0000433 *os << " TypeSlot=" << TypeSlot << "\n";
434 for ( unsigned i = 0; i < Elements.size(); ++i ) {
435 *os << " #" << i;
436 Elements[i]->print(*os);
437 *os << "\n";
438 }
439 *os << " Value=";
440 ArrayVal->print(*os);
441 *os << "\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000442 }
Reid Spencerf41aa732004-06-29 23:23:12 +0000443
Reid Spencer649ee572004-06-09 06:16:43 +0000444 bca.numConstants++;
Reid Spencer00c28a72004-06-10 08:09:13 +0000445 bca.numValues++;
Reid Spencerdac69c82004-06-07 17:53:43 +0000446 }
447
Reid Spencercbb22e22004-06-10 22:00:54 +0000448 virtual void handleConstantStruct(
Reid Spencer00c28a72004-06-10 08:09:13 +0000449 const StructType* ST,
Reid Spencerf41aa732004-06-29 23:23:12 +0000450 std::vector<Constant*>& Elements,
Reid Spencerb61cdb72004-07-04 11:00:39 +0000451 Constant* StructVal)
Reid Spencerdac69c82004-06-07 17:53:43 +0000452 {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000453 if (os) {
454 *os << " STRUC: ";
455 WriteTypeSymbolic(*os,ST,M);
456 *os << "\n";
457 for ( unsigned i = 0; i < Elements.size(); ++i ) {
Misha Brukman8a96c532005-04-21 21:44:41 +0000458 *os << " #" << i << " "; Elements[i]->print(*os);
Reid Spencer911ec6d2004-08-21 20:58:19 +0000459 *os << "\n";
460 }
461 *os << " Value=";
462 StructVal->print(*os);
463 *os << "\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000464 }
Reid Spencer649ee572004-06-09 06:16:43 +0000465 bca.numConstants++;
Reid Spencer00c28a72004-06-10 08:09:13 +0000466 bca.numValues++;
Reid Spencerdac69c82004-06-07 17:53:43 +0000467 }
468
Misha Brukman8a96c532005-04-21 21:44:41 +0000469 virtual void handleConstantPacked(
470 const PackedType* PT,
Brian Gaeke715c90b2004-08-20 06:00:58 +0000471 std::vector<Constant*>& Elements,
Misha Brukman8a96c532005-04-21 21:44:41 +0000472 unsigned TypeSlot,
473 Constant* PackedVal)
Brian Gaeke715c90b2004-08-20 06:00:58 +0000474 {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000475 if (os) {
476 *os << " PACKD: ";
477 WriteTypeSymbolic(*os,PT,M);
478 *os << " TypeSlot=" << TypeSlot << "\n";
479 for ( unsigned i = 0; i < Elements.size(); ++i ) {
480 *os << " #" << i;
481 Elements[i]->print(*os);
482 *os << "\n";
483 }
484 *os << " Value=";
485 PackedVal->print(*os);
486 *os << "\n";
Brian Gaeke715c90b2004-08-20 06:00:58 +0000487 }
Brian Gaeke715c90b2004-08-20 06:00:58 +0000488
489 bca.numConstants++;
490 bca.numValues++;
491 }
492
Misha Brukman8a96c532005-04-21 21:44:41 +0000493 virtual void handleConstantPointer( const PointerType* PT,
Reid Spencer3c90f9f2004-07-18 00:10:36 +0000494 unsigned Slot, GlobalValue* GV ) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000495 if (os) {
496 *os << " PNTR: ";
497 WriteTypeSymbolic(*os,PT,M);
498 *os << " Slot=" << Slot << " GlobalValue=";
499 GV->print(*os);
500 *os << "\n";
501 }
Reid Spencer649ee572004-06-09 06:16:43 +0000502 bca.numConstants++;
Reid Spencer00c28a72004-06-10 08:09:13 +0000503 bca.numValues++;
Reid Spencerdac69c82004-06-07 17:53:43 +0000504 }
505
Reid Spencercbb22e22004-06-10 22:00:54 +0000506 virtual void handleConstantString( const ConstantArray* CA ) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000507 if (os) {
508 *os << " STRNG: ";
Misha Brukman8a96c532005-04-21 21:44:41 +0000509 CA->print(*os);
Reid Spencer911ec6d2004-08-21 20:58:19 +0000510 *os << "\n";
511 }
Reid Spencer649ee572004-06-09 06:16:43 +0000512 bca.numConstants++;
Reid Spencer00c28a72004-06-10 08:09:13 +0000513 bca.numValues++;
Reid Spencerdac69c82004-06-07 17:53:43 +0000514 }
515
Misha Brukman8a96c532005-04-21 21:44:41 +0000516 virtual void handleGlobalConstantsEnd() {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000517 if (os)
518 *os << " } END BLOCK: GlobalConstants\n";
519
Chris Lattner05ac92c2006-07-06 18:02:27 +0000520 if (bca.progressiveVerify) {
521 std::string msg;
522 if (verifyModule(*M, ReturnStatusAction, &msg))
Reid Spencerb61cdb72004-07-04 11:00:39 +0000523 bca.VerifyInfo += "Verify@EndGlobalConstants: " + msg + "\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000524 }
525 }
Reid Spencerdac69c82004-06-07 17:53:43 +0000526
Reid Spencercbb22e22004-06-10 22:00:54 +0000527 virtual void handleAlignment(unsigned numBytes) {
Reid Spencer00c28a72004-06-10 08:09:13 +0000528 bca.numAlignment += numBytes;
Reid Spencerdac69c82004-06-07 17:53:43 +0000529 }
530
Reid Spencercbb22e22004-06-10 22:00:54 +0000531 virtual void handleBlock(
Reid Spencer00c28a72004-06-10 08:09:13 +0000532 unsigned BType, const unsigned char* StartPtr, unsigned Size) {
533 bca.numBlocks++;
Reid Spencer911ec6d2004-08-21 20:58:19 +0000534 assert(BType >= BytecodeFormat::ModuleBlockID);
535 assert(BType < BytecodeFormat::NumberOfBlockIDs);
536 bca.BlockSizes[
Reid Spencerd798a512006-11-14 04:47:22 +0000537 llvm::BytecodeFormat::BytecodeBlockIdentifiers(BType)] += Size;
Reid Spencer911ec6d2004-08-21 20:58:19 +0000538
539 if (bca.version < 3) // Check for long block headers versions
540 bca.BlockSizes[llvm::BytecodeFormat::Reserved_DoNotUse] += 8;
541 else
542 bca.BlockSizes[llvm::BytecodeFormat::Reserved_DoNotUse] += 4;
Reid Spencer00c28a72004-06-10 08:09:13 +0000543 }
544
545 virtual void handleVBR32(unsigned Size ) {
546 bca.vbrCount32++;
547 bca.vbrCompBytes += Size;
548 bca.vbrExpdBytes += sizeof(uint32_t);
Reid Spencercbb22e22004-06-10 22:00:54 +0000549 if (currFunc) {
550 currFunc->vbrCount32++;
551 currFunc->vbrCompBytes += Size;
552 currFunc->vbrExpdBytes += sizeof(uint32_t);
553 }
Reid Spencer00c28a72004-06-10 08:09:13 +0000554 }
Reid Spencercbb22e22004-06-10 22:00:54 +0000555
Reid Spencer00c28a72004-06-10 08:09:13 +0000556 virtual void handleVBR64(unsigned Size ) {
557 bca.vbrCount64++;
558 bca.vbrCompBytes += Size;
559 bca.vbrExpdBytes += sizeof(uint64_t);
Reid Spencercbb22e22004-06-10 22:00:54 +0000560 if ( currFunc ) {
561 currFunc->vbrCount64++;
562 currFunc->vbrCompBytes += Size;
563 currFunc->vbrExpdBytes += sizeof(uint64_t);
564 }
Reid Spencer00c28a72004-06-10 08:09:13 +0000565 }
Reid Spencerdac69c82004-06-07 17:53:43 +0000566};
567
Reid Spencerf41aa732004-06-29 23:23:12 +0000568
569/// @brief Utility for printing a titled unsigned value with
570/// an aligned colon.
Misha Brukman8a96c532005-04-21 21:44:41 +0000571inline static void print(std::ostream& Out, const char*title,
Reid Spencerf41aa732004-06-29 23:23:12 +0000572 unsigned val, bool nl = true ) {
Misha Brukman8a96c532005-04-21 21:44:41 +0000573 Out << std::setw(30) << std::right << title
Reid Spencerf41aa732004-06-29 23:23:12 +0000574 << std::setw(0) << ": "
575 << std::setw(9) << val << "\n";
Reid Spencerdac69c82004-06-07 17:53:43 +0000576}
577
Reid Spencerf41aa732004-06-29 23:23:12 +0000578/// @brief Utility for printing a titled double value with an
579/// aligned colon
Misha Brukman8a96c532005-04-21 21:44:41 +0000580inline static void print(std::ostream&Out, const char*title,
Reid Spencerf41aa732004-06-29 23:23:12 +0000581 double val ) {
Misha Brukman8a96c532005-04-21 21:44:41 +0000582 Out << std::setw(30) << std::right << title
Reid Spencerf41aa732004-06-29 23:23:12 +0000583 << std::setw(0) << ": "
584 << std::setw(9) << std::setprecision(6) << val << "\n" ;
585}
586
587/// @brief Utility for printing a titled double value with a
588/// percentage and aligned colon.
Misha Brukman8a96c532005-04-21 21:44:41 +0000589inline static void print(std::ostream&Out, const char*title,
Reid Spencerf41aa732004-06-29 23:23:12 +0000590 double top, double bot ) {
Misha Brukman8a96c532005-04-21 21:44:41 +0000591 Out << std::setw(30) << std::right << title
Reid Spencerf41aa732004-06-29 23:23:12 +0000592 << std::setw(0) << ": "
Misha Brukman8a96c532005-04-21 21:44:41 +0000593 << std::setw(9) << std::setprecision(6) << top
594 << " (" << std::left << std::setw(0) << std::setprecision(4)
Reid Spencerf41aa732004-06-29 23:23:12 +0000595 << (top/bot)*100.0 << "%)\n";
596}
597
598/// @brief Utility for printing a titled string value with
599/// an aligned colon.
Misha Brukman8a96c532005-04-21 21:44:41 +0000600inline static void print(std::ostream&Out, const char*title,
Reid Spencerf41aa732004-06-29 23:23:12 +0000601 std::string val, bool nl = true) {
Misha Brukman8a96c532005-04-21 21:44:41 +0000602 Out << std::setw(30) << std::right << title
Reid Spencerf41aa732004-06-29 23:23:12 +0000603 << std::setw(0) << ": "
604 << std::left << val << (nl ? "\n" : "");
605}
606
607}
608
609namespace llvm {
610
611/// This function prints the contents of rhe BytecodeAnalysis structure in
612/// a human legible form.
613/// @brief Print BytecodeAnalysis structure to an ostream
614void PrintBytecodeAnalysis(BytecodeAnalysis& bca, std::ostream& Out )
Reid Spencerdac69c82004-06-07 17:53:43 +0000615{
Reid Spencer911ec6d2004-08-21 20:58:19 +0000616 Out << "\nSummary Analysis Of " << bca.ModuleId << ": \n\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000617 print(Out, "Bytecode Analysis Of Module", bca.ModuleId);
Reid Spencer911ec6d2004-08-21 20:58:19 +0000618 print(Out, "Bytecode Version Number", bca.version);
Reid Spencerf41aa732004-06-29 23:23:12 +0000619 print(Out, "File Size", bca.byteSize);
Reid Spencer911ec6d2004-08-21 20:58:19 +0000620 print(Out, "Module Bytes",
621 double(bca.BlockSizes[BytecodeFormat::ModuleBlockID]),
622 double(bca.byteSize));
Misha Brukman8a96c532005-04-21 21:44:41 +0000623 print(Out, "Function Bytes",
Reid Spencer911ec6d2004-08-21 20:58:19 +0000624 double(bca.BlockSizes[BytecodeFormat::FunctionBlockID]),
625 double(bca.byteSize));
Misha Brukman8a96c532005-04-21 21:44:41 +0000626 print(Out, "Global Types Bytes",
Reid Spencer911ec6d2004-08-21 20:58:19 +0000627 double(bca.BlockSizes[BytecodeFormat::GlobalTypePlaneBlockID]),
628 double(bca.byteSize));
Misha Brukman8a96c532005-04-21 21:44:41 +0000629 print(Out, "Constant Pool Bytes",
Reid Spencer911ec6d2004-08-21 20:58:19 +0000630 double(bca.BlockSizes[BytecodeFormat::ConstantPoolBlockID]),
631 double(bca.byteSize));
Misha Brukman8a96c532005-04-21 21:44:41 +0000632 print(Out, "Module Globals Bytes",
Reid Spencer911ec6d2004-08-21 20:58:19 +0000633 double(bca.BlockSizes[BytecodeFormat::ModuleGlobalInfoBlockID]),
634 double(bca.byteSize));
Misha Brukman8a96c532005-04-21 21:44:41 +0000635 print(Out, "Instruction List Bytes",
Reid Spencer911ec6d2004-08-21 20:58:19 +0000636 double(bca.BlockSizes[BytecodeFormat::InstructionListBlockID]),
637 double(bca.byteSize));
Misha Brukman8a96c532005-04-21 21:44:41 +0000638 print(Out, "Compaction Table Bytes",
Reid Spencer911ec6d2004-08-21 20:58:19 +0000639 double(bca.BlockSizes[BytecodeFormat::CompactionTableBlockID]),
640 double(bca.byteSize));
Reid Spencer78d033e2007-01-06 07:24:44 +0000641 print(Out, "Value Symbol Table Bytes",
642 double(bca.BlockSizes[BytecodeFormat::ValueSymbolTableBlockID]),
643 double(bca.byteSize));
644 print(Out, "Type Symbol Table Bytes",
645 double(bca.BlockSizes[BytecodeFormat::TypeSymbolTableBlockID]),
Reid Spencer911ec6d2004-08-21 20:58:19 +0000646 double(bca.byteSize));
Misha Brukman8a96c532005-04-21 21:44:41 +0000647 print(Out, "Alignment Bytes",
Reid Spencer911ec6d2004-08-21 20:58:19 +0000648 double(bca.numAlignment), double(bca.byteSize));
Misha Brukman8a96c532005-04-21 21:44:41 +0000649 print(Out, "Block Header Bytes",
Reid Spencer911ec6d2004-08-21 20:58:19 +0000650 double(bca.BlockSizes[BytecodeFormat::Reserved_DoNotUse]),
651 double(bca.byteSize));
Misha Brukman8a96c532005-04-21 21:44:41 +0000652 print(Out, "Dependent Libraries Bytes", double(bca.libSize),
Reid Spencer911ec6d2004-08-21 20:58:19 +0000653 double(bca.byteSize));
Reid Spencerf41aa732004-06-29 23:23:12 +0000654 print(Out, "Number Of Bytecode Blocks", bca.numBlocks);
Reid Spencer911ec6d2004-08-21 20:58:19 +0000655 print(Out, "Number Of Functions", bca.numFunctions);
Reid Spencerf41aa732004-06-29 23:23:12 +0000656 print(Out, "Number Of Types", bca.numTypes);
Reid Spencerf41aa732004-06-29 23:23:12 +0000657 print(Out, "Number Of Constants", bca.numConstants);
658 print(Out, "Number Of Global Variables", bca.numGlobalVars);
Reid Spencer911ec6d2004-08-21 20:58:19 +0000659 print(Out, "Number Of Values", bca.numValues);
Reid Spencerf41aa732004-06-29 23:23:12 +0000660 print(Out, "Number Of Basic Blocks", bca.numBasicBlocks);
661 print(Out, "Number Of Instructions", bca.numInstructions);
Reid Spencer911ec6d2004-08-21 20:58:19 +0000662 print(Out, "Number Of Long Instructions", bca.longInstructions);
Reid Spencerf41aa732004-06-29 23:23:12 +0000663 print(Out, "Number Of Operands", bca.numOperands);
664 print(Out, "Number Of Compaction Tables", bca.numCmpctnTables);
665 print(Out, "Number Of Symbol Tables", bca.numSymTab);
Reid Spencer911ec6d2004-08-21 20:58:19 +0000666 print(Out, "Number Of Dependent Libs", bca.numLibraries);
667 print(Out, "Total Instruction Size", bca.instructionSize);
Misha Brukman8a96c532005-04-21 21:44:41 +0000668 print(Out, "Average Instruction Size",
Reid Spencer911ec6d2004-08-21 20:58:19 +0000669 double(bca.instructionSize)/double(bca.numInstructions));
670
Reid Spencerf41aa732004-06-29 23:23:12 +0000671 print(Out, "Maximum Type Slot Number", bca.maxTypeSlot);
672 print(Out, "Maximum Value Slot Number", bca.maxValueSlot);
Reid Spencer3120e712004-08-24 22:45:32 +0000673 print(Out, "Bytes Per Value ", bca.fileDensity);
674 print(Out, "Bytes Per Global", bca.globalsDensity);
675 print(Out, "Bytes Per Function", bca.functionDensity);
676 print(Out, "# of VBR 32-bit Integers", bca.vbrCount32);
677 print(Out, "# of VBR 64-bit Integers", bca.vbrCount64);
678 print(Out, "# of VBR Compressed Bytes", bca.vbrCompBytes);
679 print(Out, "# of VBR Expanded Bytes", bca.vbrExpdBytes);
Misha Brukman8a96c532005-04-21 21:44:41 +0000680 print(Out, "Bytes Saved With VBR",
Reid Spencer911ec6d2004-08-21 20:58:19 +0000681 double(bca.vbrExpdBytes)-double(bca.vbrCompBytes),
682 double(bca.vbrExpdBytes));
Reid Spencerf41aa732004-06-29 23:23:12 +0000683
Reid Spencer911ec6d2004-08-21 20:58:19 +0000684 if (bca.detailedResults) {
685 Out << "\nDetailed Analysis Of " << bca.ModuleId << " Functions:\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000686
Misha Brukman8a96c532005-04-21 21:44:41 +0000687 std::map<const Function*,BytecodeAnalysis::BytecodeFunctionInfo>::iterator I =
Reid Spencerf41aa732004-06-29 23:23:12 +0000688 bca.FunctionInfo.begin();
Misha Brukman8a96c532005-04-21 21:44:41 +0000689 std::map<const Function*,BytecodeAnalysis::BytecodeFunctionInfo>::iterator E =
Reid Spencerf41aa732004-06-29 23:23:12 +0000690 bca.FunctionInfo.end();
691
692 while ( I != E ) {
Chris Lattner4a8167f2004-10-15 19:40:31 +0000693 Out << std::left << std::setw(0) << "\n";
694 if (I->second.numBasicBlocks == 0) Out << "External ";
695 Out << "Function: " << I->second.name << "\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000696 print(Out, "Type:", I->second.description);
697 print(Out, "Byte Size", I->second.byteSize);
Chris Lattner4a8167f2004-10-15 19:40:31 +0000698 if (I->second.numBasicBlocks) {
699 print(Out, "Basic Blocks", I->second.numBasicBlocks);
700 print(Out, "Instructions", I->second.numInstructions);
701 print(Out, "Long Instructions", I->second.longInstructions);
702 print(Out, "Operands", I->second.numOperands);
703 print(Out, "Instruction Size", I->second.instructionSize);
Misha Brukman8a96c532005-04-21 21:44:41 +0000704 print(Out, "Average Instruction Size",
Chris Lattner4a8167f2004-10-15 19:40:31 +0000705 double(I->second.instructionSize) / I->second.numInstructions);
706 print(Out, "Bytes Per Instruction", I->second.density);
707 print(Out, "# of VBR 32-bit Integers", I->second.vbrCount32);
708 print(Out, "# of VBR 64-bit Integers", I->second.vbrCount64);
709 print(Out, "# of VBR Compressed Bytes", I->second.vbrCompBytes);
710 print(Out, "# of VBR Expanded Bytes", I->second.vbrExpdBytes);
Misha Brukman8a96c532005-04-21 21:44:41 +0000711 print(Out, "Bytes Saved With VBR",
Reid Spencere03f09c2006-11-03 01:44:51 +0000712 double(I->second.vbrExpdBytes) - I->second.vbrCompBytes);
Chris Lattner4a8167f2004-10-15 19:40:31 +0000713 }
Reid Spencerf41aa732004-06-29 23:23:12 +0000714 ++I;
715 }
716 }
717
Reid Spencerf41aa732004-06-29 23:23:12 +0000718 if ( bca.progressiveVerify )
719 Out << bca.VerifyInfo;
720}
721
Reid Spencer911ec6d2004-08-21 20:58:19 +0000722BytecodeHandler* createBytecodeAnalyzerHandler(BytecodeAnalysis& bca,
723 std::ostream* output)
Reid Spencerf41aa732004-06-29 23:23:12 +0000724{
Reid Spencer911ec6d2004-08-21 20:58:19 +0000725 return new AnalyzerHandler(bca,output);
Reid Spencerf41aa732004-06-29 23:23:12 +0000726}
727
Reid Spencerdac69c82004-06-07 17:53:43 +0000728}
729