File: | llvm/lib/Bitcode/Reader/BitcodeReader.cpp |
Warning: | line 2667, column 29 Called C++ object pointer is null |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | //===- BitcodeReader.cpp - Internal BitcodeReader implementation ----------===// | ||||
2 | // | ||||
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||||
4 | // See https://llvm.org/LICENSE.txt for license information. | ||||
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||||
6 | // | ||||
7 | //===----------------------------------------------------------------------===// | ||||
8 | |||||
9 | #include "llvm/Bitcode/BitcodeReader.h" | ||||
10 | #include "MetadataLoader.h" | ||||
11 | #include "ValueList.h" | ||||
12 | #include "llvm/ADT/APFloat.h" | ||||
13 | #include "llvm/ADT/APInt.h" | ||||
14 | #include "llvm/ADT/ArrayRef.h" | ||||
15 | #include "llvm/ADT/DenseMap.h" | ||||
16 | #include "llvm/ADT/Optional.h" | ||||
17 | #include "llvm/ADT/STLExtras.h" | ||||
18 | #include "llvm/ADT/SmallString.h" | ||||
19 | #include "llvm/ADT/SmallVector.h" | ||||
20 | #include "llvm/ADT/StringRef.h" | ||||
21 | #include "llvm/ADT/Triple.h" | ||||
22 | #include "llvm/ADT/Twine.h" | ||||
23 | #include "llvm/Bitcode/BitcodeCommon.h" | ||||
24 | #include "llvm/Bitcode/LLVMBitCodes.h" | ||||
25 | #include "llvm/Bitstream/BitstreamReader.h" | ||||
26 | #include "llvm/Config/llvm-config.h" | ||||
27 | #include "llvm/IR/Argument.h" | ||||
28 | #include "llvm/IR/Attributes.h" | ||||
29 | #include "llvm/IR/AutoUpgrade.h" | ||||
30 | #include "llvm/IR/BasicBlock.h" | ||||
31 | #include "llvm/IR/CallingConv.h" | ||||
32 | #include "llvm/IR/Comdat.h" | ||||
33 | #include "llvm/IR/Constant.h" | ||||
34 | #include "llvm/IR/Constants.h" | ||||
35 | #include "llvm/IR/DataLayout.h" | ||||
36 | #include "llvm/IR/DebugInfo.h" | ||||
37 | #include "llvm/IR/DebugInfoMetadata.h" | ||||
38 | #include "llvm/IR/DebugLoc.h" | ||||
39 | #include "llvm/IR/DerivedTypes.h" | ||||
40 | #include "llvm/IR/Function.h" | ||||
41 | #include "llvm/IR/GVMaterializer.h" | ||||
42 | #include "llvm/IR/GlobalAlias.h" | ||||
43 | #include "llvm/IR/GlobalIFunc.h" | ||||
44 | #include "llvm/IR/GlobalIndirectSymbol.h" | ||||
45 | #include "llvm/IR/GlobalObject.h" | ||||
46 | #include "llvm/IR/GlobalValue.h" | ||||
47 | #include "llvm/IR/GlobalVariable.h" | ||||
48 | #include "llvm/IR/InlineAsm.h" | ||||
49 | #include "llvm/IR/InstIterator.h" | ||||
50 | #include "llvm/IR/InstrTypes.h" | ||||
51 | #include "llvm/IR/Instruction.h" | ||||
52 | #include "llvm/IR/Instructions.h" | ||||
53 | #include "llvm/IR/Intrinsics.h" | ||||
54 | #include "llvm/IR/LLVMContext.h" | ||||
55 | #include "llvm/IR/Metadata.h" | ||||
56 | #include "llvm/IR/Module.h" | ||||
57 | #include "llvm/IR/ModuleSummaryIndex.h" | ||||
58 | #include "llvm/IR/Operator.h" | ||||
59 | #include "llvm/IR/Type.h" | ||||
60 | #include "llvm/IR/Value.h" | ||||
61 | #include "llvm/IR/Verifier.h" | ||||
62 | #include "llvm/Support/AtomicOrdering.h" | ||||
63 | #include "llvm/Support/Casting.h" | ||||
64 | #include "llvm/Support/CommandLine.h" | ||||
65 | #include "llvm/Support/Compiler.h" | ||||
66 | #include "llvm/Support/Debug.h" | ||||
67 | #include "llvm/Support/Error.h" | ||||
68 | #include "llvm/Support/ErrorHandling.h" | ||||
69 | #include "llvm/Support/ErrorOr.h" | ||||
70 | #include "llvm/Support/ManagedStatic.h" | ||||
71 | #include "llvm/Support/MathExtras.h" | ||||
72 | #include "llvm/Support/MemoryBuffer.h" | ||||
73 | #include "llvm/Support/raw_ostream.h" | ||||
74 | #include <algorithm> | ||||
75 | #include <cassert> | ||||
76 | #include <cstddef> | ||||
77 | #include <cstdint> | ||||
78 | #include <deque> | ||||
79 | #include <map> | ||||
80 | #include <memory> | ||||
81 | #include <set> | ||||
82 | #include <string> | ||||
83 | #include <system_error> | ||||
84 | #include <tuple> | ||||
85 | #include <utility> | ||||
86 | #include <vector> | ||||
87 | |||||
88 | using namespace llvm; | ||||
89 | |||||
90 | static cl::opt<bool> PrintSummaryGUIDs( | ||||
91 | "print-summary-global-ids", cl::init(false), cl::Hidden, | ||||
92 | cl::desc( | ||||
93 | "Print the global id for each value when reading the module summary")); | ||||
94 | |||||
95 | namespace { | ||||
96 | |||||
97 | enum { | ||||
98 | SWITCH_INST_MAGIC = 0x4B5 // May 2012 => 1205 => Hex | ||||
99 | }; | ||||
100 | |||||
101 | } // end anonymous namespace | ||||
102 | |||||
103 | static Error error(const Twine &Message) { | ||||
104 | return make_error<StringError>( | ||||
105 | Message, make_error_code(BitcodeError::CorruptedBitcode)); | ||||
106 | } | ||||
107 | |||||
108 | static Error hasInvalidBitcodeHeader(BitstreamCursor &Stream) { | ||||
109 | if (!Stream.canSkipToPos(4)) | ||||
110 | return createStringError(std::errc::illegal_byte_sequence, | ||||
111 | "file too small to contain bitcode header"); | ||||
112 | for (unsigned C : {'B', 'C'}) | ||||
113 | if (Expected<SimpleBitstreamCursor::word_t> Res = Stream.Read(8)) { | ||||
114 | if (Res.get() != C) | ||||
115 | return createStringError(std::errc::illegal_byte_sequence, | ||||
116 | "file doesn't start with bitcode header"); | ||||
117 | } else | ||||
118 | return Res.takeError(); | ||||
119 | for (unsigned C : {0x0, 0xC, 0xE, 0xD}) | ||||
120 | if (Expected<SimpleBitstreamCursor::word_t> Res = Stream.Read(4)) { | ||||
121 | if (Res.get() != C) | ||||
122 | return createStringError(std::errc::illegal_byte_sequence, | ||||
123 | "file doesn't start with bitcode header"); | ||||
124 | } else | ||||
125 | return Res.takeError(); | ||||
126 | return Error::success(); | ||||
127 | } | ||||
128 | |||||
129 | static Expected<BitstreamCursor> initStream(MemoryBufferRef Buffer) { | ||||
130 | const unsigned char *BufPtr = (const unsigned char *)Buffer.getBufferStart(); | ||||
131 | const unsigned char *BufEnd = BufPtr + Buffer.getBufferSize(); | ||||
132 | |||||
133 | if (Buffer.getBufferSize() & 3) | ||||
134 | return error("Invalid bitcode signature"); | ||||
135 | |||||
136 | // If we have a wrapper header, parse it and ignore the non-bc file contents. | ||||
137 | // The magic number is 0x0B17C0DE stored in little endian. | ||||
138 | if (isBitcodeWrapper(BufPtr, BufEnd)) | ||||
139 | if (SkipBitcodeWrapperHeader(BufPtr, BufEnd, true)) | ||||
140 | return error("Invalid bitcode wrapper header"); | ||||
141 | |||||
142 | BitstreamCursor Stream(ArrayRef<uint8_t>(BufPtr, BufEnd)); | ||||
143 | if (Error Err = hasInvalidBitcodeHeader(Stream)) | ||||
144 | return std::move(Err); | ||||
145 | |||||
146 | return std::move(Stream); | ||||
147 | } | ||||
148 | |||||
149 | /// Convert a string from a record into an std::string, return true on failure. | ||||
150 | template <typename StrTy> | ||||
151 | static bool convertToString(ArrayRef<uint64_t> Record, unsigned Idx, | ||||
152 | StrTy &Result) { | ||||
153 | if (Idx > Record.size()) | ||||
154 | return true; | ||||
155 | |||||
156 | Result.append(Record.begin() + Idx, Record.end()); | ||||
157 | return false; | ||||
158 | } | ||||
159 | |||||
160 | // Strip all the TBAA attachment for the module. | ||||
161 | static void stripTBAA(Module *M) { | ||||
162 | for (auto &F : *M) { | ||||
163 | if (F.isMaterializable()) | ||||
164 | continue; | ||||
165 | for (auto &I : instructions(F)) | ||||
166 | I.setMetadata(LLVMContext::MD_tbaa, nullptr); | ||||
167 | } | ||||
168 | } | ||||
169 | |||||
170 | /// Read the "IDENTIFICATION_BLOCK_ID" block, do some basic enforcement on the | ||||
171 | /// "epoch" encoded in the bitcode, and return the producer name if any. | ||||
172 | static Expected<std::string> readIdentificationBlock(BitstreamCursor &Stream) { | ||||
173 | if (Error Err = Stream.EnterSubBlock(bitc::IDENTIFICATION_BLOCK_ID)) | ||||
174 | return std::move(Err); | ||||
175 | |||||
176 | // Read all the records. | ||||
177 | SmallVector<uint64_t, 64> Record; | ||||
178 | |||||
179 | std::string ProducerIdentification; | ||||
180 | |||||
181 | while (true) { | ||||
182 | BitstreamEntry Entry; | ||||
183 | if (Expected<BitstreamEntry> Res = Stream.advance()) | ||||
184 | Entry = Res.get(); | ||||
185 | else | ||||
186 | return Res.takeError(); | ||||
187 | |||||
188 | switch (Entry.Kind) { | ||||
189 | default: | ||||
190 | case BitstreamEntry::Error: | ||||
191 | return error("Malformed block"); | ||||
192 | case BitstreamEntry::EndBlock: | ||||
193 | return ProducerIdentification; | ||||
194 | case BitstreamEntry::Record: | ||||
195 | // The interesting case. | ||||
196 | break; | ||||
197 | } | ||||
198 | |||||
199 | // Read a record. | ||||
200 | Record.clear(); | ||||
201 | Expected<unsigned> MaybeBitCode = Stream.readRecord(Entry.ID, Record); | ||||
202 | if (!MaybeBitCode) | ||||
203 | return MaybeBitCode.takeError(); | ||||
204 | switch (MaybeBitCode.get()) { | ||||
205 | default: // Default behavior: reject | ||||
206 | return error("Invalid value"); | ||||
207 | case bitc::IDENTIFICATION_CODE_STRING: // IDENTIFICATION: [strchr x N] | ||||
208 | convertToString(Record, 0, ProducerIdentification); | ||||
209 | break; | ||||
210 | case bitc::IDENTIFICATION_CODE_EPOCH: { // EPOCH: [epoch#] | ||||
211 | unsigned epoch = (unsigned)Record[0]; | ||||
212 | if (epoch != bitc::BITCODE_CURRENT_EPOCH) { | ||||
213 | return error( | ||||
214 | Twine("Incompatible epoch: Bitcode '") + Twine(epoch) + | ||||
215 | "' vs current: '" + Twine(bitc::BITCODE_CURRENT_EPOCH) + "'"); | ||||
216 | } | ||||
217 | } | ||||
218 | } | ||||
219 | } | ||||
220 | } | ||||
221 | |||||
222 | static Expected<std::string> readIdentificationCode(BitstreamCursor &Stream) { | ||||
223 | // We expect a number of well-defined blocks, though we don't necessarily | ||||
224 | // need to understand them all. | ||||
225 | while (true) { | ||||
226 | if (Stream.AtEndOfStream()) | ||||
227 | return ""; | ||||
228 | |||||
229 | BitstreamEntry Entry; | ||||
230 | if (Expected<BitstreamEntry> Res = Stream.advance()) | ||||
231 | Entry = std::move(Res.get()); | ||||
232 | else | ||||
233 | return Res.takeError(); | ||||
234 | |||||
235 | switch (Entry.Kind) { | ||||
236 | case BitstreamEntry::EndBlock: | ||||
237 | case BitstreamEntry::Error: | ||||
238 | return error("Malformed block"); | ||||
239 | |||||
240 | case BitstreamEntry::SubBlock: | ||||
241 | if (Entry.ID == bitc::IDENTIFICATION_BLOCK_ID) | ||||
242 | return readIdentificationBlock(Stream); | ||||
243 | |||||
244 | // Ignore other sub-blocks. | ||||
245 | if (Error Err = Stream.SkipBlock()) | ||||
246 | return std::move(Err); | ||||
247 | continue; | ||||
248 | case BitstreamEntry::Record: | ||||
249 | if (Expected<unsigned> Skipped = Stream.skipRecord(Entry.ID)) | ||||
250 | continue; | ||||
251 | else | ||||
252 | return Skipped.takeError(); | ||||
253 | } | ||||
254 | } | ||||
255 | } | ||||
256 | |||||
257 | static Expected<bool> hasObjCCategoryInModule(BitstreamCursor &Stream) { | ||||
258 | if (Error Err = Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID)) | ||||
259 | return std::move(Err); | ||||
260 | |||||
261 | SmallVector<uint64_t, 64> Record; | ||||
262 | // Read all the records for this module. | ||||
263 | |||||
264 | while (true) { | ||||
265 | Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks(); | ||||
266 | if (!MaybeEntry) | ||||
267 | return MaybeEntry.takeError(); | ||||
268 | BitstreamEntry Entry = MaybeEntry.get(); | ||||
269 | |||||
270 | switch (Entry.Kind) { | ||||
271 | case BitstreamEntry::SubBlock: // Handled for us already. | ||||
272 | case BitstreamEntry::Error: | ||||
273 | return error("Malformed block"); | ||||
274 | case BitstreamEntry::EndBlock: | ||||
275 | return false; | ||||
276 | case BitstreamEntry::Record: | ||||
277 | // The interesting case. | ||||
278 | break; | ||||
279 | } | ||||
280 | |||||
281 | // Read a record. | ||||
282 | Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record); | ||||
283 | if (!MaybeRecord) | ||||
284 | return MaybeRecord.takeError(); | ||||
285 | switch (MaybeRecord.get()) { | ||||
286 | default: | ||||
287 | break; // Default behavior, ignore unknown content. | ||||
288 | case bitc::MODULE_CODE_SECTIONNAME: { // SECTIONNAME: [strchr x N] | ||||
289 | std::string S; | ||||
290 | if (convertToString(Record, 0, S)) | ||||
291 | return error("Invalid record"); | ||||
292 | // Check for the i386 and other (x86_64, ARM) conventions | ||||
293 | if (S.find("__DATA,__objc_catlist") != std::string::npos || | ||||
294 | S.find("__OBJC,__category") != std::string::npos) | ||||
295 | return true; | ||||
296 | break; | ||||
297 | } | ||||
298 | } | ||||
299 | Record.clear(); | ||||
300 | } | ||||
301 | llvm_unreachable("Exit infinite loop")::llvm::llvm_unreachable_internal("Exit infinite loop", "/build/llvm-toolchain-snapshot-13~++20210621111111+acefe0eaaf82/llvm/lib/Bitcode/Reader/BitcodeReader.cpp" , 301); | ||||
302 | } | ||||
303 | |||||
304 | static Expected<bool> hasObjCCategory(BitstreamCursor &Stream) { | ||||
305 | // We expect a number of well-defined blocks, though we don't necessarily | ||||
306 | // need to understand them all. | ||||
307 | while (true) { | ||||
308 | BitstreamEntry Entry; | ||||
309 | if (Expected<BitstreamEntry> Res = Stream.advance()) | ||||
310 | Entry = std::move(Res.get()); | ||||
311 | else | ||||
312 | return Res.takeError(); | ||||
313 | |||||
314 | switch (Entry.Kind) { | ||||
315 | case BitstreamEntry::Error: | ||||
316 | return error("Malformed block"); | ||||
317 | case BitstreamEntry::EndBlock: | ||||
318 | return false; | ||||
319 | |||||
320 | case BitstreamEntry::SubBlock: | ||||
321 | if (Entry.ID == bitc::MODULE_BLOCK_ID) | ||||
322 | return hasObjCCategoryInModule(Stream); | ||||
323 | |||||
324 | // Ignore other sub-blocks. | ||||
325 | if (Error Err = Stream.SkipBlock()) | ||||
326 | return std::move(Err); | ||||
327 | continue; | ||||
328 | |||||
329 | case BitstreamEntry::Record: | ||||
330 | if (Expected<unsigned> Skipped = Stream.skipRecord(Entry.ID)) | ||||
331 | continue; | ||||
332 | else | ||||
333 | return Skipped.takeError(); | ||||
334 | } | ||||
335 | } | ||||
336 | } | ||||
337 | |||||
338 | static Expected<std::string> readModuleTriple(BitstreamCursor &Stream) { | ||||
339 | if (Error Err = Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID)) | ||||
340 | return std::move(Err); | ||||
341 | |||||
342 | SmallVector<uint64_t, 64> Record; | ||||
343 | |||||
344 | std::string Triple; | ||||
345 | |||||
346 | // Read all the records for this module. | ||||
347 | while (true) { | ||||
348 | Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks(); | ||||
349 | if (!MaybeEntry) | ||||
350 | return MaybeEntry.takeError(); | ||||
351 | BitstreamEntry Entry = MaybeEntry.get(); | ||||
352 | |||||
353 | switch (Entry.Kind) { | ||||
354 | case BitstreamEntry::SubBlock: // Handled for us already. | ||||
355 | case BitstreamEntry::Error: | ||||
356 | return error("Malformed block"); | ||||
357 | case BitstreamEntry::EndBlock: | ||||
358 | return Triple; | ||||
359 | case BitstreamEntry::Record: | ||||
360 | // The interesting case. | ||||
361 | break; | ||||
362 | } | ||||
363 | |||||
364 | // Read a record. | ||||
365 | Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record); | ||||
366 | if (!MaybeRecord) | ||||
367 | return MaybeRecord.takeError(); | ||||
368 | switch (MaybeRecord.get()) { | ||||
369 | default: break; // Default behavior, ignore unknown content. | ||||
370 | case bitc::MODULE_CODE_TRIPLE: { // TRIPLE: [strchr x N] | ||||
371 | std::string S; | ||||
372 | if (convertToString(Record, 0, S)) | ||||
373 | return error("Invalid record"); | ||||
374 | Triple = S; | ||||
375 | break; | ||||
376 | } | ||||
377 | } | ||||
378 | Record.clear(); | ||||
379 | } | ||||
380 | llvm_unreachable("Exit infinite loop")::llvm::llvm_unreachable_internal("Exit infinite loop", "/build/llvm-toolchain-snapshot-13~++20210621111111+acefe0eaaf82/llvm/lib/Bitcode/Reader/BitcodeReader.cpp" , 380); | ||||
381 | } | ||||
382 | |||||
383 | static Expected<std::string> readTriple(BitstreamCursor &Stream) { | ||||
384 | // We expect a number of well-defined blocks, though we don't necessarily | ||||
385 | // need to understand them all. | ||||
386 | while (true) { | ||||
387 | Expected<BitstreamEntry> MaybeEntry = Stream.advance(); | ||||
388 | if (!MaybeEntry) | ||||
389 | return MaybeEntry.takeError(); | ||||
390 | BitstreamEntry Entry = MaybeEntry.get(); | ||||
391 | |||||
392 | switch (Entry.Kind) { | ||||
393 | case BitstreamEntry::Error: | ||||
394 | return error("Malformed block"); | ||||
395 | case BitstreamEntry::EndBlock: | ||||
396 | return ""; | ||||
397 | |||||
398 | case BitstreamEntry::SubBlock: | ||||
399 | if (Entry.ID == bitc::MODULE_BLOCK_ID) | ||||
400 | return readModuleTriple(Stream); | ||||
401 | |||||
402 | // Ignore other sub-blocks. | ||||
403 | if (Error Err = Stream.SkipBlock()) | ||||
404 | return std::move(Err); | ||||
405 | continue; | ||||
406 | |||||
407 | case BitstreamEntry::Record: | ||||
408 | if (llvm::Expected<unsigned> Skipped = Stream.skipRecord(Entry.ID)) | ||||
409 | continue; | ||||
410 | else | ||||
411 | return Skipped.takeError(); | ||||
412 | } | ||||
413 | } | ||||
414 | } | ||||
415 | |||||
416 | namespace { | ||||
417 | |||||
418 | class BitcodeReaderBase { | ||||
419 | protected: | ||||
420 | BitcodeReaderBase(BitstreamCursor Stream, StringRef Strtab) | ||||
421 | : Stream(std::move(Stream)), Strtab(Strtab) { | ||||
422 | this->Stream.setBlockInfo(&BlockInfo); | ||||
423 | } | ||||
424 | |||||
425 | BitstreamBlockInfo BlockInfo; | ||||
426 | BitstreamCursor Stream; | ||||
427 | StringRef Strtab; | ||||
428 | |||||
429 | /// In version 2 of the bitcode we store names of global values and comdats in | ||||
430 | /// a string table rather than in the VST. | ||||
431 | bool UseStrtab = false; | ||||
432 | |||||
433 | Expected<unsigned> parseVersionRecord(ArrayRef<uint64_t> Record); | ||||
434 | |||||
435 | /// If this module uses a string table, pop the reference to the string table | ||||
436 | /// and return the referenced string and the rest of the record. Otherwise | ||||
437 | /// just return the record itself. | ||||
438 | std::pair<StringRef, ArrayRef<uint64_t>> | ||||
439 | readNameFromStrtab(ArrayRef<uint64_t> Record); | ||||
440 | |||||
441 | bool readBlockInfo(); | ||||
442 | |||||
443 | // Contains an arbitrary and optional string identifying the bitcode producer | ||||
444 | std::string ProducerIdentification; | ||||
445 | |||||
446 | Error error(const Twine &Message); | ||||
447 | }; | ||||
448 | |||||
449 | } // end anonymous namespace | ||||
450 | |||||
451 | Error BitcodeReaderBase::error(const Twine &Message) { | ||||
452 | std::string FullMsg = Message.str(); | ||||
453 | if (!ProducerIdentification.empty()) | ||||
454 | FullMsg += " (Producer: '" + ProducerIdentification + "' Reader: 'LLVM " + | ||||
455 | LLVM_VERSION_STRING"13.0.0" "')"; | ||||
456 | return ::error(FullMsg); | ||||
457 | } | ||||
458 | |||||
459 | Expected<unsigned> | ||||
460 | BitcodeReaderBase::parseVersionRecord(ArrayRef<uint64_t> Record) { | ||||
461 | if (Record.empty()) | ||||
462 | return error("Invalid record"); | ||||
463 | unsigned ModuleVersion = Record[0]; | ||||
464 | if (ModuleVersion > 2) | ||||
465 | return error("Invalid value"); | ||||
466 | UseStrtab = ModuleVersion >= 2; | ||||
467 | return ModuleVersion; | ||||
468 | } | ||||
469 | |||||
470 | std::pair<StringRef, ArrayRef<uint64_t>> | ||||
471 | BitcodeReaderBase::readNameFromStrtab(ArrayRef<uint64_t> Record) { | ||||
472 | if (!UseStrtab) | ||||
473 | return {"", Record}; | ||||
474 | // Invalid reference. Let the caller complain about the record being empty. | ||||
475 | if (Record[0] + Record[1] > Strtab.size()) | ||||
476 | return {"", {}}; | ||||
477 | return {StringRef(Strtab.data() + Record[0], Record[1]), Record.slice(2)}; | ||||
478 | } | ||||
479 | |||||
480 | namespace { | ||||
481 | |||||
482 | class BitcodeReader : public BitcodeReaderBase, public GVMaterializer { | ||||
483 | LLVMContext &Context; | ||||
484 | Module *TheModule = nullptr; | ||||
485 | // Next offset to start scanning for lazy parsing of function bodies. | ||||
486 | uint64_t NextUnreadBit = 0; | ||||
487 | // Last function offset found in the VST. | ||||
488 | uint64_t LastFunctionBlockBit = 0; | ||||
489 | bool SeenValueSymbolTable = false; | ||||
490 | uint64_t VSTOffset = 0; | ||||
491 | |||||
492 | std::vector<std::string> SectionTable; | ||||
493 | std::vector<std::string> GCTable; | ||||
494 | |||||
495 | std::vector<Type*> TypeList; | ||||
496 | DenseMap<Function *, FunctionType *> FunctionTypes; | ||||
497 | BitcodeReaderValueList ValueList; | ||||
498 | Optional<MetadataLoader> MDLoader; | ||||
499 | std::vector<Comdat *> ComdatList; | ||||
500 | SmallVector<Instruction *, 64> InstructionList; | ||||
501 | |||||
502 | std::vector<std::pair<GlobalVariable *, unsigned>> GlobalInits; | ||||
503 | std::vector<std::pair<GlobalIndirectSymbol *, unsigned>> IndirectSymbolInits; | ||||
504 | std::vector<std::pair<Function *, unsigned>> FunctionPrefixes; | ||||
505 | std::vector<std::pair<Function *, unsigned>> FunctionPrologues; | ||||
506 | std::vector<std::pair<Function *, unsigned>> FunctionPersonalityFns; | ||||
507 | |||||
508 | /// The set of attributes by index. Index zero in the file is for null, and | ||||
509 | /// is thus not represented here. As such all indices are off by one. | ||||
510 | std::vector<AttributeList> MAttributes; | ||||
511 | |||||
512 | /// The set of attribute groups. | ||||
513 | std::map<unsigned, AttributeList> MAttributeGroups; | ||||
514 | |||||
515 | /// While parsing a function body, this is a list of the basic blocks for the | ||||
516 | /// function. | ||||
517 | std::vector<BasicBlock*> FunctionBBs; | ||||
518 | |||||
519 | // When reading the module header, this list is populated with functions that | ||||
520 | // have bodies later in the file. | ||||
521 | std::vector<Function*> FunctionsWithBodies; | ||||
522 | |||||
523 | // When intrinsic functions are encountered which require upgrading they are | ||||
524 | // stored here with their replacement function. | ||||
525 | using UpdatedIntrinsicMap = DenseMap<Function *, Function *>; | ||||
526 | UpdatedIntrinsicMap UpgradedIntrinsics; | ||||
527 | // Intrinsics which were remangled because of types rename | ||||
528 | UpdatedIntrinsicMap RemangledIntrinsics; | ||||
529 | |||||
530 | // Several operations happen after the module header has been read, but | ||||
531 | // before function bodies are processed. This keeps track of whether | ||||
532 | // we've done this yet. | ||||
533 | bool SeenFirstFunctionBody = false; | ||||
534 | |||||
535 | /// When function bodies are initially scanned, this map contains info about | ||||
536 | /// where to find deferred function body in the stream. | ||||
537 | DenseMap<Function*, uint64_t> DeferredFunctionInfo; | ||||
538 | |||||
539 | /// When Metadata block is initially scanned when parsing the module, we may | ||||
540 | /// choose to defer parsing of the metadata. This vector contains info about | ||||
541 | /// which Metadata blocks are deferred. | ||||
542 | std::vector<uint64_t> DeferredMetadataInfo; | ||||
543 | |||||
544 | /// These are basic blocks forward-referenced by block addresses. They are | ||||
545 | /// inserted lazily into functions when they're loaded. The basic block ID is | ||||
546 | /// its index into the vector. | ||||
547 | DenseMap<Function *, std::vector<BasicBlock *>> BasicBlockFwdRefs; | ||||
548 | std::deque<Function *> BasicBlockFwdRefQueue; | ||||
549 | |||||
550 | /// Indicates that we are using a new encoding for instruction operands where | ||||
551 | /// most operands in the current FUNCTION_BLOCK are encoded relative to the | ||||
552 | /// instruction number, for a more compact encoding. Some instruction | ||||
553 | /// operands are not relative to the instruction ID: basic block numbers, and | ||||
554 | /// types. Once the old style function blocks have been phased out, we would | ||||
555 | /// not need this flag. | ||||
556 | bool UseRelativeIDs = false; | ||||
557 | |||||
558 | /// True if all functions will be materialized, negating the need to process | ||||
559 | /// (e.g.) blockaddress forward references. | ||||
560 | bool WillMaterializeAllForwardRefs = false; | ||||
561 | |||||
562 | bool StripDebugInfo = false; | ||||
563 | TBAAVerifier TBAAVerifyHelper; | ||||
564 | |||||
565 | std::vector<std::string> BundleTags; | ||||
566 | SmallVector<SyncScope::ID, 8> SSIDs; | ||||
567 | |||||
568 | public: | ||||
569 | BitcodeReader(BitstreamCursor Stream, StringRef Strtab, | ||||
570 | StringRef ProducerIdentification, LLVMContext &Context); | ||||
571 | |||||
572 | Error materializeForwardReferencedFunctions(); | ||||
573 | |||||
574 | Error materialize(GlobalValue *GV) override; | ||||
575 | Error materializeModule() override; | ||||
576 | std::vector<StructType *> getIdentifiedStructTypes() const override; | ||||
577 | |||||
578 | /// Main interface to parsing a bitcode buffer. | ||||
579 | /// \returns true if an error occurred. | ||||
580 | Error parseBitcodeInto( | ||||
581 | Module *M, bool ShouldLazyLoadMetadata = false, bool IsImporting = false, | ||||
582 | DataLayoutCallbackTy DataLayoutCallback = [](StringRef) { return None; }); | ||||
583 | |||||
584 | static uint64_t decodeSignRotatedValue(uint64_t V); | ||||
585 | |||||
586 | /// Materialize any deferred Metadata block. | ||||
587 | Error materializeMetadata() override; | ||||
588 | |||||
589 | void setStripDebugInfo() override; | ||||
590 | |||||
591 | private: | ||||
592 | std::vector<StructType *> IdentifiedStructTypes; | ||||
593 | StructType *createIdentifiedStructType(LLVMContext &Context, StringRef Name); | ||||
594 | StructType *createIdentifiedStructType(LLVMContext &Context); | ||||
595 | |||||
596 | Type *getTypeByID(unsigned ID); | ||||
597 | |||||
598 | Value *getFnValueByID(unsigned ID, Type *Ty) { | ||||
599 | if (Ty && Ty->isMetadataTy()) | ||||
600 | return MetadataAsValue::get(Ty->getContext(), getFnMetadataByID(ID)); | ||||
601 | return ValueList.getValueFwdRef(ID, Ty); | ||||
602 | } | ||||
603 | |||||
604 | Metadata *getFnMetadataByID(unsigned ID) { | ||||
605 | return MDLoader->getMetadataFwdRefOrLoad(ID); | ||||
606 | } | ||||
607 | |||||
608 | BasicBlock *getBasicBlock(unsigned ID) const { | ||||
609 | if (ID >= FunctionBBs.size()) return nullptr; // Invalid ID | ||||
610 | return FunctionBBs[ID]; | ||||
611 | } | ||||
612 | |||||
613 | AttributeList getAttributes(unsigned i) const { | ||||
614 | if (i-1 < MAttributes.size()) | ||||
615 | return MAttributes[i-1]; | ||||
616 | return AttributeList(); | ||||
617 | } | ||||
618 | |||||
619 | /// Read a value/type pair out of the specified record from slot 'Slot'. | ||||
620 | /// Increment Slot past the number of slots used in the record. Return true on | ||||
621 | /// failure. | ||||
622 | bool getValueTypePair(const SmallVectorImpl<uint64_t> &Record, unsigned &Slot, | ||||
623 | unsigned InstNum, Value *&ResVal) { | ||||
624 | if (Slot == Record.size()) return true; | ||||
625 | unsigned ValNo = (unsigned)Record[Slot++]; | ||||
626 | // Adjust the ValNo, if it was encoded relative to the InstNum. | ||||
627 | if (UseRelativeIDs) | ||||
628 | ValNo = InstNum - ValNo; | ||||
629 | if (ValNo < InstNum) { | ||||
630 | // If this is not a forward reference, just return the value we already | ||||
631 | // have. | ||||
632 | ResVal = getFnValueByID(ValNo, nullptr); | ||||
633 | return ResVal == nullptr; | ||||
634 | } | ||||
635 | if (Slot == Record.size()) | ||||
636 | return true; | ||||
637 | |||||
638 | unsigned TypeNo = (unsigned)Record[Slot++]; | ||||
639 | ResVal = getFnValueByID(ValNo, getTypeByID(TypeNo)); | ||||
640 | return ResVal == nullptr; | ||||
641 | } | ||||
642 | |||||
643 | /// Read a value out of the specified record from slot 'Slot'. Increment Slot | ||||
644 | /// past the number of slots used by the value in the record. Return true if | ||||
645 | /// there is an error. | ||||
646 | bool popValue(const SmallVectorImpl<uint64_t> &Record, unsigned &Slot, | ||||
647 | unsigned InstNum, Type *Ty, Value *&ResVal) { | ||||
648 | if (getValue(Record, Slot, InstNum, Ty, ResVal)) | ||||
649 | return true; | ||||
650 | // All values currently take a single record slot. | ||||
651 | ++Slot; | ||||
652 | return false; | ||||
653 | } | ||||
654 | |||||
655 | /// Like popValue, but does not increment the Slot number. | ||||
656 | bool getValue(const SmallVectorImpl<uint64_t> &Record, unsigned Slot, | ||||
657 | unsigned InstNum, Type *Ty, Value *&ResVal) { | ||||
658 | ResVal = getValue(Record, Slot, InstNum, Ty); | ||||
659 | return ResVal == nullptr; | ||||
660 | } | ||||
661 | |||||
662 | /// Version of getValue that returns ResVal directly, or 0 if there is an | ||||
663 | /// error. | ||||
664 | Value *getValue(const SmallVectorImpl<uint64_t> &Record, unsigned Slot, | ||||
665 | unsigned InstNum, Type *Ty) { | ||||
666 | if (Slot == Record.size()) return nullptr; | ||||
667 | unsigned ValNo = (unsigned)Record[Slot]; | ||||
668 | // Adjust the ValNo, if it was encoded relative to the InstNum. | ||||
669 | if (UseRelativeIDs) | ||||
670 | ValNo = InstNum - ValNo; | ||||
671 | return getFnValueByID(ValNo, Ty); | ||||
672 | } | ||||
673 | |||||
674 | /// Like getValue, but decodes signed VBRs. | ||||
675 | Value *getValueSigned(const SmallVectorImpl<uint64_t> &Record, unsigned Slot, | ||||
676 | unsigned InstNum, Type *Ty) { | ||||
677 | if (Slot == Record.size()) return nullptr; | ||||
678 | unsigned ValNo = (unsigned)decodeSignRotatedValue(Record[Slot]); | ||||
679 | // Adjust the ValNo, if it was encoded relative to the InstNum. | ||||
680 | if (UseRelativeIDs) | ||||
681 | ValNo = InstNum - ValNo; | ||||
682 | return getFnValueByID(ValNo, Ty); | ||||
683 | } | ||||
684 | |||||
685 | /// Upgrades old-style typeless byval or sret attributes by adding the | ||||
686 | /// corresponding argument's pointee type. | ||||
687 | void propagateByValSRetTypes(CallBase *CB, ArrayRef<Type *> ArgsTys); | ||||
688 | |||||
689 | /// Converts alignment exponent (i.e. power of two (or zero)) to the | ||||
690 | /// corresponding alignment to use. If alignment is too large, returns | ||||
691 | /// a corresponding error code. | ||||
692 | Error parseAlignmentValue(uint64_t Exponent, MaybeAlign &Alignment); | ||||
693 | Error parseAttrKind(uint64_t Code, Attribute::AttrKind *Kind); | ||||
694 | Error parseModule( | ||||
695 | uint64_t ResumeBit, bool ShouldLazyLoadMetadata = false, | ||||
696 | DataLayoutCallbackTy DataLayoutCallback = [](StringRef) { return None; }); | ||||
697 | |||||
698 | Error parseComdatRecord(ArrayRef<uint64_t> Record); | ||||
699 | Error parseGlobalVarRecord(ArrayRef<uint64_t> Record); | ||||
700 | Error parseFunctionRecord(ArrayRef<uint64_t> Record); | ||||
701 | Error parseGlobalIndirectSymbolRecord(unsigned BitCode, | ||||
702 | ArrayRef<uint64_t> Record); | ||||
703 | |||||
704 | Error parseAttributeBlock(); | ||||
705 | Error parseAttributeGroupBlock(); | ||||
706 | Error parseTypeTable(); | ||||
707 | Error parseTypeTableBody(); | ||||
708 | Error parseOperandBundleTags(); | ||||
709 | Error parseSyncScopeNames(); | ||||
710 | |||||
711 | Expected<Value *> recordValue(SmallVectorImpl<uint64_t> &Record, | ||||
712 | unsigned NameIndex, Triple &TT); | ||||
713 | void setDeferredFunctionInfo(unsigned FuncBitcodeOffsetDelta, Function *F, | ||||
714 | ArrayRef<uint64_t> Record); | ||||
715 | Error parseValueSymbolTable(uint64_t Offset = 0); | ||||
716 | Error parseGlobalValueSymbolTable(); | ||||
717 | Error parseConstants(); | ||||
718 | Error rememberAndSkipFunctionBodies(); | ||||
719 | Error rememberAndSkipFunctionBody(); | ||||
720 | /// Save the positions of the Metadata blocks and skip parsing the blocks. | ||||
721 | Error rememberAndSkipMetadata(); | ||||
722 | Error typeCheckLoadStoreInst(Type *ValType, Type *PtrType); | ||||
723 | Error parseFunctionBody(Function *F); | ||||
724 | Error globalCleanup(); | ||||
725 | Error resolveGlobalAndIndirectSymbolInits(); | ||||
726 | Error parseUseLists(); | ||||
727 | Error findFunctionInStream( | ||||
728 | Function *F, | ||||
729 | DenseMap<Function *, uint64_t>::iterator DeferredFunctionInfoIterator); | ||||
730 | |||||
731 | SyncScope::ID getDecodedSyncScopeID(unsigned Val); | ||||
732 | }; | ||||
733 | |||||
734 | /// Class to manage reading and parsing function summary index bitcode | ||||
735 | /// files/sections. | ||||
736 | class ModuleSummaryIndexBitcodeReader : public BitcodeReaderBase { | ||||
737 | /// The module index built during parsing. | ||||
738 | ModuleSummaryIndex &TheIndex; | ||||
739 | |||||
740 | /// Indicates whether we have encountered a global value summary section | ||||
741 | /// yet during parsing. | ||||
742 | bool SeenGlobalValSummary = false; | ||||
743 | |||||
744 | /// Indicates whether we have already parsed the VST, used for error checking. | ||||
745 | bool SeenValueSymbolTable = false; | ||||
746 | |||||
747 | /// Set to the offset of the VST recorded in the MODULE_CODE_VSTOFFSET record. | ||||
748 | /// Used to enable on-demand parsing of the VST. | ||||
749 | uint64_t VSTOffset = 0; | ||||
750 | |||||
751 | // Map to save ValueId to ValueInfo association that was recorded in the | ||||
752 | // ValueSymbolTable. It is used after the VST is parsed to convert | ||||
753 | // call graph edges read from the function summary from referencing | ||||
754 | // callees by their ValueId to using the ValueInfo instead, which is how | ||||
755 | // they are recorded in the summary index being built. | ||||
756 | // We save a GUID which refers to the same global as the ValueInfo, but | ||||
757 | // ignoring the linkage, i.e. for values other than local linkage they are | ||||
758 | // identical. | ||||
759 | DenseMap<unsigned, std::pair<ValueInfo, GlobalValue::GUID>> | ||||
760 | ValueIdToValueInfoMap; | ||||
761 | |||||
762 | /// Map populated during module path string table parsing, from the | ||||
763 | /// module ID to a string reference owned by the index's module | ||||
764 | /// path string table, used to correlate with combined index | ||||
765 | /// summary records. | ||||
766 | DenseMap<uint64_t, StringRef> ModuleIdMap; | ||||
767 | |||||
768 | /// Original source file name recorded in a bitcode record. | ||||
769 | std::string SourceFileName; | ||||
770 | |||||
771 | /// The string identifier given to this module by the client, normally the | ||||
772 | /// path to the bitcode file. | ||||
773 | StringRef ModulePath; | ||||
774 | |||||
775 | /// For per-module summary indexes, the unique numerical identifier given to | ||||
776 | /// this module by the client. | ||||
777 | unsigned ModuleId; | ||||
778 | |||||
779 | public: | ||||
780 | ModuleSummaryIndexBitcodeReader(BitstreamCursor Stream, StringRef Strtab, | ||||
781 | ModuleSummaryIndex &TheIndex, | ||||
782 | StringRef ModulePath, unsigned ModuleId); | ||||
783 | |||||
784 | Error parseModule(); | ||||
785 | |||||
786 | private: | ||||
787 | void setValueGUID(uint64_t ValueID, StringRef ValueName, | ||||
788 | GlobalValue::LinkageTypes Linkage, | ||||
789 | StringRef SourceFileName); | ||||
790 | Error parseValueSymbolTable( | ||||
791 | uint64_t Offset, | ||||
792 | DenseMap<unsigned, GlobalValue::LinkageTypes> &ValueIdToLinkageMap); | ||||
793 | std::vector<ValueInfo> makeRefList(ArrayRef<uint64_t> Record); | ||||
794 | std::vector<FunctionSummary::EdgeTy> makeCallList(ArrayRef<uint64_t> Record, | ||||
795 | bool IsOldProfileFormat, | ||||
796 | bool HasProfile, | ||||
797 | bool HasRelBF); | ||||
798 | Error parseEntireSummary(unsigned ID); | ||||
799 | Error parseModuleStringTable(); | ||||
800 | void parseTypeIdCompatibleVtableSummaryRecord(ArrayRef<uint64_t> Record); | ||||
801 | void parseTypeIdCompatibleVtableInfo(ArrayRef<uint64_t> Record, size_t &Slot, | ||||
802 | TypeIdCompatibleVtableInfo &TypeId); | ||||
803 | std::vector<FunctionSummary::ParamAccess> | ||||
804 | parseParamAccesses(ArrayRef<uint64_t> Record); | ||||
805 | |||||
806 | std::pair<ValueInfo, GlobalValue::GUID> | ||||
807 | getValueInfoFromValueId(unsigned ValueId); | ||||
808 | |||||
809 | void addThisModule(); | ||||
810 | ModuleSummaryIndex::ModuleInfo *getThisModule(); | ||||
811 | }; | ||||
812 | |||||
813 | } // end anonymous namespace | ||||
814 | |||||
815 | std::error_code llvm::errorToErrorCodeAndEmitErrors(LLVMContext &Ctx, | ||||
816 | Error Err) { | ||||
817 | if (Err) { | ||||
818 | std::error_code EC; | ||||
819 | handleAllErrors(std::move(Err), [&](ErrorInfoBase &EIB) { | ||||
820 | EC = EIB.convertToErrorCode(); | ||||
821 | Ctx.emitError(EIB.message()); | ||||
822 | }); | ||||
823 | return EC; | ||||
824 | } | ||||
825 | return std::error_code(); | ||||
826 | } | ||||
827 | |||||
828 | BitcodeReader::BitcodeReader(BitstreamCursor Stream, StringRef Strtab, | ||||
829 | StringRef ProducerIdentification, | ||||
830 | LLVMContext &Context) | ||||
831 | : BitcodeReaderBase(std::move(Stream), Strtab), Context(Context), | ||||
832 | ValueList(Context, Stream.SizeInBytes()) { | ||||
833 | this->ProducerIdentification = std::string(ProducerIdentification); | ||||
834 | } | ||||
835 | |||||
836 | Error BitcodeReader::materializeForwardReferencedFunctions() { | ||||
837 | if (WillMaterializeAllForwardRefs) | ||||
838 | return Error::success(); | ||||
839 | |||||
840 | // Prevent recursion. | ||||
841 | WillMaterializeAllForwardRefs = true; | ||||
842 | |||||
843 | while (!BasicBlockFwdRefQueue.empty()) { | ||||
844 | Function *F = BasicBlockFwdRefQueue.front(); | ||||
845 | BasicBlockFwdRefQueue.pop_front(); | ||||
846 | assert(F && "Expected valid function")(static_cast <bool> (F && "Expected valid function" ) ? void (0) : __assert_fail ("F && \"Expected valid function\"" , "/build/llvm-toolchain-snapshot-13~++20210621111111+acefe0eaaf82/llvm/lib/Bitcode/Reader/BitcodeReader.cpp" , 846, __extension__ __PRETTY_FUNCTION__)); | ||||
847 | if (!BasicBlockFwdRefs.count(F)) | ||||
848 | // Already materialized. | ||||
849 | continue; | ||||
850 | |||||
851 | // Check for a function that isn't materializable to prevent an infinite | ||||
852 | // loop. When parsing a blockaddress stored in a global variable, there | ||||
853 | // isn't a trivial way to check if a function will have a body without a | ||||
854 | // linear search through FunctionsWithBodies, so just check it here. | ||||
855 | if (!F->isMaterializable()) | ||||
856 | return error("Never resolved function from blockaddress"); | ||||
857 | |||||
858 | // Try to materialize F. | ||||
859 | if (Error Err = materialize(F)) | ||||
860 | return Err; | ||||
861 | } | ||||
862 | assert(BasicBlockFwdRefs.empty() && "Function missing from queue")(static_cast <bool> (BasicBlockFwdRefs.empty() && "Function missing from queue") ? void (0) : __assert_fail ("BasicBlockFwdRefs.empty() && \"Function missing from queue\"" , "/build/llvm-toolchain-snapshot-13~++20210621111111+acefe0eaaf82/llvm/lib/Bitcode/Reader/BitcodeReader.cpp" , 862, __extension__ __PRETTY_FUNCTION__)); | ||||
863 | |||||
864 | // Reset state. | ||||
865 | WillMaterializeAllForwardRefs = false; | ||||
866 | return Error::success(); | ||||
867 | } | ||||
868 | |||||
869 | //===----------------------------------------------------------------------===// | ||||
870 | // Helper functions to implement forward reference resolution, etc. | ||||
871 | //===----------------------------------------------------------------------===// | ||||
872 | |||||
873 | static bool hasImplicitComdat(size_t Val) { | ||||
874 | switch (Val) { | ||||
875 | default: | ||||
876 | return false; | ||||
877 | case 1: // Old WeakAnyLinkage | ||||
878 | case 4: // Old LinkOnceAnyLinkage | ||||
879 | case 10: // Old WeakODRLinkage | ||||
880 | case 11: // Old LinkOnceODRLinkage | ||||
881 | return true; | ||||
882 | } | ||||
883 | } | ||||
884 | |||||
885 | static GlobalValue::LinkageTypes getDecodedLinkage(unsigned Val) { | ||||
886 | switch (Val) { | ||||
887 | default: // Map unknown/new linkages to external | ||||
888 | case 0: | ||||
889 | return GlobalValue::ExternalLinkage; | ||||
890 | case 2: | ||||
891 | return GlobalValue::AppendingLinkage; | ||||
892 | case 3: | ||||
893 | return GlobalValue::InternalLinkage; | ||||
894 | case 5: | ||||
895 | return GlobalValue::ExternalLinkage; // Obsolete DLLImportLinkage | ||||
896 | case 6: | ||||
897 | return GlobalValue::ExternalLinkage; // Obsolete DLLExportLinkage | ||||
898 | case 7: | ||||
899 | return GlobalValue::ExternalWeakLinkage; | ||||
900 | case 8: | ||||
901 | return GlobalValue::CommonLinkage; | ||||
902 | case 9: | ||||
903 | return GlobalValue::PrivateLinkage; | ||||
904 | case 12: | ||||
905 | return GlobalValue::AvailableExternallyLinkage; | ||||
906 | case 13: | ||||
907 | return GlobalValue::PrivateLinkage; // Obsolete LinkerPrivateLinkage | ||||
908 | case 14: | ||||
909 | return GlobalValue::PrivateLinkage; // Obsolete LinkerPrivateWeakLinkage | ||||
910 | case 15: | ||||
911 | return GlobalValue::ExternalLinkage; // Obsolete LinkOnceODRAutoHideLinkage | ||||
912 | case 1: // Old value with implicit comdat. | ||||
913 | case 16: | ||||
914 | return GlobalValue::WeakAnyLinkage; | ||||
915 | case 10: // Old value with implicit comdat. | ||||
916 | case 17: | ||||
917 | return GlobalValue::WeakODRLinkage; | ||||
918 | case 4: // Old value with implicit comdat. | ||||
919 | case 18: | ||||
920 | return GlobalValue::LinkOnceAnyLinkage; | ||||
921 | case 11: // Old value with implicit comdat. | ||||
922 | case 19: | ||||
923 | return GlobalValue::LinkOnceODRLinkage; | ||||
924 | } | ||||
925 | } | ||||
926 | |||||
927 | static FunctionSummary::FFlags getDecodedFFlags(uint64_t RawFlags) { | ||||
928 | FunctionSummary::FFlags Flags; | ||||
929 | Flags.ReadNone = RawFlags & 0x1; | ||||
930 | Flags.ReadOnly = (RawFlags >> 1) & 0x1; | ||||
931 | Flags.NoRecurse = (RawFlags >> 2) & 0x1; | ||||
932 | Flags.ReturnDoesNotAlias = (RawFlags >> 3) & 0x1; | ||||
933 | Flags.NoInline = (RawFlags >> 4) & 0x1; | ||||
934 | Flags.AlwaysInline = (RawFlags >> 5) & 0x1; | ||||
935 | return Flags; | ||||
936 | } | ||||
937 | |||||
938 | // Decode the flags for GlobalValue in the summary. The bits for each attribute: | ||||
939 | // | ||||
940 | // linkage: [0,4), notEligibleToImport: 4, live: 5, local: 6, canAutoHide: 7, | ||||
941 | // visibility: [8, 10). | ||||
942 | static GlobalValueSummary::GVFlags getDecodedGVSummaryFlags(uint64_t RawFlags, | ||||
943 | uint64_t Version) { | ||||
944 | // Summary were not emitted before LLVM 3.9, we don't need to upgrade Linkage | ||||
945 | // like getDecodedLinkage() above. Any future change to the linkage enum and | ||||
946 | // to getDecodedLinkage() will need to be taken into account here as above. | ||||
947 | auto Linkage = GlobalValue::LinkageTypes(RawFlags & 0xF); // 4 bits | ||||
948 | auto Visibility = GlobalValue::VisibilityTypes((RawFlags >> 8) & 3); // 2 bits | ||||
949 | RawFlags = RawFlags >> 4; | ||||
950 | bool NotEligibleToImport = (RawFlags & 0x1) || Version < 3; | ||||
951 | // The Live flag wasn't introduced until version 3. For dead stripping | ||||
952 | // to work correctly on earlier versions, we must conservatively treat all | ||||
953 | // values as live. | ||||
954 | bool Live = (RawFlags & 0x2) || Version < 3; | ||||
955 | bool Local = (RawFlags & 0x4); | ||||
956 | bool AutoHide = (RawFlags & 0x8); | ||||
957 | |||||
958 | return GlobalValueSummary::GVFlags(Linkage, Visibility, NotEligibleToImport, | ||||
959 | Live, Local, AutoHide); | ||||
960 | } | ||||
961 | |||||
962 | // Decode the flags for GlobalVariable in the summary | ||||
963 | static GlobalVarSummary::GVarFlags getDecodedGVarFlags(uint64_t RawFlags) { | ||||
964 | return GlobalVarSummary::GVarFlags( | ||||
965 | (RawFlags & 0x1) ? true : false, (RawFlags & 0x2) ? true : false, | ||||
966 | (RawFlags & 0x4) ? true : false, | ||||
967 | (GlobalObject::VCallVisibility)(RawFlags >> 3)); | ||||
968 | } | ||||
969 | |||||
970 | static GlobalValue::VisibilityTypes getDecodedVisibility(unsigned Val) { | ||||
971 | switch (Val) { | ||||
972 | default: // Map unknown visibilities to default. | ||||
973 | case 0: return GlobalValue::DefaultVisibility; | ||||
974 | case 1: return GlobalValue::HiddenVisibility; | ||||
975 | case 2: return GlobalValue::ProtectedVisibility; | ||||
976 | } | ||||
977 | } | ||||
978 | |||||
979 | static GlobalValue::DLLStorageClassTypes | ||||
980 | getDecodedDLLStorageClass(unsigned Val) { | ||||
981 | switch (Val) { | ||||
982 | default: // Map unknown values to default. | ||||
983 | case 0: return GlobalValue::DefaultStorageClass; | ||||
984 | case 1: return GlobalValue::DLLImportStorageClass; | ||||
985 | case 2: return GlobalValue::DLLExportStorageClass; | ||||
986 | } | ||||
987 | } | ||||
988 | |||||
989 | static bool getDecodedDSOLocal(unsigned Val) { | ||||
990 | switch(Val) { | ||||
991 | default: // Map unknown values to preemptable. | ||||
992 | case 0: return false; | ||||
993 | case 1: return true; | ||||
994 | } | ||||
995 | } | ||||
996 | |||||
997 | static GlobalVariable::ThreadLocalMode getDecodedThreadLocalMode(unsigned Val) { | ||||
998 | switch (Val) { | ||||
999 | case 0: return GlobalVariable::NotThreadLocal; | ||||
1000 | default: // Map unknown non-zero value to general dynamic. | ||||
1001 | case 1: return GlobalVariable::GeneralDynamicTLSModel; | ||||
1002 | case 2: return GlobalVariable::LocalDynamicTLSModel; | ||||
1003 | case 3: return GlobalVariable::InitialExecTLSModel; | ||||
1004 | case 4: return GlobalVariable::LocalExecTLSModel; | ||||
1005 | } | ||||
1006 | } | ||||
1007 | |||||
1008 | static GlobalVariable::UnnamedAddr getDecodedUnnamedAddrType(unsigned Val) { | ||||
1009 | switch (Val) { | ||||
1010 | default: // Map unknown to UnnamedAddr::None. | ||||
1011 | case 0: return GlobalVariable::UnnamedAddr::None; | ||||
1012 | case 1: return GlobalVariable::UnnamedAddr::Global; | ||||
1013 | case 2: return GlobalVariable::UnnamedAddr::Local; | ||||
1014 | } | ||||
1015 | } | ||||
1016 | |||||
1017 | static int getDecodedCastOpcode(unsigned Val) { | ||||
1018 | switch (Val) { | ||||
1019 | default: return -1; | ||||
1020 | case bitc::CAST_TRUNC : return Instruction::Trunc; | ||||
1021 | case bitc::CAST_ZEXT : return Instruction::ZExt; | ||||
1022 | case bitc::CAST_SEXT : return Instruction::SExt; | ||||
1023 | case bitc::CAST_FPTOUI : return Instruction::FPToUI; | ||||
1024 | case bitc::CAST_FPTOSI : return Instruction::FPToSI; | ||||
1025 | case bitc::CAST_UITOFP : return Instruction::UIToFP; | ||||
1026 | case bitc::CAST_SITOFP : return Instruction::SIToFP; | ||||
1027 | case bitc::CAST_FPTRUNC : return Instruction::FPTrunc; | ||||
1028 | case bitc::CAST_FPEXT : return Instruction::FPExt; | ||||
1029 | case bitc::CAST_PTRTOINT: return Instruction::PtrToInt; | ||||
1030 | case bitc::CAST_INTTOPTR: return Instruction::IntToPtr; | ||||
1031 | case bitc::CAST_BITCAST : return Instruction::BitCast; | ||||
1032 | case bitc::CAST_ADDRSPACECAST: return Instruction::AddrSpaceCast; | ||||
1033 | } | ||||
1034 | } | ||||
1035 | |||||
1036 | static int getDecodedUnaryOpcode(unsigned Val, Type *Ty) { | ||||
1037 | bool IsFP = Ty->isFPOrFPVectorTy(); | ||||
1038 | // UnOps are only valid for int/fp or vector of int/fp types | ||||
1039 | if (!IsFP && !Ty->isIntOrIntVectorTy()) | ||||
1040 | return -1; | ||||
1041 | |||||
1042 | switch (Val) { | ||||
1043 | default: | ||||
1044 | return -1; | ||||
1045 | case bitc::UNOP_FNEG: | ||||
1046 | return IsFP ? Instruction::FNeg : -1; | ||||
1047 | } | ||||
1048 | } | ||||
1049 | |||||
1050 | static int getDecodedBinaryOpcode(unsigned Val, Type *Ty) { | ||||
1051 | bool IsFP = Ty->isFPOrFPVectorTy(); | ||||
1052 | // BinOps are only valid for int/fp or vector of int/fp types | ||||
1053 | if (!IsFP && !Ty->isIntOrIntVectorTy()) | ||||
1054 | return -1; | ||||
1055 | |||||
1056 | switch (Val) { | ||||
1057 | default: | ||||
1058 | return -1; | ||||
1059 | case bitc::BINOP_ADD: | ||||
1060 | return IsFP ? Instruction::FAdd : Instruction::Add; | ||||
1061 | case bitc::BINOP_SUB: | ||||
1062 | return IsFP ? Instruction::FSub : Instruction::Sub; | ||||
1063 | case bitc::BINOP_MUL: | ||||
1064 | return IsFP ? Instruction::FMul : Instruction::Mul; | ||||
1065 | case bitc::BINOP_UDIV: | ||||
1066 | return IsFP ? -1 : Instruction::UDiv; | ||||
1067 | case bitc::BINOP_SDIV: | ||||
1068 | return IsFP ? Instruction::FDiv : Instruction::SDiv; | ||||
1069 | case bitc::BINOP_UREM: | ||||
1070 | return IsFP ? -1 : Instruction::URem; | ||||
1071 | case bitc::BINOP_SREM: | ||||
1072 | return IsFP ? Instruction::FRem : Instruction::SRem; | ||||
1073 | case bitc::BINOP_SHL: | ||||
1074 | return IsFP ? -1 : Instruction::Shl; | ||||
1075 | case bitc::BINOP_LSHR: | ||||
1076 | return IsFP ? -1 : Instruction::LShr; | ||||
1077 | case bitc::BINOP_ASHR: | ||||
1078 | return IsFP ? -1 : Instruction::AShr; | ||||
1079 | case bitc::BINOP_AND: | ||||
1080 | return IsFP ? -1 : Instruction::And; | ||||
1081 | case bitc::BINOP_OR: | ||||
1082 | return IsFP ? -1 : Instruction::Or; | ||||
1083 | case bitc::BINOP_XOR: | ||||
1084 | return IsFP ? -1 : Instruction::Xor; | ||||
1085 | } | ||||
1086 | } | ||||
1087 | |||||
1088 | static AtomicRMWInst::BinOp getDecodedRMWOperation(unsigned Val) { | ||||
1089 | switch (Val) { | ||||
1090 | default: return AtomicRMWInst::BAD_BINOP; | ||||
1091 | case bitc::RMW_XCHG: return AtomicRMWInst::Xchg; | ||||
1092 | case bitc::RMW_ADD: return AtomicRMWInst::Add; | ||||
1093 | case bitc::RMW_SUB: return AtomicRMWInst::Sub; | ||||
1094 | case bitc::RMW_AND: return AtomicRMWInst::And; | ||||
1095 | case bitc::RMW_NAND: return AtomicRMWInst::Nand; | ||||
1096 | case bitc::RMW_OR: return AtomicRMWInst::Or; | ||||
1097 | case bitc::RMW_XOR: return AtomicRMWInst::Xor; | ||||
1098 | case bitc::RMW_MAX: return AtomicRMWInst::Max; | ||||
1099 | case bitc::RMW_MIN: return AtomicRMWInst::Min; | ||||
1100 | case bitc::RMW_UMAX: return AtomicRMWInst::UMax; | ||||
1101 | case bitc::RMW_UMIN: return AtomicRMWInst::UMin; | ||||
1102 | case bitc::RMW_FADD: return AtomicRMWInst::FAdd; | ||||
1103 | case bitc::RMW_FSUB: return AtomicRMWInst::FSub; | ||||
1104 | } | ||||
1105 | } | ||||
1106 | |||||
1107 | static AtomicOrdering getDecodedOrdering(unsigned Val) { | ||||
1108 | switch (Val) { | ||||
1109 | case bitc::ORDERING_NOTATOMIC: return AtomicOrdering::NotAtomic; | ||||
1110 | case bitc::ORDERING_UNORDERED: return AtomicOrdering::Unordered; | ||||
1111 | case bitc::ORDERING_MONOTONIC: return AtomicOrdering::Monotonic; | ||||
1112 | case bitc::ORDERING_ACQUIRE: return AtomicOrdering::Acquire; | ||||
1113 | case bitc::ORDERING_RELEASE: return AtomicOrdering::Release; | ||||
1114 | case bitc::ORDERING_ACQREL: return AtomicOrdering::AcquireRelease; | ||||
1115 | default: // Map unknown orderings to sequentially-consistent. | ||||
1116 | case bitc::ORDERING_SEQCST: return AtomicOrdering::SequentiallyConsistent; | ||||
1117 | } | ||||
1118 | } | ||||
1119 | |||||
1120 | static Comdat::SelectionKind getDecodedComdatSelectionKind(unsigned Val) { | ||||
1121 | switch (Val) { | ||||
1122 | default: // Map unknown selection kinds to any. | ||||
1123 | case bitc::COMDAT_SELECTION_KIND_ANY: | ||||
1124 | return Comdat::Any; | ||||
1125 | case bitc::COMDAT_SELECTION_KIND_EXACT_MATCH: | ||||
1126 | return Comdat::ExactMatch; | ||||
1127 | case bitc::COMDAT_SELECTION_KIND_LARGEST: | ||||
1128 | return Comdat::Largest; | ||||
1129 | case bitc::COMDAT_SELECTION_KIND_NO_DUPLICATES: | ||||
1130 | return Comdat::NoDuplicates; | ||||
1131 | case bitc::COMDAT_SELECTION_KIND_SAME_SIZE: | ||||
1132 | return Comdat::SameSize; | ||||
1133 | } | ||||
1134 | } | ||||
1135 | |||||
1136 | static FastMathFlags getDecodedFastMathFlags(unsigned Val) { | ||||
1137 | FastMathFlags FMF; | ||||
1138 | if (0 != (Val & bitc::UnsafeAlgebra)) | ||||
1139 | FMF.setFast(); | ||||
1140 | if (0 != (Val & bitc::AllowReassoc)) | ||||
1141 | FMF.setAllowReassoc(); | ||||
1142 | if (0 != (Val & bitc::NoNaNs)) | ||||
1143 | FMF.setNoNaNs(); | ||||
1144 | if (0 != (Val & bitc::NoInfs)) | ||||
1145 | FMF.setNoInfs(); | ||||
1146 | if (0 != (Val & bitc::NoSignedZeros)) | ||||
1147 | FMF.setNoSignedZeros(); | ||||
1148 | if (0 != (Val & bitc::AllowReciprocal)) | ||||
1149 | FMF.setAllowReciprocal(); | ||||
1150 | if (0 != (Val & bitc::AllowContract)) | ||||
1151 | FMF.setAllowContract(true); | ||||
1152 | if (0 != (Val & bitc::ApproxFunc)) | ||||
1153 | FMF.setApproxFunc(); | ||||
1154 | return FMF; | ||||
1155 | } | ||||
1156 | |||||
1157 | static void upgradeDLLImportExportLinkage(GlobalValue *GV, unsigned Val) { | ||||
1158 | switch (Val) { | ||||
1159 | case 5: GV->setDLLStorageClass(GlobalValue::DLLImportStorageClass); break; | ||||
1160 | case 6: GV->setDLLStorageClass(GlobalValue::DLLExportStorageClass); break; | ||||
1161 | } | ||||
1162 | } | ||||
1163 | |||||
1164 | Type *BitcodeReader::getTypeByID(unsigned ID) { | ||||
1165 | // The type table size is always specified correctly. | ||||
1166 | if (ID >= TypeList.size()) | ||||
1167 | return nullptr; | ||||
1168 | |||||
1169 | if (Type *Ty = TypeList[ID]) | ||||
1170 | return Ty; | ||||
1171 | |||||
1172 | // If we have a forward reference, the only possible case is when it is to a | ||||
1173 | // named struct. Just create a placeholder for now. | ||||
1174 | return TypeList[ID] = createIdentifiedStructType(Context); | ||||
1175 | } | ||||
1176 | |||||
1177 | StructType *BitcodeReader::createIdentifiedStructType(LLVMContext &Context, | ||||
1178 | StringRef Name) { | ||||
1179 | auto *Ret = StructType::create(Context, Name); | ||||
1180 | IdentifiedStructTypes.push_back(Ret); | ||||
1181 | return Ret; | ||||
1182 | } | ||||
1183 | |||||
1184 | StructType *BitcodeReader::createIdentifiedStructType(LLVMContext &Context) { | ||||
1185 | auto *Ret = StructType::create(Context); | ||||
1186 | IdentifiedStructTypes.push_back(Ret); | ||||
1187 | return Ret; | ||||
1188 | } | ||||
1189 | |||||
1190 | //===----------------------------------------------------------------------===// | ||||
1191 | // Functions for parsing blocks from the bitcode file | ||||
1192 | //===----------------------------------------------------------------------===// | ||||
1193 | |||||
1194 | static uint64_t getRawAttributeMask(Attribute::AttrKind Val) { | ||||
1195 | switch (Val) { | ||||
1196 | case Attribute::EndAttrKinds: | ||||
1197 | case Attribute::EmptyKey: | ||||
1198 | case Attribute::TombstoneKey: | ||||
1199 | llvm_unreachable("Synthetic enumerators which should never get here")::llvm::llvm_unreachable_internal("Synthetic enumerators which should never get here" , "/build/llvm-toolchain-snapshot-13~++20210621111111+acefe0eaaf82/llvm/lib/Bitcode/Reader/BitcodeReader.cpp" , 1199); | ||||
1200 | |||||
1201 | case Attribute::None: return 0; | ||||
1202 | case Attribute::ZExt: return 1 << 0; | ||||
1203 | case Attribute::SExt: return 1 << 1; | ||||
1204 | case Attribute::NoReturn: return 1 << 2; | ||||
1205 | case Attribute::InReg: return 1 << 3; | ||||
1206 | case Attribute::StructRet: return 1 << 4; | ||||
1207 | case Attribute::NoUnwind: return 1 << 5; | ||||
1208 | case Attribute::NoAlias: return 1 << 6; | ||||
1209 | case Attribute::ByVal: return 1 << 7; | ||||
1210 | case Attribute::Nest: return 1 << 8; | ||||
1211 | case Attribute::ReadNone: return 1 << 9; | ||||
1212 | case Attribute::ReadOnly: return 1 << 10; | ||||
1213 | case Attribute::NoInline: return 1 << 11; | ||||
1214 | case Attribute::AlwaysInline: return 1 << 12; | ||||
1215 | case Attribute::OptimizeForSize: return 1 << 13; | ||||
1216 | case Attribute::StackProtect: return 1 << 14; | ||||
1217 | case Attribute::StackProtectReq: return 1 << 15; | ||||
1218 | case Attribute::Alignment: return 31 << 16; | ||||
1219 | case Attribute::NoCapture: return 1 << 21; | ||||
1220 | case Attribute::NoRedZone: return 1 << 22; | ||||
1221 | case Attribute::NoImplicitFloat: return 1 << 23; | ||||
1222 | case Attribute::Naked: return 1 << 24; | ||||
1223 | case Attribute::InlineHint: return 1 << 25; | ||||
1224 | case Attribute::StackAlignment: return 7 << 26; | ||||
1225 | case Attribute::ReturnsTwice: return 1 << 29; | ||||
1226 | case Attribute::UWTable: return 1 << 30; | ||||
1227 | case Attribute::NonLazyBind: return 1U << 31; | ||||
1228 | case Attribute::SanitizeAddress: return 1ULL << 32; | ||||
1229 | case Attribute::MinSize: return 1ULL << 33; | ||||
1230 | case Attribute::NoDuplicate: return 1ULL << 34; | ||||
1231 | case Attribute::StackProtectStrong: return 1ULL << 35; | ||||
1232 | case Attribute::SanitizeThread: return 1ULL << 36; | ||||
1233 | case Attribute::SanitizeMemory: return 1ULL << 37; | ||||
1234 | case Attribute::NoBuiltin: return 1ULL << 38; | ||||
1235 | case Attribute::Returned: return 1ULL << 39; | ||||
1236 | case Attribute::Cold: return 1ULL << 40; | ||||
1237 | case Attribute::Builtin: return 1ULL << 41; | ||||
1238 | case Attribute::OptimizeNone: return 1ULL << 42; | ||||
1239 | case Attribute::InAlloca: return 1ULL << 43; | ||||
1240 | case Attribute::NonNull: return 1ULL << 44; | ||||
1241 | case Attribute::JumpTable: return 1ULL << 45; | ||||
1242 | case Attribute::Convergent: return 1ULL << 46; | ||||
1243 | case Attribute::SafeStack: return 1ULL << 47; | ||||
1244 | case Attribute::NoRecurse: return 1ULL << 48; | ||||
1245 | case Attribute::InaccessibleMemOnly: return 1ULL << 49; | ||||
1246 | case Attribute::InaccessibleMemOrArgMemOnly: return 1ULL << 50; | ||||
1247 | case Attribute::SwiftSelf: return 1ULL << 51; | ||||
1248 | case Attribute::SwiftError: return 1ULL << 52; | ||||
1249 | case Attribute::WriteOnly: return 1ULL << 53; | ||||
1250 | case Attribute::Speculatable: return 1ULL << 54; | ||||
1251 | case Attribute::StrictFP: return 1ULL << 55; | ||||
1252 | case Attribute::SanitizeHWAddress: return 1ULL << 56; | ||||
1253 | case Attribute::NoCfCheck: return 1ULL << 57; | ||||
1254 | case Attribute::OptForFuzzing: return 1ULL << 58; | ||||
1255 | case Attribute::ShadowCallStack: return 1ULL << 59; | ||||
1256 | case Attribute::SpeculativeLoadHardening: | ||||
1257 | return 1ULL << 60; | ||||
1258 | case Attribute::ImmArg: | ||||
1259 | return 1ULL << 61; | ||||
1260 | case Attribute::WillReturn: | ||||
1261 | return 1ULL << 62; | ||||
1262 | case Attribute::NoFree: | ||||
1263 | return 1ULL << 63; | ||||
1264 | default: | ||||
1265 | // Other attributes are not supported in the raw format, | ||||
1266 | // as we ran out of space. | ||||
1267 | return 0; | ||||
1268 | } | ||||
1269 | llvm_unreachable("Unsupported attribute type")::llvm::llvm_unreachable_internal("Unsupported attribute type" , "/build/llvm-toolchain-snapshot-13~++20210621111111+acefe0eaaf82/llvm/lib/Bitcode/Reader/BitcodeReader.cpp" , 1269); | ||||
1270 | } | ||||
1271 | |||||
1272 | static void addRawAttributeValue(AttrBuilder &B, uint64_t Val) { | ||||
1273 | if (!Val) return; | ||||
1274 | |||||
1275 | for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds; | ||||
1276 | I = Attribute::AttrKind(I + 1)) { | ||||
1277 | if (uint64_t A = (Val & getRawAttributeMask(I))) { | ||||
1278 | if (I == Attribute::Alignment) | ||||
1279 | B.addAlignmentAttr(1ULL << ((A >> 16) - 1)); | ||||
1280 | else if (I == Attribute::StackAlignment) | ||||
1281 | B.addStackAlignmentAttr(1ULL << ((A >> 26)-1)); | ||||
1282 | else | ||||
1283 | B.addAttribute(I); | ||||
1284 | } | ||||
1285 | } | ||||
1286 | } | ||||
1287 | |||||
1288 | /// This fills an AttrBuilder object with the LLVM attributes that have | ||||
1289 | /// been decoded from the given integer. This function must stay in sync with | ||||
1290 | /// 'encodeLLVMAttributesForBitcode'. | ||||
1291 | static void decodeLLVMAttributesForBitcode(AttrBuilder &B, | ||||
1292 | uint64_t EncodedAttrs) { | ||||
1293 | // The alignment is stored as a 16-bit raw value from bits 31--16. We shift | ||||
1294 | // the bits above 31 down by 11 bits. | ||||
1295 | unsigned Alignment = (EncodedAttrs & (0xffffULL << 16)) >> 16; | ||||
1296 | assert((!Alignment || isPowerOf2_32(Alignment)) &&(static_cast <bool> ((!Alignment || isPowerOf2_32(Alignment )) && "Alignment must be a power of two.") ? void (0) : __assert_fail ("(!Alignment || isPowerOf2_32(Alignment)) && \"Alignment must be a power of two.\"" , "/build/llvm-toolchain-snapshot-13~++20210621111111+acefe0eaaf82/llvm/lib/Bitcode/Reader/BitcodeReader.cpp" , 1297, __extension__ __PRETTY_FUNCTION__)) | ||||
1297 | "Alignment must be a power of two.")(static_cast <bool> ((!Alignment || isPowerOf2_32(Alignment )) && "Alignment must be a power of two.") ? void (0) : __assert_fail ("(!Alignment || isPowerOf2_32(Alignment)) && \"Alignment must be a power of two.\"" , "/build/llvm-toolchain-snapshot-13~++20210621111111+acefe0eaaf82/llvm/lib/Bitcode/Reader/BitcodeReader.cpp" , 1297, __extension__ __PRETTY_FUNCTION__)); | ||||
1298 | |||||
1299 | if (Alignment) | ||||
1300 | B.addAlignmentAttr(Alignment); | ||||
1301 | addRawAttributeValue(B, ((EncodedAttrs & (0xfffffULL << 32)) >> 11) | | ||||
1302 | (EncodedAttrs & 0xffff)); | ||||
1303 | } | ||||
1304 | |||||
1305 | Error BitcodeReader::parseAttributeBlock() { | ||||
1306 | if (Error Err = Stream.EnterSubBlock(bitc::PARAMATTR_BLOCK_ID)) | ||||
1307 | return Err; | ||||
1308 | |||||
1309 | if (!MAttributes.empty()) | ||||
1310 | return error("Invalid multiple blocks"); | ||||
1311 | |||||
1312 | SmallVector<uint64_t, 64> Record; | ||||
1313 | |||||
1314 | SmallVector<AttributeList, 8> Attrs; | ||||
1315 | |||||
1316 | // Read all the records. | ||||
1317 | while (true) { | ||||
1318 | Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks(); | ||||
1319 | if (!MaybeEntry) | ||||
1320 | return MaybeEntry.takeError(); | ||||
1321 | BitstreamEntry Entry = MaybeEntry.get(); | ||||
1322 | |||||
1323 | switch (Entry.Kind) { | ||||
1324 | case BitstreamEntry::SubBlock: // Handled for us already. | ||||
1325 | case BitstreamEntry::Error: | ||||
1326 | return error("Malformed block"); | ||||
1327 | case BitstreamEntry::EndBlock: | ||||
1328 | return Error::success(); | ||||
1329 | case BitstreamEntry::Record: | ||||
1330 | // The interesting case. | ||||
1331 | break; | ||||
1332 | } | ||||
1333 | |||||
1334 | // Read a record. | ||||
1335 | Record.clear(); | ||||
1336 | Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record); | ||||
1337 | if (!MaybeRecord) | ||||
1338 | return MaybeRecord.takeError(); | ||||
1339 | switch (MaybeRecord.get()) { | ||||
1340 | default: // Default behavior: ignore. | ||||
1341 | break; | ||||
1342 | case bitc::PARAMATTR_CODE_ENTRY_OLD: // ENTRY: [paramidx0, attr0, ...] | ||||
1343 | // Deprecated, but still needed to read old bitcode files. | ||||
1344 | if (Record.size() & 1) | ||||
1345 | return error("Invalid record"); | ||||
1346 | |||||
1347 | for (unsigned i = 0, e = Record.size(); i != e; i += 2) { | ||||
1348 | AttrBuilder B; | ||||
1349 | decodeLLVMAttributesForBitcode(B, Record[i+1]); | ||||
1350 | Attrs.push_back(AttributeList::get(Context, Record[i], B)); | ||||
1351 | } | ||||
1352 | |||||
1353 | MAttributes.push_back(AttributeList::get(Context, Attrs)); | ||||
1354 | Attrs.clear(); | ||||
1355 | break; | ||||
1356 | case bitc::PARAMATTR_CODE_ENTRY: // ENTRY: [attrgrp0, attrgrp1, ...] | ||||
1357 | for (unsigned i = 0, e = Record.size(); i != e; ++i) | ||||
1358 | Attrs.push_back(MAttributeGroups[Record[i]]); | ||||
1359 | |||||
1360 | MAttributes.push_back(AttributeList::get(Context, Attrs)); | ||||
1361 | Attrs.clear(); | ||||
1362 | break; | ||||
1363 | } | ||||
1364 | } | ||||
1365 | } | ||||
1366 | |||||
1367 | // Returns Attribute::None on unrecognized codes. | ||||
1368 | static Attribute::AttrKind getAttrFromCode(uint64_t Code) { | ||||
1369 | switch (Code) { | ||||
1370 | default: | ||||
1371 | return Attribute::None; | ||||
1372 | case bitc::ATTR_KIND_ALIGNMENT: | ||||
1373 | return Attribute::Alignment; | ||||
1374 | case bitc::ATTR_KIND_ALWAYS_INLINE: | ||||
1375 | return Attribute::AlwaysInline; | ||||
1376 | case bitc::ATTR_KIND_ARGMEMONLY: | ||||
1377 | return Attribute::ArgMemOnly; | ||||
1378 | case bitc::ATTR_KIND_BUILTIN: | ||||
1379 | return Attribute::Builtin; | ||||
1380 | case bitc::ATTR_KIND_BY_VAL: | ||||
1381 | return Attribute::ByVal; | ||||
1382 | case bitc::ATTR_KIND_IN_ALLOCA: | ||||
1383 | return Attribute::InAlloca; | ||||
1384 | case bitc::ATTR_KIND_COLD: | ||||
1385 | return Attribute::Cold; | ||||
1386 | case bitc::ATTR_KIND_CONVERGENT: | ||||
1387 | return Attribute::Convergent; | ||||
1388 | case bitc::ATTR_KIND_INACCESSIBLEMEM_ONLY: | ||||
1389 | return Attribute::InaccessibleMemOnly; | ||||
1390 | case bitc::ATTR_KIND_INACCESSIBLEMEM_OR_ARGMEMONLY: | ||||
1391 | return Attribute::InaccessibleMemOrArgMemOnly; | ||||
1392 | case bitc::ATTR_KIND_INLINE_HINT: | ||||
1393 | return Attribute::InlineHint; | ||||
1394 | case bitc::ATTR_KIND_IN_REG: | ||||
1395 | return Attribute::InReg; | ||||
1396 | case bitc::ATTR_KIND_JUMP_TABLE: | ||||
1397 | return Attribute::JumpTable; | ||||
1398 | case bitc::ATTR_KIND_MIN_SIZE: | ||||
1399 | return Attribute::MinSize; | ||||
1400 | case bitc::ATTR_KIND_NAKED: | ||||
1401 | return Attribute::Naked; | ||||
1402 | case bitc::ATTR_KIND_NEST: | ||||
1403 | return Attribute::Nest; | ||||
1404 | case bitc::ATTR_KIND_NO_ALIAS: | ||||
1405 | return Attribute::NoAlias; | ||||
1406 | case bitc::ATTR_KIND_NO_BUILTIN: | ||||
1407 | return Attribute::NoBuiltin; | ||||
1408 | case bitc::ATTR_KIND_NO_CALLBACK: | ||||
1409 | return Attribute::NoCallback; | ||||
1410 | case bitc::ATTR_KIND_NO_CAPTURE: | ||||
1411 | return Attribute::NoCapture; | ||||
1412 | case bitc::ATTR_KIND_NO_DUPLICATE: | ||||
1413 | return Attribute::NoDuplicate; | ||||
1414 | case bitc::ATTR_KIND_NOFREE: | ||||
1415 | return Attribute::NoFree; | ||||
1416 | case bitc::ATTR_KIND_NO_IMPLICIT_FLOAT: | ||||
1417 | return Attribute::NoImplicitFloat; | ||||
1418 | case bitc::ATTR_KIND_NO_INLINE: | ||||
1419 | return Attribute::NoInline; | ||||
1420 | case bitc::ATTR_KIND_NO_RECURSE: | ||||
1421 | return Attribute::NoRecurse; | ||||
1422 | case bitc::ATTR_KIND_NO_MERGE: | ||||
1423 | return Attribute::NoMerge; | ||||
1424 | case bitc::ATTR_KIND_NON_LAZY_BIND: | ||||
1425 | return Attribute::NonLazyBind; | ||||
1426 | case bitc::ATTR_KIND_NON_NULL: | ||||
1427 | return Attribute::NonNull; | ||||
1428 | case bitc::ATTR_KIND_DEREFERENCEABLE: | ||||
1429 | return Attribute::Dereferenceable; | ||||
1430 | case bitc::ATTR_KIND_DEREFERENCEABLE_OR_NULL: | ||||
1431 | return Attribute::DereferenceableOrNull; | ||||
1432 | case bitc::ATTR_KIND_ALLOC_SIZE: | ||||
1433 | return Attribute::AllocSize; | ||||
1434 | case bitc::ATTR_KIND_NO_RED_ZONE: | ||||
1435 | return Attribute::NoRedZone; | ||||
1436 | case bitc::ATTR_KIND_NO_RETURN: | ||||
1437 | return Attribute::NoReturn; | ||||
1438 | case bitc::ATTR_KIND_NOSYNC: | ||||
1439 | return Attribute::NoSync; | ||||
1440 | case bitc::ATTR_KIND_NOCF_CHECK: | ||||
1441 | return Attribute::NoCfCheck; | ||||
1442 | case bitc::ATTR_KIND_NO_UNWIND: | ||||
1443 | return Attribute::NoUnwind; | ||||
1444 | case bitc::ATTR_KIND_NO_SANITIZE_COVERAGE: | ||||
1445 | return Attribute::NoSanitizeCoverage; | ||||
1446 | case bitc::ATTR_KIND_NULL_POINTER_IS_VALID: | ||||
1447 | return Attribute::NullPointerIsValid; | ||||
1448 | case bitc::ATTR_KIND_OPT_FOR_FUZZING: | ||||
1449 | return Attribute::OptForFuzzing; | ||||
1450 | case bitc::ATTR_KIND_OPTIMIZE_FOR_SIZE: | ||||
1451 | return Attribute::OptimizeForSize; | ||||
1452 | case bitc::ATTR_KIND_OPTIMIZE_NONE: | ||||
1453 | return Attribute::OptimizeNone; | ||||
1454 | case bitc::ATTR_KIND_READ_NONE: | ||||
1455 | return Attribute::ReadNone; | ||||
1456 | case bitc::ATTR_KIND_READ_ONLY: | ||||
1457 | return Attribute::ReadOnly; | ||||
1458 | case bitc::ATTR_KIND_RETURNED: | ||||
1459 | return Attribute::Returned; | ||||
1460 | case bitc::ATTR_KIND_RETURNS_TWICE: | ||||
1461 | return Attribute::ReturnsTwice; | ||||
1462 | case bitc::ATTR_KIND_S_EXT: | ||||
1463 | return Attribute::SExt; | ||||
1464 | case bitc::ATTR_KIND_SPECULATABLE: | ||||
1465 | return Attribute::Speculatable; | ||||
1466 | case bitc::ATTR_KIND_STACK_ALIGNMENT: | ||||
1467 | return Attribute::StackAlignment; | ||||
1468 | case bitc::ATTR_KIND_STACK_PROTECT: | ||||
1469 | return Attribute::StackProtect; | ||||
1470 | case bitc::ATTR_KIND_STACK_PROTECT_REQ: | ||||
1471 | return Attribute::StackProtectReq; | ||||
1472 | case bitc::ATTR_KIND_STACK_PROTECT_STRONG: | ||||
1473 | return Attribute::StackProtectStrong; | ||||
1474 | case bitc::ATTR_KIND_SAFESTACK: | ||||
1475 | return Attribute::SafeStack; | ||||
1476 | case bitc::ATTR_KIND_SHADOWCALLSTACK: | ||||
1477 | return Attribute::ShadowCallStack; | ||||
1478 | case bitc::ATTR_KIND_STRICT_FP: | ||||
1479 | return Attribute::StrictFP; | ||||
1480 | case bitc::ATTR_KIND_STRUCT_RET: | ||||
1481 | return Attribute::StructRet; | ||||
1482 | case bitc::ATTR_KIND_SANITIZE_ADDRESS: | ||||
1483 | return Attribute::SanitizeAddress; | ||||
1484 | case bitc::ATTR_KIND_SANITIZE_HWADDRESS: | ||||
1485 | return Attribute::SanitizeHWAddress; | ||||
1486 | case bitc::ATTR_KIND_SANITIZE_THREAD: | ||||
1487 | return Attribute::SanitizeThread; | ||||
1488 | case bitc::ATTR_KIND_SANITIZE_MEMORY: | ||||
1489 | return Attribute::SanitizeMemory; | ||||
1490 | case bitc::ATTR_KIND_SPECULATIVE_LOAD_HARDENING: | ||||
1491 | return Attribute::SpeculativeLoadHardening; | ||||
1492 | case bitc::ATTR_KIND_SWIFT_ERROR: | ||||
1493 | return Attribute::SwiftError; | ||||
1494 | case bitc::ATTR_KIND_SWIFT_SELF: | ||||
1495 | return Attribute::SwiftSelf; | ||||
1496 | case bitc::ATTR_KIND_SWIFT_ASYNC: | ||||
1497 | return Attribute::SwiftAsync; | ||||
1498 | case bitc::ATTR_KIND_UW_TABLE: | ||||
1499 | return Attribute::UWTable; | ||||
1500 | case bitc::ATTR_KIND_VSCALE_RANGE: | ||||
1501 | return Attribute::VScaleRange; | ||||
1502 | case bitc::ATTR_KIND_WILLRETURN: | ||||
1503 | return Attribute::WillReturn; | ||||
1504 | case bitc::ATTR_KIND_WRITEONLY: | ||||
1505 | return Attribute::WriteOnly; | ||||
1506 | case bitc::ATTR_KIND_Z_EXT: | ||||
1507 | return Attribute::ZExt; | ||||
1508 | case bitc::ATTR_KIND_IMMARG: | ||||
1509 | return Attribute::ImmArg; | ||||
1510 | case bitc::ATTR_KIND_SANITIZE_MEMTAG: | ||||
1511 | return Attribute::SanitizeMemTag; | ||||
1512 | case bitc::ATTR_KIND_PREALLOCATED: | ||||
1513 | return Attribute::Preallocated; | ||||
1514 | case bitc::ATTR_KIND_NOUNDEF: | ||||
1515 | return Attribute::NoUndef; | ||||
1516 | case bitc::ATTR_KIND_BYREF: | ||||
1517 | return Attribute::ByRef; | ||||
1518 | case bitc::ATTR_KIND_MUSTPROGRESS: | ||||
1519 | return Attribute::MustProgress; | ||||
1520 | case bitc::ATTR_KIND_HOT: | ||||
1521 | return Attribute::Hot; | ||||
1522 | } | ||||
1523 | } | ||||
1524 | |||||
1525 | Error BitcodeReader::parseAlignmentValue(uint64_t Exponent, | ||||
1526 | MaybeAlign &Alignment) { | ||||
1527 | // Note: Alignment in bitcode files is incremented by 1, so that zero | ||||
1528 | // can be used for default alignment. | ||||
1529 | if (Exponent > Value::MaxAlignmentExponent + 1) | ||||
1530 | return error("Invalid alignment value"); | ||||
1531 | Alignment = decodeMaybeAlign(Exponent); | ||||
1532 | return Error::success(); | ||||
1533 | } | ||||
1534 | |||||
1535 | Error BitcodeReader::parseAttrKind(uint64_t Code, Attribute::AttrKind *Kind) { | ||||
1536 | *Kind = getAttrFromCode(Code); | ||||
1537 | if (*Kind == Attribute::None) | ||||
1538 | return error("Unknown attribute kind (" + Twine(Code) + ")"); | ||||
1539 | return Error::success(); | ||||
1540 | } | ||||
1541 | |||||
1542 | Error BitcodeReader::parseAttributeGroupBlock() { | ||||
1543 | if (Error Err = Stream.EnterSubBlock(bitc::PARAMATTR_GROUP_BLOCK_ID)) | ||||
1544 | return Err; | ||||
1545 | |||||
1546 | if (!MAttributeGroups.empty()) | ||||
1547 | return error("Invalid multiple blocks"); | ||||
1548 | |||||
1549 | SmallVector<uint64_t, 64> Record; | ||||
1550 | |||||
1551 | // Read all the records. | ||||
1552 | while (true) { | ||||
1553 | Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks(); | ||||
1554 | if (!MaybeEntry) | ||||
1555 | return MaybeEntry.takeError(); | ||||
1556 | BitstreamEntry Entry = MaybeEntry.get(); | ||||
1557 | |||||
1558 | switch (Entry.Kind) { | ||||
1559 | case BitstreamEntry::SubBlock: // Handled for us already. | ||||
1560 | case BitstreamEntry::Error: | ||||
1561 | return error("Malformed block"); | ||||
1562 | case BitstreamEntry::EndBlock: | ||||
1563 | return Error::success(); | ||||
1564 | case BitstreamEntry::Record: | ||||
1565 | // The interesting case. | ||||
1566 | break; | ||||
1567 | } | ||||
1568 | |||||
1569 | // Read a record. | ||||
1570 | Record.clear(); | ||||
1571 | Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record); | ||||
1572 | if (!MaybeRecord) | ||||
1573 | return MaybeRecord.takeError(); | ||||
1574 | switch (MaybeRecord.get()) { | ||||
1575 | default: // Default behavior: ignore. | ||||
1576 | break; | ||||
1577 | case bitc::PARAMATTR_GRP_CODE_ENTRY: { // ENTRY: [grpid, idx, a0, a1, ...] | ||||
1578 | if (Record.size() < 3) | ||||
1579 | return error("Invalid record"); | ||||
1580 | |||||
1581 | uint64_t GrpID = Record[0]; | ||||
1582 | uint64_t Idx = Record[1]; // Index of the object this attribute refers to. | ||||
1583 | |||||
1584 | AttrBuilder B; | ||||
1585 | for (unsigned i = 2, e = Record.size(); i != e; ++i) { | ||||
1586 | if (Record[i] == 0) { // Enum attribute | ||||
1587 | Attribute::AttrKind Kind; | ||||
1588 | if (Error Err = parseAttrKind(Record[++i], &Kind)) | ||||
1589 | return Err; | ||||
1590 | |||||
1591 | // Upgrade old-style byval attribute to one with a type, even if it's | ||||
1592 | // nullptr. We will have to insert the real type when we associate | ||||
1593 | // this AttributeList with a function. | ||||
1594 | if (Kind == Attribute::ByVal) | ||||
1595 | B.addByValAttr(nullptr); | ||||
1596 | else if (Kind == Attribute::StructRet) | ||||
1597 | B.addStructRetAttr(nullptr); | ||||
1598 | else if (Kind == Attribute::InAlloca) | ||||
1599 | B.addInAllocaAttr(nullptr); | ||||
1600 | |||||
1601 | B.addAttribute(Kind); | ||||
1602 | } else if (Record[i] == 1) { // Integer attribute | ||||
1603 | Attribute::AttrKind Kind; | ||||
1604 | if (Error Err = parseAttrKind(Record[++i], &Kind)) | ||||
1605 | return Err; | ||||
1606 | if (Kind == Attribute::Alignment) | ||||
1607 | B.addAlignmentAttr(Record[++i]); | ||||
1608 | else if (Kind == Attribute::StackAlignment) | ||||
1609 | B.addStackAlignmentAttr(Record[++i]); | ||||
1610 | else if (Kind == Attribute::Dereferenceable) | ||||
1611 | B.addDereferenceableAttr(Record[++i]); | ||||
1612 | else if (Kind == Attribute::DereferenceableOrNull) | ||||
1613 | B.addDereferenceableOrNullAttr(Record[++i]); | ||||
1614 | else if (Kind == Attribute::AllocSize) | ||||
1615 | B.addAllocSizeAttrFromRawRepr(Record[++i]); | ||||
1616 | else if (Kind == Attribute::VScaleRange) | ||||
1617 | B.addVScaleRangeAttrFromRawRepr(Record[++i]); | ||||
1618 | } else if (Record[i] == 3 || Record[i] == 4) { // String attribute | ||||
1619 | bool HasValue = (Record[i++] == 4); | ||||
1620 | SmallString<64> KindStr; | ||||
1621 | SmallString<64> ValStr; | ||||
1622 | |||||
1623 | while (Record[i] != 0 && i != e) | ||||
1624 | KindStr += Record[i++]; | ||||
1625 | assert(Record[i] == 0 && "Kind string not null terminated")(static_cast <bool> (Record[i] == 0 && "Kind string not null terminated" ) ? void (0) : __assert_fail ("Record[i] == 0 && \"Kind string not null terminated\"" , "/build/llvm-toolchain-snapshot-13~++20210621111111+acefe0eaaf82/llvm/lib/Bitcode/Reader/BitcodeReader.cpp" , 1625, __extension__ __PRETTY_FUNCTION__)); | ||||
1626 | |||||
1627 | if (HasValue) { | ||||
1628 | // Has a value associated with it. | ||||
1629 | ++i; // Skip the '0' that terminates the "kind" string. | ||||
1630 | while (Record[i] != 0 && i != e) | ||||
1631 | ValStr += Record[i++]; | ||||
1632 | assert(Record[i] == 0 && "Value string not null terminated")(static_cast <bool> (Record[i] == 0 && "Value string not null terminated" ) ? void (0) : __assert_fail ("Record[i] == 0 && \"Value string not null terminated\"" , "/build/llvm-toolchain-snapshot-13~++20210621111111+acefe0eaaf82/llvm/lib/Bitcode/Reader/BitcodeReader.cpp" , 1632, __extension__ __PRETTY_FUNCTION__)); | ||||
1633 | } | ||||
1634 | |||||
1635 | B.addAttribute(KindStr.str(), ValStr.str()); | ||||
1636 | } else { | ||||
1637 | assert((Record[i] == 5 || Record[i] == 6) &&(static_cast <bool> ((Record[i] == 5 || Record[i] == 6) && "Invalid attribute group entry") ? void (0) : __assert_fail ("(Record[i] == 5 || Record[i] == 6) && \"Invalid attribute group entry\"" , "/build/llvm-toolchain-snapshot-13~++20210621111111+acefe0eaaf82/llvm/lib/Bitcode/Reader/BitcodeReader.cpp" , 1638, __extension__ __PRETTY_FUNCTION__)) | ||||
1638 | "Invalid attribute group entry")(static_cast <bool> ((Record[i] == 5 || Record[i] == 6) && "Invalid attribute group entry") ? void (0) : __assert_fail ("(Record[i] == 5 || Record[i] == 6) && \"Invalid attribute group entry\"" , "/build/llvm-toolchain-snapshot-13~++20210621111111+acefe0eaaf82/llvm/lib/Bitcode/Reader/BitcodeReader.cpp" , 1638, __extension__ __PRETTY_FUNCTION__)); | ||||
1639 | bool HasType = Record[i] == 6; | ||||
1640 | Attribute::AttrKind Kind; | ||||
1641 | if (Error Err = parseAttrKind(Record[++i], &Kind)) | ||||
1642 | return Err; | ||||
1643 | if (Kind == Attribute::ByVal) { | ||||
1644 | B.addByValAttr(HasType ? getTypeByID(Record[++i]) : nullptr); | ||||
1645 | } else if (Kind == Attribute::StructRet) { | ||||
1646 | B.addStructRetAttr(HasType ? getTypeByID(Record[++i]) : nullptr); | ||||
1647 | } else if (Kind == Attribute::ByRef) { | ||||
1648 | B.addByRefAttr(getTypeByID(Record[++i])); | ||||
1649 | } else if (Kind == Attribute::Preallocated) { | ||||
1650 | B.addPreallocatedAttr(getTypeByID(Record[++i])); | ||||
1651 | } else if (Kind == Attribute::InAlloca) { | ||||
1652 | B.addInAllocaAttr(HasType ? getTypeByID(Record[++i]) : nullptr); | ||||
1653 | } | ||||
1654 | } | ||||
1655 | } | ||||
1656 | |||||
1657 | UpgradeAttributes(B); | ||||
1658 | MAttributeGroups[GrpID] = AttributeList::get(Context, Idx, B); | ||||
1659 | break; | ||||
1660 | } | ||||
1661 | } | ||||
1662 | } | ||||
1663 | } | ||||
1664 | |||||
1665 | Error BitcodeReader::parseTypeTable() { | ||||
1666 | if (Error Err = Stream.EnterSubBlock(bitc::TYPE_BLOCK_ID_NEW)) | ||||
1667 | return Err; | ||||
1668 | |||||
1669 | return parseTypeTableBody(); | ||||
1670 | } | ||||
1671 | |||||
1672 | Error BitcodeReader::parseTypeTableBody() { | ||||
1673 | if (!TypeList.empty()) | ||||
1674 | return error("Invalid multiple blocks"); | ||||
1675 | |||||
1676 | SmallVector<uint64_t, 64> Record; | ||||
1677 | unsigned NumRecords = 0; | ||||
1678 | |||||
1679 | SmallString<64> TypeName; | ||||
1680 | |||||
1681 | // Read all the records for this type table. | ||||
1682 | while (true) { | ||||
1683 | Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks(); | ||||
1684 | if (!MaybeEntry) | ||||
1685 | return MaybeEntry.takeError(); | ||||
1686 | BitstreamEntry Entry = MaybeEntry.get(); | ||||
1687 | |||||
1688 | switch (Entry.Kind) { | ||||
1689 | case BitstreamEntry::SubBlock: // Handled for us already. | ||||
1690 | case BitstreamEntry::Error: | ||||
1691 | return error("Malformed block"); | ||||
1692 | case BitstreamEntry::EndBlock: | ||||
1693 | if (NumRecords != TypeList.size()) | ||||
1694 | return error("Malformed block"); | ||||
1695 | return Error::success(); | ||||
1696 | case BitstreamEntry::Record: | ||||
1697 | // The interesting case. | ||||
1698 | break; | ||||
1699 | } | ||||
1700 | |||||
1701 | // Read a record. | ||||
1702 | Record.clear(); | ||||
1703 | Type *ResultTy = nullptr; | ||||
1704 | Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record); | ||||
1705 | if (!MaybeRecord) | ||||
1706 | return MaybeRecord.takeError(); | ||||
1707 | switch (MaybeRecord.get()) { | ||||
1708 | default: | ||||
1709 | return error("Invalid value"); | ||||
1710 | case bitc::TYPE_CODE_NUMENTRY: // TYPE_CODE_NUMENTRY: [numentries] | ||||
1711 | // TYPE_CODE_NUMENTRY contains a count of the number of types in the | ||||
1712 | // type list. This allows us to reserve space. | ||||
1713 | if (Record.empty()) | ||||
1714 | return error("Invalid record"); | ||||
1715 | TypeList.resize(Record[0]); | ||||
1716 | continue; | ||||
1717 | case bitc::TYPE_CODE_VOID: // VOID | ||||
1718 | ResultTy = Type::getVoidTy(Context); | ||||
1719 | break; | ||||
1720 | case bitc::TYPE_CODE_HALF: // HALF | ||||
1721 | ResultTy = Type::getHalfTy(Context); | ||||
1722 | break; | ||||
1723 | case bitc::TYPE_CODE_BFLOAT: // BFLOAT | ||||
1724 | ResultTy = Type::getBFloatTy(Context); | ||||
1725 | break; | ||||
1726 | case bitc::TYPE_CODE_FLOAT: // FLOAT | ||||
1727 | ResultTy = Type::getFloatTy(Context); | ||||
1728 | break; | ||||
1729 | case bitc::TYPE_CODE_DOUBLE: // DOUBLE | ||||
1730 | ResultTy = Type::getDoubleTy(Context); | ||||
1731 | break; | ||||
1732 | case bitc::TYPE_CODE_X86_FP80: // X86_FP80 | ||||
1733 | ResultTy = Type::getX86_FP80Ty(Context); | ||||
1734 | break; | ||||
1735 | case bitc::TYPE_CODE_FP128: // FP128 | ||||
1736 | ResultTy = Type::getFP128Ty(Context); | ||||
1737 | break; | ||||
1738 | case bitc::TYPE_CODE_PPC_FP128: // PPC_FP128 | ||||
1739 | ResultTy = Type::getPPC_FP128Ty(Context); | ||||
1740 | break; | ||||
1741 | case bitc::TYPE_CODE_LABEL: // LABEL | ||||
1742 | ResultTy = Type::getLabelTy(Context); | ||||
1743 | break; | ||||
1744 | case bitc::TYPE_CODE_METADATA: // METADATA | ||||
1745 | ResultTy = Type::getMetadataTy(Context); | ||||
1746 | break; | ||||
1747 | case bitc::TYPE_CODE_X86_MMX: // X86_MMX | ||||
1748 | ResultTy = Type::getX86_MMXTy(Context); | ||||
1749 | break; | ||||
1750 | case bitc::TYPE_CODE_X86_AMX: // X86_AMX | ||||
1751 | ResultTy = Type::getX86_AMXTy(Context); | ||||
1752 | break; | ||||
1753 | case bitc::TYPE_CODE_TOKEN: // TOKEN | ||||
1754 | ResultTy = Type::getTokenTy(Context); | ||||
1755 | break; | ||||
1756 | case bitc::TYPE_CODE_INTEGER: { // INTEGER: [width] | ||||
1757 | if (Record.empty()) | ||||
1758 | return error("Invalid record"); | ||||
1759 | |||||
1760 | uint64_t NumBits = Record[0]; | ||||
1761 | if (NumBits < IntegerType::MIN_INT_BITS || | ||||
1762 | NumBits > IntegerType::MAX_INT_BITS) | ||||
1763 | return error("Bitwidth for integer type out of range"); | ||||
1764 | ResultTy = IntegerType::get(Context, NumBits); | ||||
1765 | break; | ||||
1766 | } | ||||
1767 | case bitc::TYPE_CODE_POINTER: { // POINTER: [pointee type] or | ||||
1768 | // [pointee type, address space] | ||||
1769 | if (Record.empty()) | ||||
1770 | return error("Invalid record"); | ||||
1771 | unsigned AddressSpace = 0; | ||||
1772 | if (Record.size() == 2) | ||||
1773 | AddressSpace = Record[1]; | ||||
1774 | ResultTy = getTypeByID(Record[0]); | ||||
1775 | if (!ResultTy || | ||||
1776 | !PointerType::isValidElementType(ResultTy)) | ||||
1777 | return error("Invalid type"); | ||||
1778 | ResultTy = PointerType::get(ResultTy, AddressSpace); | ||||
1779 | break; | ||||
1780 | } | ||||
1781 | case bitc::TYPE_CODE_OPAQUE_POINTER: { // OPAQUE_POINTER: [addrspace] | ||||
1782 | if (Record.size() != 1) | ||||
1783 | return error("Invalid record"); | ||||
1784 | unsigned AddressSpace = Record[0]; | ||||
1785 | ResultTy = PointerType::get(Context, AddressSpace); | ||||
1786 | break; | ||||
1787 | } | ||||
1788 | case bitc::TYPE_CODE_FUNCTION_OLD: { | ||||
1789 | // Deprecated, but still needed to read old bitcode files. | ||||
1790 | // FUNCTION: [vararg, attrid, retty, paramty x N] | ||||
1791 | if (Record.size() < 3) | ||||
1792 | return error("Invalid record"); | ||||
1793 | SmallVector<Type*, 8> ArgTys; | ||||
1794 | for (unsigned i = 3, e = Record.size(); i != e; ++i) { | ||||
1795 | if (Type *T = getTypeByID(Record[i])) | ||||
1796 | ArgTys.push_back(T); | ||||
1797 | else | ||||
1798 | break; | ||||
1799 | } | ||||
1800 | |||||
1801 | ResultTy = getTypeByID(Record[2]); | ||||
1802 | if (!ResultTy || ArgTys.size() < Record.size()-3) | ||||
1803 | return error("Invalid type"); | ||||
1804 | |||||
1805 | ResultTy = FunctionType::get(ResultTy, ArgTys, Record[0]); | ||||
1806 | break; | ||||
1807 | } | ||||
1808 | case bitc::TYPE_CODE_FUNCTION: { | ||||
1809 | // FUNCTION: [vararg, retty, paramty x N] | ||||
1810 | if (Record.size() < 2) | ||||
1811 | return error("Invalid record"); | ||||
1812 | SmallVector<Type*, 8> ArgTys; | ||||
1813 | for (unsigned i = 2, e = Record.size(); i != e; ++i) { | ||||
1814 | if (Type *T = getTypeByID(Record[i])) { | ||||
1815 | if (!FunctionType::isValidArgumentType(T)) | ||||
1816 | return error("Invalid function argument type"); | ||||
1817 | ArgTys.push_back(T); | ||||
1818 | } | ||||
1819 | else | ||||
1820 | break; | ||||
1821 | } | ||||
1822 | |||||
1823 | ResultTy = getTypeByID(Record[1]); | ||||
1824 | if (!ResultTy || ArgTys.size() < Record.size()-2) | ||||
1825 | return error("Invalid type"); | ||||
1826 | |||||
1827 | ResultTy = FunctionType::get(ResultTy, ArgTys, Record[0]); | ||||
1828 | break; | ||||
1829 | } | ||||
1830 | case bitc::TYPE_CODE_STRUCT_ANON: { // STRUCT: [ispacked, eltty x N] | ||||
1831 | if (Record.empty()) | ||||
1832 | return error("Invalid record"); | ||||
1833 | SmallVector<Type*, 8> EltTys; | ||||
1834 | for (unsigned i = 1, e = Record.size(); i != e; ++i) { | ||||
1835 | if (Type *T = getTypeByID(Record[i])) | ||||
1836 | EltTys.push_back(T); | ||||
1837 | else | ||||
1838 | break; | ||||
1839 | } | ||||
1840 | if (EltTys.size() != Record.size()-1) | ||||
1841 | return error("Invalid type"); | ||||
1842 | ResultTy = StructType::get(Context, EltTys, Record[0]); | ||||
1843 | break; | ||||
1844 | } | ||||
1845 | case bitc::TYPE_CODE_STRUCT_NAME: // STRUCT_NAME: [strchr x N] | ||||
1846 | if (convertToString(Record, 0, TypeName)) | ||||
1847 | return error("Invalid record"); | ||||
1848 | continue; | ||||
1849 | |||||
1850 | case bitc::TYPE_CODE_STRUCT_NAMED: { // STRUCT: [ispacked, eltty x N] | ||||
1851 | if (Record.empty()) | ||||
1852 | return error("Invalid record"); | ||||
1853 | |||||
1854 | if (NumRecords >= TypeList.size()) | ||||
1855 | return error("Invalid TYPE table"); | ||||
1856 | |||||
1857 | // Check to see if this was forward referenced, if so fill in the temp. | ||||
1858 | StructType *Res = cast_or_null<StructType>(TypeList[NumRecords]); | ||||
1859 | if (Res) { | ||||
1860 | Res->setName(TypeName); | ||||
1861 | TypeList[NumRecords] = nullptr; | ||||
1862 | } else // Otherwise, create a new struct. | ||||
1863 | Res = createIdentifiedStructType(Context, TypeName); | ||||
1864 | TypeName.clear(); | ||||
1865 | |||||
1866 | SmallVector<Type*, 8> EltTys; | ||||
1867 | for (unsigned i = 1, e = Record.size(); i != e; ++i) { | ||||
1868 | if (Type *T = getTypeByID(Record[i])) | ||||
1869 | EltTys.push_back(T); | ||||
1870 | else | ||||
1871 | break; | ||||
1872 | } | ||||
1873 | if (EltTys.size() != Record.size()-1) | ||||
1874 | return error("Invalid record"); | ||||
1875 | Res->setBody(EltTys, Record[0]); | ||||
1876 | ResultTy = Res; | ||||
1877 | break; | ||||
1878 | } | ||||
1879 | case bitc::TYPE_CODE_OPAQUE: { // OPAQUE: [] | ||||
1880 | if (Record.size() != 1) | ||||
1881 | return error("Invalid record"); | ||||
1882 | |||||
1883 | if (NumRecords >= TypeList.size()) | ||||
1884 | return error("Invalid TYPE table"); | ||||
1885 | |||||
1886 | // Check to see if this was forward referenced, if so fill in the temp. | ||||
1887 | StructType *Res = cast_or_null<StructType>(TypeList[NumRecords]); | ||||
1888 | if (Res) { | ||||
1889 | Res->setName(TypeName); | ||||
1890 | TypeList[NumRecords] = nullptr; | ||||
1891 | } else // Otherwise, create a new struct with no body. | ||||
1892 | Res = createIdentifiedStructType(Context, TypeName); | ||||
1893 | TypeName.clear(); | ||||
1894 | ResultTy = Res; | ||||
1895 | break; | ||||
1896 | } | ||||
1897 | case bitc::TYPE_CODE_ARRAY: // ARRAY: [numelts, eltty] | ||||
1898 | if (Record.size() < 2) | ||||
1899 | return error("Invalid record"); | ||||
1900 | ResultTy = getTypeByID(Record[1]); | ||||
1901 | if (!ResultTy || !ArrayType::isValidElementType(ResultTy)) | ||||
1902 | return error("Invalid type"); | ||||
1903 | ResultTy = ArrayType::get(ResultTy, Record[0]); | ||||
1904 | break; | ||||
1905 | case bitc::TYPE_CODE_VECTOR: // VECTOR: [numelts, eltty] or | ||||
1906 | // [numelts, eltty, scalable] | ||||
1907 | if (Record.size() < 2) | ||||
1908 | return error("Invalid record"); | ||||
1909 | if (Record[0] == 0) | ||||
1910 | return error("Invalid vector length"); | ||||
1911 | ResultTy = getTypeByID(Record[1]); | ||||
1912 | if (!ResultTy || !StructType::isValidElementType(ResultTy)) | ||||
1913 | return error("Invalid type"); | ||||
1914 | bool Scalable = Record.size() > 2 ? Record[2] : false; | ||||
1915 | ResultTy = VectorType::get(ResultTy, Record[0], Scalable); | ||||
1916 | break; | ||||
1917 | } | ||||
1918 | |||||
1919 | if (NumRecords >= TypeList.size()) | ||||
1920 | return error("Invalid TYPE table"); | ||||
1921 | if (TypeList[NumRecords]) | ||||
1922 | return error( | ||||
1923 | "Invalid TYPE table: Only named structs can be forward referenced"); | ||||
1924 | assert(ResultTy && "Didn't read a type?")(static_cast <bool> (ResultTy && "Didn't read a type?" ) ? void (0) : __assert_fail ("ResultTy && \"Didn't read a type?\"" , "/build/llvm-toolchain-snapshot-13~++20210621111111+acefe0eaaf82/llvm/lib/Bitcode/Reader/BitcodeReader.cpp" , 1924, __extension__ __PRETTY_FUNCTION__)); | ||||
1925 | TypeList[NumRecords++] = ResultTy; | ||||
1926 | } | ||||
1927 | } | ||||
1928 | |||||
1929 | Error BitcodeReader::parseOperandBundleTags() { | ||||
1930 | if (Error Err = Stream.EnterSubBlock(bitc::OPERAND_BUNDLE_TAGS_BLOCK_ID)) | ||||
1931 | return Err; | ||||
1932 | |||||
1933 | if (!BundleTags.empty()) | ||||
1934 | return error("Invalid multiple blocks"); | ||||
1935 | |||||
1936 | SmallVector<uint64_t, 64> Record; | ||||
1937 | |||||
1938 | while (true) { | ||||
1939 | Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks(); | ||||
1940 | if (!MaybeEntry) | ||||
1941 | return MaybeEntry.takeError(); | ||||
1942 | BitstreamEntry Entry = MaybeEntry.get(); | ||||
1943 | |||||
1944 | switch (Entry.Kind) { | ||||
1945 | case BitstreamEntry::SubBlock: // Handled for us already. | ||||
1946 | case BitstreamEntry::Error: | ||||
1947 | return error("Malformed block"); | ||||
1948 | case BitstreamEntry::EndBlock: | ||||
1949 | return Error::success(); | ||||
1950 | case BitstreamEntry::Record: | ||||
1951 | // The interesting case. | ||||
1952 | break; | ||||
1953 | } | ||||
1954 | |||||
1955 | // Tags are implicitly mapped to integers by their order. | ||||
1956 | |||||
1957 | Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record); | ||||
1958 | if (!MaybeRecord) | ||||
1959 | return MaybeRecord.takeError(); | ||||
1960 | if (MaybeRecord.get() != bitc::OPERAND_BUNDLE_TAG) | ||||
1961 | return error("Invalid record"); | ||||
1962 | |||||
1963 | // OPERAND_BUNDLE_TAG: [strchr x N] | ||||
1964 | BundleTags.emplace_back(); | ||||
1965 | if (convertToString(Record, 0, BundleTags.back())) | ||||
1966 | return error("Invalid record"); | ||||
1967 | Record.clear(); | ||||
1968 | } | ||||
1969 | } | ||||
1970 | |||||
1971 | Error BitcodeReader::parseSyncScopeNames() { | ||||
1972 | if (Error Err = Stream.EnterSubBlock(bitc::SYNC_SCOPE_NAMES_BLOCK_ID)) | ||||
1973 | return Err; | ||||
1974 | |||||
1975 | if (!SSIDs.empty()) | ||||
1976 | return error("Invalid multiple synchronization scope names blocks"); | ||||
1977 | |||||
1978 | SmallVector<uint64_t, 64> Record; | ||||
1979 | while (true) { | ||||
1980 | Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks(); | ||||
1981 | if (!MaybeEntry) | ||||
1982 | return MaybeEntry.takeError(); | ||||
1983 | BitstreamEntry Entry = MaybeEntry.get(); | ||||
1984 | |||||
1985 | switch (Entry.Kind) { | ||||
1986 | case BitstreamEntry::SubBlock: // Handled for us already. | ||||
1987 | case BitstreamEntry::Error: | ||||
1988 | return error("Malformed block"); | ||||
1989 | case BitstreamEntry::EndBlock: | ||||
1990 | if (SSIDs.empty()) | ||||
1991 | return error("Invalid empty synchronization scope names block"); | ||||
1992 | return Error::success(); | ||||
1993 | case BitstreamEntry::Record: | ||||
1994 | // The interesting case. | ||||
1995 | break; | ||||
1996 | } | ||||
1997 | |||||
1998 | // Synchronization scope names are implicitly mapped to synchronization | ||||
1999 | // scope IDs by their order. | ||||
2000 | |||||
2001 | Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record); | ||||
2002 | if (!MaybeRecord) | ||||
2003 | return MaybeRecord.takeError(); | ||||
2004 | if (MaybeRecord.get() != bitc::SYNC_SCOPE_NAME) | ||||
2005 | return error("Invalid record"); | ||||
2006 | |||||
2007 | SmallString<16> SSN; | ||||
2008 | if (convertToString(Record, 0, SSN)) | ||||
2009 | return error("Invalid record"); | ||||
2010 | |||||
2011 | SSIDs.push_back(Context.getOrInsertSyncScopeID(SSN)); | ||||
2012 | Record.clear(); | ||||
2013 | } | ||||
2014 | } | ||||
2015 | |||||
2016 | /// Associate a value with its name from the given index in the provided record. | ||||
2017 | Expected<Value *> BitcodeReader::recordValue(SmallVectorImpl<uint64_t> &Record, | ||||
2018 | unsigned NameIndex, Triple &TT) { | ||||
2019 | SmallString<128> ValueName; | ||||
2020 | if (convertToString(Record, NameIndex, ValueName)) | ||||
2021 | return error("Invalid record"); | ||||
2022 | unsigned ValueID = Record[0]; | ||||
2023 | if (ValueID >= ValueList.size() || !ValueList[ValueID]) | ||||
2024 | return error("Invalid record"); | ||||
2025 | Value *V = ValueList[ValueID]; | ||||
2026 | |||||
2027 | StringRef NameStr(ValueName.data(), ValueName.size()); | ||||
2028 | if (NameStr.find_first_of(0) != StringRef::npos) | ||||
2029 | return error("Invalid value name"); | ||||
2030 | V->setName(NameStr); | ||||
2031 | auto *GO = dyn_cast<GlobalObject>(V); | ||||
2032 | if (GO) { | ||||
2033 | if (GO->getComdat() == reinterpret_cast<Comdat *>(1)) { | ||||
2034 | if (TT.supportsCOMDAT()) | ||||
2035 | GO->setComdat(TheModule->getOrInsertComdat(V->getName())); | ||||
2036 | else | ||||
2037 | GO->setComdat(nullptr); | ||||
2038 | } | ||||
2039 | } | ||||
2040 | return V; | ||||
2041 | } | ||||
2042 | |||||
2043 | /// Helper to note and return the current location, and jump to the given | ||||
2044 | /// offset. | ||||
2045 | static Expected<uint64_t> jumpToValueSymbolTable(uint64_t Offset, | ||||
2046 | BitstreamCursor &Stream) { | ||||
2047 | // Save the current parsing location so we can jump back at the end | ||||
2048 | // of the VST read. | ||||
2049 | uint64_t CurrentBit = Stream.GetCurrentBitNo(); | ||||
2050 | if (Error JumpFailed = Stream.JumpToBit(Offset * 32)) | ||||
2051 | return std::move(JumpFailed); | ||||
2052 | Expected<BitstreamEntry> MaybeEntry = Stream.advance(); | ||||
2053 | if (!MaybeEntry) | ||||
2054 | return MaybeEntry.takeError(); | ||||
2055 | assert(MaybeEntry.get().Kind == BitstreamEntry::SubBlock)(static_cast <bool> (MaybeEntry.get().Kind == BitstreamEntry ::SubBlock) ? void (0) : __assert_fail ("MaybeEntry.get().Kind == BitstreamEntry::SubBlock" , "/build/llvm-toolchain-snapshot-13~++20210621111111+acefe0eaaf82/llvm/lib/Bitcode/Reader/BitcodeReader.cpp" , 2055, __extension__ __PRETTY_FUNCTION__)); | ||||
2056 | assert(MaybeEntry.get().ID == bitc::VALUE_SYMTAB_BLOCK_ID)(static_cast <bool> (MaybeEntry.get().ID == bitc::VALUE_SYMTAB_BLOCK_ID ) ? void (0) : __assert_fail ("MaybeEntry.get().ID == bitc::VALUE_SYMTAB_BLOCK_ID" , "/build/llvm-toolchain-snapshot-13~++20210621111111+acefe0eaaf82/llvm/lib/Bitcode/Reader/BitcodeReader.cpp" , 2056, __extension__ __PRETTY_FUNCTION__)); | ||||
2057 | return CurrentBit; | ||||
2058 | } | ||||
2059 | |||||
2060 | void BitcodeReader::setDeferredFunctionInfo(unsigned FuncBitcodeOffsetDelta, | ||||
2061 | Function *F, | ||||
2062 | ArrayRef<uint64_t> Record) { | ||||
2063 | // Note that we subtract 1 here because the offset is relative to one word | ||||
2064 | // before the start of the identification or module block, which was | ||||
2065 | // historically always the start of the regular bitcode header. | ||||
2066 | uint64_t FuncWordOffset = Record[1] - 1; | ||||
2067 | uint64_t FuncBitOffset = FuncWordOffset * 32; | ||||
2068 | DeferredFunctionInfo[F] = FuncBitOffset + FuncBitcodeOffsetDelta; | ||||
2069 | // Set the LastFunctionBlockBit to point to the last function block. | ||||
2070 | // Later when parsing is resumed after function materialization, | ||||
2071 | // we can simply skip that last function block. | ||||
2072 | if (FuncBitOffset > LastFunctionBlockBit) | ||||
2073 | LastFunctionBlockBit = FuncBitOffset; | ||||
2074 | } | ||||
2075 | |||||
2076 | /// Read a new-style GlobalValue symbol table. | ||||
2077 | Error BitcodeReader::parseGlobalValueSymbolTable() { | ||||
2078 | unsigned FuncBitcodeOffsetDelta = | ||||
2079 | Stream.getAbbrevIDWidth() + bitc::BlockIDWidth; | ||||
2080 | |||||
2081 | if (Error Err = Stream.EnterSubBlock(bitc::VALUE_SYMTAB_BLOCK_ID)) | ||||
2082 | return Err; | ||||
2083 | |||||
2084 | SmallVector<uint64_t, 64> Record; | ||||
2085 | while (true) { | ||||
2086 | Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks(); | ||||
2087 | if (!MaybeEntry) | ||||
2088 | return MaybeEntry.takeError(); | ||||
2089 | BitstreamEntry Entry = MaybeEntry.get(); | ||||
2090 | |||||
2091 | switch (Entry.Kind) { | ||||
2092 | case BitstreamEntry::SubBlock: | ||||
2093 | case BitstreamEntry::Error: | ||||
2094 | return error("Malformed block"); | ||||
2095 | case BitstreamEntry::EndBlock: | ||||
2096 | return Error::success(); | ||||
2097 | case BitstreamEntry::Record: | ||||
2098 | break; | ||||
2099 | } | ||||
2100 | |||||
2101 | Record.clear(); | ||||
2102 | Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record); | ||||
2103 | if (!MaybeRecord) | ||||
2104 | return MaybeRecord.takeError(); | ||||
2105 | switch (MaybeRecord.get()) { | ||||
2106 | case bitc::VST_CODE_FNENTRY: // [valueid, offset] | ||||
2107 | setDeferredFunctionInfo(FuncBitcodeOffsetDelta, | ||||
2108 | cast<Function>(ValueList[Record[0]]), Record); | ||||
2109 | break; | ||||
2110 | } | ||||
2111 | } | ||||
2112 | } | ||||
2113 | |||||
2114 | /// Parse the value symbol table at either the current parsing location or | ||||
2115 | /// at the given bit offset if provided. | ||||
2116 | Error BitcodeReader::parseValueSymbolTable(uint64_t Offset) { | ||||
2117 | uint64_t CurrentBit; | ||||
2118 | // Pass in the Offset to distinguish between calling for the module-level | ||||
2119 | // VST (where we want to jump to the VST offset) and the function-level | ||||
2120 | // VST (where we don't). | ||||
2121 | if (Offset > 0) { | ||||
2122 | Expected<uint64_t> MaybeCurrentBit = jumpToValueSymbolTable(Offset, Stream); | ||||
2123 | if (!MaybeCurrentBit) | ||||
2124 | return MaybeCurrentBit.takeError(); | ||||
2125 | CurrentBit = MaybeCurrentBit.get(); | ||||
2126 | // If this module uses a string table, read this as a module-level VST. | ||||
2127 | if (UseStrtab) { | ||||
2128 | if (Error Err = parseGlobalValueSymbolTable()) | ||||
2129 | return Err; | ||||
2130 | if (Error JumpFailed = Stream.JumpToBit(CurrentBit)) | ||||
2131 | return JumpFailed; | ||||
2132 | return Error::success(); | ||||
2133 | } | ||||
2134 | // Otherwise, the VST will be in a similar format to a function-level VST, | ||||
2135 | // and will contain symbol names. | ||||
2136 | } | ||||
2137 | |||||
2138 | // Compute the delta between the bitcode indices in the VST (the word offset | ||||
2139 | // to the word-aligned ENTER_SUBBLOCK for the function block, and that | ||||
2140 | // expected by the lazy reader. The reader's EnterSubBlock expects to have | ||||
2141 | // already read the ENTER_SUBBLOCK code (size getAbbrevIDWidth) and BlockID | ||||
2142 | // (size BlockIDWidth). Note that we access the stream's AbbrevID width here | ||||
2143 | // just before entering the VST subblock because: 1) the EnterSubBlock | ||||
2144 | // changes the AbbrevID width; 2) the VST block is nested within the same | ||||
2145 | // outer MODULE_BLOCK as the FUNCTION_BLOCKs and therefore have the same | ||||
2146 | // AbbrevID width before calling EnterSubBlock; and 3) when we want to | ||||
2147 | // jump to the FUNCTION_BLOCK using this offset later, we don't want | ||||
2148 | // to rely on the stream's AbbrevID width being that of the MODULE_BLOCK. | ||||
2149 | unsigned FuncBitcodeOffsetDelta = | ||||
2150 | Stream.getAbbrevIDWidth() + bitc::BlockIDWidth; | ||||
2151 | |||||
2152 | if (Error Err = Stream.EnterSubBlock(bitc::VALUE_SYMTAB_BLOCK_ID)) | ||||
2153 | return Err; | ||||
2154 | |||||
2155 | SmallVector<uint64_t, 64> Record; | ||||
2156 | |||||
2157 | Triple TT(TheModule->getTargetTriple()); | ||||
2158 | |||||
2159 | // Read all the records for this value table. | ||||
2160 | SmallString<128> ValueName; | ||||
2161 | |||||
2162 | while (true) { | ||||
2163 | Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks(); | ||||
2164 | if (!MaybeEntry) | ||||
2165 | return MaybeEntry.takeError(); | ||||
2166 | BitstreamEntry Entry = MaybeEntry.get(); | ||||
2167 | |||||
2168 | switch (Entry.Kind) { | ||||
2169 | case BitstreamEntry::SubBlock: // Handled for us already. | ||||
2170 | case BitstreamEntry::Error: | ||||
2171 | return error("Malformed block"); | ||||
2172 | case BitstreamEntry::EndBlock: | ||||
2173 | if (Offset > 0) | ||||
2174 | if (Error JumpFailed = Stream.JumpToBit(CurrentBit)) | ||||
2175 | return JumpFailed; | ||||
2176 | return Error::success(); | ||||
2177 | case BitstreamEntry::Record: | ||||
2178 | // The interesting case. | ||||
2179 | break; | ||||
2180 | } | ||||
2181 | |||||
2182 | // Read a record. | ||||
2183 | Record.clear(); | ||||
2184 | Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record); | ||||
2185 | if (!MaybeRecord) | ||||
2186 | return MaybeRecord.takeError(); | ||||
2187 | switch (MaybeRecord.get()) { | ||||
2188 | default: // Default behavior: unknown type. | ||||
2189 | break; | ||||
2190 | case bitc::VST_CODE_ENTRY: { // VST_CODE_ENTRY: [valueid, namechar x N] | ||||
2191 | Expected<Value *> ValOrErr = recordValue(Record, 1, TT); | ||||
2192 | if (Error Err = ValOrErr.takeError()) | ||||
2193 | return Err; | ||||
2194 | ValOrErr.get(); | ||||
2195 | break; | ||||
2196 | } | ||||
2197 | case bitc::VST_CODE_FNENTRY: { | ||||
2198 | // VST_CODE_FNENTRY: [valueid, offset, namechar x N] | ||||
2199 | Expected<Value *> ValOrErr = recordValue(Record, 2, TT); | ||||
2200 | if (Error Err = ValOrErr.takeError()) | ||||
2201 | return Err; | ||||
2202 | Value *V = ValOrErr.get(); | ||||
2203 | |||||
2204 | // Ignore function offsets emitted for aliases of functions in older | ||||
2205 | // versions of LLVM. | ||||
2206 | if (auto *F = dyn_cast<Function>(V)) | ||||
2207 | setDeferredFunctionInfo(FuncBitcodeOffsetDelta, F, Record); | ||||
2208 | break; | ||||
2209 | } | ||||
2210 | case bitc::VST_CODE_BBENTRY: { | ||||
2211 | if (convertToString(Record, 1, ValueName)) | ||||
2212 | return error("Invalid record"); | ||||
2213 | BasicBlock *BB = getBasicBlock(Record[0]); | ||||
2214 | if (!BB) | ||||
2215 | return error("Invalid record"); | ||||
2216 | |||||
2217 | BB->setName(StringRef(ValueName.data(), ValueName.size())); | ||||
2218 | ValueName.clear(); | ||||
2219 | break; | ||||
2220 | } | ||||
2221 | } | ||||
2222 | } | ||||
2223 | } | ||||
2224 | |||||
2225 | /// Decode a signed value stored with the sign bit in the LSB for dense VBR | ||||
2226 | /// encoding. | ||||
2227 | uint64_t BitcodeReader::decodeSignRotatedValue(uint64_t V) { | ||||
2228 | if ((V & 1) == 0) | ||||
2229 | return V >> 1; | ||||
2230 | if (V != 1) | ||||
2231 | return -(V >> 1); | ||||
2232 | // There is no such thing as -0 with integers. "-0" really means MININT. | ||||
2233 | return 1ULL << 63; | ||||
2234 | } | ||||
2235 | |||||
2236 | /// Resolve all of the initializers for global values and aliases that we can. | ||||
2237 | Error BitcodeReader::resolveGlobalAndIndirectSymbolInits() { | ||||
2238 | std::vector<std::pair<GlobalVariable *, unsigned>> GlobalInitWorklist; | ||||
2239 | std::vector<std::pair<GlobalIndirectSymbol *, unsigned>> | ||||
2240 | IndirectSymbolInitWorklist; | ||||
2241 | std::vector<std::pair<Function *, unsigned>> FunctionPrefixWorklist; | ||||
2242 | std::vector<std::pair<Function *, unsigned>> FunctionPrologueWorklist; | ||||
2243 | std::vector<std::pair<Function *, unsigned>> FunctionPersonalityFnWorklist; | ||||
2244 | |||||
2245 | GlobalInitWorklist.swap(GlobalInits); | ||||
2246 | IndirectSymbolInitWorklist.swap(IndirectSymbolInits); | ||||
2247 | FunctionPrefixWorklist.swap(FunctionPrefixes); | ||||
2248 | FunctionPrologueWorklist.swap(FunctionPrologues); | ||||
2249 | FunctionPersonalityFnWorklist.swap(FunctionPersonalityFns); | ||||
2250 | |||||
2251 | while (!GlobalInitWorklist.empty()) { | ||||
2252 | unsigned ValID = GlobalInitWorklist.back().second; | ||||
2253 | if (ValID >= ValueList.size()) { | ||||
2254 | // Not ready to resolve this yet, it requires something later in the file. | ||||
2255 | GlobalInits.push_back(GlobalInitWorklist.back()); | ||||
2256 | } else { | ||||
2257 | if (Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID])) | ||||
2258 | GlobalInitWorklist.back().first->setInitializer(C); | ||||
2259 | else | ||||
2260 | return error("Expected a constant"); | ||||
2261 | } | ||||
2262 | GlobalInitWorklist.pop_back(); | ||||
2263 | } | ||||
2264 | |||||
2265 | while (!IndirectSymbolInitWorklist.empty()) { | ||||
2266 | unsigned ValID = IndirectSymbolInitWorklist.back().second; | ||||
2267 | if (ValID >= ValueList.size()) { | ||||
2268 | IndirectSymbolInits.push_back(IndirectSymbolInitWorklist.back()); | ||||
2269 | } else { | ||||
2270 | Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID]); | ||||
2271 | if (!C) | ||||
2272 | return error("Expected a constant"); | ||||
2273 | GlobalIndirectSymbol *GIS = IndirectSymbolInitWorklist.back().first; | ||||
2274 | if (isa<GlobalAlias>(GIS) && C->getType() != GIS->getType()) | ||||
2275 | return error("Alias and aliasee types don't match"); | ||||
2276 | GIS->setIndirectSymbol(C); | ||||
2277 | } | ||||
2278 | IndirectSymbolInitWorklist.pop_back(); | ||||
2279 | } | ||||
2280 | |||||
2281 | while (!FunctionPrefixWorklist.empty()) { | ||||
2282 | unsigned ValID = FunctionPrefixWorklist.back().second; | ||||
2283 | if (ValID >= ValueList.size()) { | ||||
2284 | FunctionPrefixes.push_back(FunctionPrefixWorklist.back()); | ||||
2285 | } else { | ||||
2286 | if (Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID])) | ||||
2287 | FunctionPrefixWorklist.back().first->setPrefixData(C); | ||||
2288 | else | ||||
2289 | return error("Expected a constant"); | ||||
2290 | } | ||||
2291 | FunctionPrefixWorklist.pop_back(); | ||||
2292 | } | ||||
2293 | |||||
2294 | while (!FunctionPrologueWorklist.empty()) { | ||||
2295 | unsigned ValID = FunctionPrologueWorklist.back().second; | ||||
2296 | if (ValID >= ValueList.size()) { | ||||
2297 | FunctionPrologues.push_back(FunctionPrologueWorklist.back()); | ||||
2298 | } else { | ||||
2299 | if (Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID])) | ||||
2300 | FunctionPrologueWorklist.back().first->setPrologueData(C); | ||||
2301 | else | ||||
2302 | return error("Expected a constant"); | ||||
2303 | } | ||||
2304 | FunctionPrologueWorklist.pop_back(); | ||||
2305 | } | ||||
2306 | |||||
2307 | while (!FunctionPersonalityFnWorklist.empty()) { | ||||
2308 | unsigned ValID = FunctionPersonalityFnWorklist.back().second; | ||||
2309 | if (ValID >= ValueList.size()) { | ||||
2310 | FunctionPersonalityFns.push_back(FunctionPersonalityFnWorklist.back()); | ||||
2311 | } else { | ||||
2312 | if (Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID])) | ||||
2313 | FunctionPersonalityFnWorklist.back().first->setPersonalityFn(C); | ||||
2314 | else | ||||
2315 | return error("Expected a constant"); | ||||
2316 | } | ||||
2317 | FunctionPersonalityFnWorklist.pop_back(); | ||||
2318 | } | ||||
2319 | |||||
2320 | return Error::success(); | ||||
2321 | } | ||||
2322 | |||||
2323 | APInt llvm::readWideAPInt(ArrayRef<uint64_t> Vals, unsigned TypeBits) { | ||||
2324 | SmallVector<uint64_t, 8> Words(Vals.size()); | ||||
2325 | transform(Vals, Words.begin(), | ||||
2326 | BitcodeReader::decodeSignRotatedValue); | ||||
2327 | |||||
2328 | return APInt(TypeBits, Words); | ||||
2329 | } | ||||
2330 | |||||
2331 | Error BitcodeReader::parseConstants() { | ||||
2332 | if (Error Err = Stream.EnterSubBlock(bitc::CONSTANTS_BLOCK_ID)) | ||||
| |||||
2333 | return Err; | ||||
2334 | |||||
2335 | SmallVector<uint64_t, 64> Record; | ||||
2336 | |||||
2337 | // Read all the records for this value table. | ||||
2338 | Type *CurTy = Type::getInt32Ty(Context); | ||||
2339 | unsigned NextCstNo = ValueList.size(); | ||||
2340 | |||||
2341 | struct DelayedShufTy { | ||||
2342 | VectorType *OpTy; | ||||
2343 | VectorType *RTy; | ||||
2344 | uint64_t Op0Idx; | ||||
2345 | uint64_t Op1Idx; | ||||
2346 | uint64_t Op2Idx; | ||||
2347 | unsigned CstNo; | ||||
2348 | }; | ||||
2349 | std::vector<DelayedShufTy> DelayedShuffles; | ||||
2350 | while (true) { | ||||
2351 | Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks(); | ||||
2352 | if (!MaybeEntry) | ||||
2353 | return MaybeEntry.takeError(); | ||||
2354 | BitstreamEntry Entry = MaybeEntry.get(); | ||||
2355 | |||||
2356 | switch (Entry.Kind) { | ||||
2357 | case BitstreamEntry::SubBlock: // Handled for us already. | ||||
2358 | case BitstreamEntry::Error: | ||||
2359 | return error("Malformed block"); | ||||
2360 | case BitstreamEntry::EndBlock: | ||||
2361 | // Once all the constants have been read, go through and resolve forward | ||||
2362 | // references. | ||||
2363 | // | ||||
2364 | // We have to treat shuffles specially because they don't have three | ||||
2365 | // operands anymore. We need to convert the shuffle mask into an array, | ||||
2366 | // and we can't convert a forward reference. | ||||
2367 | for (auto &DelayedShuffle : DelayedShuffles) { | ||||
2368 | VectorType *OpTy = DelayedShuffle.OpTy; | ||||
2369 | VectorType *RTy = DelayedShuffle.RTy; | ||||
2370 | uint64_t Op0Idx = DelayedShuffle.Op0Idx; | ||||
2371 | uint64_t Op1Idx = DelayedShuffle.Op1Idx; | ||||
2372 | uint64_t Op2Idx = DelayedShuffle.Op2Idx; | ||||
2373 | uint64_t CstNo = DelayedShuffle.CstNo; | ||||
2374 | Constant *Op0 = ValueList.getConstantFwdRef(Op0Idx, OpTy); | ||||
2375 | Constant *Op1 = ValueList.getConstantFwdRef(Op1Idx, OpTy); | ||||
2376 | Type *ShufTy = | ||||
2377 | VectorType::get(Type::getInt32Ty(Context), RTy->getElementCount()); | ||||
2378 | Constant *Op2 = ValueList.getConstantFwdRef(Op2Idx, ShufTy); | ||||
2379 | if (!ShuffleVectorInst::isValidOperands(Op0, Op1, Op2)) | ||||
2380 | return error("Invalid shufflevector operands"); | ||||
2381 | SmallVector<int, 16> Mask; | ||||
2382 | ShuffleVectorInst::getShuffleMask(Op2, Mask); | ||||
2383 | Value *V = ConstantExpr::getShuffleVector(Op0, Op1, Mask); | ||||
2384 | ValueList.assignValue(V, CstNo); | ||||
2385 | } | ||||
2386 | |||||
2387 | if (NextCstNo != ValueList.size()) | ||||
2388 | return error("Invalid constant reference"); | ||||
2389 | |||||
2390 | ValueList.resolveConstantForwardRefs(); | ||||
2391 | return Error::success(); | ||||
2392 | case BitstreamEntry::Record: | ||||
2393 | // The interesting case. | ||||
2394 | break; | ||||
2395 | } | ||||
2396 | |||||
2397 | // Read a record. | ||||
2398 | Record.clear(); | ||||
2399 | Type *VoidType = Type::getVoidTy(Context); | ||||
2400 | Value *V = nullptr; | ||||
2401 | Expected<unsigned> MaybeBitCode = Stream.readRecord(Entry.ID, Record); | ||||
2402 | if (!MaybeBitCode) | ||||
2403 | return MaybeBitCode.takeError(); | ||||
2404 | switch (unsigned BitCode = MaybeBitCode.get()) { | ||||
2405 | default: // Default behavior: unknown constant | ||||
2406 | case bitc::CST_CODE_UNDEF: // UNDEF | ||||
2407 | V = UndefValue::get(CurTy); | ||||
2408 | break; | ||||
2409 | case bitc::CST_CODE_POISON: // POISON | ||||
2410 | V = PoisonValue::get(CurTy); | ||||
2411 | break; | ||||
2412 | case bitc::CST_CODE_SETTYPE: // SETTYPE: [typeid] | ||||
2413 | if (Record.empty()) | ||||
2414 | return error("Invalid record"); | ||||
2415 | if (Record[0] >= TypeList.size() || !TypeList[Record[0]]) | ||||
2416 | return error("Invalid record"); | ||||
2417 | if (TypeList[Record[0]] == VoidType) | ||||
2418 | return error("Invalid constant type"); | ||||
2419 | CurTy = TypeList[Record[0]]; | ||||
2420 | continue; // Skip the ValueList manipulation. | ||||
2421 | case bitc::CST_CODE_NULL: // NULL | ||||
2422 | if (CurTy->isVoidTy() || CurTy->isFunctionTy() || CurTy->isLabelTy()) | ||||
2423 | return error("Invalid type for a constant null value"); | ||||
2424 | V = Constant::getNullValue(CurTy); | ||||
2425 | break; | ||||
2426 | case bitc::CST_CODE_INTEGER: // INTEGER: [intval] | ||||
2427 | if (!CurTy->isIntegerTy() || Record.empty()) | ||||
2428 | return error("Invalid record"); | ||||
2429 | V = ConstantInt::get(CurTy, decodeSignRotatedValue(Record[0])); | ||||
2430 | break; | ||||
2431 | case bitc::CST_CODE_WIDE_INTEGER: {// WIDE_INTEGER: [n x intval] | ||||
2432 | if (!CurTy->isIntegerTy() || Record.empty()) | ||||
2433 | return error("Invalid record"); | ||||
2434 | |||||
2435 | APInt VInt = | ||||
2436 | readWideAPInt(Record, cast<IntegerType>(CurTy)->getBitWidth()); | ||||
2437 | V = ConstantInt::get(Context, VInt); | ||||
2438 | |||||
2439 | break; | ||||
2440 | } | ||||
2441 | case bitc::CST_CODE_FLOAT: { // FLOAT: [fpval] | ||||
2442 | if (Record.empty()) | ||||
2443 | return error("Invalid record"); | ||||
2444 | if (CurTy->isHalfTy()) | ||||
2445 | V = ConstantFP::get(Context, APFloat(APFloat::IEEEhalf(), | ||||
2446 | APInt(16, (uint16_t)Record[0]))); | ||||
2447 | else if (CurTy->isBFloatTy()) | ||||
2448 | V = ConstantFP::get(Context, APFloat(APFloat::BFloat(), | ||||
2449 | APInt(16, (uint32_t)Record[0]))); | ||||
2450 | else if (CurTy->isFloatTy()) | ||||
2451 | V = ConstantFP::get(Context, APFloat(APFloat::IEEEsingle(), | ||||
2452 | APInt(32, (uint32_t)Record[0]))); | ||||
2453 | else if (CurTy->isDoubleTy()) | ||||
2454 | V = ConstantFP::get(Context, APFloat(APFloat::IEEEdouble(), | ||||
2455 | APInt(64, Record[0]))); | ||||
2456 | else if (CurTy->isX86_FP80Ty()) { | ||||
2457 | // Bits are not stored the same way as a normal i80 APInt, compensate. | ||||
2458 | uint64_t Rearrange[2]; | ||||
2459 | Rearrange[0] = (Record[1] & 0xffffLL) | (Record[0] << 16); | ||||
2460 | Rearrange[1] = Record[0] >> 48; | ||||
2461 | V = ConstantFP::get(Context, APFloat(APFloat::x87DoubleExtended(), | ||||
2462 | APInt(80, Rearrange))); | ||||
2463 | } else if (CurTy->isFP128Ty()) | ||||
2464 | V = ConstantFP::get(Context, APFloat(APFloat::IEEEquad(), | ||||
2465 | APInt(128, Record))); | ||||
2466 | else if (CurTy->isPPC_FP128Ty()) | ||||
2467 | V = ConstantFP::get(Context, APFloat(APFloat::PPCDoubleDouble(), | ||||
2468 | APInt(128, Record))); | ||||
2469 | else | ||||
2470 | V = UndefValue::get(CurTy); | ||||
2471 | break; | ||||
2472 | } | ||||
2473 | |||||
2474 | case bitc::CST_CODE_AGGREGATE: {// AGGREGATE: [n x value number] | ||||
2475 | if (Record.empty()) | ||||
2476 | return error("Invalid record"); | ||||
2477 | |||||
2478 | unsigned Size = Record.size(); | ||||
2479 | SmallVector<Constant*, 16> Elts; | ||||
2480 | |||||
2481 | if (StructType *STy = dyn_cast<StructType>(CurTy)) { | ||||
2482 | for (unsigned i = 0; i != Size; ++i) | ||||
2483 | Elts.push_back(ValueList.getConstantFwdRef(Record[i], | ||||
2484 | STy->getElementType(i))); | ||||
2485 | V = ConstantStruct::get(STy, Elts); | ||||
2486 | } else if (ArrayType *ATy = dyn_cast<ArrayType>(CurTy)) { | ||||
2487 | Type *EltTy = ATy->getElementType(); | ||||
2488 | for (unsigned i = 0; i != Size; ++i) | ||||
2489 | Elts.push_back(ValueList.getConstantFwdRef(Record[i], EltTy)); | ||||
2490 | V = ConstantArray::get(ATy, Elts); | ||||
2491 | } else if (VectorType *VTy = dyn_cast<VectorType>(CurTy)) { | ||||
2492 | Type *EltTy = VTy->getElementType(); | ||||
2493 | for (unsigned i = 0; i != Size; ++i) | ||||
2494 | Elts.push_back(ValueList.getConstantFwdRef(Record[i], EltTy)); | ||||
2495 | V = ConstantVector::get(Elts); | ||||
2496 | } else { | ||||
2497 | V = UndefValue::get(CurTy); | ||||
2498 | } | ||||
2499 | break; | ||||
2500 | } | ||||
2501 | case bitc::CST_CODE_STRING: // STRING: [values] | ||||
2502 | case bitc::CST_CODE_CSTRING: { // CSTRING: [values] | ||||
2503 | if (Record.empty()) | ||||
2504 | return error("Invalid record"); | ||||
2505 | |||||
2506 | SmallString<16> Elts(Record.begin(), Record.end()); | ||||
2507 | V = ConstantDataArray::getString(Context, Elts, | ||||
2508 | BitCode == bitc::CST_CODE_CSTRING); | ||||
2509 | break; | ||||
2510 | } | ||||
2511 | case bitc::CST_CODE_DATA: {// DATA: [n x value] | ||||
2512 | if (Record.empty()) | ||||
2513 | return error("Invalid record"); | ||||
2514 | |||||
2515 | Type *EltTy; | ||||
2516 | if (auto *Array = dyn_cast<ArrayType>(CurTy)) | ||||
2517 | EltTy = Array->getElementType(); | ||||
2518 | else | ||||
2519 | EltTy = cast<VectorType>(CurTy)->getElementType(); | ||||
2520 | if (EltTy->isIntegerTy(8)) { | ||||
2521 | SmallVector<uint8_t, 16> Elts(Record.begin(), Record.end()); | ||||
2522 | if (isa<VectorType>(CurTy)) | ||||
2523 | V = ConstantDataVector::get(Context, Elts); | ||||
2524 | else | ||||
2525 | V = ConstantDataArray::get(Context, Elts); | ||||
2526 | } else if (EltTy->isIntegerTy(16)) { | ||||
2527 | SmallVector<uint16_t, 16> Elts(Record.begin(), Record.end()); | ||||
2528 | if (isa<VectorType>(CurTy)) | ||||
2529 | V = ConstantDataVector::get(Context, Elts); | ||||
2530 | else | ||||
2531 | V = ConstantDataArray::get(Context, Elts); | ||||
2532 | } else if (EltTy->isIntegerTy(32)) { | ||||
2533 | SmallVector<uint32_t, 16> Elts(Record.begin(), Record.end()); | ||||
2534 | if (isa<VectorType>(CurTy)) | ||||
2535 | V = ConstantDataVector::get(Context, Elts); | ||||
2536 | else | ||||
2537 | V = ConstantDataArray::get(Context, Elts); | ||||
2538 | } else if (EltTy->isIntegerTy(64)) { | ||||
2539 | SmallVector<uint64_t, 16> Elts(Record.begin(), Record.end()); | ||||
2540 | if (isa<VectorType>(CurTy)) | ||||
2541 | V = ConstantDataVector::get(Context, Elts); | ||||
2542 | else | ||||
2543 | V = ConstantDataArray::get(Context, Elts); | ||||
2544 | } else if (EltTy->isHalfTy()) { | ||||
2545 | SmallVector<uint16_t, 16> Elts(Record.begin(), Record.end()); | ||||
2546 | if (isa<VectorType>(CurTy)) | ||||
2547 | V = ConstantDataVector::getFP(EltTy, Elts); | ||||
2548 | else | ||||
2549 | V = ConstantDataArray::getFP(EltTy, Elts); | ||||
2550 | } else if (EltTy->isBFloatTy()) { | ||||
2551 | SmallVector<uint16_t, 16> Elts(Record.begin(), Record.end()); | ||||
2552 | if (isa<VectorType>(CurTy)) | ||||
2553 | V = ConstantDataVector::getFP(EltTy, Elts); | ||||
2554 | else | ||||
2555 | V = ConstantDataArray::getFP(EltTy, Elts); | ||||
2556 | } else if (EltTy->isFloatTy()) { | ||||
2557 | SmallVector<uint32_t, 16> Elts(Record.begin(), Record.end()); | ||||
2558 | if (isa<VectorType>(CurTy)) | ||||
2559 | V = ConstantDataVector::getFP(EltTy, Elts); | ||||
2560 | else | ||||
2561 | V = ConstantDataArray::getFP(EltTy, Elts); | ||||
2562 | } else if (EltTy->isDoubleTy()) { | ||||
2563 | SmallVector<uint64_t, 16> Elts(Record.begin(), Record.end()); | ||||
2564 | if (isa<VectorType>(CurTy)) | ||||
2565 | V = ConstantDataVector::getFP(EltTy, Elts); | ||||
2566 | else | ||||
2567 | V = ConstantDataArray::getFP(EltTy, Elts); | ||||
2568 | } else { | ||||
2569 | return error("Invalid type for value"); | ||||
2570 | } | ||||
2571 | break; | ||||
2572 | } | ||||
2573 | case bitc::CST_CODE_CE_UNOP: { // CE_UNOP: [opcode, opval] | ||||
2574 | if (Record.size() < 2) | ||||
2575 | return error("Invalid record"); | ||||
2576 | int Opc = getDecodedUnaryOpcode(Record[0], CurTy); | ||||
2577 | if (Opc < 0) { | ||||
2578 | V = UndefValue::get(CurTy); // Unknown unop. | ||||
2579 | } else { | ||||
2580 | Constant *LHS = ValueList.getConstantFwdRef(Record[1], CurTy); | ||||
2581 | unsigned Flags = 0; | ||||
2582 | V = ConstantExpr::get(Opc, LHS, Flags); | ||||
2583 | } | ||||
2584 | break; | ||||
2585 | } | ||||
2586 | case bitc::CST_CODE_CE_BINOP: { // CE_BINOP: [opcode, opval, opval] | ||||
2587 | if (Record.size() < 3) | ||||
2588 | return error("Invalid record"); | ||||
2589 | int Opc = getDecodedBinaryOpcode(Record[0], CurTy); | ||||
2590 | if (Opc < 0) { | ||||
2591 | V = UndefValue::get(CurTy); // Unknown binop. | ||||
2592 | } else { | ||||
2593 | Constant *LHS = ValueList.getConstantFwdRef(Record[1], CurTy); | ||||
2594 | Constant *RHS = ValueList.getConstantFwdRef(Record[2], CurTy); | ||||
2595 | unsigned Flags = 0; | ||||
2596 | if (Record.size() >= 4) { | ||||
2597 | if (Opc == Instruction::Add || | ||||
2598 | Opc == Instruction::Sub || | ||||
2599 | Opc == Instruction::Mul || | ||||
2600 | Opc == Instruction::Shl) { | ||||
2601 | if (Record[3] & (1 << bitc::OBO_NO_SIGNED_WRAP)) | ||||
2602 | Flags |= OverflowingBinaryOperator::NoSignedWrap; | ||||
2603 | if (Record[3] & (1 << bitc::OBO_NO_UNSIGNED_WRAP)) | ||||
2604 | Flags |= OverflowingBinaryOperator::NoUnsignedWrap; | ||||
2605 | } else if (Opc == Instruction::SDiv || | ||||
2606 | Opc == Instruction::UDiv || | ||||
2607 | Opc == Instruction::LShr || | ||||
2608 | Opc == Instruction::AShr) { | ||||
2609 | if (Record[3] & (1 << bitc::PEO_EXACT)) | ||||
2610 | Flags |= SDivOperator::IsExact; | ||||
2611 | } | ||||
2612 | } | ||||
2613 | V = ConstantExpr::get(Opc, LHS, RHS, Flags); | ||||
2614 | } | ||||
2615 | break; | ||||
2616 | } | ||||
2617 | case bitc::CST_CODE_CE_CAST: { // CE_CAST: [opcode, opty, opval] | ||||
2618 | if (Record.size() < 3) | ||||
2619 | return error("Invalid record"); | ||||
2620 | int Opc = getDecodedCastOpcode(Record[0]); | ||||
2621 | if (Opc < 0) { | ||||
2622 | V = UndefValue::get(CurTy); // Unknown cast. | ||||
2623 | } else { | ||||
2624 | Type *OpTy = getTypeByID(Record[1]); | ||||
2625 | if (!OpTy) | ||||
2626 | return error("Invalid record"); | ||||
2627 | Constant *Op = ValueList.getConstantFwdRef(Record[2], OpTy); | ||||
2628 | V = UpgradeBitCastExpr(Opc, Op, CurTy); | ||||
2629 | if (!V) V = ConstantExpr::getCast(Opc, Op, CurTy); | ||||
2630 | } | ||||
2631 | break; | ||||
2632 | } | ||||
2633 | case bitc::CST_CODE_CE_INBOUNDS_GEP: // [ty, n x operands] | ||||
2634 | case bitc::CST_CODE_CE_GEP: // [ty, n x operands] | ||||
2635 | case bitc::CST_CODE_CE_GEP_WITH_INRANGE_INDEX: { // [ty, flags, n x | ||||
2636 | // operands] | ||||
2637 | unsigned OpNum = 0; | ||||
2638 | Type *PointeeType = nullptr; | ||||
2639 | if (BitCode
| ||||
2640 | Record.size() % 2) | ||||
2641 | PointeeType = getTypeByID(Record[OpNum++]); | ||||
2642 | |||||
2643 | bool InBounds = false; | ||||
2644 | Optional<unsigned> InRangeIndex; | ||||
2645 | if (BitCode
| ||||
2646 | uint64_t Op = Record[OpNum++]; | ||||
2647 | InBounds = Op & 1; | ||||
2648 | InRangeIndex = Op >> 1; | ||||
2649 | } else if (BitCode
| ||||
2650 | InBounds = true; | ||||
2651 | |||||
2652 | SmallVector<Constant*, 16> Elts; | ||||
2653 | Type *Elt0FullTy = nullptr; | ||||
2654 | while (OpNum != Record.size()) { | ||||
2655 | if (!Elt0FullTy) | ||||
2656 | Elt0FullTy = getTypeByID(Record[OpNum]); | ||||
2657 | Type *ElTy = getTypeByID(Record[OpNum++]); | ||||
2658 | if (!ElTy) | ||||
2659 | return error("Invalid record"); | ||||
2660 | Elts.push_back(ValueList.getConstantFwdRef(Record[OpNum++], ElTy)); | ||||
2661 | } | ||||
2662 | |||||
2663 | if (Elts.size() < 1) | ||||
2664 | return error("Invalid gep with no operands"); | ||||
2665 | |||||
2666 | Type *ImplicitPointeeType = | ||||
2667 | cast<PointerType>(Elt0FullTy->getScalarType())->getElementType(); | ||||
| |||||
2668 | if (!PointeeType) | ||||
2669 | PointeeType = ImplicitPointeeType; | ||||
2670 | else if (PointeeType != ImplicitPointeeType) | ||||
2671 | return error("Explicit gep operator type does not match pointee type " | ||||
2672 | "of pointer operand"); | ||||
2673 | |||||
2674 | ArrayRef<Constant *> Indices(Elts.begin() + 1, Elts.end()); | ||||
2675 | V = ConstantExpr::getGetElementPtr(PointeeType, Elts[0], Indices, | ||||
2676 | InBounds, InRangeIndex); | ||||
2677 | break; | ||||
2678 | } | ||||
2679 | case bitc::CST_CODE_CE_SELECT: { // CE_SELECT: [opval#, opval#, opval#] | ||||
2680 | if (Record.size() < 3) | ||||
2681 | return error("Invalid record"); | ||||
2682 | |||||
2683 | Type *SelectorTy = Type::getInt1Ty(Context); | ||||
2684 | |||||
2685 | // The selector might be an i1, an <n x i1>, or a <vscale x n x i1> | ||||
2686 | // Get the type from the ValueList before getting a forward ref. | ||||
2687 | if (VectorType *VTy = dyn_cast<VectorType>(CurTy)) | ||||
2688 | if (Value *V = ValueList[Record[0]]) | ||||
2689 | if (SelectorTy != V->getType()) | ||||
2690 | SelectorTy = VectorType::get(SelectorTy, | ||||
2691 | VTy->getElementCount()); | ||||
2692 | |||||
2693 | V = ConstantExpr::getSelect(ValueList.getConstantFwdRef(Record[0], | ||||
2694 | SelectorTy), | ||||
2695 | ValueList.getConstantFwdRef(Record[1],CurTy), | ||||
2696 | ValueList.getConstantFwdRef(Record[2],CurTy)); | ||||
2697 | break; | ||||
2698 | } | ||||
2699 | case bitc::CST_CODE_CE_EXTRACTELT | ||||
2700 | : { // CE_EXTRACTELT: [opty, opval, opty, opval] | ||||
2701 | if (Record.size() < 3) | ||||
2702 | return error("Invalid record"); | ||||
2703 | VectorType *OpTy = | ||||
2704 | dyn_cast_or_null<VectorType>(getTypeByID(Record[0])); | ||||
2705 | if (!OpTy) | ||||
2706 | return error("Invalid record"); | ||||
2707 | Constant *Op0 = ValueList.getConstantFwdRef(Record[1], OpTy); | ||||
2708 | Constant *Op1 = nullptr; | ||||
2709 | if (Record.size() == 4) { | ||||
2710 | Type *IdxTy = getTypeByID(Record[2]); | ||||
2711 | if (!IdxTy) | ||||
2712 | return error("Invalid record"); | ||||
2713 | Op1 = ValueList.getConstantFwdRef(Record[3], IdxTy); | ||||
2714 | } else { | ||||
2715 | // Deprecated, but still needed to read old bitcode files. | ||||
2716 | Op1 = ValueList.getConstantFwdRef(Record[2], Type::getInt32Ty(Context)); | ||||
2717 | } | ||||
2718 | if (!Op1) | ||||
2719 | return error("Invalid record"); | ||||
2720 | V = ConstantExpr::getExtractElement(Op0, Op1); | ||||
2721 | break; | ||||
2722 | } | ||||
2723 | case bitc::CST_CODE_CE_INSERTELT | ||||
2724 | : { // CE_INSERTELT: [opval, opval, opty, opval] | ||||
2725 | VectorType *OpTy = dyn_cast<VectorType>(CurTy); | ||||
2726 | if (Record.size() < 3 || !OpTy) | ||||
2727 | return error("Invalid record"); | ||||
2728 | Constant *Op0 = ValueList.getConstantFwdRef(Record[0], OpTy); | ||||
2729 | Constant *Op1 = ValueList.getConstantFwdRef(Record[1], | ||||
2730 | OpTy->getElementType()); | ||||
2731 | Constant *Op2 = nullptr; | ||||
2732 | if (Record.size() == 4) { | ||||
2733 | Type *IdxTy = getTypeByID(Record[2]); | ||||
2734 | if (!IdxTy) | ||||
2735 | return error("Invalid record"); | ||||
2736 | Op2 = ValueList.getConstantFwdRef(Record[3], IdxTy); | ||||
2737 | } else { | ||||
2738 | // Deprecated, but still needed to read old bitcode files. | ||||
2739 | Op2 = ValueList.getConstantFwdRef(Record[2], Type::getInt32Ty(Context)); | ||||
2740 | } | ||||
2741 | if (!Op2) | ||||
2742 | return error("Invalid record"); | ||||
2743 | V = ConstantExpr::getInsertElement(Op0, Op1, Op2); | ||||
2744 | break; | ||||
2745 | } | ||||
2746 | case bitc::CST_CODE_CE_SHUFFLEVEC: { // CE_SHUFFLEVEC: [opval, opval, opval] | ||||
2747 | VectorType *OpTy = dyn_cast<VectorType>(CurTy); | ||||
2748 | if (Record.size() < 3 || !OpTy) | ||||
2749 | return error("Invalid record"); | ||||
2750 | DelayedShuffles.push_back( | ||||
2751 | {OpTy, OpTy, Record[0], Record[1], Record[2], NextCstNo}); | ||||
2752 | ++NextCstNo; | ||||
2753 | continue; | ||||
2754 | } | ||||
2755 | case bitc::CST_CODE_CE_SHUFVEC_EX: { // [opty, opval, opval, opval] | ||||
2756 | VectorType *RTy = dyn_cast<VectorType>(CurTy); | ||||
2757 | VectorType *OpTy = | ||||
2758 | dyn_cast_or_null<VectorType>(getTypeByID(Record[0])); | ||||
2759 | if (Record.size() < 4 || !RTy || !OpTy) | ||||
2760 | return error("Invalid record"); | ||||
2761 | DelayedShuffles.push_back( | ||||
2762 | {OpTy, RTy, Record[1], Record[2], Record[3], NextCstNo}); | ||||
2763 | ++NextCstNo; | ||||
2764 | continue; | ||||
2765 | } | ||||
2766 | case bitc::CST_CODE_CE_CMP: { // CE_CMP: [opty, opval, opval, pred] | ||||
2767 | if (Record.size() < 4) | ||||
2768 | return error("Invalid record"); | ||||
2769 | Type *OpTy = getTypeByID(Record[0]); | ||||
2770 | if (!OpTy) | ||||
2771 | return error("Invalid record"); | ||||
2772 | Constant *Op0 = ValueList.getConstantFwdRef(Record[1], OpTy); | ||||
2773 | Constant *Op1 = ValueList.getConstantFwdRef(Record[2], OpTy); | ||||
2774 | |||||
2775 | if (OpTy->isFPOrFPVectorTy()) | ||||
2776 | V = ConstantExpr::getFCmp(Record[3], Op0, Op1); | ||||
2777 | else | ||||
2778 | V = ConstantExpr::getICmp(Record[3], Op0, Op1); | ||||
2779 | break; | ||||
2780 | } | ||||
2781 | // This maintains backward compatibility, pre-asm dialect keywords. | ||||
2782 | // Deprecated, but still needed to read old bitcode files. | ||||
2783 | case bitc::CST_CODE_INLINEASM_OLD: { | ||||
2784 | if (Record.size() < 2) | ||||
2785 | return error("Invalid record"); | ||||
2786 | std::string AsmStr, ConstrStr; | ||||
2787 | bool HasSideEffects = Record[0] & 1; | ||||
2788 | bool IsAlignStack = Record[0] >> 1; | ||||
2789 | unsigned AsmStrSize = Record[1]; | ||||
2790 | if (2+AsmStrSize >= Record.size()) | ||||
2791 | return error("Invalid record"); | ||||
2792 | unsigned ConstStrSize = Record[2+AsmStrSize]; | ||||
2793 | if (3+AsmStrSize+ConstStrSize > Record.size()) | ||||
2794 | return error("Invalid record"); | ||||
2795 | |||||
2796 | for (unsigned i = 0; i != AsmStrSize; ++i) | ||||
2797 | AsmStr += (char)Record[2+i]; | ||||
2798 | for (unsigned i = 0; i != ConstStrSize; ++i) | ||||
2799 | ConstrStr += (char)Record[3+AsmStrSize+i]; | ||||
2800 | UpgradeInlineAsmString(&AsmStr); | ||||
2801 | V = InlineAsm::get( | ||||
2802 | cast<FunctionType>(cast<PointerType>(CurTy)->getElementType()), | ||||
2803 | AsmStr, ConstrStr, HasSideEffects, IsAlignStack); | ||||
2804 | break; | ||||
2805 | } | ||||
2806 | // This version adds support for the asm dialect keywords (e.g., | ||||
2807 | // inteldialect). | ||||
2808 | case bitc::CST_CODE_INLINEASM_OLD2: { | ||||
2809 | if (Record.size() < 2) | ||||
2810 | return error("Invalid record"); | ||||
2811 | std::string AsmStr, ConstrStr; | ||||
2812 | bool HasSideEffects = Record[0] & 1; | ||||
2813 | bool IsAlignStack = (Record[0] >> 1) & 1; | ||||
2814 | unsigned AsmDialect = Record[0] >> 2; | ||||
2815 | unsigned AsmStrSize = Record[1]; | ||||
2816 | if (2+AsmStrSize >= Record.size()) | ||||
2817 | return error("Invalid record"); | ||||
2818 | unsigned ConstStrSize = Record[2+AsmStrSize]; | ||||
2819 | if (3+AsmStrSize+ConstStrSize > Record.size()) | ||||
2820 | return error("Invalid record"); | ||||
2821 | |||||
2822 | for (unsigned i = 0; i != AsmStrSize; ++i) | ||||
2823 | AsmStr += (char)Record[2+i]; | ||||
2824 | for (unsigned i = 0; i != ConstStrSize; ++i) | ||||
2825 | ConstrStr += (char)Record[3+AsmStrSize+i]; | ||||
2826 | UpgradeInlineAsmString(&AsmStr); | ||||
2827 | V = InlineAsm::get( | ||||
2828 | cast<FunctionType>(cast<PointerType>(CurTy)->getElementType()), | ||||
2829 | AsmStr, ConstrStr, HasSideEffects, IsAlignStack, | ||||
2830 | InlineAsm::AsmDialect(AsmDialect)); | ||||
2831 | break; | ||||
2832 | } | ||||
2833 | // This version adds support for the unwind keyword. | ||||
2834 | case bitc::CST_CODE_INLINEASM: { | ||||
2835 | if (Record.size() < 2) | ||||
2836 | return error("Invalid record"); | ||||
2837 | std::string AsmStr, ConstrStr; | ||||
2838 | bool HasSideEffects = Record[0] & 1; | ||||
2839 | bool IsAlignStack = (Record[0] >> 1) & 1; | ||||
2840 | unsigned AsmDialect = (Record[0] >> 2) & 1; | ||||
2841 | bool CanThrow = (Record[0] >> 3) & 1; | ||||
2842 | unsigned AsmStrSize = Record[1]; | ||||
2843 | if (2 + AsmStrSize >= Record.size()) | ||||
2844 | return error("Invalid record"); | ||||
2845 | unsigned ConstStrSize = Record[2 + AsmStrSize]; | ||||
2846 | if (3 + AsmStrSize + ConstStrSize > Record.size()) | ||||
2847 | return error("Invalid record"); | ||||
2848 | |||||
2849 | for (unsigned i = 0; i != AsmStrSize; ++i) | ||||
2850 | AsmStr += (char)Record[2 + i]; | ||||
2851 | for (unsigned i = 0; i != ConstStrSize; ++i) | ||||
2852 | ConstrStr += (char)Record[3 + AsmStrSize + i]; | ||||
2853 | UpgradeInlineAsmString(&AsmStr); | ||||
2854 | V = InlineAsm::get( | ||||
2855 | cast<FunctionType>(cast<PointerType>(CurTy)->getElementType()), | ||||
2856 | AsmStr, ConstrStr, HasSideEffects, IsAlignStack, | ||||
2857 | InlineAsm::AsmDialect(AsmDialect), CanThrow); | ||||
2858 | break; | ||||
2859 | } | ||||
2860 | case bitc::CST_CODE_BLOCKADDRESS:{ | ||||
2861 | if (Record.size() < 3) | ||||
2862 | return error("Invalid record"); | ||||
2863 | Type *FnTy = getTypeByID(Record[0]); | ||||
2864 | if (!FnTy) | ||||
2865 | return error("Invalid record"); | ||||
2866 | Function *Fn = | ||||
2867 | dyn_cast_or_null<Function>(ValueList.getConstantFwdRef(Record[1],FnTy)); | ||||
2868 | if (!Fn) | ||||
2869 | return error("Invalid record"); | ||||
2870 | |||||
2871 | // If the function is already parsed we can insert the block address right | ||||
2872 | // away. | ||||
2873 | BasicBlock *BB; | ||||
2874 | unsigned BBID = Record[2]; | ||||
2875 | if (!BBID) | ||||
2876 | // Invalid reference to entry block. | ||||
2877 | return error("Invalid ID"); | ||||
2878 | if (!Fn->empty()) { | ||||
2879 | Function::iterator BBI = Fn->begin(), BBE = Fn->end(); | ||||
2880 | for (size_t I = 0, E = BBID; I != E; ++I) { | ||||
2881 | if (BBI == BBE) | ||||
2882 | return error("Invalid ID"); | ||||
2883 | ++BBI; | ||||
2884 | } | ||||
2885 | BB = &*BBI; | ||||
2886 | } else { | ||||
2887 | // Otherwise insert a placeholder and remember it so it can be inserted | ||||
2888 | // when the function is parsed. | ||||
2889 | auto &FwdBBs = BasicBlockFwdRefs[Fn]; | ||||
2890 | if (FwdBBs.empty()) | ||||
2891 | BasicBlockFwdRefQueue.push_back(Fn); | ||||
2892 | if (FwdBBs.size() < BBID + 1) | ||||
2893 | FwdBBs.resize(BBID + 1); | ||||
2894 | if (!FwdBBs[BBID]) | ||||
2895 | FwdBBs[BBID] = BasicBlock::Create(Context); | ||||
2896 | BB = FwdBBs[BBID]; | ||||
2897 | } | ||||
2898 | V = BlockAddress::get(Fn, BB); | ||||
2899 | break; | ||||
2900 | } | ||||
2901 | case bitc::CST_CODE_DSO_LOCAL_EQUIVALENT: { | ||||
2902 | if (Record.size() < 2) | ||||
2903 | return error("Invalid record"); | ||||
2904 | Type *GVTy = getTypeByID(Record[0]); | ||||
2905 | if (!GVTy) | ||||
2906 | return error("Invalid record"); | ||||
2907 | GlobalValue *GV = dyn_cast_or_null<GlobalValue>( | ||||
2908 | ValueList.getConstantFwdRef(Record[1], GVTy)); | ||||
2909 | if (!GV) | ||||
2910 | return error("Invalid record"); | ||||
2911 | |||||
2912 | V = DSOLocalEquivalent::get(GV); | ||||
2913 | break; | ||||
2914 | } | ||||
2915 | } | ||||
2916 | |||||
2917 | ValueList.assignValue(V, NextCstNo); | ||||
2918 | ++NextCstNo; | ||||
2919 | } | ||||
2920 | } | ||||
2921 | |||||
2922 | Error BitcodeReader::parseUseLists() { | ||||
2923 | if (Error Err = Stream.EnterSubBlock(bitc::USELIST_BLOCK_ID)) | ||||
2924 | return Err; | ||||
2925 | |||||
2926 | // Read all the records. | ||||
2927 | SmallVector<uint64_t, 64> Record; | ||||
2928 | |||||
2929 | while (true) { | ||||
2930 | Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks(); | ||||
2931 | if (!MaybeEntry) | ||||
2932 | return MaybeEntry.takeError(); | ||||
2933 | BitstreamEntry Entry = MaybeEntry.get(); | ||||
2934 | |||||
2935 | switch (Entry.Kind) { | ||||
2936 | case BitstreamEntry::SubBlock: // Handled for us already. | ||||
2937 | case BitstreamEntry::Error: | ||||
2938 | return error("Malformed block"); | ||||
2939 | case BitstreamEntry::EndBlock: | ||||
2940 | return Error::success(); | ||||
2941 | case BitstreamEntry::Record: | ||||
2942 | // The interesting case. | ||||
2943 | break; | ||||
2944 | } | ||||
2945 | |||||
2946 | // Read a use list record. | ||||
2947 | Record.clear(); | ||||
2948 | bool IsBB = false; | ||||
2949 | Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record); | ||||
2950 | if (!MaybeRecord) | ||||
2951 | return MaybeRecord.takeError(); | ||||
2952 | switch (MaybeRecord.get()) { | ||||
2953 | default: // Default behavior: unknown type. | ||||
2954 | break; | ||||
2955 | case bitc::USELIST_CODE_BB: | ||||
2956 | IsBB = true; | ||||
2957 | LLVM_FALLTHROUGH[[gnu::fallthrough]]; | ||||
2958 | case bitc::USELIST_CODE_DEFAULT: { | ||||
2959 | unsigned RecordLength = Record.size(); | ||||
2960 | if (RecordLength < 3) | ||||
2961 | // Records should have at least an ID and two indexes. | ||||
2962 | return error("Invalid record"); | ||||
2963 | unsigned ID = Record.pop_back_val(); | ||||
2964 | |||||
2965 | Value *V; | ||||
2966 | if (IsBB) { | ||||
2967 | assert(ID < FunctionBBs.size() && "Basic block not found")(static_cast <bool> (ID < FunctionBBs.size() && "Basic block not found") ? void (0) : __assert_fail ("ID < FunctionBBs.size() && \"Basic block not found\"" , "/build/llvm-toolchain-snapshot-13~++20210621111111+acefe0eaaf82/llvm/lib/Bitcode/Reader/BitcodeReader.cpp" , 2967, __extension__ __PRETTY_FUNCTION__)); | ||||
2968 | V = FunctionBBs[ID]; | ||||
2969 | } else | ||||
2970 | V = ValueList[ID]; | ||||
2971 | unsigned NumUses = 0; | ||||
2972 | SmallDenseMap<const Use *, unsigned, 16> Order; | ||||
2973 | for (const Use &U : V->materialized_uses()) { | ||||
2974 | if (++NumUses > Record.size()) | ||||
2975 | break; | ||||
2976 | Order[&U] = Record[NumUses - 1]; | ||||
2977 | } | ||||
2978 | if (Order.size() != Record.size() || NumUses > Record.size()) | ||||
2979 | // Mismatches can happen if the functions are being materialized lazily | ||||
2980 | // (out-of-order), or a value has been upgraded. | ||||
2981 | break; | ||||
2982 | |||||
2983 | V->sortUseList([&](const Use &L, const Use &R) { | ||||
2984 | return Order.lookup(&L) < Order.lookup(&R); | ||||
2985 | }); | ||||
2986 | break; | ||||
2987 | } | ||||
2988 | } | ||||
2989 | } | ||||
2990 | } | ||||
2991 | |||||
2992 | /// When we see the block for metadata, remember where it is and then skip it. | ||||
2993 | /// This lets us lazily deserialize the metadata. | ||||
2994 | Error BitcodeReader::rememberAndSkipMetadata() { | ||||
2995 | // Save the current stream state. | ||||
2996 | uint64_t CurBit = Stream.GetCurrentBitNo(); | ||||
2997 | DeferredMetadataInfo.push_back(CurBit); | ||||
2998 | |||||
2999 | // Skip over the block for now. | ||||
3000 | if (Error Err = Stream.SkipBlock()) | ||||
3001 | return Err; | ||||
3002 | return Error::success(); | ||||
3003 | } | ||||
3004 | |||||
3005 | Error BitcodeReader::materializeMetadata() { | ||||
3006 | for (uint64_t BitPos : DeferredMetadataInfo) { | ||||
3007 | // Move the bit stream to the saved position. | ||||
3008 | if (Error JumpFailed = Stream.JumpToBit(BitPos)) | ||||
3009 | return JumpFailed; | ||||
3010 | if (Error Err = MDLoader->parseModuleMetadata()) | ||||
3011 | return Err; | ||||
3012 | } | ||||
3013 | |||||
3014 | // Upgrade "Linker Options" module flag to "llvm.linker.options" module-level | ||||
3015 | // metadata. Only upgrade if the new option doesn't exist to avoid upgrade | ||||
3016 | // multiple times. | ||||
3017 | if (!TheModule->getNamedMetadata("llvm.linker.options")) { | ||||
3018 | if (Metadata *Val = TheModule->getModuleFlag("Linker Options")) { | ||||
3019 | NamedMDNode *LinkerOpts = | ||||
3020 | TheModule->getOrInsertNamedMetadata("llvm.linker.options"); | ||||
3021 | for (const MDOperand &MDOptions : cast<MDNode>(Val)->operands()) | ||||
3022 | LinkerOpts->addOperand(cast<MDNode>(MDOptions)); | ||||
3023 | } | ||||
3024 | } | ||||
3025 | |||||
3026 | DeferredMetadataInfo.clear(); | ||||
3027 | return Error::success(); | ||||
3028 | } | ||||
3029 | |||||
3030 | void BitcodeReader::setStripDebugInfo() { StripDebugInfo = true; } | ||||
3031 | |||||
3032 | /// When we see the block for a function body, remember where it is and then | ||||
3033 | /// skip it. This lets us lazily deserialize the functions. | ||||
3034 | Error BitcodeReader::rememberAndSkipFunctionBody() { | ||||
3035 | // Get the function we are talking about. | ||||
3036 | if (FunctionsWithBodies.empty()) | ||||
3037 | return error("Insufficient function protos"); | ||||
3038 | |||||
3039 | Function *Fn = FunctionsWithBodies.back(); | ||||
3040 | FunctionsWithBodies.pop_back(); | ||||
3041 | |||||
3042 | // Save the current stream state. | ||||
3043 | uint64_t CurBit = Stream.GetCurrentBitNo(); | ||||
3044 | assert((static_cast <bool> ((DeferredFunctionInfo[Fn] == 0 || DeferredFunctionInfo [Fn] == CurBit) && "Mismatch between VST and scanned function offsets" ) ? void (0) : __assert_fail ("(DeferredFunctionInfo[Fn] == 0 || DeferredFunctionInfo[Fn] == CurBit) && \"Mismatch between VST and scanned function offsets\"" , "/build/llvm-toolchain-snapshot-13~++20210621111111+acefe0eaaf82/llvm/lib/Bitcode/Reader/BitcodeReader.cpp" , 3046, __extension__ __PRETTY_FUNCTION__)) | ||||
3045 | (DeferredFunctionInfo[Fn] == 0 || DeferredFunctionInfo[Fn] == CurBit) &&(static_cast <bool> ((DeferredFunctionInfo[Fn] == 0 || DeferredFunctionInfo [Fn] == CurBit) && "Mismatch between VST and scanned function offsets" ) ? void (0) : __assert_fail ("(DeferredFunctionInfo[Fn] == 0 || DeferredFunctionInfo[Fn] == CurBit) && \"Mismatch between VST and scanned function offsets\"" , "/build/llvm-toolchain-snapshot-13~++20210621111111+acefe0eaaf82/llvm/lib/Bitcode/Reader/BitcodeReader.cpp" , 3046, __extension__ __PRETTY_FUNCTION__)) | ||||
3046 | "Mismatch between VST and scanned function offsets")(static_cast <bool> ((DeferredFunctionInfo[Fn] == 0 || DeferredFunctionInfo [Fn] == CurBit) && "Mismatch between VST and scanned function offsets" ) ? void (0) : __assert_fail ("(DeferredFunctionInfo[Fn] == 0 || DeferredFunctionInfo[Fn] == CurBit) && \"Mismatch between VST and scanned function offsets\"" , "/build/llvm-toolchain-snapshot-13~++20210621111111+acefe0eaaf82/llvm/lib/Bitcode/Reader/BitcodeReader.cpp" , 3046, __extension__ __PRETTY_FUNCTION__)); | ||||
3047 | DeferredFunctionInfo[Fn] = CurBit; | ||||
3048 | |||||
3049 | // Skip over the function block for now. | ||||
3050 | if (Error Err = Stream.SkipBlock()) | ||||
3051 | return Err; | ||||
3052 | return Error::success(); | ||||
3053 | } | ||||
3054 | |||||
3055 | Error BitcodeReader::globalCleanup() { | ||||
3056 | // Patch the initializers for globals and aliases up. | ||||
3057 | if (Error Err = resolveGlobalAndIndirectSymbolInits()) | ||||
3058 | return Err; | ||||
3059 | if (!GlobalInits.empty() || !IndirectSymbolInits.empty()) | ||||
3060 | return error("Malformed global initializer set"); | ||||
3061 | |||||
3062 | // Look for intrinsic functions which need to be upgraded at some point | ||||
3063 | // and functions that need to have their function attributes upgraded. | ||||
3064 | for (Function &F : *TheModule) { | ||||
3065 | MDLoader->upgradeDebugIntrinsics(F); | ||||
3066 | Function *NewFn; | ||||
3067 | if (UpgradeIntrinsicFunction(&F, NewFn)) | ||||
3068 | UpgradedIntrinsics[&F] = NewFn; | ||||
3069 | else if (auto Remangled = Intrinsic::remangleIntrinsicFunction(&F)) | ||||
3070 | // Some types could be renamed during loading if several modules are | ||||
3071 | // loaded in the same LLVMContext (LTO scenario). In this case we should | ||||
3072 | // remangle intrinsics names as well. | ||||
3073 | RemangledIntrinsics[&F] = Remangled.getValue(); | ||||
3074 | // Look for functions that rely on old function attribute behavior. | ||||
3075 | UpgradeFunctionAttributes(F); | ||||
3076 | } | ||||
3077 | |||||
3078 | // Look for global variables which need to be renamed. | ||||
3079 | std::vector<std::pair<GlobalVariable *, GlobalVariable *>> UpgradedVariables; | ||||
3080 | for (GlobalVariable &GV : TheModule->globals()) | ||||
3081 | if (GlobalVariable *Upgraded = UpgradeGlobalVariable(&GV)) | ||||
3082 | UpgradedVariables.emplace_back(&GV, Upgraded); | ||||
3083 | for (auto &Pair : UpgradedVariables) { | ||||
3084 | Pair.first->eraseFromParent(); | ||||
3085 | TheModule->getGlobalList().push_back(Pair.second); | ||||
3086 | } | ||||
3087 | |||||
3088 | // Force deallocation of memory for these vectors to favor the client that | ||||
3089 | // want lazy deserialization. | ||||
3090 | std::vector<std::pair<GlobalVariable *, unsigned>>().swap(GlobalInits); | ||||
3091 | std::vector<std::pair<GlobalIndirectSymbol *, unsigned>>().swap( | ||||
3092 | IndirectSymbolInits); | ||||
3093 | return Error::success(); | ||||
3094 | } | ||||
3095 | |||||
3096 | /// Support for lazy parsing of function bodies. This is required if we | ||||
3097 | /// either have an old bitcode file without a VST forward declaration record, | ||||
3098 | /// or if we have an anonymous function being materialized, since anonymous | ||||
3099 | /// functions do not have a name and are therefore not in the VST. | ||||
3100 | Error BitcodeReader::rememberAndSkipFunctionBodies() { | ||||
3101 | if (Error JumpFailed = Stream.JumpToBit(NextUnreadBit)) | ||||
3102 | return JumpFailed; | ||||
3103 | |||||
3104 | if (Stream.AtEndOfStream()) | ||||
3105 | return error("Could not find function in stream"); | ||||
3106 | |||||
3107 | if (!SeenFirstFunctionBody) | ||||
3108 | return error("Trying to materialize functions before seeing function blocks"); | ||||
3109 | |||||
3110 | // An old bitcode file with the symbol table at the end would have | ||||
3111 | // finished the parse greedily. | ||||
3112 | assert(SeenValueSymbolTable)(static_cast <bool> (SeenValueSymbolTable) ? void (0) : __assert_fail ("SeenValueSymbolTable", "/build/llvm-toolchain-snapshot-13~++20210621111111+acefe0eaaf82/llvm/lib/Bitcode/Reader/BitcodeReader.cpp" , 3112, __extension__ __PRETTY_FUNCTION__)); | ||||
3113 | |||||
3114 | SmallVector<uint64_t, 64> Record; | ||||
3115 | |||||
3116 | while (true) { | ||||
3117 | Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); | ||||
3118 | if (!MaybeEntry) | ||||
3119 | return MaybeEntry.takeError(); | ||||
3120 | llvm::BitstreamEntry Entry = MaybeEntry.get(); | ||||
3121 | |||||
3122 | switch (Entry.Kind) { | ||||
3123 | default: | ||||
3124 | return error("Expect SubBlock"); | ||||
3125 | case BitstreamEntry::SubBlock: | ||||
3126 | switch (Entry.ID) { | ||||
3127 | default: | ||||
3128 | return error("Expect function block"); | ||||
3129 | case bitc::FUNCTION_BLOCK_ID: | ||||
3130 | if (Error Err = rememberAndSkipFunctionBody()) | ||||
3131 | return Err; | ||||
3132 | NextUnreadBit = Stream.GetCurrentBitNo(); | ||||
3133 | return Error::success(); | ||||
3134 | } | ||||
3135 | } | ||||
3136 | } | ||||
3137 | } | ||||
3138 | |||||
3139 | bool BitcodeReaderBase::readBlockInfo() { | ||||
3140 | Expected<Optional<BitstreamBlockInfo>> MaybeNewBlockInfo = | ||||
3141 | Stream.ReadBlockInfoBlock(); | ||||
3142 | if (!MaybeNewBlockInfo) | ||||
3143 | return true; // FIXME Handle the error. | ||||
3144 | Optional<BitstreamBlockInfo> NewBlockInfo = | ||||
3145 | std::move(MaybeNewBlockInfo.get()); | ||||
3146 | if (!NewBlockInfo) | ||||
3147 | return true; | ||||
3148 | BlockInfo = std::move(*NewBlockInfo); | ||||
3149 | return false; | ||||
3150 | } | ||||
3151 | |||||
3152 | Error BitcodeReader::parseComdatRecord(ArrayRef<uint64_t> Record) { | ||||
3153 | // v1: [selection_kind, name] | ||||
3154 | // v2: [strtab_offset, strtab_size, selection_kind] | ||||
3155 | StringRef Name; | ||||
3156 | std::tie(Name, Record) = readNameFromStrtab(Record); | ||||
3157 | |||||
3158 | if (Record.empty()) | ||||
3159 | return error("Invalid record"); | ||||
3160 | Comdat::SelectionKind SK = getDecodedComdatSelectionKind(Record[0]); | ||||
3161 | std::string OldFormatName; | ||||
3162 | if (!UseStrtab) { | ||||
3163 | if (Record.size() < 2) | ||||
3164 | return error("Invalid record"); | ||||
3165 | unsigned ComdatNameSize = Record[1]; | ||||
3166 | OldFormatName.reserve(ComdatNameSize); | ||||
3167 | for (unsigned i = 0; i != ComdatNameSize; ++i) | ||||
3168 | OldFormatName += (char)Record[2 + i]; | ||||
3169 | Name = OldFormatName; | ||||
3170 | } | ||||
3171 | Comdat *C = TheModule->getOrInsertComdat(Name); | ||||
3172 | C->setSelectionKind(SK); | ||||
3173 | ComdatList.push_back(C); | ||||
3174 | return Error::success(); | ||||
3175 | } | ||||
3176 | |||||
3177 | static void inferDSOLocal(GlobalValue *GV) { | ||||
3178 | // infer dso_local from linkage and visibility if it is not encoded. | ||||
3179 | if (GV->hasLocalLinkage() || | ||||
3180 | (!GV->hasDefaultVisibility() && !GV->hasExternalWeakLinkage())) | ||||
3181 | GV->setDSOLocal(true); | ||||
3182 | } | ||||
3183 | |||||
3184 | Error BitcodeReader::parseGlobalVarRecord(ArrayRef<uint64_t> Record) { | ||||
3185 | // v1: [pointer type, isconst, initid, linkage, alignment, section, | ||||
3186 | // visibility, threadlocal, unnamed_addr, externally_initialized, | ||||
3187 | // dllstorageclass, comdat, attributes, preemption specifier, | ||||
3188 | // partition strtab offset, partition strtab size] (name in VST) | ||||
3189 | // v2: [strtab_offset, strtab_size, v1] | ||||
3190 | StringRef Name; | ||||
3191 | std::tie(Name, Record) = readNameFromStrtab(Record); | ||||
3192 | |||||
3193 | if (Record.size() < 6) | ||||
3194 | return error("Invalid record"); | ||||
3195 | Type *Ty = getTypeByID(Record[0]); | ||||
3196 | if (!Ty) | ||||
3197 | return error("Invalid record"); | ||||
3198 | bool isConstant = Record[1] & 1; | ||||
3199 | bool explicitType = Record[1] & 2; | ||||
3200 | unsigned AddressSpace; | ||||
3201 | if (explicitType) { | ||||
3202 | AddressSpace = Record[1] >> 2; | ||||
3203 | } else { | ||||
3204 | if (!Ty->isPointerTy()) | ||||
3205 | return error("Invalid type for value"); | ||||
3206 | AddressSpace = cast<PointerType>(Ty)->getAddressSpace(); | ||||
3207 | Ty = cast<PointerType>(Ty)->getElementType(); | ||||
3208 | } | ||||
3209 | |||||
3210 | uint64_t RawLinkage = Record[3]; | ||||
3211 | GlobalValue::LinkageTypes Linkage = getDecodedLinkage(RawLinkage); | ||||
3212 | MaybeAlign Alignment; | ||||
3213 | if (Error Err = parseAlignmentValue(Record[4], Alignment)) | ||||
3214 | return Err; | ||||
3215 | std::string Section; | ||||
3216 | if (Record[5]) { | ||||
3217 | if (Record[5] - 1 >= SectionTable.size()) | ||||
3218 | return error("Invalid ID"); | ||||
3219 | Section = SectionTable[Record[5] - 1]; | ||||
3220 | } | ||||
3221 | GlobalValue::VisibilityTypes Visibility = GlobalValue::DefaultVisibility; | ||||
3222 | // Local linkage must have default visibility. | ||||
3223 | // auto-upgrade `hidden` and `protected` for old bitcode. | ||||
3224 | if (Record.size() > 6 && !GlobalValue::isLocalLinkage(Linkage)) | ||||
3225 | Visibility = getDecodedVisibility(Record[6]); | ||||
3226 | |||||
3227 | GlobalVariable::ThreadLocalMode TLM = GlobalVariable::NotThreadLocal; | ||||
3228 | if (Record.size() > 7) | ||||
3229 | TLM = getDecodedThreadLocalMode(Record[7]); | ||||
3230 | |||||
3231 | GlobalValue::UnnamedAddr UnnamedAddr = GlobalValue::UnnamedAddr::None; | ||||
3232 | if (Record.size() > 8) | ||||
3233 | UnnamedAddr = getDecodedUnnamedAddrType(Record[8]); | ||||
3234 | |||||
3235 | bool ExternallyInitialized = false; | ||||
3236 | if (Record.size() > 9) | ||||
3237 | ExternallyInitialized = Record[9]; | ||||
3238 | |||||
3239 | GlobalVariable *NewGV = | ||||
3240 | new GlobalVariable(*TheModule, Ty, isConstant, Linkage, nullptr, Name, | ||||
3241 | nullptr, TLM, AddressSpace, ExternallyInitialized); | ||||
3242 | NewGV->setAlignment(Alignment); | ||||
3243 | if (!Section.empty()) | ||||
3244 | NewGV->setSection(Section); | ||||
3245 | NewGV->setVisibility(Visibility); | ||||
3246 | NewGV->setUnnamedAddr(UnnamedAddr); | ||||
3247 | |||||
3248 | if (Record.size() > 10) | ||||
3249 | NewGV->setDLLStorageClass(getDecodedDLLStorageClass(Record[10])); | ||||
3250 | else | ||||
3251 | upgradeDLLImportExportLinkage(NewGV, RawLinkage); | ||||
3252 | |||||
3253 | ValueList.push_back(NewGV); | ||||
3254 | |||||
3255 | // Remember which value to use for the global initializer. | ||||
3256 | if (unsigned InitID = Record[2]) | ||||
3257 | GlobalInits.push_back(std::make_pair(NewGV, InitID - 1)); | ||||
3258 | |||||
3259 | if (Record.size() > 11) { | ||||
3260 | if (unsigned ComdatID = Record[11]) { | ||||
3261 | if (ComdatID > ComdatList.size()) | ||||
3262 | return error("Invalid global variable comdat ID"); | ||||
3263 | NewGV->setComdat(ComdatList[ComdatID - 1]); | ||||
3264 | } | ||||
3265 | } else if (hasImplicitComdat(RawLinkage)) { | ||||
3266 | NewGV->setComdat(reinterpret_cast<Comdat *>(1)); | ||||
3267 | } | ||||
3268 | |||||
3269 | if (Record.size() > 12) { | ||||
3270 | auto AS = getAttributes(Record[12]).getFnAttributes(); | ||||
3271 | NewGV->setAttributes(AS); | ||||
3272 | } | ||||
3273 | |||||
3274 | if (Record.size() > 13) { | ||||
3275 | NewGV->setDSOLocal(getDecodedDSOLocal(Record[13])); | ||||
3276 | } | ||||
3277 | inferDSOLocal(NewGV); | ||||
3278 | |||||
3279 | // Check whether we have enough values to read a partition name. | ||||
3280 | if (Record.size() > 15) | ||||
3281 | NewGV->setPartition(StringRef(Strtab.data() + Record[14], Record[15])); | ||||
3282 | |||||
3283 | return Error::success(); | ||||
3284 | } | ||||
3285 | |||||
3286 | Error BitcodeReader::parseFunctionRecord(ArrayRef<uint64_t> Record) { | ||||
3287 | // v1: [type, callingconv, isproto, linkage, paramattr, alignment, section, | ||||
3288 | // visibility, gc, unnamed_addr, prologuedata, dllstorageclass, comdat, | ||||
3289 | // prefixdata, personalityfn, preemption specifier, addrspace] (name in VST) | ||||
3290 | // v2: [strtab_offset, strtab_size, v1] | ||||
3291 | StringRef Name; | ||||
3292 | std::tie(Name, Record) = readNameFromStrtab(Record); | ||||
3293 | |||||
3294 | if (Record.size() < 8) | ||||
3295 | return error("Invalid record"); | ||||
3296 | Type *FTy = getTypeByID(Record[0]); | ||||
3297 | if (!FTy) | ||||
3298 | return error("Invalid record"); | ||||
3299 | if (auto *PTy = dyn_cast<PointerType>(FTy)) | ||||
3300 | FTy = PTy->getElementType(); | ||||
3301 | |||||
3302 | if (!isa<FunctionType>(FTy)) | ||||
3303 | return error("Invalid type for value"); | ||||
3304 | auto CC = static_cast<CallingConv::ID>(Record[1]); | ||||
3305 | if (CC & ~CallingConv::MaxID) | ||||
3306 | return error("Invalid calling convention ID"); | ||||
3307 | |||||
3308 | unsigned AddrSpace = TheModule->getDataLayout().getProgramAddressSpace(); | ||||
3309 | if (Record.size() > 16) | ||||
3310 | AddrSpace = Record[16]; | ||||
3311 | |||||
3312 | Function *Func = | ||||
3313 | Function::Create(cast<FunctionType>(FTy), GlobalValue::ExternalLinkage, | ||||
3314 | AddrSpace, Name, TheModule); | ||||
3315 | |||||
3316 | assert(Func->getFunctionType() == FTy &&(static_cast <bool> (Func->getFunctionType() == FTy && "Incorrect fully specified type provided for function") ? void (0) : __assert_fail ("Func->getFunctionType() == FTy && \"Incorrect fully specified type provided for function\"" , "/build/llvm-toolchain-snapshot-13~++20210621111111+acefe0eaaf82/llvm/lib/Bitcode/Reader/BitcodeReader.cpp" , 3317, __extension__ __PRETTY_FUNCTION__)) | ||||
3317 | "Incorrect fully specified type provided for function")(static_cast <bool> (Func->getFunctionType() == FTy && "Incorrect fully specified type provided for function") ? void (0) : __assert_fail ("Func->getFunctionType() == FTy && \"Incorrect fully specified type provided for function\"" , "/build/llvm-toolchain-snapshot-13~++20210621111111+acefe0eaaf82/llvm/lib/Bitcode/Reader/BitcodeReader.cpp" , 3317, __extension__ __PRETTY_FUNCTION__)); | ||||
3318 | FunctionTypes[Func] = cast<FunctionType>(FTy); | ||||
3319 | |||||
3320 | Func->setCallingConv(CC); | ||||
3321 | bool isProto = Record[2]; | ||||
3322 | uint64_t RawLinkage = Record[3]; | ||||
3323 | Func->setLinkage(getDecodedLinkage(RawLinkage)); | ||||
3324 | Func->setAttributes(getAttributes(Record[4])); | ||||
3325 | |||||
3326 | // Upgrade any old-style byval or sret without a type by propagating the | ||||
3327 | // argument's pointee type. There should be no opaque pointers where the byval | ||||
3328 | // type is implicit. | ||||
3329 | for (unsigned i = 0; i != Func->arg_size(); ++i) { | ||||
3330 | for (Attribute::AttrKind Kind : {Attribute::ByVal, Attribute::StructRet, | ||||
3331 | Attribute::InAlloca}) { | ||||
3332 | if (!Func->hasParamAttribute(i, Kind)) | ||||
3333 | continue; | ||||
3334 | |||||
3335 | Func->removeParamAttr(i, Kind); | ||||
3336 | |||||
3337 | Type *PTy = cast<FunctionType>(FTy)->getParamType(i); | ||||
3338 | Type *PtrEltTy = cast<PointerType>(PTy)->getElementType(); | ||||
3339 | Attribute NewAttr; | ||||
3340 | switch (Kind) { | ||||
3341 | case Attribute::ByVal: | ||||
3342 | NewAttr = Attribute::getWithByValType(Context, PtrEltTy); | ||||
3343 | break; | ||||
3344 | case Attribute::StructRet: | ||||
3345 | NewAttr = Attribute::getWithStructRetType(Context, PtrEltTy); | ||||
3346 | break; | ||||
3347 | case Attribute::InAlloca: | ||||
3348 | NewAttr = Attribute::getWithInAllocaType(Context, PtrEltTy); | ||||
3349 | break; | ||||
3350 | default: | ||||
3351 | llvm_unreachable("not an upgraded type attribute")::llvm::llvm_unreachable_internal("not an upgraded type attribute" , "/build/llvm-toolchain-snapshot-13~++20210621111111+acefe0eaaf82/llvm/lib/Bitcode/Reader/BitcodeReader.cpp" , 3351); | ||||
3352 | } | ||||
3353 | |||||
3354 | Func->addParamAttr(i, NewAttr); | ||||
3355 | } | ||||
3356 | } | ||||
3357 | |||||
3358 | MaybeAlign Alignment; | ||||
3359 | if (Error Err = parseAlignmentValue(Record[5], Alignment)) | ||||
3360 | return Err; | ||||
3361 | Func->setAlignment(Alignment); | ||||
3362 | if (Record[6]) { | ||||
3363 | if (Record[6] - 1 >= SectionTable.size()) | ||||
3364 | return error("Invalid ID"); | ||||
3365 | Func->setSection(SectionTable[Record[6] - 1]); | ||||
3366 | } | ||||
3367 | // Local linkage must have default visibility. | ||||
3368 | // auto-upgrade `hidden` and `protected` for old bitcode. | ||||
3369 | if (!Func->hasLocalLinkage()) | ||||
3370 | Func->setVisibility(getDecodedVisibility(Record[7])); | ||||
3371 | if (Record.size() > 8 && Record[8]) { | ||||
3372 | if (Record[8] - 1 >= GCTable.size()) | ||||
3373 | return error("Invalid ID"); | ||||
3374 | Func->setGC(GCTable[Record[8] - 1]); | ||||
3375 | } | ||||
3376 | GlobalValue::UnnamedAddr UnnamedAddr = GlobalValue::UnnamedAddr::None; | ||||
3377 | if (Record.size() > 9) | ||||
3378 | UnnamedAddr = getDecodedUnnamedAddrType(Record[9]); | ||||
3379 | Func->setUnnamedAddr(UnnamedAddr); | ||||
3380 | if (Record.size() > 10 && Record[10] != 0) | ||||
3381 | FunctionPrologues.push_back(std::make_pair(Func, Record[10] - 1)); | ||||
3382 | |||||
3383 | if (Record.size() > 11) | ||||
3384 | Func->setDLLStorageClass(getDecodedDLLStorageClass(Record[11])); | ||||
3385 | else | ||||
3386 | upgradeDLLImportExportLinkage(Func, RawLinkage); | ||||
3387 | |||||
3388 | if (Record.size() > 12) { | ||||
3389 | if (unsigned ComdatID = Record[12]) { | ||||
3390 | if (ComdatID > ComdatList.size()) | ||||
3391 | return error("Invalid function comdat ID"); | ||||
3392 | Func->setComdat(ComdatList[ComdatID - 1]); | ||||
3393 | } | ||||
3394 | } else if (hasImplicitComdat(RawLinkage)) { | ||||
3395 | Func->setComdat(reinterpret_cast<Comdat *>(1)); | ||||
3396 | } | ||||
3397 | |||||
3398 | if (Record.size() > 13 && Record[13] != 0) | ||||
3399 | FunctionPrefixes.push_back(std::make_pair(Func, Record[13] - 1)); | ||||
3400 | |||||
3401 | if (Record.size() > 14 && Record[14] != 0) | ||||
3402 | FunctionPersonalityFns.push_back(std::make_pair(Func, Record[14] - 1)); | ||||
3403 | |||||
3404 | if (Record.size() > 15) { | ||||
3405 | Func->setDSOLocal(getDecodedDSOLocal(Record[15])); | ||||
3406 | } | ||||
3407 | inferDSOLocal(Func); | ||||
3408 | |||||
3409 | // Record[16] is the address space number. | ||||
3410 | |||||
3411 | // Check whether we have enough values to read a partition name. | ||||
3412 | if (Record.size() > 18) | ||||
3413 | Func->setPartition(StringRef(Strtab.data() + Record[17], Record[18])); | ||||
3414 | |||||
3415 | ValueList.push_back(Func); | ||||
3416 | |||||
3417 | // If this is a function with a body, remember the prototype we are | ||||
3418 | // creating now, so that we can match up the body with them later. | ||||
3419 | if (!isProto) { | ||||
3420 | Func->setIsMaterializable(true); | ||||
3421 | FunctionsWithBodies.push_back(Func); | ||||
3422 | DeferredFunctionInfo[Func] = 0; | ||||
3423 | } | ||||
3424 | return Error::success(); | ||||
3425 | } | ||||
3426 | |||||
3427 | Error BitcodeReader::parseGlobalIndirectSymbolRecord( | ||||
3428 | unsigned BitCode, ArrayRef<uint64_t> Record) { | ||||
3429 | // v1 ALIAS_OLD: [alias type, aliasee val#, linkage] (name in VST) | ||||
3430 | // v1 ALIAS: [alias type, addrspace, aliasee val#, linkage, visibility, | ||||
3431 | // dllstorageclass, threadlocal, unnamed_addr, | ||||
3432 | // preemption specifier] (name in VST) | ||||
3433 | // v1 IFUNC: [alias type, addrspace, aliasee val#, linkage, | ||||
3434 | // visibility, dllstorageclass, threadlocal, unnamed_addr, | ||||
3435 | // preemption specifier] (name in VST) | ||||
3436 | // v2: [strtab_offset, strtab_size, v1] | ||||
3437 | StringRef Name; | ||||
3438 | std::tie(Name, Record) = readNameFromStrtab(Record); | ||||
3439 | |||||
3440 | bool NewRecord = BitCode != bitc::MODULE_CODE_ALIAS_OLD; | ||||
3441 | if (Record.size() < (3 + (unsigned)NewRecord)) | ||||
3442 | return error("Invalid record"); | ||||
3443 | unsigned OpNum = 0; | ||||
3444 | Type *Ty = getTypeByID(Record[OpNum++]); | ||||
3445 | if (!Ty) | ||||
3446 | return error("Invalid record"); | ||||
3447 | |||||
3448 | unsigned AddrSpace; | ||||
3449 | if (!NewRecord) { | ||||
3450 | auto *PTy = dyn_cast<PointerType>(Ty); | ||||
3451 | if (!PTy) | ||||
3452 | return error("Invalid type for value"); | ||||
3453 | Ty = PTy->getElementType(); | ||||
3454 | AddrSpace = PTy->getAddressSpace(); | ||||
3455 | } else { | ||||
3456 | AddrSpace = Record[OpNum++]; | ||||
3457 | } | ||||
3458 | |||||
3459 | auto Val = Record[OpNum++]; | ||||
3460 | auto Linkage = Record[OpNum++]; | ||||
3461 | GlobalIndirectSymbol *NewGA; | ||||
3462 | if (BitCode == bitc::MODULE_CODE_ALIAS || | ||||
3463 | BitCode == bitc::MODULE_CODE_ALIAS_OLD) | ||||
3464 | NewGA = GlobalAlias::create(Ty, AddrSpace, getDecodedLinkage(Linkage), Name, | ||||
3465 | TheModule); | ||||
3466 | else | ||||
3467 | NewGA = GlobalIFunc::create(Ty, AddrSpace, getDecodedLinkage(Linkage), Name, | ||||
3468 | nullptr, TheModule); | ||||
3469 | |||||
3470 | // Local linkage must have default visibility. | ||||
3471 | // auto-upgrade `hidden` and `protected` for old bitcode. | ||||
3472 | if (OpNum != Record.size()) { | ||||
3473 | auto VisInd = OpNum++; | ||||
3474 | if (!NewGA->hasLocalLinkage()) | ||||
3475 | NewGA->setVisibility(getDecodedVisibility(Record[VisInd])); | ||||
3476 | } | ||||
3477 | if (BitCode == bitc::MODULE_CODE_ALIAS || | ||||
3478 | BitCode == bitc::MODULE_CODE_ALIAS_OLD) { | ||||
3479 | if (OpNum != Record.size()) | ||||
3480 | NewGA->setDLLStorageClass(getDecodedDLLStorageClass(Record[OpNum++])); | ||||
3481 | else | ||||
3482 | upgradeDLLImportExportLinkage(NewGA, Linkage); | ||||
3483 | if (OpNum != Record.size()) | ||||
3484 | NewGA->setThreadLocalMode(getDecodedThreadLocalMode(Record[OpNum++])); | ||||
3485 | if (OpNum != Record.size()) | ||||
3486 | NewGA->setUnnamedAddr(getDecodedUnnamedAddrType(Record[OpNum++])); | ||||
3487 | } | ||||
3488 | if (OpNum != Record.size()) | ||||
3489 | NewGA->setDSOLocal(getDecodedDSOLocal(Record[OpNum++])); | ||||
3490 | inferDSOLocal(NewGA); | ||||
3491 | |||||
3492 | // Check whether we have enough values to read a partition name. | ||||
3493 | if (OpNum + 1 < Record.size()) { | ||||
3494 | NewGA->setPartition( | ||||
3495 | StringRef(Strtab.data() + Record[OpNum], Record[OpNum + 1])); | ||||
3496 | OpNum += 2; | ||||
3497 | } | ||||
3498 | |||||
3499 | ValueList.push_back(NewGA); | ||||
3500 | IndirectSymbolInits.push_back(std::make_pair(NewGA, Val)); | ||||
3501 | return Error::success(); | ||||
3502 | } | ||||
3503 | |||||
3504 | Error BitcodeReader::parseModule(uint64_t ResumeBit, | ||||
3505 | bool ShouldLazyLoadMetadata, | ||||
3506 | DataLayoutCallbackTy DataLayoutCallback) { | ||||
3507 | if (ResumeBit) { | ||||
3508 | if (Error JumpFailed = Stream.JumpToBit(ResumeBit)) | ||||
3509 | return JumpFailed; | ||||
3510 | } else if (Error Err = Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID)) | ||||
3511 | return Err; | ||||
3512 | |||||
3513 | SmallVector<uint64_t, 64> Record; | ||||
3514 | |||||
3515 | // Parts of bitcode parsing depend on the datalayout. Make sure we | ||||
3516 | // finalize the datalayout before we run any of that code. | ||||
3517 | bool ResolvedDataLayout = false; | ||||
3518 | auto ResolveDataLayout = [&] { | ||||
3519 | if (ResolvedDataLayout) | ||||
3520 | return; | ||||
3521 | |||||
3522 | // datalayout and triple can't be parsed after this point. | ||||
3523 | ResolvedDataLayout = true; | ||||
3524 | |||||
3525 | // Upgrade data layout string. | ||||
3526 | std::string DL = llvm::UpgradeDataLayoutString( | ||||
3527 | TheModule->getDataLayoutStr(), TheModule->getTargetTriple()); | ||||
3528 | TheModule->setDataLayout(DL); | ||||
3529 | |||||
3530 | if (auto LayoutOverride = | ||||
3531 | DataLayoutCallback(TheModule->getTargetTriple())) | ||||
3532 | TheModule->setDataLayout(*LayoutOverride); | ||||
3533 | }; | ||||
3534 | |||||
3535 | // Read all the records for this module. | ||||
3536 | while (true) { | ||||
3537 | Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); | ||||
3538 | if (!MaybeEntry) | ||||
3539 | return MaybeEntry.takeError(); | ||||
3540 | llvm::BitstreamEntry Entry = MaybeEntry.get(); | ||||
3541 | |||||
3542 | switch (Entry.Kind) { | ||||
3543 | case BitstreamEntry::Error: | ||||
3544 | return error("Malformed block"); | ||||
3545 | case BitstreamEntry::EndBlock: | ||||
3546 | ResolveDataLayout(); | ||||
3547 | return globalCleanup(); | ||||
3548 | |||||
3549 | case BitstreamEntry::SubBlock: | ||||
3550 | switch (Entry.ID) { | ||||
3551 | default: // Skip unknown content. | ||||
3552 | if (Error Err = Stream.SkipBlock()) | ||||
3553 | return Err; | ||||
3554 | break; | ||||
3555 | case bitc::BLOCKINFO_BLOCK_ID: | ||||
3556 | if (readBlockInfo()) | ||||
3557 | return error("Malformed block"); | ||||
3558 | break; | ||||
3559 | case bitc::PARAMATTR_BLOCK_ID: | ||||
3560 | if (Error Err = parseAttributeBlock()) | ||||
3561 | return Err; | ||||
3562 | break; | ||||
3563 | case bitc::PARAMATTR_GROUP_BLOCK_ID: | ||||
3564 | if (Error Err = parseAttributeGroupBlock()) | ||||
3565 | return Err; | ||||
3566 | break; | ||||
3567 | case bitc::TYPE_BLOCK_ID_NEW: | ||||
3568 | if (Error Err = parseTypeTable()) | ||||
3569 | return Err; | ||||
3570 | break; | ||||
3571 | case bitc::VALUE_SYMTAB_BLOCK_ID: | ||||
3572 | if (!SeenValueSymbolTable) { | ||||
3573 | // Either this is an old form VST without function index and an | ||||
3574 | // associated VST forward declaration record (which would have caused | ||||
3575 | // the VST to be jumped to and parsed before it was encountered | ||||
3576 | // normally in the stream), or there were no function blocks to | ||||
3577 | // trigger an earlier parsing of the VST. | ||||
3578 | assert(VSTOffset == 0 || FunctionsWithBodies.empty())(static_cast <bool> (VSTOffset == 0 || FunctionsWithBodies .empty()) ? void (0) : __assert_fail ("VSTOffset == 0 || FunctionsWithBodies.empty()" , "/build/llvm-toolchain-snapshot-13~++20210621111111+acefe0eaaf82/llvm/lib/Bitcode/Reader/BitcodeReader.cpp" , 3578, __extension__ __PRETTY_FUNCTION__)); | ||||
3579 | if (Error Err = parseValueSymbolTable()) | ||||
3580 | return Err; | ||||
3581 | SeenValueSymbolTable = true; | ||||
3582 | } else { | ||||
3583 | // We must have had a VST forward declaration record, which caused | ||||
3584 | // the parser to jump to and parse the VST earlier. | ||||
3585 | assert(VSTOffset > 0)(static_cast <bool> (VSTOffset > 0) ? void (0) : __assert_fail ("VSTOffset > 0", "/build/llvm-toolchain-snapshot-13~++20210621111111+acefe0eaaf82/llvm/lib/Bitcode/Reader/BitcodeReader.cpp" , 3585, __extension__ __PRETTY_FUNCTION__)); | ||||
3586 | if (Error Err = Stream.SkipBlock()) | ||||
3587 | return Err; | ||||
3588 | } | ||||
3589 | break; | ||||
3590 | case bitc::CONSTANTS_BLOCK_ID: | ||||
3591 | if (Error Err = parseConstants()) | ||||
3592 | return Err; | ||||
3593 | if (Error Err = resolveGlobalAndIndirectSymbolInits()) | ||||
3594 | return Err; | ||||
3595 | break; | ||||
3596 | case bitc::METADATA_BLOCK_ID: | ||||
3597 | if (ShouldLazyLoadMetadata) { | ||||
3598 | if (Error Err = rememberAndSkipMetadata()) | ||||
3599 | return Err; | ||||
3600 | break; | ||||
3601 | } | ||||
3602 | assert(DeferredMetadataInfo.empty() && "Unexpected deferred metadata")(static_cast <bool> (DeferredMetadataInfo.empty() && "Unexpected deferred metadata") ? void (0) : __assert_fail ( "DeferredMetadataInfo.empty() && \"Unexpected deferred metadata\"" , "/build/llvm-toolchain-snapshot-13~++20210621111111+acefe0eaaf82/llvm/lib/Bitcode/Reader/BitcodeReader.cpp" , 3602, __extension__ __PRETTY_FUNCTION__)); | ||||
3603 | if (Error Err = MDLoader->parseModuleMetadata()) | ||||
3604 | return Err; | ||||
3605 | break; | ||||
3606 | case bitc::METADATA_KIND_BLOCK_ID: | ||||
3607 | if (Error Err = MDLoader->parseMetadataKinds()) | ||||
3608 | return Err; | ||||
3609 | break; | ||||
3610 | case bitc::FUNCTION_BLOCK_ID: | ||||
3611 | ResolveDataLayout(); | ||||
3612 | |||||
3613 | // If this is the first function body we've seen, reverse the | ||||
3614 | // FunctionsWithBodies list. | ||||
3615 | if (!SeenFirstFunctionBody) { | ||||
3616 | std::reverse(FunctionsWithBodies.begin(), FunctionsWithBodies.end()); | ||||
3617 | if (Error Err = globalCleanup()) | ||||
3618 | return Err; | ||||
3619 | SeenFirstFunctionBody = true; | ||||
3620 | } | ||||
3621 | |||||
3622 | if (VSTOffset > 0) { | ||||
3623 | // If we have a VST forward declaration record, make sure we | ||||
3624 | // parse the VST now if we haven't already. It is needed to | ||||
3625 | // set up the DeferredFunctionInfo vector for lazy reading. | ||||
3626 | if (!SeenValueSymbolTable) { | ||||
3627 | if (Error Err = BitcodeReader::parseValueSymbolTable(VSTOffset)) | ||||
3628 | return Err; | ||||
3629 | SeenValueSymbolTable = true; | ||||
3630 | // Fall through so that we record the NextUnreadBit below. | ||||
3631 | // This is necessary in case we have an anonymous function that | ||||
3632 | // is later materialized. Since it will not have a VST entry we | ||||
3633 | // need to fall back to the lazy parse to find its offset. | ||||
3634 | } else { | ||||
3635 | // If we have a VST forward declaration record, but have already | ||||
3636 | // parsed the VST (just above, when the first function body was | ||||
3637 | // encountered here), then we are resuming the parse after | ||||
3638 | // materializing functions. The ResumeBit points to the | ||||
3639 | // start of the last function block recorded in the | ||||
3640 | // DeferredFunctionInfo map. Skip it. | ||||
3641 | if (Error Err = Stream.SkipBlock()) | ||||
3642 | return Err; | ||||
3643 | continue; | ||||
3644 | } | ||||
3645 | } | ||||
3646 | |||||
3647 | // Support older bitcode files that did not have the function | ||||
3648 | // index in the VST, nor a VST forward declaration record, as | ||||
3649 | // well as anonymous functions that do not have VST entries. | ||||
3650 | // Build the DeferredFunctionInfo vector on the fly. | ||||
3651 | if (Error Err = rememberAndSkipFunctionBody()) | ||||
3652 | return Err; | ||||
3653 | |||||
3654 | // Suspend parsing when we reach the function bodies. Subsequent | ||||
3655 | // materialization calls will resume it when necessary. If the bitcode | ||||
3656 | // file is old, the symbol table will be at the end instead and will not | ||||
3657 | // have been seen yet. In this case, just finish the parse now. | ||||
3658 | if (SeenValueSymbolTable) { | ||||
3659 | NextUnreadBit = Stream.GetCurrentBitNo(); | ||||
3660 | // After the VST has been parsed, we need to make sure intrinsic name | ||||
3661 | // are auto-upgraded. | ||||
3662 | return globalCleanup(); | ||||
3663 | } | ||||
3664 | break; | ||||
3665 | case bitc::USELIST_BLOCK_ID: | ||||
3666 | if (Error Err = parseUseLists()) | ||||
3667 | return Err; | ||||
3668 | break; | ||||
3669 | case bitc::OPERAND_BUNDLE_TAGS_BLOCK_ID: | ||||
3670 | if (Error Err = parseOperandBundleTags()) | ||||
3671 | return Err; | ||||
3672 | break; | ||||
3673 | case bitc::SYNC_SCOPE_NAMES_BLOCK_ID: | ||||
3674 | if (Error Err = parseSyncScopeNames()) | ||||
3675 | return Err; | ||||
3676 | break; | ||||
3677 | } | ||||
3678 | continue; | ||||
3679 | |||||
3680 | case BitstreamEntry::Record: | ||||
3681 | // The interesting case. | ||||
3682 | break; | ||||
3683 | } | ||||
3684 | |||||
3685 | // Read a record. | ||||
3686 | Expected<unsigned> MaybeBitCode = Stream.readRecord(Entry.ID, Record); | ||||
3687 | if (!MaybeBitCode) | ||||
3688 | return MaybeBitCode.takeError(); | ||||
3689 | switch (unsigned BitCode = MaybeBitCode.get()) { | ||||
3690 | default: break; // Default behavior, ignore unknown content. | ||||
3691 | case bitc::MODULE_CODE_VERSION: { | ||||
3692 | Expected<unsigned> VersionOrErr = parseVersionRecord(Record); | ||||
3693 | if (!VersionOrErr) | ||||
3694 | return VersionOrErr.takeError(); | ||||
3695 | UseRelativeIDs = *VersionOrErr >= 1; | ||||
3696 | break; | ||||
3697 | } | ||||
3698 | case bitc::MODULE_CODE_TRIPLE: { // TRIPLE: [strchr x N] | ||||
3699 | if (ResolvedDataLayout) | ||||
3700 | return error("target triple too late in module"); | ||||
3701 | std::string S; | ||||
3702 | if (convertToString(Record, 0, S)) | ||||
3703 | return error("Invalid record"); | ||||
3704 | TheModule->setTargetTriple(S); | ||||
3705 | break; | ||||
3706 | } | ||||
3707 | case bitc::MODULE_CODE_DATALAYOUT: { // DATALAYOUT: [strchr x N] | ||||
3708 | if (ResolvedDataLayout) | ||||
3709 | return error("datalayout too late in module"); | ||||
3710 | std::string S; | ||||
3711 | if (convertToString(Record, 0, S)) | ||||
3712 | return error("Invalid record"); | ||||
3713 | TheModule->setDataLayout(S); | ||||
3714 | break; | ||||
3715 | } | ||||
3716 | case bitc::MODULE_CODE_ASM: { // ASM: [strchr x N] | ||||
3717 | std::string S; | ||||
3718 | if (convertToString(Record, 0, S)) | ||||
3719 | return error("Invalid record"); | ||||
3720 | TheModule->setModuleInlineAsm(S); | ||||
3721 | break; | ||||
3722 | } | ||||
3723 | case bitc::MODULE_CODE_DEPLIB: { // DEPLIB: [strchr x N] | ||||
3724 | // Deprecated, but still needed to read old bitcode files. | ||||
3725 | std::string S; | ||||
3726 | if (convertToString(Record, 0, S)) | ||||
3727 | return error("Invalid record"); | ||||
3728 | // Ignore value. | ||||
3729 | break; | ||||
3730 | } | ||||
3731 | case bitc::MODULE_CODE_SECTIONNAME: { // SECTIONNAME: [strchr x N] | ||||
3732 | std::string S; | ||||
3733 | if (convertToString(Record, 0, S)) | ||||
3734 | return error("Invalid record"); | ||||
3735 | SectionTable.push_back(S); | ||||
3736 | break; | ||||
3737 | } | ||||
3738 | case bitc::MODULE_CODE_GCNAME: { // SECTIONNAME: [strchr x N] | ||||
3739 | std::string S; | ||||
3740 | if (convertToString(Record, 0, S)) | ||||
3741 | return error("Invalid record"); | ||||
3742 | GCTable.push_back(S); | ||||
3743 | break; | ||||
3744 | } | ||||
3745 | case bitc::MODULE_CODE_COMDAT: | ||||
3746 | if (Error Err = parseComdatRecord(Record)) | ||||
3747 | return Err; | ||||
3748 | break; | ||||
3749 | case bitc::MODULE_CODE_GLOBALVAR: | ||||
3750 | if (Error Err = parseGlobalVarRecord(Record)) | ||||
3751 | return Err; | ||||
3752 | break; | ||||
3753 | case bitc::MODULE_CODE_FUNCTION: | ||||
3754 | ResolveDataLayout(); | ||||
3755 | if (Error Err = parseFunctionRecord(Record)) | ||||
3756 | return Err; | ||||
3757 | break; | ||||
3758 | case bitc::MODULE_CODE_IFUNC: | ||||
3759 | case bitc::MODULE_CODE_ALIAS: | ||||
3760 | case bitc::MODULE_CODE_ALIAS_OLD: | ||||
3761 | if (Error Err = parseGlobalIndirectSymbolRecord(BitCode, Record)) | ||||
3762 | return Err; | ||||
3763 | break; | ||||
3764 | /// MODULE_CODE_VSTOFFSET: [offset] | ||||
3765 | case bitc::MODULE_CODE_VSTOFFSET: | ||||
3766 | if (Record.empty()) | ||||
3767 | return error("Invalid record"); | ||||
3768 | // Note that we subtract 1 here because the offset is relative to one word | ||||
3769 | // before the start of the identification or module block, which was | ||||
3770 | // historically always the start of the regular bitcode header. | ||||
3771 | VSTOffset = Record[0] - 1; | ||||
3772 | break; | ||||
3773 | /// MODULE_CODE_SOURCE_FILENAME: [namechar x N] | ||||
3774 | case bitc::MODULE_CODE_SOURCE_FILENAME: | ||||
3775 | SmallString<128> ValueName; | ||||
3776 | if (convertToString(Record, 0, ValueName)) | ||||
3777 | return error("Invalid record"); | ||||
3778 | TheModule->setSourceFileName(ValueName); | ||||
3779 | break; | ||||
3780 | } | ||||
3781 | Record.clear(); | ||||
3782 | } | ||||
3783 | } | ||||
3784 | |||||
3785 | Error BitcodeReader::parseBitcodeInto(Module *M, bool ShouldLazyLoadMetadata, | ||||
3786 | bool IsImporting, | ||||
3787 | DataLayoutCallbackTy DataLayoutCallback) { | ||||
3788 | TheModule = M; | ||||
3789 | MDLoader = MetadataLoader(Stream, *M, ValueList, IsImporting, | ||||
3790 | [&](unsigned ID) { return getTypeByID(ID); }); | ||||
3791 | return parseModule(0, ShouldLazyLoadMetadata, DataLayoutCallback); | ||||
3792 | } | ||||
3793 | |||||
3794 | Error BitcodeReader::typeCheckLoadStoreInst(Type *ValType, Type *PtrType) { | ||||
3795 | if (!isa<PointerType>(PtrType)) | ||||
3796 | return error("Load/Store operand is not a pointer type"); | ||||
3797 | |||||
3798 | if (!cast<PointerType>(PtrType)->isOpaqueOrPointeeTypeMatches(ValType)) | ||||
3799 | return error("Explicit load/store type does not match pointee " | ||||
3800 | "type of pointer operand"); | ||||
3801 | if (!PointerType::isLoadableOrStorableType(ValType)) | ||||
3802 | return error("Cannot load/store from pointer"); | ||||
3803 | return Error::success(); | ||||
3804 | } | ||||
3805 | |||||
3806 | void BitcodeReader::propagateByValSRetTypes(CallBase *CB, | ||||
3807 | ArrayRef<Type *> ArgsTys) { | ||||
3808 | for (unsigned i = 0; i != CB->arg_size(); ++i) { | ||||
3809 | for (Attribute::AttrKind Kind : {Attribute::ByVal, Attribute::StructRet, | ||||
3810 | Attribute::InAlloca}) { | ||||
3811 | if (!CB->paramHasAttr(i, Kind)) | ||||
3812 | continue; | ||||
3813 | |||||
3814 | CB->removeParamAttr(i, Kind); | ||||
3815 | |||||
3816 | Type *PtrEltTy = cast<PointerType>(ArgsTys[i])->getElementType(); | ||||
3817 | Attribute NewAttr; | ||||
3818 | switch (Kind) { | ||||
3819 | case Attribute::ByVal: | ||||
3820 | NewAttr = Attribute::getWithByValType(Context, PtrEltTy); | ||||
3821 | break; | ||||
3822 | case Attribute::StructRet: | ||||
3823 | NewAttr = Attribute::getWithStructRetType(Context, PtrEltTy); | ||||
3824 | break; | ||||
3825 | case Attribute::InAlloca: | ||||
3826 | NewAttr = Attribute::getWithInAllocaType(Context, PtrEltTy); | ||||
3827 | break; | ||||
3828 | default: | ||||
3829 | llvm_unreachable("not an upgraded type attribute")::llvm::llvm_unreachable_internal("not an upgraded type attribute" , "/build/llvm-toolchain-snapshot-13~++20210621111111+acefe0eaaf82/llvm/lib/Bitcode/Reader/BitcodeReader.cpp" , 3829); | ||||
3830 | } | ||||
3831 | |||||
3832 | CB->addParamAttr(i, NewAttr); | ||||
3833 | } | ||||
3834 | } | ||||
3835 | } | ||||
3836 | |||||
3837 | /// Lazily parse the specified function body block. | ||||
3838 | Error BitcodeReader::parseFunctionBody(Function *F) { | ||||
3839 | if (Error Err = Stream.EnterSubBlock(bitc::FUNCTION_BLOCK_ID)) | ||||
3840 | return Err; | ||||
3841 | |||||
3842 | // Unexpected unresolved metadata when parsing function. | ||||
3843 | if (MDLoader->hasFwdRefs()) | ||||
3844 | return error("Invalid function metadata: incoming forward references"); | ||||
3845 | |||||
3846 | InstructionList.clear(); | ||||
3847 | unsigned ModuleValueListSize = ValueList.size(); | ||||
3848 | unsigned ModuleMDLoaderSize = MDLoader->size(); | ||||
3849 | |||||
3850 | // Add all the function arguments to the value table. | ||||
3851 | #ifndef NDEBUG | ||||
3852 | unsigned ArgNo = 0; | ||||
3853 | FunctionType *FTy = FunctionTypes[F]; | ||||
3854 | #endif | ||||
3855 | for (Argument &I : F->args()) { | ||||
3856 | assert(I.getType() == FTy->getParamType(ArgNo++) &&(static_cast <bool> (I.getType() == FTy->getParamType (ArgNo++) && "Incorrect fully specified type for Function Argument" ) ? void (0) : __assert_fail ("I.getType() == FTy->getParamType(ArgNo++) && \"Incorrect fully specified type for Function Argument\"" , "/build/llvm-toolchain-snapshot-13~++20210621111111+acefe0eaaf82/llvm/lib/Bitcode/Reader/BitcodeReader.cpp" , 3857, __extension__ __PRETTY_FUNCTION__)) | ||||
3857 | "Incorrect fully specified type for Function Argument")(static_cast <bool> (I.getType() == FTy->getParamType (ArgNo++) && "Incorrect fully specified type for Function Argument" ) ? void (0) : __assert_fail ("I.getType() == FTy->getParamType(ArgNo++) && \"Incorrect fully specified type for Function Argument\"" , "/build/llvm-toolchain-snapshot-13~++20210621111111+acefe0eaaf82/llvm/lib/Bitcode/Reader/BitcodeReader.cpp" , 3857, __extension__ __PRETTY_FUNCTION__)); | ||||
3858 | ValueList.push_back(&I); | ||||
3859 | } | ||||
3860 | unsigned NextValueNo = ValueList.size(); | ||||
3861 | BasicBlock *CurBB = nullptr; | ||||
3862 | unsigned CurBBNo = 0; | ||||
3863 | |||||
3864 | DebugLoc LastLoc; | ||||
3865 | auto getLastInstruction = [&]() -> Instruction * { | ||||
3866 | if (CurBB && !CurBB->empty()) | ||||
3867 | return &CurBB->back(); | ||||
3868 | else if (CurBBNo && FunctionBBs[CurBBNo - 1] && | ||||
3869 | !FunctionBBs[CurBBNo - 1]->empty()) | ||||
3870 | return &FunctionBBs[CurBBNo - 1]->back(); | ||||
3871 | return nullptr; | ||||
3872 | }; | ||||
3873 | |||||
3874 | std::vector<OperandBundleDef> OperandBundles; | ||||
3875 | |||||
3876 | // Read all the records. | ||||
3877 | SmallVector<uint64_t, 64> Record; | ||||
3878 | |||||
3879 | while (true) { | ||||
3880 | Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); | ||||
3881 | if (!MaybeEntry) | ||||
3882 | return MaybeEntry.takeError(); | ||||
3883 | llvm::BitstreamEntry Entry = MaybeEntry.get(); | ||||
3884 | |||||
3885 | switch (Entry.Kind) { | ||||
3886 | case BitstreamEntry::Error: | ||||
3887 | return error("Malformed block"); | ||||
3888 | case BitstreamEntry::EndBlock: | ||||
3889 | goto OutOfRecordLoop; | ||||
3890 | |||||
3891 | case BitstreamEntry::SubBlock: | ||||
3892 | switch (Entry.ID) { | ||||
3893 | default: // Skip unknown content. | ||||
3894 | if (Error Err = Stream.SkipBlock()) | ||||
3895 | return Err; | ||||
3896 | break; | ||||
3897 | case bitc::CONSTANTS_BLOCK_ID: | ||||
3898 | if (Error Err = parseConstants()) | ||||
3899 | return Err; | ||||
3900 | NextValueNo = ValueList.size(); | ||||
3901 | break; | ||||
3902 | case bitc::VALUE_SYMTAB_BLOCK_ID: | ||||
3903 | if (Error Err = parseValueSymbolTable()) | ||||
3904 | return Err; | ||||
3905 | break; | ||||
3906 | case bitc::METADATA_ATTACHMENT_ID: | ||||
3907 | if (Error Err = MDLoader->parseMetadataAttachment(*F, InstructionList)) | ||||
3908 | return Err; | ||||
3909 | break; | ||||
3910 | case bitc::METADATA_BLOCK_ID: | ||||
3911 | assert(DeferredMetadataInfo.empty() &&(static_cast <bool> (DeferredMetadataInfo.empty() && "Must read all module-level metadata before function-level") ? void (0) : __assert_fail ("DeferredMetadataInfo.empty() && \"Must read all module-level metadata before function-level\"" , "/build/llvm-toolchain-snapshot-13~++20210621111111+acefe0eaaf82/llvm/lib/Bitcode/Reader/BitcodeReader.cpp" , 3912, __extension__ __PRETTY_FUNCTION__)) | ||||
3912 | "Must read all module-level metadata before function-level")(static_cast <bool> (DeferredMetadataInfo.empty() && "Must read all module-level metadata before function-level") ? void (0) : __assert_fail ("DeferredMetadataInfo.empty() && \"Must read all module-level metadata before function-level\"" , "/build/llvm-toolchain-snapshot-13~++20210621111111+acefe0eaaf82/llvm/lib/Bitcode/Reader/BitcodeReader.cpp" , 3912, __extension__ __PRETTY_FUNCTION__)); | ||||
3913 | if (Error Err = MDLoader->parseFunctionMetadata()) | ||||
3914 | return Err; | ||||
3915 | break; | ||||
3916 | case bitc::USELIST_BLOCK_ID: | ||||
3917 | if (Error Err = parseUseLists()) | ||||
3918 | return Err; | ||||
3919 | break; | ||||
3920 | } | ||||
3921 | continue; | ||||
3922 | |||||
3923 | case BitstreamEntry::Record: | ||||
3924 | // The interesting case. | ||||
3925 | break; | ||||
3926 | } | ||||
3927 | |||||
3928 | // Read a record. | ||||
3929 | Record.clear(); | ||||
3930 | Instruction *I = nullptr; | ||||
3931 | Expected<unsigned> MaybeBitCode = Stream.readRecord(Entry.ID, Record); | ||||
3932 | if (!MaybeBitCode) | ||||
3933 | return MaybeBitCode.takeError(); | ||||
3934 | switch (unsigned BitCode = MaybeBitCode.get()) { | ||||
3935 | default: // Default behavior: reject | ||||
3936 | return error("Invalid value"); | ||||
3937 | case bitc::FUNC_CODE_DECLAREBLOCKS: { // DECLAREBLOCKS: [nblocks] | ||||
3938 | if (Record.empty() || Record[0] == 0) | ||||
3939 | return error("Invalid record"); | ||||
3940 | // Create all the basic blocks for the function. | ||||
3941 | FunctionBBs.resize(Record[0]); | ||||
3942 | |||||
3943 | // See if anything took the address of blocks in this function. | ||||
3944 | auto BBFRI = BasicBlockFwdRefs.find(F); | ||||
3945 | if (BBFRI == BasicBlockFwdRefs.end()) { | ||||
3946 | for (unsigned i = 0, e = FunctionBBs.size(); i != e; ++i) | ||||
3947 | FunctionBBs[i] = BasicBlock::Create(Context, "", F); | ||||
3948 | } else { | ||||
3949 | auto &BBRefs = BBFRI->second; | ||||
3950 | // Check for invalid basic block references. | ||||
3951 | if (BBRefs.size() > FunctionBBs.size()) | ||||
3952 | return error("Invalid ID"); | ||||
3953 | assert(!BBRefs.empty() && "Unexpected empty array")(static_cast <bool> (!BBRefs.empty() && "Unexpected empty array" ) ? void (0) : __assert_fail ("!BBRefs.empty() && \"Unexpected empty array\"" , "/build/llvm-toolchain-snapshot-13~++20210621111111+acefe0eaaf82/llvm/lib/Bitcode/Reader/BitcodeReader.cpp" , 3953, __extension__ __PRETTY_FUNCTION__)); | ||||
3954 | assert(!BBRefs.front() && "Invalid reference to entry block")(static_cast <bool> (!BBRefs.front() && "Invalid reference to entry block" ) ? void (0) : __assert_fail ("!BBRefs.front() && \"Invalid reference to entry block\"" , "/build/llvm-toolchain-snapshot-13~++20210621111111+acefe0eaaf82/llvm/lib/Bitcode/Reader/BitcodeReader.cpp" , 3954, __extension__ __PRETTY_FUNCTION__)); | ||||
3955 | for (unsigned I = 0, E = FunctionBBs.size(), RE = BBRefs.size(); I != E; | ||||
3956 | ++I) | ||||
3957 | if (I < RE && BBRefs[I]) { | ||||
3958 | BBRefs[I]->insertInto(F); | ||||
3959 | FunctionBBs[I] = BBRefs[I]; | ||||
3960 | } else { | ||||
3961 | FunctionBBs[I] = BasicBlock::Create(Context, "", F); | ||||
3962 | } | ||||
3963 | |||||
3964 | // Erase from the table. | ||||
3965 | BasicBlockFwdRefs.erase(BBFRI); | ||||
3966 | } | ||||
3967 | |||||
3968 | CurBB = FunctionBBs[0]; | ||||
3969 | continue; | ||||
3970 | } | ||||
3971 | |||||
3972 | case bitc::FUNC_CODE_DEBUG_LOC_AGAIN: // DEBUG_LOC_AGAIN | ||||
3973 | // This record indicates that the last instruction is at the same | ||||
3974 | // location as the previous instruction with a location. | ||||
3975 | I = getLastInstruction(); | ||||
3976 | |||||
3977 | if (!I) | ||||
3978 | return error("Invalid record"); | ||||
3979 | I->setDebugLoc(LastLoc); | ||||
3980 | I = nullptr; | ||||
3981 | continue; | ||||
3982 | |||||
3983 | case bitc::FUNC_CODE_DEBUG_LOC: { // DEBUG_LOC: [line, col, scope, ia] | ||||
3984 | I = getLastInstruction(); | ||||
3985 | if (!I || Record.size() < 4) | ||||
3986 | return error("Invalid record"); | ||||
3987 | |||||
3988 | unsigned Line = Record[0], Col = Record[1]; | ||||
3989 | unsigned ScopeID = Record[2], IAID = Record[3]; | ||||
3990 | bool isImplicitCode = Record.size() == 5 && Record[4]; | ||||
3991 | |||||
3992 | MDNode *Scope = nullptr, *IA = nullptr; | ||||
3993 | if (ScopeID) { | ||||
3994 | Scope = dyn_cast_or_null<MDNode>( | ||||
3995 | MDLoader->getMetadataFwdRefOrLoad(ScopeID - 1)); | ||||
3996 | if (!Scope) | ||||
3997 | return error("Invalid record"); | ||||
3998 | } | ||||
3999 | if (IAID) { | ||||
4000 | IA = dyn_cast_or_null<MDNode>( | ||||
4001 | MDLoader->getMetadataFwdRefOrLoad(IAID - 1)); | ||||
4002 | if (!IA) | ||||
4003 | return error("Invalid record"); | ||||
4004 | } | ||||
4005 | LastLoc = DILocation::get(Scope->getContext(), Line, Col, Scope, IA, | ||||
4006 | isImplicitCode); | ||||
4007 | I->setDebugLoc(LastLoc); | ||||
4008 | I = nullptr; | ||||
4009 | continue; | ||||
4010 | } | ||||
4011 | case bitc::FUNC_CODE_INST_UNOP: { // UNOP: [opval, ty, opcode] | ||||
4012 | unsigned OpNum = 0; | ||||
4013 | Value *LHS; | ||||
4014 | if (getValueTypePair(Record, OpNum, NextValueNo, LHS) || | ||||
4015 | OpNum+1 > Record.size()) | ||||
4016 | return error("Invalid record"); | ||||
4017 | |||||
4018 | int Opc = getDecodedUnaryOpcode(Record[OpNum++], LHS->getType()); | ||||
4019 | if (Opc == -1) | ||||
4020 | return error("Invalid record"); | ||||
4021 | I = UnaryOperator::Create((Instruction::UnaryOps)Opc, LHS); | ||||
4022 | InstructionList.push_back(I); | ||||
4023 | if (OpNum < Record.size()) { | ||||
4024 | if (isa<FPMathOperator>(I)) { | ||||
4025 | FastMathFlags FMF = getDecodedFastMathFlags(Record[OpNum]); | ||||
4026 | if (FMF.any()) | ||||
4027 | I->setFastMathFlags(FMF); | ||||
4028 | } | ||||
4029 | } | ||||
4030 | break; | ||||
4031 | } | ||||
4032 | case bitc::FUNC_CODE_INST_BINOP: { // BINOP: [opval, ty, opval, opcode] | ||||
4033 | unsigned OpNum = 0; | ||||
4034 | Value *LHS, *RHS; | ||||
4035 | if (getValueTypePair(Record, OpNum, NextValueNo, LHS) || | ||||
4036 | popValue(Record, OpNum, NextValueNo, LHS->getType(), RHS) || | ||||
4037 | OpNum+1 > Record.size()) | ||||
4038 | return error("Invalid record"); | ||||
4039 | |||||
4040 | int Opc = getDecodedBinaryOpcode(Record[OpNum++], LHS->getType()); | ||||
4041 | if (Opc == -1) | ||||
4042 | return error("Invalid record"); | ||||
4043 | I = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS); | ||||
4044 | InstructionList.push_back(I); | ||||
4045 | if (OpNum < Record.size()) { | ||||
4046 | if (Opc == Instruction::Add || | ||||
4047 | Opc == Instruction::Sub || | ||||
4048 | Opc == Instruction::Mul || | ||||
4049 | Opc == Instruction::Shl) { | ||||
4050 | if (Record[OpNum] & (1 << bitc::OBO_NO_SIGNED_WRAP)) | ||||
4051 | cast<BinaryOperator>(I)->setHasNoSignedWrap(true); | ||||
4052 | if (Record[OpNum] & (1 << bitc::OBO_NO_UNSIGNED_WRAP)) | ||||
4053 | cast<BinaryOperator>(I)->setHasNoUnsignedWrap(true); | ||||
4054 | } else if (Opc == Instruction::SDiv || | ||||
4055 | Opc == Instruction::UDiv || | ||||
4056 | Opc == Instruction::LShr || | ||||
4057 | Opc == Instruction::AShr) { | ||||
4058 | if (Record[OpNum] & (1 << bitc::PEO_EXACT)) | ||||
4059 | cast<BinaryOperator>(I)->setIsExact(true); | ||||
4060 | } else if (isa<FPMathOperator>(I)) { | ||||
4061 | FastMathFlags FMF = getDecodedFastMathFlags(Record[OpNum]); | ||||
4062 | if (FMF.any()) | ||||
4063 | I->setFastMathFlags(FMF); | ||||
4064 | } | ||||
4065 | |||||
4066 | } | ||||
4067 | break; | ||||
4068 | } | ||||
4069 | case bitc::FUNC_CODE_INST_CAST: { // CAST: [opval, opty, destty, castopc] | ||||
4070 | unsigned OpNum = 0; | ||||
4071 | Value *Op; | ||||
4072 | if (getValueTypePair(Record, OpNum, NextValueNo, Op) || | ||||
4073 | OpNum+2 != Record.size()) | ||||
4074 | return error("Invalid record"); | ||||
4075 | |||||
4076 | Type *ResTy = getTypeByID(Record[OpNum]); | ||||
4077 | int Opc = getDecodedCastOpcode(Record[OpNum + 1]); | ||||
4078 | if (Opc == -1 || !ResTy) | ||||
4079 | return error("Invalid record"); | ||||
4080 | Instruction *Temp = nullptr; | ||||
4081 | if ((I = UpgradeBitCastInst(Opc, Op, ResTy, Temp))) { | ||||
4082 | if (Temp) { | ||||
4083 | InstructionList.push_back(Temp); | ||||
4084 | assert(CurBB && "No current BB?")(static_cast <bool> (CurBB && "No current BB?") ? void (0) : __assert_fail ("CurBB && \"No current BB?\"" , "/build/llvm-toolchain-snapshot-13~++20210621111111+acefe0eaaf82/llvm/lib/Bitcode/Reader/BitcodeReader.cpp" , 4084, __extension__ __PRETTY_FUNCTION__)); | ||||
4085 | CurBB->getInstList().push_back(Temp); | ||||
4086 | } | ||||
4087 | } else { | ||||
4088 | auto CastOp = (Instruction::CastOps)Opc; | ||||
4089 | if (!CastInst::castIsValid(CastOp, Op, ResTy)) | ||||
4090 | return error("Invalid cast"); | ||||
4091 | I = CastInst::Create(CastOp, Op, ResTy); | ||||
4092 | } | ||||
4093 | InstructionList.push_back(I); | ||||
4094 | break; | ||||
4095 | } | ||||
4096 | case bitc::FUNC_CODE_INST_INBOUNDS_GEP_OLD: | ||||
4097 | case bitc::FUNC_CODE_INST_GEP_OLD: | ||||
4098 | case bitc::FUNC_CODE_INST_GEP: { // GEP: type, [n x operands] | ||||
4099 | unsigned OpNum = 0; | ||||
4100 | |||||
4101 | Type *Ty; | ||||
4102 | bool InBounds; | ||||
4103 | |||||
4104 | if (BitCode == bitc::FUNC_CODE_INST_GEP) { | ||||
4105 | InBounds = Record[OpNum++]; | ||||
4106 | Ty = getTypeByID(Record[OpNum++]); | ||||
4107 | } else { | ||||
4108 | InBounds = BitCode == bitc::FUNC_CODE_INST_INBOUNDS_GEP_OLD; | ||||
4109 | Ty = nullptr; | ||||
4110 | } | ||||
4111 | |||||
4112 | Value *BasePtr; | ||||
4113 | if (getValueTypePair(Record, OpNum, NextValueNo, BasePtr)) | ||||
4114 | return error("Invalid record"); | ||||
4115 | |||||
4116 | if (!Ty) { | ||||
4117 | Ty = cast<PointerType>(BasePtr->getType()->getScalarType()) | ||||
4118 | ->getElementType(); | ||||
4119 | } else if (!cast<PointerType>(BasePtr->getType()->getScalarType()) | ||||
4120 | ->isOpaqueOrPointeeTypeMatches(Ty)) { | ||||
4121 | return error( | ||||
4122 | "Explicit gep type does not match pointee type of pointer operand"); | ||||
4123 | } | ||||
4124 | |||||
4125 | SmallVector<Value*, 16> GEPIdx; | ||||
4126 | while (OpNum != Record.size()) { | ||||
4127 | Value *Op; | ||||
4128 | if (getValueTypePair(Record, OpNum, NextValueNo, Op)) | ||||
4129 | return error("Invalid record"); | ||||
4130 | GEPIdx.push_back(Op); | ||||
4131 | } | ||||
4132 | |||||
4133 | I = GetElementPtrInst::Create(Ty, BasePtr, GEPIdx); | ||||
4134 | |||||
4135 | InstructionList.push_back(I); | ||||
4136 | if (InBounds) | ||||
4137 | cast<GetElementPtrInst>(I)->setIsInBounds(true); | ||||
4138 | break; | ||||
4139 | } | ||||
4140 | |||||
4141 | case bitc::FUNC_CODE_INST_EXTRACTVAL: { | ||||
4142 | // EXTRACTVAL: [opty, opval, n x indices] | ||||
4143 | unsigned OpNum = 0; | ||||
4144 | Value *Agg; | ||||
4145 | if (getValueTypePair(Record, OpNum, NextValueNo, Agg)) | ||||
4146 | return error("Invalid record"); | ||||
4147 | Type *Ty = Agg->getType(); | ||||
4148 | |||||
4149 | unsigned RecSize = Record.size(); | ||||
4150 | if (OpNum == RecSize) | ||||
4151 | return error("EXTRACTVAL: Invalid instruction with 0 indices"); | ||||
4152 | |||||
4153 | SmallVector<unsigned, 4> EXTRACTVALIdx; | ||||
4154 | for (; OpNum != RecSize; ++OpNum) { | ||||
4155 | bool IsArray = Ty->isArrayTy(); | ||||
4156 | bool IsStruct = Ty->isStructTy(); | ||||
4157 | uint64_t Index = Record[OpNum]; | ||||
4158 | |||||
4159 | if (!IsStruct && !IsArray) | ||||
4160 | return error("EXTRACTVAL: Invalid type"); | ||||
4161 | if ((unsigned)Index != Index) | ||||
4162 | return error("Invalid value"); | ||||
4163 | if (IsStruct && Index >= Ty->getStructNumElements()) | ||||
4164 | return error("EXTRACTVAL: Invalid struct index"); | ||||
4165 | if (IsArray && Index >= Ty->getArrayNumElements()) | ||||
4166 | return error("EXTRACTVAL: Invalid array index"); | ||||
4167 | EXTRACTVALIdx.push_back((unsigned)Index); | ||||
4168 | |||||
4169 | if (IsStruct) | ||||
4170 | Ty = Ty->getStructElementType(Index); | ||||
4171 | else | ||||
4172 | Ty = Ty->getArrayElementType(); | ||||
4173 | } | ||||
4174 | |||||
4175 | I = ExtractValueInst::Create(Agg, EXTRACTVALIdx); | ||||
4176 | InstructionList.push_back(I); | ||||
4177 | break; | ||||
4178 | } | ||||
4179 | |||||
4180 | case bitc::FUNC_CODE_INST_INSERTVAL: { | ||||
4181 | // INSERTVAL: [opty, opval, opty, opval, n x indices] | ||||
4182 | unsigned OpNum = 0; | ||||
4183 | Value *Agg; | ||||
4184 | if (getValueTypePair(Record, OpNum, NextValueNo, Agg)) | ||||
4185 | return error("Invalid record"); | ||||
4186 | Value *Val; | ||||
4187 | if (getValueTypePair(Record, OpNum, NextValueNo, Val)) | ||||
4188 | return error("Invalid record"); | ||||
4189 | |||||
4190 | unsigned RecSize = Record.size(); | ||||
4191 | if (OpNum == RecSize) | ||||
4192 | return error("INSERTVAL: Invalid instruction with 0 indices"); | ||||
4193 | |||||
4194 | SmallVector<unsigned, 4> INSERTVALIdx; | ||||
4195 | Type *CurTy = Agg->getType(); | ||||
4196 | for (; OpNum != RecSize; ++OpNum) { | ||||
4197 | bool IsArray = CurTy->isArrayTy(); | ||||
4198 | bool IsStruct = CurTy->isStructTy(); | ||||
4199 | uint64_t Index = Record[OpNum]; | ||||
4200 | |||||
4201 | if (!IsStruct && !IsArray) | ||||
4202 | return error("INSERTVAL: Invalid type"); | ||||
4203 | if ((unsigned)Index != Index) | ||||
4204 | return error("Invalid value"); | ||||
4205 | if (IsStruct && Index >= CurTy->getStructNumElements()) | ||||
4206 | return error("INSERTVAL: Invalid struct index"); | ||||
4207 | if (IsArray && Index >= CurTy->getArrayNumElements()) | ||||
4208 | return error("INSERTVAL: Invalid array index"); | ||||
4209 | |||||
4210 | INSERTVALIdx.push_back((unsigned)Index); | ||||
4211 | if (IsStruct) | ||||
4212 | CurTy = CurTy->getStructElementType(Index); | ||||
4213 | else | ||||
4214 | CurTy = CurTy->getArrayElementType(); | ||||
4215 | } | ||||
4216 | |||||
4217 | if (CurTy != Val->getType()) | ||||
4218 | return error("Inserted value type doesn't match aggregate type"); | ||||
4219 | |||||
4220 | I = InsertValueInst::Create(Agg, Val, INSERTVALIdx); | ||||
4221 | InstructionList.push_back(I); | ||||
4222 | break; | ||||
4223 | } | ||||
4224 | |||||
4225 | case bitc::FUNC_CODE_INST_SELECT: { // SELECT: [opval, ty, opval, opval] | ||||
4226 | // obsolete form of select | ||||
4227 | // handles select i1 ... in old bitcode | ||||
4228 | unsigned OpNum = 0; | ||||
4229 | Value *TrueVal, *FalseVal, *Cond; | ||||
4230 | if (getValueTypePair(Record, OpNum, NextValueNo, TrueVal) || | ||||
4231 | popValue(Record, OpNum, NextValueNo, TrueVal->getType(), FalseVal) || | ||||
4232 | popValue(Record, OpNum, NextValueNo, Type::getInt1Ty(Context), Cond)) | ||||
4233 | return error("Invalid record"); | ||||
4234 | |||||
4235 | I = SelectInst::Create(Cond, TrueVal, FalseVal); | ||||
4236 | InstructionList.push_back(I); | ||||
4237 | break; | ||||
4238 | } | ||||
4239 | |||||
4240 | case bitc::FUNC_CODE_INST_VSELECT: {// VSELECT: [ty,opval,opval,predty,pred] | ||||
4241 | // new form of select | ||||
4242 | // handles select i1 or select [N x i1] | ||||
4243 | unsigned OpNum = 0; | ||||
4244 | Value *TrueVal, *FalseVal, *Cond; | ||||
4245 | if (getValueTypePair(Record, OpNum, NextValueNo, TrueVal) || | ||||
4246 | popValue(Record, OpNum, NextValueNo, TrueVal->getType(), FalseVal) || | ||||
4247 | getValueTypePair(Record, OpNum, NextValueNo, Cond)) | ||||
4248 | return error("Invalid record"); | ||||
4249 | |||||
4250 | // select condition can be either i1 or [N x i1] | ||||
4251 | if (VectorType* vector_type = | ||||
4252 | dyn_cast<VectorType>(Cond->getType())) { | ||||
4253 | // expect <n x i1> | ||||
4254 | if (vector_type->getElementType() != Type::getInt1Ty(Context)) | ||||
4255 | return error("Invalid type for value"); | ||||
4256 | } else { | ||||
4257 | // expect i1 | ||||
4258 | if (Cond->getType() != Type::getInt1Ty(Context)) | ||||
4259 | return error("Invalid type for value"); | ||||
4260 | } | ||||
4261 | |||||
4262 | I = SelectInst::Create(Cond, TrueVal, FalseVal); | ||||
4263 | InstructionList.push_back(I); | ||||
4264 | if (OpNum < Record.size() && isa<FPMathOperator>(I)) { | ||||
4265 | FastMathFlags FMF = getDecodedFastMathFlags(Record[OpNum]); | ||||
4266 | if (FMF.any()) | ||||
4267 | I->setFastMathFlags(FMF); | ||||
4268 | } | ||||
4269 | break; | ||||
4270 | } | ||||
4271 | |||||
4272 | case bitc::FUNC_CODE_INST_EXTRACTELT: { // EXTRACTELT: [opty, opval, opval] | ||||
4273 | unsigned OpNum = 0; | ||||
4274 | Value *Vec, *Idx; | ||||
4275 | if (getValueTypePair(Record, OpNum, NextValueNo, Vec) || | ||||
4276 | getValueTypePair(Record, OpNum, NextValueNo, Idx)) | ||||
4277 | return error("Invalid record"); | ||||
4278 | if (!Vec->getType()->isVectorTy()) | ||||
4279 | return error("Invalid type for value"); | ||||
4280 | I = ExtractElementInst::Create(Vec, Idx); | ||||
4281 | InstructionList.push_back(I); | ||||
4282 | break; | ||||
4283 | } | ||||
4284 | |||||
4285 | case bitc::FUNC_CODE_INST_INSERTELT: { // INSERTELT: [ty, opval,opval,opval] | ||||
4286 | unsigned OpNum = 0; | ||||
4287 | Value *Vec, *Elt, *Idx; | ||||
4288 | if (getValueTypePair(Record, OpNum, NextValueNo, Vec)) | ||||
4289 | return error("Invalid record"); | ||||
4290 | if (!Vec->getType()->isVectorTy()) | ||||
4291 | return error("Invalid type for value"); | ||||
4292 | if (popValue(Record, OpNum, NextValueNo, | ||||
4293 | cast<VectorType>(Vec->getType())->getElementType(), Elt) || | ||||
4294 | getValueTypePair(Record, OpNum, NextValueNo, Idx)) | ||||
4295 | return error("Invalid record"); | ||||
4296 | I = InsertElementInst::Create(Vec, Elt, Idx); | ||||
4297 | InstructionList.push_back(I); | ||||
4298 | break; | ||||
4299 | } | ||||
4300 | |||||
4301 | case bitc::FUNC_CODE_INST_SHUFFLEVEC: {// SHUFFLEVEC: [opval,ty,opval,opval] | ||||
4302 | unsigned OpNum = 0; | ||||
4303 | Value *Vec1, *Vec2, *Mask; | ||||
4304 | if (getValueTypePair(Record, OpNum, NextValueNo, Vec1) || | ||||
4305 | popValue(Record, OpNum, NextValueNo, Vec1->getType(), Vec2)) | ||||
4306 | return error("Invalid record"); | ||||
4307 | |||||
4308 | if (getValueTypePair(Record, OpNum, NextValueNo, Mask)) | ||||
4309 | return error("Invalid record"); | ||||
4310 | if (!Vec1->getType()->isVectorTy() || !Vec2->getType()->isVectorTy()) | ||||
4311 | return error("Invalid type for value"); | ||||
4312 | |||||
4313 | I = new ShuffleVectorInst(Vec1, Vec2, Mask); | ||||
4314 | InstructionList.push_back(I); | ||||
4315 | break; | ||||
4316 | } | ||||
4317 | |||||
4318 | case bitc::FUNC_CODE_INST_CMP: // CMP: [opty, opval, opval, pred] | ||||
4319 | // Old form of ICmp/FCmp returning bool | ||||
4320 | // Existed to differentiate between icmp/fcmp and vicmp/vfcmp which were | ||||
4321 | // both legal on vectors but had different behaviour. | ||||
4322 | case bitc::FUNC_CODE_INST_CMP2: { // CMP2: [opty, opval, opval, pred] | ||||
4323 | // FCmp/ICmp returning bool or vector of bool | ||||
4324 | |||||
4325 | unsigned OpNum = 0; | ||||
4326 | Value *LHS, *RHS; | ||||
4327 | if (getValueTypePair(Record, OpNum, NextValueNo, LHS) || | ||||
4328 | popValue(Record, OpNum, NextValueNo, LHS->getType(), RHS)) | ||||
4329 | return error("Invalid record"); | ||||
4330 | |||||
4331 | if (OpNum >= Record.size()) | ||||
4332 | return error( | ||||
4333 | "Invalid record: operand number exceeded available operands"); | ||||
4334 | |||||
4335 | unsigned PredVal = Record[OpNum]; | ||||
4336 | bool IsFP = LHS->getType()->isFPOrFPVectorTy(); | ||||
4337 | FastMathFlags FMF; | ||||
4338 | if (IsFP && Record.size() > OpNum+1) | ||||
4339 | FMF = getDecodedFastMathFlags(Record[++OpNum]); | ||||
4340 | |||||
4341 | if (OpNum+1 != Record.size()) | ||||
4342 | return error("Invalid record"); | ||||
4343 | |||||
4344 | if (LHS->getType()->isFPOrFPVectorTy()) | ||||
4345 | I = new FCmpInst((FCmpInst::Predicate)PredVal, LHS, RHS); | ||||
4346 | else | ||||
4347 | I = new ICmpInst((ICmpInst::Predicate)PredVal, LHS, RHS); | ||||
4348 | |||||
4349 | if (FMF.any()) | ||||
4350 | I->setFastMathFlags(FMF); | ||||
4351 | InstructionList.push_back(I); | ||||
4352 | break; | ||||
4353 | } | ||||
4354 | |||||
4355 | case bitc::FUNC_CODE_INST_RET: // RET: [opty,opval<optional>] | ||||
4356 | { | ||||
4357 | unsigned Size = Record.size(); | ||||
4358 | if (Size == 0) { | ||||
4359 | I = ReturnInst::Create(Context); | ||||
4360 | InstructionList.push_back(I); | ||||
4361 | break; | ||||
4362 | } | ||||
4363 | |||||
4364 | unsigned OpNum = 0; | ||||
4365 | Value *Op = nullptr; | ||||
4366 | if (getValueTypePair(Record, OpNum, NextValueNo, Op)) | ||||
4367 | return error("Invalid record"); | ||||
4368 | if (OpNum != Record.size()) | ||||
4369 | return error("Invalid record"); | ||||
4370 | |||||
4371 | I = ReturnInst::Create(Context, Op); | ||||
4372 | InstructionList.push_back(I); | ||||
4373 | break; | ||||
4374 | } | ||||
4375 | case bitc::FUNC_CODE_INST_BR: { // BR: [bb#, bb#, opval] or [bb#] | ||||
4376 | if (Record.size() != 1 && Record.size() != 3) | ||||
4377 | return error("Invalid record"); | ||||
4378 | BasicBlock *TrueDest = getBasicBlock(Record[0]); | ||||
4379 | if (!TrueDest) | ||||
4380 | return error("Invalid record"); | ||||
4381 | |||||
4382 | if (Record.size() == 1) { | ||||
4383 | I = BranchInst::Create(TrueDest); | ||||
4384 | InstructionList.push_back(I); | ||||
4385 | } | ||||
4386 | else { | ||||
4387 | BasicBlock *FalseDest = getBasicBlock(Record[1]); | ||||
4388 | Value *Cond = getValue(Record, 2, NextValueNo, | ||||
4389 | Type::getInt1Ty(Context)); | ||||
4390 | if (!FalseDest || !Cond) | ||||
4391 | return error("Invalid record"); | ||||
4392 | I = BranchInst::Create(TrueDest, FalseDest, Cond); | ||||
4393 | InstructionList.push_back(I); | ||||
4394 | } | ||||
4395 | break; | ||||
4396 | } | ||||
4397 | case bitc::FUNC_CODE_INST_CLEANUPRET: { // CLEANUPRET: [val] or [val,bb#] | ||||
4398 | if (Record.size() != 1 && Record.size() != 2) | ||||
4399 | return error("Invalid record"); | ||||
4400 | unsigned Idx = 0; | ||||
4401 | Value *CleanupPad = | ||||
4402 | getValue(Record, Idx++, NextValueNo, Type::getTokenTy(Context)); | ||||
4403 | if (!CleanupPad) | ||||
4404 | return error("Invalid record"); | ||||
4405 | BasicBlock *UnwindDest = nullptr; | ||||
4406 | if (Record.size() == 2) { | ||||
4407 | UnwindDest = getBasicBlock(Record[Idx++]); | ||||
4408 | if (!UnwindDest) | ||||
4409 | return error("Invalid record"); | ||||
4410 | } | ||||
4411 | |||||
4412 | I = CleanupReturnInst::Create(CleanupPad, UnwindDest); | ||||
4413 | InstructionList.push_back(I); | ||||
4414 | break; | ||||
4415 | } | ||||
4416 | case bitc::FUNC_CODE_INST_CATCHRET: { // CATCHRET: [val,bb#] | ||||
4417 | if (Record.size() != 2) | ||||
4418 | return error("Invalid record"); | ||||
4419 | unsigned Idx = 0; | ||||
4420 | Value *CatchPad = | ||||
4421 | getValue(Record, Idx++, NextValueNo, Type::getTokenTy(Context)); | ||||
4422 | if (!CatchPad) | ||||
4423 | return error("Invalid record"); | ||||
4424 | BasicBlock *BB = getBasicBlock(Record[Idx++]); | ||||
4425 | if (!BB) | ||||
4426 | return error("Invalid record"); | ||||
4427 | |||||
4428 | I = CatchReturnInst::Create(CatchPad, BB); | ||||
4429 | InstructionList.push_back(I); | ||||
4430 | break; | ||||
4431 | } | ||||
4432 | case bitc::FUNC_CODE_INST_CATCHSWITCH: { // CATCHSWITCH: [tok,num,(bb)*,bb?] | ||||
4433 | // We must have, at minimum, the outer scope and the number of arguments. | ||||
4434 | if (Record.size() < 2) | ||||
4435 | return error("Invalid record"); | ||||
4436 | |||||
4437 | unsigned Idx = 0; | ||||
4438 | |||||
4439 | Value *ParentPad = | ||||
4440 | getValue(Record, Idx++, NextValueNo, Type::getTokenTy(Context)); | ||||
4441 | |||||
4442 | unsigned NumHandlers = Record[Idx++]; | ||||
4443 | |||||
4444 | SmallVector<BasicBlock *, 2> Handlers; | ||||
4445 | for (unsigned Op = 0; Op != NumHandlers; ++Op) { | ||||
4446 | BasicBlock *BB = getBasicBlock(Record[Idx++]); | ||||
4447 | if (!BB) | ||||
4448 | return error("Invalid record"); | ||||
4449 | Handlers.push_back(BB); | ||||
4450 | } | ||||
4451 | |||||
4452 | BasicBlock *UnwindDest = nullptr; | ||||
4453 | if (Idx + 1 == Record.size()) { | ||||
4454 | UnwindDest = getBasicBlock(Record[Idx++]); | ||||
4455 | if (!UnwindDest) | ||||
4456 | return error("Invalid record"); | ||||
4457 | } | ||||
4458 | |||||
4459 | if (Record.size() != Idx) | ||||
4460 | return error("Invalid record"); | ||||
4461 | |||||
4462 | auto *CatchSwitch = | ||||
4463 | CatchSwitchInst::Create(ParentPad, UnwindDest, NumHandlers); | ||||
4464 | for (BasicBlock *Handler : Handlers) | ||||
4465 | CatchSwitch->addHandler(Handler); | ||||
4466 | I = CatchSwitch; | ||||
4467 | InstructionList.push_back(I); | ||||
4468 | break; | ||||
4469 | } | ||||
4470 | case bitc::FUNC_CODE_INST_CATCHPAD: | ||||
4471 | case bitc::FUNC_CODE_INST_CLEANUPPAD: { // [tok,num,(ty,val)*] | ||||
4472 | // We must have, at minimum, the outer scope and the number of arguments. | ||||
4473 | if (Record.size() < 2) | ||||
4474 | return error("Invalid record"); | ||||
4475 | |||||
4476 | unsigned Idx = 0; | ||||
4477 | |||||
4478 | Value *ParentPad = | ||||
4479 | getValue(Record, Idx++, NextValueNo, Type::getTokenTy(Context)); | ||||
4480 | |||||
4481 | unsigned NumArgOperands = Record[Idx++]; | ||||
4482 | |||||
4483 | SmallVector<Value *, 2> Args; | ||||
4484 | for (unsigned Op = 0; Op != NumArgOperands; ++Op) { | ||||
4485 | Value *Val; | ||||
4486 | if (getValueTypePair(Record, Idx, NextValueNo, Val)) | ||||
4487 | return error("Invalid record"); | ||||
4488 | Args.push_back(Val); | ||||
4489 | } | ||||
4490 | |||||
4491 | if (Record.size() != Idx) | ||||
4492 | return error("Invalid record"); | ||||
4493 | |||||
4494 | if (BitCode == bitc::FUNC_CODE_INST_CLEANUPPAD) | ||||
4495 | I = CleanupPadInst::Create(ParentPad, Args); | ||||
4496 | else | ||||
4497 | I = CatchPadInst::Create(ParentPad, Args); | ||||
4498 | InstructionList.push_back(I); | ||||
4499 | break; | ||||
4500 | } | ||||
4501 | case bitc::FUNC_CODE_INST_SWITCH: { // SWITCH: [opty, op0, op1, ...] | ||||
4502 | // Check magic | ||||
4503 | if ((Record[0] >> 16) == SWITCH_INST_MAGIC) { | ||||
4504 | // "New" SwitchInst format with case ranges. The changes to write this | ||||
4505 | // format were reverted but we still recognize bitcode that uses it. | ||||
4506 | // Hopefully someday we will have support for case ranges and can use | ||||
4507 | // this format again. | ||||
4508 | |||||
4509 | Type *OpTy = getTypeByID(Record[1]); | ||||
4510 | unsigned ValueBitWidth = cast<IntegerType>(OpTy)->getBitWidth(); | ||||
4511 | |||||
4512 | Value *Cond = getValue(Record, 2, NextValueNo, OpTy); | ||||
4513 | BasicBlock *Default = getBasicBlock(Record[3]); | ||||
4514 | if (!OpTy || !Cond || !Default) | ||||
4515 | return error("Invalid record"); | ||||
4516 | |||||
4517 | unsigned NumCases = Record[4]; | ||||
4518 | |||||
4519 | SwitchInst *SI = SwitchInst::Create(Cond, Default, NumCases); | ||||
4520 | InstructionList.push_back(SI); | ||||
4521 | |||||
4522 | unsigned CurIdx = 5; | ||||
4523 | for (unsigned i = 0; i != NumCases; ++i) { | ||||
4524 | SmallVector<ConstantInt*, 1> CaseVals; | ||||
4525 | unsigned NumItems = Record[CurIdx++]; | ||||
4526 | for (unsigned ci = 0; ci != NumItems; ++ci) { | ||||
4527 | bool isSingleNumber = Record[CurIdx++]; | ||||
4528 | |||||
4529 | APInt Low; | ||||
4530 | unsigned ActiveWords = 1; | ||||
4531 | if (ValueBitWidth > 64) | ||||
4532 | ActiveWords = Record[CurIdx++]; | ||||
4533 | Low = readWideAPInt(makeArrayRef(&Record[CurIdx], ActiveWords), | ||||
4534 | ValueBitWidth); | ||||
4535 | CurIdx += ActiveWords; | ||||
4536 | |||||
4537 | if (!isSingleNumber) { | ||||
4538 | ActiveWords = 1; | ||||
4539 | if (ValueBitWidth > 64) | ||||
4540 | ActiveWords = Record[CurIdx++]; | ||||
4541 | APInt High = readWideAPInt( | ||||
4542 | makeArrayRef(&Record[CurIdx], ActiveWords), ValueBitWidth); | ||||
4543 | CurIdx += ActiveWords; | ||||
4544 | |||||
4545 | // FIXME: It is not clear whether values in the range should be | ||||
4546 | // compared as signed or unsigned values. The partially | ||||
4547 | // implemented changes that used this format in the past used | ||||
4548 | // unsigned comparisons. | ||||
4549 | for ( ; Low.ule(High); ++Low) | ||||
4550 | CaseVals.push_back(ConstantInt::get(Context, Low)); | ||||
4551 | } else | ||||
4552 | CaseVals.push_back(ConstantInt::get(Context, Low)); | ||||
4553 | } | ||||
4554 | BasicBlock *DestBB = getBasicBlock(Record[CurIdx++]); | ||||
4555 | for (SmallVector<ConstantInt*, 1>::iterator cvi = CaseVals.begin(), | ||||
4556 | cve = CaseVals.end(); cvi != cve; ++cvi) | ||||
4557 | SI->addCase(*cvi, DestBB); | ||||
4558 | } | ||||
4559 | I = SI; | ||||
4560 | break; | ||||
4561 | } | ||||
4562 | |||||
4563 | // Old SwitchInst format without case ranges. | ||||
4564 | |||||
4565 | if (Record.size() < 3 || (Record.size() & 1) == 0) | ||||
4566 | return error("Invalid record"); | ||||
4567 | Type *OpTy = getTypeByID(Record[0]); | ||||
4568 | Value *Cond = getValue(Record, 1, NextValueNo, OpTy); | ||||
4569 | BasicBlock *Default = getBasicBlock(Record[2]); | ||||
4570 | if (!OpTy || !Cond || !Default) | ||||
4571 | return error("Invalid record"); | ||||
4572 | unsigned NumCases = (Record.size()-3)/2; | ||||
4573 | SwitchInst *SI = SwitchInst::Create(Cond, Default, NumCases); | ||||
4574 | InstructionList.push_back(SI); | ||||
4575 | for (unsigned i = 0, e = NumCases; i != e; ++i) { | ||||
4576 | ConstantInt *CaseVal = | ||||
4577 | dyn_cast_or_null<ConstantInt>(getFnValueByID(Record[3+i*2], OpTy)); | ||||
4578 | BasicBlock *DestBB = getBasicBlock(Record[1+3+i*2]); | ||||
4579 | if (!CaseVal || !DestBB) { | ||||
4580 | delete SI; | ||||
4581 | return error("Invalid record"); | ||||
4582 | } | ||||
4583 | SI->addCase(CaseVal, DestBB); | ||||
4584 | } | ||||
4585 | I = SI; | ||||
4586 | break; | ||||
4587 | } | ||||
4588 | case bitc::FUNC_CODE_INST_INDIRECTBR: { // INDIRECTBR: [opty, op0, op1, ...] | ||||
4589 | if (Record.size() < 2) | ||||
4590 | return error("Invalid record"); | ||||
4591 | Type *OpTy = getTypeByID(Record[0]); | ||||
4592 | Value *Address = getValue(Record, 1, NextValueNo, OpTy); | ||||
4593 | if (!OpTy || !Address) | ||||
4594 | return error("Invalid record"); | ||||
4595 | unsigned NumDests = Record.size()-2; | ||||
4596 | IndirectBrInst *IBI = IndirectBrInst::Create(Address, NumDests); | ||||
4597 | InstructionList.push_back(IBI); | ||||
4598 | for (unsigned i = 0, e = NumDests; i != e; ++i) { | ||||
4599 | if (BasicBlock *DestBB = getBasicBlock(Record[2+i])) { | ||||
4600 | IBI->addDestination(DestBB); | ||||
4601 | } else { | ||||
4602 | delete IBI; | ||||
4603 | return error("Invalid record"); | ||||
4604 | } | ||||
4605 | } | ||||
4606 | I = IBI; | ||||
4607 | break; | ||||
4608 | } | ||||
4609 | |||||
4610 | case bitc::FUNC_CODE_INST_INVOKE: { | ||||
4611 | // INVOKE: [attrs, cc, normBB, unwindBB, fnty, op0,op1,op2, ...] | ||||
4612 | if (Record.size() < 4) | ||||
4613 | return error("Invalid record"); | ||||
4614 | unsigned OpNum = 0; | ||||
4615 | AttributeList PAL = getAttributes(Record[OpNum++]); | ||||
4616 | unsigned CCInfo = Record[OpNum++]; | ||||
4617 | BasicBlock *NormalBB = getBasicBlock(Record[OpNum++]); | ||||
4618 | BasicBlock *UnwindBB = getBasicBlock(Record[OpNum++]); | ||||
4619 | |||||
4620 | FunctionType *FTy = nullptr; | ||||
4621 | if ((CCInfo >> 13) & 1) { | ||||
4622 | FTy = dyn_cast<FunctionType>(getTypeByID(Record[OpNum++])); | ||||
4623 | if (!FTy) | ||||
4624 | return error("Explicit invoke type is not a function type"); | ||||
4625 | } | ||||
4626 | |||||
4627 | Value *Callee; | ||||
4628 | if (getValueTypePair(Record, OpNum, NextValueNo, Callee)) | ||||
4629 | return error("Invalid record"); | ||||
4630 | |||||
4631 | PointerType *CalleeTy = dyn_cast<PointerType>(Callee->getType()); | ||||
4632 | if (!CalleeTy) | ||||
4633 | return error("Callee is not a pointer"); | ||||
4634 | if (!FTy) { | ||||
4635 | FTy = dyn_cast<FunctionType>( | ||||
4636 | cast<PointerType>(Callee->getType())->getElementType()); | ||||
4637 | if (!FTy) | ||||
4638 | return error("Callee is not of pointer to function type"); | ||||
4639 | } else if (cast<PointerType>(Callee->getType())->getElementType() != FTy) | ||||
4640 | return error("Explicit invoke type does not match pointee type of " | ||||
4641 | "callee operand"); | ||||
4642 | if (Record.size() < FTy->getNumParams() + OpNum) | ||||
4643 | return error("Insufficient operands to call"); | ||||
4644 | |||||
4645 | SmallVector<Value*, 16> Ops; | ||||
4646 | SmallVector<Type *, 16> ArgsTys; | ||||
4647 | for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i, ++OpNum) { | ||||
4648 | Ops.push_back(getValue(Record, OpNum, NextValueNo, | ||||
4649 | FTy->getParamType(i))); | ||||
4650 | ArgsTys.push_back(FTy->getParamType(i)); | ||||
4651 | if (!Ops.back()) | ||||
4652 | return error("Invalid record"); | ||||
4653 | } | ||||
4654 | |||||
4655 | if (!FTy->isVarArg()) { | ||||
4656 | if (Record.size() != OpNum) | ||||
4657 | return error("Invalid record"); | ||||
4658 | } else { | ||||
4659 | // Read type/value pairs for varargs params. | ||||
4660 | while (OpNum != Record.size()) { | ||||
4661 | Value *Op; | ||||
4662 | if (getValueTypePair(Record, OpNum, NextValueNo, Op)) | ||||
4663 | return error("Invalid record"); | ||||
4664 | Ops.push_back(Op); | ||||
4665 | ArgsTys.push_back(Op->getType()); | ||||
4666 | } | ||||
4667 | } | ||||
4668 | |||||
4669 | I = InvokeInst::Create(FTy, Callee, NormalBB, UnwindBB, Ops, | ||||
4670 | OperandBundles); | ||||
4671 | OperandBundles.clear(); | ||||
4672 | InstructionList.push_back(I); | ||||
4673 | cast<InvokeInst>(I)->setCallingConv( | ||||
4674 | static_cast<CallingConv::ID>(CallingConv::MaxID & CCInfo)); | ||||
4675 | cast<InvokeInst>(I)->setAttributes(PAL); | ||||
4676 | propagateByValSRetTypes(cast<CallBase>(I), ArgsTys); | ||||
4677 | |||||
4678 | break; | ||||
4679 | } | ||||
4680 | case bitc::FUNC_CODE_INST_RESUME: { // RESUME: [opval] | ||||
4681 | unsigned Idx = 0; | ||||
4682 | Value *Val = nullptr; | ||||
4683 | if (getValueTypePair(Record, Idx, NextValueNo, Val)) | ||||
4684 | return error("Invalid record"); | ||||
4685 | I = ResumeInst::Create(Val); | ||||
4686 | InstructionList.push_back(I); | ||||
4687 | break; | ||||
4688 | } | ||||
4689 | case bitc::FUNC_CODE_INST_CALLBR: { | ||||
4690 | // CALLBR: [attr, cc, norm, transfs, fty, fnid, args] | ||||
4691 | unsigned OpNum = 0; | ||||
4692 | AttributeList PAL = getAttributes(Record[OpNum++]); | ||||
4693 | unsigned CCInfo = Record[OpNum++]; | ||||
4694 | |||||
4695 | BasicBlock *DefaultDest = getBasicBlock(Record[OpNum++]); | ||||
4696 | unsigned NumIndirectDests = Record[OpNum++]; | ||||
4697 | SmallVector<BasicBlock *, 16> IndirectDests; | ||||
4698 | for (unsigned i = 0, e = NumIndirectDests; i != e; ++i) | ||||
4699 | IndirectDests.push_back(getBasicBlock(Record[OpNum++])); | ||||
4700 | |||||
4701 | FunctionType *FTy = nullptr; | ||||
4702 | if ((CCInfo >> bitc::CALL_EXPLICIT_TYPE) & 1) { | ||||
4703 | FTy = dyn_cast<FunctionType>(getTypeByID(Record[OpNum++])); | ||||
4704 | if (!FTy) | ||||
4705 | return error("Explicit call type is not a function type"); | ||||
4706 | } | ||||
4707 | |||||
4708 | Value *Callee; | ||||
4709 | if (getValueTypePair(Record, OpNum, NextValueNo, Callee)) | ||||
4710 | return error("Invalid record"); | ||||
4711 | |||||
4712 | PointerType *OpTy = dyn_cast<PointerType>(Callee->getType()); | ||||
4713 | if (!OpTy) | ||||
4714 | return error("Callee is not a pointer type"); | ||||
4715 | if (!FTy) { | ||||
4716 | FTy = dyn_cast<FunctionType>( | ||||
4717 | cast<PointerType>(Callee->getType())->getElementType()); | ||||
4718 | if (!FTy) | ||||
4719 | return error("Callee is not of pointer to function type"); | ||||
4720 | } else if (cast<PointerType>(Callee->getType())->getElementType() != FTy) | ||||
4721 | return error("Explicit call type does not match pointee type of " | ||||
4722 | "callee operand"); | ||||
4723 | if (Record.size() < FTy->getNumParams() + OpNum) | ||||
4724 | return error("Insufficient operands to call"); | ||||
4725 | |||||
4726 | SmallVector<Value*, 16> Args; | ||||
4727 | // Read the fixed params. | ||||
4728 | for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i, ++OpNum) { | ||||
4729 | if (FTy->getParamType(i)->isLabelTy()) | ||||
4730 | Args.push_back(getBasicBlock(Record[OpNum])); | ||||
4731 | else | ||||
4732 | Args.push_back(getValue(Record, OpNum, NextValueNo, | ||||
4733 | FTy->getParamType(i))); | ||||
4734 | if (!Args.back()) | ||||
4735 | return error("Invalid record"); | ||||
4736 | } | ||||
4737 | |||||
4738 | // Read type/value pairs for varargs params. | ||||
4739 | if (!FTy->isVarArg()) { | ||||
4740 | if (OpNum != Record.size()) | ||||
4741 | return error("Invalid record"); | ||||
4742 | } else { | ||||
4743 | while (OpNum != Record.size()) { | ||||
4744 | Value *Op; | ||||
4745 | if (getValueTypePair(Record, OpNum, NextValueNo, Op)) | ||||
4746 | return error("Invalid record"); | ||||
4747 | Args.push_back(Op); | ||||
4748 | } | ||||
4749 | } | ||||
4750 | |||||
4751 | I = CallBrInst::Create(FTy, Callee, DefaultDest, IndirectDests, Args, | ||||
4752 | OperandBundles); | ||||
4753 | OperandBundles.clear(); | ||||
4754 | InstructionList.push_back(I); | ||||
4755 | cast<CallBrInst>(I)->setCallingConv( | ||||
4756 | static_cast<CallingConv::ID>((0x7ff & CCInfo) >> bitc::CALL_CCONV)); | ||||
4757 | cast<CallBrInst>(I)->setAttributes(PAL); | ||||
4758 | break; | ||||
4759 | } | ||||
4760 | case bitc::FUNC_CODE_INST_UNREACHABLE: // UNREACHABLE | ||||
4761 | I = new UnreachableInst(Context); | ||||
4762 | InstructionList.push_back(I); | ||||
4763 | break; | ||||
4764 | case bitc::FUNC_CODE_INST_PHI: { // PHI: [ty, val0,bb0, ...] | ||||
4765 | if (Record.empty()) | ||||
4766 | return error("Invalid record"); | ||||
4767 | // The first record specifies the type. | ||||
4768 | Type *Ty = getTypeByID(Record[0]); | ||||
4769 | if (!Ty) | ||||
4770 | return error("Invalid record"); | ||||
4771 | |||||
4772 | // Phi arguments are pairs of records of [value, basic block]. | ||||
4773 | // There is an optional final record for fast-math-flags if this phi has a | ||||
4774 | // floating-point type. | ||||
4775 | size_t NumArgs = (Record.size() - 1) / 2; | ||||
4776 | PHINode *PN = PHINode::Create(Ty, NumArgs); | ||||
4777 | if ((Record.size() - 1) % 2 == 1 && !isa<FPMathOperator>(PN)) | ||||
4778 | return error("Invalid record"); | ||||
4779 | InstructionList.push_back(PN); | ||||
4780 | |||||
4781 | for (unsigned i = 0; i != NumArgs; i++) { | ||||
4782 | Value *V; | ||||
4783 | // With the new function encoding, it is possible that operands have | ||||
4784 | // negative IDs (for forward references). Use a signed VBR | ||||
4785 | // representation to keep the encoding small. | ||||
4786 | if (UseRelativeIDs) | ||||
4787 | V = getValueSigned(Record, i * 2 + 1, NextValueNo, Ty); | ||||
4788 | else | ||||
4789 | V = getValue(Record, i * 2 + 1, NextValueNo, Ty); | ||||
4790 | BasicBlock *BB = getBasicBlock(Record[i * 2 + 2]); | ||||
4791 | if (!V || !BB) | ||||
4792 | return error("Invalid record"); | ||||
4793 | PN->addIncoming(V, BB); | ||||
4794 | } | ||||
4795 | I = PN; | ||||
4796 | |||||
4797 | // If there are an even number of records, the final record must be FMF. | ||||
4798 | if (Record.size() % 2 == 0) { | ||||
4799 | assert(isa<FPMathOperator>(I) && "Unexpected phi type")(static_cast <bool> (isa<FPMathOperator>(I) && "Unexpected phi type") ? void (0) : __assert_fail ("isa<FPMathOperator>(I) && \"Unexpected phi type\"" , "/build/llvm-toolchain-snapshot-13~++20210621111111+acefe0eaaf82/llvm/lib/Bitcode/Reader/BitcodeReader.cpp" , 4799, __extension__ __PRETTY_FUNCTION__)); | ||||
4800 | FastMathFlags FMF = getDecodedFastMathFlags(Record[Record.size() - 1]); | ||||
4801 | if (FMF.any()) | ||||
4802 | I->setFastMathFlags(FMF); | ||||
4803 | } | ||||
4804 | |||||
4805 | break; | ||||
4806 | } | ||||
4807 | |||||
4808 | case bitc::FUNC_CODE_INST_LANDINGPAD: | ||||
4809 | case bitc::FUNC_CODE_INST_LANDINGPAD_OLD: { | ||||
4810 | // LANDINGPAD: [ty, val, val, num, (id0,val0 ...)?] | ||||
4811 | unsigned Idx = 0; | ||||
4812 | if (BitCode == bitc::FUNC_CODE_INST_LANDINGPAD) { | ||||
4813 | if (Record.size() < 3) | ||||
4814 | return error("Invalid record"); | ||||
4815 | } else { | ||||
4816 | assert(BitCode == bitc::FUNC_CODE_INST_LANDINGPAD_OLD)(static_cast <bool> (BitCode == bitc::FUNC_CODE_INST_LANDINGPAD_OLD ) ? void (0) : __assert_fail ("BitCode == bitc::FUNC_CODE_INST_LANDINGPAD_OLD" , "/build/llvm-toolchain-snapshot-13~++20210621111111+acefe0eaaf82/llvm/lib/Bitcode/Reader/BitcodeReader.cpp" , 4816, __extension__ __PRETTY_FUNCTION__)); | ||||
4817 | if (Record.size() < 4) | ||||
4818 | return error("Invalid record"); | ||||
4819 | } | ||||
4820 | Type *Ty = getTypeByID(Record[Idx++]); | ||||
4821 | if (!Ty) | ||||
4822 | return error("Invalid record"); | ||||
4823 | if (BitCode == bitc::FUNC_CODE_INST_LANDINGPAD_OLD) { | ||||
4824 | Value *PersFn = nullptr; | ||||
4825 | if (getValueTypePair(Record, Idx, NextValueNo, PersFn)) | ||||
4826 | return error("Invalid record"); | ||||
4827 | |||||
4828 | if (!F->hasPersonalityFn()) | ||||
4829 | F->setPersonalityFn(cast<Constant>(PersFn)); | ||||
4830 | else if (F->getPersonalityFn() != cast<Constant>(PersFn)) | ||||
4831 | return error("Personality function mismatch"); | ||||
4832 | } | ||||
4833 | |||||
4834 | bool IsCleanup = !!Record[Idx++]; | ||||
4835 | unsigned NumClauses = Record[Idx++]; | ||||
4836 | LandingPadInst *LP = LandingPadInst::Create(Ty, NumClauses); | ||||
4837 | LP->setCleanup(IsCleanup); | ||||
4838 | for (unsigned J = 0; J != NumClauses; ++J) { | ||||
4839 | LandingPadInst::ClauseType CT = | ||||
4840 | LandingPadInst::ClauseType(Record[Idx++]); (void)CT; | ||||
4841 | Value *Val; | ||||
4842 | |||||
4843 | if (getValueTypePair(Record, Idx, NextValueNo, Val)) { | ||||
4844 | delete LP; | ||||
4845 | return error("Invalid record"); | ||||
4846 | } | ||||
4847 | |||||
4848 | assert((CT != LandingPadInst::Catch ||(static_cast <bool> ((CT != LandingPadInst::Catch || !isa <ArrayType>(Val->getType())) && "Catch clause has a invalid type!" ) ? void (0) : __assert_fail ("(CT != LandingPadInst::Catch || !isa<ArrayType>(Val->getType())) && \"Catch clause has a invalid type!\"" , "/build/llvm-toolchain-snapshot-13~++20210621111111+acefe0eaaf82/llvm/lib/Bitcode/Reader/BitcodeReader.cpp" , 4850, __extension__ __PRETTY_FUNCTION__)) | ||||
4849 | !isa<ArrayType>(Val->getType())) &&(static_cast <bool> ((CT != LandingPadInst::Catch || !isa <ArrayType>(Val->getType())) && "Catch clause has a invalid type!" ) ? void (0) : __assert_fail ("(CT != LandingPadInst::Catch || !isa<ArrayType>(Val->getType())) && \"Catch clause has a invalid type!\"" , "/build/llvm-toolchain-snapshot-13~++20210621111111+acefe0eaaf82/llvm/lib/Bitcode/Reader/BitcodeReader.cpp" , 4850, __extension__ __PRETTY_FUNCTION__)) | ||||
4850 | "Catch clause has a invalid type!")(static_cast <bool> ((CT != LandingPadInst::Catch || !isa <ArrayType>(Val->getType())) && "Catch clause has a invalid type!" ) ? void (0) : __assert_fail ("(CT != LandingPadInst::Catch || !isa<ArrayType>(Val->getType())) && \"Catch clause has a invalid type!\"" , "/build/llvm-toolchain-snapshot-13~++20210621111111+acefe0eaaf82/llvm/lib/Bitcode/Reader/BitcodeReader.cpp" , 4850, __extension__ __PRETTY_FUNCTION__)); | ||||
4851 | assert((CT != LandingPadInst::Filter ||(static_cast <bool> ((CT != LandingPadInst::Filter || isa <ArrayType>(Val->getType())) && "Filter clause has invalid type!" ) ? void (0) : __assert_fail ("(CT != LandingPadInst::Filter || isa<ArrayType>(Val->getType())) && \"Filter clause has invalid type!\"" , "/build/llvm-toolchain-snapshot-13~++20210621111111+acefe0eaaf82/llvm/lib/Bitcode/Reader/BitcodeReader.cpp" , 4853, __extension__ __PRETTY_FUNCTION__)) | ||||
4852 | isa<ArrayType>(Val->getType())) &&(static_cast <bool> ((CT != LandingPadInst::Filter || isa <ArrayType>(Val->getType())) && "Filter clause has invalid type!" ) ? void (0) : __assert_fail ("(CT != LandingPadInst::Filter || isa<ArrayType>(Val->getType())) && \"Filter clause has invalid type!\"" , "/build/llvm-toolchain-snapshot-13~++20210621111111+acefe0eaaf82/llvm/lib/Bitcode/Reader/BitcodeReader.cpp" , 4853, __extension__ __PRETTY_FUNCTION__)) | ||||
4853 | "Filter clause has invalid type!")(static_cast <bool> ((CT != LandingPadInst::Filter || isa <ArrayType>(Val->getType())) && "Filter clause has invalid type!" ) ? void (0) : __assert_fail ("(CT != LandingPadInst::Filter || isa<ArrayType>(Val->getType())) && \"Filter clause has invalid type!\"" , "/build/llvm-toolchain-snapshot-13~++20210621111111+acefe0eaaf82/llvm/lib/Bitcode/Reader/BitcodeReader.cpp" , 4853, __extension__ __PRETTY_FUNCTION__)); | ||||
4854 | LP->addClause(cast<Constant>(Val)); | ||||
4855 | } | ||||
4856 | |||||
4857 | I = LP; | ||||
4858 | InstructionList.push_back(I); | ||||
4859 | break; | ||||
4860 | } | ||||
4861 | |||||
4862 | case bitc::FUNC_CODE_INST_ALLOCA: { // ALLOCA: [instty, opty, op, align] | ||||
4863 | if (Record.size() != 4) | ||||
4864 | return error("Invalid record"); | ||||
4865 | using APV = AllocaPackedValues; | ||||
4866 | const uint64_t Rec = Record[3]; | ||||
4867 | const bool InAlloca = Bitfield::get<APV::UsedWithInAlloca>(Rec); | ||||
4868 | const bool SwiftError = Bitfield::get<APV::SwiftError>(Rec); | ||||
4869 | Type *Ty = getTypeByID(Record[0]); | ||||
4870 | if (!Bitfield::get<APV::ExplicitType>(Rec)) { | ||||
4871 | auto *PTy = dyn_cast_or_null<PointerType>(Ty); | ||||
4872 | if (!PTy) | ||||
4873 | return error("Old-style alloca with a non-pointer type"); | ||||
4874 | Ty = PTy->getElementType(); | ||||
4875 | } | ||||
4876 | Type *OpTy = getTypeByID(Record[1]); | ||||
4877 | Value *Size = getFnValueByID(Record[2], OpTy); | ||||
4878 | MaybeAlign Align; | ||||
4879 | if (Error Err = | ||||
4880 | parseAlignmentValue(Bitfield::get<APV::Align>(Rec), Align)) { | ||||
4881 | return Err; | ||||
4882 | } | ||||
4883 | if (!Ty || !Size) | ||||
4884 | return error("Invalid record"); | ||||
4885 | |||||
4886 | // FIXME: Make this an optional field. | ||||
4887 | const DataLayout &DL = TheModule->getDataLayout(); | ||||
4888 | unsigned AS = DL.getAllocaAddrSpace(); | ||||
4889 | |||||
4890 | SmallPtrSet<Type *, 4> Visited; | ||||
4891 | if (!Align && !Ty->isSized(&Visited)) | ||||
4892 | return error("alloca of unsized type"); | ||||
4893 | if (!Align) | ||||
4894 | Align = DL.getPrefTypeAlign(Ty); | ||||
4895 | |||||
4896 | AllocaInst *AI = new AllocaInst(Ty, AS, Size, *Align); | ||||
4897 | AI->setUsedWithInAlloca(InAlloca); | ||||
4898 | AI->setSwiftError(SwiftError); | ||||
4899 | I = AI; | ||||
4900 | InstructionList.push_back(I); | ||||
4901 | break; | ||||
4902 | } | ||||
4903 | case bitc::FUNC_CODE_INST_LOAD: { // LOAD: [opty, op, align, vol] | ||||
4904 | unsigned OpNum = 0; | ||||
4905 | Value *Op; | ||||
4906 | if (getValueTypePair(Record, OpNum, NextValueNo, Op) || | ||||
4907 | (OpNum + 2 != Record.size() && OpNum + 3 != Record.size())) | ||||
4908 | return error("Invalid record"); | ||||
4909 | |||||
4910 | if (!isa<PointerType>(Op->getType())) | ||||
4911 | return error("Load operand is not a pointer type"); | ||||
4912 | |||||
4913 | Type *Ty = nullptr; | ||||
4914 | if (OpNum + 3 == Record.size()) { | ||||
4915 | Ty = getTypeByID(Record[OpNum++]); | ||||
4916 | } else { | ||||
4917 | Ty = cast<PointerType>(Op->getType())->getElementType(); | ||||
4918 | } | ||||
4919 | |||||
4920 | if (Error Err = typeCheckLoadStoreInst(Ty, Op->getType())) | ||||
4921 | return Err; | ||||
4922 | |||||
4923 | MaybeAlign Align; | ||||
4924 | if (Error Err = parseAlignmentValue(Record[OpNum], Align)) | ||||
4925 | return Err; | ||||
4926 | SmallPtrSet<Type *, 4> Visited; | ||||
4927 | if (!Align && !Ty->isSized(&Visited)) | ||||
4928 | return error("load of unsized type"); | ||||
4929 | if (!Align) | ||||
4930 | Align = TheModule->getDataLayout().getABITypeAlign(Ty); | ||||
4931 | I = new LoadInst(Ty, Op, "", Record[OpNum + 1], *Align); | ||||
4932 | InstructionList.push_back(I); | ||||
4933 | break; | ||||
4934 | } | ||||
4935 | case bitc::FUNC_CODE_INST_LOADATOMIC: { | ||||
4936 | // LOADATOMIC: [opty, op, align, vol, ordering, ssid] | ||||
4937 | unsigned OpNum = 0; | ||||
4938 | Value *Op; | ||||
4939 | if (getValueTypePair(Record, OpNum, NextValueNo, Op) || | ||||
4940 | (OpNum + 4 != Record.size() && OpNum + 5 != Record.size())) | ||||
4941 | return error("Invalid record"); | ||||
4942 | |||||
4943 | if (!isa<PointerType>(Op->getType())) | ||||
4944 | return error("Load operand is not a pointer type"); | ||||
4945 | |||||
4946 | Type *Ty = nullptr; | ||||
4947 | if (OpNum + 5 == Record.size()) { | ||||
4948 | Ty = getTypeByID(Record[OpNum++]); | ||||
4949 | } else { | ||||
4950 | Ty = cast<PointerType>(Op->getType())->getElementType(); | ||||
4951 | } | ||||
4952 | |||||
4953 | if (Error Err = typeCheckLoadStoreInst(Ty, Op->getType())) | ||||
4954 | return Err; | ||||
4955 | |||||
4956 | AtomicOrdering Ordering = getDecodedOrdering(Record[OpNum + 2]); | ||||
4957 | if (Ordering == AtomicOrdering::NotAtomic || | ||||
4958 | Ordering == AtomicOrdering::Release || | ||||
4959 | Ordering == AtomicOrdering::AcquireRelease) | ||||
4960 | return error("Invalid record"); | ||||
4961 | if (Ordering != AtomicOrdering::NotAtomic && Record[OpNum] == 0) | ||||
4962 | return error("Invalid record"); | ||||
4963 | SyncScope::ID SSID = getDecodedSyncScopeID(Record[OpNum + 3]); | ||||
4964 | |||||
4965 | MaybeAlign Align; | ||||
4966 | if (Error Err = parseAlignmentValue(Record[OpNum], Align)) | ||||
4967 | return Err; | ||||
4968 | if (!Align) | ||||
4969 | return error("Alignment missing from atomic load"); | ||||
4970 | I = new LoadInst(Ty, Op, "", Record[OpNum + 1], *Align, Ordering, SSID); | ||||
4971 | InstructionList.push_back(I); | ||||
4972 | break; | ||||
4973 | } | ||||
4974 | case bitc::FUNC_CODE_INST_STORE: | ||||
4975 | case bitc::FUNC_CODE_INST_STORE_OLD: { // STORE2:[ptrty, ptr, val, align, vol] | ||||
4976 | unsigned OpNum = 0; | ||||
4977 | Value *Val, *Ptr; | ||||
4978 | if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) || | ||||
4979 | (BitCode == bitc::FUNC_CODE_INST_STORE | ||||
4980 | ? getValueTypePair(Record, OpNum, NextValueNo, Val) | ||||
4981 | : popValue(Record, OpNum, NextValueNo, | ||||
4982 | cast<PointerType>(Ptr->getType())->getElementType(), | ||||
4983 | Val)) || | ||||
4984 | OpNum + 2 != Record.size()) | ||||
4985 | return error("Invalid record"); | ||||
4986 | |||||
4987 | if (Error Err = typeCheckLoadStoreInst(Val->getType(), Ptr->getType())) | ||||
4988 | return Err; | ||||
4989 | MaybeAlign Align; | ||||
4990 | if (Error Err = parseAlignmentValue(Record[OpNum], Align)) | ||||
4991 | return Err; | ||||
4992 | SmallPtrSet<Type *, 4> Visited; | ||||
4993 | if (!Align && !Val->getType()->isSized(&Visited)) | ||||
4994 | return error("store of unsized type"); | ||||
4995 | if (!Align) | ||||
4996 | Align = TheModule->getDataLayout().getABITypeAlign(Val->getType()); | ||||
4997 | I = new StoreInst(Val, Ptr, Record[OpNum + 1], *Align); | ||||
4998 | InstructionList.push_back(I); | ||||
4999 | break; | ||||
5000 | } | ||||
5001 | case bitc::FUNC_CODE_INST_STOREATOMIC: | ||||
5002 | case bitc::FUNC_CODE_INST_STOREATOMIC_OLD: { | ||||
5003 | // STOREATOMIC: [ptrty, ptr, val, align, vol, ordering, ssid] | ||||
5004 | unsigned OpNum = 0; | ||||
5005 | Value *Val, *Ptr; | ||||
5006 | if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) || | ||||
5007 | !isa<PointerType>(Ptr->getType()) || | ||||
5008 | (BitCode == bitc::FUNC_CODE_INST_STOREATOMIC | ||||
5009 | ? getValueTypePair(Record, OpNum, NextValueNo, Val) | ||||
5010 | : popValue(Record, OpNum, NextValueNo, | ||||
5011 | cast<PointerType>(Ptr->getType())->getElementType(), | ||||
5012 | Val)) || | ||||
5013 | OpNum + 4 != Record.size()) | ||||
5014 | return error("Invalid record"); | ||||
5015 | |||||
5016 | if (Error Err = typeCheckLoadStoreInst(Val->getType(), Ptr->getType())) | ||||
5017 | return Err; | ||||
5018 | AtomicOrdering Ordering = getDecodedOrdering(Record[OpNum + 2]); | ||||
5019 | if (Ordering == AtomicOrdering::NotAtomic || | ||||
5020 | Ordering == AtomicOrdering::Acquire || | ||||
5021 | Ordering == AtomicOrdering::AcquireRelease) | ||||
5022 | return error("Invalid record"); | ||||
5023 | SyncScope::ID SSID = getDecodedSyncScopeID(Record[OpNum + 3]); | ||||
5024 | if (Ordering != AtomicOrdering::NotAtomic && Record[OpNum] == 0) | ||||
5025 | return error("Invalid record"); | ||||
5026 | |||||
5027 | MaybeAlign Align; | ||||
5028 | if (Error Err = parseAlignmentValue(Record[OpNum], Align)) | ||||
5029 | return Err; | ||||
5030 | if (!Align) | ||||
5031 | return error("Alignment missing from atomic store"); | ||||
5032 | I = new StoreInst(Val, Ptr, Record[OpNum + 1], *Align, Ordering, SSID); | ||||
5033 | InstructionList.push_back(I); | ||||
5034 | break; | ||||
5035 | } | ||||
5036 | case bitc::FUNC_CODE_INST_CMPXCHG_OLD: { | ||||
5037 | // CMPXCHG_OLD: [ptrty, ptr, cmp, val, vol, ordering, synchscope, | ||||
5038 | // failure_ordering?, weak?] | ||||
5039 | const size_t NumRecords = Record.size(); | ||||
5040 | unsigned OpNum = 0; | ||||
5041 | Value *Ptr = nullptr; | ||||
5042 | if (getValueTypePair(Record, OpNum, NextValueNo, Ptr)) | ||||
5043 | return error("Invalid record"); | ||||
5044 | |||||
5045 | if (!isa<PointerType>(Ptr->getType())) | ||||
5046 | return error("Cmpxchg operand is not a pointer type"); | ||||
5047 | |||||
5048 | Value *Cmp = nullptr; | ||||
5049 | if (popValue(Record, OpNum, NextValueNo, | ||||
5050 | cast<PointerType>(Ptr->getType())->getPointerElementType(), | ||||
5051 | Cmp)) | ||||
5052 | return error("Invalid record"); | ||||
5053 | |||||
5054 | Value *New = nullptr; | ||||
5055 | if (popValue(Record, OpNum, NextValueNo, Cmp->getType(), New) || | ||||
5056 | NumRecords < OpNum + 3 || NumRecords > OpNum + 5) | ||||
5057 | return error("Invalid record"); | ||||
5058 | |||||
5059 | const AtomicOrdering SuccessOrdering = | ||||
5060 | getDecodedOrdering(Record[OpNum + 1]); | ||||
5061 | if (SuccessOrdering == AtomicOrdering::NotAtomic || | ||||
5062 | SuccessOrdering == AtomicOrdering::Unordered) | ||||
5063 | return error("Invalid record"); | ||||
5064 | |||||
5065 | const SyncScope::ID SSID = getDecodedSyncScopeID(Record[OpNum + 2]); | ||||
5066 | |||||
5067 | if (Error Err = typeCheckLoadStoreInst(Cmp->getType(), Ptr->getType())) | ||||
5068 | return Err; | ||||
5069 | |||||
5070 | const AtomicOrdering FailureOrdering = | ||||
5071 | NumRecords < 7 | ||||
5072 | ? AtomicCmpXchgInst::getStrongestFailureOrdering(SuccessOrdering) | ||||
5073 | : getDecodedOrdering(Record[OpNum + 3]); | ||||
5074 | |||||
5075 | if (FailureOrdering == AtomicOrdering::NotAtomic || | ||||
5076 | FailureOrdering == AtomicOrdering::Unordered) | ||||
5077 | return error("Invalid record"); | ||||
5078 | |||||
5079 | const Align Alignment( | ||||
5080 | TheModule->getDataLayout().getTypeStoreSize(Cmp->getType())); | ||||
5081 | |||||
5082 | I = new AtomicCmpXchgInst(Ptr, Cmp, New, Alignment, SuccessOrdering, | ||||
5083 | FailureOrdering, SSID); | ||||
5084 | cast<AtomicCmpXchgInst>(I)->setVolatile(Record[OpNum]); | ||||
5085 | |||||
5086 | if (NumRecords < 8) { | ||||
5087 | // Before weak cmpxchgs existed, the instruction simply returned the | ||||
5088 | // value loaded from memory, so bitcode files from that era will be | ||||
5089 | // expecting the first component of a modern cmpxchg. | ||||
5090 | CurBB->getInstList().push_back(I); | ||||
5091 | I = ExtractValueInst::Create(I, 0); | ||||
5092 | } else { | ||||
5093 | cast<AtomicCmpXchgInst>(I)->setWeak(Record[OpNum + 4]); | ||||
5094 | } | ||||
5095 | |||||
5096 | InstructionList.push_back(I); | ||||
5097 | break; | ||||
5098 | } | ||||
5099 | case bitc::FUNC_CODE_INST_CMPXCHG: { | ||||
5100 | // CMPXCHG: [ptrty, ptr, cmp, val, vol, success_ordering, synchscope, | ||||
5101 | // failure_ordering, weak, align?] | ||||
5102 | const size_t NumRecords = Record.size(); | ||||
5103 | unsigned OpNum = 0; | ||||
5104 | Value *Ptr = nullptr; | ||||
5105 | if (getValueTypePair(Record, OpNum, NextValueNo, Ptr)) | ||||
5106 | return error("Invalid record"); | ||||
5107 | |||||
5108 | if (!isa<PointerType>(Ptr->getType())) | ||||
5109 | return error("Cmpxchg operand is not a pointer type"); | ||||
5110 | |||||
5111 | Value *Cmp = nullptr; | ||||
5112 | if (getValueTypePair(Record, OpNum, NextValueNo, Cmp)) | ||||
5113 | return error("Invalid record"); | ||||
5114 | |||||
5115 | Value *Val = nullptr; | ||||
5116 | if (popValue(Record, OpNum, NextValueNo, Cmp->getType(), Val)) | ||||
5117 | return error("Invalid record"); | ||||
5118 | |||||
5119 | if (NumRecords < OpNum + 3 || NumRecords > OpNum + 6) | ||||
5120 | return error("Invalid record"); | ||||
5121 | |||||
5122 | const bool IsVol = Record[OpNum]; | ||||
5123 | |||||
5124 | const AtomicOrdering SuccessOrdering = | ||||
5125 | getDecodedOrdering(Record[OpNum + 1]); | ||||
5126 | if (!AtomicCmpXchgInst::isValidSuccessOrdering(SuccessOrdering)) | ||||
5127 | return error("Invalid cmpxchg success ordering"); | ||||
5128 | |||||
5129 | const SyncScope::ID SSID = getDecodedSyncScopeID(Record[OpNum + 2]); | ||||
5130 | |||||
5131 | if (Error Err = typeCheckLoadStoreInst(Cmp->getType(), Ptr->getType())) | ||||
5132 | return Err; | ||||
5133 | |||||
5134 | const AtomicOrdering FailureOrdering = | ||||
5135 | getDecodedOrdering(Record[OpNum + 3]); | ||||
5136 | if (!AtomicCmpXchgInst::isValidFailureOrdering(FailureOrdering)) | ||||
5137 | return error("Invalid cmpxchg failure ordering"); | ||||
5138 | |||||
5139 | const bool IsWeak = Record[OpNum + 4]; | ||||
5140 | |||||
5141 | MaybeAlign Alignment; | ||||
5142 | |||||
5143 | if (NumRecords == (OpNum + 6)) { | ||||
5144 | if (Error Err = parseAlignmentValue(Record[OpNum + 5], Alignment)) | ||||
5145 | return Err; | ||||
5146 | } | ||||
5147 | if (!Alignment) | ||||
5148 | Alignment = | ||||
5149 | Align(TheModule->getDataLayout().getTypeStoreSize(Cmp->getType())); | ||||
5150 | |||||
5151 | I = new AtomicCmpXchgInst(Ptr, Cmp, Val, *Alignment, SuccessOrdering, | ||||
5152 | FailureOrdering, SSID); | ||||
5153 | cast<AtomicCmpXchgInst>(I)->setVolatile(IsVol); | ||||
5154 | cast<AtomicCmpXchgInst>(I)->setWeak(IsWeak); | ||||
5155 | |||||
5156 | InstructionList.push_back(I); | ||||
5157 | break; | ||||
5158 | } | ||||
5159 | case bitc::FUNC_CODE_INST_ATOMICRMW_OLD: | ||||
5160 | case bitc::FUNC_CODE_INST_ATOMICRMW: { | ||||
5161 | // ATOMICRMW_OLD: [ptrty, ptr, val, op, vol, ordering, ssid, align?] | ||||
5162 | // ATOMICRMW: [ptrty, ptr, valty, val, op, vol, ordering, ssid, align?] | ||||
5163 | const size_t NumRecords = Record.size(); | ||||
5164 | unsigned OpNum = 0; | ||||
5165 | |||||
5166 | Value *Ptr = nullptr; | ||||
5167 | if (getValueTypePair(Record, OpNum, NextValueNo, Ptr)) | ||||
5168 | return error("Invalid record"); | ||||
5169 | |||||
5170 | if (!isa<PointerType>(Ptr->getType())) | ||||
5171 | return error("Invalid record"); | ||||
5172 | |||||
5173 | Value *Val = nullptr; | ||||
5174 | if (BitCode == bitc::FUNC_CODE_INST_ATOMICRMW_OLD) { | ||||
5175 | if (popValue(Record, OpNum, NextValueNo, | ||||
5176 | cast<PointerType>(Ptr->getType())->getPointerElementType(), | ||||
5177 | Val)) | ||||
5178 | return error("Invalid record"); | ||||
5179 | } else { | ||||
5180 | if (getValueTypePair(Record, OpNum, NextValueNo, Val)) | ||||
5181 | return error("Invalid record"); | ||||
5182 | } | ||||
5183 | |||||
5184 | if (!(NumRecords == (OpNum + 4) || NumRecords == (OpNum + 5))) | ||||
5185 | return error("Invalid record"); | ||||
5186 | |||||
5187 | const AtomicRMWInst::BinOp Operation = | ||||
5188 | getDecodedRMWOperation(Record[OpNum]); | ||||
5189 | if (Operation < AtomicRMWInst::FIRST_BINOP || | ||||
5190 | Operation > AtomicRMWInst::LAST_BINOP) | ||||
5191 | return error("Invalid record"); | ||||
5192 | |||||
5193 | const bool IsVol = Record[OpNum + 1]; | ||||
5194 | |||||
5195 | const AtomicOrdering Ordering = getDecodedOrdering(Record[OpNum + 2]); | ||||
5196 | if (Ordering == AtomicOrdering::NotAtomic || | ||||
5197 | Ordering == AtomicOrdering::Unordered) | ||||
5198 | return error("Invalid record"); | ||||
5199 | |||||
5200 | const SyncScope::ID SSID = getDecodedSyncScopeID(Record[OpNum + 3]); | ||||
5201 | |||||
5202 | MaybeAlign Alignment; | ||||
5203 | |||||
5204 | if (NumRecords == (OpNum + 5)) { | ||||
5205 | if (Error Err = parseAlignmentValue(Record[OpNum + 4], Alignment)) | ||||
5206 | return Err; | ||||
5207 | } | ||||
5208 | |||||
5209 | if (!Alignment) | ||||
5210 | Alignment = | ||||
5211 | Align(TheModule->getDataLayout().getTypeStoreSize(Val->getType())); | ||||
5212 | |||||
5213 | I = new AtomicRMWInst(Operation, Ptr, Val, *Alignment, Ordering, SSID); | ||||
5214 | cast<AtomicRMWInst>(I)->setVolatile(IsVol); | ||||
5215 | |||||
5216 | InstructionList.push_back(I); | ||||
5217 | break; | ||||
5218 | } | ||||
5219 | case bitc::FUNC_CODE_INST_FENCE: { // FENCE:[ordering, ssid] | ||||
5220 | if (2 != Record.size()) | ||||
5221 | return error("Invalid record"); | ||||
5222 | AtomicOrdering Ordering = getDecodedOrdering(Record[0]); | ||||
5223 | if (Ordering == AtomicOrdering::NotAtomic || | ||||
5224 | Ordering == AtomicOrdering::Unordered || | ||||
5225 | Ordering == AtomicOrdering::Monotonic) | ||||
5226 | return error("Invalid record"); | ||||
5227 | SyncScope::ID SSID = getDecodedSyncScopeID(Record[1]); | ||||
5228 | I = new FenceInst(Context, Ordering, SSID); | ||||
5229 | InstructionList.push_back(I); | ||||
5230 | break; | ||||
5231 | } | ||||
5232 | case bitc::FUNC_CODE_INST_CALL: { | ||||
5233 | // CALL: [paramattrs, cc, fmf, fnty, fnid, arg0, arg1...] | ||||
5234 | if (Record.size() < 3) | ||||
5235 | return error("Invalid record"); | ||||
5236 | |||||
5237 | unsigned OpNum = 0; | ||||
5238 | AttributeList PAL = getAttributes(Record[OpNum++]); | ||||
5239 | unsigned CCInfo = Record[OpNum++]; | ||||
5240 | |||||
5241 | FastMathFlags FMF; | ||||
5242 | if ((CCInfo >> bitc::CALL_FMF) & 1) { | ||||
5243 | FMF = getDecodedFastMathFlags(Record[OpNum++]); | ||||
5244 | if (!FMF.any()) | ||||
5245 | return error("Fast math flags indicator set for call with no FMF"); | ||||
5246 | } | ||||
5247 | |||||
5248 | FunctionType *FTy = nullptr; | ||||
5249 | if ((CCInfo >> bitc::CALL_EXPLICIT_TYPE) & 1) { | ||||
5250 | FTy = dyn_cast<FunctionType>(getTypeByID(Record[OpNum++])); | ||||
5251 | if (!FTy) | ||||
5252 | return error("Explicit call type is not a function type"); | ||||
5253 | } | ||||
5254 | |||||
5255 | Value *Callee; | ||||
5256 | if (getValueTypePair(Record, OpNum, NextValueNo, Callee)) | ||||
5257 | return error("Invalid record"); | ||||
5258 | |||||
5259 | PointerType *OpTy = dyn_cast<PointerType>(Callee->getType()); | ||||
5260 | if (!OpTy) | ||||
5261 | return error("Callee is not a pointer type"); | ||||
5262 | if (!FTy) { | ||||
5263 | FTy = dyn_cast<FunctionType>( | ||||
5264 | cast<PointerType>(Callee->getType())->getElementType()); | ||||
5265 | if (!FTy) | ||||
5266 | return error("Callee is not of pointer to function type"); | ||||
5267 | } else if (cast<PointerType>(Callee->getType())->getElementType() != FTy) | ||||
5268 | return error("Explicit call type does not match pointee type of " | ||||
5269 | "callee operand"); | ||||
5270 | if (Record.size() < FTy->getNumParams() + OpNum) | ||||
5271 | return error("Insufficient operands to call"); | ||||
5272 | |||||
5273 | SmallVector<Value*, 16> Args; | ||||
5274 | SmallVector<Type *, 16> ArgsTys; | ||||
5275 | // Read the fixed params. | ||||
5276 | for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i, ++OpNum) { | ||||
5277 | if (FTy->getParamType(i)->isLabelTy()) | ||||
5278 | Args.push_back(getBasicBlock(Record[OpNum])); | ||||
5279 | else | ||||
5280 | Args.push_back(getValue(Record, OpNum, NextValueNo, | ||||
5281 | FTy->getParamType(i))); | ||||
5282 | ArgsTys.push_back(FTy->getParamType(i)); | ||||
5283 | if (!Args.back()) | ||||
5284 | return error("Invalid record"); | ||||
5285 | } | ||||
5286 | |||||
5287 | // Read type/value pairs for varargs params. | ||||
5288 | if (!FTy->isVarArg()) { | ||||
5289 | if (OpNum != Record.size()) | ||||
5290 | return error("Invalid record"); | ||||
5291 | } else { | ||||
5292 | while (OpNum != Record.size()) { | ||||
5293 | Value *Op; | ||||
5294 | if (getValueTypePair(Record, OpNum, NextValueNo, Op)) | ||||
5295 | return error("Invalid record"); | ||||
5296 | Args.push_back(Op); | ||||
5297 | ArgsTys.push_back(Op->getType()); | ||||
5298 | } | ||||
5299 | } | ||||
5300 | |||||
5301 | I = CallInst::Create(FTy, Callee, Args, OperandBundles); | ||||
5302 | OperandBundles.clear(); | ||||
5303 | InstructionList.push_back(I); | ||||
5304 | cast<CallInst>(I)->setCallingConv( | ||||
5305 | static_cast<CallingConv::ID>((0x7ff & CCInfo) >> bitc::CALL_CCONV)); | ||||
5306 | CallInst::TailCallKind TCK = CallInst::TCK_None; | ||||
5307 | if (CCInfo & 1 << bitc::CALL_TAIL) | ||||
5308 | TCK = CallInst::TCK_Tail; | ||||
5309 | if (CCInfo & (1 << bitc::CALL_MUSTTAIL)) | ||||
5310 | TCK = CallInst::TCK_MustTail; | ||||
5311 | if (CCInfo & (1 << bitc::CALL_NOTAIL)) | ||||
5312 | TCK = CallInst::TCK_NoTail; | ||||
5313 | cast<CallInst>(I)->setTailCallKind(TCK); | ||||
5314 | cast<CallInst>(I)->setAttributes(PAL); | ||||
5315 | propagateByValSRetTypes(cast<CallBase>(I), ArgsTys); | ||||
5316 | if (FMF.any()) { | ||||
5317 | if (!isa<FPMathOperator>(I)) | ||||
5318 | return error("Fast-math-flags specified for call without " | ||||
5319 | "floating-point scalar or vector return type"); | ||||
5320 | I->setFastMathFlags(FMF); | ||||
5321 | } | ||||
5322 | break; | ||||
5323 | } | ||||
5324 | case bitc::FUNC_CODE_INST_VAARG: { // VAARG: [valistty, valist, instty] | ||||
5325 | if (Record.size() < 3) | ||||
5326 | return error("Invalid record"); | ||||
5327 | Type *OpTy = getTypeByID(Record[0]); | ||||
5328 | Value *Op = getValue(Record, 1, NextValueNo, OpTy); | ||||
5329 | Type *ResTy = getTypeByID(Record[2]); | ||||
5330 | if (!OpTy || !Op || !ResTy) | ||||
5331 | return error("Invalid record"); | ||||
5332 | I = new VAArgInst(Op, ResTy); | ||||
5333 | InstructionList.push_back(I); | ||||
5334 | break; | ||||
5335 | } | ||||
5336 | |||||
5337 | case bitc::FUNC_CODE_OPERAND_BUNDLE: { | ||||
5338 | // A call or an invoke can be optionally prefixed with some variable | ||||
5339 | // number of operand bundle blocks. These blocks are read into | ||||
5340 | // OperandBundles and consumed at the next call or invoke instruction. | ||||
5341 | |||||
5342 | if (Record.empty() || Record[0] >= BundleTags.size()) | ||||
5343 | return error("Invalid record"); | ||||
5344 | |||||
5345 | std::vector<Value *> Inputs; | ||||
5346 | |||||
5347 | unsigned OpNum = 1; | ||||
5348 | while (OpNum != Record.size()) { | ||||
5349 | Value *Op; | ||||
5350 | if (getValueTypePair(Record, OpNum, NextValueNo, Op)) | ||||
5351 | return error("Invalid record"); | ||||
5352 | Inputs.push_back(Op); | ||||
5353 | } | ||||
5354 | |||||
5355 | OperandBundles.emplace_back(BundleTags[Record[0]], std::move(Inputs)); | ||||
5356 | continue; | ||||
5357 | } | ||||
5358 | |||||
5359 | case bitc::FUNC_CODE_INST_FREEZE: { // FREEZE: [opty,opval] | ||||
5360 | unsigned OpNum = 0; | ||||
5361 | Value *Op = nullptr; | ||||
5362 | if (getValueTypePair(Record, OpNum, NextValueNo, Op)) | ||||
5363 | return error("Invalid record"); | ||||
5364 | if (OpNum != Record.size()) | ||||
5365 | return error("Invalid record"); | ||||
5366 | |||||
5367 | I = new FreezeInst(Op); | ||||
5368 | InstructionList.push_back(I); | ||||
5369 | break; | ||||
5370 | } | ||||
5371 | } | ||||
5372 | |||||
5373 | // Add instruction to end of current BB. If there is no current BB, reject | ||||
5374 | // this file. | ||||
5375 | if (!CurBB) { | ||||
5376 | I->deleteValue(); | ||||
5377 | return error("Invalid instruction with no BB"); | ||||
5378 | } | ||||
5379 | if (!OperandBundles.empty()) { | ||||
5380 | I->deleteValue(); | ||||
5381 | return error("Operand bundles found with no consumer"); | ||||
5382 | } | ||||
5383 | CurBB->getInstList().push_back(I); | ||||
5384 | |||||
5385 | // If this was a terminator instruction, move to the next block. | ||||
5386 | if (I->isTerminator()) { | ||||
5387 | ++CurBBNo; | ||||
5388 | CurBB = CurBBNo < FunctionBBs.size() ? FunctionBBs[CurBBNo] : nullptr; | ||||
5389 | } | ||||
5390 | |||||
5391 | // Non-void values get registered in the value table for future use. | ||||
5392 | if (!I->getType()->isVoidTy()) | ||||
5393 | ValueList.assignValue(I, NextValueNo++); | ||||
5394 | } | ||||
5395 | |||||
5396 | OutOfRecordLoop: | ||||
5397 | |||||
5398 | if (!OperandBundles.empty()) | ||||
5399 | return error("Operand bundles found with no consumer"); | ||||
5400 | |||||
5401 | // Check the function list for unresolved values. | ||||
5402 | if (Argument *A = dyn_cast<Argument>(ValueList.back())) { | ||||
5403 | if (!A->getParent()) { | ||||
5404 | // We found at least one unresolved value. Nuke them all to avoid leaks. | ||||
5405 | for (unsigned i = ModuleValueListSize, e = ValueList.size(); i != e; ++i){ | ||||
5406 | if ((A = dyn_cast_or_null<Argument>(ValueList[i])) && !A->getParent()) { | ||||
5407 | A->replaceAllUsesWith(UndefValue::get(A->getType())); | ||||
5408 | delete A; | ||||
5409 | } | ||||
5410 | } | ||||
5411 | return error("Never resolved value found in function"); | ||||
5412 | } | ||||
5413 | } | ||||
5414 | |||||
5415 | // Unexpected unresolved metadata about to be dropped. | ||||
5416 | if (MDLoader->hasFwdRefs()) | ||||
5417 | return error("Invalid function metadata: outgoing forward refs"); | ||||
5418 | |||||
5419 | // Trim the value list down to the size it was before we parsed this function. | ||||
5420 | ValueList.shrinkTo(ModuleValueListSize); | ||||
5421 | MDLoader->shrinkTo(ModuleMDLoaderSize); | ||||
5422 | std::vector<BasicBlock*>().swap(FunctionBBs); | ||||
5423 | return Error::success(); | ||||
5424 | } | ||||
5425 | |||||
5426 | /// Find the function body in the bitcode stream | ||||
5427 | Error BitcodeReader::findFunctionInStream( | ||||
5428 | Function *F, | ||||
5429 | DenseMap<Function *, uint64_t>::iterator DeferredFunctionInfoIterator) { | ||||
5430 | while (DeferredFunctionInfoIterator->second == 0) { | ||||
5431 | // This is the fallback handling for the old format bitcode that | ||||
5432 | // didn't contain the function index in the VST, or when we have | ||||
5433 | // an anonymous function which would not have a VST entry. | ||||
5434 | // Assert that we have one of those two cases. | ||||
5435 | assert(VSTOffset == 0 || !F->hasName())(static_cast <bool> (VSTOffset == 0 || !F->hasName() ) ? void (0) : __assert_fail ("VSTOffset == 0 || !F->hasName()" , "/build/llvm-toolchain-snapshot-13~++20210621111111+acefe0eaaf82/llvm/lib/Bitcode/Reader/BitcodeReader.cpp" , 5435, __extension__ __PRETTY_FUNCTION__)); | ||||
5436 | // Parse the next body in the stream and set its position in the | ||||
5437 | // DeferredFunctionInfo map. | ||||
5438 | if (Error Err = rememberAndSkipFunctionBodies()) | ||||
5439 | return Err; | ||||
5440 | } | ||||
5441 | return Error::success(); | ||||
5442 | } | ||||
5443 | |||||
5444 | SyncScope::ID BitcodeReader::getDecodedSyncScopeID(unsigned Val) { | ||||
5445 | if (Val == SyncScope::SingleThread || Val == SyncScope::System) | ||||
5446 | return SyncScope::ID(Val); | ||||
5447 | if (Val >= SSIDs.size()) | ||||
5448 | return SyncScope::System; // Map unknown synchronization scopes to system. | ||||
5449 | return SSIDs[Val]; | ||||
5450 | } | ||||
5451 | |||||
5452 | //===----------------------------------------------------------------------===// | ||||
5453 | // GVMaterializer implementation | ||||
5454 | //===----------------------------------------------------------------------===// | ||||
5455 | |||||
5456 | Error BitcodeReader::materialize(GlobalValue *GV) { | ||||
5457 | Function *F = dyn_cast<Function>(GV); | ||||
5458 | // If it's not a function or is already material, ignore the request. | ||||
5459 | if (!F || !F->isMaterializable()) | ||||
5460 | return Error::success(); | ||||
5461 | |||||
5462 | DenseMap<Function*, uint64_t>::iterator DFII = DeferredFunctionInfo.find(F); | ||||
5463 | assert(DFII != DeferredFunctionInfo.end() && "Deferred function not found!")(static_cast <bool> (DFII != DeferredFunctionInfo.end() && "Deferred function not found!") ? void (0) : __assert_fail ("DFII != DeferredFunctionInfo.end() && \"Deferred function not found!\"" , "/build/llvm-toolchain-snapshot-13~++20210621111111+acefe0eaaf82/llvm/lib/Bitcode/Reader/BitcodeReader.cpp" , 5463, __extension__ __PRETTY_FUNCTION__)); | ||||
5464 | // If its position is recorded as 0, its body is somewhere in the stream | ||||
5465 | // but we haven't seen it yet. | ||||
5466 | if (DFII->second == 0) | ||||
5467 | if (Error Err = findFunctionInStream(F, DFII)) | ||||
5468 | return Err; | ||||
5469 | |||||
5470 | // Materialize metadata before parsing any function bodies. | ||||
5471 | if (Error Err = materializeMetadata()) | ||||
5472 | return Err; | ||||
5473 | |||||
5474 | // Move the bit stream to the saved position of the deferred function body. | ||||
5475 | if (Error JumpFailed = Stream.JumpToBit(DFII->second)) | ||||
5476 | return JumpFailed; | ||||
5477 | if (Error Err = parseFunctionBody(F)) | ||||
5478 | return Err; | ||||
5479 | F->setIsMaterializable(false); | ||||
5480 | |||||
5481 | if (StripDebugInfo) | ||||
5482 | stripDebugInfo(*F); | ||||
5483 | |||||
5484 | // Upgrade any old intrinsic calls in the function. | ||||
5485 | for (auto &I : UpgradedIntrinsics) { | ||||
5486 | for (auto UI = I.first->materialized_user_begin(), UE = I.first->user_end(); | ||||
5487 | UI != UE;) { | ||||
5488 | User *U = *UI; | ||||
5489 | ++UI; | ||||
5490 | if (CallInst *CI = dyn_cast<CallInst>(U)) | ||||
5491 | UpgradeIntrinsicCall(CI, I.second); | ||||
5492 | } | ||||
5493 | } | ||||
5494 | |||||
5495 | // Update calls to the remangled intrinsics | ||||
5496 | for (auto &I : RemangledIntrinsics) | ||||
5497 | for (auto UI = I.first->materialized_user_begin(), UE = I.first->user_end(); | ||||
5498 | UI != UE;) | ||||
5499 | // Don't expect any other users than call sites | ||||
5500 | cast<CallBase>(*UI++)->setCalledFunction(I.second); | ||||
5501 | |||||
5502 | // Finish fn->subprogram upgrade for materialized functions. | ||||
5503 | if (DISubprogram *SP = MDLoader->lookupSubprogramForFunction(F)) | ||||
5504 | F->setSubprogram(SP); | ||||
5505 | |||||
5506 | // Check if the TBAA Metadata are valid, otherwise we will need to strip them. | ||||
5507 | if (!MDLoader->isStrippingTBAA()) { | ||||
5508 | for (auto &I : instructions(F)) { | ||||
5509 | MDNode *TBAA = I.getMetadata(LLVMContext::MD_tbaa); | ||||
5510 | if (!TBAA || TBAAVerifyHelper.visitTBAAMetadata(I, TBAA)) | ||||
5511 | continue; | ||||
5512 | MDLoader->setStripTBAA(true); | ||||
5513 | stripTBAA(F->getParent()); | ||||
5514 | } | ||||
5515 | } | ||||
5516 | |||||
5517 | for (auto &I : instructions(F)) { | ||||
5518 | // "Upgrade" older incorrect branch weights by dropping them. | ||||
5519 | if (auto *MD = I.getMetadata(LLVMContext::MD_prof)) { | ||||
5520 | if (MD->getOperand(0) != nullptr && isa<MDString>(MD->getOperand(0))) { | ||||
5521 | MDString *MDS = cast<MDString>(MD->getOperand(0)); | ||||
5522 | StringRef ProfName = MDS->getString(); | ||||
5523 | // Check consistency of !prof branch_weights metadata. | ||||
5524 | if (!ProfName.equals("branch_weights")) | ||||
5525 | continue; | ||||
5526 | unsigned ExpectedNumOperands = 0; | ||||
5527 | if (BranchInst *BI = dyn_cast<BranchInst>(&I)) | ||||
5528 | ExpectedNumOperands = BI->getNumSuccessors(); | ||||
5529 | else if (SwitchInst *SI = dyn_cast<SwitchInst>(&I)) | ||||
5530 | ExpectedNumOperands = SI->getNumSuccessors(); | ||||
5531 | else if (isa<CallInst>(&I)) | ||||
5532 | ExpectedNumOperands = 1; | ||||
5533 | else if (IndirectBrInst *IBI = dyn_cast<IndirectBrInst>(&I)) | ||||
5534 | ExpectedNumOperands = IBI->getNumDestinations(); | ||||
5535 | else if (isa<SelectInst>(&I)) | ||||
5536 | ExpectedNumOperands = 2; | ||||
5537 | else | ||||
5538 | continue; // ignore and continue. | ||||
5539 | |||||
5540 | // If branch weight doesn't match, just strip branch weight. | ||||
5541 | if (MD->getNumOperands() != 1 + ExpectedNumOperands) | ||||
5542 | I.setMetadata(LLVMContext::MD_prof, nullptr); | ||||
5543 | } | ||||
5544 | } | ||||
5545 | |||||
5546 | // Remove incompatible attributes on function calls. | ||||
5547 | if (auto *CI = dyn_cast<CallBase>(&I)) { | ||||
5548 | CI->removeAttributes(AttributeList::ReturnIndex, | ||||
5549 | AttributeFuncs::typeIncompatible( | ||||
5550 | CI->getFunctionType()->getReturnType())); | ||||
5551 | |||||
5552 | for (unsigned ArgNo = 0; ArgNo < CI->arg_size(); ++ArgNo) | ||||
5553 | CI->removeParamAttrs(ArgNo, AttributeFuncs::typeIncompatible( | ||||
5554 | CI->getArgOperand(ArgNo)->getType())); | ||||
5555 | } | ||||
5556 | } | ||||
5557 | |||||
5558 | // Look for functions that rely on old function attribute behavior. | ||||
5559 | UpgradeFunctionAttributes(*F); | ||||
5560 | |||||
5561 | // Bring in any functions that this function forward-referenced via | ||||
5562 | // blockaddresses. | ||||
5563 | return materializeForwardReferencedFunctions(); | ||||
5564 | } | ||||
5565 | |||||
5566 | Error BitcodeReader::materializeModule() { | ||||
5567 | if (Error Err = materializeMetadata()) | ||||
5568 | return Err; | ||||
5569 | |||||
5570 | // Promise to materialize all forward references. | ||||
5571 | WillMaterializeAllForwardRefs = true; | ||||
5572 | |||||
5573 | // Iterate over the module, deserializing any functions that are still on | ||||
5574 | // disk. | ||||
5575 | for (Function &F : *TheModule) { | ||||
5576 | if (Error Err = materialize(&F)) | ||||
5577 | return Err; | ||||
5578 | } | ||||
5579 | // At this point, if there are any function bodies, parse the rest of | ||||
5580 | // the bits in the module past the last function block we have recorded | ||||
5581 | // through either lazy scanning or the VST. | ||||
5582 | if (LastFunctionBlockBit || NextUnreadBit) | ||||
5583 | if (Error Err = parseModule(LastFunctionBlockBit > NextUnreadBit | ||||
5584 | ? LastFunctionBlockBit | ||||
5585 | : NextUnreadBit)) | ||||
5586 | return Err; | ||||
5587 | |||||
5588 | // Check that all block address forward references got resolved (as we | ||||
5589 | // promised above). | ||||
5590 | if (!BasicBlockFwdRefs.empty()) | ||||
5591 | return error("Never resolved function from blockaddress"); | ||||
5592 | |||||
5593 | // Upgrade any intrinsic calls that slipped through (should not happen!) and | ||||
5594 | // delete the old functions to clean up. We can't do this unless the entire | ||||
5595 | // module is materialized because there could always be another function body | ||||
5596 | // with calls to the old function. | ||||
5597 | for (auto &I : UpgradedIntrinsics) { | ||||
5598 | for (auto *U : I.first->users()) { | ||||
5599 | if (CallInst *CI = dyn_cast<CallInst>(U)) | ||||
5600 | UpgradeIntrinsicCall(CI, I.second); | ||||
5601 | } | ||||
5602 | if (!I.first->use_empty()) | ||||
5603 | I.first->replaceAllUsesWith(I.second); | ||||
5604 | I.first->eraseFromParent(); | ||||
5605 | } | ||||
5606 | UpgradedIntrinsics.clear(); | ||||
5607 | // Do the same for remangled intrinsics | ||||
5608 | for (auto &I : RemangledIntrinsics) { | ||||
5609 | I.first->replaceAllUsesWith(I.second); | ||||
5610 | I.first->eraseFromParent(); | ||||
5611 | } | ||||
5612 | RemangledIntrinsics.clear(); | ||||
5613 | |||||
5614 | UpgradeDebugInfo(*TheModule); | ||||
5615 | |||||
5616 | UpgradeModuleFlags(*TheModule); | ||||
5617 | |||||
5618 | UpgradeARCRuntime(*TheModule); | ||||
5619 | |||||
5620 | return Error::success(); | ||||
5621 | } | ||||
5622 | |||||
5623 | std::vector<StructType *> BitcodeReader::getIdentifiedStructTypes() const { | ||||
5624 | return IdentifiedStructTypes; | ||||
5625 | } | ||||
5626 | |||||
5627 | ModuleSummaryIndexBitcodeReader::ModuleSummaryIndexBitcodeReader( | ||||
5628 | BitstreamCursor Cursor, StringRef Strtab, ModuleSummaryIndex &TheIndex, | ||||
5629 | StringRef ModulePath, unsigned ModuleId) | ||||
5630 | : BitcodeReaderBase(std::move(Cursor), Strtab), TheIndex(TheIndex), | ||||
5631 | ModulePath(ModulePath), ModuleId(ModuleId) {} | ||||
5632 | |||||
5633 | void ModuleSummaryIndexBitcodeReader::addThisModule() { | ||||
5634 | TheIndex.addModule(ModulePath, ModuleId); | ||||
5635 | } | ||||
5636 | |||||
5637 | ModuleSummaryIndex::ModuleInfo * | ||||
5638 | ModuleSummaryIndexBitcodeReader::getThisModule() { | ||||
5639 | return TheIndex.getModule(ModulePath); | ||||
5640 | } | ||||
5641 | |||||
5642 | std::pair<ValueInfo, GlobalValue::GUID> | ||||
5643 | ModuleSummaryIndexBitcodeReader::getValueInfoFromValueId(unsigned ValueId) { | ||||
5644 | auto VGI = ValueIdToValueInfoMap[ValueId]; | ||||
5645 | assert(VGI.first)(static_cast <bool> (VGI.first) ? void (0) : __assert_fail ("VGI.first", "/build/llvm-toolchain-snapshot-13~++20210621111111+acefe0eaaf82/llvm/lib/Bitcode/Reader/BitcodeReader.cpp" , 5645, __extension__ __PRETTY_FUNCTION__)); | ||||
5646 | return VGI; | ||||
5647 | } | ||||
5648 | |||||
5649 | void ModuleSummaryIndexBitcodeReader::setValueGUID( | ||||
5650 | uint64_t ValueID, StringRef ValueName, GlobalValue::LinkageTypes Linkage, | ||||
5651 | StringRef SourceFileName) { | ||||
5652 | std::string GlobalId = | ||||
5653 | GlobalValue::getGlobalIdentifier(ValueName, Linkage, SourceFileName); | ||||
5654 | auto ValueGUID = GlobalValue::getGUID(GlobalId); | ||||
5655 | auto OriginalNameID = ValueGUID; | ||||
5656 | if (GlobalValue::isLocalLinkage(Linkage)) | ||||
5657 | OriginalNameID = GlobalValue::getGUID(ValueName); | ||||
5658 | if (PrintSummaryGUIDs) | ||||
5659 | dbgs() << "GUID " << ValueGUID << "(" << OriginalNameID << ") is " | ||||
5660 | << ValueName << "\n"; | ||||
5661 | |||||
5662 | // UseStrtab is false for legacy summary formats and value names are | ||||
5663 | // created on stack. In that case we save the name in a string saver in | ||||
5664 | // the index so that the value name can be recorded. | ||||
5665 | ValueIdToValueInfoMap[ValueID] = std::make_pair( | ||||
5666 | TheIndex.getOrInsertValueInfo( | ||||
5667 | ValueGUID, | ||||
5668 | UseStrtab ? ValueName : TheIndex.saveString(ValueName)), | ||||
5669 | OriginalNameID); | ||||
5670 | } | ||||
5671 | |||||
5672 | // Specialized value symbol table parser used when reading module index | ||||
5673 | // blocks where we don't actually create global values. The parsed information | ||||
5674 | // is saved in the bitcode reader for use when later parsing summaries. | ||||
5675 | Error ModuleSummaryIndexBitcodeReader::parseValueSymbolTable( | ||||
5676 | uint64_t Offset, | ||||
5677 | DenseMap<unsigned, GlobalValue::LinkageTypes> &ValueIdToLinkageMap) { | ||||
5678 | // With a strtab the VST is not required to parse the summary. | ||||
5679 | if (UseStrtab) | ||||
5680 | return Error::success(); | ||||
5681 | |||||
5682 | assert(Offset > 0 && "Expected non-zero VST offset")(static_cast <bool> (Offset > 0 && "Expected non-zero VST offset" ) ? void (0) : __assert_fail ("Offset > 0 && \"Expected non-zero VST offset\"" , "/build/llvm-toolchain-snapshot-13~++20210621111111+acefe0eaaf82/llvm/lib/Bitcode/Reader/BitcodeReader.cpp" , 5682, __extension__ __PRETTY_FUNCTION__)); | ||||
5683 | Expected<uint64_t> MaybeCurrentBit = jumpToValueSymbolTable(Offset, Stream); | ||||
5684 | if (!MaybeCurrentBit) | ||||
5685 | return MaybeCurrentBit.takeError(); | ||||
5686 | uint64_t CurrentBit = MaybeCurrentBit.get(); | ||||
5687 | |||||
5688 | if (Error Err = Stream.EnterSubBlock(bitc::VALUE_SYMTAB_BLOCK_ID)) | ||||
5689 | return Err; | ||||
5690 | |||||
5691 | SmallVector<uint64_t, 64> Record; | ||||
5692 | |||||
5693 | // Read all the records for this value table. | ||||
5694 | SmallString<128> ValueName; | ||||
5695 | |||||
5696 | while (true) { | ||||
5697 | Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks(); | ||||
5698 | if (!MaybeEntry) | ||||
5699 | return MaybeEntry.takeError(); | ||||
5700 | BitstreamEntry Entry = MaybeEntry.get(); | ||||
5701 | |||||
5702 | switch (Entry.Kind) { | ||||
5703 | case BitstreamEntry::SubBlock: // Handled for us already. | ||||
5704 | case BitstreamEntry::Error: | ||||
5705 | return error("Malformed block"); | ||||
5706 | case BitstreamEntry::EndBlock: | ||||
5707 | // Done parsing VST, jump back to wherever we came from. | ||||
5708 | if (Error JumpFailed = Stream.JumpToBit(CurrentBit)) | ||||
5709 | return JumpFailed; | ||||
5710 | return Error::success(); | ||||
5711 | case BitstreamEntry::Record: | ||||
5712 | // The interesting case. | ||||
5713 | break; | ||||
5714 | } | ||||
5715 | |||||
5716 | // Read a record. | ||||
5717 | Record.clear(); | ||||
5718 | Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record); | ||||
5719 | if (!MaybeRecord) | ||||
5720 | return MaybeRecord.takeError(); | ||||
5721 | switch (MaybeRecord.get()) { | ||||
5722 | default: // Default behavior: ignore (e.g. VST_CODE_BBENTRY records). | ||||
5723 | break; | ||||
5724 | case bitc::VST_CODE_ENTRY: { // VST_CODE_ENTRY: [valueid, namechar x N] | ||||
5725 | if (convertToString(Record, 1, ValueName)) | ||||
5726 | return error("Invalid record"); | ||||
5727 | unsigned ValueID = Record[0]; | ||||
5728 | assert(!SourceFileName.empty())(static_cast <bool> (!SourceFileName.empty()) ? void (0 ) : __assert_fail ("!SourceFileName.empty()", "/build/llvm-toolchain-snapshot-13~++20210621111111+acefe0eaaf82/llvm/lib/Bitcode/Reader/BitcodeReader.cpp" , 5728, __extension__ __PRETTY_FUNCTION__)); | ||||
5729 | auto VLI = ValueIdToLinkageMap.find(ValueID); | ||||
5730 | assert(VLI != ValueIdToLinkageMap.end() &&(static_cast <bool> (VLI != ValueIdToLinkageMap.end() && "No linkage found for VST entry?") ? void (0) : __assert_fail ("VLI != ValueIdToLinkageMap.end() && \"No linkage found for VST entry?\"" , "/build/llvm-toolchain-snapshot-13~++20210621111111+acefe0eaaf82/llvm/lib/Bitcode/Reader/BitcodeReader.cpp" , 5731, __extension__ __PRETTY_FUNCTION__)) | ||||
5731 | "No linkage found for VST entry?")(static_cast <bool> (VLI != ValueIdToLinkageMap.end() && "No linkage found for VST entry?") ? void (0) : __assert_fail ("VLI != ValueIdToLinkageMap.end() && \"No linkage found for VST entry?\"" , "/build/llvm-toolchain-snapshot-13~++20210621111111+acefe0eaaf82/llvm/lib/Bitcode/Reader/BitcodeReader.cpp" , 5731, __extension__ __PRETTY_FUNCTION__)); | ||||
5732 | auto Linkage = VLI->second; | ||||
5733 | setValueGUID(ValueID, ValueName, Linkage, SourceFileName); | ||||
5734 | ValueName.clear(); | ||||
5735 | break; | ||||
5736 | } | ||||
5737 | case bitc::VST_CODE_FNENTRY: { | ||||
5738 | // VST_CODE_FNENTRY: [valueid, offset, namechar x N] | ||||
5739 | if (convertToString(Record, 2, ValueName)) | ||||
5740 | return error("Invalid record"); | ||||
5741 | unsigned ValueID = Record[0]; | ||||
5742 | assert(!SourceFileName.empty())(static_cast <bool> (!SourceFileName.empty()) ? void (0 ) : __assert_fail ("!SourceFileName.empty()", "/build/llvm-toolchain-snapshot-13~++20210621111111+acefe0eaaf82/llvm/lib/Bitcode/Reader/BitcodeReader.cpp" , 5742, __extension__ __PRETTY_FUNCTION__)); | ||||
5743 | auto VLI = ValueIdToLinkageMap.find(ValueID); | ||||
5744 | assert(VLI != ValueIdToLinkageMap.end() &&(static_cast <bool> (VLI != ValueIdToLinkageMap.end() && "No linkage found for VST entry?") ? void (0) : __assert_fail ("VLI != ValueIdToLinkageMap.end() && \"No linkage found for VST entry?\"" , "/build/llvm-toolchain-snapshot-13~++20210621111111+acefe0eaaf82/llvm/lib/Bitcode/Reader/BitcodeReader.cpp" , 5745, __extension__ __PRETTY_FUNCTION__)) | ||||
5745 | "No linkage found for VST entry?")(static_cast <bool> (VLI != ValueIdToLinkageMap.end() && "No linkage found for VST entry?") ? void (0) : __assert_fail ("VLI != ValueIdToLinkageMap.end() && \"No linkage found for VST entry?\"" , "/build/llvm-toolchain-snapshot-13~++20210621111111+acefe0eaaf82/llvm/lib/Bitcode/Reader/BitcodeReader.cpp" , 5745, __extension__ __PRETTY_FUNCTION__)); | ||||
5746 | auto Linkage = VLI->second; | ||||
5747 | setValueGUID(ValueID, ValueName, Linkage, SourceFileName); | ||||
5748 | ValueName.clear(); | ||||
5749 | break; | ||||
5750 | } | ||||
5751 | case bitc::VST_CODE_COMBINED_ENTRY: { | ||||
5752 | // VST_CODE_COMBINED_ENTRY: [valueid, refguid] | ||||
5753 | unsigned ValueID = Record[0]; | ||||
5754 | GlobalValue::GUID RefGUID = Record[1]; | ||||
5755 | // The "original name", which is the second value of the pair will be | ||||
5756 | // overriden later by a FS_COMBINED_ORIGINAL_NAME in the combined index. | ||||
5757 | ValueIdToValueInfoMap[ValueID] = | ||||
5758 | std::make_pair(TheIndex.getOrInsertValueInfo(RefGUID), RefGUID); | ||||
5759 | break; | ||||
5760 | } | ||||
5761 | } | ||||
5762 | } | ||||
5763 | } | ||||
5764 | |||||
5765 | // Parse just the blocks needed for building the index out of the module. | ||||
5766 | // At the end of this routine the module Index is populated with a map | ||||
5767 | // from global value id to GlobalValueSummary objects. | ||||
5768 | Error ModuleSummaryIndexBitcodeReader::parseModule() { | ||||
5769 | if (Error Err = Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID)) | ||||
5770 | return Err; | ||||
5771 | |||||
5772 | SmallVector<uint64_t, 64> Record; | ||||
5773 | DenseMap<unsigned, GlobalValue::LinkageTypes> ValueIdToLinkageMap; | ||||
5774 | unsigned ValueId = 0; | ||||
5775 | |||||
5776 | // Read the index for this module. | ||||
5777 | while (true) { | ||||
5778 | Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); | ||||
5779 | if (!MaybeEntry) | ||||
5780 | return MaybeEntry.takeError(); | ||||
5781 | llvm::BitstreamEntry Entry = MaybeEntry.get(); | ||||
5782 | |||||
5783 | switch (Entry.Kind) { | ||||
5784 | case BitstreamEntry::Error: | ||||
5785 | return error("Malformed block"); | ||||
5786 | case BitstreamEntry::EndBlock: | ||||
5787 | return Error::success(); | ||||
5788 | |||||
5789 | case BitstreamEntry::SubBlock: | ||||
5790 | switch (Entry.ID) { | ||||
5791 | default: // Skip unknown content. | ||||
5792 | if (Error Err = Stream.SkipBlock()) | ||||
5793 | return Err; | ||||
5794 | break; | ||||
5795 | case bitc::BLOCKINFO_BLOCK_ID: | ||||
5796 | // Need to parse these to get abbrev ids (e.g. for VST) | ||||
5797 | if (readBlockInfo()) | ||||
5798 | return error("Malformed block"); | ||||
5799 | break; | ||||
5800 | case bitc::VALUE_SYMTAB_BLOCK_ID: | ||||
5801 | // Should have been parsed earlier via VSTOffset, unless there | ||||
5802 | // is no summary section. | ||||
5803 | assert(((SeenValueSymbolTable && VSTOffset > 0) ||(static_cast <bool> (((SeenValueSymbolTable && VSTOffset > 0) || !SeenGlobalValSummary) && "Expected early VST parse via VSTOffset record" ) ? void (0) : __assert_fail ("((SeenValueSymbolTable && VSTOffset > 0) || !SeenGlobalValSummary) && \"Expected early VST parse via VSTOffset record\"" , "/build/llvm-toolchain-snapshot-13~++20210621111111+acefe0eaaf82/llvm/lib/Bitcode/Reader/BitcodeReader.cpp" , 5805, __extension__ __PRETTY_FUNCTION__)) | ||||
5804 | !SeenGlobalValSummary) &&(static_cast <bool> (((SeenValueSymbolTable && VSTOffset > 0) || !SeenGlobalValSummary) && "Expected early VST parse via VSTOffset record" ) ? void (0) : __assert_fail ("((SeenValueSymbolTable && VSTOffset > 0) || !SeenGlobalValSummary) && \"Expected early VST parse via VSTOffset record\"" , "/build/llvm-toolchain-snapshot-13~++20210621111111+acefe0eaaf82/llvm/lib/Bitcode/Reader/BitcodeReader.cpp" , 5805, __extension__ __PRETTY_FUNCTION__)) | ||||
5805 | "Expected early VST parse via VSTOffset record")(static_cast <bool> (((SeenValueSymbolTable && VSTOffset > 0) || !SeenGlobalValSummary) && "Expected early VST parse via VSTOffset record" ) ? void (0) : __assert_fail ("((SeenValueSymbolTable && VSTOffset > 0) || !SeenGlobalValSummary) && \"Expected early VST parse via VSTOffset record\"" , "/build/llvm-toolchain-snapshot-13~++20210621111111+acefe0eaaf82/llvm/lib/Bitcode/Reader/BitcodeReader.cpp" , 5805, __extension__ __PRETTY_FUNCTION__)); | ||||
5806 | if (Error Err = Stream.SkipBlock()) | ||||
5807 | return Err; | ||||
5808 | break; | ||||
5809 | case bitc::GLOBALVAL_SUMMARY_BLOCK_ID: | ||||
5810 | case bitc::FULL_LTO_GLOBALVAL_SUMMARY_BLOCK_ID: | ||||
5811 | // Add the module if it is a per-module index (has a source file name). | ||||
5812 | if (!SourceFileName.empty()) | ||||
5813 | addThisModule(); | ||||
5814 | assert(!SeenValueSymbolTable &&(static_cast <bool> (!SeenValueSymbolTable && "Already read VST when parsing summary block?" ) ? void (0) : __assert_fail ("!SeenValueSymbolTable && \"Already read VST when parsing summary block?\"" , "/build/llvm-toolchain-snapshot-13~++20210621111111+acefe0eaaf82/llvm/lib/Bitcode/Reader/BitcodeReader.cpp" , 5815, __extension__ __PRETTY_FUNCTION__)) | ||||
5815 | "Already read VST when parsing summary block?")(static_cast <bool> (!SeenValueSymbolTable && "Already read VST when parsing summary block?" ) ? void (0) : __assert_fail ("!SeenValueSymbolTable && \"Already read VST when parsing summary block?\"" , "/build/llvm-toolchain-snapshot-13~++20210621111111+acefe0eaaf82/llvm/lib/Bitcode/Reader/BitcodeReader.cpp" , 5815, __extension__ __PRETTY_FUNCTION__)); | ||||
5816 | // We might not have a VST if there were no values in the | ||||
5817 | // summary. An empty summary block generated when we are | ||||
5818 | // performing ThinLTO compiles so we don't later invoke | ||||
5819 | // the regular LTO process on them. | ||||
5820 | if (VSTOffset > 0) { | ||||
5821 | if (Error Err = parseValueSymbolTable(VSTOffset, ValueIdToLinkageMap)) | ||||
5822 | return Err; | ||||
5823 | SeenValueSymbolTable = true; | ||||
5824 | } | ||||
5825 | SeenGlobalValSummary = true; | ||||
5826 | if (Error Err = parseEntireSummary(Entry.ID)) | ||||
5827 | return Err; | ||||
5828 | break; | ||||
5829 | case bitc::MODULE_STRTAB_BLOCK_ID: | ||||
5830 | if (Error Err = parseModuleStringTable()) | ||||
5831 | return Err; | ||||
5832 | break; | ||||
5833 | } | ||||
5834 | continue; | ||||
5835 | |||||
5836 | case BitstreamEntry::Record: { | ||||
5837 | Record.clear(); | ||||
5838 | Expected<unsigned> MaybeBitCode = Stream.readRecord(Entry.ID, Record); | ||||
5839 | if (!MaybeBitCode) | ||||
5840 | return MaybeBitCode.takeError(); | ||||
5841 | switch (MaybeBitCode.get()) { | ||||
5842 | default: | ||||
5843 | break; // Default behavior, ignore unknown content. | ||||
5844 | case bitc::MODULE_CODE_VERSION: { | ||||
5845 | if (Error Err = parseVersionRecord(Record).takeError()) | ||||
5846 | return Err; | ||||
5847 | break; | ||||
5848 | } | ||||
5849 | /// MODULE_CODE_SOURCE_FILENAME: [namechar x N] | ||||
5850 | case bitc::MODULE_CODE_SOURCE_FILENAME: { | ||||
5851 | SmallString<128> ValueName; | ||||
5852 | if (convertToString(Record, 0, ValueName)) | ||||
5853 | return error("Invalid record"); | ||||
5854 | SourceFileName = ValueName.c_str(); | ||||
5855 | break; | ||||
5856 | } | ||||
5857 | /// MODULE_CODE_HASH: [5*i32] | ||||
5858 | case bitc::MODULE_CODE_HASH: { | ||||
5859 | if (Record.size() != 5) | ||||
5860 | return error("Invalid hash length " + Twine(Record.size()).str()); | ||||
5861 | auto &Hash = getThisModule()->second.second; | ||||
5862 | int Pos = 0; | ||||
5863 | for (auto &Val : Record) { | ||||
5864 | assert(!(Val >> 32) && "Unexpected high bits set")(static_cast <bool> (!(Val >> 32) && "Unexpected high bits set" ) ? void (0) : __assert_fail ("!(Val >> 32) && \"Unexpected high bits set\"" , "/build/llvm-toolchain-snapshot-13~++20210621111111+acefe0eaaf82/llvm/lib/Bitcode/Reader/BitcodeReader.cpp" , 5864, __extension__ __PRETTY_FUNCTION__)); | ||||
5865 | Hash[Pos++] = Val; | ||||
5866 | } | ||||
5867 | break; | ||||
5868 | } | ||||
5869 | /// MODULE_CODE_VSTOFFSET: [offset] | ||||
5870 | case bitc::MODULE_CODE_VSTOFFSET: | ||||
5871 | if (Record.empty()) | ||||
5872 | return error("Invalid record"); | ||||
5873 | // Note that we subtract 1 here because the offset is relative to one | ||||
5874 | // word before the start of the identification or module block, which | ||||
5875 | // was historically always the start of the regular bitcode header. | ||||
5876 | VSTOffset = Record[0] - 1; | ||||
5877 | break; | ||||
5878 | // v1 GLOBALVAR: [pointer type, isconst, initid, linkage, ...] | ||||
5879 | // v1 FUNCTION: [type, callingconv, isproto, linkage, ...] | ||||
5880 | // v1 ALIAS: [alias type, addrspace, aliasee val#, linkage, ...] | ||||
5881 | // v2: [strtab offset, strtab size, v1] | ||||
5882 | case bitc::MODULE_CODE_GLOBALVAR: | ||||
5883 | case bitc::MODULE_CODE_FUNCTION: | ||||
5884 | case bitc::MODULE_CODE_ALIAS: { | ||||
5885 | StringRef Name; | ||||
5886 | ArrayRef<uint64_t> GVRecord; | ||||
5887 | std::tie(Name, GVRecord) = readNameFromStrtab(Record); | ||||
5888 | if (GVRecord.size() <= 3) | ||||
5889 | return error("Invalid record"); | ||||
5890 | uint64_t RawLinkage = GVRecord[3]; | ||||
5891 | GlobalValue::LinkageTypes Linkage = getDecodedLinkage(RawLinkage); | ||||
5892 | if (!UseStrtab) { | ||||
5893 | ValueIdToLinkageMap[ValueId++] = Linkage; | ||||
5894 | break; | ||||
5895 | } | ||||
5896 | |||||
5897 | setValueGUID(ValueId++, Name, Linkage, SourceFileName); | ||||
5898 | break; | ||||
5899 | } | ||||
5900 | } | ||||
5901 | } | ||||
5902 | continue; | ||||
5903 | } | ||||
5904 | } | ||||
5905 | } | ||||
5906 | |||||
5907 | std::vector<ValueInfo> | ||||
5908 | ModuleSummaryIndexBitcodeReader::makeRefList(ArrayRef<uint64_t> Record) { | ||||
5909 | std::vector<ValueInfo> Ret; | ||||
5910 | Ret.reserve(Record.size()); | ||||
5911 | for (uint64_t RefValueId : Record) | ||||
5912 | Ret.push_back(getValueInfoFromValueId(RefValueId).first); | ||||
5913 | return Ret; | ||||
5914 | } | ||||
5915 | |||||
5916 | std::vector<FunctionSummary::EdgeTy> | ||||
5917 | ModuleSummaryIndexBitcodeReader::makeCallList(ArrayRef<uint64_t> Record, | ||||
5918 | bool IsOldProfileFormat, | ||||
5919 | bool HasProfile, bool HasRelBF) { | ||||
5920 | std::vector<FunctionSummary::EdgeTy> Ret; | ||||
5921 | Ret.reserve(Record.size()); | ||||
5922 | for (unsigned I = 0, E = Record.size(); I != E; ++I) { | ||||
5923 | CalleeInfo::HotnessType Hotness = CalleeInfo::HotnessType::Unknown; | ||||
5924 | uint64_t RelBF = 0; | ||||
5925 | ValueInfo Callee = getValueInfoFromValueId(Record[I]).first; | ||||
5926 | if (IsOldProfileFormat) { | ||||
5927 | I += 1; // Skip old callsitecount field | ||||
5928 | if (HasProfile) | ||||
5929 | I += 1; // Skip old profilecount field | ||||
5930 | } else if (HasProfile) | ||||
5931 | Hotness = static_cast<CalleeInfo::HotnessType>(Record[++I]); | ||||
5932 | else if (HasRelBF) | ||||
5933 | RelBF = Record[++I]; | ||||
5934 | Ret.push_back(FunctionSummary::EdgeTy{Callee, CalleeInfo(Hotness, RelBF)}); | ||||
5935 | } | ||||
5936 | return Ret; | ||||
5937 | } | ||||
5938 | |||||
5939 | static void | ||||
5940 | parseWholeProgramDevirtResolutionByArg(ArrayRef<uint64_t> Record, size_t &Slot, | ||||
5941 | WholeProgramDevirtResolution &Wpd) { | ||||
5942 | uint64_t ArgNum = Record[Slot++]; | ||||
5943 | WholeProgramDevirtResolution::ByArg &B = | ||||
5944 | Wpd.ResByArg[{Record.begin() + Slot, Record.begin() + Slot + ArgNum}]; | ||||
5945 | Slot += ArgNum; | ||||
5946 | |||||
5947 | B.TheKind = | ||||
5948 | static_cast<WholeProgramDevirtResolution::ByArg::Kind>(Record[Slot++]); | ||||
5949 | B.Info = Record[Slot++]; | ||||
5950 | B.Byte = Record[Slot++]; | ||||
5951 | B.Bit = Record[Slot++]; | ||||
5952 | } | ||||
5953 | |||||
5954 | static void parseWholeProgramDevirtResolution(ArrayRef<uint64_t> Record, | ||||
5955 | StringRef Strtab, size_t &Slot, | ||||
5956 | TypeIdSummary &TypeId) { | ||||
5957 | uint64_t Id = Record[Slot++]; | ||||
5958 | WholeProgramDevirtResolution &Wpd = TypeId.WPDRes[Id]; | ||||
5959 | |||||
5960 | Wpd.TheKind = static_cast<WholeProgramDevirtResolution::Kind>(Record[Slot++]); | ||||
5961 | Wpd.SingleImplName = {Strtab.data() + Record[Slot], | ||||
5962 | static_cast<size_t>(Record[Slot + 1])}; | ||||
5963 | Slot += 2; | ||||
5964 | |||||
5965 | uint64_t ResByArgNum = Record[Slot++]; | ||||
5966 | for (uint64_t I = 0; I != ResByArgNum; ++I) | ||||
5967 | parseWholeProgramDevirtResolutionByArg(Record, Slot, Wpd); | ||||
5968 | } | ||||
5969 | |||||
5970 | static void parseTypeIdSummaryRecord(ArrayRef<uint64_t> Record, | ||||
5971 | StringRef Strtab, | ||||
5972 | ModuleSummaryIndex &TheIndex) { | ||||
5973 | size_t Slot = 0; | ||||
5974 | TypeIdSummary &TypeId = TheIndex.getOrInsertTypeIdSummary( | ||||
5975 | {Strtab.data() + Record[Slot], static_cast<size_t>(Record[Slot + 1])}); | ||||
5976 | Slot += 2; | ||||
5977 | |||||
5978 | TypeId.TTRes.TheKind = static_cast<TypeTestResolution::Kind>(Record[Slot++]); | ||||
5979 | TypeId.TTRes.SizeM1BitWidth = Record[Slot++]; | ||||
5980 | TypeId.TTRes.AlignLog2 = Record[Slot++]; | ||||
5981 | TypeId.TTRes.SizeM1 = Record[Slot++]; | ||||
5982 | TypeId.TTRes.BitMask = Record[Slot++]; | ||||
5983 | TypeId.TTRes.InlineBits = Record[Slot++]; | ||||
5984 | |||||
5985 | while (Slot < Record.size()) | ||||
5986 | parseWholeProgramDevirtResolution(Record, Strtab, Slot, TypeId); | ||||
5987 | } | ||||
5988 | |||||
5989 | std::vector<FunctionSummary::ParamAccess> | ||||
5990 | ModuleSummaryIndexBitcodeReader::parseParamAccesses(ArrayRef<uint64_t> Record) { | ||||
5991 | auto ReadRange = [&]() { | ||||
5992 | APInt Lower(FunctionSummary::ParamAccess::RangeWidth, | ||||
5993 | BitcodeReader::decodeSignRotatedValue(Record.front())); | ||||
5994 | Record = Record.drop_front(); | ||||
5995 | APInt Upper(FunctionSummary::ParamAccess::RangeWidth, | ||||
5996 | BitcodeReader::decodeSignRotatedValue(Record.front())); | ||||
5997 | Record = Record.drop_front(); | ||||
5998 | ConstantRange Range{Lower, Upper}; | ||||
5999 | assert(!Range.isFullSet())(static_cast <bool> (!Range.isFullSet()) ? void (0) : __assert_fail ("!Range.isFullSet()", "/build/llvm-toolchain-snapshot-13~++20210621111111+acefe0eaaf82/llvm/lib/Bitcode/Reader/BitcodeReader.cpp" , 5999, __extension__ __PRETTY_FUNCTION__)); | ||||
6000 | assert(!Range.isUpperSignWrapped())(static_cast <bool> (!Range.isUpperSignWrapped()) ? void (0) : __assert_fail ("!Range.isUpperSignWrapped()", "/build/llvm-toolchain-snapshot-13~++20210621111111+acefe0eaaf82/llvm/lib/Bitcode/Reader/BitcodeReader.cpp" , 6000, __extension__ __PRETTY_FUNCTION__)); | ||||
6001 | return Range; | ||||
6002 | }; | ||||
6003 | |||||
6004 | std::vector<FunctionSummary::ParamAccess> PendingParamAccesses; | ||||
6005 | while (!Record.empty()) { | ||||
6006 | PendingParamAccesses.emplace_back(); | ||||
6007 | FunctionSummary::ParamAccess &ParamAccess = PendingParamAccesses.back(); | ||||
6008 | ParamAccess.ParamNo = Record.front(); | ||||
6009 | Record = Record.drop_front(); | ||||
6010 | ParamAccess.Use = ReadRange(); | ||||
6011 | ParamAccess.Calls.resize(Record.front()); | ||||
6012 | Record = Record.drop_front(); | ||||
6013 | for (auto &Call : ParamAccess.Calls) { | ||||
6014 | Call.ParamNo = Record.front(); | ||||
6015 | Record = Record.drop_front(); | ||||
6016 | Call.Callee = getValueInfoFromValueId(Record.front()).first; | ||||
6017 | Record = Record.drop_front(); | ||||
6018 | Call.Offsets = ReadRange(); | ||||
6019 | } | ||||
6020 | } | ||||
6021 | return PendingParamAccesses; | ||||
6022 | } | ||||
6023 | |||||
6024 | void ModuleSummaryIndexBitcodeReader::parseTypeIdCompatibleVtableInfo( | ||||
6025 | ArrayRef<uint64_t> Record, size_t &Slot, | ||||
6026 | TypeIdCompatibleVtableInfo &TypeId) { | ||||
6027 | uint64_t Offset = Record[Slot++]; | ||||
6028 | ValueInfo Callee = getValueInfoFromValueId(Record[Slot++]).first; | ||||
6029 | TypeId.push_back({Offset, Callee}); | ||||
6030 | } | ||||
6031 | |||||
6032 | void ModuleSummaryIndexBitcodeReader::parseTypeIdCompatibleVtableSummaryRecord( | ||||
6033 | ArrayRef<uint64_t> Record) { | ||||
6034 | size_t Slot = 0; | ||||
6035 | TypeIdCompatibleVtableInfo &TypeId = | ||||
6036 | TheIndex.getOrInsertTypeIdCompatibleVtableSummary( | ||||
6037 | {Strtab.data() + Record[Slot], | ||||
6038 | static_cast<size_t>(Record[Slot + 1])}); | ||||
6039 | Slot += 2; | ||||
6040 | |||||
6041 | while (Slot < Record.size()) | ||||
6042 | parseTypeIdCompatibleVtableInfo(Record, Slot, TypeId); | ||||
6043 | } | ||||
6044 | |||||
6045 | static void setSpecialRefs(std::vector<ValueInfo> &Refs, unsigned ROCnt, | ||||
6046 | unsigned WOCnt) { | ||||
6047 | // Readonly and writeonly refs are in the end of the refs list. | ||||
6048 | assert(ROCnt + WOCnt <= Refs.size())(static_cast <bool> (ROCnt + WOCnt <= Refs.size()) ? void (0) : __assert_fail ("ROCnt + WOCnt <= Refs.size()", "/build/llvm-toolchain-snapshot-13~++20210621111111+acefe0eaaf82/llvm/lib/Bitcode/Reader/BitcodeReader.cpp" , 6048, __extension__ __PRETTY_FUNCTION__)); | ||||
6049 | unsigned FirstWORef = Refs.size() - WOCnt; | ||||
6050 | unsigned RefNo = FirstWORef - ROCnt; | ||||
6051 | for (; RefNo < FirstWORef; ++RefNo) | ||||
6052 | Refs[RefNo].setReadOnly(); | ||||
6053 | for (; RefNo < Refs.size(); ++RefNo) | ||||
6054 | Refs[RefNo].setWriteOnly(); | ||||
6055 | } | ||||
6056 | |||||
6057 | // Eagerly parse the entire summary block. This populates the GlobalValueSummary | ||||
6058 | // objects in the index. | ||||
6059 | Error ModuleSummaryIndexBitcodeReader::parseEntireSummary(unsigned ID) { | ||||
6060 | if (Error Err = Stream.EnterSubBlock(ID)) | ||||
6061 | return Err; | ||||
6062 | SmallVector<uint64_t, 64> Record; | ||||
6063 | |||||
6064 | // Parse version | ||||
6065 | { | ||||
6066 | Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks(); | ||||
6067 | if (!MaybeEntry) | ||||
6068 | return MaybeEntry.takeError(); | ||||
6069 | BitstreamEntry Entry = MaybeEntry.get(); | ||||
6070 | |||||
6071 | if (Entry.Kind != BitstreamEntry::Record) | ||||
6072 | return error("Invalid Summary Block: record for version expected"); | ||||
6073 | Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record); | ||||
6074 | if (!MaybeRecord) | ||||
6075 | return MaybeRecord.takeError(); | ||||
6076 | if (MaybeRecord.get() != bitc::FS_VERSION) | ||||
6077 | return error("Invalid Summary Block: version expected"); | ||||
6078 | } | ||||
6079 | const uint64_t Version = Record[0]; | ||||
6080 | const bool IsOldProfileFormat = Version == 1; | ||||
6081 | if (Version < 1 || Version > ModuleSummaryIndex::BitcodeSummaryVersion) | ||||
6082 | return error("Invalid summary version " + Twine(Version) + | ||||
6083 | ". Version should be in the range [1-" + | ||||
6084 | Twine(ModuleSummaryIndex::BitcodeSummaryVersion) + | ||||
6085 | "]."); | ||||
6086 | Record.clear(); | ||||
6087 | |||||
6088 | // Keep around the last seen summary to be used when we see an optional | ||||
6089 | // "OriginalName" attachement. | ||||
6090 | GlobalValueSummary *LastSeenSummary = nullptr; | ||||
6091 | GlobalValue::GUID LastSeenGUID = 0; | ||||
6092 | |||||
6093 | // We can expect to see any number of type ID information records before | ||||
6094 | // each function summary records; these variables store the information | ||||
6095 | // collected so far so that it can be used to create the summary object. | ||||
6096 | std::vector<GlobalValue::GUID> PendingTypeTests; | ||||
6097 | std::vector<FunctionSummary::VFuncId> PendingTypeTestAssumeVCalls, | ||||
6098 | PendingTypeCheckedLoadVCalls; | ||||
6099 | std::vector<FunctionSummary::ConstVCall> PendingTypeTestAssumeConstVCalls, | ||||
6100 | PendingTypeCheckedLoadConstVCalls; | ||||
6101 | std::vector<FunctionSummary::ParamAccess> PendingParamAccesses; | ||||
6102 | |||||
6103 | while (true) { | ||||
6104 | Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks(); | ||||
6105 | if (!MaybeEntry) | ||||
6106 | return MaybeEntry.takeError(); | ||||
6107 | BitstreamEntry Entry = MaybeEntry.get(); | ||||
6108 | |||||
6109 | switch (Entry.Kind) { | ||||
6110 | case BitstreamEntry::SubBlock: // Handled for us already. | ||||
6111 | case BitstreamEntry::Error: | ||||
6112 | return error("Malformed block"); | ||||
6113 | case BitstreamEntry::EndBlock: | ||||
6114 | return Error::success(); | ||||
6115 | case BitstreamEntry::Record: | ||||
6116 | // The interesting case. | ||||
6117 | break; | ||||
6118 | } | ||||
6119 | |||||
6120 | // Read a record. The record format depends on whether this | ||||
6121 | // is a per-module index or a combined index file. In the per-module | ||||
6122 | // case the records contain the associated value's ID for correlation | ||||
6123 | // with VST entries. In the combined index the correlation is done | ||||
6124 | // via the bitcode offset of the summary records (which were saved | ||||
6125 | // in the combined index VST entries). The records also contain | ||||
6126 | // information used for ThinLTO renaming and importing. | ||||
6127 | Record.clear(); | ||||
6128 | Expected<unsigned> MaybeBitCode = Stream.readRecord(Entry.ID, Record); | ||||
6129 | if (!MaybeBitCode) | ||||
6130 | return MaybeBitCode.takeError(); | ||||
6131 | switch (unsigned BitCode = MaybeBitCode.get()) { | ||||
6132 | default: // Default behavior: ignore. | ||||
6133 | break; | ||||
6134 | case bitc::FS_FLAGS: { // [flags] | ||||
6135 | TheIndex.setFlags(Record[0]); | ||||
6136 | break; | ||||
6137 | } | ||||
6138 | case bitc::FS_VALUE_GUID: { // [valueid, refguid] | ||||
6139 | uint64_t ValueID = Record[0]; | ||||
6140 | GlobalValue::GUID RefGUID = Record[1]; | ||||
6141 | ValueIdToValueInfoMap[ValueID] = | ||||
6142 | std::make_pair(TheIndex.getOrInsertValueInfo(RefGUID), RefGUID); | ||||
6143 | break; | ||||
6144 | } | ||||
6145 | // FS_PERMODULE: [valueid, flags, instcount, fflags, numrefs, | ||||
6146 | // numrefs x valueid, n x (valueid)] | ||||
6147 | // FS_PERMODULE_PROFILE: [valueid, flags, instcount, fflags, numrefs, | ||||
6148 | // numrefs x valueid, | ||||
6149 | // n x (valueid, hotness)] | ||||
6150 | // FS_PERMODULE_RELBF: [valueid, flags, instcount, fflags, numrefs, | ||||
6151 | // numrefs x valueid, | ||||
6152 | // n x (valueid, relblockfreq)] | ||||
6153 | case bitc::FS_PERMODULE: | ||||
6154 | case bitc::FS_PERMODULE_RELBF: | ||||
6155 | case bitc::FS_PERMODULE_PROFILE: { | ||||
6156 | unsigned ValueID = Record[0]; | ||||
6157 | uint64_t RawFlags = Record[1]; | ||||
6158 | unsigned InstCount = Record[2]; | ||||
6159 | uint64_t RawFunFlags = 0; | ||||
6160 | unsigned NumRefs = Record[3]; | ||||
6161 | unsigned NumRORefs = 0, NumWORefs = 0; | ||||
6162 | int RefListStartIndex = 4; | ||||
6163 | if (Version >= 4) { | ||||
6164 | RawFunFlags = Record[3]; | ||||
6165 | NumRefs = Record[4]; | ||||
6166 | RefListStartIndex = 5; | ||||
6167 | if (Version >= 5) { | ||||
6168 | NumRORefs = Record[5]; | ||||
6169 | RefListStartIndex = 6; | ||||
6170 | if (Version >= 7) { | ||||
6171 | NumWORefs = Record[6]; | ||||
6172 | RefListStartIndex = 7; | ||||
6173 | } | ||||
6174 | } | ||||
6175 | } | ||||
6176 | |||||
6177 | auto Flags = getDecodedGVSummaryFlags(RawFlags, Version); | ||||
6178 | // The module path string ref set in the summary must be owned by the | ||||
6179 | // index's module string table. Since we don't have a module path | ||||
6180 | // string table section in the per-module index, we create a single | ||||
6181 | // module path string table entry with an empty (0) ID to take | ||||
6182 | // ownership. | ||||
6183 | int CallGraphEdgeStartIndex = RefListStartIndex + NumRefs; | ||||
6184 | assert(Record.size() >= RefListStartIndex + NumRefs &&(static_cast <bool> (Record.size() >= RefListStartIndex + NumRefs && "Record size inconsistent with number of references" ) ? void (0) : __assert_fail ("Record.size() >= RefListStartIndex + NumRefs && \"Record size inconsistent with number of references\"" , "/build/llvm-toolchain-snapshot-13~++20210621111111+acefe0eaaf82/llvm/lib/Bitcode/Reader/BitcodeReader.cpp" , 6185, __extension__ __PRETTY_FUNCTION__)) | ||||
6185 | "Record size inconsistent with number of references")(static_cast <bool> (Record.size() >= RefListStartIndex + NumRefs && "Record size inconsistent with number of references" ) ? void (0) : __assert_fail ("Record.size() >= RefListStartIndex + NumRefs && \"Record size inconsistent with number of references\"" , "/build/llvm-toolchain-snapshot-13~++20210621111111+acefe0eaaf82/llvm/lib/Bitcode/Reader/BitcodeReader.cpp" , 6185, __extension__ __PRETTY_FUNCTION__)); | ||||
6186 | std::vector<ValueInfo> Refs = makeRefList( | ||||
6187 | ArrayRef<uint64_t>(Record).slice(RefListStartIndex, NumRefs)); | ||||
6188 | bool HasProfile = (BitCode == bitc::FS_PERMODULE_PROFILE); | ||||
6189 | bool HasRelBF = (BitCode == bitc::FS_PERMODULE_RELBF); | ||||
6190 | std::vector<FunctionSummary::EdgeTy> Calls = makeCallList( | ||||
6191 | ArrayRef<uint64_t>(Record).slice(CallGraphEdgeStartIndex), | ||||
6192 | IsOldProfileFormat, HasProfile, HasRelBF); | ||||
6193 | setSpecialRefs(Refs, NumRORefs, NumWORefs); | ||||
6194 | auto FS = std::make_unique<FunctionSummary>( | ||||
6195 | Flags, InstCount, getDecodedFFlags(RawFunFlags), /*EntryCount=*/0, | ||||
6196 | std::move(Refs), std::move(Calls), std::move(PendingTypeTests), | ||||
6197 | std::move(PendingTypeTestAssumeVCalls), | ||||
6198 | std::move(PendingTypeCheckedLoadVCalls), | ||||
6199 | std::move(PendingTypeTestAssumeConstVCalls), | ||||
6200 | std::move(PendingTypeCheckedLoadConstVCalls), | ||||
6201 | std::move(PendingParamAccesses)); | ||||
6202 | auto VIAndOriginalGUID = getValueInfoFromValueId(ValueID); | ||||
6203 | FS->setModulePath(getThisModule()->first()); | ||||
6204 | FS->setOriginalName(VIAndOriginalGUID.second); | ||||
6205 | TheIndex.addGlobalValueSummary(VIAndOriginalGUID.first, std::move(FS)); | ||||
6206 | break; | ||||
6207 | } | ||||
6208 | // FS_ALIAS: [valueid, flags, valueid] | ||||
6209 | // Aliases must be emitted (and parsed) after all FS_PERMODULE entries, as | ||||
6210 | // they expect all aliasee summaries to be available. | ||||
6211 | case bitc::FS_ALIAS: { | ||||
6212 | unsigned ValueID = Record[0]; | ||||
6213 | uint64_t RawFlags = Record[1]; | ||||
6214 | unsigned AliaseeID = Record[2]; | ||||
6215 | auto Flags = getDecodedGVSummaryFlags(RawFlags, Version); | ||||
6216 | auto AS = std::make_unique<AliasSummary>(Flags); | ||||
6217 | // The module path string ref set in the summary must be owned by the | ||||
6218 | // index's module string table. Since we don't have a module path | ||||
6219 | // string table section in the per-module index, we create a single | ||||
6220 | // module path string table entry with an empty (0) ID to take | ||||
6221 | // ownership. | ||||
6222 | AS->setModulePath(getThisModule()->first()); | ||||
6223 | |||||
6224 | auto AliaseeVI = getValueInfoFromValueId(AliaseeID).first; | ||||
6225 | auto AliaseeInModule = TheIndex.findSummaryInModule(AliaseeVI, ModulePath); | ||||
6226 | if (!AliaseeInModule) | ||||
6227 | return error("Alias expects aliasee summary to be parsed"); | ||||
6228 | AS->setAliasee(AliaseeVI, AliaseeInModule); | ||||
6229 | |||||
6230 | auto GUID = getValueInfoFromValueId(ValueID); | ||||
6231 | AS->setOriginalName(GUID.second); | ||||
6232 | TheIndex.addGlobalValueSummary(GUID.first, std::move(AS)); | ||||
6233 | break; | ||||
6234 | } | ||||
6235 | // FS_PERMODULE_GLOBALVAR_INIT_REFS: [valueid, flags, varflags, n x valueid] | ||||
6236 | case bitc::FS_PERMODULE_GLOBALVAR_INIT_REFS: { | ||||
6237 | unsigned ValueID = Record[0]; | ||||
6238 | uint64_t RawFlags = Record[1]; | ||||
6239 | unsigned RefArrayStart = 2; | ||||
6240 | GlobalVarSummary::GVarFlags GVF(/* ReadOnly */ false, | ||||
6241 | /* WriteOnly */ false, | ||||
6242 | /* Constant */ false, | ||||
6243 | GlobalObject::VCallVisibilityPublic); | ||||
6244 | auto Flags = getDecodedGVSummaryFlags(RawFlags, Version); | ||||
6245 | if (Version >= 5) { | ||||
6246 | GVF = getDecodedGVarFlags(Record[2]); | ||||
6247 | RefArrayStart = 3; | ||||
6248 | } | ||||
6249 | std::vector<ValueInfo> Refs = | ||||
6250 | makeRefList(ArrayRef<uint64_t>(Record).slice(RefArrayStart)); | ||||
6251 | auto FS = | ||||
6252 | std::make_unique<GlobalVarSummary>(Flags, GVF, std::move(Refs)); | ||||
6253 | FS->setModulePath(getThisModule()->first()); | ||||
6254 | auto GUID = getValueInfoFromValueId(ValueID); | ||||
6255 | FS->setOriginalName(GUID.second); | ||||
6256 | TheIndex.addGlobalValueSummary(GUID.first, std::move(FS)); | ||||
6257 | break; | ||||
6258 | } | ||||
6259 | // FS_PERMODULE_VTABLE_GLOBALVAR_INIT_REFS: [valueid, flags, varflags, | ||||
6260 | // numrefs, numrefs x valueid, | ||||
6261 | // n x (valueid, offset)] | ||||
6262 | case bitc::FS_PERMODULE_VTABLE_GLOBALVAR_INIT_REFS: { | ||||
6263 | unsigned ValueID = Record[0]; | ||||
6264 | uint64_t RawFlags = Record[1]; | ||||
6265 | GlobalVarSummary::GVarFlags GVF = getDecodedGVarFlags(Record[2]); | ||||
6266 | unsigned NumRefs = Record[3]; | ||||
6267 | unsigned RefListStartIndex = 4; | ||||
6268 | unsigned VTableListStartIndex = RefListStartIndex + NumRefs; | ||||
6269 | auto Flags = getDecodedGVSummaryFlags(RawFlags, Version); | ||||
6270 | std::vector<ValueInfo> Refs = makeRefList( | ||||
6271 | ArrayRef<uint64_t>(Record).slice(RefListStartIndex, NumRefs)); | ||||
6272 | VTableFuncList VTableFuncs; | ||||
6273 | for (unsigned I = VTableListStartIndex, E = Record.size(); I != E; ++I) { | ||||
6274 | ValueInfo Callee = getValueInfoFromValueId(Record[I]).first; | ||||
6275 | uint64_t Offset = Record[++I]; | ||||
6276 | VTableFuncs.push_back({Callee, Offset}); | ||||
6277 | } | ||||
6278 | auto VS = | ||||
6279 | std::make_unique<GlobalVarSummary>(Flags, GVF, std::move(Refs)); | ||||
6280 | VS->setModulePath(getThisModule()->first()); | ||||
6281 | VS->setVTableFuncs(VTableFuncs); | ||||
6282 | auto GUID = getValueInfoFromValueId(ValueID); | ||||
6283 | VS->setOriginalName(GUID.second); | ||||
6284 | TheIndex.addGlobalValueSummary(GUID.first, std::move(VS)); | ||||
6285 | break; | ||||
6286 | } | ||||
6287 | // FS_COMBINED: [valueid, modid, flags, instcount, fflags, numrefs, | ||||
6288 | // numrefs x valueid, n x (valueid)] | ||||
6289 | // FS_COMBINED_PROFILE: [valueid, modid, flags, instcount, fflags, numrefs, | ||||
6290 | // numrefs x valueid, n x (valueid, hotness)] | ||||
6291 | case bitc::FS_COMBINED: | ||||
6292 | case bitc::FS_COMBINED_PROFILE: { | ||||
6293 | unsigned ValueID = Record[0]; | ||||
6294 | uint64_t ModuleId = Record[1]; | ||||
6295 | uint64_t RawFlags = Record[2]; | ||||
6296 | unsigned InstCount = Record[3]; | ||||
6297 | uint64_t RawFunFlags = 0; | ||||
6298 | uint64_t EntryCount = 0; | ||||
6299 | unsigned NumRefs = Record[4]; | ||||
6300 | unsigned NumRORefs = 0, NumWORefs = 0; | ||||
6301 | int RefListStartIndex = 5; | ||||
6302 | |||||
6303 | if (Version >= 4) { | ||||
6304 | RawFunFlags = Record[4]; | ||||
6305 | RefListStartIndex = 6; | ||||
6306 | size_t NumRefsIndex = 5; | ||||
6307 | if (Version >= 5) { | ||||
6308 | unsigned NumRORefsOffset = 1; | ||||
6309 | RefListStartIndex = 7; | ||||
6310 | if (Version >= 6) { | ||||
6311 | NumRefsIndex = 6; | ||||
6312 | EntryCount = Record[5]; | ||||
6313 | RefListStartIndex = 8; | ||||
6314 | if (Version >= 7) { | ||||
6315 | RefListStartIndex = 9; | ||||
6316 | NumWORefs = Record[8]; | ||||
6317 | NumRORefsOffset = 2; | ||||
6318 | } | ||||
6319 | } | ||||
6320 | NumRORefs = Record[RefListStartIndex - NumRORefsOffset]; | ||||
6321 | } | ||||
6322 | NumRefs = Record[NumRefsIndex]; | ||||
6323 | } | ||||
6324 | |||||
6325 | auto Flags = getDecodedGVSummaryFlags(RawFlags, Version); | ||||
6326 | int CallGraphEdgeStartIndex = RefListStartIndex + NumRefs; | ||||
6327 | assert(Record.size() >= RefListStartIndex + NumRefs &&(static_cast <bool> (Record.size() >= RefListStartIndex + NumRefs && "Record size inconsistent with number of references" ) ? void (0) : __assert_fail ("Record.size() >= RefListStartIndex + NumRefs && \"Record size inconsistent with number of references\"" , "/build/llvm-toolchain-snapshot-13~++20210621111111+acefe0eaaf82/llvm/lib/Bitcode/Reader/BitcodeReader.cpp" , 6328, __extension__ __PRETTY_FUNCTION__)) | ||||
6328 | "Record size inconsistent with number of references")(static_cast <bool> (Record.size() >= RefListStartIndex + NumRefs && "Record size inconsistent with number of references" ) ? void (0) : __assert_fail ("Record.size() >= RefListStartIndex + NumRefs && \"Record size inconsistent with number of references\"" , "/build/llvm-toolchain-snapshot-13~++20210621111111+acefe0eaaf82/llvm/lib/Bitcode/Reader/BitcodeReader.cpp" , 6328, __extension__ __PRETTY_FUNCTION__)); | ||||
6329 | std::vector<ValueInfo> Refs = makeRefList( | ||||
6330 | ArrayRef<uint64_t>(Record).slice(RefListStartIndex, NumRefs)); | ||||
6331 | bool HasProfile = (BitCode == bitc::FS_COMBINED_PROFILE); | ||||
6332 | std::vector<FunctionSummary::EdgeTy> Edges = makeCallList( | ||||
6333 | ArrayRef<uint64_t>(Record).slice(CallGraphEdgeStartIndex), | ||||
6334 | IsOldProfileFormat, HasProfile, false); | ||||
6335 | ValueInfo VI = getValueInfoFromValueId(ValueID).first; | ||||
6336 | setSpecialRefs(Refs, NumRORefs, NumWORefs); | ||||
6337 | auto FS = std::make_unique<FunctionSummary>( | ||||
6338 | Flags, InstCount, getDecodedFFlags(RawFunFlags), EntryCount, | ||||
6339 | std::move(Refs), std::move(Edges), std::move(PendingTypeTests), | ||||
6340 | std::move(PendingTypeTestAssumeVCalls), | ||||
6341 | std::move(PendingTypeCheckedLoadVCalls), | ||||
6342 | std::move(PendingTypeTestAssumeConstVCalls), | ||||
6343 | std::move(PendingTypeCheckedLoadConstVCalls), | ||||
6344 | std::move(PendingParamAccesses)); | ||||
6345 | LastSeenSummary = FS.get(); | ||||
6346 | LastSeenGUID = VI.getGUID(); | ||||
6347 | FS->setModulePath(ModuleIdMap[ModuleId]); | ||||
6348 | TheIndex.addGlobalValueSummary(VI, std::move(FS)); | ||||
6349 | break; | ||||
6350 | } | ||||
6351 | // FS_COMBINED_ALIAS: [valueid, modid, flags, valueid] | ||||
6352 | // Aliases must be emitted (and parsed) after all FS_COMBINED entries, as | ||||
6353 | // they expect all aliasee summaries to be available. | ||||
6354 | case bitc::FS_COMBINED_ALIAS: { | ||||
6355 | unsigned ValueID = Record[0]; | ||||
6356 | uint64_t ModuleId = Record[1]; | ||||
6357 | uint64_t RawFlags = Record[2]; | ||||
6358 | unsigned AliaseeValueId = Record[3]; | ||||
6359 | auto Flags = getDecodedGVSummaryFlags(RawFlags, Version); | ||||
6360 | auto AS = std::make_unique<AliasSummary>(Flags); | ||||
6361 | LastSeenSummary = AS.get(); | ||||
6362 | AS->setModulePath(ModuleIdMap[ModuleId]); | ||||
6363 | |||||
6364 | auto AliaseeVI = getValueInfoFromValueId(AliaseeValueId).first; | ||||
6365 | auto AliaseeInModule = TheIndex.findSummaryInModule(AliaseeVI, AS->modulePath()); | ||||
6366 | AS->setAliasee(AliaseeVI, AliaseeInModule); | ||||
6367 | |||||
6368 | ValueInfo VI = getValueInfoFromValueId(ValueID).first; | ||||
6369 | LastSeenGUID = VI.getGUID(); | ||||
6370 | TheIndex.addGlobalValueSummary(VI, std::move(AS)); | ||||
6371 | break; | ||||
6372 | } | ||||
6373 | // FS_COMBINED_GLOBALVAR_INIT_REFS: [valueid, modid, flags, n x valueid] | ||||
6374 | case bitc::FS_COMBINED_GLOBALVAR_INIT_REFS: { | ||||
6375 | unsigned ValueID = Record[0]; | ||||
6376 | uint64_t ModuleId = Record[1]; | ||||
6377 | uint64_t RawFlags = Record[2]; | ||||
6378 | unsigned RefArrayStart = 3; | ||||
6379 | GlobalVarSummary::GVarFlags GVF(/* ReadOnly */ false, | ||||
6380 | /* WriteOnly */ false, | ||||
6381 | /* Constant */ false, | ||||
6382 | GlobalObject::VCallVisibilityPublic); | ||||
6383 | auto Flags = getDecodedGVSummaryFlags(RawFlags, Version); | ||||
6384 | if (Version >= 5) { | ||||
6385 | GVF = getDecodedGVarFlags(Record[3]); | ||||
6386 | RefArrayStart = 4; | ||||
6387 | } | ||||
6388 | std::vector<ValueInfo> Refs = | ||||
6389 | makeRefList(ArrayRef<uint64_t>(Record).slice(RefArrayStart)); | ||||
6390 | auto FS = | ||||
6391 | std::make_unique<GlobalVarSummary>(Flags, GVF, std::move(Refs)); | ||||
6392 | LastSeenSummary = FS.get(); | ||||
6393 | FS->setModulePath(ModuleIdMap[ModuleId]); | ||||
6394 | ValueInfo VI = getValueInfoFromValueId(ValueID).first; | ||||
6395 | LastSeenGUID = VI.getGUID(); | ||||
6396 | TheIndex.addGlobalValueSummary(VI, std::move(FS)); | ||||
6397 | break; | ||||
6398 | } | ||||
6399 | // FS_COMBINED_ORIGINAL_NAME: [original_name] | ||||
6400 | case bitc::FS_COMBINED_ORIGINAL_NAME: { | ||||
6401 | uint64_t OriginalName = Record[0]; | ||||
6402 | if (!LastSeenSummary) | ||||
6403 | return error("Name attachment that does not follow a combined record"); | ||||
6404 | LastSeenSummary->setOriginalName(OriginalName); | ||||
6405 | TheIndex.addOriginalName(LastSeenGUID, OriginalName); | ||||
6406 | // Reset the LastSeenSummary | ||||
6407 | LastSeenSummary = nullptr; | ||||
6408 | LastSeenGUID = 0; | ||||
6409 | break; | ||||
6410 | } | ||||
6411 | case bitc::FS_TYPE_TESTS: | ||||
6412 | assert(PendingTypeTests.empty())(static_cast <bool> (PendingTypeTests.empty()) ? void ( 0) : __assert_fail ("PendingTypeTests.empty()", "/build/llvm-toolchain-snapshot-13~++20210621111111+acefe0eaaf82/llvm/lib/Bitcode/Reader/BitcodeReader.cpp" , 6412, __extension__ __PRETTY_FUNCTION__)); | ||||
6413 | llvm::append_range(PendingTypeTests, Record); | ||||
6414 | break; | ||||
6415 | |||||
6416 | case bitc::FS_TYPE_TEST_ASSUME_VCALLS: | ||||
6417 | assert(PendingTypeTestAssumeVCalls.empty())(static_cast <bool> (PendingTypeTestAssumeVCalls.empty( )) ? void (0) : __assert_fail ("PendingTypeTestAssumeVCalls.empty()" , "/build/llvm-toolchain-snapshot-13~++20210621111111+acefe0eaaf82/llvm/lib/Bitcode/Reader/BitcodeReader.cpp" , 6417, __extension__ __PRETTY_FUNCTION__)); | ||||
6418 | for (unsigned I = 0; I != Record.size(); I += 2) | ||||
6419 | PendingTypeTestAssumeVCalls.push_back({Record[I], Record[I+1]}); | ||||
6420 | break; | ||||
6421 | |||||
6422 | case bitc::FS_TYPE_CHECKED_LOAD_VCALLS: | ||||
6423 | assert(PendingTypeCheckedLoadVCalls.empty())(static_cast <bool> (PendingTypeCheckedLoadVCalls.empty ()) ? void (0) : __assert_fail ("PendingTypeCheckedLoadVCalls.empty()" , "/build/llvm-toolchain-snapshot-13~++20210621111111+acefe0eaaf82/llvm/lib/Bitcode/Reader/BitcodeReader.cpp" , 6423, __extension__ __PRETTY_FUNCTION__)); | ||||
6424 | for (unsigned I = 0; I != Record.size(); I += 2) | ||||
6425 | PendingTypeCheckedLoadVCalls.push_back({Record[I], Record[I+1]}); | ||||
6426 | break; | ||||
6427 | |||||
6428 | case bitc::FS_TYPE_TEST_ASSUME_CONST_VCALL: | ||||
6429 | PendingTypeTestAssumeConstVCalls.push_back( | ||||
6430 | {{Record[0], Record[1]}, {Record.begin() + 2, Record.end()}}); | ||||
6431 | break; | ||||
6432 | |||||
6433 | case bitc::FS_TYPE_CHECKED_LOAD_CONST_VCALL: | ||||
6434 | PendingTypeCheckedLoadConstVCalls.push_back( | ||||
6435 | {{Record[0], Record[1]}, {Record.begin() + 2, Record.end()}}); | ||||
6436 | break; | ||||
6437 | |||||
6438 | case bitc::FS_CFI_FUNCTION_DEFS: { | ||||
6439 | std::set<std::string> &CfiFunctionDefs = TheIndex.cfiFunctionDefs(); | ||||
6440 | for (unsigned I = 0; I != Record.size(); I += 2) | ||||
6441 | CfiFunctionDefs.insert( | ||||
6442 | {Strtab.data() + Record[I], static_cast<size_t>(Record[I + 1])}); | ||||
6443 | break; | ||||
6444 | } | ||||
6445 | |||||
6446 | case bitc::FS_CFI_FUNCTION_DECLS: { | ||||
6447 | std::set<std::string> &CfiFunctionDecls = TheIndex.cfiFunctionDecls(); | ||||
6448 | for (unsigned I = 0; I != Record.size(); I += 2) | ||||
6449 | CfiFunctionDecls.insert( | ||||
6450 | {Strtab.data() + Record[I], static_cast<size_t>(Record[I + 1])}); | ||||
6451 | break; | ||||
6452 | } | ||||
6453 | |||||
6454 | case bitc::FS_TYPE_ID: | ||||
6455 | parseTypeIdSummaryRecord(Record, Strtab, TheIndex); | ||||
6456 | break; | ||||
6457 | |||||
6458 | case bitc::FS_TYPE_ID_METADATA: | ||||
6459 | parseTypeIdCompatibleVtableSummaryRecord(Record); | ||||
6460 | break; | ||||
6461 | |||||
6462 | case bitc::FS_BLOCK_COUNT: | ||||
6463 | TheIndex.addBlockCount(Record[0]); | ||||
6464 | break; | ||||
6465 | |||||
6466 | case bitc::FS_PARAM_ACCESS: { | ||||
6467 | PendingParamAccesses = parseParamAccesses(Record); | ||||
6468 | break; | ||||
6469 | } | ||||
6470 | } | ||||
6471 | } | ||||
6472 | llvm_unreachable("Exit infinite loop")::llvm::llvm_unreachable_internal("Exit infinite loop", "/build/llvm-toolchain-snapshot-13~++20210621111111+acefe0eaaf82/llvm/lib/Bitcode/Reader/BitcodeReader.cpp" , 6472); | ||||
6473 | } | ||||
6474 | |||||
6475 | // Parse the module string table block into the Index. | ||||
6476 | // This populates the ModulePathStringTable map in the index. | ||||
6477 | Error ModuleSummaryIndexBitcodeReader::parseModuleStringTable() { | ||||
6478 | if (Error Err = Stream.EnterSubBlock(bitc::MODULE_STRTAB_BLOCK_ID)) | ||||
6479 | return Err; | ||||
6480 | |||||
6481 | SmallVector<uint64_t, 64> Record; | ||||
6482 | |||||
6483 | SmallString<128> ModulePath; | ||||
6484 | ModuleSummaryIndex::ModuleInfo *LastSeenModule = nullptr; | ||||
6485 | |||||
6486 | while (true) { | ||||
6487 | Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks(); | ||||
6488 | if (!MaybeEntry) | ||||
6489 | return MaybeEntry.takeError(); | ||||
6490 | BitstreamEntry Entry = MaybeEntry.get(); | ||||
6491 | |||||
6492 | switch (Entry.Kind) { | ||||
6493 | case BitstreamEntry::SubBlock: // Handled for us already. | ||||
6494 | case BitstreamEntry::Error: | ||||
6495 | return error("Malformed block"); | ||||
6496 | case BitstreamEntry::EndBlock: | ||||
6497 | return Error::success(); | ||||
6498 | case BitstreamEntry::Record: | ||||
6499 | // The interesting case. | ||||
6500 | break; | ||||
6501 | } | ||||
6502 | |||||
6503 | Record.clear(); | ||||
6504 | Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record); | ||||
6505 | if (!MaybeRecord) | ||||
6506 | return MaybeRecord.takeError(); | ||||
6507 | switch (MaybeRecord.get()) { | ||||
6508 | default: // Default behavior: ignore. | ||||
6509 | break; | ||||
6510 | case bitc::MST_CODE_ENTRY: { | ||||
6511 | // MST_ENTRY: [modid, namechar x N] | ||||
6512 | uint64_t ModuleId = Record[0]; | ||||
6513 | |||||
6514 | if (convertToString(Record, 1, ModulePath)) | ||||
6515 | return error("Invalid record"); | ||||
6516 | |||||
6517 | LastSeenModule = TheIndex.addModule(ModulePath, ModuleId); | ||||
6518 | ModuleIdMap[ModuleId] = LastSeenModule->first(); | ||||
6519 | |||||
6520 | ModulePath.clear(); | ||||
6521 | break; | ||||
6522 | } | ||||
6523 | /// MST_CODE_HASH: [5*i32] | ||||
6524 | case bitc::MST_CODE_HASH: { | ||||
6525 | if (Record.size() != 5) | ||||
6526 | return error("Invalid hash length " + Twine(Record.size()).str()); | ||||
6527 | if (!LastSeenModule) | ||||
6528 | return error("Invalid hash that does not follow a module path"); | ||||
6529 | int Pos = 0; | ||||
6530 | for (auto &Val : Record) { | ||||
6531 | assert(!(Val >> 32) && "Unexpected high bits set")(static_cast <bool> (!(Val >> 32) && "Unexpected high bits set" ) ? void (0) : __assert_fail ("!(Val >> 32) && \"Unexpected high bits set\"" , "/build/llvm-toolchain-snapshot-13~++20210621111111+acefe0eaaf82/llvm/lib/Bitcode/Reader/BitcodeReader.cpp" , 6531, __extension__ __PRETTY_FUNCTION__)); | ||||
6532 | LastSeenModule->second.second[Pos++] = Val; | ||||
6533 | } | ||||
6534 | // Reset LastSeenModule to avoid overriding the hash unexpectedly. | ||||
6535 | LastSeenModule = nullptr; | ||||
6536 | break; | ||||
6537 | } | ||||
6538 | } | ||||
6539 | } | ||||
6540 | llvm_unreachable("Exit infinite loop")::llvm::llvm_unreachable_internal("Exit infinite loop", "/build/llvm-toolchain-snapshot-13~++20210621111111+acefe0eaaf82/llvm/lib/Bitcode/Reader/BitcodeReader.cpp" , 6540); | ||||
6541 | } | ||||
6542 | |||||
6543 | namespace { | ||||
6544 | |||||
6545 | // FIXME: This class is only here to support the transition to llvm::Error. It | ||||
6546 | // will be removed once this transition is complete. Clients should prefer to | ||||
6547 | // deal with the Error value directly, rather than converting to error_code. | ||||
6548 | class BitcodeErrorCategoryType : public std::error_category { | ||||
6549 | const char *name() const noexcept override { | ||||
6550 | return "llvm.bitcode"; | ||||
6551 | } | ||||
6552 | |||||
6553 | std::string message(int IE) const override { | ||||
6554 | BitcodeError E = static_cast<BitcodeError>(IE); | ||||
6555 | switch (E) { | ||||
6556 | case BitcodeError::CorruptedBitcode: | ||||
6557 | return "Corrupted bitcode"; | ||||
6558 | } | ||||
6559 | llvm_unreachable("Unknown error type!")::llvm::llvm_unreachable_internal("Unknown error type!", "/build/llvm-toolchain-snapshot-13~++20210621111111+acefe0eaaf82/llvm/lib/Bitcode/Reader/BitcodeReader.cpp" , 6559); | ||||
6560 | } | ||||
6561 | }; | ||||
6562 | |||||
6563 | } // end anonymous namespace | ||||
6564 | |||||
6565 | static ManagedStatic<BitcodeErrorCategoryType> ErrorCategory; | ||||
6566 | |||||
6567 | const std::error_category &llvm::BitcodeErrorCategory() { | ||||
6568 | return *ErrorCategory; | ||||
6569 | } | ||||
6570 | |||||
6571 | static Expected<StringRef> readBlobInRecord(BitstreamCursor &Stream, | ||||
6572 | unsigned Block, unsigned RecordID) { | ||||
6573 | if (Error Err = Stream.EnterSubBlock(Block)) | ||||
6574 | return std::move(Err); | ||||
6575 | |||||
6576 | StringRef Strtab; | ||||
6577 | while (true) { | ||||
6578 | Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); | ||||
6579 | if (!MaybeEntry) | ||||
6580 | return MaybeEntry.takeError(); | ||||
6581 | llvm::BitstreamEntry Entry = MaybeEntry.get(); | ||||
6582 | |||||
6583 | switch (Entry.Kind) { | ||||
6584 | case BitstreamEntry::EndBlock: | ||||
6585 | return Strtab; | ||||
6586 | |||||
6587 | case BitstreamEntry::Error: | ||||
6588 | return error("Malformed block"); | ||||
6589 | |||||
6590 | case BitstreamEntry::SubBlock: | ||||
6591 | if (Error Err = Stream.SkipBlock()) | ||||
6592 | return std::move(Err); | ||||
6593 | break; | ||||
6594 | |||||
6595 | case BitstreamEntry::Record: | ||||
6596 | StringRef Blob; | ||||
6597 | SmallVector<uint64_t, 1> Record; | ||||
6598 | Expected<unsigned> MaybeRecord = | ||||
6599 | Stream.readRecord(Entry.ID, Record, &Blob); | ||||
6600 | if (!MaybeRecord) | ||||
6601 | return MaybeRecord.takeError(); | ||||
6602 | if (MaybeRecord.get() == RecordID) | ||||
6603 | Strtab = Blob; | ||||
6604 | break; | ||||
6605 | } | ||||
6606 | } | ||||
6607 | } | ||||
6608 | |||||
6609 | //===----------------------------------------------------------------------===// | ||||
6610 | // External interface | ||||
6611 | //===----------------------------------------------------------------------===// | ||||
6612 | |||||
6613 | Expected<std::vector<BitcodeModule>> | ||||
6614 | llvm::getBitcodeModuleList(MemoryBufferRef Buffer) { | ||||
6615 | auto FOrErr = getBitcodeFileContents(Buffer); | ||||
6616 | if (!FOrErr) | ||||
6617 | return FOrErr.takeError(); | ||||
6618 | return std::move(FOrErr->Mods); | ||||
6619 | } | ||||
6620 | |||||
6621 | Expected<BitcodeFileContents> | ||||
6622 | llvm::getBitcodeFileContents(MemoryBufferRef Buffer) { | ||||
6623 | Expected<BitstreamCursor> StreamOrErr = initStream(Buffer); | ||||
6624 | if (!StreamOrErr) | ||||
6625 | return StreamOrErr.takeError(); | ||||
6626 | BitstreamCursor &Stream = *StreamOrErr; | ||||
6627 | |||||
6628 | BitcodeFileContents F; | ||||
6629 | while (true) { | ||||
6630 | uint64_t BCBegin = Stream.getCurrentByteNo(); | ||||
6631 | |||||
6632 | // We may be consuming bitcode from a client that leaves garbage at the end | ||||
6633 | // of the bitcode stream (e.g. Apple's ar tool). If we are close enough to | ||||
6634 | // the end that there cannot possibly be another module, stop looking. | ||||
6635 | if (BCBegin + 8 >= Stream.getBitcodeBytes().size()) | ||||
6636 | return F; | ||||
6637 | |||||
6638 | Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); | ||||
6639 | if (!MaybeEntry) | ||||
6640 | return MaybeEntry.takeError(); | ||||
6641 | llvm::BitstreamEntry Entry = MaybeEntry.get(); | ||||
6642 | |||||
6643 | switch (Entry.Kind) { | ||||
6644 | case BitstreamEntry::EndBlock: | ||||
6645 | case BitstreamEntry::Error: | ||||
6646 | return error("Malformed block"); | ||||
6647 | |||||
6648 | case BitstreamEntry::SubBlock: { | ||||
6649 | uint64_t IdentificationBit = -1ull; | ||||
6650 | if (Entry.ID == bitc::IDENTIFICATION_BLOCK_ID) { | ||||
6651 | IdentificationBit = Stream.GetCurrentBitNo() - BCBegin * 8; | ||||
6652 | if (Error Err = Stream.SkipBlock()) | ||||
6653 | return std::move(Err); | ||||
6654 | |||||
6655 | { | ||||
6656 | Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); | ||||
6657 | if (!MaybeEntry) | ||||
6658 | return MaybeEntry.takeError(); | ||||
6659 | Entry = MaybeEntry.get(); | ||||
6660 | } | ||||
6661 | |||||
6662 | if (Entry.Kind != BitstreamEntry::SubBlock || | ||||
6663 | Entry.ID != bitc::MODULE_BLOCK_ID) | ||||
6664 | return error("Malformed block"); | ||||
6665 | } | ||||
6666 | |||||
6667 | if (Entry.ID == bitc::MODULE_BLOCK_ID) { | ||||
6668 | uint64_t ModuleBit = Stream.GetCurrentBitNo() - BCBegin * 8; | ||||
6669 | if (Error Err = Stream.SkipBlock()) | ||||
6670 | return std::move(Err); | ||||
6671 | |||||
6672 | F.Mods.push_back({Stream.getBitcodeBytes().slice( | ||||
6673 | BCBegin, Stream.getCurrentByteNo() - BCBegin), | ||||
6674 | Buffer.getBufferIdentifier(), IdentificationBit, | ||||
6675 | ModuleBit}); | ||||
6676 | continue; | ||||
6677 | } | ||||
6678 | |||||
6679 | if (Entry.ID == bitc::STRTAB_BLOCK_ID) { | ||||
6680 | Expected<StringRef> Strtab = | ||||
6681 | readBlobInRecord(Stream, bitc::STRTAB_BLOCK_ID, bitc::STRTAB_BLOB); | ||||
6682 | if (!Strtab) | ||||
6683 | return Strtab.takeError(); | ||||
6684 | // This string table is used by every preceding bitcode module that does | ||||
6685 | // not have its own string table. A bitcode file may have multiple | ||||
6686 | // string tables if it was created by binary concatenation, for example | ||||
6687 | // with "llvm-cat -b". | ||||
6688 | for (auto I = F.Mods.rbegin(), E = F.Mods.rend(); I != E; ++I) { | ||||
6689 | if (!I->Strtab.empty()) | ||||
6690 | break; | ||||
6691 | I->Strtab = *Strtab; | ||||
6692 | } | ||||
6693 | // Similarly, the string table is used by every preceding symbol table; | ||||
6694 | // normally there will be just one unless the bitcode file was created | ||||
6695 | // by binary concatenation. | ||||
6696 | if (!F.Symtab.empty() && F.StrtabForSymtab.empty()) | ||||
6697 | F.StrtabForSymtab = *Strtab; | ||||
6698 | continue; | ||||
6699 | } | ||||
6700 | |||||
6701 | if (Entry.ID == bitc::SYMTAB_BLOCK_ID) { | ||||
6702 | Expected<StringRef> SymtabOrErr = | ||||
6703 | readBlobInRecord(Stream, bitc::SYMTAB_BLOCK_ID, bitc::SYMTAB_BLOB); | ||||
6704 | if (!SymtabOrErr) | ||||
6705 | return SymtabOrErr.takeError(); | ||||
6706 | |||||
6707 | // We can expect the bitcode file to have multiple symbol tables if it | ||||
6708 | // was created by binary concatenation. In that case we silently | ||||
6709 | // ignore any subsequent symbol tables, which is fine because this is a | ||||
6710 | // low level function. The client is expected to notice that the number | ||||
6711 | // of modules in the symbol table does not match the number of modules | ||||
6712 | // in the input file and regenerate the symbol table. | ||||
6713 | if (F.Symtab.empty()) | ||||
6714 | F.Symtab = *SymtabOrErr; | ||||
6715 | continue; | ||||
6716 | } | ||||
6717 | |||||
6718 | if (Error Err = Stream.SkipBlock()) | ||||
6719 | return std::move(Err); | ||||
6720 | continue; | ||||
6721 | } | ||||
6722 | case BitstreamEntry::Record: | ||||
6723 | if (Expected<unsigned> StreamFailed = Stream.skipRecord(Entry.ID)) | ||||
6724 | continue; | ||||
6725 | else | ||||
6726 | return StreamFailed.takeError(); | ||||
6727 | } | ||||
6728 | } | ||||
6729 | } | ||||
6730 | |||||
6731 | /// Get a lazy one-at-time loading module from bitcode. | ||||
6732 | /// | ||||
6733 | /// This isn't always used in a lazy context. In particular, it's also used by | ||||
6734 | /// \a parseModule(). If this is truly lazy, then we need to eagerly pull | ||||
6735 | /// in forward-referenced functions from block address references. | ||||
6736 | /// | ||||
6737 | /// \param[in] MaterializeAll Set to \c true if we should materialize | ||||
6738 | /// everything. | ||||
6739 | Expected<std::unique_ptr<Module>> | ||||
6740 | BitcodeModule::getModuleImpl(LLVMContext &Context, bool MaterializeAll, | ||||
6741 | bool ShouldLazyLoadMetadata, bool IsImporting, | ||||
6742 | DataLayoutCallbackTy DataLayoutCallback) { | ||||
6743 | BitstreamCursor Stream(Buffer); | ||||
6744 | |||||
6745 | std::string ProducerIdentification; | ||||
6746 | if (IdentificationBit != -1ull) { | ||||
6747 | if (Error JumpFailed = Stream.JumpToBit(IdentificationBit)) | ||||
6748 | return std::move(JumpFailed); | ||||
6749 | Expected<std::string> ProducerIdentificationOrErr = | ||||
6750 | readIdentificationBlock(Stream); | ||||
6751 | if (!ProducerIdentificationOrErr) | ||||
6752 | return ProducerIdentificationOrErr.takeError(); | ||||
6753 | |||||
6754 | ProducerIdentification = *ProducerIdentificationOrErr; | ||||
6755 | } | ||||
6756 | |||||
6757 | if (Error JumpFailed = Stream.JumpToBit(ModuleBit)) | ||||
6758 | return std::move(JumpFailed); | ||||
6759 | auto *R = new BitcodeReader(std::move(Stream), Strtab, ProducerIdentification, | ||||
6760 | Context); | ||||
6761 | |||||
6762 | std::unique_ptr<Module> M = | ||||
6763 | std::make_unique<Module>(ModuleIdentifier, Context); | ||||
6764 | M->setMaterializer(R); | ||||
6765 | |||||
6766 | // Delay parsing Metadata if ShouldLazyLoadMetadata is true. | ||||
6767 | if (Error Err = R->parseBitcodeInto(M.get(), ShouldLazyLoadMetadata, | ||||
6768 | IsImporting, DataLayoutCallback)) | ||||
6769 | return std::move(Err); | ||||
6770 | |||||
6771 | if (MaterializeAll) { | ||||
6772 | // Read in the entire module, and destroy the BitcodeReader. | ||||
6773 | if (Error Err = M->materializeAll()) | ||||
6774 | return std::move(Err); | ||||
6775 | } else { | ||||
6776 | // Resolve forward references from blockaddresses. | ||||
6777 | if (Error Err = R->materializeForwardReferencedFunctions()) | ||||
6778 | return std::move(Err); | ||||
6779 | } | ||||
6780 | return std::move(M); | ||||
6781 | } | ||||
6782 | |||||
6783 | Expected<std::unique_ptr<Module>> | ||||
6784 | BitcodeModule::getLazyModule(LLVMContext &Context, bool ShouldLazyLoadMetadata, | ||||
6785 | bool IsImporting) { | ||||
6786 | return getModuleImpl(Context, false, ShouldLazyLoadMetadata, IsImporting, | ||||
6787 | [](StringRef) { return None; }); | ||||
6788 | } | ||||
6789 | |||||
6790 | // Parse the specified bitcode buffer and merge the index into CombinedIndex. | ||||
6791 | // We don't use ModuleIdentifier here because the client may need to control the | ||||
6792 | // module path used in the combined summary (e.g. when reading summaries for | ||||
6793 | // regular LTO modules). | ||||
6794 | Error BitcodeModule::readSummary(ModuleSummaryIndex &CombinedIndex, | ||||
6795 | StringRef ModulePath, uint64_t ModuleId) { | ||||
6796 | BitstreamCursor Stream(Buffer); | ||||
6797 | if (Error JumpFailed = Stream.JumpToBit(ModuleBit)) | ||||
6798 | return JumpFailed; | ||||
6799 | |||||
6800 | ModuleSummaryIndexBitcodeReader R(std::move(Stream), Strtab, CombinedIndex, | ||||
6801 | ModulePath, ModuleId); | ||||
6802 | return R.parseModule(); | ||||
6803 | } | ||||
6804 | |||||
6805 | // Parse the specified bitcode buffer, returning the function info index. | ||||
6806 | Expected<std::unique_ptr<ModuleSummaryIndex>> BitcodeModule::getSummary() { | ||||
6807 | BitstreamCursor Stream(Buffer); | ||||
6808 | if (Error JumpFailed = Stream.JumpToBit(ModuleBit)) | ||||
6809 | return std::move(JumpFailed); | ||||
6810 | |||||
6811 | auto Index = std::make_unique<ModuleSummaryIndex>(/*HaveGVs=*/false); | ||||
6812 | ModuleSummaryIndexBitcodeReader R(std::move(Stream), Strtab, *Index, | ||||
6813 | ModuleIdentifier, 0); | ||||
6814 | |||||
6815 | if (Error Err = R.parseModule()) | ||||
6816 | return std::move(Err); | ||||
6817 | |||||
6818 | return std::move(Index); | ||||
6819 | } | ||||
6820 | |||||
6821 | static Expected<bool> getEnableSplitLTOUnitFlag(BitstreamCursor &Stream, | ||||
6822 | unsigned ID) { | ||||
6823 | if (Error Err = Stream.EnterSubBlock(ID)) | ||||
6824 | return std::move(Err); | ||||
6825 | SmallVector<uint64_t, 64> Record; | ||||
6826 | |||||
6827 | while (true) { | ||||
6828 | Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks(); | ||||
6829 | if (!MaybeEntry) | ||||
6830 | return MaybeEntry.takeError(); | ||||
6831 | BitstreamEntry Entry = MaybeEntry.get(); | ||||
6832 | |||||
6833 | switch (Entry.Kind) { | ||||
6834 | case BitstreamEntry::SubBlock: // Handled for us already. | ||||
6835 | case BitstreamEntry::Error: | ||||
6836 | return error("Malformed block"); | ||||
6837 | case BitstreamEntry::EndBlock: | ||||
6838 | // If no flags record found, conservatively return true to mimic | ||||
6839 | // behavior before this flag was added. | ||||
6840 | return true; | ||||
6841 | case BitstreamEntry::Record: | ||||
6842 | // The interesting case. | ||||
6843 | break; | ||||
6844 | } | ||||
6845 | |||||
6846 | // Look for the FS_FLAGS record. | ||||
6847 | Record.clear(); | ||||
6848 | Expected<unsigned> MaybeBitCode = Stream.readRecord(Entry.ID, Record); | ||||
6849 | if (!MaybeBitCode) | ||||
6850 | return MaybeBitCode.takeError(); | ||||
6851 | switch (MaybeBitCode.get()) { | ||||
6852 | default: // Default behavior: ignore. | ||||
6853 | break; | ||||
6854 | case bitc::FS_FLAGS: { // [flags] | ||||
6855 | uint64_t Flags = Record[0]; | ||||
6856 | // Scan flags. | ||||
6857 | assert(Flags <= 0x7f && "Unexpected bits in flag")(static_cast <bool> (Flags <= 0x7f && "Unexpected bits in flag" ) ? void (0) : __assert_fail ("Flags <= 0x7f && \"Unexpected bits in flag\"" , "/build/llvm-toolchain-snapshot-13~++20210621111111+acefe0eaaf82/llvm/lib/Bitcode/Reader/BitcodeReader.cpp" , 6857, __extension__ __PRETTY_FUNCTION__)); | ||||
6858 | |||||
6859 | return Flags & 0x8; | ||||
6860 | } | ||||
6861 | } | ||||
6862 | } | ||||
6863 | llvm_unreachable("Exit infinite loop")::llvm::llvm_unreachable_internal("Exit infinite loop", "/build/llvm-toolchain-snapshot-13~++20210621111111+acefe0eaaf82/llvm/lib/Bitcode/Reader/BitcodeReader.cpp" , 6863); | ||||
6864 | } | ||||
6865 | |||||
6866 | // Check if the given bitcode buffer contains a global value summary block. | ||||
6867 | Expected<BitcodeLTOInfo> BitcodeModule::getLTOInfo() { | ||||
6868 | BitstreamCursor Stream(Buffer); | ||||
6869 | if (Error JumpFailed = Stream.JumpToBit(ModuleBit)) | ||||
6870 | return std::move(JumpFailed); | ||||
6871 | |||||
6872 | if (Error Err = Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID)) | ||||
6873 | return std::move(Err); | ||||
6874 | |||||
6875 | while (true) { | ||||
6876 | Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); | ||||
6877 | if (!MaybeEntry) | ||||
6878 | return MaybeEntry.takeError(); | ||||
6879 | llvm::BitstreamEntry Entry = MaybeEntry.get(); | ||||
6880 | |||||
6881 | switch (Entry.Kind) { | ||||
6882 | case BitstreamEntry::Error: | ||||
6883 | return error("Malformed block"); | ||||
6884 | case BitstreamEntry::EndBlock: | ||||
6885 | return BitcodeLTOInfo{/*IsThinLTO=*/false, /*HasSummary=*/false, | ||||
6886 | /*EnableSplitLTOUnit=*/false}; | ||||
6887 | |||||
6888 | case BitstreamEntry::SubBlock: | ||||
6889 | if (Entry.ID == bitc::GLOBALVAL_SUMMARY_BLOCK_ID) { | ||||
6890 | Expected<bool> EnableSplitLTOUnit = | ||||
6891 | getEnableSplitLTOUnitFlag(Stream, Entry.ID); | ||||
6892 | if (!EnableSplitLTOUnit) | ||||
6893 | return EnableSplitLTOUnit.takeError(); | ||||
6894 | return BitcodeLTOInfo{/*IsThinLTO=*/true, /*HasSummary=*/true, | ||||
6895 | *EnableSplitLTOUnit}; | ||||
6896 | } | ||||
6897 | |||||
6898 | if (Entry.ID == bitc::FULL_LTO_GLOBALVAL_SUMMARY_BLOCK_ID) { | ||||
6899 | Expected<bool> EnableSplitLTOUnit = | ||||
6900 | getEnableSplitLTOUnitFlag(Stream, Entry.ID); | ||||
6901 | if (!EnableSplitLTOUnit) | ||||
6902 | return EnableSplitLTOUnit.takeError(); | ||||
6903 | return BitcodeLTOInfo{/*IsThinLTO=*/false, /*HasSummary=*/true, | ||||
6904 | *EnableSplitLTOUnit}; | ||||
6905 | } | ||||
6906 | |||||
6907 | // Ignore other sub-blocks. | ||||
6908 | if (Error Err = Stream.SkipBlock()) | ||||
6909 | return std::move(Err); | ||||
6910 | continue; | ||||
6911 | |||||
6912 | case BitstreamEntry::Record: | ||||
6913 | if (Expected<unsigned> StreamFailed = Stream.skipRecord(Entry.ID)) | ||||
6914 | continue; | ||||
6915 | else | ||||
6916 | return StreamFailed.takeError(); | ||||
6917 | } | ||||
6918 | } | ||||
6919 | } | ||||
6920 | |||||
6921 | static Expected<BitcodeModule> getSingleModule(MemoryBufferRef Buffer) { | ||||
6922 | Expected<std::vector<BitcodeModule>> MsOrErr = getBitcodeModuleList(Buffer); | ||||
6923 | if (!MsOrErr) | ||||
6924 | return MsOrErr.takeError(); | ||||
6925 | |||||
6926 | if (MsOrErr->size() != 1) | ||||
6927 | return error("Expected a single module"); | ||||
6928 | |||||
6929 | return (*MsOrErr)[0]; | ||||
6930 | } | ||||
6931 | |||||
6932 | Expected<std::unique_ptr<Module>> | ||||
6933 | llvm::getLazyBitcodeModule(MemoryBufferRef Buffer, LLVMContext &Context, | ||||
6934 | bool ShouldLazyLoadMetadata, bool IsImporting) { | ||||
6935 | Expected<BitcodeModule> BM = getSingleModule(Buffer); | ||||
6936 | if (!BM) | ||||
6937 | return BM.takeError(); | ||||
6938 | |||||
6939 | return BM->getLazyModule(Context, ShouldLazyLoadMetadata, IsImporting); | ||||
6940 | } | ||||
6941 | |||||
6942 | Expected<std::unique_ptr<Module>> llvm::getOwningLazyBitcodeModule( | ||||
6943 | std::unique_ptr<MemoryBuffer> &&Buffer, LLVMContext &Context, | ||||
6944 | bool ShouldLazyLoadMetadata, bool IsImporting) { | ||||
6945 | auto MOrErr = getLazyBitcodeModule(*Buffer, Context, ShouldLazyLoadMetadata, | ||||
6946 | IsImporting); | ||||
6947 | if (MOrErr) | ||||
6948 | (*MOrErr)->setOwnedMemoryBuffer(std::move(Buffer)); | ||||
6949 | return MOrErr; | ||||
6950 | } | ||||
6951 | |||||
6952 | Expected<std::unique_ptr<Module>> | ||||
6953 | BitcodeModule::parseModule(LLVMContext &Context, | ||||
6954 | DataLayoutCallbackTy DataLayoutCallback) { | ||||
6955 | return getModuleImpl(Context, true, false, false, DataLayoutCallback); | ||||
6956 | // TODO: Restore the use-lists to the in-memory state when the bitcode was | ||||
6957 | // written. We must defer until the Module has been fully materialized. | ||||
6958 | } | ||||
6959 | |||||
6960 | Expected<std::unique_ptr<Module>> | ||||
6961 | llvm::parseBitcodeFile(MemoryBufferRef Buffer, LLVMContext &Context, | ||||
6962 | DataLayoutCallbackTy DataLayoutCallback) { | ||||
6963 | Expected<BitcodeModule> BM = getSingleModule(Buffer); | ||||
6964 | if (!BM) | ||||
6965 | return BM.takeError(); | ||||
6966 | |||||
6967 | return BM->parseModule(Context, DataLayoutCallback); | ||||
6968 | } | ||||
6969 | |||||
6970 | Expected<std::string> llvm::getBitcodeTargetTriple(MemoryBufferRef Buffer) { | ||||
6971 | Expected<BitstreamCursor> StreamOrErr = initStream(Buffer); | ||||
6972 | if (!StreamOrErr) | ||||
6973 | return StreamOrErr.takeError(); | ||||
6974 | |||||
6975 | return readTriple(*StreamOrErr); | ||||
6976 | } | ||||
6977 | |||||
6978 | Expected<bool> llvm::isBitcodeContainingObjCCategory(MemoryBufferRef Buffer) { | ||||
6979 | Expected<BitstreamCursor> StreamOrErr = initStream(Buffer); | ||||
6980 | if (!StreamOrErr) | ||||
6981 | return StreamOrErr.takeError(); | ||||
6982 | |||||
6983 | return hasObjCCategory(*StreamOrErr); | ||||
6984 | } | ||||
6985 | |||||
6986 | Expected<std::string> llvm::getBitcodeProducerString(MemoryBufferRef Buffer) { | ||||
6987 | Expected<BitstreamCursor> StreamOrErr = initStream(Buffer); | ||||
6988 | if (!StreamOrErr) | ||||
6989 | return StreamOrErr.takeError(); | ||||
6990 | |||||
6991 | return readIdentificationCode(*StreamOrErr); | ||||
6992 | } | ||||
6993 | |||||
6994 | Error llvm::readModuleSummaryIndex(MemoryBufferRef Buffer, | ||||
6995 | ModuleSummaryIndex &CombinedIndex, | ||||
6996 | uint64_t ModuleId) { | ||||
6997 | Expected<BitcodeModule> BM = getSingleModule(Buffer); | ||||
6998 | if (!BM) | ||||
6999 | return BM.takeError(); | ||||
7000 | |||||
7001 | return BM->readSummary(CombinedIndex, BM->getModuleIdentifier(), ModuleId); | ||||
7002 | } | ||||
7003 | |||||
7004 | Expected<std::unique_ptr<ModuleSummaryIndex>> | ||||
7005 | llvm::getModuleSummaryIndex(MemoryBufferRef Buffer) { | ||||
7006 | Expected<BitcodeModule> BM = getSingleModule(Buffer); | ||||
7007 | if (!BM) | ||||
7008 | return BM.takeError(); | ||||
7009 | |||||
7010 | return BM->getSummary(); | ||||
7011 | } | ||||
7012 | |||||
7013 | Expected<BitcodeLTOInfo> llvm::getBitcodeLTOInfo(MemoryBufferRef Buffer) { | ||||
7014 | Expected<BitcodeModule> BM = getSingleModule(Buffer); | ||||
7015 | if (!BM) | ||||
7016 | return BM.takeError(); | ||||
7017 | |||||
7018 | return BM->getLTOInfo(); | ||||
7019 | } | ||||
7020 | |||||
7021 | Expected<std::unique_ptr<ModuleSummaryIndex>> | ||||
7022 | llvm::getModuleSummaryIndexForFile(StringRef Path, | ||||
7023 | bool IgnoreEmptyThinLTOIndexFile) { | ||||
7024 | ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr = | ||||
7025 | MemoryBuffer::getFileOrSTDIN(Path); | ||||
7026 | if (!FileOrErr) | ||||
7027 | return errorCodeToError(FileOrErr.getError()); | ||||
7028 | if (IgnoreEmptyThinLTOIndexFile && !(*FileOrErr)->getBufferSize()) | ||||
7029 | return nullptr; | ||||
7030 | return getModuleSummaryIndex(**FileOrErr); | ||||
7031 | } |
1 | //===- llvm/Support/Error.h - Recoverable error handling --------*- C++ -*-===// |
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 | // This file defines an API used to report recoverable errors. |
10 | // |
11 | //===----------------------------------------------------------------------===// |
12 | |
13 | #ifndef LLVM_SUPPORT_ERROR_H |
14 | #define LLVM_SUPPORT_ERROR_H |
15 | |
16 | #include "llvm-c/Error.h" |
17 | #include "llvm/ADT/STLExtras.h" |
18 | #include "llvm/ADT/SmallVector.h" |
19 | #include "llvm/ADT/StringExtras.h" |
20 | #include "llvm/ADT/Twine.h" |
21 | #include "llvm/Config/abi-breaking.h" |
22 | #include "llvm/Support/AlignOf.h" |
23 | #include "llvm/Support/Compiler.h" |
24 | #include "llvm/Support/Debug.h" |
25 | #include "llvm/Support/ErrorHandling.h" |
26 | #include "llvm/Support/ErrorOr.h" |
27 | #include "llvm/Support/Format.h" |
28 | #include "llvm/Support/raw_ostream.h" |
29 | #include <algorithm> |
30 | #include <cassert> |
31 | #include <cstdint> |
32 | #include <cstdlib> |
33 | #include <functional> |
34 | #include <memory> |
35 | #include <new> |
36 | #include <string> |
37 | #include <system_error> |
38 | #include <type_traits> |
39 | #include <utility> |
40 | #include <vector> |
41 | |
42 | namespace llvm { |
43 | |
44 | class ErrorSuccess; |
45 | |
46 | /// Base class for error info classes. Do not extend this directly: Extend |
47 | /// the ErrorInfo template subclass instead. |
48 | class ErrorInfoBase { |
49 | public: |
50 | virtual ~ErrorInfoBase() = default; |
51 | |
52 | /// Print an error message to an output stream. |
53 | virtual void log(raw_ostream &OS) const = 0; |
54 | |
55 | /// Return the error message as a string. |
56 | virtual std::string message() const { |
57 | std::string Msg; |
58 | raw_string_ostream OS(Msg); |
59 | log(OS); |
60 | return OS.str(); |
61 | } |
62 | |
63 | /// Convert this error to a std::error_code. |
64 | /// |
65 | /// This is a temporary crutch to enable interaction with code still |
66 | /// using std::error_code. It will be removed in the future. |
67 | virtual std::error_code convertToErrorCode() const = 0; |
68 | |
69 | // Returns the class ID for this type. |
70 | static const void *classID() { return &ID; } |
71 | |
72 | // Returns the class ID for the dynamic type of this ErrorInfoBase instance. |
73 | virtual const void *dynamicClassID() const = 0; |
74 | |
75 | // Check whether this instance is a subclass of the class identified by |
76 | // ClassID. |
77 | virtual bool isA(const void *const ClassID) const { |
78 | return ClassID == classID(); |
79 | } |
80 | |
81 | // Check whether this instance is a subclass of ErrorInfoT. |
82 | template <typename ErrorInfoT> bool isA() const { |
83 | return isA(ErrorInfoT::classID()); |
84 | } |
85 | |
86 | private: |
87 | virtual void anchor(); |
88 | |
89 | static char ID; |
90 | }; |
91 | |
92 | /// Lightweight error class with error context and mandatory checking. |
93 | /// |
94 | /// Instances of this class wrap a ErrorInfoBase pointer. Failure states |
95 | /// are represented by setting the pointer to a ErrorInfoBase subclass |
96 | /// instance containing information describing the failure. Success is |
97 | /// represented by a null pointer value. |
98 | /// |
99 | /// Instances of Error also contains a 'Checked' flag, which must be set |
100 | /// before the destructor is called, otherwise the destructor will trigger a |
101 | /// runtime error. This enforces at runtime the requirement that all Error |
102 | /// instances be checked or returned to the caller. |
103 | /// |
104 | /// There are two ways to set the checked flag, depending on what state the |
105 | /// Error instance is in. For Error instances indicating success, it |
106 | /// is sufficient to invoke the boolean conversion operator. E.g.: |
107 | /// |
108 | /// @code{.cpp} |
109 | /// Error foo(<...>); |
110 | /// |
111 | /// if (auto E = foo(<...>)) |
112 | /// return E; // <- Return E if it is in the error state. |
113 | /// // We have verified that E was in the success state. It can now be safely |
114 | /// // destroyed. |
115 | /// @endcode |
116 | /// |
117 | /// A success value *can not* be dropped. For example, just calling 'foo(<...>)' |
118 | /// without testing the return value will raise a runtime error, even if foo |
119 | /// returns success. |
120 | /// |
121 | /// For Error instances representing failure, you must use either the |
122 | /// handleErrors or handleAllErrors function with a typed handler. E.g.: |
123 | /// |
124 | /// @code{.cpp} |
125 | /// class MyErrorInfo : public ErrorInfo<MyErrorInfo> { |
126 | /// // Custom error info. |
127 | /// }; |
128 | /// |
129 | /// Error foo(<...>) { return make_error<MyErrorInfo>(...); } |
130 | /// |
131 | /// auto E = foo(<...>); // <- foo returns failure with MyErrorInfo. |
132 | /// auto NewE = |
133 | /// handleErrors(E, |
134 | /// [](const MyErrorInfo &M) { |
135 | /// // Deal with the error. |
136 | /// }, |
137 | /// [](std::unique_ptr<OtherError> M) -> Error { |
138 | /// if (canHandle(*M)) { |
139 | /// // handle error. |
140 | /// return Error::success(); |
141 | /// } |
142 | /// // Couldn't handle this error instance. Pass it up the stack. |
143 | /// return Error(std::move(M)); |
144 | /// ); |
145 | /// // Note - we must check or return NewE in case any of the handlers |
146 | /// // returned a new error. |
147 | /// @endcode |
148 | /// |
149 | /// The handleAllErrors function is identical to handleErrors, except |
150 | /// that it has a void return type, and requires all errors to be handled and |
151 | /// no new errors be returned. It prevents errors (assuming they can all be |
152 | /// handled) from having to be bubbled all the way to the top-level. |
153 | /// |
154 | /// *All* Error instances must be checked before destruction, even if |
155 | /// they're moved-assigned or constructed from Success values that have already |
156 | /// been checked. This enforces checking through all levels of the call stack. |
157 | class LLVM_NODISCARD[[clang::warn_unused_result]] Error { |
158 | // ErrorList needs to be able to yank ErrorInfoBase pointers out of Errors |
159 | // to add to the error list. It can't rely on handleErrors for this, since |
160 | // handleErrors does not support ErrorList handlers. |
161 | friend class ErrorList; |
162 | |
163 | // handleErrors needs to be able to set the Checked flag. |
164 | template <typename... HandlerTs> |
165 | friend Error handleErrors(Error E, HandlerTs &&... Handlers); |
166 | |
167 | // Expected<T> needs to be able to steal the payload when constructed from an |
168 | // error. |
169 | template <typename T> friend class Expected; |
170 | |
171 | // wrap needs to be able to steal the payload. |
172 | friend LLVMErrorRef wrap(Error); |
173 | |
174 | protected: |
175 | /// Create a success value. Prefer using 'Error::success()' for readability |
176 | Error() { |
177 | setPtr(nullptr); |
178 | setChecked(false); |
179 | } |
180 | |
181 | public: |
182 | /// Create a success value. |
183 | static ErrorSuccess success(); |
184 | |
185 | // Errors are not copy-constructable. |
186 | Error(const Error &Other) = delete; |
187 | |
188 | /// Move-construct an error value. The newly constructed error is considered |
189 | /// unchecked, even if the source error had been checked. The original error |
190 | /// becomes a checked Success value, regardless of its original state. |
191 | Error(Error &&Other) { |
192 | setChecked(true); |
193 | *this = std::move(Other); |
194 | } |
195 | |
196 | /// Create an error value. Prefer using the 'make_error' function, but |
197 | /// this constructor can be useful when "re-throwing" errors from handlers. |
198 | Error(std::unique_ptr<ErrorInfoBase> Payload) { |
199 | setPtr(Payload.release()); |
200 | setChecked(false); |
201 | } |
202 | |
203 | // Errors are not copy-assignable. |
204 | Error &operator=(const Error &Other) = delete; |
205 | |
206 | /// Move-assign an error value. The current error must represent success, you |
207 | /// you cannot overwrite an unhandled error. The current error is then |
208 | /// considered unchecked. The source error becomes a checked success value, |
209 | /// regardless of its original state. |
210 | Error &operator=(Error &&Other) { |
211 | // Don't allow overwriting of unchecked values. |
212 | assertIsChecked(); |
213 | setPtr(Other.getPtr()); |
214 | |
215 | // This Error is unchecked, even if the source error was checked. |
216 | setChecked(false); |
217 | |
218 | // Null out Other's payload and set its checked bit. |
219 | Other.setPtr(nullptr); |
220 | Other.setChecked(true); |
221 | |
222 | return *this; |
223 | } |
224 | |
225 | /// Destroy a Error. Fails with a call to abort() if the error is |
226 | /// unchecked. |
227 | ~Error() { |
228 | assertIsChecked(); |
229 | delete getPtr(); |
230 | } |
231 | |
232 | /// Bool conversion. Returns true if this Error is in a failure state, |
233 | /// and false if it is in an accept state. If the error is in a Success state |
234 | /// it will be considered checked. |
235 | explicit operator bool() { |
236 | setChecked(getPtr() == nullptr); |
237 | return getPtr() != nullptr; |
238 | } |
239 | |
240 | /// Check whether one error is a subclass of another. |
241 | template <typename ErrT> bool isA() const { |
242 | return getPtr() && getPtr()->isA(ErrT::classID()); |
243 | } |
244 | |
245 | /// Returns the dynamic class id of this error, or null if this is a success |
246 | /// value. |
247 | const void* dynamicClassID() const { |
248 | if (!getPtr()) |
249 | return nullptr; |
250 | return getPtr()->dynamicClassID(); |
251 | } |
252 | |
253 | private: |
254 | #if LLVM_ENABLE_ABI_BREAKING_CHECKS1 |
255 | // assertIsChecked() happens very frequently, but under normal circumstances |
256 | // is supposed to be a no-op. So we want it to be inlined, but having a bunch |
257 | // of debug prints can cause the function to be too large for inlining. So |
258 | // it's important that we define this function out of line so that it can't be |
259 | // inlined. |
260 | LLVM_ATTRIBUTE_NORETURN__attribute__((noreturn)) |
261 | void fatalUncheckedError() const; |
262 | #endif |
263 | |
264 | void assertIsChecked() { |
265 | #if LLVM_ENABLE_ABI_BREAKING_CHECKS1 |
266 | if (LLVM_UNLIKELY(!getChecked() || getPtr())__builtin_expect((bool)(!getChecked() || getPtr()), false)) |
267 | fatalUncheckedError(); |
268 | #endif |
269 | } |
270 | |
271 | ErrorInfoBase *getPtr() const { |
272 | #if LLVM_ENABLE_ABI_BREAKING_CHECKS1 |
273 | return reinterpret_cast<ErrorInfoBase*>( |
274 | reinterpret_cast<uintptr_t>(Payload) & |
275 | ~static_cast<uintptr_t>(0x1)); |
276 | #else |
277 | return Payload; |
278 | #endif |
279 | } |
280 | |
281 | void setPtr(ErrorInfoBase *EI) { |
282 | #if LLVM_ENABLE_ABI_BREAKING_CHECKS1 |
283 | Payload = reinterpret_cast<ErrorInfoBase*>( |
284 | (reinterpret_cast<uintptr_t>(EI) & |
285 | ~static_cast<uintptr_t>(0x1)) | |
286 | (reinterpret_cast<uintptr_t>(Payload) & 0x1)); |
287 | #else |
288 | Payload = EI; |
289 | #endif |
290 | } |
291 | |
292 | bool getChecked() const { |
293 | #if LLVM_ENABLE_ABI_BREAKING_CHECKS1 |
294 | return (reinterpret_cast<uintptr_t>(Payload) & 0x1) == 0; |
295 | #else |
296 | return true; |
297 | #endif |
298 | } |
299 | |
300 | void setChecked(bool V) { |
301 | #if LLVM_ENABLE_ABI_BREAKING_CHECKS1 |
302 | Payload = reinterpret_cast<ErrorInfoBase*>( |
303 | (reinterpret_cast<uintptr_t>(Payload) & |
304 | ~static_cast<uintptr_t>(0x1)) | |
305 | (V ? 0 : 1)); |
306 | #endif |
307 | } |
308 | |
309 | std::unique_ptr<ErrorInfoBase> takePayload() { |
310 | std::unique_ptr<ErrorInfoBase> Tmp(getPtr()); |
311 | setPtr(nullptr); |
312 | setChecked(true); |
313 | return Tmp; |
314 | } |
315 | |
316 | friend raw_ostream &operator<<(raw_ostream &OS, const Error &E) { |
317 | if (auto P = E.getPtr()) |
318 | P->log(OS); |
319 | else |
320 | OS << "success"; |
321 | return OS; |
322 | } |
323 | |
324 | ErrorInfoBase *Payload = nullptr; |
325 | }; |
326 | |
327 | /// Subclass of Error for the sole purpose of identifying the success path in |
328 | /// the type system. This allows to catch invalid conversion to Expected<T> at |
329 | /// compile time. |
330 | class ErrorSuccess final : public Error {}; |
331 | |
332 | inline ErrorSuccess Error::success() { return ErrorSuccess(); } |
333 | |
334 | /// Make a Error instance representing failure using the given error info |
335 | /// type. |
336 | template <typename ErrT, typename... ArgTs> Error make_error(ArgTs &&... Args) { |
337 | return Error(std::make_unique<ErrT>(std::forward<ArgTs>(Args)...)); |
338 | } |
339 | |
340 | /// Base class for user error types. Users should declare their error types |
341 | /// like: |
342 | /// |
343 | /// class MyError : public ErrorInfo<MyError> { |
344 | /// .... |
345 | /// }; |
346 | /// |
347 | /// This class provides an implementation of the ErrorInfoBase::kind |
348 | /// method, which is used by the Error RTTI system. |
349 | template <typename ThisErrT, typename ParentErrT = ErrorInfoBase> |
350 | class ErrorInfo : public ParentErrT { |
351 | public: |
352 | using ParentErrT::ParentErrT; // inherit constructors |
353 | |
354 | static const void *classID() { return &ThisErrT::ID; } |
355 | |
356 | const void *dynamicClassID() const override { return &ThisErrT::ID; } |
357 | |
358 | bool isA(const void *const ClassID) const override { |
359 | return ClassID == classID() || ParentErrT::isA(ClassID); |
360 | } |
361 | }; |
362 | |
363 | /// Special ErrorInfo subclass representing a list of ErrorInfos. |
364 | /// Instances of this class are constructed by joinError. |
365 | class ErrorList final : public ErrorInfo<ErrorList> { |
366 | // handleErrors needs to be able to iterate the payload list of an |
367 | // ErrorList. |
368 | template <typename... HandlerTs> |
369 | friend Error handleErrors(Error E, HandlerTs &&... Handlers); |
370 | |
371 | // joinErrors is implemented in terms of join. |
372 | friend Error joinErrors(Error, Error); |
373 | |
374 | public: |
375 | void log(raw_ostream &OS) const override { |
376 | OS << "Multiple errors:\n"; |
377 | for (auto &ErrPayload : Payloads) { |
378 | ErrPayload->log(OS); |
379 | OS << "\n"; |
380 | } |
381 | } |
382 | |
383 | std::error_code convertToErrorCode() const override; |
384 | |
385 | // Used by ErrorInfo::classID. |
386 | static char ID; |
387 | |
388 | private: |
389 | ErrorList(std::unique_ptr<ErrorInfoBase> Payload1, |
390 | std::unique_ptr<ErrorInfoBase> Payload2) { |
391 | assert(!Payload1->isA<ErrorList>() && !Payload2->isA<ErrorList>() &&(static_cast <bool> (!Payload1->isA<ErrorList> () && !Payload2->isA<ErrorList>() && "ErrorList constructor payloads should be singleton errors") ? void (0) : __assert_fail ("!Payload1->isA<ErrorList>() && !Payload2->isA<ErrorList>() && \"ErrorList constructor payloads should be singleton errors\"" , "/build/llvm-toolchain-snapshot-13~++20210621111111+acefe0eaaf82/llvm/include/llvm/Support/Error.h" , 392, __extension__ __PRETTY_FUNCTION__)) |
392 | "ErrorList constructor payloads should be singleton errors")(static_cast <bool> (!Payload1->isA<ErrorList> () && !Payload2->isA<ErrorList>() && "ErrorList constructor payloads should be singleton errors") ? void (0) : __assert_fail ("!Payload1->isA<ErrorList>() && !Payload2->isA<ErrorList>() && \"ErrorList constructor payloads should be singleton errors\"" , "/build/llvm-toolchain-snapshot-13~++20210621111111+acefe0eaaf82/llvm/include/llvm/Support/Error.h" , 392, __extension__ __PRETTY_FUNCTION__)); |
393 | Payloads.push_back(std::move(Payload1)); |
394 | Payloads.push_back(std::move(Payload2)); |
395 | } |
396 | |
397 | static Error join(Error E1, Error E2) { |
398 | if (!E1) |
399 | return E2; |
400 | if (!E2) |
401 | return E1; |
402 | if (E1.isA<ErrorList>()) { |
403 | auto &E1List = static_cast<ErrorList &>(*E1.getPtr()); |
404 | if (E2.isA<ErrorList>()) { |
405 | auto E2Payload = E2.takePayload(); |
406 | auto &E2List = static_cast<ErrorList &>(*E2Payload); |
407 | for (auto &Payload : E2List.Payloads) |
408 | E1List.Payloads.push_back(std::move(Payload)); |
409 | } else |
410 | E1List.Payloads.push_back(E2.takePayload()); |
411 | |
412 | return E1; |
413 | } |
414 | if (E2.isA<ErrorList>()) { |
415 | auto &E2List = static_cast<ErrorList &>(*E2.getPtr()); |
416 | E2List.Payloads.insert(E2List.Payloads.begin(), E1.takePayload()); |
417 | return E2; |
418 | } |
419 | return Error(std::unique_ptr<ErrorList>( |
420 | new ErrorList(E1.takePayload(), E2.takePayload()))); |
421 | } |
422 | |
423 | std::vector<std::unique_ptr<ErrorInfoBase>> Payloads; |
424 | }; |
425 | |
426 | /// Concatenate errors. The resulting Error is unchecked, and contains the |
427 | /// ErrorInfo(s), if any, contained in E1, followed by the |
428 | /// ErrorInfo(s), if any, contained in E2. |
429 | inline Error joinErrors(Error E1, Error E2) { |
430 | return ErrorList::join(std::move(E1), std::move(E2)); |
431 | } |
432 | |
433 | /// Tagged union holding either a T or a Error. |
434 | /// |
435 | /// This class parallels ErrorOr, but replaces error_code with Error. Since |
436 | /// Error cannot be copied, this class replaces getError() with |
437 | /// takeError(). It also adds an bool errorIsA<ErrT>() method for testing the |
438 | /// error class type. |
439 | template <class T> class LLVM_NODISCARD[[clang::warn_unused_result]] Expected { |
440 | template <class T1> friend class ExpectedAsOutParameter; |
441 | template <class OtherT> friend class Expected; |
442 | |
443 | static constexpr bool isRef = std::is_reference<T>::value; |
444 | |
445 | using wrap = std::reference_wrapper<std::remove_reference_t<T>>; |
446 | |
447 | using error_type = std::unique_ptr<ErrorInfoBase>; |
448 | |
449 | public: |
450 | using storage_type = std::conditional_t<isRef, wrap, T>; |
451 | using value_type = T; |
452 | |
453 | private: |
454 | using reference = std::remove_reference_t<T> &; |
455 | using const_reference = const std::remove_reference_t<T> &; |
456 | using pointer = std::remove_reference_t<T> *; |
457 | using const_pointer = const std::remove_reference_t<T> *; |
458 | |
459 | public: |
460 | /// Create an Expected<T> error value from the given Error. |
461 | Expected(Error Err) |
462 | : HasError(true) |
463 | #if LLVM_ENABLE_ABI_BREAKING_CHECKS1 |
464 | // Expected is unchecked upon construction in Debug builds. |
465 | , Unchecked(true) |
466 | #endif |
467 | { |
468 | assert(Err && "Cannot create Expected<T> from Error success value.")(static_cast <bool> (Err && "Cannot create Expected<T> from Error success value." ) ? void (0) : __assert_fail ("Err && \"Cannot create Expected<T> from Error success value.\"" , "/build/llvm-toolchain-snapshot-13~++20210621111111+acefe0eaaf82/llvm/include/llvm/Support/Error.h" , 468, __extension__ __PRETTY_FUNCTION__)); |
469 | new (getErrorStorage()) error_type(Err.takePayload()); |
470 | } |
471 | |
472 | /// Forbid to convert from Error::success() implicitly, this avoids having |
473 | /// Expected<T> foo() { return Error::success(); } which compiles otherwise |
474 | /// but triggers the assertion above. |
475 | Expected(ErrorSuccess) = delete; |
476 | |
477 | /// Create an Expected<T> success value from the given OtherT value, which |
478 | /// must be convertible to T. |
479 | template <typename OtherT> |
480 | Expected(OtherT &&Val, |
481 | std::enable_if_t<std::is_convertible<OtherT, T>::value> * = nullptr) |
482 | : HasError(false) |
483 | #if LLVM_ENABLE_ABI_BREAKING_CHECKS1 |
484 | // Expected is unchecked upon construction in Debug builds. |
485 | , |
486 | Unchecked(true) |
487 | #endif |
488 | { |
489 | new (getStorage()) storage_type(std::forward<OtherT>(Val)); |
490 | } |
491 | |
492 | /// Move construct an Expected<T> value. |
493 | Expected(Expected &&Other) { moveConstruct(std::move(Other)); } |
494 | |
495 | /// Move construct an Expected<T> value from an Expected<OtherT>, where OtherT |
496 | /// must be convertible to T. |
497 | template <class OtherT> |
498 | Expected( |
499 | Expected<OtherT> &&Other, |
500 | std::enable_if_t<std::is_convertible<OtherT, T>::value> * = nullptr) { |
501 | moveConstruct(std::move(Other)); |
502 | } |
503 | |
504 | /// Move construct an Expected<T> value from an Expected<OtherT>, where OtherT |
505 | /// isn't convertible to T. |
506 | template <class OtherT> |
507 | explicit Expected( |
508 | Expected<OtherT> &&Other, |
509 | std::enable_if_t<!std::is_convertible<OtherT, T>::value> * = nullptr) { |
510 | moveConstruct(std::move(Other)); |
511 | } |
512 | |
513 | /// Move-assign from another Expected<T>. |
514 | Expected &operator=(Expected &&Other) { |
515 | moveAssign(std::move(Other)); |
516 | return *this; |
517 | } |
518 | |
519 | /// Destroy an Expected<T>. |
520 | ~Expected() { |
521 | assertIsChecked(); |
522 | if (!HasError) |
523 | getStorage()->~storage_type(); |
524 | else |
525 | getErrorStorage()->~error_type(); |
526 | } |
527 | |
528 | /// Return false if there is an error. |
529 | explicit operator bool() { |
530 | #if LLVM_ENABLE_ABI_BREAKING_CHECKS1 |
531 | Unchecked = HasError; |
532 | #endif |
533 | return !HasError; |
534 | } |
535 | |
536 | /// Returns a reference to the stored T value. |
537 | reference get() { |
538 | assertIsChecked(); |
539 | return *getStorage(); |
540 | } |
541 | |
542 | /// Returns a const reference to the stored T value. |
543 | const_reference get() const { |
544 | assertIsChecked(); |
545 | return const_cast<Expected<T> *>(this)->get(); |
546 | } |
547 | |
548 | /// Check that this Expected<T> is an error of type ErrT. |
549 | template <typename ErrT> bool errorIsA() const { |
550 | return HasError && (*getErrorStorage())->template isA<ErrT>(); |
551 | } |
552 | |
553 | /// Take ownership of the stored error. |
554 | /// After calling this the Expected<T> is in an indeterminate state that can |
555 | /// only be safely destructed. No further calls (beside the destructor) should |
556 | /// be made on the Expected<T> value. |
557 | Error takeError() { |
558 | #if LLVM_ENABLE_ABI_BREAKING_CHECKS1 |
559 | Unchecked = false; |
560 | #endif |
561 | return HasError ? Error(std::move(*getErrorStorage())) : Error::success(); |
562 | } |
563 | |
564 | /// Returns a pointer to the stored T value. |
565 | pointer operator->() { |
566 | assertIsChecked(); |
567 | return toPointer(getStorage()); |
568 | } |
569 | |
570 | /// Returns a const pointer to the stored T value. |
571 | const_pointer operator->() const { |
572 | assertIsChecked(); |
573 | return toPointer(getStorage()); |
574 | } |
575 | |
576 | /// Returns a reference to the stored T value. |
577 | reference operator*() { |
578 | assertIsChecked(); |
579 | return *getStorage(); |
580 | } |
581 | |
582 | /// Returns a const reference to the stored T value. |
583 | const_reference operator*() const { |
584 | assertIsChecked(); |
585 | return *getStorage(); |
586 | } |
587 | |
588 | private: |
589 | template <class T1> |
590 | static bool compareThisIfSameType(const T1 &a, const T1 &b) { |
591 | return &a == &b; |
592 | } |
593 | |
594 | template <class T1, class T2> |
595 | static bool compareThisIfSameType(const T1 &, const T2 &) { |
596 | return false; |
597 | } |
598 | |
599 | template <class OtherT> void moveConstruct(Expected<OtherT> &&Other) { |
600 | HasError = Other.HasError; |
601 | #if LLVM_ENABLE_ABI_BREAKING_CHECKS1 |
602 | Unchecked = true; |
603 | Other.Unchecked = false; |
604 | #endif |
605 | |
606 | if (!HasError) |
607 | new (getStorage()) storage_type(std::move(*Other.getStorage())); |
608 | else |
609 | new (getErrorStorage()) error_type(std::move(*Other.getErrorStorage())); |
610 | } |
611 | |
612 | template <class OtherT> void moveAssign(Expected<OtherT> &&Other) { |
613 | assertIsChecked(); |
614 | |
615 | if (compareThisIfSameType(*this, Other)) |
616 | return; |
617 | |
618 | this->~Expected(); |
619 | new (this) Expected(std::move(Other)); |
620 | } |
621 | |
622 | pointer toPointer(pointer Val) { return Val; } |
623 | |
624 | const_pointer toPointer(const_pointer Val) const { return Val; } |
625 | |
626 | pointer toPointer(wrap *Val) { return &Val->get(); } |
627 | |
628 | const_pointer toPointer(const wrap *Val) const { return &Val->get(); } |
629 | |
630 | storage_type *getStorage() { |
631 | assert(!HasError && "Cannot get value when an error exists!")(static_cast <bool> (!HasError && "Cannot get value when an error exists!" ) ? void (0) : __assert_fail ("!HasError && \"Cannot get value when an error exists!\"" , "/build/llvm-toolchain-snapshot-13~++20210621111111+acefe0eaaf82/llvm/include/llvm/Support/Error.h" , 631, __extension__ __PRETTY_FUNCTION__)); |
632 | return reinterpret_cast<storage_type *>(&TStorage); |
633 | } |
634 | |
635 | const storage_type *getStorage() const { |
636 | assert(!HasError && "Cannot get value when an error exists!")(static_cast <bool> (!HasError && "Cannot get value when an error exists!" ) ? void (0) : __assert_fail ("!HasError && \"Cannot get value when an error exists!\"" , "/build/llvm-toolchain-snapshot-13~++20210621111111+acefe0eaaf82/llvm/include/llvm/Support/Error.h" , 636, __extension__ __PRETTY_FUNCTION__)); |
637 | return reinterpret_cast<const storage_type *>(&TStorage); |
638 | } |
639 | |
640 | error_type *getErrorStorage() { |
641 | assert(HasError && "Cannot get error when a value exists!")(static_cast <bool> (HasError && "Cannot get error when a value exists!" ) ? void (0) : __assert_fail ("HasError && \"Cannot get error when a value exists!\"" , "/build/llvm-toolchain-snapshot-13~++20210621111111+acefe0eaaf82/llvm/include/llvm/Support/Error.h" , 641, __extension__ __PRETTY_FUNCTION__)); |
642 | return reinterpret_cast<error_type *>(&ErrorStorage); |
643 | } |
644 | |
645 | const error_type *getErrorStorage() const { |
646 | assert(HasError && "Cannot get error when a value exists!")(static_cast <bool> (HasError && "Cannot get error when a value exists!" ) ? void (0) : __assert_fail ("HasError && \"Cannot get error when a value exists!\"" , "/build/llvm-toolchain-snapshot-13~++20210621111111+acefe0eaaf82/llvm/include/llvm/Support/Error.h" , 646, __extension__ __PRETTY_FUNCTION__)); |
647 | return reinterpret_cast<const error_type *>(&ErrorStorage); |
648 | } |
649 | |
650 | // Used by ExpectedAsOutParameter to reset the checked flag. |
651 | void setUnchecked() { |
652 | #if LLVM_ENABLE_ABI_BREAKING_CHECKS1 |
653 | Unchecked = true; |
654 | #endif |
655 | } |
656 | |
657 | #if LLVM_ENABLE_ABI_BREAKING_CHECKS1 |
658 | LLVM_ATTRIBUTE_NORETURN__attribute__((noreturn)) |
659 | LLVM_ATTRIBUTE_NOINLINE__attribute__((noinline)) |
660 | void fatalUncheckedExpected() const { |
661 | dbgs() << "Expected<T> must be checked before access or destruction.\n"; |
662 | if (HasError) { |
663 | dbgs() << "Unchecked Expected<T> contained error:\n"; |
664 | (*getErrorStorage())->log(dbgs()); |
665 | } else |
666 | dbgs() << "Expected<T> value was in success state. (Note: Expected<T> " |
667 | "values in success mode must still be checked prior to being " |
668 | "destroyed).\n"; |
669 | abort(); |
670 | } |
671 | #endif |
672 | |
673 | void assertIsChecked() { |
674 | #if LLVM_ENABLE_ABI_BREAKING_CHECKS1 |
675 | if (LLVM_UNLIKELY(Unchecked)__builtin_expect((bool)(Unchecked), false)) |
676 | fatalUncheckedExpected(); |
677 | #endif |
678 | } |
679 | |
680 | union { |
681 | AlignedCharArrayUnion<storage_type> TStorage; |
682 | AlignedCharArrayUnion<error_type> ErrorStorage; |
683 | }; |
684 | bool HasError : 1; |
685 | #if LLVM_ENABLE_ABI_BREAKING_CHECKS1 |
686 | bool Unchecked : 1; |
687 | #endif |
688 | }; |
689 | |
690 | /// Report a serious error, calling any installed error handler. See |
691 | /// ErrorHandling.h. |
692 | LLVM_ATTRIBUTE_NORETURN__attribute__((noreturn)) void report_fatal_error(Error Err, |
693 | bool gen_crash_diag = true); |
694 | |
695 | /// Report a fatal error if Err is a failure value. |
696 | /// |
697 | /// This function can be used to wrap calls to fallible functions ONLY when it |
698 | /// is known that the Error will always be a success value. E.g. |
699 | /// |
700 | /// @code{.cpp} |
701 | /// // foo only attempts the fallible operation if DoFallibleOperation is |
702 | /// // true. If DoFallibleOperation is false then foo always returns |
703 | /// // Error::success(). |
704 | /// Error foo(bool DoFallibleOperation); |
705 | /// |
706 | /// cantFail(foo(false)); |
707 | /// @endcode |
708 | inline void cantFail(Error Err, const char *Msg = nullptr) { |
709 | if (Err) { |
710 | if (!Msg) |
711 | Msg = "Failure value returned from cantFail wrapped call"; |
712 | #ifndef NDEBUG |
713 | std::string Str; |
714 | raw_string_ostream OS(Str); |
715 | OS << Msg << "\n" << Err; |
716 | Msg = OS.str().c_str(); |
717 | #endif |
718 | llvm_unreachable(Msg)::llvm::llvm_unreachable_internal(Msg, "/build/llvm-toolchain-snapshot-13~++20210621111111+acefe0eaaf82/llvm/include/llvm/Support/Error.h" , 718); |
719 | } |
720 | } |
721 | |
722 | /// Report a fatal error if ValOrErr is a failure value, otherwise unwraps and |
723 | /// returns the contained value. |
724 | /// |
725 | /// This function can be used to wrap calls to fallible functions ONLY when it |
726 | /// is known that the Error will always be a success value. E.g. |
727 | /// |
728 | /// @code{.cpp} |
729 | /// // foo only attempts the fallible operation if DoFallibleOperation is |
730 | /// // true. If DoFallibleOperation is false then foo always returns an int. |
731 | /// Expected<int> foo(bool DoFallibleOperation); |
732 | /// |
733 | /// int X = cantFail(foo(false)); |
734 | /// @endcode |
735 | template <typename T> |
736 | T cantFail(Expected<T> ValOrErr, const char *Msg = nullptr) { |
737 | if (ValOrErr) |
738 | return std::move(*ValOrErr); |
739 | else { |
740 | if (!Msg) |
741 | Msg = "Failure value returned from cantFail wrapped call"; |
742 | #ifndef NDEBUG |
743 | std::string Str; |
744 | raw_string_ostream OS(Str); |
745 | auto E = ValOrErr.takeError(); |
746 | OS << Msg << "\n" << E; |
747 | Msg = OS.str().c_str(); |
748 | #endif |
749 | llvm_unreachable(Msg)::llvm::llvm_unreachable_internal(Msg, "/build/llvm-toolchain-snapshot-13~++20210621111111+acefe0eaaf82/llvm/include/llvm/Support/Error.h" , 749); |
750 | } |
751 | } |
752 | |
753 | /// Report a fatal error if ValOrErr is a failure value, otherwise unwraps and |
754 | /// returns the contained reference. |
755 | /// |
756 | /// This function can be used to wrap calls to fallible functions ONLY when it |
757 | /// is known that the Error will always be a success value. E.g. |
758 | /// |
759 | /// @code{.cpp} |
760 | /// // foo only attempts the fallible operation if DoFallibleOperation is |
761 | /// // true. If DoFallibleOperation is false then foo always returns a Bar&. |
762 | /// Expected<Bar&> foo(bool DoFallibleOperation); |
763 | /// |
764 | /// Bar &X = cantFail(foo(false)); |
765 | /// @endcode |
766 | template <typename T> |
767 | T& cantFail(Expected<T&> ValOrErr, const char *Msg = nullptr) { |
768 | if (ValOrErr) |
769 | return *ValOrErr; |
770 | else { |
771 | if (!Msg) |
772 | Msg = "Failure value returned from cantFail wrapped call"; |
773 | #ifndef NDEBUG |
774 | std::string Str; |
775 | raw_string_ostream OS(Str); |
776 | auto E = ValOrErr.takeError(); |
777 | OS << Msg << "\n" << E; |
778 | Msg = OS.str().c_str(); |
779 | #endif |
780 | llvm_unreachable(Msg)::llvm::llvm_unreachable_internal(Msg, "/build/llvm-toolchain-snapshot-13~++20210621111111+acefe0eaaf82/llvm/include/llvm/Support/Error.h" , 780); |
781 | } |
782 | } |
783 | |
784 | /// Helper for testing applicability of, and applying, handlers for |
785 | /// ErrorInfo types. |
786 | template <typename HandlerT> |
787 | class ErrorHandlerTraits |
788 | : public ErrorHandlerTraits<decltype( |
789 | &std::remove_reference<HandlerT>::type::operator())> {}; |
790 | |
791 | // Specialization functions of the form 'Error (const ErrT&)'. |
792 | template <typename ErrT> class ErrorHandlerTraits<Error (&)(ErrT &)> { |
793 | public: |
794 | static bool appliesTo(const ErrorInfoBase &E) { |
795 | return E.template isA<ErrT>(); |
796 | } |
797 | |
798 | template <typename HandlerT> |
799 | static Error apply(HandlerT &&H, std::unique_ptr<ErrorInfoBase> E) { |
800 | assert(appliesTo(*E) && "Applying incorrect handler")(static_cast <bool> (appliesTo(*E) && "Applying incorrect handler" ) ? void (0) : __assert_fail ("appliesTo(*E) && \"Applying incorrect handler\"" , "/build/llvm-toolchain-snapshot-13~++20210621111111+acefe0eaaf82/llvm/include/llvm/Support/Error.h" , 800, __extension__ __PRETTY_FUNCTION__)); |
801 | return H(static_cast<ErrT &>(*E)); |
802 | } |
803 | }; |
804 | |
805 | // Specialization functions of the form 'void (const ErrT&)'. |
806 | template <typename ErrT> class ErrorHandlerTraits<void (&)(ErrT &)> { |
807 | public: |
808 | static bool appliesTo(const ErrorInfoBase &E) { |
809 | return E.template isA<ErrT>(); |
810 | } |
811 | |
812 | template <typename HandlerT> |
813 | static Error apply(HandlerT &&H, std::unique_ptr<ErrorInfoBase> E) { |
814 | assert(appliesTo(*E) && "Applying incorrect handler")(static_cast <bool> (appliesTo(*E) && "Applying incorrect handler" ) ? void (0) : __assert_fail ("appliesTo(*E) && \"Applying incorrect handler\"" , "/build/llvm-toolchain-snapshot-13~++20210621111111+acefe0eaaf82/llvm/include/llvm/Support/Error.h" , 814, __extension__ __PRETTY_FUNCTION__)); |
815 | H(static_cast<ErrT &>(*E)); |
816 | return Error::success(); |
817 | } |
818 | }; |
819 | |
820 | /// Specialization for functions of the form 'Error (std::unique_ptr<ErrT>)'. |
821 | template <typename ErrT> |
822 | class ErrorHandlerTraits<Error (&)(std::unique_ptr<ErrT>)> { |
823 | public: |
824 | static bool appliesTo(const ErrorInfoBase &E) { |
825 | return E.template isA<ErrT>(); |
826 | } |
827 | |
828 | template <typename HandlerT> |
829 | static Error apply(HandlerT &&H, std::unique_ptr<ErrorInfoBase> E) { |
830 | assert(appliesTo(*E) && "Applying incorrect handler")(static_cast <bool> (appliesTo(*E) && "Applying incorrect handler" ) ? void (0) : __assert_fail ("appliesTo(*E) && \"Applying incorrect handler\"" , "/build/llvm-toolchain-snapshot-13~++20210621111111+acefe0eaaf82/llvm/include/llvm/Support/Error.h" , 830, __extension__ __PRETTY_FUNCTION__)); |
831 | std::unique_ptr<ErrT> SubE(static_cast<ErrT *>(E.release())); |
832 | return H(std::move(SubE)); |
833 | } |
834 | }; |
835 | |
836 | /// Specialization for functions of the form 'void (std::unique_ptr<ErrT>)'. |
837 | template <typename ErrT> |
838 | class ErrorHandlerTraits<void (&)(std::unique_ptr<ErrT>)> { |
839 | public: |
840 | static bool appliesTo(const ErrorInfoBase &E) { |
841 | return E.template isA<ErrT>(); |
842 | } |
843 | |
844 | template <typename HandlerT> |
845 | static Error apply(HandlerT &&H, std::unique_ptr<ErrorInfoBase> E) { |
846 | assert(appliesTo(*E) && "Applying incorrect handler")(static_cast <bool> (appliesTo(*E) && "Applying incorrect handler" ) ? void (0) : __assert_fail ("appliesTo(*E) && \"Applying incorrect handler\"" , "/build/llvm-toolchain-snapshot-13~++20210621111111+acefe0eaaf82/llvm/include/llvm/Support/Error.h" , 846, __extension__ __PRETTY_FUNCTION__)); |
847 | std::unique_ptr<ErrT> SubE(static_cast<ErrT *>(E.release())); |
848 | H(std::move(SubE)); |
849 | return Error::success(); |
850 | } |
851 | }; |
852 | |
853 | // Specialization for member functions of the form 'RetT (const ErrT&)'. |
854 | template <typename C, typename RetT, typename ErrT> |
855 | class ErrorHandlerTraits<RetT (C::*)(ErrT &)> |
856 | : public ErrorHandlerTraits<RetT (&)(ErrT &)> {}; |
857 | |
858 | // Specialization for member functions of the form 'RetT (const ErrT&) const'. |
859 | template <typename C, typename RetT, typename ErrT> |
860 | class ErrorHandlerTraits<RetT (C::*)(ErrT &) const> |
861 | : public ErrorHandlerTraits<RetT (&)(ErrT &)> {}; |
862 | |
863 | // Specialization for member functions of the form 'RetT (const ErrT&)'. |
864 | template <typename C, typename RetT, typename ErrT> |
865 | class ErrorHandlerTraits<RetT (C::*)(const ErrT &)> |
866 | : public ErrorHandlerTraits<RetT (&)(ErrT &)> {}; |
867 | |
868 | // Specialization for member functions of the form 'RetT (const ErrT&) const'. |
869 | template <typename C, typename RetT, typename ErrT> |
870 | class ErrorHandlerTraits<RetT (C::*)(const ErrT &) const> |
871 | : public ErrorHandlerTraits<RetT (&)(ErrT &)> {}; |
872 | |
873 | /// Specialization for member functions of the form |
874 | /// 'RetT (std::unique_ptr<ErrT>)'. |
875 | template <typename C, typename RetT, typename ErrT> |
876 | class ErrorHandlerTraits<RetT (C::*)(std::unique_ptr<ErrT>)> |
877 | : public ErrorHandlerTraits<RetT (&)(std::unique_ptr<ErrT>)> {}; |
878 | |
879 | /// Specialization for member functions of the form |
880 | /// 'RetT (std::unique_ptr<ErrT>) const'. |
881 | template <typename C, typename RetT, typename ErrT> |
882 | class ErrorHandlerTraits<RetT (C::*)(std::unique_ptr<ErrT>) const> |
883 | : public ErrorHandlerTraits<RetT (&)(std::unique_ptr<ErrT>)> {}; |
884 | |
885 | inline Error handleErrorImpl(std::unique_ptr<ErrorInfoBase> Payload) { |
886 | return Error(std::move(Payload)); |
887 | } |
888 | |
889 | template <typename HandlerT, typename... HandlerTs> |
890 | Error handleErrorImpl(std::unique_ptr<ErrorInfoBase> Payload, |
891 | HandlerT &&Handler, HandlerTs &&... Handlers) { |
892 | if (ErrorHandlerTraits<HandlerT>::appliesTo(*Payload)) |
893 | return ErrorHandlerTraits<HandlerT>::apply(std::forward<HandlerT>(Handler), |
894 | std::move(Payload)); |
895 | return handleErrorImpl(std::move(Payload), |
896 | std::forward<HandlerTs>(Handlers)...); |
897 | } |
898 | |
899 | /// Pass the ErrorInfo(s) contained in E to their respective handlers. Any |
900 | /// unhandled errors (or Errors returned by handlers) are re-concatenated and |
901 | /// returned. |
902 | /// Because this function returns an error, its result must also be checked |
903 | /// or returned. If you intend to handle all errors use handleAllErrors |
904 | /// (which returns void, and will abort() on unhandled errors) instead. |
905 | template <typename... HandlerTs> |
906 | Error handleErrors(Error E, HandlerTs &&... Hs) { |
907 | if (!E) |
908 | return Error::success(); |
909 | |
910 | std::unique_ptr<ErrorInfoBase> Payload = E.takePayload(); |
911 | |
912 | if (Payload->isA<ErrorList>()) { |
913 | ErrorList &List = static_cast<ErrorList &>(*Payload); |
914 | Error R; |
915 | for (auto &P : List.Payloads) |
916 | R = ErrorList::join( |
917 | std::move(R), |
918 | handleErrorImpl(std::move(P), std::forward<HandlerTs>(Hs)...)); |
919 | return R; |
920 | } |
921 | |
922 | return handleErrorImpl(std::move(Payload), std::forward<HandlerTs>(Hs)...); |
923 | } |
924 | |
925 | /// Behaves the same as handleErrors, except that by contract all errors |
926 | /// *must* be handled by the given handlers (i.e. there must be no remaining |
927 | /// errors after running the handlers, or llvm_unreachable is called). |
928 | template <typename... HandlerTs> |
929 | void handleAllErrors(Error E, HandlerTs &&... Handlers) { |
930 | cantFail(handleErrors(std::move(E), std::forward<HandlerTs>(Handlers)...)); |
931 | } |
932 | |
933 | /// Check that E is a non-error, then drop it. |
934 | /// If E is an error, llvm_unreachable will be called. |
935 | inline void handleAllErrors(Error E) { |
936 | cantFail(std::move(E)); |
937 | } |
938 | |
939 | /// Handle any errors (if present) in an Expected<T>, then try a recovery path. |
940 | /// |
941 | /// If the incoming value is a success value it is returned unmodified. If it |
942 | /// is a failure value then it the contained error is passed to handleErrors. |
943 | /// If handleErrors is able to handle the error then the RecoveryPath functor |
944 | /// is called to supply the final result. If handleErrors is not able to |
945 | /// handle all errors then the unhandled errors are returned. |
946 | /// |
947 | /// This utility enables the follow pattern: |
948 | /// |
949 | /// @code{.cpp} |
950 | /// enum FooStrategy { Aggressive, Conservative }; |
951 | /// Expected<Foo> foo(FooStrategy S); |
952 | /// |
953 | /// auto ResultOrErr = |
954 | /// handleExpected( |
955 | /// foo(Aggressive), |
956 | /// []() { return foo(Conservative); }, |
957 | /// [](AggressiveStrategyError&) { |
958 | /// // Implicitly conusme this - we'll recover by using a conservative |
959 | /// // strategy. |
960 | /// }); |
961 | /// |
962 | /// @endcode |
963 | template <typename T, typename RecoveryFtor, typename... HandlerTs> |
964 | Expected<T> handleExpected(Expected<T> ValOrErr, RecoveryFtor &&RecoveryPath, |
965 | HandlerTs &&... Handlers) { |
966 | if (ValOrErr) |
967 | return ValOrErr; |
968 | |
969 | if (auto Err = handleErrors(ValOrErr.takeError(), |
970 | std::forward<HandlerTs>(Handlers)...)) |
971 | return std::move(Err); |
972 | |
973 | return RecoveryPath(); |
974 | } |
975 | |
976 | /// Log all errors (if any) in E to OS. If there are any errors, ErrorBanner |
977 | /// will be printed before the first one is logged. A newline will be printed |
978 | /// after each error. |
979 | /// |
980 | /// This function is compatible with the helpers from Support/WithColor.h. You |
981 | /// can pass any of them as the OS. Please consider using them instead of |
982 | /// including 'error: ' in the ErrorBanner. |
983 | /// |
984 | /// This is useful in the base level of your program to allow clean termination |
985 | /// (allowing clean deallocation of resources, etc.), while reporting error |
986 | /// information to the user. |
987 | void logAllUnhandledErrors(Error E, raw_ostream &OS, Twine ErrorBanner = {}); |
988 | |
989 | /// Write all error messages (if any) in E to a string. The newline character |
990 | /// is used to separate error messages. |
991 | inline std::string toString(Error E) { |
992 | SmallVector<std::string, 2> Errors; |
993 | handleAllErrors(std::move(E), [&Errors](const ErrorInfoBase &EI) { |
994 | Errors.push_back(EI.message()); |
995 | }); |
996 | return join(Errors.begin(), Errors.end(), "\n"); |
997 | } |
998 | |
999 | /// Consume a Error without doing anything. This method should be used |
1000 | /// only where an error can be considered a reasonable and expected return |
1001 | /// value. |
1002 | /// |
1003 | /// Uses of this method are potentially indicative of design problems: If it's |
1004 | /// legitimate to do nothing while processing an "error", the error-producer |
1005 | /// might be more clearly refactored to return an Optional<T>. |
1006 | inline void consumeError(Error Err) { |
1007 | handleAllErrors(std::move(Err), [](const ErrorInfoBase &) {}); |
1008 | } |
1009 | |
1010 | /// Convert an Expected to an Optional without doing anything. This method |
1011 | /// should be used only where an error can be considered a reasonable and |
1012 | /// expected return value. |
1013 | /// |
1014 | /// Uses of this method are potentially indicative of problems: perhaps the |
1015 | /// error should be propagated further, or the error-producer should just |
1016 | /// return an Optional in the first place. |
1017 | template <typename T> Optional<T> expectedToOptional(Expected<T> &&E) { |
1018 | if (E) |
1019 | return std::move(*E); |
1020 | consumeError(E.takeError()); |
1021 | return None; |
1022 | } |
1023 | |
1024 | /// Helper for converting an Error to a bool. |
1025 | /// |
1026 | /// This method returns true if Err is in an error state, or false if it is |
1027 | /// in a success state. Puts Err in a checked state in both cases (unlike |
1028 | /// Error::operator bool(), which only does this for success states). |
1029 | inline bool errorToBool(Error Err) { |
1030 | bool IsError = static_cast<bool>(Err); |
1031 | if (IsError) |
1032 | consumeError(std::move(Err)); |
1033 | return IsError; |
1034 | } |
1035 | |
1036 | /// Helper for Errors used as out-parameters. |
1037 | /// |
1038 | /// This helper is for use with the Error-as-out-parameter idiom, where an error |
1039 | /// is passed to a function or method by reference, rather than being returned. |
1040 | /// In such cases it is helpful to set the checked bit on entry to the function |
1041 | /// so that the error can be written to (unchecked Errors abort on assignment) |
1042 | /// and clear the checked bit on exit so that clients cannot accidentally forget |
1043 | /// to check the result. This helper performs these actions automatically using |
1044 | /// RAII: |
1045 | /// |
1046 | /// @code{.cpp} |
1047 | /// Result foo(Error &Err) { |
1048 | /// ErrorAsOutParameter ErrAsOutParam(&Err); // 'Checked' flag set |
1049 | /// // <body of foo> |
1050 | /// // <- 'Checked' flag auto-cleared when ErrAsOutParam is destructed. |
1051 | /// } |
1052 | /// @endcode |
1053 | /// |
1054 | /// ErrorAsOutParameter takes an Error* rather than Error& so that it can be |
1055 | /// used with optional Errors (Error pointers that are allowed to be null). If |
1056 | /// ErrorAsOutParameter took an Error reference, an instance would have to be |
1057 | /// created inside every condition that verified that Error was non-null. By |
1058 | /// taking an Error pointer we can just create one instance at the top of the |
1059 | /// function. |
1060 | class ErrorAsOutParameter { |
1061 | public: |
1062 | ErrorAsOutParameter(Error *Err) : Err(Err) { |
1063 | // Raise the checked bit if Err is success. |
1064 | if (Err) |
1065 | (void)!!*Err; |
1066 | } |
1067 | |
1068 | ~ErrorAsOutParameter() { |
1069 | // Clear the checked bit. |
1070 | if (Err && !*Err) |
1071 | *Err = Error::success(); |
1072 | } |
1073 | |
1074 | private: |
1075 | Error *Err; |
1076 | }; |
1077 | |
1078 | /// Helper for Expected<T>s used as out-parameters. |
1079 | /// |
1080 | /// See ErrorAsOutParameter. |
1081 | template <typename T> |
1082 | class ExpectedAsOutParameter { |
1083 | public: |
1084 | ExpectedAsOutParameter(Expected<T> *ValOrErr) |
1085 | : ValOrErr(ValOrErr) { |
1086 | if (ValOrErr) |
1087 | (void)!!*ValOrErr; |
1088 | } |
1089 | |
1090 | ~ExpectedAsOutParameter() { |
1091 | if (ValOrErr) |
1092 | ValOrErr->setUnchecked(); |
1093 | } |
1094 | |
1095 | private: |
1096 | Expected<T> *ValOrErr; |
1097 | }; |
1098 | |
1099 | /// This class wraps a std::error_code in a Error. |
1100 | /// |
1101 | /// This is useful if you're writing an interface that returns a Error |
1102 | /// (or Expected) and you want to call code that still returns |
1103 | /// std::error_codes. |
1104 | class ECError : public ErrorInfo<ECError> { |
1105 | friend Error errorCodeToError(std::error_code); |
1106 | |
1107 | virtual void anchor() override; |
1108 | |
1109 | public: |
1110 | void setErrorCode(std::error_code EC) { this->EC = EC; } |
1111 | std::error_code convertToErrorCode() const override { return EC; } |
1112 | void log(raw_ostream &OS) const override { OS << EC.message(); } |
1113 | |
1114 | // Used by ErrorInfo::classID. |
1115 | static char ID; |
1116 | |
1117 | protected: |
1118 | ECError() = default; |
1119 | ECError(std::error_code EC) : EC(EC) {} |
1120 | |
1121 | std::error_code EC; |
1122 | }; |
1123 | |
1124 | /// The value returned by this function can be returned from convertToErrorCode |
1125 | /// for Error values where no sensible translation to std::error_code exists. |
1126 | /// It should only be used in this situation, and should never be used where a |
1127 | /// sensible conversion to std::error_code is available, as attempts to convert |
1128 | /// to/from this error will result in a fatal error. (i.e. it is a programmatic |
1129 | ///error to try to convert such a value). |
1130 | std::error_code inconvertibleErrorCode(); |
1131 | |
1132 | /// Helper for converting an std::error_code to a Error. |
1133 | Error errorCodeToError(std::error_code EC); |
1134 | |
1135 | /// Helper for converting an ECError to a std::error_code. |
1136 | /// |
1137 | /// This method requires that Err be Error() or an ECError, otherwise it |
1138 | /// will trigger a call to abort(). |
1139 | std::error_code errorToErrorCode(Error Err); |
1140 | |
1141 | /// Convert an ErrorOr<T> to an Expected<T>. |
1142 | template <typename T> Expected<T> errorOrToExpected(ErrorOr<T> &&EO) { |
1143 | if (auto EC = EO.getError()) |
1144 | return errorCodeToError(EC); |
1145 | return std::move(*EO); |
1146 | } |
1147 | |
1148 | /// Convert an Expected<T> to an ErrorOr<T>. |
1149 | template <typename T> ErrorOr<T> expectedToErrorOr(Expected<T> &&E) { |
1150 | if (auto Err = E.takeError()) |
1151 | return errorToErrorCode(std::move(Err)); |
1152 | return std::move(*E); |
1153 | } |
1154 | |
1155 | /// This class wraps a string in an Error. |
1156 | /// |
1157 | /// StringError is useful in cases where the client is not expected to be able |
1158 | /// to consume the specific error message programmatically (for example, if the |
1159 | /// error message is to be presented to the user). |
1160 | /// |
1161 | /// StringError can also be used when additional information is to be printed |
1162 | /// along with a error_code message. Depending on the constructor called, this |
1163 | /// class can either display: |
1164 | /// 1. the error_code message (ECError behavior) |
1165 | /// 2. a string |
1166 | /// 3. the error_code message and a string |
1167 | /// |
1168 | /// These behaviors are useful when subtyping is required; for example, when a |
1169 | /// specific library needs an explicit error type. In the example below, |
1170 | /// PDBError is derived from StringError: |
1171 | /// |
1172 | /// @code{.cpp} |
1173 | /// Expected<int> foo() { |
1174 | /// return llvm::make_error<PDBError>(pdb_error_code::dia_failed_loading, |
1175 | /// "Additional information"); |
1176 | /// } |
1177 | /// @endcode |
1178 | /// |
1179 | class StringError : public ErrorInfo<StringError> { |
1180 | public: |
1181 | static char ID; |
1182 | |
1183 | // Prints EC + S and converts to EC |
1184 | StringError(std::error_code EC, const Twine &S = Twine()); |
1185 | |
1186 | // Prints S and converts to EC |
1187 | StringError(const Twine &S, std::error_code EC); |
1188 | |
1189 | void log(raw_ostream &OS) const override; |
1190 | std::error_code convertToErrorCode() const override; |
1191 | |
1192 | const std::string &getMessage() const { return Msg; } |
1193 | |
1194 | private: |
1195 | std::string Msg; |
1196 | std::error_code EC; |
1197 | const bool PrintMsgOnly = false; |
1198 | }; |
1199 | |
1200 | /// Create formatted StringError object. |
1201 | template <typename... Ts> |
1202 | inline Error createStringError(std::error_code EC, char const *Fmt, |
1203 | const Ts &... Vals) { |
1204 | std::string Buffer; |
1205 | raw_string_ostream Stream(Buffer); |
1206 | Stream << format(Fmt, Vals...); |
1207 | return make_error<StringError>(Stream.str(), EC); |
1208 | } |
1209 | |
1210 | Error createStringError(std::error_code EC, char const *Msg); |
1211 | |
1212 | inline Error createStringError(std::error_code EC, const Twine &S) { |
1213 | return createStringError(EC, S.str().c_str()); |
1214 | } |
1215 | |
1216 | template <typename... Ts> |
1217 | inline Error createStringError(std::errc EC, char const *Fmt, |
1218 | const Ts &... Vals) { |
1219 | return createStringError(std::make_error_code(EC), Fmt, Vals...); |
1220 | } |
1221 | |
1222 | /// This class wraps a filename and another Error. |
1223 | /// |
1224 | /// In some cases, an error needs to live along a 'source' name, in order to |
1225 | /// show more detailed information to the user. |
1226 | class FileError final : public ErrorInfo<FileError> { |
1227 | |
1228 | friend Error createFileError(const Twine &, Error); |
1229 | friend Error createFileError(const Twine &, size_t, Error); |
1230 | |
1231 | public: |
1232 | void log(raw_ostream &OS) const override { |
1233 | assert(Err && !FileName.empty() && "Trying to log after takeError().")(static_cast <bool> (Err && !FileName.empty() && "Trying to log after takeError().") ? void (0) : __assert_fail ("Err && !FileName.empty() && \"Trying to log after takeError().\"" , "/build/llvm-toolchain-snapshot-13~++20210621111111+acefe0eaaf82/llvm/include/llvm/Support/Error.h" , 1233, __extension__ __PRETTY_FUNCTION__)); |
1234 | OS << "'" << FileName << "': "; |
1235 | if (Line.hasValue()) |
1236 | OS << "line " << Line.getValue() << ": "; |
1237 | Err->log(OS); |
1238 | } |
1239 | |
1240 | StringRef getFileName() { return FileName; } |
1241 | |
1242 | Error takeError() { return Error(std::move(Err)); } |
1243 | |
1244 | std::error_code convertToErrorCode() const override; |
1245 | |
1246 | // Used by ErrorInfo::classID. |
1247 | static char ID; |
1248 | |
1249 | private: |
1250 | FileError(const Twine &F, Optional<size_t> LineNum, |
1251 | std::unique_ptr<ErrorInfoBase> E) { |
1252 | assert(E && "Cannot create FileError from Error success value.")(static_cast <bool> (E && "Cannot create FileError from Error success value." ) ? void (0) : __assert_fail ("E && \"Cannot create FileError from Error success value.\"" , "/build/llvm-toolchain-snapshot-13~++20210621111111+acefe0eaaf82/llvm/include/llvm/Support/Error.h" , 1252, __extension__ __PRETTY_FUNCTION__)); |
1253 | assert(!F.isTriviallyEmpty() &&(static_cast <bool> (!F.isTriviallyEmpty() && "The file name provided to FileError must not be empty." ) ? void (0) : __assert_fail ("!F.isTriviallyEmpty() && \"The file name provided to FileError must not be empty.\"" , "/build/llvm-toolchain-snapshot-13~++20210621111111+acefe0eaaf82/llvm/include/llvm/Support/Error.h" , 1254, __extension__ __PRETTY_FUNCTION__)) |
1254 | "The file name provided to FileError must not be empty.")(static_cast <bool> (!F.isTriviallyEmpty() && "The file name provided to FileError must not be empty." ) ? void (0) : __assert_fail ("!F.isTriviallyEmpty() && \"The file name provided to FileError must not be empty.\"" , "/build/llvm-toolchain-snapshot-13~++20210621111111+acefe0eaaf82/llvm/include/llvm/Support/Error.h" , 1254, __extension__ __PRETTY_FUNCTION__)); |
1255 | FileName = F.str(); |
1256 | Err = std::move(E); |
1257 | Line = std::move(LineNum); |
1258 | } |
1259 | |
1260 | static Error build(const Twine &F, Optional<size_t> Line, Error E) { |
1261 | std::unique_ptr<ErrorInfoBase> Payload; |
1262 | handleAllErrors(std::move(E), |
1263 | [&](std::unique_ptr<ErrorInfoBase> EIB) -> Error { |
1264 | Payload = std::move(EIB); |
1265 | return Error::success(); |
1266 | }); |
1267 | return Error( |
1268 | std::unique_ptr<FileError>(new FileError(F, Line, std::move(Payload)))); |
1269 | } |
1270 | |
1271 | std::string FileName; |
1272 | Optional<size_t> Line; |
1273 | std::unique_ptr<ErrorInfoBase> Err; |
1274 | }; |
1275 | |
1276 | /// Concatenate a source file path and/or name with an Error. The resulting |
1277 | /// Error is unchecked. |
1278 | inline Error createFileError(const Twine &F, Error E) { |
1279 | return FileError::build(F, Optional<size_t>(), std::move(E)); |
1280 | } |
1281 | |
1282 | /// Concatenate a source file path and/or name with line number and an Error. |
1283 | /// The resulting Error is unchecked. |
1284 | inline Error createFileError(const Twine &F, size_t Line, Error E) { |
1285 | return FileError::build(F, Optional<size_t>(Line), std::move(E)); |
1286 | } |
1287 | |
1288 | /// Concatenate a source file path and/or name with a std::error_code |
1289 | /// to form an Error object. |
1290 | inline Error createFileError(const Twine &F, std::error_code EC) { |
1291 | return createFileError(F, errorCodeToError(EC)); |
1292 | } |
1293 | |
1294 | /// Concatenate a source file path and/or name with line number and |
1295 | /// std::error_code to form an Error object. |
1296 | inline Error createFileError(const Twine &F, size_t Line, std::error_code EC) { |
1297 | return createFileError(F, Line, errorCodeToError(EC)); |
1298 | } |
1299 | |
1300 | Error createFileError(const Twine &F, ErrorSuccess) = delete; |
1301 | |
1302 | /// Helper for check-and-exit error handling. |
1303 | /// |
1304 | /// For tool use only. NOT FOR USE IN LIBRARY CODE. |
1305 | /// |
1306 | class ExitOnError { |
1307 | public: |
1308 | /// Create an error on exit helper. |
1309 | ExitOnError(std::string Banner = "", int DefaultErrorExitCode = 1) |
1310 | : Banner(std::move(Banner)), |
1311 | GetExitCode([=](const Error &) { return DefaultErrorExitCode; }) {} |
1312 | |
1313 | /// Set the banner string for any errors caught by operator(). |
1314 | void setBanner(std::string Banner) { this->Banner = std::move(Banner); } |
1315 | |
1316 | /// Set the exit-code mapper function. |
1317 | void setExitCodeMapper(std::function<int(const Error &)> GetExitCode) { |
1318 | this->GetExitCode = std::move(GetExitCode); |
1319 | } |
1320 | |
1321 | /// Check Err. If it's in a failure state log the error(s) and exit. |
1322 | void operator()(Error Err) const { checkError(std::move(Err)); } |
1323 | |
1324 | /// Check E. If it's in a success state then return the contained value. If |
1325 | /// it's in a failure state log the error(s) and exit. |
1326 | template <typename T> T operator()(Expected<T> &&E) const { |
1327 | checkError(E.takeError()); |
1328 | return std::move(*E); |
1329 | } |
1330 | |
1331 | /// Check E. If it's in a success state then return the contained reference. If |
1332 | /// it's in a failure state log the error(s) and exit. |
1333 | template <typename T> T& operator()(Expected<T&> &&E) const { |
1334 | checkError(E.takeError()); |
1335 | return *E; |
1336 | } |
1337 | |
1338 | private: |
1339 | void checkError(Error Err) const { |
1340 | if (Err) { |
1341 | int ExitCode = GetExitCode(Err); |
1342 | logAllUnhandledErrors(std::move(Err), errs(), Banner); |
1343 | exit(ExitCode); |
1344 | } |
1345 | } |
1346 | |
1347 | std::string Banner; |
1348 | std::function<int(const Error &)> GetExitCode; |
1349 | }; |
1350 | |
1351 | /// Conversion from Error to LLVMErrorRef for C error bindings. |
1352 | inline LLVMErrorRef wrap(Error Err) { |
1353 | return reinterpret_cast<LLVMErrorRef>(Err.takePayload().release()); |
1354 | } |
1355 | |
1356 | /// Conversion from LLVMErrorRef to Error for C error bindings. |
1357 | inline Error unwrap(LLVMErrorRef ErrRef) { |
1358 | return Error(std::unique_ptr<ErrorInfoBase>( |
1359 | reinterpret_cast<ErrorInfoBase *>(ErrRef))); |
1360 | } |
1361 | |
1362 | } // end namespace llvm |
1363 | |
1364 | #endif // LLVM_SUPPORT_ERROR_H |