Reid Spencer | f41aa73 | 2004-06-29 23:23:12 +0000 | [diff] [blame^] | 1 | //===-- Analyzer.cpp - Analysis and Dumping of Bytecode 000000---*- C++ -*-===// |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 2 | // |
| 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 Spencer | f41aa73 | 2004-06-29 23:23:12 +0000 | [diff] [blame^] | 10 | // 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 Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 16 | // |
| 17 | //===----------------------------------------------------------------------===// |
| 18 | |
Reid Spencer | f41aa73 | 2004-06-29 23:23:12 +0000 | [diff] [blame^] | 19 | #include "Reader.h" |
| 20 | #include "llvm/Constants.h" |
| 21 | #include "llvm/DerivedTypes.h" |
| 22 | #include "llvm/Module.h" |
| 23 | #include "llvm/Analysis/Verifier.h" |
| 24 | #include "llvm/Bytecode/Analyzer.h" |
| 25 | #include "llvm/Bytecode/BytecodeHandler.h" |
| 26 | #include <iomanip> |
| 27 | #include <sstream> |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 28 | |
| 29 | using namespace llvm; |
| 30 | |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 31 | namespace { |
| 32 | |
Reid Spencer | f41aa73 | 2004-06-29 23:23:12 +0000 | [diff] [blame^] | 33 | /// @brief Bytecode reading handler for analyzing bytecode. |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 34 | class AnalyzerHandler : public BytecodeHandler { |
Reid Spencer | f41aa73 | 2004-06-29 23:23:12 +0000 | [diff] [blame^] | 35 | BytecodeAnalysis& bca; ///< The structure in which data is recorded |
| 36 | std::ostringstream dump; ///< A convenience for dumping data. |
| 37 | /// @brief Keeps track of current function |
| 38 | BytecodeAnalysis::BytecodeFunctionInfo* currFunc; |
| 39 | Module* M; ///< Keeps track of current module |
| 40 | |
| 41 | /// @name Constructor |
| 42 | /// @{ |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 43 | public: |
Reid Spencer | f41aa73 | 2004-06-29 23:23:12 +0000 | [diff] [blame^] | 44 | /// 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 Spencer | 649ee57 | 2004-06-09 06:16:43 +0000 | [diff] [blame] | 47 | AnalyzerHandler(BytecodeAnalysis& TheBca) |
Reid Spencer | cbb22e2 | 2004-06-10 22:00:54 +0000 | [diff] [blame] | 48 | : bca(TheBca) |
Reid Spencer | f41aa73 | 2004-06-29 23:23:12 +0000 | [diff] [blame^] | 49 | , dump() |
Reid Spencer | cbb22e2 | 2004-06-10 22:00:54 +0000 | [diff] [blame] | 50 | , currFunc(0) |
| 51 | { } |
Reid Spencer | 649ee57 | 2004-06-09 06:16:43 +0000 | [diff] [blame] | 52 | |
Reid Spencer | f41aa73 | 2004-06-29 23:23:12 +0000 | [diff] [blame^] | 53 | /// @} |
| 54 | /// @name BytecodeHandler Implementations |
| 55 | /// @{ |
| 56 | public: |
| 57 | virtual void handleError(const std::string& str ) { |
| 58 | dump << "ERROR: " << str << "\n"; |
| 59 | bca.BytecodeDump = dump.str() ; |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 60 | } |
| 61 | |
Reid Spencer | f41aa73 | 2004-06-29 23:23:12 +0000 | [diff] [blame^] | 62 | virtual void handleStart( Module* Mod, unsigned theSize ) { |
| 63 | M = Mod; |
| 64 | dump << "Bytecode {\n"; |
| 65 | bca.byteSize = theSize; |
Reid Spencer | 649ee57 | 2004-06-09 06:16:43 +0000 | [diff] [blame] | 66 | bca.ModuleId.clear(); |
Reid Spencer | 00c28a7 | 2004-06-10 08:09:13 +0000 | [diff] [blame] | 67 | bca.numBlocks = 0; |
Reid Spencer | 649ee57 | 2004-06-09 06:16:43 +0000 | [diff] [blame] | 68 | bca.numTypes = 0; |
| 69 | bca.numValues = 0; |
| 70 | bca.numFunctions = 0; |
| 71 | bca.numConstants = 0; |
| 72 | bca.numGlobalVars = 0; |
| 73 | bca.numInstructions = 0; |
| 74 | bca.numBasicBlocks = 0; |
| 75 | bca.numOperands = 0; |
| 76 | bca.numCmpctnTables = 0; |
| 77 | bca.numSymTab = 0; |
| 78 | bca.maxTypeSlot = 0; |
| 79 | bca.maxValueSlot = 0; |
Reid Spencer | 00c28a7 | 2004-06-10 08:09:13 +0000 | [diff] [blame] | 80 | bca.numAlignment = 0; |
| 81 | bca.fileDensity = 0.0; |
| 82 | bca.globalsDensity = 0.0; |
| 83 | bca.functionDensity = 0.0; |
Reid Spencer | 1cf5024 | 2004-06-11 15:10:38 +0000 | [diff] [blame] | 84 | bca.instructionSize = 0; |
| 85 | bca.longInstructions = 0; |
Reid Spencer | 00c28a7 | 2004-06-10 08:09:13 +0000 | [diff] [blame] | 86 | bca.vbrCount32 = 0; |
| 87 | bca.vbrCount64 = 0; |
| 88 | bca.vbrCompBytes = 0; |
| 89 | bca.vbrExpdBytes = 0; |
Reid Spencer | 649ee57 | 2004-06-09 06:16:43 +0000 | [diff] [blame] | 90 | bca.FunctionInfo.clear(); |
| 91 | bca.BytecodeDump.clear(); |
Reid Spencer | 00c28a7 | 2004-06-10 08:09:13 +0000 | [diff] [blame] | 92 | bca.BlockSizes[BytecodeFormat::Module] = 0; |
| 93 | bca.BlockSizes[BytecodeFormat::Function] = 0; |
| 94 | bca.BlockSizes[BytecodeFormat::ConstantPool] = 0; |
| 95 | bca.BlockSizes[BytecodeFormat::SymbolTable] = 0; |
| 96 | bca.BlockSizes[BytecodeFormat::ModuleGlobalInfo] = 0; |
| 97 | bca.BlockSizes[BytecodeFormat::GlobalTypePlane] = 0; |
| 98 | bca.BlockSizes[BytecodeFormat::BasicBlock] = 0; |
| 99 | bca.BlockSizes[BytecodeFormat::InstructionList] = 0; |
| 100 | bca.BlockSizes[BytecodeFormat::CompactionTable] = 0; |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 101 | } |
| 102 | |
Reid Spencer | cbb22e2 | 2004-06-10 22:00:54 +0000 | [diff] [blame] | 103 | virtual void handleFinish() { |
Reid Spencer | f41aa73 | 2004-06-29 23:23:12 +0000 | [diff] [blame^] | 104 | dump << "} End Bytecode\n"; |
| 105 | bca.BytecodeDump = dump.str() ; |
| 106 | |
Reid Spencer | 00c28a7 | 2004-06-10 08:09:13 +0000 | [diff] [blame] | 107 | bca.fileDensity = double(bca.byteSize) / double( bca.numTypes + bca.numValues ); |
| 108 | double globalSize = 0.0; |
| 109 | globalSize += double(bca.BlockSizes[BytecodeFormat::ConstantPool]); |
| 110 | globalSize += double(bca.BlockSizes[BytecodeFormat::ModuleGlobalInfo]); |
| 111 | globalSize += double(bca.BlockSizes[BytecodeFormat::GlobalTypePlane]); |
| 112 | bca.globalsDensity = globalSize / double( bca.numTypes + bca.numConstants + |
| 113 | bca.numGlobalVars ); |
| 114 | bca.functionDensity = double(bca.BlockSizes[BytecodeFormat::Function]) / |
| 115 | double(bca.numFunctions); |
Reid Spencer | f41aa73 | 2004-06-29 23:23:12 +0000 | [diff] [blame^] | 116 | |
| 117 | if ( bca.progressiveVerify ) { |
| 118 | try { |
| 119 | verifyModule(*M, ThrowExceptionAction); |
| 120 | } catch ( std::string& msg ) { |
| 121 | bca.VerifyInfo += "Verify@Finish: " + msg + "\n"; |
| 122 | } |
| 123 | } |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 124 | } |
| 125 | |
Reid Spencer | cbb22e2 | 2004-06-10 22:00:54 +0000 | [diff] [blame] | 126 | virtual void handleModuleBegin(const std::string& id) { |
Reid Spencer | f41aa73 | 2004-06-29 23:23:12 +0000 | [diff] [blame^] | 127 | dump << " Module " << id << " {\n"; |
Reid Spencer | 649ee57 | 2004-06-09 06:16:43 +0000 | [diff] [blame] | 128 | bca.ModuleId = id; |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Reid Spencer | f41aa73 | 2004-06-29 23:23:12 +0000 | [diff] [blame^] | 131 | virtual void handleModuleEnd(const std::string& id) { |
| 132 | dump << " } End Module " << id << "\n"; |
| 133 | if ( bca.progressiveVerify ) { |
| 134 | try { |
| 135 | verifyModule(*M, ThrowExceptionAction); |
| 136 | } catch ( std::string& msg ) { |
| 137 | bca.VerifyInfo += "Verify@EndModule: " + msg + "\n"; |
| 138 | } |
| 139 | } |
| 140 | } |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 141 | |
Reid Spencer | cbb22e2 | 2004-06-10 22:00:54 +0000 | [diff] [blame] | 142 | virtual void handleVersionInfo( |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 143 | unsigned char RevisionNum, ///< Byte code revision number |
| 144 | Module::Endianness Endianness, ///< Endianness indicator |
| 145 | Module::PointerSize PointerSize ///< PointerSize indicator |
Reid Spencer | f41aa73 | 2004-06-29 23:23:12 +0000 | [diff] [blame^] | 146 | ) { |
| 147 | dump << " RevisionNum: " << int(RevisionNum) |
| 148 | << " Endianness: " << Endianness |
| 149 | << " PointerSize: " << PointerSize << "\n"; |
| 150 | } |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 151 | |
Reid Spencer | f41aa73 | 2004-06-29 23:23:12 +0000 | [diff] [blame^] | 152 | virtual void handleModuleGlobalsBegin() { |
| 153 | dump << " BLOCK: ModuleGlobalInfo {\n"; |
| 154 | } |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 155 | |
Reid Spencer | cbb22e2 | 2004-06-10 22:00:54 +0000 | [diff] [blame] | 156 | virtual void handleGlobalVariable( |
Reid Spencer | f41aa73 | 2004-06-29 23:23:12 +0000 | [diff] [blame^] | 157 | const Type* ElemType, |
| 158 | bool isConstant, |
| 159 | GlobalValue::LinkageTypes Linkage, |
| 160 | unsigned SlotNum, |
| 161 | unsigned initSlot |
Reid Spencer | cbb22e2 | 2004-06-10 22:00:54 +0000 | [diff] [blame] | 162 | ) { |
Reid Spencer | 649ee57 | 2004-06-09 06:16:43 +0000 | [diff] [blame] | 163 | bca.numGlobalVars++; |
Reid Spencer | 00c28a7 | 2004-06-10 08:09:13 +0000 | [diff] [blame] | 164 | bca.numValues++; |
Reid Spencer | f41aa73 | 2004-06-29 23:23:12 +0000 | [diff] [blame^] | 165 | |
| 166 | dump << " GV: " |
| 167 | << ( initSlot == 0 ? "Uni" : "I" ) << "nitialized, " |
| 168 | << ( isConstant? "Constant, " : "Variable, ") |
| 169 | << " Linkage=" << Linkage << " Type=" |
| 170 | << ElemType->getDescription() |
| 171 | << " Slot=" << SlotNum << " InitSlot=" << initSlot |
| 172 | << "\n"; |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 173 | } |
| 174 | |
Reid Spencer | f41aa73 | 2004-06-29 23:23:12 +0000 | [diff] [blame^] | 175 | virtual void handleType( const Type* Ty ) { |
| 176 | bca.numTypes++; |
| 177 | dump << " Type: " << Ty->getDescription() << "\n"; |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 178 | } |
| 179 | |
Reid Spencer | cbb22e2 | 2004-06-10 22:00:54 +0000 | [diff] [blame] | 180 | virtual void handleFunctionDeclaration( |
Reid Spencer | f41aa73 | 2004-06-29 23:23:12 +0000 | [diff] [blame^] | 181 | Function* Func ///< The function |
Reid Spencer | cbb22e2 | 2004-06-10 22:00:54 +0000 | [diff] [blame] | 182 | ) { |
Reid Spencer | 649ee57 | 2004-06-09 06:16:43 +0000 | [diff] [blame] | 183 | bca.numFunctions++; |
Reid Spencer | 00c28a7 | 2004-06-10 08:09:13 +0000 | [diff] [blame] | 184 | bca.numValues++; |
Reid Spencer | f41aa73 | 2004-06-29 23:23:12 +0000 | [diff] [blame^] | 185 | dump << " Function Decl: " << Func->getType()->getDescription() << "\n"; |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 186 | } |
| 187 | |
Reid Spencer | f41aa73 | 2004-06-29 23:23:12 +0000 | [diff] [blame^] | 188 | virtual void handleGlobalInitializer(GlobalVariable* GV, Constant* CV) { |
| 189 | dump << " Initializer: GV="; |
| 190 | GV->print(dump); |
| 191 | dump << " CV="; |
| 192 | CV->print(dump); |
| 193 | dump << "\n"; |
| 194 | } |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 195 | |
Reid Spencer | f41aa73 | 2004-06-29 23:23:12 +0000 | [diff] [blame^] | 196 | virtual void handleModuleGlobalsEnd() { |
| 197 | dump << " } END BLOCK: ModuleGlobalInfo\n"; |
| 198 | if ( bca.progressiveVerify ) { |
| 199 | try { |
| 200 | verifyModule(*M, ThrowExceptionAction); |
| 201 | } catch ( std::string& msg ) { |
| 202 | bca.VerifyInfo += "Verify@EndModuleGlobalInfo: " + msg + "\n"; |
| 203 | } |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | virtual void handleCompactionTableBegin() { |
| 208 | dump << " BLOCK: CompactionTable {\n"; |
| 209 | } |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 210 | |
Reid Spencer | cbb22e2 | 2004-06-10 22:00:54 +0000 | [diff] [blame] | 211 | virtual void handleCompactionTablePlane( unsigned Ty, unsigned NumEntries) { |
Reid Spencer | 649ee57 | 2004-06-09 06:16:43 +0000 | [diff] [blame] | 212 | bca.numCmpctnTables++; |
Reid Spencer | f41aa73 | 2004-06-29 23:23:12 +0000 | [diff] [blame^] | 213 | dump << " Plane: Ty=" << Ty << " Size=" << NumEntries << "\n"; |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 214 | } |
| 215 | |
Reid Spencer | cbb22e2 | 2004-06-10 22:00:54 +0000 | [diff] [blame] | 216 | virtual void handleCompactionTableType( unsigned i, unsigned TypSlot, |
Reid Spencer | f41aa73 | 2004-06-29 23:23:12 +0000 | [diff] [blame^] | 217 | const Type* Ty ) { |
| 218 | dump << " Type: " << i << " Slot:" << TypSlot |
| 219 | << " is " << Ty->getDescription() << "\n"; |
| 220 | } |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 221 | |
Reid Spencer | cbb22e2 | 2004-06-10 22:00:54 +0000 | [diff] [blame] | 222 | virtual void handleCompactionTableValue( |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 223 | unsigned i, |
Reid Spencer | f41aa73 | 2004-06-29 23:23:12 +0000 | [diff] [blame^] | 224 | unsigned TypSlot, |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 225 | unsigned ValSlot, |
Reid Spencer | f41aa73 | 2004-06-29 23:23:12 +0000 | [diff] [blame^] | 226 | const Type* Ty ) { |
| 227 | dump << " Value: " << i << " TypSlot: " << TypSlot |
| 228 | << " ValSlot:" << ValSlot << " is " << Ty->getDescription() |
| 229 | << "\n"; |
| 230 | } |
Reid Spencer | cbb22e2 | 2004-06-10 22:00:54 +0000 | [diff] [blame] | 231 | |
Reid Spencer | f41aa73 | 2004-06-29 23:23:12 +0000 | [diff] [blame^] | 232 | virtual void handleCompactionTableEnd() { |
| 233 | dump << " } END BLOCK: CompactionTable\n"; |
| 234 | } |
Reid Spencer | cbb22e2 | 2004-06-10 22:00:54 +0000 | [diff] [blame] | 235 | |
Reid Spencer | f41aa73 | 2004-06-29 23:23:12 +0000 | [diff] [blame^] | 236 | virtual void handleSymbolTableBegin(Function* CF, SymbolTable* ST) { |
| 237 | bca.numSymTab++; |
| 238 | dump << " BLOCK: SymbolTable {\n"; |
| 239 | } |
Reid Spencer | cbb22e2 | 2004-06-10 22:00:54 +0000 | [diff] [blame] | 240 | |
Reid Spencer | f41aa73 | 2004-06-29 23:23:12 +0000 | [diff] [blame^] | 241 | virtual void handleSymbolTablePlane(unsigned Ty, unsigned NumEntries, |
| 242 | const Type* Typ) { |
| 243 | dump << " Plane: Ty=" << Ty << " Size=" << NumEntries |
| 244 | << " Type: " << Typ->getDescription() << "\n"; |
| 245 | } |
Reid Spencer | cbb22e2 | 2004-06-10 22:00:54 +0000 | [diff] [blame] | 246 | |
Reid Spencer | f41aa73 | 2004-06-29 23:23:12 +0000 | [diff] [blame^] | 247 | virtual void handleSymbolTableType(unsigned i, unsigned slot, |
| 248 | const std::string& name ) { |
| 249 | dump << " Type " << i << " Slot=" << slot |
| 250 | << " Name: " << name << "\n"; |
| 251 | } |
Reid Spencer | cbb22e2 | 2004-06-10 22:00:54 +0000 | [diff] [blame] | 252 | |
Reid Spencer | f41aa73 | 2004-06-29 23:23:12 +0000 | [diff] [blame^] | 253 | virtual void handleSymbolTableValue(unsigned i, unsigned slot, |
| 254 | const std::string& name ) { |
| 255 | dump << " Value " << i << " Slot=" << slot |
| 256 | << " Name: " << name << "\n"; |
| 257 | } |
Reid Spencer | cbb22e2 | 2004-06-10 22:00:54 +0000 | [diff] [blame] | 258 | |
Reid Spencer | f41aa73 | 2004-06-29 23:23:12 +0000 | [diff] [blame^] | 259 | virtual void handleSymbolTableEnd() { |
| 260 | dump << " } END BLOCK: SymbolTable\n"; |
| 261 | } |
Reid Spencer | cbb22e2 | 2004-06-10 22:00:54 +0000 | [diff] [blame] | 262 | |
Reid Spencer | f41aa73 | 2004-06-29 23:23:12 +0000 | [diff] [blame^] | 263 | virtual void handleFunctionBegin(Function* Func, unsigned Size) { |
| 264 | dump << "BLOCK: Function {\n"; |
| 265 | dump << " Linkage: " << Func->getLinkage() << "\n"; |
| 266 | dump << " Type: " << Func->getType()->getDescription() << "\n"; |
Reid Spencer | cbb22e2 | 2004-06-10 22:00:54 +0000 | [diff] [blame] | 267 | const FunctionType* FType = |
| 268 | cast<FunctionType>(Func->getType()->getElementType()); |
| 269 | currFunc = &bca.FunctionInfo[Func]; |
| 270 | currFunc->description = FType->getDescription(); |
| 271 | currFunc->name = Func->getName(); |
| 272 | currFunc->byteSize = Size; |
| 273 | currFunc->numInstructions = 0; |
| 274 | currFunc->numBasicBlocks = 0; |
| 275 | currFunc->numPhis = 0; |
| 276 | currFunc->numOperands = 0; |
| 277 | currFunc->density = 0.0; |
Reid Spencer | 1cf5024 | 2004-06-11 15:10:38 +0000 | [diff] [blame] | 278 | currFunc->instructionSize = 0; |
| 279 | currFunc->longInstructions = 0; |
Reid Spencer | cbb22e2 | 2004-06-10 22:00:54 +0000 | [diff] [blame] | 280 | currFunc->vbrCount32 = 0; |
| 281 | currFunc->vbrCount64 = 0; |
| 282 | currFunc->vbrCompBytes = 0; |
| 283 | currFunc->vbrExpdBytes = 0; |
Reid Spencer | f41aa73 | 2004-06-29 23:23:12 +0000 | [diff] [blame^] | 284 | |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 285 | } |
| 286 | |
Reid Spencer | cbb22e2 | 2004-06-10 22:00:54 +0000 | [diff] [blame] | 287 | virtual void handleFunctionEnd( Function* Func) { |
Reid Spencer | f41aa73 | 2004-06-29 23:23:12 +0000 | [diff] [blame^] | 288 | dump << "} END BLOCK: Function\n"; |
Reid Spencer | cbb22e2 | 2004-06-10 22:00:54 +0000 | [diff] [blame] | 289 | currFunc->density = double(currFunc->byteSize) / |
| 290 | double(currFunc->numInstructions+currFunc->numBasicBlocks); |
Reid Spencer | f41aa73 | 2004-06-29 23:23:12 +0000 | [diff] [blame^] | 291 | |
| 292 | if ( bca.progressiveVerify ) { |
| 293 | try { |
| 294 | verifyModule(*M, ThrowExceptionAction); |
| 295 | } catch ( std::string& msg ) { |
| 296 | bca.VerifyInfo += "Verify@EndFunction: " + msg + "\n"; |
| 297 | } |
| 298 | } |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 299 | } |
| 300 | |
Reid Spencer | cbb22e2 | 2004-06-10 22:00:54 +0000 | [diff] [blame] | 301 | virtual void handleBasicBlockBegin( unsigned blocknum) { |
Reid Spencer | f41aa73 | 2004-06-29 23:23:12 +0000 | [diff] [blame^] | 302 | dump << " BLOCK: BasicBlock #" << blocknum << "{\n"; |
Reid Spencer | 649ee57 | 2004-06-09 06:16:43 +0000 | [diff] [blame] | 303 | bca.numBasicBlocks++; |
Reid Spencer | 00c28a7 | 2004-06-10 08:09:13 +0000 | [diff] [blame] | 304 | bca.numValues++; |
Reid Spencer | cbb22e2 | 2004-06-10 22:00:54 +0000 | [diff] [blame] | 305 | if ( currFunc ) currFunc->numBasicBlocks++; |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 306 | } |
| 307 | |
Reid Spencer | cbb22e2 | 2004-06-10 22:00:54 +0000 | [diff] [blame] | 308 | virtual bool handleInstruction( unsigned Opcode, const Type* iType, |
Reid Spencer | f41aa73 | 2004-06-29 23:23:12 +0000 | [diff] [blame^] | 309 | std::vector<unsigned>& Operands, unsigned Size){ |
| 310 | dump << " INST: OpCode=" |
| 311 | << Instruction::getOpcodeName(Opcode) << " Type=" |
| 312 | << iType->getDescription() << "\n"; |
| 313 | for ( unsigned i = 0; i < Operands.size(); ++i ) |
| 314 | dump << " Op#" << i << " Slot=" << Operands[i] << "\n"; |
| 315 | |
Reid Spencer | 649ee57 | 2004-06-09 06:16:43 +0000 | [diff] [blame] | 316 | bca.numInstructions++; |
Reid Spencer | 00c28a7 | 2004-06-10 08:09:13 +0000 | [diff] [blame] | 317 | bca.numValues++; |
Reid Spencer | 1cf5024 | 2004-06-11 15:10:38 +0000 | [diff] [blame] | 318 | bca.instructionSize += Size; |
| 319 | if (Size > 4 ) bca.longInstructions++; |
Reid Spencer | 00c28a7 | 2004-06-10 08:09:13 +0000 | [diff] [blame] | 320 | bca.numOperands += Operands.size(); |
Reid Spencer | cbb22e2 | 2004-06-10 22:00:54 +0000 | [diff] [blame] | 321 | if ( currFunc ) { |
| 322 | currFunc->numInstructions++; |
Reid Spencer | 1cf5024 | 2004-06-11 15:10:38 +0000 | [diff] [blame] | 323 | currFunc->instructionSize += Size; |
| 324 | if (Size > 4 ) currFunc->longInstructions++; |
Reid Spencer | 8a9a370 | 2004-06-11 03:06:43 +0000 | [diff] [blame] | 325 | if ( Opcode == Instruction::PHI ) currFunc->numPhis++; |
Reid Spencer | cbb22e2 | 2004-06-10 22:00:54 +0000 | [diff] [blame] | 326 | } |
Reid Spencer | 649ee57 | 2004-06-09 06:16:43 +0000 | [diff] [blame] | 327 | return Instruction::isTerminator(Opcode); |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 328 | } |
| 329 | |
Reid Spencer | f41aa73 | 2004-06-29 23:23:12 +0000 | [diff] [blame^] | 330 | virtual void handleBasicBlockEnd(unsigned blocknum) { |
| 331 | dump << " } END BLOCK: BasicBlock #" << blocknum << "{\n"; |
| 332 | } |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 333 | |
Reid Spencer | f41aa73 | 2004-06-29 23:23:12 +0000 | [diff] [blame^] | 334 | virtual void handleGlobalConstantsBegin() { |
| 335 | dump << " BLOCK: GlobalConstants {\n"; |
| 336 | } |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 337 | |
Reid Spencer | f41aa73 | 2004-06-29 23:23:12 +0000 | [diff] [blame^] | 338 | virtual void handleConstantExpression( unsigned Opcode, |
| 339 | std::vector<Constant*> ArgVec, Constant* C ) { |
| 340 | dump << " EXPR: " << Instruction::getOpcodeName(Opcode) << "\n"; |
| 341 | for ( unsigned i = 0; i < ArgVec.size(); ++i ) { |
| 342 | dump << " Arg#" << i << " "; ArgVec[i]->print(dump); dump << "\n"; |
| 343 | } |
| 344 | dump << " Value="; |
| 345 | C->print(dump); |
| 346 | dump << "\n"; |
Reid Spencer | 649ee57 | 2004-06-09 06:16:43 +0000 | [diff] [blame] | 347 | bca.numConstants++; |
Reid Spencer | 00c28a7 | 2004-06-10 08:09:13 +0000 | [diff] [blame] | 348 | bca.numValues++; |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 349 | } |
| 350 | |
Reid Spencer | cbb22e2 | 2004-06-10 22:00:54 +0000 | [diff] [blame] | 351 | virtual void handleConstantValue( Constant * c ) { |
Reid Spencer | f41aa73 | 2004-06-29 23:23:12 +0000 | [diff] [blame^] | 352 | dump << " VALUE: "; |
| 353 | c->print(dump); |
| 354 | dump << "\n"; |
Reid Spencer | 649ee57 | 2004-06-09 06:16:43 +0000 | [diff] [blame] | 355 | bca.numConstants++; |
Reid Spencer | 00c28a7 | 2004-06-10 08:09:13 +0000 | [diff] [blame] | 356 | bca.numValues++; |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 357 | } |
| 358 | |
Reid Spencer | cbb22e2 | 2004-06-10 22:00:54 +0000 | [diff] [blame] | 359 | virtual void handleConstantArray( const ArrayType* AT, |
Reid Spencer | f41aa73 | 2004-06-29 23:23:12 +0000 | [diff] [blame^] | 360 | std::vector<Constant*>& Elements, |
| 361 | unsigned TypeSlot, |
| 362 | Constant* ArrayVal ) { |
| 363 | dump << " ARRAY: " << AT->getDescription() |
| 364 | << " TypeSlot=" << TypeSlot << "\n"; |
| 365 | for ( unsigned i = 0; i < Elements.size(); ++i ) { |
| 366 | dump << " #" << i; |
| 367 | Elements[i]->print(dump); |
| 368 | dump << "\n"; |
| 369 | } |
| 370 | dump << " Value="; |
| 371 | ArrayVal->print(dump); |
| 372 | dump << "\n"; |
| 373 | |
Reid Spencer | 649ee57 | 2004-06-09 06:16:43 +0000 | [diff] [blame] | 374 | bca.numConstants++; |
Reid Spencer | 00c28a7 | 2004-06-10 08:09:13 +0000 | [diff] [blame] | 375 | bca.numValues++; |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 376 | } |
| 377 | |
Reid Spencer | cbb22e2 | 2004-06-10 22:00:54 +0000 | [diff] [blame] | 378 | virtual void handleConstantStruct( |
Reid Spencer | 00c28a7 | 2004-06-10 08:09:13 +0000 | [diff] [blame] | 379 | const StructType* ST, |
Reid Spencer | f41aa73 | 2004-06-29 23:23:12 +0000 | [diff] [blame^] | 380 | std::vector<Constant*>& Elements, |
| 381 | Constant* StructVal) |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 382 | { |
Reid Spencer | f41aa73 | 2004-06-29 23:23:12 +0000 | [diff] [blame^] | 383 | dump << " STRUC: " << ST->getDescription() << "\n"; |
| 384 | for ( unsigned i = 0; i < Elements.size(); ++i ) { |
| 385 | dump << " #" << i << " "; Elements[i]->print(dump); dump << "\n"; |
| 386 | } |
| 387 | dump << " Value="; |
| 388 | StructVal->print(dump); |
| 389 | dump << "\n"; |
Reid Spencer | 649ee57 | 2004-06-09 06:16:43 +0000 | [diff] [blame] | 390 | bca.numConstants++; |
Reid Spencer | 00c28a7 | 2004-06-10 08:09:13 +0000 | [diff] [blame] | 391 | bca.numValues++; |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 392 | } |
| 393 | |
Reid Spencer | f41aa73 | 2004-06-29 23:23:12 +0000 | [diff] [blame^] | 394 | virtual void handleConstantPointer( const PointerType* PT, |
| 395 | unsigned Slot, GlobalValue* GV, Constant* PtrVal) { |
| 396 | dump << " PNTR: " << PT->getDescription() |
| 397 | << " Slot=" << Slot << " GlobalValue="; |
| 398 | GV->print(dump); |
| 399 | dump << "\n Value="; |
| 400 | PtrVal->print(dump); |
| 401 | dump << "\n"; |
Reid Spencer | 649ee57 | 2004-06-09 06:16:43 +0000 | [diff] [blame] | 402 | bca.numConstants++; |
Reid Spencer | 00c28a7 | 2004-06-10 08:09:13 +0000 | [diff] [blame] | 403 | bca.numValues++; |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 404 | } |
| 405 | |
Reid Spencer | cbb22e2 | 2004-06-10 22:00:54 +0000 | [diff] [blame] | 406 | virtual void handleConstantString( const ConstantArray* CA ) { |
Reid Spencer | f41aa73 | 2004-06-29 23:23:12 +0000 | [diff] [blame^] | 407 | dump << " STRNG: "; |
| 408 | CA->print(dump); |
| 409 | dump << "\n"; |
Reid Spencer | 649ee57 | 2004-06-09 06:16:43 +0000 | [diff] [blame] | 410 | bca.numConstants++; |
Reid Spencer | 00c28a7 | 2004-06-10 08:09:13 +0000 | [diff] [blame] | 411 | bca.numValues++; |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 412 | } |
| 413 | |
Reid Spencer | f41aa73 | 2004-06-29 23:23:12 +0000 | [diff] [blame^] | 414 | virtual void handleGlobalConstantsEnd() { |
| 415 | dump << " } END BLOCK: GlobalConstants\n"; |
| 416 | if ( bca.progressiveVerify ) { |
| 417 | try { |
| 418 | verifyModule(*M, ThrowExceptionAction); |
| 419 | } catch ( std::string& msg ) { |
| 420 | bca.VerifyInfo += "Verify@EndGlobalConstants: " + msg + "\n"; |
| 421 | } |
| 422 | } |
| 423 | } |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 424 | |
Reid Spencer | cbb22e2 | 2004-06-10 22:00:54 +0000 | [diff] [blame] | 425 | virtual void handleAlignment(unsigned numBytes) { |
Reid Spencer | 00c28a7 | 2004-06-10 08:09:13 +0000 | [diff] [blame] | 426 | bca.numAlignment += numBytes; |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 427 | } |
| 428 | |
Reid Spencer | cbb22e2 | 2004-06-10 22:00:54 +0000 | [diff] [blame] | 429 | virtual void handleBlock( |
Reid Spencer | 00c28a7 | 2004-06-10 08:09:13 +0000 | [diff] [blame] | 430 | unsigned BType, const unsigned char* StartPtr, unsigned Size) { |
| 431 | bca.numBlocks++; |
| 432 | bca.BlockSizes[llvm::BytecodeFormat::FileBlockIDs(BType)] += Size; |
| 433 | } |
| 434 | |
| 435 | virtual void handleVBR32(unsigned Size ) { |
| 436 | bca.vbrCount32++; |
| 437 | bca.vbrCompBytes += Size; |
| 438 | bca.vbrExpdBytes += sizeof(uint32_t); |
Reid Spencer | cbb22e2 | 2004-06-10 22:00:54 +0000 | [diff] [blame] | 439 | if (currFunc) { |
| 440 | currFunc->vbrCount32++; |
| 441 | currFunc->vbrCompBytes += Size; |
| 442 | currFunc->vbrExpdBytes += sizeof(uint32_t); |
| 443 | } |
Reid Spencer | 00c28a7 | 2004-06-10 08:09:13 +0000 | [diff] [blame] | 444 | } |
Reid Spencer | cbb22e2 | 2004-06-10 22:00:54 +0000 | [diff] [blame] | 445 | |
Reid Spencer | 00c28a7 | 2004-06-10 08:09:13 +0000 | [diff] [blame] | 446 | virtual void handleVBR64(unsigned Size ) { |
| 447 | bca.vbrCount64++; |
| 448 | bca.vbrCompBytes += Size; |
| 449 | bca.vbrExpdBytes += sizeof(uint64_t); |
Reid Spencer | cbb22e2 | 2004-06-10 22:00:54 +0000 | [diff] [blame] | 450 | if ( currFunc ) { |
| 451 | currFunc->vbrCount64++; |
| 452 | currFunc->vbrCompBytes += Size; |
| 453 | currFunc->vbrExpdBytes += sizeof(uint64_t); |
| 454 | } |
Reid Spencer | 00c28a7 | 2004-06-10 08:09:13 +0000 | [diff] [blame] | 455 | } |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 456 | }; |
| 457 | |
Reid Spencer | f41aa73 | 2004-06-29 23:23:12 +0000 | [diff] [blame^] | 458 | |
| 459 | /// @brief Utility for printing a titled unsigned value with |
| 460 | /// an aligned colon. |
| 461 | inline static void print(std::ostream& Out, const char*title, |
| 462 | unsigned val, bool nl = true ) { |
| 463 | Out << std::setw(30) << std::right << title |
| 464 | << std::setw(0) << ": " |
| 465 | << std::setw(9) << val << "\n"; |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 466 | } |
| 467 | |
Reid Spencer | f41aa73 | 2004-06-29 23:23:12 +0000 | [diff] [blame^] | 468 | /// @brief Utility for printing a titled double value with an |
| 469 | /// aligned colon |
| 470 | inline static void print(std::ostream&Out, const char*title, |
| 471 | double val ) { |
| 472 | Out << std::setw(30) << std::right << title |
| 473 | << std::setw(0) << ": " |
| 474 | << std::setw(9) << std::setprecision(6) << val << "\n" ; |
| 475 | } |
| 476 | |
| 477 | /// @brief Utility for printing a titled double value with a |
| 478 | /// percentage and aligned colon. |
| 479 | inline static void print(std::ostream&Out, const char*title, |
| 480 | double top, double bot ) { |
| 481 | Out << std::setw(30) << std::right << title |
| 482 | << std::setw(0) << ": " |
| 483 | << std::setw(9) << std::setprecision(6) << top |
| 484 | << " (" << std::left << std::setw(0) << std::setprecision(4) |
| 485 | << (top/bot)*100.0 << "%)\n"; |
| 486 | } |
| 487 | |
| 488 | /// @brief Utility for printing a titled string value with |
| 489 | /// an aligned colon. |
| 490 | inline static void print(std::ostream&Out, const char*title, |
| 491 | std::string val, bool nl = true) { |
| 492 | Out << std::setw(30) << std::right << title |
| 493 | << std::setw(0) << ": " |
| 494 | << std::left << val << (nl ? "\n" : ""); |
| 495 | } |
| 496 | |
| 497 | } |
| 498 | |
| 499 | namespace llvm { |
| 500 | |
| 501 | /// This function prints the contents of rhe BytecodeAnalysis structure in |
| 502 | /// a human legible form. |
| 503 | /// @brief Print BytecodeAnalysis structure to an ostream |
| 504 | void PrintBytecodeAnalysis(BytecodeAnalysis& bca, std::ostream& Out ) |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 505 | { |
Reid Spencer | f41aa73 | 2004-06-29 23:23:12 +0000 | [diff] [blame^] | 506 | print(Out, "Bytecode Analysis Of Module", bca.ModuleId); |
| 507 | print(Out, "File Size", bca.byteSize); |
| 508 | print(Out, "Bytecode Compression Index",std::string("TBD")); |
| 509 | print(Out, "Number Of Bytecode Blocks", bca.numBlocks); |
| 510 | print(Out, "Number Of Types", bca.numTypes); |
| 511 | print(Out, "Number Of Values", bca.numValues); |
| 512 | print(Out, "Number Of Constants", bca.numConstants); |
| 513 | print(Out, "Number Of Global Variables", bca.numGlobalVars); |
| 514 | print(Out, "Number Of Functions", bca.numFunctions); |
| 515 | print(Out, "Number Of Basic Blocks", bca.numBasicBlocks); |
| 516 | print(Out, "Number Of Instructions", bca.numInstructions); |
| 517 | print(Out, "Number Of Operands", bca.numOperands); |
| 518 | print(Out, "Number Of Compaction Tables", bca.numCmpctnTables); |
| 519 | print(Out, "Number Of Symbol Tables", bca.numSymTab); |
| 520 | print(Out, "Long Instructions", bca.longInstructions); |
| 521 | print(Out, "Instruction Size", bca.instructionSize); |
| 522 | print(Out, "Average Instruction Size", |
| 523 | double(bca.instructionSize)/double(bca.numInstructions)); |
| 524 | print(Out, "Maximum Type Slot Number", bca.maxTypeSlot); |
| 525 | print(Out, "Maximum Value Slot Number", bca.maxValueSlot); |
| 526 | print(Out, "Bytes Thrown To Alignment", double(bca.numAlignment), |
| 527 | double(bca.byteSize)); |
| 528 | print(Out, "File Density (bytes/def)", bca.fileDensity); |
| 529 | print(Out, "Globals Density (bytes/def)", bca.globalsDensity); |
| 530 | print(Out, "Function Density (bytes/func)", bca.functionDensity); |
| 531 | print(Out, "Number of VBR 32-bit Integers", bca.vbrCount32); |
| 532 | print(Out, "Number of VBR 64-bit Integers", bca.vbrCount64); |
| 533 | print(Out, "Number of VBR Compressed Bytes", bca.vbrCompBytes); |
| 534 | print(Out, "Number of VBR Expanded Bytes", bca.vbrExpdBytes); |
| 535 | print(Out, "VBR Savings", |
| 536 | double(bca.vbrExpdBytes)-double(bca.vbrCompBytes), |
| 537 | double(bca.byteSize)); |
| 538 | |
| 539 | if ( bca.detailedResults ) { |
| 540 | print(Out, "Module Bytes", |
| 541 | double(bca.BlockSizes[BytecodeFormat::Module]), |
| 542 | double(bca.byteSize)); |
| 543 | print(Out, "Function Bytes", |
| 544 | double(bca.BlockSizes[BytecodeFormat::Function]), |
| 545 | double(bca.byteSize)); |
| 546 | print(Out, "Constant Pool Bytes", |
| 547 | double(bca.BlockSizes[BytecodeFormat::ConstantPool]), |
| 548 | double(bca.byteSize)); |
| 549 | print(Out, "Symbol Table Bytes", |
| 550 | double(bca.BlockSizes[BytecodeFormat::SymbolTable]), |
| 551 | double(bca.byteSize)); |
| 552 | print(Out, "Module Global Info Bytes", |
| 553 | double(bca.BlockSizes[BytecodeFormat::ModuleGlobalInfo]), |
| 554 | double(bca.byteSize)); |
| 555 | print(Out, "Global Type Plane Bytes", |
| 556 | double(bca.BlockSizes[BytecodeFormat::GlobalTypePlane]), |
| 557 | double(bca.byteSize)); |
| 558 | print(Out, "Basic Block Bytes", |
| 559 | double(bca.BlockSizes[BytecodeFormat::BasicBlock]), |
| 560 | double(bca.byteSize)); |
| 561 | print(Out, "Instruction List Bytes", |
| 562 | double(bca.BlockSizes[BytecodeFormat::InstructionList]), |
| 563 | double(bca.byteSize)); |
| 564 | print(Out, "Compaction Table Bytes", |
| 565 | double(bca.BlockSizes[BytecodeFormat::CompactionTable]), |
| 566 | double(bca.byteSize)); |
| 567 | |
| 568 | std::map<const Function*,BytecodeAnalysis::BytecodeFunctionInfo>::iterator I = |
| 569 | bca.FunctionInfo.begin(); |
| 570 | std::map<const Function*,BytecodeAnalysis::BytecodeFunctionInfo>::iterator E = |
| 571 | bca.FunctionInfo.end(); |
| 572 | |
| 573 | while ( I != E ) { |
| 574 | Out << std::left << std::setw(0); |
| 575 | Out << "Function: " << I->second.name << "\n"; |
| 576 | print(Out, "Type:", I->second.description); |
| 577 | print(Out, "Byte Size", I->second.byteSize); |
| 578 | print(Out, "Instructions", I->second.numInstructions); |
| 579 | print(Out, "Long Instructions", I->second.longInstructions); |
| 580 | print(Out, "Instruction Size", I->second.instructionSize); |
| 581 | print(Out, "Average Instruction Size", |
| 582 | double(I->second.instructionSize)/double(I->second.numInstructions)); |
| 583 | print(Out, "Basic Blocks", I->second.numBasicBlocks); |
| 584 | print(Out, "Operand", I->second.numOperands); |
| 585 | print(Out, "Function Density", I->second.density); |
| 586 | print(Out, "Number of VBR 32-bit Integers", I->second.vbrCount32); |
| 587 | print(Out, "Number of VBR 64-bit Integers", I->second.vbrCount64); |
| 588 | print(Out, "Number of VBR Compressed Bytes", I->second.vbrCompBytes); |
| 589 | print(Out, "Number of VBR Expanded Bytes", I->second.vbrExpdBytes); |
| 590 | print(Out, "VBR Savings", |
| 591 | double(I->second.vbrExpdBytes)-double(I->second.vbrCompBytes), |
| 592 | double(I->second.byteSize)); |
| 593 | ++I; |
| 594 | } |
| 595 | } |
| 596 | |
| 597 | if ( bca.dumpBytecode ) |
| 598 | Out << bca.BytecodeDump; |
| 599 | |
| 600 | if ( bca.progressiveVerify ) |
| 601 | Out << bca.VerifyInfo; |
| 602 | } |
| 603 | |
| 604 | BytecodeHandler* createBytecodeAnalyzerHandler(BytecodeAnalysis& bca) |
| 605 | { |
| 606 | return new AnalyzerHandler(bca); |
| 607 | } |
| 608 | |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 609 | } |
| 610 | |
| 611 | // vim: sw=2 |