Bug Summary

File:lib/Bitcode/Reader/BitcodeReader.cpp
Warning:line 2945, column 3
1st function call argument is an uninitialized value

Annotated Source Code

/build/llvm-toolchain-snapshot-6.0~svn318211/lib/Bitcode/Reader/BitcodeReader.cpp

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

/build/llvm-toolchain-snapshot-6.0~svn318211/include/llvm/ADT/Twine.h

1//===- Twine.h - Fast Temporary String Concatenation ------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef LLVM_ADT_TWINE_H
11#define LLVM_ADT_TWINE_H
12
13#include "llvm/ADT/SmallVector.h"
14#include "llvm/ADT/StringRef.h"
15#include "llvm/Support/ErrorHandling.h"
16#include <cassert>
17#include <cstdint>
18#include <string>
19
20namespace llvm {
21
22 class formatv_object_base;
23 class raw_ostream;
24
25 /// Twine - A lightweight data structure for efficiently representing the
26 /// concatenation of temporary values as strings.
27 ///
28 /// A Twine is a kind of rope, it represents a concatenated string using a
29 /// binary-tree, where the string is the preorder of the nodes. Since the
30 /// Twine can be efficiently rendered into a buffer when its result is used,
31 /// it avoids the cost of generating temporary values for intermediate string
32 /// results -- particularly in cases when the Twine result is never
33 /// required. By explicitly tracking the type of leaf nodes, we can also avoid
34 /// the creation of temporary strings for conversions operations (such as
35 /// appending an integer to a string).
36 ///
37 /// A Twine is not intended for use directly and should not be stored, its
38 /// implementation relies on the ability to store pointers to temporary stack
39 /// objects which may be deallocated at the end of a statement. Twines should
40 /// only be used accepted as const references in arguments, when an API wishes
41 /// to accept possibly-concatenated strings.
42 ///
43 /// Twines support a special 'null' value, which always concatenates to form
44 /// itself, and renders as an empty string. This can be returned from APIs to
45 /// effectively nullify any concatenations performed on the result.
46 ///
47 /// \b Implementation
48 ///
49 /// Given the nature of a Twine, it is not possible for the Twine's
50 /// concatenation method to construct interior nodes; the result must be
51 /// represented inside the returned value. For this reason a Twine object
52 /// actually holds two values, the left- and right-hand sides of a
53 /// concatenation. We also have nullary Twine objects, which are effectively
54 /// sentinel values that represent empty strings.
55 ///
56 /// Thus, a Twine can effectively have zero, one, or two children. The \see
57 /// isNullary(), \see isUnary(), and \see isBinary() predicates exist for
58 /// testing the number of children.
59 ///
60 /// We maintain a number of invariants on Twine objects (FIXME: Why):
61 /// - Nullary twines are always represented with their Kind on the left-hand
62 /// side, and the Empty kind on the right-hand side.
63 /// - Unary twines are always represented with the value on the left-hand
64 /// side, and the Empty kind on the right-hand side.
65 /// - If a Twine has another Twine as a child, that child should always be
66 /// binary (otherwise it could have been folded into the parent).
67 ///
68 /// These invariants are check by \see isValid().
69 ///
70 /// \b Efficiency Considerations
71 ///
72 /// The Twine is designed to yield efficient and small code for common
73 /// situations. For this reason, the concat() method is inlined so that
74 /// concatenations of leaf nodes can be optimized into stores directly into a
75 /// single stack allocated object.
76 ///
77 /// In practice, not all compilers can be trusted to optimize concat() fully,
78 /// so we provide two additional methods (and accompanying operator+
79 /// overloads) to guarantee that particularly important cases (cstring plus
80 /// StringRef) codegen as desired.
81 class Twine {
82 /// NodeKind - Represent the type of an argument.
83 enum NodeKind : unsigned char {
84 /// An empty string; the result of concatenating anything with it is also
85 /// empty.
86 NullKind,
87
88 /// The empty string.
89 EmptyKind,
90
91 /// A pointer to a Twine instance.
92 TwineKind,
93
94 /// A pointer to a C string instance.
95 CStringKind,
96
97 /// A pointer to an std::string instance.
98 StdStringKind,
99
100 /// A pointer to a StringRef instance.
101 StringRefKind,
102
103 /// A pointer to a SmallString instance.
104 SmallStringKind,
105
106 /// A pointer to a formatv_object_base instance.
107 FormatvObjectKind,
108
109 /// A char value, to render as a character.
110 CharKind,
111
112 /// An unsigned int value, to render as an unsigned decimal integer.
113 DecUIKind,
114
115 /// An int value, to render as a signed decimal integer.
116 DecIKind,
117
118 /// A pointer to an unsigned long value, to render as an unsigned decimal
119 /// integer.
120 DecULKind,
121
122 /// A pointer to a long value, to render as a signed decimal integer.
123 DecLKind,
124
125 /// A pointer to an unsigned long long value, to render as an unsigned
126 /// decimal integer.
127 DecULLKind,
128
129 /// A pointer to a long long value, to render as a signed decimal integer.
130 DecLLKind,
131
132 /// A pointer to a uint64_t value, to render as an unsigned hexadecimal
133 /// integer.
134 UHexKind
135 };
136
137 union Child
138 {
139 const Twine *twine;
140 const char *cString;
141 const std::string *stdString;
142 const StringRef *stringRef;
143 const SmallVectorImpl<char> *smallString;
144 const formatv_object_base *formatvObject;
145 char character;
146 unsigned int decUI;
147 int decI;
148 const unsigned long *decUL;
149 const long *decL;
150 const unsigned long long *decULL;
151 const long long *decLL;
152 const uint64_t *uHex;
153 };
154
155 /// LHS - The prefix in the concatenation, which may be uninitialized for
156 /// Null or Empty kinds.
157 Child LHS;
158
159 /// RHS - The suffix in the concatenation, which may be uninitialized for
160 /// Null or Empty kinds.
161 Child RHS;
162
163 /// LHSKind - The NodeKind of the left hand side, \see getLHSKind().
164 NodeKind LHSKind = EmptyKind;
165
166 /// RHSKind - The NodeKind of the right hand side, \see getRHSKind().
167 NodeKind RHSKind = EmptyKind;
168
169 /// Construct a nullary twine; the kind must be NullKind or EmptyKind.
170 explicit Twine(NodeKind Kind) : LHSKind(Kind) {
171 assert(isNullary() && "Invalid kind!")((isNullary() && "Invalid kind!") ? static_cast<void
> (0) : __assert_fail ("isNullary() && \"Invalid kind!\""
, "/build/llvm-toolchain-snapshot-6.0~svn318211/include/llvm/ADT/Twine.h"
, 171, __PRETTY_FUNCTION__))
;
172 }
173
174 /// Construct a binary twine.
175 explicit Twine(const Twine &LHS, const Twine &RHS)
176 : LHSKind(TwineKind), RHSKind(TwineKind) {
177 this->LHS.twine = &LHS;
178 this->RHS.twine = &RHS;
179 assert(isValid() && "Invalid twine!")((isValid() && "Invalid twine!") ? static_cast<void
> (0) : __assert_fail ("isValid() && \"Invalid twine!\""
, "/build/llvm-toolchain-snapshot-6.0~svn318211/include/llvm/ADT/Twine.h"
, 179, __PRETTY_FUNCTION__))
;
180 }
181
182 /// Construct a twine from explicit values.
183 explicit Twine(Child LHS, NodeKind LHSKind, Child RHS, NodeKind RHSKind)
184 : LHS(LHS), RHS(RHS), LHSKind(LHSKind), RHSKind(RHSKind) {
185 assert(isValid() && "Invalid twine!")((isValid() && "Invalid twine!") ? static_cast<void
> (0) : __assert_fail ("isValid() && \"Invalid twine!\""
, "/build/llvm-toolchain-snapshot-6.0~svn318211/include/llvm/ADT/Twine.h"
, 185, __PRETTY_FUNCTION__))
;
186 }
187
188 /// Check for the null twine.
189 bool isNull() const {
190 return getLHSKind() == NullKind;
191 }
192
193 /// Check for the empty twine.
194 bool isEmpty() const {
195 return getLHSKind() == EmptyKind;
196 }
197
198 /// Check if this is a nullary twine (null or empty).
199 bool isNullary() const {
200 return isNull() || isEmpty();
201 }
202
203 /// Check if this is a unary twine.
204 bool isUnary() const {
205 return getRHSKind() == EmptyKind && !isNullary();
206 }
207
208 /// Check if this is a binary twine.
209 bool isBinary() const {
210 return getLHSKind() != NullKind && getRHSKind() != EmptyKind;
211 }
212
213 /// Check if this is a valid twine (satisfying the invariants on
214 /// order and number of arguments).
215 bool isValid() const {
216 // Nullary twines always have Empty on the RHS.
217 if (isNullary() && getRHSKind() != EmptyKind)
218 return false;
219
220 // Null should never appear on the RHS.
221 if (getRHSKind() == NullKind)
222 return false;
223
224 // The RHS cannot be non-empty if the LHS is empty.
225 if (getRHSKind() != EmptyKind && getLHSKind() == EmptyKind)
226 return false;
227
228 // A twine child should always be binary.
229 if (getLHSKind() == TwineKind &&
230 !LHS.twine->isBinary())
231 return false;
232 if (getRHSKind() == TwineKind &&
233 !RHS.twine->isBinary())
234 return false;
235
236 return true;
237 }
238
239 /// Get the NodeKind of the left-hand side.
240 NodeKind getLHSKind() const { return LHSKind; }
241
242 /// Get the NodeKind of the right-hand side.
243 NodeKind getRHSKind() const { return RHSKind; }
244
245 /// Print one child from a twine.
246 void printOneChild(raw_ostream &OS, Child Ptr, NodeKind Kind) const;
247
248 /// Print the representation of one child from a twine.
249 void printOneChildRepr(raw_ostream &OS, Child Ptr,
250 NodeKind Kind) const;
251
252 public:
253 /// @name Constructors
254 /// @{
255
256 /// Construct from an empty string.
257 /*implicit*/ Twine() {
258 assert(isValid() && "Invalid twine!")((isValid() && "Invalid twine!") ? static_cast<void
> (0) : __assert_fail ("isValid() && \"Invalid twine!\""
, "/build/llvm-toolchain-snapshot-6.0~svn318211/include/llvm/ADT/Twine.h"
, 258, __PRETTY_FUNCTION__))
;
259 }
260
261 Twine(const Twine &) = default;
262
263 /// Construct from a C string.
264 ///
265 /// We take care here to optimize "" into the empty twine -- this will be
266 /// optimized out for string constants. This allows Twine arguments have
267 /// default "" values, without introducing unnecessary string constants.
268 /*implicit*/ Twine(const char *Str) {
15
Calling implicit default constructor for 'Child'
16
Returning from default constructor for 'Child'
17
Calling implicit default constructor for 'Child'
18
Returning from default constructor for 'Child'
269 if (Str[0] != '\0') {
19
Taking true branch
270 LHS.cString = Str;
271 LHSKind = CStringKind;
272 } else
273 LHSKind = EmptyKind;
274
275 assert(isValid() && "Invalid twine!")((isValid() && "Invalid twine!") ? static_cast<void
> (0) : __assert_fail ("isValid() && \"Invalid twine!\""
, "/build/llvm-toolchain-snapshot-6.0~svn318211/include/llvm/ADT/Twine.h"
, 275, __PRETTY_FUNCTION__))
;
20
Within the expansion of the macro 'assert':
a
Assuming the condition is true
276 }
277
278 /// Construct from an std::string.
279 /*implicit*/ Twine(const std::string &Str) : LHSKind(StdStringKind) {
26
Calling implicit default constructor for 'Child'
27
Returning from default constructor for 'Child'
28
Calling implicit default constructor for 'Child'
29
Returning from default constructor for 'Child'
280 LHS.stdString = &Str;
281 assert(isValid() && "Invalid twine!")((isValid() && "Invalid twine!") ? static_cast<void
> (0) : __assert_fail ("isValid() && \"Invalid twine!\""
, "/build/llvm-toolchain-snapshot-6.0~svn318211/include/llvm/ADT/Twine.h"
, 281, __PRETTY_FUNCTION__))
;
30
Within the expansion of the macro 'assert':
a
Assuming the condition is true
282 }
283
284 /// Construct from a StringRef.
285 /*implicit*/ Twine(const StringRef &Str) : LHSKind(StringRefKind) {
286 LHS.stringRef = &Str;
287 assert(isValid() && "Invalid twine!")((isValid() && "Invalid twine!") ? static_cast<void
> (0) : __assert_fail ("isValid() && \"Invalid twine!\""
, "/build/llvm-toolchain-snapshot-6.0~svn318211/include/llvm/ADT/Twine.h"
, 287, __PRETTY_FUNCTION__))
;
288 }
289
290 /// Construct from a SmallString.
291 /*implicit*/ Twine(const SmallVectorImpl<char> &Str)
292 : LHSKind(SmallStringKind) {
293 LHS.smallString = &Str;
294 assert(isValid() && "Invalid twine!")((isValid() && "Invalid twine!") ? static_cast<void
> (0) : __assert_fail ("isValid() && \"Invalid twine!\""
, "/build/llvm-toolchain-snapshot-6.0~svn318211/include/llvm/ADT/Twine.h"
, 294, __PRETTY_FUNCTION__))
;
295 }
296
297 /// Construct from a formatv_object_base.
298 /*implicit*/ Twine(const formatv_object_base &Fmt)
299 : LHSKind(FormatvObjectKind) {
300 LHS.formatvObject = &Fmt;
301 assert(isValid() && "Invalid twine!")((isValid() && "Invalid twine!") ? static_cast<void
> (0) : __assert_fail ("isValid() && \"Invalid twine!\""
, "/build/llvm-toolchain-snapshot-6.0~svn318211/include/llvm/ADT/Twine.h"
, 301, __PRETTY_FUNCTION__))
;
302 }
303
304 /// Construct from a char.
305 explicit Twine(char Val) : LHSKind(CharKind) {
306 LHS.character = Val;
307 }
308
309 /// Construct from a signed char.
310 explicit Twine(signed char Val) : LHSKind(CharKind) {
311 LHS.character = static_cast<char>(Val);
312 }
313
314 /// Construct from an unsigned char.
315 explicit Twine(unsigned char Val) : LHSKind(CharKind) {
316 LHS.character = static_cast<char>(Val);
317 }
318
319 /// Construct a twine to print \p Val as an unsigned decimal integer.
320 explicit Twine(unsigned Val) : LHSKind(DecUIKind) {
321 LHS.decUI = Val;
322 }
323
324 /// Construct a twine to print \p Val as a signed decimal integer.
325 explicit Twine(int Val) : LHSKind(DecIKind) {
326 LHS.decI = Val;
327 }
328
329 /// Construct a twine to print \p Val as an unsigned decimal integer.
330 explicit Twine(const unsigned long &Val) : LHSKind(DecULKind) {
331 LHS.decUL = &Val;
332 }
333
334 /// Construct a twine to print \p Val as a signed decimal integer.
335 explicit Twine(const long &Val) : LHSKind(DecLKind) {
336 LHS.decL = &Val;
337 }
338
339 /// Construct a twine to print \p Val as an unsigned decimal integer.
340 explicit Twine(const unsigned long long &Val) : LHSKind(DecULLKind) {
341 LHS.decULL = &Val;
342 }
343
344 /// Construct a twine to print \p Val as a signed decimal integer.
345 explicit Twine(const long long &Val) : LHSKind(DecLLKind) {
346 LHS.decLL = &Val;
347 }
348
349 // FIXME: Unfortunately, to make sure this is as efficient as possible we
350 // need extra binary constructors from particular types. We can't rely on
351 // the compiler to be smart enough to fold operator+()/concat() down to the
352 // right thing. Yet.
353
354 /// Construct as the concatenation of a C string and a StringRef.
355 /*implicit*/ Twine(const char *LHS, const StringRef &RHS)
356 : LHSKind(CStringKind), RHSKind(StringRefKind) {
357 this->LHS.cString = LHS;
358 this->RHS.stringRef = &RHS;
359 assert(isValid() && "Invalid twine!")((isValid() && "Invalid twine!") ? static_cast<void
> (0) : __assert_fail ("isValid() && \"Invalid twine!\""
, "/build/llvm-toolchain-snapshot-6.0~svn318211/include/llvm/ADT/Twine.h"
, 359, __PRETTY_FUNCTION__))
;
360 }
361
362 /// Construct as the concatenation of a StringRef and a C string.
363 /*implicit*/ Twine(const StringRef &LHS, const char *RHS)
364 : LHSKind(StringRefKind), RHSKind(CStringKind) {
365 this->LHS.stringRef = &LHS;
366 this->RHS.cString = RHS;
367 assert(isValid() && "Invalid twine!")((isValid() && "Invalid twine!") ? static_cast<void
> (0) : __assert_fail ("isValid() && \"Invalid twine!\""
, "/build/llvm-toolchain-snapshot-6.0~svn318211/include/llvm/ADT/Twine.h"
, 367, __PRETTY_FUNCTION__))
;
368 }
369
370 /// Since the intended use of twines is as temporary objects, assignments
371 /// when concatenating might cause undefined behavior or stack corruptions
372 Twine &operator=(const Twine &) = delete;
373
374 /// Create a 'null' string, which is an empty string that always
375 /// concatenates to form another empty string.
376 static Twine createNull() {
377 return Twine(NullKind);
378 }
379
380 /// @}
381 /// @name Numeric Conversions
382 /// @{
383
384 // Construct a twine to print \p Val as an unsigned hexadecimal integer.
385 static Twine utohexstr(const uint64_t &Val) {
386 Child LHS, RHS;
387 LHS.uHex = &Val;
388 RHS.twine = nullptr;
389 return Twine(LHS, UHexKind, RHS, EmptyKind);
390 }
391
392 /// @}
393 /// @name Predicate Operations
394 /// @{
395
396 /// Check if this twine is trivially empty; a false return value does not
397 /// necessarily mean the twine is empty.
398 bool isTriviallyEmpty() const {
399 return isNullary();
400 }
401
402 /// Return true if this twine can be dynamically accessed as a single
403 /// StringRef value with getSingleStringRef().
404 bool isSingleStringRef() const {
405 if (getRHSKind() != EmptyKind) return false;
406
407 switch (getLHSKind()) {
408 case EmptyKind:
409 case CStringKind:
410 case StdStringKind:
411 case StringRefKind:
412 case SmallStringKind:
413 return true;
414 default:
415 return false;
416 }
417 }
418
419 /// @}
420 /// @name String Operations
421 /// @{
422
423 Twine concat(const Twine &Suffix) const;
424
425 /// @}
426 /// @name Output & Conversion.
427 /// @{
428
429 /// Return the twine contents as a std::string.
430 std::string str() const;
431
432 /// Append the concatenated string into the given SmallString or SmallVector.
433 void toVector(SmallVectorImpl<char> &Out) const;
434
435 /// This returns the twine as a single StringRef. This method is only valid
436 /// if isSingleStringRef() is true.
437 StringRef getSingleStringRef() const {
438 assert(isSingleStringRef() &&"This cannot be had as a single stringref!")((isSingleStringRef() &&"This cannot be had as a single stringref!"
) ? static_cast<void> (0) : __assert_fail ("isSingleStringRef() &&\"This cannot be had as a single stringref!\""
, "/build/llvm-toolchain-snapshot-6.0~svn318211/include/llvm/ADT/Twine.h"
, 438, __PRETTY_FUNCTION__))
;
439 switch (getLHSKind()) {
440 default: llvm_unreachable("Out of sync with isSingleStringRef")::llvm::llvm_unreachable_internal("Out of sync with isSingleStringRef"
, "/build/llvm-toolchain-snapshot-6.0~svn318211/include/llvm/ADT/Twine.h"
, 440)
;
441 case EmptyKind: return StringRef();
442 case CStringKind: return StringRef(LHS.cString);
443 case StdStringKind: return StringRef(*LHS.stdString);
444 case StringRefKind: return *LHS.stringRef;
445 case SmallStringKind:
446 return StringRef(LHS.smallString->data(), LHS.smallString->size());
447 }
448 }
449
450 /// This returns the twine as a single StringRef if it can be
451 /// represented as such. Otherwise the twine is written into the given
452 /// SmallVector and a StringRef to the SmallVector's data is returned.
453 StringRef toStringRef(SmallVectorImpl<char> &Out) const {
454 if (isSingleStringRef())
455 return getSingleStringRef();
456 toVector(Out);
457 return StringRef(Out.data(), Out.size());
458 }
459
460 /// This returns the twine as a single null terminated StringRef if it
461 /// can be represented as such. Otherwise the twine is written into the
462 /// given SmallVector and a StringRef to the SmallVector's data is returned.
463 ///
464 /// The returned StringRef's size does not include the null terminator.
465 StringRef toNullTerminatedStringRef(SmallVectorImpl<char> &Out) const;
466
467 /// Write the concatenated string represented by this twine to the
468 /// stream \p OS.
469 void print(raw_ostream &OS) const;
470
471 /// Dump the concatenated string represented by this twine to stderr.
472 void dump() const;
473
474 /// Write the representation of this twine to the stream \p OS.
475 void printRepr(raw_ostream &OS) const;
476
477 /// Dump the representation of this twine to stderr.
478 void dumpRepr() const;
479
480 /// @}
481 };
482
483 /// @name Twine Inline Implementations
484 /// @{
485
486 inline Twine Twine::concat(const Twine &Suffix) const {
487 // Concatenation with null is null.
488 if (isNull() || Suffix.isNull())
489 return Twine(NullKind);
490
491 // Concatenation with empty yields the other side.
492 if (isEmpty())
493 return Suffix;
494 if (Suffix.isEmpty())
495 return *this;
496
497 // Otherwise we need to create a new node, taking care to fold in unary
498 // twines.
499 Child NewLHS, NewRHS;
500 NewLHS.twine = this;
501 NewRHS.twine = &Suffix;
502 NodeKind NewLHSKind = TwineKind, NewRHSKind = TwineKind;
503 if (isUnary()) {
504 NewLHS = LHS;
505 NewLHSKind = getLHSKind();
506 }
507 if (Suffix.isUnary()) {
508 NewRHS = Suffix.LHS;
509 NewRHSKind = Suffix.getLHSKind();
510 }
511
512 return Twine(NewLHS, NewLHSKind, NewRHS, NewRHSKind);
513 }
514
515 inline Twine operator+(const Twine &LHS, const Twine &RHS) {
516 return LHS.concat(RHS);
517 }
518
519 /// Additional overload to guarantee simplified codegen; this is equivalent to
520 /// concat().
521
522 inline Twine operator+(const char *LHS, const StringRef &RHS) {
523 return Twine(LHS, RHS);
524 }
525
526 /// Additional overload to guarantee simplified codegen; this is equivalent to
527 /// concat().
528
529 inline Twine operator+(const StringRef &LHS, const char *RHS) {
530 return Twine(LHS, RHS);
531 }
532
533 inline raw_ostream &operator<<(raw_ostream &OS, const Twine &RHS) {
534 RHS.print(OS);
535 return OS;
536 }
537
538 /// @}
539
540} // end namespace llvm
541
542#endif // LLVM_ADT_TWINE_H

