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 | // |
Chris Lattner | 21c62da | 2007-12-29 20:44:31 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // 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" |
Chris Lattner | e2a466b | 2009-04-06 20:54:32 +0000 | [diff] [blame] | 33 | #include "llvm/Bitcode/ReaderWriter.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 34 | #include "llvm/Support/CommandLine.h" |
Chris Lattner | c30598b | 2006-12-06 01:18:01 +0000 | [diff] [blame] | 35 | #include "llvm/Support/ManagedStatic.h" |
Chris Lattner | 45e0f89 | 2007-04-29 08:12:22 +0000 | [diff] [blame] | 36 | #include "llvm/Support/MemoryBuffer.h" |
Chris Lattner | cc14d25 | 2009-03-06 05:34:10 +0000 | [diff] [blame] | 37 | #include "llvm/Support/PrettyStackTrace.h" |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 38 | #include "llvm/System/Signals.h" |
Chris Lattner | 44dadff | 2007-05-06 09:29:57 +0000 | [diff] [blame] | 39 | #include <map> |
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 | f9a3ec8 | 2009-04-26 22:21:57 +0000 | [diff] [blame] | 73 | static const char *GetBlockName(unsigned BlockID, |
| 74 | const BitstreamReader &StreamFile) { |
Chris Lattner | 1772b12 | 2007-05-05 00:17:42 +0000 | [diff] [blame] | 75 | // Standard blocks for all bitcode files. |
| 76 | if (BlockID < bitc::FIRST_APPLICATION_BLOCKID) { |
| 77 | if (BlockID == bitc::BLOCKINFO_BLOCK_ID) |
| 78 | return "BLOCKINFO_BLOCK"; |
| 79 | return 0; |
| 80 | } |
| 81 | |
Chris Lattner | f9a3ec8 | 2009-04-26 22:21:57 +0000 | [diff] [blame] | 82 | // Check to see if we have a blockinfo record for this block, with a name. |
| 83 | if (const BitstreamReader::BlockInfo *Info = |
| 84 | StreamFile.getBlockInfo(BlockID)) { |
| 85 | if (!Info->Name.empty()) |
| 86 | return Info->Name.c_str(); |
| 87 | } |
| 88 | |
| 89 | |
Chris Lattner | b30c925 | 2007-04-29 21:48:19 +0000 | [diff] [blame] | 90 | if (CurStreamType != LLVMIRBitstream) return 0; |
Chris Lattner | 4238d47 | 2007-04-29 20:00:02 +0000 | [diff] [blame] | 91 | |
| 92 | switch (BlockID) { |
Chris Lattner | b30c925 | 2007-04-29 21:48:19 +0000 | [diff] [blame] | 93 | default: return 0; |
Chris Lattner | 4238d47 | 2007-04-29 20:00:02 +0000 | [diff] [blame] | 94 | case bitc::MODULE_BLOCK_ID: return "MODULE_BLOCK"; |
Chris Lattner | cd5b7d7 | 2007-05-04 03:01:41 +0000 | [diff] [blame] | 95 | case bitc::PARAMATTR_BLOCK_ID: return "PARAMATTR_BLOCK"; |
Chris Lattner | 4238d47 | 2007-04-29 20:00:02 +0000 | [diff] [blame] | 96 | case bitc::TYPE_BLOCK_ID: return "TYPE_BLOCK"; |
| 97 | case bitc::CONSTANTS_BLOCK_ID: return "CONSTANTS_BLOCK"; |
| 98 | case bitc::FUNCTION_BLOCK_ID: return "FUNCTION_BLOCK"; |
| 99 | case bitc::TYPE_SYMTAB_BLOCK_ID: return "TYPE_SYMTAB"; |
| 100 | case bitc::VALUE_SYMTAB_BLOCK_ID: return "VALUE_SYMTAB"; |
Devang Patel | e54abc9 | 2009-07-22 17:43:22 +0000 | [diff] [blame] | 101 | case bitc::METADATA_BLOCK_ID: return "METADATA_BLOCK"; |
Chris Lattner | 4238d47 | 2007-04-29 20:00:02 +0000 | [diff] [blame] | 102 | } |
| 103 | } |
| 104 | |
Chris Lattner | b30c925 | 2007-04-29 21:48:19 +0000 | [diff] [blame] | 105 | /// GetCodeName - Return a symbolic code name if known, otherwise return |
| 106 | /// null. |
Chris Lattner | f9a3ec8 | 2009-04-26 22:21:57 +0000 | [diff] [blame] | 107 | static const char *GetCodeName(unsigned CodeID, unsigned BlockID, |
| 108 | const BitstreamReader &StreamFile) { |
Chris Lattner | 1772b12 | 2007-05-05 00:17:42 +0000 | [diff] [blame] | 109 | // Standard blocks for all bitcode files. |
| 110 | if (BlockID < bitc::FIRST_APPLICATION_BLOCKID) { |
| 111 | if (BlockID == bitc::BLOCKINFO_BLOCK_ID) { |
| 112 | switch (CodeID) { |
| 113 | default: return 0; |
Chris Lattner | f9a3ec8 | 2009-04-26 22:21:57 +0000 | [diff] [blame] | 114 | case bitc::BLOCKINFO_CODE_SETBID: return "SETBID"; |
| 115 | case bitc::BLOCKINFO_CODE_BLOCKNAME: return "BLOCKNAME"; |
| 116 | case bitc::BLOCKINFO_CODE_SETRECORDNAME: return "SETRECORDNAME"; |
Chris Lattner | 1772b12 | 2007-05-05 00:17:42 +0000 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | return 0; |
| 120 | } |
| 121 | |
Chris Lattner | f9a3ec8 | 2009-04-26 22:21:57 +0000 | [diff] [blame] | 122 | // Check to see if we have a blockinfo record for this record, with a name. |
| 123 | if (const BitstreamReader::BlockInfo *Info = |
| 124 | StreamFile.getBlockInfo(BlockID)) { |
| 125 | for (unsigned i = 0, e = Info->RecordNames.size(); i != e; ++i) |
| 126 | if (Info->RecordNames[i].first == CodeID) |
| 127 | return Info->RecordNames[i].second.c_str(); |
| 128 | } |
| 129 | |
| 130 | |
Chris Lattner | b30c925 | 2007-04-29 21:48:19 +0000 | [diff] [blame] | 131 | if (CurStreamType != LLVMIRBitstream) return 0; |
| 132 | |
| 133 | switch (BlockID) { |
| 134 | default: return 0; |
| 135 | case bitc::MODULE_BLOCK_ID: |
| 136 | switch (CodeID) { |
| 137 | default: return 0; |
| 138 | case bitc::MODULE_CODE_VERSION: return "VERSION"; |
| 139 | case bitc::MODULE_CODE_TRIPLE: return "TRIPLE"; |
| 140 | case bitc::MODULE_CODE_DATALAYOUT: return "DATALAYOUT"; |
| 141 | case bitc::MODULE_CODE_ASM: return "ASM"; |
| 142 | case bitc::MODULE_CODE_SECTIONNAME: return "SECTIONNAME"; |
| 143 | case bitc::MODULE_CODE_DEPLIB: return "DEPLIB"; |
| 144 | case bitc::MODULE_CODE_GLOBALVAR: return "GLOBALVAR"; |
| 145 | case bitc::MODULE_CODE_FUNCTION: return "FUNCTION"; |
| 146 | case bitc::MODULE_CODE_ALIAS: return "ALIAS"; |
| 147 | case bitc::MODULE_CODE_PURGEVALS: return "PURGEVALS"; |
Nick Lewycky | 82b80d9 | 2008-11-07 14:52:51 +0000 | [diff] [blame] | 148 | case bitc::MODULE_CODE_GCNAME: return "GCNAME"; |
Chris Lattner | b30c925 | 2007-04-29 21:48:19 +0000 | [diff] [blame] | 149 | } |
Chris Lattner | cd5b7d7 | 2007-05-04 03:01:41 +0000 | [diff] [blame] | 150 | case bitc::PARAMATTR_BLOCK_ID: |
| 151 | switch (CodeID) { |
| 152 | default: return 0; |
| 153 | case bitc::PARAMATTR_CODE_ENTRY: return "ENTRY"; |
| 154 | } |
Chris Lattner | b30c925 | 2007-04-29 21:48:19 +0000 | [diff] [blame] | 155 | case bitc::TYPE_BLOCK_ID: |
| 156 | switch (CodeID) { |
| 157 | default: return 0; |
Nick Lewycky | 82b80d9 | 2008-11-07 14:52:51 +0000 | [diff] [blame] | 158 | case bitc::TYPE_CODE_NUMENTRY: return "NUMENTRY"; |
| 159 | case bitc::TYPE_CODE_VOID: return "VOID"; |
| 160 | case bitc::TYPE_CODE_FLOAT: return "FLOAT"; |
| 161 | case bitc::TYPE_CODE_DOUBLE: return "DOUBLE"; |
| 162 | case bitc::TYPE_CODE_LABEL: return "LABEL"; |
| 163 | case bitc::TYPE_CODE_OPAQUE: return "OPAQUE"; |
| 164 | case bitc::TYPE_CODE_INTEGER: return "INTEGER"; |
| 165 | case bitc::TYPE_CODE_POINTER: return "POINTER"; |
| 166 | case bitc::TYPE_CODE_FUNCTION: return "FUNCTION"; |
| 167 | case bitc::TYPE_CODE_STRUCT: return "STRUCT"; |
| 168 | case bitc::TYPE_CODE_ARRAY: return "ARRAY"; |
| 169 | case bitc::TYPE_CODE_VECTOR: return "VECTOR"; |
| 170 | case bitc::TYPE_CODE_X86_FP80: return "X86_FP80"; |
| 171 | case bitc::TYPE_CODE_FP128: return "FP128"; |
| 172 | case bitc::TYPE_CODE_PPC_FP128: return "PPC_FP128"; |
Nick Lewycky | 079c034 | 2009-06-01 04:41:03 +0000 | [diff] [blame] | 173 | case bitc::TYPE_CODE_METADATA: return "METADATA"; |
Chris Lattner | b30c925 | 2007-04-29 21:48:19 +0000 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | case bitc::CONSTANTS_BLOCK_ID: |
| 177 | switch (CodeID) { |
| 178 | default: return 0; |
| 179 | case bitc::CST_CODE_SETTYPE: return "SETTYPE"; |
| 180 | case bitc::CST_CODE_NULL: return "NULL"; |
| 181 | case bitc::CST_CODE_UNDEF: return "UNDEF"; |
| 182 | case bitc::CST_CODE_INTEGER: return "INTEGER"; |
| 183 | case bitc::CST_CODE_WIDE_INTEGER: return "WIDE_INTEGER"; |
| 184 | case bitc::CST_CODE_FLOAT: return "FLOAT"; |
| 185 | case bitc::CST_CODE_AGGREGATE: return "AGGREGATE"; |
Chris Lattner | cb3d91b | 2007-05-06 00:53:07 +0000 | [diff] [blame] | 186 | case bitc::CST_CODE_STRING: return "STRING"; |
| 187 | case bitc::CST_CODE_CSTRING: return "CSTRING"; |
Chris Lattner | b30c925 | 2007-04-29 21:48:19 +0000 | [diff] [blame] | 188 | case bitc::CST_CODE_CE_BINOP: return "CE_BINOP"; |
| 189 | case bitc::CST_CODE_CE_CAST: return "CE_CAST"; |
| 190 | case bitc::CST_CODE_CE_GEP: return "CE_GEP"; |
| 191 | case bitc::CST_CODE_CE_SELECT: return "CE_SELECT"; |
| 192 | case bitc::CST_CODE_CE_EXTRACTELT: return "CE_EXTRACTELT"; |
| 193 | case bitc::CST_CODE_CE_INSERTELT: return "CE_INSERTELT"; |
| 194 | case bitc::CST_CODE_CE_SHUFFLEVEC: return "CE_SHUFFLEVEC"; |
| 195 | case bitc::CST_CODE_CE_CMP: return "CE_CMP"; |
Chris Lattner | c5ff2cc | 2007-05-06 01:50:11 +0000 | [diff] [blame] | 196 | case bitc::CST_CODE_INLINEASM: return "INLINEASM"; |
Nick Lewycky | 079c034 | 2009-06-01 04:41:03 +0000 | [diff] [blame] | 197 | case bitc::CST_CODE_CE_SHUFVEC_EX: return "CE_SHUFVEC_EX"; |
Chris Lattner | b30c925 | 2007-04-29 21:48:19 +0000 | [diff] [blame] | 198 | } |
| 199 | case bitc::FUNCTION_BLOCK_ID: |
| 200 | switch (CodeID) { |
| 201 | default: return 0; |
| 202 | case bitc::FUNC_CODE_DECLAREBLOCKS: return "DECLAREBLOCKS"; |
| 203 | |
| 204 | case bitc::FUNC_CODE_INST_BINOP: return "INST_BINOP"; |
| 205 | case bitc::FUNC_CODE_INST_CAST: return "INST_CAST"; |
| 206 | case bitc::FUNC_CODE_INST_GEP: return "INST_GEP"; |
| 207 | case bitc::FUNC_CODE_INST_SELECT: return "INST_SELECT"; |
| 208 | case bitc::FUNC_CODE_INST_EXTRACTELT: return "INST_EXTRACTELT"; |
| 209 | case bitc::FUNC_CODE_INST_INSERTELT: return "INST_INSERTELT"; |
| 210 | case bitc::FUNC_CODE_INST_SHUFFLEVEC: return "INST_SHUFFLEVEC"; |
| 211 | case bitc::FUNC_CODE_INST_CMP: return "INST_CMP"; |
| 212 | |
| 213 | case bitc::FUNC_CODE_INST_RET: return "INST_RET"; |
| 214 | case bitc::FUNC_CODE_INST_BR: return "INST_BR"; |
| 215 | case bitc::FUNC_CODE_INST_SWITCH: return "INST_SWITCH"; |
| 216 | case bitc::FUNC_CODE_INST_INVOKE: return "INST_INVOKE"; |
| 217 | case bitc::FUNC_CODE_INST_UNWIND: return "INST_UNWIND"; |
| 218 | case bitc::FUNC_CODE_INST_UNREACHABLE: return "INST_UNREACHABLE"; |
| 219 | |
Chris Lattner | 8f92668 | 2007-05-01 02:43:46 +0000 | [diff] [blame] | 220 | case bitc::FUNC_CODE_INST_PHI: return "INST_PHI"; |
Chris Lattner | b30c925 | 2007-04-29 21:48:19 +0000 | [diff] [blame] | 221 | case bitc::FUNC_CODE_INST_MALLOC: return "INST_MALLOC"; |
| 222 | case bitc::FUNC_CODE_INST_FREE: return "INST_FREE"; |
| 223 | case bitc::FUNC_CODE_INST_ALLOCA: return "INST_ALLOCA"; |
| 224 | case bitc::FUNC_CODE_INST_LOAD: return "INST_LOAD"; |
| 225 | case bitc::FUNC_CODE_INST_STORE: return "INST_STORE"; |
| 226 | case bitc::FUNC_CODE_INST_CALL: return "INST_CALL"; |
| 227 | case bitc::FUNC_CODE_INST_VAARG: return "INST_VAARG"; |
Christopher Lamb | fe63fb9 | 2007-12-11 08:59:05 +0000 | [diff] [blame] | 228 | case bitc::FUNC_CODE_INST_STORE2: return "INST_STORE2"; |
Nick Lewycky | 33a834a | 2008-03-01 21:47:06 +0000 | [diff] [blame] | 229 | case bitc::FUNC_CODE_INST_GETRESULT: return "INST_GETRESULT"; |
Nick Lewycky | 82b80d9 | 2008-11-07 14:52:51 +0000 | [diff] [blame] | 230 | case bitc::FUNC_CODE_INST_EXTRACTVAL: return "INST_EXTRACTVAL"; |
| 231 | case bitc::FUNC_CODE_INST_INSERTVAL: return "INST_INSERTVAL"; |
| 232 | case bitc::FUNC_CODE_INST_CMP2: return "INST_CMP2"; |
| 233 | case bitc::FUNC_CODE_INST_VSELECT: return "INST_VSELECT"; |
Chris Lattner | b30c925 | 2007-04-29 21:48:19 +0000 | [diff] [blame] | 234 | } |
| 235 | case bitc::TYPE_SYMTAB_BLOCK_ID: |
| 236 | switch (CodeID) { |
| 237 | default: return 0; |
| 238 | case bitc::TST_CODE_ENTRY: return "ENTRY"; |
| 239 | } |
| 240 | case bitc::VALUE_SYMTAB_BLOCK_ID: |
| 241 | switch (CodeID) { |
| 242 | default: return 0; |
| 243 | case bitc::VST_CODE_ENTRY: return "ENTRY"; |
Chris Lattner | cd5b7d7 | 2007-05-04 03:01:41 +0000 | [diff] [blame] | 244 | case bitc::VST_CODE_BBENTRY: return "BBENTRY"; |
Chris Lattner | b30c925 | 2007-04-29 21:48:19 +0000 | [diff] [blame] | 245 | } |
Devang Patel | e54abc9 | 2009-07-22 17:43:22 +0000 | [diff] [blame] | 246 | case bitc::METADATA_BLOCK_ID: |
| 247 | switch(CodeID) { |
| 248 | default:return 0; |
| 249 | case bitc::METADATA_STRING: return "MDSTRING"; |
Devang Patel | 104cf9e | 2009-07-23 01:07:34 +0000 | [diff] [blame^] | 250 | case bitc::METADATA_NODE: return "MDNODE"; |
Devang Patel | e54abc9 | 2009-07-22 17:43:22 +0000 | [diff] [blame] | 251 | } |
Chris Lattner | b30c925 | 2007-04-29 21:48:19 +0000 | [diff] [blame] | 252 | } |
| 253 | } |
| 254 | |
Chris Lattner | 2443747 | 2009-04-27 17:59:34 +0000 | [diff] [blame] | 255 | struct PerRecordStats { |
| 256 | unsigned NumInstances; |
Chris Lattner | c167cac | 2009-04-27 18:15:27 +0000 | [diff] [blame] | 257 | unsigned NumAbbrev; |
| 258 | uint64_t TotalBits; |
| 259 | |
| 260 | PerRecordStats() : NumInstances(0), NumAbbrev(0), TotalBits(0) {} |
Chris Lattner | 2443747 | 2009-04-27 17:59:34 +0000 | [diff] [blame] | 261 | }; |
Chris Lattner | 4238d47 | 2007-04-29 20:00:02 +0000 | [diff] [blame] | 262 | |
| 263 | struct PerBlockIDStats { |
| 264 | /// NumInstances - This the number of times this block ID has been seen. |
| 265 | unsigned NumInstances; |
| 266 | |
| 267 | /// NumBits - The total size in bits of all of these blocks. |
| 268 | uint64_t NumBits; |
| 269 | |
| 270 | /// NumSubBlocks - The total number of blocks these blocks contain. |
| 271 | unsigned NumSubBlocks; |
| 272 | |
| 273 | /// NumAbbrevs - The total number of abbreviations. |
| 274 | unsigned NumAbbrevs; |
| 275 | |
| 276 | /// NumRecords - The total number of records these blocks contain, and the |
| 277 | /// number that are abbreviated. |
| 278 | unsigned NumRecords, NumAbbreviatedRecords; |
| 279 | |
Chris Lattner | b1e85b5 | 2007-05-05 01:46:49 +0000 | [diff] [blame] | 280 | /// CodeFreq - Keep track of the number of times we see each code. |
Chris Lattner | 2443747 | 2009-04-27 17:59:34 +0000 | [diff] [blame] | 281 | std::vector<PerRecordStats> CodeFreq; |
Chris Lattner | b1e85b5 | 2007-05-05 01:46:49 +0000 | [diff] [blame] | 282 | |
Chris Lattner | 4238d47 | 2007-04-29 20:00:02 +0000 | [diff] [blame] | 283 | PerBlockIDStats() |
| 284 | : NumInstances(0), NumBits(0), |
| 285 | NumSubBlocks(0), NumAbbrevs(0), NumRecords(0), NumAbbreviatedRecords(0) {} |
| 286 | }; |
| 287 | |
| 288 | static std::map<unsigned, PerBlockIDStats> BlockIDStats; |
| 289 | |
| 290 | |
| 291 | |
Chris Lattner | 32de633 | 2007-04-29 08:31:14 +0000 | [diff] [blame] | 292 | /// Error - All bitcode analysis errors go through this function, making this a |
| 293 | /// good place to breakpoint if debugging. |
| 294 | static bool Error(const std::string &Err) { |
Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 295 | errs() << Err << "\n"; |
Chris Lattner | 32de633 | 2007-04-29 08:31:14 +0000 | [diff] [blame] | 296 | return true; |
| 297 | } |
| 298 | |
| 299 | /// ParseBlock - Read a block, updating statistics, etc. |
Chris Lattner | 962dde3 | 2009-04-26 20:59:02 +0000 | [diff] [blame] | 300 | static bool ParseBlock(BitstreamCursor &Stream, unsigned IndentLevel) { |
Chris Lattner | 1772b12 | 2007-05-05 00:17:42 +0000 | [diff] [blame] | 301 | std::string Indent(IndentLevel*2, ' '); |
Chris Lattner | 4238d47 | 2007-04-29 20:00:02 +0000 | [diff] [blame] | 302 | uint64_t BlockBitStart = Stream.GetCurrentBitNo(); |
Chris Lattner | 32de633 | 2007-04-29 08:31:14 +0000 | [diff] [blame] | 303 | unsigned BlockID = Stream.ReadSubBlockID(); |
Chris Lattner | b30c925 | 2007-04-29 21:48:19 +0000 | [diff] [blame] | 304 | |
Chris Lattner | 4238d47 | 2007-04-29 20:00:02 +0000 | [diff] [blame] | 305 | // Get the statistics for this BlockID. |
| 306 | PerBlockIDStats &BlockStats = BlockIDStats[BlockID]; |
| 307 | |
| 308 | BlockStats.NumInstances++; |
Chris Lattner | 32de633 | 2007-04-29 08:31:14 +0000 | [diff] [blame] | 309 | |
Chris Lattner | 1772b12 | 2007-05-05 00:17:42 +0000 | [diff] [blame] | 310 | // BLOCKINFO is a special part of the stream. |
| 311 | if (BlockID == bitc::BLOCKINFO_BLOCK_ID) { |
Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 312 | if (Dump) errs() << Indent << "<BLOCKINFO_BLOCK/>\n"; |
Chris Lattner | 1772b12 | 2007-05-05 00:17:42 +0000 | [diff] [blame] | 313 | if (Stream.ReadBlockInfoBlock()) |
| 314 | return Error("Malformed BlockInfoBlock"); |
| 315 | uint64_t BlockBitEnd = Stream.GetCurrentBitNo(); |
| 316 | BlockStats.NumBits += BlockBitEnd-BlockBitStart; |
| 317 | return false; |
| 318 | } |
| 319 | |
Chris Lattner | b30c925 | 2007-04-29 21:48:19 +0000 | [diff] [blame] | 320 | unsigned NumWords = 0; |
Chris Lattner | 1772b12 | 2007-05-05 00:17:42 +0000 | [diff] [blame] | 321 | if (Stream.EnterSubBlock(BlockID, &NumWords)) |
Chris Lattner | 32de633 | 2007-04-29 08:31:14 +0000 | [diff] [blame] | 322 | return Error("Malformed block record"); |
| 323 | |
Chris Lattner | b30c925 | 2007-04-29 21:48:19 +0000 | [diff] [blame] | 324 | const char *BlockName = 0; |
| 325 | if (Dump) { |
Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 326 | errs() << Indent << "<"; |
Chris Lattner | f9a3ec8 | 2009-04-26 22:21:57 +0000 | [diff] [blame] | 327 | if ((BlockName = GetBlockName(BlockID, *Stream.getBitStreamReader()))) |
Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 328 | errs() << BlockName; |
Chris Lattner | b30c925 | 2007-04-29 21:48:19 +0000 | [diff] [blame] | 329 | else |
Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 330 | errs() << "UnknownBlock" << BlockID; |
Chris Lattner | b30c925 | 2007-04-29 21:48:19 +0000 | [diff] [blame] | 331 | |
| 332 | if (NonSymbolic && BlockName) |
Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 333 | errs() << " BlockID=" << BlockID; |
Chris Lattner | b30c925 | 2007-04-29 21:48:19 +0000 | [diff] [blame] | 334 | |
Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 335 | errs() << " NumWords=" << NumWords |
| 336 | << " BlockCodeSize=" << Stream.GetAbbrevIDWidth() << ">\n"; |
Chris Lattner | b30c925 | 2007-04-29 21:48:19 +0000 | [diff] [blame] | 337 | } |
| 338 | |
Chris Lattner | 32de633 | 2007-04-29 08:31:14 +0000 | [diff] [blame] | 339 | SmallVector<uint64_t, 64> Record; |
| 340 | |
| 341 | // Read all the records for this block. |
| 342 | while (1) { |
| 343 | if (Stream.AtEndOfStream()) |
| 344 | return Error("Premature end of bitstream"); |
| 345 | |
Chris Lattner | c167cac | 2009-04-27 18:15:27 +0000 | [diff] [blame] | 346 | uint64_t RecordStartBit = Stream.GetCurrentBitNo(); |
| 347 | |
Chris Lattner | 32de633 | 2007-04-29 08:31:14 +0000 | [diff] [blame] | 348 | // Read the code for this record. |
| 349 | unsigned AbbrevID = Stream.ReadCode(); |
| 350 | switch (AbbrevID) { |
Chris Lattner | 4238d47 | 2007-04-29 20:00:02 +0000 | [diff] [blame] | 351 | case bitc::END_BLOCK: { |
Chris Lattner | 32de633 | 2007-04-29 08:31:14 +0000 | [diff] [blame] | 352 | if (Stream.ReadBlockEnd()) |
| 353 | return Error("Error at end of block"); |
Chris Lattner | 4238d47 | 2007-04-29 20:00:02 +0000 | [diff] [blame] | 354 | uint64_t BlockBitEnd = Stream.GetCurrentBitNo(); |
| 355 | BlockStats.NumBits += BlockBitEnd-BlockBitStart; |
Chris Lattner | b30c925 | 2007-04-29 21:48:19 +0000 | [diff] [blame] | 356 | if (Dump) { |
Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 357 | errs() << Indent << "</"; |
Chris Lattner | b30c925 | 2007-04-29 21:48:19 +0000 | [diff] [blame] | 358 | if (BlockName) |
Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 359 | errs() << BlockName << ">\n"; |
Chris Lattner | b30c925 | 2007-04-29 21:48:19 +0000 | [diff] [blame] | 360 | else |
Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 361 | errs() << "UnknownBlock" << BlockID << ">\n"; |
Chris Lattner | b30c925 | 2007-04-29 21:48:19 +0000 | [diff] [blame] | 362 | } |
Chris Lattner | 32de633 | 2007-04-29 08:31:14 +0000 | [diff] [blame] | 363 | return false; |
Chris Lattner | 4238d47 | 2007-04-29 20:00:02 +0000 | [diff] [blame] | 364 | } |
Chris Lattner | 44b0f10 | 2007-05-05 01:29:31 +0000 | [diff] [blame] | 365 | case bitc::ENTER_SUBBLOCK: { |
| 366 | uint64_t SubBlockBitStart = Stream.GetCurrentBitNo(); |
Chris Lattner | b30c925 | 2007-04-29 21:48:19 +0000 | [diff] [blame] | 367 | if (ParseBlock(Stream, IndentLevel+1)) |
Chris Lattner | 32de633 | 2007-04-29 08:31:14 +0000 | [diff] [blame] | 368 | return true; |
Chris Lattner | 4238d47 | 2007-04-29 20:00:02 +0000 | [diff] [blame] | 369 | ++BlockStats.NumSubBlocks; |
Chris Lattner | 44b0f10 | 2007-05-05 01:29:31 +0000 | [diff] [blame] | 370 | uint64_t SubBlockBitEnd = Stream.GetCurrentBitNo(); |
| 371 | |
| 372 | // Don't include subblock sizes in the size of this block. |
| 373 | BlockBitStart += SubBlockBitEnd-SubBlockBitStart; |
Chris Lattner | 32de633 | 2007-04-29 08:31:14 +0000 | [diff] [blame] | 374 | break; |
Chris Lattner | 44b0f10 | 2007-05-05 01:29:31 +0000 | [diff] [blame] | 375 | } |
Chris Lattner | 32de633 | 2007-04-29 08:31:14 +0000 | [diff] [blame] | 376 | case bitc::DEFINE_ABBREV: |
| 377 | Stream.ReadAbbrevRecord(); |
Chris Lattner | 4238d47 | 2007-04-29 20:00:02 +0000 | [diff] [blame] | 378 | ++BlockStats.NumAbbrevs; |
Chris Lattner | 32de633 | 2007-04-29 08:31:14 +0000 | [diff] [blame] | 379 | break; |
| 380 | default: |
| 381 | Record.clear(); |
Chris Lattner | 3f75d31 | 2009-04-06 22:44:40 +0000 | [diff] [blame] | 382 | |
| 383 | ++BlockStats.NumRecords; |
Chris Lattner | ae7dd80 | 2009-04-07 02:56:46 +0000 | [diff] [blame] | 384 | if (AbbrevID != bitc::UNABBREV_RECORD) |
Chris Lattner | 3f75d31 | 2009-04-06 22:44:40 +0000 | [diff] [blame] | 385 | ++BlockStats.NumAbbreviatedRecords; |
Chris Lattner | 3f75d31 | 2009-04-06 22:44:40 +0000 | [diff] [blame] | 386 | |
Chris Lattner | 3f75d31 | 2009-04-06 22:44:40 +0000 | [diff] [blame] | 387 | const char *BlobStart = 0; |
| 388 | unsigned BlobLen = 0; |
Chris Lattner | ae7dd80 | 2009-04-07 02:56:46 +0000 | [diff] [blame] | 389 | unsigned Code = Stream.ReadRecord(AbbrevID, Record, BlobStart, BlobLen); |
Chris Lattner | b1e85b5 | 2007-05-05 01:46:49 +0000 | [diff] [blame] | 390 | |
Chris Lattner | c167cac | 2009-04-27 18:15:27 +0000 | [diff] [blame] | 391 | |
| 392 | |
Chris Lattner | b1e85b5 | 2007-05-05 01:46:49 +0000 | [diff] [blame] | 393 | // Increment the # occurrences of this code. |
| 394 | if (BlockStats.CodeFreq.size() <= Code) |
| 395 | BlockStats.CodeFreq.resize(Code+1); |
Chris Lattner | 2443747 | 2009-04-27 17:59:34 +0000 | [diff] [blame] | 396 | BlockStats.CodeFreq[Code].NumInstances++; |
Chris Lattner | c167cac | 2009-04-27 18:15:27 +0000 | [diff] [blame] | 397 | BlockStats.CodeFreq[Code].TotalBits += |
| 398 | Stream.GetCurrentBitNo()-RecordStartBit; |
| 399 | if (AbbrevID != bitc::UNABBREV_RECORD) |
| 400 | BlockStats.CodeFreq[Code].NumAbbrev++; |
| 401 | |
Chris Lattner | b30c925 | 2007-04-29 21:48:19 +0000 | [diff] [blame] | 402 | if (Dump) { |
Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 403 | errs() << Indent << " <"; |
Chris Lattner | f9a3ec8 | 2009-04-26 22:21:57 +0000 | [diff] [blame] | 404 | if (const char *CodeName = |
| 405 | GetCodeName(Code, BlockID, *Stream.getBitStreamReader())) |
Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 406 | errs() << CodeName; |
Chris Lattner | b30c925 | 2007-04-29 21:48:19 +0000 | [diff] [blame] | 407 | else |
Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 408 | errs() << "UnknownCode" << Code; |
Chris Lattner | f9a3ec8 | 2009-04-26 22:21:57 +0000 | [diff] [blame] | 409 | if (NonSymbolic && |
| 410 | GetCodeName(Code, BlockID, *Stream.getBitStreamReader())) |
Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 411 | errs() << " codeid=" << Code; |
Chris Lattner | b30c925 | 2007-04-29 21:48:19 +0000 | [diff] [blame] | 412 | if (AbbrevID != bitc::UNABBREV_RECORD) |
Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 413 | errs() << " abbrevid=" << AbbrevID; |
Chris Lattner | b30c925 | 2007-04-29 21:48:19 +0000 | [diff] [blame] | 414 | |
| 415 | for (unsigned i = 0, e = Record.size(); i != e; ++i) |
Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 416 | errs() << " op" << i << "=" << (int64_t)Record[i]; |
Chris Lattner | b30c925 | 2007-04-29 21:48:19 +0000 | [diff] [blame] | 417 | |
Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 418 | errs() << "/>"; |
Chris Lattner | ae7dd80 | 2009-04-07 02:56:46 +0000 | [diff] [blame] | 419 | |
| 420 | if (BlobStart) { |
Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 421 | errs() << " blob data = "; |
Chris Lattner | ae7dd80 | 2009-04-07 02:56:46 +0000 | [diff] [blame] | 422 | bool BlobIsPrintable = true; |
| 423 | for (unsigned i = 0; i != BlobLen; ++i) |
| 424 | if (!isprint(BlobStart[i])) { |
| 425 | BlobIsPrintable = false; |
| 426 | break; |
| 427 | } |
| 428 | |
| 429 | if (BlobIsPrintable) |
Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 430 | errs() << "'" << std::string(BlobStart, BlobStart+BlobLen) <<"'"; |
Chris Lattner | ae7dd80 | 2009-04-07 02:56:46 +0000 | [diff] [blame] | 431 | else |
Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 432 | errs() << "unprintable, " << BlobLen << " bytes."; |
Chris Lattner | ae7dd80 | 2009-04-07 02:56:46 +0000 | [diff] [blame] | 433 | } |
| 434 | |
Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 435 | errs() << "\n"; |
Chris Lattner | b30c925 | 2007-04-29 21:48:19 +0000 | [diff] [blame] | 436 | } |
| 437 | |
Chris Lattner | 32de633 | 2007-04-29 08:31:14 +0000 | [diff] [blame] | 438 | break; |
| 439 | } |
| 440 | } |
| 441 | } |
| 442 | |
Chris Lattner | 4238d47 | 2007-04-29 20:00:02 +0000 | [diff] [blame] | 443 | static void PrintSize(double Bits) { |
Chris Lattner | 2443747 | 2009-04-27 17:59:34 +0000 | [diff] [blame] | 444 | fprintf(stderr, "%.2f/%.2fB/%lluW", Bits, Bits/8,(unsigned long long)Bits/32); |
| 445 | } |
| 446 | static void PrintSize(uint64_t Bits) { |
| 447 | fprintf(stderr, "%llub/%.2fB/%lluW", (unsigned long long)Bits, |
| 448 | (double)Bits/8, (unsigned long long)Bits/32); |
Chris Lattner | 4238d47 | 2007-04-29 20:00:02 +0000 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | |
Chris Lattner | 45e0f89 | 2007-04-29 08:12:22 +0000 | [diff] [blame] | 452 | /// AnalyzeBitcode - Analyze the bitcode file specified by InputFilename. |
| 453 | static int AnalyzeBitcode() { |
| 454 | // Read the input file. |
Chris Lattner | e2a466b | 2009-04-06 20:54:32 +0000 | [diff] [blame] | 455 | MemoryBuffer *MemBuf = MemoryBuffer::getFileOrSTDIN(InputFilename.c_str()); |
Chris Lattner | 45e0f89 | 2007-04-29 08:12:22 +0000 | [diff] [blame] | 456 | |
Chris Lattner | e2a466b | 2009-04-06 20:54:32 +0000 | [diff] [blame] | 457 | if (MemBuf == 0) |
Chris Lattner | 32de633 | 2007-04-29 08:31:14 +0000 | [diff] [blame] | 458 | return Error("Error reading '" + InputFilename + "'."); |
Chris Lattner | 45e0f89 | 2007-04-29 08:12:22 +0000 | [diff] [blame] | 459 | |
Chris Lattner | e2a466b | 2009-04-06 20:54:32 +0000 | [diff] [blame] | 460 | if (MemBuf->getBufferSize() & 3) |
Chris Lattner | 32de633 | 2007-04-29 08:31:14 +0000 | [diff] [blame] | 461 | 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] | 462 | |
Chris Lattner | e2a466b | 2009-04-06 20:54:32 +0000 | [diff] [blame] | 463 | unsigned char *BufPtr = (unsigned char *)MemBuf->getBufferStart(); |
| 464 | unsigned char *EndBufPtr = BufPtr+MemBuf->getBufferSize(); |
| 465 | |
| 466 | // If we have a wrapper header, parse it and ignore the non-bc file contents. |
| 467 | // The magic number is 0x0B17C0DE stored in little endian. |
| 468 | if (isBitcodeWrapper(BufPtr, EndBufPtr)) |
| 469 | if (SkipBitcodeWrapperHeader(BufPtr, EndBufPtr)) |
| 470 | return Error("Invalid bitcode wrapper header"); |
| 471 | |
Chris Lattner | 962dde3 | 2009-04-26 20:59:02 +0000 | [diff] [blame] | 472 | BitstreamReader StreamFile(BufPtr, EndBufPtr); |
| 473 | BitstreamCursor Stream(StreamFile); |
Chris Lattner | 0370cc6 | 2009-04-27 20:04:08 +0000 | [diff] [blame] | 474 | StreamFile.CollectBlockInfoNames(); |
Chris Lattner | 45e0f89 | 2007-04-29 08:12:22 +0000 | [diff] [blame] | 475 | |
| 476 | // Read the stream signature. |
| 477 | char Signature[6]; |
| 478 | Signature[0] = Stream.Read(8); |
| 479 | Signature[1] = Stream.Read(8); |
| 480 | Signature[2] = Stream.Read(4); |
| 481 | Signature[3] = Stream.Read(4); |
| 482 | Signature[4] = Stream.Read(4); |
| 483 | Signature[5] = Stream.Read(4); |
| 484 | |
Chris Lattner | 32de633 | 2007-04-29 08:31:14 +0000 | [diff] [blame] | 485 | // Autodetect the file contents, if it is one we know. |
Chris Lattner | 45e0f89 | 2007-04-29 08:12:22 +0000 | [diff] [blame] | 486 | CurStreamType = UnknownBitstream; |
| 487 | if (Signature[0] == 'B' && Signature[1] == 'C' && |
| 488 | Signature[2] == 0x0 && Signature[3] == 0xC && |
| 489 | Signature[4] == 0xE && Signature[5] == 0xD) |
| 490 | CurStreamType = LLVMIRBitstream; |
| 491 | |
Chris Lattner | 4238d47 | 2007-04-29 20:00:02 +0000 | [diff] [blame] | 492 | unsigned NumTopBlocks = 0; |
| 493 | |
Chris Lattner | 32de633 | 2007-04-29 08:31:14 +0000 | [diff] [blame] | 494 | // Parse the top-level structure. We only allow blocks at the top-level. |
| 495 | while (!Stream.AtEndOfStream()) { |
| 496 | unsigned Code = Stream.ReadCode(); |
| 497 | if (Code != bitc::ENTER_SUBBLOCK) |
| 498 | return Error("Invalid record at top-level"); |
| 499 | |
Chris Lattner | b30c925 | 2007-04-29 21:48:19 +0000 | [diff] [blame] | 500 | if (ParseBlock(Stream, 0)) |
Chris Lattner | 32de633 | 2007-04-29 08:31:14 +0000 | [diff] [blame] | 501 | return true; |
Chris Lattner | 4238d47 | 2007-04-29 20:00:02 +0000 | [diff] [blame] | 502 | ++NumTopBlocks; |
Chris Lattner | 32de633 | 2007-04-29 08:31:14 +0000 | [diff] [blame] | 503 | } |
| 504 | |
Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 505 | if (Dump) errs() << "\n\n"; |
Chris Lattner | b30c925 | 2007-04-29 21:48:19 +0000 | [diff] [blame] | 506 | |
Chris Lattner | e2a466b | 2009-04-06 20:54:32 +0000 | [diff] [blame] | 507 | uint64_t BufferSizeBits = (EndBufPtr-BufPtr)*CHAR_BIT; |
Chris Lattner | 32de633 | 2007-04-29 08:31:14 +0000 | [diff] [blame] | 508 | // Print a summary of the read file. |
Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 509 | errs() << "Summary of " << InputFilename << ":\n"; |
| 510 | errs() << " Total size: "; |
Chris Lattner | 8f92668 | 2007-05-01 02:43:46 +0000 | [diff] [blame] | 511 | PrintSize(BufferSizeBits); |
Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 512 | errs() << "\n"; |
| 513 | errs() << " Stream type: "; |
Chris Lattner | 45e0f89 | 2007-04-29 08:12:22 +0000 | [diff] [blame] | 514 | switch (CurStreamType) { |
| 515 | default: assert(0 && "Unknown bitstream type"); |
Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 516 | case UnknownBitstream: errs() << "unknown\n"; break; |
| 517 | case LLVMIRBitstream: errs() << "LLVM IR\n"; break; |
Chris Lattner | 45e0f89 | 2007-04-29 08:12:22 +0000 | [diff] [blame] | 518 | } |
Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 519 | errs() << " # Toplevel Blocks: " << NumTopBlocks << "\n"; |
| 520 | errs() << "\n"; |
Chris Lattner | 4238d47 | 2007-04-29 20:00:02 +0000 | [diff] [blame] | 521 | |
| 522 | // Emit per-block stats. |
Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 523 | errs() << "Per-block Summary:\n"; |
Chris Lattner | 4238d47 | 2007-04-29 20:00:02 +0000 | [diff] [blame] | 524 | for (std::map<unsigned, PerBlockIDStats>::iterator I = BlockIDStats.begin(), |
| 525 | E = BlockIDStats.end(); I != E; ++I) { |
Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 526 | errs() << " Block ID #" << I->first; |
Chris Lattner | f9a3ec8 | 2009-04-26 22:21:57 +0000 | [diff] [blame] | 527 | if (const char *BlockName = GetBlockName(I->first, StreamFile)) |
Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 528 | errs() << " (" << BlockName << ")"; |
| 529 | errs() << ":\n"; |
Chris Lattner | 4238d47 | 2007-04-29 20:00:02 +0000 | [diff] [blame] | 530 | |
| 531 | const PerBlockIDStats &Stats = I->second; |
Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 532 | errs() << " Num Instances: " << Stats.NumInstances << "\n"; |
| 533 | errs() << " Total Size: "; |
Chris Lattner | 4238d47 | 2007-04-29 20:00:02 +0000 | [diff] [blame] | 534 | PrintSize(Stats.NumBits); |
Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 535 | errs() << "\n"; |
| 536 | errs() << " % of file: " |
| 537 | << Stats.NumBits/(double)BufferSizeBits*100 << "\n"; |
Chris Lattner | b1e85b5 | 2007-05-05 01:46:49 +0000 | [diff] [blame] | 538 | if (Stats.NumInstances > 1) { |
Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 539 | errs() << " Average Size: "; |
Chris Lattner | b1e85b5 | 2007-05-05 01:46:49 +0000 | [diff] [blame] | 540 | PrintSize(Stats.NumBits/(double)Stats.NumInstances); |
Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 541 | errs() << "\n"; |
| 542 | errs() << " Tot/Avg SubBlocks: " << Stats.NumSubBlocks << "/" |
| 543 | << Stats.NumSubBlocks/(double)Stats.NumInstances << "\n"; |
| 544 | errs() << " Tot/Avg Abbrevs: " << Stats.NumAbbrevs << "/" |
| 545 | << Stats.NumAbbrevs/(double)Stats.NumInstances << "\n"; |
| 546 | errs() << " Tot/Avg Records: " << Stats.NumRecords << "/" |
| 547 | << Stats.NumRecords/(double)Stats.NumInstances << "\n"; |
Chris Lattner | b1e85b5 | 2007-05-05 01:46:49 +0000 | [diff] [blame] | 548 | } else { |
Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 549 | errs() << " Num SubBlocks: " << Stats.NumSubBlocks << "\n"; |
| 550 | errs() << " Num Abbrevs: " << Stats.NumAbbrevs << "\n"; |
| 551 | errs() << " Num Records: " << Stats.NumRecords << "\n"; |
Chris Lattner | b1e85b5 | 2007-05-05 01:46:49 +0000 | [diff] [blame] | 552 | } |
Chris Lattner | 1772b12 | 2007-05-05 00:17:42 +0000 | [diff] [blame] | 553 | if (Stats.NumRecords) |
Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 554 | errs() << " % Abbrev Recs: " << (Stats.NumAbbreviatedRecords/ |
| 555 | (double)Stats.NumRecords)*100 << "\n"; |
| 556 | errs() << "\n"; |
Chris Lattner | b1e85b5 | 2007-05-05 01:46:49 +0000 | [diff] [blame] | 557 | |
| 558 | // Print a histogram of the codes we see. |
| 559 | if (!NoHistogram && !Stats.CodeFreq.empty()) { |
| 560 | std::vector<std::pair<unsigned, unsigned> > FreqPairs; // <freq,code> |
| 561 | for (unsigned i = 0, e = Stats.CodeFreq.size(); i != e; ++i) |
Chris Lattner | 2443747 | 2009-04-27 17:59:34 +0000 | [diff] [blame] | 562 | if (unsigned Freq = Stats.CodeFreq[i].NumInstances) |
Chris Lattner | b1e85b5 | 2007-05-05 01:46:49 +0000 | [diff] [blame] | 563 | FreqPairs.push_back(std::make_pair(Freq, i)); |
| 564 | std::stable_sort(FreqPairs.begin(), FreqPairs.end()); |
| 565 | std::reverse(FreqPairs.begin(), FreqPairs.end()); |
| 566 | |
Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 567 | errs() << "\tRecord Histogram:\n"; |
Chris Lattner | c167cac | 2009-04-27 18:15:27 +0000 | [diff] [blame] | 568 | fprintf(stderr, "\t\t Count # Bits %% Abv Record Kind\n"); |
Chris Lattner | b1e85b5 | 2007-05-05 01:46:49 +0000 | [diff] [blame] | 569 | for (unsigned i = 0, e = FreqPairs.size(); i != e; ++i) { |
Chris Lattner | c167cac | 2009-04-27 18:15:27 +0000 | [diff] [blame] | 570 | const PerRecordStats &RecStats = Stats.CodeFreq[FreqPairs[i].second]; |
| 571 | |
| 572 | fprintf(stderr, "\t\t%7d %9llu ", RecStats.NumInstances, |
Dan Gohman | fbccdef | 2009-05-01 16:33:33 +0000 | [diff] [blame] | 573 | (unsigned long long)RecStats.TotalBits); |
Chris Lattner | c167cac | 2009-04-27 18:15:27 +0000 | [diff] [blame] | 574 | |
| 575 | if (RecStats.NumAbbrev) |
| 576 | fprintf(stderr, "%7.2f ", |
| 577 | (double)RecStats.NumAbbrev/RecStats.NumInstances*100); |
| 578 | else |
| 579 | fprintf(stderr, " "); |
Chris Lattner | 2443747 | 2009-04-27 17:59:34 +0000 | [diff] [blame] | 580 | |
Chris Lattner | f9a3ec8 | 2009-04-26 22:21:57 +0000 | [diff] [blame] | 581 | if (const char *CodeName = |
| 582 | GetCodeName(FreqPairs[i].second, I->first, StreamFile)) |
Chris Lattner | 2443747 | 2009-04-27 17:59:34 +0000 | [diff] [blame] | 583 | fprintf(stderr, "%s\n", CodeName); |
Chris Lattner | b1e85b5 | 2007-05-05 01:46:49 +0000 | [diff] [blame] | 584 | else |
Chris Lattner | 2443747 | 2009-04-27 17:59:34 +0000 | [diff] [blame] | 585 | fprintf(stderr, "UnknownCode%d\n", FreqPairs[i].second); |
Chris Lattner | b1e85b5 | 2007-05-05 01:46:49 +0000 | [diff] [blame] | 586 | } |
Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 587 | errs() << "\n"; |
Chris Lattner | b1e85b5 | 2007-05-05 01:46:49 +0000 | [diff] [blame] | 588 | |
| 589 | } |
Chris Lattner | 4238d47 | 2007-04-29 20:00:02 +0000 | [diff] [blame] | 590 | } |
Chris Lattner | 45e0f89 | 2007-04-29 08:12:22 +0000 | [diff] [blame] | 591 | return 0; |
| 592 | } |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 593 | |
Chris Lattner | 32de633 | 2007-04-29 08:31:14 +0000 | [diff] [blame] | 594 | |
Chris Lattner | c30598b | 2006-12-06 01:18:01 +0000 | [diff] [blame] | 595 | int main(int argc, char **argv) { |
Chris Lattner | cc14d25 | 2009-03-06 05:34:10 +0000 | [diff] [blame] | 596 | // Print a stack trace if we signal out. |
Chris Lattner | 45e0f89 | 2007-04-29 08:12:22 +0000 | [diff] [blame] | 597 | sys::PrintStackTraceOnErrorSignal(); |
Chris Lattner | cc14d25 | 2009-03-06 05:34:10 +0000 | [diff] [blame] | 598 | PrettyStackTraceProgram X(argc, argv); |
| 599 | llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. |
| 600 | cl::ParseCommandLineOptions(argc, argv, "llvm-bcanalyzer file analyzer\n"); |
Chris Lattner | 45e0f89 | 2007-04-29 08:12:22 +0000 | [diff] [blame] | 601 | |
Chris Lattner | 44dadff | 2007-05-06 09:29:57 +0000 | [diff] [blame] | 602 | return AnalyzeBitcode(); |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 603 | } |