Bug Summary

File:src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Bitcode/Reader/BitcodeReader.cpp
Warning:line 2670, column 50
Called C++ object pointer is null

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple amd64-unknown-openbsd7.0 -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name BitcodeReader.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 1 -fhalf-no-semantic-interposition -mframe-pointer=all -relaxed-aliasing -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fcoverage-compilation-dir=/usr/src/gnu/usr.bin/clang/libLLVM/obj -resource-dir /usr/local/lib/clang/13.0.0 -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Analysis -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ASMParser -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/BinaryFormat -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Bitcode -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Bitcode -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Bitstream -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /include/llvm/CodeGen -I /include/llvm/CodeGen/PBQP -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/IR -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/IR -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms/Coroutines -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ProfileData/Coverage -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/DebugInfo/CodeView -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/DebugInfo/DWARF -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/DebugInfo -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/DebugInfo/MSF -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/DebugInfo/PDB -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Demangle -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ExecutionEngine -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ExecutionEngine/JITLink -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ExecutionEngine/Orc -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Frontend -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Frontend/OpenACC -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Frontend -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Frontend/OpenMP -I /include/llvm/CodeGen/GlobalISel -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/IRReader -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms/InstCombine -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/Transforms/InstCombine -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/LTO -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Linker -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/MC -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/MC/MCParser -I /include/llvm/CodeGen/MIRParser -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Object -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Option -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Passes -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ProfileData -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms/Scalar -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ADT -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Support -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/DebugInfo/Symbolize -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Target -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms/Utils -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms/Vectorize -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms/IPO -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include -I /usr/src/gnu/usr.bin/clang/libLLVM/../include -I /usr/src/gnu/usr.bin/clang/libLLVM/obj -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include -D NDEBUG -D __STDC_LIMIT_MACROS -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D LLVM_PREFIX="/usr" -D PIC -internal-isystem /usr/include/c++/v1 -internal-isystem /usr/local/lib/clang/13.0.0/include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-comment -std=c++14 -fdeprecated-macro -fdebug-compilation-dir=/usr/src/gnu/usr.bin/clang/libLLVM/obj -ferror-limit 19 -fvisibility-inlines-hidden -fwrapv -D_RET_PROTECTOR -ret-protector -fno-rtti -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-valloc -fno-builtin-free -fno-builtin-strdup -fno-builtin-strndup -analyzer-output=html -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /home/ben/Projects/vmm/scan-build/2022-01-12-194120-40624-1 -x c++ /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Bitcode/Reader/BitcodeReader.cpp

/usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Bitcode/Reader/BitcodeReader.cpp

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

/usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Support/Error.h

