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/AttributeMask.h" |
27 | #include "llvm/IR/Attributes.h" |
28 | #include "llvm/IR/AutoUpgrade.h" |
29 | #include "llvm/IR/BasicBlock.h" |
30 | #include "llvm/IR/CallingConv.h" |
31 | #include "llvm/IR/Comdat.h" |
32 | #include "llvm/IR/Constant.h" |
33 | #include "llvm/IR/Constants.h" |
34 | #include "llvm/IR/DataLayout.h" |
35 | #include "llvm/IR/DebugInfo.h" |
36 | #include "llvm/IR/DebugInfoMetadata.h" |
37 | #include "llvm/IR/DebugLoc.h" |
38 | #include "llvm/IR/DerivedTypes.h" |
39 | #include "llvm/IR/Function.h" |
40 | #include "llvm/IR/GVMaterializer.h" |
41 | #include "llvm/IR/GetElementPtrTypeIterator.h" |
42 | #include "llvm/IR/GlobalAlias.h" |
43 | #include "llvm/IR/GlobalIFunc.h" |
44 | #include "llvm/IR/GlobalObject.h" |
45 | #include "llvm/IR/GlobalValue.h" |
46 | #include "llvm/IR/GlobalVariable.h" |
47 | #include "llvm/IR/InlineAsm.h" |
48 | #include "llvm/IR/InstIterator.h" |
49 | #include "llvm/IR/InstrTypes.h" |
50 | #include "llvm/IR/Instruction.h" |
51 | #include "llvm/IR/Instructions.h" |
52 | #include "llvm/IR/Intrinsics.h" |
53 | #include "llvm/IR/IntrinsicsAArch64.h" |
54 | #include "llvm/IR/IntrinsicsARM.h" |
55 | #include "llvm/IR/LLVMContext.h" |
56 | #include "llvm/IR/Metadata.h" |
57 | #include "llvm/IR/Module.h" |
58 | #include "llvm/IR/ModuleSummaryIndex.h" |
59 | #include "llvm/IR/Operator.h" |
60 | #include "llvm/IR/Type.h" |
61 | #include "llvm/IR/Value.h" |
62 | #include "llvm/IR/Verifier.h" |
63 | #include "llvm/Support/AtomicOrdering.h" |
64 | #include "llvm/Support/Casting.h" |
65 | #include "llvm/Support/CommandLine.h" |
66 | #include "llvm/Support/Compiler.h" |
67 | #include "llvm/Support/Debug.h" |
68 | #include "llvm/Support/Error.h" |
69 | #include "llvm/Support/ErrorHandling.h" |
70 | #include "llvm/Support/ErrorOr.h" |
71 | #include "llvm/Support/MathExtras.h" |
72 | #include "llvm/Support/MemoryBuffer.h" |
73 | #include "llvm/Support/ModRef.h" |
74 | #include "llvm/Support/raw_ostream.h" |
75 | #include "llvm/TargetParser/Triple.h" |
76 | #include <algorithm> |
77 | #include <cassert> |
78 | #include <cstddef> |
79 | #include <cstdint> |
80 | #include <deque> |
81 | #include <map> |
82 | #include <memory> |
83 | #include <optional> |
84 | #include <set> |
85 | #include <string> |
86 | #include <system_error> |
87 | #include <tuple> |
88 | #include <utility> |
89 | #include <vector> |
90 | |
91 | using namespace llvm; |
92 | |
93 | static cl::opt<bool> PrintSummaryGUIDs( |
94 | "print-summary-global-ids" , cl::init(Val: false), cl::Hidden, |
95 | cl::desc( |
96 | "Print the global id for each value when reading the module summary" )); |
97 | |
98 | static cl::opt<bool> ExpandConstantExprs( |
99 | "expand-constant-exprs" , cl::Hidden, |
100 | cl::desc( |
101 | "Expand constant expressions to instructions for testing purposes" )); |
102 | |
103 | /// Load bitcode directly into RemoveDIs format (use debug records instead |
104 | /// of debug intrinsics). UNSET is treated as FALSE, so the default action |
105 | /// is to do nothing. Individual tools can override this to incrementally add |
106 | /// support for the RemoveDIs format. |
107 | cl::opt<cl::boolOrDefault> LoadBitcodeIntoNewDbgInfoFormat( |
108 | "load-bitcode-into-experimental-debuginfo-iterators" , cl::Hidden, |
109 | cl::desc("Load bitcode directly into the new debug info format (regardless " |
110 | "of input format)" )); |
111 | extern cl::opt<cl::boolOrDefault> PreserveInputDbgFormat; |
112 | extern bool WriteNewDbgInfoFormatToBitcode; |
113 | extern cl::opt<bool> WriteNewDbgInfoFormat; |
114 | |
115 | namespace { |
116 | |
117 | enum { |
118 | SWITCH_INST_MAGIC = 0x4B5 // May 2012 => 1205 => Hex |
119 | }; |
120 | |
121 | } // end anonymous namespace |
122 | |
123 | static Error error(const Twine &Message) { |
124 | return make_error<StringError>( |
125 | Args: Message, Args: make_error_code(E: BitcodeError::CorruptedBitcode)); |
126 | } |
127 | |
128 | static Error (BitstreamCursor &Stream) { |
129 | if (!Stream.canSkipToPos(pos: 4)) |
130 | return createStringError(EC: std::errc::illegal_byte_sequence, |
131 | Fmt: "file too small to contain bitcode header" ); |
132 | for (unsigned C : {'B', 'C'}) |
133 | if (Expected<SimpleBitstreamCursor::word_t> Res = Stream.Read(NumBits: 8)) { |
134 | if (Res.get() != C) |
135 | return createStringError(EC: std::errc::illegal_byte_sequence, |
136 | Fmt: "file doesn't start with bitcode header" ); |
137 | } else |
138 | return Res.takeError(); |
139 | for (unsigned C : {0x0, 0xC, 0xE, 0xD}) |
140 | if (Expected<SimpleBitstreamCursor::word_t> Res = Stream.Read(NumBits: 4)) { |
141 | if (Res.get() != C) |
142 | return createStringError(EC: std::errc::illegal_byte_sequence, |
143 | Fmt: "file doesn't start with bitcode header" ); |
144 | } else |
145 | return Res.takeError(); |
146 | return Error::success(); |
147 | } |
148 | |
149 | static Expected<BitstreamCursor> initStream(MemoryBufferRef Buffer) { |
150 | const unsigned char *BufPtr = (const unsigned char *)Buffer.getBufferStart(); |
151 | const unsigned char *BufEnd = BufPtr + Buffer.getBufferSize(); |
152 | |
153 | if (Buffer.getBufferSize() & 3) |
154 | return error(Message: "Invalid bitcode signature" ); |
155 | |
156 | // If we have a wrapper header, parse it and ignore the non-bc file contents. |
157 | // The magic number is 0x0B17C0DE stored in little endian. |
158 | if (isBitcodeWrapper(BufPtr, BufEnd)) |
159 | if (SkipBitcodeWrapperHeader(BufPtr, BufEnd, VerifyBufferSize: true)) |
160 | return error(Message: "Invalid bitcode wrapper header" ); |
161 | |
162 | BitstreamCursor Stream(ArrayRef<uint8_t>(BufPtr, BufEnd)); |
163 | if (Error Err = hasInvalidBitcodeHeader(Stream)) |
164 | return std::move(Err); |
165 | |
166 | return std::move(Stream); |
167 | } |
168 | |
169 | /// Convert a string from a record into an std::string, return true on failure. |
170 | template <typename StrTy> |
171 | static bool convertToString(ArrayRef<uint64_t> Record, unsigned Idx, |
172 | StrTy &Result) { |
173 | if (Idx > Record.size()) |
174 | return true; |
175 | |
176 | Result.append(Record.begin() + Idx, Record.end()); |
177 | return false; |
178 | } |
179 | |
180 | // Strip all the TBAA attachment for the module. |
181 | static void stripTBAA(Module *M) { |
182 | for (auto &F : *M) { |
183 | if (F.isMaterializable()) |
184 | continue; |
185 | for (auto &I : instructions(F)) |
186 | I.setMetadata(KindID: LLVMContext::MD_tbaa, Node: nullptr); |
187 | } |
188 | } |
189 | |
190 | /// Read the "IDENTIFICATION_BLOCK_ID" block, do some basic enforcement on the |
191 | /// "epoch" encoded in the bitcode, and return the producer name if any. |
192 | static Expected<std::string> readIdentificationBlock(BitstreamCursor &Stream) { |
193 | if (Error Err = Stream.EnterSubBlock(BlockID: bitc::IDENTIFICATION_BLOCK_ID)) |
194 | return std::move(Err); |
195 | |
196 | // Read all the records. |
197 | SmallVector<uint64_t, 64> Record; |
198 | |
199 | std::string ProducerIdentification; |
200 | |
201 | while (true) { |
202 | BitstreamEntry Entry; |
203 | if (Error E = Stream.advance().moveInto(Value&: Entry)) |
204 | return std::move(E); |
205 | |
206 | switch (Entry.Kind) { |
207 | default: |
208 | case BitstreamEntry::Error: |
209 | return error(Message: "Malformed block" ); |
210 | case BitstreamEntry::EndBlock: |
211 | return ProducerIdentification; |
212 | case BitstreamEntry::Record: |
213 | // The interesting case. |
214 | break; |
215 | } |
216 | |
217 | // Read a record. |
218 | Record.clear(); |
219 | Expected<unsigned> MaybeBitCode = Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record); |
220 | if (!MaybeBitCode) |
221 | return MaybeBitCode.takeError(); |
222 | switch (MaybeBitCode.get()) { |
223 | default: // Default behavior: reject |
224 | return error(Message: "Invalid value" ); |
225 | case bitc::IDENTIFICATION_CODE_STRING: // IDENTIFICATION: [strchr x N] |
226 | convertToString(Record, Idx: 0, Result&: ProducerIdentification); |
227 | break; |
228 | case bitc::IDENTIFICATION_CODE_EPOCH: { // EPOCH: [epoch#] |
229 | unsigned epoch = (unsigned)Record[0]; |
230 | if (epoch != bitc::BITCODE_CURRENT_EPOCH) { |
231 | return error( |
232 | Message: Twine("Incompatible epoch: Bitcode '" ) + Twine(epoch) + |
233 | "' vs current: '" + Twine(bitc::BITCODE_CURRENT_EPOCH) + "'" ); |
234 | } |
235 | } |
236 | } |
237 | } |
238 | } |
239 | |
240 | static Expected<std::string> readIdentificationCode(BitstreamCursor &Stream) { |
241 | // We expect a number of well-defined blocks, though we don't necessarily |
242 | // need to understand them all. |
243 | while (true) { |
244 | if (Stream.AtEndOfStream()) |
245 | return "" ; |
246 | |
247 | BitstreamEntry Entry; |
248 | if (Error E = Stream.advance().moveInto(Value&: Entry)) |
249 | return std::move(E); |
250 | |
251 | switch (Entry.Kind) { |
252 | case BitstreamEntry::EndBlock: |
253 | case BitstreamEntry::Error: |
254 | return error(Message: "Malformed block" ); |
255 | |
256 | case BitstreamEntry::SubBlock: |
257 | if (Entry.ID == bitc::IDENTIFICATION_BLOCK_ID) |
258 | return readIdentificationBlock(Stream); |
259 | |
260 | // Ignore other sub-blocks. |
261 | if (Error Err = Stream.SkipBlock()) |
262 | return std::move(Err); |
263 | continue; |
264 | case BitstreamEntry::Record: |
265 | if (Error E = Stream.skipRecord(AbbrevID: Entry.ID).takeError()) |
266 | return std::move(E); |
267 | continue; |
268 | } |
269 | } |
270 | } |
271 | |
272 | static Expected<bool> hasObjCCategoryInModule(BitstreamCursor &Stream) { |
273 | if (Error Err = Stream.EnterSubBlock(BlockID: bitc::MODULE_BLOCK_ID)) |
274 | return std::move(Err); |
275 | |
276 | SmallVector<uint64_t, 64> Record; |
277 | // Read all the records for this module. |
278 | |
279 | while (true) { |
280 | Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks(); |
281 | if (!MaybeEntry) |
282 | return MaybeEntry.takeError(); |
283 | BitstreamEntry Entry = MaybeEntry.get(); |
284 | |
285 | switch (Entry.Kind) { |
286 | case BitstreamEntry::SubBlock: // Handled for us already. |
287 | case BitstreamEntry::Error: |
288 | return error(Message: "Malformed block" ); |
289 | case BitstreamEntry::EndBlock: |
290 | return false; |
291 | case BitstreamEntry::Record: |
292 | // The interesting case. |
293 | break; |
294 | } |
295 | |
296 | // Read a record. |
297 | Expected<unsigned> MaybeRecord = Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record); |
298 | if (!MaybeRecord) |
299 | return MaybeRecord.takeError(); |
300 | switch (MaybeRecord.get()) { |
301 | default: |
302 | break; // Default behavior, ignore unknown content. |
303 | case bitc::MODULE_CODE_SECTIONNAME: { // SECTIONNAME: [strchr x N] |
304 | std::string S; |
305 | if (convertToString(Record, Idx: 0, Result&: S)) |
306 | return error(Message: "Invalid section name record" ); |
307 | // Check for the i386 and other (x86_64, ARM) conventions |
308 | if (S.find(s: "__DATA,__objc_catlist" ) != std::string::npos || |
309 | S.find(s: "__OBJC,__category" ) != std::string::npos) |
310 | return true; |
311 | break; |
312 | } |
313 | } |
314 | Record.clear(); |
315 | } |
316 | llvm_unreachable("Exit infinite loop" ); |
317 | } |
318 | |
319 | static Expected<bool> hasObjCCategory(BitstreamCursor &Stream) { |
320 | // We expect a number of well-defined blocks, though we don't necessarily |
321 | // need to understand them all. |
322 | while (true) { |
323 | BitstreamEntry Entry; |
324 | if (Error E = Stream.advance().moveInto(Value&: Entry)) |
325 | return std::move(E); |
326 | |
327 | switch (Entry.Kind) { |
328 | case BitstreamEntry::Error: |
329 | return error(Message: "Malformed block" ); |
330 | case BitstreamEntry::EndBlock: |
331 | return false; |
332 | |
333 | case BitstreamEntry::SubBlock: |
334 | if (Entry.ID == bitc::MODULE_BLOCK_ID) |
335 | return hasObjCCategoryInModule(Stream); |
336 | |
337 | // Ignore other sub-blocks. |
338 | if (Error Err = Stream.SkipBlock()) |
339 | return std::move(Err); |
340 | continue; |
341 | |
342 | case BitstreamEntry::Record: |
343 | if (Error E = Stream.skipRecord(AbbrevID: Entry.ID).takeError()) |
344 | return std::move(E); |
345 | continue; |
346 | } |
347 | } |
348 | } |
349 | |
350 | static Expected<std::string> readModuleTriple(BitstreamCursor &Stream) { |
351 | if (Error Err = Stream.EnterSubBlock(BlockID: bitc::MODULE_BLOCK_ID)) |
352 | return std::move(Err); |
353 | |
354 | SmallVector<uint64_t, 64> Record; |
355 | |
356 | std::string Triple; |
357 | |
358 | // Read all the records for this module. |
359 | while (true) { |
360 | Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks(); |
361 | if (!MaybeEntry) |
362 | return MaybeEntry.takeError(); |
363 | BitstreamEntry Entry = MaybeEntry.get(); |
364 | |
365 | switch (Entry.Kind) { |
366 | case BitstreamEntry::SubBlock: // Handled for us already. |
367 | case BitstreamEntry::Error: |
368 | return error(Message: "Malformed block" ); |
369 | case BitstreamEntry::EndBlock: |
370 | return Triple; |
371 | case BitstreamEntry::Record: |
372 | // The interesting case. |
373 | break; |
374 | } |
375 | |
376 | // Read a record. |
377 | Expected<unsigned> MaybeRecord = Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record); |
378 | if (!MaybeRecord) |
379 | return MaybeRecord.takeError(); |
380 | switch (MaybeRecord.get()) { |
381 | default: break; // Default behavior, ignore unknown content. |
382 | case bitc::MODULE_CODE_TRIPLE: { // TRIPLE: [strchr x N] |
383 | std::string S; |
384 | if (convertToString(Record, Idx: 0, Result&: S)) |
385 | return error(Message: "Invalid triple record" ); |
386 | Triple = S; |
387 | break; |
388 | } |
389 | } |
390 | Record.clear(); |
391 | } |
392 | llvm_unreachable("Exit infinite loop" ); |
393 | } |
394 | |
395 | static Expected<std::string> readTriple(BitstreamCursor &Stream) { |
396 | // We expect a number of well-defined blocks, though we don't necessarily |
397 | // need to understand them all. |
398 | while (true) { |
399 | Expected<BitstreamEntry> MaybeEntry = Stream.advance(); |
400 | if (!MaybeEntry) |
401 | return MaybeEntry.takeError(); |
402 | BitstreamEntry Entry = MaybeEntry.get(); |
403 | |
404 | switch (Entry.Kind) { |
405 | case BitstreamEntry::Error: |
406 | return error(Message: "Malformed block" ); |
407 | case BitstreamEntry::EndBlock: |
408 | return "" ; |
409 | |
410 | case BitstreamEntry::SubBlock: |
411 | if (Entry.ID == bitc::MODULE_BLOCK_ID) |
412 | return readModuleTriple(Stream); |
413 | |
414 | // Ignore other sub-blocks. |
415 | if (Error Err = Stream.SkipBlock()) |
416 | return std::move(Err); |
417 | continue; |
418 | |
419 | case BitstreamEntry::Record: |
420 | if (llvm::Expected<unsigned> Skipped = Stream.skipRecord(AbbrevID: Entry.ID)) |
421 | continue; |
422 | else |
423 | return Skipped.takeError(); |
424 | } |
425 | } |
426 | } |
427 | |
428 | namespace { |
429 | |
430 | class BitcodeReaderBase { |
431 | protected: |
432 | BitcodeReaderBase(BitstreamCursor Stream, StringRef Strtab) |
433 | : Stream(std::move(Stream)), Strtab(Strtab) { |
434 | this->Stream.setBlockInfo(&BlockInfo); |
435 | } |
436 | |
437 | BitstreamBlockInfo BlockInfo; |
438 | BitstreamCursor Stream; |
439 | StringRef Strtab; |
440 | |
441 | /// In version 2 of the bitcode we store names of global values and comdats in |
442 | /// a string table rather than in the VST. |
443 | bool UseStrtab = false; |
444 | |
445 | Expected<unsigned> parseVersionRecord(ArrayRef<uint64_t> Record); |
446 | |
447 | /// If this module uses a string table, pop the reference to the string table |
448 | /// and return the referenced string and the rest of the record. Otherwise |
449 | /// just return the record itself. |
450 | std::pair<StringRef, ArrayRef<uint64_t>> |
451 | readNameFromStrtab(ArrayRef<uint64_t> Record); |
452 | |
453 | Error readBlockInfo(); |
454 | |
455 | // Contains an arbitrary and optional string identifying the bitcode producer |
456 | std::string ProducerIdentification; |
457 | |
458 | Error error(const Twine &Message); |
459 | }; |
460 | |
461 | } // end anonymous namespace |
462 | |
463 | Error BitcodeReaderBase::error(const Twine &Message) { |
464 | std::string FullMsg = Message.str(); |
465 | if (!ProducerIdentification.empty()) |
466 | FullMsg += " (Producer: '" + ProducerIdentification + "' Reader: 'LLVM " + |
467 | LLVM_VERSION_STRING "')" ; |
468 | return ::error(Message: FullMsg); |
469 | } |
470 | |
471 | Expected<unsigned> |
472 | BitcodeReaderBase::parseVersionRecord(ArrayRef<uint64_t> Record) { |
473 | if (Record.empty()) |
474 | return error(Message: "Invalid version record" ); |
475 | unsigned ModuleVersion = Record[0]; |
476 | if (ModuleVersion > 2) |
477 | return error(Message: "Invalid value" ); |
478 | UseStrtab = ModuleVersion >= 2; |
479 | return ModuleVersion; |
480 | } |
481 | |
482 | std::pair<StringRef, ArrayRef<uint64_t>> |
483 | BitcodeReaderBase::readNameFromStrtab(ArrayRef<uint64_t> Record) { |
484 | if (!UseStrtab) |
485 | return {"" , Record}; |
486 | // Invalid reference. Let the caller complain about the record being empty. |
487 | if (Record[0] + Record[1] > Strtab.size()) |
488 | return {"" , {}}; |
489 | return {StringRef(Strtab.data() + Record[0], Record[1]), Record.slice(N: 2)}; |
490 | } |
491 | |
492 | namespace { |
493 | |
494 | /// This represents a constant expression or constant aggregate using a custom |
495 | /// structure internal to the bitcode reader. Later, this structure will be |
496 | /// expanded by materializeValue() either into a constant expression/aggregate, |
497 | /// or into an instruction sequence at the point of use. This allows us to |
498 | /// upgrade bitcode using constant expressions even if this kind of constant |
499 | /// expression is no longer supported. |
500 | class BitcodeConstant final : public Value, |
501 | TrailingObjects<BitcodeConstant, unsigned> { |
502 | friend TrailingObjects; |
503 | |
504 | // Value subclass ID: Pick largest possible value to avoid any clashes. |
505 | static constexpr uint8_t SubclassID = 255; |
506 | |
507 | public: |
508 | // Opcodes used for non-expressions. This includes constant aggregates |
509 | // (struct, array, vector) that might need expansion, as well as non-leaf |
510 | // constants that don't need expansion (no_cfi, dso_local, blockaddress), |
511 | // but still go through BitcodeConstant to avoid different uselist orders |
512 | // between the two cases. |
513 | static constexpr uint8_t ConstantStructOpcode = 255; |
514 | static constexpr uint8_t ConstantArrayOpcode = 254; |
515 | static constexpr uint8_t ConstantVectorOpcode = 253; |
516 | static constexpr uint8_t NoCFIOpcode = 252; |
517 | static constexpr uint8_t DSOLocalEquivalentOpcode = 251; |
518 | static constexpr uint8_t BlockAddressOpcode = 250; |
519 | static constexpr uint8_t FirstSpecialOpcode = BlockAddressOpcode; |
520 | |
521 | // Separate struct to make passing different number of parameters to |
522 | // BitcodeConstant::create() more convenient. |
523 | struct { |
524 | uint8_t ; |
525 | uint8_t ; |
526 | unsigned = 0; |
527 | Type * = nullptr; |
528 | std::optional<ConstantRange> ; |
529 | |
530 | (uint8_t Opcode, uint8_t Flags = 0, Type *SrcElemTy = nullptr, |
531 | std::optional<ConstantRange> InRange = std::nullopt) |
532 | : Opcode(Opcode), Flags(Flags), SrcElemTy(SrcElemTy), |
533 | InRange(std::move(InRange)) {} |
534 | |
535 | (uint8_t Opcode, uint8_t Flags, unsigned BlockAddressBB) |
536 | : Opcode(Opcode), Flags(Flags), BlockAddressBB(BlockAddressBB) {} |
537 | }; |
538 | |
539 | uint8_t Opcode; |
540 | uint8_t Flags; |
541 | unsigned NumOperands; |
542 | unsigned BlockAddressBB; |
543 | Type *SrcElemTy; // GEP source element type. |
544 | std::optional<ConstantRange> InRange; // GEP inrange attribute. |
545 | |
546 | private: |
547 | (Type *Ty, const ExtraInfo &Info, ArrayRef<unsigned> OpIDs) |
548 | : Value(Ty, SubclassID), Opcode(Info.Opcode), Flags(Info.Flags), |
549 | NumOperands(OpIDs.size()), BlockAddressBB(Info.BlockAddressBB), |
550 | SrcElemTy(Info.SrcElemTy), InRange(Info.InRange) { |
551 | std::uninitialized_copy(first: OpIDs.begin(), last: OpIDs.end(), |
552 | result: getTrailingObjects<unsigned>()); |
553 | } |
554 | |
555 | BitcodeConstant &operator=(const BitcodeConstant &) = delete; |
556 | |
557 | public: |
558 | static BitcodeConstant *(BumpPtrAllocator &A, Type *Ty, |
559 | const ExtraInfo &Info, |
560 | ArrayRef<unsigned> OpIDs) { |
561 | void *Mem = A.Allocate(Size: totalSizeToAlloc<unsigned>(Counts: OpIDs.size()), |
562 | Alignment: alignof(BitcodeConstant)); |
563 | return new (Mem) BitcodeConstant(Ty, Info, OpIDs); |
564 | } |
565 | |
566 | static bool classof(const Value *V) { return V->getValueID() == SubclassID; } |
567 | |
568 | ArrayRef<unsigned> getOperandIDs() const { |
569 | return ArrayRef(getTrailingObjects<unsigned>(), NumOperands); |
570 | } |
571 | |
572 | std::optional<ConstantRange> getInRange() const { |
573 | assert(Opcode == Instruction::GetElementPtr); |
574 | return InRange; |
575 | } |
576 | |
577 | const char *getOpcodeName() const { |
578 | return Instruction::getOpcodeName(Opcode); |
579 | } |
580 | }; |
581 | |
582 | class BitcodeReader : public BitcodeReaderBase, public GVMaterializer { |
583 | LLVMContext &Context; |
584 | Module *TheModule = nullptr; |
585 | // Next offset to start scanning for lazy parsing of function bodies. |
586 | uint64_t NextUnreadBit = 0; |
587 | // Last function offset found in the VST. |
588 | uint64_t LastFunctionBlockBit = 0; |
589 | bool SeenValueSymbolTable = false; |
590 | uint64_t VSTOffset = 0; |
591 | |
592 | std::vector<std::string> SectionTable; |
593 | std::vector<std::string> GCTable; |
594 | |
595 | std::vector<Type *> TypeList; |
596 | /// Track type IDs of contained types. Order is the same as the contained |
597 | /// types of a Type*. This is used during upgrades of typed pointer IR in |
598 | /// opaque pointer mode. |
599 | DenseMap<unsigned, SmallVector<unsigned, 1>> ContainedTypeIDs; |
600 | /// In some cases, we need to create a type ID for a type that was not |
601 | /// explicitly encoded in the bitcode, or we don't know about at the current |
602 | /// point. For example, a global may explicitly encode the value type ID, but |
603 | /// not have a type ID for the pointer to value type, for which we create a |
604 | /// virtual type ID instead. This map stores the new type ID that was created |
605 | /// for the given pair of Type and contained type ID. |
606 | DenseMap<std::pair<Type *, unsigned>, unsigned> VirtualTypeIDs; |
607 | DenseMap<Function *, unsigned> FunctionTypeIDs; |
608 | /// Allocator for BitcodeConstants. This should come before ValueList, |
609 | /// because the ValueList might hold ValueHandles to these constants, so |
610 | /// ValueList must be destroyed before Alloc. |
611 | BumpPtrAllocator Alloc; |
612 | BitcodeReaderValueList ValueList; |
613 | std::optional<MetadataLoader> MDLoader; |
614 | std::vector<Comdat *> ComdatList; |
615 | DenseSet<GlobalObject *> ImplicitComdatObjects; |
616 | SmallVector<Instruction *, 64> InstructionList; |
617 | |
618 | std::vector<std::pair<GlobalVariable *, unsigned>> GlobalInits; |
619 | std::vector<std::pair<GlobalValue *, unsigned>> IndirectSymbolInits; |
620 | |
621 | struct FunctionOperandInfo { |
622 | Function *F; |
623 | unsigned PersonalityFn; |
624 | unsigned Prefix; |
625 | unsigned Prologue; |
626 | }; |
627 | std::vector<FunctionOperandInfo> FunctionOperands; |
628 | |
629 | /// The set of attributes by index. Index zero in the file is for null, and |
630 | /// is thus not represented here. As such all indices are off by one. |
631 | std::vector<AttributeList> MAttributes; |
632 | |
633 | /// The set of attribute groups. |
634 | std::map<unsigned, AttributeList> MAttributeGroups; |
635 | |
636 | /// While parsing a function body, this is a list of the basic blocks for the |
637 | /// function. |
638 | std::vector<BasicBlock*> FunctionBBs; |
639 | |
640 | // When reading the module header, this list is populated with functions that |
641 | // have bodies later in the file. |
642 | std::vector<Function*> FunctionsWithBodies; |
643 | |
644 | // When intrinsic functions are encountered which require upgrading they are |
645 | // stored here with their replacement function. |
646 | using UpdatedIntrinsicMap = DenseMap<Function *, Function *>; |
647 | UpdatedIntrinsicMap UpgradedIntrinsics; |
648 | |
649 | // Several operations happen after the module header has been read, but |
650 | // before function bodies are processed. This keeps track of whether |
651 | // we've done this yet. |
652 | bool SeenFirstFunctionBody = false; |
653 | |
654 | /// When function bodies are initially scanned, this map contains info about |
655 | /// where to find deferred function body in the stream. |
656 | DenseMap<Function*, uint64_t> DeferredFunctionInfo; |
657 | |
658 | /// When Metadata block is initially scanned when parsing the module, we may |
659 | /// choose to defer parsing of the metadata. This vector contains info about |
660 | /// which Metadata blocks are deferred. |
661 | std::vector<uint64_t> DeferredMetadataInfo; |
662 | |
663 | /// These are basic blocks forward-referenced by block addresses. They are |
664 | /// inserted lazily into functions when they're loaded. The basic block ID is |
665 | /// its index into the vector. |
666 | DenseMap<Function *, std::vector<BasicBlock *>> BasicBlockFwdRefs; |
667 | std::deque<Function *> BasicBlockFwdRefQueue; |
668 | |
669 | /// These are Functions that contain BlockAddresses which refer a different |
670 | /// Function. When parsing the different Function, queue Functions that refer |
671 | /// to the different Function. Those Functions must be materialized in order |
672 | /// to resolve their BlockAddress constants before the different Function |
673 | /// gets moved into another Module. |
674 | std::vector<Function *> BackwardRefFunctions; |
675 | |
676 | /// Indicates that we are using a new encoding for instruction operands where |
677 | /// most operands in the current FUNCTION_BLOCK are encoded relative to the |
678 | /// instruction number, for a more compact encoding. Some instruction |
679 | /// operands are not relative to the instruction ID: basic block numbers, and |
680 | /// types. Once the old style function blocks have been phased out, we would |
681 | /// not need this flag. |
682 | bool UseRelativeIDs = false; |
683 | |
684 | /// True if all functions will be materialized, negating the need to process |
685 | /// (e.g.) blockaddress forward references. |
686 | bool WillMaterializeAllForwardRefs = false; |
687 | |
688 | /// Tracks whether we have seen debug intrinsics or records in this bitcode; |
689 | /// seeing both in a single module is currently a fatal error. |
690 | bool SeenDebugIntrinsic = false; |
691 | bool SeenDebugRecord = false; |
692 | |
693 | bool StripDebugInfo = false; |
694 | TBAAVerifier TBAAVerifyHelper; |
695 | |
696 | std::vector<std::string> BundleTags; |
697 | SmallVector<SyncScope::ID, 8> SSIDs; |
698 | |
699 | std::optional<ValueTypeCallbackTy> ValueTypeCallback; |
700 | |
701 | public: |
702 | BitcodeReader(BitstreamCursor Stream, StringRef Strtab, |
703 | StringRef ProducerIdentification, LLVMContext &Context); |
704 | |
705 | Error materializeForwardReferencedFunctions(); |
706 | |
707 | Error materialize(GlobalValue *GV) override; |
708 | Error materializeModule() override; |
709 | std::vector<StructType *> getIdentifiedStructTypes() const override; |
710 | |
711 | /// Main interface to parsing a bitcode buffer. |
712 | /// \returns true if an error occurred. |
713 | Error parseBitcodeInto(Module *M, bool ShouldLazyLoadMetadata, |
714 | bool IsImporting, ParserCallbacks Callbacks = {}); |
715 | |
716 | static uint64_t decodeSignRotatedValue(uint64_t V); |
717 | |
718 | /// Materialize any deferred Metadata block. |
719 | Error materializeMetadata() override; |
720 | |
721 | void setStripDebugInfo() override; |
722 | |
723 | private: |
724 | std::vector<StructType *> IdentifiedStructTypes; |
725 | StructType *createIdentifiedStructType(LLVMContext &Context, StringRef Name); |
726 | StructType *createIdentifiedStructType(LLVMContext &Context); |
727 | |
728 | static constexpr unsigned InvalidTypeID = ~0u; |
729 | |
730 | Type *getTypeByID(unsigned ID); |
731 | Type *getPtrElementTypeByID(unsigned ID); |
732 | unsigned getContainedTypeID(unsigned ID, unsigned Idx = 0); |
733 | unsigned getVirtualTypeID(Type *Ty, ArrayRef<unsigned> ContainedTypeIDs = {}); |
734 | |
735 | void callValueTypeCallback(Value *F, unsigned TypeID); |
736 | Expected<Value *> materializeValue(unsigned ValID, BasicBlock *InsertBB); |
737 | Expected<Constant *> getValueForInitializer(unsigned ID); |
738 | |
739 | Value *getFnValueByID(unsigned ID, Type *Ty, unsigned TyID, |
740 | BasicBlock *ConstExprInsertBB) { |
741 | if (Ty && Ty->isMetadataTy()) |
742 | return MetadataAsValue::get(Context&: Ty->getContext(), MD: getFnMetadataByID(ID)); |
743 | return ValueList.getValueFwdRef(Idx: ID, Ty, TyID, ConstExprInsertBB); |
744 | } |
745 | |
746 | Metadata *getFnMetadataByID(unsigned ID) { |
747 | return MDLoader->getMetadataFwdRefOrLoad(Idx: ID); |
748 | } |
749 | |
750 | BasicBlock *getBasicBlock(unsigned ID) const { |
751 | if (ID >= FunctionBBs.size()) return nullptr; // Invalid ID |
752 | return FunctionBBs[ID]; |
753 | } |
754 | |
755 | AttributeList getAttributes(unsigned i) const { |
756 | if (i-1 < MAttributes.size()) |
757 | return MAttributes[i-1]; |
758 | return AttributeList(); |
759 | } |
760 | |
761 | /// Read a value/type pair out of the specified record from slot 'Slot'. |
762 | /// Increment Slot past the number of slots used in the record. Return true on |
763 | /// failure. |
764 | bool getValueTypePair(const SmallVectorImpl<uint64_t> &Record, unsigned &Slot, |
765 | unsigned InstNum, Value *&ResVal, unsigned &TypeID, |
766 | BasicBlock *ConstExprInsertBB) { |
767 | if (Slot == Record.size()) return true; |
768 | unsigned ValNo = (unsigned)Record[Slot++]; |
769 | // Adjust the ValNo, if it was encoded relative to the InstNum. |
770 | if (UseRelativeIDs) |
771 | ValNo = InstNum - ValNo; |
772 | if (ValNo < InstNum) { |
773 | // If this is not a forward reference, just return the value we already |
774 | // have. |
775 | TypeID = ValueList.getTypeID(ValNo); |
776 | ResVal = getFnValueByID(ID: ValNo, Ty: nullptr, TyID: TypeID, ConstExprInsertBB); |
777 | assert((!ResVal || ResVal->getType() == getTypeByID(TypeID)) && |
778 | "Incorrect type ID stored for value" ); |
779 | return ResVal == nullptr; |
780 | } |
781 | if (Slot == Record.size()) |
782 | return true; |
783 | |
784 | TypeID = (unsigned)Record[Slot++]; |
785 | ResVal = getFnValueByID(ID: ValNo, Ty: getTypeByID(ID: TypeID), TyID: TypeID, |
786 | ConstExprInsertBB); |
787 | return ResVal == nullptr; |
788 | } |
789 | |
790 | /// Read a value out of the specified record from slot 'Slot'. Increment Slot |
791 | /// past the number of slots used by the value in the record. Return true if |
792 | /// there is an error. |
793 | bool popValue(const SmallVectorImpl<uint64_t> &Record, unsigned &Slot, |
794 | unsigned InstNum, Type *Ty, unsigned TyID, Value *&ResVal, |
795 | BasicBlock *ConstExprInsertBB) { |
796 | if (getValue(Record, Slot, InstNum, Ty, TyID, ResVal, ConstExprInsertBB)) |
797 | return true; |
798 | // All values currently take a single record slot. |
799 | ++Slot; |
800 | return false; |
801 | } |
802 | |
803 | /// Like popValue, but does not increment the Slot number. |
804 | bool getValue(const SmallVectorImpl<uint64_t> &Record, unsigned Slot, |
805 | unsigned InstNum, Type *Ty, unsigned TyID, Value *&ResVal, |
806 | BasicBlock *ConstExprInsertBB) { |
807 | ResVal = getValue(Record, Slot, InstNum, Ty, TyID, ConstExprInsertBB); |
808 | return ResVal == nullptr; |
809 | } |
810 | |
811 | /// Version of getValue that returns ResVal directly, or 0 if there is an |
812 | /// error. |
813 | Value *getValue(const SmallVectorImpl<uint64_t> &Record, unsigned Slot, |
814 | unsigned InstNum, Type *Ty, unsigned TyID, |
815 | BasicBlock *ConstExprInsertBB) { |
816 | if (Slot == Record.size()) return nullptr; |
817 | unsigned ValNo = (unsigned)Record[Slot]; |
818 | // Adjust the ValNo, if it was encoded relative to the InstNum. |
819 | if (UseRelativeIDs) |
820 | ValNo = InstNum - ValNo; |
821 | return getFnValueByID(ID: ValNo, Ty, TyID, ConstExprInsertBB); |
822 | } |
823 | |
824 | /// Like getValue, but decodes signed VBRs. |
825 | Value *getValueSigned(const SmallVectorImpl<uint64_t> &Record, unsigned Slot, |
826 | unsigned InstNum, Type *Ty, unsigned TyID, |
827 | BasicBlock *ConstExprInsertBB) { |
828 | if (Slot == Record.size()) return nullptr; |
829 | unsigned ValNo = (unsigned)decodeSignRotatedValue(V: Record[Slot]); |
830 | // Adjust the ValNo, if it was encoded relative to the InstNum. |
831 | if (UseRelativeIDs) |
832 | ValNo = InstNum - ValNo; |
833 | return getFnValueByID(ID: ValNo, Ty, TyID, ConstExprInsertBB); |
834 | } |
835 | |
836 | Expected<ConstantRange> readConstantRange(ArrayRef<uint64_t> Record, |
837 | unsigned &OpNum) { |
838 | if (Record.size() - OpNum < 3) |
839 | return error(Message: "Too few records for range" ); |
840 | unsigned BitWidth = Record[OpNum++]; |
841 | if (BitWidth > 64) { |
842 | unsigned LowerActiveWords = Record[OpNum]; |
843 | unsigned UpperActiveWords = Record[OpNum++] >> 32; |
844 | if (Record.size() - OpNum < LowerActiveWords + UpperActiveWords) |
845 | return error(Message: "Too few records for range" ); |
846 | APInt Lower = |
847 | readWideAPInt(Vals: ArrayRef(&Record[OpNum], LowerActiveWords), TypeBits: BitWidth); |
848 | OpNum += LowerActiveWords; |
849 | APInt Upper = |
850 | readWideAPInt(Vals: ArrayRef(&Record[OpNum], UpperActiveWords), TypeBits: BitWidth); |
851 | OpNum += UpperActiveWords; |
852 | return ConstantRange(Lower, Upper); |
853 | } else { |
854 | int64_t Start = BitcodeReader::decodeSignRotatedValue(V: Record[OpNum++]); |
855 | int64_t End = BitcodeReader::decodeSignRotatedValue(V: Record[OpNum++]); |
856 | return ConstantRange(APInt(BitWidth, Start), APInt(BitWidth, End)); |
857 | } |
858 | } |
859 | |
860 | /// Upgrades old-style typeless byval/sret/inalloca attributes by adding the |
861 | /// corresponding argument's pointee type. Also upgrades intrinsics that now |
862 | /// require an elementtype attribute. |
863 | Error propagateAttributeTypes(CallBase *CB, ArrayRef<unsigned> ArgsTys); |
864 | |
865 | /// Converts alignment exponent (i.e. power of two (or zero)) to the |
866 | /// corresponding alignment to use. If alignment is too large, returns |
867 | /// a corresponding error code. |
868 | Error parseAlignmentValue(uint64_t Exponent, MaybeAlign &Alignment); |
869 | Error parseAttrKind(uint64_t Code, Attribute::AttrKind *Kind); |
870 | Error parseModule(uint64_t ResumeBit, bool ShouldLazyLoadMetadata = false, |
871 | ParserCallbacks Callbacks = {}); |
872 | |
873 | Error parseComdatRecord(ArrayRef<uint64_t> Record); |
874 | Error parseGlobalVarRecord(ArrayRef<uint64_t> Record); |
875 | Error parseFunctionRecord(ArrayRef<uint64_t> Record); |
876 | Error parseGlobalIndirectSymbolRecord(unsigned BitCode, |
877 | ArrayRef<uint64_t> Record); |
878 | |
879 | Error parseAttributeBlock(); |
880 | Error parseAttributeGroupBlock(); |
881 | Error parseTypeTable(); |
882 | Error parseTypeTableBody(); |
883 | Error parseOperandBundleTags(); |
884 | Error parseSyncScopeNames(); |
885 | |
886 | Expected<Value *> recordValue(SmallVectorImpl<uint64_t> &Record, |
887 | unsigned NameIndex, Triple &TT); |
888 | void setDeferredFunctionInfo(unsigned FuncBitcodeOffsetDelta, Function *F, |
889 | ArrayRef<uint64_t> Record); |
890 | Error parseValueSymbolTable(uint64_t Offset = 0); |
891 | Error parseGlobalValueSymbolTable(); |
892 | Error parseConstants(); |
893 | Error rememberAndSkipFunctionBodies(); |
894 | Error rememberAndSkipFunctionBody(); |
895 | /// Save the positions of the Metadata blocks and skip parsing the blocks. |
896 | Error rememberAndSkipMetadata(); |
897 | Error typeCheckLoadStoreInst(Type *ValType, Type *PtrType); |
898 | Error parseFunctionBody(Function *F); |
899 | Error globalCleanup(); |
900 | Error resolveGlobalAndIndirectSymbolInits(); |
901 | Error parseUseLists(); |
902 | Error findFunctionInStream( |
903 | Function *F, |
904 | DenseMap<Function *, uint64_t>::iterator DeferredFunctionInfoIterator); |
905 | |
906 | SyncScope::ID getDecodedSyncScopeID(unsigned Val); |
907 | }; |
908 | |
909 | /// Class to manage reading and parsing function summary index bitcode |
910 | /// files/sections. |
911 | class ModuleSummaryIndexBitcodeReader : public BitcodeReaderBase { |
912 | /// The module index built during parsing. |
913 | ModuleSummaryIndex &TheIndex; |
914 | |
915 | /// Indicates whether we have encountered a global value summary section |
916 | /// yet during parsing. |
917 | bool SeenGlobalValSummary = false; |
918 | |
919 | /// Indicates whether we have already parsed the VST, used for error checking. |
920 | bool SeenValueSymbolTable = false; |
921 | |
922 | /// Set to the offset of the VST recorded in the MODULE_CODE_VSTOFFSET record. |
923 | /// Used to enable on-demand parsing of the VST. |
924 | uint64_t VSTOffset = 0; |
925 | |
926 | // Map to save ValueId to ValueInfo association that was recorded in the |
927 | // ValueSymbolTable. It is used after the VST is parsed to convert |
928 | // call graph edges read from the function summary from referencing |
929 | // callees by their ValueId to using the ValueInfo instead, which is how |
930 | // they are recorded in the summary index being built. |
931 | // We save a GUID which refers to the same global as the ValueInfo, but |
932 | // ignoring the linkage, i.e. for values other than local linkage they are |
933 | // identical (this is the second tuple member). |
934 | // The third tuple member is the real GUID of the ValueInfo. |
935 | DenseMap<unsigned, |
936 | std::tuple<ValueInfo, GlobalValue::GUID, GlobalValue::GUID>> |
937 | ValueIdToValueInfoMap; |
938 | |
939 | /// Map populated during module path string table parsing, from the |
940 | /// module ID to a string reference owned by the index's module |
941 | /// path string table, used to correlate with combined index |
942 | /// summary records. |
943 | DenseMap<uint64_t, StringRef> ModuleIdMap; |
944 | |
945 | /// Original source file name recorded in a bitcode record. |
946 | std::string SourceFileName; |
947 | |
948 | /// The string identifier given to this module by the client, normally the |
949 | /// path to the bitcode file. |
950 | StringRef ModulePath; |
951 | |
952 | /// Callback to ask whether a symbol is the prevailing copy when invoked |
953 | /// during combined index building. |
954 | std::function<bool(GlobalValue::GUID)> IsPrevailing; |
955 | |
956 | /// Saves the stack ids from the STACK_IDS record to consult when adding stack |
957 | /// ids from the lists in the callsite and alloc entries to the index. |
958 | std::vector<uint64_t> StackIds; |
959 | |
960 | public: |
961 | ModuleSummaryIndexBitcodeReader( |
962 | BitstreamCursor Stream, StringRef Strtab, ModuleSummaryIndex &TheIndex, |
963 | StringRef ModulePath, |
964 | std::function<bool(GlobalValue::GUID)> IsPrevailing = nullptr); |
965 | |
966 | Error parseModule(); |
967 | |
968 | private: |
969 | void setValueGUID(uint64_t ValueID, StringRef ValueName, |
970 | GlobalValue::LinkageTypes Linkage, |
971 | StringRef SourceFileName); |
972 | Error parseValueSymbolTable( |
973 | uint64_t Offset, |
974 | DenseMap<unsigned, GlobalValue::LinkageTypes> &ValueIdToLinkageMap); |
975 | std::vector<ValueInfo> makeRefList(ArrayRef<uint64_t> Record); |
976 | std::vector<FunctionSummary::EdgeTy> makeCallList(ArrayRef<uint64_t> Record, |
977 | bool IsOldProfileFormat, |
978 | bool HasProfile, |
979 | bool HasRelBF); |
980 | Error parseEntireSummary(unsigned ID); |
981 | Error parseModuleStringTable(); |
982 | void parseTypeIdCompatibleVtableSummaryRecord(ArrayRef<uint64_t> Record); |
983 | void parseTypeIdCompatibleVtableInfo(ArrayRef<uint64_t> Record, size_t &Slot, |
984 | TypeIdCompatibleVtableInfo &TypeId); |
985 | std::vector<FunctionSummary::ParamAccess> |
986 | parseParamAccesses(ArrayRef<uint64_t> Record); |
987 | |
988 | template <bool AllowNullValueInfo = false> |
989 | std::tuple<ValueInfo, GlobalValue::GUID, GlobalValue::GUID> |
990 | getValueInfoFromValueId(unsigned ValueId); |
991 | |
992 | void addThisModule(); |
993 | ModuleSummaryIndex::ModuleInfo *getThisModule(); |
994 | }; |
995 | |
996 | } // end anonymous namespace |
997 | |
998 | std::error_code llvm::errorToErrorCodeAndEmitErrors(LLVMContext &Ctx, |
999 | Error Err) { |
1000 | if (Err) { |
1001 | std::error_code EC; |
1002 | handleAllErrors(E: std::move(Err), Handlers: [&](ErrorInfoBase &EIB) { |
1003 | EC = EIB.convertToErrorCode(); |
1004 | Ctx.emitError(ErrorStr: EIB.message()); |
1005 | }); |
1006 | return EC; |
1007 | } |
1008 | return std::error_code(); |
1009 | } |
1010 | |
1011 | BitcodeReader::BitcodeReader(BitstreamCursor Stream, StringRef Strtab, |
1012 | StringRef ProducerIdentification, |
1013 | LLVMContext &Context) |
1014 | : BitcodeReaderBase(std::move(Stream), Strtab), Context(Context), |
1015 | ValueList(this->Stream.SizeInBytes(), |
1016 | [this](unsigned ValID, BasicBlock *InsertBB) { |
1017 | return materializeValue(ValID, InsertBB); |
1018 | }) { |
1019 | this->ProducerIdentification = std::string(ProducerIdentification); |
1020 | } |
1021 | |
1022 | Error BitcodeReader::materializeForwardReferencedFunctions() { |
1023 | if (WillMaterializeAllForwardRefs) |
1024 | return Error::success(); |
1025 | |
1026 | // Prevent recursion. |
1027 | WillMaterializeAllForwardRefs = true; |
1028 | |
1029 | while (!BasicBlockFwdRefQueue.empty()) { |
1030 | Function *F = BasicBlockFwdRefQueue.front(); |
1031 | BasicBlockFwdRefQueue.pop_front(); |
1032 | assert(F && "Expected valid function" ); |
1033 | if (!BasicBlockFwdRefs.count(Val: F)) |
1034 | // Already materialized. |
1035 | continue; |
1036 | |
1037 | // Check for a function that isn't materializable to prevent an infinite |
1038 | // loop. When parsing a blockaddress stored in a global variable, there |
1039 | // isn't a trivial way to check if a function will have a body without a |
1040 | // linear search through FunctionsWithBodies, so just check it here. |
1041 | if (!F->isMaterializable()) |
1042 | return error(Message: "Never resolved function from blockaddress" ); |
1043 | |
1044 | // Try to materialize F. |
1045 | if (Error Err = materialize(GV: F)) |
1046 | return Err; |
1047 | } |
1048 | assert(BasicBlockFwdRefs.empty() && "Function missing from queue" ); |
1049 | |
1050 | for (Function *F : BackwardRefFunctions) |
1051 | if (Error Err = materialize(GV: F)) |
1052 | return Err; |
1053 | BackwardRefFunctions.clear(); |
1054 | |
1055 | // Reset state. |
1056 | WillMaterializeAllForwardRefs = false; |
1057 | return Error::success(); |
1058 | } |
1059 | |
1060 | //===----------------------------------------------------------------------===// |
1061 | // Helper functions to implement forward reference resolution, etc. |
1062 | //===----------------------------------------------------------------------===// |
1063 | |
1064 | static bool hasImplicitComdat(size_t Val) { |
1065 | switch (Val) { |
1066 | default: |
1067 | return false; |
1068 | case 1: // Old WeakAnyLinkage |
1069 | case 4: // Old LinkOnceAnyLinkage |
1070 | case 10: // Old WeakODRLinkage |
1071 | case 11: // Old LinkOnceODRLinkage |
1072 | return true; |
1073 | } |
1074 | } |
1075 | |
1076 | static GlobalValue::LinkageTypes getDecodedLinkage(unsigned Val) { |
1077 | switch (Val) { |
1078 | default: // Map unknown/new linkages to external |
1079 | case 0: |
1080 | return GlobalValue::ExternalLinkage; |
1081 | case 2: |
1082 | return GlobalValue::AppendingLinkage; |
1083 | case 3: |
1084 | return GlobalValue::InternalLinkage; |
1085 | case 5: |
1086 | return GlobalValue::ExternalLinkage; // Obsolete DLLImportLinkage |
1087 | case 6: |
1088 | return GlobalValue::ExternalLinkage; // Obsolete DLLExportLinkage |
1089 | case 7: |
1090 | return GlobalValue::ExternalWeakLinkage; |
1091 | case 8: |
1092 | return GlobalValue::CommonLinkage; |
1093 | case 9: |
1094 | return GlobalValue::PrivateLinkage; |
1095 | case 12: |
1096 | return GlobalValue::AvailableExternallyLinkage; |
1097 | case 13: |
1098 | return GlobalValue::PrivateLinkage; // Obsolete LinkerPrivateLinkage |
1099 | case 14: |
1100 | return GlobalValue::PrivateLinkage; // Obsolete LinkerPrivateWeakLinkage |
1101 | case 15: |
1102 | return GlobalValue::ExternalLinkage; // Obsolete LinkOnceODRAutoHideLinkage |
1103 | case 1: // Old value with implicit comdat. |
1104 | case 16: |
1105 | return GlobalValue::WeakAnyLinkage; |
1106 | case 10: // Old value with implicit comdat. |
1107 | case 17: |
1108 | return GlobalValue::WeakODRLinkage; |
1109 | case 4: // Old value with implicit comdat. |
1110 | case 18: |
1111 | return GlobalValue::LinkOnceAnyLinkage; |
1112 | case 11: // Old value with implicit comdat. |
1113 | case 19: |
1114 | return GlobalValue::LinkOnceODRLinkage; |
1115 | } |
1116 | } |
1117 | |
1118 | static FunctionSummary::FFlags getDecodedFFlags(uint64_t RawFlags) { |
1119 | FunctionSummary::FFlags Flags; |
1120 | Flags.ReadNone = RawFlags & 0x1; |
1121 | Flags.ReadOnly = (RawFlags >> 1) & 0x1; |
1122 | Flags.NoRecurse = (RawFlags >> 2) & 0x1; |
1123 | Flags.ReturnDoesNotAlias = (RawFlags >> 3) & 0x1; |
1124 | Flags.NoInline = (RawFlags >> 4) & 0x1; |
1125 | Flags.AlwaysInline = (RawFlags >> 5) & 0x1; |
1126 | Flags.NoUnwind = (RawFlags >> 6) & 0x1; |
1127 | Flags.MayThrow = (RawFlags >> 7) & 0x1; |
1128 | Flags.HasUnknownCall = (RawFlags >> 8) & 0x1; |
1129 | Flags.MustBeUnreachable = (RawFlags >> 9) & 0x1; |
1130 | return Flags; |
1131 | } |
1132 | |
1133 | // Decode the flags for GlobalValue in the summary. The bits for each attribute: |
1134 | // |
1135 | // linkage: [0,4), notEligibleToImport: 4, live: 5, local: 6, canAutoHide: 7, |
1136 | // visibility: [8, 10). |
1137 | static GlobalValueSummary::GVFlags getDecodedGVSummaryFlags(uint64_t RawFlags, |
1138 | uint64_t Version) { |
1139 | // Summary were not emitted before LLVM 3.9, we don't need to upgrade Linkage |
1140 | // like getDecodedLinkage() above. Any future change to the linkage enum and |
1141 | // to getDecodedLinkage() will need to be taken into account here as above. |
1142 | auto Linkage = GlobalValue::LinkageTypes(RawFlags & 0xF); // 4 bits |
1143 | auto Visibility = GlobalValue::VisibilityTypes((RawFlags >> 8) & 3); // 2 bits |
1144 | auto IK = GlobalValueSummary::ImportKind((RawFlags >> 10) & 1); // 1 bit |
1145 | RawFlags = RawFlags >> 4; |
1146 | bool NotEligibleToImport = (RawFlags & 0x1) || Version < 3; |
1147 | // The Live flag wasn't introduced until version 3. For dead stripping |
1148 | // to work correctly on earlier versions, we must conservatively treat all |
1149 | // values as live. |
1150 | bool Live = (RawFlags & 0x2) || Version < 3; |
1151 | bool Local = (RawFlags & 0x4); |
1152 | bool AutoHide = (RawFlags & 0x8); |
1153 | |
1154 | return GlobalValueSummary::GVFlags(Linkage, Visibility, NotEligibleToImport, |
1155 | Live, Local, AutoHide, IK); |
1156 | } |
1157 | |
1158 | // Decode the flags for GlobalVariable in the summary |
1159 | static GlobalVarSummary::GVarFlags getDecodedGVarFlags(uint64_t RawFlags) { |
1160 | return GlobalVarSummary::GVarFlags( |
1161 | (RawFlags & 0x1) ? true : false, (RawFlags & 0x2) ? true : false, |
1162 | (RawFlags & 0x4) ? true : false, |
1163 | (GlobalObject::VCallVisibility)(RawFlags >> 3)); |
1164 | } |
1165 | |
1166 | static std::pair<CalleeInfo::HotnessType, bool> |
1167 | getDecodedHotnessCallEdgeInfo(uint64_t RawFlags) { |
1168 | CalleeInfo::HotnessType Hotness = |
1169 | static_cast<CalleeInfo::HotnessType>(RawFlags & 0x7); // 3 bits |
1170 | bool HasTailCall = (RawFlags & 0x8); // 1 bit |
1171 | return {Hotness, HasTailCall}; |
1172 | } |
1173 | |
1174 | static void getDecodedRelBFCallEdgeInfo(uint64_t RawFlags, uint64_t &RelBF, |
1175 | bool &HasTailCall) { |
1176 | static constexpr uint64_t RelBlockFreqMask = |
1177 | (1 << CalleeInfo::RelBlockFreqBits) - 1; |
1178 | RelBF = RawFlags & RelBlockFreqMask; // RelBlockFreqBits bits |
1179 | HasTailCall = (RawFlags & (1 << CalleeInfo::RelBlockFreqBits)); // 1 bit |
1180 | } |
1181 | |
1182 | static GlobalValue::VisibilityTypes getDecodedVisibility(unsigned Val) { |
1183 | switch (Val) { |
1184 | default: // Map unknown visibilities to default. |
1185 | case 0: return GlobalValue::DefaultVisibility; |
1186 | case 1: return GlobalValue::HiddenVisibility; |
1187 | case 2: return GlobalValue::ProtectedVisibility; |
1188 | } |
1189 | } |
1190 | |
1191 | static GlobalValue::DLLStorageClassTypes |
1192 | getDecodedDLLStorageClass(unsigned Val) { |
1193 | switch (Val) { |
1194 | default: // Map unknown values to default. |
1195 | case 0: return GlobalValue::DefaultStorageClass; |
1196 | case 1: return GlobalValue::DLLImportStorageClass; |
1197 | case 2: return GlobalValue::DLLExportStorageClass; |
1198 | } |
1199 | } |
1200 | |
1201 | static bool getDecodedDSOLocal(unsigned Val) { |
1202 | switch(Val) { |
1203 | default: // Map unknown values to preemptable. |
1204 | case 0: return false; |
1205 | case 1: return true; |
1206 | } |
1207 | } |
1208 | |
1209 | static std::optional<CodeModel::Model> getDecodedCodeModel(unsigned Val) { |
1210 | switch (Val) { |
1211 | case 1: |
1212 | return CodeModel::Tiny; |
1213 | case 2: |
1214 | return CodeModel::Small; |
1215 | case 3: |
1216 | return CodeModel::Kernel; |
1217 | case 4: |
1218 | return CodeModel::Medium; |
1219 | case 5: |
1220 | return CodeModel::Large; |
1221 | } |
1222 | |
1223 | return {}; |
1224 | } |
1225 | |
1226 | static GlobalVariable::ThreadLocalMode getDecodedThreadLocalMode(unsigned Val) { |
1227 | switch (Val) { |
1228 | case 0: return GlobalVariable::NotThreadLocal; |
1229 | default: // Map unknown non-zero value to general dynamic. |
1230 | case 1: return GlobalVariable::GeneralDynamicTLSModel; |
1231 | case 2: return GlobalVariable::LocalDynamicTLSModel; |
1232 | case 3: return GlobalVariable::InitialExecTLSModel; |
1233 | case 4: return GlobalVariable::LocalExecTLSModel; |
1234 | } |
1235 | } |
1236 | |
1237 | static GlobalVariable::UnnamedAddr getDecodedUnnamedAddrType(unsigned Val) { |
1238 | switch (Val) { |
1239 | default: // Map unknown to UnnamedAddr::None. |
1240 | case 0: return GlobalVariable::UnnamedAddr::None; |
1241 | case 1: return GlobalVariable::UnnamedAddr::Global; |
1242 | case 2: return GlobalVariable::UnnamedAddr::Local; |
1243 | } |
1244 | } |
1245 | |
1246 | static int getDecodedCastOpcode(unsigned Val) { |
1247 | switch (Val) { |
1248 | default: return -1; |
1249 | case bitc::CAST_TRUNC : return Instruction::Trunc; |
1250 | case bitc::CAST_ZEXT : return Instruction::ZExt; |
1251 | case bitc::CAST_SEXT : return Instruction::SExt; |
1252 | case bitc::CAST_FPTOUI : return Instruction::FPToUI; |
1253 | case bitc::CAST_FPTOSI : return Instruction::FPToSI; |
1254 | case bitc::CAST_UITOFP : return Instruction::UIToFP; |
1255 | case bitc::CAST_SITOFP : return Instruction::SIToFP; |
1256 | case bitc::CAST_FPTRUNC : return Instruction::FPTrunc; |
1257 | case bitc::CAST_FPEXT : return Instruction::FPExt; |
1258 | case bitc::CAST_PTRTOINT: return Instruction::PtrToInt; |
1259 | case bitc::CAST_INTTOPTR: return Instruction::IntToPtr; |
1260 | case bitc::CAST_BITCAST : return Instruction::BitCast; |
1261 | case bitc::CAST_ADDRSPACECAST: return Instruction::AddrSpaceCast; |
1262 | } |
1263 | } |
1264 | |
1265 | static int getDecodedUnaryOpcode(unsigned Val, Type *Ty) { |
1266 | bool IsFP = Ty->isFPOrFPVectorTy(); |
1267 | // UnOps are only valid for int/fp or vector of int/fp types |
1268 | if (!IsFP && !Ty->isIntOrIntVectorTy()) |
1269 | return -1; |
1270 | |
1271 | switch (Val) { |
1272 | default: |
1273 | return -1; |
1274 | case bitc::UNOP_FNEG: |
1275 | return IsFP ? Instruction::FNeg : -1; |
1276 | } |
1277 | } |
1278 | |
1279 | static int getDecodedBinaryOpcode(unsigned Val, Type *Ty) { |
1280 | bool IsFP = Ty->isFPOrFPVectorTy(); |
1281 | // BinOps are only valid for int/fp or vector of int/fp types |
1282 | if (!IsFP && !Ty->isIntOrIntVectorTy()) |
1283 | return -1; |
1284 | |
1285 | switch (Val) { |
1286 | default: |
1287 | return -1; |
1288 | case bitc::BINOP_ADD: |
1289 | return IsFP ? Instruction::FAdd : Instruction::Add; |
1290 | case bitc::BINOP_SUB: |
1291 | return IsFP ? Instruction::FSub : Instruction::Sub; |
1292 | case bitc::BINOP_MUL: |
1293 | return IsFP ? Instruction::FMul : Instruction::Mul; |
1294 | case bitc::BINOP_UDIV: |
1295 | return IsFP ? -1 : Instruction::UDiv; |
1296 | case bitc::BINOP_SDIV: |
1297 | return IsFP ? Instruction::FDiv : Instruction::SDiv; |
1298 | case bitc::BINOP_UREM: |
1299 | return IsFP ? -1 : Instruction::URem; |
1300 | case bitc::BINOP_SREM: |
1301 | return IsFP ? Instruction::FRem : Instruction::SRem; |
1302 | case bitc::BINOP_SHL: |
1303 | return IsFP ? -1 : Instruction::Shl; |
1304 | case bitc::BINOP_LSHR: |
1305 | return IsFP ? -1 : Instruction::LShr; |
1306 | case bitc::BINOP_ASHR: |
1307 | return IsFP ? -1 : Instruction::AShr; |
1308 | case bitc::BINOP_AND: |
1309 | return IsFP ? -1 : Instruction::And; |
1310 | case bitc::BINOP_OR: |
1311 | return IsFP ? -1 : Instruction::Or; |
1312 | case bitc::BINOP_XOR: |
1313 | return IsFP ? -1 : Instruction::Xor; |
1314 | } |
1315 | } |
1316 | |
1317 | static AtomicRMWInst::BinOp getDecodedRMWOperation(unsigned Val) { |
1318 | switch (Val) { |
1319 | default: return AtomicRMWInst::BAD_BINOP; |
1320 | case bitc::RMW_XCHG: return AtomicRMWInst::Xchg; |
1321 | case bitc::RMW_ADD: return AtomicRMWInst::Add; |
1322 | case bitc::RMW_SUB: return AtomicRMWInst::Sub; |
1323 | case bitc::RMW_AND: return AtomicRMWInst::And; |
1324 | case bitc::RMW_NAND: return AtomicRMWInst::Nand; |
1325 | case bitc::RMW_OR: return AtomicRMWInst::Or; |
1326 | case bitc::RMW_XOR: return AtomicRMWInst::Xor; |
1327 | case bitc::RMW_MAX: return AtomicRMWInst::Max; |
1328 | case bitc::RMW_MIN: return AtomicRMWInst::Min; |
1329 | case bitc::RMW_UMAX: return AtomicRMWInst::UMax; |
1330 | case bitc::RMW_UMIN: return AtomicRMWInst::UMin; |
1331 | case bitc::RMW_FADD: return AtomicRMWInst::FAdd; |
1332 | case bitc::RMW_FSUB: return AtomicRMWInst::FSub; |
1333 | case bitc::RMW_FMAX: return AtomicRMWInst::FMax; |
1334 | case bitc::RMW_FMIN: return AtomicRMWInst::FMin; |
1335 | case bitc::RMW_UINC_WRAP: |
1336 | return AtomicRMWInst::UIncWrap; |
1337 | case bitc::RMW_UDEC_WRAP: |
1338 | return AtomicRMWInst::UDecWrap; |
1339 | } |
1340 | } |
1341 | |
1342 | static AtomicOrdering getDecodedOrdering(unsigned Val) { |
1343 | switch (Val) { |
1344 | case bitc::ORDERING_NOTATOMIC: return AtomicOrdering::NotAtomic; |
1345 | case bitc::ORDERING_UNORDERED: return AtomicOrdering::Unordered; |
1346 | case bitc::ORDERING_MONOTONIC: return AtomicOrdering::Monotonic; |
1347 | case bitc::ORDERING_ACQUIRE: return AtomicOrdering::Acquire; |
1348 | case bitc::ORDERING_RELEASE: return AtomicOrdering::Release; |
1349 | case bitc::ORDERING_ACQREL: return AtomicOrdering::AcquireRelease; |
1350 | default: // Map unknown orderings to sequentially-consistent. |
1351 | case bitc::ORDERING_SEQCST: return AtomicOrdering::SequentiallyConsistent; |
1352 | } |
1353 | } |
1354 | |
1355 | static Comdat::SelectionKind getDecodedComdatSelectionKind(unsigned Val) { |
1356 | switch (Val) { |
1357 | default: // Map unknown selection kinds to any. |
1358 | case bitc::COMDAT_SELECTION_KIND_ANY: |
1359 | return Comdat::Any; |
1360 | case bitc::COMDAT_SELECTION_KIND_EXACT_MATCH: |
1361 | return Comdat::ExactMatch; |
1362 | case bitc::COMDAT_SELECTION_KIND_LARGEST: |
1363 | return Comdat::Largest; |
1364 | case bitc::COMDAT_SELECTION_KIND_NO_DUPLICATES: |
1365 | return Comdat::NoDeduplicate; |
1366 | case bitc::COMDAT_SELECTION_KIND_SAME_SIZE: |
1367 | return Comdat::SameSize; |
1368 | } |
1369 | } |
1370 | |
1371 | static FastMathFlags getDecodedFastMathFlags(unsigned Val) { |
1372 | FastMathFlags FMF; |
1373 | if (0 != (Val & bitc::UnsafeAlgebra)) |
1374 | FMF.setFast(); |
1375 | if (0 != (Val & bitc::AllowReassoc)) |
1376 | FMF.setAllowReassoc(); |
1377 | if (0 != (Val & bitc::NoNaNs)) |
1378 | FMF.setNoNaNs(); |
1379 | if (0 != (Val & bitc::NoInfs)) |
1380 | FMF.setNoInfs(); |
1381 | if (0 != (Val & bitc::NoSignedZeros)) |
1382 | FMF.setNoSignedZeros(); |
1383 | if (0 != (Val & bitc::AllowReciprocal)) |
1384 | FMF.setAllowReciprocal(); |
1385 | if (0 != (Val & bitc::AllowContract)) |
1386 | FMF.setAllowContract(true); |
1387 | if (0 != (Val & bitc::ApproxFunc)) |
1388 | FMF.setApproxFunc(); |
1389 | return FMF; |
1390 | } |
1391 | |
1392 | static void upgradeDLLImportExportLinkage(GlobalValue *GV, unsigned Val) { |
1393 | // A GlobalValue with local linkage cannot have a DLL storage class. |
1394 | if (GV->hasLocalLinkage()) |
1395 | return; |
1396 | switch (Val) { |
1397 | case 5: GV->setDLLStorageClass(GlobalValue::DLLImportStorageClass); break; |
1398 | case 6: GV->setDLLStorageClass(GlobalValue::DLLExportStorageClass); break; |
1399 | } |
1400 | } |
1401 | |
1402 | Type *BitcodeReader::getTypeByID(unsigned ID) { |
1403 | // The type table size is always specified correctly. |
1404 | if (ID >= TypeList.size()) |
1405 | return nullptr; |
1406 | |
1407 | if (Type *Ty = TypeList[ID]) |
1408 | return Ty; |
1409 | |
1410 | // If we have a forward reference, the only possible case is when it is to a |
1411 | // named struct. Just create a placeholder for now. |
1412 | return TypeList[ID] = createIdentifiedStructType(Context); |
1413 | } |
1414 | |
1415 | unsigned BitcodeReader::getContainedTypeID(unsigned ID, unsigned Idx) { |
1416 | auto It = ContainedTypeIDs.find(Val: ID); |
1417 | if (It == ContainedTypeIDs.end()) |
1418 | return InvalidTypeID; |
1419 | |
1420 | if (Idx >= It->second.size()) |
1421 | return InvalidTypeID; |
1422 | |
1423 | return It->second[Idx]; |
1424 | } |
1425 | |
1426 | Type *BitcodeReader::getPtrElementTypeByID(unsigned ID) { |
1427 | if (ID >= TypeList.size()) |
1428 | return nullptr; |
1429 | |
1430 | Type *Ty = TypeList[ID]; |
1431 | if (!Ty->isPointerTy()) |
1432 | return nullptr; |
1433 | |
1434 | return getTypeByID(ID: getContainedTypeID(ID, Idx: 0)); |
1435 | } |
1436 | |
1437 | unsigned BitcodeReader::getVirtualTypeID(Type *Ty, |
1438 | ArrayRef<unsigned> ChildTypeIDs) { |
1439 | unsigned ChildTypeID = ChildTypeIDs.empty() ? InvalidTypeID : ChildTypeIDs[0]; |
1440 | auto CacheKey = std::make_pair(x&: Ty, y&: ChildTypeID); |
1441 | auto It = VirtualTypeIDs.find(Val: CacheKey); |
1442 | if (It != VirtualTypeIDs.end()) { |
1443 | // The cmpxchg return value is the only place we need more than one |
1444 | // contained type ID, however the second one will always be the same (i1), |
1445 | // so we don't need to include it in the cache key. This asserts that the |
1446 | // contained types are indeed as expected and there are no collisions. |
1447 | assert((ChildTypeIDs.empty() || |
1448 | ContainedTypeIDs[It->second] == ChildTypeIDs) && |
1449 | "Incorrect cached contained type IDs" ); |
1450 | return It->second; |
1451 | } |
1452 | |
1453 | unsigned TypeID = TypeList.size(); |
1454 | TypeList.push_back(x: Ty); |
1455 | if (!ChildTypeIDs.empty()) |
1456 | append_range(C&: ContainedTypeIDs[TypeID], R&: ChildTypeIDs); |
1457 | VirtualTypeIDs.insert(KV: {CacheKey, TypeID}); |
1458 | return TypeID; |
1459 | } |
1460 | |
1461 | static bool isConstExprSupported(const BitcodeConstant *BC) { |
1462 | uint8_t Opcode = BC->Opcode; |
1463 | |
1464 | // These are not real constant expressions, always consider them supported. |
1465 | if (Opcode >= BitcodeConstant::FirstSpecialOpcode) |
1466 | return true; |
1467 | |
1468 | // If -expand-constant-exprs is set, we want to consider all expressions |
1469 | // as unsupported. |
1470 | if (ExpandConstantExprs) |
1471 | return false; |
1472 | |
1473 | if (Instruction::isBinaryOp(Opcode)) |
1474 | return ConstantExpr::isSupportedBinOp(Opcode); |
1475 | |
1476 | if (Instruction::isCast(Opcode)) |
1477 | return ConstantExpr::isSupportedCastOp(Opcode); |
1478 | |
1479 | if (Opcode == Instruction::GetElementPtr) |
1480 | return ConstantExpr::isSupportedGetElementPtr(SrcElemTy: BC->SrcElemTy); |
1481 | |
1482 | switch (Opcode) { |
1483 | case Instruction::FNeg: |
1484 | case Instruction::Select: |
1485 | return false; |
1486 | default: |
1487 | return true; |
1488 | } |
1489 | } |
1490 | |
1491 | Expected<Value *> BitcodeReader::materializeValue(unsigned StartValID, |
1492 | BasicBlock *InsertBB) { |
1493 | // Quickly handle the case where there is no BitcodeConstant to resolve. |
1494 | if (StartValID < ValueList.size() && ValueList[StartValID] && |
1495 | !isa<BitcodeConstant>(Val: ValueList[StartValID])) |
1496 | return ValueList[StartValID]; |
1497 | |
1498 | SmallDenseMap<unsigned, Value *> MaterializedValues; |
1499 | SmallVector<unsigned> Worklist; |
1500 | Worklist.push_back(Elt: StartValID); |
1501 | while (!Worklist.empty()) { |
1502 | unsigned ValID = Worklist.back(); |
1503 | if (MaterializedValues.count(Val: ValID)) { |
1504 | // Duplicate expression that was already handled. |
1505 | Worklist.pop_back(); |
1506 | continue; |
1507 | } |
1508 | |
1509 | if (ValID >= ValueList.size() || !ValueList[ValID]) |
1510 | return error(Message: "Invalid value ID" ); |
1511 | |
1512 | Value *V = ValueList[ValID]; |
1513 | auto *BC = dyn_cast<BitcodeConstant>(Val: V); |
1514 | if (!BC) { |
1515 | MaterializedValues.insert(KV: {ValID, V}); |
1516 | Worklist.pop_back(); |
1517 | continue; |
1518 | } |
1519 | |
1520 | // Iterate in reverse, so values will get popped from the worklist in |
1521 | // expected order. |
1522 | SmallVector<Value *> Ops; |
1523 | for (unsigned OpID : reverse(C: BC->getOperandIDs())) { |
1524 | auto It = MaterializedValues.find(Val: OpID); |
1525 | if (It != MaterializedValues.end()) |
1526 | Ops.push_back(Elt: It->second); |
1527 | else |
1528 | Worklist.push_back(Elt: OpID); |
1529 | } |
1530 | |
1531 | // Some expressions have not been resolved yet, handle them first and then |
1532 | // revisit this one. |
1533 | if (Ops.size() != BC->getOperandIDs().size()) |
1534 | continue; |
1535 | std::reverse(first: Ops.begin(), last: Ops.end()); |
1536 | |
1537 | SmallVector<Constant *> ConstOps; |
1538 | for (Value *Op : Ops) |
1539 | if (auto *C = dyn_cast<Constant>(Val: Op)) |
1540 | ConstOps.push_back(Elt: C); |
1541 | |
1542 | // Materialize as constant expression if possible. |
1543 | if (isConstExprSupported(BC) && ConstOps.size() == Ops.size()) { |
1544 | Constant *C; |
1545 | if (Instruction::isCast(Opcode: BC->Opcode)) { |
1546 | C = UpgradeBitCastExpr(Opc: BC->Opcode, C: ConstOps[0], DestTy: BC->getType()); |
1547 | if (!C) |
1548 | C = ConstantExpr::getCast(ops: BC->Opcode, C: ConstOps[0], Ty: BC->getType()); |
1549 | } else if (Instruction::isBinaryOp(Opcode: BC->Opcode)) { |
1550 | C = ConstantExpr::get(Opcode: BC->Opcode, C1: ConstOps[0], C2: ConstOps[1], Flags: BC->Flags); |
1551 | } else { |
1552 | switch (BC->Opcode) { |
1553 | case BitcodeConstant::NoCFIOpcode: { |
1554 | auto *GV = dyn_cast<GlobalValue>(Val: ConstOps[0]); |
1555 | if (!GV) |
1556 | return error(Message: "no_cfi operand must be GlobalValue" ); |
1557 | C = NoCFIValue::get(GV); |
1558 | break; |
1559 | } |
1560 | case BitcodeConstant::DSOLocalEquivalentOpcode: { |
1561 | auto *GV = dyn_cast<GlobalValue>(Val: ConstOps[0]); |
1562 | if (!GV) |
1563 | return error(Message: "dso_local operand must be GlobalValue" ); |
1564 | C = DSOLocalEquivalent::get(GV); |
1565 | break; |
1566 | } |
1567 | case BitcodeConstant::BlockAddressOpcode: { |
1568 | Function *Fn = dyn_cast<Function>(Val: ConstOps[0]); |
1569 | if (!Fn) |
1570 | return error(Message: "blockaddress operand must be a function" ); |
1571 | |
1572 | // If the function is already parsed we can insert the block address |
1573 | // right away. |
1574 | BasicBlock *BB; |
1575 | unsigned BBID = BC->BlockAddressBB; |
1576 | if (!BBID) |
1577 | // Invalid reference to entry block. |
1578 | return error(Message: "Invalid ID" ); |
1579 | if (!Fn->empty()) { |
1580 | Function::iterator BBI = Fn->begin(), BBE = Fn->end(); |
1581 | for (size_t I = 0, E = BBID; I != E; ++I) { |
1582 | if (BBI == BBE) |
1583 | return error(Message: "Invalid ID" ); |
1584 | ++BBI; |
1585 | } |
1586 | BB = &*BBI; |
1587 | } else { |
1588 | // Otherwise insert a placeholder and remember it so it can be |
1589 | // inserted when the function is parsed. |
1590 | auto &FwdBBs = BasicBlockFwdRefs[Fn]; |
1591 | if (FwdBBs.empty()) |
1592 | BasicBlockFwdRefQueue.push_back(x: Fn); |
1593 | if (FwdBBs.size() < BBID + 1) |
1594 | FwdBBs.resize(new_size: BBID + 1); |
1595 | if (!FwdBBs[BBID]) |
1596 | FwdBBs[BBID] = BasicBlock::Create(Context); |
1597 | BB = FwdBBs[BBID]; |
1598 | } |
1599 | C = BlockAddress::get(F: Fn, BB); |
1600 | break; |
1601 | } |
1602 | case BitcodeConstant::ConstantStructOpcode: |
1603 | C = ConstantStruct::get(T: cast<StructType>(Val: BC->getType()), V: ConstOps); |
1604 | break; |
1605 | case BitcodeConstant::ConstantArrayOpcode: |
1606 | C = ConstantArray::get(T: cast<ArrayType>(Val: BC->getType()), V: ConstOps); |
1607 | break; |
1608 | case BitcodeConstant::ConstantVectorOpcode: |
1609 | C = ConstantVector::get(V: ConstOps); |
1610 | break; |
1611 | case Instruction::ICmp: |
1612 | case Instruction::FCmp: |
1613 | C = ConstantExpr::getCompare(pred: BC->Flags, C1: ConstOps[0], C2: ConstOps[1]); |
1614 | break; |
1615 | case Instruction::GetElementPtr: |
1616 | C = ConstantExpr::getGetElementPtr(Ty: BC->SrcElemTy, C: ConstOps[0], |
1617 | IdxList: ArrayRef(ConstOps).drop_front(), |
1618 | InBounds: BC->Flags, InRange: BC->getInRange()); |
1619 | break; |
1620 | case Instruction::ExtractElement: |
1621 | C = ConstantExpr::getExtractElement(Vec: ConstOps[0], Idx: ConstOps[1]); |
1622 | break; |
1623 | case Instruction::InsertElement: |
1624 | C = ConstantExpr::getInsertElement(Vec: ConstOps[0], Elt: ConstOps[1], |
1625 | Idx: ConstOps[2]); |
1626 | break; |
1627 | case Instruction::ShuffleVector: { |
1628 | SmallVector<int, 16> Mask; |
1629 | ShuffleVectorInst::getShuffleMask(Mask: ConstOps[2], Result&: Mask); |
1630 | C = ConstantExpr::getShuffleVector(V1: ConstOps[0], V2: ConstOps[1], Mask); |
1631 | break; |
1632 | } |
1633 | default: |
1634 | llvm_unreachable("Unhandled bitcode constant" ); |
1635 | } |
1636 | } |
1637 | |
1638 | // Cache resolved constant. |
1639 | ValueList.replaceValueWithoutRAUW(ValNo: ValID, NewV: C); |
1640 | MaterializedValues.insert(KV: {ValID, C}); |
1641 | Worklist.pop_back(); |
1642 | continue; |
1643 | } |
1644 | |
1645 | if (!InsertBB) |
1646 | return error(Message: Twine("Value referenced by initializer is an unsupported " |
1647 | "constant expression of type " ) + |
1648 | BC->getOpcodeName()); |
1649 | |
1650 | // Materialize as instructions if necessary. |
1651 | Instruction *I; |
1652 | if (Instruction::isCast(Opcode: BC->Opcode)) { |
1653 | I = CastInst::Create((Instruction::CastOps)BC->Opcode, S: Ops[0], |
1654 | Ty: BC->getType(), Name: "constexpr" , InsertAtEnd: InsertBB); |
1655 | } else if (Instruction::isUnaryOp(Opcode: BC->Opcode)) { |
1656 | I = UnaryOperator::Create(Op: (Instruction::UnaryOps)BC->Opcode, S: Ops[0], |
1657 | Name: "constexpr" , InsertAtEnd: InsertBB); |
1658 | } else if (Instruction::isBinaryOp(Opcode: BC->Opcode)) { |
1659 | I = BinaryOperator::Create(Op: (Instruction::BinaryOps)BC->Opcode, S1: Ops[0], |
1660 | S2: Ops[1], Name: "constexpr" , InsertAtEnd: InsertBB); |
1661 | if (isa<OverflowingBinaryOperator>(Val: I)) { |
1662 | if (BC->Flags & OverflowingBinaryOperator::NoSignedWrap) |
1663 | I->setHasNoSignedWrap(); |
1664 | if (BC->Flags & OverflowingBinaryOperator::NoUnsignedWrap) |
1665 | I->setHasNoUnsignedWrap(); |
1666 | } |
1667 | if (isa<PossiblyExactOperator>(Val: I) && |
1668 | (BC->Flags & PossiblyExactOperator::IsExact)) |
1669 | I->setIsExact(); |
1670 | } else { |
1671 | switch (BC->Opcode) { |
1672 | case BitcodeConstant::ConstantVectorOpcode: { |
1673 | Type *IdxTy = Type::getInt32Ty(C&: BC->getContext()); |
1674 | Value *V = PoisonValue::get(T: BC->getType()); |
1675 | for (auto Pair : enumerate(First&: Ops)) { |
1676 | Value *Idx = ConstantInt::get(Ty: IdxTy, V: Pair.index()); |
1677 | V = InsertElementInst::Create(Vec: V, NewElt: Pair.value(), Idx, NameStr: "constexpr.ins" , |
1678 | InsertAtEnd: InsertBB); |
1679 | } |
1680 | I = cast<Instruction>(Val: V); |
1681 | break; |
1682 | } |
1683 | case BitcodeConstant::ConstantStructOpcode: |
1684 | case BitcodeConstant::ConstantArrayOpcode: { |
1685 | Value *V = PoisonValue::get(T: BC->getType()); |
1686 | for (auto Pair : enumerate(First&: Ops)) |
1687 | V = InsertValueInst::Create(Agg: V, Val: Pair.value(), Idxs: Pair.index(), |
1688 | NameStr: "constexpr.ins" , InsertAtEnd: InsertBB); |
1689 | I = cast<Instruction>(Val: V); |
1690 | break; |
1691 | } |
1692 | case Instruction::ICmp: |
1693 | case Instruction::FCmp: |
1694 | I = CmpInst::Create(Op: (Instruction::OtherOps)BC->Opcode, |
1695 | Pred: (CmpInst::Predicate)BC->Flags, S1: Ops[0], S2: Ops[1], |
1696 | Name: "constexpr" , InsertAtEnd: InsertBB); |
1697 | break; |
1698 | case Instruction::GetElementPtr: |
1699 | I = GetElementPtrInst::Create(PointeeType: BC->SrcElemTy, Ptr: Ops[0], |
1700 | IdxList: ArrayRef(Ops).drop_front(), NameStr: "constexpr" , |
1701 | InsertAtEnd: InsertBB); |
1702 | if (BC->Flags) |
1703 | cast<GetElementPtrInst>(Val: I)->setIsInBounds(); |
1704 | break; |
1705 | case Instruction::Select: |
1706 | I = SelectInst::Create(C: Ops[0], S1: Ops[1], S2: Ops[2], NameStr: "constexpr" , InsertAtEnd: InsertBB); |
1707 | break; |
1708 | case Instruction::ExtractElement: |
1709 | I = ExtractElementInst::Create(Vec: Ops[0], Idx: Ops[1], NameStr: "constexpr" , InsertAtEnd: InsertBB); |
1710 | break; |
1711 | case Instruction::InsertElement: |
1712 | I = InsertElementInst::Create(Vec: Ops[0], NewElt: Ops[1], Idx: Ops[2], NameStr: "constexpr" , |
1713 | InsertAtEnd: InsertBB); |
1714 | break; |
1715 | case Instruction::ShuffleVector: |
1716 | I = new ShuffleVectorInst(Ops[0], Ops[1], Ops[2], "constexpr" , |
1717 | InsertBB); |
1718 | break; |
1719 | default: |
1720 | llvm_unreachable("Unhandled bitcode constant" ); |
1721 | } |
1722 | } |
1723 | |
1724 | MaterializedValues.insert(KV: {ValID, I}); |
1725 | Worklist.pop_back(); |
1726 | } |
1727 | |
1728 | return MaterializedValues[StartValID]; |
1729 | } |
1730 | |
1731 | Expected<Constant *> BitcodeReader::getValueForInitializer(unsigned ID) { |
1732 | Expected<Value *> MaybeV = materializeValue(StartValID: ID, /* InsertBB */ nullptr); |
1733 | if (!MaybeV) |
1734 | return MaybeV.takeError(); |
1735 | |
1736 | // Result must be Constant if InsertBB is nullptr. |
1737 | return cast<Constant>(Val: MaybeV.get()); |
1738 | } |
1739 | |
1740 | StructType *BitcodeReader::createIdentifiedStructType(LLVMContext &Context, |
1741 | StringRef Name) { |
1742 | auto *Ret = StructType::create(Context, Name); |
1743 | IdentifiedStructTypes.push_back(x: Ret); |
1744 | return Ret; |
1745 | } |
1746 | |
1747 | StructType *BitcodeReader::createIdentifiedStructType(LLVMContext &Context) { |
1748 | auto *Ret = StructType::create(Context); |
1749 | IdentifiedStructTypes.push_back(x: Ret); |
1750 | return Ret; |
1751 | } |
1752 | |
1753 | //===----------------------------------------------------------------------===// |
1754 | // Functions for parsing blocks from the bitcode file |
1755 | //===----------------------------------------------------------------------===// |
1756 | |
1757 | static uint64_t getRawAttributeMask(Attribute::AttrKind Val) { |
1758 | switch (Val) { |
1759 | case Attribute::EndAttrKinds: |
1760 | case Attribute::EmptyKey: |
1761 | case Attribute::TombstoneKey: |
1762 | llvm_unreachable("Synthetic enumerators which should never get here" ); |
1763 | |
1764 | case Attribute::None: return 0; |
1765 | case Attribute::ZExt: return 1 << 0; |
1766 | case Attribute::SExt: return 1 << 1; |
1767 | case Attribute::NoReturn: return 1 << 2; |
1768 | case Attribute::InReg: return 1 << 3; |
1769 | case Attribute::StructRet: return 1 << 4; |
1770 | case Attribute::NoUnwind: return 1 << 5; |
1771 | case Attribute::NoAlias: return 1 << 6; |
1772 | case Attribute::ByVal: return 1 << 7; |
1773 | case Attribute::Nest: return 1 << 8; |
1774 | case Attribute::ReadNone: return 1 << 9; |
1775 | case Attribute::ReadOnly: return 1 << 10; |
1776 | case Attribute::NoInline: return 1 << 11; |
1777 | case Attribute::AlwaysInline: return 1 << 12; |
1778 | case Attribute::OptimizeForSize: return 1 << 13; |
1779 | case Attribute::StackProtect: return 1 << 14; |
1780 | case Attribute::StackProtectReq: return 1 << 15; |
1781 | case Attribute::Alignment: return 31 << 16; |
1782 | case Attribute::NoCapture: return 1 << 21; |
1783 | case Attribute::NoRedZone: return 1 << 22; |
1784 | case Attribute::NoImplicitFloat: return 1 << 23; |
1785 | case Attribute::Naked: return 1 << 24; |
1786 | case Attribute::InlineHint: return 1 << 25; |
1787 | case Attribute::StackAlignment: return 7 << 26; |
1788 | case Attribute::ReturnsTwice: return 1 << 29; |
1789 | case Attribute::UWTable: return 1 << 30; |
1790 | case Attribute::NonLazyBind: return 1U << 31; |
1791 | case Attribute::SanitizeAddress: return 1ULL << 32; |
1792 | case Attribute::MinSize: return 1ULL << 33; |
1793 | case Attribute::NoDuplicate: return 1ULL << 34; |
1794 | case Attribute::StackProtectStrong: return 1ULL << 35; |
1795 | case Attribute::SanitizeThread: return 1ULL << 36; |
1796 | case Attribute::SanitizeMemory: return 1ULL << 37; |
1797 | case Attribute::NoBuiltin: return 1ULL << 38; |
1798 | case Attribute::Returned: return 1ULL << 39; |
1799 | case Attribute::Cold: return 1ULL << 40; |
1800 | case Attribute::Builtin: return 1ULL << 41; |
1801 | case Attribute::OptimizeNone: return 1ULL << 42; |
1802 | case Attribute::InAlloca: return 1ULL << 43; |
1803 | case Attribute::NonNull: return 1ULL << 44; |
1804 | case Attribute::JumpTable: return 1ULL << 45; |
1805 | case Attribute::Convergent: return 1ULL << 46; |
1806 | case Attribute::SafeStack: return 1ULL << 47; |
1807 | case Attribute::NoRecurse: return 1ULL << 48; |
1808 | // 1ULL << 49 is InaccessibleMemOnly, which is upgraded separately. |
1809 | // 1ULL << 50 is InaccessibleMemOrArgMemOnly, which is upgraded separately. |
1810 | case Attribute::SwiftSelf: return 1ULL << 51; |
1811 | case Attribute::SwiftError: return 1ULL << 52; |
1812 | case Attribute::WriteOnly: return 1ULL << 53; |
1813 | case Attribute::Speculatable: return 1ULL << 54; |
1814 | case Attribute::StrictFP: return 1ULL << 55; |
1815 | case Attribute::SanitizeHWAddress: return 1ULL << 56; |
1816 | case Attribute::NoCfCheck: return 1ULL << 57; |
1817 | case Attribute::OptForFuzzing: return 1ULL << 58; |
1818 | case Attribute::ShadowCallStack: return 1ULL << 59; |
1819 | case Attribute::SpeculativeLoadHardening: |
1820 | return 1ULL << 60; |
1821 | case Attribute::ImmArg: |
1822 | return 1ULL << 61; |
1823 | case Attribute::WillReturn: |
1824 | return 1ULL << 62; |
1825 | case Attribute::NoFree: |
1826 | return 1ULL << 63; |
1827 | default: |
1828 | // Other attributes are not supported in the raw format, |
1829 | // as we ran out of space. |
1830 | return 0; |
1831 | } |
1832 | llvm_unreachable("Unsupported attribute type" ); |
1833 | } |
1834 | |
1835 | static void addRawAttributeValue(AttrBuilder &B, uint64_t Val) { |
1836 | if (!Val) return; |
1837 | |
1838 | for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds; |
1839 | I = Attribute::AttrKind(I + 1)) { |
1840 | if (uint64_t A = (Val & getRawAttributeMask(Val: I))) { |
1841 | if (I == Attribute::Alignment) |
1842 | B.addAlignmentAttr(Align: 1ULL << ((A >> 16) - 1)); |
1843 | else if (I == Attribute::StackAlignment) |
1844 | B.addStackAlignmentAttr(Align: 1ULL << ((A >> 26)-1)); |
1845 | else if (Attribute::isTypeAttrKind(Kind: I)) |
1846 | B.addTypeAttr(Kind: I, Ty: nullptr); // Type will be auto-upgraded. |
1847 | else |
1848 | B.addAttribute(Val: I); |
1849 | } |
1850 | } |
1851 | } |
1852 | |
1853 | /// This fills an AttrBuilder object with the LLVM attributes that have |
1854 | /// been decoded from the given integer. This function must stay in sync with |
1855 | /// 'encodeLLVMAttributesForBitcode'. |
1856 | static void decodeLLVMAttributesForBitcode(AttrBuilder &B, |
1857 | uint64_t EncodedAttrs, |
1858 | uint64_t AttrIdx) { |
1859 | // The alignment is stored as a 16-bit raw value from bits 31--16. We shift |
1860 | // the bits above 31 down by 11 bits. |
1861 | unsigned Alignment = (EncodedAttrs & (0xffffULL << 16)) >> 16; |
1862 | assert((!Alignment || isPowerOf2_32(Alignment)) && |
1863 | "Alignment must be a power of two." ); |
1864 | |
1865 | if (Alignment) |
1866 | B.addAlignmentAttr(Align: Alignment); |
1867 | |
1868 | uint64_t Attrs = ((EncodedAttrs & (0xfffffULL << 32)) >> 11) | |
1869 | (EncodedAttrs & 0xffff); |
1870 | |
1871 | if (AttrIdx == AttributeList::FunctionIndex) { |
1872 | // Upgrade old memory attributes. |
1873 | MemoryEffects ME = MemoryEffects::unknown(); |
1874 | if (Attrs & (1ULL << 9)) { |
1875 | // ReadNone |
1876 | Attrs &= ~(1ULL << 9); |
1877 | ME &= MemoryEffects::none(); |
1878 | } |
1879 | if (Attrs & (1ULL << 10)) { |
1880 | // ReadOnly |
1881 | Attrs &= ~(1ULL << 10); |
1882 | ME &= MemoryEffects::readOnly(); |
1883 | } |
1884 | if (Attrs & (1ULL << 49)) { |
1885 | // InaccessibleMemOnly |
1886 | Attrs &= ~(1ULL << 49); |
1887 | ME &= MemoryEffects::inaccessibleMemOnly(); |
1888 | } |
1889 | if (Attrs & (1ULL << 50)) { |
1890 | // InaccessibleMemOrArgMemOnly |
1891 | Attrs &= ~(1ULL << 50); |
1892 | ME &= MemoryEffects::inaccessibleOrArgMemOnly(); |
1893 | } |
1894 | if (Attrs & (1ULL << 53)) { |
1895 | // WriteOnly |
1896 | Attrs &= ~(1ULL << 53); |
1897 | ME &= MemoryEffects::writeOnly(); |
1898 | } |
1899 | if (ME != MemoryEffects::unknown()) |
1900 | B.addMemoryAttr(ME); |
1901 | } |
1902 | |
1903 | addRawAttributeValue(B, Val: Attrs); |
1904 | } |
1905 | |
1906 | Error BitcodeReader::parseAttributeBlock() { |
1907 | if (Error Err = Stream.EnterSubBlock(BlockID: bitc::PARAMATTR_BLOCK_ID)) |
1908 | return Err; |
1909 | |
1910 | if (!MAttributes.empty()) |
1911 | return error(Message: "Invalid multiple blocks" ); |
1912 | |
1913 | SmallVector<uint64_t, 64> Record; |
1914 | |
1915 | SmallVector<AttributeList, 8> Attrs; |
1916 | |
1917 | // Read all the records. |
1918 | while (true) { |
1919 | Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks(); |
1920 | if (!MaybeEntry) |
1921 | return MaybeEntry.takeError(); |
1922 | BitstreamEntry Entry = MaybeEntry.get(); |
1923 | |
1924 | switch (Entry.Kind) { |
1925 | case BitstreamEntry::SubBlock: // Handled for us already. |
1926 | case BitstreamEntry::Error: |
1927 | return error(Message: "Malformed block" ); |
1928 | case BitstreamEntry::EndBlock: |
1929 | return Error::success(); |
1930 | case BitstreamEntry::Record: |
1931 | // The interesting case. |
1932 | break; |
1933 | } |
1934 | |
1935 | // Read a record. |
1936 | Record.clear(); |
1937 | Expected<unsigned> MaybeRecord = Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record); |
1938 | if (!MaybeRecord) |
1939 | return MaybeRecord.takeError(); |
1940 | switch (MaybeRecord.get()) { |
1941 | default: // Default behavior: ignore. |
1942 | break; |
1943 | case bitc::PARAMATTR_CODE_ENTRY_OLD: // ENTRY: [paramidx0, attr0, ...] |
1944 | // Deprecated, but still needed to read old bitcode files. |
1945 | if (Record.size() & 1) |
1946 | return error(Message: "Invalid parameter attribute record" ); |
1947 | |
1948 | for (unsigned i = 0, e = Record.size(); i != e; i += 2) { |
1949 | AttrBuilder B(Context); |
1950 | decodeLLVMAttributesForBitcode(B, EncodedAttrs: Record[i+1], AttrIdx: Record[i]); |
1951 | Attrs.push_back(Elt: AttributeList::get(C&: Context, Index: Record[i], B)); |
1952 | } |
1953 | |
1954 | MAttributes.push_back(x: AttributeList::get(C&: Context, Attrs)); |
1955 | Attrs.clear(); |
1956 | break; |
1957 | case bitc::PARAMATTR_CODE_ENTRY: // ENTRY: [attrgrp0, attrgrp1, ...] |
1958 | for (unsigned i = 0, e = Record.size(); i != e; ++i) |
1959 | Attrs.push_back(Elt: MAttributeGroups[Record[i]]); |
1960 | |
1961 | MAttributes.push_back(x: AttributeList::get(C&: Context, Attrs)); |
1962 | Attrs.clear(); |
1963 | break; |
1964 | } |
1965 | } |
1966 | } |
1967 | |
1968 | // Returns Attribute::None on unrecognized codes. |
1969 | static Attribute::AttrKind getAttrFromCode(uint64_t Code) { |
1970 | switch (Code) { |
1971 | default: |
1972 | return Attribute::None; |
1973 | case bitc::ATTR_KIND_ALIGNMENT: |
1974 | return Attribute::Alignment; |
1975 | case bitc::ATTR_KIND_ALWAYS_INLINE: |
1976 | return Attribute::AlwaysInline; |
1977 | case bitc::ATTR_KIND_BUILTIN: |
1978 | return Attribute::Builtin; |
1979 | case bitc::ATTR_KIND_BY_VAL: |
1980 | return Attribute::ByVal; |
1981 | case bitc::ATTR_KIND_IN_ALLOCA: |
1982 | return Attribute::InAlloca; |
1983 | case bitc::ATTR_KIND_COLD: |
1984 | return Attribute::Cold; |
1985 | case bitc::ATTR_KIND_CONVERGENT: |
1986 | return Attribute::Convergent; |
1987 | case bitc::ATTR_KIND_DISABLE_SANITIZER_INSTRUMENTATION: |
1988 | return Attribute::DisableSanitizerInstrumentation; |
1989 | case bitc::ATTR_KIND_ELEMENTTYPE: |
1990 | return Attribute::ElementType; |
1991 | case bitc::ATTR_KIND_FNRETTHUNK_EXTERN: |
1992 | return Attribute::FnRetThunkExtern; |
1993 | case bitc::ATTR_KIND_INLINE_HINT: |
1994 | return Attribute::InlineHint; |
1995 | case bitc::ATTR_KIND_IN_REG: |
1996 | return Attribute::InReg; |
1997 | case bitc::ATTR_KIND_JUMP_TABLE: |
1998 | return Attribute::JumpTable; |
1999 | case bitc::ATTR_KIND_MEMORY: |
2000 | return Attribute::Memory; |
2001 | case bitc::ATTR_KIND_NOFPCLASS: |
2002 | return Attribute::NoFPClass; |
2003 | case bitc::ATTR_KIND_MIN_SIZE: |
2004 | return Attribute::MinSize; |
2005 | case bitc::ATTR_KIND_NAKED: |
2006 | return Attribute::Naked; |
2007 | case bitc::ATTR_KIND_NEST: |
2008 | return Attribute::Nest; |
2009 | case bitc::ATTR_KIND_NO_ALIAS: |
2010 | return Attribute::NoAlias; |
2011 | case bitc::ATTR_KIND_NO_BUILTIN: |
2012 | return Attribute::NoBuiltin; |
2013 | case bitc::ATTR_KIND_NO_CALLBACK: |
2014 | return Attribute::NoCallback; |
2015 | case bitc::ATTR_KIND_NO_CAPTURE: |
2016 | return Attribute::NoCapture; |
2017 | case bitc::ATTR_KIND_NO_DUPLICATE: |
2018 | return Attribute::NoDuplicate; |
2019 | case bitc::ATTR_KIND_NOFREE: |
2020 | return Attribute::NoFree; |
2021 | case bitc::ATTR_KIND_NO_IMPLICIT_FLOAT: |
2022 | return Attribute::NoImplicitFloat; |
2023 | case bitc::ATTR_KIND_NO_INLINE: |
2024 | return Attribute::NoInline; |
2025 | case bitc::ATTR_KIND_NO_RECURSE: |
2026 | return Attribute::NoRecurse; |
2027 | case bitc::ATTR_KIND_NO_MERGE: |
2028 | return Attribute::NoMerge; |
2029 | case bitc::ATTR_KIND_NON_LAZY_BIND: |
2030 | return Attribute::NonLazyBind; |
2031 | case bitc::ATTR_KIND_NON_NULL: |
2032 | return Attribute::NonNull; |
2033 | case bitc::ATTR_KIND_DEREFERENCEABLE: |
2034 | return Attribute::Dereferenceable; |
2035 | case bitc::ATTR_KIND_DEREFERENCEABLE_OR_NULL: |
2036 | return Attribute::DereferenceableOrNull; |
2037 | case bitc::ATTR_KIND_ALLOC_ALIGN: |
2038 | return Attribute::AllocAlign; |
2039 | case bitc::ATTR_KIND_ALLOC_KIND: |
2040 | return Attribute::AllocKind; |
2041 | case bitc::ATTR_KIND_ALLOC_SIZE: |
2042 | return Attribute::AllocSize; |
2043 | case bitc::ATTR_KIND_ALLOCATED_POINTER: |
2044 | return Attribute::AllocatedPointer; |
2045 | case bitc::ATTR_KIND_NO_RED_ZONE: |
2046 | return Attribute::NoRedZone; |
2047 | case bitc::ATTR_KIND_NO_RETURN: |
2048 | return Attribute::NoReturn; |
2049 | case bitc::ATTR_KIND_NOSYNC: |
2050 | return Attribute::NoSync; |
2051 | case bitc::ATTR_KIND_NOCF_CHECK: |
2052 | return Attribute::NoCfCheck; |
2053 | case bitc::ATTR_KIND_NO_PROFILE: |
2054 | return Attribute::NoProfile; |
2055 | case bitc::ATTR_KIND_SKIP_PROFILE: |
2056 | return Attribute::SkipProfile; |
2057 | case bitc::ATTR_KIND_NO_UNWIND: |
2058 | return Attribute::NoUnwind; |
2059 | case bitc::ATTR_KIND_NO_SANITIZE_BOUNDS: |
2060 | return Attribute::NoSanitizeBounds; |
2061 | case bitc::ATTR_KIND_NO_SANITIZE_COVERAGE: |
2062 | return Attribute::NoSanitizeCoverage; |
2063 | case bitc::ATTR_KIND_NULL_POINTER_IS_VALID: |
2064 | return Attribute::NullPointerIsValid; |
2065 | case bitc::ATTR_KIND_OPTIMIZE_FOR_DEBUGGING: |
2066 | return Attribute::OptimizeForDebugging; |
2067 | case bitc::ATTR_KIND_OPT_FOR_FUZZING: |
2068 | return Attribute::OptForFuzzing; |
2069 | case bitc::ATTR_KIND_OPTIMIZE_FOR_SIZE: |
2070 | return Attribute::OptimizeForSize; |
2071 | case bitc::ATTR_KIND_OPTIMIZE_NONE: |
2072 | return Attribute::OptimizeNone; |
2073 | case bitc::ATTR_KIND_READ_NONE: |
2074 | return Attribute::ReadNone; |
2075 | case bitc::ATTR_KIND_READ_ONLY: |
2076 | return Attribute::ReadOnly; |
2077 | case bitc::ATTR_KIND_RETURNED: |
2078 | return Attribute::Returned; |
2079 | case bitc::ATTR_KIND_RETURNS_TWICE: |
2080 | return Attribute::ReturnsTwice; |
2081 | case bitc::ATTR_KIND_S_EXT: |
2082 | return Attribute::SExt; |
2083 | case bitc::ATTR_KIND_SPECULATABLE: |
2084 | return Attribute::Speculatable; |
2085 | case bitc::ATTR_KIND_STACK_ALIGNMENT: |
2086 | return Attribute::StackAlignment; |
2087 | case bitc::ATTR_KIND_STACK_PROTECT: |
2088 | return Attribute::StackProtect; |
2089 | case bitc::ATTR_KIND_STACK_PROTECT_REQ: |
2090 | return Attribute::StackProtectReq; |
2091 | case bitc::ATTR_KIND_STACK_PROTECT_STRONG: |
2092 | return Attribute::StackProtectStrong; |
2093 | case bitc::ATTR_KIND_SAFESTACK: |
2094 | return Attribute::SafeStack; |
2095 | case bitc::ATTR_KIND_SHADOWCALLSTACK: |
2096 | return Attribute::ShadowCallStack; |
2097 | case bitc::ATTR_KIND_STRICT_FP: |
2098 | return Attribute::StrictFP; |
2099 | case bitc::ATTR_KIND_STRUCT_RET: |
2100 | return Attribute::StructRet; |
2101 | case bitc::ATTR_KIND_SANITIZE_ADDRESS: |
2102 | return Attribute::SanitizeAddress; |
2103 | case bitc::ATTR_KIND_SANITIZE_HWADDRESS: |
2104 | return Attribute::SanitizeHWAddress; |
2105 | case bitc::ATTR_KIND_SANITIZE_THREAD: |
2106 | return Attribute::SanitizeThread; |
2107 | case bitc::ATTR_KIND_SANITIZE_MEMORY: |
2108 | return Attribute::SanitizeMemory; |
2109 | case bitc::ATTR_KIND_SPECULATIVE_LOAD_HARDENING: |
2110 | return Attribute::SpeculativeLoadHardening; |
2111 | case bitc::ATTR_KIND_SWIFT_ERROR: |
2112 | return Attribute::SwiftError; |
2113 | case bitc::ATTR_KIND_SWIFT_SELF: |
2114 | return Attribute::SwiftSelf; |
2115 | case bitc::ATTR_KIND_SWIFT_ASYNC: |
2116 | return Attribute::SwiftAsync; |
2117 | case bitc::ATTR_KIND_UW_TABLE: |
2118 | return Attribute::UWTable; |
2119 | case bitc::ATTR_KIND_VSCALE_RANGE: |
2120 | return Attribute::VScaleRange; |
2121 | case bitc::ATTR_KIND_WILLRETURN: |
2122 | return Attribute::WillReturn; |
2123 | case bitc::ATTR_KIND_WRITEONLY: |
2124 | return Attribute::WriteOnly; |
2125 | case bitc::ATTR_KIND_Z_EXT: |
2126 | return Attribute::ZExt; |
2127 | case bitc::ATTR_KIND_IMMARG: |
2128 | return Attribute::ImmArg; |
2129 | case bitc::ATTR_KIND_SANITIZE_MEMTAG: |
2130 | return Attribute::SanitizeMemTag; |
2131 | case bitc::ATTR_KIND_PREALLOCATED: |
2132 | return Attribute::Preallocated; |
2133 | case bitc::ATTR_KIND_NOUNDEF: |
2134 | return Attribute::NoUndef; |
2135 | case bitc::ATTR_KIND_BYREF: |
2136 | return Attribute::ByRef; |
2137 | case bitc::ATTR_KIND_MUSTPROGRESS: |
2138 | return Attribute::MustProgress; |
2139 | case bitc::ATTR_KIND_HOT: |
2140 | return Attribute::Hot; |
2141 | case bitc::ATTR_KIND_PRESPLIT_COROUTINE: |
2142 | return Attribute::PresplitCoroutine; |
2143 | case bitc::ATTR_KIND_WRITABLE: |
2144 | return Attribute::Writable; |
2145 | case bitc::ATTR_KIND_CORO_ONLY_DESTROY_WHEN_COMPLETE: |
2146 | return Attribute::CoroDestroyOnlyWhenComplete; |
2147 | case bitc::ATTR_KIND_DEAD_ON_UNWIND: |
2148 | return Attribute::DeadOnUnwind; |
2149 | case bitc::ATTR_KIND_RANGE: |
2150 | return Attribute::Range; |
2151 | } |
2152 | } |
2153 | |
2154 | Error BitcodeReader::parseAlignmentValue(uint64_t Exponent, |
2155 | MaybeAlign &Alignment) { |
2156 | // Note: Alignment in bitcode files is incremented by 1, so that zero |
2157 | // can be used for default alignment. |
2158 | if (Exponent > Value::MaxAlignmentExponent + 1) |
2159 | return error(Message: "Invalid alignment value" ); |
2160 | Alignment = decodeMaybeAlign(Value: Exponent); |
2161 | return Error::success(); |
2162 | } |
2163 | |
2164 | Error BitcodeReader::parseAttrKind(uint64_t Code, Attribute::AttrKind *Kind) { |
2165 | *Kind = getAttrFromCode(Code); |
2166 | if (*Kind == Attribute::None) |
2167 | return error(Message: "Unknown attribute kind (" + Twine(Code) + ")" ); |
2168 | return Error::success(); |
2169 | } |
2170 | |
2171 | static bool upgradeOldMemoryAttribute(MemoryEffects &ME, uint64_t EncodedKind) { |
2172 | switch (EncodedKind) { |
2173 | case bitc::ATTR_KIND_READ_NONE: |
2174 | ME &= MemoryEffects::none(); |
2175 | return true; |
2176 | case bitc::ATTR_KIND_READ_ONLY: |
2177 | ME &= MemoryEffects::readOnly(); |
2178 | return true; |
2179 | case bitc::ATTR_KIND_WRITEONLY: |
2180 | ME &= MemoryEffects::writeOnly(); |
2181 | return true; |
2182 | case bitc::ATTR_KIND_ARGMEMONLY: |
2183 | ME &= MemoryEffects::argMemOnly(); |
2184 | return true; |
2185 | case bitc::ATTR_KIND_INACCESSIBLEMEM_ONLY: |
2186 | ME &= MemoryEffects::inaccessibleMemOnly(); |
2187 | return true; |
2188 | case bitc::ATTR_KIND_INACCESSIBLEMEM_OR_ARGMEMONLY: |
2189 | ME &= MemoryEffects::inaccessibleOrArgMemOnly(); |
2190 | return true; |
2191 | default: |
2192 | return false; |
2193 | } |
2194 | } |
2195 | |
2196 | Error BitcodeReader::parseAttributeGroupBlock() { |
2197 | if (Error Err = Stream.EnterSubBlock(BlockID: bitc::PARAMATTR_GROUP_BLOCK_ID)) |
2198 | return Err; |
2199 | |
2200 | if (!MAttributeGroups.empty()) |
2201 | return error(Message: "Invalid multiple blocks" ); |
2202 | |
2203 | SmallVector<uint64_t, 64> Record; |
2204 | |
2205 | // Read all the records. |
2206 | while (true) { |
2207 | Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks(); |
2208 | if (!MaybeEntry) |
2209 | return MaybeEntry.takeError(); |
2210 | BitstreamEntry Entry = MaybeEntry.get(); |
2211 | |
2212 | switch (Entry.Kind) { |
2213 | case BitstreamEntry::SubBlock: // Handled for us already. |
2214 | case BitstreamEntry::Error: |
2215 | return error(Message: "Malformed block" ); |
2216 | case BitstreamEntry::EndBlock: |
2217 | return Error::success(); |
2218 | case BitstreamEntry::Record: |
2219 | // The interesting case. |
2220 | break; |
2221 | } |
2222 | |
2223 | // Read a record. |
2224 | Record.clear(); |
2225 | Expected<unsigned> MaybeRecord = Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record); |
2226 | if (!MaybeRecord) |
2227 | return MaybeRecord.takeError(); |
2228 | switch (MaybeRecord.get()) { |
2229 | default: // Default behavior: ignore. |
2230 | break; |
2231 | case bitc::PARAMATTR_GRP_CODE_ENTRY: { // ENTRY: [grpid, idx, a0, a1, ...] |
2232 | if (Record.size() < 3) |
2233 | return error(Message: "Invalid grp record" ); |
2234 | |
2235 | uint64_t GrpID = Record[0]; |
2236 | uint64_t Idx = Record[1]; // Index of the object this attribute refers to. |
2237 | |
2238 | AttrBuilder B(Context); |
2239 | MemoryEffects ME = MemoryEffects::unknown(); |
2240 | for (unsigned i = 2, e = Record.size(); i != e; ++i) { |
2241 | if (Record[i] == 0) { // Enum attribute |
2242 | Attribute::AttrKind Kind; |
2243 | uint64_t EncodedKind = Record[++i]; |
2244 | if (Idx == AttributeList::FunctionIndex && |
2245 | upgradeOldMemoryAttribute(ME, EncodedKind)) |
2246 | continue; |
2247 | |
2248 | if (Error Err = parseAttrKind(Code: EncodedKind, Kind: &Kind)) |
2249 | return Err; |
2250 | |
2251 | // Upgrade old-style byval attribute to one with a type, even if it's |
2252 | // nullptr. We will have to insert the real type when we associate |
2253 | // this AttributeList with a function. |
2254 | if (Kind == Attribute::ByVal) |
2255 | B.addByValAttr(Ty: nullptr); |
2256 | else if (Kind == Attribute::StructRet) |
2257 | B.addStructRetAttr(Ty: nullptr); |
2258 | else if (Kind == Attribute::InAlloca) |
2259 | B.addInAllocaAttr(Ty: nullptr); |
2260 | else if (Kind == Attribute::UWTable) |
2261 | B.addUWTableAttr(Kind: UWTableKind::Default); |
2262 | else if (Attribute::isEnumAttrKind(Kind)) |
2263 | B.addAttribute(Val: Kind); |
2264 | else |
2265 | return error(Message: "Not an enum attribute" ); |
2266 | } else if (Record[i] == 1) { // Integer attribute |
2267 | Attribute::AttrKind Kind; |
2268 | if (Error Err = parseAttrKind(Code: Record[++i], Kind: &Kind)) |
2269 | return Err; |
2270 | if (!Attribute::isIntAttrKind(Kind)) |
2271 | return error(Message: "Not an int attribute" ); |
2272 | if (Kind == Attribute::Alignment) |
2273 | B.addAlignmentAttr(Align: Record[++i]); |
2274 | else if (Kind == Attribute::StackAlignment) |
2275 | B.addStackAlignmentAttr(Align: Record[++i]); |
2276 | else if (Kind == Attribute::Dereferenceable) |
2277 | B.addDereferenceableAttr(Bytes: Record[++i]); |
2278 | else if (Kind == Attribute::DereferenceableOrNull) |
2279 | B.addDereferenceableOrNullAttr(Bytes: Record[++i]); |
2280 | else if (Kind == Attribute::AllocSize) |
2281 | B.addAllocSizeAttrFromRawRepr(RawAllocSizeRepr: Record[++i]); |
2282 | else if (Kind == Attribute::VScaleRange) |
2283 | B.addVScaleRangeAttrFromRawRepr(RawVScaleRangeRepr: Record[++i]); |
2284 | else if (Kind == Attribute::UWTable) |
2285 | B.addUWTableAttr(Kind: UWTableKind(Record[++i])); |
2286 | else if (Kind == Attribute::AllocKind) |
2287 | B.addAllocKindAttr(Kind: static_cast<AllocFnKind>(Record[++i])); |
2288 | else if (Kind == Attribute::Memory) |
2289 | B.addMemoryAttr(ME: MemoryEffects::createFromIntValue(Data: Record[++i])); |
2290 | else if (Kind == Attribute::NoFPClass) |
2291 | B.addNoFPClassAttr( |
2292 | NoFPClassMask: static_cast<FPClassTest>(Record[++i] & fcAllFlags)); |
2293 | } else if (Record[i] == 3 || Record[i] == 4) { // String attribute |
2294 | bool HasValue = (Record[i++] == 4); |
2295 | SmallString<64> KindStr; |
2296 | SmallString<64> ValStr; |
2297 | |
2298 | while (Record[i] != 0 && i != e) |
2299 | KindStr += Record[i++]; |
2300 | assert(Record[i] == 0 && "Kind string not null terminated" ); |
2301 | |
2302 | if (HasValue) { |
2303 | // Has a value associated with it. |
2304 | ++i; // Skip the '0' that terminates the "kind" string. |
2305 | while (Record[i] != 0 && i != e) |
2306 | ValStr += Record[i++]; |
2307 | assert(Record[i] == 0 && "Value string not null terminated" ); |
2308 | } |
2309 | |
2310 | B.addAttribute(A: KindStr.str(), V: ValStr.str()); |
2311 | } else if (Record[i] == 5 || Record[i] == 6) { |
2312 | bool HasType = Record[i] == 6; |
2313 | Attribute::AttrKind Kind; |
2314 | if (Error Err = parseAttrKind(Code: Record[++i], Kind: &Kind)) |
2315 | return Err; |
2316 | if (!Attribute::isTypeAttrKind(Kind)) |
2317 | return error(Message: "Not a type attribute" ); |
2318 | |
2319 | B.addTypeAttr(Kind, Ty: HasType ? getTypeByID(ID: Record[++i]) : nullptr); |
2320 | } else if (Record[i] == 7) { |
2321 | Attribute::AttrKind Kind; |
2322 | |
2323 | i++; |
2324 | if (Error Err = parseAttrKind(Code: Record[i++], Kind: &Kind)) |
2325 | return Err; |
2326 | if (!Attribute::isConstantRangeAttrKind(Kind)) |
2327 | return error(Message: "Not a ConstantRange attribute" ); |
2328 | |
2329 | Expected<ConstantRange> MaybeCR = readConstantRange(Record, OpNum&: i); |
2330 | if (!MaybeCR) |
2331 | return MaybeCR.takeError(); |
2332 | i--; |
2333 | |
2334 | B.addConstantRangeAttr(Kind, CR: MaybeCR.get()); |
2335 | } else { |
2336 | return error(Message: "Invalid attribute group entry" ); |
2337 | } |
2338 | } |
2339 | |
2340 | if (ME != MemoryEffects::unknown()) |
2341 | B.addMemoryAttr(ME); |
2342 | |
2343 | UpgradeAttributes(B); |
2344 | MAttributeGroups[GrpID] = AttributeList::get(C&: Context, Index: Idx, B); |
2345 | break; |
2346 | } |
2347 | } |
2348 | } |
2349 | } |
2350 | |
2351 | Error BitcodeReader::parseTypeTable() { |
2352 | if (Error Err = Stream.EnterSubBlock(BlockID: bitc::TYPE_BLOCK_ID_NEW)) |
2353 | return Err; |
2354 | |
2355 | return parseTypeTableBody(); |
2356 | } |
2357 | |
2358 | Error BitcodeReader::parseTypeTableBody() { |
2359 | if (!TypeList.empty()) |
2360 | return error(Message: "Invalid multiple blocks" ); |
2361 | |
2362 | SmallVector<uint64_t, 64> Record; |
2363 | unsigned NumRecords = 0; |
2364 | |
2365 | SmallString<64> TypeName; |
2366 | |
2367 | // Read all the records for this type table. |
2368 | while (true) { |
2369 | Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks(); |
2370 | if (!MaybeEntry) |
2371 | return MaybeEntry.takeError(); |
2372 | BitstreamEntry Entry = MaybeEntry.get(); |
2373 | |
2374 | switch (Entry.Kind) { |
2375 | case BitstreamEntry::SubBlock: // Handled for us already. |
2376 | case BitstreamEntry::Error: |
2377 | return error(Message: "Malformed block" ); |
2378 | case BitstreamEntry::EndBlock: |
2379 | if (NumRecords != TypeList.size()) |
2380 | return error(Message: "Malformed block" ); |
2381 | return Error::success(); |
2382 | case BitstreamEntry::Record: |
2383 | // The interesting case. |
2384 | break; |
2385 | } |
2386 | |
2387 | // Read a record. |
2388 | Record.clear(); |
2389 | Type *ResultTy = nullptr; |
2390 | SmallVector<unsigned> ContainedIDs; |
2391 | Expected<unsigned> MaybeRecord = Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record); |
2392 | if (!MaybeRecord) |
2393 | return MaybeRecord.takeError(); |
2394 | switch (MaybeRecord.get()) { |
2395 | default: |
2396 | return error(Message: "Invalid value" ); |
2397 | case bitc::TYPE_CODE_NUMENTRY: // TYPE_CODE_NUMENTRY: [numentries] |
2398 | // TYPE_CODE_NUMENTRY contains a count of the number of types in the |
2399 | // type list. This allows us to reserve space. |
2400 | if (Record.empty()) |
2401 | return error(Message: "Invalid numentry record" ); |
2402 | TypeList.resize(new_size: Record[0]); |
2403 | continue; |
2404 | case bitc::TYPE_CODE_VOID: // VOID |
2405 | ResultTy = Type::getVoidTy(C&: Context); |
2406 | break; |
2407 | case bitc::TYPE_CODE_HALF: // HALF |
2408 | ResultTy = Type::getHalfTy(C&: Context); |
2409 | break; |
2410 | case bitc::TYPE_CODE_BFLOAT: // BFLOAT |
2411 | ResultTy = Type::getBFloatTy(C&: Context); |
2412 | break; |
2413 | case bitc::TYPE_CODE_FLOAT: // FLOAT |
2414 | ResultTy = Type::getFloatTy(C&: Context); |
2415 | break; |
2416 | case bitc::TYPE_CODE_DOUBLE: // DOUBLE |
2417 | ResultTy = Type::getDoubleTy(C&: Context); |
2418 | break; |
2419 | case bitc::TYPE_CODE_X86_FP80: // X86_FP80 |
2420 | ResultTy = Type::getX86_FP80Ty(C&: Context); |
2421 | break; |
2422 | case bitc::TYPE_CODE_FP128: // FP128 |
2423 | ResultTy = Type::getFP128Ty(C&: Context); |
2424 | break; |
2425 | case bitc::TYPE_CODE_PPC_FP128: // PPC_FP128 |
2426 | ResultTy = Type::getPPC_FP128Ty(C&: Context); |
2427 | break; |
2428 | case bitc::TYPE_CODE_LABEL: // LABEL |
2429 | ResultTy = Type::getLabelTy(C&: Context); |
2430 | break; |
2431 | case bitc::TYPE_CODE_METADATA: // METADATA |
2432 | ResultTy = Type::getMetadataTy(C&: Context); |
2433 | break; |
2434 | case bitc::TYPE_CODE_X86_MMX: // X86_MMX |
2435 | ResultTy = Type::getX86_MMXTy(C&: Context); |
2436 | break; |
2437 | case bitc::TYPE_CODE_X86_AMX: // X86_AMX |
2438 | ResultTy = Type::getX86_AMXTy(C&: Context); |
2439 | break; |
2440 | case bitc::TYPE_CODE_TOKEN: // TOKEN |
2441 | ResultTy = Type::getTokenTy(C&: Context); |
2442 | break; |
2443 | case bitc::TYPE_CODE_INTEGER: { // INTEGER: [width] |
2444 | if (Record.empty()) |
2445 | return error(Message: "Invalid integer record" ); |
2446 | |
2447 | uint64_t NumBits = Record[0]; |
2448 | if (NumBits < IntegerType::MIN_INT_BITS || |
2449 | NumBits > IntegerType::MAX_INT_BITS) |
2450 | return error(Message: "Bitwidth for integer type out of range" ); |
2451 | ResultTy = IntegerType::get(C&: Context, NumBits); |
2452 | break; |
2453 | } |
2454 | case bitc::TYPE_CODE_POINTER: { // POINTER: [pointee type] or |
2455 | // [pointee type, address space] |
2456 | if (Record.empty()) |
2457 | return error(Message: "Invalid pointer record" ); |
2458 | unsigned AddressSpace = 0; |
2459 | if (Record.size() == 2) |
2460 | AddressSpace = Record[1]; |
2461 | ResultTy = getTypeByID(ID: Record[0]); |
2462 | if (!ResultTy || |
2463 | !PointerType::isValidElementType(ElemTy: ResultTy)) |
2464 | return error(Message: "Invalid type" ); |
2465 | ContainedIDs.push_back(Elt: Record[0]); |
2466 | ResultTy = PointerType::get(ElementType: ResultTy, AddressSpace); |
2467 | break; |
2468 | } |
2469 | case bitc::TYPE_CODE_OPAQUE_POINTER: { // OPAQUE_POINTER: [addrspace] |
2470 | if (Record.size() != 1) |
2471 | return error(Message: "Invalid opaque pointer record" ); |
2472 | unsigned AddressSpace = Record[0]; |
2473 | ResultTy = PointerType::get(C&: Context, AddressSpace); |
2474 | break; |
2475 | } |
2476 | case bitc::TYPE_CODE_FUNCTION_OLD: { |
2477 | // Deprecated, but still needed to read old bitcode files. |
2478 | // FUNCTION: [vararg, attrid, retty, paramty x N] |
2479 | if (Record.size() < 3) |
2480 | return error(Message: "Invalid function record" ); |
2481 | SmallVector<Type*, 8> ArgTys; |
2482 | for (unsigned i = 3, e = Record.size(); i != e; ++i) { |
2483 | if (Type *T = getTypeByID(ID: Record[i])) |
2484 | ArgTys.push_back(Elt: T); |
2485 | else |
2486 | break; |
2487 | } |
2488 | |
2489 | ResultTy = getTypeByID(ID: Record[2]); |
2490 | if (!ResultTy || ArgTys.size() < Record.size()-3) |
2491 | return error(Message: "Invalid type" ); |
2492 | |
2493 | ContainedIDs.append(in_start: Record.begin() + 2, in_end: Record.end()); |
2494 | ResultTy = FunctionType::get(Result: ResultTy, Params: ArgTys, isVarArg: Record[0]); |
2495 | break; |
2496 | } |
2497 | case bitc::TYPE_CODE_FUNCTION: { |
2498 | // FUNCTION: [vararg, retty, paramty x N] |
2499 | if (Record.size() < 2) |
2500 | return error(Message: "Invalid function record" ); |
2501 | SmallVector<Type*, 8> ArgTys; |
2502 | for (unsigned i = 2, e = Record.size(); i != e; ++i) { |
2503 | if (Type *T = getTypeByID(ID: Record[i])) { |
2504 | if (!FunctionType::isValidArgumentType(ArgTy: T)) |
2505 | return error(Message: "Invalid function argument type" ); |
2506 | ArgTys.push_back(Elt: T); |
2507 | } |
2508 | else |
2509 | break; |
2510 | } |
2511 | |
2512 | ResultTy = getTypeByID(ID: Record[1]); |
2513 | if (!ResultTy || ArgTys.size() < Record.size()-2) |
2514 | return error(Message: "Invalid type" ); |
2515 | |
2516 | ContainedIDs.append(in_start: Record.begin() + 1, in_end: Record.end()); |
2517 | ResultTy = FunctionType::get(Result: ResultTy, Params: ArgTys, isVarArg: Record[0]); |
2518 | break; |
2519 | } |
2520 | case bitc::TYPE_CODE_STRUCT_ANON: { // STRUCT: [ispacked, eltty x N] |
2521 | if (Record.empty()) |
2522 | return error(Message: "Invalid anon struct record" ); |
2523 | SmallVector<Type*, 8> EltTys; |
2524 | for (unsigned i = 1, e = Record.size(); i != e; ++i) { |
2525 | if (Type *T = getTypeByID(ID: Record[i])) |
2526 | EltTys.push_back(Elt: T); |
2527 | else |
2528 | break; |
2529 | } |
2530 | if (EltTys.size() != Record.size()-1) |
2531 | return error(Message: "Invalid type" ); |
2532 | ContainedIDs.append(in_start: Record.begin() + 1, in_end: Record.end()); |
2533 | ResultTy = StructType::get(Context, Elements: EltTys, isPacked: Record[0]); |
2534 | break; |
2535 | } |
2536 | case bitc::TYPE_CODE_STRUCT_NAME: // STRUCT_NAME: [strchr x N] |
2537 | if (convertToString(Record, Idx: 0, Result&: TypeName)) |
2538 | return error(Message: "Invalid struct name record" ); |
2539 | continue; |
2540 | |
2541 | case bitc::TYPE_CODE_STRUCT_NAMED: { // STRUCT: [ispacked, eltty x N] |
2542 | if (Record.empty()) |
2543 | return error(Message: "Invalid named struct record" ); |
2544 | |
2545 | if (NumRecords >= TypeList.size()) |
2546 | return error(Message: "Invalid TYPE table" ); |
2547 | |
2548 | // Check to see if this was forward referenced, if so fill in the temp. |
2549 | StructType *Res = cast_or_null<StructType>(Val: TypeList[NumRecords]); |
2550 | if (Res) { |
2551 | Res->setName(TypeName); |
2552 | TypeList[NumRecords] = nullptr; |
2553 | } else // Otherwise, create a new struct. |
2554 | Res = createIdentifiedStructType(Context, Name: TypeName); |
2555 | TypeName.clear(); |
2556 | |
2557 | SmallVector<Type*, 8> EltTys; |
2558 | for (unsigned i = 1, e = Record.size(); i != e; ++i) { |
2559 | if (Type *T = getTypeByID(ID: Record[i])) |
2560 | EltTys.push_back(Elt: T); |
2561 | else |
2562 | break; |
2563 | } |
2564 | if (EltTys.size() != Record.size()-1) |
2565 | return error(Message: "Invalid named struct record" ); |
2566 | Res->setBody(Elements: EltTys, isPacked: Record[0]); |
2567 | ContainedIDs.append(in_start: Record.begin() + 1, in_end: Record.end()); |
2568 | ResultTy = Res; |
2569 | break; |
2570 | } |
2571 | case bitc::TYPE_CODE_OPAQUE: { // OPAQUE: [] |
2572 | if (Record.size() != 1) |
2573 | return error(Message: "Invalid opaque type record" ); |
2574 | |
2575 | if (NumRecords >= TypeList.size()) |
2576 | return error(Message: "Invalid TYPE table" ); |
2577 | |
2578 | // Check to see if this was forward referenced, if so fill in the temp. |
2579 | StructType *Res = cast_or_null<StructType>(Val: TypeList[NumRecords]); |
2580 | if (Res) { |
2581 | Res->setName(TypeName); |
2582 | TypeList[NumRecords] = nullptr; |
2583 | } else // Otherwise, create a new struct with no body. |
2584 | Res = createIdentifiedStructType(Context, Name: TypeName); |
2585 | TypeName.clear(); |
2586 | ResultTy = Res; |
2587 | break; |
2588 | } |
2589 | case bitc::TYPE_CODE_TARGET_TYPE: { // TARGET_TYPE: [NumTy, Tys..., Ints...] |
2590 | if (Record.size() < 1) |
2591 | return error(Message: "Invalid target extension type record" ); |
2592 | |
2593 | if (NumRecords >= TypeList.size()) |
2594 | return error(Message: "Invalid TYPE table" ); |
2595 | |
2596 | if (Record[0] >= Record.size()) |
2597 | return error(Message: "Too many type parameters" ); |
2598 | |
2599 | unsigned NumTys = Record[0]; |
2600 | SmallVector<Type *, 4> TypeParams; |
2601 | SmallVector<unsigned, 8> IntParams; |
2602 | for (unsigned i = 0; i < NumTys; i++) { |
2603 | if (Type *T = getTypeByID(ID: Record[i + 1])) |
2604 | TypeParams.push_back(Elt: T); |
2605 | else |
2606 | return error(Message: "Invalid type" ); |
2607 | } |
2608 | |
2609 | for (unsigned i = NumTys + 1, e = Record.size(); i < e; i++) { |
2610 | if (Record[i] > UINT_MAX) |
2611 | return error(Message: "Integer parameter too large" ); |
2612 | IntParams.push_back(Elt: Record[i]); |
2613 | } |
2614 | ResultTy = TargetExtType::get(Context, Name: TypeName, Types: TypeParams, Ints: IntParams); |
2615 | TypeName.clear(); |
2616 | break; |
2617 | } |
2618 | case bitc::TYPE_CODE_ARRAY: // ARRAY: [numelts, eltty] |
2619 | if (Record.size() < 2) |
2620 | return error(Message: "Invalid array type record" ); |
2621 | ResultTy = getTypeByID(ID: Record[1]); |
2622 | if (!ResultTy || !ArrayType::isValidElementType(ElemTy: ResultTy)) |
2623 | return error(Message: "Invalid type" ); |
2624 | ContainedIDs.push_back(Elt: Record[1]); |
2625 | ResultTy = ArrayType::get(ElementType: ResultTy, NumElements: Record[0]); |
2626 | break; |
2627 | case bitc::TYPE_CODE_VECTOR: // VECTOR: [numelts, eltty] or |
2628 | // [numelts, eltty, scalable] |
2629 | if (Record.size() < 2) |
2630 | return error(Message: "Invalid vector type record" ); |
2631 | if (Record[0] == 0) |
2632 | return error(Message: "Invalid vector length" ); |
2633 | ResultTy = getTypeByID(ID: Record[1]); |
2634 | if (!ResultTy || !VectorType::isValidElementType(ElemTy: ResultTy)) |
2635 | return error(Message: "Invalid type" ); |
2636 | bool Scalable = Record.size() > 2 ? Record[2] : false; |
2637 | ContainedIDs.push_back(Elt: Record[1]); |
2638 | ResultTy = VectorType::get(ElementType: ResultTy, NumElements: Record[0], Scalable); |
2639 | break; |
2640 | } |
2641 | |
2642 | if (NumRecords >= TypeList.size()) |
2643 | return error(Message: "Invalid TYPE table" ); |
2644 | if (TypeList[NumRecords]) |
2645 | return error( |
2646 | Message: "Invalid TYPE table: Only named structs can be forward referenced" ); |
2647 | assert(ResultTy && "Didn't read a type?" ); |
2648 | TypeList[NumRecords] = ResultTy; |
2649 | if (!ContainedIDs.empty()) |
2650 | ContainedTypeIDs[NumRecords] = std::move(ContainedIDs); |
2651 | ++NumRecords; |
2652 | } |
2653 | } |
2654 | |
2655 | Error BitcodeReader::parseOperandBundleTags() { |
2656 | if (Error Err = Stream.EnterSubBlock(BlockID: bitc::OPERAND_BUNDLE_TAGS_BLOCK_ID)) |
2657 | return Err; |
2658 | |
2659 | if (!BundleTags.empty()) |
2660 | return error(Message: "Invalid multiple blocks" ); |
2661 | |
2662 | SmallVector<uint64_t, 64> Record; |
2663 | |
2664 | while (true) { |
2665 | Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks(); |
2666 | if (!MaybeEntry) |
2667 | return MaybeEntry.takeError(); |
2668 | BitstreamEntry Entry = MaybeEntry.get(); |
2669 | |
2670 | switch (Entry.Kind) { |
2671 | case BitstreamEntry::SubBlock: // Handled for us already. |
2672 | case BitstreamEntry::Error: |
2673 | return error(Message: "Malformed block" ); |
2674 | case BitstreamEntry::EndBlock: |
2675 | return Error::success(); |
2676 | case BitstreamEntry::Record: |
2677 | // The interesting case. |
2678 | break; |
2679 | } |
2680 | |
2681 | // Tags are implicitly mapped to integers by their order. |
2682 | |
2683 | Expected<unsigned> MaybeRecord = Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record); |
2684 | if (!MaybeRecord) |
2685 | return MaybeRecord.takeError(); |
2686 | if (MaybeRecord.get() != bitc::OPERAND_BUNDLE_TAG) |
2687 | return error(Message: "Invalid operand bundle record" ); |
2688 | |
2689 | // OPERAND_BUNDLE_TAG: [strchr x N] |
2690 | BundleTags.emplace_back(); |
2691 | if (convertToString(Record, Idx: 0, Result&: BundleTags.back())) |
2692 | return error(Message: "Invalid operand bundle record" ); |
2693 | Record.clear(); |
2694 | } |
2695 | } |
2696 | |
2697 | Error BitcodeReader::parseSyncScopeNames() { |
2698 | if (Error Err = Stream.EnterSubBlock(BlockID: bitc::SYNC_SCOPE_NAMES_BLOCK_ID)) |
2699 | return Err; |
2700 | |
2701 | if (!SSIDs.empty()) |
2702 | return error(Message: "Invalid multiple synchronization scope names blocks" ); |
2703 | |
2704 | SmallVector<uint64_t, 64> Record; |
2705 | while (true) { |
2706 | Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks(); |
2707 | if (!MaybeEntry) |
2708 | return MaybeEntry.takeError(); |
2709 | BitstreamEntry Entry = MaybeEntry.get(); |
2710 | |
2711 | switch (Entry.Kind) { |
2712 | case BitstreamEntry::SubBlock: // Handled for us already. |
2713 | case BitstreamEntry::Error: |
2714 | return error(Message: "Malformed block" ); |
2715 | case BitstreamEntry::EndBlock: |
2716 | if (SSIDs.empty()) |
2717 | return error(Message: "Invalid empty synchronization scope names block" ); |
2718 | return Error::success(); |
2719 | case BitstreamEntry::Record: |
2720 | // The interesting case. |
2721 | break; |
2722 | } |
2723 | |
2724 | // Synchronization scope names are implicitly mapped to synchronization |
2725 | // scope IDs by their order. |
2726 | |
2727 | Expected<unsigned> MaybeRecord = Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record); |
2728 | if (!MaybeRecord) |
2729 | return MaybeRecord.takeError(); |
2730 | if (MaybeRecord.get() != bitc::SYNC_SCOPE_NAME) |
2731 | return error(Message: "Invalid sync scope record" ); |
2732 | |
2733 | SmallString<16> SSN; |
2734 | if (convertToString(Record, Idx: 0, Result&: SSN)) |
2735 | return error(Message: "Invalid sync scope record" ); |
2736 | |
2737 | SSIDs.push_back(Elt: Context.getOrInsertSyncScopeID(SSN)); |
2738 | Record.clear(); |
2739 | } |
2740 | } |
2741 | |
2742 | /// Associate a value with its name from the given index in the provided record. |
2743 | Expected<Value *> BitcodeReader::recordValue(SmallVectorImpl<uint64_t> &Record, |
2744 | unsigned NameIndex, Triple &TT) { |
2745 | SmallString<128> ValueName; |
2746 | if (convertToString(Record, Idx: NameIndex, Result&: ValueName)) |
2747 | return error(Message: "Invalid record" ); |
2748 | unsigned ValueID = Record[0]; |
2749 | if (ValueID >= ValueList.size() || !ValueList[ValueID]) |
2750 | return error(Message: "Invalid record" ); |
2751 | Value *V = ValueList[ValueID]; |
2752 | |
2753 | StringRef NameStr(ValueName.data(), ValueName.size()); |
2754 | if (NameStr.contains(C: 0)) |
2755 | return error(Message: "Invalid value name" ); |
2756 | V->setName(NameStr); |
2757 | auto *GO = dyn_cast<GlobalObject>(Val: V); |
2758 | if (GO && ImplicitComdatObjects.contains(V: GO) && TT.supportsCOMDAT()) |
2759 | GO->setComdat(TheModule->getOrInsertComdat(Name: V->getName())); |
2760 | return V; |
2761 | } |
2762 | |
2763 | /// Helper to note and return the current location, and jump to the given |
2764 | /// offset. |
2765 | static Expected<uint64_t> jumpToValueSymbolTable(uint64_t Offset, |
2766 | BitstreamCursor &Stream) { |
2767 | // Save the current parsing location so we can jump back at the end |
2768 | // of the VST read. |
2769 | uint64_t CurrentBit = Stream.GetCurrentBitNo(); |
2770 | if (Error JumpFailed = Stream.JumpToBit(BitNo: Offset * 32)) |
2771 | return std::move(JumpFailed); |
2772 | Expected<BitstreamEntry> MaybeEntry = Stream.advance(); |
2773 | if (!MaybeEntry) |
2774 | return MaybeEntry.takeError(); |
2775 | if (MaybeEntry.get().Kind != BitstreamEntry::SubBlock || |
2776 | MaybeEntry.get().ID != bitc::VALUE_SYMTAB_BLOCK_ID) |
2777 | return error(Message: "Expected value symbol table subblock" ); |
2778 | return CurrentBit; |
2779 | } |
2780 | |
2781 | void BitcodeReader::setDeferredFunctionInfo(unsigned FuncBitcodeOffsetDelta, |
2782 | Function *F, |
2783 | ArrayRef<uint64_t> Record) { |
2784 | // Note that we subtract 1 here because the offset is relative to one word |
2785 | // before the start of the identification or module block, which was |
2786 | // historically always the start of the regular bitcode header. |
2787 | uint64_t FuncWordOffset = Record[1] - 1; |
2788 | uint64_t FuncBitOffset = FuncWordOffset * 32; |
2789 | DeferredFunctionInfo[F] = FuncBitOffset + FuncBitcodeOffsetDelta; |
2790 | // Set the LastFunctionBlockBit to point to the last function block. |
2791 | // Later when parsing is resumed after function materialization, |
2792 | // we can simply skip that last function block. |
2793 | if (FuncBitOffset > LastFunctionBlockBit) |
2794 | LastFunctionBlockBit = FuncBitOffset; |
2795 | } |
2796 | |
2797 | /// Read a new-style GlobalValue symbol table. |
2798 | Error BitcodeReader::parseGlobalValueSymbolTable() { |
2799 | unsigned FuncBitcodeOffsetDelta = |
2800 | Stream.getAbbrevIDWidth() + bitc::BlockIDWidth; |
2801 | |
2802 | if (Error Err = Stream.EnterSubBlock(BlockID: bitc::VALUE_SYMTAB_BLOCK_ID)) |
2803 | return Err; |
2804 | |
2805 | SmallVector<uint64_t, 64> Record; |
2806 | while (true) { |
2807 | Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks(); |
2808 | if (!MaybeEntry) |
2809 | return MaybeEntry.takeError(); |
2810 | BitstreamEntry Entry = MaybeEntry.get(); |
2811 | |
2812 | switch (Entry.Kind) { |
2813 | case BitstreamEntry::SubBlock: |
2814 | case BitstreamEntry::Error: |
2815 | return error(Message: "Malformed block" ); |
2816 | case BitstreamEntry::EndBlock: |
2817 | return Error::success(); |
2818 | case BitstreamEntry::Record: |
2819 | break; |
2820 | } |
2821 | |
2822 | Record.clear(); |
2823 | Expected<unsigned> MaybeRecord = Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record); |
2824 | if (!MaybeRecord) |
2825 | return MaybeRecord.takeError(); |
2826 | switch (MaybeRecord.get()) { |
2827 | case bitc::VST_CODE_FNENTRY: { // [valueid, offset] |
2828 | unsigned ValueID = Record[0]; |
2829 | if (ValueID >= ValueList.size() || !ValueList[ValueID]) |
2830 | return error(Message: "Invalid value reference in symbol table" ); |
2831 | setDeferredFunctionInfo(FuncBitcodeOffsetDelta, |
2832 | F: cast<Function>(Val: ValueList[ValueID]), Record); |
2833 | break; |
2834 | } |
2835 | } |
2836 | } |
2837 | } |
2838 | |
2839 | /// Parse the value symbol table at either the current parsing location or |
2840 | /// at the given bit offset if provided. |
2841 | Error BitcodeReader::parseValueSymbolTable(uint64_t Offset) { |
2842 | uint64_t CurrentBit; |
2843 | // Pass in the Offset to distinguish between calling for the module-level |
2844 | // VST (where we want to jump to the VST offset) and the function-level |
2845 | // VST (where we don't). |
2846 | if (Offset > 0) { |
2847 | Expected<uint64_t> MaybeCurrentBit = jumpToValueSymbolTable(Offset, Stream); |
2848 | if (!MaybeCurrentBit) |
2849 | return MaybeCurrentBit.takeError(); |
2850 | CurrentBit = MaybeCurrentBit.get(); |
2851 | // If this module uses a string table, read this as a module-level VST. |
2852 | if (UseStrtab) { |
2853 | if (Error Err = parseGlobalValueSymbolTable()) |
2854 | return Err; |
2855 | if (Error JumpFailed = Stream.JumpToBit(BitNo: CurrentBit)) |
2856 | return JumpFailed; |
2857 | return Error::success(); |
2858 | } |
2859 | // Otherwise, the VST will be in a similar format to a function-level VST, |
2860 | // and will contain symbol names. |
2861 | } |
2862 | |
2863 | // Compute the delta between the bitcode indices in the VST (the word offset |
2864 | // to the word-aligned ENTER_SUBBLOCK for the function block, and that |
2865 | // expected by the lazy reader. The reader's EnterSubBlock expects to have |
2866 | // already read the ENTER_SUBBLOCK code (size getAbbrevIDWidth) and BlockID |
2867 | // (size BlockIDWidth). Note that we access the stream's AbbrevID width here |
2868 | // just before entering the VST subblock because: 1) the EnterSubBlock |
2869 | // changes the AbbrevID width; 2) the VST block is nested within the same |
2870 | // outer MODULE_BLOCK as the FUNCTION_BLOCKs and therefore have the same |
2871 | // AbbrevID width before calling EnterSubBlock; and 3) when we want to |
2872 | // jump to the FUNCTION_BLOCK using this offset later, we don't want |
2873 | // to rely on the stream's AbbrevID width being that of the MODULE_BLOCK. |
2874 | unsigned FuncBitcodeOffsetDelta = |
2875 | Stream.getAbbrevIDWidth() + bitc::BlockIDWidth; |
2876 | |
2877 | if (Error Err = Stream.EnterSubBlock(BlockID: bitc::VALUE_SYMTAB_BLOCK_ID)) |
2878 | return Err; |
2879 | |
2880 | SmallVector<uint64_t, 64> Record; |
2881 | |
2882 | Triple TT(TheModule->getTargetTriple()); |
2883 | |
2884 | // Read all the records for this value table. |
2885 | SmallString<128> ValueName; |
2886 | |
2887 | while (true) { |
2888 | Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks(); |
2889 | if (!MaybeEntry) |
2890 | return MaybeEntry.takeError(); |
2891 | BitstreamEntry Entry = MaybeEntry.get(); |
2892 | |
2893 | switch (Entry.Kind) { |
2894 | case BitstreamEntry::SubBlock: // Handled for us already. |
2895 | case BitstreamEntry::Error: |
2896 | return error(Message: "Malformed block" ); |
2897 | case BitstreamEntry::EndBlock: |
2898 | if (Offset > 0) |
2899 | if (Error JumpFailed = Stream.JumpToBit(BitNo: CurrentBit)) |
2900 | return JumpFailed; |
2901 | return Error::success(); |
2902 | case BitstreamEntry::Record: |
2903 | // The interesting case. |
2904 | break; |
2905 | } |
2906 | |
2907 | // Read a record. |
2908 | Record.clear(); |
2909 | Expected<unsigned> MaybeRecord = Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record); |
2910 | if (!MaybeRecord) |
2911 | return MaybeRecord.takeError(); |
2912 | switch (MaybeRecord.get()) { |
2913 | default: // Default behavior: unknown type. |
2914 | break; |
2915 | case bitc::VST_CODE_ENTRY: { // VST_CODE_ENTRY: [valueid, namechar x N] |
2916 | Expected<Value *> ValOrErr = recordValue(Record, NameIndex: 1, TT); |
2917 | if (Error Err = ValOrErr.takeError()) |
2918 | return Err; |
2919 | ValOrErr.get(); |
2920 | break; |
2921 | } |
2922 | case bitc::VST_CODE_FNENTRY: { |
2923 | // VST_CODE_FNENTRY: [valueid, offset, namechar x N] |
2924 | Expected<Value *> ValOrErr = recordValue(Record, NameIndex: 2, TT); |
2925 | if (Error Err = ValOrErr.takeError()) |
2926 | return Err; |
2927 | Value *V = ValOrErr.get(); |
2928 | |
2929 | // Ignore function offsets emitted for aliases of functions in older |
2930 | // versions of LLVM. |
2931 | if (auto *F = dyn_cast<Function>(Val: V)) |
2932 | setDeferredFunctionInfo(FuncBitcodeOffsetDelta, F, Record); |
2933 | break; |
2934 | } |
2935 | case bitc::VST_CODE_BBENTRY: { |
2936 | if (convertToString(Record, Idx: 1, Result&: ValueName)) |
2937 | return error(Message: "Invalid bbentry record" ); |
2938 | BasicBlock *BB = getBasicBlock(ID: Record[0]); |
2939 | if (!BB) |
2940 | return error(Message: "Invalid bbentry record" ); |
2941 | |
2942 | BB->setName(StringRef(ValueName.data(), ValueName.size())); |
2943 | ValueName.clear(); |
2944 | break; |
2945 | } |
2946 | } |
2947 | } |
2948 | } |
2949 | |
2950 | /// Decode a signed value stored with the sign bit in the LSB for dense VBR |
2951 | /// encoding. |
2952 | uint64_t BitcodeReader::decodeSignRotatedValue(uint64_t V) { |
2953 | if ((V & 1) == 0) |
2954 | return V >> 1; |
2955 | if (V != 1) |
2956 | return -(V >> 1); |
2957 | // There is no such thing as -0 with integers. "-0" really means MININT. |
2958 | return 1ULL << 63; |
2959 | } |
2960 | |
2961 | /// Resolve all of the initializers for global values and aliases that we can. |
2962 | Error BitcodeReader::resolveGlobalAndIndirectSymbolInits() { |
2963 | std::vector<std::pair<GlobalVariable *, unsigned>> GlobalInitWorklist; |
2964 | std::vector<std::pair<GlobalValue *, unsigned>> IndirectSymbolInitWorklist; |
2965 | std::vector<FunctionOperandInfo> FunctionOperandWorklist; |
2966 | |
2967 | GlobalInitWorklist.swap(x&: GlobalInits); |
2968 | IndirectSymbolInitWorklist.swap(x&: IndirectSymbolInits); |
2969 | FunctionOperandWorklist.swap(x&: FunctionOperands); |
2970 | |
2971 | while (!GlobalInitWorklist.empty()) { |
2972 | unsigned ValID = GlobalInitWorklist.back().second; |
2973 | if (ValID >= ValueList.size()) { |
2974 | // Not ready to resolve this yet, it requires something later in the file. |
2975 | GlobalInits.push_back(x: GlobalInitWorklist.back()); |
2976 | } else { |
2977 | Expected<Constant *> MaybeC = getValueForInitializer(ID: ValID); |
2978 | if (!MaybeC) |
2979 | return MaybeC.takeError(); |
2980 | GlobalInitWorklist.back().first->setInitializer(MaybeC.get()); |
2981 | } |
2982 | GlobalInitWorklist.pop_back(); |
2983 | } |
2984 | |
2985 | while (!IndirectSymbolInitWorklist.empty()) { |
2986 | unsigned ValID = IndirectSymbolInitWorklist.back().second; |
2987 | if (ValID >= ValueList.size()) { |
2988 | IndirectSymbolInits.push_back(x: IndirectSymbolInitWorklist.back()); |
2989 | } else { |
2990 | Expected<Constant *> MaybeC = getValueForInitializer(ID: ValID); |
2991 | if (!MaybeC) |
2992 | return MaybeC.takeError(); |
2993 | Constant *C = MaybeC.get(); |
2994 | GlobalValue *GV = IndirectSymbolInitWorklist.back().first; |
2995 | if (auto *GA = dyn_cast<GlobalAlias>(Val: GV)) { |
2996 | if (C->getType() != GV->getType()) |
2997 | return error(Message: "Alias and aliasee types don't match" ); |
2998 | GA->setAliasee(C); |
2999 | } else if (auto *GI = dyn_cast<GlobalIFunc>(Val: GV)) { |
3000 | GI->setResolver(C); |
3001 | } else { |
3002 | return error(Message: "Expected an alias or an ifunc" ); |
3003 | } |
3004 | } |
3005 | IndirectSymbolInitWorklist.pop_back(); |
3006 | } |
3007 | |
3008 | while (!FunctionOperandWorklist.empty()) { |
3009 | FunctionOperandInfo &Info = FunctionOperandWorklist.back(); |
3010 | if (Info.PersonalityFn) { |
3011 | unsigned ValID = Info.PersonalityFn - 1; |
3012 | if (ValID < ValueList.size()) { |
3013 | Expected<Constant *> MaybeC = getValueForInitializer(ID: ValID); |
3014 | if (!MaybeC) |
3015 | return MaybeC.takeError(); |
3016 | Info.F->setPersonalityFn(MaybeC.get()); |
3017 | Info.PersonalityFn = 0; |
3018 | } |
3019 | } |
3020 | if (Info.Prefix) { |
3021 | unsigned ValID = Info.Prefix - 1; |
3022 | if (ValID < ValueList.size()) { |
3023 | Expected<Constant *> MaybeC = getValueForInitializer(ID: ValID); |
3024 | if (!MaybeC) |
3025 | return MaybeC.takeError(); |
3026 | Info.F->setPrefixData(MaybeC.get()); |
3027 | Info.Prefix = 0; |
3028 | } |
3029 | } |
3030 | if (Info.Prologue) { |
3031 | unsigned ValID = Info.Prologue - 1; |
3032 | if (ValID < ValueList.size()) { |
3033 | Expected<Constant *> MaybeC = getValueForInitializer(ID: ValID); |
3034 | if (!MaybeC) |
3035 | return MaybeC.takeError(); |
3036 | Info.F->setPrologueData(MaybeC.get()); |
3037 | Info.Prologue = 0; |
3038 | } |
3039 | } |
3040 | if (Info.PersonalityFn || Info.Prefix || Info.Prologue) |
3041 | FunctionOperands.push_back(x: Info); |
3042 | FunctionOperandWorklist.pop_back(); |
3043 | } |
3044 | |
3045 | return Error::success(); |
3046 | } |
3047 | |
3048 | APInt llvm::readWideAPInt(ArrayRef<uint64_t> Vals, unsigned TypeBits) { |
3049 | SmallVector<uint64_t, 8> Words(Vals.size()); |
3050 | transform(Range&: Vals, d_first: Words.begin(), |
3051 | F: BitcodeReader::decodeSignRotatedValue); |
3052 | |
3053 | return APInt(TypeBits, Words); |
3054 | } |
3055 | |
3056 | Error BitcodeReader::parseConstants() { |
3057 | if (Error Err = Stream.EnterSubBlock(BlockID: bitc::CONSTANTS_BLOCK_ID)) |
3058 | return Err; |
3059 | |
3060 | SmallVector<uint64_t, 64> Record; |
3061 | |
3062 | // Read all the records for this value table. |
3063 | Type *CurTy = Type::getInt32Ty(C&: Context); |
3064 | unsigned Int32TyID = getVirtualTypeID(Ty: CurTy); |
3065 | unsigned CurTyID = Int32TyID; |
3066 | Type *CurElemTy = nullptr; |
3067 | unsigned NextCstNo = ValueList.size(); |
3068 | |
3069 | while (true) { |
3070 | Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks(); |
3071 | if (!MaybeEntry) |
3072 | return MaybeEntry.takeError(); |
3073 | BitstreamEntry Entry = MaybeEntry.get(); |
3074 | |
3075 | switch (Entry.Kind) { |
3076 | case BitstreamEntry::SubBlock: // Handled for us already. |
3077 | case BitstreamEntry::Error: |
3078 | return error(Message: "Malformed block" ); |
3079 | case BitstreamEntry::EndBlock: |
3080 | if (NextCstNo != ValueList.size()) |
3081 | return error(Message: "Invalid constant reference" ); |
3082 | return Error::success(); |
3083 | case BitstreamEntry::Record: |
3084 | // The interesting case. |
3085 | break; |
3086 | } |
3087 | |
3088 | // Read a record. |
3089 | Record.clear(); |
3090 | Type *VoidType = Type::getVoidTy(C&: Context); |
3091 | Value *V = nullptr; |
3092 | Expected<unsigned> MaybeBitCode = Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record); |
3093 | if (!MaybeBitCode) |
3094 | return MaybeBitCode.takeError(); |
3095 | switch (unsigned BitCode = MaybeBitCode.get()) { |
3096 | default: // Default behavior: unknown constant |
3097 | case bitc::CST_CODE_UNDEF: // UNDEF |
3098 | V = UndefValue::get(T: CurTy); |
3099 | break; |
3100 | case bitc::CST_CODE_POISON: // POISON |
3101 | V = PoisonValue::get(T: CurTy); |
3102 | break; |
3103 | case bitc::CST_CODE_SETTYPE: // SETTYPE: [typeid] |
3104 | if (Record.empty()) |
3105 | return error(Message: "Invalid settype record" ); |
3106 | if (Record[0] >= TypeList.size() || !TypeList[Record[0]]) |
3107 | return error(Message: "Invalid settype record" ); |
3108 | if (TypeList[Record[0]] == VoidType) |
3109 | return error(Message: "Invalid constant type" ); |
3110 | CurTyID = Record[0]; |
3111 | CurTy = TypeList[CurTyID]; |
3112 | CurElemTy = getPtrElementTypeByID(ID: CurTyID); |
3113 | continue; // Skip the ValueList manipulation. |
3114 | case bitc::CST_CODE_NULL: // NULL |
3115 | if (CurTy->isVoidTy() || CurTy->isFunctionTy() || CurTy->isLabelTy()) |
3116 | return error(Message: "Invalid type for a constant null value" ); |
3117 | if (auto *TETy = dyn_cast<TargetExtType>(Val: CurTy)) |
3118 | if (!TETy->hasProperty(Prop: TargetExtType::HasZeroInit)) |
3119 | return error(Message: "Invalid type for a constant null value" ); |
3120 | V = Constant::getNullValue(Ty: CurTy); |
3121 | break; |
3122 | case bitc::CST_CODE_INTEGER: // INTEGER: [intval] |
3123 | if (!CurTy->isIntOrIntVectorTy() || Record.empty()) |
3124 | return error(Message: "Invalid integer const record" ); |
3125 | V = ConstantInt::get(Ty: CurTy, V: decodeSignRotatedValue(V: Record[0])); |
3126 | break; |
3127 | case bitc::CST_CODE_WIDE_INTEGER: {// WIDE_INTEGER: [n x intval] |
3128 | if (!CurTy->isIntOrIntVectorTy() || Record.empty()) |
3129 | return error(Message: "Invalid wide integer const record" ); |
3130 | |
3131 | auto *ScalarTy = cast<IntegerType>(Val: CurTy->getScalarType()); |
3132 | APInt VInt = readWideAPInt(Vals: Record, TypeBits: ScalarTy->getBitWidth()); |
3133 | V = ConstantInt::get(Ty: CurTy, V: VInt); |
3134 | break; |
3135 | } |
3136 | case bitc::CST_CODE_FLOAT: { // FLOAT: [fpval] |
3137 | if (Record.empty()) |
3138 | return error(Message: "Invalid float const record" ); |
3139 | |
3140 | auto *ScalarTy = CurTy->getScalarType(); |
3141 | if (ScalarTy->isHalfTy()) |
3142 | V = ConstantFP::get(Ty: CurTy, V: APFloat(APFloat::IEEEhalf(), |
3143 | APInt(16, (uint16_t)Record[0]))); |
3144 | else if (ScalarTy->isBFloatTy()) |
3145 | V = ConstantFP::get( |
3146 | Ty: CurTy, V: APFloat(APFloat::BFloat(), APInt(16, (uint32_t)Record[0]))); |
3147 | else if (ScalarTy->isFloatTy()) |
3148 | V = ConstantFP::get(Ty: CurTy, V: APFloat(APFloat::IEEEsingle(), |
3149 | APInt(32, (uint32_t)Record[0]))); |
3150 | else if (ScalarTy->isDoubleTy()) |
3151 | V = ConstantFP::get( |
3152 | Ty: CurTy, V: APFloat(APFloat::IEEEdouble(), APInt(64, Record[0]))); |
3153 | else if (ScalarTy->isX86_FP80Ty()) { |
3154 | // Bits are not stored the same way as a normal i80 APInt, compensate. |
3155 | uint64_t Rearrange[2]; |
3156 | Rearrange[0] = (Record[1] & 0xffffLL) | (Record[0] << 16); |
3157 | Rearrange[1] = Record[0] >> 48; |
3158 | V = ConstantFP::get( |
3159 | Ty: CurTy, V: APFloat(APFloat::x87DoubleExtended(), APInt(80, Rearrange))); |
3160 | } else if (ScalarTy->isFP128Ty()) |
3161 | V = ConstantFP::get(Ty: CurTy, |
3162 | V: APFloat(APFloat::IEEEquad(), APInt(128, Record))); |
3163 | else if (ScalarTy->isPPC_FP128Ty()) |
3164 | V = ConstantFP::get( |
3165 | Ty: CurTy, V: APFloat(APFloat::PPCDoubleDouble(), APInt(128, Record))); |
3166 | else |
3167 | V = UndefValue::get(T: CurTy); |
3168 | break; |
3169 | } |
3170 | |
3171 | case bitc::CST_CODE_AGGREGATE: {// AGGREGATE: [n x value number] |
3172 | if (Record.empty()) |
3173 | return error(Message: "Invalid aggregate record" ); |
3174 | |
3175 | unsigned Size = Record.size(); |
3176 | SmallVector<unsigned, 16> Elts; |
3177 | for (unsigned i = 0; i != Size; ++i) |
3178 | Elts.push_back(Elt: Record[i]); |
3179 | |
3180 | if (isa<StructType>(Val: CurTy)) { |
3181 | V = BitcodeConstant::create( |
3182 | A&: Alloc, Ty: CurTy, Info: BitcodeConstant::ConstantStructOpcode, OpIDs: Elts); |
3183 | } else if (isa<ArrayType>(Val: CurTy)) { |
3184 | V = BitcodeConstant::create(A&: Alloc, Ty: CurTy, |
3185 | Info: BitcodeConstant::ConstantArrayOpcode, OpIDs: Elts); |
3186 | } else if (isa<VectorType>(Val: CurTy)) { |
3187 | V = BitcodeConstant::create( |
3188 | A&: Alloc, Ty: CurTy, Info: BitcodeConstant::ConstantVectorOpcode, OpIDs: Elts); |
3189 | } else { |
3190 | V = UndefValue::get(T: CurTy); |
3191 | } |
3192 | break; |
3193 | } |
3194 | case bitc::CST_CODE_STRING: // STRING: [values] |
3195 | case bitc::CST_CODE_CSTRING: { // CSTRING: [values] |
3196 | if (Record.empty()) |
3197 | return error(Message: "Invalid string record" ); |
3198 | |
3199 | SmallString<16> Elts(Record.begin(), Record.end()); |
3200 | V = ConstantDataArray::getString(Context, Initializer: Elts, |
3201 | AddNull: BitCode == bitc::CST_CODE_CSTRING); |
3202 | break; |
3203 | } |
3204 | case bitc::CST_CODE_DATA: {// DATA: [n x value] |
3205 | if (Record.empty()) |
3206 | return error(Message: "Invalid data record" ); |
3207 | |
3208 | Type *EltTy; |
3209 | if (auto *Array = dyn_cast<ArrayType>(Val: CurTy)) |
3210 | EltTy = Array->getElementType(); |
3211 | else |
3212 | EltTy = cast<VectorType>(Val: CurTy)->getElementType(); |
3213 | if (EltTy->isIntegerTy(Bitwidth: 8)) { |
3214 | SmallVector<uint8_t, 16> Elts(Record.begin(), Record.end()); |
3215 | if (isa<VectorType>(Val: CurTy)) |
3216 | V = ConstantDataVector::get(Context, Elts); |
3217 | else |
3218 | V = ConstantDataArray::get(Context, Elts); |
3219 | } else if (EltTy->isIntegerTy(Bitwidth: 16)) { |
3220 | SmallVector<uint16_t, 16> Elts(Record.begin(), Record.end()); |
3221 | if (isa<VectorType>(Val: CurTy)) |
3222 | V = ConstantDataVector::get(Context, Elts); |
3223 | else |
3224 | V = ConstantDataArray::get(Context, Elts); |
3225 | } else if (EltTy->isIntegerTy(Bitwidth: 32)) { |
3226 | SmallVector<uint32_t, 16> Elts(Record.begin(), Record.end()); |
3227 | if (isa<VectorType>(Val: CurTy)) |
3228 | V = ConstantDataVector::get(Context, Elts); |
3229 | else |
3230 | V = ConstantDataArray::get(Context, Elts); |
3231 | } else if (EltTy->isIntegerTy(Bitwidth: 64)) { |
3232 | SmallVector<uint64_t, 16> Elts(Record.begin(), Record.end()); |
3233 | if (isa<VectorType>(Val: CurTy)) |
3234 | V = ConstantDataVector::get(Context, Elts); |
3235 | else |
3236 | V = ConstantDataArray::get(Context, Elts); |
3237 | } else if (EltTy->isHalfTy()) { |
3238 | SmallVector<uint16_t, 16> Elts(Record.begin(), Record.end()); |
3239 | if (isa<VectorType>(Val: CurTy)) |
3240 | V = ConstantDataVector::getFP(ElementType: EltTy, Elts); |
3241 | else |
3242 | V = ConstantDataArray::getFP(ElementType: EltTy, Elts); |
3243 | } else if (EltTy->isBFloatTy()) { |
3244 | SmallVector<uint16_t, 16> Elts(Record.begin(), Record.end()); |
3245 | if (isa<VectorType>(Val: CurTy)) |
3246 | V = ConstantDataVector::getFP(ElementType: EltTy, Elts); |
3247 | else |
3248 | V = ConstantDataArray::getFP(ElementType: EltTy, Elts); |
3249 | } else if (EltTy->isFloatTy()) { |
3250 | SmallVector<uint32_t, 16> Elts(Record.begin(), Record.end()); |
3251 | if (isa<VectorType>(Val: CurTy)) |
3252 | V = ConstantDataVector::getFP(ElementType: EltTy, Elts); |
3253 | else |
3254 | V = ConstantDataArray::getFP(ElementType: EltTy, Elts); |
3255 | } else if (EltTy->isDoubleTy()) { |
3256 | SmallVector<uint64_t, 16> Elts(Record.begin(), Record.end()); |
3257 | if (isa<VectorType>(Val: CurTy)) |
3258 | V = ConstantDataVector::getFP(ElementType: EltTy, Elts); |
3259 | else |
3260 | V = ConstantDataArray::getFP(ElementType: EltTy, Elts); |
3261 | } else { |
3262 | return error(Message: "Invalid type for value" ); |
3263 | } |
3264 | break; |
3265 | } |
3266 | case bitc::CST_CODE_CE_UNOP: { // CE_UNOP: [opcode, opval] |
3267 | if (Record.size() < 2) |
3268 | return error(Message: "Invalid unary op constexpr record" ); |
3269 | int Opc = getDecodedUnaryOpcode(Val: Record[0], Ty: CurTy); |
3270 | if (Opc < 0) { |
3271 | V = UndefValue::get(T: CurTy); // Unknown unop. |
3272 | } else { |
3273 | V = BitcodeConstant::create(A&: Alloc, Ty: CurTy, Info: Opc, OpIDs: (unsigned)Record[1]); |
3274 | } |
3275 | break; |
3276 | } |
3277 | case bitc::CST_CODE_CE_BINOP: { // CE_BINOP: [opcode, opval, opval] |
3278 | if (Record.size() < 3) |
3279 | return error(Message: "Invalid binary op constexpr record" ); |
3280 | int Opc = getDecodedBinaryOpcode(Val: Record[0], Ty: CurTy); |
3281 | if (Opc < 0) { |
3282 | V = UndefValue::get(T: CurTy); // Unknown binop. |
3283 | } else { |
3284 | uint8_t Flags = 0; |
3285 | if (Record.size() >= 4) { |
3286 | if (Opc == Instruction::Add || |
3287 | Opc == Instruction::Sub || |
3288 | Opc == Instruction::Mul || |
3289 | Opc == Instruction::Shl) { |
3290 | if (Record[3] & (1 << bitc::OBO_NO_SIGNED_WRAP)) |
3291 | Flags |= OverflowingBinaryOperator::NoSignedWrap; |
3292 | if (Record[3] & (1 << bitc::OBO_NO_UNSIGNED_WRAP)) |
3293 | Flags |= OverflowingBinaryOperator::NoUnsignedWrap; |
3294 | } else if (Opc == Instruction::SDiv || |
3295 | Opc == Instruction::UDiv || |
3296 | Opc == Instruction::LShr || |
3297 | Opc == Instruction::AShr) { |
3298 | if (Record[3] & (1 << bitc::PEO_EXACT)) |
3299 | Flags |= PossiblyExactOperator::IsExact; |
3300 | } |
3301 | } |
3302 | V = BitcodeConstant::create(A&: Alloc, Ty: CurTy, Info: {(uint8_t)Opc, Flags}, |
3303 | OpIDs: {(unsigned)Record[1], (unsigned)Record[2]}); |
3304 | } |
3305 | break; |
3306 | } |
3307 | case bitc::CST_CODE_CE_CAST: { // CE_CAST: [opcode, opty, opval] |
3308 | if (Record.size() < 3) |
3309 | return error(Message: "Invalid cast constexpr record" ); |
3310 | int Opc = getDecodedCastOpcode(Val: Record[0]); |
3311 | if (Opc < 0) { |
3312 | V = UndefValue::get(T: CurTy); // Unknown cast. |
3313 | } else { |
3314 | unsigned OpTyID = Record[1]; |
3315 | Type *OpTy = getTypeByID(ID: OpTyID); |
3316 | if (!OpTy) |
3317 | return error(Message: "Invalid cast constexpr record" ); |
3318 | V = BitcodeConstant::create(A&: Alloc, Ty: CurTy, Info: Opc, OpIDs: (unsigned)Record[2]); |
3319 | } |
3320 | break; |
3321 | } |
3322 | case bitc::CST_CODE_CE_INBOUNDS_GEP: // [ty, n x operands] |
3323 | case bitc::CST_CODE_CE_GEP: // [ty, n x operands] |
3324 | case bitc::CST_CODE_CE_GEP_WITH_INRANGE_INDEX_OLD: // [ty, flags, n x |
3325 | // operands] |
3326 | case bitc::CST_CODE_CE_GEP_WITH_INRANGE: { // [ty, flags, start, end, n x |
3327 | // operands] |
3328 | if (Record.size() < 2) |
3329 | return error(Message: "Constant GEP record must have at least two elements" ); |
3330 | unsigned OpNum = 0; |
3331 | Type *PointeeType = nullptr; |
3332 | if (BitCode == bitc::CST_CODE_CE_GEP_WITH_INRANGE_INDEX_OLD || |
3333 | BitCode == bitc::CST_CODE_CE_GEP_WITH_INRANGE || Record.size() % 2) |
3334 | PointeeType = getTypeByID(ID: Record[OpNum++]); |
3335 | |
3336 | bool InBounds = false; |
3337 | std::optional<ConstantRange> InRange; |
3338 | if (BitCode == bitc::CST_CODE_CE_GEP_WITH_INRANGE_INDEX_OLD) { |
3339 | uint64_t Op = Record[OpNum++]; |
3340 | InBounds = Op & 1; |
3341 | unsigned InRangeIndex = Op >> 1; |
3342 | // "Upgrade" inrange by dropping it. The feature is too niche to |
3343 | // bother. |
3344 | (void)InRangeIndex; |
3345 | } else if (BitCode == bitc::CST_CODE_CE_GEP_WITH_INRANGE) { |
3346 | uint64_t Op = Record[OpNum++]; |
3347 | InBounds = Op & 1; |
3348 | Expected<ConstantRange> MaybeInRange = readConstantRange(Record, OpNum); |
3349 | if (!MaybeInRange) |
3350 | return MaybeInRange.takeError(); |
3351 | InRange = MaybeInRange.get(); |
3352 | } else if (BitCode == bitc::CST_CODE_CE_INBOUNDS_GEP) |
3353 | InBounds = true; |
3354 | |
3355 | SmallVector<unsigned, 16> Elts; |
3356 | unsigned BaseTypeID = Record[OpNum]; |
3357 | while (OpNum != Record.size()) { |
3358 | unsigned ElTyID = Record[OpNum++]; |
3359 | Type *ElTy = getTypeByID(ID: ElTyID); |
3360 | if (!ElTy) |
3361 | return error(Message: "Invalid getelementptr constexpr record" ); |
3362 | Elts.push_back(Elt: Record[OpNum++]); |
3363 | } |
3364 | |
3365 | if (Elts.size() < 1) |
3366 | return error(Message: "Invalid gep with no operands" ); |
3367 | |
3368 | Type *BaseType = getTypeByID(ID: BaseTypeID); |
3369 | if (isa<VectorType>(Val: BaseType)) { |
3370 | BaseTypeID = getContainedTypeID(ID: BaseTypeID, Idx: 0); |
3371 | BaseType = getTypeByID(ID: BaseTypeID); |
3372 | } |
3373 | |
3374 | PointerType *OrigPtrTy = dyn_cast_or_null<PointerType>(Val: BaseType); |
3375 | if (!OrigPtrTy) |
3376 | return error(Message: "GEP base operand must be pointer or vector of pointer" ); |
3377 | |
3378 | if (!PointeeType) { |
3379 | PointeeType = getPtrElementTypeByID(ID: BaseTypeID); |
3380 | if (!PointeeType) |
3381 | return error(Message: "Missing element type for old-style constant GEP" ); |
3382 | } |
3383 | |
3384 | V = BitcodeConstant::create( |
3385 | A&: Alloc, Ty: CurTy, |
3386 | Info: {Instruction::GetElementPtr, InBounds, PointeeType, InRange}, OpIDs: Elts); |
3387 | break; |
3388 | } |
3389 | case bitc::CST_CODE_CE_SELECT: { // CE_SELECT: [opval#, opval#, opval#] |
3390 | if (Record.size() < 3) |
3391 | return error(Message: "Invalid select constexpr record" ); |
3392 | |
3393 | V = BitcodeConstant::create( |
3394 | A&: Alloc, Ty: CurTy, Info: Instruction::Select, |
3395 | OpIDs: {(unsigned)Record[0], (unsigned)Record[1], (unsigned)Record[2]}); |
3396 | break; |
3397 | } |
3398 | case bitc::CST_CODE_CE_EXTRACTELT |
3399 | : { // CE_EXTRACTELT: [opty, opval, opty, opval] |
3400 | if (Record.size() < 3) |
3401 | return error(Message: "Invalid extractelement constexpr record" ); |
3402 | unsigned OpTyID = Record[0]; |
3403 | VectorType *OpTy = |
3404 | dyn_cast_or_null<VectorType>(Val: getTypeByID(ID: OpTyID)); |
3405 | if (!OpTy) |
3406 | return error(Message: "Invalid extractelement constexpr record" ); |
3407 | unsigned IdxRecord; |
3408 | if (Record.size() == 4) { |
3409 | unsigned IdxTyID = Record[2]; |
3410 | Type *IdxTy = getTypeByID(ID: IdxTyID); |
3411 | if (!IdxTy) |
3412 | return error(Message: "Invalid extractelement constexpr record" ); |
3413 | IdxRecord = Record[3]; |
3414 | } else { |
3415 | // Deprecated, but still needed to read old bitcode files. |
3416 | IdxRecord = Record[2]; |
3417 | } |
3418 | V = BitcodeConstant::create(A&: Alloc, Ty: CurTy, Info: Instruction::ExtractElement, |
3419 | OpIDs: {(unsigned)Record[1], IdxRecord}); |
3420 | break; |
3421 | } |
3422 | case bitc::CST_CODE_CE_INSERTELT |
3423 | : { // CE_INSERTELT: [opval, opval, opty, opval] |
3424 | VectorType *OpTy = dyn_cast<VectorType>(Val: CurTy); |
3425 | if (Record.size() < 3 || !OpTy) |
3426 | return error(Message: "Invalid insertelement constexpr record" ); |
3427 | unsigned IdxRecord; |
3428 | if (Record.size() == 4) { |
3429 | unsigned IdxTyID = Record[2]; |
3430 | Type *IdxTy = getTypeByID(ID: IdxTyID); |
3431 | if (!IdxTy) |
3432 | return error(Message: "Invalid insertelement constexpr record" ); |
3433 | IdxRecord = Record[3]; |
3434 | } else { |
3435 | // Deprecated, but still needed to read old bitcode files. |
3436 | IdxRecord = Record[2]; |
3437 | } |
3438 | V = BitcodeConstant::create( |
3439 | A&: Alloc, Ty: CurTy, Info: Instruction::InsertElement, |
3440 | OpIDs: {(unsigned)Record[0], (unsigned)Record[1], IdxRecord}); |
3441 | break; |
3442 | } |
3443 | case bitc::CST_CODE_CE_SHUFFLEVEC: { // CE_SHUFFLEVEC: [opval, opval, opval] |
3444 | VectorType *OpTy = dyn_cast<VectorType>(Val: CurTy); |
3445 | if (Record.size() < 3 || !OpTy) |
3446 | return error(Message: "Invalid shufflevector constexpr record" ); |
3447 | V = BitcodeConstant::create( |
3448 | A&: Alloc, Ty: CurTy, Info: Instruction::ShuffleVector, |
3449 | OpIDs: {(unsigned)Record[0], (unsigned)Record[1], (unsigned)Record[2]}); |
3450 | break; |
3451 | } |
3452 | case bitc::CST_CODE_CE_SHUFVEC_EX: { // [opty, opval, opval, opval] |
3453 | VectorType *RTy = dyn_cast<VectorType>(Val: CurTy); |
3454 | VectorType *OpTy = |
3455 | dyn_cast_or_null<VectorType>(Val: getTypeByID(ID: Record[0])); |
3456 | if (Record.size() < 4 || !RTy || !OpTy) |
3457 | return error(Message: "Invalid shufflevector constexpr record" ); |
3458 | V = BitcodeConstant::create( |
3459 | A&: Alloc, Ty: CurTy, Info: Instruction::ShuffleVector, |
3460 | OpIDs: {(unsigned)Record[1], (unsigned)Record[2], (unsigned)Record[3]}); |
3461 | break; |
3462 | } |
3463 | case bitc::CST_CODE_CE_CMP: { // CE_CMP: [opty, opval, opval, pred] |
3464 | if (Record.size() < 4) |
3465 | return error(Message: "Invalid cmp constexpt record" ); |
3466 | unsigned OpTyID = Record[0]; |
3467 | Type *OpTy = getTypeByID(ID: OpTyID); |
3468 | if (!OpTy) |
3469 | return error(Message: "Invalid cmp constexpr record" ); |
3470 | V = BitcodeConstant::create( |
3471 | A&: Alloc, Ty: CurTy, |
3472 | Info: {(uint8_t)(OpTy->isFPOrFPVectorTy() ? Instruction::FCmp |
3473 | : Instruction::ICmp), |
3474 | (uint8_t)Record[3]}, |
3475 | OpIDs: {(unsigned)Record[1], (unsigned)Record[2]}); |
3476 | break; |
3477 | } |
3478 | // This maintains backward compatibility, pre-asm dialect keywords. |
3479 | // Deprecated, but still needed to read old bitcode files. |
3480 | case bitc::CST_CODE_INLINEASM_OLD: { |
3481 | if (Record.size() < 2) |
3482 | return error(Message: "Invalid inlineasm record" ); |
3483 | std::string AsmStr, ConstrStr; |
3484 | bool HasSideEffects = Record[0] & 1; |
3485 | bool IsAlignStack = Record[0] >> 1; |
3486 | unsigned AsmStrSize = Record[1]; |
3487 | if (2+AsmStrSize >= Record.size()) |
3488 | return error(Message: "Invalid inlineasm record" ); |
3489 | unsigned ConstStrSize = Record[2+AsmStrSize]; |
3490 | if (3+AsmStrSize+ConstStrSize > Record.size()) |
3491 | return error(Message: "Invalid inlineasm record" ); |
3492 | |
3493 | for (unsigned i = 0; i != AsmStrSize; ++i) |
3494 | AsmStr += (char)Record[2+i]; |
3495 | for (unsigned i = 0; i != ConstStrSize; ++i) |
3496 | ConstrStr += (char)Record[3+AsmStrSize+i]; |
3497 | UpgradeInlineAsmString(AsmStr: &AsmStr); |
3498 | if (!CurElemTy) |
3499 | return error(Message: "Missing element type for old-style inlineasm" ); |
3500 | V = InlineAsm::get(Ty: cast<FunctionType>(Val: CurElemTy), AsmString: AsmStr, Constraints: ConstrStr, |
3501 | hasSideEffects: HasSideEffects, isAlignStack: IsAlignStack); |
3502 | break; |
3503 | } |
3504 | // This version adds support for the asm dialect keywords (e.g., |
3505 | // inteldialect). |
3506 | case bitc::CST_CODE_INLINEASM_OLD2: { |
3507 | if (Record.size() < 2) |
3508 | return error(Message: "Invalid inlineasm record" ); |
3509 | std::string AsmStr, ConstrStr; |
3510 | bool HasSideEffects = Record[0] & 1; |
3511 | bool IsAlignStack = (Record[0] >> 1) & 1; |
3512 | unsigned AsmDialect = Record[0] >> 2; |
3513 | unsigned AsmStrSize = Record[1]; |
3514 | if (2+AsmStrSize >= Record.size()) |
3515 | return error(Message: "Invalid inlineasm record" ); |
3516 | unsigned ConstStrSize = Record[2+AsmStrSize]; |
3517 | if (3+AsmStrSize+ConstStrSize > Record.size()) |
3518 | return error(Message: "Invalid inlineasm record" ); |
3519 | |
3520 | for (unsigned i = 0; i != AsmStrSize; ++i) |
3521 | AsmStr += (char)Record[2+i]; |
3522 | for (unsigned i = 0; i != ConstStrSize; ++i) |
3523 | ConstrStr += (char)Record[3+AsmStrSize+i]; |
3524 | UpgradeInlineAsmString(AsmStr: &AsmStr); |
3525 | if (!CurElemTy) |
3526 | return error(Message: "Missing element type for old-style inlineasm" ); |
3527 | V = InlineAsm::get(Ty: cast<FunctionType>(Val: CurElemTy), AsmString: AsmStr, Constraints: ConstrStr, |
3528 | hasSideEffects: HasSideEffects, isAlignStack: IsAlignStack, |
3529 | asmDialect: InlineAsm::AsmDialect(AsmDialect)); |
3530 | break; |
3531 | } |
3532 | // This version adds support for the unwind keyword. |
3533 | case bitc::CST_CODE_INLINEASM_OLD3: { |
3534 | if (Record.size() < 2) |
3535 | return error(Message: "Invalid inlineasm record" ); |
3536 | unsigned OpNum = 0; |
3537 | std::string AsmStr, ConstrStr; |
3538 | bool HasSideEffects = Record[OpNum] & 1; |
3539 | bool IsAlignStack = (Record[OpNum] >> 1) & 1; |
3540 | unsigned AsmDialect = (Record[OpNum] >> 2) & 1; |
3541 | bool CanThrow = (Record[OpNum] >> 3) & 1; |
3542 | ++OpNum; |
3543 | unsigned AsmStrSize = Record[OpNum]; |
3544 | ++OpNum; |
3545 | if (OpNum + AsmStrSize >= Record.size()) |
3546 | return error(Message: "Invalid inlineasm record" ); |
3547 | unsigned ConstStrSize = Record[OpNum + AsmStrSize]; |
3548 | if (OpNum + 1 + AsmStrSize + ConstStrSize > Record.size()) |
3549 | return error(Message: "Invalid inlineasm record" ); |
3550 | |
3551 | for (unsigned i = 0; i != AsmStrSize; ++i) |
3552 | AsmStr += (char)Record[OpNum + i]; |
3553 | ++OpNum; |
3554 | for (unsigned i = 0; i != ConstStrSize; ++i) |
3555 | ConstrStr += (char)Record[OpNum + AsmStrSize + i]; |
3556 | UpgradeInlineAsmString(AsmStr: &AsmStr); |
3557 | if (!CurElemTy) |
3558 | return error(Message: "Missing element type for old-style inlineasm" ); |
3559 | V = InlineAsm::get(Ty: cast<FunctionType>(Val: CurElemTy), AsmString: AsmStr, Constraints: ConstrStr, |
3560 | hasSideEffects: HasSideEffects, isAlignStack: IsAlignStack, |
3561 | asmDialect: InlineAsm::AsmDialect(AsmDialect), canThrow: CanThrow); |
3562 | break; |
3563 | } |
3564 | // This version adds explicit function type. |
3565 | case bitc::CST_CODE_INLINEASM: { |
3566 | if (Record.size() < 3) |
3567 | return error(Message: "Invalid inlineasm record" ); |
3568 | unsigned OpNum = 0; |
3569 | auto *FnTy = dyn_cast_or_null<FunctionType>(Val: getTypeByID(ID: Record[OpNum])); |
3570 | ++OpNum; |
3571 | if (!FnTy) |
3572 | return error(Message: "Invalid inlineasm record" ); |
3573 | std::string AsmStr, ConstrStr; |
3574 | bool HasSideEffects = Record[OpNum] & 1; |
3575 | bool IsAlignStack = (Record[OpNum] >> 1) & 1; |
3576 | unsigned AsmDialect = (Record[OpNum] >> 2) & 1; |
3577 | bool CanThrow = (Record[OpNum] >> 3) & 1; |
3578 | ++OpNum; |
3579 | unsigned AsmStrSize = Record[OpNum]; |
3580 | ++OpNum; |
3581 | if (OpNum + AsmStrSize >= Record.size()) |
3582 | return error(Message: "Invalid inlineasm record" ); |
3583 | unsigned ConstStrSize = Record[OpNum + AsmStrSize]; |
3584 | if (OpNum + 1 + AsmStrSize + ConstStrSize > Record.size()) |
3585 | return error(Message: "Invalid inlineasm record" ); |
3586 | |
3587 | for (unsigned i = 0; i != AsmStrSize; ++i) |
3588 | AsmStr += (char)Record[OpNum + i]; |
3589 | ++OpNum; |
3590 | for (unsigned i = 0; i != ConstStrSize; ++i) |
3591 | ConstrStr += (char)Record[OpNum + AsmStrSize + i]; |
3592 | UpgradeInlineAsmString(AsmStr: &AsmStr); |
3593 | V = InlineAsm::get(Ty: FnTy, AsmString: AsmStr, Constraints: ConstrStr, hasSideEffects: HasSideEffects, isAlignStack: IsAlignStack, |
3594 | asmDialect: InlineAsm::AsmDialect(AsmDialect), canThrow: CanThrow); |
3595 | break; |
3596 | } |
3597 | case bitc::CST_CODE_BLOCKADDRESS:{ |
3598 | if (Record.size() < 3) |
3599 | return error(Message: "Invalid blockaddress record" ); |
3600 | unsigned FnTyID = Record[0]; |
3601 | Type *FnTy = getTypeByID(ID: FnTyID); |
3602 | if (!FnTy) |
3603 | return error(Message: "Invalid blockaddress record" ); |
3604 | V = BitcodeConstant::create( |
3605 | A&: Alloc, Ty: CurTy, |
3606 | Info: {BitcodeConstant::BlockAddressOpcode, 0, (unsigned)Record[2]}, |
3607 | OpIDs: Record[1]); |
3608 | break; |
3609 | } |
3610 | case bitc::CST_CODE_DSO_LOCAL_EQUIVALENT: { |
3611 | if (Record.size() < 2) |
3612 | return error(Message: "Invalid dso_local record" ); |
3613 | unsigned GVTyID = Record[0]; |
3614 | Type *GVTy = getTypeByID(ID: GVTyID); |
3615 | if (!GVTy) |
3616 | return error(Message: "Invalid dso_local record" ); |
3617 | V = BitcodeConstant::create( |
3618 | A&: Alloc, Ty: CurTy, Info: BitcodeConstant::DSOLocalEquivalentOpcode, OpIDs: Record[1]); |
3619 | break; |
3620 | } |
3621 | case bitc::CST_CODE_NO_CFI_VALUE: { |
3622 | if (Record.size() < 2) |
3623 | return error(Message: "Invalid no_cfi record" ); |
3624 | unsigned GVTyID = Record[0]; |
3625 | Type *GVTy = getTypeByID(ID: GVTyID); |
3626 | if (!GVTy) |
3627 | return error(Message: "Invalid no_cfi record" ); |
3628 | V = BitcodeConstant::create(A&: Alloc, Ty: CurTy, Info: BitcodeConstant::NoCFIOpcode, |
3629 | OpIDs: Record[1]); |
3630 | break; |
3631 | } |
3632 | } |
3633 | |
3634 | assert(V->getType() == getTypeByID(CurTyID) && "Incorrect result type ID" ); |
3635 | if (Error Err = ValueList.assignValue(Idx: NextCstNo, V, TypeID: CurTyID)) |
3636 | return Err; |
3637 | ++NextCstNo; |
3638 | } |
3639 | } |
3640 | |
3641 | Error BitcodeReader::parseUseLists() { |
3642 | if (Error Err = Stream.EnterSubBlock(BlockID: bitc::USELIST_BLOCK_ID)) |
3643 | return Err; |
3644 | |
3645 | // Read all the records. |
3646 | SmallVector<uint64_t, 64> Record; |
3647 | |
3648 | while (true) { |
3649 | Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks(); |
3650 | if (!MaybeEntry) |
3651 | return MaybeEntry.takeError(); |
3652 | BitstreamEntry Entry = MaybeEntry.get(); |
3653 | |
3654 | switch (Entry.Kind) { |
3655 | case BitstreamEntry::SubBlock: // Handled for us already. |
3656 | case BitstreamEntry::Error: |
3657 | return error(Message: "Malformed block" ); |
3658 | case BitstreamEntry::EndBlock: |
3659 | return Error::success(); |
3660 | case BitstreamEntry::Record: |
3661 | // The interesting case. |
3662 | break; |
3663 | } |
3664 | |
3665 | // Read a use list record. |
3666 | Record.clear(); |
3667 | bool IsBB = false; |
3668 | Expected<unsigned> MaybeRecord = Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record); |
3669 | if (!MaybeRecord) |
3670 | return MaybeRecord.takeError(); |
3671 | switch (MaybeRecord.get()) { |
3672 | default: // Default behavior: unknown type. |
3673 | break; |
3674 | case bitc::USELIST_CODE_BB: |
3675 | IsBB = true; |
3676 | [[fallthrough]]; |
3677 | case bitc::USELIST_CODE_DEFAULT: { |
3678 | unsigned RecordLength = Record.size(); |
3679 | if (RecordLength < 3) |
3680 | // Records should have at least an ID and two indexes. |
3681 | return error(Message: "Invalid record" ); |
3682 | unsigned ID = Record.pop_back_val(); |
3683 | |
3684 | Value *V; |
3685 | if (IsBB) { |
3686 | assert(ID < FunctionBBs.size() && "Basic block not found" ); |
3687 | V = FunctionBBs[ID]; |
3688 | } else |
3689 | V = ValueList[ID]; |
3690 | unsigned NumUses = 0; |
3691 | SmallDenseMap<const Use *, unsigned, 16> Order; |
3692 | for (const Use &U : V->materialized_uses()) { |
3693 | if (++NumUses > Record.size()) |
3694 | break; |
3695 | Order[&U] = Record[NumUses - 1]; |
3696 | } |
3697 | if (Order.size() != Record.size() || NumUses > Record.size()) |
3698 | // Mismatches can happen if the functions are being materialized lazily |
3699 | // (out-of-order), or a value has been upgraded. |
3700 | break; |
3701 | |
3702 | V->sortUseList(Cmp: [&](const Use &L, const Use &R) { |
3703 | return Order.lookup(Val: &L) < Order.lookup(Val: &R); |
3704 | }); |
3705 | break; |
3706 | } |
3707 | } |
3708 | } |
3709 | } |
3710 | |
3711 | /// When we see the block for metadata, remember where it is and then skip it. |
3712 | /// This lets us lazily deserialize the metadata. |
3713 | Error BitcodeReader::rememberAndSkipMetadata() { |
3714 | // Save the current stream state. |
3715 | uint64_t CurBit = Stream.GetCurrentBitNo(); |
3716 | DeferredMetadataInfo.push_back(x: CurBit); |
3717 | |
3718 | // Skip over the block for now. |
3719 | if (Error Err = Stream.SkipBlock()) |
3720 | return Err; |
3721 | return Error::success(); |
3722 | } |
3723 | |
3724 | Error BitcodeReader::materializeMetadata() { |
3725 | for (uint64_t BitPos : DeferredMetadataInfo) { |
3726 | // Move the bit stream to the saved position. |
3727 | if (Error JumpFailed = Stream.JumpToBit(BitNo: BitPos)) |
3728 | return JumpFailed; |
3729 | if (Error Err = MDLoader->parseModuleMetadata()) |
3730 | return Err; |
3731 | } |
3732 | |
3733 | // Upgrade "Linker Options" module flag to "llvm.linker.options" module-level |
3734 | // metadata. Only upgrade if the new option doesn't exist to avoid upgrade |
3735 | // multiple times. |
3736 | if (!TheModule->getNamedMetadata(Name: "llvm.linker.options" )) { |
3737 | if (Metadata *Val = TheModule->getModuleFlag(Key: "Linker Options" )) { |
3738 | NamedMDNode *LinkerOpts = |
3739 | TheModule->getOrInsertNamedMetadata(Name: "llvm.linker.options" ); |
3740 | for (const MDOperand &MDOptions : cast<MDNode>(Val)->operands()) |
3741 | LinkerOpts->addOperand(M: cast<MDNode>(Val: MDOptions)); |
3742 | } |
3743 | } |
3744 | |
3745 | DeferredMetadataInfo.clear(); |
3746 | return Error::success(); |
3747 | } |
3748 | |
3749 | void BitcodeReader::setStripDebugInfo() { StripDebugInfo = true; } |
3750 | |
3751 | /// When we see the block for a function body, remember where it is and then |
3752 | /// skip it. This lets us lazily deserialize the functions. |
3753 | Error BitcodeReader::rememberAndSkipFunctionBody() { |
3754 | // Get the function we are talking about. |
3755 | if (FunctionsWithBodies.empty()) |
3756 | return error(Message: "Insufficient function protos" ); |
3757 | |
3758 | Function *Fn = FunctionsWithBodies.back(); |
3759 | FunctionsWithBodies.pop_back(); |
3760 | |
3761 | // Save the current stream state. |
3762 | uint64_t CurBit = Stream.GetCurrentBitNo(); |
3763 | assert( |
3764 | (DeferredFunctionInfo[Fn] == 0 || DeferredFunctionInfo[Fn] == CurBit) && |
3765 | "Mismatch between VST and scanned function offsets" ); |
3766 | DeferredFunctionInfo[Fn] = CurBit; |
3767 | |
3768 | // Skip over the function block for now. |
3769 | if (Error Err = Stream.SkipBlock()) |
3770 | return Err; |
3771 | return Error::success(); |
3772 | } |
3773 | |
3774 | Error BitcodeReader::globalCleanup() { |
3775 | // Patch the initializers for globals and aliases up. |
3776 | if (Error Err = resolveGlobalAndIndirectSymbolInits()) |
3777 | return Err; |
3778 | if (!GlobalInits.empty() || !IndirectSymbolInits.empty()) |
3779 | return error(Message: "Malformed global initializer set" ); |
3780 | |
3781 | // Look for intrinsic functions which need to be upgraded at some point |
3782 | // and functions that need to have their function attributes upgraded. |
3783 | for (Function &F : *TheModule) { |
3784 | MDLoader->upgradeDebugIntrinsics(F); |
3785 | Function *NewFn; |
3786 | // If PreserveInputDbgFormat=true, then we don't know whether we want |
3787 | // intrinsics or records, and we won't perform any conversions in either |
3788 | // case, so don't upgrade intrinsics to records. |
3789 | if (UpgradeIntrinsicFunction( |
3790 | F: &F, NewFn, CanUpgradeDebugIntrinsicsToRecords: PreserveInputDbgFormat != cl::boolOrDefault::BOU_TRUE)) |
3791 | UpgradedIntrinsics[&F] = NewFn; |
3792 | // Look for functions that rely on old function attribute behavior. |
3793 | UpgradeFunctionAttributes(F); |
3794 | } |
3795 | |
3796 | // Look for global variables which need to be renamed. |
3797 | std::vector<std::pair<GlobalVariable *, GlobalVariable *>> UpgradedVariables; |
3798 | for (GlobalVariable &GV : TheModule->globals()) |
3799 | if (GlobalVariable *Upgraded = UpgradeGlobalVariable(GV: &GV)) |
3800 | UpgradedVariables.emplace_back(args: &GV, args&: Upgraded); |
3801 | for (auto &Pair : UpgradedVariables) { |
3802 | Pair.first->eraseFromParent(); |
3803 | TheModule->insertGlobalVariable(GV: Pair.second); |
3804 | } |
3805 | |
3806 | // Force deallocation of memory for these vectors to favor the client that |
3807 | // want lazy deserialization. |
3808 | std::vector<std::pair<GlobalVariable *, unsigned>>().swap(x&: GlobalInits); |
3809 | std::vector<std::pair<GlobalValue *, unsigned>>().swap(x&: IndirectSymbolInits); |
3810 | return Error::success(); |
3811 | } |
3812 | |
3813 | /// Support for lazy parsing of function bodies. This is required if we |
3814 | /// either have an old bitcode file without a VST forward declaration record, |
3815 | /// or if we have an anonymous function being materialized, since anonymous |
3816 | /// functions do not have a name and are therefore not in the VST. |
3817 | Error BitcodeReader::rememberAndSkipFunctionBodies() { |
3818 | if (Error JumpFailed = Stream.JumpToBit(BitNo: NextUnreadBit)) |
3819 | return JumpFailed; |
3820 | |
3821 | if (Stream.AtEndOfStream()) |
3822 | return error(Message: "Could not find function in stream" ); |
3823 | |
3824 | if (!SeenFirstFunctionBody) |
3825 | return error(Message: "Trying to materialize functions before seeing function blocks" ); |
3826 | |
3827 | // An old bitcode file with the symbol table at the end would have |
3828 | // finished the parse greedily. |
3829 | assert(SeenValueSymbolTable); |
3830 | |
3831 | SmallVector<uint64_t, 64> Record; |
3832 | |
3833 | while (true) { |
3834 | Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); |
3835 | if (!MaybeEntry) |
3836 | return MaybeEntry.takeError(); |
3837 | llvm::BitstreamEntry Entry = MaybeEntry.get(); |
3838 | |
3839 | switch (Entry.Kind) { |
3840 | default: |
3841 | return error(Message: "Expect SubBlock" ); |
3842 | case BitstreamEntry::SubBlock: |
3843 | switch (Entry.ID) { |
3844 | default: |
3845 | return error(Message: "Expect function block" ); |
3846 | case bitc::FUNCTION_BLOCK_ID: |
3847 | if (Error Err = rememberAndSkipFunctionBody()) |
3848 | return Err; |
3849 | NextUnreadBit = Stream.GetCurrentBitNo(); |
3850 | return Error::success(); |
3851 | } |
3852 | } |
3853 | } |
3854 | } |
3855 | |
3856 | Error BitcodeReaderBase::readBlockInfo() { |
3857 | Expected<std::optional<BitstreamBlockInfo>> MaybeNewBlockInfo = |
3858 | Stream.ReadBlockInfoBlock(); |
3859 | if (!MaybeNewBlockInfo) |
3860 | return MaybeNewBlockInfo.takeError(); |
3861 | std::optional<BitstreamBlockInfo> NewBlockInfo = |
3862 | std::move(MaybeNewBlockInfo.get()); |
3863 | if (!NewBlockInfo) |
3864 | return error(Message: "Malformed block" ); |
3865 | BlockInfo = std::move(*NewBlockInfo); |
3866 | return Error::success(); |
3867 | } |
3868 | |
3869 | Error BitcodeReader::parseComdatRecord(ArrayRef<uint64_t> Record) { |
3870 | // v1: [selection_kind, name] |
3871 | // v2: [strtab_offset, strtab_size, selection_kind] |
3872 | StringRef Name; |
3873 | std::tie(args&: Name, args&: Record) = readNameFromStrtab(Record); |
3874 | |
3875 | if (Record.empty()) |
3876 | return error(Message: "Invalid record" ); |
3877 | Comdat::SelectionKind SK = getDecodedComdatSelectionKind(Val: Record[0]); |
3878 | std::string OldFormatName; |
3879 | if (!UseStrtab) { |
3880 | if (Record.size() < 2) |
3881 | return error(Message: "Invalid record" ); |
3882 | unsigned ComdatNameSize = Record[1]; |
3883 | if (ComdatNameSize > Record.size() - 2) |
3884 | return error(Message: "Comdat name size too large" ); |
3885 | OldFormatName.reserve(res: ComdatNameSize); |
3886 | for (unsigned i = 0; i != ComdatNameSize; ++i) |
3887 | OldFormatName += (char)Record[2 + i]; |
3888 | Name = OldFormatName; |
3889 | } |
3890 | Comdat *C = TheModule->getOrInsertComdat(Name); |
3891 | C->setSelectionKind(SK); |
3892 | ComdatList.push_back(x: C); |
3893 | return Error::success(); |
3894 | } |
3895 | |
3896 | static void inferDSOLocal(GlobalValue *GV) { |
3897 | // infer dso_local from linkage and visibility if it is not encoded. |
3898 | if (GV->hasLocalLinkage() || |
3899 | (!GV->hasDefaultVisibility() && !GV->hasExternalWeakLinkage())) |
3900 | GV->setDSOLocal(true); |
3901 | } |
3902 | |
3903 | GlobalValue::SanitizerMetadata deserializeSanitizerMetadata(unsigned V) { |
3904 | GlobalValue::SanitizerMetadata Meta; |
3905 | if (V & (1 << 0)) |
3906 | Meta.NoAddress = true; |
3907 | if (V & (1 << 1)) |
3908 | Meta.NoHWAddress = true; |
3909 | if (V & (1 << 2)) |
3910 | Meta.Memtag = true; |
3911 | if (V & (1 << 3)) |
3912 | Meta.IsDynInit = true; |
3913 | return Meta; |
3914 | } |
3915 | |
3916 | Error BitcodeReader::parseGlobalVarRecord(ArrayRef<uint64_t> Record) { |
3917 | // v1: [pointer type, isconst, initid, linkage, alignment, section, |
3918 | // visibility, threadlocal, unnamed_addr, externally_initialized, |
3919 | // dllstorageclass, comdat, attributes, preemption specifier, |
3920 | // partition strtab offset, partition strtab size] (name in VST) |
3921 | // v2: [strtab_offset, strtab_size, v1] |
3922 | // v3: [v2, code_model] |
3923 | StringRef Name; |
3924 | std::tie(args&: Name, args&: Record) = readNameFromStrtab(Record); |
3925 | |
3926 | if (Record.size() < 6) |
3927 | return error(Message: "Invalid record" ); |
3928 | unsigned TyID = Record[0]; |
3929 | Type *Ty = getTypeByID(ID: TyID); |
3930 | if (!Ty) |
3931 | return error(Message: "Invalid record" ); |
3932 | bool isConstant = Record[1] & 1; |
3933 | bool explicitType = Record[1] & 2; |
3934 | unsigned AddressSpace; |
3935 | if (explicitType) { |
3936 | AddressSpace = Record[1] >> 2; |
3937 | } else { |
3938 | if (!Ty->isPointerTy()) |
3939 | return error(Message: "Invalid type for value" ); |
3940 | AddressSpace = cast<PointerType>(Val: Ty)->getAddressSpace(); |
3941 | TyID = getContainedTypeID(ID: TyID); |
3942 | Ty = getTypeByID(ID: TyID); |
3943 | if (!Ty) |
3944 | return error(Message: "Missing element type for old-style global" ); |
3945 | } |
3946 | |
3947 | uint64_t RawLinkage = Record[3]; |
3948 | GlobalValue::LinkageTypes Linkage = getDecodedLinkage(Val: RawLinkage); |
3949 | MaybeAlign Alignment; |
3950 | if (Error Err = parseAlignmentValue(Exponent: Record[4], Alignment)) |
3951 | return Err; |
3952 | std::string Section; |
3953 | if (Record[5]) { |
3954 | if (Record[5] - 1 >= SectionTable.size()) |
3955 | return error(Message: "Invalid ID" ); |
3956 | Section = SectionTable[Record[5] - 1]; |
3957 | } |
3958 | GlobalValue::VisibilityTypes Visibility = GlobalValue::DefaultVisibility; |
3959 | // Local linkage must have default visibility. |
3960 | // auto-upgrade `hidden` and `protected` for old bitcode. |
3961 | if (Record.size() > 6 && !GlobalValue::isLocalLinkage(Linkage)) |
3962 | Visibility = getDecodedVisibility(Val: Record[6]); |
3963 | |
3964 | GlobalVariable::ThreadLocalMode TLM = GlobalVariable::NotThreadLocal; |
3965 | if (Record.size() > 7) |
3966 | TLM = getDecodedThreadLocalMode(Val: Record[7]); |
3967 | |
3968 | GlobalValue::UnnamedAddr UnnamedAddr = GlobalValue::UnnamedAddr::None; |
3969 | if (Record.size() > 8) |
3970 | UnnamedAddr = getDecodedUnnamedAddrType(Val: Record[8]); |
3971 | |
3972 | bool ExternallyInitialized = false; |
3973 | if (Record.size() > 9) |
3974 | ExternallyInitialized = Record[9]; |
3975 | |
3976 | GlobalVariable *NewGV = |
3977 | new GlobalVariable(*TheModule, Ty, isConstant, Linkage, nullptr, Name, |
3978 | nullptr, TLM, AddressSpace, ExternallyInitialized); |
3979 | if (Alignment) |
3980 | NewGV->setAlignment(*Alignment); |
3981 | if (!Section.empty()) |
3982 | NewGV->setSection(Section); |
3983 | NewGV->setVisibility(Visibility); |
3984 | NewGV->setUnnamedAddr(UnnamedAddr); |
3985 | |
3986 | if (Record.size() > 10) { |
3987 | // A GlobalValue with local linkage cannot have a DLL storage class. |
3988 | if (!NewGV->hasLocalLinkage()) { |
3989 | NewGV->setDLLStorageClass(getDecodedDLLStorageClass(Val: Record[10])); |
3990 | } |
3991 | } else { |
3992 | upgradeDLLImportExportLinkage(GV: NewGV, Val: RawLinkage); |
3993 | } |
3994 | |
3995 | ValueList.push_back(V: NewGV, TypeID: getVirtualTypeID(Ty: NewGV->getType(), ChildTypeIDs: TyID)); |
3996 | |
3997 | // Remember which value to use for the global initializer. |
3998 | if (unsigned InitID = Record[2]) |
3999 | GlobalInits.push_back(x: std::make_pair(x&: NewGV, y: InitID - 1)); |
4000 | |
4001 | if (Record.size() > 11) { |
4002 | if (unsigned ComdatID = Record[11]) { |
4003 | if (ComdatID > ComdatList.size()) |
4004 | return error(Message: "Invalid global variable comdat ID" ); |
4005 | NewGV->setComdat(ComdatList[ComdatID - 1]); |
4006 | } |
4007 | } else if (hasImplicitComdat(Val: RawLinkage)) { |
4008 | ImplicitComdatObjects.insert(V: NewGV); |
4009 | } |
4010 | |
4011 | if (Record.size() > 12) { |
4012 | auto AS = getAttributes(i: Record[12]).getFnAttrs(); |
4013 | NewGV->setAttributes(AS); |
4014 | } |
4015 | |
4016 | if (Record.size() > 13) { |
4017 | NewGV->setDSOLocal(getDecodedDSOLocal(Val: Record[13])); |
4018 | } |
4019 | inferDSOLocal(GV: NewGV); |
4020 | |
4021 | // Check whether we have enough values to read a partition name. |
4022 | if (Record.size() > 15) |
4023 | NewGV->setPartition(StringRef(Strtab.data() + Record[14], Record[15])); |
4024 | |
4025 | if (Record.size() > 16 && Record[16]) { |
4026 | llvm::GlobalValue::SanitizerMetadata Meta = |
4027 | deserializeSanitizerMetadata(V: Record[16]); |
4028 | NewGV->setSanitizerMetadata(Meta); |
4029 | } |
4030 | |
4031 | if (Record.size() > 17 && Record[17]) { |
4032 | if (auto CM = getDecodedCodeModel(Val: Record[17])) |
4033 | NewGV->setCodeModel(*CM); |
4034 | else |
4035 | return error(Message: "Invalid global variable code model" ); |
4036 | } |
4037 | |
4038 | return Error::success(); |
4039 | } |
4040 | |
4041 | void BitcodeReader::callValueTypeCallback(Value *F, unsigned TypeID) { |
4042 | if (ValueTypeCallback) { |
4043 | (*ValueTypeCallback)( |
4044 | F, TypeID, [this](unsigned I) { return getTypeByID(ID: I); }, |
4045 | [this](unsigned I, unsigned J) { return getContainedTypeID(ID: I, Idx: J); }); |
4046 | } |
4047 | } |
4048 | |
4049 | Error BitcodeReader::parseFunctionRecord(ArrayRef<uint64_t> Record) { |
4050 | // v1: [type, callingconv, isproto, linkage, paramattr, alignment, section, |
4051 | // visibility, gc, unnamed_addr, prologuedata, dllstorageclass, comdat, |
4052 | // prefixdata, personalityfn, preemption specifier, addrspace] (name in VST) |
4053 | // v2: [strtab_offset, strtab_size, v1] |
4054 | StringRef Name; |
4055 | std::tie(args&: Name, args&: Record) = readNameFromStrtab(Record); |
4056 | |
4057 | if (Record.size() < 8) |
4058 | return error(Message: "Invalid record" ); |
4059 | unsigned FTyID = Record[0]; |
4060 | Type *FTy = getTypeByID(ID: FTyID); |
4061 | if (!FTy) |
4062 | return error(Message: "Invalid record" ); |
4063 | if (isa<PointerType>(Val: FTy)) { |
4064 | FTyID = getContainedTypeID(ID: FTyID, Idx: 0); |
4065 | FTy = getTypeByID(ID: FTyID); |
4066 | if (!FTy) |
4067 | return error(Message: "Missing element type for old-style function" ); |
4068 | } |
4069 | |
4070 | if (!isa<FunctionType>(Val: FTy)) |
4071 | return error(Message: "Invalid type for value" ); |
4072 | auto CC = static_cast<CallingConv::ID>(Record[1]); |
4073 | if (CC & ~CallingConv::MaxID) |
4074 | return error(Message: "Invalid calling convention ID" ); |
4075 | |
4076 | unsigned AddrSpace = TheModule->getDataLayout().getProgramAddressSpace(); |
4077 | if (Record.size() > 16) |
4078 | AddrSpace = Record[16]; |
4079 | |
4080 | Function *Func = |
4081 | Function::Create(Ty: cast<FunctionType>(Val: FTy), Linkage: GlobalValue::ExternalLinkage, |
4082 | AddrSpace, N: Name, M: TheModule); |
4083 | |
4084 | assert(Func->getFunctionType() == FTy && |
4085 | "Incorrect fully specified type provided for function" ); |
4086 | FunctionTypeIDs[Func] = FTyID; |
4087 | |
4088 | Func->setCallingConv(CC); |
4089 | bool isProto = Record[2]; |
4090 | uint64_t RawLinkage = Record[3]; |
4091 | Func->setLinkage(getDecodedLinkage(Val: RawLinkage)); |
4092 | Func->setAttributes(getAttributes(i: Record[4])); |
4093 | callValueTypeCallback(F: Func, TypeID: FTyID); |
4094 | |
4095 | // Upgrade any old-style byval or sret without a type by propagating the |
4096 | // argument's pointee type. There should be no opaque pointers where the byval |
4097 | // type is implicit. |
4098 | for (unsigned i = 0; i != Func->arg_size(); ++i) { |
4099 | for (Attribute::AttrKind Kind : {Attribute::ByVal, Attribute::StructRet, |
4100 | Attribute::InAlloca}) { |
4101 | if (!Func->hasParamAttribute(i, Kind)) |
4102 | continue; |
4103 | |
4104 | if (Func->getParamAttribute(i, Kind).getValueAsType()) |
4105 | continue; |
4106 | |
4107 | Func->removeParamAttr(i, Kind); |
4108 | |
4109 | unsigned ParamTypeID = getContainedTypeID(FTyID, i + 1); |
4110 | Type *PtrEltTy = getPtrElementTypeByID(ParamTypeID); |
4111 | if (!PtrEltTy) |
4112 | return error("Missing param element type for attribute upgrade" ); |
4113 | |
4114 | Attribute NewAttr; |
4115 | switch (Kind) { |
4116 | case Attribute::ByVal: |
4117 | NewAttr = Attribute::getWithByValType(Context, PtrEltTy); |
4118 | break; |
4119 | case Attribute::StructRet: |
4120 | NewAttr = Attribute::getWithStructRetType(Context, PtrEltTy); |
4121 | break; |
4122 | case Attribute::InAlloca: |
4123 | NewAttr = Attribute::getWithInAllocaType(Context, PtrEltTy); |
4124 | break; |
4125 | default: |
4126 | llvm_unreachable("not an upgraded type attribute" ); |
4127 | } |
4128 | |
4129 | Func->addParamAttr(i, NewAttr); |
4130 | } |
4131 | } |
4132 | |
4133 | if (Func->getCallingConv() == CallingConv::X86_INTR && |
4134 | !Func->arg_empty() && !Func->hasParamAttribute(0, Attribute::ByVal)) { |
4135 | unsigned ParamTypeID = getContainedTypeID(ID: FTyID, Idx: 1); |
4136 | Type *ByValTy = getPtrElementTypeByID(ID: ParamTypeID); |
4137 | if (!ByValTy) |
4138 | return error(Message: "Missing param element type for x86_intrcc upgrade" ); |
4139 | Attribute NewAttr = Attribute::getWithByValType(Context, Ty: ByValTy); |
4140 | Func->addParamAttr(ArgNo: 0, Attr: NewAttr); |
4141 | } |
4142 | |
4143 | MaybeAlign Alignment; |
4144 | if (Error Err = parseAlignmentValue(Exponent: Record[5], Alignment)) |
4145 | return Err; |
4146 | if (Alignment) |
4147 | Func->setAlignment(*Alignment); |
4148 | if (Record[6]) { |
4149 | if (Record[6] - 1 >= SectionTable.size()) |
4150 | return error(Message: "Invalid ID" ); |
4151 | Func->setSection(SectionTable[Record[6] - 1]); |
4152 | } |
4153 | // Local linkage must have default visibility. |
4154 | // auto-upgrade `hidden` and `protected` for old bitcode. |
4155 | if (!Func->hasLocalLinkage()) |
4156 | Func->setVisibility(getDecodedVisibility(Val: Record[7])); |
4157 | if (Record.size() > 8 && Record[8]) { |
4158 | if (Record[8] - 1 >= GCTable.size()) |
4159 | return error(Message: "Invalid ID" ); |
4160 | Func->setGC(GCTable[Record[8] - 1]); |
4161 | } |
4162 | GlobalValue::UnnamedAddr UnnamedAddr = GlobalValue::UnnamedAddr::None; |
4163 | if (Record.size() > 9) |
4164 | UnnamedAddr = getDecodedUnnamedAddrType(Val: Record[9]); |
4165 | Func->setUnnamedAddr(UnnamedAddr); |
4166 | |
4167 | FunctionOperandInfo OperandInfo = {.F: Func, .PersonalityFn: 0, .Prefix: 0, .Prologue: 0}; |
4168 | if (Record.size() > 10) |
4169 | OperandInfo.Prologue = Record[10]; |
4170 | |
4171 | if (Record.size() > 11) { |
4172 | // A GlobalValue with local linkage cannot have a DLL storage class. |
4173 | if (!Func->hasLocalLinkage()) { |
4174 | Func->setDLLStorageClass(getDecodedDLLStorageClass(Val: Record[11])); |
4175 | } |
4176 | } else { |
4177 | upgradeDLLImportExportLinkage(GV: Func, Val: RawLinkage); |
4178 | } |
4179 | |
4180 | if (Record.size() > 12) { |
4181 | if (unsigned ComdatID = Record[12]) { |
4182 | if (ComdatID > ComdatList.size()) |
4183 | return error(Message: "Invalid function comdat ID" ); |
4184 | Func->setComdat(ComdatList[ComdatID - 1]); |
4185 | } |
4186 | } else if (hasImplicitComdat(Val: RawLinkage)) { |
4187 | ImplicitComdatObjects.insert(V: Func); |
4188 | } |
4189 | |
4190 | if (Record.size() > 13) |
4191 | OperandInfo.Prefix = Record[13]; |
4192 | |
4193 | if (Record.size() > 14) |
4194 | OperandInfo.PersonalityFn = Record[14]; |
4195 | |
4196 | if (Record.size() > 15) { |
4197 | Func->setDSOLocal(getDecodedDSOLocal(Val: Record[15])); |
4198 | } |
4199 | inferDSOLocal(GV: Func); |
4200 | |
4201 | // Record[16] is the address space number. |
4202 | |
4203 | // Check whether we have enough values to read a partition name. Also make |
4204 | // sure Strtab has enough values. |
4205 | if (Record.size() > 18 && Strtab.data() && |
4206 | Record[17] + Record[18] <= Strtab.size()) { |
4207 | Func->setPartition(StringRef(Strtab.data() + Record[17], Record[18])); |
4208 | } |
4209 | |
4210 | ValueList.push_back(V: Func, TypeID: getVirtualTypeID(Ty: Func->getType(), ChildTypeIDs: FTyID)); |
4211 | |
4212 | if (OperandInfo.PersonalityFn || OperandInfo.Prefix || OperandInfo.Prologue) |
4213 | FunctionOperands.push_back(x: OperandInfo); |
4214 | |
4215 | // If this is a function with a body, remember the prototype we are |
4216 | // creating now, so that we can match up the body with them later. |
4217 | if (!isProto) { |
4218 | Func->setIsMaterializable(true); |
4219 | FunctionsWithBodies.push_back(x: Func); |
4220 | DeferredFunctionInfo[Func] = 0; |
4221 | } |
4222 | return Error::success(); |
4223 | } |
4224 | |
4225 | Error BitcodeReader::parseGlobalIndirectSymbolRecord( |
4226 | unsigned BitCode, ArrayRef<uint64_t> Record) { |
4227 | // v1 ALIAS_OLD: [alias type, aliasee val#, linkage] (name in VST) |
4228 | // v1 ALIAS: [alias type, addrspace, aliasee val#, linkage, visibility, |
4229 | // dllstorageclass, threadlocal, unnamed_addr, |
4230 | // preemption specifier] (name in VST) |
4231 | // v1 IFUNC: [alias type, addrspace, aliasee val#, linkage, |
4232 | // visibility, dllstorageclass, threadlocal, unnamed_addr, |
4233 | // preemption specifier] (name in VST) |
4234 | // v2: [strtab_offset, strtab_size, v1] |
4235 | StringRef Name; |
4236 | std::tie(args&: Name, args&: Record) = readNameFromStrtab(Record); |
4237 | |
4238 | bool NewRecord = BitCode != bitc::MODULE_CODE_ALIAS_OLD; |
4239 | if (Record.size() < (3 + (unsigned)NewRecord)) |
4240 | return error(Message: "Invalid record" ); |
4241 | unsigned OpNum = 0; |
4242 | unsigned TypeID = Record[OpNum++]; |
4243 | Type *Ty = getTypeByID(ID: TypeID); |
4244 | if (!Ty) |
4245 | return error(Message: "Invalid record" ); |
4246 | |
4247 | unsigned AddrSpace; |
4248 | if (!NewRecord) { |
4249 | auto *PTy = dyn_cast<PointerType>(Val: Ty); |
4250 | if (!PTy) |
4251 | return error(Message: "Invalid type for value" ); |
4252 | AddrSpace = PTy->getAddressSpace(); |
4253 | TypeID = getContainedTypeID(ID: TypeID); |
4254 | Ty = getTypeByID(ID: TypeID); |
4255 | if (!Ty) |
4256 | return error(Message: "Missing element type for old-style indirect symbol" ); |
4257 | } else { |
4258 | AddrSpace = Record[OpNum++]; |
4259 | } |
4260 | |
4261 | auto Val = Record[OpNum++]; |
4262 | auto Linkage = Record[OpNum++]; |
4263 | GlobalValue *NewGA; |
4264 | if (BitCode == bitc::MODULE_CODE_ALIAS || |
4265 | BitCode == bitc::MODULE_CODE_ALIAS_OLD) |
4266 | NewGA = GlobalAlias::create(Ty, AddressSpace: AddrSpace, Linkage: getDecodedLinkage(Val: Linkage), Name, |
4267 | Parent: TheModule); |
4268 | else |
4269 | NewGA = GlobalIFunc::create(Ty, AddressSpace: AddrSpace, Linkage: getDecodedLinkage(Val: Linkage), Name, |
4270 | Resolver: nullptr, Parent: TheModule); |
4271 | |
4272 | // Local linkage must have default visibility. |
4273 | // auto-upgrade `hidden` and `protected` for old bitcode. |
4274 | if (OpNum != Record.size()) { |
4275 | auto VisInd = OpNum++; |
4276 | if (!NewGA->hasLocalLinkage()) |
4277 | NewGA->setVisibility(getDecodedVisibility(Val: Record[VisInd])); |
4278 | } |
4279 | if (BitCode == bitc::MODULE_CODE_ALIAS || |
4280 | BitCode == bitc::MODULE_CODE_ALIAS_OLD) { |
4281 | if (OpNum != Record.size()) { |
4282 | auto S = Record[OpNum++]; |
4283 | // A GlobalValue with local linkage cannot have a DLL storage class. |
4284 | if (!NewGA->hasLocalLinkage()) |
4285 | NewGA->setDLLStorageClass(getDecodedDLLStorageClass(Val: S)); |
4286 | } |
4287 | else |
4288 | upgradeDLLImportExportLinkage(GV: NewGA, Val: Linkage); |
4289 | if (OpNum != Record.size()) |
4290 | NewGA->setThreadLocalMode(getDecodedThreadLocalMode(Val: Record[OpNum++])); |
4291 | if (OpNum != Record.size()) |
4292 | NewGA->setUnnamedAddr(getDecodedUnnamedAddrType(Val: Record[OpNum++])); |
4293 | } |
4294 | if (OpNum != Record.size()) |
4295 | NewGA->setDSOLocal(getDecodedDSOLocal(Val: Record[OpNum++])); |
4296 | inferDSOLocal(GV: NewGA); |
4297 | |
4298 | // Check whether we have enough values to read a partition name. |
4299 | if (OpNum + 1 < Record.size()) { |
4300 | // Check Strtab has enough values for the partition. |
4301 | if (Record[OpNum] + Record[OpNum + 1] > Strtab.size()) |
4302 | return error(Message: "Malformed partition, too large." ); |
4303 | NewGA->setPartition( |
4304 | StringRef(Strtab.data() + Record[OpNum], Record[OpNum + 1])); |
4305 | OpNum += 2; |
4306 | } |
4307 | |
4308 | ValueList.push_back(V: NewGA, TypeID: getVirtualTypeID(Ty: NewGA->getType(), ChildTypeIDs: TypeID)); |
4309 | IndirectSymbolInits.push_back(x: std::make_pair(x&: NewGA, y&: Val)); |
4310 | return Error::success(); |
4311 | } |
4312 | |
4313 | Error BitcodeReader::parseModule(uint64_t ResumeBit, |
4314 | bool ShouldLazyLoadMetadata, |
4315 | ParserCallbacks Callbacks) { |
4316 | // Load directly into RemoveDIs format if LoadBitcodeIntoNewDbgInfoFormat |
4317 | // has been set to true and we aren't attempting to preserve the existing |
4318 | // format in the bitcode (default action: load into the old debug format). |
4319 | if (PreserveInputDbgFormat != cl::boolOrDefault::BOU_TRUE) { |
4320 | TheModule->IsNewDbgInfoFormat = |
4321 | UseNewDbgInfoFormat && |
4322 | LoadBitcodeIntoNewDbgInfoFormat == cl::boolOrDefault::BOU_TRUE; |
4323 | } |
4324 | |
4325 | this->ValueTypeCallback = std::move(Callbacks.ValueType); |
4326 | if (ResumeBit) { |
4327 | if (Error JumpFailed = Stream.JumpToBit(BitNo: ResumeBit)) |
4328 | return JumpFailed; |
4329 | } else if (Error Err = Stream.EnterSubBlock(BlockID: bitc::MODULE_BLOCK_ID)) |
4330 | return Err; |
4331 | |
4332 | SmallVector<uint64_t, 64> Record; |
4333 | |
4334 | // Parts of bitcode parsing depend on the datalayout. Make sure we |
4335 | // finalize the datalayout before we run any of that code. |
4336 | bool ResolvedDataLayout = false; |
4337 | // In order to support importing modules with illegal data layout strings, |
4338 | // delay parsing the data layout string until after upgrades and overrides |
4339 | // have been applied, allowing to fix illegal data layout strings. |
4340 | // Initialize to the current module's layout string in case none is specified. |
4341 | std::string TentativeDataLayoutStr = TheModule->getDataLayoutStr(); |
4342 | |
4343 | auto ResolveDataLayout = [&]() -> Error { |
4344 | if (ResolvedDataLayout) |
4345 | return Error::success(); |
4346 | |
4347 | // Datalayout and triple can't be parsed after this point. |
4348 | ResolvedDataLayout = true; |
4349 | |
4350 | // Auto-upgrade the layout string |
4351 | TentativeDataLayoutStr = llvm::UpgradeDataLayoutString( |
4352 | DL: TentativeDataLayoutStr, Triple: TheModule->getTargetTriple()); |
4353 | |
4354 | // Apply override |
4355 | if (Callbacks.DataLayout) { |
4356 | if (auto LayoutOverride = (*Callbacks.DataLayout)( |
4357 | TheModule->getTargetTriple(), TentativeDataLayoutStr)) |
4358 | TentativeDataLayoutStr = *LayoutOverride; |
4359 | } |
4360 | |
4361 | // Now the layout string is finalized in TentativeDataLayoutStr. Parse it. |
4362 | Expected<DataLayout> MaybeDL = DataLayout::parse(LayoutDescription: TentativeDataLayoutStr); |
4363 | if (!MaybeDL) |
4364 | return MaybeDL.takeError(); |
4365 | |
4366 | TheModule->setDataLayout(MaybeDL.get()); |
4367 | return Error::success(); |
4368 | }; |
4369 | |
4370 | // Read all the records for this module. |
4371 | while (true) { |
4372 | Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); |
4373 | if (!MaybeEntry) |
4374 | return MaybeEntry.takeError(); |
4375 | llvm::BitstreamEntry Entry = MaybeEntry.get(); |
4376 | |
4377 | switch (Entry.Kind) { |
4378 | case BitstreamEntry::Error: |
4379 | return error(Message: "Malformed block" ); |
4380 | case BitstreamEntry::EndBlock: |
4381 | if (Error Err = ResolveDataLayout()) |
4382 | return Err; |
4383 | return globalCleanup(); |
4384 | |
4385 | case BitstreamEntry::SubBlock: |
4386 | switch (Entry.ID) { |
4387 | default: // Skip unknown content. |
4388 | if (Error Err = Stream.SkipBlock()) |
4389 | return Err; |
4390 | break; |
4391 | case bitc::BLOCKINFO_BLOCK_ID: |
4392 | if (Error Err = readBlockInfo()) |
4393 | return Err; |
4394 | break; |
4395 | case bitc::PARAMATTR_BLOCK_ID: |
4396 | if (Error Err = parseAttributeBlock()) |
4397 | return Err; |
4398 | break; |
4399 | case bitc::PARAMATTR_GROUP_BLOCK_ID: |
4400 | if (Error Err = parseAttributeGroupBlock()) |
4401 | return Err; |
4402 | break; |
4403 | case bitc::TYPE_BLOCK_ID_NEW: |
4404 | if (Error Err = parseTypeTable()) |
4405 | return Err; |
4406 | break; |
4407 | case bitc::VALUE_SYMTAB_BLOCK_ID: |
4408 | if (!SeenValueSymbolTable) { |
4409 | // Either this is an old form VST without function index and an |
4410 | // associated VST forward declaration record (which would have caused |
4411 | // the VST to be jumped to and parsed before it was encountered |
4412 | // normally in the stream), or there were no function blocks to |
4413 | // trigger an earlier parsing of the VST. |
4414 | assert(VSTOffset == 0 || FunctionsWithBodies.empty()); |
4415 | if (Error Err = parseValueSymbolTable()) |
4416 | return Err; |
4417 | SeenValueSymbolTable = true; |
4418 | } else { |
4419 | // We must have had a VST forward declaration record, which caused |
4420 | // the parser to jump to and parse the VST earlier. |
4421 | assert(VSTOffset > 0); |
4422 | if (Error Err = Stream.SkipBlock()) |
4423 | return Err; |
4424 | } |
4425 | break; |
4426 | case bitc::CONSTANTS_BLOCK_ID: |
4427 | if (Error Err = parseConstants()) |
4428 | return Err; |
4429 | if (Error Err = resolveGlobalAndIndirectSymbolInits()) |
4430 | return Err; |
4431 | break; |
4432 | case bitc::METADATA_BLOCK_ID: |
4433 | if (ShouldLazyLoadMetadata) { |
4434 | if (Error Err = rememberAndSkipMetadata()) |
4435 | return Err; |
4436 | break; |
4437 | } |
4438 | assert(DeferredMetadataInfo.empty() && "Unexpected deferred metadata" ); |
4439 | if (Error Err = MDLoader->parseModuleMetadata()) |
4440 | return Err; |
4441 | break; |
4442 | case bitc::METADATA_KIND_BLOCK_ID: |
4443 | if (Error Err = MDLoader->parseMetadataKinds()) |
4444 | return Err; |
4445 | break; |
4446 | case bitc::FUNCTION_BLOCK_ID: |
4447 | if (Error Err = ResolveDataLayout()) |
4448 | return Err; |
4449 | |
4450 | // If this is the first function body we've seen, reverse the |
4451 | // FunctionsWithBodies list. |
4452 | if (!SeenFirstFunctionBody) { |
4453 | std::reverse(first: FunctionsWithBodies.begin(), last: FunctionsWithBodies.end()); |
4454 | if (Error Err = globalCleanup()) |
4455 | return Err; |
4456 | SeenFirstFunctionBody = true; |
4457 | } |
4458 | |
4459 | if (VSTOffset > 0) { |
4460 | // If we have a VST forward declaration record, make sure we |
4461 | // parse the VST now if we haven't already. It is needed to |
4462 | // set up the DeferredFunctionInfo vector for lazy reading. |
4463 | if (!SeenValueSymbolTable) { |
4464 | if (Error Err = BitcodeReader::parseValueSymbolTable(Offset: VSTOffset)) |
4465 | return Err; |
4466 | SeenValueSymbolTable = true; |
4467 | // Fall through so that we record the NextUnreadBit below. |
4468 | // This is necessary in case we have an anonymous function that |
4469 | // is later materialized. Since it will not have a VST entry we |
4470 | // need to fall back to the lazy parse to find its offset. |
4471 | } else { |
4472 | // If we have a VST forward declaration record, but have already |
4473 | // parsed the VST (just above, when the first function body was |
4474 | // encountered here), then we are resuming the parse after |
4475 | // materializing functions. The ResumeBit points to the |
4476 | // start of the last function block recorded in the |
4477 | // DeferredFunctionInfo map. Skip it. |
4478 | if (Error Err = Stream.SkipBlock()) |
4479 | return Err; |
4480 | continue; |
4481 | } |
4482 | } |
4483 | |
4484 | // Support older bitcode files that did not have the function |
4485 | // index in the VST, nor a VST forward declaration record, as |
4486 | // well as anonymous functions that do not have VST entries. |
4487 | // Build the DeferredFunctionInfo vector on the fly. |
4488 | if (Error Err = rememberAndSkipFunctionBody()) |
4489 | return Err; |
4490 | |
4491 | // Suspend parsing when we reach the function bodies. Subsequent |
4492 | // materialization calls will resume it when necessary. If the bitcode |
4493 | // file is old, the symbol table will be at the end instead and will not |
4494 | // have been seen yet. In this case, just finish the parse now. |
4495 | if (SeenValueSymbolTable) { |
4496 | NextUnreadBit = Stream.GetCurrentBitNo(); |
4497 | // After the VST has been parsed, we need to make sure intrinsic name |
4498 | // are auto-upgraded. |
4499 | return globalCleanup(); |
4500 | } |
4501 | break; |
4502 | case bitc::USELIST_BLOCK_ID: |
4503 | if (Error Err = parseUseLists()) |
4504 | return Err; |
4505 | break; |
4506 | case bitc::OPERAND_BUNDLE_TAGS_BLOCK_ID: |
4507 | if (Error Err = parseOperandBundleTags()) |
4508 | return Err; |
4509 | break; |
4510 | case bitc::SYNC_SCOPE_NAMES_BLOCK_ID: |
4511 | if (Error Err = parseSyncScopeNames()) |
4512 | return Err; |
4513 | break; |
4514 | } |
4515 | continue; |
4516 | |
4517 | case BitstreamEntry::Record: |
4518 | // The interesting case. |
4519 | break; |
4520 | } |
4521 | |
4522 | // Read a record. |
4523 | Expected<unsigned> MaybeBitCode = Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record); |
4524 | if (!MaybeBitCode) |
4525 | return MaybeBitCode.takeError(); |
4526 | switch (unsigned BitCode = MaybeBitCode.get()) { |
4527 | default: break; // Default behavior, ignore unknown content. |
4528 | case bitc::MODULE_CODE_VERSION: { |
4529 | Expected<unsigned> VersionOrErr = parseVersionRecord(Record); |
4530 | if (!VersionOrErr) |
4531 | return VersionOrErr.takeError(); |
4532 | UseRelativeIDs = *VersionOrErr >= 1; |
4533 | break; |
4534 | } |
4535 | case bitc::MODULE_CODE_TRIPLE: { // TRIPLE: [strchr x N] |
4536 | if (ResolvedDataLayout) |
4537 | return error(Message: "target triple too late in module" ); |
4538 | std::string S; |
4539 | if (convertToString(Record, Idx: 0, Result&: S)) |
4540 | return error(Message: "Invalid record" ); |
4541 | TheModule->setTargetTriple(S); |
4542 | break; |
4543 | } |
4544 | case bitc::MODULE_CODE_DATALAYOUT: { // DATALAYOUT: [strchr x N] |
4545 | if (ResolvedDataLayout) |
4546 | return error(Message: "datalayout too late in module" ); |
4547 | if (convertToString(Record, Idx: 0, Result&: TentativeDataLayoutStr)) |
4548 | return error(Message: "Invalid record" ); |
4549 | break; |
4550 | } |
4551 | case bitc::MODULE_CODE_ASM: { // ASM: [strchr x N] |
4552 | std::string S; |
4553 | if (convertToString(Record, Idx: 0, Result&: S)) |
4554 | return error(Message: "Invalid record" ); |
4555 | TheModule->setModuleInlineAsm(S); |
4556 | break; |
4557 | } |
4558 | case bitc::MODULE_CODE_DEPLIB: { // DEPLIB: [strchr x N] |
4559 | // Deprecated, but still needed to read old bitcode files. |
4560 | std::string S; |
4561 | if (convertToString(Record, Idx: 0, Result&: S)) |
4562 | return error(Message: "Invalid record" ); |
4563 | // Ignore value. |
4564 | break; |
4565 | } |
4566 | case bitc::MODULE_CODE_SECTIONNAME: { // SECTIONNAME: [strchr x N] |
4567 | std::string S; |
4568 | if (convertToString(Record, Idx: 0, Result&: S)) |
4569 | return error(Message: "Invalid record" ); |
4570 | SectionTable.push_back(x: S); |
4571 | break; |
4572 | } |
4573 | case bitc::MODULE_CODE_GCNAME: { // SECTIONNAME: [strchr x N] |
4574 | std::string S; |
4575 | if (convertToString(Record, Idx: 0, Result&: S)) |
4576 | return error(Message: "Invalid record" ); |
4577 | GCTable.push_back(x: S); |
4578 | break; |
4579 | } |
4580 | case bitc::MODULE_CODE_COMDAT: |
4581 | if (Error Err = parseComdatRecord(Record)) |
4582 | return Err; |
4583 | break; |
4584 | // FIXME: BitcodeReader should handle {GLOBALVAR, FUNCTION, ALIAS, IFUNC} |
4585 | // written by ThinLinkBitcodeWriter. See |
4586 | // `ThinLinkBitcodeWriter::writeSimplifiedModuleInfo` for the format of each |
4587 | // record |
4588 | // (https://github.com/llvm/llvm-project/blob/b6a93967d9c11e79802b5e75cec1584d6c8aa472/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp#L4714) |
4589 | case bitc::MODULE_CODE_GLOBALVAR: |
4590 | if (Error Err = parseGlobalVarRecord(Record)) |
4591 | return Err; |
4592 | break; |
4593 | case bitc::MODULE_CODE_FUNCTION: |
4594 | if (Error Err = ResolveDataLayout()) |
4595 | return Err; |
4596 | if (Error Err = parseFunctionRecord(Record)) |
4597 | return Err; |
4598 | break; |
4599 | case bitc::MODULE_CODE_IFUNC: |
4600 | case bitc::MODULE_CODE_ALIAS: |
4601 | case bitc::MODULE_CODE_ALIAS_OLD: |
4602 | if (Error Err = parseGlobalIndirectSymbolRecord(BitCode, Record)) |
4603 | return Err; |
4604 | break; |
4605 | /// MODULE_CODE_VSTOFFSET: [offset] |
4606 | case bitc::MODULE_CODE_VSTOFFSET: |
4607 | if (Record.empty()) |
4608 | return error(Message: "Invalid record" ); |
4609 | // Note that we subtract 1 here because the offset is relative to one word |
4610 | // before the start of the identification or module block, which was |
4611 | // historically always the start of the regular bitcode header. |
4612 | VSTOffset = Record[0] - 1; |
4613 | break; |
4614 | /// MODULE_CODE_SOURCE_FILENAME: [namechar x N] |
4615 | case bitc::MODULE_CODE_SOURCE_FILENAME: |
4616 | SmallString<128> ValueName; |
4617 | if (convertToString(Record, Idx: 0, Result&: ValueName)) |
4618 | return error(Message: "Invalid record" ); |
4619 | TheModule->setSourceFileName(ValueName); |
4620 | break; |
4621 | } |
4622 | Record.clear(); |
4623 | } |
4624 | this->ValueTypeCallback = std::nullopt; |
4625 | return Error::success(); |
4626 | } |
4627 | |
4628 | Error BitcodeReader::parseBitcodeInto(Module *M, bool ShouldLazyLoadMetadata, |
4629 | bool IsImporting, |
4630 | ParserCallbacks Callbacks) { |
4631 | TheModule = M; |
4632 | MetadataLoaderCallbacks MDCallbacks; |
4633 | MDCallbacks.GetTypeByID = [&](unsigned ID) { return getTypeByID(ID); }; |
4634 | MDCallbacks.GetContainedTypeID = [&](unsigned I, unsigned J) { |
4635 | return getContainedTypeID(ID: I, Idx: J); |
4636 | }; |
4637 | MDCallbacks.MDType = Callbacks.MDType; |
4638 | MDLoader = MetadataLoader(Stream, *M, ValueList, IsImporting, MDCallbacks); |
4639 | return parseModule(ResumeBit: 0, ShouldLazyLoadMetadata, Callbacks); |
4640 | } |
4641 | |
4642 | Error BitcodeReader::typeCheckLoadStoreInst(Type *ValType, Type *PtrType) { |
4643 | if (!isa<PointerType>(Val: PtrType)) |
4644 | return error(Message: "Load/Store operand is not a pointer type" ); |
4645 | if (!PointerType::isLoadableOrStorableType(ElemTy: ValType)) |
4646 | return error(Message: "Cannot load/store from pointer" ); |
4647 | return Error::success(); |
4648 | } |
4649 | |
4650 | Error BitcodeReader::propagateAttributeTypes(CallBase *CB, |
4651 | ArrayRef<unsigned> ArgTyIDs) { |
4652 | AttributeList Attrs = CB->getAttributes(); |
4653 | for (unsigned i = 0; i != CB->arg_size(); ++i) { |
4654 | for (Attribute::AttrKind Kind : {Attribute::ByVal, Attribute::StructRet, |
4655 | Attribute::InAlloca}) { |
4656 | if (!Attrs.hasParamAttr(i, Kind) || |
4657 | Attrs.getParamAttr(i, Kind).getValueAsType()) |
4658 | continue; |
4659 | |
4660 | Type *PtrEltTy = getPtrElementTypeByID(ArgTyIDs[i]); |
4661 | if (!PtrEltTy) |
4662 | return error("Missing element type for typed attribute upgrade" ); |
4663 | |
4664 | Attribute NewAttr; |
4665 | switch (Kind) { |
4666 | case Attribute::ByVal: |
4667 | NewAttr = Attribute::getWithByValType(Context, PtrEltTy); |
4668 | break; |
4669 | case Attribute::StructRet: |
4670 | NewAttr = Attribute::getWithStructRetType(Context, PtrEltTy); |
4671 | break; |
4672 | case Attribute::InAlloca: |
4673 | NewAttr = Attribute::getWithInAllocaType(Context, PtrEltTy); |
4674 | break; |
4675 | default: |
4676 | llvm_unreachable("not an upgraded type attribute" ); |
4677 | } |
4678 | |
4679 | Attrs = Attrs.addParamAttribute(Context, i, NewAttr); |
4680 | } |
4681 | } |
4682 | |
4683 | if (CB->isInlineAsm()) { |
4684 | const InlineAsm *IA = cast<InlineAsm>(Val: CB->getCalledOperand()); |
4685 | unsigned ArgNo = 0; |
4686 | for (const InlineAsm::ConstraintInfo &CI : IA->ParseConstraints()) { |
4687 | if (!CI.hasArg()) |
4688 | continue; |
4689 | |
4690 | if (CI.isIndirect && !Attrs.getParamElementType(ArgNo)) { |
4691 | Type *ElemTy = getPtrElementTypeByID(ID: ArgTyIDs[ArgNo]); |
4692 | if (!ElemTy) |
4693 | return error(Message: "Missing element type for inline asm upgrade" ); |
4694 | Attrs = Attrs.addParamAttribute( |
4695 | Context, ArgNo, |
4696 | Attribute::get(Context, Attribute::ElementType, ElemTy)); |
4697 | } |
4698 | |
4699 | ArgNo++; |
4700 | } |
4701 | } |
4702 | |
4703 | switch (CB->getIntrinsicID()) { |
4704 | case Intrinsic::preserve_array_access_index: |
4705 | case Intrinsic::preserve_struct_access_index: |
4706 | case Intrinsic::aarch64_ldaxr: |
4707 | case Intrinsic::aarch64_ldxr: |
4708 | case Intrinsic::aarch64_stlxr: |
4709 | case Intrinsic::aarch64_stxr: |
4710 | case Intrinsic::arm_ldaex: |
4711 | case Intrinsic::arm_ldrex: |
4712 | case Intrinsic::arm_stlex: |
4713 | case Intrinsic::arm_strex: { |
4714 | unsigned ArgNo; |
4715 | switch (CB->getIntrinsicID()) { |
4716 | case Intrinsic::aarch64_stlxr: |
4717 | case Intrinsic::aarch64_stxr: |
4718 | case Intrinsic::arm_stlex: |
4719 | case Intrinsic::arm_strex: |
4720 | ArgNo = 1; |
4721 | break; |
4722 | default: |
4723 | ArgNo = 0; |
4724 | break; |
4725 | } |
4726 | if (!Attrs.getParamElementType(ArgNo)) { |
4727 | Type *ElTy = getPtrElementTypeByID(ID: ArgTyIDs[ArgNo]); |
4728 | if (!ElTy) |
4729 | return error(Message: "Missing element type for elementtype upgrade" ); |
4730 | Attribute NewAttr = Attribute::get(Context, Attribute::ElementType, ElTy); |
4731 | Attrs = Attrs.addParamAttribute(C&: Context, ArgNos: ArgNo, A: NewAttr); |
4732 | } |
4733 | break; |
4734 | } |
4735 | default: |
4736 | break; |
4737 | } |
4738 | |
4739 | CB->setAttributes(Attrs); |
4740 | return Error::success(); |
4741 | } |
4742 | |
4743 | /// Lazily parse the specified function body block. |
4744 | Error BitcodeReader::parseFunctionBody(Function *F) { |
4745 | if (Error Err = Stream.EnterSubBlock(BlockID: bitc::FUNCTION_BLOCK_ID)) |
4746 | return Err; |
4747 | |
4748 | // Unexpected unresolved metadata when parsing function. |
4749 | if (MDLoader->hasFwdRefs()) |
4750 | return error(Message: "Invalid function metadata: incoming forward references" ); |
4751 | |
4752 | InstructionList.clear(); |
4753 | unsigned ModuleValueListSize = ValueList.size(); |
4754 | unsigned ModuleMDLoaderSize = MDLoader->size(); |
4755 | |
4756 | // Add all the function arguments to the value table. |
4757 | unsigned ArgNo = 0; |
4758 | unsigned FTyID = FunctionTypeIDs[F]; |
4759 | for (Argument &I : F->args()) { |
4760 | unsigned ArgTyID = getContainedTypeID(ID: FTyID, Idx: ArgNo + 1); |
4761 | assert(I.getType() == getTypeByID(ArgTyID) && |
4762 | "Incorrect fully specified type for Function Argument" ); |
4763 | ValueList.push_back(V: &I, TypeID: ArgTyID); |
4764 | ++ArgNo; |
4765 | } |
4766 | unsigned NextValueNo = ValueList.size(); |
4767 | BasicBlock *CurBB = nullptr; |
4768 | unsigned CurBBNo = 0; |
4769 | // Block into which constant expressions from phi nodes are materialized. |
4770 | BasicBlock *PhiConstExprBB = nullptr; |
4771 | // Edge blocks for phi nodes into which constant expressions have been |
4772 | // expanded. |
4773 | SmallMapVector<std::pair<BasicBlock *, BasicBlock *>, BasicBlock *, 4> |
4774 | ConstExprEdgeBBs; |
4775 | |
4776 | DebugLoc LastLoc; |
4777 | auto getLastInstruction = [&]() -> Instruction * { |
4778 | if (CurBB && !CurBB->empty()) |
4779 | return &CurBB->back(); |
4780 | else if (CurBBNo && FunctionBBs[CurBBNo - 1] && |
4781 | !FunctionBBs[CurBBNo - 1]->empty()) |
4782 | return &FunctionBBs[CurBBNo - 1]->back(); |
4783 | return nullptr; |
4784 | }; |
4785 | |
4786 | std::vector<OperandBundleDef> OperandBundles; |
4787 | |
4788 | // Read all the records. |
4789 | SmallVector<uint64_t, 64> Record; |
4790 | |
4791 | while (true) { |
4792 | Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); |
4793 | if (!MaybeEntry) |
4794 | return MaybeEntry.takeError(); |
4795 | llvm::BitstreamEntry Entry = MaybeEntry.get(); |
4796 | |
4797 | switch (Entry.Kind) { |
4798 | case BitstreamEntry::Error: |
4799 | return error(Message: "Malformed block" ); |
4800 | case BitstreamEntry::EndBlock: |
4801 | goto OutOfRecordLoop; |
4802 | |
4803 | case BitstreamEntry::SubBlock: |
4804 | switch (Entry.ID) { |
4805 | default: // Skip unknown content. |
4806 | if (Error Err = Stream.SkipBlock()) |
4807 | return Err; |
4808 | break; |
4809 | case bitc::CONSTANTS_BLOCK_ID: |
4810 | if (Error Err = parseConstants()) |
4811 | return Err; |
4812 | NextValueNo = ValueList.size(); |
4813 | break; |
4814 | case bitc::VALUE_SYMTAB_BLOCK_ID: |
4815 | if (Error Err = parseValueSymbolTable()) |
4816 | return Err; |
4817 | break; |
4818 | case bitc::METADATA_ATTACHMENT_ID: |
4819 | if (Error Err = MDLoader->parseMetadataAttachment(F&: *F, InstructionList)) |
4820 | return Err; |
4821 | break; |
4822 | case bitc::METADATA_BLOCK_ID: |
4823 | assert(DeferredMetadataInfo.empty() && |
4824 | "Must read all module-level metadata before function-level" ); |
4825 | if (Error Err = MDLoader->parseFunctionMetadata()) |
4826 | return Err; |
4827 | break; |
4828 | case bitc::USELIST_BLOCK_ID: |
4829 | if (Error Err = parseUseLists()) |
4830 | return Err; |
4831 | break; |
4832 | } |
4833 | continue; |
4834 | |
4835 | case BitstreamEntry::Record: |
4836 | // The interesting case. |
4837 | break; |
4838 | } |
4839 | |
4840 | // Read a record. |
4841 | Record.clear(); |
4842 | Instruction *I = nullptr; |
4843 | unsigned ResTypeID = InvalidTypeID; |
4844 | Expected<unsigned> MaybeBitCode = Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record); |
4845 | if (!MaybeBitCode) |
4846 | return MaybeBitCode.takeError(); |
4847 | switch (unsigned BitCode = MaybeBitCode.get()) { |
4848 | default: // Default behavior: reject |
4849 | return error(Message: "Invalid value" ); |
4850 | case bitc::FUNC_CODE_DECLAREBLOCKS: { // DECLAREBLOCKS: [nblocks] |
4851 | if (Record.empty() || Record[0] == 0) |
4852 | return error(Message: "Invalid record" ); |
4853 | // Create all the basic blocks for the function. |
4854 | FunctionBBs.resize(new_size: Record[0]); |
4855 | |
4856 | // See if anything took the address of blocks in this function. |
4857 | auto BBFRI = BasicBlockFwdRefs.find(Val: F); |
4858 | if (BBFRI == BasicBlockFwdRefs.end()) { |
4859 | for (BasicBlock *&BB : FunctionBBs) |
4860 | BB = BasicBlock::Create(Context, Name: "" , Parent: F); |
4861 | } else { |
4862 | auto &BBRefs = BBFRI->second; |
4863 | // Check for invalid basic block references. |
4864 | if (BBRefs.size() > FunctionBBs.size()) |
4865 | return error(Message: "Invalid ID" ); |
4866 | assert(!BBRefs.empty() && "Unexpected empty array" ); |
4867 | assert(!BBRefs.front() && "Invalid reference to entry block" ); |
4868 | for (unsigned I = 0, E = FunctionBBs.size(), RE = BBRefs.size(); I != E; |
4869 | ++I) |
4870 | if (I < RE && BBRefs[I]) { |
4871 | BBRefs[I]->insertInto(Parent: F); |
4872 | FunctionBBs[I] = BBRefs[I]; |
4873 | } else { |
4874 | FunctionBBs[I] = BasicBlock::Create(Context, Name: "" , Parent: F); |
4875 | } |
4876 | |
4877 | // Erase from the table. |
4878 | BasicBlockFwdRefs.erase(I: BBFRI); |
4879 | } |
4880 | |
4881 | CurBB = FunctionBBs[0]; |
4882 | continue; |
4883 | } |
4884 | |
4885 | case bitc::FUNC_CODE_BLOCKADDR_USERS: // BLOCKADDR_USERS: [vals...] |
4886 | // The record should not be emitted if it's an empty list. |
4887 | if (Record.empty()) |
4888 | return error(Message: "Invalid record" ); |
4889 | // When we have the RARE case of a BlockAddress Constant that is not |
4890 | // scoped to the Function it refers to, we need to conservatively |
4891 | // materialize the referred to Function, regardless of whether or not |
4892 | // that Function will ultimately be linked, otherwise users of |
4893 | // BitcodeReader might start splicing out Function bodies such that we |
4894 | // might no longer be able to materialize the BlockAddress since the |
4895 | // BasicBlock (and entire body of the Function) the BlockAddress refers |
4896 | // to may have been moved. In the case that the user of BitcodeReader |
4897 | // decides ultimately not to link the Function body, materializing here |
4898 | // could be considered wasteful, but it's better than a deserialization |
4899 | // failure as described. This keeps BitcodeReader unaware of complex |
4900 | // linkage policy decisions such as those use by LTO, leaving those |
4901 | // decisions "one layer up." |
4902 | for (uint64_t ValID : Record) |
4903 | if (auto *F = dyn_cast<Function>(Val: ValueList[ValID])) |
4904 | BackwardRefFunctions.push_back(x: F); |
4905 | else |
4906 | return error(Message: "Invalid record" ); |
4907 | |
4908 | continue; |
4909 | |
4910 | case bitc::FUNC_CODE_DEBUG_LOC_AGAIN: // DEBUG_LOC_AGAIN |
4911 | // This record indicates that the last instruction is at the same |
4912 | // location as the previous instruction with a location. |
4913 | I = getLastInstruction(); |
4914 | |
4915 | if (!I) |
4916 | return error(Message: "Invalid record" ); |
4917 | I->setDebugLoc(LastLoc); |
4918 | I = nullptr; |
4919 | continue; |
4920 | |
4921 | case bitc::FUNC_CODE_DEBUG_LOC: { // DEBUG_LOC: [line, col, scope, ia] |
4922 | I = getLastInstruction(); |
4923 | if (!I || Record.size() < 4) |
4924 | return error(Message: "Invalid record" ); |
4925 | |
4926 | unsigned Line = Record[0], Col = Record[1]; |
4927 | unsigned ScopeID = Record[2], IAID = Record[3]; |
4928 | bool isImplicitCode = Record.size() == 5 && Record[4]; |
4929 | |
4930 | MDNode *Scope = nullptr, *IA = nullptr; |
4931 | if (ScopeID) { |
4932 | Scope = dyn_cast_or_null<MDNode>( |
4933 | Val: MDLoader->getMetadataFwdRefOrLoad(Idx: ScopeID - 1)); |
4934 | if (!Scope) |
4935 | return error(Message: "Invalid record" ); |
4936 | } |
4937 | if (IAID) { |
4938 | IA = dyn_cast_or_null<MDNode>( |
4939 | Val: MDLoader->getMetadataFwdRefOrLoad(Idx: IAID - 1)); |
4940 | if (!IA) |
4941 | return error(Message: "Invalid record" ); |
4942 | } |
4943 | LastLoc = DILocation::get(Context&: Scope->getContext(), Line, Column: Col, Scope, InlinedAt: IA, |
4944 | ImplicitCode: isImplicitCode); |
4945 | I->setDebugLoc(LastLoc); |
4946 | I = nullptr; |
4947 | continue; |
4948 | } |
4949 | case bitc::FUNC_CODE_INST_UNOP: { // UNOP: [opval, ty, opcode] |
4950 | unsigned OpNum = 0; |
4951 | Value *LHS; |
4952 | unsigned TypeID; |
4953 | if (getValueTypePair(Record, Slot&: OpNum, InstNum: NextValueNo, ResVal&: LHS, TypeID, ConstExprInsertBB: CurBB) || |
4954 | OpNum+1 > Record.size()) |
4955 | return error(Message: "Invalid record" ); |
4956 | |
4957 | int Opc = getDecodedUnaryOpcode(Val: Record[OpNum++], Ty: LHS->getType()); |
4958 | if (Opc == -1) |
4959 | return error(Message: "Invalid record" ); |
4960 | I = UnaryOperator::Create(Op: (Instruction::UnaryOps)Opc, S: LHS); |
4961 | ResTypeID = TypeID; |
4962 | InstructionList.push_back(Elt: I); |
4963 | if (OpNum < Record.size()) { |
4964 | if (isa<FPMathOperator>(Val: I)) { |
4965 | FastMathFlags FMF = getDecodedFastMathFlags(Val: Record[OpNum]); |
4966 | if (FMF.any()) |
4967 | I->setFastMathFlags(FMF); |
4968 | } |
4969 | } |
4970 | break; |
4971 | } |
4972 | case bitc::FUNC_CODE_INST_BINOP: { // BINOP: [opval, ty, opval, opcode] |
4973 | unsigned OpNum = 0; |
4974 | Value *LHS, *RHS; |
4975 | unsigned TypeID; |
4976 | if (getValueTypePair(Record, Slot&: OpNum, InstNum: NextValueNo, ResVal&: LHS, TypeID, ConstExprInsertBB: CurBB) || |
4977 | popValue(Record, Slot&: OpNum, InstNum: NextValueNo, Ty: LHS->getType(), TyID: TypeID, ResVal&: RHS, |
4978 | ConstExprInsertBB: CurBB) || |
4979 | OpNum+1 > Record.size()) |
4980 | return error(Message: "Invalid record" ); |
4981 | |
4982 | int Opc = getDecodedBinaryOpcode(Val: Record[OpNum++], Ty: LHS->getType()); |
4983 | if (Opc == -1) |
4984 | return error(Message: "Invalid record" ); |
4985 | I = BinaryOperator::Create(Op: (Instruction::BinaryOps)Opc, S1: LHS, S2: RHS); |
4986 | ResTypeID = TypeID; |
4987 | InstructionList.push_back(Elt: I); |
4988 | if (OpNum < Record.size()) { |
4989 | if (Opc == Instruction::Add || |
4990 | Opc == Instruction::Sub || |
4991 | Opc == Instruction::Mul || |
4992 | Opc == Instruction::Shl) { |
4993 | if (Record[OpNum] & (1 << bitc::OBO_NO_SIGNED_WRAP)) |
4994 | cast<BinaryOperator>(Val: I)->setHasNoSignedWrap(true); |
4995 | if (Record[OpNum] & (1 << bitc::OBO_NO_UNSIGNED_WRAP)) |
4996 | cast<BinaryOperator>(Val: I)->setHasNoUnsignedWrap(true); |
4997 | } else if (Opc == Instruction::SDiv || |
4998 | Opc == Instruction::UDiv || |
4999 | Opc == Instruction::LShr || |
5000 | Opc == Instruction::AShr) { |
5001 | if (Record[OpNum] & (1 << bitc::PEO_EXACT)) |
5002 | cast<BinaryOperator>(Val: I)->setIsExact(true); |
5003 | } else if (Opc == Instruction::Or) { |
5004 | if (Record[OpNum] & (1 << bitc::PDI_DISJOINT)) |
5005 | cast<PossiblyDisjointInst>(Val: I)->setIsDisjoint(true); |
5006 | } else if (isa<FPMathOperator>(Val: I)) { |
5007 | FastMathFlags FMF = getDecodedFastMathFlags(Val: Record[OpNum]); |
5008 | if (FMF.any()) |
5009 | I->setFastMathFlags(FMF); |
5010 | } |
5011 | } |
5012 | break; |
5013 | } |
5014 | case bitc::FUNC_CODE_INST_CAST: { // CAST: [opval, opty, destty, castopc] |
5015 | unsigned OpNum = 0; |
5016 | Value *Op; |
5017 | unsigned OpTypeID; |
5018 | if (getValueTypePair(Record, Slot&: OpNum, InstNum: NextValueNo, ResVal&: Op, TypeID&: OpTypeID, ConstExprInsertBB: CurBB) || |
5019 | OpNum + 1 > Record.size()) |
5020 | return error(Message: "Invalid record" ); |
5021 | |
5022 | ResTypeID = Record[OpNum++]; |
5023 | Type *ResTy = getTypeByID(ID: ResTypeID); |
5024 | int Opc = getDecodedCastOpcode(Val: Record[OpNum++]); |
5025 | |
5026 | if (Opc == -1 || !ResTy) |
5027 | return error(Message: "Invalid record" ); |
5028 | Instruction *Temp = nullptr; |
5029 | if ((I = UpgradeBitCastInst(Opc, V: Op, DestTy: ResTy, Temp))) { |
5030 | if (Temp) { |
5031 | InstructionList.push_back(Elt: Temp); |
5032 | assert(CurBB && "No current BB?" ); |
5033 | Temp->insertInto(ParentBB: CurBB, It: CurBB->end()); |
5034 | } |
5035 | } else { |
5036 | auto CastOp = (Instruction::CastOps)Opc; |
5037 | if (!CastInst::castIsValid(op: CastOp, S: Op, DstTy: ResTy)) |
5038 | return error(Message: "Invalid cast" ); |
5039 | I = CastInst::Create(CastOp, S: Op, Ty: ResTy); |
5040 | } |
5041 | |
5042 | if (OpNum < Record.size()) { |
5043 | if (Opc == Instruction::ZExt || Opc == Instruction::UIToFP) { |
5044 | if (Record[OpNum] & (1 << bitc::PNNI_NON_NEG)) |
5045 | cast<PossiblyNonNegInst>(Val: I)->setNonNeg(true); |
5046 | } else if (Opc == Instruction::Trunc) { |
5047 | if (Record[OpNum] & (1 << bitc::TIO_NO_UNSIGNED_WRAP)) |
5048 | cast<TruncInst>(Val: I)->setHasNoUnsignedWrap(true); |
5049 | if (Record[OpNum] & (1 << bitc::TIO_NO_SIGNED_WRAP)) |
5050 | cast<TruncInst>(Val: I)->setHasNoSignedWrap(true); |
5051 | } |
5052 | } |
5053 | |
5054 | InstructionList.push_back(Elt: I); |
5055 | break; |
5056 | } |
5057 | case bitc::FUNC_CODE_INST_INBOUNDS_GEP_OLD: |
5058 | case bitc::FUNC_CODE_INST_GEP_OLD: |
5059 | case bitc::FUNC_CODE_INST_GEP: { // GEP: type, [n x operands] |
5060 | unsigned OpNum = 0; |
5061 | |
5062 | unsigned TyID; |
5063 | Type *Ty; |
5064 | bool InBounds; |
5065 | |
5066 | if (BitCode == bitc::FUNC_CODE_INST_GEP) { |
5067 | InBounds = Record[OpNum++]; |
5068 | TyID = Record[OpNum++]; |
5069 | Ty = getTypeByID(ID: TyID); |
5070 | } else { |
5071 | InBounds = BitCode == bitc::FUNC_CODE_INST_INBOUNDS_GEP_OLD; |
5072 | TyID = InvalidTypeID; |
5073 | Ty = nullptr; |
5074 | } |
5075 | |
5076 | Value *BasePtr; |
5077 | unsigned BasePtrTypeID; |
5078 | if (getValueTypePair(Record, Slot&: OpNum, InstNum: NextValueNo, ResVal&: BasePtr, TypeID&: BasePtrTypeID, |
5079 | ConstExprInsertBB: CurBB)) |
5080 | return error(Message: "Invalid record" ); |
5081 | |
5082 | if (!Ty) { |
5083 | TyID = getContainedTypeID(ID: BasePtrTypeID); |
5084 | if (BasePtr->getType()->isVectorTy()) |
5085 | TyID = getContainedTypeID(ID: TyID); |
5086 | Ty = getTypeByID(ID: TyID); |
5087 | } |
5088 | |
5089 | SmallVector<Value*, 16> GEPIdx; |
5090 | while (OpNum != Record.size()) { |
5091 | Value *Op; |
5092 | unsigned OpTypeID; |
5093 | if (getValueTypePair(Record, Slot&: OpNum, InstNum: NextValueNo, ResVal&: Op, TypeID&: OpTypeID, ConstExprInsertBB: CurBB)) |
5094 | return error(Message: "Invalid record" ); |
5095 | GEPIdx.push_back(Elt: Op); |
5096 | } |
5097 | |
5098 | I = GetElementPtrInst::Create(PointeeType: Ty, Ptr: BasePtr, IdxList: GEPIdx); |
5099 | |
5100 | ResTypeID = TyID; |
5101 | if (cast<GEPOperator>(Val: I)->getNumIndices() != 0) { |
5102 | auto GTI = std::next(x: gep_type_begin(GEP: I)); |
5103 | for (Value *Idx : drop_begin(RangeOrContainer: cast<GEPOperator>(Val: I)->indices())) { |
5104 | unsigned SubType = 0; |
5105 | if (GTI.isStruct()) { |
5106 | ConstantInt *IdxC = |
5107 | Idx->getType()->isVectorTy() |
5108 | ? cast<ConstantInt>(Val: cast<Constant>(Val: Idx)->getSplatValue()) |
5109 | : cast<ConstantInt>(Val: Idx); |
5110 | SubType = IdxC->getZExtValue(); |
5111 | } |
5112 | ResTypeID = getContainedTypeID(ID: ResTypeID, Idx: SubType); |
5113 | ++GTI; |
5114 | } |
5115 | } |
5116 | |
5117 | // At this point ResTypeID is the result element type. We need a pointer |
5118 | // or vector of pointer to it. |
5119 | ResTypeID = getVirtualTypeID(Ty: I->getType()->getScalarType(), ChildTypeIDs: ResTypeID); |
5120 | if (I->getType()->isVectorTy()) |
5121 | ResTypeID = getVirtualTypeID(Ty: I->getType(), ChildTypeIDs: ResTypeID); |
5122 | |
5123 | InstructionList.push_back(Elt: I); |
5124 | if (InBounds) |
5125 | cast<GetElementPtrInst>(Val: I)->setIsInBounds(true); |
5126 | break; |
5127 | } |
5128 | |
5129 | case bitc::FUNC_CODE_INST_EXTRACTVAL: { |
5130 | // EXTRACTVAL: [opty, opval, n x indices] |
5131 | unsigned OpNum = 0; |
5132 | Value *Agg; |
5133 | unsigned AggTypeID; |
5134 | if (getValueTypePair(Record, Slot&: OpNum, InstNum: NextValueNo, ResVal&: Agg, TypeID&: AggTypeID, ConstExprInsertBB: CurBB)) |
5135 | return error(Message: "Invalid record" ); |
5136 | Type *Ty = Agg->getType(); |
5137 | |
5138 | unsigned RecSize = Record.size(); |
5139 | if (OpNum == RecSize) |
5140 | return error(Message: "EXTRACTVAL: Invalid instruction with 0 indices" ); |
5141 | |
5142 | SmallVector<unsigned, 4> ; |
5143 | ResTypeID = AggTypeID; |
5144 | for (; OpNum != RecSize; ++OpNum) { |
5145 | bool IsArray = Ty->isArrayTy(); |
5146 | bool IsStruct = Ty->isStructTy(); |
5147 | uint64_t Index = Record[OpNum]; |
5148 | |
5149 | if (!IsStruct && !IsArray) |
5150 | return error(Message: "EXTRACTVAL: Invalid type" ); |
5151 | if ((unsigned)Index != Index) |
5152 | return error(Message: "Invalid value" ); |
5153 | if (IsStruct && Index >= Ty->getStructNumElements()) |
5154 | return error(Message: "EXTRACTVAL: Invalid struct index" ); |
5155 | if (IsArray && Index >= Ty->getArrayNumElements()) |
5156 | return error(Message: "EXTRACTVAL: Invalid array index" ); |
5157 | EXTRACTVALIdx.push_back(Elt: (unsigned)Index); |
5158 | |
5159 | if (IsStruct) { |
5160 | Ty = Ty->getStructElementType(N: Index); |
5161 | ResTypeID = getContainedTypeID(ID: ResTypeID, Idx: Index); |
5162 | } else { |
5163 | Ty = Ty->getArrayElementType(); |
5164 | ResTypeID = getContainedTypeID(ID: ResTypeID); |
5165 | } |
5166 | } |
5167 | |
5168 | I = ExtractValueInst::Create(Agg, Idxs: EXTRACTVALIdx); |
5169 | InstructionList.push_back(Elt: I); |
5170 | break; |
5171 | } |
5172 | |
5173 | case bitc::FUNC_CODE_INST_INSERTVAL: { |
5174 | // INSERTVAL: [opty, opval, opty, opval, n x indices] |
5175 | unsigned OpNum = 0; |
5176 | Value *Agg; |
5177 | unsigned AggTypeID; |
5178 | if (getValueTypePair(Record, Slot&: OpNum, InstNum: NextValueNo, ResVal&: Agg, TypeID&: AggTypeID, ConstExprInsertBB: CurBB)) |
5179 | return error(Message: "Invalid record" ); |
5180 | Value *Val; |
5181 | unsigned ValTypeID; |
5182 | if (getValueTypePair(Record, Slot&: OpNum, InstNum: NextValueNo, ResVal&: Val, TypeID&: ValTypeID, ConstExprInsertBB: CurBB)) |
5183 | return error(Message: "Invalid record" ); |
5184 | |
5185 | unsigned RecSize = Record.size(); |
5186 | if (OpNum == RecSize) |
5187 | return error(Message: "INSERTVAL: Invalid instruction with 0 indices" ); |
5188 | |
5189 | SmallVector<unsigned, 4> INSERTVALIdx; |
5190 | Type *CurTy = Agg->getType(); |
5191 | for (; OpNum != RecSize; ++OpNum) { |
5192 | bool IsArray = CurTy->isArrayTy(); |
5193 | bool IsStruct = CurTy->isStructTy(); |
5194 | uint64_t Index = Record[OpNum]; |
5195 | |
5196 | if (!IsStruct && !IsArray) |
5197 | return error(Message: "INSERTVAL: Invalid type" ); |
5198 | if ((unsigned)Index != Index) |
5199 | return error(Message: "Invalid value" ); |
5200 | if (IsStruct && Index >= CurTy->getStructNumElements()) |
5201 | return error(Message: "INSERTVAL: Invalid struct index" ); |
5202 | if (IsArray && Index >= CurTy->getArrayNumElements()) |
5203 | return error(Message: "INSERTVAL: Invalid array index" ); |
5204 | |
5205 | INSERTVALIdx.push_back(Elt: (unsigned)Index); |
5206 | if (IsStruct) |
5207 | CurTy = CurTy->getStructElementType(N: Index); |
5208 | else |
5209 | CurTy = CurTy->getArrayElementType(); |
5210 | } |
5211 | |
5212 | if (CurTy != Val->getType()) |
5213 | return error(Message: "Inserted value type doesn't match aggregate type" ); |
5214 | |
5215 | I = InsertValueInst::Create(Agg, Val, Idxs: INSERTVALIdx); |
5216 | ResTypeID = AggTypeID; |
5217 | InstructionList.push_back(Elt: I); |
5218 | break; |
5219 | } |
5220 | |
5221 | case bitc::FUNC_CODE_INST_SELECT: { // SELECT: [opval, ty, opval, opval] |
5222 | // obsolete form of select |
5223 | // handles select i1 ... in old bitcode |
5224 | unsigned OpNum = 0; |
5225 | Value *TrueVal, *FalseVal, *Cond; |
5226 | unsigned TypeID; |
5227 | Type *CondType = Type::getInt1Ty(C&: Context); |
5228 | if (getValueTypePair(Record, Slot&: OpNum, InstNum: NextValueNo, ResVal&: TrueVal, TypeID, |
5229 | ConstExprInsertBB: CurBB) || |
5230 | popValue(Record, Slot&: OpNum, InstNum: NextValueNo, Ty: TrueVal->getType(), TyID: TypeID, |
5231 | ResVal&: FalseVal, ConstExprInsertBB: CurBB) || |
5232 | popValue(Record, Slot&: OpNum, InstNum: NextValueNo, Ty: CondType, |
5233 | TyID: getVirtualTypeID(Ty: CondType), ResVal&: Cond, ConstExprInsertBB: CurBB)) |
5234 | return error(Message: "Invalid record" ); |
5235 | |
5236 | I = SelectInst::Create(C: Cond, S1: TrueVal, S2: FalseVal); |
5237 | ResTypeID = TypeID; |
5238 | InstructionList.push_back(Elt: I); |
5239 | break; |
5240 | } |
5241 | |
5242 | case bitc::FUNC_CODE_INST_VSELECT: {// VSELECT: [ty,opval,opval,predty,pred] |
5243 | // new form of select |
5244 | // handles select i1 or select [N x i1] |
5245 | unsigned OpNum = 0; |
5246 | Value *TrueVal, *FalseVal, *Cond; |
5247 | unsigned ValTypeID, CondTypeID; |
5248 | if (getValueTypePair(Record, Slot&: OpNum, InstNum: NextValueNo, ResVal&: TrueVal, TypeID&: ValTypeID, |
5249 | ConstExprInsertBB: CurBB) || |
5250 | popValue(Record, Slot&: OpNum, InstNum: NextValueNo, Ty: TrueVal->getType(), TyID: ValTypeID, |
5251 | ResVal&: FalseVal, ConstExprInsertBB: CurBB) || |
5252 | getValueTypePair(Record, Slot&: OpNum, InstNum: NextValueNo, ResVal&: Cond, TypeID&: CondTypeID, ConstExprInsertBB: CurBB)) |
5253 | return error(Message: "Invalid record" ); |
5254 | |
5255 | // select condition can be either i1 or [N x i1] |
5256 | if (VectorType* vector_type = |
5257 | dyn_cast<VectorType>(Val: Cond->getType())) { |
5258 | // expect <n x i1> |
5259 | if (vector_type->getElementType() != Type::getInt1Ty(C&: Context)) |
5260 | return error(Message: "Invalid type for value" ); |
5261 | } else { |
5262 | // expect i1 |
5263 | if (Cond->getType() != Type::getInt1Ty(C&: Context)) |
5264 | return error(Message: "Invalid type for value" ); |
5265 | } |
5266 | |
5267 | I = SelectInst::Create(C: Cond, S1: TrueVal, S2: FalseVal); |
5268 | ResTypeID = ValTypeID; |
5269 | InstructionList.push_back(Elt: I); |
5270 | if (OpNum < Record.size() && isa<FPMathOperator>(Val: I)) { |
5271 | FastMathFlags FMF = getDecodedFastMathFlags(Val: Record[OpNum]); |
5272 | if (FMF.any()) |
5273 | I->setFastMathFlags(FMF); |
5274 | } |
5275 | break; |
5276 | } |
5277 | |
5278 | case bitc::FUNC_CODE_INST_EXTRACTELT: { // EXTRACTELT: [opty, opval, opval] |
5279 | unsigned OpNum = 0; |
5280 | Value *Vec, *Idx; |
5281 | unsigned VecTypeID, IdxTypeID; |
5282 | if (getValueTypePair(Record, Slot&: OpNum, InstNum: NextValueNo, ResVal&: Vec, TypeID&: VecTypeID, ConstExprInsertBB: CurBB) || |
5283 | getValueTypePair(Record, Slot&: OpNum, InstNum: NextValueNo, ResVal&: Idx, TypeID&: IdxTypeID, ConstExprInsertBB: CurBB)) |
5284 | return error(Message: "Invalid record" ); |
5285 | if (!Vec->getType()->isVectorTy()) |
5286 | return error(Message: "Invalid type for value" ); |
5287 | I = ExtractElementInst::Create(Vec, Idx); |
5288 | ResTypeID = getContainedTypeID(ID: VecTypeID); |
5289 | InstructionList.push_back(Elt: I); |
5290 | break; |
5291 | } |
5292 | |
5293 | case bitc::FUNC_CODE_INST_INSERTELT: { // INSERTELT: [ty, opval,opval,opval] |
5294 | unsigned OpNum = 0; |
5295 | Value *Vec, *Elt, *Idx; |
5296 | unsigned VecTypeID, IdxTypeID; |
5297 | if (getValueTypePair(Record, Slot&: OpNum, InstNum: NextValueNo, ResVal&: Vec, TypeID&: VecTypeID, ConstExprInsertBB: CurBB)) |
5298 | return error(Message: "Invalid record" ); |
5299 | if (!Vec->getType()->isVectorTy()) |
5300 | return error(Message: "Invalid type for value" ); |
5301 | if (popValue(Record, Slot&: OpNum, InstNum: NextValueNo, |
5302 | Ty: cast<VectorType>(Val: Vec->getType())->getElementType(), |
5303 | TyID: getContainedTypeID(ID: VecTypeID), ResVal&: Elt, ConstExprInsertBB: CurBB) || |
5304 | getValueTypePair(Record, Slot&: OpNum, InstNum: NextValueNo, ResVal&: Idx, TypeID&: IdxTypeID, ConstExprInsertBB: CurBB)) |
5305 | return error(Message: "Invalid record" ); |
5306 | I = InsertElementInst::Create(Vec, NewElt: Elt, Idx); |
5307 | ResTypeID = VecTypeID; |
5308 | InstructionList.push_back(Elt: I); |
5309 | break; |
5310 | } |
5311 | |
5312 | case bitc::FUNC_CODE_INST_SHUFFLEVEC: {// SHUFFLEVEC: [opval,ty,opval,opval] |
5313 | unsigned OpNum = 0; |
5314 | Value *Vec1, *Vec2, *Mask; |
5315 | unsigned Vec1TypeID; |
5316 | if (getValueTypePair(Record, Slot&: OpNum, InstNum: NextValueNo, ResVal&: Vec1, TypeID&: Vec1TypeID, |
5317 | ConstExprInsertBB: CurBB) || |
5318 | popValue(Record, Slot&: OpNum, InstNum: NextValueNo, Ty: Vec1->getType(), TyID: Vec1TypeID, |
5319 | ResVal&: Vec2, ConstExprInsertBB: CurBB)) |
5320 | return error(Message: "Invalid record" ); |
5321 | |
5322 | unsigned MaskTypeID; |
5323 | if (getValueTypePair(Record, Slot&: OpNum, InstNum: NextValueNo, ResVal&: Mask, TypeID&: MaskTypeID, ConstExprInsertBB: CurBB)) |
5324 | return error(Message: "Invalid record" ); |
5325 | if (!Vec1->getType()->isVectorTy() || !Vec2->getType()->isVectorTy()) |
5326 | return error(Message: "Invalid type for value" ); |
5327 | |
5328 | I = new ShuffleVectorInst(Vec1, Vec2, Mask); |
5329 | ResTypeID = |
5330 | getVirtualTypeID(Ty: I->getType(), ChildTypeIDs: getContainedTypeID(ID: Vec1TypeID)); |
5331 | InstructionList.push_back(Elt: I); |
5332 | break; |
5333 | } |
5334 | |
5335 | case bitc::FUNC_CODE_INST_CMP: // CMP: [opty, opval, opval, pred] |
5336 | // Old form of ICmp/FCmp returning bool |
5337 | // Existed to differentiate between icmp/fcmp and vicmp/vfcmp which were |
5338 | // both legal on vectors but had different behaviour. |
5339 | case bitc::FUNC_CODE_INST_CMP2: { // CMP2: [opty, opval, opval, pred] |
5340 | // FCmp/ICmp returning bool or vector of bool |
5341 | |
5342 | unsigned OpNum = 0; |
5343 | Value *LHS, *RHS; |
5344 | unsigned LHSTypeID; |
5345 | if (getValueTypePair(Record, Slot&: OpNum, InstNum: NextValueNo, ResVal&: LHS, TypeID&: LHSTypeID, ConstExprInsertBB: CurBB) || |
5346 | popValue(Record, Slot&: OpNum, InstNum: NextValueNo, Ty: LHS->getType(), TyID: LHSTypeID, ResVal&: RHS, |
5347 | ConstExprInsertBB: CurBB)) |
5348 | return error(Message: "Invalid record" ); |
5349 | |
5350 | if (OpNum >= Record.size()) |
5351 | return error( |
5352 | Message: "Invalid record: operand number exceeded available operands" ); |
5353 | |
5354 | CmpInst::Predicate PredVal = CmpInst::Predicate(Record[OpNum]); |
5355 | bool IsFP = LHS->getType()->isFPOrFPVectorTy(); |
5356 | FastMathFlags FMF; |
5357 | if (IsFP && Record.size() > OpNum+1) |
5358 | FMF = getDecodedFastMathFlags(Val: Record[++OpNum]); |
5359 | |
5360 | if (OpNum+1 != Record.size()) |
5361 | return error(Message: "Invalid record" ); |
5362 | |
5363 | if (IsFP) { |
5364 | if (!CmpInst::isFPPredicate(P: PredVal)) |
5365 | return error(Message: "Invalid fcmp predicate" ); |
5366 | I = new FCmpInst(PredVal, LHS, RHS); |
5367 | } else { |
5368 | if (!CmpInst::isIntPredicate(P: PredVal)) |
5369 | return error(Message: "Invalid icmp predicate" ); |
5370 | I = new ICmpInst(PredVal, LHS, RHS); |
5371 | } |
5372 | |
5373 | ResTypeID = getVirtualTypeID(Ty: I->getType()->getScalarType()); |
5374 | if (LHS->getType()->isVectorTy()) |
5375 | ResTypeID = getVirtualTypeID(Ty: I->getType(), ChildTypeIDs: ResTypeID); |
5376 | |
5377 | if (FMF.any()) |
5378 | I->setFastMathFlags(FMF); |
5379 | InstructionList.push_back(Elt: I); |
5380 | break; |
5381 | } |
5382 | |
5383 | case bitc::FUNC_CODE_INST_RET: // RET: [opty,opval<optional>] |
5384 | { |
5385 | unsigned Size = Record.size(); |
5386 | if (Size == 0) { |
5387 | I = ReturnInst::Create(C&: Context); |
5388 | InstructionList.push_back(Elt: I); |
5389 | break; |
5390 | } |
5391 | |
5392 | unsigned OpNum = 0; |
5393 | Value *Op = nullptr; |
5394 | unsigned OpTypeID; |
5395 | if (getValueTypePair(Record, Slot&: OpNum, InstNum: NextValueNo, ResVal&: Op, TypeID&: OpTypeID, ConstExprInsertBB: CurBB)) |
5396 | return error(Message: "Invalid record" ); |
5397 | if (OpNum != Record.size()) |
5398 | return error(Message: "Invalid record" ); |
5399 | |
5400 | I = ReturnInst::Create(C&: Context, retVal: Op); |
5401 | InstructionList.push_back(Elt: I); |
5402 | break; |
5403 | } |
5404 | case bitc::FUNC_CODE_INST_BR: { // BR: [bb#, bb#, opval] or [bb#] |
5405 | if (Record.size() != 1 && Record.size() != 3) |
5406 | return error(Message: "Invalid record" ); |
5407 | BasicBlock *TrueDest = getBasicBlock(ID: Record[0]); |
5408 | if (!TrueDest) |
5409 | return error(Message: "Invalid record" ); |
5410 | |
5411 | if (Record.size() == 1) { |
5412 | I = BranchInst::Create(IfTrue: TrueDest); |
5413 | InstructionList.push_back(Elt: I); |
5414 | } |
5415 | else { |
5416 | BasicBlock *FalseDest = getBasicBlock(ID: Record[1]); |
5417 | Type *CondType = Type::getInt1Ty(C&: Context); |
5418 | Value *Cond = getValue(Record, Slot: 2, InstNum: NextValueNo, Ty: CondType, |
5419 | TyID: getVirtualTypeID(Ty: CondType), ConstExprInsertBB: CurBB); |
5420 | if (!FalseDest || !Cond) |
5421 | return error(Message: "Invalid record" ); |
5422 | I = BranchInst::Create(IfTrue: TrueDest, IfFalse: FalseDest, Cond); |
5423 | InstructionList.push_back(Elt: I); |
5424 | } |
5425 | break; |
5426 | } |
5427 | case bitc::FUNC_CODE_INST_CLEANUPRET: { // CLEANUPRET: [val] or [val,bb#] |
5428 | if (Record.size() != 1 && Record.size() != 2) |
5429 | return error(Message: "Invalid record" ); |
5430 | unsigned Idx = 0; |
5431 | Type *TokenTy = Type::getTokenTy(C&: Context); |
5432 | Value *CleanupPad = getValue(Record, Slot: Idx++, InstNum: NextValueNo, Ty: TokenTy, |
5433 | TyID: getVirtualTypeID(Ty: TokenTy), ConstExprInsertBB: CurBB); |
5434 | if (!CleanupPad) |
5435 | return error(Message: "Invalid record" ); |
5436 | BasicBlock *UnwindDest = nullptr; |
5437 | if (Record.size() == 2) { |
5438 | UnwindDest = getBasicBlock(ID: Record[Idx++]); |
5439 | if (!UnwindDest) |
5440 | return error(Message: "Invalid record" ); |
5441 | } |
5442 | |
5443 | I = CleanupReturnInst::Create(CleanupPad, UnwindBB: UnwindDest); |
5444 | InstructionList.push_back(Elt: I); |
5445 | break; |
5446 | } |
5447 | case bitc::FUNC_CODE_INST_CATCHRET: { // CATCHRET: [val,bb#] |
5448 | if (Record.size() != 2) |
5449 | return error(Message: "Invalid record" ); |
5450 | unsigned Idx = 0; |
5451 | Type *TokenTy = Type::getTokenTy(C&: Context); |
5452 | Value *CatchPad = getValue(Record, Slot: Idx++, InstNum: NextValueNo, Ty: TokenTy, |
5453 | TyID: getVirtualTypeID(Ty: TokenTy), ConstExprInsertBB: CurBB); |
5454 | if (!CatchPad) |
5455 | return error(Message: "Invalid record" ); |
5456 | BasicBlock *BB = getBasicBlock(ID: Record[Idx++]); |
5457 | if (!BB) |
5458 | return error(Message: "Invalid record" ); |
5459 | |
5460 | I = CatchReturnInst::Create(CatchPad, BB); |
5461 | InstructionList.push_back(Elt: I); |
5462 | break; |
5463 | } |
5464 | case bitc::FUNC_CODE_INST_CATCHSWITCH: { // CATCHSWITCH: [tok,num,(bb)*,bb?] |
5465 | // We must have, at minimum, the outer scope and the number of arguments. |
5466 | if (Record.size() < 2) |
5467 | return error(Message: "Invalid record" ); |
5468 | |
5469 | unsigned Idx = 0; |
5470 | |
5471 | Type *TokenTy = Type::getTokenTy(C&: Context); |
5472 | Value *ParentPad = getValue(Record, Slot: Idx++, InstNum: NextValueNo, Ty: TokenTy, |
5473 | TyID: getVirtualTypeID(Ty: TokenTy), ConstExprInsertBB: CurBB); |
5474 | if (!ParentPad) |
5475 | return error(Message: "Invalid record" ); |
5476 | |
5477 | unsigned NumHandlers = Record[Idx++]; |
5478 | |
5479 | SmallVector<BasicBlock *, 2> Handlers; |
5480 | for (unsigned Op = 0; Op != NumHandlers; ++Op) { |
5481 | BasicBlock *BB = getBasicBlock(ID: Record[Idx++]); |
5482 | if (!BB) |
5483 | return error(Message: "Invalid record" ); |
5484 | Handlers.push_back(Elt: BB); |
5485 | } |
5486 | |
5487 | BasicBlock *UnwindDest = nullptr; |
5488 | if (Idx + 1 == Record.size()) { |
5489 | UnwindDest = getBasicBlock(ID: Record[Idx++]); |
5490 | if (!UnwindDest) |
5491 | return error(Message: "Invalid record" ); |
5492 | } |
5493 | |
5494 | if (Record.size() != Idx) |
5495 | return error(Message: "Invalid record" ); |
5496 | |
5497 | auto *CatchSwitch = |
5498 | CatchSwitchInst::Create(ParentPad, UnwindDest, NumHandlers); |
5499 | for (BasicBlock *Handler : Handlers) |
5500 | CatchSwitch->addHandler(Dest: Handler); |
5501 | I = CatchSwitch; |
5502 | ResTypeID = getVirtualTypeID(Ty: I->getType()); |
5503 | InstructionList.push_back(Elt: I); |
5504 | break; |
5505 | } |
5506 | case bitc::FUNC_CODE_INST_CATCHPAD: |
5507 | case bitc::FUNC_CODE_INST_CLEANUPPAD: { // [tok,num,(ty,val)*] |
5508 | // We must have, at minimum, the outer scope and the number of arguments. |
5509 | if (Record.size() < 2) |
5510 | return error(Message: "Invalid record" ); |
5511 | |
5512 | unsigned Idx = 0; |
5513 | |
5514 | Type *TokenTy = Type::getTokenTy(C&: Context); |
5515 | Value *ParentPad = getValue(Record, Slot: Idx++, InstNum: NextValueNo, Ty: TokenTy, |
5516 | TyID: getVirtualTypeID(Ty: TokenTy), ConstExprInsertBB: CurBB); |
5517 | if (!ParentPad) |
5518 | return error(Message: "Invald record" ); |
5519 | |
5520 | unsigned NumArgOperands = Record[Idx++]; |
5521 | |
5522 | SmallVector<Value *, 2> Args; |
5523 | for (unsigned Op = 0; Op != NumArgOperands; ++Op) { |
5524 | Value *Val; |
5525 | unsigned ValTypeID; |
5526 | if (getValueTypePair(Record, Slot&: Idx, InstNum: NextValueNo, ResVal&: Val, TypeID&: ValTypeID, ConstExprInsertBB: nullptr)) |
5527 | return error(Message: "Invalid record" ); |
5528 | Args.push_back(Elt: Val); |
5529 | } |
5530 | |
5531 | if (Record.size() != Idx) |
5532 | return error(Message: "Invalid record" ); |
5533 | |
5534 | if (BitCode == bitc::FUNC_CODE_INST_CLEANUPPAD) |
5535 | I = CleanupPadInst::Create(ParentPad, Args); |
5536 | else |
5537 | I = CatchPadInst::Create(CatchSwitch: ParentPad, Args); |
5538 | ResTypeID = getVirtualTypeID(Ty: I->getType()); |
5539 | InstructionList.push_back(Elt: I); |
5540 | break; |
5541 | } |
5542 | case bitc::FUNC_CODE_INST_SWITCH: { // SWITCH: [opty, op0, op1, ...] |
5543 | // Check magic |
5544 | if ((Record[0] >> 16) == SWITCH_INST_MAGIC) { |
5545 | // "New" SwitchInst format with case ranges. The changes to write this |
5546 | // format were reverted but we still recognize bitcode that uses it. |
5547 | // Hopefully someday we will have support for case ranges and can use |
5548 | // this format again. |
5549 | |
5550 | unsigned OpTyID = Record[1]; |
5551 | Type *OpTy = getTypeByID(ID: OpTyID); |
5552 | unsigned ValueBitWidth = cast<IntegerType>(Val: OpTy)->getBitWidth(); |
5553 | |
5554 | Value *Cond = getValue(Record, Slot: 2, InstNum: NextValueNo, Ty: OpTy, TyID: OpTyID, ConstExprInsertBB: CurBB); |
5555 | BasicBlock *Default = getBasicBlock(ID: Record[3]); |
5556 | if (!OpTy || !Cond || !Default) |
5557 | return error(Message: "Invalid record" ); |
5558 | |
5559 | unsigned NumCases = Record[4]; |
5560 | |
5561 | SwitchInst *SI = SwitchInst::Create(Value: Cond, Default, NumCases); |
5562 | InstructionList.push_back(Elt: SI); |
5563 | |
5564 | unsigned CurIdx = 5; |
5565 | for (unsigned i = 0; i != NumCases; ++i) { |
5566 | SmallVector<ConstantInt*, 1> CaseVals; |
5567 | unsigned NumItems = Record[CurIdx++]; |
5568 | for (unsigned ci = 0; ci != NumItems; ++ci) { |
5569 | bool isSingleNumber = Record[CurIdx++]; |
5570 | |
5571 | APInt Low; |
5572 | unsigned ActiveWords = 1; |
5573 | if (ValueBitWidth > 64) |
5574 | ActiveWords = Record[CurIdx++]; |
5575 | Low = readWideAPInt(Vals: ArrayRef(&Record[CurIdx], ActiveWords), |
5576 | TypeBits: ValueBitWidth); |
5577 | CurIdx += ActiveWords; |
5578 | |
5579 | if (!isSingleNumber) { |
5580 | ActiveWords = 1; |
5581 | if (ValueBitWidth > 64) |
5582 | ActiveWords = Record[CurIdx++]; |
5583 | APInt High = readWideAPInt(Vals: ArrayRef(&Record[CurIdx], ActiveWords), |
5584 | TypeBits: ValueBitWidth); |
5585 | CurIdx += ActiveWords; |
5586 | |
5587 | // FIXME: It is not clear whether values in the range should be |
5588 | // compared as signed or unsigned values. The partially |
5589 | // implemented changes that used this format in the past used |
5590 | // unsigned comparisons. |
5591 | for ( ; Low.ule(RHS: High); ++Low) |
5592 | CaseVals.push_back(Elt: ConstantInt::get(Context, V: Low)); |
5593 | } else |
5594 | CaseVals.push_back(Elt: ConstantInt::get(Context, V: Low)); |
5595 | } |
5596 | BasicBlock *DestBB = getBasicBlock(ID: Record[CurIdx++]); |
5597 | for (ConstantInt *Cst : CaseVals) |
5598 | SI->addCase(OnVal: Cst, Dest: DestBB); |
5599 | } |
5600 | I = SI; |
5601 | break; |
5602 | } |
5603 | |
5604 | // Old SwitchInst format without case ranges. |
5605 | |
5606 | if (Record.size() < 3 || (Record.size() & 1) == 0) |
5607 | return error(Message: "Invalid record" ); |
5608 | unsigned OpTyID = Record[0]; |
5609 | Type *OpTy = getTypeByID(ID: OpTyID); |
5610 | Value *Cond = getValue(Record, Slot: 1, InstNum: NextValueNo, Ty: OpTy, TyID: OpTyID, ConstExprInsertBB: CurBB); |
5611 | BasicBlock *Default = getBasicBlock(ID: Record[2]); |
5612 | if (!OpTy || !Cond || !Default) |
5613 | return error(Message: "Invalid record" ); |
5614 | unsigned NumCases = (Record.size()-3)/2; |
5615 | SwitchInst *SI = SwitchInst::Create(Value: Cond, Default, NumCases); |
5616 | InstructionList.push_back(Elt: SI); |
5617 | for (unsigned i = 0, e = NumCases; i != e; ++i) { |
5618 | ConstantInt *CaseVal = dyn_cast_or_null<ConstantInt>( |
5619 | Val: getFnValueByID(ID: Record[3+i*2], Ty: OpTy, TyID: OpTyID, ConstExprInsertBB: nullptr)); |
5620 | BasicBlock *DestBB = getBasicBlock(ID: Record[1+3+i*2]); |
5621 | if (!CaseVal || !DestBB) { |
5622 | delete SI; |
5623 | return error(Message: "Invalid record" ); |
5624 | } |
5625 | SI->addCase(OnVal: CaseVal, Dest: DestBB); |
5626 | } |
5627 | I = SI; |
5628 | break; |
5629 | } |
5630 | case bitc::FUNC_CODE_INST_INDIRECTBR: { // INDIRECTBR: [opty, op0, op1, ...] |
5631 | if (Record.size() < 2) |
5632 | return error(Message: "Invalid record" ); |
5633 | unsigned OpTyID = Record[0]; |
5634 | Type *OpTy = getTypeByID(ID: OpTyID); |
5635 | Value *Address = getValue(Record, Slot: 1, InstNum: NextValueNo, Ty: OpTy, TyID: OpTyID, ConstExprInsertBB: CurBB); |
5636 | if (!OpTy || !Address) |
5637 | return error(Message: "Invalid record" ); |
5638 | unsigned NumDests = Record.size()-2; |
5639 | IndirectBrInst *IBI = IndirectBrInst::Create(Address, NumDests); |
5640 | InstructionList.push_back(Elt: IBI); |
5641 | for (unsigned i = 0, e = NumDests; i != e; ++i) { |
5642 | if (BasicBlock *DestBB = getBasicBlock(ID: Record[2+i])) { |
5643 | IBI->addDestination(Dest: DestBB); |
5644 | } else { |
5645 | delete IBI; |
5646 | return error(Message: "Invalid record" ); |
5647 | } |
5648 | } |
5649 | I = IBI; |
5650 | break; |
5651 | } |
5652 | |
5653 | case bitc::FUNC_CODE_INST_INVOKE: { |
5654 | // INVOKE: [attrs, cc, normBB, unwindBB, fnty, op0,op1,op2, ...] |
5655 | if (Record.size() < 4) |
5656 | return error(Message: "Invalid record" ); |
5657 | unsigned OpNum = 0; |
5658 | AttributeList PAL = getAttributes(i: Record[OpNum++]); |
5659 | unsigned CCInfo = Record[OpNum++]; |
5660 | BasicBlock *NormalBB = getBasicBlock(ID: Record[OpNum++]); |
5661 | BasicBlock *UnwindBB = getBasicBlock(ID: Record[OpNum++]); |
5662 | |
5663 | unsigned FTyID = InvalidTypeID; |
5664 | FunctionType *FTy = nullptr; |
5665 | if ((CCInfo >> 13) & 1) { |
5666 | FTyID = Record[OpNum++]; |
5667 | FTy = dyn_cast<FunctionType>(Val: getTypeByID(ID: FTyID)); |
5668 | if (!FTy) |
5669 | return error(Message: "Explicit invoke type is not a function type" ); |
5670 | } |
5671 | |
5672 | Value *Callee; |
5673 | unsigned CalleeTypeID; |
5674 | if (getValueTypePair(Record, Slot&: OpNum, InstNum: NextValueNo, ResVal&: Callee, TypeID&: CalleeTypeID, |
5675 | ConstExprInsertBB: CurBB)) |
5676 | return error(Message: "Invalid record" ); |
5677 | |
5678 | PointerType *CalleeTy = dyn_cast<PointerType>(Val: Callee->getType()); |
5679 | if (!CalleeTy) |
5680 | return error(Message: "Callee is not a pointer" ); |
5681 | if (!FTy) { |
5682 | FTyID = getContainedTypeID(ID: CalleeTypeID); |
5683 | FTy = dyn_cast_or_null<FunctionType>(Val: getTypeByID(ID: FTyID)); |
5684 | if (!FTy) |
5685 | return error(Message: "Callee is not of pointer to function type" ); |
5686 | } |
5687 | if (Record.size() < FTy->getNumParams() + OpNum) |
5688 | return error(Message: "Insufficient operands to call" ); |
5689 | |
5690 | SmallVector<Value*, 16> Ops; |
5691 | SmallVector<unsigned, 16> ArgTyIDs; |
5692 | for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i, ++OpNum) { |
5693 | unsigned ArgTyID = getContainedTypeID(ID: FTyID, Idx: i + 1); |
5694 | Ops.push_back(Elt: getValue(Record, Slot: OpNum, InstNum: NextValueNo, Ty: FTy->getParamType(i), |
5695 | TyID: ArgTyID, ConstExprInsertBB: CurBB)); |
5696 | ArgTyIDs.push_back(Elt: ArgTyID); |
5697 | if (!Ops.back()) |
5698 | return error(Message: "Invalid record" ); |
5699 | } |
5700 | |
5701 | if (!FTy->isVarArg()) { |
5702 | if (Record.size() != OpNum) |
5703 | return error(Message: "Invalid record" ); |
5704 | } else { |
5705 | // Read type/value pairs for varargs params. |
5706 | while (OpNum != Record.size()) { |
5707 | Value *Op; |
5708 | unsigned OpTypeID; |
5709 | if (getValueTypePair(Record, Slot&: OpNum, InstNum: NextValueNo, ResVal&: Op, TypeID&: OpTypeID, ConstExprInsertBB: CurBB)) |
5710 | return error(Message: "Invalid record" ); |
5711 | Ops.push_back(Elt: Op); |
5712 | ArgTyIDs.push_back(Elt: OpTypeID); |
5713 | } |
5714 | } |
5715 | |
5716 | // Upgrade the bundles if needed. |
5717 | if (!OperandBundles.empty()) |
5718 | UpgradeOperandBundles(OperandBundles); |
5719 | |
5720 | I = InvokeInst::Create(Ty: FTy, Func: Callee, IfNormal: NormalBB, IfException: UnwindBB, Args: Ops, |
5721 | Bundles: OperandBundles); |
5722 | ResTypeID = getContainedTypeID(ID: FTyID); |
5723 | OperandBundles.clear(); |
5724 | InstructionList.push_back(Elt: I); |
5725 | cast<InvokeInst>(Val: I)->setCallingConv( |
5726 | static_cast<CallingConv::ID>(CallingConv::MaxID & CCInfo)); |
5727 | cast<InvokeInst>(Val: I)->setAttributes(PAL); |
5728 | if (Error Err = propagateAttributeTypes(CB: cast<CallBase>(Val: I), ArgTyIDs)) { |
5729 | I->deleteValue(); |
5730 | return Err; |
5731 | } |
5732 | |
5733 | break; |
5734 | } |
5735 | case bitc::FUNC_CODE_INST_RESUME: { // RESUME: [opval] |
5736 | unsigned Idx = 0; |
5737 | Value *Val = nullptr; |
5738 | unsigned ValTypeID; |
5739 | if (getValueTypePair(Record, Slot&: Idx, InstNum: NextValueNo, ResVal&: Val, TypeID&: ValTypeID, ConstExprInsertBB: CurBB)) |
5740 | return error(Message: "Invalid record" ); |
5741 | I = ResumeInst::Create(Exn: Val); |
5742 | InstructionList.push_back(Elt: I); |
5743 | break; |
5744 | } |
5745 | case bitc::FUNC_CODE_INST_CALLBR: { |
5746 | // CALLBR: [attr, cc, norm, transfs, fty, fnid, args] |
5747 | unsigned OpNum = 0; |
5748 | AttributeList PAL = getAttributes(i: Record[OpNum++]); |
5749 | unsigned CCInfo = Record[OpNum++]; |
5750 | |
5751 | BasicBlock *DefaultDest = getBasicBlock(ID: Record[OpNum++]); |
5752 | unsigned NumIndirectDests = Record[OpNum++]; |
5753 | SmallVector<BasicBlock *, 16> IndirectDests; |
5754 | for (unsigned i = 0, e = NumIndirectDests; i != e; ++i) |
5755 | IndirectDests.push_back(Elt: getBasicBlock(ID: Record[OpNum++])); |
5756 | |
5757 | unsigned FTyID = InvalidTypeID; |
5758 | FunctionType *FTy = nullptr; |
5759 | if ((CCInfo >> bitc::CALL_EXPLICIT_TYPE) & 1) { |
5760 | FTyID = Record[OpNum++]; |
5761 | FTy = dyn_cast_or_null<FunctionType>(Val: getTypeByID(ID: FTyID)); |
5762 | if (!FTy) |
5763 | return error(Message: "Explicit call type is not a function type" ); |
5764 | } |
5765 | |
5766 | Value *Callee; |
5767 | unsigned CalleeTypeID; |
5768 | if (getValueTypePair(Record, Slot&: OpNum, InstNum: NextValueNo, ResVal&: Callee, TypeID&: CalleeTypeID, |
5769 | ConstExprInsertBB: CurBB)) |
5770 | return error(Message: "Invalid record" ); |
5771 | |
5772 | PointerType *OpTy = dyn_cast<PointerType>(Val: Callee->getType()); |
5773 | if (!OpTy) |
5774 | return error(Message: "Callee is not a pointer type" ); |
5775 | if (!FTy) { |
5776 | FTyID = getContainedTypeID(ID: CalleeTypeID); |
5777 | FTy = dyn_cast_or_null<FunctionType>(Val: getTypeByID(ID: FTyID)); |
5778 | if (!FTy) |
5779 | return error(Message: "Callee is not of pointer to function type" ); |
5780 | } |
5781 | if (Record.size() < FTy->getNumParams() + OpNum) |
5782 | return error(Message: "Insufficient operands to call" ); |
5783 | |
5784 | SmallVector<Value*, 16> Args; |
5785 | SmallVector<unsigned, 16> ArgTyIDs; |
5786 | // Read the fixed params. |
5787 | for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i, ++OpNum) { |
5788 | Value *Arg; |
5789 | unsigned ArgTyID = getContainedTypeID(ID: FTyID, Idx: i + 1); |
5790 | if (FTy->getParamType(i)->isLabelTy()) |
5791 | Arg = getBasicBlock(ID: Record[OpNum]); |
5792 | else |
5793 | Arg = getValue(Record, Slot: OpNum, InstNum: NextValueNo, Ty: FTy->getParamType(i), |
5794 | TyID: ArgTyID, ConstExprInsertBB: CurBB); |
5795 | if (!Arg) |
5796 | return error(Message: "Invalid record" ); |
5797 | Args.push_back(Elt: Arg); |
5798 | ArgTyIDs.push_back(Elt: ArgTyID); |
5799 | } |
5800 | |
5801 | // Read type/value pairs for varargs params. |
5802 | if (!FTy->isVarArg()) { |
5803 | if (OpNum != Record.size()) |
5804 | return error(Message: "Invalid record" ); |
5805 | } else { |
5806 | while (OpNum != Record.size()) { |
5807 | Value *Op; |
5808 | unsigned OpTypeID; |
5809 | if (getValueTypePair(Record, Slot&: OpNum, InstNum: NextValueNo, ResVal&: Op, TypeID&: OpTypeID, ConstExprInsertBB: CurBB)) |
5810 | return error(Message: "Invalid record" ); |
5811 | Args.push_back(Elt: Op); |
5812 | ArgTyIDs.push_back(Elt: OpTypeID); |
5813 | } |
5814 | } |
5815 | |
5816 | // Upgrade the bundles if needed. |
5817 | if (!OperandBundles.empty()) |
5818 | UpgradeOperandBundles(OperandBundles); |
5819 | |
5820 | if (auto *IA = dyn_cast<InlineAsm>(Val: Callee)) { |
5821 | InlineAsm::ConstraintInfoVector ConstraintInfo = IA->ParseConstraints(); |
5822 | auto IsLabelConstraint = [](const InlineAsm::ConstraintInfo &CI) { |
5823 | return CI.Type == InlineAsm::isLabel; |
5824 | }; |
5825 | if (none_of(Range&: ConstraintInfo, P: IsLabelConstraint)) { |
5826 | // Upgrade explicit blockaddress arguments to label constraints. |
5827 | // Verify that the last arguments are blockaddress arguments that |
5828 | // match the indirect destinations. Clang always generates callbr |
5829 | // in this form. We could support reordering with more effort. |
5830 | unsigned FirstBlockArg = Args.size() - IndirectDests.size(); |
5831 | for (unsigned ArgNo = FirstBlockArg; ArgNo < Args.size(); ++ArgNo) { |
5832 | unsigned LabelNo = ArgNo - FirstBlockArg; |
5833 | auto *BA = dyn_cast<BlockAddress>(Val: Args[ArgNo]); |
5834 | if (!BA || BA->getFunction() != F || |
5835 | LabelNo > IndirectDests.size() || |
5836 | BA->getBasicBlock() != IndirectDests[LabelNo]) |
5837 | return error(Message: "callbr argument does not match indirect dest" ); |
5838 | } |
5839 | |
5840 | // Remove blockaddress arguments. |
5841 | Args.erase(CS: Args.begin() + FirstBlockArg, CE: Args.end()); |
5842 | ArgTyIDs.erase(CS: ArgTyIDs.begin() + FirstBlockArg, CE: ArgTyIDs.end()); |
5843 | |
5844 | // Recreate the function type with less arguments. |
5845 | SmallVector<Type *> ArgTys; |
5846 | for (Value *Arg : Args) |
5847 | ArgTys.push_back(Elt: Arg->getType()); |
5848 | FTy = |
5849 | FunctionType::get(Result: FTy->getReturnType(), Params: ArgTys, isVarArg: FTy->isVarArg()); |
5850 | |
5851 | // Update constraint string to use label constraints. |
5852 | std::string Constraints = IA->getConstraintString(); |
5853 | unsigned ArgNo = 0; |
5854 | size_t Pos = 0; |
5855 | for (const auto &CI : ConstraintInfo) { |
5856 | if (CI.hasArg()) { |
5857 | if (ArgNo >= FirstBlockArg) |
5858 | Constraints.insert(pos: Pos, s: "!" ); |
5859 | ++ArgNo; |
5860 | } |
5861 | |
5862 | // Go to next constraint in string. |
5863 | Pos = Constraints.find(c: ',', pos: Pos); |
5864 | if (Pos == std::string::npos) |
5865 | break; |
5866 | ++Pos; |
5867 | } |
5868 | |
5869 | Callee = InlineAsm::get(Ty: FTy, AsmString: IA->getAsmString(), Constraints, |
5870 | hasSideEffects: IA->hasSideEffects(), isAlignStack: IA->isAlignStack(), |
5871 | asmDialect: IA->getDialect(), canThrow: IA->canThrow()); |
5872 | } |
5873 | } |
5874 | |
5875 | I = CallBrInst::Create(Ty: FTy, Func: Callee, DefaultDest, IndirectDests, Args, |
5876 | Bundles: OperandBundles); |
5877 | ResTypeID = getContainedTypeID(ID: FTyID); |
5878 | OperandBundles.clear(); |
5879 | InstructionList.push_back(Elt: I); |
5880 | cast<CallBrInst>(Val: I)->setCallingConv( |
5881 | static_cast<CallingConv::ID>((0x7ff & CCInfo) >> bitc::CALL_CCONV)); |
5882 | cast<CallBrInst>(Val: I)->setAttributes(PAL); |
5883 | if (Error Err = propagateAttributeTypes(CB: cast<CallBase>(Val: I), ArgTyIDs)) { |
5884 | I->deleteValue(); |
5885 | return Err; |
5886 | } |
5887 | break; |
5888 | } |
5889 | case bitc::FUNC_CODE_INST_UNREACHABLE: // UNREACHABLE |
5890 | I = new UnreachableInst(Context); |
5891 | InstructionList.push_back(Elt: I); |
5892 | break; |
5893 | case bitc::FUNC_CODE_INST_PHI: { // PHI: [ty, val0,bb0, ...] |
5894 | if (Record.empty()) |
5895 | return error(Message: "Invalid phi record" ); |
5896 | // The first record specifies the type. |
5897 | unsigned TyID = Record[0]; |
5898 | Type *Ty = getTypeByID(ID: TyID); |
5899 | if (!Ty) |
5900 | return error(Message: "Invalid phi record" ); |
5901 | |
5902 | // Phi arguments are pairs of records of [value, basic block]. |
5903 | // There is an optional final record for fast-math-flags if this phi has a |
5904 | // floating-point type. |
5905 | size_t NumArgs = (Record.size() - 1) / 2; |
5906 | PHINode *PN = PHINode::Create(Ty, NumReservedValues: NumArgs); |
5907 | if ((Record.size() - 1) % 2 == 1 && !isa<FPMathOperator>(Val: PN)) { |
5908 | PN->deleteValue(); |
5909 | return error(Message: "Invalid phi record" ); |
5910 | } |
5911 | InstructionList.push_back(Elt: PN); |
5912 | |
5913 | SmallDenseMap<BasicBlock *, Value *> Args; |
5914 | for (unsigned i = 0; i != NumArgs; i++) { |
5915 | BasicBlock *BB = getBasicBlock(ID: Record[i * 2 + 2]); |
5916 | if (!BB) { |
5917 | PN->deleteValue(); |
5918 | return error(Message: "Invalid phi BB" ); |
5919 | } |
5920 | |
5921 | // Phi nodes may contain the same predecessor multiple times, in which |
5922 | // case the incoming value must be identical. Directly reuse the already |
5923 | // seen value here, to avoid expanding a constant expression multiple |
5924 | // times. |
5925 | auto It = Args.find(Val: BB); |
5926 | if (It != Args.end()) { |
5927 | PN->addIncoming(V: It->second, BB); |
5928 | continue; |
5929 | } |
5930 | |
5931 | // If there already is a block for this edge (from a different phi), |
5932 | // use it. |
5933 | BasicBlock *EdgeBB = ConstExprEdgeBBs.lookup(Key: {BB, CurBB}); |
5934 | if (!EdgeBB) { |
5935 | // Otherwise, use a temporary block (that we will discard if it |
5936 | // turns out to be unnecessary). |
5937 | if (!PhiConstExprBB) |
5938 | PhiConstExprBB = BasicBlock::Create(Context, Name: "phi.constexpr" , Parent: F); |
5939 | EdgeBB = PhiConstExprBB; |
5940 | } |
5941 | |
5942 | // With the new function encoding, it is possible that operands have |
5943 | // negative IDs (for forward references). Use a signed VBR |
5944 | // representation to keep the encoding small. |
5945 | Value *V; |
5946 | if (UseRelativeIDs) |
5947 | V = getValueSigned(Record, Slot: i * 2 + 1, InstNum: NextValueNo, Ty, TyID, ConstExprInsertBB: EdgeBB); |
5948 | else |
5949 | V = getValue(Record, Slot: i * 2 + 1, InstNum: NextValueNo, Ty, TyID, ConstExprInsertBB: EdgeBB); |
5950 | if (!V) { |
5951 | PN->deleteValue(); |
5952 | PhiConstExprBB->eraseFromParent(); |
5953 | return error(Message: "Invalid phi record" ); |
5954 | } |
5955 | |
5956 | if (EdgeBB == PhiConstExprBB && !EdgeBB->empty()) { |
5957 | ConstExprEdgeBBs.insert(KV: {{BB, CurBB}, EdgeBB}); |
5958 | PhiConstExprBB = nullptr; |
5959 | } |
5960 | PN->addIncoming(V, BB); |
5961 | Args.insert(KV: {BB, V}); |
5962 | } |
5963 | I = PN; |
5964 | ResTypeID = TyID; |
5965 | |
5966 | // If there are an even number of records, the final record must be FMF. |
5967 | if (Record.size() % 2 == 0) { |
5968 | assert(isa<FPMathOperator>(I) && "Unexpected phi type" ); |
5969 | FastMathFlags FMF = getDecodedFastMathFlags(Val: Record[Record.size() - 1]); |
5970 | if (FMF.any()) |
5971 | I->setFastMathFlags(FMF); |
5972 | } |
5973 | |
5974 | break; |
5975 | } |
5976 | |
5977 | case bitc::FUNC_CODE_INST_LANDINGPAD: |
5978 | case bitc::FUNC_CODE_INST_LANDINGPAD_OLD: { |
5979 | // LANDINGPAD: [ty, val, val, num, (id0,val0 ...)?] |
5980 | unsigned Idx = 0; |
5981 | if (BitCode == bitc::FUNC_CODE_INST_LANDINGPAD) { |
5982 | if (Record.size() < 3) |
5983 | return error(Message: "Invalid record" ); |
5984 | } else { |
5985 | assert(BitCode == bitc::FUNC_CODE_INST_LANDINGPAD_OLD); |
5986 | if (Record.size() < 4) |
5987 | return error(Message: "Invalid record" ); |
5988 | } |
5989 | ResTypeID = Record[Idx++]; |
5990 | Type *Ty = getTypeByID(ID: ResTypeID); |
5991 | if (!Ty) |
5992 | return error(Message: "Invalid record" ); |
5993 | if (BitCode == bitc::FUNC_CODE_INST_LANDINGPAD_OLD) { |
5994 | Value *PersFn = nullptr; |
5995 | unsigned PersFnTypeID; |
5996 | if (getValueTypePair(Record, Slot&: Idx, InstNum: NextValueNo, ResVal&: PersFn, TypeID&: PersFnTypeID, |
5997 | ConstExprInsertBB: nullptr)) |
5998 | return error(Message: "Invalid record" ); |
5999 | |
6000 | if (!F->hasPersonalityFn()) |
6001 | F->setPersonalityFn(cast<Constant>(Val: PersFn)); |
6002 | else if (F->getPersonalityFn() != cast<Constant>(Val: PersFn)) |
6003 | return error(Message: "Personality function mismatch" ); |
6004 | } |
6005 | |
6006 | bool IsCleanup = !!Record[Idx++]; |
6007 | unsigned NumClauses = Record[Idx++]; |
6008 | LandingPadInst *LP = LandingPadInst::Create(RetTy: Ty, NumReservedClauses: NumClauses); |
6009 | LP->setCleanup(IsCleanup); |
6010 | for (unsigned J = 0; J != NumClauses; ++J) { |
6011 | LandingPadInst::ClauseType CT = |
6012 | LandingPadInst::ClauseType(Record[Idx++]); (void)CT; |
6013 | Value *Val; |
6014 | unsigned ValTypeID; |
6015 | |
6016 | if (getValueTypePair(Record, Slot&: Idx, InstNum: NextValueNo, ResVal&: Val, TypeID&: ValTypeID, |
6017 | ConstExprInsertBB: nullptr)) { |
6018 | delete LP; |
6019 | return error(Message: "Invalid record" ); |
6020 | } |
6021 | |
6022 | assert((CT != LandingPadInst::Catch || |
6023 | !isa<ArrayType>(Val->getType())) && |
6024 | "Catch clause has a invalid type!" ); |
6025 | assert((CT != LandingPadInst::Filter || |
6026 | isa<ArrayType>(Val->getType())) && |
6027 | "Filter clause has invalid type!" ); |
6028 | LP->addClause(ClauseVal: cast<Constant>(Val)); |
6029 | } |
6030 | |
6031 | I = LP; |
6032 | InstructionList.push_back(Elt: I); |
6033 | break; |
6034 | } |
6035 | |
6036 | case bitc::FUNC_CODE_INST_ALLOCA: { // ALLOCA: [instty, opty, op, align] |
6037 | if (Record.size() != 4 && Record.size() != 5) |
6038 | return error(Message: "Invalid record" ); |
6039 | using APV = AllocaPackedValues; |
6040 | const uint64_t Rec = Record[3]; |
6041 | const bool InAlloca = Bitfield::get<APV::UsedWithInAlloca>(Packed: Rec); |
6042 | const bool SwiftError = Bitfield::get<APV::SwiftError>(Packed: Rec); |
6043 | unsigned TyID = Record[0]; |
6044 | Type *Ty = getTypeByID(ID: TyID); |
6045 | if (!Bitfield::get<APV::ExplicitType>(Packed: Rec)) { |
6046 | TyID = getContainedTypeID(ID: TyID); |
6047 | Ty = getTypeByID(ID: TyID); |
6048 | if (!Ty) |
6049 | return error(Message: "Missing element type for old-style alloca" ); |
6050 | } |
6051 | unsigned OpTyID = Record[1]; |
6052 | Type *OpTy = getTypeByID(ID: OpTyID); |
6053 | Value *Size = getFnValueByID(ID: Record[2], Ty: OpTy, TyID: OpTyID, ConstExprInsertBB: CurBB); |
6054 | MaybeAlign Align; |
6055 | uint64_t AlignExp = |
6056 | Bitfield::get<APV::AlignLower>(Packed: Rec) | |
6057 | (Bitfield::get<APV::AlignUpper>(Packed: Rec) << APV::AlignLower::Bits); |
6058 | if (Error Err = parseAlignmentValue(Exponent: AlignExp, Alignment&: Align)) { |
6059 | return Err; |
6060 | } |
6061 | if (!Ty || !Size) |
6062 | return error(Message: "Invalid record" ); |
6063 | |
6064 | const DataLayout &DL = TheModule->getDataLayout(); |
6065 | unsigned AS = Record.size() == 5 ? Record[4] : DL.getAllocaAddrSpace(); |
6066 | |
6067 | SmallPtrSet<Type *, 4> Visited; |
6068 | if (!Align && !Ty->isSized(Visited: &Visited)) |
6069 | return error(Message: "alloca of unsized type" ); |
6070 | if (!Align) |
6071 | Align = DL.getPrefTypeAlign(Ty); |
6072 | |
6073 | if (!Size->getType()->isIntegerTy()) |
6074 | return error(Message: "alloca element count must have integer type" ); |
6075 | |
6076 | AllocaInst *AI = new AllocaInst(Ty, AS, Size, *Align); |
6077 | AI->setUsedWithInAlloca(InAlloca); |
6078 | AI->setSwiftError(SwiftError); |
6079 | I = AI; |
6080 | ResTypeID = getVirtualTypeID(Ty: AI->getType(), ChildTypeIDs: TyID); |
6081 | InstructionList.push_back(Elt: I); |
6082 | break; |
6083 | } |
6084 | case bitc::FUNC_CODE_INST_LOAD: { // LOAD: [opty, op, align, vol] |
6085 | unsigned OpNum = 0; |
6086 | Value *Op; |
6087 | unsigned OpTypeID; |
6088 | if (getValueTypePair(Record, Slot&: OpNum, InstNum: NextValueNo, ResVal&: Op, TypeID&: OpTypeID, ConstExprInsertBB: CurBB) || |
6089 | (OpNum + 2 != Record.size() && OpNum + 3 != Record.size())) |
6090 | return error(Message: "Invalid record" ); |
6091 | |
6092 | if (!isa<PointerType>(Val: Op->getType())) |
6093 | return error(Message: "Load operand is not a pointer type" ); |
6094 | |
6095 | Type *Ty = nullptr; |
6096 | if (OpNum + 3 == Record.size()) { |
6097 | ResTypeID = Record[OpNum++]; |
6098 | Ty = getTypeByID(ID: ResTypeID); |
6099 | } else { |
6100 | ResTypeID = getContainedTypeID(ID: OpTypeID); |
6101 | Ty = getTypeByID(ID: ResTypeID); |
6102 | } |
6103 | |
6104 | if (!Ty) |
6105 | return error(Message: "Missing load type" ); |
6106 | |
6107 | if (Error Err = typeCheckLoadStoreInst(ValType: Ty, PtrType: Op->getType())) |
6108 | return Err; |
6109 | |
6110 | MaybeAlign Align; |
6111 | if (Error Err = parseAlignmentValue(Exponent: Record[OpNum], Alignment&: Align)) |
6112 | return Err; |
6113 | SmallPtrSet<Type *, 4> Visited; |
6114 | if (!Align && !Ty->isSized(Visited: &Visited)) |
6115 | return error(Message: "load of unsized type" ); |
6116 | if (!Align) |
6117 | Align = TheModule->getDataLayout().getABITypeAlign(Ty); |
6118 | I = new LoadInst(Ty, Op, "" , Record[OpNum + 1], *Align); |
6119 | InstructionList.push_back(Elt: I); |
6120 | break; |
6121 | } |
6122 | case bitc::FUNC_CODE_INST_LOADATOMIC: { |
6123 | // LOADATOMIC: [opty, op, align, vol, ordering, ssid] |
6124 | unsigned OpNum = 0; |
6125 | Value *Op; |
6126 | unsigned OpTypeID; |
6127 | if (getValueTypePair(Record, Slot&: OpNum, InstNum: NextValueNo, ResVal&: Op, TypeID&: OpTypeID, ConstExprInsertBB: CurBB) || |
6128 | (OpNum + 4 != Record.size() && OpNum + 5 != Record.size())) |
6129 | return error(Message: "Invalid record" ); |
6130 | |
6131 | if (!isa<PointerType>(Val: Op->getType())) |
6132 | return error(Message: "Load operand is not a pointer type" ); |
6133 | |
6134 | Type *Ty = nullptr; |
6135 | if (OpNum + 5 == Record.size()) { |
6136 | ResTypeID = Record[OpNum++]; |
6137 | Ty = getTypeByID(ID: ResTypeID); |
6138 | } else { |
6139 | ResTypeID = getContainedTypeID(ID: OpTypeID); |
6140 | Ty = getTypeByID(ID: ResTypeID); |
6141 | } |
6142 | |
6143 | if (!Ty) |
6144 | return error(Message: "Missing atomic load type" ); |
6145 | |
6146 | if (Error Err = typeCheckLoadStoreInst(ValType: Ty, PtrType: Op->getType())) |
6147 | return Err; |
6148 | |
6149 | AtomicOrdering Ordering = getDecodedOrdering(Val: Record[OpNum + 2]); |
6150 | if (Ordering == AtomicOrdering::NotAtomic || |
6151 | Ordering == AtomicOrdering::Release || |
6152 | Ordering == AtomicOrdering::AcquireRelease) |
6153 | return error(Message: "Invalid record" ); |
6154 | if (Ordering != AtomicOrdering::NotAtomic && Record[OpNum] == 0) |
6155 | return error(Message: "Invalid record" ); |
6156 | SyncScope::ID SSID = getDecodedSyncScopeID(Val: Record[OpNum + 3]); |
6157 | |
6158 | MaybeAlign Align; |
6159 | if (Error Err = parseAlignmentValue(Exponent: Record[OpNum], Alignment&: Align)) |
6160 | return Err; |
6161 | if (!Align) |
6162 | return error(Message: "Alignment missing from atomic load" ); |
6163 | I = new LoadInst(Ty, Op, "" , Record[OpNum + 1], *Align, Ordering, SSID); |
6164 | InstructionList.push_back(Elt: I); |
6165 | break; |
6166 | } |
6167 | case bitc::FUNC_CODE_INST_STORE: |
6168 | case bitc::FUNC_CODE_INST_STORE_OLD: { // STORE2:[ptrty, ptr, val, align, vol] |
6169 | unsigned OpNum = 0; |
6170 | Value *Val, *Ptr; |
6171 | unsigned PtrTypeID, ValTypeID; |
6172 | if (getValueTypePair(Record, Slot&: OpNum, InstNum: NextValueNo, ResVal&: Ptr, TypeID&: PtrTypeID, ConstExprInsertBB: CurBB)) |
6173 | return error(Message: "Invalid record" ); |
6174 | |
6175 | if (BitCode == bitc::FUNC_CODE_INST_STORE) { |
6176 | if (getValueTypePair(Record, Slot&: OpNum, InstNum: NextValueNo, ResVal&: Val, TypeID&: ValTypeID, ConstExprInsertBB: CurBB)) |
6177 | return error(Message: "Invalid record" ); |
6178 | } else { |
6179 | ValTypeID = getContainedTypeID(ID: PtrTypeID); |
6180 | if (popValue(Record, Slot&: OpNum, InstNum: NextValueNo, Ty: getTypeByID(ID: ValTypeID), |
6181 | TyID: ValTypeID, ResVal&: Val, ConstExprInsertBB: CurBB)) |
6182 | return error(Message: "Invalid record" ); |
6183 | } |
6184 | |
6185 | if (OpNum + 2 != Record.size()) |
6186 | return error(Message: "Invalid record" ); |
6187 | |
6188 | if (Error Err = typeCheckLoadStoreInst(ValType: Val->getType(), PtrType: Ptr->getType())) |
6189 | return Err; |
6190 | MaybeAlign Align; |
6191 | if (Error Err = parseAlignmentValue(Exponent: Record[OpNum], Alignment&: Align)) |
6192 | return Err; |
6193 | SmallPtrSet<Type *, 4> Visited; |
6194 | if (!Align && !Val->getType()->isSized(Visited: &Visited)) |
6195 | return error(Message: "store of unsized type" ); |
6196 | if (!Align) |
6197 | Align = TheModule->getDataLayout().getABITypeAlign(Ty: Val->getType()); |
6198 | I = new StoreInst(Val, Ptr, Record[OpNum + 1], *Align); |
6199 | InstructionList.push_back(Elt: I); |
6200 | break; |
6201 | } |
6202 | case bitc::FUNC_CODE_INST_STOREATOMIC: |
6203 | case bitc::FUNC_CODE_INST_STOREATOMIC_OLD: { |
6204 | // STOREATOMIC: [ptrty, ptr, val, align, vol, ordering, ssid] |
6205 | unsigned OpNum = 0; |
6206 | Value *Val, *Ptr; |
6207 | unsigned PtrTypeID, ValTypeID; |
6208 | if (getValueTypePair(Record, Slot&: OpNum, InstNum: NextValueNo, ResVal&: Ptr, TypeID&: PtrTypeID, ConstExprInsertBB: CurBB) || |
6209 | !isa<PointerType>(Val: Ptr->getType())) |
6210 | return error(Message: "Invalid record" ); |
6211 | if (BitCode == bitc::FUNC_CODE_INST_STOREATOMIC) { |
6212 | if (getValueTypePair(Record, Slot&: OpNum, InstNum: NextValueNo, ResVal&: Val, TypeID&: ValTypeID, ConstExprInsertBB: CurBB)) |
6213 | return error(Message: "Invalid record" ); |
6214 | } else { |
6215 | ValTypeID = getContainedTypeID(ID: PtrTypeID); |
6216 | if (popValue(Record, Slot&: OpNum, InstNum: NextValueNo, Ty: getTypeByID(ID: ValTypeID), |
6217 | TyID: ValTypeID, ResVal&: Val, ConstExprInsertBB: CurBB)) |
6218 | return error(Message: "Invalid record" ); |
6219 | } |
6220 | |
6221 | if (OpNum + 4 != Record.size()) |
6222 | return error(Message: "Invalid record" ); |
6223 | |
6224 | if (Error Err = typeCheckLoadStoreInst(ValType: Val->getType(), PtrType: Ptr->getType())) |
6225 | return Err; |
6226 | AtomicOrdering Ordering = getDecodedOrdering(Val: Record[OpNum + 2]); |
6227 | if (Ordering == AtomicOrdering::NotAtomic || |
6228 | Ordering == AtomicOrdering::Acquire || |
6229 | Ordering == AtomicOrdering::AcquireRelease) |
6230 | return error(Message: "Invalid record" ); |
6231 | SyncScope::ID SSID = getDecodedSyncScopeID(Val: Record[OpNum + 3]); |
6232 | if (Ordering != AtomicOrdering::NotAtomic && Record[OpNum] == 0) |
6233 | return error(Message: "Invalid record" ); |
6234 | |
6235 | MaybeAlign Align; |
6236 | if (Error Err = parseAlignmentValue(Exponent: Record[OpNum], Alignment&: Align)) |
6237 | return Err; |
6238 | if (!Align) |
6239 | return error(Message: "Alignment missing from atomic store" ); |
6240 | I = new StoreInst(Val, Ptr, Record[OpNum + 1], *Align, Ordering, SSID); |
6241 | InstructionList.push_back(Elt: I); |
6242 | break; |
6243 | } |
6244 | case bitc::FUNC_CODE_INST_CMPXCHG_OLD: { |
6245 | // CMPXCHG_OLD: [ptrty, ptr, cmp, val, vol, ordering, synchscope, |
6246 | // failure_ordering?, weak?] |
6247 | const size_t NumRecords = Record.size(); |
6248 | unsigned OpNum = 0; |
6249 | Value *Ptr = nullptr; |
6250 | unsigned PtrTypeID; |
6251 | if (getValueTypePair(Record, Slot&: OpNum, InstNum: NextValueNo, ResVal&: Ptr, TypeID&: PtrTypeID, ConstExprInsertBB: CurBB)) |
6252 | return error(Message: "Invalid record" ); |
6253 | |
6254 | if (!isa<PointerType>(Val: Ptr->getType())) |
6255 | return error(Message: "Cmpxchg operand is not a pointer type" ); |
6256 | |
6257 | Value *Cmp = nullptr; |
6258 | unsigned CmpTypeID = getContainedTypeID(ID: PtrTypeID); |
6259 | if (popValue(Record, Slot&: OpNum, InstNum: NextValueNo, Ty: getTypeByID(ID: CmpTypeID), |
6260 | TyID: CmpTypeID, ResVal&: Cmp, ConstExprInsertBB: CurBB)) |
6261 | return error(Message: "Invalid record" ); |
6262 | |
6263 | Value *New = nullptr; |
6264 | if (popValue(Record, Slot&: OpNum, InstNum: NextValueNo, Ty: Cmp->getType(), TyID: CmpTypeID, |
6265 | ResVal&: New, ConstExprInsertBB: CurBB) || |
6266 | NumRecords < OpNum + 3 || NumRecords > OpNum + 5) |
6267 | return error(Message: "Invalid record" ); |
6268 | |
6269 | const AtomicOrdering SuccessOrdering = |
6270 | getDecodedOrdering(Val: Record[OpNum + 1]); |
6271 | if (SuccessOrdering == AtomicOrdering::NotAtomic || |
6272 | SuccessOrdering == AtomicOrdering::Unordered) |
6273 | return error(Message: "Invalid record" ); |
6274 | |
6275 | const SyncScope::ID SSID = getDecodedSyncScopeID(Val: Record[OpNum + 2]); |
6276 | |
6277 | if (Error Err = typeCheckLoadStoreInst(ValType: Cmp->getType(), PtrType: Ptr->getType())) |
6278 | return Err; |
6279 | |
6280 | const AtomicOrdering FailureOrdering = |
6281 | NumRecords < 7 |
6282 | ? AtomicCmpXchgInst::getStrongestFailureOrdering(SuccessOrdering) |
6283 | : getDecodedOrdering(Val: Record[OpNum + 3]); |
6284 | |
6285 | if (FailureOrdering == AtomicOrdering::NotAtomic || |
6286 | FailureOrdering == AtomicOrdering::Unordered) |
6287 | return error(Message: "Invalid record" ); |
6288 | |
6289 | const Align Alignment( |
6290 | TheModule->getDataLayout().getTypeStoreSize(Ty: Cmp->getType())); |
6291 | |
6292 | I = new AtomicCmpXchgInst(Ptr, Cmp, New, Alignment, SuccessOrdering, |
6293 | FailureOrdering, SSID); |
6294 | cast<AtomicCmpXchgInst>(Val: I)->setVolatile(Record[OpNum]); |
6295 | |
6296 | if (NumRecords < 8) { |
6297 | // Before weak cmpxchgs existed, the instruction simply returned the |
6298 | // value loaded from memory, so bitcode files from that era will be |
6299 | // expecting the first component of a modern cmpxchg. |
6300 | I->insertInto(ParentBB: CurBB, It: CurBB->end()); |
6301 | I = ExtractValueInst::Create(Agg: I, Idxs: 0); |
6302 | ResTypeID = CmpTypeID; |
6303 | } else { |
6304 | cast<AtomicCmpXchgInst>(Val: I)->setWeak(Record[OpNum + 4]); |
6305 | unsigned I1TypeID = getVirtualTypeID(Ty: Type::getInt1Ty(C&: Context)); |
6306 | ResTypeID = getVirtualTypeID(Ty: I->getType(), ChildTypeIDs: {CmpTypeID, I1TypeID}); |
6307 | } |
6308 | |
6309 | InstructionList.push_back(Elt: I); |
6310 | break; |
6311 | } |
6312 | case bitc::FUNC_CODE_INST_CMPXCHG: { |
6313 | // CMPXCHG: [ptrty, ptr, cmp, val, vol, success_ordering, synchscope, |
6314 | // failure_ordering, weak, align?] |
6315 | const size_t NumRecords = Record.size(); |
6316 | unsigned OpNum = 0; |
6317 | Value *Ptr = nullptr; |
6318 | unsigned PtrTypeID; |
6319 | if (getValueTypePair(Record, Slot&: OpNum, InstNum: NextValueNo, ResVal&: Ptr, TypeID&: PtrTypeID, ConstExprInsertBB: CurBB)) |
6320 | return error(Message: "Invalid record" ); |
6321 | |
6322 | if (!isa<PointerType>(Val: Ptr->getType())) |
6323 | return error(Message: "Cmpxchg operand is not a pointer type" ); |
6324 | |
6325 | Value *Cmp = nullptr; |
6326 | unsigned CmpTypeID; |
6327 | if (getValueTypePair(Record, Slot&: OpNum, InstNum: NextValueNo, ResVal&: Cmp, TypeID&: CmpTypeID, ConstExprInsertBB: CurBB)) |
6328 | return error(Message: "Invalid record" ); |
6329 | |
6330 | Value *Val = nullptr; |
6331 | if (popValue(Record, Slot&: OpNum, InstNum: NextValueNo, Ty: Cmp->getType(), TyID: CmpTypeID, ResVal&: Val, |
6332 | ConstExprInsertBB: CurBB)) |
6333 | return error(Message: "Invalid record" ); |
6334 | |
6335 | if (NumRecords < OpNum + 3 || NumRecords > OpNum + 6) |
6336 | return error(Message: "Invalid record" ); |
6337 | |
6338 | const bool IsVol = Record[OpNum]; |
6339 | |
6340 | const AtomicOrdering SuccessOrdering = |
6341 | getDecodedOrdering(Val: Record[OpNum + 1]); |
6342 | if (!AtomicCmpXchgInst::isValidSuccessOrdering(Ordering: SuccessOrdering)) |
6343 | return error(Message: "Invalid cmpxchg success ordering" ); |
6344 | |
6345 | const SyncScope::ID SSID = getDecodedSyncScopeID(Val: Record[OpNum + 2]); |
6346 | |
6347 | if (Error Err = typeCheckLoadStoreInst(ValType: Cmp->getType(), PtrType: Ptr->getType())) |
6348 | return Err; |
6349 | |
6350 | const AtomicOrdering FailureOrdering = |
6351 | getDecodedOrdering(Val: Record[OpNum + 3]); |
6352 | if (!AtomicCmpXchgInst::isValidFailureOrdering(Ordering: FailureOrdering)) |
6353 | return error(Message: "Invalid cmpxchg failure ordering" ); |
6354 | |
6355 | const bool IsWeak = Record[OpNum + 4]; |
6356 | |
6357 | MaybeAlign Alignment; |
6358 | |
6359 | if (NumRecords == (OpNum + 6)) { |
6360 | if (Error Err = parseAlignmentValue(Exponent: Record[OpNum + 5], Alignment)) |
6361 | return Err; |
6362 | } |
6363 | if (!Alignment) |
6364 | Alignment = |
6365 | Align(TheModule->getDataLayout().getTypeStoreSize(Ty: Cmp->getType())); |
6366 | |
6367 | I = new AtomicCmpXchgInst(Ptr, Cmp, Val, *Alignment, SuccessOrdering, |
6368 | FailureOrdering, SSID); |
6369 | cast<AtomicCmpXchgInst>(Val: I)->setVolatile(IsVol); |
6370 | cast<AtomicCmpXchgInst>(Val: I)->setWeak(IsWeak); |
6371 | |
6372 | unsigned I1TypeID = getVirtualTypeID(Ty: Type::getInt1Ty(C&: Context)); |
6373 | ResTypeID = getVirtualTypeID(Ty: I->getType(), ChildTypeIDs: {CmpTypeID, I1TypeID}); |
6374 | |
6375 | InstructionList.push_back(Elt: I); |
6376 | break; |
6377 | } |
6378 | case bitc::FUNC_CODE_INST_ATOMICRMW_OLD: |
6379 | case bitc::FUNC_CODE_INST_ATOMICRMW: { |
6380 | // ATOMICRMW_OLD: [ptrty, ptr, val, op, vol, ordering, ssid, align?] |
6381 | // ATOMICRMW: [ptrty, ptr, valty, val, op, vol, ordering, ssid, align?] |
6382 | const size_t NumRecords = Record.size(); |
6383 | unsigned OpNum = 0; |
6384 | |
6385 | Value *Ptr = nullptr; |
6386 | unsigned PtrTypeID; |
6387 | if (getValueTypePair(Record, Slot&: OpNum, InstNum: NextValueNo, ResVal&: Ptr, TypeID&: PtrTypeID, ConstExprInsertBB: CurBB)) |
6388 | return error(Message: "Invalid record" ); |
6389 | |
6390 | if (!isa<PointerType>(Val: Ptr->getType())) |
6391 | return error(Message: "Invalid record" ); |
6392 | |
6393 | Value *Val = nullptr; |
6394 | unsigned ValTypeID = InvalidTypeID; |
6395 | if (BitCode == bitc::FUNC_CODE_INST_ATOMICRMW_OLD) { |
6396 | ValTypeID = getContainedTypeID(ID: PtrTypeID); |
6397 | if (popValue(Record, Slot&: OpNum, InstNum: NextValueNo, |
6398 | Ty: getTypeByID(ID: ValTypeID), TyID: ValTypeID, ResVal&: Val, ConstExprInsertBB: CurBB)) |
6399 | return error(Message: "Invalid record" ); |
6400 | } else { |
6401 | if (getValueTypePair(Record, Slot&: OpNum, InstNum: NextValueNo, ResVal&: Val, TypeID&: ValTypeID, ConstExprInsertBB: CurBB)) |
6402 | return error(Message: "Invalid record" ); |
6403 | } |
6404 | |
6405 | if (!(NumRecords == (OpNum + 4) || NumRecords == (OpNum + 5))) |
6406 | return error(Message: "Invalid record" ); |
6407 | |
6408 | const AtomicRMWInst::BinOp Operation = |
6409 | getDecodedRMWOperation(Val: Record[OpNum]); |
6410 | if (Operation < AtomicRMWInst::FIRST_BINOP || |
6411 | Operation > AtomicRMWInst::LAST_BINOP) |
6412 | return error(Message: "Invalid record" ); |
6413 | |
6414 | const bool IsVol = Record[OpNum + 1]; |
6415 | |
6416 | const AtomicOrdering Ordering = getDecodedOrdering(Val: Record[OpNum + 2]); |
6417 | if (Ordering == AtomicOrdering::NotAtomic || |
6418 | Ordering == AtomicOrdering::Unordered) |
6419 | return error(Message: "Invalid record" ); |
6420 | |
6421 | const SyncScope::ID SSID = getDecodedSyncScopeID(Val: Record[OpNum + 3]); |
6422 | |
6423 | MaybeAlign Alignment; |
6424 | |
6425 | if (NumRecords == (OpNum + 5)) { |
6426 | if (Error Err = parseAlignmentValue(Exponent: Record[OpNum + 4], Alignment)) |
6427 | return Err; |
6428 | } |
6429 | |
6430 | if (!Alignment) |
6431 | Alignment = |
6432 | Align(TheModule->getDataLayout().getTypeStoreSize(Ty: Val->getType())); |
6433 | |
6434 | I = new AtomicRMWInst(Operation, Ptr, Val, *Alignment, Ordering, SSID); |
6435 | ResTypeID = ValTypeID; |
6436 | cast<AtomicRMWInst>(Val: I)->setVolatile(IsVol); |
6437 | |
6438 | InstructionList.push_back(Elt: I); |
6439 | break; |
6440 | } |
6441 | case bitc::FUNC_CODE_INST_FENCE: { // FENCE:[ordering, ssid] |
6442 | if (2 != Record.size()) |
6443 | return error(Message: "Invalid record" ); |
6444 | AtomicOrdering Ordering = getDecodedOrdering(Val: Record[0]); |
6445 | if (Ordering == AtomicOrdering::NotAtomic || |
6446 | Ordering == AtomicOrdering::Unordered || |
6447 | Ordering == AtomicOrdering::Monotonic) |
6448 | return error(Message: "Invalid record" ); |
6449 | SyncScope::ID SSID = getDecodedSyncScopeID(Val: Record[1]); |
6450 | I = new FenceInst(Context, Ordering, SSID); |
6451 | InstructionList.push_back(Elt: I); |
6452 | break; |
6453 | } |
6454 | case bitc::FUNC_CODE_DEBUG_RECORD_LABEL: { |
6455 | // DbgLabelRecords are placed after the Instructions that they are |
6456 | // attached to. |
6457 | SeenDebugRecord = true; |
6458 | Instruction *Inst = getLastInstruction(); |
6459 | if (!Inst) |
6460 | return error(Message: "Invalid dbg record: missing instruction" ); |
6461 | DILocation *DIL = cast<DILocation>(Val: getFnMetadataByID(ID: Record[0])); |
6462 | DILabel *Label = cast<DILabel>(Val: getFnMetadataByID(ID: Record[1])); |
6463 | Inst->getParent()->insertDbgRecordBefore( |
6464 | DR: new DbgLabelRecord(Label, DebugLoc(DIL)), Here: Inst->getIterator()); |
6465 | continue; // This isn't an instruction. |
6466 | } |
6467 | case bitc::FUNC_CODE_DEBUG_RECORD_VALUE_SIMPLE: |
6468 | case bitc::FUNC_CODE_DEBUG_RECORD_VALUE: |
6469 | case bitc::FUNC_CODE_DEBUG_RECORD_DECLARE: |
6470 | case bitc::FUNC_CODE_DEBUG_RECORD_ASSIGN: { |
6471 | // DbgVariableRecords are placed after the Instructions that they are |
6472 | // attached to. |
6473 | SeenDebugRecord = true; |
6474 | Instruction *Inst = getLastInstruction(); |
6475 | if (!Inst) |
6476 | return error(Message: "Invalid dbg record: missing instruction" ); |
6477 | |
6478 | // First 3 fields are common to all kinds: |
6479 | // DILocation, DILocalVariable, DIExpression |
6480 | // dbg_value (FUNC_CODE_DEBUG_RECORD_VALUE) |
6481 | // ..., LocationMetadata |
6482 | // dbg_value (FUNC_CODE_DEBUG_RECORD_VALUE_SIMPLE - abbrev'd) |
6483 | // ..., Value |
6484 | // dbg_declare (FUNC_CODE_DEBUG_RECORD_DECLARE) |
6485 | // ..., LocationMetadata |
6486 | // dbg_assign (FUNC_CODE_DEBUG_RECORD_ASSIGN) |
6487 | // ..., LocationMetadata, DIAssignID, DIExpression, LocationMetadata |
6488 | unsigned Slot = 0; |
6489 | // Common fields (0-2). |
6490 | DILocation *DIL = cast<DILocation>(Val: getFnMetadataByID(ID: Record[Slot++])); |
6491 | DILocalVariable *Var = |
6492 | cast<DILocalVariable>(Val: getFnMetadataByID(ID: Record[Slot++])); |
6493 | DIExpression *Expr = |
6494 | cast<DIExpression>(Val: getFnMetadataByID(ID: Record[Slot++])); |
6495 | |
6496 | // Union field (3: LocationMetadata | Value). |
6497 | Metadata *RawLocation = nullptr; |
6498 | if (BitCode == bitc::FUNC_CODE_DEBUG_RECORD_VALUE_SIMPLE) { |
6499 | Value *V = nullptr; |
6500 | unsigned TyID = 0; |
6501 | // We never expect to see a fwd reference value here because |
6502 | // use-before-defs are encoded with the standard non-abbrev record |
6503 | // type (they'd require encoding the type too, and they're rare). As a |
6504 | // result, getValueTypePair only ever increments Slot by one here (once |
6505 | // for the value, never twice for value and type). |
6506 | unsigned SlotBefore = Slot; |
6507 | if (getValueTypePair(Record, Slot, InstNum: NextValueNo, ResVal&: V, TypeID&: TyID, ConstExprInsertBB: CurBB)) |
6508 | return error(Message: "Invalid dbg record: invalid value" ); |
6509 | (void)SlotBefore; |
6510 | assert((SlotBefore == Slot - 1) && "unexpected fwd ref" ); |
6511 | RawLocation = ValueAsMetadata::get(V); |
6512 | } else { |
6513 | RawLocation = getFnMetadataByID(ID: Record[Slot++]); |
6514 | } |
6515 | |
6516 | DbgVariableRecord *DVR = nullptr; |
6517 | switch (BitCode) { |
6518 | case bitc::FUNC_CODE_DEBUG_RECORD_VALUE: |
6519 | case bitc::FUNC_CODE_DEBUG_RECORD_VALUE_SIMPLE: |
6520 | DVR = new DbgVariableRecord(RawLocation, Var, Expr, DIL, |
6521 | DbgVariableRecord::LocationType::Value); |
6522 | break; |
6523 | case bitc::FUNC_CODE_DEBUG_RECORD_DECLARE: |
6524 | DVR = new DbgVariableRecord(RawLocation, Var, Expr, DIL, |
6525 | DbgVariableRecord::LocationType::Declare); |
6526 | break; |
6527 | case bitc::FUNC_CODE_DEBUG_RECORD_ASSIGN: { |
6528 | DIAssignID *ID = cast<DIAssignID>(Val: getFnMetadataByID(ID: Record[Slot++])); |
6529 | DIExpression *AddrExpr = |
6530 | cast<DIExpression>(Val: getFnMetadataByID(ID: Record[Slot++])); |
6531 | Metadata *Addr = getFnMetadataByID(ID: Record[Slot++]); |
6532 | DVR = new DbgVariableRecord(RawLocation, Var, Expr, ID, Addr, AddrExpr, |
6533 | DIL); |
6534 | break; |
6535 | } |
6536 | default: |
6537 | llvm_unreachable("Unknown DbgVariableRecord bitcode" ); |
6538 | } |
6539 | Inst->getParent()->insertDbgRecordBefore(DR: DVR, Here: Inst->getIterator()); |
6540 | continue; // This isn't an instruction. |
6541 | } |
6542 | case bitc::FUNC_CODE_INST_CALL: { |
6543 | // CALL: [paramattrs, cc, fmf, fnty, fnid, arg0, arg1...] |
6544 | if (Record.size() < 3) |
6545 | return error(Message: "Invalid record" ); |
6546 | |
6547 | unsigned OpNum = 0; |
6548 | AttributeList PAL = getAttributes(i: Record[OpNum++]); |
6549 | unsigned CCInfo = Record[OpNum++]; |
6550 | |
6551 | FastMathFlags FMF; |
6552 | if ((CCInfo >> bitc::CALL_FMF) & 1) { |
6553 | FMF = getDecodedFastMathFlags(Val: Record[OpNum++]); |
6554 | if (!FMF.any()) |
6555 | return error(Message: "Fast math flags indicator set for call with no FMF" ); |
6556 | } |
6557 | |
6558 | unsigned FTyID = InvalidTypeID; |
6559 | FunctionType *FTy = nullptr; |
6560 | if ((CCInfo >> bitc::CALL_EXPLICIT_TYPE) & 1) { |
6561 | FTyID = Record[OpNum++]; |
6562 | FTy = dyn_cast_or_null<FunctionType>(Val: getTypeByID(ID: FTyID)); |
6563 | if (!FTy) |
6564 | return error(Message: "Explicit call type is not a function type" ); |
6565 | } |
6566 | |
6567 | Value *Callee; |
6568 | unsigned CalleeTypeID; |
6569 | if (getValueTypePair(Record, Slot&: OpNum, InstNum: NextValueNo, ResVal&: Callee, TypeID&: CalleeTypeID, |
6570 | ConstExprInsertBB: CurBB)) |
6571 | return error(Message: "Invalid record" ); |
6572 | |
6573 | PointerType *OpTy = dyn_cast<PointerType>(Val: Callee->getType()); |
6574 | if (!OpTy) |
6575 | return error(Message: "Callee is not a pointer type" ); |
6576 | if (!FTy) { |
6577 | FTyID = getContainedTypeID(ID: CalleeTypeID); |
6578 | FTy = dyn_cast_or_null<FunctionType>(Val: getTypeByID(ID: FTyID)); |
6579 | if (!FTy) |
6580 | return error(Message: "Callee is not of pointer to function type" ); |
6581 | } |
6582 | if (Record.size() < FTy->getNumParams() + OpNum) |
6583 | return error(Message: "Insufficient operands to call" ); |
6584 | |
6585 | SmallVector<Value*, 16> Args; |
6586 | SmallVector<unsigned, 16> ArgTyIDs; |
6587 | // Read the fixed params. |
6588 | for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i, ++OpNum) { |
6589 | unsigned ArgTyID = getContainedTypeID(ID: FTyID, Idx: i + 1); |
6590 | if (FTy->getParamType(i)->isLabelTy()) |
6591 | Args.push_back(Elt: getBasicBlock(ID: Record[OpNum])); |
6592 | else |
6593 | Args.push_back(Elt: getValue(Record, Slot: OpNum, InstNum: NextValueNo, |
6594 | Ty: FTy->getParamType(i), TyID: ArgTyID, ConstExprInsertBB: CurBB)); |
6595 | ArgTyIDs.push_back(Elt: ArgTyID); |
6596 | if (!Args.back()) |
6597 | return error(Message: "Invalid record" ); |
6598 | } |
6599 | |
6600 | // Read type/value pairs for varargs params. |
6601 | if (!FTy->isVarArg()) { |
6602 | if (OpNum != Record.size()) |
6603 | return error(Message: "Invalid record" ); |
6604 | } else { |
6605 | while (OpNum != Record.size()) { |
6606 | Value *Op; |
6607 | unsigned OpTypeID; |
6608 | if (getValueTypePair(Record, Slot&: OpNum, InstNum: NextValueNo, ResVal&: Op, TypeID&: OpTypeID, ConstExprInsertBB: CurBB)) |
6609 | return error(Message: "Invalid record" ); |
6610 | Args.push_back(Elt: Op); |
6611 | ArgTyIDs.push_back(Elt: OpTypeID); |
6612 | } |
6613 | } |
6614 | |
6615 | // Upgrade the bundles if needed. |
6616 | if (!OperandBundles.empty()) |
6617 | UpgradeOperandBundles(OperandBundles); |
6618 | |
6619 | I = CallInst::Create(Ty: FTy, Func: Callee, Args, Bundles: OperandBundles); |
6620 | ResTypeID = getContainedTypeID(ID: FTyID); |
6621 | OperandBundles.clear(); |
6622 | InstructionList.push_back(Elt: I); |
6623 | cast<CallInst>(Val: I)->setCallingConv( |
6624 | static_cast<CallingConv::ID>((0x7ff & CCInfo) >> bitc::CALL_CCONV)); |
6625 | CallInst::TailCallKind TCK = CallInst::TCK_None; |
6626 | if (CCInfo & (1 << bitc::CALL_TAIL)) |
6627 | TCK = CallInst::TCK_Tail; |
6628 | if (CCInfo & (1 << bitc::CALL_MUSTTAIL)) |
6629 | TCK = CallInst::TCK_MustTail; |
6630 | if (CCInfo & (1 << bitc::CALL_NOTAIL)) |
6631 | TCK = CallInst::TCK_NoTail; |
6632 | cast<CallInst>(Val: I)->setTailCallKind(TCK); |
6633 | cast<CallInst>(Val: I)->setAttributes(PAL); |
6634 | if (isa<DbgInfoIntrinsic>(Val: I)) |
6635 | SeenDebugIntrinsic = true; |
6636 | if (Error Err = propagateAttributeTypes(CB: cast<CallBase>(Val: I), ArgTyIDs)) { |
6637 | I->deleteValue(); |
6638 | return Err; |
6639 | } |
6640 | if (FMF.any()) { |
6641 | if (!isa<FPMathOperator>(Val: I)) |
6642 | return error(Message: "Fast-math-flags specified for call without " |
6643 | "floating-point scalar or vector return type" ); |
6644 | I->setFastMathFlags(FMF); |
6645 | } |
6646 | break; |
6647 | } |
6648 | case bitc::FUNC_CODE_INST_VAARG: { // VAARG: [valistty, valist, instty] |
6649 | if (Record.size() < 3) |
6650 | return error(Message: "Invalid record" ); |
6651 | unsigned OpTyID = Record[0]; |
6652 | Type *OpTy = getTypeByID(ID: OpTyID); |
6653 | Value *Op = getValue(Record, Slot: 1, InstNum: NextValueNo, Ty: OpTy, TyID: OpTyID, ConstExprInsertBB: CurBB); |
6654 | ResTypeID = Record[2]; |
6655 | Type *ResTy = getTypeByID(ID: ResTypeID); |
6656 | if (!OpTy || !Op || !ResTy) |
6657 | return error(Message: "Invalid record" ); |
6658 | I = new VAArgInst(Op, ResTy); |
6659 | InstructionList.push_back(Elt: I); |
6660 | break; |
6661 | } |
6662 | |
6663 | case bitc::FUNC_CODE_OPERAND_BUNDLE: { |
6664 | // A call or an invoke can be optionally prefixed with some variable |
6665 | // number of operand bundle blocks. These blocks are read into |
6666 | // OperandBundles and consumed at the next call or invoke instruction. |
6667 | |
6668 | if (Record.empty() || Record[0] >= BundleTags.size()) |
6669 | return error(Message: "Invalid record" ); |
6670 | |
6671 | std::vector<Value *> Inputs; |
6672 | |
6673 | unsigned OpNum = 1; |
6674 | while (OpNum != Record.size()) { |
6675 | Value *Op; |
6676 | unsigned OpTypeID; |
6677 | if (getValueTypePair(Record, Slot&: OpNum, InstNum: NextValueNo, ResVal&: Op, TypeID&: OpTypeID, ConstExprInsertBB: CurBB)) |
6678 | return error(Message: "Invalid record" ); |
6679 | Inputs.push_back(x: Op); |
6680 | } |
6681 | |
6682 | OperandBundles.emplace_back(args&: BundleTags[Record[0]], args: std::move(Inputs)); |
6683 | continue; |
6684 | } |
6685 | |
6686 | case bitc::FUNC_CODE_INST_FREEZE: { // FREEZE: [opty,opval] |
6687 | unsigned OpNum = 0; |
6688 | Value *Op = nullptr; |
6689 | unsigned OpTypeID; |
6690 | if (getValueTypePair(Record, Slot&: OpNum, InstNum: NextValueNo, ResVal&: Op, TypeID&: OpTypeID, ConstExprInsertBB: CurBB)) |
6691 | return error(Message: "Invalid record" ); |
6692 | if (OpNum != Record.size()) |
6693 | return error(Message: "Invalid record" ); |
6694 | |
6695 | I = new FreezeInst(Op); |
6696 | ResTypeID = OpTypeID; |
6697 | InstructionList.push_back(Elt: I); |
6698 | break; |
6699 | } |
6700 | } |
6701 | |
6702 | // Add instruction to end of current BB. If there is no current BB, reject |
6703 | // this file. |
6704 | if (!CurBB) { |
6705 | I->deleteValue(); |
6706 | return error(Message: "Invalid instruction with no BB" ); |
6707 | } |
6708 | if (!OperandBundles.empty()) { |
6709 | I->deleteValue(); |
6710 | return error(Message: "Operand bundles found with no consumer" ); |
6711 | } |
6712 | I->insertInto(ParentBB: CurBB, It: CurBB->end()); |
6713 | |
6714 | // If this was a terminator instruction, move to the next block. |
6715 | if (I->isTerminator()) { |
6716 | ++CurBBNo; |
6717 | CurBB = CurBBNo < FunctionBBs.size() ? FunctionBBs[CurBBNo] : nullptr; |
6718 | } |
6719 | |
6720 | // Non-void values get registered in the value table for future use. |
6721 | if (!I->getType()->isVoidTy()) { |
6722 | assert(I->getType() == getTypeByID(ResTypeID) && |
6723 | "Incorrect result type ID" ); |
6724 | if (Error Err = ValueList.assignValue(Idx: NextValueNo++, V: I, TypeID: ResTypeID)) |
6725 | return Err; |
6726 | } |
6727 | } |
6728 | |
6729 | OutOfRecordLoop: |
6730 | |
6731 | if (!OperandBundles.empty()) |
6732 | return error(Message: "Operand bundles found with no consumer" ); |
6733 | |
6734 | // Check the function list for unresolved values. |
6735 | if (Argument *A = dyn_cast<Argument>(Val: ValueList.back())) { |
6736 | if (!A->getParent()) { |
6737 | // We found at least one unresolved value. Nuke them all to avoid leaks. |
6738 | for (unsigned i = ModuleValueListSize, e = ValueList.size(); i != e; ++i){ |
6739 | if ((A = dyn_cast_or_null<Argument>(Val: ValueList[i])) && !A->getParent()) { |
6740 | A->replaceAllUsesWith(V: PoisonValue::get(T: A->getType())); |
6741 | delete A; |
6742 | } |
6743 | } |
6744 | return error(Message: "Never resolved value found in function" ); |
6745 | } |
6746 | } |
6747 | |
6748 | // Unexpected unresolved metadata about to be dropped. |
6749 | if (MDLoader->hasFwdRefs()) |
6750 | return error(Message: "Invalid function metadata: outgoing forward refs" ); |
6751 | |
6752 | if (PhiConstExprBB) |
6753 | PhiConstExprBB->eraseFromParent(); |
6754 | |
6755 | for (const auto &Pair : ConstExprEdgeBBs) { |
6756 | BasicBlock *From = Pair.first.first; |
6757 | BasicBlock *To = Pair.first.second; |
6758 | BasicBlock *EdgeBB = Pair.second; |
6759 | BranchInst::Create(IfTrue: To, InsertAtEnd: EdgeBB); |
6760 | From->getTerminator()->replaceSuccessorWith(OldBB: To, NewBB: EdgeBB); |
6761 | To->replacePhiUsesWith(Old: From, New: EdgeBB); |
6762 | EdgeBB->moveBefore(MovePos: To); |
6763 | } |
6764 | |
6765 | // Trim the value list down to the size it was before we parsed this function. |
6766 | ValueList.shrinkTo(N: ModuleValueListSize); |
6767 | MDLoader->shrinkTo(N: ModuleMDLoaderSize); |
6768 | std::vector<BasicBlock*>().swap(x&: FunctionBBs); |
6769 | return Error::success(); |
6770 | } |
6771 | |
6772 | /// Find the function body in the bitcode stream |
6773 | Error BitcodeReader::findFunctionInStream( |
6774 | Function *F, |
6775 | DenseMap<Function *, uint64_t>::iterator DeferredFunctionInfoIterator) { |
6776 | while (DeferredFunctionInfoIterator->second == 0) { |
6777 | // This is the fallback handling for the old format bitcode that |
6778 | // didn't contain the function index in the VST, or when we have |
6779 | // an anonymous function which would not have a VST entry. |
6780 | // Assert that we have one of those two cases. |
6781 | assert(VSTOffset == 0 || !F->hasName()); |
6782 | // Parse the next body in the stream and set its position in the |
6783 | // DeferredFunctionInfo map. |
6784 | if (Error Err = rememberAndSkipFunctionBodies()) |
6785 | return Err; |
6786 | } |
6787 | return Error::success(); |
6788 | } |
6789 | |
6790 | SyncScope::ID BitcodeReader::getDecodedSyncScopeID(unsigned Val) { |
6791 | if (Val == SyncScope::SingleThread || Val == SyncScope::System) |
6792 | return SyncScope::ID(Val); |
6793 | if (Val >= SSIDs.size()) |
6794 | return SyncScope::System; // Map unknown synchronization scopes to system. |
6795 | return SSIDs[Val]; |
6796 | } |
6797 | |
6798 | //===----------------------------------------------------------------------===// |
6799 | // GVMaterializer implementation |
6800 | //===----------------------------------------------------------------------===// |
6801 | |
6802 | Error BitcodeReader::materialize(GlobalValue *GV) { |
6803 | Function *F = dyn_cast<Function>(Val: GV); |
6804 | // If it's not a function or is already material, ignore the request. |
6805 | if (!F || !F->isMaterializable()) |
6806 | return Error::success(); |
6807 | |
6808 | DenseMap<Function*, uint64_t>::iterator DFII = DeferredFunctionInfo.find(Val: F); |
6809 | assert(DFII != DeferredFunctionInfo.end() && "Deferred function not found!" ); |
6810 | // If its position is recorded as 0, its body is somewhere in the stream |
6811 | // but we haven't seen it yet. |
6812 | if (DFII->second == 0) |
6813 | if (Error Err = findFunctionInStream(F, DeferredFunctionInfoIterator: DFII)) |
6814 | return Err; |
6815 | |
6816 | // Materialize metadata before parsing any function bodies. |
6817 | if (Error Err = materializeMetadata()) |
6818 | return Err; |
6819 | |
6820 | // Move the bit stream to the saved position of the deferred function body. |
6821 | if (Error JumpFailed = Stream.JumpToBit(BitNo: DFII->second)) |
6822 | return JumpFailed; |
6823 | |
6824 | // Regardless of the debug info format we want to end up in, we need |
6825 | // IsNewDbgInfoFormat=true to construct any debug records seen in the bitcode. |
6826 | F->IsNewDbgInfoFormat = true; |
6827 | |
6828 | if (Error Err = parseFunctionBody(F)) |
6829 | return Err; |
6830 | F->setIsMaterializable(false); |
6831 | |
6832 | // All parsed Functions should load into the debug info format dictated by the |
6833 | // Module, unless we're attempting to preserve the input debug info format. |
6834 | if (SeenDebugIntrinsic && SeenDebugRecord) |
6835 | return error(Message: "Mixed debug intrinsics and debug records in bitcode module!" ); |
6836 | if (PreserveInputDbgFormat == cl::boolOrDefault::BOU_TRUE) { |
6837 | bool SeenAnyDebugInfo = SeenDebugIntrinsic || SeenDebugRecord; |
6838 | bool NewDbgInfoFormatDesired = |
6839 | SeenAnyDebugInfo ? SeenDebugRecord : F->getParent()->IsNewDbgInfoFormat; |
6840 | if (SeenAnyDebugInfo) { |
6841 | UseNewDbgInfoFormat = SeenDebugRecord; |
6842 | WriteNewDbgInfoFormatToBitcode = SeenDebugRecord; |
6843 | WriteNewDbgInfoFormat = SeenDebugRecord; |
6844 | } |
6845 | // If the module's debug info format doesn't match the observed input |
6846 | // format, then set its format now; we don't need to call the conversion |
6847 | // function because there must be no existing intrinsics to convert. |
6848 | // Otherwise, just set the format on this function now. |
6849 | if (NewDbgInfoFormatDesired != F->getParent()->IsNewDbgInfoFormat) |
6850 | F->getParent()->setNewDbgInfoFormatFlag(NewDbgInfoFormatDesired); |
6851 | else |
6852 | F->setNewDbgInfoFormatFlag(NewDbgInfoFormatDesired); |
6853 | } else { |
6854 | // If we aren't preserving formats, we use the Module flag to get our |
6855 | // desired format instead of reading flags, in case we are lazy-loading and |
6856 | // the format of the module has been changed since it was set by the flags. |
6857 | // We only need to convert debug info here if we have debug records but |
6858 | // desire the intrinsic format; everything else is a no-op or handled by the |
6859 | // autoupgrader. |
6860 | bool ModuleIsNewDbgInfoFormat = F->getParent()->IsNewDbgInfoFormat; |
6861 | if (ModuleIsNewDbgInfoFormat || !SeenDebugRecord) |
6862 | F->setNewDbgInfoFormatFlag(ModuleIsNewDbgInfoFormat); |
6863 | else |
6864 | F->setIsNewDbgInfoFormat(ModuleIsNewDbgInfoFormat); |
6865 | } |
6866 | |
6867 | if (StripDebugInfo) |
6868 | stripDebugInfo(F&: *F); |
6869 | |
6870 | // Upgrade any old intrinsic calls in the function. |
6871 | for (auto &I : UpgradedIntrinsics) { |
6872 | for (User *U : llvm::make_early_inc_range(Range: I.first->materialized_users())) |
6873 | if (CallInst *CI = dyn_cast<CallInst>(Val: U)) |
6874 | UpgradeIntrinsicCall(CB: CI, NewFn: I.second); |
6875 | } |
6876 | |
6877 | // Finish fn->subprogram upgrade for materialized functions. |
6878 | if (DISubprogram *SP = MDLoader->lookupSubprogramForFunction(F)) |
6879 | F->setSubprogram(SP); |
6880 | |
6881 | // Check if the TBAA Metadata are valid, otherwise we will need to strip them. |
6882 | if (!MDLoader->isStrippingTBAA()) { |
6883 | for (auto &I : instructions(F)) { |
6884 | MDNode *TBAA = I.getMetadata(KindID: LLVMContext::MD_tbaa); |
6885 | if (!TBAA || TBAAVerifyHelper.visitTBAAMetadata(I, MD: TBAA)) |
6886 | continue; |
6887 | MDLoader->setStripTBAA(true); |
6888 | stripTBAA(M: F->getParent()); |
6889 | } |
6890 | } |
6891 | |
6892 | for (auto &I : instructions(F)) { |
6893 | // "Upgrade" older incorrect branch weights by dropping them. |
6894 | if (auto *MD = I.getMetadata(KindID: LLVMContext::MD_prof)) { |
6895 | if (MD->getOperand(I: 0) != nullptr && isa<MDString>(Val: MD->getOperand(I: 0))) { |
6896 | MDString *MDS = cast<MDString>(Val: MD->getOperand(I: 0)); |
6897 | StringRef ProfName = MDS->getString(); |
6898 | // Check consistency of !prof branch_weights metadata. |
6899 | if (!ProfName.equals(RHS: "branch_weights" )) |
6900 | continue; |
6901 | unsigned ExpectedNumOperands = 0; |
6902 | if (BranchInst *BI = dyn_cast<BranchInst>(Val: &I)) |
6903 | ExpectedNumOperands = BI->getNumSuccessors(); |
6904 | else if (SwitchInst *SI = dyn_cast<SwitchInst>(Val: &I)) |
6905 | ExpectedNumOperands = SI->getNumSuccessors(); |
6906 | else if (isa<CallInst>(Val: &I)) |
6907 | ExpectedNumOperands = 1; |
6908 | else if (IndirectBrInst *IBI = dyn_cast<IndirectBrInst>(Val: &I)) |
6909 | ExpectedNumOperands = IBI->getNumDestinations(); |
6910 | else if (isa<SelectInst>(Val: &I)) |
6911 | ExpectedNumOperands = 2; |
6912 | else |
6913 | continue; // ignore and continue. |
6914 | |
6915 | // If branch weight doesn't match, just strip branch weight. |
6916 | if (MD->getNumOperands() != 1 + ExpectedNumOperands) |
6917 | I.setMetadata(KindID: LLVMContext::MD_prof, Node: nullptr); |
6918 | } |
6919 | } |
6920 | |
6921 | // Remove incompatible attributes on function calls. |
6922 | if (auto *CI = dyn_cast<CallBase>(Val: &I)) { |
6923 | CI->removeRetAttrs(AttrsToRemove: AttributeFuncs::typeIncompatible( |
6924 | Ty: CI->getFunctionType()->getReturnType())); |
6925 | |
6926 | for (unsigned ArgNo = 0; ArgNo < CI->arg_size(); ++ArgNo) |
6927 | CI->removeParamAttrs(ArgNo, AttrsToRemove: AttributeFuncs::typeIncompatible( |
6928 | Ty: CI->getArgOperand(i: ArgNo)->getType())); |
6929 | } |
6930 | } |
6931 | |
6932 | // Look for functions that rely on old function attribute behavior. |
6933 | UpgradeFunctionAttributes(F&: *F); |
6934 | |
6935 | // Bring in any functions that this function forward-referenced via |
6936 | // blockaddresses. |
6937 | return materializeForwardReferencedFunctions(); |
6938 | } |
6939 | |
6940 | Error BitcodeReader::materializeModule() { |
6941 | if (Error Err = materializeMetadata()) |
6942 | return Err; |
6943 | |
6944 | // Promise to materialize all forward references. |
6945 | WillMaterializeAllForwardRefs = true; |
6946 | |
6947 | // Iterate over the module, deserializing any functions that are still on |
6948 | // disk. |
6949 | for (Function &F : *TheModule) { |
6950 | if (Error Err = materialize(GV: &F)) |
6951 | return Err; |
6952 | } |
6953 | // At this point, if there are any function bodies, parse the rest of |
6954 | // the bits in the module past the last function block we have recorded |
6955 | // through either lazy scanning or the VST. |
6956 | if (LastFunctionBlockBit || NextUnreadBit) |
6957 | if (Error Err = parseModule(ResumeBit: LastFunctionBlockBit > NextUnreadBit |
6958 | ? LastFunctionBlockBit |
6959 | : NextUnreadBit)) |
6960 | return Err; |
6961 | |
6962 | // Check that all block address forward references got resolved (as we |
6963 | // promised above). |
6964 | if (!BasicBlockFwdRefs.empty()) |
6965 | return error(Message: "Never resolved function from blockaddress" ); |
6966 | |
6967 | // Upgrade any intrinsic calls that slipped through (should not happen!) and |
6968 | // delete the old functions to clean up. We can't do this unless the entire |
6969 | // module is materialized because there could always be another function body |
6970 | // with calls to the old function. |
6971 | for (auto &I : UpgradedIntrinsics) { |
6972 | for (auto *U : I.first->users()) { |
6973 | if (CallInst *CI = dyn_cast<CallInst>(Val: U)) |
6974 | UpgradeIntrinsicCall(CB: CI, NewFn: I.second); |
6975 | } |
6976 | if (!I.first->use_empty()) |
6977 | I.first->replaceAllUsesWith(V: I.second); |
6978 | I.first->eraseFromParent(); |
6979 | } |
6980 | UpgradedIntrinsics.clear(); |
6981 | |
6982 | UpgradeDebugInfo(M&: *TheModule); |
6983 | |
6984 | UpgradeModuleFlags(M&: *TheModule); |
6985 | |
6986 | UpgradeARCRuntime(M&: *TheModule); |
6987 | |
6988 | return Error::success(); |
6989 | } |
6990 | |
6991 | std::vector<StructType *> BitcodeReader::getIdentifiedStructTypes() const { |
6992 | return IdentifiedStructTypes; |
6993 | } |
6994 | |
6995 | ModuleSummaryIndexBitcodeReader::ModuleSummaryIndexBitcodeReader( |
6996 | BitstreamCursor Cursor, StringRef Strtab, ModuleSummaryIndex &TheIndex, |
6997 | StringRef ModulePath, std::function<bool(GlobalValue::GUID)> IsPrevailing) |
6998 | : BitcodeReaderBase(std::move(Cursor), Strtab), TheIndex(TheIndex), |
6999 | ModulePath(ModulePath), IsPrevailing(IsPrevailing) {} |
7000 | |
7001 | void ModuleSummaryIndexBitcodeReader::addThisModule() { |
7002 | TheIndex.addModule(ModPath: ModulePath); |
7003 | } |
7004 | |
7005 | ModuleSummaryIndex::ModuleInfo * |
7006 | ModuleSummaryIndexBitcodeReader::getThisModule() { |
7007 | return TheIndex.getModule(ModPath: ModulePath); |
7008 | } |
7009 | |
7010 | template <bool AllowNullValueInfo> |
7011 | std::tuple<ValueInfo, GlobalValue::GUID, GlobalValue::GUID> |
7012 | ModuleSummaryIndexBitcodeReader::getValueInfoFromValueId(unsigned ValueId) { |
7013 | auto VGI = ValueIdToValueInfoMap[ValueId]; |
7014 | // We can have a null value info for memprof callsite info records in |
7015 | // distributed ThinLTO index files when the callee function summary is not |
7016 | // included in the index. The bitcode writer records 0 in that case, |
7017 | // and the caller of this helper will set AllowNullValueInfo to true. |
7018 | assert(AllowNullValueInfo || std::get<0>(VGI)); |
7019 | return VGI; |
7020 | } |
7021 | |
7022 | void ModuleSummaryIndexBitcodeReader::setValueGUID( |
7023 | uint64_t ValueID, StringRef ValueName, GlobalValue::LinkageTypes Linkage, |
7024 | StringRef SourceFileName) { |
7025 | std::string GlobalId = |
7026 | GlobalValue::getGlobalIdentifier(Name: ValueName, Linkage, FileName: SourceFileName); |
7027 | auto ValueGUID = GlobalValue::getGUID(GlobalName: GlobalId); |
7028 | auto OriginalNameID = ValueGUID; |
7029 | if (GlobalValue::isLocalLinkage(Linkage)) |
7030 | OriginalNameID = GlobalValue::getGUID(GlobalName: ValueName); |
7031 | if (PrintSummaryGUIDs) |
7032 | dbgs() << "GUID " << ValueGUID << "(" << OriginalNameID << ") is " |
7033 | << ValueName << "\n" ; |
7034 | |
7035 | // UseStrtab is false for legacy summary formats and value names are |
7036 | // created on stack. In that case we save the name in a string saver in |
7037 | // the index so that the value name can be recorded. |
7038 | ValueIdToValueInfoMap[ValueID] = std::make_tuple( |
7039 | args: TheIndex.getOrInsertValueInfo( |
7040 | GUID: ValueGUID, Name: UseStrtab ? ValueName : TheIndex.saveString(String: ValueName)), |
7041 | args&: OriginalNameID, args&: ValueGUID); |
7042 | } |
7043 | |
7044 | // Specialized value symbol table parser used when reading module index |
7045 | // blocks where we don't actually create global values. The parsed information |
7046 | // is saved in the bitcode reader for use when later parsing summaries. |
7047 | Error ModuleSummaryIndexBitcodeReader::parseValueSymbolTable( |
7048 | uint64_t Offset, |
7049 | DenseMap<unsigned, GlobalValue::LinkageTypes> &ValueIdToLinkageMap) { |
7050 | // With a strtab the VST is not required to parse the summary. |
7051 | if (UseStrtab) |
7052 | return Error::success(); |
7053 | |
7054 | assert(Offset > 0 && "Expected non-zero VST offset" ); |
7055 | Expected<uint64_t> MaybeCurrentBit = jumpToValueSymbolTable(Offset, Stream); |
7056 | if (!MaybeCurrentBit) |
7057 | return MaybeCurrentBit.takeError(); |
7058 | uint64_t CurrentBit = MaybeCurrentBit.get(); |
7059 | |
7060 | if (Error Err = Stream.EnterSubBlock(BlockID: bitc::VALUE_SYMTAB_BLOCK_ID)) |
7061 | return Err; |
7062 | |
7063 | SmallVector<uint64_t, 64> Record; |
7064 | |
7065 | // Read all the records for this value table. |
7066 | SmallString<128> ValueName; |
7067 | |
7068 | while (true) { |
7069 | Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks(); |
7070 | if (!MaybeEntry) |
7071 | return MaybeEntry.takeError(); |
7072 | BitstreamEntry Entry = MaybeEntry.get(); |
7073 | |
7074 | switch (Entry.Kind) { |
7075 | case BitstreamEntry::SubBlock: // Handled for us already. |
7076 | case BitstreamEntry::Error: |
7077 | return error(Message: "Malformed block" ); |
7078 | case BitstreamEntry::EndBlock: |
7079 | // Done parsing VST, jump back to wherever we came from. |
7080 | if (Error JumpFailed = Stream.JumpToBit(BitNo: CurrentBit)) |
7081 | return JumpFailed; |
7082 | return Error::success(); |
7083 | case BitstreamEntry::Record: |
7084 | // The interesting case. |
7085 | break; |
7086 | } |
7087 | |
7088 | // Read a record. |
7089 | Record.clear(); |
7090 | Expected<unsigned> MaybeRecord = Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record); |
7091 | if (!MaybeRecord) |
7092 | return MaybeRecord.takeError(); |
7093 | switch (MaybeRecord.get()) { |
7094 | default: // Default behavior: ignore (e.g. VST_CODE_BBENTRY records). |
7095 | break; |
7096 | case bitc::VST_CODE_ENTRY: { // VST_CODE_ENTRY: [valueid, namechar x N] |
7097 | if (convertToString(Record, Idx: 1, Result&: ValueName)) |
7098 | return error(Message: "Invalid record" ); |
7099 | unsigned ValueID = Record[0]; |
7100 | assert(!SourceFileName.empty()); |
7101 | auto VLI = ValueIdToLinkageMap.find(Val: ValueID); |
7102 | assert(VLI != ValueIdToLinkageMap.end() && |
7103 | "No linkage found for VST entry?" ); |
7104 | auto Linkage = VLI->second; |
7105 | setValueGUID(ValueID, ValueName, Linkage, SourceFileName); |
7106 | ValueName.clear(); |
7107 | break; |
7108 | } |
7109 | case bitc::VST_CODE_FNENTRY: { |
7110 | // VST_CODE_FNENTRY: [valueid, offset, namechar x N] |
7111 | if (convertToString(Record, Idx: 2, Result&: ValueName)) |
7112 | return error(Message: "Invalid record" ); |
7113 | unsigned ValueID = Record[0]; |
7114 | assert(!SourceFileName.empty()); |
7115 | auto VLI = ValueIdToLinkageMap.find(Val: ValueID); |
7116 | assert(VLI != ValueIdToLinkageMap.end() && |
7117 | "No linkage found for VST entry?" ); |
7118 | auto Linkage = VLI->second; |
7119 | setValueGUID(ValueID, ValueName, Linkage, SourceFileName); |
7120 | ValueName.clear(); |
7121 | break; |
7122 | } |
7123 | case bitc::VST_CODE_COMBINED_ENTRY: { |
7124 | // VST_CODE_COMBINED_ENTRY: [valueid, refguid] |
7125 | unsigned ValueID = Record[0]; |
7126 | GlobalValue::GUID RefGUID = Record[1]; |
7127 | // The "original name", which is the second value of the pair will be |
7128 | // overriden later by a FS_COMBINED_ORIGINAL_NAME in the combined index. |
7129 | ValueIdToValueInfoMap[ValueID] = std::make_tuple( |
7130 | args: TheIndex.getOrInsertValueInfo(GUID: RefGUID), args&: RefGUID, args&: RefGUID); |
7131 | break; |
7132 | } |
7133 | } |
7134 | } |
7135 | } |
7136 | |
7137 | // Parse just the blocks needed for building the index out of the module. |
7138 | // At the end of this routine the module Index is populated with a map |
7139 | // from global value id to GlobalValueSummary objects. |
7140 | Error ModuleSummaryIndexBitcodeReader::parseModule() { |
7141 | if (Error Err = Stream.EnterSubBlock(BlockID: bitc::MODULE_BLOCK_ID)) |
7142 | return Err; |
7143 | |
7144 | SmallVector<uint64_t, 64> Record; |
7145 | DenseMap<unsigned, GlobalValue::LinkageTypes> ValueIdToLinkageMap; |
7146 | unsigned ValueId = 0; |
7147 | |
7148 | // Read the index for this module. |
7149 | while (true) { |
7150 | Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); |
7151 | if (!MaybeEntry) |
7152 | return MaybeEntry.takeError(); |
7153 | llvm::BitstreamEntry Entry = MaybeEntry.get(); |
7154 | |
7155 | switch (Entry.Kind) { |
7156 | case BitstreamEntry::Error: |
7157 | return error(Message: "Malformed block" ); |
7158 | case BitstreamEntry::EndBlock: |
7159 | return Error::success(); |
7160 | |
7161 | case BitstreamEntry::SubBlock: |
7162 | switch (Entry.ID) { |
7163 | default: // Skip unknown content. |
7164 | if (Error Err = Stream.SkipBlock()) |
7165 | return Err; |
7166 | break; |
7167 | case bitc::BLOCKINFO_BLOCK_ID: |
7168 | // Need to parse these to get abbrev ids (e.g. for VST) |
7169 | if (Error Err = readBlockInfo()) |
7170 | return Err; |
7171 | break; |
7172 | case bitc::VALUE_SYMTAB_BLOCK_ID: |
7173 | // Should have been parsed earlier via VSTOffset, unless there |
7174 | // is no summary section. |
7175 | assert(((SeenValueSymbolTable && VSTOffset > 0) || |
7176 | !SeenGlobalValSummary) && |
7177 | "Expected early VST parse via VSTOffset record" ); |
7178 | if (Error Err = Stream.SkipBlock()) |
7179 | return Err; |
7180 | break; |
7181 | case bitc::GLOBALVAL_SUMMARY_BLOCK_ID: |
7182 | case bitc::FULL_LTO_GLOBALVAL_SUMMARY_BLOCK_ID: |
7183 | // Add the module if it is a per-module index (has a source file name). |
7184 | if (!SourceFileName.empty()) |
7185 | addThisModule(); |
7186 | assert(!SeenValueSymbolTable && |
7187 | "Already read VST when parsing summary block?" ); |
7188 | // We might not have a VST if there were no values in the |
7189 | // summary. An empty summary block generated when we are |
7190 | // performing ThinLTO compiles so we don't later invoke |
7191 | // the regular LTO process on them. |
7192 | if (VSTOffset > 0) { |
7193 | if (Error Err = parseValueSymbolTable(Offset: VSTOffset, ValueIdToLinkageMap)) |
7194 | return Err; |
7195 | SeenValueSymbolTable = true; |
7196 | } |
7197 | SeenGlobalValSummary = true; |
7198 | if (Error Err = parseEntireSummary(ID: Entry.ID)) |
7199 | return Err; |
7200 | break; |
7201 | case bitc::MODULE_STRTAB_BLOCK_ID: |
7202 | if (Error Err = parseModuleStringTable()) |
7203 | return Err; |
7204 | break; |
7205 | } |
7206 | continue; |
7207 | |
7208 | case BitstreamEntry::Record: { |
7209 | Record.clear(); |
7210 | Expected<unsigned> MaybeBitCode = Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record); |
7211 | if (!MaybeBitCode) |
7212 | return MaybeBitCode.takeError(); |
7213 | switch (MaybeBitCode.get()) { |
7214 | default: |
7215 | break; // Default behavior, ignore unknown content. |
7216 | case bitc::MODULE_CODE_VERSION: { |
7217 | if (Error Err = parseVersionRecord(Record).takeError()) |
7218 | return Err; |
7219 | break; |
7220 | } |
7221 | /// MODULE_CODE_SOURCE_FILENAME: [namechar x N] |
7222 | case bitc::MODULE_CODE_SOURCE_FILENAME: { |
7223 | SmallString<128> ValueName; |
7224 | if (convertToString(Record, Idx: 0, Result&: ValueName)) |
7225 | return error(Message: "Invalid record" ); |
7226 | SourceFileName = ValueName.c_str(); |
7227 | break; |
7228 | } |
7229 | /// MODULE_CODE_HASH: [5*i32] |
7230 | case bitc::MODULE_CODE_HASH: { |
7231 | if (Record.size() != 5) |
7232 | return error(Message: "Invalid hash length " + Twine(Record.size()).str()); |
7233 | auto &Hash = getThisModule()->second; |
7234 | int Pos = 0; |
7235 | for (auto &Val : Record) { |
7236 | assert(!(Val >> 32) && "Unexpected high bits set" ); |
7237 | Hash[Pos++] = Val; |
7238 | } |
7239 | break; |
7240 | } |
7241 | /// MODULE_CODE_VSTOFFSET: [offset] |
7242 | case bitc::MODULE_CODE_VSTOFFSET: |
7243 | if (Record.empty()) |
7244 | return error(Message: "Invalid record" ); |
7245 | // Note that we subtract 1 here because the offset is relative to one |
7246 | // word before the start of the identification or module block, which |
7247 | // was historically always the start of the regular bitcode header. |
7248 | VSTOffset = Record[0] - 1; |
7249 | break; |
7250 | // v1 GLOBALVAR: [pointer type, isconst, initid, linkage, ...] |
7251 | // v1 FUNCTION: [type, callingconv, isproto, linkage, ...] |
7252 | // v1 ALIAS: [alias type, addrspace, aliasee val#, linkage, ...] |
7253 | // v2: [strtab offset, strtab size, v1] |
7254 | case bitc::MODULE_CODE_GLOBALVAR: |
7255 | case bitc::MODULE_CODE_FUNCTION: |
7256 | case bitc::MODULE_CODE_ALIAS: { |
7257 | StringRef Name; |
7258 | ArrayRef<uint64_t> GVRecord; |
7259 | std::tie(args&: Name, args&: GVRecord) = readNameFromStrtab(Record); |
7260 | if (GVRecord.size() <= 3) |
7261 | return error(Message: "Invalid record" ); |
7262 | uint64_t RawLinkage = GVRecord[3]; |
7263 | GlobalValue::LinkageTypes Linkage = getDecodedLinkage(Val: RawLinkage); |
7264 | if (!UseStrtab) { |
7265 | ValueIdToLinkageMap[ValueId++] = Linkage; |
7266 | break; |
7267 | } |
7268 | |
7269 | setValueGUID(ValueID: ValueId++, ValueName: Name, Linkage, SourceFileName); |
7270 | break; |
7271 | } |
7272 | } |
7273 | } |
7274 | continue; |
7275 | } |
7276 | } |
7277 | } |
7278 | |
7279 | std::vector<ValueInfo> |
7280 | ModuleSummaryIndexBitcodeReader::makeRefList(ArrayRef<uint64_t> Record) { |
7281 | std::vector<ValueInfo> Ret; |
7282 | Ret.reserve(n: Record.size()); |
7283 | for (uint64_t RefValueId : Record) |
7284 | Ret.push_back(x: std::get<0>(t: getValueInfoFromValueId(ValueId: RefValueId))); |
7285 | return Ret; |
7286 | } |
7287 | |
7288 | std::vector<FunctionSummary::EdgeTy> |
7289 | ModuleSummaryIndexBitcodeReader::makeCallList(ArrayRef<uint64_t> Record, |
7290 | bool IsOldProfileFormat, |
7291 | bool HasProfile, bool HasRelBF) { |
7292 | std::vector<FunctionSummary::EdgeTy> Ret; |
7293 | Ret.reserve(n: Record.size()); |
7294 | for (unsigned I = 0, E = Record.size(); I != E; ++I) { |
7295 | CalleeInfo::HotnessType Hotness = CalleeInfo::HotnessType::Unknown; |
7296 | bool HasTailCall = false; |
7297 | uint64_t RelBF = 0; |
7298 | ValueInfo Callee = std::get<0>(t: getValueInfoFromValueId(ValueId: Record[I])); |
7299 | if (IsOldProfileFormat) { |
7300 | I += 1; // Skip old callsitecount field |
7301 | if (HasProfile) |
7302 | I += 1; // Skip old profilecount field |
7303 | } else if (HasProfile) |
7304 | std::tie(args&: Hotness, args&: HasTailCall) = |
7305 | getDecodedHotnessCallEdgeInfo(RawFlags: Record[++I]); |
7306 | else if (HasRelBF) |
7307 | getDecodedRelBFCallEdgeInfo(RawFlags: Record[++I], RelBF, HasTailCall); |
7308 | Ret.push_back(x: FunctionSummary::EdgeTy{ |
7309 | Callee, CalleeInfo(Hotness, HasTailCall, RelBF)}); |
7310 | } |
7311 | return Ret; |
7312 | } |
7313 | |
7314 | static void |
7315 | parseWholeProgramDevirtResolutionByArg(ArrayRef<uint64_t> Record, size_t &Slot, |
7316 | WholeProgramDevirtResolution &Wpd) { |
7317 | uint64_t ArgNum = Record[Slot++]; |
7318 | WholeProgramDevirtResolution::ByArg &B = |
7319 | Wpd.ResByArg[{Record.begin() + Slot, Record.begin() + Slot + ArgNum}]; |
7320 | Slot += ArgNum; |
7321 | |
7322 | B.TheKind = |
7323 | static_cast<WholeProgramDevirtResolution::ByArg::Kind>(Record[Slot++]); |
7324 | B.Info = Record[Slot++]; |
7325 | B.Byte = Record[Slot++]; |
7326 | B.Bit = Record[Slot++]; |
7327 | } |
7328 | |
7329 | static void parseWholeProgramDevirtResolution(ArrayRef<uint64_t> Record, |
7330 | StringRef Strtab, size_t &Slot, |
7331 | TypeIdSummary &TypeId) { |
7332 | uint64_t Id = Record[Slot++]; |
7333 | WholeProgramDevirtResolution &Wpd = TypeId.WPDRes[Id]; |
7334 | |
7335 | Wpd.TheKind = static_cast<WholeProgramDevirtResolution::Kind>(Record[Slot++]); |
7336 | Wpd.SingleImplName = {Strtab.data() + Record[Slot], |
7337 | static_cast<size_t>(Record[Slot + 1])}; |
7338 | Slot += 2; |
7339 | |
7340 | uint64_t ResByArgNum = Record[Slot++]; |
7341 | for (uint64_t I = 0; I != ResByArgNum; ++I) |
7342 | parseWholeProgramDevirtResolutionByArg(Record, Slot, Wpd); |
7343 | } |
7344 | |
7345 | static void parseTypeIdSummaryRecord(ArrayRef<uint64_t> Record, |
7346 | StringRef Strtab, |
7347 | ModuleSummaryIndex &TheIndex) { |
7348 | size_t Slot = 0; |
7349 | TypeIdSummary &TypeId = TheIndex.getOrInsertTypeIdSummary( |
7350 | TypeId: {Strtab.data() + Record[Slot], static_cast<size_t>(Record[Slot + 1])}); |
7351 | Slot += 2; |
7352 | |
7353 | TypeId.TTRes.TheKind = static_cast<TypeTestResolution::Kind>(Record[Slot++]); |
7354 | TypeId.TTRes.SizeM1BitWidth = Record[Slot++]; |
7355 | TypeId.TTRes.AlignLog2 = Record[Slot++]; |
7356 | TypeId.TTRes.SizeM1 = Record[Slot++]; |
7357 | TypeId.TTRes.BitMask = Record[Slot++]; |
7358 | TypeId.TTRes.InlineBits = Record[Slot++]; |
7359 | |
7360 | while (Slot < Record.size()) |
7361 | parseWholeProgramDevirtResolution(Record, Strtab, Slot, TypeId); |
7362 | } |
7363 | |
7364 | std::vector<FunctionSummary::ParamAccess> |
7365 | ModuleSummaryIndexBitcodeReader::parseParamAccesses(ArrayRef<uint64_t> Record) { |
7366 | auto ReadRange = [&]() { |
7367 | APInt Lower(FunctionSummary::ParamAccess::RangeWidth, |
7368 | BitcodeReader::decodeSignRotatedValue(V: Record.front())); |
7369 | Record = Record.drop_front(); |
7370 | APInt Upper(FunctionSummary::ParamAccess::RangeWidth, |
7371 | BitcodeReader::decodeSignRotatedValue(V: Record.front())); |
7372 | Record = Record.drop_front(); |
7373 | ConstantRange Range{Lower, Upper}; |
7374 | assert(!Range.isFullSet()); |
7375 | assert(!Range.isUpperSignWrapped()); |
7376 | return Range; |
7377 | }; |
7378 | |
7379 | std::vector<FunctionSummary::ParamAccess> PendingParamAccesses; |
7380 | while (!Record.empty()) { |
7381 | PendingParamAccesses.emplace_back(); |
7382 | FunctionSummary::ParamAccess &ParamAccess = PendingParamAccesses.back(); |
7383 | ParamAccess.ParamNo = Record.front(); |
7384 | Record = Record.drop_front(); |
7385 | ParamAccess.Use = ReadRange(); |
7386 | ParamAccess.Calls.resize(new_size: Record.front()); |
7387 | Record = Record.drop_front(); |
7388 | for (auto &Call : ParamAccess.Calls) { |
7389 | Call.ParamNo = Record.front(); |
7390 | Record = Record.drop_front(); |
7391 | Call.Callee = std::get<0>(t: getValueInfoFromValueId(ValueId: Record.front())); |
7392 | Record = Record.drop_front(); |
7393 | Call.Offsets = ReadRange(); |
7394 | } |
7395 | } |
7396 | return PendingParamAccesses; |
7397 | } |
7398 | |
7399 | void ModuleSummaryIndexBitcodeReader::parseTypeIdCompatibleVtableInfo( |
7400 | ArrayRef<uint64_t> Record, size_t &Slot, |
7401 | TypeIdCompatibleVtableInfo &TypeId) { |
7402 | uint64_t Offset = Record[Slot++]; |
7403 | ValueInfo Callee = std::get<0>(t: getValueInfoFromValueId(ValueId: Record[Slot++])); |
7404 | TypeId.push_back(x: {Offset, Callee}); |
7405 | } |
7406 | |
7407 | void ModuleSummaryIndexBitcodeReader::parseTypeIdCompatibleVtableSummaryRecord( |
7408 | ArrayRef<uint64_t> Record) { |
7409 | size_t Slot = 0; |
7410 | TypeIdCompatibleVtableInfo &TypeId = |
7411 | TheIndex.getOrInsertTypeIdCompatibleVtableSummary( |
7412 | TypeId: {Strtab.data() + Record[Slot], |
7413 | static_cast<size_t>(Record[Slot + 1])}); |
7414 | Slot += 2; |
7415 | |
7416 | while (Slot < Record.size()) |
7417 | parseTypeIdCompatibleVtableInfo(Record, Slot, TypeId); |
7418 | } |
7419 | |
7420 | static void setSpecialRefs(std::vector<ValueInfo> &Refs, unsigned ROCnt, |
7421 | unsigned WOCnt) { |
7422 | // Readonly and writeonly refs are in the end of the refs list. |
7423 | assert(ROCnt + WOCnt <= Refs.size()); |
7424 | unsigned FirstWORef = Refs.size() - WOCnt; |
7425 | unsigned RefNo = FirstWORef - ROCnt; |
7426 | for (; RefNo < FirstWORef; ++RefNo) |
7427 | Refs[RefNo].setReadOnly(); |
7428 | for (; RefNo < Refs.size(); ++RefNo) |
7429 | Refs[RefNo].setWriteOnly(); |
7430 | } |
7431 | |
7432 | // Eagerly parse the entire summary block. This populates the GlobalValueSummary |
7433 | // objects in the index. |
7434 | Error ModuleSummaryIndexBitcodeReader::parseEntireSummary(unsigned ID) { |
7435 | if (Error Err = Stream.EnterSubBlock(BlockID: ID)) |
7436 | return Err; |
7437 | SmallVector<uint64_t, 64> Record; |
7438 | |
7439 | // Parse version |
7440 | { |
7441 | Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks(); |
7442 | if (!MaybeEntry) |
7443 | return MaybeEntry.takeError(); |
7444 | BitstreamEntry Entry = MaybeEntry.get(); |
7445 | |
7446 | if (Entry.Kind != BitstreamEntry::Record) |
7447 | return error(Message: "Invalid Summary Block: record for version expected" ); |
7448 | Expected<unsigned> MaybeRecord = Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record); |
7449 | if (!MaybeRecord) |
7450 | return MaybeRecord.takeError(); |
7451 | if (MaybeRecord.get() != bitc::FS_VERSION) |
7452 | return error(Message: "Invalid Summary Block: version expected" ); |
7453 | } |
7454 | const uint64_t Version = Record[0]; |
7455 | const bool IsOldProfileFormat = Version == 1; |
7456 | if (Version < 1 || Version > ModuleSummaryIndex::BitcodeSummaryVersion) |
7457 | return error(Message: "Invalid summary version " + Twine(Version) + |
7458 | ". Version should be in the range [1-" + |
7459 | Twine(ModuleSummaryIndex::BitcodeSummaryVersion) + |
7460 | "]." ); |
7461 | Record.clear(); |
7462 | |
7463 | // Keep around the last seen summary to be used when we see an optional |
7464 | // "OriginalName" attachement. |
7465 | GlobalValueSummary *LastSeenSummary = nullptr; |
7466 | GlobalValue::GUID LastSeenGUID = 0; |
7467 | |
7468 | // We can expect to see any number of type ID information records before |
7469 | // each function summary records; these variables store the information |
7470 | // collected so far so that it can be used to create the summary object. |
7471 | std::vector<GlobalValue::GUID> PendingTypeTests; |
7472 | std::vector<FunctionSummary::VFuncId> PendingTypeTestAssumeVCalls, |
7473 | PendingTypeCheckedLoadVCalls; |
7474 | std::vector<FunctionSummary::ConstVCall> PendingTypeTestAssumeConstVCalls, |
7475 | PendingTypeCheckedLoadConstVCalls; |
7476 | std::vector<FunctionSummary::ParamAccess> PendingParamAccesses; |
7477 | |
7478 | std::vector<CallsiteInfo> PendingCallsites; |
7479 | std::vector<AllocInfo> PendingAllocs; |
7480 | |
7481 | while (true) { |
7482 | Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks(); |
7483 | if (!MaybeEntry) |
7484 | return MaybeEntry.takeError(); |
7485 | BitstreamEntry Entry = MaybeEntry.get(); |
7486 | |
7487 | switch (Entry.Kind) { |
7488 | case BitstreamEntry::SubBlock: // Handled for us already. |
7489 | case BitstreamEntry::Error: |
7490 | return error(Message: "Malformed block" ); |
7491 | case BitstreamEntry::EndBlock: |
7492 | return Error::success(); |
7493 | case BitstreamEntry::Record: |
7494 | // The interesting case. |
7495 | break; |
7496 | } |
7497 | |
7498 | // Read a record. The record format depends on whether this |
7499 | // is a per-module index or a combined index file. In the per-module |
7500 | // case the records contain the associated value's ID for correlation |
7501 | // with VST entries. In the combined index the correlation is done |
7502 | // via the bitcode offset of the summary records (which were saved |
7503 | // in the combined index VST entries). The records also contain |
7504 | // information used for ThinLTO renaming and importing. |
7505 | Record.clear(); |
7506 | Expected<unsigned> MaybeBitCode = Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record); |
7507 | if (!MaybeBitCode) |
7508 | return MaybeBitCode.takeError(); |
7509 | switch (unsigned BitCode = MaybeBitCode.get()) { |
7510 | default: // Default behavior: ignore. |
7511 | break; |
7512 | case bitc::FS_FLAGS: { // [flags] |
7513 | TheIndex.setFlags(Record[0]); |
7514 | break; |
7515 | } |
7516 | case bitc::FS_VALUE_GUID: { // [valueid, refguid] |
7517 | uint64_t ValueID = Record[0]; |
7518 | GlobalValue::GUID RefGUID = Record[1]; |
7519 | ValueIdToValueInfoMap[ValueID] = std::make_tuple( |
7520 | args: TheIndex.getOrInsertValueInfo(GUID: RefGUID), args&: RefGUID, args&: RefGUID); |
7521 | break; |
7522 | } |
7523 | // FS_PERMODULE is legacy and does not have support for the tail call flag. |
7524 | // FS_PERMODULE: [valueid, flags, instcount, fflags, numrefs, |
7525 | // numrefs x valueid, n x (valueid)] |
7526 | // FS_PERMODULE_PROFILE: [valueid, flags, instcount, fflags, numrefs, |
7527 | // numrefs x valueid, |
7528 | // n x (valueid, hotness+tailcall flags)] |
7529 | // FS_PERMODULE_RELBF: [valueid, flags, instcount, fflags, numrefs, |
7530 | // numrefs x valueid, |
7531 | // n x (valueid, relblockfreq+tailcall)] |
7532 | case bitc::FS_PERMODULE: |
7533 | case bitc::FS_PERMODULE_RELBF: |
7534 | case bitc::FS_PERMODULE_PROFILE: { |
7535 | unsigned ValueID = Record[0]; |
7536 | uint64_t RawFlags = Record[1]; |
7537 | unsigned InstCount = Record[2]; |
7538 | uint64_t RawFunFlags = 0; |
7539 | unsigned NumRefs = Record[3]; |
7540 | unsigned NumRORefs = 0, NumWORefs = 0; |
7541 | int RefListStartIndex = 4; |
7542 | if (Version >= 4) { |
7543 | RawFunFlags = Record[3]; |
7544 | NumRefs = Record[4]; |
7545 | RefListStartIndex = 5; |
7546 | if (Version >= 5) { |
7547 | NumRORefs = Record[5]; |
7548 | RefListStartIndex = 6; |
7549 | if (Version >= 7) { |
7550 | NumWORefs = Record[6]; |
7551 | RefListStartIndex = 7; |
7552 | } |
7553 | } |
7554 | } |
7555 | |
7556 | auto Flags = getDecodedGVSummaryFlags(RawFlags, Version); |
7557 | // The module path string ref set in the summary must be owned by the |
7558 | // index's module string table. Since we don't have a module path |
7559 | // string table section in the per-module index, we create a single |
7560 | // module path string table entry with an empty (0) ID to take |
7561 | // ownership. |
7562 | int CallGraphEdgeStartIndex = RefListStartIndex + NumRefs; |
7563 | assert(Record.size() >= RefListStartIndex + NumRefs && |
7564 | "Record size inconsistent with number of references" ); |
7565 | std::vector<ValueInfo> Refs = makeRefList( |
7566 | Record: ArrayRef<uint64_t>(Record).slice(N: RefListStartIndex, M: NumRefs)); |
7567 | bool HasProfile = (BitCode == bitc::FS_PERMODULE_PROFILE); |
7568 | bool HasRelBF = (BitCode == bitc::FS_PERMODULE_RELBF); |
7569 | std::vector<FunctionSummary::EdgeTy> Calls = makeCallList( |
7570 | Record: ArrayRef<uint64_t>(Record).slice(N: CallGraphEdgeStartIndex), |
7571 | IsOldProfileFormat, HasProfile, HasRelBF); |
7572 | setSpecialRefs(Refs, ROCnt: NumRORefs, WOCnt: NumWORefs); |
7573 | auto VIAndOriginalGUID = getValueInfoFromValueId(ValueId: ValueID); |
7574 | // In order to save memory, only record the memprof summaries if this is |
7575 | // the prevailing copy of a symbol. The linker doesn't resolve local |
7576 | // linkage values so don't check whether those are prevailing. |
7577 | auto LT = (GlobalValue::LinkageTypes)Flags.Linkage; |
7578 | if (IsPrevailing && |
7579 | !GlobalValue::isLocalLinkage(Linkage: LT) && |
7580 | !IsPrevailing(std::get<2>(t&: VIAndOriginalGUID))) { |
7581 | PendingCallsites.clear(); |
7582 | PendingAllocs.clear(); |
7583 | } |
7584 | auto FS = std::make_unique<FunctionSummary>( |
7585 | args&: Flags, args&: InstCount, args: getDecodedFFlags(RawFlags: RawFunFlags), /*EntryCount=*/args: 0, |
7586 | args: std::move(Refs), args: std::move(Calls), args: std::move(PendingTypeTests), |
7587 | args: std::move(PendingTypeTestAssumeVCalls), |
7588 | args: std::move(PendingTypeCheckedLoadVCalls), |
7589 | args: std::move(PendingTypeTestAssumeConstVCalls), |
7590 | args: std::move(PendingTypeCheckedLoadConstVCalls), |
7591 | args: std::move(PendingParamAccesses), args: std::move(PendingCallsites), |
7592 | args: std::move(PendingAllocs)); |
7593 | FS->setModulePath(getThisModule()->first()); |
7594 | FS->setOriginalName(std::get<1>(t&: VIAndOriginalGUID)); |
7595 | TheIndex.addGlobalValueSummary(VI: std::get<0>(t&: VIAndOriginalGUID), |
7596 | Summary: std::move(FS)); |
7597 | break; |
7598 | } |
7599 | // FS_ALIAS: [valueid, flags, valueid] |
7600 | // Aliases must be emitted (and parsed) after all FS_PERMODULE entries, as |
7601 | // they expect all aliasee summaries to be available. |
7602 | case bitc::FS_ALIAS: { |
7603 | unsigned ValueID = Record[0]; |
7604 | uint64_t RawFlags = Record[1]; |
7605 | unsigned AliaseeID = Record[2]; |
7606 | auto Flags = getDecodedGVSummaryFlags(RawFlags, Version); |
7607 | auto AS = std::make_unique<AliasSummary>(args&: Flags); |
7608 | // The module path string ref set in the summary must be owned by the |
7609 | // index's module string table. Since we don't have a module path |
7610 | // string table section in the per-module index, we create a single |
7611 | // module path string table entry with an empty (0) ID to take |
7612 | // ownership. |
7613 | AS->setModulePath(getThisModule()->first()); |
7614 | |
7615 | auto AliaseeVI = std::get<0>(t: getValueInfoFromValueId(ValueId: AliaseeID)); |
7616 | auto AliaseeInModule = TheIndex.findSummaryInModule(VI: AliaseeVI, ModuleId: ModulePath); |
7617 | if (!AliaseeInModule) |
7618 | return error(Message: "Alias expects aliasee summary to be parsed" ); |
7619 | AS->setAliasee(AliaseeVI, Aliasee: AliaseeInModule); |
7620 | |
7621 | auto GUID = getValueInfoFromValueId(ValueId: ValueID); |
7622 | AS->setOriginalName(std::get<1>(t&: GUID)); |
7623 | TheIndex.addGlobalValueSummary(VI: std::get<0>(t&: GUID), Summary: std::move(AS)); |
7624 | break; |
7625 | } |
7626 | // FS_PERMODULE_GLOBALVAR_INIT_REFS: [valueid, flags, varflags, n x valueid] |
7627 | case bitc::FS_PERMODULE_GLOBALVAR_INIT_REFS: { |
7628 | unsigned ValueID = Record[0]; |
7629 | uint64_t RawFlags = Record[1]; |
7630 | unsigned RefArrayStart = 2; |
7631 | GlobalVarSummary::GVarFlags GVF(/* ReadOnly */ false, |
7632 | /* WriteOnly */ false, |
7633 | /* Constant */ false, |
7634 | GlobalObject::VCallVisibilityPublic); |
7635 | auto Flags = getDecodedGVSummaryFlags(RawFlags, Version); |
7636 | if (Version >= 5) { |
7637 | GVF = getDecodedGVarFlags(RawFlags: Record[2]); |
7638 | RefArrayStart = 3; |
7639 | } |
7640 | std::vector<ValueInfo> Refs = |
7641 | makeRefList(Record: ArrayRef<uint64_t>(Record).slice(N: RefArrayStart)); |
7642 | auto FS = |
7643 | std::make_unique<GlobalVarSummary>(args&: Flags, args&: GVF, args: std::move(Refs)); |
7644 | FS->setModulePath(getThisModule()->first()); |
7645 | auto GUID = getValueInfoFromValueId(ValueId: ValueID); |
7646 | FS->setOriginalName(std::get<1>(t&: GUID)); |
7647 | TheIndex.addGlobalValueSummary(VI: std::get<0>(t&: GUID), Summary: std::move(FS)); |
7648 | break; |
7649 | } |
7650 | // FS_PERMODULE_VTABLE_GLOBALVAR_INIT_REFS: [valueid, flags, varflags, |
7651 | // numrefs, numrefs x valueid, |
7652 | // n x (valueid, offset)] |
7653 | case bitc::FS_PERMODULE_VTABLE_GLOBALVAR_INIT_REFS: { |
7654 | unsigned ValueID = Record[0]; |
7655 | uint64_t RawFlags = Record[1]; |
7656 | GlobalVarSummary::GVarFlags GVF = getDecodedGVarFlags(RawFlags: Record[2]); |
7657 | unsigned NumRefs = Record[3]; |
7658 | unsigned RefListStartIndex = 4; |
7659 | unsigned VTableListStartIndex = RefListStartIndex + NumRefs; |
7660 | auto Flags = getDecodedGVSummaryFlags(RawFlags, Version); |
7661 | std::vector<ValueInfo> Refs = makeRefList( |
7662 | Record: ArrayRef<uint64_t>(Record).slice(N: RefListStartIndex, M: NumRefs)); |
7663 | VTableFuncList VTableFuncs; |
7664 | for (unsigned I = VTableListStartIndex, E = Record.size(); I != E; ++I) { |
7665 | ValueInfo Callee = std::get<0>(t: getValueInfoFromValueId(ValueId: Record[I])); |
7666 | uint64_t Offset = Record[++I]; |
7667 | VTableFuncs.push_back(x: {Callee, Offset}); |
7668 | } |
7669 | auto VS = |
7670 | std::make_unique<GlobalVarSummary>(args&: Flags, args&: GVF, args: std::move(Refs)); |
7671 | VS->setModulePath(getThisModule()->first()); |
7672 | VS->setVTableFuncs(VTableFuncs); |
7673 | auto GUID = getValueInfoFromValueId(ValueId: ValueID); |
7674 | VS->setOriginalName(std::get<1>(t&: GUID)); |
7675 | TheIndex.addGlobalValueSummary(VI: std::get<0>(t&: GUID), Summary: std::move(VS)); |
7676 | break; |
7677 | } |
7678 | // FS_COMBINED is legacy and does not have support for the tail call flag. |
7679 | // FS_COMBINED: [valueid, modid, flags, instcount, fflags, numrefs, |
7680 | // numrefs x valueid, n x (valueid)] |
7681 | // FS_COMBINED_PROFILE: [valueid, modid, flags, instcount, fflags, numrefs, |
7682 | // numrefs x valueid, |
7683 | // n x (valueid, hotness+tailcall flags)] |
7684 | case bitc::FS_COMBINED: |
7685 | case bitc::FS_COMBINED_PROFILE: { |
7686 | unsigned ValueID = Record[0]; |
7687 | uint64_t ModuleId = Record[1]; |
7688 | uint64_t RawFlags = Record[2]; |
7689 | unsigned InstCount = Record[3]; |
7690 | uint64_t RawFunFlags = 0; |
7691 | uint64_t EntryCount = 0; |
7692 | unsigned NumRefs = Record[4]; |
7693 | unsigned NumRORefs = 0, NumWORefs = 0; |
7694 | int RefListStartIndex = 5; |
7695 | |
7696 | if (Version >= 4) { |
7697 | RawFunFlags = Record[4]; |
7698 | RefListStartIndex = 6; |
7699 | size_t NumRefsIndex = 5; |
7700 | if (Version >= 5) { |
7701 | unsigned NumRORefsOffset = 1; |
7702 | RefListStartIndex = 7; |
7703 | if (Version >= 6) { |
7704 | NumRefsIndex = 6; |
7705 | EntryCount = Record[5]; |
7706 | RefListStartIndex = 8; |
7707 | if (Version >= 7) { |
7708 | RefListStartIndex = 9; |
7709 | NumWORefs = Record[8]; |
7710 | NumRORefsOffset = 2; |
7711 | } |
7712 | } |
7713 | NumRORefs = Record[RefListStartIndex - NumRORefsOffset]; |
7714 | } |
7715 | NumRefs = Record[NumRefsIndex]; |
7716 | } |
7717 | |
7718 | auto Flags = getDecodedGVSummaryFlags(RawFlags, Version); |
7719 | int CallGraphEdgeStartIndex = RefListStartIndex + NumRefs; |
7720 | assert(Record.size() >= RefListStartIndex + NumRefs && |
7721 | "Record size inconsistent with number of references" ); |
7722 | std::vector<ValueInfo> Refs = makeRefList( |
7723 | Record: ArrayRef<uint64_t>(Record).slice(N: RefListStartIndex, M: NumRefs)); |
7724 | bool HasProfile = (BitCode == bitc::FS_COMBINED_PROFILE); |
7725 | std::vector<FunctionSummary::EdgeTy> Edges = makeCallList( |
7726 | Record: ArrayRef<uint64_t>(Record).slice(N: CallGraphEdgeStartIndex), |
7727 | IsOldProfileFormat, HasProfile, HasRelBF: false); |
7728 | ValueInfo VI = std::get<0>(t: getValueInfoFromValueId(ValueId: ValueID)); |
7729 | setSpecialRefs(Refs, ROCnt: NumRORefs, WOCnt: NumWORefs); |
7730 | auto FS = std::make_unique<FunctionSummary>( |
7731 | args&: Flags, args&: InstCount, args: getDecodedFFlags(RawFlags: RawFunFlags), args&: EntryCount, |
7732 | args: std::move(Refs), args: std::move(Edges), args: std::move(PendingTypeTests), |
7733 | args: std::move(PendingTypeTestAssumeVCalls), |
7734 | args: std::move(PendingTypeCheckedLoadVCalls), |
7735 | args: std::move(PendingTypeTestAssumeConstVCalls), |
7736 | args: std::move(PendingTypeCheckedLoadConstVCalls), |
7737 | args: std::move(PendingParamAccesses), args: std::move(PendingCallsites), |
7738 | args: std::move(PendingAllocs)); |
7739 | LastSeenSummary = FS.get(); |
7740 | LastSeenGUID = VI.getGUID(); |
7741 | FS->setModulePath(ModuleIdMap[ModuleId]); |
7742 | TheIndex.addGlobalValueSummary(VI, Summary: std::move(FS)); |
7743 | break; |
7744 | } |
7745 | // FS_COMBINED_ALIAS: [valueid, modid, flags, valueid] |
7746 | // Aliases must be emitted (and parsed) after all FS_COMBINED entries, as |
7747 | // they expect all aliasee summaries to be available. |
7748 | case bitc::FS_COMBINED_ALIAS: { |
7749 | unsigned ValueID = Record[0]; |
7750 | uint64_t ModuleId = Record[1]; |
7751 | uint64_t RawFlags = Record[2]; |
7752 | unsigned AliaseeValueId = Record[3]; |
7753 | auto Flags = getDecodedGVSummaryFlags(RawFlags, Version); |
7754 | auto AS = std::make_unique<AliasSummary>(args&: Flags); |
7755 | LastSeenSummary = AS.get(); |
7756 | AS->setModulePath(ModuleIdMap[ModuleId]); |
7757 | |
7758 | auto AliaseeVI = std::get<0>(t: getValueInfoFromValueId(ValueId: AliaseeValueId)); |
7759 | auto AliaseeInModule = TheIndex.findSummaryInModule(VI: AliaseeVI, ModuleId: AS->modulePath()); |
7760 | AS->setAliasee(AliaseeVI, Aliasee: AliaseeInModule); |
7761 | |
7762 | ValueInfo VI = std::get<0>(t: getValueInfoFromValueId(ValueId: ValueID)); |
7763 | LastSeenGUID = VI.getGUID(); |
7764 | TheIndex.addGlobalValueSummary(VI, Summary: std::move(AS)); |
7765 | break; |
7766 | } |
7767 | // FS_COMBINED_GLOBALVAR_INIT_REFS: [valueid, modid, flags, n x valueid] |
7768 | case bitc::FS_COMBINED_GLOBALVAR_INIT_REFS: { |
7769 | unsigned ValueID = Record[0]; |
7770 | uint64_t ModuleId = Record[1]; |
7771 | uint64_t RawFlags = Record[2]; |
7772 | unsigned RefArrayStart = 3; |
7773 | GlobalVarSummary::GVarFlags GVF(/* ReadOnly */ false, |
7774 | /* WriteOnly */ false, |
7775 | /* Constant */ false, |
7776 | GlobalObject::VCallVisibilityPublic); |
7777 | auto Flags = getDecodedGVSummaryFlags(RawFlags, Version); |
7778 | if (Version >= 5) { |
7779 | GVF = getDecodedGVarFlags(RawFlags: Record[3]); |
7780 | RefArrayStart = 4; |
7781 | } |
7782 | std::vector<ValueInfo> Refs = |
7783 | makeRefList(Record: ArrayRef<uint64_t>(Record).slice(N: RefArrayStart)); |
7784 | auto FS = |
7785 | std::make_unique<GlobalVarSummary>(args&: Flags, args&: GVF, args: std::move(Refs)); |
7786 | LastSeenSummary = FS.get(); |
7787 | FS->setModulePath(ModuleIdMap[ModuleId]); |
7788 | ValueInfo VI = std::get<0>(t: getValueInfoFromValueId(ValueId: ValueID)); |
7789 | LastSeenGUID = VI.getGUID(); |
7790 | TheIndex.addGlobalValueSummary(VI, Summary: std::move(FS)); |
7791 | break; |
7792 | } |
7793 | // FS_COMBINED_ORIGINAL_NAME: [original_name] |
7794 | case bitc::FS_COMBINED_ORIGINAL_NAME: { |
7795 | uint64_t OriginalName = Record[0]; |
7796 | if (!LastSeenSummary) |
7797 | return error(Message: "Name attachment that does not follow a combined record" ); |
7798 | LastSeenSummary->setOriginalName(OriginalName); |
7799 | TheIndex.addOriginalName(ValueGUID: LastSeenGUID, OrigGUID: OriginalName); |
7800 | // Reset the LastSeenSummary |
7801 | LastSeenSummary = nullptr; |
7802 | LastSeenGUID = 0; |
7803 | break; |
7804 | } |
7805 | case bitc::FS_TYPE_TESTS: |
7806 | assert(PendingTypeTests.empty()); |
7807 | llvm::append_range(C&: PendingTypeTests, R&: Record); |
7808 | break; |
7809 | |
7810 | case bitc::FS_TYPE_TEST_ASSUME_VCALLS: |
7811 | assert(PendingTypeTestAssumeVCalls.empty()); |
7812 | for (unsigned I = 0; I != Record.size(); I += 2) |
7813 | PendingTypeTestAssumeVCalls.push_back(x: {.GUID: Record[I], .Offset: Record[I+1]}); |
7814 | break; |
7815 | |
7816 | case bitc::FS_TYPE_CHECKED_LOAD_VCALLS: |
7817 | assert(PendingTypeCheckedLoadVCalls.empty()); |
7818 | for (unsigned I = 0; I != Record.size(); I += 2) |
7819 | PendingTypeCheckedLoadVCalls.push_back(x: {.GUID: Record[I], .Offset: Record[I+1]}); |
7820 | break; |
7821 | |
7822 | case bitc::FS_TYPE_TEST_ASSUME_CONST_VCALL: |
7823 | PendingTypeTestAssumeConstVCalls.push_back( |
7824 | x: {.VFunc: {.GUID: Record[0], .Offset: Record[1]}, .Args: {Record.begin() + 2, Record.end()}}); |
7825 | break; |
7826 | |
7827 | case bitc::FS_TYPE_CHECKED_LOAD_CONST_VCALL: |
7828 | PendingTypeCheckedLoadConstVCalls.push_back( |
7829 | x: {.VFunc: {.GUID: Record[0], .Offset: Record[1]}, .Args: {Record.begin() + 2, Record.end()}}); |
7830 | break; |
7831 | |
7832 | case bitc::FS_CFI_FUNCTION_DEFS: { |
7833 | std::set<std::string> &CfiFunctionDefs = TheIndex.cfiFunctionDefs(); |
7834 | for (unsigned I = 0; I != Record.size(); I += 2) |
7835 | CfiFunctionDefs.insert( |
7836 | x: {Strtab.data() + Record[I], static_cast<size_t>(Record[I + 1])}); |
7837 | break; |
7838 | } |
7839 | |
7840 | case bitc::FS_CFI_FUNCTION_DECLS: { |
7841 | std::set<std::string> &CfiFunctionDecls = TheIndex.cfiFunctionDecls(); |
7842 | for (unsigned I = 0; I != Record.size(); I += 2) |
7843 | CfiFunctionDecls.insert( |
7844 | x: {Strtab.data() + Record[I], static_cast<size_t>(Record[I + 1])}); |
7845 | break; |
7846 | } |
7847 | |
7848 | case bitc::FS_TYPE_ID: |
7849 | parseTypeIdSummaryRecord(Record, Strtab, TheIndex); |
7850 | break; |
7851 | |
7852 | case bitc::FS_TYPE_ID_METADATA: |
7853 | parseTypeIdCompatibleVtableSummaryRecord(Record); |
7854 | break; |
7855 | |
7856 | case bitc::FS_BLOCK_COUNT: |
7857 | TheIndex.addBlockCount(C: Record[0]); |
7858 | break; |
7859 | |
7860 | case bitc::FS_PARAM_ACCESS: { |
7861 | PendingParamAccesses = parseParamAccesses(Record); |
7862 | break; |
7863 | } |
7864 | |
7865 | case bitc::FS_STACK_IDS: { // [n x stackid] |
7866 | // Save stack ids in the reader to consult when adding stack ids from the |
7867 | // lists in the stack node and alloc node entries. |
7868 | StackIds = ArrayRef<uint64_t>(Record); |
7869 | break; |
7870 | } |
7871 | |
7872 | case bitc::FS_PERMODULE_CALLSITE_INFO: { |
7873 | unsigned ValueID = Record[0]; |
7874 | SmallVector<unsigned> StackIdList; |
7875 | for (auto R = Record.begin() + 1; R != Record.end(); R++) { |
7876 | assert(*R < StackIds.size()); |
7877 | StackIdList.push_back(Elt: TheIndex.addOrGetStackIdIndex(StackId: StackIds[*R])); |
7878 | } |
7879 | ValueInfo VI = std::get<0>(t: getValueInfoFromValueId(ValueId: ValueID)); |
7880 | PendingCallsites.push_back(x: CallsiteInfo({VI, std::move(StackIdList)})); |
7881 | break; |
7882 | } |
7883 | |
7884 | case bitc::FS_COMBINED_CALLSITE_INFO: { |
7885 | auto RecordIter = Record.begin(); |
7886 | unsigned ValueID = *RecordIter++; |
7887 | unsigned NumStackIds = *RecordIter++; |
7888 | unsigned NumVersions = *RecordIter++; |
7889 | assert(Record.size() == 3 + NumStackIds + NumVersions); |
7890 | SmallVector<unsigned> StackIdList; |
7891 | for (unsigned J = 0; J < NumStackIds; J++) { |
7892 | assert(*RecordIter < StackIds.size()); |
7893 | StackIdList.push_back( |
7894 | Elt: TheIndex.addOrGetStackIdIndex(StackId: StackIds[*RecordIter++])); |
7895 | } |
7896 | SmallVector<unsigned> Versions; |
7897 | for (unsigned J = 0; J < NumVersions; J++) |
7898 | Versions.push_back(Elt: *RecordIter++); |
7899 | ValueInfo VI = std::get<0>( |
7900 | t: getValueInfoFromValueId</*AllowNullValueInfo*/ true>(ValueId: ValueID)); |
7901 | PendingCallsites.push_back( |
7902 | x: CallsiteInfo({VI, std::move(Versions), std::move(StackIdList)})); |
7903 | break; |
7904 | } |
7905 | |
7906 | case bitc::FS_PERMODULE_ALLOC_INFO: { |
7907 | unsigned I = 0; |
7908 | std::vector<MIBInfo> MIBs; |
7909 | while (I < Record.size()) { |
7910 | assert(Record.size() - I >= 2); |
7911 | AllocationType AllocType = (AllocationType)Record[I++]; |
7912 | unsigned NumStackEntries = Record[I++]; |
7913 | assert(Record.size() - I >= NumStackEntries); |
7914 | SmallVector<unsigned> StackIdList; |
7915 | for (unsigned J = 0; J < NumStackEntries; J++) { |
7916 | assert(Record[I] < StackIds.size()); |
7917 | StackIdList.push_back( |
7918 | Elt: TheIndex.addOrGetStackIdIndex(StackId: StackIds[Record[I++]])); |
7919 | } |
7920 | MIBs.push_back(x: MIBInfo(AllocType, std::move(StackIdList))); |
7921 | } |
7922 | PendingAllocs.push_back(x: AllocInfo(std::move(MIBs))); |
7923 | break; |
7924 | } |
7925 | |
7926 | case bitc::FS_COMBINED_ALLOC_INFO: { |
7927 | unsigned I = 0; |
7928 | std::vector<MIBInfo> MIBs; |
7929 | unsigned NumMIBs = Record[I++]; |
7930 | unsigned NumVersions = Record[I++]; |
7931 | unsigned MIBsRead = 0; |
7932 | while (MIBsRead++ < NumMIBs) { |
7933 | assert(Record.size() - I >= 2); |
7934 | AllocationType AllocType = (AllocationType)Record[I++]; |
7935 | unsigned NumStackEntries = Record[I++]; |
7936 | assert(Record.size() - I >= NumStackEntries); |
7937 | SmallVector<unsigned> StackIdList; |
7938 | for (unsigned J = 0; J < NumStackEntries; J++) { |
7939 | assert(Record[I] < StackIds.size()); |
7940 | StackIdList.push_back( |
7941 | Elt: TheIndex.addOrGetStackIdIndex(StackId: StackIds[Record[I++]])); |
7942 | } |
7943 | MIBs.push_back(x: MIBInfo(AllocType, std::move(StackIdList))); |
7944 | } |
7945 | assert(Record.size() - I >= NumVersions); |
7946 | SmallVector<uint8_t> Versions; |
7947 | for (unsigned J = 0; J < NumVersions; J++) |
7948 | Versions.push_back(Elt: Record[I++]); |
7949 | PendingAllocs.push_back( |
7950 | x: AllocInfo(std::move(Versions), std::move(MIBs))); |
7951 | break; |
7952 | } |
7953 | } |
7954 | } |
7955 | llvm_unreachable("Exit infinite loop" ); |
7956 | } |
7957 | |
7958 | // Parse the module string table block into the Index. |
7959 | // This populates the ModulePathStringTable map in the index. |
7960 | Error ModuleSummaryIndexBitcodeReader::parseModuleStringTable() { |
7961 | if (Error Err = Stream.EnterSubBlock(BlockID: bitc::MODULE_STRTAB_BLOCK_ID)) |
7962 | return Err; |
7963 | |
7964 | SmallVector<uint64_t, 64> Record; |
7965 | |
7966 | SmallString<128> ModulePath; |
7967 | ModuleSummaryIndex::ModuleInfo *LastSeenModule = nullptr; |
7968 | |
7969 | while (true) { |
7970 | Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks(); |
7971 | if (!MaybeEntry) |
7972 | return MaybeEntry.takeError(); |
7973 | BitstreamEntry Entry = MaybeEntry.get(); |
7974 | |
7975 | switch (Entry.Kind) { |
7976 | case BitstreamEntry::SubBlock: // Handled for us already. |
7977 | case BitstreamEntry::Error: |
7978 | return error(Message: "Malformed block" ); |
7979 | case BitstreamEntry::EndBlock: |
7980 | return Error::success(); |
7981 | case BitstreamEntry::Record: |
7982 | // The interesting case. |
7983 | break; |
7984 | } |
7985 | |
7986 | Record.clear(); |
7987 | Expected<unsigned> MaybeRecord = Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record); |
7988 | if (!MaybeRecord) |
7989 | return MaybeRecord.takeError(); |
7990 | switch (MaybeRecord.get()) { |
7991 | default: // Default behavior: ignore. |
7992 | break; |
7993 | case bitc::MST_CODE_ENTRY: { |
7994 | // MST_ENTRY: [modid, namechar x N] |
7995 | uint64_t ModuleId = Record[0]; |
7996 | |
7997 | if (convertToString(Record, Idx: 1, Result&: ModulePath)) |
7998 | return error(Message: "Invalid record" ); |
7999 | |
8000 | LastSeenModule = TheIndex.addModule(ModPath: ModulePath); |
8001 | ModuleIdMap[ModuleId] = LastSeenModule->first(); |
8002 | |
8003 | ModulePath.clear(); |
8004 | break; |
8005 | } |
8006 | /// MST_CODE_HASH: [5*i32] |
8007 | case bitc::MST_CODE_HASH: { |
8008 | if (Record.size() != 5) |
8009 | return error(Message: "Invalid hash length " + Twine(Record.size()).str()); |
8010 | if (!LastSeenModule) |
8011 | return error(Message: "Invalid hash that does not follow a module path" ); |
8012 | int Pos = 0; |
8013 | for (auto &Val : Record) { |
8014 | assert(!(Val >> 32) && "Unexpected high bits set" ); |
8015 | LastSeenModule->second[Pos++] = Val; |
8016 | } |
8017 | // Reset LastSeenModule to avoid overriding the hash unexpectedly. |
8018 | LastSeenModule = nullptr; |
8019 | break; |
8020 | } |
8021 | } |
8022 | } |
8023 | llvm_unreachable("Exit infinite loop" ); |
8024 | } |
8025 | |
8026 | namespace { |
8027 | |
8028 | // FIXME: This class is only here to support the transition to llvm::Error. It |
8029 | // will be removed once this transition is complete. Clients should prefer to |
8030 | // deal with the Error value directly, rather than converting to error_code. |
8031 | class BitcodeErrorCategoryType : public std::error_category { |
8032 | const char *name() const noexcept override { |
8033 | return "llvm.bitcode" ; |
8034 | } |
8035 | |
8036 | std::string message(int IE) const override { |
8037 | BitcodeError E = static_cast<BitcodeError>(IE); |
8038 | switch (E) { |
8039 | case BitcodeError::CorruptedBitcode: |
8040 | return "Corrupted bitcode" ; |
8041 | } |
8042 | llvm_unreachable("Unknown error type!" ); |
8043 | } |
8044 | }; |
8045 | |
8046 | } // end anonymous namespace |
8047 | |
8048 | const std::error_category &llvm::BitcodeErrorCategory() { |
8049 | static BitcodeErrorCategoryType ErrorCategory; |
8050 | return ErrorCategory; |
8051 | } |
8052 | |
8053 | static Expected<StringRef> readBlobInRecord(BitstreamCursor &Stream, |
8054 | unsigned Block, unsigned RecordID) { |
8055 | if (Error Err = Stream.EnterSubBlock(BlockID: Block)) |
8056 | return std::move(Err); |
8057 | |
8058 | StringRef Strtab; |
8059 | while (true) { |
8060 | Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); |
8061 | if (!MaybeEntry) |
8062 | return MaybeEntry.takeError(); |
8063 | llvm::BitstreamEntry Entry = MaybeEntry.get(); |
8064 | |
8065 | switch (Entry.Kind) { |
8066 | case BitstreamEntry::EndBlock: |
8067 | return Strtab; |
8068 | |
8069 | case BitstreamEntry::Error: |
8070 | return error(Message: "Malformed block" ); |
8071 | |
8072 | case BitstreamEntry::SubBlock: |
8073 | if (Error Err = Stream.SkipBlock()) |
8074 | return std::move(Err); |
8075 | break; |
8076 | |
8077 | case BitstreamEntry::Record: |
8078 | StringRef Blob; |
8079 | SmallVector<uint64_t, 1> Record; |
8080 | Expected<unsigned> MaybeRecord = |
8081 | Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob); |
8082 | if (!MaybeRecord) |
8083 | return MaybeRecord.takeError(); |
8084 | if (MaybeRecord.get() == RecordID) |
8085 | Strtab = Blob; |
8086 | break; |
8087 | } |
8088 | } |
8089 | } |
8090 | |
8091 | //===----------------------------------------------------------------------===// |
8092 | // External interface |
8093 | //===----------------------------------------------------------------------===// |
8094 | |
8095 | Expected<std::vector<BitcodeModule>> |
8096 | llvm::getBitcodeModuleList(MemoryBufferRef Buffer) { |
8097 | auto FOrErr = getBitcodeFileContents(Buffer); |
8098 | if (!FOrErr) |
8099 | return FOrErr.takeError(); |
8100 | return std::move(FOrErr->Mods); |
8101 | } |
8102 | |
8103 | Expected<BitcodeFileContents> |
8104 | llvm::getBitcodeFileContents(MemoryBufferRef Buffer) { |
8105 | Expected<BitstreamCursor> StreamOrErr = initStream(Buffer); |
8106 | if (!StreamOrErr) |
8107 | return StreamOrErr.takeError(); |
8108 | BitstreamCursor &Stream = *StreamOrErr; |
8109 | |
8110 | BitcodeFileContents F; |
8111 | while (true) { |
8112 | uint64_t BCBegin = Stream.getCurrentByteNo(); |
8113 | |
8114 | // We may be consuming bitcode from a client that leaves garbage at the end |
8115 | // of the bitcode stream (e.g. Apple's ar tool). If we are close enough to |
8116 | // the end that there cannot possibly be another module, stop looking. |
8117 | if (BCBegin + 8 >= Stream.getBitcodeBytes().size()) |
8118 | return F; |
8119 | |
8120 | Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); |
8121 | if (!MaybeEntry) |
8122 | return MaybeEntry.takeError(); |
8123 | llvm::BitstreamEntry Entry = MaybeEntry.get(); |
8124 | |
8125 | switch (Entry.Kind) { |
8126 | case BitstreamEntry::EndBlock: |
8127 | case BitstreamEntry::Error: |
8128 | return error(Message: "Malformed block" ); |
8129 | |
8130 | case BitstreamEntry::SubBlock: { |
8131 | uint64_t IdentificationBit = -1ull; |
8132 | if (Entry.ID == bitc::IDENTIFICATION_BLOCK_ID) { |
8133 | IdentificationBit = Stream.GetCurrentBitNo() - BCBegin * 8; |
8134 | if (Error Err = Stream.SkipBlock()) |
8135 | return std::move(Err); |
8136 | |
8137 | { |
8138 | Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); |
8139 | if (!MaybeEntry) |
8140 | return MaybeEntry.takeError(); |
8141 | Entry = MaybeEntry.get(); |
8142 | } |
8143 | |
8144 | if (Entry.Kind != BitstreamEntry::SubBlock || |
8145 | Entry.ID != bitc::MODULE_BLOCK_ID) |
8146 | return error(Message: "Malformed block" ); |
8147 | } |
8148 | |
8149 | if (Entry.ID == bitc::MODULE_BLOCK_ID) { |
8150 | uint64_t ModuleBit = Stream.GetCurrentBitNo() - BCBegin * 8; |
8151 | if (Error Err = Stream.SkipBlock()) |
8152 | return std::move(Err); |
8153 | |
8154 | F.Mods.push_back(x: {Stream.getBitcodeBytes().slice( |
8155 | N: BCBegin, M: Stream.getCurrentByteNo() - BCBegin), |
8156 | Buffer.getBufferIdentifier(), IdentificationBit, |
8157 | ModuleBit}); |
8158 | continue; |
8159 | } |
8160 | |
8161 | if (Entry.ID == bitc::STRTAB_BLOCK_ID) { |
8162 | Expected<StringRef> Strtab = |
8163 | readBlobInRecord(Stream, Block: bitc::STRTAB_BLOCK_ID, RecordID: bitc::STRTAB_BLOB); |
8164 | if (!Strtab) |
8165 | return Strtab.takeError(); |
8166 | // This string table is used by every preceding bitcode module that does |
8167 | // not have its own string table. A bitcode file may have multiple |
8168 | // string tables if it was created by binary concatenation, for example |
8169 | // with "llvm-cat -b". |
8170 | for (BitcodeModule &I : llvm::reverse(C&: F.Mods)) { |
8171 | if (!I.Strtab.empty()) |
8172 | break; |
8173 | I.Strtab = *Strtab; |
8174 | } |
8175 | // Similarly, the string table is used by every preceding symbol table; |
8176 | // normally there will be just one unless the bitcode file was created |
8177 | // by binary concatenation. |
8178 | if (!F.Symtab.empty() && F.StrtabForSymtab.empty()) |
8179 | F.StrtabForSymtab = *Strtab; |
8180 | continue; |
8181 | } |
8182 | |
8183 | if (Entry.ID == bitc::SYMTAB_BLOCK_ID) { |
8184 | Expected<StringRef> SymtabOrErr = |
8185 | readBlobInRecord(Stream, Block: bitc::SYMTAB_BLOCK_ID, RecordID: bitc::SYMTAB_BLOB); |
8186 | if (!SymtabOrErr) |
8187 | return SymtabOrErr.takeError(); |
8188 | |
8189 | // We can expect the bitcode file to have multiple symbol tables if it |
8190 | // was created by binary concatenation. In that case we silently |
8191 | // ignore any subsequent symbol tables, which is fine because this is a |
8192 | // low level function. The client is expected to notice that the number |
8193 | // of modules in the symbol table does not match the number of modules |
8194 | // in the input file and regenerate the symbol table. |
8195 | if (F.Symtab.empty()) |
8196 | F.Symtab = *SymtabOrErr; |
8197 | continue; |
8198 | } |
8199 | |
8200 | if (Error Err = Stream.SkipBlock()) |
8201 | return std::move(Err); |
8202 | continue; |
8203 | } |
8204 | case BitstreamEntry::Record: |
8205 | if (Error E = Stream.skipRecord(AbbrevID: Entry.ID).takeError()) |
8206 | return std::move(E); |
8207 | continue; |
8208 | } |
8209 | } |
8210 | } |
8211 | |
8212 | /// Get a lazy one-at-time loading module from bitcode. |
8213 | /// |
8214 | /// This isn't always used in a lazy context. In particular, it's also used by |
8215 | /// \a parseModule(). If this is truly lazy, then we need to eagerly pull |
8216 | /// in forward-referenced functions from block address references. |
8217 | /// |
8218 | /// \param[in] MaterializeAll Set to \c true if we should materialize |
8219 | /// everything. |
8220 | Expected<std::unique_ptr<Module>> |
8221 | BitcodeModule::getModuleImpl(LLVMContext &Context, bool MaterializeAll, |
8222 | bool ShouldLazyLoadMetadata, bool IsImporting, |
8223 | ParserCallbacks Callbacks) { |
8224 | BitstreamCursor Stream(Buffer); |
8225 | |
8226 | std::string ProducerIdentification; |
8227 | if (IdentificationBit != -1ull) { |
8228 | if (Error JumpFailed = Stream.JumpToBit(BitNo: IdentificationBit)) |
8229 | return std::move(JumpFailed); |
8230 | if (Error E = |
8231 | readIdentificationBlock(Stream).moveInto(Value&: ProducerIdentification)) |
8232 | return std::move(E); |
8233 | } |
8234 | |
8235 | if (Error JumpFailed = Stream.JumpToBit(BitNo: ModuleBit)) |
8236 | return std::move(JumpFailed); |
8237 | auto *R = new BitcodeReader(std::move(Stream), Strtab, ProducerIdentification, |
8238 | Context); |
8239 | |
8240 | std::unique_ptr<Module> M = |
8241 | std::make_unique<Module>(args&: ModuleIdentifier, args&: Context); |
8242 | M->setMaterializer(R); |
8243 | |
8244 | // Delay parsing Metadata if ShouldLazyLoadMetadata is true. |
8245 | if (Error Err = R->parseBitcodeInto(M: M.get(), ShouldLazyLoadMetadata, |
8246 | IsImporting, Callbacks)) |
8247 | return std::move(Err); |
8248 | |
8249 | if (MaterializeAll) { |
8250 | // Read in the entire module, and destroy the BitcodeReader. |
8251 | if (Error Err = M->materializeAll()) |
8252 | return std::move(Err); |
8253 | } else { |
8254 | // Resolve forward references from blockaddresses. |
8255 | if (Error Err = R->materializeForwardReferencedFunctions()) |
8256 | return std::move(Err); |
8257 | } |
8258 | |
8259 | return std::move(M); |
8260 | } |
8261 | |
8262 | Expected<std::unique_ptr<Module>> |
8263 | BitcodeModule::getLazyModule(LLVMContext &Context, bool ShouldLazyLoadMetadata, |
8264 | bool IsImporting, ParserCallbacks Callbacks) { |
8265 | return getModuleImpl(Context, MaterializeAll: false, ShouldLazyLoadMetadata, IsImporting, |
8266 | Callbacks); |
8267 | } |
8268 | |
8269 | // Parse the specified bitcode buffer and merge the index into CombinedIndex. |
8270 | // We don't use ModuleIdentifier here because the client may need to control the |
8271 | // module path used in the combined summary (e.g. when reading summaries for |
8272 | // regular LTO modules). |
8273 | Error BitcodeModule::readSummary( |
8274 | ModuleSummaryIndex &CombinedIndex, StringRef ModulePath, |
8275 | std::function<bool(GlobalValue::GUID)> IsPrevailing) { |
8276 | BitstreamCursor Stream(Buffer); |
8277 | if (Error JumpFailed = Stream.JumpToBit(BitNo: ModuleBit)) |
8278 | return JumpFailed; |
8279 | |
8280 | ModuleSummaryIndexBitcodeReader R(std::move(Stream), Strtab, CombinedIndex, |
8281 | ModulePath, IsPrevailing); |
8282 | return R.parseModule(); |
8283 | } |
8284 | |
8285 | // Parse the specified bitcode buffer, returning the function info index. |
8286 | Expected<std::unique_ptr<ModuleSummaryIndex>> BitcodeModule::getSummary() { |
8287 | BitstreamCursor Stream(Buffer); |
8288 | if (Error JumpFailed = Stream.JumpToBit(BitNo: ModuleBit)) |
8289 | return std::move(JumpFailed); |
8290 | |
8291 | auto Index = std::make_unique<ModuleSummaryIndex>(/*HaveGVs=*/args: false); |
8292 | ModuleSummaryIndexBitcodeReader R(std::move(Stream), Strtab, *Index, |
8293 | ModuleIdentifier, 0); |
8294 | |
8295 | if (Error Err = R.parseModule()) |
8296 | return std::move(Err); |
8297 | |
8298 | return std::move(Index); |
8299 | } |
8300 | |
8301 | static Expected<std::pair<bool, bool>> |
8302 | getEnableSplitLTOUnitAndUnifiedFlag(BitstreamCursor &Stream, |
8303 | unsigned ID, |
8304 | BitcodeLTOInfo <OInfo) { |
8305 | if (Error Err = Stream.EnterSubBlock(BlockID: ID)) |
8306 | return std::move(Err); |
8307 | SmallVector<uint64_t, 64> Record; |
8308 | |
8309 | while (true) { |
8310 | BitstreamEntry Entry; |
8311 | std::pair<bool, bool> Result = {false,false}; |
8312 | if (Error E = Stream.advanceSkippingSubblocks().moveInto(Value&: Entry)) |
8313 | return std::move(E); |
8314 | |
8315 | switch (Entry.Kind) { |
8316 | case BitstreamEntry::SubBlock: // Handled for us already. |
8317 | case BitstreamEntry::Error: |
8318 | return error(Message: "Malformed block" ); |
8319 | case BitstreamEntry::EndBlock: { |
8320 | // If no flags record found, set both flags to false. |
8321 | return Result; |
8322 | } |
8323 | case BitstreamEntry::Record: |
8324 | // The interesting case. |
8325 | break; |
8326 | } |
8327 | |
8328 | // Look for the FS_FLAGS record. |
8329 | Record.clear(); |
8330 | Expected<unsigned> MaybeBitCode = Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record); |
8331 | if (!MaybeBitCode) |
8332 | return MaybeBitCode.takeError(); |
8333 | switch (MaybeBitCode.get()) { |
8334 | default: // Default behavior: ignore. |
8335 | break; |
8336 | case bitc::FS_FLAGS: { // [flags] |
8337 | uint64_t Flags = Record[0]; |
8338 | // Scan flags. |
8339 | assert(Flags <= 0x2ff && "Unexpected bits in flag" ); |
8340 | |
8341 | bool EnableSplitLTOUnit = Flags & 0x8; |
8342 | bool UnifiedLTO = Flags & 0x200; |
8343 | Result = {EnableSplitLTOUnit, UnifiedLTO}; |
8344 | |
8345 | return Result; |
8346 | } |
8347 | } |
8348 | } |
8349 | llvm_unreachable("Exit infinite loop" ); |
8350 | } |
8351 | |
8352 | // Check if the given bitcode buffer contains a global value summary block. |
8353 | Expected<BitcodeLTOInfo> BitcodeModule::getLTOInfo() { |
8354 | BitstreamCursor Stream(Buffer); |
8355 | if (Error JumpFailed = Stream.JumpToBit(BitNo: ModuleBit)) |
8356 | return std::move(JumpFailed); |
8357 | |
8358 | if (Error Err = Stream.EnterSubBlock(BlockID: bitc::MODULE_BLOCK_ID)) |
8359 | return std::move(Err); |
8360 | |
8361 | while (true) { |
8362 | llvm::BitstreamEntry Entry; |
8363 | if (Error E = Stream.advance().moveInto(Value&: Entry)) |
8364 | return std::move(E); |
8365 | |
8366 | switch (Entry.Kind) { |
8367 | case BitstreamEntry::Error: |
8368 | return error(Message: "Malformed block" ); |
8369 | case BitstreamEntry::EndBlock: |
8370 | return BitcodeLTOInfo{/*IsThinLTO=*/false, /*HasSummary=*/false, |
8371 | /*EnableSplitLTOUnit=*/false, /*UnifiedLTO=*/false}; |
8372 | |
8373 | case BitstreamEntry::SubBlock: |
8374 | if (Entry.ID == bitc::GLOBALVAL_SUMMARY_BLOCK_ID) { |
8375 | BitcodeLTOInfo LTOInfo; |
8376 | Expected<std::pair<bool, bool>> Flags = |
8377 | getEnableSplitLTOUnitAndUnifiedFlag(Stream, ID: Entry.ID, LTOInfo); |
8378 | if (!Flags) |
8379 | return Flags.takeError(); |
8380 | std::tie(args&: LTOInfo.EnableSplitLTOUnit, args&: LTOInfo.UnifiedLTO) = Flags.get(); |
8381 | LTOInfo.IsThinLTO = true; |
8382 | LTOInfo.HasSummary = true; |
8383 | return LTOInfo; |
8384 | } |
8385 | |
8386 | if (Entry.ID == bitc::FULL_LTO_GLOBALVAL_SUMMARY_BLOCK_ID) { |
8387 | BitcodeLTOInfo LTOInfo; |
8388 | Expected<std::pair<bool, bool>> Flags = |
8389 | getEnableSplitLTOUnitAndUnifiedFlag(Stream, ID: Entry.ID, LTOInfo); |
8390 | if (!Flags) |
8391 | return Flags.takeError(); |
8392 | std::tie(args&: LTOInfo.EnableSplitLTOUnit, args&: LTOInfo.UnifiedLTO) = Flags.get(); |
8393 | LTOInfo.IsThinLTO = false; |
8394 | LTOInfo.HasSummary = true; |
8395 | return LTOInfo; |
8396 | } |
8397 | |
8398 | // Ignore other sub-blocks. |
8399 | if (Error Err = Stream.SkipBlock()) |
8400 | return std::move(Err); |
8401 | continue; |
8402 | |
8403 | case BitstreamEntry::Record: |
8404 | if (Expected<unsigned> StreamFailed = Stream.skipRecord(AbbrevID: Entry.ID)) |
8405 | continue; |
8406 | else |
8407 | return StreamFailed.takeError(); |
8408 | } |
8409 | } |
8410 | } |
8411 | |
8412 | static Expected<BitcodeModule> getSingleModule(MemoryBufferRef Buffer) { |
8413 | Expected<std::vector<BitcodeModule>> MsOrErr = getBitcodeModuleList(Buffer); |
8414 | if (!MsOrErr) |
8415 | return MsOrErr.takeError(); |
8416 | |
8417 | if (MsOrErr->size() != 1) |
8418 | return error(Message: "Expected a single module" ); |
8419 | |
8420 | return (*MsOrErr)[0]; |
8421 | } |
8422 | |
8423 | Expected<std::unique_ptr<Module>> |
8424 | llvm::getLazyBitcodeModule(MemoryBufferRef Buffer, LLVMContext &Context, |
8425 | bool ShouldLazyLoadMetadata, bool IsImporting, |
8426 | ParserCallbacks Callbacks) { |
8427 | Expected<BitcodeModule> BM = getSingleModule(Buffer); |
8428 | if (!BM) |
8429 | return BM.takeError(); |
8430 | |
8431 | return BM->getLazyModule(Context, ShouldLazyLoadMetadata, IsImporting, |
8432 | Callbacks); |
8433 | } |
8434 | |
8435 | Expected<std::unique_ptr<Module>> llvm::getOwningLazyBitcodeModule( |
8436 | std::unique_ptr<MemoryBuffer> &&Buffer, LLVMContext &Context, |
8437 | bool ShouldLazyLoadMetadata, bool IsImporting, ParserCallbacks Callbacks) { |
8438 | auto MOrErr = getLazyBitcodeModule(Buffer: *Buffer, Context, ShouldLazyLoadMetadata, |
8439 | IsImporting, Callbacks); |
8440 | if (MOrErr) |
8441 | (*MOrErr)->setOwnedMemoryBuffer(std::move(Buffer)); |
8442 | return MOrErr; |
8443 | } |
8444 | |
8445 | Expected<std::unique_ptr<Module>> |
8446 | BitcodeModule::parseModule(LLVMContext &Context, ParserCallbacks Callbacks) { |
8447 | return getModuleImpl(Context, MaterializeAll: true, ShouldLazyLoadMetadata: false, IsImporting: false, Callbacks); |
8448 | // TODO: Restore the use-lists to the in-memory state when the bitcode was |
8449 | // written. We must defer until the Module has been fully materialized. |
8450 | } |
8451 | |
8452 | Expected<std::unique_ptr<Module>> |
8453 | llvm::parseBitcodeFile(MemoryBufferRef Buffer, LLVMContext &Context, |
8454 | ParserCallbacks Callbacks) { |
8455 | Expected<BitcodeModule> BM = getSingleModule(Buffer); |
8456 | if (!BM) |
8457 | return BM.takeError(); |
8458 | |
8459 | return BM->parseModule(Context, Callbacks); |
8460 | } |
8461 | |
8462 | Expected<std::string> llvm::getBitcodeTargetTriple(MemoryBufferRef Buffer) { |
8463 | Expected<BitstreamCursor> StreamOrErr = initStream(Buffer); |
8464 | if (!StreamOrErr) |
8465 | return StreamOrErr.takeError(); |
8466 | |
8467 | return readTriple(Stream&: *StreamOrErr); |
8468 | } |
8469 | |
8470 | Expected<bool> llvm::isBitcodeContainingObjCCategory(MemoryBufferRef Buffer) { |
8471 | Expected<BitstreamCursor> StreamOrErr = initStream(Buffer); |
8472 | if (!StreamOrErr) |
8473 | return StreamOrErr.takeError(); |
8474 | |
8475 | return hasObjCCategory(Stream&: *StreamOrErr); |
8476 | } |
8477 | |
8478 | Expected<std::string> llvm::getBitcodeProducerString(MemoryBufferRef Buffer) { |
8479 | Expected<BitstreamCursor> StreamOrErr = initStream(Buffer); |
8480 | if (!StreamOrErr) |
8481 | return StreamOrErr.takeError(); |
8482 | |
8483 | return readIdentificationCode(Stream&: *StreamOrErr); |
8484 | } |
8485 | |
8486 | Error llvm::readModuleSummaryIndex(MemoryBufferRef Buffer, |
8487 | ModuleSummaryIndex &CombinedIndex) { |
8488 | Expected<BitcodeModule> BM = getSingleModule(Buffer); |
8489 | if (!BM) |
8490 | return BM.takeError(); |
8491 | |
8492 | return BM->readSummary(CombinedIndex, ModulePath: BM->getModuleIdentifier()); |
8493 | } |
8494 | |
8495 | Expected<std::unique_ptr<ModuleSummaryIndex>> |
8496 | llvm::getModuleSummaryIndex(MemoryBufferRef Buffer) { |
8497 | Expected<BitcodeModule> BM = getSingleModule(Buffer); |
8498 | if (!BM) |
8499 | return BM.takeError(); |
8500 | |
8501 | return BM->getSummary(); |
8502 | } |
8503 | |
8504 | Expected<BitcodeLTOInfo> llvm::getBitcodeLTOInfo(MemoryBufferRef Buffer) { |
8505 | Expected<BitcodeModule> BM = getSingleModule(Buffer); |
8506 | if (!BM) |
8507 | return BM.takeError(); |
8508 | |
8509 | return BM->getLTOInfo(); |
8510 | } |
8511 | |
8512 | Expected<std::unique_ptr<ModuleSummaryIndex>> |
8513 | llvm::getModuleSummaryIndexForFile(StringRef Path, |
8514 | bool IgnoreEmptyThinLTOIndexFile) { |
8515 | ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr = |
8516 | MemoryBuffer::getFileOrSTDIN(Filename: Path); |
8517 | if (!FileOrErr) |
8518 | return errorCodeToError(EC: FileOrErr.getError()); |
8519 | if (IgnoreEmptyThinLTOIndexFile && !(*FileOrErr)->getBufferSize()) |
8520 | return nullptr; |
8521 | return getModuleSummaryIndex(Buffer: **FileOrErr); |
8522 | } |
8523 | |