/build/llvm-toolchain-snapshot-6.0~svn318211/include/llvm/Bitcode/BitcodeReader.h

1//===- llvm/Bitcode/BitcodeReader.h - Bitcode reader ------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This header defines interfaces to read LLVM bitcode files/streams.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_BITCODE_BITCODEREADER_H
15#define LLVM_BITCODE_BITCODEREADER_H
16
17#include "llvm/ADT/ArrayRef.h"
18#include "llvm/ADT/StringRef.h"
19#include "llvm/Bitcode/BitCodes.h"
20#include "llvm/IR/ModuleSummaryIndex.h"
21#include "llvm/Support/Endian.h"
22#include "llvm/Support/Error.h"
23#include "llvm/Support/ErrorOr.h"
24#include "llvm/Support/MemoryBuffer.h"
25#include <cstdint>
26#include <memory>
27#include <string>
28#include <system_error>
29#include <vector>
30namespace llvm {
31
32class LLVMContext;
33class Module;
34
35 // These functions are for converting Expected/Error values to
36 // ErrorOr/std::error_code for compatibility with legacy clients. FIXME:
37 // Remove these functions once no longer needed by the C and libLTO APIs.
38
39 std::error_code errorToErrorCodeAndEmitErrors(LLVMContext &Ctx, Error Err);
40
41 template <typename T>
42 ErrorOr<T> expectedToErrorOrAndEmitErrors(LLVMContext &Ctx, Expected<T> Val) {
43 if (!Val)
44 return errorToErrorCodeAndEmitErrors(Ctx, Val.takeError());
45 return std::move(*Val);
46 }
47
48 struct BitcodeFileContents;
49
50 /// Basic information extracted from a bitcode module to be used for LTO.
51 struct BitcodeLTOInfo {
52 bool IsThinLTO;
53 bool HasSummary;
54 };
55
56 /// Represents a module in a bitcode file.
57 class BitcodeModule {
58 // This covers the identification (if present) and module blocks.
59 ArrayRef<uint8_t> Buffer;
60 StringRef ModuleIdentifier;
61
62 // The string table used to interpret this module.
63 StringRef Strtab;
64
65 // The bitstream location of the IDENTIFICATION_BLOCK.
66 uint64_t IdentificationBit;
67
68 // The bitstream location of this module's MODULE_BLOCK.
69 uint64_t ModuleBit;
70
71 BitcodeModule(ArrayRef<uint8_t> Buffer, StringRef ModuleIdentifier,
72 uint64_t IdentificationBit, uint64_t ModuleBit)
73 : Buffer(Buffer), ModuleIdentifier(ModuleIdentifier),
74 IdentificationBit(IdentificationBit), ModuleBit(ModuleBit) {}
75
76 // Calls the ctor.
77 friend Expected<BitcodeFileContents>
78 getBitcodeFileContents(MemoryBufferRef Buffer);
79
80 Expected<std::unique_ptr<Module>> getModuleImpl(LLVMContext &Context,
81 bool MaterializeAll,
82 bool ShouldLazyLoadMetadata,
83 bool IsImporting);
84
85 public:
86 StringRef getBuffer() const {
87 return StringRef((const char *)Buffer.begin(), Buffer.size());
88 }
89
90 StringRef getStrtab() const { return Strtab; }
91
92 StringRef getModuleIdentifier() const { return ModuleIdentifier; }
93
94 /// Read the bitcode module and prepare for lazy deserialization of function
95 /// bodies. If ShouldLazyLoadMetadata is true, lazily load metadata as well.
96 /// If IsImporting is true, this module is being parsed for ThinLTO
97 /// importing into another module.
98 Expected<std::unique_ptr<Module>> getLazyModule(LLVMContext &Context,
99 bool ShouldLazyLoadMetadata,
100 bool IsImporting);
101
102 /// Read the entire bitcode module and return it.
103 Expected<std::unique_ptr<Module>> parseModule(LLVMContext &Context);
104
105 /// Returns information about the module to be used for LTO: whether to
106 /// compile with ThinLTO, and whether it has a summary.
107 Expected<BitcodeLTOInfo> getLTOInfo();
108
109 /// Parse the specified bitcode buffer, returning the module summary index.
110 Expected<std::unique_ptr<ModuleSummaryIndex>> getSummary();
111
112 /// Parse the specified bitcode buffer and merge its module summary index
113 /// into CombinedIndex.
114 Error readSummary(ModuleSummaryIndex &CombinedIndex, StringRef ModulePath,
115 uint64_t ModuleId);
116 };
117
118 struct BitcodeFileContents {
119 std::vector<BitcodeModule> Mods;
120 StringRef Symtab, StrtabForSymtab;
121 };
122
123 /// Returns the contents of a bitcode file. This includes the raw contents of
124 /// the symbol table embedded in the bitcode file. Clients which require a
125 /// symbol table should prefer to use irsymtab::read instead of this function
126 /// because it creates a reader for the irsymtab and handles upgrading bitcode
127 /// files without a symbol table or with an old symbol table.
128 Expected<BitcodeFileContents> getBitcodeFileContents(MemoryBufferRef Buffer);
129
130 /// Returns a list of modules in the specified bitcode buffer.
131 Expected<std::vector<BitcodeModule>>
132 getBitcodeModuleList(MemoryBufferRef Buffer);
133
134 /// Read the header of the specified bitcode buffer and prepare for lazy
135 /// deserialization of function bodies. If ShouldLazyLoadMetadata is true,
136 /// lazily load metadata as well. If IsImporting is true, this module is
137 /// being parsed for ThinLTO importing into another module.
138 Expected<std::unique_ptr<Module>>
139 getLazyBitcodeModule(MemoryBufferRef Buffer, LLVMContext &Context,
140 bool ShouldLazyLoadMetadata = false,
141 bool IsImporting = false);
142
143 /// Like getLazyBitcodeModule, except that the module takes ownership of
144 /// the memory buffer if successful. If successful, this moves Buffer. On
145 /// error, this *does not* move Buffer. If IsImporting is true, this module is
146 /// being parsed for ThinLTO importing into another module.
147 Expected<std::unique_ptr<Module>> getOwningLazyBitcodeModule(
148 std::unique_ptr<MemoryBuffer> &&Buffer, LLVMContext &Context,
149 bool ShouldLazyLoadMetadata = false, bool IsImporting = false);
150
151 /// Read the header of the specified bitcode buffer and extract just the
152 /// triple information. If successful, this returns a string. On error, this
153 /// returns "".
154 Expected<std::string> getBitcodeTargetTriple(MemoryBufferRef Buffer);
155
156 /// Return true if \p Buffer contains a bitcode file with ObjC code (category
157 /// or class) in it.
158 Expected<bool> isBitcodeContainingObjCCategory(MemoryBufferRef Buffer);
159
160 /// Read the header of the specified bitcode buffer and extract just the
161 /// producer string information. If successful, this returns a string. On
162 /// error, this returns "".
163 Expected<std::string> getBitcodeProducerString(MemoryBufferRef Buffer);
164
165 /// Read the specified bitcode file, returning the module.
166 Expected<std::unique_ptr<Module>> parseBitcodeFile(MemoryBufferRef Buffer,
167 LLVMContext &Context);
168
169 /// Returns LTO information for the specified bitcode file.
170 Expected<BitcodeLTOInfo> getBitcodeLTOInfo(MemoryBufferRef Buffer);
171
172 /// Parse the specified bitcode buffer, returning the module summary index.
173 Expected<std::unique_ptr<ModuleSummaryIndex>>
174 getModuleSummaryIndex(MemoryBufferRef Buffer);
175
176 /// Parse the specified bitcode buffer and merge the index into CombinedIndex.
177 Error readModuleSummaryIndex(MemoryBufferRef Buffer,
178 ModuleSummaryIndex &CombinedIndex,
179 uint64_t ModuleId);
180
181 /// Parse the module summary index out of an IR file and return the module
182 /// summary index object if found, or an empty summary if not. If Path refers
183 /// to an empty file and IgnoreEmptyThinLTOIndexFile is true, then
184 /// this function will return nullptr.
185 Expected<std::unique_ptr<ModuleSummaryIndex>>
186 getModuleSummaryIndexForFile(StringRef Path,
187 bool IgnoreEmptyThinLTOIndexFile = false);
188
189 /// isBitcodeWrapper - Return true if the given bytes are the magic bytes
190 /// for an LLVM IR bitcode wrapper.
191 inline bool isBitcodeWrapper(const unsigned char *BufPtr,
192 const unsigned char *BufEnd) {
193 // See if you can find the hidden message in the magic bytes :-).
194 // (Hint: it's a little-endian encoding.)
195 return BufPtr != BufEnd &&
196 BufPtr[0] == 0xDE &&
197 BufPtr[1] == 0xC0 &&
198 BufPtr[2] == 0x17 &&
199 BufPtr[3] == 0x0B;
200 }
201
202 /// isRawBitcode - Return true if the given bytes are the magic bytes for
203 /// raw LLVM IR bitcode (without a wrapper).
204 inline bool isRawBitcode(const unsigned char *BufPtr,
205 const unsigned char *BufEnd) {
206 // These bytes sort of have a hidden message, but it's not in
207 // little-endian this time, and it's a little redundant.
208 return BufPtr != BufEnd &&
209 BufPtr[0] == 'B' &&
210 BufPtr[1] == 'C' &&
211 BufPtr[2] == 0xc0 &&
212 BufPtr[3] == 0xde;
213 }
214
215 /// isBitcode - Return true if the given bytes are the magic bytes for
216 /// LLVM IR bitcode, either with or without a wrapper.
217 inline bool isBitcode(const unsigned char *BufPtr,
218 const unsigned char *BufEnd) {
219 return isBitcodeWrapper(BufPtr, BufEnd) ||
220 isRawBitcode(BufPtr, BufEnd);
221 }
222
223 /// SkipBitcodeWrapperHeader - Some systems wrap bc files with a special
224 /// header for padding or other reasons. The format of this header is:
225 ///
226 /// struct bc_header {
227 /// uint32_t Magic; // 0x0B17C0DE
228 /// uint32_t Version; // Version, currently always 0.
229 /// uint32_t BitcodeOffset; // Offset to traditional bitcode file.
230 /// uint32_t BitcodeSize; // Size of traditional bitcode file.
231 /// ... potentially other gunk ...
232 /// };
233 ///
234 /// This function is called when we find a file with a matching magic number.
235 /// In this case, skip down to the subsection of the file that is actually a
236 /// BC file.
237 /// If 'VerifyBufferSize' is true, check that the buffer is large enough to
238 /// contain the whole bitcode file.
239 inline bool SkipBitcodeWrapperHeader(const unsigned char *&BufPtr,
240 const unsigned char *&BufEnd,
241 bool VerifyBufferSize) {
242 // Must contain the offset and size field!
243 if (unsigned(BufEnd - BufPtr) < BWH_SizeField + 4)
244 return true;
245
246 unsigned Offset = support::endian::read32le(&BufPtr[BWH_OffsetField]);
247 unsigned Size = support::endian::read32le(&BufPtr[BWH_SizeField]);
248 uint64_t BitcodeOffsetEnd = (uint64_t)Offset + (uint64_t)Size;
249
250 // Verify that Offset+Size fits in the file.
251 if (VerifyBufferSize && BitcodeOffsetEnd > uint64_t(BufEnd-BufPtr))
252 return true;
253 BufPtr += Offset;
254 BufEnd = BufPtr+Size;
255 return false;
256 }
257
258 const std::error_category &BitcodeErrorCategory();
259 enum class BitcodeError { CorruptedBitcode = 1 };
260 inline std::error_code make_error_code(BitcodeError E) {
261 return std::error_code(static_cast<int>(E), BitcodeErrorCategory());
34
Calling 'BitcodeErrorCategory'
51
Returning from 'BitcodeErrorCategory'
52
Calling constructor for 'error_code'
53
Returning from constructor for 'error_code'
262 }
263
264} // end namespace llvm
265
266namespace std {
267
268template <> struct is_error_code_enum<llvm::BitcodeError> : std::true_type {};
269
270} // end namespace std
271
272#endif // LLVM_BITCODE_BITCODEREADER_H

