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