blob: 378b72969963e2ba82015fd004584ef83414346e [file] [log] [blame]
Reid Spencerf41aa732004-06-29 23:23:12 +00001//===-- Analyzer.cpp - Analysis and Dumping of Bytecode 000000---*- C++ -*-===//
Reid Spencerdac69c82004-06-07 17:53:43 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Reid Spencer and is distributed under the
6// University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Reid Spencerf41aa732004-06-29 23:23:12 +000010// This file implements the AnalyzerHandler class and PrintBytecodeAnalysis
11// function which together comprise the basic functionality of the llmv-abcd
12// tool. The AnalyzerHandler collects information about the bytecode file into
13// the BytecodeAnalysis structure. The PrintBytecodeAnalysis function prints
14// out the content of that structure.
15// @see include/llvm/Bytecode/Analysis.h
Reid Spencerdac69c82004-06-07 17:53:43 +000016//
17//===----------------------------------------------------------------------===//
18
Reid Spencerf41aa732004-06-29 23:23:12 +000019#include "Reader.h"
20#include "llvm/Constants.h"
21#include "llvm/DerivedTypes.h"
22#include "llvm/Module.h"
23#include "llvm/Analysis/Verifier.h"
Reid Spencerf41aa732004-06-29 23:23:12 +000024#include "llvm/Bytecode/BytecodeHandler.h"
Reid Spencer911ec6d2004-08-21 20:58:19 +000025#include "llvm/Assembly/Writer.h"
Reid Spencerf41aa732004-06-29 23:23:12 +000026#include <iomanip>
27#include <sstream>
Reid Spencerdac69c82004-06-07 17:53:43 +000028
29using namespace llvm;
30
Reid Spencerdac69c82004-06-07 17:53:43 +000031namespace {
32
Reid Spencerf41aa732004-06-29 23:23:12 +000033/// @brief Bytecode reading handler for analyzing bytecode.
Reid Spencerdac69c82004-06-07 17:53:43 +000034class AnalyzerHandler : public BytecodeHandler {
Reid Spencerf41aa732004-06-29 23:23:12 +000035 BytecodeAnalysis& bca; ///< The structure in which data is recorded
Reid Spencer911ec6d2004-08-21 20:58:19 +000036 std::ostream* os; ///< A convenience for osing data.
Reid Spencerf41aa732004-06-29 23:23:12 +000037 /// @brief Keeps track of current function
38 BytecodeAnalysis::BytecodeFunctionInfo* currFunc;
39 Module* M; ///< Keeps track of current module
40
41/// @name Constructor
42/// @{
Reid Spencerdac69c82004-06-07 17:53:43 +000043public:
Reid Spencerf41aa732004-06-29 23:23:12 +000044 /// The only way to construct an AnalyzerHandler. All that is needed is a
45 /// reference to the BytecodeAnalysis structure where the output will be
46 /// placed.
Reid Spencer911ec6d2004-08-21 20:58:19 +000047 AnalyzerHandler(BytecodeAnalysis& TheBca, std::ostream* output)
Reid Spencercbb22e22004-06-10 22:00:54 +000048 : bca(TheBca)
Reid Spencer911ec6d2004-08-21 20:58:19 +000049 , os(output)
Reid Spencercbb22e22004-06-10 22:00:54 +000050 , currFunc(0)
Reid Spencer911ec6d2004-08-21 20:58:19 +000051 { }
Reid Spencer649ee572004-06-09 06:16:43 +000052
Reid Spencerf41aa732004-06-29 23:23:12 +000053/// @}
54/// @name BytecodeHandler Implementations
55/// @{
56public:
57 virtual void handleError(const std::string& str ) {
Reid Spencer911ec6d2004-08-21 20:58:19 +000058 if (os)
59 *os << "ERROR: " << str << "\n";
Reid Spencerdac69c82004-06-07 17:53:43 +000060 }
61
Reid Spencerf41aa732004-06-29 23:23:12 +000062 virtual void handleStart( Module* Mod, unsigned theSize ) {
63 M = Mod;
Reid Spencer911ec6d2004-08-21 20:58:19 +000064 if (os)
65 *os << "Bytecode {\n";
Reid Spencerf41aa732004-06-29 23:23:12 +000066 bca.byteSize = theSize;
Reid Spencer649ee572004-06-09 06:16:43 +000067 bca.ModuleId.clear();
Reid Spencer00c28a72004-06-10 08:09:13 +000068 bca.numBlocks = 0;
Reid Spencer649ee572004-06-09 06:16:43 +000069 bca.numTypes = 0;
70 bca.numValues = 0;
71 bca.numFunctions = 0;
72 bca.numConstants = 0;
73 bca.numGlobalVars = 0;
74 bca.numInstructions = 0;
75 bca.numBasicBlocks = 0;
76 bca.numOperands = 0;
77 bca.numCmpctnTables = 0;
78 bca.numSymTab = 0;
Reid Spencer911ec6d2004-08-21 20:58:19 +000079 bca.numLibraries = 0;
80 bca.libSize = 0;
Reid Spencer649ee572004-06-09 06:16:43 +000081 bca.maxTypeSlot = 0;
82 bca.maxValueSlot = 0;
Reid Spencer00c28a72004-06-10 08:09:13 +000083 bca.numAlignment = 0;
84 bca.fileDensity = 0.0;
85 bca.globalsDensity = 0.0;
86 bca.functionDensity = 0.0;
Reid Spencer1cf50242004-06-11 15:10:38 +000087 bca.instructionSize = 0;
88 bca.longInstructions = 0;
Reid Spencer00c28a72004-06-10 08:09:13 +000089 bca.vbrCount32 = 0;
90 bca.vbrCount64 = 0;
91 bca.vbrCompBytes = 0;
92 bca.vbrExpdBytes = 0;
Reid Spencer649ee572004-06-09 06:16:43 +000093 bca.FunctionInfo.clear();
Reid Spencer911ec6d2004-08-21 20:58:19 +000094 bca.BlockSizes[BytecodeFormat::Reserved_DoNotUse] = 0;
95 bca.BlockSizes[BytecodeFormat::ModuleBlockID] = theSize;
96 bca.BlockSizes[BytecodeFormat::FunctionBlockID] = 0;
97 bca.BlockSizes[BytecodeFormat::ConstantPoolBlockID] = 0;
98 bca.BlockSizes[BytecodeFormat::SymbolTableBlockID] = 0;
99 bca.BlockSizes[BytecodeFormat::ModuleGlobalInfoBlockID] = 0;
100 bca.BlockSizes[BytecodeFormat::GlobalTypePlaneBlockID] = 0;
101 bca.BlockSizes[BytecodeFormat::InstructionListBlockID] = 0;
102 bca.BlockSizes[BytecodeFormat::CompactionTableBlockID] = 0;
Reid Spencerdac69c82004-06-07 17:53:43 +0000103 }
104
Reid Spencercbb22e22004-06-10 22:00:54 +0000105 virtual void handleFinish() {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000106 if (os)
107 *os << "} End Bytecode\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000108
Reid Spencer00c28a72004-06-10 08:09:13 +0000109 bca.fileDensity = double(bca.byteSize) / double( bca.numTypes + bca.numValues );
110 double globalSize = 0.0;
Reid Spencer911ec6d2004-08-21 20:58:19 +0000111 globalSize += double(bca.BlockSizes[BytecodeFormat::ConstantPoolBlockID]);
112 globalSize += double(bca.BlockSizes[BytecodeFormat::ModuleGlobalInfoBlockID]);
113 globalSize += double(bca.BlockSizes[BytecodeFormat::GlobalTypePlaneBlockID]);
Reid Spencer00c28a72004-06-10 08:09:13 +0000114 bca.globalsDensity = globalSize / double( bca.numTypes + bca.numConstants +
115 bca.numGlobalVars );
Reid Spencer911ec6d2004-08-21 20:58:19 +0000116 bca.functionDensity = double(bca.BlockSizes[BytecodeFormat::FunctionBlockID]) /
Reid Spencer00c28a72004-06-10 08:09:13 +0000117 double(bca.numFunctions);
Reid Spencerf41aa732004-06-29 23:23:12 +0000118
119 if ( bca.progressiveVerify ) {
120 try {
Reid Spencerb61cdb72004-07-04 11:00:39 +0000121 verifyModule(*M, ThrowExceptionAction);
Reid Spencerf41aa732004-06-29 23:23:12 +0000122 } catch ( std::string& msg ) {
Reid Spencerb61cdb72004-07-04 11:00:39 +0000123 bca.VerifyInfo += "Verify@Finish: " + msg + "\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000124 }
125 }
Reid Spencerdac69c82004-06-07 17:53:43 +0000126 }
127
Reid Spencercbb22e22004-06-10 22:00:54 +0000128 virtual void handleModuleBegin(const std::string& id) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000129 if (os)
130 *os << " Module " << id << " {\n";
Reid Spencer649ee572004-06-09 06:16:43 +0000131 bca.ModuleId = id;
Reid Spencerdac69c82004-06-07 17:53:43 +0000132 }
133
Reid Spencerf41aa732004-06-29 23:23:12 +0000134 virtual void handleModuleEnd(const std::string& id) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000135 if (os)
136 *os << " } End Module " << id << "\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000137 if ( bca.progressiveVerify ) {
138 try {
Reid Spencerb61cdb72004-07-04 11:00:39 +0000139 verifyModule(*M, ThrowExceptionAction);
Reid Spencerf41aa732004-06-29 23:23:12 +0000140 } catch ( std::string& msg ) {
Reid Spencerb61cdb72004-07-04 11:00:39 +0000141 bca.VerifyInfo += "Verify@EndModule: " + msg + "\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000142 }
143 }
144 }
Reid Spencerdac69c82004-06-07 17:53:43 +0000145
Reid Spencercbb22e22004-06-10 22:00:54 +0000146 virtual void handleVersionInfo(
Reid Spencerdac69c82004-06-07 17:53:43 +0000147 unsigned char RevisionNum, ///< Byte code revision number
148 Module::Endianness Endianness, ///< Endianness indicator
149 Module::PointerSize PointerSize ///< PointerSize indicator
Reid Spencerf41aa732004-06-29 23:23:12 +0000150 ) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000151 if (os)
152 *os << " RevisionNum: " << int(RevisionNum)
Reid Spencerb61cdb72004-07-04 11:00:39 +0000153 << " Endianness: " << Endianness
154 << " PointerSize: " << PointerSize << "\n";
Reid Spencer911ec6d2004-08-21 20:58:19 +0000155 bca.version = RevisionNum;
Reid Spencerf41aa732004-06-29 23:23:12 +0000156 }
Reid Spencerdac69c82004-06-07 17:53:43 +0000157
Reid Spencerf41aa732004-06-29 23:23:12 +0000158 virtual void handleModuleGlobalsBegin() {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000159 if (os)
160 *os << " BLOCK: ModuleGlobalInfo {\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000161 }
Reid Spencerdac69c82004-06-07 17:53:43 +0000162
Reid Spencercbb22e22004-06-10 22:00:54 +0000163 virtual void handleGlobalVariable(
Reid Spencerf41aa732004-06-29 23:23:12 +0000164 const Type* ElemType,
165 bool isConstant,
166 GlobalValue::LinkageTypes Linkage,
167 unsigned SlotNum,
168 unsigned initSlot
Reid Spencercbb22e22004-06-10 22:00:54 +0000169 ) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000170 if (os) {
171 *os << " GV: "
172 << ( initSlot == 0 ? "Uni" : "I" ) << "nitialized, "
173 << ( isConstant? "Constant, " : "Variable, ")
174 << " Linkage=" << Linkage << " Type=";
175 WriteTypeSymbolic(*os, ElemType, M);
176 *os << " Slot=" << SlotNum << " InitSlot=" << initSlot
177 << "\n";
178 }
179
Reid Spencer649ee572004-06-09 06:16:43 +0000180 bca.numGlobalVars++;
Reid Spencer00c28a72004-06-10 08:09:13 +0000181 bca.numValues++;
Reid Spencer911ec6d2004-08-21 20:58:19 +0000182 if (SlotNum > bca.maxValueSlot)
183 bca.maxValueSlot = SlotNum;
184 if (initSlot > bca.maxValueSlot)
185 bca.maxValueSlot = initSlot;
Reid Spencerf41aa732004-06-29 23:23:12 +0000186
Reid Spencer911ec6d2004-08-21 20:58:19 +0000187 }
188
189 virtual void handleTypeList(unsigned numEntries) {
190 bca.maxTypeSlot = numEntries - 1;
Reid Spencerdac69c82004-06-07 17:53:43 +0000191 }
192
Reid Spencerf41aa732004-06-29 23:23:12 +0000193 virtual void handleType( const Type* Ty ) {
194 bca.numTypes++;
Reid Spencer911ec6d2004-08-21 20:58:19 +0000195 if (os) {
196 *os << " Type: ";
197 WriteTypeSymbolic(*os,Ty,M);
198 *os << "\n";
199 }
Reid Spencerdac69c82004-06-07 17:53:43 +0000200 }
201
Reid Spencercbb22e22004-06-10 22:00:54 +0000202 virtual void handleFunctionDeclaration(
Reid Spencerb61cdb72004-07-04 11:00:39 +0000203 Function* Func ///< The function
Reid Spencercbb22e22004-06-10 22:00:54 +0000204 ) {
Reid Spencer649ee572004-06-09 06:16:43 +0000205 bca.numFunctions++;
Reid Spencer00c28a72004-06-10 08:09:13 +0000206 bca.numValues++;
Reid Spencer911ec6d2004-08-21 20:58:19 +0000207 if (os) {
208 *os << " Function Decl: ";
209 WriteTypeSymbolic(*os,Func->getType(),M);
210 *os << "\n";
211 }
Reid Spencerdac69c82004-06-07 17:53:43 +0000212 }
213
Reid Spencerf41aa732004-06-29 23:23:12 +0000214 virtual void handleGlobalInitializer(GlobalVariable* GV, Constant* CV) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000215 if (os) {
216 *os << " Initializer: GV=";
217 GV->print(*os);
218 *os << " CV=";
219 CV->print(*os);
220 *os << "\n";
221 }
222 }
223
224 virtual void handleDependentLibrary(const std::string& libName) {
225 bca.numLibraries++;
226 bca.libSize += libName.size() + (libName.size() < 128 ? 1 : 2);
Reid Spencerf41aa732004-06-29 23:23:12 +0000227 }
Reid Spencerdac69c82004-06-07 17:53:43 +0000228
Reid Spencerf41aa732004-06-29 23:23:12 +0000229 virtual void handleModuleGlobalsEnd() {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000230 if (os)
231 *os << " } END BLOCK: ModuleGlobalInfo\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000232 if ( bca.progressiveVerify ) {
233 try {
Reid Spencerb61cdb72004-07-04 11:00:39 +0000234 verifyModule(*M, ThrowExceptionAction);
Reid Spencerf41aa732004-06-29 23:23:12 +0000235 } catch ( std::string& 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 }
240
241 virtual void handleCompactionTableBegin() {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000242 if (os)
243 *os << " BLOCK: CompactionTable {\n";
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 Spencer649ee572004-06-09 06:16:43 +0000247 bca.numCmpctnTables++;
Reid Spencer911ec6d2004-08-21 20:58:19 +0000248 if (os)
249 *os << " Plane: Ty=" << Ty << " Size=" << NumEntries << "\n";
Reid Spencerdac69c82004-06-07 17:53:43 +0000250 }
251
Reid Spencercbb22e22004-06-10 22:00:54 +0000252 virtual void handleCompactionTableType( unsigned i, unsigned TypSlot,
Reid Spencerf41aa732004-06-29 23:23:12 +0000253 const Type* Ty ) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000254 if (os) {
255 *os << " Type: " << i << " Slot:" << TypSlot << " is ";
256 WriteTypeSymbolic(*os,Ty,M);
257 *os << "\n";
258 }
Reid Spencerf41aa732004-06-29 23:23:12 +0000259 }
Reid Spencerdac69c82004-06-07 17:53:43 +0000260
Chris Lattner2c6c14d2004-08-04 00:19:23 +0000261 virtual void handleCompactionTableValue(unsigned i, unsigned TypSlot,
262 unsigned ValSlot) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000263 if (os)
264 *os << " Value: " << i << " TypSlot: " << TypSlot
Chris Lattner2c6c14d2004-08-04 00:19:23 +0000265 << " ValSlot:" << ValSlot << "\n";
Reid Spencer911ec6d2004-08-21 20:58:19 +0000266 if (ValSlot > bca.maxValueSlot)
267 bca.maxValueSlot = ValSlot;
Reid Spencerf41aa732004-06-29 23:23:12 +0000268 }
Reid Spencercbb22e22004-06-10 22:00:54 +0000269
Reid Spencerf41aa732004-06-29 23:23:12 +0000270 virtual void handleCompactionTableEnd() {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000271 if (os)
272 *os << " } END BLOCK: CompactionTable\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000273 }
Reid Spencercbb22e22004-06-10 22:00:54 +0000274
Reid Spencerf41aa732004-06-29 23:23:12 +0000275 virtual void handleSymbolTableBegin(Function* CF, SymbolTable* ST) {
276 bca.numSymTab++;
Reid Spencer911ec6d2004-08-21 20:58:19 +0000277 if (os)
278 *os << " BLOCK: SymbolTable {\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000279 }
Reid Spencercbb22e22004-06-10 22:00:54 +0000280
Reid Spencerf41aa732004-06-29 23:23:12 +0000281 virtual void handleSymbolTablePlane(unsigned Ty, unsigned NumEntries,
282 const Type* Typ) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000283 if (os) {
284 *os << " Plane: Ty=" << Ty << " Size=" << NumEntries << " Type: ";
285 WriteTypeSymbolic(*os,Typ,M);
286 *os << "\n";
287 }
Reid Spencerf41aa732004-06-29 23:23:12 +0000288 }
Reid Spencercbb22e22004-06-10 22:00:54 +0000289
Reid Spencer911ec6d2004-08-21 20:58:19 +0000290 virtual void handleSymbolTableType(unsigned i, unsigned TypSlot,
Reid Spencerf41aa732004-06-29 23:23:12 +0000291 const std::string& name ) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000292 if (os)
293 *os << " Type " << i << " Slot=" << TypSlot
294 << " Name: " << name << "\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000295 }
Reid Spencercbb22e22004-06-10 22:00:54 +0000296
Reid Spencer911ec6d2004-08-21 20:58:19 +0000297 virtual void handleSymbolTableValue(unsigned i, unsigned ValSlot,
Reid Spencerf41aa732004-06-29 23:23:12 +0000298 const std::string& name ) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000299 if (os)
300 *os << " Value " << i << " Slot=" << ValSlot
301 << " Name: " << name << "\n";
302 if (ValSlot > bca.maxValueSlot)
303 bca.maxValueSlot = ValSlot;
Reid Spencerf41aa732004-06-29 23:23:12 +0000304 }
Reid Spencercbb22e22004-06-10 22:00:54 +0000305
Reid Spencerf41aa732004-06-29 23:23:12 +0000306 virtual void handleSymbolTableEnd() {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000307 if (os)
308 *os << " } END BLOCK: SymbolTable\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000309 }
Reid Spencercbb22e22004-06-10 22:00:54 +0000310
Reid Spencerf41aa732004-06-29 23:23:12 +0000311 virtual void handleFunctionBegin(Function* Func, unsigned Size) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000312 if (os) {
313 *os << " BLOCK: Function {\n"
314 << " Linkage: " << Func->getLinkage() << "\n"
315 << " Type: ";
316 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
346 if ( bca.progressiveVerify ) {
347 try {
Reid Spencerb61cdb72004-07-04 11:00:39 +0000348 verifyModule(*M, ThrowExceptionAction);
Reid Spencerf41aa732004-06-29 23:23:12 +0000349 } catch ( std::string& msg ) {
Reid Spencerb61cdb72004-07-04 11:00:39 +0000350 bca.VerifyInfo += "Verify@EndFunction: " + msg + "\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000351 }
352 }
Reid Spencerdac69c82004-06-07 17:53:43 +0000353 }
354
Reid Spencercbb22e22004-06-10 22:00:54 +0000355 virtual void handleBasicBlockBegin( unsigned blocknum) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000356 if (os)
357 *os << " BLOCK: BasicBlock #" << blocknum << "{\n";
Reid Spencer649ee572004-06-09 06:16:43 +0000358 bca.numBasicBlocks++;
Reid Spencer00c28a72004-06-10 08:09:13 +0000359 bca.numValues++;
Reid Spencercbb22e22004-06-10 22:00:54 +0000360 if ( currFunc ) currFunc->numBasicBlocks++;
Reid Spencerdac69c82004-06-07 17:53:43 +0000361 }
362
Reid Spencercbb22e22004-06-10 22:00:54 +0000363 virtual bool handleInstruction( unsigned Opcode, const Type* iType,
Reid Spencerb61cdb72004-07-04 11:00:39 +0000364 std::vector<unsigned>& Operands, unsigned Size){
Reid Spencer911ec6d2004-08-21 20:58:19 +0000365 if (os) {
366 *os << " INST: OpCode="
367 << Instruction::getOpcodeName(Opcode) << " Type=\"";
368 WriteTypeSymbolic(*os,iType,M);
369 *os << "\"";
370 for ( unsigned i = 0; i < Operands.size(); ++i )
371 *os << " Op(" << i << ")=Slot(" << Operands[i] << ")";
372 *os << "\n";
373 }
Reid Spencerf41aa732004-06-29 23:23:12 +0000374
Reid Spencer649ee572004-06-09 06:16:43 +0000375 bca.numInstructions++;
Reid Spencer00c28a72004-06-10 08:09:13 +0000376 bca.numValues++;
Reid Spencer1cf50242004-06-11 15:10:38 +0000377 bca.instructionSize += Size;
378 if (Size > 4 ) bca.longInstructions++;
Reid Spencer00c28a72004-06-10 08:09:13 +0000379 bca.numOperands += Operands.size();
Reid Spencer911ec6d2004-08-21 20:58:19 +0000380 for (unsigned i = 0; i < Operands.size(); ++i )
381 if (Operands[i] > bca.maxValueSlot)
382 bca.maxValueSlot = Operands[i];
Reid Spencercbb22e22004-06-10 22:00:54 +0000383 if ( currFunc ) {
384 currFunc->numInstructions++;
Reid Spencer1cf50242004-06-11 15:10:38 +0000385 currFunc->instructionSize += Size;
386 if (Size > 4 ) currFunc->longInstructions++;
Reid Spencer8a9a3702004-06-11 03:06:43 +0000387 if ( Opcode == Instruction::PHI ) currFunc->numPhis++;
Reid Spencercbb22e22004-06-10 22:00:54 +0000388 }
Reid Spencer649ee572004-06-09 06:16:43 +0000389 return Instruction::isTerminator(Opcode);
Reid Spencerdac69c82004-06-07 17:53:43 +0000390 }
391
Reid Spencerf41aa732004-06-29 23:23:12 +0000392 virtual void handleBasicBlockEnd(unsigned blocknum) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000393 if (os)
394 *os << " } END BLOCK: BasicBlock #" << blocknum << "{\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000395 }
Reid Spencerdac69c82004-06-07 17:53:43 +0000396
Reid Spencerf41aa732004-06-29 23:23:12 +0000397 virtual void handleGlobalConstantsBegin() {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000398 if (os)
399 *os << " BLOCK: GlobalConstants {\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000400 }
Reid Spencerdac69c82004-06-07 17:53:43 +0000401
Reid Spencerf41aa732004-06-29 23:23:12 +0000402 virtual void handleConstantExpression( unsigned Opcode,
403 std::vector<Constant*> ArgVec, Constant* C ) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000404 if (os) {
405 *os << " EXPR: " << Instruction::getOpcodeName(Opcode) << "\n";
406 for ( unsigned i = 0; i < ArgVec.size(); ++i ) {
407 *os << " Arg#" << i << " "; ArgVec[i]->print(*os);
408 *os << "\n";
409 }
410 *os << " Value=";
411 C->print(*os);
412 *os << "\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000413 }
Reid Spencer649ee572004-06-09 06:16:43 +0000414 bca.numConstants++;
Reid Spencer00c28a72004-06-10 08:09:13 +0000415 bca.numValues++;
Reid Spencerdac69c82004-06-07 17:53:43 +0000416 }
417
Reid Spencercbb22e22004-06-10 22:00:54 +0000418 virtual void handleConstantValue( Constant * c ) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000419 if (os) {
420 *os << " VALUE: ";
421 c->print(*os);
422 *os << "\n";
423 }
Reid Spencer649ee572004-06-09 06:16:43 +0000424 bca.numConstants++;
Reid Spencer00c28a72004-06-10 08:09:13 +0000425 bca.numValues++;
Reid Spencerdac69c82004-06-07 17:53:43 +0000426 }
427
Reid Spencercbb22e22004-06-10 22:00:54 +0000428 virtual void handleConstantArray( const ArrayType* AT,
Reid Spencerf41aa732004-06-29 23:23:12 +0000429 std::vector<Constant*>& Elements,
Reid Spencerb61cdb72004-07-04 11:00:39 +0000430 unsigned TypeSlot,
431 Constant* ArrayVal ) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000432 if (os) {
433 *os << " ARRAY: ";
434 WriteTypeSymbolic(*os,AT,M);
435 *os << " TypeSlot=" << TypeSlot << "\n";
436 for ( unsigned i = 0; i < Elements.size(); ++i ) {
437 *os << " #" << i;
438 Elements[i]->print(*os);
439 *os << "\n";
440 }
441 *os << " Value=";
442 ArrayVal->print(*os);
443 *os << "\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000444 }
Reid Spencerf41aa732004-06-29 23:23:12 +0000445
Reid Spencer649ee572004-06-09 06:16:43 +0000446 bca.numConstants++;
Reid Spencer00c28a72004-06-10 08:09:13 +0000447 bca.numValues++;
Reid Spencerdac69c82004-06-07 17:53:43 +0000448 }
449
Reid Spencercbb22e22004-06-10 22:00:54 +0000450 virtual void handleConstantStruct(
Reid Spencer00c28a72004-06-10 08:09:13 +0000451 const StructType* ST,
Reid Spencerf41aa732004-06-29 23:23:12 +0000452 std::vector<Constant*>& Elements,
Reid Spencerb61cdb72004-07-04 11:00:39 +0000453 Constant* StructVal)
Reid Spencerdac69c82004-06-07 17:53:43 +0000454 {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000455 if (os) {
456 *os << " STRUC: ";
457 WriteTypeSymbolic(*os,ST,M);
458 *os << "\n";
459 for ( unsigned i = 0; i < Elements.size(); ++i ) {
460 *os << " #" << i << " "; Elements[i]->print(*os);
461 *os << "\n";
462 }
463 *os << " Value=";
464 StructVal->print(*os);
465 *os << "\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000466 }
Reid Spencer649ee572004-06-09 06:16:43 +0000467 bca.numConstants++;
Reid Spencer00c28a72004-06-10 08:09:13 +0000468 bca.numValues++;
Reid Spencerdac69c82004-06-07 17:53:43 +0000469 }
470
Brian Gaeke715c90b2004-08-20 06:00:58 +0000471 virtual void handleConstantPacked(
472 const PackedType* PT,
473 std::vector<Constant*>& Elements,
474 unsigned TypeSlot,
475 Constant* PackedVal)
476 {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000477 if (os) {
478 *os << " PACKD: ";
479 WriteTypeSymbolic(*os,PT,M);
480 *os << " TypeSlot=" << TypeSlot << "\n";
481 for ( unsigned i = 0; i < Elements.size(); ++i ) {
482 *os << " #" << i;
483 Elements[i]->print(*os);
484 *os << "\n";
485 }
486 *os << " Value=";
487 PackedVal->print(*os);
488 *os << "\n";
Brian Gaeke715c90b2004-08-20 06:00:58 +0000489 }
Brian Gaeke715c90b2004-08-20 06:00:58 +0000490
491 bca.numConstants++;
492 bca.numValues++;
493 }
494
Reid Spencerf41aa732004-06-29 23:23:12 +0000495 virtual void handleConstantPointer( const PointerType* PT,
Reid Spencer3c90f9f2004-07-18 00:10:36 +0000496 unsigned Slot, GlobalValue* GV ) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000497 if (os) {
498 *os << " PNTR: ";
499 WriteTypeSymbolic(*os,PT,M);
500 *os << " Slot=" << Slot << " GlobalValue=";
501 GV->print(*os);
502 *os << "\n";
503 }
Reid Spencer649ee572004-06-09 06:16:43 +0000504 bca.numConstants++;
Reid Spencer00c28a72004-06-10 08:09:13 +0000505 bca.numValues++;
Reid Spencerdac69c82004-06-07 17:53:43 +0000506 }
507
Reid Spencercbb22e22004-06-10 22:00:54 +0000508 virtual void handleConstantString( const ConstantArray* CA ) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000509 if (os) {
510 *os << " STRNG: ";
511 CA->print(*os);
512 *os << "\n";
513 }
Reid Spencer649ee572004-06-09 06:16:43 +0000514 bca.numConstants++;
Reid Spencer00c28a72004-06-10 08:09:13 +0000515 bca.numValues++;
Reid Spencerdac69c82004-06-07 17:53:43 +0000516 }
517
Reid Spencerf41aa732004-06-29 23:23:12 +0000518 virtual void handleGlobalConstantsEnd() {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000519 if (os)
520 *os << " } END BLOCK: GlobalConstants\n";
521
Reid Spencerf41aa732004-06-29 23:23:12 +0000522 if ( bca.progressiveVerify ) {
523 try {
Reid Spencerb61cdb72004-07-04 11:00:39 +0000524 verifyModule(*M, ThrowExceptionAction);
Reid Spencerf41aa732004-06-29 23:23:12 +0000525 } catch ( std::string& msg ) {
Reid Spencerb61cdb72004-07-04 11:00:39 +0000526 bca.VerifyInfo += "Verify@EndGlobalConstants: " + msg + "\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000527 }
528 }
529 }
Reid Spencerdac69c82004-06-07 17:53:43 +0000530
Reid Spencercbb22e22004-06-10 22:00:54 +0000531 virtual void handleAlignment(unsigned numBytes) {
Reid Spencer00c28a72004-06-10 08:09:13 +0000532 bca.numAlignment += numBytes;
Reid Spencerdac69c82004-06-07 17:53:43 +0000533 }
534
Reid Spencercbb22e22004-06-10 22:00:54 +0000535 virtual void handleBlock(
Reid Spencer00c28a72004-06-10 08:09:13 +0000536 unsigned BType, const unsigned char* StartPtr, unsigned Size) {
537 bca.numBlocks++;
Reid Spencer911ec6d2004-08-21 20:58:19 +0000538 assert(BType >= BytecodeFormat::ModuleBlockID);
539 assert(BType < BytecodeFormat::NumberOfBlockIDs);
540 bca.BlockSizes[
541 llvm::BytecodeFormat::CompressedBytecodeBlockIdentifiers(BType)] += Size;
542
543 if (bca.version < 3) // Check for long block headers versions
544 bca.BlockSizes[llvm::BytecodeFormat::Reserved_DoNotUse] += 8;
545 else
546 bca.BlockSizes[llvm::BytecodeFormat::Reserved_DoNotUse] += 4;
Reid Spencer00c28a72004-06-10 08:09:13 +0000547 }
548
549 virtual void handleVBR32(unsigned Size ) {
550 bca.vbrCount32++;
551 bca.vbrCompBytes += Size;
552 bca.vbrExpdBytes += sizeof(uint32_t);
Reid Spencercbb22e22004-06-10 22:00:54 +0000553 if (currFunc) {
554 currFunc->vbrCount32++;
555 currFunc->vbrCompBytes += Size;
556 currFunc->vbrExpdBytes += sizeof(uint32_t);
557 }
Reid Spencer00c28a72004-06-10 08:09:13 +0000558 }
Reid Spencercbb22e22004-06-10 22:00:54 +0000559
Reid Spencer00c28a72004-06-10 08:09:13 +0000560 virtual void handleVBR64(unsigned Size ) {
561 bca.vbrCount64++;
562 bca.vbrCompBytes += Size;
563 bca.vbrExpdBytes += sizeof(uint64_t);
Reid Spencercbb22e22004-06-10 22:00:54 +0000564 if ( currFunc ) {
565 currFunc->vbrCount64++;
566 currFunc->vbrCompBytes += Size;
567 currFunc->vbrExpdBytes += sizeof(uint64_t);
568 }
Reid Spencer00c28a72004-06-10 08:09:13 +0000569 }
Reid Spencerdac69c82004-06-07 17:53:43 +0000570};
571
Reid Spencerf41aa732004-06-29 23:23:12 +0000572
573/// @brief Utility for printing a titled unsigned value with
574/// an aligned colon.
575inline static void print(std::ostream& Out, const char*title,
576 unsigned val, bool nl = true ) {
577 Out << std::setw(30) << std::right << title
578 << std::setw(0) << ": "
579 << std::setw(9) << val << "\n";
Reid Spencerdac69c82004-06-07 17:53:43 +0000580}
581
Reid Spencerf41aa732004-06-29 23:23:12 +0000582/// @brief Utility for printing a titled double value with an
583/// aligned colon
584inline static void print(std::ostream&Out, const char*title,
585 double val ) {
586 Out << std::setw(30) << std::right << title
587 << std::setw(0) << ": "
588 << std::setw(9) << std::setprecision(6) << val << "\n" ;
589}
590
591/// @brief Utility for printing a titled double value with a
592/// percentage and aligned colon.
593inline static void print(std::ostream&Out, const char*title,
594 double top, double bot ) {
595 Out << std::setw(30) << std::right << title
596 << std::setw(0) << ": "
597 << std::setw(9) << std::setprecision(6) << top
598 << " (" << std::left << std::setw(0) << std::setprecision(4)
599 << (top/bot)*100.0 << "%)\n";
600}
601
602/// @brief Utility for printing a titled string value with
603/// an aligned colon.
604inline static void print(std::ostream&Out, const char*title,
605 std::string val, bool nl = true) {
606 Out << std::setw(30) << std::right << title
607 << std::setw(0) << ": "
608 << std::left << val << (nl ? "\n" : "");
609}
610
611}
612
613namespace llvm {
614
615/// This function prints the contents of rhe BytecodeAnalysis structure in
616/// a human legible form.
617/// @brief Print BytecodeAnalysis structure to an ostream
618void PrintBytecodeAnalysis(BytecodeAnalysis& bca, std::ostream& Out )
Reid Spencerdac69c82004-06-07 17:53:43 +0000619{
Reid Spencer911ec6d2004-08-21 20:58:19 +0000620 Out << "\nSummary Analysis Of " << bca.ModuleId << ": \n\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000621 print(Out, "Bytecode Analysis Of Module", bca.ModuleId);
Reid Spencer911ec6d2004-08-21 20:58:19 +0000622 print(Out, "Bytecode Version Number", bca.version);
Reid Spencerf41aa732004-06-29 23:23:12 +0000623 print(Out, "File Size", bca.byteSize);
Reid Spencer911ec6d2004-08-21 20:58:19 +0000624 print(Out, "Module Bytes",
625 double(bca.BlockSizes[BytecodeFormat::ModuleBlockID]),
626 double(bca.byteSize));
627 print(Out, "Function Bytes",
628 double(bca.BlockSizes[BytecodeFormat::FunctionBlockID]),
629 double(bca.byteSize));
630 print(Out, "Global Types Bytes",
631 double(bca.BlockSizes[BytecodeFormat::GlobalTypePlaneBlockID]),
632 double(bca.byteSize));
633 print(Out, "Constant Pool Bytes",
634 double(bca.BlockSizes[BytecodeFormat::ConstantPoolBlockID]),
635 double(bca.byteSize));
636 print(Out, "Module Globals Bytes",
637 double(bca.BlockSizes[BytecodeFormat::ModuleGlobalInfoBlockID]),
638 double(bca.byteSize));
639 print(Out, "Instruction List Bytes",
640 double(bca.BlockSizes[BytecodeFormat::InstructionListBlockID]),
641 double(bca.byteSize));
642 print(Out, "Compaction Table Bytes",
643 double(bca.BlockSizes[BytecodeFormat::CompactionTableBlockID]),
644 double(bca.byteSize));
645 print(Out, "Symbol Table Bytes",
646 double(bca.BlockSizes[BytecodeFormat::SymbolTableBlockID]),
647 double(bca.byteSize));
648 print(Out, "Alignment Bytes",
649 double(bca.numAlignment), double(bca.byteSize));
650 print(Out, "Block Header Bytes",
651 double(bca.BlockSizes[BytecodeFormat::Reserved_DoNotUse]),
652 double(bca.byteSize));
653 print(Out, "Dependent Libraries Bytes", double(bca.libSize),
654 double(bca.byteSize));
Reid Spencerf41aa732004-06-29 23:23:12 +0000655 print(Out, "Number Of Bytecode Blocks", bca.numBlocks);
Reid Spencer911ec6d2004-08-21 20:58:19 +0000656 print(Out, "Number Of Functions", bca.numFunctions);
Reid Spencerf41aa732004-06-29 23:23:12 +0000657 print(Out, "Number Of Types", bca.numTypes);
Reid Spencerf41aa732004-06-29 23:23:12 +0000658 print(Out, "Number Of Constants", bca.numConstants);
659 print(Out, "Number Of Global Variables", bca.numGlobalVars);
Reid Spencer911ec6d2004-08-21 20:58:19 +0000660 print(Out, "Number Of Values", bca.numValues);
Reid Spencerf41aa732004-06-29 23:23:12 +0000661 print(Out, "Number Of Basic Blocks", bca.numBasicBlocks);
662 print(Out, "Number Of Instructions", bca.numInstructions);
Reid Spencer911ec6d2004-08-21 20:58:19 +0000663 print(Out, "Number Of Long Instructions", bca.longInstructions);
Reid Spencerf41aa732004-06-29 23:23:12 +0000664 print(Out, "Number Of Operands", bca.numOperands);
665 print(Out, "Number Of Compaction Tables", bca.numCmpctnTables);
666 print(Out, "Number Of Symbol Tables", bca.numSymTab);
Reid Spencer911ec6d2004-08-21 20:58:19 +0000667 print(Out, "Number Of Dependent Libs", bca.numLibraries);
668 print(Out, "Total Instruction Size", bca.instructionSize);
Reid Spencerf41aa732004-06-29 23:23:12 +0000669 print(Out, "Average Instruction Size",
Reid Spencer911ec6d2004-08-21 20:58:19 +0000670 double(bca.instructionSize)/double(bca.numInstructions));
671
Reid Spencerf41aa732004-06-29 23:23:12 +0000672 print(Out, "Maximum Type Slot Number", bca.maxTypeSlot);
673 print(Out, "Maximum Value Slot Number", bca.maxValueSlot);
Reid Spencer3120e712004-08-24 22:45:32 +0000674 print(Out, "Bytes Per Value ", bca.fileDensity);
675 print(Out, "Bytes Per Global", bca.globalsDensity);
676 print(Out, "Bytes Per Function", bca.functionDensity);
677 print(Out, "# of VBR 32-bit Integers", bca.vbrCount32);
678 print(Out, "# of VBR 64-bit Integers", bca.vbrCount64);
679 print(Out, "# of VBR Compressed Bytes", bca.vbrCompBytes);
680 print(Out, "# of VBR Expanded Bytes", bca.vbrExpdBytes);
681 print(Out, "Bytes Saved With VBR",
Reid Spencer911ec6d2004-08-21 20:58:19 +0000682 double(bca.vbrExpdBytes)-double(bca.vbrCompBytes),
683 double(bca.vbrExpdBytes));
Reid Spencerf41aa732004-06-29 23:23:12 +0000684
Reid Spencer911ec6d2004-08-21 20:58:19 +0000685 if (bca.detailedResults) {
686 Out << "\nDetailed Analysis Of " << bca.ModuleId << " Functions:\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000687
688 std::map<const Function*,BytecodeAnalysis::BytecodeFunctionInfo>::iterator I =
689 bca.FunctionInfo.begin();
690 std::map<const Function*,BytecodeAnalysis::BytecodeFunctionInfo>::iterator E =
691 bca.FunctionInfo.end();
692
693 while ( I != E ) {
694 Out << std::left << std::setw(0);
Reid Spencer911ec6d2004-08-21 20:58:19 +0000695 Out << "\nFunction: " << 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);
Reid Spencer3120e712004-08-24 22:45:32 +0000698 print(Out, "Basic Blocks", I->second.numBasicBlocks);
Reid Spencerf41aa732004-06-29 23:23:12 +0000699 print(Out, "Instructions", I->second.numInstructions);
700 print(Out, "Long Instructions", I->second.longInstructions);
Reid Spencer3120e712004-08-24 22:45:32 +0000701 print(Out, "Operands", I->second.numOperands);
Reid Spencerf41aa732004-06-29 23:23:12 +0000702 print(Out, "Instruction Size", I->second.instructionSize);
703 print(Out, "Average Instruction Size",
Reid Spencer911ec6d2004-08-21 20:58:19 +0000704 double(I->second.instructionSize)/double(I->second.numInstructions));
Reid Spencer3120e712004-08-24 22:45:32 +0000705 print(Out, "Bytes Per Instruction", I->second.density);
706 print(Out, "# of VBR 32-bit Integers", I->second.vbrCount32);
707 print(Out, "# of VBR 64-bit Integers", I->second.vbrCount64);
708 print(Out, "# of VBR Compressed Bytes", I->second.vbrCompBytes);
709 print(Out, "# of VBR Expanded Bytes", I->second.vbrExpdBytes);
710 print(Out, "Bytes Saved With VBR",
Reid Spencer911ec6d2004-08-21 20:58:19 +0000711 double(I->second.vbrExpdBytes)-double(I->second.vbrCompBytes),
712 double(I->second.vbrExpdBytes));
Reid Spencerf41aa732004-06-29 23:23:12 +0000713 ++I;
714 }
715 }
716
Reid Spencerf41aa732004-06-29 23:23:12 +0000717 if ( bca.progressiveVerify )
718 Out << bca.VerifyInfo;
719}
720
Reid Spencer911ec6d2004-08-21 20:58:19 +0000721BytecodeHandler* createBytecodeAnalyzerHandler(BytecodeAnalysis& bca,
722 std::ostream* output)
Reid Spencerf41aa732004-06-29 23:23:12 +0000723{
Reid Spencer911ec6d2004-08-21 20:58:19 +0000724 return new AnalyzerHandler(bca,output);
Reid Spencerf41aa732004-06-29 23:23:12 +0000725}
726
Reid Spencerdac69c82004-06-07 17:53:43 +0000727}
728
729// vim: sw=2