blob: 02e1c66ae6cbf851e68b096e1d2f74495591b270 [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 +000029using 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
Misha Brukman8a96c532005-04-21 21:44:41 +000038 BytecodeAnalysis::BytecodeFunctionInfo* currFunc;
Reid Spencerf41aa732004-06-29 23:23:12 +000039 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.
Misha Brukman8a96c532005-04-21 21:44:41 +000047 AnalyzerHandler(BytecodeAnalysis& TheBca, std::ostream* output)
48 : 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:
Misha Brukman8a96c532005-04-21 21:44:41 +000057 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;
Reid Spencer78d033e2007-01-06 07:24:44 +000098 bca.BlockSizes[BytecodeFormat::ValueSymbolTableBlockID] = 0;
Reid Spencer911ec6d2004-08-21 20:58:19 +000099 bca.BlockSizes[BytecodeFormat::ModuleGlobalInfoBlockID] = 0;
100 bca.BlockSizes[BytecodeFormat::GlobalTypePlaneBlockID] = 0;
101 bca.BlockSizes[BytecodeFormat::InstructionListBlockID] = 0;
Reid Spencer78d033e2007-01-06 07:24:44 +0000102 bca.BlockSizes[BytecodeFormat::TypeSymbolTableBlockID] = 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)
Misha Brukman8a96c532005-04-21 21:44:41 +0000107 *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]);
Misha Brukman8a96c532005-04-21 21:44:41 +0000114 bca.globalsDensity = globalSize / double( bca.numTypes + bca.numConstants +
Reid Spencer00c28a72004-06-10 08:09:13 +0000115 bca.numGlobalVars );
Misha Brukman8a96c532005-04-21 21:44:41 +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
Chris Lattner05ac92c2006-07-06 18:02:27 +0000119 if (bca.progressiveVerify) {
120 std::string msg;
121 if (verifyModule(*M, ReturnStatusAction, &msg))
Reid Spencerb61cdb72004-07-04 11:00:39 +0000122 bca.VerifyInfo += "Verify@Finish: " + msg + "\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000123 }
Reid Spencerdac69c82004-06-07 17:53:43 +0000124 }
125
Reid Spencercbb22e22004-06-10 22:00:54 +0000126 virtual void handleModuleBegin(const std::string& id) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000127 if (os)
128 *os << " Module " << id << " {\n";
Reid Spencer649ee572004-06-09 06:16:43 +0000129 bca.ModuleId = id;
Reid Spencerdac69c82004-06-07 17:53:43 +0000130 }
131
Misha Brukman8a96c532005-04-21 21:44:41 +0000132 virtual void handleModuleEnd(const std::string& id) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000133 if (os)
134 *os << " } End Module " << id << "\n";
Chris Lattner05ac92c2006-07-06 18:02:27 +0000135 if (bca.progressiveVerify) {
136 std::string msg;
137 if (verifyModule(*M, ReturnStatusAction, &msg))
Reid Spencerb61cdb72004-07-04 11:00:39 +0000138 bca.VerifyInfo += "Verify@EndModule: " + msg + "\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000139 }
140 }
Reid Spencerdac69c82004-06-07 17:53:43 +0000141
Reid Spencercbb22e22004-06-10 22:00:54 +0000142 virtual void handleVersionInfo(
Reid Spenceraacc35a2007-01-26 08:10:24 +0000143 unsigned char RevisionNum ///< Byte code revision number
Misha Brukman8a96c532005-04-21 21:44:41 +0000144 ) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000145 if (os)
Reid Spenceraacc35a2007-01-26 08:10:24 +0000146 *os << " RevisionNum: " << int(RevisionNum) << "\n";
Reid Spencer911ec6d2004-08-21 20:58:19 +0000147 bca.version = RevisionNum;
Reid Spencerf41aa732004-06-29 23:23:12 +0000148 }
Reid Spencerdac69c82004-06-07 17:53:43 +0000149
Misha Brukman8a96c532005-04-21 21:44:41 +0000150 virtual void handleModuleGlobalsBegin() {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000151 if (os)
152 *os << " BLOCK: ModuleGlobalInfo {\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000153 }
Reid Spencerdac69c82004-06-07 17:53:43 +0000154
Misha Brukman8a96c532005-04-21 21:44:41 +0000155 virtual void handleGlobalVariable(
156 const Type* ElemType,
157 bool isConstant,
Reid Spencerf41aa732004-06-29 23:23:12 +0000158 GlobalValue::LinkageTypes Linkage,
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000159 GlobalValue::VisibilityTypes Visibility,
Reid Spencerf41aa732004-06-29 23:23:12 +0000160 unsigned SlotNum,
161 unsigned initSlot
Reid Spencercbb22e22004-06-10 22:00:54 +0000162 ) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000163 if (os) {
164 *os << " GV: "
165 << ( initSlot == 0 ? "Uni" : "I" ) << "nitialized, "
166 << ( isConstant? "Constant, " : "Variable, ")
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000167 << " Linkage=" << Linkage
168 << " Visibility="<< Visibility
169 << " Type=";
Reid Spencer911ec6d2004-08-21 20:58:19 +0000170 WriteTypeSymbolic(*os, ElemType, M);
Misha Brukman8a96c532005-04-21 21:44:41 +0000171 *os << " Slot=" << SlotNum << " InitSlot=" << initSlot
Reid Spencer911ec6d2004-08-21 20:58:19 +0000172 << "\n";
173 }
174
Reid Spencer649ee572004-06-09 06:16:43 +0000175 bca.numGlobalVars++;
Reid Spencer00c28a72004-06-10 08:09:13 +0000176 bca.numValues++;
Reid Spencer911ec6d2004-08-21 20:58:19 +0000177 if (SlotNum > bca.maxValueSlot)
178 bca.maxValueSlot = SlotNum;
179 if (initSlot > bca.maxValueSlot)
180 bca.maxValueSlot = initSlot;
Reid Spencerf41aa732004-06-29 23:23:12 +0000181
Reid Spencer911ec6d2004-08-21 20:58:19 +0000182 }
183
184 virtual void handleTypeList(unsigned numEntries) {
185 bca.maxTypeSlot = numEntries - 1;
Reid Spencerdac69c82004-06-07 17:53:43 +0000186 }
187
Misha Brukman8a96c532005-04-21 21:44:41 +0000188 virtual void handleType( const Type* Ty ) {
189 bca.numTypes++;
Reid Spencer911ec6d2004-08-21 20:58:19 +0000190 if (os) {
191 *os << " Type: ";
192 WriteTypeSymbolic(*os,Ty,M);
193 *os << "\n";
194 }
Reid Spencerdac69c82004-06-07 17:53:43 +0000195 }
196
Misha Brukman8a96c532005-04-21 21:44:41 +0000197 virtual void handleFunctionDeclaration(
Reid Spencerb61cdb72004-07-04 11:00:39 +0000198 Function* Func ///< The function
Reid Spencercbb22e22004-06-10 22:00:54 +0000199 ) {
Reid Spencer649ee572004-06-09 06:16:43 +0000200 bca.numFunctions++;
Reid Spencer00c28a72004-06-10 08:09:13 +0000201 bca.numValues++;
Reid Spencer911ec6d2004-08-21 20:58:19 +0000202 if (os) {
203 *os << " Function Decl: ";
204 WriteTypeSymbolic(*os,Func->getType(),M);
Anton Korobeynikov93c2b372006-09-17 13:06:18 +0000205 *os <<", Linkage=" << Func->getLinkage();
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000206 *os <<", Visibility=" << Func->getVisibility();
Reid Spencer911ec6d2004-08-21 20:58:19 +0000207 *os << "\n";
208 }
Reid Spencerdac69c82004-06-07 17:53:43 +0000209 }
210
Reid Spencerf41aa732004-06-29 23:23:12 +0000211 virtual void handleGlobalInitializer(GlobalVariable* GV, Constant* CV) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000212 if (os) {
213 *os << " Initializer: GV=";
214 GV->print(*os);
215 *os << " CV=";
216 CV->print(*os);
217 *os << "\n";
218 }
219 }
220
221 virtual void handleDependentLibrary(const std::string& libName) {
222 bca.numLibraries++;
223 bca.libSize += libName.size() + (libName.size() < 128 ? 1 : 2);
Reid Spencer3ee8eed2004-09-11 04:14:07 +0000224 if (os)
225 *os << " Library: '" << libName << "'\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000226 }
Reid Spencerdac69c82004-06-07 17:53:43 +0000227
Misha Brukman8a96c532005-04-21 21:44:41 +0000228 virtual void handleModuleGlobalsEnd() {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000229 if (os)
230 *os << " } END BLOCK: ModuleGlobalInfo\n";
Chris Lattner05ac92c2006-07-06 18:02:27 +0000231 if (bca.progressiveVerify) {
232 std::string msg;
233 if (verifyModule(*M, ReturnStatusAction, &msg))
Reid Spencerb61cdb72004-07-04 11:00:39 +0000234 bca.VerifyInfo += "Verify@EndModuleGlobalInfo: " + msg + "\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000235 }
236 }
237
Misha Brukman8a96c532005-04-21 21:44:41 +0000238 virtual void handleCompactionTableBegin() {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000239 if (os)
240 *os << " BLOCK: CompactionTable {\n";
Reid Spencer488d73a2004-08-27 00:43:51 +0000241 bca.numCmpctnTables++;
Reid Spencerf41aa732004-06-29 23:23:12 +0000242 }
Reid Spencerdac69c82004-06-07 17:53:43 +0000243
Reid Spencercbb22e22004-06-10 22:00:54 +0000244 virtual void handleCompactionTablePlane( unsigned Ty, unsigned NumEntries) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000245 if (os)
246 *os << " Plane: Ty=" << Ty << " Size=" << NumEntries << "\n";
Reid Spencerdac69c82004-06-07 17:53:43 +0000247 }
248
Misha Brukman8a96c532005-04-21 21:44:41 +0000249 virtual void handleCompactionTableType( unsigned i, unsigned TypSlot,
Reid Spencerf41aa732004-06-29 23:23:12 +0000250 const Type* Ty ) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000251 if (os) {
252 *os << " Type: " << i << " Slot:" << TypSlot << " is ";
253 WriteTypeSymbolic(*os,Ty,M);
Misha Brukman8a96c532005-04-21 21:44:41 +0000254 *os << "\n";
Reid Spencer911ec6d2004-08-21 20:58:19 +0000255 }
Reid Spencerf41aa732004-06-29 23:23:12 +0000256 }
Reid Spencerdac69c82004-06-07 17:53:43 +0000257
Chris Lattner2c6c14d2004-08-04 00:19:23 +0000258 virtual void handleCompactionTableValue(unsigned i, unsigned TypSlot,
Misha Brukman8a96c532005-04-21 21:44:41 +0000259 unsigned ValSlot) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000260 if (os)
Misha Brukman8a96c532005-04-21 21:44:41 +0000261 *os << " Value: " << i << " TypSlot: " << TypSlot
Chris Lattner2c6c14d2004-08-04 00:19:23 +0000262 << " ValSlot:" << ValSlot << "\n";
Reid Spencer911ec6d2004-08-21 20:58:19 +0000263 if (ValSlot > bca.maxValueSlot)
264 bca.maxValueSlot = ValSlot;
Reid Spencerf41aa732004-06-29 23:23:12 +0000265 }
Reid Spencercbb22e22004-06-10 22:00:54 +0000266
Misha Brukman8a96c532005-04-21 21:44:41 +0000267 virtual void handleCompactionTableEnd() {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000268 if (os)
269 *os << " } END BLOCK: CompactionTable\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000270 }
Reid Spencercbb22e22004-06-10 22:00:54 +0000271
Reid Spenceref9b9a72007-02-05 20:47:22 +0000272 virtual void handleTypeSymbolTableBegin(TypeSymbolTable* ST) {
Misha Brukman8a96c532005-04-21 21:44:41 +0000273 bca.numSymTab++;
Reid Spencer911ec6d2004-08-21 20:58:19 +0000274 if (os)
Reid Spenceref9b9a72007-02-05 20:47:22 +0000275 *os << " BLOCK: TypeSymbolTable {\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000276 }
Reid Spenceref9b9a72007-02-05 20:47:22 +0000277 virtual void handleValueSymbolTableBegin(Function* CF, ValueSymbolTable* ST) {
278 bca.numSymTab++;
279 if (os)
280 *os << " BLOCK: ValueSymbolTable {\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000281 }
Reid Spencercbb22e22004-06-10 22:00:54 +0000282
Misha Brukman8a96c532005-04-21 21:44:41 +0000283 virtual void handleSymbolTableType(unsigned i, unsigned TypSlot,
284 const std::string& name ) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000285 if (os)
286 *os << " Type " << i << " Slot=" << TypSlot
Misha Brukman8a96c532005-04-21 21:44:41 +0000287 << " Name: " << name << "\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000288 }
Reid Spencercbb22e22004-06-10 22:00:54 +0000289
Reid Spenceref9b9a72007-02-05 20:47:22 +0000290 virtual void handleSymbolTableValue(unsigned TySlot, unsigned ValSlot,
291 const std::string& name) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000292 if (os)
Reid Spenceref9b9a72007-02-05 20:47:22 +0000293 *os << " Value " << TySlot << " Slot=" << ValSlot
Reid Spencer911ec6d2004-08-21 20:58:19 +0000294 << " Name: " << name << "\n";
295 if (ValSlot > bca.maxValueSlot)
296 bca.maxValueSlot = ValSlot;
Reid Spencerf41aa732004-06-29 23:23:12 +0000297 }
Reid Spencercbb22e22004-06-10 22:00:54 +0000298
Reid Spenceref9b9a72007-02-05 20:47:22 +0000299 virtual void handleValueSymbolTableEnd() {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000300 if (os)
Reid Spenceref9b9a72007-02-05 20:47:22 +0000301 *os << " } END BLOCK: ValueSymbolTable\n";
302 }
303
304 virtual void handleTypeSymbolTableEnd() {
305 if (os)
306 *os << " } END BLOCK: TypeSymbolTable\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000307 }
Reid Spencercbb22e22004-06-10 22:00:54 +0000308
Reid Spencerf41aa732004-06-29 23:23:12 +0000309 virtual void handleFunctionBegin(Function* Func, unsigned Size) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000310 if (os) {
311 *os << " BLOCK: Function {\n"
312 << " Linkage: " << Func->getLinkage() << "\n"
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000313 << " Visibility: " << Func->getVisibility() << "\n"
Misha Brukman8a96c532005-04-21 21:44:41 +0000314 << " Type: ";
Reid Spencer911ec6d2004-08-21 20:58:19 +0000315 WriteTypeSymbolic(*os,Func->getType(),M);
316 *os << "\n";
317 }
318
Reid Spencercbb22e22004-06-10 22:00:54 +0000319 currFunc = &bca.FunctionInfo[Func];
Reid Spencer911ec6d2004-08-21 20:58:19 +0000320 std::ostringstream tmp;
321 WriteTypeSymbolic(tmp,Func->getType(),M);
322 currFunc->description = tmp.str();
Reid Spencercbb22e22004-06-10 22:00:54 +0000323 currFunc->name = Func->getName();
324 currFunc->byteSize = Size;
325 currFunc->numInstructions = 0;
326 currFunc->numBasicBlocks = 0;
327 currFunc->numPhis = 0;
328 currFunc->numOperands = 0;
329 currFunc->density = 0.0;
Reid Spencer1cf50242004-06-11 15:10:38 +0000330 currFunc->instructionSize = 0;
331 currFunc->longInstructions = 0;
Reid Spencercbb22e22004-06-10 22:00:54 +0000332 currFunc->vbrCount32 = 0;
333 currFunc->vbrCount64 = 0;
334 currFunc->vbrCompBytes = 0;
335 currFunc->vbrExpdBytes = 0;
Reid Spencerf41aa732004-06-29 23:23:12 +0000336
Reid Spencerdac69c82004-06-07 17:53:43 +0000337 }
338
Reid Spencercbb22e22004-06-10 22:00:54 +0000339 virtual void handleFunctionEnd( Function* Func) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000340 if (os)
341 *os << " } END BLOCK: Function\n";
Reid Spencercbb22e22004-06-10 22:00:54 +0000342 currFunc->density = double(currFunc->byteSize) /
Reid Spencer3120e712004-08-24 22:45:32 +0000343 double(currFunc->numInstructions);
Reid Spencerf41aa732004-06-29 23:23:12 +0000344
Chris Lattner05ac92c2006-07-06 18:02:27 +0000345 if (bca.progressiveVerify) {
346 std::string msg;
347 if (verifyModule(*M, ReturnStatusAction, &msg))
Reid Spencerb61cdb72004-07-04 11:00:39 +0000348 bca.VerifyInfo += "Verify@EndFunction: " + msg + "\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000349 }
Reid Spencerdac69c82004-06-07 17:53:43 +0000350 }
351
Reid Spencercbb22e22004-06-10 22:00:54 +0000352 virtual void handleBasicBlockBegin( unsigned blocknum) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000353 if (os)
354 *os << " BLOCK: BasicBlock #" << blocknum << "{\n";
Reid Spencer649ee572004-06-09 06:16:43 +0000355 bca.numBasicBlocks++;
Reid Spencer00c28a72004-06-10 08:09:13 +0000356 bca.numValues++;
Reid Spencercbb22e22004-06-10 22:00:54 +0000357 if ( currFunc ) currFunc->numBasicBlocks++;
Reid Spencerdac69c82004-06-07 17:53:43 +0000358 }
359
Misha Brukman8a96c532005-04-21 21:44:41 +0000360 virtual bool handleInstruction( unsigned Opcode, const Type* iType,
Chris Lattner63cf59e2007-02-07 05:08:39 +0000361 unsigned *Operands, unsigned NumOps,
Reid Spenceref9b9a72007-02-05 20:47:22 +0000362 Instruction *Inst,
363 unsigned Size){
Reid Spencer911ec6d2004-08-21 20:58:19 +0000364 if (os) {
Misha Brukman8a96c532005-04-21 21:44:41 +0000365 *os << " INST: OpCode="
Reid Spenceref9b9a72007-02-05 20:47:22 +0000366 << Instruction::getOpcodeName(Opcode);
Chris Lattner63cf59e2007-02-07 05:08:39 +0000367 for (unsigned i = 0; i != NumOps; ++i)
Reid Spenceref9b9a72007-02-05 20:47:22 +0000368 *os << " Op(" << Operands[i] << ")";
369 *os << *Inst;
Reid Spencer911ec6d2004-08-21 20:58:19 +0000370 }
Reid Spencerf41aa732004-06-29 23:23:12 +0000371
Reid Spencer649ee572004-06-09 06:16:43 +0000372 bca.numInstructions++;
Reid Spencer00c28a72004-06-10 08:09:13 +0000373 bca.numValues++;
Reid Spencer1cf50242004-06-11 15:10:38 +0000374 bca.instructionSize += Size;
375 if (Size > 4 ) bca.longInstructions++;
Chris Lattner63cf59e2007-02-07 05:08:39 +0000376 bca.numOperands += NumOps;
377 for (unsigned i = 0; i != NumOps; ++i)
Reid Spencer911ec6d2004-08-21 20:58:19 +0000378 if (Operands[i] > bca.maxValueSlot)
379 bca.maxValueSlot = Operands[i];
Reid Spencercbb22e22004-06-10 22:00:54 +0000380 if ( currFunc ) {
381 currFunc->numInstructions++;
Reid Spencer1cf50242004-06-11 15:10:38 +0000382 currFunc->instructionSize += Size;
383 if (Size > 4 ) currFunc->longInstructions++;
Chris Lattner63cf59e2007-02-07 05:08:39 +0000384 if (Opcode == Instruction::PHI) currFunc->numPhis++;
Reid Spencercbb22e22004-06-10 22:00:54 +0000385 }
Misha Brukman8a96c532005-04-21 21:44:41 +0000386 return Instruction::isTerminator(Opcode);
Reid Spencerdac69c82004-06-07 17:53:43 +0000387 }
388
Misha Brukman8a96c532005-04-21 21:44:41 +0000389 virtual void handleBasicBlockEnd(unsigned blocknum) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000390 if (os)
Reid Spencerc6d416a2006-12-15 21:46:37 +0000391 *os << " } END BLOCK: BasicBlock #" << blocknum << "\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000392 }
Reid Spencerdac69c82004-06-07 17:53:43 +0000393
Misha Brukman8a96c532005-04-21 21:44:41 +0000394 virtual void handleGlobalConstantsBegin() {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000395 if (os)
396 *os << " BLOCK: GlobalConstants {\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000397 }
Reid Spencerdac69c82004-06-07 17:53:43 +0000398
Chris Lattner63cf59e2007-02-07 05:08:39 +0000399 virtual void handleConstantExpression(unsigned Opcode,
400 Constant**ArgVec, unsigned NumArgs, Constant* C) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000401 if (os) {
402 *os << " EXPR: " << Instruction::getOpcodeName(Opcode) << "\n";
Chris Lattner63cf59e2007-02-07 05:08:39 +0000403 for ( unsigned i = 0; i != NumArgs; ++i ) {
Misha Brukman8a96c532005-04-21 21:44:41 +0000404 *os << " Arg#" << i << " "; ArgVec[i]->print(*os);
Reid Spencer911ec6d2004-08-21 20:58:19 +0000405 *os << "\n";
406 }
407 *os << " Value=";
408 C->print(*os);
409 *os << "\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000410 }
Reid Spencer649ee572004-06-09 06:16:43 +0000411 bca.numConstants++;
Reid Spencer00c28a72004-06-10 08:09:13 +0000412 bca.numValues++;
Reid Spencerdac69c82004-06-07 17:53:43 +0000413 }
414
Reid Spencercbb22e22004-06-10 22:00:54 +0000415 virtual void handleConstantValue( Constant * c ) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000416 if (os) {
417 *os << " VALUE: ";
418 c->print(*os);
419 *os << "\n";
420 }
Reid Spencer649ee572004-06-09 06:16:43 +0000421 bca.numConstants++;
Reid Spencer00c28a72004-06-10 08:09:13 +0000422 bca.numValues++;
Reid Spencerdac69c82004-06-07 17:53:43 +0000423 }
424
Misha Brukman8a96c532005-04-21 21:44:41 +0000425 virtual void handleConstantArray( const ArrayType* AT,
Chris Lattner63cf59e2007-02-07 05:08:39 +0000426 Constant**Elements, unsigned NumElts,
Reid Spencerb61cdb72004-07-04 11:00:39 +0000427 unsigned TypeSlot,
428 Constant* ArrayVal ) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000429 if (os) {
430 *os << " ARRAY: ";
Misha Brukman8a96c532005-04-21 21:44:41 +0000431 WriteTypeSymbolic(*os,AT,M);
Reid Spencer911ec6d2004-08-21 20:58:19 +0000432 *os << " TypeSlot=" << TypeSlot << "\n";
Chris Lattner63cf59e2007-02-07 05:08:39 +0000433 for (unsigned i = 0; i != NumElts; ++i) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000434 *os << " #" << i;
435 Elements[i]->print(*os);
436 *os << "\n";
437 }
438 *os << " Value=";
439 ArrayVal->print(*os);
440 *os << "\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000441 }
Reid Spencerf41aa732004-06-29 23:23:12 +0000442
Reid Spencer649ee572004-06-09 06:16:43 +0000443 bca.numConstants++;
Reid Spencer00c28a72004-06-10 08:09:13 +0000444 bca.numValues++;
Reid Spencerdac69c82004-06-07 17:53:43 +0000445 }
446
Reid Spencercbb22e22004-06-10 22:00:54 +0000447 virtual void handleConstantStruct(
Reid Spencer00c28a72004-06-10 08:09:13 +0000448 const StructType* ST,
Chris Lattner63cf59e2007-02-07 05:08:39 +0000449 Constant**Elements, unsigned NumElts,
Reid Spencerb61cdb72004-07-04 11:00:39 +0000450 Constant* StructVal)
Reid Spencerdac69c82004-06-07 17:53:43 +0000451 {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000452 if (os) {
453 *os << " STRUC: ";
454 WriteTypeSymbolic(*os,ST,M);
455 *os << "\n";
Chris Lattner63cf59e2007-02-07 05:08:39 +0000456 for ( unsigned i = 0; i != NumElts; ++i) {
Misha Brukman8a96c532005-04-21 21:44:41 +0000457 *os << " #" << i << " "; Elements[i]->print(*os);
Reid Spencer911ec6d2004-08-21 20:58:19 +0000458 *os << "\n";
459 }
460 *os << " Value=";
461 StructVal->print(*os);
462 *os << "\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000463 }
Reid Spencer649ee572004-06-09 06:16:43 +0000464 bca.numConstants++;
Reid Spencer00c28a72004-06-10 08:09:13 +0000465 bca.numValues++;
Reid Spencerdac69c82004-06-07 17:53:43 +0000466 }
467
Misha Brukman8a96c532005-04-21 21:44:41 +0000468 virtual void handleConstantPacked(
469 const PackedType* PT,
Chris Lattner63cf59e2007-02-07 05:08:39 +0000470 Constant**Elements, unsigned NumElts,
Misha Brukman8a96c532005-04-21 21:44:41 +0000471 unsigned TypeSlot,
472 Constant* PackedVal)
Brian Gaeke715c90b2004-08-20 06:00:58 +0000473 {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000474 if (os) {
475 *os << " PACKD: ";
476 WriteTypeSymbolic(*os,PT,M);
477 *os << " TypeSlot=" << TypeSlot << "\n";
Chris Lattner63cf59e2007-02-07 05:08:39 +0000478 for ( unsigned i = 0; i != NumElts; ++i ) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000479 *os << " #" << i;
480 Elements[i]->print(*os);
481 *os << "\n";
482 }
483 *os << " Value=";
484 PackedVal->print(*os);
485 *os << "\n";
Brian Gaeke715c90b2004-08-20 06:00:58 +0000486 }
Brian Gaeke715c90b2004-08-20 06:00:58 +0000487
488 bca.numConstants++;
489 bca.numValues++;
490 }
491
Misha Brukman8a96c532005-04-21 21:44:41 +0000492 virtual void handleConstantPointer( const PointerType* PT,
Reid Spencer3c90f9f2004-07-18 00:10:36 +0000493 unsigned Slot, GlobalValue* GV ) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000494 if (os) {
495 *os << " PNTR: ";
496 WriteTypeSymbolic(*os,PT,M);
497 *os << " Slot=" << Slot << " GlobalValue=";
498 GV->print(*os);
499 *os << "\n";
500 }
Reid Spencer649ee572004-06-09 06:16:43 +0000501 bca.numConstants++;
Reid Spencer00c28a72004-06-10 08:09:13 +0000502 bca.numValues++;
Reid Spencerdac69c82004-06-07 17:53:43 +0000503 }
504
Reid Spencercbb22e22004-06-10 22:00:54 +0000505 virtual void handleConstantString( const ConstantArray* CA ) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000506 if (os) {
507 *os << " STRNG: ";
Misha Brukman8a96c532005-04-21 21:44:41 +0000508 CA->print(*os);
Reid Spencer911ec6d2004-08-21 20:58:19 +0000509 *os << "\n";
510 }
Reid Spencer649ee572004-06-09 06:16:43 +0000511 bca.numConstants++;
Reid Spencer00c28a72004-06-10 08:09:13 +0000512 bca.numValues++;
Reid Spencerdac69c82004-06-07 17:53:43 +0000513 }
514
Misha Brukman8a96c532005-04-21 21:44:41 +0000515 virtual void handleGlobalConstantsEnd() {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000516 if (os)
517 *os << " } END BLOCK: GlobalConstants\n";
518
Chris Lattner05ac92c2006-07-06 18:02:27 +0000519 if (bca.progressiveVerify) {
520 std::string msg;
521 if (verifyModule(*M, ReturnStatusAction, &msg))
Reid Spencerb61cdb72004-07-04 11:00:39 +0000522 bca.VerifyInfo += "Verify@EndGlobalConstants: " + msg + "\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000523 }
524 }
Reid Spencerdac69c82004-06-07 17:53:43 +0000525
Reid Spencercbb22e22004-06-10 22:00:54 +0000526 virtual void handleAlignment(unsigned numBytes) {
Reid Spencer00c28a72004-06-10 08:09:13 +0000527 bca.numAlignment += numBytes;
Reid Spencerdac69c82004-06-07 17:53:43 +0000528 }
529
Reid Spencercbb22e22004-06-10 22:00:54 +0000530 virtual void handleBlock(
Reid Spencer00c28a72004-06-10 08:09:13 +0000531 unsigned BType, const unsigned char* StartPtr, unsigned Size) {
532 bca.numBlocks++;
Reid Spencer911ec6d2004-08-21 20:58:19 +0000533 assert(BType >= BytecodeFormat::ModuleBlockID);
534 assert(BType < BytecodeFormat::NumberOfBlockIDs);
535 bca.BlockSizes[
Reid Spencerd798a512006-11-14 04:47:22 +0000536 llvm::BytecodeFormat::BytecodeBlockIdentifiers(BType)] += Size;
Reid Spencer911ec6d2004-08-21 20:58:19 +0000537
538 if (bca.version < 3) // Check for long block headers versions
539 bca.BlockSizes[llvm::BytecodeFormat::Reserved_DoNotUse] += 8;
540 else
541 bca.BlockSizes[llvm::BytecodeFormat::Reserved_DoNotUse] += 4;
Reid Spencer00c28a72004-06-10 08:09:13 +0000542 }
543
Reid Spencerdac69c82004-06-07 17:53:43 +0000544};
545
Reid Spencerf41aa732004-06-29 23:23:12 +0000546
547/// @brief Utility for printing a titled unsigned value with
548/// an aligned colon.
Misha Brukman8a96c532005-04-21 21:44:41 +0000549inline static void print(std::ostream& Out, const char*title,
Reid Spencerf41aa732004-06-29 23:23:12 +0000550 unsigned val, bool nl = true ) {
Misha Brukman8a96c532005-04-21 21:44:41 +0000551 Out << std::setw(30) << std::right << title
Reid Spencerf41aa732004-06-29 23:23:12 +0000552 << std::setw(0) << ": "
553 << std::setw(9) << val << "\n";
Reid Spencerdac69c82004-06-07 17:53:43 +0000554}
555
Reid Spencerf41aa732004-06-29 23:23:12 +0000556/// @brief Utility for printing a titled double value with an
557/// aligned colon
Misha Brukman8a96c532005-04-21 21:44:41 +0000558inline static void print(std::ostream&Out, const char*title,
Reid Spencerf41aa732004-06-29 23:23:12 +0000559 double val ) {
Misha Brukman8a96c532005-04-21 21:44:41 +0000560 Out << std::setw(30) << std::right << title
Reid Spencerf41aa732004-06-29 23:23:12 +0000561 << std::setw(0) << ": "
562 << std::setw(9) << std::setprecision(6) << val << "\n" ;
563}
564
565/// @brief Utility for printing a titled double value with a
566/// percentage and aligned colon.
Misha Brukman8a96c532005-04-21 21:44:41 +0000567inline static void print(std::ostream&Out, const char*title,
Reid Spencerf41aa732004-06-29 23:23:12 +0000568 double top, double bot ) {
Misha Brukman8a96c532005-04-21 21:44:41 +0000569 Out << std::setw(30) << std::right << title
Reid Spencerf41aa732004-06-29 23:23:12 +0000570 << std::setw(0) << ": "
Misha Brukman8a96c532005-04-21 21:44:41 +0000571 << std::setw(9) << std::setprecision(6) << top
572 << " (" << std::left << std::setw(0) << std::setprecision(4)
Reid Spencerf41aa732004-06-29 23:23:12 +0000573 << (top/bot)*100.0 << "%)\n";
574}
575
576/// @brief Utility for printing a titled string value with
577/// an aligned colon.
Misha Brukman8a96c532005-04-21 21:44:41 +0000578inline static void print(std::ostream&Out, const char*title,
Reid Spencerf41aa732004-06-29 23:23:12 +0000579 std::string val, bool nl = true) {
Misha Brukman8a96c532005-04-21 21:44:41 +0000580 Out << std::setw(30) << std::right << title
Reid Spencerf41aa732004-06-29 23:23:12 +0000581 << std::setw(0) << ": "
582 << std::left << val << (nl ? "\n" : "");
583}
584
585}
586
587namespace llvm {
588
589/// This function prints the contents of rhe BytecodeAnalysis structure in
590/// a human legible form.
591/// @brief Print BytecodeAnalysis structure to an ostream
592void PrintBytecodeAnalysis(BytecodeAnalysis& bca, std::ostream& Out )
Reid Spencerdac69c82004-06-07 17:53:43 +0000593{
Reid Spencer911ec6d2004-08-21 20:58:19 +0000594 Out << "\nSummary Analysis Of " << bca.ModuleId << ": \n\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000595 print(Out, "Bytecode Analysis Of Module", bca.ModuleId);
Reid Spencer911ec6d2004-08-21 20:58:19 +0000596 print(Out, "Bytecode Version Number", bca.version);
Reid Spencerf41aa732004-06-29 23:23:12 +0000597 print(Out, "File Size", bca.byteSize);
Reid Spencer911ec6d2004-08-21 20:58:19 +0000598 print(Out, "Module Bytes",
599 double(bca.BlockSizes[BytecodeFormat::ModuleBlockID]),
600 double(bca.byteSize));
Misha Brukman8a96c532005-04-21 21:44:41 +0000601 print(Out, "Function Bytes",
Reid Spencer911ec6d2004-08-21 20:58:19 +0000602 double(bca.BlockSizes[BytecodeFormat::FunctionBlockID]),
603 double(bca.byteSize));
Misha Brukman8a96c532005-04-21 21:44:41 +0000604 print(Out, "Global Types Bytes",
Reid Spencer911ec6d2004-08-21 20:58:19 +0000605 double(bca.BlockSizes[BytecodeFormat::GlobalTypePlaneBlockID]),
606 double(bca.byteSize));
Misha Brukman8a96c532005-04-21 21:44:41 +0000607 print(Out, "Constant Pool Bytes",
Reid Spencer911ec6d2004-08-21 20:58:19 +0000608 double(bca.BlockSizes[BytecodeFormat::ConstantPoolBlockID]),
609 double(bca.byteSize));
Misha Brukman8a96c532005-04-21 21:44:41 +0000610 print(Out, "Module Globals Bytes",
Reid Spencer911ec6d2004-08-21 20:58:19 +0000611 double(bca.BlockSizes[BytecodeFormat::ModuleGlobalInfoBlockID]),
612 double(bca.byteSize));
Misha Brukman8a96c532005-04-21 21:44:41 +0000613 print(Out, "Instruction List Bytes",
Reid Spencer911ec6d2004-08-21 20:58:19 +0000614 double(bca.BlockSizes[BytecodeFormat::InstructionListBlockID]),
615 double(bca.byteSize));
Reid Spencer78d033e2007-01-06 07:24:44 +0000616 print(Out, "Value Symbol Table Bytes",
617 double(bca.BlockSizes[BytecodeFormat::ValueSymbolTableBlockID]),
618 double(bca.byteSize));
619 print(Out, "Type Symbol Table Bytes",
620 double(bca.BlockSizes[BytecodeFormat::TypeSymbolTableBlockID]),
Reid Spencer911ec6d2004-08-21 20:58:19 +0000621 double(bca.byteSize));
Misha Brukman8a96c532005-04-21 21:44:41 +0000622 print(Out, "Alignment Bytes",
Reid Spencer911ec6d2004-08-21 20:58:19 +0000623 double(bca.numAlignment), double(bca.byteSize));
Misha Brukman8a96c532005-04-21 21:44:41 +0000624 print(Out, "Block Header Bytes",
Reid Spencer911ec6d2004-08-21 20:58:19 +0000625 double(bca.BlockSizes[BytecodeFormat::Reserved_DoNotUse]),
626 double(bca.byteSize));
Misha Brukman8a96c532005-04-21 21:44:41 +0000627 print(Out, "Dependent Libraries Bytes", double(bca.libSize),
Reid Spencer911ec6d2004-08-21 20:58:19 +0000628 double(bca.byteSize));
Reid Spencerf41aa732004-06-29 23:23:12 +0000629 print(Out, "Number Of Bytecode Blocks", bca.numBlocks);
Reid Spencer911ec6d2004-08-21 20:58:19 +0000630 print(Out, "Number Of Functions", bca.numFunctions);
Reid Spencerf41aa732004-06-29 23:23:12 +0000631 print(Out, "Number Of Types", bca.numTypes);
Reid Spencerf41aa732004-06-29 23:23:12 +0000632 print(Out, "Number Of Constants", bca.numConstants);
633 print(Out, "Number Of Global Variables", bca.numGlobalVars);
Reid Spencer911ec6d2004-08-21 20:58:19 +0000634 print(Out, "Number Of Values", bca.numValues);
Reid Spencerf41aa732004-06-29 23:23:12 +0000635 print(Out, "Number Of Basic Blocks", bca.numBasicBlocks);
636 print(Out, "Number Of Instructions", bca.numInstructions);
Reid Spencer911ec6d2004-08-21 20:58:19 +0000637 print(Out, "Number Of Long Instructions", bca.longInstructions);
Reid Spencerf41aa732004-06-29 23:23:12 +0000638 print(Out, "Number Of Operands", bca.numOperands);
639 print(Out, "Number Of Compaction Tables", bca.numCmpctnTables);
640 print(Out, "Number Of Symbol Tables", bca.numSymTab);
Reid Spencer911ec6d2004-08-21 20:58:19 +0000641 print(Out, "Number Of Dependent Libs", bca.numLibraries);
642 print(Out, "Total Instruction Size", bca.instructionSize);
Misha Brukman8a96c532005-04-21 21:44:41 +0000643 print(Out, "Average Instruction Size",
Reid Spencer911ec6d2004-08-21 20:58:19 +0000644 double(bca.instructionSize)/double(bca.numInstructions));
645
Reid Spencerf41aa732004-06-29 23:23:12 +0000646 print(Out, "Maximum Type Slot Number", bca.maxTypeSlot);
647 print(Out, "Maximum Value Slot Number", bca.maxValueSlot);
Reid Spencer3120e712004-08-24 22:45:32 +0000648 print(Out, "Bytes Per Value ", bca.fileDensity);
649 print(Out, "Bytes Per Global", bca.globalsDensity);
650 print(Out, "Bytes Per Function", bca.functionDensity);
651 print(Out, "# of VBR 32-bit Integers", bca.vbrCount32);
652 print(Out, "# of VBR 64-bit Integers", bca.vbrCount64);
653 print(Out, "# of VBR Compressed Bytes", bca.vbrCompBytes);
654 print(Out, "# of VBR Expanded Bytes", bca.vbrExpdBytes);
Misha Brukman8a96c532005-04-21 21:44:41 +0000655 print(Out, "Bytes Saved With VBR",
Reid Spencer911ec6d2004-08-21 20:58:19 +0000656 double(bca.vbrExpdBytes)-double(bca.vbrCompBytes),
657 double(bca.vbrExpdBytes));
Reid Spencerf41aa732004-06-29 23:23:12 +0000658
Reid Spencer911ec6d2004-08-21 20:58:19 +0000659 if (bca.detailedResults) {
660 Out << "\nDetailed Analysis Of " << bca.ModuleId << " Functions:\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000661
Misha Brukman8a96c532005-04-21 21:44:41 +0000662 std::map<const Function*,BytecodeAnalysis::BytecodeFunctionInfo>::iterator I =
Reid Spencerf41aa732004-06-29 23:23:12 +0000663 bca.FunctionInfo.begin();
Misha Brukman8a96c532005-04-21 21:44:41 +0000664 std::map<const Function*,BytecodeAnalysis::BytecodeFunctionInfo>::iterator E =
Reid Spencerf41aa732004-06-29 23:23:12 +0000665 bca.FunctionInfo.end();
666
667 while ( I != E ) {
Chris Lattner4a8167f2004-10-15 19:40:31 +0000668 Out << std::left << std::setw(0) << "\n";
669 if (I->second.numBasicBlocks == 0) Out << "External ";
670 Out << "Function: " << I->second.name << "\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000671 print(Out, "Type:", I->second.description);
672 print(Out, "Byte Size", I->second.byteSize);
Chris Lattner4a8167f2004-10-15 19:40:31 +0000673 if (I->second.numBasicBlocks) {
674 print(Out, "Basic Blocks", I->second.numBasicBlocks);
675 print(Out, "Instructions", I->second.numInstructions);
676 print(Out, "Long Instructions", I->second.longInstructions);
677 print(Out, "Operands", I->second.numOperands);
678 print(Out, "Instruction Size", I->second.instructionSize);
Misha Brukman8a96c532005-04-21 21:44:41 +0000679 print(Out, "Average Instruction Size",
Chris Lattner4a8167f2004-10-15 19:40:31 +0000680 double(I->second.instructionSize) / I->second.numInstructions);
681 print(Out, "Bytes Per Instruction", I->second.density);
682 print(Out, "# of VBR 32-bit Integers", I->second.vbrCount32);
683 print(Out, "# of VBR 64-bit Integers", I->second.vbrCount64);
684 print(Out, "# of VBR Compressed Bytes", I->second.vbrCompBytes);
685 print(Out, "# of VBR Expanded Bytes", I->second.vbrExpdBytes);
Misha Brukman8a96c532005-04-21 21:44:41 +0000686 print(Out, "Bytes Saved With VBR",
Reid Spencere03f09c2006-11-03 01:44:51 +0000687 double(I->second.vbrExpdBytes) - I->second.vbrCompBytes);
Chris Lattner4a8167f2004-10-15 19:40:31 +0000688 }
Reid Spencerf41aa732004-06-29 23:23:12 +0000689 ++I;
690 }
691 }
692
Reid Spencerf41aa732004-06-29 23:23:12 +0000693 if ( bca.progressiveVerify )
694 Out << bca.VerifyInfo;
695}
696
Reid Spencer911ec6d2004-08-21 20:58:19 +0000697BytecodeHandler* createBytecodeAnalyzerHandler(BytecodeAnalysis& bca,
698 std::ostream* output)
Reid Spencerf41aa732004-06-29 23:23:12 +0000699{
Reid Spencer911ec6d2004-08-21 20:58:19 +0000700 return new AnalyzerHandler(bca,output);
Reid Spencerf41aa732004-06-29 23:23:12 +0000701}
702
Reid Spencerdac69c82004-06-07 17:53:43 +0000703}
704