blob: aef482a289fb85086b5d285b1a402daf6daeaea9 [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
Reid Spenceref9b9a72007-02-05 20:47:22 +0000234 virtual void handleTypeSymbolTableBegin(TypeSymbolTable* ST) {
Misha Brukman8a96c532005-04-21 21:44:41 +0000235 bca.numSymTab++;
Reid Spencer911ec6d2004-08-21 20:58:19 +0000236 if (os)
Reid Spenceref9b9a72007-02-05 20:47:22 +0000237 *os << " BLOCK: TypeSymbolTable {\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000238 }
Reid Spenceref9b9a72007-02-05 20:47:22 +0000239 virtual void handleValueSymbolTableBegin(Function* CF, ValueSymbolTable* ST) {
240 bca.numSymTab++;
241 if (os)
242 *os << " BLOCK: ValueSymbolTable {\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000243 }
Reid Spencercbb22e22004-06-10 22:00:54 +0000244
Misha Brukman8a96c532005-04-21 21:44:41 +0000245 virtual void handleSymbolTableType(unsigned i, unsigned TypSlot,
246 const std::string& name ) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000247 if (os)
248 *os << " Type " << i << " Slot=" << TypSlot
Misha Brukman8a96c532005-04-21 21:44:41 +0000249 << " Name: " << name << "\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000250 }
Reid Spencercbb22e22004-06-10 22:00:54 +0000251
Reid Spenceref9b9a72007-02-05 20:47:22 +0000252 virtual void handleSymbolTableValue(unsigned TySlot, unsigned ValSlot,
Chris Lattnerdd8cec52007-02-12 18:53:43 +0000253 const char *Name, unsigned NameLen) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000254 if (os)
Reid Spenceref9b9a72007-02-05 20:47:22 +0000255 *os << " Value " << TySlot << " Slot=" << ValSlot
Chris Lattnerdd8cec52007-02-12 18:53:43 +0000256 << " Name: " << std::string(Name, Name+NameLen) << "\n";
Reid Spencer911ec6d2004-08-21 20:58:19 +0000257 if (ValSlot > bca.maxValueSlot)
258 bca.maxValueSlot = ValSlot;
Reid Spencerf41aa732004-06-29 23:23:12 +0000259 }
Reid Spencercbb22e22004-06-10 22:00:54 +0000260
Reid Spenceref9b9a72007-02-05 20:47:22 +0000261 virtual void handleValueSymbolTableEnd() {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000262 if (os)
Reid Spenceref9b9a72007-02-05 20:47:22 +0000263 *os << " } END BLOCK: ValueSymbolTable\n";
264 }
265
266 virtual void handleTypeSymbolTableEnd() {
267 if (os)
268 *os << " } END BLOCK: TypeSymbolTable\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000269 }
Reid Spencercbb22e22004-06-10 22:00:54 +0000270
Reid Spencerf41aa732004-06-29 23:23:12 +0000271 virtual void handleFunctionBegin(Function* Func, unsigned Size) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000272 if (os) {
273 *os << " BLOCK: Function {\n"
274 << " Linkage: " << Func->getLinkage() << "\n"
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000275 << " Visibility: " << Func->getVisibility() << "\n"
Misha Brukman8a96c532005-04-21 21:44:41 +0000276 << " Type: ";
Reid Spencer911ec6d2004-08-21 20:58:19 +0000277 WriteTypeSymbolic(*os,Func->getType(),M);
278 *os << "\n";
279 }
280
Reid Spencercbb22e22004-06-10 22:00:54 +0000281 currFunc = &bca.FunctionInfo[Func];
Reid Spencer911ec6d2004-08-21 20:58:19 +0000282 std::ostringstream tmp;
283 WriteTypeSymbolic(tmp,Func->getType(),M);
284 currFunc->description = tmp.str();
Reid Spencercbb22e22004-06-10 22:00:54 +0000285 currFunc->name = Func->getName();
286 currFunc->byteSize = Size;
287 currFunc->numInstructions = 0;
288 currFunc->numBasicBlocks = 0;
289 currFunc->numPhis = 0;
290 currFunc->numOperands = 0;
291 currFunc->density = 0.0;
Reid Spencer1cf50242004-06-11 15:10:38 +0000292 currFunc->instructionSize = 0;
293 currFunc->longInstructions = 0;
Reid Spencerf41aa732004-06-29 23:23:12 +0000294
Reid Spencerdac69c82004-06-07 17:53:43 +0000295 }
296
Reid Spencercbb22e22004-06-10 22:00:54 +0000297 virtual void handleFunctionEnd( Function* Func) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000298 if (os)
299 *os << " } END BLOCK: Function\n";
Reid Spencercbb22e22004-06-10 22:00:54 +0000300 currFunc->density = double(currFunc->byteSize) /
Reid Spencer3120e712004-08-24 22:45:32 +0000301 double(currFunc->numInstructions);
Reid Spencerf41aa732004-06-29 23:23:12 +0000302
Chris Lattner05ac92c2006-07-06 18:02:27 +0000303 if (bca.progressiveVerify) {
304 std::string msg;
305 if (verifyModule(*M, ReturnStatusAction, &msg))
Reid Spencerb61cdb72004-07-04 11:00:39 +0000306 bca.VerifyInfo += "Verify@EndFunction: " + msg + "\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000307 }
Reid Spencerdac69c82004-06-07 17:53:43 +0000308 }
309
Reid Spencercbb22e22004-06-10 22:00:54 +0000310 virtual void handleBasicBlockBegin( unsigned blocknum) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000311 if (os)
312 *os << " BLOCK: BasicBlock #" << blocknum << "{\n";
Reid Spencer649ee572004-06-09 06:16:43 +0000313 bca.numBasicBlocks++;
Reid Spencer00c28a72004-06-10 08:09:13 +0000314 bca.numValues++;
Reid Spencercbb22e22004-06-10 22:00:54 +0000315 if ( currFunc ) currFunc->numBasicBlocks++;
Reid Spencerdac69c82004-06-07 17:53:43 +0000316 }
317
Misha Brukman8a96c532005-04-21 21:44:41 +0000318 virtual bool handleInstruction( unsigned Opcode, const Type* iType,
Chris Lattner63cf59e2007-02-07 05:08:39 +0000319 unsigned *Operands, unsigned NumOps,
Reid Spenceref9b9a72007-02-05 20:47:22 +0000320 Instruction *Inst,
321 unsigned Size){
Reid Spencer911ec6d2004-08-21 20:58:19 +0000322 if (os) {
Misha Brukman8a96c532005-04-21 21:44:41 +0000323 *os << " INST: OpCode="
Reid Spenceref9b9a72007-02-05 20:47:22 +0000324 << Instruction::getOpcodeName(Opcode);
Chris Lattner63cf59e2007-02-07 05:08:39 +0000325 for (unsigned i = 0; i != NumOps; ++i)
Reid Spenceref9b9a72007-02-05 20:47:22 +0000326 *os << " Op(" << Operands[i] << ")";
327 *os << *Inst;
Reid Spencer911ec6d2004-08-21 20:58:19 +0000328 }
Reid Spencerf41aa732004-06-29 23:23:12 +0000329
Reid Spencer649ee572004-06-09 06:16:43 +0000330 bca.numInstructions++;
Reid Spencer00c28a72004-06-10 08:09:13 +0000331 bca.numValues++;
Reid Spencer1cf50242004-06-11 15:10:38 +0000332 bca.instructionSize += Size;
333 if (Size > 4 ) bca.longInstructions++;
Chris Lattner63cf59e2007-02-07 05:08:39 +0000334 bca.numOperands += NumOps;
335 for (unsigned i = 0; i != NumOps; ++i)
Reid Spencer911ec6d2004-08-21 20:58:19 +0000336 if (Operands[i] > bca.maxValueSlot)
337 bca.maxValueSlot = Operands[i];
Reid Spencercbb22e22004-06-10 22:00:54 +0000338 if ( currFunc ) {
339 currFunc->numInstructions++;
Reid Spencer1cf50242004-06-11 15:10:38 +0000340 currFunc->instructionSize += Size;
341 if (Size > 4 ) currFunc->longInstructions++;
Chris Lattner63cf59e2007-02-07 05:08:39 +0000342 if (Opcode == Instruction::PHI) currFunc->numPhis++;
Reid Spencercbb22e22004-06-10 22:00:54 +0000343 }
Misha Brukman8a96c532005-04-21 21:44:41 +0000344 return Instruction::isTerminator(Opcode);
Reid Spencerdac69c82004-06-07 17:53:43 +0000345 }
346
Misha Brukman8a96c532005-04-21 21:44:41 +0000347 virtual void handleBasicBlockEnd(unsigned blocknum) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000348 if (os)
Reid Spencerc6d416a2006-12-15 21:46:37 +0000349 *os << " } END BLOCK: BasicBlock #" << blocknum << "\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000350 }
Reid Spencerdac69c82004-06-07 17:53:43 +0000351
Misha Brukman8a96c532005-04-21 21:44:41 +0000352 virtual void handleGlobalConstantsBegin() {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000353 if (os)
354 *os << " BLOCK: GlobalConstants {\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000355 }
Reid Spencerdac69c82004-06-07 17:53:43 +0000356
Chris Lattner63cf59e2007-02-07 05:08:39 +0000357 virtual void handleConstantExpression(unsigned Opcode,
358 Constant**ArgVec, unsigned NumArgs, Constant* C) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000359 if (os) {
360 *os << " EXPR: " << Instruction::getOpcodeName(Opcode) << "\n";
Chris Lattner63cf59e2007-02-07 05:08:39 +0000361 for ( unsigned i = 0; i != NumArgs; ++i ) {
Misha Brukman8a96c532005-04-21 21:44:41 +0000362 *os << " Arg#" << i << " "; ArgVec[i]->print(*os);
Reid Spencer911ec6d2004-08-21 20:58:19 +0000363 *os << "\n";
364 }
365 *os << " Value=";
366 C->print(*os);
367 *os << "\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000368 }
Reid Spencer649ee572004-06-09 06:16:43 +0000369 bca.numConstants++;
Reid Spencer00c28a72004-06-10 08:09:13 +0000370 bca.numValues++;
Reid Spencerdac69c82004-06-07 17:53:43 +0000371 }
372
Reid Spencercbb22e22004-06-10 22:00:54 +0000373 virtual void handleConstantValue( Constant * c ) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000374 if (os) {
375 *os << " VALUE: ";
376 c->print(*os);
377 *os << "\n";
378 }
Reid Spencer649ee572004-06-09 06:16:43 +0000379 bca.numConstants++;
Reid Spencer00c28a72004-06-10 08:09:13 +0000380 bca.numValues++;
Reid Spencerdac69c82004-06-07 17:53:43 +0000381 }
382
Misha Brukman8a96c532005-04-21 21:44:41 +0000383 virtual void handleConstantArray( const ArrayType* AT,
Chris Lattner63cf59e2007-02-07 05:08:39 +0000384 Constant**Elements, unsigned NumElts,
Reid Spencerb61cdb72004-07-04 11:00:39 +0000385 unsigned TypeSlot,
386 Constant* ArrayVal ) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000387 if (os) {
388 *os << " ARRAY: ";
Misha Brukman8a96c532005-04-21 21:44:41 +0000389 WriteTypeSymbolic(*os,AT,M);
Reid Spencer911ec6d2004-08-21 20:58:19 +0000390 *os << " TypeSlot=" << TypeSlot << "\n";
Chris Lattner63cf59e2007-02-07 05:08:39 +0000391 for (unsigned i = 0; i != NumElts; ++i) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000392 *os << " #" << i;
393 Elements[i]->print(*os);
394 *os << "\n";
395 }
396 *os << " Value=";
397 ArrayVal->print(*os);
398 *os << "\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000399 }
Reid Spencerf41aa732004-06-29 23:23:12 +0000400
Reid Spencer649ee572004-06-09 06:16:43 +0000401 bca.numConstants++;
Reid Spencer00c28a72004-06-10 08:09:13 +0000402 bca.numValues++;
Reid Spencerdac69c82004-06-07 17:53:43 +0000403 }
404
Reid Spencercbb22e22004-06-10 22:00:54 +0000405 virtual void handleConstantStruct(
Reid Spencer00c28a72004-06-10 08:09:13 +0000406 const StructType* ST,
Chris Lattner63cf59e2007-02-07 05:08:39 +0000407 Constant**Elements, unsigned NumElts,
Reid Spencerb61cdb72004-07-04 11:00:39 +0000408 Constant* StructVal)
Reid Spencerdac69c82004-06-07 17:53:43 +0000409 {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000410 if (os) {
411 *os << " STRUC: ";
412 WriteTypeSymbolic(*os,ST,M);
413 *os << "\n";
Chris Lattner63cf59e2007-02-07 05:08:39 +0000414 for ( unsigned i = 0; i != NumElts; ++i) {
Misha Brukman8a96c532005-04-21 21:44:41 +0000415 *os << " #" << i << " "; Elements[i]->print(*os);
Reid Spencer911ec6d2004-08-21 20:58:19 +0000416 *os << "\n";
417 }
418 *os << " Value=";
419 StructVal->print(*os);
420 *os << "\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000421 }
Reid Spencer649ee572004-06-09 06:16:43 +0000422 bca.numConstants++;
Reid Spencer00c28a72004-06-10 08:09:13 +0000423 bca.numValues++;
Reid Spencerdac69c82004-06-07 17:53:43 +0000424 }
425
Reid Spencer9d6565a2007-02-15 02:26:10 +0000426 virtual void handleConstantVector(
427 const VectorType* PT,
Chris Lattner63cf59e2007-02-07 05:08:39 +0000428 Constant**Elements, unsigned NumElts,
Misha Brukman8a96c532005-04-21 21:44:41 +0000429 unsigned TypeSlot,
Reid Spencerac9dcb92007-02-15 03:39:18 +0000430 Constant* VectorVal)
Brian Gaeke715c90b2004-08-20 06:00:58 +0000431 {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000432 if (os) {
433 *os << " PACKD: ";
434 WriteTypeSymbolic(*os,PT,M);
435 *os << " TypeSlot=" << TypeSlot << "\n";
Chris Lattner63cf59e2007-02-07 05:08:39 +0000436 for ( unsigned i = 0; i != NumElts; ++i ) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000437 *os << " #" << i;
438 Elements[i]->print(*os);
439 *os << "\n";
440 }
441 *os << " Value=";
Reid Spencerac9dcb92007-02-15 03:39:18 +0000442 VectorVal->print(*os);
Reid Spencer911ec6d2004-08-21 20:58:19 +0000443 *os << "\n";
Brian Gaeke715c90b2004-08-20 06:00:58 +0000444 }
Brian Gaeke715c90b2004-08-20 06:00:58 +0000445
446 bca.numConstants++;
447 bca.numValues++;
448 }
449
Misha Brukman8a96c532005-04-21 21:44:41 +0000450 virtual void handleConstantPointer( const PointerType* PT,
Reid Spencer3c90f9f2004-07-18 00:10:36 +0000451 unsigned Slot, GlobalValue* GV ) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000452 if (os) {
453 *os << " PNTR: ";
454 WriteTypeSymbolic(*os,PT,M);
455 *os << " Slot=" << Slot << " GlobalValue=";
456 GV->print(*os);
457 *os << "\n";
458 }
Reid Spencer649ee572004-06-09 06:16:43 +0000459 bca.numConstants++;
Reid Spencer00c28a72004-06-10 08:09:13 +0000460 bca.numValues++;
Reid Spencerdac69c82004-06-07 17:53:43 +0000461 }
462
Reid Spencercbb22e22004-06-10 22:00:54 +0000463 virtual void handleConstantString( const ConstantArray* CA ) {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000464 if (os) {
465 *os << " STRNG: ";
Misha Brukman8a96c532005-04-21 21:44:41 +0000466 CA->print(*os);
Reid Spencer911ec6d2004-08-21 20:58:19 +0000467 *os << "\n";
468 }
Reid Spencer649ee572004-06-09 06:16:43 +0000469 bca.numConstants++;
Reid Spencer00c28a72004-06-10 08:09:13 +0000470 bca.numValues++;
Reid Spencerdac69c82004-06-07 17:53:43 +0000471 }
472
Misha Brukman8a96c532005-04-21 21:44:41 +0000473 virtual void handleGlobalConstantsEnd() {
Reid Spencer911ec6d2004-08-21 20:58:19 +0000474 if (os)
475 *os << " } END BLOCK: GlobalConstants\n";
476
Chris Lattner05ac92c2006-07-06 18:02:27 +0000477 if (bca.progressiveVerify) {
478 std::string msg;
479 if (verifyModule(*M, ReturnStatusAction, &msg))
Reid Spencerb61cdb72004-07-04 11:00:39 +0000480 bca.VerifyInfo += "Verify@EndGlobalConstants: " + msg + "\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000481 }
482 }
Reid Spencerdac69c82004-06-07 17:53:43 +0000483
Reid Spencercbb22e22004-06-10 22:00:54 +0000484 virtual void handleAlignment(unsigned numBytes) {
Reid Spencer00c28a72004-06-10 08:09:13 +0000485 bca.numAlignment += numBytes;
Reid Spencerdac69c82004-06-07 17:53:43 +0000486 }
487
Reid Spencercbb22e22004-06-10 22:00:54 +0000488 virtual void handleBlock(
Reid Spencer00c28a72004-06-10 08:09:13 +0000489 unsigned BType, const unsigned char* StartPtr, unsigned Size) {
490 bca.numBlocks++;
Reid Spencer911ec6d2004-08-21 20:58:19 +0000491 assert(BType >= BytecodeFormat::ModuleBlockID);
492 assert(BType < BytecodeFormat::NumberOfBlockIDs);
493 bca.BlockSizes[
Reid Spencerd798a512006-11-14 04:47:22 +0000494 llvm::BytecodeFormat::BytecodeBlockIdentifiers(BType)] += Size;
Reid Spencer911ec6d2004-08-21 20:58:19 +0000495
496 if (bca.version < 3) // Check for long block headers versions
497 bca.BlockSizes[llvm::BytecodeFormat::Reserved_DoNotUse] += 8;
498 else
499 bca.BlockSizes[llvm::BytecodeFormat::Reserved_DoNotUse] += 4;
Reid Spencer00c28a72004-06-10 08:09:13 +0000500 }
501
Reid Spencerdac69c82004-06-07 17:53:43 +0000502};
Chris Lattnerc6d0b162007-02-07 23:46:55 +0000503} // end anonymous namespace
Reid Spencerf41aa732004-06-29 23:23:12 +0000504
505/// @brief Utility for printing a titled unsigned value with
506/// an aligned colon.
Misha Brukman8a96c532005-04-21 21:44:41 +0000507inline static void print(std::ostream& Out, const char*title,
Reid Spencerf41aa732004-06-29 23:23:12 +0000508 unsigned val, bool nl = true ) {
Misha Brukman8a96c532005-04-21 21:44:41 +0000509 Out << std::setw(30) << std::right << title
Reid Spencerf41aa732004-06-29 23:23:12 +0000510 << std::setw(0) << ": "
511 << std::setw(9) << val << "\n";
Reid Spencerdac69c82004-06-07 17:53:43 +0000512}
513
Reid Spencerf41aa732004-06-29 23:23:12 +0000514/// @brief Utility for printing a titled double value with an
515/// aligned colon
Misha Brukman8a96c532005-04-21 21:44:41 +0000516inline static void print(std::ostream&Out, const char*title,
Reid Spencerf41aa732004-06-29 23:23:12 +0000517 double val ) {
Misha Brukman8a96c532005-04-21 21:44:41 +0000518 Out << std::setw(30) << std::right << title
Reid Spencerf41aa732004-06-29 23:23:12 +0000519 << std::setw(0) << ": "
520 << std::setw(9) << std::setprecision(6) << val << "\n" ;
521}
522
523/// @brief Utility for printing a titled double value with a
524/// percentage and aligned colon.
Misha Brukman8a96c532005-04-21 21:44:41 +0000525inline static void print(std::ostream&Out, const char*title,
Reid Spencerf41aa732004-06-29 23:23:12 +0000526 double top, double bot ) {
Misha Brukman8a96c532005-04-21 21:44:41 +0000527 Out << std::setw(30) << std::right << title
Reid Spencerf41aa732004-06-29 23:23:12 +0000528 << std::setw(0) << ": "
Misha Brukman8a96c532005-04-21 21:44:41 +0000529 << std::setw(9) << std::setprecision(6) << top
530 << " (" << std::left << std::setw(0) << std::setprecision(4)
Reid Spencerf41aa732004-06-29 23:23:12 +0000531 << (top/bot)*100.0 << "%)\n";
532}
533
534/// @brief Utility for printing a titled string value with
535/// an aligned colon.
Misha Brukman8a96c532005-04-21 21:44:41 +0000536inline static void print(std::ostream&Out, const char*title,
Reid Spencerf41aa732004-06-29 23:23:12 +0000537 std::string val, bool nl = true) {
Misha Brukman8a96c532005-04-21 21:44:41 +0000538 Out << std::setw(30) << std::right << title
Reid Spencerf41aa732004-06-29 23:23:12 +0000539 << std::setw(0) << ": "
540 << std::left << val << (nl ? "\n" : "");
541}
542
Reid Spencerf41aa732004-06-29 23:23:12 +0000543/// This function prints the contents of rhe BytecodeAnalysis structure in
544/// a human legible form.
545/// @brief Print BytecodeAnalysis structure to an ostream
Chris Lattnerc6d0b162007-02-07 23:46:55 +0000546void llvm::PrintBytecodeAnalysis(BytecodeAnalysis& bca, std::ostream& Out )
Reid Spencerdac69c82004-06-07 17:53:43 +0000547{
Reid Spencer911ec6d2004-08-21 20:58:19 +0000548 Out << "\nSummary Analysis Of " << bca.ModuleId << ": \n\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000549 print(Out, "Bytecode Analysis Of Module", bca.ModuleId);
Reid Spencer911ec6d2004-08-21 20:58:19 +0000550 print(Out, "Bytecode Version Number", bca.version);
Reid Spencerf41aa732004-06-29 23:23:12 +0000551 print(Out, "File Size", bca.byteSize);
Reid Spencer911ec6d2004-08-21 20:58:19 +0000552 print(Out, "Module Bytes",
553 double(bca.BlockSizes[BytecodeFormat::ModuleBlockID]),
554 double(bca.byteSize));
Misha Brukman8a96c532005-04-21 21:44:41 +0000555 print(Out, "Function Bytes",
Reid Spencer911ec6d2004-08-21 20:58:19 +0000556 double(bca.BlockSizes[BytecodeFormat::FunctionBlockID]),
557 double(bca.byteSize));
Misha Brukman8a96c532005-04-21 21:44:41 +0000558 print(Out, "Global Types Bytes",
Reid Spencer911ec6d2004-08-21 20:58:19 +0000559 double(bca.BlockSizes[BytecodeFormat::GlobalTypePlaneBlockID]),
560 double(bca.byteSize));
Misha Brukman8a96c532005-04-21 21:44:41 +0000561 print(Out, "Constant Pool Bytes",
Reid Spencer911ec6d2004-08-21 20:58:19 +0000562 double(bca.BlockSizes[BytecodeFormat::ConstantPoolBlockID]),
563 double(bca.byteSize));
Misha Brukman8a96c532005-04-21 21:44:41 +0000564 print(Out, "Module Globals Bytes",
Reid Spencer911ec6d2004-08-21 20:58:19 +0000565 double(bca.BlockSizes[BytecodeFormat::ModuleGlobalInfoBlockID]),
566 double(bca.byteSize));
Misha Brukman8a96c532005-04-21 21:44:41 +0000567 print(Out, "Instruction List Bytes",
Reid Spencer911ec6d2004-08-21 20:58:19 +0000568 double(bca.BlockSizes[BytecodeFormat::InstructionListBlockID]),
569 double(bca.byteSize));
Reid Spencer78d033e2007-01-06 07:24:44 +0000570 print(Out, "Value Symbol Table Bytes",
571 double(bca.BlockSizes[BytecodeFormat::ValueSymbolTableBlockID]),
572 double(bca.byteSize));
573 print(Out, "Type Symbol Table Bytes",
574 double(bca.BlockSizes[BytecodeFormat::TypeSymbolTableBlockID]),
Reid Spencer911ec6d2004-08-21 20:58:19 +0000575 double(bca.byteSize));
Misha Brukman8a96c532005-04-21 21:44:41 +0000576 print(Out, "Alignment Bytes",
Reid Spencer911ec6d2004-08-21 20:58:19 +0000577 double(bca.numAlignment), double(bca.byteSize));
Misha Brukman8a96c532005-04-21 21:44:41 +0000578 print(Out, "Block Header Bytes",
Reid Spencer911ec6d2004-08-21 20:58:19 +0000579 double(bca.BlockSizes[BytecodeFormat::Reserved_DoNotUse]),
580 double(bca.byteSize));
Misha Brukman8a96c532005-04-21 21:44:41 +0000581 print(Out, "Dependent Libraries Bytes", double(bca.libSize),
Reid Spencer911ec6d2004-08-21 20:58:19 +0000582 double(bca.byteSize));
Reid Spencerf41aa732004-06-29 23:23:12 +0000583 print(Out, "Number Of Bytecode Blocks", bca.numBlocks);
Reid Spencer911ec6d2004-08-21 20:58:19 +0000584 print(Out, "Number Of Functions", bca.numFunctions);
Reid Spencerf41aa732004-06-29 23:23:12 +0000585 print(Out, "Number Of Types", bca.numTypes);
Reid Spencerf41aa732004-06-29 23:23:12 +0000586 print(Out, "Number Of Constants", bca.numConstants);
587 print(Out, "Number Of Global Variables", bca.numGlobalVars);
Reid Spencer911ec6d2004-08-21 20:58:19 +0000588 print(Out, "Number Of Values", bca.numValues);
Reid Spencerf41aa732004-06-29 23:23:12 +0000589 print(Out, "Number Of Basic Blocks", bca.numBasicBlocks);
590 print(Out, "Number Of Instructions", bca.numInstructions);
Reid Spencer911ec6d2004-08-21 20:58:19 +0000591 print(Out, "Number Of Long Instructions", bca.longInstructions);
Reid Spencerf41aa732004-06-29 23:23:12 +0000592 print(Out, "Number Of Operands", bca.numOperands);
Reid Spencerf41aa732004-06-29 23:23:12 +0000593 print(Out, "Number Of Symbol Tables", bca.numSymTab);
Reid Spencer911ec6d2004-08-21 20:58:19 +0000594 print(Out, "Number Of Dependent Libs", bca.numLibraries);
595 print(Out, "Total Instruction Size", bca.instructionSize);
Misha Brukman8a96c532005-04-21 21:44:41 +0000596 print(Out, "Average Instruction Size",
Reid Spencer911ec6d2004-08-21 20:58:19 +0000597 double(bca.instructionSize)/double(bca.numInstructions));
598
Reid Spencerf41aa732004-06-29 23:23:12 +0000599 print(Out, "Maximum Type Slot Number", bca.maxTypeSlot);
600 print(Out, "Maximum Value Slot Number", bca.maxValueSlot);
Reid Spencer3120e712004-08-24 22:45:32 +0000601 print(Out, "Bytes Per Value ", bca.fileDensity);
602 print(Out, "Bytes Per Global", bca.globalsDensity);
603 print(Out, "Bytes Per Function", bca.functionDensity);
Reid Spencerf41aa732004-06-29 23:23:12 +0000604
Reid Spencer911ec6d2004-08-21 20:58:19 +0000605 if (bca.detailedResults) {
606 Out << "\nDetailed Analysis Of " << bca.ModuleId << " Functions:\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000607
Misha Brukman8a96c532005-04-21 21:44:41 +0000608 std::map<const Function*,BytecodeAnalysis::BytecodeFunctionInfo>::iterator I =
Reid Spencerf41aa732004-06-29 23:23:12 +0000609 bca.FunctionInfo.begin();
Misha Brukman8a96c532005-04-21 21:44:41 +0000610 std::map<const Function*,BytecodeAnalysis::BytecodeFunctionInfo>::iterator E =
Reid Spencerf41aa732004-06-29 23:23:12 +0000611 bca.FunctionInfo.end();
612
613 while ( I != E ) {
Chris Lattner4a8167f2004-10-15 19:40:31 +0000614 Out << std::left << std::setw(0) << "\n";
615 if (I->second.numBasicBlocks == 0) Out << "External ";
616 Out << "Function: " << I->second.name << "\n";
Reid Spencerf41aa732004-06-29 23:23:12 +0000617 print(Out, "Type:", I->second.description);
618 print(Out, "Byte Size", I->second.byteSize);
Chris Lattner4a8167f2004-10-15 19:40:31 +0000619 if (I->second.numBasicBlocks) {
620 print(Out, "Basic Blocks", I->second.numBasicBlocks);
621 print(Out, "Instructions", I->second.numInstructions);
622 print(Out, "Long Instructions", I->second.longInstructions);
623 print(Out, "Operands", I->second.numOperands);
624 print(Out, "Instruction Size", I->second.instructionSize);
Misha Brukman8a96c532005-04-21 21:44:41 +0000625 print(Out, "Average Instruction Size",
Chris Lattner4a8167f2004-10-15 19:40:31 +0000626 double(I->second.instructionSize) / I->second.numInstructions);
627 print(Out, "Bytes Per Instruction", I->second.density);
Chris Lattner4a8167f2004-10-15 19:40:31 +0000628 }
Reid Spencerf41aa732004-06-29 23:23:12 +0000629 ++I;
630 }
631 }
632
Reid Spencerf41aa732004-06-29 23:23:12 +0000633 if ( bca.progressiveVerify )
634 Out << bca.VerifyInfo;
635}
636
Chris Lattnerc6d0b162007-02-07 23:46:55 +0000637// AnalyzeBytecodeFile - analyze one file
638Module* llvm::AnalyzeBytecodeFile(const std::string &Filename, ///< File to analyze
639 BytecodeAnalysis& bca, ///< Statistical output
640 BCDecompressor_t *BCDC,
641 std::string *ErrMsg, ///< Error output
642 std::ostream* output ///< Dump output
643 ) {
644 BytecodeHandler* AH = new AnalyzerHandler(bca, output);
645 ModuleProvider* MP = getBytecodeModuleProvider(Filename, BCDC, ErrMsg, AH);
646 if (!MP) return 0;
647 Module *M = MP->releaseModule(ErrMsg);
648 delete MP;
649 return M;
Reid Spencerf41aa732004-06-29 23:23:12 +0000650}