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