File: | lib/Bitcode/Reader/BitcodeReader.cpp |
Warning: | line 5416, column 7 Use of memory after it is freed |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
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 | ||||
88 | using namespace llvm; | |||
89 | ||||
90 | static cl::opt<bool> PrintSummaryGUIDs( | |||
91 | "print-summary-global-ids", cl::init(false), cl::Hidden, | |||
92 | cl::desc( | |||
93 | "Print the global id for each value when reading the module summary")); | |||
94 | ||||
95 | namespace { | |||
96 | ||||
97 | enum { | |||
98 | SWITCH_INST_MAGIC = 0x4B5 // May 2012 => 1205 => Hex | |||
99 | }; | |||
100 | ||||
101 | } // end anonymous namespace | |||
102 | ||||
103 | static Error error(const Twine &Message) { | |||
104 | return make_error<StringError>( | |||
105 | Message, make_error_code(BitcodeError::CorruptedBitcode)); | |||
106 | } | |||
107 | ||||
108 | /// Helper to read the header common to all bitcode files. | |||
109 | static 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 | ||||
122 | static 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. | |||
143 | template <typename StrTy> | |||
144 | static 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. | |||
155 | static 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. | |||
166 | static 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 | ||||
210 | static 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 | ||||
238 | static 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-7~svn329677/lib/Bitcode/Reader/BitcodeReader.cpp" , 276); | |||
277 | } | |||
278 | ||||
279 | static 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 | ||||
307 | static 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-7~svn329677/lib/Bitcode/Reader/BitcodeReader.cpp" , 343); | |||
344 | } | |||
345 | ||||
346 | static 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 | ||||
374 | namespace { | |||
375 | ||||
376 | class BitcodeReaderBase { | |||
377 | protected: | |||
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 | ||||
409 | Error BitcodeReaderBase::error(const Twine &Message) { | |||
410 | std::string FullMsg = Message.str(); | |||
411 | if (!ProducerIdentification.empty()) | |||
412 | FullMsg += " (Producer: '" + ProducerIdentification + "' Reader: 'LLVM " + | |||
413 | LLVM_VERSION_STRING"7.0.0" "')"; | |||
414 | return ::error(FullMsg); | |||
415 | } | |||
416 | ||||
417 | Expected<unsigned> | |||
418 | BitcodeReaderBase::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 | ||||
428 | std::pair<StringRef, ArrayRef<uint64_t>> | |||
429 | BitcodeReaderBase::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 | ||||
438 | namespace { | |||
439 | ||||
440 | class 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 | ||||
525 | public: | |||
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 | ||||
547 | private: | |||
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. | |||
686 | class 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 | ||||
729 | public: | |||
730 | ModuleSummaryIndexBitcodeReader(BitstreamCursor Stream, StringRef Strtab, | |||
731 | ModuleSummaryIndex &TheIndex, | |||
732 | StringRef ModulePath, unsigned ModuleId); | |||
733 | ||||
734 | Error parseModule(); | |||
735 | ||||
736 | private: | |||
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 | bool HasRelBF); | |||
748 | Error parseEntireSummary(unsigned ID); | |||
749 | Error parseModuleStringTable(); | |||
750 | ||||
751 | std::pair<ValueInfo, GlobalValue::GUID> | |||
752 | getValueInfoFromValueId(unsigned ValueId); | |||
753 | ||||
754 | ModuleSummaryIndex::ModuleInfo *addThisModule(); | |||
755 | }; | |||
756 | ||||
757 | } // end anonymous namespace | |||
758 | ||||
759 | std::error_code llvm::errorToErrorCodeAndEmitErrors(LLVMContext &Ctx, | |||
760 | Error Err) { | |||
761 | if (Err) { | |||
762 | std::error_code EC; | |||
763 | handleAllErrors(std::move(Err), [&](ErrorInfoBase &EIB) { | |||
764 | EC = EIB.convertToErrorCode(); | |||
765 | Ctx.emitError(EIB.message()); | |||
766 | }); | |||
767 | return EC; | |||
768 | } | |||
769 | return std::error_code(); | |||
770 | } | |||
771 | ||||
772 | BitcodeReader::BitcodeReader(BitstreamCursor Stream, StringRef Strtab, | |||
773 | StringRef ProducerIdentification, | |||
774 | LLVMContext &Context) | |||
775 | : BitcodeReaderBase(std::move(Stream), Strtab), Context(Context), | |||
776 | ValueList(Context) { | |||
777 | this->ProducerIdentification = ProducerIdentification; | |||
778 | } | |||
779 | ||||
780 | Error BitcodeReader::materializeForwardReferencedFunctions() { | |||
781 | if (WillMaterializeAllForwardRefs) | |||
782 | return Error::success(); | |||
783 | ||||
784 | // Prevent recursion. | |||
785 | WillMaterializeAllForwardRefs = true; | |||
786 | ||||
787 | while (!BasicBlockFwdRefQueue.empty()) { | |||
788 | Function *F = BasicBlockFwdRefQueue.front(); | |||
789 | BasicBlockFwdRefQueue.pop_front(); | |||
790 | assert(F && "Expected valid function")(static_cast <bool> (F && "Expected valid function" ) ? void (0) : __assert_fail ("F && \"Expected valid function\"" , "/build/llvm-toolchain-snapshot-7~svn329677/lib/Bitcode/Reader/BitcodeReader.cpp" , 790, __extension__ __PRETTY_FUNCTION__)); | |||
791 | if (!BasicBlockFwdRefs.count(F)) | |||
792 | // Already materialized. | |||
793 | continue; | |||
794 | ||||
795 | // Check for a function that isn't materializable to prevent an infinite | |||
796 | // loop. When parsing a blockaddress stored in a global variable, there | |||
797 | // isn't a trivial way to check if a function will have a body without a | |||
798 | // linear search through FunctionsWithBodies, so just check it here. | |||
799 | if (!F->isMaterializable()) | |||
800 | return error("Never resolved function from blockaddress"); | |||
801 | ||||
802 | // Try to materialize F. | |||
803 | if (Error Err = materialize(F)) | |||
804 | return Err; | |||
805 | } | |||
806 | assert(BasicBlockFwdRefs.empty() && "Function missing from queue")(static_cast <bool> (BasicBlockFwdRefs.empty() && "Function missing from queue") ? void (0) : __assert_fail ("BasicBlockFwdRefs.empty() && \"Function missing from queue\"" , "/build/llvm-toolchain-snapshot-7~svn329677/lib/Bitcode/Reader/BitcodeReader.cpp" , 806, __extension__ __PRETTY_FUNCTION__)); | |||
807 | ||||
808 | // Reset state. | |||
809 | WillMaterializeAllForwardRefs = false; | |||
810 | return Error::success(); | |||
811 | } | |||
812 | ||||
813 | //===----------------------------------------------------------------------===// | |||
814 | // Helper functions to implement forward reference resolution, etc. | |||
815 | //===----------------------------------------------------------------------===// | |||
816 | ||||
817 | static bool hasImplicitComdat(size_t Val) { | |||
818 | switch (Val) { | |||
819 | default: | |||
820 | return false; | |||
821 | case 1: // Old WeakAnyLinkage | |||
822 | case 4: // Old LinkOnceAnyLinkage | |||
823 | case 10: // Old WeakODRLinkage | |||
824 | case 11: // Old LinkOnceODRLinkage | |||
825 | return true; | |||
826 | } | |||
827 | } | |||
828 | ||||
829 | static GlobalValue::LinkageTypes getDecodedLinkage(unsigned Val) { | |||
830 | switch (Val) { | |||
831 | default: // Map unknown/new linkages to external | |||
832 | case 0: | |||
833 | return GlobalValue::ExternalLinkage; | |||
834 | case 2: | |||
835 | return GlobalValue::AppendingLinkage; | |||
836 | case 3: | |||
837 | return GlobalValue::InternalLinkage; | |||
838 | case 5: | |||
839 | return GlobalValue::ExternalLinkage; // Obsolete DLLImportLinkage | |||
840 | case 6: | |||
841 | return GlobalValue::ExternalLinkage; // Obsolete DLLExportLinkage | |||
842 | case 7: | |||
843 | return GlobalValue::ExternalWeakLinkage; | |||
844 | case 8: | |||
845 | return GlobalValue::CommonLinkage; | |||
846 | case 9: | |||
847 | return GlobalValue::PrivateLinkage; | |||
848 | case 12: | |||
849 | return GlobalValue::AvailableExternallyLinkage; | |||
850 | case 13: | |||
851 | return GlobalValue::PrivateLinkage; // Obsolete LinkerPrivateLinkage | |||
852 | case 14: | |||
853 | return GlobalValue::PrivateLinkage; // Obsolete LinkerPrivateWeakLinkage | |||
854 | case 15: | |||
855 | return GlobalValue::ExternalLinkage; // Obsolete LinkOnceODRAutoHideLinkage | |||
856 | case 1: // Old value with implicit comdat. | |||
857 | case 16: | |||
858 | return GlobalValue::WeakAnyLinkage; | |||
859 | case 10: // Old value with implicit comdat. | |||
860 | case 17: | |||
861 | return GlobalValue::WeakODRLinkage; | |||
862 | case 4: // Old value with implicit comdat. | |||
863 | case 18: | |||
864 | return GlobalValue::LinkOnceAnyLinkage; | |||
865 | case 11: // Old value with implicit comdat. | |||
866 | case 19: | |||
867 | return GlobalValue::LinkOnceODRLinkage; | |||
868 | } | |||
869 | } | |||
870 | ||||
871 | static FunctionSummary::FFlags getDecodedFFlags(uint64_t RawFlags) { | |||
872 | FunctionSummary::FFlags Flags; | |||
873 | Flags.ReadNone = RawFlags & 0x1; | |||
874 | Flags.ReadOnly = (RawFlags >> 1) & 0x1; | |||
875 | Flags.NoRecurse = (RawFlags >> 2) & 0x1; | |||
876 | Flags.ReturnDoesNotAlias = (RawFlags >> 3) & 0x1; | |||
877 | return Flags; | |||
878 | } | |||
879 | ||||
880 | /// Decode the flags for GlobalValue in the summary. | |||
881 | static GlobalValueSummary::GVFlags getDecodedGVSummaryFlags(uint64_t RawFlags, | |||
882 | uint64_t Version) { | |||
883 | // Summary were not emitted before LLVM 3.9, we don't need to upgrade Linkage | |||
884 | // like getDecodedLinkage() above. Any future change to the linkage enum and | |||
885 | // to getDecodedLinkage() will need to be taken into account here as above. | |||
886 | auto Linkage = GlobalValue::LinkageTypes(RawFlags & 0xF); // 4 bits | |||
887 | RawFlags = RawFlags >> 4; | |||
888 | bool NotEligibleToImport = (RawFlags & 0x1) || Version < 3; | |||
889 | // The Live flag wasn't introduced until version 3. For dead stripping | |||
890 | // to work correctly on earlier versions, we must conservatively treat all | |||
891 | // values as live. | |||
892 | bool Live = (RawFlags & 0x2) || Version < 3; | |||
893 | bool Local = (RawFlags & 0x4); | |||
894 | ||||
895 | return GlobalValueSummary::GVFlags(Linkage, NotEligibleToImport, Live, Local); | |||
896 | } | |||
897 | ||||
898 | static GlobalValue::VisibilityTypes getDecodedVisibility(unsigned Val) { | |||
899 | switch (Val) { | |||
900 | default: // Map unknown visibilities to default. | |||
901 | case 0: return GlobalValue::DefaultVisibility; | |||
902 | case 1: return GlobalValue::HiddenVisibility; | |||
903 | case 2: return GlobalValue::ProtectedVisibility; | |||
904 | } | |||
905 | } | |||
906 | ||||
907 | static GlobalValue::DLLStorageClassTypes | |||
908 | getDecodedDLLStorageClass(unsigned Val) { | |||
909 | switch (Val) { | |||
910 | default: // Map unknown values to default. | |||
911 | case 0: return GlobalValue::DefaultStorageClass; | |||
912 | case 1: return GlobalValue::DLLImportStorageClass; | |||
913 | case 2: return GlobalValue::DLLExportStorageClass; | |||
914 | } | |||
915 | } | |||
916 | ||||
917 | static bool getDecodedDSOLocal(unsigned Val) { | |||
918 | switch(Val) { | |||
919 | default: // Map unknown values to preemptable. | |||
920 | case 0: return false; | |||
921 | case 1: return true; | |||
922 | } | |||
923 | } | |||
924 | ||||
925 | static GlobalVariable::ThreadLocalMode getDecodedThreadLocalMode(unsigned Val) { | |||
926 | switch (Val) { | |||
927 | case 0: return GlobalVariable::NotThreadLocal; | |||
928 | default: // Map unknown non-zero value to general dynamic. | |||
929 | case 1: return GlobalVariable::GeneralDynamicTLSModel; | |||
930 | case 2: return GlobalVariable::LocalDynamicTLSModel; | |||
931 | case 3: return GlobalVariable::InitialExecTLSModel; | |||
932 | case 4: return GlobalVariable::LocalExecTLSModel; | |||
933 | } | |||
934 | } | |||
935 | ||||
936 | static GlobalVariable::UnnamedAddr getDecodedUnnamedAddrType(unsigned Val) { | |||
937 | switch (Val) { | |||
938 | default: // Map unknown to UnnamedAddr::None. | |||
939 | case 0: return GlobalVariable::UnnamedAddr::None; | |||
940 | case 1: return GlobalVariable::UnnamedAddr::Global; | |||
941 | case 2: return GlobalVariable::UnnamedAddr::Local; | |||
942 | } | |||
943 | } | |||
944 | ||||
945 | static int getDecodedCastOpcode(unsigned Val) { | |||
946 | switch (Val) { | |||
947 | default: return -1; | |||
948 | case bitc::CAST_TRUNC : return Instruction::Trunc; | |||
949 | case bitc::CAST_ZEXT : return Instruction::ZExt; | |||
950 | case bitc::CAST_SEXT : return Instruction::SExt; | |||
951 | case bitc::CAST_FPTOUI : return Instruction::FPToUI; | |||
952 | case bitc::CAST_FPTOSI : return Instruction::FPToSI; | |||
953 | case bitc::CAST_UITOFP : return Instruction::UIToFP; | |||
954 | case bitc::CAST_SITOFP : return Instruction::SIToFP; | |||
955 | case bitc::CAST_FPTRUNC : return Instruction::FPTrunc; | |||
956 | case bitc::CAST_FPEXT : return Instruction::FPExt; | |||
957 | case bitc::CAST_PTRTOINT: return Instruction::PtrToInt; | |||
958 | case bitc::CAST_INTTOPTR: return Instruction::IntToPtr; | |||
959 | case bitc::CAST_BITCAST : return Instruction::BitCast; | |||
960 | case bitc::CAST_ADDRSPACECAST: return Instruction::AddrSpaceCast; | |||
961 | } | |||
962 | } | |||
963 | ||||
964 | static int getDecodedBinaryOpcode(unsigned Val, Type *Ty) { | |||
965 | bool IsFP = Ty->isFPOrFPVectorTy(); | |||
966 | // BinOps are only valid for int/fp or vector of int/fp types | |||
967 | if (!IsFP && !Ty->isIntOrIntVectorTy()) | |||
968 | return -1; | |||
969 | ||||
970 | switch (Val) { | |||
971 | default: | |||
972 | return -1; | |||
973 | case bitc::BINOP_ADD: | |||
974 | return IsFP ? Instruction::FAdd : Instruction::Add; | |||
975 | case bitc::BINOP_SUB: | |||
976 | return IsFP ? Instruction::FSub : Instruction::Sub; | |||
977 | case bitc::BINOP_MUL: | |||
978 | return IsFP ? Instruction::FMul : Instruction::Mul; | |||
979 | case bitc::BINOP_UDIV: | |||
980 | return IsFP ? -1 : Instruction::UDiv; | |||
981 | case bitc::BINOP_SDIV: | |||
982 | return IsFP ? Instruction::FDiv : Instruction::SDiv; | |||
983 | case bitc::BINOP_UREM: | |||
984 | return IsFP ? -1 : Instruction::URem; | |||
985 | case bitc::BINOP_SREM: | |||
986 | return IsFP ? Instruction::FRem : Instruction::SRem; | |||
987 | case bitc::BINOP_SHL: | |||
988 | return IsFP ? -1 : Instruction::Shl; | |||
989 | case bitc::BINOP_LSHR: | |||
990 | return IsFP ? -1 : Instruction::LShr; | |||
991 | case bitc::BINOP_ASHR: | |||
992 | return IsFP ? -1 : Instruction::AShr; | |||
993 | case bitc::BINOP_AND: | |||
994 | return IsFP ? -1 : Instruction::And; | |||
995 | case bitc::BINOP_OR: | |||
996 | return IsFP ? -1 : Instruction::Or; | |||
997 | case bitc::BINOP_XOR: | |||
998 | return IsFP ? -1 : Instruction::Xor; | |||
999 | } | |||
1000 | } | |||
1001 | ||||
1002 | static AtomicRMWInst::BinOp getDecodedRMWOperation(unsigned Val) { | |||
1003 | switch (Val) { | |||
1004 | default: return AtomicRMWInst::BAD_BINOP; | |||
1005 | case bitc::RMW_XCHG: return AtomicRMWInst::Xchg; | |||
1006 | case bitc::RMW_ADD: return AtomicRMWInst::Add; | |||
1007 | case bitc::RMW_SUB: return AtomicRMWInst::Sub; | |||
1008 | case bitc::RMW_AND: return AtomicRMWInst::And; | |||
1009 | case bitc::RMW_NAND: return AtomicRMWInst::Nand; | |||
1010 | case bitc::RMW_OR: return AtomicRMWInst::Or; | |||
1011 | case bitc::RMW_XOR: return AtomicRMWInst::Xor; | |||
1012 | case bitc::RMW_MAX: return AtomicRMWInst::Max; | |||
1013 | case bitc::RMW_MIN: return AtomicRMWInst::Min; | |||
1014 | case bitc::RMW_UMAX: return AtomicRMWInst::UMax; | |||
1015 | case bitc::RMW_UMIN: return AtomicRMWInst::UMin; | |||
1016 | } | |||
1017 | } | |||
1018 | ||||
1019 | static AtomicOrdering getDecodedOrdering(unsigned Val) { | |||
1020 | switch (Val) { | |||
1021 | case bitc::ORDERING_NOTATOMIC: return AtomicOrdering::NotAtomic; | |||
1022 | case bitc::ORDERING_UNORDERED: return AtomicOrdering::Unordered; | |||
1023 | case bitc::ORDERING_MONOTONIC: return AtomicOrdering::Monotonic; | |||
1024 | case bitc::ORDERING_ACQUIRE: return AtomicOrdering::Acquire; | |||
1025 | case bitc::ORDERING_RELEASE: return AtomicOrdering::Release; | |||
1026 | case bitc::ORDERING_ACQREL: return AtomicOrdering::AcquireRelease; | |||
1027 | default: // Map unknown orderings to sequentially-consistent. | |||
1028 | case bitc::ORDERING_SEQCST: return AtomicOrdering::SequentiallyConsistent; | |||
1029 | } | |||
1030 | } | |||
1031 | ||||
1032 | static Comdat::SelectionKind getDecodedComdatSelectionKind(unsigned Val) { | |||
1033 | switch (Val) { | |||
1034 | default: // Map unknown selection kinds to any. | |||
1035 | case bitc::COMDAT_SELECTION_KIND_ANY: | |||
1036 | return Comdat::Any; | |||
1037 | case bitc::COMDAT_SELECTION_KIND_EXACT_MATCH: | |||
1038 | return Comdat::ExactMatch; | |||
1039 | case bitc::COMDAT_SELECTION_KIND_LARGEST: | |||
1040 | return Comdat::Largest; | |||
1041 | case bitc::COMDAT_SELECTION_KIND_NO_DUPLICATES: | |||
1042 | return Comdat::NoDuplicates; | |||
1043 | case bitc::COMDAT_SELECTION_KIND_SAME_SIZE: | |||
1044 | return Comdat::SameSize; | |||
1045 | } | |||
1046 | } | |||
1047 | ||||
1048 | static FastMathFlags getDecodedFastMathFlags(unsigned Val) { | |||
1049 | FastMathFlags FMF; | |||
1050 | if (0 != (Val & bitc::UnsafeAlgebra)) | |||
1051 | FMF.setFast(); | |||
1052 | if (0 != (Val & bitc::AllowReassoc)) | |||
1053 | FMF.setAllowReassoc(); | |||
1054 | if (0 != (Val & bitc::NoNaNs)) | |||
1055 | FMF.setNoNaNs(); | |||
1056 | if (0 != (Val & bitc::NoInfs)) | |||
1057 | FMF.setNoInfs(); | |||
1058 | if (0 != (Val & bitc::NoSignedZeros)) | |||
1059 | FMF.setNoSignedZeros(); | |||
1060 | if (0 != (Val & bitc::AllowReciprocal)) | |||
1061 | FMF.setAllowReciprocal(); | |||
1062 | if (0 != (Val & bitc::AllowContract)) | |||
1063 | FMF.setAllowContract(true); | |||
1064 | if (0 != (Val & bitc::ApproxFunc)) | |||
1065 | FMF.setApproxFunc(); | |||
1066 | return FMF; | |||
1067 | } | |||
1068 | ||||
1069 | static void upgradeDLLImportExportLinkage(GlobalValue *GV, unsigned Val) { | |||
1070 | switch (Val) { | |||
1071 | case 5: GV->setDLLStorageClass(GlobalValue::DLLImportStorageClass); break; | |||
1072 | case 6: GV->setDLLStorageClass(GlobalValue::DLLExportStorageClass); break; | |||
1073 | } | |||
1074 | } | |||
1075 | ||||
1076 | Type *BitcodeReader::getTypeByID(unsigned ID) { | |||
1077 | // The type table size is always specified correctly. | |||
1078 | if (ID >= TypeList.size()) | |||
1079 | return nullptr; | |||
1080 | ||||
1081 | if (Type *Ty = TypeList[ID]) | |||
1082 | return Ty; | |||
1083 | ||||
1084 | // If we have a forward reference, the only possible case is when it is to a | |||
1085 | // named struct. Just create a placeholder for now. | |||
1086 | return TypeList[ID] = createIdentifiedStructType(Context); | |||
1087 | } | |||
1088 | ||||
1089 | StructType *BitcodeReader::createIdentifiedStructType(LLVMContext &Context, | |||
1090 | StringRef Name) { | |||
1091 | auto *Ret = StructType::create(Context, Name); | |||
1092 | IdentifiedStructTypes.push_back(Ret); | |||
1093 | return Ret; | |||
1094 | } | |||
1095 | ||||
1096 | StructType *BitcodeReader::createIdentifiedStructType(LLVMContext &Context) { | |||
1097 | auto *Ret = StructType::create(Context); | |||
1098 | IdentifiedStructTypes.push_back(Ret); | |||
1099 | return Ret; | |||
1100 | } | |||
1101 | ||||
1102 | //===----------------------------------------------------------------------===// | |||
1103 | // Functions for parsing blocks from the bitcode file | |||
1104 | //===----------------------------------------------------------------------===// | |||
1105 | ||||
1106 | static uint64_t getRawAttributeMask(Attribute::AttrKind Val) { | |||
1107 | switch (Val) { | |||
1108 | case Attribute::EndAttrKinds: | |||
1109 | llvm_unreachable("Synthetic enumerators which should never get here")::llvm::llvm_unreachable_internal("Synthetic enumerators which should never get here" , "/build/llvm-toolchain-snapshot-7~svn329677/lib/Bitcode/Reader/BitcodeReader.cpp" , 1109); | |||
1110 | ||||
1111 | case Attribute::None: return 0; | |||
1112 | case Attribute::ZExt: return 1 << 0; | |||
1113 | case Attribute::SExt: return 1 << 1; | |||
1114 | case Attribute::NoReturn: return 1 << 2; | |||
1115 | case Attribute::InReg: return 1 << 3; | |||
1116 | case Attribute::StructRet: return 1 << 4; | |||
1117 | case Attribute::NoUnwind: return 1 << 5; | |||
1118 | case Attribute::NoAlias: return 1 << 6; | |||
1119 | case Attribute::ByVal: return 1 << 7; | |||
1120 | case Attribute::Nest: return 1 << 8; | |||
1121 | case Attribute::ReadNone: return 1 << 9; | |||
1122 | case Attribute::ReadOnly: return 1 << 10; | |||
1123 | case Attribute::NoInline: return 1 << 11; | |||
1124 | case Attribute::AlwaysInline: return 1 << 12; | |||
1125 | case Attribute::OptimizeForSize: return 1 << 13; | |||
1126 | case Attribute::StackProtect: return 1 << 14; | |||
1127 | case Attribute::StackProtectReq: return 1 << 15; | |||
1128 | case Attribute::Alignment: return 31 << 16; | |||
1129 | case Attribute::NoCapture: return 1 << 21; | |||
1130 | case Attribute::NoRedZone: return 1 << 22; | |||
1131 | case Attribute::NoImplicitFloat: return 1 << 23; | |||
1132 | case Attribute::Naked: return 1 << 24; | |||
1133 | case Attribute::InlineHint: return 1 << 25; | |||
1134 | case Attribute::StackAlignment: return 7 << 26; | |||
1135 | case Attribute::ReturnsTwice: return 1 << 29; | |||
1136 | case Attribute::UWTable: return 1 << 30; | |||
1137 | case Attribute::NonLazyBind: return 1U << 31; | |||
1138 | case Attribute::SanitizeAddress: return 1ULL << 32; | |||
1139 | case Attribute::MinSize: return 1ULL << 33; | |||
1140 | case Attribute::NoDuplicate: return 1ULL << 34; | |||
1141 | case Attribute::StackProtectStrong: return 1ULL << 35; | |||
1142 | case Attribute::SanitizeThread: return 1ULL << 36; | |||
1143 | case Attribute::SanitizeMemory: return 1ULL << 37; | |||
1144 | case Attribute::NoBuiltin: return 1ULL << 38; | |||
1145 | case Attribute::Returned: return 1ULL << 39; | |||
1146 | case Attribute::Cold: return 1ULL << 40; | |||
1147 | case Attribute::Builtin: return 1ULL << 41; | |||
1148 | case Attribute::OptimizeNone: return 1ULL << 42; | |||
1149 | case Attribute::InAlloca: return 1ULL << 43; | |||
1150 | case Attribute::NonNull: return 1ULL << 44; | |||
1151 | case Attribute::JumpTable: return 1ULL << 45; | |||
1152 | case Attribute::Convergent: return 1ULL << 46; | |||
1153 | case Attribute::SafeStack: return 1ULL << 47; | |||
1154 | case Attribute::NoRecurse: return 1ULL << 48; | |||
1155 | case Attribute::InaccessibleMemOnly: return 1ULL << 49; | |||
1156 | case Attribute::InaccessibleMemOrArgMemOnly: return 1ULL << 50; | |||
1157 | case Attribute::SwiftSelf: return 1ULL << 51; | |||
1158 | case Attribute::SwiftError: return 1ULL << 52; | |||
1159 | case Attribute::WriteOnly: return 1ULL << 53; | |||
1160 | case Attribute::Speculatable: return 1ULL << 54; | |||
1161 | case Attribute::StrictFP: return 1ULL << 55; | |||
1162 | case Attribute::SanitizeHWAddress: return 1ULL << 56; | |||
1163 | case Attribute::NoCfCheck: return 1ULL << 57; | |||
1164 | case Attribute::OptForFuzzing: return 1ULL << 58; | |||
1165 | case Attribute::ShadowCallStack: return 1ULL << 59; | |||
1166 | case Attribute::Dereferenceable: | |||
1167 | llvm_unreachable("dereferenceable attribute not supported in raw format")::llvm::llvm_unreachable_internal("dereferenceable attribute not supported in raw format" , "/build/llvm-toolchain-snapshot-7~svn329677/lib/Bitcode/Reader/BitcodeReader.cpp" , 1167); | |||
1168 | break; | |||
1169 | case Attribute::DereferenceableOrNull: | |||
1170 | 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-7~svn329677/lib/Bitcode/Reader/BitcodeReader.cpp" , 1171) | |||
1171 | "format")::llvm::llvm_unreachable_internal("dereferenceable_or_null attribute not supported in raw " "format", "/build/llvm-toolchain-snapshot-7~svn329677/lib/Bitcode/Reader/BitcodeReader.cpp" , 1171); | |||
1172 | break; | |||
1173 | case Attribute::ArgMemOnly: | |||
1174 | llvm_unreachable("argmemonly attribute not supported in raw format")::llvm::llvm_unreachable_internal("argmemonly attribute not supported in raw format" , "/build/llvm-toolchain-snapshot-7~svn329677/lib/Bitcode/Reader/BitcodeReader.cpp" , 1174); | |||
1175 | break; | |||
1176 | case Attribute::AllocSize: | |||
1177 | llvm_unreachable("allocsize not supported in raw format")::llvm::llvm_unreachable_internal("allocsize not supported in raw format" , "/build/llvm-toolchain-snapshot-7~svn329677/lib/Bitcode/Reader/BitcodeReader.cpp" , 1177); | |||
1178 | break; | |||
1179 | } | |||
1180 | llvm_unreachable("Unsupported attribute type")::llvm::llvm_unreachable_internal("Unsupported attribute type" , "/build/llvm-toolchain-snapshot-7~svn329677/lib/Bitcode/Reader/BitcodeReader.cpp" , 1180); | |||
1181 | } | |||
1182 | ||||
1183 | static void addRawAttributeValue(AttrBuilder &B, uint64_t Val) { | |||
1184 | if (!Val) return; | |||
1185 | ||||
1186 | for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds; | |||
1187 | I = Attribute::AttrKind(I + 1)) { | |||
1188 | if (I == Attribute::Dereferenceable || | |||
1189 | I == Attribute::DereferenceableOrNull || | |||
1190 | I == Attribute::ArgMemOnly || | |||
1191 | I == Attribute::AllocSize) | |||
1192 | continue; | |||
1193 | if (uint64_t A = (Val & getRawAttributeMask(I))) { | |||
1194 | if (I == Attribute::Alignment) | |||
1195 | B.addAlignmentAttr(1ULL << ((A >> 16) - 1)); | |||
1196 | else if (I == Attribute::StackAlignment) | |||
1197 | B.addStackAlignmentAttr(1ULL << ((A >> 26)-1)); | |||
1198 | else | |||
1199 | B.addAttribute(I); | |||
1200 | } | |||
1201 | } | |||
1202 | } | |||
1203 | ||||
1204 | /// \brief This fills an AttrBuilder object with the LLVM attributes that have | |||
1205 | /// been decoded from the given integer. This function must stay in sync with | |||
1206 | /// 'encodeLLVMAttributesForBitcode'. | |||
1207 | static void decodeLLVMAttributesForBitcode(AttrBuilder &B, | |||
1208 | uint64_t EncodedAttrs) { | |||
1209 | // FIXME: Remove in 4.0. | |||
1210 | ||||
1211 | // The alignment is stored as a 16-bit raw value from bits 31--16. We shift | |||
1212 | // the bits above 31 down by 11 bits. | |||
1213 | unsigned Alignment = (EncodedAttrs & (0xffffULL << 16)) >> 16; | |||
1214 | assert((!Alignment || isPowerOf2_32(Alignment)) &&(static_cast <bool> ((!Alignment || isPowerOf2_32(Alignment )) && "Alignment must be a power of two.") ? void (0) : __assert_fail ("(!Alignment || isPowerOf2_32(Alignment)) && \"Alignment must be a power of two.\"" , "/build/llvm-toolchain-snapshot-7~svn329677/lib/Bitcode/Reader/BitcodeReader.cpp" , 1215, __extension__ __PRETTY_FUNCTION__)) | |||
1215 | "Alignment must be a power of two.")(static_cast <bool> ((!Alignment || isPowerOf2_32(Alignment )) && "Alignment must be a power of two.") ? void (0) : __assert_fail ("(!Alignment || isPowerOf2_32(Alignment)) && \"Alignment must be a power of two.\"" , "/build/llvm-toolchain-snapshot-7~svn329677/lib/Bitcode/Reader/BitcodeReader.cpp" , 1215, __extension__ __PRETTY_FUNCTION__)); | |||
1216 | ||||
1217 | if (Alignment) | |||
1218 | B.addAlignmentAttr(Alignment); | |||
1219 | addRawAttributeValue(B, ((EncodedAttrs & (0xfffffULL << 32)) >> 11) | | |||
1220 | (EncodedAttrs & 0xffff)); | |||
1221 | } | |||
1222 | ||||
1223 | Error BitcodeReader::parseAttributeBlock() { | |||
1224 | if (Stream.EnterSubBlock(bitc::PARAMATTR_BLOCK_ID)) | |||
1225 | return error("Invalid record"); | |||
1226 | ||||
1227 | if (!MAttributes.empty()) | |||
1228 | return error("Invalid multiple blocks"); | |||
1229 | ||||
1230 | SmallVector<uint64_t, 64> Record; | |||
1231 | ||||
1232 | SmallVector<AttributeList, 8> Attrs; | |||
1233 | ||||
1234 | // Read all the records. | |||
1235 | while (true) { | |||
1236 | BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); | |||
1237 | ||||
1238 | switch (Entry.Kind) { | |||
1239 | case BitstreamEntry::SubBlock: // Handled for us already. | |||
1240 | case BitstreamEntry::Error: | |||
1241 | return error("Malformed block"); | |||
1242 | case BitstreamEntry::EndBlock: | |||
1243 | return Error::success(); | |||
1244 | case BitstreamEntry::Record: | |||
1245 | // The interesting case. | |||
1246 | break; | |||
1247 | } | |||
1248 | ||||
1249 | // Read a record. | |||
1250 | Record.clear(); | |||
1251 | switch (Stream.readRecord(Entry.ID, Record)) { | |||
1252 | default: // Default behavior: ignore. | |||
1253 | break; | |||
1254 | case bitc::PARAMATTR_CODE_ENTRY_OLD: // ENTRY: [paramidx0, attr0, ...] | |||
1255 | // FIXME: Remove in 4.0. | |||
1256 | if (Record.size() & 1) | |||
1257 | return error("Invalid record"); | |||
1258 | ||||
1259 | for (unsigned i = 0, e = Record.size(); i != e; i += 2) { | |||
1260 | AttrBuilder B; | |||
1261 | decodeLLVMAttributesForBitcode(B, Record[i+1]); | |||
1262 | Attrs.push_back(AttributeList::get(Context, Record[i], B)); | |||
1263 | } | |||
1264 | ||||
1265 | MAttributes.push_back(AttributeList::get(Context, Attrs)); | |||
1266 | Attrs.clear(); | |||
1267 | break; | |||
1268 | case bitc::PARAMATTR_CODE_ENTRY: // ENTRY: [attrgrp0, attrgrp1, ...] | |||
1269 | for (unsigned i = 0, e = Record.size(); i != e; ++i) | |||
1270 | Attrs.push_back(MAttributeGroups[Record[i]]); | |||
1271 | ||||
1272 | MAttributes.push_back(AttributeList::get(Context, Attrs)); | |||
1273 | Attrs.clear(); | |||
1274 | break; | |||
1275 | } | |||
1276 | } | |||
1277 | } | |||
1278 | ||||
1279 | // Returns Attribute::None on unrecognized codes. | |||
1280 | static Attribute::AttrKind getAttrFromCode(uint64_t Code) { | |||
1281 | switch (Code) { | |||
1282 | default: | |||
1283 | return Attribute::None; | |||
1284 | case bitc::ATTR_KIND_ALIGNMENT: | |||
1285 | return Attribute::Alignment; | |||
1286 | case bitc::ATTR_KIND_ALWAYS_INLINE: | |||
1287 | return Attribute::AlwaysInline; | |||
1288 | case bitc::ATTR_KIND_ARGMEMONLY: | |||
1289 | return Attribute::ArgMemOnly; | |||
1290 | case bitc::ATTR_KIND_BUILTIN: | |||
1291 | return Attribute::Builtin; | |||
1292 | case bitc::ATTR_KIND_BY_VAL: | |||
1293 | return Attribute::ByVal; | |||
1294 | case bitc::ATTR_KIND_IN_ALLOCA: | |||
1295 | return Attribute::InAlloca; | |||
1296 | case bitc::ATTR_KIND_COLD: | |||
1297 | return Attribute::Cold; | |||
1298 | case bitc::ATTR_KIND_CONVERGENT: | |||
1299 | return Attribute::Convergent; | |||
1300 | case bitc::ATTR_KIND_INACCESSIBLEMEM_ONLY: | |||
1301 | return Attribute::InaccessibleMemOnly; | |||
1302 | case bitc::ATTR_KIND_INACCESSIBLEMEM_OR_ARGMEMONLY: | |||
1303 | return Attribute::InaccessibleMemOrArgMemOnly; | |||
1304 | case bitc::ATTR_KIND_INLINE_HINT: | |||
1305 | return Attribute::InlineHint; | |||
1306 | case bitc::ATTR_KIND_IN_REG: | |||
1307 | return Attribute::InReg; | |||
1308 | case bitc::ATTR_KIND_JUMP_TABLE: | |||
1309 | return Attribute::JumpTable; | |||
1310 | case bitc::ATTR_KIND_MIN_SIZE: | |||
1311 | return Attribute::MinSize; | |||
1312 | case bitc::ATTR_KIND_NAKED: | |||
1313 | return Attribute::Naked; | |||
1314 | case bitc::ATTR_KIND_NEST: | |||
1315 | return Attribute::Nest; | |||
1316 | case bitc::ATTR_KIND_NO_ALIAS: | |||
1317 | return Attribute::NoAlias; | |||
1318 | case bitc::ATTR_KIND_NO_BUILTIN: | |||
1319 | return Attribute::NoBuiltin; | |||
1320 | case bitc::ATTR_KIND_NO_CAPTURE: | |||
1321 | return Attribute::NoCapture; | |||
1322 | case bitc::ATTR_KIND_NO_DUPLICATE: | |||
1323 | return Attribute::NoDuplicate; | |||
1324 | case bitc::ATTR_KIND_NO_IMPLICIT_FLOAT: | |||
1325 | return Attribute::NoImplicitFloat; | |||
1326 | case bitc::ATTR_KIND_NO_INLINE: | |||
1327 | return Attribute::NoInline; | |||
1328 | case bitc::ATTR_KIND_NO_RECURSE: | |||
1329 | return Attribute::NoRecurse; | |||
1330 | case bitc::ATTR_KIND_NON_LAZY_BIND: | |||
1331 | return Attribute::NonLazyBind; | |||
1332 | case bitc::ATTR_KIND_NON_NULL: | |||
1333 | return Attribute::NonNull; | |||
1334 | case bitc::ATTR_KIND_DEREFERENCEABLE: | |||
1335 | return Attribute::Dereferenceable; | |||
1336 | case bitc::ATTR_KIND_DEREFERENCEABLE_OR_NULL: | |||
1337 | return Attribute::DereferenceableOrNull; | |||
1338 | case bitc::ATTR_KIND_ALLOC_SIZE: | |||
1339 | return Attribute::AllocSize; | |||
1340 | case bitc::ATTR_KIND_NO_RED_ZONE: | |||
1341 | return Attribute::NoRedZone; | |||
1342 | case bitc::ATTR_KIND_NO_RETURN: | |||
1343 | return Attribute::NoReturn; | |||
1344 | case bitc::ATTR_KIND_NOCF_CHECK: | |||
1345 | return Attribute::NoCfCheck; | |||
1346 | case bitc::ATTR_KIND_NO_UNWIND: | |||
1347 | return Attribute::NoUnwind; | |||
1348 | case bitc::ATTR_KIND_OPT_FOR_FUZZING: | |||
1349 | return Attribute::OptForFuzzing; | |||
1350 | case bitc::ATTR_KIND_OPTIMIZE_FOR_SIZE: | |||
1351 | return Attribute::OptimizeForSize; | |||
1352 | case bitc::ATTR_KIND_OPTIMIZE_NONE: | |||
1353 | return Attribute::OptimizeNone; | |||
1354 | case bitc::ATTR_KIND_READ_NONE: | |||
1355 | return Attribute::ReadNone; | |||
1356 | case bitc::ATTR_KIND_READ_ONLY: | |||
1357 | return Attribute::ReadOnly; | |||
1358 | case bitc::ATTR_KIND_RETURNED: | |||
1359 | return Attribute::Returned; | |||
1360 | case bitc::ATTR_KIND_RETURNS_TWICE: | |||
1361 | return Attribute::ReturnsTwice; | |||
1362 | case bitc::ATTR_KIND_S_EXT: | |||
1363 | return Attribute::SExt; | |||
1364 | case bitc::ATTR_KIND_SPECULATABLE: | |||
1365 | return Attribute::Speculatable; | |||
1366 | case bitc::ATTR_KIND_STACK_ALIGNMENT: | |||
1367 | return Attribute::StackAlignment; | |||
1368 | case bitc::ATTR_KIND_STACK_PROTECT: | |||
1369 | return Attribute::StackProtect; | |||
1370 | case bitc::ATTR_KIND_STACK_PROTECT_REQ: | |||
1371 | return Attribute::StackProtectReq; | |||
1372 | case bitc::ATTR_KIND_STACK_PROTECT_STRONG: | |||
1373 | return Attribute::StackProtectStrong; | |||
1374 | case bitc::ATTR_KIND_SAFESTACK: | |||
1375 | return Attribute::SafeStack; | |||
1376 | case bitc::ATTR_KIND_SHADOWCALLSTACK: | |||
1377 | return Attribute::ShadowCallStack; | |||
1378 | case bitc::ATTR_KIND_STRICT_FP: | |||
1379 | return Attribute::StrictFP; | |||
1380 | case bitc::ATTR_KIND_STRUCT_RET: | |||
1381 | return Attribute::StructRet; | |||
1382 | case bitc::ATTR_KIND_SANITIZE_ADDRESS: | |||
1383 | return Attribute::SanitizeAddress; | |||
1384 | case bitc::ATTR_KIND_SANITIZE_HWADDRESS: | |||
1385 | return Attribute::SanitizeHWAddress; | |||
1386 | case bitc::ATTR_KIND_SANITIZE_THREAD: | |||
1387 | return Attribute::SanitizeThread; | |||
1388 | case bitc::ATTR_KIND_SANITIZE_MEMORY: | |||
1389 | return Attribute::SanitizeMemory; | |||
1390 | case bitc::ATTR_KIND_SWIFT_ERROR: | |||
1391 | return Attribute::SwiftError; | |||
1392 | case bitc::ATTR_KIND_SWIFT_SELF: | |||
1393 | return Attribute::SwiftSelf; | |||
1394 | case bitc::ATTR_KIND_UW_TABLE: | |||
1395 | return Attribute::UWTable; | |||
1396 | case bitc::ATTR_KIND_WRITEONLY: | |||
1397 | return Attribute::WriteOnly; | |||
1398 | case bitc::ATTR_KIND_Z_EXT: | |||
1399 | return Attribute::ZExt; | |||
1400 | } | |||
1401 | } | |||
1402 | ||||
1403 | Error BitcodeReader::parseAlignmentValue(uint64_t Exponent, | |||
1404 | unsigned &Alignment) { | |||
1405 | // Note: Alignment in bitcode files is incremented by 1, so that zero | |||
1406 | // can be used for default alignment. | |||
1407 | if (Exponent > Value::MaxAlignmentExponent + 1) | |||
1408 | return error("Invalid alignment value"); | |||
1409 | Alignment = (1 << static_cast<unsigned>(Exponent)) >> 1; | |||
1410 | return Error::success(); | |||
1411 | } | |||
1412 | ||||
1413 | Error BitcodeReader::parseAttrKind(uint64_t Code, Attribute::AttrKind *Kind) { | |||
1414 | *Kind = getAttrFromCode(Code); | |||
1415 | if (*Kind == Attribute::None) | |||
1416 | return error("Unknown attribute kind (" + Twine(Code) + ")"); | |||
1417 | return Error::success(); | |||
1418 | } | |||
1419 | ||||
1420 | Error BitcodeReader::parseAttributeGroupBlock() { | |||
1421 | if (Stream.EnterSubBlock(bitc::PARAMATTR_GROUP_BLOCK_ID)) | |||
1422 | return error("Invalid record"); | |||
1423 | ||||
1424 | if (!MAttributeGroups.empty()) | |||
1425 | return error("Invalid multiple blocks"); | |||
1426 | ||||
1427 | SmallVector<uint64_t, 64> Record; | |||
1428 | ||||
1429 | // Read all the records. | |||
1430 | while (true) { | |||
1431 | BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); | |||
1432 | ||||
1433 | switch (Entry.Kind) { | |||
1434 | case BitstreamEntry::SubBlock: // Handled for us already. | |||
1435 | case BitstreamEntry::Error: | |||
1436 | return error("Malformed block"); | |||
1437 | case BitstreamEntry::EndBlock: | |||
1438 | return Error::success(); | |||
1439 | case BitstreamEntry::Record: | |||
1440 | // The interesting case. | |||
1441 | break; | |||
1442 | } | |||
1443 | ||||
1444 | // Read a record. | |||
1445 | Record.clear(); | |||
1446 | switch (Stream.readRecord(Entry.ID, Record)) { | |||
1447 | default: // Default behavior: ignore. | |||
1448 | break; | |||
1449 | case bitc::PARAMATTR_GRP_CODE_ENTRY: { // ENTRY: [grpid, idx, a0, a1, ...] | |||
1450 | if (Record.size() < 3) | |||
1451 | return error("Invalid record"); | |||
1452 | ||||
1453 | uint64_t GrpID = Record[0]; | |||
1454 | uint64_t Idx = Record[1]; // Index of the object this attribute refers to. | |||
1455 | ||||
1456 | AttrBuilder B; | |||
1457 | for (unsigned i = 2, e = Record.size(); i != e; ++i) { | |||
1458 | if (Record[i] == 0) { // Enum attribute | |||
1459 | Attribute::AttrKind Kind; | |||
1460 | if (Error Err = parseAttrKind(Record[++i], &Kind)) | |||
1461 | return Err; | |||
1462 | ||||
1463 | B.addAttribute(Kind); | |||
1464 | } else if (Record[i] == 1) { // Integer attribute | |||
1465 | Attribute::AttrKind Kind; | |||
1466 | if (Error Err = parseAttrKind(Record[++i], &Kind)) | |||
1467 | return Err; | |||
1468 | if (Kind == Attribute::Alignment) | |||
1469 | B.addAlignmentAttr(Record[++i]); | |||
1470 | else if (Kind == Attribute::StackAlignment) | |||
1471 | B.addStackAlignmentAttr(Record[++i]); | |||
1472 | else if (Kind == Attribute::Dereferenceable) | |||
1473 | B.addDereferenceableAttr(Record[++i]); | |||
1474 | else if (Kind == Attribute::DereferenceableOrNull) | |||
1475 | B.addDereferenceableOrNullAttr(Record[++i]); | |||
1476 | else if (Kind == Attribute::AllocSize) | |||
1477 | B.addAllocSizeAttrFromRawRepr(Record[++i]); | |||
1478 | } else { // String attribute | |||
1479 | assert((Record[i] == 3 || Record[i] == 4) &&(static_cast <bool> ((Record[i] == 3 || Record[i] == 4) && "Invalid attribute group entry") ? void (0) : __assert_fail ("(Record[i] == 3 || Record[i] == 4) && \"Invalid attribute group entry\"" , "/build/llvm-toolchain-snapshot-7~svn329677/lib/Bitcode/Reader/BitcodeReader.cpp" , 1480, __extension__ __PRETTY_FUNCTION__)) | |||
1480 | "Invalid attribute group entry")(static_cast <bool> ((Record[i] == 3 || Record[i] == 4) && "Invalid attribute group entry") ? void (0) : __assert_fail ("(Record[i] == 3 || Record[i] == 4) && \"Invalid attribute group entry\"" , "/build/llvm-toolchain-snapshot-7~svn329677/lib/Bitcode/Reader/BitcodeReader.cpp" , 1480, __extension__ __PRETTY_FUNCTION__)); | |||
1481 | bool HasValue = (Record[i++] == 4); | |||
1482 | SmallString<64> KindStr; | |||
1483 | SmallString<64> ValStr; | |||
1484 | ||||
1485 | while (Record[i] != 0 && i != e) | |||
1486 | KindStr += Record[i++]; | |||
1487 | assert(Record[i] == 0 && "Kind string not null terminated")(static_cast <bool> (Record[i] == 0 && "Kind string not null terminated" ) ? void (0) : __assert_fail ("Record[i] == 0 && \"Kind string not null terminated\"" , "/build/llvm-toolchain-snapshot-7~svn329677/lib/Bitcode/Reader/BitcodeReader.cpp" , 1487, __extension__ __PRETTY_FUNCTION__)); | |||
1488 | ||||
1489 | if (HasValue) { | |||
1490 | // Has a value associated with it. | |||
1491 | ++i; // Skip the '0' that terminates the "kind" string. | |||
1492 | while (Record[i] != 0 && i != e) | |||
1493 | ValStr += Record[i++]; | |||
1494 | assert(Record[i] == 0 && "Value string not null terminated")(static_cast <bool> (Record[i] == 0 && "Value string not null terminated" ) ? void (0) : __assert_fail ("Record[i] == 0 && \"Value string not null terminated\"" , "/build/llvm-toolchain-snapshot-7~svn329677/lib/Bitcode/Reader/BitcodeReader.cpp" , 1494, __extension__ __PRETTY_FUNCTION__)); | |||
1495 | } | |||
1496 | ||||
1497 | B.addAttribute(KindStr.str(), ValStr.str()); | |||
1498 | } | |||
1499 | } | |||
1500 | ||||
1501 | MAttributeGroups[GrpID] = AttributeList::get(Context, Idx, B); | |||
1502 | break; | |||
1503 | } | |||
1504 | } | |||
1505 | } | |||
1506 | } | |||
1507 | ||||
1508 | Error BitcodeReader::parseTypeTable() { | |||
1509 | if (Stream.EnterSubBlock(bitc::TYPE_BLOCK_ID_NEW)) | |||
1510 | return error("Invalid record"); | |||
1511 | ||||
1512 | return parseTypeTableBody(); | |||
1513 | } | |||
1514 | ||||
1515 | Error BitcodeReader::parseTypeTableBody() { | |||
1516 | if (!TypeList.empty()) | |||
1517 | return error("Invalid multiple blocks"); | |||
1518 | ||||
1519 | SmallVector<uint64_t, 64> Record; | |||
1520 | unsigned NumRecords = 0; | |||
1521 | ||||
1522 | SmallString<64> TypeName; | |||
1523 | ||||
1524 | // Read all the records for this type table. | |||
1525 | while (true) { | |||
1526 | BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); | |||
1527 | ||||
1528 | switch (Entry.Kind) { | |||
1529 | case BitstreamEntry::SubBlock: // Handled for us already. | |||
1530 | case BitstreamEntry::Error: | |||
1531 | return error("Malformed block"); | |||
1532 | case BitstreamEntry::EndBlock: | |||
1533 | if (NumRecords != TypeList.size()) | |||
1534 | return error("Malformed block"); | |||
1535 | return Error::success(); | |||
1536 | case BitstreamEntry::Record: | |||
1537 | // The interesting case. | |||
1538 | break; | |||
1539 | } | |||
1540 | ||||
1541 | // Read a record. | |||
1542 | Record.clear(); | |||
1543 | Type *ResultTy = nullptr; | |||
1544 | switch (Stream.readRecord(Entry.ID, Record)) { | |||
1545 | default: | |||
1546 | return error("Invalid value"); | |||
1547 | case bitc::TYPE_CODE_NUMENTRY: // TYPE_CODE_NUMENTRY: [numentries] | |||
1548 | // TYPE_CODE_NUMENTRY contains a count of the number of types in the | |||
1549 | // type list. This allows us to reserve space. | |||
1550 | if (Record.size() < 1) | |||
1551 | return error("Invalid record"); | |||
1552 | TypeList.resize(Record[0]); | |||
1553 | continue; | |||
1554 | case bitc::TYPE_CODE_VOID: // VOID | |||
1555 | ResultTy = Type::getVoidTy(Context); | |||
1556 | break; | |||
1557 | case bitc::TYPE_CODE_HALF: // HALF | |||
1558 | ResultTy = Type::getHalfTy(Context); | |||
1559 | break; | |||
1560 | case bitc::TYPE_CODE_FLOAT: // FLOAT | |||
1561 | ResultTy = Type::getFloatTy(Context); | |||
1562 | break; | |||
1563 | case bitc::TYPE_CODE_DOUBLE: // DOUBLE | |||
1564 | ResultTy = Type::getDoubleTy(Context); | |||
1565 | break; | |||
1566 | case bitc::TYPE_CODE_X86_FP80: // X86_FP80 | |||
1567 | ResultTy = Type::getX86_FP80Ty(Context); | |||
1568 | break; | |||
1569 | case bitc::TYPE_CODE_FP128: // FP128 | |||
1570 | ResultTy = Type::getFP128Ty(Context); | |||
1571 | break; | |||
1572 | case bitc::TYPE_CODE_PPC_FP128: // PPC_FP128 | |||
1573 | ResultTy = Type::getPPC_FP128Ty(Context); | |||
1574 | break; | |||
1575 | case bitc::TYPE_CODE_LABEL: // LABEL | |||
1576 | ResultTy = Type::getLabelTy(Context); | |||
1577 | break; | |||
1578 | case bitc::TYPE_CODE_METADATA: // METADATA | |||
1579 | ResultTy = Type::getMetadataTy(Context); | |||
1580 | break; | |||
1581 | case bitc::TYPE_CODE_X86_MMX: // X86_MMX | |||
1582 | ResultTy = Type::getX86_MMXTy(Context); | |||
1583 | break; | |||
1584 | case bitc::TYPE_CODE_TOKEN: // TOKEN | |||
1585 | ResultTy = Type::getTokenTy(Context); | |||
1586 | break; | |||
1587 | case bitc::TYPE_CODE_INTEGER: { // INTEGER: [width] | |||
1588 | if (Record.size() < 1) | |||
1589 | return error("Invalid record"); | |||
1590 | ||||
1591 | uint64_t NumBits = Record[0]; | |||
1592 | if (NumBits < IntegerType::MIN_INT_BITS || | |||
1593 | NumBits > IntegerType::MAX_INT_BITS) | |||
1594 | return error("Bitwidth for integer type out of range"); | |||
1595 | ResultTy = IntegerType::get(Context, NumBits); | |||
1596 | break; | |||
1597 | } | |||
1598 | case bitc::TYPE_CODE_POINTER: { // POINTER: [pointee type] or | |||
1599 | // [pointee type, address space] | |||
1600 | if (Record.size() < 1) | |||
1601 | return error("Invalid record"); | |||
1602 | unsigned AddressSpace = 0; | |||
1603 | if (Record.size() == 2) | |||
1604 | AddressSpace = Record[1]; | |||
1605 | ResultTy = getTypeByID(Record[0]); | |||
1606 | if (!ResultTy || | |||
1607 | !PointerType::isValidElementType(ResultTy)) | |||
1608 | return error("Invalid type"); | |||
1609 | ResultTy = PointerType::get(ResultTy, AddressSpace); | |||
1610 | break; | |||
1611 | } | |||
1612 | case bitc::TYPE_CODE_FUNCTION_OLD: { | |||
1613 | // FIXME: attrid is dead, remove it in LLVM 4.0 | |||
1614 | // FUNCTION: [vararg, attrid, retty, paramty x N] | |||
1615 | if (Record.size() < 3) | |||
1616 | return error("Invalid record"); | |||
1617 | SmallVector<Type*, 8> ArgTys; | |||
1618 | for (unsigned i = 3, e = Record.size(); i != e; ++i) { | |||
1619 | if (Type *T = getTypeByID(Record[i])) | |||
1620 | ArgTys.push_back(T); | |||
1621 | else | |||
1622 | break; | |||
1623 | } | |||
1624 | ||||
1625 | ResultTy = getTypeByID(Record[2]); | |||
1626 | if (!ResultTy || ArgTys.size() < Record.size()-3) | |||
1627 | return error("Invalid type"); | |||
1628 | ||||
1629 | ResultTy = FunctionType::get(ResultTy, ArgTys, Record[0]); | |||
1630 | break; | |||
1631 | } | |||
1632 | case bitc::TYPE_CODE_FUNCTION: { | |||
1633 | // FUNCTION: [vararg, retty, paramty x N] | |||
1634 | if (Record.size() < 2) | |||
1635 | return error("Invalid record"); | |||
1636 | SmallVector<Type*, 8> ArgTys; | |||
1637 | for (unsigned i = 2, e = Record.size(); i != e; ++i) { | |||
1638 | if (Type *T = getTypeByID(Record[i])) { | |||
1639 | if (!FunctionType::isValidArgumentType(T)) | |||
1640 | return error("Invalid function argument type"); | |||
1641 | ArgTys.push_back(T); | |||
1642 | } | |||
1643 | else | |||
1644 | break; | |||
1645 | } | |||
1646 | ||||
1647 | ResultTy = getTypeByID(Record[1]); | |||
1648 | if (!ResultTy || ArgTys.size() < Record.size()-2) | |||
1649 | return error("Invalid type"); | |||
1650 | ||||
1651 | ResultTy = FunctionType::get(ResultTy, ArgTys, Record[0]); | |||
1652 | break; | |||
1653 | } | |||
1654 | case bitc::TYPE_CODE_STRUCT_ANON: { // STRUCT: [ispacked, eltty x N] | |||
1655 | if (Record.size() < 1) | |||
1656 | return error("Invalid record"); | |||
1657 | SmallVector<Type*, 8> EltTys; | |||
1658 | for (unsigned i = 1, e = Record.size(); i != e; ++i) { | |||
1659 | if (Type *T = getTypeByID(Record[i])) | |||
1660 | EltTys.push_back(T); | |||
1661 | else | |||
1662 | break; | |||
1663 | } | |||
1664 | if (EltTys.size() != Record.size()-1) | |||
1665 | return error("Invalid type"); | |||
1666 | ResultTy = StructType::get(Context, EltTys, Record[0]); | |||
1667 | break; | |||
1668 | } | |||
1669 | case bitc::TYPE_CODE_STRUCT_NAME: // STRUCT_NAME: [strchr x N] | |||
1670 | if (convertToString(Record, 0, TypeName)) | |||
1671 | return error("Invalid record"); | |||
1672 | continue; | |||
1673 | ||||
1674 | case bitc::TYPE_CODE_STRUCT_NAMED: { // STRUCT: [ispacked, eltty x N] | |||
1675 | if (Record.size() < 1) | |||
1676 | return error("Invalid record"); | |||
1677 | ||||
1678 | if (NumRecords >= TypeList.size()) | |||
1679 | return error("Invalid TYPE table"); | |||
1680 | ||||
1681 | // Check to see if this was forward referenced, if so fill in the temp. | |||
1682 | StructType *Res = cast_or_null<StructType>(TypeList[NumRecords]); | |||
1683 | if (Res) { | |||
1684 | Res->setName(TypeName); | |||
1685 | TypeList[NumRecords] = nullptr; | |||
1686 | } else // Otherwise, create a new struct. | |||
1687 | Res = createIdentifiedStructType(Context, TypeName); | |||
1688 | TypeName.clear(); | |||
1689 | ||||
1690 | SmallVector<Type*, 8> EltTys; | |||
1691 | for (unsigned i = 1, e = Record.size(); i != e; ++i) { | |||
1692 | if (Type *T = getTypeByID(Record[i])) | |||
1693 | EltTys.push_back(T); | |||
1694 | else | |||
1695 | break; | |||
1696 | } | |||
1697 | if (EltTys.size() != Record.size()-1) | |||
1698 | return error("Invalid record"); | |||
1699 | Res->setBody(EltTys, Record[0]); | |||
1700 | ResultTy = Res; | |||
1701 | break; | |||
1702 | } | |||
1703 | case bitc::TYPE_CODE_OPAQUE: { // OPAQUE: [] | |||
1704 | if (Record.size() != 1) | |||
1705 | return error("Invalid record"); | |||
1706 | ||||
1707 | if (NumRecords >= TypeList.size()) | |||
1708 | return error("Invalid TYPE table"); | |||
1709 | ||||
1710 | // Check to see if this was forward referenced, if so fill in the temp. | |||
1711 | StructType *Res = cast_or_null<StructType>(TypeList[NumRecords]); | |||
1712 | if (Res) { | |||
1713 | Res->setName(TypeName); | |||
1714 | TypeList[NumRecords] = nullptr; | |||
1715 | } else // Otherwise, create a new struct with no body. | |||
1716 | Res = createIdentifiedStructType(Context, TypeName); | |||
1717 | TypeName.clear(); | |||
1718 | ResultTy = Res; | |||
1719 | break; | |||
1720 | } | |||
1721 | case bitc::TYPE_CODE_ARRAY: // ARRAY: [numelts, eltty] | |||
1722 | if (Record.size() < 2) | |||
1723 | return error("Invalid record"); | |||
1724 | ResultTy = getTypeByID(Record[1]); | |||
1725 | if (!ResultTy || !ArrayType::isValidElementType(ResultTy)) | |||
1726 | return error("Invalid type"); | |||
1727 | ResultTy = ArrayType::get(ResultTy, Record[0]); | |||
1728 | break; | |||
1729 | case bitc::TYPE_CODE_VECTOR: // VECTOR: [numelts, eltty] | |||
1730 | if (Record.size() < 2) | |||
1731 | return error("Invalid record"); | |||
1732 | if (Record[0] == 0) | |||
1733 | return error("Invalid vector length"); | |||
1734 | ResultTy = getTypeByID(Record[1]); | |||
1735 | if (!ResultTy || !StructType::isValidElementType(ResultTy)) | |||
1736 | return error("Invalid type"); | |||
1737 | ResultTy = VectorType::get(ResultTy, Record[0]); | |||
1738 | break; | |||
1739 | } | |||
1740 | ||||
1741 | if (NumRecords >= TypeList.size()) | |||
1742 | return error("Invalid TYPE table"); | |||
1743 | if (TypeList[NumRecords]) | |||
1744 | return error( | |||
1745 | "Invalid TYPE table: Only named structs can be forward referenced"); | |||
1746 | assert(ResultTy && "Didn't read a type?")(static_cast <bool> (ResultTy && "Didn't read a type?" ) ? void (0) : __assert_fail ("ResultTy && \"Didn't read a type?\"" , "/build/llvm-toolchain-snapshot-7~svn329677/lib/Bitcode/Reader/BitcodeReader.cpp" , 1746, __extension__ __PRETTY_FUNCTION__)); | |||
1747 | TypeList[NumRecords++] = ResultTy; | |||
1748 | } | |||
1749 | } | |||
1750 | ||||
1751 | Error BitcodeReader::parseOperandBundleTags() { | |||
1752 | if (Stream.EnterSubBlock(bitc::OPERAND_BUNDLE_TAGS_BLOCK_ID)) | |||
1753 | return error("Invalid record"); | |||
1754 | ||||
1755 | if (!BundleTags.empty()) | |||
1756 | return error("Invalid multiple blocks"); | |||
1757 | ||||
1758 | SmallVector<uint64_t, 64> Record; | |||
1759 | ||||
1760 | while (true) { | |||
1761 | BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); | |||
1762 | ||||
1763 | switch (Entry.Kind) { | |||
1764 | case BitstreamEntry::SubBlock: // Handled for us already. | |||
1765 | case BitstreamEntry::Error: | |||
1766 | return error("Malformed block"); | |||
1767 | case BitstreamEntry::EndBlock: | |||
1768 | return Error::success(); | |||
1769 | case BitstreamEntry::Record: | |||
1770 | // The interesting case. | |||
1771 | break; | |||
1772 | } | |||
1773 | ||||
1774 | // Tags are implicitly mapped to integers by their order. | |||
1775 | ||||
1776 | if (Stream.readRecord(Entry.ID, Record) != bitc::OPERAND_BUNDLE_TAG) | |||
1777 | return error("Invalid record"); | |||
1778 | ||||
1779 | // OPERAND_BUNDLE_TAG: [strchr x N] | |||
1780 | BundleTags.emplace_back(); | |||
1781 | if (convertToString(Record, 0, BundleTags.back())) | |||
1782 | return error("Invalid record"); | |||
1783 | Record.clear(); | |||
1784 | } | |||
1785 | } | |||
1786 | ||||
1787 | Error BitcodeReader::parseSyncScopeNames() { | |||
1788 | if (Stream.EnterSubBlock(bitc::SYNC_SCOPE_NAMES_BLOCK_ID)) | |||
1789 | return error("Invalid record"); | |||
1790 | ||||
1791 | if (!SSIDs.empty()) | |||
1792 | return error("Invalid multiple synchronization scope names blocks"); | |||
1793 | ||||
1794 | SmallVector<uint64_t, 64> Record; | |||
1795 | while (true) { | |||
1796 | BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); | |||
1797 | switch (Entry.Kind) { | |||
1798 | case BitstreamEntry::SubBlock: // Handled for us already. | |||
1799 | case BitstreamEntry::Error: | |||
1800 | return error("Malformed block"); | |||
1801 | case BitstreamEntry::EndBlock: | |||
1802 | if (SSIDs.empty()) | |||
1803 | return error("Invalid empty synchronization scope names block"); | |||
1804 | return Error::success(); | |||
1805 | case BitstreamEntry::Record: | |||
1806 | // The interesting case. | |||
1807 | break; | |||
1808 | } | |||
1809 | ||||
1810 | // Synchronization scope names are implicitly mapped to synchronization | |||
1811 | // scope IDs by their order. | |||
1812 | ||||
1813 | if (Stream.readRecord(Entry.ID, Record) != bitc::SYNC_SCOPE_NAME) | |||
1814 | return error("Invalid record"); | |||
1815 | ||||
1816 | SmallString<16> SSN; | |||
1817 | if (convertToString(Record, 0, SSN)) | |||
1818 | return error("Invalid record"); | |||
1819 | ||||
1820 | SSIDs.push_back(Context.getOrInsertSyncScopeID(SSN)); | |||
1821 | Record.clear(); | |||
1822 | } | |||
1823 | } | |||
1824 | ||||
1825 | /// Associate a value with its name from the given index in the provided record. | |||
1826 | Expected<Value *> BitcodeReader::recordValue(SmallVectorImpl<uint64_t> &Record, | |||
1827 | unsigned NameIndex, Triple &TT) { | |||
1828 | SmallString<128> ValueName; | |||
1829 | if (convertToString(Record, NameIndex, ValueName)) | |||
1830 | return error("Invalid record"); | |||
1831 | unsigned ValueID = Record[0]; | |||
1832 | if (ValueID >= ValueList.size() || !ValueList[ValueID]) | |||
1833 | return error("Invalid record"); | |||
1834 | Value *V = ValueList[ValueID]; | |||
1835 | ||||
1836 | StringRef NameStr(ValueName.data(), ValueName.size()); | |||
1837 | if (NameStr.find_first_of(0) != StringRef::npos) | |||
1838 | return error("Invalid value name"); | |||
1839 | V->setName(NameStr); | |||
1840 | auto *GO = dyn_cast<GlobalObject>(V); | |||
1841 | if (GO) { | |||
1842 | if (GO->getComdat() == reinterpret_cast<Comdat *>(1)) { | |||
1843 | if (TT.supportsCOMDAT()) | |||
1844 | GO->setComdat(TheModule->getOrInsertComdat(V->getName())); | |||
1845 | else | |||
1846 | GO->setComdat(nullptr); | |||
1847 | } | |||
1848 | } | |||
1849 | return V; | |||
1850 | } | |||
1851 | ||||
1852 | /// Helper to note and return the current location, and jump to the given | |||
1853 | /// offset. | |||
1854 | static uint64_t jumpToValueSymbolTable(uint64_t Offset, | |||
1855 | BitstreamCursor &Stream) { | |||
1856 | // Save the current parsing location so we can jump back at the end | |||
1857 | // of the VST read. | |||
1858 | uint64_t CurrentBit = Stream.GetCurrentBitNo(); | |||
1859 | Stream.JumpToBit(Offset * 32); | |||
1860 | #ifndef NDEBUG | |||
1861 | // Do some checking if we are in debug mode. | |||
1862 | BitstreamEntry Entry = Stream.advance(); | |||
1863 | assert(Entry.Kind == BitstreamEntry::SubBlock)(static_cast <bool> (Entry.Kind == BitstreamEntry::SubBlock ) ? void (0) : __assert_fail ("Entry.Kind == BitstreamEntry::SubBlock" , "/build/llvm-toolchain-snapshot-7~svn329677/lib/Bitcode/Reader/BitcodeReader.cpp" , 1863, __extension__ __PRETTY_FUNCTION__)); | |||
1864 | assert(Entry.ID == bitc::VALUE_SYMTAB_BLOCK_ID)(static_cast <bool> (Entry.ID == bitc::VALUE_SYMTAB_BLOCK_ID ) ? void (0) : __assert_fail ("Entry.ID == bitc::VALUE_SYMTAB_BLOCK_ID" , "/build/llvm-toolchain-snapshot-7~svn329677/lib/Bitcode/Reader/BitcodeReader.cpp" , 1864, __extension__ __PRETTY_FUNCTION__)); | |||
1865 | #else | |||
1866 | // In NDEBUG mode ignore the output so we don't get an unused variable | |||
1867 | // warning. | |||
1868 | Stream.advance(); | |||
1869 | #endif | |||
1870 | return CurrentBit; | |||
1871 | } | |||
1872 | ||||
1873 | void BitcodeReader::setDeferredFunctionInfo(unsigned FuncBitcodeOffsetDelta, | |||
1874 | Function *F, | |||
1875 | ArrayRef<uint64_t> Record) { | |||
1876 | // Note that we subtract 1 here because the offset is relative to one word | |||
1877 | // before the start of the identification or module block, which was | |||
1878 | // historically always the start of the regular bitcode header. | |||
1879 | uint64_t FuncWordOffset = Record[1] - 1; | |||
1880 | uint64_t FuncBitOffset = FuncWordOffset * 32; | |||
1881 | DeferredFunctionInfo[F] = FuncBitOffset + FuncBitcodeOffsetDelta; | |||
1882 | // Set the LastFunctionBlockBit to point to the last function block. | |||
1883 | // Later when parsing is resumed after function materialization, | |||
1884 | // we can simply skip that last function block. | |||
1885 | if (FuncBitOffset > LastFunctionBlockBit) | |||
1886 | LastFunctionBlockBit = FuncBitOffset; | |||
1887 | } | |||
1888 | ||||
1889 | /// Read a new-style GlobalValue symbol table. | |||
1890 | Error BitcodeReader::parseGlobalValueSymbolTable() { | |||
1891 | unsigned FuncBitcodeOffsetDelta = | |||
1892 | Stream.getAbbrevIDWidth() + bitc::BlockIDWidth; | |||
1893 | ||||
1894 | if (Stream.EnterSubBlock(bitc::VALUE_SYMTAB_BLOCK_ID)) | |||
1895 | return error("Invalid record"); | |||
1896 | ||||
1897 | SmallVector<uint64_t, 64> Record; | |||
1898 | while (true) { | |||
1899 | BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); | |||
1900 | ||||
1901 | switch (Entry.Kind) { | |||
1902 | case BitstreamEntry::SubBlock: | |||
1903 | case BitstreamEntry::Error: | |||
1904 | return error("Malformed block"); | |||
1905 | case BitstreamEntry::EndBlock: | |||
1906 | return Error::success(); | |||
1907 | case BitstreamEntry::Record: | |||
1908 | break; | |||
1909 | } | |||
1910 | ||||
1911 | Record.clear(); | |||
1912 | switch (Stream.readRecord(Entry.ID, Record)) { | |||
1913 | case bitc::VST_CODE_FNENTRY: // [valueid, offset] | |||
1914 | setDeferredFunctionInfo(FuncBitcodeOffsetDelta, | |||
1915 | cast<Function>(ValueList[Record[0]]), Record); | |||
1916 | break; | |||
1917 | } | |||
1918 | } | |||
1919 | } | |||
1920 | ||||
1921 | /// Parse the value symbol table at either the current parsing location or | |||
1922 | /// at the given bit offset if provided. | |||
1923 | Error BitcodeReader::parseValueSymbolTable(uint64_t Offset) { | |||
1924 | uint64_t CurrentBit; | |||
1925 | // Pass in the Offset to distinguish between calling for the module-level | |||
1926 | // VST (where we want to jump to the VST offset) and the function-level | |||
1927 | // VST (where we don't). | |||
1928 | if (Offset > 0) { | |||
1929 | CurrentBit = jumpToValueSymbolTable(Offset, Stream); | |||
1930 | // If this module uses a string table, read this as a module-level VST. | |||
1931 | if (UseStrtab) { | |||
1932 | if (Error Err = parseGlobalValueSymbolTable()) | |||
1933 | return Err; | |||
1934 | Stream.JumpToBit(CurrentBit); | |||
1935 | return Error::success(); | |||
1936 | } | |||
1937 | // Otherwise, the VST will be in a similar format to a function-level VST, | |||
1938 | // and will contain symbol names. | |||
1939 | } | |||
1940 | ||||
1941 | // Compute the delta between the bitcode indices in the VST (the word offset | |||
1942 | // to the word-aligned ENTER_SUBBLOCK for the function block, and that | |||
1943 | // expected by the lazy reader. The reader's EnterSubBlock expects to have | |||
1944 | // already read the ENTER_SUBBLOCK code (size getAbbrevIDWidth) and BlockID | |||
1945 | // (size BlockIDWidth). Note that we access the stream's AbbrevID width here | |||
1946 | // just before entering the VST subblock because: 1) the EnterSubBlock | |||
1947 | // changes the AbbrevID width; 2) the VST block is nested within the same | |||
1948 | // outer MODULE_BLOCK as the FUNCTION_BLOCKs and therefore have the same | |||
1949 | // AbbrevID width before calling EnterSubBlock; and 3) when we want to | |||
1950 | // jump to the FUNCTION_BLOCK using this offset later, we don't want | |||
1951 | // to rely on the stream's AbbrevID width being that of the MODULE_BLOCK. | |||
1952 | unsigned FuncBitcodeOffsetDelta = | |||
1953 | Stream.getAbbrevIDWidth() + bitc::BlockIDWidth; | |||
1954 | ||||
1955 | if (Stream.EnterSubBlock(bitc::VALUE_SYMTAB_BLOCK_ID)) | |||
1956 | return error("Invalid record"); | |||
1957 | ||||
1958 | SmallVector<uint64_t, 64> Record; | |||
1959 | ||||
1960 | Triple TT(TheModule->getTargetTriple()); | |||
1961 | ||||
1962 | // Read all the records for this value table. | |||
1963 | SmallString<128> ValueName; | |||
1964 | ||||
1965 | while (true) { | |||
1966 | BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); | |||
1967 | ||||
1968 | switch (Entry.Kind) { | |||
1969 | case BitstreamEntry::SubBlock: // Handled for us already. | |||
1970 | case BitstreamEntry::Error: | |||
1971 | return error("Malformed block"); | |||
1972 | case BitstreamEntry::EndBlock: | |||
1973 | if (Offset > 0) | |||
1974 | Stream.JumpToBit(CurrentBit); | |||
1975 | return Error::success(); | |||
1976 | case BitstreamEntry::Record: | |||
1977 | // The interesting case. | |||
1978 | break; | |||
1979 | } | |||
1980 | ||||
1981 | // Read a record. | |||
1982 | Record.clear(); | |||
1983 | switch (Stream.readRecord(Entry.ID, Record)) { | |||
1984 | default: // Default behavior: unknown type. | |||
1985 | break; | |||
1986 | case bitc::VST_CODE_ENTRY: { // VST_CODE_ENTRY: [valueid, namechar x N] | |||
1987 | Expected<Value *> ValOrErr = recordValue(Record, 1, TT); | |||
1988 | if (Error Err = ValOrErr.takeError()) | |||
1989 | return Err; | |||
1990 | ValOrErr.get(); | |||
1991 | break; | |||
1992 | } | |||
1993 | case bitc::VST_CODE_FNENTRY: { | |||
1994 | // VST_CODE_FNENTRY: [valueid, offset, namechar x N] | |||
1995 | Expected<Value *> ValOrErr = recordValue(Record, 2, TT); | |||
1996 | if (Error Err = ValOrErr.takeError()) | |||
1997 | return Err; | |||
1998 | Value *V = ValOrErr.get(); | |||
1999 | ||||
2000 | // Ignore function offsets emitted for aliases of functions in older | |||
2001 | // versions of LLVM. | |||
2002 | if (auto *F = dyn_cast<Function>(V)) | |||
2003 | setDeferredFunctionInfo(FuncBitcodeOffsetDelta, F, Record); | |||
2004 | break; | |||
2005 | } | |||
2006 | case bitc::VST_CODE_BBENTRY: { | |||
2007 | if (convertToString(Record, 1, ValueName)) | |||
2008 | return error("Invalid record"); | |||
2009 | BasicBlock *BB = getBasicBlock(Record[0]); | |||
2010 | if (!BB) | |||
2011 | return error("Invalid record"); | |||
2012 | ||||
2013 | BB->setName(StringRef(ValueName.data(), ValueName.size())); | |||
2014 | ValueName.clear(); | |||
2015 | break; | |||
2016 | } | |||
2017 | } | |||
2018 | } | |||
2019 | } | |||
2020 | ||||
2021 | /// Decode a signed value stored with the sign bit in the LSB for dense VBR | |||
2022 | /// encoding. | |||
2023 | uint64_t BitcodeReader::decodeSignRotatedValue(uint64_t V) { | |||
2024 | if ((V & 1) == 0) | |||
2025 | return V >> 1; | |||
2026 | if (V != 1) | |||
2027 | return -(V >> 1); | |||
2028 | // There is no such thing as -0 with integers. "-0" really means MININT. | |||
2029 | return 1ULL << 63; | |||
2030 | } | |||
2031 | ||||
2032 | /// Resolve all of the initializers for global values and aliases that we can. | |||
2033 | Error BitcodeReader::resolveGlobalAndIndirectSymbolInits() { | |||
2034 | std::vector<std::pair<GlobalVariable *, unsigned>> GlobalInitWorklist; | |||
2035 | std::vector<std::pair<GlobalIndirectSymbol *, unsigned>> | |||
2036 | IndirectSymbolInitWorklist; | |||
2037 | std::vector<std::pair<Function *, unsigned>> FunctionPrefixWorklist; | |||
2038 | std::vector<std::pair<Function *, unsigned>> FunctionPrologueWorklist; | |||
2039 | std::vector<std::pair<Function *, unsigned>> FunctionPersonalityFnWorklist; | |||
2040 | ||||
2041 | GlobalInitWorklist.swap(GlobalInits); | |||
2042 | IndirectSymbolInitWorklist.swap(IndirectSymbolInits); | |||
2043 | FunctionPrefixWorklist.swap(FunctionPrefixes); | |||
2044 | FunctionPrologueWorklist.swap(FunctionPrologues); | |||
2045 | FunctionPersonalityFnWorklist.swap(FunctionPersonalityFns); | |||
2046 | ||||
2047 | while (!GlobalInitWorklist.empty()) { | |||
2048 | unsigned ValID = GlobalInitWorklist.back().second; | |||
2049 | if (ValID >= ValueList.size()) { | |||
2050 | // Not ready to resolve this yet, it requires something later in the file. | |||
2051 | GlobalInits.push_back(GlobalInitWorklist.back()); | |||
2052 | } else { | |||
2053 | if (Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID])) | |||
2054 | GlobalInitWorklist.back().first->setInitializer(C); | |||
2055 | else | |||
2056 | return error("Expected a constant"); | |||
2057 | } | |||
2058 | GlobalInitWorklist.pop_back(); | |||
2059 | } | |||
2060 | ||||
2061 | while (!IndirectSymbolInitWorklist.empty()) { | |||
2062 | unsigned ValID = IndirectSymbolInitWorklist.back().second; | |||
2063 | if (ValID >= ValueList.size()) { | |||
2064 | IndirectSymbolInits.push_back(IndirectSymbolInitWorklist.back()); | |||
2065 | } else { | |||
2066 | Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID]); | |||
2067 | if (!C) | |||
2068 | return error("Expected a constant"); | |||
2069 | GlobalIndirectSymbol *GIS = IndirectSymbolInitWorklist.back().first; | |||
2070 | if (isa<GlobalAlias>(GIS) && C->getType() != GIS->getType()) | |||
2071 | return error("Alias and aliasee types don't match"); | |||
2072 | GIS->setIndirectSymbol(C); | |||
2073 | } | |||
2074 | IndirectSymbolInitWorklist.pop_back(); | |||
2075 | } | |||
2076 | ||||
2077 | while (!FunctionPrefixWorklist.empty()) { | |||
2078 | unsigned ValID = FunctionPrefixWorklist.back().second; | |||
2079 | if (ValID >= ValueList.size()) { | |||
2080 | FunctionPrefixes.push_back(FunctionPrefixWorklist.back()); | |||
2081 | } else { | |||
2082 | if (Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID])) | |||
2083 | FunctionPrefixWorklist.back().first->setPrefixData(C); | |||
2084 | else | |||
2085 | return error("Expected a constant"); | |||
2086 | } | |||
2087 | FunctionPrefixWorklist.pop_back(); | |||
2088 | } | |||
2089 | ||||
2090 | while (!FunctionPrologueWorklist.empty()) { | |||
2091 | unsigned ValID = FunctionPrologueWorklist.back().second; | |||
2092 | if (ValID >= ValueList.size()) { | |||
2093 | FunctionPrologues.push_back(FunctionPrologueWorklist.back()); | |||
2094 | } else { | |||
2095 | if (Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID])) | |||
2096 | FunctionPrologueWorklist.back().first->setPrologueData(C); | |||
2097 | else | |||
2098 | return error("Expected a constant"); | |||
2099 | } | |||
2100 | FunctionPrologueWorklist.pop_back(); | |||
2101 | } | |||
2102 | ||||
2103 | while (!FunctionPersonalityFnWorklist.empty()) { | |||
2104 | unsigned ValID = FunctionPersonalityFnWorklist.back().second; | |||
2105 | if (ValID >= ValueList.size()) { | |||
2106 | FunctionPersonalityFns.push_back(FunctionPersonalityFnWorklist.back()); | |||
2107 | } else { | |||
2108 | if (Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID])) | |||
2109 | FunctionPersonalityFnWorklist.back().first->setPersonalityFn(C); | |||
2110 | else | |||
2111 | return error("Expected a constant"); | |||
2112 | } | |||
2113 | FunctionPersonalityFnWorklist.pop_back(); | |||
2114 | } | |||
2115 | ||||
2116 | return Error::success(); | |||
2117 | } | |||
2118 | ||||
2119 | static APInt readWideAPInt(ArrayRef<uint64_t> Vals, unsigned TypeBits) { | |||
2120 | SmallVector<uint64_t, 8> Words(Vals.size()); | |||
2121 | transform(Vals, Words.begin(), | |||
2122 | BitcodeReader::decodeSignRotatedValue); | |||
2123 | ||||
2124 | return APInt(TypeBits, Words); | |||
2125 | } | |||
2126 | ||||
2127 | Error BitcodeReader::parseConstants() { | |||
2128 | if (Stream.EnterSubBlock(bitc::CONSTANTS_BLOCK_ID)) | |||
2129 | return error("Invalid record"); | |||
2130 | ||||
2131 | SmallVector<uint64_t, 64> Record; | |||
2132 | ||||
2133 | // Read all the records for this value table. | |||
2134 | Type *CurTy = Type::getInt32Ty(Context); | |||
2135 | unsigned NextCstNo = ValueList.size(); | |||
2136 | ||||
2137 | while (true) { | |||
2138 | BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); | |||
2139 | ||||
2140 | switch (Entry.Kind) { | |||
2141 | case BitstreamEntry::SubBlock: // Handled for us already. | |||
2142 | case BitstreamEntry::Error: | |||
2143 | return error("Malformed block"); | |||
2144 | case BitstreamEntry::EndBlock: | |||
2145 | if (NextCstNo != ValueList.size()) | |||
2146 | return error("Invalid constant reference"); | |||
2147 | ||||
2148 | // Once all the constants have been read, go through and resolve forward | |||
2149 | // references. | |||
2150 | ValueList.resolveConstantForwardRefs(); | |||
2151 | return Error::success(); | |||
2152 | case BitstreamEntry::Record: | |||
2153 | // The interesting case. | |||
2154 | break; | |||
2155 | } | |||
2156 | ||||
2157 | // Read a record. | |||
2158 | Record.clear(); | |||
2159 | Type *VoidType = Type::getVoidTy(Context); | |||
2160 | Value *V = nullptr; | |||
2161 | unsigned BitCode = Stream.readRecord(Entry.ID, Record); | |||
2162 | switch (BitCode) { | |||
2163 | default: // Default behavior: unknown constant | |||
2164 | case bitc::CST_CODE_UNDEF: // UNDEF | |||
2165 | V = UndefValue::get(CurTy); | |||
2166 | break; | |||
2167 | case bitc::CST_CODE_SETTYPE: // SETTYPE: [typeid] | |||
2168 | if (Record.empty()) | |||
2169 | return error("Invalid record"); | |||
2170 | if (Record[0] >= TypeList.size() || !TypeList[Record[0]]) | |||
2171 | return error("Invalid record"); | |||
2172 | if (TypeList[Record[0]] == VoidType) | |||
2173 | return error("Invalid constant type"); | |||
2174 | CurTy = TypeList[Record[0]]; | |||
2175 | continue; // Skip the ValueList manipulation. | |||
2176 | case bitc::CST_CODE_NULL: // NULL | |||
2177 | V = Constant::getNullValue(CurTy); | |||
2178 | break; | |||
2179 | case bitc::CST_CODE_INTEGER: // INTEGER: [intval] | |||
2180 | if (!CurTy->isIntegerTy() || Record.empty()) | |||
2181 | return error("Invalid record"); | |||
2182 | V = ConstantInt::get(CurTy, decodeSignRotatedValue(Record[0])); | |||
2183 | break; | |||
2184 | case bitc::CST_CODE_WIDE_INTEGER: {// WIDE_INTEGER: [n x intval] | |||
2185 | if (!CurTy->isIntegerTy() || Record.empty()) | |||
2186 | return error("Invalid record"); | |||
2187 | ||||
2188 | APInt VInt = | |||
2189 | readWideAPInt(Record, cast<IntegerType>(CurTy)->getBitWidth()); | |||
2190 | V = ConstantInt::get(Context, VInt); | |||
2191 | ||||
2192 | break; | |||
2193 | } | |||
2194 | case bitc::CST_CODE_FLOAT: { // FLOAT: [fpval] | |||
2195 | if (Record.empty()) | |||
2196 | return error("Invalid record"); | |||
2197 | if (CurTy->isHalfTy()) | |||
2198 | V = ConstantFP::get(Context, APFloat(APFloat::IEEEhalf(), | |||
2199 | APInt(16, (uint16_t)Record[0]))); | |||
2200 | else if (CurTy->isFloatTy()) | |||
2201 | V = ConstantFP::get(Context, APFloat(APFloat::IEEEsingle(), | |||
2202 | APInt(32, (uint32_t)Record[0]))); | |||
2203 | else if (CurTy->isDoubleTy()) | |||
2204 | V = ConstantFP::get(Context, APFloat(APFloat::IEEEdouble(), | |||
2205 | APInt(64, Record[0]))); | |||
2206 | else if (CurTy->isX86_FP80Ty()) { | |||
2207 | // Bits are not stored the same way as a normal i80 APInt, compensate. | |||
2208 | uint64_t Rearrange[2]; | |||
2209 | Rearrange[0] = (Record[1] & 0xffffLL) | (Record[0] << 16); | |||
2210 | Rearrange[1] = Record[0] >> 48; | |||
2211 | V = ConstantFP::get(Context, APFloat(APFloat::x87DoubleExtended(), | |||
2212 | APInt(80, Rearrange))); | |||
2213 | } else if (CurTy->isFP128Ty()) | |||
2214 | V = ConstantFP::get(Context, APFloat(APFloat::IEEEquad(), | |||
2215 | APInt(128, Record))); | |||
2216 | else if (CurTy->isPPC_FP128Ty()) | |||
2217 | V = ConstantFP::get(Context, APFloat(APFloat::PPCDoubleDouble(), | |||
2218 | APInt(128, Record))); | |||
2219 | else | |||
2220 | V = UndefValue::get(CurTy); | |||
2221 | break; | |||
2222 | } | |||
2223 | ||||
2224 | case bitc::CST_CODE_AGGREGATE: {// AGGREGATE: [n x value number] | |||
2225 | if (Record.empty()) | |||
2226 | return error("Invalid record"); | |||
2227 | ||||
2228 | unsigned Size = Record.size(); | |||
2229 | SmallVector<Constant*, 16> Elts; | |||
2230 | ||||
2231 | if (StructType *STy = dyn_cast<StructType>(CurTy)) { | |||
2232 | for (unsigned i = 0; i != Size; ++i) | |||
2233 | Elts.push_back(ValueList.getConstantFwdRef(Record[i], | |||
2234 | STy->getElementType(i))); | |||
2235 | V = ConstantStruct::get(STy, Elts); | |||
2236 | } else if (ArrayType *ATy = dyn_cast<ArrayType>(CurTy)) { | |||
2237 | Type *EltTy = ATy->getElementType(); | |||
2238 | for (unsigned i = 0; i != Size; ++i) | |||
2239 | Elts.push_back(ValueList.getConstantFwdRef(Record[i], EltTy)); | |||
2240 | V = ConstantArray::get(ATy, Elts); | |||
2241 | } else if (VectorType *VTy = dyn_cast<VectorType>(CurTy)) { | |||
2242 | Type *EltTy = VTy->getElementType(); | |||
2243 | for (unsigned i = 0; i != Size; ++i) | |||
2244 | Elts.push_back(ValueList.getConstantFwdRef(Record[i], EltTy)); | |||
2245 | V = ConstantVector::get(Elts); | |||
2246 | } else { | |||
2247 | V = UndefValue::get(CurTy); | |||
2248 | } | |||
2249 | break; | |||
2250 | } | |||
2251 | case bitc::CST_CODE_STRING: // STRING: [values] | |||
2252 | case bitc::CST_CODE_CSTRING: { // CSTRING: [values] | |||
2253 | if (Record.empty()) | |||
2254 | return error("Invalid record"); | |||
2255 | ||||
2256 | SmallString<16> Elts(Record.begin(), Record.end()); | |||
2257 | V = ConstantDataArray::getString(Context, Elts, | |||
2258 | BitCode == bitc::CST_CODE_CSTRING); | |||
2259 | break; | |||
2260 | } | |||
2261 | case bitc::CST_CODE_DATA: {// DATA: [n x value] | |||
2262 | if (Record.empty()) | |||
2263 | return error("Invalid record"); | |||
2264 | ||||
2265 | Type *EltTy = cast<SequentialType>(CurTy)->getElementType(); | |||
2266 | if (EltTy->isIntegerTy(8)) { | |||
2267 | SmallVector<uint8_t, 16> Elts(Record.begin(), Record.end()); | |||
2268 | if (isa<VectorType>(CurTy)) | |||
2269 | V = ConstantDataVector::get(Context, Elts); | |||
2270 | else | |||
2271 | V = ConstantDataArray::get(Context, Elts); | |||
2272 | } else if (EltTy->isIntegerTy(16)) { | |||
2273 | SmallVector<uint16_t, 16> Elts(Record.begin(), Record.end()); | |||
2274 | if (isa<VectorType>(CurTy)) | |||
2275 | V = ConstantDataVector::get(Context, Elts); | |||
2276 | else | |||
2277 | V = ConstantDataArray::get(Context, Elts); | |||
2278 | } else if (EltTy->isIntegerTy(32)) { | |||
2279 | SmallVector<uint32_t, 16> Elts(Record.begin(), Record.end()); | |||
2280 | if (isa<VectorType>(CurTy)) | |||
2281 | V = ConstantDataVector::get(Context, Elts); | |||
2282 | else | |||
2283 | V = ConstantDataArray::get(Context, Elts); | |||
2284 | } else if (EltTy->isIntegerTy(64)) { | |||
2285 | SmallVector<uint64_t, 16> Elts(Record.begin(), Record.end()); | |||
2286 | if (isa<VectorType>(CurTy)) | |||
2287 | V = ConstantDataVector::get(Context, Elts); | |||
2288 | else | |||
2289 | V = ConstantDataArray::get(Context, Elts); | |||
2290 | } else if (EltTy->isHalfTy()) { | |||
2291 | SmallVector<uint16_t, 16> Elts(Record.begin(), Record.end()); | |||
2292 | if (isa<VectorType>(CurTy)) | |||
2293 | V = ConstantDataVector::getFP(Context, Elts); | |||
2294 | else | |||
2295 | V = ConstantDataArray::getFP(Context, Elts); | |||
2296 | } else if (EltTy->isFloatTy()) { | |||
2297 | SmallVector<uint32_t, 16> Elts(Record.begin(), Record.end()); | |||
2298 | if (isa<VectorType>(CurTy)) | |||
2299 | V = ConstantDataVector::getFP(Context, Elts); | |||
2300 | else | |||
2301 | V = ConstantDataArray::getFP(Context, Elts); | |||
2302 | } else if (EltTy->isDoubleTy()) { | |||
2303 | SmallVector<uint64_t, 16> Elts(Record.begin(), Record.end()); | |||
2304 | if (isa<VectorType>(CurTy)) | |||
2305 | V = ConstantDataVector::getFP(Context, Elts); | |||
2306 | else | |||
2307 | V = ConstantDataArray::getFP(Context, Elts); | |||
2308 | } else { | |||
2309 | return error("Invalid type for value"); | |||
2310 | } | |||
2311 | break; | |||
2312 | } | |||
2313 | case bitc::CST_CODE_CE_BINOP: { // CE_BINOP: [opcode, opval, opval] | |||
2314 | if (Record.size() < 3) | |||
2315 | return error("Invalid record"); | |||
2316 | int Opc = getDecodedBinaryOpcode(Record[0], CurTy); | |||
2317 | if (Opc < 0) { | |||
2318 | V = UndefValue::get(CurTy); // Unknown binop. | |||
2319 | } else { | |||
2320 | Constant *LHS = ValueList.getConstantFwdRef(Record[1], CurTy); | |||
2321 | Constant *RHS = ValueList.getConstantFwdRef(Record[2], CurTy); | |||
2322 | unsigned Flags = 0; | |||
2323 | if (Record.size() >= 4) { | |||
2324 | if (Opc == Instruction::Add || | |||
2325 | Opc == Instruction::Sub || | |||
2326 | Opc == Instruction::Mul || | |||
2327 | Opc == Instruction::Shl) { | |||
2328 | if (Record[3] & (1 << bitc::OBO_NO_SIGNED_WRAP)) | |||
2329 | Flags |= OverflowingBinaryOperator::NoSignedWrap; | |||
2330 | if (Record[3] & (1 << bitc::OBO_NO_UNSIGNED_WRAP)) | |||
2331 | Flags |= OverflowingBinaryOperator::NoUnsignedWrap; | |||
2332 | } else if (Opc == Instruction::SDiv || | |||
2333 | Opc == Instruction::UDiv || | |||
2334 | Opc == Instruction::LShr || | |||
2335 | Opc == Instruction::AShr) { | |||
2336 | if (Record[3] & (1 << bitc::PEO_EXACT)) | |||
2337 | Flags |= SDivOperator::IsExact; | |||
2338 | } | |||
2339 | } | |||
2340 | V = ConstantExpr::get(Opc, LHS, RHS, Flags); | |||
2341 | } | |||
2342 | break; | |||
2343 | } | |||
2344 | case bitc::CST_CODE_CE_CAST: { // CE_CAST: [opcode, opty, opval] | |||
2345 | if (Record.size() < 3) | |||
2346 | return error("Invalid record"); | |||
2347 | int Opc = getDecodedCastOpcode(Record[0]); | |||
2348 | if (Opc < 0) { | |||
2349 | V = UndefValue::get(CurTy); // Unknown cast. | |||
2350 | } else { | |||
2351 | Type *OpTy = getTypeByID(Record[1]); | |||
2352 | if (!OpTy) | |||
2353 | return error("Invalid record"); | |||
2354 | Constant *Op = ValueList.getConstantFwdRef(Record[2], OpTy); | |||
2355 | V = UpgradeBitCastExpr(Opc, Op, CurTy); | |||
2356 | if (!V) V = ConstantExpr::getCast(Opc, Op, CurTy); | |||
2357 | } | |||
2358 | break; | |||
2359 | } | |||
2360 | case bitc::CST_CODE_CE_INBOUNDS_GEP: // [ty, n x operands] | |||
2361 | case bitc::CST_CODE_CE_GEP: // [ty, n x operands] | |||
2362 | case bitc::CST_CODE_CE_GEP_WITH_INRANGE_INDEX: { // [ty, flags, n x | |||
2363 | // operands] | |||
2364 | unsigned OpNum = 0; | |||
2365 | Type *PointeeType = nullptr; | |||
2366 | if (BitCode == bitc::CST_CODE_CE_GEP_WITH_INRANGE_INDEX || | |||
2367 | Record.size() % 2) | |||
2368 | PointeeType = getTypeByID(Record[OpNum++]); | |||
2369 | ||||
2370 | bool InBounds = false; | |||
2371 | Optional<unsigned> InRangeIndex; | |||
2372 | if (BitCode == bitc::CST_CODE_CE_GEP_WITH_INRANGE_INDEX) { | |||
2373 | uint64_t Op = Record[OpNum++]; | |||
2374 | InBounds = Op & 1; | |||
2375 | InRangeIndex = Op >> 1; | |||
2376 | } else if (BitCode == bitc::CST_CODE_CE_INBOUNDS_GEP) | |||
2377 | InBounds = true; | |||
2378 | ||||
2379 | SmallVector<Constant*, 16> Elts; | |||
2380 | while (OpNum != Record.size()) { | |||
2381 | Type *ElTy = getTypeByID(Record[OpNum++]); | |||
2382 | if (!ElTy) | |||
2383 | return error("Invalid record"); | |||
2384 | Elts.push_back(ValueList.getConstantFwdRef(Record[OpNum++], ElTy)); | |||
2385 | } | |||
2386 | ||||
2387 | if (PointeeType && | |||
2388 | PointeeType != | |||
2389 | cast<PointerType>(Elts[0]->getType()->getScalarType()) | |||
2390 | ->getElementType()) | |||
2391 | return error("Explicit gep operator type does not match pointee type " | |||
2392 | "of pointer operand"); | |||
2393 | ||||
2394 | if (Elts.size() < 1) | |||
2395 | return error("Invalid gep with no operands"); | |||
2396 | ||||
2397 | ArrayRef<Constant *> Indices(Elts.begin() + 1, Elts.end()); | |||
2398 | V = ConstantExpr::getGetElementPtr(PointeeType, Elts[0], Indices, | |||
2399 | InBounds, InRangeIndex); | |||
2400 | break; | |||
2401 | } | |||
2402 | case bitc::CST_CODE_CE_SELECT: { // CE_SELECT: [opval#, opval#, opval#] | |||
2403 | if (Record.size() < 3) | |||
2404 | return error("Invalid record"); | |||
2405 | ||||
2406 | Type *SelectorTy = Type::getInt1Ty(Context); | |||
2407 | ||||
2408 | // The selector might be an i1 or an <n x i1> | |||
2409 | // Get the type from the ValueList before getting a forward ref. | |||
2410 | if (VectorType *VTy = dyn_cast<VectorType>(CurTy)) | |||
2411 | if (Value *V = ValueList[Record[0]]) | |||
2412 | if (SelectorTy != V->getType()) | |||
2413 | SelectorTy = VectorType::get(SelectorTy, VTy->getNumElements()); | |||
2414 | ||||
2415 | V = ConstantExpr::getSelect(ValueList.getConstantFwdRef(Record[0], | |||
2416 | SelectorTy), | |||
2417 | ValueList.getConstantFwdRef(Record[1],CurTy), | |||
2418 | ValueList.getConstantFwdRef(Record[2],CurTy)); | |||
2419 | break; | |||
2420 | } | |||
2421 | case bitc::CST_CODE_CE_EXTRACTELT | |||
2422 | : { // CE_EXTRACTELT: [opty, opval, opty, opval] | |||
2423 | if (Record.size() < 3) | |||
2424 | return error("Invalid record"); | |||
2425 | VectorType *OpTy = | |||
2426 | dyn_cast_or_null<VectorType>(getTypeByID(Record[0])); | |||
2427 | if (!OpTy) | |||
2428 | return error("Invalid record"); | |||
2429 | Constant *Op0 = ValueList.getConstantFwdRef(Record[1], OpTy); | |||
2430 | Constant *Op1 = nullptr; | |||
2431 | if (Record.size() == 4) { | |||
2432 | Type *IdxTy = getTypeByID(Record[2]); | |||
2433 | if (!IdxTy) | |||
2434 | return error("Invalid record"); | |||
2435 | Op1 = ValueList.getConstantFwdRef(Record[3], IdxTy); | |||
2436 | } else // TODO: Remove with llvm 4.0 | |||
2437 | Op1 = ValueList.getConstantFwdRef(Record[2], Type::getInt32Ty(Context)); | |||
2438 | if (!Op1) | |||
2439 | return error("Invalid record"); | |||
2440 | V = ConstantExpr::getExtractElement(Op0, Op1); | |||
2441 | break; | |||
2442 | } | |||
2443 | case bitc::CST_CODE_CE_INSERTELT | |||
2444 | : { // CE_INSERTELT: [opval, opval, opty, opval] | |||
2445 | VectorType *OpTy = dyn_cast<VectorType>(CurTy); | |||
2446 | if (Record.size() < 3 || !OpTy) | |||
2447 | return error("Invalid record"); | |||
2448 | Constant *Op0 = ValueList.getConstantFwdRef(Record[0], OpTy); | |||
2449 | Constant *Op1 = ValueList.getConstantFwdRef(Record[1], | |||
2450 | OpTy->getElementType()); | |||
2451 | Constant *Op2 = nullptr; | |||
2452 | if (Record.size() == 4) { | |||
2453 | Type *IdxTy = getTypeByID(Record[2]); | |||
2454 | if (!IdxTy) | |||
2455 | return error("Invalid record"); | |||
2456 | Op2 = ValueList.getConstantFwdRef(Record[3], IdxTy); | |||
2457 | } else // TODO: Remove with llvm 4.0 | |||
2458 | Op2 = ValueList.getConstantFwdRef(Record[2], Type::getInt32Ty(Context)); | |||
2459 | if (!Op2) | |||
2460 | return error("Invalid record"); | |||
2461 | V = ConstantExpr::getInsertElement(Op0, Op1, Op2); | |||
2462 | break; | |||
2463 | } | |||
2464 | case bitc::CST_CODE_CE_SHUFFLEVEC: { // CE_SHUFFLEVEC: [opval, opval, opval] | |||
2465 | VectorType *OpTy = dyn_cast<VectorType>(CurTy); | |||
2466 | if (Record.size() < 3 || !OpTy) | |||
2467 | return error("Invalid record"); | |||
2468 | Constant *Op0 = ValueList.getConstantFwdRef(Record[0], OpTy); | |||
2469 | Constant *Op1 = ValueList.getConstantFwdRef(Record[1], OpTy); | |||
2470 | Type *ShufTy = VectorType::get(Type::getInt32Ty(Context), | |||
2471 | OpTy->getNumElements()); | |||
2472 | Constant *Op2 = ValueList.getConstantFwdRef(Record[2], ShufTy); | |||
2473 | V = ConstantExpr::getShuffleVector(Op0, Op1, Op2); | |||
2474 | break; | |||
2475 | } | |||
2476 | case bitc::CST_CODE_CE_SHUFVEC_EX: { // [opty, opval, opval, opval] | |||
2477 | VectorType *RTy = dyn_cast<VectorType>(CurTy); | |||
2478 | VectorType *OpTy = | |||
2479 | dyn_cast_or_null<VectorType>(getTypeByID(Record[0])); | |||
2480 | if (Record.size() < 4 || !RTy || !OpTy) | |||
2481 | return error("Invalid record"); | |||
2482 | Constant *Op0 = ValueList.getConstantFwdRef(Record[1], OpTy); | |||
2483 | Constant *Op1 = ValueList.getConstantFwdRef(Record[2], OpTy); | |||
2484 | Type *ShufTy = VectorType::get(Type::getInt32Ty(Context), | |||
2485 | RTy->getNumElements()); | |||
2486 | Constant *Op2 = ValueList.getConstantFwdRef(Record[3], ShufTy); | |||
2487 | V = ConstantExpr::getShuffleVector(Op0, Op1, Op2); | |||
2488 | break; | |||
2489 | } | |||
2490 | case bitc::CST_CODE_CE_CMP: { // CE_CMP: [opty, opval, opval, pred] | |||
2491 | if (Record.size() < 4) | |||
2492 | return error("Invalid record"); | |||
2493 | Type *OpTy = getTypeByID(Record[0]); | |||
2494 | if (!OpTy) | |||
2495 | return error("Invalid record"); | |||
2496 | Constant *Op0 = ValueList.getConstantFwdRef(Record[1], OpTy); | |||
2497 | Constant *Op1 = ValueList.getConstantFwdRef(Record[2], OpTy); | |||
2498 | ||||
2499 | if (OpTy->isFPOrFPVectorTy()) | |||
2500 | V = ConstantExpr::getFCmp(Record[3], Op0, Op1); | |||
2501 | else | |||
2502 | V = ConstantExpr::getICmp(Record[3], Op0, Op1); | |||
2503 | break; | |||
2504 | } | |||
2505 | // This maintains backward compatibility, pre-asm dialect keywords. | |||
2506 | // FIXME: Remove with the 4.0 release. | |||
2507 | case bitc::CST_CODE_INLINEASM_OLD: { | |||
2508 | if (Record.size() < 2) | |||
2509 | return error("Invalid record"); | |||
2510 | std::string AsmStr, ConstrStr; | |||
2511 | bool HasSideEffects = Record[0] & 1; | |||
2512 | bool IsAlignStack = Record[0] >> 1; | |||
2513 | unsigned AsmStrSize = Record[1]; | |||
2514 | if (2+AsmStrSize >= Record.size()) | |||
2515 | return error("Invalid record"); | |||
2516 | unsigned ConstStrSize = Record[2+AsmStrSize]; | |||
2517 | if (3+AsmStrSize+ConstStrSize > Record.size()) | |||
2518 | return error("Invalid record"); | |||
2519 | ||||
2520 | for (unsigned i = 0; i != AsmStrSize; ++i) | |||
2521 | AsmStr += (char)Record[2+i]; | |||
2522 | for (unsigned i = 0; i != ConstStrSize; ++i) | |||
2523 | ConstrStr += (char)Record[3+AsmStrSize+i]; | |||
2524 | PointerType *PTy = cast<PointerType>(CurTy); | |||
2525 | V = InlineAsm::get(cast<FunctionType>(PTy->getElementType()), | |||
2526 | AsmStr, ConstrStr, HasSideEffects, IsAlignStack); | |||
2527 | break; | |||
2528 | } | |||
2529 | // This version adds support for the asm dialect keywords (e.g., | |||
2530 | // inteldialect). | |||
2531 | case bitc::CST_CODE_INLINEASM: { | |||
2532 | if (Record.size() < 2) | |||
2533 | return error("Invalid record"); | |||
2534 | std::string AsmStr, ConstrStr; | |||
2535 | bool HasSideEffects = Record[0] & 1; | |||
2536 | bool IsAlignStack = (Record[0] >> 1) & 1; | |||
2537 | unsigned AsmDialect = Record[0] >> 2; | |||
2538 | unsigned AsmStrSize = Record[1]; | |||
2539 | if (2+AsmStrSize >= Record.size()) | |||
2540 | return error("Invalid record"); | |||
2541 | unsigned ConstStrSize = Record[2+AsmStrSize]; | |||
2542 | if (3+AsmStrSize+ConstStrSize > Record.size()) | |||
2543 | return error("Invalid record"); | |||
2544 | ||||
2545 | for (unsigned i = 0; i != AsmStrSize; ++i) | |||
2546 | AsmStr += (char)Record[2+i]; | |||
2547 | for (unsigned i = 0; i != ConstStrSize; ++i) | |||
2548 | ConstrStr += (char)Record[3+AsmStrSize+i]; | |||
2549 | PointerType *PTy = cast<PointerType>(CurTy); | |||
2550 | V = InlineAsm::get(cast<FunctionType>(PTy->getElementType()), | |||
2551 | AsmStr, ConstrStr, HasSideEffects, IsAlignStack, | |||
2552 | InlineAsm::AsmDialect(AsmDialect)); | |||
2553 | break; | |||
2554 | } | |||
2555 | case bitc::CST_CODE_BLOCKADDRESS:{ | |||
2556 | if (Record.size() < 3) | |||
2557 | return error("Invalid record"); | |||
2558 | Type *FnTy = getTypeByID(Record[0]); | |||
2559 | if (!FnTy) | |||
2560 | return error("Invalid record"); | |||
2561 | Function *Fn = | |||
2562 | dyn_cast_or_null<Function>(ValueList.getConstantFwdRef(Record[1],FnTy)); | |||
2563 | if (!Fn) | |||
2564 | return error("Invalid record"); | |||
2565 | ||||
2566 | // If the function is already parsed we can insert the block address right | |||
2567 | // away. | |||
2568 | BasicBlock *BB; | |||
2569 | unsigned BBID = Record[2]; | |||
2570 | if (!BBID) | |||
2571 | // Invalid reference to entry block. | |||
2572 | return error("Invalid ID"); | |||
2573 | if (!Fn->empty()) { | |||
2574 | Function::iterator BBI = Fn->begin(), BBE = Fn->end(); | |||
2575 | for (size_t I = 0, E = BBID; I != E; ++I) { | |||
2576 | if (BBI == BBE) | |||
2577 | return error("Invalid ID"); | |||
2578 | ++BBI; | |||
2579 | } | |||
2580 | BB = &*BBI; | |||
2581 | } else { | |||
2582 | // Otherwise insert a placeholder and remember it so it can be inserted | |||
2583 | // when the function is parsed. | |||
2584 | auto &FwdBBs = BasicBlockFwdRefs[Fn]; | |||
2585 | if (FwdBBs.empty()) | |||
2586 | BasicBlockFwdRefQueue.push_back(Fn); | |||
2587 | if (FwdBBs.size() < BBID + 1) | |||
2588 | FwdBBs.resize(BBID + 1); | |||
2589 | if (!FwdBBs[BBID]) | |||
2590 | FwdBBs[BBID] = BasicBlock::Create(Context); | |||
2591 | BB = FwdBBs[BBID]; | |||
2592 | } | |||
2593 | V = BlockAddress::get(Fn, BB); | |||
2594 | break; | |||
2595 | } | |||
2596 | } | |||
2597 | ||||
2598 | ValueList.assignValue(V, NextCstNo); | |||
2599 | ++NextCstNo; | |||
2600 | } | |||
2601 | } | |||
2602 | ||||
2603 | Error BitcodeReader::parseUseLists() { | |||
2604 | if (Stream.EnterSubBlock(bitc::USELIST_BLOCK_ID)) | |||
2605 | return error("Invalid record"); | |||
2606 | ||||
2607 | // Read all the records. | |||
2608 | SmallVector<uint64_t, 64> Record; | |||
2609 | ||||
2610 | while (true) { | |||
2611 | BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); | |||
2612 | ||||
2613 | switch (Entry.Kind) { | |||
2614 | case BitstreamEntry::SubBlock: // Handled for us already. | |||
2615 | case BitstreamEntry::Error: | |||
2616 | return error("Malformed block"); | |||
2617 | case BitstreamEntry::EndBlock: | |||
2618 | return Error::success(); | |||
2619 | case BitstreamEntry::Record: | |||
2620 | // The interesting case. | |||
2621 | break; | |||
2622 | } | |||
2623 | ||||
2624 | // Read a use list record. | |||
2625 | Record.clear(); | |||
2626 | bool IsBB = false; | |||
2627 | switch (Stream.readRecord(Entry.ID, Record)) { | |||
2628 | default: // Default behavior: unknown type. | |||
2629 | break; | |||
2630 | case bitc::USELIST_CODE_BB: | |||
2631 | IsBB = true; | |||
2632 | LLVM_FALLTHROUGH[[clang::fallthrough]]; | |||
2633 | case bitc::USELIST_CODE_DEFAULT: { | |||
2634 | unsigned RecordLength = Record.size(); | |||
2635 | if (RecordLength < 3) | |||
2636 | // Records should have at least an ID and two indexes. | |||
2637 | return error("Invalid record"); | |||
2638 | unsigned ID = Record.back(); | |||
2639 | Record.pop_back(); | |||
2640 | ||||
2641 | Value *V; | |||
2642 | if (IsBB) { | |||
2643 | assert(ID < FunctionBBs.size() && "Basic block not found")(static_cast <bool> (ID < FunctionBBs.size() && "Basic block not found") ? void (0) : __assert_fail ("ID < FunctionBBs.size() && \"Basic block not found\"" , "/build/llvm-toolchain-snapshot-7~svn329677/lib/Bitcode/Reader/BitcodeReader.cpp" , 2643, __extension__ __PRETTY_FUNCTION__)); | |||
2644 | V = FunctionBBs[ID]; | |||
2645 | } else | |||
2646 | V = ValueList[ID]; | |||
2647 | unsigned NumUses = 0; | |||
2648 | SmallDenseMap<const Use *, unsigned, 16> Order; | |||
2649 | for (const Use &U : V->materialized_uses()) { | |||
2650 | if (++NumUses > Record.size()) | |||
2651 | break; | |||
2652 | Order[&U] = Record[NumUses - 1]; | |||
2653 | } | |||
2654 | if (Order.size() != Record.size() || NumUses > Record.size()) | |||
2655 | // Mismatches can happen if the functions are being materialized lazily | |||
2656 | // (out-of-order), or a value has been upgraded. | |||
2657 | break; | |||
2658 | ||||
2659 | V->sortUseList([&](const Use &L, const Use &R) { | |||
2660 | return Order.lookup(&L) < Order.lookup(&R); | |||
2661 | }); | |||
2662 | break; | |||
2663 | } | |||
2664 | } | |||
2665 | } | |||
2666 | } | |||
2667 | ||||
2668 | /// When we see the block for metadata, remember where it is and then skip it. | |||
2669 | /// This lets us lazily deserialize the metadata. | |||
2670 | Error BitcodeReader::rememberAndSkipMetadata() { | |||
2671 | // Save the current stream state. | |||
2672 | uint64_t CurBit = Stream.GetCurrentBitNo(); | |||
2673 | DeferredMetadataInfo.push_back(CurBit); | |||
2674 | ||||
2675 | // Skip over the block for now. | |||
2676 | if (Stream.SkipBlock()) | |||
2677 | return error("Invalid record"); | |||
2678 | return Error::success(); | |||
2679 | } | |||
2680 | ||||
2681 | Error BitcodeReader::materializeMetadata() { | |||
2682 | for (uint64_t BitPos : DeferredMetadataInfo) { | |||
2683 | // Move the bit stream to the saved position. | |||
2684 | Stream.JumpToBit(BitPos); | |||
2685 | if (Error Err = MDLoader->parseModuleMetadata()) | |||
2686 | return Err; | |||
2687 | } | |||
2688 | ||||
2689 | // Upgrade "Linker Options" module flag to "llvm.linker.options" module-level | |||
2690 | // metadata. | |||
2691 | if (Metadata *Val = TheModule->getModuleFlag("Linker Options")) { | |||
2692 | NamedMDNode *LinkerOpts = | |||
2693 | TheModule->getOrInsertNamedMetadata("llvm.linker.options"); | |||
2694 | for (const MDOperand &MDOptions : cast<MDNode>(Val)->operands()) | |||
2695 | LinkerOpts->addOperand(cast<MDNode>(MDOptions)); | |||
2696 | } | |||
2697 | ||||
2698 | DeferredMetadataInfo.clear(); | |||
2699 | return Error::success(); | |||
2700 | } | |||
2701 | ||||
2702 | void BitcodeReader::setStripDebugInfo() { StripDebugInfo = true; } | |||
2703 | ||||
2704 | /// When we see the block for a function body, remember where it is and then | |||
2705 | /// skip it. This lets us lazily deserialize the functions. | |||
2706 | Error BitcodeReader::rememberAndSkipFunctionBody() { | |||
2707 | // Get the function we are talking about. | |||
2708 | if (FunctionsWithBodies.empty()) | |||
2709 | return error("Insufficient function protos"); | |||
2710 | ||||
2711 | Function *Fn = FunctionsWithBodies.back(); | |||
2712 | FunctionsWithBodies.pop_back(); | |||
2713 | ||||
2714 | // Save the current stream state. | |||
2715 | uint64_t CurBit = Stream.GetCurrentBitNo(); | |||
2716 | assert((static_cast <bool> ((DeferredFunctionInfo[Fn] == 0 || DeferredFunctionInfo [Fn] == CurBit) && "Mismatch between VST and scanned function offsets" ) ? void (0) : __assert_fail ("(DeferredFunctionInfo[Fn] == 0 || DeferredFunctionInfo[Fn] == CurBit) && \"Mismatch between VST and scanned function offsets\"" , "/build/llvm-toolchain-snapshot-7~svn329677/lib/Bitcode/Reader/BitcodeReader.cpp" , 2718, __extension__ __PRETTY_FUNCTION__)) | |||
2717 | (DeferredFunctionInfo[Fn] == 0 || DeferredFunctionInfo[Fn] == CurBit) &&(static_cast <bool> ((DeferredFunctionInfo[Fn] == 0 || DeferredFunctionInfo [Fn] == CurBit) && "Mismatch between VST and scanned function offsets" ) ? void (0) : __assert_fail ("(DeferredFunctionInfo[Fn] == 0 || DeferredFunctionInfo[Fn] == CurBit) && \"Mismatch between VST and scanned function offsets\"" , "/build/llvm-toolchain-snapshot-7~svn329677/lib/Bitcode/Reader/BitcodeReader.cpp" , 2718, __extension__ __PRETTY_FUNCTION__)) | |||
2718 | "Mismatch between VST and scanned function offsets")(static_cast <bool> ((DeferredFunctionInfo[Fn] == 0 || DeferredFunctionInfo [Fn] == CurBit) && "Mismatch between VST and scanned function offsets" ) ? void (0) : __assert_fail ("(DeferredFunctionInfo[Fn] == 0 || DeferredFunctionInfo[Fn] == CurBit) && \"Mismatch between VST and scanned function offsets\"" , "/build/llvm-toolchain-snapshot-7~svn329677/lib/Bitcode/Reader/BitcodeReader.cpp" , 2718, __extension__ __PRETTY_FUNCTION__)); | |||
2719 | DeferredFunctionInfo[Fn] = CurBit; | |||
2720 | ||||
2721 | // Skip over the function block for now. | |||
2722 | if (Stream.SkipBlock()) | |||
2723 | return error("Invalid record"); | |||
2724 | return Error::success(); | |||
2725 | } | |||
2726 | ||||
2727 | Error BitcodeReader::globalCleanup() { | |||
2728 | // Patch the initializers for globals and aliases up. | |||
2729 | if (Error Err = resolveGlobalAndIndirectSymbolInits()) | |||
2730 | return Err; | |||
2731 | if (!GlobalInits.empty() || !IndirectSymbolInits.empty()) | |||
2732 | return error("Malformed global initializer set"); | |||
2733 | ||||
2734 | // Look for intrinsic functions which need to be upgraded at some point | |||
2735 | for (Function &F : *TheModule) { | |||
2736 | MDLoader->upgradeDebugIntrinsics(F); | |||
2737 | Function *NewFn; | |||
2738 | if (UpgradeIntrinsicFunction(&F, NewFn)) | |||
2739 | UpgradedIntrinsics[&F] = NewFn; | |||
2740 | else if (auto Remangled = Intrinsic::remangleIntrinsicFunction(&F)) | |||
2741 | // Some types could be renamed during loading if several modules are | |||
2742 | // loaded in the same LLVMContext (LTO scenario). In this case we should | |||
2743 | // remangle intrinsics names as well. | |||
2744 | RemangledIntrinsics[&F] = Remangled.getValue(); | |||
2745 | } | |||
2746 | ||||
2747 | // Look for global variables which need to be renamed. | |||
2748 | for (GlobalVariable &GV : TheModule->globals()) | |||
2749 | UpgradeGlobalVariable(&GV); | |||
2750 | ||||
2751 | // Force deallocation of memory for these vectors to favor the client that | |||
2752 | // want lazy deserialization. | |||
2753 | std::vector<std::pair<GlobalVariable *, unsigned>>().swap(GlobalInits); | |||
2754 | std::vector<std::pair<GlobalIndirectSymbol *, unsigned>>().swap( | |||
2755 | IndirectSymbolInits); | |||
2756 | return Error::success(); | |||
2757 | } | |||
2758 | ||||
2759 | /// Support for lazy parsing of function bodies. This is required if we | |||
2760 | /// either have an old bitcode file without a VST forward declaration record, | |||
2761 | /// or if we have an anonymous function being materialized, since anonymous | |||
2762 | /// functions do not have a name and are therefore not in the VST. | |||
2763 | Error BitcodeReader::rememberAndSkipFunctionBodies() { | |||
2764 | Stream.JumpToBit(NextUnreadBit); | |||
2765 | ||||
2766 | if (Stream.AtEndOfStream()) | |||
2767 | return error("Could not find function in stream"); | |||
2768 | ||||
2769 | if (!SeenFirstFunctionBody) | |||
2770 | return error("Trying to materialize functions before seeing function blocks"); | |||
2771 | ||||
2772 | // An old bitcode file with the symbol table at the end would have | |||
2773 | // finished the parse greedily. | |||
2774 | assert(SeenValueSymbolTable)(static_cast <bool> (SeenValueSymbolTable) ? void (0) : __assert_fail ("SeenValueSymbolTable", "/build/llvm-toolchain-snapshot-7~svn329677/lib/Bitcode/Reader/BitcodeReader.cpp" , 2774, __extension__ __PRETTY_FUNCTION__)); | |||
2775 | ||||
2776 | SmallVector<uint64_t, 64> Record; | |||
2777 | ||||
2778 | while (true) { | |||
2779 | BitstreamEntry Entry = Stream.advance(); | |||
2780 | switch (Entry.Kind) { | |||
2781 | default: | |||
2782 | return error("Expect SubBlock"); | |||
2783 | case BitstreamEntry::SubBlock: | |||
2784 | switch (Entry.ID) { | |||
2785 | default: | |||
2786 | return error("Expect function block"); | |||
2787 | case bitc::FUNCTION_BLOCK_ID: | |||
2788 | if (Error Err = rememberAndSkipFunctionBody()) | |||
2789 | return Err; | |||
2790 | NextUnreadBit = Stream.GetCurrentBitNo(); | |||
2791 | return Error::success(); | |||
2792 | } | |||
2793 | } | |||
2794 | } | |||
2795 | } | |||
2796 | ||||
2797 | bool BitcodeReaderBase::readBlockInfo() { | |||
2798 | Optional<BitstreamBlockInfo> NewBlockInfo = Stream.ReadBlockInfoBlock(); | |||
2799 | if (!NewBlockInfo) | |||
2800 | return true; | |||
2801 | BlockInfo = std::move(*NewBlockInfo); | |||
2802 | return false; | |||
2803 | } | |||
2804 | ||||
2805 | Error BitcodeReader::parseComdatRecord(ArrayRef<uint64_t> Record) { | |||
2806 | // v1: [selection_kind, name] | |||
2807 | // v2: [strtab_offset, strtab_size, selection_kind] | |||
2808 | StringRef Name; | |||
2809 | std::tie(Name, Record) = readNameFromStrtab(Record); | |||
2810 | ||||
2811 | if (Record.empty()) | |||
2812 | return error("Invalid record"); | |||
2813 | Comdat::SelectionKind SK = getDecodedComdatSelectionKind(Record[0]); | |||
2814 | std::string OldFormatName; | |||
2815 | if (!UseStrtab) { | |||
2816 | if (Record.size() < 2) | |||
2817 | return error("Invalid record"); | |||
2818 | unsigned ComdatNameSize = Record[1]; | |||
2819 | OldFormatName.reserve(ComdatNameSize); | |||
2820 | for (unsigned i = 0; i != ComdatNameSize; ++i) | |||
2821 | OldFormatName += (char)Record[2 + i]; | |||
2822 | Name = OldFormatName; | |||
2823 | } | |||
2824 | Comdat *C = TheModule->getOrInsertComdat(Name); | |||
2825 | C->setSelectionKind(SK); | |||
2826 | ComdatList.push_back(C); | |||
2827 | return Error::success(); | |||
2828 | } | |||
2829 | ||||
2830 | Error BitcodeReader::parseGlobalVarRecord(ArrayRef<uint64_t> Record) { | |||
2831 | // v1: [pointer type, isconst, initid, linkage, alignment, section, | |||
2832 | // visibility, threadlocal, unnamed_addr, externally_initialized, | |||
2833 | // dllstorageclass, comdat, attributes, preemption specifier] (name in VST) | |||
2834 | // v2: [strtab_offset, strtab_size, v1] | |||
2835 | StringRef Name; | |||
2836 | std::tie(Name, Record) = readNameFromStrtab(Record); | |||
2837 | ||||
2838 | if (Record.size() < 6) | |||
2839 | return error("Invalid record"); | |||
2840 | Type *Ty = getTypeByID(Record[0]); | |||
2841 | if (!Ty) | |||
2842 | return error("Invalid record"); | |||
2843 | bool isConstant = Record[1] & 1; | |||
2844 | bool explicitType = Record[1] & 2; | |||
2845 | unsigned AddressSpace; | |||
2846 | if (explicitType) { | |||
2847 | AddressSpace = Record[1] >> 2; | |||
2848 | } else { | |||
2849 | if (!Ty->isPointerTy()) | |||
2850 | return error("Invalid type for value"); | |||
2851 | AddressSpace = cast<PointerType>(Ty)->getAddressSpace(); | |||
2852 | Ty = cast<PointerType>(Ty)->getElementType(); | |||
2853 | } | |||
2854 | ||||
2855 | uint64_t RawLinkage = Record[3]; | |||
2856 | GlobalValue::LinkageTypes Linkage = getDecodedLinkage(RawLinkage); | |||
2857 | unsigned Alignment; | |||
2858 | if (Error Err = parseAlignmentValue(Record[4], Alignment)) | |||
2859 | return Err; | |||
2860 | std::string Section; | |||
2861 | if (Record[5]) { | |||
2862 | if (Record[5] - 1 >= SectionTable.size()) | |||
2863 | return error("Invalid ID"); | |||
2864 | Section = SectionTable[Record[5] - 1]; | |||
2865 | } | |||
2866 | GlobalValue::VisibilityTypes Visibility = GlobalValue::DefaultVisibility; | |||
2867 | // Local linkage must have default visibility. | |||
2868 | if (Record.size() > 6 && !GlobalValue::isLocalLinkage(Linkage)) | |||
2869 | // FIXME: Change to an error if non-default in 4.0. | |||
2870 | Visibility = getDecodedVisibility(Record[6]); | |||
2871 | ||||
2872 | GlobalVariable::ThreadLocalMode TLM = GlobalVariable::NotThreadLocal; | |||
2873 | if (Record.size() > 7) | |||
2874 | TLM = getDecodedThreadLocalMode(Record[7]); | |||
2875 | ||||
2876 | GlobalValue::UnnamedAddr UnnamedAddr = GlobalValue::UnnamedAddr::None; | |||
2877 | if (Record.size() > 8) | |||
2878 | UnnamedAddr = getDecodedUnnamedAddrType(Record[8]); | |||
2879 | ||||
2880 | bool ExternallyInitialized = false; | |||
2881 | if (Record.size() > 9) | |||
2882 | ExternallyInitialized = Record[9]; | |||
2883 | ||||
2884 | GlobalVariable *NewGV = | |||
2885 | new GlobalVariable(*TheModule, Ty, isConstant, Linkage, nullptr, Name, | |||
2886 | nullptr, TLM, AddressSpace, ExternallyInitialized); | |||
2887 | NewGV->setAlignment(Alignment); | |||
2888 | if (!Section.empty()) | |||
2889 | NewGV->setSection(Section); | |||
2890 | NewGV->setVisibility(Visibility); | |||
2891 | NewGV->setUnnamedAddr(UnnamedAddr); | |||
2892 | ||||
2893 | if (Record.size() > 10) | |||
2894 | NewGV->setDLLStorageClass(getDecodedDLLStorageClass(Record[10])); | |||
2895 | else | |||
2896 | upgradeDLLImportExportLinkage(NewGV, RawLinkage); | |||
2897 | ||||
2898 | ValueList.push_back(NewGV); | |||
2899 | ||||
2900 | // Remember which value to use for the global initializer. | |||
2901 | if (unsigned InitID = Record[2]) | |||
2902 | GlobalInits.push_back(std::make_pair(NewGV, InitID - 1)); | |||
2903 | ||||
2904 | if (Record.size() > 11) { | |||
2905 | if (unsigned ComdatID = Record[11]) { | |||
2906 | if (ComdatID > ComdatList.size()) | |||
2907 | return error("Invalid global variable comdat ID"); | |||
2908 | NewGV->setComdat(ComdatList[ComdatID - 1]); | |||
2909 | } | |||
2910 | } else if (hasImplicitComdat(RawLinkage)) { | |||
2911 | NewGV->setComdat(reinterpret_cast<Comdat *>(1)); | |||
2912 | } | |||
2913 | ||||
2914 | if (Record.size() > 12) { | |||
2915 | auto AS = getAttributes(Record[12]).getFnAttributes(); | |||
2916 | NewGV->setAttributes(AS); | |||
2917 | } | |||
2918 | ||||
2919 | if (Record.size() > 13) { | |||
2920 | NewGV->setDSOLocal(getDecodedDSOLocal(Record[13])); | |||
2921 | } | |||
2922 | ||||
2923 | return Error::success(); | |||
2924 | } | |||
2925 | ||||
2926 | Error BitcodeReader::parseFunctionRecord(ArrayRef<uint64_t> Record) { | |||
2927 | // v1: [type, callingconv, isproto, linkage, paramattr, alignment, section, | |||
2928 | // visibility, gc, unnamed_addr, prologuedata, dllstorageclass, comdat, | |||
2929 | // prefixdata, personalityfn, preemption specifier] (name in VST) | |||
2930 | // v2: [strtab_offset, strtab_size, v1] | |||
2931 | StringRef Name; | |||
2932 | std::tie(Name, Record) = readNameFromStrtab(Record); | |||
2933 | ||||
2934 | if (Record.size() < 8) | |||
2935 | return error("Invalid record"); | |||
2936 | Type *Ty = getTypeByID(Record[0]); | |||
2937 | if (!Ty) | |||
2938 | return error("Invalid record"); | |||
2939 | if (auto *PTy = dyn_cast<PointerType>(Ty)) | |||
2940 | Ty = PTy->getElementType(); | |||
2941 | auto *FTy = dyn_cast<FunctionType>(Ty); | |||
2942 | if (!FTy) | |||
2943 | return error("Invalid type for value"); | |||
2944 | auto CC = static_cast<CallingConv::ID>(Record[1]); | |||
2945 | if (CC & ~CallingConv::MaxID) | |||
2946 | return error("Invalid calling convention ID"); | |||
2947 | ||||
2948 | Function *Func = | |||
2949 | Function::Create(FTy, GlobalValue::ExternalLinkage, Name, TheModule); | |||
2950 | ||||
2951 | Func->setCallingConv(CC); | |||
2952 | bool isProto = Record[2]; | |||
2953 | uint64_t RawLinkage = Record[3]; | |||
2954 | Func->setLinkage(getDecodedLinkage(RawLinkage)); | |||
2955 | Func->setAttributes(getAttributes(Record[4])); | |||
2956 | ||||
2957 | unsigned Alignment; | |||
2958 | if (Error Err = parseAlignmentValue(Record[5], Alignment)) | |||
2959 | return Err; | |||
2960 | Func->setAlignment(Alignment); | |||
2961 | if (Record[6]) { | |||
2962 | if (Record[6] - 1 >= SectionTable.size()) | |||
2963 | return error("Invalid ID"); | |||
2964 | Func->setSection(SectionTable[Record[6] - 1]); | |||
2965 | } | |||
2966 | // Local linkage must have default visibility. | |||
2967 | if (!Func->hasLocalLinkage()) | |||
2968 | // FIXME: Change to an error if non-default in 4.0. | |||
2969 | Func->setVisibility(getDecodedVisibility(Record[7])); | |||
2970 | if (Record.size() > 8 && Record[8]) { | |||
2971 | if (Record[8] - 1 >= GCTable.size()) | |||
2972 | return error("Invalid ID"); | |||
2973 | Func->setGC(GCTable[Record[8] - 1]); | |||
2974 | } | |||
2975 | GlobalValue::UnnamedAddr UnnamedAddr = GlobalValue::UnnamedAddr::None; | |||
2976 | if (Record.size() > 9) | |||
2977 | UnnamedAddr = getDecodedUnnamedAddrType(Record[9]); | |||
2978 | Func->setUnnamedAddr(UnnamedAddr); | |||
2979 | if (Record.size() > 10 && Record[10] != 0) | |||
2980 | FunctionPrologues.push_back(std::make_pair(Func, Record[10] - 1)); | |||
2981 | ||||
2982 | if (Record.size() > 11) | |||
2983 | Func->setDLLStorageClass(getDecodedDLLStorageClass(Record[11])); | |||
2984 | else | |||
2985 | upgradeDLLImportExportLinkage(Func, RawLinkage); | |||
2986 | ||||
2987 | if (Record.size() > 12) { | |||
2988 | if (unsigned ComdatID = Record[12]) { | |||
2989 | if (ComdatID > ComdatList.size()) | |||
2990 | return error("Invalid function comdat ID"); | |||
2991 | Func->setComdat(ComdatList[ComdatID - 1]); | |||
2992 | } | |||
2993 | } else if (hasImplicitComdat(RawLinkage)) { | |||
2994 | Func->setComdat(reinterpret_cast<Comdat *>(1)); | |||
2995 | } | |||
2996 | ||||
2997 | if (Record.size() > 13 && Record[13] != 0) | |||
2998 | FunctionPrefixes.push_back(std::make_pair(Func, Record[13] - 1)); | |||
2999 | ||||
3000 | if (Record.size() > 14 && Record[14] != 0) | |||
3001 | FunctionPersonalityFns.push_back(std::make_pair(Func, Record[14] - 1)); | |||
3002 | ||||
3003 | if (Record.size() > 15) { | |||
3004 | Func->setDSOLocal(getDecodedDSOLocal(Record[15])); | |||
3005 | } | |||
3006 | ||||
3007 | ValueList.push_back(Func); | |||
3008 | ||||
3009 | // If this is a function with a body, remember the prototype we are | |||
3010 | // creating now, so that we can match up the body with them later. | |||
3011 | if (!isProto) { | |||
3012 | Func->setIsMaterializable(true); | |||
3013 | FunctionsWithBodies.push_back(Func); | |||
3014 | DeferredFunctionInfo[Func] = 0; | |||
3015 | } | |||
3016 | return Error::success(); | |||
3017 | } | |||
3018 | ||||
3019 | Error BitcodeReader::parseGlobalIndirectSymbolRecord( | |||
3020 | unsigned BitCode, ArrayRef<uint64_t> Record) { | |||
3021 | // v1 ALIAS_OLD: [alias type, aliasee val#, linkage] (name in VST) | |||
3022 | // v1 ALIAS: [alias type, addrspace, aliasee val#, linkage, visibility, | |||
3023 | // dllstorageclass, threadlocal, unnamed_addr, | |||
3024 | // preemption specifier] (name in VST) | |||
3025 | // v1 IFUNC: [alias type, addrspace, aliasee val#, linkage, | |||
3026 | // visibility, dllstorageclass, threadlocal, unnamed_addr, | |||
3027 | // preemption specifier] (name in VST) | |||
3028 | // v2: [strtab_offset, strtab_size, v1] | |||
3029 | StringRef Name; | |||
3030 | std::tie(Name, Record) = readNameFromStrtab(Record); | |||
3031 | ||||
3032 | bool NewRecord = BitCode != bitc::MODULE_CODE_ALIAS_OLD; | |||
3033 | if (Record.size() < (3 + (unsigned)NewRecord)) | |||
3034 | return error("Invalid record"); | |||
3035 | unsigned OpNum = 0; | |||
3036 | Type *Ty = getTypeByID(Record[OpNum++]); | |||
3037 | if (!Ty) | |||
3038 | return error("Invalid record"); | |||
3039 | ||||
3040 | unsigned AddrSpace; | |||
3041 | if (!NewRecord) { | |||
3042 | auto *PTy = dyn_cast<PointerType>(Ty); | |||
3043 | if (!PTy) | |||
3044 | return error("Invalid type for value"); | |||
3045 | Ty = PTy->getElementType(); | |||
3046 | AddrSpace = PTy->getAddressSpace(); | |||
3047 | } else { | |||
3048 | AddrSpace = Record[OpNum++]; | |||
3049 | } | |||
3050 | ||||
3051 | auto Val = Record[OpNum++]; | |||
3052 | auto Linkage = Record[OpNum++]; | |||
3053 | GlobalIndirectSymbol *NewGA; | |||
3054 | if (BitCode == bitc::MODULE_CODE_ALIAS || | |||
3055 | BitCode == bitc::MODULE_CODE_ALIAS_OLD) | |||
3056 | NewGA = GlobalAlias::create(Ty, AddrSpace, getDecodedLinkage(Linkage), Name, | |||
3057 | TheModule); | |||
3058 | else | |||
3059 | NewGA = GlobalIFunc::create(Ty, AddrSpace, getDecodedLinkage(Linkage), Name, | |||
3060 | nullptr, TheModule); | |||
3061 | // Old bitcode files didn't have visibility field. | |||
3062 | // Local linkage must have default visibility. | |||
3063 | if (OpNum != Record.size()) { | |||
3064 | auto VisInd = OpNum++; | |||
3065 | if (!NewGA->hasLocalLinkage()) | |||
3066 | // FIXME: Change to an error if non-default in 4.0. | |||
3067 | NewGA->setVisibility(getDecodedVisibility(Record[VisInd])); | |||
3068 | } | |||
3069 | if (BitCode == bitc::MODULE_CODE_ALIAS || | |||
3070 | BitCode == bitc::MODULE_CODE_ALIAS_OLD) { | |||
3071 | if (OpNum != Record.size()) | |||
3072 | NewGA->setDLLStorageClass(getDecodedDLLStorageClass(Record[OpNum++])); | |||
3073 | else | |||
3074 | upgradeDLLImportExportLinkage(NewGA, Linkage); | |||
3075 | if (OpNum != Record.size()) | |||
3076 | NewGA->setThreadLocalMode(getDecodedThreadLocalMode(Record[OpNum++])); | |||
3077 | if (OpNum != Record.size()) | |||
3078 | NewGA->setUnnamedAddr(getDecodedUnnamedAddrType(Record[OpNum++])); | |||
3079 | } | |||
3080 | if (OpNum != Record.size()) | |||
3081 | NewGA->setDSOLocal(getDecodedDSOLocal(Record[OpNum++])); | |||
3082 | ValueList.push_back(NewGA); | |||
3083 | IndirectSymbolInits.push_back(std::make_pair(NewGA, Val)); | |||
3084 | return Error::success(); | |||
3085 | } | |||
3086 | ||||
3087 | Error BitcodeReader::parseModule(uint64_t ResumeBit, | |||
3088 | bool ShouldLazyLoadMetadata) { | |||
3089 | if (ResumeBit) | |||
3090 | Stream.JumpToBit(ResumeBit); | |||
3091 | else if (Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID)) | |||
3092 | return error("Invalid record"); | |||
3093 | ||||
3094 | SmallVector<uint64_t, 64> Record; | |||
3095 | ||||
3096 | // Read all the records for this module. | |||
3097 | while (true) { | |||
3098 | BitstreamEntry Entry = Stream.advance(); | |||
3099 | ||||
3100 | switch (Entry.Kind) { | |||
3101 | case BitstreamEntry::Error: | |||
3102 | return error("Malformed block"); | |||
3103 | case BitstreamEntry::EndBlock: | |||
3104 | return globalCleanup(); | |||
3105 | ||||
3106 | case BitstreamEntry::SubBlock: | |||
3107 | switch (Entry.ID) { | |||
3108 | default: // Skip unknown content. | |||
3109 | if (Stream.SkipBlock()) | |||
3110 | return error("Invalid record"); | |||
3111 | break; | |||
3112 | case bitc::BLOCKINFO_BLOCK_ID: | |||
3113 | if (readBlockInfo()) | |||
3114 | return error("Malformed block"); | |||
3115 | break; | |||
3116 | case bitc::PARAMATTR_BLOCK_ID: | |||
3117 | if (Error Err = parseAttributeBlock()) | |||
3118 | return Err; | |||
3119 | break; | |||
3120 | case bitc::PARAMATTR_GROUP_BLOCK_ID: | |||
3121 | if (Error Err = parseAttributeGroupBlock()) | |||
3122 | return Err; | |||
3123 | break; | |||
3124 | case bitc::TYPE_BLOCK_ID_NEW: | |||
3125 | if (Error Err = parseTypeTable()) | |||
3126 | return Err; | |||
3127 | break; | |||
3128 | case bitc::VALUE_SYMTAB_BLOCK_ID: | |||
3129 | if (!SeenValueSymbolTable) { | |||
3130 | // Either this is an old form VST without function index and an | |||
3131 | // associated VST forward declaration record (which would have caused | |||
3132 | // the VST to be jumped to and parsed before it was encountered | |||
3133 | // normally in the stream), or there were no function blocks to | |||
3134 | // trigger an earlier parsing of the VST. | |||
3135 | assert(VSTOffset == 0 || FunctionsWithBodies.empty())(static_cast <bool> (VSTOffset == 0 || FunctionsWithBodies .empty()) ? void (0) : __assert_fail ("VSTOffset == 0 || FunctionsWithBodies.empty()" , "/build/llvm-toolchain-snapshot-7~svn329677/lib/Bitcode/Reader/BitcodeReader.cpp" , 3135, __extension__ __PRETTY_FUNCTION__)); | |||
3136 | if (Error Err = parseValueSymbolTable()) | |||
3137 | return Err; | |||
3138 | SeenValueSymbolTable = true; | |||
3139 | } else { | |||
3140 | // We must have had a VST forward declaration record, which caused | |||
3141 | // the parser to jump to and parse the VST earlier. | |||
3142 | assert(VSTOffset > 0)(static_cast <bool> (VSTOffset > 0) ? void (0) : __assert_fail ("VSTOffset > 0", "/build/llvm-toolchain-snapshot-7~svn329677/lib/Bitcode/Reader/BitcodeReader.cpp" , 3142, __extension__ __PRETTY_FUNCTION__)); | |||
3143 | if (Stream.SkipBlock()) | |||
3144 | return error("Invalid record"); | |||
3145 | } | |||
3146 | break; | |||
3147 | case bitc::CONSTANTS_BLOCK_ID: | |||
3148 | if (Error Err = parseConstants()) | |||
3149 | return Err; | |||
3150 | if (Error Err = resolveGlobalAndIndirectSymbolInits()) | |||
3151 | return Err; | |||
3152 | break; | |||
3153 | case bitc::METADATA_BLOCK_ID: | |||
3154 | if (ShouldLazyLoadMetadata) { | |||
3155 | if (Error Err = rememberAndSkipMetadata()) | |||
3156 | return Err; | |||
3157 | break; | |||
3158 | } | |||
3159 | assert(DeferredMetadataInfo.empty() && "Unexpected deferred metadata")(static_cast <bool> (DeferredMetadataInfo.empty() && "Unexpected deferred metadata") ? void (0) : __assert_fail ( "DeferredMetadataInfo.empty() && \"Unexpected deferred metadata\"" , "/build/llvm-toolchain-snapshot-7~svn329677/lib/Bitcode/Reader/BitcodeReader.cpp" , 3159, __extension__ __PRETTY_FUNCTION__)); | |||
3160 | if (Error Err = MDLoader->parseModuleMetadata()) | |||
3161 | return Err; | |||
3162 | break; | |||
3163 | case bitc::METADATA_KIND_BLOCK_ID: | |||
3164 | if (Error Err = MDLoader->parseMetadataKinds()) | |||
3165 | return Err; | |||
3166 | break; | |||
3167 | case bitc::FUNCTION_BLOCK_ID: | |||
3168 | // If this is the first function body we've seen, reverse the | |||
3169 | // FunctionsWithBodies list. | |||
3170 | if (!SeenFirstFunctionBody) { | |||
3171 | std::reverse(FunctionsWithBodies.begin(), FunctionsWithBodies.end()); | |||
3172 | if (Error Err = globalCleanup()) | |||
3173 | return Err; | |||
3174 | SeenFirstFunctionBody = true; | |||
3175 | } | |||
3176 | ||||
3177 | if (VSTOffset > 0) { | |||
3178 | // If we have a VST forward declaration record, make sure we | |||
3179 | // parse the VST now if we haven't already. It is needed to | |||
3180 | // set up the DeferredFunctionInfo vector for lazy reading. | |||
3181 | if (!SeenValueSymbolTable) { | |||
3182 | if (Error Err = BitcodeReader::parseValueSymbolTable(VSTOffset)) | |||
3183 | return Err; | |||
3184 | SeenValueSymbolTable = true; | |||
3185 | // Fall through so that we record the NextUnreadBit below. | |||
3186 | // This is necessary in case we have an anonymous function that | |||
3187 | // is later materialized. Since it will not have a VST entry we | |||
3188 | // need to fall back to the lazy parse to find its offset. | |||
3189 | } else { | |||
3190 | // If we have a VST forward declaration record, but have already | |||
3191 | // parsed the VST (just above, when the first function body was | |||
3192 | // encountered here), then we are resuming the parse after | |||
3193 | // materializing functions. The ResumeBit points to the | |||
3194 | // start of the last function block recorded in the | |||
3195 | // DeferredFunctionInfo map. Skip it. | |||
3196 | if (Stream.SkipBlock()) | |||
3197 | return error("Invalid record"); | |||
3198 | continue; | |||
3199 | } | |||
3200 | } | |||
3201 | ||||
3202 | // Support older bitcode files that did not have the function | |||
3203 | // index in the VST, nor a VST forward declaration record, as | |||
3204 | // well as anonymous functions that do not have VST entries. | |||
3205 | // Build the DeferredFunctionInfo vector on the fly. | |||
3206 | if (Error Err = rememberAndSkipFunctionBody()) | |||
3207 | return Err; | |||
3208 | ||||
3209 | // Suspend parsing when we reach the function bodies. Subsequent | |||
3210 | // materialization calls will resume it when necessary. If the bitcode | |||
3211 | // file is old, the symbol table will be at the end instead and will not | |||
3212 | // have been seen yet. In this case, just finish the parse now. | |||
3213 | if (SeenValueSymbolTable) { | |||
3214 | NextUnreadBit = Stream.GetCurrentBitNo(); | |||
3215 | // After the VST has been parsed, we need to make sure intrinsic name | |||
3216 | // are auto-upgraded. | |||
3217 | return globalCleanup(); | |||
3218 | } | |||
3219 | break; | |||
3220 | case bitc::USELIST_BLOCK_ID: | |||
3221 | if (Error Err = parseUseLists()) | |||
3222 | return Err; | |||
3223 | break; | |||
3224 | case bitc::OPERAND_BUNDLE_TAGS_BLOCK_ID: | |||
3225 | if (Error Err = parseOperandBundleTags()) | |||
3226 | return Err; | |||
3227 | break; | |||
3228 | case bitc::SYNC_SCOPE_NAMES_BLOCK_ID: | |||
3229 | if (Error Err = parseSyncScopeNames()) | |||
3230 | return Err; | |||
3231 | break; | |||
3232 | } | |||
3233 | continue; | |||
3234 | ||||
3235 | case BitstreamEntry::Record: | |||
3236 | // The interesting case. | |||
3237 | break; | |||
3238 | } | |||
3239 | ||||
3240 | // Read a record. | |||
3241 | auto BitCode = Stream.readRecord(Entry.ID, Record); | |||
3242 | switch (BitCode) { | |||
3243 | default: break; // Default behavior, ignore unknown content. | |||
3244 | case bitc::MODULE_CODE_VERSION: { | |||
3245 | Expected<unsigned> VersionOrErr = parseVersionRecord(Record); | |||
3246 | if (!VersionOrErr) | |||
3247 | return VersionOrErr.takeError(); | |||
3248 | UseRelativeIDs = *VersionOrErr >= 1; | |||
3249 | break; | |||
3250 | } | |||
3251 | case bitc::MODULE_CODE_TRIPLE: { // TRIPLE: [strchr x N] | |||
3252 | std::string S; | |||
3253 | if (convertToString(Record, 0, S)) | |||
3254 | return error("Invalid record"); | |||
3255 | TheModule->setTargetTriple(S); | |||
3256 | break; | |||
3257 | } | |||
3258 | case bitc::MODULE_CODE_DATALAYOUT: { // DATALAYOUT: [strchr x N] | |||
3259 | std::string S; | |||
3260 | if (convertToString(Record, 0, S)) | |||
3261 | return error("Invalid record"); | |||
3262 | TheModule->setDataLayout(S); | |||
3263 | break; | |||
3264 | } | |||
3265 | case bitc::MODULE_CODE_ASM: { // ASM: [strchr x N] | |||
3266 | std::string S; | |||
3267 | if (convertToString(Record, 0, S)) | |||
3268 | return error("Invalid record"); | |||
3269 | TheModule->setModuleInlineAsm(S); | |||
3270 | break; | |||
3271 | } | |||
3272 | case bitc::MODULE_CODE_DEPLIB: { // DEPLIB: [strchr x N] | |||
3273 | // FIXME: Remove in 4.0. | |||
3274 | std::string S; | |||
3275 | if (convertToString(Record, 0, S)) | |||
3276 | return error("Invalid record"); | |||
3277 | // Ignore value. | |||
3278 | break; | |||
3279 | } | |||
3280 | case bitc::MODULE_CODE_SECTIONNAME: { // SECTIONNAME: [strchr x N] | |||
3281 | std::string S; | |||
3282 | if (convertToString(Record, 0, S)) | |||
3283 | return error("Invalid record"); | |||
3284 | SectionTable.push_back(S); | |||
3285 | break; | |||
3286 | } | |||
3287 | case bitc::MODULE_CODE_GCNAME: { // SECTIONNAME: [strchr x N] | |||
3288 | std::string S; | |||
3289 | if (convertToString(Record, 0, S)) | |||
3290 | return error("Invalid record"); | |||
3291 | GCTable.push_back(S); | |||
3292 | break; | |||
3293 | } | |||
3294 | case bitc::MODULE_CODE_COMDAT: | |||
3295 | if (Error Err = parseComdatRecord(Record)) | |||
3296 | return Err; | |||
3297 | break; | |||
3298 | case bitc::MODULE_CODE_GLOBALVAR: | |||
3299 | if (Error Err = parseGlobalVarRecord(Record)) | |||
3300 | return Err; | |||
3301 | break; | |||
3302 | case bitc::MODULE_CODE_FUNCTION: | |||
3303 | if (Error Err = parseFunctionRecord(Record)) | |||
3304 | return Err; | |||
3305 | break; | |||
3306 | case bitc::MODULE_CODE_IFUNC: | |||
3307 | case bitc::MODULE_CODE_ALIAS: | |||
3308 | case bitc::MODULE_CODE_ALIAS_OLD: | |||
3309 | if (Error Err = parseGlobalIndirectSymbolRecord(BitCode, Record)) | |||
3310 | return Err; | |||
3311 | break; | |||
3312 | /// MODULE_CODE_VSTOFFSET: [offset] | |||
3313 | case bitc::MODULE_CODE_VSTOFFSET: | |||
3314 | if (Record.size() < 1) | |||
3315 | return error("Invalid record"); | |||
3316 | // Note that we subtract 1 here because the offset is relative to one word | |||
3317 | // before the start of the identification or module block, which was | |||
3318 | // historically always the start of the regular bitcode header. | |||
3319 | VSTOffset = Record[0] - 1; | |||
3320 | break; | |||
3321 | /// MODULE_CODE_SOURCE_FILENAME: [namechar x N] | |||
3322 | case bitc::MODULE_CODE_SOURCE_FILENAME: | |||
3323 | SmallString<128> ValueName; | |||
3324 | if (convertToString(Record, 0, ValueName)) | |||
3325 | return error("Invalid record"); | |||
3326 | TheModule->setSourceFileName(ValueName); | |||
3327 | break; | |||
3328 | } | |||
3329 | Record.clear(); | |||
3330 | } | |||
3331 | } | |||
3332 | ||||
3333 | Error BitcodeReader::parseBitcodeInto(Module *M, bool ShouldLazyLoadMetadata, | |||
3334 | bool IsImporting) { | |||
3335 | TheModule = M; | |||
3336 | MDLoader = MetadataLoader(Stream, *M, ValueList, IsImporting, | |||
3337 | [&](unsigned ID) { return getTypeByID(ID); }); | |||
3338 | return parseModule(0, ShouldLazyLoadMetadata); | |||
3339 | } | |||
3340 | ||||
3341 | Error BitcodeReader::typeCheckLoadStoreInst(Type *ValType, Type *PtrType) { | |||
3342 | if (!isa<PointerType>(PtrType)) | |||
3343 | return error("Load/Store operand is not a pointer type"); | |||
3344 | Type *ElemType = cast<PointerType>(PtrType)->getElementType(); | |||
3345 | ||||
3346 | if (ValType && ValType != ElemType) | |||
3347 | return error("Explicit load/store type does not match pointee " | |||
3348 | "type of pointer operand"); | |||
3349 | if (!PointerType::isLoadableOrStorableType(ElemType)) | |||
3350 | return error("Cannot load/store from pointer"); | |||
3351 | return Error::success(); | |||
3352 | } | |||
3353 | ||||
3354 | /// Lazily parse the specified function body block. | |||
3355 | Error BitcodeReader::parseFunctionBody(Function *F) { | |||
3356 | if (Stream.EnterSubBlock(bitc::FUNCTION_BLOCK_ID)) | |||
3357 | return error("Invalid record"); | |||
3358 | ||||
3359 | // Unexpected unresolved metadata when parsing function. | |||
3360 | if (MDLoader->hasFwdRefs()) | |||
3361 | return error("Invalid function metadata: incoming forward references"); | |||
3362 | ||||
3363 | InstructionList.clear(); | |||
3364 | unsigned ModuleValueListSize = ValueList.size(); | |||
3365 | unsigned ModuleMDLoaderSize = MDLoader->size(); | |||
3366 | ||||
3367 | // Add all the function arguments to the value table. | |||
3368 | for (Argument &I : F->args()) | |||
3369 | ValueList.push_back(&I); | |||
3370 | ||||
3371 | unsigned NextValueNo = ValueList.size(); | |||
3372 | BasicBlock *CurBB = nullptr; | |||
3373 | unsigned CurBBNo = 0; | |||
3374 | ||||
3375 | DebugLoc LastLoc; | |||
3376 | auto getLastInstruction = [&]() -> Instruction * { | |||
3377 | if (CurBB && !CurBB->empty()) | |||
3378 | return &CurBB->back(); | |||
3379 | else if (CurBBNo && FunctionBBs[CurBBNo - 1] && | |||
3380 | !FunctionBBs[CurBBNo - 1]->empty()) | |||
3381 | return &FunctionBBs[CurBBNo - 1]->back(); | |||
3382 | return nullptr; | |||
3383 | }; | |||
3384 | ||||
3385 | std::vector<OperandBundleDef> OperandBundles; | |||
3386 | ||||
3387 | // Read all the records. | |||
3388 | SmallVector<uint64_t, 64> Record; | |||
3389 | ||||
3390 | while (true) { | |||
3391 | BitstreamEntry Entry = Stream.advance(); | |||
3392 | ||||
3393 | switch (Entry.Kind) { | |||
3394 | case BitstreamEntry::Error: | |||
3395 | return error("Malformed block"); | |||
3396 | case BitstreamEntry::EndBlock: | |||
3397 | goto OutOfRecordLoop; | |||
3398 | ||||
3399 | case BitstreamEntry::SubBlock: | |||
3400 | switch (Entry.ID) { | |||
3401 | default: // Skip unknown content. | |||
3402 | if (Stream.SkipBlock()) | |||
3403 | return error("Invalid record"); | |||
3404 | break; | |||
3405 | case bitc::CONSTANTS_BLOCK_ID: | |||
3406 | if (Error Err = parseConstants()) | |||
3407 | return Err; | |||
3408 | NextValueNo = ValueList.size(); | |||
3409 | break; | |||
3410 | case bitc::VALUE_SYMTAB_BLOCK_ID: | |||
3411 | if (Error Err = parseValueSymbolTable()) | |||
3412 | return Err; | |||
3413 | break; | |||
3414 | case bitc::METADATA_ATTACHMENT_ID: | |||
3415 | if (Error Err = MDLoader->parseMetadataAttachment(*F, InstructionList)) | |||
3416 | return Err; | |||
3417 | break; | |||
3418 | case bitc::METADATA_BLOCK_ID: | |||
3419 | assert(DeferredMetadataInfo.empty() &&(static_cast <bool> (DeferredMetadataInfo.empty() && "Must read all module-level metadata before function-level") ? void (0) : __assert_fail ("DeferredMetadataInfo.empty() && \"Must read all module-level metadata before function-level\"" , "/build/llvm-toolchain-snapshot-7~svn329677/lib/Bitcode/Reader/BitcodeReader.cpp" , 3420, __extension__ __PRETTY_FUNCTION__)) | |||
3420 | "Must read all module-level metadata before function-level")(static_cast <bool> (DeferredMetadataInfo.empty() && "Must read all module-level metadata before function-level") ? void (0) : __assert_fail ("DeferredMetadataInfo.empty() && \"Must read all module-level metadata before function-level\"" , "/build/llvm-toolchain-snapshot-7~svn329677/lib/Bitcode/Reader/BitcodeReader.cpp" , 3420, __extension__ __PRETTY_FUNCTION__)); | |||
3421 | if (Error Err = MDLoader->parseFunctionMetadata()) | |||
3422 | return Err; | |||
3423 | break; | |||
3424 | case bitc::USELIST_BLOCK_ID: | |||
3425 | if (Error Err = parseUseLists()) | |||
3426 | return Err; | |||
3427 | break; | |||
3428 | } | |||
3429 | continue; | |||
3430 | ||||
3431 | case BitstreamEntry::Record: | |||
3432 | // The interesting case. | |||
3433 | break; | |||
3434 | } | |||
3435 | ||||
3436 | // Read a record. | |||
3437 | Record.clear(); | |||
3438 | Instruction *I = nullptr; | |||
3439 | unsigned BitCode = Stream.readRecord(Entry.ID, Record); | |||
3440 | switch (BitCode) { | |||
3441 | default: // Default behavior: reject | |||
3442 | return error("Invalid value"); | |||
3443 | case bitc::FUNC_CODE_DECLAREBLOCKS: { // DECLAREBLOCKS: [nblocks] | |||
3444 | if (Record.size() < 1 || Record[0] == 0) | |||
3445 | return error("Invalid record"); | |||
3446 | // Create all the basic blocks for the function. | |||
3447 | FunctionBBs.resize(Record[0]); | |||
3448 | ||||
3449 | // See if anything took the address of blocks in this function. | |||
3450 | auto BBFRI = BasicBlockFwdRefs.find(F); | |||
3451 | if (BBFRI == BasicBlockFwdRefs.end()) { | |||
3452 | for (unsigned i = 0, e = FunctionBBs.size(); i != e; ++i) | |||
3453 | FunctionBBs[i] = BasicBlock::Create(Context, "", F); | |||
3454 | } else { | |||
3455 | auto &BBRefs = BBFRI->second; | |||
3456 | // Check for invalid basic block references. | |||
3457 | if (BBRefs.size() > FunctionBBs.size()) | |||
3458 | return error("Invalid ID"); | |||
3459 | assert(!BBRefs.empty() && "Unexpected empty array")(static_cast <bool> (!BBRefs.empty() && "Unexpected empty array" ) ? void (0) : __assert_fail ("!BBRefs.empty() && \"Unexpected empty array\"" , "/build/llvm-toolchain-snapshot-7~svn329677/lib/Bitcode/Reader/BitcodeReader.cpp" , 3459, __extension__ __PRETTY_FUNCTION__)); | |||
3460 | assert(!BBRefs.front() && "Invalid reference to entry block")(static_cast <bool> (!BBRefs.front() && "Invalid reference to entry block" ) ? void (0) : __assert_fail ("!BBRefs.front() && \"Invalid reference to entry block\"" , "/build/llvm-toolchain-snapshot-7~svn329677/lib/Bitcode/Reader/BitcodeReader.cpp" , 3460, __extension__ __PRETTY_FUNCTION__)); | |||
3461 | for (unsigned I = 0, E = FunctionBBs.size(), RE = BBRefs.size(); I != E; | |||
3462 | ++I) | |||
3463 | if (I < RE && BBRefs[I]) { | |||
3464 | BBRefs[I]->insertInto(F); | |||
3465 | FunctionBBs[I] = BBRefs[I]; | |||
3466 | } else { | |||
3467 | FunctionBBs[I] = BasicBlock::Create(Context, "", F); | |||
3468 | } | |||
3469 | ||||
3470 | // Erase from the table. | |||
3471 | BasicBlockFwdRefs.erase(BBFRI); | |||
3472 | } | |||
3473 | ||||
3474 | CurBB = FunctionBBs[0]; | |||
3475 | continue; | |||
3476 | } | |||
3477 | ||||
3478 | case bitc::FUNC_CODE_DEBUG_LOC_AGAIN: // DEBUG_LOC_AGAIN | |||
3479 | // This record indicates that the last instruction is at the same | |||
3480 | // location as the previous instruction with a location. | |||
3481 | I = getLastInstruction(); | |||
3482 | ||||
3483 | if (!I) | |||
3484 | return error("Invalid record"); | |||
3485 | I->setDebugLoc(LastLoc); | |||
3486 | I = nullptr; | |||
3487 | continue; | |||
3488 | ||||
3489 | case bitc::FUNC_CODE_DEBUG_LOC: { // DEBUG_LOC: [line, col, scope, ia] | |||
3490 | I = getLastInstruction(); | |||
3491 | if (!I || Record.size() < 4) | |||
3492 | return error("Invalid record"); | |||
3493 | ||||
3494 | unsigned Line = Record[0], Col = Record[1]; | |||
3495 | unsigned ScopeID = Record[2], IAID = Record[3]; | |||
3496 | ||||
3497 | MDNode *Scope = nullptr, *IA = nullptr; | |||
3498 | if (ScopeID) { | |||
3499 | Scope = MDLoader->getMDNodeFwdRefOrNull(ScopeID - 1); | |||
3500 | if (!Scope) | |||
3501 | return error("Invalid record"); | |||
3502 | } | |||
3503 | if (IAID) { | |||
3504 | IA = MDLoader->getMDNodeFwdRefOrNull(IAID - 1); | |||
3505 | if (!IA) | |||
3506 | return error("Invalid record"); | |||
3507 | } | |||
3508 | LastLoc = DebugLoc::get(Line, Col, Scope, IA); | |||
3509 | I->setDebugLoc(LastLoc); | |||
3510 | I = nullptr; | |||
3511 | continue; | |||
3512 | } | |||
3513 | ||||
3514 | case bitc::FUNC_CODE_INST_BINOP: { // BINOP: [opval, ty, opval, opcode] | |||
3515 | unsigned OpNum = 0; | |||
3516 | Value *LHS, *RHS; | |||
3517 | if (getValueTypePair(Record, OpNum, NextValueNo, LHS) || | |||
3518 | popValue(Record, OpNum, NextValueNo, LHS->getType(), RHS) || | |||
3519 | OpNum+1 > Record.size()) | |||
3520 | return error("Invalid record"); | |||
3521 | ||||
3522 | int Opc = getDecodedBinaryOpcode(Record[OpNum++], LHS->getType()); | |||
3523 | if (Opc == -1) | |||
3524 | return error("Invalid record"); | |||
3525 | I = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS); | |||
3526 | InstructionList.push_back(I); | |||
3527 | if (OpNum < Record.size()) { | |||
3528 | if (Opc == Instruction::Add || | |||
3529 | Opc == Instruction::Sub || | |||
3530 | Opc == Instruction::Mul || | |||
3531 | Opc == Instruction::Shl) { | |||
3532 | if (Record[OpNum] & (1 << bitc::OBO_NO_SIGNED_WRAP)) | |||
3533 | cast<BinaryOperator>(I)->setHasNoSignedWrap(true); | |||
3534 | if (Record[OpNum] & (1 << bitc::OBO_NO_UNSIGNED_WRAP)) | |||
3535 | cast<BinaryOperator>(I)->setHasNoUnsignedWrap(true); | |||
3536 | } else if (Opc == Instruction::SDiv || | |||
3537 | Opc == Instruction::UDiv || | |||
3538 | Opc == Instruction::LShr || | |||
3539 | Opc == Instruction::AShr) { | |||
3540 | if (Record[OpNum] & (1 << bitc::PEO_EXACT)) | |||
3541 | cast<BinaryOperator>(I)->setIsExact(true); | |||
3542 | } else if (isa<FPMathOperator>(I)) { | |||
3543 | FastMathFlags FMF = getDecodedFastMathFlags(Record[OpNum]); | |||
3544 | if (FMF.any()) | |||
3545 | I->setFastMathFlags(FMF); | |||
3546 | } | |||
3547 | ||||
3548 | } | |||
3549 | break; | |||
3550 | } | |||
3551 | case bitc::FUNC_CODE_INST_CAST: { // CAST: [opval, opty, destty, castopc] | |||
3552 | unsigned OpNum = 0; | |||
3553 | Value *Op; | |||
3554 | if (getValueTypePair(Record, OpNum, NextValueNo, Op) || | |||
3555 | OpNum+2 != Record.size()) | |||
3556 | return error("Invalid record"); | |||
3557 | ||||
3558 | Type *ResTy = getTypeByID(Record[OpNum]); | |||
3559 | int Opc = getDecodedCastOpcode(Record[OpNum + 1]); | |||
3560 | if (Opc == -1 || !ResTy) | |||
3561 | return error("Invalid record"); | |||
3562 | Instruction *Temp = nullptr; | |||
3563 | if ((I = UpgradeBitCastInst(Opc, Op, ResTy, Temp))) { | |||
3564 | if (Temp) { | |||
3565 | InstructionList.push_back(Temp); | |||
3566 | CurBB->getInstList().push_back(Temp); | |||
3567 | } | |||
3568 | } else { | |||
3569 | auto CastOp = (Instruction::CastOps)Opc; | |||
3570 | if (!CastInst::castIsValid(CastOp, Op, ResTy)) | |||
3571 | return error("Invalid cast"); | |||
3572 | I = CastInst::Create(CastOp, Op, ResTy); | |||
3573 | } | |||
3574 | InstructionList.push_back(I); | |||
3575 | break; | |||
3576 | } | |||
3577 | case bitc::FUNC_CODE_INST_INBOUNDS_GEP_OLD: | |||
3578 | case bitc::FUNC_CODE_INST_GEP_OLD: | |||
3579 | case bitc::FUNC_CODE_INST_GEP: { // GEP: type, [n x operands] | |||
3580 | unsigned OpNum = 0; | |||
3581 | ||||
3582 | Type *Ty; | |||
3583 | bool InBounds; | |||
3584 | ||||
3585 | if (BitCode == bitc::FUNC_CODE_INST_GEP) { | |||
3586 | InBounds = Record[OpNum++]; | |||
3587 | Ty = getTypeByID(Record[OpNum++]); | |||
3588 | } else { | |||
3589 | InBounds = BitCode == bitc::FUNC_CODE_INST_INBOUNDS_GEP_OLD; | |||
3590 | Ty = nullptr; | |||
3591 | } | |||
3592 | ||||
3593 | Value *BasePtr; | |||
3594 | if (getValueTypePair(Record, OpNum, NextValueNo, BasePtr)) | |||
3595 | return error("Invalid record"); | |||
3596 | ||||
3597 | if (!Ty) | |||
3598 | Ty = cast<PointerType>(BasePtr->getType()->getScalarType()) | |||
3599 | ->getElementType(); | |||
3600 | else if (Ty != | |||
3601 | cast<PointerType>(BasePtr->getType()->getScalarType()) | |||
3602 | ->getElementType()) | |||
3603 | return error( | |||
3604 | "Explicit gep type does not match pointee type of pointer operand"); | |||
3605 | ||||
3606 | SmallVector<Value*, 16> GEPIdx; | |||
3607 | while (OpNum != Record.size()) { | |||
3608 | Value *Op; | |||
3609 | if (getValueTypePair(Record, OpNum, NextValueNo, Op)) | |||
3610 | return error("Invalid record"); | |||
3611 | GEPIdx.push_back(Op); | |||
3612 | } | |||
3613 | ||||
3614 | I = GetElementPtrInst::Create(Ty, BasePtr, GEPIdx); | |||
3615 | ||||
3616 | InstructionList.push_back(I); | |||
3617 | if (InBounds) | |||
3618 | cast<GetElementPtrInst>(I)->setIsInBounds(true); | |||
3619 | break; | |||
3620 | } | |||
3621 | ||||
3622 | case bitc::FUNC_CODE_INST_EXTRACTVAL: { | |||
3623 | // EXTRACTVAL: [opty, opval, n x indices] | |||
3624 | unsigned OpNum = 0; | |||
3625 | Value *Agg; | |||
3626 | if (getValueTypePair(Record, OpNum, NextValueNo, Agg)) | |||
3627 | return error("Invalid record"); | |||
3628 | ||||
3629 | unsigned RecSize = Record.size(); | |||
3630 | if (OpNum == RecSize) | |||
3631 | return error("EXTRACTVAL: Invalid instruction with 0 indices"); | |||
3632 | ||||
3633 | SmallVector<unsigned, 4> EXTRACTVALIdx; | |||
3634 | Type *CurTy = Agg->getType(); | |||
3635 | for (; OpNum != RecSize; ++OpNum) { | |||
3636 | bool IsArray = CurTy->isArrayTy(); | |||
3637 | bool IsStruct = CurTy->isStructTy(); | |||
3638 | uint64_t Index = Record[OpNum]; | |||
3639 | ||||
3640 | if (!IsStruct && !IsArray) | |||
3641 | return error("EXTRACTVAL: Invalid type"); | |||
3642 | if ((unsigned)Index != Index) | |||
3643 | return error("Invalid value"); | |||
3644 | if (IsStruct && Index >= CurTy->subtypes().size()) | |||
3645 | return error("EXTRACTVAL: Invalid struct index"); | |||
3646 | if (IsArray && Index >= CurTy->getArrayNumElements()) | |||
3647 | return error("EXTRACTVAL: Invalid array index"); | |||
3648 | EXTRACTVALIdx.push_back((unsigned)Index); | |||
3649 | ||||
3650 | if (IsStruct) | |||
3651 | CurTy = CurTy->subtypes()[Index]; | |||
3652 | else | |||
3653 | CurTy = CurTy->subtypes()[0]; | |||
3654 | } | |||
3655 | ||||
3656 | I = ExtractValueInst::Create(Agg, EXTRACTVALIdx); | |||
3657 | InstructionList.push_back(I); | |||
3658 | break; | |||
3659 | } | |||
3660 | ||||
3661 | case bitc::FUNC_CODE_INST_INSERTVAL: { | |||
3662 | // INSERTVAL: [opty, opval, opty, opval, n x indices] | |||
3663 | unsigned OpNum = 0; | |||
3664 | Value *Agg; | |||
3665 | if (getValueTypePair(Record, OpNum, NextValueNo, Agg)) | |||
3666 | return error("Invalid record"); | |||
3667 | Value *Val; | |||
3668 | if (getValueTypePair(Record, OpNum, NextValueNo, Val)) | |||
3669 | return error("Invalid record"); | |||
3670 | ||||
3671 | unsigned RecSize = Record.size(); | |||
3672 | if (OpNum == RecSize) | |||
3673 | return error("INSERTVAL: Invalid instruction with 0 indices"); | |||
3674 | ||||
3675 | SmallVector<unsigned, 4> INSERTVALIdx; | |||
3676 | Type *CurTy = Agg->getType(); | |||
3677 | for (; OpNum != RecSize; ++OpNum) { | |||
3678 | bool IsArray = CurTy->isArrayTy(); | |||
3679 | bool IsStruct = CurTy->isStructTy(); | |||
3680 | uint64_t Index = Record[OpNum]; | |||
3681 | ||||
3682 | if (!IsStruct && !IsArray) | |||
3683 | return error("INSERTVAL: Invalid type"); | |||
3684 | if ((unsigned)Index != Index) | |||
3685 | return error("Invalid value"); | |||
3686 | if (IsStruct && Index >= CurTy->subtypes().size()) | |||
3687 | return error("INSERTVAL: Invalid struct index"); | |||
3688 | if (IsArray && Index >= CurTy->getArrayNumElements()) | |||
3689 | return error("INSERTVAL: Invalid array index"); | |||
3690 | ||||
3691 | INSERTVALIdx.push_back((unsigned)Index); | |||
3692 | if (IsStruct) | |||
3693 | CurTy = CurTy->subtypes()[Index]; | |||
3694 | else | |||
3695 | CurTy = CurTy->subtypes()[0]; | |||
3696 | } | |||
3697 | ||||
3698 | if (CurTy != Val->getType()) | |||
3699 | return error("Inserted value type doesn't match aggregate type"); | |||
3700 | ||||
3701 | I = InsertValueInst::Create(Agg, Val, INSERTVALIdx); | |||
3702 | InstructionList.push_back(I); | |||
3703 | break; | |||
3704 | } | |||
3705 | ||||
3706 | case bitc::FUNC_CODE_INST_SELECT: { // SELECT: [opval, ty, opval, opval] | |||
3707 | // obsolete form of select | |||
3708 | // handles select i1 ... in old bitcode | |||
3709 | unsigned OpNum = 0; | |||
3710 | Value *TrueVal, *FalseVal, *Cond; | |||
3711 | if (getValueTypePair(Record, OpNum, NextValueNo, TrueVal) || | |||
3712 | popValue(Record, OpNum, NextValueNo, TrueVal->getType(), FalseVal) || | |||
3713 | popValue(Record, OpNum, NextValueNo, Type::getInt1Ty(Context), Cond)) | |||
3714 | return error("Invalid record"); | |||
3715 | ||||
3716 | I = SelectInst::Create(Cond, TrueVal, FalseVal); | |||
3717 | InstructionList.push_back(I); | |||
3718 | break; | |||
3719 | } | |||
3720 | ||||
3721 | case bitc::FUNC_CODE_INST_VSELECT: {// VSELECT: [ty,opval,opval,predty,pred] | |||
3722 | // new form of select | |||
3723 | // handles select i1 or select [N x i1] | |||
3724 | unsigned OpNum = 0; | |||
3725 | Value *TrueVal, *FalseVal, *Cond; | |||
3726 | if (getValueTypePair(Record, OpNum, NextValueNo, TrueVal) || | |||
3727 | popValue(Record, OpNum, NextValueNo, TrueVal->getType(), FalseVal) || | |||
3728 | getValueTypePair(Record, OpNum, NextValueNo, Cond)) | |||
3729 | return error("Invalid record"); | |||
3730 | ||||
3731 | // select condition can be either i1 or [N x i1] | |||
3732 | if (VectorType* vector_type = | |||
3733 | dyn_cast<VectorType>(Cond->getType())) { | |||
3734 | // expect <n x i1> | |||
3735 | if (vector_type->getElementType() != Type::getInt1Ty(Context)) | |||
3736 | return error("Invalid type for value"); | |||
3737 | } else { | |||
3738 | // expect i1 | |||
3739 | if (Cond->getType() != Type::getInt1Ty(Context)) | |||
3740 | return error("Invalid type for value"); | |||
3741 | } | |||
3742 | ||||
3743 | I = SelectInst::Create(Cond, TrueVal, FalseVal); | |||
3744 | InstructionList.push_back(I); | |||
3745 | break; | |||
3746 | } | |||
3747 | ||||
3748 | case bitc::FUNC_CODE_INST_EXTRACTELT: { // EXTRACTELT: [opty, opval, opval] | |||
3749 | unsigned OpNum = 0; | |||
3750 | Value *Vec, *Idx; | |||
3751 | if (getValueTypePair(Record, OpNum, NextValueNo, Vec) || | |||
3752 | getValueTypePair(Record, OpNum, NextValueNo, Idx)) | |||
3753 | return error("Invalid record"); | |||
3754 | if (!Vec->getType()->isVectorTy()) | |||
3755 | return error("Invalid type for value"); | |||
3756 | I = ExtractElementInst::Create(Vec, Idx); | |||
3757 | InstructionList.push_back(I); | |||
3758 | break; | |||
3759 | } | |||
3760 | ||||
3761 | case bitc::FUNC_CODE_INST_INSERTELT: { // INSERTELT: [ty, opval,opval,opval] | |||
3762 | unsigned OpNum = 0; | |||
3763 | Value *Vec, *Elt, *Idx; | |||
3764 | if (getValueTypePair(Record, OpNum, NextValueNo, Vec)) | |||
3765 | return error("Invalid record"); | |||
3766 | if (!Vec->getType()->isVectorTy()) | |||
3767 | return error("Invalid type for value"); | |||
3768 | if (popValue(Record, OpNum, NextValueNo, | |||
3769 | cast<VectorType>(Vec->getType())->getElementType(), Elt) || | |||
3770 | getValueTypePair(Record, OpNum, NextValueNo, Idx)) | |||
3771 | return error("Invalid record"); | |||
3772 | I = InsertElementInst::Create(Vec, Elt, Idx); | |||
3773 | InstructionList.push_back(I); | |||
3774 | break; | |||
3775 | } | |||
3776 | ||||
3777 | case bitc::FUNC_CODE_INST_SHUFFLEVEC: {// SHUFFLEVEC: [opval,ty,opval,opval] | |||
3778 | unsigned OpNum = 0; | |||
3779 | Value *Vec1, *Vec2, *Mask; | |||
3780 | if (getValueTypePair(Record, OpNum, NextValueNo, Vec1) || | |||
3781 | popValue(Record, OpNum, NextValueNo, Vec1->getType(), Vec2)) | |||
3782 | return error("Invalid record"); | |||
3783 | ||||
3784 | if (getValueTypePair(Record, OpNum, NextValueNo, Mask)) | |||
3785 | return error("Invalid record"); | |||
3786 | if (!Vec1->getType()->isVectorTy() || !Vec2->getType()->isVectorTy()) | |||
3787 | return error("Invalid type for value"); | |||
3788 | I = new ShuffleVectorInst(Vec1, Vec2, Mask); | |||
3789 | InstructionList.push_back(I); | |||
3790 | break; | |||
3791 | } | |||
3792 | ||||
3793 | case bitc::FUNC_CODE_INST_CMP: // CMP: [opty, opval, opval, pred] | |||
3794 | // Old form of ICmp/FCmp returning bool | |||
3795 | // Existed to differentiate between icmp/fcmp and vicmp/vfcmp which were | |||
3796 | // both legal on vectors but had different behaviour. | |||
3797 | case bitc::FUNC_CODE_INST_CMP2: { // CMP2: [opty, opval, opval, pred] | |||
3798 | // FCmp/ICmp returning bool or vector of bool | |||
3799 | ||||
3800 | unsigned OpNum = 0; | |||
3801 | Value *LHS, *RHS; | |||
3802 | if (getValueTypePair(Record, OpNum, NextValueNo, LHS) || | |||
3803 | popValue(Record, OpNum, NextValueNo, LHS->getType(), RHS)) | |||
3804 | return error("Invalid record"); | |||
3805 | ||||
3806 | unsigned PredVal = Record[OpNum]; | |||
3807 | bool IsFP = LHS->getType()->isFPOrFPVectorTy(); | |||
3808 | FastMathFlags FMF; | |||
3809 | if (IsFP && Record.size() > OpNum+1) | |||
3810 | FMF = getDecodedFastMathFlags(Record[++OpNum]); | |||
3811 | ||||
3812 | if (OpNum+1 != Record.size()) | |||
3813 | return error("Invalid record"); | |||
3814 | ||||
3815 | if (LHS->getType()->isFPOrFPVectorTy()) | |||
3816 | I = new FCmpInst((FCmpInst::Predicate)PredVal, LHS, RHS); | |||
3817 | else | |||
3818 | I = new ICmpInst((ICmpInst::Predicate)PredVal, LHS, RHS); | |||
3819 | ||||
3820 | if (FMF.any()) | |||
3821 | I->setFastMathFlags(FMF); | |||
3822 | InstructionList.push_back(I); | |||
3823 | break; | |||
3824 | } | |||
3825 | ||||
3826 | case bitc::FUNC_CODE_INST_RET: // RET: [opty,opval<optional>] | |||
3827 | { | |||
3828 | unsigned Size = Record.size(); | |||
3829 | if (Size == 0) { | |||
3830 | I = ReturnInst::Create(Context); | |||
3831 | InstructionList.push_back(I); | |||
3832 | break; | |||
3833 | } | |||
3834 | ||||
3835 | unsigned OpNum = 0; | |||
3836 | Value *Op = nullptr; | |||
3837 | if (getValueTypePair(Record, OpNum, NextValueNo, Op)) | |||
3838 | return error("Invalid record"); | |||
3839 | if (OpNum != Record.size()) | |||
3840 | return error("Invalid record"); | |||
3841 | ||||
3842 | I = ReturnInst::Create(Context, Op); | |||
3843 | InstructionList.push_back(I); | |||
3844 | break; | |||
3845 | } | |||
3846 | case bitc::FUNC_CODE_INST_BR: { // BR: [bb#, bb#, opval] or [bb#] | |||
3847 | if (Record.size() != 1 && Record.size() != 3) | |||
3848 | return error("Invalid record"); | |||
3849 | BasicBlock *TrueDest = getBasicBlock(Record[0]); | |||
3850 | if (!TrueDest) | |||
3851 | return error("Invalid record"); | |||
3852 | ||||
3853 | if (Record.size() == 1) { | |||
3854 | I = BranchInst::Create(TrueDest); | |||
3855 | InstructionList.push_back(I); | |||
3856 | } | |||
3857 | else { | |||
3858 | BasicBlock *FalseDest = getBasicBlock(Record[1]); | |||
3859 | Value *Cond = getValue(Record, 2, NextValueNo, | |||
3860 | Type::getInt1Ty(Context)); | |||
3861 | if (!FalseDest || !Cond) | |||
3862 | return error("Invalid record"); | |||
3863 | I = BranchInst::Create(TrueDest, FalseDest, Cond); | |||
3864 | InstructionList.push_back(I); | |||
3865 | } | |||
3866 | break; | |||
3867 | } | |||
3868 | case bitc::FUNC_CODE_INST_CLEANUPRET: { // CLEANUPRET: [val] or [val,bb#] | |||
3869 | if (Record.size() != 1 && Record.size() != 2) | |||
3870 | return error("Invalid record"); | |||
3871 | unsigned Idx = 0; | |||
3872 | Value *CleanupPad = | |||
3873 | getValue(Record, Idx++, NextValueNo, Type::getTokenTy(Context)); | |||
3874 | if (!CleanupPad) | |||
3875 | return error("Invalid record"); | |||
3876 | BasicBlock *UnwindDest = nullptr; | |||
3877 | if (Record.size() == 2) { | |||
3878 | UnwindDest = getBasicBlock(Record[Idx++]); | |||
3879 | if (!UnwindDest) | |||
3880 | return error("Invalid record"); | |||
3881 | } | |||
3882 | ||||
3883 | I = CleanupReturnInst::Create(CleanupPad, UnwindDest); | |||
3884 | InstructionList.push_back(I); | |||
3885 | break; | |||
3886 | } | |||
3887 | case bitc::FUNC_CODE_INST_CATCHRET: { // CATCHRET: [val,bb#] | |||
3888 | if (Record.size() != 2) | |||
3889 | return error("Invalid record"); | |||
3890 | unsigned Idx = 0; | |||
3891 | Value *CatchPad = | |||
3892 | getValue(Record, Idx++, NextValueNo, Type::getTokenTy(Context)); | |||
3893 | if (!CatchPad) | |||
3894 | return error("Invalid record"); | |||
3895 | BasicBlock *BB = getBasicBlock(Record[Idx++]); | |||
3896 | if (!BB) | |||
3897 | return error("Invalid record"); | |||
3898 | ||||
3899 | I = CatchReturnInst::Create(CatchPad, BB); | |||
3900 | InstructionList.push_back(I); | |||
3901 | break; | |||
3902 | } | |||
3903 | case bitc::FUNC_CODE_INST_CATCHSWITCH: { // CATCHSWITCH: [tok,num,(bb)*,bb?] | |||
3904 | // We must have, at minimum, the outer scope and the number of arguments. | |||
3905 | if (Record.size() < 2) | |||
3906 | return error("Invalid record"); | |||
3907 | ||||
3908 | unsigned Idx = 0; | |||
3909 | ||||
3910 | Value *ParentPad = | |||
3911 | getValue(Record, Idx++, NextValueNo, Type::getTokenTy(Context)); | |||
3912 | ||||
3913 | unsigned NumHandlers = Record[Idx++]; | |||
3914 | ||||
3915 | SmallVector<BasicBlock *, 2> Handlers; | |||
3916 | for (unsigned Op = 0; Op != NumHandlers; ++Op) { | |||
3917 | BasicBlock *BB = getBasicBlock(Record[Idx++]); | |||
3918 | if (!BB) | |||
3919 | return error("Invalid record"); | |||
3920 | Handlers.push_back(BB); | |||
3921 | } | |||
3922 | ||||
3923 | BasicBlock *UnwindDest = nullptr; | |||
3924 | if (Idx + 1 == Record.size()) { | |||
3925 | UnwindDest = getBasicBlock(Record[Idx++]); | |||
3926 | if (!UnwindDest) | |||
3927 | return error("Invalid record"); | |||
3928 | } | |||
3929 | ||||
3930 | if (Record.size() != Idx) | |||
3931 | return error("Invalid record"); | |||
3932 | ||||
3933 | auto *CatchSwitch = | |||
3934 | CatchSwitchInst::Create(ParentPad, UnwindDest, NumHandlers); | |||
3935 | for (BasicBlock *Handler : Handlers) | |||
3936 | CatchSwitch->addHandler(Handler); | |||
3937 | I = CatchSwitch; | |||
3938 | InstructionList.push_back(I); | |||
3939 | break; | |||
3940 | } | |||
3941 | case bitc::FUNC_CODE_INST_CATCHPAD: | |||
3942 | case bitc::FUNC_CODE_INST_CLEANUPPAD: { // [tok,num,(ty,val)*] | |||
3943 | // We must have, at minimum, the outer scope and the number of arguments. | |||
3944 | if (Record.size() < 2) | |||
3945 | return error("Invalid record"); | |||
3946 | ||||
3947 | unsigned Idx = 0; | |||
3948 | ||||
3949 | Value *ParentPad = | |||
3950 | getValue(Record, Idx++, NextValueNo, Type::getTokenTy(Context)); | |||
3951 | ||||
3952 | unsigned NumArgOperands = Record[Idx++]; | |||
3953 | ||||
3954 | SmallVector<Value *, 2> Args; | |||
3955 | for (unsigned Op = 0; Op != NumArgOperands; ++Op) { | |||
3956 | Value *Val; | |||
3957 | if (getValueTypePair(Record, Idx, NextValueNo, Val)) | |||
3958 | return error("Invalid record"); | |||
3959 | Args.push_back(Val); | |||
3960 | } | |||
3961 | ||||
3962 | if (Record.size() != Idx) | |||
3963 | return error("Invalid record"); | |||
3964 | ||||
3965 | if (BitCode == bitc::FUNC_CODE_INST_CLEANUPPAD) | |||
3966 | I = CleanupPadInst::Create(ParentPad, Args); | |||
3967 | else | |||
3968 | I = CatchPadInst::Create(ParentPad, Args); | |||
3969 | InstructionList.push_back(I); | |||
3970 | break; | |||
3971 | } | |||
3972 | case bitc::FUNC_CODE_INST_SWITCH: { // SWITCH: [opty, op0, op1, ...] | |||
3973 | // Check magic | |||
3974 | if ((Record[0] >> 16) == SWITCH_INST_MAGIC) { | |||
3975 | // "New" SwitchInst format with case ranges. The changes to write this | |||
3976 | // format were reverted but we still recognize bitcode that uses it. | |||
3977 | // Hopefully someday we will have support for case ranges and can use | |||
3978 | // this format again. | |||
3979 | ||||
3980 | Type *OpTy = getTypeByID(Record[1]); | |||
3981 | unsigned ValueBitWidth = cast<IntegerType>(OpTy)->getBitWidth(); | |||
3982 | ||||
3983 | Value *Cond = getValue(Record, 2, NextValueNo, OpTy); | |||
3984 | BasicBlock *Default = getBasicBlock(Record[3]); | |||
3985 | if (!OpTy || !Cond || !Default) | |||
3986 | return error("Invalid record"); | |||
3987 | ||||
3988 | unsigned NumCases = Record[4]; | |||
3989 | ||||
3990 | SwitchInst *SI = SwitchInst::Create(Cond, Default, NumCases); | |||
3991 | InstructionList.push_back(SI); | |||
3992 | ||||
3993 | unsigned CurIdx = 5; | |||
3994 | for (unsigned i = 0; i != NumCases; ++i) { | |||
3995 | SmallVector<ConstantInt*, 1> CaseVals; | |||
3996 | unsigned NumItems = Record[CurIdx++]; | |||
3997 | for (unsigned ci = 0; ci != NumItems; ++ci) { | |||
3998 | bool isSingleNumber = Record[CurIdx++]; | |||
3999 | ||||
4000 | APInt Low; | |||
4001 | unsigned ActiveWords = 1; | |||
4002 | if (ValueBitWidth > 64) | |||
4003 | ActiveWords = Record[CurIdx++]; | |||
4004 | Low = readWideAPInt(makeArrayRef(&Record[CurIdx], ActiveWords), | |||
4005 | ValueBitWidth); | |||
4006 | CurIdx += ActiveWords; | |||
4007 | ||||
4008 | if (!isSingleNumber) { | |||
4009 | ActiveWords = 1; | |||
4010 | if (ValueBitWidth > 64) | |||
4011 | ActiveWords = Record[CurIdx++]; | |||
4012 | APInt High = readWideAPInt( | |||
4013 | makeArrayRef(&Record[CurIdx], ActiveWords), ValueBitWidth); | |||
4014 | CurIdx += ActiveWords; | |||
4015 | ||||
4016 | // FIXME: It is not clear whether values in the range should be | |||
4017 | // compared as signed or unsigned values. The partially | |||
4018 | // implemented changes that used this format in the past used | |||
4019 | // unsigned comparisons. | |||
4020 | for ( ; Low.ule(High); ++Low) | |||
4021 | CaseVals.push_back(ConstantInt::get(Context, Low)); | |||
4022 | } else | |||
4023 | CaseVals.push_back(ConstantInt::get(Context, Low)); | |||
4024 | } | |||
4025 | BasicBlock *DestBB = getBasicBlock(Record[CurIdx++]); | |||
4026 | for (SmallVector<ConstantInt*, 1>::iterator cvi = CaseVals.begin(), | |||
4027 | cve = CaseVals.end(); cvi != cve; ++cvi) | |||
4028 | SI->addCase(*cvi, DestBB); | |||
4029 | } | |||
4030 | I = SI; | |||
4031 | break; | |||
4032 | } | |||
4033 | ||||
4034 | // Old SwitchInst format without case ranges. | |||
4035 | ||||
4036 | if (Record.size() < 3 || (Record.size() & 1) == 0) | |||
4037 | return error("Invalid record"); | |||
4038 | Type *OpTy = getTypeByID(Record[0]); | |||
4039 | Value *Cond = getValue(Record, 1, NextValueNo, OpTy); | |||
4040 | BasicBlock *Default = getBasicBlock(Record[2]); | |||
4041 | if (!OpTy || !Cond || !Default) | |||
4042 | return error("Invalid record"); | |||
4043 | unsigned NumCases = (Record.size()-3)/2; | |||
4044 | SwitchInst *SI = SwitchInst::Create(Cond, Default, NumCases); | |||
4045 | InstructionList.push_back(SI); | |||
4046 | for (unsigned i = 0, e = NumCases; i != e; ++i) { | |||
4047 | ConstantInt *CaseVal = | |||
4048 | dyn_cast_or_null<ConstantInt>(getFnValueByID(Record[3+i*2], OpTy)); | |||
4049 | BasicBlock *DestBB = getBasicBlock(Record[1+3+i*2]); | |||
4050 | if (!CaseVal || !DestBB) { | |||
4051 | delete SI; | |||
4052 | return error("Invalid record"); | |||
4053 | } | |||
4054 | SI->addCase(CaseVal, DestBB); | |||
4055 | } | |||
4056 | I = SI; | |||
4057 | break; | |||
4058 | } | |||
4059 | case bitc::FUNC_CODE_INST_INDIRECTBR: { // INDIRECTBR: [opty, op0, op1, ...] | |||
4060 | if (Record.size() < 2) | |||
4061 | return error("Invalid record"); | |||
4062 | Type *OpTy = getTypeByID(Record[0]); | |||
4063 | Value *Address = getValue(Record, 1, NextValueNo, OpTy); | |||
4064 | if (!OpTy || !Address) | |||
4065 | return error("Invalid record"); | |||
4066 | unsigned NumDests = Record.size()-2; | |||
4067 | IndirectBrInst *IBI = IndirectBrInst::Create(Address, NumDests); | |||
4068 | InstructionList.push_back(IBI); | |||
4069 | for (unsigned i = 0, e = NumDests; i != e; ++i) { | |||
4070 | if (BasicBlock *DestBB = getBasicBlock(Record[2+i])) { | |||
4071 | IBI->addDestination(DestBB); | |||
4072 | } else { | |||
4073 | delete IBI; | |||
4074 | return error("Invalid record"); | |||
4075 | } | |||
4076 | } | |||
4077 | I = IBI; | |||
4078 | break; | |||
4079 | } | |||
4080 | ||||
4081 | case bitc::FUNC_CODE_INST_INVOKE: { | |||
4082 | // INVOKE: [attrs, cc, normBB, unwindBB, fnty, op0,op1,op2, ...] | |||
4083 | if (Record.size() < 4) | |||
4084 | return error("Invalid record"); | |||
4085 | unsigned OpNum = 0; | |||
4086 | AttributeList PAL = getAttributes(Record[OpNum++]); | |||
4087 | unsigned CCInfo = Record[OpNum++]; | |||
4088 | BasicBlock *NormalBB = getBasicBlock(Record[OpNum++]); | |||
4089 | BasicBlock *UnwindBB = getBasicBlock(Record[OpNum++]); | |||
4090 | ||||
4091 | FunctionType *FTy = nullptr; | |||
4092 | if (CCInfo >> 13 & 1 && | |||
4093 | !(FTy = dyn_cast<FunctionType>(getTypeByID(Record[OpNum++])))) | |||
4094 | return error("Explicit invoke type is not a function type"); | |||
4095 | ||||
4096 | Value *Callee; | |||
4097 | if (getValueTypePair(Record, OpNum, NextValueNo, Callee)) | |||
4098 | return error("Invalid record"); | |||
4099 | ||||
4100 | PointerType *CalleeTy = dyn_cast<PointerType>(Callee->getType()); | |||
4101 | if (!CalleeTy) | |||
4102 | return error("Callee is not a pointer"); | |||
4103 | if (!FTy) { | |||
4104 | FTy = dyn_cast<FunctionType>(CalleeTy->getElementType()); | |||
4105 | if (!FTy) | |||
4106 | return error("Callee is not of pointer to function type"); | |||
4107 | } else if (CalleeTy->getElementType() != FTy) | |||
4108 | return error("Explicit invoke type does not match pointee type of " | |||
4109 | "callee operand"); | |||
4110 | if (Record.size() < FTy->getNumParams() + OpNum) | |||
4111 | return error("Insufficient operands to call"); | |||
4112 | ||||
4113 | SmallVector<Value*, 16> Ops; | |||
4114 | for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i, ++OpNum) { | |||
4115 | Ops.push_back(getValue(Record, OpNum, NextValueNo, | |||
4116 | FTy->getParamType(i))); | |||
4117 | if (!Ops.back()) | |||
4118 | return error("Invalid record"); | |||
4119 | } | |||
4120 | ||||
4121 | if (!FTy->isVarArg()) { | |||
4122 | if (Record.size() != OpNum) | |||
4123 | return error("Invalid record"); | |||
4124 | } else { | |||
4125 | // Read type/value pairs for varargs params. | |||
4126 | while (OpNum != Record.size()) { | |||
4127 | Value *Op; | |||
4128 | if (getValueTypePair(Record, OpNum, NextValueNo, Op)) | |||
4129 | return error("Invalid record"); | |||
4130 | Ops.push_back(Op); | |||
4131 | } | |||
4132 | } | |||
4133 | ||||
4134 | I = InvokeInst::Create(Callee, NormalBB, UnwindBB, Ops, OperandBundles); | |||
4135 | OperandBundles.clear(); | |||
4136 | InstructionList.push_back(I); | |||
4137 | cast<InvokeInst>(I)->setCallingConv( | |||
4138 | static_cast<CallingConv::ID>(CallingConv::MaxID & CCInfo)); | |||
4139 | cast<InvokeInst>(I)->setAttributes(PAL); | |||
4140 | break; | |||
4141 | } | |||
4142 | case bitc::FUNC_CODE_INST_RESUME: { // RESUME: [opval] | |||
4143 | unsigned Idx = 0; | |||
4144 | Value *Val = nullptr; | |||
4145 | if (getValueTypePair(Record, Idx, NextValueNo, Val)) | |||
4146 | return error("Invalid record"); | |||
4147 | I = ResumeInst::Create(Val); | |||
4148 | InstructionList.push_back(I); | |||
4149 | break; | |||
4150 | } | |||
4151 | case bitc::FUNC_CODE_INST_UNREACHABLE: // UNREACHABLE | |||
4152 | I = new UnreachableInst(Context); | |||
4153 | InstructionList.push_back(I); | |||
4154 | break; | |||
4155 | case bitc::FUNC_CODE_INST_PHI: { // PHI: [ty, val0,bb0, ...] | |||
4156 | if (Record.size() < 1 || ((Record.size()-1)&1)) | |||
4157 | return error("Invalid record"); | |||
4158 | Type *Ty = getTypeByID(Record[0]); | |||
4159 | if (!Ty) | |||
4160 | return error("Invalid record"); | |||
4161 | ||||
4162 | PHINode *PN = PHINode::Create(Ty, (Record.size()-1)/2); | |||
4163 | InstructionList.push_back(PN); | |||
4164 | ||||
4165 | for (unsigned i = 0, e = Record.size()-1; i != e; i += 2) { | |||
4166 | Value *V; | |||
4167 | // With the new function encoding, it is possible that operands have | |||
4168 | // negative IDs (for forward references). Use a signed VBR | |||
4169 | // representation to keep the encoding small. | |||
4170 | if (UseRelativeIDs) | |||
4171 | V = getValueSigned(Record, 1+i, NextValueNo, Ty); | |||
4172 | else | |||
4173 | V = getValue(Record, 1+i, NextValueNo, Ty); | |||
4174 | BasicBlock *BB = getBasicBlock(Record[2+i]); | |||
4175 | if (!V || !BB) | |||
4176 | return error("Invalid record"); | |||
4177 | PN->addIncoming(V, BB); | |||
4178 | } | |||
4179 | I = PN; | |||
4180 | break; | |||
4181 | } | |||
4182 | ||||
4183 | case bitc::FUNC_CODE_INST_LANDINGPAD: | |||
4184 | case bitc::FUNC_CODE_INST_LANDINGPAD_OLD: { | |||
4185 | // LANDINGPAD: [ty, val, val, num, (id0,val0 ...)?] | |||
4186 | unsigned Idx = 0; | |||
4187 | if (BitCode == bitc::FUNC_CODE_INST_LANDINGPAD) { | |||
4188 | if (Record.size() < 3) | |||
4189 | return error("Invalid record"); | |||
4190 | } else { | |||
4191 | assert(BitCode == bitc::FUNC_CODE_INST_LANDINGPAD_OLD)(static_cast <bool> (BitCode == bitc::FUNC_CODE_INST_LANDINGPAD_OLD ) ? void (0) : __assert_fail ("BitCode == bitc::FUNC_CODE_INST_LANDINGPAD_OLD" , "/build/llvm-toolchain-snapshot-7~svn329677/lib/Bitcode/Reader/BitcodeReader.cpp" , 4191, __extension__ __PRETTY_FUNCTION__)); | |||
4192 | if (Record.size() < 4) | |||
4193 | return error("Invalid record"); | |||
4194 | } | |||
4195 | Type *Ty = getTypeByID(Record[Idx++]); | |||
4196 | if (!Ty) | |||
4197 | return error("Invalid record"); | |||
4198 | if (BitCode == bitc::FUNC_CODE_INST_LANDINGPAD_OLD) { | |||
4199 | Value *PersFn = nullptr; | |||
4200 | if (getValueTypePair(Record, Idx, NextValueNo, PersFn)) | |||
4201 | return error("Invalid record"); | |||
4202 | ||||
4203 | if (!F->hasPersonalityFn()) | |||
4204 | F->setPersonalityFn(cast<Constant>(PersFn)); | |||
4205 | else if (F->getPersonalityFn() != cast<Constant>(PersFn)) | |||
4206 | return error("Personality function mismatch"); | |||
4207 | } | |||
4208 | ||||
4209 | bool IsCleanup = !!Record[Idx++]; | |||
4210 | unsigned NumClauses = Record[Idx++]; | |||
4211 | LandingPadInst *LP = LandingPadInst::Create(Ty, NumClauses); | |||
4212 | LP->setCleanup(IsCleanup); | |||
4213 | for (unsigned J = 0; J != NumClauses; ++J) { | |||
4214 | LandingPadInst::ClauseType CT = | |||
4215 | LandingPadInst::ClauseType(Record[Idx++]); (void)CT; | |||
4216 | Value *Val; | |||
4217 | ||||
4218 | if (getValueTypePair(Record, Idx, NextValueNo, Val)) { | |||
4219 | delete LP; | |||
4220 | return error("Invalid record"); | |||
4221 | } | |||
4222 | ||||
4223 | assert((CT != LandingPadInst::Catch ||(static_cast <bool> ((CT != LandingPadInst::Catch || !isa <ArrayType>(Val->getType())) && "Catch clause has a invalid type!" ) ? void (0) : __assert_fail ("(CT != LandingPadInst::Catch || !isa<ArrayType>(Val->getType())) && \"Catch clause has a invalid type!\"" , "/build/llvm-toolchain-snapshot-7~svn329677/lib/Bitcode/Reader/BitcodeReader.cpp" , 4225, __extension__ __PRETTY_FUNCTION__)) | |||
4224 | !isa<ArrayType>(Val->getType())) &&(static_cast <bool> ((CT != LandingPadInst::Catch || !isa <ArrayType>(Val->getType())) && "Catch clause has a invalid type!" ) ? void (0) : __assert_fail ("(CT != LandingPadInst::Catch || !isa<ArrayType>(Val->getType())) && \"Catch clause has a invalid type!\"" , "/build/llvm-toolchain-snapshot-7~svn329677/lib/Bitcode/Reader/BitcodeReader.cpp" , 4225, __extension__ __PRETTY_FUNCTION__)) | |||
4225 | "Catch clause has a invalid type!")(static_cast <bool> ((CT != LandingPadInst::Catch || !isa <ArrayType>(Val->getType())) && "Catch clause has a invalid type!" ) ? void (0) : __assert_fail ("(CT != LandingPadInst::Catch || !isa<ArrayType>(Val->getType())) && \"Catch clause has a invalid type!\"" , "/build/llvm-toolchain-snapshot-7~svn329677/lib/Bitcode/Reader/BitcodeReader.cpp" , 4225, __extension__ __PRETTY_FUNCTION__)); | |||
4226 | assert((CT != LandingPadInst::Filter ||(static_cast <bool> ((CT != LandingPadInst::Filter || isa <ArrayType>(Val->getType())) && "Filter clause has invalid type!" ) ? void (0) : __assert_fail ("(CT != LandingPadInst::Filter || isa<ArrayType>(Val->getType())) && \"Filter clause has invalid type!\"" , "/build/llvm-toolchain-snapshot-7~svn329677/lib/Bitcode/Reader/BitcodeReader.cpp" , 4228, __extension__ __PRETTY_FUNCTION__)) | |||
4227 | isa<ArrayType>(Val->getType())) &&(static_cast <bool> ((CT != LandingPadInst::Filter || isa <ArrayType>(Val->getType())) && "Filter clause has invalid type!" ) ? void (0) : __assert_fail ("(CT != LandingPadInst::Filter || isa<ArrayType>(Val->getType())) && \"Filter clause has invalid type!\"" , "/build/llvm-toolchain-snapshot-7~svn329677/lib/Bitcode/Reader/BitcodeReader.cpp" , 4228, __extension__ __PRETTY_FUNCTION__)) | |||
4228 | "Filter clause has invalid type!")(static_cast <bool> ((CT != LandingPadInst::Filter || isa <ArrayType>(Val->getType())) && "Filter clause has invalid type!" ) ? void (0) : __assert_fail ("(CT != LandingPadInst::Filter || isa<ArrayType>(Val->getType())) && \"Filter clause has invalid type!\"" , "/build/llvm-toolchain-snapshot-7~svn329677/lib/Bitcode/Reader/BitcodeReader.cpp" , 4228, __extension__ __PRETTY_FUNCTION__)); | |||
4229 | LP->addClause(cast<Constant>(Val)); | |||
4230 | } | |||
4231 | ||||
4232 | I = LP; | |||
4233 | InstructionList.push_back(I); | |||
4234 | break; | |||
4235 | } | |||
4236 | ||||
4237 | case bitc::FUNC_CODE_INST_ALLOCA: { // ALLOCA: [instty, opty, op, align] | |||
4238 | if (Record.size() != 4) | |||
4239 | return error("Invalid record"); | |||
4240 | uint64_t AlignRecord = Record[3]; | |||
4241 | const uint64_t InAllocaMask = uint64_t(1) << 5; | |||
4242 | const uint64_t ExplicitTypeMask = uint64_t(1) << 6; | |||
4243 | const uint64_t SwiftErrorMask = uint64_t(1) << 7; | |||
4244 | const uint64_t FlagMask = InAllocaMask | ExplicitTypeMask | | |||
4245 | SwiftErrorMask; | |||
4246 | bool InAlloca = AlignRecord & InAllocaMask; | |||
4247 | bool SwiftError = AlignRecord & SwiftErrorMask; | |||
4248 | Type *Ty = getTypeByID(Record[0]); | |||
4249 | if ((AlignRecord & ExplicitTypeMask) == 0) { | |||
4250 | auto *PTy = dyn_cast_or_null<PointerType>(Ty); | |||
4251 | if (!PTy) | |||
4252 | return error("Old-style alloca with a non-pointer type"); | |||
4253 | Ty = PTy->getElementType(); | |||
4254 | } | |||
4255 | Type *OpTy = getTypeByID(Record[1]); | |||
4256 | Value *Size = getFnValueByID(Record[2], OpTy); | |||
4257 | unsigned Align; | |||
4258 | if (Error Err = parseAlignmentValue(AlignRecord & ~FlagMask, Align)) { | |||
4259 | return Err; | |||
4260 | } | |||
4261 | if (!Ty || !Size) | |||
4262 | return error("Invalid record"); | |||
4263 | ||||
4264 | // FIXME: Make this an optional field. | |||
4265 | const DataLayout &DL = TheModule->getDataLayout(); | |||
4266 | unsigned AS = DL.getAllocaAddrSpace(); | |||
4267 | ||||
4268 | AllocaInst *AI = new AllocaInst(Ty, AS, Size, Align); | |||
4269 | AI->setUsedWithInAlloca(InAlloca); | |||
4270 | AI->setSwiftError(SwiftError); | |||
4271 | I = AI; | |||
4272 | InstructionList.push_back(I); | |||
4273 | break; | |||
4274 | } | |||
4275 | case bitc::FUNC_CODE_INST_LOAD: { // LOAD: [opty, op, align, vol] | |||
4276 | unsigned OpNum = 0; | |||
4277 | Value *Op; | |||
4278 | if (getValueTypePair(Record, OpNum, NextValueNo, Op) || | |||
4279 | (OpNum + 2 != Record.size() && OpNum + 3 != Record.size())) | |||
4280 | return error("Invalid record"); | |||
4281 | ||||
4282 | Type *Ty = nullptr; | |||
4283 | if (OpNum + 3 == Record.size()) | |||
4284 | Ty = getTypeByID(Record[OpNum++]); | |||
4285 | if (Error Err = typeCheckLoadStoreInst(Ty, Op->getType())) | |||
4286 | return Err; | |||
4287 | if (!Ty) | |||
4288 | Ty = cast<PointerType>(Op->getType())->getElementType(); | |||
4289 | ||||
4290 | unsigned Align; | |||
4291 | if (Error Err = parseAlignmentValue(Record[OpNum], Align)) | |||
4292 | return Err; | |||
4293 | I = new LoadInst(Ty, Op, "", Record[OpNum + 1], Align); | |||
4294 | ||||
4295 | InstructionList.push_back(I); | |||
4296 | break; | |||
4297 | } | |||
4298 | case bitc::FUNC_CODE_INST_LOADATOMIC: { | |||
4299 | // LOADATOMIC: [opty, op, align, vol, ordering, ssid] | |||
4300 | unsigned OpNum = 0; | |||
4301 | Value *Op; | |||
4302 | if (getValueTypePair(Record, OpNum, NextValueNo, Op) || | |||
4303 | (OpNum + 4 != Record.size() && OpNum + 5 != Record.size())) | |||
4304 | return error("Invalid record"); | |||
4305 | ||||
4306 | Type *Ty = nullptr; | |||
4307 | if (OpNum + 5 == Record.size()) | |||
4308 | Ty = getTypeByID(Record[OpNum++]); | |||
4309 | if (Error Err = typeCheckLoadStoreInst(Ty, Op->getType())) | |||
4310 | return Err; | |||
4311 | if (!Ty) | |||
4312 | Ty = cast<PointerType>(Op->getType())->getElementType(); | |||
4313 | ||||
4314 | AtomicOrdering Ordering = getDecodedOrdering(Record[OpNum + 2]); | |||
4315 | if (Ordering == AtomicOrdering::NotAtomic || | |||
4316 | Ordering == AtomicOrdering::Release || | |||
4317 | Ordering == AtomicOrdering::AcquireRelease) | |||
4318 | return error("Invalid record"); | |||
4319 | if (Ordering != AtomicOrdering::NotAtomic && Record[OpNum] == 0) | |||
4320 | return error("Invalid record"); | |||
4321 | SyncScope::ID SSID = getDecodedSyncScopeID(Record[OpNum + 3]); | |||
4322 | ||||
4323 | unsigned Align; | |||
4324 | if (Error Err = parseAlignmentValue(Record[OpNum], Align)) | |||
4325 | return Err; | |||
4326 | I = new LoadInst(Op, "", Record[OpNum+1], Align, Ordering, SSID); | |||
4327 | ||||
4328 | InstructionList.push_back(I); | |||
4329 | break; | |||
4330 | } | |||
4331 | case bitc::FUNC_CODE_INST_STORE: | |||
4332 | case bitc::FUNC_CODE_INST_STORE_OLD: { // STORE2:[ptrty, ptr, val, align, vol] | |||
4333 | unsigned OpNum = 0; | |||
4334 | Value *Val, *Ptr; | |||
4335 | if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) || | |||
4336 | (BitCode == bitc::FUNC_CODE_INST_STORE | |||
4337 | ? getValueTypePair(Record, OpNum, NextValueNo, Val) | |||
4338 | : popValue(Record, OpNum, NextValueNo, | |||
4339 | cast<PointerType>(Ptr->getType())->getElementType(), | |||
4340 | Val)) || | |||
4341 | OpNum + 2 != Record.size()) | |||
4342 | return error("Invalid record"); | |||
4343 | ||||
4344 | if (Error Err = typeCheckLoadStoreInst(Val->getType(), Ptr->getType())) | |||
4345 | return Err; | |||
4346 | unsigned Align; | |||
4347 | if (Error Err = parseAlignmentValue(Record[OpNum], Align)) | |||
4348 | return Err; | |||
4349 | I = new StoreInst(Val, Ptr, Record[OpNum+1], Align); | |||
4350 | InstructionList.push_back(I); | |||
4351 | break; | |||
4352 | } | |||
4353 | case bitc::FUNC_CODE_INST_STOREATOMIC: | |||
4354 | case bitc::FUNC_CODE_INST_STOREATOMIC_OLD: { | |||
4355 | // STOREATOMIC: [ptrty, ptr, val, align, vol, ordering, ssid] | |||
4356 | unsigned OpNum = 0; | |||
4357 | Value *Val, *Ptr; | |||
4358 | if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) || | |||
4359 | !isa<PointerType>(Ptr->getType()) || | |||
4360 | (BitCode == bitc::FUNC_CODE_INST_STOREATOMIC | |||
4361 | ? getValueTypePair(Record, OpNum, NextValueNo, Val) | |||
4362 | : popValue(Record, OpNum, NextValueNo, | |||
4363 | cast<PointerType>(Ptr->getType())->getElementType(), | |||
4364 | Val)) || | |||
4365 | OpNum + 4 != Record.size()) | |||
4366 | return error("Invalid record"); | |||
4367 | ||||
4368 | if (Error Err = typeCheckLoadStoreInst(Val->getType(), Ptr->getType())) | |||
4369 | return Err; | |||
4370 | AtomicOrdering Ordering = getDecodedOrdering(Record[OpNum + 2]); | |||
4371 | if (Ordering == AtomicOrdering::NotAtomic || | |||
4372 | Ordering == AtomicOrdering::Acquire || | |||
4373 | Ordering == AtomicOrdering::AcquireRelease) | |||
4374 | return error("Invalid record"); | |||
4375 | SyncScope::ID SSID = getDecodedSyncScopeID(Record[OpNum + 3]); | |||
4376 | if (Ordering != AtomicOrdering::NotAtomic && Record[OpNum] == 0) | |||
4377 | return error("Invalid record"); | |||
4378 | ||||
4379 | unsigned Align; | |||
4380 | if (Error Err = parseAlignmentValue(Record[OpNum], Align)) | |||
4381 | return Err; | |||
4382 | I = new StoreInst(Val, Ptr, Record[OpNum+1], Align, Ordering, SSID); | |||
4383 | InstructionList.push_back(I); | |||
4384 | break; | |||
4385 | } | |||
4386 | case bitc::FUNC_CODE_INST_CMPXCHG_OLD: | |||
4387 | case bitc::FUNC_CODE_INST_CMPXCHG: { | |||
4388 | // CMPXCHG:[ptrty, ptr, cmp, new, vol, successordering, ssid, | |||
4389 | // failureordering?, isweak?] | |||
4390 | unsigned OpNum = 0; | |||
4391 | Value *Ptr, *Cmp, *New; | |||
4392 | if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) || | |||
4393 | (BitCode == bitc::FUNC_CODE_INST_CMPXCHG | |||
4394 | ? getValueTypePair(Record, OpNum, NextValueNo, Cmp) | |||
4395 | : popValue(Record, OpNum, NextValueNo, | |||
4396 | cast<PointerType>(Ptr->getType())->getElementType(), | |||
4397 | Cmp)) || | |||
4398 | popValue(Record, OpNum, NextValueNo, Cmp->getType(), New) || | |||
4399 | Record.size() < OpNum + 3 || Record.size() > OpNum + 5) | |||
4400 | return error("Invalid record"); | |||
4401 | AtomicOrdering SuccessOrdering = getDecodedOrdering(Record[OpNum + 1]); | |||
4402 | if (SuccessOrdering == AtomicOrdering::NotAtomic || | |||
4403 | SuccessOrdering == AtomicOrdering::Unordered) | |||
4404 | return error("Invalid record"); | |||
4405 | SyncScope::ID SSID = getDecodedSyncScopeID(Record[OpNum + 2]); | |||
4406 | ||||
4407 | if (Error Err = typeCheckLoadStoreInst(Cmp->getType(), Ptr->getType())) | |||
4408 | return Err; | |||
4409 | AtomicOrdering FailureOrdering; | |||
4410 | if (Record.size() < 7) | |||
4411 | FailureOrdering = | |||
4412 | AtomicCmpXchgInst::getStrongestFailureOrdering(SuccessOrdering); | |||
4413 | else | |||
4414 | FailureOrdering = getDecodedOrdering(Record[OpNum + 3]); | |||
4415 | ||||
4416 | I = new AtomicCmpXchgInst(Ptr, Cmp, New, SuccessOrdering, FailureOrdering, | |||
4417 | SSID); | |||
4418 | cast<AtomicCmpXchgInst>(I)->setVolatile(Record[OpNum]); | |||
4419 | ||||
4420 | if (Record.size() < 8) { | |||
4421 | // Before weak cmpxchgs existed, the instruction simply returned the | |||
4422 | // value loaded from memory, so bitcode files from that era will be | |||
4423 | // expecting the first component of a modern cmpxchg. | |||
4424 | CurBB->getInstList().push_back(I); | |||
4425 | I = ExtractValueInst::Create(I, 0); | |||
4426 | } else { | |||
4427 | cast<AtomicCmpXchgInst>(I)->setWeak(Record[OpNum+4]); | |||
4428 | } | |||
4429 | ||||
4430 | InstructionList.push_back(I); | |||
4431 | break; | |||
4432 | } | |||
4433 | case bitc::FUNC_CODE_INST_ATOMICRMW: { | |||
4434 | // ATOMICRMW:[ptrty, ptr, val, op, vol, ordering, ssid] | |||
4435 | unsigned OpNum = 0; | |||
4436 | Value *Ptr, *Val; | |||
4437 | if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) || | |||
4438 | !isa<PointerType>(Ptr->getType()) || | |||
4439 | popValue(Record, OpNum, NextValueNo, | |||
4440 | cast<PointerType>(Ptr->getType())->getElementType(), Val) || | |||
4441 | OpNum+4 != Record.size()) | |||
4442 | return error("Invalid record"); | |||
4443 | AtomicRMWInst::BinOp Operation = getDecodedRMWOperation(Record[OpNum]); | |||
4444 | if (Operation < AtomicRMWInst::FIRST_BINOP || | |||
4445 | Operation > AtomicRMWInst::LAST_BINOP) | |||
4446 | return error("Invalid record"); | |||
4447 | AtomicOrdering Ordering = getDecodedOrdering(Record[OpNum + 2]); | |||
4448 | if (Ordering == AtomicOrdering::NotAtomic || | |||
4449 | Ordering == AtomicOrdering::Unordered) | |||
4450 | return error("Invalid record"); | |||
4451 | SyncScope::ID SSID = getDecodedSyncScopeID(Record[OpNum + 3]); | |||
4452 | I = new AtomicRMWInst(Operation, Ptr, Val, Ordering, SSID); | |||
4453 | cast<AtomicRMWInst>(I)->setVolatile(Record[OpNum+1]); | |||
4454 | InstructionList.push_back(I); | |||
4455 | break; | |||
4456 | } | |||
4457 | case bitc::FUNC_CODE_INST_FENCE: { // FENCE:[ordering, ssid] | |||
4458 | if (2 != Record.size()) | |||
4459 | return error("Invalid record"); | |||
4460 | AtomicOrdering Ordering = getDecodedOrdering(Record[0]); | |||
4461 | if (Ordering == AtomicOrdering::NotAtomic || | |||
4462 | Ordering == AtomicOrdering::Unordered || | |||
4463 | Ordering == AtomicOrdering::Monotonic) | |||
4464 | return error("Invalid record"); | |||
4465 | SyncScope::ID SSID = getDecodedSyncScopeID(Record[1]); | |||
4466 | I = new FenceInst(Context, Ordering, SSID); | |||
4467 | InstructionList.push_back(I); | |||
4468 | break; | |||
4469 | } | |||
4470 | case bitc::FUNC_CODE_INST_CALL: { | |||
4471 | // CALL: [paramattrs, cc, fmf, fnty, fnid, arg0, arg1...] | |||
4472 | if (Record.size() < 3) | |||
4473 | return error("Invalid record"); | |||
4474 | ||||
4475 | unsigned OpNum = 0; | |||
4476 | AttributeList PAL = getAttributes(Record[OpNum++]); | |||
4477 | unsigned CCInfo = Record[OpNum++]; | |||
4478 | ||||
4479 | FastMathFlags FMF; | |||
4480 | if ((CCInfo >> bitc::CALL_FMF) & 1) { | |||
4481 | FMF = getDecodedFastMathFlags(Record[OpNum++]); | |||
4482 | if (!FMF.any()) | |||
4483 | return error("Fast math flags indicator set for call with no FMF"); | |||
4484 | } | |||
4485 | ||||
4486 | FunctionType *FTy = nullptr; | |||
4487 | if (CCInfo >> bitc::CALL_EXPLICIT_TYPE & 1 && | |||
4488 | !(FTy = dyn_cast<FunctionType>(getTypeByID(Record[OpNum++])))) | |||
4489 | return error("Explicit call type is not a function type"); | |||
4490 | ||||
4491 | Value *Callee; | |||
4492 | if (getValueTypePair(Record, OpNum, NextValueNo, Callee)) | |||
4493 | return error("Invalid record"); | |||
4494 | ||||
4495 | PointerType *OpTy = dyn_cast<PointerType>(Callee->getType()); | |||
4496 | if (!OpTy) | |||
4497 | return error("Callee is not a pointer type"); | |||
4498 | if (!FTy) { | |||
4499 | FTy = dyn_cast<FunctionType>(OpTy->getElementType()); | |||
4500 | if (!FTy) | |||
4501 | return error("Callee is not of pointer to function type"); | |||
4502 | } else if (OpTy->getElementType() != FTy) | |||
4503 | return error("Explicit call type does not match pointee type of " | |||
4504 | "callee operand"); | |||
4505 | if (Record.size() < FTy->getNumParams() + OpNum) | |||
4506 | return error("Insufficient operands to call"); | |||
4507 | ||||
4508 | SmallVector<Value*, 16> Args; | |||
4509 | // Read the fixed params. | |||
4510 | for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i, ++OpNum) { | |||
4511 | if (FTy->getParamType(i)->isLabelTy()) | |||
4512 | Args.push_back(getBasicBlock(Record[OpNum])); | |||
4513 | else | |||
4514 | Args.push_back(getValue(Record, OpNum, NextValueNo, | |||
4515 | FTy->getParamType(i))); | |||
4516 | if (!Args.back()) | |||
4517 | return error("Invalid record"); | |||
4518 | } | |||
4519 | ||||
4520 | // Read type/value pairs for varargs params. | |||
4521 | if (!FTy->isVarArg()) { | |||
4522 | if (OpNum != Record.size()) | |||
4523 | return error("Invalid record"); | |||
4524 | } else { | |||
4525 | while (OpNum != Record.size()) { | |||
4526 | Value *Op; | |||
4527 | if (getValueTypePair(Record, OpNum, NextValueNo, Op)) | |||
4528 | return error("Invalid record"); | |||
4529 | Args.push_back(Op); | |||
4530 | } | |||
4531 | } | |||
4532 | ||||
4533 | I = CallInst::Create(FTy, Callee, Args, OperandBundles); | |||
4534 | OperandBundles.clear(); | |||
4535 | InstructionList.push_back(I); | |||
4536 | cast<CallInst>(I)->setCallingConv( | |||
4537 | static_cast<CallingConv::ID>((0x7ff & CCInfo) >> bitc::CALL_CCONV)); | |||
4538 | CallInst::TailCallKind TCK = CallInst::TCK_None; | |||
4539 | if (CCInfo & 1 << bitc::CALL_TAIL) | |||
4540 | TCK = CallInst::TCK_Tail; | |||
4541 | if (CCInfo & (1 << bitc::CALL_MUSTTAIL)) | |||
4542 | TCK = CallInst::TCK_MustTail; | |||
4543 | if (CCInfo & (1 << bitc::CALL_NOTAIL)) | |||
4544 | TCK = CallInst::TCK_NoTail; | |||
4545 | cast<CallInst>(I)->setTailCallKind(TCK); | |||
4546 | cast<CallInst>(I)->setAttributes(PAL); | |||
4547 | if (FMF.any()) { | |||
4548 | if (!isa<FPMathOperator>(I)) | |||
4549 | return error("Fast-math-flags specified for call without " | |||
4550 | "floating-point scalar or vector return type"); | |||
4551 | I->setFastMathFlags(FMF); | |||
4552 | } | |||
4553 | break; | |||
4554 | } | |||
4555 | case bitc::FUNC_CODE_INST_VAARG: { // VAARG: [valistty, valist, instty] | |||
4556 | if (Record.size() < 3) | |||
4557 | return error("Invalid record"); | |||
4558 | Type *OpTy = getTypeByID(Record[0]); | |||
4559 | Value *Op = getValue(Record, 1, NextValueNo, OpTy); | |||
4560 | Type *ResTy = getTypeByID(Record[2]); | |||
4561 | if (!OpTy || !Op || !ResTy) | |||
4562 | return error("Invalid record"); | |||
4563 | I = new VAArgInst(Op, ResTy); | |||
4564 | InstructionList.push_back(I); | |||
4565 | break; | |||
4566 | } | |||
4567 | ||||
4568 | case bitc::FUNC_CODE_OPERAND_BUNDLE: { | |||
4569 | // A call or an invoke can be optionally prefixed with some variable | |||
4570 | // number of operand bundle blocks. These blocks are read into | |||
4571 | // OperandBundles and consumed at the next call or invoke instruction. | |||
4572 | ||||
4573 | if (Record.size() < 1 || Record[0] >= BundleTags.size()) | |||
4574 | return error("Invalid record"); | |||
4575 | ||||
4576 | std::vector<Value *> Inputs; | |||
4577 | ||||
4578 | unsigned OpNum = 1; | |||
4579 | while (OpNum != Record.size()) { | |||
4580 | Value *Op; | |||
4581 | if (getValueTypePair(Record, OpNum, NextValueNo, Op)) | |||
4582 | return error("Invalid record"); | |||
4583 | Inputs.push_back(Op); | |||
4584 | } | |||
4585 | ||||
4586 | OperandBundles.emplace_back(BundleTags[Record[0]], std::move(Inputs)); | |||
4587 | continue; | |||
4588 | } | |||
4589 | } | |||
4590 | ||||
4591 | // Add instruction to end of current BB. If there is no current BB, reject | |||
4592 | // this file. | |||
4593 | if (!CurBB) { | |||
4594 | I->deleteValue(); | |||
4595 | return error("Invalid instruction with no BB"); | |||
4596 | } | |||
4597 | if (!OperandBundles.empty()) { | |||
4598 | I->deleteValue(); | |||
4599 | return error("Operand bundles found with no consumer"); | |||
4600 | } | |||
4601 | CurBB->getInstList().push_back(I); | |||
4602 | ||||
4603 | // If this was a terminator instruction, move to the next block. | |||
4604 | if (isa<TerminatorInst>(I)) { | |||
4605 | ++CurBBNo; | |||
4606 | CurBB = CurBBNo < FunctionBBs.size() ? FunctionBBs[CurBBNo] : nullptr; | |||
4607 | } | |||
4608 | ||||
4609 | // Non-void values get registered in the value table for future use. | |||
4610 | if (I && !I->getType()->isVoidTy()) | |||
4611 | ValueList.assignValue(I, NextValueNo++); | |||
4612 | } | |||
4613 | ||||
4614 | OutOfRecordLoop: | |||
4615 | ||||
4616 | if (!OperandBundles.empty()) | |||
4617 | return error("Operand bundles found with no consumer"); | |||
4618 | ||||
4619 | // Check the function list for unresolved values. | |||
4620 | if (Argument *A = dyn_cast<Argument>(ValueList.back())) { | |||
4621 | if (!A->getParent()) { | |||
4622 | // We found at least one unresolved value. Nuke them all to avoid leaks. | |||
4623 | for (unsigned i = ModuleValueListSize, e = ValueList.size(); i != e; ++i){ | |||
4624 | if ((A = dyn_cast_or_null<Argument>(ValueList[i])) && !A->getParent()) { | |||
4625 | A->replaceAllUsesWith(UndefValue::get(A->getType())); | |||
4626 | delete A; | |||
4627 | } | |||
4628 | } | |||
4629 | return error("Never resolved value found in function"); | |||
4630 | } | |||
4631 | } | |||
4632 | ||||
4633 | // Unexpected unresolved metadata about to be dropped. | |||
4634 | if (MDLoader->hasFwdRefs()) | |||
4635 | return error("Invalid function metadata: outgoing forward refs"); | |||
4636 | ||||
4637 | // Trim the value list down to the size it was before we parsed this function. | |||
4638 | ValueList.shrinkTo(ModuleValueListSize); | |||
4639 | MDLoader->shrinkTo(ModuleMDLoaderSize); | |||
4640 | std::vector<BasicBlock*>().swap(FunctionBBs); | |||
4641 | return Error::success(); | |||
4642 | } | |||
4643 | ||||
4644 | /// Find the function body in the bitcode stream | |||
4645 | Error BitcodeReader::findFunctionInStream( | |||
4646 | Function *F, | |||
4647 | DenseMap<Function *, uint64_t>::iterator DeferredFunctionInfoIterator) { | |||
4648 | while (DeferredFunctionInfoIterator->second == 0) { | |||
4649 | // This is the fallback handling for the old format bitcode that | |||
4650 | // didn't contain the function index in the VST, or when we have | |||
4651 | // an anonymous function which would not have a VST entry. | |||
4652 | // Assert that we have one of those two cases. | |||
4653 | assert(VSTOffset == 0 || !F->hasName())(static_cast <bool> (VSTOffset == 0 || !F->hasName() ) ? void (0) : __assert_fail ("VSTOffset == 0 || !F->hasName()" , "/build/llvm-toolchain-snapshot-7~svn329677/lib/Bitcode/Reader/BitcodeReader.cpp" , 4653, __extension__ __PRETTY_FUNCTION__)); | |||
4654 | // Parse the next body in the stream and set its position in the | |||
4655 | // DeferredFunctionInfo map. | |||
4656 | if (Error Err = rememberAndSkipFunctionBodies()) | |||
4657 | return Err; | |||
4658 | } | |||
4659 | return Error::success(); | |||
4660 | } | |||
4661 | ||||
4662 | SyncScope::ID BitcodeReader::getDecodedSyncScopeID(unsigned Val) { | |||
4663 | if (Val == SyncScope::SingleThread || Val == SyncScope::System) | |||
4664 | return SyncScope::ID(Val); | |||
4665 | if (Val >= SSIDs.size()) | |||
4666 | return SyncScope::System; // Map unknown synchronization scopes to system. | |||
4667 | return SSIDs[Val]; | |||
4668 | } | |||
4669 | ||||
4670 | //===----------------------------------------------------------------------===// | |||
4671 | // GVMaterializer implementation | |||
4672 | //===----------------------------------------------------------------------===// | |||
4673 | ||||
4674 | Error BitcodeReader::materialize(GlobalValue *GV) { | |||
4675 | Function *F = dyn_cast<Function>(GV); | |||
4676 | // If it's not a function or is already material, ignore the request. | |||
4677 | if (!F || !F->isMaterializable()) | |||
4678 | return Error::success(); | |||
4679 | ||||
4680 | DenseMap<Function*, uint64_t>::iterator DFII = DeferredFunctionInfo.find(F); | |||
4681 | assert(DFII != DeferredFunctionInfo.end() && "Deferred function not found!")(static_cast <bool> (DFII != DeferredFunctionInfo.end() && "Deferred function not found!") ? void (0) : __assert_fail ("DFII != DeferredFunctionInfo.end() && \"Deferred function not found!\"" , "/build/llvm-toolchain-snapshot-7~svn329677/lib/Bitcode/Reader/BitcodeReader.cpp" , 4681, __extension__ __PRETTY_FUNCTION__)); | |||
4682 | // If its position is recorded as 0, its body is somewhere in the stream | |||
4683 | // but we haven't seen it yet. | |||
4684 | if (DFII->second == 0) | |||
4685 | if (Error Err = findFunctionInStream(F, DFII)) | |||
4686 | return Err; | |||
4687 | ||||
4688 | // Materialize metadata before parsing any function bodies. | |||
4689 | if (Error Err = materializeMetadata()) | |||
4690 | return Err; | |||
4691 | ||||
4692 | // Move the bit stream to the saved position of the deferred function body. | |||
4693 | Stream.JumpToBit(DFII->second); | |||
4694 | ||||
4695 | if (Error Err = parseFunctionBody(F)) | |||
4696 | return Err; | |||
4697 | F->setIsMaterializable(false); | |||
4698 | ||||
4699 | if (StripDebugInfo) | |||
4700 | stripDebugInfo(*F); | |||
4701 | ||||
4702 | // Upgrade any old intrinsic calls in the function. | |||
4703 | for (auto &I : UpgradedIntrinsics) { | |||
4704 | for (auto UI = I.first->materialized_user_begin(), UE = I.first->user_end(); | |||
4705 | UI != UE;) { | |||
4706 | User *U = *UI; | |||
4707 | ++UI; | |||
4708 | if (CallInst *CI = dyn_cast<CallInst>(U)) | |||
4709 | UpgradeIntrinsicCall(CI, I.second); | |||
4710 | } | |||
4711 | } | |||
4712 | ||||
4713 | // Update calls to the remangled intrinsics | |||
4714 | for (auto &I : RemangledIntrinsics) | |||
4715 | for (auto UI = I.first->materialized_user_begin(), UE = I.first->user_end(); | |||
4716 | UI != UE;) | |||
4717 | // Don't expect any other users than call sites | |||
4718 | CallSite(*UI++).setCalledFunction(I.second); | |||
4719 | ||||
4720 | // Finish fn->subprogram upgrade for materialized functions. | |||
4721 | if (DISubprogram *SP = MDLoader->lookupSubprogramForFunction(F)) | |||
4722 | F->setSubprogram(SP); | |||
4723 | ||||
4724 | // Check if the TBAA Metadata are valid, otherwise we will need to strip them. | |||
4725 | if (!MDLoader->isStrippingTBAA()) { | |||
4726 | for (auto &I : instructions(F)) { | |||
4727 | MDNode *TBAA = I.getMetadata(LLVMContext::MD_tbaa); | |||
4728 | if (!TBAA || TBAAVerifyHelper.visitTBAAMetadata(I, TBAA)) | |||
4729 | continue; | |||
4730 | MDLoader->setStripTBAA(true); | |||
4731 | stripTBAA(F->getParent()); | |||
4732 | } | |||
4733 | } | |||
4734 | ||||
4735 | // Bring in any functions that this function forward-referenced via | |||
4736 | // blockaddresses. | |||
4737 | return materializeForwardReferencedFunctions(); | |||
4738 | } | |||
4739 | ||||
4740 | Error BitcodeReader::materializeModule() { | |||
4741 | if (Error Err = materializeMetadata()) | |||
4742 | return Err; | |||
4743 | ||||
4744 | // Promise to materialize all forward references. | |||
4745 | WillMaterializeAllForwardRefs = true; | |||
4746 | ||||
4747 | // Iterate over the module, deserializing any functions that are still on | |||
4748 | // disk. | |||
4749 | for (Function &F : *TheModule) { | |||
4750 | if (Error Err = materialize(&F)) | |||
4751 | return Err; | |||
4752 | } | |||
4753 | // At this point, if there are any function bodies, parse the rest of | |||
4754 | // the bits in the module past the last function block we have recorded | |||
4755 | // through either lazy scanning or the VST. | |||
4756 | if (LastFunctionBlockBit || NextUnreadBit) | |||
4757 | if (Error Err = parseModule(LastFunctionBlockBit > NextUnreadBit | |||
4758 | ? LastFunctionBlockBit | |||
4759 | : NextUnreadBit)) | |||
4760 | return Err; | |||
4761 | ||||
4762 | // Check that all block address forward references got resolved (as we | |||
4763 | // promised above). | |||
4764 | if (!BasicBlockFwdRefs.empty()) | |||
4765 | return error("Never resolved function from blockaddress"); | |||
4766 | ||||
4767 | // Upgrade any intrinsic calls that slipped through (should not happen!) and | |||
4768 | // delete the old functions to clean up. We can't do this unless the entire | |||
4769 | // module is materialized because there could always be another function body | |||
4770 | // with calls to the old function. | |||
4771 | for (auto &I : UpgradedIntrinsics) { | |||
4772 | for (auto *U : I.first->users()) { | |||
4773 | if (CallInst *CI = dyn_cast<CallInst>(U)) | |||
4774 | UpgradeIntrinsicCall(CI, I.second); | |||
4775 | } | |||
4776 | if (!I.first->use_empty()) | |||
4777 | I.first->replaceAllUsesWith(I.second); | |||
4778 | I.first->eraseFromParent(); | |||
4779 | } | |||
4780 | UpgradedIntrinsics.clear(); | |||
4781 | // Do the same for remangled intrinsics | |||
4782 | for (auto &I : RemangledIntrinsics) { | |||
4783 | I.first->replaceAllUsesWith(I.second); | |||
4784 | I.first->eraseFromParent(); | |||
4785 | } | |||
4786 | RemangledIntrinsics.clear(); | |||
4787 | ||||
4788 | UpgradeDebugInfo(*TheModule); | |||
4789 | ||||
4790 | UpgradeModuleFlags(*TheModule); | |||
4791 | ||||
4792 | UpgradeRetainReleaseMarker(*TheModule); | |||
4793 | ||||
4794 | return Error::success(); | |||
4795 | } | |||
4796 | ||||
4797 | std::vector<StructType *> BitcodeReader::getIdentifiedStructTypes() const { | |||
4798 | return IdentifiedStructTypes; | |||
4799 | } | |||
4800 | ||||
4801 | ModuleSummaryIndexBitcodeReader::ModuleSummaryIndexBitcodeReader( | |||
4802 | BitstreamCursor Cursor, StringRef Strtab, ModuleSummaryIndex &TheIndex, | |||
4803 | StringRef ModulePath, unsigned ModuleId) | |||
4804 | : BitcodeReaderBase(std::move(Cursor), Strtab), TheIndex(TheIndex), | |||
4805 | ModulePath(ModulePath), ModuleId(ModuleId) {} | |||
4806 | ||||
4807 | ModuleSummaryIndex::ModuleInfo * | |||
4808 | ModuleSummaryIndexBitcodeReader::addThisModule() { | |||
4809 | return TheIndex.addModule(ModulePath, ModuleId); | |||
4810 | } | |||
4811 | ||||
4812 | std::pair<ValueInfo, GlobalValue::GUID> | |||
4813 | ModuleSummaryIndexBitcodeReader::getValueInfoFromValueId(unsigned ValueId) { | |||
4814 | auto VGI = ValueIdToValueInfoMap[ValueId]; | |||
4815 | assert(VGI.first)(static_cast <bool> (VGI.first) ? void (0) : __assert_fail ("VGI.first", "/build/llvm-toolchain-snapshot-7~svn329677/lib/Bitcode/Reader/BitcodeReader.cpp" , 4815, __extension__ __PRETTY_FUNCTION__)); | |||
4816 | return VGI; | |||
4817 | } | |||
4818 | ||||
4819 | void ModuleSummaryIndexBitcodeReader::setValueGUID( | |||
4820 | uint64_t ValueID, StringRef ValueName, GlobalValue::LinkageTypes Linkage, | |||
4821 | StringRef SourceFileName) { | |||
4822 | std::string GlobalId = | |||
4823 | GlobalValue::getGlobalIdentifier(ValueName, Linkage, SourceFileName); | |||
4824 | auto ValueGUID = GlobalValue::getGUID(GlobalId); | |||
4825 | auto OriginalNameID = ValueGUID; | |||
4826 | if (GlobalValue::isLocalLinkage(Linkage)) | |||
4827 | OriginalNameID = GlobalValue::getGUID(ValueName); | |||
4828 | if (PrintSummaryGUIDs) | |||
4829 | dbgs() << "GUID " << ValueGUID << "(" << OriginalNameID << ") is " | |||
4830 | << ValueName << "\n"; | |||
4831 | ||||
4832 | // UseStrtab is false for legacy summary formats and value names are | |||
4833 | // created on stack. We can't use them outside of parseValueSymbolTable. | |||
4834 | ValueIdToValueInfoMap[ValueID] = std::make_pair( | |||
4835 | TheIndex.getOrInsertValueInfo(ValueGUID, UseStrtab ? ValueName : ""), | |||
4836 | OriginalNameID); | |||
4837 | } | |||
4838 | ||||
4839 | // Specialized value symbol table parser used when reading module index | |||
4840 | // blocks where we don't actually create global values. The parsed information | |||
4841 | // is saved in the bitcode reader for use when later parsing summaries. | |||
4842 | Error ModuleSummaryIndexBitcodeReader::parseValueSymbolTable( | |||
4843 | uint64_t Offset, | |||
4844 | DenseMap<unsigned, GlobalValue::LinkageTypes> &ValueIdToLinkageMap) { | |||
4845 | // With a strtab the VST is not required to parse the summary. | |||
4846 | if (UseStrtab) | |||
4847 | return Error::success(); | |||
4848 | ||||
4849 | assert(Offset > 0 && "Expected non-zero VST offset")(static_cast <bool> (Offset > 0 && "Expected non-zero VST offset" ) ? void (0) : __assert_fail ("Offset > 0 && \"Expected non-zero VST offset\"" , "/build/llvm-toolchain-snapshot-7~svn329677/lib/Bitcode/Reader/BitcodeReader.cpp" , 4849, __extension__ __PRETTY_FUNCTION__)); | |||
4850 | uint64_t CurrentBit = jumpToValueSymbolTable(Offset, Stream); | |||
4851 | ||||
4852 | if (Stream.EnterSubBlock(bitc::VALUE_SYMTAB_BLOCK_ID)) | |||
4853 | return error("Invalid record"); | |||
4854 | ||||
4855 | SmallVector<uint64_t, 64> Record; | |||
4856 | ||||
4857 | // Read all the records for this value table. | |||
4858 | SmallString<128> ValueName; | |||
4859 | ||||
4860 | while (true) { | |||
4861 | BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); | |||
4862 | ||||
4863 | switch (Entry.Kind) { | |||
4864 | case BitstreamEntry::SubBlock: // Handled for us already. | |||
4865 | case BitstreamEntry::Error: | |||
4866 | return error("Malformed block"); | |||
4867 | case BitstreamEntry::EndBlock: | |||
4868 | // Done parsing VST, jump back to wherever we came from. | |||
4869 | Stream.JumpToBit(CurrentBit); | |||
4870 | return Error::success(); | |||
4871 | case BitstreamEntry::Record: | |||
4872 | // The interesting case. | |||
4873 | break; | |||
4874 | } | |||
4875 | ||||
4876 | // Read a record. | |||
4877 | Record.clear(); | |||
4878 | switch (Stream.readRecord(Entry.ID, Record)) { | |||
4879 | default: // Default behavior: ignore (e.g. VST_CODE_BBENTRY records). | |||
4880 | break; | |||
4881 | case bitc::VST_CODE_ENTRY: { // VST_CODE_ENTRY: [valueid, namechar x N] | |||
4882 | if (convertToString(Record, 1, ValueName)) | |||
4883 | return error("Invalid record"); | |||
4884 | unsigned ValueID = Record[0]; | |||
4885 | assert(!SourceFileName.empty())(static_cast <bool> (!SourceFileName.empty()) ? void (0 ) : __assert_fail ("!SourceFileName.empty()", "/build/llvm-toolchain-snapshot-7~svn329677/lib/Bitcode/Reader/BitcodeReader.cpp" , 4885, __extension__ __PRETTY_FUNCTION__)); | |||
4886 | auto VLI = ValueIdToLinkageMap.find(ValueID); | |||
4887 | assert(VLI != ValueIdToLinkageMap.end() &&(static_cast <bool> (VLI != ValueIdToLinkageMap.end() && "No linkage found for VST entry?") ? void (0) : __assert_fail ("VLI != ValueIdToLinkageMap.end() && \"No linkage found for VST entry?\"" , "/build/llvm-toolchain-snapshot-7~svn329677/lib/Bitcode/Reader/BitcodeReader.cpp" , 4888, __extension__ __PRETTY_FUNCTION__)) | |||
4888 | "No linkage found for VST entry?")(static_cast <bool> (VLI != ValueIdToLinkageMap.end() && "No linkage found for VST entry?") ? void (0) : __assert_fail ("VLI != ValueIdToLinkageMap.end() && \"No linkage found for VST entry?\"" , "/build/llvm-toolchain-snapshot-7~svn329677/lib/Bitcode/Reader/BitcodeReader.cpp" , 4888, __extension__ __PRETTY_FUNCTION__)); | |||
4889 | auto Linkage = VLI->second; | |||
4890 | setValueGUID(ValueID, ValueName, Linkage, SourceFileName); | |||
4891 | ValueName.clear(); | |||
4892 | break; | |||
4893 | } | |||
4894 | case bitc::VST_CODE_FNENTRY: { | |||
4895 | // VST_CODE_FNENTRY: [valueid, offset, namechar x N] | |||
4896 | if (convertToString(Record, 2, ValueName)) | |||
4897 | return error("Invalid record"); | |||
4898 | unsigned ValueID = Record[0]; | |||
4899 | assert(!SourceFileName.empty())(static_cast <bool> (!SourceFileName.empty()) ? void (0 ) : __assert_fail ("!SourceFileName.empty()", "/build/llvm-toolchain-snapshot-7~svn329677/lib/Bitcode/Reader/BitcodeReader.cpp" , 4899, __extension__ __PRETTY_FUNCTION__)); | |||
4900 | auto VLI = ValueIdToLinkageMap.find(ValueID); | |||
4901 | assert(VLI != ValueIdToLinkageMap.end() &&(static_cast <bool> (VLI != ValueIdToLinkageMap.end() && "No linkage found for VST entry?") ? void (0) : __assert_fail ("VLI != ValueIdToLinkageMap.end() && \"No linkage found for VST entry?\"" , "/build/llvm-toolchain-snapshot-7~svn329677/lib/Bitcode/Reader/BitcodeReader.cpp" , 4902, __extension__ __PRETTY_FUNCTION__)) | |||
4902 | "No linkage found for VST entry?")(static_cast <bool> (VLI != ValueIdToLinkageMap.end() && "No linkage found for VST entry?") ? void (0) : __assert_fail ("VLI != ValueIdToLinkageMap.end() && \"No linkage found for VST entry?\"" , "/build/llvm-toolchain-snapshot-7~svn329677/lib/Bitcode/Reader/BitcodeReader.cpp" , 4902, __extension__ __PRETTY_FUNCTION__)); | |||
4903 | auto Linkage = VLI->second; | |||
4904 | setValueGUID(ValueID, ValueName, Linkage, SourceFileName); | |||
4905 | ValueName.clear(); | |||
4906 | break; | |||
4907 | } | |||
4908 | case bitc::VST_CODE_COMBINED_ENTRY: { | |||
4909 | // VST_CODE_COMBINED_ENTRY: [valueid, refguid] | |||
4910 | unsigned ValueID = Record[0]; | |||
4911 | GlobalValue::GUID RefGUID = Record[1]; | |||
4912 | // The "original name", which is the second value of the pair will be | |||
4913 | // overriden later by a FS_COMBINED_ORIGINAL_NAME in the combined index. | |||
4914 | ValueIdToValueInfoMap[ValueID] = | |||
4915 | std::make_pair(TheIndex.getOrInsertValueInfo(RefGUID), RefGUID); | |||
4916 | break; | |||
4917 | } | |||
4918 | } | |||
4919 | } | |||
4920 | } | |||
4921 | ||||
4922 | // Parse just the blocks needed for building the index out of the module. | |||
4923 | // At the end of this routine the module Index is populated with a map | |||
4924 | // from global value id to GlobalValueSummary objects. | |||
4925 | Error ModuleSummaryIndexBitcodeReader::parseModule() { | |||
4926 | if (Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID)) | |||
4927 | return error("Invalid record"); | |||
4928 | ||||
4929 | SmallVector<uint64_t, 64> Record; | |||
4930 | DenseMap<unsigned, GlobalValue::LinkageTypes> ValueIdToLinkageMap; | |||
4931 | unsigned ValueId = 0; | |||
4932 | ||||
4933 | // Read the index for this module. | |||
4934 | while (true) { | |||
4935 | BitstreamEntry Entry = Stream.advance(); | |||
4936 | ||||
4937 | switch (Entry.Kind) { | |||
4938 | case BitstreamEntry::Error: | |||
4939 | return error("Malformed block"); | |||
4940 | case BitstreamEntry::EndBlock: | |||
4941 | return Error::success(); | |||
4942 | ||||
4943 | case BitstreamEntry::SubBlock: | |||
4944 | switch (Entry.ID) { | |||
4945 | default: // Skip unknown content. | |||
4946 | if (Stream.SkipBlock()) | |||
4947 | return error("Invalid record"); | |||
4948 | break; | |||
4949 | case bitc::BLOCKINFO_BLOCK_ID: | |||
4950 | // Need to parse these to get abbrev ids (e.g. for VST) | |||
4951 | if (readBlockInfo()) | |||
4952 | return error("Malformed block"); | |||
4953 | break; | |||
4954 | case bitc::VALUE_SYMTAB_BLOCK_ID: | |||
4955 | // Should have been parsed earlier via VSTOffset, unless there | |||
4956 | // is no summary section. | |||
4957 | assert(((SeenValueSymbolTable && VSTOffset > 0) ||(static_cast <bool> (((SeenValueSymbolTable && VSTOffset > 0) || !SeenGlobalValSummary) && "Expected early VST parse via VSTOffset record" ) ? void (0) : __assert_fail ("((SeenValueSymbolTable && VSTOffset > 0) || !SeenGlobalValSummary) && \"Expected early VST parse via VSTOffset record\"" , "/build/llvm-toolchain-snapshot-7~svn329677/lib/Bitcode/Reader/BitcodeReader.cpp" , 4959, __extension__ __PRETTY_FUNCTION__)) | |||
4958 | !SeenGlobalValSummary) &&(static_cast <bool> (((SeenValueSymbolTable && VSTOffset > 0) || !SeenGlobalValSummary) && "Expected early VST parse via VSTOffset record" ) ? void (0) : __assert_fail ("((SeenValueSymbolTable && VSTOffset > 0) || !SeenGlobalValSummary) && \"Expected early VST parse via VSTOffset record\"" , "/build/llvm-toolchain-snapshot-7~svn329677/lib/Bitcode/Reader/BitcodeReader.cpp" , 4959, __extension__ __PRETTY_FUNCTION__)) | |||
4959 | "Expected early VST parse via VSTOffset record")(static_cast <bool> (((SeenValueSymbolTable && VSTOffset > 0) || !SeenGlobalValSummary) && "Expected early VST parse via VSTOffset record" ) ? void (0) : __assert_fail ("((SeenValueSymbolTable && VSTOffset > 0) || !SeenGlobalValSummary) && \"Expected early VST parse via VSTOffset record\"" , "/build/llvm-toolchain-snapshot-7~svn329677/lib/Bitcode/Reader/BitcodeReader.cpp" , 4959, __extension__ __PRETTY_FUNCTION__)); | |||
4960 | if (Stream.SkipBlock()) | |||
4961 | return error("Invalid record"); | |||
4962 | break; | |||
4963 | case bitc::GLOBALVAL_SUMMARY_BLOCK_ID: | |||
4964 | case bitc::FULL_LTO_GLOBALVAL_SUMMARY_BLOCK_ID: | |||
4965 | assert(!SeenValueSymbolTable &&(static_cast <bool> (!SeenValueSymbolTable && "Already read VST when parsing summary block?" ) ? void (0) : __assert_fail ("!SeenValueSymbolTable && \"Already read VST when parsing summary block?\"" , "/build/llvm-toolchain-snapshot-7~svn329677/lib/Bitcode/Reader/BitcodeReader.cpp" , 4966, __extension__ __PRETTY_FUNCTION__)) | |||
4966 | "Already read VST when parsing summary block?")(static_cast <bool> (!SeenValueSymbolTable && "Already read VST when parsing summary block?" ) ? void (0) : __assert_fail ("!SeenValueSymbolTable && \"Already read VST when parsing summary block?\"" , "/build/llvm-toolchain-snapshot-7~svn329677/lib/Bitcode/Reader/BitcodeReader.cpp" , 4966, __extension__ __PRETTY_FUNCTION__)); | |||
4967 | // We might not have a VST if there were no values in the | |||
4968 | // summary. An empty summary block generated when we are | |||
4969 | // performing ThinLTO compiles so we don't later invoke | |||
4970 | // the regular LTO process on them. | |||
4971 | if (VSTOffset > 0) { | |||
4972 | if (Error Err = parseValueSymbolTable(VSTOffset, ValueIdToLinkageMap)) | |||
4973 | return Err; | |||
4974 | SeenValueSymbolTable = true; | |||
4975 | } | |||
4976 | SeenGlobalValSummary = true; | |||
4977 | if (Error Err = parseEntireSummary(Entry.ID)) | |||
4978 | return Err; | |||
4979 | break; | |||
4980 | case bitc::MODULE_STRTAB_BLOCK_ID: | |||
4981 | if (Error Err = parseModuleStringTable()) | |||
4982 | return Err; | |||
4983 | break; | |||
4984 | } | |||
4985 | continue; | |||
4986 | ||||
4987 | case BitstreamEntry::Record: { | |||
4988 | Record.clear(); | |||
4989 | auto BitCode = Stream.readRecord(Entry.ID, Record); | |||
4990 | switch (BitCode) { | |||
4991 | default: | |||
4992 | break; // Default behavior, ignore unknown content. | |||
4993 | case bitc::MODULE_CODE_VERSION: { | |||
4994 | if (Error Err = parseVersionRecord(Record).takeError()) | |||
4995 | return Err; | |||
4996 | break; | |||
4997 | } | |||
4998 | /// MODULE_CODE_SOURCE_FILENAME: [namechar x N] | |||
4999 | case bitc::MODULE_CODE_SOURCE_FILENAME: { | |||
5000 | SmallString<128> ValueName; | |||
5001 | if (convertToString(Record, 0, ValueName)) | |||
5002 | return error("Invalid record"); | |||
5003 | SourceFileName = ValueName.c_str(); | |||
5004 | break; | |||
5005 | } | |||
5006 | /// MODULE_CODE_HASH: [5*i32] | |||
5007 | case bitc::MODULE_CODE_HASH: { | |||
5008 | if (Record.size() != 5) | |||
5009 | return error("Invalid hash length " + Twine(Record.size()).str()); | |||
5010 | auto &Hash = addThisModule()->second.second; | |||
5011 | int Pos = 0; | |||
5012 | for (auto &Val : Record) { | |||
5013 | assert(!(Val >> 32) && "Unexpected high bits set")(static_cast <bool> (!(Val >> 32) && "Unexpected high bits set" ) ? void (0) : __assert_fail ("!(Val >> 32) && \"Unexpected high bits set\"" , "/build/llvm-toolchain-snapshot-7~svn329677/lib/Bitcode/Reader/BitcodeReader.cpp" , 5013, __extension__ __PRETTY_FUNCTION__)); | |||
5014 | Hash[Pos++] = Val; | |||
5015 | } | |||
5016 | break; | |||
5017 | } | |||
5018 | /// MODULE_CODE_VSTOFFSET: [offset] | |||
5019 | case bitc::MODULE_CODE_VSTOFFSET: | |||
5020 | if (Record.size() < 1) | |||
5021 | return error("Invalid record"); | |||
5022 | // Note that we subtract 1 here because the offset is relative to one | |||
5023 | // word before the start of the identification or module block, which | |||
5024 | // was historically always the start of the regular bitcode header. | |||
5025 | VSTOffset = Record[0] - 1; | |||
5026 | break; | |||
5027 | // v1 GLOBALVAR: [pointer type, isconst, initid, linkage, ...] | |||
5028 | // v1 FUNCTION: [type, callingconv, isproto, linkage, ...] | |||
5029 | // v1 ALIAS: [alias type, addrspace, aliasee val#, linkage, ...] | |||
5030 | // v2: [strtab offset, strtab size, v1] | |||
5031 | case bitc::MODULE_CODE_GLOBALVAR: | |||
5032 | case bitc::MODULE_CODE_FUNCTION: | |||
5033 | case bitc::MODULE_CODE_ALIAS: { | |||
5034 | StringRef Name; | |||
5035 | ArrayRef<uint64_t> GVRecord; | |||
5036 | std::tie(Name, GVRecord) = readNameFromStrtab(Record); | |||
5037 | if (GVRecord.size() <= 3) | |||
5038 | return error("Invalid record"); | |||
5039 | uint64_t RawLinkage = GVRecord[3]; | |||
5040 | GlobalValue::LinkageTypes Linkage = getDecodedLinkage(RawLinkage); | |||
5041 | if (!UseStrtab) { | |||
5042 | ValueIdToLinkageMap[ValueId++] = Linkage; | |||
5043 | break; | |||
5044 | } | |||
5045 | ||||
5046 | setValueGUID(ValueId++, Name, Linkage, SourceFileName); | |||
5047 | break; | |||
5048 | } | |||
5049 | } | |||
5050 | } | |||
5051 | continue; | |||
5052 | } | |||
5053 | } | |||
5054 | } | |||
5055 | ||||
5056 | std::vector<ValueInfo> | |||
5057 | ModuleSummaryIndexBitcodeReader::makeRefList(ArrayRef<uint64_t> Record) { | |||
5058 | std::vector<ValueInfo> Ret; | |||
5059 | Ret.reserve(Record.size()); | |||
5060 | for (uint64_t RefValueId : Record) | |||
5061 | Ret.push_back(getValueInfoFromValueId(RefValueId).first); | |||
5062 | return Ret; | |||
5063 | } | |||
5064 | ||||
5065 | std::vector<FunctionSummary::EdgeTy> | |||
5066 | ModuleSummaryIndexBitcodeReader::makeCallList(ArrayRef<uint64_t> Record, | |||
5067 | bool IsOldProfileFormat, | |||
5068 | bool HasProfile, bool HasRelBF) { | |||
5069 | std::vector<FunctionSummary::EdgeTy> Ret; | |||
5070 | Ret.reserve(Record.size()); | |||
5071 | for (unsigned I = 0, E = Record.size(); I != E; ++I) { | |||
5072 | CalleeInfo::HotnessType Hotness = CalleeInfo::HotnessType::Unknown; | |||
5073 | uint64_t RelBF = 0; | |||
5074 | ValueInfo Callee = getValueInfoFromValueId(Record[I]).first; | |||
5075 | if (IsOldProfileFormat) { | |||
5076 | I += 1; // Skip old callsitecount field | |||
5077 | if (HasProfile) | |||
5078 | I += 1; // Skip old profilecount field | |||
5079 | } else if (HasProfile) | |||
5080 | Hotness = static_cast<CalleeInfo::HotnessType>(Record[++I]); | |||
5081 | else if (HasRelBF) | |||
5082 | RelBF = Record[++I]; | |||
5083 | Ret.push_back(FunctionSummary::EdgeTy{Callee, CalleeInfo(Hotness, RelBF)}); | |||
5084 | } | |||
5085 | return Ret; | |||
5086 | } | |||
5087 | ||||
5088 | static void | |||
5089 | parseWholeProgramDevirtResolutionByArg(ArrayRef<uint64_t> Record, size_t &Slot, | |||
5090 | WholeProgramDevirtResolution &Wpd) { | |||
5091 | uint64_t ArgNum = Record[Slot++]; | |||
5092 | WholeProgramDevirtResolution::ByArg &B = | |||
5093 | Wpd.ResByArg[{Record.begin() + Slot, Record.begin() + Slot + ArgNum}]; | |||
5094 | Slot += ArgNum; | |||
5095 | ||||
5096 | B.TheKind = | |||
5097 | static_cast<WholeProgramDevirtResolution::ByArg::Kind>(Record[Slot++]); | |||
5098 | B.Info = Record[Slot++]; | |||
5099 | B.Byte = Record[Slot++]; | |||
5100 | B.Bit = Record[Slot++]; | |||
5101 | } | |||
5102 | ||||
5103 | static void parseWholeProgramDevirtResolution(ArrayRef<uint64_t> Record, | |||
5104 | StringRef Strtab, size_t &Slot, | |||
5105 | TypeIdSummary &TypeId) { | |||
5106 | uint64_t Id = Record[Slot++]; | |||
5107 | WholeProgramDevirtResolution &Wpd = TypeId.WPDRes[Id]; | |||
5108 | ||||
5109 | Wpd.TheKind = static_cast<WholeProgramDevirtResolution::Kind>(Record[Slot++]); | |||
5110 | Wpd.SingleImplName = {Strtab.data() + Record[Slot], | |||
5111 | static_cast<size_t>(Record[Slot + 1])}; | |||
5112 | Slot += 2; | |||
5113 | ||||
5114 | uint64_t ResByArgNum = Record[Slot++]; | |||
5115 | for (uint64_t I = 0; I != ResByArgNum; ++I) | |||
5116 | parseWholeProgramDevirtResolutionByArg(Record, Slot, Wpd); | |||
5117 | } | |||
5118 | ||||
5119 | static void parseTypeIdSummaryRecord(ArrayRef<uint64_t> Record, | |||
5120 | StringRef Strtab, | |||
5121 | ModuleSummaryIndex &TheIndex) { | |||
5122 | size_t Slot = 0; | |||
5123 | TypeIdSummary &TypeId = TheIndex.getOrInsertTypeIdSummary( | |||
5124 | {Strtab.data() + Record[Slot], static_cast<size_t>(Record[Slot + 1])}); | |||
5125 | Slot += 2; | |||
5126 | ||||
5127 | TypeId.TTRes.TheKind = static_cast<TypeTestResolution::Kind>(Record[Slot++]); | |||
5128 | TypeId.TTRes.SizeM1BitWidth = Record[Slot++]; | |||
5129 | TypeId.TTRes.AlignLog2 = Record[Slot++]; | |||
5130 | TypeId.TTRes.SizeM1 = Record[Slot++]; | |||
5131 | TypeId.TTRes.BitMask = Record[Slot++]; | |||
5132 | TypeId.TTRes.InlineBits = Record[Slot++]; | |||
5133 | ||||
5134 | while (Slot < Record.size()) | |||
5135 | parseWholeProgramDevirtResolution(Record, Strtab, Slot, TypeId); | |||
5136 | } | |||
5137 | ||||
5138 | // Eagerly parse the entire summary block. This populates the GlobalValueSummary | |||
5139 | // objects in the index. | |||
5140 | Error ModuleSummaryIndexBitcodeReader::parseEntireSummary(unsigned ID) { | |||
5141 | if (Stream.EnterSubBlock(ID)) | |||
| ||||
5142 | return error("Invalid record"); | |||
5143 | SmallVector<uint64_t, 64> Record; | |||
5144 | ||||
5145 | // Parse version | |||
5146 | { | |||
5147 | BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); | |||
5148 | if (Entry.Kind != BitstreamEntry::Record) | |||
5149 | return error("Invalid Summary Block: record for version expected"); | |||
5150 | if (Stream.readRecord(Entry.ID, Record) != bitc::FS_VERSION) | |||
5151 | return error("Invalid Summary Block: version expected"); | |||
5152 | } | |||
5153 | const uint64_t Version = Record[0]; | |||
5154 | const bool IsOldProfileFormat = Version == 1; | |||
5155 | if (Version < 1 || Version > 4) | |||
5156 | return error("Invalid summary version " + Twine(Version) + | |||
5157 | ", 1, 2, 3 or 4 expected"); | |||
5158 | Record.clear(); | |||
5159 | ||||
5160 | // Keep around the last seen summary to be used when we see an optional | |||
5161 | // "OriginalName" attachement. | |||
5162 | GlobalValueSummary *LastSeenSummary = nullptr; | |||
5163 | GlobalValue::GUID LastSeenGUID = 0; | |||
5164 | ||||
5165 | // We can expect to see any number of type ID information records before | |||
5166 | // each function summary records; these variables store the information | |||
5167 | // collected so far so that it can be used to create the summary object. | |||
5168 | std::vector<GlobalValue::GUID> PendingTypeTests; | |||
5169 | std::vector<FunctionSummary::VFuncId> PendingTypeTestAssumeVCalls, | |||
5170 | PendingTypeCheckedLoadVCalls; | |||
5171 | std::vector<FunctionSummary::ConstVCall> PendingTypeTestAssumeConstVCalls, | |||
5172 | PendingTypeCheckedLoadConstVCalls; | |||
5173 | ||||
5174 | while (true) { | |||
5175 | BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); | |||
5176 | ||||
5177 | switch (Entry.Kind) { | |||
5178 | case BitstreamEntry::SubBlock: // Handled for us already. | |||
5179 | case BitstreamEntry::Error: | |||
5180 | return error("Malformed block"); | |||
5181 | case BitstreamEntry::EndBlock: | |||
5182 | return Error::success(); | |||
5183 | case BitstreamEntry::Record: | |||
5184 | // The interesting case. | |||
5185 | break; | |||
5186 | } | |||
5187 | ||||
5188 | // Read a record. The record format depends on whether this | |||
5189 | // is a per-module index or a combined index file. In the per-module | |||
5190 | // case the records contain the associated value's ID for correlation | |||
5191 | // with VST entries. In the combined index the correlation is done | |||
5192 | // via the bitcode offset of the summary records (which were saved | |||
5193 | // in the combined index VST entries). The records also contain | |||
5194 | // information used for ThinLTO renaming and importing. | |||
5195 | Record.clear(); | |||
5196 | auto BitCode = Stream.readRecord(Entry.ID, Record); | |||
5197 | switch (BitCode) { | |||
5198 | default: // Default behavior: ignore. | |||
5199 | break; | |||
5200 | case bitc::FS_FLAGS: { // [flags] | |||
5201 | uint64_t Flags = Record[0]; | |||
5202 | // Scan flags (set only on the combined index). | |||
5203 | assert(Flags <= 0x3 && "Unexpected bits in flag")(static_cast <bool> (Flags <= 0x3 && "Unexpected bits in flag" ) ? void (0) : __assert_fail ("Flags <= 0x3 && \"Unexpected bits in flag\"" , "/build/llvm-toolchain-snapshot-7~svn329677/lib/Bitcode/Reader/BitcodeReader.cpp" , 5203, __extension__ __PRETTY_FUNCTION__)); | |||
5204 | ||||
5205 | // 1 bit: WithGlobalValueDeadStripping flag. | |||
5206 | if (Flags & 0x1) | |||
5207 | TheIndex.setWithGlobalValueDeadStripping(); | |||
5208 | // 1 bit: SkipModuleByDistributedBackend flag. | |||
5209 | if (Flags & 0x2) | |||
5210 | TheIndex.setSkipModuleByDistributedBackend(); | |||
5211 | break; | |||
5212 | } | |||
5213 | case bitc::FS_VALUE_GUID: { // [valueid, refguid] | |||
5214 | uint64_t ValueID = Record[0]; | |||
5215 | GlobalValue::GUID RefGUID = Record[1]; | |||
5216 | ValueIdToValueInfoMap[ValueID] = | |||
5217 | std::make_pair(TheIndex.getOrInsertValueInfo(RefGUID), RefGUID); | |||
5218 | break; | |||
5219 | } | |||
5220 | // FS_PERMODULE: [valueid, flags, instcount, fflags, numrefs, | |||
5221 | // numrefs x valueid, n x (valueid)] | |||
5222 | // FS_PERMODULE_PROFILE: [valueid, flags, instcount, fflags, numrefs, | |||
5223 | // numrefs x valueid, | |||
5224 | // n x (valueid, hotness)] | |||
5225 | // FS_PERMODULE_RELBF: [valueid, flags, instcount, fflags, numrefs, | |||
5226 | // numrefs x valueid, | |||
5227 | // n x (valueid, relblockfreq)] | |||
5228 | case bitc::FS_PERMODULE: | |||
5229 | case bitc::FS_PERMODULE_RELBF: | |||
5230 | case bitc::FS_PERMODULE_PROFILE: { | |||
5231 | unsigned ValueID = Record[0]; | |||
5232 | uint64_t RawFlags = Record[1]; | |||
5233 | unsigned InstCount = Record[2]; | |||
5234 | uint64_t RawFunFlags = 0; | |||
5235 | unsigned NumRefs = Record[3]; | |||
5236 | int RefListStartIndex = 4; | |||
5237 | if (Version >= 4) { | |||
5238 | RawFunFlags = Record[3]; | |||
5239 | NumRefs = Record[4]; | |||
5240 | RefListStartIndex = 5; | |||
5241 | } | |||
5242 | ||||
5243 | auto Flags = getDecodedGVSummaryFlags(RawFlags, Version); | |||
5244 | // The module path string ref set in the summary must be owned by the | |||
5245 | // index's module string table. Since we don't have a module path | |||
5246 | // string table section in the per-module index, we create a single | |||
5247 | // module path string table entry with an empty (0) ID to take | |||
5248 | // ownership. | |||
5249 | int CallGraphEdgeStartIndex = RefListStartIndex + NumRefs; | |||
5250 | assert(Record.size() >= RefListStartIndex + NumRefs &&(static_cast <bool> (Record.size() >= RefListStartIndex + NumRefs && "Record size inconsistent with number of references" ) ? void (0) : __assert_fail ("Record.size() >= RefListStartIndex + NumRefs && \"Record size inconsistent with number of references\"" , "/build/llvm-toolchain-snapshot-7~svn329677/lib/Bitcode/Reader/BitcodeReader.cpp" , 5251, __extension__ __PRETTY_FUNCTION__)) | |||
5251 | "Record size inconsistent with number of references")(static_cast <bool> (Record.size() >= RefListStartIndex + NumRefs && "Record size inconsistent with number of references" ) ? void (0) : __assert_fail ("Record.size() >= RefListStartIndex + NumRefs && \"Record size inconsistent with number of references\"" , "/build/llvm-toolchain-snapshot-7~svn329677/lib/Bitcode/Reader/BitcodeReader.cpp" , 5251, __extension__ __PRETTY_FUNCTION__)); | |||
5252 | std::vector<ValueInfo> Refs = makeRefList( | |||
5253 | ArrayRef<uint64_t>(Record).slice(RefListStartIndex, NumRefs)); | |||
5254 | bool HasProfile = (BitCode == bitc::FS_PERMODULE_PROFILE); | |||
5255 | bool HasRelBF = (BitCode == bitc::FS_PERMODULE_RELBF); | |||
5256 | std::vector<FunctionSummary::EdgeTy> Calls = makeCallList( | |||
5257 | ArrayRef<uint64_t>(Record).slice(CallGraphEdgeStartIndex), | |||
5258 | IsOldProfileFormat, HasProfile, HasRelBF); | |||
5259 | auto FS = llvm::make_unique<FunctionSummary>( | |||
5260 | Flags, InstCount, getDecodedFFlags(RawFunFlags), std::move(Refs), | |||
5261 | std::move(Calls), std::move(PendingTypeTests), | |||
5262 | std::move(PendingTypeTestAssumeVCalls), | |||
5263 | std::move(PendingTypeCheckedLoadVCalls), | |||
5264 | std::move(PendingTypeTestAssumeConstVCalls), | |||
5265 | std::move(PendingTypeCheckedLoadConstVCalls)); | |||
5266 | PendingTypeTests.clear(); | |||
5267 | PendingTypeTestAssumeVCalls.clear(); | |||
5268 | PendingTypeCheckedLoadVCalls.clear(); | |||
5269 | PendingTypeTestAssumeConstVCalls.clear(); | |||
5270 | PendingTypeCheckedLoadConstVCalls.clear(); | |||
5271 | auto VIAndOriginalGUID = getValueInfoFromValueId(ValueID); | |||
5272 | FS->setModulePath(addThisModule()->first()); | |||
5273 | FS->setOriginalName(VIAndOriginalGUID.second); | |||
5274 | TheIndex.addGlobalValueSummary(VIAndOriginalGUID.first, std::move(FS)); | |||
5275 | break; | |||
5276 | } | |||
5277 | // FS_ALIAS: [valueid, flags, valueid] | |||
5278 | // Aliases must be emitted (and parsed) after all FS_PERMODULE entries, as | |||
5279 | // they expect all aliasee summaries to be available. | |||
5280 | case bitc::FS_ALIAS: { | |||
5281 | unsigned ValueID = Record[0]; | |||
5282 | uint64_t RawFlags = Record[1]; | |||
5283 | unsigned AliaseeID = Record[2]; | |||
5284 | auto Flags = getDecodedGVSummaryFlags(RawFlags, Version); | |||
5285 | auto AS = llvm::make_unique<AliasSummary>(Flags); | |||
5286 | // The module path string ref set in the summary must be owned by the | |||
5287 | // index's module string table. Since we don't have a module path | |||
5288 | // string table section in the per-module index, we create a single | |||
5289 | // module path string table entry with an empty (0) ID to take | |||
5290 | // ownership. | |||
5291 | AS->setModulePath(addThisModule()->first()); | |||
5292 | ||||
5293 | GlobalValue::GUID AliaseeGUID = | |||
5294 | getValueInfoFromValueId(AliaseeID).first.getGUID(); | |||
5295 | auto AliaseeInModule = | |||
5296 | TheIndex.findSummaryInModule(AliaseeGUID, ModulePath); | |||
5297 | if (!AliaseeInModule) | |||
5298 | return error("Alias expects aliasee summary to be parsed"); | |||
5299 | AS->setAliasee(AliaseeInModule); | |||
5300 | AS->setAliaseeGUID(AliaseeGUID); | |||
5301 | ||||
5302 | auto GUID = getValueInfoFromValueId(ValueID); | |||
5303 | AS->setOriginalName(GUID.second); | |||
5304 | TheIndex.addGlobalValueSummary(GUID.first, std::move(AS)); | |||
5305 | break; | |||
5306 | } | |||
5307 | // FS_PERMODULE_GLOBALVAR_INIT_REFS: [valueid, flags, n x valueid] | |||
5308 | case bitc::FS_PERMODULE_GLOBALVAR_INIT_REFS: { | |||
5309 | unsigned ValueID = Record[0]; | |||
5310 | uint64_t RawFlags = Record[1]; | |||
5311 | auto Flags = getDecodedGVSummaryFlags(RawFlags, Version); | |||
5312 | std::vector<ValueInfo> Refs = | |||
5313 | makeRefList(ArrayRef<uint64_t>(Record).slice(2)); | |||
5314 | auto FS = llvm::make_unique<GlobalVarSummary>(Flags, std::move(Refs)); | |||
5315 | FS->setModulePath(addThisModule()->first()); | |||
5316 | auto GUID = getValueInfoFromValueId(ValueID); | |||
5317 | FS->setOriginalName(GUID.second); | |||
5318 | TheIndex.addGlobalValueSummary(GUID.first, std::move(FS)); | |||
5319 | break; | |||
5320 | } | |||
5321 | // FS_COMBINED: [valueid, modid, flags, instcount, fflags, numrefs, | |||
5322 | // numrefs x valueid, n x (valueid)] | |||
5323 | // FS_COMBINED_PROFILE: [valueid, modid, flags, instcount, fflags, numrefs, | |||
5324 | // numrefs x valueid, n x (valueid, hotness)] | |||
5325 | case bitc::FS_COMBINED: | |||
5326 | case bitc::FS_COMBINED_PROFILE: { | |||
5327 | unsigned ValueID = Record[0]; | |||
5328 | uint64_t ModuleId = Record[1]; | |||
5329 | uint64_t RawFlags = Record[2]; | |||
5330 | unsigned InstCount = Record[3]; | |||
5331 | uint64_t RawFunFlags = 0; | |||
5332 | unsigned NumRefs = Record[4]; | |||
5333 | int RefListStartIndex = 5; | |||
5334 | ||||
5335 | if (Version >= 4) { | |||
5336 | RawFunFlags = Record[4]; | |||
5337 | NumRefs = Record[5]; | |||
5338 | RefListStartIndex = 6; | |||
5339 | } | |||
5340 | ||||
5341 | auto Flags = getDecodedGVSummaryFlags(RawFlags, Version); | |||
5342 | int CallGraphEdgeStartIndex = RefListStartIndex + NumRefs; | |||
5343 | assert(Record.size() >= RefListStartIndex + NumRefs &&(static_cast <bool> (Record.size() >= RefListStartIndex + NumRefs && "Record size inconsistent with number of references" ) ? void (0) : __assert_fail ("Record.size() >= RefListStartIndex + NumRefs && \"Record size inconsistent with number of references\"" , "/build/llvm-toolchain-snapshot-7~svn329677/lib/Bitcode/Reader/BitcodeReader.cpp" , 5344, __extension__ __PRETTY_FUNCTION__)) | |||
5344 | "Record size inconsistent with number of references")(static_cast <bool> (Record.size() >= RefListStartIndex + NumRefs && "Record size inconsistent with number of references" ) ? void (0) : __assert_fail ("Record.size() >= RefListStartIndex + NumRefs && \"Record size inconsistent with number of references\"" , "/build/llvm-toolchain-snapshot-7~svn329677/lib/Bitcode/Reader/BitcodeReader.cpp" , 5344, __extension__ __PRETTY_FUNCTION__)); | |||
5345 | std::vector<ValueInfo> Refs = makeRefList( | |||
5346 | ArrayRef<uint64_t>(Record).slice(RefListStartIndex, NumRefs)); | |||
5347 | bool HasProfile = (BitCode == bitc::FS_COMBINED_PROFILE); | |||
5348 | std::vector<FunctionSummary::EdgeTy> Edges = makeCallList( | |||
5349 | ArrayRef<uint64_t>(Record).slice(CallGraphEdgeStartIndex), | |||
5350 | IsOldProfileFormat, HasProfile, false); | |||
5351 | ValueInfo VI = getValueInfoFromValueId(ValueID).first; | |||
5352 | auto FS = llvm::make_unique<FunctionSummary>( | |||
5353 | Flags, InstCount, getDecodedFFlags(RawFunFlags), std::move(Refs), | |||
5354 | std::move(Edges), std::move(PendingTypeTests), | |||
5355 | std::move(PendingTypeTestAssumeVCalls), | |||
5356 | std::move(PendingTypeCheckedLoadVCalls), | |||
5357 | std::move(PendingTypeTestAssumeConstVCalls), | |||
5358 | std::move(PendingTypeCheckedLoadConstVCalls)); | |||
5359 | PendingTypeTests.clear(); | |||
5360 | PendingTypeTestAssumeVCalls.clear(); | |||
5361 | PendingTypeCheckedLoadVCalls.clear(); | |||
5362 | PendingTypeTestAssumeConstVCalls.clear(); | |||
5363 | PendingTypeCheckedLoadConstVCalls.clear(); | |||
5364 | LastSeenSummary = FS.get(); | |||
5365 | LastSeenGUID = VI.getGUID(); | |||
5366 | FS->setModulePath(ModuleIdMap[ModuleId]); | |||
5367 | TheIndex.addGlobalValueSummary(VI, std::move(FS)); | |||
5368 | break; | |||
5369 | } | |||
5370 | // FS_COMBINED_ALIAS: [valueid, modid, flags, valueid] | |||
5371 | // Aliases must be emitted (and parsed) after all FS_COMBINED entries, as | |||
5372 | // they expect all aliasee summaries to be available. | |||
5373 | case bitc::FS_COMBINED_ALIAS: { | |||
5374 | unsigned ValueID = Record[0]; | |||
5375 | uint64_t ModuleId = Record[1]; | |||
5376 | uint64_t RawFlags = Record[2]; | |||
5377 | unsigned AliaseeValueId = Record[3]; | |||
5378 | auto Flags = getDecodedGVSummaryFlags(RawFlags, Version); | |||
5379 | auto AS = llvm::make_unique<AliasSummary>(Flags); | |||
5380 | LastSeenSummary = AS.get(); | |||
5381 | AS->setModulePath(ModuleIdMap[ModuleId]); | |||
5382 | ||||
5383 | auto AliaseeGUID = | |||
5384 | getValueInfoFromValueId(AliaseeValueId).first.getGUID(); | |||
5385 | auto AliaseeInModule = | |||
5386 | TheIndex.findSummaryInModule(AliaseeGUID, AS->modulePath()); | |||
5387 | AS->setAliasee(AliaseeInModule); | |||
5388 | AS->setAliaseeGUID(AliaseeGUID); | |||
5389 | ||||
5390 | ValueInfo VI = getValueInfoFromValueId(ValueID).first; | |||
5391 | LastSeenGUID = VI.getGUID(); | |||
5392 | TheIndex.addGlobalValueSummary(VI, std::move(AS)); | |||
5393 | break; | |||
5394 | } | |||
5395 | // FS_COMBINED_GLOBALVAR_INIT_REFS: [valueid, modid, flags, n x valueid] | |||
5396 | case bitc::FS_COMBINED_GLOBALVAR_INIT_REFS: { | |||
5397 | unsigned ValueID = Record[0]; | |||
5398 | uint64_t ModuleId = Record[1]; | |||
5399 | uint64_t RawFlags = Record[2]; | |||
5400 | auto Flags = getDecodedGVSummaryFlags(RawFlags, Version); | |||
5401 | std::vector<ValueInfo> Refs = | |||
5402 | makeRefList(ArrayRef<uint64_t>(Record).slice(3)); | |||
5403 | auto FS = llvm::make_unique<GlobalVarSummary>(Flags, std::move(Refs)); | |||
5404 | LastSeenSummary = FS.get(); | |||
5405 | FS->setModulePath(ModuleIdMap[ModuleId]); | |||
5406 | ValueInfo VI = getValueInfoFromValueId(ValueID).first; | |||
5407 | LastSeenGUID = VI.getGUID(); | |||
5408 | TheIndex.addGlobalValueSummary(VI, std::move(FS)); | |||
5409 | break; | |||
5410 | } | |||
5411 | // FS_COMBINED_ORIGINAL_NAME: [original_name] | |||
5412 | case bitc::FS_COMBINED_ORIGINAL_NAME: { | |||
5413 | uint64_t OriginalName = Record[0]; | |||
5414 | if (!LastSeenSummary) | |||
5415 | return error("Name attachment that does not follow a combined record"); | |||
5416 | LastSeenSummary->setOriginalName(OriginalName); | |||
| ||||
5417 | TheIndex.addOriginalName(LastSeenGUID, OriginalName); | |||
5418 | // Reset the LastSeenSummary | |||
5419 | LastSeenSummary = nullptr; | |||
5420 | LastSeenGUID = 0; | |||
5421 | break; | |||
5422 | } | |||
5423 | case bitc::FS_TYPE_TESTS: | |||
5424 | assert(PendingTypeTests.empty())(static_cast <bool> (PendingTypeTests.empty()) ? void ( 0) : __assert_fail ("PendingTypeTests.empty()", "/build/llvm-toolchain-snapshot-7~svn329677/lib/Bitcode/Reader/BitcodeReader.cpp" , 5424, __extension__ __PRETTY_FUNCTION__)); | |||
5425 | PendingTypeTests.insert(PendingTypeTests.end(), Record.begin(), | |||
5426 | Record.end()); | |||
5427 | break; | |||
5428 | ||||
5429 | case bitc::FS_TYPE_TEST_ASSUME_VCALLS: | |||
5430 | assert(PendingTypeTestAssumeVCalls.empty())(static_cast <bool> (PendingTypeTestAssumeVCalls.empty( )) ? void (0) : __assert_fail ("PendingTypeTestAssumeVCalls.empty()" , "/build/llvm-toolchain-snapshot-7~svn329677/lib/Bitcode/Reader/BitcodeReader.cpp" , 5430, __extension__ __PRETTY_FUNCTION__)); | |||
5431 | for (unsigned I = 0; I != Record.size(); I += 2) | |||
5432 | PendingTypeTestAssumeVCalls.push_back({Record[I], Record[I+1]}); | |||
5433 | break; | |||
5434 | ||||
5435 | case bitc::FS_TYPE_CHECKED_LOAD_VCALLS: | |||
5436 | assert(PendingTypeCheckedLoadVCalls.empty())(static_cast <bool> (PendingTypeCheckedLoadVCalls.empty ()) ? void (0) : __assert_fail ("PendingTypeCheckedLoadVCalls.empty()" , "/build/llvm-toolchain-snapshot-7~svn329677/lib/Bitcode/Reader/BitcodeReader.cpp" , 5436, __extension__ __PRETTY_FUNCTION__)); | |||
5437 | for (unsigned I = 0; I != Record.size(); I += 2) | |||
5438 | PendingTypeCheckedLoadVCalls.push_back({Record[I], Record[I+1]}); | |||
5439 | break; | |||
5440 | ||||
5441 | case bitc::FS_TYPE_TEST_ASSUME_CONST_VCALL: | |||
5442 | PendingTypeTestAssumeConstVCalls.push_back( | |||
5443 | {{Record[0], Record[1]}, {Record.begin() + 2, Record.end()}}); | |||
5444 | break; | |||
5445 | ||||
5446 | case bitc::FS_TYPE_CHECKED_LOAD_CONST_VCALL: | |||
5447 | PendingTypeCheckedLoadConstVCalls.push_back( | |||
5448 | {{Record[0], Record[1]}, {Record.begin() + 2, Record.end()}}); | |||
5449 | break; | |||
5450 | ||||
5451 | case bitc::FS_CFI_FUNCTION_DEFS: { | |||
5452 | std::set<std::string> &CfiFunctionDefs = TheIndex.cfiFunctionDefs(); | |||
5453 | for (unsigned I = 0; I != Record.size(); I += 2) | |||
5454 | CfiFunctionDefs.insert( | |||
5455 | {Strtab.data() + Record[I], static_cast<size_t>(Record[I + 1])}); | |||
5456 | break; | |||
5457 | } | |||
5458 | ||||
5459 | case bitc::FS_CFI_FUNCTION_DECLS: { | |||
5460 | std::set<std::string> &CfiFunctionDecls = TheIndex.cfiFunctionDecls(); | |||
5461 | for (unsigned I = 0; I != Record.size(); I += 2) | |||
5462 | CfiFunctionDecls.insert( | |||
5463 | {Strtab.data() + Record[I], static_cast<size_t>(Record[I + 1])}); | |||
5464 | break; | |||
5465 | } | |||
5466 | ||||
5467 | case bitc::FS_TYPE_ID: | |||
5468 | parseTypeIdSummaryRecord(Record, Strtab, TheIndex); | |||
5469 | break; | |||
5470 | } | |||
5471 | } | |||
5472 | llvm_unreachable("Exit infinite loop")::llvm::llvm_unreachable_internal("Exit infinite loop", "/build/llvm-toolchain-snapshot-7~svn329677/lib/Bitcode/Reader/BitcodeReader.cpp" , 5472); | |||
5473 | } | |||
5474 | ||||
5475 | // Parse the module string table block into the Index. | |||
5476 | // This populates the ModulePathStringTable map in the index. | |||
5477 | Error ModuleSummaryIndexBitcodeReader::parseModuleStringTable() { | |||
5478 | if (Stream.EnterSubBlock(bitc::MODULE_STRTAB_BLOCK_ID)) | |||
5479 | return error("Invalid record"); | |||
5480 | ||||
5481 | SmallVector<uint64_t, 64> Record; | |||
5482 | ||||
5483 | SmallString<128> ModulePath; | |||
5484 | ModuleSummaryIndex::ModuleInfo *LastSeenModule = nullptr; | |||
5485 | ||||
5486 | while (true) { | |||
5487 | BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); | |||
5488 | ||||
5489 | switch (Entry.Kind) { | |||
5490 | case BitstreamEntry::SubBlock: // Handled for us already. | |||
5491 | case BitstreamEntry::Error: | |||
5492 | return error("Malformed block"); | |||
5493 | case BitstreamEntry::EndBlock: | |||
5494 | return Error::success(); | |||
5495 | case BitstreamEntry::Record: | |||
5496 | // The interesting case. | |||
5497 | break; | |||
5498 | } | |||
5499 | ||||
5500 | Record.clear(); | |||
5501 | switch (Stream.readRecord(Entry.ID, Record)) { | |||
5502 | default: // Default behavior: ignore. | |||
5503 | break; | |||
5504 | case bitc::MST_CODE_ENTRY: { | |||
5505 | // MST_ENTRY: [modid, namechar x N] | |||
5506 | uint64_t ModuleId = Record[0]; | |||
5507 | ||||
5508 | if (convertToString(Record, 1, ModulePath)) | |||
5509 | return error("Invalid record"); | |||
5510 | ||||
5511 | LastSeenModule = TheIndex.addModule(ModulePath, ModuleId); | |||
5512 | ModuleIdMap[ModuleId] = LastSeenModule->first(); | |||
5513 | ||||
5514 | ModulePath.clear(); | |||
5515 | break; | |||
5516 | } | |||
5517 | /// MST_CODE_HASH: [5*i32] | |||
5518 | case bitc::MST_CODE_HASH: { | |||
5519 | if (Record.size() != 5) | |||
5520 | return error("Invalid hash length " + Twine(Record.size()).str()); | |||
5521 | if (!LastSeenModule) | |||
5522 | return error("Invalid hash that does not follow a module path"); | |||
5523 | int Pos = 0; | |||
5524 | for (auto &Val : Record) { | |||
5525 | assert(!(Val >> 32) && "Unexpected high bits set")(static_cast <bool> (!(Val >> 32) && "Unexpected high bits set" ) ? void (0) : __assert_fail ("!(Val >> 32) && \"Unexpected high bits set\"" , "/build/llvm-toolchain-snapshot-7~svn329677/lib/Bitcode/Reader/BitcodeReader.cpp" , 5525, __extension__ __PRETTY_FUNCTION__)); | |||
5526 | LastSeenModule->second.second[Pos++] = Val; | |||
5527 | } | |||
5528 | // Reset LastSeenModule to avoid overriding the hash unexpectedly. | |||
5529 | LastSeenModule = nullptr; | |||
5530 | break; | |||
5531 | } | |||
5532 | } | |||
5533 | } | |||
5534 | llvm_unreachable("Exit infinite loop")::llvm::llvm_unreachable_internal("Exit infinite loop", "/build/llvm-toolchain-snapshot-7~svn329677/lib/Bitcode/Reader/BitcodeReader.cpp" , 5534); | |||
5535 | } | |||
5536 | ||||
5537 | namespace { | |||
5538 | ||||
5539 | // FIXME: This class is only here to support the transition to llvm::Error. It | |||
5540 | // will be removed once this transition is complete. Clients should prefer to | |||
5541 | // deal with the Error value directly, rather than converting to error_code. | |||
5542 | class BitcodeErrorCategoryType : public std::error_category { | |||
5543 | const char *name() const noexcept override { | |||
5544 | return "llvm.bitcode"; | |||
5545 | } | |||
5546 | ||||
5547 | std::string message(int IE) const override { | |||
5548 | BitcodeError E = static_cast<BitcodeError>(IE); | |||
5549 | switch (E) { | |||
5550 | case BitcodeError::CorruptedBitcode: | |||
5551 | return "Corrupted bitcode"; | |||
5552 | } | |||
5553 | llvm_unreachable("Unknown error type!")::llvm::llvm_unreachable_internal("Unknown error type!", "/build/llvm-toolchain-snapshot-7~svn329677/lib/Bitcode/Reader/BitcodeReader.cpp" , 5553); | |||
5554 | } | |||
5555 | }; | |||
5556 | ||||
5557 | } // end anonymous namespace | |||
5558 | ||||
5559 | static ManagedStatic<BitcodeErrorCategoryType> ErrorCategory; | |||
5560 | ||||
5561 | const std::error_category &llvm::BitcodeErrorCategory() { | |||
5562 | return *ErrorCategory; | |||
5563 | } | |||
5564 | ||||
5565 | static Expected<StringRef> readBlobInRecord(BitstreamCursor &Stream, | |||
5566 | unsigned Block, unsigned RecordID) { | |||
5567 | if (Stream.EnterSubBlock(Block)) | |||
5568 | return error("Invalid record"); | |||
5569 | ||||
5570 | StringRef Strtab; | |||
5571 | while (true) { | |||
5572 | BitstreamEntry Entry = Stream.advance(); | |||
5573 | switch (Entry.Kind) { | |||
5574 | case BitstreamEntry::EndBlock: | |||
5575 | return Strtab; | |||
5576 | ||||
5577 | case BitstreamEntry::Error: | |||
5578 | return error("Malformed block"); | |||
5579 | ||||
5580 | case BitstreamEntry::SubBlock: | |||
5581 | if (Stream.SkipBlock()) | |||
5582 | return error("Malformed block"); | |||
5583 | break; | |||
5584 | ||||
5585 | case BitstreamEntry::Record: | |||
5586 | StringRef Blob; | |||
5587 | SmallVector<uint64_t, 1> Record; | |||
5588 | if (Stream.readRecord(Entry.ID, Record, &Blob) == RecordID) | |||
5589 | Strtab = Blob; | |||
5590 | break; | |||
5591 | } | |||
5592 | } | |||
5593 | } | |||
5594 | ||||
5595 | //===----------------------------------------------------------------------===// | |||
5596 | // External interface | |||
5597 | //===----------------------------------------------------------------------===// | |||
5598 | ||||
5599 | Expected<std::vector<BitcodeModule>> | |||
5600 | llvm::getBitcodeModuleList(MemoryBufferRef Buffer) { | |||
5601 | auto FOrErr = getBitcodeFileContents(Buffer); | |||
5602 | if (!FOrErr) | |||
5603 | return FOrErr.takeError(); | |||
5604 | return std::move(FOrErr->Mods); | |||
5605 | } | |||
5606 | ||||
5607 | Expected<BitcodeFileContents> | |||
5608 | llvm::getBitcodeFileContents(MemoryBufferRef Buffer) { | |||
5609 | Expected<BitstreamCursor> StreamOrErr = initStream(Buffer); | |||
5610 | if (!StreamOrErr) | |||
5611 | return StreamOrErr.takeError(); | |||
5612 | BitstreamCursor &Stream = *StreamOrErr; | |||
5613 | ||||
5614 | BitcodeFileContents F; | |||
5615 | while (true) { | |||
5616 | uint64_t BCBegin = Stream.getCurrentByteNo(); | |||
5617 | ||||
5618 | // We may be consuming bitcode from a client that leaves garbage at the end | |||
5619 | // of the bitcode stream (e.g. Apple's ar tool). If we are close enough to | |||
5620 | // the end that there cannot possibly be another module, stop looking. | |||
5621 | if (BCBegin + 8 >= Stream.getBitcodeBytes().size()) | |||
5622 | return F; | |||
5623 | ||||
5624 | BitstreamEntry Entry = Stream.advance(); | |||
5625 | switch (Entry.Kind) { | |||
5626 | case BitstreamEntry::EndBlock: | |||
5627 | case BitstreamEntry::Error: | |||
5628 | return error("Malformed block"); | |||
5629 | ||||
5630 | case BitstreamEntry::SubBlock: { | |||
5631 | uint64_t IdentificationBit = -1ull; | |||
5632 | if (Entry.ID == bitc::IDENTIFICATION_BLOCK_ID) { | |||
5633 | IdentificationBit = Stream.GetCurrentBitNo() - BCBegin * 8; | |||
5634 | if (Stream.SkipBlock()) | |||
5635 | return error("Malformed block"); | |||
5636 | ||||
5637 | Entry = Stream.advance(); | |||
5638 | if (Entry.Kind != BitstreamEntry::SubBlock || | |||
5639 | Entry.ID != bitc::MODULE_BLOCK_ID) | |||
5640 | return error("Malformed block"); | |||
5641 | } | |||
5642 | ||||
5643 | if (Entry.ID == bitc::MODULE_BLOCK_ID) { | |||
5644 | uint64_t ModuleBit = Stream.GetCurrentBitNo() - BCBegin * 8; | |||
5645 | if (Stream.SkipBlock()) | |||
5646 | return error("Malformed block"); | |||
5647 | ||||
5648 | F.Mods.push_back({Stream.getBitcodeBytes().slice( | |||
5649 | BCBegin, Stream.getCurrentByteNo() - BCBegin), | |||
5650 | Buffer.getBufferIdentifier(), IdentificationBit, | |||
5651 | ModuleBit}); | |||
5652 | continue; | |||
5653 | } | |||
5654 | ||||
5655 | if (Entry.ID == bitc::STRTAB_BLOCK_ID) { | |||
5656 | Expected<StringRef> Strtab = | |||
5657 | readBlobInRecord(Stream, bitc::STRTAB_BLOCK_ID, bitc::STRTAB_BLOB); | |||
5658 | if (!Strtab) | |||
5659 | return Strtab.takeError(); | |||
5660 | // This string table is used by every preceding bitcode module that does | |||
5661 | // not have its own string table. A bitcode file may have multiple | |||
5662 | // string tables if it was created by binary concatenation, for example | |||
5663 | // with "llvm-cat -b". | |||
5664 | for (auto I = F.Mods.rbegin(), E = F.Mods.rend(); I != E; ++I) { | |||
5665 | if (!I->Strtab.empty()) | |||
5666 | break; | |||
5667 | I->Strtab = *Strtab; | |||
5668 | } | |||
5669 | // Similarly, the string table is used by every preceding symbol table; | |||
5670 | // normally there will be just one unless the bitcode file was created | |||
5671 | // by binary concatenation. | |||
5672 | if (!F.Symtab.empty() && F.StrtabForSymtab.empty()) | |||
5673 | F.StrtabForSymtab = *Strtab; | |||
5674 | continue; | |||
5675 | } | |||
5676 | ||||
5677 | if (Entry.ID == bitc::SYMTAB_BLOCK_ID) { | |||
5678 | Expected<StringRef> SymtabOrErr = | |||
5679 | readBlobInRecord(Stream, bitc::SYMTAB_BLOCK_ID, bitc::SYMTAB_BLOB); | |||
5680 | if (!SymtabOrErr) | |||
5681 | return SymtabOrErr.takeError(); | |||
5682 | ||||
5683 | // We can expect the bitcode file to have multiple symbol tables if it | |||
5684 | // was created by binary concatenation. In that case we silently | |||
5685 | // ignore any subsequent symbol tables, which is fine because this is a | |||
5686 | // low level function. The client is expected to notice that the number | |||
5687 | // of modules in the symbol table does not match the number of modules | |||
5688 | // in the input file and regenerate the symbol table. | |||
5689 | if (F.Symtab.empty()) | |||
5690 | F.Symtab = *SymtabOrErr; | |||
5691 | continue; | |||
5692 | } | |||
5693 | ||||
5694 | if (Stream.SkipBlock()) | |||
5695 | return error("Malformed block"); | |||
5696 | continue; | |||
5697 | } | |||
5698 | case BitstreamEntry::Record: | |||
5699 | Stream.skipRecord(Entry.ID); | |||
5700 | continue; | |||
5701 | } | |||
5702 | } | |||
5703 | } | |||
5704 | ||||
5705 | /// \brief Get a lazy one-at-time loading module from bitcode. | |||
5706 | /// | |||
5707 | /// This isn't always used in a lazy context. In particular, it's also used by | |||
5708 | /// \a parseModule(). If this is truly lazy, then we need to eagerly pull | |||
5709 | /// in forward-referenced functions from block address references. | |||
5710 | /// | |||
5711 | /// \param[in] MaterializeAll Set to \c true if we should materialize | |||
5712 | /// everything. | |||
5713 | Expected<std::unique_ptr<Module>> | |||
5714 | BitcodeModule::getModuleImpl(LLVMContext &Context, bool MaterializeAll, | |||
5715 | bool ShouldLazyLoadMetadata, bool IsImporting) { | |||
5716 | BitstreamCursor Stream(Buffer); | |||
5717 | ||||
5718 | std::string ProducerIdentification; | |||
5719 | if (IdentificationBit != -1ull) { | |||
5720 | Stream.JumpToBit(IdentificationBit); | |||
5721 | Expected<std::string> ProducerIdentificationOrErr = | |||
5722 | readIdentificationBlock(Stream); | |||
5723 | if (!ProducerIdentificationOrErr) | |||
5724 | return ProducerIdentificationOrErr.takeError(); | |||
5725 | ||||
5726 | ProducerIdentification = *ProducerIdentificationOrErr; | |||
5727 | } | |||
5728 | ||||
5729 | Stream.JumpToBit(ModuleBit); | |||
5730 | auto *R = new BitcodeReader(std::move(Stream), Strtab, ProducerIdentification, | |||
5731 | Context); | |||
5732 | ||||
5733 | std::unique_ptr<Module> M = | |||
5734 | llvm::make_unique<Module>(ModuleIdentifier, Context); | |||
5735 | M->setMaterializer(R); | |||
5736 | ||||
5737 | // Delay parsing Metadata if ShouldLazyLoadMetadata is true. | |||
5738 | if (Error Err = | |||
5739 | R->parseBitcodeInto(M.get(), ShouldLazyLoadMetadata, IsImporting)) | |||
5740 | return std::move(Err); | |||
5741 | ||||
5742 | if (MaterializeAll) { | |||
5743 | // Read in the entire module, and destroy the BitcodeReader. | |||
5744 | if (Error Err = M->materializeAll()) | |||
5745 | return std::move(Err); | |||
5746 | } else { | |||
5747 | // Resolve forward references from blockaddresses. | |||
5748 | if (Error Err = R->materializeForwardReferencedFunctions()) | |||
5749 | return std::move(Err); | |||
5750 | } | |||
5751 | return std::move(M); | |||
5752 | } | |||
5753 | ||||
5754 | Expected<std::unique_ptr<Module>> | |||
5755 | BitcodeModule::getLazyModule(LLVMContext &Context, bool ShouldLazyLoadMetadata, | |||
5756 | bool IsImporting) { | |||
5757 | return getModuleImpl(Context, false, ShouldLazyLoadMetadata, IsImporting); | |||
5758 | } | |||
5759 | ||||
5760 | // Parse the specified bitcode buffer and merge the index into CombinedIndex. | |||
5761 | // We don't use ModuleIdentifier here because the client may need to control the | |||
5762 | // module path used in the combined summary (e.g. when reading summaries for | |||
5763 | // regular LTO modules). | |||
5764 | Error BitcodeModule::readSummary(ModuleSummaryIndex &CombinedIndex, | |||
5765 | StringRef ModulePath, uint64_t ModuleId) { | |||
5766 | BitstreamCursor Stream(Buffer); | |||
5767 | Stream.JumpToBit(ModuleBit); | |||
5768 | ||||
5769 | ModuleSummaryIndexBitcodeReader R(std::move(Stream), Strtab, CombinedIndex, | |||
5770 | ModulePath, ModuleId); | |||
5771 | return R.parseModule(); | |||
5772 | } | |||
5773 | ||||
5774 | // Parse the specified bitcode buffer, returning the function info index. | |||
5775 | Expected<std::unique_ptr<ModuleSummaryIndex>> BitcodeModule::getSummary() { | |||
5776 | BitstreamCursor Stream(Buffer); | |||
5777 | Stream.JumpToBit(ModuleBit); | |||
5778 | ||||
5779 | auto Index = | |||
5780 | llvm::make_unique<ModuleSummaryIndex>(/*IsPerformingAnalysis=*/false); | |||
5781 | ModuleSummaryIndexBitcodeReader R(std::move(Stream), Strtab, *Index, | |||
5782 | ModuleIdentifier, 0); | |||
5783 | ||||
5784 | if (Error Err = R.parseModule()) | |||
5785 | return std::move(Err); | |||
5786 | ||||
5787 | return std::move(Index); | |||
5788 | } | |||
5789 | ||||
5790 | // Check if the given bitcode buffer contains a global value summary block. | |||
5791 | Expected<BitcodeLTOInfo> BitcodeModule::getLTOInfo() { | |||
5792 | BitstreamCursor Stream(Buffer); | |||
5793 | Stream.JumpToBit(ModuleBit); | |||
5794 | ||||
5795 | if (Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID)) | |||
5796 | return error("Invalid record"); | |||
5797 | ||||
5798 | while (true) { | |||
5799 | BitstreamEntry Entry = Stream.advance(); | |||
5800 | ||||
5801 | switch (Entry.Kind) { | |||
5802 | case BitstreamEntry::Error: | |||
5803 | return error("Malformed block"); | |||
5804 | case BitstreamEntry::EndBlock: | |||
5805 | return BitcodeLTOInfo{/*IsThinLTO=*/false, /*HasSummary=*/false}; | |||
5806 | ||||
5807 | case BitstreamEntry::SubBlock: | |||
5808 | if (Entry.ID == bitc::GLOBALVAL_SUMMARY_BLOCK_ID) | |||
5809 | return BitcodeLTOInfo{/*IsThinLTO=*/true, /*HasSummary=*/true}; | |||
5810 | ||||
5811 | if (Entry.ID == bitc::FULL_LTO_GLOBALVAL_SUMMARY_BLOCK_ID) | |||
5812 | return BitcodeLTOInfo{/*IsThinLTO=*/false, /*HasSummary=*/true}; | |||
5813 | ||||
5814 | // Ignore other sub-blocks. | |||
5815 | if (Stream.SkipBlock()) | |||
5816 | return error("Malformed block"); | |||
5817 | continue; | |||
5818 | ||||
5819 | case BitstreamEntry::Record: | |||
5820 | Stream.skipRecord(Entry.ID); | |||
5821 | continue; | |||
5822 | } | |||
5823 | } | |||
5824 | } | |||
5825 | ||||
5826 | static Expected<BitcodeModule> getSingleModule(MemoryBufferRef Buffer) { | |||
5827 | Expected<std::vector<BitcodeModule>> MsOrErr = getBitcodeModuleList(Buffer); | |||
5828 | if (!MsOrErr) | |||
5829 | return MsOrErr.takeError(); | |||
5830 | ||||
5831 | if (MsOrErr->size() != 1) | |||
5832 | return error("Expected a single module"); | |||
5833 | ||||
5834 | return (*MsOrErr)[0]; | |||
5835 | } | |||
5836 | ||||
5837 | Expected<std::unique_ptr<Module>> | |||
5838 | llvm::getLazyBitcodeModule(MemoryBufferRef Buffer, LLVMContext &Context, | |||
5839 | bool ShouldLazyLoadMetadata, bool IsImporting) { | |||
5840 | Expected<BitcodeModule> BM = getSingleModule(Buffer); | |||
5841 | if (!BM) | |||
5842 | return BM.takeError(); | |||
5843 | ||||
5844 | return BM->getLazyModule(Context, ShouldLazyLoadMetadata, IsImporting); | |||
5845 | } | |||
5846 | ||||
5847 | Expected<std::unique_ptr<Module>> llvm::getOwningLazyBitcodeModule( | |||
5848 | std::unique_ptr<MemoryBuffer> &&Buffer, LLVMContext &Context, | |||
5849 | bool ShouldLazyLoadMetadata, bool IsImporting) { | |||
5850 | auto MOrErr = getLazyBitcodeModule(*Buffer, Context, ShouldLazyLoadMetadata, | |||
5851 | IsImporting); | |||
5852 | if (MOrErr) | |||
5853 | (*MOrErr)->setOwnedMemoryBuffer(std::move(Buffer)); | |||
5854 | return MOrErr; | |||
5855 | } | |||
5856 | ||||
5857 | Expected<std::unique_ptr<Module>> | |||
5858 | BitcodeModule::parseModule(LLVMContext &Context) { | |||
5859 | return getModuleImpl(Context, true, false, false); | |||
5860 | // TODO: Restore the use-lists to the in-memory state when the bitcode was | |||
5861 | // written. We must defer until the Module has been fully materialized. | |||
5862 | } | |||
5863 | ||||
5864 | Expected<std::unique_ptr<Module>> llvm::parseBitcodeFile(MemoryBufferRef Buffer, | |||
5865 | LLVMContext &Context) { | |||
5866 | Expected<BitcodeModule> BM = getSingleModule(Buffer); | |||
5867 | if (!BM) | |||
5868 | return BM.takeError(); | |||
5869 | ||||
5870 | return BM->parseModule(Context); | |||
5871 | } | |||
5872 | ||||
5873 | Expected<std::string> llvm::getBitcodeTargetTriple(MemoryBufferRef Buffer) { | |||
5874 | Expected<BitstreamCursor> StreamOrErr = initStream(Buffer); | |||
5875 | if (!StreamOrErr) | |||
5876 | return StreamOrErr.takeError(); | |||
5877 | ||||
5878 | return readTriple(*StreamOrErr); | |||
5879 | } | |||
5880 | ||||
5881 | Expected<bool> llvm::isBitcodeContainingObjCCategory(MemoryBufferRef Buffer) { | |||
5882 | Expected<BitstreamCursor> StreamOrErr = initStream(Buffer); | |||
5883 | if (!StreamOrErr) | |||
5884 | return StreamOrErr.takeError(); | |||
5885 | ||||
5886 | return hasObjCCategory(*StreamOrErr); | |||
5887 | } | |||
5888 | ||||
5889 | Expected<std::string> llvm::getBitcodeProducerString(MemoryBufferRef Buffer) { | |||
5890 | Expected<BitstreamCursor> StreamOrErr = initStream(Buffer); | |||
5891 | if (!StreamOrErr) | |||
5892 | return StreamOrErr.takeError(); | |||
5893 | ||||
5894 | return readIdentificationCode(*StreamOrErr); | |||
5895 | } | |||
5896 | ||||
5897 | Error llvm::readModuleSummaryIndex(MemoryBufferRef Buffer, | |||
5898 | ModuleSummaryIndex &CombinedIndex, | |||
5899 | uint64_t ModuleId) { | |||
5900 | Expected<BitcodeModule> BM = getSingleModule(Buffer); | |||
5901 | if (!BM) | |||
5902 | return BM.takeError(); | |||
5903 | ||||
5904 | return BM->readSummary(CombinedIndex, BM->getModuleIdentifier(), ModuleId); | |||
5905 | } | |||
5906 | ||||
5907 | Expected<std::unique_ptr<ModuleSummaryIndex>> | |||
5908 | llvm::getModuleSummaryIndex(MemoryBufferRef Buffer) { | |||
5909 | Expected<BitcodeModule> BM = getSingleModule(Buffer); | |||
5910 | if (!BM) | |||
5911 | return BM.takeError(); | |||
5912 | ||||
5913 | return BM->getSummary(); | |||
5914 | } | |||
5915 | ||||
5916 | Expected<BitcodeLTOInfo> llvm::getBitcodeLTOInfo(MemoryBufferRef Buffer) { | |||
5917 | Expected<BitcodeModule> BM = getSingleModule(Buffer); | |||
5918 | if (!BM) | |||
5919 | return BM.takeError(); | |||
5920 | ||||
5921 | return BM->getLTOInfo(); | |||
5922 | } | |||
5923 | ||||
5924 | Expected<std::unique_ptr<ModuleSummaryIndex>> | |||
5925 | llvm::getModuleSummaryIndexForFile(StringRef Path, | |||
5926 | bool IgnoreEmptyThinLTOIndexFile) { | |||
5927 | ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr = | |||
5928 | MemoryBuffer::getFileOrSTDIN(Path); | |||
5929 | if (!FileOrErr) | |||
5930 | return errorCodeToError(FileOrErr.getError()); | |||
5931 | if (IgnoreEmptyThinLTOIndexFile && !(*FileOrErr)->getBufferSize()) | |||
5932 | return nullptr; | |||
5933 | return getModuleSummaryIndex(**FileOrErr); | |||
5934 | } |
1 | //===- llvm/ADT/STLExtras.h - Useful STL related functions ------*- 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 contains some templates that are useful if you are working with the |
11 | // STL at all. |
12 | // |
13 | // No library is required when using these functions. |
14 | // |
15 | //===----------------------------------------------------------------------===// |
16 | |
17 | #ifndef LLVM_ADT_STLEXTRAS_H |
18 | #define LLVM_ADT_STLEXTRAS_H |
19 | |
20 | #include "llvm/ADT/Optional.h" |
21 | #include "llvm/ADT/SmallVector.h" |
22 | #include "llvm/ADT/iterator.h" |
23 | #include "llvm/ADT/iterator_range.h" |
24 | #include "llvm/Support/ErrorHandling.h" |
25 | #include <algorithm> |
26 | #include <cassert> |
27 | #include <cstddef> |
28 | #include <cstdint> |
29 | #include <cstdlib> |
30 | #include <functional> |
31 | #include <initializer_list> |
32 | #include <iterator> |
33 | #include <limits> |
34 | #include <memory> |
35 | #include <tuple> |
36 | #include <type_traits> |
37 | #include <utility> |
38 | |
39 | #ifdef EXPENSIVE_CHECKS |
40 | #include <random> // for std::mt19937 |
41 | #endif |
42 | |
43 | namespace llvm { |
44 | |
45 | // Only used by compiler if both template types are the same. Useful when |
46 | // using SFINAE to test for the existence of member functions. |
47 | template <typename T, T> struct SameType; |
48 | |
49 | namespace detail { |
50 | |
51 | template <typename RangeT> |
52 | using IterOfRange = decltype(std::begin(std::declval<RangeT &>())); |
53 | |
54 | template <typename RangeT> |
55 | using ValueOfRange = typename std::remove_reference<decltype( |
56 | *std::begin(std::declval<RangeT &>()))>::type; |
57 | |
58 | } // end namespace detail |
59 | |
60 | //===----------------------------------------------------------------------===// |
61 | // Extra additions to <functional> |
62 | //===----------------------------------------------------------------------===// |
63 | |
64 | template <class Ty> struct identity { |
65 | using argument_type = Ty; |
66 | |
67 | Ty &operator()(Ty &self) const { |
68 | return self; |
69 | } |
70 | const Ty &operator()(const Ty &self) const { |
71 | return self; |
72 | } |
73 | }; |
74 | |
75 | template <class Ty> struct less_ptr { |
76 | bool operator()(const Ty* left, const Ty* right) const { |
77 | return *left < *right; |
78 | } |
79 | }; |
80 | |
81 | template <class Ty> struct greater_ptr { |
82 | bool operator()(const Ty* left, const Ty* right) const { |
83 | return *right < *left; |
84 | } |
85 | }; |
86 | |
87 | /// An efficient, type-erasing, non-owning reference to a callable. This is |
88 | /// intended for use as the type of a function parameter that is not used |
89 | /// after the function in question returns. |
90 | /// |
91 | /// This class does not own the callable, so it is not in general safe to store |
92 | /// a function_ref. |
93 | template<typename Fn> class function_ref; |
94 | |
95 | template<typename Ret, typename ...Params> |
96 | class function_ref<Ret(Params...)> { |
97 | Ret (*callback)(intptr_t callable, Params ...params) = nullptr; |
98 | intptr_t callable; |
99 | |
100 | template<typename Callable> |
101 | static Ret callback_fn(intptr_t callable, Params ...params) { |
102 | return (*reinterpret_cast<Callable*>(callable))( |
103 | std::forward<Params>(params)...); |
104 | } |
105 | |
106 | public: |
107 | function_ref() = default; |
108 | function_ref(std::nullptr_t) {} |
109 | |
110 | template <typename Callable> |
111 | function_ref(Callable &&callable, |
112 | typename std::enable_if< |
113 | !std::is_same<typename std::remove_reference<Callable>::type, |
114 | function_ref>::value>::type * = nullptr) |
115 | : callback(callback_fn<typename std::remove_reference<Callable>::type>), |
116 | callable(reinterpret_cast<intptr_t>(&callable)) {} |
117 | |
118 | Ret operator()(Params ...params) const { |
119 | return callback(callable, std::forward<Params>(params)...); |
120 | } |
121 | |
122 | operator bool() const { return callback; } |
123 | }; |
124 | |
125 | // deleter - Very very very simple method that is used to invoke operator |
126 | // delete on something. It is used like this: |
127 | // |
128 | // for_each(V.begin(), B.end(), deleter<Interval>); |
129 | template <class T> |
130 | inline void deleter(T *Ptr) { |
131 | delete Ptr; |
132 | } |
133 | |
134 | //===----------------------------------------------------------------------===// |
135 | // Extra additions to <iterator> |
136 | //===----------------------------------------------------------------------===// |
137 | |
138 | namespace adl_detail { |
139 | |
140 | using std::begin; |
141 | |
142 | template <typename ContainerTy> |
143 | auto adl_begin(ContainerTy &&container) |
144 | -> decltype(begin(std::forward<ContainerTy>(container))) { |
145 | return begin(std::forward<ContainerTy>(container)); |
146 | } |
147 | |
148 | using std::end; |
149 | |
150 | template <typename ContainerTy> |
151 | auto adl_end(ContainerTy &&container) |
152 | -> decltype(end(std::forward<ContainerTy>(container))) { |
153 | return end(std::forward<ContainerTy>(container)); |
154 | } |
155 | |
156 | using std::swap; |
157 | |
158 | template <typename T> |
159 | void adl_swap(T &&lhs, T &&rhs) noexcept(noexcept(swap(std::declval<T>(), |
160 | std::declval<T>()))) { |
161 | swap(std::forward<T>(lhs), std::forward<T>(rhs)); |
162 | } |
163 | |
164 | } // end namespace adl_detail |
165 | |
166 | template <typename ContainerTy> |
167 | auto adl_begin(ContainerTy &&container) |
168 | -> decltype(adl_detail::adl_begin(std::forward<ContainerTy>(container))) { |
169 | return adl_detail::adl_begin(std::forward<ContainerTy>(container)); |
170 | } |
171 | |
172 | template <typename ContainerTy> |
173 | auto adl_end(ContainerTy &&container) |
174 | -> decltype(adl_detail::adl_end(std::forward<ContainerTy>(container))) { |
175 | return adl_detail::adl_end(std::forward<ContainerTy>(container)); |
176 | } |
177 | |
178 | template <typename T> |
179 | void adl_swap(T &&lhs, T &&rhs) noexcept( |
180 | noexcept(adl_detail::adl_swap(std::declval<T>(), std::declval<T>()))) { |
181 | adl_detail::adl_swap(std::forward<T>(lhs), std::forward<T>(rhs)); |
182 | } |
183 | |
184 | // mapped_iterator - This is a simple iterator adapter that causes a function to |
185 | // be applied whenever operator* is invoked on the iterator. |
186 | |
187 | template <typename ItTy, typename FuncTy, |
188 | typename FuncReturnTy = |
189 | decltype(std::declval<FuncTy>()(*std::declval<ItTy>()))> |
190 | class mapped_iterator |
191 | : public iterator_adaptor_base< |
192 | mapped_iterator<ItTy, FuncTy>, ItTy, |
193 | typename std::iterator_traits<ItTy>::iterator_category, |
194 | typename std::remove_reference<FuncReturnTy>::type> { |
195 | public: |
196 | mapped_iterator(ItTy U, FuncTy F) |
197 | : mapped_iterator::iterator_adaptor_base(std::move(U)), F(std::move(F)) {} |
198 | |
199 | ItTy getCurrent() { return this->I; } |
200 | |
201 | FuncReturnTy operator*() { return F(*this->I); } |
202 | |
203 | private: |
204 | FuncTy F; |
205 | }; |
206 | |
207 | // map_iterator - Provide a convenient way to create mapped_iterators, just like |
208 | // make_pair is useful for creating pairs... |
209 | template <class ItTy, class FuncTy> |
210 | inline mapped_iterator<ItTy, FuncTy> map_iterator(ItTy I, FuncTy F) { |
211 | return mapped_iterator<ItTy, FuncTy>(std::move(I), std::move(F)); |
212 | } |
213 | |
214 | /// Helper to determine if type T has a member called rbegin(). |
215 | template <typename Ty> class has_rbegin_impl { |
216 | using yes = char[1]; |
217 | using no = char[2]; |
218 | |
219 | template <typename Inner> |
220 | static yes& test(Inner *I, decltype(I->rbegin()) * = nullptr); |
221 | |
222 | template <typename> |
223 | static no& test(...); |
224 | |
225 | public: |
226 | static const bool value = sizeof(test<Ty>(nullptr)) == sizeof(yes); |
227 | }; |
228 | |
229 | /// Metafunction to determine if T& or T has a member called rbegin(). |
230 | template <typename Ty> |
231 | struct has_rbegin : has_rbegin_impl<typename std::remove_reference<Ty>::type> { |
232 | }; |
233 | |
234 | // Returns an iterator_range over the given container which iterates in reverse. |
235 | // Note that the container must have rbegin()/rend() methods for this to work. |
236 | template <typename ContainerTy> |
237 | auto reverse(ContainerTy &&C, |
238 | typename std::enable_if<has_rbegin<ContainerTy>::value>::type * = |
239 | nullptr) -> decltype(make_range(C.rbegin(), C.rend())) { |
240 | return make_range(C.rbegin(), C.rend()); |
241 | } |
242 | |
243 | // Returns a std::reverse_iterator wrapped around the given iterator. |
244 | template <typename IteratorTy> |
245 | std::reverse_iterator<IteratorTy> make_reverse_iterator(IteratorTy It) { |
246 | return std::reverse_iterator<IteratorTy>(It); |
247 | } |
248 | |
249 | // Returns an iterator_range over the given container which iterates in reverse. |
250 | // Note that the container must have begin()/end() methods which return |
251 | // bidirectional iterators for this to work. |
252 | template <typename ContainerTy> |
253 | auto reverse( |
254 | ContainerTy &&C, |
255 | typename std::enable_if<!has_rbegin<ContainerTy>::value>::type * = nullptr) |
256 | -> decltype(make_range(llvm::make_reverse_iterator(std::end(C)), |
257 | llvm::make_reverse_iterator(std::begin(C)))) { |
258 | return make_range(llvm::make_reverse_iterator(std::end(C)), |
259 | llvm::make_reverse_iterator(std::begin(C))); |
260 | } |
261 | |
262 | /// An iterator adaptor that filters the elements of given inner iterators. |
263 | /// |
264 | /// The predicate parameter should be a callable object that accepts the wrapped |
265 | /// iterator's reference type and returns a bool. When incrementing or |
266 | /// decrementing the iterator, it will call the predicate on each element and |
267 | /// skip any where it returns false. |
268 | /// |
269 | /// \code |
270 | /// int A[] = { 1, 2, 3, 4 }; |
271 | /// auto R = make_filter_range(A, [](int N) { return N % 2 == 1; }); |
272 | /// // R contains { 1, 3 }. |
273 | /// \endcode |
274 | template <typename WrappedIteratorT, typename PredicateT> |
275 | class filter_iterator |
276 | : public iterator_adaptor_base< |
277 | filter_iterator<WrappedIteratorT, PredicateT>, WrappedIteratorT, |
278 | typename std::common_type< |
279 | std::forward_iterator_tag, |
280 | typename std::iterator_traits< |
281 | WrappedIteratorT>::iterator_category>::type> { |
282 | using BaseT = iterator_adaptor_base< |
283 | filter_iterator<WrappedIteratorT, PredicateT>, WrappedIteratorT, |
284 | typename std::common_type< |
285 | std::forward_iterator_tag, |
286 | typename std::iterator_traits<WrappedIteratorT>::iterator_category>:: |
287 | type>; |
288 | |
289 | struct PayloadType { |
290 | WrappedIteratorT End; |
291 | PredicateT Pred; |
292 | }; |
293 | |
294 | Optional<PayloadType> Payload; |
295 | |
296 | void findNextValid() { |
297 | assert(Payload && "Payload should be engaged when findNextValid is called")(static_cast <bool> (Payload && "Payload should be engaged when findNextValid is called" ) ? void (0) : __assert_fail ("Payload && \"Payload should be engaged when findNextValid is called\"" , "/build/llvm-toolchain-snapshot-7~svn329677/include/llvm/ADT/STLExtras.h" , 297, __extension__ __PRETTY_FUNCTION__)); |
298 | while (this->I != Payload->End && !Payload->Pred(*this->I)) |
299 | BaseT::operator++(); |
300 | } |
301 | |
302 | // Construct the begin iterator. The begin iterator requires to know where end |
303 | // is, so that it can properly stop when it hits end. |
304 | filter_iterator(WrappedIteratorT Begin, WrappedIteratorT End, PredicateT Pred) |
305 | : BaseT(std::move(Begin)), |
306 | Payload(PayloadType{std::move(End), std::move(Pred)}) { |
307 | findNextValid(); |
308 | } |
309 | |
310 | // Construct the end iterator. It's not incrementable, so Payload doesn't |
311 | // have to be engaged. |
312 | filter_iterator(WrappedIteratorT End) : BaseT(End) {} |
313 | |
314 | public: |
315 | using BaseT::operator++; |
316 | |
317 | filter_iterator &operator++() { |
318 | BaseT::operator++(); |
319 | findNextValid(); |
320 | return *this; |
321 | } |
322 | |
323 | template <typename RT, typename PT> |
324 | friend iterator_range<filter_iterator<detail::IterOfRange<RT>, PT>> |
325 | make_filter_range(RT &&, PT); |
326 | }; |
327 | |
328 | /// Convenience function that takes a range of elements and a predicate, |
329 | /// and return a new filter_iterator range. |
330 | /// |
331 | /// FIXME: Currently if RangeT && is a rvalue reference to a temporary, the |
332 | /// lifetime of that temporary is not kept by the returned range object, and the |
333 | /// temporary is going to be dropped on the floor after the make_iterator_range |
334 | /// full expression that contains this function call. |
335 | template <typename RangeT, typename PredicateT> |
336 | iterator_range<filter_iterator<detail::IterOfRange<RangeT>, PredicateT>> |
337 | make_filter_range(RangeT &&Range, PredicateT Pred) { |
338 | using FilterIteratorT = |
339 | filter_iterator<detail::IterOfRange<RangeT>, PredicateT>; |
340 | return make_range(FilterIteratorT(std::begin(std::forward<RangeT>(Range)), |
341 | std::end(std::forward<RangeT>(Range)), |
342 | std::move(Pred)), |
343 | FilterIteratorT(std::end(std::forward<RangeT>(Range)))); |
344 | } |
345 | |
346 | // forward declarations required by zip_shortest/zip_first |
347 | template <typename R, typename UnaryPredicate> |
348 | bool all_of(R &&range, UnaryPredicate P); |
349 | |
350 | template <size_t... I> struct index_sequence; |
351 | |
352 | template <class... Ts> struct index_sequence_for; |
353 | |
354 | namespace detail { |
355 | |
356 | using std::declval; |
357 | |
358 | // We have to alias this since inlining the actual type at the usage site |
359 | // in the parameter list of iterator_facade_base<> below ICEs MSVC 2017. |
360 | template<typename... Iters> struct ZipTupleType { |
361 | using type = std::tuple<decltype(*declval<Iters>())...>; |
362 | }; |
363 | |
364 | template <typename ZipType, typename... Iters> |
365 | using zip_traits = iterator_facade_base< |
366 | ZipType, typename std::common_type<std::bidirectional_iterator_tag, |
367 | typename std::iterator_traits< |
368 | Iters>::iterator_category...>::type, |
369 | // ^ TODO: Implement random access methods. |
370 | typename ZipTupleType<Iters...>::type, |
371 | typename std::iterator_traits<typename std::tuple_element< |
372 | 0, std::tuple<Iters...>>::type>::difference_type, |
373 | // ^ FIXME: This follows boost::make_zip_iterator's assumption that all |
374 | // inner iterators have the same difference_type. It would fail if, for |
375 | // instance, the second field's difference_type were non-numeric while the |
376 | // first is. |
377 | typename ZipTupleType<Iters...>::type *, |
378 | typename ZipTupleType<Iters...>::type>; |
379 | |
380 | template <typename ZipType, typename... Iters> |
381 | struct zip_common : public zip_traits<ZipType, Iters...> { |
382 | using Base = zip_traits<ZipType, Iters...>; |
383 | using value_type = typename Base::value_type; |
384 | |
385 | std::tuple<Iters...> iterators; |
386 | |
387 | protected: |
388 | template <size_t... Ns> value_type deref(index_sequence<Ns...>) const { |
389 | return value_type(*std::get<Ns>(iterators)...); |
390 | } |
391 | |
392 | template <size_t... Ns> |
393 | decltype(iterators) tup_inc(index_sequence<Ns...>) const { |
394 | return std::tuple<Iters...>(std::next(std::get<Ns>(iterators))...); |
395 | } |
396 | |
397 | template <size_t... Ns> |
398 | decltype(iterators) tup_dec(index_sequence<Ns...>) const { |
399 | return std::tuple<Iters...>(std::prev(std::get<Ns>(iterators))...); |
400 | } |
401 | |
402 | public: |
403 | zip_common(Iters &&... ts) : iterators(std::forward<Iters>(ts)...) {} |
404 | |
405 | value_type operator*() { return deref(index_sequence_for<Iters...>{}); } |
406 | |
407 | const value_type operator*() const { |
408 | return deref(index_sequence_for<Iters...>{}); |
409 | } |
410 | |
411 | ZipType &operator++() { |
412 | iterators = tup_inc(index_sequence_for<Iters...>{}); |
413 | return *reinterpret_cast<ZipType *>(this); |
414 | } |
415 | |
416 | ZipType &operator--() { |
417 | static_assert(Base::IsBidirectional, |
418 | "All inner iterators must be at least bidirectional."); |
419 | iterators = tup_dec(index_sequence_for<Iters...>{}); |
420 | return *reinterpret_cast<ZipType *>(this); |
421 | } |
422 | }; |
423 | |
424 | template <typename... Iters> |
425 | struct zip_first : public zip_common<zip_first<Iters...>, Iters...> { |
426 | using Base = zip_common<zip_first<Iters...>, Iters...>; |
427 | |
428 | bool operator==(const zip_first<Iters...> &other) const { |
429 | return std::get<0>(this->iterators) == std::get<0>(other.iterators); |
430 | } |
431 | |
432 | zip_first(Iters &&... ts) : Base(std::forward<Iters>(ts)...) {} |
433 | }; |
434 | |
435 | template <typename... Iters> |
436 | class zip_shortest : public zip_common<zip_shortest<Iters...>, Iters...> { |
437 | template <size_t... Ns> |
438 | bool test(const zip_shortest<Iters...> &other, index_sequence<Ns...>) const { |
439 | return all_of(std::initializer_list<bool>{std::get<Ns>(this->iterators) != |
440 | std::get<Ns>(other.iterators)...}, |
441 | identity<bool>{}); |
442 | } |
443 | |
444 | public: |
445 | using Base = zip_common<zip_shortest<Iters...>, Iters...>; |
446 | |
447 | zip_shortest(Iters &&... ts) : Base(std::forward<Iters>(ts)...) {} |
448 | |
449 | bool operator==(const zip_shortest<Iters...> &other) const { |
450 | return !test(other, index_sequence_for<Iters...>{}); |
451 | } |
452 | }; |
453 | |
454 | template <template <typename...> class ItType, typename... Args> class zippy { |
455 | public: |
456 | using iterator = ItType<decltype(std::begin(std::declval<Args>()))...>; |
457 | using iterator_category = typename iterator::iterator_category; |
458 | using value_type = typename iterator::value_type; |
459 | using difference_type = typename iterator::difference_type; |
460 | using pointer = typename iterator::pointer; |
461 | using reference = typename iterator::reference; |
462 | |
463 | private: |
464 | std::tuple<Args...> ts; |
465 | |
466 | template <size_t... Ns> iterator begin_impl(index_sequence<Ns...>) const { |
467 | return iterator(std::begin(std::get<Ns>(ts))...); |
468 | } |
469 | template <size_t... Ns> iterator end_impl(index_sequence<Ns...>) const { |
470 | return iterator(std::end(std::get<Ns>(ts))...); |
471 | } |
472 | |
473 | public: |
474 | zippy(Args &&... ts_) : ts(std::forward<Args>(ts_)...) {} |
475 | |
476 | iterator begin() const { return begin_impl(index_sequence_for<Args...>{}); } |
477 | iterator end() const { return end_impl(index_sequence_for<Args...>{}); } |
478 | }; |
479 | |
480 | } // end namespace detail |
481 | |
482 | /// zip iterator for two or more iteratable types. |
483 | template <typename T, typename U, typename... Args> |
484 | detail::zippy<detail::zip_shortest, T, U, Args...> zip(T &&t, U &&u, |
485 | Args &&... args) { |
486 | return detail::zippy<detail::zip_shortest, T, U, Args...>( |
487 | std::forward<T>(t), std::forward<U>(u), std::forward<Args>(args)...); |
488 | } |
489 | |
490 | /// zip iterator that, for the sake of efficiency, assumes the first iteratee to |
491 | /// be the shortest. |
492 | template <typename T, typename U, typename... Args> |
493 | detail::zippy<detail::zip_first, T, U, Args...> zip_first(T &&t, U &&u, |
494 | Args &&... args) { |
495 | return detail::zippy<detail::zip_first, T, U, Args...>( |
496 | std::forward<T>(t), std::forward<U>(u), std::forward<Args>(args)...); |
497 | } |
498 | |
499 | /// Iterator wrapper that concatenates sequences together. |
500 | /// |
501 | /// This can concatenate different iterators, even with different types, into |
502 | /// a single iterator provided the value types of all the concatenated |
503 | /// iterators expose `reference` and `pointer` types that can be converted to |
504 | /// `ValueT &` and `ValueT *` respectively. It doesn't support more |
505 | /// interesting/customized pointer or reference types. |
506 | /// |
507 | /// Currently this only supports forward or higher iterator categories as |
508 | /// inputs and always exposes a forward iterator interface. |
509 | template <typename ValueT, typename... IterTs> |
510 | class concat_iterator |
511 | : public iterator_facade_base<concat_iterator<ValueT, IterTs...>, |
512 | std::forward_iterator_tag, ValueT> { |
513 | using BaseT = typename concat_iterator::iterator_facade_base; |
514 | |
515 | /// We store both the current and end iterators for each concatenated |
516 | /// sequence in a tuple of pairs. |
517 | /// |
518 | /// Note that something like iterator_range seems nice at first here, but the |
519 | /// range properties are of little benefit and end up getting in the way |
520 | /// because we need to do mutation on the current iterators. |
521 | std::tuple<std::pair<IterTs, IterTs>...> IterPairs; |
522 | |
523 | /// Attempts to increment a specific iterator. |
524 | /// |
525 | /// Returns true if it was able to increment the iterator. Returns false if |
526 | /// the iterator is already at the end iterator. |
527 | template <size_t Index> bool incrementHelper() { |
528 | auto &IterPair = std::get<Index>(IterPairs); |
529 | if (IterPair.first == IterPair.second) |
530 | return false; |
531 | |
532 | ++IterPair.first; |
533 | return true; |
534 | } |
535 | |
536 | /// Increments the first non-end iterator. |
537 | /// |
538 | /// It is an error to call this with all iterators at the end. |
539 | template <size_t... Ns> void increment(index_sequence<Ns...>) { |
540 | // Build a sequence of functions to increment each iterator if possible. |
541 | bool (concat_iterator::*IncrementHelperFns[])() = { |
542 | &concat_iterator::incrementHelper<Ns>...}; |
543 | |
544 | // Loop over them, and stop as soon as we succeed at incrementing one. |
545 | for (auto &IncrementHelperFn : IncrementHelperFns) |
546 | if ((this->*IncrementHelperFn)()) |
547 | return; |
548 | |
549 | llvm_unreachable("Attempted to increment an end concat iterator!")::llvm::llvm_unreachable_internal("Attempted to increment an end concat iterator!" , "/build/llvm-toolchain-snapshot-7~svn329677/include/llvm/ADT/STLExtras.h" , 549); |
550 | } |
551 | |
552 | /// Returns null if the specified iterator is at the end. Otherwise, |
553 | /// dereferences the iterator and returns the address of the resulting |
554 | /// reference. |
555 | template <size_t Index> ValueT *getHelper() const { |
556 | auto &IterPair = std::get<Index>(IterPairs); |
557 | if (IterPair.first == IterPair.second) |
558 | return nullptr; |
559 | |
560 | return &*IterPair.first; |
561 | } |
562 | |
563 | /// Finds the first non-end iterator, dereferences, and returns the resulting |
564 | /// reference. |
565 | /// |
566 | /// It is an error to call this with all iterators at the end. |
567 | template <size_t... Ns> ValueT &get(index_sequence<Ns...>) const { |
568 | // Build a sequence of functions to get from iterator if possible. |
569 | ValueT *(concat_iterator::*GetHelperFns[])() const = { |
570 | &concat_iterator::getHelper<Ns>...}; |
571 | |
572 | // Loop over them, and return the first result we find. |
573 | for (auto &GetHelperFn : GetHelperFns) |
574 | if (ValueT *P = (this->*GetHelperFn)()) |
575 | return *P; |
576 | |
577 | llvm_unreachable("Attempted to get a pointer from an end concat iterator!")::llvm::llvm_unreachable_internal("Attempted to get a pointer from an end concat iterator!" , "/build/llvm-toolchain-snapshot-7~svn329677/include/llvm/ADT/STLExtras.h" , 577); |
578 | } |
579 | |
580 | public: |
581 | /// Constructs an iterator from a squence of ranges. |
582 | /// |
583 | /// We need the full range to know how to switch between each of the |
584 | /// iterators. |
585 | template <typename... RangeTs> |
586 | explicit concat_iterator(RangeTs &&... Ranges) |
587 | : IterPairs({std::begin(Ranges), std::end(Ranges)}...) {} |
588 | |
589 | using BaseT::operator++; |
590 | |
591 | concat_iterator &operator++() { |
592 | increment(index_sequence_for<IterTs...>()); |
593 | return *this; |
594 | } |
595 | |
596 | ValueT &operator*() const { return get(index_sequence_for<IterTs...>()); } |
597 | |
598 | bool operator==(const concat_iterator &RHS) const { |
599 | return IterPairs == RHS.IterPairs; |
600 | } |
601 | }; |
602 | |
603 | namespace detail { |
604 | |
605 | /// Helper to store a sequence of ranges being concatenated and access them. |
606 | /// |
607 | /// This is designed to facilitate providing actual storage when temporaries |
608 | /// are passed into the constructor such that we can use it as part of range |
609 | /// based for loops. |
610 | template <typename ValueT, typename... RangeTs> class concat_range { |
611 | public: |
612 | using iterator = |
613 | concat_iterator<ValueT, |
614 | decltype(std::begin(std::declval<RangeTs &>()))...>; |
615 | |
616 | private: |
617 | std::tuple<RangeTs...> Ranges; |
618 | |
619 | template <size_t... Ns> iterator begin_impl(index_sequence<Ns...>) { |
620 | return iterator(std::get<Ns>(Ranges)...); |
621 | } |
622 | template <size_t... Ns> iterator end_impl(index_sequence<Ns...>) { |
623 | return iterator(make_range(std::end(std::get<Ns>(Ranges)), |
624 | std::end(std::get<Ns>(Ranges)))...); |
625 | } |
626 | |
627 | public: |
628 | concat_range(RangeTs &&... Ranges) |
629 | : Ranges(std::forward<RangeTs>(Ranges)...) {} |
630 | |
631 | iterator begin() { return begin_impl(index_sequence_for<RangeTs...>{}); } |
632 | iterator end() { return end_impl(index_sequence_for<RangeTs...>{}); } |
633 | }; |
634 | |
635 | } // end namespace detail |
636 | |
637 | /// Concatenated range across two or more ranges. |
638 | /// |
639 | /// The desired value type must be explicitly specified. |
640 | template <typename ValueT, typename... RangeTs> |
641 | detail::concat_range<ValueT, RangeTs...> concat(RangeTs &&... Ranges) { |
642 | static_assert(sizeof...(RangeTs) > 1, |
643 | "Need more than one range to concatenate!"); |
644 | return detail::concat_range<ValueT, RangeTs...>( |
645 | std::forward<RangeTs>(Ranges)...); |
646 | } |
647 | |
648 | //===----------------------------------------------------------------------===// |
649 | // Extra additions to <utility> |
650 | //===----------------------------------------------------------------------===// |
651 | |
652 | /// \brief Function object to check whether the first component of a std::pair |
653 | /// compares less than the first component of another std::pair. |
654 | struct less_first { |
655 | template <typename T> bool operator()(const T &lhs, const T &rhs) const { |
656 | return lhs.first < rhs.first; |
657 | } |
658 | }; |
659 | |
660 | /// \brief Function object to check whether the second component of a std::pair |
661 | /// compares less than the second component of another std::pair. |
662 | struct less_second { |
663 | template <typename T> bool operator()(const T &lhs, const T &rhs) const { |
664 | return lhs.second < rhs.second; |
665 | } |
666 | }; |
667 | |
668 | // A subset of N3658. More stuff can be added as-needed. |
669 | |
670 | /// \brief Represents a compile-time sequence of integers. |
671 | template <class T, T... I> struct integer_sequence { |
672 | using value_type = T; |
673 | |
674 | static constexpr size_t size() { return sizeof...(I); } |
675 | }; |
676 | |
677 | /// \brief Alias for the common case of a sequence of size_ts. |
678 | template <size_t... I> |
679 | struct index_sequence : integer_sequence<std::size_t, I...> {}; |
680 | |
681 | template <std::size_t N, std::size_t... I> |
682 | struct build_index_impl : build_index_impl<N - 1, N - 1, I...> {}; |
683 | template <std::size_t... I> |
684 | struct build_index_impl<0, I...> : index_sequence<I...> {}; |
685 | |
686 | /// \brief Creates a compile-time integer sequence for a parameter pack. |
687 | template <class... Ts> |
688 | struct index_sequence_for : build_index_impl<sizeof...(Ts)> {}; |
689 | |
690 | /// Utility type to build an inheritance chain that makes it easy to rank |
691 | /// overload candidates. |
692 | template <int N> struct rank : rank<N - 1> {}; |
693 | template <> struct rank<0> {}; |
694 | |
695 | /// \brief traits class for checking whether type T is one of any of the given |
696 | /// types in the variadic list. |
697 | template <typename T, typename... Ts> struct is_one_of { |
698 | static const bool value = false; |
699 | }; |
700 | |
701 | template <typename T, typename U, typename... Ts> |
702 | struct is_one_of<T, U, Ts...> { |
703 | static const bool value = |
704 | std::is_same<T, U>::value || is_one_of<T, Ts...>::value; |
705 | }; |
706 | |
707 | /// \brief traits class for checking whether type T is a base class for all |
708 | /// the given types in the variadic list. |
709 | template <typename T, typename... Ts> struct are_base_of { |
710 | static const bool value = true; |
711 | }; |
712 | |
713 | template <typename T, typename U, typename... Ts> |
714 | struct are_base_of<T, U, Ts...> { |
715 | static const bool value = |
716 | std::is_base_of<T, U>::value && are_base_of<T, Ts...>::value; |
717 | }; |
718 | |
719 | //===----------------------------------------------------------------------===// |
720 | // Extra additions for arrays |
721 | //===----------------------------------------------------------------------===// |
722 | |
723 | /// Find the length of an array. |
724 | template <class T, std::size_t N> |
725 | constexpr inline size_t array_lengthof(T (&)[N]) { |
726 | return N; |
727 | } |
728 | |
729 | /// Adapt std::less<T> for array_pod_sort. |
730 | template<typename T> |
731 | inline int array_pod_sort_comparator(const void *P1, const void *P2) { |
732 | if (std::less<T>()(*reinterpret_cast<const T*>(P1), |
733 | *reinterpret_cast<const T*>(P2))) |
734 | return -1; |
735 | if (std::less<T>()(*reinterpret_cast<const T*>(P2), |
736 | *reinterpret_cast<const T*>(P1))) |
737 | return 1; |
738 | return 0; |
739 | } |
740 | |
741 | /// get_array_pod_sort_comparator - This is an internal helper function used to |
742 | /// get type deduction of T right. |
743 | template<typename T> |
744 | inline int (*get_array_pod_sort_comparator(const T &)) |
745 | (const void*, const void*) { |
746 | return array_pod_sort_comparator<T>; |
747 | } |
748 | |
749 | /// array_pod_sort - This sorts an array with the specified start and end |
750 | /// extent. This is just like std::sort, except that it calls qsort instead of |
751 | /// using an inlined template. qsort is slightly slower than std::sort, but |
752 | /// most sorts are not performance critical in LLVM and std::sort has to be |
753 | /// template instantiated for each type, leading to significant measured code |
754 | /// bloat. This function should generally be used instead of std::sort where |
755 | /// possible. |
756 | /// |
757 | /// This function assumes that you have simple POD-like types that can be |
758 | /// compared with std::less and can be moved with memcpy. If this isn't true, |
759 | /// you should use std::sort. |
760 | /// |
761 | /// NOTE: If qsort_r were portable, we could allow a custom comparator and |
762 | /// default to std::less. |
763 | template<class IteratorTy> |
764 | inline void array_pod_sort(IteratorTy Start, IteratorTy End) { |
765 | // Don't inefficiently call qsort with one element or trigger undefined |
766 | // behavior with an empty sequence. |
767 | auto NElts = End - Start; |
768 | if (NElts <= 1) return; |
769 | #ifdef EXPENSIVE_CHECKS |
770 | std::mt19937 Generator(std::random_device{}()); |
771 | std::shuffle(Start, End, Generator); |
772 | #endif |
773 | qsort(&*Start, NElts, sizeof(*Start), get_array_pod_sort_comparator(*Start)); |
774 | } |
775 | |
776 | template <class IteratorTy> |
777 | inline void array_pod_sort( |
778 | IteratorTy Start, IteratorTy End, |
779 | int (*Compare)( |
780 | const typename std::iterator_traits<IteratorTy>::value_type *, |
781 | const typename std::iterator_traits<IteratorTy>::value_type *)) { |
782 | // Don't inefficiently call qsort with one element or trigger undefined |
783 | // behavior with an empty sequence. |
784 | auto NElts = End - Start; |
785 | if (NElts <= 1) return; |
786 | #ifdef EXPENSIVE_CHECKS |
787 | std::mt19937 Generator(std::random_device{}()); |
788 | std::shuffle(Start, End, Generator); |
789 | #endif |
790 | qsort(&*Start, NElts, sizeof(*Start), |
791 | reinterpret_cast<int (*)(const void *, const void *)>(Compare)); |
792 | } |
793 | |
794 | // Provide wrappers to std::sort which shuffle the elements before sorting |
795 | // to help uncover non-deterministic behavior (PR35135). |
796 | template <typename IteratorTy> |
797 | inline void sort(IteratorTy Start, IteratorTy End) { |
798 | #ifdef EXPENSIVE_CHECKS |
799 | std::mt19937 Generator(std::random_device{}()); |
800 | std::shuffle(Start, End, Generator); |
801 | #endif |
802 | std::sort(Start, End); |
803 | } |
804 | |
805 | template <typename IteratorTy, typename Compare> |
806 | inline void sort(IteratorTy Start, IteratorTy End, Compare Comp) { |
807 | #ifdef EXPENSIVE_CHECKS |
808 | std::mt19937 Generator(std::random_device{}()); |
809 | std::shuffle(Start, End, Generator); |
810 | #endif |
811 | std::sort(Start, End, Comp); |
812 | } |
813 | |
814 | //===----------------------------------------------------------------------===// |
815 | // Extra additions to <algorithm> |
816 | //===----------------------------------------------------------------------===// |
817 | |
818 | /// For a container of pointers, deletes the pointers and then clears the |
819 | /// container. |
820 | template<typename Container> |
821 | void DeleteContainerPointers(Container &C) { |
822 | for (auto V : C) |
823 | delete V; |
824 | C.clear(); |
825 | } |
826 | |
827 | /// In a container of pairs (usually a map) whose second element is a pointer, |
828 | /// deletes the second elements and then clears the container. |
829 | template<typename Container> |
830 | void DeleteContainerSeconds(Container &C) { |
831 | for (auto &V : C) |
832 | delete V.second; |
833 | C.clear(); |
834 | } |
835 | |
836 | /// Provide wrappers to std::for_each which take ranges instead of having to |
837 | /// pass begin/end explicitly. |
838 | template <typename R, typename UnaryPredicate> |
839 | UnaryPredicate for_each(R &&Range, UnaryPredicate P) { |
840 | return std::for_each(adl_begin(Range), adl_end(Range), P); |
841 | } |
842 | |
843 | /// Provide wrappers to std::all_of which take ranges instead of having to pass |
844 | /// begin/end explicitly. |
845 | template <typename R, typename UnaryPredicate> |
846 | bool all_of(R &&Range, UnaryPredicate P) { |
847 | return std::all_of(adl_begin(Range), adl_end(Range), P); |
848 | } |
849 | |
850 | /// Provide wrappers to std::any_of which take ranges instead of having to pass |
851 | /// begin/end explicitly. |
852 | template <typename R, typename UnaryPredicate> |
853 | bool any_of(R &&Range, UnaryPredicate P) { |
854 | return std::any_of(adl_begin(Range), adl_end(Range), P); |
855 | } |
856 | |
857 | /// Provide wrappers to std::none_of which take ranges instead of having to pass |
858 | /// begin/end explicitly. |
859 | template <typename R, typename UnaryPredicate> |
860 | bool none_of(R &&Range, UnaryPredicate P) { |
861 | return std::none_of(adl_begin(Range), adl_end(Range), P); |
862 | } |
863 | |
864 | /// Provide wrappers to std::find which take ranges instead of having to pass |
865 | /// begin/end explicitly. |
866 | template <typename R, typename T> |
867 | auto find(R &&Range, const T &Val) -> decltype(adl_begin(Range)) { |
868 | return std::find(adl_begin(Range), adl_end(Range), Val); |
869 | } |
870 | |
871 | /// Provide wrappers to std::find_if which take ranges instead of having to pass |
872 | /// begin/end explicitly. |
873 | template <typename R, typename UnaryPredicate> |
874 | auto find_if(R &&Range, UnaryPredicate P) -> decltype(adl_begin(Range)) { |
875 | return std::find_if(adl_begin(Range), adl_end(Range), P); |
876 | } |
877 | |
878 | template <typename R, typename UnaryPredicate> |
879 | auto find_if_not(R &&Range, UnaryPredicate P) -> decltype(adl_begin(Range)) { |
880 | return std::find_if_not(adl_begin(Range), adl_end(Range), P); |
881 | } |
882 | |
883 | /// Provide wrappers to std::remove_if which take ranges instead of having to |
884 | /// pass begin/end explicitly. |
885 | template <typename R, typename UnaryPredicate> |
886 | auto remove_if(R &&Range, UnaryPredicate P) -> decltype(adl_begin(Range)) { |
887 | return std::remove_if(adl_begin(Range), adl_end(Range), P); |
888 | } |
889 | |
890 | /// Provide wrappers to std::copy_if which take ranges instead of having to |
891 | /// pass begin/end explicitly. |
892 | template <typename R, typename OutputIt, typename UnaryPredicate> |
893 | OutputIt copy_if(R &&Range, OutputIt Out, UnaryPredicate P) { |
894 | return std::copy_if(adl_begin(Range), adl_end(Range), Out, P); |
895 | } |
896 | |
897 | template <typename R, typename OutputIt> |
898 | OutputIt copy(R &&Range, OutputIt Out) { |
899 | return std::copy(adl_begin(Range), adl_end(Range), Out); |
900 | } |
901 | |
902 | /// Wrapper function around std::find to detect if an element exists |
903 | /// in a container. |
904 | template <typename R, typename E> |
905 | bool is_contained(R &&Range, const E &Element) { |
906 | return std::find(adl_begin(Range), adl_end(Range), Element) != adl_end(Range); |
907 | } |
908 | |
909 | /// Wrapper function around std::count to count the number of times an element |
910 | /// \p Element occurs in the given range \p Range. |
911 | template <typename R, typename E> |
912 | auto count(R &&Range, const E &Element) -> |
913 | typename std::iterator_traits<decltype(adl_begin(Range))>::difference_type { |
914 | return std::count(adl_begin(Range), adl_end(Range), Element); |
915 | } |
916 | |
917 | /// Wrapper function around std::count_if to count the number of times an |
918 | /// element satisfying a given predicate occurs in a range. |
919 | template <typename R, typename UnaryPredicate> |
920 | auto count_if(R &&Range, UnaryPredicate P) -> |
921 | typename std::iterator_traits<decltype(adl_begin(Range))>::difference_type { |
922 | return std::count_if(adl_begin(Range), adl_end(Range), P); |
923 | } |
924 | |
925 | /// Wrapper function around std::transform to apply a function to a range and |
926 | /// store the result elsewhere. |
927 | template <typename R, typename OutputIt, typename UnaryPredicate> |
928 | OutputIt transform(R &&Range, OutputIt d_first, UnaryPredicate P) { |
929 | return std::transform(adl_begin(Range), adl_end(Range), d_first, P); |
930 | } |
931 | |
932 | /// Provide wrappers to std::partition which take ranges instead of having to |
933 | /// pass begin/end explicitly. |
934 | template <typename R, typename UnaryPredicate> |
935 | auto partition(R &&Range, UnaryPredicate P) -> decltype(adl_begin(Range)) { |
936 | return std::partition(adl_begin(Range), adl_end(Range), P); |
937 | } |
938 | |
939 | /// Provide wrappers to std::lower_bound which take ranges instead of having to |
940 | /// pass begin/end explicitly. |
941 | template <typename R, typename ForwardIt> |
942 | auto lower_bound(R &&Range, ForwardIt I) -> decltype(adl_begin(Range)) { |
943 | return std::lower_bound(adl_begin(Range), adl_end(Range), I); |
944 | } |
945 | |
946 | /// \brief Given a range of type R, iterate the entire range and return a |
947 | /// SmallVector with elements of the vector. This is useful, for example, |
948 | /// when you want to iterate a range and then sort the results. |
949 | template <unsigned Size, typename R> |
950 | SmallVector<typename std::remove_const<detail::ValueOfRange<R>>::type, Size> |
951 | to_vector(R &&Range) { |
952 | return {adl_begin(Range), adl_end(Range)}; |
953 | } |
954 | |
955 | /// Provide a container algorithm similar to C++ Library Fundamentals v2's |
956 | /// `erase_if` which is equivalent to: |
957 | /// |
958 | /// C.erase(remove_if(C, pred), C.end()); |
959 | /// |
960 | /// This version works for any container with an erase method call accepting |
961 | /// two iterators. |
962 | template <typename Container, typename UnaryPredicate> |
963 | void erase_if(Container &C, UnaryPredicate P) { |
964 | C.erase(remove_if(C, P), C.end()); |
965 | } |
966 | |
967 | //===----------------------------------------------------------------------===// |
968 | // Extra additions to <memory> |
969 | //===----------------------------------------------------------------------===// |
970 | |
971 | // Implement make_unique according to N3656. |
972 | |
973 | /// \brief Constructs a `new T()` with the given args and returns a |
974 | /// `unique_ptr<T>` which owns the object. |
975 | /// |
976 | /// Example: |
977 | /// |
978 | /// auto p = make_unique<int>(); |
979 | /// auto p = make_unique<std::tuple<int, int>>(0, 1); |
980 | template <class T, class... Args> |
981 | typename std::enable_if<!std::is_array<T>::value, std::unique_ptr<T>>::type |
982 | make_unique(Args &&... args) { |
983 | return std::unique_ptr<T>(new T(std::forward<Args>(args)...)); |
984 | } |
985 | |
986 | /// \brief Constructs a `new T[n]` with the given args and returns a |
987 | /// `unique_ptr<T[]>` which owns the object. |
988 | /// |
989 | /// \param n size of the new array. |
990 | /// |
991 | /// Example: |
992 | /// |
993 | /// auto p = make_unique<int[]>(2); // value-initializes the array with 0's. |
994 | template <class T> |
995 | typename std::enable_if<std::is_array<T>::value && std::extent<T>::value == 0, |
996 | std::unique_ptr<T>>::type |
997 | make_unique(size_t n) { |
998 | return std::unique_ptr<T>(new typename std::remove_extent<T>::type[n]()); |
999 | } |
1000 | |
1001 | /// This function isn't used and is only here to provide better compile errors. |
1002 | template <class T, class... Args> |
1003 | typename std::enable_if<std::extent<T>::value != 0>::type |
1004 | make_unique(Args &&...) = delete; |
1005 | |
1006 | struct FreeDeleter { |
1007 | void operator()(void* v) { |
1008 | ::free(v); |
1009 | } |
1010 | }; |
1011 | |
1012 | template<typename First, typename Second> |
1013 | struct pair_hash { |
1014 | size_t operator()(const std::pair<First, Second> &P) const { |
1015 | return std::hash<First>()(P.first) * 31 + std::hash<Second>()(P.second); |
1016 | } |
1017 | }; |
1018 | |
1019 | /// A functor like C++14's std::less<void> in its absence. |
1020 | struct less { |
1021 | template <typename A, typename B> bool operator()(A &&a, B &&b) const { |
1022 | return std::forward<A>(a) < std::forward<B>(b); |
1023 | } |
1024 | }; |
1025 | |
1026 | /// A functor like C++14's std::equal<void> in its absence. |
1027 | struct equal { |
1028 | template <typename A, typename B> bool operator()(A &&a, B &&b) const { |
1029 | return std::forward<A>(a) == std::forward<B>(b); |
1030 | } |
1031 | }; |
1032 | |
1033 | /// Binary functor that adapts to any other binary functor after dereferencing |
1034 | /// operands. |
1035 | template <typename T> struct deref { |
1036 | T func; |
1037 | |
1038 | // Could be further improved to cope with non-derivable functors and |
1039 | // non-binary functors (should be a variadic template member function |
1040 | // operator()). |
1041 | template <typename A, typename B> |
1042 | auto operator()(A &lhs, B &rhs) const -> decltype(func(*lhs, *rhs)) { |
1043 | assert(lhs)(static_cast <bool> (lhs) ? void (0) : __assert_fail ("lhs" , "/build/llvm-toolchain-snapshot-7~svn329677/include/llvm/ADT/STLExtras.h" , 1043, __extension__ __PRETTY_FUNCTION__)); |
1044 | assert(rhs)(static_cast <bool> (rhs) ? void (0) : __assert_fail ("rhs" , "/build/llvm-toolchain-snapshot-7~svn329677/include/llvm/ADT/STLExtras.h" , 1044, __extension__ __PRETTY_FUNCTION__)); |
1045 | return func(*lhs, *rhs); |
1046 | } |
1047 | }; |
1048 | |
1049 | namespace detail { |
1050 | |
1051 | template <typename R> class enumerator_iter; |
1052 | |
1053 | template <typename R> struct result_pair { |
1054 | friend class enumerator_iter<R>; |
1055 | |
1056 | result_pair() = default; |
1057 | result_pair(std::size_t Index, IterOfRange<R> Iter) |
1058 | : Index(Index), Iter(Iter) {} |
1059 | |
1060 | result_pair<R> &operator=(const result_pair<R> &Other) { |
1061 | Index = Other.Index; |
1062 | Iter = Other.Iter; |
1063 | return *this; |
1064 | } |
1065 | |
1066 | std::size_t index() const { return Index; } |
1067 | const ValueOfRange<R> &value() const { return *Iter; } |
1068 | ValueOfRange<R> &value() { return *Iter; } |
1069 | |
1070 | private: |
1071 | std::size_t Index = std::numeric_limits<std::size_t>::max(); |
1072 | IterOfRange<R> Iter; |
1073 | }; |
1074 | |
1075 | template <typename R> |
1076 | class enumerator_iter |
1077 | : public iterator_facade_base< |
1078 | enumerator_iter<R>, std::forward_iterator_tag, result_pair<R>, |
1079 | typename std::iterator_traits<IterOfRange<R>>::difference_type, |
1080 | typename std::iterator_traits<IterOfRange<R>>::pointer, |
1081 | typename std::iterator_traits<IterOfRange<R>>::reference> { |
1082 | using result_type = result_pair<R>; |
1083 | |
1084 | public: |
1085 | explicit enumerator_iter(IterOfRange<R> EndIter) |
1086 | : Result(std::numeric_limits<size_t>::max(), EndIter) {} |
1087 | |
1088 | enumerator_iter(std::size_t Index, IterOfRange<R> Iter) |
1089 | : Result(Index, Iter) {} |
1090 | |
1091 | result_type &operator*() { return Result; } |
1092 | const result_type &operator*() const { return Result; } |
1093 | |
1094 | enumerator_iter<R> &operator++() { |
1095 | assert(Result.Index != std::numeric_limits<size_t>::max())(static_cast <bool> (Result.Index != std::numeric_limits <size_t>::max()) ? void (0) : __assert_fail ("Result.Index != std::numeric_limits<size_t>::max()" , "/build/llvm-toolchain-snapshot-7~svn329677/include/llvm/ADT/STLExtras.h" , 1095, __extension__ __PRETTY_FUNCTION__)); |
1096 | ++Result.Iter; |
1097 | ++Result.Index; |
1098 | return *this; |
1099 | } |
1100 | |
1101 | bool operator==(const enumerator_iter<R> &RHS) const { |
1102 | // Don't compare indices here, only iterators. It's possible for an end |
1103 | // iterator to have different indices depending on whether it was created |
1104 | // by calling std::end() versus incrementing a valid iterator. |
1105 | return Result.Iter == RHS.Result.Iter; |
1106 | } |
1107 | |
1108 | enumerator_iter<R> &operator=(const enumerator_iter<R> &Other) { |
1109 | Result = Other.Result; |
1110 | return *this; |
1111 | } |
1112 | |
1113 | private: |
1114 | result_type Result; |
1115 | }; |
1116 | |
1117 | template <typename R> class enumerator { |
1118 | public: |
1119 | explicit enumerator(R &&Range) : TheRange(std::forward<R>(Range)) {} |
1120 | |
1121 | enumerator_iter<R> begin() { |
1122 | return enumerator_iter<R>(0, std::begin(TheRange)); |
1123 | } |
1124 | |
1125 | enumerator_iter<R> end() { |
1126 | return enumerator_iter<R>(std::end(TheRange)); |
1127 | } |
1128 | |
1129 | private: |
1130 | R TheRange; |
1131 | }; |
1132 | |
1133 | } // end namespace detail |
1134 | |
1135 | /// Given an input range, returns a new range whose values are are pair (A,B) |
1136 | /// such that A is the 0-based index of the item in the sequence, and B is |
1137 | /// the value from the original sequence. Example: |
1138 | /// |
1139 | /// std::vector<char> Items = {'A', 'B', 'C', 'D'}; |
1140 | /// for (auto X : enumerate(Items)) { |
1141 | /// printf("Item %d - %c\n", X.index(), X.value()); |
1142 | /// } |
1143 | /// |
1144 | /// Output: |
1145 | /// Item 0 - A |
1146 | /// Item 1 - B |
1147 | /// Item 2 - C |
1148 | /// Item 3 - D |
1149 | /// |
1150 | template <typename R> detail::enumerator<R> enumerate(R &&TheRange) { |
1151 | return detail::enumerator<R>(std::forward<R>(TheRange)); |
1152 | } |
1153 | |
1154 | namespace detail { |
1155 | |
1156 | template <typename F, typename Tuple, std::size_t... I> |
1157 | auto apply_tuple_impl(F &&f, Tuple &&t, index_sequence<I...>) |
1158 | -> decltype(std::forward<F>(f)(std::get<I>(std::forward<Tuple>(t))...)) { |
1159 | return std::forward<F>(f)(std::get<I>(std::forward<Tuple>(t))...); |
1160 | } |
1161 | |
1162 | } // end namespace detail |
1163 | |
1164 | /// Given an input tuple (a1, a2, ..., an), pass the arguments of the |
1165 | /// tuple variadically to f as if by calling f(a1, a2, ..., an) and |
1166 | /// return the result. |
1167 | template <typename F, typename Tuple> |
1168 | auto apply_tuple(F &&f, Tuple &&t) -> decltype(detail::apply_tuple_impl( |
1169 | std::forward<F>(f), std::forward<Tuple>(t), |
1170 | build_index_impl< |
1171 | std::tuple_size<typename std::decay<Tuple>::type>::value>{})) { |
1172 | using Indices = build_index_impl< |
1173 | std::tuple_size<typename std::decay<Tuple>::type>::value>; |
1174 | |
1175 | return detail::apply_tuple_impl(std::forward<F>(f), std::forward<Tuple>(t), |
1176 | Indices{}); |
1177 | } |
1178 | |
1179 | } // end namespace llvm |
1180 | |
1181 | #endif // LLVM_ADT_STLEXTRAS_H |
1 | // unique_ptr implementation -*- C++ -*- |
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 bits/unique_ptr.h |
26 | * This is an internal header file, included by other library headers. |
27 | * Do not attempt to use it directly. @headername{memory} |
28 | */ |
29 | |
30 | #ifndef _UNIQUE_PTR_H1 |
31 | #define _UNIQUE_PTR_H1 1 |
32 | |
33 | #include <bits/c++config.h> |
34 | #include <debug/assertions.h> |
35 | #include <type_traits> |
36 | #include <utility> |
37 | #include <tuple> |
38 | #include <bits/stl_function.h> |
39 | #include <bits/functional_hash.h> |
40 | |
41 | namespace std _GLIBCXX_VISIBILITY(default)__attribute__ ((__visibility__ ("default"))) |
42 | { |
43 | _GLIBCXX_BEGIN_NAMESPACE_VERSION |
44 | |
45 | /** |
46 | * @addtogroup pointer_abstractions |
47 | * @{ |
48 | */ |
49 | |
50 | #if _GLIBCXX_USE_DEPRECATED1 |
51 | template<typename> class auto_ptr; |
52 | #endif |
53 | |
54 | /// Primary template of default_delete, used by unique_ptr |
55 | template<typename _Tp> |
56 | struct default_delete |
57 | { |
58 | /// Default constructor |
59 | constexpr default_delete() noexcept = default; |
60 | |
61 | /** @brief Converting constructor. |
62 | * |
63 | * Allows conversion from a deleter for arrays of another type, @p _Up, |
64 | * only if @p _Up* is convertible to @p _Tp*. |
65 | */ |
66 | template<typename _Up, typename = typename |
67 | enable_if<is_convertible<_Up*, _Tp*>::value>::type> |
68 | default_delete(const default_delete<_Up>&) noexcept { } |
69 | |
70 | /// Calls @c delete @p __ptr |
71 | void |
72 | operator()(_Tp* __ptr) const |
73 | { |
74 | static_assert(!is_void<_Tp>::value, |
75 | "can't delete pointer to incomplete type"); |
76 | static_assert(sizeof(_Tp)>0, |
77 | "can't delete pointer to incomplete type"); |
78 | delete __ptr; |
79 | } |
80 | }; |
81 | |
82 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
83 | // DR 740 - omit specialization for array objects with a compile time length |
84 | /// Specialization for arrays, default_delete. |
85 | template<typename _Tp> |
86 | struct default_delete<_Tp[]> |
87 | { |
88 | public: |
89 | /// Default constructor |
90 | constexpr default_delete() noexcept = default; |
91 | |
92 | /** @brief Converting constructor. |
93 | * |
94 | * Allows conversion from a deleter for arrays of another type, such as |
95 | * a const-qualified version of @p _Tp. |
96 | * |
97 | * Conversions from types derived from @c _Tp are not allowed because |
98 | * it is unsafe to @c delete[] an array of derived types through a |
99 | * pointer to the base type. |
100 | */ |
101 | template<typename _Up, typename = typename |
102 | enable_if<is_convertible<_Up(*)[], _Tp(*)[]>::value>::type> |
103 | default_delete(const default_delete<_Up[]>&) noexcept { } |
104 | |
105 | /// Calls @c delete[] @p __ptr |
106 | template<typename _Up> |
107 | typename enable_if<is_convertible<_Up(*)[], _Tp(*)[]>::value>::type |
108 | operator()(_Up* __ptr) const |
109 | { |
110 | static_assert(sizeof(_Tp)>0, |
111 | "can't delete pointer to incomplete type"); |
112 | delete [] __ptr; |
113 | } |
114 | }; |
115 | |
116 | template <typename _Tp, typename _Dp> |
117 | class __uniq_ptr_impl |
118 | { |
119 | template <typename _Up, typename _Ep, typename = void> |
120 | struct _Ptr |
121 | { |
122 | using type = _Up*; |
123 | }; |
124 | |
125 | template <typename _Up, typename _Ep> |
126 | struct |
127 | _Ptr<_Up, _Ep, __void_t<typename remove_reference<_Ep>::type::pointer>> |
128 | { |
129 | using type = typename remove_reference<_Ep>::type::pointer; |
130 | }; |
131 | |
132 | public: |
133 | using _DeleterConstraint = enable_if< |
134 | __and_<__not_<is_pointer<_Dp>>, |
135 | is_default_constructible<_Dp>>::value>; |
136 | |
137 | using pointer = typename _Ptr<_Tp, _Dp>::type; |
138 | |
139 | __uniq_ptr_impl() = default; |
140 | __uniq_ptr_impl(pointer __p) : _M_t() { _M_ptr() = __p; } |
141 | |
142 | template<typename _Del> |
143 | __uniq_ptr_impl(pointer __p, _Del&& __d) |
144 | : _M_t(__p, std::forward<_Del>(__d)) { } |
145 | |
146 | pointer& _M_ptr() { return std::get<0>(_M_t); } |
147 | pointer _M_ptr() const { return std::get<0>(_M_t); } |
148 | _Dp& _M_deleter() { return std::get<1>(_M_t); } |
149 | const _Dp& _M_deleter() const { return std::get<1>(_M_t); } |
150 | |
151 | private: |
152 | tuple<pointer, _Dp> _M_t; |
153 | }; |
154 | |
155 | /// 20.7.1.2 unique_ptr for single objects. |
156 | template <typename _Tp, typename _Dp = default_delete<_Tp>> |
157 | class unique_ptr |
158 | { |
159 | template <class _Up> |
160 | using _DeleterConstraint = |
161 | typename __uniq_ptr_impl<_Tp, _Up>::_DeleterConstraint::type; |
162 | |
163 | __uniq_ptr_impl<_Tp, _Dp> _M_t; |
164 | |
165 | public: |
166 | using pointer = typename __uniq_ptr_impl<_Tp, _Dp>::pointer; |
167 | using element_type = _Tp; |
168 | using deleter_type = _Dp; |
169 | |
170 | // helper template for detecting a safe conversion from another |
171 | // unique_ptr |
172 | template<typename _Up, typename _Ep> |
173 | using __safe_conversion_up = __and_< |
174 | is_convertible<typename unique_ptr<_Up, _Ep>::pointer, pointer>, |
175 | __not_<is_array<_Up>>, |
176 | __or_<__and_<is_reference<deleter_type>, |
177 | is_same<deleter_type, _Ep>>, |
178 | __and_<__not_<is_reference<deleter_type>>, |
179 | is_convertible<_Ep, deleter_type>> |
180 | > |
181 | >; |
182 | |
183 | // Constructors. |
184 | |
185 | /// Default constructor, creates a unique_ptr that owns nothing. |
186 | template <typename _Up = _Dp, |
187 | typename = _DeleterConstraint<_Up>> |
188 | constexpr unique_ptr() noexcept |
189 | : _M_t() |
190 | { } |
191 | |
192 | /** Takes ownership of a pointer. |
193 | * |
194 | * @param __p A pointer to an object of @c element_type |
195 | * |
196 | * The deleter will be value-initialized. |
197 | */ |
198 | template <typename _Up = _Dp, |
199 | typename = _DeleterConstraint<_Up>> |
200 | explicit |
201 | unique_ptr(pointer __p) noexcept |
202 | : _M_t(__p) |
203 | { } |
204 | |
205 | /** Takes ownership of a pointer. |
206 | * |
207 | * @param __p A pointer to an object of @c element_type |
208 | * @param __d A reference to a deleter. |
209 | * |
210 | * The deleter will be initialized with @p __d |
211 | */ |
212 | unique_ptr(pointer __p, |
213 | typename conditional<is_reference<deleter_type>::value, |
214 | deleter_type, const deleter_type&>::type __d) noexcept |
215 | : _M_t(__p, __d) { } |
216 | |
217 | /** Takes ownership of a pointer. |
218 | * |
219 | * @param __p A pointer to an object of @c element_type |
220 | * @param __d An rvalue reference to a deleter. |
221 | * |
222 | * The deleter will be initialized with @p std::move(__d) |
223 | */ |
224 | unique_ptr(pointer __p, |
225 | typename remove_reference<deleter_type>::type&& __d) noexcept |
226 | : _M_t(std::move(__p), std::move(__d)) |
227 | { static_assert(!std::is_reference<deleter_type>::value, |
228 | "rvalue deleter bound to reference"); } |
229 | |
230 | /// Creates a unique_ptr that owns nothing. |
231 | template <typename _Up = _Dp, |
232 | typename = _DeleterConstraint<_Up>> |
233 | constexpr unique_ptr(nullptr_t) noexcept : unique_ptr() { } |
234 | |
235 | // Move constructors. |
236 | |
237 | /// Move constructor. |
238 | unique_ptr(unique_ptr&& __u) noexcept |
239 | : _M_t(__u.release(), std::forward<deleter_type>(__u.get_deleter())) { } |
240 | |
241 | /** @brief Converting constructor from another type |
242 | * |
243 | * Requires that the pointer owned by @p __u is convertible to the |
244 | * type of pointer owned by this object, @p __u does not own an array, |
245 | * and @p __u has a compatible deleter type. |
246 | */ |
247 | template<typename _Up, typename _Ep, typename = _Require< |
248 | __safe_conversion_up<_Up, _Ep>, |
249 | typename conditional<is_reference<_Dp>::value, |
250 | is_same<_Ep, _Dp>, |
251 | is_convertible<_Ep, _Dp>>::type>> |
252 | unique_ptr(unique_ptr<_Up, _Ep>&& __u) noexcept |
253 | : _M_t(__u.release(), std::forward<_Ep>(__u.get_deleter())) |
254 | { } |
255 | |
256 | #if _GLIBCXX_USE_DEPRECATED1 |
257 | /// Converting constructor from @c auto_ptr |
258 | template<typename _Up, typename = _Require< |
259 | is_convertible<_Up*, _Tp*>, is_same<_Dp, default_delete<_Tp>>>> |
260 | unique_ptr(auto_ptr<_Up>&& __u) noexcept; |
261 | #endif |
262 | |
263 | /// Destructor, invokes the deleter if the stored pointer is not null. |
264 | ~unique_ptr() noexcept |
265 | { |
266 | auto& __ptr = _M_t._M_ptr(); |
267 | if (__ptr != nullptr) |
268 | get_deleter()(__ptr); |
269 | __ptr = pointer(); |
270 | } |
271 | |
272 | // Assignment. |
273 | |
274 | /** @brief Move assignment operator. |
275 | * |
276 | * @param __u The object to transfer ownership from. |
277 | * |
278 | * Invokes the deleter first if this object owns a pointer. |
279 | */ |
280 | unique_ptr& |
281 | operator=(unique_ptr&& __u) noexcept |
282 | { |
283 | reset(__u.release()); |
284 | get_deleter() = std::forward<deleter_type>(__u.get_deleter()); |
285 | return *this; |
286 | } |
287 | |
288 | /** @brief Assignment from another type. |
289 | * |
290 | * @param __u The object to transfer ownership from, which owns a |
291 | * convertible pointer to a non-array object. |
292 | * |
293 | * Invokes the deleter first if this object owns a pointer. |
294 | */ |
295 | template<typename _Up, typename _Ep> |
296 | typename enable_if< __and_< |
297 | __safe_conversion_up<_Up, _Ep>, |
298 | is_assignable<deleter_type&, _Ep&&> |
299 | >::value, |
300 | unique_ptr&>::type |
301 | operator=(unique_ptr<_Up, _Ep>&& __u) noexcept |
302 | { |
303 | reset(__u.release()); |
304 | get_deleter() = std::forward<_Ep>(__u.get_deleter()); |
305 | return *this; |
306 | } |
307 | |
308 | /// Reset the %unique_ptr to empty, invoking the deleter if necessary. |
309 | unique_ptr& |
310 | operator=(nullptr_t) noexcept |
311 | { |
312 | reset(); |
313 | return *this; |
314 | } |
315 | |
316 | // Observers. |
317 | |
318 | /// Dereference the stored pointer. |
319 | typename add_lvalue_reference<element_type>::type |
320 | operator*() const |
321 | { |
322 | __glibcxx_assert(get() != pointer()); |
323 | return *get(); |
324 | } |
325 | |
326 | /// Return the stored pointer. |
327 | pointer |
328 | operator->() const noexcept |
329 | { |
330 | _GLIBCXX_DEBUG_PEDASSERT(get() != pointer()); |
331 | return get(); |
332 | } |
333 | |
334 | /// Return the stored pointer. |
335 | pointer |
336 | get() const noexcept |
337 | { return _M_t._M_ptr(); } |
338 | |
339 | /// Return a reference to the stored deleter. |
340 | deleter_type& |
341 | get_deleter() noexcept |
342 | { return _M_t._M_deleter(); } |
343 | |
344 | /// Return a reference to the stored deleter. |
345 | const deleter_type& |
346 | get_deleter() const noexcept |
347 | { return _M_t._M_deleter(); } |
348 | |
349 | /// Return @c true if the stored pointer is not null. |
350 | explicit operator bool() const noexcept |
351 | { return get() == pointer() ? false : true; } |
352 | |
353 | // Modifiers. |
354 | |
355 | /// Release ownership of any stored pointer. |
356 | pointer |
357 | release() noexcept |
358 | { |
359 | pointer __p = get(); |
360 | _M_t._M_ptr() = pointer(); |
361 | return __p; |
362 | } |
363 | |
364 | /** @brief Replace the stored pointer. |
365 | * |
366 | * @param __p The new pointer to store. |
367 | * |
368 | * The deleter will be invoked if a pointer is already owned. |
369 | */ |
370 | void |
371 | reset(pointer __p = pointer()) noexcept |
372 | { |
373 | using std::swap; |
374 | swap(_M_t._M_ptr(), __p); |
375 | if (__p != pointer()) |
376 | get_deleter()(__p); |
377 | } |
378 | |
379 | /// Exchange the pointer and deleter with another object. |
380 | void |
381 | swap(unique_ptr& __u) noexcept |
382 | { |
383 | using std::swap; |
384 | swap(_M_t, __u._M_t); |
385 | } |
386 | |
387 | // Disable copy from lvalue. |
388 | unique_ptr(const unique_ptr&) = delete; |
389 | unique_ptr& operator=(const unique_ptr&) = delete; |
390 | }; |
391 | |
392 | /// 20.7.1.3 unique_ptr for array objects with a runtime length |
393 | // [unique.ptr.runtime] |
394 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
395 | // DR 740 - omit specialization for array objects with a compile time length |
396 | template<typename _Tp, typename _Dp> |
397 | class unique_ptr<_Tp[], _Dp> |
398 | { |
399 | template <typename _Up> |
400 | using _DeleterConstraint = |
401 | typename __uniq_ptr_impl<_Tp, _Up>::_DeleterConstraint::type; |
402 | |
403 | __uniq_ptr_impl<_Tp, _Dp> _M_t; |
404 | |
405 | template<typename _Up> |
406 | using __remove_cv = typename remove_cv<_Up>::type; |
407 | |
408 | // like is_base_of<_Tp, _Up> but false if unqualified types are the same |
409 | template<typename _Up> |
410 | using __is_derived_Tp |
411 | = __and_< is_base_of<_Tp, _Up>, |
412 | __not_<is_same<__remove_cv<_Tp>, __remove_cv<_Up>>> >; |
413 | |
414 | public: |
415 | using pointer = typename __uniq_ptr_impl<_Tp, _Dp>::pointer; |
416 | using element_type = _Tp; |
417 | using deleter_type = _Dp; |
418 | |
419 | // helper template for detecting a safe conversion from another |
420 | // unique_ptr |
421 | template<typename _Up, typename _Ep, |
422 | typename _Up_up = unique_ptr<_Up, _Ep>, |
423 | typename _Up_element_type = typename _Up_up::element_type> |
424 | using __safe_conversion_up = __and_< |
425 | is_array<_Up>, |
426 | is_same<pointer, element_type*>, |
427 | is_same<typename _Up_up::pointer, _Up_element_type*>, |
428 | is_convertible<_Up_element_type(*)[], element_type(*)[]>, |
429 | __or_<__and_<is_reference<deleter_type>, is_same<deleter_type, _Ep>>, |
430 | __and_<__not_<is_reference<deleter_type>>, |
431 | is_convertible<_Ep, deleter_type>>> |
432 | >; |
433 | |
434 | // helper template for detecting a safe conversion from a raw pointer |
435 | template<typename _Up> |
436 | using __safe_conversion_raw = __and_< |
437 | __or_<__or_<is_same<_Up, pointer>, |
438 | is_same<_Up, nullptr_t>>, |
439 | __and_<is_pointer<_Up>, |
440 | is_same<pointer, element_type*>, |
441 | is_convertible< |
442 | typename remove_pointer<_Up>::type(*)[], |
443 | element_type(*)[]> |
444 | > |
445 | > |
446 | >; |
447 | |
448 | // Constructors. |
449 | |
450 | /// Default constructor, creates a unique_ptr that owns nothing. |
451 | template <typename _Up = _Dp, |
452 | typename = _DeleterConstraint<_Up>> |
453 | constexpr unique_ptr() noexcept |
454 | : _M_t() |
455 | { } |
456 | |
457 | /** Takes ownership of a pointer. |
458 | * |
459 | * @param __p A pointer to an array of a type safely convertible |
460 | * to an array of @c element_type |
461 | * |
462 | * The deleter will be value-initialized. |
463 | */ |
464 | template<typename _Up, |
465 | typename _Vp = _Dp, |
466 | typename = _DeleterConstraint<_Vp>, |
467 | typename = typename enable_if< |
468 | __safe_conversion_raw<_Up>::value, bool>::type> |
469 | explicit |
470 | unique_ptr(_Up __p) noexcept |
471 | : _M_t(__p) |
472 | { } |
473 | |
474 | /** Takes ownership of a pointer. |
475 | * |
476 | * @param __p A pointer to an array of a type safely convertible |
477 | * to an array of @c element_type |
478 | * @param __d A reference to a deleter. |
479 | * |
480 | * The deleter will be initialized with @p __d |
481 | */ |
482 | template<typename _Up, |
483 | typename = typename enable_if< |
484 | __safe_conversion_raw<_Up>::value, bool>::type> |
485 | unique_ptr(_Up __p, |
486 | typename conditional<is_reference<deleter_type>::value, |
487 | deleter_type, const deleter_type&>::type __d) noexcept |
488 | : _M_t(__p, __d) { } |
489 | |
490 | /** Takes ownership of a pointer. |
491 | * |
492 | * @param __p A pointer to an array of a type safely convertible |
493 | * to an array of @c element_type |
494 | * @param __d A reference to a deleter. |
495 | * |
496 | * The deleter will be initialized with @p std::move(__d) |
497 | */ |
498 | template<typename _Up, |
499 | typename = typename enable_if< |
500 | __safe_conversion_raw<_Up>::value, bool>::type> |
501 | unique_ptr(_Up __p, typename |
502 | remove_reference<deleter_type>::type&& __d) noexcept |
503 | : _M_t(std::move(__p), std::move(__d)) |
504 | { static_assert(!is_reference<deleter_type>::value, |
505 | "rvalue deleter bound to reference"); } |
506 | |
507 | /// Move constructor. |
508 | unique_ptr(unique_ptr&& __u) noexcept |
509 | : _M_t(__u.release(), std::forward<deleter_type>(__u.get_deleter())) { } |
510 | |
511 | /// Creates a unique_ptr that owns nothing. |
512 | template <typename _Up = _Dp, |
513 | typename = _DeleterConstraint<_Up>> |
514 | constexpr unique_ptr(nullptr_t) noexcept : unique_ptr() { } |
515 | |
516 | template<typename _Up, typename _Ep, |
517 | typename = _Require<__safe_conversion_up<_Up, _Ep>>> |
518 | unique_ptr(unique_ptr<_Up, _Ep>&& __u) noexcept |
519 | : _M_t(__u.release(), std::forward<_Ep>(__u.get_deleter())) |
520 | { } |
521 | |
522 | /// Destructor, invokes the deleter if the stored pointer is not null. |
523 | ~unique_ptr() |
524 | { |
525 | auto& __ptr = _M_t._M_ptr(); |
526 | if (__ptr != nullptr) |
527 | get_deleter()(__ptr); |
528 | __ptr = pointer(); |
529 | } |
530 | |
531 | // Assignment. |
532 | |
533 | /** @brief Move assignment operator. |
534 | * |
535 | * @param __u The object to transfer ownership from. |
536 | * |
537 | * Invokes the deleter first if this object owns a pointer. |
538 | */ |
539 | unique_ptr& |
540 | operator=(unique_ptr&& __u) noexcept |
541 | { |
542 | reset(__u.release()); |
543 | get_deleter() = std::forward<deleter_type>(__u.get_deleter()); |
544 | return *this; |
545 | } |
546 | |
547 | /** @brief Assignment from another type. |
548 | * |
549 | * @param __u The object to transfer ownership from, which owns a |
550 | * convertible pointer to an array object. |
551 | * |
552 | * Invokes the deleter first if this object owns a pointer. |
553 | */ |
554 | template<typename _Up, typename _Ep> |
555 | typename |
556 | enable_if<__and_<__safe_conversion_up<_Up, _Ep>, |
557 | is_assignable<deleter_type&, _Ep&&> |
558 | >::value, |
559 | unique_ptr&>::type |
560 | operator=(unique_ptr<_Up, _Ep>&& __u) noexcept |
561 | { |
562 | reset(__u.release()); |
563 | get_deleter() = std::forward<_Ep>(__u.get_deleter()); |
564 | return *this; |
565 | } |
566 | |
567 | /// Reset the %unique_ptr to empty, invoking the deleter if necessary. |
568 | unique_ptr& |
569 | operator=(nullptr_t) noexcept |
570 | { |
571 | reset(); |
572 | return *this; |
573 | } |
574 | |
575 | // Observers. |
576 | |
577 | /// Access an element of owned array. |
578 | typename std::add_lvalue_reference<element_type>::type |
579 | operator[](size_t __i) const |
580 | { |
581 | __glibcxx_assert(get() != pointer()); |
582 | return get()[__i]; |
583 | } |
584 | |
585 | /// Return the stored pointer. |
586 | pointer |
587 | get() const noexcept |
588 | { return _M_t._M_ptr(); } |
589 | |
590 | /// Return a reference to the stored deleter. |
591 | deleter_type& |
592 | get_deleter() noexcept |
593 | { return _M_t._M_deleter(); } |
594 | |
595 | /// Return a reference to the stored deleter. |
596 | const deleter_type& |
597 | get_deleter() const noexcept |
598 | { return _M_t._M_deleter(); } |
599 | |
600 | /// Return @c true if the stored pointer is not null. |
601 | explicit operator bool() const noexcept |
602 | { return get() == pointer() ? false : true; } |
603 | |
604 | // Modifiers. |
605 | |
606 | /// Release ownership of any stored pointer. |
607 | pointer |
608 | release() noexcept |
609 | { |
610 | pointer __p = get(); |
611 | _M_t._M_ptr() = pointer(); |
612 | return __p; |
613 | } |
614 | |
615 | /** @brief Replace the stored pointer. |
616 | * |
617 | * @param __p The new pointer to store. |
618 | * |
619 | * The deleter will be invoked if a pointer is already owned. |
620 | */ |
621 | template <typename _Up, |
622 | typename = _Require< |
623 | __or_<is_same<_Up, pointer>, |
624 | __and_<is_same<pointer, element_type*>, |
625 | is_pointer<_Up>, |
626 | is_convertible< |
627 | typename remove_pointer<_Up>::type(*)[], |
628 | element_type(*)[] |
629 | > |
630 | > |
631 | > |
632 | >> |
633 | void |
634 | reset(_Up __p) noexcept |
635 | { |
636 | pointer __ptr = __p; |
637 | using std::swap; |
638 | swap(_M_t._M_ptr(), __ptr); |
639 | if (__ptr != nullptr) |
640 | get_deleter()(__ptr); |
641 | } |
642 | |
643 | void reset(nullptr_t = nullptr) noexcept |
644 | { |
645 | reset(pointer()); |
646 | } |
647 | |
648 | /// Exchange the pointer and deleter with another object. |
649 | void |
650 | swap(unique_ptr& __u) noexcept |
651 | { |
652 | using std::swap; |
653 | swap(_M_t, __u._M_t); |
654 | } |
655 | |
656 | // Disable copy from lvalue. |
657 | unique_ptr(const unique_ptr&) = delete; |
658 | unique_ptr& operator=(const unique_ptr&) = delete; |
659 | }; |
660 | |
661 | template<typename _Tp, typename _Dp> |
662 | inline |
663 | #if __cplusplus201103L > 201402L || !defined(__STRICT_ANSI__1) // c++1z or gnu++11 |
664 | // Constrained free swap overload, see p0185r1 |
665 | typename enable_if<__is_swappable<_Dp>::value>::type |
666 | #else |
667 | void |
668 | #endif |
669 | swap(unique_ptr<_Tp, _Dp>& __x, |
670 | unique_ptr<_Tp, _Dp>& __y) noexcept |
671 | { __x.swap(__y); } |
672 | |
673 | #if __cplusplus201103L > 201402L || !defined(__STRICT_ANSI__1) // c++1z or gnu++11 |
674 | template<typename _Tp, typename _Dp> |
675 | typename enable_if<!__is_swappable<_Dp>::value>::type |
676 | swap(unique_ptr<_Tp, _Dp>&, |
677 | unique_ptr<_Tp, _Dp>&) = delete; |
678 | #endif |
679 | |
680 | template<typename _Tp, typename _Dp, |
681 | typename _Up, typename _Ep> |
682 | inline bool |
683 | operator==(const unique_ptr<_Tp, _Dp>& __x, |
684 | const unique_ptr<_Up, _Ep>& __y) |
685 | { return __x.get() == __y.get(); } |
686 | |
687 | template<typename _Tp, typename _Dp> |
688 | inline bool |
689 | operator==(const unique_ptr<_Tp, _Dp>& __x, nullptr_t) noexcept |
690 | { return !__x; } |
691 | |
692 | template<typename _Tp, typename _Dp> |
693 | inline bool |
694 | operator==(nullptr_t, const unique_ptr<_Tp, _Dp>& __x) noexcept |
695 | { return !__x; } |
696 | |
697 | template<typename _Tp, typename _Dp, |
698 | typename _Up, typename _Ep> |
699 | inline bool |
700 | operator!=(const unique_ptr<_Tp, _Dp>& __x, |
701 | const unique_ptr<_Up, _Ep>& __y) |
702 | { return __x.get() != __y.get(); } |
703 | |
704 | template<typename _Tp, typename _Dp> |
705 | inline bool |
706 | operator!=(const unique_ptr<_Tp, _Dp>& __x, nullptr_t) noexcept |
707 | { return (bool)__x; } |
708 | |
709 | template<typename _Tp, typename _Dp> |
710 | inline bool |
711 | operator!=(nullptr_t, const unique_ptr<_Tp, _Dp>& __x) noexcept |
712 | { return (bool)__x; } |
713 | |
714 | template<typename _Tp, typename _Dp, |
715 | typename _Up, typename _Ep> |
716 | inline bool |
717 | operator<(const unique_ptr<_Tp, _Dp>& __x, |
718 | const unique_ptr<_Up, _Ep>& __y) |
719 | { |
720 | typedef typename |
721 | std::common_type<typename unique_ptr<_Tp, _Dp>::pointer, |
722 | typename unique_ptr<_Up, _Ep>::pointer>::type _CT; |
723 | return std::less<_CT>()(__x.get(), __y.get()); |
724 | } |
725 | |
726 | template<typename _Tp, typename _Dp> |
727 | inline bool |
728 | operator<(const unique_ptr<_Tp, _Dp>& __x, nullptr_t) |
729 | { return std::less<typename unique_ptr<_Tp, _Dp>::pointer>()(__x.get(), |
730 | nullptr); } |
731 | |
732 | template<typename _Tp, typename _Dp> |
733 | inline bool |
734 | operator<(nullptr_t, const unique_ptr<_Tp, _Dp>& __x) |
735 | { return std::less<typename unique_ptr<_Tp, _Dp>::pointer>()(nullptr, |
736 | __x.get()); } |
737 | |
738 | template<typename _Tp, typename _Dp, |
739 | typename _Up, typename _Ep> |
740 | inline bool |
741 | operator<=(const unique_ptr<_Tp, _Dp>& __x, |
742 | const unique_ptr<_Up, _Ep>& __y) |
743 | { return !(__y < __x); } |
744 | |
745 | template<typename _Tp, typename _Dp> |
746 | inline bool |
747 | operator<=(const unique_ptr<_Tp, _Dp>& __x, nullptr_t) |
748 | { return !(nullptr < __x); } |
749 | |
750 | template<typename _Tp, typename _Dp> |
751 | inline bool |
752 | operator<=(nullptr_t, const unique_ptr<_Tp, _Dp>& __x) |
753 | { return !(__x < nullptr); } |
754 | |
755 | template<typename _Tp, typename _Dp, |
756 | typename _Up, typename _Ep> |
757 | inline bool |
758 | operator>(const unique_ptr<_Tp, _Dp>& __x, |
759 | const unique_ptr<_Up, _Ep>& __y) |
760 | { return (__y < __x); } |
761 | |
762 | template<typename _Tp, typename _Dp> |
763 | inline bool |
764 | operator>(const unique_ptr<_Tp, _Dp>& __x, nullptr_t) |
765 | { return std::less<typename unique_ptr<_Tp, _Dp>::pointer>()(nullptr, |
766 | __x.get()); } |
767 | |
768 | template<typename _Tp, typename _Dp> |
769 | inline bool |
770 | operator>(nullptr_t, const unique_ptr<_Tp, _Dp>& __x) |
771 | { return std::less<typename unique_ptr<_Tp, _Dp>::pointer>()(__x.get(), |
772 | nullptr); } |
773 | |
774 | template<typename _Tp, typename _Dp, |
775 | typename _Up, typename _Ep> |
776 | inline bool |
777 | operator>=(const unique_ptr<_Tp, _Dp>& __x, |
778 | const unique_ptr<_Up, _Ep>& __y) |
779 | { return !(__x < __y); } |
780 | |
781 | template<typename _Tp, typename _Dp> |
782 | inline bool |
783 | operator>=(const unique_ptr<_Tp, _Dp>& __x, nullptr_t) |
784 | { return !(__x < nullptr); } |
785 | |
786 | template<typename _Tp, typename _Dp> |
787 | inline bool |
788 | operator>=(nullptr_t, const unique_ptr<_Tp, _Dp>& __x) |
789 | { return !(nullptr < __x); } |
790 | |
791 | /// std::hash specialization for unique_ptr. |
792 | template<typename _Tp, typename _Dp> |
793 | struct hash<unique_ptr<_Tp, _Dp>> |
794 | : public __hash_base<size_t, unique_ptr<_Tp, _Dp>>, |
795 | private __poison_hash<typename unique_ptr<_Tp, _Dp>::pointer> |
796 | { |
797 | size_t |
798 | operator()(const unique_ptr<_Tp, _Dp>& __u) const noexcept |
799 | { |
800 | typedef unique_ptr<_Tp, _Dp> _UP; |
801 | return std::hash<typename _UP::pointer>()(__u.get()); |
802 | } |
803 | }; |
804 | |
805 | #if __cplusplus201103L > 201103L |
806 | |
807 | #define __cpp_lib_make_unique 201304 |
808 | |
809 | template<typename _Tp> |
810 | struct _MakeUniq |
811 | { typedef unique_ptr<_Tp> __single_object; }; |
812 | |
813 | template<typename _Tp> |
814 | struct _MakeUniq<_Tp[]> |
815 | { typedef unique_ptr<_Tp[]> __array; }; |
816 | |
817 | template<typename _Tp, size_t _Bound> |
818 | struct _MakeUniq<_Tp[_Bound]> |
819 | { struct __invalid_type { }; }; |
820 | |
821 | /// std::make_unique for single objects |
822 | template<typename _Tp, typename... _Args> |
823 | inline typename _MakeUniq<_Tp>::__single_object |
824 | make_unique(_Args&&... __args) |
825 | { return unique_ptr<_Tp>(new _Tp(std::forward<_Args>(__args)...)); } |
826 | |
827 | /// std::make_unique for arrays of unknown bound |
828 | template<typename _Tp> |
829 | inline typename _MakeUniq<_Tp>::__array |
830 | make_unique(size_t __num) |
831 | { return unique_ptr<_Tp>(new remove_extent_t<_Tp>[__num]()); } |
832 | |
833 | /// Disable std::make_unique for arrays of known bound |
834 | template<typename _Tp, typename... _Args> |
835 | inline typename _MakeUniq<_Tp>::__invalid_type |
836 | make_unique(_Args&&...) = delete; |
837 | #endif |
838 | |
839 | // @} group pointer_abstractions |
840 | |
841 | _GLIBCXX_END_NAMESPACE_VERSION |
842 | } // namespace |
843 | |
844 | #endif /* _UNIQUE_PTR_H */ |