1//===- llvm/Support/Error.h - Recoverable error handling --------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defines an API used to report recoverable errors.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_SUPPORT_ERROR_H
14#define LLVM_SUPPORT_ERROR_H
15
16#include "llvm-c/Error.h"
17#include "llvm/ADT/STLExtras.h"
18#include "llvm/ADT/SmallVector.h"
19#include "llvm/ADT/StringExtras.h"
20#include "llvm/ADT/Twine.h"
21#include "llvm/Config/abi-breaking.h"
22#include "llvm/Support/AlignOf.h"
23#include "llvm/Support/Compiler.h"
24#include "llvm/Support/Debug.h"
25#include "llvm/Support/ErrorHandling.h"
26#include "llvm/Support/ErrorOr.h"
27#include "llvm/Support/Format.h"
28#include "llvm/Support/raw_ostream.h"
29#include <algorithm>
30#include <cassert>
31#include <cstdint>
32#include <cstdlib>
33#include <functional>
34#include <memory>
35#include <new>
36#include <string>
37#include <system_error>
38#include <type_traits>
39#include <utility>
40#include <vector>
41
42namespace llvm {
43
44class ErrorSuccess;
45
46/// Base class for error info classes. Do not extend this directly: Extend
47/// the ErrorInfo template subclass instead.
48class ErrorInfoBase {
49public:
50 virtual ~ErrorInfoBase() = default;
51
52 /// Print an error message to an output stream.
53 virtual void log(raw_ostream &OS) const = 0;
54
55 /// Return the error message as a string.
56 virtual std::string message() const {
57 std::string Msg;
58 raw_string_ostream OS(Msg);
59 log(OS);
60 return OS.str();
61 }
62
63 /// Convert this error to a std::error_code.
64 ///
65 /// This is a temporary crutch to enable interaction with code still
66 /// using std::error_code. It will be removed in the future.
67 virtual std::error_code convertToErrorCode() const = 0;
68
69 // Returns the class ID for this type.
70 static const void *classID() { return &ID; }
71
72 // Returns the class ID for the dynamic type of this ErrorInfoBase instance.
73 virtual const void *dynamicClassID() const = 0;
74
75 // Check whether this instance is a subclass of the class identified by
76 // ClassID.
77 virtual bool isA(const void *const ClassID) const {
78 return ClassID == classID();
79 }
80
81 // Check whether this instance is a subclass of ErrorInfoT.
82 template <typename ErrorInfoT> bool isA() const {
83 return isA(ErrorInfoT::classID());
84 }
85
86private:
87 virtual void anchor();
88
89 static char ID;
90};
91
92/// Lightweight error class with error context and mandatory checking.
93///
94/// Instances of this class wrap a ErrorInfoBase pointer. Failure states
95/// are represented by setting the pointer to a ErrorInfoBase subclass
96/// instance containing information describing the failure. Success is
97/// represented by a null pointer value.
98///
99/// Instances of Error also contains a 'Checked' flag, which must be set
100/// before the destructor is called, otherwise the destructor will trigger a
101/// runtime error. This enforces at runtime the requirement that all Error
102/// instances be checked or returned to the caller.
103///
104/// There are two ways to set the checked flag, depending on what state the
105/// Error instance is in. For Error instances indicating success, it
106/// is sufficient to invoke the boolean conversion operator. E.g.:
107///
108/// @code{.cpp}
109/// Error foo(<...>);
110///
111/// if (auto E = foo(<...>))
112/// return E; // <- Return E if it is in the error state.
113/// // We have verified that E was in the success state. It can now be safely
114/// // destroyed.
115/// @endcode
116///
117/// A success value *can not* be dropped. For example, just calling 'foo(<...>)'
118/// without testing the return value will raise a runtime error, even if foo
119/// returns success.
120///
121/// For Error instances representing failure, you must use either the
122/// handleErrors or handleAllErrors function with a typed handler. E.g.:
123///
124/// @code{.cpp}
125/// class MyErrorInfo : public ErrorInfo<MyErrorInfo> {
126/// // Custom error info.
127/// };
128///
129/// Error foo(<...>) { return make_error<MyErrorInfo>(...); }
130///
131/// auto E = foo(<...>); // <- foo returns failure with MyErrorInfo.
132/// auto NewE =
133/// handleErrors(E,
134/// [](const MyErrorInfo &M) {
135/// // Deal with the error.
136/// },
137/// [](std::unique_ptr<OtherError> M) -> Error {
138/// if (canHandle(*M)) {
139/// // handle error.
140/// return Error::success();
141/// }
142/// // Couldn't handle this error instance. Pass it up the stack.
143/// return Error(std::move(M));
144/// );
145/// // Note - we must check or return NewE in case any of the handlers
146/// // returned a new error.
147/// @endcode
148///
149/// The handleAllErrors function is identical to handleErrors, except
150/// that it has a void return type, and requires all errors to be handled and
151/// no new errors be returned. It prevents errors (assuming they can all be
152/// handled) from having to be bubbled all the way to the top-level.
153///
154/// *All* Error instances must be checked before destruction, even if
155/// they're moved-assigned or constructed from Success values that have already
156/// been checked. This enforces checking through all levels of the call stack.
157class LLVM_NODISCARD[[clang::warn_unused_result]] Error {
158 // ErrorList needs to be able to yank ErrorInfoBase pointers out of Errors
159 // to add to the error list. It can't rely on handleErrors for this, since
160 // handleErrors does not support ErrorList handlers.
161 friend class ErrorList;
162
163 // handleErrors needs to be able to set the Checked flag.
164 template <typename... HandlerTs>
165 friend Error handleErrors(Error E, HandlerTs &&... Handlers);
166
167 // Expected<T> needs to be able to steal the payload when constructed from an
168 // error.
169 template <typename T> friend class Expected;
170
171 // wrap needs to be able to steal the payload.
172 friend LLVMErrorRef wrap(Error);
173
174protected:
175 /// Create a success value. Prefer using 'Error::success()' for readability
176 Error() {
177 setPtr(nullptr);
178 setChecked(false);
179 }
180
181public:
182 /// Create a success value.
183 static ErrorSuccess success();
184
185 // Errors are not copy-constructable.
186 Error(const Error &Other) = delete;
187
188 /// Move-construct an error value. The newly constructed error is considered
189 /// unchecked, even if the source error had been checked. The original error
190 /// becomes a checked Success value, regardless of its original state.
191 Error(Error &&Other) {
192 setChecked(true);
193 *this = std::move(Other);
194 }
195
196 /// Create an error value. Prefer using the 'make_error' function, but
197 /// this constructor can be useful when "re-throwing" errors from handlers.
198 Error(std::unique_ptr<ErrorInfoBase> Payload) {
199 setPtr(Payload.release());
200 setChecked(false);
201 }
202
203 // Errors are not copy-assignable.
204 Error &operator=(const Error &Other) = delete;
205
206 /// Move-assign an error value. The current error must represent success, you
207 /// you cannot overwrite an unhandled error. The current error is then
208 /// considered unchecked. The source error becomes a checked success value,
209 /// regardless of its original state.
210 Error &operator=(Error &&Other) {
211 // Don't allow overwriting of unchecked values.
212 assertIsChecked();
213 setPtr(Other.getPtr());
214
215 // This Error is unchecked, even if the source error was checked.
216 setChecked(false);
217
218 // Null out Other's payload and set its checked bit.
219 Other.setPtr(nullptr);
220 Other.setChecked(true);
221
222 return *this;
223 }
224
225 /// Destroy a Error. Fails with a call to abort() if the error is
226 /// unchecked.
227 ~Error() {
228 assertIsChecked();
229 delete getPtr();
230 }
231
232 /// Bool conversion. Returns true if this Error is in a failure state,
233 /// and false if it is in an accept state. If the error is in a Success state
234 /// it will be considered checked.
235 explicit operator bool() {
236 setChecked(getPtr() == nullptr);
2
Assuming the condition is true
237 return getPtr() != nullptr;
3
Returning zero, which participates in a condition later
238 }
239
240 /// Check whether one error is a subclass of another.
241 template <typename ErrT> bool isA() const {
242 return getPtr() && getPtr()->isA(ErrT::classID());
243 }
244
245 /// Returns the dynamic class id of this error, or null if this is a success
246 /// value.
247 const void* dynamicClassID() const {
248 if (!getPtr())
249 return nullptr;
250 return getPtr()->dynamicClassID();
251 }
252
253private:
254#if LLVM_ENABLE_ABI_BREAKING_CHECKS0
255 // assertIsChecked() happens very frequently, but under normal circumstances
256 // is supposed to be a no-op. So we want it to be inlined, but having a bunch
257 // of debug prints can cause the function to be too large for inlining. So
258 // it's important that we define this function out of line so that it can't be
259 // inlined.
260 LLVM_ATTRIBUTE_NORETURN__attribute__((noreturn))
261 void fatalUncheckedError() const;
262#endif
263
264 void assertIsChecked() {
265#if LLVM_ENABLE_ABI_BREAKING_CHECKS0
266 if (LLVM_UNLIKELY(!getChecked() || getPtr())__builtin_expect((bool)(!getChecked() || getPtr()), false))
267 fatalUncheckedError();
268#endif
269 }
270
271 ErrorInfoBase *getPtr() const {
272#if LLVM_ENABLE_ABI_BREAKING_CHECKS0
273 return reinterpret_cast<ErrorInfoBase*>(
274 reinterpret_cast<uintptr_t>(Payload) &
275 ~static_cast<uintptr_t>(0x1));
276#else
277 return Payload;
278#endif
279 }
280
281 void setPtr(ErrorInfoBase *EI) {
282#if LLVM_ENABLE_ABI_BREAKING_CHECKS0
283 Payload = reinterpret_cast<ErrorInfoBase*>(
284 (reinterpret_cast<uintptr_t>(EI) &
285 ~static_cast<uintptr_t>(0x1)) |
286 (reinterpret_cast<uintptr_t>(Payload) & 0x1));
287#else
288 Payload = EI;
289#endif
290 }
291
292 bool getChecked() const {
293#if LLVM_ENABLE_ABI_BREAKING_CHECKS0
294 return (reinterpret_cast<uintptr_t>(Payload) & 0x1) == 0;
295#else
296 return true;
297#endif
298 }
299
300 void setChecked(bool V) {
301#if LLVM_ENABLE_ABI_BREAKING_CHECKS0
302 Payload = reinterpret_cast<ErrorInfoBase*>(
303 (reinterpret_cast<uintptr_t>(Payload) &
304 ~static_cast<uintptr_t>(0x1)) |
305 (V ? 0 : 1));
306#endif
307 }
308
309 std::unique_ptr<ErrorInfoBase> takePayload() {
310 std::unique_ptr<ErrorInfoBase> Tmp(getPtr());
311 setPtr(nullptr);
312 setChecked(true);
313 return Tmp;
314 }
315
316 friend raw_ostream &operator<<(raw_ostream &OS, const Error &E) {
317 if (auto P = E.getPtr())
318 P->log(OS);
319 else
320 OS << "success";
321 return OS;
322 }
323
324 ErrorInfoBase *Payload = nullptr;
325};
326
327/// Subclass of Error for the sole purpose of identifying the success path in
328/// the type system. This allows to catch invalid conversion to Expected<T> at
329/// compile time.
330class ErrorSuccess final : public Error {};
331
332inline ErrorSuccess Error::success() { return ErrorSuccess(); }
333
334/// Make a Error instance representing failure using the given error info
335/// type.
336template <typename ErrT, typename... ArgTs> Error make_error(ArgTs &&... Args) {
337 return Error(std::make_unique<ErrT>(std::forward<ArgTs>(Args)...));
338}
339
340/// Base class for user error types. Users should declare their error types
341/// like:
342///
343/// class MyError : public ErrorInfo<MyError> {
344/// ....
345/// };
346///
347/// This class provides an implementation of the ErrorInfoBase::kind
348/// method, which is used by the Error RTTI system.
349template <typename ThisErrT, typename ParentErrT = ErrorInfoBase>
350class ErrorInfo : public ParentErrT {
351public:
352 using ParentErrT::ParentErrT; // inherit constructors
353
354 static const void *classID() { return &ThisErrT::ID; }
355
356 const void *dynamicClassID() const override { return &ThisErrT::ID; }
357
358 bool isA(const void *const ClassID) const override {
359 return ClassID == classID() || ParentErrT::isA(ClassID);
360 }
361};
362
363/// Special ErrorInfo subclass representing a list of ErrorInfos.
364/// Instances of this class are constructed by joinError.
365class ErrorList final : public ErrorInfo<ErrorList> {
366 // handleErrors needs to be able to iterate the payload list of an
367 // ErrorList.
368 template <typename... HandlerTs>
369 friend Error handleErrors(Error E, HandlerTs &&... Handlers);
370
371 // joinErrors is implemented in terms of join.
372 friend Error joinErrors(Error, Error);
373
374public:
375 void log(raw_ostream &OS) const override {
376 OS << "Multiple errors:\n";
377 for (auto &ErrPayload : Payloads) {
378 ErrPayload->log(OS);
379 OS << "\n";
380 }
381 }
382
383 std::error_code convertToErrorCode() const override;
384
385 // Used by ErrorInfo::classID.
386 static char ID;
387
388private:
389 ErrorList(std::unique_ptr<ErrorInfoBase> Payload1,
390 std::unique_ptr<ErrorInfoBase> Payload2) {
391 assert(!Payload1->isA<ErrorList>() && !Payload2->isA<ErrorList>() &&((void)0)
392 "ErrorList constructor payloads should be singleton errors")((void)0);
393 Payloads.push_back(std::move(Payload1));
394 Payloads.push_back(std::move(Payload2));
395 }
396
397 static Error join(Error E1, Error E2) {
398 if (!E1)
399 return E2;
400 if (!E2)
401 return E1;
402 if (E1.isA<ErrorList>()) {
403 auto &E1List = static_cast<ErrorList &>(*E1.getPtr());
404 if (E2.isA<ErrorList>()) {
405 auto E2Payload = E2.takePayload();
406 auto &E2List = static_cast<ErrorList &>(*E2Payload);
407 for (auto &Payload : E2List.Payloads)
408 E1List.Payloads.push_back(std::move(Payload));
409 } else
410 E1List.Payloads.push_back(E2.takePayload());
411
412 return E1;
413 }
414 if (E2.isA<ErrorList>()) {
415 auto &E2List = static_cast<ErrorList &>(*E2.getPtr());
416 E2List.Payloads.insert(E2List.Payloads.begin(), E1.takePayload());
417 return E2;
418 }
419 return Error(std::unique_ptr<ErrorList>(
420 new ErrorList(E1.takePayload(), E2.takePayload())));
421 }
422
423 std::vector<std::unique_ptr<ErrorInfoBase>> Payloads;
424};
425
426/// Concatenate errors. The resulting Error is unchecked, and contains the
427/// ErrorInfo(s), if any, contained in E1, followed by the
428/// ErrorInfo(s), if any, contained in E2.
429inline Error joinErrors(Error E1, Error E2) {
430 return ErrorList::join(std::move(E1), std::move(E2));
431}
432
433/// Tagged union holding either a T or a Error.
434///
435/// This class parallels ErrorOr, but replaces error_code with Error. Since
436/// Error cannot be copied, this class replaces getError() with
437/// takeError(). It also adds an bool errorIsA<ErrT>() method for testing the
438/// error class type.
439///
440/// Example usage of 'Expected<T>' as a function return type:
441///
442/// @code{.cpp}
443/// Expected<int> myDivide(int A, int B) {
444/// if (B == 0) {
445/// // return an Error
446/// return createStringError(inconvertibleErrorCode(),
447/// "B must not be zero!");
448/// }
449/// // return an integer
450/// return A / B;
451/// }
452/// @endcode
453///
454/// Checking the results of to a function returning 'Expected<T>':
455/// @code{.cpp}
456/// if (auto E = Result.takeError()) {
457/// // We must consume the error. Typically one of:
458/// // - return the error to our caller
459/// // - toString(), when logging
460/// // - consumeError(), to silently swallow the error
461/// // - handleErrors(), to distinguish error types
462/// errs() << "Problem with division " << toString(std::move(E)) << "\n";
463/// return;
464/// }
465/// // use the result
466/// outs() << "The answer is " << *Result << "\n";
467/// @endcode
468///
469/// For unit-testing a function returning an 'Expceted<T>', see the
470/// 'EXPECT_THAT_EXPECTED' macros in llvm/Testing/Support/Error.h
471
472template <class T> class LLVM_NODISCARD[[clang::warn_unused_result]] Expected {
473 template <class T1> friend class ExpectedAsOutParameter;
474 template <class OtherT> friend class Expected;
475
476 static constexpr bool isRef = std::is_reference<T>::value;
477
478 using wrap = std::reference_wrapper<std::remove_reference_t<T>>;
479
480 using error_type = std::unique_ptr<ErrorInfoBase>;
481
482public:
483 using storage_type = std::conditional_t<isRef, wrap, T>;
484 using value_type = T;
485
486private:
487 using reference = std::remove_reference_t<T> &;
488 using const_reference = const std::remove_reference_t<T> &;
489 using pointer = std::remove_reference_t<T> *;
490 using const_pointer = const std::remove_reference_t<T> *;
491
492public:
493 /// Create an Expected<T> error value from the given Error.
494 Expected(Error Err)
495 : HasError(true)
496#if LLVM_ENABLE_ABI_BREAKING_CHECKS0
497 // Expected is unchecked upon construction in Debug builds.
498 , Unchecked(true)
499#endif
500 {
501 assert(Err && "Cannot create Expected<T> from Error success value.")((void)0);
502 new (getErrorStorage()) error_type(Err.takePayload());
503 }
504
505 /// Forbid to convert from Error::success() implicitly, this avoids having
506 /// Expected<T> foo() { return Error::success(); } which compiles otherwise
507 /// but triggers the assertion above.
508 Expected(ErrorSuccess) = delete;
509
510 /// Create an Expected<T> success value from the given OtherT value, which
511 /// must be convertible to T.
512 template <typename OtherT>
513 Expected(OtherT &&Val,
514 std::enable_if_t<std::is_convertible<OtherT, T>::value> * = nullptr)
515 : HasError(false)
516#if LLVM_ENABLE_ABI_BREAKING_CHECKS0
517 // Expected is unchecked upon construction in Debug builds.
518 ,
519 Unchecked(true)
520#endif
521 {
522 new (getStorage()) storage_type(std::forward<OtherT>(Val));
523 }
524
525 /// Move construct an Expected<T> value.
526 Expected(Expected &&Other) { moveConstruct(std::move(Other)); }
527
528 /// Move construct an Expected<T> value from an Expected<OtherT>, where OtherT
529 /// must be convertible to T.
530 template <class OtherT>
531 Expected(
532 Expected<OtherT> &&Other,
533 std::enable_if_t<std::is_convertible<OtherT, T>::value> * = nullptr) {
534 moveConstruct(std::move(Other));
535 }
536
537 /// Move construct an Expected<T> value from an Expected<OtherT>, where OtherT
538 /// isn't convertible to T.
539 template <class OtherT>
540 explicit Expected(
541 Expected<OtherT> &&Other,
542 std::enable_if_t<!std::is_convertible<OtherT, T>::value> * = nullptr) {
543 moveConstruct(std::move(Other));
544 }
545
546 /// Move-assign from another Expected<T>.
547 Expected &operator=(Expected &&Other) {
548 moveAssign(std::move(Other));
549 return *this;
550 }
551
552 /// Destroy an Expected<T>.
553 ~Expected() {
554 assertIsChecked();
555 if (!HasError)
556 getStorage()->~storage_type();
557 else
558 getErrorStorage()->~error_type();
559 }
560
561 /// Return false if there is an error.
562 explicit operator bool() {
563#if LLVM_ENABLE_ABI_BREAKING_CHECKS0
564 Unchecked = HasError;
565#endif
566 return !HasError;
8
Assuming field 'HasError' is false, which participates in a condition later
9
Returning the value 1, which participates in a condition later
15
Assuming field 'HasError' is false, which participates in a condition later
16
Returning the value 1, which participates in a condition later
567 }
568
569 /// Returns a reference to the stored T value.
570 reference get() {
571 assertIsChecked();
572 return *getStorage();
573 }
574
575 /// Returns a const reference to the stored T value.
576 const_reference get() const {
577 assertIsChecked();
578 return const_cast<Expected<T> *>(this)->get();
579 }
580
581 /// Check that this Expected<T> is an error of type ErrT.
582 template <typename ErrT> bool errorIsA() const {
583 return HasError && (*getErrorStorage())->template isA<ErrT>();
584 }
585
586 /// Take ownership of the stored error.
587 /// After calling this the Expected<T> is in an indeterminate state that can
588 /// only be safely destructed. No further calls (beside the destructor) should
589 /// be made on the Expected<T> value.
590 Error takeError() {
591#if LLVM_ENABLE_ABI_BREAKING_CHECKS0
592 Unchecked = false;
593#endif
594 return HasError ? Error(std::move(*getErrorStorage())) : Error::success();
595 }
596
597 /// Returns a pointer to the stored T value.
598 pointer operator->() {
599 assertIsChecked();
600 return toPointer(getStorage());
601 }
602
603 /// Returns a const pointer to the stored T value.
604 const_pointer operator->() const {
605 assertIsChecked();
606 return toPointer(getStorage());
607 }
608
609 /// Returns a reference to the stored T value.
610 reference operator*() {
611 assertIsChecked();
612 return *getStorage();
613 }
614
615 /// Returns a const reference to the stored T value.
616 const_reference operator*() const {
617 assertIsChecked();
618 return *getStorage();
619 }
620
621private:
622 template <class T1>
623 static bool compareThisIfSameType(const T1 &a, const T1 &b) {
624 return &a == &b;
625 }
626
627 template <class T1, class T2>
628 static bool compareThisIfSameType(const T1 &, const T2 &) {
629 return false;
630 }
631
632 template <class OtherT> void moveConstruct(Expected<OtherT> &&Other) {
633 HasError = Other.HasError;
634#if LLVM_ENABLE_ABI_BREAKING_CHECKS0
635 Unchecked = true;
636 Other.Unchecked = false;
637#endif
638
639 if (!HasError)
640 new (getStorage()) storage_type(std::move(*Other.getStorage()));
641 else
642 new (getErrorStorage()) error_type(std::move(*Other.getErrorStorage()));
643 }
644
645 template <class OtherT> void moveAssign(Expected<OtherT> &&Other) {
646 assertIsChecked();
647
648 if (compareThisIfSameType(*this, Other))
649 return;
650
651 this->~Expected();
652 new (this) Expected(std::move(Other));
653 }
654
655 pointer toPointer(pointer Val) { return Val; }
656
657 const_pointer toPointer(const_pointer Val) const { return Val; }
658
659 pointer toPointer(wrap *Val) { return &Val->get(); }
660
661 const_pointer toPointer(const wrap *Val) const { return &Val->get(); }
662
663 storage_type *getStorage() {
664 assert(!HasError && "Cannot get value when an error exists!")((void)0);
665 return reinterpret_cast<storage_type *>(&TStorage);
666 }
667
668 const storage_type *getStorage() const {
669 assert(!HasError && "Cannot get value when an error exists!")((void)0);
670 return reinterpret_cast<const storage_type *>(&TStorage);
671 }
672
673 error_type *getErrorStorage() {
674 assert(HasError && "Cannot get error when a value exists!")((void)0);
675 return reinterpret_cast<error_type *>(&ErrorStorage);
676 }
677
678 const error_type *getErrorStorage() const {
679 assert(HasError && "Cannot get error when a value exists!")((void)0);
680 return reinterpret_cast<const error_type *>(&ErrorStorage);
681 }
682
683 // Used by ExpectedAsOutParameter to reset the checked flag.
684 void setUnchecked() {
685#if LLVM_ENABLE_ABI_BREAKING_CHECKS0
686 Unchecked = true;
687#endif
688 }
689
690#if LLVM_ENABLE_ABI_BREAKING_CHECKS0
691 LLVM_ATTRIBUTE_NORETURN__attribute__((noreturn))
692 LLVM_ATTRIBUTE_NOINLINE__attribute__((noinline))
693 void fatalUncheckedExpected() const {
694 dbgs() << "Expected<T> must be checked before access or destruction.\n";
695 if (HasError) {
696 dbgs() << "Unchecked Expected<T> contained error:\n";
697 (*getErrorStorage())->log(dbgs());
698 } else
699 dbgs() << "Expected<T> value was in success state. (Note: Expected<T> "
700 "values in success mode must still be checked prior to being "
701 "destroyed).\n";
702 abort();
703 }
704#endif
705
706 void assertIsChecked() const {
707#if LLVM_ENABLE_ABI_BREAKING_CHECKS0
708 if (LLVM_UNLIKELY(Unchecked)__builtin_expect((bool)(Unchecked), false))
709 fatalUncheckedExpected();
710#endif
711 }
712
713 union {
714 AlignedCharArrayUnion<storage_type> TStorage;
715 AlignedCharArrayUnion<error_type> ErrorStorage;
716 };
717 bool HasError : 1;
718#if LLVM_ENABLE_ABI_BREAKING_CHECKS0
719 bool Unchecked : 1;
720#endif
721};
722
723/// Report a serious error, calling any installed error handler. See
724/// ErrorHandling.h.
725LLVM_ATTRIBUTE_NORETURN__attribute__((noreturn)) void report_fatal_error(Error Err,
726 bool gen_crash_diag = true);
727
728/// Report a fatal error if Err is a failure value.
729///
730/// This function can be used to wrap calls to fallible functions ONLY when it
731/// is known that the Error will always be a success value. E.g.
732///
733/// @code{.cpp}
734/// // foo only attempts the fallible operation if DoFallibleOperation is
735/// // true. If DoFallibleOperation is false then foo always returns
736/// // Error::success().
737/// Error foo(bool DoFallibleOperation);
738///
739/// cantFail(foo(false));
740/// @endcode
741inline void cantFail(Error Err, const char *Msg = nullptr) {
742 if (Err) {
743 if (!Msg)
744 Msg = "Failure value returned from cantFail wrapped call";
745#ifndef NDEBUG1
746 std::string Str;
747 raw_string_ostream OS(Str);
748 OS << Msg << "\n" << Err;
749 Msg = OS.str().c_str();
750#endif
751 llvm_unreachable(Msg)__builtin_unreachable();
752 }
753}
754
755/// Report a fatal error if ValOrErr is a failure value, otherwise unwraps and
756/// returns the contained value.
757///
758/// This function can be used to wrap calls to fallible functions ONLY when it
759/// is known that the Error will always be a success value. E.g.
760///
761/// @code{.cpp}
762/// // foo only attempts the fallible operation if DoFallibleOperation is
763/// // true. If DoFallibleOperation is false then foo always returns an int.
764/// Expected<int> foo(bool DoFallibleOperation);
765///
766/// int X = cantFail(foo(false));
767/// @endcode
768template <typename T>
769T cantFail(Expected<T> ValOrErr, const char *Msg = nullptr) {
770 if (ValOrErr)
771 return std::move(*ValOrErr);
772 else {
773 if (!Msg)
774 Msg = "Failure value returned from cantFail wrapped call";
775#ifndef NDEBUG1
776 std::string Str;
777 raw_string_ostream OS(Str);
778 auto E = ValOrErr.takeError();
779 OS << Msg << "\n" << E;
780 Msg = OS.str().c_str();
781#endif
782 llvm_unreachable(Msg)__builtin_unreachable();
783 }
784}
785
786/// Report a fatal error if ValOrErr is a failure value, otherwise unwraps and
787/// returns the contained reference.
788///
789/// This function can be used to wrap calls to fallible functions ONLY when it
790/// is known that the Error will always be a success value. E.g.
791///
792/// @code{.cpp}
793/// // foo only attempts the fallible operation if DoFallibleOperation is
794/// // true. If DoFallibleOperation is false then foo always returns a Bar&.
795/// Expected<Bar&> foo(bool DoFallibleOperation);
796///
797/// Bar &X = cantFail(foo(false));
798/// @endcode
799template <typename T>
800T& cantFail(Expected<T&> ValOrErr, const char *Msg = nullptr) {
801 if (ValOrErr)
802 return *ValOrErr;
803 else {
804 if (!Msg)
805 Msg = "Failure value returned from cantFail wrapped call";
806#ifndef NDEBUG1
807 std::string Str;
808 raw_string_ostream OS(Str);
809 auto E = ValOrErr.takeError();
810 OS << Msg << "\n" << E;
811 Msg = OS.str().c_str();
812#endif
813 llvm_unreachable(Msg)__builtin_unreachable();
814 }
815}
816
817/// Helper for testing applicability of, and applying, handlers for
818/// ErrorInfo types.
819template <typename HandlerT>
820class ErrorHandlerTraits
821 : public ErrorHandlerTraits<decltype(
822 &std::remove_reference<HandlerT>::type::operator())> {};
823
824// Specialization functions of the form 'Error (const ErrT&)'.
825template <typename ErrT> class ErrorHandlerTraits<Error (&)(ErrT &)> {
826public:
827 static bool appliesTo(const ErrorInfoBase &E) {
828 return E.template isA<ErrT>();
829 }
830
831 template <typename HandlerT>
832 static Error apply(HandlerT &&H, std::unique_ptr<ErrorInfoBase> E) {
833 assert(appliesTo(*E) && "Applying incorrect handler")((void)0);
834 return H(static_cast<ErrT &>(*E));
835 }
836};
837
838// Specialization functions of the form 'void (const ErrT&)'.
839template <typename ErrT> class ErrorHandlerTraits<void (&)(ErrT &)> {
840public:
841 static bool appliesTo(const ErrorInfoBase &E) {
842 return E.template isA<ErrT>();
843 }
844
845 template <typename HandlerT>
846 static Error apply(HandlerT &&H, std::unique_ptr<ErrorInfoBase> E) {
847 assert(appliesTo(*E) && "Applying incorrect handler")((void)0);
848 H(static_cast<ErrT &>(*E));
849 return Error::success();
850 }
851};
852
853/// Specialization for functions of the form 'Error (std::unique_ptr<ErrT>)'.
854template <typename ErrT>
855class ErrorHandlerTraits<Error (&)(std::unique_ptr<ErrT>)> {
856public:
857 static bool appliesTo(const ErrorInfoBase &E) {
858 return E.template isA<ErrT>();
859 }
860
861 template <typename HandlerT>
862 static Error apply(HandlerT &&H, std::unique_ptr<ErrorInfoBase> E) {
863 assert(appliesTo(*E) && "Applying incorrect handler")((void)0);
864 std::unique_ptr<ErrT> SubE(static_cast<ErrT *>(E.release()));
865 return H(std::move(SubE));
866 }
867};
868
869/// Specialization for functions of the form 'void (std::unique_ptr<ErrT>)'.
870template <typename ErrT>
871class ErrorHandlerTraits<void (&)(std::unique_ptr<ErrT>)> {
872public:
873 static bool appliesTo(const ErrorInfoBase &E) {
874 return E.template isA<ErrT>();
875 }
876
877 template <typename HandlerT>
878 static Error apply(HandlerT &&H, std::unique_ptr<ErrorInfoBase> E) {
879 assert(appliesTo(*E) && "Applying incorrect handler")((void)0);
880 std::unique_ptr<ErrT> SubE(static_cast<ErrT *>(E.release()));
881 H(std::move(SubE));
882 return Error::success();
883 }
884};
885
886// Specialization for member functions of the form 'RetT (const ErrT&)'.
887template <typename C, typename RetT, typename ErrT>
888class ErrorHandlerTraits<RetT (C::*)(ErrT &)>
889 : public ErrorHandlerTraits<RetT (&)(ErrT &)> {};
890
891// Specialization for member functions of the form 'RetT (const ErrT&) const'.
892template <typename C, typename RetT, typename ErrT>
893class ErrorHandlerTraits<RetT (C::*)(ErrT &) const>
894 : public ErrorHandlerTraits<RetT (&)(ErrT &)> {};
895
896// Specialization for member functions of the form 'RetT (const ErrT&)'.
897template <typename C, typename RetT, typename ErrT>
898class ErrorHandlerTraits<RetT (C::*)(const ErrT &)>
899 : public ErrorHandlerTraits<RetT (&)(ErrT &)> {};
900
901// Specialization for member functions of the form 'RetT (const ErrT&) const'.
902template <typename C, typename RetT, typename ErrT>
903class ErrorHandlerTraits<RetT (C::*)(const ErrT &) const>
904 : public ErrorHandlerTraits<RetT (&)(ErrT &)> {};
905
906/// Specialization for member functions of the form
907/// 'RetT (std::unique_ptr<ErrT>)'.
908template <typename C, typename RetT, typename ErrT>
909class ErrorHandlerTraits<RetT (C::*)(std::unique_ptr<ErrT>)>
910 : public ErrorHandlerTraits<RetT (&)(std::unique_ptr<ErrT>)> {};
911
912/// Specialization for member functions of the form
913/// 'RetT (std::unique_ptr<ErrT>) const'.
914template <typename C, typename RetT, typename ErrT>
915class ErrorHandlerTraits<RetT (C::*)(std::unique_ptr<ErrT>) const>
916 : public ErrorHandlerTraits<RetT (&)(std::unique_ptr<ErrT>)> {};
917
918inline Error handleErrorImpl(std::unique_ptr<ErrorInfoBase> Payload) {
919 return Error(std::move(Payload));
920}
921
922template <typename HandlerT, typename... HandlerTs>
923Error handleErrorImpl(std::unique_ptr<ErrorInfoBase> Payload,
924 HandlerT &&Handler, HandlerTs &&... Handlers) {
925 if (ErrorHandlerTraits<HandlerT>::appliesTo(*Payload))
926 return ErrorHandlerTraits<HandlerT>::apply(std::forward<HandlerT>(Handler),
927 std::move(Payload));
928 return handleErrorImpl(std::move(Payload),
929 std::forward<HandlerTs>(Handlers)...);
930}
931
932/// Pass the ErrorInfo(s) contained in E to their respective handlers. Any
933/// unhandled errors (or Errors returned by handlers) are re-concatenated and
934/// returned.
935/// Because this function returns an error, its result must also be checked
936/// or returned. If you intend to handle all errors use handleAllErrors
937/// (which returns void, and will abort() on unhandled errors) instead.
938template <typename... HandlerTs>
939Error handleErrors(Error E, HandlerTs &&... Hs) {
940 if (!E)
941 return Error::success();
942
943 std::unique_ptr<ErrorInfoBase> Payload = E.takePayload();
944
945 if (Payload->isA<ErrorList>()) {
946 ErrorList &List = static_cast<ErrorList &>(*Payload);
947 Error R;
948 for (auto &P : List.Payloads)
949 R = ErrorList::join(
950 std::move(R),
951 handleErrorImpl(std::move(P), std::forward<HandlerTs>(Hs)...));
952 return R;
953 }
954
955 return handleErrorImpl(std::move(Payload), std::forward<HandlerTs>(Hs)...);
956}
957
958/// Behaves the same as handleErrors, except that by contract all errors
959/// *must* be handled by the given handlers (i.e. there must be no remaining
960/// errors after running the handlers, or llvm_unreachable is called).
961template <typename... HandlerTs>
962void handleAllErrors(Error E, HandlerTs &&... Handlers) {
963 cantFail(handleErrors(std::move(E), std::forward<HandlerTs>(Handlers)...));
964}
965
966/// Check that E is a non-error, then drop it.
967/// If E is an error, llvm_unreachable will be called.
968inline void handleAllErrors(Error E) {
969 cantFail(std::move(E));
970}
971
972/// Handle any errors (if present) in an Expected<T>, then try a recovery path.
973///
974/// If the incoming value is a success value it is returned unmodified. If it
975/// is a failure value then it the contained error is passed to handleErrors.
976/// If handleErrors is able to handle the error then the RecoveryPath functor
977/// is called to supply the final result. If handleErrors is not able to
978/// handle all errors then the unhandled errors are returned.
979///
980/// This utility enables the follow pattern:
981///
982/// @code{.cpp}
983/// enum FooStrategy { Aggressive, Conservative };
984/// Expected<Foo> foo(FooStrategy S);
985///
986/// auto ResultOrErr =
987/// handleExpected(
988/// foo(Aggressive),
989/// []() { return foo(Conservative); },
990/// [](AggressiveStrategyError&) {
991/// // Implicitly conusme this - we'll recover by using a conservative
992/// // strategy.
993/// });
994///
995/// @endcode
996template <typename T, typename RecoveryFtor, typename... HandlerTs>
997Expected<T> handleExpected(Expected<T> ValOrErr, RecoveryFtor &&RecoveryPath,
998 HandlerTs &&... Handlers) {
999 if (ValOrErr)
1000 return ValOrErr;
1001
1002 if (auto Err = handleErrors(ValOrErr.takeError(),
1003 std::forward<HandlerTs>(Handlers)...))
1004 return std::move(Err);
1005
1006 return RecoveryPath();
1007}
1008
1009/// Log all errors (if any) in E to OS. If there are any errors, ErrorBanner
1010/// will be printed before the first one is logged. A newline will be printed
1011/// after each error.
1012///
1013/// This function is compatible with the helpers from Support/WithColor.h. You
1014/// can pass any of them as the OS. Please consider using them instead of
1015/// including 'error: ' in the ErrorBanner.
1016///
1017/// This is useful in the base level of your program to allow clean termination
1018/// (allowing clean deallocation of resources, etc.), while reporting error
1019/// information to the user.
1020void logAllUnhandledErrors(Error E, raw_ostream &OS, Twine ErrorBanner = {});
1021
1022/// Write all error messages (if any) in E to a string. The newline character
1023/// is used to separate error messages.
1024inline std::string toString(Error E) {
1025 SmallVector<std::string, 2> Errors;
1026 handleAllErrors(std::move(E), [&Errors](const ErrorInfoBase &EI) {
1027 Errors.push_back(EI.message());
1028 });
1029 return join(Errors.begin(), Errors.end(), "\n");
1030}
1031
1032/// Consume a Error without doing anything. This method should be used
1033/// only where an error can be considered a reasonable and expected return
1034/// value.
1035///
1036/// Uses of this method are potentially indicative of design problems: If it's
1037/// legitimate to do nothing while processing an "error", the error-producer
1038/// might be more clearly refactored to return an Optional<T>.
1039inline void consumeError(Error Err) {
1040 handleAllErrors(std::move(Err), [](const ErrorInfoBase &) {});
1041}
1042
1043/// Convert an Expected to an Optional without doing anything. This method
1044/// should be used only where an error can be considered a reasonable and
1045/// expected return value.
1046///
1047/// Uses of this method are potentially indicative of problems: perhaps the
1048/// error should be propagated further, or the error-producer should just
1049/// return an Optional in the first place.
1050template <typename T> Optional<T> expectedToOptional(Expected<T> &&E) {
1051 if (E)
1052 return std::move(*E);
1053 consumeError(E.takeError());
1054 return None;
1055}
1056
1057/// Helper for converting an Error to a bool.
1058///
1059/// This method returns true if Err is in an error state, or false if it is
1060/// in a success state. Puts Err in a checked state in both cases (unlike
1061/// Error::operator bool(), which only does this for success states).
1062inline bool errorToBool(Error Err) {
1063 bool IsError = static_cast<bool>(Err);
1064 if (IsError)
1065 consumeError(std::move(Err));
1066 return IsError;
1067}
1068
1069/// Helper for Errors used as out-parameters.
1070///
1071/// This helper is for use with the Error-as-out-parameter idiom, where an error
1072/// is passed to a function or method by reference, rather than being returned.
1073/// In such cases it is helpful to set the checked bit on entry to the function
1074/// so that the error can be written to (unchecked Errors abort on assignment)
1075/// and clear the checked bit on exit so that clients cannot accidentally forget
1076/// to check the result. This helper performs these actions automatically using
1077/// RAII:
1078///
1079/// @code{.cpp}
1080/// Result foo(Error &Err) {
1081/// ErrorAsOutParameter ErrAsOutParam(&Err); // 'Checked' flag set
1082/// // <body of foo>
1083/// // <- 'Checked' flag auto-cleared when ErrAsOutParam is destructed.
1084/// }
1085/// @endcode
1086///
1087/// ErrorAsOutParameter takes an Error* rather than Error& so that it can be
1088/// used with optional Errors (Error pointers that are allowed to be null). If
1089/// ErrorAsOutParameter took an Error reference, an instance would have to be
1090/// created inside every condition that verified that Error was non-null. By
1091/// taking an Error pointer we can just create one instance at the top of the
1092/// function.
1093class ErrorAsOutParameter {
1094public:
1095 ErrorAsOutParameter(Error *Err) : Err(Err) {
1096 // Raise the checked bit if Err is success.
1097 if (Err)
1098 (void)!!*Err;
1099 }
1100
1101 ~ErrorAsOutParameter() {
1102 // Clear the checked bit.
1103 if (Err && !*Err)
1104 *Err = Error::success();
1105 }
1106
1107private:
1108 Error *Err;
1109};
1110
1111/// Helper for Expected<T>s used as out-parameters.
1112///
1113/// See ErrorAsOutParameter.
1114template <typename T>
1115class ExpectedAsOutParameter {
1116public:
1117 ExpectedAsOutParameter(Expected<T> *ValOrErr)
1118 : ValOrErr(ValOrErr) {
1119 if (ValOrErr)
1120 (void)!!*ValOrErr;
1121 }
1122
1123 ~ExpectedAsOutParameter() {
1124 if (ValOrErr)
1125 ValOrErr->setUnchecked();
1126 }
1127
1128private:
1129 Expected<T> *ValOrErr;
1130};
1131
1132/// This class wraps a std::error_code in a Error.
1133///
1134/// This is useful if you're writing an interface that returns a Error
1135/// (or Expected) and you want to call code that still returns
1136/// std::error_codes.
1137class ECError : public ErrorInfo<ECError> {
1138 friend Error errorCodeToError(std::error_code);
1139
1140 virtual void anchor() override;
1141
1142public:
1143 void setErrorCode(std::error_code EC) { this->EC = EC; }
1144 std::error_code convertToErrorCode() const override { return EC; }
1145 void log(raw_ostream &OS) const override { OS << EC.message(); }
1146
1147 // Used by ErrorInfo::classID.
1148 static char ID;
1149
1150protected:
1151 ECError() = default;
1152 ECError(std::error_code EC) : EC(EC) {}
1153
1154 std::error_code EC;
1155};
1156
1157/// The value returned by this function can be returned from convertToErrorCode
1158/// for Error values where no sensible translation to std::error_code exists.
1159/// It should only be used in this situation, and should never be used where a
1160/// sensible conversion to std::error_code is available, as attempts to convert
1161/// to/from this error will result in a fatal error. (i.e. it is a programmatic
1162///error to try to convert such a value).
1163std::error_code inconvertibleErrorCode();
1164
1165/// Helper for converting an std::error_code to a Error.
1166Error errorCodeToError(std::error_code EC);
1167
1168/// Helper for converting an ECError to a std::error_code.
1169///
1170/// This method requires that Err be Error() or an ECError, otherwise it
1171/// will trigger a call to abort().
1172std::error_code errorToErrorCode(Error Err);
1173
1174/// Convert an ErrorOr<T> to an Expected<T>.
1175template <typename T> Expected<T> errorOrToExpected(ErrorOr<T> &&EO) {
1176 if (auto EC = EO.getError())
1177 return errorCodeToError(EC);
1178 return std::move(*EO);
1179}
1180
1181/// Convert an Expected<T> to an ErrorOr<T>.
1182template <typename T> ErrorOr<T> expectedToErrorOr(Expected<T> &&E) {
1183 if (auto Err = E.takeError())
1184 return errorToErrorCode(std::move(Err));
1185 return std::move(*E);
1186}
1187
1188/// This class wraps a string in an Error.
1189///
1190/// StringError is useful in cases where the client is not expected to be able
1191/// to consume the specific error message programmatically (for example, if the
1192/// error message is to be presented to the user).
1193///
1194/// StringError can also be used when additional information is to be printed
1195/// along with a error_code message. Depending on the constructor called, this
1196/// class can either display:
1197/// 1. the error_code message (ECError behavior)
1198/// 2. a string
1199/// 3. the error_code message and a string
1200///
1201/// These behaviors are useful when subtyping is required; for example, when a
1202/// specific library needs an explicit error type. In the example below,
1203/// PDBError is derived from StringError:
1204///
1205/// @code{.cpp}
1206/// Expected<int> foo() {
1207/// return llvm::make_error<PDBError>(pdb_error_code::dia_failed_loading,
1208/// "Additional information");
1209/// }
1210/// @endcode
1211///
1212class StringError : public ErrorInfo<StringError> {
1213public:
1214 static char ID;
1215
1216 // Prints EC + S and converts to EC
1217 StringError(std::error_code EC, const Twine &S = Twine());
1218
1219 // Prints S and converts to EC
1220 StringError(const Twine &S, std::error_code EC);
1221
1222 void log(raw_ostream &OS) const override;
1223 std::error_code convertToErrorCode() const override;
1224
1225 const std::string &getMessage() const { return Msg; }
1226
1227private:
1228 std::string Msg;
1229 std::error_code EC;
1230 const bool PrintMsgOnly = false;
1231};
1232
1233/// Create formatted StringError object.
1234template <typename... Ts>
1235inline Error createStringError(std::error_code EC, char const *Fmt,
1236 const Ts &... Vals) {
1237 std::string Buffer;
1238 raw_string_ostream Stream(Buffer);
1239 Stream << format(Fmt, Vals...);
1240 return make_error<StringError>(Stream.str(), EC);
1241}
1242
1243Error createStringError(std::error_code EC, char const *Msg);
1244
1245inline Error createStringError(std::error_code EC, const Twine &S) {
1246 return createStringError(EC, S.str().c_str());
1247}
1248
1249template <typename... Ts>
1250inline Error createStringError(std::errc EC, char const *Fmt,
1251 const Ts &... Vals) {
1252 return createStringError(std::make_error_code(EC), Fmt, Vals...);
1253}
1254
1255/// This class wraps a filename and another Error.
1256///
1257/// In some cases, an error needs to live along a 'source' name, in order to
1258/// show more detailed information to the user.
1259class FileError final : public ErrorInfo<FileError> {
1260
1261 friend Error createFileError(const Twine &, Error);
1262 friend Error createFileError(const Twine &, size_t, Error);
1263
1264public:
1265 void log(raw_ostream &OS) const override {
1266 assert(Err && !FileName.empty() && "Trying to log after takeError().")((void)0);
1267 OS << "'" << FileName << "': ";
1268 if (Line.hasValue())
1269 OS << "line " << Line.getValue() << ": ";
1270 Err->log(OS);
1271 }
1272
1273 StringRef getFileName() { return FileName; }
1274
1275 Error takeError() { return Error(std::move(Err)); }
1276
1277 std::error_code convertToErrorCode() const override;
1278
1279 // Used by ErrorInfo::classID.
1280 static char ID;
1281
1282private:
1283 FileError(const Twine &F, Optional<size_t> LineNum,
1284 std::unique_ptr<ErrorInfoBase> E) {
1285 assert(E && "Cannot create FileError from Error success value.")((void)0);
1286 assert(!F.isTriviallyEmpty() &&((void)0)
1287 "The file name provided to FileError must not be empty.")((void)0);
1288 FileName = F.str();
1289 Err = std::move(E);
1290 Line = std::move(LineNum);
1291 }
1292
1293 static Error build(const Twine &F, Optional<size_t> Line, Error E) {
1294 std::unique_ptr<ErrorInfoBase> Payload;
1295 handleAllErrors(std::move(E),
1296 [&](std::unique_ptr<ErrorInfoBase> EIB) -> Error {
1297 Payload = std::move(EIB);
1298 return Error::success();
1299 });
1300 return Error(
1301 std::unique_ptr<FileError>(new FileError(F, Line, std::move(Payload))));
1302 }
1303
1304 std::string FileName;
1305 Optional<size_t> Line;
1306 std::unique_ptr<ErrorInfoBase> Err;
1307};
1308
1309/// Concatenate a source file path and/or name with an Error. The resulting
1310/// Error is unchecked.
1311inline Error createFileError(const Twine &F, Error E) {
1312 return FileError::build(F, Optional<size_t>(), std::move(E));
1313}
1314
1315/// Concatenate a source file path and/or name with line number and an Error.
1316/// The resulting Error is unchecked.
1317inline Error createFileError(const Twine &F, size_t Line, Error E) {
1318 return FileError::build(F, Optional<size_t>(Line), std::move(E));
1319}
1320
1321/// Concatenate a source file path and/or name with a std::error_code
1322/// to form an Error object.
1323inline Error createFileError(const Twine &F, std::error_code EC) {
1324 return createFileError(F, errorCodeToError(EC));
1325}
1326
1327/// Concatenate a source file path and/or name with line number and
1328/// std::error_code to form an Error object.
1329inline Error createFileError(const Twine &F, size_t Line, std::error_code EC) {
1330 return createFileError(F, Line, errorCodeToError(EC));
1331}
1332
1333Error createFileError(const Twine &F, ErrorSuccess) = delete;
1334
1335/// Helper for check-and-exit error handling.
1336///
1337/// For tool use only. NOT FOR USE IN LIBRARY CODE.
1338///
1339class ExitOnError {
1340public:
1341 /// Create an error on exit helper.
1342 ExitOnError(std::string Banner = "", int DefaultErrorExitCode = 1)
1343 : Banner(std::move(Banner)),
1344 GetExitCode([=](const Error &) { return DefaultErrorExitCode; }) {}
1345
1346 /// Set the banner string for any errors caught by operator().
1347 void setBanner(std::string Banner) { this->Banner = std::move(Banner); }
1348
1349 /// Set the exit-code mapper function.
1350 void setExitCodeMapper(std::function<int(const Error &)> GetExitCode) {
1351 this->GetExitCode = std::move(GetExitCode);
1352 }
1353
1354 /// Check Err. If it's in a failure state log the error(s) and exit.
1355 void operator()(Error Err) const { checkError(std::move(Err)); }
1356
1357 /// Check E. If it's in a success state then return the contained value. If
1358 /// it's in a failure state log the error(s) and exit.
1359 template <typename T> T operator()(Expected<T> &&E) const {
1360 checkError(E.takeError());
1361 return std::move(*E);
1362 }
1363
1364 /// Check E. If it's in a success state then return the contained reference. If
1365 /// it's in a failure state log the error(s) and exit.
1366 template <typename T> T& operator()(Expected<T&> &&E) const {
1367 checkError(E.takeError());
1368 return *E;
1369 }
1370
1371private:
1372 void checkError(Error Err) const {
1373 if (Err) {
1374 int ExitCode = GetExitCode(Err);
1375 logAllUnhandledErrors(std::move(Err), errs(), Banner);
1376 exit(ExitCode);
1377 }
1378 }
1379
1380 std::string Banner;
1381 std::function<int(const Error &)> GetExitCode;
1382};
1383
1384/// Conversion from Error to LLVMErrorRef for C error bindings.
1385inline LLVMErrorRef wrap(Error Err) {
1386 return reinterpret_cast<LLVMErrorRef>(Err.takePayload().release());
1387}
1388
1389/// Conversion from LLVMErrorRef to Error for C error bindings.
1390inline Error unwrap(LLVMErrorRef ErrRef) {
1391 return Error(std::unique_ptr<ErrorInfoBase>(
1392 reinterpret_cast<ErrorInfoBase *>(ErrRef)));
1393}
1394
1395} // end namespace llvm
1396
1397#endif // LLVM_SUPPORT_ERROR_H