Bug Summary

File:llvm/include/llvm/Bitstream/BitstreamReader.h
Warning:line 221, column 39
The result of the right shift is undefined due to shifting by '64', which is greater or equal to the width of type 'llvm::SimpleBitstreamCursor::word_t'

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name MetadataLoader.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 -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -mthread-model posix -mframe-pointer=none -fmath-errno -fno-rounding-math -masm-verbose -mconstructor-aliases -munwind-tables -target-cpu x86-64 -dwarf-column-info -fno-split-dwarf-inlining -debugger-tuning=gdb -ffunction-sections -fdata-sections -resource-dir /usr/lib/llvm-11/lib/clang/11.0.0 -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I /build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/build-llvm/lib/Bitcode/Reader -I /build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/Bitcode/Reader -I /build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/build-llvm/include -I /build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include -U NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/x86_64-linux-gnu/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/x86_64-linux-gnu/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0/backward -internal-isystem /usr/local/include -internal-isystem /usr/lib/llvm-11/lib/clang/11.0.0/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-maybe-uninitialized -Wno-comment -std=c++14 -fdeprecated-macro -fdebug-compilation-dir /build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/build-llvm/lib/Bitcode/Reader -fdebug-prefix-map=/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347=. -ferror-limit 19 -fmessage-length 0 -fvisibility-inlines-hidden -stack-protector 2 -fgnuc-version=4.2.1 -fobjc-runtime=gcc -fdiagnostics-show-option -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -faddrsig -o /tmp/scan-build-2020-03-09-184146-41876-1 -x c++ /build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/Bitcode/Reader/MetadataLoader.cpp

/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/Bitcode/Reader/MetadataLoader.cpp

