Reid Spencer | 8a542ae | 2004-07-02 03:22:53 +0000 | [diff] [blame] | 1 | //===-- llvm-bcanalyzer.cpp - Byte Code Analyzer --------------------------===// |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 2 | // |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 5 | // This file was developed by Reid Spencer and is distributed under the |
Reid Spencer | 96684ef | 2004-06-08 05:56:58 +0000 | [diff] [blame] | 6 | // University of Illinois Open Source License. See LICENSE.TXT for details. |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 7 | // |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Reid Spencer | 96684ef | 2004-06-08 05:56:58 +0000 | [diff] [blame] | 10 | // This tool may be invoked in the following manner: |
Reid Spencer | 8a542ae | 2004-07-02 03:22:53 +0000 | [diff] [blame] | 11 | // llvm-bcanalyzer [options] - Read LLVM bytecode from stdin |
| 12 | // llvm-bcanalyzer [options] x.bc - Read LLVM bytecode from the x.bc file |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 13 | // |
Reid Spencer | 96684ef | 2004-06-08 05:56:58 +0000 | [diff] [blame] | 14 | // Options: |
Reid Spencer | 23d46e7 | 2004-06-10 18:38:44 +0000 | [diff] [blame] | 15 | // --help - Output information about command line switches |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 16 | // --nodetails - Don't print out detailed informaton about individual |
Reid Spencer | 23d46e7 | 2004-06-10 18:38:44 +0000 | [diff] [blame] | 17 | // blocks and functions |
| 18 | // --dump - Dump low-level bytecode structure in readable format |
Reid Spencer | 96684ef | 2004-06-08 05:56:58 +0000 | [diff] [blame] | 19 | // |
| 20 | // This tool provides analytical information about a bytecode file. It is |
| 21 | // intended as an aid to developers of bytecode reading and writing software. It |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 22 | // produces on std::out a summary of the bytecode file that shows various |
Reid Spencer | 23d46e7 | 2004-06-10 18:38:44 +0000 | [diff] [blame] | 23 | // statistics about the contents of the file. By default this information is |
| 24 | // detailed and contains information about individual bytecode blocks and the |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 25 | // functions in the module. To avoid this more detailed output, use the |
Reid Spencer | 23d46e7 | 2004-06-10 18:38:44 +0000 | [diff] [blame] | 26 | // -nodetails option to limit the output to just module level information. |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 27 | // The tool is also able to print a bytecode file in a straight forward text |
| 28 | // format that shows the containment and relationships of the information in |
| 29 | // the bytecode file (-dump option). |
Chris Lattner | 63db485 | 2007-04-29 05:51:00 +0000 | [diff] [blame] | 30 | // |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 31 | //===----------------------------------------------------------------------===// |
| 32 | |
Reid Spencer | 86a9a7a | 2004-06-29 23:34:27 +0000 | [diff] [blame] | 33 | #include "llvm/Analysis/Verifier.h" |
Chris Lattner | 45e0f89 | 2007-04-29 08:12:22 +0000 | [diff] [blame] | 34 | #include "llvm/Bitcode/BitstreamReader.h" |
Chris Lattner | 4238d47 | 2007-04-29 20:00:02 +0000 | [diff] [blame] | 35 | #include "llvm/Bitcode/LLVMBitCodes.h" |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 36 | #include "llvm/Bytecode/Analyzer.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 37 | #include "llvm/Support/CommandLine.h" |
Chris Lattner | f2e292c | 2007-02-07 21:41:02 +0000 | [diff] [blame] | 38 | #include "llvm/Support/Compressor.h" |
Chris Lattner | c30598b | 2006-12-06 01:18:01 +0000 | [diff] [blame] | 39 | #include "llvm/Support/ManagedStatic.h" |
Chris Lattner | 45e0f89 | 2007-04-29 08:12:22 +0000 | [diff] [blame] | 40 | #include "llvm/Support/MemoryBuffer.h" |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 41 | #include "llvm/System/Signals.h" |
| 42 | #include <fstream> |
| 43 | #include <iostream> |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 44 | using namespace llvm; |
| 45 | |
| 46 | static cl::opt<std::string> |
| 47 | InputFilename(cl::Positional, cl::desc("<input bytecode>"), cl::init("-")); |
| 48 | |
Reid Spencer | 7593745 | 2004-08-21 21:00:24 +0000 | [diff] [blame] | 49 | static cl::opt<std::string> |
| 50 | OutputFilename("-o", cl::init("-"), cl::desc("<output file>")); |
| 51 | |
Chris Lattner | 63db485 | 2007-04-29 05:51:00 +0000 | [diff] [blame] | 52 | static cl::opt<bool> NoDetails("nodetails", cl::desc("Skip detailed output")); |
| 53 | static cl::opt<bool> Dump("dump", cl::desc("Dump low level bytecode trace")); |
| 54 | static cl::opt<bool> Verify("verify", cl::desc("Progressively verify module")); |
Chris Lattner | 32de633 | 2007-04-29 08:31:14 +0000 | [diff] [blame] | 55 | |
| 56 | //===----------------------------------------------------------------------===// |
| 57 | // Bitcode specific analysis. |
| 58 | //===----------------------------------------------------------------------===// |
| 59 | |
Chris Lattner | 45e0f89 | 2007-04-29 08:12:22 +0000 | [diff] [blame] | 60 | static cl::opt<bool> Bitcode("bitcode", cl::desc("Read a bitcode file")); |
| 61 | |
Chris Lattner | b30c925 | 2007-04-29 21:48:19 +0000 | [diff] [blame] | 62 | static cl::opt<bool> |
| 63 | NonSymbolic("non-symbolic", |
| 64 | cl::desc("Emit numberic info in dump even if" |
| 65 | " symbolic info is available")); |
| 66 | |
Chris Lattner | 45e0f89 | 2007-04-29 08:12:22 +0000 | [diff] [blame] | 67 | /// CurStreamType - If we can sniff the flavor of this stream, we can produce |
| 68 | /// better dump info. |
| 69 | static enum { |
| 70 | UnknownBitstream, |
| 71 | LLVMIRBitstream |
| 72 | } CurStreamType; |
| 73 | |
Chris Lattner | 4238d47 | 2007-04-29 20:00:02 +0000 | [diff] [blame] | 74 | |
| 75 | /// GetBlockName - Return a symbolic block name if known, otherwise return |
Chris Lattner | b30c925 | 2007-04-29 21:48:19 +0000 | [diff] [blame] | 76 | /// null. |
Chris Lattner | 4238d47 | 2007-04-29 20:00:02 +0000 | [diff] [blame] | 77 | static const char *GetBlockName(unsigned BlockID) { |
Chris Lattner | 1772b12 | 2007-05-05 00:17:42 +0000 | [diff] [blame^] | 78 | // Standard blocks for all bitcode files. |
| 79 | if (BlockID < bitc::FIRST_APPLICATION_BLOCKID) { |
| 80 | if (BlockID == bitc::BLOCKINFO_BLOCK_ID) |
| 81 | return "BLOCKINFO_BLOCK"; |
| 82 | return 0; |
| 83 | } |
| 84 | |
Chris Lattner | b30c925 | 2007-04-29 21:48:19 +0000 | [diff] [blame] | 85 | if (CurStreamType != LLVMIRBitstream) return 0; |
Chris Lattner | 4238d47 | 2007-04-29 20:00:02 +0000 | [diff] [blame] | 86 | |
| 87 | switch (BlockID) { |
Chris Lattner | b30c925 | 2007-04-29 21:48:19 +0000 | [diff] [blame] | 88 | default: return 0; |
Chris Lattner | 4238d47 | 2007-04-29 20:00:02 +0000 | [diff] [blame] | 89 | case bitc::MODULE_BLOCK_ID: return "MODULE_BLOCK"; |
Chris Lattner | cd5b7d7 | 2007-05-04 03:01:41 +0000 | [diff] [blame] | 90 | case bitc::PARAMATTR_BLOCK_ID: return "PARAMATTR_BLOCK"; |
Chris Lattner | 4238d47 | 2007-04-29 20:00:02 +0000 | [diff] [blame] | 91 | case bitc::TYPE_BLOCK_ID: return "TYPE_BLOCK"; |
| 92 | case bitc::CONSTANTS_BLOCK_ID: return "CONSTANTS_BLOCK"; |
| 93 | case bitc::FUNCTION_BLOCK_ID: return "FUNCTION_BLOCK"; |
| 94 | case bitc::TYPE_SYMTAB_BLOCK_ID: return "TYPE_SYMTAB"; |
| 95 | case bitc::VALUE_SYMTAB_BLOCK_ID: return "VALUE_SYMTAB"; |
| 96 | } |
| 97 | } |
| 98 | |
Chris Lattner | b30c925 | 2007-04-29 21:48:19 +0000 | [diff] [blame] | 99 | /// GetCodeName - Return a symbolic code name if known, otherwise return |
| 100 | /// null. |
| 101 | static const char *GetCodeName(unsigned CodeID, unsigned BlockID) { |
Chris Lattner | 1772b12 | 2007-05-05 00:17:42 +0000 | [diff] [blame^] | 102 | // Standard blocks for all bitcode files. |
| 103 | if (BlockID < bitc::FIRST_APPLICATION_BLOCKID) { |
| 104 | if (BlockID == bitc::BLOCKINFO_BLOCK_ID) { |
| 105 | switch (CodeID) { |
| 106 | default: return 0; |
| 107 | case bitc::MODULE_CODE_VERSION: return "VERSION"; |
| 108 | } |
| 109 | } |
| 110 | return 0; |
| 111 | } |
| 112 | |
| 113 | |
Chris Lattner | b30c925 | 2007-04-29 21:48:19 +0000 | [diff] [blame] | 114 | if (CurStreamType != LLVMIRBitstream) return 0; |
| 115 | |
| 116 | switch (BlockID) { |
| 117 | default: return 0; |
| 118 | case bitc::MODULE_BLOCK_ID: |
| 119 | switch (CodeID) { |
| 120 | default: return 0; |
| 121 | case bitc::MODULE_CODE_VERSION: return "VERSION"; |
| 122 | case bitc::MODULE_CODE_TRIPLE: return "TRIPLE"; |
| 123 | case bitc::MODULE_CODE_DATALAYOUT: return "DATALAYOUT"; |
| 124 | case bitc::MODULE_CODE_ASM: return "ASM"; |
| 125 | case bitc::MODULE_CODE_SECTIONNAME: return "SECTIONNAME"; |
| 126 | case bitc::MODULE_CODE_DEPLIB: return "DEPLIB"; |
| 127 | case bitc::MODULE_CODE_GLOBALVAR: return "GLOBALVAR"; |
| 128 | case bitc::MODULE_CODE_FUNCTION: return "FUNCTION"; |
| 129 | case bitc::MODULE_CODE_ALIAS: return "ALIAS"; |
| 130 | case bitc::MODULE_CODE_PURGEVALS: return "PURGEVALS"; |
| 131 | } |
Chris Lattner | cd5b7d7 | 2007-05-04 03:01:41 +0000 | [diff] [blame] | 132 | case bitc::PARAMATTR_BLOCK_ID: |
| 133 | switch (CodeID) { |
| 134 | default: return 0; |
| 135 | case bitc::PARAMATTR_CODE_ENTRY: return "ENTRY"; |
| 136 | } |
Chris Lattner | b30c925 | 2007-04-29 21:48:19 +0000 | [diff] [blame] | 137 | case bitc::TYPE_BLOCK_ID: |
| 138 | switch (CodeID) { |
| 139 | default: return 0; |
| 140 | case bitc::TYPE_CODE_NUMENTRY: return "NUMENTRY"; |
Chris Lattner | b30c925 | 2007-04-29 21:48:19 +0000 | [diff] [blame] | 141 | case bitc::TYPE_CODE_VOID: return "VOID"; |
| 142 | case bitc::TYPE_CODE_FLOAT: return "FLOAT"; |
| 143 | case bitc::TYPE_CODE_DOUBLE: return "DOUBLE"; |
| 144 | case bitc::TYPE_CODE_LABEL: return "LABEL"; |
| 145 | case bitc::TYPE_CODE_OPAQUE: return "OPAQUE"; |
| 146 | case bitc::TYPE_CODE_INTEGER: return "INTEGER"; |
| 147 | case bitc::TYPE_CODE_POINTER: return "POINTER"; |
| 148 | case bitc::TYPE_CODE_FUNCTION: return "FUNCTION"; |
| 149 | case bitc::TYPE_CODE_STRUCT: return "STRUCT"; |
| 150 | case bitc::TYPE_CODE_ARRAY: return "ARRAY"; |
| 151 | case bitc::TYPE_CODE_VECTOR: return "VECTOR"; |
| 152 | } |
| 153 | |
| 154 | case bitc::CONSTANTS_BLOCK_ID: |
| 155 | switch (CodeID) { |
| 156 | default: return 0; |
| 157 | case bitc::CST_CODE_SETTYPE: return "SETTYPE"; |
| 158 | case bitc::CST_CODE_NULL: return "NULL"; |
| 159 | case bitc::CST_CODE_UNDEF: return "UNDEF"; |
| 160 | case bitc::CST_CODE_INTEGER: return "INTEGER"; |
| 161 | case bitc::CST_CODE_WIDE_INTEGER: return "WIDE_INTEGER"; |
| 162 | case bitc::CST_CODE_FLOAT: return "FLOAT"; |
| 163 | case bitc::CST_CODE_AGGREGATE: return "AGGREGATE"; |
| 164 | case bitc::CST_CODE_CE_BINOP: return "CE_BINOP"; |
| 165 | case bitc::CST_CODE_CE_CAST: return "CE_CAST"; |
| 166 | case bitc::CST_CODE_CE_GEP: return "CE_GEP"; |
| 167 | case bitc::CST_CODE_CE_SELECT: return "CE_SELECT"; |
| 168 | case bitc::CST_CODE_CE_EXTRACTELT: return "CE_EXTRACTELT"; |
| 169 | case bitc::CST_CODE_CE_INSERTELT: return "CE_INSERTELT"; |
| 170 | case bitc::CST_CODE_CE_SHUFFLEVEC: return "CE_SHUFFLEVEC"; |
| 171 | case bitc::CST_CODE_CE_CMP: return "CE_CMP"; |
| 172 | } |
| 173 | case bitc::FUNCTION_BLOCK_ID: |
| 174 | switch (CodeID) { |
| 175 | default: return 0; |
| 176 | case bitc::FUNC_CODE_DECLAREBLOCKS: return "DECLAREBLOCKS"; |
| 177 | |
| 178 | case bitc::FUNC_CODE_INST_BINOP: return "INST_BINOP"; |
| 179 | case bitc::FUNC_CODE_INST_CAST: return "INST_CAST"; |
| 180 | case bitc::FUNC_CODE_INST_GEP: return "INST_GEP"; |
| 181 | case bitc::FUNC_CODE_INST_SELECT: return "INST_SELECT"; |
| 182 | case bitc::FUNC_CODE_INST_EXTRACTELT: return "INST_EXTRACTELT"; |
| 183 | case bitc::FUNC_CODE_INST_INSERTELT: return "INST_INSERTELT"; |
| 184 | case bitc::FUNC_CODE_INST_SHUFFLEVEC: return "INST_SHUFFLEVEC"; |
| 185 | case bitc::FUNC_CODE_INST_CMP: return "INST_CMP"; |
| 186 | |
| 187 | case bitc::FUNC_CODE_INST_RET: return "INST_RET"; |
| 188 | case bitc::FUNC_CODE_INST_BR: return "INST_BR"; |
| 189 | case bitc::FUNC_CODE_INST_SWITCH: return "INST_SWITCH"; |
| 190 | case bitc::FUNC_CODE_INST_INVOKE: return "INST_INVOKE"; |
| 191 | case bitc::FUNC_CODE_INST_UNWIND: return "INST_UNWIND"; |
| 192 | case bitc::FUNC_CODE_INST_UNREACHABLE: return "INST_UNREACHABLE"; |
| 193 | |
Chris Lattner | 8f92668 | 2007-05-01 02:43:46 +0000 | [diff] [blame] | 194 | case bitc::FUNC_CODE_INST_PHI: return "INST_PHI"; |
Chris Lattner | b30c925 | 2007-04-29 21:48:19 +0000 | [diff] [blame] | 195 | case bitc::FUNC_CODE_INST_MALLOC: return "INST_MALLOC"; |
| 196 | case bitc::FUNC_CODE_INST_FREE: return "INST_FREE"; |
| 197 | case bitc::FUNC_CODE_INST_ALLOCA: return "INST_ALLOCA"; |
| 198 | case bitc::FUNC_CODE_INST_LOAD: return "INST_LOAD"; |
| 199 | case bitc::FUNC_CODE_INST_STORE: return "INST_STORE"; |
| 200 | case bitc::FUNC_CODE_INST_CALL: return "INST_CALL"; |
| 201 | case bitc::FUNC_CODE_INST_VAARG: return "INST_VAARG"; |
| 202 | } |
| 203 | case bitc::TYPE_SYMTAB_BLOCK_ID: |
| 204 | switch (CodeID) { |
| 205 | default: return 0; |
| 206 | case bitc::TST_CODE_ENTRY: return "ENTRY"; |
| 207 | } |
| 208 | case bitc::VALUE_SYMTAB_BLOCK_ID: |
| 209 | switch (CodeID) { |
| 210 | default: return 0; |
| 211 | case bitc::VST_CODE_ENTRY: return "ENTRY"; |
Chris Lattner | cd5b7d7 | 2007-05-04 03:01:41 +0000 | [diff] [blame] | 212 | case bitc::VST_CODE_BBENTRY: return "BBENTRY"; |
Chris Lattner | b30c925 | 2007-04-29 21:48:19 +0000 | [diff] [blame] | 213 | } |
| 214 | } |
| 215 | } |
| 216 | |
Chris Lattner | 4238d47 | 2007-04-29 20:00:02 +0000 | [diff] [blame] | 217 | |
| 218 | struct PerBlockIDStats { |
| 219 | /// NumInstances - This the number of times this block ID has been seen. |
| 220 | unsigned NumInstances; |
| 221 | |
| 222 | /// NumBits - The total size in bits of all of these blocks. |
| 223 | uint64_t NumBits; |
| 224 | |
| 225 | /// NumSubBlocks - The total number of blocks these blocks contain. |
| 226 | unsigned NumSubBlocks; |
| 227 | |
| 228 | /// NumAbbrevs - The total number of abbreviations. |
| 229 | unsigned NumAbbrevs; |
| 230 | |
| 231 | /// NumRecords - The total number of records these blocks contain, and the |
| 232 | /// number that are abbreviated. |
| 233 | unsigned NumRecords, NumAbbreviatedRecords; |
| 234 | |
| 235 | PerBlockIDStats() |
| 236 | : NumInstances(0), NumBits(0), |
| 237 | NumSubBlocks(0), NumAbbrevs(0), NumRecords(0), NumAbbreviatedRecords(0) {} |
| 238 | }; |
| 239 | |
| 240 | static std::map<unsigned, PerBlockIDStats> BlockIDStats; |
| 241 | |
| 242 | |
| 243 | |
Chris Lattner | 32de633 | 2007-04-29 08:31:14 +0000 | [diff] [blame] | 244 | /// Error - All bitcode analysis errors go through this function, making this a |
| 245 | /// good place to breakpoint if debugging. |
| 246 | static bool Error(const std::string &Err) { |
| 247 | std::cerr << Err << "\n"; |
| 248 | return true; |
| 249 | } |
| 250 | |
| 251 | /// ParseBlock - Read a block, updating statistics, etc. |
Chris Lattner | b30c925 | 2007-04-29 21:48:19 +0000 | [diff] [blame] | 252 | static bool ParseBlock(BitstreamReader &Stream, unsigned IndentLevel) { |
Chris Lattner | 1772b12 | 2007-05-05 00:17:42 +0000 | [diff] [blame^] | 253 | std::string Indent(IndentLevel*2, ' '); |
Chris Lattner | 4238d47 | 2007-04-29 20:00:02 +0000 | [diff] [blame] | 254 | uint64_t BlockBitStart = Stream.GetCurrentBitNo(); |
Chris Lattner | 32de633 | 2007-04-29 08:31:14 +0000 | [diff] [blame] | 255 | unsigned BlockID = Stream.ReadSubBlockID(); |
Chris Lattner | b30c925 | 2007-04-29 21:48:19 +0000 | [diff] [blame] | 256 | |
Chris Lattner | 4238d47 | 2007-04-29 20:00:02 +0000 | [diff] [blame] | 257 | // Get the statistics for this BlockID. |
| 258 | PerBlockIDStats &BlockStats = BlockIDStats[BlockID]; |
| 259 | |
| 260 | BlockStats.NumInstances++; |
Chris Lattner | 32de633 | 2007-04-29 08:31:14 +0000 | [diff] [blame] | 261 | |
Chris Lattner | 1772b12 | 2007-05-05 00:17:42 +0000 | [diff] [blame^] | 262 | // BLOCKINFO is a special part of the stream. |
| 263 | if (BlockID == bitc::BLOCKINFO_BLOCK_ID) { |
| 264 | if (Dump) std::cerr << Indent << "<BLOCKINFO_BLOCK/>\n"; |
| 265 | if (Stream.ReadBlockInfoBlock()) |
| 266 | return Error("Malformed BlockInfoBlock"); |
| 267 | uint64_t BlockBitEnd = Stream.GetCurrentBitNo(); |
| 268 | BlockStats.NumBits += BlockBitEnd-BlockBitStart; |
| 269 | return false; |
| 270 | } |
| 271 | |
Chris Lattner | b30c925 | 2007-04-29 21:48:19 +0000 | [diff] [blame] | 272 | unsigned NumWords = 0; |
Chris Lattner | 1772b12 | 2007-05-05 00:17:42 +0000 | [diff] [blame^] | 273 | if (Stream.EnterSubBlock(BlockID, &NumWords)) |
Chris Lattner | 32de633 | 2007-04-29 08:31:14 +0000 | [diff] [blame] | 274 | return Error("Malformed block record"); |
| 275 | |
Chris Lattner | b30c925 | 2007-04-29 21:48:19 +0000 | [diff] [blame] | 276 | const char *BlockName = 0; |
| 277 | if (Dump) { |
| 278 | std::cerr << Indent << "<"; |
| 279 | if ((BlockName = GetBlockName(BlockID))) |
| 280 | std::cerr << BlockName; |
| 281 | else |
| 282 | std::cerr << "UnknownBlock" << BlockID; |
| 283 | |
| 284 | if (NonSymbolic && BlockName) |
| 285 | std::cerr << " BlockID=" << BlockID; |
| 286 | |
| 287 | std::cerr << " NumWords=" << NumWords |
| 288 | << " BlockCodeSize=" << Stream.GetAbbrevIDWidth() << ">\n"; |
| 289 | } |
| 290 | |
Chris Lattner | 32de633 | 2007-04-29 08:31:14 +0000 | [diff] [blame] | 291 | SmallVector<uint64_t, 64> Record; |
| 292 | |
| 293 | // Read all the records for this block. |
| 294 | while (1) { |
| 295 | if (Stream.AtEndOfStream()) |
| 296 | return Error("Premature end of bitstream"); |
| 297 | |
| 298 | // Read the code for this record. |
| 299 | unsigned AbbrevID = Stream.ReadCode(); |
| 300 | switch (AbbrevID) { |
Chris Lattner | 4238d47 | 2007-04-29 20:00:02 +0000 | [diff] [blame] | 301 | case bitc::END_BLOCK: { |
Chris Lattner | 32de633 | 2007-04-29 08:31:14 +0000 | [diff] [blame] | 302 | if (Stream.ReadBlockEnd()) |
| 303 | return Error("Error at end of block"); |
Chris Lattner | 4238d47 | 2007-04-29 20:00:02 +0000 | [diff] [blame] | 304 | uint64_t BlockBitEnd = Stream.GetCurrentBitNo(); |
| 305 | BlockStats.NumBits += BlockBitEnd-BlockBitStart; |
Chris Lattner | b30c925 | 2007-04-29 21:48:19 +0000 | [diff] [blame] | 306 | if (Dump) { |
| 307 | std::cerr << Indent << "</"; |
| 308 | if (BlockName) |
| 309 | std::cerr << BlockName << ">\n"; |
| 310 | else |
| 311 | std::cerr << "UnknownBlock" << BlockID << ">\n"; |
| 312 | } |
Chris Lattner | 32de633 | 2007-04-29 08:31:14 +0000 | [diff] [blame] | 313 | return false; |
Chris Lattner | 4238d47 | 2007-04-29 20:00:02 +0000 | [diff] [blame] | 314 | } |
Chris Lattner | 32de633 | 2007-04-29 08:31:14 +0000 | [diff] [blame] | 315 | case bitc::ENTER_SUBBLOCK: |
Chris Lattner | b30c925 | 2007-04-29 21:48:19 +0000 | [diff] [blame] | 316 | if (ParseBlock(Stream, IndentLevel+1)) |
Chris Lattner | 32de633 | 2007-04-29 08:31:14 +0000 | [diff] [blame] | 317 | return true; |
Chris Lattner | 4238d47 | 2007-04-29 20:00:02 +0000 | [diff] [blame] | 318 | ++BlockStats.NumSubBlocks; |
Chris Lattner | 32de633 | 2007-04-29 08:31:14 +0000 | [diff] [blame] | 319 | break; |
| 320 | case bitc::DEFINE_ABBREV: |
| 321 | Stream.ReadAbbrevRecord(); |
Chris Lattner | 4238d47 | 2007-04-29 20:00:02 +0000 | [diff] [blame] | 322 | ++BlockStats.NumAbbrevs; |
Chris Lattner | 32de633 | 2007-04-29 08:31:14 +0000 | [diff] [blame] | 323 | break; |
| 324 | default: |
Chris Lattner | 4238d47 | 2007-04-29 20:00:02 +0000 | [diff] [blame] | 325 | ++BlockStats.NumRecords; |
| 326 | if (AbbrevID != bitc::UNABBREV_RECORD) |
| 327 | ++BlockStats.NumAbbreviatedRecords; |
| 328 | |
Chris Lattner | 32de633 | 2007-04-29 08:31:14 +0000 | [diff] [blame] | 329 | Record.clear(); |
| 330 | unsigned Code = Stream.ReadRecord(AbbrevID, Record); |
| 331 | // TODO: Compute per-blockid/code stats. |
Chris Lattner | b30c925 | 2007-04-29 21:48:19 +0000 | [diff] [blame] | 332 | |
| 333 | if (Dump) { |
| 334 | std::cerr << Indent << " <"; |
| 335 | if (const char *CodeName = GetCodeName(Code, BlockID)) |
| 336 | std::cerr << CodeName; |
| 337 | else |
| 338 | std::cerr << "UnknownCode" << Code; |
| 339 | if (NonSymbolic && GetCodeName(Code, BlockID)) |
| 340 | std::cerr << " codeid=" << Code; |
| 341 | if (AbbrevID != bitc::UNABBREV_RECORD) |
| 342 | std::cerr << " abbrevid=" << AbbrevID; |
| 343 | |
| 344 | for (unsigned i = 0, e = Record.size(); i != e; ++i) |
| 345 | std::cerr << " op" << i << "=" << (int64_t)Record[i]; |
| 346 | |
Chris Lattner | 1772b12 | 2007-05-05 00:17:42 +0000 | [diff] [blame^] | 347 | std::cerr << "/>\n"; |
Chris Lattner | b30c925 | 2007-04-29 21:48:19 +0000 | [diff] [blame] | 348 | } |
| 349 | |
Chris Lattner | 32de633 | 2007-04-29 08:31:14 +0000 | [diff] [blame] | 350 | break; |
| 351 | } |
| 352 | } |
| 353 | } |
| 354 | |
Chris Lattner | 4238d47 | 2007-04-29 20:00:02 +0000 | [diff] [blame] | 355 | static void PrintSize(double Bits) { |
| 356 | std::cerr << Bits << "b/" << Bits/8 << "B/" << Bits/32 << "W"; |
| 357 | } |
| 358 | |
| 359 | |
Chris Lattner | 45e0f89 | 2007-04-29 08:12:22 +0000 | [diff] [blame] | 360 | /// AnalyzeBitcode - Analyze the bitcode file specified by InputFilename. |
| 361 | static int AnalyzeBitcode() { |
| 362 | // Read the input file. |
| 363 | MemoryBuffer *Buffer; |
| 364 | if (InputFilename == "-") |
| 365 | Buffer = MemoryBuffer::getSTDIN(); |
| 366 | else |
| 367 | Buffer = MemoryBuffer::getFile(&InputFilename[0], InputFilename.size()); |
| 368 | |
Chris Lattner | 32de633 | 2007-04-29 08:31:14 +0000 | [diff] [blame] | 369 | if (Buffer == 0) |
| 370 | return Error("Error reading '" + InputFilename + "'."); |
Chris Lattner | 45e0f89 | 2007-04-29 08:12:22 +0000 | [diff] [blame] | 371 | |
Chris Lattner | 32de633 | 2007-04-29 08:31:14 +0000 | [diff] [blame] | 372 | if (Buffer->getBufferSize() & 3) |
| 373 | return Error("Bitcode stream should be a multiple of 4 bytes in length"); |
Chris Lattner | 45e0f89 | 2007-04-29 08:12:22 +0000 | [diff] [blame] | 374 | |
| 375 | unsigned char *BufPtr = (unsigned char *)Buffer->getBufferStart(); |
| 376 | BitstreamReader Stream(BufPtr, BufPtr+Buffer->getBufferSize()); |
| 377 | |
| 378 | |
| 379 | // Read the stream signature. |
| 380 | char Signature[6]; |
| 381 | Signature[0] = Stream.Read(8); |
| 382 | Signature[1] = Stream.Read(8); |
| 383 | Signature[2] = Stream.Read(4); |
| 384 | Signature[3] = Stream.Read(4); |
| 385 | Signature[4] = Stream.Read(4); |
| 386 | Signature[5] = Stream.Read(4); |
| 387 | |
Chris Lattner | 32de633 | 2007-04-29 08:31:14 +0000 | [diff] [blame] | 388 | // Autodetect the file contents, if it is one we know. |
Chris Lattner | 45e0f89 | 2007-04-29 08:12:22 +0000 | [diff] [blame] | 389 | CurStreamType = UnknownBitstream; |
| 390 | if (Signature[0] == 'B' && Signature[1] == 'C' && |
| 391 | Signature[2] == 0x0 && Signature[3] == 0xC && |
| 392 | Signature[4] == 0xE && Signature[5] == 0xD) |
| 393 | CurStreamType = LLVMIRBitstream; |
| 394 | |
Chris Lattner | 4238d47 | 2007-04-29 20:00:02 +0000 | [diff] [blame] | 395 | unsigned NumTopBlocks = 0; |
| 396 | |
Chris Lattner | 32de633 | 2007-04-29 08:31:14 +0000 | [diff] [blame] | 397 | // Parse the top-level structure. We only allow blocks at the top-level. |
| 398 | while (!Stream.AtEndOfStream()) { |
| 399 | unsigned Code = Stream.ReadCode(); |
| 400 | if (Code != bitc::ENTER_SUBBLOCK) |
| 401 | return Error("Invalid record at top-level"); |
| 402 | |
Chris Lattner | b30c925 | 2007-04-29 21:48:19 +0000 | [diff] [blame] | 403 | if (ParseBlock(Stream, 0)) |
Chris Lattner | 32de633 | 2007-04-29 08:31:14 +0000 | [diff] [blame] | 404 | return true; |
Chris Lattner | 4238d47 | 2007-04-29 20:00:02 +0000 | [diff] [blame] | 405 | ++NumTopBlocks; |
Chris Lattner | 32de633 | 2007-04-29 08:31:14 +0000 | [diff] [blame] | 406 | } |
| 407 | |
Chris Lattner | b30c925 | 2007-04-29 21:48:19 +0000 | [diff] [blame] | 408 | if (Dump) std::cerr << "\n\n"; |
| 409 | |
Chris Lattner | 8f92668 | 2007-05-01 02:43:46 +0000 | [diff] [blame] | 410 | uint64_t BufferSizeBits = Buffer->getBufferSize()*8; |
Chris Lattner | 32de633 | 2007-04-29 08:31:14 +0000 | [diff] [blame] | 411 | // Print a summary of the read file. |
Chris Lattner | 45e0f89 | 2007-04-29 08:12:22 +0000 | [diff] [blame] | 412 | std::cerr << "Summary of " << InputFilename << ":\n"; |
Chris Lattner | 4238d47 | 2007-04-29 20:00:02 +0000 | [diff] [blame] | 413 | std::cerr << " Total size: "; |
Chris Lattner | 8f92668 | 2007-05-01 02:43:46 +0000 | [diff] [blame] | 414 | PrintSize(BufferSizeBits); |
Chris Lattner | 4238d47 | 2007-04-29 20:00:02 +0000 | [diff] [blame] | 415 | std::cerr << "\n"; |
| 416 | std::cerr << " Stream type: "; |
Chris Lattner | 45e0f89 | 2007-04-29 08:12:22 +0000 | [diff] [blame] | 417 | switch (CurStreamType) { |
| 418 | default: assert(0 && "Unknown bitstream type"); |
| 419 | case UnknownBitstream: std::cerr << "unknown\n"; break; |
| 420 | case LLVMIRBitstream: std::cerr << "LLVM IR\n"; break; |
| 421 | } |
Chris Lattner | 4238d47 | 2007-04-29 20:00:02 +0000 | [diff] [blame] | 422 | std::cerr << " # Toplevel Blocks: " << NumTopBlocks << "\n"; |
| 423 | std::cerr << "\n"; |
| 424 | |
| 425 | // Emit per-block stats. |
| 426 | std::cerr << "Per-block Summary:\n"; |
| 427 | for (std::map<unsigned, PerBlockIDStats>::iterator I = BlockIDStats.begin(), |
| 428 | E = BlockIDStats.end(); I != E; ++I) { |
| 429 | std::cerr << " Block ID #" << I->first; |
Chris Lattner | b30c925 | 2007-04-29 21:48:19 +0000 | [diff] [blame] | 430 | if (const char *BlockName = GetBlockName(I->first)) |
Chris Lattner | 4238d47 | 2007-04-29 20:00:02 +0000 | [diff] [blame] | 431 | std::cerr << " (" << BlockName << ")"; |
| 432 | std::cerr << ":\n"; |
| 433 | |
| 434 | const PerBlockIDStats &Stats = I->second; |
| 435 | std::cerr << " Num Instances: " << Stats.NumInstances << "\n"; |
| 436 | std::cerr << " Total Size: "; |
| 437 | PrintSize(Stats.NumBits); |
| 438 | std::cerr << "\n"; |
| 439 | std::cerr << " Average Size: "; |
| 440 | PrintSize(Stats.NumBits/(double)Stats.NumInstances); |
| 441 | std::cerr << "\n"; |
Chris Lattner | 8f92668 | 2007-05-01 02:43:46 +0000 | [diff] [blame] | 442 | std::cerr << " % of file: " |
| 443 | << Stats.NumBits/(double)BufferSizeBits*100 << "\n"; |
Chris Lattner | 4238d47 | 2007-04-29 20:00:02 +0000 | [diff] [blame] | 444 | std::cerr << " Tot/Avg SubBlocks: " << Stats.NumSubBlocks << "/" |
| 445 | << Stats.NumSubBlocks/(double)Stats.NumInstances << "\n"; |
| 446 | std::cerr << " Tot/Avg Abbrevs: " << Stats.NumAbbrevs << "/" |
| 447 | << Stats.NumAbbrevs/(double)Stats.NumInstances << "\n"; |
| 448 | std::cerr << " Tot/Avg Records: " << Stats.NumRecords << "/" |
| 449 | << Stats.NumRecords/(double)Stats.NumInstances << "\n"; |
Chris Lattner | 1772b12 | 2007-05-05 00:17:42 +0000 | [diff] [blame^] | 450 | if (Stats.NumRecords) |
| 451 | std::cerr << " % Abbrev Recs: " << (Stats.NumAbbreviatedRecords/ |
| 452 | (double)Stats.NumRecords)*100 << "\n"; |
Chris Lattner | 4238d47 | 2007-04-29 20:00:02 +0000 | [diff] [blame] | 453 | std::cerr << "\n"; |
| 454 | } |
Chris Lattner | 45e0f89 | 2007-04-29 08:12:22 +0000 | [diff] [blame] | 455 | return 0; |
| 456 | } |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 457 | |
Chris Lattner | 32de633 | 2007-04-29 08:31:14 +0000 | [diff] [blame] | 458 | |
| 459 | //===----------------------------------------------------------------------===// |
| 460 | // Bytecode specific analysis. |
| 461 | //===----------------------------------------------------------------------===// |
| 462 | |
Chris Lattner | c30598b | 2006-12-06 01:18:01 +0000 | [diff] [blame] | 463 | int main(int argc, char **argv) { |
| 464 | llvm_shutdown_obj X; // Call llvm_shutdown() on exit. |
Chris Lattner | 45e0f89 | 2007-04-29 08:12:22 +0000 | [diff] [blame] | 465 | cl::ParseCommandLineOptions(argc, argv, " llvm-bcanalyzer file analyzer\n"); |
| 466 | |
| 467 | sys::PrintStackTraceOnErrorSignal(); |
| 468 | |
| 469 | if (Bitcode) |
| 470 | return AnalyzeBitcode(); |
| 471 | |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 472 | try { |
Chris Lattner | 63db485 | 2007-04-29 05:51:00 +0000 | [diff] [blame] | 473 | std::ostream *Out = &std::cout; // Default to printing to stdout... |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 474 | std::string ErrorMessage; |
| 475 | BytecodeAnalysis bca; |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 476 | |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 477 | /// Determine what to generate |
| 478 | bca.detailedResults = !NoDetails; |
| 479 | bca.progressiveVerify = Verify; |
Reid Spencer | 96684ef | 2004-06-08 05:56:58 +0000 | [diff] [blame] | 480 | |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 481 | /// Analyze the bytecode file |
Chris Lattner | f2e292c | 2007-02-07 21:41:02 +0000 | [diff] [blame] | 482 | Module* M = AnalyzeBytecodeFile(InputFilename, bca, |
| 483 | Compressor::decompressToNewBuffer, |
| 484 | &ErrorMessage, (Dump?Out:0)); |
Reid Spencer | 86a9a7a | 2004-06-29 23:34:27 +0000 | [diff] [blame] | 485 | |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 486 | // All that bcanalyzer does is write the gathered statistics to the output |
| 487 | PrintBytecodeAnalysis(bca,*Out); |
Reid Spencer | 86a9a7a | 2004-06-29 23:34:27 +0000 | [diff] [blame] | 488 | |
Chris Lattner | 45e0f89 | 2007-04-29 08:12:22 +0000 | [diff] [blame] | 489 | if (M && Verify) { |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 490 | std::string verificationMsg; |
Chris Lattner | 05ac92c | 2006-07-06 18:02:27 +0000 | [diff] [blame] | 491 | if (verifyModule(*M, ReturnStatusAction, &verificationMsg)) |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 492 | std::cerr << "Final Verification Message: " << verificationMsg << "\n"; |
Reid Spencer | 86a9a7a | 2004-06-29 23:34:27 +0000 | [diff] [blame] | 493 | } |
Reid Spencer | 86a9a7a | 2004-06-29 23:34:27 +0000 | [diff] [blame] | 494 | |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 495 | if (Out != &std::cout) { |
| 496 | ((std::ofstream*)Out)->close(); |
| 497 | delete Out; |
| 498 | } |
| 499 | return 0; |
| 500 | } catch (const std::string& msg) { |
| 501 | std::cerr << argv[0] << ": " << msg << "\n"; |
| 502 | } catch (...) { |
| 503 | std::cerr << argv[0] << ": Unexpected unknown exception occurred.\n"; |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 504 | } |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 505 | return 1; |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 506 | } |