blob: 68d0a9295745678e75187c970df90c765e239e6d [file] [log] [blame]
Chris Lattner4f9ff5a2007-02-07 07:19:19 +00001//===-- Analyzer.cpp - Analysis and Dumping of Bytecode ---------*- 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 Spencer649ee572004-06-09 06:16:43 +000089 bca.FunctionInfo.clear();
Reid Spencer911ec6d2004-08-21 20:58:19 +000090 bca.BlockSizes[BytecodeFormat::Reserved_DoNotUse] = 0;
91 bca.BlockSizes[BytecodeFormat::ModuleBlockID] = theSize;
92 bca.BlockSizes[BytecodeFormat::FunctionBlockID] = 0;
93 bca.BlockSizes[BytecodeFormat::ConstantPoolBlockID] = 0;
Reid Spencer78d033e2007-01-06 07:24:44 +000094 bca.BlockSizes[BytecodeFormat::ValueSymbolTableBlockID] = 0;
Reid Spencer911ec6d2004-08-21 20:58:19 +000095 bca.BlockSizes[BytecodeFormat::ModuleGlobalInfoBlockID] = 0;
96 bca.BlockSizes[BytecodeFormat::GlobalTypePlaneBlockID] = 0;
97 bca.BlockSizes[BytecodeFormat::InstructionListBlockID] = 0;
Reid Spencer78d033e2007-01-06 07:24:44 +000098 bca.BlockSizes[BytecodeFormat::TypeSymbolTableBlockID] = 0;
Reid Spencerdac69c82004-06-07 17:53:43 +000099 }
100
Reid Spencercbb22e22004-06-10 22:00:54 +0000101 virtual void handleFinish() {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000102 if (os)
Misha Brukman8a96c532005-04-21 21:44:41 +0000103 *os << "} End Bytecode\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000104
Reid Spencer00c28a72004-06-10 08:09:13 +0000105 bca.fileDensity = double(bca.byteSize) / double( bca.numTypes + bca.numValues );
106 double globalSize = 0.0;
Reid Spencer911ec6d2004-08-21 20:58:19 +0000107 globalSize += double(bca.BlockSizes[BytecodeFormat::ConstantPoolBlockID]);
108 globalSize += double(bca.BlockSizes[BytecodeFormat::ModuleGlobalInfoBlockID]);
109 globalSize += double(bca.BlockSizes[BytecodeFormat::GlobalTypePlaneBlockID]);
Misha Brukman8a96c532005-04-21 21:44:41 +0000110 bca.globalsDensity = globalSize / double( bca.numTypes + bca.numConstants +
Reid Spencer00c28a72004-06-10 08:09:13 +0000111 bca.numGlobalVars );
Misha Brukman8a96c532005-04-21 21:44:41 +0000112 bca.functionDensity = double(bca.BlockSizes[BytecodeFormat::FunctionBlockID]) /
Reid Spencer00c28a72004-06-10 08:09:13 +0000113 double(bca.numFunctions);
Reid Spencerf41aa732004-06-29 23:23:12 +0000114
Chris Lattner05ac92c2006-07-06 18:02:27 +0000115 if (bca.progressiveVerify) {
116 std::string msg;
117 if (verifyModule(*M, ReturnStatusAction, &msg))
Reid Spencerb61cdb72004-07-04 11:00:39 +0000118 bca.VerifyInfo += "Verify@Finish: " + msg + "\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000119 }
Reid Spencerdac69c82004-06-07 17:53:43 +0000120 }
121
Reid Spencercbb22e22004-06-10 22:00:54 +0000122 virtual void handleModuleBegin(const std::string& id) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000123 if (os)
124 *os << " Module " << id << " {\n";
Reid Spencer649ee572004-06-09 06:16:43 +0000125 bca.ModuleId = id;
Reid Spencerdac69c82004-06-07 17:53:43 +0000126 }
127
Misha Brukman8a96c532005-04-21 21:44:41 +0000128 virtual void handleModuleEnd(const std::string& id) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000129 if (os)
130 *os << " } End Module " << id << "\n";
Chris Lattner05ac92c2006-07-06 18:02:27 +0000131 if (bca.progressiveVerify) {
132 std::string msg;
133 if (verifyModule(*M, ReturnStatusAction, &msg))
Reid Spencerb61cdb72004-07-04 11:00:39 +0000134 bca.VerifyInfo += "Verify@EndModule: " + msg + "\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000135 }
136 }
Reid Spencerdac69c82004-06-07 17:53:43 +0000137
Reid Spencercbb22e22004-06-10 22:00:54 +0000138 virtual void handleVersionInfo(
Reid Spenceraacc35a2007-01-26 08:10:24 +0000139 unsigned char RevisionNum ///< Byte code revision number
Misha Brukman8a96c532005-04-21 21:44:41 +0000140 ) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000141 if (os)
Reid Spenceraacc35a2007-01-26 08:10:24 +0000142 *os << " RevisionNum: " << int(RevisionNum) << "\n";
Reid Spencer911ec6d2004-08-21 20:58:19 +0000143 bca.version = RevisionNum;
Reid Spencerf41aa732004-06-29 23:23:12 +0000144 }
Reid Spencerdac69c82004-06-07 17:53:43 +0000145
Misha Brukman8a96c532005-04-21 21:44:41 +0000146 virtual void handleModuleGlobalsBegin() {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000147 if (os)
148 *os << " BLOCK: ModuleGlobalInfo {\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000149 }
Reid Spencerdac69c82004-06-07 17:53:43 +0000150
Misha Brukman8a96c532005-04-21 21:44:41 +0000151 virtual void handleGlobalVariable(
152 const Type* ElemType,
153 bool isConstant,
Reid Spencerf41aa732004-06-29 23:23:12 +0000154 GlobalValue::LinkageTypes Linkage,
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000155 GlobalValue::VisibilityTypes Visibility,
Reid Spencerf41aa732004-06-29 23:23:12 +0000156 unsigned SlotNum,
157 unsigned initSlot
Reid Spencercbb22e22004-06-10 22:00:54 +0000158 ) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000159 if (os) {
160 *os << " GV: "
161 << ( initSlot == 0 ? "Uni" : "I" ) << "nitialized, "
162 << ( isConstant? "Constant, " : "Variable, ")
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000163 << " Linkage=" << Linkage
164 << " Visibility="<< Visibility
165 << " Type=";
Reid Spencer911ec6d2004-08-21 20:58:19 +0000166 WriteTypeSymbolic(*os, ElemType, M);
Misha Brukman8a96c532005-04-21 21:44:41 +0000167 *os << " Slot=" << SlotNum << " InitSlot=" << initSlot
Reid Spencer911ec6d2004-08-21 20:58:19 +0000168 << "\n";
169 }
170
Reid Spencer649ee572004-06-09 06:16:43 +0000171 bca.numGlobalVars++;
Reid Spencer00c28a72004-06-10 08:09:13 +0000172 bca.numValues++;
Reid Spencer911ec6d2004-08-21 20:58:19 +0000173 if (SlotNum > bca.maxValueSlot)
174 bca.maxValueSlot = SlotNum;
175 if (initSlot > bca.maxValueSlot)
176 bca.maxValueSlot = initSlot;
Reid Spencerf41aa732004-06-29 23:23:12 +0000177
Reid Spencer911ec6d2004-08-21 20:58:19 +0000178 }
179
180 virtual void handleTypeList(unsigned numEntries) {
181 bca.maxTypeSlot = numEntries - 1;
Reid Spencerdac69c82004-06-07 17:53:43 +0000182 }
183
Misha Brukman8a96c532005-04-21 21:44:41 +0000184 virtual void handleType( const Type* Ty ) {
185 bca.numTypes++;
Reid Spencer911ec6d2004-08-21 20:58:19 +0000186 if (os) {
187 *os << " Type: ";
188 WriteTypeSymbolic(*os,Ty,M);
189 *os << "\n";
190 }
Reid Spencerdac69c82004-06-07 17:53:43 +0000191 }
192
Misha Brukman8a96c532005-04-21 21:44:41 +0000193 virtual void handleFunctionDeclaration(
Reid Spencerb61cdb72004-07-04 11:00:39 +0000194 Function* Func ///< The function
Reid Spencercbb22e22004-06-10 22:00:54 +0000195 ) {
Reid Spencer649ee572004-06-09 06:16:43 +0000196 bca.numFunctions++;
Reid Spencer00c28a72004-06-10 08:09:13 +0000197 bca.numValues++;
Reid Spencer911ec6d2004-08-21 20:58:19 +0000198 if (os) {
199 *os << " Function Decl: ";
200 WriteTypeSymbolic(*os,Func->getType(),M);
Anton Korobeynikov93c2b372006-09-17 13:06:18 +0000201 *os <<", Linkage=" << Func->getLinkage();
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000202 *os <<", Visibility=" << Func->getVisibility();
Reid Spencer911ec6d2004-08-21 20:58:19 +0000203 *os << "\n";
204 }
Reid Spencerdac69c82004-06-07 17:53:43 +0000205 }
206
Reid Spencerf41aa732004-06-29 23:23:12 +0000207 virtual void handleGlobalInitializer(GlobalVariable* GV, Constant* CV) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000208 if (os) {
209 *os << " Initializer: GV=";
210 GV->print(*os);
211 *os << " CV=";
212 CV->print(*os);
213 *os << "\n";
214 }
215 }
216
217 virtual void handleDependentLibrary(const std::string& libName) {
218 bca.numLibraries++;
219 bca.libSize += libName.size() + (libName.size() < 128 ? 1 : 2);
Reid Spencer3ee8eed2004-09-11 04:14:07 +0000220 if (os)
221 *os << " Library: '" << libName << "'\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000222 }
Reid Spencerdac69c82004-06-07 17:53:43 +0000223
Misha Brukman8a96c532005-04-21 21:44:41 +0000224 virtual void handleModuleGlobalsEnd() {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000225 if (os)
226 *os << " } END BLOCK: ModuleGlobalInfo\n";
Chris Lattner05ac92c2006-07-06 18:02:27 +0000227 if (bca.progressiveVerify) {
228 std::string msg;
229 if (verifyModule(*M, ReturnStatusAction, &msg))
Reid Spencerb61cdb72004-07-04 11:00:39 +0000230 bca.VerifyInfo += "Verify@EndModuleGlobalInfo: " + msg + "\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000231 }
232 }
233
Misha Brukman8a96c532005-04-21 21:44:41 +0000234 virtual void handleCompactionTableBegin() {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000235 if (os)
236 *os << " BLOCK: CompactionTable {\n";
Reid Spencer488d73a2004-08-27 00:43:51 +0000237 bca.numCmpctnTables++;
Reid Spencerf41aa732004-06-29 23:23:12 +0000238 }
Reid Spencerdac69c82004-06-07 17:53:43 +0000239
Reid Spencercbb22e22004-06-10 22:00:54 +0000240 virtual void handleCompactionTablePlane( unsigned Ty, unsigned NumEntries) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000241 if (os)
242 *os << " Plane: Ty=" << Ty << " Size=" << NumEntries << "\n";
Reid Spencerdac69c82004-06-07 17:53:43 +0000243 }
244
Misha Brukman8a96c532005-04-21 21:44:41 +0000245 virtual void handleCompactionTableType( unsigned i, unsigned TypSlot,
Reid Spencerf41aa732004-06-29 23:23:12 +0000246 const Type* Ty ) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000247 if (os) {
248 *os << " Type: " << i << " Slot:" << TypSlot << " is ";
249 WriteTypeSymbolic(*os,Ty,M);
Misha Brukman8a96c532005-04-21 21:44:41 +0000250 *os << "\n";
Reid Spencer911ec6d2004-08-21 20:58:19 +0000251 }
Reid Spencerf41aa732004-06-29 23:23:12 +0000252 }
Reid Spencerdac69c82004-06-07 17:53:43 +0000253
Chris Lattner2c6c14d2004-08-04 00:19:23 +0000254 virtual void handleCompactionTableValue(unsigned i, unsigned TypSlot,
Misha Brukman8a96c532005-04-21 21:44:41 +0000255 unsigned ValSlot) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000256 if (os)
Misha Brukman8a96c532005-04-21 21:44:41 +0000257 *os << " Value: " << i << " TypSlot: " << TypSlot
Chris Lattner2c6c14d2004-08-04 00:19:23 +0000258 << " ValSlot:" << ValSlot << "\n";
Reid Spencer911ec6d2004-08-21 20:58:19 +0000259 if (ValSlot > bca.maxValueSlot)
260 bca.maxValueSlot = ValSlot;
Reid Spencerf41aa732004-06-29 23:23:12 +0000261 }
Reid Spencercbb22e22004-06-10 22:00:54 +0000262
Misha Brukman8a96c532005-04-21 21:44:41 +0000263 virtual void handleCompactionTableEnd() {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000264 if (os)
265 *os << " } END BLOCK: CompactionTable\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000266 }
Reid Spencercbb22e22004-06-10 22:00:54 +0000267
Reid Spenceref9b9a72007-02-05 20:47:22 +0000268 virtual void handleTypeSymbolTableBegin(TypeSymbolTable* ST) {
Misha Brukman8a96c532005-04-21 21:44:41 +0000269 bca.numSymTab++;
Reid Spencer911ec6d2004-08-21 20:58:19 +0000270 if (os)
Reid Spenceref9b9a72007-02-05 20:47:22 +0000271 *os << " BLOCK: TypeSymbolTable {\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000272 }
Reid Spenceref9b9a72007-02-05 20:47:22 +0000273 virtual void handleValueSymbolTableBegin(Function* CF, ValueSymbolTable* ST) {
274 bca.numSymTab++;
275 if (os)
276 *os << " BLOCK: ValueSymbolTable {\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000277 }
Reid Spencercbb22e22004-06-10 22:00:54 +0000278
Misha Brukman8a96c532005-04-21 21:44:41 +0000279 virtual void handleSymbolTableType(unsigned i, unsigned TypSlot,
280 const std::string& name ) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000281 if (os)
282 *os << " Type " << i << " Slot=" << TypSlot
Misha Brukman8a96c532005-04-21 21:44:41 +0000283 << " Name: " << name << "\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000284 }
Reid Spencercbb22e22004-06-10 22:00:54 +0000285
Reid Spenceref9b9a72007-02-05 20:47:22 +0000286 virtual void handleSymbolTableValue(unsigned TySlot, unsigned ValSlot,
287 const std::string& name) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000288 if (os)
Reid Spenceref9b9a72007-02-05 20:47:22 +0000289 *os << " Value " << TySlot << " Slot=" << ValSlot
Reid Spencer911ec6d2004-08-21 20:58:19 +0000290 << " Name: " << name << "\n";
291 if (ValSlot > bca.maxValueSlot)
292 bca.maxValueSlot = ValSlot;
Reid Spencerf41aa732004-06-29 23:23:12 +0000293 }
Reid Spencercbb22e22004-06-10 22:00:54 +0000294
Reid Spenceref9b9a72007-02-05 20:47:22 +0000295 virtual void handleValueSymbolTableEnd() {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000296 if (os)
Reid Spenceref9b9a72007-02-05 20:47:22 +0000297 *os << " } END BLOCK: ValueSymbolTable\n";
298 }
299
300 virtual void handleTypeSymbolTableEnd() {
301 if (os)
302 *os << " } END BLOCK: TypeSymbolTable\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000303 }
Reid Spencercbb22e22004-06-10 22:00:54 +0000304
Reid Spencerf41aa732004-06-29 23:23:12 +0000305 virtual void handleFunctionBegin(Function* Func, unsigned Size) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000306 if (os) {
307 *os << " BLOCK: Function {\n"
308 << " Linkage: " << Func->getLinkage() << "\n"
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000309 << " Visibility: " << Func->getVisibility() << "\n"
Misha Brukman8a96c532005-04-21 21:44:41 +0000310 << " Type: ";
Reid Spencer911ec6d2004-08-21 20:58:19 +0000311 WriteTypeSymbolic(*os,Func->getType(),M);
312 *os << "\n";
313 }
314
Reid Spencercbb22e22004-06-10 22:00:54 +0000315 currFunc = &bca.FunctionInfo[Func];
Reid Spencer911ec6d2004-08-21 20:58:19 +0000316 std::ostringstream tmp;
317 WriteTypeSymbolic(tmp,Func->getType(),M);
318 currFunc->description = tmp.str();
Reid Spencercbb22e22004-06-10 22:00:54 +0000319 currFunc->name = Func->getName();
320 currFunc->byteSize = Size;
321 currFunc->numInstructions = 0;
322 currFunc->numBasicBlocks = 0;
323 currFunc->numPhis = 0;
324 currFunc->numOperands = 0;
325 currFunc->density = 0.0;
Reid Spencer1cf50242004-06-11 15:10:38 +0000326 currFunc->instructionSize = 0;
327 currFunc->longInstructions = 0;
Reid Spencerf41aa732004-06-29 23:23:12 +0000328
Reid Spencerdac69c82004-06-07 17:53:43 +0000329 }
330
Reid Spencercbb22e22004-06-10 22:00:54 +0000331 virtual void handleFunctionEnd( Function* Func) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000332 if (os)
333 *os << " } END BLOCK: Function\n";
Reid Spencercbb22e22004-06-10 22:00:54 +0000334 currFunc->density = double(currFunc->byteSize) /
Reid Spencer3120e712004-08-24 22:45:32 +0000335 double(currFunc->numInstructions);
Reid Spencerf41aa732004-06-29 23:23:12 +0000336
Chris Lattner05ac92c2006-07-06 18:02:27 +0000337 if (bca.progressiveVerify) {
338 std::string msg;
339 if (verifyModule(*M, ReturnStatusAction, &msg))
Reid Spencerb61cdb72004-07-04 11:00:39 +0000340 bca.VerifyInfo += "Verify@EndFunction: " + msg + "\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000341 }
Reid Spencerdac69c82004-06-07 17:53:43 +0000342 }
343
Reid Spencercbb22e22004-06-10 22:00:54 +0000344 virtual void handleBasicBlockBegin( unsigned blocknum) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000345 if (os)
346 *os << " BLOCK: BasicBlock #" << blocknum << "{\n";
Reid Spencer649ee572004-06-09 06:16:43 +0000347 bca.numBasicBlocks++;
Reid Spencer00c28a72004-06-10 08:09:13 +0000348 bca.numValues++;
Reid Spencercbb22e22004-06-10 22:00:54 +0000349 if ( currFunc ) currFunc->numBasicBlocks++;
Reid Spencerdac69c82004-06-07 17:53:43 +0000350 }
351
Misha Brukman8a96c532005-04-21 21:44:41 +0000352 virtual bool handleInstruction( unsigned Opcode, const Type* iType,
Chris Lattner63cf59e2007-02-07 05:08:39 +0000353 unsigned *Operands, unsigned NumOps,
Reid Spenceref9b9a72007-02-05 20:47:22 +0000354 Instruction *Inst,
355 unsigned Size){
Reid Spencer911ec6d2004-08-21 20:58:19 +0000356 if (os) {
Misha Brukman8a96c532005-04-21 21:44:41 +0000357 *os << " INST: OpCode="
Reid Spenceref9b9a72007-02-05 20:47:22 +0000358 << Instruction::getOpcodeName(Opcode);
Chris Lattner63cf59e2007-02-07 05:08:39 +0000359 for (unsigned i = 0; i != NumOps; ++i)
Reid Spenceref9b9a72007-02-05 20:47:22 +0000360 *os << " Op(" << Operands[i] << ")";
361 *os << *Inst;
Reid Spencer911ec6d2004-08-21 20:58:19 +0000362 }
Reid Spencerf41aa732004-06-29 23:23:12 +0000363
Reid Spencer649ee572004-06-09 06:16:43 +0000364 bca.numInstructions++;
Reid Spencer00c28a72004-06-10 08:09:13 +0000365 bca.numValues++;
Reid Spencer1cf50242004-06-11 15:10:38 +0000366 bca.instructionSize += Size;
367 if (Size > 4 ) bca.longInstructions++;
Chris Lattner63cf59e2007-02-07 05:08:39 +0000368 bca.numOperands += NumOps;
369 for (unsigned i = 0; i != NumOps; ++i)
Reid Spencer911ec6d2004-08-21 20:58:19 +0000370 if (Operands[i] > bca.maxValueSlot)
371 bca.maxValueSlot = Operands[i];
Reid Spencercbb22e22004-06-10 22:00:54 +0000372 if ( currFunc ) {
373 currFunc->numInstructions++;
Reid Spencer1cf50242004-06-11 15:10:38 +0000374 currFunc->instructionSize += Size;
375 if (Size > 4 ) currFunc->longInstructions++;
Chris Lattner63cf59e2007-02-07 05:08:39 +0000376 if (Opcode == Instruction::PHI) currFunc->numPhis++;
Reid Spencercbb22e22004-06-10 22:00:54 +0000377 }
Misha Brukman8a96c532005-04-21 21:44:41 +0000378 return Instruction::isTerminator(Opcode);
Reid Spencerdac69c82004-06-07 17:53:43 +0000379 }
380
Misha Brukman8a96c532005-04-21 21:44:41 +0000381 virtual void handleBasicBlockEnd(unsigned blocknum) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000382 if (os)
Reid Spencerc6d416a2006-12-15 21:46:37 +0000383 *os << " } END BLOCK: BasicBlock #" << blocknum << "\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000384 }
Reid Spencerdac69c82004-06-07 17:53:43 +0000385
Misha Brukman8a96c532005-04-21 21:44:41 +0000386 virtual void handleGlobalConstantsBegin() {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000387 if (os)
388 *os << " BLOCK: GlobalConstants {\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000389 }
Reid Spencerdac69c82004-06-07 17:53:43 +0000390
Chris Lattner63cf59e2007-02-07 05:08:39 +0000391 virtual void handleConstantExpression(unsigned Opcode,
392 Constant**ArgVec, unsigned NumArgs, Constant* C) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000393 if (os) {
394 *os << " EXPR: " << Instruction::getOpcodeName(Opcode) << "\n";
Chris Lattner63cf59e2007-02-07 05:08:39 +0000395 for ( unsigned i = 0; i != NumArgs; ++i ) {
Misha Brukman8a96c532005-04-21 21:44:41 +0000396 *os << " Arg#" << i << " "; ArgVec[i]->print(*os);
Reid Spencer911ec6d2004-08-21 20:58:19 +0000397 *os << "\n";
398 }
399 *os << " Value=";
400 C->print(*os);
401 *os << "\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000402 }
Reid Spencer649ee572004-06-09 06:16:43 +0000403 bca.numConstants++;
Reid Spencer00c28a72004-06-10 08:09:13 +0000404 bca.numValues++;
Reid Spencerdac69c82004-06-07 17:53:43 +0000405 }
406
Reid Spencercbb22e22004-06-10 22:00:54 +0000407 virtual void handleConstantValue( Constant * c ) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000408 if (os) {
409 *os << " VALUE: ";
410 c->print(*os);
411 *os << "\n";
412 }
Reid Spencer649ee572004-06-09 06:16:43 +0000413 bca.numConstants++;
Reid Spencer00c28a72004-06-10 08:09:13 +0000414 bca.numValues++;
Reid Spencerdac69c82004-06-07 17:53:43 +0000415 }
416
Misha Brukman8a96c532005-04-21 21:44:41 +0000417 virtual void handleConstantArray( const ArrayType* AT,
Chris Lattner63cf59e2007-02-07 05:08:39 +0000418 Constant**Elements, unsigned NumElts,
Reid Spencerb61cdb72004-07-04 11:00:39 +0000419 unsigned TypeSlot,
420 Constant* ArrayVal ) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000421 if (os) {
422 *os << " ARRAY: ";
Misha Brukman8a96c532005-04-21 21:44:41 +0000423 WriteTypeSymbolic(*os,AT,M);
Reid Spencer911ec6d2004-08-21 20:58:19 +0000424 *os << " TypeSlot=" << TypeSlot << "\n";
Chris Lattner63cf59e2007-02-07 05:08:39 +0000425 for (unsigned i = 0; i != NumElts; ++i) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000426 *os << " #" << i;
427 Elements[i]->print(*os);
428 *os << "\n";
429 }
430 *os << " Value=";
431 ArrayVal->print(*os);
432 *os << "\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000433 }
Reid Spencerf41aa732004-06-29 23:23:12 +0000434
Reid Spencer649ee572004-06-09 06:16:43 +0000435 bca.numConstants++;
Reid Spencer00c28a72004-06-10 08:09:13 +0000436 bca.numValues++;
Reid Spencerdac69c82004-06-07 17:53:43 +0000437 }
438
Reid Spencercbb22e22004-06-10 22:00:54 +0000439 virtual void handleConstantStruct(
Reid Spencer00c28a72004-06-10 08:09:13 +0000440 const StructType* ST,
Chris Lattner63cf59e2007-02-07 05:08:39 +0000441 Constant**Elements, unsigned NumElts,
Reid Spencerb61cdb72004-07-04 11:00:39 +0000442 Constant* StructVal)
Reid Spencerdac69c82004-06-07 17:53:43 +0000443 {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000444 if (os) {
445 *os << " STRUC: ";
446 WriteTypeSymbolic(*os,ST,M);
447 *os << "\n";
Chris Lattner63cf59e2007-02-07 05:08:39 +0000448 for ( unsigned i = 0; i != NumElts; ++i) {
Misha Brukman8a96c532005-04-21 21:44:41 +0000449 *os << " #" << i << " "; Elements[i]->print(*os);
Reid Spencer911ec6d2004-08-21 20:58:19 +0000450 *os << "\n";
451 }
452 *os << " Value=";
453 StructVal->print(*os);
454 *os << "\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000455 }
Reid Spencer649ee572004-06-09 06:16:43 +0000456 bca.numConstants++;
Reid Spencer00c28a72004-06-10 08:09:13 +0000457 bca.numValues++;
Reid Spencerdac69c82004-06-07 17:53:43 +0000458 }
459
Misha Brukman8a96c532005-04-21 21:44:41 +0000460 virtual void handleConstantPacked(
461 const PackedType* PT,
Chris Lattner63cf59e2007-02-07 05:08:39 +0000462 Constant**Elements, unsigned NumElts,
Misha Brukman8a96c532005-04-21 21:44:41 +0000463 unsigned TypeSlot,
464 Constant* PackedVal)
Brian Gaeke715c90b2004-08-20 06:00:58 +0000465 {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000466 if (os) {
467 *os << " PACKD: ";
468 WriteTypeSymbolic(*os,PT,M);
469 *os << " TypeSlot=" << TypeSlot << "\n";
Chris Lattner63cf59e2007-02-07 05:08:39 +0000470 for ( unsigned i = 0; i != NumElts; ++i ) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000471 *os << " #" << i;
472 Elements[i]->print(*os);
473 *os << "\n";
474 }
475 *os << " Value=";
476 PackedVal->print(*os);
477 *os << "\n";
Brian Gaeke715c90b2004-08-20 06:00:58 +0000478 }
Brian Gaeke715c90b2004-08-20 06:00:58 +0000479
480 bca.numConstants++;
481 bca.numValues++;
482 }
483
Misha Brukman8a96c532005-04-21 21:44:41 +0000484 virtual void handleConstantPointer( const PointerType* PT,
Reid Spencer3c90f9f2004-07-18 00:10:36 +0000485 unsigned Slot, GlobalValue* GV ) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000486 if (os) {
487 *os << " PNTR: ";
488 WriteTypeSymbolic(*os,PT,M);
489 *os << " Slot=" << Slot << " GlobalValue=";
490 GV->print(*os);
491 *os << "\n";
492 }
Reid Spencer649ee572004-06-09 06:16:43 +0000493 bca.numConstants++;
Reid Spencer00c28a72004-06-10 08:09:13 +0000494 bca.numValues++;
Reid Spencerdac69c82004-06-07 17:53:43 +0000495 }
496
Reid Spencercbb22e22004-06-10 22:00:54 +0000497 virtual void handleConstantString( const ConstantArray* CA ) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000498 if (os) {
499 *os << " STRNG: ";
Misha Brukman8a96c532005-04-21 21:44:41 +0000500 CA->print(*os);
Reid Spencer911ec6d2004-08-21 20:58:19 +0000501 *os << "\n";
502 }
Reid Spencer649ee572004-06-09 06:16:43 +0000503 bca.numConstants++;
Reid Spencer00c28a72004-06-10 08:09:13 +0000504 bca.numValues++;
Reid Spencerdac69c82004-06-07 17:53:43 +0000505 }
506
Misha Brukman8a96c532005-04-21 21:44:41 +0000507 virtual void handleGlobalConstantsEnd() {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000508 if (os)
509 *os << " } END BLOCK: GlobalConstants\n";
510
Chris Lattner05ac92c2006-07-06 18:02:27 +0000511 if (bca.progressiveVerify) {
512 std::string msg;
513 if (verifyModule(*M, ReturnStatusAction, &msg))
Reid Spencerb61cdb72004-07-04 11:00:39 +0000514 bca.VerifyInfo += "Verify@EndGlobalConstants: " + msg + "\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000515 }
516 }
Reid Spencerdac69c82004-06-07 17:53:43 +0000517
Reid Spencercbb22e22004-06-10 22:00:54 +0000518 virtual void handleAlignment(unsigned numBytes) {
Reid Spencer00c28a72004-06-10 08:09:13 +0000519 bca.numAlignment += numBytes;
Reid Spencerdac69c82004-06-07 17:53:43 +0000520 }
521
Reid Spencercbb22e22004-06-10 22:00:54 +0000522 virtual void handleBlock(
Reid Spencer00c28a72004-06-10 08:09:13 +0000523 unsigned BType, const unsigned char* StartPtr, unsigned Size) {
524 bca.numBlocks++;
Reid Spencer911ec6d2004-08-21 20:58:19 +0000525 assert(BType >= BytecodeFormat::ModuleBlockID);
526 assert(BType < BytecodeFormat::NumberOfBlockIDs);
527 bca.BlockSizes[
Reid Spencerd798a512006-11-14 04:47:22 +0000528 llvm::BytecodeFormat::BytecodeBlockIdentifiers(BType)] += Size;
Reid Spencer911ec6d2004-08-21 20:58:19 +0000529
530 if (bca.version < 3) // Check for long block headers versions
531 bca.BlockSizes[llvm::BytecodeFormat::Reserved_DoNotUse] += 8;
532 else
533 bca.BlockSizes[llvm::BytecodeFormat::Reserved_DoNotUse] += 4;
Reid Spencer00c28a72004-06-10 08:09:13 +0000534 }
535
Reid Spencerdac69c82004-06-07 17:53:43 +0000536};
Chris Lattnerc6d0b162007-02-07 23:46:55 +0000537} // end anonymous namespace
Reid Spencerf41aa732004-06-29 23:23:12 +0000538
539/// @brief Utility for printing a titled unsigned value with
540/// an aligned colon.
Misha Brukman8a96c532005-04-21 21:44:41 +0000541inline static void print(std::ostream& Out, const char*title,
Reid Spencerf41aa732004-06-29 23:23:12 +0000542 unsigned val, bool nl = true ) {
Misha Brukman8a96c532005-04-21 21:44:41 +0000543 Out << std::setw(30) << std::right << title
Reid Spencerf41aa732004-06-29 23:23:12 +0000544 << std::setw(0) << ": "
545 << std::setw(9) << val << "\n";
Reid Spencerdac69c82004-06-07 17:53:43 +0000546}
547
Reid Spencerf41aa732004-06-29 23:23:12 +0000548/// @brief Utility for printing a titled double value with an
549/// aligned colon
Misha Brukman8a96c532005-04-21 21:44:41 +0000550inline static void print(std::ostream&Out, const char*title,
Reid Spencerf41aa732004-06-29 23:23:12 +0000551 double val ) {
Misha Brukman8a96c532005-04-21 21:44:41 +0000552 Out << std::setw(30) << std::right << title
Reid Spencerf41aa732004-06-29 23:23:12 +0000553 << std::setw(0) << ": "
554 << std::setw(9) << std::setprecision(6) << val << "\n" ;
555}
556
557/// @brief Utility for printing a titled double value with a
558/// percentage and aligned colon.
Misha Brukman8a96c532005-04-21 21:44:41 +0000559inline static void print(std::ostream&Out, const char*title,
Reid Spencerf41aa732004-06-29 23:23:12 +0000560 double top, double bot ) {
Misha Brukman8a96c532005-04-21 21:44:41 +0000561 Out << std::setw(30) << std::right << title
Reid Spencerf41aa732004-06-29 23:23:12 +0000562 << std::setw(0) << ": "
Misha Brukman8a96c532005-04-21 21:44:41 +0000563 << std::setw(9) << std::setprecision(6) << top
564 << " (" << std::left << std::setw(0) << std::setprecision(4)
Reid Spencerf41aa732004-06-29 23:23:12 +0000565 << (top/bot)*100.0 << "%)\n";
566}
567
568/// @brief Utility for printing a titled string value with
569/// an aligned colon.
Misha Brukman8a96c532005-04-21 21:44:41 +0000570inline static void print(std::ostream&Out, const char*title,
Reid Spencerf41aa732004-06-29 23:23:12 +0000571 std::string val, bool nl = true) {
Misha Brukman8a96c532005-04-21 21:44:41 +0000572 Out << std::setw(30) << std::right << title
Reid Spencerf41aa732004-06-29 23:23:12 +0000573 << std::setw(0) << ": "
574 << std::left << val << (nl ? "\n" : "");
575}
576
Reid Spencerf41aa732004-06-29 23:23:12 +0000577/// This function prints the contents of rhe BytecodeAnalysis structure in
578/// a human legible form.
579/// @brief Print BytecodeAnalysis structure to an ostream
Chris Lattnerc6d0b162007-02-07 23:46:55 +0000580void llvm::PrintBytecodeAnalysis(BytecodeAnalysis& bca, std::ostream& Out )
Reid Spencerdac69c82004-06-07 17:53:43 +0000581{
Reid Spencer911ec6d2004-08-21 20:58:19 +0000582 Out << "\nSummary Analysis Of " << bca.ModuleId << ": \n\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000583 print(Out, "Bytecode Analysis Of Module", bca.ModuleId);
Reid Spencer911ec6d2004-08-21 20:58:19 +0000584 print(Out, "Bytecode Version Number", bca.version);
Reid Spencerf41aa732004-06-29 23:23:12 +0000585 print(Out, "File Size", bca.byteSize);
Reid Spencer911ec6d2004-08-21 20:58:19 +0000586 print(Out, "Module Bytes",
587 double(bca.BlockSizes[BytecodeFormat::ModuleBlockID]),
588 double(bca.byteSize));
Misha Brukman8a96c532005-04-21 21:44:41 +0000589 print(Out, "Function Bytes",
Reid Spencer911ec6d2004-08-21 20:58:19 +0000590 double(bca.BlockSizes[BytecodeFormat::FunctionBlockID]),
591 double(bca.byteSize));
Misha Brukman8a96c532005-04-21 21:44:41 +0000592 print(Out, "Global Types Bytes",
Reid Spencer911ec6d2004-08-21 20:58:19 +0000593 double(bca.BlockSizes[BytecodeFormat::GlobalTypePlaneBlockID]),
594 double(bca.byteSize));
Misha Brukman8a96c532005-04-21 21:44:41 +0000595 print(Out, "Constant Pool Bytes",
Reid Spencer911ec6d2004-08-21 20:58:19 +0000596 double(bca.BlockSizes[BytecodeFormat::ConstantPoolBlockID]),
597 double(bca.byteSize));
Misha Brukman8a96c532005-04-21 21:44:41 +0000598 print(Out, "Module Globals Bytes",
Reid Spencer911ec6d2004-08-21 20:58:19 +0000599 double(bca.BlockSizes[BytecodeFormat::ModuleGlobalInfoBlockID]),
600 double(bca.byteSize));
Misha Brukman8a96c532005-04-21 21:44:41 +0000601 print(Out, "Instruction List Bytes",
Reid Spencer911ec6d2004-08-21 20:58:19 +0000602 double(bca.BlockSizes[BytecodeFormat::InstructionListBlockID]),
603 double(bca.byteSize));
Reid Spencer78d033e2007-01-06 07:24:44 +0000604 print(Out, "Value Symbol Table Bytes",
605 double(bca.BlockSizes[BytecodeFormat::ValueSymbolTableBlockID]),
606 double(bca.byteSize));
607 print(Out, "Type Symbol Table Bytes",
608 double(bca.BlockSizes[BytecodeFormat::TypeSymbolTableBlockID]),
Reid Spencer911ec6d2004-08-21 20:58:19 +0000609 double(bca.byteSize));
Misha Brukman8a96c532005-04-21 21:44:41 +0000610 print(Out, "Alignment Bytes",
Reid Spencer911ec6d2004-08-21 20:58:19 +0000611 double(bca.numAlignment), double(bca.byteSize));
Misha Brukman8a96c532005-04-21 21:44:41 +0000612 print(Out, "Block Header Bytes",
Reid Spencer911ec6d2004-08-21 20:58:19 +0000613 double(bca.BlockSizes[BytecodeFormat::Reserved_DoNotUse]),
614 double(bca.byteSize));
Misha Brukman8a96c532005-04-21 21:44:41 +0000615 print(Out, "Dependent Libraries Bytes", double(bca.libSize),
Reid Spencer911ec6d2004-08-21 20:58:19 +0000616 double(bca.byteSize));
Reid Spencerf41aa732004-06-29 23:23:12 +0000617 print(Out, "Number Of Bytecode Blocks", bca.numBlocks);
Reid Spencer911ec6d2004-08-21 20:58:19 +0000618 print(Out, "Number Of Functions", bca.numFunctions);
Reid Spencerf41aa732004-06-29 23:23:12 +0000619 print(Out, "Number Of Types", bca.numTypes);
Reid Spencerf41aa732004-06-29 23:23:12 +0000620 print(Out, "Number Of Constants", bca.numConstants);
621 print(Out, "Number Of Global Variables", bca.numGlobalVars);
Reid Spencer911ec6d2004-08-21 20:58:19 +0000622 print(Out, "Number Of Values", bca.numValues);
Reid Spencerf41aa732004-06-29 23:23:12 +0000623 print(Out, "Number Of Basic Blocks", bca.numBasicBlocks);
624 print(Out, "Number Of Instructions", bca.numInstructions);
Reid Spencer911ec6d2004-08-21 20:58:19 +0000625 print(Out, "Number Of Long Instructions", bca.longInstructions);
Reid Spencerf41aa732004-06-29 23:23:12 +0000626 print(Out, "Number Of Operands", bca.numOperands);
627 print(Out, "Number Of Compaction Tables", bca.numCmpctnTables);
628 print(Out, "Number Of Symbol Tables", bca.numSymTab);
Reid Spencer911ec6d2004-08-21 20:58:19 +0000629 print(Out, "Number Of Dependent Libs", bca.numLibraries);
630 print(Out, "Total Instruction Size", bca.instructionSize);
Misha Brukman8a96c532005-04-21 21:44:41 +0000631 print(Out, "Average Instruction Size",
Reid Spencer911ec6d2004-08-21 20:58:19 +0000632 double(bca.instructionSize)/double(bca.numInstructions));
633
Reid Spencerf41aa732004-06-29 23:23:12 +0000634 print(Out, "Maximum Type Slot Number", bca.maxTypeSlot);
635 print(Out, "Maximum Value Slot Number", bca.maxValueSlot);
Reid Spencer3120e712004-08-24 22:45:32 +0000636 print(Out, "Bytes Per Value ", bca.fileDensity);
637 print(Out, "Bytes Per Global", bca.globalsDensity);
638 print(Out, "Bytes Per Function", bca.functionDensity);
Reid Spencerf41aa732004-06-29 23:23:12 +0000639
Reid Spencer911ec6d2004-08-21 20:58:19 +0000640 if (bca.detailedResults) {
641 Out << "\nDetailed Analysis Of " << bca.ModuleId << " Functions:\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000642
Misha Brukman8a96c532005-04-21 21:44:41 +0000643 std::map<const Function*,BytecodeAnalysis::BytecodeFunctionInfo>::iterator I =
Reid Spencerf41aa732004-06-29 23:23:12 +0000644 bca.FunctionInfo.begin();
Misha Brukman8a96c532005-04-21 21:44:41 +0000645 std::map<const Function*,BytecodeAnalysis::BytecodeFunctionInfo>::iterator E =
Reid Spencerf41aa732004-06-29 23:23:12 +0000646 bca.FunctionInfo.end();
647
648 while ( I != E ) {
Chris Lattner4a8167f2004-10-15 19:40:31 +0000649 Out << std::left << std::setw(0) << "\n";
650 if (I->second.numBasicBlocks == 0) Out << "External ";
651 Out << "Function: " << I->second.name << "\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000652 print(Out, "Type:", I->second.description);
653 print(Out, "Byte Size", I->second.byteSize);
Chris Lattner4a8167f2004-10-15 19:40:31 +0000654 if (I->second.numBasicBlocks) {
655 print(Out, "Basic Blocks", I->second.numBasicBlocks);
656 print(Out, "Instructions", I->second.numInstructions);
657 print(Out, "Long Instructions", I->second.longInstructions);
658 print(Out, "Operands", I->second.numOperands);
659 print(Out, "Instruction Size", I->second.instructionSize);
Misha Brukman8a96c532005-04-21 21:44:41 +0000660 print(Out, "Average Instruction Size",
Chris Lattner4a8167f2004-10-15 19:40:31 +0000661 double(I->second.instructionSize) / I->second.numInstructions);
662 print(Out, "Bytes Per Instruction", I->second.density);
Chris Lattner4a8167f2004-10-15 19:40:31 +0000663 }
Reid Spencerf41aa732004-06-29 23:23:12 +0000664 ++I;
665 }
666 }
667
Reid Spencerf41aa732004-06-29 23:23:12 +0000668 if ( bca.progressiveVerify )
669 Out << bca.VerifyInfo;
670}
671
Chris Lattnerc6d0b162007-02-07 23:46:55 +0000672// AnalyzeBytecodeFile - analyze one file
673Module* llvm::AnalyzeBytecodeFile(const std::string &Filename, ///< File to analyze
674 BytecodeAnalysis& bca, ///< Statistical output
675 BCDecompressor_t *BCDC,
676 std::string *ErrMsg, ///< Error output
677 std::ostream* output ///< Dump output
678 ) {
679 BytecodeHandler* AH = new AnalyzerHandler(bca, output);
680 ModuleProvider* MP = getBytecodeModuleProvider(Filename, BCDC, ErrMsg, AH);
681 if (!MP) return 0;
682 Module *M = MP->releaseModule(ErrMsg);
683 delete MP;
684 return M;
Reid Spencerf41aa732004-06-29 23:23:12 +0000685}