1//===- MetadataLoader.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 "MetadataLoader.h"
10#include "ValueList.h"
11
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/DenseSet.h"
17#include "llvm/ADT/None.h"
18#include "llvm/ADT/STLExtras.h"
19#include "llvm/ADT/SmallString.h"
20#include "llvm/ADT/SmallVector.h"
21#include "llvm/ADT/Statistic.h"
22#include "llvm/ADT/StringRef.h"
23#include "llvm/ADT/Twine.h"
24#include "llvm/Bitcode/BitcodeReader.h"
25#include "llvm/Bitstream/BitstreamReader.h"
26#include "llvm/Bitcode/LLVMBitCodes.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/DebugInfo.h"
36#include "llvm/IR/DebugInfoMetadata.h"
37#include "llvm/IR/DebugLoc.h"
38#include "llvm/IR/DerivedTypes.h"
39#include "llvm/IR/DiagnosticPrinter.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/InstrTypes.h"
50#include "llvm/IR/Instruction.h"
51#include "llvm/IR/Instructions.h"
52#include "llvm/IR/IntrinsicInst.h"
53#include "llvm/IR/Intrinsics.h"
54#include "llvm/IR/LLVMContext.h"
55#include "llvm/IR/Module.h"
56#include "llvm/IR/ModuleSummaryIndex.h"
57#include "llvm/IR/OperandTraits.h"
58#include "llvm/IR/TrackingMDRef.h"
59#include "llvm/IR/Type.h"
60#include "llvm/IR/ValueHandle.h"
61#include "llvm/Support/AtomicOrdering.h"
62#include "llvm/Support/Casting.h"
63#include "llvm/Support/CommandLine.h"
64#include "llvm/Support/Compiler.h"
65#include "llvm/Support/Debug.h"
66#include "llvm/Support/Error.h"
67#include "llvm/Support/ErrorHandling.h"
68#include "llvm/Support/ManagedStatic.h"
69#include "llvm/Support/MemoryBuffer.h"
70#include "llvm/Support/raw_ostream.h"
71#include <algorithm>
72#include <cassert>
73#include <cstddef>
74#include <cstdint>
75#include <deque>
76#include <limits>
77#include <map>
78#include <memory>
79#include <string>
80#include <system_error>
81#include <tuple>
82#include <utility>
83#include <vector>
84
85using namespace llvm;
86
87#define DEBUG_TYPE"bitcode-reader" "bitcode-reader"
88
89STATISTIC(NumMDStringLoaded, "Number of MDStrings loaded")static llvm::Statistic NumMDStringLoaded = {"bitcode-reader",
"NumMDStringLoaded", "Number of MDStrings loaded"}
;
90STATISTIC(NumMDNodeTemporary, "Number of MDNode::Temporary created")static llvm::Statistic NumMDNodeTemporary = {"bitcode-reader"
, "NumMDNodeTemporary", "Number of MDNode::Temporary created"
}
;
91STATISTIC(NumMDRecordLoaded, "Number of Metadata records loaded")static llvm::Statistic NumMDRecordLoaded = {"bitcode-reader",
"NumMDRecordLoaded", "Number of Metadata records loaded"}
;
92
93/// Flag whether we need to import full type definitions for ThinLTO.
94/// Currently needed for Darwin and LLDB.
95static cl::opt<bool> ImportFullTypeDefinitions(
96 "import-full-type-definitions", cl::init(false), cl::Hidden,
97 cl::desc("Import full type definitions for ThinLTO."));
98
99static cl::opt<bool> DisableLazyLoading(
100 "disable-ondemand-mds-loading", cl::init(false), cl::Hidden,
101 cl::desc("Force disable the lazy-loading on-demand of metadata when "
102 "loading bitcode for importing."));
103
104namespace {
105
106static int64_t unrotateSign(uint64_t U) { return (U & 1) ? ~(U >> 1) : U >> 1; }
107
108class BitcodeReaderMetadataList {
109 /// Array of metadata references.
110 ///
111 /// Don't use std::vector here. Some versions of libc++ copy (instead of
112 /// move) on resize, and TrackingMDRef is very expensive to copy.
113 SmallVector<TrackingMDRef, 1> MetadataPtrs;
114
115 /// The set of indices in MetadataPtrs above of forward references that were
116 /// generated.
117 SmallDenseSet<unsigned, 1> ForwardReference;
118
119 /// The set of indices in MetadataPtrs above of Metadata that need to be
120 /// resolved.
121 SmallDenseSet<unsigned, 1> UnresolvedNodes;
122
123 /// Structures for resolving old type refs.
124 struct {
125 SmallDenseMap<MDString *, TempMDTuple, 1> Unknown;
126 SmallDenseMap<MDString *, DICompositeType *, 1> Final;
127 SmallDenseMap<MDString *, DICompositeType *, 1> FwdDecls;
128 SmallVector<std::pair<TrackingMDRef, TempMDTuple>, 1> Arrays;
129 } OldTypeRefs;
130
131 LLVMContext &Context;
132
133 /// Maximum number of valid references. Forward references exceeding the
134 /// maximum must be invalid.
135 unsigned RefsUpperBound;
136
137public:
138 BitcodeReaderMetadataList(LLVMContext &C, size_t RefsUpperBound)
139 : Context(C),
140 RefsUpperBound(std::min((size_t)std::numeric_limits<unsigned>::max(),
141 RefsUpperBound)) {}
142
143 // vector compatibility methods
144 unsigned size() const { return MetadataPtrs.size(); }
145 void resize(unsigned N) { MetadataPtrs.resize(N); }
146 void push_back(Metadata *MD) { MetadataPtrs.emplace_back(MD); }
147 void clear() { MetadataPtrs.clear(); }
148 Metadata *back() const { return MetadataPtrs.back(); }
149 void pop_back() { MetadataPtrs.pop_back(); }
150 bool empty() const { return MetadataPtrs.empty(); }
151
152 Metadata *operator[](unsigned i) const {
153 assert(i < MetadataPtrs.size())((i < MetadataPtrs.size()) ? static_cast<void> (0) :
__assert_fail ("i < MetadataPtrs.size()", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/Bitcode/Reader/MetadataLoader.cpp"
, 153, __PRETTY_FUNCTION__))
;
154 return MetadataPtrs[i];
155 }
156
157 Metadata *lookup(unsigned I) const {
158 if (I < MetadataPtrs.size())
159 return MetadataPtrs[I];
160 return nullptr;
161 }
162
163 void shrinkTo(unsigned N) {
164 assert(N <= size() && "Invalid shrinkTo request!")((N <= size() && "Invalid shrinkTo request!") ? static_cast
<void> (0) : __assert_fail ("N <= size() && \"Invalid shrinkTo request!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/Bitcode/Reader/MetadataLoader.cpp"
, 164, __PRETTY_FUNCTION__))
;
165 assert(ForwardReference.empty() && "Unexpected forward refs")((ForwardReference.empty() && "Unexpected forward refs"
) ? static_cast<void> (0) : __assert_fail ("ForwardReference.empty() && \"Unexpected forward refs\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/Bitcode/Reader/MetadataLoader.cpp"
, 165, __PRETTY_FUNCTION__))
;
166 assert(UnresolvedNodes.empty() && "Unexpected unresolved node")((UnresolvedNodes.empty() && "Unexpected unresolved node"
) ? static_cast<void> (0) : __assert_fail ("UnresolvedNodes.empty() && \"Unexpected unresolved node\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/Bitcode/Reader/MetadataLoader.cpp"
, 166, __PRETTY_FUNCTION__))
;
167 MetadataPtrs.resize(N);
168 }
169
170 /// Return the given metadata, creating a replaceable forward reference if
171 /// necessary.
172 Metadata *getMetadataFwdRef(unsigned Idx);
173
174 /// Return the given metadata only if it is fully resolved.
175 ///
176 /// Gives the same result as \a lookup(), unless \a MDNode::isResolved()
177 /// would give \c false.
178 Metadata *getMetadataIfResolved(unsigned Idx);
179
180 MDNode *getMDNodeFwdRefOrNull(unsigned Idx);
181 void assignValue(Metadata *MD, unsigned Idx);
182 void tryToResolveCycles();
183 bool hasFwdRefs() const { return !ForwardReference.empty(); }
184 int getNextFwdRef() {
185 assert(hasFwdRefs())((hasFwdRefs()) ? static_cast<void> (0) : __assert_fail
("hasFwdRefs()", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/Bitcode/Reader/MetadataLoader.cpp"
, 185, __PRETTY_FUNCTION__))
;
186 return *ForwardReference.begin();
187 }
188
189 /// Upgrade a type that had an MDString reference.
190 void addTypeRef(MDString &UUID, DICompositeType &CT);
191
192 /// Upgrade a type that had an MDString reference.
193 Metadata *upgradeTypeRef(Metadata *MaybeUUID);
194
195 /// Upgrade a type ref array that may have MDString references.
196 Metadata *upgradeTypeRefArray(Metadata *MaybeTuple);
197
198private:
199 Metadata *resolveTypeRefArray(Metadata *MaybeTuple);
200};
201
202void BitcodeReaderMetadataList::assignValue(Metadata *MD, unsigned Idx) {
203 if (auto *MDN = dyn_cast<MDNode>(MD))
204 if (!MDN->isResolved())
205 UnresolvedNodes.insert(Idx);
206
207 if (Idx == size()) {
208 push_back(MD);
209 return;
210 }
211
212 if (Idx >= size())
213 resize(Idx + 1);
214
215 TrackingMDRef &OldMD = MetadataPtrs[Idx];
216 if (!OldMD) {
217 OldMD.reset(MD);
218 return;
219 }
220
221 // If there was a forward reference to this value, replace it.
222 TempMDTuple PrevMD(cast<MDTuple>(OldMD.get()));
223 PrevMD->replaceAllUsesWith(MD);
224 ForwardReference.erase(Idx);
225}
226
227Metadata *BitcodeReaderMetadataList::getMetadataFwdRef(unsigned Idx) {
228 // Bail out for a clearly invalid value.
229 if (Idx >= RefsUpperBound)
230 return nullptr;
231
232 if (Idx >= size())
233 resize(Idx + 1);
234
235 if (Metadata *MD = MetadataPtrs[Idx])
236 return MD;
237
238 // Track forward refs to be resolved later.
239 ForwardReference.insert(Idx);
240
241 // Create and return a placeholder, which will later be RAUW'd.
242 ++NumMDNodeTemporary;
243 Metadata *MD = MDNode::getTemporary(Context, None).release();
244 MetadataPtrs[Idx].reset(MD);
245 return MD;
246}
247
248Metadata *BitcodeReaderMetadataList::getMetadataIfResolved(unsigned Idx) {
249 Metadata *MD = lookup(Idx);
250 if (auto *N = dyn_cast_or_null<MDNode>(MD))
251 if (!N->isResolved())
252 return nullptr;
253 return MD;
254}
255
256MDNode *BitcodeReaderMetadataList::getMDNodeFwdRefOrNull(unsigned Idx) {
257 return dyn_cast_or_null<MDNode>(getMetadataFwdRef(Idx));
258}
259
260void BitcodeReaderMetadataList::tryToResolveCycles() {
261 if (!ForwardReference.empty())
262 // Still forward references... can't resolve cycles.
263 return;
264
265 // Give up on finding a full definition for any forward decls that remain.
266 for (const auto &Ref : OldTypeRefs.FwdDecls)
267 OldTypeRefs.Final.insert(Ref);
268 OldTypeRefs.FwdDecls.clear();
269
270 // Upgrade from old type ref arrays. In strange cases, this could add to
271 // OldTypeRefs.Unknown.
272 for (const auto &Array : OldTypeRefs.Arrays)
273 Array.second->replaceAllUsesWith(resolveTypeRefArray(Array.first.get()));
274 OldTypeRefs.Arrays.clear();
275
276 // Replace old string-based type refs with the resolved node, if possible.
277 // If we haven't seen the node, leave it to the verifier to complain about
278 // the invalid string reference.
279 for (const auto &Ref : OldTypeRefs.Unknown) {
280 if (DICompositeType *CT = OldTypeRefs.Final.lookup(Ref.first))
281 Ref.second->replaceAllUsesWith(CT);
282 else
283 Ref.second->replaceAllUsesWith(Ref.first);
284 }
285 OldTypeRefs.Unknown.clear();
286
287 if (UnresolvedNodes.empty())
288 // Nothing to do.
289 return;
290
291 // Resolve any cycles.
292 for (unsigned I : UnresolvedNodes) {
293 auto &MD = MetadataPtrs[I];
294 auto *N = dyn_cast_or_null<MDNode>(MD);
295 if (!N)
296 continue;
297
298 assert(!N->isTemporary() && "Unexpected forward reference")((!N->isTemporary() && "Unexpected forward reference"
) ? static_cast<void> (0) : __assert_fail ("!N->isTemporary() && \"Unexpected forward reference\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/Bitcode/Reader/MetadataLoader.cpp"
, 298, __PRETTY_FUNCTION__))
;
299 N->resolveCycles();
300 }
301
302 // Make sure we return early again until there's another unresolved ref.
303 UnresolvedNodes.clear();
304}
305
306void BitcodeReaderMetadataList::addTypeRef(MDString &UUID,
307 DICompositeType &CT) {
308 assert(CT.getRawIdentifier() == &UUID && "Mismatched UUID")((CT.getRawIdentifier() == &UUID && "Mismatched UUID"
) ? static_cast<void> (0) : __assert_fail ("CT.getRawIdentifier() == &UUID && \"Mismatched UUID\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/Bitcode/Reader/MetadataLoader.cpp"
, 308, __PRETTY_FUNCTION__))
;
309 if (CT.isForwardDecl())
310 OldTypeRefs.FwdDecls.insert(std::make_pair(&UUID, &CT));
311 else
312 OldTypeRefs.Final.insert(std::make_pair(&UUID, &CT));
313}
314
315Metadata *BitcodeReaderMetadataList::upgradeTypeRef(Metadata *MaybeUUID) {
316 auto *UUID = dyn_cast_or_null<MDString>(MaybeUUID);
317 if (LLVM_LIKELY(!UUID)__builtin_expect((bool)(!UUID), true))
318 return MaybeUUID;
319
320 if (auto *CT = OldTypeRefs.Final.lookup(UUID))
321 return CT;
322
323 auto &Ref = OldTypeRefs.Unknown[UUID];
324 if (!Ref)
325 Ref = MDNode::getTemporary(Context, None);
326 return Ref.get();
327}
328
329Metadata *BitcodeReaderMetadataList::upgradeTypeRefArray(Metadata *MaybeTuple) {
330 auto *Tuple = dyn_cast_or_null<MDTuple>(MaybeTuple);
331 if (!Tuple || Tuple->isDistinct())
332 return MaybeTuple;
333
334 // Look through the array immediately if possible.
335 if (!Tuple->isTemporary())
336 return resolveTypeRefArray(Tuple);
337
338 // Create and return a placeholder to use for now. Eventually
339 // resolveTypeRefArrays() will be resolve this forward reference.
340 OldTypeRefs.Arrays.emplace_back(
341 std::piecewise_construct, std::forward_as_tuple(Tuple),
342 std::forward_as_tuple(MDTuple::getTemporary(Context, None)));
343 return OldTypeRefs.Arrays.back().second.get();
344}
345
346Metadata *BitcodeReaderMetadataList::resolveTypeRefArray(Metadata *MaybeTuple) {
347 auto *Tuple = dyn_cast_or_null<MDTuple>(MaybeTuple);
348 if (!Tuple || Tuple->isDistinct())
349 return MaybeTuple;
350
351 // Look through the DITypeRefArray, upgrading each DIType *.
352 SmallVector<Metadata *, 32> Ops;
353 Ops.reserve(Tuple->getNumOperands());
354 for (Metadata *MD : Tuple->operands())
355 Ops.push_back(upgradeTypeRef(MD));
356
357 return MDTuple::get(Context, Ops);
358}
359
360namespace {
361
362class PlaceholderQueue {
363 // Placeholders would thrash around when moved, so store in a std::deque
364 // instead of some sort of vector.
365 std::deque<DistinctMDOperandPlaceholder> PHs;
366
367public:
368 ~PlaceholderQueue() {
369 assert(empty() && "PlaceholderQueue hasn't been flushed before being destroyed")((empty() && "PlaceholderQueue hasn't been flushed before being destroyed"
) ? static_cast<void> (0) : __assert_fail ("empty() && \"PlaceholderQueue hasn't been flushed before being destroyed\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/Bitcode/Reader/MetadataLoader.cpp"
, 369, __PRETTY_FUNCTION__))
;
370 }
371 bool empty() { return PHs.empty(); }
372 DistinctMDOperandPlaceholder &getPlaceholderOp(unsigned ID);
373 void flush(BitcodeReaderMetadataList &MetadataList);
374
375 /// Return the list of temporaries nodes in the queue, these need to be
376 /// loaded before we can flush the queue.
377 void getTemporaries(BitcodeReaderMetadataList &MetadataList,
378 DenseSet<unsigned> &Temporaries) {
379 for (auto &PH : PHs) {
380 auto ID = PH.getID();
381 auto *MD = MetadataList.lookup(ID);
382 if (!MD) {
383 Temporaries.insert(ID);
384 continue;
385 }
386 auto *N = dyn_cast_or_null<MDNode>(MD);
387 if (N && N->isTemporary())
388 Temporaries.insert(ID);
389 }
390 }
391};
392
393} // end anonymous namespace
394
395DistinctMDOperandPlaceholder &PlaceholderQueue::getPlaceholderOp(unsigned ID) {
396 PHs.emplace_back(ID);
397 return PHs.back();
398}
399
400void PlaceholderQueue::flush(BitcodeReaderMetadataList &MetadataList) {
401 while (!PHs.empty()) {
402 auto *MD = MetadataList.lookup(PHs.front().getID());
403 assert(MD && "Flushing placeholder on unassigned MD")((MD && "Flushing placeholder on unassigned MD") ? static_cast
<void> (0) : __assert_fail ("MD && \"Flushing placeholder on unassigned MD\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/Bitcode/Reader/MetadataLoader.cpp"
, 403, __PRETTY_FUNCTION__))
;
404#ifndef NDEBUG
405 if (auto *MDN = dyn_cast<MDNode>(MD))
406 assert(MDN->isResolved() &&((MDN->isResolved() && "Flushing Placeholder while cycles aren't resolved"
) ? static_cast<void> (0) : __assert_fail ("MDN->isResolved() && \"Flushing Placeholder while cycles aren't resolved\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/Bitcode/Reader/MetadataLoader.cpp"
, 407, __PRETTY_FUNCTION__))
407 "Flushing Placeholder while cycles aren't resolved")((MDN->isResolved() && "Flushing Placeholder while cycles aren't resolved"
) ? static_cast<void> (0) : __assert_fail ("MDN->isResolved() && \"Flushing Placeholder while cycles aren't resolved\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/Bitcode/Reader/MetadataLoader.cpp"
, 407, __PRETTY_FUNCTION__))
;
408#endif
409 PHs.front().replaceUseWith(MD);
410 PHs.pop_front();
411 }
412}
413
414} // anonymous namespace
415
416static Error error(const Twine &Message) {
417 return make_error<StringError>(
418 Message, make_error_code(BitcodeError::CorruptedBitcode));
419}
420
421class MetadataLoader::MetadataLoaderImpl {
422 BitcodeReaderMetadataList MetadataList;
423 BitcodeReaderValueList &ValueList;
424 BitstreamCursor &Stream;
425 LLVMContext &Context;
426 Module &TheModule;
427 std::function<Type *(unsigned)> getTypeByID;
428
429 /// Cursor associated with the lazy-loading of Metadata. This is the easy way
430 /// to keep around the right "context" (Abbrev list) to be able to jump in
431 /// the middle of the metadata block and load any record.
432 BitstreamCursor IndexCursor;
433
434 /// Index that keeps track of MDString values.
435 std::vector<StringRef> MDStringRef;
436
437 /// On-demand loading of a single MDString. Requires the index above to be
438 /// populated.
439 MDString *lazyLoadOneMDString(unsigned Idx);
440
441 /// Index that keeps track of where to find a metadata record in the stream.
442 std::vector<uint64_t> GlobalMetadataBitPosIndex;
443
444 /// Populate the index above to enable lazily loading of metadata, and load
445 /// the named metadata as well as the transitively referenced global
446 /// Metadata.
447 Expected<bool> lazyLoadModuleMetadataBlock();
448
449 /// On-demand loading of a single metadata. Requires the index above to be
450 /// populated.
451 void lazyLoadOneMetadata(unsigned Idx, PlaceholderQueue &Placeholders);
452
453 // Keep mapping of seens pair of old-style CU <-> SP, and update pointers to
454 // point from SP to CU after a block is completly parsed.
455 std::vector<std::pair<DICompileUnit *, Metadata *>> CUSubprograms;
456
457 /// Functions that need to be matched with subprograms when upgrading old
458 /// metadata.
459 SmallDenseMap<Function *, DISubprogram *, 16> FunctionsWithSPs;
460
461 // Map the bitcode's custom MDKind ID to the Module's MDKind ID.
462 DenseMap<unsigned, unsigned> MDKindMap;
463
464 bool StripTBAA = false;
465 bool HasSeenOldLoopTags = false;
466 bool NeedUpgradeToDIGlobalVariableExpression = false;
467 bool NeedDeclareExpressionUpgrade = false;
468
469 /// True if metadata is being parsed for a module being ThinLTO imported.
470 bool IsImporting = false;
471
472 Error parseOneMetadata(SmallVectorImpl<uint64_t> &Record, unsigned Code,
473 PlaceholderQueue &Placeholders, StringRef Blob,
474 unsigned &NextMetadataNo);
475 Error parseMetadataStrings(ArrayRef<uint64_t> Record, StringRef Blob,
476 function_ref<void(StringRef)> CallBack);
477 Error parseGlobalObjectAttachment(GlobalObject &GO,
478 ArrayRef<uint64_t> Record);
479 Error parseMetadataKindRecord(SmallVectorImpl<uint64_t> &Record);
480
481 void resolveForwardRefsAndPlaceholders(PlaceholderQueue &Placeholders);
482
483 /// Upgrade old-style CU <-> SP pointers to point from SP to CU.
484 void upgradeCUSubprograms() {
485 for (auto CU_SP : CUSubprograms)
486 if (auto *SPs = dyn_cast_or_null<MDTuple>(CU_SP.second))
487 for (auto &Op : SPs->operands())
488 if (auto *SP = dyn_cast_or_null<DISubprogram>(Op))
489 SP->replaceUnit(CU_SP.first);
490 CUSubprograms.clear();
491 }
492
493 /// Upgrade old-style bare DIGlobalVariables to DIGlobalVariableExpressions.
494 void upgradeCUVariables() {
495 if (!NeedUpgradeToDIGlobalVariableExpression)
496 return;
497
498 // Upgrade list of variables attached to the CUs.
499 if (NamedMDNode *CUNodes = TheModule.getNamedMetadata("llvm.dbg.cu"))
500 for (unsigned I = 0, E = CUNodes->getNumOperands(); I != E; ++I) {
501 auto *CU = cast<DICompileUnit>(CUNodes->getOperand(I));
502 if (auto *GVs = dyn_cast_or_null<MDTuple>(CU->getRawGlobalVariables()))
503 for (unsigned I = 0; I < GVs->getNumOperands(); I++)
504 if (auto *GV =
505 dyn_cast_or_null<DIGlobalVariable>(GVs->getOperand(I))) {
506 auto *DGVE = DIGlobalVariableExpression::getDistinct(
507 Context, GV, DIExpression::get(Context, {}));
508 GVs->replaceOperandWith(I, DGVE);
509 }
510 }
511
512 // Upgrade variables attached to globals.
513 for (auto &GV : TheModule.globals()) {
514 SmallVector<MDNode *, 1> MDs;
515 GV.getMetadata(LLVMContext::MD_dbg, MDs);
516 GV.eraseMetadata(LLVMContext::MD_dbg);
517 for (auto *MD : MDs)
518 if (auto *DGV = dyn_cast<DIGlobalVariable>(MD)) {
519 auto *DGVE = DIGlobalVariableExpression::getDistinct(
520 Context, DGV, DIExpression::get(Context, {}));
521 GV.addMetadata(LLVMContext::MD_dbg, *DGVE);
522 } else
523 GV.addMetadata(LLVMContext::MD_dbg, *MD);
524 }
525 }
526
527 /// Remove a leading DW_OP_deref from DIExpressions in a dbg.declare that
528 /// describes a function argument.
529 void upgradeDeclareExpressions(Function &F) {
530 if (!NeedDeclareExpressionUpgrade)
531 return;
532
533 for (auto &BB : F)
534 for (auto &I : BB)
535 if (auto *DDI = dyn_cast<DbgDeclareInst>(&I))
536 if (auto *DIExpr = DDI->getExpression())
537 if (DIExpr->startsWithDeref() &&
538 dyn_cast_or_null<Argument>(DDI->getAddress())) {
539 SmallVector<uint64_t, 8> Ops;
540 Ops.append(std::next(DIExpr->elements_begin()),
541 DIExpr->elements_end());
542 auto *E = DIExpression::get(Context, Ops);
543 DDI->setOperand(2, MetadataAsValue::get(Context, E));
544 }
545 }
546
547 /// Upgrade the expression from previous versions.
548 Error upgradeDIExpression(uint64_t FromVersion,
549 MutableArrayRef<uint64_t> &Expr,
550 SmallVectorImpl<uint64_t> &Buffer) {
551 auto N = Expr.size();
552 switch (FromVersion) {
553 default:
554 return error("Invalid record");
555 case 0:
556 if (N >= 3 && Expr[N - 3] == dwarf::DW_OP_bit_piece)
557 Expr[N - 3] = dwarf::DW_OP_LLVM_fragment;
558 LLVM_FALLTHROUGH[[gnu::fallthrough]];
559 case 1:
560 // Move DW_OP_deref to the end.
561 if (N && Expr[0] == dwarf::DW_OP_deref) {
562 auto End = Expr.end();
563 if (Expr.size() >= 3 &&
564 *std::prev(End, 3) == dwarf::DW_OP_LLVM_fragment)
565 End = std::prev(End, 3);
566 std::move(std::next(Expr.begin()), End, Expr.begin());
567 *std::prev(End) = dwarf::DW_OP_deref;
568 }
569 NeedDeclareExpressionUpgrade = true;
570 LLVM_FALLTHROUGH[[gnu::fallthrough]];
571 case 2: {
572 // Change DW_OP_plus to DW_OP_plus_uconst.
573 // Change DW_OP_minus to DW_OP_uconst, DW_OP_minus
574 auto SubExpr = ArrayRef<uint64_t>(Expr);
575 while (!SubExpr.empty()) {
576 // Skip past other operators with their operands
577 // for this version of the IR, obtained from
578 // from historic DIExpression::ExprOperand::getSize().
579 size_t HistoricSize;
580 switch (SubExpr.front()) {
581 default:
582 HistoricSize = 1;
583 break;
584 case dwarf::DW_OP_constu:
585 case dwarf::DW_OP_minus:
586 case dwarf::DW_OP_plus:
587 HistoricSize = 2;
588 break;
589 case dwarf::DW_OP_LLVM_fragment:
590 HistoricSize = 3;
591 break;
592 }
593
594 // If the expression is malformed, make sure we don't
595 // copy more elements than we should.
596 HistoricSize = std::min(SubExpr.size(), HistoricSize);
597 ArrayRef<uint64_t> Args = SubExpr.slice(1, HistoricSize-1);
598
599 switch (SubExpr.front()) {
600 case dwarf::DW_OP_plus:
601 Buffer.push_back(dwarf::DW_OP_plus_uconst);
602 Buffer.append(Args.begin(), Args.end());
603 break;
604 case dwarf::DW_OP_minus:
605 Buffer.push_back(dwarf::DW_OP_constu);
606 Buffer.append(Args.begin(), Args.end());
607 Buffer.push_back(dwarf::DW_OP_minus);
608 break;
609 default:
610 Buffer.push_back(*SubExpr.begin());
611 Buffer.append(Args.begin(), Args.end());
612 break;
613 }
614
615 // Continue with remaining elements.
616 SubExpr = SubExpr.slice(HistoricSize);
617 }
618 Expr = MutableArrayRef<uint64_t>(Buffer);
619 LLVM_FALLTHROUGH[[gnu::fallthrough]];
620 }
621 case 3:
622 // Up-to-date!
623 break;
624 }
625
626 return Error::success();
627 }
628
629 void upgradeDebugInfo() {
630 upgradeCUSubprograms();
631 upgradeCUVariables();
632 }
633
634public:
635 MetadataLoaderImpl(BitstreamCursor &Stream, Module &TheModule,
636 BitcodeReaderValueList &ValueList,
637 std::function<Type *(unsigned)> getTypeByID,
638 bool IsImporting)
639 : MetadataList(TheModule.getContext(), Stream.SizeInBytes()),
640 ValueList(ValueList), Stream(Stream), Context(TheModule.getContext()),
641 TheModule(TheModule), getTypeByID(std::move(getTypeByID)),
642 IsImporting(IsImporting) {}
643
644 Error parseMetadata(bool ModuleLevel);
645
646 bool hasFwdRefs() const { return MetadataList.hasFwdRefs(); }
647
648 Metadata *getMetadataFwdRefOrLoad(unsigned ID) {
649 if (ID < MDStringRef.size())
650 return lazyLoadOneMDString(ID);
651 if (auto *MD = MetadataList.lookup(ID))
652 return MD;
653 // If lazy-loading is enabled, we try recursively to load the operand
654 // instead of creating a temporary.
655 if (ID < (MDStringRef.size() + GlobalMetadataBitPosIndex.size())) {
656 PlaceholderQueue Placeholders;
657 lazyLoadOneMetadata(ID, Placeholders);
658 resolveForwardRefsAndPlaceholders(Placeholders);
659 return MetadataList.lookup(ID);
660 }
661 return MetadataList.getMetadataFwdRef(ID);
662 }
663
664 DISubprogram *lookupSubprogramForFunction(Function *F) {
665 return FunctionsWithSPs.lookup(F);
666 }
667
668 bool hasSeenOldLoopTags() { return HasSeenOldLoopTags; }
669
670 Error parseMetadataAttachment(
671 Function &F, const SmallVectorImpl<Instruction *> &InstructionList);
672
673 Error parseMetadataKinds();
674
675 void setStripTBAA(bool Value) { StripTBAA = Value; }
676 bool isStrippingTBAA() { return StripTBAA; }
677
678 unsigned size() const { return MetadataList.size(); }
679 void shrinkTo(unsigned N) { MetadataList.shrinkTo(N); }
680 void upgradeDebugIntrinsics(Function &F) { upgradeDeclareExpressions(F); }
681};
682
683Expected<bool>
684MetadataLoader::MetadataLoaderImpl::lazyLoadModuleMetadataBlock() {
685 IndexCursor = Stream;
686 SmallVector<uint64_t, 64> Record;
687 // Get the abbrevs, and preload record positions to make them lazy-loadable.
688 while (true) {
689 Expected<BitstreamEntry> MaybeEntry = IndexCursor.advanceSkippingSubblocks(
690 BitstreamCursor::AF_DontPopBlockAtEnd);
691 if (!MaybeEntry)
692 return MaybeEntry.takeError();
693 BitstreamEntry Entry = MaybeEntry.get();
694
695 switch (Entry.Kind) {
696 case BitstreamEntry::SubBlock: // Handled for us already.
697 case BitstreamEntry::Error:
698 return error("Malformed block");
699 case BitstreamEntry::EndBlock: {
700 return true;
701 }
702 case BitstreamEntry::Record: {
703 // The interesting case.
704 ++NumMDRecordLoaded;
705 uint64_t CurrentPos = IndexCursor.GetCurrentBitNo();
706 Expected<unsigned> MaybeCode = IndexCursor.skipRecord(Entry.ID);
707 if (!MaybeCode)
708 return MaybeCode.takeError();
709 unsigned Code = MaybeCode.get();
710 switch (Code) {
711 case bitc::METADATA_STRINGS: {
712 // Rewind and parse the strings.
713 if (Error Err = IndexCursor.JumpToBit(CurrentPos))
714 return std::move(Err);
715 StringRef Blob;
716 Record.clear();
717 if (Expected<unsigned> MaybeRecord =
718 IndexCursor.readRecord(Entry.ID, Record, &Blob))
719 ;
720 else
721 return MaybeRecord.takeError();
722 unsigned NumStrings = Record[0];
723 MDStringRef.reserve(NumStrings);
724 auto IndexNextMDString = [&](StringRef Str) {
725 MDStringRef.push_back(Str);
726 };
727 if (auto Err = parseMetadataStrings(Record, Blob, IndexNextMDString))
728 return std::move(Err);
729 break;
730 }
731 case bitc::METADATA_INDEX_OFFSET: {
732 // This is the offset to the index, when we see this we skip all the
733 // records and load only an index to these.
734 if (Error Err = IndexCursor.JumpToBit(CurrentPos))
735 return std::move(Err);
736 Record.clear();
737 if (Expected<unsigned> MaybeRecord =
738 IndexCursor.readRecord(Entry.ID, Record))
739 ;
740 else
741 return MaybeRecord.takeError();
742 if (Record.size() != 2)
743 return error("Invalid record");
744 auto Offset = Record[0] + (Record[1] << 32);
745 auto BeginPos = IndexCursor.GetCurrentBitNo();
746 if (Error Err = IndexCursor.JumpToBit(BeginPos + Offset))
747 return std::move(Err);
748 Expected<BitstreamEntry> MaybeEntry =
749 IndexCursor.advanceSkippingSubblocks(
750 BitstreamCursor::AF_DontPopBlockAtEnd);
751 if (!MaybeEntry)
752 return MaybeEntry.takeError();
753 Entry = MaybeEntry.get();
754 assert(Entry.Kind == BitstreamEntry::Record &&((Entry.Kind == BitstreamEntry::Record && "Corrupted bitcode: Expected `Record` when trying to find the "
"Metadata index") ? static_cast<void> (0) : __assert_fail
("Entry.Kind == BitstreamEntry::Record && \"Corrupted bitcode: Expected `Record` when trying to find the \" \"Metadata index\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/Bitcode/Reader/MetadataLoader.cpp"
, 756, __PRETTY_FUNCTION__))
755 "Corrupted bitcode: Expected `Record` when trying to find the "((Entry.Kind == BitstreamEntry::Record && "Corrupted bitcode: Expected `Record` when trying to find the "
"Metadata index") ? static_cast<void> (0) : __assert_fail
("Entry.Kind == BitstreamEntry::Record && \"Corrupted bitcode: Expected `Record` when trying to find the \" \"Metadata index\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/Bitcode/Reader/MetadataLoader.cpp"
, 756, __PRETTY_FUNCTION__))
756 "Metadata index")((Entry.Kind == BitstreamEntry::Record && "Corrupted bitcode: Expected `Record` when trying to find the "
"Metadata index") ? static_cast<void> (0) : __assert_fail
("Entry.Kind == BitstreamEntry::Record && \"Corrupted bitcode: Expected `Record` when trying to find the \" \"Metadata index\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/Bitcode/Reader/MetadataLoader.cpp"
, 756, __PRETTY_FUNCTION__))
;
757 Record.clear();
758 if (Expected<unsigned> MaybeCode =
759 IndexCursor.readRecord(Entry.ID, Record))
760 assert(MaybeCode.get() == bitc::METADATA_INDEX &&((MaybeCode.get() == bitc::METADATA_INDEX && "Corrupted bitcode: Expected `METADATA_INDEX` when trying to "
"find the Metadata index") ? static_cast<void> (0) : __assert_fail
("MaybeCode.get() == bitc::METADATA_INDEX && \"Corrupted bitcode: Expected `METADATA_INDEX` when trying to \" \"find the Metadata index\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/Bitcode/Reader/MetadataLoader.cpp"
, 762, __PRETTY_FUNCTION__))
761 "Corrupted bitcode: Expected `METADATA_INDEX` when trying to "((MaybeCode.get() == bitc::METADATA_INDEX && "Corrupted bitcode: Expected `METADATA_INDEX` when trying to "
"find the Metadata index") ? static_cast<void> (0) : __assert_fail
("MaybeCode.get() == bitc::METADATA_INDEX && \"Corrupted bitcode: Expected `METADATA_INDEX` when trying to \" \"find the Metadata index\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/Bitcode/Reader/MetadataLoader.cpp"
, 762, __PRETTY_FUNCTION__))
762 "find the Metadata index")((MaybeCode.get() == bitc::METADATA_INDEX && "Corrupted bitcode: Expected `METADATA_INDEX` when trying to "
"find the Metadata index") ? static_cast<void> (0) : __assert_fail
("MaybeCode.get() == bitc::METADATA_INDEX && \"Corrupted bitcode: Expected `METADATA_INDEX` when trying to \" \"find the Metadata index\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/Bitcode/Reader/MetadataLoader.cpp"
, 762, __PRETTY_FUNCTION__))
;
763 else
764 return MaybeCode.takeError();
765 // Delta unpack
766 auto CurrentValue = BeginPos;
767 GlobalMetadataBitPosIndex.reserve(Record.size());
768 for (auto &Elt : Record) {
769 CurrentValue += Elt;
770 GlobalMetadataBitPosIndex.push_back(CurrentValue);
771 }
772 break;
773 }
774 case bitc::METADATA_INDEX:
775 // We don't expect to get there, the Index is loaded when we encounter
776 // the offset.
777 return error("Corrupted Metadata block");
778 case bitc::METADATA_NAME: {
779 // Named metadata need to be materialized now and aren't deferred.
780 if (Error Err = IndexCursor.JumpToBit(CurrentPos))
781 return std::move(Err);
782 Record.clear();
783
784 unsigned Code;
785 if (Expected<unsigned> MaybeCode =
786 IndexCursor.readRecord(Entry.ID, Record)) {
787 Code = MaybeCode.get();
788 assert(Code == bitc::METADATA_NAME)((Code == bitc::METADATA_NAME) ? static_cast<void> (0) :
__assert_fail ("Code == bitc::METADATA_NAME", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/Bitcode/Reader/MetadataLoader.cpp"
, 788, __PRETTY_FUNCTION__))
;
789 } else
790 return MaybeCode.takeError();
791
792 // Read name of the named metadata.
793 SmallString<8> Name(Record.begin(), Record.end());
794 if (Expected<unsigned> MaybeCode = IndexCursor.ReadCode())
795 Code = MaybeCode.get();
796 else
797 return MaybeCode.takeError();
798
799 // Named Metadata comes in two parts, we expect the name to be followed
800 // by the node
801 Record.clear();
802 if (Expected<unsigned> MaybeNextBitCode =
803 IndexCursor.readRecord(Code, Record))
804 assert(MaybeNextBitCode.get() == bitc::METADATA_NAMED_NODE)((MaybeNextBitCode.get() == bitc::METADATA_NAMED_NODE) ? static_cast
<void> (0) : __assert_fail ("MaybeNextBitCode.get() == bitc::METADATA_NAMED_NODE"
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/Bitcode/Reader/MetadataLoader.cpp"
, 804, __PRETTY_FUNCTION__))
;
805 else
806 return MaybeNextBitCode.takeError();
807
808 // Read named metadata elements.
809 unsigned Size = Record.size();
810 NamedMDNode *NMD = TheModule.getOrInsertNamedMetadata(Name);
811 for (unsigned i = 0; i != Size; ++i) {
812 // FIXME: We could use a placeholder here, however NamedMDNode are
813 // taking MDNode as operand and not using the Metadata infrastructure.
814 // It is acknowledged by 'TODO: Inherit from Metadata' in the
815 // NamedMDNode class definition.
816 MDNode *MD = MetadataList.getMDNodeFwdRefOrNull(Record[i]);
817 assert(MD && "Invalid metadata: expect fwd ref to MDNode")((MD && "Invalid metadata: expect fwd ref to MDNode")
? static_cast<void> (0) : __assert_fail ("MD && \"Invalid metadata: expect fwd ref to MDNode\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/Bitcode/Reader/MetadataLoader.cpp"
, 817, __PRETTY_FUNCTION__))
;
818 NMD->addOperand(MD);
819 }
820 break;
821 }
822 case bitc::METADATA_GLOBAL_DECL_ATTACHMENT: {
823 // FIXME: we need to do this early because we don't materialize global
824 // value explicitly.
825 if (Error Err = IndexCursor.JumpToBit(CurrentPos))
826 return std::move(Err);
827 Record.clear();
828 if (Expected<unsigned> MaybeRecord =
829 IndexCursor.readRecord(Entry.ID, Record))
830 ;
831 else
832 return MaybeRecord.takeError();
833 if (Record.size() % 2 == 0)
834 return error("Invalid record");
835 unsigned ValueID = Record[0];
836 if (ValueID >= ValueList.size())
837 return error("Invalid record");
838 if (auto *GO = dyn_cast<GlobalObject>(ValueList[ValueID]))
839 if (Error Err = parseGlobalObjectAttachment(
840 *GO, ArrayRef<uint64_t>(Record).slice(1)))
841 return std::move(Err);
842 break;
843 }
844 case bitc::METADATA_KIND:
845 case bitc::METADATA_STRING_OLD:
846 case bitc::METADATA_OLD_FN_NODE:
847 case bitc::METADATA_OLD_NODE:
848 case bitc::METADATA_VALUE:
849 case bitc::METADATA_DISTINCT_NODE:
850 case bitc::METADATA_NODE:
851 case bitc::METADATA_LOCATION:
852 case bitc::METADATA_GENERIC_DEBUG:
853 case bitc::METADATA_SUBRANGE:
854 case bitc::METADATA_ENUMERATOR:
855 case bitc::METADATA_BASIC_TYPE:
856 case bitc::METADATA_DERIVED_TYPE:
857 case bitc::METADATA_COMPOSITE_TYPE:
858 case bitc::METADATA_SUBROUTINE_TYPE:
859 case bitc::METADATA_MODULE:
860 case bitc::METADATA_FILE:
861 case bitc::METADATA_COMPILE_UNIT:
862 case bitc::METADATA_SUBPROGRAM:
863 case bitc::METADATA_LEXICAL_BLOCK:
864 case bitc::METADATA_LEXICAL_BLOCK_FILE:
865 case bitc::METADATA_NAMESPACE:
866 case bitc::METADATA_COMMON_BLOCK:
867 case bitc::METADATA_MACRO:
868 case bitc::METADATA_MACRO_FILE:
869 case bitc::METADATA_TEMPLATE_TYPE:
870 case bitc::METADATA_TEMPLATE_VALUE:
871 case bitc::METADATA_GLOBAL_VAR:
872 case bitc::METADATA_LOCAL_VAR:
873 case bitc::METADATA_LABEL:
874 case bitc::METADATA_EXPRESSION:
875 case bitc::METADATA_OBJC_PROPERTY:
876 case bitc::METADATA_IMPORTED_ENTITY:
877 case bitc::METADATA_GLOBAL_VAR_EXPR:
878 // We don't expect to see any of these, if we see one, give up on
879 // lazy-loading and fallback.
880 MDStringRef.clear();
881 GlobalMetadataBitPosIndex.clear();
882 return false;
883 }
884 break;
885 }
886 }
887 }
888}
889
890/// Parse a METADATA_BLOCK. If ModuleLevel is true then we are parsing
891/// module level metadata.
892Error MetadataLoader::MetadataLoaderImpl::parseMetadata(bool ModuleLevel) {
893 if (!ModuleLevel && MetadataList.hasFwdRefs())
894 return error("Invalid metadata: fwd refs into function blocks");
895
896 // Record the entry position so that we can jump back here and efficiently
897 // skip the whole block in case we lazy-load.
898 auto EntryPos = Stream.GetCurrentBitNo();
899
900 if (Error Err = Stream.EnterSubBlock(bitc::METADATA_BLOCK_ID))
901 return Err;
902
903 SmallVector<uint64_t, 64> Record;
904 PlaceholderQueue Placeholders;
905
906 // We lazy-load module-level metadata: we build an index for each record, and
907 // then load individual record as needed, starting with the named metadata.
908 if (ModuleLevel && IsImporting && MetadataList.empty() &&
909 !DisableLazyLoading) {
910 auto SuccessOrErr = lazyLoadModuleMetadataBlock();
911 if (!SuccessOrErr)
912 return SuccessOrErr.takeError();
913 if (SuccessOrErr.get()) {
914 // An index was successfully created and we will be able to load metadata
915 // on-demand.
916 MetadataList.resize(MDStringRef.size() +
917 GlobalMetadataBitPosIndex.size());
918
919 // Reading the named metadata created forward references and/or
920 // placeholders, that we flush here.
921 resolveForwardRefsAndPlaceholders(Placeholders);
922 upgradeDebugInfo();
923 // Return at the beginning of the block, since it is easy to skip it
924 // entirely from there.
925 Stream.ReadBlockEnd(); // Pop the abbrev block context.
926 if (Error Err = IndexCursor.JumpToBit(EntryPos))
927 return Err;
928 if (Error Err = Stream.SkipBlock()) {
929 // FIXME this drops the error on the floor, which
930 // ThinLTO/X86/debuginfo-cu-import.ll relies on.
931 consumeError(std::move(Err));
932 return Error::success();
933 }
934 return Error::success();
935 }
936 // Couldn't load an index, fallback to loading all the block "old-style".
937 }
938
939 unsigned NextMetadataNo = MetadataList.size();
940
941 // Read all the records.
942 while (true) {
943 Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks();
944 if (!MaybeEntry)
945 return MaybeEntry.takeError();
946 BitstreamEntry Entry = MaybeEntry.get();
947
948 switch (Entry.Kind) {
949 case BitstreamEntry::SubBlock: // Handled for us already.
950 case BitstreamEntry::Error:
951 return error("Malformed block");
952 case BitstreamEntry::EndBlock:
953 resolveForwardRefsAndPlaceholders(Placeholders);
954 upgradeDebugInfo();
955 return Error::success();
956 case BitstreamEntry::Record:
957 // The interesting case.
958 break;
959 }
960
961 // Read a record.
962 Record.clear();
963 StringRef Blob;
964 ++NumMDRecordLoaded;
965 if (Expected<unsigned> MaybeCode =
966 Stream.readRecord(Entry.ID, Record, &Blob)) {
967 if (Error Err = parseOneMetadata(Record, MaybeCode.get(), Placeholders,
968 Blob, NextMetadataNo))
969 return Err;
970 } else
971 return MaybeCode.takeError();
972 }
973}
974
975MDString *MetadataLoader::MetadataLoaderImpl::lazyLoadOneMDString(unsigned ID) {
976 ++NumMDStringLoaded;
977 if (Metadata *MD = MetadataList.lookup(ID))
978 return cast<MDString>(MD);
979 auto MDS = MDString::get(Context, MDStringRef[ID]);
980 MetadataList.assignValue(MDS, ID);
981 return MDS;
982}
983
984void MetadataLoader::MetadataLoaderImpl::lazyLoadOneMetadata(
985 unsigned ID, PlaceholderQueue &Placeholders) {
986 assert(ID < (MDStringRef.size()) + GlobalMetadataBitPosIndex.size())((ID < (MDStringRef.size()) + GlobalMetadataBitPosIndex.size
()) ? static_cast<void> (0) : __assert_fail ("ID < (MDStringRef.size()) + GlobalMetadataBitPosIndex.size()"
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/Bitcode/Reader/MetadataLoader.cpp"
, 986, __PRETTY_FUNCTION__))
;
987 assert(ID >= MDStringRef.size() && "Unexpected lazy-loading of MDString")((ID >= MDStringRef.size() && "Unexpected lazy-loading of MDString"
) ? static_cast<void> (0) : __assert_fail ("ID >= MDStringRef.size() && \"Unexpected lazy-loading of MDString\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/Bitcode/Reader/MetadataLoader.cpp"
, 987, __PRETTY_FUNCTION__))
;
988 // Lookup first if the metadata hasn't already been loaded.
989 if (auto *MD = MetadataList.lookup(ID)) {
990 auto *N = cast<MDNode>(MD);
991 if (!N->isTemporary())
992 return;
993 }
994 SmallVector<uint64_t, 64> Record;
995 StringRef Blob;
996 if (Error Err = IndexCursor.JumpToBit(
997 GlobalMetadataBitPosIndex[ID - MDStringRef.size()]))
998 report_fatal_error("lazyLoadOneMetadata failed jumping: " +
999 toString(std::move(Err)));
1000 Expected<BitstreamEntry> MaybeEntry = IndexCursor.advanceSkippingSubblocks();
1001 if (!MaybeEntry)
1002 // FIXME this drops the error on the floor.
1003 report_fatal_error("lazyLoadOneMetadata failed advanceSkippingSubblocks: " +
1004 toString(MaybeEntry.takeError()));
1005 BitstreamEntry Entry = MaybeEntry.get();
1006 ++NumMDRecordLoaded;
1007 if (Expected<unsigned> MaybeCode =
1008 IndexCursor.readRecord(Entry.ID, Record, &Blob)) {
1009 if (Error Err =
1010 parseOneMetadata(Record, MaybeCode.get(), Placeholders, Blob, ID))
1011 report_fatal_error("Can't lazyload MD, parseOneMetadata: " +
1012 toString(std::move(Err)));
1013 } else
1014 report_fatal_error("Can't lazyload MD: " + toString(MaybeCode.takeError()));
1015}
1016
1017/// Ensure that all forward-references and placeholders are resolved.
1018/// Iteratively lazy-loading metadata on-demand if needed.
1019void MetadataLoader::MetadataLoaderImpl::resolveForwardRefsAndPlaceholders(
1020 PlaceholderQueue &Placeholders) {
1021 DenseSet<unsigned> Temporaries;
1022 while (1) {
1023 // Populate Temporaries with the placeholders that haven't been loaded yet.
1024 Placeholders.getTemporaries(MetadataList, Temporaries);
1025
1026 // If we don't have any temporary, or FwdReference, we're done!
1027 if (Temporaries.empty() && !MetadataList.hasFwdRefs())
1028 break;
1029
1030 // First, load all the temporaries. This can add new placeholders or
1031 // forward references.
1032 for (auto ID : Temporaries)
1033 lazyLoadOneMetadata(ID, Placeholders);
1034 Temporaries.clear();
1035
1036 // Second, load the forward-references. This can also add new placeholders
1037 // or forward references.
1038 while (MetadataList.hasFwdRefs())
1039 lazyLoadOneMetadata(MetadataList.getNextFwdRef(), Placeholders);
1040 }
1041 // At this point we don't have any forward reference remaining, or temporary
1042 // that haven't been loaded. We can safely drop RAUW support and mark cycles
1043 // as resolved.
1044 MetadataList.tryToResolveCycles();
1045
1046 // Finally, everything is in place, we can replace the placeholders operands
1047 // with the final node they refer to.
1048 Placeholders.flush(MetadataList);
1049}
1050
1051Error MetadataLoader::MetadataLoaderImpl::parseOneMetadata(
1052 SmallVectorImpl<uint64_t> &Record, unsigned Code,
1053 PlaceholderQueue &Placeholders, StringRef Blob, unsigned &NextMetadataNo) {
1054
1055 bool IsDistinct = false;
1056 auto getMD = [&](unsigned ID) -> Metadata * {
1057 if (ID < MDStringRef.size())
1058 return lazyLoadOneMDString(ID);
1059 if (!IsDistinct) {
1060 if (auto *MD = MetadataList.lookup(ID))
1061 return MD;
1062 // If lazy-loading is enabled, we try recursively to load the operand
1063 // instead of creating a temporary.
1064 if (ID < (MDStringRef.size() + GlobalMetadataBitPosIndex.size())) {
1065 // Create a temporary for the node that is referencing the operand we
1066 // will lazy-load. It is needed before recursing in case there are
1067 // uniquing cycles.
1068 MetadataList.getMetadataFwdRef(NextMetadataNo);
1069 lazyLoadOneMetadata(ID, Placeholders);
1070 return MetadataList.lookup(ID);
1071 }
1072 // Return a temporary.
1073 return MetadataList.getMetadataFwdRef(ID);
1074 }
1075 if (auto *MD = MetadataList.getMetadataIfResolved(ID))
1076 return MD;
1077 return &Placeholders.getPlaceholderOp(ID);
1078 };
1079 auto getMDOrNull = [&](unsigned ID) -> Metadata * {
1080 if (ID)
1081 return getMD(ID - 1);
1082 return nullptr;
1083 };
1084 auto getMDOrNullWithoutPlaceholders = [&](unsigned ID) -> Metadata * {
1085 if (ID)
1086 return MetadataList.getMetadataFwdRef(ID - 1);
1087 return nullptr;
1088 };
1089 auto getMDString = [&](unsigned ID) -> MDString * {
1090 // This requires that the ID is not really a forward reference. In
1091 // particular, the MDString must already have been resolved.
1092 auto MDS = getMDOrNull(ID);
1093 return cast_or_null<MDString>(MDS);
1094 };
1095
1096 // Support for old type refs.
1097 auto getDITypeRefOrNull = [&](unsigned ID) {
1098 return MetadataList.upgradeTypeRef(getMDOrNull(ID));
1099 };
1100
1101#define GET_OR_DISTINCT(CLASS, ARGS) \
1102 (IsDistinct ? CLASS::getDistinct ARGS : CLASS::get ARGS)
1103
1104 switch (Code) {
1105 default: // Default behavior: ignore.
1106 break;
1107 case bitc::METADATA_NAME: {
1108 // Read name of the named metadata.
1109 SmallString<8> Name(Record.begin(), Record.end());
1110 Record.clear();
1111 Expected<unsigned> MaybeCode = Stream.ReadCode();
1112 if (!MaybeCode)
1113 return MaybeCode.takeError();
1114 Code = MaybeCode.get();
1115
1116 ++NumMDRecordLoaded;
1117 if (Expected<unsigned> MaybeNextBitCode = Stream.readRecord(Code, Record)) {
1118 if (MaybeNextBitCode.get() != bitc::METADATA_NAMED_NODE)
1119 return error("METADATA_NAME not followed by METADATA_NAMED_NODE");
1120 } else
1121 return MaybeNextBitCode.takeError();
1122
1123 // Read named metadata elements.
1124 unsigned Size = Record.size();
1125 NamedMDNode *NMD = TheModule.getOrInsertNamedMetadata(Name);
1126 for (unsigned i = 0; i != Size; ++i) {
1127 MDNode *MD = MetadataList.getMDNodeFwdRefOrNull(Record[i]);
1128 if (!MD)
1129 return error("Invalid named metadata: expect fwd ref to MDNode");
1130 NMD->addOperand(MD);
1131 }
1132 break;
1133 }
1134 case bitc::METADATA_OLD_FN_NODE: {
1135 // FIXME: Remove in 4.0.
1136 // This is a LocalAsMetadata record, the only type of function-local
1137 // metadata.
1138 if (Record.size() % 2 == 1)
1139 return error("Invalid record");
1140
1141 // If this isn't a LocalAsMetadata record, we're dropping it. This used
1142 // to be legal, but there's no upgrade path.
1143 auto dropRecord = [&] {
1144 MetadataList.assignValue(MDNode::get(Context, None), NextMetadataNo);
1145 NextMetadataNo++;
1146 };
1147 if (Record.size() != 2) {
1148 dropRecord();
1149 break;
1150 }
1151
1152 Type *Ty = getTypeByID(Record[0]);
1153 if (Ty->isMetadataTy() || Ty->isVoidTy()) {
1154 dropRecord();
1155 break;
1156 }
1157
1158 MetadataList.assignValue(
1159 LocalAsMetadata::get(ValueList.getValueFwdRef(Record[1], Ty)),
1160 NextMetadataNo);
1161 NextMetadataNo++;
1162 break;
1163 }
1164 case bitc::METADATA_OLD_NODE: {
1165 // FIXME: Remove in 4.0.
1166 if (Record.size() % 2 == 1)
1167 return error("Invalid record");
1168
1169 unsigned Size = Record.size();
1170 SmallVector<Metadata *, 8> Elts;
1171 for (unsigned i = 0; i != Size; i += 2) {
1172 Type *Ty = getTypeByID(Record[i]);
1173 if (!Ty)
1174 return error("Invalid record");
1175 if (Ty->isMetadataTy())
1176 Elts.push_back(getMD(Record[i + 1]));
1177 else if (!Ty->isVoidTy()) {
1178 auto *MD =
1179 ValueAsMetadata::get(ValueList.getValueFwdRef(Record[i + 1], Ty));
1180 assert(isa<ConstantAsMetadata>(MD) &&((isa<ConstantAsMetadata>(MD) && "Expected non-function-local metadata"
) ? static_cast<void> (0) : __assert_fail ("isa<ConstantAsMetadata>(MD) && \"Expected non-function-local metadata\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/Bitcode/Reader/MetadataLoader.cpp"
, 1181, __PRETTY_FUNCTION__))
1181 "Expected non-function-local metadata")((isa<ConstantAsMetadata>(MD) && "Expected non-function-local metadata"
) ? static_cast<void> (0) : __assert_fail ("isa<ConstantAsMetadata>(MD) && \"Expected non-function-local metadata\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/Bitcode/Reader/MetadataLoader.cpp"
, 1181, __PRETTY_FUNCTION__))
;
1182 Elts.push_back(MD);
1183 } else
1184 Elts.push_back(nullptr);
1185 }
1186 MetadataList.assignValue(MDNode::get(Context, Elts), NextMetadataNo);
1187 NextMetadataNo++;
1188 break;
1189 }
1190 case bitc::METADATA_VALUE: {
1191 if (Record.size() != 2)
1192 return error("Invalid record");
1193
1194 Type *Ty = getTypeByID(Record[0]);
1195 if (Ty->isMetadataTy() || Ty->isVoidTy())
1196 return error("Invalid record");
1197
1198 MetadataList.assignValue(
1199 ValueAsMetadata::get(ValueList.getValueFwdRef(Record[1], Ty)),
1200 NextMetadataNo);
1201 NextMetadataNo++;
1202 break;
1203 }
1204 case bitc::METADATA_DISTINCT_NODE:
1205 IsDistinct = true;
1206 LLVM_FALLTHROUGH[[gnu::fallthrough]];
1207 case bitc::METADATA_NODE: {
1208 SmallVector<Metadata *, 8> Elts;
1209 Elts.reserve(Record.size());
1210 for (unsigned ID : Record)
1211 Elts.push_back(getMDOrNull(ID));
1212 MetadataList.assignValue(IsDistinct ? MDNode::getDistinct(Context, Elts)
1213 : MDNode::get(Context, Elts),
1214 NextMetadataNo);
1215 NextMetadataNo++;
1216 break;
1217 }
1218 case bitc::METADATA_LOCATION: {
1219 if (Record.size() != 5 && Record.size() != 6)
1220 return error("Invalid record");
1221
1222 IsDistinct = Record[0];
1223 unsigned Line = Record[1];
1224 unsigned Column = Record[2];
1225 Metadata *Scope = getMD(Record[3]);
1226 Metadata *InlinedAt = getMDOrNull(Record[4]);
1227 bool ImplicitCode = Record.size() == 6 && Record[5];
1228 MetadataList.assignValue(
1229 GET_OR_DISTINCT(DILocation, (Context, Line, Column, Scope, InlinedAt,
1230 ImplicitCode)),
1231 NextMetadataNo);
1232 NextMetadataNo++;
1233 break;
1234 }
1235 case bitc::METADATA_GENERIC_DEBUG: {
1236 if (Record.size() < 4)
1237 return error("Invalid record");
1238
1239 IsDistinct = Record[0];
1240 unsigned Tag = Record[1];
1241 unsigned Version = Record[2];
1242
1243 if (Tag >= 1u << 16 || Version != 0)
1244 return error("Invalid record");
1245
1246 auto *Header = getMDString(Record[3]);
1247 SmallVector<Metadata *, 8> DwarfOps;
1248 for (unsigned I = 4, E = Record.size(); I != E; ++I)
1249 DwarfOps.push_back(getMDOrNull(Record[I]));
1250 MetadataList.assignValue(
1251 GET_OR_DISTINCT(GenericDINode, (Context, Tag, Header, DwarfOps)),
1252 NextMetadataNo);
1253 NextMetadataNo++;
1254 break;
1255 }
1256 case bitc::METADATA_SUBRANGE: {
1257 Metadata *Val = nullptr;
1258 // Operand 'count' is interpreted as:
1259 // - Signed integer (version 0)
1260 // - Metadata node (version 1)
1261 switch (Record[0] >> 1) {
1262 case 0:
1263 Val = GET_OR_DISTINCT(DISubrange,
1264 (Context, Record[1], unrotateSign(Record.back())));
1265 break;
1266 case 1:
1267 Val = GET_OR_DISTINCT(DISubrange, (Context, getMDOrNull(Record[1]),
1268 unrotateSign(Record.back())));
1269 break;
1270 default:
1271 return error("Invalid record: Unsupported version of DISubrange");
1272 }
1273
1274 MetadataList.assignValue(Val, NextMetadataNo);
1275 IsDistinct = Record[0] & 1;
1276 NextMetadataNo++;
1277 break;
1278 }
1279 case bitc::METADATA_ENUMERATOR: {
1280 if (Record.size() != 3)
1281 return error("Invalid record");
1282
1283 IsDistinct = Record[0] & 1;
1284 bool IsUnsigned = Record[0] & 2;
1285 MetadataList.assignValue(
1286 GET_OR_DISTINCT(DIEnumerator, (Context, unrotateSign(Record[1]),
1287 IsUnsigned, getMDString(Record[2]))),
1288 NextMetadataNo);
1289 NextMetadataNo++;
1290 break;
1291 }
1292 case bitc::METADATA_BASIC_TYPE: {
1293 if (Record.size() < 6 || Record.size() > 7)
1294 return error("Invalid record");
1295
1296 IsDistinct = Record[0];
1297 DINode::DIFlags Flags = (Record.size() > 6) ?
1298 static_cast<DINode::DIFlags>(Record[6]) : DINode::FlagZero;
1299
1300 MetadataList.assignValue(
1301 GET_OR_DISTINCT(DIBasicType,
1302 (Context, Record[1], getMDString(Record[2]), Record[3],
1303 Record[4], Record[5], Flags)),
1304 NextMetadataNo);
1305 NextMetadataNo++;
1306 break;
1307 }
1308 case bitc::METADATA_DERIVED_TYPE: {
1309 if (Record.size() < 12 || Record.size() > 13)
1310 return error("Invalid record");
1311
1312 // DWARF address space is encoded as N->getDWARFAddressSpace() + 1. 0 means
1313 // that there is no DWARF address space associated with DIDerivedType.
1314 Optional<unsigned> DWARFAddressSpace;
1315 if (Record.size() > 12 && Record[12])
1316 DWARFAddressSpace = Record[12] - 1;
1317
1318 IsDistinct = Record[0];
1319 DINode::DIFlags Flags = static_cast<DINode::DIFlags>(Record[10]);
1320 MetadataList.assignValue(
1321 GET_OR_DISTINCT(DIDerivedType,
1322 (Context, Record[1], getMDString(Record[2]),
1323 getMDOrNull(Record[3]), Record[4],
1324 getDITypeRefOrNull(Record[5]),
1325 getDITypeRefOrNull(Record[6]), Record[7], Record[8],
1326 Record[9], DWARFAddressSpace, Flags,
1327 getDITypeRefOrNull(Record[11]))),
1328 NextMetadataNo);
1329 NextMetadataNo++;
1330 break;
1331 }
1332 case bitc::METADATA_COMPOSITE_TYPE: {
1333 if (Record.size() < 16 || Record.size() > 17)
1334 return error("Invalid record");
1335
1336 // If we have a UUID and this is not a forward declaration, lookup the
1337 // mapping.
1338 IsDistinct = Record[0] & 0x1;
1339 bool IsNotUsedInTypeRef = Record[0] >= 2;
1340 unsigned Tag = Record[1];
1341 MDString *Name = getMDString(Record[2]);
1342 Metadata *File = getMDOrNull(Record[3]);
1343 unsigned Line = Record[4];
1344 Metadata *Scope = getDITypeRefOrNull(Record[5]);
1345 Metadata *BaseType = nullptr;
1346 uint64_t SizeInBits = Record[7];
1347 if (Record[8] > (uint64_t)std::numeric_limits<uint32_t>::max())
1348 return error("Alignment value is too large");
1349 uint32_t AlignInBits = Record[8];
1350 uint64_t OffsetInBits = 0;
1351 DINode::DIFlags Flags = static_cast<DINode::DIFlags>(Record[10]);
1352 Metadata *Elements = nullptr;
1353 unsigned RuntimeLang = Record[12];
1354 Metadata *VTableHolder = nullptr;
1355 Metadata *TemplateParams = nullptr;
1356 Metadata *Discriminator = nullptr;
1357 auto *Identifier = getMDString(Record[15]);
1358 // If this module is being parsed so that it can be ThinLTO imported
1359 // into another module, composite types only need to be imported
1360 // as type declarations (unless full type definitions requested).
1361 // Create type declarations up front to save memory. Also, buildODRType
1362 // handles the case where this is type ODRed with a definition needed
1363 // by the importing module, in which case the existing definition is
1364 // used.
1365 if (IsImporting && !ImportFullTypeDefinitions && Identifier &&
1366 (Tag == dwarf::DW_TAG_enumeration_type ||
1367 Tag == dwarf::DW_TAG_class_type ||
1368 Tag == dwarf::DW_TAG_structure_type ||
1369 Tag == dwarf::DW_TAG_union_type)) {
1370 Flags = Flags | DINode::FlagFwdDecl;
1371 } else {
1372 BaseType = getDITypeRefOrNull(Record[6]);
1373 OffsetInBits = Record[9];
1374 Elements = getMDOrNull(Record[11]);
1375 VTableHolder = getDITypeRefOrNull(Record[13]);
1376 TemplateParams = getMDOrNull(Record[14]);
1377 if (Record.size() > 16)
1378 Discriminator = getMDOrNull(Record[16]);
1379 }
1380 DICompositeType *CT = nullptr;
1381 if (Identifier)
1382 CT = DICompositeType::buildODRType(
1383 Context, *Identifier, Tag, Name, File, Line, Scope, BaseType,
1384 SizeInBits, AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
1385 VTableHolder, TemplateParams, Discriminator);
1386
1387 // Create a node if we didn't get a lazy ODR type.
1388 if (!CT)
1389 CT = GET_OR_DISTINCT(DICompositeType,
1390 (Context, Tag, Name, File, Line, Scope, BaseType,
1391 SizeInBits, AlignInBits, OffsetInBits, Flags,
1392 Elements, RuntimeLang, VTableHolder, TemplateParams,
1393 Identifier, Discriminator));
1394 if (!IsNotUsedInTypeRef && Identifier)
1395 MetadataList.addTypeRef(*Identifier, *cast<DICompositeType>(CT));
1396
1397 MetadataList.assignValue(CT, NextMetadataNo);
1398 NextMetadataNo++;
1399 break;
1400 }
1401 case bitc::METADATA_SUBROUTINE_TYPE: {
1402 if (Record.size() < 3 || Record.size() > 4)
1403 return error("Invalid record");
1404 bool IsOldTypeRefArray = Record[0] < 2;
1405 unsigned CC = (Record.size() > 3) ? Record[3] : 0;
1406
1407 IsDistinct = Record[0] & 0x1;
1408 DINode::DIFlags Flags = static_cast<DINode::DIFlags>(Record[1]);
1409 Metadata *Types = getMDOrNull(Record[2]);
1410 if (LLVM_UNLIKELY(IsOldTypeRefArray)__builtin_expect((bool)(IsOldTypeRefArray), false))
1411 Types = MetadataList.upgradeTypeRefArray(Types);
1412
1413 MetadataList.assignValue(
1414 GET_OR_DISTINCT(DISubroutineType, (Context, Flags, CC, Types)),
1415 NextMetadataNo);
1416 NextMetadataNo++;
1417 break;
1418 }
1419
1420 case bitc::METADATA_MODULE: {
1421 if (Record.size() < 5 || Record.size() > 6)
1422 return error("Invalid record");
1423
1424 IsDistinct = Record[0];
1425 MetadataList.assignValue(
1426 GET_OR_DISTINCT(
1427 DIModule, (Context, getMDOrNull(Record[1]), getMDString(Record[2]),
1428 getMDString(Record[3]), getMDString(Record[4]))),
1429 NextMetadataNo);
1430 NextMetadataNo++;
1431 break;
1432 }
1433
1434 case bitc::METADATA_FILE: {
1435 if (Record.size() != 3 && Record.size() != 5 && Record.size() != 6)
1436 return error("Invalid record");
1437
1438 IsDistinct = Record[0];
1439 Optional<DIFile::ChecksumInfo<MDString *>> Checksum;
1440 // The BitcodeWriter writes null bytes into Record[3:4] when the Checksum
1441 // is not present. This matches up with the old internal representation,
1442 // and the old encoding for CSK_None in the ChecksumKind. The new
1443 // representation reserves the value 0 in the ChecksumKind to continue to
1444 // encode None in a backwards-compatible way.
1445 if (Record.size() > 4 && Record[3] && Record[4])
1446 Checksum.emplace(static_cast<DIFile::ChecksumKind>(Record[3]),
1447 getMDString(Record[4]));
1448 MetadataList.assignValue(
1449 GET_OR_DISTINCT(
1450 DIFile,
1451 (Context, getMDString(Record[1]), getMDString(Record[2]), Checksum,
1452 Record.size() > 5 ? Optional<MDString *>(getMDString(Record[5]))
1453 : None)),
1454 NextMetadataNo);
1455 NextMetadataNo++;
1456 break;
1457 }
1458 case bitc::METADATA_COMPILE_UNIT: {
1459 if (Record.size() < 14 || Record.size() > 21)
1460 return error("Invalid record");
1461
1462 // Ignore Record[0], which indicates whether this compile unit is
1463 // distinct. It's always distinct.
1464 IsDistinct = true;
1465 auto *CU = DICompileUnit::getDistinct(
1466 Context, Record[1], getMDOrNull(Record[2]), getMDString(Record[3]),
1467 Record[4], getMDString(Record[5]), Record[6], getMDString(Record[7]),
1468 Record[8], getMDOrNull(Record[9]), getMDOrNull(Record[10]),
1469 getMDOrNull(Record[12]), getMDOrNull(Record[13]),
1470 Record.size() <= 15 ? nullptr : getMDOrNull(Record[15]),
1471 Record.size() <= 14 ? 0 : Record[14],
1472 Record.size() <= 16 ? true : Record[16],
1473 Record.size() <= 17 ? false : Record[17],
1474 Record.size() <= 18 ? 0 : Record[18],
1475 false, // FIXME: https://reviews.llvm.org/rGc51b45e32ef7f35c11891f60871aa9c2c04cd991
1476 // Record.size() <= 19 ? 0 : Record[19],
1477 Record.size() <= 20 ? nullptr : getMDString(Record[20]));
1478
1479 MetadataList.assignValue(CU, NextMetadataNo);
1480 NextMetadataNo++;
1481
1482 // Move the Upgrade the list of subprograms.
1483 if (Metadata *SPs = getMDOrNullWithoutPlaceholders(Record[11]))
1484 CUSubprograms.push_back({CU, SPs});
1485 break;
1486 }
1487 case bitc::METADATA_SUBPROGRAM: {
1488 if (Record.size() < 18 || Record.size() > 21)
1489 return error("Invalid record");
1490
1491 bool HasSPFlags = Record[0] & 4;
1492
1493 DINode::DIFlags Flags;
1494 DISubprogram::DISPFlags SPFlags;
1495 if (!HasSPFlags)
1496 Flags = static_cast<DINode::DIFlags>(Record[11 + 2]);
1497 else {
1498 Flags = static_cast<DINode::DIFlags>(Record[11]);
1499 SPFlags = static_cast<DISubprogram::DISPFlags>(Record[9]);
1500 }
1501
1502 // Support for old metadata when
1503 // subprogram specific flags are placed in DIFlags.
1504 const unsigned DIFlagMainSubprogram = 1 << 21;
1505 bool HasOldMainSubprogramFlag = Flags & DIFlagMainSubprogram;
1506 if (HasOldMainSubprogramFlag)
1507 // Remove old DIFlagMainSubprogram from DIFlags.
1508 // Note: This assumes that any future use of bit 21 defaults to it
1509 // being 0.
1510 Flags &= ~static_cast<DINode::DIFlags>(DIFlagMainSubprogram);
1511
1512 if (HasOldMainSubprogramFlag && HasSPFlags)
1513 SPFlags |= DISubprogram::SPFlagMainSubprogram;
1514 else if (!HasSPFlags)
1515 SPFlags = DISubprogram::toSPFlags(
1516 /*IsLocalToUnit=*/Record[7], /*IsDefinition=*/Record[8],
1517 /*IsOptimized=*/Record[14], /*Virtuality=*/Record[11],
1518 /*DIFlagMainSubprogram*/HasOldMainSubprogramFlag);
1519
1520 // All definitions should be distinct.
1521 IsDistinct = (Record[0] & 1) || (SPFlags & DISubprogram::SPFlagDefinition);
1522 // Version 1 has a Function as Record[15].
1523 // Version 2 has removed Record[15].
1524 // Version 3 has the Unit as Record[15].
1525 // Version 4 added thisAdjustment.
1526 // Version 5 repacked flags into DISPFlags, changing many element numbers.
1527 bool HasUnit = Record[0] & 2;
1528 if (!HasSPFlags && HasUnit && Record.size() < 19)
1529 return error("Invalid record");
1530 if (HasSPFlags && !HasUnit)
1531 return error("Invalid record");
1532 // Accommodate older formats.
1533 bool HasFn = false;
1534 bool HasThisAdj = true;
1535 bool HasThrownTypes = true;
1536 unsigned OffsetA = 0;
1537 unsigned OffsetB = 0;
1538 if (!HasSPFlags) {
1539 OffsetA = 2;
1540 OffsetB = 2;
1541 if (Record.size() >= 19) {
1542 HasFn = !HasUnit;
1543 OffsetB++;
1544 }
1545 HasThisAdj = Record.size() >= 20;
1546 HasThrownTypes = Record.size() >= 21;
1547 }
1548 Metadata *CUorFn = getMDOrNull(Record[12 + OffsetB]);
1549 DISubprogram *SP = GET_OR_DISTINCT(
1550 DISubprogram,
1551 (Context,
1552 getDITypeRefOrNull(Record[1]), // scope
1553 getMDString(Record[2]), // name
1554 getMDString(Record[3]), // linkageName
1555 getMDOrNull(Record[4]), // file
1556 Record[5], // line
1557 getMDOrNull(Record[6]), // type
1558 Record[7 + OffsetA], // scopeLine
1559 getDITypeRefOrNull(Record[8 + OffsetA]), // containingType
1560 Record[10 + OffsetA], // virtualIndex
1561 HasThisAdj ? Record[16 + OffsetB] : 0, // thisAdjustment
1562 Flags, // flags
1563 SPFlags, // SPFlags
1564 HasUnit ? CUorFn : nullptr, // unit
1565 getMDOrNull(Record[13 + OffsetB]), // templateParams
1566 getMDOrNull(Record[14 + OffsetB]), // declaration
1567 getMDOrNull(Record[15 + OffsetB]), // retainedNodes
1568 HasThrownTypes ? getMDOrNull(Record[17 + OffsetB])
1569 : nullptr // thrownTypes
1570 ));
1571 MetadataList.assignValue(SP, NextMetadataNo);
1572 NextMetadataNo++;
1573
1574 // Upgrade sp->function mapping to function->sp mapping.
1575 if (HasFn) {
1576 if (auto *CMD = dyn_cast_or_null<ConstantAsMetadata>(CUorFn))
1577 if (auto *F = dyn_cast<Function>(CMD->getValue())) {
1578 if (F->isMaterializable())
1579 // Defer until materialized; unmaterialized functions may not have
1580 // metadata.
1581 FunctionsWithSPs[F] = SP;
1582 else if (!F->empty())
1583 F->setSubprogram(SP);
1584 }
1585 }
1586 break;
1587 }
1588 case bitc::METADATA_LEXICAL_BLOCK: {
1589 if (Record.size() != 5)
1590 return error("Invalid record");
1591
1592 IsDistinct = Record[0];
1593 MetadataList.assignValue(
1594 GET_OR_DISTINCT(DILexicalBlock,
1595 (Context, getMDOrNull(Record[1]),
1596 getMDOrNull(Record[2]), Record[3], Record[4])),
1597 NextMetadataNo);
1598 NextMetadataNo++;
1599 break;
1600 }
1601 case bitc::METADATA_LEXICAL_BLOCK_FILE: {
1602 if (Record.size() != 4)
1603 return error("Invalid record");
1604
1605 IsDistinct = Record[0];
1606 MetadataList.assignValue(
1607 GET_OR_DISTINCT(DILexicalBlockFile,
1608 (Context, getMDOrNull(Record[1]),
1609 getMDOrNull(Record[2]), Record[3])),
1610 NextMetadataNo);
1611 NextMetadataNo++;
1612 break;
1613 }
1614 case bitc::METADATA_COMMON_BLOCK: {
1615 IsDistinct = Record[0] & 1;
1616 MetadataList.assignValue(
1617 GET_OR_DISTINCT(DICommonBlock,
1618 (Context, getMDOrNull(Record[1]),
1619 getMDOrNull(Record[2]), getMDString(Record[3]),
1620 getMDOrNull(Record[4]), Record[5])),
1621 NextMetadataNo);
1622 NextMetadataNo++;
1623 break;
1624 }
1625 case bitc::METADATA_NAMESPACE: {
1626 // Newer versions of DINamespace dropped file and line.
1627 MDString *Name;
1628 if (Record.size() == 3)
1629 Name = getMDString(Record[2]);
1630 else if (Record.size() == 5)
1631 Name = getMDString(Record[3]);
1632 else
1633 return error("Invalid record");
1634
1635 IsDistinct = Record[0] & 1;
1636 bool ExportSymbols = Record[0] & 2;
1637 MetadataList.assignValue(
1638 GET_OR_DISTINCT(DINamespace,
1639 (Context, getMDOrNull(Record[1]), Name, ExportSymbols)),
1640 NextMetadataNo);
1641 NextMetadataNo++;
1642 break;
1643 }
1644 case bitc::METADATA_MACRO: {
1645 if (Record.size() != 5)
1646 return error("Invalid record");
1647
1648 IsDistinct = Record[0];
1649 MetadataList.assignValue(
1650 GET_OR_DISTINCT(DIMacro,
1651 (Context, Record[1], Record[2], getMDString(Record[3]),
1652 getMDString(Record[4]))),
1653 NextMetadataNo);
1654 NextMetadataNo++;
1655 break;
1656 }
1657 case bitc::METADATA_MACRO_FILE: {
1658 if (Record.size() != 5)
1659 return error("Invalid record");
1660
1661 IsDistinct = Record[0];
1662 MetadataList.assignValue(
1663 GET_OR_DISTINCT(DIMacroFile,
1664 (Context, Record[1], Record[2], getMDOrNull(Record[3]),
1665 getMDOrNull(Record[4]))),
1666 NextMetadataNo);
1667 NextMetadataNo++;
1668 break;
1669 }
1670 case bitc::METADATA_TEMPLATE_TYPE: {
1671 if (Record.size() < 3 || Record.size() > 4)
1672 return error("Invalid record");
1673
1674 IsDistinct = Record[0];
1675 MetadataList.assignValue(
1676 GET_OR_DISTINCT(DITemplateTypeParameter,
1677 (Context, getMDString(Record[1]),
1678 getDITypeRefOrNull(Record[2]),
1679 (Record.size() == 4) ? getMDOrNull(Record[3])
1680 : getMDOrNull(false))),
1681 NextMetadataNo);
1682 NextMetadataNo++;
1683 break;
1684 }
1685 case bitc::METADATA_TEMPLATE_VALUE: {
1686 if (Record.size() < 5 || Record.size() > 6)
1687 return error("Invalid record");
1688
1689 IsDistinct = Record[0];
1690
1691 MetadataList.assignValue(
1692 GET_OR_DISTINCT(
1693 DITemplateValueParameter,
1694 (Context, Record[1], getMDString(Record[2]),
1695 getDITypeRefOrNull(Record[3]),
1696 (Record.size() == 6) ? getMDOrNull(Record[4]) : getMDOrNull(false),
1697 (Record.size() == 6) ? getMDOrNull(Record[5])
1698 : getMDOrNull(Record[4]))),
1699 NextMetadataNo);
1700 NextMetadataNo++;
1701 break;
1702 }
1703 case bitc::METADATA_GLOBAL_VAR: {
1704 if (Record.size() < 11 || Record.size() > 13)
1705 return error("Invalid record");
1706
1707 IsDistinct = Record[0] & 1;
1708 unsigned Version = Record[0] >> 1;
1709
1710 if (Version == 2) {
1711 MetadataList.assignValue(
1712 GET_OR_DISTINCT(
1713 DIGlobalVariable,
1714 (Context, getMDOrNull(Record[1]), getMDString(Record[2]),
1715 getMDString(Record[3]), getMDOrNull(Record[4]), Record[5],
1716 getDITypeRefOrNull(Record[6]), Record[7], Record[8],
1717 getMDOrNull(Record[9]), getMDOrNull(Record[10]), Record[11])),
1718 NextMetadataNo);
1719
1720 NextMetadataNo++;
1721 } else if (Version == 1) {
1722 // No upgrade necessary. A null field will be introduced to indicate
1723 // that no parameter information is available.
1724 MetadataList.assignValue(
1725 GET_OR_DISTINCT(DIGlobalVariable,
1726 (Context, getMDOrNull(Record[1]),
1727 getMDString(Record[2]), getMDString(Record[3]),
1728 getMDOrNull(Record[4]), Record[5],
1729 getDITypeRefOrNull(Record[6]), Record[7], Record[8],
1730 getMDOrNull(Record[10]), nullptr, Record[11])),
1731 NextMetadataNo);
1732
1733 NextMetadataNo++;
1734 } else if (Version == 0) {
1735 // Upgrade old metadata, which stored a global variable reference or a
1736 // ConstantInt here.
1737 NeedUpgradeToDIGlobalVariableExpression = true;
1738 Metadata *Expr = getMDOrNull(Record[9]);
1739 uint32_t AlignInBits = 0;
1740 if (Record.size() > 11) {
1741 if (Record[11] > (uint64_t)std::numeric_limits<uint32_t>::max())
1742 return error("Alignment value is too large");
1743 AlignInBits = Record[11];
1744 }
1745 GlobalVariable *Attach = nullptr;
1746 if (auto *CMD = dyn_cast_or_null<ConstantAsMetadata>(Expr)) {
1747 if (auto *GV = dyn_cast<GlobalVariable>(CMD->getValue())) {
1748 Attach = GV;
1749 Expr = nullptr;
1750 } else if (auto *CI = dyn_cast<ConstantInt>(CMD->getValue())) {
1751 Expr = DIExpression::get(Context,
1752 {dwarf::DW_OP_constu, CI->getZExtValue(),
1753 dwarf::DW_OP_stack_value});
1754 } else {
1755 Expr = nullptr;
1756 }
1757 }
1758 DIGlobalVariable *DGV = GET_OR_DISTINCT(
1759 DIGlobalVariable,
1760 (Context, getMDOrNull(Record[1]), getMDString(Record[2]),
1761 getMDString(Record[3]), getMDOrNull(Record[4]), Record[5],
1762 getDITypeRefOrNull(Record[6]), Record[7], Record[8],
1763 getMDOrNull(Record[10]), nullptr, AlignInBits));
1764
1765 DIGlobalVariableExpression *DGVE = nullptr;
1766 if (Attach || Expr)
1767 DGVE = DIGlobalVariableExpression::getDistinct(
1768 Context, DGV, Expr ? Expr : DIExpression::get(Context, {}));
1769 if (Attach)
1770 Attach->addDebugInfo(DGVE);
1771
1772 auto *MDNode = Expr ? cast<Metadata>(DGVE) : cast<Metadata>(DGV);
1773 MetadataList.assignValue(MDNode, NextMetadataNo);
1774 NextMetadataNo++;
1775 } else
1776 return error("Invalid record");
1777
1778 break;
1779 }
1780 case bitc::METADATA_LOCAL_VAR: {
1781 // 10th field is for the obseleted 'inlinedAt:' field.
1782 if (Record.size() < 8 || Record.size() > 10)
1783 return error("Invalid record");
1784
1785 IsDistinct = Record[0] & 1;
1786 bool HasAlignment = Record[0] & 2;
1787 // 2nd field used to be an artificial tag, either DW_TAG_auto_variable or
1788 // DW_TAG_arg_variable, if we have alignment flag encoded it means, that
1789 // this is newer version of record which doesn't have artificial tag.
1790 bool HasTag = !HasAlignment && Record.size() > 8;
1791 DINode::DIFlags Flags = static_cast<DINode::DIFlags>(Record[7 + HasTag]);
1792 uint32_t AlignInBits = 0;
1793 if (HasAlignment) {
1794 if (Record[8 + HasTag] > (uint64_t)std::numeric_limits<uint32_t>::max())
1795 return error("Alignment value is too large");
1796 AlignInBits = Record[8 + HasTag];
1797 }
1798 MetadataList.assignValue(
1799 GET_OR_DISTINCT(DILocalVariable,
1800 (Context, getMDOrNull(Record[1 + HasTag]),
1801 getMDString(Record[2 + HasTag]),
1802 getMDOrNull(Record[3 + HasTag]), Record[4 + HasTag],
1803 getDITypeRefOrNull(Record[5 + HasTag]),
1804 Record[6 + HasTag], Flags, AlignInBits)),
1805 NextMetadataNo);
1806 NextMetadataNo++;
1807 break;
1808 }
1809 case bitc::METADATA_LABEL: {
1810 if (Record.size() != 5)
1811 return error("Invalid record");
1812
1813 IsDistinct = Record[0] & 1;
1814 MetadataList.assignValue(
1815 GET_OR_DISTINCT(DILabel,
1816 (Context, getMDOrNull(Record[1]),
1817 getMDString(Record[2]),
1818 getMDOrNull(Record[3]), Record[4])),
1819 NextMetadataNo);
1820 NextMetadataNo++;
1821 break;
1822 }
1823 case bitc::METADATA_EXPRESSION: {
1824 if (Record.size() < 1)
1825 return error("Invalid record");
1826
1827 IsDistinct = Record[0] & 1;
1828 uint64_t Version = Record[0] >> 1;
1829 auto Elts = MutableArrayRef<uint64_t>(Record).slice(1);
1830
1831 SmallVector<uint64_t, 6> Buffer;
1832 if (Error Err = upgradeDIExpression(Version, Elts, Buffer))
1833 return Err;
1834
1835 MetadataList.assignValue(
1836 GET_OR_DISTINCT(DIExpression, (Context, Elts)), NextMetadataNo);
1837 NextMetadataNo++;
1838 break;
1839 }
1840 case bitc::METADATA_GLOBAL_VAR_EXPR: {
1841 if (Record.size() != 3)
1842 return error("Invalid record");
1843
1844 IsDistinct = Record[0];
1845 Metadata *Expr = getMDOrNull(Record[2]);
1846 if (!Expr)
1847 Expr = DIExpression::get(Context, {});
1848 MetadataList.assignValue(
1849 GET_OR_DISTINCT(DIGlobalVariableExpression,
1850 (Context, getMDOrNull(Record[1]), Expr)),
1851 NextMetadataNo);
1852 NextMetadataNo++;
1853 break;
1854 }
1855 case bitc::METADATA_OBJC_PROPERTY: {
1856 if (Record.size() != 8)
1857 return error("Invalid record");
1858
1859 IsDistinct = Record[0];
1860 MetadataList.assignValue(
1861 GET_OR_DISTINCT(DIObjCProperty,
1862 (Context, getMDString(Record[1]),
1863 getMDOrNull(Record[2]), Record[3],
1864 getMDString(Record[4]), getMDString(Record[5]),
1865 Record[6], getDITypeRefOrNull(Record[7]))),
1866 NextMetadataNo);
1867 NextMetadataNo++;
1868 break;
1869 }
1870 case bitc::METADATA_IMPORTED_ENTITY: {
1871 if (Record.size() != 6 && Record.size() != 7)
1872 return error("Invalid record");
1873
1874 IsDistinct = Record[0];
1875 bool HasFile = (Record.size() == 7);
1876 MetadataList.assignValue(
1877 GET_OR_DISTINCT(DIImportedEntity,
1878 (Context, Record[1], getMDOrNull(Record[2]),
1879 getDITypeRefOrNull(Record[3]),
1880 HasFile ? getMDOrNull(Record[6]) : nullptr,
1881 HasFile ? Record[4] : 0, getMDString(Record[5]))),
1882 NextMetadataNo);
1883 NextMetadataNo++;
1884 break;
1885 }
1886 case bitc::METADATA_STRING_OLD: {
1887 std::string String(Record.begin(), Record.end());
1888
1889 // Test for upgrading !llvm.loop.
1890 HasSeenOldLoopTags |= mayBeOldLoopAttachmentTag(String);
1891 ++NumMDStringLoaded;
1892 Metadata *MD = MDString::get(Context, String);
1893 MetadataList.assignValue(MD, NextMetadataNo);
1894 NextMetadataNo++;
1895 break;
1896 }
1897 case bitc::METADATA_STRINGS: {
1898 auto CreateNextMDString = [&](StringRef Str) {
1899 ++NumMDStringLoaded;
1900 MetadataList.assignValue(MDString::get(Context, Str), NextMetadataNo);
1901 NextMetadataNo++;
1902 };
1903 if (Error Err = parseMetadataStrings(Record, Blob, CreateNextMDString))
1904 return Err;
1905 break;
1906 }
1907 case bitc::METADATA_GLOBAL_DECL_ATTACHMENT: {
1908 if (Record.size() % 2 == 0)
1909 return error("Invalid record");
1910 unsigned ValueID = Record[0];
1911 if (ValueID >= ValueList.size())
1912 return error("Invalid record");
1913 if (auto *GO = dyn_cast<GlobalObject>(ValueList[ValueID]))
1914 if (Error Err = parseGlobalObjectAttachment(
1915 *GO, ArrayRef<uint64_t>(Record).slice(1)))
1916 return Err;
1917 break;
1918 }
1919 case bitc::METADATA_KIND: {
1920 // Support older bitcode files that had METADATA_KIND records in a
1921 // block with METADATA_BLOCK_ID.
1922 if (Error Err = parseMetadataKindRecord(Record))
1923 return Err;
1924 break;
1925 }
1926 }
1927 return Error::success();
1928#undef GET_OR_DISTINCT
1929}
1930
1931Error MetadataLoader::MetadataLoaderImpl::parseMetadataStrings(
1932 ArrayRef<uint64_t> Record, StringRef Blob,
1933 function_ref<void(StringRef)> CallBack) {
1934 // All the MDStrings in the block are emitted together in a single
1935 // record. The strings are concatenated and stored in a blob along with
1936 // their sizes.
1937 if (Record.size() != 2)
1938 return error("Invalid record: metadata strings layout");
1939
1940 unsigned NumStrings = Record[0];
1941 unsigned StringsOffset = Record[1];
1942 if (!NumStrings)
1943 return error("Invalid record: metadata strings with no strings");
1944 if (StringsOffset > Blob.size())
1945 return error("Invalid record: metadata strings corrupt offset");
1946
1947 StringRef Lengths = Blob.slice(0, StringsOffset);
1948 SimpleBitstreamCursor R(Lengths);
1949
1950 StringRef Strings = Blob.drop_front(StringsOffset);
1951 do {
1952 if (R.AtEndOfStream())
1953 return error("Invalid record: metadata strings bad length");
1954
1955 Expected<uint32_t> MaybeSize = R.ReadVBR(6);
1956 if (!MaybeSize)
1957 return MaybeSize.takeError();
1958 uint32_t Size = MaybeSize.get();
1959 if (Strings.size() < Size)
1960 return error("Invalid record: metadata strings truncated chars");
1961
1962 CallBack(Strings.slice(0, Size));
1963 Strings = Strings.drop_front(Size);
1964 } while (--NumStrings);
1965
1966 return Error::success();
1967}
1968
1969Error MetadataLoader::MetadataLoaderImpl::parseGlobalObjectAttachment(
1970 GlobalObject &GO, ArrayRef<uint64_t> Record) {
1971 assert(Record.size() % 2 == 0)((Record.size() % 2 == 0) ? static_cast<void> (0) : __assert_fail
("Record.size() % 2 == 0", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/Bitcode/Reader/MetadataLoader.cpp"
, 1971, __PRETTY_FUNCTION__))
;
1972 for (unsigned I = 0, E = Record.size(); I != E; I += 2) {
1973 auto K = MDKindMap.find(Record[I]);
1974 if (K == MDKindMap.end())
1975 return error("Invalid ID");
1976 MDNode *MD = MetadataList.getMDNodeFwdRefOrNull(Record[I + 1]);
1977 if (!MD)
1978 return error("Invalid metadata attachment: expect fwd ref to MDNode");
1979 GO.addMetadata(K->second, *MD);
1980 }
1981 return Error::success();
1982}
1983
1984/// Parse metadata attachments.
1985Error MetadataLoader::MetadataLoaderImpl::parseMetadataAttachment(
1986 Function &F, const SmallVectorImpl<Instruction *> &InstructionList) {
1987 if (Error Err = Stream.EnterSubBlock(bitc::METADATA_ATTACHMENT_ID))
1988 return Err;
1989
1990 SmallVector<uint64_t, 64> Record;
1991 PlaceholderQueue Placeholders;
1992
1993 while (true) {
1994 Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks();
1995 if (!MaybeEntry)
1996 return MaybeEntry.takeError();
1997 BitstreamEntry Entry = MaybeEntry.get();
1998
1999 switch (Entry.Kind) {
2000 case BitstreamEntry::SubBlock: // Handled for us already.
2001 case BitstreamEntry::Error:
2002 return error("Malformed block");
2003 case BitstreamEntry::EndBlock:
2004 resolveForwardRefsAndPlaceholders(Placeholders);
2005 return Error::success();
2006 case BitstreamEntry::Record:
2007 // The interesting case.
2008 break;
2009 }
2010
2011 // Read a metadata attachment record.
2012 Record.clear();
2013 ++NumMDRecordLoaded;
2014 Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record);
2015 if (!MaybeRecord)
2016 return MaybeRecord.takeError();
2017 switch (MaybeRecord.get()) {
2018 default: // Default behavior: ignore.
2019 break;
2020 case bitc::METADATA_ATTACHMENT: {
2021 unsigned RecordLength = Record.size();
2022 if (Record.empty())
2023 return error("Invalid record");
2024 if (RecordLength % 2 == 0) {
2025 // A function attachment.
2026 if (Error Err = parseGlobalObjectAttachment(F, Record))
2027 return Err;
2028 continue;
2029 }
2030
2031 // An instruction attachment.
2032 Instruction *Inst = InstructionList[Record[0]];
2033 for (unsigned i = 1; i != RecordLength; i = i + 2) {
2034 unsigned Kind = Record[i];
2035 DenseMap<unsigned, unsigned>::iterator I = MDKindMap.find(Kind);
2036 if (I == MDKindMap.end())
2037 return error("Invalid ID");
2038 if (I->second == LLVMContext::MD_tbaa && StripTBAA)
2039 continue;
2040
2041 auto Idx = Record[i + 1];
2042 if (Idx < (MDStringRef.size() + GlobalMetadataBitPosIndex.size()) &&
2043 !MetadataList.lookup(Idx)) {
2044 // Load the attachment if it is in the lazy-loadable range and hasn't
2045 // been loaded yet.
2046 lazyLoadOneMetadata(Idx, Placeholders);
2047 resolveForwardRefsAndPlaceholders(Placeholders);
2048 }
2049
2050 Metadata *Node = MetadataList.getMetadataFwdRef(Idx);
2051 if (isa<LocalAsMetadata>(Node))
2052 // Drop the attachment. This used to be legal, but there's no
2053 // upgrade path.
2054 break;
2055 MDNode *MD = dyn_cast_or_null<MDNode>(Node);
2056 if (!MD)
2057 return error("Invalid metadata attachment");
2058
2059 if (HasSeenOldLoopTags && I->second == LLVMContext::MD_loop)
2060 MD = upgradeInstructionLoopAttachment(*MD);
2061
2062 if (I->second == LLVMContext::MD_tbaa) {
2063 assert(!MD->isTemporary() && "should load MDs before attachments")((!MD->isTemporary() && "should load MDs before attachments"
) ? static_cast<void> (0) : __assert_fail ("!MD->isTemporary() && \"should load MDs before attachments\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/Bitcode/Reader/MetadataLoader.cpp"
, 2063, __PRETTY_FUNCTION__))
;
2064 MD = UpgradeTBAANode(*MD);
2065 }
2066 Inst->setMetadata(I->second, MD);
2067 }
2068 break;
2069 }
2070 }
2071 }
2072}
2073
2074/// Parse a single METADATA_KIND record, inserting result in MDKindMap.
2075Error MetadataLoader::MetadataLoaderImpl::parseMetadataKindRecord(
2076 SmallVectorImpl<uint64_t> &Record) {
2077 if (Record.size() < 2)
2078 return error("Invalid record");
2079
2080 unsigned Kind = Record[0];
2081 SmallString<8> Name(Record.begin() + 1, Record.end());
2082
2083 unsigned NewKind = TheModule.getMDKindID(Name.str());
2084 if (!MDKindMap.insert(std::make_pair(Kind, NewKind)).second)
2085 return error("Conflicting METADATA_KIND records");
2086 return Error::success();
2087}
2088
2089/// Parse the metadata kinds out of the METADATA_KIND_BLOCK.
2090Error MetadataLoader::MetadataLoaderImpl::parseMetadataKinds() {
2091 if (Error Err = Stream.EnterSubBlock(bitc::METADATA_KIND_BLOCK_ID))
2
Assuming the condition is false
3
Taking false branch
2092 return Err;
2093
2094 SmallVector<uint64_t, 64> Record;
2095
2096 // Read all the records.
2097 while (true) {
4
Loop condition is true. Entering loop body
2098 Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks();
5
Calling 'BitstreamCursor::advanceSkippingSubblocks'
2099 if (!MaybeEntry)
2100 return MaybeEntry.takeError();
2101 BitstreamEntry Entry = MaybeEntry.get();
2102
2103 switch (Entry.Kind) {
2104 case BitstreamEntry::SubBlock: // Handled for us already.
2105 case BitstreamEntry::Error:
2106 return error("Malformed block");
2107 case BitstreamEntry::EndBlock:
2108 return Error::success();
2109 case BitstreamEntry::Record:
2110 // The interesting case.
2111 break;
2112 }
2113
2114 // Read a record.
2115 Record.clear();
2116 ++NumMDRecordLoaded;
2117 Expected<unsigned> MaybeCode = Stream.readRecord(Entry.ID, Record);
2118 if (!MaybeCode)
2119 return MaybeCode.takeError();
2120 switch (MaybeCode.get()) {
2121 default: // Default behavior: ignore.
2122 break;
2123 case bitc::METADATA_KIND: {
2124 if (Error Err = parseMetadataKindRecord(Record))
2125 return Err;
2126 break;
2127 }
2128 }
2129 }
2130}
2131
2132MetadataLoader &MetadataLoader::operator=(MetadataLoader &&RHS) {
2133 Pimpl = std::move(RHS.Pimpl);
2134 return *this;
2135}
2136MetadataLoader::MetadataLoader(MetadataLoader &&RHS)
2137 : Pimpl(std::move(RHS.Pimpl)) {}
2138
2139MetadataLoader::~MetadataLoader() = default;
2140MetadataLoader::MetadataLoader(BitstreamCursor &Stream, Module &TheModule,
2141 BitcodeReaderValueList &ValueList,
2142 bool IsImporting,
2143 std::function<Type *(unsigned)> getTypeByID)
2144 : Pimpl(std::make_unique<MetadataLoaderImpl>(
2145 Stream, TheModule, ValueList, std::move(getTypeByID), IsImporting)) {}
2146
2147Error MetadataLoader::parseMetadata(bool ModuleLevel) {
2148 return Pimpl->parseMetadata(ModuleLevel);
2149}
2150
2151bool MetadataLoader::hasFwdRefs() const { return Pimpl->hasFwdRefs(); }
2152
2153/// Return the given metadata, creating a replaceable forward reference if
2154/// necessary.
2155Metadata *MetadataLoader::getMetadataFwdRefOrLoad(unsigned Idx) {
2156 return Pimpl->getMetadataFwdRefOrLoad(Idx);
2157}
2158
2159DISubprogram *MetadataLoader::lookupSubprogramForFunction(Function *F) {
2160 return Pimpl->lookupSubprogramForFunction(F);
2161}
2162
2163Error MetadataLoader::parseMetadataAttachment(
2164 Function &F, const SmallVectorImpl<Instruction *> &InstructionList) {
2165 return Pimpl->parseMetadataAttachment(F, InstructionList);
2166}
2167
2168Error MetadataLoader::parseMetadataKinds() {
2169 return Pimpl->parseMetadataKinds();
1
Calling 'MetadataLoaderImpl::parseMetadataKinds'
2170}
2171
2172void MetadataLoader::setStripTBAA(bool StripTBAA) {
2173 return Pimpl->setStripTBAA(StripTBAA);
2174}
2175
2176bool MetadataLoader::isStrippingTBAA() { return Pimpl->isStrippingTBAA(); }
2177
2178unsigned MetadataLoader::size() const { return Pimpl->size(); }
2179void MetadataLoader::shrinkTo(unsigned N) { return Pimpl->shrinkTo(N); }
2180
2181void MetadataLoader::upgradeDebugIntrinsics(Function &F) {
2182 return Pimpl->upgradeDebugIntrinsics(F);
2183}