/build/llvm-toolchain-snapshot-6.0~svn318211/include/llvm/Support/ManagedStatic.h

1//===-- llvm/Support/ManagedStatic.h - Static Global wrapper ----*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the ManagedStatic class and the llvm_shutdown() function.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_SUPPORT_MANAGEDSTATIC_H
15#define LLVM_SUPPORT_MANAGEDSTATIC_H
16
17#include <atomic>
18#include <cstddef>
19
20namespace llvm {
21
22/// object_creator - Helper method for ManagedStatic.
23template <class C> struct object_creator {
24 static void *call() { return new C(); }
25};
26
27/// object_deleter - Helper method for ManagedStatic.
28///
29template <typename T> struct object_deleter {
30 static void call(void *Ptr) { delete (T *)Ptr; }
31};
32template <typename T, size_t N> struct object_deleter<T[N]> {
33 static void call(void *Ptr) { delete[](T *)Ptr; }
34};
35
36/// ManagedStaticBase - Common base class for ManagedStatic instances.
37class ManagedStaticBase {
38protected:
39 // This should only be used as a static variable, which guarantees that this
40 // will be zero initialized.
41 mutable std::atomic<void *> Ptr;
42 mutable void (*DeleterFn)(void*);
43 mutable const ManagedStaticBase *Next;
44
45 void RegisterManagedStatic(void *(*creator)(), void (*deleter)(void*)) const;
46
47public:
48 /// isConstructed - Return true if this object has not been created yet.
49 bool isConstructed() const { return Ptr != nullptr; }
50
51 void destroy() const;
52};
53
54/// ManagedStatic - This transparently changes the behavior of global statics to
55/// be lazily constructed on demand (good for reducing startup times of dynamic
56/// libraries that link in LLVM components) and for making destruction be
57/// explicit through the llvm_shutdown() function call.
58///
59template <class C, class Creator = object_creator<C>,
60 class Deleter = object_deleter<C>>
61class ManagedStatic : public ManagedStaticBase {
62public:
63 // Accessors.
64 C &operator*() {
65 void *Tmp = Ptr.load(std::memory_order_acquire);
36
Calling 'atomic::load'
41
Returning from 'atomic::load'
66 if (!Tmp)
42
Assuming 'Tmp' is non-null
43
Taking false branch
67 RegisterManagedStatic(Creator::call, Deleter::call);
68
69 return *static_cast<C *>(Ptr.load(std::memory_order_relaxed));
44
Calling 'atomic::load'
49
Returning from 'atomic::load'
70 }
71
72 C *operator->() { return &**this; }
73
74 const C &operator*() const {
75 void *Tmp = Ptr.load(std::memory_order_acquire);
76 if (!Tmp)
77 RegisterManagedStatic(Creator::call, Deleter::call);
78
79 return *static_cast<C *>(Ptr.load(std::memory_order_relaxed));
80 }
81
82 const C *operator->() const { return &**this; }
83};
84
85/// llvm_shutdown - Deallocate and destroy all ManagedStatic variables.
86void llvm_shutdown();
87
88/// llvm_shutdown_obj - This is a simple helper class that calls
89/// llvm_shutdown() when it is destroyed.
90struct llvm_shutdown_obj {
91 llvm_shutdown_obj() = default;
92 ~llvm_shutdown_obj() { llvm_shutdown(); }
93};
94
95} // end namespace llvm
96
97#endif // LLVM_SUPPORT_MANAGEDSTATIC_H

