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 | |
Mehdi Amini | d2f4701 | 2016-04-01 05:33:11 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/StringExtras.h" |
Teresa Johnson | a547919 | 2016-11-11 05:34:58 +0000 | [diff] [blame] | 31 | #include "llvm/Bitcode/BitcodeReader.h" |
Mehdi Amini | f6071e1 | 2016-04-18 09:17:29 +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" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 34 | #include "llvm/Support/CommandLine.h" |
Daniel Dunbar | 42985bb | 2009-09-25 16:04:21 +0000 | [diff] [blame] | 35 | #include "llvm/Support/Format.h" |
Rui Ueyama | 0b9d56a | 2018-04-13 18:26:06 +0000 | [diff] [blame] | 36 | #include "llvm/Support/InitLLVM.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" |
Mehdi Amini | d2f4701 | 2016-04-01 05:33:11 +0000 | [diff] [blame] | 39 | #include "llvm/Support/SHA1.h" |
Jonas Devlieghere | ae6875e | 2018-04-21 21:11:59 +0000 | [diff] [blame] | 40 | #include "llvm/Support/WithColor.h" |
Chandler Carruth | f010c46 | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 41 | #include "llvm/Support/raw_ostream.h" |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 42 | using namespace llvm; |
| 43 | |
| 44 | static cl::opt<std::string> |
Gabor Greif | 8ff70c2 | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 45 | InputFilename(cl::Positional, cl::desc("<input bitcode>"), cl::init("-")); |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 46 | |
Gabor Greif | 8ff70c2 | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 47 | 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] | 48 | |
| 49 | //===----------------------------------------------------------------------===// |
| 50 | // Bitcode specific analysis. |
| 51 | //===----------------------------------------------------------------------===// |
| 52 | |
Chris Lattner | b1e85b5 | 2007-05-05 01:46:49 +0000 | [diff] [blame] | 53 | static cl::opt<bool> NoHistogram("disable-histogram", |
| 54 | cl::desc("Do not print per-code histogram")); |
Chris Lattner | 45e0f89 | 2007-04-29 08:12:22 +0000 | [diff] [blame] | 55 | |
Chris Lattner | b30c925 | 2007-04-29 21:48:19 +0000 | [diff] [blame] | 56 | static cl::opt<bool> |
| 57 | NonSymbolic("non-symbolic", |
Michael J. Spencer | 3ff9563 | 2010-12-16 03:29:14 +0000 | [diff] [blame] | 58 | cl::desc("Emit numeric info in dump even if" |
Chris Lattner | b30c925 | 2007-04-29 21:48:19 +0000 | [diff] [blame] | 59 | " symbolic info is available")); |
| 60 | |
Jordan Rose | d421075 | 2014-08-30 17:07:55 +0000 | [diff] [blame] | 61 | static cl::opt<std::string> |
| 62 | BlockInfoFilename("block-info", |
| 63 | cl::desc("Use the BLOCK_INFO from the given file")); |
| 64 | |
Jordan Rose | 1826197 | 2015-05-13 18:51:49 +0000 | [diff] [blame] | 65 | static cl::opt<bool> |
| 66 | ShowBinaryBlobs("show-binary-blobs", |
| 67 | cl::desc("Print binary blobs using hex escapes")); |
| 68 | |
Peter Collingbourne | 67a3f7f | 2017-07-06 17:56:01 +0000 | [diff] [blame] | 69 | static cl::opt<std::string> CheckHash( |
| 70 | "check-hash", |
| 71 | cl::desc("Check module hash using the argument as a string table")); |
| 72 | |
Dan Gohman | e7e4b51 | 2010-12-09 20:35:40 +0000 | [diff] [blame] | 73 | namespace { |
| 74 | |
| 75 | /// CurStreamTypeType - A type for CurStreamType |
| 76 | enum CurStreamTypeType { |
Chris Lattner | 45e0f89 | 2007-04-29 08:12:22 +0000 | [diff] [blame] | 77 | UnknownBitstream, |
Brian Gesiak | 37f1378 | 2018-04-21 23:52:04 +0000 | [diff] [blame] | 78 | LLVMIRBitstream, |
| 79 | ClangSerializedASTBitstream, |
| 80 | ClangSerializedDiagnosticsBitstream, |
Dan Gohman | e7e4b51 | 2010-12-09 20:35:40 +0000 | [diff] [blame] | 81 | }; |
| 82 | |
| 83 | } |
| 84 | |
Chris Lattner | 4238d47 | 2007-04-29 20:00:02 +0000 | [diff] [blame] | 85 | /// GetBlockName - Return a symbolic block name if known, otherwise return |
Chris Lattner | b30c925 | 2007-04-29 21:48:19 +0000 | [diff] [blame] | 86 | /// null. |
Chris Lattner | f9a3ec8 | 2009-04-26 22:21:57 +0000 | [diff] [blame] | 87 | static const char *GetBlockName(unsigned BlockID, |
Peter Collingbourne | 8fc9b4d | 2016-11-08 04:17:11 +0000 | [diff] [blame] | 88 | const BitstreamBlockInfo &BlockInfo, |
Jordan Rose | d421075 | 2014-08-30 17:07:55 +0000 | [diff] [blame] | 89 | CurStreamTypeType CurStreamType) { |
Chris Lattner | 1772b12d | 2007-05-05 00:17:42 +0000 | [diff] [blame] | 90 | // Standard blocks for all bitcode files. |
| 91 | if (BlockID < bitc::FIRST_APPLICATION_BLOCKID) { |
| 92 | if (BlockID == bitc::BLOCKINFO_BLOCK_ID) |
| 93 | return "BLOCKINFO_BLOCK"; |
Craig Topper | 573faec | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 94 | return nullptr; |
Chris Lattner | 1772b12d | 2007-05-05 00:17:42 +0000 | [diff] [blame] | 95 | } |
Daniel Dunbar | 80ba3d2 | 2009-09-25 16:03:57 +0000 | [diff] [blame] | 96 | |
Chris Lattner | f9a3ec8 | 2009-04-26 22:21:57 +0000 | [diff] [blame] | 97 | // Check to see if we have a blockinfo record for this block, with a name. |
Peter Collingbourne | 8fc9b4d | 2016-11-08 04:17:11 +0000 | [diff] [blame] | 98 | if (const BitstreamBlockInfo::BlockInfo *Info = |
| 99 | BlockInfo.getBlockInfo(BlockID)) { |
Chris Lattner | f9a3ec8 | 2009-04-26 22:21:57 +0000 | [diff] [blame] | 100 | if (!Info->Name.empty()) |
| 101 | return Info->Name.c_str(); |
| 102 | } |
Daniel Dunbar | 80ba3d2 | 2009-09-25 16:03:57 +0000 | [diff] [blame] | 103 | |
| 104 | |
Craig Topper | 573faec | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 105 | if (CurStreamType != LLVMIRBitstream) return nullptr; |
Daniel Dunbar | 80ba3d2 | 2009-09-25 16:03:57 +0000 | [diff] [blame] | 106 | |
Chris Lattner | 4238d47 | 2007-04-29 20:00:02 +0000 | [diff] [blame] | 107 | switch (BlockID) { |
Sanjoy Das | 9ad5da1 | 2016-04-26 05:59:14 +0000 | [diff] [blame] | 108 | default: return nullptr; |
Sanjoy Das | 6588d8d | 2016-04-26 05:59:08 +0000 | [diff] [blame] | 109 | case bitc::OPERAND_BUNDLE_TAGS_BLOCK_ID: return "OPERAND_BUNDLE_TAGS_BLOCK"; |
Sanjoy Das | 9ad5da1 | 2016-04-26 05:59:14 +0000 | [diff] [blame] | 110 | case bitc::MODULE_BLOCK_ID: return "MODULE_BLOCK"; |
| 111 | case bitc::PARAMATTR_BLOCK_ID: return "PARAMATTR_BLOCK"; |
| 112 | case bitc::PARAMATTR_GROUP_BLOCK_ID: return "PARAMATTR_GROUP_BLOCK_ID"; |
| 113 | case bitc::TYPE_BLOCK_ID_NEW: return "TYPE_BLOCK_ID"; |
| 114 | case bitc::CONSTANTS_BLOCK_ID: return "CONSTANTS_BLOCK"; |
| 115 | case bitc::FUNCTION_BLOCK_ID: return "FUNCTION_BLOCK"; |
Mehdi Amini | b738d34 | 2015-10-26 18:37:00 +0000 | [diff] [blame] | 116 | case bitc::IDENTIFICATION_BLOCK_ID: |
Sanjoy Das | 9ad5da1 | 2016-04-26 05:59:14 +0000 | [diff] [blame] | 117 | return "IDENTIFICATION_BLOCK_ID"; |
| 118 | case bitc::VALUE_SYMTAB_BLOCK_ID: return "VALUE_SYMTAB"; |
| 119 | case bitc::METADATA_BLOCK_ID: return "METADATA_BLOCK"; |
| 120 | case bitc::METADATA_KIND_BLOCK_ID: return "METADATA_KIND_BLOCK"; |
| 121 | case bitc::METADATA_ATTACHMENT_ID: return "METADATA_ATTACHMENT_BLOCK"; |
| 122 | case bitc::USELIST_BLOCK_ID: return "USELIST_BLOCK_ID"; |
Teresa Johnson | dc6615a | 2016-03-11 18:52:24 +0000 | [diff] [blame] | 123 | case bitc::GLOBALVAL_SUMMARY_BLOCK_ID: |
Sanjoy Das | 9ad5da1 | 2016-04-26 05:59:14 +0000 | [diff] [blame] | 124 | return "GLOBALVAL_SUMMARY_BLOCK"; |
Peter Collingbourne | e74c64e | 2017-06-08 23:01:49 +0000 | [diff] [blame] | 125 | case bitc::FULL_LTO_GLOBALVAL_SUMMARY_BLOCK_ID: |
| 126 | return "FULL_LTO_GLOBALVAL_SUMMARY_BLOCK"; |
Sanjoy Das | 9ad5da1 | 2016-04-26 05:59:14 +0000 | [diff] [blame] | 127 | case bitc::MODULE_STRTAB_BLOCK_ID: return "MODULE_STRTAB_BLOCK"; |
Peter Collingbourne | 6163b4a | 2017-04-17 17:51:36 +0000 | [diff] [blame] | 128 | case bitc::STRTAB_BLOCK_ID: return "STRTAB_BLOCK"; |
Peter Collingbourne | fdc1250 | 2017-06-27 23:50:11 +0000 | [diff] [blame] | 129 | case bitc::SYMTAB_BLOCK_ID: return "SYMTAB_BLOCK"; |
Chris Lattner | 4238d47 | 2007-04-29 20:00:02 +0000 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | |
Chris Lattner | b30c925 | 2007-04-29 21:48:19 +0000 | [diff] [blame] | 133 | /// GetCodeName - Return a symbolic code name if known, otherwise return |
| 134 | /// null. |
Chris Lattner | f9a3ec8 | 2009-04-26 22:21:57 +0000 | [diff] [blame] | 135 | static const char *GetCodeName(unsigned CodeID, unsigned BlockID, |
Peter Collingbourne | 8fc9b4d | 2016-11-08 04:17:11 +0000 | [diff] [blame] | 136 | const BitstreamBlockInfo &BlockInfo, |
Jordan Rose | d421075 | 2014-08-30 17:07:55 +0000 | [diff] [blame] | 137 | CurStreamTypeType CurStreamType) { |
Chris Lattner | 1772b12d | 2007-05-05 00:17:42 +0000 | [diff] [blame] | 138 | // Standard blocks for all bitcode files. |
| 139 | if (BlockID < bitc::FIRST_APPLICATION_BLOCKID) { |
| 140 | if (BlockID == bitc::BLOCKINFO_BLOCK_ID) { |
| 141 | switch (CodeID) { |
Craig Topper | 573faec | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 142 | default: return nullptr; |
Chris Lattner | f9a3ec8 | 2009-04-26 22:21:57 +0000 | [diff] [blame] | 143 | case bitc::BLOCKINFO_CODE_SETBID: return "SETBID"; |
| 144 | case bitc::BLOCKINFO_CODE_BLOCKNAME: return "BLOCKNAME"; |
| 145 | case bitc::BLOCKINFO_CODE_SETRECORDNAME: return "SETRECORDNAME"; |
Chris Lattner | 1772b12d | 2007-05-05 00:17:42 +0000 | [diff] [blame] | 146 | } |
| 147 | } |
Craig Topper | 573faec | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 148 | return nullptr; |
Chris Lattner | 1772b12d | 2007-05-05 00:17:42 +0000 | [diff] [blame] | 149 | } |
Daniel Dunbar | 80ba3d2 | 2009-09-25 16:03:57 +0000 | [diff] [blame] | 150 | |
Chris Lattner | f9a3ec8 | 2009-04-26 22:21:57 +0000 | [diff] [blame] | 151 | // Check to see if we have a blockinfo record for this record, with a name. |
Peter Collingbourne | 8fc9b4d | 2016-11-08 04:17:11 +0000 | [diff] [blame] | 152 | if (const BitstreamBlockInfo::BlockInfo *Info = |
| 153 | BlockInfo.getBlockInfo(BlockID)) { |
Chris Lattner | f9a3ec8 | 2009-04-26 22:21:57 +0000 | [diff] [blame] | 154 | for (unsigned i = 0, e = Info->RecordNames.size(); i != e; ++i) |
| 155 | if (Info->RecordNames[i].first == CodeID) |
| 156 | return Info->RecordNames[i].second.c_str(); |
| 157 | } |
Daniel Dunbar | 80ba3d2 | 2009-09-25 16:03:57 +0000 | [diff] [blame] | 158 | |
| 159 | |
Craig Topper | 573faec | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 160 | if (CurStreamType != LLVMIRBitstream) return nullptr; |
Daniel Dunbar | 80ba3d2 | 2009-09-25 16:03:57 +0000 | [diff] [blame] | 161 | |
Duncan P. N. Exon Smith | 70dde5f | 2015-06-29 22:50:32 +0000 | [diff] [blame] | 162 | #define STRINGIFY_CODE(PREFIX, CODE) \ |
| 163 | case bitc::PREFIX##_##CODE: \ |
| 164 | return #CODE; |
Chris Lattner | b30c925 | 2007-04-29 21:48:19 +0000 | [diff] [blame] | 165 | switch (BlockID) { |
Craig Topper | 573faec | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 166 | default: return nullptr; |
Chris Lattner | b30c925 | 2007-04-29 21:48:19 +0000 | [diff] [blame] | 167 | case bitc::MODULE_BLOCK_ID: |
| 168 | switch (CodeID) { |
Craig Topper | 573faec | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 169 | default: return nullptr; |
Duncan P. N. Exon Smith | 70dde5f | 2015-06-29 22:50:32 +0000 | [diff] [blame] | 170 | STRINGIFY_CODE(MODULE_CODE, VERSION) |
| 171 | STRINGIFY_CODE(MODULE_CODE, TRIPLE) |
| 172 | STRINGIFY_CODE(MODULE_CODE, DATALAYOUT) |
| 173 | STRINGIFY_CODE(MODULE_CODE, ASM) |
| 174 | STRINGIFY_CODE(MODULE_CODE, SECTIONNAME) |
| 175 | STRINGIFY_CODE(MODULE_CODE, DEPLIB) // FIXME: Remove in 4.0 |
| 176 | STRINGIFY_CODE(MODULE_CODE, GLOBALVAR) |
| 177 | STRINGIFY_CODE(MODULE_CODE, FUNCTION) |
| 178 | STRINGIFY_CODE(MODULE_CODE, ALIAS) |
Duncan P. N. Exon Smith | 70dde5f | 2015-06-29 22:50:32 +0000 | [diff] [blame] | 179 | STRINGIFY_CODE(MODULE_CODE, GCNAME) |
Teresa Johnson | 3011017 | 2015-09-17 20:12:00 +0000 | [diff] [blame] | 180 | STRINGIFY_CODE(MODULE_CODE, VSTOFFSET) |
Duncan P. N. Exon Smith | f206a2c | 2016-03-25 01:29:50 +0000 | [diff] [blame] | 181 | STRINGIFY_CODE(MODULE_CODE, METADATA_VALUES_UNUSED) |
Teresa Johnson | 0060160 | 2016-02-10 21:55:02 +0000 | [diff] [blame] | 182 | STRINGIFY_CODE(MODULE_CODE, SOURCE_FILENAME) |
Mehdi Amini | d2f4701 | 2016-04-01 05:33:11 +0000 | [diff] [blame] | 183 | STRINGIFY_CODE(MODULE_CODE, HASH) |
Chris Lattner | b30c925 | 2007-04-29 21:48:19 +0000 | [diff] [blame] | 184 | } |
Mehdi Amini | b738d34 | 2015-10-26 18:37:00 +0000 | [diff] [blame] | 185 | case bitc::IDENTIFICATION_BLOCK_ID: |
| 186 | switch (CodeID) { |
| 187 | default: |
| 188 | return nullptr; |
| 189 | STRINGIFY_CODE(IDENTIFICATION_CODE, STRING) |
| 190 | STRINGIFY_CODE(IDENTIFICATION_CODE, EPOCH) |
| 191 | } |
Chris Lattner | cd5b7d7 | 2007-05-04 03:01:41 +0000 | [diff] [blame] | 192 | case bitc::PARAMATTR_BLOCK_ID: |
| 193 | switch (CodeID) { |
Craig Topper | 573faec | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 194 | default: return nullptr; |
Duncan P. N. Exon Smith | 70dde5f | 2015-06-29 22:50:32 +0000 | [diff] [blame] | 195 | // FIXME: Should these be different? |
Bill Wendling | 8620bd3 | 2013-02-10 23:17:10 +0000 | [diff] [blame] | 196 | case bitc::PARAMATTR_CODE_ENTRY_OLD: return "ENTRY"; |
| 197 | case bitc::PARAMATTR_CODE_ENTRY: return "ENTRY"; |
Justin Bogner | a935ab1 | 2016-03-15 22:37:25 +0000 | [diff] [blame] | 198 | } |
| 199 | case bitc::PARAMATTR_GROUP_BLOCK_ID: |
| 200 | switch (CodeID) { |
| 201 | default: return nullptr; |
Bill Wendling | 8620bd3 | 2013-02-10 23:17:10 +0000 | [diff] [blame] | 202 | case bitc::PARAMATTR_GRP_CODE_ENTRY: return "ENTRY"; |
Chris Lattner | cd5b7d7 | 2007-05-04 03:01:41 +0000 | [diff] [blame] | 203 | } |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 204 | case bitc::TYPE_BLOCK_ID_NEW: |
Chris Lattner | b30c925 | 2007-04-29 21:48:19 +0000 | [diff] [blame] | 205 | switch (CodeID) { |
Craig Topper | 573faec | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 206 | default: return nullptr; |
Duncan P. N. Exon Smith | 70dde5f | 2015-06-29 22:50:32 +0000 | [diff] [blame] | 207 | STRINGIFY_CODE(TYPE_CODE, NUMENTRY) |
| 208 | STRINGIFY_CODE(TYPE_CODE, VOID) |
| 209 | STRINGIFY_CODE(TYPE_CODE, FLOAT) |
| 210 | STRINGIFY_CODE(TYPE_CODE, DOUBLE) |
| 211 | STRINGIFY_CODE(TYPE_CODE, LABEL) |
| 212 | STRINGIFY_CODE(TYPE_CODE, OPAQUE) |
| 213 | STRINGIFY_CODE(TYPE_CODE, INTEGER) |
| 214 | STRINGIFY_CODE(TYPE_CODE, POINTER) |
| 215 | STRINGIFY_CODE(TYPE_CODE, ARRAY) |
| 216 | STRINGIFY_CODE(TYPE_CODE, VECTOR) |
| 217 | STRINGIFY_CODE(TYPE_CODE, X86_FP80) |
| 218 | STRINGIFY_CODE(TYPE_CODE, FP128) |
| 219 | STRINGIFY_CODE(TYPE_CODE, PPC_FP128) |
| 220 | STRINGIFY_CODE(TYPE_CODE, METADATA) |
| 221 | STRINGIFY_CODE(TYPE_CODE, STRUCT_ANON) |
| 222 | STRINGIFY_CODE(TYPE_CODE, STRUCT_NAME) |
| 223 | STRINGIFY_CODE(TYPE_CODE, STRUCT_NAMED) |
| 224 | STRINGIFY_CODE(TYPE_CODE, FUNCTION) |
Chris Lattner | b30c925 | 2007-04-29 21:48:19 +0000 | [diff] [blame] | 225 | } |
Daniel Dunbar | 80ba3d2 | 2009-09-25 16:03:57 +0000 | [diff] [blame] | 226 | |
Chris Lattner | b30c925 | 2007-04-29 21:48:19 +0000 | [diff] [blame] | 227 | case bitc::CONSTANTS_BLOCK_ID: |
| 228 | switch (CodeID) { |
Craig Topper | 573faec | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 229 | default: return nullptr; |
Duncan P. N. Exon Smith | 70dde5f | 2015-06-29 22:50:32 +0000 | [diff] [blame] | 230 | STRINGIFY_CODE(CST_CODE, SETTYPE) |
| 231 | STRINGIFY_CODE(CST_CODE, NULL) |
| 232 | STRINGIFY_CODE(CST_CODE, UNDEF) |
| 233 | STRINGIFY_CODE(CST_CODE, INTEGER) |
| 234 | STRINGIFY_CODE(CST_CODE, WIDE_INTEGER) |
| 235 | STRINGIFY_CODE(CST_CODE, FLOAT) |
| 236 | STRINGIFY_CODE(CST_CODE, AGGREGATE) |
| 237 | STRINGIFY_CODE(CST_CODE, STRING) |
| 238 | STRINGIFY_CODE(CST_CODE, CSTRING) |
| 239 | STRINGIFY_CODE(CST_CODE, CE_BINOP) |
| 240 | STRINGIFY_CODE(CST_CODE, CE_CAST) |
| 241 | STRINGIFY_CODE(CST_CODE, CE_GEP) |
| 242 | STRINGIFY_CODE(CST_CODE, CE_INBOUNDS_GEP) |
| 243 | STRINGIFY_CODE(CST_CODE, CE_SELECT) |
| 244 | STRINGIFY_CODE(CST_CODE, CE_EXTRACTELT) |
| 245 | STRINGIFY_CODE(CST_CODE, CE_INSERTELT) |
| 246 | STRINGIFY_CODE(CST_CODE, CE_SHUFFLEVEC) |
| 247 | STRINGIFY_CODE(CST_CODE, CE_CMP) |
| 248 | STRINGIFY_CODE(CST_CODE, INLINEASM) |
| 249 | STRINGIFY_CODE(CST_CODE, CE_SHUFVEC_EX) |
Cameron McInally | ca8cb68 | 2018-11-13 18:15:47 +0000 | [diff] [blame] | 250 | STRINGIFY_CODE(CST_CODE, CE_UNOP) |
Chris Lattner | d408f06 | 2012-01-30 00:51:16 +0000 | [diff] [blame] | 251 | case bitc::CST_CODE_BLOCKADDRESS: return "CST_CODE_BLOCKADDRESS"; |
Duncan P. N. Exon Smith | 70dde5f | 2015-06-29 22:50:32 +0000 | [diff] [blame] | 252 | STRINGIFY_CODE(CST_CODE, DATA) |
Daniel Dunbar | 80ba3d2 | 2009-09-25 16:03:57 +0000 | [diff] [blame] | 253 | } |
Chris Lattner | b30c925 | 2007-04-29 21:48:19 +0000 | [diff] [blame] | 254 | case bitc::FUNCTION_BLOCK_ID: |
| 255 | switch (CodeID) { |
Craig Topper | 573faec | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 256 | default: return nullptr; |
Duncan P. N. Exon Smith | 70dde5f | 2015-06-29 22:50:32 +0000 | [diff] [blame] | 257 | STRINGIFY_CODE(FUNC_CODE, DECLAREBLOCKS) |
| 258 | STRINGIFY_CODE(FUNC_CODE, INST_BINOP) |
| 259 | STRINGIFY_CODE(FUNC_CODE, INST_CAST) |
| 260 | STRINGIFY_CODE(FUNC_CODE, INST_GEP_OLD) |
| 261 | STRINGIFY_CODE(FUNC_CODE, INST_INBOUNDS_GEP_OLD) |
| 262 | STRINGIFY_CODE(FUNC_CODE, INST_SELECT) |
| 263 | STRINGIFY_CODE(FUNC_CODE, INST_EXTRACTELT) |
| 264 | STRINGIFY_CODE(FUNC_CODE, INST_INSERTELT) |
| 265 | STRINGIFY_CODE(FUNC_CODE, INST_SHUFFLEVEC) |
| 266 | STRINGIFY_CODE(FUNC_CODE, INST_CMP) |
| 267 | STRINGIFY_CODE(FUNC_CODE, INST_RET) |
| 268 | STRINGIFY_CODE(FUNC_CODE, INST_BR) |
| 269 | STRINGIFY_CODE(FUNC_CODE, INST_SWITCH) |
| 270 | STRINGIFY_CODE(FUNC_CODE, INST_INVOKE) |
Cameron McInally | ca8cb68 | 2018-11-13 18:15:47 +0000 | [diff] [blame] | 271 | STRINGIFY_CODE(FUNC_CODE, INST_UNOP) |
Duncan P. N. Exon Smith | 70dde5f | 2015-06-29 22:50:32 +0000 | [diff] [blame] | 272 | STRINGIFY_CODE(FUNC_CODE, INST_UNREACHABLE) |
David Majnemer | 4a45f08 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 273 | STRINGIFY_CODE(FUNC_CODE, INST_CLEANUPRET) |
| 274 | STRINGIFY_CODE(FUNC_CODE, INST_CATCHRET) |
| 275 | STRINGIFY_CODE(FUNC_CODE, INST_CATCHPAD) |
Duncan P. N. Exon Smith | 70dde5f | 2015-06-29 22:50:32 +0000 | [diff] [blame] | 276 | STRINGIFY_CODE(FUNC_CODE, INST_PHI) |
| 277 | STRINGIFY_CODE(FUNC_CODE, INST_ALLOCA) |
| 278 | STRINGIFY_CODE(FUNC_CODE, INST_LOAD) |
| 279 | STRINGIFY_CODE(FUNC_CODE, INST_VAARG) |
| 280 | STRINGIFY_CODE(FUNC_CODE, INST_STORE) |
| 281 | STRINGIFY_CODE(FUNC_CODE, INST_EXTRACTVAL) |
| 282 | STRINGIFY_CODE(FUNC_CODE, INST_INSERTVAL) |
| 283 | STRINGIFY_CODE(FUNC_CODE, INST_CMP2) |
| 284 | STRINGIFY_CODE(FUNC_CODE, INST_VSELECT) |
| 285 | STRINGIFY_CODE(FUNC_CODE, DEBUG_LOC_AGAIN) |
| 286 | STRINGIFY_CODE(FUNC_CODE, INST_CALL) |
| 287 | STRINGIFY_CODE(FUNC_CODE, DEBUG_LOC) |
| 288 | STRINGIFY_CODE(FUNC_CODE, INST_GEP) |
Sanjoy Das | 6588d8d | 2016-04-26 05:59:08 +0000 | [diff] [blame] | 289 | STRINGIFY_CODE(FUNC_CODE, OPERAND_BUNDLE) |
Matt Arsenault | 076203b | 2018-09-24 12:47:17 +0000 | [diff] [blame] | 290 | STRINGIFY_CODE(FUNC_CODE, INST_FENCE) |
| 291 | STRINGIFY_CODE(FUNC_CODE, INST_ATOMICRMW) |
| 292 | STRINGIFY_CODE(FUNC_CODE, INST_LOADATOMIC) |
| 293 | STRINGIFY_CODE(FUNC_CODE, INST_STOREATOMIC) |
| 294 | STRINGIFY_CODE(FUNC_CODE, INST_CMPXCHG) |
Chris Lattner | b30c925 | 2007-04-29 21:48:19 +0000 | [diff] [blame] | 295 | } |
Chris Lattner | b30c925 | 2007-04-29 21:48:19 +0000 | [diff] [blame] | 296 | case bitc::VALUE_SYMTAB_BLOCK_ID: |
| 297 | switch (CodeID) { |
Craig Topper | 573faec | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 298 | default: return nullptr; |
Duncan P. N. Exon Smith | 70dde5f | 2015-06-29 22:50:32 +0000 | [diff] [blame] | 299 | STRINGIFY_CODE(VST_CODE, ENTRY) |
| 300 | STRINGIFY_CODE(VST_CODE, BBENTRY) |
Teresa Johnson | 3011017 | 2015-09-17 20:12:00 +0000 | [diff] [blame] | 301 | STRINGIFY_CODE(VST_CODE, FNENTRY) |
Teresa Johnson | dc6615a | 2016-03-11 18:52:24 +0000 | [diff] [blame] | 302 | STRINGIFY_CODE(VST_CODE, COMBINED_ENTRY) |
Teresa Johnson | b97baa5 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 303 | } |
| 304 | case bitc::MODULE_STRTAB_BLOCK_ID: |
| 305 | switch (CodeID) { |
Teresa Johnson | 9dd98c0 | 2015-11-02 18:02:11 +0000 | [diff] [blame] | 306 | default: |
| 307 | return nullptr; |
| 308 | STRINGIFY_CODE(MST_CODE, ENTRY) |
Mehdi Amini | d2f4701 | 2016-04-01 05:33:11 +0000 | [diff] [blame] | 309 | STRINGIFY_CODE(MST_CODE, HASH) |
Teresa Johnson | b97baa5 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 310 | } |
Teresa Johnson | dc6615a | 2016-03-11 18:52:24 +0000 | [diff] [blame] | 311 | case bitc::GLOBALVAL_SUMMARY_BLOCK_ID: |
Peter Collingbourne | e74c64e | 2017-06-08 23:01:49 +0000 | [diff] [blame] | 312 | case bitc::FULL_LTO_GLOBALVAL_SUMMARY_BLOCK_ID: |
Teresa Johnson | b97baa5 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 313 | switch (CodeID) { |
Teresa Johnson | 9dd98c0 | 2015-11-02 18:02:11 +0000 | [diff] [blame] | 314 | default: |
| 315 | return nullptr; |
Teresa Johnson | dc6615a | 2016-03-11 18:52:24 +0000 | [diff] [blame] | 316 | STRINGIFY_CODE(FS, PERMODULE) |
| 317 | STRINGIFY_CODE(FS, PERMODULE_PROFILE) |
Easwaran Raman | bdb305a | 2018-01-25 19:27:17 +0000 | [diff] [blame] | 318 | STRINGIFY_CODE(FS, PERMODULE_RELBF) |
Teresa Johnson | dc6615a | 2016-03-11 18:52:24 +0000 | [diff] [blame] | 319 | STRINGIFY_CODE(FS, PERMODULE_GLOBALVAR_INIT_REFS) |
| 320 | STRINGIFY_CODE(FS, COMBINED) |
| 321 | STRINGIFY_CODE(FS, COMBINED_PROFILE) |
| 322 | STRINGIFY_CODE(FS, COMBINED_GLOBALVAR_INIT_REFS) |
Mehdi Amini | eb79e6e | 2016-04-16 06:56:44 +0000 | [diff] [blame] | 323 | STRINGIFY_CODE(FS, ALIAS) |
| 324 | STRINGIFY_CODE(FS, COMBINED_ALIAS) |
Mehdi Amini | 9ab3b37 | 2016-04-23 23:38:17 +0000 | [diff] [blame] | 325 | STRINGIFY_CODE(FS, COMBINED_ORIGINAL_NAME) |
Mehdi Amini | 3f53abc | 2016-04-24 03:18:11 +0000 | [diff] [blame] | 326 | STRINGIFY_CODE(FS, VERSION) |
Teresa Johnson | d3f03a6 | 2018-02-07 04:05:59 +0000 | [diff] [blame] | 327 | STRINGIFY_CODE(FS, FLAGS) |
Peter Collingbourne | ea3f918 | 2016-12-21 23:03:45 +0000 | [diff] [blame] | 328 | STRINGIFY_CODE(FS, TYPE_TESTS) |
Peter Collingbourne | 446f8d7 | 2017-02-10 22:29:38 +0000 | [diff] [blame] | 329 | STRINGIFY_CODE(FS, TYPE_TEST_ASSUME_VCALLS) |
| 330 | STRINGIFY_CODE(FS, TYPE_CHECKED_LOAD_VCALLS) |
| 331 | STRINGIFY_CODE(FS, TYPE_TEST_ASSUME_CONST_VCALL) |
| 332 | STRINGIFY_CODE(FS, TYPE_CHECKED_LOAD_CONST_VCALL) |
Peter Collingbourne | 6163b4a | 2017-04-17 17:51:36 +0000 | [diff] [blame] | 333 | STRINGIFY_CODE(FS, VALUE_GUID) |
Evgeniy Stepanov | 3184f8d | 2017-06-16 00:18:29 +0000 | [diff] [blame] | 334 | STRINGIFY_CODE(FS, CFI_FUNCTION_DEFS) |
| 335 | STRINGIFY_CODE(FS, CFI_FUNCTION_DECLS) |
Vitaly Buka | 4a81fc6 | 2018-02-14 22:41:15 +0000 | [diff] [blame] | 336 | STRINGIFY_CODE(FS, TYPE_ID) |
Chris Lattner | b30c925 | 2007-04-29 21:48:19 +0000 | [diff] [blame] | 337 | } |
Devang Patel | e8e0213 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 338 | case bitc::METADATA_ATTACHMENT_ID: |
| 339 | switch(CodeID) { |
Craig Topper | 573faec | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 340 | default:return nullptr; |
Duncan P. N. Exon Smith | 88f33c9 | 2015-06-29 22:50:35 +0000 | [diff] [blame] | 341 | STRINGIFY_CODE(METADATA, ATTACHMENT) |
Devang Patel | e8e0213 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 342 | } |
Devang Patel | e54abc9 | 2009-07-22 17:43:22 +0000 | [diff] [blame] | 343 | case bitc::METADATA_BLOCK_ID: |
| 344 | switch(CodeID) { |
Craig Topper | 573faec | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 345 | default:return nullptr; |
Duncan P. N. Exon Smith | a73e509 | 2016-03-27 23:17:54 +0000 | [diff] [blame] | 346 | STRINGIFY_CODE(METADATA, STRING_OLD) |
Duncan P. N. Exon Smith | 88f33c9 | 2015-06-29 22:50:35 +0000 | [diff] [blame] | 347 | STRINGIFY_CODE(METADATA, VALUE) |
Adrian Prantl | 639592d | 2017-01-03 19:17:49 +0000 | [diff] [blame] | 348 | STRINGIFY_CODE(METADATA, NODE) |
| 349 | STRINGIFY_CODE(METADATA, NAME) |
| 350 | STRINGIFY_CODE(METADATA, DISTINCT_NODE) |
| 351 | STRINGIFY_CODE(METADATA, KIND) // Older bitcode has it in a MODULE_BLOCK |
| 352 | STRINGIFY_CODE(METADATA, LOCATION) |
Duncan P. N. Exon Smith | 88f33c9 | 2015-06-29 22:50:35 +0000 | [diff] [blame] | 353 | STRINGIFY_CODE(METADATA, OLD_NODE) |
| 354 | STRINGIFY_CODE(METADATA, OLD_FN_NODE) |
| 355 | STRINGIFY_CODE(METADATA, NAMED_NODE) |
Duncan P. N. Exon Smith | 88f33c9 | 2015-06-29 22:50:35 +0000 | [diff] [blame] | 356 | STRINGIFY_CODE(METADATA, GENERIC_DEBUG) |
| 357 | STRINGIFY_CODE(METADATA, SUBRANGE) |
| 358 | STRINGIFY_CODE(METADATA, ENUMERATOR) |
| 359 | STRINGIFY_CODE(METADATA, BASIC_TYPE) |
| 360 | STRINGIFY_CODE(METADATA, FILE) |
| 361 | STRINGIFY_CODE(METADATA, DERIVED_TYPE) |
| 362 | STRINGIFY_CODE(METADATA, COMPOSITE_TYPE) |
| 363 | STRINGIFY_CODE(METADATA, SUBROUTINE_TYPE) |
| 364 | STRINGIFY_CODE(METADATA, COMPILE_UNIT) |
| 365 | STRINGIFY_CODE(METADATA, SUBPROGRAM) |
| 366 | STRINGIFY_CODE(METADATA, LEXICAL_BLOCK) |
| 367 | STRINGIFY_CODE(METADATA, LEXICAL_BLOCK_FILE) |
| 368 | STRINGIFY_CODE(METADATA, NAMESPACE) |
| 369 | STRINGIFY_CODE(METADATA, TEMPLATE_TYPE) |
| 370 | STRINGIFY_CODE(METADATA, TEMPLATE_VALUE) |
| 371 | STRINGIFY_CODE(METADATA, GLOBAL_VAR) |
| 372 | STRINGIFY_CODE(METADATA, LOCAL_VAR) |
| 373 | STRINGIFY_CODE(METADATA, EXPRESSION) |
| 374 | STRINGIFY_CODE(METADATA, OBJC_PROPERTY) |
| 375 | STRINGIFY_CODE(METADATA, IMPORTED_ENTITY) |
Adrian Prantl | 7ce02c1 | 2015-06-30 00:25:41 +0000 | [diff] [blame] | 376 | STRINGIFY_CODE(METADATA, MODULE) |
Adrian Prantl | 639592d | 2017-01-03 19:17:49 +0000 | [diff] [blame] | 377 | STRINGIFY_CODE(METADATA, MACRO) |
| 378 | STRINGIFY_CODE(METADATA, MACRO_FILE) |
| 379 | STRINGIFY_CODE(METADATA, STRINGS) |
| 380 | STRINGIFY_CODE(METADATA, GLOBAL_DECL_ATTACHMENT) |
| 381 | STRINGIFY_CODE(METADATA, GLOBAL_VAR_EXPR) |
Mehdi Amini | 89bf969 | 2016-12-28 22:30:28 +0000 | [diff] [blame] | 382 | STRINGIFY_CODE(METADATA, INDEX_OFFSET) |
| 383 | STRINGIFY_CODE(METADATA, INDEX) |
Devang Patel | e54abc9 | 2009-07-22 17:43:22 +0000 | [diff] [blame] | 384 | } |
Teresa Johnson | 778af35 | 2015-11-15 02:00:09 +0000 | [diff] [blame] | 385 | case bitc::METADATA_KIND_BLOCK_ID: |
| 386 | switch (CodeID) { |
| 387 | default: |
| 388 | return nullptr; |
| 389 | STRINGIFY_CODE(METADATA, KIND) |
| 390 | } |
Chad Rosier | 837b4e4 | 2011-12-07 21:45:13 +0000 | [diff] [blame] | 391 | case bitc::USELIST_BLOCK_ID: |
| 392 | switch(CodeID) { |
Craig Topper | 573faec | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 393 | default:return nullptr; |
Duncan P. N. Exon Smith | bd24fe8 | 2014-07-28 21:19:41 +0000 | [diff] [blame] | 394 | case bitc::USELIST_CODE_DEFAULT: return "USELIST_CODE_DEFAULT"; |
| 395 | case bitc::USELIST_CODE_BB: return "USELIST_CODE_BB"; |
Chad Rosier | 837b4e4 | 2011-12-07 21:45:13 +0000 | [diff] [blame] | 396 | } |
Sanjoy Das | 6588d8d | 2016-04-26 05:59:08 +0000 | [diff] [blame] | 397 | |
| 398 | case bitc::OPERAND_BUNDLE_TAGS_BLOCK_ID: |
| 399 | switch(CodeID) { |
| 400 | default: return nullptr; |
| 401 | case bitc::OPERAND_BUNDLE_TAG: return "OPERAND_BUNDLE_TAG"; |
| 402 | } |
Peter Collingbourne | 6163b4a | 2017-04-17 17:51:36 +0000 | [diff] [blame] | 403 | case bitc::STRTAB_BLOCK_ID: |
| 404 | switch(CodeID) { |
| 405 | default: return nullptr; |
| 406 | case bitc::STRTAB_BLOB: return "BLOB"; |
| 407 | } |
Peter Collingbourne | fdc1250 | 2017-06-27 23:50:11 +0000 | [diff] [blame] | 408 | case bitc::SYMTAB_BLOCK_ID: |
| 409 | switch(CodeID) { |
| 410 | default: return nullptr; |
| 411 | case bitc::SYMTAB_BLOB: return "BLOB"; |
| 412 | } |
Chris Lattner | b30c925 | 2007-04-29 21:48:19 +0000 | [diff] [blame] | 413 | } |
Duncan P. N. Exon Smith | 70dde5f | 2015-06-29 22:50:32 +0000 | [diff] [blame] | 414 | #undef STRINGIFY_CODE |
Chris Lattner | b30c925 | 2007-04-29 21:48:19 +0000 | [diff] [blame] | 415 | } |
| 416 | |
Chris Lattner | 2443747 | 2009-04-27 17:59:34 +0000 | [diff] [blame] | 417 | struct PerRecordStats { |
| 418 | unsigned NumInstances; |
Chris Lattner | c167cac | 2009-04-27 18:15:27 +0000 | [diff] [blame] | 419 | unsigned NumAbbrev; |
| 420 | uint64_t TotalBits; |
Daniel Dunbar | 80ba3d2 | 2009-09-25 16:03:57 +0000 | [diff] [blame] | 421 | |
Mehdi Amini | 33ee997 | 2015-10-21 06:10:55 +0000 | [diff] [blame] | 422 | PerRecordStats() : NumInstances(0), NumAbbrev(0), TotalBits(0) {} |
Chris Lattner | 2443747 | 2009-04-27 17:59:34 +0000 | [diff] [blame] | 423 | }; |
Chris Lattner | 4238d47 | 2007-04-29 20:00:02 +0000 | [diff] [blame] | 424 | |
| 425 | struct PerBlockIDStats { |
| 426 | /// NumInstances - This the number of times this block ID has been seen. |
| 427 | unsigned NumInstances; |
Daniel Dunbar | 80ba3d2 | 2009-09-25 16:03:57 +0000 | [diff] [blame] | 428 | |
Chris Lattner | 4238d47 | 2007-04-29 20:00:02 +0000 | [diff] [blame] | 429 | /// NumBits - The total size in bits of all of these blocks. |
| 430 | uint64_t NumBits; |
Daniel Dunbar | 80ba3d2 | 2009-09-25 16:03:57 +0000 | [diff] [blame] | 431 | |
Chris Lattner | 4238d47 | 2007-04-29 20:00:02 +0000 | [diff] [blame] | 432 | /// NumSubBlocks - The total number of blocks these blocks contain. |
| 433 | unsigned NumSubBlocks; |
Daniel Dunbar | 80ba3d2 | 2009-09-25 16:03:57 +0000 | [diff] [blame] | 434 | |
Chris Lattner | 4238d47 | 2007-04-29 20:00:02 +0000 | [diff] [blame] | 435 | /// NumAbbrevs - The total number of abbreviations. |
| 436 | unsigned NumAbbrevs; |
Daniel Dunbar | 80ba3d2 | 2009-09-25 16:03:57 +0000 | [diff] [blame] | 437 | |
| 438 | /// NumRecords - The total number of records these blocks contain, and the |
Chris Lattner | 4238d47 | 2007-04-29 20:00:02 +0000 | [diff] [blame] | 439 | /// number that are abbreviated. |
| 440 | unsigned NumRecords, NumAbbreviatedRecords; |
Daniel Dunbar | 80ba3d2 | 2009-09-25 16:03:57 +0000 | [diff] [blame] | 441 | |
Chris Lattner | b1e85b5 | 2007-05-05 01:46:49 +0000 | [diff] [blame] | 442 | /// CodeFreq - Keep track of the number of times we see each code. |
Chris Lattner | 2443747 | 2009-04-27 17:59:34 +0000 | [diff] [blame] | 443 | std::vector<PerRecordStats> CodeFreq; |
Daniel Dunbar | 80ba3d2 | 2009-09-25 16:03:57 +0000 | [diff] [blame] | 444 | |
Chris Lattner | 4238d47 | 2007-04-29 20:00:02 +0000 | [diff] [blame] | 445 | PerBlockIDStats() |
| 446 | : NumInstances(0), NumBits(0), |
| 447 | NumSubBlocks(0), NumAbbrevs(0), NumRecords(0), NumAbbreviatedRecords(0) {} |
| 448 | }; |
| 449 | |
| 450 | static std::map<unsigned, PerBlockIDStats> BlockIDStats; |
| 451 | |
| 452 | |
| 453 | |
Zachary Turner | 9326308 | 2016-10-03 18:17:18 +0000 | [diff] [blame] | 454 | /// ReportError - All bitcode analysis errors go through this function, making this a |
Chris Lattner | 32de633 | 2007-04-29 08:31:14 +0000 | [diff] [blame] | 455 | /// good place to breakpoint if debugging. |
Zachary Turner | 9326308 | 2016-10-03 18:17:18 +0000 | [diff] [blame] | 456 | static bool ReportError(const Twine &Err) { |
Jonas Devlieghere | ae6875e | 2018-04-21 21:11:59 +0000 | [diff] [blame] | 457 | WithColor::error() << Err << "\n"; |
Chris Lattner | 32de633 | 2007-04-29 08:31:14 +0000 | [diff] [blame] | 458 | return true; |
| 459 | } |
| 460 | |
Peter Collingbourne | 8fc9b4d | 2016-11-08 04:17:11 +0000 | [diff] [blame] | 461 | static bool decodeMetadataStringsBlob(StringRef Indent, |
Duncan P. N. Exon Smith | a73e509 | 2016-03-27 23:17:54 +0000 | [diff] [blame] | 462 | ArrayRef<uint64_t> Record, |
| 463 | StringRef Blob) { |
| 464 | if (Blob.empty()) |
| 465 | return true; |
| 466 | |
| 467 | if (Record.size() != 2) |
| 468 | return true; |
| 469 | |
| 470 | unsigned NumStrings = Record[0]; |
| 471 | unsigned StringsOffset = Record[1]; |
| 472 | outs() << " num-strings = " << NumStrings << " {\n"; |
| 473 | |
| 474 | StringRef Lengths = Blob.slice(0, StringsOffset); |
Peter Collingbourne | 8fc9b4d | 2016-11-08 04:17:11 +0000 | [diff] [blame] | 475 | SimpleBitstreamCursor R(Lengths); |
Duncan P. N. Exon Smith | a73e509 | 2016-03-27 23:17:54 +0000 | [diff] [blame] | 476 | StringRef Strings = Blob.drop_front(StringsOffset); |
| 477 | do { |
| 478 | if (R.AtEndOfStream()) |
Zachary Turner | 9326308 | 2016-10-03 18:17:18 +0000 | [diff] [blame] | 479 | return ReportError("bad length"); |
Duncan P. N. Exon Smith | a73e509 | 2016-03-27 23:17:54 +0000 | [diff] [blame] | 480 | |
| 481 | unsigned Size = R.ReadVBR(6); |
| 482 | if (Strings.size() < Size) |
Zachary Turner | 9326308 | 2016-10-03 18:17:18 +0000 | [diff] [blame] | 483 | return ReportError("truncated chars"); |
Duncan P. N. Exon Smith | a73e509 | 2016-03-27 23:17:54 +0000 | [diff] [blame] | 484 | |
| 485 | outs() << Indent << " '"; |
| 486 | outs().write_escaped(Strings.slice(0, Size), /*hex=*/true); |
| 487 | outs() << "'\n"; |
| 488 | Strings = Strings.drop_front(Size); |
| 489 | } while (--NumStrings); |
| 490 | |
| 491 | outs() << Indent << " }"; |
| 492 | return false; |
| 493 | } |
| 494 | |
Peter Collingbourne | 8fc9b4d | 2016-11-08 04:17:11 +0000 | [diff] [blame] | 495 | static bool decodeBlob(unsigned Code, unsigned BlockID, StringRef Indent, |
| 496 | ArrayRef<uint64_t> Record, StringRef Blob) { |
Duncan P. N. Exon Smith | a73e509 | 2016-03-27 23:17:54 +0000 | [diff] [blame] | 497 | if (BlockID != bitc::METADATA_BLOCK_ID) |
| 498 | return true; |
| 499 | if (Code != bitc::METADATA_STRINGS) |
| 500 | return true; |
| 501 | |
Peter Collingbourne | 8fc9b4d | 2016-11-08 04:17:11 +0000 | [diff] [blame] | 502 | return decodeMetadataStringsBlob(Indent, Record, Blob); |
Duncan P. N. Exon Smith | a73e509 | 2016-03-27 23:17:54 +0000 | [diff] [blame] | 503 | } |
| 504 | |
Chris Lattner | 32de633 | 2007-04-29 08:31:14 +0000 | [diff] [blame] | 505 | /// ParseBlock - Read a block, updating statistics, etc. |
Peter Collingbourne | 8fc9b4d | 2016-11-08 04:17:11 +0000 | [diff] [blame] | 506 | static bool ParseBlock(BitstreamCursor &Stream, BitstreamBlockInfo &BlockInfo, |
| 507 | unsigned BlockID, unsigned IndentLevel, |
| 508 | CurStreamTypeType CurStreamType) { |
Chris Lattner | 1772b12d | 2007-05-05 00:17:42 +0000 | [diff] [blame] | 509 | std::string Indent(IndentLevel*2, ' '); |
Chris Lattner | 4238d47 | 2007-04-29 20:00:02 +0000 | [diff] [blame] | 510 | uint64_t BlockBitStart = Stream.GetCurrentBitNo(); |
Chris Lattner | b30c925 | 2007-04-29 21:48:19 +0000 | [diff] [blame] | 511 | |
Chris Lattner | 4238d47 | 2007-04-29 20:00:02 +0000 | [diff] [blame] | 512 | // Get the statistics for this BlockID. |
| 513 | PerBlockIDStats &BlockStats = BlockIDStats[BlockID]; |
Daniel Dunbar | 80ba3d2 | 2009-09-25 16:03:57 +0000 | [diff] [blame] | 514 | |
Chris Lattner | 4238d47 | 2007-04-29 20:00:02 +0000 | [diff] [blame] | 515 | BlockStats.NumInstances++; |
Daniel Dunbar | 80ba3d2 | 2009-09-25 16:03:57 +0000 | [diff] [blame] | 516 | |
Chris Lattner | 1772b12d | 2007-05-05 00:17:42 +0000 | [diff] [blame] | 517 | // BLOCKINFO is a special part of the stream. |
Richard Smith | 0f73f7a | 2016-02-06 00:46:09 +0000 | [diff] [blame] | 518 | bool DumpRecords = Dump; |
Chris Lattner | 1772b12d | 2007-05-05 00:17:42 +0000 | [diff] [blame] | 519 | if (BlockID == bitc::BLOCKINFO_BLOCK_ID) { |
Chris Lattner | e0ac6f8 | 2012-03-19 23:40:48 +0000 | [diff] [blame] | 520 | if (Dump) outs() << Indent << "<BLOCKINFO_BLOCK/>\n"; |
Peter Collingbourne | 8fc9b4d | 2016-11-08 04:17:11 +0000 | [diff] [blame] | 521 | Optional<BitstreamBlockInfo> NewBlockInfo = |
| 522 | Stream.ReadBlockInfoBlock(/*ReadBlockInfoNames=*/true); |
| 523 | if (!NewBlockInfo) |
Zachary Turner | 9326308 | 2016-10-03 18:17:18 +0000 | [diff] [blame] | 524 | return ReportError("Malformed BlockInfoBlock"); |
Peter Collingbourne | 8fc9b4d | 2016-11-08 04:17:11 +0000 | [diff] [blame] | 525 | BlockInfo = std::move(*NewBlockInfo); |
| 526 | Stream.JumpToBit(BlockBitStart); |
Richard Smith | 0f73f7a | 2016-02-06 00:46:09 +0000 | [diff] [blame] | 527 | // It's not really interesting to dump the contents of the blockinfo block. |
| 528 | DumpRecords = false; |
Chris Lattner | 1772b12d | 2007-05-05 00:17:42 +0000 | [diff] [blame] | 529 | } |
Daniel Dunbar | 80ba3d2 | 2009-09-25 16:03:57 +0000 | [diff] [blame] | 530 | |
Chris Lattner | b30c925 | 2007-04-29 21:48:19 +0000 | [diff] [blame] | 531 | unsigned NumWords = 0; |
Chris Lattner | 1772b12d | 2007-05-05 00:17:42 +0000 | [diff] [blame] | 532 | if (Stream.EnterSubBlock(BlockID, &NumWords)) |
Zachary Turner | 9326308 | 2016-10-03 18:17:18 +0000 | [diff] [blame] | 533 | return ReportError("Malformed block record"); |
Chris Lattner | 32de633 | 2007-04-29 08:31:14 +0000 | [diff] [blame] | 534 | |
Mehdi Amini | d2f4701 | 2016-04-01 05:33:11 +0000 | [diff] [blame] | 535 | // Keep it for later, when we see a MODULE_HASH record |
| 536 | uint64_t BlockEntryPos = Stream.getCurrentByteNo(); |
| 537 | |
Craig Topper | 573faec | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 538 | const char *BlockName = nullptr; |
Richard Smith | 0f73f7a | 2016-02-06 00:46:09 +0000 | [diff] [blame] | 539 | if (DumpRecords) { |
Chris Lattner | e0ac6f8 | 2012-03-19 23:40:48 +0000 | [diff] [blame] | 540 | outs() << Indent << "<"; |
Peter Collingbourne | 8fc9b4d | 2016-11-08 04:17:11 +0000 | [diff] [blame] | 541 | if ((BlockName = GetBlockName(BlockID, BlockInfo, CurStreamType))) |
Chris Lattner | e0ac6f8 | 2012-03-19 23:40:48 +0000 | [diff] [blame] | 542 | outs() << BlockName; |
Chris Lattner | b30c925 | 2007-04-29 21:48:19 +0000 | [diff] [blame] | 543 | else |
Chris Lattner | e0ac6f8 | 2012-03-19 23:40:48 +0000 | [diff] [blame] | 544 | outs() << "UnknownBlock" << BlockID; |
Daniel Dunbar | 80ba3d2 | 2009-09-25 16:03:57 +0000 | [diff] [blame] | 545 | |
Chris Lattner | b30c925 | 2007-04-29 21:48:19 +0000 | [diff] [blame] | 546 | if (NonSymbolic && BlockName) |
Chris Lattner | e0ac6f8 | 2012-03-19 23:40:48 +0000 | [diff] [blame] | 547 | outs() << " BlockID=" << BlockID; |
Daniel Dunbar | 80ba3d2 | 2009-09-25 16:03:57 +0000 | [diff] [blame] | 548 | |
Chris Lattner | e0ac6f8 | 2012-03-19 23:40:48 +0000 | [diff] [blame] | 549 | outs() << " NumWords=" << NumWords |
Chris Lattner | bc83720 | 2013-01-19 21:37:14 +0000 | [diff] [blame] | 550 | << " BlockCodeSize=" << Stream.getAbbrevIDWidth() << ">\n"; |
Chris Lattner | b30c925 | 2007-04-29 21:48:19 +0000 | [diff] [blame] | 551 | } |
Daniel Dunbar | 80ba3d2 | 2009-09-25 16:03:57 +0000 | [diff] [blame] | 552 | |
Chris Lattner | 32de633 | 2007-04-29 08:31:14 +0000 | [diff] [blame] | 553 | SmallVector<uint64_t, 64> Record; |
| 554 | |
Mehdi Amini | 89bf969 | 2016-12-28 22:30:28 +0000 | [diff] [blame] | 555 | // Keep the offset to the metadata index if seen. |
| 556 | uint64_t MetadataIndexOffset = 0; |
| 557 | |
Chris Lattner | 32de633 | 2007-04-29 08:31:14 +0000 | [diff] [blame] | 558 | // Read all the records for this block. |
| 559 | while (1) { |
| 560 | if (Stream.AtEndOfStream()) |
Zachary Turner | 9326308 | 2016-10-03 18:17:18 +0000 | [diff] [blame] | 561 | return ReportError("Premature end of bitstream"); |
Chris Lattner | 32de633 | 2007-04-29 08:31:14 +0000 | [diff] [blame] | 562 | |
Chris Lattner | c167cac | 2009-04-27 18:15:27 +0000 | [diff] [blame] | 563 | uint64_t RecordStartBit = Stream.GetCurrentBitNo(); |
Daniel Dunbar | 80ba3d2 | 2009-09-25 16:03:57 +0000 | [diff] [blame] | 564 | |
Chris Lattner | 4156ca7 | 2013-01-20 02:50:32 +0000 | [diff] [blame] | 565 | BitstreamEntry Entry = |
| 566 | Stream.advance(BitstreamCursor::AF_DontAutoprocessAbbrevs); |
| 567 | |
| 568 | switch (Entry.Kind) { |
| 569 | case BitstreamEntry::Error: |
Zachary Turner | 9326308 | 2016-10-03 18:17:18 +0000 | [diff] [blame] | 570 | return ReportError("malformed bitcode file"); |
Chris Lattner | 4156ca7 | 2013-01-20 02:50:32 +0000 | [diff] [blame] | 571 | case BitstreamEntry::EndBlock: { |
Chris Lattner | 4238d47 | 2007-04-29 20:00:02 +0000 | [diff] [blame] | 572 | uint64_t BlockBitEnd = Stream.GetCurrentBitNo(); |
| 573 | BlockStats.NumBits += BlockBitEnd-BlockBitStart; |
Richard Smith | 0f73f7a | 2016-02-06 00:46:09 +0000 | [diff] [blame] | 574 | if (DumpRecords) { |
Chris Lattner | e0ac6f8 | 2012-03-19 23:40:48 +0000 | [diff] [blame] | 575 | outs() << Indent << "</"; |
Chris Lattner | b30c925 | 2007-04-29 21:48:19 +0000 | [diff] [blame] | 576 | if (BlockName) |
Chris Lattner | e0ac6f8 | 2012-03-19 23:40:48 +0000 | [diff] [blame] | 577 | outs() << BlockName << ">\n"; |
Chris Lattner | b30c925 | 2007-04-29 21:48:19 +0000 | [diff] [blame] | 578 | else |
Chris Lattner | e0ac6f8 | 2012-03-19 23:40:48 +0000 | [diff] [blame] | 579 | outs() << "UnknownBlock" << BlockID << ">\n"; |
Chris Lattner | b30c925 | 2007-04-29 21:48:19 +0000 | [diff] [blame] | 580 | } |
Chris Lattner | 32de633 | 2007-04-29 08:31:14 +0000 | [diff] [blame] | 581 | return false; |
Daniel Dunbar | 80ba3d2 | 2009-09-25 16:03:57 +0000 | [diff] [blame] | 582 | } |
Chris Lattner | 4156ca7 | 2013-01-20 02:50:32 +0000 | [diff] [blame] | 583 | |
| 584 | case BitstreamEntry::SubBlock: { |
Chris Lattner | 44b0f10 | 2007-05-05 01:29:31 +0000 | [diff] [blame] | 585 | uint64_t SubBlockBitStart = Stream.GetCurrentBitNo(); |
Peter Collingbourne | 8fc9b4d | 2016-11-08 04:17:11 +0000 | [diff] [blame] | 586 | if (ParseBlock(Stream, BlockInfo, Entry.ID, IndentLevel + 1, |
| 587 | CurStreamType)) |
Chris Lattner | 32de633 | 2007-04-29 08:31:14 +0000 | [diff] [blame] | 588 | return true; |
Chris Lattner | 4238d47 | 2007-04-29 20:00:02 +0000 | [diff] [blame] | 589 | ++BlockStats.NumSubBlocks; |
Chris Lattner | 44b0f10 | 2007-05-05 01:29:31 +0000 | [diff] [blame] | 590 | uint64_t SubBlockBitEnd = Stream.GetCurrentBitNo(); |
Chris Lattner | 4156ca7 | 2013-01-20 02:50:32 +0000 | [diff] [blame] | 591 | |
Chris Lattner | 44b0f10 | 2007-05-05 01:29:31 +0000 | [diff] [blame] | 592 | // Don't include subblock sizes in the size of this block. |
| 593 | BlockBitStart += SubBlockBitEnd-SubBlockBitStart; |
Chris Lattner | 4156ca7 | 2013-01-20 02:50:32 +0000 | [diff] [blame] | 594 | continue; |
| 595 | } |
| 596 | case BitstreamEntry::Record: |
| 597 | // The interesting case. |
Chris Lattner | 32de633 | 2007-04-29 08:31:14 +0000 | [diff] [blame] | 598 | break; |
Chris Lattner | 44b0f10 | 2007-05-05 01:29:31 +0000 | [diff] [blame] | 599 | } |
Chris Lattner | 4156ca7 | 2013-01-20 02:50:32 +0000 | [diff] [blame] | 600 | |
| 601 | if (Entry.ID == bitc::DEFINE_ABBREV) { |
Chris Lattner | 32de633 | 2007-04-29 08:31:14 +0000 | [diff] [blame] | 602 | Stream.ReadAbbrevRecord(); |
Chris Lattner | 4238d47 | 2007-04-29 20:00:02 +0000 | [diff] [blame] | 603 | ++BlockStats.NumAbbrevs; |
Chris Lattner | 4156ca7 | 2013-01-20 02:50:32 +0000 | [diff] [blame] | 604 | continue; |
| 605 | } |
| 606 | |
| 607 | Record.clear(); |
Chris Lattner | 3f75d31 | 2009-04-06 22:44:40 +0000 | [diff] [blame] | 608 | |
Chris Lattner | 4156ca7 | 2013-01-20 02:50:32 +0000 | [diff] [blame] | 609 | ++BlockStats.NumRecords; |
Daniel Dunbar | 80ba3d2 | 2009-09-25 16:03:57 +0000 | [diff] [blame] | 610 | |
Chris Lattner | 4156ca7 | 2013-01-20 02:50:32 +0000 | [diff] [blame] | 611 | StringRef Blob; |
Teresa Johnson | 06b7597 | 2018-06-04 19:20:02 +0000 | [diff] [blame] | 612 | uint64_t CurrentRecordPos = Stream.GetCurrentBitNo(); |
Chris Lattner | 4156ca7 | 2013-01-20 02:50:32 +0000 | [diff] [blame] | 613 | unsigned Code = Stream.readRecord(Entry.ID, Record, &Blob); |
Chris Lattner | b1e85b5 | 2007-05-05 01:46:49 +0000 | [diff] [blame] | 614 | |
Chris Lattner | 4156ca7 | 2013-01-20 02:50:32 +0000 | [diff] [blame] | 615 | // Increment the # occurrences of this code. |
| 616 | if (BlockStats.CodeFreq.size() <= Code) |
| 617 | BlockStats.CodeFreq.resize(Code+1); |
| 618 | BlockStats.CodeFreq[Code].NumInstances++; |
| 619 | BlockStats.CodeFreq[Code].TotalBits += |
| 620 | Stream.GetCurrentBitNo()-RecordStartBit; |
| 621 | if (Entry.ID != bitc::UNABBREV_RECORD) { |
| 622 | BlockStats.CodeFreq[Code].NumAbbrev++; |
| 623 | ++BlockStats.NumAbbreviatedRecords; |
| 624 | } |
Daniel Dunbar | 80ba3d2 | 2009-09-25 16:03:57 +0000 | [diff] [blame] | 625 | |
Richard Smith | 0f73f7a | 2016-02-06 00:46:09 +0000 | [diff] [blame] | 626 | if (DumpRecords) { |
Chris Lattner | 4156ca7 | 2013-01-20 02:50:32 +0000 | [diff] [blame] | 627 | outs() << Indent << " <"; |
| 628 | if (const char *CodeName = |
Peter Collingbourne | 8fc9b4d | 2016-11-08 04:17:11 +0000 | [diff] [blame] | 629 | GetCodeName(Code, BlockID, BlockInfo, CurStreamType)) |
Chris Lattner | 4156ca7 | 2013-01-20 02:50:32 +0000 | [diff] [blame] | 630 | outs() << CodeName; |
| 631 | else |
| 632 | outs() << "UnknownCode" << Code; |
Peter Collingbourne | 8fc9b4d | 2016-11-08 04:17:11 +0000 | [diff] [blame] | 633 | if (NonSymbolic && GetCodeName(Code, BlockID, BlockInfo, CurStreamType)) |
Chris Lattner | 4156ca7 | 2013-01-20 02:50:32 +0000 | [diff] [blame] | 634 | outs() << " codeid=" << Code; |
Teresa Johnson | 6b9f46e | 2015-10-08 15:56:24 +0000 | [diff] [blame] | 635 | const BitCodeAbbrev *Abbv = nullptr; |
| 636 | if (Entry.ID != bitc::UNABBREV_RECORD) { |
| 637 | Abbv = Stream.getAbbrev(Entry.ID); |
Chris Lattner | 4156ca7 | 2013-01-20 02:50:32 +0000 | [diff] [blame] | 638 | outs() << " abbrevid=" << Entry.ID; |
Teresa Johnson | 6b9f46e | 2015-10-08 15:56:24 +0000 | [diff] [blame] | 639 | } |
Chris Lattner | b30c925 | 2007-04-29 21:48:19 +0000 | [diff] [blame] | 640 | |
Chris Lattner | 4156ca7 | 2013-01-20 02:50:32 +0000 | [diff] [blame] | 641 | for (unsigned i = 0, e = Record.size(); i != e; ++i) |
| 642 | outs() << " op" << i << "=" << (int64_t)Record[i]; |
Daniel Dunbar | 80ba3d2 | 2009-09-25 16:03:57 +0000 | [diff] [blame] | 643 | |
Mehdi Amini | 89bf969 | 2016-12-28 22:30:28 +0000 | [diff] [blame] | 644 | // If we found a metadata index, let's verify that we had an offset before |
| 645 | // and validate its forward reference offset was correct! |
| 646 | if (BlockID == bitc::METADATA_BLOCK_ID) { |
| 647 | if (Code == bitc::METADATA_INDEX_OFFSET) { |
Mehdi Amini | 6fc58a7 | 2016-12-28 23:45:54 +0000 | [diff] [blame] | 648 | if (Record.size() != 2) |
| 649 | outs() << "(Invalid record)"; |
| 650 | else { |
| 651 | auto Offset = Record[0] + (Record[1] << 32); |
| 652 | MetadataIndexOffset = Stream.GetCurrentBitNo() + Offset; |
| 653 | } |
Mehdi Amini | 89bf969 | 2016-12-28 22:30:28 +0000 | [diff] [blame] | 654 | } |
| 655 | if (Code == bitc::METADATA_INDEX) { |
| 656 | outs() << " (offset "; |
| 657 | if (MetadataIndexOffset == RecordStartBit) |
| 658 | outs() << "match)"; |
| 659 | else |
| 660 | outs() << "mismatch: " << MetadataIndexOffset << " vs " |
| 661 | << RecordStartBit << ")"; |
| 662 | } |
| 663 | } |
| 664 | |
Mehdi Amini | d2f4701 | 2016-04-01 05:33:11 +0000 | [diff] [blame] | 665 | // If we found a module hash, let's verify that it matches! |
Peter Collingbourne | 67a3f7f | 2017-07-06 17:56:01 +0000 | [diff] [blame] | 666 | if (BlockID == bitc::MODULE_BLOCK_ID && Code == bitc::MODULE_CODE_HASH && |
| 667 | !CheckHash.empty()) { |
Mehdi Amini | d2f4701 | 2016-04-01 05:33:11 +0000 | [diff] [blame] | 668 | if (Record.size() != 5) |
| 669 | outs() << " (invalid)"; |
| 670 | else { |
| 671 | // Recompute the hash and compare it to the one in the bitcode |
| 672 | SHA1 Hasher; |
| 673 | StringRef Hash; |
Peter Collingbourne | 67a3f7f | 2017-07-06 17:56:01 +0000 | [diff] [blame] | 674 | Hasher.update(CheckHash); |
Mehdi Amini | d2f4701 | 2016-04-01 05:33:11 +0000 | [diff] [blame] | 675 | { |
Peter Collingbourne | ca0a598 | 2016-12-01 05:47:58 +0000 | [diff] [blame] | 676 | int BlockSize = (CurrentRecordPos / 8) - BlockEntryPos; |
Mehdi Amini | d2f4701 | 2016-04-01 05:33:11 +0000 | [diff] [blame] | 677 | auto Ptr = Stream.getPointerToByte(BlockEntryPos, BlockSize); |
| 678 | Hasher.update(ArrayRef<uint8_t>(Ptr, BlockSize)); |
| 679 | Hash = Hasher.result(); |
| 680 | } |
| 681 | SmallString<20> RecordedHash; |
| 682 | RecordedHash.resize(20); |
| 683 | int Pos = 0; |
| 684 | for (auto &Val : Record) { |
| 685 | assert(!(Val >> 32) && "Unexpected high bits set"); |
| 686 | RecordedHash[Pos++] = (Val >> 24) & 0xFF; |
| 687 | RecordedHash[Pos++] = (Val >> 16) & 0xFF; |
| 688 | RecordedHash[Pos++] = (Val >> 8) & 0xFF; |
| 689 | RecordedHash[Pos++] = (Val >> 0) & 0xFF; |
| 690 | } |
| 691 | if (Hash == RecordedHash) |
| 692 | outs() << " (match)"; |
| 693 | else |
| 694 | outs() << " (!mismatch!)"; |
| 695 | } |
| 696 | } |
| 697 | |
Chris Lattner | 4156ca7 | 2013-01-20 02:50:32 +0000 | [diff] [blame] | 698 | outs() << "/>"; |
Daniel Dunbar | 80ba3d2 | 2009-09-25 16:03:57 +0000 | [diff] [blame] | 699 | |
Teresa Johnson | 6b9f46e | 2015-10-08 15:56:24 +0000 | [diff] [blame] | 700 | if (Abbv) { |
| 701 | for (unsigned i = 1, e = Abbv->getNumOperandInfos(); i != e; ++i) { |
| 702 | const BitCodeAbbrevOp &Op = Abbv->getOperandInfo(i); |
| 703 | if (!Op.isEncoding() || Op.getEncoding() != BitCodeAbbrevOp::Array) |
| 704 | continue; |
| 705 | assert(i + 2 == e && "Array op not second to last"); |
| 706 | std::string Str; |
| 707 | bool ArrayIsPrintable = true; |
| 708 | for (unsigned j = i - 1, je = Record.size(); j != je; ++j) { |
Michael Kruse | 6069e66 | 2018-07-26 15:31:41 +0000 | [diff] [blame] | 709 | if (!isPrint(static_cast<unsigned char>(Record[j]))) { |
Teresa Johnson | 6b9f46e | 2015-10-08 15:56:24 +0000 | [diff] [blame] | 710 | ArrayIsPrintable = false; |
| 711 | break; |
| 712 | } |
| 713 | Str += (char)Record[j]; |
| 714 | } |
Teresa Johnson | 9dd98c0 | 2015-11-02 18:02:11 +0000 | [diff] [blame] | 715 | if (ArrayIsPrintable) |
| 716 | outs() << " record string = '" << Str << "'"; |
Teresa Johnson | 6b9f46e | 2015-10-08 15:56:24 +0000 | [diff] [blame] | 717 | break; |
| 718 | } |
| 719 | } |
| 720 | |
Peter Collingbourne | 8fc9b4d | 2016-11-08 04:17:11 +0000 | [diff] [blame] | 721 | if (Blob.data() && decodeBlob(Code, BlockID, Indent, Record, Blob)) { |
Chris Lattner | 4156ca7 | 2013-01-20 02:50:32 +0000 | [diff] [blame] | 722 | outs() << " blob data = "; |
Jordan Rose | 1826197 | 2015-05-13 18:51:49 +0000 | [diff] [blame] | 723 | if (ShowBinaryBlobs) { |
| 724 | outs() << "'"; |
| 725 | outs().write_escaped(Blob, /*hex=*/true) << "'"; |
| 726 | } else { |
| 727 | bool BlobIsPrintable = true; |
| 728 | for (unsigned i = 0, e = Blob.size(); i != e; ++i) |
Michael Kruse | 6069e66 | 2018-07-26 15:31:41 +0000 | [diff] [blame] | 729 | if (!isPrint(static_cast<unsigned char>(Blob[i]))) { |
Jordan Rose | 1826197 | 2015-05-13 18:51:49 +0000 | [diff] [blame] | 730 | BlobIsPrintable = false; |
| 731 | break; |
| 732 | } |
Daniel Dunbar | 80ba3d2 | 2009-09-25 16:03:57 +0000 | [diff] [blame] | 733 | |
Jordan Rose | 1826197 | 2015-05-13 18:51:49 +0000 | [diff] [blame] | 734 | if (BlobIsPrintable) |
| 735 | outs() << "'" << Blob << "'"; |
| 736 | else |
| 737 | outs() << "unprintable, " << Blob.size() << " bytes."; |
| 738 | } |
Chris Lattner | b30c925 | 2007-04-29 21:48:19 +0000 | [diff] [blame] | 739 | } |
Daniel Dunbar | 80ba3d2 | 2009-09-25 16:03:57 +0000 | [diff] [blame] | 740 | |
Chris Lattner | 4156ca7 | 2013-01-20 02:50:32 +0000 | [diff] [blame] | 741 | outs() << "\n"; |
Chris Lattner | 32de633 | 2007-04-29 08:31:14 +0000 | [diff] [blame] | 742 | } |
Peter Collingbourne | ca0a598 | 2016-12-01 05:47:58 +0000 | [diff] [blame] | 743 | |
| 744 | // Make sure that we can skip the current record. |
| 745 | Stream.JumpToBit(CurrentRecordPos); |
| 746 | Stream.skipRecord(Entry.ID); |
Chris Lattner | 32de633 | 2007-04-29 08:31:14 +0000 | [diff] [blame] | 747 | } |
| 748 | } |
| 749 | |
Chris Lattner | 4238d47 | 2007-04-29 20:00:02 +0000 | [diff] [blame] | 750 | static void PrintSize(double Bits) { |
Jan Wen Voung | 851343c | 2012-09-05 20:55:54 +0000 | [diff] [blame] | 751 | outs() << format("%.2f/%.2fB/%luW", Bits, Bits/8,(unsigned long)(Bits/32)); |
Chris Lattner | 2443747 | 2009-04-27 17:59:34 +0000 | [diff] [blame] | 752 | } |
| 753 | static void PrintSize(uint64_t Bits) { |
Jan Wen Voung | 851343c | 2012-09-05 20:55:54 +0000 | [diff] [blame] | 754 | outs() << format("%lub/%.2fB/%luW", (unsigned long)Bits, |
| 755 | (double)Bits/8, (unsigned long)(Bits/32)); |
Chris Lattner | 4238d47 | 2007-04-29 20:00:02 +0000 | [diff] [blame] | 756 | } |
| 757 | |
Brian Gesiak | 37f1378 | 2018-04-21 23:52:04 +0000 | [diff] [blame] | 758 | static CurStreamTypeType ReadSignature(BitstreamCursor &Stream) { |
| 759 | char Signature[6]; |
| 760 | Signature[0] = Stream.Read(8); |
| 761 | Signature[1] = Stream.Read(8); |
| 762 | |
| 763 | // Autodetect the file contents, if it is one we know. |
| 764 | if (Signature[0] == 'C' && Signature[1] == 'P') { |
| 765 | Signature[2] = Stream.Read(8); |
| 766 | Signature[3] = Stream.Read(8); |
| 767 | if (Signature[2] == 'C' && Signature[3] == 'H') |
| 768 | return ClangSerializedASTBitstream; |
| 769 | } else if (Signature[0] == 'D' && Signature[1] == 'I') { |
| 770 | Signature[2] = Stream.Read(8); |
| 771 | Signature[3] = Stream.Read(8); |
| 772 | if (Signature[2] == 'A' && Signature[3] == 'G') |
| 773 | return ClangSerializedDiagnosticsBitstream; |
| 774 | } else { |
| 775 | Signature[2] = Stream.Read(4); |
| 776 | Signature[3] = Stream.Read(4); |
| 777 | Signature[4] = Stream.Read(4); |
| 778 | Signature[5] = Stream.Read(4); |
| 779 | if (Signature[0] == 'B' && Signature[1] == 'C' && |
| 780 | Signature[2] == 0x0 && Signature[3] == 0xC && |
| 781 | Signature[4] == 0xE && Signature[5] == 0xD) |
| 782 | return LLVMIRBitstream; |
| 783 | } |
| 784 | return UnknownBitstream; |
| 785 | } |
| 786 | |
Jordan Rose | d421075 | 2014-08-30 17:07:55 +0000 | [diff] [blame] | 787 | static bool openBitcodeFile(StringRef Path, |
| 788 | std::unique_ptr<MemoryBuffer> &MemBuf, |
Jordan Rose | d421075 | 2014-08-30 17:07:55 +0000 | [diff] [blame] | 789 | BitstreamCursor &Stream, |
| 790 | CurStreamTypeType &CurStreamType) { |
Chris Lattner | 45e0f89 | 2007-04-29 08:12:22 +0000 | [diff] [blame] | 791 | // Read the input file. |
Rafael Espindola | 7cba2a9 | 2014-07-06 17:43:13 +0000 | [diff] [blame] | 792 | ErrorOr<std::unique_ptr<MemoryBuffer>> MemBufOrErr = |
Jordan Rose | d421075 | 2014-08-30 17:07:55 +0000 | [diff] [blame] | 793 | MemoryBuffer::getFileOrSTDIN(Path); |
Rafael Espindola | 7cba2a9 | 2014-07-06 17:43:13 +0000 | [diff] [blame] | 794 | if (std::error_code EC = MemBufOrErr.getError()) |
Zachary Turner | 9326308 | 2016-10-03 18:17:18 +0000 | [diff] [blame] | 795 | return ReportError(Twine("ReportError reading '") + Path + "': " + EC.message()); |
Jordan Rose | d421075 | 2014-08-30 17:07:55 +0000 | [diff] [blame] | 796 | MemBuf = std::move(MemBufOrErr.get()); |
Daniel Dunbar | 80ba3d2 | 2009-09-25 16:03:57 +0000 | [diff] [blame] | 797 | |
Jordan Rose | d421075 | 2014-08-30 17:07:55 +0000 | [diff] [blame] | 798 | if (MemBuf->getBufferSize() & 3) |
Zachary Turner | 9326308 | 2016-10-03 18:17:18 +0000 | [diff] [blame] | 799 | return ReportError("Bitcode stream should be a multiple of 4 bytes in length"); |
Daniel Dunbar | 80ba3d2 | 2009-09-25 16:03:57 +0000 | [diff] [blame] | 800 | |
Jordan Rose | d421075 | 2014-08-30 17:07:55 +0000 | [diff] [blame] | 801 | const unsigned char *BufPtr = (const unsigned char *)MemBuf->getBufferStart(); |
| 802 | const unsigned char *EndBufPtr = BufPtr + MemBuf->getBufferSize(); |
Daniel Dunbar | 80ba3d2 | 2009-09-25 16:03:57 +0000 | [diff] [blame] | 803 | |
Chris Lattner | e2a466b | 2009-04-06 20:54:32 +0000 | [diff] [blame] | 804 | // If we have a wrapper header, parse it and ignore the non-bc file contents. |
| 805 | // The magic number is 0x0B17C0DE stored in little endian. |
Akira Hatanaka | 0dc389d | 2016-01-29 05:55:09 +0000 | [diff] [blame] | 806 | if (isBitcodeWrapper(BufPtr, EndBufPtr)) { |
Mehdi Amini | d4b1021 | 2016-04-01 05:19:14 +0000 | [diff] [blame] | 807 | if (MemBuf->getBufferSize() < BWH_HeaderSize) |
Zachary Turner | 9326308 | 2016-10-03 18:17:18 +0000 | [diff] [blame] | 808 | return ReportError("Invalid bitcode wrapper header"); |
Akira Hatanaka | 0dc389d | 2016-01-29 05:55:09 +0000 | [diff] [blame] | 809 | |
| 810 | if (Dump) { |
| 811 | unsigned Magic = support::endian::read32le(&BufPtr[BWH_MagicField]); |
| 812 | unsigned Version = support::endian::read32le(&BufPtr[BWH_VersionField]); |
| 813 | unsigned Offset = support::endian::read32le(&BufPtr[BWH_OffsetField]); |
| 814 | unsigned Size = support::endian::read32le(&BufPtr[BWH_SizeField]); |
| 815 | unsigned CPUType = support::endian::read32le(&BufPtr[BWH_CPUTypeField]); |
| 816 | |
| 817 | outs() << "<BITCODE_WRAPPER_HEADER" |
| 818 | << " Magic=" << format_hex(Magic, 10) |
| 819 | << " Version=" << format_hex(Version, 10) |
| 820 | << " Offset=" << format_hex(Offset, 10) |
| 821 | << " Size=" << format_hex(Size, 10) |
| 822 | << " CPUType=" << format_hex(CPUType, 10) << "/>\n"; |
| 823 | } |
| 824 | |
Derek Schuff | 2ea9387 | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 825 | if (SkipBitcodeWrapperHeader(BufPtr, EndBufPtr, true)) |
Zachary Turner | 9326308 | 2016-10-03 18:17:18 +0000 | [diff] [blame] | 826 | return ReportError("Invalid bitcode wrapper header"); |
Akira Hatanaka | 0dc389d | 2016-01-29 05:55:09 +0000 | [diff] [blame] | 827 | } |
Daniel Dunbar | 80ba3d2 | 2009-09-25 16:03:57 +0000 | [diff] [blame] | 828 | |
Peter Collingbourne | 8fc9b4d | 2016-11-08 04:17:11 +0000 | [diff] [blame] | 829 | Stream = BitstreamCursor(ArrayRef<uint8_t>(BufPtr, EndBufPtr)); |
Brian Gesiak | 37f1378 | 2018-04-21 23:52:04 +0000 | [diff] [blame] | 830 | CurStreamType = ReadSignature(Stream); |
Chris Lattner | 45e0f89 | 2007-04-29 08:12:22 +0000 | [diff] [blame] | 831 | |
Jordan Rose | d421075 | 2014-08-30 17:07:55 +0000 | [diff] [blame] | 832 | return false; |
| 833 | } |
| 834 | |
| 835 | /// AnalyzeBitcode - Analyze the bitcode file specified by InputFilename. |
| 836 | static int AnalyzeBitcode() { |
| 837 | std::unique_ptr<MemoryBuffer> StreamBuffer; |
Jordan Rose | d421075 | 2014-08-30 17:07:55 +0000 | [diff] [blame] | 838 | BitstreamCursor Stream; |
Peter Collingbourne | 8fc9b4d | 2016-11-08 04:17:11 +0000 | [diff] [blame] | 839 | BitstreamBlockInfo BlockInfo; |
Jordan Rose | d421075 | 2014-08-30 17:07:55 +0000 | [diff] [blame] | 840 | CurStreamTypeType CurStreamType; |
Peter Collingbourne | 8fc9b4d | 2016-11-08 04:17:11 +0000 | [diff] [blame] | 841 | if (openBitcodeFile(InputFilename, StreamBuffer, Stream, CurStreamType)) |
Jordan Rose | d421075 | 2014-08-30 17:07:55 +0000 | [diff] [blame] | 842 | return true; |
Peter Collingbourne | 8fc9b4d | 2016-11-08 04:17:11 +0000 | [diff] [blame] | 843 | Stream.setBlockInfo(&BlockInfo); |
Jordan Rose | d421075 | 2014-08-30 17:07:55 +0000 | [diff] [blame] | 844 | |
| 845 | // Read block info from BlockInfoFilename, if specified. |
| 846 | // The block info must be a top-level block. |
| 847 | if (!BlockInfoFilename.empty()) { |
| 848 | std::unique_ptr<MemoryBuffer> BlockInfoBuffer; |
Jordan Rose | d421075 | 2014-08-30 17:07:55 +0000 | [diff] [blame] | 849 | BitstreamCursor BlockInfoCursor; |
| 850 | CurStreamTypeType BlockInfoStreamType; |
Peter Collingbourne | 8fc9b4d | 2016-11-08 04:17:11 +0000 | [diff] [blame] | 851 | if (openBitcodeFile(BlockInfoFilename, BlockInfoBuffer, BlockInfoCursor, |
| 852 | BlockInfoStreamType)) |
Jordan Rose | d421075 | 2014-08-30 17:07:55 +0000 | [diff] [blame] | 853 | return true; |
| 854 | |
| 855 | while (!BlockInfoCursor.AtEndOfStream()) { |
| 856 | unsigned Code = BlockInfoCursor.ReadCode(); |
| 857 | if (Code != bitc::ENTER_SUBBLOCK) |
Zachary Turner | 9326308 | 2016-10-03 18:17:18 +0000 | [diff] [blame] | 858 | return ReportError("Invalid record at top-level in block info file"); |
Jordan Rose | d421075 | 2014-08-30 17:07:55 +0000 | [diff] [blame] | 859 | |
| 860 | unsigned BlockID = BlockInfoCursor.ReadSubBlockID(); |
| 861 | if (BlockID == bitc::BLOCKINFO_BLOCK_ID) { |
Peter Collingbourne | 8fc9b4d | 2016-11-08 04:17:11 +0000 | [diff] [blame] | 862 | Optional<BitstreamBlockInfo> NewBlockInfo = |
| 863 | BlockInfoCursor.ReadBlockInfoBlock(/*ReadBlockInfoNames=*/true); |
| 864 | if (!NewBlockInfo) |
Zachary Turner | 9326308 | 2016-10-03 18:17:18 +0000 | [diff] [blame] | 865 | return ReportError("Malformed BlockInfoBlock in block info file"); |
Peter Collingbourne | 8fc9b4d | 2016-11-08 04:17:11 +0000 | [diff] [blame] | 866 | BlockInfo = std::move(*NewBlockInfo); |
Jordan Rose | d421075 | 2014-08-30 17:07:55 +0000 | [diff] [blame] | 867 | break; |
| 868 | } |
| 869 | |
| 870 | BlockInfoCursor.SkipBlock(); |
| 871 | } |
Jordan Rose | d421075 | 2014-08-30 17:07:55 +0000 | [diff] [blame] | 872 | } |
| 873 | |
Chris Lattner | 4238d47 | 2007-04-29 20:00:02 +0000 | [diff] [blame] | 874 | unsigned NumTopBlocks = 0; |
Daniel Dunbar | 80ba3d2 | 2009-09-25 16:03:57 +0000 | [diff] [blame] | 875 | |
Chris Lattner | 32de633 | 2007-04-29 08:31:14 +0000 | [diff] [blame] | 876 | // Parse the top-level structure. We only allow blocks at the top-level. |
| 877 | while (!Stream.AtEndOfStream()) { |
| 878 | unsigned Code = Stream.ReadCode(); |
| 879 | if (Code != bitc::ENTER_SUBBLOCK) |
Zachary Turner | 9326308 | 2016-10-03 18:17:18 +0000 | [diff] [blame] | 880 | return ReportError("Invalid record at top-level"); |
Daniel Dunbar | 80ba3d2 | 2009-09-25 16:03:57 +0000 | [diff] [blame] | 881 | |
Chris Lattner | 4156ca7 | 2013-01-20 02:50:32 +0000 | [diff] [blame] | 882 | unsigned BlockID = Stream.ReadSubBlockID(); |
| 883 | |
Peter Collingbourne | 8fc9b4d | 2016-11-08 04:17:11 +0000 | [diff] [blame] | 884 | if (ParseBlock(Stream, BlockInfo, BlockID, 0, CurStreamType)) |
Chris Lattner | 32de633 | 2007-04-29 08:31:14 +0000 | [diff] [blame] | 885 | return true; |
Chris Lattner | 4238d47 | 2007-04-29 20:00:02 +0000 | [diff] [blame] | 886 | ++NumTopBlocks; |
Chris Lattner | 32de633 | 2007-04-29 08:31:14 +0000 | [diff] [blame] | 887 | } |
Daniel Dunbar | 80ba3d2 | 2009-09-25 16:03:57 +0000 | [diff] [blame] | 888 | |
Chris Lattner | e0ac6f8 | 2012-03-19 23:40:48 +0000 | [diff] [blame] | 889 | if (Dump) outs() << "\n\n"; |
Daniel Dunbar | 80ba3d2 | 2009-09-25 16:03:57 +0000 | [diff] [blame] | 890 | |
Peter Collingbourne | 8fc9b4d | 2016-11-08 04:17:11 +0000 | [diff] [blame] | 891 | uint64_t BufferSizeBits = Stream.getBitcodeBytes().size() * CHAR_BIT; |
Chris Lattner | 32de633 | 2007-04-29 08:31:14 +0000 | [diff] [blame] | 892 | // Print a summary of the read file. |
Chris Lattner | e0ac6f8 | 2012-03-19 23:40:48 +0000 | [diff] [blame] | 893 | outs() << "Summary of " << InputFilename << ":\n"; |
| 894 | outs() << " Total size: "; |
Chris Lattner | 8f92668 | 2007-05-01 02:43:46 +0000 | [diff] [blame] | 895 | PrintSize(BufferSizeBits); |
Chris Lattner | e0ac6f8 | 2012-03-19 23:40:48 +0000 | [diff] [blame] | 896 | outs() << "\n"; |
| 897 | outs() << " Stream type: "; |
Chris Lattner | 45e0f89 | 2007-04-29 08:12:22 +0000 | [diff] [blame] | 898 | switch (CurStreamType) { |
Brian Gesiak | 37f1378 | 2018-04-21 23:52:04 +0000 | [diff] [blame] | 899 | case UnknownBitstream: |
| 900 | outs() << "unknown\n"; |
| 901 | break; |
| 902 | case LLVMIRBitstream: |
| 903 | outs() << "LLVM IR\n"; |
| 904 | break; |
| 905 | case ClangSerializedASTBitstream: |
| 906 | outs() << "Clang Serialized AST\n"; |
| 907 | break; |
| 908 | case ClangSerializedDiagnosticsBitstream: |
| 909 | outs() << "Clang Serialized Diagnostics\n"; |
| 910 | break; |
Chris Lattner | 45e0f89 | 2007-04-29 08:12:22 +0000 | [diff] [blame] | 911 | } |
Chris Lattner | e0ac6f8 | 2012-03-19 23:40:48 +0000 | [diff] [blame] | 912 | outs() << " # Toplevel Blocks: " << NumTopBlocks << "\n"; |
| 913 | outs() << "\n"; |
Chris Lattner | 4238d47 | 2007-04-29 20:00:02 +0000 | [diff] [blame] | 914 | |
| 915 | // Emit per-block stats. |
Chris Lattner | e0ac6f8 | 2012-03-19 23:40:48 +0000 | [diff] [blame] | 916 | outs() << "Per-block Summary:\n"; |
Chris Lattner | 4238d47 | 2007-04-29 20:00:02 +0000 | [diff] [blame] | 917 | for (std::map<unsigned, PerBlockIDStats>::iterator I = BlockIDStats.begin(), |
| 918 | E = BlockIDStats.end(); I != E; ++I) { |
Chris Lattner | e0ac6f8 | 2012-03-19 23:40:48 +0000 | [diff] [blame] | 919 | outs() << " Block ID #" << I->first; |
Peter Collingbourne | 8fc9b4d | 2016-11-08 04:17:11 +0000 | [diff] [blame] | 920 | if (const char *BlockName = |
| 921 | GetBlockName(I->first, BlockInfo, CurStreamType)) |
Chris Lattner | e0ac6f8 | 2012-03-19 23:40:48 +0000 | [diff] [blame] | 922 | outs() << " (" << BlockName << ")"; |
| 923 | outs() << ":\n"; |
Daniel Dunbar | 80ba3d2 | 2009-09-25 16:03:57 +0000 | [diff] [blame] | 924 | |
Chris Lattner | 4238d47 | 2007-04-29 20:00:02 +0000 | [diff] [blame] | 925 | const PerBlockIDStats &Stats = I->second; |
Chris Lattner | e0ac6f8 | 2012-03-19 23:40:48 +0000 | [diff] [blame] | 926 | outs() << " Num Instances: " << Stats.NumInstances << "\n"; |
| 927 | outs() << " Total Size: "; |
Chris Lattner | 4238d47 | 2007-04-29 20:00:02 +0000 | [diff] [blame] | 928 | PrintSize(Stats.NumBits); |
Chris Lattner | e0ac6f8 | 2012-03-19 23:40:48 +0000 | [diff] [blame] | 929 | outs() << "\n"; |
Daniel Dunbar | 42985bb | 2009-09-25 16:04:21 +0000 | [diff] [blame] | 930 | double pct = (Stats.NumBits * 100.0) / BufferSizeBits; |
Jan Wen Voung | 851343c | 2012-09-05 20:55:54 +0000 | [diff] [blame] | 931 | outs() << " Percent of file: " << format("%2.4f%%", pct) << "\n"; |
Chris Lattner | b1e85b5 | 2007-05-05 01:46:49 +0000 | [diff] [blame] | 932 | if (Stats.NumInstances > 1) { |
Chris Lattner | e0ac6f8 | 2012-03-19 23:40:48 +0000 | [diff] [blame] | 933 | outs() << " Average Size: "; |
Chris Lattner | b1e85b5 | 2007-05-05 01:46:49 +0000 | [diff] [blame] | 934 | PrintSize(Stats.NumBits/(double)Stats.NumInstances); |
Chris Lattner | e0ac6f8 | 2012-03-19 23:40:48 +0000 | [diff] [blame] | 935 | outs() << "\n"; |
| 936 | outs() << " Tot/Avg SubBlocks: " << Stats.NumSubBlocks << "/" |
Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 937 | << Stats.NumSubBlocks/(double)Stats.NumInstances << "\n"; |
Chris Lattner | e0ac6f8 | 2012-03-19 23:40:48 +0000 | [diff] [blame] | 938 | outs() << " Tot/Avg Abbrevs: " << Stats.NumAbbrevs << "/" |
Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 939 | << Stats.NumAbbrevs/(double)Stats.NumInstances << "\n"; |
Chris Lattner | e0ac6f8 | 2012-03-19 23:40:48 +0000 | [diff] [blame] | 940 | outs() << " Tot/Avg Records: " << Stats.NumRecords << "/" |
Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 941 | << Stats.NumRecords/(double)Stats.NumInstances << "\n"; |
Chris Lattner | b1e85b5 | 2007-05-05 01:46:49 +0000 | [diff] [blame] | 942 | } else { |
Chris Lattner | e0ac6f8 | 2012-03-19 23:40:48 +0000 | [diff] [blame] | 943 | outs() << " Num SubBlocks: " << Stats.NumSubBlocks << "\n"; |
| 944 | outs() << " Num Abbrevs: " << Stats.NumAbbrevs << "\n"; |
| 945 | outs() << " Num Records: " << Stats.NumRecords << "\n"; |
Chris Lattner | b1e85b5 | 2007-05-05 01:46:49 +0000 | [diff] [blame] | 946 | } |
Daniel Dunbar | 42985bb | 2009-09-25 16:04:21 +0000 | [diff] [blame] | 947 | if (Stats.NumRecords) { |
| 948 | double pct = (Stats.NumAbbreviatedRecords * 100.0) / Stats.NumRecords; |
Chris Lattner | e0ac6f8 | 2012-03-19 23:40:48 +0000 | [diff] [blame] | 949 | outs() << " Percent Abbrevs: " << format("%2.4f%%", pct) << "\n"; |
Daniel Dunbar | 42985bb | 2009-09-25 16:04:21 +0000 | [diff] [blame] | 950 | } |
Chris Lattner | e0ac6f8 | 2012-03-19 23:40:48 +0000 | [diff] [blame] | 951 | outs() << "\n"; |
Daniel Dunbar | 80ba3d2 | 2009-09-25 16:03:57 +0000 | [diff] [blame] | 952 | |
Chris Lattner | b1e85b5 | 2007-05-05 01:46:49 +0000 | [diff] [blame] | 953 | // Print a histogram of the codes we see. |
| 954 | if (!NoHistogram && !Stats.CodeFreq.empty()) { |
Mehdi Amini | 33ee997 | 2015-10-21 06:10:55 +0000 | [diff] [blame] | 955 | std::vector<std::pair<unsigned, unsigned> > FreqPairs; // <freq,code> |
Chris Lattner | b1e85b5 | 2007-05-05 01:46:49 +0000 | [diff] [blame] | 956 | for (unsigned i = 0, e = Stats.CodeFreq.size(); i != e; ++i) |
Mehdi Amini | 33ee997 | 2015-10-21 06:10:55 +0000 | [diff] [blame] | 957 | if (unsigned Freq = Stats.CodeFreq[i].NumInstances) |
Chris Lattner | b1e85b5 | 2007-05-05 01:46:49 +0000 | [diff] [blame] | 958 | FreqPairs.push_back(std::make_pair(Freq, i)); |
| 959 | std::stable_sort(FreqPairs.begin(), FreqPairs.end()); |
| 960 | std::reverse(FreqPairs.begin(), FreqPairs.end()); |
Daniel Dunbar | 80ba3d2 | 2009-09-25 16:03:57 +0000 | [diff] [blame] | 961 | |
Chris Lattner | e0ac6f8 | 2012-03-19 23:40:48 +0000 | [diff] [blame] | 962 | outs() << "\tRecord Histogram:\n"; |
Richard Smith | 0f73f7a | 2016-02-06 00:46:09 +0000 | [diff] [blame] | 963 | outs() << "\t\t Count # Bits b/Rec % Abv Record Kind\n"; |
Chris Lattner | b1e85b5 | 2007-05-05 01:46:49 +0000 | [diff] [blame] | 964 | for (unsigned i = 0, e = FreqPairs.size(); i != e; ++i) { |
Chris Lattner | c167cac | 2009-04-27 18:15:27 +0000 | [diff] [blame] | 965 | const PerRecordStats &RecStats = Stats.CodeFreq[FreqPairs[i].second]; |
Daniel Dunbar | 80ba3d2 | 2009-09-25 16:03:57 +0000 | [diff] [blame] | 966 | |
Mehdi Amini | 33ee997 | 2015-10-21 06:10:55 +0000 | [diff] [blame] | 967 | outs() << format("\t\t%7d %9lu", |
Jan Wen Voung | 73e562a | 2012-09-05 20:55:57 +0000 | [diff] [blame] | 968 | RecStats.NumInstances, |
| 969 | (unsigned long)RecStats.TotalBits); |
Daniel Dunbar | 80ba3d2 | 2009-09-25 16:03:57 +0000 | [diff] [blame] | 970 | |
Richard Smith | 0f73f7a | 2016-02-06 00:46:09 +0000 | [diff] [blame] | 971 | if (RecStats.NumInstances > 1) |
| 972 | outs() << format(" %9.1f", |
| 973 | (double)RecStats.TotalBits/RecStats.NumInstances); |
| 974 | else |
| 975 | outs() << " "; |
| 976 | |
Chris Lattner | c167cac | 2009-04-27 18:15:27 +0000 | [diff] [blame] | 977 | if (RecStats.NumAbbrev) |
Jan Wen Voung | 851343c | 2012-09-05 20:55:54 +0000 | [diff] [blame] | 978 | outs() << |
Richard Smith | 0f73f7a | 2016-02-06 00:46:09 +0000 | [diff] [blame] | 979 | format(" %7.2f", |
Jan Wen Voung | 851343c | 2012-09-05 20:55:54 +0000 | [diff] [blame] | 980 | (double)RecStats.NumAbbrev/RecStats.NumInstances*100); |
Chris Lattner | c167cac | 2009-04-27 18:15:27 +0000 | [diff] [blame] | 981 | else |
Richard Smith | 0f73f7a | 2016-02-06 00:46:09 +0000 | [diff] [blame] | 982 | outs() << " "; |
Daniel Dunbar | 80ba3d2 | 2009-09-25 16:03:57 +0000 | [diff] [blame] | 983 | |
Richard Smith | 0f73f7a | 2016-02-06 00:46:09 +0000 | [diff] [blame] | 984 | outs() << " "; |
Peter Collingbourne | 8fc9b4d | 2016-11-08 04:17:11 +0000 | [diff] [blame] | 985 | if (const char *CodeName = GetCodeName(FreqPairs[i].second, I->first, |
| 986 | BlockInfo, CurStreamType)) |
Jan Wen Voung | 851343c | 2012-09-05 20:55:54 +0000 | [diff] [blame] | 987 | outs() << CodeName << "\n"; |
Chris Lattner | b1e85b5 | 2007-05-05 01:46:49 +0000 | [diff] [blame] | 988 | else |
Jan Wen Voung | 851343c | 2012-09-05 20:55:54 +0000 | [diff] [blame] | 989 | outs() << "UnknownCode" << FreqPairs[i].second << "\n"; |
Chris Lattner | b1e85b5 | 2007-05-05 01:46:49 +0000 | [diff] [blame] | 990 | } |
Chris Lattner | e0ac6f8 | 2012-03-19 23:40:48 +0000 | [diff] [blame] | 991 | outs() << "\n"; |
Daniel Dunbar | 80ba3d2 | 2009-09-25 16:03:57 +0000 | [diff] [blame] | 992 | |
Chris Lattner | b1e85b5 | 2007-05-05 01:46:49 +0000 | [diff] [blame] | 993 | } |
Chris Lattner | 4238d47 | 2007-04-29 20:00:02 +0000 | [diff] [blame] | 994 | } |
Chris Lattner | 45e0f89 | 2007-04-29 08:12:22 +0000 | [diff] [blame] | 995 | return 0; |
| 996 | } |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 997 | |
Chris Lattner | 32de633 | 2007-04-29 08:31:14 +0000 | [diff] [blame] | 998 | |
Chris Lattner | c30598b | 2006-12-06 01:18:01 +0000 | [diff] [blame] | 999 | int main(int argc, char **argv) { |
Rui Ueyama | 0b9d56a | 2018-04-13 18:26:06 +0000 | [diff] [blame] | 1000 | InitLLVM X(argc, argv); |
Chris Lattner | cc14d25 | 2009-03-06 05:34:10 +0000 | [diff] [blame] | 1001 | cl::ParseCommandLineOptions(argc, argv, "llvm-bcanalyzer file analyzer\n"); |
Chris Lattner | 44dadff | 2007-05-06 09:29:57 +0000 | [diff] [blame] | 1002 | return AnalyzeBitcode(); |
Reid Spencer | dac69c8 | 2004-06-07 17:53:43 +0000 | [diff] [blame] | 1003 | } |