/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/Bitstream/BitstreamReader.h

1//===- BitstreamReader.h - Low-level bitstream reader interface -*- 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 header defines the BitstreamReader class. This class can be used to
10// read an arbitrary bitstream, regardless of its contents.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_BITSTREAM_BITSTREAMREADER_H
15#define LLVM_BITSTREAM_BITSTREAMREADER_H
16
17#include "llvm/ADT/ArrayRef.h"
18#include "llvm/ADT/SmallVector.h"
19#include "llvm/Bitstream/BitCodes.h"
20#include "llvm/Support/Endian.h"
21#include "llvm/Support/Error.h"
22#include "llvm/Support/ErrorHandling.h"
23#include "llvm/Support/MathExtras.h"
24#include "llvm/Support/MemoryBuffer.h"
25#include <algorithm>
26#include <cassert>
27#include <climits>
28#include <cstddef>
29#include <cstdint>
30#include <memory>
31#include <string>
32#include <utility>
33#include <vector>
34
35namespace llvm {
36
37/// This class maintains the abbreviations read from a block info block.
38class BitstreamBlockInfo {
39public:
40 /// This contains information emitted to BLOCKINFO_BLOCK blocks. These
41 /// describe abbreviations that all blocks of the specified ID inherit.
42 struct BlockInfo {
43 unsigned BlockID = 0;
44 std::vector<std::shared_ptr<BitCodeAbbrev>> Abbrevs;
45 std::string Name;
46 std::vector<std::pair<unsigned, std::string>> RecordNames;
47 };
48
49private:
50 std::vector<BlockInfo> BlockInfoRecords;
51
52public:
53 /// If there is block info for the specified ID, return it, otherwise return
54 /// null.
55 const BlockInfo *getBlockInfo(unsigned BlockID) const {
56 // Common case, the most recent entry matches BlockID.
57 if (!BlockInfoRecords.empty() && BlockInfoRecords.back().BlockID == BlockID)
58 return &BlockInfoRecords.back();
59
60 for (unsigned i = 0, e = static_cast<unsigned>(BlockInfoRecords.size());
61 i != e; ++i)
62 if (BlockInfoRecords[i].BlockID == BlockID)
63 return &BlockInfoRecords[i];
64 return nullptr;
65 }
66
67 BlockInfo &getOrCreateBlockInfo(unsigned BlockID) {
68 if (const BlockInfo *BI = getBlockInfo(BlockID))
69 return *const_cast<BlockInfo*>(BI);
70
71 // Otherwise, add a new record.
72 BlockInfoRecords.emplace_back();
73 BlockInfoRecords.back().BlockID = BlockID;
74 return BlockInfoRecords.back();
75 }
76};
77
78/// This represents a position within a bitstream. There may be multiple
79/// independent cursors reading within one bitstream, each maintaining their
80/// own local state.
81class SimpleBitstreamCursor {
82 ArrayRef<uint8_t> BitcodeBytes;
83 size_t NextChar = 0;
84
85public:
86 /// This is the current data we have pulled from the stream but have not
87 /// returned to the client. This is specifically and intentionally defined to
88 /// follow the word size of the host machine for efficiency. We use word_t in
89 /// places that are aware of this to make it perfectly explicit what is going
90 /// on.
91 using word_t = size_t;
92
93private:
94 word_t CurWord = 0;
95
96 /// This is the number of bits in CurWord that are valid. This is always from
97 /// [0...bits_of(size_t)-1] inclusive.
98 unsigned BitsInCurWord = 0;
99
100public:
101 static const constexpr size_t MaxChunkSize = sizeof(word_t) * 8;
102
103 SimpleBitstreamCursor() = default;
104 explicit SimpleBitstreamCursor(ArrayRef<uint8_t> BitcodeBytes)
105 : BitcodeBytes(BitcodeBytes) {}
106 explicit SimpleBitstreamCursor(StringRef BitcodeBytes)
107 : BitcodeBytes(arrayRefFromStringRef(BitcodeBytes)) {}
108 explicit SimpleBitstreamCursor(MemoryBufferRef BitcodeBytes)
109 : SimpleBitstreamCursor(BitcodeBytes.getBuffer()) {}
110
111 bool canSkipToPos(size_t pos) const {
112 // pos can be skipped to if it is a valid address or one byte past the end.
113 return pos <= BitcodeBytes.size();
114 }
115
116 bool AtEndOfStream() {
117 return BitsInCurWord == 0 && BitcodeBytes.size() <= NextChar;
118 }
119
120 /// Return the bit # of the bit we are reading.
121 uint64_t GetCurrentBitNo() const {
122 return NextChar*CHAR_BIT8 - BitsInCurWord;
123 }
124
125 // Return the byte # of the current bit.
126 uint64_t getCurrentByteNo() const { return GetCurrentBitNo() / 8; }
127
128 ArrayRef<uint8_t> getBitcodeBytes() const { return BitcodeBytes; }
129
130 /// Reset the stream to the specified bit number.
131 Error JumpToBit(uint64_t BitNo) {
132 size_t ByteNo = size_t(BitNo/8) & ~(sizeof(word_t)-1);
133 unsigned WordBitNo = unsigned(BitNo & (sizeof(word_t)*8-1));
134 assert(canSkipToPos(ByteNo) && "Invalid location")((canSkipToPos(ByteNo) && "Invalid location") ? static_cast
<void> (0) : __assert_fail ("canSkipToPos(ByteNo) && \"Invalid location\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/Bitstream/BitstreamReader.h"
, 134, __PRETTY_FUNCTION__))
;
135
136 // Move the cursor to the right word.
137 NextChar = ByteNo;
138 BitsInCurWord = 0;
139
140 // Skip over any bits that are already consumed.
141 if (WordBitNo) {
142 if (Expected<word_t> Res = Read(WordBitNo))
143 return Error::success();
144 else
145 return Res.takeError();
146 }
147
148 return Error::success();
149 }
150
151 /// Get a pointer into the bitstream at the specified byte offset.
152 const uint8_t *getPointerToByte(uint64_t ByteNo, uint64_t NumBytes) {
153 return BitcodeBytes.data() + ByteNo;
154 }
155
156 /// Get a pointer into the bitstream at the specified bit offset.
157 ///
158 /// The bit offset must be on a byte boundary.
159 const uint8_t *getPointerToBit(uint64_t BitNo, uint64_t NumBytes) {
160 assert(!(BitNo % 8) && "Expected bit on byte boundary")((!(BitNo % 8) && "Expected bit on byte boundary") ? static_cast
<void> (0) : __assert_fail ("!(BitNo % 8) && \"Expected bit on byte boundary\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/Bitstream/BitstreamReader.h"
, 160, __PRETTY_FUNCTION__))
;
161 return getPointerToByte(BitNo / 8, NumBytes);
162 }
163
164 Error fillCurWord() {
165 if (NextChar >= BitcodeBytes.size())
166 return createStringError(std::errc::io_error,
167 "Unexpected end of file reading %u of %u bytes",
168 NextChar, BitcodeBytes.size());
169
170 // Read the next word from the stream.
171 const uint8_t *NextCharPtr = BitcodeBytes.data() + NextChar;
172 unsigned BytesRead;
173 if (BitcodeBytes.size() >= NextChar + sizeof(word_t)) {
174 BytesRead = sizeof(word_t);
175 CurWord =
176 support::endian::read<word_t, support::little, support::unaligned>(
177 NextCharPtr);
178 } else {
179 // Short read.
180 BytesRead = BitcodeBytes.size() - NextChar;
181 CurWord = 0;
182 for (unsigned B = 0; B != BytesRead; ++B)
183 CurWord |= uint64_t(NextCharPtr[B]) << (B * 8);
184 }
185 NextChar += BytesRead;
186 BitsInCurWord = BytesRead * 8;
187 return Error::success();
188 }
189
190 Expected<word_t> Read(unsigned NumBits) {
191 static const unsigned BitsInWord = MaxChunkSize;
192
193 assert(NumBits && NumBits <= BitsInWord &&((NumBits && NumBits <= BitsInWord && "Cannot return zero or more than BitsInWord bits!"
) ? static_cast<void> (0) : __assert_fail ("NumBits && NumBits <= BitsInWord && \"Cannot return zero or more than BitsInWord bits!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/Bitstream/BitstreamReader.h"
, 194, __PRETTY_FUNCTION__))
12
Assuming 'NumBits' is not equal to 0
13
Assuming 'NumBits' is <= 'BitsInWord'
14
'?' condition is true
194 "Cannot return zero or more than BitsInWord bits!")((NumBits && NumBits <= BitsInWord && "Cannot return zero or more than BitsInWord bits!"
) ? static_cast<void> (0) : __assert_fail ("NumBits && NumBits <= BitsInWord && \"Cannot return zero or more than BitsInWord bits!\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include/llvm/Bitstream/BitstreamReader.h"
, 194, __PRETTY_FUNCTION__))
;
195
196 static const unsigned Mask = sizeof(word_t) > 4 ? 0x3f : 0x1f;
15
'?' condition is true
197
198 // If the field is fully contained by CurWord, return it quickly.
199 if (BitsInCurWord >= NumBits) {
16
Assuming 'NumBits' is > field 'BitsInCurWord'
17
Taking false branch
200 word_t R = CurWord & (~word_t(0) >> (BitsInWord - NumBits));
201
202 // Use a mask to avoid undefined behavior.
203 CurWord >>= (NumBits & Mask);
204
205 BitsInCurWord -= NumBits;
206 return R;
207 }
208
209 word_t R = BitsInCurWord
17.1
Field 'BitsInCurWord' is not equal to 0
17.1
Field 'BitsInCurWord' is not equal to 0
? CurWord : 0;
18
'?' condition is true
210 unsigned BitsLeft = NumBits - BitsInCurWord;
211
212 if (Error fillResult = fillCurWord())
19
Assuming the condition is false
20
Taking false branch
213 return std::move(fillResult);
214
215 // If we run out of data, abort.
216 if (BitsLeft > BitsInCurWord)
21
Assuming 'BitsLeft' is <= field 'BitsInCurWord'
22
Taking false branch
217 return createStringError(std::errc::io_error,
218 "Unexpected end of file reading %u of %u bits",
219 BitsInCurWord, BitsLeft);
220
221 word_t R2 = CurWord & (~word_t(0) >> (BitsInWord - BitsLeft));
23
The result of the right shift is undefined due to shifting by '64', which is greater or equal to the width of type 'llvm::SimpleBitstreamCursor::word_t'
222
223 // Use a mask to avoid undefined behavior.
224 CurWord >>= (BitsLeft & Mask);
225
226 BitsInCurWord -= BitsLeft;
227
228 R |= R2 << (NumBits - BitsLeft);
229
230 return R;
231 }
232
233 Expected<uint32_t> ReadVBR(unsigned NumBits) {
234 Expected<unsigned> MaybeRead = Read(NumBits);
235 if (!MaybeRead)
236 return MaybeRead;
237 uint32_t Piece = MaybeRead.get();
238
239 if ((Piece & (1U << (NumBits-1))) == 0)
240 return Piece;
241
242 uint32_t Result = 0;
243 unsigned NextBit = 0;
244 while (true) {
245 Result |= (Piece & ((1U << (NumBits-1))-1)) << NextBit;
246
247 if ((Piece & (1U << (NumBits-1))) == 0)
248 return Result;
249
250 NextBit += NumBits-1;
251 MaybeRead = Read(NumBits);
252 if (!MaybeRead)
253 return MaybeRead;
254 Piece = MaybeRead.get();
255 }
256 }
257
258 // Read a VBR that may have a value up to 64-bits in size. The chunk size of
259 // the VBR must still be <= 32 bits though.
260 Expected<uint64_t> ReadVBR64(unsigned NumBits) {
261 Expected<uint64_t> MaybeRead = Read(NumBits);
262 if (!MaybeRead)
263 return MaybeRead;
264 uint32_t Piece = MaybeRead.get();
265
266 if ((Piece & (1U << (NumBits-1))) == 0)
267 return uint64_t(Piece);
268
269 uint64_t Result = 0;
270 unsigned NextBit = 0;
271 while (true) {
272 Result |= uint64_t(Piece & ((1U << (NumBits-1))-1)) << NextBit;
273
274 if ((Piece & (1U << (NumBits-1))) == 0)
275 return Result;
276
277 NextBit += NumBits-1;
278 MaybeRead = Read(NumBits);
279 if (!MaybeRead)
280 return MaybeRead;
281 Piece = MaybeRead.get();
282 }
283 }
284
285 void SkipToFourByteBoundary() {
286 // If word_t is 64-bits and if we've read less than 32 bits, just dump
287 // the bits we have up to the next 32-bit boundary.
288 if (sizeof(word_t) > 4 &&
289 BitsInCurWord >= 32) {
290 CurWord >>= BitsInCurWord-32;
291 BitsInCurWord = 32;
292 return;
293 }
294
295 BitsInCurWord = 0;
296 }
297
298 /// Return the size of the stream in bytes.
299 size_t SizeInBytes() const { return BitcodeBytes.size(); }
300
301 /// Skip to the end of the file.
302 void skipToEnd() { NextChar = BitcodeBytes.size(); }
303};
304
305/// When advancing through a bitstream cursor, each advance can discover a few
306/// different kinds of entries:
307struct BitstreamEntry {
308 enum {
309 Error, // Malformed bitcode was found.
310 EndBlock, // We've reached the end of the current block, (or the end of the
311 // file, which is treated like a series of EndBlock records.
312 SubBlock, // This is the start of a new subblock of a specific ID.
313 Record // This is a record with a specific AbbrevID.
314 } Kind;
315
316 unsigned ID;
317
318 static BitstreamEntry getError() {
319 BitstreamEntry E; E.Kind = Error; return E;
320 }
321
322 static BitstreamEntry getEndBlock() {
323 BitstreamEntry E; E.Kind = EndBlock; return E;
324 }
325
326 static BitstreamEntry getSubBlock(unsigned ID) {
327 BitstreamEntry E; E.Kind = SubBlock; E.ID = ID; return E;
328 }
329
330 static BitstreamEntry getRecord(unsigned AbbrevID) {
331 BitstreamEntry E; E.Kind = Record; E.ID = AbbrevID; return E;
332 }
333};
334
335/// This represents a position within a bitcode file, implemented on top of a
336/// SimpleBitstreamCursor.
337///
338/// Unlike iterators, BitstreamCursors are heavy-weight objects that should not
339/// be passed by value.
340class BitstreamCursor : SimpleBitstreamCursor {
341 // This is the declared size of code values used for the current block, in
342 // bits.
343 unsigned CurCodeSize = 2;
344
345 /// Abbrevs installed at in this block.
346 std::vector<std::shared_ptr<BitCodeAbbrev>> CurAbbrevs;
347
348 struct Block {
349 unsigned PrevCodeSize;
350 std::vector<std::shared_ptr<BitCodeAbbrev>> PrevAbbrevs;
351
352 explicit Block(unsigned PCS) : PrevCodeSize(PCS) {}
353 };
354
355 /// This tracks the codesize of parent blocks.
356 SmallVector<Block, 8> BlockScope;
357
358 BitstreamBlockInfo *BlockInfo = nullptr;
359
360public:
361 static const size_t MaxChunkSize = sizeof(word_t) * 8;
362
363 BitstreamCursor() = default;
364 explicit BitstreamCursor(ArrayRef<uint8_t> BitcodeBytes)
365 : SimpleBitstreamCursor(BitcodeBytes) {}
366 explicit BitstreamCursor(StringRef BitcodeBytes)
367 : SimpleBitstreamCursor(BitcodeBytes) {}
368 explicit BitstreamCursor(MemoryBufferRef BitcodeBytes)
369 : SimpleBitstreamCursor(BitcodeBytes) {}
370
371 using SimpleBitstreamCursor::AtEndOfStream;
372 using SimpleBitstreamCursor::canSkipToPos;
373 using SimpleBitstreamCursor::fillCurWord;
374 using SimpleBitstreamCursor::getBitcodeBytes;
375 using SimpleBitstreamCursor::GetCurrentBitNo;
376 using SimpleBitstreamCursor::getCurrentByteNo;
377 using SimpleBitstreamCursor::getPointerToByte;
378 using SimpleBitstreamCursor::JumpToBit;
379 using SimpleBitstreamCursor::Read;
380 using SimpleBitstreamCursor::ReadVBR;
381 using SimpleBitstreamCursor::ReadVBR64;
382 using SimpleBitstreamCursor::SizeInBytes;
383 using SimpleBitstreamCursor::skipToEnd;
384
385 /// Return the number of bits used to encode an abbrev #.
386 unsigned getAbbrevIDWidth() const { return CurCodeSize; }
387
388 /// Flags that modify the behavior of advance().
389 enum {
390 /// If this flag is used, the advance() method does not automatically pop
391 /// the block scope when the end of a block is reached.
392 AF_DontPopBlockAtEnd = 1,
393
394 /// If this flag is used, abbrev entries are returned just like normal
395 /// records.
396 AF_DontAutoprocessAbbrevs = 2
397 };
398
399 /// Advance the current bitstream, returning the next entry in the stream.
400 Expected<BitstreamEntry> advance(unsigned Flags = 0) {
401 while (true) {
8
Loop condition is true. Entering loop body
402 if (AtEndOfStream())
9
Taking false branch
403 return BitstreamEntry::getError();
404
405 Expected<unsigned> MaybeCode = ReadCode();
10
Calling 'BitstreamCursor::ReadCode'
406 if (!MaybeCode)
407 return MaybeCode.takeError();
408 unsigned Code = MaybeCode.get();
409
410 if (Code == bitc::END_BLOCK) {
411 // Pop the end of the block unless Flags tells us not to.
412 if (!(Flags & AF_DontPopBlockAtEnd) && ReadBlockEnd())
413 return BitstreamEntry::getError();
414 return BitstreamEntry::getEndBlock();
415 }
416
417 if (Code == bitc::ENTER_SUBBLOCK) {
418 if (Expected<unsigned> MaybeSubBlock = ReadSubBlockID())
419 return BitstreamEntry::getSubBlock(MaybeSubBlock.get());
420 else
421 return MaybeSubBlock.takeError();
422 }
423
424 if (Code == bitc::DEFINE_ABBREV &&
425 !(Flags & AF_DontAutoprocessAbbrevs)) {
426 // We read and accumulate abbrev's, the client can't do anything with
427 // them anyway.
428 if (Error Err = ReadAbbrevRecord())
429 return std::move(Err);
430 continue;
431 }
432
433 return BitstreamEntry::getRecord(Code);
434 }
435 }
436
437 /// This is a convenience function for clients that don't expect any
438 /// subblocks. This just skips over them automatically.
439 Expected<BitstreamEntry> advanceSkippingSubblocks(unsigned Flags = 0) {
440 while (true) {
6
Loop condition is true. Entering loop body
441 // If we found a normal entry, return it.
442 Expected<BitstreamEntry> MaybeEntry = advance(Flags);
7
Calling 'BitstreamCursor::advance'
443 if (!MaybeEntry)
444 return MaybeEntry;
445 BitstreamEntry Entry = MaybeEntry.get();
446
447 if (Entry.Kind != BitstreamEntry::SubBlock)
448 return Entry;
449
450 // If we found a sub-block, just skip over it and check the next entry.
451 if (Error Err = SkipBlock())
452 return std::move(Err);
453 }
454 }
455
456 Expected<unsigned> ReadCode() { return Read(CurCodeSize); }
11
Calling 'SimpleBitstreamCursor::Read'
457
458 // Block header:
459 // [ENTER_SUBBLOCK, blockid, newcodelen, <align4bytes>, blocklen]
460
461 /// Having read the ENTER_SUBBLOCK code, read the BlockID for the block.
462 Expected<unsigned> ReadSubBlockID() { return ReadVBR(bitc::BlockIDWidth); }
463
464 /// Having read the ENTER_SUBBLOCK abbrevid and a BlockID, skip over the body
465 /// of this block.
466 Error SkipBlock() {
467 // Read and ignore the codelen value.
468 if (Expected<uint32_t> Res = ReadVBR(bitc::CodeLenWidth))
469 ; // Since we are skipping this block, we don't care what code widths are
470 // used inside of it.
471 else
472 return Res.takeError();
473
474 SkipToFourByteBoundary();
475 Expected<unsigned> MaybeNum = Read(bitc::BlockSizeWidth);
476 if (!MaybeNum)
477 return MaybeNum.takeError();
478 size_t NumFourBytes = MaybeNum.get();
479
480 // Check that the block wasn't partially defined, and that the offset isn't
481 // bogus.
482 size_t SkipTo = GetCurrentBitNo() + NumFourBytes * 4 * 8;
483 if (AtEndOfStream())
484 return createStringError(std::errc::illegal_byte_sequence,
485 "can't skip block: already at end of stream");
486 if (!canSkipToPos(SkipTo / 8))
487 return createStringError(std::errc::illegal_byte_sequence,
488 "can't skip to bit %zu from %" PRIu64"l" "u", SkipTo,
489 GetCurrentBitNo());
490
491 if (Error Res = JumpToBit(SkipTo))
492 return Res;
493
494 return Error::success();
495 }
496
497 /// Having read the ENTER_SUBBLOCK abbrevid, and enter the block.
498 Error EnterSubBlock(unsigned BlockID, unsigned *NumWordsP = nullptr);
499
500 bool ReadBlockEnd() {
501 if (BlockScope.empty()) return true;
502
503 // Block tail:
504 // [END_BLOCK, <align4bytes>]
505 SkipToFourByteBoundary();
506
507 popBlockScope();
508 return false;
509 }
510
511private:
512 void popBlockScope() {
513 CurCodeSize = BlockScope.back().PrevCodeSize;
514
515 CurAbbrevs = std::move(BlockScope.back().PrevAbbrevs);
516 BlockScope.pop_back();
517 }
518
519 //===--------------------------------------------------------------------===//
520 // Record Processing
521 //===--------------------------------------------------------------------===//
522
523public:
524 /// Return the abbreviation for the specified AbbrevId.
525 const BitCodeAbbrev *getAbbrev(unsigned AbbrevID) {
526 unsigned AbbrevNo = AbbrevID - bitc::FIRST_APPLICATION_ABBREV;
527 if (AbbrevNo >= CurAbbrevs.size())
528 report_fatal_error("Invalid abbrev number");
529 return CurAbbrevs[AbbrevNo].get();
530 }
531
532 /// Read the current record and discard it, returning the code for the record.
533 Expected<unsigned> skipRecord(unsigned AbbrevID);
534
535 Expected<unsigned> readRecord(unsigned AbbrevID,
536 SmallVectorImpl<uint64_t> &Vals,
537 StringRef *Blob = nullptr);
538
539 //===--------------------------------------------------------------------===//
540 // Abbrev Processing
541 //===--------------------------------------------------------------------===//
542 Error ReadAbbrevRecord();
543
544 /// Read and return a block info block from the bitstream. If an error was
545 /// encountered, return None.
546 ///
547 /// \param ReadBlockInfoNames Whether to read block/record name information in
548 /// the BlockInfo block. Only llvm-bcanalyzer uses this.
549 Expected<Optional<BitstreamBlockInfo>>
550 ReadBlockInfoBlock(bool ReadBlockInfoNames = false);
551
552 /// Set the block info to be used by this BitstreamCursor to interpret
553 /// abbreviated records.
554 void setBlockInfo(BitstreamBlockInfo *BI) { BlockInfo = BI; }
555};
556
557} // end llvm namespace
558
559#endif // LLVM_BITSTREAM_BITSTREAMREADER_H