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