Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1 | //===- BitcodeReader.cpp - Internal BitcodeReader implementation ----------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 9 | |
Teresa Johnson | ad17679 | 2016-11-11 05:34:58 +0000 | [diff] [blame] | 10 | #include "llvm/Bitcode/BitcodeReader.h" |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 11 | #include "MetadataLoader.h" |
| 12 | #include "ValueList.h" |
| 13 | |
Eugene Zelenko | 1804a77 | 2016-08-25 00:45:04 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/APFloat.h" |
| 15 | #include "llvm/ADT/APInt.h" |
| 16 | #include "llvm/ADT/ArrayRef.h" |
| 17 | #include "llvm/ADT/DenseMap.h" |
| 18 | #include "llvm/ADT/None.h" |
Benjamin Kramer | 0a446fd | 2015-03-01 21:28:53 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/STLExtras.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/SmallString.h" |
| 21 | #include "llvm/ADT/SmallVector.h" |
Eugene Zelenko | 1804a77 | 2016-08-25 00:45:04 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/StringRef.h" |
David Majnemer | 3087b22 | 2015-01-20 05:58:07 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/Triple.h" |
Eugene Zelenko | 1804a77 | 2016-08-25 00:45:04 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/Twine.h" |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 25 | #include "llvm/Bitcode/BitstreamReader.h" |
Tobias Grosser | 0a8e12f | 2013-07-26 04:16:55 +0000 | [diff] [blame] | 26 | #include "llvm/Bitcode/LLVMBitCodes.h" |
Eugene Zelenko | 1804a77 | 2016-08-25 00:45:04 +0000 | [diff] [blame] | 27 | #include "llvm/IR/Argument.h" |
| 28 | #include "llvm/IR/Attributes.h" |
Chandler Carruth | 9106521 | 2014-03-05 10:34:14 +0000 | [diff] [blame] | 29 | #include "llvm/IR/AutoUpgrade.h" |
Eugene Zelenko | 1804a77 | 2016-08-25 00:45:04 +0000 | [diff] [blame] | 30 | #include "llvm/IR/BasicBlock.h" |
Artur Pilipenko | 6c7a8ab | 2016-06-24 15:10:29 +0000 | [diff] [blame] | 31 | #include "llvm/IR/CallSite.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame^] | 32 | #include "llvm/IR/CallingConv.h" |
Eugene Zelenko | 1804a77 | 2016-08-25 00:45:04 +0000 | [diff] [blame] | 33 | #include "llvm/IR/Comdat.h" |
| 34 | #include "llvm/IR/Constant.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 35 | #include "llvm/IR/Constants.h" |
Rafael Espindola | 0d68b4c | 2015-03-30 21:36:43 +0000 | [diff] [blame] | 36 | #include "llvm/IR/DebugInfo.h" |
Duncan P. N. Exon Smith | d9901ff | 2015-02-02 18:53:21 +0000 | [diff] [blame] | 37 | #include "llvm/IR/DebugInfoMetadata.h" |
Eugene Zelenko | 1804a77 | 2016-08-25 00:45:04 +0000 | [diff] [blame] | 38 | #include "llvm/IR/DebugLoc.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 39 | #include "llvm/IR/DerivedTypes.h" |
Eugene Zelenko | 1804a77 | 2016-08-25 00:45:04 +0000 | [diff] [blame] | 40 | #include "llvm/IR/DiagnosticInfo.h" |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 41 | #include "llvm/IR/DiagnosticPrinter.h" |
Eugene Zelenko | 1804a77 | 2016-08-25 00:45:04 +0000 | [diff] [blame] | 42 | #include "llvm/IR/Function.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame^] | 43 | #include "llvm/IR/GVMaterializer.h" |
Eugene Zelenko | 1804a77 | 2016-08-25 00:45:04 +0000 | [diff] [blame] | 44 | #include "llvm/IR/GlobalAlias.h" |
| 45 | #include "llvm/IR/GlobalIFunc.h" |
| 46 | #include "llvm/IR/GlobalIndirectSymbol.h" |
| 47 | #include "llvm/IR/GlobalObject.h" |
| 48 | #include "llvm/IR/GlobalValue.h" |
| 49 | #include "llvm/IR/GlobalVariable.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 50 | #include "llvm/IR/InlineAsm.h" |
Mehdi Amini | 8662305 | 2016-12-16 19:16:29 +0000 | [diff] [blame] | 51 | #include "llvm/IR/InstIterator.h" |
Eugene Zelenko | 1804a77 | 2016-08-25 00:45:04 +0000 | [diff] [blame] | 52 | #include "llvm/IR/InstrTypes.h" |
| 53 | #include "llvm/IR/Instruction.h" |
| 54 | #include "llvm/IR/Instructions.h" |
| 55 | #include "llvm/IR/Intrinsics.h" |
Manman Ren | 209b17c | 2013-09-28 00:22:27 +0000 | [diff] [blame] | 56 | #include "llvm/IR/LLVMContext.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 57 | #include "llvm/IR/Module.h" |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 58 | #include "llvm/IR/ModuleSummaryIndex.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 59 | #include "llvm/IR/OperandTraits.h" |
| 60 | #include "llvm/IR/Operator.h" |
Eugene Zelenko | 1804a77 | 2016-08-25 00:45:04 +0000 | [diff] [blame] | 61 | #include "llvm/IR/TrackingMDRef.h" |
| 62 | #include "llvm/IR/Type.h" |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 63 | #include "llvm/IR/ValueHandle.h" |
Mehdi Amini | 8662305 | 2016-12-16 19:16:29 +0000 | [diff] [blame] | 64 | #include "llvm/IR/Verifier.h" |
Eugene Zelenko | 1804a77 | 2016-08-25 00:45:04 +0000 | [diff] [blame] | 65 | #include "llvm/Support/AtomicOrdering.h" |
| 66 | #include "llvm/Support/Casting.h" |
Teresa Johnson | 916495d | 2016-04-04 18:52:58 +0000 | [diff] [blame] | 67 | #include "llvm/Support/CommandLine.h" |
Eugene Zelenko | 1804a77 | 2016-08-25 00:45:04 +0000 | [diff] [blame] | 68 | #include "llvm/Support/Compiler.h" |
Teresa Johnson | 916495d | 2016-04-04 18:52:58 +0000 | [diff] [blame] | 69 | #include "llvm/Support/Debug.h" |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 70 | #include "llvm/Support/Error.h" |
Eugene Zelenko | 1804a77 | 2016-08-25 00:45:04 +0000 | [diff] [blame] | 71 | #include "llvm/Support/ErrorHandling.h" |
Chandler Carruth | d990388 | 2015-01-14 11:23:27 +0000 | [diff] [blame] | 72 | #include "llvm/Support/ManagedStatic.h" |
Chris Lattner | 6694f60 | 2007-04-29 07:54:31 +0000 | [diff] [blame] | 73 | #include "llvm/Support/MemoryBuffer.h" |
Tobias Grosser | 0a8e12f | 2013-07-26 04:16:55 +0000 | [diff] [blame] | 74 | #include "llvm/Support/raw_ostream.h" |
Eugene Zelenko | 1804a77 | 2016-08-25 00:45:04 +0000 | [diff] [blame] | 75 | #include <algorithm> |
| 76 | #include <cassert> |
| 77 | #include <cstddef> |
| 78 | #include <cstdint> |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 79 | #include <deque> |
Eugene Zelenko | 1804a77 | 2016-08-25 00:45:04 +0000 | [diff] [blame] | 80 | #include <limits> |
| 81 | #include <map> |
| 82 | #include <memory> |
| 83 | #include <string> |
| 84 | #include <system_error> |
| 85 | #include <tuple> |
Benjamin Kramer | 82de7d3 | 2016-05-27 14:27:24 +0000 | [diff] [blame] | 86 | #include <utility> |
Eugene Zelenko | 1804a77 | 2016-08-25 00:45:04 +0000 | [diff] [blame] | 87 | #include <vector> |
Eugene Zelenko | 6ac3f73 | 2016-01-26 18:48:36 +0000 | [diff] [blame] | 88 | |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 89 | using namespace llvm; |
| 90 | |
Teresa Johnson | 916495d | 2016-04-04 18:52:58 +0000 | [diff] [blame] | 91 | static cl::opt<bool> PrintSummaryGUIDs( |
| 92 | "print-summary-global-ids", cl::init(false), cl::Hidden, |
| 93 | cl::desc( |
| 94 | "Print the global id for each value when reading the module summary")); |
| 95 | |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 96 | namespace { |
Eugene Zelenko | 1804a77 | 2016-08-25 00:45:04 +0000 | [diff] [blame] | 97 | |
Stepan Dyatkovskiy | 0beab5e | 2012-05-12 10:48:17 +0000 | [diff] [blame] | 98 | enum { |
| 99 | SWITCH_INST_MAGIC = 0x4B5 // May 2012 => 1205 => Hex |
| 100 | }; |
| 101 | |
Peter Collingbourne | c0032b7 | 2016-11-11 19:50:10 +0000 | [diff] [blame] | 102 | Error error(const Twine &Message) { |
| 103 | return make_error<StringError>( |
| 104 | Message, make_error_code(BitcodeError::CorruptedBitcode)); |
| 105 | } |
| 106 | |
| 107 | /// Helper to read the header common to all bitcode files. |
| 108 | bool hasValidBitcodeHeader(BitstreamCursor &Stream) { |
| 109 | // Sniff for the signature. |
| 110 | if (!Stream.canSkipToPos(4) || |
| 111 | Stream.Read(8) != 'B' || |
| 112 | Stream.Read(8) != 'C' || |
| 113 | Stream.Read(4) != 0x0 || |
| 114 | Stream.Read(4) != 0xC || |
| 115 | Stream.Read(4) != 0xE || |
| 116 | Stream.Read(4) != 0xD) |
| 117 | return false; |
| 118 | return true; |
| 119 | } |
| 120 | |
| 121 | Expected<BitstreamCursor> initStream(MemoryBufferRef Buffer) { |
| 122 | const unsigned char *BufPtr = (const unsigned char *)Buffer.getBufferStart(); |
| 123 | const unsigned char *BufEnd = BufPtr + Buffer.getBufferSize(); |
| 124 | |
| 125 | if (Buffer.getBufferSize() & 3) |
| 126 | return error("Invalid bitcode signature"); |
| 127 | |
| 128 | // If we have a wrapper header, parse it and ignore the non-bc file contents. |
| 129 | // The magic number is 0x0B17C0DE stored in little endian. |
| 130 | if (isBitcodeWrapper(BufPtr, BufEnd)) |
| 131 | if (SkipBitcodeWrapperHeader(BufPtr, BufEnd, true)) |
| 132 | return error("Invalid bitcode wrapper header"); |
| 133 | |
| 134 | BitstreamCursor Stream(ArrayRef<uint8_t>(BufPtr, BufEnd)); |
| 135 | if (!hasValidBitcodeHeader(Stream)) |
| 136 | return error("Invalid bitcode signature"); |
| 137 | |
| 138 | return std::move(Stream); |
| 139 | } |
| 140 | |
Peter Collingbourne | cd513a4 | 2016-11-11 19:50:24 +0000 | [diff] [blame] | 141 | /// Convert a string from a record into an std::string, return true on failure. |
| 142 | template <typename StrTy> |
| 143 | static bool convertToString(ArrayRef<uint64_t> Record, unsigned Idx, |
| 144 | StrTy &Result) { |
| 145 | if (Idx > Record.size()) |
| 146 | return true; |
| 147 | |
| 148 | for (unsigned i = Idx, e = Record.size(); i != e; ++i) |
| 149 | Result += (char)Record[i]; |
| 150 | return false; |
| 151 | } |
| 152 | |
Mehdi Amini | 8662305 | 2016-12-16 19:16:29 +0000 | [diff] [blame] | 153 | // Strip all the TBAA attachment for the module. |
| 154 | void stripTBAA(Module *M) { |
| 155 | for (auto &F : *M) { |
| 156 | if (F.isMaterializable()) |
| 157 | continue; |
| 158 | for (auto &I : instructions(F)) |
| 159 | I.setMetadata(LLVMContext::MD_tbaa, nullptr); |
| 160 | } |
| 161 | } |
| 162 | |
Peter Collingbourne | cd513a4 | 2016-11-11 19:50:24 +0000 | [diff] [blame] | 163 | /// Read the "IDENTIFICATION_BLOCK_ID" block, do some basic enforcement on the |
| 164 | /// "epoch" encoded in the bitcode, and return the producer name if any. |
| 165 | Expected<std::string> readIdentificationBlock(BitstreamCursor &Stream) { |
| 166 | if (Stream.EnterSubBlock(bitc::IDENTIFICATION_BLOCK_ID)) |
| 167 | return error("Invalid record"); |
| 168 | |
| 169 | // Read all the records. |
| 170 | SmallVector<uint64_t, 64> Record; |
| 171 | |
| 172 | std::string ProducerIdentification; |
| 173 | |
| 174 | while (true) { |
| 175 | BitstreamEntry Entry = Stream.advance(); |
| 176 | |
| 177 | switch (Entry.Kind) { |
| 178 | default: |
| 179 | case BitstreamEntry::Error: |
| 180 | return error("Malformed block"); |
| 181 | case BitstreamEntry::EndBlock: |
| 182 | return ProducerIdentification; |
| 183 | case BitstreamEntry::Record: |
| 184 | // The interesting case. |
| 185 | break; |
| 186 | } |
| 187 | |
| 188 | // Read a record. |
| 189 | Record.clear(); |
| 190 | unsigned BitCode = Stream.readRecord(Entry.ID, Record); |
| 191 | switch (BitCode) { |
| 192 | default: // Default behavior: reject |
| 193 | return error("Invalid value"); |
| 194 | case bitc::IDENTIFICATION_CODE_STRING: // IDENTIFICATION: [strchr x N] |
| 195 | convertToString(Record, 0, ProducerIdentification); |
| 196 | break; |
| 197 | case bitc::IDENTIFICATION_CODE_EPOCH: { // EPOCH: [epoch#] |
| 198 | unsigned epoch = (unsigned)Record[0]; |
| 199 | if (epoch != bitc::BITCODE_CURRENT_EPOCH) { |
| 200 | return error( |
| 201 | Twine("Incompatible epoch: Bitcode '") + Twine(epoch) + |
| 202 | "' vs current: '" + Twine(bitc::BITCODE_CURRENT_EPOCH) + "'"); |
| 203 | } |
| 204 | } |
| 205 | } |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | Expected<std::string> readIdentificationCode(BitstreamCursor &Stream) { |
| 210 | // We expect a number of well-defined blocks, though we don't necessarily |
| 211 | // need to understand them all. |
| 212 | while (true) { |
| 213 | if (Stream.AtEndOfStream()) |
| 214 | return ""; |
| 215 | |
| 216 | BitstreamEntry Entry = Stream.advance(); |
| 217 | switch (Entry.Kind) { |
| 218 | case BitstreamEntry::EndBlock: |
| 219 | case BitstreamEntry::Error: |
| 220 | return error("Malformed block"); |
| 221 | |
| 222 | case BitstreamEntry::SubBlock: |
| 223 | if (Entry.ID == bitc::IDENTIFICATION_BLOCK_ID) |
| 224 | return readIdentificationBlock(Stream); |
| 225 | |
| 226 | // Ignore other sub-blocks. |
| 227 | if (Stream.SkipBlock()) |
| 228 | return error("Malformed block"); |
| 229 | continue; |
| 230 | case BitstreamEntry::Record: |
| 231 | Stream.skipRecord(Entry.ID); |
| 232 | continue; |
| 233 | } |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | Expected<bool> hasObjCCategoryInModule(BitstreamCursor &Stream) { |
| 238 | if (Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID)) |
| 239 | return error("Invalid record"); |
| 240 | |
| 241 | SmallVector<uint64_t, 64> Record; |
| 242 | // Read all the records for this module. |
| 243 | |
| 244 | while (true) { |
| 245 | BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); |
| 246 | |
| 247 | switch (Entry.Kind) { |
| 248 | case BitstreamEntry::SubBlock: // Handled for us already. |
| 249 | case BitstreamEntry::Error: |
| 250 | return error("Malformed block"); |
| 251 | case BitstreamEntry::EndBlock: |
| 252 | return false; |
| 253 | case BitstreamEntry::Record: |
| 254 | // The interesting case. |
| 255 | break; |
| 256 | } |
| 257 | |
| 258 | // Read a record. |
| 259 | switch (Stream.readRecord(Entry.ID, Record)) { |
| 260 | default: |
| 261 | break; // Default behavior, ignore unknown content. |
| 262 | case bitc::MODULE_CODE_SECTIONNAME: { // SECTIONNAME: [strchr x N] |
| 263 | std::string S; |
| 264 | if (convertToString(Record, 0, S)) |
| 265 | return error("Invalid record"); |
| 266 | // Check for the i386 and other (x86_64, ARM) conventions |
| 267 | if (S.find("__DATA, __objc_catlist") != std::string::npos || |
| 268 | S.find("__OBJC,__category") != std::string::npos) |
| 269 | return true; |
| 270 | break; |
| 271 | } |
| 272 | } |
| 273 | Record.clear(); |
| 274 | } |
| 275 | llvm_unreachable("Exit infinite loop"); |
| 276 | } |
| 277 | |
| 278 | Expected<bool> hasObjCCategory(BitstreamCursor &Stream) { |
| 279 | // We expect a number of well-defined blocks, though we don't necessarily |
| 280 | // need to understand them all. |
| 281 | while (true) { |
| 282 | BitstreamEntry Entry = Stream.advance(); |
| 283 | |
| 284 | switch (Entry.Kind) { |
| 285 | case BitstreamEntry::Error: |
| 286 | return error("Malformed block"); |
| 287 | case BitstreamEntry::EndBlock: |
| 288 | return false; |
| 289 | |
| 290 | case BitstreamEntry::SubBlock: |
| 291 | if (Entry.ID == bitc::MODULE_BLOCK_ID) |
| 292 | return hasObjCCategoryInModule(Stream); |
| 293 | |
| 294 | // Ignore other sub-blocks. |
| 295 | if (Stream.SkipBlock()) |
| 296 | return error("Malformed block"); |
| 297 | continue; |
| 298 | |
| 299 | case BitstreamEntry::Record: |
| 300 | Stream.skipRecord(Entry.ID); |
| 301 | continue; |
| 302 | } |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | Expected<std::string> readModuleTriple(BitstreamCursor &Stream) { |
| 307 | if (Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID)) |
| 308 | return error("Invalid record"); |
| 309 | |
| 310 | SmallVector<uint64_t, 64> Record; |
| 311 | |
| 312 | std::string Triple; |
| 313 | |
| 314 | // Read all the records for this module. |
| 315 | while (true) { |
| 316 | BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); |
| 317 | |
| 318 | switch (Entry.Kind) { |
| 319 | case BitstreamEntry::SubBlock: // Handled for us already. |
| 320 | case BitstreamEntry::Error: |
| 321 | return error("Malformed block"); |
| 322 | case BitstreamEntry::EndBlock: |
| 323 | return Triple; |
| 324 | case BitstreamEntry::Record: |
| 325 | // The interesting case. |
| 326 | break; |
| 327 | } |
| 328 | |
| 329 | // Read a record. |
| 330 | switch (Stream.readRecord(Entry.ID, Record)) { |
| 331 | default: break; // Default behavior, ignore unknown content. |
| 332 | case bitc::MODULE_CODE_TRIPLE: { // TRIPLE: [strchr x N] |
| 333 | std::string S; |
| 334 | if (convertToString(Record, 0, S)) |
| 335 | return error("Invalid record"); |
| 336 | Triple = S; |
| 337 | break; |
| 338 | } |
| 339 | } |
| 340 | Record.clear(); |
| 341 | } |
| 342 | llvm_unreachable("Exit infinite loop"); |
| 343 | } |
| 344 | |
| 345 | Expected<std::string> readTriple(BitstreamCursor &Stream) { |
| 346 | // We expect a number of well-defined blocks, though we don't necessarily |
| 347 | // need to understand them all. |
| 348 | while (true) { |
| 349 | BitstreamEntry Entry = Stream.advance(); |
| 350 | |
| 351 | switch (Entry.Kind) { |
| 352 | case BitstreamEntry::Error: |
| 353 | return error("Malformed block"); |
| 354 | case BitstreamEntry::EndBlock: |
| 355 | return ""; |
| 356 | |
| 357 | case BitstreamEntry::SubBlock: |
| 358 | if (Entry.ID == bitc::MODULE_BLOCK_ID) |
| 359 | return readModuleTriple(Stream); |
| 360 | |
| 361 | // Ignore other sub-blocks. |
| 362 | if (Stream.SkipBlock()) |
| 363 | return error("Malformed block"); |
| 364 | continue; |
| 365 | |
| 366 | case BitstreamEntry::Record: |
| 367 | Stream.skipRecord(Entry.ID); |
| 368 | continue; |
| 369 | } |
| 370 | } |
| 371 | } |
| 372 | |
Peter Collingbourne | 6e86075 | 2016-09-23 18:27:42 +0000 | [diff] [blame] | 373 | class BitcodeReaderBase { |
| 374 | protected: |
Peter Collingbourne | a0f371a | 2017-04-17 17:51:36 +0000 | [diff] [blame] | 375 | BitcodeReaderBase(BitstreamCursor Stream, StringRef Strtab) |
| 376 | : Stream(std::move(Stream)), Strtab(Strtab) { |
Peter Collingbourne | c0032b7 | 2016-11-11 19:50:10 +0000 | [diff] [blame] | 377 | this->Stream.setBlockInfo(&BlockInfo); |
| 378 | } |
Peter Collingbourne | 6e86075 | 2016-09-23 18:27:42 +0000 | [diff] [blame] | 379 | |
Peter Collingbourne | 77c89b6 | 2016-11-08 04:17:11 +0000 | [diff] [blame] | 380 | BitstreamBlockInfo BlockInfo; |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 381 | BitstreamCursor Stream; |
Peter Collingbourne | a0f371a | 2017-04-17 17:51:36 +0000 | [diff] [blame] | 382 | StringRef Strtab; |
| 383 | |
| 384 | /// In version 2 of the bitcode we store names of global values and comdats in |
| 385 | /// a string table rather than in the VST. |
| 386 | bool UseStrtab = false; |
Peter Collingbourne | 6e86075 | 2016-09-23 18:27:42 +0000 | [diff] [blame] | 387 | |
Peter Collingbourne | ce24a2a | 2017-04-12 20:02:09 +0000 | [diff] [blame] | 388 | Expected<unsigned> parseVersionRecord(ArrayRef<uint64_t> Record); |
| 389 | |
Peter Collingbourne | a0f371a | 2017-04-17 17:51:36 +0000 | [diff] [blame] | 390 | /// If this module uses a string table, pop the reference to the string table |
| 391 | /// and return the referenced string and the rest of the record. Otherwise |
| 392 | /// just return the record itself. |
| 393 | std::pair<StringRef, ArrayRef<uint64_t>> |
| 394 | readNameFromStrtab(ArrayRef<uint64_t> Record); |
| 395 | |
Peter Collingbourne | 939c7d9 | 2016-11-08 04:16:57 +0000 | [diff] [blame] | 396 | bool readBlockInfo(); |
Peter Collingbourne | 6e86075 | 2016-09-23 18:27:42 +0000 | [diff] [blame] | 397 | |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 398 | // Contains an arbitrary and optional string identifying the bitcode producer |
| 399 | std::string ProducerIdentification; |
| 400 | |
| 401 | Error error(const Twine &Message); |
Peter Collingbourne | 6e86075 | 2016-09-23 18:27:42 +0000 | [diff] [blame] | 402 | }; |
| 403 | |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 404 | Error BitcodeReaderBase::error(const Twine &Message) { |
| 405 | std::string FullMsg = Message.str(); |
| 406 | if (!ProducerIdentification.empty()) |
| 407 | FullMsg += " (Producer: '" + ProducerIdentification + "' Reader: 'LLVM " + |
| 408 | LLVM_VERSION_STRING "')"; |
Peter Collingbourne | c0032b7 | 2016-11-11 19:50:10 +0000 | [diff] [blame] | 409 | return ::error(FullMsg); |
Peter Collingbourne | 6e86075 | 2016-09-23 18:27:42 +0000 | [diff] [blame] | 410 | } |
| 411 | |
Peter Collingbourne | ce24a2a | 2017-04-12 20:02:09 +0000 | [diff] [blame] | 412 | Expected<unsigned> |
| 413 | BitcodeReaderBase::parseVersionRecord(ArrayRef<uint64_t> Record) { |
| 414 | if (Record.size() < 1) |
| 415 | return error("Invalid record"); |
| 416 | unsigned ModuleVersion = Record[0]; |
Peter Collingbourne | a0f371a | 2017-04-17 17:51:36 +0000 | [diff] [blame] | 417 | if (ModuleVersion > 2) |
Peter Collingbourne | ce24a2a | 2017-04-12 20:02:09 +0000 | [diff] [blame] | 418 | return error("Invalid value"); |
Peter Collingbourne | a0f371a | 2017-04-17 17:51:36 +0000 | [diff] [blame] | 419 | UseStrtab = ModuleVersion >= 2; |
Peter Collingbourne | ce24a2a | 2017-04-12 20:02:09 +0000 | [diff] [blame] | 420 | return ModuleVersion; |
| 421 | } |
| 422 | |
Peter Collingbourne | a0f371a | 2017-04-17 17:51:36 +0000 | [diff] [blame] | 423 | std::pair<StringRef, ArrayRef<uint64_t>> |
| 424 | BitcodeReaderBase::readNameFromStrtab(ArrayRef<uint64_t> Record) { |
| 425 | if (!UseStrtab) |
| 426 | return {"", Record}; |
| 427 | // Invalid reference. Let the caller complain about the record being empty. |
| 428 | if (Record[0] + Record[1] > Strtab.size()) |
| 429 | return {"", {}}; |
| 430 | return {StringRef(Strtab.data() + Record[0], Record[1]), Record.slice(2)}; |
| 431 | } |
| 432 | |
Peter Collingbourne | 6e86075 | 2016-09-23 18:27:42 +0000 | [diff] [blame] | 433 | class BitcodeReader : public BitcodeReaderBase, public GVMaterializer { |
| 434 | LLVMContext &Context; |
| 435 | Module *TheModule = nullptr; |
Teresa Johnson | 1493ad9 | 2015-10-10 14:18:36 +0000 | [diff] [blame] | 436 | // Next offset to start scanning for lazy parsing of function bodies. |
Rafael Espindola | 4223a1f | 2015-06-15 20:08:17 +0000 | [diff] [blame] | 437 | uint64_t NextUnreadBit = 0; |
Teresa Johnson | 1493ad9 | 2015-10-10 14:18:36 +0000 | [diff] [blame] | 438 | // Last function offset found in the VST. |
| 439 | uint64_t LastFunctionBlockBit = 0; |
Rafael Espindola | 4223a1f | 2015-06-15 20:08:17 +0000 | [diff] [blame] | 440 | bool SeenValueSymbolTable = false; |
Peter Collingbourne | 128a976 | 2015-10-27 23:01:25 +0000 | [diff] [blame] | 441 | uint64_t VSTOffset = 0; |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 442 | |
Peter Collingbourne | ce24a2a | 2017-04-12 20:02:09 +0000 | [diff] [blame] | 443 | std::vector<std::string> SectionTable; |
| 444 | std::vector<std::string> GCTable; |
| 445 | |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 446 | std::vector<Type*> TypeList; |
| 447 | BitcodeReaderValueList ValueList; |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 448 | Optional<MetadataLoader> MDLoader; |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 449 | std::vector<Comdat *> ComdatList; |
| 450 | SmallVector<Instruction *, 64> InstructionList; |
| 451 | |
| 452 | std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInits; |
Dmitry Polukhin | a3d5b0b | 2016-04-05 08:47:51 +0000 | [diff] [blame] | 453 | std::vector<std::pair<GlobalIndirectSymbol*, unsigned> > IndirectSymbolInits; |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 454 | std::vector<std::pair<Function*, unsigned> > FunctionPrefixes; |
| 455 | std::vector<std::pair<Function*, unsigned> > FunctionPrologues; |
David Majnemer | 7fddecc | 2015-06-17 20:52:32 +0000 | [diff] [blame] | 456 | std::vector<std::pair<Function*, unsigned> > FunctionPersonalityFns; |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 457 | |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 458 | /// The set of attributes by index. Index zero in the file is for null, and |
| 459 | /// is thus not represented here. As such all indices are off by one. |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 460 | std::vector<AttributeList> MAttributes; |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 461 | |
Karl Schimpf | 3644008 | 2015-08-31 16:43:55 +0000 | [diff] [blame] | 462 | /// The set of attribute groups. |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 463 | std::map<unsigned, AttributeList> MAttributeGroups; |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 464 | |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 465 | /// While parsing a function body, this is a list of the basic blocks for the |
| 466 | /// function. |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 467 | std::vector<BasicBlock*> FunctionBBs; |
| 468 | |
| 469 | // When reading the module header, this list is populated with functions that |
| 470 | // have bodies later in the file. |
| 471 | std::vector<Function*> FunctionsWithBodies; |
| 472 | |
| 473 | // When intrinsic functions are encountered which require upgrading they are |
| 474 | // stored here with their replacement function. |
Artur Pilipenko | 6c7a8ab | 2016-06-24 15:10:29 +0000 | [diff] [blame] | 475 | typedef DenseMap<Function*, Function*> UpdatedIntrinsicMap; |
| 476 | UpdatedIntrinsicMap UpgradedIntrinsics; |
| 477 | // Intrinsics which were remangled because of types rename |
| 478 | UpdatedIntrinsicMap RemangledIntrinsics; |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 479 | |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 480 | // Several operations happen after the module header has been read, but |
| 481 | // before function bodies are processed. This keeps track of whether |
| 482 | // we've done this yet. |
Rafael Espindola | 4223a1f | 2015-06-15 20:08:17 +0000 | [diff] [blame] | 483 | bool SeenFirstFunctionBody = false; |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 484 | |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 485 | /// When function bodies are initially scanned, this map contains info about |
| 486 | /// where to find deferred function body in the stream. |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 487 | DenseMap<Function*, uint64_t> DeferredFunctionInfo; |
| 488 | |
| 489 | /// When Metadata block is initially scanned when parsing the module, we may |
| 490 | /// choose to defer parsing of the metadata. This vector contains info about |
| 491 | /// which Metadata blocks are deferred. |
| 492 | std::vector<uint64_t> DeferredMetadataInfo; |
| 493 | |
| 494 | /// These are basic blocks forward-referenced by block addresses. They are |
| 495 | /// inserted lazily into functions when they're loaded. The basic block ID is |
| 496 | /// its index into the vector. |
| 497 | DenseMap<Function *, std::vector<BasicBlock *>> BasicBlockFwdRefs; |
| 498 | std::deque<Function *> BasicBlockFwdRefQueue; |
| 499 | |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 500 | /// Indicates that we are using a new encoding for instruction operands where |
| 501 | /// most operands in the current FUNCTION_BLOCK are encoded relative to the |
| 502 | /// instruction number, for a more compact encoding. Some instruction |
| 503 | /// operands are not relative to the instruction ID: basic block numbers, and |
| 504 | /// types. Once the old style function blocks have been phased out, we would |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 505 | /// not need this flag. |
Rafael Espindola | 4223a1f | 2015-06-15 20:08:17 +0000 | [diff] [blame] | 506 | bool UseRelativeIDs = false; |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 507 | |
| 508 | /// True if all functions will be materialized, negating the need to process |
| 509 | /// (e.g.) blockaddress forward references. |
Rafael Espindola | 4223a1f | 2015-06-15 20:08:17 +0000 | [diff] [blame] | 510 | bool WillMaterializeAllForwardRefs = false; |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 511 | |
Rafael Espindola | 0d68b4c | 2015-03-30 21:36:43 +0000 | [diff] [blame] | 512 | bool StripDebugInfo = false; |
Mehdi Amini | 8662305 | 2016-12-16 19:16:29 +0000 | [diff] [blame] | 513 | TBAAVerifier TBAAVerifyHelper; |
Rafael Espindola | 0d68b4c | 2015-03-30 21:36:43 +0000 | [diff] [blame] | 514 | |
Sanjoy Das | b513a9f | 2015-09-24 23:34:52 +0000 | [diff] [blame] | 515 | std::vector<std::string> BundleTags; |
| 516 | |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 517 | public: |
Peter Collingbourne | a0f371a | 2017-04-17 17:51:36 +0000 | [diff] [blame] | 518 | BitcodeReader(BitstreamCursor Stream, StringRef Strtab, |
| 519 | StringRef ProducerIdentification, LLVMContext &Context); |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 520 | |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 521 | Error materializeForwardReferencedFunctions(); |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 522 | |
Peter Collingbourne | 7f00d0a | 2016-11-09 17:49:19 +0000 | [diff] [blame] | 523 | Error materialize(GlobalValue *GV) override; |
| 524 | Error materializeModule() override; |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 525 | std::vector<StructType *> getIdentifiedStructTypes() const override; |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 526 | |
Rafael Espindola | 6ace685 | 2015-06-15 21:02:49 +0000 | [diff] [blame] | 527 | /// \brief Main interface to parsing a bitcode buffer. |
| 528 | /// \returns true if an error occurred. |
Teresa Johnson | a61f5e3 | 2016-12-16 21:25:01 +0000 | [diff] [blame] | 529 | Error parseBitcodeInto(Module *M, bool ShouldLazyLoadMetadata = false, |
| 530 | bool IsImporting = false); |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 531 | |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 532 | static uint64_t decodeSignRotatedValue(uint64_t V); |
| 533 | |
| 534 | /// Materialize any deferred Metadata block. |
Peter Collingbourne | 7f00d0a | 2016-11-09 17:49:19 +0000 | [diff] [blame] | 535 | Error materializeMetadata() override; |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 536 | |
Rafael Espindola | 0d68b4c | 2015-03-30 21:36:43 +0000 | [diff] [blame] | 537 | void setStripDebugInfo() override; |
| 538 | |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 539 | private: |
| 540 | std::vector<StructType *> IdentifiedStructTypes; |
| 541 | StructType *createIdentifiedStructType(LLVMContext &Context, StringRef Name); |
| 542 | StructType *createIdentifiedStructType(LLVMContext &Context); |
| 543 | |
| 544 | Type *getTypeByID(unsigned ID); |
Eugene Zelenko | 1804a77 | 2016-08-25 00:45:04 +0000 | [diff] [blame] | 545 | |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 546 | Value *getFnValueByID(unsigned ID, Type *Ty) { |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 547 | if (Ty && Ty->isMetadataTy()) |
| 548 | return MetadataAsValue::get(Ty->getContext(), getFnMetadataByID(ID)); |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 549 | return ValueList.getValueFwdRef(ID, Ty); |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 550 | } |
Eugene Zelenko | 1804a77 | 2016-08-25 00:45:04 +0000 | [diff] [blame] | 551 | |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 552 | Metadata *getFnMetadataByID(unsigned ID) { |
Mehdi Amini | 3bb4d01 | 2017-01-20 20:29:16 +0000 | [diff] [blame] | 553 | return MDLoader->getMetadataFwdRefOrLoad(ID); |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 554 | } |
Eugene Zelenko | 1804a77 | 2016-08-25 00:45:04 +0000 | [diff] [blame] | 555 | |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 556 | BasicBlock *getBasicBlock(unsigned ID) const { |
| 557 | if (ID >= FunctionBBs.size()) return nullptr; // Invalid ID |
| 558 | return FunctionBBs[ID]; |
| 559 | } |
Eugene Zelenko | 1804a77 | 2016-08-25 00:45:04 +0000 | [diff] [blame] | 560 | |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 561 | AttributeList getAttributes(unsigned i) const { |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 562 | if (i-1 < MAttributes.size()) |
| 563 | return MAttributes[i-1]; |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 564 | return AttributeList(); |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 565 | } |
| 566 | |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 567 | /// Read a value/type pair out of the specified record from slot 'Slot'. |
| 568 | /// Increment Slot past the number of slots used in the record. Return true on |
| 569 | /// failure. |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 570 | bool getValueTypePair(SmallVectorImpl<uint64_t> &Record, unsigned &Slot, |
| 571 | unsigned InstNum, Value *&ResVal) { |
| 572 | if (Slot == Record.size()) return true; |
| 573 | unsigned ValNo = (unsigned)Record[Slot++]; |
| 574 | // Adjust the ValNo, if it was encoded relative to the InstNum. |
| 575 | if (UseRelativeIDs) |
| 576 | ValNo = InstNum - ValNo; |
| 577 | if (ValNo < InstNum) { |
| 578 | // If this is not a forward reference, just return the value we already |
| 579 | // have. |
| 580 | ResVal = getFnValueByID(ValNo, nullptr); |
| 581 | return ResVal == nullptr; |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 582 | } |
David Blaikie | dbe6e0f | 2015-04-17 06:40:14 +0000 | [diff] [blame] | 583 | if (Slot == Record.size()) |
| 584 | return true; |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 585 | |
| 586 | unsigned TypeNo = (unsigned)Record[Slot++]; |
| 587 | ResVal = getFnValueByID(ValNo, getTypeByID(TypeNo)); |
| 588 | return ResVal == nullptr; |
| 589 | } |
| 590 | |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 591 | /// Read a value out of the specified record from slot 'Slot'. Increment Slot |
| 592 | /// past the number of slots used by the value in the record. Return true if |
| 593 | /// there is an error. |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 594 | bool popValue(SmallVectorImpl<uint64_t> &Record, unsigned &Slot, |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 595 | unsigned InstNum, Type *Ty, Value *&ResVal) { |
| 596 | if (getValue(Record, Slot, InstNum, Ty, ResVal)) |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 597 | return true; |
| 598 | // All values currently take a single record slot. |
| 599 | ++Slot; |
| 600 | return false; |
| 601 | } |
| 602 | |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 603 | /// Like popValue, but does not increment the Slot number. |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 604 | bool getValue(SmallVectorImpl<uint64_t> &Record, unsigned Slot, |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 605 | unsigned InstNum, Type *Ty, Value *&ResVal) { |
| 606 | ResVal = getValue(Record, Slot, InstNum, Ty); |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 607 | return ResVal == nullptr; |
| 608 | } |
| 609 | |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 610 | /// Version of getValue that returns ResVal directly, or 0 if there is an |
| 611 | /// error. |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 612 | Value *getValue(SmallVectorImpl<uint64_t> &Record, unsigned Slot, |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 613 | unsigned InstNum, Type *Ty) { |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 614 | if (Slot == Record.size()) return nullptr; |
| 615 | unsigned ValNo = (unsigned)Record[Slot]; |
| 616 | // Adjust the ValNo, if it was encoded relative to the InstNum. |
| 617 | if (UseRelativeIDs) |
| 618 | ValNo = InstNum - ValNo; |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 619 | return getFnValueByID(ValNo, Ty); |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 620 | } |
| 621 | |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 622 | /// Like getValue, but decodes signed VBRs. |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 623 | Value *getValueSigned(SmallVectorImpl<uint64_t> &Record, unsigned Slot, |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 624 | unsigned InstNum, Type *Ty) { |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 625 | if (Slot == Record.size()) return nullptr; |
| 626 | unsigned ValNo = (unsigned)decodeSignRotatedValue(Record[Slot]); |
| 627 | // Adjust the ValNo, if it was encoded relative to the InstNum. |
| 628 | if (UseRelativeIDs) |
| 629 | ValNo = InstNum - ValNo; |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 630 | return getFnValueByID(ValNo, Ty); |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 631 | } |
| 632 | |
| 633 | /// Converts alignment exponent (i.e. power of two (or zero)) to the |
| 634 | /// corresponding alignment to use. If alignment is too large, returns |
| 635 | /// a corresponding error code. |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 636 | Error parseAlignmentValue(uint64_t Exponent, unsigned &Alignment); |
| 637 | Error parseAttrKind(uint64_t Code, Attribute::AttrKind *Kind); |
| 638 | Error parseModule(uint64_t ResumeBit, bool ShouldLazyLoadMetadata = false); |
Peter Collingbourne | ce24a2a | 2017-04-12 20:02:09 +0000 | [diff] [blame] | 639 | |
| 640 | Error parseComdatRecord(ArrayRef<uint64_t> Record); |
| 641 | Error parseGlobalVarRecord(ArrayRef<uint64_t> Record); |
| 642 | Error parseFunctionRecord(ArrayRef<uint64_t> Record); |
| 643 | Error parseGlobalIndirectSymbolRecord(unsigned BitCode, |
| 644 | ArrayRef<uint64_t> Record); |
| 645 | |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 646 | Error parseAttributeBlock(); |
| 647 | Error parseAttributeGroupBlock(); |
| 648 | Error parseTypeTable(); |
| 649 | Error parseTypeTableBody(); |
| 650 | Error parseOperandBundleTags(); |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 651 | |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 652 | Expected<Value *> recordValue(SmallVectorImpl<uint64_t> &Record, |
| 653 | unsigned NameIndex, Triple &TT); |
Peter Collingbourne | a0f371a | 2017-04-17 17:51:36 +0000 | [diff] [blame] | 654 | void setDeferredFunctionInfo(unsigned FuncBitcodeOffsetDelta, Function *F, |
| 655 | ArrayRef<uint64_t> Record); |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 656 | Error parseValueSymbolTable(uint64_t Offset = 0); |
Peter Collingbourne | a0f371a | 2017-04-17 17:51:36 +0000 | [diff] [blame] | 657 | Error parseGlobalValueSymbolTable(); |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 658 | Error parseConstants(); |
| 659 | Error rememberAndSkipFunctionBodies(); |
| 660 | Error rememberAndSkipFunctionBody(); |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 661 | /// Save the positions of the Metadata blocks and skip parsing the blocks. |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 662 | Error rememberAndSkipMetadata(); |
| 663 | Error typeCheckLoadStoreInst(Type *ValType, Type *PtrType); |
| 664 | Error parseFunctionBody(Function *F); |
| 665 | Error globalCleanup(); |
| 666 | Error resolveGlobalAndIndirectSymbolInits(); |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 667 | Error parseUseLists(); |
| 668 | Error findFunctionInStream( |
Benjamin Kramer | cced8be | 2015-03-17 20:40:24 +0000 | [diff] [blame] | 669 | Function *F, |
| 670 | DenseMap<Function *, uint64_t>::iterator DeferredFunctionInfoIterator); |
| 671 | }; |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 672 | |
| 673 | /// Class to manage reading and parsing function summary index bitcode |
| 674 | /// files/sections. |
Peter Collingbourne | 6e86075 | 2016-09-23 18:27:42 +0000 | [diff] [blame] | 675 | class ModuleSummaryIndexBitcodeReader : public BitcodeReaderBase { |
Peter Collingbourne | a46ec9f | 2016-12-01 06:00:53 +0000 | [diff] [blame] | 676 | /// The module index built during parsing. |
Peter Collingbourne | 57f9b8c | 2016-12-01 06:21:08 +0000 | [diff] [blame] | 677 | ModuleSummaryIndex &TheIndex; |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 678 | |
Teresa Johnson | 76a1c1d | 2016-03-11 18:52:24 +0000 | [diff] [blame] | 679 | /// Indicates whether we have encountered a global value summary section |
Peter Collingbourne | a46ec9f | 2016-12-01 06:00:53 +0000 | [diff] [blame] | 680 | /// yet during parsing. |
Teresa Johnson | 76a1c1d | 2016-03-11 18:52:24 +0000 | [diff] [blame] | 681 | bool SeenGlobalValSummary = false; |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 682 | |
Teresa Johnson | 76a1c1d | 2016-03-11 18:52:24 +0000 | [diff] [blame] | 683 | /// Indicates whether we have already parsed the VST, used for error checking. |
| 684 | bool SeenValueSymbolTable = false; |
| 685 | |
| 686 | /// Set to the offset of the VST recorded in the MODULE_CODE_VSTOFFSET record. |
| 687 | /// Used to enable on-demand parsing of the VST. |
| 688 | uint64_t VSTOffset = 0; |
| 689 | |
Peter Collingbourne | 9667b91 | 2017-05-04 18:03:25 +0000 | [diff] [blame] | 690 | // Map to save ValueId to ValueInfo association that was recorded in the |
Teresa Johnson | 76a1c1d | 2016-03-11 18:52:24 +0000 | [diff] [blame] | 691 | // ValueSymbolTable. It is used after the VST is parsed to convert |
| 692 | // call graph edges read from the function summary from referencing |
Peter Collingbourne | 9667b91 | 2017-05-04 18:03:25 +0000 | [diff] [blame] | 693 | // callees by their ValueId to using the ValueInfo instead, which is how |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 694 | // they are recorded in the summary index being built. |
Peter Collingbourne | 9667b91 | 2017-05-04 18:03:25 +0000 | [diff] [blame] | 695 | // We save a GUID which refers to the same global as the ValueInfo, but |
| 696 | // ignoring the linkage, i.e. for values other than local linkage they are |
| 697 | // identical. |
| 698 | DenseMap<unsigned, std::pair<ValueInfo, GlobalValue::GUID>> |
| 699 | ValueIdToValueInfoMap; |
Teresa Johnson | 76a1c1d | 2016-03-11 18:52:24 +0000 | [diff] [blame] | 700 | |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 701 | /// Map populated during module path string table parsing, from the |
| 702 | /// module ID to a string reference owned by the index's module |
Teresa Johnson | 76a1c1d | 2016-03-11 18:52:24 +0000 | [diff] [blame] | 703 | /// path string table, used to correlate with combined index |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 704 | /// summary records. |
| 705 | DenseMap<uint64_t, StringRef> ModuleIdMap; |
| 706 | |
Teresa Johnson | e1164de | 2016-02-10 21:55:02 +0000 | [diff] [blame] | 707 | /// Original source file name recorded in a bitcode record. |
| 708 | std::string SourceFileName; |
| 709 | |
Peter Collingbourne | 74d22dd | 2017-05-01 22:04:36 +0000 | [diff] [blame] | 710 | /// The string identifier given to this module by the client, normally the |
| 711 | /// path to the bitcode file. |
| 712 | StringRef ModulePath; |
| 713 | |
| 714 | /// For per-module summary indexes, the unique numerical identifier given to |
| 715 | /// this module by the client. |
| 716 | unsigned ModuleId; |
| 717 | |
Teresa Johnson | f72278f | 2015-11-02 18:02:11 +0000 | [diff] [blame] | 718 | public: |
Peter Collingbourne | a0f371a | 2017-04-17 17:51:36 +0000 | [diff] [blame] | 719 | ModuleSummaryIndexBitcodeReader(BitstreamCursor Stream, StringRef Strtab, |
Peter Collingbourne | 74d22dd | 2017-05-01 22:04:36 +0000 | [diff] [blame] | 720 | ModuleSummaryIndex &TheIndex, |
| 721 | StringRef ModulePath, unsigned ModuleId); |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 722 | |
Peter Collingbourne | 74d22dd | 2017-05-01 22:04:36 +0000 | [diff] [blame] | 723 | Error parseModule(); |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 724 | |
Teresa Johnson | f72278f | 2015-11-02 18:02:11 +0000 | [diff] [blame] | 725 | private: |
Peter Collingbourne | a0f371a | 2017-04-17 17:51:36 +0000 | [diff] [blame] | 726 | void setValueGUID(uint64_t ValueID, StringRef ValueName, |
| 727 | GlobalValue::LinkageTypes Linkage, |
| 728 | StringRef SourceFileName); |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 729 | Error parseValueSymbolTable( |
Teresa Johnson | 76a1c1d | 2016-03-11 18:52:24 +0000 | [diff] [blame] | 730 | uint64_t Offset, |
| 731 | DenseMap<unsigned, GlobalValue::LinkageTypes> &ValueIdToLinkageMap); |
Peter Collingbourne | 0c30f08 | 2016-12-20 21:12:28 +0000 | [diff] [blame] | 732 | std::vector<ValueInfo> makeRefList(ArrayRef<uint64_t> Record); |
| 733 | std::vector<FunctionSummary::EdgeTy> makeCallList(ArrayRef<uint64_t> Record, |
| 734 | bool IsOldProfileFormat, |
| 735 | bool HasProfile); |
Peter Collingbourne | 74d22dd | 2017-05-01 22:04:36 +0000 | [diff] [blame] | 736 | Error parseEntireSummary(); |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 737 | Error parseModuleStringTable(); |
Eugene Zelenko | 1804a77 | 2016-08-25 00:45:04 +0000 | [diff] [blame] | 738 | |
Peter Collingbourne | 9667b91 | 2017-05-04 18:03:25 +0000 | [diff] [blame] | 739 | std::pair<ValueInfo, GlobalValue::GUID> |
| 740 | getValueInfoFromValueId(unsigned ValueId); |
Peter Collingbourne | 74d22dd | 2017-05-01 22:04:36 +0000 | [diff] [blame] | 741 | |
| 742 | ModulePathStringTableTy::iterator addThisModulePath(); |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 743 | }; |
Eugene Zelenko | 1804a77 | 2016-08-25 00:45:04 +0000 | [diff] [blame] | 744 | |
Peter Collingbourne | cd513a4 | 2016-11-11 19:50:24 +0000 | [diff] [blame] | 745 | } // end anonymous namespace |
| 746 | |
| 747 | std::error_code llvm::errorToErrorCodeAndEmitErrors(LLVMContext &Ctx, |
| 748 | Error Err) { |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 749 | if (Err) { |
| 750 | std::error_code EC; |
| 751 | handleAllErrors(std::move(Err), [&](ErrorInfoBase &EIB) { |
| 752 | EC = EIB.convertToErrorCode(); |
| 753 | Ctx.emitError(EIB.message()); |
| 754 | }); |
| 755 | return EC; |
| 756 | } |
| 757 | return std::error_code(); |
| 758 | } |
| 759 | |
Peter Collingbourne | a0f371a | 2017-04-17 17:51:36 +0000 | [diff] [blame] | 760 | BitcodeReader::BitcodeReader(BitstreamCursor Stream, StringRef Strtab, |
Peter Collingbourne | 7a74803 | 2016-11-16 21:44:45 +0000 | [diff] [blame] | 761 | StringRef ProducerIdentification, |
| 762 | LLVMContext &Context) |
Peter Collingbourne | a0f371a | 2017-04-17 17:51:36 +0000 | [diff] [blame] | 763 | : BitcodeReaderBase(std::move(Stream), Strtab), Context(Context), |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 764 | ValueList(Context) { |
Peter Collingbourne | 7a74803 | 2016-11-16 21:44:45 +0000 | [diff] [blame] | 765 | this->ProducerIdentification = ProducerIdentification; |
| 766 | } |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 767 | |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 768 | Error BitcodeReader::materializeForwardReferencedFunctions() { |
Duncan P. N. Exon Smith | 908d809 | 2014-08-01 21:11:34 +0000 | [diff] [blame] | 769 | if (WillMaterializeAllForwardRefs) |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 770 | return Error::success(); |
Duncan P. N. Exon Smith | 908d809 | 2014-08-01 21:11:34 +0000 | [diff] [blame] | 771 | |
| 772 | // Prevent recursion. |
| 773 | WillMaterializeAllForwardRefs = true; |
| 774 | |
Duncan P. N. Exon Smith | 5a511b5 | 2014-08-05 17:49:48 +0000 | [diff] [blame] | 775 | while (!BasicBlockFwdRefQueue.empty()) { |
| 776 | Function *F = BasicBlockFwdRefQueue.front(); |
| 777 | BasicBlockFwdRefQueue.pop_front(); |
Duncan P. N. Exon Smith | 908d809 | 2014-08-01 21:11:34 +0000 | [diff] [blame] | 778 | assert(F && "Expected valid function"); |
Duncan P. N. Exon Smith | 5a511b5 | 2014-08-05 17:49:48 +0000 | [diff] [blame] | 779 | if (!BasicBlockFwdRefs.count(F)) |
| 780 | // Already materialized. |
| 781 | continue; |
| 782 | |
Duncan P. N. Exon Smith | 908d809 | 2014-08-01 21:11:34 +0000 | [diff] [blame] | 783 | // Check for a function that isn't materializable to prevent an infinite |
| 784 | // loop. When parsing a blockaddress stored in a global variable, there |
| 785 | // isn't a trivial way to check if a function will have a body without a |
| 786 | // linear search through FunctionsWithBodies, so just check it here. |
| 787 | if (!F->isMaterializable()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 788 | return error("Never resolved function from blockaddress"); |
Duncan P. N. Exon Smith | 908d809 | 2014-08-01 21:11:34 +0000 | [diff] [blame] | 789 | |
| 790 | // Try to materialize F. |
Peter Collingbourne | 7f00d0a | 2016-11-09 17:49:19 +0000 | [diff] [blame] | 791 | if (Error Err = materialize(F)) |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 792 | return Err; |
Rafael Espindola | b799346 | 2012-01-02 07:49:53 +0000 | [diff] [blame] | 793 | } |
Duncan P. N. Exon Smith | 5a511b5 | 2014-08-05 17:49:48 +0000 | [diff] [blame] | 794 | assert(BasicBlockFwdRefs.empty() && "Function missing from queue"); |
Duncan P. N. Exon Smith | 908d809 | 2014-08-01 21:11:34 +0000 | [diff] [blame] | 795 | |
| 796 | // Reset state. |
| 797 | WillMaterializeAllForwardRefs = false; |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 798 | return Error::success(); |
Rafael Espindola | b799346 | 2012-01-02 07:49:53 +0000 | [diff] [blame] | 799 | } |
| 800 | |
Chris Lattner | fee5a37 | 2007-05-04 03:30:17 +0000 | [diff] [blame] | 801 | //===----------------------------------------------------------------------===// |
| 802 | // Helper functions to implement forward reference resolution, etc. |
| 803 | //===----------------------------------------------------------------------===// |
Chris Lattner | 6694f60 | 2007-04-29 07:54:31 +0000 | [diff] [blame] | 804 | |
Rafael Espindola | 12ca34f | 2015-01-19 15:16:06 +0000 | [diff] [blame] | 805 | static bool hasImplicitComdat(size_t Val) { |
| 806 | switch (Val) { |
| 807 | default: |
| 808 | return false; |
| 809 | case 1: // Old WeakAnyLinkage |
| 810 | case 4: // Old LinkOnceAnyLinkage |
| 811 | case 10: // Old WeakODRLinkage |
| 812 | case 11: // Old LinkOnceODRLinkage |
| 813 | return true; |
| 814 | } |
| 815 | } |
| 816 | |
Rafael Espindola | 7b4b2dc | 2015-01-08 15:36:32 +0000 | [diff] [blame] | 817 | static GlobalValue::LinkageTypes getDecodedLinkage(unsigned Val) { |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 818 | switch (Val) { |
| 819 | default: // Map unknown/new linkages to external |
Rafael Espindola | 7b4b2dc | 2015-01-08 15:36:32 +0000 | [diff] [blame] | 820 | case 0: |
| 821 | return GlobalValue::ExternalLinkage; |
Rafael Espindola | 7b4b2dc | 2015-01-08 15:36:32 +0000 | [diff] [blame] | 822 | case 2: |
| 823 | return GlobalValue::AppendingLinkage; |
| 824 | case 3: |
| 825 | return GlobalValue::InternalLinkage; |
Rafael Espindola | 7b4b2dc | 2015-01-08 15:36:32 +0000 | [diff] [blame] | 826 | case 5: |
| 827 | return GlobalValue::ExternalLinkage; // Obsolete DLLImportLinkage |
| 828 | case 6: |
| 829 | return GlobalValue::ExternalLinkage; // Obsolete DLLExportLinkage |
| 830 | case 7: |
| 831 | return GlobalValue::ExternalWeakLinkage; |
| 832 | case 8: |
| 833 | return GlobalValue::CommonLinkage; |
| 834 | case 9: |
| 835 | return GlobalValue::PrivateLinkage; |
Rafael Espindola | 7b4b2dc | 2015-01-08 15:36:32 +0000 | [diff] [blame] | 836 | case 12: |
| 837 | return GlobalValue::AvailableExternallyLinkage; |
Rafael Espindola | 2fb5bc3 | 2014-03-13 23:18:37 +0000 | [diff] [blame] | 838 | case 13: |
| 839 | return GlobalValue::PrivateLinkage; // Obsolete LinkerPrivateLinkage |
| 840 | case 14: |
| 841 | return GlobalValue::PrivateLinkage; // Obsolete LinkerPrivateWeakLinkage |
Rafael Espindola | bec6af6 | 2015-01-08 15:39:50 +0000 | [diff] [blame] | 842 | case 15: |
| 843 | return GlobalValue::ExternalLinkage; // Obsolete LinkOnceODRAutoHideLinkage |
Rafael Espindola | 12ca34f | 2015-01-19 15:16:06 +0000 | [diff] [blame] | 844 | case 1: // Old value with implicit comdat. |
| 845 | case 16: |
| 846 | return GlobalValue::WeakAnyLinkage; |
| 847 | case 10: // Old value with implicit comdat. |
| 848 | case 17: |
| 849 | return GlobalValue::WeakODRLinkage; |
| 850 | case 4: // Old value with implicit comdat. |
| 851 | case 18: |
| 852 | return GlobalValue::LinkOnceAnyLinkage; |
| 853 | case 11: // Old value with implicit comdat. |
| 854 | case 19: |
| 855 | return GlobalValue::LinkOnceODRLinkage; |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 856 | } |
| 857 | } |
| 858 | |
Piotr Padlewski | 332b3b2 | 2016-08-11 22:13:57 +0000 | [diff] [blame] | 859 | /// Decode the flags for GlobalValue in the summary. |
Mehdi Amini | c3ed48c | 2016-04-24 03:18:18 +0000 | [diff] [blame] | 860 | static GlobalValueSummary::GVFlags getDecodedGVSummaryFlags(uint64_t RawFlags, |
| 861 | uint64_t Version) { |
| 862 | // Summary were not emitted before LLVM 3.9, we don't need to upgrade Linkage |
| 863 | // like getDecodedLinkage() above. Any future change to the linkage enum and |
| 864 | // to getDecodedLinkage() will need to be taken into account here as above. |
| 865 | auto Linkage = GlobalValue::LinkageTypes(RawFlags & 0xF); // 4 bits |
Mehdi Amini | 1380edf | 2017-02-03 07:41:43 +0000 | [diff] [blame] | 866 | RawFlags = RawFlags >> 4; |
| 867 | bool NotEligibleToImport = (RawFlags & 0x1) || Version < 3; |
Evgeniy Stepanov | 56584bb | 2017-06-01 20:30:06 +0000 | [diff] [blame] | 868 | // The Live flag wasn't introduced until version 3. For dead stripping |
Teresa Johnson | 6c475a7 | 2017-01-05 21:34:18 +0000 | [diff] [blame] | 869 | // to work correctly on earlier versions, we must conservatively treat all |
| 870 | // values as live. |
Evgeniy Stepanov | 56584bb | 2017-06-01 20:30:06 +0000 | [diff] [blame] | 871 | bool Live = (RawFlags & 0x2) || Version < 3; |
| 872 | return GlobalValueSummary::GVFlags(Linkage, NotEligibleToImport, Live); |
Mehdi Amini | c3ed48c | 2016-04-24 03:18:18 +0000 | [diff] [blame] | 873 | } |
| 874 | |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 875 | static GlobalValue::VisibilityTypes getDecodedVisibility(unsigned Val) { |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 876 | switch (Val) { |
| 877 | default: // Map unknown visibilities to default. |
| 878 | case 0: return GlobalValue::DefaultVisibility; |
| 879 | case 1: return GlobalValue::HiddenVisibility; |
Anton Korobeynikov | 31fc4f9 | 2007-04-29 20:56:48 +0000 | [diff] [blame] | 880 | case 2: return GlobalValue::ProtectedVisibility; |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 881 | } |
| 882 | } |
| 883 | |
Nico Rieck | 7157bb7 | 2014-01-14 15:22:47 +0000 | [diff] [blame] | 884 | static GlobalValue::DLLStorageClassTypes |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 885 | getDecodedDLLStorageClass(unsigned Val) { |
Nico Rieck | 7157bb7 | 2014-01-14 15:22:47 +0000 | [diff] [blame] | 886 | switch (Val) { |
| 887 | default: // Map unknown values to default. |
| 888 | case 0: return GlobalValue::DefaultStorageClass; |
| 889 | case 1: return GlobalValue::DLLImportStorageClass; |
| 890 | case 2: return GlobalValue::DLLExportStorageClass; |
| 891 | } |
| 892 | } |
| 893 | |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 894 | static GlobalVariable::ThreadLocalMode getDecodedThreadLocalMode(unsigned Val) { |
Hans Wennborg | cbe34b4 | 2012-06-23 11:37:03 +0000 | [diff] [blame] | 895 | switch (Val) { |
| 896 | case 0: return GlobalVariable::NotThreadLocal; |
| 897 | default: // Map unknown non-zero value to general dynamic. |
| 898 | case 1: return GlobalVariable::GeneralDynamicTLSModel; |
| 899 | case 2: return GlobalVariable::LocalDynamicTLSModel; |
| 900 | case 3: return GlobalVariable::InitialExecTLSModel; |
| 901 | case 4: return GlobalVariable::LocalExecTLSModel; |
| 902 | } |
| 903 | } |
| 904 | |
Peter Collingbourne | 96efdd6 | 2016-06-14 21:01:22 +0000 | [diff] [blame] | 905 | static GlobalVariable::UnnamedAddr getDecodedUnnamedAddrType(unsigned Val) { |
| 906 | switch (Val) { |
| 907 | default: // Map unknown to UnnamedAddr::None. |
| 908 | case 0: return GlobalVariable::UnnamedAddr::None; |
| 909 | case 1: return GlobalVariable::UnnamedAddr::Global; |
| 910 | case 2: return GlobalVariable::UnnamedAddr::Local; |
| 911 | } |
| 912 | } |
| 913 | |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 914 | static int getDecodedCastOpcode(unsigned Val) { |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 915 | switch (Val) { |
| 916 | default: return -1; |
| 917 | case bitc::CAST_TRUNC : return Instruction::Trunc; |
| 918 | case bitc::CAST_ZEXT : return Instruction::ZExt; |
| 919 | case bitc::CAST_SEXT : return Instruction::SExt; |
| 920 | case bitc::CAST_FPTOUI : return Instruction::FPToUI; |
| 921 | case bitc::CAST_FPTOSI : return Instruction::FPToSI; |
| 922 | case bitc::CAST_UITOFP : return Instruction::UIToFP; |
| 923 | case bitc::CAST_SITOFP : return Instruction::SIToFP; |
| 924 | case bitc::CAST_FPTRUNC : return Instruction::FPTrunc; |
| 925 | case bitc::CAST_FPEXT : return Instruction::FPExt; |
| 926 | case bitc::CAST_PTRTOINT: return Instruction::PtrToInt; |
| 927 | case bitc::CAST_INTTOPTR: return Instruction::IntToPtr; |
| 928 | case bitc::CAST_BITCAST : return Instruction::BitCast; |
Matt Arsenault | 3aa9b03 | 2013-11-18 02:51:33 +0000 | [diff] [blame] | 929 | case bitc::CAST_ADDRSPACECAST: return Instruction::AddrSpaceCast; |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 930 | } |
| 931 | } |
Filipe Cabecinhas | ea79c5b | 2015-04-22 09:06:21 +0000 | [diff] [blame] | 932 | |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 933 | static int getDecodedBinaryOpcode(unsigned Val, Type *Ty) { |
Filipe Cabecinhas | ea79c5b | 2015-04-22 09:06:21 +0000 | [diff] [blame] | 934 | bool IsFP = Ty->isFPOrFPVectorTy(); |
| 935 | // BinOps are only valid for int/fp or vector of int/fp types |
| 936 | if (!IsFP && !Ty->isIntOrIntVectorTy()) |
| 937 | return -1; |
| 938 | |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 939 | switch (Val) { |
Filipe Cabecinhas | ea79c5b | 2015-04-22 09:06:21 +0000 | [diff] [blame] | 940 | default: |
| 941 | return -1; |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 942 | case bitc::BINOP_ADD: |
Filipe Cabecinhas | ea79c5b | 2015-04-22 09:06:21 +0000 | [diff] [blame] | 943 | return IsFP ? Instruction::FAdd : Instruction::Add; |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 944 | case bitc::BINOP_SUB: |
Filipe Cabecinhas | ea79c5b | 2015-04-22 09:06:21 +0000 | [diff] [blame] | 945 | return IsFP ? Instruction::FSub : Instruction::Sub; |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 946 | case bitc::BINOP_MUL: |
Filipe Cabecinhas | ea79c5b | 2015-04-22 09:06:21 +0000 | [diff] [blame] | 947 | return IsFP ? Instruction::FMul : Instruction::Mul; |
| 948 | case bitc::BINOP_UDIV: |
| 949 | return IsFP ? -1 : Instruction::UDiv; |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 950 | case bitc::BINOP_SDIV: |
Filipe Cabecinhas | ea79c5b | 2015-04-22 09:06:21 +0000 | [diff] [blame] | 951 | return IsFP ? Instruction::FDiv : Instruction::SDiv; |
| 952 | case bitc::BINOP_UREM: |
| 953 | return IsFP ? -1 : Instruction::URem; |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 954 | case bitc::BINOP_SREM: |
Filipe Cabecinhas | ea79c5b | 2015-04-22 09:06:21 +0000 | [diff] [blame] | 955 | return IsFP ? Instruction::FRem : Instruction::SRem; |
| 956 | case bitc::BINOP_SHL: |
| 957 | return IsFP ? -1 : Instruction::Shl; |
| 958 | case bitc::BINOP_LSHR: |
| 959 | return IsFP ? -1 : Instruction::LShr; |
| 960 | case bitc::BINOP_ASHR: |
| 961 | return IsFP ? -1 : Instruction::AShr; |
| 962 | case bitc::BINOP_AND: |
| 963 | return IsFP ? -1 : Instruction::And; |
| 964 | case bitc::BINOP_OR: |
| 965 | return IsFP ? -1 : Instruction::Or; |
| 966 | case bitc::BINOP_XOR: |
| 967 | return IsFP ? -1 : Instruction::Xor; |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 968 | } |
| 969 | } |
| 970 | |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 971 | static AtomicRMWInst::BinOp getDecodedRMWOperation(unsigned Val) { |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 972 | switch (Val) { |
| 973 | default: return AtomicRMWInst::BAD_BINOP; |
| 974 | case bitc::RMW_XCHG: return AtomicRMWInst::Xchg; |
| 975 | case bitc::RMW_ADD: return AtomicRMWInst::Add; |
| 976 | case bitc::RMW_SUB: return AtomicRMWInst::Sub; |
| 977 | case bitc::RMW_AND: return AtomicRMWInst::And; |
| 978 | case bitc::RMW_NAND: return AtomicRMWInst::Nand; |
| 979 | case bitc::RMW_OR: return AtomicRMWInst::Or; |
| 980 | case bitc::RMW_XOR: return AtomicRMWInst::Xor; |
| 981 | case bitc::RMW_MAX: return AtomicRMWInst::Max; |
| 982 | case bitc::RMW_MIN: return AtomicRMWInst::Min; |
| 983 | case bitc::RMW_UMAX: return AtomicRMWInst::UMax; |
| 984 | case bitc::RMW_UMIN: return AtomicRMWInst::UMin; |
| 985 | } |
| 986 | } |
| 987 | |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 988 | static AtomicOrdering getDecodedOrdering(unsigned Val) { |
Eli Friedman | fee02c6 | 2011-07-25 23:16:38 +0000 | [diff] [blame] | 989 | switch (Val) { |
JF Bastien | 800f87a | 2016-04-06 21:19:33 +0000 | [diff] [blame] | 990 | case bitc::ORDERING_NOTATOMIC: return AtomicOrdering::NotAtomic; |
| 991 | case bitc::ORDERING_UNORDERED: return AtomicOrdering::Unordered; |
| 992 | case bitc::ORDERING_MONOTONIC: return AtomicOrdering::Monotonic; |
| 993 | case bitc::ORDERING_ACQUIRE: return AtomicOrdering::Acquire; |
| 994 | case bitc::ORDERING_RELEASE: return AtomicOrdering::Release; |
| 995 | case bitc::ORDERING_ACQREL: return AtomicOrdering::AcquireRelease; |
Eli Friedman | fee02c6 | 2011-07-25 23:16:38 +0000 | [diff] [blame] | 996 | default: // Map unknown orderings to sequentially-consistent. |
JF Bastien | 800f87a | 2016-04-06 21:19:33 +0000 | [diff] [blame] | 997 | case bitc::ORDERING_SEQCST: return AtomicOrdering::SequentiallyConsistent; |
Eli Friedman | fee02c6 | 2011-07-25 23:16:38 +0000 | [diff] [blame] | 998 | } |
| 999 | } |
| 1000 | |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1001 | static SynchronizationScope getDecodedSynchScope(unsigned Val) { |
Eli Friedman | fee02c6 | 2011-07-25 23:16:38 +0000 | [diff] [blame] | 1002 | switch (Val) { |
| 1003 | case bitc::SYNCHSCOPE_SINGLETHREAD: return SingleThread; |
| 1004 | default: // Map unknown scopes to cross-thread. |
| 1005 | case bitc::SYNCHSCOPE_CROSSTHREAD: return CrossThread; |
| 1006 | } |
| 1007 | } |
| 1008 | |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 1009 | static Comdat::SelectionKind getDecodedComdatSelectionKind(unsigned Val) { |
| 1010 | switch (Val) { |
| 1011 | default: // Map unknown selection kinds to any. |
| 1012 | case bitc::COMDAT_SELECTION_KIND_ANY: |
| 1013 | return Comdat::Any; |
| 1014 | case bitc::COMDAT_SELECTION_KIND_EXACT_MATCH: |
| 1015 | return Comdat::ExactMatch; |
| 1016 | case bitc::COMDAT_SELECTION_KIND_LARGEST: |
| 1017 | return Comdat::Largest; |
| 1018 | case bitc::COMDAT_SELECTION_KIND_NO_DUPLICATES: |
| 1019 | return Comdat::NoDuplicates; |
| 1020 | case bitc::COMDAT_SELECTION_KIND_SAME_SIZE: |
| 1021 | return Comdat::SameSize; |
| 1022 | } |
| 1023 | } |
| 1024 | |
James Molloy | 88eb535 | 2015-07-10 12:52:00 +0000 | [diff] [blame] | 1025 | static FastMathFlags getDecodedFastMathFlags(unsigned Val) { |
| 1026 | FastMathFlags FMF; |
| 1027 | if (0 != (Val & FastMathFlags::UnsafeAlgebra)) |
| 1028 | FMF.setUnsafeAlgebra(); |
| 1029 | if (0 != (Val & FastMathFlags::NoNaNs)) |
| 1030 | FMF.setNoNaNs(); |
| 1031 | if (0 != (Val & FastMathFlags::NoInfs)) |
| 1032 | FMF.setNoInfs(); |
| 1033 | if (0 != (Val & FastMathFlags::NoSignedZeros)) |
| 1034 | FMF.setNoSignedZeros(); |
| 1035 | if (0 != (Val & FastMathFlags::AllowReciprocal)) |
| 1036 | FMF.setAllowReciprocal(); |
Adam Nemet | cd847a8 | 2017-03-28 20:11:52 +0000 | [diff] [blame] | 1037 | if (0 != (Val & FastMathFlags::AllowContract)) |
| 1038 | FMF.setAllowContract(true); |
James Molloy | 88eb535 | 2015-07-10 12:52:00 +0000 | [diff] [blame] | 1039 | return FMF; |
| 1040 | } |
| 1041 | |
Eugene Zelenko | 1804a77 | 2016-08-25 00:45:04 +0000 | [diff] [blame] | 1042 | static void upgradeDLLImportExportLinkage(GlobalValue *GV, unsigned Val) { |
Nico Rieck | 7157bb7 | 2014-01-14 15:22:47 +0000 | [diff] [blame] | 1043 | switch (Val) { |
| 1044 | case 5: GV->setDLLStorageClass(GlobalValue::DLLImportStorageClass); break; |
| 1045 | case 6: GV->setDLLStorageClass(GlobalValue::DLLExportStorageClass); break; |
| 1046 | } |
| 1047 | } |
| 1048 | |
Duncan P. N. Exon Smith | a59d3e5 | 2016-04-23 21:08:00 +0000 | [diff] [blame] | 1049 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1050 | Type *BitcodeReader::getTypeByID(unsigned ID) { |
| 1051 | // The type table size is always specified correctly. |
| 1052 | if (ID >= TypeList.size()) |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 1053 | return nullptr; |
Derek Schuff | 206dddd | 2012-02-06 19:03:04 +0000 | [diff] [blame] | 1054 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1055 | if (Type *Ty = TypeList[ID]) |
| 1056 | return Ty; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1057 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1058 | // If we have a forward reference, the only possible case is when it is to a |
| 1059 | // named struct. Just create a placeholder for now. |
Rafael Espindola | 2fa1e43 | 2014-12-03 07:18:23 +0000 | [diff] [blame] | 1060 | return TypeList[ID] = createIdentifiedStructType(Context); |
| 1061 | } |
| 1062 | |
| 1063 | StructType *BitcodeReader::createIdentifiedStructType(LLVMContext &Context, |
| 1064 | StringRef Name) { |
| 1065 | auto *Ret = StructType::create(Context, Name); |
| 1066 | IdentifiedStructTypes.push_back(Ret); |
| 1067 | return Ret; |
| 1068 | } |
| 1069 | |
| 1070 | StructType *BitcodeReader::createIdentifiedStructType(LLVMContext &Context) { |
| 1071 | auto *Ret = StructType::create(Context); |
| 1072 | IdentifiedStructTypes.push_back(Ret); |
| 1073 | return Ret; |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1074 | } |
| 1075 | |
Chris Lattner | fee5a37 | 2007-05-04 03:30:17 +0000 | [diff] [blame] | 1076 | //===----------------------------------------------------------------------===// |
| 1077 | // Functions for parsing blocks from the bitcode file |
| 1078 | //===----------------------------------------------------------------------===// |
| 1079 | |
Amaury Sechet | 74084c4 | 2016-11-06 07:48:46 +0000 | [diff] [blame] | 1080 | static uint64_t getRawAttributeMask(Attribute::AttrKind Val) { |
| 1081 | switch (Val) { |
| 1082 | case Attribute::EndAttrKinds: |
| 1083 | llvm_unreachable("Synthetic enumerators which should never get here"); |
| 1084 | |
| 1085 | case Attribute::None: return 0; |
| 1086 | case Attribute::ZExt: return 1 << 0; |
| 1087 | case Attribute::SExt: return 1 << 1; |
| 1088 | case Attribute::NoReturn: return 1 << 2; |
| 1089 | case Attribute::InReg: return 1 << 3; |
| 1090 | case Attribute::StructRet: return 1 << 4; |
| 1091 | case Attribute::NoUnwind: return 1 << 5; |
| 1092 | case Attribute::NoAlias: return 1 << 6; |
| 1093 | case Attribute::ByVal: return 1 << 7; |
| 1094 | case Attribute::Nest: return 1 << 8; |
| 1095 | case Attribute::ReadNone: return 1 << 9; |
| 1096 | case Attribute::ReadOnly: return 1 << 10; |
| 1097 | case Attribute::NoInline: return 1 << 11; |
| 1098 | case Attribute::AlwaysInline: return 1 << 12; |
| 1099 | case Attribute::OptimizeForSize: return 1 << 13; |
| 1100 | case Attribute::StackProtect: return 1 << 14; |
| 1101 | case Attribute::StackProtectReq: return 1 << 15; |
| 1102 | case Attribute::Alignment: return 31 << 16; |
| 1103 | case Attribute::NoCapture: return 1 << 21; |
| 1104 | case Attribute::NoRedZone: return 1 << 22; |
| 1105 | case Attribute::NoImplicitFloat: return 1 << 23; |
| 1106 | case Attribute::Naked: return 1 << 24; |
| 1107 | case Attribute::InlineHint: return 1 << 25; |
| 1108 | case Attribute::StackAlignment: return 7 << 26; |
| 1109 | case Attribute::ReturnsTwice: return 1 << 29; |
| 1110 | case Attribute::UWTable: return 1 << 30; |
| 1111 | case Attribute::NonLazyBind: return 1U << 31; |
| 1112 | case Attribute::SanitizeAddress: return 1ULL << 32; |
| 1113 | case Attribute::MinSize: return 1ULL << 33; |
| 1114 | case Attribute::NoDuplicate: return 1ULL << 34; |
| 1115 | case Attribute::StackProtectStrong: return 1ULL << 35; |
| 1116 | case Attribute::SanitizeThread: return 1ULL << 36; |
| 1117 | case Attribute::SanitizeMemory: return 1ULL << 37; |
| 1118 | case Attribute::NoBuiltin: return 1ULL << 38; |
| 1119 | case Attribute::Returned: return 1ULL << 39; |
| 1120 | case Attribute::Cold: return 1ULL << 40; |
| 1121 | case Attribute::Builtin: return 1ULL << 41; |
| 1122 | case Attribute::OptimizeNone: return 1ULL << 42; |
| 1123 | case Attribute::InAlloca: return 1ULL << 43; |
| 1124 | case Attribute::NonNull: return 1ULL << 44; |
| 1125 | case Attribute::JumpTable: return 1ULL << 45; |
| 1126 | case Attribute::Convergent: return 1ULL << 46; |
| 1127 | case Attribute::SafeStack: return 1ULL << 47; |
| 1128 | case Attribute::NoRecurse: return 1ULL << 48; |
| 1129 | case Attribute::InaccessibleMemOnly: return 1ULL << 49; |
| 1130 | case Attribute::InaccessibleMemOrArgMemOnly: return 1ULL << 50; |
| 1131 | case Attribute::SwiftSelf: return 1ULL << 51; |
| 1132 | case Attribute::SwiftError: return 1ULL << 52; |
| 1133 | case Attribute::WriteOnly: return 1ULL << 53; |
Matt Arsenault | b19b57e | 2017-04-28 20:25:27 +0000 | [diff] [blame] | 1134 | case Attribute::Speculatable: return 1ULL << 54; |
Amaury Sechet | 74084c4 | 2016-11-06 07:48:46 +0000 | [diff] [blame] | 1135 | case Attribute::Dereferenceable: |
| 1136 | llvm_unreachable("dereferenceable attribute not supported in raw format"); |
| 1137 | break; |
| 1138 | case Attribute::DereferenceableOrNull: |
| 1139 | llvm_unreachable("dereferenceable_or_null attribute not supported in raw " |
| 1140 | "format"); |
| 1141 | break; |
| 1142 | case Attribute::ArgMemOnly: |
| 1143 | llvm_unreachable("argmemonly attribute not supported in raw format"); |
| 1144 | break; |
| 1145 | case Attribute::AllocSize: |
| 1146 | llvm_unreachable("allocsize not supported in raw format"); |
| 1147 | break; |
| 1148 | } |
| 1149 | llvm_unreachable("Unsupported attribute type"); |
| 1150 | } |
| 1151 | |
| 1152 | static void addRawAttributeValue(AttrBuilder &B, uint64_t Val) { |
| 1153 | if (!Val) return; |
| 1154 | |
| 1155 | for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds; |
| 1156 | I = Attribute::AttrKind(I + 1)) { |
| 1157 | if (I == Attribute::Dereferenceable || |
| 1158 | I == Attribute::DereferenceableOrNull || |
| 1159 | I == Attribute::ArgMemOnly || |
| 1160 | I == Attribute::AllocSize) |
| 1161 | continue; |
| 1162 | if (uint64_t A = (Val & getRawAttributeMask(I))) { |
| 1163 | if (I == Attribute::Alignment) |
| 1164 | B.addAlignmentAttr(1ULL << ((A >> 16) - 1)); |
| 1165 | else if (I == Attribute::StackAlignment) |
| 1166 | B.addStackAlignmentAttr(1ULL << ((A >> 26)-1)); |
| 1167 | else |
| 1168 | B.addAttribute(I); |
| 1169 | } |
| 1170 | } |
| 1171 | } |
| 1172 | |
Bill Wendling | 56aeccc | 2013-02-04 23:32:23 +0000 | [diff] [blame] | 1173 | /// \brief This fills an AttrBuilder object with the LLVM attributes that have |
| 1174 | /// been decoded from the given integer. This function must stay in sync with |
| 1175 | /// 'encodeLLVMAttributesForBitcode'. |
| 1176 | static void decodeLLVMAttributesForBitcode(AttrBuilder &B, |
| 1177 | uint64_t EncodedAttrs) { |
| 1178 | // FIXME: Remove in 4.0. |
| 1179 | |
| 1180 | // The alignment is stored as a 16-bit raw value from bits 31--16. We shift |
| 1181 | // the bits above 31 down by 11 bits. |
| 1182 | unsigned Alignment = (EncodedAttrs & (0xffffULL << 16)) >> 16; |
| 1183 | assert((!Alignment || isPowerOf2_32(Alignment)) && |
| 1184 | "Alignment must be a power of two."); |
| 1185 | |
| 1186 | if (Alignment) |
| 1187 | B.addAlignmentAttr(Alignment); |
Amaury Sechet | 74084c4 | 2016-11-06 07:48:46 +0000 | [diff] [blame] | 1188 | addRawAttributeValue(B, ((EncodedAttrs & (0xfffffULL << 32)) >> 11) | |
| 1189 | (EncodedAttrs & 0xffff)); |
Bill Wendling | 56aeccc | 2013-02-04 23:32:23 +0000 | [diff] [blame] | 1190 | } |
| 1191 | |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 1192 | Error BitcodeReader::parseAttributeBlock() { |
Chris Lattner | 982ec1e | 2007-05-05 00:17:00 +0000 | [diff] [blame] | 1193 | if (Stream.EnterSubBlock(bitc::PARAMATTR_BLOCK_ID)) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1194 | return error("Invalid record"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1195 | |
Devang Patel | a05633e | 2008-09-26 22:53:05 +0000 | [diff] [blame] | 1196 | if (!MAttributes.empty()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1197 | return error("Invalid multiple blocks"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1198 | |
Chris Lattner | fee5a37 | 2007-05-04 03:30:17 +0000 | [diff] [blame] | 1199 | SmallVector<uint64_t, 64> Record; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1200 | |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 1201 | SmallVector<AttributeList, 8> Attrs; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1202 | |
Chris Lattner | fee5a37 | 2007-05-04 03:30:17 +0000 | [diff] [blame] | 1203 | // Read all the records. |
Eugene Zelenko | 1804a77 | 2016-08-25 00:45:04 +0000 | [diff] [blame] | 1204 | while (true) { |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 1205 | BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); |
Joe Abbey | 97b7a17 | 2013-02-06 22:14:06 +0000 | [diff] [blame] | 1206 | |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 1207 | switch (Entry.Kind) { |
| 1208 | case BitstreamEntry::SubBlock: // Handled for us already. |
| 1209 | case BitstreamEntry::Error: |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1210 | return error("Malformed block"); |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 1211 | case BitstreamEntry::EndBlock: |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 1212 | return Error::success(); |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 1213 | case BitstreamEntry::Record: |
| 1214 | // The interesting case. |
| 1215 | break; |
Chris Lattner | fee5a37 | 2007-05-04 03:30:17 +0000 | [diff] [blame] | 1216 | } |
Joe Abbey | 97b7a17 | 2013-02-06 22:14:06 +0000 | [diff] [blame] | 1217 | |
Chris Lattner | fee5a37 | 2007-05-04 03:30:17 +0000 | [diff] [blame] | 1218 | // Read a record. |
| 1219 | Record.clear(); |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 1220 | switch (Stream.readRecord(Entry.ID, Record)) { |
Chris Lattner | fee5a37 | 2007-05-04 03:30:17 +0000 | [diff] [blame] | 1221 | default: // Default behavior: ignore. |
| 1222 | break; |
Bill Wendling | 56aeccc | 2013-02-04 23:32:23 +0000 | [diff] [blame] | 1223 | case bitc::PARAMATTR_CODE_ENTRY_OLD: { // ENTRY: [paramidx0, attr0, ...] |
| 1224 | // FIXME: Remove in 4.0. |
Chris Lattner | fee5a37 | 2007-05-04 03:30:17 +0000 | [diff] [blame] | 1225 | if (Record.size() & 1) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1226 | return error("Invalid record"); |
Chris Lattner | fee5a37 | 2007-05-04 03:30:17 +0000 | [diff] [blame] | 1227 | |
Chris Lattner | fee5a37 | 2007-05-04 03:30:17 +0000 | [diff] [blame] | 1228 | for (unsigned i = 0, e = Record.size(); i != e; i += 2) { |
Bill Wendling | 60011b8 | 2013-01-29 01:43:29 +0000 | [diff] [blame] | 1229 | AttrBuilder B; |
Bill Wendling | 56aeccc | 2013-02-04 23:32:23 +0000 | [diff] [blame] | 1230 | decodeLLVMAttributesForBitcode(B, Record[i+1]); |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 1231 | Attrs.push_back(AttributeList::get(Context, Record[i], B)); |
Devang Patel | a05633e | 2008-09-26 22:53:05 +0000 | [diff] [blame] | 1232 | } |
Devang Patel | a05633e | 2008-09-26 22:53:05 +0000 | [diff] [blame] | 1233 | |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 1234 | MAttributes.push_back(AttributeList::get(Context, Attrs)); |
Chris Lattner | fee5a37 | 2007-05-04 03:30:17 +0000 | [diff] [blame] | 1235 | Attrs.clear(); |
| 1236 | break; |
| 1237 | } |
Bill Wendling | 0dc0891 | 2013-02-12 08:13:50 +0000 | [diff] [blame] | 1238 | case bitc::PARAMATTR_CODE_ENTRY: { // ENTRY: [attrgrp0, attrgrp1, ...] |
| 1239 | for (unsigned i = 0, e = Record.size(); i != e; ++i) |
| 1240 | Attrs.push_back(MAttributeGroups[Record[i]]); |
| 1241 | |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 1242 | MAttributes.push_back(AttributeList::get(Context, Attrs)); |
Bill Wendling | 0dc0891 | 2013-02-12 08:13:50 +0000 | [diff] [blame] | 1243 | Attrs.clear(); |
| 1244 | break; |
| 1245 | } |
Duncan Sands | 04eb67e | 2007-11-20 14:09:29 +0000 | [diff] [blame] | 1246 | } |
Chris Lattner | fee5a37 | 2007-05-04 03:30:17 +0000 | [diff] [blame] | 1247 | } |
| 1248 | } |
| 1249 | |
Reid Kleckner | e9f36af | 2013-11-12 01:31:00 +0000 | [diff] [blame] | 1250 | // Returns Attribute::None on unrecognized codes. |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1251 | static Attribute::AttrKind getAttrFromCode(uint64_t Code) { |
Reid Kleckner | e9f36af | 2013-11-12 01:31:00 +0000 | [diff] [blame] | 1252 | switch (Code) { |
| 1253 | default: |
| 1254 | return Attribute::None; |
| 1255 | case bitc::ATTR_KIND_ALIGNMENT: |
| 1256 | return Attribute::Alignment; |
| 1257 | case bitc::ATTR_KIND_ALWAYS_INLINE: |
| 1258 | return Attribute::AlwaysInline; |
Igor Laevsky | 39d662f | 2015-07-11 10:30:36 +0000 | [diff] [blame] | 1259 | case bitc::ATTR_KIND_ARGMEMONLY: |
| 1260 | return Attribute::ArgMemOnly; |
Reid Kleckner | e9f36af | 2013-11-12 01:31:00 +0000 | [diff] [blame] | 1261 | case bitc::ATTR_KIND_BUILTIN: |
| 1262 | return Attribute::Builtin; |
| 1263 | case bitc::ATTR_KIND_BY_VAL: |
| 1264 | return Attribute::ByVal; |
Reid Kleckner | a534a38 | 2013-12-19 02:14:12 +0000 | [diff] [blame] | 1265 | case bitc::ATTR_KIND_IN_ALLOCA: |
| 1266 | return Attribute::InAlloca; |
Reid Kleckner | e9f36af | 2013-11-12 01:31:00 +0000 | [diff] [blame] | 1267 | case bitc::ATTR_KIND_COLD: |
| 1268 | return Attribute::Cold; |
Owen Anderson | 85fa7d5 | 2015-05-26 23:48:40 +0000 | [diff] [blame] | 1269 | case bitc::ATTR_KIND_CONVERGENT: |
| 1270 | return Attribute::Convergent; |
Vaivaswatha Nagaraj | fb3f490 | 2015-12-16 16:16:19 +0000 | [diff] [blame] | 1271 | case bitc::ATTR_KIND_INACCESSIBLEMEM_ONLY: |
| 1272 | return Attribute::InaccessibleMemOnly; |
| 1273 | case bitc::ATTR_KIND_INACCESSIBLEMEM_OR_ARGMEMONLY: |
| 1274 | return Attribute::InaccessibleMemOrArgMemOnly; |
Reid Kleckner | e9f36af | 2013-11-12 01:31:00 +0000 | [diff] [blame] | 1275 | case bitc::ATTR_KIND_INLINE_HINT: |
| 1276 | return Attribute::InlineHint; |
| 1277 | case bitc::ATTR_KIND_IN_REG: |
| 1278 | return Attribute::InReg; |
Tom Roeder | 44cb65f | 2014-06-05 19:29:43 +0000 | [diff] [blame] | 1279 | case bitc::ATTR_KIND_JUMP_TABLE: |
| 1280 | return Attribute::JumpTable; |
Reid Kleckner | e9f36af | 2013-11-12 01:31:00 +0000 | [diff] [blame] | 1281 | case bitc::ATTR_KIND_MIN_SIZE: |
| 1282 | return Attribute::MinSize; |
| 1283 | case bitc::ATTR_KIND_NAKED: |
| 1284 | return Attribute::Naked; |
| 1285 | case bitc::ATTR_KIND_NEST: |
| 1286 | return Attribute::Nest; |
| 1287 | case bitc::ATTR_KIND_NO_ALIAS: |
| 1288 | return Attribute::NoAlias; |
| 1289 | case bitc::ATTR_KIND_NO_BUILTIN: |
| 1290 | return Attribute::NoBuiltin; |
| 1291 | case bitc::ATTR_KIND_NO_CAPTURE: |
| 1292 | return Attribute::NoCapture; |
| 1293 | case bitc::ATTR_KIND_NO_DUPLICATE: |
| 1294 | return Attribute::NoDuplicate; |
| 1295 | case bitc::ATTR_KIND_NO_IMPLICIT_FLOAT: |
| 1296 | return Attribute::NoImplicitFloat; |
| 1297 | case bitc::ATTR_KIND_NO_INLINE: |
| 1298 | return Attribute::NoInline; |
James Molloy | e6f87ca | 2015-11-06 10:32:53 +0000 | [diff] [blame] | 1299 | case bitc::ATTR_KIND_NO_RECURSE: |
| 1300 | return Attribute::NoRecurse; |
Reid Kleckner | e9f36af | 2013-11-12 01:31:00 +0000 | [diff] [blame] | 1301 | case bitc::ATTR_KIND_NON_LAZY_BIND: |
| 1302 | return Attribute::NonLazyBind; |
Nick Lewycky | d52b152 | 2014-05-20 01:23:40 +0000 | [diff] [blame] | 1303 | case bitc::ATTR_KIND_NON_NULL: |
| 1304 | return Attribute::NonNull; |
Hal Finkel | b0407ba | 2014-07-18 15:51:28 +0000 | [diff] [blame] | 1305 | case bitc::ATTR_KIND_DEREFERENCEABLE: |
| 1306 | return Attribute::Dereferenceable; |
Sanjoy Das | 31ea6d1 | 2015-04-16 20:29:50 +0000 | [diff] [blame] | 1307 | case bitc::ATTR_KIND_DEREFERENCEABLE_OR_NULL: |
| 1308 | return Attribute::DereferenceableOrNull; |
George Burgess IV | 278199f | 2016-04-12 01:05:35 +0000 | [diff] [blame] | 1309 | case bitc::ATTR_KIND_ALLOC_SIZE: |
| 1310 | return Attribute::AllocSize; |
Reid Kleckner | e9f36af | 2013-11-12 01:31:00 +0000 | [diff] [blame] | 1311 | case bitc::ATTR_KIND_NO_RED_ZONE: |
| 1312 | return Attribute::NoRedZone; |
| 1313 | case bitc::ATTR_KIND_NO_RETURN: |
| 1314 | return Attribute::NoReturn; |
| 1315 | case bitc::ATTR_KIND_NO_UNWIND: |
| 1316 | return Attribute::NoUnwind; |
| 1317 | case bitc::ATTR_KIND_OPTIMIZE_FOR_SIZE: |
| 1318 | return Attribute::OptimizeForSize; |
| 1319 | case bitc::ATTR_KIND_OPTIMIZE_NONE: |
| 1320 | return Attribute::OptimizeNone; |
| 1321 | case bitc::ATTR_KIND_READ_NONE: |
| 1322 | return Attribute::ReadNone; |
| 1323 | case bitc::ATTR_KIND_READ_ONLY: |
| 1324 | return Attribute::ReadOnly; |
| 1325 | case bitc::ATTR_KIND_RETURNED: |
| 1326 | return Attribute::Returned; |
| 1327 | case bitc::ATTR_KIND_RETURNS_TWICE: |
| 1328 | return Attribute::ReturnsTwice; |
| 1329 | case bitc::ATTR_KIND_S_EXT: |
| 1330 | return Attribute::SExt; |
Matt Arsenault | b19b57e | 2017-04-28 20:25:27 +0000 | [diff] [blame] | 1331 | case bitc::ATTR_KIND_SPECULATABLE: |
| 1332 | return Attribute::Speculatable; |
Reid Kleckner | e9f36af | 2013-11-12 01:31:00 +0000 | [diff] [blame] | 1333 | case bitc::ATTR_KIND_STACK_ALIGNMENT: |
| 1334 | return Attribute::StackAlignment; |
| 1335 | case bitc::ATTR_KIND_STACK_PROTECT: |
| 1336 | return Attribute::StackProtect; |
| 1337 | case bitc::ATTR_KIND_STACK_PROTECT_REQ: |
| 1338 | return Attribute::StackProtectReq; |
| 1339 | case bitc::ATTR_KIND_STACK_PROTECT_STRONG: |
| 1340 | return Attribute::StackProtectStrong; |
Peter Collingbourne | 82437bf | 2015-06-15 21:07:11 +0000 | [diff] [blame] | 1341 | case bitc::ATTR_KIND_SAFESTACK: |
| 1342 | return Attribute::SafeStack; |
Reid Kleckner | e9f36af | 2013-11-12 01:31:00 +0000 | [diff] [blame] | 1343 | case bitc::ATTR_KIND_STRUCT_RET: |
| 1344 | return Attribute::StructRet; |
| 1345 | case bitc::ATTR_KIND_SANITIZE_ADDRESS: |
| 1346 | return Attribute::SanitizeAddress; |
| 1347 | case bitc::ATTR_KIND_SANITIZE_THREAD: |
| 1348 | return Attribute::SanitizeThread; |
| 1349 | case bitc::ATTR_KIND_SANITIZE_MEMORY: |
| 1350 | return Attribute::SanitizeMemory; |
Manman Ren | 9bfd0d0 | 2016-04-01 21:41:15 +0000 | [diff] [blame] | 1351 | case bitc::ATTR_KIND_SWIFT_ERROR: |
| 1352 | return Attribute::SwiftError; |
Manman Ren | f46262e | 2016-03-29 17:37:21 +0000 | [diff] [blame] | 1353 | case bitc::ATTR_KIND_SWIFT_SELF: |
| 1354 | return Attribute::SwiftSelf; |
Reid Kleckner | e9f36af | 2013-11-12 01:31:00 +0000 | [diff] [blame] | 1355 | case bitc::ATTR_KIND_UW_TABLE: |
| 1356 | return Attribute::UWTable; |
Nicolai Haehnle | 84c9f99 | 2016-07-04 08:01:29 +0000 | [diff] [blame] | 1357 | case bitc::ATTR_KIND_WRITEONLY: |
| 1358 | return Attribute::WriteOnly; |
Reid Kleckner | e9f36af | 2013-11-12 01:31:00 +0000 | [diff] [blame] | 1359 | case bitc::ATTR_KIND_Z_EXT: |
| 1360 | return Attribute::ZExt; |
| 1361 | } |
| 1362 | } |
| 1363 | |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 1364 | Error BitcodeReader::parseAlignmentValue(uint64_t Exponent, |
| 1365 | unsigned &Alignment) { |
JF Bastien | 30bf96b | 2015-02-22 19:32:03 +0000 | [diff] [blame] | 1366 | // Note: Alignment in bitcode files is incremented by 1, so that zero |
| 1367 | // can be used for default alignment. |
| 1368 | if (Exponent > Value::MaxAlignmentExponent + 1) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1369 | return error("Invalid alignment value"); |
JF Bastien | 30bf96b | 2015-02-22 19:32:03 +0000 | [diff] [blame] | 1370 | Alignment = (1 << static_cast<unsigned>(Exponent)) >> 1; |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 1371 | return Error::success(); |
JF Bastien | 30bf96b | 2015-02-22 19:32:03 +0000 | [diff] [blame] | 1372 | } |
| 1373 | |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 1374 | Error BitcodeReader::parseAttrKind(uint64_t Code, Attribute::AttrKind *Kind) { |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1375 | *Kind = getAttrFromCode(Code); |
Reid Kleckner | e9f36af | 2013-11-12 01:31:00 +0000 | [diff] [blame] | 1376 | if (*Kind == Attribute::None) |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 1377 | return error("Unknown attribute kind (" + Twine(Code) + ")"); |
| 1378 | return Error::success(); |
Tobias Grosser | 0a8e12f | 2013-07-26 04:16:55 +0000 | [diff] [blame] | 1379 | } |
| 1380 | |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 1381 | Error BitcodeReader::parseAttributeGroupBlock() { |
Bill Wendling | ba62933 | 2013-02-10 23:24:25 +0000 | [diff] [blame] | 1382 | if (Stream.EnterSubBlock(bitc::PARAMATTR_GROUP_BLOCK_ID)) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1383 | return error("Invalid record"); |
Bill Wendling | ba62933 | 2013-02-10 23:24:25 +0000 | [diff] [blame] | 1384 | |
| 1385 | if (!MAttributeGroups.empty()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1386 | return error("Invalid multiple blocks"); |
Bill Wendling | ba62933 | 2013-02-10 23:24:25 +0000 | [diff] [blame] | 1387 | |
| 1388 | SmallVector<uint64_t, 64> Record; |
| 1389 | |
| 1390 | // Read all the records. |
Eugene Zelenko | 1804a77 | 2016-08-25 00:45:04 +0000 | [diff] [blame] | 1391 | while (true) { |
Bill Wendling | ba62933 | 2013-02-10 23:24:25 +0000 | [diff] [blame] | 1392 | BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); |
| 1393 | |
| 1394 | switch (Entry.Kind) { |
| 1395 | case BitstreamEntry::SubBlock: // Handled for us already. |
| 1396 | case BitstreamEntry::Error: |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1397 | return error("Malformed block"); |
Bill Wendling | ba62933 | 2013-02-10 23:24:25 +0000 | [diff] [blame] | 1398 | case BitstreamEntry::EndBlock: |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 1399 | return Error::success(); |
Bill Wendling | ba62933 | 2013-02-10 23:24:25 +0000 | [diff] [blame] | 1400 | case BitstreamEntry::Record: |
| 1401 | // The interesting case. |
| 1402 | break; |
| 1403 | } |
| 1404 | |
| 1405 | // Read a record. |
| 1406 | Record.clear(); |
| 1407 | switch (Stream.readRecord(Entry.ID, Record)) { |
| 1408 | default: // Default behavior: ignore. |
| 1409 | break; |
| 1410 | case bitc::PARAMATTR_GRP_CODE_ENTRY: { // ENTRY: [grpid, idx, a0, a1, ...] |
| 1411 | if (Record.size() < 3) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1412 | return error("Invalid record"); |
Bill Wendling | ba62933 | 2013-02-10 23:24:25 +0000 | [diff] [blame] | 1413 | |
Bill Wendling | e46707e | 2013-02-11 22:32:29 +0000 | [diff] [blame] | 1414 | uint64_t GrpID = Record[0]; |
Bill Wendling | ba62933 | 2013-02-10 23:24:25 +0000 | [diff] [blame] | 1415 | uint64_t Idx = Record[1]; // Index of the object this attribute refers to. |
| 1416 | |
| 1417 | AttrBuilder B; |
| 1418 | for (unsigned i = 2, e = Record.size(); i != e; ++i) { |
| 1419 | if (Record[i] == 0) { // Enum attribute |
Tobias Grosser | 0a8e12f | 2013-07-26 04:16:55 +0000 | [diff] [blame] | 1420 | Attribute::AttrKind Kind; |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 1421 | if (Error Err = parseAttrKind(Record[++i], &Kind)) |
| 1422 | return Err; |
Tobias Grosser | 0a8e12f | 2013-07-26 04:16:55 +0000 | [diff] [blame] | 1423 | |
| 1424 | B.addAttribute(Kind); |
Hal Finkel | e15442c | 2014-07-18 06:51:55 +0000 | [diff] [blame] | 1425 | } else if (Record[i] == 1) { // Integer attribute |
Tobias Grosser | 0a8e12f | 2013-07-26 04:16:55 +0000 | [diff] [blame] | 1426 | Attribute::AttrKind Kind; |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 1427 | if (Error Err = parseAttrKind(Record[++i], &Kind)) |
| 1428 | return Err; |
Tobias Grosser | 0a8e12f | 2013-07-26 04:16:55 +0000 | [diff] [blame] | 1429 | if (Kind == Attribute::Alignment) |
Bill Wendling | ba62933 | 2013-02-10 23:24:25 +0000 | [diff] [blame] | 1430 | B.addAlignmentAttr(Record[++i]); |
Hal Finkel | b0407ba | 2014-07-18 15:51:28 +0000 | [diff] [blame] | 1431 | else if (Kind == Attribute::StackAlignment) |
Bill Wendling | ba62933 | 2013-02-10 23:24:25 +0000 | [diff] [blame] | 1432 | B.addStackAlignmentAttr(Record[++i]); |
Hal Finkel | b0407ba | 2014-07-18 15:51:28 +0000 | [diff] [blame] | 1433 | else if (Kind == Attribute::Dereferenceable) |
| 1434 | B.addDereferenceableAttr(Record[++i]); |
Sanjoy Das | 31ea6d1 | 2015-04-16 20:29:50 +0000 | [diff] [blame] | 1435 | else if (Kind == Attribute::DereferenceableOrNull) |
| 1436 | B.addDereferenceableOrNullAttr(Record[++i]); |
George Burgess IV | 278199f | 2016-04-12 01:05:35 +0000 | [diff] [blame] | 1437 | else if (Kind == Attribute::AllocSize) |
| 1438 | B.addAllocSizeAttrFromRawRepr(Record[++i]); |
Bill Wendling | ba62933 | 2013-02-10 23:24:25 +0000 | [diff] [blame] | 1439 | } else { // String attribute |
Bill Wendling | e46707e | 2013-02-11 22:32:29 +0000 | [diff] [blame] | 1440 | assert((Record[i] == 3 || Record[i] == 4) && |
| 1441 | "Invalid attribute group entry"); |
Bill Wendling | ba62933 | 2013-02-10 23:24:25 +0000 | [diff] [blame] | 1442 | bool HasValue = (Record[i++] == 4); |
| 1443 | SmallString<64> KindStr; |
| 1444 | SmallString<64> ValStr; |
| 1445 | |
| 1446 | while (Record[i] != 0 && i != e) |
| 1447 | KindStr += Record[i++]; |
Bill Wendling | e46707e | 2013-02-11 22:32:29 +0000 | [diff] [blame] | 1448 | assert(Record[i] == 0 && "Kind string not null terminated"); |
Bill Wendling | ba62933 | 2013-02-10 23:24:25 +0000 | [diff] [blame] | 1449 | |
| 1450 | if (HasValue) { |
| 1451 | // Has a value associated with it. |
Bill Wendling | e46707e | 2013-02-11 22:32:29 +0000 | [diff] [blame] | 1452 | ++i; // Skip the '0' that terminates the "kind" string. |
Bill Wendling | ba62933 | 2013-02-10 23:24:25 +0000 | [diff] [blame] | 1453 | while (Record[i] != 0 && i != e) |
| 1454 | ValStr += Record[i++]; |
Bill Wendling | e46707e | 2013-02-11 22:32:29 +0000 | [diff] [blame] | 1455 | assert(Record[i] == 0 && "Value string not null terminated"); |
Bill Wendling | ba62933 | 2013-02-10 23:24:25 +0000 | [diff] [blame] | 1456 | } |
| 1457 | |
| 1458 | B.addAttribute(KindStr.str(), ValStr.str()); |
| 1459 | } |
| 1460 | } |
| 1461 | |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 1462 | MAttributeGroups[GrpID] = AttributeList::get(Context, Idx, B); |
Bill Wendling | ba62933 | 2013-02-10 23:24:25 +0000 | [diff] [blame] | 1463 | break; |
| 1464 | } |
| 1465 | } |
| 1466 | } |
| 1467 | } |
| 1468 | |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 1469 | Error BitcodeReader::parseTypeTable() { |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1470 | if (Stream.EnterSubBlock(bitc::TYPE_BLOCK_ID_NEW)) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1471 | return error("Invalid record"); |
Derek Schuff | 206dddd | 2012-02-06 19:03:04 +0000 | [diff] [blame] | 1472 | |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1473 | return parseTypeTableBody(); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1474 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1475 | |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 1476 | Error BitcodeReader::parseTypeTableBody() { |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1477 | if (!TypeList.empty()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1478 | return error("Invalid multiple blocks"); |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1479 | |
| 1480 | SmallVector<uint64_t, 64> Record; |
| 1481 | unsigned NumRecords = 0; |
| 1482 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1483 | SmallString<64> TypeName; |
Derek Schuff | 206dddd | 2012-02-06 19:03:04 +0000 | [diff] [blame] | 1484 | |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1485 | // Read all the records for this type table. |
Eugene Zelenko | 1804a77 | 2016-08-25 00:45:04 +0000 | [diff] [blame] | 1486 | while (true) { |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 1487 | BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); |
Joe Abbey | 97b7a17 | 2013-02-06 22:14:06 +0000 | [diff] [blame] | 1488 | |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 1489 | switch (Entry.Kind) { |
| 1490 | case BitstreamEntry::SubBlock: // Handled for us already. |
| 1491 | case BitstreamEntry::Error: |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1492 | return error("Malformed block"); |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 1493 | case BitstreamEntry::EndBlock: |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1494 | if (NumRecords != TypeList.size()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1495 | return error("Malformed block"); |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 1496 | return Error::success(); |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 1497 | case BitstreamEntry::Record: |
| 1498 | // The interesting case. |
| 1499 | break; |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1500 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1501 | |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1502 | // Read a record. |
| 1503 | Record.clear(); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 1504 | Type *ResultTy = nullptr; |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 1505 | switch (Stream.readRecord(Entry.ID, Record)) { |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 1506 | default: |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1507 | return error("Invalid value"); |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1508 | case bitc::TYPE_CODE_NUMENTRY: // TYPE_CODE_NUMENTRY: [numentries] |
| 1509 | // TYPE_CODE_NUMENTRY contains a count of the number of types in the |
| 1510 | // type list. This allows us to reserve space. |
| 1511 | if (Record.size() < 1) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1512 | return error("Invalid record"); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1513 | TypeList.resize(Record[0]); |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1514 | continue; |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1515 | case bitc::TYPE_CODE_VOID: // VOID |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1516 | ResultTy = Type::getVoidTy(Context); |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1517 | break; |
Dan Gohman | 518cda4 | 2011-12-17 00:04:22 +0000 | [diff] [blame] | 1518 | case bitc::TYPE_CODE_HALF: // HALF |
| 1519 | ResultTy = Type::getHalfTy(Context); |
| 1520 | break; |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1521 | case bitc::TYPE_CODE_FLOAT: // FLOAT |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1522 | ResultTy = Type::getFloatTy(Context); |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1523 | break; |
| 1524 | case bitc::TYPE_CODE_DOUBLE: // DOUBLE |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1525 | ResultTy = Type::getDoubleTy(Context); |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1526 | break; |
Dale Johannesen | ff4c3be | 2007-08-03 01:03:46 +0000 | [diff] [blame] | 1527 | case bitc::TYPE_CODE_X86_FP80: // X86_FP80 |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1528 | ResultTy = Type::getX86_FP80Ty(Context); |
Dale Johannesen | ff4c3be | 2007-08-03 01:03:46 +0000 | [diff] [blame] | 1529 | break; |
| 1530 | case bitc::TYPE_CODE_FP128: // FP128 |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1531 | ResultTy = Type::getFP128Ty(Context); |
Dale Johannesen | ff4c3be | 2007-08-03 01:03:46 +0000 | [diff] [blame] | 1532 | break; |
| 1533 | case bitc::TYPE_CODE_PPC_FP128: // PPC_FP128 |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1534 | ResultTy = Type::getPPC_FP128Ty(Context); |
Dale Johannesen | ff4c3be | 2007-08-03 01:03:46 +0000 | [diff] [blame] | 1535 | break; |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1536 | case bitc::TYPE_CODE_LABEL: // LABEL |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1537 | ResultTy = Type::getLabelTy(Context); |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1538 | break; |
Nick Lewycky | adbc284 | 2009-05-30 05:06:04 +0000 | [diff] [blame] | 1539 | case bitc::TYPE_CODE_METADATA: // METADATA |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1540 | ResultTy = Type::getMetadataTy(Context); |
Nick Lewycky | adbc284 | 2009-05-30 05:06:04 +0000 | [diff] [blame] | 1541 | break; |
Dale Johannesen | baa5d04 | 2010-09-10 20:55:01 +0000 | [diff] [blame] | 1542 | case bitc::TYPE_CODE_X86_MMX: // X86_MMX |
| 1543 | ResultTy = Type::getX86_MMXTy(Context); |
| 1544 | break; |
David Majnemer | b611e3f | 2015-08-14 05:09:07 +0000 | [diff] [blame] | 1545 | case bitc::TYPE_CODE_TOKEN: // TOKEN |
| 1546 | ResultTy = Type::getTokenTy(Context); |
| 1547 | break; |
Filipe Cabecinhas | fcd044b | 2015-01-30 18:13:50 +0000 | [diff] [blame] | 1548 | case bitc::TYPE_CODE_INTEGER: { // INTEGER: [width] |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1549 | if (Record.size() < 1) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1550 | return error("Invalid record"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1551 | |
Filipe Cabecinhas | fcd044b | 2015-01-30 18:13:50 +0000 | [diff] [blame] | 1552 | uint64_t NumBits = Record[0]; |
| 1553 | if (NumBits < IntegerType::MIN_INT_BITS || |
| 1554 | NumBits > IntegerType::MAX_INT_BITS) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1555 | return error("Bitwidth for integer type out of range"); |
Filipe Cabecinhas | fcd044b | 2015-01-30 18:13:50 +0000 | [diff] [blame] | 1556 | ResultTy = IntegerType::get(Context, NumBits); |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1557 | break; |
Filipe Cabecinhas | fcd044b | 2015-01-30 18:13:50 +0000 | [diff] [blame] | 1558 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1559 | case bitc::TYPE_CODE_POINTER: { // POINTER: [pointee type] or |
Christopher Lamb | 54dd24c | 2007-12-11 08:59:05 +0000 | [diff] [blame] | 1560 | // [pointee type, address space] |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1561 | if (Record.size() < 1) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1562 | return error("Invalid record"); |
Christopher Lamb | 54dd24c | 2007-12-11 08:59:05 +0000 | [diff] [blame] | 1563 | unsigned AddressSpace = 0; |
| 1564 | if (Record.size() == 2) |
| 1565 | AddressSpace = Record[1]; |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1566 | ResultTy = getTypeByID(Record[0]); |
Filipe Cabecinhas | d8a1bcd | 2015-04-29 02:27:28 +0000 | [diff] [blame] | 1567 | if (!ResultTy || |
| 1568 | !PointerType::isValidElementType(ResultTy)) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1569 | return error("Invalid type"); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1570 | ResultTy = PointerType::get(ResultTy, AddressSpace); |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1571 | break; |
Christopher Lamb | 54dd24c | 2007-12-11 08:59:05 +0000 | [diff] [blame] | 1572 | } |
Nuno Lopes | 561dae0 | 2012-05-23 15:19:39 +0000 | [diff] [blame] | 1573 | case bitc::TYPE_CODE_FUNCTION_OLD: { |
| 1574 | // FIXME: attrid is dead, remove it in LLVM 4.0 |
| 1575 | // FUNCTION: [vararg, attrid, retty, paramty x N] |
| 1576 | if (Record.size() < 3) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1577 | return error("Invalid record"); |
Nuno Lopes | 561dae0 | 2012-05-23 15:19:39 +0000 | [diff] [blame] | 1578 | SmallVector<Type*, 8> ArgTys; |
| 1579 | for (unsigned i = 3, e = Record.size(); i != e; ++i) { |
| 1580 | if (Type *T = getTypeByID(Record[i])) |
| 1581 | ArgTys.push_back(T); |
| 1582 | else |
| 1583 | break; |
| 1584 | } |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 1585 | |
Nuno Lopes | 561dae0 | 2012-05-23 15:19:39 +0000 | [diff] [blame] | 1586 | ResultTy = getTypeByID(Record[2]); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 1587 | if (!ResultTy || ArgTys.size() < Record.size()-3) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1588 | return error("Invalid type"); |
Nuno Lopes | 561dae0 | 2012-05-23 15:19:39 +0000 | [diff] [blame] | 1589 | |
| 1590 | ResultTy = FunctionType::get(ResultTy, ArgTys, Record[0]); |
| 1591 | break; |
| 1592 | } |
Chad Rosier | 9589872 | 2011-11-03 00:14:01 +0000 | [diff] [blame] | 1593 | case bitc::TYPE_CODE_FUNCTION: { |
| 1594 | // FUNCTION: [vararg, retty, paramty x N] |
| 1595 | if (Record.size() < 2) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1596 | return error("Invalid record"); |
Chris Lattner | cc3aaf1 | 2012-01-27 03:15:49 +0000 | [diff] [blame] | 1597 | SmallVector<Type*, 8> ArgTys; |
Chad Rosier | 9589872 | 2011-11-03 00:14:01 +0000 | [diff] [blame] | 1598 | for (unsigned i = 2, e = Record.size(); i != e; ++i) { |
Filipe Cabecinhas | 32af542 | 2015-05-19 01:21:06 +0000 | [diff] [blame] | 1599 | if (Type *T = getTypeByID(Record[i])) { |
| 1600 | if (!FunctionType::isValidArgumentType(T)) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1601 | return error("Invalid function argument type"); |
Chad Rosier | 9589872 | 2011-11-03 00:14:01 +0000 | [diff] [blame] | 1602 | ArgTys.push_back(T); |
Filipe Cabecinhas | 32af542 | 2015-05-19 01:21:06 +0000 | [diff] [blame] | 1603 | } |
Chad Rosier | 9589872 | 2011-11-03 00:14:01 +0000 | [diff] [blame] | 1604 | else |
| 1605 | break; |
| 1606 | } |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 1607 | |
Chad Rosier | 9589872 | 2011-11-03 00:14:01 +0000 | [diff] [blame] | 1608 | ResultTy = getTypeByID(Record[1]); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 1609 | if (!ResultTy || ArgTys.size() < Record.size()-2) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1610 | return error("Invalid type"); |
Chad Rosier | 9589872 | 2011-11-03 00:14:01 +0000 | [diff] [blame] | 1611 | |
| 1612 | ResultTy = FunctionType::get(ResultTy, ArgTys, Record[0]); |
| 1613 | break; |
| 1614 | } |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1615 | case bitc::TYPE_CODE_STRUCT_ANON: { // STRUCT: [ispacked, eltty x N] |
Chris Lattner | 3c5616e | 2007-05-06 08:21:50 +0000 | [diff] [blame] | 1616 | if (Record.size() < 1) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1617 | return error("Invalid record"); |
Chris Lattner | cc3aaf1 | 2012-01-27 03:15:49 +0000 | [diff] [blame] | 1618 | SmallVector<Type*, 8> EltTys; |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1619 | for (unsigned i = 1, e = Record.size(); i != e; ++i) { |
| 1620 | if (Type *T = getTypeByID(Record[i])) |
| 1621 | EltTys.push_back(T); |
| 1622 | else |
| 1623 | break; |
| 1624 | } |
| 1625 | if (EltTys.size() != Record.size()-1) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1626 | return error("Invalid type"); |
Owen Anderson | 03cb69f | 2009-08-05 23:16:16 +0000 | [diff] [blame] | 1627 | ResultTy = StructType::get(Context, EltTys, Record[0]); |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1628 | break; |
| 1629 | } |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1630 | case bitc::TYPE_CODE_STRUCT_NAME: // STRUCT_NAME: [strchr x N] |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1631 | if (convertToString(Record, 0, TypeName)) |
| 1632 | return error("Invalid record"); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1633 | continue; |
| 1634 | |
| 1635 | case bitc::TYPE_CODE_STRUCT_NAMED: { // STRUCT: [ispacked, eltty x N] |
| 1636 | if (Record.size() < 1) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1637 | return error("Invalid record"); |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 1638 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1639 | if (NumRecords >= TypeList.size()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1640 | return error("Invalid TYPE table"); |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 1641 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1642 | // Check to see if this was forward referenced, if so fill in the temp. |
| 1643 | StructType *Res = cast_or_null<StructType>(TypeList[NumRecords]); |
| 1644 | if (Res) { |
| 1645 | Res->setName(TypeName); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 1646 | TypeList[NumRecords] = nullptr; |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1647 | } else // Otherwise, create a new struct. |
Rafael Espindola | 2fa1e43 | 2014-12-03 07:18:23 +0000 | [diff] [blame] | 1648 | Res = createIdentifiedStructType(Context, TypeName); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1649 | TypeName.clear(); |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 1650 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1651 | SmallVector<Type*, 8> EltTys; |
| 1652 | for (unsigned i = 1, e = Record.size(); i != e; ++i) { |
| 1653 | if (Type *T = getTypeByID(Record[i])) |
| 1654 | EltTys.push_back(T); |
| 1655 | else |
| 1656 | break; |
| 1657 | } |
| 1658 | if (EltTys.size() != Record.size()-1) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1659 | return error("Invalid record"); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1660 | Res->setBody(EltTys, Record[0]); |
| 1661 | ResultTy = Res; |
| 1662 | break; |
| 1663 | } |
| 1664 | case bitc::TYPE_CODE_OPAQUE: { // OPAQUE: [] |
| 1665 | if (Record.size() != 1) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1666 | return error("Invalid record"); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1667 | |
| 1668 | if (NumRecords >= TypeList.size()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1669 | return error("Invalid TYPE table"); |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 1670 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1671 | // Check to see if this was forward referenced, if so fill in the temp. |
| 1672 | StructType *Res = cast_or_null<StructType>(TypeList[NumRecords]); |
| 1673 | if (Res) { |
| 1674 | Res->setName(TypeName); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 1675 | TypeList[NumRecords] = nullptr; |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1676 | } else // Otherwise, create a new struct with no body. |
Rafael Espindola | 2fa1e43 | 2014-12-03 07:18:23 +0000 | [diff] [blame] | 1677 | Res = createIdentifiedStructType(Context, TypeName); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1678 | TypeName.clear(); |
| 1679 | ResultTy = Res; |
| 1680 | break; |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 1681 | } |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1682 | case bitc::TYPE_CODE_ARRAY: // ARRAY: [numelts, eltty] |
| 1683 | if (Record.size() < 2) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1684 | return error("Invalid record"); |
Filipe Cabecinhas | 6fe8aab | 2015-04-29 02:36:08 +0000 | [diff] [blame] | 1685 | ResultTy = getTypeByID(Record[1]); |
| 1686 | if (!ResultTy || !ArrayType::isValidElementType(ResultTy)) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1687 | return error("Invalid type"); |
Filipe Cabecinhas | 6fe8aab | 2015-04-29 02:36:08 +0000 | [diff] [blame] | 1688 | ResultTy = ArrayType::get(ResultTy, Record[0]); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1689 | break; |
| 1690 | case bitc::TYPE_CODE_VECTOR: // VECTOR: [numelts, eltty] |
| 1691 | if (Record.size() < 2) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1692 | return error("Invalid record"); |
Filipe Cabecinhas | 8e42190 | 2015-06-03 00:05:30 +0000 | [diff] [blame] | 1693 | if (Record[0] == 0) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1694 | return error("Invalid vector length"); |
Filipe Cabecinhas | 6fe8aab | 2015-04-29 02:36:08 +0000 | [diff] [blame] | 1695 | ResultTy = getTypeByID(Record[1]); |
| 1696 | if (!ResultTy || !StructType::isValidElementType(ResultTy)) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1697 | return error("Invalid type"); |
Filipe Cabecinhas | 6fe8aab | 2015-04-29 02:36:08 +0000 | [diff] [blame] | 1698 | ResultTy = VectorType::get(ResultTy, Record[0]); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1699 | break; |
| 1700 | } |
| 1701 | |
| 1702 | if (NumRecords >= TypeList.size()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1703 | return error("Invalid TYPE table"); |
Filipe Cabecinhas | d0858e1 | 2015-01-30 10:57:58 +0000 | [diff] [blame] | 1704 | if (TypeList[NumRecords]) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1705 | return error( |
Filipe Cabecinhas | d0858e1 | 2015-01-30 10:57:58 +0000 | [diff] [blame] | 1706 | "Invalid TYPE table: Only named structs can be forward referenced"); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1707 | assert(ResultTy && "Didn't read a type?"); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1708 | TypeList[NumRecords++] = ResultTy; |
| 1709 | } |
| 1710 | } |
| 1711 | |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 1712 | Error BitcodeReader::parseOperandBundleTags() { |
Sanjoy Das | b513a9f | 2015-09-24 23:34:52 +0000 | [diff] [blame] | 1713 | if (Stream.EnterSubBlock(bitc::OPERAND_BUNDLE_TAGS_BLOCK_ID)) |
| 1714 | return error("Invalid record"); |
| 1715 | |
| 1716 | if (!BundleTags.empty()) |
| 1717 | return error("Invalid multiple blocks"); |
| 1718 | |
| 1719 | SmallVector<uint64_t, 64> Record; |
| 1720 | |
Eugene Zelenko | 1804a77 | 2016-08-25 00:45:04 +0000 | [diff] [blame] | 1721 | while (true) { |
Sanjoy Das | b513a9f | 2015-09-24 23:34:52 +0000 | [diff] [blame] | 1722 | BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); |
| 1723 | |
| 1724 | switch (Entry.Kind) { |
| 1725 | case BitstreamEntry::SubBlock: // Handled for us already. |
| 1726 | case BitstreamEntry::Error: |
| 1727 | return error("Malformed block"); |
| 1728 | case BitstreamEntry::EndBlock: |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 1729 | return Error::success(); |
Sanjoy Das | b513a9f | 2015-09-24 23:34:52 +0000 | [diff] [blame] | 1730 | case BitstreamEntry::Record: |
| 1731 | // The interesting case. |
| 1732 | break; |
| 1733 | } |
| 1734 | |
| 1735 | // Tags are implicitly mapped to integers by their order. |
| 1736 | |
| 1737 | if (Stream.readRecord(Entry.ID, Record) != bitc::OPERAND_BUNDLE_TAG) |
| 1738 | return error("Invalid record"); |
| 1739 | |
| 1740 | // OPERAND_BUNDLE_TAG: [strchr x N] |
| 1741 | BundleTags.emplace_back(); |
| 1742 | if (convertToString(Record, 0, BundleTags.back())) |
| 1743 | return error("Invalid record"); |
| 1744 | Record.clear(); |
| 1745 | } |
| 1746 | } |
| 1747 | |
Teresa Johnson | ff642b9 | 2015-09-17 20:12:00 +0000 | [diff] [blame] | 1748 | /// Associate a value with its name from the given index in the provided record. |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 1749 | Expected<Value *> BitcodeReader::recordValue(SmallVectorImpl<uint64_t> &Record, |
| 1750 | unsigned NameIndex, Triple &TT) { |
Teresa Johnson | ff642b9 | 2015-09-17 20:12:00 +0000 | [diff] [blame] | 1751 | SmallString<128> ValueName; |
| 1752 | if (convertToString(Record, NameIndex, ValueName)) |
| 1753 | return error("Invalid record"); |
| 1754 | unsigned ValueID = Record[0]; |
| 1755 | if (ValueID >= ValueList.size() || !ValueList[ValueID]) |
| 1756 | return error("Invalid record"); |
| 1757 | Value *V = ValueList[ValueID]; |
| 1758 | |
Filipe Cabecinhas | a2b0ac4 | 2015-11-04 14:53:36 +0000 | [diff] [blame] | 1759 | StringRef NameStr(ValueName.data(), ValueName.size()); |
| 1760 | if (NameStr.find_first_of(0) != StringRef::npos) |
| 1761 | return error("Invalid value name"); |
| 1762 | V->setName(NameStr); |
Teresa Johnson | ff642b9 | 2015-09-17 20:12:00 +0000 | [diff] [blame] | 1763 | auto *GO = dyn_cast<GlobalObject>(V); |
| 1764 | if (GO) { |
| 1765 | if (GO->getComdat() == reinterpret_cast<Comdat *>(1)) { |
| 1766 | if (TT.isOSBinFormatMachO()) |
| 1767 | GO->setComdat(nullptr); |
| 1768 | else |
| 1769 | GO->setComdat(TheModule->getOrInsertComdat(V->getName())); |
| 1770 | } |
| 1771 | } |
| 1772 | return V; |
| 1773 | } |
| 1774 | |
Teresa Johnson | 76a1c1d | 2016-03-11 18:52:24 +0000 | [diff] [blame] | 1775 | /// Helper to note and return the current location, and jump to the given |
| 1776 | /// offset. |
| 1777 | static uint64_t jumpToValueSymbolTable(uint64_t Offset, |
| 1778 | BitstreamCursor &Stream) { |
| 1779 | // Save the current parsing location so we can jump back at the end |
| 1780 | // of the VST read. |
| 1781 | uint64_t CurrentBit = Stream.GetCurrentBitNo(); |
| 1782 | Stream.JumpToBit(Offset * 32); |
| 1783 | #ifndef NDEBUG |
| 1784 | // Do some checking if we are in debug mode. |
| 1785 | BitstreamEntry Entry = Stream.advance(); |
| 1786 | assert(Entry.Kind == BitstreamEntry::SubBlock); |
| 1787 | assert(Entry.ID == bitc::VALUE_SYMTAB_BLOCK_ID); |
| 1788 | #else |
| 1789 | // In NDEBUG mode ignore the output so we don't get an unused variable |
| 1790 | // warning. |
| 1791 | Stream.advance(); |
| 1792 | #endif |
| 1793 | return CurrentBit; |
| 1794 | } |
| 1795 | |
Peter Collingbourne | a0f371a | 2017-04-17 17:51:36 +0000 | [diff] [blame] | 1796 | void BitcodeReader::setDeferredFunctionInfo(unsigned FuncBitcodeOffsetDelta, |
| 1797 | Function *F, |
| 1798 | ArrayRef<uint64_t> Record) { |
| 1799 | // Note that we subtract 1 here because the offset is relative to one word |
| 1800 | // before the start of the identification or module block, which was |
| 1801 | // historically always the start of the regular bitcode header. |
| 1802 | uint64_t FuncWordOffset = Record[1] - 1; |
| 1803 | uint64_t FuncBitOffset = FuncWordOffset * 32; |
| 1804 | DeferredFunctionInfo[F] = FuncBitOffset + FuncBitcodeOffsetDelta; |
| 1805 | // Set the LastFunctionBlockBit to point to the last function block. |
| 1806 | // Later when parsing is resumed after function materialization, |
| 1807 | // we can simply skip that last function block. |
| 1808 | if (FuncBitOffset > LastFunctionBlockBit) |
| 1809 | LastFunctionBlockBit = FuncBitOffset; |
| 1810 | } |
| 1811 | |
| 1812 | /// Read a new-style GlobalValue symbol table. |
| 1813 | Error BitcodeReader::parseGlobalValueSymbolTable() { |
| 1814 | unsigned FuncBitcodeOffsetDelta = |
| 1815 | Stream.getAbbrevIDWidth() + bitc::BlockIDWidth; |
| 1816 | |
| 1817 | if (Stream.EnterSubBlock(bitc::VALUE_SYMTAB_BLOCK_ID)) |
| 1818 | return error("Invalid record"); |
| 1819 | |
| 1820 | SmallVector<uint64_t, 64> Record; |
| 1821 | while (true) { |
| 1822 | BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); |
| 1823 | |
| 1824 | switch (Entry.Kind) { |
| 1825 | case BitstreamEntry::SubBlock: |
| 1826 | case BitstreamEntry::Error: |
| 1827 | return error("Malformed block"); |
| 1828 | case BitstreamEntry::EndBlock: |
| 1829 | return Error::success(); |
| 1830 | case BitstreamEntry::Record: |
| 1831 | break; |
| 1832 | } |
| 1833 | |
| 1834 | Record.clear(); |
| 1835 | switch (Stream.readRecord(Entry.ID, Record)) { |
| 1836 | case bitc::VST_CODE_FNENTRY: // [valueid, offset] |
| 1837 | setDeferredFunctionInfo(FuncBitcodeOffsetDelta, |
| 1838 | cast<Function>(ValueList[Record[0]]), Record); |
| 1839 | break; |
| 1840 | } |
| 1841 | } |
| 1842 | } |
| 1843 | |
Teresa Johnson | ff642b9 | 2015-09-17 20:12:00 +0000 | [diff] [blame] | 1844 | /// Parse the value symbol table at either the current parsing location or |
| 1845 | /// at the given bit offset if provided. |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 1846 | Error BitcodeReader::parseValueSymbolTable(uint64_t Offset) { |
Teresa Johnson | ff642b9 | 2015-09-17 20:12:00 +0000 | [diff] [blame] | 1847 | uint64_t CurrentBit; |
| 1848 | // Pass in the Offset to distinguish between calling for the module-level |
| 1849 | // VST (where we want to jump to the VST offset) and the function-level |
| 1850 | // VST (where we don't). |
Peter Collingbourne | a0f371a | 2017-04-17 17:51:36 +0000 | [diff] [blame] | 1851 | if (Offset > 0) { |
Teresa Johnson | 76a1c1d | 2016-03-11 18:52:24 +0000 | [diff] [blame] | 1852 | CurrentBit = jumpToValueSymbolTable(Offset, Stream); |
Peter Collingbourne | a0f371a | 2017-04-17 17:51:36 +0000 | [diff] [blame] | 1853 | // If this module uses a string table, read this as a module-level VST. |
| 1854 | if (UseStrtab) { |
| 1855 | if (Error Err = parseGlobalValueSymbolTable()) |
| 1856 | return Err; |
| 1857 | Stream.JumpToBit(CurrentBit); |
| 1858 | return Error::success(); |
| 1859 | } |
| 1860 | // Otherwise, the VST will be in a similar format to a function-level VST, |
| 1861 | // and will contain symbol names. |
| 1862 | } |
Teresa Johnson | ff642b9 | 2015-09-17 20:12:00 +0000 | [diff] [blame] | 1863 | |
| 1864 | // Compute the delta between the bitcode indices in the VST (the word offset |
| 1865 | // to the word-aligned ENTER_SUBBLOCK for the function block, and that |
| 1866 | // expected by the lazy reader. The reader's EnterSubBlock expects to have |
| 1867 | // already read the ENTER_SUBBLOCK code (size getAbbrevIDWidth) and BlockID |
| 1868 | // (size BlockIDWidth). Note that we access the stream's AbbrevID width here |
| 1869 | // just before entering the VST subblock because: 1) the EnterSubBlock |
| 1870 | // changes the AbbrevID width; 2) the VST block is nested within the same |
| 1871 | // outer MODULE_BLOCK as the FUNCTION_BLOCKs and therefore have the same |
| 1872 | // AbbrevID width before calling EnterSubBlock; and 3) when we want to |
| 1873 | // jump to the FUNCTION_BLOCK using this offset later, we don't want |
| 1874 | // to rely on the stream's AbbrevID width being that of the MODULE_BLOCK. |
| 1875 | unsigned FuncBitcodeOffsetDelta = |
| 1876 | Stream.getAbbrevIDWidth() + bitc::BlockIDWidth; |
| 1877 | |
Chris Lattner | 982ec1e | 2007-05-05 00:17:00 +0000 | [diff] [blame] | 1878 | if (Stream.EnterSubBlock(bitc::VALUE_SYMTAB_BLOCK_ID)) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1879 | return error("Invalid record"); |
Chris Lattner | ccaa448 | 2007-04-23 21:26:05 +0000 | [diff] [blame] | 1880 | |
| 1881 | SmallVector<uint64_t, 64> Record; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1882 | |
David Majnemer | 3087b22 | 2015-01-20 05:58:07 +0000 | [diff] [blame] | 1883 | Triple TT(TheModule->getTargetTriple()); |
| 1884 | |
Chris Lattner | ccaa448 | 2007-04-23 21:26:05 +0000 | [diff] [blame] | 1885 | // Read all the records for this value table. |
| 1886 | SmallString<128> ValueName; |
Eugene Zelenko | 1804a77 | 2016-08-25 00:45:04 +0000 | [diff] [blame] | 1887 | |
| 1888 | while (true) { |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 1889 | BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); |
Joe Abbey | 97b7a17 | 2013-02-06 22:14:06 +0000 | [diff] [blame] | 1890 | |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 1891 | switch (Entry.Kind) { |
| 1892 | case BitstreamEntry::SubBlock: // Handled for us already. |
| 1893 | case BitstreamEntry::Error: |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1894 | return error("Malformed block"); |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 1895 | case BitstreamEntry::EndBlock: |
Teresa Johnson | ff642b9 | 2015-09-17 20:12:00 +0000 | [diff] [blame] | 1896 | if (Offset > 0) |
| 1897 | Stream.JumpToBit(CurrentBit); |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 1898 | return Error::success(); |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 1899 | case BitstreamEntry::Record: |
| 1900 | // The interesting case. |
| 1901 | break; |
Chris Lattner | ccaa448 | 2007-04-23 21:26:05 +0000 | [diff] [blame] | 1902 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1903 | |
Chris Lattner | ccaa448 | 2007-04-23 21:26:05 +0000 | [diff] [blame] | 1904 | // Read a record. |
| 1905 | Record.clear(); |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 1906 | switch (Stream.readRecord(Entry.ID, Record)) { |
Chris Lattner | ccaa448 | 2007-04-23 21:26:05 +0000 | [diff] [blame] | 1907 | default: // Default behavior: unknown type. |
| 1908 | break; |
Teresa Johnson | 79d4e2f | 2016-02-10 15:02:51 +0000 | [diff] [blame] | 1909 | case bitc::VST_CODE_ENTRY: { // VST_CODE_ENTRY: [valueid, namechar x N] |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 1910 | Expected<Value *> ValOrErr = recordValue(Record, 1, TT); |
| 1911 | if (Error Err = ValOrErr.takeError()) |
| 1912 | return Err; |
Teresa Johnson | ff642b9 | 2015-09-17 20:12:00 +0000 | [diff] [blame] | 1913 | ValOrErr.get(); |
| 1914 | break; |
| 1915 | } |
| 1916 | case bitc::VST_CODE_FNENTRY: { |
Teresa Johnson | 79d4e2f | 2016-02-10 15:02:51 +0000 | [diff] [blame] | 1917 | // VST_CODE_FNENTRY: [valueid, offset, namechar x N] |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 1918 | Expected<Value *> ValOrErr = recordValue(Record, 2, TT); |
| 1919 | if (Error Err = ValOrErr.takeError()) |
| 1920 | return Err; |
Teresa Johnson | ff642b9 | 2015-09-17 20:12:00 +0000 | [diff] [blame] | 1921 | Value *V = ValOrErr.get(); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1922 | |
Peter Collingbourne | db4cafa | 2017-04-06 19:39:24 +0000 | [diff] [blame] | 1923 | // Ignore function offsets emitted for aliases of functions in older |
| 1924 | // versions of LLVM. |
Peter Collingbourne | a0f371a | 2017-04-17 17:51:36 +0000 | [diff] [blame] | 1925 | if (auto *F = dyn_cast<Function>(V)) |
| 1926 | setDeferredFunctionInfo(FuncBitcodeOffsetDelta, F, Record); |
Chris Lattner | ccaa448 | 2007-04-23 21:26:05 +0000 | [diff] [blame] | 1927 | break; |
Reid Spencer | dea02bd | 2007-05-04 01:43:33 +0000 | [diff] [blame] | 1928 | } |
Bill Wendling | 35a9c3c | 2011-04-10 23:18:04 +0000 | [diff] [blame] | 1929 | case bitc::VST_CODE_BBENTRY: { |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1930 | if (convertToString(Record, 1, ValueName)) |
| 1931 | return error("Invalid record"); |
Chris Lattner | 6be58c6 | 2007-05-03 22:18:21 +0000 | [diff] [blame] | 1932 | BasicBlock *BB = getBasicBlock(Record[0]); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 1933 | if (!BB) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1934 | return error("Invalid record"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1935 | |
Daniel Dunbar | d786b51 | 2009-07-26 00:34:27 +0000 | [diff] [blame] | 1936 | BB->setName(StringRef(ValueName.data(), ValueName.size())); |
Chris Lattner | 6be58c6 | 2007-05-03 22:18:21 +0000 | [diff] [blame] | 1937 | ValueName.clear(); |
| 1938 | break; |
Chris Lattner | ccaa448 | 2007-04-23 21:26:05 +0000 | [diff] [blame] | 1939 | } |
Reid Spencer | dea02bd | 2007-05-04 01:43:33 +0000 | [diff] [blame] | 1940 | } |
Chris Lattner | ccaa448 | 2007-04-23 21:26:05 +0000 | [diff] [blame] | 1941 | } |
| 1942 | } |
| 1943 | |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1944 | /// Decode a signed value stored with the sign bit in the LSB for dense VBR |
| 1945 | /// encoding. |
Jan Wen Voung | afaced0 | 2012-10-11 20:20:40 +0000 | [diff] [blame] | 1946 | uint64_t BitcodeReader::decodeSignRotatedValue(uint64_t V) { |
Chris Lattner | 08feb1e | 2007-04-24 04:04:35 +0000 | [diff] [blame] | 1947 | if ((V & 1) == 0) |
| 1948 | return V >> 1; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1949 | if (V != 1) |
Chris Lattner | 08feb1e | 2007-04-24 04:04:35 +0000 | [diff] [blame] | 1950 | return -(V >> 1); |
| 1951 | // There is no such thing as -0 with integers. "-0" really means MININT. |
| 1952 | return 1ULL << 63; |
| 1953 | } |
| 1954 | |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1955 | /// Resolve all of the initializers for global values and aliases that we can. |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 1956 | Error BitcodeReader::resolveGlobalAndIndirectSymbolInits() { |
Chris Lattner | 44c1707 | 2007-04-26 02:46:40 +0000 | [diff] [blame] | 1957 | std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInitWorklist; |
Dmitry Polukhin | a3d5b0b | 2016-04-05 08:47:51 +0000 | [diff] [blame] | 1958 | std::vector<std::pair<GlobalIndirectSymbol*, unsigned> > |
| 1959 | IndirectSymbolInitWorklist; |
Peter Collingbourne | 3fa50f9 | 2013-09-16 01:08:15 +0000 | [diff] [blame] | 1960 | std::vector<std::pair<Function*, unsigned> > FunctionPrefixWorklist; |
Peter Collingbourne | 51d2de7 | 2014-12-03 02:08:38 +0000 | [diff] [blame] | 1961 | std::vector<std::pair<Function*, unsigned> > FunctionPrologueWorklist; |
David Majnemer | 7fddecc | 2015-06-17 20:52:32 +0000 | [diff] [blame] | 1962 | std::vector<std::pair<Function*, unsigned> > FunctionPersonalityFnWorklist; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1963 | |
Chris Lattner | 44c1707 | 2007-04-26 02:46:40 +0000 | [diff] [blame] | 1964 | GlobalInitWorklist.swap(GlobalInits); |
Dmitry Polukhin | a3d5b0b | 2016-04-05 08:47:51 +0000 | [diff] [blame] | 1965 | IndirectSymbolInitWorklist.swap(IndirectSymbolInits); |
Peter Collingbourne | 3fa50f9 | 2013-09-16 01:08:15 +0000 | [diff] [blame] | 1966 | FunctionPrefixWorklist.swap(FunctionPrefixes); |
Peter Collingbourne | 51d2de7 | 2014-12-03 02:08:38 +0000 | [diff] [blame] | 1967 | FunctionPrologueWorklist.swap(FunctionPrologues); |
David Majnemer | 7fddecc | 2015-06-17 20:52:32 +0000 | [diff] [blame] | 1968 | FunctionPersonalityFnWorklist.swap(FunctionPersonalityFns); |
Chris Lattner | 44c1707 | 2007-04-26 02:46:40 +0000 | [diff] [blame] | 1969 | |
| 1970 | while (!GlobalInitWorklist.empty()) { |
Chris Lattner | 831d420 | 2007-04-26 03:27:58 +0000 | [diff] [blame] | 1971 | unsigned ValID = GlobalInitWorklist.back().second; |
Chris Lattner | 44c1707 | 2007-04-26 02:46:40 +0000 | [diff] [blame] | 1972 | if (ValID >= ValueList.size()) { |
| 1973 | // Not ready to resolve this yet, it requires something later in the file. |
Chris Lattner | 831d420 | 2007-04-26 03:27:58 +0000 | [diff] [blame] | 1974 | GlobalInits.push_back(GlobalInitWorklist.back()); |
Chris Lattner | 44c1707 | 2007-04-26 02:46:40 +0000 | [diff] [blame] | 1975 | } else { |
Karthik Bhat | 82540e9 | 2014-03-27 12:08:23 +0000 | [diff] [blame] | 1976 | if (Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID])) |
Chris Lattner | 44c1707 | 2007-04-26 02:46:40 +0000 | [diff] [blame] | 1977 | GlobalInitWorklist.back().first->setInitializer(C); |
| 1978 | else |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1979 | return error("Expected a constant"); |
Chris Lattner | 44c1707 | 2007-04-26 02:46:40 +0000 | [diff] [blame] | 1980 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1981 | GlobalInitWorklist.pop_back(); |
Chris Lattner | 44c1707 | 2007-04-26 02:46:40 +0000 | [diff] [blame] | 1982 | } |
| 1983 | |
Dmitry Polukhin | a3d5b0b | 2016-04-05 08:47:51 +0000 | [diff] [blame] | 1984 | while (!IndirectSymbolInitWorklist.empty()) { |
| 1985 | unsigned ValID = IndirectSymbolInitWorklist.back().second; |
Chris Lattner | 44c1707 | 2007-04-26 02:46:40 +0000 | [diff] [blame] | 1986 | if (ValID >= ValueList.size()) { |
Dmitry Polukhin | a3d5b0b | 2016-04-05 08:47:51 +0000 | [diff] [blame] | 1987 | IndirectSymbolInits.push_back(IndirectSymbolInitWorklist.back()); |
Chris Lattner | 44c1707 | 2007-04-26 02:46:40 +0000 | [diff] [blame] | 1988 | } else { |
Filipe Cabecinhas | a911af0 | 2015-06-06 20:44:53 +0000 | [diff] [blame] | 1989 | Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID]); |
| 1990 | if (!C) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1991 | return error("Expected a constant"); |
Dmitry Polukhin | a3d5b0b | 2016-04-05 08:47:51 +0000 | [diff] [blame] | 1992 | GlobalIndirectSymbol *GIS = IndirectSymbolInitWorklist.back().first; |
| 1993 | if (isa<GlobalAlias>(GIS) && C->getType() != GIS->getType()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 1994 | return error("Alias and aliasee types don't match"); |
Dmitry Polukhin | a3d5b0b | 2016-04-05 08:47:51 +0000 | [diff] [blame] | 1995 | GIS->setIndirectSymbol(C); |
Chris Lattner | 44c1707 | 2007-04-26 02:46:40 +0000 | [diff] [blame] | 1996 | } |
Dmitry Polukhin | a3d5b0b | 2016-04-05 08:47:51 +0000 | [diff] [blame] | 1997 | IndirectSymbolInitWorklist.pop_back(); |
Chris Lattner | 44c1707 | 2007-04-26 02:46:40 +0000 | [diff] [blame] | 1998 | } |
Peter Collingbourne | 3fa50f9 | 2013-09-16 01:08:15 +0000 | [diff] [blame] | 1999 | |
| 2000 | while (!FunctionPrefixWorklist.empty()) { |
| 2001 | unsigned ValID = FunctionPrefixWorklist.back().second; |
| 2002 | if (ValID >= ValueList.size()) { |
| 2003 | FunctionPrefixes.push_back(FunctionPrefixWorklist.back()); |
| 2004 | } else { |
Karthik Bhat | 82540e9 | 2014-03-27 12:08:23 +0000 | [diff] [blame] | 2005 | if (Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID])) |
Peter Collingbourne | 3fa50f9 | 2013-09-16 01:08:15 +0000 | [diff] [blame] | 2006 | FunctionPrefixWorklist.back().first->setPrefixData(C); |
| 2007 | else |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2008 | return error("Expected a constant"); |
Peter Collingbourne | 3fa50f9 | 2013-09-16 01:08:15 +0000 | [diff] [blame] | 2009 | } |
| 2010 | FunctionPrefixWorklist.pop_back(); |
| 2011 | } |
| 2012 | |
Peter Collingbourne | 51d2de7 | 2014-12-03 02:08:38 +0000 | [diff] [blame] | 2013 | while (!FunctionPrologueWorklist.empty()) { |
| 2014 | unsigned ValID = FunctionPrologueWorklist.back().second; |
| 2015 | if (ValID >= ValueList.size()) { |
| 2016 | FunctionPrologues.push_back(FunctionPrologueWorklist.back()); |
| 2017 | } else { |
| 2018 | if (Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID])) |
| 2019 | FunctionPrologueWorklist.back().first->setPrologueData(C); |
| 2020 | else |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2021 | return error("Expected a constant"); |
Peter Collingbourne | 51d2de7 | 2014-12-03 02:08:38 +0000 | [diff] [blame] | 2022 | } |
| 2023 | FunctionPrologueWorklist.pop_back(); |
| 2024 | } |
| 2025 | |
David Majnemer | 7fddecc | 2015-06-17 20:52:32 +0000 | [diff] [blame] | 2026 | while (!FunctionPersonalityFnWorklist.empty()) { |
| 2027 | unsigned ValID = FunctionPersonalityFnWorklist.back().second; |
| 2028 | if (ValID >= ValueList.size()) { |
| 2029 | FunctionPersonalityFns.push_back(FunctionPersonalityFnWorklist.back()); |
| 2030 | } else { |
| 2031 | if (Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID])) |
| 2032 | FunctionPersonalityFnWorklist.back().first->setPersonalityFn(C); |
| 2033 | else |
| 2034 | return error("Expected a constant"); |
| 2035 | } |
| 2036 | FunctionPersonalityFnWorklist.pop_back(); |
| 2037 | } |
| 2038 | |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 2039 | return Error::success(); |
Chris Lattner | 44c1707 | 2007-04-26 02:46:40 +0000 | [diff] [blame] | 2040 | } |
| 2041 | |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2042 | static APInt readWideAPInt(ArrayRef<uint64_t> Vals, unsigned TypeBits) { |
Benjamin Kramer | 9704ed0 | 2012-05-28 14:10:31 +0000 | [diff] [blame] | 2043 | SmallVector<uint64_t, 8> Words(Vals.size()); |
David Majnemer | 2d006e7 | 2016-08-12 04:32:42 +0000 | [diff] [blame] | 2044 | transform(Vals, Words.begin(), |
Jan Wen Voung | afaced0 | 2012-10-11 20:20:40 +0000 | [diff] [blame] | 2045 | BitcodeReader::decodeSignRotatedValue); |
Benjamin Kramer | 9704ed0 | 2012-05-28 14:10:31 +0000 | [diff] [blame] | 2046 | |
Stepan Dyatkovskiy | 0beab5e | 2012-05-12 10:48:17 +0000 | [diff] [blame] | 2047 | return APInt(TypeBits, Words); |
| 2048 | } |
| 2049 | |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 2050 | Error BitcodeReader::parseConstants() { |
Chris Lattner | 982ec1e | 2007-05-05 00:17:00 +0000 | [diff] [blame] | 2051 | if (Stream.EnterSubBlock(bitc::CONSTANTS_BLOCK_ID)) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2052 | return error("Invalid record"); |
Chris Lattner | fbc1d33 | 2007-04-24 03:30:34 +0000 | [diff] [blame] | 2053 | |
| 2054 | SmallVector<uint64_t, 64> Record; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2055 | |
Chris Lattner | fbc1d33 | 2007-04-24 03:30:34 +0000 | [diff] [blame] | 2056 | // Read all the records for this value table. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2057 | Type *CurTy = Type::getInt32Ty(Context); |
Chris Lattner | 1663cca | 2007-04-24 05:48:56 +0000 | [diff] [blame] | 2058 | unsigned NextCstNo = ValueList.size(); |
Eugene Zelenko | 1804a77 | 2016-08-25 00:45:04 +0000 | [diff] [blame] | 2059 | |
| 2060 | while (true) { |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 2061 | BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); |
Joe Abbey | 97b7a17 | 2013-02-06 22:14:06 +0000 | [diff] [blame] | 2062 | |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 2063 | switch (Entry.Kind) { |
| 2064 | case BitstreamEntry::SubBlock: // Handled for us already. |
| 2065 | case BitstreamEntry::Error: |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2066 | return error("Malformed block"); |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 2067 | case BitstreamEntry::EndBlock: |
| 2068 | if (NextCstNo != ValueList.size()) |
George Burgess IV | 1030d68 | 2016-01-20 22:15:23 +0000 | [diff] [blame] | 2069 | return error("Invalid constant reference"); |
Joe Abbey | 97b7a17 | 2013-02-06 22:14:06 +0000 | [diff] [blame] | 2070 | |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 2071 | // Once all the constants have been read, go through and resolve forward |
| 2072 | // references. |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2073 | ValueList.resolveConstantForwardRefs(); |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 2074 | return Error::success(); |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 2075 | case BitstreamEntry::Record: |
| 2076 | // The interesting case. |
Chris Lattner | 7442993 | 2008-08-21 02:34:16 +0000 | [diff] [blame] | 2077 | break; |
Chris Lattner | fbc1d33 | 2007-04-24 03:30:34 +0000 | [diff] [blame] | 2078 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2079 | |
Chris Lattner | fbc1d33 | 2007-04-24 03:30:34 +0000 | [diff] [blame] | 2080 | // Read a record. |
| 2081 | Record.clear(); |
Filipe Cabecinhas | 2849b48 | 2016-06-05 18:43:17 +0000 | [diff] [blame] | 2082 | Type *VoidType = Type::getVoidTy(Context); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 2083 | Value *V = nullptr; |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 2084 | unsigned BitCode = Stream.readRecord(Entry.ID, Record); |
Dan Gohman | 0ebd696 | 2009-07-20 21:19:07 +0000 | [diff] [blame] | 2085 | switch (BitCode) { |
Chris Lattner | fbc1d33 | 2007-04-24 03:30:34 +0000 | [diff] [blame] | 2086 | default: // Default behavior: unknown constant |
| 2087 | case bitc::CST_CODE_UNDEF: // UNDEF |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 2088 | V = UndefValue::get(CurTy); |
Chris Lattner | fbc1d33 | 2007-04-24 03:30:34 +0000 | [diff] [blame] | 2089 | break; |
| 2090 | case bitc::CST_CODE_SETTYPE: // SETTYPE: [typeid] |
| 2091 | if (Record.empty()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2092 | return error("Invalid record"); |
Karthik Bhat | 82540e9 | 2014-03-27 12:08:23 +0000 | [diff] [blame] | 2093 | if (Record[0] >= TypeList.size() || !TypeList[Record[0]]) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2094 | return error("Invalid record"); |
Filipe Cabecinhas | 2849b48 | 2016-06-05 18:43:17 +0000 | [diff] [blame] | 2095 | if (TypeList[Record[0]] == VoidType) |
| 2096 | return error("Invalid constant type"); |
Chris Lattner | fbc1d33 | 2007-04-24 03:30:34 +0000 | [diff] [blame] | 2097 | CurTy = TypeList[Record[0]]; |
Chris Lattner | 08feb1e | 2007-04-24 04:04:35 +0000 | [diff] [blame] | 2098 | continue; // Skip the ValueList manipulation. |
Chris Lattner | fbc1d33 | 2007-04-24 03:30:34 +0000 | [diff] [blame] | 2099 | case bitc::CST_CODE_NULL: // NULL |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 2100 | V = Constant::getNullValue(CurTy); |
Chris Lattner | fbc1d33 | 2007-04-24 03:30:34 +0000 | [diff] [blame] | 2101 | break; |
| 2102 | case bitc::CST_CODE_INTEGER: // INTEGER: [intval] |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2103 | if (!CurTy->isIntegerTy() || Record.empty()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2104 | return error("Invalid record"); |
Jan Wen Voung | afaced0 | 2012-10-11 20:20:40 +0000 | [diff] [blame] | 2105 | V = ConstantInt::get(CurTy, decodeSignRotatedValue(Record[0])); |
Chris Lattner | 08feb1e | 2007-04-24 04:04:35 +0000 | [diff] [blame] | 2106 | break; |
Chris Lattner | e14cb88 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 2107 | case bitc::CST_CODE_WIDE_INTEGER: {// WIDE_INTEGER: [n x intval] |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2108 | if (!CurTy->isIntegerTy() || Record.empty()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2109 | return error("Invalid record"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2110 | |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2111 | APInt VInt = |
| 2112 | readWideAPInt(Record, cast<IntegerType>(CurTy)->getBitWidth()); |
Stepan Dyatkovskiy | 0beab5e | 2012-05-12 10:48:17 +0000 | [diff] [blame] | 2113 | V = ConstantInt::get(Context, VInt); |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 2114 | |
Chris Lattner | 08feb1e | 2007-04-24 04:04:35 +0000 | [diff] [blame] | 2115 | break; |
| 2116 | } |
Dale Johannesen | 245dceb | 2007-09-11 18:32:33 +0000 | [diff] [blame] | 2117 | case bitc::CST_CODE_FLOAT: { // FLOAT: [fpval] |
Chris Lattner | 08feb1e | 2007-04-24 04:04:35 +0000 | [diff] [blame] | 2118 | if (Record.empty()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2119 | return error("Invalid record"); |
Dan Gohman | 518cda4 | 2011-12-17 00:04:22 +0000 | [diff] [blame] | 2120 | if (CurTy->isHalfTy()) |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 2121 | V = ConstantFP::get(Context, APFloat(APFloat::IEEEhalf(), |
Tim Northover | 29178a3 | 2013-01-22 09:46:31 +0000 | [diff] [blame] | 2122 | APInt(16, (uint16_t)Record[0]))); |
Dan Gohman | 518cda4 | 2011-12-17 00:04:22 +0000 | [diff] [blame] | 2123 | else if (CurTy->isFloatTy()) |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 2124 | V = ConstantFP::get(Context, APFloat(APFloat::IEEEsingle(), |
Tim Northover | 29178a3 | 2013-01-22 09:46:31 +0000 | [diff] [blame] | 2125 | APInt(32, (uint32_t)Record[0]))); |
Chris Lattner | fdd8790 | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 2126 | else if (CurTy->isDoubleTy()) |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 2127 | V = ConstantFP::get(Context, APFloat(APFloat::IEEEdouble(), |
Tim Northover | 29178a3 | 2013-01-22 09:46:31 +0000 | [diff] [blame] | 2128 | APInt(64, Record[0]))); |
Chris Lattner | fdd8790 | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 2129 | else if (CurTy->isX86_FP80Ty()) { |
Dale Johannesen | 93eefa0 | 2009-03-23 21:16:53 +0000 | [diff] [blame] | 2130 | // Bits are not stored the same way as a normal i80 APInt, compensate. |
| 2131 | uint64_t Rearrange[2]; |
| 2132 | Rearrange[0] = (Record[1] & 0xffffLL) | (Record[0] << 16); |
| 2133 | Rearrange[1] = Record[0] >> 48; |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 2134 | V = ConstantFP::get(Context, APFloat(APFloat::x87DoubleExtended(), |
Tim Northover | 29178a3 | 2013-01-22 09:46:31 +0000 | [diff] [blame] | 2135 | APInt(80, Rearrange))); |
Chris Lattner | fdd8790 | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 2136 | } else if (CurTy->isFP128Ty()) |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 2137 | V = ConstantFP::get(Context, APFloat(APFloat::IEEEquad(), |
Tim Northover | 29178a3 | 2013-01-22 09:46:31 +0000 | [diff] [blame] | 2138 | APInt(128, Record))); |
Chris Lattner | fdd8790 | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 2139 | else if (CurTy->isPPC_FP128Ty()) |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 2140 | V = ConstantFP::get(Context, APFloat(APFloat::PPCDoubleDouble(), |
Tim Northover | 29178a3 | 2013-01-22 09:46:31 +0000 | [diff] [blame] | 2141 | APInt(128, Record))); |
Chris Lattner | fbc1d33 | 2007-04-24 03:30:34 +0000 | [diff] [blame] | 2142 | else |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 2143 | V = UndefValue::get(CurTy); |
Chris Lattner | fbc1d33 | 2007-04-24 03:30:34 +0000 | [diff] [blame] | 2144 | break; |
Dale Johannesen | 245dceb | 2007-09-11 18:32:33 +0000 | [diff] [blame] | 2145 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2146 | |
Chris Lattner | e14cb88 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 2147 | case bitc::CST_CODE_AGGREGATE: {// AGGREGATE: [n x value number] |
| 2148 | if (Record.empty()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2149 | return error("Invalid record"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2150 | |
Chris Lattner | e14cb88 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 2151 | unsigned Size = Record.size(); |
Chris Lattner | cc3aaf1 | 2012-01-27 03:15:49 +0000 | [diff] [blame] | 2152 | SmallVector<Constant*, 16> Elts; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2153 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2154 | if (StructType *STy = dyn_cast<StructType>(CurTy)) { |
Chris Lattner | 1663cca | 2007-04-24 05:48:56 +0000 | [diff] [blame] | 2155 | for (unsigned i = 0; i != Size; ++i) |
Chris Lattner | e14cb88 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 2156 | Elts.push_back(ValueList.getConstantFwdRef(Record[i], |
Chris Lattner | 1663cca | 2007-04-24 05:48:56 +0000 | [diff] [blame] | 2157 | STy->getElementType(i))); |
Owen Anderson | 45308b5 | 2009-07-27 22:29:26 +0000 | [diff] [blame] | 2158 | V = ConstantStruct::get(STy, Elts); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2159 | } else if (ArrayType *ATy = dyn_cast<ArrayType>(CurTy)) { |
| 2160 | Type *EltTy = ATy->getElementType(); |
Chris Lattner | 1663cca | 2007-04-24 05:48:56 +0000 | [diff] [blame] | 2161 | for (unsigned i = 0; i != Size; ++i) |
Chris Lattner | e14cb88 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 2162 | Elts.push_back(ValueList.getConstantFwdRef(Record[i], EltTy)); |
Owen Anderson | c2c7932 | 2009-07-28 18:32:17 +0000 | [diff] [blame] | 2163 | V = ConstantArray::get(ATy, Elts); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2164 | } else if (VectorType *VTy = dyn_cast<VectorType>(CurTy)) { |
| 2165 | Type *EltTy = VTy->getElementType(); |
Chris Lattner | 1663cca | 2007-04-24 05:48:56 +0000 | [diff] [blame] | 2166 | for (unsigned i = 0; i != Size; ++i) |
Chris Lattner | e14cb88 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 2167 | Elts.push_back(ValueList.getConstantFwdRef(Record[i], EltTy)); |
Owen Anderson | 4aa3295 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 2168 | V = ConstantVector::get(Elts); |
Chris Lattner | 1663cca | 2007-04-24 05:48:56 +0000 | [diff] [blame] | 2169 | } else { |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 2170 | V = UndefValue::get(CurTy); |
Chris Lattner | 1663cca | 2007-04-24 05:48:56 +0000 | [diff] [blame] | 2171 | } |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 2172 | break; |
| 2173 | } |
Chris Lattner | bb8278a | 2012-02-05 02:41:35 +0000 | [diff] [blame] | 2174 | case bitc::CST_CODE_STRING: // STRING: [values] |
Chris Lattner | f25f710 | 2007-05-06 00:53:07 +0000 | [diff] [blame] | 2175 | case bitc::CST_CODE_CSTRING: { // CSTRING: [values] |
| 2176 | if (Record.empty()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2177 | return error("Invalid record"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2178 | |
Benjamin Kramer | 9704ed0 | 2012-05-28 14:10:31 +0000 | [diff] [blame] | 2179 | SmallString<16> Elts(Record.begin(), Record.end()); |
Chris Lattner | bb8278a | 2012-02-05 02:41:35 +0000 | [diff] [blame] | 2180 | V = ConstantDataArray::getString(Context, Elts, |
| 2181 | BitCode == bitc::CST_CODE_CSTRING); |
Chris Lattner | f25f710 | 2007-05-06 00:53:07 +0000 | [diff] [blame] | 2182 | break; |
| 2183 | } |
Chris Lattner | 372dd1e | 2012-01-30 00:51:16 +0000 | [diff] [blame] | 2184 | case bitc::CST_CODE_DATA: {// DATA: [n x value] |
| 2185 | if (Record.empty()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2186 | return error("Invalid record"); |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 2187 | |
Chris Lattner | 372dd1e | 2012-01-30 00:51:16 +0000 | [diff] [blame] | 2188 | Type *EltTy = cast<SequentialType>(CurTy)->getElementType(); |
Chris Lattner | 372dd1e | 2012-01-30 00:51:16 +0000 | [diff] [blame] | 2189 | if (EltTy->isIntegerTy(8)) { |
| 2190 | SmallVector<uint8_t, 16> Elts(Record.begin(), Record.end()); |
| 2191 | if (isa<VectorType>(CurTy)) |
| 2192 | V = ConstantDataVector::get(Context, Elts); |
| 2193 | else |
| 2194 | V = ConstantDataArray::get(Context, Elts); |
| 2195 | } else if (EltTy->isIntegerTy(16)) { |
| 2196 | SmallVector<uint16_t, 16> Elts(Record.begin(), Record.end()); |
| 2197 | if (isa<VectorType>(CurTy)) |
| 2198 | V = ConstantDataVector::get(Context, Elts); |
| 2199 | else |
| 2200 | V = ConstantDataArray::get(Context, Elts); |
| 2201 | } else if (EltTy->isIntegerTy(32)) { |
| 2202 | SmallVector<uint32_t, 16> Elts(Record.begin(), Record.end()); |
| 2203 | if (isa<VectorType>(CurTy)) |
| 2204 | V = ConstantDataVector::get(Context, Elts); |
| 2205 | else |
| 2206 | V = ConstantDataArray::get(Context, Elts); |
| 2207 | } else if (EltTy->isIntegerTy(64)) { |
| 2208 | SmallVector<uint64_t, 16> Elts(Record.begin(), Record.end()); |
| 2209 | if (isa<VectorType>(CurTy)) |
| 2210 | V = ConstantDataVector::get(Context, Elts); |
| 2211 | else |
| 2212 | V = ConstantDataArray::get(Context, Elts); |
Justin Bogner | a43eacb | 2016-01-06 22:31:32 +0000 | [diff] [blame] | 2213 | } else if (EltTy->isHalfTy()) { |
| 2214 | SmallVector<uint16_t, 16> Elts(Record.begin(), Record.end()); |
| 2215 | if (isa<VectorType>(CurTy)) |
| 2216 | V = ConstantDataVector::getFP(Context, Elts); |
| 2217 | else |
| 2218 | V = ConstantDataArray::getFP(Context, Elts); |
Chris Lattner | 372dd1e | 2012-01-30 00:51:16 +0000 | [diff] [blame] | 2219 | } else if (EltTy->isFloatTy()) { |
Justin Bogner | a43eacb | 2016-01-06 22:31:32 +0000 | [diff] [blame] | 2220 | SmallVector<uint32_t, 16> Elts(Record.begin(), Record.end()); |
Chris Lattner | 372dd1e | 2012-01-30 00:51:16 +0000 | [diff] [blame] | 2221 | if (isa<VectorType>(CurTy)) |
Justin Bogner | a43eacb | 2016-01-06 22:31:32 +0000 | [diff] [blame] | 2222 | V = ConstantDataVector::getFP(Context, Elts); |
Chris Lattner | 372dd1e | 2012-01-30 00:51:16 +0000 | [diff] [blame] | 2223 | else |
Justin Bogner | a43eacb | 2016-01-06 22:31:32 +0000 | [diff] [blame] | 2224 | V = ConstantDataArray::getFP(Context, Elts); |
Chris Lattner | 372dd1e | 2012-01-30 00:51:16 +0000 | [diff] [blame] | 2225 | } else if (EltTy->isDoubleTy()) { |
Justin Bogner | a43eacb | 2016-01-06 22:31:32 +0000 | [diff] [blame] | 2226 | SmallVector<uint64_t, 16> Elts(Record.begin(), Record.end()); |
Chris Lattner | 372dd1e | 2012-01-30 00:51:16 +0000 | [diff] [blame] | 2227 | if (isa<VectorType>(CurTy)) |
Justin Bogner | a43eacb | 2016-01-06 22:31:32 +0000 | [diff] [blame] | 2228 | V = ConstantDataVector::getFP(Context, Elts); |
Chris Lattner | 372dd1e | 2012-01-30 00:51:16 +0000 | [diff] [blame] | 2229 | else |
Justin Bogner | a43eacb | 2016-01-06 22:31:32 +0000 | [diff] [blame] | 2230 | V = ConstantDataArray::getFP(Context, Elts); |
Chris Lattner | 372dd1e | 2012-01-30 00:51:16 +0000 | [diff] [blame] | 2231 | } else { |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2232 | return error("Invalid type for value"); |
Chris Lattner | 372dd1e | 2012-01-30 00:51:16 +0000 | [diff] [blame] | 2233 | } |
| 2234 | break; |
| 2235 | } |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 2236 | case bitc::CST_CODE_CE_BINOP: { // CE_BINOP: [opcode, opval, opval] |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 2237 | if (Record.size() < 3) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2238 | return error("Invalid record"); |
| 2239 | int Opc = getDecodedBinaryOpcode(Record[0], CurTy); |
Chris Lattner | 890683d | 2007-04-24 18:15:21 +0000 | [diff] [blame] | 2240 | if (Opc < 0) { |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 2241 | V = UndefValue::get(CurTy); // Unknown binop. |
Chris Lattner | 890683d | 2007-04-24 18:15:21 +0000 | [diff] [blame] | 2242 | } else { |
| 2243 | Constant *LHS = ValueList.getConstantFwdRef(Record[1], CurTy); |
| 2244 | Constant *RHS = ValueList.getConstantFwdRef(Record[2], CurTy); |
Dan Gohman | 1b84908 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 2245 | unsigned Flags = 0; |
| 2246 | if (Record.size() >= 4) { |
| 2247 | if (Opc == Instruction::Add || |
| 2248 | Opc == Instruction::Sub || |
Chris Lattner | a676c0f | 2011-02-07 16:40:21 +0000 | [diff] [blame] | 2249 | Opc == Instruction::Mul || |
| 2250 | Opc == Instruction::Shl) { |
Dan Gohman | 1b84908 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 2251 | if (Record[3] & (1 << bitc::OBO_NO_SIGNED_WRAP)) |
| 2252 | Flags |= OverflowingBinaryOperator::NoSignedWrap; |
| 2253 | if (Record[3] & (1 << bitc::OBO_NO_UNSIGNED_WRAP)) |
| 2254 | Flags |= OverflowingBinaryOperator::NoUnsignedWrap; |
Chris Lattner | 35315d0 | 2011-02-06 21:44:57 +0000 | [diff] [blame] | 2255 | } else if (Opc == Instruction::SDiv || |
Chris Lattner | a676c0f | 2011-02-07 16:40:21 +0000 | [diff] [blame] | 2256 | Opc == Instruction::UDiv || |
| 2257 | Opc == Instruction::LShr || |
| 2258 | Opc == Instruction::AShr) { |
Chris Lattner | 35315d0 | 2011-02-06 21:44:57 +0000 | [diff] [blame] | 2259 | if (Record[3] & (1 << bitc::PEO_EXACT)) |
Dan Gohman | 1b84908 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 2260 | Flags |= SDivOperator::IsExact; |
| 2261 | } |
| 2262 | } |
| 2263 | V = ConstantExpr::get(Opc, LHS, RHS, Flags); |
Chris Lattner | 890683d | 2007-04-24 18:15:21 +0000 | [diff] [blame] | 2264 | } |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 2265 | break; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2266 | } |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 2267 | case bitc::CST_CODE_CE_CAST: { // CE_CAST: [opcode, opty, opval] |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 2268 | if (Record.size() < 3) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2269 | return error("Invalid record"); |
| 2270 | int Opc = getDecodedCastOpcode(Record[0]); |
Chris Lattner | 890683d | 2007-04-24 18:15:21 +0000 | [diff] [blame] | 2271 | if (Opc < 0) { |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 2272 | V = UndefValue::get(CurTy); // Unknown cast. |
Chris Lattner | 890683d | 2007-04-24 18:15:21 +0000 | [diff] [blame] | 2273 | } else { |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2274 | Type *OpTy = getTypeByID(Record[1]); |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 2275 | if (!OpTy) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2276 | return error("Invalid record"); |
Chris Lattner | 890683d | 2007-04-24 18:15:21 +0000 | [diff] [blame] | 2277 | Constant *Op = ValueList.getConstantFwdRef(Record[2], OpTy); |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2278 | V = UpgradeBitCastExpr(Opc, Op, CurTy); |
| 2279 | if (!V) V = ConstantExpr::getCast(Opc, Op, CurTy); |
Chris Lattner | 890683d | 2007-04-24 18:15:21 +0000 | [diff] [blame] | 2280 | } |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 2281 | break; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2282 | } |
Peter Collingbourne | d93620b | 2016-11-10 22:34:55 +0000 | [diff] [blame] | 2283 | case bitc::CST_CODE_CE_INBOUNDS_GEP: // [ty, n x operands] |
| 2284 | case bitc::CST_CODE_CE_GEP: // [ty, n x operands] |
| 2285 | case bitc::CST_CODE_CE_GEP_WITH_INRANGE_INDEX: { // [ty, flags, n x |
| 2286 | // operands] |
David Blaikie | b926357 | 2015-03-13 21:03:36 +0000 | [diff] [blame] | 2287 | unsigned OpNum = 0; |
| 2288 | Type *PointeeType = nullptr; |
Peter Collingbourne | d93620b | 2016-11-10 22:34:55 +0000 | [diff] [blame] | 2289 | if (BitCode == bitc::CST_CODE_CE_GEP_WITH_INRANGE_INDEX || |
| 2290 | Record.size() % 2) |
David Blaikie | b926357 | 2015-03-13 21:03:36 +0000 | [diff] [blame] | 2291 | PointeeType = getTypeByID(Record[OpNum++]); |
Peter Collingbourne | d93620b | 2016-11-10 22:34:55 +0000 | [diff] [blame] | 2292 | |
| 2293 | bool InBounds = false; |
| 2294 | Optional<unsigned> InRangeIndex; |
| 2295 | if (BitCode == bitc::CST_CODE_CE_GEP_WITH_INRANGE_INDEX) { |
| 2296 | uint64_t Op = Record[OpNum++]; |
| 2297 | InBounds = Op & 1; |
| 2298 | InRangeIndex = Op >> 1; |
| 2299 | } else if (BitCode == bitc::CST_CODE_CE_INBOUNDS_GEP) |
| 2300 | InBounds = true; |
| 2301 | |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 2302 | SmallVector<Constant*, 16> Elts; |
David Blaikie | b926357 | 2015-03-13 21:03:36 +0000 | [diff] [blame] | 2303 | while (OpNum != Record.size()) { |
| 2304 | Type *ElTy = getTypeByID(Record[OpNum++]); |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 2305 | if (!ElTy) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2306 | return error("Invalid record"); |
David Blaikie | b926357 | 2015-03-13 21:03:36 +0000 | [diff] [blame] | 2307 | Elts.push_back(ValueList.getConstantFwdRef(Record[OpNum++], ElTy)); |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 2308 | } |
David Blaikie | b926357 | 2015-03-13 21:03:36 +0000 | [diff] [blame] | 2309 | |
David Blaikie | b926357 | 2015-03-13 21:03:36 +0000 | [diff] [blame] | 2310 | if (PointeeType && |
David Blaikie | 4a2e73b | 2015-04-02 18:55:32 +0000 | [diff] [blame] | 2311 | PointeeType != |
Peter Collingbourne | 9ef5a8c | 2016-11-13 06:59:28 +0000 | [diff] [blame] | 2312 | cast<PointerType>(Elts[0]->getType()->getScalarType()) |
David Blaikie | 4a2e73b | 2015-04-02 18:55:32 +0000 | [diff] [blame] | 2313 | ->getElementType()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2314 | return error("Explicit gep operator type does not match pointee type " |
David Blaikie | 12cf5d70 | 2015-03-16 22:03:50 +0000 | [diff] [blame] | 2315 | "of pointer operand"); |
David Blaikie | 4a2e73b | 2015-04-02 18:55:32 +0000 | [diff] [blame] | 2316 | |
Filipe Cabecinhas | fc2a3c9 | 2016-06-05 18:43:26 +0000 | [diff] [blame] | 2317 | if (Elts.size() < 1) |
| 2318 | return error("Invalid gep with no operands"); |
| 2319 | |
David Blaikie | 4a2e73b | 2015-04-02 18:55:32 +0000 | [diff] [blame] | 2320 | ArrayRef<Constant *> Indices(Elts.begin() + 1, Elts.end()); |
| 2321 | V = ConstantExpr::getGetElementPtr(PointeeType, Elts[0], Indices, |
Peter Collingbourne | d93620b | 2016-11-10 22:34:55 +0000 | [diff] [blame] | 2322 | InBounds, InRangeIndex); |
Chris Lattner | 890683d | 2007-04-24 18:15:21 +0000 | [diff] [blame] | 2323 | break; |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 2324 | } |
Joe Abbey | 1a6e770 | 2013-09-12 22:02:31 +0000 | [diff] [blame] | 2325 | case bitc::CST_CODE_CE_SELECT: { // CE_SELECT: [opval#, opval#, opval#] |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 2326 | if (Record.size() < 3) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2327 | return error("Invalid record"); |
Joe Abbey | 1a6e770 | 2013-09-12 22:02:31 +0000 | [diff] [blame] | 2328 | |
| 2329 | Type *SelectorTy = Type::getInt1Ty(Context); |
| 2330 | |
Filipe Cabecinhas | 984fefd | 2015-08-31 18:00:30 +0000 | [diff] [blame] | 2331 | // The selector might be an i1 or an <n x i1> |
| 2332 | // Get the type from the ValueList before getting a forward ref. |
Joe Abbey | 1a6e770 | 2013-09-12 22:02:31 +0000 | [diff] [blame] | 2333 | if (VectorType *VTy = dyn_cast<VectorType>(CurTy)) |
Filipe Cabecinhas | 984fefd | 2015-08-31 18:00:30 +0000 | [diff] [blame] | 2334 | if (Value *V = ValueList[Record[0]]) |
| 2335 | if (SelectorTy != V->getType()) |
| 2336 | SelectorTy = VectorType::get(SelectorTy, VTy->getNumElements()); |
Joe Abbey | 1a6e770 | 2013-09-12 22:02:31 +0000 | [diff] [blame] | 2337 | |
| 2338 | V = ConstantExpr::getSelect(ValueList.getConstantFwdRef(Record[0], |
| 2339 | SelectorTy), |
| 2340 | ValueList.getConstantFwdRef(Record[1],CurTy), |
| 2341 | ValueList.getConstantFwdRef(Record[2],CurTy)); |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 2342 | break; |
Joe Abbey | 1a6e770 | 2013-09-12 22:02:31 +0000 | [diff] [blame] | 2343 | } |
Michael J. Spencer | 1f10c5ea | 2014-05-01 22:12:39 +0000 | [diff] [blame] | 2344 | case bitc::CST_CODE_CE_EXTRACTELT |
| 2345 | : { // CE_EXTRACTELT: [opty, opval, opty, opval] |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 2346 | if (Record.size() < 3) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2347 | return error("Invalid record"); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2348 | VectorType *OpTy = |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 2349 | dyn_cast_or_null<VectorType>(getTypeByID(Record[0])); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 2350 | if (!OpTy) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2351 | return error("Invalid record"); |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 2352 | Constant *Op0 = ValueList.getConstantFwdRef(Record[1], OpTy); |
Michael J. Spencer | 1f10c5ea | 2014-05-01 22:12:39 +0000 | [diff] [blame] | 2353 | Constant *Op1 = nullptr; |
| 2354 | if (Record.size() == 4) { |
| 2355 | Type *IdxTy = getTypeByID(Record[2]); |
| 2356 | if (!IdxTy) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2357 | return error("Invalid record"); |
Michael J. Spencer | 1f10c5ea | 2014-05-01 22:12:39 +0000 | [diff] [blame] | 2358 | Op1 = ValueList.getConstantFwdRef(Record[3], IdxTy); |
| 2359 | } else // TODO: Remove with llvm 4.0 |
| 2360 | Op1 = ValueList.getConstantFwdRef(Record[2], Type::getInt32Ty(Context)); |
| 2361 | if (!Op1) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2362 | return error("Invalid record"); |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2363 | V = ConstantExpr::getExtractElement(Op0, Op1); |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 2364 | break; |
| 2365 | } |
Michael J. Spencer | 1f10c5ea | 2014-05-01 22:12:39 +0000 | [diff] [blame] | 2366 | case bitc::CST_CODE_CE_INSERTELT |
| 2367 | : { // CE_INSERTELT: [opval, opval, opty, opval] |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2368 | VectorType *OpTy = dyn_cast<VectorType>(CurTy); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 2369 | if (Record.size() < 3 || !OpTy) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2370 | return error("Invalid record"); |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 2371 | Constant *Op0 = ValueList.getConstantFwdRef(Record[0], OpTy); |
| 2372 | Constant *Op1 = ValueList.getConstantFwdRef(Record[1], |
| 2373 | OpTy->getElementType()); |
Michael J. Spencer | 1f10c5ea | 2014-05-01 22:12:39 +0000 | [diff] [blame] | 2374 | Constant *Op2 = nullptr; |
| 2375 | if (Record.size() == 4) { |
| 2376 | Type *IdxTy = getTypeByID(Record[2]); |
| 2377 | if (!IdxTy) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2378 | return error("Invalid record"); |
Michael J. Spencer | 1f10c5ea | 2014-05-01 22:12:39 +0000 | [diff] [blame] | 2379 | Op2 = ValueList.getConstantFwdRef(Record[3], IdxTy); |
| 2380 | } else // TODO: Remove with llvm 4.0 |
| 2381 | Op2 = ValueList.getConstantFwdRef(Record[2], Type::getInt32Ty(Context)); |
| 2382 | if (!Op2) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2383 | return error("Invalid record"); |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2384 | V = ConstantExpr::getInsertElement(Op0, Op1, Op2); |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 2385 | break; |
| 2386 | } |
| 2387 | case bitc::CST_CODE_CE_SHUFFLEVEC: { // CE_SHUFFLEVEC: [opval, opval, opval] |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2388 | VectorType *OpTy = dyn_cast<VectorType>(CurTy); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 2389 | if (Record.size() < 3 || !OpTy) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2390 | return error("Invalid record"); |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 2391 | Constant *Op0 = ValueList.getConstantFwdRef(Record[0], OpTy); |
| 2392 | Constant *Op1 = ValueList.getConstantFwdRef(Record[1], OpTy); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2393 | Type *ShufTy = VectorType::get(Type::getInt32Ty(Context), |
Owen Anderson | e9f9804 | 2009-07-07 20:18:58 +0000 | [diff] [blame] | 2394 | OpTy->getNumElements()); |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 2395 | Constant *Op2 = ValueList.getConstantFwdRef(Record[2], ShufTy); |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2396 | V = ConstantExpr::getShuffleVector(Op0, Op1, Op2); |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 2397 | break; |
| 2398 | } |
Nate Begeman | 94aa38d | 2009-02-12 21:28:33 +0000 | [diff] [blame] | 2399 | case bitc::CST_CODE_CE_SHUFVEC_EX: { // [opty, opval, opval, opval] |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2400 | VectorType *RTy = dyn_cast<VectorType>(CurTy); |
| 2401 | VectorType *OpTy = |
Duncan Sands | 89d412a | 2010-10-28 15:47:26 +0000 | [diff] [blame] | 2402 | dyn_cast_or_null<VectorType>(getTypeByID(Record[0])); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 2403 | if (Record.size() < 4 || !RTy || !OpTy) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2404 | return error("Invalid record"); |
Nate Begeman | 94aa38d | 2009-02-12 21:28:33 +0000 | [diff] [blame] | 2405 | Constant *Op0 = ValueList.getConstantFwdRef(Record[1], OpTy); |
| 2406 | Constant *Op1 = ValueList.getConstantFwdRef(Record[2], OpTy); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2407 | Type *ShufTy = VectorType::get(Type::getInt32Ty(Context), |
Owen Anderson | e9f9804 | 2009-07-07 20:18:58 +0000 | [diff] [blame] | 2408 | RTy->getNumElements()); |
Nate Begeman | 94aa38d | 2009-02-12 21:28:33 +0000 | [diff] [blame] | 2409 | Constant *Op2 = ValueList.getConstantFwdRef(Record[3], ShufTy); |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2410 | V = ConstantExpr::getShuffleVector(Op0, Op1, Op2); |
Nate Begeman | 94aa38d | 2009-02-12 21:28:33 +0000 | [diff] [blame] | 2411 | break; |
| 2412 | } |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 2413 | case bitc::CST_CODE_CE_CMP: { // CE_CMP: [opty, opval, opval, pred] |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 2414 | if (Record.size() < 4) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2415 | return error("Invalid record"); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2416 | Type *OpTy = getTypeByID(Record[0]); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 2417 | if (!OpTy) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2418 | return error("Invalid record"); |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 2419 | Constant *Op0 = ValueList.getConstantFwdRef(Record[1], OpTy); |
| 2420 | Constant *Op1 = ValueList.getConstantFwdRef(Record[2], OpTy); |
| 2421 | |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2422 | if (OpTy->isFPOrFPVectorTy()) |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2423 | V = ConstantExpr::getFCmp(Record[3], Op0, Op1); |
Nate Begeman | d219570 | 2008-05-12 19:01:56 +0000 | [diff] [blame] | 2424 | else |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2425 | V = ConstantExpr::getICmp(Record[3], Op0, Op1); |
Chris Lattner | 1e16bcf7 | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 2426 | break; |
Chris Lattner | 1663cca | 2007-04-24 05:48:56 +0000 | [diff] [blame] | 2427 | } |
Chad Rosier | d8c7610 | 2012-09-05 19:00:49 +0000 | [diff] [blame] | 2428 | // This maintains backward compatibility, pre-asm dialect keywords. |
Chad Rosier | 5895eda | 2012-09-05 06:28:52 +0000 | [diff] [blame] | 2429 | // FIXME: Remove with the 4.0 release. |
Chad Rosier | 18fcdcf | 2012-09-05 00:56:20 +0000 | [diff] [blame] | 2430 | case bitc::CST_CODE_INLINEASM_OLD: { |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 2431 | if (Record.size() < 2) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2432 | return error("Invalid record"); |
Chris Lattner | af8fffc | 2007-05-06 01:58:20 +0000 | [diff] [blame] | 2433 | std::string AsmStr, ConstrStr; |
Dale Johannesen | fd04c74 | 2009-10-13 20:46:56 +0000 | [diff] [blame] | 2434 | bool HasSideEffects = Record[0] & 1; |
Dale Johannesen | 1cfb958 | 2009-10-21 23:28:00 +0000 | [diff] [blame] | 2435 | bool IsAlignStack = Record[0] >> 1; |
Chris Lattner | af8fffc | 2007-05-06 01:58:20 +0000 | [diff] [blame] | 2436 | unsigned AsmStrSize = Record[1]; |
| 2437 | if (2+AsmStrSize >= Record.size()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2438 | return error("Invalid record"); |
Chris Lattner | af8fffc | 2007-05-06 01:58:20 +0000 | [diff] [blame] | 2439 | unsigned ConstStrSize = Record[2+AsmStrSize]; |
| 2440 | if (3+AsmStrSize+ConstStrSize > Record.size()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2441 | return error("Invalid record"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2442 | |
Chris Lattner | af8fffc | 2007-05-06 01:58:20 +0000 | [diff] [blame] | 2443 | for (unsigned i = 0; i != AsmStrSize; ++i) |
| 2444 | AsmStr += (char)Record[2+i]; |
| 2445 | for (unsigned i = 0; i != ConstStrSize; ++i) |
| 2446 | ConstrStr += (char)Record[3+AsmStrSize+i]; |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2447 | PointerType *PTy = cast<PointerType>(CurTy); |
Chris Lattner | af8fffc | 2007-05-06 01:58:20 +0000 | [diff] [blame] | 2448 | V = InlineAsm::get(cast<FunctionType>(PTy->getElementType()), |
Dale Johannesen | 1cfb958 | 2009-10-21 23:28:00 +0000 | [diff] [blame] | 2449 | AsmStr, ConstrStr, HasSideEffects, IsAlignStack); |
Chris Lattner | af8fffc | 2007-05-06 01:58:20 +0000 | [diff] [blame] | 2450 | break; |
| 2451 | } |
Chad Rosier | d8c7610 | 2012-09-05 19:00:49 +0000 | [diff] [blame] | 2452 | // This version adds support for the asm dialect keywords (e.g., |
| 2453 | // inteldialect). |
Chad Rosier | 18fcdcf | 2012-09-05 00:56:20 +0000 | [diff] [blame] | 2454 | case bitc::CST_CODE_INLINEASM: { |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 2455 | if (Record.size() < 2) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2456 | return error("Invalid record"); |
Chad Rosier | 18fcdcf | 2012-09-05 00:56:20 +0000 | [diff] [blame] | 2457 | std::string AsmStr, ConstrStr; |
| 2458 | bool HasSideEffects = Record[0] & 1; |
| 2459 | bool IsAlignStack = (Record[0] >> 1) & 1; |
| 2460 | unsigned AsmDialect = Record[0] >> 2; |
| 2461 | unsigned AsmStrSize = Record[1]; |
| 2462 | if (2+AsmStrSize >= Record.size()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2463 | return error("Invalid record"); |
Chad Rosier | 18fcdcf | 2012-09-05 00:56:20 +0000 | [diff] [blame] | 2464 | unsigned ConstStrSize = Record[2+AsmStrSize]; |
| 2465 | if (3+AsmStrSize+ConstStrSize > Record.size()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2466 | return error("Invalid record"); |
Chad Rosier | 18fcdcf | 2012-09-05 00:56:20 +0000 | [diff] [blame] | 2467 | |
| 2468 | for (unsigned i = 0; i != AsmStrSize; ++i) |
| 2469 | AsmStr += (char)Record[2+i]; |
| 2470 | for (unsigned i = 0; i != ConstStrSize; ++i) |
| 2471 | ConstrStr += (char)Record[3+AsmStrSize+i]; |
| 2472 | PointerType *PTy = cast<PointerType>(CurTy); |
| 2473 | V = InlineAsm::get(cast<FunctionType>(PTy->getElementType()), |
| 2474 | AsmStr, ConstrStr, HasSideEffects, IsAlignStack, |
Chad Rosier | d8c7610 | 2012-09-05 19:00:49 +0000 | [diff] [blame] | 2475 | InlineAsm::AsmDialect(AsmDialect)); |
Chad Rosier | 18fcdcf | 2012-09-05 00:56:20 +0000 | [diff] [blame] | 2476 | break; |
| 2477 | } |
Chris Lattner | 5956dc8 | 2009-10-28 05:53:48 +0000 | [diff] [blame] | 2478 | case bitc::CST_CODE_BLOCKADDRESS:{ |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 2479 | if (Record.size() < 3) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2480 | return error("Invalid record"); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2481 | Type *FnTy = getTypeByID(Record[0]); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 2482 | if (!FnTy) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2483 | return error("Invalid record"); |
Chris Lattner | 5956dc8 | 2009-10-28 05:53:48 +0000 | [diff] [blame] | 2484 | Function *Fn = |
| 2485 | dyn_cast_or_null<Function>(ValueList.getConstantFwdRef(Record[1],FnTy)); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 2486 | if (!Fn) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2487 | return error("Invalid record"); |
Benjamin Kramer | 736a4fc | 2012-09-21 14:34:31 +0000 | [diff] [blame] | 2488 | |
| 2489 | // If the function is already parsed we can insert the block address right |
| 2490 | // away. |
Duncan P. N. Exon Smith | 00f20ac | 2014-08-01 21:51:52 +0000 | [diff] [blame] | 2491 | BasicBlock *BB; |
| 2492 | unsigned BBID = Record[2]; |
| 2493 | if (!BBID) |
| 2494 | // Invalid reference to entry block. |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2495 | return error("Invalid ID"); |
Benjamin Kramer | 736a4fc | 2012-09-21 14:34:31 +0000 | [diff] [blame] | 2496 | if (!Fn->empty()) { |
| 2497 | Function::iterator BBI = Fn->begin(), BBE = Fn->end(); |
Duncan P. N. Exon Smith | 00f20ac | 2014-08-01 21:51:52 +0000 | [diff] [blame] | 2498 | for (size_t I = 0, E = BBID; I != E; ++I) { |
Benjamin Kramer | 736a4fc | 2012-09-21 14:34:31 +0000 | [diff] [blame] | 2499 | if (BBI == BBE) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2500 | return error("Invalid ID"); |
Benjamin Kramer | 736a4fc | 2012-09-21 14:34:31 +0000 | [diff] [blame] | 2501 | ++BBI; |
| 2502 | } |
Duncan P. N. Exon Smith | fb1743a3 | 2015-10-13 16:48:55 +0000 | [diff] [blame] | 2503 | BB = &*BBI; |
Benjamin Kramer | 736a4fc | 2012-09-21 14:34:31 +0000 | [diff] [blame] | 2504 | } else { |
| 2505 | // Otherwise insert a placeholder and remember it so it can be inserted |
| 2506 | // when the function is parsed. |
Duncan P. N. Exon Smith | 5a511b5 | 2014-08-05 17:49:48 +0000 | [diff] [blame] | 2507 | auto &FwdBBs = BasicBlockFwdRefs[Fn]; |
| 2508 | if (FwdBBs.empty()) |
| 2509 | BasicBlockFwdRefQueue.push_back(Fn); |
Duncan P. N. Exon Smith | 5a5fd7b | 2014-08-16 01:54:37 +0000 | [diff] [blame] | 2510 | if (FwdBBs.size() < BBID + 1) |
| 2511 | FwdBBs.resize(BBID + 1); |
| 2512 | if (!FwdBBs[BBID]) |
| 2513 | FwdBBs[BBID] = BasicBlock::Create(Context); |
| 2514 | BB = FwdBBs[BBID]; |
Benjamin Kramer | 736a4fc | 2012-09-21 14:34:31 +0000 | [diff] [blame] | 2515 | } |
Duncan P. N. Exon Smith | 00f20ac | 2014-08-01 21:51:52 +0000 | [diff] [blame] | 2516 | V = BlockAddress::get(Fn, BB); |
Chris Lattner | 5956dc8 | 2009-10-28 05:53:48 +0000 | [diff] [blame] | 2517 | break; |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 2518 | } |
Chris Lattner | fbc1d33 | 2007-04-24 03:30:34 +0000 | [diff] [blame] | 2519 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2520 | |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 2521 | ValueList.assignValue(V, NextCstNo); |
Chris Lattner | 1663cca | 2007-04-24 05:48:56 +0000 | [diff] [blame] | 2522 | ++NextCstNo; |
Chris Lattner | fbc1d33 | 2007-04-24 03:30:34 +0000 | [diff] [blame] | 2523 | } |
| 2524 | } |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 2525 | |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 2526 | Error BitcodeReader::parseUseLists() { |
Chad Rosier | ca2567b | 2011-12-07 21:44:12 +0000 | [diff] [blame] | 2527 | if (Stream.EnterSubBlock(bitc::USELIST_BLOCK_ID)) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2528 | return error("Invalid record"); |
Chad Rosier | ca2567b | 2011-12-07 21:44:12 +0000 | [diff] [blame] | 2529 | |
Chad Rosier | ca2567b | 2011-12-07 21:44:12 +0000 | [diff] [blame] | 2530 | // Read all the records. |
Duncan P. N. Exon Smith | 1f66c85 | 2014-07-28 21:19:41 +0000 | [diff] [blame] | 2531 | SmallVector<uint64_t, 64> Record; |
Eugene Zelenko | 1804a77 | 2016-08-25 00:45:04 +0000 | [diff] [blame] | 2532 | |
| 2533 | while (true) { |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 2534 | BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); |
Joe Abbey | 97b7a17 | 2013-02-06 22:14:06 +0000 | [diff] [blame] | 2535 | |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 2536 | switch (Entry.Kind) { |
| 2537 | case BitstreamEntry::SubBlock: // Handled for us already. |
| 2538 | case BitstreamEntry::Error: |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2539 | return error("Malformed block"); |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 2540 | case BitstreamEntry::EndBlock: |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 2541 | return Error::success(); |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 2542 | case BitstreamEntry::Record: |
| 2543 | // The interesting case. |
| 2544 | break; |
Chad Rosier | ca2567b | 2011-12-07 21:44:12 +0000 | [diff] [blame] | 2545 | } |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 2546 | |
Chad Rosier | ca2567b | 2011-12-07 21:44:12 +0000 | [diff] [blame] | 2547 | // Read a use list record. |
| 2548 | Record.clear(); |
Duncan P. N. Exon Smith | 1f66c85 | 2014-07-28 21:19:41 +0000 | [diff] [blame] | 2549 | bool IsBB = false; |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 2550 | switch (Stream.readRecord(Entry.ID, Record)) { |
Chad Rosier | ca2567b | 2011-12-07 21:44:12 +0000 | [diff] [blame] | 2551 | default: // Default behavior: unknown type. |
| 2552 | break; |
Duncan P. N. Exon Smith | 1f66c85 | 2014-07-28 21:19:41 +0000 | [diff] [blame] | 2553 | case bitc::USELIST_CODE_BB: |
| 2554 | IsBB = true; |
Justin Bogner | b03fd12 | 2016-08-17 05:10:15 +0000 | [diff] [blame] | 2555 | LLVM_FALLTHROUGH; |
Duncan P. N. Exon Smith | 1f66c85 | 2014-07-28 21:19:41 +0000 | [diff] [blame] | 2556 | case bitc::USELIST_CODE_DEFAULT: { |
Chad Rosier | ca2567b | 2011-12-07 21:44:12 +0000 | [diff] [blame] | 2557 | unsigned RecordLength = Record.size(); |
Duncan P. N. Exon Smith | 1f66c85 | 2014-07-28 21:19:41 +0000 | [diff] [blame] | 2558 | if (RecordLength < 3) |
| 2559 | // Records should have at least an ID and two indexes. |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2560 | return error("Invalid record"); |
Duncan P. N. Exon Smith | 1f66c85 | 2014-07-28 21:19:41 +0000 | [diff] [blame] | 2561 | unsigned ID = Record.back(); |
| 2562 | Record.pop_back(); |
| 2563 | |
| 2564 | Value *V; |
| 2565 | if (IsBB) { |
| 2566 | assert(ID < FunctionBBs.size() && "Basic block not found"); |
| 2567 | V = FunctionBBs[ID]; |
| 2568 | } else |
| 2569 | V = ValueList[ID]; |
| 2570 | unsigned NumUses = 0; |
| 2571 | SmallDenseMap<const Use *, unsigned, 16> Order; |
Rafael Espindola | 257a353 | 2016-01-15 19:00:20 +0000 | [diff] [blame] | 2572 | for (const Use &U : V->materialized_uses()) { |
Duncan P. N. Exon Smith | 1318364 | 2014-08-16 01:54:34 +0000 | [diff] [blame] | 2573 | if (++NumUses > Record.size()) |
Duncan P. N. Exon Smith | 1f66c85 | 2014-07-28 21:19:41 +0000 | [diff] [blame] | 2574 | break; |
Duncan P. N. Exon Smith | 1318364 | 2014-08-16 01:54:34 +0000 | [diff] [blame] | 2575 | Order[&U] = Record[NumUses - 1]; |
Duncan P. N. Exon Smith | 1f66c85 | 2014-07-28 21:19:41 +0000 | [diff] [blame] | 2576 | } |
| 2577 | if (Order.size() != Record.size() || NumUses > Record.size()) |
| 2578 | // Mismatches can happen if the functions are being materialized lazily |
| 2579 | // (out-of-order), or a value has been upgraded. |
| 2580 | break; |
| 2581 | |
| 2582 | V->sortUseList([&](const Use &L, const Use &R) { |
| 2583 | return Order.lookup(&L) < Order.lookup(&R); |
| 2584 | }); |
Chad Rosier | ca2567b | 2011-12-07 21:44:12 +0000 | [diff] [blame] | 2585 | break; |
| 2586 | } |
| 2587 | } |
| 2588 | } |
| 2589 | } |
| 2590 | |
Manman Ren | 4a9b0eb | 2015-03-13 19:24:30 +0000 | [diff] [blame] | 2591 | /// When we see the block for metadata, remember where it is and then skip it. |
| 2592 | /// This lets us lazily deserialize the metadata. |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 2593 | Error BitcodeReader::rememberAndSkipMetadata() { |
Manman Ren | 4a9b0eb | 2015-03-13 19:24:30 +0000 | [diff] [blame] | 2594 | // Save the current stream state. |
| 2595 | uint64_t CurBit = Stream.GetCurrentBitNo(); |
| 2596 | DeferredMetadataInfo.push_back(CurBit); |
| 2597 | |
| 2598 | // Skip over the block for now. |
| 2599 | if (Stream.SkipBlock()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2600 | return error("Invalid record"); |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 2601 | return Error::success(); |
Manman Ren | 4a9b0eb | 2015-03-13 19:24:30 +0000 | [diff] [blame] | 2602 | } |
| 2603 | |
Peter Collingbourne | 7f00d0a | 2016-11-09 17:49:19 +0000 | [diff] [blame] | 2604 | Error BitcodeReader::materializeMetadata() { |
Manman Ren | 4a9b0eb | 2015-03-13 19:24:30 +0000 | [diff] [blame] | 2605 | for (uint64_t BitPos : DeferredMetadataInfo) { |
| 2606 | // Move the bit stream to the saved position. |
| 2607 | Stream.JumpToBit(BitPos); |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 2608 | if (Error Err = MDLoader->parseModuleMetadata()) |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 2609 | return Err; |
Manman Ren | 4a9b0eb | 2015-03-13 19:24:30 +0000 | [diff] [blame] | 2610 | } |
| 2611 | DeferredMetadataInfo.clear(); |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 2612 | return Error::success(); |
Manman Ren | 4a9b0eb | 2015-03-13 19:24:30 +0000 | [diff] [blame] | 2613 | } |
| 2614 | |
Rafael Espindola | 468b868 | 2015-04-01 14:44:59 +0000 | [diff] [blame] | 2615 | void BitcodeReader::setStripDebugInfo() { StripDebugInfo = true; } |
Rafael Espindola | 0d68b4c | 2015-03-30 21:36:43 +0000 | [diff] [blame] | 2616 | |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2617 | /// When we see the block for a function body, remember where it is and then |
| 2618 | /// skip it. This lets us lazily deserialize the functions. |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 2619 | Error BitcodeReader::rememberAndSkipFunctionBody() { |
Chris Lattner | 51ffe7c | 2007-05-01 04:59:48 +0000 | [diff] [blame] | 2620 | // Get the function we are talking about. |
| 2621 | if (FunctionsWithBodies.empty()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2622 | return error("Insufficient function protos"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2623 | |
Chris Lattner | 51ffe7c | 2007-05-01 04:59:48 +0000 | [diff] [blame] | 2624 | Function *Fn = FunctionsWithBodies.back(); |
| 2625 | FunctionsWithBodies.pop_back(); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2626 | |
Chris Lattner | 51ffe7c | 2007-05-01 04:59:48 +0000 | [diff] [blame] | 2627 | // Save the current stream state. |
| 2628 | uint64_t CurBit = Stream.GetCurrentBitNo(); |
Teresa Johnson | 1493ad9 | 2015-10-10 14:18:36 +0000 | [diff] [blame] | 2629 | assert( |
| 2630 | (DeferredFunctionInfo[Fn] == 0 || DeferredFunctionInfo[Fn] == CurBit) && |
| 2631 | "Mismatch between VST and scanned function offsets"); |
Jeffrey Yasskin | 091217b | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 2632 | DeferredFunctionInfo[Fn] = CurBit; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2633 | |
Chris Lattner | 51ffe7c | 2007-05-01 04:59:48 +0000 | [diff] [blame] | 2634 | // Skip over the function block for now. |
| 2635 | if (Stream.SkipBlock()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2636 | return error("Invalid record"); |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 2637 | return Error::success(); |
Chris Lattner | 51ffe7c | 2007-05-01 04:59:48 +0000 | [diff] [blame] | 2638 | } |
| 2639 | |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 2640 | Error BitcodeReader::globalCleanup() { |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 2641 | // Patch the initializers for globals and aliases up. |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 2642 | if (Error Err = resolveGlobalAndIndirectSymbolInits()) |
| 2643 | return Err; |
Dmitry Polukhin | a3d5b0b | 2016-04-05 08:47:51 +0000 | [diff] [blame] | 2644 | if (!GlobalInits.empty() || !IndirectSymbolInits.empty()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2645 | return error("Malformed global initializer set"); |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 2646 | |
| 2647 | // Look for intrinsic functions which need to be upgraded at some point |
Yaron Keren | ef5e7ad | 2015-06-12 18:13:20 +0000 | [diff] [blame] | 2648 | for (Function &F : *TheModule) { |
Adrian Prantl | 6825fb6 | 2017-04-18 01:21:53 +0000 | [diff] [blame] | 2649 | MDLoader->upgradeDebugIntrinsics(F); |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 2650 | Function *NewFn; |
Yaron Keren | ef5e7ad | 2015-06-12 18:13:20 +0000 | [diff] [blame] | 2651 | if (UpgradeIntrinsicFunction(&F, NewFn)) |
Rafael Espindola | 4e72121 | 2015-07-02 16:22:40 +0000 | [diff] [blame] | 2652 | UpgradedIntrinsics[&F] = NewFn; |
Artur Pilipenko | 6c7a8ab | 2016-06-24 15:10:29 +0000 | [diff] [blame] | 2653 | else if (auto Remangled = Intrinsic::remangleIntrinsicFunction(&F)) |
| 2654 | // Some types could be renamed during loading if several modules are |
| 2655 | // loaded in the same LLVMContext (LTO scenario). In this case we should |
| 2656 | // remangle intrinsics names as well. |
| 2657 | RemangledIntrinsics[&F] = Remangled.getValue(); |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 2658 | } |
| 2659 | |
| 2660 | // Look for global variables which need to be renamed. |
Yaron Keren | ef5e7ad | 2015-06-12 18:13:20 +0000 | [diff] [blame] | 2661 | for (GlobalVariable &GV : TheModule->globals()) |
| 2662 | UpgradeGlobalVariable(&GV); |
Reid Kleckner | fceb76f | 2014-05-16 20:39:27 +0000 | [diff] [blame] | 2663 | |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 2664 | // Force deallocation of memory for these vectors to favor the client that |
| 2665 | // want lazy deserialization. |
| 2666 | std::vector<std::pair<GlobalVariable*, unsigned> >().swap(GlobalInits); |
Dmitry Polukhin | a3d5b0b | 2016-04-05 08:47:51 +0000 | [diff] [blame] | 2667 | std::vector<std::pair<GlobalIndirectSymbol*, unsigned> >().swap( |
| 2668 | IndirectSymbolInits); |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 2669 | return Error::success(); |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 2670 | } |
| 2671 | |
Teresa Johnson | 1493ad9 | 2015-10-10 14:18:36 +0000 | [diff] [blame] | 2672 | /// Support for lazy parsing of function bodies. This is required if we |
| 2673 | /// either have an old bitcode file without a VST forward declaration record, |
| 2674 | /// or if we have an anonymous function being materialized, since anonymous |
| 2675 | /// functions do not have a name and are therefore not in the VST. |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 2676 | Error BitcodeReader::rememberAndSkipFunctionBodies() { |
Teresa Johnson | 1493ad9 | 2015-10-10 14:18:36 +0000 | [diff] [blame] | 2677 | Stream.JumpToBit(NextUnreadBit); |
| 2678 | |
Teresa Johnson | f72278f | 2015-11-02 18:02:11 +0000 | [diff] [blame] | 2679 | if (Stream.AtEndOfStream()) |
| 2680 | return error("Could not find function in stream"); |
Teresa Johnson | 1493ad9 | 2015-10-10 14:18:36 +0000 | [diff] [blame] | 2681 | |
Filipe Cabecinhas | 7aae2f2 | 2015-11-03 13:48:26 +0000 | [diff] [blame] | 2682 | if (!SeenFirstFunctionBody) |
| 2683 | return error("Trying to materialize functions before seeing function blocks"); |
| 2684 | |
Teresa Johnson | 1493ad9 | 2015-10-10 14:18:36 +0000 | [diff] [blame] | 2685 | // An old bitcode file with the symbol table at the end would have |
| 2686 | // finished the parse greedily. |
| 2687 | assert(SeenValueSymbolTable); |
| 2688 | |
| 2689 | SmallVector<uint64_t, 64> Record; |
| 2690 | |
Eugene Zelenko | 1804a77 | 2016-08-25 00:45:04 +0000 | [diff] [blame] | 2691 | while (true) { |
Teresa Johnson | 1493ad9 | 2015-10-10 14:18:36 +0000 | [diff] [blame] | 2692 | BitstreamEntry Entry = Stream.advance(); |
| 2693 | switch (Entry.Kind) { |
Teresa Johnson | f72278f | 2015-11-02 18:02:11 +0000 | [diff] [blame] | 2694 | default: |
| 2695 | return error("Expect SubBlock"); |
| 2696 | case BitstreamEntry::SubBlock: |
| 2697 | switch (Entry.ID) { |
Teresa Johnson | 1493ad9 | 2015-10-10 14:18:36 +0000 | [diff] [blame] | 2698 | default: |
Teresa Johnson | f72278f | 2015-11-02 18:02:11 +0000 | [diff] [blame] | 2699 | return error("Expect function block"); |
| 2700 | case bitc::FUNCTION_BLOCK_ID: |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 2701 | if (Error Err = rememberAndSkipFunctionBody()) |
| 2702 | return Err; |
Teresa Johnson | f72278f | 2015-11-02 18:02:11 +0000 | [diff] [blame] | 2703 | NextUnreadBit = Stream.GetCurrentBitNo(); |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 2704 | return Error::success(); |
Teresa Johnson | f72278f | 2015-11-02 18:02:11 +0000 | [diff] [blame] | 2705 | } |
Teresa Johnson | 1493ad9 | 2015-10-10 14:18:36 +0000 | [diff] [blame] | 2706 | } |
| 2707 | } |
| 2708 | } |
| 2709 | |
Peter Collingbourne | 939c7d9 | 2016-11-08 04:16:57 +0000 | [diff] [blame] | 2710 | bool BitcodeReaderBase::readBlockInfo() { |
Peter Collingbourne | 77c89b6 | 2016-11-08 04:17:11 +0000 | [diff] [blame] | 2711 | Optional<BitstreamBlockInfo> NewBlockInfo = Stream.ReadBlockInfoBlock(); |
| 2712 | if (!NewBlockInfo) |
| 2713 | return true; |
| 2714 | BlockInfo = std::move(*NewBlockInfo); |
| 2715 | return false; |
Peter Collingbourne | 939c7d9 | 2016-11-08 04:16:57 +0000 | [diff] [blame] | 2716 | } |
| 2717 | |
Peter Collingbourne | ce24a2a | 2017-04-12 20:02:09 +0000 | [diff] [blame] | 2718 | Error BitcodeReader::parseComdatRecord(ArrayRef<uint64_t> Record) { |
Peter Collingbourne | a0f371a | 2017-04-17 17:51:36 +0000 | [diff] [blame] | 2719 | // v1: [selection_kind, name] |
| 2720 | // v2: [strtab_offset, strtab_size, selection_kind] |
| 2721 | StringRef Name; |
| 2722 | std::tie(Name, Record) = readNameFromStrtab(Record); |
| 2723 | |
| 2724 | if (Record.size() < 1) |
Peter Collingbourne | ce24a2a | 2017-04-12 20:02:09 +0000 | [diff] [blame] | 2725 | return error("Invalid record"); |
| 2726 | Comdat::SelectionKind SK = getDecodedComdatSelectionKind(Record[0]); |
Peter Collingbourne | a0f371a | 2017-04-17 17:51:36 +0000 | [diff] [blame] | 2727 | std::string OldFormatName; |
| 2728 | if (!UseStrtab) { |
| 2729 | if (Record.size() < 2) |
| 2730 | return error("Invalid record"); |
| 2731 | unsigned ComdatNameSize = Record[1]; |
| 2732 | OldFormatName.reserve(ComdatNameSize); |
| 2733 | for (unsigned i = 0; i != ComdatNameSize; ++i) |
| 2734 | OldFormatName += (char)Record[2 + i]; |
| 2735 | Name = OldFormatName; |
| 2736 | } |
Peter Collingbourne | ce24a2a | 2017-04-12 20:02:09 +0000 | [diff] [blame] | 2737 | Comdat *C = TheModule->getOrInsertComdat(Name); |
| 2738 | C->setSelectionKind(SK); |
| 2739 | ComdatList.push_back(C); |
| 2740 | return Error::success(); |
| 2741 | } |
| 2742 | |
| 2743 | Error BitcodeReader::parseGlobalVarRecord(ArrayRef<uint64_t> Record) { |
Peter Collingbourne | a0f371a | 2017-04-17 17:51:36 +0000 | [diff] [blame] | 2744 | // v1: [pointer type, isconst, initid, linkage, alignment, section, |
Peter Collingbourne | ce24a2a | 2017-04-12 20:02:09 +0000 | [diff] [blame] | 2745 | // visibility, threadlocal, unnamed_addr, externally_initialized, |
Javed Absar | f3d7904 | 2017-05-11 12:28:08 +0000 | [diff] [blame] | 2746 | // dllstorageclass, comdat, attributes] (name in VST) |
Peter Collingbourne | a0f371a | 2017-04-17 17:51:36 +0000 | [diff] [blame] | 2747 | // v2: [strtab_offset, strtab_size, v1] |
| 2748 | StringRef Name; |
| 2749 | std::tie(Name, Record) = readNameFromStrtab(Record); |
| 2750 | |
Peter Collingbourne | ce24a2a | 2017-04-12 20:02:09 +0000 | [diff] [blame] | 2751 | if (Record.size() < 6) |
| 2752 | return error("Invalid record"); |
| 2753 | Type *Ty = getTypeByID(Record[0]); |
| 2754 | if (!Ty) |
| 2755 | return error("Invalid record"); |
| 2756 | bool isConstant = Record[1] & 1; |
| 2757 | bool explicitType = Record[1] & 2; |
| 2758 | unsigned AddressSpace; |
| 2759 | if (explicitType) { |
| 2760 | AddressSpace = Record[1] >> 2; |
| 2761 | } else { |
| 2762 | if (!Ty->isPointerTy()) |
| 2763 | return error("Invalid type for value"); |
| 2764 | AddressSpace = cast<PointerType>(Ty)->getAddressSpace(); |
| 2765 | Ty = cast<PointerType>(Ty)->getElementType(); |
| 2766 | } |
| 2767 | |
| 2768 | uint64_t RawLinkage = Record[3]; |
| 2769 | GlobalValue::LinkageTypes Linkage = getDecodedLinkage(RawLinkage); |
| 2770 | unsigned Alignment; |
| 2771 | if (Error Err = parseAlignmentValue(Record[4], Alignment)) |
| 2772 | return Err; |
| 2773 | std::string Section; |
| 2774 | if (Record[5]) { |
| 2775 | if (Record[5] - 1 >= SectionTable.size()) |
| 2776 | return error("Invalid ID"); |
| 2777 | Section = SectionTable[Record[5] - 1]; |
| 2778 | } |
| 2779 | GlobalValue::VisibilityTypes Visibility = GlobalValue::DefaultVisibility; |
| 2780 | // Local linkage must have default visibility. |
| 2781 | if (Record.size() > 6 && !GlobalValue::isLocalLinkage(Linkage)) |
| 2782 | // FIXME: Change to an error if non-default in 4.0. |
| 2783 | Visibility = getDecodedVisibility(Record[6]); |
| 2784 | |
| 2785 | GlobalVariable::ThreadLocalMode TLM = GlobalVariable::NotThreadLocal; |
| 2786 | if (Record.size() > 7) |
| 2787 | TLM = getDecodedThreadLocalMode(Record[7]); |
| 2788 | |
| 2789 | GlobalValue::UnnamedAddr UnnamedAddr = GlobalValue::UnnamedAddr::None; |
| 2790 | if (Record.size() > 8) |
| 2791 | UnnamedAddr = getDecodedUnnamedAddrType(Record[8]); |
| 2792 | |
| 2793 | bool ExternallyInitialized = false; |
| 2794 | if (Record.size() > 9) |
| 2795 | ExternallyInitialized = Record[9]; |
| 2796 | |
| 2797 | GlobalVariable *NewGV = |
Peter Collingbourne | a0f371a | 2017-04-17 17:51:36 +0000 | [diff] [blame] | 2798 | new GlobalVariable(*TheModule, Ty, isConstant, Linkage, nullptr, Name, |
Peter Collingbourne | ce24a2a | 2017-04-12 20:02:09 +0000 | [diff] [blame] | 2799 | nullptr, TLM, AddressSpace, ExternallyInitialized); |
| 2800 | NewGV->setAlignment(Alignment); |
| 2801 | if (!Section.empty()) |
| 2802 | NewGV->setSection(Section); |
| 2803 | NewGV->setVisibility(Visibility); |
| 2804 | NewGV->setUnnamedAddr(UnnamedAddr); |
| 2805 | |
| 2806 | if (Record.size() > 10) |
| 2807 | NewGV->setDLLStorageClass(getDecodedDLLStorageClass(Record[10])); |
| 2808 | else |
| 2809 | upgradeDLLImportExportLinkage(NewGV, RawLinkage); |
| 2810 | |
| 2811 | ValueList.push_back(NewGV); |
| 2812 | |
| 2813 | // Remember which value to use for the global initializer. |
| 2814 | if (unsigned InitID = Record[2]) |
| 2815 | GlobalInits.push_back(std::make_pair(NewGV, InitID - 1)); |
| 2816 | |
| 2817 | if (Record.size() > 11) { |
| 2818 | if (unsigned ComdatID = Record[11]) { |
| 2819 | if (ComdatID > ComdatList.size()) |
| 2820 | return error("Invalid global variable comdat ID"); |
| 2821 | NewGV->setComdat(ComdatList[ComdatID - 1]); |
| 2822 | } |
| 2823 | } else if (hasImplicitComdat(RawLinkage)) { |
| 2824 | NewGV->setComdat(reinterpret_cast<Comdat *>(1)); |
| 2825 | } |
Javed Absar | f3d7904 | 2017-05-11 12:28:08 +0000 | [diff] [blame] | 2826 | |
| 2827 | if (Record.size() > 12) { |
| 2828 | auto AS = getAttributes(Record[12]).getFnAttributes(); |
| 2829 | NewGV->setAttributes(AS); |
| 2830 | } |
Peter Collingbourne | ce24a2a | 2017-04-12 20:02:09 +0000 | [diff] [blame] | 2831 | return Error::success(); |
| 2832 | } |
| 2833 | |
| 2834 | Error BitcodeReader::parseFunctionRecord(ArrayRef<uint64_t> Record) { |
Peter Collingbourne | a0f371a | 2017-04-17 17:51:36 +0000 | [diff] [blame] | 2835 | // v1: [type, callingconv, isproto, linkage, paramattr, alignment, section, |
Peter Collingbourne | ce24a2a | 2017-04-12 20:02:09 +0000 | [diff] [blame] | 2836 | // visibility, gc, unnamed_addr, prologuedata, dllstorageclass, comdat, |
Peter Collingbourne | a0f371a | 2017-04-17 17:51:36 +0000 | [diff] [blame] | 2837 | // prefixdata] (name in VST) |
| 2838 | // v2: [strtab_offset, strtab_size, v1] |
| 2839 | StringRef Name; |
| 2840 | std::tie(Name, Record) = readNameFromStrtab(Record); |
| 2841 | |
Peter Collingbourne | ce24a2a | 2017-04-12 20:02:09 +0000 | [diff] [blame] | 2842 | if (Record.size() < 8) |
| 2843 | return error("Invalid record"); |
| 2844 | Type *Ty = getTypeByID(Record[0]); |
| 2845 | if (!Ty) |
| 2846 | return error("Invalid record"); |
| 2847 | if (auto *PTy = dyn_cast<PointerType>(Ty)) |
| 2848 | Ty = PTy->getElementType(); |
| 2849 | auto *FTy = dyn_cast<FunctionType>(Ty); |
| 2850 | if (!FTy) |
| 2851 | return error("Invalid type for value"); |
| 2852 | auto CC = static_cast<CallingConv::ID>(Record[1]); |
| 2853 | if (CC & ~CallingConv::MaxID) |
| 2854 | return error("Invalid calling convention ID"); |
| 2855 | |
| 2856 | Function *Func = |
Peter Collingbourne | a0f371a | 2017-04-17 17:51:36 +0000 | [diff] [blame] | 2857 | Function::Create(FTy, GlobalValue::ExternalLinkage, Name, TheModule); |
Peter Collingbourne | ce24a2a | 2017-04-12 20:02:09 +0000 | [diff] [blame] | 2858 | |
| 2859 | Func->setCallingConv(CC); |
| 2860 | bool isProto = Record[2]; |
| 2861 | uint64_t RawLinkage = Record[3]; |
| 2862 | Func->setLinkage(getDecodedLinkage(RawLinkage)); |
| 2863 | Func->setAttributes(getAttributes(Record[4])); |
| 2864 | |
| 2865 | unsigned Alignment; |
| 2866 | if (Error Err = parseAlignmentValue(Record[5], Alignment)) |
| 2867 | return Err; |
| 2868 | Func->setAlignment(Alignment); |
| 2869 | if (Record[6]) { |
| 2870 | if (Record[6] - 1 >= SectionTable.size()) |
| 2871 | return error("Invalid ID"); |
| 2872 | Func->setSection(SectionTable[Record[6] - 1]); |
| 2873 | } |
| 2874 | // Local linkage must have default visibility. |
| 2875 | if (!Func->hasLocalLinkage()) |
| 2876 | // FIXME: Change to an error if non-default in 4.0. |
| 2877 | Func->setVisibility(getDecodedVisibility(Record[7])); |
| 2878 | if (Record.size() > 8 && Record[8]) { |
| 2879 | if (Record[8] - 1 >= GCTable.size()) |
| 2880 | return error("Invalid ID"); |
| 2881 | Func->setGC(GCTable[Record[8] - 1]); |
| 2882 | } |
| 2883 | GlobalValue::UnnamedAddr UnnamedAddr = GlobalValue::UnnamedAddr::None; |
| 2884 | if (Record.size() > 9) |
| 2885 | UnnamedAddr = getDecodedUnnamedAddrType(Record[9]); |
| 2886 | Func->setUnnamedAddr(UnnamedAddr); |
| 2887 | if (Record.size() > 10 && Record[10] != 0) |
| 2888 | FunctionPrologues.push_back(std::make_pair(Func, Record[10] - 1)); |
| 2889 | |
| 2890 | if (Record.size() > 11) |
| 2891 | Func->setDLLStorageClass(getDecodedDLLStorageClass(Record[11])); |
| 2892 | else |
| 2893 | upgradeDLLImportExportLinkage(Func, RawLinkage); |
| 2894 | |
| 2895 | if (Record.size() > 12) { |
| 2896 | if (unsigned ComdatID = Record[12]) { |
| 2897 | if (ComdatID > ComdatList.size()) |
| 2898 | return error("Invalid function comdat ID"); |
| 2899 | Func->setComdat(ComdatList[ComdatID - 1]); |
| 2900 | } |
| 2901 | } else if (hasImplicitComdat(RawLinkage)) { |
| 2902 | Func->setComdat(reinterpret_cast<Comdat *>(1)); |
| 2903 | } |
| 2904 | |
| 2905 | if (Record.size() > 13 && Record[13] != 0) |
| 2906 | FunctionPrefixes.push_back(std::make_pair(Func, Record[13] - 1)); |
| 2907 | |
| 2908 | if (Record.size() > 14 && Record[14] != 0) |
| 2909 | FunctionPersonalityFns.push_back(std::make_pair(Func, Record[14] - 1)); |
| 2910 | |
| 2911 | ValueList.push_back(Func); |
| 2912 | |
| 2913 | // If this is a function with a body, remember the prototype we are |
| 2914 | // creating now, so that we can match up the body with them later. |
| 2915 | if (!isProto) { |
| 2916 | Func->setIsMaterializable(true); |
| 2917 | FunctionsWithBodies.push_back(Func); |
| 2918 | DeferredFunctionInfo[Func] = 0; |
| 2919 | } |
| 2920 | return Error::success(); |
| 2921 | } |
| 2922 | |
| 2923 | Error BitcodeReader::parseGlobalIndirectSymbolRecord( |
| 2924 | unsigned BitCode, ArrayRef<uint64_t> Record) { |
Peter Collingbourne | a0f371a | 2017-04-17 17:51:36 +0000 | [diff] [blame] | 2925 | // v1 ALIAS_OLD: [alias type, aliasee val#, linkage] (name in VST) |
| 2926 | // v1 ALIAS: [alias type, addrspace, aliasee val#, linkage, visibility, |
| 2927 | // dllstorageclass] (name in VST) |
| 2928 | // v1 IFUNC: [alias type, addrspace, aliasee val#, linkage, |
| 2929 | // visibility, dllstorageclass] (name in VST) |
| 2930 | // v2: [strtab_offset, strtab_size, v1] |
| 2931 | StringRef Name; |
| 2932 | std::tie(Name, Record) = readNameFromStrtab(Record); |
| 2933 | |
Peter Collingbourne | ce24a2a | 2017-04-12 20:02:09 +0000 | [diff] [blame] | 2934 | bool NewRecord = BitCode != bitc::MODULE_CODE_ALIAS_OLD; |
| 2935 | if (Record.size() < (3 + (unsigned)NewRecord)) |
| 2936 | return error("Invalid record"); |
| 2937 | unsigned OpNum = 0; |
| 2938 | Type *Ty = getTypeByID(Record[OpNum++]); |
| 2939 | if (!Ty) |
| 2940 | return error("Invalid record"); |
| 2941 | |
| 2942 | unsigned AddrSpace; |
| 2943 | if (!NewRecord) { |
| 2944 | auto *PTy = dyn_cast<PointerType>(Ty); |
| 2945 | if (!PTy) |
| 2946 | return error("Invalid type for value"); |
| 2947 | Ty = PTy->getElementType(); |
| 2948 | AddrSpace = PTy->getAddressSpace(); |
| 2949 | } else { |
| 2950 | AddrSpace = Record[OpNum++]; |
| 2951 | } |
| 2952 | |
| 2953 | auto Val = Record[OpNum++]; |
| 2954 | auto Linkage = Record[OpNum++]; |
| 2955 | GlobalIndirectSymbol *NewGA; |
| 2956 | if (BitCode == bitc::MODULE_CODE_ALIAS || |
| 2957 | BitCode == bitc::MODULE_CODE_ALIAS_OLD) |
Peter Collingbourne | a0f371a | 2017-04-17 17:51:36 +0000 | [diff] [blame] | 2958 | NewGA = GlobalAlias::create(Ty, AddrSpace, getDecodedLinkage(Linkage), Name, |
Peter Collingbourne | ce24a2a | 2017-04-12 20:02:09 +0000 | [diff] [blame] | 2959 | TheModule); |
| 2960 | else |
Peter Collingbourne | a0f371a | 2017-04-17 17:51:36 +0000 | [diff] [blame] | 2961 | NewGA = GlobalIFunc::create(Ty, AddrSpace, getDecodedLinkage(Linkage), Name, |
Peter Collingbourne | ce24a2a | 2017-04-12 20:02:09 +0000 | [diff] [blame] | 2962 | nullptr, TheModule); |
| 2963 | // Old bitcode files didn't have visibility field. |
| 2964 | // Local linkage must have default visibility. |
| 2965 | if (OpNum != Record.size()) { |
| 2966 | auto VisInd = OpNum++; |
| 2967 | if (!NewGA->hasLocalLinkage()) |
| 2968 | // FIXME: Change to an error if non-default in 4.0. |
| 2969 | NewGA->setVisibility(getDecodedVisibility(Record[VisInd])); |
| 2970 | } |
| 2971 | if (OpNum != Record.size()) |
| 2972 | NewGA->setDLLStorageClass(getDecodedDLLStorageClass(Record[OpNum++])); |
| 2973 | else |
| 2974 | upgradeDLLImportExportLinkage(NewGA, Linkage); |
| 2975 | if (OpNum != Record.size()) |
| 2976 | NewGA->setThreadLocalMode(getDecodedThreadLocalMode(Record[OpNum++])); |
| 2977 | if (OpNum != Record.size()) |
| 2978 | NewGA->setUnnamedAddr(getDecodedUnnamedAddrType(Record[OpNum++])); |
| 2979 | ValueList.push_back(NewGA); |
| 2980 | IndirectSymbolInits.push_back(std::make_pair(NewGA, Val)); |
| 2981 | return Error::success(); |
| 2982 | } |
| 2983 | |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 2984 | Error BitcodeReader::parseModule(uint64_t ResumeBit, |
| 2985 | bool ShouldLazyLoadMetadata) { |
Teresa Johnson | 1493ad9 | 2015-10-10 14:18:36 +0000 | [diff] [blame] | 2986 | if (ResumeBit) |
| 2987 | Stream.JumpToBit(ResumeBit); |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 2988 | else if (Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID)) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2989 | return error("Invalid record"); |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 2990 | |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 2991 | SmallVector<uint64_t, 64> Record; |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 2992 | |
| 2993 | // Read all the records for this module. |
Eugene Zelenko | 1804a77 | 2016-08-25 00:45:04 +0000 | [diff] [blame] | 2994 | while (true) { |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 2995 | BitstreamEntry Entry = Stream.advance(); |
Joe Abbey | 97b7a17 | 2013-02-06 22:14:06 +0000 | [diff] [blame] | 2996 | |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 2997 | switch (Entry.Kind) { |
| 2998 | case BitstreamEntry::Error: |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 2999 | return error("Malformed block"); |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 3000 | case BitstreamEntry::EndBlock: |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3001 | return globalCleanup(); |
Joe Abbey | 97b7a17 | 2013-02-06 22:14:06 +0000 | [diff] [blame] | 3002 | |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 3003 | case BitstreamEntry::SubBlock: |
| 3004 | switch (Entry.ID) { |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 3005 | default: // Skip unknown content. |
| 3006 | if (Stream.SkipBlock()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3007 | return error("Invalid record"); |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 3008 | break; |
Chris Lattner | 6eeea5d | 2007-05-05 18:57:30 +0000 | [diff] [blame] | 3009 | case bitc::BLOCKINFO_BLOCK_ID: |
Peter Collingbourne | 939c7d9 | 2016-11-08 04:16:57 +0000 | [diff] [blame] | 3010 | if (readBlockInfo()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3011 | return error("Malformed block"); |
Chris Lattner | 6eeea5d | 2007-05-05 18:57:30 +0000 | [diff] [blame] | 3012 | break; |
Chris Lattner | fee5a37 | 2007-05-04 03:30:17 +0000 | [diff] [blame] | 3013 | case bitc::PARAMATTR_BLOCK_ID: |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 3014 | if (Error Err = parseAttributeBlock()) |
| 3015 | return Err; |
Chris Lattner | fee5a37 | 2007-05-04 03:30:17 +0000 | [diff] [blame] | 3016 | break; |
Bill Wendling | ba62933 | 2013-02-10 23:24:25 +0000 | [diff] [blame] | 3017 | case bitc::PARAMATTR_GROUP_BLOCK_ID: |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 3018 | if (Error Err = parseAttributeGroupBlock()) |
| 3019 | return Err; |
Bill Wendling | ba62933 | 2013-02-10 23:24:25 +0000 | [diff] [blame] | 3020 | break; |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 3021 | case bitc::TYPE_BLOCK_ID_NEW: |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 3022 | if (Error Err = parseTypeTable()) |
| 3023 | return Err; |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 3024 | break; |
Chris Lattner | ccaa448 | 2007-04-23 21:26:05 +0000 | [diff] [blame] | 3025 | case bitc::VALUE_SYMTAB_BLOCK_ID: |
Teresa Johnson | ff642b9 | 2015-09-17 20:12:00 +0000 | [diff] [blame] | 3026 | if (!SeenValueSymbolTable) { |
| 3027 | // Either this is an old form VST without function index and an |
| 3028 | // associated VST forward declaration record (which would have caused |
| 3029 | // the VST to be jumped to and parsed before it was encountered |
| 3030 | // normally in the stream), or there were no function blocks to |
| 3031 | // trigger an earlier parsing of the VST. |
| 3032 | assert(VSTOffset == 0 || FunctionsWithBodies.empty()); |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 3033 | if (Error Err = parseValueSymbolTable()) |
| 3034 | return Err; |
Teresa Johnson | ff642b9 | 2015-09-17 20:12:00 +0000 | [diff] [blame] | 3035 | SeenValueSymbolTable = true; |
| 3036 | } else { |
| 3037 | // We must have had a VST forward declaration record, which caused |
| 3038 | // the parser to jump to and parse the VST earlier. |
| 3039 | assert(VSTOffset > 0); |
| 3040 | if (Stream.SkipBlock()) |
| 3041 | return error("Invalid record"); |
| 3042 | } |
Chris Lattner | ccaa448 | 2007-04-23 21:26:05 +0000 | [diff] [blame] | 3043 | break; |
Chris Lattner | fbc1d33 | 2007-04-24 03:30:34 +0000 | [diff] [blame] | 3044 | case bitc::CONSTANTS_BLOCK_ID: |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 3045 | if (Error Err = parseConstants()) |
| 3046 | return Err; |
| 3047 | if (Error Err = resolveGlobalAndIndirectSymbolInits()) |
| 3048 | return Err; |
Chris Lattner | fbc1d33 | 2007-04-24 03:30:34 +0000 | [diff] [blame] | 3049 | break; |
Devang Patel | 7428d8a | 2009-07-22 17:43:22 +0000 | [diff] [blame] | 3050 | case bitc::METADATA_BLOCK_ID: |
Mehdi Amini | bf2090e | 2016-12-12 19:23:39 +0000 | [diff] [blame] | 3051 | if (ShouldLazyLoadMetadata) { |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 3052 | if (Error Err = rememberAndSkipMetadata()) |
| 3053 | return Err; |
Manman Ren | 4a9b0eb | 2015-03-13 19:24:30 +0000 | [diff] [blame] | 3054 | break; |
| 3055 | } |
| 3056 | assert(DeferredMetadataInfo.empty() && "Unexpected deferred metadata"); |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 3057 | if (Error Err = MDLoader->parseModuleMetadata()) |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 3058 | return Err; |
Devang Patel | 7428d8a | 2009-07-22 17:43:22 +0000 | [diff] [blame] | 3059 | break; |
Teresa Johnson | 1254507 | 2015-11-15 02:00:09 +0000 | [diff] [blame] | 3060 | case bitc::METADATA_KIND_BLOCK_ID: |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 3061 | if (Error Err = MDLoader->parseMetadataKinds()) |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 3062 | return Err; |
Teresa Johnson | 1254507 | 2015-11-15 02:00:09 +0000 | [diff] [blame] | 3063 | break; |
Chris Lattner | 51ffe7c | 2007-05-01 04:59:48 +0000 | [diff] [blame] | 3064 | case bitc::FUNCTION_BLOCK_ID: |
Mehdi Amini | 466a64e | 2016-08-13 23:39:14 +0000 | [diff] [blame] | 3065 | // If this is the first function body we've seen, reverse the |
| 3066 | // FunctionsWithBodies list. |
| 3067 | if (!SeenFirstFunctionBody) { |
| 3068 | std::reverse(FunctionsWithBodies.begin(), FunctionsWithBodies.end()); |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 3069 | if (Error Err = globalCleanup()) |
| 3070 | return Err; |
Mehdi Amini | 466a64e | 2016-08-13 23:39:14 +0000 | [diff] [blame] | 3071 | SeenFirstFunctionBody = true; |
| 3072 | } |
| 3073 | |
Teresa Johnson | ff642b9 | 2015-09-17 20:12:00 +0000 | [diff] [blame] | 3074 | if (VSTOffset > 0) { |
| 3075 | // If we have a VST forward declaration record, make sure we |
| 3076 | // parse the VST now if we haven't already. It is needed to |
| 3077 | // set up the DeferredFunctionInfo vector for lazy reading. |
| 3078 | if (!SeenValueSymbolTable) { |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 3079 | if (Error Err = BitcodeReader::parseValueSymbolTable(VSTOffset)) |
| 3080 | return Err; |
Teresa Johnson | ff642b9 | 2015-09-17 20:12:00 +0000 | [diff] [blame] | 3081 | SeenValueSymbolTable = true; |
Teresa Johnson | 1493ad9 | 2015-10-10 14:18:36 +0000 | [diff] [blame] | 3082 | // Fall through so that we record the NextUnreadBit below. |
| 3083 | // This is necessary in case we have an anonymous function that |
| 3084 | // is later materialized. Since it will not have a VST entry we |
| 3085 | // need to fall back to the lazy parse to find its offset. |
Teresa Johnson | ff642b9 | 2015-09-17 20:12:00 +0000 | [diff] [blame] | 3086 | } else { |
| 3087 | // If we have a VST forward declaration record, but have already |
| 3088 | // parsed the VST (just above, when the first function body was |
| 3089 | // encountered here), then we are resuming the parse after |
Teresa Johnson | 1493ad9 | 2015-10-10 14:18:36 +0000 | [diff] [blame] | 3090 | // materializing functions. The ResumeBit points to the |
| 3091 | // start of the last function block recorded in the |
| 3092 | // DeferredFunctionInfo map. Skip it. |
Teresa Johnson | ff642b9 | 2015-09-17 20:12:00 +0000 | [diff] [blame] | 3093 | if (Stream.SkipBlock()) |
| 3094 | return error("Invalid record"); |
| 3095 | continue; |
| 3096 | } |
| 3097 | } |
| 3098 | |
| 3099 | // Support older bitcode files that did not have the function |
Teresa Johnson | 1493ad9 | 2015-10-10 14:18:36 +0000 | [diff] [blame] | 3100 | // index in the VST, nor a VST forward declaration record, as |
| 3101 | // well as anonymous functions that do not have VST entries. |
Teresa Johnson | ff642b9 | 2015-09-17 20:12:00 +0000 | [diff] [blame] | 3102 | // Build the DeferredFunctionInfo vector on the fly. |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 3103 | if (Error Err = rememberAndSkipFunctionBody()) |
| 3104 | return Err; |
Teresa Johnson | 1493ad9 | 2015-10-10 14:18:36 +0000 | [diff] [blame] | 3105 | |
Rafael Espindola | 1c863ca | 2015-06-22 18:06:15 +0000 | [diff] [blame] | 3106 | // Suspend parsing when we reach the function bodies. Subsequent |
| 3107 | // materialization calls will resume it when necessary. If the bitcode |
| 3108 | // file is old, the symbol table will be at the end instead and will not |
| 3109 | // have been seen yet. In this case, just finish the parse now. |
| 3110 | if (SeenValueSymbolTable) { |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 3111 | NextUnreadBit = Stream.GetCurrentBitNo(); |
Mehdi Amini | a71002e | 2016-08-14 00:01:27 +0000 | [diff] [blame] | 3112 | // After the VST has been parsed, we need to make sure intrinsic name |
| 3113 | // are auto-upgraded. |
| 3114 | return globalCleanup(); |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 3115 | } |
Chris Lattner | 51ffe7c | 2007-05-01 04:59:48 +0000 | [diff] [blame] | 3116 | break; |
Chad Rosier | ca2567b | 2011-12-07 21:44:12 +0000 | [diff] [blame] | 3117 | case bitc::USELIST_BLOCK_ID: |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 3118 | if (Error Err = parseUseLists()) |
| 3119 | return Err; |
Chad Rosier | ca2567b | 2011-12-07 21:44:12 +0000 | [diff] [blame] | 3120 | break; |
Sanjoy Das | b513a9f | 2015-09-24 23:34:52 +0000 | [diff] [blame] | 3121 | case bitc::OPERAND_BUNDLE_TAGS_BLOCK_ID: |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 3122 | if (Error Err = parseOperandBundleTags()) |
| 3123 | return Err; |
Sanjoy Das | b513a9f | 2015-09-24 23:34:52 +0000 | [diff] [blame] | 3124 | break; |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 3125 | } |
| 3126 | continue; |
Joe Abbey | 97b7a17 | 2013-02-06 22:14:06 +0000 | [diff] [blame] | 3127 | |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 3128 | case BitstreamEntry::Record: |
| 3129 | // The interesting case. |
| 3130 | break; |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 3131 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3132 | |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 3133 | // Read a record. |
David Blaikie | 6a51dbd | 2015-09-17 22:18:59 +0000 | [diff] [blame] | 3134 | auto BitCode = Stream.readRecord(Entry.ID, Record); |
| 3135 | switch (BitCode) { |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 3136 | default: break; // Default behavior, ignore unknown content. |
Peter Collingbourne | ce24a2a | 2017-04-12 20:02:09 +0000 | [diff] [blame] | 3137 | case bitc::MODULE_CODE_VERSION: { |
| 3138 | Expected<unsigned> VersionOrErr = parseVersionRecord(Record); |
| 3139 | if (!VersionOrErr) |
| 3140 | return VersionOrErr.takeError(); |
| 3141 | UseRelativeIDs = *VersionOrErr >= 1; |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 3142 | break; |
Jan Wen Voung | afaced0 | 2012-10-11 20:20:40 +0000 | [diff] [blame] | 3143 | } |
Chris Lattner | e14cb88 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 3144 | case bitc::MODULE_CODE_TRIPLE: { // TRIPLE: [strchr x N] |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 3145 | std::string S; |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3146 | if (convertToString(Record, 0, S)) |
| 3147 | return error("Invalid record"); |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 3148 | TheModule->setTargetTriple(S); |
| 3149 | break; |
| 3150 | } |
Chris Lattner | e14cb88 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 3151 | case bitc::MODULE_CODE_DATALAYOUT: { // DATALAYOUT: [strchr x N] |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 3152 | std::string S; |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3153 | if (convertToString(Record, 0, S)) |
| 3154 | return error("Invalid record"); |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 3155 | TheModule->setDataLayout(S); |
| 3156 | break; |
| 3157 | } |
Chris Lattner | e14cb88 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 3158 | case bitc::MODULE_CODE_ASM: { // ASM: [strchr x N] |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 3159 | std::string S; |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3160 | if (convertToString(Record, 0, S)) |
| 3161 | return error("Invalid record"); |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 3162 | TheModule->setModuleInlineAsm(S); |
| 3163 | break; |
| 3164 | } |
Bill Wendling | 706d3d6 | 2012-11-28 08:41:48 +0000 | [diff] [blame] | 3165 | case bitc::MODULE_CODE_DEPLIB: { // DEPLIB: [strchr x N] |
| 3166 | // FIXME: Remove in 4.0. |
| 3167 | std::string S; |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3168 | if (convertToString(Record, 0, S)) |
| 3169 | return error("Invalid record"); |
Bill Wendling | 706d3d6 | 2012-11-28 08:41:48 +0000 | [diff] [blame] | 3170 | // Ignore value. |
| 3171 | break; |
| 3172 | } |
Chris Lattner | e14cb88 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 3173 | case bitc::MODULE_CODE_SECTIONNAME: { // SECTIONNAME: [strchr x N] |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 3174 | std::string S; |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3175 | if (convertToString(Record, 0, S)) |
| 3176 | return error("Invalid record"); |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 3177 | SectionTable.push_back(S); |
| 3178 | break; |
| 3179 | } |
Gordon Henriksen | d930f91 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 3180 | case bitc::MODULE_CODE_GCNAME: { // SECTIONNAME: [strchr x N] |
Gordon Henriksen | 71183b6 | 2007-12-10 03:18:06 +0000 | [diff] [blame] | 3181 | std::string S; |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3182 | if (convertToString(Record, 0, S)) |
| 3183 | return error("Invalid record"); |
Gordon Henriksen | d930f91 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 3184 | GCTable.push_back(S); |
Gordon Henriksen | 71183b6 | 2007-12-10 03:18:06 +0000 | [diff] [blame] | 3185 | break; |
| 3186 | } |
Peter Collingbourne | ce24a2a | 2017-04-12 20:02:09 +0000 | [diff] [blame] | 3187 | case bitc::MODULE_CODE_COMDAT: { |
| 3188 | if (Error Err = parseComdatRecord(Record)) |
| 3189 | return Err; |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 3190 | break; |
| 3191 | } |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 3192 | case bitc::MODULE_CODE_GLOBALVAR: { |
Peter Collingbourne | ce24a2a | 2017-04-12 20:02:09 +0000 | [diff] [blame] | 3193 | if (Error Err = parseGlobalVarRecord(Record)) |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 3194 | return Err; |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 3195 | break; |
| 3196 | } |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 3197 | case bitc::MODULE_CODE_FUNCTION: { |
Peter Collingbourne | ce24a2a | 2017-04-12 20:02:09 +0000 | [diff] [blame] | 3198 | if (Error Err = parseFunctionRecord(Record)) |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 3199 | return Err; |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 3200 | break; |
| 3201 | } |
Dmitry Polukhin | a1feff7 | 2016-04-07 12:32:19 +0000 | [diff] [blame] | 3202 | case bitc::MODULE_CODE_IFUNC: |
David Blaikie | 6a51dbd | 2015-09-17 22:18:59 +0000 | [diff] [blame] | 3203 | case bitc::MODULE_CODE_ALIAS: |
| 3204 | case bitc::MODULE_CODE_ALIAS_OLD: { |
Peter Collingbourne | ce24a2a | 2017-04-12 20:02:09 +0000 | [diff] [blame] | 3205 | if (Error Err = parseGlobalIndirectSymbolRecord(BitCode, Record)) |
| 3206 | return Err; |
Chris Lattner | 44c1707 | 2007-04-26 02:46:40 +0000 | [diff] [blame] | 3207 | break; |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 3208 | } |
Teresa Johnson | ff642b9 | 2015-09-17 20:12:00 +0000 | [diff] [blame] | 3209 | /// MODULE_CODE_VSTOFFSET: [offset] |
| 3210 | case bitc::MODULE_CODE_VSTOFFSET: |
| 3211 | if (Record.size() < 1) |
| 3212 | return error("Invalid record"); |
Peter Collingbourne | bfcf980 | 2016-11-29 02:27:04 +0000 | [diff] [blame] | 3213 | // Note that we subtract 1 here because the offset is relative to one word |
| 3214 | // before the start of the identification or module block, which was |
| 3215 | // historically always the start of the regular bitcode header. |
| 3216 | VSTOffset = Record[0] - 1; |
Teresa Johnson | ff642b9 | 2015-09-17 20:12:00 +0000 | [diff] [blame] | 3217 | break; |
Teresa Johnson | e1164de | 2016-02-10 21:55:02 +0000 | [diff] [blame] | 3218 | /// MODULE_CODE_SOURCE_FILENAME: [namechar x N] |
| 3219 | case bitc::MODULE_CODE_SOURCE_FILENAME: |
| 3220 | SmallString<128> ValueName; |
| 3221 | if (convertToString(Record, 0, ValueName)) |
| 3222 | return error("Invalid record"); |
| 3223 | TheModule->setSourceFileName(ValueName); |
| 3224 | break; |
Chris Lattner | 831d420 | 2007-04-26 03:27:58 +0000 | [diff] [blame] | 3225 | } |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 3226 | Record.clear(); |
| 3227 | } |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 3228 | } |
| 3229 | |
Teresa Johnson | a61f5e3 | 2016-12-16 21:25:01 +0000 | [diff] [blame] | 3230 | Error BitcodeReader::parseBitcodeInto(Module *M, bool ShouldLazyLoadMetadata, |
| 3231 | bool IsImporting) { |
Rafael Espindola | c6afe0d | 2015-06-16 20:03:39 +0000 | [diff] [blame] | 3232 | TheModule = M; |
Teresa Johnson | a61f5e3 | 2016-12-16 21:25:01 +0000 | [diff] [blame] | 3233 | MDLoader = MetadataLoader(Stream, *M, ValueList, IsImporting, |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 3234 | [&](unsigned ID) { return getTypeByID(ID); }); |
Peter Collingbourne | 7a74803 | 2016-11-16 21:44:45 +0000 | [diff] [blame] | 3235 | return parseModule(0, ShouldLazyLoadMetadata); |
Chris Lattner | 1314b99 | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 3236 | } |
Chris Lattner | 6694f60 | 2007-04-29 07:54:31 +0000 | [diff] [blame] | 3237 | |
Chris Lattner | 51ffe7c | 2007-05-01 04:59:48 +0000 | [diff] [blame] | 3238 | |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 3239 | Error BitcodeReader::typeCheckLoadStoreInst(Type *ValType, Type *PtrType) { |
Filipe Cabecinhas | 11bb849 | 2015-05-18 21:48:55 +0000 | [diff] [blame] | 3240 | if (!isa<PointerType>(PtrType)) |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 3241 | return error("Load/Store operand is not a pointer type"); |
Filipe Cabecinhas | 11bb849 | 2015-05-18 21:48:55 +0000 | [diff] [blame] | 3242 | Type *ElemType = cast<PointerType>(PtrType)->getElementType(); |
| 3243 | |
| 3244 | if (ValType && ValType != ElemType) |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 3245 | return error("Explicit load/store type does not match pointee " |
| 3246 | "type of pointer operand"); |
Filipe Cabecinhas | 11bb849 | 2015-05-18 21:48:55 +0000 | [diff] [blame] | 3247 | if (!PointerType::isLoadableOrStorableType(ElemType)) |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 3248 | return error("Cannot load/store from pointer"); |
| 3249 | return Error::success(); |
Filipe Cabecinhas | 11bb849 | 2015-05-18 21:48:55 +0000 | [diff] [blame] | 3250 | } |
| 3251 | |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3252 | /// Lazily parse the specified function body block. |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 3253 | Error BitcodeReader::parseFunctionBody(Function *F) { |
Chris Lattner | 982ec1e | 2007-05-05 00:17:00 +0000 | [diff] [blame] | 3254 | if (Stream.EnterSubBlock(bitc::FUNCTION_BLOCK_ID)) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3255 | return error("Invalid record"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3256 | |
Duncan P. N. Exon Smith | 8742de9 | 2016-04-02 14:55:01 +0000 | [diff] [blame] | 3257 | // Unexpected unresolved metadata when parsing function. |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 3258 | if (MDLoader->hasFwdRefs()) |
Duncan P. N. Exon Smith | 8742de9 | 2016-04-02 14:55:01 +0000 | [diff] [blame] | 3259 | return error("Invalid function metadata: incoming forward references"); |
| 3260 | |
Nick Lewycky | a72e1af | 2010-02-25 08:30:17 +0000 | [diff] [blame] | 3261 | InstructionList.clear(); |
Chris Lattner | 85b7b40 | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 3262 | unsigned ModuleValueListSize = ValueList.size(); |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 3263 | unsigned ModuleMDLoaderSize = MDLoader->size(); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3264 | |
Chris Lattner | 85b7b40 | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 3265 | // Add all the function arguments to the value table. |
Duncan P. N. Exon Smith | fb1743a3 | 2015-10-13 16:48:55 +0000 | [diff] [blame] | 3266 | for (Argument &I : F->args()) |
| 3267 | ValueList.push_back(&I); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3268 | |
Chris Lattner | 8393055 | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 3269 | unsigned NextValueNo = ValueList.size(); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 3270 | BasicBlock *CurBB = nullptr; |
Chris Lattner | e53603e | 2007-05-02 04:27:25 +0000 | [diff] [blame] | 3271 | unsigned CurBBNo = 0; |
| 3272 | |
Chris Lattner | 07d09ed | 2010-04-03 02:17:50 +0000 | [diff] [blame] | 3273 | DebugLoc LastLoc; |
Duncan P. N. Exon Smith | 52d0f16 | 2015-01-09 02:51:45 +0000 | [diff] [blame] | 3274 | auto getLastInstruction = [&]() -> Instruction * { |
| 3275 | if (CurBB && !CurBB->empty()) |
| 3276 | return &CurBB->back(); |
| 3277 | else if (CurBBNo && FunctionBBs[CurBBNo - 1] && |
| 3278 | !FunctionBBs[CurBBNo - 1]->empty()) |
| 3279 | return &FunctionBBs[CurBBNo - 1]->back(); |
| 3280 | return nullptr; |
| 3281 | }; |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 3282 | |
Sanjoy Das | b513a9f | 2015-09-24 23:34:52 +0000 | [diff] [blame] | 3283 | std::vector<OperandBundleDef> OperandBundles; |
| 3284 | |
Chris Lattner | 85b7b40 | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 3285 | // Read all the records. |
| 3286 | SmallVector<uint64_t, 64> Record; |
Eugene Zelenko | 1804a77 | 2016-08-25 00:45:04 +0000 | [diff] [blame] | 3287 | |
| 3288 | while (true) { |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 3289 | BitstreamEntry Entry = Stream.advance(); |
Joe Abbey | 97b7a17 | 2013-02-06 22:14:06 +0000 | [diff] [blame] | 3290 | |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 3291 | switch (Entry.Kind) { |
| 3292 | case BitstreamEntry::Error: |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3293 | return error("Malformed block"); |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 3294 | case BitstreamEntry::EndBlock: |
| 3295 | goto OutOfRecordLoop; |
Joe Abbey | 97b7a17 | 2013-02-06 22:14:06 +0000 | [diff] [blame] | 3296 | |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 3297 | case BitstreamEntry::SubBlock: |
| 3298 | switch (Entry.ID) { |
Chris Lattner | 85b7b40 | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 3299 | default: // Skip unknown content. |
| 3300 | if (Stream.SkipBlock()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3301 | return error("Invalid record"); |
Chris Lattner | 85b7b40 | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 3302 | break; |
| 3303 | case bitc::CONSTANTS_BLOCK_ID: |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 3304 | if (Error Err = parseConstants()) |
| 3305 | return Err; |
Chris Lattner | 8393055 | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 3306 | NextValueNo = ValueList.size(); |
Chris Lattner | 85b7b40 | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 3307 | break; |
| 3308 | case bitc::VALUE_SYMTAB_BLOCK_ID: |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 3309 | if (Error Err = parseValueSymbolTable()) |
| 3310 | return Err; |
Chris Lattner | 85b7b40 | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 3311 | break; |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 3312 | case bitc::METADATA_ATTACHMENT_ID: |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 3313 | if (Error Err = MDLoader->parseMetadataAttachment(*F, InstructionList)) |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 3314 | return Err; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3315 | break; |
Victor Hernandez | 108d3ac | 2010-01-13 19:34:08 +0000 | [diff] [blame] | 3316 | case bitc::METADATA_BLOCK_ID: |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 3317 | assert(DeferredMetadataInfo.empty() && |
| 3318 | "Must read all module-level metadata before function-level"); |
| 3319 | if (Error Err = MDLoader->parseFunctionMetadata()) |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 3320 | return Err; |
Victor Hernandez | 108d3ac | 2010-01-13 19:34:08 +0000 | [diff] [blame] | 3321 | break; |
Duncan P. N. Exon Smith | 1f66c85 | 2014-07-28 21:19:41 +0000 | [diff] [blame] | 3322 | case bitc::USELIST_BLOCK_ID: |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 3323 | if (Error Err = parseUseLists()) |
| 3324 | return Err; |
Duncan P. N. Exon Smith | 1f66c85 | 2014-07-28 21:19:41 +0000 | [diff] [blame] | 3325 | break; |
Chris Lattner | 85b7b40 | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 3326 | } |
| 3327 | continue; |
Joe Abbey | 97b7a17 | 2013-02-06 22:14:06 +0000 | [diff] [blame] | 3328 | |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 3329 | case BitstreamEntry::Record: |
| 3330 | // The interesting case. |
| 3331 | break; |
Chris Lattner | 85b7b40 | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 3332 | } |
Joe Abbey | 97b7a17 | 2013-02-06 22:14:06 +0000 | [diff] [blame] | 3333 | |
Chris Lattner | 85b7b40 | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 3334 | // Read a record. |
| 3335 | Record.clear(); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 3336 | Instruction *I = nullptr; |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 3337 | unsigned BitCode = Stream.readRecord(Entry.ID, Record); |
Dan Gohman | 0ebd696 | 2009-07-20 21:19:07 +0000 | [diff] [blame] | 3338 | switch (BitCode) { |
Chris Lattner | 8393055 | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 3339 | default: // Default behavior: reject |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3340 | return error("Invalid value"); |
Duncan P. N. Exon Smith | 00f20ac | 2014-08-01 21:51:52 +0000 | [diff] [blame] | 3341 | case bitc::FUNC_CODE_DECLAREBLOCKS: { // DECLAREBLOCKS: [nblocks] |
Chris Lattner | 8393055 | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 3342 | if (Record.size() < 1 || Record[0] == 0) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3343 | return error("Invalid record"); |
Chris Lattner | 85b7b40 | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 3344 | // Create all the basic blocks for the function. |
Chris Lattner | 6ce15cb | 2007-05-03 22:09:51 +0000 | [diff] [blame] | 3345 | FunctionBBs.resize(Record[0]); |
Duncan P. N. Exon Smith | 00f20ac | 2014-08-01 21:51:52 +0000 | [diff] [blame] | 3346 | |
| 3347 | // See if anything took the address of blocks in this function. |
| 3348 | auto BBFRI = BasicBlockFwdRefs.find(F); |
| 3349 | if (BBFRI == BasicBlockFwdRefs.end()) { |
| 3350 | for (unsigned i = 0, e = FunctionBBs.size(); i != e; ++i) |
| 3351 | FunctionBBs[i] = BasicBlock::Create(Context, "", F); |
| 3352 | } else { |
| 3353 | auto &BBRefs = BBFRI->second; |
Duncan P. N. Exon Smith | 5a5fd7b | 2014-08-16 01:54:37 +0000 | [diff] [blame] | 3354 | // Check for invalid basic block references. |
| 3355 | if (BBRefs.size() > FunctionBBs.size()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3356 | return error("Invalid ID"); |
Duncan P. N. Exon Smith | 5a5fd7b | 2014-08-16 01:54:37 +0000 | [diff] [blame] | 3357 | assert(!BBRefs.empty() && "Unexpected empty array"); |
| 3358 | assert(!BBRefs.front() && "Invalid reference to entry block"); |
| 3359 | for (unsigned I = 0, E = FunctionBBs.size(), RE = BBRefs.size(); I != E; |
| 3360 | ++I) |
| 3361 | if (I < RE && BBRefs[I]) { |
| 3362 | BBRefs[I]->insertInto(F); |
| 3363 | FunctionBBs[I] = BBRefs[I]; |
Duncan P. N. Exon Smith | 00f20ac | 2014-08-01 21:51:52 +0000 | [diff] [blame] | 3364 | } else { |
| 3365 | FunctionBBs[I] = BasicBlock::Create(Context, "", F); |
| 3366 | } |
Duncan P. N. Exon Smith | 00f20ac | 2014-08-01 21:51:52 +0000 | [diff] [blame] | 3367 | |
| 3368 | // Erase from the table. |
| 3369 | BasicBlockFwdRefs.erase(BBFRI); |
| 3370 | } |
| 3371 | |
Chris Lattner | 8393055 | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 3372 | CurBB = FunctionBBs[0]; |
| 3373 | continue; |
Duncan P. N. Exon Smith | 00f20ac | 2014-08-01 21:51:52 +0000 | [diff] [blame] | 3374 | } |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 3375 | |
Chris Lattner | 07d09ed | 2010-04-03 02:17:50 +0000 | [diff] [blame] | 3376 | case bitc::FUNC_CODE_DEBUG_LOC_AGAIN: // DEBUG_LOC_AGAIN |
| 3377 | // This record indicates that the last instruction is at the same |
| 3378 | // location as the previous instruction with a location. |
Duncan P. N. Exon Smith | 52d0f16 | 2015-01-09 02:51:45 +0000 | [diff] [blame] | 3379 | I = getLastInstruction(); |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 3380 | |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 3381 | if (!I) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3382 | return error("Invalid record"); |
Chris Lattner | 07d09ed | 2010-04-03 02:17:50 +0000 | [diff] [blame] | 3383 | I->setDebugLoc(LastLoc); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 3384 | I = nullptr; |
Chris Lattner | 07d09ed | 2010-04-03 02:17:50 +0000 | [diff] [blame] | 3385 | continue; |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 3386 | |
Duncan P. N. Exon Smith | 9ed1966 | 2015-01-09 17:53:27 +0000 | [diff] [blame] | 3387 | case bitc::FUNC_CODE_DEBUG_LOC: { // DEBUG_LOC: [line, col, scope, ia] |
Duncan P. N. Exon Smith | 52d0f16 | 2015-01-09 02:51:45 +0000 | [diff] [blame] | 3388 | I = getLastInstruction(); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 3389 | if (!I || Record.size() < 4) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3390 | return error("Invalid record"); |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 3391 | |
Chris Lattner | 07d09ed | 2010-04-03 02:17:50 +0000 | [diff] [blame] | 3392 | unsigned Line = Record[0], Col = Record[1]; |
| 3393 | unsigned ScopeID = Record[2], IAID = Record[3]; |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 3394 | |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 3395 | MDNode *Scope = nullptr, *IA = nullptr; |
Justin Bogner | ae341c6 | 2016-03-17 20:12:06 +0000 | [diff] [blame] | 3396 | if (ScopeID) { |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 3397 | Scope = MDLoader->getMDNodeFwdRefOrNull(ScopeID - 1); |
Justin Bogner | ae341c6 | 2016-03-17 20:12:06 +0000 | [diff] [blame] | 3398 | if (!Scope) |
| 3399 | return error("Invalid record"); |
| 3400 | } |
| 3401 | if (IAID) { |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 3402 | IA = MDLoader->getMDNodeFwdRefOrNull(IAID - 1); |
Justin Bogner | ae341c6 | 2016-03-17 20:12:06 +0000 | [diff] [blame] | 3403 | if (!IA) |
| 3404 | return error("Invalid record"); |
| 3405 | } |
Chris Lattner | 07d09ed | 2010-04-03 02:17:50 +0000 | [diff] [blame] | 3406 | LastLoc = DebugLoc::get(Line, Col, Scope, IA); |
| 3407 | I->setDebugLoc(LastLoc); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 3408 | I = nullptr; |
Chris Lattner | 07d09ed | 2010-04-03 02:17:50 +0000 | [diff] [blame] | 3409 | continue; |
| 3410 | } |
| 3411 | |
Chris Lattner | e9759c2 | 2007-05-06 00:21:25 +0000 | [diff] [blame] | 3412 | case bitc::FUNC_CODE_INST_BINOP: { // BINOP: [opval, ty, opval, opcode] |
| 3413 | unsigned OpNum = 0; |
| 3414 | Value *LHS, *RHS; |
| 3415 | if (getValueTypePair(Record, OpNum, NextValueNo, LHS) || |
Jan Wen Voung | afaced0 | 2012-10-11 20:20:40 +0000 | [diff] [blame] | 3416 | popValue(Record, OpNum, NextValueNo, LHS->getType(), RHS) || |
Dan Gohman | 0ebd696 | 2009-07-20 21:19:07 +0000 | [diff] [blame] | 3417 | OpNum+1 > Record.size()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3418 | return error("Invalid record"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3419 | |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3420 | int Opc = getDecodedBinaryOpcode(Record[OpNum++], LHS->getType()); |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 3421 | if (Opc == -1) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3422 | return error("Invalid record"); |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3423 | I = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS); |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 3424 | InstructionList.push_back(I); |
Dan Gohman | 1b84908 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 3425 | if (OpNum < Record.size()) { |
| 3426 | if (Opc == Instruction::Add || |
| 3427 | Opc == Instruction::Sub || |
Chris Lattner | a676c0f | 2011-02-07 16:40:21 +0000 | [diff] [blame] | 3428 | Opc == Instruction::Mul || |
| 3429 | Opc == Instruction::Shl) { |
Dan Gohman | 00f4747 | 2010-01-25 21:55:39 +0000 | [diff] [blame] | 3430 | if (Record[OpNum] & (1 << bitc::OBO_NO_SIGNED_WRAP)) |
Dan Gohman | 1b84908 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 3431 | cast<BinaryOperator>(I)->setHasNoSignedWrap(true); |
Dan Gohman | 00f4747 | 2010-01-25 21:55:39 +0000 | [diff] [blame] | 3432 | if (Record[OpNum] & (1 << bitc::OBO_NO_UNSIGNED_WRAP)) |
Dan Gohman | 1b84908 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 3433 | cast<BinaryOperator>(I)->setHasNoUnsignedWrap(true); |
Chris Lattner | 35315d0 | 2011-02-06 21:44:57 +0000 | [diff] [blame] | 3434 | } else if (Opc == Instruction::SDiv || |
Chris Lattner | a676c0f | 2011-02-07 16:40:21 +0000 | [diff] [blame] | 3435 | Opc == Instruction::UDiv || |
| 3436 | Opc == Instruction::LShr || |
| 3437 | Opc == Instruction::AShr) { |
Chris Lattner | 35315d0 | 2011-02-06 21:44:57 +0000 | [diff] [blame] | 3438 | if (Record[OpNum] & (1 << bitc::PEO_EXACT)) |
Dan Gohman | 1b84908 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 3439 | cast<BinaryOperator>(I)->setIsExact(true); |
Michael Ilseman | 9978d7e | 2012-11-27 00:43:38 +0000 | [diff] [blame] | 3440 | } else if (isa<FPMathOperator>(I)) { |
James Molloy | 88eb535 | 2015-07-10 12:52:00 +0000 | [diff] [blame] | 3441 | FastMathFlags FMF = getDecodedFastMathFlags(Record[OpNum]); |
Michael Ilseman | 9978d7e | 2012-11-27 00:43:38 +0000 | [diff] [blame] | 3442 | if (FMF.any()) |
| 3443 | I->setFastMathFlags(FMF); |
Dan Gohman | 1b84908 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 3444 | } |
Michael Ilseman | 9978d7e | 2012-11-27 00:43:38 +0000 | [diff] [blame] | 3445 | |
Dan Gohman | 1b84908 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 3446 | } |
Chris Lattner | 85b7b40 | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 3447 | break; |
| 3448 | } |
Chris Lattner | e9759c2 | 2007-05-06 00:21:25 +0000 | [diff] [blame] | 3449 | case bitc::FUNC_CODE_INST_CAST: { // CAST: [opval, opty, destty, castopc] |
| 3450 | unsigned OpNum = 0; |
| 3451 | Value *Op; |
| 3452 | if (getValueTypePair(Record, OpNum, NextValueNo, Op) || |
| 3453 | OpNum+2 != Record.size()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3454 | return error("Invalid record"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3455 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3456 | Type *ResTy = getTypeByID(Record[OpNum]); |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3457 | int Opc = getDecodedCastOpcode(Record[OpNum + 1]); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 3458 | if (Opc == -1 || !ResTy) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3459 | return error("Invalid record"); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 3460 | Instruction *Temp = nullptr; |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 3461 | if ((I = UpgradeBitCastInst(Opc, Op, ResTy, Temp))) { |
| 3462 | if (Temp) { |
| 3463 | InstructionList.push_back(Temp); |
| 3464 | CurBB->getInstList().push_back(Temp); |
| 3465 | } |
| 3466 | } else { |
Filipe Cabecinhas | b70fd87 | 2015-10-06 12:37:54 +0000 | [diff] [blame] | 3467 | auto CastOp = (Instruction::CastOps)Opc; |
| 3468 | if (!CastInst::castIsValid(CastOp, Op, ResTy)) |
| 3469 | return error("Invalid cast"); |
| 3470 | I = CastInst::Create(CastOp, Op, ResTy); |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 3471 | } |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 3472 | InstructionList.push_back(I); |
Chris Lattner | e53603e | 2007-05-02 04:27:25 +0000 | [diff] [blame] | 3473 | break; |
| 3474 | } |
David Blaikie | b5b5efd | 2015-02-25 01:08:52 +0000 | [diff] [blame] | 3475 | case bitc::FUNC_CODE_INST_INBOUNDS_GEP_OLD: |
| 3476 | case bitc::FUNC_CODE_INST_GEP_OLD: |
| 3477 | case bitc::FUNC_CODE_INST_GEP: { // GEP: type, [n x operands] |
Chris Lattner | df1233d | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 3478 | unsigned OpNum = 0; |
David Blaikie | b5b5efd | 2015-02-25 01:08:52 +0000 | [diff] [blame] | 3479 | |
| 3480 | Type *Ty; |
| 3481 | bool InBounds; |
| 3482 | |
| 3483 | if (BitCode == bitc::FUNC_CODE_INST_GEP) { |
| 3484 | InBounds = Record[OpNum++]; |
| 3485 | Ty = getTypeByID(Record[OpNum++]); |
| 3486 | } else { |
| 3487 | InBounds = BitCode == bitc::FUNC_CODE_INST_INBOUNDS_GEP_OLD; |
| 3488 | Ty = nullptr; |
| 3489 | } |
| 3490 | |
Chris Lattner | df1233d | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 3491 | Value *BasePtr; |
| 3492 | if (getValueTypePair(Record, OpNum, NextValueNo, BasePtr)) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3493 | return error("Invalid record"); |
Chris Lattner | 1fc27f0 | 2007-05-02 05:16:49 +0000 | [diff] [blame] | 3494 | |
David Blaikie | 60310f2 | 2015-05-08 00:42:26 +0000 | [diff] [blame] | 3495 | if (!Ty) |
Peter Collingbourne | 9ef5a8c | 2016-11-13 06:59:28 +0000 | [diff] [blame] | 3496 | Ty = cast<PointerType>(BasePtr->getType()->getScalarType()) |
David Blaikie | 60310f2 | 2015-05-08 00:42:26 +0000 | [diff] [blame] | 3497 | ->getElementType(); |
| 3498 | else if (Ty != |
Peter Collingbourne | 9ef5a8c | 2016-11-13 06:59:28 +0000 | [diff] [blame] | 3499 | cast<PointerType>(BasePtr->getType()->getScalarType()) |
David Blaikie | 60310f2 | 2015-05-08 00:42:26 +0000 | [diff] [blame] | 3500 | ->getElementType()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3501 | return error( |
David Blaikie | 675e8cb | 2015-03-16 21:35:48 +0000 | [diff] [blame] | 3502 | "Explicit gep type does not match pointee type of pointer operand"); |
| 3503 | |
Chris Lattner | 5285b5e | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 3504 | SmallVector<Value*, 16> GEPIdx; |
Chris Lattner | df1233d | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 3505 | while (OpNum != Record.size()) { |
| 3506 | Value *Op; |
| 3507 | if (getValueTypePair(Record, OpNum, NextValueNo, Op)) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3508 | return error("Invalid record"); |
Chris Lattner | df1233d | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 3509 | GEPIdx.push_back(Op); |
Chris Lattner | 1fc27f0 | 2007-05-02 05:16:49 +0000 | [diff] [blame] | 3510 | } |
| 3511 | |
David Blaikie | 096b1da | 2015-03-14 19:53:33 +0000 | [diff] [blame] | 3512 | I = GetElementPtrInst::Create(Ty, BasePtr, GEPIdx); |
David Blaikie | 675e8cb | 2015-03-16 21:35:48 +0000 | [diff] [blame] | 3513 | |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 3514 | InstructionList.push_back(I); |
David Blaikie | b5b5efd | 2015-02-25 01:08:52 +0000 | [diff] [blame] | 3515 | if (InBounds) |
Dan Gohman | 1b84908 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 3516 | cast<GetElementPtrInst>(I)->setIsInBounds(true); |
Chris Lattner | 1fc27f0 | 2007-05-02 05:16:49 +0000 | [diff] [blame] | 3517 | break; |
| 3518 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3519 | |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 3520 | case bitc::FUNC_CODE_INST_EXTRACTVAL: { |
| 3521 | // EXTRACTVAL: [opty, opval, n x indices] |
Dan Gohman | 3049984 | 2008-05-23 01:55:30 +0000 | [diff] [blame] | 3522 | unsigned OpNum = 0; |
| 3523 | Value *Agg; |
| 3524 | if (getValueTypePair(Record, OpNum, NextValueNo, Agg)) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3525 | return error("Invalid record"); |
Dan Gohman | 3049984 | 2008-05-23 01:55:30 +0000 | [diff] [blame] | 3526 | |
Filipe Cabecinhas | 1c299d0 | 2015-05-16 00:33:12 +0000 | [diff] [blame] | 3527 | unsigned RecSize = Record.size(); |
| 3528 | if (OpNum == RecSize) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3529 | return error("EXTRACTVAL: Invalid instruction with 0 indices"); |
Filipe Cabecinhas | 1c299d0 | 2015-05-16 00:33:12 +0000 | [diff] [blame] | 3530 | |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 3531 | SmallVector<unsigned, 4> EXTRACTVALIdx; |
Filipe Cabecinhas | ecf8f7f | 2015-02-16 00:03:11 +0000 | [diff] [blame] | 3532 | Type *CurTy = Agg->getType(); |
Filipe Cabecinhas | 1c299d0 | 2015-05-16 00:33:12 +0000 | [diff] [blame] | 3533 | for (; OpNum != RecSize; ++OpNum) { |
Filipe Cabecinhas | ecf8f7f | 2015-02-16 00:03:11 +0000 | [diff] [blame] | 3534 | bool IsArray = CurTy->isArrayTy(); |
| 3535 | bool IsStruct = CurTy->isStructTy(); |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 3536 | uint64_t Index = Record[OpNum]; |
Filipe Cabecinhas | ecf8f7f | 2015-02-16 00:03:11 +0000 | [diff] [blame] | 3537 | |
| 3538 | if (!IsStruct && !IsArray) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3539 | return error("EXTRACTVAL: Invalid type"); |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 3540 | if ((unsigned)Index != Index) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3541 | return error("Invalid value"); |
Filipe Cabecinhas | ecf8f7f | 2015-02-16 00:03:11 +0000 | [diff] [blame] | 3542 | if (IsStruct && Index >= CurTy->subtypes().size()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3543 | return error("EXTRACTVAL: Invalid struct index"); |
Filipe Cabecinhas | ecf8f7f | 2015-02-16 00:03:11 +0000 | [diff] [blame] | 3544 | if (IsArray && Index >= CurTy->getArrayNumElements()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3545 | return error("EXTRACTVAL: Invalid array index"); |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 3546 | EXTRACTVALIdx.push_back((unsigned)Index); |
Filipe Cabecinhas | ecf8f7f | 2015-02-16 00:03:11 +0000 | [diff] [blame] | 3547 | |
| 3548 | if (IsStruct) |
| 3549 | CurTy = CurTy->subtypes()[Index]; |
| 3550 | else |
| 3551 | CurTy = CurTy->subtypes()[0]; |
Dan Gohman | 3049984 | 2008-05-23 01:55:30 +0000 | [diff] [blame] | 3552 | } |
| 3553 | |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 3554 | I = ExtractValueInst::Create(Agg, EXTRACTVALIdx); |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 3555 | InstructionList.push_back(I); |
Dan Gohman | 3049984 | 2008-05-23 01:55:30 +0000 | [diff] [blame] | 3556 | break; |
| 3557 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3558 | |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 3559 | case bitc::FUNC_CODE_INST_INSERTVAL: { |
| 3560 | // INSERTVAL: [opty, opval, opty, opval, n x indices] |
Dan Gohman | 3049984 | 2008-05-23 01:55:30 +0000 | [diff] [blame] | 3561 | unsigned OpNum = 0; |
| 3562 | Value *Agg; |
| 3563 | if (getValueTypePair(Record, OpNum, NextValueNo, Agg)) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3564 | return error("Invalid record"); |
Dan Gohman | 3049984 | 2008-05-23 01:55:30 +0000 | [diff] [blame] | 3565 | Value *Val; |
| 3566 | if (getValueTypePair(Record, OpNum, NextValueNo, Val)) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3567 | return error("Invalid record"); |
Dan Gohman | 3049984 | 2008-05-23 01:55:30 +0000 | [diff] [blame] | 3568 | |
Filipe Cabecinhas | 1c299d0 | 2015-05-16 00:33:12 +0000 | [diff] [blame] | 3569 | unsigned RecSize = Record.size(); |
| 3570 | if (OpNum == RecSize) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3571 | return error("INSERTVAL: Invalid instruction with 0 indices"); |
Filipe Cabecinhas | 1c299d0 | 2015-05-16 00:33:12 +0000 | [diff] [blame] | 3572 | |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 3573 | SmallVector<unsigned, 4> INSERTVALIdx; |
Filipe Cabecinhas | ecf8f7f | 2015-02-16 00:03:11 +0000 | [diff] [blame] | 3574 | Type *CurTy = Agg->getType(); |
Filipe Cabecinhas | 1c299d0 | 2015-05-16 00:33:12 +0000 | [diff] [blame] | 3575 | for (; OpNum != RecSize; ++OpNum) { |
Filipe Cabecinhas | ecf8f7f | 2015-02-16 00:03:11 +0000 | [diff] [blame] | 3576 | bool IsArray = CurTy->isArrayTy(); |
| 3577 | bool IsStruct = CurTy->isStructTy(); |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 3578 | uint64_t Index = Record[OpNum]; |
Filipe Cabecinhas | ecf8f7f | 2015-02-16 00:03:11 +0000 | [diff] [blame] | 3579 | |
| 3580 | if (!IsStruct && !IsArray) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3581 | return error("INSERTVAL: Invalid type"); |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 3582 | if ((unsigned)Index != Index) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3583 | return error("Invalid value"); |
Filipe Cabecinhas | ecf8f7f | 2015-02-16 00:03:11 +0000 | [diff] [blame] | 3584 | if (IsStruct && Index >= CurTy->subtypes().size()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3585 | return error("INSERTVAL: Invalid struct index"); |
Filipe Cabecinhas | ecf8f7f | 2015-02-16 00:03:11 +0000 | [diff] [blame] | 3586 | if (IsArray && Index >= CurTy->getArrayNumElements()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3587 | return error("INSERTVAL: Invalid array index"); |
Filipe Cabecinhas | ecf8f7f | 2015-02-16 00:03:11 +0000 | [diff] [blame] | 3588 | |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 3589 | INSERTVALIdx.push_back((unsigned)Index); |
Filipe Cabecinhas | ecf8f7f | 2015-02-16 00:03:11 +0000 | [diff] [blame] | 3590 | if (IsStruct) |
| 3591 | CurTy = CurTy->subtypes()[Index]; |
| 3592 | else |
| 3593 | CurTy = CurTy->subtypes()[0]; |
Dan Gohman | 3049984 | 2008-05-23 01:55:30 +0000 | [diff] [blame] | 3594 | } |
| 3595 | |
Filipe Cabecinhas | 4708a02 | 2015-05-18 22:27:11 +0000 | [diff] [blame] | 3596 | if (CurTy != Val->getType()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3597 | return error("Inserted value type doesn't match aggregate type"); |
Filipe Cabecinhas | 4708a02 | 2015-05-18 22:27:11 +0000 | [diff] [blame] | 3598 | |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 3599 | I = InsertValueInst::Create(Agg, Val, INSERTVALIdx); |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 3600 | InstructionList.push_back(I); |
Dan Gohman | 3049984 | 2008-05-23 01:55:30 +0000 | [diff] [blame] | 3601 | break; |
| 3602 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3603 | |
Chris Lattner | e9759c2 | 2007-05-06 00:21:25 +0000 | [diff] [blame] | 3604 | case bitc::FUNC_CODE_INST_SELECT: { // SELECT: [opval, ty, opval, opval] |
Dan Gohman | c5d2892 | 2008-09-16 01:01:33 +0000 | [diff] [blame] | 3605 | // obsolete form of select |
| 3606 | // handles select i1 ... in old bitcode |
Chris Lattner | e9759c2 | 2007-05-06 00:21:25 +0000 | [diff] [blame] | 3607 | unsigned OpNum = 0; |
| 3608 | Value *TrueVal, *FalseVal, *Cond; |
| 3609 | if (getValueTypePair(Record, OpNum, NextValueNo, TrueVal) || |
Jan Wen Voung | afaced0 | 2012-10-11 20:20:40 +0000 | [diff] [blame] | 3610 | popValue(Record, OpNum, NextValueNo, TrueVal->getType(), FalseVal) || |
| 3611 | popValue(Record, OpNum, NextValueNo, Type::getInt1Ty(Context), Cond)) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3612 | return error("Invalid record"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3613 | |
Dan Gohman | c5d2892 | 2008-09-16 01:01:33 +0000 | [diff] [blame] | 3614 | I = SelectInst::Create(Cond, TrueVal, FalseVal); |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 3615 | InstructionList.push_back(I); |
Dan Gohman | c5d2892 | 2008-09-16 01:01:33 +0000 | [diff] [blame] | 3616 | break; |
| 3617 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3618 | |
Dan Gohman | c5d2892 | 2008-09-16 01:01:33 +0000 | [diff] [blame] | 3619 | case bitc::FUNC_CODE_INST_VSELECT: {// VSELECT: [ty,opval,opval,predty,pred] |
| 3620 | // new form of select |
| 3621 | // handles select i1 or select [N x i1] |
| 3622 | unsigned OpNum = 0; |
| 3623 | Value *TrueVal, *FalseVal, *Cond; |
| 3624 | if (getValueTypePair(Record, OpNum, NextValueNo, TrueVal) || |
Jan Wen Voung | afaced0 | 2012-10-11 20:20:40 +0000 | [diff] [blame] | 3625 | popValue(Record, OpNum, NextValueNo, TrueVal->getType(), FalseVal) || |
Dan Gohman | c5d2892 | 2008-09-16 01:01:33 +0000 | [diff] [blame] | 3626 | getValueTypePair(Record, OpNum, NextValueNo, Cond)) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3627 | return error("Invalid record"); |
Dan Gohman | c579d97 | 2008-09-09 01:02:47 +0000 | [diff] [blame] | 3628 | |
| 3629 | // select condition can be either i1 or [N x i1] |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3630 | if (VectorType* vector_type = |
| 3631 | dyn_cast<VectorType>(Cond->getType())) { |
Dan Gohman | c579d97 | 2008-09-09 01:02:47 +0000 | [diff] [blame] | 3632 | // expect <n x i1> |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3633 | if (vector_type->getElementType() != Type::getInt1Ty(Context)) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3634 | return error("Invalid type for value"); |
Dan Gohman | c579d97 | 2008-09-09 01:02:47 +0000 | [diff] [blame] | 3635 | } else { |
| 3636 | // expect i1 |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3637 | if (Cond->getType() != Type::getInt1Ty(Context)) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3638 | return error("Invalid type for value"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3639 | } |
| 3640 | |
Gabor Greif | e9ecc68 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 3641 | I = SelectInst::Create(Cond, TrueVal, FalseVal); |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 3642 | InstructionList.push_back(I); |
Chris Lattner | 1fc27f0 | 2007-05-02 05:16:49 +0000 | [diff] [blame] | 3643 | break; |
| 3644 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3645 | |
Chris Lattner | 1fc27f0 | 2007-05-02 05:16:49 +0000 | [diff] [blame] | 3646 | case bitc::FUNC_CODE_INST_EXTRACTELT: { // EXTRACTELT: [opty, opval, opval] |
Chris Lattner | e9759c2 | 2007-05-06 00:21:25 +0000 | [diff] [blame] | 3647 | unsigned OpNum = 0; |
| 3648 | Value *Vec, *Idx; |
| 3649 | if (getValueTypePair(Record, OpNum, NextValueNo, Vec) || |
Michael J. Spencer | 1f10c5ea | 2014-05-01 22:12:39 +0000 | [diff] [blame] | 3650 | getValueTypePair(Record, OpNum, NextValueNo, Idx)) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3651 | return error("Invalid record"); |
Filipe Cabecinhas | ff1e234 | 2015-04-24 11:30:15 +0000 | [diff] [blame] | 3652 | if (!Vec->getType()->isVectorTy()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3653 | return error("Invalid type for value"); |
Eric Christopher | c974225 | 2009-07-25 02:28:41 +0000 | [diff] [blame] | 3654 | I = ExtractElementInst::Create(Vec, Idx); |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 3655 | InstructionList.push_back(I); |
Chris Lattner | 1fc27f0 | 2007-05-02 05:16:49 +0000 | [diff] [blame] | 3656 | break; |
| 3657 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3658 | |
Chris Lattner | 1fc27f0 | 2007-05-02 05:16:49 +0000 | [diff] [blame] | 3659 | case bitc::FUNC_CODE_INST_INSERTELT: { // INSERTELT: [ty, opval,opval,opval] |
Chris Lattner | e9759c2 | 2007-05-06 00:21:25 +0000 | [diff] [blame] | 3660 | unsigned OpNum = 0; |
| 3661 | Value *Vec, *Elt, *Idx; |
Filipe Cabecinhas | ff1e234 | 2015-04-24 11:30:15 +0000 | [diff] [blame] | 3662 | if (getValueTypePair(Record, OpNum, NextValueNo, Vec)) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3663 | return error("Invalid record"); |
Filipe Cabecinhas | ff1e234 | 2015-04-24 11:30:15 +0000 | [diff] [blame] | 3664 | if (!Vec->getType()->isVectorTy()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3665 | return error("Invalid type for value"); |
Filipe Cabecinhas | ff1e234 | 2015-04-24 11:30:15 +0000 | [diff] [blame] | 3666 | if (popValue(Record, OpNum, NextValueNo, |
Chris Lattner | e9759c2 | 2007-05-06 00:21:25 +0000 | [diff] [blame] | 3667 | cast<VectorType>(Vec->getType())->getElementType(), Elt) || |
Michael J. Spencer | 1f10c5ea | 2014-05-01 22:12:39 +0000 | [diff] [blame] | 3668 | getValueTypePair(Record, OpNum, NextValueNo, Idx)) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3669 | return error("Invalid record"); |
Gabor Greif | e9ecc68 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 3670 | I = InsertElementInst::Create(Vec, Elt, Idx); |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 3671 | InstructionList.push_back(I); |
Chris Lattner | 1fc27f0 | 2007-05-02 05:16:49 +0000 | [diff] [blame] | 3672 | break; |
| 3673 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3674 | |
Chris Lattner | e9759c2 | 2007-05-06 00:21:25 +0000 | [diff] [blame] | 3675 | case bitc::FUNC_CODE_INST_SHUFFLEVEC: {// SHUFFLEVEC: [opval,ty,opval,opval] |
| 3676 | unsigned OpNum = 0; |
| 3677 | Value *Vec1, *Vec2, *Mask; |
| 3678 | if (getValueTypePair(Record, OpNum, NextValueNo, Vec1) || |
Jan Wen Voung | afaced0 | 2012-10-11 20:20:40 +0000 | [diff] [blame] | 3679 | popValue(Record, OpNum, NextValueNo, Vec1->getType(), Vec2)) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3680 | return error("Invalid record"); |
Chris Lattner | e9759c2 | 2007-05-06 00:21:25 +0000 | [diff] [blame] | 3681 | |
Mon P Wang | 25f0106 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 3682 | if (getValueTypePair(Record, OpNum, NextValueNo, Mask)) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3683 | return error("Invalid record"); |
Filipe Cabecinhas | ff1e234 | 2015-04-24 11:30:15 +0000 | [diff] [blame] | 3684 | if (!Vec1->getType()->isVectorTy() || !Vec2->getType()->isVectorTy()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3685 | return error("Invalid type for value"); |
Chris Lattner | 1fc27f0 | 2007-05-02 05:16:49 +0000 | [diff] [blame] | 3686 | I = new ShuffleVectorInst(Vec1, Vec2, Mask); |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 3687 | InstructionList.push_back(I); |
Chris Lattner | 1fc27f0 | 2007-05-02 05:16:49 +0000 | [diff] [blame] | 3688 | break; |
| 3689 | } |
Mon P Wang | 25f0106 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 3690 | |
Nick Lewycky | a21d3da | 2009-07-08 03:04:38 +0000 | [diff] [blame] | 3691 | case bitc::FUNC_CODE_INST_CMP: // CMP: [opty, opval, opval, pred] |
| 3692 | // Old form of ICmp/FCmp returning bool |
| 3693 | // Existed to differentiate between icmp/fcmp and vicmp/vfcmp which were |
| 3694 | // both legal on vectors but had different behaviour. |
| 3695 | case bitc::FUNC_CODE_INST_CMP2: { // CMP2: [opty, opval, opval, pred] |
| 3696 | // FCmp/ICmp returning bool or vector of bool |
| 3697 | |
Chris Lattner | df1233d | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 3698 | unsigned OpNum = 0; |
| 3699 | Value *LHS, *RHS; |
| 3700 | if (getValueTypePair(Record, OpNum, NextValueNo, LHS) || |
James Molloy | 88eb535 | 2015-07-10 12:52:00 +0000 | [diff] [blame] | 3701 | popValue(Record, OpNum, NextValueNo, LHS->getType(), RHS)) |
| 3702 | return error("Invalid record"); |
| 3703 | |
| 3704 | unsigned PredVal = Record[OpNum]; |
| 3705 | bool IsFP = LHS->getType()->isFPOrFPVectorTy(); |
| 3706 | FastMathFlags FMF; |
| 3707 | if (IsFP && Record.size() > OpNum+1) |
| 3708 | FMF = getDecodedFastMathFlags(Record[++OpNum]); |
| 3709 | |
| 3710 | if (OpNum+1 != Record.size()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3711 | return error("Invalid record"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3712 | |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 3713 | if (LHS->getType()->isFPOrFPVectorTy()) |
James Molloy | 88eb535 | 2015-07-10 12:52:00 +0000 | [diff] [blame] | 3714 | I = new FCmpInst((FCmpInst::Predicate)PredVal, LHS, RHS); |
Nick Lewycky | a21d3da | 2009-07-08 03:04:38 +0000 | [diff] [blame] | 3715 | else |
James Molloy | 88eb535 | 2015-07-10 12:52:00 +0000 | [diff] [blame] | 3716 | I = new ICmpInst((ICmpInst::Predicate)PredVal, LHS, RHS); |
| 3717 | |
| 3718 | if (FMF.any()) |
| 3719 | I->setFastMathFlags(FMF); |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 3720 | InstructionList.push_back(I); |
Dan Gohman | c579d97 | 2008-09-09 01:02:47 +0000 | [diff] [blame] | 3721 | break; |
| 3722 | } |
Nick Lewycky | a21d3da | 2009-07-08 03:04:38 +0000 | [diff] [blame] | 3723 | |
Chris Lattner | e53603e | 2007-05-02 04:27:25 +0000 | [diff] [blame] | 3724 | case bitc::FUNC_CODE_INST_RET: // RET: [opty,opval<optional>] |
Devang Patel | bbfd874 | 2008-02-26 01:29:32 +0000 | [diff] [blame] | 3725 | { |
| 3726 | unsigned Size = Record.size(); |
| 3727 | if (Size == 0) { |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 3728 | I = ReturnInst::Create(Context); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3729 | InstructionList.push_back(I); |
Devang Patel | bbfd874 | 2008-02-26 01:29:32 +0000 | [diff] [blame] | 3730 | break; |
Dan Gohman | fa1211f | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 3731 | } |
Devang Patel | bbfd874 | 2008-02-26 01:29:32 +0000 | [diff] [blame] | 3732 | |
Dan Gohman | fa1211f | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 3733 | unsigned OpNum = 0; |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 3734 | Value *Op = nullptr; |
Chris Lattner | f1c8710 | 2011-06-17 18:09:11 +0000 | [diff] [blame] | 3735 | if (getValueTypePair(Record, OpNum, NextValueNo, Op)) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3736 | return error("Invalid record"); |
Chris Lattner | f1c8710 | 2011-06-17 18:09:11 +0000 | [diff] [blame] | 3737 | if (OpNum != Record.size()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3738 | return error("Invalid record"); |
Dan Gohman | fa1211f | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 3739 | |
Chris Lattner | f1c8710 | 2011-06-17 18:09:11 +0000 | [diff] [blame] | 3740 | I = ReturnInst::Create(Context, Op); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3741 | InstructionList.push_back(I); |
Dan Gohman | fa1211f | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 3742 | break; |
Chris Lattner | e53603e | 2007-05-02 04:27:25 +0000 | [diff] [blame] | 3743 | } |
Chris Lattner | 5285b5e | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 3744 | case bitc::FUNC_CODE_INST_BR: { // BR: [bb#, bb#, opval] or [bb#] |
Chris Lattner | 6ce15cb | 2007-05-03 22:09:51 +0000 | [diff] [blame] | 3745 | if (Record.size() != 1 && Record.size() != 3) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3746 | return error("Invalid record"); |
Chris Lattner | 5285b5e | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 3747 | BasicBlock *TrueDest = getBasicBlock(Record[0]); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 3748 | if (!TrueDest) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3749 | return error("Invalid record"); |
Chris Lattner | 5285b5e | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 3750 | |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 3751 | if (Record.size() == 1) { |
Gabor Greif | e9ecc68 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 3752 | I = BranchInst::Create(TrueDest); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3753 | InstructionList.push_back(I); |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 3754 | } |
Chris Lattner | 5285b5e | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 3755 | else { |
| 3756 | BasicBlock *FalseDest = getBasicBlock(Record[1]); |
Jan Wen Voung | afaced0 | 2012-10-11 20:20:40 +0000 | [diff] [blame] | 3757 | Value *Cond = getValue(Record, 2, NextValueNo, |
| 3758 | Type::getInt1Ty(Context)); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 3759 | if (!FalseDest || !Cond) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3760 | return error("Invalid record"); |
Gabor Greif | e9ecc68 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 3761 | I = BranchInst::Create(TrueDest, FalseDest, Cond); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3762 | InstructionList.push_back(I); |
Chris Lattner | 5285b5e | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 3763 | } |
| 3764 | break; |
| 3765 | } |
David Majnemer | b01aa9f | 2015-08-23 19:22:31 +0000 | [diff] [blame] | 3766 | case bitc::FUNC_CODE_INST_CLEANUPRET: { // CLEANUPRET: [val] or [val,bb#] |
Joseph Tremoulet | 8220bcc | 2015-08-23 00:26:33 +0000 | [diff] [blame] | 3767 | if (Record.size() != 1 && Record.size() != 2) |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 3768 | return error("Invalid record"); |
| 3769 | unsigned Idx = 0; |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 3770 | Value *CleanupPad = |
| 3771 | getValue(Record, Idx++, NextValueNo, Type::getTokenTy(Context)); |
Joseph Tremoulet | 8220bcc | 2015-08-23 00:26:33 +0000 | [diff] [blame] | 3772 | if (!CleanupPad) |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 3773 | return error("Invalid record"); |
Joseph Tremoulet | 8220bcc | 2015-08-23 00:26:33 +0000 | [diff] [blame] | 3774 | BasicBlock *UnwindDest = nullptr; |
| 3775 | if (Record.size() == 2) { |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 3776 | UnwindDest = getBasicBlock(Record[Idx++]); |
| 3777 | if (!UnwindDest) |
| 3778 | return error("Invalid record"); |
| 3779 | } |
| 3780 | |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 3781 | I = CleanupReturnInst::Create(CleanupPad, UnwindDest); |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 3782 | InstructionList.push_back(I); |
| 3783 | break; |
| 3784 | } |
Joseph Tremoulet | 8220bcc | 2015-08-23 00:26:33 +0000 | [diff] [blame] | 3785 | case bitc::FUNC_CODE_INST_CATCHRET: { // CATCHRET: [val,bb#] |
| 3786 | if (Record.size() != 2) |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 3787 | return error("Invalid record"); |
David Majnemer | 0bc0eef | 2015-08-15 02:46:08 +0000 | [diff] [blame] | 3788 | unsigned Idx = 0; |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 3789 | Value *CatchPad = |
| 3790 | getValue(Record, Idx++, NextValueNo, Type::getTokenTy(Context)); |
Joseph Tremoulet | 8220bcc | 2015-08-23 00:26:33 +0000 | [diff] [blame] | 3791 | if (!CatchPad) |
| 3792 | return error("Invalid record"); |
David Majnemer | 0bc0eef | 2015-08-15 02:46:08 +0000 | [diff] [blame] | 3793 | BasicBlock *BB = getBasicBlock(Record[Idx++]); |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 3794 | if (!BB) |
| 3795 | return error("Invalid record"); |
David Majnemer | 0bc0eef | 2015-08-15 02:46:08 +0000 | [diff] [blame] | 3796 | |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 3797 | I = CatchReturnInst::Create(CatchPad, BB); |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 3798 | InstructionList.push_back(I); |
| 3799 | break; |
| 3800 | } |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 3801 | case bitc::FUNC_CODE_INST_CATCHSWITCH: { // CATCHSWITCH: [tok,num,(bb)*,bb?] |
| 3802 | // We must have, at minimum, the outer scope and the number of arguments. |
| 3803 | if (Record.size() < 2) |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 3804 | return error("Invalid record"); |
| 3805 | |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 3806 | unsigned Idx = 0; |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 3807 | |
| 3808 | Value *ParentPad = |
| 3809 | getValue(Record, Idx++, NextValueNo, Type::getTokenTy(Context)); |
| 3810 | |
| 3811 | unsigned NumHandlers = Record[Idx++]; |
| 3812 | |
| 3813 | SmallVector<BasicBlock *, 2> Handlers; |
| 3814 | for (unsigned Op = 0; Op != NumHandlers; ++Op) { |
| 3815 | BasicBlock *BB = getBasicBlock(Record[Idx++]); |
| 3816 | if (!BB) |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 3817 | return error("Invalid record"); |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 3818 | Handlers.push_back(BB); |
| 3819 | } |
| 3820 | |
| 3821 | BasicBlock *UnwindDest = nullptr; |
| 3822 | if (Idx + 1 == Record.size()) { |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 3823 | UnwindDest = getBasicBlock(Record[Idx++]); |
| 3824 | if (!UnwindDest) |
| 3825 | return error("Invalid record"); |
| 3826 | } |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 3827 | |
| 3828 | if (Record.size() != Idx) |
| 3829 | return error("Invalid record"); |
| 3830 | |
| 3831 | auto *CatchSwitch = |
| 3832 | CatchSwitchInst::Create(ParentPad, UnwindDest, NumHandlers); |
| 3833 | for (BasicBlock *Handler : Handlers) |
| 3834 | CatchSwitch->addHandler(Handler); |
| 3835 | I = CatchSwitch; |
| 3836 | InstructionList.push_back(I); |
| 3837 | break; |
| 3838 | } |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 3839 | case bitc::FUNC_CODE_INST_CATCHPAD: |
| 3840 | case bitc::FUNC_CODE_INST_CLEANUPPAD: { // [tok,num,(ty,val)*] |
| 3841 | // We must have, at minimum, the outer scope and the number of arguments. |
| 3842 | if (Record.size() < 2) |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 3843 | return error("Invalid record"); |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 3844 | |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 3845 | unsigned Idx = 0; |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 3846 | |
| 3847 | Value *ParentPad = |
| 3848 | getValue(Record, Idx++, NextValueNo, Type::getTokenTy(Context)); |
| 3849 | |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 3850 | unsigned NumArgOperands = Record[Idx++]; |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 3851 | |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 3852 | SmallVector<Value *, 2> Args; |
| 3853 | for (unsigned Op = 0; Op != NumArgOperands; ++Op) { |
| 3854 | Value *Val; |
| 3855 | if (getValueTypePair(Record, Idx, NextValueNo, Val)) |
| 3856 | return error("Invalid record"); |
| 3857 | Args.push_back(Val); |
| 3858 | } |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 3859 | |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 3860 | if (Record.size() != Idx) |
| 3861 | return error("Invalid record"); |
| 3862 | |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 3863 | if (BitCode == bitc::FUNC_CODE_INST_CLEANUPPAD) |
| 3864 | I = CleanupPadInst::Create(ParentPad, Args); |
| 3865 | else |
| 3866 | I = CatchPadInst::Create(ParentPad, Args); |
Joseph Tremoulet | 9ce71f7 | 2015-09-03 09:09:43 +0000 | [diff] [blame] | 3867 | InstructionList.push_back(I); |
| 3868 | break; |
| 3869 | } |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3870 | case bitc::FUNC_CODE_INST_SWITCH: { // SWITCH: [opty, op0, op1, ...] |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 3871 | // Check magic |
Stepan Dyatkovskiy | 0beab5e | 2012-05-12 10:48:17 +0000 | [diff] [blame] | 3872 | if ((Record[0] >> 16) == SWITCH_INST_MAGIC) { |
Bob Wilson | e407736 | 2013-09-09 19:14:35 +0000 | [diff] [blame] | 3873 | // "New" SwitchInst format with case ranges. The changes to write this |
| 3874 | // format were reverted but we still recognize bitcode that uses it. |
| 3875 | // Hopefully someday we will have support for case ranges and can use |
| 3876 | // this format again. |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 3877 | |
Stepan Dyatkovskiy | 0beab5e | 2012-05-12 10:48:17 +0000 | [diff] [blame] | 3878 | Type *OpTy = getTypeByID(Record[1]); |
| 3879 | unsigned ValueBitWidth = cast<IntegerType>(OpTy)->getBitWidth(); |
| 3880 | |
Jan Wen Voung | afaced0 | 2012-10-11 20:20:40 +0000 | [diff] [blame] | 3881 | Value *Cond = getValue(Record, 2, NextValueNo, OpTy); |
Stepan Dyatkovskiy | 0beab5e | 2012-05-12 10:48:17 +0000 | [diff] [blame] | 3882 | BasicBlock *Default = getBasicBlock(Record[3]); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 3883 | if (!OpTy || !Cond || !Default) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3884 | return error("Invalid record"); |
Stepan Dyatkovskiy | 0beab5e | 2012-05-12 10:48:17 +0000 | [diff] [blame] | 3885 | |
| 3886 | unsigned NumCases = Record[4]; |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 3887 | |
Stepan Dyatkovskiy | 0beab5e | 2012-05-12 10:48:17 +0000 | [diff] [blame] | 3888 | SwitchInst *SI = SwitchInst::Create(Cond, Default, NumCases); |
| 3889 | InstructionList.push_back(SI); |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 3890 | |
Stepan Dyatkovskiy | 0beab5e | 2012-05-12 10:48:17 +0000 | [diff] [blame] | 3891 | unsigned CurIdx = 5; |
| 3892 | for (unsigned i = 0; i != NumCases; ++i) { |
Bob Wilson | e407736 | 2013-09-09 19:14:35 +0000 | [diff] [blame] | 3893 | SmallVector<ConstantInt*, 1> CaseVals; |
Stepan Dyatkovskiy | 0beab5e | 2012-05-12 10:48:17 +0000 | [diff] [blame] | 3894 | unsigned NumItems = Record[CurIdx++]; |
| 3895 | for (unsigned ci = 0; ci != NumItems; ++ci) { |
| 3896 | bool isSingleNumber = Record[CurIdx++]; |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 3897 | |
Stepan Dyatkovskiy | 0beab5e | 2012-05-12 10:48:17 +0000 | [diff] [blame] | 3898 | APInt Low; |
| 3899 | unsigned ActiveWords = 1; |
| 3900 | if (ValueBitWidth > 64) |
| 3901 | ActiveWords = Record[CurIdx++]; |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3902 | Low = readWideAPInt(makeArrayRef(&Record[CurIdx], ActiveWords), |
Benjamin Kramer | 9704ed0 | 2012-05-28 14:10:31 +0000 | [diff] [blame] | 3903 | ValueBitWidth); |
Stepan Dyatkovskiy | 0beab5e | 2012-05-12 10:48:17 +0000 | [diff] [blame] | 3904 | CurIdx += ActiveWords; |
Stepan Dyatkovskiy | e3e19cb | 2012-05-28 12:39:09 +0000 | [diff] [blame] | 3905 | |
Stepan Dyatkovskiy | 0beab5e | 2012-05-12 10:48:17 +0000 | [diff] [blame] | 3906 | if (!isSingleNumber) { |
| 3907 | ActiveWords = 1; |
| 3908 | if (ValueBitWidth > 64) |
| 3909 | ActiveWords = Record[CurIdx++]; |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3910 | APInt High = readWideAPInt( |
| 3911 | makeArrayRef(&Record[CurIdx], ActiveWords), ValueBitWidth); |
Stepan Dyatkovskiy | 0beab5e | 2012-05-12 10:48:17 +0000 | [diff] [blame] | 3912 | CurIdx += ActiveWords; |
Bob Wilson | e407736 | 2013-09-09 19:14:35 +0000 | [diff] [blame] | 3913 | |
| 3914 | // FIXME: It is not clear whether values in the range should be |
| 3915 | // compared as signed or unsigned values. The partially |
| 3916 | // implemented changes that used this format in the past used |
| 3917 | // unsigned comparisons. |
| 3918 | for ( ; Low.ule(High); ++Low) |
| 3919 | CaseVals.push_back(ConstantInt::get(Context, Low)); |
Stepan Dyatkovskiy | 0beab5e | 2012-05-12 10:48:17 +0000 | [diff] [blame] | 3920 | } else |
Bob Wilson | e407736 | 2013-09-09 19:14:35 +0000 | [diff] [blame] | 3921 | CaseVals.push_back(ConstantInt::get(Context, Low)); |
Stepan Dyatkovskiy | 0beab5e | 2012-05-12 10:48:17 +0000 | [diff] [blame] | 3922 | } |
| 3923 | BasicBlock *DestBB = getBasicBlock(Record[CurIdx++]); |
Bob Wilson | e407736 | 2013-09-09 19:14:35 +0000 | [diff] [blame] | 3924 | for (SmallVector<ConstantInt*, 1>::iterator cvi = CaseVals.begin(), |
| 3925 | cve = CaseVals.end(); cvi != cve; ++cvi) |
| 3926 | SI->addCase(*cvi, DestBB); |
Stepan Dyatkovskiy | 0beab5e | 2012-05-12 10:48:17 +0000 | [diff] [blame] | 3927 | } |
Stepan Dyatkovskiy | 0beab5e | 2012-05-12 10:48:17 +0000 | [diff] [blame] | 3928 | I = SI; |
| 3929 | break; |
| 3930 | } |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 3931 | |
Stepan Dyatkovskiy | 0beab5e | 2012-05-12 10:48:17 +0000 | [diff] [blame] | 3932 | // Old SwitchInst format without case ranges. |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 3933 | |
Chris Lattner | 5285b5e | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 3934 | if (Record.size() < 3 || (Record.size() & 1) == 0) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3935 | return error("Invalid record"); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3936 | Type *OpTy = getTypeByID(Record[0]); |
Jan Wen Voung | afaced0 | 2012-10-11 20:20:40 +0000 | [diff] [blame] | 3937 | Value *Cond = getValue(Record, 1, NextValueNo, OpTy); |
Chris Lattner | 5285b5e | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 3938 | BasicBlock *Default = getBasicBlock(Record[2]); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 3939 | if (!OpTy || !Cond || !Default) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3940 | return error("Invalid record"); |
Chris Lattner | 5285b5e | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 3941 | unsigned NumCases = (Record.size()-3)/2; |
Gabor Greif | e9ecc68 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 3942 | SwitchInst *SI = SwitchInst::Create(Cond, Default, NumCases); |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 3943 | InstructionList.push_back(SI); |
Chris Lattner | 5285b5e | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 3944 | for (unsigned i = 0, e = NumCases; i != e; ++i) { |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3945 | ConstantInt *CaseVal = |
Chris Lattner | 5285b5e | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 3946 | dyn_cast_or_null<ConstantInt>(getFnValueByID(Record[3+i*2], OpTy)); |
| 3947 | BasicBlock *DestBB = getBasicBlock(Record[1+3+i*2]); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 3948 | if (!CaseVal || !DestBB) { |
Chris Lattner | 5285b5e | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 3949 | delete SI; |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3950 | return error("Invalid record"); |
Chris Lattner | 5285b5e | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 3951 | } |
| 3952 | SI->addCase(CaseVal, DestBB); |
| 3953 | } |
| 3954 | I = SI; |
| 3955 | break; |
| 3956 | } |
Chris Lattner | d04cb6d | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 3957 | case bitc::FUNC_CODE_INST_INDIRECTBR: { // INDIRECTBR: [opty, op0, op1, ...] |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3958 | if (Record.size() < 2) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3959 | return error("Invalid record"); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3960 | Type *OpTy = getTypeByID(Record[0]); |
Jan Wen Voung | afaced0 | 2012-10-11 20:20:40 +0000 | [diff] [blame] | 3961 | Value *Address = getValue(Record, 1, NextValueNo, OpTy); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 3962 | if (!OpTy || !Address) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3963 | return error("Invalid record"); |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3964 | unsigned NumDests = Record.size()-2; |
Chris Lattner | d04cb6d | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 3965 | IndirectBrInst *IBI = IndirectBrInst::Create(Address, NumDests); |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3966 | InstructionList.push_back(IBI); |
| 3967 | for (unsigned i = 0, e = NumDests; i != e; ++i) { |
| 3968 | if (BasicBlock *DestBB = getBasicBlock(Record[2+i])) { |
| 3969 | IBI->addDestination(DestBB); |
| 3970 | } else { |
| 3971 | delete IBI; |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3972 | return error("Invalid record"); |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3973 | } |
| 3974 | } |
| 3975 | I = IBI; |
| 3976 | break; |
| 3977 | } |
Michael Ilseman | 26ee2b8 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 3978 | |
Duncan Sands | ad0ea2d | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 3979 | case bitc::FUNC_CODE_INST_INVOKE: { |
| 3980 | // INVOKE: [attrs, cc, normBB, unwindBB, fnty, op0,op1,op2, ...] |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 3981 | if (Record.size() < 4) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3982 | return error("Invalid record"); |
David Blaikie | 5ea1f7b | 2015-04-24 18:06:06 +0000 | [diff] [blame] | 3983 | unsigned OpNum = 0; |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 3984 | AttributeList PAL = getAttributes(Record[OpNum++]); |
David Blaikie | 5ea1f7b | 2015-04-24 18:06:06 +0000 | [diff] [blame] | 3985 | unsigned CCInfo = Record[OpNum++]; |
| 3986 | BasicBlock *NormalBB = getBasicBlock(Record[OpNum++]); |
| 3987 | BasicBlock *UnwindBB = getBasicBlock(Record[OpNum++]); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3988 | |
David Blaikie | 5ea1f7b | 2015-04-24 18:06:06 +0000 | [diff] [blame] | 3989 | FunctionType *FTy = nullptr; |
| 3990 | if (CCInfo >> 13 & 1 && |
| 3991 | !(FTy = dyn_cast<FunctionType>(getTypeByID(Record[OpNum++])))) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3992 | return error("Explicit invoke type is not a function type"); |
David Blaikie | 5ea1f7b | 2015-04-24 18:06:06 +0000 | [diff] [blame] | 3993 | |
Chris Lattner | df1233d | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 3994 | Value *Callee; |
| 3995 | if (getValueTypePair(Record, OpNum, NextValueNo, Callee)) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 3996 | return error("Invalid record"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 3997 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3998 | PointerType *CalleeTy = dyn_cast<PointerType>(Callee->getType()); |
David Blaikie | 5ea1f7b | 2015-04-24 18:06:06 +0000 | [diff] [blame] | 3999 | if (!CalleeTy) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4000 | return error("Callee is not a pointer"); |
David Blaikie | 5ea1f7b | 2015-04-24 18:06:06 +0000 | [diff] [blame] | 4001 | if (!FTy) { |
| 4002 | FTy = dyn_cast<FunctionType>(CalleeTy->getElementType()); |
| 4003 | if (!FTy) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4004 | return error("Callee is not of pointer to function type"); |
David Blaikie | 5ea1f7b | 2015-04-24 18:06:06 +0000 | [diff] [blame] | 4005 | } else if (CalleeTy->getElementType() != FTy) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4006 | return error("Explicit invoke type does not match pointee type of " |
David Blaikie | 5ea1f7b | 2015-04-24 18:06:06 +0000 | [diff] [blame] | 4007 | "callee operand"); |
| 4008 | if (Record.size() < FTy->getNumParams() + OpNum) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4009 | return error("Insufficient operands to call"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4010 | |
Chris Lattner | 5285b5e | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 4011 | SmallVector<Value*, 16> Ops; |
Chris Lattner | df1233d | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 4012 | for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i, ++OpNum) { |
Jan Wen Voung | afaced0 | 2012-10-11 20:20:40 +0000 | [diff] [blame] | 4013 | Ops.push_back(getValue(Record, OpNum, NextValueNo, |
| 4014 | FTy->getParamType(i))); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 4015 | if (!Ops.back()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4016 | return error("Invalid record"); |
Chris Lattner | 5285b5e | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 4017 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4018 | |
Chris Lattner | df1233d | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 4019 | if (!FTy->isVarArg()) { |
| 4020 | if (Record.size() != OpNum) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4021 | return error("Invalid record"); |
Chris Lattner | 5285b5e | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 4022 | } else { |
Chris Lattner | df1233d | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 4023 | // Read type/value pairs for varargs params. |
| 4024 | while (OpNum != Record.size()) { |
| 4025 | Value *Op; |
| 4026 | if (getValueTypePair(Record, OpNum, NextValueNo, Op)) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4027 | return error("Invalid record"); |
Chris Lattner | df1233d | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 4028 | Ops.push_back(Op); |
| 4029 | } |
Chris Lattner | 5285b5e | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 4030 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4031 | |
Sanjoy Das | b513a9f | 2015-09-24 23:34:52 +0000 | [diff] [blame] | 4032 | I = InvokeInst::Create(Callee, NormalBB, UnwindBB, Ops, OperandBundles); |
| 4033 | OperandBundles.clear(); |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 4034 | InstructionList.push_back(I); |
Vedant Kumar | ad6d6e7 | 2015-10-27 21:17:06 +0000 | [diff] [blame] | 4035 | cast<InvokeInst>(I)->setCallingConv( |
| 4036 | static_cast<CallingConv::ID>(CallingConv::MaxID & CCInfo)); |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 4037 | cast<InvokeInst>(I)->setAttributes(PAL); |
Chris Lattner | 5285b5e | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 4038 | break; |
| 4039 | } |
Bill Wendling | f891bf8 | 2011-07-31 06:30:59 +0000 | [diff] [blame] | 4040 | case bitc::FUNC_CODE_INST_RESUME: { // RESUME: [opval] |
| 4041 | unsigned Idx = 0; |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 4042 | Value *Val = nullptr; |
Bill Wendling | f891bf8 | 2011-07-31 06:30:59 +0000 | [diff] [blame] | 4043 | if (getValueTypePair(Record, Idx, NextValueNo, Val)) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4044 | return error("Invalid record"); |
Bill Wendling | f891bf8 | 2011-07-31 06:30:59 +0000 | [diff] [blame] | 4045 | I = ResumeInst::Create(Val); |
Bill Wendling | b9a8999 | 2011-09-01 00:50:20 +0000 | [diff] [blame] | 4046 | InstructionList.push_back(I); |
Bill Wendling | f891bf8 | 2011-07-31 06:30:59 +0000 | [diff] [blame] | 4047 | break; |
| 4048 | } |
Chris Lattner | e53603e | 2007-05-02 04:27:25 +0000 | [diff] [blame] | 4049 | case bitc::FUNC_CODE_INST_UNREACHABLE: // UNREACHABLE |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 4050 | I = new UnreachableInst(Context); |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 4051 | InstructionList.push_back(I); |
Chris Lattner | e53603e | 2007-05-02 04:27:25 +0000 | [diff] [blame] | 4052 | break; |
Chris Lattner | e9759c2 | 2007-05-06 00:21:25 +0000 | [diff] [blame] | 4053 | case bitc::FUNC_CODE_INST_PHI: { // PHI: [ty, val0,bb0, ...] |
Chris Lattner | e14cb88 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 4054 | if (Record.size() < 1 || ((Record.size()-1)&1)) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4055 | return error("Invalid record"); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 4056 | Type *Ty = getTypeByID(Record[0]); |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 4057 | if (!Ty) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4058 | return error("Invalid record"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4059 | |
Jay Foad | 5213134 | 2011-03-30 11:28:46 +0000 | [diff] [blame] | 4060 | PHINode *PN = PHINode::Create(Ty, (Record.size()-1)/2); |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 4061 | InstructionList.push_back(PN); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4062 | |
Chris Lattner | e14cb88 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 4063 | for (unsigned i = 0, e = Record.size()-1; i != e; i += 2) { |
Jan Wen Voung | afaced0 | 2012-10-11 20:20:40 +0000 | [diff] [blame] | 4064 | Value *V; |
| 4065 | // With the new function encoding, it is possible that operands have |
| 4066 | // negative IDs (for forward references). Use a signed VBR |
| 4067 | // representation to keep the encoding small. |
| 4068 | if (UseRelativeIDs) |
| 4069 | V = getValueSigned(Record, 1+i, NextValueNo, Ty); |
| 4070 | else |
| 4071 | V = getValue(Record, 1+i, NextValueNo, Ty); |
Chris Lattner | e14cb88 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 4072 | BasicBlock *BB = getBasicBlock(Record[2+i]); |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 4073 | if (!V || !BB) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4074 | return error("Invalid record"); |
Chris Lattner | c332bba | 2007-05-03 18:58:09 +0000 | [diff] [blame] | 4075 | PN->addIncoming(V, BB); |
| 4076 | } |
| 4077 | I = PN; |
| 4078 | break; |
| 4079 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4080 | |
David Majnemer | 7fddecc | 2015-06-17 20:52:32 +0000 | [diff] [blame] | 4081 | case bitc::FUNC_CODE_INST_LANDINGPAD: |
| 4082 | case bitc::FUNC_CODE_INST_LANDINGPAD_OLD: { |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 4083 | // LANDINGPAD: [ty, val, val, num, (id0,val0 ...)?] |
| 4084 | unsigned Idx = 0; |
David Majnemer | 7fddecc | 2015-06-17 20:52:32 +0000 | [diff] [blame] | 4085 | if (BitCode == bitc::FUNC_CODE_INST_LANDINGPAD) { |
| 4086 | if (Record.size() < 3) |
| 4087 | return error("Invalid record"); |
| 4088 | } else { |
| 4089 | assert(BitCode == bitc::FUNC_CODE_INST_LANDINGPAD_OLD); |
| 4090 | if (Record.size() < 4) |
| 4091 | return error("Invalid record"); |
| 4092 | } |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 4093 | Type *Ty = getTypeByID(Record[Idx++]); |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 4094 | if (!Ty) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4095 | return error("Invalid record"); |
David Majnemer | 7fddecc | 2015-06-17 20:52:32 +0000 | [diff] [blame] | 4096 | if (BitCode == bitc::FUNC_CODE_INST_LANDINGPAD_OLD) { |
| 4097 | Value *PersFn = nullptr; |
| 4098 | if (getValueTypePair(Record, Idx, NextValueNo, PersFn)) |
| 4099 | return error("Invalid record"); |
| 4100 | |
| 4101 | if (!F->hasPersonalityFn()) |
| 4102 | F->setPersonalityFn(cast<Constant>(PersFn)); |
| 4103 | else if (F->getPersonalityFn() != cast<Constant>(PersFn)) |
| 4104 | return error("Personality function mismatch"); |
| 4105 | } |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 4106 | |
| 4107 | bool IsCleanup = !!Record[Idx++]; |
| 4108 | unsigned NumClauses = Record[Idx++]; |
David Majnemer | 7fddecc | 2015-06-17 20:52:32 +0000 | [diff] [blame] | 4109 | LandingPadInst *LP = LandingPadInst::Create(Ty, NumClauses); |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 4110 | LP->setCleanup(IsCleanup); |
| 4111 | for (unsigned J = 0; J != NumClauses; ++J) { |
| 4112 | LandingPadInst::ClauseType CT = |
| 4113 | LandingPadInst::ClauseType(Record[Idx++]); (void)CT; |
| 4114 | Value *Val; |
| 4115 | |
| 4116 | if (getValueTypePair(Record, Idx, NextValueNo, Val)) { |
| 4117 | delete LP; |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4118 | return error("Invalid record"); |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 4119 | } |
| 4120 | |
| 4121 | assert((CT != LandingPadInst::Catch || |
| 4122 | !isa<ArrayType>(Val->getType())) && |
| 4123 | "Catch clause has a invalid type!"); |
| 4124 | assert((CT != LandingPadInst::Filter || |
| 4125 | isa<ArrayType>(Val->getType())) && |
| 4126 | "Filter clause has invalid type!"); |
Rafael Espindola | 4dc5dfc | 2014-06-04 18:51:31 +0000 | [diff] [blame] | 4127 | LP->addClause(cast<Constant>(Val)); |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 4128 | } |
| 4129 | |
| 4130 | I = LP; |
Bill Wendling | b9a8999 | 2011-09-01 00:50:20 +0000 | [diff] [blame] | 4131 | InstructionList.push_back(I); |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 4132 | break; |
| 4133 | } |
| 4134 | |
Chris Lattner | f1c8710 | 2011-06-17 18:09:11 +0000 | [diff] [blame] | 4135 | case bitc::FUNC_CODE_INST_ALLOCA: { // ALLOCA: [instty, opty, op, align] |
| 4136 | if (Record.size() != 4) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4137 | return error("Invalid record"); |
JF Bastien | 30bf96b | 2015-02-22 19:32:03 +0000 | [diff] [blame] | 4138 | uint64_t AlignRecord = Record[3]; |
| 4139 | const uint64_t InAllocaMask = uint64_t(1) << 5; |
David Blaikie | bdb4910 | 2015-04-28 16:51:01 +0000 | [diff] [blame] | 4140 | const uint64_t ExplicitTypeMask = uint64_t(1) << 6; |
Manman Ren | 9bfd0d0 | 2016-04-01 21:41:15 +0000 | [diff] [blame] | 4141 | const uint64_t SwiftErrorMask = uint64_t(1) << 7; |
| 4142 | const uint64_t FlagMask = InAllocaMask | ExplicitTypeMask | |
| 4143 | SwiftErrorMask; |
JF Bastien | 30bf96b | 2015-02-22 19:32:03 +0000 | [diff] [blame] | 4144 | bool InAlloca = AlignRecord & InAllocaMask; |
Manman Ren | 9bfd0d0 | 2016-04-01 21:41:15 +0000 | [diff] [blame] | 4145 | bool SwiftError = AlignRecord & SwiftErrorMask; |
David Blaikie | bdb4910 | 2015-04-28 16:51:01 +0000 | [diff] [blame] | 4146 | Type *Ty = getTypeByID(Record[0]); |
| 4147 | if ((AlignRecord & ExplicitTypeMask) == 0) { |
| 4148 | auto *PTy = dyn_cast_or_null<PointerType>(Ty); |
| 4149 | if (!PTy) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4150 | return error("Old-style alloca with a non-pointer type"); |
David Blaikie | bdb4910 | 2015-04-28 16:51:01 +0000 | [diff] [blame] | 4151 | Ty = PTy->getElementType(); |
| 4152 | } |
| 4153 | Type *OpTy = getTypeByID(Record[1]); |
| 4154 | Value *Size = getFnValueByID(Record[2], OpTy); |
JF Bastien | 30bf96b | 2015-02-22 19:32:03 +0000 | [diff] [blame] | 4155 | unsigned Align; |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 4156 | if (Error Err = parseAlignmentValue(AlignRecord & ~FlagMask, Align)) { |
| 4157 | return Err; |
JF Bastien | 30bf96b | 2015-02-22 19:32:03 +0000 | [diff] [blame] | 4158 | } |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 4159 | if (!Ty || !Size) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4160 | return error("Invalid record"); |
Matt Arsenault | 3c1fc76 | 2017-04-10 22:27:50 +0000 | [diff] [blame] | 4161 | |
| 4162 | // FIXME: Make this an optional field. |
| 4163 | const DataLayout &DL = TheModule->getDataLayout(); |
| 4164 | unsigned AS = DL.getAllocaAddrSpace(); |
| 4165 | |
| 4166 | AllocaInst *AI = new AllocaInst(Ty, AS, Size, Align); |
Reid Kleckner | 56b56ea | 2014-07-16 01:34:27 +0000 | [diff] [blame] | 4167 | AI->setUsedWithInAlloca(InAlloca); |
Manman Ren | 9bfd0d0 | 2016-04-01 21:41:15 +0000 | [diff] [blame] | 4168 | AI->setSwiftError(SwiftError); |
Reid Kleckner | 56b56ea | 2014-07-16 01:34:27 +0000 | [diff] [blame] | 4169 | I = AI; |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 4170 | InstructionList.push_back(I); |
Chris Lattner | c332bba | 2007-05-03 18:58:09 +0000 | [diff] [blame] | 4171 | break; |
| 4172 | } |
Chris Lattner | 9f600c5 | 2007-05-03 22:04:19 +0000 | [diff] [blame] | 4173 | case bitc::FUNC_CODE_INST_LOAD: { // LOAD: [opty, op, align, vol] |
Chris Lattner | df1233d | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 4174 | unsigned OpNum = 0; |
| 4175 | Value *Op; |
| 4176 | if (getValueTypePair(Record, OpNum, NextValueNo, Op) || |
David Blaikie | 8503565 | 2015-02-25 01:07:20 +0000 | [diff] [blame] | 4177 | (OpNum + 2 != Record.size() && OpNum + 3 != Record.size())) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4178 | return error("Invalid record"); |
David Blaikie | 8503565 | 2015-02-25 01:07:20 +0000 | [diff] [blame] | 4179 | |
| 4180 | Type *Ty = nullptr; |
| 4181 | if (OpNum + 3 == Record.size()) |
| 4182 | Ty = getTypeByID(Record[OpNum++]); |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 4183 | if (Error Err = typeCheckLoadStoreInst(Ty, Op->getType())) |
| 4184 | return Err; |
David Blaikie | b7a02987 | 2015-04-17 19:56:21 +0000 | [diff] [blame] | 4185 | if (!Ty) |
| 4186 | Ty = cast<PointerType>(Op->getType())->getElementType(); |
David Blaikie | 8503565 | 2015-02-25 01:07:20 +0000 | [diff] [blame] | 4187 | |
JF Bastien | 30bf96b | 2015-02-22 19:32:03 +0000 | [diff] [blame] | 4188 | unsigned Align; |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 4189 | if (Error Err = parseAlignmentValue(Record[OpNum], Align)) |
| 4190 | return Err; |
David Blaikie | b7a02987 | 2015-04-17 19:56:21 +0000 | [diff] [blame] | 4191 | I = new LoadInst(Ty, Op, "", Record[OpNum + 1], Align); |
David Blaikie | 8503565 | 2015-02-25 01:07:20 +0000 | [diff] [blame] | 4192 | |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 4193 | InstructionList.push_back(I); |
Chris Lattner | 8393055 | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 4194 | break; |
Chris Lattner | 9f600c5 | 2007-05-03 22:04:19 +0000 | [diff] [blame] | 4195 | } |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 4196 | case bitc::FUNC_CODE_INST_LOADATOMIC: { |
| 4197 | // LOADATOMIC: [opty, op, align, vol, ordering, synchscope] |
| 4198 | unsigned OpNum = 0; |
| 4199 | Value *Op; |
| 4200 | if (getValueTypePair(Record, OpNum, NextValueNo, Op) || |
David Blaikie | 8503565 | 2015-02-25 01:07:20 +0000 | [diff] [blame] | 4201 | (OpNum + 4 != Record.size() && OpNum + 5 != Record.size())) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4202 | return error("Invalid record"); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 4203 | |
David Blaikie | 8503565 | 2015-02-25 01:07:20 +0000 | [diff] [blame] | 4204 | Type *Ty = nullptr; |
| 4205 | if (OpNum + 5 == Record.size()) |
| 4206 | Ty = getTypeByID(Record[OpNum++]); |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 4207 | if (Error Err = typeCheckLoadStoreInst(Ty, Op->getType())) |
| 4208 | return Err; |
Filipe Cabecinhas | 11bb849 | 2015-05-18 21:48:55 +0000 | [diff] [blame] | 4209 | if (!Ty) |
| 4210 | Ty = cast<PointerType>(Op->getType())->getElementType(); |
David Blaikie | 8503565 | 2015-02-25 01:07:20 +0000 | [diff] [blame] | 4211 | |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4212 | AtomicOrdering Ordering = getDecodedOrdering(Record[OpNum + 2]); |
JF Bastien | 800f87a | 2016-04-06 21:19:33 +0000 | [diff] [blame] | 4213 | if (Ordering == AtomicOrdering::NotAtomic || |
| 4214 | Ordering == AtomicOrdering::Release || |
| 4215 | Ordering == AtomicOrdering::AcquireRelease) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4216 | return error("Invalid record"); |
JF Bastien | 800f87a | 2016-04-06 21:19:33 +0000 | [diff] [blame] | 4217 | if (Ordering != AtomicOrdering::NotAtomic && Record[OpNum] == 0) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4218 | return error("Invalid record"); |
| 4219 | SynchronizationScope SynchScope = getDecodedSynchScope(Record[OpNum + 3]); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 4220 | |
JF Bastien | 30bf96b | 2015-02-22 19:32:03 +0000 | [diff] [blame] | 4221 | unsigned Align; |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 4222 | if (Error Err = parseAlignmentValue(Record[OpNum], Align)) |
| 4223 | return Err; |
JF Bastien | 30bf96b | 2015-02-22 19:32:03 +0000 | [diff] [blame] | 4224 | I = new LoadInst(Op, "", Record[OpNum+1], Align, Ordering, SynchScope); |
David Blaikie | 8503565 | 2015-02-25 01:07:20 +0000 | [diff] [blame] | 4225 | |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 4226 | InstructionList.push_back(I); |
| 4227 | break; |
| 4228 | } |
David Blaikie | 612ddbf | 2015-04-22 04:14:42 +0000 | [diff] [blame] | 4229 | case bitc::FUNC_CODE_INST_STORE: |
| 4230 | case bitc::FUNC_CODE_INST_STORE_OLD: { // STORE2:[ptrty, ptr, val, align, vol] |
Christopher Lamb | 54dd24c | 2007-12-11 08:59:05 +0000 | [diff] [blame] | 4231 | unsigned OpNum = 0; |
| 4232 | Value *Val, *Ptr; |
| 4233 | if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) || |
David Blaikie | 612ddbf | 2015-04-22 04:14:42 +0000 | [diff] [blame] | 4234 | (BitCode == bitc::FUNC_CODE_INST_STORE |
| 4235 | ? getValueTypePair(Record, OpNum, NextValueNo, Val) |
| 4236 | : popValue(Record, OpNum, NextValueNo, |
| 4237 | cast<PointerType>(Ptr->getType())->getElementType(), |
| 4238 | Val)) || |
| 4239 | OpNum + 2 != Record.size()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4240 | return error("Invalid record"); |
Filipe Cabecinhas | 11bb849 | 2015-05-18 21:48:55 +0000 | [diff] [blame] | 4241 | |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 4242 | if (Error Err = typeCheckLoadStoreInst(Val->getType(), Ptr->getType())) |
| 4243 | return Err; |
JF Bastien | 30bf96b | 2015-02-22 19:32:03 +0000 | [diff] [blame] | 4244 | unsigned Align; |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 4245 | if (Error Err = parseAlignmentValue(Record[OpNum], Align)) |
| 4246 | return Err; |
JF Bastien | 30bf96b | 2015-02-22 19:32:03 +0000 | [diff] [blame] | 4247 | I = new StoreInst(Val, Ptr, Record[OpNum+1], Align); |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 4248 | InstructionList.push_back(I); |
Christopher Lamb | 54dd24c | 2007-12-11 08:59:05 +0000 | [diff] [blame] | 4249 | break; |
| 4250 | } |
David Blaikie | 50a0615 | 2015-04-22 04:14:46 +0000 | [diff] [blame] | 4251 | case bitc::FUNC_CODE_INST_STOREATOMIC: |
| 4252 | case bitc::FUNC_CODE_INST_STOREATOMIC_OLD: { |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 4253 | // STOREATOMIC: [ptrty, ptr, val, align, vol, ordering, synchscope] |
| 4254 | unsigned OpNum = 0; |
| 4255 | Value *Val, *Ptr; |
| 4256 | if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) || |
Filipe Cabecinhas | 036e73c | 2016-06-05 18:43:33 +0000 | [diff] [blame] | 4257 | !isa<PointerType>(Ptr->getType()) || |
David Blaikie | 50a0615 | 2015-04-22 04:14:46 +0000 | [diff] [blame] | 4258 | (BitCode == bitc::FUNC_CODE_INST_STOREATOMIC |
| 4259 | ? getValueTypePair(Record, OpNum, NextValueNo, Val) |
| 4260 | : popValue(Record, OpNum, NextValueNo, |
| 4261 | cast<PointerType>(Ptr->getType())->getElementType(), |
| 4262 | Val)) || |
| 4263 | OpNum + 4 != Record.size()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4264 | return error("Invalid record"); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 4265 | |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 4266 | if (Error Err = typeCheckLoadStoreInst(Val->getType(), Ptr->getType())) |
| 4267 | return Err; |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4268 | AtomicOrdering Ordering = getDecodedOrdering(Record[OpNum + 2]); |
JF Bastien | 800f87a | 2016-04-06 21:19:33 +0000 | [diff] [blame] | 4269 | if (Ordering == AtomicOrdering::NotAtomic || |
| 4270 | Ordering == AtomicOrdering::Acquire || |
| 4271 | Ordering == AtomicOrdering::AcquireRelease) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4272 | return error("Invalid record"); |
| 4273 | SynchronizationScope SynchScope = getDecodedSynchScope(Record[OpNum + 3]); |
JF Bastien | 800f87a | 2016-04-06 21:19:33 +0000 | [diff] [blame] | 4274 | if (Ordering != AtomicOrdering::NotAtomic && Record[OpNum] == 0) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4275 | return error("Invalid record"); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 4276 | |
JF Bastien | 30bf96b | 2015-02-22 19:32:03 +0000 | [diff] [blame] | 4277 | unsigned Align; |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 4278 | if (Error Err = parseAlignmentValue(Record[OpNum], Align)) |
| 4279 | return Err; |
JF Bastien | 30bf96b | 2015-02-22 19:32:03 +0000 | [diff] [blame] | 4280 | I = new StoreInst(Val, Ptr, Record[OpNum+1], Align, Ordering, SynchScope); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 4281 | InstructionList.push_back(I); |
| 4282 | break; |
| 4283 | } |
David Blaikie | 2a661cd | 2015-04-28 04:30:29 +0000 | [diff] [blame] | 4284 | case bitc::FUNC_CODE_INST_CMPXCHG_OLD: |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 4285 | case bitc::FUNC_CODE_INST_CMPXCHG: { |
Tim Northover | e94a518 | 2014-03-11 10:48:52 +0000 | [diff] [blame] | 4286 | // CMPXCHG:[ptrty, ptr, cmp, new, vol, successordering, synchscope, |
Tim Northover | 420a216 | 2014-06-13 14:24:07 +0000 | [diff] [blame] | 4287 | // failureordering?, isweak?] |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 4288 | unsigned OpNum = 0; |
| 4289 | Value *Ptr, *Cmp, *New; |
| 4290 | if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) || |
David Blaikie | 2a661cd | 2015-04-28 04:30:29 +0000 | [diff] [blame] | 4291 | (BitCode == bitc::FUNC_CODE_INST_CMPXCHG |
| 4292 | ? getValueTypePair(Record, OpNum, NextValueNo, Cmp) |
| 4293 | : popValue(Record, OpNum, NextValueNo, |
| 4294 | cast<PointerType>(Ptr->getType())->getElementType(), |
| 4295 | Cmp)) || |
| 4296 | popValue(Record, OpNum, NextValueNo, Cmp->getType(), New) || |
| 4297 | Record.size() < OpNum + 3 || Record.size() > OpNum + 5) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4298 | return error("Invalid record"); |
| 4299 | AtomicOrdering SuccessOrdering = getDecodedOrdering(Record[OpNum + 1]); |
JF Bastien | 800f87a | 2016-04-06 21:19:33 +0000 | [diff] [blame] | 4300 | if (SuccessOrdering == AtomicOrdering::NotAtomic || |
| 4301 | SuccessOrdering == AtomicOrdering::Unordered) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4302 | return error("Invalid record"); |
| 4303 | SynchronizationScope SynchScope = getDecodedSynchScope(Record[OpNum + 2]); |
Tim Northover | e94a518 | 2014-03-11 10:48:52 +0000 | [diff] [blame] | 4304 | |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 4305 | if (Error Err = typeCheckLoadStoreInst(Cmp->getType(), Ptr->getType())) |
| 4306 | return Err; |
Tim Northover | e94a518 | 2014-03-11 10:48:52 +0000 | [diff] [blame] | 4307 | AtomicOrdering FailureOrdering; |
| 4308 | if (Record.size() < 7) |
| 4309 | FailureOrdering = |
| 4310 | AtomicCmpXchgInst::getStrongestFailureOrdering(SuccessOrdering); |
| 4311 | else |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4312 | FailureOrdering = getDecodedOrdering(Record[OpNum + 3]); |
Tim Northover | e94a518 | 2014-03-11 10:48:52 +0000 | [diff] [blame] | 4313 | |
| 4314 | I = new AtomicCmpXchgInst(Ptr, Cmp, New, SuccessOrdering, FailureOrdering, |
| 4315 | SynchScope); |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 4316 | cast<AtomicCmpXchgInst>(I)->setVolatile(Record[OpNum]); |
Tim Northover | 420a216 | 2014-06-13 14:24:07 +0000 | [diff] [blame] | 4317 | |
| 4318 | if (Record.size() < 8) { |
| 4319 | // Before weak cmpxchgs existed, the instruction simply returned the |
| 4320 | // value loaded from memory, so bitcode files from that era will be |
| 4321 | // expecting the first component of a modern cmpxchg. |
| 4322 | CurBB->getInstList().push_back(I); |
| 4323 | I = ExtractValueInst::Create(I, 0); |
| 4324 | } else { |
| 4325 | cast<AtomicCmpXchgInst>(I)->setWeak(Record[OpNum+4]); |
| 4326 | } |
| 4327 | |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 4328 | InstructionList.push_back(I); |
| 4329 | break; |
| 4330 | } |
| 4331 | case bitc::FUNC_CODE_INST_ATOMICRMW: { |
| 4332 | // ATOMICRMW:[ptrty, ptr, val, op, vol, ordering, synchscope] |
| 4333 | unsigned OpNum = 0; |
| 4334 | Value *Ptr, *Val; |
| 4335 | if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) || |
Filipe Cabecinhas | 6328f8e | 2016-06-05 18:43:40 +0000 | [diff] [blame] | 4336 | !isa<PointerType>(Ptr->getType()) || |
Jan Wen Voung | afaced0 | 2012-10-11 20:20:40 +0000 | [diff] [blame] | 4337 | popValue(Record, OpNum, NextValueNo, |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 4338 | cast<PointerType>(Ptr->getType())->getElementType(), Val) || |
| 4339 | OpNum+4 != Record.size()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4340 | return error("Invalid record"); |
| 4341 | AtomicRMWInst::BinOp Operation = getDecodedRMWOperation(Record[OpNum]); |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 4342 | if (Operation < AtomicRMWInst::FIRST_BINOP || |
| 4343 | Operation > AtomicRMWInst::LAST_BINOP) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4344 | return error("Invalid record"); |
| 4345 | AtomicOrdering Ordering = getDecodedOrdering(Record[OpNum + 2]); |
JF Bastien | 800f87a | 2016-04-06 21:19:33 +0000 | [diff] [blame] | 4346 | if (Ordering == AtomicOrdering::NotAtomic || |
| 4347 | Ordering == AtomicOrdering::Unordered) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4348 | return error("Invalid record"); |
| 4349 | SynchronizationScope SynchScope = getDecodedSynchScope(Record[OpNum + 3]); |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 4350 | I = new AtomicRMWInst(Operation, Ptr, Val, Ordering, SynchScope); |
| 4351 | cast<AtomicRMWInst>(I)->setVolatile(Record[OpNum+1]); |
| 4352 | InstructionList.push_back(I); |
| 4353 | break; |
| 4354 | } |
Eli Friedman | fee02c6 | 2011-07-25 23:16:38 +0000 | [diff] [blame] | 4355 | case bitc::FUNC_CODE_INST_FENCE: { // FENCE:[ordering, synchscope] |
| 4356 | if (2 != Record.size()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4357 | return error("Invalid record"); |
| 4358 | AtomicOrdering Ordering = getDecodedOrdering(Record[0]); |
JF Bastien | 800f87a | 2016-04-06 21:19:33 +0000 | [diff] [blame] | 4359 | if (Ordering == AtomicOrdering::NotAtomic || |
| 4360 | Ordering == AtomicOrdering::Unordered || |
| 4361 | Ordering == AtomicOrdering::Monotonic) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4362 | return error("Invalid record"); |
| 4363 | SynchronizationScope SynchScope = getDecodedSynchScope(Record[1]); |
Eli Friedman | fee02c6 | 2011-07-25 23:16:38 +0000 | [diff] [blame] | 4364 | I = new FenceInst(Context, Ordering, SynchScope); |
| 4365 | InstructionList.push_back(I); |
| 4366 | break; |
| 4367 | } |
Chris Lattner | c4407080 | 2011-06-17 18:17:37 +0000 | [diff] [blame] | 4368 | case bitc::FUNC_CODE_INST_CALL: { |
Sanjay Patel | fa54ace | 2015-12-14 21:59:03 +0000 | [diff] [blame] | 4369 | // CALL: [paramattrs, cc, fmf, fnty, fnid, arg0, arg1...] |
Duncan Sands | ad0ea2d | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 4370 | if (Record.size() < 3) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4371 | return error("Invalid record"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4372 | |
David Blaikie | dbe6e0f | 2015-04-17 06:40:14 +0000 | [diff] [blame] | 4373 | unsigned OpNum = 0; |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 4374 | AttributeList PAL = getAttributes(Record[OpNum++]); |
David Blaikie | dbe6e0f | 2015-04-17 06:40:14 +0000 | [diff] [blame] | 4375 | unsigned CCInfo = Record[OpNum++]; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4376 | |
Sanjay Patel | fa54ace | 2015-12-14 21:59:03 +0000 | [diff] [blame] | 4377 | FastMathFlags FMF; |
| 4378 | if ((CCInfo >> bitc::CALL_FMF) & 1) { |
| 4379 | FMF = getDecodedFastMathFlags(Record[OpNum++]); |
| 4380 | if (!FMF.any()) |
| 4381 | return error("Fast math flags indicator set for call with no FMF"); |
| 4382 | } |
| 4383 | |
David Blaikie | dbe6e0f | 2015-04-17 06:40:14 +0000 | [diff] [blame] | 4384 | FunctionType *FTy = nullptr; |
Akira Hatanaka | 97cb397 | 2015-11-07 02:48:49 +0000 | [diff] [blame] | 4385 | if (CCInfo >> bitc::CALL_EXPLICIT_TYPE & 1 && |
David Blaikie | dbe6e0f | 2015-04-17 06:40:14 +0000 | [diff] [blame] | 4386 | !(FTy = dyn_cast<FunctionType>(getTypeByID(Record[OpNum++])))) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4387 | return error("Explicit call type is not a function type"); |
David Blaikie | dbe6e0f | 2015-04-17 06:40:14 +0000 | [diff] [blame] | 4388 | |
Chris Lattner | df1233d | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 4389 | Value *Callee; |
| 4390 | if (getValueTypePair(Record, OpNum, NextValueNo, Callee)) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4391 | return error("Invalid record"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4392 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 4393 | PointerType *OpTy = dyn_cast<PointerType>(Callee->getType()); |
David Blaikie | dbe6e0f | 2015-04-17 06:40:14 +0000 | [diff] [blame] | 4394 | if (!OpTy) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4395 | return error("Callee is not a pointer type"); |
David Blaikie | 348de69 | 2015-04-23 21:36:23 +0000 | [diff] [blame] | 4396 | if (!FTy) { |
| 4397 | FTy = dyn_cast<FunctionType>(OpTy->getElementType()); |
| 4398 | if (!FTy) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4399 | return error("Callee is not of pointer to function type"); |
David Blaikie | 348de69 | 2015-04-23 21:36:23 +0000 | [diff] [blame] | 4400 | } else if (OpTy->getElementType() != FTy) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4401 | return error("Explicit call type does not match pointee type of " |
David Blaikie | dbe6e0f | 2015-04-17 06:40:14 +0000 | [diff] [blame] | 4402 | "callee operand"); |
| 4403 | if (Record.size() < FTy->getNumParams() + OpNum) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4404 | return error("Insufficient operands to call"); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4405 | |
Chris Lattner | 9f600c5 | 2007-05-03 22:04:19 +0000 | [diff] [blame] | 4406 | SmallVector<Value*, 16> Args; |
| 4407 | // Read the fixed params. |
Chris Lattner | df1233d | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 4408 | for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i, ++OpNum) { |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 4409 | if (FTy->getParamType(i)->isLabelTy()) |
Dale Johannesen | 4646aa3 | 2007-11-05 21:20:28 +0000 | [diff] [blame] | 4410 | Args.push_back(getBasicBlock(Record[OpNum])); |
Dan Gohman | bbcd04d | 2010-09-13 18:00:48 +0000 | [diff] [blame] | 4411 | else |
Jan Wen Voung | afaced0 | 2012-10-11 20:20:40 +0000 | [diff] [blame] | 4412 | Args.push_back(getValue(Record, OpNum, NextValueNo, |
| 4413 | FTy->getParamType(i))); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 4414 | if (!Args.back()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4415 | return error("Invalid record"); |
Chris Lattner | 9f600c5 | 2007-05-03 22:04:19 +0000 | [diff] [blame] | 4416 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4417 | |
Chris Lattner | 9f600c5 | 2007-05-03 22:04:19 +0000 | [diff] [blame] | 4418 | // Read type/value pairs for varargs params. |
Chris Lattner | 9f600c5 | 2007-05-03 22:04:19 +0000 | [diff] [blame] | 4419 | if (!FTy->isVarArg()) { |
Chris Lattner | df1233d | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 4420 | if (OpNum != Record.size()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4421 | return error("Invalid record"); |
Chris Lattner | 9f600c5 | 2007-05-03 22:04:19 +0000 | [diff] [blame] | 4422 | } else { |
Chris Lattner | df1233d | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 4423 | while (OpNum != Record.size()) { |
| 4424 | Value *Op; |
| 4425 | if (getValueTypePair(Record, OpNum, NextValueNo, Op)) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4426 | return error("Invalid record"); |
Chris Lattner | df1233d | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 4427 | Args.push_back(Op); |
Chris Lattner | 9f600c5 | 2007-05-03 22:04:19 +0000 | [diff] [blame] | 4428 | } |
| 4429 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4430 | |
Sanjoy Das | b513a9f | 2015-09-24 23:34:52 +0000 | [diff] [blame] | 4431 | I = CallInst::Create(FTy, Callee, Args, OperandBundles); |
| 4432 | OperandBundles.clear(); |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 4433 | InstructionList.push_back(I); |
Sandeep Patel | 68c5f47 | 2009-09-02 08:44:58 +0000 | [diff] [blame] | 4434 | cast<CallInst>(I)->setCallingConv( |
Akira Hatanaka | 97cb397 | 2015-11-07 02:48:49 +0000 | [diff] [blame] | 4435 | static_cast<CallingConv::ID>((0x7ff & CCInfo) >> bitc::CALL_CCONV)); |
Reid Kleckner | 5772b77 | 2014-04-24 20:14:34 +0000 | [diff] [blame] | 4436 | CallInst::TailCallKind TCK = CallInst::TCK_None; |
Akira Hatanaka | 97cb397 | 2015-11-07 02:48:49 +0000 | [diff] [blame] | 4437 | if (CCInfo & 1 << bitc::CALL_TAIL) |
Reid Kleckner | 5772b77 | 2014-04-24 20:14:34 +0000 | [diff] [blame] | 4438 | TCK = CallInst::TCK_Tail; |
Akira Hatanaka | 97cb397 | 2015-11-07 02:48:49 +0000 | [diff] [blame] | 4439 | if (CCInfo & (1 << bitc::CALL_MUSTTAIL)) |
Reid Kleckner | 5772b77 | 2014-04-24 20:14:34 +0000 | [diff] [blame] | 4440 | TCK = CallInst::TCK_MustTail; |
Akira Hatanaka | 97cb397 | 2015-11-07 02:48:49 +0000 | [diff] [blame] | 4441 | if (CCInfo & (1 << bitc::CALL_NOTAIL)) |
Akira Hatanaka | 5cfcce12 | 2015-11-06 23:55:38 +0000 | [diff] [blame] | 4442 | TCK = CallInst::TCK_NoTail; |
Reid Kleckner | 5772b77 | 2014-04-24 20:14:34 +0000 | [diff] [blame] | 4443 | cast<CallInst>(I)->setTailCallKind(TCK); |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 4444 | cast<CallInst>(I)->setAttributes(PAL); |
Sanjay Patel | fa54ace | 2015-12-14 21:59:03 +0000 | [diff] [blame] | 4445 | if (FMF.any()) { |
| 4446 | if (!isa<FPMathOperator>(I)) |
| 4447 | return error("Fast-math-flags specified for call without " |
| 4448 | "floating-point scalar or vector return type"); |
| 4449 | I->setFastMathFlags(FMF); |
| 4450 | } |
Chris Lattner | 9f600c5 | 2007-05-03 22:04:19 +0000 | [diff] [blame] | 4451 | break; |
| 4452 | } |
| 4453 | case bitc::FUNC_CODE_INST_VAARG: { // VAARG: [valistty, valist, instty] |
| 4454 | if (Record.size() < 3) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4455 | return error("Invalid record"); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 4456 | Type *OpTy = getTypeByID(Record[0]); |
Jan Wen Voung | afaced0 | 2012-10-11 20:20:40 +0000 | [diff] [blame] | 4457 | Value *Op = getValue(Record, 1, NextValueNo, OpTy); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 4458 | Type *ResTy = getTypeByID(Record[2]); |
Chris Lattner | 9f600c5 | 2007-05-03 22:04:19 +0000 | [diff] [blame] | 4459 | if (!OpTy || !Op || !ResTy) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4460 | return error("Invalid record"); |
Chris Lattner | 9f600c5 | 2007-05-03 22:04:19 +0000 | [diff] [blame] | 4461 | I = new VAArgInst(Op, ResTy); |
Devang Patel | af206b8 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 4462 | InstructionList.push_back(I); |
Chris Lattner | 9f600c5 | 2007-05-03 22:04:19 +0000 | [diff] [blame] | 4463 | break; |
| 4464 | } |
Sanjoy Das | b513a9f | 2015-09-24 23:34:52 +0000 | [diff] [blame] | 4465 | |
| 4466 | case bitc::FUNC_CODE_OPERAND_BUNDLE: { |
| 4467 | // A call or an invoke can be optionally prefixed with some variable |
| 4468 | // number of operand bundle blocks. These blocks are read into |
| 4469 | // OperandBundles and consumed at the next call or invoke instruction. |
| 4470 | |
| 4471 | if (Record.size() < 1 || Record[0] >= BundleTags.size()) |
| 4472 | return error("Invalid record"); |
| 4473 | |
Sanjoy Das | f79d344 | 2015-11-18 08:30:07 +0000 | [diff] [blame] | 4474 | std::vector<Value *> Inputs; |
Sanjoy Das | b513a9f | 2015-09-24 23:34:52 +0000 | [diff] [blame] | 4475 | |
| 4476 | unsigned OpNum = 1; |
| 4477 | while (OpNum != Record.size()) { |
| 4478 | Value *Op; |
| 4479 | if (getValueTypePair(Record, OpNum, NextValueNo, Op)) |
| 4480 | return error("Invalid record"); |
| 4481 | Inputs.push_back(Op); |
| 4482 | } |
| 4483 | |
Sanjoy Das | f79d344 | 2015-11-18 08:30:07 +0000 | [diff] [blame] | 4484 | OperandBundles.emplace_back(BundleTags[Record[0]], std::move(Inputs)); |
Sanjoy Das | b513a9f | 2015-09-24 23:34:52 +0000 | [diff] [blame] | 4485 | continue; |
| 4486 | } |
Chris Lattner | 8393055 | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 4487 | } |
| 4488 | |
| 4489 | // Add instruction to end of current BB. If there is no current BB, reject |
| 4490 | // this file. |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 4491 | if (!CurBB) { |
Reid Kleckner | 96ab872 | 2017-05-18 17:24:10 +0000 | [diff] [blame] | 4492 | I->deleteValue(); |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4493 | return error("Invalid instruction with no BB"); |
Chris Lattner | 8393055 | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 4494 | } |
Sanjoy Das | b513a9f | 2015-09-24 23:34:52 +0000 | [diff] [blame] | 4495 | if (!OperandBundles.empty()) { |
Reid Kleckner | 96ab872 | 2017-05-18 17:24:10 +0000 | [diff] [blame] | 4496 | I->deleteValue(); |
Sanjoy Das | b513a9f | 2015-09-24 23:34:52 +0000 | [diff] [blame] | 4497 | return error("Operand bundles found with no consumer"); |
| 4498 | } |
Chris Lattner | 8393055 | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 4499 | CurBB->getInstList().push_back(I); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4500 | |
Chris Lattner | 8393055 | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 4501 | // If this was a terminator instruction, move to the next block. |
| 4502 | if (isa<TerminatorInst>(I)) { |
| 4503 | ++CurBBNo; |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 4504 | CurBB = CurBBNo < FunctionBBs.size() ? FunctionBBs[CurBBNo] : nullptr; |
Chris Lattner | 8393055 | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 4505 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4506 | |
Chris Lattner | 8393055 | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 4507 | // Non-void values get registered in the value table for future use. |
Benjamin Kramer | ccce8ba | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 4508 | if (I && !I->getType()->isVoidTy()) |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 4509 | ValueList.assignValue(I, NextValueNo++); |
Chris Lattner | 85b7b40 | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 4510 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4511 | |
Chris Lattner | 27d3875 | 2013-01-20 02:13:19 +0000 | [diff] [blame] | 4512 | OutOfRecordLoop: |
Joe Abbey | 97b7a17 | 2013-02-06 22:14:06 +0000 | [diff] [blame] | 4513 | |
Sanjoy Das | b513a9f | 2015-09-24 23:34:52 +0000 | [diff] [blame] | 4514 | if (!OperandBundles.empty()) |
| 4515 | return error("Operand bundles found with no consumer"); |
| 4516 | |
Chris Lattner | 8393055 | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 4517 | // Check the function list for unresolved values. |
| 4518 | if (Argument *A = dyn_cast<Argument>(ValueList.back())) { |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 4519 | if (!A->getParent()) { |
Chris Lattner | 8393055 | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 4520 | // We found at least one unresolved value. Nuke them all to avoid leaks. |
| 4521 | for (unsigned i = ModuleValueListSize, e = ValueList.size(); i != e; ++i){ |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 4522 | if ((A = dyn_cast_or_null<Argument>(ValueList[i])) && !A->getParent()) { |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 4523 | A->replaceAllUsesWith(UndefValue::get(A->getType())); |
Chris Lattner | 8393055 | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 4524 | delete A; |
| 4525 | } |
| 4526 | } |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4527 | return error("Never resolved value found in function"); |
Chris Lattner | 8393055 | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 4528 | } |
Chris Lattner | 8393055 | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 4529 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4530 | |
Duncan P. N. Exon Smith | 8742de9 | 2016-04-02 14:55:01 +0000 | [diff] [blame] | 4531 | // Unexpected unresolved metadata about to be dropped. |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 4532 | if (MDLoader->hasFwdRefs()) |
Duncan P. N. Exon Smith | 8742de9 | 2016-04-02 14:55:01 +0000 | [diff] [blame] | 4533 | return error("Invalid function metadata: outgoing forward refs"); |
Dan Gohman | 9b9ff46 | 2010-08-25 20:23:38 +0000 | [diff] [blame] | 4534 | |
Chris Lattner | 85b7b40 | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 4535 | // Trim the value list down to the size it was before we parsed this function. |
| 4536 | ValueList.shrinkTo(ModuleValueListSize); |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 4537 | MDLoader->shrinkTo(ModuleMDLoaderSize); |
Chris Lattner | 85b7b40 | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 4538 | std::vector<BasicBlock*>().swap(FunctionBBs); |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 4539 | return Error::success(); |
Chris Lattner | 51ffe7c | 2007-05-01 04:59:48 +0000 | [diff] [blame] | 4540 | } |
| 4541 | |
Rafael Espindola | 7d71203 | 2013-11-05 17:16:08 +0000 | [diff] [blame] | 4542 | /// Find the function body in the bitcode stream |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 4543 | Error BitcodeReader::findFunctionInStream( |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 4544 | Function *F, |
| 4545 | DenseMap<Function *, uint64_t>::iterator DeferredFunctionInfoIterator) { |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 4546 | while (DeferredFunctionInfoIterator->second == 0) { |
Teresa Johnson | ff642b9 | 2015-09-17 20:12:00 +0000 | [diff] [blame] | 4547 | // This is the fallback handling for the old format bitcode that |
Teresa Johnson | 1493ad9 | 2015-10-10 14:18:36 +0000 | [diff] [blame] | 4548 | // didn't contain the function index in the VST, or when we have |
| 4549 | // an anonymous function which would not have a VST entry. |
| 4550 | // Assert that we have one of those two cases. |
| 4551 | assert(VSTOffset == 0 || !F->hasName()); |
| 4552 | // Parse the next body in the stream and set its position in the |
| 4553 | // DeferredFunctionInfo map. |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 4554 | if (Error Err = rememberAndSkipFunctionBodies()) |
| 4555 | return Err; |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 4556 | } |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 4557 | return Error::success(); |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 4558 | } |
| 4559 | |
Chris Lattner | 9eeada9 | 2007-05-18 04:02:46 +0000 | [diff] [blame] | 4560 | //===----------------------------------------------------------------------===// |
Jeffrey Yasskin | 091217b | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 4561 | // GVMaterializer implementation |
Chris Lattner | 9eeada9 | 2007-05-18 04:02:46 +0000 | [diff] [blame] | 4562 | //===----------------------------------------------------------------------===// |
| 4563 | |
Peter Collingbourne | 7f00d0a | 2016-11-09 17:49:19 +0000 | [diff] [blame] | 4564 | Error BitcodeReader::materialize(GlobalValue *GV) { |
Jeffrey Yasskin | 091217b | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 4565 | Function *F = dyn_cast<Function>(GV); |
| 4566 | // If it's not a function or is already material, ignore the request. |
Rafael Espindola | 2b11ad4 | 2013-11-05 19:36:34 +0000 | [diff] [blame] | 4567 | if (!F || !F->isMaterializable()) |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 4568 | return Error::success(); |
Jeffrey Yasskin | 091217b | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 4569 | |
| 4570 | DenseMap<Function*, uint64_t>::iterator DFII = DeferredFunctionInfo.find(F); |
Chris Lattner | 9eeada9 | 2007-05-18 04:02:46 +0000 | [diff] [blame] | 4571 | assert(DFII != DeferredFunctionInfo.end() && "Deferred function not found!"); |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 4572 | // If its position is recorded as 0, its body is somewhere in the stream |
| 4573 | // but we haven't seen it yet. |
Rafael Espindola | 1c863ca | 2015-06-22 18:06:15 +0000 | [diff] [blame] | 4574 | if (DFII->second == 0) |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 4575 | if (Error Err = findFunctionInStream(F, DFII)) |
| 4576 | return Err; |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4577 | |
Duncan P. N. Exon Smith | 03a04a5 | 2016-04-24 15:04:28 +0000 | [diff] [blame] | 4578 | // Materialize metadata before parsing any function bodies. |
Peter Collingbourne | 7f00d0a | 2016-11-09 17:49:19 +0000 | [diff] [blame] | 4579 | if (Error Err = materializeMetadata()) |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 4580 | return Err; |
Duncan P. N. Exon Smith | 03a04a5 | 2016-04-24 15:04:28 +0000 | [diff] [blame] | 4581 | |
Jeffrey Yasskin | 091217b | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 4582 | // Move the bit stream to the saved position of the deferred function body. |
| 4583 | Stream.JumpToBit(DFII->second); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4584 | |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 4585 | if (Error Err = parseFunctionBody(F)) |
| 4586 | return Err; |
Rafael Espindola | d4bcefc | 2014-10-24 18:13:04 +0000 | [diff] [blame] | 4587 | F->setIsMaterializable(false); |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 4588 | |
Rafael Espindola | 0d68b4c | 2015-03-30 21:36:43 +0000 | [diff] [blame] | 4589 | if (StripDebugInfo) |
| 4590 | stripDebugInfo(*F); |
| 4591 | |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 4592 | // Upgrade any old intrinsic calls in the function. |
Rafael Espindola | 86e3340 | 2015-07-02 15:55:09 +0000 | [diff] [blame] | 4593 | for (auto &I : UpgradedIntrinsics) { |
Rafael Espindola | 257a353 | 2016-01-15 19:00:20 +0000 | [diff] [blame] | 4594 | for (auto UI = I.first->materialized_user_begin(), UE = I.first->user_end(); |
| 4595 | UI != UE;) { |
Filipe Cabecinhas | 0011c58 | 2015-07-03 20:12:01 +0000 | [diff] [blame] | 4596 | User *U = *UI; |
| 4597 | ++UI; |
| 4598 | if (CallInst *CI = dyn_cast<CallInst>(U)) |
| 4599 | UpgradeIntrinsicCall(CI, I.second); |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 4600 | } |
| 4601 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4602 | |
Artur Pilipenko | 6c7a8ab | 2016-06-24 15:10:29 +0000 | [diff] [blame] | 4603 | // Update calls to the remangled intrinsics |
| 4604 | for (auto &I : RemangledIntrinsics) |
| 4605 | for (auto UI = I.first->materialized_user_begin(), UE = I.first->user_end(); |
| 4606 | UI != UE;) |
| 4607 | // Don't expect any other users than call sites |
| 4608 | CallSite(*UI++).setCalledFunction(I.second); |
| 4609 | |
Peter Collingbourne | d4bff30 | 2015-11-05 22:03:56 +0000 | [diff] [blame] | 4610 | // Finish fn->subprogram upgrade for materialized functions. |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 4611 | if (DISubprogram *SP = MDLoader->lookupSubprogramForFunction(F)) |
Peter Collingbourne | d4bff30 | 2015-11-05 22:03:56 +0000 | [diff] [blame] | 4612 | F->setSubprogram(SP); |
| 4613 | |
Mehdi Amini | 8662305 | 2016-12-16 19:16:29 +0000 | [diff] [blame] | 4614 | // Check if the TBAA Metadata are valid, otherwise we will need to strip them. |
| 4615 | if (!MDLoader->isStrippingTBAA()) { |
| 4616 | for (auto &I : instructions(F)) { |
| 4617 | MDNode *TBAA = I.getMetadata(LLVMContext::MD_tbaa); |
| 4618 | if (!TBAA || TBAAVerifyHelper.visitTBAAMetadata(I, TBAA)) |
| 4619 | continue; |
| 4620 | MDLoader->setStripTBAA(true); |
| 4621 | stripTBAA(F->getParent()); |
| 4622 | } |
| 4623 | } |
| 4624 | |
Duncan P. N. Exon Smith | 908d809 | 2014-08-01 21:11:34 +0000 | [diff] [blame] | 4625 | // Bring in any functions that this function forward-referenced via |
| 4626 | // blockaddresses. |
| 4627 | return materializeForwardReferencedFunctions(); |
Chris Lattner | 9eeada9 | 2007-05-18 04:02:46 +0000 | [diff] [blame] | 4628 | } |
| 4629 | |
Peter Collingbourne | 7f00d0a | 2016-11-09 17:49:19 +0000 | [diff] [blame] | 4630 | Error BitcodeReader::materializeModule() { |
| 4631 | if (Error Err = materializeMetadata()) |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 4632 | return Err; |
Manman Ren | 4a9b0eb | 2015-03-13 19:24:30 +0000 | [diff] [blame] | 4633 | |
Duncan P. N. Exon Smith | 908d809 | 2014-08-01 21:11:34 +0000 | [diff] [blame] | 4634 | // Promise to materialize all forward references. |
| 4635 | WillMaterializeAllForwardRefs = true; |
| 4636 | |
Chris Lattner | 06310bf | 2009-06-16 05:15:21 +0000 | [diff] [blame] | 4637 | // Iterate over the module, deserializing any functions that are still on |
| 4638 | // disk. |
Duncan P. N. Exon Smith | fb1743a3 | 2015-10-13 16:48:55 +0000 | [diff] [blame] | 4639 | for (Function &F : *TheModule) { |
Peter Collingbourne | 7f00d0a | 2016-11-09 17:49:19 +0000 | [diff] [blame] | 4640 | if (Error Err = materialize(&F)) |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 4641 | return Err; |
Rafael Espindola | 2b11ad4 | 2013-11-05 19:36:34 +0000 | [diff] [blame] | 4642 | } |
Teresa Johnson | 1493ad9 | 2015-10-10 14:18:36 +0000 | [diff] [blame] | 4643 | // At this point, if there are any function bodies, parse the rest of |
| 4644 | // the bits in the module past the last function block we have recorded |
| 4645 | // through either lazy scanning or the VST. |
| 4646 | if (LastFunctionBlockBit || NextUnreadBit) |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 4647 | if (Error Err = parseModule(LastFunctionBlockBit > NextUnreadBit |
| 4648 | ? LastFunctionBlockBit |
| 4649 | : NextUnreadBit)) |
| 4650 | return Err; |
Derek Schuff | 92ef975 | 2012-02-29 00:07:09 +0000 | [diff] [blame] | 4651 | |
Duncan P. N. Exon Smith | 908d809 | 2014-08-01 21:11:34 +0000 | [diff] [blame] | 4652 | // Check that all block address forward references got resolved (as we |
| 4653 | // promised above). |
Duncan P. N. Exon Smith | 00f20ac | 2014-08-01 21:51:52 +0000 | [diff] [blame] | 4654 | if (!BasicBlockFwdRefs.empty()) |
Rafael Espindola | cbdcb50 | 2015-06-15 20:55:37 +0000 | [diff] [blame] | 4655 | return error("Never resolved function from blockaddress"); |
Duncan P. N. Exon Smith | 908d809 | 2014-08-01 21:11:34 +0000 | [diff] [blame] | 4656 | |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 4657 | // Upgrade any intrinsic calls that slipped through (should not happen!) and |
| 4658 | // delete the old functions to clean up. We can't do this unless the entire |
| 4659 | // module is materialized because there could always be another function body |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 4660 | // with calls to the old function. |
Rafael Espindola | 86e3340 | 2015-07-02 15:55:09 +0000 | [diff] [blame] | 4661 | for (auto &I : UpgradedIntrinsics) { |
Filipe Cabecinhas | 0011c58 | 2015-07-03 20:12:01 +0000 | [diff] [blame] | 4662 | for (auto *U : I.first->users()) { |
| 4663 | if (CallInst *CI = dyn_cast<CallInst>(U)) |
| 4664 | UpgradeIntrinsicCall(CI, I.second); |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 4665 | } |
Filipe Cabecinhas | 0011c58 | 2015-07-03 20:12:01 +0000 | [diff] [blame] | 4666 | if (!I.first->use_empty()) |
| 4667 | I.first->replaceAllUsesWith(I.second); |
| 4668 | I.first->eraseFromParent(); |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 4669 | } |
Rafael Espindola | 4e72121 | 2015-07-02 16:22:40 +0000 | [diff] [blame] | 4670 | UpgradedIntrinsics.clear(); |
Artur Pilipenko | 6c7a8ab | 2016-06-24 15:10:29 +0000 | [diff] [blame] | 4671 | // Do the same for remangled intrinsics |
| 4672 | for (auto &I : RemangledIntrinsics) { |
| 4673 | I.first->replaceAllUsesWith(I.second); |
| 4674 | I.first->eraseFromParent(); |
| 4675 | } |
| 4676 | RemangledIntrinsics.clear(); |
Devang Patel | 80ae349 | 2009-08-28 23:24:31 +0000 | [diff] [blame] | 4677 | |
Rafael Espindola | 79753a0 | 2015-12-18 21:18:57 +0000 | [diff] [blame] | 4678 | UpgradeDebugInfo(*TheModule); |
Manman Ren | b5d7ff4 | 2016-05-25 23:14:48 +0000 | [diff] [blame] | 4679 | |
| 4680 | UpgradeModuleFlags(*TheModule); |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 4681 | return Error::success(); |
Chris Lattner | 9eeada9 | 2007-05-18 04:02:46 +0000 | [diff] [blame] | 4682 | } |
| 4683 | |
Rafael Espindola | 2fa1e43 | 2014-12-03 07:18:23 +0000 | [diff] [blame] | 4684 | std::vector<StructType *> BitcodeReader::getIdentifiedStructTypes() const { |
| 4685 | return IdentifiedStructTypes; |
| 4686 | } |
| 4687 | |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 4688 | ModuleSummaryIndexBitcodeReader::ModuleSummaryIndexBitcodeReader( |
Peter Collingbourne | 74d22dd | 2017-05-01 22:04:36 +0000 | [diff] [blame] | 4689 | BitstreamCursor Cursor, StringRef Strtab, ModuleSummaryIndex &TheIndex, |
| 4690 | StringRef ModulePath, unsigned ModuleId) |
| 4691 | : BitcodeReaderBase(std::move(Cursor), Strtab), TheIndex(TheIndex), |
| 4692 | ModulePath(ModulePath), ModuleId(ModuleId) {} |
| 4693 | |
| 4694 | ModulePathStringTableTy::iterator |
| 4695 | ModuleSummaryIndexBitcodeReader::addThisModulePath() { |
| 4696 | return TheIndex.addModulePath(ModulePath, ModuleId); |
| 4697 | } |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 4698 | |
Peter Collingbourne | 9667b91 | 2017-05-04 18:03:25 +0000 | [diff] [blame] | 4699 | std::pair<ValueInfo, GlobalValue::GUID> |
| 4700 | ModuleSummaryIndexBitcodeReader::getValueInfoFromValueId(unsigned ValueId) { |
| 4701 | auto VGI = ValueIdToValueInfoMap[ValueId]; |
| 4702 | assert(VGI.first); |
| 4703 | return VGI; |
Teresa Johnson | 76a1c1d | 2016-03-11 18:52:24 +0000 | [diff] [blame] | 4704 | } |
| 4705 | |
Peter Collingbourne | a0f371a | 2017-04-17 17:51:36 +0000 | [diff] [blame] | 4706 | void ModuleSummaryIndexBitcodeReader::setValueGUID( |
| 4707 | uint64_t ValueID, StringRef ValueName, GlobalValue::LinkageTypes Linkage, |
| 4708 | StringRef SourceFileName) { |
| 4709 | std::string GlobalId = |
| 4710 | GlobalValue::getGlobalIdentifier(ValueName, Linkage, SourceFileName); |
| 4711 | auto ValueGUID = GlobalValue::getGUID(GlobalId); |
| 4712 | auto OriginalNameID = ValueGUID; |
| 4713 | if (GlobalValue::isLocalLinkage(Linkage)) |
| 4714 | OriginalNameID = GlobalValue::getGUID(ValueName); |
| 4715 | if (PrintSummaryGUIDs) |
| 4716 | dbgs() << "GUID " << ValueGUID << "(" << OriginalNameID << ") is " |
| 4717 | << ValueName << "\n"; |
Peter Collingbourne | 9667b91 | 2017-05-04 18:03:25 +0000 | [diff] [blame] | 4718 | ValueIdToValueInfoMap[ValueID] = |
| 4719 | std::make_pair(TheIndex.getOrInsertValueInfo(ValueGUID), OriginalNameID); |
Peter Collingbourne | a0f371a | 2017-04-17 17:51:36 +0000 | [diff] [blame] | 4720 | } |
| 4721 | |
Teresa Johnson | 76a1c1d | 2016-03-11 18:52:24 +0000 | [diff] [blame] | 4722 | // Specialized value symbol table parser used when reading module index |
Teresa Johnson | 28e457b | 2016-04-24 14:57:11 +0000 | [diff] [blame] | 4723 | // blocks where we don't actually create global values. The parsed information |
| 4724 | // is saved in the bitcode reader for use when later parsing summaries. |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 4725 | Error ModuleSummaryIndexBitcodeReader::parseValueSymbolTable( |
Teresa Johnson | 76a1c1d | 2016-03-11 18:52:24 +0000 | [diff] [blame] | 4726 | uint64_t Offset, |
| 4727 | DenseMap<unsigned, GlobalValue::LinkageTypes> &ValueIdToLinkageMap) { |
Peter Collingbourne | a0f371a | 2017-04-17 17:51:36 +0000 | [diff] [blame] | 4728 | // With a strtab the VST is not required to parse the summary. |
| 4729 | if (UseStrtab) |
| 4730 | return Error::success(); |
| 4731 | |
Teresa Johnson | 76a1c1d | 2016-03-11 18:52:24 +0000 | [diff] [blame] | 4732 | assert(Offset > 0 && "Expected non-zero VST offset"); |
| 4733 | uint64_t CurrentBit = jumpToValueSymbolTable(Offset, Stream); |
| 4734 | |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 4735 | if (Stream.EnterSubBlock(bitc::VALUE_SYMTAB_BLOCK_ID)) |
| 4736 | return error("Invalid record"); |
| 4737 | |
| 4738 | SmallVector<uint64_t, 64> Record; |
| 4739 | |
| 4740 | // Read all the records for this value table. |
| 4741 | SmallString<128> ValueName; |
Eugene Zelenko | 1804a77 | 2016-08-25 00:45:04 +0000 | [diff] [blame] | 4742 | |
| 4743 | while (true) { |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 4744 | BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); |
| 4745 | |
| 4746 | switch (Entry.Kind) { |
Teresa Johnson | f72278f | 2015-11-02 18:02:11 +0000 | [diff] [blame] | 4747 | case BitstreamEntry::SubBlock: // Handled for us already. |
| 4748 | case BitstreamEntry::Error: |
| 4749 | return error("Malformed block"); |
| 4750 | case BitstreamEntry::EndBlock: |
Teresa Johnson | 76a1c1d | 2016-03-11 18:52:24 +0000 | [diff] [blame] | 4751 | // Done parsing VST, jump back to wherever we came from. |
| 4752 | Stream.JumpToBit(CurrentBit); |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 4753 | return Error::success(); |
Teresa Johnson | f72278f | 2015-11-02 18:02:11 +0000 | [diff] [blame] | 4754 | case BitstreamEntry::Record: |
| 4755 | // The interesting case. |
| 4756 | break; |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 4757 | } |
| 4758 | |
| 4759 | // Read a record. |
| 4760 | Record.clear(); |
| 4761 | switch (Stream.readRecord(Entry.ID, Record)) { |
Teresa Johnson | f72278f | 2015-11-02 18:02:11 +0000 | [diff] [blame] | 4762 | default: // Default behavior: ignore (e.g. VST_CODE_BBENTRY records). |
| 4763 | break; |
Teresa Johnson | 76a1c1d | 2016-03-11 18:52:24 +0000 | [diff] [blame] | 4764 | case bitc::VST_CODE_ENTRY: { // VST_CODE_ENTRY: [valueid, namechar x N] |
| 4765 | if (convertToString(Record, 1, ValueName)) |
| 4766 | return error("Invalid record"); |
| 4767 | unsigned ValueID = Record[0]; |
Teresa Johnson | 76a1c1d | 2016-03-11 18:52:24 +0000 | [diff] [blame] | 4768 | assert(!SourceFileName.empty()); |
| 4769 | auto VLI = ValueIdToLinkageMap.find(ValueID); |
| 4770 | assert(VLI != ValueIdToLinkageMap.end() && |
| 4771 | "No linkage found for VST entry?"); |
Mehdi Amini | ae64eaf | 2016-04-23 23:38:17 +0000 | [diff] [blame] | 4772 | auto Linkage = VLI->second; |
Peter Collingbourne | a0f371a | 2017-04-17 17:51:36 +0000 | [diff] [blame] | 4773 | setValueGUID(ValueID, ValueName, Linkage, SourceFileName); |
Teresa Johnson | 76a1c1d | 2016-03-11 18:52:24 +0000 | [diff] [blame] | 4774 | ValueName.clear(); |
| 4775 | break; |
| 4776 | } |
Teresa Johnson | f72278f | 2015-11-02 18:02:11 +0000 | [diff] [blame] | 4777 | case bitc::VST_CODE_FNENTRY: { |
Teresa Johnson | 79d4e2f | 2016-02-10 15:02:51 +0000 | [diff] [blame] | 4778 | // VST_CODE_FNENTRY: [valueid, offset, namechar x N] |
Teresa Johnson | f72278f | 2015-11-02 18:02:11 +0000 | [diff] [blame] | 4779 | if (convertToString(Record, 2, ValueName)) |
| 4780 | return error("Invalid record"); |
| 4781 | unsigned ValueID = Record[0]; |
Teresa Johnson | 76a1c1d | 2016-03-11 18:52:24 +0000 | [diff] [blame] | 4782 | assert(!SourceFileName.empty()); |
| 4783 | auto VLI = ValueIdToLinkageMap.find(ValueID); |
| 4784 | assert(VLI != ValueIdToLinkageMap.end() && |
| 4785 | "No linkage found for VST entry?"); |
Mehdi Amini | ae64eaf | 2016-04-23 23:38:17 +0000 | [diff] [blame] | 4786 | auto Linkage = VLI->second; |
Peter Collingbourne | a0f371a | 2017-04-17 17:51:36 +0000 | [diff] [blame] | 4787 | setValueGUID(ValueID, ValueName, Linkage, SourceFileName); |
Teresa Johnson | f72278f | 2015-11-02 18:02:11 +0000 | [diff] [blame] | 4788 | ValueName.clear(); |
| 4789 | break; |
| 4790 | } |
Teresa Johnson | 76a1c1d | 2016-03-11 18:52:24 +0000 | [diff] [blame] | 4791 | case bitc::VST_CODE_COMBINED_ENTRY: { |
| 4792 | // VST_CODE_COMBINED_ENTRY: [valueid, refguid] |
| 4793 | unsigned ValueID = Record[0]; |
Mehdi Amini | ad5741b | 2016-04-02 05:07:53 +0000 | [diff] [blame] | 4794 | GlobalValue::GUID RefGUID = Record[1]; |
Mehdi Amini | ae64eaf | 2016-04-23 23:38:17 +0000 | [diff] [blame] | 4795 | // The "original name", which is the second value of the pair will be |
| 4796 | // overriden later by a FS_COMBINED_ORIGINAL_NAME in the combined index. |
Peter Collingbourne | 9667b91 | 2017-05-04 18:03:25 +0000 | [diff] [blame] | 4797 | ValueIdToValueInfoMap[ValueID] = |
| 4798 | std::make_pair(TheIndex.getOrInsertValueInfo(RefGUID), RefGUID); |
Teresa Johnson | f72278f | 2015-11-02 18:02:11 +0000 | [diff] [blame] | 4799 | break; |
| 4800 | } |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 4801 | } |
| 4802 | } |
| 4803 | } |
| 4804 | |
Teresa Johnson | 76a1c1d | 2016-03-11 18:52:24 +0000 | [diff] [blame] | 4805 | // Parse just the blocks needed for building the index out of the module. |
| 4806 | // At the end of this routine the module Index is populated with a map |
Teresa Johnson | 28e457b | 2016-04-24 14:57:11 +0000 | [diff] [blame] | 4807 | // from global value id to GlobalValueSummary objects. |
Peter Collingbourne | 74d22dd | 2017-05-01 22:04:36 +0000 | [diff] [blame] | 4808 | Error ModuleSummaryIndexBitcodeReader::parseModule() { |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 4809 | if (Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID)) |
| 4810 | return error("Invalid record"); |
| 4811 | |
Teresa Johnson | e1164de | 2016-02-10 21:55:02 +0000 | [diff] [blame] | 4812 | SmallVector<uint64_t, 64> Record; |
Teresa Johnson | 76a1c1d | 2016-03-11 18:52:24 +0000 | [diff] [blame] | 4813 | DenseMap<unsigned, GlobalValue::LinkageTypes> ValueIdToLinkageMap; |
| 4814 | unsigned ValueId = 0; |
Teresa Johnson | e1164de | 2016-02-10 21:55:02 +0000 | [diff] [blame] | 4815 | |
Teresa Johnson | 76a1c1d | 2016-03-11 18:52:24 +0000 | [diff] [blame] | 4816 | // Read the index for this module. |
Eugene Zelenko | 1804a77 | 2016-08-25 00:45:04 +0000 | [diff] [blame] | 4817 | while (true) { |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 4818 | BitstreamEntry Entry = Stream.advance(); |
| 4819 | |
| 4820 | switch (Entry.Kind) { |
Teresa Johnson | f72278f | 2015-11-02 18:02:11 +0000 | [diff] [blame] | 4821 | case BitstreamEntry::Error: |
| 4822 | return error("Malformed block"); |
| 4823 | case BitstreamEntry::EndBlock: |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 4824 | return Error::success(); |
Teresa Johnson | f72278f | 2015-11-02 18:02:11 +0000 | [diff] [blame] | 4825 | |
| 4826 | case BitstreamEntry::SubBlock: |
Teresa Johnson | f72278f | 2015-11-02 18:02:11 +0000 | [diff] [blame] | 4827 | switch (Entry.ID) { |
| 4828 | default: // Skip unknown content. |
| 4829 | if (Stream.SkipBlock()) |
| 4830 | return error("Invalid record"); |
| 4831 | break; |
| 4832 | case bitc::BLOCKINFO_BLOCK_ID: |
| 4833 | // Need to parse these to get abbrev ids (e.g. for VST) |
Peter Collingbourne | 939c7d9 | 2016-11-08 04:16:57 +0000 | [diff] [blame] | 4834 | if (readBlockInfo()) |
Teresa Johnson | f72278f | 2015-11-02 18:02:11 +0000 | [diff] [blame] | 4835 | return error("Malformed block"); |
| 4836 | break; |
| 4837 | case bitc::VALUE_SYMTAB_BLOCK_ID: |
Teresa Johnson | 76a1c1d | 2016-03-11 18:52:24 +0000 | [diff] [blame] | 4838 | // Should have been parsed earlier via VSTOffset, unless there |
| 4839 | // is no summary section. |
| 4840 | assert(((SeenValueSymbolTable && VSTOffset > 0) || |
| 4841 | !SeenGlobalValSummary) && |
| 4842 | "Expected early VST parse via VSTOffset record"); |
| 4843 | if (Stream.SkipBlock()) |
| 4844 | return error("Invalid record"); |
Teresa Johnson | f72278f | 2015-11-02 18:02:11 +0000 | [diff] [blame] | 4845 | break; |
Teresa Johnson | 76a1c1d | 2016-03-11 18:52:24 +0000 | [diff] [blame] | 4846 | case bitc::GLOBALVAL_SUMMARY_BLOCK_ID: |
Teresa Johnson | 76a1c1d | 2016-03-11 18:52:24 +0000 | [diff] [blame] | 4847 | assert(!SeenValueSymbolTable && |
| 4848 | "Already read VST when parsing summary block?"); |
Teresa Johnson | 620c140 | 2016-09-20 23:07:17 +0000 | [diff] [blame] | 4849 | // We might not have a VST if there were no values in the |
| 4850 | // summary. An empty summary block generated when we are |
| 4851 | // performing ThinLTO compiles so we don't later invoke |
| 4852 | // the regular LTO process on them. |
| 4853 | if (VSTOffset > 0) { |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 4854 | if (Error Err = parseValueSymbolTable(VSTOffset, ValueIdToLinkageMap)) |
| 4855 | return Err; |
Teresa Johnson | 620c140 | 2016-09-20 23:07:17 +0000 | [diff] [blame] | 4856 | SeenValueSymbolTable = true; |
| 4857 | } |
Teresa Johnson | 76a1c1d | 2016-03-11 18:52:24 +0000 | [diff] [blame] | 4858 | SeenGlobalValSummary = true; |
Peter Collingbourne | 74d22dd | 2017-05-01 22:04:36 +0000 | [diff] [blame] | 4859 | if (Error Err = parseEntireSummary()) |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 4860 | return Err; |
Teresa Johnson | f72278f | 2015-11-02 18:02:11 +0000 | [diff] [blame] | 4861 | break; |
| 4862 | case bitc::MODULE_STRTAB_BLOCK_ID: |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 4863 | if (Error Err = parseModuleStringTable()) |
| 4864 | return Err; |
Teresa Johnson | f72278f | 2015-11-02 18:02:11 +0000 | [diff] [blame] | 4865 | break; |
| 4866 | } |
| 4867 | continue; |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 4868 | |
Mehdi Amini | d7ad221 | 2016-04-01 05:33:11 +0000 | [diff] [blame] | 4869 | case BitstreamEntry::Record: { |
Teresa Johnson | e1164de | 2016-02-10 21:55:02 +0000 | [diff] [blame] | 4870 | Record.clear(); |
| 4871 | auto BitCode = Stream.readRecord(Entry.ID, Record); |
| 4872 | switch (BitCode) { |
| 4873 | default: |
| 4874 | break; // Default behavior, ignore unknown content. |
Peter Collingbourne | a0f371a | 2017-04-17 17:51:36 +0000 | [diff] [blame] | 4875 | case bitc::MODULE_CODE_VERSION: { |
| 4876 | if (Error Err = parseVersionRecord(Record).takeError()) |
| 4877 | return Err; |
| 4878 | break; |
| 4879 | } |
Teresa Johnson | e1164de | 2016-02-10 21:55:02 +0000 | [diff] [blame] | 4880 | /// MODULE_CODE_SOURCE_FILENAME: [namechar x N] |
Teresa Johnson | 76a1c1d | 2016-03-11 18:52:24 +0000 | [diff] [blame] | 4881 | case bitc::MODULE_CODE_SOURCE_FILENAME: { |
Teresa Johnson | e1164de | 2016-02-10 21:55:02 +0000 | [diff] [blame] | 4882 | SmallString<128> ValueName; |
| 4883 | if (convertToString(Record, 0, ValueName)) |
| 4884 | return error("Invalid record"); |
| 4885 | SourceFileName = ValueName.c_str(); |
| 4886 | break; |
| 4887 | } |
Mehdi Amini | d7ad221 | 2016-04-01 05:33:11 +0000 | [diff] [blame] | 4888 | /// MODULE_CODE_HASH: [5*i32] |
| 4889 | case bitc::MODULE_CODE_HASH: { |
| 4890 | if (Record.size() != 5) |
| 4891 | return error("Invalid hash length " + Twine(Record.size()).str()); |
Peter Collingbourne | 74d22dd | 2017-05-01 22:04:36 +0000 | [diff] [blame] | 4892 | auto &Hash = addThisModulePath()->second.second; |
Mehdi Amini | d7ad221 | 2016-04-01 05:33:11 +0000 | [diff] [blame] | 4893 | int Pos = 0; |
| 4894 | for (auto &Val : Record) { |
| 4895 | assert(!(Val >> 32) && "Unexpected high bits set"); |
| 4896 | Hash[Pos++] = Val; |
| 4897 | } |
| 4898 | break; |
| 4899 | } |
Teresa Johnson | 76a1c1d | 2016-03-11 18:52:24 +0000 | [diff] [blame] | 4900 | /// MODULE_CODE_VSTOFFSET: [offset] |
| 4901 | case bitc::MODULE_CODE_VSTOFFSET: |
| 4902 | if (Record.size() < 1) |
| 4903 | return error("Invalid record"); |
Peter Collingbourne | a46ec9f | 2016-12-01 06:00:53 +0000 | [diff] [blame] | 4904 | // Note that we subtract 1 here because the offset is relative to one |
| 4905 | // word before the start of the identification or module block, which |
| 4906 | // was historically always the start of the regular bitcode header. |
| 4907 | VSTOffset = Record[0] - 1; |
Teresa Johnson | 76a1c1d | 2016-03-11 18:52:24 +0000 | [diff] [blame] | 4908 | break; |
Peter Collingbourne | a0f371a | 2017-04-17 17:51:36 +0000 | [diff] [blame] | 4909 | // v1 GLOBALVAR: [pointer type, isconst, initid, linkage, ...] |
| 4910 | // v1 FUNCTION: [type, callingconv, isproto, linkage, ...] |
| 4911 | // v1 ALIAS: [alias type, addrspace, aliasee val#, linkage, ...] |
| 4912 | // v2: [strtab offset, strtab size, v1] |
Peter Collingbourne | f43e0ae | 2017-04-10 21:17:54 +0000 | [diff] [blame] | 4913 | case bitc::MODULE_CODE_GLOBALVAR: |
| 4914 | case bitc::MODULE_CODE_FUNCTION: |
Teresa Johnson | 76a1c1d | 2016-03-11 18:52:24 +0000 | [diff] [blame] | 4915 | case bitc::MODULE_CODE_ALIAS: { |
Peter Collingbourne | a0f371a | 2017-04-17 17:51:36 +0000 | [diff] [blame] | 4916 | StringRef Name; |
| 4917 | ArrayRef<uint64_t> GVRecord; |
| 4918 | std::tie(Name, GVRecord) = readNameFromStrtab(Record); |
| 4919 | if (GVRecord.size() <= 3) |
Teresa Johnson | 76a1c1d | 2016-03-11 18:52:24 +0000 | [diff] [blame] | 4920 | return error("Invalid record"); |
Peter Collingbourne | a0f371a | 2017-04-17 17:51:36 +0000 | [diff] [blame] | 4921 | uint64_t RawLinkage = GVRecord[3]; |
Teresa Johnson | 76a1c1d | 2016-03-11 18:52:24 +0000 | [diff] [blame] | 4922 | GlobalValue::LinkageTypes Linkage = getDecodedLinkage(RawLinkage); |
Peter Collingbourne | a0f371a | 2017-04-17 17:51:36 +0000 | [diff] [blame] | 4923 | if (!UseStrtab) { |
| 4924 | ValueIdToLinkageMap[ValueId++] = Linkage; |
| 4925 | break; |
| 4926 | } |
| 4927 | |
| 4928 | setValueGUID(ValueId++, Name, Linkage, SourceFileName); |
Teresa Johnson | 76a1c1d | 2016-03-11 18:52:24 +0000 | [diff] [blame] | 4929 | break; |
| 4930 | } |
| 4931 | } |
Teresa Johnson | e1164de | 2016-02-10 21:55:02 +0000 | [diff] [blame] | 4932 | } |
Teresa Johnson | f72278f | 2015-11-02 18:02:11 +0000 | [diff] [blame] | 4933 | continue; |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 4934 | } |
| 4935 | } |
| 4936 | } |
| 4937 | |
Peter Collingbourne | 0c30f08 | 2016-12-20 21:12:28 +0000 | [diff] [blame] | 4938 | std::vector<ValueInfo> |
| 4939 | ModuleSummaryIndexBitcodeReader::makeRefList(ArrayRef<uint64_t> Record) { |
| 4940 | std::vector<ValueInfo> Ret; |
| 4941 | Ret.reserve(Record.size()); |
| 4942 | for (uint64_t RefValueId : Record) |
Peter Collingbourne | 9667b91 | 2017-05-04 18:03:25 +0000 | [diff] [blame] | 4943 | Ret.push_back(getValueInfoFromValueId(RefValueId).first); |
Peter Collingbourne | 0c30f08 | 2016-12-20 21:12:28 +0000 | [diff] [blame] | 4944 | return Ret; |
| 4945 | } |
| 4946 | |
| 4947 | std::vector<FunctionSummary::EdgeTy> ModuleSummaryIndexBitcodeReader::makeCallList( |
| 4948 | ArrayRef<uint64_t> Record, bool IsOldProfileFormat, bool HasProfile) { |
| 4949 | std::vector<FunctionSummary::EdgeTy> Ret; |
| 4950 | Ret.reserve(Record.size()); |
| 4951 | for (unsigned I = 0, E = Record.size(); I != E; ++I) { |
| 4952 | CalleeInfo::HotnessType Hotness = CalleeInfo::HotnessType::Unknown; |
Peter Collingbourne | 9667b91 | 2017-05-04 18:03:25 +0000 | [diff] [blame] | 4953 | ValueInfo Callee = getValueInfoFromValueId(Record[I]).first; |
Peter Collingbourne | 0c30f08 | 2016-12-20 21:12:28 +0000 | [diff] [blame] | 4954 | if (IsOldProfileFormat) { |
| 4955 | I += 1; // Skip old callsitecount field |
| 4956 | if (HasProfile) |
| 4957 | I += 1; // Skip old profilecount field |
| 4958 | } else if (HasProfile) |
| 4959 | Hotness = static_cast<CalleeInfo::HotnessType>(Record[++I]); |
Peter Collingbourne | 9667b91 | 2017-05-04 18:03:25 +0000 | [diff] [blame] | 4960 | Ret.push_back(FunctionSummary::EdgeTy{Callee, CalleeInfo{Hotness}}); |
Peter Collingbourne | 0c30f08 | 2016-12-20 21:12:28 +0000 | [diff] [blame] | 4961 | } |
| 4962 | return Ret; |
| 4963 | } |
| 4964 | |
Teresa Johnson | 76a1c1d | 2016-03-11 18:52:24 +0000 | [diff] [blame] | 4965 | // Eagerly parse the entire summary block. This populates the GlobalValueSummary |
| 4966 | // objects in the index. |
Peter Collingbourne | 74d22dd | 2017-05-01 22:04:36 +0000 | [diff] [blame] | 4967 | Error ModuleSummaryIndexBitcodeReader::parseEntireSummary() { |
Teresa Johnson | 76a1c1d | 2016-03-11 18:52:24 +0000 | [diff] [blame] | 4968 | if (Stream.EnterSubBlock(bitc::GLOBALVAL_SUMMARY_BLOCK_ID)) |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 4969 | return error("Invalid record"); |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 4970 | SmallVector<uint64_t, 64> Record; |
Mehdi Amini | 8fe6936 | 2016-04-24 03:18:11 +0000 | [diff] [blame] | 4971 | |
| 4972 | // Parse version |
| 4973 | { |
| 4974 | BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); |
| 4975 | if (Entry.Kind != BitstreamEntry::Record) |
| 4976 | return error("Invalid Summary Block: record for version expected"); |
| 4977 | if (Stream.readRecord(Entry.ID, Record) != bitc::FS_VERSION) |
| 4978 | return error("Invalid Summary Block: version expected"); |
| 4979 | } |
| 4980 | const uint64_t Version = Record[0]; |
Piotr Padlewski | d9830eb | 2016-09-26 20:37:32 +0000 | [diff] [blame] | 4981 | const bool IsOldProfileFormat = Version == 1; |
Teresa Johnson | 519465b | 2017-01-05 14:32:16 +0000 | [diff] [blame] | 4982 | if (Version < 1 || Version > 3) |
Piotr Padlewski | d9830eb | 2016-09-26 20:37:32 +0000 | [diff] [blame] | 4983 | return error("Invalid summary version " + Twine(Version) + |
Teresa Johnson | 519465b | 2017-01-05 14:32:16 +0000 | [diff] [blame] | 4984 | ", 1, 2 or 3 expected"); |
Mehdi Amini | 8fe6936 | 2016-04-24 03:18:11 +0000 | [diff] [blame] | 4985 | Record.clear(); |
| 4986 | |
Mehdi Amini | ae64eaf | 2016-04-23 23:38:17 +0000 | [diff] [blame] | 4987 | // Keep around the last seen summary to be used when we see an optional |
| 4988 | // "OriginalName" attachement. |
| 4989 | GlobalValueSummary *LastSeenSummary = nullptr; |
Dehao Chen | 4a435e0 | 2017-03-14 17:33:01 +0000 | [diff] [blame] | 4990 | GlobalValue::GUID LastSeenGUID = 0; |
Peter Collingbourne | fa3175f | 2017-02-11 03:19:22 +0000 | [diff] [blame] | 4991 | |
| 4992 | // We can expect to see any number of type ID information records before |
| 4993 | // each function summary records; these variables store the information |
| 4994 | // collected so far so that it can be used to create the summary object. |
Peter Collingbourne | 1b4137a7 | 2016-12-21 23:03:45 +0000 | [diff] [blame] | 4995 | std::vector<GlobalValue::GUID> PendingTypeTests; |
Peter Collingbourne | be9ffaa | 2017-02-10 22:29:38 +0000 | [diff] [blame] | 4996 | std::vector<FunctionSummary::VFuncId> PendingTypeTestAssumeVCalls, |
| 4997 | PendingTypeCheckedLoadVCalls; |
| 4998 | std::vector<FunctionSummary::ConstVCall> PendingTypeTestAssumeConstVCalls, |
| 4999 | PendingTypeCheckedLoadConstVCalls; |
Eugene Zelenko | 1804a77 | 2016-08-25 00:45:04 +0000 | [diff] [blame] | 5000 | |
| 5001 | while (true) { |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 5002 | BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); |
| 5003 | |
| 5004 | switch (Entry.Kind) { |
Teresa Johnson | f72278f | 2015-11-02 18:02:11 +0000 | [diff] [blame] | 5005 | case BitstreamEntry::SubBlock: // Handled for us already. |
| 5006 | case BitstreamEntry::Error: |
| 5007 | return error("Malformed block"); |
| 5008 | case BitstreamEntry::EndBlock: |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 5009 | return Error::success(); |
Teresa Johnson | f72278f | 2015-11-02 18:02:11 +0000 | [diff] [blame] | 5010 | case BitstreamEntry::Record: |
| 5011 | // The interesting case. |
| 5012 | break; |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 5013 | } |
| 5014 | |
| 5015 | // Read a record. The record format depends on whether this |
| 5016 | // is a per-module index or a combined index file. In the per-module |
| 5017 | // case the records contain the associated value's ID for correlation |
| 5018 | // with VST entries. In the combined index the correlation is done |
| 5019 | // via the bitcode offset of the summary records (which were saved |
| 5020 | // in the combined index VST entries). The records also contain |
| 5021 | // information used for ThinLTO renaming and importing. |
| 5022 | Record.clear(); |
Teresa Johnson | 76a1c1d | 2016-03-11 18:52:24 +0000 | [diff] [blame] | 5023 | auto BitCode = Stream.readRecord(Entry.ID, Record); |
| 5024 | switch (BitCode) { |
Teresa Johnson | f72278f | 2015-11-02 18:02:11 +0000 | [diff] [blame] | 5025 | default: // Default behavior: ignore. |
| 5026 | break; |
Peter Collingbourne | a0f371a | 2017-04-17 17:51:36 +0000 | [diff] [blame] | 5027 | case bitc::FS_VALUE_GUID: { // [valueid, refguid] |
| 5028 | uint64_t ValueID = Record[0]; |
| 5029 | GlobalValue::GUID RefGUID = Record[1]; |
Peter Collingbourne | 9667b91 | 2017-05-04 18:03:25 +0000 | [diff] [blame] | 5030 | ValueIdToValueInfoMap[ValueID] = |
| 5031 | std::make_pair(TheIndex.getOrInsertValueInfo(RefGUID), RefGUID); |
Peter Collingbourne | a0f371a | 2017-04-17 17:51:36 +0000 | [diff] [blame] | 5032 | break; |
| 5033 | } |
Mehdi Amini | c3ed48c | 2016-04-24 03:18:18 +0000 | [diff] [blame] | 5034 | // FS_PERMODULE: [valueid, flags, instcount, numrefs, numrefs x valueid, |
Piotr Padlewski | d9830eb | 2016-09-26 20:37:32 +0000 | [diff] [blame] | 5035 | // n x (valueid)] |
Mehdi Amini | c3ed48c | 2016-04-24 03:18:18 +0000 | [diff] [blame] | 5036 | // FS_PERMODULE_PROFILE: [valueid, flags, instcount, numrefs, |
Teresa Johnson | 76a1c1d | 2016-03-11 18:52:24 +0000 | [diff] [blame] | 5037 | // numrefs x valueid, |
Piotr Padlewski | d9830eb | 2016-09-26 20:37:32 +0000 | [diff] [blame] | 5038 | // n x (valueid, hotness)] |
Teresa Johnson | 76a1c1d | 2016-03-11 18:52:24 +0000 | [diff] [blame] | 5039 | case bitc::FS_PERMODULE: |
| 5040 | case bitc::FS_PERMODULE_PROFILE: { |
Teresa Johnson | f72278f | 2015-11-02 18:02:11 +0000 | [diff] [blame] | 5041 | unsigned ValueID = Record[0]; |
Mehdi Amini | c3ed48c | 2016-04-24 03:18:18 +0000 | [diff] [blame] | 5042 | uint64_t RawFlags = Record[1]; |
Teresa Johnson | f72278f | 2015-11-02 18:02:11 +0000 | [diff] [blame] | 5043 | unsigned InstCount = Record[2]; |
Teresa Johnson | 76a1c1d | 2016-03-11 18:52:24 +0000 | [diff] [blame] | 5044 | unsigned NumRefs = Record[3]; |
Mehdi Amini | c3ed48c | 2016-04-24 03:18:18 +0000 | [diff] [blame] | 5045 | auto Flags = getDecodedGVSummaryFlags(RawFlags, Version); |
Teresa Johnson | f72278f | 2015-11-02 18:02:11 +0000 | [diff] [blame] | 5046 | // The module path string ref set in the summary must be owned by the |
| 5047 | // index's module string table. Since we don't have a module path |
| 5048 | // string table section in the per-module index, we create a single |
| 5049 | // module path string table entry with an empty (0) ID to take |
| 5050 | // ownership. |
Teresa Johnson | 76a1c1d | 2016-03-11 18:52:24 +0000 | [diff] [blame] | 5051 | static int RefListStartIndex = 4; |
| 5052 | int CallGraphEdgeStartIndex = RefListStartIndex + NumRefs; |
| 5053 | assert(Record.size() >= RefListStartIndex + NumRefs && |
| 5054 | "Record size inconsistent with number of references"); |
Peter Collingbourne | 0c30f08 | 2016-12-20 21:12:28 +0000 | [diff] [blame] | 5055 | std::vector<ValueInfo> Refs = makeRefList( |
| 5056 | ArrayRef<uint64_t>(Record).slice(RefListStartIndex, NumRefs)); |
Teresa Johnson | 76a1c1d | 2016-03-11 18:52:24 +0000 | [diff] [blame] | 5057 | bool HasProfile = (BitCode == bitc::FS_PERMODULE_PROFILE); |
Peter Collingbourne | 0c30f08 | 2016-12-20 21:12:28 +0000 | [diff] [blame] | 5058 | std::vector<FunctionSummary::EdgeTy> Calls = makeCallList( |
| 5059 | ArrayRef<uint64_t>(Record).slice(CallGraphEdgeStartIndex), |
| 5060 | IsOldProfileFormat, HasProfile); |
| 5061 | auto FS = llvm::make_unique<FunctionSummary>( |
Peter Collingbourne | 1b4137a7 | 2016-12-21 23:03:45 +0000 | [diff] [blame] | 5062 | Flags, InstCount, std::move(Refs), std::move(Calls), |
Peter Collingbourne | be9ffaa | 2017-02-10 22:29:38 +0000 | [diff] [blame] | 5063 | std::move(PendingTypeTests), std::move(PendingTypeTestAssumeVCalls), |
| 5064 | std::move(PendingTypeCheckedLoadVCalls), |
| 5065 | std::move(PendingTypeTestAssumeConstVCalls), |
| 5066 | std::move(PendingTypeCheckedLoadConstVCalls)); |
Peter Collingbourne | 704f814 | 2016-12-22 02:52:23 +0000 | [diff] [blame] | 5067 | PendingTypeTests.clear(); |
Peter Collingbourne | be9ffaa | 2017-02-10 22:29:38 +0000 | [diff] [blame] | 5068 | PendingTypeTestAssumeVCalls.clear(); |
| 5069 | PendingTypeCheckedLoadVCalls.clear(); |
| 5070 | PendingTypeTestAssumeConstVCalls.clear(); |
| 5071 | PendingTypeCheckedLoadConstVCalls.clear(); |
Peter Collingbourne | 9667b91 | 2017-05-04 18:03:25 +0000 | [diff] [blame] | 5072 | auto VIAndOriginalGUID = getValueInfoFromValueId(ValueID); |
Peter Collingbourne | 74d22dd | 2017-05-01 22:04:36 +0000 | [diff] [blame] | 5073 | FS->setModulePath(addThisModulePath()->first()); |
Peter Collingbourne | 9667b91 | 2017-05-04 18:03:25 +0000 | [diff] [blame] | 5074 | FS->setOriginalName(VIAndOriginalGUID.second); |
| 5075 | TheIndex.addGlobalValueSummary(VIAndOriginalGUID.first, std::move(FS)); |
Teresa Johnson | bbe0545 | 2016-02-24 17:57:28 +0000 | [diff] [blame] | 5076 | break; |
Teresa Johnson | f72278f | 2015-11-02 18:02:11 +0000 | [diff] [blame] | 5077 | } |
Mehdi Amini | c3ed48c | 2016-04-24 03:18:18 +0000 | [diff] [blame] | 5078 | // FS_ALIAS: [valueid, flags, valueid] |
Mehdi Amini | 2d28f7a | 2016-04-16 06:56:44 +0000 | [diff] [blame] | 5079 | // Aliases must be emitted (and parsed) after all FS_PERMODULE entries, as |
| 5080 | // they expect all aliasee summaries to be available. |
| 5081 | case bitc::FS_ALIAS: { |
| 5082 | unsigned ValueID = Record[0]; |
Mehdi Amini | c3ed48c | 2016-04-24 03:18:18 +0000 | [diff] [blame] | 5083 | uint64_t RawFlags = Record[1]; |
Mehdi Amini | 2d28f7a | 2016-04-16 06:56:44 +0000 | [diff] [blame] | 5084 | unsigned AliaseeID = Record[2]; |
Mehdi Amini | c3ed48c | 2016-04-24 03:18:18 +0000 | [diff] [blame] | 5085 | auto Flags = getDecodedGVSummaryFlags(RawFlags, Version); |
Peter Collingbourne | 0c30f08 | 2016-12-20 21:12:28 +0000 | [diff] [blame] | 5086 | auto AS = |
| 5087 | llvm::make_unique<AliasSummary>(Flags, std::vector<ValueInfo>{}); |
Mehdi Amini | 2d28f7a | 2016-04-16 06:56:44 +0000 | [diff] [blame] | 5088 | // The module path string ref set in the summary must be owned by the |
| 5089 | // index's module string table. Since we don't have a module path |
| 5090 | // string table section in the per-module index, we create a single |
| 5091 | // module path string table entry with an empty (0) ID to take |
| 5092 | // ownership. |
Peter Collingbourne | 74d22dd | 2017-05-01 22:04:36 +0000 | [diff] [blame] | 5093 | AS->setModulePath(addThisModulePath()->first()); |
Mehdi Amini | 2d28f7a | 2016-04-16 06:56:44 +0000 | [diff] [blame] | 5094 | |
Peter Collingbourne | 9667b91 | 2017-05-04 18:03:25 +0000 | [diff] [blame] | 5095 | GlobalValue::GUID AliaseeGUID = |
| 5096 | getValueInfoFromValueId(AliaseeID).first.getGUID(); |
Peter Collingbourne | 74d22dd | 2017-05-01 22:04:36 +0000 | [diff] [blame] | 5097 | auto AliaseeInModule = |
| 5098 | TheIndex.findSummaryInModule(AliaseeGUID, ModulePath); |
| 5099 | if (!AliaseeInModule) |
Mehdi Amini | 2d28f7a | 2016-04-16 06:56:44 +0000 | [diff] [blame] | 5100 | return error("Alias expects aliasee summary to be parsed"); |
Peter Collingbourne | 74d22dd | 2017-05-01 22:04:36 +0000 | [diff] [blame] | 5101 | AS->setAliasee(AliaseeInModule); |
Mehdi Amini | 2d28f7a | 2016-04-16 06:56:44 +0000 | [diff] [blame] | 5102 | |
Peter Collingbourne | 9667b91 | 2017-05-04 18:03:25 +0000 | [diff] [blame] | 5103 | auto GUID = getValueInfoFromValueId(ValueID); |
Mehdi Amini | ae64eaf | 2016-04-23 23:38:17 +0000 | [diff] [blame] | 5104 | AS->setOriginalName(GUID.second); |
Peter Collingbourne | 57f9b8c | 2016-12-01 06:21:08 +0000 | [diff] [blame] | 5105 | TheIndex.addGlobalValueSummary(GUID.first, std::move(AS)); |
Mehdi Amini | 2d28f7a | 2016-04-16 06:56:44 +0000 | [diff] [blame] | 5106 | break; |
| 5107 | } |
Mehdi Amini | c3ed48c | 2016-04-24 03:18:18 +0000 | [diff] [blame] | 5108 | // FS_PERMODULE_GLOBALVAR_INIT_REFS: [valueid, flags, n x valueid] |
Teresa Johnson | 76a1c1d | 2016-03-11 18:52:24 +0000 | [diff] [blame] | 5109 | case bitc::FS_PERMODULE_GLOBALVAR_INIT_REFS: { |
| 5110 | unsigned ValueID = Record[0]; |
Mehdi Amini | c3ed48c | 2016-04-24 03:18:18 +0000 | [diff] [blame] | 5111 | uint64_t RawFlags = Record[1]; |
| 5112 | auto Flags = getDecodedGVSummaryFlags(RawFlags, Version); |
Peter Collingbourne | 0c30f08 | 2016-12-20 21:12:28 +0000 | [diff] [blame] | 5113 | std::vector<ValueInfo> Refs = |
| 5114 | makeRefList(ArrayRef<uint64_t>(Record).slice(2)); |
| 5115 | auto FS = llvm::make_unique<GlobalVarSummary>(Flags, std::move(Refs)); |
Peter Collingbourne | 74d22dd | 2017-05-01 22:04:36 +0000 | [diff] [blame] | 5116 | FS->setModulePath(addThisModulePath()->first()); |
Peter Collingbourne | 9667b91 | 2017-05-04 18:03:25 +0000 | [diff] [blame] | 5117 | auto GUID = getValueInfoFromValueId(ValueID); |
Mehdi Amini | ae64eaf | 2016-04-23 23:38:17 +0000 | [diff] [blame] | 5118 | FS->setOriginalName(GUID.second); |
Peter Collingbourne | 57f9b8c | 2016-12-01 06:21:08 +0000 | [diff] [blame] | 5119 | TheIndex.addGlobalValueSummary(GUID.first, std::move(FS)); |
Teresa Johnson | 76a1c1d | 2016-03-11 18:52:24 +0000 | [diff] [blame] | 5120 | break; |
| 5121 | } |
Teresa Johnson | 02e9833 | 2016-04-27 13:28:35 +0000 | [diff] [blame] | 5122 | // FS_COMBINED: [valueid, modid, flags, instcount, numrefs, |
Piotr Padlewski | d9830eb | 2016-09-26 20:37:32 +0000 | [diff] [blame] | 5123 | // numrefs x valueid, n x (valueid)] |
Teresa Johnson | 02e9833 | 2016-04-27 13:28:35 +0000 | [diff] [blame] | 5124 | // FS_COMBINED_PROFILE: [valueid, modid, flags, instcount, numrefs, |
Piotr Padlewski | d9830eb | 2016-09-26 20:37:32 +0000 | [diff] [blame] | 5125 | // numrefs x valueid, n x (valueid, hotness)] |
Teresa Johnson | 76a1c1d | 2016-03-11 18:52:24 +0000 | [diff] [blame] | 5126 | case bitc::FS_COMBINED: |
| 5127 | case bitc::FS_COMBINED_PROFILE: { |
Teresa Johnson | 02e9833 | 2016-04-27 13:28:35 +0000 | [diff] [blame] | 5128 | unsigned ValueID = Record[0]; |
| 5129 | uint64_t ModuleId = Record[1]; |
| 5130 | uint64_t RawFlags = Record[2]; |
| 5131 | unsigned InstCount = Record[3]; |
| 5132 | unsigned NumRefs = Record[4]; |
Mehdi Amini | c3ed48c | 2016-04-24 03:18:18 +0000 | [diff] [blame] | 5133 | auto Flags = getDecodedGVSummaryFlags(RawFlags, Version); |
Teresa Johnson | 02e9833 | 2016-04-27 13:28:35 +0000 | [diff] [blame] | 5134 | static int RefListStartIndex = 5; |
Teresa Johnson | 76a1c1d | 2016-03-11 18:52:24 +0000 | [diff] [blame] | 5135 | int CallGraphEdgeStartIndex = RefListStartIndex + NumRefs; |
| 5136 | assert(Record.size() >= RefListStartIndex + NumRefs && |
| 5137 | "Record size inconsistent with number of references"); |
Peter Collingbourne | 0c30f08 | 2016-12-20 21:12:28 +0000 | [diff] [blame] | 5138 | std::vector<ValueInfo> Refs = makeRefList( |
| 5139 | ArrayRef<uint64_t>(Record).slice(RefListStartIndex, NumRefs)); |
Teresa Johnson | 76a1c1d | 2016-03-11 18:52:24 +0000 | [diff] [blame] | 5140 | bool HasProfile = (BitCode == bitc::FS_COMBINED_PROFILE); |
Peter Collingbourne | 0c30f08 | 2016-12-20 21:12:28 +0000 | [diff] [blame] | 5141 | std::vector<FunctionSummary::EdgeTy> Edges = makeCallList( |
| 5142 | ArrayRef<uint64_t>(Record).slice(CallGraphEdgeStartIndex), |
| 5143 | IsOldProfileFormat, HasProfile); |
Peter Collingbourne | 9667b91 | 2017-05-04 18:03:25 +0000 | [diff] [blame] | 5144 | ValueInfo VI = getValueInfoFromValueId(ValueID).first; |
Peter Collingbourne | 0c30f08 | 2016-12-20 21:12:28 +0000 | [diff] [blame] | 5145 | auto FS = llvm::make_unique<FunctionSummary>( |
Peter Collingbourne | 1b4137a7 | 2016-12-21 23:03:45 +0000 | [diff] [blame] | 5146 | Flags, InstCount, std::move(Refs), std::move(Edges), |
Peter Collingbourne | be9ffaa | 2017-02-10 22:29:38 +0000 | [diff] [blame] | 5147 | std::move(PendingTypeTests), std::move(PendingTypeTestAssumeVCalls), |
| 5148 | std::move(PendingTypeCheckedLoadVCalls), |
| 5149 | std::move(PendingTypeTestAssumeConstVCalls), |
| 5150 | std::move(PendingTypeCheckedLoadConstVCalls)); |
Peter Collingbourne | 704f814 | 2016-12-22 02:52:23 +0000 | [diff] [blame] | 5151 | PendingTypeTests.clear(); |
Peter Collingbourne | be9ffaa | 2017-02-10 22:29:38 +0000 | [diff] [blame] | 5152 | PendingTypeTestAssumeVCalls.clear(); |
| 5153 | PendingTypeCheckedLoadVCalls.clear(); |
| 5154 | PendingTypeTestAssumeConstVCalls.clear(); |
| 5155 | PendingTypeCheckedLoadConstVCalls.clear(); |
Peter Collingbourne | 0c30f08 | 2016-12-20 21:12:28 +0000 | [diff] [blame] | 5156 | LastSeenSummary = FS.get(); |
Peter Collingbourne | 9667b91 | 2017-05-04 18:03:25 +0000 | [diff] [blame] | 5157 | LastSeenGUID = VI.getGUID(); |
Peter Collingbourne | 0c30f08 | 2016-12-20 21:12:28 +0000 | [diff] [blame] | 5158 | FS->setModulePath(ModuleIdMap[ModuleId]); |
Peter Collingbourne | 9667b91 | 2017-05-04 18:03:25 +0000 | [diff] [blame] | 5159 | TheIndex.addGlobalValueSummary(VI, std::move(FS)); |
Teresa Johnson | 76a1c1d | 2016-03-11 18:52:24 +0000 | [diff] [blame] | 5160 | break; |
| 5161 | } |
Teresa Johnson | 02e9833 | 2016-04-27 13:28:35 +0000 | [diff] [blame] | 5162 | // FS_COMBINED_ALIAS: [valueid, modid, flags, valueid] |
Mehdi Amini | 2d28f7a | 2016-04-16 06:56:44 +0000 | [diff] [blame] | 5163 | // Aliases must be emitted (and parsed) after all FS_COMBINED entries, as |
| 5164 | // they expect all aliasee summaries to be available. |
| 5165 | case bitc::FS_COMBINED_ALIAS: { |
Teresa Johnson | 02e9833 | 2016-04-27 13:28:35 +0000 | [diff] [blame] | 5166 | unsigned ValueID = Record[0]; |
| 5167 | uint64_t ModuleId = Record[1]; |
| 5168 | uint64_t RawFlags = Record[2]; |
| 5169 | unsigned AliaseeValueId = Record[3]; |
Mehdi Amini | c3ed48c | 2016-04-24 03:18:18 +0000 | [diff] [blame] | 5170 | auto Flags = getDecodedGVSummaryFlags(RawFlags, Version); |
Peter Collingbourne | 0c30f08 | 2016-12-20 21:12:28 +0000 | [diff] [blame] | 5171 | auto AS = llvm::make_unique<AliasSummary>(Flags, std::vector<ValueInfo>{}); |
Mehdi Amini | ae64eaf | 2016-04-23 23:38:17 +0000 | [diff] [blame] | 5172 | LastSeenSummary = AS.get(); |
Mehdi Amini | 2d28f7a | 2016-04-16 06:56:44 +0000 | [diff] [blame] | 5173 | AS->setModulePath(ModuleIdMap[ModuleId]); |
| 5174 | |
Peter Collingbourne | 9667b91 | 2017-05-04 18:03:25 +0000 | [diff] [blame] | 5175 | auto AliaseeGUID = |
| 5176 | getValueInfoFromValueId(AliaseeValueId).first.getGUID(); |
Teresa Johnson | 02e9833 | 2016-04-27 13:28:35 +0000 | [diff] [blame] | 5177 | auto AliaseeInModule = |
Peter Collingbourne | 57f9b8c | 2016-12-01 06:21:08 +0000 | [diff] [blame] | 5178 | TheIndex.findSummaryInModule(AliaseeGUID, AS->modulePath()); |
Teresa Johnson | 02e9833 | 2016-04-27 13:28:35 +0000 | [diff] [blame] | 5179 | if (!AliaseeInModule) |
Mehdi Amini | 2d28f7a | 2016-04-16 06:56:44 +0000 | [diff] [blame] | 5180 | return error("Alias expects aliasee summary to be parsed"); |
Teresa Johnson | 02e9833 | 2016-04-27 13:28:35 +0000 | [diff] [blame] | 5181 | AS->setAliasee(AliaseeInModule); |
Mehdi Amini | 2d28f7a | 2016-04-16 06:56:44 +0000 | [diff] [blame] | 5182 | |
Peter Collingbourne | 9667b91 | 2017-05-04 18:03:25 +0000 | [diff] [blame] | 5183 | ValueInfo VI = getValueInfoFromValueId(ValueID).first; |
| 5184 | LastSeenGUID = VI.getGUID(); |
| 5185 | TheIndex.addGlobalValueSummary(VI, std::move(AS)); |
Mehdi Amini | 2d28f7a | 2016-04-16 06:56:44 +0000 | [diff] [blame] | 5186 | break; |
| 5187 | } |
Teresa Johnson | 02e9833 | 2016-04-27 13:28:35 +0000 | [diff] [blame] | 5188 | // FS_COMBINED_GLOBALVAR_INIT_REFS: [valueid, modid, flags, n x valueid] |
Teresa Johnson | 76a1c1d | 2016-03-11 18:52:24 +0000 | [diff] [blame] | 5189 | case bitc::FS_COMBINED_GLOBALVAR_INIT_REFS: { |
Teresa Johnson | 02e9833 | 2016-04-27 13:28:35 +0000 | [diff] [blame] | 5190 | unsigned ValueID = Record[0]; |
| 5191 | uint64_t ModuleId = Record[1]; |
| 5192 | uint64_t RawFlags = Record[2]; |
Mehdi Amini | c3ed48c | 2016-04-24 03:18:18 +0000 | [diff] [blame] | 5193 | auto Flags = getDecodedGVSummaryFlags(RawFlags, Version); |
Peter Collingbourne | 0c30f08 | 2016-12-20 21:12:28 +0000 | [diff] [blame] | 5194 | std::vector<ValueInfo> Refs = |
| 5195 | makeRefList(ArrayRef<uint64_t>(Record).slice(3)); |
| 5196 | auto FS = llvm::make_unique<GlobalVarSummary>(Flags, std::move(Refs)); |
Mehdi Amini | ae64eaf | 2016-04-23 23:38:17 +0000 | [diff] [blame] | 5197 | LastSeenSummary = FS.get(); |
Teresa Johnson | 76a1c1d | 2016-03-11 18:52:24 +0000 | [diff] [blame] | 5198 | FS->setModulePath(ModuleIdMap[ModuleId]); |
Peter Collingbourne | 9667b91 | 2017-05-04 18:03:25 +0000 | [diff] [blame] | 5199 | ValueInfo VI = getValueInfoFromValueId(ValueID).first; |
| 5200 | LastSeenGUID = VI.getGUID(); |
| 5201 | TheIndex.addGlobalValueSummary(VI, std::move(FS)); |
Teresa Johnson | bbe0545 | 2016-02-24 17:57:28 +0000 | [diff] [blame] | 5202 | break; |
Teresa Johnson | f72278f | 2015-11-02 18:02:11 +0000 | [diff] [blame] | 5203 | } |
Mehdi Amini | ae64eaf | 2016-04-23 23:38:17 +0000 | [diff] [blame] | 5204 | // FS_COMBINED_ORIGINAL_NAME: [original_name] |
| 5205 | case bitc::FS_COMBINED_ORIGINAL_NAME: { |
| 5206 | uint64_t OriginalName = Record[0]; |
| 5207 | if (!LastSeenSummary) |
| 5208 | return error("Name attachment that does not follow a combined record"); |
| 5209 | LastSeenSummary->setOriginalName(OriginalName); |
Dehao Chen | 4a435e0 | 2017-03-14 17:33:01 +0000 | [diff] [blame] | 5210 | TheIndex.addOriginalName(LastSeenGUID, OriginalName); |
Mehdi Amini | ae64eaf | 2016-04-23 23:38:17 +0000 | [diff] [blame] | 5211 | // Reset the LastSeenSummary |
| 5212 | LastSeenSummary = nullptr; |
Dehao Chen | 4a435e0 | 2017-03-14 17:33:01 +0000 | [diff] [blame] | 5213 | LastSeenGUID = 0; |
Peter Collingbourne | 1b4137a7 | 2016-12-21 23:03:45 +0000 | [diff] [blame] | 5214 | break; |
| 5215 | } |
| 5216 | case bitc::FS_TYPE_TESTS: { |
| 5217 | assert(PendingTypeTests.empty()); |
| 5218 | PendingTypeTests.insert(PendingTypeTests.end(), Record.begin(), |
| 5219 | Record.end()); |
| 5220 | break; |
Mehdi Amini | ae64eaf | 2016-04-23 23:38:17 +0000 | [diff] [blame] | 5221 | } |
Peter Collingbourne | be9ffaa | 2017-02-10 22:29:38 +0000 | [diff] [blame] | 5222 | case bitc::FS_TYPE_TEST_ASSUME_VCALLS: { |
| 5223 | assert(PendingTypeTestAssumeVCalls.empty()); |
| 5224 | for (unsigned I = 0; I != Record.size(); I += 2) |
| 5225 | PendingTypeTestAssumeVCalls.push_back({Record[I], Record[I+1]}); |
| 5226 | break; |
| 5227 | } |
| 5228 | case bitc::FS_TYPE_CHECKED_LOAD_VCALLS: { |
| 5229 | assert(PendingTypeCheckedLoadVCalls.empty()); |
| 5230 | for (unsigned I = 0; I != Record.size(); I += 2) |
| 5231 | PendingTypeCheckedLoadVCalls.push_back({Record[I], Record[I+1]}); |
| 5232 | break; |
| 5233 | } |
| 5234 | case bitc::FS_TYPE_TEST_ASSUME_CONST_VCALL: { |
| 5235 | PendingTypeTestAssumeConstVCalls.push_back( |
| 5236 | {{Record[0], Record[1]}, {Record.begin() + 2, Record.end()}}); |
| 5237 | break; |
| 5238 | } |
| 5239 | case bitc::FS_TYPE_CHECKED_LOAD_CONST_VCALL: { |
| 5240 | PendingTypeCheckedLoadConstVCalls.push_back( |
| 5241 | {{Record[0], Record[1]}, {Record.begin() + 2, Record.end()}}); |
| 5242 | break; |
| 5243 | } |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 5244 | } |
| 5245 | } |
| 5246 | llvm_unreachable("Exit infinite loop"); |
| 5247 | } |
| 5248 | |
| 5249 | // Parse the module string table block into the Index. |
| 5250 | // This populates the ModulePathStringTable map in the index. |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 5251 | Error ModuleSummaryIndexBitcodeReader::parseModuleStringTable() { |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 5252 | if (Stream.EnterSubBlock(bitc::MODULE_STRTAB_BLOCK_ID)) |
| 5253 | return error("Invalid record"); |
| 5254 | |
| 5255 | SmallVector<uint64_t, 64> Record; |
| 5256 | |
| 5257 | SmallString<128> ModulePath; |
Mehdi Amini | d7ad221 | 2016-04-01 05:33:11 +0000 | [diff] [blame] | 5258 | ModulePathStringTableTy::iterator LastSeenModulePath; |
Eugene Zelenko | 1804a77 | 2016-08-25 00:45:04 +0000 | [diff] [blame] | 5259 | |
| 5260 | while (true) { |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 5261 | BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); |
| 5262 | |
| 5263 | switch (Entry.Kind) { |
Teresa Johnson | f72278f | 2015-11-02 18:02:11 +0000 | [diff] [blame] | 5264 | case BitstreamEntry::SubBlock: // Handled for us already. |
| 5265 | case BitstreamEntry::Error: |
| 5266 | return error("Malformed block"); |
| 5267 | case BitstreamEntry::EndBlock: |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 5268 | return Error::success(); |
Teresa Johnson | f72278f | 2015-11-02 18:02:11 +0000 | [diff] [blame] | 5269 | case BitstreamEntry::Record: |
| 5270 | // The interesting case. |
| 5271 | break; |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 5272 | } |
| 5273 | |
| 5274 | Record.clear(); |
| 5275 | switch (Stream.readRecord(Entry.ID, Record)) { |
Teresa Johnson | f72278f | 2015-11-02 18:02:11 +0000 | [diff] [blame] | 5276 | default: // Default behavior: ignore. |
| 5277 | break; |
| 5278 | case bitc::MST_CODE_ENTRY: { |
| 5279 | // MST_ENTRY: [modid, namechar x N] |
Mehdi Amini | d7ad221 | 2016-04-01 05:33:11 +0000 | [diff] [blame] | 5280 | uint64_t ModuleId = Record[0]; |
| 5281 | |
Teresa Johnson | f72278f | 2015-11-02 18:02:11 +0000 | [diff] [blame] | 5282 | if (convertToString(Record, 1, ModulePath)) |
| 5283 | return error("Invalid record"); |
Mehdi Amini | d7ad221 | 2016-04-01 05:33:11 +0000 | [diff] [blame] | 5284 | |
Peter Collingbourne | 57f9b8c | 2016-12-01 06:21:08 +0000 | [diff] [blame] | 5285 | LastSeenModulePath = TheIndex.addModulePath(ModulePath, ModuleId); |
Mehdi Amini | d7ad221 | 2016-04-01 05:33:11 +0000 | [diff] [blame] | 5286 | ModuleIdMap[ModuleId] = LastSeenModulePath->first(); |
| 5287 | |
Teresa Johnson | f72278f | 2015-11-02 18:02:11 +0000 | [diff] [blame] | 5288 | ModulePath.clear(); |
| 5289 | break; |
| 5290 | } |
Mehdi Amini | d7ad221 | 2016-04-01 05:33:11 +0000 | [diff] [blame] | 5291 | /// MST_CODE_HASH: [5*i32] |
| 5292 | case bitc::MST_CODE_HASH: { |
| 5293 | if (Record.size() != 5) |
| 5294 | return error("Invalid hash length " + Twine(Record.size()).str()); |
Peter Collingbourne | 57f9b8c | 2016-12-01 06:21:08 +0000 | [diff] [blame] | 5295 | if (LastSeenModulePath == TheIndex.modulePaths().end()) |
Mehdi Amini | d7ad221 | 2016-04-01 05:33:11 +0000 | [diff] [blame] | 5296 | return error("Invalid hash that does not follow a module path"); |
| 5297 | int Pos = 0; |
| 5298 | for (auto &Val : Record) { |
| 5299 | assert(!(Val >> 32) && "Unexpected high bits set"); |
| 5300 | LastSeenModulePath->second.second[Pos++] = Val; |
| 5301 | } |
| 5302 | // Reset LastSeenModulePath to avoid overriding the hash unexpectedly. |
Peter Collingbourne | 57f9b8c | 2016-12-01 06:21:08 +0000 | [diff] [blame] | 5303 | LastSeenModulePath = TheIndex.modulePaths().end(); |
Mehdi Amini | d7ad221 | 2016-04-01 05:33:11 +0000 | [diff] [blame] | 5304 | break; |
| 5305 | } |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 5306 | } |
| 5307 | } |
| 5308 | llvm_unreachable("Exit infinite loop"); |
| 5309 | } |
| 5310 | |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 5311 | namespace { |
Eugene Zelenko | 1804a77 | 2016-08-25 00:45:04 +0000 | [diff] [blame] | 5312 | |
Peter Collingbourne | 4718f8b | 2016-05-24 20:13:46 +0000 | [diff] [blame] | 5313 | // FIXME: This class is only here to support the transition to llvm::Error. It |
| 5314 | // will be removed once this transition is complete. Clients should prefer to |
| 5315 | // deal with the Error value directly, rather than converting to error_code. |
Rafael Espindola | 25188c9 | 2014-06-12 01:45:43 +0000 | [diff] [blame] | 5316 | class BitcodeErrorCategoryType : public std::error_category { |
Reid Kleckner | 990504e | 2016-10-19 23:52:38 +0000 | [diff] [blame] | 5317 | const char *name() const noexcept override { |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 5318 | return "llvm.bitcode"; |
| 5319 | } |
Craig Topper | 7315602 | 2014-03-02 09:09:27 +0000 | [diff] [blame] | 5320 | std::string message(int IE) const override { |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 5321 | BitcodeError E = static_cast<BitcodeError>(IE); |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 5322 | switch (E) { |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 5323 | case BitcodeError::CorruptedBitcode: |
| 5324 | return "Corrupted bitcode"; |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 5325 | } |
Benjamin Kramer | 77db163 | 2013-11-05 13:45:09 +0000 | [diff] [blame] | 5326 | llvm_unreachable("Unknown error type!"); |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 5327 | } |
| 5328 | }; |
Eugene Zelenko | 1804a77 | 2016-08-25 00:45:04 +0000 | [diff] [blame] | 5329 | |
Eugene Zelenko | 6ac3f73 | 2016-01-26 18:48:36 +0000 | [diff] [blame] | 5330 | } // end anonymous namespace |
Rafael Espindola | 48da4f4 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 5331 | |
Chris Bieneman | 770163e | 2014-09-19 20:29:02 +0000 | [diff] [blame] | 5332 | static ManagedStatic<BitcodeErrorCategoryType> ErrorCategory; |
| 5333 | |
Rafael Espindola | c3f2e73 | 2014-07-29 20:22:46 +0000 | [diff] [blame] | 5334 | const std::error_category &llvm::BitcodeErrorCategory() { |
Chris Bieneman | 770163e | 2014-09-19 20:29:02 +0000 | [diff] [blame] | 5335 | return *ErrorCategory; |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 5336 | } |
Chris Lattner | 51ffe7c | 2007-05-01 04:59:48 +0000 | [diff] [blame] | 5337 | |
Peter Collingbourne | a0f371a | 2017-04-17 17:51:36 +0000 | [diff] [blame] | 5338 | static Expected<StringRef> readStrtab(BitstreamCursor &Stream) { |
| 5339 | if (Stream.EnterSubBlock(bitc::STRTAB_BLOCK_ID)) |
| 5340 | return error("Invalid record"); |
| 5341 | |
| 5342 | StringRef Strtab; |
| 5343 | while (1) { |
| 5344 | BitstreamEntry Entry = Stream.advance(); |
| 5345 | switch (Entry.Kind) { |
| 5346 | case BitstreamEntry::EndBlock: |
| 5347 | return Strtab; |
| 5348 | |
| 5349 | case BitstreamEntry::Error: |
| 5350 | return error("Malformed block"); |
| 5351 | |
| 5352 | case BitstreamEntry::SubBlock: |
| 5353 | if (Stream.SkipBlock()) |
| 5354 | return error("Malformed block"); |
| 5355 | break; |
| 5356 | |
| 5357 | case BitstreamEntry::Record: |
| 5358 | StringRef Blob; |
| 5359 | SmallVector<uint64_t, 1> Record; |
| 5360 | if (Stream.readRecord(Entry.ID, Record, &Blob) == bitc::STRTAB_BLOB) |
| 5361 | Strtab = Blob; |
| 5362 | break; |
| 5363 | } |
| 5364 | } |
| 5365 | } |
| 5366 | |
Chris Lattner | 6694f60 | 2007-04-29 07:54:31 +0000 | [diff] [blame] | 5367 | //===----------------------------------------------------------------------===// |
| 5368 | // External interface |
| 5369 | //===----------------------------------------------------------------------===// |
| 5370 | |
Peter Collingbourne | 7a74803 | 2016-11-16 21:44:45 +0000 | [diff] [blame] | 5371 | Expected<std::vector<BitcodeModule>> |
| 5372 | llvm::getBitcodeModuleList(MemoryBufferRef Buffer) { |
| 5373 | Expected<BitstreamCursor> StreamOrErr = initStream(Buffer); |
| 5374 | if (!StreamOrErr) |
| 5375 | return StreamOrErr.takeError(); |
| 5376 | BitstreamCursor &Stream = *StreamOrErr; |
| 5377 | |
Peter Collingbourne | 7a74803 | 2016-11-16 21:44:45 +0000 | [diff] [blame] | 5378 | std::vector<BitcodeModule> Modules; |
| 5379 | while (true) { |
Peter Collingbourne | bfcf980 | 2016-11-29 02:27:04 +0000 | [diff] [blame] | 5380 | uint64_t BCBegin = Stream.getCurrentByteNo(); |
| 5381 | |
Peter Collingbourne | 7a74803 | 2016-11-16 21:44:45 +0000 | [diff] [blame] | 5382 | // We may be consuming bitcode from a client that leaves garbage at the end |
| 5383 | // of the bitcode stream (e.g. Apple's ar tool). If we are close enough to |
| 5384 | // the end that there cannot possibly be another module, stop looking. |
Peter Collingbourne | bfcf980 | 2016-11-29 02:27:04 +0000 | [diff] [blame] | 5385 | if (BCBegin + 8 >= Stream.getBitcodeBytes().size()) |
Peter Collingbourne | 7a74803 | 2016-11-16 21:44:45 +0000 | [diff] [blame] | 5386 | return Modules; |
| 5387 | |
| 5388 | BitstreamEntry Entry = Stream.advance(); |
| 5389 | switch (Entry.Kind) { |
| 5390 | case BitstreamEntry::EndBlock: |
| 5391 | case BitstreamEntry::Error: |
| 5392 | return error("Malformed block"); |
| 5393 | |
Peter Collingbourne | bfcf980 | 2016-11-29 02:27:04 +0000 | [diff] [blame] | 5394 | case BitstreamEntry::SubBlock: { |
| 5395 | uint64_t IdentificationBit = -1ull; |
| 5396 | if (Entry.ID == bitc::IDENTIFICATION_BLOCK_ID) { |
| 5397 | IdentificationBit = Stream.GetCurrentBitNo() - BCBegin * 8; |
| 5398 | if (Stream.SkipBlock()) |
| 5399 | return error("Malformed block"); |
| 5400 | |
| 5401 | Entry = Stream.advance(); |
| 5402 | if (Entry.Kind != BitstreamEntry::SubBlock || |
| 5403 | Entry.ID != bitc::MODULE_BLOCK_ID) |
| 5404 | return error("Malformed block"); |
| 5405 | } |
| 5406 | |
| 5407 | if (Entry.ID == bitc::MODULE_BLOCK_ID) { |
| 5408 | uint64_t ModuleBit = Stream.GetCurrentBitNo() - BCBegin * 8; |
| 5409 | if (Stream.SkipBlock()) |
| 5410 | return error("Malformed block"); |
| 5411 | |
| 5412 | Modules.push_back({Stream.getBitcodeBytes().slice( |
| 5413 | BCBegin, Stream.getCurrentByteNo() - BCBegin), |
Peter Collingbourne | 7a74803 | 2016-11-16 21:44:45 +0000 | [diff] [blame] | 5414 | Buffer.getBufferIdentifier(), IdentificationBit, |
Peter Collingbourne | bfcf980 | 2016-11-29 02:27:04 +0000 | [diff] [blame] | 5415 | ModuleBit}); |
| 5416 | continue; |
| 5417 | } |
Peter Collingbourne | 7a74803 | 2016-11-16 21:44:45 +0000 | [diff] [blame] | 5418 | |
Peter Collingbourne | a0f371a | 2017-04-17 17:51:36 +0000 | [diff] [blame] | 5419 | if (Entry.ID == bitc::STRTAB_BLOCK_ID) { |
| 5420 | Expected<StringRef> Strtab = readStrtab(Stream); |
| 5421 | if (!Strtab) |
| 5422 | return Strtab.takeError(); |
| 5423 | // This string table is used by every preceding bitcode module that does |
| 5424 | // not have its own string table. A bitcode file may have multiple |
| 5425 | // string tables if it was created by binary concatenation, for example |
| 5426 | // with "llvm-cat -b". |
| 5427 | for (auto I = Modules.rbegin(), E = Modules.rend(); I != E; ++I) { |
| 5428 | if (!I->Strtab.empty()) |
| 5429 | break; |
| 5430 | I->Strtab = *Strtab; |
| 5431 | } |
| 5432 | continue; |
| 5433 | } |
| 5434 | |
Peter Collingbourne | 7a74803 | 2016-11-16 21:44:45 +0000 | [diff] [blame] | 5435 | if (Stream.SkipBlock()) |
| 5436 | return error("Malformed block"); |
| 5437 | continue; |
Peter Collingbourne | bfcf980 | 2016-11-29 02:27:04 +0000 | [diff] [blame] | 5438 | } |
Peter Collingbourne | 7a74803 | 2016-11-16 21:44:45 +0000 | [diff] [blame] | 5439 | case BitstreamEntry::Record: |
| 5440 | Stream.skipRecord(Entry.ID); |
| 5441 | continue; |
| 5442 | } |
| 5443 | } |
| 5444 | } |
| 5445 | |
Duncan P. N. Exon Smith | 6e1009b | 2014-08-01 22:27:19 +0000 | [diff] [blame] | 5446 | /// \brief Get a lazy one-at-time loading module from bitcode. |
Chris Lattner | 6694f60 | 2007-04-29 07:54:31 +0000 | [diff] [blame] | 5447 | /// |
Duncan P. N. Exon Smith | 6e1009b | 2014-08-01 22:27:19 +0000 | [diff] [blame] | 5448 | /// This isn't always used in a lazy context. In particular, it's also used by |
Peter Collingbourne | 7a74803 | 2016-11-16 21:44:45 +0000 | [diff] [blame] | 5449 | /// \a parseModule(). If this is truly lazy, then we need to eagerly pull |
Duncan P. N. Exon Smith | 6e1009b | 2014-08-01 22:27:19 +0000 | [diff] [blame] | 5450 | /// in forward-referenced functions from block address references. |
| 5451 | /// |
Rafael Espindola | 728074b | 2015-06-17 00:40:56 +0000 | [diff] [blame] | 5452 | /// \param[in] MaterializeAll Set to \c true if we should materialize |
| 5453 | /// everything. |
Peter Collingbourne | 7a74803 | 2016-11-16 21:44:45 +0000 | [diff] [blame] | 5454 | Expected<std::unique_ptr<Module>> |
| 5455 | BitcodeModule::getModuleImpl(LLVMContext &Context, bool MaterializeAll, |
Teresa Johnson | a61f5e3 | 2016-12-16 21:25:01 +0000 | [diff] [blame] | 5456 | bool ShouldLazyLoadMetadata, bool IsImporting) { |
Peter Collingbourne | 7a74803 | 2016-11-16 21:44:45 +0000 | [diff] [blame] | 5457 | BitstreamCursor Stream(Buffer); |
Peter Collingbourne | c0032b7 | 2016-11-11 19:50:10 +0000 | [diff] [blame] | 5458 | |
Peter Collingbourne | 7a74803 | 2016-11-16 21:44:45 +0000 | [diff] [blame] | 5459 | std::string ProducerIdentification; |
| 5460 | if (IdentificationBit != -1ull) { |
| 5461 | Stream.JumpToBit(IdentificationBit); |
| 5462 | Expected<std::string> ProducerIdentificationOrErr = |
| 5463 | readIdentificationBlock(Stream); |
| 5464 | if (!ProducerIdentificationOrErr) |
| 5465 | return ProducerIdentificationOrErr.takeError(); |
| 5466 | |
| 5467 | ProducerIdentification = *ProducerIdentificationOrErr; |
| 5468 | } |
| 5469 | |
| 5470 | Stream.JumpToBit(ModuleBit); |
Peter Collingbourne | a0f371a | 2017-04-17 17:51:36 +0000 | [diff] [blame] | 5471 | auto *R = new BitcodeReader(std::move(Stream), Strtab, ProducerIdentification, |
| 5472 | Context); |
Duncan P. N. Exon Smith | 908d809 | 2014-08-01 21:11:34 +0000 | [diff] [blame] | 5473 | |
Peter Collingbourne | e2dcf7c | 2016-11-08 06:03:43 +0000 | [diff] [blame] | 5474 | std::unique_ptr<Module> M = |
Peter Collingbourne | 7a74803 | 2016-11-16 21:44:45 +0000 | [diff] [blame] | 5475 | llvm::make_unique<Module>(ModuleIdentifier, Context); |
Peter Collingbourne | e2dcf7c | 2016-11-08 06:03:43 +0000 | [diff] [blame] | 5476 | M->setMaterializer(R); |
Rafael Espindola | b799346 | 2012-01-02 07:49:53 +0000 | [diff] [blame] | 5477 | |
Peter Collingbourne | e2dcf7c | 2016-11-08 06:03:43 +0000 | [diff] [blame] | 5478 | // Delay parsing Metadata if ShouldLazyLoadMetadata is true. |
Teresa Johnson | a61f5e3 | 2016-12-16 21:25:01 +0000 | [diff] [blame] | 5479 | if (Error Err = |
| 5480 | R->parseBitcodeInto(M.get(), ShouldLazyLoadMetadata, IsImporting)) |
Peter Collingbourne | d9445c4 | 2016-11-13 07:00:17 +0000 | [diff] [blame] | 5481 | return std::move(Err); |
Peter Collingbourne | e2dcf7c | 2016-11-08 06:03:43 +0000 | [diff] [blame] | 5482 | |
| 5483 | if (MaterializeAll) { |
| 5484 | // Read in the entire module, and destroy the BitcodeReader. |
Peter Collingbourne | 7f00d0a | 2016-11-09 17:49:19 +0000 | [diff] [blame] | 5485 | if (Error Err = M->materializeAll()) |
Peter Collingbourne | d9445c4 | 2016-11-13 07:00:17 +0000 | [diff] [blame] | 5486 | return std::move(Err); |
Peter Collingbourne | e2dcf7c | 2016-11-08 06:03:43 +0000 | [diff] [blame] | 5487 | } else { |
| 5488 | // Resolve forward references from blockaddresses. |
Peter Collingbourne | 58f7f07 | 2016-11-09 00:51:04 +0000 | [diff] [blame] | 5489 | if (Error Err = R->materializeForwardReferencedFunctions()) |
Peter Collingbourne | d9445c4 | 2016-11-13 07:00:17 +0000 | [diff] [blame] | 5490 | return std::move(Err); |
Peter Collingbourne | e2dcf7c | 2016-11-08 06:03:43 +0000 | [diff] [blame] | 5491 | } |
| 5492 | return std::move(M); |
Chris Lattner | 6694f60 | 2007-04-29 07:54:31 +0000 | [diff] [blame] | 5493 | } |
| 5494 | |
Peter Collingbourne | d9445c4 | 2016-11-13 07:00:17 +0000 | [diff] [blame] | 5495 | Expected<std::unique_ptr<Module>> |
Teresa Johnson | a61f5e3 | 2016-12-16 21:25:01 +0000 | [diff] [blame] | 5496 | BitcodeModule::getLazyModule(LLVMContext &Context, bool ShouldLazyLoadMetadata, |
| 5497 | bool IsImporting) { |
| 5498 | return getModuleImpl(Context, false, ShouldLazyLoadMetadata, IsImporting); |
Peter Collingbourne | 7a74803 | 2016-11-16 21:44:45 +0000 | [diff] [blame] | 5499 | } |
| 5500 | |
Peter Collingbourne | 74d22dd | 2017-05-01 22:04:36 +0000 | [diff] [blame] | 5501 | // Parse the specified bitcode buffer and merge the index into CombinedIndex. |
| 5502 | Error BitcodeModule::readSummary(ModuleSummaryIndex &CombinedIndex, |
| 5503 | unsigned ModuleId) { |
| 5504 | BitstreamCursor Stream(Buffer); |
| 5505 | Stream.JumpToBit(ModuleBit); |
| 5506 | |
| 5507 | ModuleSummaryIndexBitcodeReader R(std::move(Stream), Strtab, CombinedIndex, |
| 5508 | ModuleIdentifier, ModuleId); |
Peter Collingbourne | 440e204 | 2017-05-01 22:48:10 +0000 | [diff] [blame] | 5509 | return R.parseModule(); |
Peter Collingbourne | 74d22dd | 2017-05-01 22:04:36 +0000 | [diff] [blame] | 5510 | } |
| 5511 | |
Peter Collingbourne | a46ec9f | 2016-12-01 06:00:53 +0000 | [diff] [blame] | 5512 | // Parse the specified bitcode buffer, returning the function info index. |
| 5513 | Expected<std::unique_ptr<ModuleSummaryIndex>> BitcodeModule::getSummary() { |
| 5514 | BitstreamCursor Stream(Buffer); |
| 5515 | Stream.JumpToBit(ModuleBit); |
| 5516 | |
| 5517 | auto Index = llvm::make_unique<ModuleSummaryIndex>(); |
Peter Collingbourne | 74d22dd | 2017-05-01 22:04:36 +0000 | [diff] [blame] | 5518 | ModuleSummaryIndexBitcodeReader R(std::move(Stream), Strtab, *Index, |
| 5519 | ModuleIdentifier, 0); |
Peter Collingbourne | a46ec9f | 2016-12-01 06:00:53 +0000 | [diff] [blame] | 5520 | |
Peter Collingbourne | 74d22dd | 2017-05-01 22:04:36 +0000 | [diff] [blame] | 5521 | if (Error Err = R.parseModule()) |
Peter Collingbourne | a46ec9f | 2016-12-01 06:00:53 +0000 | [diff] [blame] | 5522 | return std::move(Err); |
| 5523 | |
| 5524 | return std::move(Index); |
| 5525 | } |
| 5526 | |
| 5527 | // Check if the given bitcode buffer contains a global value summary block. |
| 5528 | Expected<bool> BitcodeModule::hasSummary() { |
| 5529 | BitstreamCursor Stream(Buffer); |
| 5530 | Stream.JumpToBit(ModuleBit); |
| 5531 | |
| 5532 | if (Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID)) |
| 5533 | return error("Invalid record"); |
| 5534 | |
| 5535 | while (true) { |
| 5536 | BitstreamEntry Entry = Stream.advance(); |
| 5537 | |
| 5538 | switch (Entry.Kind) { |
| 5539 | case BitstreamEntry::Error: |
| 5540 | return error("Malformed block"); |
| 5541 | case BitstreamEntry::EndBlock: |
| 5542 | return false; |
| 5543 | |
| 5544 | case BitstreamEntry::SubBlock: |
| 5545 | if (Entry.ID == bitc::GLOBALVAL_SUMMARY_BLOCK_ID) |
| 5546 | return true; |
| 5547 | |
| 5548 | // Ignore other sub-blocks. |
| 5549 | if (Stream.SkipBlock()) |
| 5550 | return error("Malformed block"); |
| 5551 | continue; |
| 5552 | |
| 5553 | case BitstreamEntry::Record: |
| 5554 | Stream.skipRecord(Entry.ID); |
| 5555 | continue; |
| 5556 | } |
| 5557 | } |
| 5558 | } |
| 5559 | |
| 5560 | static Expected<BitcodeModule> getSingleModule(MemoryBufferRef Buffer) { |
Peter Collingbourne | 7a74803 | 2016-11-16 21:44:45 +0000 | [diff] [blame] | 5561 | Expected<std::vector<BitcodeModule>> MsOrErr = getBitcodeModuleList(Buffer); |
| 5562 | if (!MsOrErr) |
| 5563 | return MsOrErr.takeError(); |
| 5564 | |
| 5565 | if (MsOrErr->size() != 1) |
| 5566 | return error("Expected a single module"); |
| 5567 | |
Peter Collingbourne | a46ec9f | 2016-12-01 06:00:53 +0000 | [diff] [blame] | 5568 | return (*MsOrErr)[0]; |
| 5569 | } |
| 5570 | |
| 5571 | Expected<std::unique_ptr<Module>> |
Teresa Johnson | a61f5e3 | 2016-12-16 21:25:01 +0000 | [diff] [blame] | 5572 | llvm::getLazyBitcodeModule(MemoryBufferRef Buffer, LLVMContext &Context, |
| 5573 | bool ShouldLazyLoadMetadata, bool IsImporting) { |
Peter Collingbourne | a46ec9f | 2016-12-01 06:00:53 +0000 | [diff] [blame] | 5574 | Expected<BitcodeModule> BM = getSingleModule(Buffer); |
| 5575 | if (!BM) |
| 5576 | return BM.takeError(); |
| 5577 | |
Teresa Johnson | a61f5e3 | 2016-12-16 21:25:01 +0000 | [diff] [blame] | 5578 | return BM->getLazyModule(Context, ShouldLazyLoadMetadata, IsImporting); |
Duncan P. N. Exon Smith | 6e1009b | 2014-08-01 22:27:19 +0000 | [diff] [blame] | 5579 | } |
Derek Schuff | 8b2dcad | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 5580 | |
Teresa Johnson | a61f5e3 | 2016-12-16 21:25:01 +0000 | [diff] [blame] | 5581 | Expected<std::unique_ptr<Module>> llvm::getOwningLazyBitcodeModule( |
| 5582 | std::unique_ptr<MemoryBuffer> &&Buffer, LLVMContext &Context, |
| 5583 | bool ShouldLazyLoadMetadata, bool IsImporting) { |
| 5584 | auto MOrErr = getLazyBitcodeModule(*Buffer, Context, ShouldLazyLoadMetadata, |
| 5585 | IsImporting); |
Peter Collingbourne | e2dcf7c | 2016-11-08 06:03:43 +0000 | [diff] [blame] | 5586 | if (MOrErr) |
| 5587 | (*MOrErr)->setOwnedMemoryBuffer(std::move(Buffer)); |
| 5588 | return MOrErr; |
| 5589 | } |
| 5590 | |
Peter Collingbourne | 7a74803 | 2016-11-16 21:44:45 +0000 | [diff] [blame] | 5591 | Expected<std::unique_ptr<Module>> |
| 5592 | BitcodeModule::parseModule(LLVMContext &Context) { |
Teresa Johnson | a61f5e3 | 2016-12-16 21:25:01 +0000 | [diff] [blame] | 5593 | return getModuleImpl(Context, true, false, false); |
Chad Rosier | ca2567b | 2011-12-07 21:44:12 +0000 | [diff] [blame] | 5594 | // TODO: Restore the use-lists to the in-memory state when the bitcode was |
| 5595 | // written. We must defer until the Module has been fully materialized. |
Chris Lattner | 6694f60 | 2007-04-29 07:54:31 +0000 | [diff] [blame] | 5596 | } |
Bill Wendling | 0198ce0 | 2010-10-06 01:22:42 +0000 | [diff] [blame] | 5597 | |
Peter Collingbourne | 7a74803 | 2016-11-16 21:44:45 +0000 | [diff] [blame] | 5598 | Expected<std::unique_ptr<Module>> llvm::parseBitcodeFile(MemoryBufferRef Buffer, |
| 5599 | LLVMContext &Context) { |
Peter Collingbourne | a46ec9f | 2016-12-01 06:00:53 +0000 | [diff] [blame] | 5600 | Expected<BitcodeModule> BM = getSingleModule(Buffer); |
| 5601 | if (!BM) |
| 5602 | return BM.takeError(); |
Peter Collingbourne | 7a74803 | 2016-11-16 21:44:45 +0000 | [diff] [blame] | 5603 | |
Peter Collingbourne | a46ec9f | 2016-12-01 06:00:53 +0000 | [diff] [blame] | 5604 | return BM->parseModule(Context); |
Peter Collingbourne | 7a74803 | 2016-11-16 21:44:45 +0000 | [diff] [blame] | 5605 | } |
| 5606 | |
Peter Collingbourne | cd513a4 | 2016-11-11 19:50:24 +0000 | [diff] [blame] | 5607 | Expected<std::string> llvm::getBitcodeTargetTriple(MemoryBufferRef Buffer) { |
| 5608 | Expected<BitstreamCursor> StreamOrErr = initStream(Buffer); |
Peter Collingbourne | c0032b7 | 2016-11-11 19:50:10 +0000 | [diff] [blame] | 5609 | if (!StreamOrErr) |
Peter Collingbourne | cd513a4 | 2016-11-11 19:50:24 +0000 | [diff] [blame] | 5610 | return StreamOrErr.takeError(); |
Peter Collingbourne | c0032b7 | 2016-11-11 19:50:10 +0000 | [diff] [blame] | 5611 | |
Peter Collingbourne | cd513a4 | 2016-11-11 19:50:24 +0000 | [diff] [blame] | 5612 | return readTriple(*StreamOrErr); |
Bill Wendling | 0198ce0 | 2010-10-06 01:22:42 +0000 | [diff] [blame] | 5613 | } |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 5614 | |
Peter Collingbourne | cd513a4 | 2016-11-11 19:50:24 +0000 | [diff] [blame] | 5615 | Expected<bool> llvm::isBitcodeContainingObjCCategory(MemoryBufferRef Buffer) { |
| 5616 | Expected<BitstreamCursor> StreamOrErr = initStream(Buffer); |
Peter Collingbourne | c0032b7 | 2016-11-11 19:50:10 +0000 | [diff] [blame] | 5617 | if (!StreamOrErr) |
Peter Collingbourne | cd513a4 | 2016-11-11 19:50:24 +0000 | [diff] [blame] | 5618 | return StreamOrErr.takeError(); |
Peter Collingbourne | c0032b7 | 2016-11-11 19:50:10 +0000 | [diff] [blame] | 5619 | |
Peter Collingbourne | cd513a4 | 2016-11-11 19:50:24 +0000 | [diff] [blame] | 5620 | return hasObjCCategory(*StreamOrErr); |
Mehdi Amini | e75aa6f | 2016-07-11 23:10:18 +0000 | [diff] [blame] | 5621 | } |
| 5622 | |
Peter Collingbourne | cd513a4 | 2016-11-11 19:50:24 +0000 | [diff] [blame] | 5623 | Expected<std::string> llvm::getBitcodeProducerString(MemoryBufferRef Buffer) { |
| 5624 | Expected<BitstreamCursor> StreamOrErr = initStream(Buffer); |
Peter Collingbourne | c0032b7 | 2016-11-11 19:50:10 +0000 | [diff] [blame] | 5625 | if (!StreamOrErr) |
Peter Collingbourne | cd513a4 | 2016-11-11 19:50:24 +0000 | [diff] [blame] | 5626 | return StreamOrErr.takeError(); |
| 5627 | |
| 5628 | return readIdentificationCode(*StreamOrErr); |
Mehdi Amini | 3383ccc | 2015-11-09 02:46:41 +0000 | [diff] [blame] | 5629 | } |
| 5630 | |
Peter Collingbourne | 74d22dd | 2017-05-01 22:04:36 +0000 | [diff] [blame] | 5631 | Error llvm::readModuleSummaryIndex(MemoryBufferRef Buffer, |
| 5632 | ModuleSummaryIndex &CombinedIndex, |
| 5633 | unsigned ModuleId) { |
| 5634 | Expected<BitcodeModule> BM = getSingleModule(Buffer); |
| 5635 | if (!BM) |
| 5636 | return BM.takeError(); |
| 5637 | |
| 5638 | return BM->readSummary(CombinedIndex, ModuleId); |
| 5639 | } |
| 5640 | |
Peter Collingbourne | 6de481a | 2016-11-11 19:50:39 +0000 | [diff] [blame] | 5641 | Expected<std::unique_ptr<ModuleSummaryIndex>> |
| 5642 | llvm::getModuleSummaryIndex(MemoryBufferRef Buffer) { |
Peter Collingbourne | a46ec9f | 2016-12-01 06:00:53 +0000 | [diff] [blame] | 5643 | Expected<BitcodeModule> BM = getSingleModule(Buffer); |
| 5644 | if (!BM) |
| 5645 | return BM.takeError(); |
Peter Collingbourne | c0032b7 | 2016-11-11 19:50:10 +0000 | [diff] [blame] | 5646 | |
Peter Collingbourne | a46ec9f | 2016-12-01 06:00:53 +0000 | [diff] [blame] | 5647 | return BM->getSummary(); |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 5648 | } |
| 5649 | |
Peter Collingbourne | cd513a4 | 2016-11-11 19:50:24 +0000 | [diff] [blame] | 5650 | Expected<bool> llvm::hasGlobalValueSummary(MemoryBufferRef Buffer) { |
Peter Collingbourne | a46ec9f | 2016-12-01 06:00:53 +0000 | [diff] [blame] | 5651 | Expected<BitcodeModule> BM = getSingleModule(Buffer); |
| 5652 | if (!BM) |
| 5653 | return BM.takeError(); |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 5654 | |
Peter Collingbourne | a46ec9f | 2016-12-01 06:00:53 +0000 | [diff] [blame] | 5655 | return BM->hasSummary(); |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 5656 | } |
Peter Collingbourne | c15d60b | 2017-05-01 20:42:32 +0000 | [diff] [blame] | 5657 | |
| 5658 | Expected<std::unique_ptr<ModuleSummaryIndex>> |
Teresa Johnson | 4cd12ce | 2017-05-12 19:32:11 +0000 | [diff] [blame] | 5659 | llvm::getModuleSummaryIndexForFile(StringRef Path, |
| 5660 | bool IgnoreEmptyThinLTOIndexFile) { |
Peter Collingbourne | c15d60b | 2017-05-01 20:42:32 +0000 | [diff] [blame] | 5661 | ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr = |
| 5662 | MemoryBuffer::getFileOrSTDIN(Path); |
| 5663 | if (!FileOrErr) |
| 5664 | return errorCodeToError(FileOrErr.getError()); |
| 5665 | if (IgnoreEmptyThinLTOIndexFile && !(*FileOrErr)->getBufferSize()) |
| 5666 | return nullptr; |
| 5667 | return getModuleSummaryIndex(**FileOrErr); |
| 5668 | } |