File: | build/source/llvm/lib/Bitcode/Reader/BitcodeReader.cpp |
Warning: | line 4200, column 5 Value stored to 'OpNum' is never read |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | //===- BitcodeReader.cpp - Internal BitcodeReader implementation ----------===// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | #include "llvm/Bitcode/BitcodeReader.h" |
10 | #include "MetadataLoader.h" |
11 | #include "ValueList.h" |
12 | #include "llvm/ADT/APFloat.h" |
13 | #include "llvm/ADT/APInt.h" |
14 | #include "llvm/ADT/ArrayRef.h" |
15 | #include "llvm/ADT/DenseMap.h" |
16 | #include "llvm/ADT/STLExtras.h" |
17 | #include "llvm/ADT/SmallString.h" |
18 | #include "llvm/ADT/SmallVector.h" |
19 | #include "llvm/ADT/StringRef.h" |
20 | #include "llvm/ADT/Twine.h" |
21 | #include "llvm/Bitcode/BitcodeCommon.h" |
22 | #include "llvm/Bitcode/LLVMBitCodes.h" |
23 | #include "llvm/Bitstream/BitstreamReader.h" |
24 | #include "llvm/Config/llvm-config.h" |
25 | #include "llvm/IR/Argument.h" |
26 | #include "llvm/IR/Attributes.h" |
27 | #include "llvm/IR/AutoUpgrade.h" |
28 | #include "llvm/IR/BasicBlock.h" |
29 | #include "llvm/IR/CallingConv.h" |
30 | #include "llvm/IR/Comdat.h" |
31 | #include "llvm/IR/Constant.h" |
32 | #include "llvm/IR/Constants.h" |
33 | #include "llvm/IR/DataLayout.h" |
34 | #include "llvm/IR/DebugInfo.h" |
35 | #include "llvm/IR/DebugInfoMetadata.h" |
36 | #include "llvm/IR/DebugLoc.h" |
37 | #include "llvm/IR/DerivedTypes.h" |
38 | #include "llvm/IR/Function.h" |
39 | #include "llvm/IR/GVMaterializer.h" |
40 | #include "llvm/IR/GetElementPtrTypeIterator.h" |
41 | #include "llvm/IR/GlobalAlias.h" |
42 | #include "llvm/IR/GlobalIFunc.h" |
43 | #include "llvm/IR/GlobalObject.h" |
44 | #include "llvm/IR/GlobalValue.h" |
45 | #include "llvm/IR/GlobalVariable.h" |
46 | #include "llvm/IR/InlineAsm.h" |
47 | #include "llvm/IR/InstIterator.h" |
48 | #include "llvm/IR/InstrTypes.h" |
49 | #include "llvm/IR/Instruction.h" |
50 | #include "llvm/IR/Instructions.h" |
51 | #include "llvm/IR/Intrinsics.h" |
52 | #include "llvm/IR/IntrinsicsAArch64.h" |
53 | #include "llvm/IR/IntrinsicsARM.h" |
54 | #include "llvm/IR/LLVMContext.h" |
55 | #include "llvm/IR/Metadata.h" |
56 | #include "llvm/IR/Module.h" |
57 | #include "llvm/IR/ModuleSummaryIndex.h" |
58 | #include "llvm/IR/Operator.h" |
59 | #include "llvm/IR/Type.h" |
60 | #include "llvm/IR/Value.h" |
61 | #include "llvm/IR/Verifier.h" |
62 | #include "llvm/Support/AtomicOrdering.h" |
63 | #include "llvm/Support/Casting.h" |
64 | #include "llvm/Support/CommandLine.h" |
65 | #include "llvm/Support/Compiler.h" |
66 | #include "llvm/Support/Debug.h" |
67 | #include "llvm/Support/Error.h" |
68 | #include "llvm/Support/ErrorHandling.h" |
69 | #include "llvm/Support/ErrorOr.h" |
70 | #include "llvm/Support/MathExtras.h" |
71 | #include "llvm/Support/MemoryBuffer.h" |
72 | #include "llvm/Support/ModRef.h" |
73 | #include "llvm/Support/raw_ostream.h" |
74 | #include "llvm/TargetParser/Triple.h" |
75 | #include <algorithm> |
76 | #include <cassert> |
77 | #include <cstddef> |
78 | #include <cstdint> |
79 | #include <deque> |
80 | #include <map> |
81 | #include <memory> |
82 | #include <optional> |
83 | #include <set> |
84 | #include <string> |
85 | #include <system_error> |
86 | #include <tuple> |
87 | #include <utility> |
88 | #include <vector> |
89 | |
90 | using namespace llvm; |
91 | |
92 | static cl::opt<bool> PrintSummaryGUIDs( |
93 | "print-summary-global-ids", cl::init(false), cl::Hidden, |
94 | cl::desc( |
95 | "Print the global id for each value when reading the module summary")); |
96 | |
97 | static cl::opt<bool> ExpandConstantExprs( |
98 | "expand-constant-exprs", cl::Hidden, |
99 | cl::desc( |
100 | "Expand constant expressions to instructions for testing purposes")); |
101 | |
102 | namespace { |
103 | |
104 | enum { |
105 | SWITCH_INST_MAGIC = 0x4B5 // May 2012 => 1205 => Hex |
106 | }; |
107 | |
108 | } // end anonymous namespace |
109 | |
110 | static Error error(const Twine &Message) { |
111 | return make_error<StringError>( |
112 | Message, make_error_code(BitcodeError::CorruptedBitcode)); |
113 | } |
114 | |
115 | static Error hasInvalidBitcodeHeader(BitstreamCursor &Stream) { |
116 | if (!Stream.canSkipToPos(4)) |
117 | return createStringError(std::errc::illegal_byte_sequence, |
118 | "file too small to contain bitcode header"); |
119 | for (unsigned C : {'B', 'C'}) |
120 | if (Expected<SimpleBitstreamCursor::word_t> Res = Stream.Read(8)) { |
121 | if (Res.get() != C) |
122 | return createStringError(std::errc::illegal_byte_sequence, |
123 | "file doesn't start with bitcode header"); |
124 | } else |
125 | return Res.takeError(); |
126 | for (unsigned C : {0x0, 0xC, 0xE, 0xD}) |
127 | if (Expected<SimpleBitstreamCursor::word_t> Res = Stream.Read(4)) { |
128 | if (Res.get() != C) |
129 | return createStringError(std::errc::illegal_byte_sequence, |
130 | "file doesn't start with bitcode header"); |
131 | } else |
132 | return Res.takeError(); |
133 | return Error::success(); |
134 | } |
135 | |
136 | static Expected<BitstreamCursor> initStream(MemoryBufferRef Buffer) { |
137 | const unsigned char *BufPtr = (const unsigned char *)Buffer.getBufferStart(); |
138 | const unsigned char *BufEnd = BufPtr + Buffer.getBufferSize(); |
139 | |
140 | if (Buffer.getBufferSize() & 3) |
141 | return error("Invalid bitcode signature"); |
142 | |
143 | // If we have a wrapper header, parse it and ignore the non-bc file contents. |
144 | // The magic number is 0x0B17C0DE stored in little endian. |
145 | if (isBitcodeWrapper(BufPtr, BufEnd)) |
146 | if (SkipBitcodeWrapperHeader(BufPtr, BufEnd, true)) |
147 | return error("Invalid bitcode wrapper header"); |
148 | |
149 | BitstreamCursor Stream(ArrayRef<uint8_t>(BufPtr, BufEnd)); |
150 | if (Error Err = hasInvalidBitcodeHeader(Stream)) |
151 | return std::move(Err); |
152 | |
153 | return std::move(Stream); |
154 | } |
155 | |
156 | /// Convert a string from a record into an std::string, return true on failure. |
157 | template <typename StrTy> |
158 | static bool convertToString(ArrayRef<uint64_t> Record, unsigned Idx, |
159 | StrTy &Result) { |
160 | if (Idx > Record.size()) |
161 | return true; |
162 | |
163 | Result.append(Record.begin() + Idx, Record.end()); |
164 | return false; |
165 | } |
166 | |
167 | // Strip all the TBAA attachment for the module. |
168 | static void stripTBAA(Module *M) { |
169 | for (auto &F : *M) { |
170 | if (F.isMaterializable()) |
171 | continue; |
172 | for (auto &I : instructions(F)) |
173 | I.setMetadata(LLVMContext::MD_tbaa, nullptr); |
174 | } |
175 | } |
176 | |
177 | /// Read the "IDENTIFICATION_BLOCK_ID" block, do some basic enforcement on the |
178 | /// "epoch" encoded in the bitcode, and return the producer name if any. |
179 | static Expected<std::string> readIdentificationBlock(BitstreamCursor &Stream) { |
180 | if (Error Err = Stream.EnterSubBlock(bitc::IDENTIFICATION_BLOCK_ID)) |
181 | return std::move(Err); |
182 | |
183 | // Read all the records. |
184 | SmallVector<uint64_t, 64> Record; |
185 | |
186 | std::string ProducerIdentification; |
187 | |
188 | while (true) { |
189 | BitstreamEntry Entry; |
190 | if (Error E = Stream.advance().moveInto(Entry)) |
191 | return std::move(E); |
192 | |
193 | switch (Entry.Kind) { |
194 | default: |
195 | case BitstreamEntry::Error: |
196 | return error("Malformed block"); |
197 | case BitstreamEntry::EndBlock: |
198 | return ProducerIdentification; |
199 | case BitstreamEntry::Record: |
200 | // The interesting case. |
201 | break; |
202 | } |
203 | |
204 | // Read a record. |
205 | Record.clear(); |
206 | Expected<unsigned> MaybeBitCode = Stream.readRecord(Entry.ID, Record); |
207 | if (!MaybeBitCode) |
208 | return MaybeBitCode.takeError(); |
209 | switch (MaybeBitCode.get()) { |
210 | default: // Default behavior: reject |
211 | return error("Invalid value"); |
212 | case bitc::IDENTIFICATION_CODE_STRING: // IDENTIFICATION: [strchr x N] |
213 | convertToString(Record, 0, ProducerIdentification); |
214 | break; |
215 | case bitc::IDENTIFICATION_CODE_EPOCH: { // EPOCH: [epoch#] |
216 | unsigned epoch = (unsigned)Record[0]; |
217 | if (epoch != bitc::BITCODE_CURRENT_EPOCH) { |
218 | return error( |
219 | Twine("Incompatible epoch: Bitcode '") + Twine(epoch) + |
220 | "' vs current: '" + Twine(bitc::BITCODE_CURRENT_EPOCH) + "'"); |
221 | } |
222 | } |
223 | } |
224 | } |
225 | } |
226 | |
227 | static Expected<std::string> readIdentificationCode(BitstreamCursor &Stream) { |
228 | // We expect a number of well-defined blocks, though we don't necessarily |
229 | // need to understand them all. |
230 | while (true) { |
231 | if (Stream.AtEndOfStream()) |
232 | return ""; |
233 | |
234 | BitstreamEntry Entry; |
235 | if (Error E = Stream.advance().moveInto(Entry)) |
236 | return std::move(E); |
237 | |
238 | switch (Entry.Kind) { |
239 | case BitstreamEntry::EndBlock: |
240 | case BitstreamEntry::Error: |
241 | return error("Malformed block"); |
242 | |
243 | case BitstreamEntry::SubBlock: |
244 | if (Entry.ID == bitc::IDENTIFICATION_BLOCK_ID) |
245 | return readIdentificationBlock(Stream); |
246 | |
247 | // Ignore other sub-blocks. |
248 | if (Error Err = Stream.SkipBlock()) |
249 | return std::move(Err); |
250 | continue; |
251 | case BitstreamEntry::Record: |
252 | if (Error E = Stream.skipRecord(Entry.ID).takeError()) |
253 | return std::move(E); |
254 | continue; |
255 | } |
256 | } |
257 | } |
258 | |
259 | static Expected<bool> hasObjCCategoryInModule(BitstreamCursor &Stream) { |
260 | if (Error Err = Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID)) |
261 | return std::move(Err); |
262 | |
263 | SmallVector<uint64_t, 64> Record; |
264 | // Read all the records for this module. |
265 | |
266 | while (true) { |
267 | Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks(); |
268 | if (!MaybeEntry) |
269 | return MaybeEntry.takeError(); |
270 | BitstreamEntry Entry = MaybeEntry.get(); |
271 | |
272 | switch (Entry.Kind) { |
273 | case BitstreamEntry::SubBlock: // Handled for us already. |
274 | case BitstreamEntry::Error: |
275 | return error("Malformed block"); |
276 | case BitstreamEntry::EndBlock: |
277 | return false; |
278 | case BitstreamEntry::Record: |
279 | // The interesting case. |
280 | break; |
281 | } |
282 | |
283 | // Read a record. |
284 | Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record); |
285 | if (!MaybeRecord) |
286 | return MaybeRecord.takeError(); |
287 | switch (MaybeRecord.get()) { |
288 | default: |
289 | break; // Default behavior, ignore unknown content. |
290 | case bitc::MODULE_CODE_SECTIONNAME: { // SECTIONNAME: [strchr x N] |
291 | std::string S; |
292 | if (convertToString(Record, 0, S)) |
293 | return error("Invalid section name record"); |
294 | // Check for the i386 and other (x86_64, ARM) conventions |
295 | if (S.find("__DATA,__objc_catlist") != std::string::npos || |
296 | S.find("__OBJC,__category") != std::string::npos) |
297 | return true; |
298 | break; |
299 | } |
300 | } |
301 | Record.clear(); |
302 | } |
303 | llvm_unreachable("Exit infinite loop")::llvm::llvm_unreachable_internal("Exit infinite loop", "llvm/lib/Bitcode/Reader/BitcodeReader.cpp" , 303); |
304 | } |
305 | |
306 | static Expected<bool> hasObjCCategory(BitstreamCursor &Stream) { |
307 | // We expect a number of well-defined blocks, though we don't necessarily |
308 | // need to understand them all. |
309 | while (true) { |
310 | BitstreamEntry Entry; |
311 | if (Error E = Stream.advance().moveInto(Entry)) |
312 | return std::move(E); |
313 | |
314 | switch (Entry.Kind) { |
315 | case BitstreamEntry::Error: |
316 | return error("Malformed block"); |
317 | case BitstreamEntry::EndBlock: |
318 | return false; |
319 | |
320 | case BitstreamEntry::SubBlock: |
321 | if (Entry.ID == bitc::MODULE_BLOCK_ID) |
322 | return hasObjCCategoryInModule(Stream); |
323 | |
324 | // Ignore other sub-blocks. |
325 | if (Error Err = Stream.SkipBlock()) |
326 | return std::move(Err); |
327 | continue; |
328 | |
329 | case BitstreamEntry::Record: |
330 | if (Error E = Stream.skipRecord(Entry.ID).takeError()) |
331 | return std::move(E); |
332 | continue; |
333 | } |
334 | } |
335 | } |
336 | |
337 | static Expected<std::string> readModuleTriple(BitstreamCursor &Stream) { |
338 | if (Error Err = Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID)) |
339 | return std::move(Err); |
340 | |
341 | SmallVector<uint64_t, 64> Record; |
342 | |
343 | std::string Triple; |
344 | |
345 | // Read all the records for this module. |
346 | while (true) { |
347 | Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks(); |
348 | if (!MaybeEntry) |
349 | return MaybeEntry.takeError(); |
350 | BitstreamEntry Entry = MaybeEntry.get(); |
351 | |
352 | switch (Entry.Kind) { |
353 | case BitstreamEntry::SubBlock: // Handled for us already. |
354 | case BitstreamEntry::Error: |
355 | return error("Malformed block"); |
356 | case BitstreamEntry::EndBlock: |
357 | return Triple; |
358 | case BitstreamEntry::Record: |
359 | // The interesting case. |
360 | break; |
361 | } |
362 | |
363 | // Read a record. |
364 | Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record); |
365 | if (!MaybeRecord) |
366 | return MaybeRecord.takeError(); |
367 | switch (MaybeRecord.get()) { |
368 | default: break; // Default behavior, ignore unknown content. |
369 | case bitc::MODULE_CODE_TRIPLE: { // TRIPLE: [strchr x N] |
370 | std::string S; |
371 | if (convertToString(Record, 0, S)) |
372 | return error("Invalid triple record"); |
373 | Triple = S; |
374 | break; |
375 | } |
376 | } |
377 | Record.clear(); |
378 | } |
379 | llvm_unreachable("Exit infinite loop")::llvm::llvm_unreachable_internal("Exit infinite loop", "llvm/lib/Bitcode/Reader/BitcodeReader.cpp" , 379); |
380 | } |
381 | |
382 | static Expected<std::string> readTriple(BitstreamCursor &Stream) { |
383 | // We expect a number of well-defined blocks, though we don't necessarily |
384 | // need to understand them all. |
385 | while (true) { |
386 | Expected<BitstreamEntry> MaybeEntry = Stream.advance(); |
387 | if (!MaybeEntry) |
388 | return MaybeEntry.takeError(); |
389 | BitstreamEntry Entry = MaybeEntry.get(); |
390 | |
391 | switch (Entry.Kind) { |
392 | case BitstreamEntry::Error: |
393 | return error("Malformed block"); |
394 | case BitstreamEntry::EndBlock: |
395 | return ""; |
396 | |
397 | case BitstreamEntry::SubBlock: |
398 | if (Entry.ID == bitc::MODULE_BLOCK_ID) |
399 | return readModuleTriple(Stream); |
400 | |
401 | // Ignore other sub-blocks. |
402 | if (Error Err = Stream.SkipBlock()) |
403 | return std::move(Err); |
404 | continue; |
405 | |
406 | case BitstreamEntry::Record: |
407 | if (llvm::Expected<unsigned> Skipped = Stream.skipRecord(Entry.ID)) |
408 | continue; |
409 | else |
410 | return Skipped.takeError(); |
411 | } |
412 | } |
413 | } |
414 | |
415 | namespace { |
416 | |
417 | class BitcodeReaderBase { |
418 | protected: |
419 | BitcodeReaderBase(BitstreamCursor Stream, StringRef Strtab) |
420 | : Stream(std::move(Stream)), Strtab(Strtab) { |
421 | this->Stream.setBlockInfo(&BlockInfo); |
422 | } |
423 | |
424 | BitstreamBlockInfo BlockInfo; |
425 | BitstreamCursor Stream; |
426 | StringRef Strtab; |
427 | |
428 | /// In version 2 of the bitcode we store names of global values and comdats in |
429 | /// a string table rather than in the VST. |
430 | bool UseStrtab = false; |
431 | |
432 | Expected<unsigned> parseVersionRecord(ArrayRef<uint64_t> Record); |
433 | |
434 | /// If this module uses a string table, pop the reference to the string table |
435 | /// and return the referenced string and the rest of the record. Otherwise |
436 | /// just return the record itself. |
437 | std::pair<StringRef, ArrayRef<uint64_t>> |
438 | readNameFromStrtab(ArrayRef<uint64_t> Record); |
439 | |
440 | Error readBlockInfo(); |
441 | |
442 | // Contains an arbitrary and optional string identifying the bitcode producer |
443 | std::string ProducerIdentification; |
444 | |
445 | Error error(const Twine &Message); |
446 | }; |
447 | |
448 | } // end anonymous namespace |
449 | |
450 | Error BitcodeReaderBase::error(const Twine &Message) { |
451 | std::string FullMsg = Message.str(); |
452 | if (!ProducerIdentification.empty()) |
453 | FullMsg += " (Producer: '" + ProducerIdentification + "' Reader: 'LLVM " + |
454 | LLVM_VERSION_STRING"17.0.0" "')"; |
455 | return ::error(FullMsg); |
456 | } |
457 | |
458 | Expected<unsigned> |
459 | BitcodeReaderBase::parseVersionRecord(ArrayRef<uint64_t> Record) { |
460 | if (Record.empty()) |
461 | return error("Invalid version record"); |
462 | unsigned ModuleVersion = Record[0]; |
463 | if (ModuleVersion > 2) |
464 | return error("Invalid value"); |
465 | UseStrtab = ModuleVersion >= 2; |
466 | return ModuleVersion; |
467 | } |
468 | |
469 | std::pair<StringRef, ArrayRef<uint64_t>> |
470 | BitcodeReaderBase::readNameFromStrtab(ArrayRef<uint64_t> Record) { |
471 | if (!UseStrtab) |
472 | return {"", Record}; |
473 | // Invalid reference. Let the caller complain about the record being empty. |
474 | if (Record[0] + Record[1] > Strtab.size()) |
475 | return {"", {}}; |
476 | return {StringRef(Strtab.data() + Record[0], Record[1]), Record.slice(2)}; |
477 | } |
478 | |
479 | namespace { |
480 | |
481 | /// This represents a constant expression or constant aggregate using a custom |
482 | /// structure internal to the bitcode reader. Later, this structure will be |
483 | /// expanded by materializeValue() either into a constant expression/aggregate, |
484 | /// or into an instruction sequence at the point of use. This allows us to |
485 | /// upgrade bitcode using constant expressions even if this kind of constant |
486 | /// expression is no longer supported. |
487 | class BitcodeConstant final : public Value, |
488 | TrailingObjects<BitcodeConstant, unsigned> { |
489 | friend TrailingObjects; |
490 | |
491 | // Value subclass ID: Pick largest possible value to avoid any clashes. |
492 | static constexpr uint8_t SubclassID = 255; |
493 | |
494 | public: |
495 | // Opcodes used for non-expressions. This includes constant aggregates |
496 | // (struct, array, vector) that might need expansion, as well as non-leaf |
497 | // constants that don't need expansion (no_cfi, dso_local, blockaddress), |
498 | // but still go through BitcodeConstant to avoid different uselist orders |
499 | // between the two cases. |
500 | static constexpr uint8_t ConstantStructOpcode = 255; |
501 | static constexpr uint8_t ConstantArrayOpcode = 254; |
502 | static constexpr uint8_t ConstantVectorOpcode = 253; |
503 | static constexpr uint8_t NoCFIOpcode = 252; |
504 | static constexpr uint8_t DSOLocalEquivalentOpcode = 251; |
505 | static constexpr uint8_t BlockAddressOpcode = 250; |
506 | static constexpr uint8_t FirstSpecialOpcode = BlockAddressOpcode; |
507 | |
508 | // Separate struct to make passing different number of parameters to |
509 | // BitcodeConstant::create() more convenient. |
510 | struct ExtraInfo { |
511 | uint8_t Opcode; |
512 | uint8_t Flags; |
513 | unsigned Extra; |
514 | Type *SrcElemTy; |
515 | |
516 | ExtraInfo(uint8_t Opcode, uint8_t Flags = 0, unsigned Extra = 0, |
517 | Type *SrcElemTy = nullptr) |
518 | : Opcode(Opcode), Flags(Flags), Extra(Extra), SrcElemTy(SrcElemTy) {} |
519 | }; |
520 | |
521 | uint8_t Opcode; |
522 | uint8_t Flags; |
523 | unsigned NumOperands; |
524 | unsigned Extra; // GEP inrange index or blockaddress BB id. |
525 | Type *SrcElemTy; // GEP source element type. |
526 | |
527 | private: |
528 | BitcodeConstant(Type *Ty, const ExtraInfo &Info, ArrayRef<unsigned> OpIDs) |
529 | : Value(Ty, SubclassID), Opcode(Info.Opcode), Flags(Info.Flags), |
530 | NumOperands(OpIDs.size()), Extra(Info.Extra), |
531 | SrcElemTy(Info.SrcElemTy) { |
532 | std::uninitialized_copy(OpIDs.begin(), OpIDs.end(), |
533 | getTrailingObjects<unsigned>()); |
534 | } |
535 | |
536 | BitcodeConstant &operator=(const BitcodeConstant &) = delete; |
537 | |
538 | public: |
539 | static BitcodeConstant *create(BumpPtrAllocator &A, Type *Ty, |
540 | const ExtraInfo &Info, |
541 | ArrayRef<unsigned> OpIDs) { |
542 | void *Mem = A.Allocate(totalSizeToAlloc<unsigned>(OpIDs.size()), |
543 | alignof(BitcodeConstant)); |
544 | return new (Mem) BitcodeConstant(Ty, Info, OpIDs); |
545 | } |
546 | |
547 | static bool classof(const Value *V) { return V->getValueID() == SubclassID; } |
548 | |
549 | ArrayRef<unsigned> getOperandIDs() const { |
550 | return ArrayRef(getTrailingObjects<unsigned>(), NumOperands); |
551 | } |
552 | |
553 | std::optional<unsigned> getInRangeIndex() const { |
554 | assert(Opcode == Instruction::GetElementPtr)(static_cast <bool> (Opcode == Instruction::GetElementPtr ) ? void (0) : __assert_fail ("Opcode == Instruction::GetElementPtr" , "llvm/lib/Bitcode/Reader/BitcodeReader.cpp", 554, __extension__ __PRETTY_FUNCTION__)); |
555 | if (Extra == (unsigned)-1) |
556 | return std::nullopt; |
557 | return Extra; |
558 | } |
559 | |
560 | const char *getOpcodeName() const { |
561 | return Instruction::getOpcodeName(Opcode); |
562 | } |
563 | }; |
564 | |
565 | class BitcodeReader : public BitcodeReaderBase, public GVMaterializer { |
566 | LLVMContext &Context; |
567 | Module *TheModule = nullptr; |
568 | // Next offset to start scanning for lazy parsing of function bodies. |
569 | uint64_t NextUnreadBit = 0; |
570 | // Last function offset found in the VST. |
571 | uint64_t LastFunctionBlockBit = 0; |
572 | bool SeenValueSymbolTable = false; |
573 | uint64_t VSTOffset = 0; |
574 | |
575 | std::vector<std::string> SectionTable; |
576 | std::vector<std::string> GCTable; |
577 | |
578 | std::vector<Type *> TypeList; |
579 | /// Track type IDs of contained types. Order is the same as the contained |
580 | /// types of a Type*. This is used during upgrades of typed pointer IR in |
581 | /// opaque pointer mode. |
582 | DenseMap<unsigned, SmallVector<unsigned, 1>> ContainedTypeIDs; |
583 | /// In some cases, we need to create a type ID for a type that was not |
584 | /// explicitly encoded in the bitcode, or we don't know about at the current |
585 | /// point. For example, a global may explicitly encode the value type ID, but |
586 | /// not have a type ID for the pointer to value type, for which we create a |
587 | /// virtual type ID instead. This map stores the new type ID that was created |
588 | /// for the given pair of Type and contained type ID. |
589 | DenseMap<std::pair<Type *, unsigned>, unsigned> VirtualTypeIDs; |
590 | DenseMap<Function *, unsigned> FunctionTypeIDs; |
591 | /// Allocator for BitcodeConstants. This should come before ValueList, |
592 | /// because the ValueList might hold ValueHandles to these constants, so |
593 | /// ValueList must be destroyed before Alloc. |
594 | BumpPtrAllocator Alloc; |
595 | BitcodeReaderValueList ValueList; |
596 | std::optional<MetadataLoader> MDLoader; |
597 | std::vector<Comdat *> ComdatList; |
598 | DenseSet<GlobalObject *> ImplicitComdatObjects; |
599 | SmallVector<Instruction *, 64> InstructionList; |
600 | |
601 | std::vector<std::pair<GlobalVariable *, unsigned>> GlobalInits; |
602 | std::vector<std::pair<GlobalValue *, unsigned>> IndirectSymbolInits; |
603 | |
604 | struct FunctionOperandInfo { |
605 | Function *F; |
606 | unsigned PersonalityFn; |
607 | unsigned Prefix; |
608 | unsigned Prologue; |
609 | }; |
610 | std::vector<FunctionOperandInfo> FunctionOperands; |
611 | |
612 | /// The set of attributes by index. Index zero in the file is for null, and |
613 | /// is thus not represented here. As such all indices are off by one. |
614 | std::vector<AttributeList> MAttributes; |
615 | |
616 | /// The set of attribute groups. |
617 | std::map<unsigned, AttributeList> MAttributeGroups; |
618 | |
619 | /// While parsing a function body, this is a list of the basic blocks for the |
620 | /// function. |
621 | std::vector<BasicBlock*> FunctionBBs; |
622 | |
623 | // When reading the module header, this list is populated with functions that |
624 | // have bodies later in the file. |
625 | std::vector<Function*> FunctionsWithBodies; |
626 | |
627 | // When intrinsic functions are encountered which require upgrading they are |
628 | // stored here with their replacement function. |
629 | using UpdatedIntrinsicMap = DenseMap<Function *, Function *>; |
630 | UpdatedIntrinsicMap UpgradedIntrinsics; |
631 | |
632 | // Several operations happen after the module header has been read, but |
633 | // before function bodies are processed. This keeps track of whether |
634 | // we've done this yet. |
635 | bool SeenFirstFunctionBody = false; |
636 | |
637 | /// When function bodies are initially scanned, this map contains info about |
638 | /// where to find deferred function body in the stream. |
639 | DenseMap<Function*, uint64_t> DeferredFunctionInfo; |
640 | |
641 | /// When Metadata block is initially scanned when parsing the module, we may |
642 | /// choose to defer parsing of the metadata. This vector contains info about |
643 | /// which Metadata blocks are deferred. |
644 | std::vector<uint64_t> DeferredMetadataInfo; |
645 | |
646 | /// These are basic blocks forward-referenced by block addresses. They are |
647 | /// inserted lazily into functions when they're loaded. The basic block ID is |
648 | /// its index into the vector. |
649 | DenseMap<Function *, std::vector<BasicBlock *>> BasicBlockFwdRefs; |
650 | std::deque<Function *> BasicBlockFwdRefQueue; |
651 | |
652 | /// These are Functions that contain BlockAddresses which refer a different |
653 | /// Function. When parsing the different Function, queue Functions that refer |
654 | /// to the different Function. Those Functions must be materialized in order |
655 | /// to resolve their BlockAddress constants before the different Function |
656 | /// gets moved into another Module. |
657 | std::vector<Function *> BackwardRefFunctions; |
658 | |
659 | /// Indicates that we are using a new encoding for instruction operands where |
660 | /// most operands in the current FUNCTION_BLOCK are encoded relative to the |
661 | /// instruction number, for a more compact encoding. Some instruction |
662 | /// operands are not relative to the instruction ID: basic block numbers, and |
663 | /// types. Once the old style function blocks have been phased out, we would |
664 | /// not need this flag. |
665 | bool UseRelativeIDs = false; |
666 | |
667 | /// True if all functions will be materialized, negating the need to process |
668 | /// (e.g.) blockaddress forward references. |
669 | bool WillMaterializeAllForwardRefs = false; |
670 | |
671 | bool StripDebugInfo = false; |
672 | TBAAVerifier TBAAVerifyHelper; |
673 | |
674 | std::vector<std::string> BundleTags; |
675 | SmallVector<SyncScope::ID, 8> SSIDs; |
676 | |
677 | std::optional<ValueTypeCallbackTy> ValueTypeCallback; |
678 | |
679 | public: |
680 | BitcodeReader(BitstreamCursor Stream, StringRef Strtab, |
681 | StringRef ProducerIdentification, LLVMContext &Context); |
682 | |
683 | Error materializeForwardReferencedFunctions(); |
684 | |
685 | Error materialize(GlobalValue *GV) override; |
686 | Error materializeModule() override; |
687 | std::vector<StructType *> getIdentifiedStructTypes() const override; |
688 | |
689 | /// Main interface to parsing a bitcode buffer. |
690 | /// \returns true if an error occurred. |
691 | Error parseBitcodeInto(Module *M, bool ShouldLazyLoadMetadata, |
692 | bool IsImporting, ParserCallbacks Callbacks = {}); |
693 | |
694 | static uint64_t decodeSignRotatedValue(uint64_t V); |
695 | |
696 | /// Materialize any deferred Metadata block. |
697 | Error materializeMetadata() override; |
698 | |
699 | void setStripDebugInfo() override; |
700 | |
701 | private: |
702 | std::vector<StructType *> IdentifiedStructTypes; |
703 | StructType *createIdentifiedStructType(LLVMContext &Context, StringRef Name); |
704 | StructType *createIdentifiedStructType(LLVMContext &Context); |
705 | |
706 | static constexpr unsigned InvalidTypeID = ~0u; |
707 | |
708 | Type *getTypeByID(unsigned ID); |
709 | Type *getPtrElementTypeByID(unsigned ID); |
710 | unsigned getContainedTypeID(unsigned ID, unsigned Idx = 0); |
711 | unsigned getVirtualTypeID(Type *Ty, ArrayRef<unsigned> ContainedTypeIDs = {}); |
712 | |
713 | void callValueTypeCallback(Value *F, unsigned TypeID); |
714 | Expected<Value *> materializeValue(unsigned ValID, BasicBlock *InsertBB); |
715 | Expected<Constant *> getValueForInitializer(unsigned ID); |
716 | |
717 | Value *getFnValueByID(unsigned ID, Type *Ty, unsigned TyID, |
718 | BasicBlock *ConstExprInsertBB) { |
719 | if (Ty && Ty->isMetadataTy()) |
720 | return MetadataAsValue::get(Ty->getContext(), getFnMetadataByID(ID)); |
721 | return ValueList.getValueFwdRef(ID, Ty, TyID, ConstExprInsertBB); |
722 | } |
723 | |
724 | Metadata *getFnMetadataByID(unsigned ID) { |
725 | return MDLoader->getMetadataFwdRefOrLoad(ID); |
726 | } |
727 | |
728 | BasicBlock *getBasicBlock(unsigned ID) const { |
729 | if (ID >= FunctionBBs.size()) return nullptr; // Invalid ID |
730 | return FunctionBBs[ID]; |
731 | } |
732 | |
733 | AttributeList getAttributes(unsigned i) const { |
734 | if (i-1 < MAttributes.size()) |
735 | return MAttributes[i-1]; |
736 | return AttributeList(); |
737 | } |
738 | |
739 | /// Read a value/type pair out of the specified record from slot 'Slot'. |
740 | /// Increment Slot past the number of slots used in the record. Return true on |
741 | /// failure. |
742 | bool getValueTypePair(const SmallVectorImpl<uint64_t> &Record, unsigned &Slot, |
743 | unsigned InstNum, Value *&ResVal, unsigned &TypeID, |
744 | BasicBlock *ConstExprInsertBB) { |
745 | if (Slot == Record.size()) return true; |
746 | unsigned ValNo = (unsigned)Record[Slot++]; |
747 | // Adjust the ValNo, if it was encoded relative to the InstNum. |
748 | if (UseRelativeIDs) |
749 | ValNo = InstNum - ValNo; |
750 | if (ValNo < InstNum) { |
751 | // If this is not a forward reference, just return the value we already |
752 | // have. |
753 | TypeID = ValueList.getTypeID(ValNo); |
754 | ResVal = getFnValueByID(ValNo, nullptr, TypeID, ConstExprInsertBB); |
755 | assert((!ResVal || ResVal->getType() == getTypeByID(TypeID)) &&(static_cast <bool> ((!ResVal || ResVal->getType() == getTypeByID(TypeID)) && "Incorrect type ID stored for value" ) ? void (0) : __assert_fail ("(!ResVal || ResVal->getType() == getTypeByID(TypeID)) && \"Incorrect type ID stored for value\"" , "llvm/lib/Bitcode/Reader/BitcodeReader.cpp", 756, __extension__ __PRETTY_FUNCTION__)) |
756 | "Incorrect type ID stored for value")(static_cast <bool> ((!ResVal || ResVal->getType() == getTypeByID(TypeID)) && "Incorrect type ID stored for value" ) ? void (0) : __assert_fail ("(!ResVal || ResVal->getType() == getTypeByID(TypeID)) && \"Incorrect type ID stored for value\"" , "llvm/lib/Bitcode/Reader/BitcodeReader.cpp", 756, __extension__ __PRETTY_FUNCTION__)); |
757 | return ResVal == nullptr; |
758 | } |
759 | if (Slot == Record.size()) |
760 | return true; |
761 | |
762 | TypeID = (unsigned)Record[Slot++]; |
763 | ResVal = getFnValueByID(ValNo, getTypeByID(TypeID), TypeID, |
764 | ConstExprInsertBB); |
765 | return ResVal == nullptr; |
766 | } |
767 | |
768 | /// Read a value out of the specified record from slot 'Slot'. Increment Slot |
769 | /// past the number of slots used by the value in the record. Return true if |
770 | /// there is an error. |
771 | bool popValue(const SmallVectorImpl<uint64_t> &Record, unsigned &Slot, |
772 | unsigned InstNum, Type *Ty, unsigned TyID, Value *&ResVal, |
773 | BasicBlock *ConstExprInsertBB) { |
774 | if (getValue(Record, Slot, InstNum, Ty, TyID, ResVal, ConstExprInsertBB)) |
775 | return true; |
776 | // All values currently take a single record slot. |
777 | ++Slot; |
778 | return false; |
779 | } |
780 | |
781 | /// Like popValue, but does not increment the Slot number. |
782 | bool getValue(const SmallVectorImpl<uint64_t> &Record, unsigned Slot, |
783 | unsigned InstNum, Type *Ty, unsigned TyID, Value *&ResVal, |
784 | BasicBlock *ConstExprInsertBB) { |
785 | ResVal = getValue(Record, Slot, InstNum, Ty, TyID, ConstExprInsertBB); |
786 | return ResVal == nullptr; |
787 | } |
788 | |
789 | /// Version of getValue that returns ResVal directly, or 0 if there is an |
790 | /// error. |
791 | Value *getValue(const SmallVectorImpl<uint64_t> &Record, unsigned Slot, |
792 | unsigned InstNum, Type *Ty, unsigned TyID, |
793 | BasicBlock *ConstExprInsertBB) { |
794 | if (Slot == Record.size()) return nullptr; |
795 | unsigned ValNo = (unsigned)Record[Slot]; |
796 | // Adjust the ValNo, if it was encoded relative to the InstNum. |
797 | if (UseRelativeIDs) |
798 | ValNo = InstNum - ValNo; |
799 | return getFnValueByID(ValNo, Ty, TyID, ConstExprInsertBB); |
800 | } |
801 | |
802 | /// Like getValue, but decodes signed VBRs. |
803 | Value *getValueSigned(const SmallVectorImpl<uint64_t> &Record, unsigned Slot, |
804 | unsigned InstNum, Type *Ty, unsigned TyID, |
805 | BasicBlock *ConstExprInsertBB) { |
806 | if (Slot == Record.size()) return nullptr; |
807 | unsigned ValNo = (unsigned)decodeSignRotatedValue(Record[Slot]); |
808 | // Adjust the ValNo, if it was encoded relative to the InstNum. |
809 | if (UseRelativeIDs) |
810 | ValNo = InstNum - ValNo; |
811 | return getFnValueByID(ValNo, Ty, TyID, ConstExprInsertBB); |
812 | } |
813 | |
814 | /// Upgrades old-style typeless byval/sret/inalloca attributes by adding the |
815 | /// corresponding argument's pointee type. Also upgrades intrinsics that now |
816 | /// require an elementtype attribute. |
817 | Error propagateAttributeTypes(CallBase *CB, ArrayRef<unsigned> ArgsTys); |
818 | |
819 | /// Converts alignment exponent (i.e. power of two (or zero)) to the |
820 | /// corresponding alignment to use. If alignment is too large, returns |
821 | /// a corresponding error code. |
822 | Error parseAlignmentValue(uint64_t Exponent, MaybeAlign &Alignment); |
823 | Error parseAttrKind(uint64_t Code, Attribute::AttrKind *Kind); |
824 | Error parseModule(uint64_t ResumeBit, bool ShouldLazyLoadMetadata = false, |
825 | ParserCallbacks Callbacks = {}); |
826 | |
827 | Error parseComdatRecord(ArrayRef<uint64_t> Record); |
828 | Error parseGlobalVarRecord(ArrayRef<uint64_t> Record); |
829 | Error parseFunctionRecord(ArrayRef<uint64_t> Record); |
830 | Error parseGlobalIndirectSymbolRecord(unsigned BitCode, |
831 | ArrayRef<uint64_t> Record); |
832 | |
833 | Error parseAttributeBlock(); |
834 | Error parseAttributeGroupBlock(); |
835 | Error parseTypeTable(); |
836 | Error parseTypeTableBody(); |
837 | Error parseOperandBundleTags(); |
838 | Error parseSyncScopeNames(); |
839 | |
840 | Expected<Value *> recordValue(SmallVectorImpl<uint64_t> &Record, |
841 | unsigned NameIndex, Triple &TT); |
842 | void setDeferredFunctionInfo(unsigned FuncBitcodeOffsetDelta, Function *F, |
843 | ArrayRef<uint64_t> Record); |
844 | Error parseValueSymbolTable(uint64_t Offset = 0); |
845 | Error parseGlobalValueSymbolTable(); |
846 | Error parseConstants(); |
847 | Error rememberAndSkipFunctionBodies(); |
848 | Error rememberAndSkipFunctionBody(); |
849 | /// Save the positions of the Metadata blocks and skip parsing the blocks. |
850 | Error rememberAndSkipMetadata(); |
851 | Error typeCheckLoadStoreInst(Type *ValType, Type *PtrType); |
852 | Error parseFunctionBody(Function *F); |
853 | Error globalCleanup(); |
854 | Error resolveGlobalAndIndirectSymbolInits(); |
855 | Error parseUseLists(); |
856 | Error findFunctionInStream( |
857 | Function *F, |
858 | DenseMap<Function *, uint64_t>::iterator DeferredFunctionInfoIterator); |
859 | |
860 | SyncScope::ID getDecodedSyncScopeID(unsigned Val); |
861 | }; |
862 | |
863 | /// Class to manage reading and parsing function summary index bitcode |
864 | /// files/sections. |
865 | class ModuleSummaryIndexBitcodeReader : public BitcodeReaderBase { |
866 | /// The module index built during parsing. |
867 | ModuleSummaryIndex &TheIndex; |
868 | |
869 | /// Indicates whether we have encountered a global value summary section |
870 | /// yet during parsing. |
871 | bool SeenGlobalValSummary = false; |
872 | |
873 | /// Indicates whether we have already parsed the VST, used for error checking. |
874 | bool SeenValueSymbolTable = false; |
875 | |
876 | /// Set to the offset of the VST recorded in the MODULE_CODE_VSTOFFSET record. |
877 | /// Used to enable on-demand parsing of the VST. |
878 | uint64_t VSTOffset = 0; |
879 | |
880 | // Map to save ValueId to ValueInfo association that was recorded in the |
881 | // ValueSymbolTable. It is used after the VST is parsed to convert |
882 | // call graph edges read from the function summary from referencing |
883 | // callees by their ValueId to using the ValueInfo instead, which is how |
884 | // they are recorded in the summary index being built. |
885 | // We save a GUID which refers to the same global as the ValueInfo, but |
886 | // ignoring the linkage, i.e. for values other than local linkage they are |
887 | // identical (this is the second tuple member). |
888 | // The third tuple member is the real GUID of the ValueInfo. |
889 | DenseMap<unsigned, |
890 | std::tuple<ValueInfo, GlobalValue::GUID, GlobalValue::GUID>> |
891 | ValueIdToValueInfoMap; |
892 | |
893 | /// Map populated during module path string table parsing, from the |
894 | /// module ID to a string reference owned by the index's module |
895 | /// path string table, used to correlate with combined index |
896 | /// summary records. |
897 | DenseMap<uint64_t, StringRef> ModuleIdMap; |
898 | |
899 | /// Original source file name recorded in a bitcode record. |
900 | std::string SourceFileName; |
901 | |
902 | /// The string identifier given to this module by the client, normally the |
903 | /// path to the bitcode file. |
904 | StringRef ModulePath; |
905 | |
906 | /// For per-module summary indexes, the unique numerical identifier given to |
907 | /// this module by the client. |
908 | unsigned ModuleId; |
909 | |
910 | /// Callback to ask whether a symbol is the prevailing copy when invoked |
911 | /// during combined index building. |
912 | std::function<bool(GlobalValue::GUID)> IsPrevailing; |
913 | |
914 | /// Saves the stack ids from the STACK_IDS record to consult when adding stack |
915 | /// ids from the lists in the callsite and alloc entries to the index. |
916 | std::vector<uint64_t> StackIds; |
917 | |
918 | public: |
919 | ModuleSummaryIndexBitcodeReader( |
920 | BitstreamCursor Stream, StringRef Strtab, ModuleSummaryIndex &TheIndex, |
921 | StringRef ModulePath, unsigned ModuleId, |
922 | std::function<bool(GlobalValue::GUID)> IsPrevailing = nullptr); |
923 | |
924 | Error parseModule(); |
925 | |
926 | private: |
927 | void setValueGUID(uint64_t ValueID, StringRef ValueName, |
928 | GlobalValue::LinkageTypes Linkage, |
929 | StringRef SourceFileName); |
930 | Error parseValueSymbolTable( |
931 | uint64_t Offset, |
932 | DenseMap<unsigned, GlobalValue::LinkageTypes> &ValueIdToLinkageMap); |
933 | std::vector<ValueInfo> makeRefList(ArrayRef<uint64_t> Record); |
934 | std::vector<FunctionSummary::EdgeTy> makeCallList(ArrayRef<uint64_t> Record, |
935 | bool IsOldProfileFormat, |
936 | bool HasProfile, |
937 | bool HasRelBF); |
938 | Error parseEntireSummary(unsigned ID); |
939 | Error parseModuleStringTable(); |
940 | void parseTypeIdCompatibleVtableSummaryRecord(ArrayRef<uint64_t> Record); |
941 | void parseTypeIdCompatibleVtableInfo(ArrayRef<uint64_t> Record, size_t &Slot, |
942 | TypeIdCompatibleVtableInfo &TypeId); |
943 | std::vector<FunctionSummary::ParamAccess> |
944 | parseParamAccesses(ArrayRef<uint64_t> Record); |
945 | |
946 | template <bool AllowNullValueInfo = false> |
947 | std::tuple<ValueInfo, GlobalValue::GUID, GlobalValue::GUID> |
948 | getValueInfoFromValueId(unsigned ValueId); |
949 | |
950 | void addThisModule(); |
951 | ModuleSummaryIndex::ModuleInfo *getThisModule(); |
952 | }; |
953 | |
954 | } // end anonymous namespace |
955 | |
956 | std::error_code llvm::errorToErrorCodeAndEmitErrors(LLVMContext &Ctx, |
957 | Error Err) { |
958 | if (Err) { |
959 | std::error_code EC; |
960 | handleAllErrors(std::move(Err), [&](ErrorInfoBase &EIB) { |
961 | EC = EIB.convertToErrorCode(); |
962 | Ctx.emitError(EIB.message()); |
963 | }); |
964 | return EC; |
965 | } |
966 | return std::error_code(); |
967 | } |
968 | |
969 | BitcodeReader::BitcodeReader(BitstreamCursor Stream, StringRef Strtab, |
970 | StringRef ProducerIdentification, |
971 | LLVMContext &Context) |
972 | : BitcodeReaderBase(std::move(Stream), Strtab), Context(Context), |
973 | ValueList(this->Stream.SizeInBytes(), |
974 | [this](unsigned ValID, BasicBlock *InsertBB) { |
975 | return materializeValue(ValID, InsertBB); |
976 | }) { |
977 | this->ProducerIdentification = std::string(ProducerIdentification); |
978 | } |
979 | |
980 | Error BitcodeReader::materializeForwardReferencedFunctions() { |
981 | if (WillMaterializeAllForwardRefs) |
982 | return Error::success(); |
983 | |
984 | // Prevent recursion. |
985 | WillMaterializeAllForwardRefs = true; |
986 | |
987 | while (!BasicBlockFwdRefQueue.empty()) { |
988 | Function *F = BasicBlockFwdRefQueue.front(); |
989 | BasicBlockFwdRefQueue.pop_front(); |
990 | assert(F && "Expected valid function")(static_cast <bool> (F && "Expected valid function" ) ? void (0) : __assert_fail ("F && \"Expected valid function\"" , "llvm/lib/Bitcode/Reader/BitcodeReader.cpp", 990, __extension__ __PRETTY_FUNCTION__)); |
991 | if (!BasicBlockFwdRefs.count(F)) |
992 | // Already materialized. |
993 | continue; |
994 | |
995 | // Check for a function that isn't materializable to prevent an infinite |
996 | // loop. When parsing a blockaddress stored in a global variable, there |
997 | // isn't a trivial way to check if a function will have a body without a |
998 | // linear search through FunctionsWithBodies, so just check it here. |
999 | if (!F->isMaterializable()) |
1000 | return error("Never resolved function from blockaddress"); |
1001 | |
1002 | // Try to materialize F. |
1003 | if (Error Err = materialize(F)) |
1004 | return Err; |
1005 | } |
1006 | assert(BasicBlockFwdRefs.empty() && "Function missing from queue")(static_cast <bool> (BasicBlockFwdRefs.empty() && "Function missing from queue") ? void (0) : __assert_fail ("BasicBlockFwdRefs.empty() && \"Function missing from queue\"" , "llvm/lib/Bitcode/Reader/BitcodeReader.cpp", 1006, __extension__ __PRETTY_FUNCTION__)); |
1007 | |
1008 | for (Function *F : BackwardRefFunctions) |
1009 | if (Error Err = materialize(F)) |
1010 | return Err; |
1011 | BackwardRefFunctions.clear(); |
1012 | |
1013 | // Reset state. |
1014 | WillMaterializeAllForwardRefs = false; |
1015 | return Error::success(); |
1016 | } |
1017 | |
1018 | //===----------------------------------------------------------------------===// |
1019 | // Helper functions to implement forward reference resolution, etc. |
1020 | //===----------------------------------------------------------------------===// |
1021 | |
1022 | static bool hasImplicitComdat(size_t Val) { |
1023 | switch (Val) { |
1024 | default: |
1025 | return false; |
1026 | case 1: // Old WeakAnyLinkage |
1027 | case 4: // Old LinkOnceAnyLinkage |
1028 | case 10: // Old WeakODRLinkage |
1029 | case 11: // Old LinkOnceODRLinkage |
1030 | return true; |
1031 | } |
1032 | } |
1033 | |
1034 | static GlobalValue::LinkageTypes getDecodedLinkage(unsigned Val) { |
1035 | switch (Val) { |
1036 | default: // Map unknown/new linkages to external |
1037 | case 0: |
1038 | return GlobalValue::ExternalLinkage; |
1039 | case 2: |
1040 | return GlobalValue::AppendingLinkage; |
1041 | case 3: |
1042 | return GlobalValue::InternalLinkage; |
1043 | case 5: |
1044 | return GlobalValue::ExternalLinkage; // Obsolete DLLImportLinkage |
1045 | case 6: |
1046 | return GlobalValue::ExternalLinkage; // Obsolete DLLExportLinkage |
1047 | case 7: |
1048 | return GlobalValue::ExternalWeakLinkage; |
1049 | case 8: |
1050 | return GlobalValue::CommonLinkage; |
1051 | case 9: |
1052 | return GlobalValue::PrivateLinkage; |
1053 | case 12: |
1054 | return GlobalValue::AvailableExternallyLinkage; |
1055 | case 13: |
1056 | return GlobalValue::PrivateLinkage; // Obsolete LinkerPrivateLinkage |
1057 | case 14: |
1058 | return GlobalValue::PrivateLinkage; // Obsolete LinkerPrivateWeakLinkage |
1059 | case 15: |
1060 | return GlobalValue::ExternalLinkage; // Obsolete LinkOnceODRAutoHideLinkage |
1061 | case 1: // Old value with implicit comdat. |
1062 | case 16: |
1063 | return GlobalValue::WeakAnyLinkage; |
1064 | case 10: // Old value with implicit comdat. |
1065 | case 17: |
1066 | return GlobalValue::WeakODRLinkage; |
1067 | case 4: // Old value with implicit comdat. |
1068 | case 18: |
1069 | return GlobalValue::LinkOnceAnyLinkage; |
1070 | case 11: // Old value with implicit comdat. |
1071 | case 19: |
1072 | return GlobalValue::LinkOnceODRLinkage; |
1073 | } |
1074 | } |
1075 | |
1076 | static FunctionSummary::FFlags getDecodedFFlags(uint64_t RawFlags) { |
1077 | FunctionSummary::FFlags Flags; |
1078 | Flags.ReadNone = RawFlags & 0x1; |
1079 | Flags.ReadOnly = (RawFlags >> 1) & 0x1; |
1080 | Flags.NoRecurse = (RawFlags >> 2) & 0x1; |
1081 | Flags.ReturnDoesNotAlias = (RawFlags >> 3) & 0x1; |
1082 | Flags.NoInline = (RawFlags >> 4) & 0x1; |
1083 | Flags.AlwaysInline = (RawFlags >> 5) & 0x1; |
1084 | Flags.NoUnwind = (RawFlags >> 6) & 0x1; |
1085 | Flags.MayThrow = (RawFlags >> 7) & 0x1; |
1086 | Flags.HasUnknownCall = (RawFlags >> 8) & 0x1; |
1087 | Flags.MustBeUnreachable = (RawFlags >> 9) & 0x1; |
1088 | return Flags; |
1089 | } |
1090 | |
1091 | // Decode the flags for GlobalValue in the summary. The bits for each attribute: |
1092 | // |
1093 | // linkage: [0,4), notEligibleToImport: 4, live: 5, local: 6, canAutoHide: 7, |
1094 | // visibility: [8, 10). |
1095 | static GlobalValueSummary::GVFlags getDecodedGVSummaryFlags(uint64_t RawFlags, |
1096 | uint64_t Version) { |
1097 | // Summary were not emitted before LLVM 3.9, we don't need to upgrade Linkage |
1098 | // like getDecodedLinkage() above. Any future change to the linkage enum and |
1099 | // to getDecodedLinkage() will need to be taken into account here as above. |
1100 | auto Linkage = GlobalValue::LinkageTypes(RawFlags & 0xF); // 4 bits |
1101 | auto Visibility = GlobalValue::VisibilityTypes((RawFlags >> 8) & 3); // 2 bits |
1102 | RawFlags = RawFlags >> 4; |
1103 | bool NotEligibleToImport = (RawFlags & 0x1) || Version < 3; |
1104 | // The Live flag wasn't introduced until version 3. For dead stripping |
1105 | // to work correctly on earlier versions, we must conservatively treat all |
1106 | // values as live. |
1107 | bool Live = (RawFlags & 0x2) || Version < 3; |
1108 | bool Local = (RawFlags & 0x4); |
1109 | bool AutoHide = (RawFlags & 0x8); |
1110 | |
1111 | return GlobalValueSummary::GVFlags(Linkage, Visibility, NotEligibleToImport, |
1112 | Live, Local, AutoHide); |
1113 | } |
1114 | |
1115 | // Decode the flags for GlobalVariable in the summary |
1116 | static GlobalVarSummary::GVarFlags getDecodedGVarFlags(uint64_t RawFlags) { |
1117 | return GlobalVarSummary::GVarFlags( |
1118 | (RawFlags & 0x1) ? true : false, (RawFlags & 0x2) ? true : false, |
1119 | (RawFlags & 0x4) ? true : false, |
1120 | (GlobalObject::VCallVisibility)(RawFlags >> 3)); |
1121 | } |
1122 | |
1123 | static GlobalValue::VisibilityTypes getDecodedVisibility(unsigned Val) { |
1124 | switch (Val) { |
1125 | default: // Map unknown visibilities to default. |
1126 | case 0: return GlobalValue::DefaultVisibility; |
1127 | case 1: return GlobalValue::HiddenVisibility; |
1128 | case 2: return GlobalValue::ProtectedVisibility; |
1129 | } |
1130 | } |
1131 | |
1132 | static GlobalValue::DLLStorageClassTypes |
1133 | getDecodedDLLStorageClass(unsigned Val) { |
1134 | switch (Val) { |
1135 | default: // Map unknown values to default. |
1136 | case 0: return GlobalValue::DefaultStorageClass; |
1137 | case 1: return GlobalValue::DLLImportStorageClass; |
1138 | case 2: return GlobalValue::DLLExportStorageClass; |
1139 | } |
1140 | } |
1141 | |
1142 | static bool getDecodedDSOLocal(unsigned Val) { |
1143 | switch(Val) { |
1144 | default: // Map unknown values to preemptable. |
1145 | case 0: return false; |
1146 | case 1: return true; |
1147 | } |
1148 | } |
1149 | |
1150 | static GlobalVariable::ThreadLocalMode getDecodedThreadLocalMode(unsigned Val) { |
1151 | switch (Val) { |
1152 | case 0: return GlobalVariable::NotThreadLocal; |
1153 | default: // Map unknown non-zero value to general dynamic. |
1154 | case 1: return GlobalVariable::GeneralDynamicTLSModel; |
1155 | case 2: return GlobalVariable::LocalDynamicTLSModel; |
1156 | case 3: return GlobalVariable::InitialExecTLSModel; |
1157 | case 4: return GlobalVariable::LocalExecTLSModel; |
1158 | } |
1159 | } |
1160 | |
1161 | static GlobalVariable::UnnamedAddr getDecodedUnnamedAddrType(unsigned Val) { |
1162 | switch (Val) { |
1163 | default: // Map unknown to UnnamedAddr::None. |
1164 | case 0: return GlobalVariable::UnnamedAddr::None; |
1165 | case 1: return GlobalVariable::UnnamedAddr::Global; |
1166 | case 2: return GlobalVariable::UnnamedAddr::Local; |
1167 | } |
1168 | } |
1169 | |
1170 | static int getDecodedCastOpcode(unsigned Val) { |
1171 | switch (Val) { |
1172 | default: return -1; |
1173 | case bitc::CAST_TRUNC : return Instruction::Trunc; |
1174 | case bitc::CAST_ZEXT : return Instruction::ZExt; |
1175 | case bitc::CAST_SEXT : return Instruction::SExt; |
1176 | case bitc::CAST_FPTOUI : return Instruction::FPToUI; |
1177 | case bitc::CAST_FPTOSI : return Instruction::FPToSI; |
1178 | case bitc::CAST_UITOFP : return Instruction::UIToFP; |
1179 | case bitc::CAST_SITOFP : return Instruction::SIToFP; |
1180 | case bitc::CAST_FPTRUNC : return Instruction::FPTrunc; |
1181 | case bitc::CAST_FPEXT : return Instruction::FPExt; |
1182 | case bitc::CAST_PTRTOINT: return Instruction::PtrToInt; |
1183 | case bitc::CAST_INTTOPTR: return Instruction::IntToPtr; |
1184 | case bitc::CAST_BITCAST : return Instruction::BitCast; |
1185 | case bitc::CAST_ADDRSPACECAST: return Instruction::AddrSpaceCast; |
1186 | } |
1187 | } |
1188 | |
1189 | static int getDecodedUnaryOpcode(unsigned Val, Type *Ty) { |
1190 | bool IsFP = Ty->isFPOrFPVectorTy(); |
1191 | // UnOps are only valid for int/fp or vector of int/fp types |
1192 | if (!IsFP && !Ty->isIntOrIntVectorTy()) |
1193 | return -1; |
1194 | |
1195 | switch (Val) { |
1196 | default: |
1197 | return -1; |
1198 | case bitc::UNOP_FNEG: |
1199 | return IsFP ? Instruction::FNeg : -1; |
1200 | } |
1201 | } |
1202 | |
1203 | static int getDecodedBinaryOpcode(unsigned Val, Type *Ty) { |
1204 | bool IsFP = Ty->isFPOrFPVectorTy(); |
1205 | // BinOps are only valid for int/fp or vector of int/fp types |
1206 | if (!IsFP && !Ty->isIntOrIntVectorTy()) |
1207 | return -1; |
1208 | |
1209 | switch (Val) { |
1210 | default: |
1211 | return -1; |
1212 | case bitc::BINOP_ADD: |
1213 | return IsFP ? Instruction::FAdd : Instruction::Add; |
1214 | case bitc::BINOP_SUB: |
1215 | return IsFP ? Instruction::FSub : Instruction::Sub; |
1216 | case bitc::BINOP_MUL: |
1217 | return IsFP ? Instruction::FMul : Instruction::Mul; |
1218 | case bitc::BINOP_UDIV: |
1219 | return IsFP ? -1 : Instruction::UDiv; |
1220 | case bitc::BINOP_SDIV: |
1221 | return IsFP ? Instruction::FDiv : Instruction::SDiv; |
1222 | case bitc::BINOP_UREM: |
1223 | return IsFP ? -1 : Instruction::URem; |
1224 | case bitc::BINOP_SREM: |
1225 | return IsFP ? Instruction::FRem : Instruction::SRem; |
1226 | case bitc::BINOP_SHL: |
1227 | return IsFP ? -1 : Instruction::Shl; |
1228 | case bitc::BINOP_LSHR: |
1229 | return IsFP ? -1 : Instruction::LShr; |
1230 | case bitc::BINOP_ASHR: |
1231 | return IsFP ? -1 : Instruction::AShr; |
1232 | case bitc::BINOP_AND: |
1233 | return IsFP ? -1 : Instruction::And; |
1234 | case bitc::BINOP_OR: |
1235 | return IsFP ? -1 : Instruction::Or; |
1236 | case bitc::BINOP_XOR: |
1237 | return IsFP ? -1 : Instruction::Xor; |
1238 | } |
1239 | } |
1240 | |
1241 | static AtomicRMWInst::BinOp getDecodedRMWOperation(unsigned Val) { |
1242 | switch (Val) { |
1243 | default: return AtomicRMWInst::BAD_BINOP; |
1244 | case bitc::RMW_XCHG: return AtomicRMWInst::Xchg; |
1245 | case bitc::RMW_ADD: return AtomicRMWInst::Add; |
1246 | case bitc::RMW_SUB: return AtomicRMWInst::Sub; |
1247 | case bitc::RMW_AND: return AtomicRMWInst::And; |
1248 | case bitc::RMW_NAND: return AtomicRMWInst::Nand; |
1249 | case bitc::RMW_OR: return AtomicRMWInst::Or; |
1250 | case bitc::RMW_XOR: return AtomicRMWInst::Xor; |
1251 | case bitc::RMW_MAX: return AtomicRMWInst::Max; |
1252 | case bitc::RMW_MIN: return AtomicRMWInst::Min; |
1253 | case bitc::RMW_UMAX: return AtomicRMWInst::UMax; |
1254 | case bitc::RMW_UMIN: return AtomicRMWInst::UMin; |
1255 | case bitc::RMW_FADD: return AtomicRMWInst::FAdd; |
1256 | case bitc::RMW_FSUB: return AtomicRMWInst::FSub; |
1257 | case bitc::RMW_FMAX: return AtomicRMWInst::FMax; |
1258 | case bitc::RMW_FMIN: return AtomicRMWInst::FMin; |
1259 | case bitc::RMW_UINC_WRAP: |
1260 | return AtomicRMWInst::UIncWrap; |
1261 | case bitc::RMW_UDEC_WRAP: |
1262 | return AtomicRMWInst::UDecWrap; |
1263 | } |
1264 | } |
1265 | |
1266 | static AtomicOrdering getDecodedOrdering(unsigned Val) { |
1267 | switch (Val) { |
1268 | case bitc::ORDERING_NOTATOMIC: return AtomicOrdering::NotAtomic; |
1269 | case bitc::ORDERING_UNORDERED: return AtomicOrdering::Unordered; |
1270 | case bitc::ORDERING_MONOTONIC: return AtomicOrdering::Monotonic; |
1271 | case bitc::ORDERING_ACQUIRE: return AtomicOrdering::Acquire; |
1272 | case bitc::ORDERING_RELEASE: return AtomicOrdering::Release; |
1273 | case bitc::ORDERING_ACQREL: return AtomicOrdering::AcquireRelease; |
1274 | default: // Map unknown orderings to sequentially-consistent. |
1275 | case bitc::ORDERING_SEQCST: return AtomicOrdering::SequentiallyConsistent; |
1276 | } |
1277 | } |
1278 | |
1279 | static Comdat::SelectionKind getDecodedComdatSelectionKind(unsigned Val) { |
1280 | switch (Val) { |
1281 | default: // Map unknown selection kinds to any. |
1282 | case bitc::COMDAT_SELECTION_KIND_ANY: |
1283 | return Comdat::Any; |
1284 | case bitc::COMDAT_SELECTION_KIND_EXACT_MATCH: |
1285 | return Comdat::ExactMatch; |
1286 | case bitc::COMDAT_SELECTION_KIND_LARGEST: |
1287 | return Comdat::Largest; |
1288 | case bitc::COMDAT_SELECTION_KIND_NO_DUPLICATES: |
1289 | return Comdat::NoDeduplicate; |
1290 | case bitc::COMDAT_SELECTION_KIND_SAME_SIZE: |
1291 | return Comdat::SameSize; |
1292 | } |
1293 | } |
1294 | |
1295 | static FastMathFlags getDecodedFastMathFlags(unsigned Val) { |
1296 | FastMathFlags FMF; |
1297 | if (0 != (Val & bitc::UnsafeAlgebra)) |
1298 | FMF.setFast(); |
1299 | if (0 != (Val & bitc::AllowReassoc)) |
1300 | FMF.setAllowReassoc(); |
1301 | if (0 != (Val & bitc::NoNaNs)) |
1302 | FMF.setNoNaNs(); |
1303 | if (0 != (Val & bitc::NoInfs)) |
1304 | FMF.setNoInfs(); |
1305 | if (0 != (Val & bitc::NoSignedZeros)) |
1306 | FMF.setNoSignedZeros(); |
1307 | if (0 != (Val & bitc::AllowReciprocal)) |
1308 | FMF.setAllowReciprocal(); |
1309 | if (0 != (Val & bitc::AllowContract)) |
1310 | FMF.setAllowContract(true); |
1311 | if (0 != (Val & bitc::ApproxFunc)) |
1312 | FMF.setApproxFunc(); |
1313 | return FMF; |
1314 | } |
1315 | |
1316 | static void upgradeDLLImportExportLinkage(GlobalValue *GV, unsigned Val) { |
1317 | // A GlobalValue with local linkage cannot have a DLL storage class. |
1318 | if (GV->hasLocalLinkage()) |
1319 | return; |
1320 | switch (Val) { |
1321 | case 5: GV->setDLLStorageClass(GlobalValue::DLLImportStorageClass); break; |
1322 | case 6: GV->setDLLStorageClass(GlobalValue::DLLExportStorageClass); break; |
1323 | } |
1324 | } |
1325 | |
1326 | Type *BitcodeReader::getTypeByID(unsigned ID) { |
1327 | // The type table size is always specified correctly. |
1328 | if (ID >= TypeList.size()) |
1329 | return nullptr; |
1330 | |
1331 | if (Type *Ty = TypeList[ID]) |
1332 | return Ty; |
1333 | |
1334 | // If we have a forward reference, the only possible case is when it is to a |
1335 | // named struct. Just create a placeholder for now. |
1336 | return TypeList[ID] = createIdentifiedStructType(Context); |
1337 | } |
1338 | |
1339 | unsigned BitcodeReader::getContainedTypeID(unsigned ID, unsigned Idx) { |
1340 | auto It = ContainedTypeIDs.find(ID); |
1341 | if (It == ContainedTypeIDs.end()) |
1342 | return InvalidTypeID; |
1343 | |
1344 | if (Idx >= It->second.size()) |
1345 | return InvalidTypeID; |
1346 | |
1347 | return It->second[Idx]; |
1348 | } |
1349 | |
1350 | Type *BitcodeReader::getPtrElementTypeByID(unsigned ID) { |
1351 | if (ID >= TypeList.size()) |
1352 | return nullptr; |
1353 | |
1354 | Type *Ty = TypeList[ID]; |
1355 | if (!Ty->isPointerTy()) |
1356 | return nullptr; |
1357 | |
1358 | Type *ElemTy = getTypeByID(getContainedTypeID(ID, 0)); |
1359 | if (!ElemTy) |
1360 | return nullptr; |
1361 | |
1362 | assert(cast<PointerType>(Ty)->isOpaqueOrPointeeTypeMatches(ElemTy) &&(static_cast <bool> (cast<PointerType>(Ty)->isOpaqueOrPointeeTypeMatches (ElemTy) && "Incorrect element type") ? void (0) : __assert_fail ("cast<PointerType>(Ty)->isOpaqueOrPointeeTypeMatches(ElemTy) && \"Incorrect element type\"" , "llvm/lib/Bitcode/Reader/BitcodeReader.cpp", 1363, __extension__ __PRETTY_FUNCTION__)) |
1363 | "Incorrect element type")(static_cast <bool> (cast<PointerType>(Ty)->isOpaqueOrPointeeTypeMatches (ElemTy) && "Incorrect element type") ? void (0) : __assert_fail ("cast<PointerType>(Ty)->isOpaqueOrPointeeTypeMatches(ElemTy) && \"Incorrect element type\"" , "llvm/lib/Bitcode/Reader/BitcodeReader.cpp", 1363, __extension__ __PRETTY_FUNCTION__)); |
1364 | return ElemTy; |
1365 | } |
1366 | |
1367 | unsigned BitcodeReader::getVirtualTypeID(Type *Ty, |
1368 | ArrayRef<unsigned> ChildTypeIDs) { |
1369 | unsigned ChildTypeID = ChildTypeIDs.empty() ? InvalidTypeID : ChildTypeIDs[0]; |
1370 | auto CacheKey = std::make_pair(Ty, ChildTypeID); |
1371 | auto It = VirtualTypeIDs.find(CacheKey); |
1372 | if (It != VirtualTypeIDs.end()) { |
1373 | // The cmpxchg return value is the only place we need more than one |
1374 | // contained type ID, however the second one will always be the same (i1), |
1375 | // so we don't need to include it in the cache key. This asserts that the |
1376 | // contained types are indeed as expected and there are no collisions. |
1377 | assert((ChildTypeIDs.empty() ||(static_cast <bool> ((ChildTypeIDs.empty() || ContainedTypeIDs [It->second] == ChildTypeIDs) && "Incorrect cached contained type IDs" ) ? void (0) : __assert_fail ("(ChildTypeIDs.empty() || ContainedTypeIDs[It->second] == ChildTypeIDs) && \"Incorrect cached contained type IDs\"" , "llvm/lib/Bitcode/Reader/BitcodeReader.cpp", 1379, __extension__ __PRETTY_FUNCTION__)) |
1378 | ContainedTypeIDs[It->second] == ChildTypeIDs) &&(static_cast <bool> ((ChildTypeIDs.empty() || ContainedTypeIDs [It->second] == ChildTypeIDs) && "Incorrect cached contained type IDs" ) ? void (0) : __assert_fail ("(ChildTypeIDs.empty() || ContainedTypeIDs[It->second] == ChildTypeIDs) && \"Incorrect cached contained type IDs\"" , "llvm/lib/Bitcode/Reader/BitcodeReader.cpp", 1379, __extension__ __PRETTY_FUNCTION__)) |
1379 | "Incorrect cached contained type IDs")(static_cast <bool> ((ChildTypeIDs.empty() || ContainedTypeIDs [It->second] == ChildTypeIDs) && "Incorrect cached contained type IDs" ) ? void (0) : __assert_fail ("(ChildTypeIDs.empty() || ContainedTypeIDs[It->second] == ChildTypeIDs) && \"Incorrect cached contained type IDs\"" , "llvm/lib/Bitcode/Reader/BitcodeReader.cpp", 1379, __extension__ __PRETTY_FUNCTION__)); |
1380 | return It->second; |
1381 | } |
1382 | |
1383 | #ifndef NDEBUG |
1384 | if (!Ty->isOpaquePointerTy()) { |
1385 | assert(Ty->getNumContainedTypes() == ChildTypeIDs.size() &&(static_cast <bool> (Ty->getNumContainedTypes() == ChildTypeIDs .size() && "Wrong number of contained types") ? void ( 0) : __assert_fail ("Ty->getNumContainedTypes() == ChildTypeIDs.size() && \"Wrong number of contained types\"" , "llvm/lib/Bitcode/Reader/BitcodeReader.cpp", 1386, __extension__ __PRETTY_FUNCTION__)) |
1386 | "Wrong number of contained types")(static_cast <bool> (Ty->getNumContainedTypes() == ChildTypeIDs .size() && "Wrong number of contained types") ? void ( 0) : __assert_fail ("Ty->getNumContainedTypes() == ChildTypeIDs.size() && \"Wrong number of contained types\"" , "llvm/lib/Bitcode/Reader/BitcodeReader.cpp", 1386, __extension__ __PRETTY_FUNCTION__)); |
1387 | for (auto Pair : zip(Ty->subtypes(), ChildTypeIDs)) { |
1388 | assert(std::get<0>(Pair) == getTypeByID(std::get<1>(Pair)) &&(static_cast <bool> (std::get<0>(Pair) == getTypeByID (std::get<1>(Pair)) && "Incorrect contained type ID" ) ? void (0) : __assert_fail ("std::get<0>(Pair) == getTypeByID(std::get<1>(Pair)) && \"Incorrect contained type ID\"" , "llvm/lib/Bitcode/Reader/BitcodeReader.cpp", 1389, __extension__ __PRETTY_FUNCTION__)) |
1389 | "Incorrect contained type ID")(static_cast <bool> (std::get<0>(Pair) == getTypeByID (std::get<1>(Pair)) && "Incorrect contained type ID" ) ? void (0) : __assert_fail ("std::get<0>(Pair) == getTypeByID(std::get<1>(Pair)) && \"Incorrect contained type ID\"" , "llvm/lib/Bitcode/Reader/BitcodeReader.cpp", 1389, __extension__ __PRETTY_FUNCTION__)); |
1390 | } |
1391 | } |
1392 | #endif |
1393 | |
1394 | unsigned TypeID = TypeList.size(); |
1395 | TypeList.push_back(Ty); |
1396 | if (!ChildTypeIDs.empty()) |
1397 | append_range(ContainedTypeIDs[TypeID], ChildTypeIDs); |
1398 | VirtualTypeIDs.insert({CacheKey, TypeID}); |
1399 | return TypeID; |
1400 | } |
1401 | |
1402 | static bool isConstExprSupported(const BitcodeConstant *BC) { |
1403 | uint8_t Opcode = BC->Opcode; |
1404 | |
1405 | // These are not real constant expressions, always consider them supported. |
1406 | if (Opcode >= BitcodeConstant::FirstSpecialOpcode) |
1407 | return true; |
1408 | |
1409 | // If -expand-constant-exprs is set, we want to consider all expressions |
1410 | // as unsupported. |
1411 | if (ExpandConstantExprs) |
1412 | return false; |
1413 | |
1414 | if (Instruction::isBinaryOp(Opcode)) |
1415 | return ConstantExpr::isSupportedBinOp(Opcode); |
1416 | |
1417 | if (Opcode == Instruction::GetElementPtr) |
1418 | return ConstantExpr::isSupportedGetElementPtr(BC->SrcElemTy); |
1419 | |
1420 | switch (Opcode) { |
1421 | case Instruction::FNeg: |
1422 | case Instruction::Select: |
1423 | return false; |
1424 | default: |
1425 | return true; |
1426 | } |
1427 | } |
1428 | |
1429 | Expected<Value *> BitcodeReader::materializeValue(unsigned StartValID, |
1430 | BasicBlock *InsertBB) { |
1431 | // Quickly handle the case where there is no BitcodeConstant to resolve. |
1432 | if (StartValID < ValueList.size() && ValueList[StartValID] && |
1433 | !isa<BitcodeConstant>(ValueList[StartValID])) |
1434 | return ValueList[StartValID]; |
1435 | |
1436 | SmallDenseMap<unsigned, Value *> MaterializedValues; |
1437 | SmallVector<unsigned> Worklist; |
1438 | Worklist.push_back(StartValID); |
1439 | while (!Worklist.empty()) { |
1440 | unsigned ValID = Worklist.back(); |
1441 | if (MaterializedValues.count(ValID)) { |
1442 | // Duplicate expression that was already handled. |
1443 | Worklist.pop_back(); |
1444 | continue; |
1445 | } |
1446 | |
1447 | if (ValID >= ValueList.size() || !ValueList[ValID]) |
1448 | return error("Invalid value ID"); |
1449 | |
1450 | Value *V = ValueList[ValID]; |
1451 | auto *BC = dyn_cast<BitcodeConstant>(V); |
1452 | if (!BC) { |
1453 | MaterializedValues.insert({ValID, V}); |
1454 | Worklist.pop_back(); |
1455 | continue; |
1456 | } |
1457 | |
1458 | // Iterate in reverse, so values will get popped from the worklist in |
1459 | // expected order. |
1460 | SmallVector<Value *> Ops; |
1461 | for (unsigned OpID : reverse(BC->getOperandIDs())) { |
1462 | auto It = MaterializedValues.find(OpID); |
1463 | if (It != MaterializedValues.end()) |
1464 | Ops.push_back(It->second); |
1465 | else |
1466 | Worklist.push_back(OpID); |
1467 | } |
1468 | |
1469 | // Some expressions have not been resolved yet, handle them first and then |
1470 | // revisit this one. |
1471 | if (Ops.size() != BC->getOperandIDs().size()) |
1472 | continue; |
1473 | std::reverse(Ops.begin(), Ops.end()); |
1474 | |
1475 | SmallVector<Constant *> ConstOps; |
1476 | for (Value *Op : Ops) |
1477 | if (auto *C = dyn_cast<Constant>(Op)) |
1478 | ConstOps.push_back(C); |
1479 | |
1480 | // Materialize as constant expression if possible. |
1481 | if (isConstExprSupported(BC) && ConstOps.size() == Ops.size()) { |
1482 | Constant *C; |
1483 | if (Instruction::isCast(BC->Opcode)) { |
1484 | C = UpgradeBitCastExpr(BC->Opcode, ConstOps[0], BC->getType()); |
1485 | if (!C) |
1486 | C = ConstantExpr::getCast(BC->Opcode, ConstOps[0], BC->getType()); |
1487 | } else if (Instruction::isBinaryOp(BC->Opcode)) { |
1488 | C = ConstantExpr::get(BC->Opcode, ConstOps[0], ConstOps[1], BC->Flags); |
1489 | } else { |
1490 | switch (BC->Opcode) { |
1491 | case BitcodeConstant::NoCFIOpcode: { |
1492 | auto *GV = dyn_cast<GlobalValue>(ConstOps[0]); |
1493 | if (!GV) |
1494 | return error("no_cfi operand must be GlobalValue"); |
1495 | C = NoCFIValue::get(GV); |
1496 | break; |
1497 | } |
1498 | case BitcodeConstant::DSOLocalEquivalentOpcode: { |
1499 | auto *GV = dyn_cast<GlobalValue>(ConstOps[0]); |
1500 | if (!GV) |
1501 | return error("dso_local operand must be GlobalValue"); |
1502 | C = DSOLocalEquivalent::get(GV); |
1503 | break; |
1504 | } |
1505 | case BitcodeConstant::BlockAddressOpcode: { |
1506 | Function *Fn = dyn_cast<Function>(ConstOps[0]); |
1507 | if (!Fn) |
1508 | return error("blockaddress operand must be a function"); |
1509 | |
1510 | // If the function is already parsed we can insert the block address |
1511 | // right away. |
1512 | BasicBlock *BB; |
1513 | unsigned BBID = BC->Extra; |
1514 | if (!BBID) |
1515 | // Invalid reference to entry block. |
1516 | return error("Invalid ID"); |
1517 | if (!Fn->empty()) { |
1518 | Function::iterator BBI = Fn->begin(), BBE = Fn->end(); |
1519 | for (size_t I = 0, E = BBID; I != E; ++I) { |
1520 | if (BBI == BBE) |
1521 | return error("Invalid ID"); |
1522 | ++BBI; |
1523 | } |
1524 | BB = &*BBI; |
1525 | } else { |
1526 | // Otherwise insert a placeholder and remember it so it can be |
1527 | // inserted when the function is parsed. |
1528 | auto &FwdBBs = BasicBlockFwdRefs[Fn]; |
1529 | if (FwdBBs.empty()) |
1530 | BasicBlockFwdRefQueue.push_back(Fn); |
1531 | if (FwdBBs.size() < BBID + 1) |
1532 | FwdBBs.resize(BBID + 1); |
1533 | if (!FwdBBs[BBID]) |
1534 | FwdBBs[BBID] = BasicBlock::Create(Context); |
1535 | BB = FwdBBs[BBID]; |
1536 | } |
1537 | C = BlockAddress::get(Fn, BB); |
1538 | break; |
1539 | } |
1540 | case BitcodeConstant::ConstantStructOpcode: |
1541 | C = ConstantStruct::get(cast<StructType>(BC->getType()), ConstOps); |
1542 | break; |
1543 | case BitcodeConstant::ConstantArrayOpcode: |
1544 | C = ConstantArray::get(cast<ArrayType>(BC->getType()), ConstOps); |
1545 | break; |
1546 | case BitcodeConstant::ConstantVectorOpcode: |
1547 | C = ConstantVector::get(ConstOps); |
1548 | break; |
1549 | case Instruction::ICmp: |
1550 | case Instruction::FCmp: |
1551 | C = ConstantExpr::getCompare(BC->Flags, ConstOps[0], ConstOps[1]); |
1552 | break; |
1553 | case Instruction::GetElementPtr: |
1554 | C = ConstantExpr::getGetElementPtr(BC->SrcElemTy, ConstOps[0], |
1555 | ArrayRef(ConstOps).drop_front(), |
1556 | BC->Flags, BC->getInRangeIndex()); |
1557 | break; |
1558 | case Instruction::ExtractElement: |
1559 | C = ConstantExpr::getExtractElement(ConstOps[0], ConstOps[1]); |
1560 | break; |
1561 | case Instruction::InsertElement: |
1562 | C = ConstantExpr::getInsertElement(ConstOps[0], ConstOps[1], |
1563 | ConstOps[2]); |
1564 | break; |
1565 | case Instruction::ShuffleVector: { |
1566 | SmallVector<int, 16> Mask; |
1567 | ShuffleVectorInst::getShuffleMask(ConstOps[2], Mask); |
1568 | C = ConstantExpr::getShuffleVector(ConstOps[0], ConstOps[1], Mask); |
1569 | break; |
1570 | } |
1571 | default: |
1572 | llvm_unreachable("Unhandled bitcode constant")::llvm::llvm_unreachable_internal("Unhandled bitcode constant" , "llvm/lib/Bitcode/Reader/BitcodeReader.cpp", 1572); |
1573 | } |
1574 | } |
1575 | |
1576 | // Cache resolved constant. |
1577 | ValueList.replaceValueWithoutRAUW(ValID, C); |
1578 | MaterializedValues.insert({ValID, C}); |
1579 | Worklist.pop_back(); |
1580 | continue; |
1581 | } |
1582 | |
1583 | if (!InsertBB) |
1584 | return error(Twine("Value referenced by initializer is an unsupported " |
1585 | "constant expression of type ") + |
1586 | BC->getOpcodeName()); |
1587 | |
1588 | // Materialize as instructions if necessary. |
1589 | Instruction *I; |
1590 | if (Instruction::isCast(BC->Opcode)) { |
1591 | I = CastInst::Create((Instruction::CastOps)BC->Opcode, Ops[0], |
1592 | BC->getType(), "constexpr", InsertBB); |
1593 | } else if (Instruction::isUnaryOp(BC->Opcode)) { |
1594 | I = UnaryOperator::Create((Instruction::UnaryOps)BC->Opcode, Ops[0], |
1595 | "constexpr", InsertBB); |
1596 | } else if (Instruction::isBinaryOp(BC->Opcode)) { |
1597 | I = BinaryOperator::Create((Instruction::BinaryOps)BC->Opcode, Ops[0], |
1598 | Ops[1], "constexpr", InsertBB); |
1599 | if (isa<OverflowingBinaryOperator>(I)) { |
1600 | if (BC->Flags & OverflowingBinaryOperator::NoSignedWrap) |
1601 | I->setHasNoSignedWrap(); |
1602 | if (BC->Flags & OverflowingBinaryOperator::NoUnsignedWrap) |
1603 | I->setHasNoUnsignedWrap(); |
1604 | } |
1605 | if (isa<PossiblyExactOperator>(I) && |
1606 | (BC->Flags & PossiblyExactOperator::IsExact)) |
1607 | I->setIsExact(); |
1608 | } else { |
1609 | switch (BC->Opcode) { |
1610 | case BitcodeConstant::ConstantVectorOpcode: { |
1611 | Type *IdxTy = Type::getInt32Ty(BC->getContext()); |
1612 | Value *V = PoisonValue::get(BC->getType()); |
1613 | for (auto Pair : enumerate(Ops)) { |
1614 | Value *Idx = ConstantInt::get(IdxTy, Pair.index()); |
1615 | V = InsertElementInst::Create(V, Pair.value(), Idx, "constexpr.ins", |
1616 | InsertBB); |
1617 | } |
1618 | I = cast<Instruction>(V); |
1619 | break; |
1620 | } |
1621 | case BitcodeConstant::ConstantStructOpcode: |
1622 | case BitcodeConstant::ConstantArrayOpcode: { |
1623 | Value *V = PoisonValue::get(BC->getType()); |
1624 | for (auto Pair : enumerate(Ops)) |
1625 | V = InsertValueInst::Create(V, Pair.value(), Pair.index(), |
1626 | "constexpr.ins", InsertBB); |
1627 | I = cast<Instruction>(V); |
1628 | break; |
1629 | } |
1630 | case Instruction::ICmp: |
1631 | case Instruction::FCmp: |
1632 | I = CmpInst::Create((Instruction::OtherOps)BC->Opcode, |
1633 | (CmpInst::Predicate)BC->Flags, Ops[0], Ops[1], |
1634 | "constexpr", InsertBB); |
1635 | break; |
1636 | case Instruction::GetElementPtr: |
1637 | I = GetElementPtrInst::Create(BC->SrcElemTy, Ops[0], |
1638 | ArrayRef(Ops).drop_front(), "constexpr", |
1639 | InsertBB); |
1640 | if (BC->Flags) |
1641 | cast<GetElementPtrInst>(I)->setIsInBounds(); |
1642 | break; |
1643 | case Instruction::Select: |
1644 | I = SelectInst::Create(Ops[0], Ops[1], Ops[2], "constexpr", InsertBB); |
1645 | break; |
1646 | case Instruction::ExtractElement: |
1647 | I = ExtractElementInst::Create(Ops[0], Ops[1], "constexpr", InsertBB); |
1648 | break; |
1649 | case Instruction::InsertElement: |
1650 | I = InsertElementInst::Create(Ops[0], Ops[1], Ops[2], "constexpr", |
1651 | InsertBB); |
1652 | break; |
1653 | case Instruction::ShuffleVector: |
1654 | I = new ShuffleVectorInst(Ops[0], Ops[1], Ops[2], "constexpr", |
1655 | InsertBB); |
1656 | break; |
1657 | default: |
1658 | llvm_unreachable("Unhandled bitcode constant")::llvm::llvm_unreachable_internal("Unhandled bitcode constant" , "llvm/lib/Bitcode/Reader/BitcodeReader.cpp", 1658); |
1659 | } |
1660 | } |
1661 | |
1662 | MaterializedValues.insert({ValID, I}); |
1663 | Worklist.pop_back(); |
1664 | } |
1665 | |
1666 | return MaterializedValues[StartValID]; |
1667 | } |
1668 | |
1669 | Expected<Constant *> BitcodeReader::getValueForInitializer(unsigned ID) { |
1670 | Expected<Value *> MaybeV = materializeValue(ID, /* InsertBB */ nullptr); |
1671 | if (!MaybeV) |
1672 | return MaybeV.takeError(); |
1673 | |
1674 | // Result must be Constant if InsertBB is nullptr. |
1675 | return cast<Constant>(MaybeV.get()); |
1676 | } |
1677 | |
1678 | StructType *BitcodeReader::createIdentifiedStructType(LLVMContext &Context, |
1679 | StringRef Name) { |
1680 | auto *Ret = StructType::create(Context, Name); |
1681 | IdentifiedStructTypes.push_back(Ret); |
1682 | return Ret; |
1683 | } |
1684 | |
1685 | StructType *BitcodeReader::createIdentifiedStructType(LLVMContext &Context) { |
1686 | auto *Ret = StructType::create(Context); |
1687 | IdentifiedStructTypes.push_back(Ret); |
1688 | return Ret; |
1689 | } |
1690 | |
1691 | //===----------------------------------------------------------------------===// |
1692 | // Functions for parsing blocks from the bitcode file |
1693 | //===----------------------------------------------------------------------===// |
1694 | |
1695 | static uint64_t getRawAttributeMask(Attribute::AttrKind Val) { |
1696 | switch (Val) { |
1697 | case Attribute::EndAttrKinds: |
1698 | case Attribute::EmptyKey: |
1699 | case Attribute::TombstoneKey: |
1700 | llvm_unreachable("Synthetic enumerators which should never get here")::llvm::llvm_unreachable_internal("Synthetic enumerators which should never get here" , "llvm/lib/Bitcode/Reader/BitcodeReader.cpp", 1700); |
1701 | |
1702 | case Attribute::None: return 0; |
1703 | case Attribute::ZExt: return 1 << 0; |
1704 | case Attribute::SExt: return 1 << 1; |
1705 | case Attribute::NoReturn: return 1 << 2; |
1706 | case Attribute::InReg: return 1 << 3; |
1707 | case Attribute::StructRet: return 1 << 4; |
1708 | case Attribute::NoUnwind: return 1 << 5; |
1709 | case Attribute::NoAlias: return 1 << 6; |
1710 | case Attribute::ByVal: return 1 << 7; |
1711 | case Attribute::Nest: return 1 << 8; |
1712 | case Attribute::ReadNone: return 1 << 9; |
1713 | case Attribute::ReadOnly: return 1 << 10; |
1714 | case Attribute::NoInline: return 1 << 11; |
1715 | case Attribute::AlwaysInline: return 1 << 12; |
1716 | case Attribute::OptimizeForSize: return 1 << 13; |
1717 | case Attribute::StackProtect: return 1 << 14; |
1718 | case Attribute::StackProtectReq: return 1 << 15; |
1719 | case Attribute::Alignment: return 31 << 16; |
1720 | case Attribute::NoCapture: return 1 << 21; |
1721 | case Attribute::NoRedZone: return 1 << 22; |
1722 | case Attribute::NoImplicitFloat: return 1 << 23; |
1723 | case Attribute::Naked: return 1 << 24; |
1724 | case Attribute::InlineHint: return 1 << 25; |
1725 | case Attribute::StackAlignment: return 7 << 26; |
1726 | case Attribute::ReturnsTwice: return 1 << 29; |
1727 | case Attribute::UWTable: return 1 << 30; |
1728 | case Attribute::NonLazyBind: return 1U << 31; |
1729 | case Attribute::SanitizeAddress: return 1ULL << 32; |
1730 | case Attribute::MinSize: return 1ULL << 33; |
1731 | case Attribute::NoDuplicate: return 1ULL << 34; |
1732 | case Attribute::StackProtectStrong: return 1ULL << 35; |
1733 | case Attribute::SanitizeThread: return 1ULL << 36; |
1734 | case Attribute::SanitizeMemory: return 1ULL << 37; |
1735 | case Attribute::NoBuiltin: return 1ULL << 38; |
1736 | case Attribute::Returned: return 1ULL << 39; |
1737 | case Attribute::Cold: return 1ULL << 40; |
1738 | case Attribute::Builtin: return 1ULL << 41; |
1739 | case Attribute::OptimizeNone: return 1ULL << 42; |
1740 | case Attribute::InAlloca: return 1ULL << 43; |
1741 | case Attribute::NonNull: return 1ULL << 44; |
1742 | case Attribute::JumpTable: return 1ULL << 45; |
1743 | case Attribute::Convergent: return 1ULL << 46; |
1744 | case Attribute::SafeStack: return 1ULL << 47; |
1745 | case Attribute::NoRecurse: return 1ULL << 48; |
1746 | // 1ULL << 49 is InaccessibleMemOnly, which is upgraded separately. |
1747 | // 1ULL << 50 is InaccessibleMemOrArgMemOnly, which is upgraded separately. |
1748 | case Attribute::SwiftSelf: return 1ULL << 51; |
1749 | case Attribute::SwiftError: return 1ULL << 52; |
1750 | case Attribute::WriteOnly: return 1ULL << 53; |
1751 | case Attribute::Speculatable: return 1ULL << 54; |
1752 | case Attribute::StrictFP: return 1ULL << 55; |
1753 | case Attribute::SanitizeHWAddress: return 1ULL << 56; |
1754 | case Attribute::NoCfCheck: return 1ULL << 57; |
1755 | case Attribute::OptForFuzzing: return 1ULL << 58; |
1756 | case Attribute::ShadowCallStack: return 1ULL << 59; |
1757 | case Attribute::SpeculativeLoadHardening: |
1758 | return 1ULL << 60; |
1759 | case Attribute::ImmArg: |
1760 | return 1ULL << 61; |
1761 | case Attribute::WillReturn: |
1762 | return 1ULL << 62; |
1763 | case Attribute::NoFree: |
1764 | return 1ULL << 63; |
1765 | default: |
1766 | // Other attributes are not supported in the raw format, |
1767 | // as we ran out of space. |
1768 | return 0; |
1769 | } |
1770 | llvm_unreachable("Unsupported attribute type")::llvm::llvm_unreachable_internal("Unsupported attribute type" , "llvm/lib/Bitcode/Reader/BitcodeReader.cpp", 1770); |
1771 | } |
1772 | |
1773 | static void addRawAttributeValue(AttrBuilder &B, uint64_t Val) { |
1774 | if (!Val) return; |
1775 | |
1776 | for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds; |
1777 | I = Attribute::AttrKind(I + 1)) { |
1778 | if (uint64_t A = (Val & getRawAttributeMask(I))) { |
1779 | if (I == Attribute::Alignment) |
1780 | B.addAlignmentAttr(1ULL << ((A >> 16) - 1)); |
1781 | else if (I == Attribute::StackAlignment) |
1782 | B.addStackAlignmentAttr(1ULL << ((A >> 26)-1)); |
1783 | else if (Attribute::isTypeAttrKind(I)) |
1784 | B.addTypeAttr(I, nullptr); // Type will be auto-upgraded. |
1785 | else |
1786 | B.addAttribute(I); |
1787 | } |
1788 | } |
1789 | } |
1790 | |
1791 | /// This fills an AttrBuilder object with the LLVM attributes that have |
1792 | /// been decoded from the given integer. This function must stay in sync with |
1793 | /// 'encodeLLVMAttributesForBitcode'. |
1794 | static void decodeLLVMAttributesForBitcode(AttrBuilder &B, |
1795 | uint64_t EncodedAttrs, |
1796 | uint64_t AttrIdx) { |
1797 | // The alignment is stored as a 16-bit raw value from bits 31--16. We shift |
1798 | // the bits above 31 down by 11 bits. |
1799 | unsigned Alignment = (EncodedAttrs & (0xffffULL << 16)) >> 16; |
1800 | assert((!Alignment || isPowerOf2_32(Alignment)) &&(static_cast <bool> ((!Alignment || isPowerOf2_32(Alignment )) && "Alignment must be a power of two.") ? void (0) : __assert_fail ("(!Alignment || isPowerOf2_32(Alignment)) && \"Alignment must be a power of two.\"" , "llvm/lib/Bitcode/Reader/BitcodeReader.cpp", 1801, __extension__ __PRETTY_FUNCTION__)) |
1801 | "Alignment must be a power of two.")(static_cast <bool> ((!Alignment || isPowerOf2_32(Alignment )) && "Alignment must be a power of two.") ? void (0) : __assert_fail ("(!Alignment || isPowerOf2_32(Alignment)) && \"Alignment must be a power of two.\"" , "llvm/lib/Bitcode/Reader/BitcodeReader.cpp", 1801, __extension__ __PRETTY_FUNCTION__)); |
1802 | |
1803 | if (Alignment) |
1804 | B.addAlignmentAttr(Alignment); |
1805 | |
1806 | uint64_t Attrs = ((EncodedAttrs & (0xfffffULL << 32)) >> 11) | |
1807 | (EncodedAttrs & 0xffff); |
1808 | |
1809 | if (AttrIdx == AttributeList::FunctionIndex) { |
1810 | // Upgrade old memory attributes. |
1811 | MemoryEffects ME = MemoryEffects::unknown(); |
1812 | if (Attrs & (1ULL << 9)) { |
1813 | // ReadNone |
1814 | Attrs &= ~(1ULL << 9); |
1815 | ME &= MemoryEffects::none(); |
1816 | } |
1817 | if (Attrs & (1ULL << 10)) { |
1818 | // ReadOnly |
1819 | Attrs &= ~(1ULL << 10); |
1820 | ME &= MemoryEffects::readOnly(); |
1821 | } |
1822 | if (Attrs & (1ULL << 49)) { |
1823 | // InaccessibleMemOnly |
1824 | Attrs &= ~(1ULL << 49); |
1825 | ME &= MemoryEffects::inaccessibleMemOnly(); |
1826 | } |
1827 | if (Attrs & (1ULL << 50)) { |
1828 | // InaccessibleMemOrArgMemOnly |
1829 | Attrs &= ~(1ULL << 50); |
1830 | ME &= MemoryEffects::inaccessibleOrArgMemOnly(); |
1831 | } |
1832 | if (Attrs & (1ULL << 53)) { |
1833 | // WriteOnly |
1834 | Attrs &= ~(1ULL << 53); |
1835 | ME &= MemoryEffects::writeOnly(); |
1836 | } |
1837 | if (ME != MemoryEffects::unknown()) |
1838 | B.addMemoryAttr(ME); |
1839 | } |
1840 | |
1841 | addRawAttributeValue(B, Attrs); |
1842 | } |
1843 | |
1844 | Error BitcodeReader::parseAttributeBlock() { |
1845 | if (Error Err = Stream.EnterSubBlock(bitc::PARAMATTR_BLOCK_ID)) |
1846 | return Err; |
1847 | |
1848 | if (!MAttributes.empty()) |
1849 | return error("Invalid multiple blocks"); |
1850 | |
1851 | SmallVector<uint64_t, 64> Record; |
1852 | |
1853 | SmallVector<AttributeList, 8> Attrs; |
1854 | |
1855 | // Read all the records. |
1856 | while (true) { |
1857 | Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks(); |
1858 | if (!MaybeEntry) |
1859 | return MaybeEntry.takeError(); |
1860 | BitstreamEntry Entry = MaybeEntry.get(); |
1861 | |
1862 | switch (Entry.Kind) { |
1863 | case BitstreamEntry::SubBlock: // Handled for us already. |
1864 | case BitstreamEntry::Error: |
1865 | return error("Malformed block"); |
1866 | case BitstreamEntry::EndBlock: |
1867 | return Error::success(); |
1868 | case BitstreamEntry::Record: |
1869 | // The interesting case. |
1870 | break; |
1871 | } |
1872 | |
1873 | // Read a record. |
1874 | Record.clear(); |
1875 | Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record); |
1876 | if (!MaybeRecord) |
1877 | return MaybeRecord.takeError(); |
1878 | switch (MaybeRecord.get()) { |
1879 | default: // Default behavior: ignore. |
1880 | break; |
1881 | case bitc::PARAMATTR_CODE_ENTRY_OLD: // ENTRY: [paramidx0, attr0, ...] |
1882 | // Deprecated, but still needed to read old bitcode files. |
1883 | if (Record.size() & 1) |
1884 | return error("Invalid parameter attribute record"); |
1885 | |
1886 | for (unsigned i = 0, e = Record.size(); i != e; i += 2) { |
1887 | AttrBuilder B(Context); |
1888 | decodeLLVMAttributesForBitcode(B, Record[i+1], Record[i]); |
1889 | Attrs.push_back(AttributeList::get(Context, Record[i], B)); |
1890 | } |
1891 | |
1892 | MAttributes.push_back(AttributeList::get(Context, Attrs)); |
1893 | Attrs.clear(); |
1894 | break; |
1895 | case bitc::PARAMATTR_CODE_ENTRY: // ENTRY: [attrgrp0, attrgrp1, ...] |
1896 | for (unsigned i = 0, e = Record.size(); i != e; ++i) |
1897 | Attrs.push_back(MAttributeGroups[Record[i]]); |
1898 | |
1899 | MAttributes.push_back(AttributeList::get(Context, Attrs)); |
1900 | Attrs.clear(); |
1901 | break; |
1902 | } |
1903 | } |
1904 | } |
1905 | |
1906 | // Returns Attribute::None on unrecognized codes. |
1907 | static Attribute::AttrKind getAttrFromCode(uint64_t Code) { |
1908 | switch (Code) { |
1909 | default: |
1910 | return Attribute::None; |
1911 | case bitc::ATTR_KIND_ALIGNMENT: |
1912 | return Attribute::Alignment; |
1913 | case bitc::ATTR_KIND_ALWAYS_INLINE: |
1914 | return Attribute::AlwaysInline; |
1915 | case bitc::ATTR_KIND_BUILTIN: |
1916 | return Attribute::Builtin; |
1917 | case bitc::ATTR_KIND_BY_VAL: |
1918 | return Attribute::ByVal; |
1919 | case bitc::ATTR_KIND_IN_ALLOCA: |
1920 | return Attribute::InAlloca; |
1921 | case bitc::ATTR_KIND_COLD: |
1922 | return Attribute::Cold; |
1923 | case bitc::ATTR_KIND_CONVERGENT: |
1924 | return Attribute::Convergent; |
1925 | case bitc::ATTR_KIND_DISABLE_SANITIZER_INSTRUMENTATION: |
1926 | return Attribute::DisableSanitizerInstrumentation; |
1927 | case bitc::ATTR_KIND_ELEMENTTYPE: |
1928 | return Attribute::ElementType; |
1929 | case bitc::ATTR_KIND_FNRETTHUNK_EXTERN: |
1930 | return Attribute::FnRetThunkExtern; |
1931 | case bitc::ATTR_KIND_INLINE_HINT: |
1932 | return Attribute::InlineHint; |
1933 | case bitc::ATTR_KIND_IN_REG: |
1934 | return Attribute::InReg; |
1935 | case bitc::ATTR_KIND_JUMP_TABLE: |
1936 | return Attribute::JumpTable; |
1937 | case bitc::ATTR_KIND_MEMORY: |
1938 | return Attribute::Memory; |
1939 | case bitc::ATTR_KIND_NOFPCLASS: |
1940 | return Attribute::NoFPClass; |
1941 | case bitc::ATTR_KIND_MIN_SIZE: |
1942 | return Attribute::MinSize; |
1943 | case bitc::ATTR_KIND_NAKED: |
1944 | return Attribute::Naked; |
1945 | case bitc::ATTR_KIND_NEST: |
1946 | return Attribute::Nest; |
1947 | case bitc::ATTR_KIND_NO_ALIAS: |
1948 | return Attribute::NoAlias; |
1949 | case bitc::ATTR_KIND_NO_BUILTIN: |
1950 | return Attribute::NoBuiltin; |
1951 | case bitc::ATTR_KIND_NO_CALLBACK: |
1952 | return Attribute::NoCallback; |
1953 | case bitc::ATTR_KIND_NO_CAPTURE: |
1954 | return Attribute::NoCapture; |
1955 | case bitc::ATTR_KIND_NO_DUPLICATE: |
1956 | return Attribute::NoDuplicate; |
1957 | case bitc::ATTR_KIND_NOFREE: |
1958 | return Attribute::NoFree; |
1959 | case bitc::ATTR_KIND_NO_IMPLICIT_FLOAT: |
1960 | return Attribute::NoImplicitFloat; |
1961 | case bitc::ATTR_KIND_NO_INLINE: |
1962 | return Attribute::NoInline; |
1963 | case bitc::ATTR_KIND_NO_RECURSE: |
1964 | return Attribute::NoRecurse; |
1965 | case bitc::ATTR_KIND_NO_MERGE: |
1966 | return Attribute::NoMerge; |
1967 | case bitc::ATTR_KIND_NON_LAZY_BIND: |
1968 | return Attribute::NonLazyBind; |
1969 | case bitc::ATTR_KIND_NON_NULL: |
1970 | return Attribute::NonNull; |
1971 | case bitc::ATTR_KIND_DEREFERENCEABLE: |
1972 | return Attribute::Dereferenceable; |
1973 | case bitc::ATTR_KIND_DEREFERENCEABLE_OR_NULL: |
1974 | return Attribute::DereferenceableOrNull; |
1975 | case bitc::ATTR_KIND_ALLOC_ALIGN: |
1976 | return Attribute::AllocAlign; |
1977 | case bitc::ATTR_KIND_ALLOC_KIND: |
1978 | return Attribute::AllocKind; |
1979 | case bitc::ATTR_KIND_ALLOC_SIZE: |
1980 | return Attribute::AllocSize; |
1981 | case bitc::ATTR_KIND_ALLOCATED_POINTER: |
1982 | return Attribute::AllocatedPointer; |
1983 | case bitc::ATTR_KIND_NO_RED_ZONE: |
1984 | return Attribute::NoRedZone; |
1985 | case bitc::ATTR_KIND_NO_RETURN: |
1986 | return Attribute::NoReturn; |
1987 | case bitc::ATTR_KIND_NOSYNC: |
1988 | return Attribute::NoSync; |
1989 | case bitc::ATTR_KIND_NOCF_CHECK: |
1990 | return Attribute::NoCfCheck; |
1991 | case bitc::ATTR_KIND_NO_PROFILE: |
1992 | return Attribute::NoProfile; |
1993 | case bitc::ATTR_KIND_SKIP_PROFILE: |
1994 | return Attribute::SkipProfile; |
1995 | case bitc::ATTR_KIND_NO_UNWIND: |
1996 | return Attribute::NoUnwind; |
1997 | case bitc::ATTR_KIND_NO_SANITIZE_BOUNDS: |
1998 | return Attribute::NoSanitizeBounds; |
1999 | case bitc::ATTR_KIND_NO_SANITIZE_COVERAGE: |
2000 | return Attribute::NoSanitizeCoverage; |
2001 | case bitc::ATTR_KIND_NULL_POINTER_IS_VALID: |
2002 | return Attribute::NullPointerIsValid; |
2003 | case bitc::ATTR_KIND_OPT_FOR_FUZZING: |
2004 | return Attribute::OptForFuzzing; |
2005 | case bitc::ATTR_KIND_OPTIMIZE_FOR_SIZE: |
2006 | return Attribute::OptimizeForSize; |
2007 | case bitc::ATTR_KIND_OPTIMIZE_NONE: |
2008 | return Attribute::OptimizeNone; |
2009 | case bitc::ATTR_KIND_READ_NONE: |
2010 | return Attribute::ReadNone; |
2011 | case bitc::ATTR_KIND_READ_ONLY: |
2012 | return Attribute::ReadOnly; |
2013 | case bitc::ATTR_KIND_RETURNED: |
2014 | return Attribute::Returned; |
2015 | case bitc::ATTR_KIND_RETURNS_TWICE: |
2016 | return Attribute::ReturnsTwice; |
2017 | case bitc::ATTR_KIND_S_EXT: |
2018 | return Attribute::SExt; |
2019 | case bitc::ATTR_KIND_SPECULATABLE: |
2020 | return Attribute::Speculatable; |
2021 | case bitc::ATTR_KIND_STACK_ALIGNMENT: |
2022 | return Attribute::StackAlignment; |
2023 | case bitc::ATTR_KIND_STACK_PROTECT: |
2024 | return Attribute::StackProtect; |
2025 | case bitc::ATTR_KIND_STACK_PROTECT_REQ: |
2026 | return Attribute::StackProtectReq; |
2027 | case bitc::ATTR_KIND_STACK_PROTECT_STRONG: |
2028 | return Attribute::StackProtectStrong; |
2029 | case bitc::ATTR_KIND_SAFESTACK: |
2030 | return Attribute::SafeStack; |
2031 | case bitc::ATTR_KIND_SHADOWCALLSTACK: |
2032 | return Attribute::ShadowCallStack; |
2033 | case bitc::ATTR_KIND_STRICT_FP: |
2034 | return Attribute::StrictFP; |
2035 | case bitc::ATTR_KIND_STRUCT_RET: |
2036 | return Attribute::StructRet; |
2037 | case bitc::ATTR_KIND_SANITIZE_ADDRESS: |
2038 | return Attribute::SanitizeAddress; |
2039 | case bitc::ATTR_KIND_SANITIZE_HWADDRESS: |
2040 | return Attribute::SanitizeHWAddress; |
2041 | case bitc::ATTR_KIND_SANITIZE_THREAD: |
2042 | return Attribute::SanitizeThread; |
2043 | case bitc::ATTR_KIND_SANITIZE_MEMORY: |
2044 | return Attribute::SanitizeMemory; |
2045 | case bitc::ATTR_KIND_SPECULATIVE_LOAD_HARDENING: |
2046 | return Attribute::SpeculativeLoadHardening; |
2047 | case bitc::ATTR_KIND_SWIFT_ERROR: |
2048 | return Attribute::SwiftError; |
2049 | case bitc::ATTR_KIND_SWIFT_SELF: |
2050 | return Attribute::SwiftSelf; |
2051 | case bitc::ATTR_KIND_SWIFT_ASYNC: |
2052 | return Attribute::SwiftAsync; |
2053 | case bitc::ATTR_KIND_UW_TABLE: |
2054 | return Attribute::UWTable; |
2055 | case bitc::ATTR_KIND_VSCALE_RANGE: |
2056 | return Attribute::VScaleRange; |
2057 | case bitc::ATTR_KIND_WILLRETURN: |
2058 | return Attribute::WillReturn; |
2059 | case bitc::ATTR_KIND_WRITEONLY: |
2060 | return Attribute::WriteOnly; |
2061 | case bitc::ATTR_KIND_Z_EXT: |
2062 | return Attribute::ZExt; |
2063 | case bitc::ATTR_KIND_IMMARG: |
2064 | return Attribute::ImmArg; |
2065 | case bitc::ATTR_KIND_SANITIZE_MEMTAG: |
2066 | return Attribute::SanitizeMemTag; |
2067 | case bitc::ATTR_KIND_PREALLOCATED: |
2068 | return Attribute::Preallocated; |
2069 | case bitc::ATTR_KIND_NOUNDEF: |
2070 | return Attribute::NoUndef; |
2071 | case bitc::ATTR_KIND_BYREF: |
2072 | return Attribute::ByRef; |
2073 | case bitc::ATTR_KIND_MUSTPROGRESS: |
2074 | return Attribute::MustProgress; |
2075 | case bitc::ATTR_KIND_HOT: |
2076 | return Attribute::Hot; |
2077 | case bitc::ATTR_KIND_PRESPLIT_COROUTINE: |
2078 | return Attribute::PresplitCoroutine; |
2079 | } |
2080 | } |
2081 | |
2082 | Error BitcodeReader::parseAlignmentValue(uint64_t Exponent, |
2083 | MaybeAlign &Alignment) { |
2084 | // Note: Alignment in bitcode files is incremented by 1, so that zero |
2085 | // can be used for default alignment. |
2086 | if (Exponent > Value::MaxAlignmentExponent + 1) |
2087 | return error("Invalid alignment value"); |
2088 | Alignment = decodeMaybeAlign(Exponent); |
2089 | return Error::success(); |
2090 | } |
2091 | |
2092 | Error BitcodeReader::parseAttrKind(uint64_t Code, Attribute::AttrKind *Kind) { |
2093 | *Kind = getAttrFromCode(Code); |
2094 | if (*Kind == Attribute::None) |
2095 | return error("Unknown attribute kind (" + Twine(Code) + ")"); |
2096 | return Error::success(); |
2097 | } |
2098 | |
2099 | static bool upgradeOldMemoryAttribute(MemoryEffects &ME, uint64_t EncodedKind) { |
2100 | switch (EncodedKind) { |
2101 | case bitc::ATTR_KIND_READ_NONE: |
2102 | ME &= MemoryEffects::none(); |
2103 | return true; |
2104 | case bitc::ATTR_KIND_READ_ONLY: |
2105 | ME &= MemoryEffects::readOnly(); |
2106 | return true; |
2107 | case bitc::ATTR_KIND_WRITEONLY: |
2108 | ME &= MemoryEffects::writeOnly(); |
2109 | return true; |
2110 | case bitc::ATTR_KIND_ARGMEMONLY: |
2111 | ME &= MemoryEffects::argMemOnly(); |
2112 | return true; |
2113 | case bitc::ATTR_KIND_INACCESSIBLEMEM_ONLY: |
2114 | ME &= MemoryEffects::inaccessibleMemOnly(); |
2115 | return true; |
2116 | case bitc::ATTR_KIND_INACCESSIBLEMEM_OR_ARGMEMONLY: |
2117 | ME &= MemoryEffects::inaccessibleOrArgMemOnly(); |
2118 | return true; |
2119 | default: |
2120 | return false; |
2121 | } |
2122 | } |
2123 | |
2124 | Error BitcodeReader::parseAttributeGroupBlock() { |
2125 | if (Error Err = Stream.EnterSubBlock(bitc::PARAMATTR_GROUP_BLOCK_ID)) |
2126 | return Err; |
2127 | |
2128 | if (!MAttributeGroups.empty()) |
2129 | return error("Invalid multiple blocks"); |
2130 | |
2131 | SmallVector<uint64_t, 64> Record; |
2132 | |
2133 | // Read all the records. |
2134 | while (true) { |
2135 | Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks(); |
2136 | if (!MaybeEntry) |
2137 | return MaybeEntry.takeError(); |
2138 | BitstreamEntry Entry = MaybeEntry.get(); |
2139 | |
2140 | switch (Entry.Kind) { |
2141 | case BitstreamEntry::SubBlock: // Handled for us already. |
2142 | case BitstreamEntry::Error: |
2143 | return error("Malformed block"); |
2144 | case BitstreamEntry::EndBlock: |
2145 | return Error::success(); |
2146 | case BitstreamEntry::Record: |
2147 | // The interesting case. |
2148 | break; |
2149 | } |
2150 | |
2151 | // Read a record. |
2152 | Record.clear(); |
2153 | Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record); |
2154 | if (!MaybeRecord) |
2155 | return MaybeRecord.takeError(); |
2156 | switch (MaybeRecord.get()) { |
2157 | default: // Default behavior: ignore. |
2158 | break; |
2159 | case bitc::PARAMATTR_GRP_CODE_ENTRY: { // ENTRY: [grpid, idx, a0, a1, ...] |
2160 | if (Record.size() < 3) |
2161 | return error("Invalid grp record"); |
2162 | |
2163 | uint64_t GrpID = Record[0]; |
2164 | uint64_t Idx = Record[1]; // Index of the object this attribute refers to. |
2165 | |
2166 | AttrBuilder B(Context); |
2167 | MemoryEffects ME = MemoryEffects::unknown(); |
2168 | for (unsigned i = 2, e = Record.size(); i != e; ++i) { |
2169 | if (Record[i] == 0) { // Enum attribute |
2170 | Attribute::AttrKind Kind; |
2171 | uint64_t EncodedKind = Record[++i]; |
2172 | if (Idx == AttributeList::FunctionIndex && |
2173 | upgradeOldMemoryAttribute(ME, EncodedKind)) |
2174 | continue; |
2175 | |
2176 | if (Error Err = parseAttrKind(EncodedKind, &Kind)) |
2177 | return Err; |
2178 | |
2179 | // Upgrade old-style byval attribute to one with a type, even if it's |
2180 | // nullptr. We will have to insert the real type when we associate |
2181 | // this AttributeList with a function. |
2182 | if (Kind == Attribute::ByVal) |
2183 | B.addByValAttr(nullptr); |
2184 | else if (Kind == Attribute::StructRet) |
2185 | B.addStructRetAttr(nullptr); |
2186 | else if (Kind == Attribute::InAlloca) |
2187 | B.addInAllocaAttr(nullptr); |
2188 | else if (Kind == Attribute::UWTable) |
2189 | B.addUWTableAttr(UWTableKind::Default); |
2190 | else if (Attribute::isEnumAttrKind(Kind)) |
2191 | B.addAttribute(Kind); |
2192 | else |
2193 | return error("Not an enum attribute"); |
2194 | } else if (Record[i] == 1) { // Integer attribute |
2195 | Attribute::AttrKind Kind; |
2196 | if (Error Err = parseAttrKind(Record[++i], &Kind)) |
2197 | return Err; |
2198 | if (!Attribute::isIntAttrKind(Kind)) |
2199 | return error("Not an int attribute"); |
2200 | if (Kind == Attribute::Alignment) |
2201 | B.addAlignmentAttr(Record[++i]); |
2202 | else if (Kind == Attribute::StackAlignment) |
2203 | B.addStackAlignmentAttr(Record[++i]); |
2204 | else if (Kind == Attribute::Dereferenceable) |
2205 | B.addDereferenceableAttr(Record[++i]); |
2206 | else if (Kind == Attribute::DereferenceableOrNull) |
2207 | B.addDereferenceableOrNullAttr(Record[++i]); |
2208 | else if (Kind == Attribute::AllocSize) |
2209 | B.addAllocSizeAttrFromRawRepr(Record[++i]); |
2210 | else if (Kind == Attribute::VScaleRange) |
2211 | B.addVScaleRangeAttrFromRawRepr(Record[++i]); |
2212 | else if (Kind == Attribute::UWTable) |
2213 | B.addUWTableAttr(UWTableKind(Record[++i])); |
2214 | else if (Kind == Attribute::AllocKind) |
2215 | B.addAllocKindAttr(static_cast<AllocFnKind>(Record[++i])); |
2216 | else if (Kind == Attribute::Memory) |
2217 | B.addMemoryAttr(MemoryEffects::createFromIntValue(Record[++i])); |
2218 | else if (Kind == Attribute::NoFPClass) |
2219 | B.addNoFPClassAttr( |
2220 | static_cast<FPClassTest>(Record[++i] & fcAllFlags)); |
2221 | } else if (Record[i] == 3 || Record[i] == 4) { // String attribute |
2222 | bool HasValue = (Record[i++] == 4); |
2223 | SmallString<64> KindStr; |
2224 | SmallString<64> ValStr; |
2225 | |
2226 | while (Record[i] != 0 && i != e) |
2227 | KindStr += Record[i++]; |
2228 | assert(Record[i] == 0 && "Kind string not null terminated")(static_cast <bool> (Record[i] == 0 && "Kind string not null terminated" ) ? void (0) : __assert_fail ("Record[i] == 0 && \"Kind string not null terminated\"" , "llvm/lib/Bitcode/Reader/BitcodeReader.cpp", 2228, __extension__ __PRETTY_FUNCTION__)); |
2229 | |
2230 | if (HasValue) { |
2231 | // Has a value associated with it. |
2232 | ++i; // Skip the '0' that terminates the "kind" string. |
2233 | while (Record[i] != 0 && i != e) |
2234 | ValStr += Record[i++]; |
2235 | assert(Record[i] == 0 && "Value string not null terminated")(static_cast <bool> (Record[i] == 0 && "Value string not null terminated" ) ? void (0) : __assert_fail ("Record[i] == 0 && \"Value string not null terminated\"" , "llvm/lib/Bitcode/Reader/BitcodeReader.cpp", 2235, __extension__ __PRETTY_FUNCTION__)); |
2236 | } |
2237 | |
2238 | B.addAttribute(KindStr.str(), ValStr.str()); |
2239 | } else if (Record[i] == 5 || Record[i] == 6) { |
2240 | bool HasType = Record[i] == 6; |
2241 | Attribute::AttrKind Kind; |
2242 | if (Error Err = parseAttrKind(Record[++i], &Kind)) |
2243 | return Err; |
2244 | if (!Attribute::isTypeAttrKind(Kind)) |
2245 | return error("Not a type attribute"); |
2246 | |
2247 | B.addTypeAttr(Kind, HasType ? getTypeByID(Record[++i]) : nullptr); |
2248 | } else { |
2249 | return error("Invalid attribute group entry"); |
2250 | } |
2251 | } |
2252 | |
2253 | if (ME != MemoryEffects::unknown()) |
2254 | B.addMemoryAttr(ME); |
2255 | |
2256 | UpgradeAttributes(B); |
2257 | MAttributeGroups[GrpID] = AttributeList::get(Context, Idx, B); |
2258 | break; |
2259 | } |
2260 | } |
2261 | } |
2262 | } |
2263 | |
2264 | Error BitcodeReader::parseTypeTable() { |
2265 | if (Error Err = Stream.EnterSubBlock(bitc::TYPE_BLOCK_ID_NEW)) |
2266 | return Err; |
2267 | |
2268 | return parseTypeTableBody(); |
2269 | } |
2270 | |
2271 | Error BitcodeReader::parseTypeTableBody() { |
2272 | if (!TypeList.empty()) |
2273 | return error("Invalid multiple blocks"); |
2274 | |
2275 | SmallVector<uint64_t, 64> Record; |
2276 | unsigned NumRecords = 0; |
2277 | |
2278 | SmallString<64> TypeName; |
2279 | |
2280 | // Read all the records for this type table. |
2281 | while (true) { |
2282 | Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks(); |
2283 | if (!MaybeEntry) |
2284 | return MaybeEntry.takeError(); |
2285 | BitstreamEntry Entry = MaybeEntry.get(); |
2286 | |
2287 | switch (Entry.Kind) { |
2288 | case BitstreamEntry::SubBlock: // Handled for us already. |
2289 | case BitstreamEntry::Error: |
2290 | return error("Malformed block"); |
2291 | case BitstreamEntry::EndBlock: |
2292 | if (NumRecords != TypeList.size()) |
2293 | return error("Malformed block"); |
2294 | return Error::success(); |
2295 | case BitstreamEntry::Record: |
2296 | // The interesting case. |
2297 | break; |
2298 | } |
2299 | |
2300 | // Read a record. |
2301 | Record.clear(); |
2302 | Type *ResultTy = nullptr; |
2303 | SmallVector<unsigned> ContainedIDs; |
2304 | Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record); |
2305 | if (!MaybeRecord) |
2306 | return MaybeRecord.takeError(); |
2307 | switch (MaybeRecord.get()) { |
2308 | default: |
2309 | return error("Invalid value"); |
2310 | case bitc::TYPE_CODE_NUMENTRY: // TYPE_CODE_NUMENTRY: [numentries] |
2311 | // TYPE_CODE_NUMENTRY contains a count of the number of types in the |
2312 | // type list. This allows us to reserve space. |
2313 | if (Record.empty()) |
2314 | return error("Invalid numentry record"); |
2315 | TypeList.resize(Record[0]); |
2316 | continue; |
2317 | case bitc::TYPE_CODE_VOID: // VOID |
2318 | ResultTy = Type::getVoidTy(Context); |
2319 | break; |
2320 | case bitc::TYPE_CODE_HALF: // HALF |
2321 | ResultTy = Type::getHalfTy(Context); |
2322 | break; |
2323 | case bitc::TYPE_CODE_BFLOAT: // BFLOAT |
2324 | ResultTy = Type::getBFloatTy(Context); |
2325 | break; |
2326 | case bitc::TYPE_CODE_FLOAT: // FLOAT |
2327 | ResultTy = Type::getFloatTy(Context); |
2328 | break; |
2329 | case bitc::TYPE_CODE_DOUBLE: // DOUBLE |
2330 | ResultTy = Type::getDoubleTy(Context); |
2331 | break; |
2332 | case bitc::TYPE_CODE_X86_FP80: // X86_FP80 |
2333 | ResultTy = Type::getX86_FP80Ty(Context); |
2334 | break; |
2335 | case bitc::TYPE_CODE_FP128: // FP128 |
2336 | ResultTy = Type::getFP128Ty(Context); |
2337 | break; |
2338 | case bitc::TYPE_CODE_PPC_FP128: // PPC_FP128 |
2339 | ResultTy = Type::getPPC_FP128Ty(Context); |
2340 | break; |
2341 | case bitc::TYPE_CODE_LABEL: // LABEL |
2342 | ResultTy = Type::getLabelTy(Context); |
2343 | break; |
2344 | case bitc::TYPE_CODE_METADATA: // METADATA |
2345 | ResultTy = Type::getMetadataTy(Context); |
2346 | break; |
2347 | case bitc::TYPE_CODE_X86_MMX: // X86_MMX |
2348 | ResultTy = Type::getX86_MMXTy(Context); |
2349 | break; |
2350 | case bitc::TYPE_CODE_X86_AMX: // X86_AMX |
2351 | ResultTy = Type::getX86_AMXTy(Context); |
2352 | break; |
2353 | case bitc::TYPE_CODE_TOKEN: // TOKEN |
2354 | ResultTy = Type::getTokenTy(Context); |
2355 | break; |
2356 | case bitc::TYPE_CODE_INTEGER: { // INTEGER: [width] |
2357 | if (Record.empty()) |
2358 | return error("Invalid integer record"); |
2359 | |
2360 | uint64_t NumBits = Record[0]; |
2361 | if (NumBits < IntegerType::MIN_INT_BITS || |
2362 | NumBits > IntegerType::MAX_INT_BITS) |
2363 | return error("Bitwidth for integer type out of range"); |
2364 | ResultTy = IntegerType::get(Context, NumBits); |
2365 | break; |
2366 | } |
2367 | case bitc::TYPE_CODE_POINTER: { // POINTER: [pointee type] or |
2368 | // [pointee type, address space] |
2369 | if (Record.empty()) |
2370 | return error("Invalid pointer record"); |
2371 | unsigned AddressSpace = 0; |
2372 | if (Record.size() == 2) |
2373 | AddressSpace = Record[1]; |
2374 | ResultTy = getTypeByID(Record[0]); |
2375 | if (!ResultTy || |
2376 | !PointerType::isValidElementType(ResultTy)) |
2377 | return error("Invalid type"); |
2378 | ContainedIDs.push_back(Record[0]); |
2379 | ResultTy = PointerType::get(ResultTy, AddressSpace); |
2380 | break; |
2381 | } |
2382 | case bitc::TYPE_CODE_OPAQUE_POINTER: { // OPAQUE_POINTER: [addrspace] |
2383 | if (Record.size() != 1) |
2384 | return error("Invalid opaque pointer record"); |
2385 | if (Context.supportsTypedPointers()) |
2386 | return error( |
2387 | "Opaque pointers are only supported in -opaque-pointers mode"); |
2388 | unsigned AddressSpace = Record[0]; |
2389 | ResultTy = PointerType::get(Context, AddressSpace); |
2390 | break; |
2391 | } |
2392 | case bitc::TYPE_CODE_FUNCTION_OLD: { |
2393 | // Deprecated, but still needed to read old bitcode files. |
2394 | // FUNCTION: [vararg, attrid, retty, paramty x N] |
2395 | if (Record.size() < 3) |
2396 | return error("Invalid function record"); |
2397 | SmallVector<Type*, 8> ArgTys; |
2398 | for (unsigned i = 3, e = Record.size(); i != e; ++i) { |
2399 | if (Type *T = getTypeByID(Record[i])) |
2400 | ArgTys.push_back(T); |
2401 | else |
2402 | break; |
2403 | } |
2404 | |
2405 | ResultTy = getTypeByID(Record[2]); |
2406 | if (!ResultTy || ArgTys.size() < Record.size()-3) |
2407 | return error("Invalid type"); |
2408 | |
2409 | ContainedIDs.append(Record.begin() + 2, Record.end()); |
2410 | ResultTy = FunctionType::get(ResultTy, ArgTys, Record[0]); |
2411 | break; |
2412 | } |
2413 | case bitc::TYPE_CODE_FUNCTION: { |
2414 | // FUNCTION: [vararg, retty, paramty x N] |
2415 | if (Record.size() < 2) |
2416 | return error("Invalid function record"); |
2417 | SmallVector<Type*, 8> ArgTys; |
2418 | for (unsigned i = 2, e = Record.size(); i != e; ++i) { |
2419 | if (Type *T = getTypeByID(Record[i])) { |
2420 | if (!FunctionType::isValidArgumentType(T)) |
2421 | return error("Invalid function argument type"); |
2422 | ArgTys.push_back(T); |
2423 | } |
2424 | else |
2425 | break; |
2426 | } |
2427 | |
2428 | ResultTy = getTypeByID(Record[1]); |
2429 | if (!ResultTy || ArgTys.size() < Record.size()-2) |
2430 | return error("Invalid type"); |
2431 | |
2432 | ContainedIDs.append(Record.begin() + 1, Record.end()); |
2433 | ResultTy = FunctionType::get(ResultTy, ArgTys, Record[0]); |
2434 | break; |
2435 | } |
2436 | case bitc::TYPE_CODE_STRUCT_ANON: { // STRUCT: [ispacked, eltty x N] |
2437 | if (Record.empty()) |
2438 | return error("Invalid anon struct record"); |
2439 | SmallVector<Type*, 8> EltTys; |
2440 | for (unsigned i = 1, e = Record.size(); i != e; ++i) { |
2441 | if (Type *T = getTypeByID(Record[i])) |
2442 | EltTys.push_back(T); |
2443 | else |
2444 | break; |
2445 | } |
2446 | if (EltTys.size() != Record.size()-1) |
2447 | return error("Invalid type"); |
2448 | ContainedIDs.append(Record.begin() + 1, Record.end()); |
2449 | ResultTy = StructType::get(Context, EltTys, Record[0]); |
2450 | break; |
2451 | } |
2452 | case bitc::TYPE_CODE_STRUCT_NAME: // STRUCT_NAME: [strchr x N] |
2453 | if (convertToString(Record, 0, TypeName)) |
2454 | return error("Invalid struct name record"); |
2455 | continue; |
2456 | |
2457 | case bitc::TYPE_CODE_STRUCT_NAMED: { // STRUCT: [ispacked, eltty x N] |
2458 | if (Record.empty()) |
2459 | return error("Invalid named struct record"); |
2460 | |
2461 | if (NumRecords >= TypeList.size()) |
2462 | return error("Invalid TYPE table"); |
2463 | |
2464 | // Check to see if this was forward referenced, if so fill in the temp. |
2465 | StructType *Res = cast_or_null<StructType>(TypeList[NumRecords]); |
2466 | if (Res) { |
2467 | Res->setName(TypeName); |
2468 | TypeList[NumRecords] = nullptr; |
2469 | } else // Otherwise, create a new struct. |
2470 | Res = createIdentifiedStructType(Context, TypeName); |
2471 | TypeName.clear(); |
2472 | |
2473 | SmallVector<Type*, 8> EltTys; |
2474 | for (unsigned i = 1, e = Record.size(); i != e; ++i) { |
2475 | if (Type *T = getTypeByID(Record[i])) |
2476 | EltTys.push_back(T); |
2477 | else |
2478 | break; |
2479 | } |
2480 | if (EltTys.size() != Record.size()-1) |
2481 | return error("Invalid named struct record"); |
2482 | Res->setBody(EltTys, Record[0]); |
2483 | ContainedIDs.append(Record.begin() + 1, Record.end()); |
2484 | ResultTy = Res; |
2485 | break; |
2486 | } |
2487 | case bitc::TYPE_CODE_OPAQUE: { // OPAQUE: [] |
2488 | if (Record.size() != 1) |
2489 | return error("Invalid opaque type record"); |
2490 | |
2491 | if (NumRecords >= TypeList.size()) |
2492 | return error("Invalid TYPE table"); |
2493 | |
2494 | // Check to see if this was forward referenced, if so fill in the temp. |
2495 | StructType *Res = cast_or_null<StructType>(TypeList[NumRecords]); |
2496 | if (Res) { |
2497 | Res->setName(TypeName); |
2498 | TypeList[NumRecords] = nullptr; |
2499 | } else // Otherwise, create a new struct with no body. |
2500 | Res = createIdentifiedStructType(Context, TypeName); |
2501 | TypeName.clear(); |
2502 | ResultTy = Res; |
2503 | break; |
2504 | } |
2505 | case bitc::TYPE_CODE_TARGET_TYPE: { // TARGET_TYPE: [NumTy, Tys..., Ints...] |
2506 | if (Record.size() < 1) |
2507 | return error("Invalid target extension type record"); |
2508 | |
2509 | if (NumRecords >= TypeList.size()) |
2510 | return error("Invalid TYPE table"); |
2511 | |
2512 | if (Record[0] >= Record.size()) |
2513 | return error("Too many type parameters"); |
2514 | |
2515 | unsigned NumTys = Record[0]; |
2516 | SmallVector<Type *, 4> TypeParams; |
2517 | SmallVector<unsigned, 8> IntParams; |
2518 | for (unsigned i = 0; i < NumTys; i++) { |
2519 | if (Type *T = getTypeByID(Record[i + 1])) |
2520 | TypeParams.push_back(T); |
2521 | else |
2522 | return error("Invalid type"); |
2523 | } |
2524 | |
2525 | for (unsigned i = NumTys + 1, e = Record.size(); i < e; i++) { |
2526 | if (Record[i] > UINT_MAX(2147483647 *2U +1U)) |
2527 | return error("Integer parameter too large"); |
2528 | IntParams.push_back(Record[i]); |
2529 | } |
2530 | ResultTy = TargetExtType::get(Context, TypeName, TypeParams, IntParams); |
2531 | TypeName.clear(); |
2532 | break; |
2533 | } |
2534 | case bitc::TYPE_CODE_ARRAY: // ARRAY: [numelts, eltty] |
2535 | if (Record.size() < 2) |
2536 | return error("Invalid array type record"); |
2537 | ResultTy = getTypeByID(Record[1]); |
2538 | if (!ResultTy || !ArrayType::isValidElementType(ResultTy)) |
2539 | return error("Invalid type"); |
2540 | ContainedIDs.push_back(Record[1]); |
2541 | ResultTy = ArrayType::get(ResultTy, Record[0]); |
2542 | break; |
2543 | case bitc::TYPE_CODE_VECTOR: // VECTOR: [numelts, eltty] or |
2544 | // [numelts, eltty, scalable] |
2545 | if (Record.size() < 2) |
2546 | return error("Invalid vector type record"); |
2547 | if (Record[0] == 0) |
2548 | return error("Invalid vector length"); |
2549 | ResultTy = getTypeByID(Record[1]); |
2550 | if (!ResultTy || !VectorType::isValidElementType(ResultTy)) |
2551 | return error("Invalid type"); |
2552 | bool Scalable = Record.size() > 2 ? Record[2] : false; |
2553 | ContainedIDs.push_back(Record[1]); |
2554 | ResultTy = VectorType::get(ResultTy, Record[0], Scalable); |
2555 | break; |
2556 | } |
2557 | |
2558 | if (NumRecords >= TypeList.size()) |
2559 | return error("Invalid TYPE table"); |
2560 | if (TypeList[NumRecords]) |
2561 | return error( |
2562 | "Invalid TYPE table: Only named structs can be forward referenced"); |
2563 | assert(ResultTy && "Didn't read a type?")(static_cast <bool> (ResultTy && "Didn't read a type?" ) ? void (0) : __assert_fail ("ResultTy && \"Didn't read a type?\"" , "llvm/lib/Bitcode/Reader/BitcodeReader.cpp", 2563, __extension__ __PRETTY_FUNCTION__)); |
2564 | TypeList[NumRecords] = ResultTy; |
2565 | if (!ContainedIDs.empty()) |
2566 | ContainedTypeIDs[NumRecords] = std::move(ContainedIDs); |
2567 | ++NumRecords; |
2568 | } |
2569 | } |
2570 | |
2571 | Error BitcodeReader::parseOperandBundleTags() { |
2572 | if (Error Err = Stream.EnterSubBlock(bitc::OPERAND_BUNDLE_TAGS_BLOCK_ID)) |
2573 | return Err; |
2574 | |
2575 | if (!BundleTags.empty()) |
2576 | return error("Invalid multiple blocks"); |
2577 | |
2578 | SmallVector<uint64_t, 64> Record; |
2579 | |
2580 | while (true) { |
2581 | Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks(); |
2582 | if (!MaybeEntry) |
2583 | return MaybeEntry.takeError(); |
2584 | BitstreamEntry Entry = MaybeEntry.get(); |
2585 | |
2586 | switch (Entry.Kind) { |
2587 | case BitstreamEntry::SubBlock: // Handled for us already. |
2588 | case BitstreamEntry::Error: |
2589 | return error("Malformed block"); |
2590 | case BitstreamEntry::EndBlock: |
2591 | return Error::success(); |
2592 | case BitstreamEntry::Record: |
2593 | // The interesting case. |
2594 | break; |
2595 | } |
2596 | |
2597 | // Tags are implicitly mapped to integers by their order. |
2598 | |
2599 | Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record); |
2600 | if (!MaybeRecord) |
2601 | return MaybeRecord.takeError(); |
2602 | if (MaybeRecord.get() != bitc::OPERAND_BUNDLE_TAG) |
2603 | return error("Invalid operand bundle record"); |
2604 | |
2605 | // OPERAND_BUNDLE_TAG: [strchr x N] |
2606 | BundleTags.emplace_back(); |
2607 | if (convertToString(Record, 0, BundleTags.back())) |
2608 | return error("Invalid operand bundle record"); |
2609 | Record.clear(); |
2610 | } |
2611 | } |
2612 | |
2613 | Error BitcodeReader::parseSyncScopeNames() { |
2614 | if (Error Err = Stream.EnterSubBlock(bitc::SYNC_SCOPE_NAMES_BLOCK_ID)) |
2615 | return Err; |
2616 | |
2617 | if (!SSIDs.empty()) |
2618 | return error("Invalid multiple synchronization scope names blocks"); |
2619 | |
2620 | SmallVector<uint64_t, 64> Record; |
2621 | while (true) { |
2622 | Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks(); |
2623 | if (!MaybeEntry) |
2624 | return MaybeEntry.takeError(); |
2625 | BitstreamEntry Entry = MaybeEntry.get(); |
2626 | |
2627 | switch (Entry.Kind) { |
2628 | case BitstreamEntry::SubBlock: // Handled for us already. |
2629 | case BitstreamEntry::Error: |
2630 | return error("Malformed block"); |
2631 | case BitstreamEntry::EndBlock: |
2632 | if (SSIDs.empty()) |
2633 | return error("Invalid empty synchronization scope names block"); |
2634 | return Error::success(); |
2635 | case BitstreamEntry::Record: |
2636 | // The interesting case. |
2637 | break; |
2638 | } |
2639 | |
2640 | // Synchronization scope names are implicitly mapped to synchronization |
2641 | // scope IDs by their order. |
2642 | |
2643 | Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record); |
2644 | if (!MaybeRecord) |
2645 | return MaybeRecord.takeError(); |
2646 | if (MaybeRecord.get() != bitc::SYNC_SCOPE_NAME) |
2647 | return error("Invalid sync scope record"); |
2648 | |
2649 | SmallString<16> SSN; |
2650 | if (convertToString(Record, 0, SSN)) |
2651 | return error("Invalid sync scope record"); |
2652 | |
2653 | SSIDs.push_back(Context.getOrInsertSyncScopeID(SSN)); |
2654 | Record.clear(); |
2655 | } |
2656 | } |
2657 | |
2658 | /// Associate a value with its name from the given index in the provided record. |
2659 | Expected<Value *> BitcodeReader::recordValue(SmallVectorImpl<uint64_t> &Record, |
2660 | unsigned NameIndex, Triple &TT) { |
2661 | SmallString<128> ValueName; |
2662 | if (convertToString(Record, NameIndex, ValueName)) |
2663 | return error("Invalid record"); |
2664 | unsigned ValueID = Record[0]; |
2665 | if (ValueID >= ValueList.size() || !ValueList[ValueID]) |
2666 | return error("Invalid record"); |
2667 | Value *V = ValueList[ValueID]; |
2668 | |
2669 | StringRef NameStr(ValueName.data(), ValueName.size()); |
2670 | if (NameStr.find_first_of(0) != StringRef::npos) |
2671 | return error("Invalid value name"); |
2672 | V->setName(NameStr); |
2673 | auto *GO = dyn_cast<GlobalObject>(V); |
2674 | if (GO && ImplicitComdatObjects.contains(GO) && TT.supportsCOMDAT()) |
2675 | GO->setComdat(TheModule->getOrInsertComdat(V->getName())); |
2676 | return V; |
2677 | } |
2678 | |
2679 | /// Helper to note and return the current location, and jump to the given |
2680 | /// offset. |
2681 | static Expected<uint64_t> jumpToValueSymbolTable(uint64_t Offset, |
2682 | BitstreamCursor &Stream) { |
2683 | // Save the current parsing location so we can jump back at the end |
2684 | // of the VST read. |
2685 | uint64_t CurrentBit = Stream.GetCurrentBitNo(); |
2686 | if (Error JumpFailed = Stream.JumpToBit(Offset * 32)) |
2687 | return std::move(JumpFailed); |
2688 | Expected<BitstreamEntry> MaybeEntry = Stream.advance(); |
2689 | if (!MaybeEntry) |
2690 | return MaybeEntry.takeError(); |
2691 | if (MaybeEntry.get().Kind != BitstreamEntry::SubBlock || |
2692 | MaybeEntry.get().ID != bitc::VALUE_SYMTAB_BLOCK_ID) |
2693 | return error("Expected value symbol table subblock"); |
2694 | return CurrentBit; |
2695 | } |
2696 | |
2697 | void BitcodeReader::setDeferredFunctionInfo(unsigned FuncBitcodeOffsetDelta, |
2698 | Function *F, |
2699 | ArrayRef<uint64_t> Record) { |
2700 | // Note that we subtract 1 here because the offset is relative to one word |
2701 | // before the start of the identification or module block, which was |
2702 | // historically always the start of the regular bitcode header. |
2703 | uint64_t FuncWordOffset = Record[1] - 1; |
2704 | uint64_t FuncBitOffset = FuncWordOffset * 32; |
2705 | DeferredFunctionInfo[F] = FuncBitOffset + FuncBitcodeOffsetDelta; |
2706 | // Set the LastFunctionBlockBit to point to the last function block. |
2707 | // Later when parsing is resumed after function materialization, |
2708 | // we can simply skip that last function block. |
2709 | if (FuncBitOffset > LastFunctionBlockBit) |
2710 | LastFunctionBlockBit = FuncBitOffset; |
2711 | } |
2712 | |
2713 | /// Read a new-style GlobalValue symbol table. |
2714 | Error BitcodeReader::parseGlobalValueSymbolTable() { |
2715 | unsigned FuncBitcodeOffsetDelta = |
2716 | Stream.getAbbrevIDWidth() + bitc::BlockIDWidth; |
2717 | |
2718 | if (Error Err = Stream.EnterSubBlock(bitc::VALUE_SYMTAB_BLOCK_ID)) |
2719 | return Err; |
2720 | |
2721 | SmallVector<uint64_t, 64> Record; |
2722 | while (true) { |
2723 | Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks(); |
2724 | if (!MaybeEntry) |
2725 | return MaybeEntry.takeError(); |
2726 | BitstreamEntry Entry = MaybeEntry.get(); |
2727 | |
2728 | switch (Entry.Kind) { |
2729 | case BitstreamEntry::SubBlock: |
2730 | case BitstreamEntry::Error: |
2731 | return error("Malformed block"); |
2732 | case BitstreamEntry::EndBlock: |
2733 | return Error::success(); |
2734 | case BitstreamEntry::Record: |
2735 | break; |
2736 | } |
2737 | |
2738 | Record.clear(); |
2739 | Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record); |
2740 | if (!MaybeRecord) |
2741 | return MaybeRecord.takeError(); |
2742 | switch (MaybeRecord.get()) { |
2743 | case bitc::VST_CODE_FNENTRY: { // [valueid, offset] |
2744 | unsigned ValueID = Record[0]; |
2745 | if (ValueID >= ValueList.size() || !ValueList[ValueID]) |
2746 | return error("Invalid value reference in symbol table"); |
2747 | setDeferredFunctionInfo(FuncBitcodeOffsetDelta, |
2748 | cast<Function>(ValueList[ValueID]), Record); |
2749 | break; |
2750 | } |
2751 | } |
2752 | } |
2753 | } |
2754 | |
2755 | /// Parse the value symbol table at either the current parsing location or |
2756 | /// at the given bit offset if provided. |
2757 | Error BitcodeReader::parseValueSymbolTable(uint64_t Offset) { |
2758 | uint64_t CurrentBit; |
2759 | // Pass in the Offset to distinguish between calling for the module-level |
2760 | // VST (where we want to jump to the VST offset) and the function-level |
2761 | // VST (where we don't). |
2762 | if (Offset > 0) { |
2763 | Expected<uint64_t> MaybeCurrentBit = jumpToValueSymbolTable(Offset, Stream); |
2764 | if (!MaybeCurrentBit) |
2765 | return MaybeCurrentBit.takeError(); |
2766 | CurrentBit = MaybeCurrentBit.get(); |
2767 | // If this module uses a string table, read this as a module-level VST. |
2768 | if (UseStrtab) { |
2769 | if (Error Err = parseGlobalValueSymbolTable()) |
2770 | return Err; |
2771 | if (Error JumpFailed = Stream.JumpToBit(CurrentBit)) |
2772 | return JumpFailed; |
2773 | return Error::success(); |
2774 | } |
2775 | // Otherwise, the VST will be in a similar format to a function-level VST, |
2776 | // and will contain symbol names. |
2777 | } |
2778 | |
2779 | // Compute the delta between the bitcode indices in the VST (the word offset |
2780 | // to the word-aligned ENTER_SUBBLOCK for the function block, and that |
2781 | // expected by the lazy reader. The reader's EnterSubBlock expects to have |
2782 | // already read the ENTER_SUBBLOCK code (size getAbbrevIDWidth) and BlockID |
2783 | // (size BlockIDWidth). Note that we access the stream's AbbrevID width here |
2784 | // just before entering the VST subblock because: 1) the EnterSubBlock |
2785 | // changes the AbbrevID width; 2) the VST block is nested within the same |
2786 | // outer MODULE_BLOCK as the FUNCTION_BLOCKs and therefore have the same |
2787 | // AbbrevID width before calling EnterSubBlock; and 3) when we want to |
2788 | // jump to the FUNCTION_BLOCK using this offset later, we don't want |
2789 | // to rely on the stream's AbbrevID width being that of the MODULE_BLOCK. |
2790 | unsigned FuncBitcodeOffsetDelta = |
2791 | Stream.getAbbrevIDWidth() + bitc::BlockIDWidth; |
2792 | |
2793 | if (Error Err = Stream.EnterSubBlock(bitc::VALUE_SYMTAB_BLOCK_ID)) |
2794 | return Err; |
2795 | |
2796 | SmallVector<uint64_t, 64> Record; |
2797 | |
2798 | Triple TT(TheModule->getTargetTriple()); |
2799 | |
2800 | // Read all the records for this value table. |
2801 | SmallString<128> ValueName; |
2802 | |
2803 | while (true) { |
2804 | Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks(); |
2805 | if (!MaybeEntry) |
2806 | return MaybeEntry.takeError(); |
2807 | BitstreamEntry Entry = MaybeEntry.get(); |
2808 | |
2809 | switch (Entry.Kind) { |
2810 | case BitstreamEntry::SubBlock: // Handled for us already. |
2811 | case BitstreamEntry::Error: |
2812 | return error("Malformed block"); |
2813 | case BitstreamEntry::EndBlock: |
2814 | if (Offset > 0) |
2815 | if (Error JumpFailed = Stream.JumpToBit(CurrentBit)) |
2816 | return JumpFailed; |
2817 | return Error::success(); |
2818 | case BitstreamEntry::Record: |
2819 | // The interesting case. |
2820 | break; |
2821 | } |
2822 | |
2823 | // Read a record. |
2824 | Record.clear(); |
2825 | Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record); |
2826 | if (!MaybeRecord) |
2827 | return MaybeRecord.takeError(); |
2828 | switch (MaybeRecord.get()) { |
2829 | default: // Default behavior: unknown type. |
2830 | break; |
2831 | case bitc::VST_CODE_ENTRY: { // VST_CODE_ENTRY: [valueid, namechar x N] |
2832 | Expected<Value *> ValOrErr = recordValue(Record, 1, TT); |
2833 | if (Error Err = ValOrErr.takeError()) |
2834 | return Err; |
2835 | ValOrErr.get(); |
2836 | break; |
2837 | } |
2838 | case bitc::VST_CODE_FNENTRY: { |
2839 | // VST_CODE_FNENTRY: [valueid, offset, namechar x N] |
2840 | Expected<Value *> ValOrErr = recordValue(Record, 2, TT); |
2841 | if (Error Err = ValOrErr.takeError()) |
2842 | return Err; |
2843 | Value *V = ValOrErr.get(); |
2844 | |
2845 | // Ignore function offsets emitted for aliases of functions in older |
2846 | // versions of LLVM. |
2847 | if (auto *F = dyn_cast<Function>(V)) |
2848 | setDeferredFunctionInfo(FuncBitcodeOffsetDelta, F, Record); |
2849 | break; |
2850 | } |
2851 | case bitc::VST_CODE_BBENTRY: { |
2852 | if (convertToString(Record, 1, ValueName)) |
2853 | return error("Invalid bbentry record"); |
2854 | BasicBlock *BB = getBasicBlock(Record[0]); |
2855 | if (!BB) |
2856 | return error("Invalid bbentry record"); |
2857 | |
2858 | BB->setName(StringRef(ValueName.data(), ValueName.size())); |
2859 | ValueName.clear(); |
2860 | break; |
2861 | } |
2862 | } |
2863 | } |
2864 | } |
2865 | |
2866 | /// Decode a signed value stored with the sign bit in the LSB for dense VBR |
2867 | /// encoding. |
2868 | uint64_t BitcodeReader::decodeSignRotatedValue(uint64_t V) { |
2869 | if ((V & 1) == 0) |
2870 | return V >> 1; |
2871 | if (V != 1) |
2872 | return -(V >> 1); |
2873 | // There is no such thing as -0 with integers. "-0" really means MININT. |
2874 | return 1ULL << 63; |
2875 | } |
2876 | |
2877 | /// Resolve all of the initializers for global values and aliases that we can. |
2878 | Error BitcodeReader::resolveGlobalAndIndirectSymbolInits() { |
2879 | std::vector<std::pair<GlobalVariable *, unsigned>> GlobalInitWorklist; |
2880 | std::vector<std::pair<GlobalValue *, unsigned>> IndirectSymbolInitWorklist; |
2881 | std::vector<FunctionOperandInfo> FunctionOperandWorklist; |
2882 | |
2883 | GlobalInitWorklist.swap(GlobalInits); |
2884 | IndirectSymbolInitWorklist.swap(IndirectSymbolInits); |
2885 | FunctionOperandWorklist.swap(FunctionOperands); |
2886 | |
2887 | while (!GlobalInitWorklist.empty()) { |
2888 | unsigned ValID = GlobalInitWorklist.back().second; |
2889 | if (ValID >= ValueList.size()) { |
2890 | // Not ready to resolve this yet, it requires something later in the file. |
2891 | GlobalInits.push_back(GlobalInitWorklist.back()); |
2892 | } else { |
2893 | Expected<Constant *> MaybeC = getValueForInitializer(ValID); |
2894 | if (!MaybeC) |
2895 | return MaybeC.takeError(); |
2896 | GlobalInitWorklist.back().first->setInitializer(MaybeC.get()); |
2897 | } |
2898 | GlobalInitWorklist.pop_back(); |
2899 | } |
2900 | |
2901 | while (!IndirectSymbolInitWorklist.empty()) { |
2902 | unsigned ValID = IndirectSymbolInitWorklist.back().second; |
2903 | if (ValID >= ValueList.size()) { |
2904 | IndirectSymbolInits.push_back(IndirectSymbolInitWorklist.back()); |
2905 | } else { |
2906 | Expected<Constant *> MaybeC = getValueForInitializer(ValID); |
2907 | if (!MaybeC) |
2908 | return MaybeC.takeError(); |
2909 | Constant *C = MaybeC.get(); |
2910 | GlobalValue *GV = IndirectSymbolInitWorklist.back().first; |
2911 | if (auto *GA = dyn_cast<GlobalAlias>(GV)) { |
2912 | if (C->getType() != GV->getType()) |
2913 | return error("Alias and aliasee types don't match"); |
2914 | GA->setAliasee(C); |
2915 | } else if (auto *GI = dyn_cast<GlobalIFunc>(GV)) { |
2916 | Type *ResolverFTy = |
2917 | GlobalIFunc::getResolverFunctionType(GI->getValueType()); |
2918 | // Transparently fix up the type for compatibility with older bitcode |
2919 | GI->setResolver(ConstantExpr::getBitCast( |
2920 | C, ResolverFTy->getPointerTo(GI->getAddressSpace()))); |
2921 | } else { |
2922 | return error("Expected an alias or an ifunc"); |
2923 | } |
2924 | } |
2925 | IndirectSymbolInitWorklist.pop_back(); |
2926 | } |
2927 | |
2928 | while (!FunctionOperandWorklist.empty()) { |
2929 | FunctionOperandInfo &Info = FunctionOperandWorklist.back(); |
2930 | if (Info.PersonalityFn) { |
2931 | unsigned ValID = Info.PersonalityFn - 1; |
2932 | if (ValID < ValueList.size()) { |
2933 | Expected<Constant *> MaybeC = getValueForInitializer(ValID); |
2934 | if (!MaybeC) |
2935 | return MaybeC.takeError(); |
2936 | Info.F->setPersonalityFn(MaybeC.get()); |
2937 | Info.PersonalityFn = 0; |
2938 | } |
2939 | } |
2940 | if (Info.Prefix) { |
2941 | unsigned ValID = Info.Prefix - 1; |
2942 | if (ValID < ValueList.size()) { |
2943 | Expected<Constant *> MaybeC = getValueForInitializer(ValID); |
2944 | if (!MaybeC) |
2945 | return MaybeC.takeError(); |
2946 | Info.F->setPrefixData(MaybeC.get()); |
2947 | Info.Prefix = 0; |
2948 | } |
2949 | } |
2950 | if (Info.Prologue) { |
2951 | unsigned ValID = Info.Prologue - 1; |
2952 | if (ValID < ValueList.size()) { |
2953 | Expected<Constant *> MaybeC = getValueForInitializer(ValID); |
2954 | if (!MaybeC) |
2955 | return MaybeC.takeError(); |
2956 | Info.F->setPrologueData(MaybeC.get()); |
2957 | Info.Prologue = 0; |
2958 | } |
2959 | } |
2960 | if (Info.PersonalityFn || Info.Prefix || Info.Prologue) |
2961 | FunctionOperands.push_back(Info); |
2962 | FunctionOperandWorklist.pop_back(); |
2963 | } |
2964 | |
2965 | return Error::success(); |
2966 | } |
2967 | |
2968 | APInt llvm::readWideAPInt(ArrayRef<uint64_t> Vals, unsigned TypeBits) { |
2969 | SmallVector<uint64_t, 8> Words(Vals.size()); |
2970 | transform(Vals, Words.begin(), |
2971 | BitcodeReader::decodeSignRotatedValue); |
2972 | |
2973 | return APInt(TypeBits, Words); |
2974 | } |
2975 | |
2976 | Error BitcodeReader::parseConstants() { |
2977 | if (Error Err = Stream.EnterSubBlock(bitc::CONSTANTS_BLOCK_ID)) |
2978 | return Err; |
2979 | |
2980 | SmallVector<uint64_t, 64> Record; |
2981 | |
2982 | // Read all the records for this value table. |
2983 | Type *CurTy = Type::getInt32Ty(Context); |
2984 | unsigned Int32TyID = getVirtualTypeID(CurTy); |
2985 | unsigned CurTyID = Int32TyID; |
2986 | Type *CurElemTy = nullptr; |
2987 | unsigned NextCstNo = ValueList.size(); |
2988 | |
2989 | while (true) { |
2990 | Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks(); |
2991 | if (!MaybeEntry) |
2992 | return MaybeEntry.takeError(); |
2993 | BitstreamEntry Entry = MaybeEntry.get(); |
2994 | |
2995 | switch (Entry.Kind) { |
2996 | case BitstreamEntry::SubBlock: // Handled for us already. |
2997 | case BitstreamEntry::Error: |
2998 | return error("Malformed block"); |
2999 | case BitstreamEntry::EndBlock: |
3000 | if (NextCstNo != ValueList.size()) |
3001 | return error("Invalid constant reference"); |
3002 | return Error::success(); |
3003 | case BitstreamEntry::Record: |
3004 | // The interesting case. |
3005 | break; |
3006 | } |
3007 | |
3008 | // Read a record. |
3009 | Record.clear(); |
3010 | Type *VoidType = Type::getVoidTy(Context); |
3011 | Value *V = nullptr; |
3012 | Expected<unsigned> MaybeBitCode = Stream.readRecord(Entry.ID, Record); |
3013 | if (!MaybeBitCode) |
3014 | return MaybeBitCode.takeError(); |
3015 | switch (unsigned BitCode = MaybeBitCode.get()) { |
3016 | default: // Default behavior: unknown constant |
3017 | case bitc::CST_CODE_UNDEF: // UNDEF |
3018 | V = UndefValue::get(CurTy); |
3019 | break; |
3020 | case bitc::CST_CODE_POISON: // POISON |
3021 | V = PoisonValue::get(CurTy); |
3022 | break; |
3023 | case bitc::CST_CODE_SETTYPE: // SETTYPE: [typeid] |
3024 | if (Record.empty()) |
3025 | return error("Invalid settype record"); |
3026 | if (Record[0] >= TypeList.size() || !TypeList[Record[0]]) |
3027 | return error("Invalid settype record"); |
3028 | if (TypeList[Record[0]] == VoidType) |
3029 | return error("Invalid constant type"); |
3030 | CurTyID = Record[0]; |
3031 | CurTy = TypeList[CurTyID]; |
3032 | CurElemTy = getPtrElementTypeByID(CurTyID); |
3033 | continue; // Skip the ValueList manipulation. |
3034 | case bitc::CST_CODE_NULL: // NULL |
3035 | if (CurTy->isVoidTy() || CurTy->isFunctionTy() || CurTy->isLabelTy()) |
3036 | return error("Invalid type for a constant null value"); |
3037 | if (auto *TETy = dyn_cast<TargetExtType>(CurTy)) |
3038 | if (!TETy->hasProperty(TargetExtType::HasZeroInit)) |
3039 | return error("Invalid type for a constant null value"); |
3040 | V = Constant::getNullValue(CurTy); |
3041 | break; |
3042 | case bitc::CST_CODE_INTEGER: // INTEGER: [intval] |
3043 | if (!CurTy->isIntegerTy() || Record.empty()) |
3044 | return error("Invalid integer const record"); |
3045 | V = ConstantInt::get(CurTy, decodeSignRotatedValue(Record[0])); |
3046 | break; |
3047 | case bitc::CST_CODE_WIDE_INTEGER: {// WIDE_INTEGER: [n x intval] |
3048 | if (!CurTy->isIntegerTy() || Record.empty()) |
3049 | return error("Invalid wide integer const record"); |
3050 | |
3051 | APInt VInt = |
3052 | readWideAPInt(Record, cast<IntegerType>(CurTy)->getBitWidth()); |
3053 | V = ConstantInt::get(Context, VInt); |
3054 | |
3055 | break; |
3056 | } |
3057 | case bitc::CST_CODE_FLOAT: { // FLOAT: [fpval] |
3058 | if (Record.empty()) |
3059 | return error("Invalid float const record"); |
3060 | if (CurTy->isHalfTy()) |
3061 | V = ConstantFP::get(Context, APFloat(APFloat::IEEEhalf(), |
3062 | APInt(16, (uint16_t)Record[0]))); |
3063 | else if (CurTy->isBFloatTy()) |
3064 | V = ConstantFP::get(Context, APFloat(APFloat::BFloat(), |
3065 | APInt(16, (uint32_t)Record[0]))); |
3066 | else if (CurTy->isFloatTy()) |
3067 | V = ConstantFP::get(Context, APFloat(APFloat::IEEEsingle(), |
3068 | APInt(32, (uint32_t)Record[0]))); |
3069 | else if (CurTy->isDoubleTy()) |
3070 | V = ConstantFP::get(Context, APFloat(APFloat::IEEEdouble(), |
3071 | APInt(64, Record[0]))); |
3072 | else if (CurTy->isX86_FP80Ty()) { |
3073 | // Bits are not stored the same way as a normal i80 APInt, compensate. |
3074 | uint64_t Rearrange[2]; |
3075 | Rearrange[0] = (Record[1] & 0xffffLL) | (Record[0] << 16); |
3076 | Rearrange[1] = Record[0] >> 48; |
3077 | V = ConstantFP::get(Context, APFloat(APFloat::x87DoubleExtended(), |
3078 | APInt(80, Rearrange))); |
3079 | } else if (CurTy->isFP128Ty()) |
3080 | V = ConstantFP::get(Context, APFloat(APFloat::IEEEquad(), |
3081 | APInt(128, Record))); |
3082 | else if (CurTy->isPPC_FP128Ty()) |
3083 | V = ConstantFP::get(Context, APFloat(APFloat::PPCDoubleDouble(), |
3084 | APInt(128, Record))); |
3085 | else |
3086 | V = UndefValue::get(CurTy); |
3087 | break; |
3088 | } |
3089 | |
3090 | case bitc::CST_CODE_AGGREGATE: {// AGGREGATE: [n x value number] |
3091 | if (Record.empty()) |
3092 | return error("Invalid aggregate record"); |
3093 | |
3094 | unsigned Size = Record.size(); |
3095 | SmallVector<unsigned, 16> Elts; |
3096 | for (unsigned i = 0; i != Size; ++i) |
3097 | Elts.push_back(Record[i]); |
3098 | |
3099 | if (isa<StructType>(CurTy)) { |
3100 | V = BitcodeConstant::create( |
3101 | Alloc, CurTy, BitcodeConstant::ConstantStructOpcode, Elts); |
3102 | } else if (isa<ArrayType>(CurTy)) { |
3103 | V = BitcodeConstant::create(Alloc, CurTy, |
3104 | BitcodeConstant::ConstantArrayOpcode, Elts); |
3105 | } else if (isa<VectorType>(CurTy)) { |
3106 | V = BitcodeConstant::create( |
3107 | Alloc, CurTy, BitcodeConstant::ConstantVectorOpcode, Elts); |
3108 | } else { |
3109 | V = UndefValue::get(CurTy); |
3110 | } |
3111 | break; |
3112 | } |
3113 | case bitc::CST_CODE_STRING: // STRING: [values] |
3114 | case bitc::CST_CODE_CSTRING: { // CSTRING: [values] |
3115 | if (Record.empty()) |
3116 | return error("Invalid string record"); |
3117 | |
3118 | SmallString<16> Elts(Record.begin(), Record.end()); |
3119 | V = ConstantDataArray::getString(Context, Elts, |
3120 | BitCode == bitc::CST_CODE_CSTRING); |
3121 | break; |
3122 | } |
3123 | case bitc::CST_CODE_DATA: {// DATA: [n x value] |
3124 | if (Record.empty()) |
3125 | return error("Invalid data record"); |
3126 | |
3127 | Type *EltTy; |
3128 | if (auto *Array = dyn_cast<ArrayType>(CurTy)) |
3129 | EltTy = Array->getElementType(); |
3130 | else |
3131 | EltTy = cast<VectorType>(CurTy)->getElementType(); |
3132 | if (EltTy->isIntegerTy(8)) { |
3133 | SmallVector<uint8_t, 16> Elts(Record.begin(), Record.end()); |
3134 | if (isa<VectorType>(CurTy)) |
3135 | V = ConstantDataVector::get(Context, Elts); |
3136 | else |
3137 | V = ConstantDataArray::get(Context, Elts); |
3138 | } else if (EltTy->isIntegerTy(16)) { |
3139 | SmallVector<uint16_t, 16> Elts(Record.begin(), Record.end()); |
3140 | if (isa<VectorType>(CurTy)) |
3141 | V = ConstantDataVector::get(Context, Elts); |
3142 | else |
3143 | V = ConstantDataArray::get(Context, Elts); |
3144 | } else if (EltTy->isIntegerTy(32)) { |
3145 | SmallVector<uint32_t, 16> Elts(Record.begin(), Record.end()); |
3146 | if (isa<VectorType>(CurTy)) |
3147 | V = ConstantDataVector::get(Context, Elts); |
3148 | else |
3149 | V = ConstantDataArray::get(Context, Elts); |
3150 | } else if (EltTy->isIntegerTy(64)) { |
3151 | SmallVector<uint64_t, 16> Elts(Record.begin(), Record.end()); |
3152 | if (isa<VectorType>(CurTy)) |
3153 | V = ConstantDataVector::get(Context, Elts); |
3154 | else |
3155 | V = ConstantDataArray::get(Context, Elts); |
3156 | } else if (EltTy->isHalfTy()) { |
3157 | SmallVector<uint16_t, 16> Elts(Record.begin(), Record.end()); |
3158 | if (isa<VectorType>(CurTy)) |
3159 | V = ConstantDataVector::getFP(EltTy, Elts); |
3160 | else |
3161 | V = ConstantDataArray::getFP(EltTy, Elts); |
3162 | } else if (EltTy->isBFloatTy()) { |
3163 | SmallVector<uint16_t, 16> Elts(Record.begin(), Record.end()); |
3164 | if (isa<VectorType>(CurTy)) |
3165 | V = ConstantDataVector::getFP(EltTy, Elts); |
3166 | else |
3167 | V = ConstantDataArray::getFP(EltTy, Elts); |
3168 | } else if (EltTy->isFloatTy()) { |
3169 | SmallVector<uint32_t, 16> Elts(Record.begin(), Record.end()); |
3170 | if (isa<VectorType>(CurTy)) |
3171 | V = ConstantDataVector::getFP(EltTy, Elts); |
3172 | else |
3173 | V = ConstantDataArray::getFP(EltTy, Elts); |
3174 | } else if (EltTy->isDoubleTy()) { |
3175 | SmallVector<uint64_t, 16> Elts(Record.begin(), Record.end()); |
3176 | if (isa<VectorType>(CurTy)) |
3177 | V = ConstantDataVector::getFP(EltTy, Elts); |
3178 | else |
3179 | V = ConstantDataArray::getFP(EltTy, Elts); |
3180 | } else { |
3181 | return error("Invalid type for value"); |
3182 | } |
3183 | break; |
3184 | } |
3185 | case bitc::CST_CODE_CE_UNOP: { // CE_UNOP: [opcode, opval] |
3186 | if (Record.size() < 2) |
3187 | return error("Invalid unary op constexpr record"); |
3188 | int Opc = getDecodedUnaryOpcode(Record[0], CurTy); |
3189 | if (Opc < 0) { |
3190 | V = UndefValue::get(CurTy); // Unknown unop. |
3191 | } else { |
3192 | V = BitcodeConstant::create(Alloc, CurTy, Opc, (unsigned)Record[1]); |
3193 | } |
3194 | break; |
3195 | } |
3196 | case bitc::CST_CODE_CE_BINOP: { // CE_BINOP: [opcode, opval, opval] |
3197 | if (Record.size() < 3) |
3198 | return error("Invalid binary op constexpr record"); |
3199 | int Opc = getDecodedBinaryOpcode(Record[0], CurTy); |
3200 | if (Opc < 0) { |
3201 | V = UndefValue::get(CurTy); // Unknown binop. |
3202 | } else { |
3203 | uint8_t Flags = 0; |
3204 | if (Record.size() >= 4) { |
3205 | if (Opc == Instruction::Add || |
3206 | Opc == Instruction::Sub || |
3207 | Opc == Instruction::Mul || |
3208 | Opc == Instruction::Shl) { |
3209 | if (Record[3] & (1 << bitc::OBO_NO_SIGNED_WRAP)) |
3210 | Flags |= OverflowingBinaryOperator::NoSignedWrap; |
3211 | if (Record[3] & (1 << bitc::OBO_NO_UNSIGNED_WRAP)) |
3212 | Flags |= OverflowingBinaryOperator::NoUnsignedWrap; |
3213 | } else if (Opc == Instruction::SDiv || |
3214 | Opc == Instruction::UDiv || |
3215 | Opc == Instruction::LShr || |
3216 | Opc == Instruction::AShr) { |
3217 | if (Record[3] & (1 << bitc::PEO_EXACT)) |
3218 | Flags |= SDivOperator::IsExact; |
3219 | } |
3220 | } |
3221 | V = BitcodeConstant::create(Alloc, CurTy, {(uint8_t)Opc, Flags}, |
3222 | {(unsigned)Record[1], (unsigned)Record[2]}); |
3223 | } |
3224 | break; |
3225 | } |
3226 | case bitc::CST_CODE_CE_CAST: { // CE_CAST: [opcode, opty, opval] |
3227 | if (Record.size() < 3) |
3228 | return error("Invalid cast constexpr record"); |
3229 | int Opc = getDecodedCastOpcode(Record[0]); |
3230 | if (Opc < 0) { |
3231 | V = UndefValue::get(CurTy); // Unknown cast. |
3232 | } else { |
3233 | unsigned OpTyID = Record[1]; |
3234 | Type *OpTy = getTypeByID(OpTyID); |
3235 | if (!OpTy) |
3236 | return error("Invalid cast constexpr record"); |
3237 | V = BitcodeConstant::create(Alloc, CurTy, Opc, (unsigned)Record[2]); |
3238 | } |
3239 | break; |
3240 | } |
3241 | case bitc::CST_CODE_CE_INBOUNDS_GEP: // [ty, n x operands] |
3242 | case bitc::CST_CODE_CE_GEP: // [ty, n x operands] |
3243 | case bitc::CST_CODE_CE_GEP_WITH_INRANGE_INDEX: { // [ty, flags, n x |
3244 | // operands] |
3245 | if (Record.size() < 2) |
3246 | return error("Constant GEP record must have at least two elements"); |
3247 | unsigned OpNum = 0; |
3248 | Type *PointeeType = nullptr; |
3249 | if (BitCode == bitc::CST_CODE_CE_GEP_WITH_INRANGE_INDEX || |
3250 | Record.size() % 2) |
3251 | PointeeType = getTypeByID(Record[OpNum++]); |
3252 | |
3253 | bool InBounds = false; |
3254 | std::optional<unsigned> InRangeIndex; |
3255 | if (BitCode == bitc::CST_CODE_CE_GEP_WITH_INRANGE_INDEX) { |
3256 | uint64_t Op = Record[OpNum++]; |
3257 | InBounds = Op & 1; |
3258 | InRangeIndex = Op >> 1; |
3259 | } else if (BitCode == bitc::CST_CODE_CE_INBOUNDS_GEP) |
3260 | InBounds = true; |
3261 | |
3262 | SmallVector<unsigned, 16> Elts; |
3263 | unsigned BaseTypeID = Record[OpNum]; |
3264 | while (OpNum != Record.size()) { |
3265 | unsigned ElTyID = Record[OpNum++]; |
3266 | Type *ElTy = getTypeByID(ElTyID); |
3267 | if (!ElTy) |
3268 | return error("Invalid getelementptr constexpr record"); |
3269 | Elts.push_back(Record[OpNum++]); |
3270 | } |
3271 | |
3272 | if (Elts.size() < 1) |
3273 | return error("Invalid gep with no operands"); |
3274 | |
3275 | Type *BaseType = getTypeByID(BaseTypeID); |
3276 | if (isa<VectorType>(BaseType)) { |
3277 | BaseTypeID = getContainedTypeID(BaseTypeID, 0); |
3278 | BaseType = getTypeByID(BaseTypeID); |
3279 | } |
3280 | |
3281 | PointerType *OrigPtrTy = dyn_cast_or_null<PointerType>(BaseType); |
3282 | if (!OrigPtrTy) |
3283 | return error("GEP base operand must be pointer or vector of pointer"); |
3284 | |
3285 | if (!PointeeType) { |
3286 | PointeeType = getPtrElementTypeByID(BaseTypeID); |
3287 | if (!PointeeType) |
3288 | return error("Missing element type for old-style constant GEP"); |
3289 | } else if (!OrigPtrTy->isOpaqueOrPointeeTypeMatches(PointeeType)) |
3290 | return error("Explicit gep operator type does not match pointee type " |
3291 | "of pointer operand"); |
3292 | |
3293 | V = BitcodeConstant::create(Alloc, CurTy, |
3294 | {Instruction::GetElementPtr, InBounds, |
3295 | InRangeIndex.value_or(-1), PointeeType}, |
3296 | Elts); |
3297 | break; |
3298 | } |
3299 | case bitc::CST_CODE_CE_SELECT: { // CE_SELECT: [opval#, opval#, opval#] |
3300 | if (Record.size() < 3) |
3301 | return error("Invalid select constexpr record"); |
3302 | |
3303 | V = BitcodeConstant::create( |
3304 | Alloc, CurTy, Instruction::Select, |
3305 | {(unsigned)Record[0], (unsigned)Record[1], (unsigned)Record[2]}); |
3306 | break; |
3307 | } |
3308 | case bitc::CST_CODE_CE_EXTRACTELT |
3309 | : { // CE_EXTRACTELT: [opty, opval, opty, opval] |
3310 | if (Record.size() < 3) |
3311 | return error("Invalid extractelement constexpr record"); |
3312 | unsigned OpTyID = Record[0]; |
3313 | VectorType *OpTy = |
3314 | dyn_cast_or_null<VectorType>(getTypeByID(OpTyID)); |
3315 | if (!OpTy) |
3316 | return error("Invalid extractelement constexpr record"); |
3317 | unsigned IdxRecord; |
3318 | if (Record.size() == 4) { |
3319 | unsigned IdxTyID = Record[2]; |
3320 | Type *IdxTy = getTypeByID(IdxTyID); |
3321 | if (!IdxTy) |
3322 | return error("Invalid extractelement constexpr record"); |
3323 | IdxRecord = Record[3]; |
3324 | } else { |
3325 | // Deprecated, but still needed to read old bitcode files. |
3326 | IdxRecord = Record[2]; |
3327 | } |
3328 | V = BitcodeConstant::create(Alloc, CurTy, Instruction::ExtractElement, |
3329 | {(unsigned)Record[1], IdxRecord}); |
3330 | break; |
3331 | } |
3332 | case bitc::CST_CODE_CE_INSERTELT |
3333 | : { // CE_INSERTELT: [opval, opval, opty, opval] |
3334 | VectorType *OpTy = dyn_cast<VectorType>(CurTy); |
3335 | if (Record.size() < 3 || !OpTy) |
3336 | return error("Invalid insertelement constexpr record"); |
3337 | unsigned IdxRecord; |
3338 | if (Record.size() == 4) { |
3339 | unsigned IdxTyID = Record[2]; |
3340 | Type *IdxTy = getTypeByID(IdxTyID); |
3341 | if (!IdxTy) |
3342 | return error("Invalid insertelement constexpr record"); |
3343 | IdxRecord = Record[3]; |
3344 | } else { |
3345 | // Deprecated, but still needed to read old bitcode files. |
3346 | IdxRecord = Record[2]; |
3347 | } |
3348 | V = BitcodeConstant::create( |
3349 | Alloc, CurTy, Instruction::InsertElement, |
3350 | {(unsigned)Record[0], (unsigned)Record[1], IdxRecord}); |
3351 | break; |
3352 | } |
3353 | case bitc::CST_CODE_CE_SHUFFLEVEC: { // CE_SHUFFLEVEC: [opval, opval, opval] |
3354 | VectorType *OpTy = dyn_cast<VectorType>(CurTy); |
3355 | if (Record.size() < 3 || !OpTy) |
3356 | return error("Invalid shufflevector constexpr record"); |
3357 | V = BitcodeConstant::create( |
3358 | Alloc, CurTy, Instruction::ShuffleVector, |
3359 | {(unsigned)Record[0], (unsigned)Record[1], (unsigned)Record[2]}); |
3360 | break; |
3361 | } |
3362 | case bitc::CST_CODE_CE_SHUFVEC_EX: { // [opty, opval, opval, opval] |
3363 | VectorType *RTy = dyn_cast<VectorType>(CurTy); |
3364 | VectorType *OpTy = |
3365 | dyn_cast_or_null<VectorType>(getTypeByID(Record[0])); |
3366 | if (Record.size() < 4 || !RTy || !OpTy) |
3367 | return error("Invalid shufflevector constexpr record"); |
3368 | V = BitcodeConstant::create( |
3369 | Alloc, CurTy, Instruction::ShuffleVector, |
3370 | {(unsigned)Record[1], (unsigned)Record[2], (unsigned)Record[3]}); |
3371 | break; |
3372 | } |
3373 | case bitc::CST_CODE_CE_CMP: { // CE_CMP: [opty, opval, opval, pred] |
3374 | if (Record.size() < 4) |
3375 | return error("Invalid cmp constexpt record"); |
3376 | unsigned OpTyID = Record[0]; |
3377 | Type *OpTy = getTypeByID(OpTyID); |
3378 | if (!OpTy) |
3379 | return error("Invalid cmp constexpr record"); |
3380 | V = BitcodeConstant::create( |
3381 | Alloc, CurTy, |
3382 | {(uint8_t)(OpTy->isFPOrFPVectorTy() ? Instruction::FCmp |
3383 | : Instruction::ICmp), |
3384 | (uint8_t)Record[3]}, |
3385 | {(unsigned)Record[1], (unsigned)Record[2]}); |
3386 | break; |
3387 | } |
3388 | // This maintains backward compatibility, pre-asm dialect keywords. |
3389 | // Deprecated, but still needed to read old bitcode files. |
3390 | case bitc::CST_CODE_INLINEASM_OLD: { |
3391 | if (Record.size() < 2) |
3392 | return error("Invalid inlineasm record"); |
3393 | std::string AsmStr, ConstrStr; |
3394 | bool HasSideEffects = Record[0] & 1; |
3395 | bool IsAlignStack = Record[0] >> 1; |
3396 | unsigned AsmStrSize = Record[1]; |
3397 | if (2+AsmStrSize >= Record.size()) |
3398 | return error("Invalid inlineasm record"); |
3399 | unsigned ConstStrSize = Record[2+AsmStrSize]; |
3400 | if (3+AsmStrSize+ConstStrSize > Record.size()) |
3401 | return error("Invalid inlineasm record"); |
3402 | |
3403 | for (unsigned i = 0; i != AsmStrSize; ++i) |
3404 | AsmStr += (char)Record[2+i]; |
3405 | for (unsigned i = 0; i != ConstStrSize; ++i) |
3406 | ConstrStr += (char)Record[3+AsmStrSize+i]; |
3407 | UpgradeInlineAsmString(&AsmStr); |
3408 | if (!CurElemTy) |
3409 | return error("Missing element type for old-style inlineasm"); |
3410 | V = InlineAsm::get(cast<FunctionType>(CurElemTy), AsmStr, ConstrStr, |
3411 | HasSideEffects, IsAlignStack); |
3412 | break; |
3413 | } |
3414 | // This version adds support for the asm dialect keywords (e.g., |
3415 | // inteldialect). |
3416 | case bitc::CST_CODE_INLINEASM_OLD2: { |
3417 | if (Record.size() < 2) |
3418 | return error("Invalid inlineasm record"); |
3419 | std::string AsmStr, ConstrStr; |
3420 | bool HasSideEffects = Record[0] & 1; |
3421 | bool IsAlignStack = (Record[0] >> 1) & 1; |
3422 | unsigned AsmDialect = Record[0] >> 2; |
3423 | unsigned AsmStrSize = Record[1]; |
3424 | if (2+AsmStrSize >= Record.size()) |
3425 | return error("Invalid inlineasm record"); |
3426 | unsigned ConstStrSize = Record[2+AsmStrSize]; |
3427 | if (3+AsmStrSize+ConstStrSize > Record.size()) |
3428 | return error("Invalid inlineasm record"); |
3429 | |
3430 | for (unsigned i = 0; i != AsmStrSize; ++i) |
3431 | AsmStr += (char)Record[2+i]; |
3432 | for (unsigned i = 0; i != ConstStrSize; ++i) |
3433 | ConstrStr += (char)Record[3+AsmStrSize+i]; |
3434 | UpgradeInlineAsmString(&AsmStr); |
3435 | if (!CurElemTy) |
3436 | return error("Missing element type for old-style inlineasm"); |
3437 | V = InlineAsm::get(cast<FunctionType>(CurElemTy), AsmStr, ConstrStr, |
3438 | HasSideEffects, IsAlignStack, |
3439 | InlineAsm::AsmDialect(AsmDialect)); |
3440 | break; |
3441 | } |
3442 | // This version adds support for the unwind keyword. |
3443 | case bitc::CST_CODE_INLINEASM_OLD3: { |
3444 | if (Record.size() < 2) |
3445 | return error("Invalid inlineasm record"); |
3446 | unsigned OpNum = 0; |
3447 | std::string AsmStr, ConstrStr; |
3448 | bool HasSideEffects = Record[OpNum] & 1; |
3449 | bool IsAlignStack = (Record[OpNum] >> 1) & 1; |
3450 | unsigned AsmDialect = (Record[OpNum] >> 2) & 1; |
3451 | bool CanThrow = (Record[OpNum] >> 3) & 1; |
3452 | ++OpNum; |
3453 | unsigned AsmStrSize = Record[OpNum]; |
3454 | ++OpNum; |
3455 | if (OpNum + AsmStrSize >= Record.size()) |
3456 | return error("Invalid inlineasm record"); |
3457 | unsigned ConstStrSize = Record[OpNum + AsmStrSize]; |
3458 | if (OpNum + 1 + AsmStrSize + ConstStrSize > Record.size()) |
3459 | return error("Invalid inlineasm record"); |
3460 | |
3461 | for (unsigned i = 0; i != AsmStrSize; ++i) |
3462 | AsmStr += (char)Record[OpNum + i]; |
3463 | ++OpNum; |
3464 | for (unsigned i = 0; i != ConstStrSize; ++i) |
3465 | ConstrStr += (char)Record[OpNum + AsmStrSize + i]; |
3466 | UpgradeInlineAsmString(&AsmStr); |
3467 | if (!CurElemTy) |
3468 | return error("Missing element type for old-style inlineasm"); |
3469 | V = InlineAsm::get(cast<FunctionType>(CurElemTy), AsmStr, ConstrStr, |
3470 | HasSideEffects, IsAlignStack, |
3471 | InlineAsm::AsmDialect(AsmDialect), CanThrow); |
3472 | break; |
3473 | } |
3474 | // This version adds explicit function type. |
3475 | case bitc::CST_CODE_INLINEASM: { |
3476 | if (Record.size() < 3) |
3477 | return error("Invalid inlineasm record"); |
3478 | unsigned OpNum = 0; |
3479 | auto *FnTy = dyn_cast_or_null<FunctionType>(getTypeByID(Record[OpNum])); |
3480 | ++OpNum; |
3481 | if (!FnTy) |
3482 | return error("Invalid inlineasm record"); |
3483 | std::string AsmStr, ConstrStr; |
3484 | bool HasSideEffects = Record[OpNum] & 1; |
3485 | bool IsAlignStack = (Record[OpNum] >> 1) & 1; |
3486 | unsigned AsmDialect = (Record[OpNum] >> 2) & 1; |
3487 | bool CanThrow = (Record[OpNum] >> 3) & 1; |
3488 | ++OpNum; |
3489 | unsigned AsmStrSize = Record[OpNum]; |
3490 | ++OpNum; |
3491 | if (OpNum + AsmStrSize >= Record.size()) |
3492 | return error("Invalid inlineasm record"); |
3493 | unsigned ConstStrSize = Record[OpNum + AsmStrSize]; |
3494 | if (OpNum + 1 + AsmStrSize + ConstStrSize > Record.size()) |
3495 | return error("Invalid inlineasm record"); |
3496 | |
3497 | for (unsigned i = 0; i != AsmStrSize; ++i) |
3498 | AsmStr += (char)Record[OpNum + i]; |
3499 | ++OpNum; |
3500 | for (unsigned i = 0; i != ConstStrSize; ++i) |
3501 | ConstrStr += (char)Record[OpNum + AsmStrSize + i]; |
3502 | UpgradeInlineAsmString(&AsmStr); |
3503 | V = InlineAsm::get(FnTy, AsmStr, ConstrStr, HasSideEffects, IsAlignStack, |
3504 | InlineAsm::AsmDialect(AsmDialect), CanThrow); |
3505 | break; |
3506 | } |
3507 | case bitc::CST_CODE_BLOCKADDRESS:{ |
3508 | if (Record.size() < 3) |
3509 | return error("Invalid blockaddress record"); |
3510 | unsigned FnTyID = Record[0]; |
3511 | Type *FnTy = getTypeByID(FnTyID); |
3512 | if (!FnTy) |
3513 | return error("Invalid blockaddress record"); |
3514 | V = BitcodeConstant::create( |
3515 | Alloc, CurTy, |
3516 | {BitcodeConstant::BlockAddressOpcode, 0, (unsigned)Record[2]}, |
3517 | Record[1]); |
3518 | break; |
3519 | } |
3520 | case bitc::CST_CODE_DSO_LOCAL_EQUIVALENT: { |
3521 | if (Record.size() < 2) |
3522 | return error("Invalid dso_local record"); |
3523 | unsigned GVTyID = Record[0]; |
3524 | Type *GVTy = getTypeByID(GVTyID); |
3525 | if (!GVTy) |
3526 | return error("Invalid dso_local record"); |
3527 | V = BitcodeConstant::create( |
3528 | Alloc, CurTy, BitcodeConstant::DSOLocalEquivalentOpcode, Record[1]); |
3529 | break; |
3530 | } |
3531 | case bitc::CST_CODE_NO_CFI_VALUE: { |
3532 | if (Record.size() < 2) |
3533 | return error("Invalid no_cfi record"); |
3534 | unsigned GVTyID = Record[0]; |
3535 | Type *GVTy = getTypeByID(GVTyID); |
3536 | if (!GVTy) |
3537 | return error("Invalid no_cfi record"); |
3538 | V = BitcodeConstant::create(Alloc, CurTy, BitcodeConstant::NoCFIOpcode, |
3539 | Record[1]); |
3540 | break; |
3541 | } |
3542 | } |
3543 | |
3544 | assert(V->getType() == getTypeByID(CurTyID) && "Incorrect result type ID")(static_cast <bool> (V->getType() == getTypeByID(CurTyID ) && "Incorrect result type ID") ? void (0) : __assert_fail ("V->getType() == getTypeByID(CurTyID) && \"Incorrect result type ID\"" , "llvm/lib/Bitcode/Reader/BitcodeReader.cpp", 3544, __extension__ __PRETTY_FUNCTION__)); |
3545 | if (Error Err = ValueList.assignValue(NextCstNo, V, CurTyID)) |
3546 | return Err; |
3547 | ++NextCstNo; |
3548 | } |
3549 | } |
3550 | |
3551 | Error BitcodeReader::parseUseLists() { |
3552 | if (Error Err = Stream.EnterSubBlock(bitc::USELIST_BLOCK_ID)) |
3553 | return Err; |
3554 | |
3555 | // Read all the records. |
3556 | SmallVector<uint64_t, 64> Record; |
3557 | |
3558 | while (true) { |
3559 | Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks(); |
3560 | if (!MaybeEntry) |
3561 | return MaybeEntry.takeError(); |
3562 | BitstreamEntry Entry = MaybeEntry.get(); |
3563 | |
3564 | switch (Entry.Kind) { |
3565 | case BitstreamEntry::SubBlock: // Handled for us already. |
3566 | case BitstreamEntry::Error: |
3567 | return error("Malformed block"); |
3568 | case BitstreamEntry::EndBlock: |
3569 | return Error::success(); |
3570 | case BitstreamEntry::Record: |
3571 | // The interesting case. |
3572 | break; |
3573 | } |
3574 | |
3575 | // Read a use list record. |
3576 | Record.clear(); |
3577 | bool IsBB = false; |
3578 | Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record); |
3579 | if (!MaybeRecord) |
3580 | return MaybeRecord.takeError(); |
3581 | switch (MaybeRecord.get()) { |
3582 | default: // Default behavior: unknown type. |
3583 | break; |
3584 | case bitc::USELIST_CODE_BB: |
3585 | IsBB = true; |
3586 | [[fallthrough]]; |
3587 | case bitc::USELIST_CODE_DEFAULT: { |
3588 | unsigned RecordLength = Record.size(); |
3589 | if (RecordLength < 3) |
3590 | // Records should have at least an ID and two indexes. |
3591 | return error("Invalid record"); |
3592 | unsigned ID = Record.pop_back_val(); |
3593 | |
3594 | Value *V; |
3595 | if (IsBB) { |
3596 | assert(ID < FunctionBBs.size() && "Basic block not found")(static_cast <bool> (ID < FunctionBBs.size() && "Basic block not found") ? void (0) : __assert_fail ("ID < FunctionBBs.size() && \"Basic block not found\"" , "llvm/lib/Bitcode/Reader/BitcodeReader.cpp", 3596, __extension__ __PRETTY_FUNCTION__)); |
3597 | V = FunctionBBs[ID]; |
3598 | } else |
3599 | V = ValueList[ID]; |
3600 | unsigned NumUses = 0; |
3601 | SmallDenseMap<const Use *, unsigned, 16> Order; |
3602 | for (const Use &U : V->materialized_uses()) { |
3603 | if (++NumUses > Record.size()) |
3604 | break; |
3605 | Order[&U] = Record[NumUses - 1]; |
3606 | } |
3607 | if (Order.size() != Record.size() || NumUses > Record.size()) |
3608 | // Mismatches can happen if the functions are being materialized lazily |
3609 | // (out-of-order), or a value has been upgraded. |
3610 | break; |
3611 | |
3612 | V->sortUseList([&](const Use &L, const Use &R) { |
3613 | return Order.lookup(&L) < Order.lookup(&R); |
3614 | }); |
3615 | break; |
3616 | } |
3617 | } |
3618 | } |
3619 | } |
3620 | |
3621 | /// When we see the block for metadata, remember where it is and then skip it. |
3622 | /// This lets us lazily deserialize the metadata. |
3623 | Error BitcodeReader::rememberAndSkipMetadata() { |
3624 | // Save the current stream state. |
3625 | uint64_t CurBit = Stream.GetCurrentBitNo(); |
3626 | DeferredMetadataInfo.push_back(CurBit); |
3627 | |
3628 | // Skip over the block for now. |
3629 | if (Error Err = Stream.SkipBlock()) |
3630 | return Err; |
3631 | return Error::success(); |
3632 | } |
3633 | |
3634 | Error BitcodeReader::materializeMetadata() { |
3635 | for (uint64_t BitPos : DeferredMetadataInfo) { |
3636 | // Move the bit stream to the saved position. |
3637 | if (Error JumpFailed = Stream.JumpToBit(BitPos)) |
3638 | return JumpFailed; |
3639 | if (Error Err = MDLoader->parseModuleMetadata()) |
3640 | return Err; |
3641 | } |
3642 | |
3643 | // Upgrade "Linker Options" module flag to "llvm.linker.options" module-level |
3644 | // metadata. Only upgrade if the new option doesn't exist to avoid upgrade |
3645 | // multiple times. |
3646 | if (!TheModule->getNamedMetadata("llvm.linker.options")) { |
3647 | if (Metadata *Val = TheModule->getModuleFlag("Linker Options")) { |
3648 | NamedMDNode *LinkerOpts = |
3649 | TheModule->getOrInsertNamedMetadata("llvm.linker.options"); |
3650 | for (const MDOperand &MDOptions : cast<MDNode>(Val)->operands()) |
3651 | LinkerOpts->addOperand(cast<MDNode>(MDOptions)); |
3652 | } |
3653 | } |
3654 | |
3655 | DeferredMetadataInfo.clear(); |
3656 | return Error::success(); |
3657 | } |
3658 | |
3659 | void BitcodeReader::setStripDebugInfo() { StripDebugInfo = true; } |
3660 | |
3661 | /// When we see the block for a function body, remember where it is and then |
3662 | /// skip it. This lets us lazily deserialize the functions. |
3663 | Error BitcodeReader::rememberAndSkipFunctionBody() { |
3664 | // Get the function we are talking about. |
3665 | if (FunctionsWithBodies.empty()) |
3666 | return error("Insufficient function protos"); |
3667 | |
3668 | Function *Fn = FunctionsWithBodies.back(); |
3669 | FunctionsWithBodies.pop_back(); |
3670 | |
3671 | // Save the current stream state. |
3672 | uint64_t CurBit = Stream.GetCurrentBitNo(); |
3673 | assert((static_cast <bool> ((DeferredFunctionInfo[Fn] == 0 || DeferredFunctionInfo [Fn] == CurBit) && "Mismatch between VST and scanned function offsets" ) ? void (0) : __assert_fail ("(DeferredFunctionInfo[Fn] == 0 || DeferredFunctionInfo[Fn] == CurBit) && \"Mismatch between VST and scanned function offsets\"" , "llvm/lib/Bitcode/Reader/BitcodeReader.cpp", 3675, __extension__ __PRETTY_FUNCTION__)) |
3674 | (DeferredFunctionInfo[Fn] == 0 || DeferredFunctionInfo[Fn] == CurBit) &&(static_cast <bool> ((DeferredFunctionInfo[Fn] == 0 || DeferredFunctionInfo [Fn] == CurBit) && "Mismatch between VST and scanned function offsets" ) ? void (0) : __assert_fail ("(DeferredFunctionInfo[Fn] == 0 || DeferredFunctionInfo[Fn] == CurBit) && \"Mismatch between VST and scanned function offsets\"" , "llvm/lib/Bitcode/Reader/BitcodeReader.cpp", 3675, __extension__ __PRETTY_FUNCTION__)) |
3675 | "Mismatch between VST and scanned function offsets")(static_cast <bool> ((DeferredFunctionInfo[Fn] == 0 || DeferredFunctionInfo [Fn] == CurBit) && "Mismatch between VST and scanned function offsets" ) ? void (0) : __assert_fail ("(DeferredFunctionInfo[Fn] == 0 || DeferredFunctionInfo[Fn] == CurBit) && \"Mismatch between VST and scanned function offsets\"" , "llvm/lib/Bitcode/Reader/BitcodeReader.cpp", 3675, __extension__ __PRETTY_FUNCTION__)); |
3676 | DeferredFunctionInfo[Fn] = CurBit; |
3677 | |
3678 | // Skip over the function block for now. |
3679 | if (Error Err = Stream.SkipBlock()) |
3680 | return Err; |
3681 | return Error::success(); |
3682 | } |
3683 | |
3684 | Error BitcodeReader::globalCleanup() { |
3685 | // Patch the initializers for globals and aliases up. |
3686 | if (Error Err = resolveGlobalAndIndirectSymbolInits()) |
3687 | return Err; |
3688 | if (!GlobalInits.empty() || !IndirectSymbolInits.empty()) |
3689 | return error("Malformed global initializer set"); |
3690 | |
3691 | // Look for intrinsic functions which need to be upgraded at some point |
3692 | // and functions that need to have their function attributes upgraded. |
3693 | for (Function &F : *TheModule) { |
3694 | MDLoader->upgradeDebugIntrinsics(F); |
3695 | Function *NewFn; |
3696 | if (UpgradeIntrinsicFunction(&F, NewFn)) |
3697 | UpgradedIntrinsics[&F] = NewFn; |
3698 | // Look for functions that rely on old function attribute behavior. |
3699 | UpgradeFunctionAttributes(F); |
3700 | } |
3701 | |
3702 | // Look for global variables which need to be renamed. |
3703 | std::vector<std::pair<GlobalVariable *, GlobalVariable *>> UpgradedVariables; |
3704 | for (GlobalVariable &GV : TheModule->globals()) |
3705 | if (GlobalVariable *Upgraded = UpgradeGlobalVariable(&GV)) |
3706 | UpgradedVariables.emplace_back(&GV, Upgraded); |
3707 | for (auto &Pair : UpgradedVariables) { |
3708 | Pair.first->eraseFromParent(); |
3709 | TheModule->insertGlobalVariable(Pair.second); |
3710 | } |
3711 | |
3712 | // Force deallocation of memory for these vectors to favor the client that |
3713 | // want lazy deserialization. |
3714 | std::vector<std::pair<GlobalVariable *, unsigned>>().swap(GlobalInits); |
3715 | std::vector<std::pair<GlobalValue *, unsigned>>().swap(IndirectSymbolInits); |
3716 | return Error::success(); |
3717 | } |
3718 | |
3719 | /// Support for lazy parsing of function bodies. This is required if we |
3720 | /// either have an old bitcode file without a VST forward declaration record, |
3721 | /// or if we have an anonymous function being materialized, since anonymous |
3722 | /// functions do not have a name and are therefore not in the VST. |
3723 | Error BitcodeReader::rememberAndSkipFunctionBodies() { |
3724 | if (Error JumpFailed = Stream.JumpToBit(NextUnreadBit)) |
3725 | return JumpFailed; |
3726 | |
3727 | if (Stream.AtEndOfStream()) |
3728 | return error("Could not find function in stream"); |
3729 | |
3730 | if (!SeenFirstFunctionBody) |
3731 | return error("Trying to materialize functions before seeing function blocks"); |
3732 | |
3733 | // An old bitcode file with the symbol table at the end would have |
3734 | // finished the parse greedily. |
3735 | assert(SeenValueSymbolTable)(static_cast <bool> (SeenValueSymbolTable) ? void (0) : __assert_fail ("SeenValueSymbolTable", "llvm/lib/Bitcode/Reader/BitcodeReader.cpp" , 3735, __extension__ __PRETTY_FUNCTION__)); |
3736 | |
3737 | SmallVector<uint64_t, 64> Record; |
3738 | |
3739 | while (true) { |
3740 | Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); |
3741 | if (!MaybeEntry) |
3742 | return MaybeEntry.takeError(); |
3743 | llvm::BitstreamEntry Entry = MaybeEntry.get(); |
3744 | |
3745 | switch (Entry.Kind) { |
3746 | default: |
3747 | return error("Expect SubBlock"); |
3748 | case BitstreamEntry::SubBlock: |
3749 | switch (Entry.ID) { |
3750 | default: |
3751 | return error("Expect function block"); |
3752 | case bitc::FUNCTION_BLOCK_ID: |
3753 | if (Error Err = rememberAndSkipFunctionBody()) |
3754 | return Err; |
3755 | NextUnreadBit = Stream.GetCurrentBitNo(); |
3756 | return Error::success(); |
3757 | } |
3758 | } |
3759 | } |
3760 | } |
3761 | |
3762 | Error BitcodeReaderBase::readBlockInfo() { |
3763 | Expected<std::optional<BitstreamBlockInfo>> MaybeNewBlockInfo = |
3764 | Stream.ReadBlockInfoBlock(); |
3765 | if (!MaybeNewBlockInfo) |
3766 | return MaybeNewBlockInfo.takeError(); |
3767 | std::optional<BitstreamBlockInfo> NewBlockInfo = |
3768 | std::move(MaybeNewBlockInfo.get()); |
3769 | if (!NewBlockInfo) |
3770 | return error("Malformed block"); |
3771 | BlockInfo = std::move(*NewBlockInfo); |
3772 | return Error::success(); |
3773 | } |
3774 | |
3775 | Error BitcodeReader::parseComdatRecord(ArrayRef<uint64_t> Record) { |
3776 | // v1: [selection_kind, name] |
3777 | // v2: [strtab_offset, strtab_size, selection_kind] |
3778 | StringRef Name; |
3779 | std::tie(Name, Record) = readNameFromStrtab(Record); |
3780 | |
3781 | if (Record.empty()) |
3782 | return error("Invalid record"); |
3783 | Comdat::SelectionKind SK = getDecodedComdatSelectionKind(Record[0]); |
3784 | std::string OldFormatName; |
3785 | if (!UseStrtab) { |
3786 | if (Record.size() < 2) |
3787 | return error("Invalid record"); |
3788 | unsigned ComdatNameSize = Record[1]; |
3789 | if (ComdatNameSize > Record.size() - 2) |
3790 | return error("Comdat name size too large"); |
3791 | OldFormatName.reserve(ComdatNameSize); |
3792 | for (unsigned i = 0; i != ComdatNameSize; ++i) |
3793 | OldFormatName += (char)Record[2 + i]; |
3794 | Name = OldFormatName; |
3795 | } |
3796 | Comdat *C = TheModule->getOrInsertComdat(Name); |
3797 | C->setSelectionKind(SK); |
3798 | ComdatList.push_back(C); |
3799 | return Error::success(); |
3800 | } |
3801 | |
3802 | static void inferDSOLocal(GlobalValue *GV) { |
3803 | // infer dso_local from linkage and visibility if it is not encoded. |
3804 | if (GV->hasLocalLinkage() || |
3805 | (!GV->hasDefaultVisibility() && !GV->hasExternalWeakLinkage())) |
3806 | GV->setDSOLocal(true); |
3807 | } |
3808 | |
3809 | GlobalValue::SanitizerMetadata deserializeSanitizerMetadata(unsigned V) { |
3810 | GlobalValue::SanitizerMetadata Meta; |
3811 | if (V & (1 << 0)) |
3812 | Meta.NoAddress = true; |
3813 | if (V & (1 << 1)) |
3814 | Meta.NoHWAddress = true; |
3815 | if (V & (1 << 2)) |
3816 | Meta.Memtag = true; |
3817 | if (V & (1 << 3)) |
3818 | Meta.IsDynInit = true; |
3819 | return Meta; |
3820 | } |
3821 | |
3822 | Error BitcodeReader::parseGlobalVarRecord(ArrayRef<uint64_t> Record) { |
3823 | // v1: [pointer type, isconst, initid, linkage, alignment, section, |
3824 | // visibility, threadlocal, unnamed_addr, externally_initialized, |
3825 | // dllstorageclass, comdat, attributes, preemption specifier, |
3826 | // partition strtab offset, partition strtab size] (name in VST) |
3827 | // v2: [strtab_offset, strtab_size, v1] |
3828 | StringRef Name; |
3829 | std::tie(Name, Record) = readNameFromStrtab(Record); |
3830 | |
3831 | if (Record.size() < 6) |
3832 | return error("Invalid record"); |
3833 | unsigned TyID = Record[0]; |
3834 | Type *Ty = getTypeByID(TyID); |
3835 | if (!Ty) |
3836 | return error("Invalid record"); |
3837 | bool isConstant = Record[1] & 1; |
3838 | bool explicitType = Record[1] & 2; |
3839 | unsigned AddressSpace; |
3840 | if (explicitType) { |
3841 | AddressSpace = Record[1] >> 2; |
3842 | } else { |
3843 | if (!Ty->isPointerTy()) |
3844 | return error("Invalid type for value"); |
3845 | AddressSpace = cast<PointerType>(Ty)->getAddressSpace(); |
3846 | TyID = getContainedTypeID(TyID); |
3847 | Ty = getTypeByID(TyID); |
3848 | if (!Ty) |
3849 | return error("Missing element type for old-style global"); |
3850 | } |
3851 | |
3852 | uint64_t RawLinkage = Record[3]; |
3853 | GlobalValue::LinkageTypes Linkage = getDecodedLinkage(RawLinkage); |
3854 | MaybeAlign Alignment; |
3855 | if (Error Err = parseAlignmentValue(Record[4], Alignment)) |
3856 | return Err; |
3857 | std::string Section; |
3858 | if (Record[5]) { |
3859 | if (Record[5] - 1 >= SectionTable.size()) |
3860 | return error("Invalid ID"); |
3861 | Section = SectionTable[Record[5] - 1]; |
3862 | } |
3863 | GlobalValue::VisibilityTypes Visibility = GlobalValue::DefaultVisibility; |
3864 | // Local linkage must have default visibility. |
3865 | // auto-upgrade `hidden` and `protected` for old bitcode. |
3866 | if (Record.size() > 6 && !GlobalValue::isLocalLinkage(Linkage)) |
3867 | Visibility = getDecodedVisibility(Record[6]); |
3868 | |
3869 | GlobalVariable::ThreadLocalMode TLM = GlobalVariable::NotThreadLocal; |
3870 | if (Record.size() > 7) |
3871 | TLM = getDecodedThreadLocalMode(Record[7]); |
3872 | |
3873 | GlobalValue::UnnamedAddr UnnamedAddr = GlobalValue::UnnamedAddr::None; |
3874 | if (Record.size() > 8) |
3875 | UnnamedAddr = getDecodedUnnamedAddrType(Record[8]); |
3876 | |
3877 | bool ExternallyInitialized = false; |
3878 | if (Record.size() > 9) |
3879 | ExternallyInitialized = Record[9]; |
3880 | |
3881 | GlobalVariable *NewGV = |
3882 | new GlobalVariable(*TheModule, Ty, isConstant, Linkage, nullptr, Name, |
3883 | nullptr, TLM, AddressSpace, ExternallyInitialized); |
3884 | if (Alignment) |
3885 | NewGV->setAlignment(*Alignment); |
3886 | if (!Section.empty()) |
3887 | NewGV->setSection(Section); |
3888 | NewGV->setVisibility(Visibility); |
3889 | NewGV->setUnnamedAddr(UnnamedAddr); |
3890 | |
3891 | if (Record.size() > 10) { |
3892 | // A GlobalValue with local linkage cannot have a DLL storage class. |
3893 | if (!NewGV->hasLocalLinkage()) { |
3894 | NewGV->setDLLStorageClass(getDecodedDLLStorageClass(Record[10])); |
3895 | } |
3896 | } else { |
3897 | upgradeDLLImportExportLinkage(NewGV, RawLinkage); |
3898 | } |
3899 | |
3900 | ValueList.push_back(NewGV, getVirtualTypeID(NewGV->getType(), TyID)); |
3901 | |
3902 | // Remember which value to use for the global initializer. |
3903 | if (unsigned InitID = Record[2]) |
3904 | GlobalInits.push_back(std::make_pair(NewGV, InitID - 1)); |
3905 | |
3906 | if (Record.size() > 11) { |
3907 | if (unsigned ComdatID = Record[11]) { |
3908 | if (ComdatID > ComdatList.size()) |
3909 | return error("Invalid global variable comdat ID"); |
3910 | NewGV->setComdat(ComdatList[ComdatID - 1]); |
3911 | } |
3912 | } else if (hasImplicitComdat(RawLinkage)) { |
3913 | ImplicitComdatObjects.insert(NewGV); |
3914 | } |
3915 | |
3916 | if (Record.size() > 12) { |
3917 | auto AS = getAttributes(Record[12]).getFnAttrs(); |
3918 | NewGV->setAttributes(AS); |
3919 | } |
3920 | |
3921 | if (Record.size() > 13) { |
3922 | NewGV->setDSOLocal(getDecodedDSOLocal(Record[13])); |
3923 | } |
3924 | inferDSOLocal(NewGV); |
3925 | |
3926 | // Check whether we have enough values to read a partition name. |
3927 | if (Record.size() > 15) |
3928 | NewGV->setPartition(StringRef(Strtab.data() + Record[14], Record[15])); |
3929 | |
3930 | if (Record.size() > 16 && Record[16]) { |
3931 | llvm::GlobalValue::SanitizerMetadata Meta = |
3932 | deserializeSanitizerMetadata(Record[16]); |
3933 | NewGV->setSanitizerMetadata(Meta); |
3934 | } |
3935 | |
3936 | return Error::success(); |
3937 | } |
3938 | |
3939 | void BitcodeReader::callValueTypeCallback(Value *F, unsigned TypeID) { |
3940 | if (ValueTypeCallback) { |
3941 | (*ValueTypeCallback)( |
3942 | F, TypeID, [this](unsigned I) { return getTypeByID(I); }, |
3943 | [this](unsigned I, unsigned J) { return getContainedTypeID(I, J); }); |
3944 | } |
3945 | } |
3946 | |
3947 | Error BitcodeReader::parseFunctionRecord(ArrayRef<uint64_t> Record) { |
3948 | // v1: [type, callingconv, isproto, linkage, paramattr, alignment, section, |
3949 | // visibility, gc, unnamed_addr, prologuedata, dllstorageclass, comdat, |
3950 | // prefixdata, personalityfn, preemption specifier, addrspace] (name in VST) |
3951 | // v2: [strtab_offset, strtab_size, v1] |
3952 | StringRef Name; |
3953 | std::tie(Name, Record) = readNameFromStrtab(Record); |
3954 | |
3955 | if (Record.size() < 8) |
3956 | return error("Invalid record"); |
3957 | unsigned FTyID = Record[0]; |
3958 | Type *FTy = getTypeByID(FTyID); |
3959 | if (!FTy) |
3960 | return error("Invalid record"); |
3961 | if (isa<PointerType>(FTy)) { |
3962 | FTyID = getContainedTypeID(FTyID, 0); |
3963 | FTy = getTypeByID(FTyID); |
3964 | if (!FTy) |
3965 | return error("Missing element type for old-style function"); |
3966 | } |
3967 | |
3968 | if (!isa<FunctionType>(FTy)) |
3969 | return error("Invalid type for value"); |
3970 | auto CC = static_cast<CallingConv::ID>(Record[1]); |
3971 | if (CC & ~CallingConv::MaxID) |
3972 | return error("Invalid calling convention ID"); |
3973 | |
3974 | unsigned AddrSpace = TheModule->getDataLayout().getProgramAddressSpace(); |
3975 | if (Record.size() > 16) |
3976 | AddrSpace = Record[16]; |
3977 | |
3978 | Function *Func = |
3979 | Function::Create(cast<FunctionType>(FTy), GlobalValue::ExternalLinkage, |
3980 | AddrSpace, Name, TheModule); |
3981 | |
3982 | assert(Func->getFunctionType() == FTy &&(static_cast <bool> (Func->getFunctionType() == FTy && "Incorrect fully specified type provided for function") ? void (0) : __assert_fail ("Func->getFunctionType() == FTy && \"Incorrect fully specified type provided for function\"" , "llvm/lib/Bitcode/Reader/BitcodeReader.cpp", 3983, __extension__ __PRETTY_FUNCTION__)) |
3983 | "Incorrect fully specified type provided for function")(static_cast <bool> (Func->getFunctionType() == FTy && "Incorrect fully specified type provided for function") ? void (0) : __assert_fail ("Func->getFunctionType() == FTy && \"Incorrect fully specified type provided for function\"" , "llvm/lib/Bitcode/Reader/BitcodeReader.cpp", 3983, __extension__ __PRETTY_FUNCTION__)); |
3984 | FunctionTypeIDs[Func] = FTyID; |
3985 | |
3986 | Func->setCallingConv(CC); |
3987 | bool isProto = Record[2]; |
3988 | uint64_t RawLinkage = Record[3]; |
3989 | Func->setLinkage(getDecodedLinkage(RawLinkage)); |
3990 | Func->setAttributes(getAttributes(Record[4])); |
3991 | callValueTypeCallback(Func, FTyID); |
3992 | |
3993 | // Upgrade any old-style byval or sret without a type by propagating the |
3994 | // argument's pointee type. There should be no opaque pointers where the byval |
3995 | // type is implicit. |
3996 | for (unsigned i = 0; i != Func->arg_size(); ++i) { |
3997 | for (Attribute::AttrKind Kind : {Attribute::ByVal, Attribute::StructRet, |
3998 | Attribute::InAlloca}) { |
3999 | if (!Func->hasParamAttribute(i, Kind)) |
4000 | continue; |
4001 | |
4002 | if (Func->getParamAttribute(i, Kind).getValueAsType()) |
4003 | continue; |
4004 | |
4005 | Func->removeParamAttr(i, Kind); |
4006 | |
4007 | unsigned ParamTypeID = getContainedTypeID(FTyID, i + 1); |
4008 | Type *PtrEltTy = getPtrElementTypeByID(ParamTypeID); |
4009 | if (!PtrEltTy) |
4010 | return error("Missing param element type for attribute upgrade"); |
4011 | |
4012 | Attribute NewAttr; |
4013 | switch (Kind) { |
4014 | case Attribute::ByVal: |
4015 | NewAttr = Attribute::getWithByValType(Context, PtrEltTy); |
4016 | break; |
4017 | case Attribute::StructRet: |
4018 | NewAttr = Attribute::getWithStructRetType(Context, PtrEltTy); |
4019 | break; |
4020 | case Attribute::InAlloca: |
4021 | NewAttr = Attribute::getWithInAllocaType(Context, PtrEltTy); |
4022 | break; |
4023 | default: |
4024 | llvm_unreachable("not an upgraded type attribute")::llvm::llvm_unreachable_internal("not an upgraded type attribute" , "llvm/lib/Bitcode/Reader/BitcodeReader.cpp", 4024); |
4025 | } |
4026 | |
4027 | Func->addParamAttr(i, NewAttr); |
4028 | } |
4029 | } |
4030 | |
4031 | if (Func->getCallingConv() == CallingConv::X86_INTR && |
4032 | !Func->arg_empty() && !Func->hasParamAttribute(0, Attribute::ByVal)) { |
4033 | unsigned ParamTypeID = getContainedTypeID(FTyID, 1); |
4034 | Type *ByValTy = getPtrElementTypeByID(ParamTypeID); |
4035 | if (!ByValTy) |
4036 | return error("Missing param element type for x86_intrcc upgrade"); |
4037 | Attribute NewAttr = Attribute::getWithByValType(Context, ByValTy); |
4038 | Func->addParamAttr(0, NewAttr); |
4039 | } |
4040 | |
4041 | MaybeAlign Alignment; |
4042 | if (Error Err = parseAlignmentValue(Record[5], Alignment)) |
4043 | return Err; |
4044 | if (Alignment) |
4045 | Func->setAlignment(*Alignment); |
4046 | if (Record[6]) { |
4047 | if (Record[6] - 1 >= SectionTable.size()) |
4048 | return error("Invalid ID"); |
4049 | Func->setSection(SectionTable[Record[6] - 1]); |
4050 | } |
4051 | // Local linkage must have default visibility. |
4052 | // auto-upgrade `hidden` and `protected` for old bitcode. |
4053 | if (!Func->hasLocalLinkage()) |
4054 | Func->setVisibility(getDecodedVisibility(Record[7])); |
4055 | if (Record.size() > 8 && Record[8]) { |
4056 | if (Record[8] - 1 >= GCTable.size()) |
4057 | return error("Invalid ID"); |
4058 | Func->setGC(GCTable[Record[8] - 1]); |
4059 | } |
4060 | GlobalValue::UnnamedAddr UnnamedAddr = GlobalValue::UnnamedAddr::None; |
4061 | if (Record.size() > 9) |
4062 | UnnamedAddr = getDecodedUnnamedAddrType(Record[9]); |
4063 | Func->setUnnamedAddr(UnnamedAddr); |
4064 | |
4065 | FunctionOperandInfo OperandInfo = {Func, 0, 0, 0}; |
4066 | if (Record.size() > 10) |
4067 | OperandInfo.Prologue = Record[10]; |
4068 | |
4069 | if (Record.size() > 11) { |
4070 | // A GlobalValue with local linkage cannot have a DLL storage class. |
4071 | if (!Func->hasLocalLinkage()) { |
4072 | Func->setDLLStorageClass(getDecodedDLLStorageClass(Record[11])); |
4073 | } |
4074 | } else { |
4075 | upgradeDLLImportExportLinkage(Func, RawLinkage); |
4076 | } |
4077 | |
4078 | if (Record.size() > 12) { |
4079 | if (unsigned ComdatID = Record[12]) { |
4080 | if (ComdatID > ComdatList.size()) |
4081 | return error("Invalid function comdat ID"); |
4082 | Func->setComdat(ComdatList[ComdatID - 1]); |
4083 | } |
4084 | } else if (hasImplicitComdat(RawLinkage)) { |
4085 | ImplicitComdatObjects.insert(Func); |
4086 | } |
4087 | |
4088 | if (Record.size() > 13) |
4089 | OperandInfo.Prefix = Record[13]; |
4090 | |
4091 | if (Record.size() > 14) |
4092 | OperandInfo.PersonalityFn = Record[14]; |
4093 | |
4094 | if (Record.size() > 15) { |
4095 | Func->setDSOLocal(getDecodedDSOLocal(Record[15])); |
4096 | } |
4097 | inferDSOLocal(Func); |
4098 | |
4099 | // Record[16] is the address space number. |
4100 | |
4101 | // Check whether we have enough values to read a partition name. Also make |
4102 | // sure Strtab has enough values. |
4103 | if (Record.size() > 18 && Strtab.data() && |
4104 | Record[17] + Record[18] <= Strtab.size()) { |
4105 | Func->setPartition(StringRef(Strtab.data() + Record[17], Record[18])); |
4106 | } |
4107 | |
4108 | ValueList.push_back(Func, getVirtualTypeID(Func->getType(), FTyID)); |
4109 | |
4110 | if (OperandInfo.PersonalityFn || OperandInfo.Prefix || OperandInfo.Prologue) |
4111 | FunctionOperands.push_back(OperandInfo); |
4112 | |
4113 | // If this is a function with a body, remember the prototype we are |
4114 | // creating now, so that we can match up the body with them later. |
4115 | if (!isProto) { |
4116 | Func->setIsMaterializable(true); |
4117 | FunctionsWithBodies.push_back(Func); |
4118 | DeferredFunctionInfo[Func] = 0; |
4119 | } |
4120 | return Error::success(); |
4121 | } |
4122 | |
4123 | Error BitcodeReader::parseGlobalIndirectSymbolRecord( |
4124 | unsigned BitCode, ArrayRef<uint64_t> Record) { |
4125 | // v1 ALIAS_OLD: [alias type, aliasee val#, linkage] (name in VST) |
4126 | // v1 ALIAS: [alias type, addrspace, aliasee val#, linkage, visibility, |
4127 | // dllstorageclass, threadlocal, unnamed_addr, |
4128 | // preemption specifier] (name in VST) |
4129 | // v1 IFUNC: [alias type, addrspace, aliasee val#, linkage, |
4130 | // visibility, dllstorageclass, threadlocal, unnamed_addr, |
4131 | // preemption specifier] (name in VST) |
4132 | // v2: [strtab_offset, strtab_size, v1] |
4133 | StringRef Name; |
4134 | std::tie(Name, Record) = readNameFromStrtab(Record); |
4135 | |
4136 | bool NewRecord = BitCode != bitc::MODULE_CODE_ALIAS_OLD; |
4137 | if (Record.size() < (3 + (unsigned)NewRecord)) |
4138 | return error("Invalid record"); |
4139 | unsigned OpNum = 0; |
4140 | unsigned TypeID = Record[OpNum++]; |
4141 | Type *Ty = getTypeByID(TypeID); |
4142 | if (!Ty) |
4143 | return error("Invalid record"); |
4144 | |
4145 | unsigned AddrSpace; |
4146 | if (!NewRecord) { |
4147 | auto *PTy = dyn_cast<PointerType>(Ty); |
4148 | if (!PTy) |
4149 | return error("Invalid type for value"); |
4150 | AddrSpace = PTy->getAddressSpace(); |
4151 | TypeID = getContainedTypeID(TypeID); |
4152 | Ty = getTypeByID(TypeID); |
4153 | if (!Ty) |
4154 | return error("Missing element type for old-style indirect symbol"); |
4155 | } else { |
4156 | AddrSpace = Record[OpNum++]; |
4157 | } |
4158 | |
4159 | auto Val = Record[OpNum++]; |
4160 | auto Linkage = Record[OpNum++]; |
4161 | GlobalValue *NewGA; |
4162 | if (BitCode == bitc::MODULE_CODE_ALIAS || |
4163 | BitCode == bitc::MODULE_CODE_ALIAS_OLD) |
4164 | NewGA = GlobalAlias::create(Ty, AddrSpace, getDecodedLinkage(Linkage), Name, |
4165 | TheModule); |
4166 | else |
4167 | NewGA = GlobalIFunc::create(Ty, AddrSpace, getDecodedLinkage(Linkage), Name, |
4168 | nullptr, TheModule); |
4169 | |
4170 | // Local linkage must have default visibility. |
4171 | // auto-upgrade `hidden` and `protected` for old bitcode. |
4172 | if (OpNum != Record.size()) { |
4173 | auto VisInd = OpNum++; |
4174 | if (!NewGA->hasLocalLinkage()) |
4175 | NewGA->setVisibility(getDecodedVisibility(Record[VisInd])); |
4176 | } |
4177 | if (BitCode == bitc::MODULE_CODE_ALIAS || |
4178 | BitCode == bitc::MODULE_CODE_ALIAS_OLD) { |
4179 | if (OpNum != Record.size()) { |
4180 | auto S = Record[OpNum++]; |
4181 | // A GlobalValue with local linkage cannot have a DLL storage class. |
4182 | if (!NewGA->hasLocalLinkage()) |
4183 | NewGA->setDLLStorageClass(getDecodedDLLStorageClass(S)); |
4184 | } |
4185 | else |
4186 | upgradeDLLImportExportLinkage(NewGA, Linkage); |
4187 | if (OpNum != Record.size()) |
4188 | NewGA->setThreadLocalMode(getDecodedThreadLocalMode(Record[OpNum++])); |
4189 | if (OpNum != Record.size()) |
4190 | NewGA->setUnnamedAddr(getDecodedUnnamedAddrType(Record[OpNum++])); |
4191 | } |
4192 | if (OpNum != Record.size()) |
4193 | NewGA->setDSOLocal(getDecodedDSOLocal(Record[OpNum++])); |
4194 | inferDSOLocal(NewGA); |
4195 | |
4196 | // Check whether we have enough values to read a partition name. |
4197 | if (OpNum + 1 < Record.size()) { |
4198 | NewGA->setPartition( |
4199 | StringRef(Strtab.data() + Record[OpNum], Record[OpNum + 1])); |
4200 | OpNum += 2; |
Value stored to 'OpNum' is never read | |
4201 | } |
4202 | |
4203 | ValueList.push_back(NewGA, getVirtualTypeID(NewGA->getType(), TypeID)); |
4204 | IndirectSymbolInits.push_back(std::make_pair(NewGA, Val)); |
4205 | return Error::success(); |
4206 | } |
4207 | |
4208 | Error BitcodeReader::parseModule(uint64_t ResumeBit, |
4209 | bool ShouldLazyLoadMetadata, |
4210 | ParserCallbacks Callbacks) { |
4211 | this->ValueTypeCallback = std::move(Callbacks.ValueType); |
4212 | if (ResumeBit) { |
4213 | if (Error JumpFailed = Stream.JumpToBit(ResumeBit)) |
4214 | return JumpFailed; |
4215 | } else if (Error Err = Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID)) |
4216 | return Err; |
4217 | |
4218 | SmallVector<uint64_t, 64> Record; |
4219 | |
4220 | // Parts of bitcode parsing depend on the datalayout. Make sure we |
4221 | // finalize the datalayout before we run any of that code. |
4222 | bool ResolvedDataLayout = false; |
4223 | // In order to support importing modules with illegal data layout strings, |
4224 | // delay parsing the data layout string until after upgrades and overrides |
4225 | // have been applied, allowing to fix illegal data layout strings. |
4226 | // Initialize to the current module's layout string in case none is specified. |
4227 | std::string TentativeDataLayoutStr = TheModule->getDataLayoutStr(); |
4228 | |
4229 | auto ResolveDataLayout = [&]() -> Error { |
4230 | if (ResolvedDataLayout) |
4231 | return Error::success(); |
4232 | |
4233 | // Datalayout and triple can't be parsed after this point. |
4234 | ResolvedDataLayout = true; |
4235 | |
4236 | // Auto-upgrade the layout string |
4237 | TentativeDataLayoutStr = llvm::UpgradeDataLayoutString( |
4238 | TentativeDataLayoutStr, TheModule->getTargetTriple()); |
4239 | |
4240 | // Apply override |
4241 | if (Callbacks.DataLayout) { |
4242 | if (auto LayoutOverride = (*Callbacks.DataLayout)( |
4243 | TheModule->getTargetTriple(), TentativeDataLayoutStr)) |
4244 | TentativeDataLayoutStr = *LayoutOverride; |
4245 | } |
4246 | |
4247 | // Now the layout string is finalized in TentativeDataLayoutStr. Parse it. |
4248 | Expected<DataLayout> MaybeDL = DataLayout::parse(TentativeDataLayoutStr); |
4249 | if (!MaybeDL) |
4250 | return MaybeDL.takeError(); |
4251 | |
4252 | TheModule->setDataLayout(MaybeDL.get()); |
4253 | return Error::success(); |
4254 | }; |
4255 | |
4256 | // Read all the records for this module. |
4257 | while (true) { |
4258 | Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); |
4259 | if (!MaybeEntry) |
4260 | return MaybeEntry.takeError(); |
4261 | llvm::BitstreamEntry Entry = MaybeEntry.get(); |
4262 | |
4263 | switch (Entry.Kind) { |
4264 | case BitstreamEntry::Error: |
4265 | return error("Malformed block"); |
4266 | case BitstreamEntry::EndBlock: |
4267 | if (Error Err = ResolveDataLayout()) |
4268 | return Err; |
4269 | return globalCleanup(); |
4270 | |
4271 | case BitstreamEntry::SubBlock: |
4272 | switch (Entry.ID) { |
4273 | default: // Skip unknown content. |
4274 | if (Error Err = Stream.SkipBlock()) |
4275 | return Err; |
4276 | break; |
4277 | case bitc::BLOCKINFO_BLOCK_ID: |
4278 | if (Error Err = readBlockInfo()) |
4279 | return Err; |
4280 | break; |
4281 | case bitc::PARAMATTR_BLOCK_ID: |
4282 | if (Error Err = parseAttributeBlock()) |
4283 | return Err; |
4284 | break; |
4285 | case bitc::PARAMATTR_GROUP_BLOCK_ID: |
4286 | if (Error Err = parseAttributeGroupBlock()) |
4287 | return Err; |
4288 | break; |
4289 | case bitc::TYPE_BLOCK_ID_NEW: |
4290 | if (Error Err = parseTypeTable()) |
4291 | return Err; |
4292 | break; |
4293 | case bitc::VALUE_SYMTAB_BLOCK_ID: |
4294 | if (!SeenValueSymbolTable) { |
4295 | // Either this is an old form VST without function index and an |
4296 | // associated VST forward declaration record (which would have caused |
4297 | // the VST to be jumped to and parsed before it was encountered |
4298 | // normally in the stream), or there were no function blocks to |
4299 | // trigger an earlier parsing of the VST. |
4300 | assert(VSTOffset == 0 || FunctionsWithBodies.empty())(static_cast <bool> (VSTOffset == 0 || FunctionsWithBodies .empty()) ? void (0) : __assert_fail ("VSTOffset == 0 || FunctionsWithBodies.empty()" , "llvm/lib/Bitcode/Reader/BitcodeReader.cpp", 4300, __extension__ __PRETTY_FUNCTION__)); |
4301 | if (Error Err = parseValueSymbolTable()) |
4302 | return Err; |
4303 | SeenValueSymbolTable = true; |
4304 | } else { |
4305 | // We must have had a VST forward declaration record, which caused |
4306 | // the parser to jump to and parse the VST earlier. |
4307 | assert(VSTOffset > 0)(static_cast <bool> (VSTOffset > 0) ? void (0) : __assert_fail ("VSTOffset > 0", "llvm/lib/Bitcode/Reader/BitcodeReader.cpp" , 4307, __extension__ __PRETTY_FUNCTION__)); |
4308 | if (Error Err = Stream.SkipBlock()) |
4309 | return Err; |
4310 | } |
4311 | break; |
4312 | case bitc::CONSTANTS_BLOCK_ID: |
4313 | if (Error Err = parseConstants()) |
4314 | return Err; |
4315 | if (Error Err = resolveGlobalAndIndirectSymbolInits()) |
4316 | return Err; |
4317 | break; |
4318 | case bitc::METADATA_BLOCK_ID: |
4319 | if (ShouldLazyLoadMetadata) { |
4320 | if (Error Err = rememberAndSkipMetadata()) |
4321 | return Err; |
4322 | break; |
4323 | } |
4324 | assert(DeferredMetadataInfo.empty() && "Unexpected deferred metadata")(static_cast <bool> (DeferredMetadataInfo.empty() && "Unexpected deferred metadata") ? void (0) : __assert_fail ( "DeferredMetadataInfo.empty() && \"Unexpected deferred metadata\"" , "llvm/lib/Bitcode/Reader/BitcodeReader.cpp", 4324, __extension__ __PRETTY_FUNCTION__)); |
4325 | if (Error Err = MDLoader->parseModuleMetadata()) |
4326 | return Err; |
4327 | break; |
4328 | case bitc::METADATA_KIND_BLOCK_ID: |
4329 | if (Error Err = MDLoader->parseMetadataKinds()) |
4330 | return Err; |
4331 | break; |
4332 | case bitc::FUNCTION_BLOCK_ID: |
4333 | if (Error Err = ResolveDataLayout()) |
4334 | return Err; |
4335 | |
4336 | // If this is the first function body we've seen, reverse the |
4337 | // FunctionsWithBodies list. |
4338 | if (!SeenFirstFunctionBody) { |
4339 | std::reverse(FunctionsWithBodies.begin(), FunctionsWithBodies.end()); |
4340 | if (Error Err = globalCleanup()) |
4341 | return Err; |
4342 | SeenFirstFunctionBody = true; |
4343 | } |
4344 | |
4345 | if (VSTOffset > 0) { |
4346 | // If we have a VST forward declaration record, make sure we |
4347 | // parse the VST now if we haven't already. It is needed to |
4348 | // set up the DeferredFunctionInfo vector for lazy reading. |
4349 | if (!SeenValueSymbolTable) { |
4350 | if (Error Err = BitcodeReader::parseValueSymbolTable(VSTOffset)) |
4351 | return Err; |
4352 | SeenValueSymbolTable = true; |
4353 | // Fall through so that we record the NextUnreadBit below. |
4354 | // This is necessary in case we have an anonymous function that |
4355 | // is later materialized. Since it will not have a VST entry we |
4356 | // need to fall back to the lazy parse to find its offset. |
4357 | } else { |
4358 | // If we have a VST forward declaration record, but have already |
4359 | // parsed the VST (just above, when the first function body was |
4360 | // encountered here), then we are resuming the parse after |
4361 | // materializing functions. The ResumeBit points to the |
4362 | // start of the last function block recorded in the |
4363 | // DeferredFunctionInfo map. Skip it. |
4364 | if (Error Err = Stream.SkipBlock()) |
4365 | return Err; |
4366 | continue; |
4367 | } |
4368 | } |
4369 | |
4370 | // Support older bitcode files that did not have the function |
4371 | // index in the VST, nor a VST forward declaration record, as |
4372 | // well as anonymous functions that do not have VST entries. |
4373 | // Build the DeferredFunctionInfo vector on the fly. |
4374 | if (Error Err = rememberAndSkipFunctionBody()) |
4375 | return Err; |
4376 | |
4377 | // Suspend parsing when we reach the function bodies. Subsequent |
4378 | // materialization calls will resume it when necessary. If the bitcode |
4379 | // file is old, the symbol table will be at the end instead and will not |
4380 | // have been seen yet. In this case, just finish the parse now. |
4381 | if (SeenValueSymbolTable) { |
4382 | NextUnreadBit = Stream.GetCurrentBitNo(); |
4383 | // After the VST has been parsed, we need to make sure intrinsic name |
4384 | // are auto-upgraded. |
4385 | return globalCleanup(); |
4386 | } |
4387 | break; |
4388 | case bitc::USELIST_BLOCK_ID: |
4389 | if (Error Err = parseUseLists()) |
4390 | return Err; |
4391 | break; |
4392 | case bitc::OPERAND_BUNDLE_TAGS_BLOCK_ID: |
4393 | if (Error Err = parseOperandBundleTags()) |
4394 | return Err; |
4395 | break; |
4396 | case bitc::SYNC_SCOPE_NAMES_BLOCK_ID: |
4397 | if (Error Err = parseSyncScopeNames()) |
4398 | return Err; |
4399 | break; |
4400 | } |
4401 | continue; |
4402 | |
4403 | case BitstreamEntry::Record: |
4404 | // The interesting case. |
4405 | break; |
4406 | } |
4407 | |
4408 | // Read a record. |
4409 | Expected<unsigned> MaybeBitCode = Stream.readRecord(Entry.ID, Record); |
4410 | if (!MaybeBitCode) |
4411 | return MaybeBitCode.takeError(); |
4412 | switch (unsigned BitCode = MaybeBitCode.get()) { |
4413 | default: break; // Default behavior, ignore unknown content. |
4414 | case bitc::MODULE_CODE_VERSION: { |
4415 | Expected<unsigned> VersionOrErr = parseVersionRecord(Record); |
4416 | if (!VersionOrErr) |
4417 | return VersionOrErr.takeError(); |
4418 | UseRelativeIDs = *VersionOrErr >= 1; |
4419 | break; |
4420 | } |
4421 | case bitc::MODULE_CODE_TRIPLE: { // TRIPLE: [strchr x N] |
4422 | if (ResolvedDataLayout) |
4423 | return error("target triple too late in module"); |
4424 | std::string S; |
4425 | if (convertToString(Record, 0, S)) |
4426 | return error("Invalid record"); |
4427 | TheModule->setTargetTriple(S); |
4428 | break; |
4429 | } |
4430 | case bitc::MODULE_CODE_DATALAYOUT: { // DATALAYOUT: [strchr x N] |
4431 | if (ResolvedDataLayout) |
4432 | return error("datalayout too late in module"); |
4433 | if (convertToString(Record, 0, TentativeDataLayoutStr)) |
4434 | return error("Invalid record"); |
4435 | break; |
4436 | } |
4437 | case bitc::MODULE_CODE_ASM: { // ASM: [strchr x N] |
4438 | std::string S; |
4439 | if (convertToString(Record, 0, S)) |
4440 | return error("Invalid record"); |
4441 | TheModule->setModuleInlineAsm(S); |
4442 | break; |
4443 | } |
4444 | case bitc::MODULE_CODE_DEPLIB: { // DEPLIB: [strchr x N] |
4445 | // Deprecated, but still needed to read old bitcode files. |
4446 | std::string S; |
4447 | if (convertToString(Record, 0, S)) |
4448 | return error("Invalid record"); |
4449 | // Ignore value. |
4450 | break; |
4451 | } |
4452 | case bitc::MODULE_CODE_SECTIONNAME: { // SECTIONNAME: [strchr x N] |
4453 | std::string S; |
4454 | if (convertToString(Record, 0, S)) |
4455 | return error("Invalid record"); |
4456 | SectionTable.push_back(S); |
4457 | break; |
4458 | } |
4459 | case bitc::MODULE_CODE_GCNAME: { // SECTIONNAME: [strchr x N] |
4460 | std::string S; |
4461 | if (convertToString(Record, 0, S)) |
4462 | return error("Invalid record"); |
4463 | GCTable.push_back(S); |
4464 | break; |
4465 | } |
4466 | case bitc::MODULE_CODE_COMDAT: |
4467 | if (Error Err = parseComdatRecord(Record)) |
4468 | return Err; |
4469 | break; |
4470 | // FIXME: BitcodeReader should handle {GLOBALVAR, FUNCTION, ALIAS, IFUNC} |
4471 | // written by ThinLinkBitcodeWriter. See |
4472 | // `ThinLinkBitcodeWriter::writeSimplifiedModuleInfo` for the format of each |
4473 | // record |
4474 | // (https://github.com/llvm/llvm-project/blob/b6a93967d9c11e79802b5e75cec1584d6c8aa472/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp#L4714) |
4475 | case bitc::MODULE_CODE_GLOBALVAR: |
4476 | if (Error Err = parseGlobalVarRecord(Record)) |
4477 | return Err; |
4478 | break; |
4479 | case bitc::MODULE_CODE_FUNCTION: |
4480 | if (Error Err = ResolveDataLayout()) |
4481 | return Err; |
4482 | if (Error Err = parseFunctionRecord(Record)) |
4483 | return Err; |
4484 | break; |
4485 | case bitc::MODULE_CODE_IFUNC: |
4486 | case bitc::MODULE_CODE_ALIAS: |
4487 | case bitc::MODULE_CODE_ALIAS_OLD: |
4488 | if (Error Err = parseGlobalIndirectSymbolRecord(BitCode, Record)) |
4489 | return Err; |
4490 | break; |
4491 | /// MODULE_CODE_VSTOFFSET: [offset] |
4492 | case bitc::MODULE_CODE_VSTOFFSET: |
4493 | if (Record.empty()) |
4494 | return error("Invalid record"); |
4495 | // Note that we subtract 1 here because the offset is relative to one word |
4496 | // before the start of the identification or module block, which was |
4497 | // historically always the start of the regular bitcode header. |
4498 | VSTOffset = Record[0] - 1; |
4499 | break; |
4500 | /// MODULE_CODE_SOURCE_FILENAME: [namechar x N] |
4501 | case bitc::MODULE_CODE_SOURCE_FILENAME: |
4502 | SmallString<128> ValueName; |
4503 | if (convertToString(Record, 0, ValueName)) |
4504 | return error("Invalid record"); |
4505 | TheModule->setSourceFileName(ValueName); |
4506 | break; |
4507 | } |
4508 | Record.clear(); |
4509 | } |
4510 | this->ValueTypeCallback = std::nullopt; |
4511 | return Error::success(); |
4512 | } |
4513 | |
4514 | Error BitcodeReader::parseBitcodeInto(Module *M, bool ShouldLazyLoadMetadata, |
4515 | bool IsImporting, |
4516 | ParserCallbacks Callbacks) { |
4517 | TheModule = M; |
4518 | MetadataLoaderCallbacks MDCallbacks; |
4519 | MDCallbacks.GetTypeByID = [&](unsigned ID) { return getTypeByID(ID); }; |
4520 | MDCallbacks.GetContainedTypeID = [&](unsigned I, unsigned J) { |
4521 | return getContainedTypeID(I, J); |
4522 | }; |
4523 | MDCallbacks.MDType = Callbacks.MDType; |
4524 | MDLoader = MetadataLoader(Stream, *M, ValueList, IsImporting, MDCallbacks); |
4525 | return parseModule(0, ShouldLazyLoadMetadata, Callbacks); |
4526 | } |
4527 | |
4528 | Error BitcodeReader::typeCheckLoadStoreInst(Type *ValType, Type *PtrType) { |
4529 | if (!isa<PointerType>(PtrType)) |
4530 | return error("Load/Store operand is not a pointer type"); |
4531 | |
4532 | if (!cast<PointerType>(PtrType)->isOpaqueOrPointeeTypeMatches(ValType)) |
4533 | return error("Explicit load/store type does not match pointee " |
4534 | "type of pointer operand"); |
4535 | if (!PointerType::isLoadableOrStorableType(ValType)) |
4536 | return error("Cannot load/store from pointer"); |
4537 | return Error::success(); |
4538 | } |
4539 | |
4540 | Error BitcodeReader::propagateAttributeTypes(CallBase *CB, |
4541 | ArrayRef<unsigned> ArgTyIDs) { |
4542 | AttributeList Attrs = CB->getAttributes(); |
4543 | for (unsigned i = 0; i != CB->arg_size(); ++i) { |
4544 | for (Attribute::AttrKind Kind : {Attribute::ByVal, Attribute::StructRet, |
4545 | Attribute::InAlloca}) { |
4546 | if (!Attrs.hasParamAttr(i, Kind) || |
4547 | Attrs.getParamAttr(i, Kind).getValueAsType()) |
4548 | continue; |
4549 | |
4550 | Type *PtrEltTy = getPtrElementTypeByID(ArgTyIDs[i]); |
4551 | if (!PtrEltTy) |
4552 | return error("Missing element type for typed attribute upgrade"); |
4553 | |
4554 | Attribute NewAttr; |
4555 | switch (Kind) { |
4556 | case Attribute::ByVal: |
4557 | NewAttr = Attribute::getWithByValType(Context, PtrEltTy); |
4558 | break; |
4559 | case Attribute::StructRet: |
4560 | NewAttr = Attribute::getWithStructRetType(Context, PtrEltTy); |
4561 | break; |
4562 | case Attribute::InAlloca: |
4563 | NewAttr = Attribute::getWithInAllocaType(Context, PtrEltTy); |
4564 | break; |
4565 | default: |
4566 | llvm_unreachable("not an upgraded type attribute")::llvm::llvm_unreachable_internal("not an upgraded type attribute" , "llvm/lib/Bitcode/Reader/BitcodeReader.cpp", 4566); |
4567 | } |
4568 | |
4569 | Attrs = Attrs.addParamAttribute(Context, i, NewAttr); |
4570 | } |
4571 | } |
4572 | |
4573 | if (CB->isInlineAsm()) { |
4574 | const InlineAsm *IA = cast<InlineAsm>(CB->getCalledOperand()); |
4575 | unsigned ArgNo = 0; |
4576 | for (const InlineAsm::ConstraintInfo &CI : IA->ParseConstraints()) { |
4577 | if (!CI.hasArg()) |
4578 | continue; |
4579 | |
4580 | if (CI.isIndirect && !Attrs.getParamElementType(ArgNo)) { |
4581 | Type *ElemTy = getPtrElementTypeByID(ArgTyIDs[ArgNo]); |
4582 | if (!ElemTy) |
4583 | return error("Missing element type for inline asm upgrade"); |
4584 | Attrs = Attrs.addParamAttribute( |
4585 | Context, ArgNo, |
4586 | Attribute::get(Context, Attribute::ElementType, ElemTy)); |
4587 | } |
4588 | |
4589 | ArgNo++; |
4590 | } |
4591 | } |
4592 | |
4593 | switch (CB->getIntrinsicID()) { |
4594 | case Intrinsic::preserve_array_access_index: |
4595 | case Intrinsic::preserve_struct_access_index: |
4596 | case Intrinsic::aarch64_ldaxr: |
4597 | case Intrinsic::aarch64_ldxr: |
4598 | case Intrinsic::aarch64_stlxr: |
4599 | case Intrinsic::aarch64_stxr: |
4600 | case Intrinsic::arm_ldaex: |
4601 | case Intrinsic::arm_ldrex: |
4602 | case Intrinsic::arm_stlex: |
4603 | case Intrinsic::arm_strex: { |
4604 | unsigned ArgNo; |
4605 | switch (CB->getIntrinsicID()) { |
4606 | case Intrinsic::aarch64_stlxr: |
4607 | case Intrinsic::aarch64_stxr: |
4608 | case Intrinsic::arm_stlex: |
4609 | case Intrinsic::arm_strex: |
4610 | ArgNo = 1; |
4611 | break; |
4612 | default: |
4613 | ArgNo = 0; |
4614 | break; |
4615 | } |
4616 | if (!Attrs.getParamElementType(ArgNo)) { |
4617 | Type *ElTy = getPtrElementTypeByID(ArgTyIDs[ArgNo]); |
4618 | if (!ElTy) |
4619 | return error("Missing element type for elementtype upgrade"); |
4620 | Attribute NewAttr = Attribute::get(Context, Attribute::ElementType, ElTy); |
4621 | Attrs = Attrs.addParamAttribute(Context, ArgNo, NewAttr); |
4622 | } |
4623 | break; |
4624 | } |
4625 | default: |
4626 | break; |
4627 | } |
4628 | |
4629 | CB->setAttributes(Attrs); |
4630 | return Error::success(); |
4631 | } |
4632 | |
4633 | /// Lazily parse the specified function body block. |
4634 | Error BitcodeReader::parseFunctionBody(Function *F) { |
4635 | if (Error Err = Stream.EnterSubBlock(bitc::FUNCTION_BLOCK_ID)) |
4636 | return Err; |
4637 | |
4638 | // Unexpected unresolved metadata when parsing function. |
4639 | if (MDLoader->hasFwdRefs()) |
4640 | return error("Invalid function metadata: incoming forward references"); |
4641 | |
4642 | InstructionList.clear(); |
4643 | unsigned ModuleValueListSize = ValueList.size(); |
4644 | unsigned ModuleMDLoaderSize = MDLoader->size(); |
4645 | |
4646 | // Add all the function arguments to the value table. |
4647 | unsigned ArgNo = 0; |
4648 | unsigned FTyID = FunctionTypeIDs[F]; |
4649 | for (Argument &I : F->args()) { |
4650 | unsigned ArgTyID = getContainedTypeID(FTyID, ArgNo + 1); |
4651 | assert(I.getType() == getTypeByID(ArgTyID) &&(static_cast <bool> (I.getType() == getTypeByID(ArgTyID ) && "Incorrect fully specified type for Function Argument" ) ? void (0) : __assert_fail ("I.getType() == getTypeByID(ArgTyID) && \"Incorrect fully specified type for Function Argument\"" , "llvm/lib/Bitcode/Reader/BitcodeReader.cpp", 4652, __extension__ __PRETTY_FUNCTION__)) |
4652 | "Incorrect fully specified type for Function Argument")(static_cast <bool> (I.getType() == getTypeByID(ArgTyID ) && "Incorrect fully specified type for Function Argument" ) ? void (0) : __assert_fail ("I.getType() == getTypeByID(ArgTyID) && \"Incorrect fully specified type for Function Argument\"" , "llvm/lib/Bitcode/Reader/BitcodeReader.cpp", 4652, __extension__ __PRETTY_FUNCTION__)); |
4653 | ValueList.push_back(&I, ArgTyID); |
4654 | ++ArgNo; |
4655 | } |
4656 | unsigned NextValueNo = ValueList.size(); |
4657 | BasicBlock *CurBB = nullptr; |
4658 | unsigned CurBBNo = 0; |
4659 | // Block into which constant expressions from phi nodes are materialized. |
4660 | BasicBlock *PhiConstExprBB = nullptr; |
4661 | // Edge blocks for phi nodes into which constant expressions have been |
4662 | // expanded. |
4663 | SmallMapVector<std::pair<BasicBlock *, BasicBlock *>, BasicBlock *, 4> |
4664 | ConstExprEdgeBBs; |
4665 | |
4666 | DebugLoc LastLoc; |
4667 | auto getLastInstruction = [&]() -> Instruction * { |
4668 | if (CurBB && !CurBB->empty()) |
4669 | return &CurBB->back(); |
4670 | else if (CurBBNo && FunctionBBs[CurBBNo - 1] && |
4671 | !FunctionBBs[CurBBNo - 1]->empty()) |
4672 | return &FunctionBBs[CurBBNo - 1]->back(); |
4673 | return nullptr; |
4674 | }; |
4675 | |
4676 | std::vector<OperandBundleDef> OperandBundles; |
4677 | |
4678 | // Read all the records. |
4679 | SmallVector<uint64_t, 64> Record; |
4680 | |
4681 | while (true) { |
4682 | Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); |
4683 | if (!MaybeEntry) |
4684 | return MaybeEntry.takeError(); |
4685 | llvm::BitstreamEntry Entry = MaybeEntry.get(); |
4686 | |
4687 | switch (Entry.Kind) { |
4688 | case BitstreamEntry::Error: |
4689 | return error("Malformed block"); |
4690 | case BitstreamEntry::EndBlock: |
4691 | goto OutOfRecordLoop; |
4692 | |
4693 | case BitstreamEntry::SubBlock: |
4694 | switch (Entry.ID) { |
4695 | default: // Skip unknown content. |
4696 | if (Error Err = Stream.SkipBlock()) |
4697 | return Err; |
4698 | break; |
4699 | case bitc::CONSTANTS_BLOCK_ID: |
4700 | if (Error Err = parseConstants()) |
4701 | return Err; |
4702 | NextValueNo = ValueList.size(); |
4703 | break; |
4704 | case bitc::VALUE_SYMTAB_BLOCK_ID: |
4705 | if (Error Err = parseValueSymbolTable()) |
4706 | return Err; |
4707 | break; |
4708 | case bitc::METADATA_ATTACHMENT_ID: |
4709 | if (Error Err = MDLoader->parseMetadataAttachment(*F, InstructionList)) |
4710 | return Err; |
4711 | break; |
4712 | case bitc::METADATA_BLOCK_ID: |
4713 | assert(DeferredMetadataInfo.empty() &&(static_cast <bool> (DeferredMetadataInfo.empty() && "Must read all module-level metadata before function-level") ? void (0) : __assert_fail ("DeferredMetadataInfo.empty() && \"Must read all module-level metadata before function-level\"" , "llvm/lib/Bitcode/Reader/BitcodeReader.cpp", 4714, __extension__ __PRETTY_FUNCTION__)) |
4714 | "Must read all module-level metadata before function-level")(static_cast <bool> (DeferredMetadataInfo.empty() && "Must read all module-level metadata before function-level") ? void (0) : __assert_fail ("DeferredMetadataInfo.empty() && \"Must read all module-level metadata before function-level\"" , "llvm/lib/Bitcode/Reader/BitcodeReader.cpp", 4714, __extension__ __PRETTY_FUNCTION__)); |
4715 | if (Error Err = MDLoader->parseFunctionMetadata()) |
4716 | return Err; |
4717 | break; |
4718 | case bitc::USELIST_BLOCK_ID: |
4719 | if (Error Err = parseUseLists()) |
4720 | return Err; |
4721 | break; |
4722 | } |
4723 | continue; |
4724 | |
4725 | case BitstreamEntry::Record: |
4726 | // The interesting case. |
4727 | break; |
4728 | } |
4729 | |
4730 | // Read a record. |
4731 | Record.clear(); |
4732 | Instruction *I = nullptr; |
4733 | unsigned ResTypeID = InvalidTypeID; |
4734 | Expected<unsigned> MaybeBitCode = Stream.readRecord(Entry.ID, Record); |
4735 | if (!MaybeBitCode) |
4736 | return MaybeBitCode.takeError(); |
4737 | switch (unsigned BitCode = MaybeBitCode.get()) { |
4738 | default: // Default behavior: reject |
4739 | return error("Invalid value"); |
4740 | case bitc::FUNC_CODE_DECLAREBLOCKS: { // DECLAREBLOCKS: [nblocks] |
4741 | if (Record.empty() || Record[0] == 0) |
4742 | return error("Invalid record"); |
4743 | // Create all the basic blocks for the function. |
4744 | FunctionBBs.resize(Record[0]); |
4745 | |
4746 | // See if anything took the address of blocks in this function. |
4747 | auto BBFRI = BasicBlockFwdRefs.find(F); |
4748 | if (BBFRI == BasicBlockFwdRefs.end()) { |
4749 | for (BasicBlock *&BB : FunctionBBs) |
4750 | BB = BasicBlock::Create(Context, "", F); |
4751 | } else { |
4752 | auto &BBRefs = BBFRI->second; |
4753 | // Check for invalid basic block references. |
4754 | if (BBRefs.size() > FunctionBBs.size()) |
4755 | return error("Invalid ID"); |
4756 | assert(!BBRefs.empty() && "Unexpected empty array")(static_cast <bool> (!BBRefs.empty() && "Unexpected empty array" ) ? void (0) : __assert_fail ("!BBRefs.empty() && \"Unexpected empty array\"" , "llvm/lib/Bitcode/Reader/BitcodeReader.cpp", 4756, __extension__ __PRETTY_FUNCTION__)); |
4757 | assert(!BBRefs.front() && "Invalid reference to entry block")(static_cast <bool> (!BBRefs.front() && "Invalid reference to entry block" ) ? void (0) : __assert_fail ("!BBRefs.front() && \"Invalid reference to entry block\"" , "llvm/lib/Bitcode/Reader/BitcodeReader.cpp", 4757, __extension__ __PRETTY_FUNCTION__)); |
4758 | for (unsigned I = 0, E = FunctionBBs.size(), RE = BBRefs.size(); I != E; |
4759 | ++I) |
4760 | if (I < RE && BBRefs[I]) { |
4761 | BBRefs[I]->insertInto(F); |
4762 | FunctionBBs[I] = BBRefs[I]; |
4763 | } else { |
4764 | FunctionBBs[I] = BasicBlock::Create(Context, "", F); |
4765 | } |
4766 | |
4767 | // Erase from the table. |
4768 | BasicBlockFwdRefs.erase(BBFRI); |
4769 | } |
4770 | |
4771 | CurBB = FunctionBBs[0]; |
4772 | continue; |
4773 | } |
4774 | |
4775 | case bitc::FUNC_CODE_BLOCKADDR_USERS: // BLOCKADDR_USERS: [vals...] |
4776 | // The record should not be emitted if it's an empty list. |
4777 | if (Record.empty()) |
4778 | return error("Invalid record"); |
4779 | // When we have the RARE case of a BlockAddress Constant that is not |
4780 | // scoped to the Function it refers to, we need to conservatively |
4781 | // materialize the referred to Function, regardless of whether or not |
4782 | // that Function will ultimately be linked, otherwise users of |
4783 | // BitcodeReader might start splicing out Function bodies such that we |
4784 | // might no longer be able to materialize the BlockAddress since the |
4785 | // BasicBlock (and entire body of the Function) the BlockAddress refers |
4786 | // to may have been moved. In the case that the user of BitcodeReader |
4787 | // decides ultimately not to link the Function body, materializing here |
4788 | // could be considered wasteful, but it's better than a deserialization |
4789 | // failure as described. This keeps BitcodeReader unaware of complex |
4790 | // linkage policy decisions such as those use by LTO, leaving those |
4791 | // decisions "one layer up." |
4792 | for (uint64_t ValID : Record) |
4793 | if (auto *F = dyn_cast<Function>(ValueList[ValID])) |
4794 | BackwardRefFunctions.push_back(F); |
4795 | else |
4796 | return error("Invalid record"); |
4797 | |
4798 | continue; |
4799 | |
4800 | case bitc::FUNC_CODE_DEBUG_LOC_AGAIN: // DEBUG_LOC_AGAIN |
4801 | // This record indicates that the last instruction is at the same |
4802 | // location as the previous instruction with a location. |
4803 | I = getLastInstruction(); |
4804 | |
4805 | if (!I) |
4806 | return error("Invalid record"); |
4807 | I->setDebugLoc(LastLoc); |
4808 | I = nullptr; |
4809 | continue; |
4810 | |
4811 | case bitc::FUNC_CODE_DEBUG_LOC: { // DEBUG_LOC: [line, col, scope, ia] |
4812 | I = getLastInstruction(); |
4813 | if (!I || Record.size() < 4) |
4814 | return error("Invalid record"); |
4815 | |
4816 | unsigned Line = Record[0], Col = Record[1]; |
4817 | unsigned ScopeID = Record[2], IAID = Record[3]; |
4818 | bool isImplicitCode = Record.size() == 5 && Record[4]; |
4819 | |
4820 | MDNode *Scope = nullptr, *IA = nullptr; |
4821 | if (ScopeID) { |
4822 | Scope = dyn_cast_or_null<MDNode>( |
4823 | MDLoader->getMetadataFwdRefOrLoad(ScopeID - 1)); |
4824 | if (!Scope) |
4825 | return error("Invalid record"); |
4826 | } |
4827 | if (IAID) { |
4828 | IA = dyn_cast_or_null<MDNode>( |
4829 | MDLoader->getMetadataFwdRefOrLoad(IAID - 1)); |
4830 | if (!IA) |
4831 | return error("Invalid record"); |
4832 | } |
4833 | LastLoc = DILocation::get(Scope->getContext(), Line, Col, Scope, IA, |
4834 | isImplicitCode); |
4835 | I->setDebugLoc(LastLoc); |
4836 | I = nullptr; |
4837 | continue; |
4838 | } |
4839 | case bitc::FUNC_CODE_INST_UNOP: { // UNOP: [opval, ty, opcode] |
4840 | unsigned OpNum = 0; |
4841 | Value *LHS; |
4842 | unsigned TypeID; |
4843 | if (getValueTypePair(Record, OpNum, NextValueNo, LHS, TypeID, CurBB) || |
4844 | OpNum+1 > Record.size()) |
4845 | return error("Invalid record"); |
4846 | |
4847 | int Opc = getDecodedUnaryOpcode(Record[OpNum++], LHS->getType()); |
4848 | if (Opc == -1) |
4849 | return error("Invalid record"); |
4850 | I = UnaryOperator::Create((Instruction::UnaryOps)Opc, LHS); |
4851 | ResTypeID = TypeID; |
4852 | InstructionList.push_back(I); |
4853 | if (OpNum < Record.size()) { |
4854 | if (isa<FPMathOperator>(I)) { |
4855 | FastMathFlags FMF = getDecodedFastMathFlags(Record[OpNum]); |
4856 | if (FMF.any()) |
4857 | I->setFastMathFlags(FMF); |
4858 | } |
4859 | } |
4860 | break; |
4861 | } |
4862 | case bitc::FUNC_CODE_INST_BINOP: { // BINOP: [opval, ty, opval, opcode] |
4863 | unsigned OpNum = 0; |
4864 | Value *LHS, *RHS; |
4865 | unsigned TypeID; |
4866 | if (getValueTypePair(Record, OpNum, NextValueNo, LHS, TypeID, CurBB) || |
4867 | popValue(Record, OpNum, NextValueNo, LHS->getType(), TypeID, RHS, |
4868 | CurBB) || |
4869 | OpNum+1 > Record.size()) |
4870 | return error("Invalid record"); |
4871 | |
4872 | int Opc = getDecodedBinaryOpcode(Record[OpNum++], LHS->getType()); |
4873 | if (Opc == -1) |
4874 | return error("Invalid record"); |
4875 | I = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS); |
4876 | ResTypeID = TypeID; |
4877 | InstructionList.push_back(I); |
4878 | if (OpNum < Record.size()) { |
4879 | if (Opc == Instruction::Add || |
4880 | Opc == Instruction::Sub || |
4881 | Opc == Instruction::Mul || |
4882 | Opc == Instruction::Shl) { |
4883 | if (Record[OpNum] & (1 << bitc::OBO_NO_SIGNED_WRAP)) |
4884 | cast<BinaryOperator>(I)->setHasNoSignedWrap(true); |
4885 | if (Record[OpNum] & (1 << bitc::OBO_NO_UNSIGNED_WRAP)) |
4886 | cast<BinaryOperator>(I)->setHasNoUnsignedWrap(true); |
4887 | } else if (Opc == Instruction::SDiv || |
4888 | Opc == Instruction::UDiv || |
4889 | Opc == Instruction::LShr || |
4890 | Opc == Instruction::AShr) { |
4891 | if (Record[OpNum] & (1 << bitc::PEO_EXACT)) |
4892 | cast<BinaryOperator>(I)->setIsExact(true); |
4893 | } else if (isa<FPMathOperator>(I)) { |
4894 | FastMathFlags FMF = getDecodedFastMathFlags(Record[OpNum]); |
4895 | if (FMF.any()) |
4896 | I->setFastMathFlags(FMF); |
4897 | } |
4898 | |
4899 | } |
4900 | break; |
4901 | } |
4902 | case bitc::FUNC_CODE_INST_CAST: { // CAST: [opval, opty, destty, castopc] |
4903 | unsigned OpNum = 0; |
4904 | Value *Op; |
4905 | unsigned OpTypeID; |
4906 | if (getValueTypePair(Record, OpNum, NextValueNo, Op, OpTypeID, CurBB) || |
4907 | OpNum+2 != Record.size()) |
4908 | return error("Invalid record"); |
4909 | |
4910 | ResTypeID = Record[OpNum]; |
4911 | Type *ResTy = getTypeByID(ResTypeID); |
4912 | int Opc = getDecodedCastOpcode(Record[OpNum + 1]); |
4913 | if (Opc == -1 || !ResTy) |
4914 | return error("Invalid record"); |
4915 | Instruction *Temp = nullptr; |
4916 | if ((I = UpgradeBitCastInst(Opc, Op, ResTy, Temp))) { |
4917 | if (Temp) { |
4918 | InstructionList.push_back(Temp); |
4919 | assert(CurBB && "No current BB?")(static_cast <bool> (CurBB && "No current BB?") ? void (0) : __assert_fail ("CurBB && \"No current BB?\"" , "llvm/lib/Bitcode/Reader/BitcodeReader.cpp", 4919, __extension__ __PRETTY_FUNCTION__)); |
4920 | Temp->insertInto(CurBB, CurBB->end()); |
4921 | } |
4922 | } else { |
4923 | auto CastOp = (Instruction::CastOps)Opc; |
4924 | if (!CastInst::castIsValid(CastOp, Op, ResTy)) |
4925 | return error("Invalid cast"); |
4926 | I = CastInst::Create(CastOp, Op, ResTy); |
4927 | } |
4928 | InstructionList.push_back(I); |
4929 | break; |
4930 | } |
4931 | case bitc::FUNC_CODE_INST_INBOUNDS_GEP_OLD: |
4932 | case bitc::FUNC_CODE_INST_GEP_OLD: |
4933 | case bitc::FUNC_CODE_INST_GEP: { // GEP: type, [n x operands] |
4934 | unsigned OpNum = 0; |
4935 | |
4936 | unsigned TyID; |
4937 | Type *Ty; |
4938 | bool InBounds; |
4939 | |
4940 | if (BitCode == bitc::FUNC_CODE_INST_GEP) { |
4941 | InBounds = Record[OpNum++]; |
4942 | TyID = Record[OpNum++]; |
4943 | Ty = getTypeByID(TyID); |
4944 | } else { |
4945 | InBounds = BitCode == bitc::FUNC_CODE_INST_INBOUNDS_GEP_OLD; |
4946 | TyID = InvalidTypeID; |
4947 | Ty = nullptr; |
4948 | } |
4949 | |
4950 | Value *BasePtr; |
4951 | unsigned BasePtrTypeID; |
4952 | if (getValueTypePair(Record, OpNum, NextValueNo, BasePtr, BasePtrTypeID, |
4953 | CurBB)) |
4954 | return error("Invalid record"); |
4955 | |
4956 | if (!Ty) { |
4957 | TyID = getContainedTypeID(BasePtrTypeID); |
4958 | if (BasePtr->getType()->isVectorTy()) |
4959 | TyID = getContainedTypeID(TyID); |
4960 | Ty = getTypeByID(TyID); |
4961 | } else if (!cast<PointerType>(BasePtr->getType()->getScalarType()) |
4962 | ->isOpaqueOrPointeeTypeMatches(Ty)) { |
4963 | return error( |
4964 | "Explicit gep type does not match pointee type of pointer operand"); |
4965 | } |
4966 | |
4967 | SmallVector<Value*, 16> GEPIdx; |
4968 | while (OpNum != Record.size()) { |
4969 | Value *Op; |
4970 | unsigned OpTypeID; |
4971 | if (getValueTypePair(Record, OpNum, NextValueNo, Op, OpTypeID, CurBB)) |
4972 | return error("Invalid record"); |
4973 | GEPIdx.push_back(Op); |
4974 | } |
4975 | |
4976 | I = GetElementPtrInst::Create(Ty, BasePtr, GEPIdx); |
4977 | |
4978 | ResTypeID = TyID; |
4979 | if (cast<GEPOperator>(I)->getNumIndices() != 0) { |
4980 | auto GTI = std::next(gep_type_begin(I)); |
4981 | for (Value *Idx : drop_begin(cast<GEPOperator>(I)->indices())) { |
4982 | unsigned SubType = 0; |
4983 | if (GTI.isStruct()) { |
4984 | ConstantInt *IdxC = |
4985 | Idx->getType()->isVectorTy() |
4986 | ? cast<ConstantInt>(cast<Constant>(Idx)->getSplatValue()) |
4987 | : cast<ConstantInt>(Idx); |
4988 | SubType = IdxC->getZExtValue(); |
4989 | } |
4990 | ResTypeID = getContainedTypeID(ResTypeID, SubType); |
4991 | ++GTI; |
4992 | } |
4993 | } |
4994 | |
4995 | // At this point ResTypeID is the result element type. We need a pointer |
4996 | // or vector of pointer to it. |
4997 | ResTypeID = getVirtualTypeID(I->getType()->getScalarType(), ResTypeID); |
4998 | if (I->getType()->isVectorTy()) |
4999 | ResTypeID = getVirtualTypeID(I->getType(), ResTypeID); |
5000 | |
5001 | InstructionList.push_back(I); |
5002 | if (InBounds) |
5003 | cast<GetElementPtrInst>(I)->setIsInBounds(true); |
5004 | break; |
5005 | } |
5006 | |
5007 | case bitc::FUNC_CODE_INST_EXTRACTVAL: { |
5008 | // EXTRACTVAL: [opty, opval, n x indices] |
5009 | unsigned OpNum = 0; |
5010 | Value *Agg; |
5011 | unsigned AggTypeID; |
5012 | if (getValueTypePair(Record, OpNum, NextValueNo, Agg, AggTypeID, CurBB)) |
5013 | return error("Invalid record"); |
5014 | Type *Ty = Agg->getType(); |
5015 | |
5016 | unsigned RecSize = Record.size(); |
5017 | if (OpNum == RecSize) |
5018 | return error("EXTRACTVAL: Invalid instruction with 0 indices"); |
5019 | |
5020 | SmallVector<unsigned, 4> EXTRACTVALIdx; |
5021 | ResTypeID = AggTypeID; |
5022 | for (; OpNum != RecSize; ++OpNum) { |
5023 | bool IsArray = Ty->isArrayTy(); |
5024 | bool IsStruct = Ty->isStructTy(); |
5025 | uint64_t Index = Record[OpNum]; |
5026 | |
5027 | if (!IsStruct && !IsArray) |
5028 | return error("EXTRACTVAL: Invalid type"); |
5029 | if ((unsigned)Index != Index) |
5030 | return error("Invalid value"); |
5031 | if (IsStruct && Index >= Ty->getStructNumElements()) |
5032 | return error("EXTRACTVAL: Invalid struct index"); |
5033 | if (IsArray && Index >= Ty->getArrayNumElements()) |
5034 | return error("EXTRACTVAL: Invalid array index"); |
5035 | EXTRACTVALIdx.push_back((unsigned)Index); |
5036 | |
5037 | if (IsStruct) { |
5038 | Ty = Ty->getStructElementType(Index); |
5039 | ResTypeID = getContainedTypeID(ResTypeID, Index); |
5040 | } else { |
5041 | Ty = Ty->getArrayElementType(); |
5042 | ResTypeID = getContainedTypeID(ResTypeID); |
5043 | } |
5044 | } |
5045 | |
5046 | I = ExtractValueInst::Create(Agg, EXTRACTVALIdx); |
5047 | InstructionList.push_back(I); |
5048 | break; |
5049 | } |
5050 | |
5051 | case bitc::FUNC_CODE_INST_INSERTVAL: { |
5052 | // INSERTVAL: [opty, opval, opty, opval, n x indices] |
5053 | unsigned OpNum = 0; |
5054 | Value *Agg; |
5055 | unsigned AggTypeID; |
5056 | if (getValueTypePair(Record, OpNum, NextValueNo, Agg, AggTypeID, CurBB)) |
5057 | return error("Invalid record"); |
5058 | Value *Val; |
5059 | unsigned ValTypeID; |
5060 | if (getValueTypePair(Record, OpNum, NextValueNo, Val, ValTypeID, CurBB)) |
5061 | return error("Invalid record"); |
5062 | |
5063 | unsigned RecSize = Record.size(); |
5064 | if (OpNum == RecSize) |
5065 | return error("INSERTVAL: Invalid instruction with 0 indices"); |
5066 | |
5067 | SmallVector<unsigned, 4> INSERTVALIdx; |
5068 | Type *CurTy = Agg->getType(); |
5069 | for (; OpNum != RecSize; ++OpNum) { |
5070 | bool IsArray = CurTy->isArrayTy(); |
5071 | bool IsStruct = CurTy->isStructTy(); |
5072 | uint64_t Index = Record[OpNum]; |
5073 | |
5074 | if (!IsStruct && !IsArray) |
5075 | return error("INSERTVAL: Invalid type"); |
5076 | if ((unsigned)Index != Index) |
5077 | return error("Invalid value"); |
5078 | if (IsStruct && Index >= CurTy->getStructNumElements()) |
5079 | return error("INSERTVAL: Invalid struct index"); |
5080 | if (IsArray && Index >= CurTy->getArrayNumElements()) |
5081 | return error("INSERTVAL: Invalid array index"); |
5082 | |
5083 | INSERTVALIdx.push_back((unsigned)Index); |
5084 | if (IsStruct) |
5085 | CurTy = CurTy->getStructElementType(Index); |
5086 | else |
5087 | CurTy = CurTy->getArrayElementType(); |
5088 | } |
5089 | |
5090 | if (CurTy != Val->getType()) |
5091 | return error("Inserted value type doesn't match aggregate type"); |
5092 | |
5093 | I = InsertValueInst::Create(Agg, Val, INSERTVALIdx); |
5094 | ResTypeID = AggTypeID; |
5095 | InstructionList.push_back(I); |
5096 | break; |
5097 | } |
5098 | |
5099 | case bitc::FUNC_CODE_INST_SELECT: { // SELECT: [opval, ty, opval, opval] |
5100 | // obsolete form of select |
5101 | // handles select i1 ... in old bitcode |
5102 | unsigned OpNum = 0; |
5103 | Value *TrueVal, *FalseVal, *Cond; |
5104 | unsigned TypeID; |
5105 | Type *CondType = Type::getInt1Ty(Context); |
5106 | if (getValueTypePair(Record, OpNum, NextValueNo, TrueVal, TypeID, |
5107 | CurBB) || |
5108 | popValue(Record, OpNum, NextValueNo, TrueVal->getType(), TypeID, |
5109 | FalseVal, CurBB) || |
5110 | popValue(Record, OpNum, NextValueNo, CondType, |
5111 | getVirtualTypeID(CondType), Cond, CurBB)) |
5112 | return error("Invalid record"); |
5113 | |
5114 | I = SelectInst::Create(Cond, TrueVal, FalseVal); |
5115 | ResTypeID = TypeID; |
5116 | InstructionList.push_back(I); |
5117 | break; |
5118 | } |
5119 | |
5120 | case bitc::FUNC_CODE_INST_VSELECT: {// VSELECT: [ty,opval,opval,predty,pred] |
5121 | // new form of select |
5122 | // handles select i1 or select [N x i1] |
5123 | unsigned OpNum = 0; |
5124 | Value *TrueVal, *FalseVal, *Cond; |
5125 | unsigned ValTypeID, CondTypeID; |
5126 | if (getValueTypePair(Record, OpNum, NextValueNo, TrueVal, ValTypeID, |
5127 | CurBB) || |
5128 | popValue(Record, OpNum, NextValueNo, TrueVal->getType(), ValTypeID, |
5129 | FalseVal, CurBB) || |
5130 | getValueTypePair(Record, OpNum, NextValueNo, Cond, CondTypeID, CurBB)) |
5131 | return error("Invalid record"); |
5132 | |
5133 | // select condition can be either i1 or [N x i1] |
5134 | if (VectorType* vector_type = |
5135 | dyn_cast<VectorType>(Cond->getType())) { |
5136 | // expect <n x i1> |
5137 | if (vector_type->getElementType() != Type::getInt1Ty(Context)) |
5138 | return error("Invalid type for value"); |
5139 | } else { |
5140 | // expect i1 |
5141 | if (Cond->getType() != Type::getInt1Ty(Context)) |
5142 | return error("Invalid type for value"); |
5143 | } |
5144 | |
5145 | I = SelectInst::Create(Cond, TrueVal, FalseVal); |
5146 | ResTypeID = ValTypeID; |
5147 | InstructionList.push_back(I); |
5148 | if (OpNum < Record.size() && isa<FPMathOperator>(I)) { |
5149 | FastMathFlags FMF = getDecodedFastMathFlags(Record[OpNum]); |
5150 | if (FMF.any()) |
5151 | I->setFastMathFlags(FMF); |
5152 | } |
5153 | break; |
5154 | } |
5155 | |
5156 | case bitc::FUNC_CODE_INST_EXTRACTELT: { // EXTRACTELT: [opty, opval, opval] |
5157 | unsigned OpNum = 0; |
5158 | Value *Vec, *Idx; |
5159 | unsigned VecTypeID, IdxTypeID; |
5160 | if (getValueTypePair(Record, OpNum, NextValueNo, Vec, VecTypeID, CurBB) || |
5161 | getValueTypePair(Record, OpNum, NextValueNo, Idx, IdxTypeID, CurBB)) |
5162 | return error("Invalid record"); |
5163 | if (!Vec->getType()->isVectorTy()) |
5164 | return error("Invalid type for value"); |
5165 | I = ExtractElementInst::Create(Vec, Idx); |
5166 | ResTypeID = getContainedTypeID(VecTypeID); |
5167 | InstructionList.push_back(I); |
5168 | break; |
5169 | } |
5170 | |
5171 | case bitc::FUNC_CODE_INST_INSERTELT: { // INSERTELT: [ty, opval,opval,opval] |
5172 | unsigned OpNum = 0; |
5173 | Value *Vec, *Elt, *Idx; |
5174 | unsigned VecTypeID, IdxTypeID; |
5175 | if (getValueTypePair(Record, OpNum, NextValueNo, Vec, VecTypeID, CurBB)) |
5176 | return error("Invalid record"); |
5177 | if (!Vec->getType()->isVectorTy()) |
5178 | return error("Invalid type for value"); |
5179 | if (popValue(Record, OpNum, NextValueNo, |
5180 | cast<VectorType>(Vec->getType())->getElementType(), |
5181 | getContainedTypeID(VecTypeID), Elt, CurBB) || |
5182 | getValueTypePair(Record, OpNum, NextValueNo, Idx, IdxTypeID, CurBB)) |
5183 | return error("Invalid record"); |
5184 | I = InsertElementInst::Create(Vec, Elt, Idx); |
5185 | ResTypeID = VecTypeID; |
5186 | InstructionList.push_back(I); |
5187 | break; |
5188 | } |
5189 | |
5190 | case bitc::FUNC_CODE_INST_SHUFFLEVEC: {// SHUFFLEVEC: [opval,ty,opval,opval] |
5191 | unsigned OpNum = 0; |
5192 | Value *Vec1, *Vec2, *Mask; |
5193 | unsigned Vec1TypeID; |
5194 | if (getValueTypePair(Record, OpNum, NextValueNo, Vec1, Vec1TypeID, |
5195 | CurBB) || |
5196 | popValue(Record, OpNum, NextValueNo, Vec1->getType(), Vec1TypeID, |
5197 | Vec2, CurBB)) |
5198 | return error("Invalid record"); |
5199 | |
5200 | unsigned MaskTypeID; |
5201 | if (getValueTypePair(Record, OpNum, NextValueNo, Mask, MaskTypeID, CurBB)) |
5202 | return error("Invalid record"); |
5203 | if (!Vec1->getType()->isVectorTy() || !Vec2->getType()->isVectorTy()) |
5204 | return error("Invalid type for value"); |
5205 | |
5206 | I = new ShuffleVectorInst(Vec1, Vec2, Mask); |
5207 | ResTypeID = |
5208 | getVirtualTypeID(I->getType(), getContainedTypeID(Vec1TypeID)); |
5209 | InstructionList.push_back(I); |
5210 | break; |
5211 | } |
5212 | |
5213 | case bitc::FUNC_CODE_INST_CMP: // CMP: [opty, opval, opval, pred] |
5214 | // Old form of ICmp/FCmp returning bool |
5215 | // Existed to differentiate between icmp/fcmp and vicmp/vfcmp which were |
5216 | // both legal on vectors but had different behaviour. |
5217 | case bitc::FUNC_CODE_INST_CMP2: { // CMP2: [opty, opval, opval, pred] |
5218 | // FCmp/ICmp returning bool or vector of bool |
5219 | |
5220 | unsigned OpNum = 0; |
5221 | Value *LHS, *RHS; |
5222 | unsigned LHSTypeID; |
5223 | if (getValueTypePair(Record, OpNum, NextValueNo, LHS, LHSTypeID, CurBB) || |
5224 | popValue(Record, OpNum, NextValueNo, LHS->getType(), LHSTypeID, RHS, |
5225 | CurBB)) |
5226 | return error("Invalid record"); |
5227 | |
5228 | if (OpNum >= Record.size()) |
5229 | return error( |
5230 | "Invalid record: operand number exceeded available operands"); |
5231 | |
5232 | unsigned PredVal = Record[OpNum]; |
5233 | bool IsFP = LHS->getType()->isFPOrFPVectorTy(); |
5234 | FastMathFlags FMF; |
5235 | if (IsFP && Record.size() > OpNum+1) |
5236 | FMF = getDecodedFastMathFlags(Record[++OpNum]); |
5237 | |
5238 | if (OpNum+1 != Record.size()) |
5239 | return error("Invalid record"); |
5240 | |
5241 | if (LHS->getType()->isFPOrFPVectorTy()) |
5242 | I = new FCmpInst((FCmpInst::Predicate)PredVal, LHS, RHS); |
5243 | else |
5244 | I = new ICmpInst((ICmpInst::Predicate)PredVal, LHS, RHS); |
5245 | |
5246 | ResTypeID = getVirtualTypeID(I->getType()->getScalarType()); |
5247 | if (LHS->getType()->isVectorTy()) |
5248 | ResTypeID = getVirtualTypeID(I->getType(), ResTypeID); |
5249 | |
5250 | if (FMF.any()) |
5251 | I->setFastMathFlags(FMF); |
5252 | InstructionList.push_back(I); |
5253 | break; |
5254 | } |
5255 | |
5256 | case bitc::FUNC_CODE_INST_RET: // RET: [opty,opval<optional>] |
5257 | { |
5258 | unsigned Size = Record.size(); |
5259 | if (Size == 0) { |
5260 | I = ReturnInst::Create(Context); |
5261 | InstructionList.push_back(I); |
5262 | break; |
5263 | } |
5264 | |
5265 | unsigned OpNum = 0; |
5266 | Value *Op = nullptr; |
5267 | unsigned OpTypeID; |
5268 | if (getValueTypePair(Record, OpNum, NextValueNo, Op, OpTypeID, CurBB)) |
5269 | return error("Invalid record"); |
5270 | if (OpNum != Record.size()) |
5271 | return error("Invalid record"); |
5272 | |
5273 | I = ReturnInst::Create(Context, Op); |
5274 | InstructionList.push_back(I); |
5275 | break; |
5276 | } |
5277 | case bitc::FUNC_CODE_INST_BR: { // BR: [bb#, bb#, opval] or [bb#] |
5278 | if (Record.size() != 1 && Record.size() != 3) |
5279 | return error("Invalid record"); |
5280 | BasicBlock *TrueDest = getBasicBlock(Record[0]); |
5281 | if (!TrueDest) |
5282 | return error("Invalid record"); |
5283 | |
5284 | if (Record.size() == 1) { |
5285 | I = BranchInst::Create(TrueDest); |
5286 | InstructionList.push_back(I); |
5287 | } |
5288 | else { |
5289 | BasicBlock *FalseDest = getBasicBlock(Record[1]); |
5290 | Type *CondType = Type::getInt1Ty(Context); |
5291 | Value *Cond = getValue(Record, 2, NextValueNo, CondType, |
5292 | getVirtualTypeID(CondType), CurBB); |
5293 | if (!FalseDest || !Cond) |
5294 | return error("Invalid record"); |
5295 | I = BranchInst::Create(TrueDest, FalseDest, Cond); |
5296 | InstructionList.push_back(I); |
5297 | } |
5298 | break; |
5299 | } |
5300 | case bitc::FUNC_CODE_INST_CLEANUPRET: { // CLEANUPRET: [val] or [val,bb#] |
5301 | if (Record.size() != 1 && Record.size() != 2) |
5302 | return error("Invalid record"); |
5303 | unsigned Idx = 0; |
5304 | Type *TokenTy = Type::getTokenTy(Context); |
5305 | Value *CleanupPad = getValue(Record, Idx++, NextValueNo, TokenTy, |
5306 | getVirtualTypeID(TokenTy), CurBB); |
5307 | if (!CleanupPad) |
5308 | return error("Invalid record"); |
5309 | BasicBlock *UnwindDest = nullptr; |
5310 | if (Record.size() == 2) { |
5311 | UnwindDest = getBasicBlock(Record[Idx++]); |
5312 | if (!UnwindDest) |
5313 | return error("Invalid record"); |
5314 | } |
5315 | |
5316 | I = CleanupReturnInst::Create(CleanupPad, UnwindDest); |
5317 | InstructionList.push_back(I); |
5318 | break; |
5319 | } |
5320 | case bitc::FUNC_CODE_INST_CATCHRET: { // CATCHRET: [val,bb#] |
5321 | if (Record.size() != 2) |
5322 | return error("Invalid record"); |
5323 | unsigned Idx = 0; |
5324 | Type *TokenTy = Type::getTokenTy(Context); |
5325 | Value *CatchPad = getValue(Record, Idx++, NextValueNo, TokenTy, |
5326 | getVirtualTypeID(TokenTy), CurBB); |
5327 | if (!CatchPad) |
5328 | return error("Invalid record"); |
5329 | BasicBlock *BB = getBasicBlock(Record[Idx++]); |
5330 | if (!BB) |
5331 | return error("Invalid record"); |
5332 | |
5333 | I = CatchReturnInst::Create(CatchPad, BB); |
5334 | InstructionList.push_back(I); |
5335 | break; |
5336 | } |
5337 | case bitc::FUNC_CODE_INST_CATCHSWITCH: { // CATCHSWITCH: [tok,num,(bb)*,bb?] |
5338 | // We must have, at minimum, the outer scope and the number of arguments. |
5339 | if (Record.size() < 2) |
5340 | return error("Invalid record"); |
5341 | |
5342 | unsigned Idx = 0; |
5343 | |
5344 | Type *TokenTy = Type::getTokenTy(Context); |
5345 | Value *ParentPad = getValue(Record, Idx++, NextValueNo, TokenTy, |
5346 | getVirtualTypeID(TokenTy), CurBB); |
5347 | |
5348 | unsigned NumHandlers = Record[Idx++]; |
5349 | |
5350 | SmallVector<BasicBlock *, 2> Handlers; |
5351 | for (unsigned Op = 0; Op != NumHandlers; ++Op) { |
5352 | BasicBlock *BB = getBasicBlock(Record[Idx++]); |
5353 | if (!BB) |
5354 | return error("Invalid record"); |
5355 | Handlers.push_back(BB); |
5356 | } |
5357 | |
5358 | BasicBlock *UnwindDest = nullptr; |
5359 | if (Idx + 1 == Record.size()) { |
5360 | UnwindDest = getBasicBlock(Record[Idx++]); |
5361 | if (!UnwindDest) |
5362 | return error("Invalid record"); |
5363 | } |
5364 | |
5365 | if (Record.size() != Idx) |
5366 | return error("Invalid record"); |
5367 | |
5368 | auto *CatchSwitch = |
5369 | CatchSwitchInst::Create(ParentPad, UnwindDest, NumHandlers); |
5370 | for (BasicBlock *Handler : Handlers) |
5371 | CatchSwitch->addHandler(Handler); |
5372 | I = CatchSwitch; |
5373 | ResTypeID = getVirtualTypeID(I->getType()); |
5374 | InstructionList.push_back(I); |
5375 | break; |
5376 | } |
5377 | case bitc::FUNC_CODE_INST_CATCHPAD: |
5378 | case bitc::FUNC_CODE_INST_CLEANUPPAD: { // [tok,num,(ty,val)*] |
5379 | // We must have, at minimum, the outer scope and the number of arguments. |
5380 | if (Record.size() < 2) |
5381 | return error("Invalid record"); |
5382 | |
5383 | unsigned Idx = 0; |
5384 | |
5385 | Type *TokenTy = Type::getTokenTy(Context); |
5386 | Value *ParentPad = getValue(Record, Idx++, NextValueNo, TokenTy, |
5387 | getVirtualTypeID(TokenTy), CurBB); |
5388 | |
5389 | unsigned NumArgOperands = Record[Idx++]; |
5390 | |
5391 | SmallVector<Value *, 2> Args; |
5392 | for (unsigned Op = 0; Op != NumArgOperands; ++Op) { |
5393 | Value *Val; |
5394 | unsigned ValTypeID; |
5395 | if (getValueTypePair(Record, Idx, NextValueNo, Val, ValTypeID, nullptr)) |
5396 | return error("Invalid record"); |
5397 | Args.push_back(Val); |
5398 | } |
5399 | |
5400 | if (Record.size() != Idx) |
5401 | return error("Invalid record"); |
5402 | |
5403 | if (BitCode == bitc::FUNC_CODE_INST_CLEANUPPAD) |
5404 | I = CleanupPadInst::Create(ParentPad, Args); |
5405 | else |
5406 | I = CatchPadInst::Create(ParentPad, Args); |
5407 | ResTypeID = getVirtualTypeID(I->getType()); |
5408 | InstructionList.push_back(I); |
5409 | break; |
5410 | } |
5411 | case bitc::FUNC_CODE_INST_SWITCH: { // SWITCH: [opty, op0, op1, ...] |
5412 | // Check magic |
5413 | if ((Record[0] >> 16) == SWITCH_INST_MAGIC) { |
5414 | // "New" SwitchInst format with case ranges. The changes to write this |
5415 | // format were reverted but we still recognize bitcode that uses it. |
5416 | // Hopefully someday we will have support for case ranges and can use |
5417 | // this format again. |
5418 | |
5419 | unsigned OpTyID = Record[1]; |
5420 | Type *OpTy = getTypeByID(OpTyID); |
5421 | unsigned ValueBitWidth = cast<IntegerType>(OpTy)->getBitWidth(); |
5422 | |
5423 | Value *Cond = getValue(Record, 2, NextValueNo, OpTy, OpTyID, CurBB); |
5424 | BasicBlock *Default = getBasicBlock(Record[3]); |
5425 | if (!OpTy || !Cond || !Default) |
5426 | return error("Invalid record"); |
5427 | |
5428 | unsigned NumCases = Record[4]; |
5429 | |
5430 | SwitchInst *SI = SwitchInst::Create(Cond, Default, NumCases); |
5431 | InstructionList.push_back(SI); |
5432 | |
5433 | unsigned CurIdx = 5; |
5434 | for (unsigned i = 0; i != NumCases; ++i) { |
5435 | SmallVector<ConstantInt*, 1> CaseVals; |
5436 | unsigned NumItems = Record[CurIdx++]; |
5437 | for (unsigned ci = 0; ci != NumItems; ++ci) { |
5438 | bool isSingleNumber = Record[CurIdx++]; |
5439 | |
5440 | APInt Low; |
5441 | unsigned ActiveWords = 1; |
5442 | if (ValueBitWidth > 64) |
5443 | ActiveWords = Record[CurIdx++]; |
5444 | Low = readWideAPInt(ArrayRef(&Record[CurIdx], ActiveWords), |
5445 | ValueBitWidth); |
5446 | CurIdx += ActiveWords; |
5447 | |
5448 | if (!isSingleNumber) { |
5449 | ActiveWords = 1; |
5450 | if (ValueBitWidth > 64) |
5451 | ActiveWords = Record[CurIdx++]; |
5452 | APInt High = readWideAPInt(ArrayRef(&Record[CurIdx], ActiveWords), |
5453 | ValueBitWidth); |
5454 | CurIdx += ActiveWords; |
5455 | |
5456 | // FIXME: It is not clear whether values in the range should be |
5457 | // compared as signed or unsigned values. The partially |
5458 | // implemented changes that used this format in the past used |
5459 | // unsigned comparisons. |
5460 | for ( ; Low.ule(High); ++Low) |
5461 | CaseVals.push_back(ConstantInt::get(Context, Low)); |
5462 | } else |
5463 | CaseVals.push_back(ConstantInt::get(Context, Low)); |
5464 | } |
5465 | BasicBlock *DestBB = getBasicBlock(Record[CurIdx++]); |
5466 | for (ConstantInt *Cst : CaseVals) |
5467 | SI->addCase(Cst, DestBB); |
5468 | } |
5469 | I = SI; |
5470 | break; |
5471 | } |
5472 | |
5473 | // Old SwitchInst format without case ranges. |
5474 | |
5475 | if (Record.size() < 3 || (Record.size() & 1) == 0) |
5476 | return error("Invalid record"); |
5477 | unsigned OpTyID = Record[0]; |
5478 | Type *OpTy = getTypeByID(OpTyID); |
5479 | Value *Cond = getValue(Record, 1, NextValueNo, OpTy, OpTyID, CurBB); |
5480 | BasicBlock *Default = getBasicBlock(Record[2]); |
5481 | if (!OpTy || !Cond || !Default) |
5482 | return error("Invalid record"); |
5483 | unsigned NumCases = (Record.size()-3)/2; |
5484 | SwitchInst *SI = SwitchInst::Create(Cond, Default, NumCases); |
5485 | InstructionList.push_back(SI); |
5486 | for (unsigned i = 0, e = NumCases; i != e; ++i) { |
5487 | ConstantInt *CaseVal = dyn_cast_or_null<ConstantInt>( |
5488 | getFnValueByID(Record[3+i*2], OpTy, OpTyID, nullptr)); |
5489 | BasicBlock *DestBB = getBasicBlock(Record[1+3+i*2]); |
5490 | if (!CaseVal || !DestBB) { |
5491 | delete SI; |
5492 | return error("Invalid record"); |
5493 | } |
5494 | SI->addCase(CaseVal, DestBB); |
5495 | } |
5496 | I = SI; |
5497 | break; |
5498 | } |
5499 | case bitc::FUNC_CODE_INST_INDIRECTBR: { // INDIRECTBR: [opty, op0, op1, ...] |
5500 | if (Record.size() < 2) |
5501 | return error("Invalid record"); |
5502 | unsigned OpTyID = Record[0]; |
5503 | Type *OpTy = getTypeByID(OpTyID); |
5504 | Value *Address = getValue(Record, 1, NextValueNo, OpTy, OpTyID, CurBB); |
5505 | if (!OpTy || !Address) |
5506 | return error("Invalid record"); |
5507 | unsigned NumDests = Record.size()-2; |
5508 | IndirectBrInst *IBI = IndirectBrInst::Create(Address, NumDests); |
5509 | InstructionList.push_back(IBI); |
5510 | for (unsigned i = 0, e = NumDests; i != e; ++i) { |
5511 | if (BasicBlock *DestBB = getBasicBlock(Record[2+i])) { |
5512 | IBI->addDestination(DestBB); |
5513 | } else { |
5514 | delete IBI; |
5515 | return error("Invalid record"); |
5516 | } |
5517 | } |
5518 | I = IBI; |
5519 | break; |
5520 | } |
5521 | |
5522 | case bitc::FUNC_CODE_INST_INVOKE: { |
5523 | // INVOKE: [attrs, cc, normBB, unwindBB, fnty, op0,op1,op2, ...] |
5524 | if (Record.size() < 4) |
5525 | return error("Invalid record"); |
5526 | unsigned OpNum = 0; |
5527 | AttributeList PAL = getAttributes(Record[OpNum++]); |
5528 | unsigned CCInfo = Record[OpNum++]; |
5529 | BasicBlock *NormalBB = getBasicBlock(Record[OpNum++]); |
5530 | BasicBlock *UnwindBB = getBasicBlock(Record[OpNum++]); |
5531 | |
5532 | unsigned FTyID = InvalidTypeID; |
5533 | FunctionType *FTy = nullptr; |
5534 | if ((CCInfo >> 13) & 1) { |
5535 | FTyID = Record[OpNum++]; |
5536 | FTy = dyn_cast<FunctionType>(getTypeByID(FTyID)); |
5537 | if (!FTy) |
5538 | return error("Explicit invoke type is not a function type"); |
5539 | } |
5540 | |
5541 | Value *Callee; |
5542 | unsigned CalleeTypeID; |
5543 | if (getValueTypePair(Record, OpNum, NextValueNo, Callee, CalleeTypeID, |
5544 | CurBB)) |
5545 | return error("Invalid record"); |
5546 | |
5547 | PointerType *CalleeTy = dyn_cast<PointerType>(Callee->getType()); |
5548 | if (!CalleeTy) |
5549 | return error("Callee is not a pointer"); |
5550 | if (!FTy) { |
5551 | FTyID = getContainedTypeID(CalleeTypeID); |
5552 | FTy = dyn_cast_or_null<FunctionType>(getTypeByID(FTyID)); |
5553 | if (!FTy) |
5554 | return error("Callee is not of pointer to function type"); |
5555 | } else if (!CalleeTy->isOpaqueOrPointeeTypeMatches(FTy)) |
5556 | return error("Explicit invoke type does not match pointee type of " |
5557 | "callee operand"); |
5558 | if (Record.size() < FTy->getNumParams() + OpNum) |
5559 | return error("Insufficient operands to call"); |
5560 | |
5561 | SmallVector<Value*, 16> Ops; |
5562 | SmallVector<unsigned, 16> ArgTyIDs; |
5563 | for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i, ++OpNum) { |
5564 | unsigned ArgTyID = getContainedTypeID(FTyID, i + 1); |
5565 | Ops.push_back(getValue(Record, OpNum, NextValueNo, FTy->getParamType(i), |
5566 | ArgTyID, CurBB)); |
5567 | ArgTyIDs.push_back(ArgTyID); |
5568 | if (!Ops.back()) |
5569 | return error("Invalid record"); |
5570 | } |
5571 | |
5572 | if (!FTy->isVarArg()) { |
5573 | if (Record.size() != OpNum) |
5574 | return error("Invalid record"); |
5575 | } else { |
5576 | // Read type/value pairs for varargs params. |
5577 | while (OpNum != Record.size()) { |
5578 | Value *Op; |
5579 | unsigned OpTypeID; |
5580 | if (getValueTypePair(Record, OpNum, NextValueNo, Op, OpTypeID, CurBB)) |
5581 | return error("Invalid record"); |
5582 | Ops.push_back(Op); |
5583 | ArgTyIDs.push_back(OpTypeID); |
5584 | } |
5585 | } |
5586 | |
5587 | // Upgrade the bundles if needed. |
5588 | if (!OperandBundles.empty()) |
5589 | UpgradeOperandBundles(OperandBundles); |
5590 | |
5591 | I = InvokeInst::Create(FTy, Callee, NormalBB, UnwindBB, Ops, |
5592 | OperandBundles); |
5593 | ResTypeID = getContainedTypeID(FTyID); |
5594 | OperandBundles.clear(); |
5595 | InstructionList.push_back(I); |
5596 | cast<InvokeInst>(I)->setCallingConv( |
5597 | static_cast<CallingConv::ID>(CallingConv::MaxID & CCInfo)); |
5598 | cast<InvokeInst>(I)->setAttributes(PAL); |
5599 | if (Error Err = propagateAttributeTypes(cast<CallBase>(I), ArgTyIDs)) { |
5600 | I->deleteValue(); |
5601 | return Err; |
5602 | } |
5603 | |
5604 | break; |
5605 | } |
5606 | case bitc::FUNC_CODE_INST_RESUME: { // RESUME: [opval] |
5607 | unsigned Idx = 0; |
5608 | Value *Val = nullptr; |
5609 | unsigned ValTypeID; |
5610 | if (getValueTypePair(Record, Idx, NextValueNo, Val, ValTypeID, CurBB)) |
5611 | return error("Invalid record"); |
5612 | I = ResumeInst::Create(Val); |
5613 | InstructionList.push_back(I); |
5614 | break; |
5615 | } |
5616 | case bitc::FUNC_CODE_INST_CALLBR: { |
5617 | // CALLBR: [attr, cc, norm, transfs, fty, fnid, args] |
5618 | unsigned OpNum = 0; |
5619 | AttributeList PAL = getAttributes(Record[OpNum++]); |
5620 | unsigned CCInfo = Record[OpNum++]; |
5621 | |
5622 | BasicBlock *DefaultDest = getBasicBlock(Record[OpNum++]); |
5623 | unsigned NumIndirectDests = Record[OpNum++]; |
5624 | SmallVector<BasicBlock *, 16> IndirectDests; |
5625 | for (unsigned i = 0, e = NumIndirectDests; i != e; ++i) |
5626 | IndirectDests.push_back(getBasicBlock(Record[OpNum++])); |
5627 | |
5628 | unsigned FTyID = InvalidTypeID; |
5629 | FunctionType *FTy = nullptr; |
5630 | if ((CCInfo >> bitc::CALL_EXPLICIT_TYPE) & 1) { |
5631 | FTyID = Record[OpNum++]; |
5632 | FTy = dyn_cast_or_null<FunctionType>(getTypeByID(FTyID)); |
5633 | if (!FTy) |
5634 | return error("Explicit call type is not a function type"); |
5635 | } |
5636 | |
5637 | Value *Callee; |
5638 | unsigned CalleeTypeID; |
5639 | if (getValueTypePair(Record, OpNum, NextValueNo, Callee, CalleeTypeID, |
5640 | CurBB)) |
5641 | return error("Invalid record"); |
5642 | |
5643 | PointerType *OpTy = dyn_cast<PointerType>(Callee->getType()); |
5644 | if (!OpTy) |
5645 | return error("Callee is not a pointer type"); |
5646 | if (!FTy) { |
5647 | FTyID = getContainedTypeID(CalleeTypeID); |
5648 | FTy = dyn_cast_or_null<FunctionType>(getTypeByID(FTyID)); |
5649 | if (!FTy) |
5650 | return error("Callee is not of pointer to function type"); |
5651 | } else if (!OpTy->isOpaqueOrPointeeTypeMatches(FTy)) |
5652 | return error("Explicit call type does not match pointee type of " |
5653 | "callee operand"); |
5654 | if (Record.size() < FTy->getNumParams() + OpNum) |
5655 | return error("Insufficient operands to call"); |
5656 | |
5657 | SmallVector<Value*, 16> Args; |
5658 | SmallVector<unsigned, 16> ArgTyIDs; |
5659 | // Read the fixed params. |
5660 | for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i, ++OpNum) { |
5661 | Value *Arg; |
5662 | unsigned ArgTyID = getContainedTypeID(FTyID, i + 1); |
5663 | if (FTy->getParamType(i)->isLabelTy()) |
5664 | Arg = getBasicBlock(Record[OpNum]); |
5665 | else |
5666 | Arg = getValue(Record, OpNum, NextValueNo, FTy->getParamType(i), |
5667 | ArgTyID, CurBB); |
5668 | if (!Arg) |
5669 | return error("Invalid record"); |
5670 | Args.push_back(Arg); |
5671 | ArgTyIDs.push_back(ArgTyID); |
5672 | } |
5673 | |
5674 | // Read type/value pairs for varargs params. |
5675 | if (!FTy->isVarArg()) { |
5676 | if (OpNum != Record.size()) |
5677 | return error("Invalid record"); |
5678 | } else { |
5679 | while (OpNum != Record.size()) { |
5680 | Value *Op; |
5681 | unsigned OpTypeID; |
5682 | if (getValueTypePair(Record, OpNum, NextValueNo, Op, OpTypeID, CurBB)) |
5683 | return error("Invalid record"); |
5684 | Args.push_back(Op); |
5685 | ArgTyIDs.push_back(OpTypeID); |
5686 | } |
5687 | } |
5688 | |
5689 | // Upgrade the bundles if needed. |
5690 | if (!OperandBundles.empty()) |
5691 | UpgradeOperandBundles(OperandBundles); |
5692 | |
5693 | if (auto *IA = dyn_cast<InlineAsm>(Callee)) { |
5694 | InlineAsm::ConstraintInfoVector ConstraintInfo = IA->ParseConstraints(); |
5695 | auto IsLabelConstraint = [](const InlineAsm::ConstraintInfo &CI) { |
5696 | return CI.Type == InlineAsm::isLabel; |
5697 | }; |
5698 | if (none_of(ConstraintInfo, IsLabelConstraint)) { |
5699 | // Upgrade explicit blockaddress arguments to label constraints. |
5700 | // Verify that the last arguments are blockaddress arguments that |
5701 | // match the indirect destinations. Clang always generates callbr |
5702 | // in this form. We could support reordering with more effort. |
5703 | unsigned FirstBlockArg = Args.size() - IndirectDests.size(); |
5704 | for (unsigned ArgNo = FirstBlockArg; ArgNo < Args.size(); ++ArgNo) { |
5705 | unsigned LabelNo = ArgNo - FirstBlockArg; |
5706 | auto *BA = dyn_cast<BlockAddress>(Args[ArgNo]); |
5707 | if (!BA || BA->getFunction() != F || |
5708 | LabelNo > IndirectDests.size() || |
5709 | BA->getBasicBlock() != IndirectDests[LabelNo]) |
5710 | return error("callbr argument does not match indirect dest"); |
5711 | } |
5712 | |
5713 | // Remove blockaddress arguments. |
5714 | Args.erase(Args.begin() + FirstBlockArg, Args.end()); |
5715 | ArgTyIDs.erase(ArgTyIDs.begin() + FirstBlockArg, ArgTyIDs.end()); |
5716 | |
5717 | // Recreate the function type with less arguments. |
5718 | SmallVector<Type *> ArgTys; |
5719 | for (Value *Arg : Args) |
5720 | ArgTys.push_back(Arg->getType()); |
5721 | FTy = |
5722 | FunctionType::get(FTy->getReturnType(), ArgTys, FTy->isVarArg()); |
5723 | |
5724 | // Update constraint string to use label constraints. |
5725 | std::string Constraints = IA->getConstraintString(); |
5726 | unsigned ArgNo = 0; |
5727 | size_t Pos = 0; |
5728 | for (const auto &CI : ConstraintInfo) { |
5729 | if (CI.hasArg()) { |
5730 | if (ArgNo >= FirstBlockArg) |
5731 | Constraints.insert(Pos, "!"); |
5732 | ++ArgNo; |
5733 | } |
5734 | |
5735 | // Go to next constraint in string. |
5736 | Pos = Constraints.find(',', Pos); |
5737 | if (Pos == std::string::npos) |
5738 | break; |
5739 | ++Pos; |
5740 | } |
5741 | |
5742 | Callee = InlineAsm::get(FTy, IA->getAsmString(), Constraints, |
5743 | IA->hasSideEffects(), IA->isAlignStack(), |
5744 | IA->getDialect(), IA->canThrow()); |
5745 | } |
5746 | } |
5747 | |
5748 | I = CallBrInst::Create(FTy, Callee, DefaultDest, IndirectDests, Args, |
5749 | OperandBundles); |
5750 | ResTypeID = getContainedTypeID(FTyID); |
5751 | OperandBundles.clear(); |
5752 | InstructionList.push_back(I); |
5753 | cast<CallBrInst>(I)->setCallingConv( |
5754 | static_cast<CallingConv::ID>((0x7ff & CCInfo) >> bitc::CALL_CCONV)); |
5755 | cast<CallBrInst>(I)->setAttributes(PAL); |
5756 | if (Error Err = propagateAttributeTypes(cast<CallBase>(I), ArgTyIDs)) { |
5757 | I->deleteValue(); |
5758 | return Err; |
5759 | } |
5760 | break; |
5761 | } |
5762 | case bitc::FUNC_CODE_INST_UNREACHABLE: // UNREACHABLE |
5763 | I = new UnreachableInst(Context); |
5764 | InstructionList.push_back(I); |
5765 | break; |
5766 | case bitc::FUNC_CODE_INST_PHI: { // PHI: [ty, val0,bb0, ...] |
5767 | if (Record.empty()) |
5768 | return error("Invalid phi record"); |
5769 | // The first record specifies the type. |
5770 | unsigned TyID = Record[0]; |
5771 | Type *Ty = getTypeByID(TyID); |
5772 | if (!Ty) |
5773 | return error("Invalid phi record"); |
5774 | |
5775 | // Phi arguments are pairs of records of [value, basic block]. |
5776 | // There is an optional final record for fast-math-flags if this phi has a |
5777 | // floating-point type. |
5778 | size_t NumArgs = (Record.size() - 1) / 2; |
5779 | PHINode *PN = PHINode::Create(Ty, NumArgs); |
5780 | if ((Record.size() - 1) % 2 == 1 && !isa<FPMathOperator>(PN)) { |
5781 | PN->deleteValue(); |
5782 | return error("Invalid phi record"); |
5783 | } |
5784 | InstructionList.push_back(PN); |
5785 | |
5786 | SmallDenseMap<BasicBlock *, Value *> Args; |
5787 | for (unsigned i = 0; i != NumArgs; i++) { |
5788 | BasicBlock *BB = getBasicBlock(Record[i * 2 + 2]); |
5789 | if (!BB) { |
5790 | PN->deleteValue(); |
5791 | return error("Invalid phi BB"); |
5792 | } |
5793 | |
5794 | // Phi nodes may contain the same predecessor multiple times, in which |
5795 | // case the incoming value must be identical. Directly reuse the already |
5796 | // seen value here, to avoid expanding a constant expression multiple |
5797 | // times. |
5798 | auto It = Args.find(BB); |
5799 | if (It != Args.end()) { |
5800 | PN->addIncoming(It->second, BB); |
5801 | continue; |
5802 | } |
5803 | |
5804 | // If there already is a block for this edge (from a different phi), |
5805 | // use it. |
5806 | BasicBlock *EdgeBB = ConstExprEdgeBBs.lookup({BB, CurBB}); |
5807 | if (!EdgeBB) { |
5808 | // Otherwise, use a temporary block (that we will discard if it |
5809 | // turns out to be unnecessary). |
5810 | if (!PhiConstExprBB) |
5811 | PhiConstExprBB = BasicBlock::Create(Context, "phi.constexpr", F); |
5812 | EdgeBB = PhiConstExprBB; |
5813 | } |
5814 | |
5815 | // With the new function encoding, it is possible that operands have |
5816 | // negative IDs (for forward references). Use a signed VBR |
5817 | // representation to keep the encoding small. |
5818 | Value *V; |
5819 | if (UseRelativeIDs) |
5820 | V = getValueSigned(Record, i * 2 + 1, NextValueNo, Ty, TyID, EdgeBB); |
5821 | else |
5822 | V = getValue(Record, i * 2 + 1, NextValueNo, Ty, TyID, EdgeBB); |
5823 | if (!V) { |
5824 | PN->deleteValue(); |
5825 | PhiConstExprBB->eraseFromParent(); |
5826 | return error("Invalid phi record"); |
5827 | } |
5828 | |
5829 | if (EdgeBB == PhiConstExprBB && !EdgeBB->empty()) { |
5830 | ConstExprEdgeBBs.insert({{BB, CurBB}, EdgeBB}); |
5831 | PhiConstExprBB = nullptr; |
5832 | } |
5833 | PN->addIncoming(V, BB); |
5834 | Args.insert({BB, V}); |
5835 | } |
5836 | I = PN; |
5837 | ResTypeID = TyID; |
5838 | |
5839 | // If there are an even number of records, the final record must be FMF. |
5840 | if (Record.size() % 2 == 0) { |
5841 | assert(isa<FPMathOperator>(I) && "Unexpected phi type")(static_cast <bool> (isa<FPMathOperator>(I) && "Unexpected phi type") ? void (0) : __assert_fail ("isa<FPMathOperator>(I) && \"Unexpected phi type\"" , "llvm/lib/Bitcode/Reader/BitcodeReader.cpp", 5841, __extension__ __PRETTY_FUNCTION__)); |
5842 | FastMathFlags FMF = getDecodedFastMathFlags(Record[Record.size() - 1]); |
5843 | if (FMF.any()) |
5844 | I->setFastMathFlags(FMF); |
5845 | } |
5846 | |
5847 | break; |
5848 | } |
5849 | |
5850 | case bitc::FUNC_CODE_INST_LANDINGPAD: |
5851 | case bitc::FUNC_CODE_INST_LANDINGPAD_OLD: { |
5852 | // LANDINGPAD: [ty, val, val, num, (id0,val0 ...)?] |
5853 | unsigned Idx = 0; |
5854 | if (BitCode == bitc::FUNC_CODE_INST_LANDINGPAD) { |
5855 | if (Record.size() < 3) |
5856 | return error("Invalid record"); |
5857 | } else { |
5858 | assert(BitCode == bitc::FUNC_CODE_INST_LANDINGPAD_OLD)(static_cast <bool> (BitCode == bitc::FUNC_CODE_INST_LANDINGPAD_OLD ) ? void (0) : __assert_fail ("BitCode == bitc::FUNC_CODE_INST_LANDINGPAD_OLD" , "llvm/lib/Bitcode/Reader/BitcodeReader.cpp", 5858, __extension__ __PRETTY_FUNCTION__)); |
5859 | if (Record.size() < 4) |
5860 | return error("Invalid record"); |
5861 | } |
5862 | ResTypeID = Record[Idx++]; |
5863 | Type *Ty = getTypeByID(ResTypeID); |
5864 | if (!Ty) |
5865 | return error("Invalid record"); |
5866 | if (BitCode == bitc::FUNC_CODE_INST_LANDINGPAD_OLD) { |
5867 | Value *PersFn = nullptr; |
5868 | unsigned PersFnTypeID; |
5869 | if (getValueTypePair(Record, Idx, NextValueNo, PersFn, PersFnTypeID, |
5870 | nullptr)) |
5871 | return error("Invalid record"); |
5872 | |
5873 | if (!F->hasPersonalityFn()) |
5874 | F->setPersonalityFn(cast<Constant>(PersFn)); |
5875 | else if (F->getPersonalityFn() != cast<Constant>(PersFn)) |
5876 | return error("Personality function mismatch"); |
5877 | } |
5878 | |
5879 | bool IsCleanup = !!Record[Idx++]; |
5880 | unsigned NumClauses = Record[Idx++]; |
5881 | LandingPadInst *LP = LandingPadInst::Create(Ty, NumClauses); |
5882 | LP->setCleanup(IsCleanup); |
5883 | for (unsigned J = 0; J != NumClauses; ++J) { |
5884 | LandingPadInst::ClauseType CT = |
5885 | LandingPadInst::ClauseType(Record[Idx++]); (void)CT; |
5886 | Value *Val; |
5887 | unsigned ValTypeID; |
5888 | |
5889 | if (getValueTypePair(Record, Idx, NextValueNo, Val, ValTypeID, |
5890 | nullptr)) { |
5891 | delete LP; |
5892 | return error("Invalid record"); |
5893 | } |
5894 | |
5895 | assert((CT != LandingPadInst::Catch ||(static_cast <bool> ((CT != LandingPadInst::Catch || !isa <ArrayType>(Val->getType())) && "Catch clause has a invalid type!" ) ? void (0) : __assert_fail ("(CT != LandingPadInst::Catch || !isa<ArrayType>(Val->getType())) && \"Catch clause has a invalid type!\"" , "llvm/lib/Bitcode/Reader/BitcodeReader.cpp", 5897, __extension__ __PRETTY_FUNCTION__)) |
5896 | !isa<ArrayType>(Val->getType())) &&(static_cast <bool> ((CT != LandingPadInst::Catch || !isa <ArrayType>(Val->getType())) && "Catch clause has a invalid type!" ) ? void (0) : __assert_fail ("(CT != LandingPadInst::Catch || !isa<ArrayType>(Val->getType())) && \"Catch clause has a invalid type!\"" , "llvm/lib/Bitcode/Reader/BitcodeReader.cpp", 5897, __extension__ __PRETTY_FUNCTION__)) |
5897 | "Catch clause has a invalid type!")(static_cast <bool> ((CT != LandingPadInst::Catch || !isa <ArrayType>(Val->getType())) && "Catch clause has a invalid type!" ) ? void (0) : __assert_fail ("(CT != LandingPadInst::Catch || !isa<ArrayType>(Val->getType())) && \"Catch clause has a invalid type!\"" , "llvm/lib/Bitcode/Reader/BitcodeReader.cpp", 5897, __extension__ __PRETTY_FUNCTION__)); |
5898 | assert((CT != LandingPadInst::Filter ||(static_cast <bool> ((CT != LandingPadInst::Filter || isa <ArrayType>(Val->getType())) && "Filter clause has invalid type!" ) ? void (0) : __assert_fail ("(CT != LandingPadInst::Filter || isa<ArrayType>(Val->getType())) && \"Filter clause has invalid type!\"" , "llvm/lib/Bitcode/Reader/BitcodeReader.cpp", 5900, __extension__ __PRETTY_FUNCTION__)) |
5899 | isa<ArrayType>(Val->getType())) &&(static_cast <bool> ((CT != LandingPadInst::Filter || isa <ArrayType>(Val->getType())) && "Filter clause has invalid type!" ) ? void (0) : __assert_fail ("(CT != LandingPadInst::Filter || isa<ArrayType>(Val->getType())) && \"Filter clause has invalid type!\"" , "llvm/lib/Bitcode/Reader/BitcodeReader.cpp", 5900, __extension__ __PRETTY_FUNCTION__)) |
5900 | "Filter clause has invalid type!")(static_cast <bool> ((CT != LandingPadInst::Filter || isa <ArrayType>(Val->getType())) && "Filter clause has invalid type!" ) ? void (0) : __assert_fail ("(CT != LandingPadInst::Filter || isa<ArrayType>(Val->getType())) && \"Filter clause has invalid type!\"" , "llvm/lib/Bitcode/Reader/BitcodeReader.cpp", 5900, __extension__ __PRETTY_FUNCTION__)); |
5901 | LP->addClause(cast<Constant>(Val)); |
5902 | } |
5903 | |
5904 | I = LP; |
5905 | InstructionList.push_back(I); |
5906 | break; |
5907 | } |
5908 | |
5909 | case bitc::FUNC_CODE_INST_ALLOCA: { // ALLOCA: [instty, opty, op, align] |
5910 | if (Record.size() != 4 && Record.size() != 5) |
5911 | return error("Invalid record"); |
5912 | using APV = AllocaPackedValues; |
5913 | const uint64_t Rec = Record[3]; |
5914 | const bool InAlloca = Bitfield::get<APV::UsedWithInAlloca>(Rec); |
5915 | const bool SwiftError = Bitfield::get<APV::SwiftError>(Rec); |
5916 | unsigned TyID = Record[0]; |
5917 | Type *Ty = getTypeByID(TyID); |
5918 | if (!Bitfield::get<APV::ExplicitType>(Rec)) { |
5919 | TyID = getContainedTypeID(TyID); |
5920 | Ty = getTypeByID(TyID); |
5921 | if (!Ty) |
5922 | return error("Missing element type for old-style alloca"); |
5923 | } |
5924 | unsigned OpTyID = Record[1]; |
5925 | Type *OpTy = getTypeByID(OpTyID); |
5926 | Value *Size = getFnValueByID(Record[2], OpTy, OpTyID, CurBB); |
5927 | MaybeAlign Align; |
5928 | uint64_t AlignExp = |
5929 | Bitfield::get<APV::AlignLower>(Rec) | |
5930 | (Bitfield::get<APV::AlignUpper>(Rec) << APV::AlignLower::Bits); |
5931 | if (Error Err = parseAlignmentValue(AlignExp, Align)) { |
5932 | return Err; |
5933 | } |
5934 | if (!Ty || !Size) |
5935 | return error("Invalid record"); |
5936 | |
5937 | const DataLayout &DL = TheModule->getDataLayout(); |
5938 | unsigned AS = Record.size() == 5 ? Record[4] : DL.getAllocaAddrSpace(); |
5939 | |
5940 | SmallPtrSet<Type *, 4> Visited; |
5941 | if (!Align && !Ty->isSized(&Visited)) |
5942 | return error("alloca of unsized type"); |
5943 | if (!Align) |
5944 | Align = DL.getPrefTypeAlign(Ty); |
5945 | |
5946 | AllocaInst *AI = new AllocaInst(Ty, AS, Size, *Align); |
5947 | AI->setUsedWithInAlloca(InAlloca); |
5948 | AI->setSwiftError(SwiftError); |
5949 | I = AI; |
5950 | ResTypeID = getVirtualTypeID(AI->getType(), TyID); |
5951 | InstructionList.push_back(I); |
5952 | break; |
5953 | } |
5954 | case bitc::FUNC_CODE_INST_LOAD: { // LOAD: [opty, op, align, vol] |
5955 | unsigned OpNum = 0; |
5956 | Value *Op; |
5957 | unsigned OpTypeID; |
5958 | if (getValueTypePair(Record, OpNum, NextValueNo, Op, OpTypeID, CurBB) || |
5959 | (OpNum + 2 != Record.size() && OpNum + 3 != Record.size())) |
5960 | return error("Invalid record"); |
5961 | |
5962 | if (!isa<PointerType>(Op->getType())) |
5963 | return error("Load operand is not a pointer type"); |
5964 | |
5965 | Type *Ty = nullptr; |
5966 | if (OpNum + 3 == Record.size()) { |
5967 | ResTypeID = Record[OpNum++]; |
5968 | Ty = getTypeByID(ResTypeID); |
5969 | } else { |
5970 | ResTypeID = getContainedTypeID(OpTypeID); |
5971 | Ty = getTypeByID(ResTypeID); |
5972 | if (!Ty) |
5973 | return error("Missing element type for old-style load"); |
5974 | } |
5975 | |
5976 | if (Error Err = typeCheckLoadStoreInst(Ty, Op->getType())) |
5977 | return Err; |
5978 | |
5979 | MaybeAlign Align; |
5980 | if (Error Err = parseAlignmentValue(Record[OpNum], Align)) |
5981 | return Err; |
5982 | SmallPtrSet<Type *, 4> Visited; |
5983 | if (!Align && !Ty->isSized(&Visited)) |
5984 | return error("load of unsized type"); |
5985 | if (!Align) |
5986 | Align = TheModule->getDataLayout().getABITypeAlign(Ty); |
5987 | I = new LoadInst(Ty, Op, "", Record[OpNum + 1], *Align); |
5988 | InstructionList.push_back(I); |
5989 | break; |
5990 | } |
5991 | case bitc::FUNC_CODE_INST_LOADATOMIC: { |
5992 | // LOADATOMIC: [opty, op, align, vol, ordering, ssid] |
5993 | unsigned OpNum = 0; |
5994 | Value *Op; |
5995 | unsigned OpTypeID; |
5996 | if (getValueTypePair(Record, OpNum, NextValueNo, Op, OpTypeID, CurBB) || |
5997 | (OpNum + 4 != Record.size() && OpNum + 5 != Record.size())) |
5998 | return error("Invalid record"); |
5999 | |
6000 | if (!isa<PointerType>(Op->getType())) |
6001 | return error("Load operand is not a pointer type"); |
6002 | |
6003 | Type *Ty = nullptr; |
6004 | if (OpNum + 5 == Record.size()) { |
6005 | ResTypeID = Record[OpNum++]; |
6006 | Ty = getTypeByID(ResTypeID); |