/usr/lib/gcc/x86_64-linux-gnu/7.2.0/../../../../include/c++/7.2.0/atomic

1// -*- C++ -*- header.
2
3// Copyright (C) 2008-2017 Free Software Foundation, Inc.
4//
5// This file is part of the GNU ISO C++ Library. This library is free
6// software; you can redistribute it and/or modify it under the
7// terms of the GNU General Public License as published by the
8// Free Software Foundation; either version 3, or (at your option)
9// any later version.
10
11// This library is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// Under Section 7 of GPL version 3, you are granted additional
17// permissions described in the GCC Runtime Library Exception, version
18// 3.1, as published by the Free Software Foundation.
19
20// You should have received a copy of the GNU General Public License and
21// a copy of the GCC Runtime Library Exception along with this program;
22// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23// <http://www.gnu.org/licenses/>.
24
25/** @file include/atomic
26 * This is a Standard C++ Library header.
27 */
28
29// Based on "C++ Atomic Types and Operations" by Hans Boehm and Lawrence Crowl.
30// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2427.html
31
32#ifndef _GLIBCXX_ATOMIC1
33#define _GLIBCXX_ATOMIC1 1
34
35#pragma GCC system_header
36
37#if __cplusplus201103L < 201103L
38# include <bits/c++0x_warning.h>
39#else
40
41#include <bits/atomic_base.h>
42#include <bits/move.h>
43
44namespace std _GLIBCXX_VISIBILITY(default)__attribute__ ((__visibility__ ("default")))
45{
46_GLIBCXX_BEGIN_NAMESPACE_VERSION
47
48 /**
49 * @addtogroup atomics
50 * @{
51 */
52
53#if __cplusplus201103L > 201402L
54# define __cpp_lib_atomic_is_always_lock_free 201603
55#endif
56
57 template<typename _Tp>
58 struct atomic;
59
60 /// atomic<bool>
61 // NB: No operators or fetch-operations for this type.
62 template<>
63 struct atomic<bool>
64 {
65 private:
66 __atomic_base<bool> _M_base;
67
68 public:
69 atomic() noexcept = default;
70 ~atomic() noexcept = default;
71 atomic(const atomic&) = delete;
72 atomic& operator=(const atomic&) = delete;
73 atomic& operator=(const atomic&) volatile = delete;
74
75 constexpr atomic(bool __i) noexcept : _M_base(__i) { }
76
77 bool
78 operator=(bool __i) noexcept
79 { return _M_base.operator=(__i); }
80
81 bool
82 operator=(bool __i) volatile noexcept
83 { return _M_base.operator=(__i); }
84
85 operator bool() const noexcept
86 { return _M_base.load(); }
87
88 operator bool() const volatile noexcept
89 { return _M_base.load(); }
90
91 bool
92 is_lock_free() const noexcept { return _M_base.is_lock_free(); }
93
94 bool
95 is_lock_free() const volatile noexcept { return _M_base.is_lock_free(); }
96
97#if __cplusplus201103L > 201402L
98 static constexpr bool is_always_lock_free = ATOMIC_BOOL_LOCK_FREE2 == 2;
99#endif
100
101 void
102 store(bool __i, memory_order __m = memory_order_seq_cst) noexcept
103 { _M_base.store(__i, __m); }
104
105 void
106 store(bool __i, memory_order __m = memory_order_seq_cst) volatile noexcept
107 { _M_base.store(__i, __m); }
108
109 bool
110 load(memory_order __m = memory_order_seq_cst) const noexcept
111 { return _M_base.load(__m); }
112
113 bool
114 load(memory_order __m = memory_order_seq_cst) const volatile noexcept
115 { return _M_base.load(__m); }
116
117 bool
118 exchange(bool __i, memory_order __m = memory_order_seq_cst) noexcept
119 { return _M_base.exchange(__i, __m); }
120
121 bool
122 exchange(bool __i,
123 memory_order __m = memory_order_seq_cst) volatile noexcept
124 { return _M_base.exchange(__i, __m); }
125
126 bool
127 compare_exchange_weak(bool& __i1, bool __i2, memory_order __m1,
128 memory_order __m2) noexcept
129 { return _M_base.compare_exchange_weak(__i1, __i2, __m1, __m2); }
130
131 bool
132 compare_exchange_weak(bool& __i1, bool __i2, memory_order __m1,
133 memory_order __m2) volatile noexcept
134 { return _M_base.compare_exchange_weak(__i1, __i2, __m1, __m2); }
135
136 bool
137 compare_exchange_weak(bool& __i1, bool __i2,
138 memory_order __m = memory_order_seq_cst) noexcept
139 { return _M_base.compare_exchange_weak(__i1, __i2, __m); }
140
141 bool
142 compare_exchange_weak(bool& __i1, bool __i2,
143 memory_order __m = memory_order_seq_cst) volatile noexcept
144 { return _M_base.compare_exchange_weak(__i1, __i2, __m); }
145
146 bool
147 compare_exchange_strong(bool& __i1, bool __i2, memory_order __m1,
148 memory_order __m2) noexcept
149 { return _M_base.compare_exchange_strong(__i1, __i2, __m1, __m2); }
150
151 bool
152 compare_exchange_strong(bool& __i1, bool __i2, memory_order __m1,
153 memory_order __m2) volatile noexcept
154 { return _M_base.compare_exchange_strong(__i1, __i2, __m1, __m2); }
155
156 bool
157 compare_exchange_strong(bool& __i1, bool __i2,
158 memory_order __m = memory_order_seq_cst) noexcept
159 { return _M_base.compare_exchange_strong(__i1, __i2, __m); }
160
161 bool
162 compare_exchange_strong(bool& __i1, bool __i2,
163 memory_order __m = memory_order_seq_cst) volatile noexcept
164 { return _M_base.compare_exchange_strong(__i1, __i2, __m); }
165 };
166
167
168 /**
169 * @brief Generic atomic type, primary class template.
170 *
171 * @tparam _Tp Type to be made atomic, must be trivally copyable.
172 */
173 template<typename _Tp>
174 struct atomic
175 {
176 private:
177 // Align 1/2/4/8/16-byte types to at least their size.
178 static constexpr int _S_min_alignment
179 = (sizeof(_Tp) & (sizeof(_Tp) - 1)) || sizeof(_Tp) > 16
180 ? 0 : sizeof(_Tp);
181
182 static constexpr int _S_alignment
183 = _S_min_alignment > alignof(_Tp) ? _S_min_alignment : alignof(_Tp);
184
185 alignas(_S_alignment) _Tp _M_i;
186
187 static_assert(__is_trivially_copyable(_Tp),
188 "std::atomic requires a trivially copyable type");
189
190 static_assert(sizeof(_Tp) > 0,
191 "Incomplete or zero-sized types are not supported");
192
193 public:
194 atomic() noexcept = default;
195 ~atomic() noexcept = default;
196 atomic(const atomic&) = delete;
197 atomic& operator=(const atomic&) = delete;
198 atomic& operator=(const atomic&) volatile = delete;
199
200 constexpr atomic(_Tp __i) noexcept : _M_i(__i) { }
201
202 operator _Tp() const noexcept
203 { return load(); }
204
205 operator _Tp() const volatile noexcept
206 { return load(); }
207
208 _Tp
209 operator=(_Tp __i) noexcept
210 { store(__i); return __i; }
211
212 _Tp
213 operator=(_Tp __i) volatile noexcept
214 { store(__i); return __i; }
215
216 bool
217 is_lock_free() const noexcept
218 {
219 // Produce a fake, minimally aligned pointer.
220 return __atomic_is_lock_free(sizeof(_M_i),
221 reinterpret_cast<void *>(-__alignof(_M_i)));
222 }
223
224 bool
225 is_lock_free() const volatile noexcept
226 {
227 // Produce a fake, minimally aligned pointer.
228 return __atomic_is_lock_free(sizeof(_M_i),
229 reinterpret_cast<void *>(-__alignof(_M_i)));
230 }
231
232#if __cplusplus201103L > 201402L
233 static constexpr bool is_always_lock_free
234 = __atomic_always_lock_free(sizeof(_M_i), 0);
235#endif
236
237 void
238 store(_Tp __i, memory_order __m = memory_order_seq_cst) noexcept
239 { __atomic_store(std::__addressof(_M_i), std::__addressof(__i), __m); }
240
241 void
242 store(_Tp __i, memory_order __m = memory_order_seq_cst) volatile noexcept
243 { __atomic_store(std::__addressof(_M_i), std::__addressof(__i), __m); }
244
245 _Tp
246 load(memory_order __m = memory_order_seq_cst) const noexcept
247 {
248 alignas(_Tp) unsigned char __buf[sizeof(_Tp)];
249 _Tp* __ptr = reinterpret_cast<_Tp*>(__buf);
250 __atomic_load(std::__addressof(_M_i), __ptr, __m);
251 return *__ptr;
252 }
253
254 _Tp
255 load(memory_order __m = memory_order_seq_cst) const volatile noexcept
256 {
257 alignas(_Tp) unsigned char __buf[sizeof(_Tp)];
258 _Tp* __ptr = reinterpret_cast<_Tp*>(__buf);
259 __atomic_load(std::__addressof(_M_i), __ptr, __m);
260 return *__ptr;
261 }
262
263 _Tp
264 exchange(_Tp __i, memory_order __m = memory_order_seq_cst) noexcept
265 {
266 alignas(_Tp) unsigned char __buf[sizeof(_Tp)];
267 _Tp* __ptr = reinterpret_cast<_Tp*>(__buf);
268 __atomic_exchange(std::__addressof(_M_i), std::__addressof(__i),
269 __ptr, __m);
270 return *__ptr;
271 }
272
273 _Tp
274 exchange(_Tp __i,
275 memory_order __m = memory_order_seq_cst) volatile noexcept
276 {
277 alignas(_Tp) unsigned char __buf[sizeof(_Tp)];
278 _Tp* __ptr = reinterpret_cast<_Tp*>(__buf);
279 __atomic_exchange(std::__addressof(_M_i), std::__addressof(__i),
280 __ptr, __m);
281 return *__ptr;
282 }
283
284 bool
285 compare_exchange_weak(_Tp& __e, _Tp __i, memory_order __s,
286 memory_order __f) noexcept
287 {
288 return __atomic_compare_exchange(std::__addressof(_M_i),
289 std::__addressof(__e),
290 std::__addressof(__i),
291 true, __s, __f);
292 }
293
294 bool
295 compare_exchange_weak(_Tp& __e, _Tp __i, memory_order __s,
296 memory_order __f) volatile noexcept
297 {
298 return __atomic_compare_exchange(std::__addressof(_M_i),
299 std::__addressof(__e),
300 std::__addressof(__i),
301 true, __s, __f);
302 }
303
304 bool
305 compare_exchange_weak(_Tp& __e, _Tp __i,
306 memory_order __m = memory_order_seq_cst) noexcept
307 { return compare_exchange_weak(__e, __i, __m,
308 __cmpexch_failure_order(__m)); }
309
310 bool
311 compare_exchange_weak(_Tp& __e, _Tp __i,
312 memory_order __m = memory_order_seq_cst) volatile noexcept
313 { return compare_exchange_weak(__e, __i, __m,
314 __cmpexch_failure_order(__m)); }
315
316 bool
317 compare_exchange_strong(_Tp& __e, _Tp __i, memory_order __s,
318 memory_order __f) noexcept
319 {
320 return __atomic_compare_exchange(std::__addressof(_M_i),
321 std::__addressof(__e),
322 std::__addressof(__i),
323 false, __s, __f);
324 }
325
326 bool
327 compare_exchange_strong(_Tp& __e, _Tp __i, memory_order __s,
328 memory_order __f) volatile noexcept
329 {
330 return __atomic_compare_exchange(std::__addressof(_M_i),
331 std::__addressof(__e),
332 std::__addressof(__i),
333 false, __s, __f);
334 }
335
336 bool
337 compare_exchange_strong(_Tp& __e, _Tp __i,
338 memory_order __m = memory_order_seq_cst) noexcept
339 { return compare_exchange_strong(__e, __i, __m,
340 __cmpexch_failure_order(__m)); }
341
342 bool
343 compare_exchange_strong(_Tp& __e, _Tp __i,
344 memory_order __m = memory_order_seq_cst) volatile noexcept
345 { return compare_exchange_strong(__e, __i, __m,
346 __cmpexch_failure_order(__m)); }
347 };
348
349
350 /// Partial specialization for pointer types.
351 template<typename _Tp>
352 struct atomic<_Tp*>
353 {
354 typedef _Tp* __pointer_type;
355 typedef __atomic_base<_Tp*> __base_type;
356 __base_type _M_b;
357
358 atomic() noexcept = default;
359 ~atomic() noexcept = default;
360 atomic(const atomic&) = delete;
361 atomic& operator=(const atomic&) = delete;
362 atomic& operator=(const atomic&) volatile = delete;
363
364 constexpr atomic(__pointer_type __p) noexcept : _M_b(__p) { }
365
366 operator __pointer_type() const noexcept
367 { return __pointer_type(_M_b); }
368
369 operator __pointer_type() const volatile noexcept
370 { return __pointer_type(_M_b); }
371
372 __pointer_type
373 operator=(__pointer_type __p) noexcept
374 { return _M_b.operator=(__p); }
375
376 __pointer_type
377 operator=(__pointer_type __p) volatile noexcept
378 { return _M_b.operator=(__p); }
379
380 __pointer_type
381 operator++(int) noexcept
382 { return _M_b++; }
383
384 __pointer_type
385 operator++(int) volatile noexcept
386 { return _M_b++; }
387
388 __pointer_type
389 operator--(int) noexcept
390 { return _M_b--; }
391
392 __pointer_type
393 operator--(int) volatile noexcept
394 { return _M_b--; }
395
396 __pointer_type
397 operator++() noexcept
398 { return ++_M_b; }
399
400 __pointer_type
401 operator++() volatile noexcept
402 { return ++_M_b; }
403
404 __pointer_type
405 operator--() noexcept
406 { return --_M_b; }
407
408 __pointer_type
409 operator--() volatile noexcept
410 { return --_M_b; }
411
412 __pointer_type
413 operator+=(ptrdiff_t __d) noexcept
414 { return _M_b.operator+=(__d); }
415
416 __pointer_type
417 operator+=(ptrdiff_t __d) volatile noexcept
418 { return _M_b.operator+=(__d); }
419
420 __pointer_type
421 operator-=(ptrdiff_t __d) noexcept
422 { return _M_b.operator-=(__d); }
423
424 __pointer_type
425 operator-=(ptrdiff_t __d) volatile noexcept
426 { return _M_b.operator-=(__d); }
427
428 bool
429 is_lock_free() const noexcept
430 { return _M_b.is_lock_free(); }
431
432 bool
433 is_lock_free() const volatile noexcept
434 { return _M_b.is_lock_free(); }
435
436#if __cplusplus201103L > 201402L
437 static constexpr bool is_always_lock_free = ATOMIC_POINTER_LOCK_FREE2 == 2;
438#endif
439
440 void
441 store(__pointer_type __p,
442 memory_order __m = memory_order_seq_cst) noexcept
443 { return _M_b.store(__p, __m); }
444
445 void
446 store(__pointer_type __p,
447 memory_order __m = memory_order_seq_cst) volatile noexcept
448 { return _M_b.store(__p, __m); }
449
450 __pointer_type
451 load(memory_order __m = memory_order_seq_cst) const noexcept
452 { return _M_b.load(__m); }
37
Calling '__atomic_base::load'
40
Returning from '__atomic_base::load'
45
Calling '__atomic_base::load'
48
Returning from '__atomic_base::load'
453
454 __pointer_type
455 load(memory_order __m = memory_order_seq_cst) const volatile noexcept
456 { return _M_b.load(__m); }
457
458 __pointer_type
459 exchange(__pointer_type __p,
460 memory_order __m = memory_order_seq_cst) noexcept
461 { return _M_b.exchange(__p, __m); }
462
463 __pointer_type
464 exchange(__pointer_type __p,
465 memory_order __m = memory_order_seq_cst) volatile noexcept
466 { return _M_b.exchange(__p, __m); }
467
468 bool
469 compare_exchange_weak(__pointer_type& __p1, __pointer_type __p2,
470 memory_order __m1, memory_order __m2) noexcept
471 { return _M_b.compare_exchange_strong(__p1, __p2, __m1, __m2); }
472
473 bool
474 compare_exchange_weak(__pointer_type& __p1, __pointer_type __p2,
475 memory_order __m1,
476 memory_order __m2) volatile noexcept
477 { return _M_b.compare_exchange_strong(__p1, __p2, __m1, __m2); }
478
479 bool
480 compare_exchange_weak(__pointer_type& __p1, __pointer_type __p2,
481 memory_order __m = memory_order_seq_cst) noexcept
482 {
483 return compare_exchange_weak(__p1, __p2, __m,
484 __cmpexch_failure_order(__m));
485 }
486
487 bool
488 compare_exchange_weak(__pointer_type& __p1, __pointer_type __p2,
489 memory_order __m = memory_order_seq_cst) volatile noexcept
490 {
491 return compare_exchange_weak(__p1, __p2, __m,
492 __cmpexch_failure_order(__m));
493 }
494
495 bool
496 compare_exchange_strong(__pointer_type& __p1, __pointer_type __p2,
497 memory_order __m1, memory_order __m2) noexcept
498 { return _M_b.compare_exchange_strong(__p1, __p2, __m1, __m2); }
499
500 bool
501 compare_exchange_strong(__pointer_type& __p1, __pointer_type __p2,
502 memory_order __m1,
503 memory_order __m2) volatile noexcept
504 { return _M_b.compare_exchange_strong(__p1, __p2, __m1, __m2); }
505
506 bool
507 compare_exchange_strong(__pointer_type& __p1, __pointer_type __p2,
508 memory_order __m = memory_order_seq_cst) noexcept
509 {
510 return _M_b.compare_exchange_strong(__p1, __p2, __m,
511 __cmpexch_failure_order(__m));
512 }
513
514 bool
515 compare_exchange_strong(__pointer_type& __p1, __pointer_type __p2,
516 memory_order __m = memory_order_seq_cst) volatile noexcept
517 {
518 return _M_b.compare_exchange_strong(__p1, __p2, __m,
519 __cmpexch_failure_order(__m));
520 }
521
522 __pointer_type
523 fetch_add(ptrdiff_t __d,
524 memory_order __m = memory_order_seq_cst) noexcept
525 { return _M_b.fetch_add(__d, __m); }
526
527 __pointer_type
528 fetch_add(ptrdiff_t __d,
529 memory_order __m = memory_order_seq_cst) volatile noexcept
530 { return _M_b.fetch_add(__d, __m); }
531
532 __pointer_type
533 fetch_sub(ptrdiff_t __d,
534 memory_order __m = memory_order_seq_cst) noexcept
535 { return _M_b.fetch_sub(__d, __m); }
536
537 __pointer_type
538 fetch_sub(ptrdiff_t __d,
539 memory_order __m = memory_order_seq_cst) volatile noexcept
540 { return _M_b.fetch_sub(__d, __m); }
541 };
542
543
544 /// Explicit specialization for char.
545 template<>
546 struct atomic<char> : __atomic_base<char>
547 {
548 typedef char __integral_type;
549 typedef __atomic_base<char> __base_type;
550
551 atomic() noexcept = default;
552 ~atomic() noexcept = default;
553 atomic(const atomic&) = delete;
554 atomic& operator=(const atomic&) = delete;
555 atomic& operator=(const atomic&) volatile = delete;
556
557 constexpr atomic(__integral_type __i) noexcept : __base_type(__i) { }
558
559 using __base_type::operator __integral_type;
560 using __base_type::operator=;
561
562#if __cplusplus201103L > 201402L
563 static constexpr bool is_always_lock_free = ATOMIC_CHAR_LOCK_FREE2 == 2;
564#endif
565 };
566
567 /// Explicit specialization for signed char.
568 template<>
569 struct atomic<signed char> : __atomic_base<signed char>
570 {
571 typedef signed char __integral_type;
572 typedef __atomic_base<signed char> __base_type;
573
574 atomic() noexcept= default;
575 ~atomic() noexcept = default;
576 atomic(const atomic&) = delete;
577 atomic& operator=(const atomic&) = delete;
578 atomic& operator=(const atomic&) volatile = delete;
579
580 constexpr atomic(__integral_type __i) noexcept : __base_type(__i) { }
581
582 using __base_type::operator __integral_type;
583 using __base_type::operator=;
584
585#if __cplusplus201103L > 201402L
586 static constexpr bool is_always_lock_free = ATOMIC_CHAR_LOCK_FREE2 == 2;
587#endif
588 };
589
590 /// Explicit specialization for unsigned char.
591 template<>
592 struct atomic<unsigned char> : __atomic_base<unsigned char>
593 {
594 typedef unsigned char __integral_type;
595 typedef __atomic_base<unsigned char> __base_type;
596
597 atomic() noexcept= default;
598 ~atomic() noexcept = default;
599 atomic(const atomic&) = delete;
600 atomic& operator=(const atomic&) = delete;
601 atomic& operator=(const atomic&) volatile = delete;
602
603 constexpr atomic(__integral_type __i) noexcept : __base_type(__i) { }
604
605 using __base_type::operator __integral_type;
606 using __base_type::operator=;
607
608#if __cplusplus201103L > 201402L
609 static constexpr bool is_always_lock_free = ATOMIC_CHAR_LOCK_FREE2 == 2;
610#endif
611 };
612
613 /// Explicit specialization for short.
614 template<>
615 struct atomic<short> : __atomic_base<short>
616 {
617 typedef short __integral_type;
618 typedef __atomic_base<short> __base_type;
619
620 atomic() noexcept = default;
621 ~atomic() noexcept = default;
622 atomic(const atomic&) = delete;
623 atomic& operator=(const atomic&) = delete;
624 atomic& operator=(const atomic&) volatile = delete;
625
626 constexpr atomic(__integral_type __i) noexcept : __base_type(__i) { }
627
628 using __base_type::operator __integral_type;
629 using __base_type::operator=;
630
631#if __cplusplus201103L > 201402L
632 static constexpr bool is_always_lock_free = ATOMIC_SHORT_LOCK_FREE2 == 2;
633#endif
634 };
635
636 /// Explicit specialization for unsigned short.
637 template<>
638 struct atomic<unsigned short> : __atomic_base<unsigned short>
639 {
640 typedef unsigned short __integral_type;
641 typedef __atomic_base<unsigned short> __base_type;
642
643 atomic() noexcept = default;
644 ~atomic() noexcept = default;
645 atomic(const atomic&) = delete;
646 atomic& operator=(const atomic&) = delete;
647 atomic& operator=(const atomic&) volatile = delete;
648
649 constexpr atomic(__integral_type __i) noexcept : __base_type(__i) { }
650
651 using __base_type::operator __integral_type;
652 using __base_type::operator=;
653
654#if __cplusplus201103L > 201402L
655 static constexpr bool is_always_lock_free = ATOMIC_SHORT_LOCK_FREE2 == 2;
656#endif
657 };
658
659 /// Explicit specialization for int.
660 template<>
661 struct atomic<int> : __atomic_base<int>
662 {
663 typedef int __integral_type;
664 typedef __atomic_base<int> __base_type;
665
666 atomic() noexcept = default;
667 ~atomic() noexcept = default;
668 atomic(const atomic&) = delete;
669 atomic& operator=(const atomic&) = delete;
670 atomic& operator=(const atomic&) volatile = delete;
671
672 constexpr atomic(__integral_type __i) noexcept : __base_type(__i) { }
673
674 using __base_type::operator __integral_type;
675 using __base_type::operator=;
676
677#if __cplusplus201103L > 201402L
678 static constexpr bool is_always_lock_free = ATOMIC_INT_LOCK_FREE2 == 2;
679#endif
680 };
681
682 /// Explicit specialization for unsigned int.
683 template<>
684 struct atomic<unsigned int> : __atomic_base<unsigned int>
685 {
686 typedef unsigned int __integral_type;
687 typedef __atomic_base<unsigned int> __base_type;
688
689 atomic() noexcept = default;
690 ~atomic() noexcept = default;
691 atomic(const atomic&) = delete;
692 atomic& operator=(const atomic&) = delete;
693 atomic& operator=(const atomic&) volatile = delete;
694
695 constexpr atomic(__integral_type __i) noexcept : __base_type(__i) { }
696
697 using __base_type::operator __integral_type;
698 using __base_type::operator=;
699
700#if __cplusplus201103L > 201402L
701 static constexpr bool is_always_lock_free = ATOMIC_INT_LOCK_FREE2 == 2;
702#endif
703 };
704
705 /// Explicit specialization for long.
706 template<>
707 struct atomic<long> : __atomic_base<long>
708 {
709 typedef long __integral_type;
710 typedef __atomic_base<long> __base_type;
711
712 atomic() noexcept = default;
713 ~atomic() noexcept = default;
714 atomic(const atomic&) = delete;
715 atomic& operator=(const atomic&) = delete;
716 atomic& operator=(const atomic&) volatile = delete;
717
718 constexpr atomic(__integral_type __i) noexcept : __base_type(__i) { }
719
720 using __base_type::operator __integral_type;
721 using __base_type::operator=;
722
723#if __cplusplus201103L > 201402L
724 static constexpr bool is_always_lock_free = ATOMIC_LONG_LOCK_FREE2 == 2;
725#endif
726 };
727
728 /// Explicit specialization for unsigned long.
729 template<>
730 struct atomic<unsigned long> : __atomic_base<unsigned long>
731 {
732 typedef unsigned long __integral_type;
733 typedef __atomic_base<unsigned long> __base_type;
734
735 atomic() noexcept = default;
736 ~atomic() noexcept = default;
737 atomic(const atomic&) = delete;
738 atomic& operator=(const atomic&) = delete;
739 atomic& operator=(const atomic&) volatile = delete;
740
741 constexpr atomic(__integral_type __i) noexcept : __base_type(__i) { }
742
743 using __base_type::operator __integral_type;
744 using __base_type::operator=;
745
746#if __cplusplus201103L > 201402L
747 static constexpr bool is_always_lock_free = ATOMIC_LONG_LOCK_FREE2 == 2;
748#endif
749 };
750
751 /// Explicit specialization for long long.
752 template<>
753 struct atomic<long long> : __atomic_base<long long>
754 {
755 typedef long long __integral_type;
756 typedef __atomic_base<long long> __base_type;
757
758 atomic() noexcept = default;
759 ~atomic() noexcept = default;
760 atomic(const atomic&) = delete;
761 atomic& operator=(const atomic&) = delete;
762 atomic& operator=(const atomic&) volatile = delete;
763
764 constexpr atomic(__integral_type __i) noexcept : __base_type(__i) { }
765
766 using __base_type::operator __integral_type;
767 using __base_type::operator=;
768
769#if __cplusplus201103L > 201402L
770 static constexpr bool is_always_lock_free = ATOMIC_LLONG_LOCK_FREE2 == 2;
771#endif
772 };
773
774 /// Explicit specialization for unsigned long long.
775 template<>
776 struct atomic<unsigned long long> : __atomic_base<unsigned long long>
777 {
778 typedef unsigned long long __integral_type;
779 typedef __atomic_base<unsigned long long> __base_type;
780
781 atomic() noexcept = default;
782 ~atomic() noexcept = default;
783 atomic(const atomic&) = delete;
784 atomic& operator=(const atomic&) = delete;
785 atomic& operator=(const atomic&) volatile = delete;
786
787 constexpr atomic(__integral_type __i) noexcept : __base_type(__i) { }
788
789 using __base_type::operator __integral_type;
790 using __base_type::operator=;
791
792#if __cplusplus201103L > 201402L
793 static constexpr bool is_always_lock_free = ATOMIC_LLONG_LOCK_FREE2 == 2;
794#endif
795 };
796
797 /// Explicit specialization for wchar_t.
798 template<>
799 struct atomic<wchar_t> : __atomic_base<wchar_t>
800 {
801 typedef wchar_t __integral_type;
802 typedef __atomic_base<wchar_t> __base_type;
803
804 atomic() noexcept = default;
805 ~atomic() noexcept = default;
806 atomic(const atomic&) = delete;
807 atomic& operator=(const atomic&) = delete;
808 atomic& operator=(const atomic&) volatile = delete;
809
810 constexpr atomic(__integral_type __i) noexcept : __base_type(__i) { }
811
812 using __base_type::operator __integral_type;
813 using __base_type::operator=;
814
815#if __cplusplus201103L > 201402L
816 static constexpr bool is_always_lock_free = ATOMIC_WCHAR_T_LOCK_FREE2 == 2;
817#endif
818 };
819
820 /// Explicit specialization for char16_t.
821 template<>
822 struct atomic<char16_t> : __atomic_base<char16_t>
823 {
824 typedef char16_t __integral_type;
825 typedef __atomic_base<char16_t> __base_type;
826
827 atomic() noexcept = default;
828 ~atomic() noexcept = default;
829 atomic(const atomic&) = delete;
830 atomic& operator=(const atomic&) = delete;
831 atomic& operator=(const atomic&) volatile = delete;
832
833 constexpr atomic(__integral_type __i) noexcept : __base_type(__i) { }
834
835 using __base_type::operator __integral_type;
836 using __base_type::operator=;
837
838#if __cplusplus201103L > 201402L
839 static constexpr bool is_always_lock_free = ATOMIC_CHAR16_T_LOCK_FREE2 == 2;
840#endif
841 };
842
843 /// Explicit specialization for char32_t.
844 template<>
845 struct atomic<char32_t> : __atomic_base<char32_t>
846 {
847 typedef char32_t __integral_type;
848 typedef __atomic_base<char32_t> __base_type;
849
850 atomic() noexcept = default;
851 ~atomic() noexcept = default;
852 atomic(const atomic&) = delete;
853 atomic& operator=(const atomic&) = delete;
854 atomic& operator=(const atomic&) volatile = delete;
855
856 constexpr atomic(__integral_type __i) noexcept : __base_type(__i) { }
857
858 using __base_type::operator __integral_type;
859 using __base_type::operator=;
860
861#if __cplusplus201103L > 201402L
862 static constexpr bool is_always_lock_free = ATOMIC_CHAR32_T_LOCK_FREE2 == 2;
863#endif
864 };
865
866
867 /// atomic_bool
868 typedef atomic<bool> atomic_bool;
869
870 /// atomic_char
871 typedef atomic<char> atomic_char;
872
873 /// atomic_schar
874 typedef atomic<signed char> atomic_schar;
875
876 /// atomic_uchar
877 typedef atomic<unsigned char> atomic_uchar;
878
879 /// atomic_short
880 typedef atomic<short> atomic_short;
881
882 /// atomic_ushort
883 typedef atomic<unsigned short> atomic_ushort;
884
885 /// atomic_int
886 typedef atomic<int> atomic_int;
887
888 /// atomic_uint
889 typedef atomic<unsigned int> atomic_uint;
890
891 /// atomic_long
892 typedef atomic<long> atomic_long;
893
894 /// atomic_ulong
895 typedef atomic<unsigned long> atomic_ulong;
896
897 /// atomic_llong
898 typedef atomic<long long> atomic_llong;
899
900 /// atomic_ullong
901 typedef atomic<unsigned long long> atomic_ullong;
902
903 /// atomic_wchar_t
904 typedef atomic<wchar_t> atomic_wchar_t;
905
906 /// atomic_char16_t
907 typedef atomic<char16_t> atomic_char16_t;
908
909 /// atomic_char32_t
910 typedef atomic<char32_t> atomic_char32_t;
911
912
913 // _GLIBCXX_RESOLVE_LIB_DEFECTS
914 // 2441. Exact-width atomic typedefs should be provided
915
916 /// atomic_int8_t
917 typedef atomic<int8_t> atomic_int8_t;
918
919 /// atomic_uint8_t
920 typedef atomic<uint8_t> atomic_uint8_t;
921
922 /// atomic_int16_t
923 typedef atomic<int16_t> atomic_int16_t;
924
925 /// atomic_uint16_t
926 typedef atomic<uint16_t> atomic_uint16_t;
927
928 /// atomic_int32_t
929 typedef atomic<int32_t> atomic_int32_t;
930
931 /// atomic_uint32_t
932 typedef atomic<uint32_t> atomic_uint32_t;
933
934 /// atomic_int64_t
935 typedef atomic<int64_t> atomic_int64_t;
936
937 /// atomic_uint64_t
938 typedef atomic<uint64_t> atomic_uint64_t;
939
940
941 /// atomic_int_least8_t
942 typedef atomic<int_least8_t> atomic_int_least8_t;
943
944 /// atomic_uint_least8_t
945 typedef atomic<uint_least8_t> atomic_uint_least8_t;
946
947 /// atomic_int_least16_t
948 typedef atomic<int_least16_t> atomic_int_least16_t;
949
950 /// atomic_uint_least16_t
951 typedef atomic<uint_least16_t> atomic_uint_least16_t;
952
953 /// atomic_int_least32_t
954 typedef atomic<int_least32_t> atomic_int_least32_t;
955
956 /// atomic_uint_least32_t
957 typedef atomic<uint_least32_t> atomic_uint_least32_t;
958
959 /// atomic_int_least64_t
960 typedef atomic<int_least64_t> atomic_int_least64_t;
961
962 /// atomic_uint_least64_t
963 typedef atomic<uint_least64_t> atomic_uint_least64_t;
964
965
966 /// atomic_int_fast8_t
967 typedef atomic<int_fast8_t> atomic_int_fast8_t;
968
969 /// atomic_uint_fast8_t
970 typedef atomic<uint_fast8_t> atomic_uint_fast8_t;
971
972 /// atomic_int_fast16_t
973 typedef atomic<int_fast16_t> atomic_int_fast16_t;
974
975 /// atomic_uint_fast16_t
976 typedef atomic<uint_fast16_t> atomic_uint_fast16_t;
977
978 /// atomic_int_fast32_t
979 typedef atomic<int_fast32_t> atomic_int_fast32_t;
980
981 /// atomic_uint_fast32_t
982 typedef atomic<uint_fast32_t> atomic_uint_fast32_t;
983
984 /// atomic_int_fast64_t
985 typedef atomic<int_fast64_t> atomic_int_fast64_t;
986
987 /// atomic_uint_fast64_t
988 typedef atomic<uint_fast64_t> atomic_uint_fast64_t;
989
990
991 /// atomic_intptr_t
992 typedef atomic<intptr_t> atomic_intptr_t;
993
994 /// atomic_uintptr_t
995 typedef atomic<uintptr_t> atomic_uintptr_t;
996
997 /// atomic_size_t
998 typedef atomic<size_t> atomic_size_t;
999
1000 /// atomic_intmax_t
1001 typedef atomic<intmax_t> atomic_intmax_t;
1002
1003 /// atomic_uintmax_t
1004 typedef atomic<uintmax_t> atomic_uintmax_t;
1005
1006 /// atomic_ptrdiff_t
1007 typedef atomic<ptrdiff_t> atomic_ptrdiff_t;
1008
1009
1010 // Function definitions, atomic_flag operations.
1011 inline bool
1012 atomic_flag_test_and_set_explicit(atomic_flag* __a,
1013 memory_order __m) noexcept
1014 { return __a->test_and_set(__m); }
1015
1016 inline bool
1017 atomic_flag_test_and_set_explicit(volatile atomic_flag* __a,
1018 memory_order __m) noexcept
1019 { return __a->test_and_set(__m); }
1020
1021 inline void
1022 atomic_flag_clear_explicit(atomic_flag* __a, memory_order __m) noexcept
1023 { __a->clear(__m); }
1024
1025 inline void
1026 atomic_flag_clear_explicit(volatile atomic_flag* __a,
1027 memory_order __m) noexcept
1028 { __a->clear(__m); }
1029
1030 inline bool
1031 atomic_flag_test_and_set(atomic_flag* __a) noexcept
1032 { return atomic_flag_test_and_set_explicit(__a, memory_order_seq_cst); }
1033
1034