10b57cec5SDimitry Andric //===- MetadataLoader.cpp - Internal BitcodeReader implementation ---------===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric 90b57cec5SDimitry Andric #include "MetadataLoader.h" 100b57cec5SDimitry Andric #include "ValueList.h" 110b57cec5SDimitry Andric 120b57cec5SDimitry Andric #include "llvm/ADT/APInt.h" 130b57cec5SDimitry Andric #include "llvm/ADT/ArrayRef.h" 1481ad6265SDimitry Andric #include "llvm/ADT/BitmaskEnum.h" 150b57cec5SDimitry Andric #include "llvm/ADT/DenseMap.h" 160b57cec5SDimitry Andric #include "llvm/ADT/DenseSet.h" 1781ad6265SDimitry Andric #include "llvm/ADT/STLFunctionalExtras.h" 18*fe013be4SDimitry Andric #include "llvm/ADT/SetVector.h" 190b57cec5SDimitry Andric #include "llvm/ADT/SmallString.h" 2081ad6265SDimitry Andric #include "llvm/ADT/SmallVector.h" 210b57cec5SDimitry Andric #include "llvm/ADT/Statistic.h" 220b57cec5SDimitry Andric #include "llvm/ADT/StringRef.h" 230b57cec5SDimitry Andric #include "llvm/ADT/Twine.h" 2481ad6265SDimitry Andric #include "llvm/ADT/ilist_iterator.h" 2581ad6265SDimitry Andric #include "llvm/ADT/iterator_range.h" 2681ad6265SDimitry Andric #include "llvm/BinaryFormat/Dwarf.h" 270b57cec5SDimitry Andric #include "llvm/Bitcode/BitcodeReader.h" 280b57cec5SDimitry Andric #include "llvm/Bitcode/LLVMBitCodes.h" 29349cc55cSDimitry Andric #include "llvm/Bitstream/BitstreamReader.h" 300b57cec5SDimitry Andric #include "llvm/IR/AutoUpgrade.h" 310b57cec5SDimitry Andric #include "llvm/IR/BasicBlock.h" 320b57cec5SDimitry Andric #include "llvm/IR/Constants.h" 330b57cec5SDimitry Andric #include "llvm/IR/DebugInfoMetadata.h" 340b57cec5SDimitry Andric #include "llvm/IR/Function.h" 350b57cec5SDimitry Andric #include "llvm/IR/GlobalObject.h" 360b57cec5SDimitry Andric #include "llvm/IR/GlobalVariable.h" 370b57cec5SDimitry Andric #include "llvm/IR/Instruction.h" 380b57cec5SDimitry Andric #include "llvm/IR/IntrinsicInst.h" 390b57cec5SDimitry Andric #include "llvm/IR/LLVMContext.h" 4081ad6265SDimitry Andric #include "llvm/IR/Metadata.h" 410b57cec5SDimitry Andric #include "llvm/IR/Module.h" 420b57cec5SDimitry Andric #include "llvm/IR/TrackingMDRef.h" 430b57cec5SDimitry Andric #include "llvm/IR/Type.h" 440b57cec5SDimitry Andric #include "llvm/Support/Casting.h" 450b57cec5SDimitry Andric #include "llvm/Support/CommandLine.h" 460b57cec5SDimitry Andric #include "llvm/Support/Compiler.h" 470b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h" 4881ad6265SDimitry Andric #include "llvm/Support/type_traits.h" 4981ad6265SDimitry Andric 500b57cec5SDimitry Andric #include <algorithm> 510b57cec5SDimitry Andric #include <cassert> 520b57cec5SDimitry Andric #include <cstddef> 530b57cec5SDimitry Andric #include <cstdint> 540b57cec5SDimitry Andric #include <deque> 5581ad6265SDimitry Andric #include <iterator> 560b57cec5SDimitry Andric #include <limits> 57*fe013be4SDimitry Andric #include <map> 58bdd1243dSDimitry Andric #include <optional> 590b57cec5SDimitry Andric #include <string> 600b57cec5SDimitry Andric #include <tuple> 6181ad6265SDimitry Andric #include <type_traits> 620b57cec5SDimitry Andric #include <utility> 630b57cec5SDimitry Andric #include <vector> 6481ad6265SDimitry Andric namespace llvm { 6581ad6265SDimitry Andric class Argument; 6681ad6265SDimitry Andric } 670b57cec5SDimitry Andric 680b57cec5SDimitry Andric using namespace llvm; 690b57cec5SDimitry Andric 700b57cec5SDimitry Andric #define DEBUG_TYPE "bitcode-reader" 710b57cec5SDimitry Andric 720b57cec5SDimitry Andric STATISTIC(NumMDStringLoaded, "Number of MDStrings loaded"); 730b57cec5SDimitry Andric STATISTIC(NumMDNodeTemporary, "Number of MDNode::Temporary created"); 740b57cec5SDimitry Andric STATISTIC(NumMDRecordLoaded, "Number of Metadata records loaded"); 750b57cec5SDimitry Andric 760b57cec5SDimitry Andric /// Flag whether we need to import full type definitions for ThinLTO. 770b57cec5SDimitry Andric /// Currently needed for Darwin and LLDB. 780b57cec5SDimitry Andric static cl::opt<bool> ImportFullTypeDefinitions( 790b57cec5SDimitry Andric "import-full-type-definitions", cl::init(false), cl::Hidden, 800b57cec5SDimitry Andric cl::desc("Import full type definitions for ThinLTO.")); 810b57cec5SDimitry Andric 820b57cec5SDimitry Andric static cl::opt<bool> DisableLazyLoading( 830b57cec5SDimitry Andric "disable-ondemand-mds-loading", cl::init(false), cl::Hidden, 840b57cec5SDimitry Andric cl::desc("Force disable the lazy-loading on-demand of metadata when " 850b57cec5SDimitry Andric "loading bitcode for importing.")); 860b57cec5SDimitry Andric 870b57cec5SDimitry Andric namespace { 880b57cec5SDimitry Andric 890b57cec5SDimitry Andric static int64_t unrotateSign(uint64_t U) { return (U & 1) ? ~(U >> 1) : U >> 1; } 900b57cec5SDimitry Andric 910b57cec5SDimitry Andric class BitcodeReaderMetadataList { 920b57cec5SDimitry Andric /// Array of metadata references. 930b57cec5SDimitry Andric /// 940b57cec5SDimitry Andric /// Don't use std::vector here. Some versions of libc++ copy (instead of 950b57cec5SDimitry Andric /// move) on resize, and TrackingMDRef is very expensive to copy. 960b57cec5SDimitry Andric SmallVector<TrackingMDRef, 1> MetadataPtrs; 970b57cec5SDimitry Andric 980b57cec5SDimitry Andric /// The set of indices in MetadataPtrs above of forward references that were 990b57cec5SDimitry Andric /// generated. 1000b57cec5SDimitry Andric SmallDenseSet<unsigned, 1> ForwardReference; 1010b57cec5SDimitry Andric 1020b57cec5SDimitry Andric /// The set of indices in MetadataPtrs above of Metadata that need to be 1030b57cec5SDimitry Andric /// resolved. 1040b57cec5SDimitry Andric SmallDenseSet<unsigned, 1> UnresolvedNodes; 1050b57cec5SDimitry Andric 1060b57cec5SDimitry Andric /// Structures for resolving old type refs. 1070b57cec5SDimitry Andric struct { 1080b57cec5SDimitry Andric SmallDenseMap<MDString *, TempMDTuple, 1> Unknown; 1090b57cec5SDimitry Andric SmallDenseMap<MDString *, DICompositeType *, 1> Final; 1100b57cec5SDimitry Andric SmallDenseMap<MDString *, DICompositeType *, 1> FwdDecls; 1110b57cec5SDimitry Andric SmallVector<std::pair<TrackingMDRef, TempMDTuple>, 1> Arrays; 1120b57cec5SDimitry Andric } OldTypeRefs; 1130b57cec5SDimitry Andric 1140b57cec5SDimitry Andric LLVMContext &Context; 1150b57cec5SDimitry Andric 1160b57cec5SDimitry Andric /// Maximum number of valid references. Forward references exceeding the 1170b57cec5SDimitry Andric /// maximum must be invalid. 1180b57cec5SDimitry Andric unsigned RefsUpperBound; 1190b57cec5SDimitry Andric 1200b57cec5SDimitry Andric public: 1210b57cec5SDimitry Andric BitcodeReaderMetadataList(LLVMContext &C, size_t RefsUpperBound) 1220b57cec5SDimitry Andric : Context(C), 1230b57cec5SDimitry Andric RefsUpperBound(std::min((size_t)std::numeric_limits<unsigned>::max(), 1240b57cec5SDimitry Andric RefsUpperBound)) {} 1250b57cec5SDimitry Andric 1260b57cec5SDimitry Andric // vector compatibility methods 1270b57cec5SDimitry Andric unsigned size() const { return MetadataPtrs.size(); } 1280b57cec5SDimitry Andric void resize(unsigned N) { MetadataPtrs.resize(N); } 1290b57cec5SDimitry Andric void push_back(Metadata *MD) { MetadataPtrs.emplace_back(MD); } 1300b57cec5SDimitry Andric void clear() { MetadataPtrs.clear(); } 1310b57cec5SDimitry Andric Metadata *back() const { return MetadataPtrs.back(); } 1320b57cec5SDimitry Andric void pop_back() { MetadataPtrs.pop_back(); } 1330b57cec5SDimitry Andric bool empty() const { return MetadataPtrs.empty(); } 1340b57cec5SDimitry Andric 1350b57cec5SDimitry Andric Metadata *operator[](unsigned i) const { 1360b57cec5SDimitry Andric assert(i < MetadataPtrs.size()); 1370b57cec5SDimitry Andric return MetadataPtrs[i]; 1380b57cec5SDimitry Andric } 1390b57cec5SDimitry Andric 1400b57cec5SDimitry Andric Metadata *lookup(unsigned I) const { 1410b57cec5SDimitry Andric if (I < MetadataPtrs.size()) 1420b57cec5SDimitry Andric return MetadataPtrs[I]; 1430b57cec5SDimitry Andric return nullptr; 1440b57cec5SDimitry Andric } 1450b57cec5SDimitry Andric 1460b57cec5SDimitry Andric void shrinkTo(unsigned N) { 1470b57cec5SDimitry Andric assert(N <= size() && "Invalid shrinkTo request!"); 1480b57cec5SDimitry Andric assert(ForwardReference.empty() && "Unexpected forward refs"); 1490b57cec5SDimitry Andric assert(UnresolvedNodes.empty() && "Unexpected unresolved node"); 1500b57cec5SDimitry Andric MetadataPtrs.resize(N); 1510b57cec5SDimitry Andric } 1520b57cec5SDimitry Andric 1530b57cec5SDimitry Andric /// Return the given metadata, creating a replaceable forward reference if 1540b57cec5SDimitry Andric /// necessary. 1550b57cec5SDimitry Andric Metadata *getMetadataFwdRef(unsigned Idx); 1560b57cec5SDimitry Andric 1570b57cec5SDimitry Andric /// Return the given metadata only if it is fully resolved. 1580b57cec5SDimitry Andric /// 1590b57cec5SDimitry Andric /// Gives the same result as \a lookup(), unless \a MDNode::isResolved() 1600b57cec5SDimitry Andric /// would give \c false. 1610b57cec5SDimitry Andric Metadata *getMetadataIfResolved(unsigned Idx); 1620b57cec5SDimitry Andric 1630b57cec5SDimitry Andric MDNode *getMDNodeFwdRefOrNull(unsigned Idx); 1640b57cec5SDimitry Andric void assignValue(Metadata *MD, unsigned Idx); 1650b57cec5SDimitry Andric void tryToResolveCycles(); 1660b57cec5SDimitry Andric bool hasFwdRefs() const { return !ForwardReference.empty(); } 1670b57cec5SDimitry Andric int getNextFwdRef() { 1680b57cec5SDimitry Andric assert(hasFwdRefs()); 1690b57cec5SDimitry Andric return *ForwardReference.begin(); 1700b57cec5SDimitry Andric } 1710b57cec5SDimitry Andric 1720b57cec5SDimitry Andric /// Upgrade a type that had an MDString reference. 1730b57cec5SDimitry Andric void addTypeRef(MDString &UUID, DICompositeType &CT); 1740b57cec5SDimitry Andric 1750b57cec5SDimitry Andric /// Upgrade a type that had an MDString reference. 1760b57cec5SDimitry Andric Metadata *upgradeTypeRef(Metadata *MaybeUUID); 1770b57cec5SDimitry Andric 1780b57cec5SDimitry Andric /// Upgrade a type ref array that may have MDString references. 1790b57cec5SDimitry Andric Metadata *upgradeTypeRefArray(Metadata *MaybeTuple); 1800b57cec5SDimitry Andric 1810b57cec5SDimitry Andric private: 1820b57cec5SDimitry Andric Metadata *resolveTypeRefArray(Metadata *MaybeTuple); 1830b57cec5SDimitry Andric }; 1840b57cec5SDimitry Andric 1850b57cec5SDimitry Andric void BitcodeReaderMetadataList::assignValue(Metadata *MD, unsigned Idx) { 1860b57cec5SDimitry Andric if (auto *MDN = dyn_cast<MDNode>(MD)) 1870b57cec5SDimitry Andric if (!MDN->isResolved()) 1880b57cec5SDimitry Andric UnresolvedNodes.insert(Idx); 1890b57cec5SDimitry Andric 1900b57cec5SDimitry Andric if (Idx == size()) { 1910b57cec5SDimitry Andric push_back(MD); 1920b57cec5SDimitry Andric return; 1930b57cec5SDimitry Andric } 1940b57cec5SDimitry Andric 1950b57cec5SDimitry Andric if (Idx >= size()) 1960b57cec5SDimitry Andric resize(Idx + 1); 1970b57cec5SDimitry Andric 1980b57cec5SDimitry Andric TrackingMDRef &OldMD = MetadataPtrs[Idx]; 1990b57cec5SDimitry Andric if (!OldMD) { 2000b57cec5SDimitry Andric OldMD.reset(MD); 2010b57cec5SDimitry Andric return; 2020b57cec5SDimitry Andric } 2030b57cec5SDimitry Andric 2040b57cec5SDimitry Andric // If there was a forward reference to this value, replace it. 2050b57cec5SDimitry Andric TempMDTuple PrevMD(cast<MDTuple>(OldMD.get())); 2060b57cec5SDimitry Andric PrevMD->replaceAllUsesWith(MD); 2070b57cec5SDimitry Andric ForwardReference.erase(Idx); 2080b57cec5SDimitry Andric } 2090b57cec5SDimitry Andric 2100b57cec5SDimitry Andric Metadata *BitcodeReaderMetadataList::getMetadataFwdRef(unsigned Idx) { 2110b57cec5SDimitry Andric // Bail out for a clearly invalid value. 2120b57cec5SDimitry Andric if (Idx >= RefsUpperBound) 2130b57cec5SDimitry Andric return nullptr; 2140b57cec5SDimitry Andric 2150b57cec5SDimitry Andric if (Idx >= size()) 2160b57cec5SDimitry Andric resize(Idx + 1); 2170b57cec5SDimitry Andric 2180b57cec5SDimitry Andric if (Metadata *MD = MetadataPtrs[Idx]) 2190b57cec5SDimitry Andric return MD; 2200b57cec5SDimitry Andric 2210b57cec5SDimitry Andric // Track forward refs to be resolved later. 2220b57cec5SDimitry Andric ForwardReference.insert(Idx); 2230b57cec5SDimitry Andric 2240b57cec5SDimitry Andric // Create and return a placeholder, which will later be RAUW'd. 2250b57cec5SDimitry Andric ++NumMDNodeTemporary; 226bdd1243dSDimitry Andric Metadata *MD = MDNode::getTemporary(Context, std::nullopt).release(); 2270b57cec5SDimitry Andric MetadataPtrs[Idx].reset(MD); 2280b57cec5SDimitry Andric return MD; 2290b57cec5SDimitry Andric } 2300b57cec5SDimitry Andric 2310b57cec5SDimitry Andric Metadata *BitcodeReaderMetadataList::getMetadataIfResolved(unsigned Idx) { 2320b57cec5SDimitry Andric Metadata *MD = lookup(Idx); 2330b57cec5SDimitry Andric if (auto *N = dyn_cast_or_null<MDNode>(MD)) 2340b57cec5SDimitry Andric if (!N->isResolved()) 2350b57cec5SDimitry Andric return nullptr; 2360b57cec5SDimitry Andric return MD; 2370b57cec5SDimitry Andric } 2380b57cec5SDimitry Andric 2390b57cec5SDimitry Andric MDNode *BitcodeReaderMetadataList::getMDNodeFwdRefOrNull(unsigned Idx) { 2400b57cec5SDimitry Andric return dyn_cast_or_null<MDNode>(getMetadataFwdRef(Idx)); 2410b57cec5SDimitry Andric } 2420b57cec5SDimitry Andric 2430b57cec5SDimitry Andric void BitcodeReaderMetadataList::tryToResolveCycles() { 2440b57cec5SDimitry Andric if (!ForwardReference.empty()) 2450b57cec5SDimitry Andric // Still forward references... can't resolve cycles. 2460b57cec5SDimitry Andric return; 2470b57cec5SDimitry Andric 2480b57cec5SDimitry Andric // Give up on finding a full definition for any forward decls that remain. 2490b57cec5SDimitry Andric for (const auto &Ref : OldTypeRefs.FwdDecls) 2500b57cec5SDimitry Andric OldTypeRefs.Final.insert(Ref); 2510b57cec5SDimitry Andric OldTypeRefs.FwdDecls.clear(); 2520b57cec5SDimitry Andric 2530b57cec5SDimitry Andric // Upgrade from old type ref arrays. In strange cases, this could add to 2540b57cec5SDimitry Andric // OldTypeRefs.Unknown. 2550b57cec5SDimitry Andric for (const auto &Array : OldTypeRefs.Arrays) 2560b57cec5SDimitry Andric Array.second->replaceAllUsesWith(resolveTypeRefArray(Array.first.get())); 2570b57cec5SDimitry Andric OldTypeRefs.Arrays.clear(); 2580b57cec5SDimitry Andric 2590b57cec5SDimitry Andric // Replace old string-based type refs with the resolved node, if possible. 2600b57cec5SDimitry Andric // If we haven't seen the node, leave it to the verifier to complain about 2610b57cec5SDimitry Andric // the invalid string reference. 2620b57cec5SDimitry Andric for (const auto &Ref : OldTypeRefs.Unknown) { 2630b57cec5SDimitry Andric if (DICompositeType *CT = OldTypeRefs.Final.lookup(Ref.first)) 2640b57cec5SDimitry Andric Ref.second->replaceAllUsesWith(CT); 2650b57cec5SDimitry Andric else 2660b57cec5SDimitry Andric Ref.second->replaceAllUsesWith(Ref.first); 2670b57cec5SDimitry Andric } 2680b57cec5SDimitry Andric OldTypeRefs.Unknown.clear(); 2690b57cec5SDimitry Andric 2700b57cec5SDimitry Andric if (UnresolvedNodes.empty()) 2710b57cec5SDimitry Andric // Nothing to do. 2720b57cec5SDimitry Andric return; 2730b57cec5SDimitry Andric 2740b57cec5SDimitry Andric // Resolve any cycles. 2750b57cec5SDimitry Andric for (unsigned I : UnresolvedNodes) { 2760b57cec5SDimitry Andric auto &MD = MetadataPtrs[I]; 2770b57cec5SDimitry Andric auto *N = dyn_cast_or_null<MDNode>(MD); 2780b57cec5SDimitry Andric if (!N) 2790b57cec5SDimitry Andric continue; 2800b57cec5SDimitry Andric 2810b57cec5SDimitry Andric assert(!N->isTemporary() && "Unexpected forward reference"); 2820b57cec5SDimitry Andric N->resolveCycles(); 2830b57cec5SDimitry Andric } 2840b57cec5SDimitry Andric 2850b57cec5SDimitry Andric // Make sure we return early again until there's another unresolved ref. 2860b57cec5SDimitry Andric UnresolvedNodes.clear(); 2870b57cec5SDimitry Andric } 2880b57cec5SDimitry Andric 2890b57cec5SDimitry Andric void BitcodeReaderMetadataList::addTypeRef(MDString &UUID, 2900b57cec5SDimitry Andric DICompositeType &CT) { 2910b57cec5SDimitry Andric assert(CT.getRawIdentifier() == &UUID && "Mismatched UUID"); 2920b57cec5SDimitry Andric if (CT.isForwardDecl()) 2930b57cec5SDimitry Andric OldTypeRefs.FwdDecls.insert(std::make_pair(&UUID, &CT)); 2940b57cec5SDimitry Andric else 2950b57cec5SDimitry Andric OldTypeRefs.Final.insert(std::make_pair(&UUID, &CT)); 2960b57cec5SDimitry Andric } 2970b57cec5SDimitry Andric 2980b57cec5SDimitry Andric Metadata *BitcodeReaderMetadataList::upgradeTypeRef(Metadata *MaybeUUID) { 2990b57cec5SDimitry Andric auto *UUID = dyn_cast_or_null<MDString>(MaybeUUID); 3000b57cec5SDimitry Andric if (LLVM_LIKELY(!UUID)) 3010b57cec5SDimitry Andric return MaybeUUID; 3020b57cec5SDimitry Andric 3030b57cec5SDimitry Andric if (auto *CT = OldTypeRefs.Final.lookup(UUID)) 3040b57cec5SDimitry Andric return CT; 3050b57cec5SDimitry Andric 3060b57cec5SDimitry Andric auto &Ref = OldTypeRefs.Unknown[UUID]; 3070b57cec5SDimitry Andric if (!Ref) 308bdd1243dSDimitry Andric Ref = MDNode::getTemporary(Context, std::nullopt); 3090b57cec5SDimitry Andric return Ref.get(); 3100b57cec5SDimitry Andric } 3110b57cec5SDimitry Andric 3120b57cec5SDimitry Andric Metadata *BitcodeReaderMetadataList::upgradeTypeRefArray(Metadata *MaybeTuple) { 3130b57cec5SDimitry Andric auto *Tuple = dyn_cast_or_null<MDTuple>(MaybeTuple); 3140b57cec5SDimitry Andric if (!Tuple || Tuple->isDistinct()) 3150b57cec5SDimitry Andric return MaybeTuple; 3160b57cec5SDimitry Andric 3170b57cec5SDimitry Andric // Look through the array immediately if possible. 3180b57cec5SDimitry Andric if (!Tuple->isTemporary()) 3190b57cec5SDimitry Andric return resolveTypeRefArray(Tuple); 3200b57cec5SDimitry Andric 3210b57cec5SDimitry Andric // Create and return a placeholder to use for now. Eventually 3220b57cec5SDimitry Andric // resolveTypeRefArrays() will be resolve this forward reference. 3230b57cec5SDimitry Andric OldTypeRefs.Arrays.emplace_back( 3240b57cec5SDimitry Andric std::piecewise_construct, std::forward_as_tuple(Tuple), 325bdd1243dSDimitry Andric std::forward_as_tuple(MDTuple::getTemporary(Context, std::nullopt))); 3260b57cec5SDimitry Andric return OldTypeRefs.Arrays.back().second.get(); 3270b57cec5SDimitry Andric } 3280b57cec5SDimitry Andric 3290b57cec5SDimitry Andric Metadata *BitcodeReaderMetadataList::resolveTypeRefArray(Metadata *MaybeTuple) { 3300b57cec5SDimitry Andric auto *Tuple = dyn_cast_or_null<MDTuple>(MaybeTuple); 3310b57cec5SDimitry Andric if (!Tuple || Tuple->isDistinct()) 3320b57cec5SDimitry Andric return MaybeTuple; 3330b57cec5SDimitry Andric 3340b57cec5SDimitry Andric // Look through the DITypeRefArray, upgrading each DIType *. 3350b57cec5SDimitry Andric SmallVector<Metadata *, 32> Ops; 3360b57cec5SDimitry Andric Ops.reserve(Tuple->getNumOperands()); 3370b57cec5SDimitry Andric for (Metadata *MD : Tuple->operands()) 3380b57cec5SDimitry Andric Ops.push_back(upgradeTypeRef(MD)); 3390b57cec5SDimitry Andric 3400b57cec5SDimitry Andric return MDTuple::get(Context, Ops); 3410b57cec5SDimitry Andric } 3420b57cec5SDimitry Andric 3430b57cec5SDimitry Andric namespace { 3440b57cec5SDimitry Andric 3450b57cec5SDimitry Andric class PlaceholderQueue { 3460b57cec5SDimitry Andric // Placeholders would thrash around when moved, so store in a std::deque 3470b57cec5SDimitry Andric // instead of some sort of vector. 3480b57cec5SDimitry Andric std::deque<DistinctMDOperandPlaceholder> PHs; 3490b57cec5SDimitry Andric 3500b57cec5SDimitry Andric public: 3510b57cec5SDimitry Andric ~PlaceholderQueue() { 352349cc55cSDimitry Andric assert(empty() && 353349cc55cSDimitry Andric "PlaceholderQueue hasn't been flushed before being destroyed"); 3540b57cec5SDimitry Andric } 355e8d8bef9SDimitry Andric bool empty() const { return PHs.empty(); } 3560b57cec5SDimitry Andric DistinctMDOperandPlaceholder &getPlaceholderOp(unsigned ID); 3570b57cec5SDimitry Andric void flush(BitcodeReaderMetadataList &MetadataList); 3580b57cec5SDimitry Andric 3590b57cec5SDimitry Andric /// Return the list of temporaries nodes in the queue, these need to be 3600b57cec5SDimitry Andric /// loaded before we can flush the queue. 3610b57cec5SDimitry Andric void getTemporaries(BitcodeReaderMetadataList &MetadataList, 3620b57cec5SDimitry Andric DenseSet<unsigned> &Temporaries) { 3630b57cec5SDimitry Andric for (auto &PH : PHs) { 3640b57cec5SDimitry Andric auto ID = PH.getID(); 3650b57cec5SDimitry Andric auto *MD = MetadataList.lookup(ID); 3660b57cec5SDimitry Andric if (!MD) { 3670b57cec5SDimitry Andric Temporaries.insert(ID); 3680b57cec5SDimitry Andric continue; 3690b57cec5SDimitry Andric } 3700b57cec5SDimitry Andric auto *N = dyn_cast_or_null<MDNode>(MD); 3710b57cec5SDimitry Andric if (N && N->isTemporary()) 3720b57cec5SDimitry Andric Temporaries.insert(ID); 3730b57cec5SDimitry Andric } 3740b57cec5SDimitry Andric } 3750b57cec5SDimitry Andric }; 3760b57cec5SDimitry Andric 3770b57cec5SDimitry Andric } // end anonymous namespace 3780b57cec5SDimitry Andric 3790b57cec5SDimitry Andric DistinctMDOperandPlaceholder &PlaceholderQueue::getPlaceholderOp(unsigned ID) { 3800b57cec5SDimitry Andric PHs.emplace_back(ID); 3810b57cec5SDimitry Andric return PHs.back(); 3820b57cec5SDimitry Andric } 3830b57cec5SDimitry Andric 3840b57cec5SDimitry Andric void PlaceholderQueue::flush(BitcodeReaderMetadataList &MetadataList) { 3850b57cec5SDimitry Andric while (!PHs.empty()) { 3860b57cec5SDimitry Andric auto *MD = MetadataList.lookup(PHs.front().getID()); 3870b57cec5SDimitry Andric assert(MD && "Flushing placeholder on unassigned MD"); 3880b57cec5SDimitry Andric #ifndef NDEBUG 3890b57cec5SDimitry Andric if (auto *MDN = dyn_cast<MDNode>(MD)) 3900b57cec5SDimitry Andric assert(MDN->isResolved() && 3910b57cec5SDimitry Andric "Flushing Placeholder while cycles aren't resolved"); 3920b57cec5SDimitry Andric #endif 3930b57cec5SDimitry Andric PHs.front().replaceUseWith(MD); 3940b57cec5SDimitry Andric PHs.pop_front(); 3950b57cec5SDimitry Andric } 3960b57cec5SDimitry Andric } 3970b57cec5SDimitry Andric 398480093f4SDimitry Andric } // anonymous namespace 3990b57cec5SDimitry Andric 4000b57cec5SDimitry Andric static Error error(const Twine &Message) { 4010b57cec5SDimitry Andric return make_error<StringError>( 4020b57cec5SDimitry Andric Message, make_error_code(BitcodeError::CorruptedBitcode)); 4030b57cec5SDimitry Andric } 4040b57cec5SDimitry Andric 4050b57cec5SDimitry Andric class MetadataLoader::MetadataLoaderImpl { 4060b57cec5SDimitry Andric BitcodeReaderMetadataList MetadataList; 4070b57cec5SDimitry Andric BitcodeReaderValueList &ValueList; 4080b57cec5SDimitry Andric BitstreamCursor &Stream; 4090b57cec5SDimitry Andric LLVMContext &Context; 4100b57cec5SDimitry Andric Module &TheModule; 411bdd1243dSDimitry Andric MetadataLoaderCallbacks Callbacks; 4120b57cec5SDimitry Andric 4130b57cec5SDimitry Andric /// Cursor associated with the lazy-loading of Metadata. This is the easy way 4140b57cec5SDimitry Andric /// to keep around the right "context" (Abbrev list) to be able to jump in 4150b57cec5SDimitry Andric /// the middle of the metadata block and load any record. 4160b57cec5SDimitry Andric BitstreamCursor IndexCursor; 4170b57cec5SDimitry Andric 4180b57cec5SDimitry Andric /// Index that keeps track of MDString values. 4190b57cec5SDimitry Andric std::vector<StringRef> MDStringRef; 4200b57cec5SDimitry Andric 4210b57cec5SDimitry Andric /// On-demand loading of a single MDString. Requires the index above to be 4220b57cec5SDimitry Andric /// populated. 4230b57cec5SDimitry Andric MDString *lazyLoadOneMDString(unsigned Idx); 4240b57cec5SDimitry Andric 4250b57cec5SDimitry Andric /// Index that keeps track of where to find a metadata record in the stream. 4260b57cec5SDimitry Andric std::vector<uint64_t> GlobalMetadataBitPosIndex; 4270b57cec5SDimitry Andric 428e8d8bef9SDimitry Andric /// Cursor position of the start of the global decl attachments, to enable 429e8d8bef9SDimitry Andric /// loading using the index built for lazy loading, instead of forward 430e8d8bef9SDimitry Andric /// references. 431e8d8bef9SDimitry Andric uint64_t GlobalDeclAttachmentPos = 0; 432e8d8bef9SDimitry Andric 433e8d8bef9SDimitry Andric #ifndef NDEBUG 4344824e7fdSDimitry Andric /// Baisic correctness check that we end up parsing all of the global decl 4354824e7fdSDimitry Andric /// attachments. 436e8d8bef9SDimitry Andric unsigned NumGlobalDeclAttachSkipped = 0; 437e8d8bef9SDimitry Andric unsigned NumGlobalDeclAttachParsed = 0; 438e8d8bef9SDimitry Andric #endif 439e8d8bef9SDimitry Andric 440e8d8bef9SDimitry Andric /// Load the global decl attachments, using the index built for lazy loading. 441e8d8bef9SDimitry Andric Expected<bool> loadGlobalDeclAttachments(); 442e8d8bef9SDimitry Andric 4430b57cec5SDimitry Andric /// Populate the index above to enable lazily loading of metadata, and load 4440b57cec5SDimitry Andric /// the named metadata as well as the transitively referenced global 4450b57cec5SDimitry Andric /// Metadata. 4460b57cec5SDimitry Andric Expected<bool> lazyLoadModuleMetadataBlock(); 4470b57cec5SDimitry Andric 4480b57cec5SDimitry Andric /// On-demand loading of a single metadata. Requires the index above to be 4490b57cec5SDimitry Andric /// populated. 4500b57cec5SDimitry Andric void lazyLoadOneMetadata(unsigned Idx, PlaceholderQueue &Placeholders); 4510b57cec5SDimitry Andric 4520b57cec5SDimitry Andric // Keep mapping of seens pair of old-style CU <-> SP, and update pointers to 4530b57cec5SDimitry Andric // point from SP to CU after a block is completly parsed. 4540b57cec5SDimitry Andric std::vector<std::pair<DICompileUnit *, Metadata *>> CUSubprograms; 4550b57cec5SDimitry Andric 4560b57cec5SDimitry Andric /// Functions that need to be matched with subprograms when upgrading old 4570b57cec5SDimitry Andric /// metadata. 4580b57cec5SDimitry Andric SmallDenseMap<Function *, DISubprogram *, 16> FunctionsWithSPs; 4590b57cec5SDimitry Andric 4600b57cec5SDimitry Andric // Map the bitcode's custom MDKind ID to the Module's MDKind ID. 4610b57cec5SDimitry Andric DenseMap<unsigned, unsigned> MDKindMap; 4620b57cec5SDimitry Andric 4630b57cec5SDimitry Andric bool StripTBAA = false; 4640b57cec5SDimitry Andric bool HasSeenOldLoopTags = false; 4650b57cec5SDimitry Andric bool NeedUpgradeToDIGlobalVariableExpression = false; 4660b57cec5SDimitry Andric bool NeedDeclareExpressionUpgrade = false; 4670b57cec5SDimitry Andric 468*fe013be4SDimitry Andric /// Map DILocalScope to the enclosing DISubprogram, if any. 469*fe013be4SDimitry Andric DenseMap<DILocalScope *, DISubprogram *> ParentSubprogram; 470*fe013be4SDimitry Andric 4710b57cec5SDimitry Andric /// True if metadata is being parsed for a module being ThinLTO imported. 4720b57cec5SDimitry Andric bool IsImporting = false; 4730b57cec5SDimitry Andric 4740b57cec5SDimitry Andric Error parseOneMetadata(SmallVectorImpl<uint64_t> &Record, unsigned Code, 4750b57cec5SDimitry Andric PlaceholderQueue &Placeholders, StringRef Blob, 4760b57cec5SDimitry Andric unsigned &NextMetadataNo); 4770b57cec5SDimitry Andric Error parseMetadataStrings(ArrayRef<uint64_t> Record, StringRef Blob, 4780b57cec5SDimitry Andric function_ref<void(StringRef)> CallBack); 4790b57cec5SDimitry Andric Error parseGlobalObjectAttachment(GlobalObject &GO, 4800b57cec5SDimitry Andric ArrayRef<uint64_t> Record); 4810b57cec5SDimitry Andric Error parseMetadataKindRecord(SmallVectorImpl<uint64_t> &Record); 4820b57cec5SDimitry Andric 4830b57cec5SDimitry Andric void resolveForwardRefsAndPlaceholders(PlaceholderQueue &Placeholders); 4840b57cec5SDimitry Andric 4850b57cec5SDimitry Andric /// Upgrade old-style CU <-> SP pointers to point from SP to CU. 4860b57cec5SDimitry Andric void upgradeCUSubprograms() { 4870b57cec5SDimitry Andric for (auto CU_SP : CUSubprograms) 4880b57cec5SDimitry Andric if (auto *SPs = dyn_cast_or_null<MDTuple>(CU_SP.second)) 4890b57cec5SDimitry Andric for (auto &Op : SPs->operands()) 4900b57cec5SDimitry Andric if (auto *SP = dyn_cast_or_null<DISubprogram>(Op)) 4910b57cec5SDimitry Andric SP->replaceUnit(CU_SP.first); 4920b57cec5SDimitry Andric CUSubprograms.clear(); 4930b57cec5SDimitry Andric } 4940b57cec5SDimitry Andric 4950b57cec5SDimitry Andric /// Upgrade old-style bare DIGlobalVariables to DIGlobalVariableExpressions. 4960b57cec5SDimitry Andric void upgradeCUVariables() { 4970b57cec5SDimitry Andric if (!NeedUpgradeToDIGlobalVariableExpression) 4980b57cec5SDimitry Andric return; 4990b57cec5SDimitry Andric 5000b57cec5SDimitry Andric // Upgrade list of variables attached to the CUs. 5010b57cec5SDimitry Andric if (NamedMDNode *CUNodes = TheModule.getNamedMetadata("llvm.dbg.cu")) 5020b57cec5SDimitry Andric for (unsigned I = 0, E = CUNodes->getNumOperands(); I != E; ++I) { 5030b57cec5SDimitry Andric auto *CU = cast<DICompileUnit>(CUNodes->getOperand(I)); 5040b57cec5SDimitry Andric if (auto *GVs = dyn_cast_or_null<MDTuple>(CU->getRawGlobalVariables())) 5050b57cec5SDimitry Andric for (unsigned I = 0; I < GVs->getNumOperands(); I++) 5060b57cec5SDimitry Andric if (auto *GV = 5070b57cec5SDimitry Andric dyn_cast_or_null<DIGlobalVariable>(GVs->getOperand(I))) { 5080b57cec5SDimitry Andric auto *DGVE = DIGlobalVariableExpression::getDistinct( 5090b57cec5SDimitry Andric Context, GV, DIExpression::get(Context, {})); 5100b57cec5SDimitry Andric GVs->replaceOperandWith(I, DGVE); 5110b57cec5SDimitry Andric } 5120b57cec5SDimitry Andric } 5130b57cec5SDimitry Andric 5140b57cec5SDimitry Andric // Upgrade variables attached to globals. 5150b57cec5SDimitry Andric for (auto &GV : TheModule.globals()) { 5160b57cec5SDimitry Andric SmallVector<MDNode *, 1> MDs; 5170b57cec5SDimitry Andric GV.getMetadata(LLVMContext::MD_dbg, MDs); 5180b57cec5SDimitry Andric GV.eraseMetadata(LLVMContext::MD_dbg); 5190b57cec5SDimitry Andric for (auto *MD : MDs) 5208bcb0991SDimitry Andric if (auto *DGV = dyn_cast<DIGlobalVariable>(MD)) { 5210b57cec5SDimitry Andric auto *DGVE = DIGlobalVariableExpression::getDistinct( 5220b57cec5SDimitry Andric Context, DGV, DIExpression::get(Context, {})); 5230b57cec5SDimitry Andric GV.addMetadata(LLVMContext::MD_dbg, *DGVE); 5240b57cec5SDimitry Andric } else 5250b57cec5SDimitry Andric GV.addMetadata(LLVMContext::MD_dbg, *MD); 5260b57cec5SDimitry Andric } 5270b57cec5SDimitry Andric } 5280b57cec5SDimitry Andric 529*fe013be4SDimitry Andric DISubprogram *findEnclosingSubprogram(DILocalScope *S) { 530*fe013be4SDimitry Andric if (!S) 531*fe013be4SDimitry Andric return nullptr; 532*fe013be4SDimitry Andric if (auto *SP = ParentSubprogram[S]) { 533*fe013be4SDimitry Andric return SP; 534*fe013be4SDimitry Andric } 535*fe013be4SDimitry Andric 536*fe013be4SDimitry Andric DILocalScope *InitialScope = S; 537*fe013be4SDimitry Andric DenseSet<DILocalScope *> Visited; 538*fe013be4SDimitry Andric while (S && !isa<DISubprogram>(S)) { 539*fe013be4SDimitry Andric S = dyn_cast_or_null<DILocalScope>(S->getScope()); 540*fe013be4SDimitry Andric if (Visited.contains(S)) 541*fe013be4SDimitry Andric break; 542*fe013be4SDimitry Andric Visited.insert(S); 543*fe013be4SDimitry Andric } 544*fe013be4SDimitry Andric ParentSubprogram[InitialScope] = llvm::dyn_cast_or_null<DISubprogram>(S); 545*fe013be4SDimitry Andric 546*fe013be4SDimitry Andric return ParentSubprogram[InitialScope]; 547*fe013be4SDimitry Andric } 548*fe013be4SDimitry Andric 549*fe013be4SDimitry Andric /// Move local imports from DICompileUnit's 'imports' field to 550*fe013be4SDimitry Andric /// DISubprogram's retainedNodes. 551*fe013be4SDimitry Andric void upgradeCULocals() { 552*fe013be4SDimitry Andric if (NamedMDNode *CUNodes = TheModule.getNamedMetadata("llvm.dbg.cu")) { 553*fe013be4SDimitry Andric for (unsigned I = 0, E = CUNodes->getNumOperands(); I != E; ++I) { 554*fe013be4SDimitry Andric auto *CU = dyn_cast<DICompileUnit>(CUNodes->getOperand(I)); 555*fe013be4SDimitry Andric if (!CU) 556*fe013be4SDimitry Andric continue; 557*fe013be4SDimitry Andric 558*fe013be4SDimitry Andric if (auto *RawImported = CU->getRawImportedEntities()) { 559*fe013be4SDimitry Andric // Collect a set of imported entities to be moved. 560*fe013be4SDimitry Andric SetVector<Metadata *> EntitiesToRemove; 561*fe013be4SDimitry Andric for (Metadata *Op : CU->getImportedEntities()->operands()) { 562*fe013be4SDimitry Andric auto *IE = cast<DIImportedEntity>(Op); 563*fe013be4SDimitry Andric if (auto *S = dyn_cast_or_null<DILocalScope>(IE->getScope())) { 564*fe013be4SDimitry Andric EntitiesToRemove.insert(IE); 565*fe013be4SDimitry Andric } 566*fe013be4SDimitry Andric } 567*fe013be4SDimitry Andric 568*fe013be4SDimitry Andric if (!EntitiesToRemove.empty()) { 569*fe013be4SDimitry Andric // Make a new list of CU's 'imports'. 570*fe013be4SDimitry Andric SmallVector<Metadata *> NewImports; 571*fe013be4SDimitry Andric for (Metadata *Op : CU->getImportedEntities()->operands()) { 572*fe013be4SDimitry Andric if (!EntitiesToRemove.contains(cast<DIImportedEntity>(Op))) { 573*fe013be4SDimitry Andric NewImports.push_back(Op); 574*fe013be4SDimitry Andric } 575*fe013be4SDimitry Andric } 576*fe013be4SDimitry Andric 577*fe013be4SDimitry Andric // Find DISubprogram corresponding to each entity. 578*fe013be4SDimitry Andric std::map<DISubprogram *, SmallVector<Metadata *>> SPToEntities; 579*fe013be4SDimitry Andric for (auto *I : EntitiesToRemove) { 580*fe013be4SDimitry Andric auto *Entity = cast<DIImportedEntity>(I); 581*fe013be4SDimitry Andric if (auto *SP = findEnclosingSubprogram( 582*fe013be4SDimitry Andric cast<DILocalScope>(Entity->getScope()))) { 583*fe013be4SDimitry Andric SPToEntities[SP].push_back(Entity); 584*fe013be4SDimitry Andric } 585*fe013be4SDimitry Andric } 586*fe013be4SDimitry Andric 587*fe013be4SDimitry Andric // Update DISubprograms' retainedNodes. 588*fe013be4SDimitry Andric for (auto I = SPToEntities.begin(); I != SPToEntities.end(); ++I) { 589*fe013be4SDimitry Andric auto *SP = I->first; 590*fe013be4SDimitry Andric auto RetainedNodes = SP->getRetainedNodes(); 591*fe013be4SDimitry Andric SmallVector<Metadata *> MDs(RetainedNodes.begin(), 592*fe013be4SDimitry Andric RetainedNodes.end()); 593*fe013be4SDimitry Andric MDs.append(I->second); 594*fe013be4SDimitry Andric SP->replaceRetainedNodes(MDNode::get(Context, MDs)); 595*fe013be4SDimitry Andric } 596*fe013be4SDimitry Andric 597*fe013be4SDimitry Andric // Remove entities with local scope from CU. 598*fe013be4SDimitry Andric CU->replaceImportedEntities(MDTuple::get(Context, NewImports)); 599*fe013be4SDimitry Andric } 600*fe013be4SDimitry Andric } 601*fe013be4SDimitry Andric } 602*fe013be4SDimitry Andric } 603*fe013be4SDimitry Andric 604*fe013be4SDimitry Andric ParentSubprogram.clear(); 605*fe013be4SDimitry Andric } 606*fe013be4SDimitry Andric 6070b57cec5SDimitry Andric /// Remove a leading DW_OP_deref from DIExpressions in a dbg.declare that 6080b57cec5SDimitry Andric /// describes a function argument. 6090b57cec5SDimitry Andric void upgradeDeclareExpressions(Function &F) { 6100b57cec5SDimitry Andric if (!NeedDeclareExpressionUpgrade) 6110b57cec5SDimitry Andric return; 6120b57cec5SDimitry Andric 6130b57cec5SDimitry Andric for (auto &BB : F) 6140b57cec5SDimitry Andric for (auto &I : BB) 6150b57cec5SDimitry Andric if (auto *DDI = dyn_cast<DbgDeclareInst>(&I)) 6160b57cec5SDimitry Andric if (auto *DIExpr = DDI->getExpression()) 6170b57cec5SDimitry Andric if (DIExpr->startsWithDeref() && 618349cc55cSDimitry Andric isa_and_nonnull<Argument>(DDI->getAddress())) { 6190b57cec5SDimitry Andric SmallVector<uint64_t, 8> Ops; 6200b57cec5SDimitry Andric Ops.append(std::next(DIExpr->elements_begin()), 6210b57cec5SDimitry Andric DIExpr->elements_end()); 622fe6060f1SDimitry Andric DDI->setExpression(DIExpression::get(Context, Ops)); 6230b57cec5SDimitry Andric } 6240b57cec5SDimitry Andric } 6250b57cec5SDimitry Andric 6260b57cec5SDimitry Andric /// Upgrade the expression from previous versions. 6270b57cec5SDimitry Andric Error upgradeDIExpression(uint64_t FromVersion, 6280b57cec5SDimitry Andric MutableArrayRef<uint64_t> &Expr, 6290b57cec5SDimitry Andric SmallVectorImpl<uint64_t> &Buffer) { 6300b57cec5SDimitry Andric auto N = Expr.size(); 6310b57cec5SDimitry Andric switch (FromVersion) { 6320b57cec5SDimitry Andric default: 6330b57cec5SDimitry Andric return error("Invalid record"); 6340b57cec5SDimitry Andric case 0: 6350b57cec5SDimitry Andric if (N >= 3 && Expr[N - 3] == dwarf::DW_OP_bit_piece) 6360b57cec5SDimitry Andric Expr[N - 3] = dwarf::DW_OP_LLVM_fragment; 637bdd1243dSDimitry Andric [[fallthrough]]; 6380b57cec5SDimitry Andric case 1: 6390b57cec5SDimitry Andric // Move DW_OP_deref to the end. 6400b57cec5SDimitry Andric if (N && Expr[0] == dwarf::DW_OP_deref) { 6410b57cec5SDimitry Andric auto End = Expr.end(); 6420b57cec5SDimitry Andric if (Expr.size() >= 3 && 6430b57cec5SDimitry Andric *std::prev(End, 3) == dwarf::DW_OP_LLVM_fragment) 6440b57cec5SDimitry Andric End = std::prev(End, 3); 6450b57cec5SDimitry Andric std::move(std::next(Expr.begin()), End, Expr.begin()); 6460b57cec5SDimitry Andric *std::prev(End) = dwarf::DW_OP_deref; 6470b57cec5SDimitry Andric } 6480b57cec5SDimitry Andric NeedDeclareExpressionUpgrade = true; 649bdd1243dSDimitry Andric [[fallthrough]]; 6500b57cec5SDimitry Andric case 2: { 6510b57cec5SDimitry Andric // Change DW_OP_plus to DW_OP_plus_uconst. 6520b57cec5SDimitry Andric // Change DW_OP_minus to DW_OP_uconst, DW_OP_minus 6530b57cec5SDimitry Andric auto SubExpr = ArrayRef<uint64_t>(Expr); 6540b57cec5SDimitry Andric while (!SubExpr.empty()) { 6550b57cec5SDimitry Andric // Skip past other operators with their operands 6560b57cec5SDimitry Andric // for this version of the IR, obtained from 6570b57cec5SDimitry Andric // from historic DIExpression::ExprOperand::getSize(). 6580b57cec5SDimitry Andric size_t HistoricSize; 6590b57cec5SDimitry Andric switch (SubExpr.front()) { 6600b57cec5SDimitry Andric default: 6610b57cec5SDimitry Andric HistoricSize = 1; 6620b57cec5SDimitry Andric break; 6630b57cec5SDimitry Andric case dwarf::DW_OP_constu: 6640b57cec5SDimitry Andric case dwarf::DW_OP_minus: 6650b57cec5SDimitry Andric case dwarf::DW_OP_plus: 6660b57cec5SDimitry Andric HistoricSize = 2; 6670b57cec5SDimitry Andric break; 6680b57cec5SDimitry Andric case dwarf::DW_OP_LLVM_fragment: 6690b57cec5SDimitry Andric HistoricSize = 3; 6700b57cec5SDimitry Andric break; 6710b57cec5SDimitry Andric } 6720b57cec5SDimitry Andric 6730b57cec5SDimitry Andric // If the expression is malformed, make sure we don't 6740b57cec5SDimitry Andric // copy more elements than we should. 6750b57cec5SDimitry Andric HistoricSize = std::min(SubExpr.size(), HistoricSize); 6760b57cec5SDimitry Andric ArrayRef<uint64_t> Args = SubExpr.slice(1, HistoricSize - 1); 6770b57cec5SDimitry Andric 6780b57cec5SDimitry Andric switch (SubExpr.front()) { 6790b57cec5SDimitry Andric case dwarf::DW_OP_plus: 6800b57cec5SDimitry Andric Buffer.push_back(dwarf::DW_OP_plus_uconst); 6810b57cec5SDimitry Andric Buffer.append(Args.begin(), Args.end()); 6820b57cec5SDimitry Andric break; 6830b57cec5SDimitry Andric case dwarf::DW_OP_minus: 6840b57cec5SDimitry Andric Buffer.push_back(dwarf::DW_OP_constu); 6850b57cec5SDimitry Andric Buffer.append(Args.begin(), Args.end()); 6860b57cec5SDimitry Andric Buffer.push_back(dwarf::DW_OP_minus); 6870b57cec5SDimitry Andric break; 6880b57cec5SDimitry Andric default: 6890b57cec5SDimitry Andric Buffer.push_back(*SubExpr.begin()); 6900b57cec5SDimitry Andric Buffer.append(Args.begin(), Args.end()); 6910b57cec5SDimitry Andric break; 6920b57cec5SDimitry Andric } 6930b57cec5SDimitry Andric 6940b57cec5SDimitry Andric // Continue with remaining elements. 6950b57cec5SDimitry Andric SubExpr = SubExpr.slice(HistoricSize); 6960b57cec5SDimitry Andric } 6970b57cec5SDimitry Andric Expr = MutableArrayRef<uint64_t>(Buffer); 698bdd1243dSDimitry Andric [[fallthrough]]; 6990b57cec5SDimitry Andric } 7000b57cec5SDimitry Andric case 3: 7010b57cec5SDimitry Andric // Up-to-date! 7020b57cec5SDimitry Andric break; 7030b57cec5SDimitry Andric } 7040b57cec5SDimitry Andric 7050b57cec5SDimitry Andric return Error::success(); 7060b57cec5SDimitry Andric } 7070b57cec5SDimitry Andric 7080b57cec5SDimitry Andric void upgradeDebugInfo() { 7090b57cec5SDimitry Andric upgradeCUSubprograms(); 7100b57cec5SDimitry Andric upgradeCUVariables(); 711*fe013be4SDimitry Andric upgradeCULocals(); 7120b57cec5SDimitry Andric } 7130b57cec5SDimitry Andric 714bdd1243dSDimitry Andric void callMDTypeCallback(Metadata **Val, unsigned TypeID); 715bdd1243dSDimitry Andric 7160b57cec5SDimitry Andric public: 7170b57cec5SDimitry Andric MetadataLoaderImpl(BitstreamCursor &Stream, Module &TheModule, 7180b57cec5SDimitry Andric BitcodeReaderValueList &ValueList, 719bdd1243dSDimitry Andric MetadataLoaderCallbacks Callbacks, bool IsImporting) 7200b57cec5SDimitry Andric : MetadataList(TheModule.getContext(), Stream.SizeInBytes()), 7210b57cec5SDimitry Andric ValueList(ValueList), Stream(Stream), Context(TheModule.getContext()), 722bdd1243dSDimitry Andric TheModule(TheModule), Callbacks(std::move(Callbacks)), 7230b57cec5SDimitry Andric IsImporting(IsImporting) {} 7240b57cec5SDimitry Andric 7250b57cec5SDimitry Andric Error parseMetadata(bool ModuleLevel); 7260b57cec5SDimitry Andric 7270b57cec5SDimitry Andric bool hasFwdRefs() const { return MetadataList.hasFwdRefs(); } 7280b57cec5SDimitry Andric 7290b57cec5SDimitry Andric Metadata *getMetadataFwdRefOrLoad(unsigned ID) { 7300b57cec5SDimitry Andric if (ID < MDStringRef.size()) 7310b57cec5SDimitry Andric return lazyLoadOneMDString(ID); 7320b57cec5SDimitry Andric if (auto *MD = MetadataList.lookup(ID)) 7330b57cec5SDimitry Andric return MD; 7340b57cec5SDimitry Andric // If lazy-loading is enabled, we try recursively to load the operand 7350b57cec5SDimitry Andric // instead of creating a temporary. 7360b57cec5SDimitry Andric if (ID < (MDStringRef.size() + GlobalMetadataBitPosIndex.size())) { 7370b57cec5SDimitry Andric PlaceholderQueue Placeholders; 7380b57cec5SDimitry Andric lazyLoadOneMetadata(ID, Placeholders); 7390b57cec5SDimitry Andric resolveForwardRefsAndPlaceholders(Placeholders); 7400b57cec5SDimitry Andric return MetadataList.lookup(ID); 7410b57cec5SDimitry Andric } 7420b57cec5SDimitry Andric return MetadataList.getMetadataFwdRef(ID); 7430b57cec5SDimitry Andric } 7440b57cec5SDimitry Andric 7450b57cec5SDimitry Andric DISubprogram *lookupSubprogramForFunction(Function *F) { 7460b57cec5SDimitry Andric return FunctionsWithSPs.lookup(F); 7470b57cec5SDimitry Andric } 7480b57cec5SDimitry Andric 749e8d8bef9SDimitry Andric bool hasSeenOldLoopTags() const { return HasSeenOldLoopTags; } 7500b57cec5SDimitry Andric 75181ad6265SDimitry Andric Error parseMetadataAttachment(Function &F, 75281ad6265SDimitry Andric ArrayRef<Instruction *> InstructionList); 7530b57cec5SDimitry Andric 7540b57cec5SDimitry Andric Error parseMetadataKinds(); 7550b57cec5SDimitry Andric 7560b57cec5SDimitry Andric void setStripTBAA(bool Value) { StripTBAA = Value; } 757e8d8bef9SDimitry Andric bool isStrippingTBAA() const { return StripTBAA; } 7580b57cec5SDimitry Andric 7590b57cec5SDimitry Andric unsigned size() const { return MetadataList.size(); } 7600b57cec5SDimitry Andric void shrinkTo(unsigned N) { MetadataList.shrinkTo(N); } 7610b57cec5SDimitry Andric void upgradeDebugIntrinsics(Function &F) { upgradeDeclareExpressions(F); } 7620b57cec5SDimitry Andric }; 7630b57cec5SDimitry Andric 7640b57cec5SDimitry Andric Expected<bool> 7650b57cec5SDimitry Andric MetadataLoader::MetadataLoaderImpl::lazyLoadModuleMetadataBlock() { 7660b57cec5SDimitry Andric IndexCursor = Stream; 7670b57cec5SDimitry Andric SmallVector<uint64_t, 64> Record; 768e8d8bef9SDimitry Andric GlobalDeclAttachmentPos = 0; 7690b57cec5SDimitry Andric // Get the abbrevs, and preload record positions to make them lazy-loadable. 7700b57cec5SDimitry Andric while (true) { 771e8d8bef9SDimitry Andric uint64_t SavedPos = IndexCursor.GetCurrentBitNo(); 772349cc55cSDimitry Andric BitstreamEntry Entry; 773349cc55cSDimitry Andric if (Error E = 774349cc55cSDimitry Andric IndexCursor 775349cc55cSDimitry Andric .advanceSkippingSubblocks(BitstreamCursor::AF_DontPopBlockAtEnd) 776349cc55cSDimitry Andric .moveInto(Entry)) 777349cc55cSDimitry Andric return std::move(E); 7780b57cec5SDimitry Andric 7790b57cec5SDimitry Andric switch (Entry.Kind) { 7800b57cec5SDimitry Andric case BitstreamEntry::SubBlock: // Handled for us already. 7810b57cec5SDimitry Andric case BitstreamEntry::Error: 7820b57cec5SDimitry Andric return error("Malformed block"); 7830b57cec5SDimitry Andric case BitstreamEntry::EndBlock: { 7840b57cec5SDimitry Andric return true; 7850b57cec5SDimitry Andric } 7860b57cec5SDimitry Andric case BitstreamEntry::Record: { 7870b57cec5SDimitry Andric // The interesting case. 7880b57cec5SDimitry Andric ++NumMDRecordLoaded; 7890b57cec5SDimitry Andric uint64_t CurrentPos = IndexCursor.GetCurrentBitNo(); 790349cc55cSDimitry Andric unsigned Code; 791349cc55cSDimitry Andric if (Error E = IndexCursor.skipRecord(Entry.ID).moveInto(Code)) 792349cc55cSDimitry Andric return std::move(E); 7930b57cec5SDimitry Andric switch (Code) { 7940b57cec5SDimitry Andric case bitc::METADATA_STRINGS: { 7950b57cec5SDimitry Andric // Rewind and parse the strings. 7960b57cec5SDimitry Andric if (Error Err = IndexCursor.JumpToBit(CurrentPos)) 7970b57cec5SDimitry Andric return std::move(Err); 7980b57cec5SDimitry Andric StringRef Blob; 7990b57cec5SDimitry Andric Record.clear(); 8000b57cec5SDimitry Andric if (Expected<unsigned> MaybeRecord = 8010b57cec5SDimitry Andric IndexCursor.readRecord(Entry.ID, Record, &Blob)) 8020b57cec5SDimitry Andric ; 8030b57cec5SDimitry Andric else 8040b57cec5SDimitry Andric return MaybeRecord.takeError(); 8050b57cec5SDimitry Andric unsigned NumStrings = Record[0]; 8060b57cec5SDimitry Andric MDStringRef.reserve(NumStrings); 8070b57cec5SDimitry Andric auto IndexNextMDString = [&](StringRef Str) { 8080b57cec5SDimitry Andric MDStringRef.push_back(Str); 8090b57cec5SDimitry Andric }; 8100b57cec5SDimitry Andric if (auto Err = parseMetadataStrings(Record, Blob, IndexNextMDString)) 8110b57cec5SDimitry Andric return std::move(Err); 8120b57cec5SDimitry Andric break; 8130b57cec5SDimitry Andric } 8140b57cec5SDimitry Andric case bitc::METADATA_INDEX_OFFSET: { 8150b57cec5SDimitry Andric // This is the offset to the index, when we see this we skip all the 8160b57cec5SDimitry Andric // records and load only an index to these. 8170b57cec5SDimitry Andric if (Error Err = IndexCursor.JumpToBit(CurrentPos)) 8180b57cec5SDimitry Andric return std::move(Err); 8190b57cec5SDimitry Andric Record.clear(); 8200b57cec5SDimitry Andric if (Expected<unsigned> MaybeRecord = 8210b57cec5SDimitry Andric IndexCursor.readRecord(Entry.ID, Record)) 8220b57cec5SDimitry Andric ; 8230b57cec5SDimitry Andric else 8240b57cec5SDimitry Andric return MaybeRecord.takeError(); 8250b57cec5SDimitry Andric if (Record.size() != 2) 8260b57cec5SDimitry Andric return error("Invalid record"); 8270b57cec5SDimitry Andric auto Offset = Record[0] + (Record[1] << 32); 8280b57cec5SDimitry Andric auto BeginPos = IndexCursor.GetCurrentBitNo(); 8290b57cec5SDimitry Andric if (Error Err = IndexCursor.JumpToBit(BeginPos + Offset)) 8300b57cec5SDimitry Andric return std::move(Err); 8310b57cec5SDimitry Andric Expected<BitstreamEntry> MaybeEntry = 8320b57cec5SDimitry Andric IndexCursor.advanceSkippingSubblocks( 8330b57cec5SDimitry Andric BitstreamCursor::AF_DontPopBlockAtEnd); 8340b57cec5SDimitry Andric if (!MaybeEntry) 8350b57cec5SDimitry Andric return MaybeEntry.takeError(); 8360b57cec5SDimitry Andric Entry = MaybeEntry.get(); 8370b57cec5SDimitry Andric assert(Entry.Kind == BitstreamEntry::Record && 8380b57cec5SDimitry Andric "Corrupted bitcode: Expected `Record` when trying to find the " 8390b57cec5SDimitry Andric "Metadata index"); 8400b57cec5SDimitry Andric Record.clear(); 8410b57cec5SDimitry Andric if (Expected<unsigned> MaybeCode = 8420b57cec5SDimitry Andric IndexCursor.readRecord(Entry.ID, Record)) 8430b57cec5SDimitry Andric assert(MaybeCode.get() == bitc::METADATA_INDEX && 8440b57cec5SDimitry Andric "Corrupted bitcode: Expected `METADATA_INDEX` when trying to " 8450b57cec5SDimitry Andric "find the Metadata index"); 8460b57cec5SDimitry Andric else 8470b57cec5SDimitry Andric return MaybeCode.takeError(); 8480b57cec5SDimitry Andric // Delta unpack 8490b57cec5SDimitry Andric auto CurrentValue = BeginPos; 8500b57cec5SDimitry Andric GlobalMetadataBitPosIndex.reserve(Record.size()); 8510b57cec5SDimitry Andric for (auto &Elt : Record) { 8520b57cec5SDimitry Andric CurrentValue += Elt; 8530b57cec5SDimitry Andric GlobalMetadataBitPosIndex.push_back(CurrentValue); 8540b57cec5SDimitry Andric } 8550b57cec5SDimitry Andric break; 8560b57cec5SDimitry Andric } 8570b57cec5SDimitry Andric case bitc::METADATA_INDEX: 8580b57cec5SDimitry Andric // We don't expect to get there, the Index is loaded when we encounter 8590b57cec5SDimitry Andric // the offset. 8600b57cec5SDimitry Andric return error("Corrupted Metadata block"); 8610b57cec5SDimitry Andric case bitc::METADATA_NAME: { 8620b57cec5SDimitry Andric // Named metadata need to be materialized now and aren't deferred. 8630b57cec5SDimitry Andric if (Error Err = IndexCursor.JumpToBit(CurrentPos)) 8640b57cec5SDimitry Andric return std::move(Err); 8650b57cec5SDimitry Andric Record.clear(); 8660b57cec5SDimitry Andric 8670b57cec5SDimitry Andric unsigned Code; 8680b57cec5SDimitry Andric if (Expected<unsigned> MaybeCode = 8690b57cec5SDimitry Andric IndexCursor.readRecord(Entry.ID, Record)) { 8700b57cec5SDimitry Andric Code = MaybeCode.get(); 8710b57cec5SDimitry Andric assert(Code == bitc::METADATA_NAME); 8720b57cec5SDimitry Andric } else 8730b57cec5SDimitry Andric return MaybeCode.takeError(); 8740b57cec5SDimitry Andric 8750b57cec5SDimitry Andric // Read name of the named metadata. 8760b57cec5SDimitry Andric SmallString<8> Name(Record.begin(), Record.end()); 8770b57cec5SDimitry Andric if (Expected<unsigned> MaybeCode = IndexCursor.ReadCode()) 8780b57cec5SDimitry Andric Code = MaybeCode.get(); 8790b57cec5SDimitry Andric else 8800b57cec5SDimitry Andric return MaybeCode.takeError(); 8810b57cec5SDimitry Andric 8820b57cec5SDimitry Andric // Named Metadata comes in two parts, we expect the name to be followed 8830b57cec5SDimitry Andric // by the node 8840b57cec5SDimitry Andric Record.clear(); 8850b57cec5SDimitry Andric if (Expected<unsigned> MaybeNextBitCode = 8860b57cec5SDimitry Andric IndexCursor.readRecord(Code, Record)) 8870b57cec5SDimitry Andric assert(MaybeNextBitCode.get() == bitc::METADATA_NAMED_NODE); 8880b57cec5SDimitry Andric else 8890b57cec5SDimitry Andric return MaybeNextBitCode.takeError(); 8900b57cec5SDimitry Andric 8910b57cec5SDimitry Andric // Read named metadata elements. 8920b57cec5SDimitry Andric unsigned Size = Record.size(); 8930b57cec5SDimitry Andric NamedMDNode *NMD = TheModule.getOrInsertNamedMetadata(Name); 8940b57cec5SDimitry Andric for (unsigned i = 0; i != Size; ++i) { 8950b57cec5SDimitry Andric // FIXME: We could use a placeholder here, however NamedMDNode are 8960b57cec5SDimitry Andric // taking MDNode as operand and not using the Metadata infrastructure. 8970b57cec5SDimitry Andric // It is acknowledged by 'TODO: Inherit from Metadata' in the 8980b57cec5SDimitry Andric // NamedMDNode class definition. 8990b57cec5SDimitry Andric MDNode *MD = MetadataList.getMDNodeFwdRefOrNull(Record[i]); 9000b57cec5SDimitry Andric assert(MD && "Invalid metadata: expect fwd ref to MDNode"); 9010b57cec5SDimitry Andric NMD->addOperand(MD); 9020b57cec5SDimitry Andric } 9030b57cec5SDimitry Andric break; 9040b57cec5SDimitry Andric } 9050b57cec5SDimitry Andric case bitc::METADATA_GLOBAL_DECL_ATTACHMENT: { 906e8d8bef9SDimitry Andric if (!GlobalDeclAttachmentPos) 907e8d8bef9SDimitry Andric GlobalDeclAttachmentPos = SavedPos; 908e8d8bef9SDimitry Andric #ifndef NDEBUG 909e8d8bef9SDimitry Andric NumGlobalDeclAttachSkipped++; 910e8d8bef9SDimitry Andric #endif 9110b57cec5SDimitry Andric break; 9120b57cec5SDimitry Andric } 9130b57cec5SDimitry Andric case bitc::METADATA_KIND: 9140b57cec5SDimitry Andric case bitc::METADATA_STRING_OLD: 9150b57cec5SDimitry Andric case bitc::METADATA_OLD_FN_NODE: 9160b57cec5SDimitry Andric case bitc::METADATA_OLD_NODE: 9170b57cec5SDimitry Andric case bitc::METADATA_VALUE: 9180b57cec5SDimitry Andric case bitc::METADATA_DISTINCT_NODE: 9190b57cec5SDimitry Andric case bitc::METADATA_NODE: 9200b57cec5SDimitry Andric case bitc::METADATA_LOCATION: 9210b57cec5SDimitry Andric case bitc::METADATA_GENERIC_DEBUG: 9220b57cec5SDimitry Andric case bitc::METADATA_SUBRANGE: 9230b57cec5SDimitry Andric case bitc::METADATA_ENUMERATOR: 9240b57cec5SDimitry Andric case bitc::METADATA_BASIC_TYPE: 925e8d8bef9SDimitry Andric case bitc::METADATA_STRING_TYPE: 9260b57cec5SDimitry Andric case bitc::METADATA_DERIVED_TYPE: 9270b57cec5SDimitry Andric case bitc::METADATA_COMPOSITE_TYPE: 9280b57cec5SDimitry Andric case bitc::METADATA_SUBROUTINE_TYPE: 9290b57cec5SDimitry Andric case bitc::METADATA_MODULE: 9300b57cec5SDimitry Andric case bitc::METADATA_FILE: 9310b57cec5SDimitry Andric case bitc::METADATA_COMPILE_UNIT: 9320b57cec5SDimitry Andric case bitc::METADATA_SUBPROGRAM: 9330b57cec5SDimitry Andric case bitc::METADATA_LEXICAL_BLOCK: 9340b57cec5SDimitry Andric case bitc::METADATA_LEXICAL_BLOCK_FILE: 9350b57cec5SDimitry Andric case bitc::METADATA_NAMESPACE: 9360b57cec5SDimitry Andric case bitc::METADATA_COMMON_BLOCK: 9370b57cec5SDimitry Andric case bitc::METADATA_MACRO: 9380b57cec5SDimitry Andric case bitc::METADATA_MACRO_FILE: 9390b57cec5SDimitry Andric case bitc::METADATA_TEMPLATE_TYPE: 9400b57cec5SDimitry Andric case bitc::METADATA_TEMPLATE_VALUE: 9410b57cec5SDimitry Andric case bitc::METADATA_GLOBAL_VAR: 9420b57cec5SDimitry Andric case bitc::METADATA_LOCAL_VAR: 943bdd1243dSDimitry Andric case bitc::METADATA_ASSIGN_ID: 9440b57cec5SDimitry Andric case bitc::METADATA_LABEL: 9450b57cec5SDimitry Andric case bitc::METADATA_EXPRESSION: 9460b57cec5SDimitry Andric case bitc::METADATA_OBJC_PROPERTY: 9470b57cec5SDimitry Andric case bitc::METADATA_IMPORTED_ENTITY: 9480b57cec5SDimitry Andric case bitc::METADATA_GLOBAL_VAR_EXPR: 949e8d8bef9SDimitry Andric case bitc::METADATA_GENERIC_SUBRANGE: 9500b57cec5SDimitry Andric // We don't expect to see any of these, if we see one, give up on 9510b57cec5SDimitry Andric // lazy-loading and fallback. 9520b57cec5SDimitry Andric MDStringRef.clear(); 9530b57cec5SDimitry Andric GlobalMetadataBitPosIndex.clear(); 9540b57cec5SDimitry Andric return false; 9550b57cec5SDimitry Andric } 9560b57cec5SDimitry Andric break; 9570b57cec5SDimitry Andric } 9580b57cec5SDimitry Andric } 9590b57cec5SDimitry Andric } 9600b57cec5SDimitry Andric } 9610b57cec5SDimitry Andric 962e8d8bef9SDimitry Andric // Load the global decl attachments after building the lazy loading index. 963e8d8bef9SDimitry Andric // We don't load them "lazily" - all global decl attachments must be 964e8d8bef9SDimitry Andric // parsed since they aren't materialized on demand. However, by delaying 965e8d8bef9SDimitry Andric // their parsing until after the index is created, we can use the index 966e8d8bef9SDimitry Andric // instead of creating temporaries. 967e8d8bef9SDimitry Andric Expected<bool> MetadataLoader::MetadataLoaderImpl::loadGlobalDeclAttachments() { 968e8d8bef9SDimitry Andric // Nothing to do if we didn't find any of these metadata records. 969e8d8bef9SDimitry Andric if (!GlobalDeclAttachmentPos) 970e8d8bef9SDimitry Andric return true; 971e8d8bef9SDimitry Andric // Use a temporary cursor so that we don't mess up the main Stream cursor or 972e8d8bef9SDimitry Andric // the lazy loading IndexCursor (which holds the necessary abbrev ids). 973e8d8bef9SDimitry Andric BitstreamCursor TempCursor = Stream; 974e8d8bef9SDimitry Andric SmallVector<uint64_t, 64> Record; 975e8d8bef9SDimitry Andric // Jump to the position before the first global decl attachment, so we can 976e8d8bef9SDimitry Andric // scan for the first BitstreamEntry record. 977e8d8bef9SDimitry Andric if (Error Err = TempCursor.JumpToBit(GlobalDeclAttachmentPos)) 978e8d8bef9SDimitry Andric return std::move(Err); 979e8d8bef9SDimitry Andric while (true) { 980349cc55cSDimitry Andric BitstreamEntry Entry; 981349cc55cSDimitry Andric if (Error E = 982349cc55cSDimitry Andric TempCursor 983349cc55cSDimitry Andric .advanceSkippingSubblocks(BitstreamCursor::AF_DontPopBlockAtEnd) 984349cc55cSDimitry Andric .moveInto(Entry)) 985349cc55cSDimitry Andric return std::move(E); 986e8d8bef9SDimitry Andric 987e8d8bef9SDimitry Andric switch (Entry.Kind) { 988e8d8bef9SDimitry Andric case BitstreamEntry::SubBlock: // Handled for us already. 989e8d8bef9SDimitry Andric case BitstreamEntry::Error: 990e8d8bef9SDimitry Andric return error("Malformed block"); 991e8d8bef9SDimitry Andric case BitstreamEntry::EndBlock: 9924824e7fdSDimitry Andric // Check that we parsed them all. 993e8d8bef9SDimitry Andric assert(NumGlobalDeclAttachSkipped == NumGlobalDeclAttachParsed); 994e8d8bef9SDimitry Andric return true; 995e8d8bef9SDimitry Andric case BitstreamEntry::Record: 996e8d8bef9SDimitry Andric break; 997e8d8bef9SDimitry Andric } 998e8d8bef9SDimitry Andric uint64_t CurrentPos = TempCursor.GetCurrentBitNo(); 999e8d8bef9SDimitry Andric Expected<unsigned> MaybeCode = TempCursor.skipRecord(Entry.ID); 1000e8d8bef9SDimitry Andric if (!MaybeCode) 1001e8d8bef9SDimitry Andric return MaybeCode.takeError(); 1002e8d8bef9SDimitry Andric if (MaybeCode.get() != bitc::METADATA_GLOBAL_DECL_ATTACHMENT) { 1003e8d8bef9SDimitry Andric // Anything other than a global decl attachment signals the end of 10044824e7fdSDimitry Andric // these records. Check that we parsed them all. 1005e8d8bef9SDimitry Andric assert(NumGlobalDeclAttachSkipped == NumGlobalDeclAttachParsed); 1006e8d8bef9SDimitry Andric return true; 1007e8d8bef9SDimitry Andric } 1008e8d8bef9SDimitry Andric #ifndef NDEBUG 1009e8d8bef9SDimitry Andric NumGlobalDeclAttachParsed++; 1010e8d8bef9SDimitry Andric #endif 1011e8d8bef9SDimitry Andric // FIXME: we need to do this early because we don't materialize global 1012e8d8bef9SDimitry Andric // value explicitly. 1013e8d8bef9SDimitry Andric if (Error Err = TempCursor.JumpToBit(CurrentPos)) 1014e8d8bef9SDimitry Andric return std::move(Err); 1015e8d8bef9SDimitry Andric Record.clear(); 1016e8d8bef9SDimitry Andric if (Expected<unsigned> MaybeRecord = 1017e8d8bef9SDimitry Andric TempCursor.readRecord(Entry.ID, Record)) 1018e8d8bef9SDimitry Andric ; 1019e8d8bef9SDimitry Andric else 1020e8d8bef9SDimitry Andric return MaybeRecord.takeError(); 1021e8d8bef9SDimitry Andric if (Record.size() % 2 == 0) 1022e8d8bef9SDimitry Andric return error("Invalid record"); 1023e8d8bef9SDimitry Andric unsigned ValueID = Record[0]; 1024e8d8bef9SDimitry Andric if (ValueID >= ValueList.size()) 1025e8d8bef9SDimitry Andric return error("Invalid record"); 1026e8d8bef9SDimitry Andric if (auto *GO = dyn_cast<GlobalObject>(ValueList[ValueID])) { 1027e8d8bef9SDimitry Andric // Need to save and restore the current position since 1028e8d8bef9SDimitry Andric // parseGlobalObjectAttachment will resolve all forward references which 1029e8d8bef9SDimitry Andric // would require parsing from locations stored in the index. 1030e8d8bef9SDimitry Andric CurrentPos = TempCursor.GetCurrentBitNo(); 1031e8d8bef9SDimitry Andric if (Error Err = parseGlobalObjectAttachment( 1032e8d8bef9SDimitry Andric *GO, ArrayRef<uint64_t>(Record).slice(1))) 1033e8d8bef9SDimitry Andric return std::move(Err); 1034e8d8bef9SDimitry Andric if (Error Err = TempCursor.JumpToBit(CurrentPos)) 1035e8d8bef9SDimitry Andric return std::move(Err); 1036e8d8bef9SDimitry Andric } 1037e8d8bef9SDimitry Andric } 1038e8d8bef9SDimitry Andric } 1039e8d8bef9SDimitry Andric 1040bdd1243dSDimitry Andric void MetadataLoader::MetadataLoaderImpl::callMDTypeCallback(Metadata **Val, 1041bdd1243dSDimitry Andric unsigned TypeID) { 1042bdd1243dSDimitry Andric if (Callbacks.MDType) { 1043bdd1243dSDimitry Andric (*Callbacks.MDType)(Val, TypeID, Callbacks.GetTypeByID, 1044bdd1243dSDimitry Andric Callbacks.GetContainedTypeID); 1045bdd1243dSDimitry Andric } 1046bdd1243dSDimitry Andric } 1047bdd1243dSDimitry Andric 10480b57cec5SDimitry Andric /// Parse a METADATA_BLOCK. If ModuleLevel is true then we are parsing 10490b57cec5SDimitry Andric /// module level metadata. 10500b57cec5SDimitry Andric Error MetadataLoader::MetadataLoaderImpl::parseMetadata(bool ModuleLevel) { 10510b57cec5SDimitry Andric if (!ModuleLevel && MetadataList.hasFwdRefs()) 10520b57cec5SDimitry Andric return error("Invalid metadata: fwd refs into function blocks"); 10530b57cec5SDimitry Andric 10540b57cec5SDimitry Andric // Record the entry position so that we can jump back here and efficiently 10550b57cec5SDimitry Andric // skip the whole block in case we lazy-load. 10560b57cec5SDimitry Andric auto EntryPos = Stream.GetCurrentBitNo(); 10570b57cec5SDimitry Andric 10580b57cec5SDimitry Andric if (Error Err = Stream.EnterSubBlock(bitc::METADATA_BLOCK_ID)) 10590b57cec5SDimitry Andric return Err; 10600b57cec5SDimitry Andric 10610b57cec5SDimitry Andric SmallVector<uint64_t, 64> Record; 10620b57cec5SDimitry Andric PlaceholderQueue Placeholders; 10630b57cec5SDimitry Andric 10640b57cec5SDimitry Andric // We lazy-load module-level metadata: we build an index for each record, and 10650b57cec5SDimitry Andric // then load individual record as needed, starting with the named metadata. 10660b57cec5SDimitry Andric if (ModuleLevel && IsImporting && MetadataList.empty() && 10670b57cec5SDimitry Andric !DisableLazyLoading) { 10680b57cec5SDimitry Andric auto SuccessOrErr = lazyLoadModuleMetadataBlock(); 10690b57cec5SDimitry Andric if (!SuccessOrErr) 10700b57cec5SDimitry Andric return SuccessOrErr.takeError(); 10710b57cec5SDimitry Andric if (SuccessOrErr.get()) { 10720b57cec5SDimitry Andric // An index was successfully created and we will be able to load metadata 10730b57cec5SDimitry Andric // on-demand. 10740b57cec5SDimitry Andric MetadataList.resize(MDStringRef.size() + 10750b57cec5SDimitry Andric GlobalMetadataBitPosIndex.size()); 10760b57cec5SDimitry Andric 1077e8d8bef9SDimitry Andric // Now that we have built the index, load the global decl attachments 1078e8d8bef9SDimitry Andric // that were deferred during that process. This avoids creating 1079e8d8bef9SDimitry Andric // temporaries. 1080e8d8bef9SDimitry Andric SuccessOrErr = loadGlobalDeclAttachments(); 1081e8d8bef9SDimitry Andric if (!SuccessOrErr) 1082e8d8bef9SDimitry Andric return SuccessOrErr.takeError(); 1083e8d8bef9SDimitry Andric assert(SuccessOrErr.get()); 1084e8d8bef9SDimitry Andric 10850b57cec5SDimitry Andric // Reading the named metadata created forward references and/or 10860b57cec5SDimitry Andric // placeholders, that we flush here. 10870b57cec5SDimitry Andric resolveForwardRefsAndPlaceholders(Placeholders); 10880b57cec5SDimitry Andric upgradeDebugInfo(); 10890b57cec5SDimitry Andric // Return at the beginning of the block, since it is easy to skip it 10900b57cec5SDimitry Andric // entirely from there. 10910b57cec5SDimitry Andric Stream.ReadBlockEnd(); // Pop the abbrev block context. 10920b57cec5SDimitry Andric if (Error Err = IndexCursor.JumpToBit(EntryPos)) 10930b57cec5SDimitry Andric return Err; 10940b57cec5SDimitry Andric if (Error Err = Stream.SkipBlock()) { 10950b57cec5SDimitry Andric // FIXME this drops the error on the floor, which 10960b57cec5SDimitry Andric // ThinLTO/X86/debuginfo-cu-import.ll relies on. 10970b57cec5SDimitry Andric consumeError(std::move(Err)); 10980b57cec5SDimitry Andric return Error::success(); 10990b57cec5SDimitry Andric } 11000b57cec5SDimitry Andric return Error::success(); 11010b57cec5SDimitry Andric } 11020b57cec5SDimitry Andric // Couldn't load an index, fallback to loading all the block "old-style". 11030b57cec5SDimitry Andric } 11040b57cec5SDimitry Andric 11050b57cec5SDimitry Andric unsigned NextMetadataNo = MetadataList.size(); 11060b57cec5SDimitry Andric 11070b57cec5SDimitry Andric // Read all the records. 11080b57cec5SDimitry Andric while (true) { 1109349cc55cSDimitry Andric BitstreamEntry Entry; 1110349cc55cSDimitry Andric if (Error E = Stream.advanceSkippingSubblocks().moveInto(Entry)) 1111349cc55cSDimitry Andric return E; 11120b57cec5SDimitry Andric 11130b57cec5SDimitry Andric switch (Entry.Kind) { 11140b57cec5SDimitry Andric case BitstreamEntry::SubBlock: // Handled for us already. 11150b57cec5SDimitry Andric case BitstreamEntry::Error: 11160b57cec5SDimitry Andric return error("Malformed block"); 11170b57cec5SDimitry Andric case BitstreamEntry::EndBlock: 11180b57cec5SDimitry Andric resolveForwardRefsAndPlaceholders(Placeholders); 11190b57cec5SDimitry Andric upgradeDebugInfo(); 11200b57cec5SDimitry Andric return Error::success(); 11210b57cec5SDimitry Andric case BitstreamEntry::Record: 11220b57cec5SDimitry Andric // The interesting case. 11230b57cec5SDimitry Andric break; 11240b57cec5SDimitry Andric } 11250b57cec5SDimitry Andric 11260b57cec5SDimitry Andric // Read a record. 11270b57cec5SDimitry Andric Record.clear(); 11280b57cec5SDimitry Andric StringRef Blob; 11290b57cec5SDimitry Andric ++NumMDRecordLoaded; 11300b57cec5SDimitry Andric if (Expected<unsigned> MaybeCode = 11310b57cec5SDimitry Andric Stream.readRecord(Entry.ID, Record, &Blob)) { 11320b57cec5SDimitry Andric if (Error Err = parseOneMetadata(Record, MaybeCode.get(), Placeholders, 11330b57cec5SDimitry Andric Blob, NextMetadataNo)) 11340b57cec5SDimitry Andric return Err; 11350b57cec5SDimitry Andric } else 11360b57cec5SDimitry Andric return MaybeCode.takeError(); 11370b57cec5SDimitry Andric } 11380b57cec5SDimitry Andric } 11390b57cec5SDimitry Andric 11400b57cec5SDimitry Andric MDString *MetadataLoader::MetadataLoaderImpl::lazyLoadOneMDString(unsigned ID) { 11410b57cec5SDimitry Andric ++NumMDStringLoaded; 11420b57cec5SDimitry Andric if (Metadata *MD = MetadataList.lookup(ID)) 11430b57cec5SDimitry Andric return cast<MDString>(MD); 11440b57cec5SDimitry Andric auto MDS = MDString::get(Context, MDStringRef[ID]); 11450b57cec5SDimitry Andric MetadataList.assignValue(MDS, ID); 11460b57cec5SDimitry Andric return MDS; 11470b57cec5SDimitry Andric } 11480b57cec5SDimitry Andric 11490b57cec5SDimitry Andric void MetadataLoader::MetadataLoaderImpl::lazyLoadOneMetadata( 11500b57cec5SDimitry Andric unsigned ID, PlaceholderQueue &Placeholders) { 11510b57cec5SDimitry Andric assert(ID < (MDStringRef.size()) + GlobalMetadataBitPosIndex.size()); 11520b57cec5SDimitry Andric assert(ID >= MDStringRef.size() && "Unexpected lazy-loading of MDString"); 11530b57cec5SDimitry Andric // Lookup first if the metadata hasn't already been loaded. 11540b57cec5SDimitry Andric if (auto *MD = MetadataList.lookup(ID)) { 11558bcb0991SDimitry Andric auto *N = cast<MDNode>(MD); 11560b57cec5SDimitry Andric if (!N->isTemporary()) 11570b57cec5SDimitry Andric return; 11580b57cec5SDimitry Andric } 11590b57cec5SDimitry Andric SmallVector<uint64_t, 64> Record; 11600b57cec5SDimitry Andric StringRef Blob; 11610b57cec5SDimitry Andric if (Error Err = IndexCursor.JumpToBit( 11620b57cec5SDimitry Andric GlobalMetadataBitPosIndex[ID - MDStringRef.size()])) 11630b57cec5SDimitry Andric report_fatal_error("lazyLoadOneMetadata failed jumping: " + 1164349cc55cSDimitry Andric Twine(toString(std::move(Err)))); 1165349cc55cSDimitry Andric BitstreamEntry Entry; 1166349cc55cSDimitry Andric if (Error E = IndexCursor.advanceSkippingSubblocks().moveInto(Entry)) 11670b57cec5SDimitry Andric // FIXME this drops the error on the floor. 11680b57cec5SDimitry Andric report_fatal_error("lazyLoadOneMetadata failed advanceSkippingSubblocks: " + 1169349cc55cSDimitry Andric Twine(toString(std::move(E)))); 11700b57cec5SDimitry Andric ++NumMDRecordLoaded; 11710b57cec5SDimitry Andric if (Expected<unsigned> MaybeCode = 11720b57cec5SDimitry Andric IndexCursor.readRecord(Entry.ID, Record, &Blob)) { 11730b57cec5SDimitry Andric if (Error Err = 11740b57cec5SDimitry Andric parseOneMetadata(Record, MaybeCode.get(), Placeholders, Blob, ID)) 11750b57cec5SDimitry Andric report_fatal_error("Can't lazyload MD, parseOneMetadata: " + 1176349cc55cSDimitry Andric Twine(toString(std::move(Err)))); 11770b57cec5SDimitry Andric } else 1178349cc55cSDimitry Andric report_fatal_error("Can't lazyload MD: " + 1179349cc55cSDimitry Andric Twine(toString(MaybeCode.takeError()))); 11800b57cec5SDimitry Andric } 11810b57cec5SDimitry Andric 11820b57cec5SDimitry Andric /// Ensure that all forward-references and placeholders are resolved. 11830b57cec5SDimitry Andric /// Iteratively lazy-loading metadata on-demand if needed. 11840b57cec5SDimitry Andric void MetadataLoader::MetadataLoaderImpl::resolveForwardRefsAndPlaceholders( 11850b57cec5SDimitry Andric PlaceholderQueue &Placeholders) { 11860b57cec5SDimitry Andric DenseSet<unsigned> Temporaries; 118704eeddc0SDimitry Andric while (true) { 11880b57cec5SDimitry Andric // Populate Temporaries with the placeholders that haven't been loaded yet. 11890b57cec5SDimitry Andric Placeholders.getTemporaries(MetadataList, Temporaries); 11900b57cec5SDimitry Andric 11910b57cec5SDimitry Andric // If we don't have any temporary, or FwdReference, we're done! 11920b57cec5SDimitry Andric if (Temporaries.empty() && !MetadataList.hasFwdRefs()) 11930b57cec5SDimitry Andric break; 11940b57cec5SDimitry Andric 11950b57cec5SDimitry Andric // First, load all the temporaries. This can add new placeholders or 11960b57cec5SDimitry Andric // forward references. 11970b57cec5SDimitry Andric for (auto ID : Temporaries) 11980b57cec5SDimitry Andric lazyLoadOneMetadata(ID, Placeholders); 11990b57cec5SDimitry Andric Temporaries.clear(); 12000b57cec5SDimitry Andric 12010b57cec5SDimitry Andric // Second, load the forward-references. This can also add new placeholders 12020b57cec5SDimitry Andric // or forward references. 12030b57cec5SDimitry Andric while (MetadataList.hasFwdRefs()) 12040b57cec5SDimitry Andric lazyLoadOneMetadata(MetadataList.getNextFwdRef(), Placeholders); 12050b57cec5SDimitry Andric } 12060b57cec5SDimitry Andric // At this point we don't have any forward reference remaining, or temporary 12070b57cec5SDimitry Andric // that haven't been loaded. We can safely drop RAUW support and mark cycles 12080b57cec5SDimitry Andric // as resolved. 12090b57cec5SDimitry Andric MetadataList.tryToResolveCycles(); 12100b57cec5SDimitry Andric 12110b57cec5SDimitry Andric // Finally, everything is in place, we can replace the placeholders operands 12120b57cec5SDimitry Andric // with the final node they refer to. 12130b57cec5SDimitry Andric Placeholders.flush(MetadataList); 12140b57cec5SDimitry Andric } 12150b57cec5SDimitry Andric 12160b57cec5SDimitry Andric Error MetadataLoader::MetadataLoaderImpl::parseOneMetadata( 12170b57cec5SDimitry Andric SmallVectorImpl<uint64_t> &Record, unsigned Code, 12180b57cec5SDimitry Andric PlaceholderQueue &Placeholders, StringRef Blob, unsigned &NextMetadataNo) { 12190b57cec5SDimitry Andric 12200b57cec5SDimitry Andric bool IsDistinct = false; 12210b57cec5SDimitry Andric auto getMD = [&](unsigned ID) -> Metadata * { 12220b57cec5SDimitry Andric if (ID < MDStringRef.size()) 12230b57cec5SDimitry Andric return lazyLoadOneMDString(ID); 12240b57cec5SDimitry Andric if (!IsDistinct) { 12250b57cec5SDimitry Andric if (auto *MD = MetadataList.lookup(ID)) 12260b57cec5SDimitry Andric return MD; 12270b57cec5SDimitry Andric // If lazy-loading is enabled, we try recursively to load the operand 12280b57cec5SDimitry Andric // instead of creating a temporary. 12290b57cec5SDimitry Andric if (ID < (MDStringRef.size() + GlobalMetadataBitPosIndex.size())) { 12300b57cec5SDimitry Andric // Create a temporary for the node that is referencing the operand we 12310b57cec5SDimitry Andric // will lazy-load. It is needed before recursing in case there are 12320b57cec5SDimitry Andric // uniquing cycles. 12330b57cec5SDimitry Andric MetadataList.getMetadataFwdRef(NextMetadataNo); 12340b57cec5SDimitry Andric lazyLoadOneMetadata(ID, Placeholders); 12350b57cec5SDimitry Andric return MetadataList.lookup(ID); 12360b57cec5SDimitry Andric } 12370b57cec5SDimitry Andric // Return a temporary. 12380b57cec5SDimitry Andric return MetadataList.getMetadataFwdRef(ID); 12390b57cec5SDimitry Andric } 12400b57cec5SDimitry Andric if (auto *MD = MetadataList.getMetadataIfResolved(ID)) 12410b57cec5SDimitry Andric return MD; 12420b57cec5SDimitry Andric return &Placeholders.getPlaceholderOp(ID); 12430b57cec5SDimitry Andric }; 12440b57cec5SDimitry Andric auto getMDOrNull = [&](unsigned ID) -> Metadata * { 12450b57cec5SDimitry Andric if (ID) 12460b57cec5SDimitry Andric return getMD(ID - 1); 12470b57cec5SDimitry Andric return nullptr; 12480b57cec5SDimitry Andric }; 12490b57cec5SDimitry Andric auto getMDOrNullWithoutPlaceholders = [&](unsigned ID) -> Metadata * { 12500b57cec5SDimitry Andric if (ID) 12510b57cec5SDimitry Andric return MetadataList.getMetadataFwdRef(ID - 1); 12520b57cec5SDimitry Andric return nullptr; 12530b57cec5SDimitry Andric }; 12540b57cec5SDimitry Andric auto getMDString = [&](unsigned ID) -> MDString * { 12550b57cec5SDimitry Andric // This requires that the ID is not really a forward reference. In 12560b57cec5SDimitry Andric // particular, the MDString must already have been resolved. 12570b57cec5SDimitry Andric auto MDS = getMDOrNull(ID); 12580b57cec5SDimitry Andric return cast_or_null<MDString>(MDS); 12590b57cec5SDimitry Andric }; 12600b57cec5SDimitry Andric 12610b57cec5SDimitry Andric // Support for old type refs. 12620b57cec5SDimitry Andric auto getDITypeRefOrNull = [&](unsigned ID) { 12630b57cec5SDimitry Andric return MetadataList.upgradeTypeRef(getMDOrNull(ID)); 12640b57cec5SDimitry Andric }; 12650b57cec5SDimitry Andric 12660b57cec5SDimitry Andric #define GET_OR_DISTINCT(CLASS, ARGS) \ 12670b57cec5SDimitry Andric (IsDistinct ? CLASS::getDistinct ARGS : CLASS::get ARGS) 12680b57cec5SDimitry Andric 12690b57cec5SDimitry Andric switch (Code) { 12700b57cec5SDimitry Andric default: // Default behavior: ignore. 12710b57cec5SDimitry Andric break; 12720b57cec5SDimitry Andric case bitc::METADATA_NAME: { 12730b57cec5SDimitry Andric // Read name of the named metadata. 12740b57cec5SDimitry Andric SmallString<8> Name(Record.begin(), Record.end()); 12750b57cec5SDimitry Andric Record.clear(); 1276349cc55cSDimitry Andric if (Error E = Stream.ReadCode().moveInto(Code)) 1277349cc55cSDimitry Andric return E; 12780b57cec5SDimitry Andric 12790b57cec5SDimitry Andric ++NumMDRecordLoaded; 12800b57cec5SDimitry Andric if (Expected<unsigned> MaybeNextBitCode = Stream.readRecord(Code, Record)) { 12810b57cec5SDimitry Andric if (MaybeNextBitCode.get() != bitc::METADATA_NAMED_NODE) 12820b57cec5SDimitry Andric return error("METADATA_NAME not followed by METADATA_NAMED_NODE"); 12830b57cec5SDimitry Andric } else 12840b57cec5SDimitry Andric return MaybeNextBitCode.takeError(); 12850b57cec5SDimitry Andric 12860b57cec5SDimitry Andric // Read named metadata elements. 12870b57cec5SDimitry Andric unsigned Size = Record.size(); 12880b57cec5SDimitry Andric NamedMDNode *NMD = TheModule.getOrInsertNamedMetadata(Name); 12890b57cec5SDimitry Andric for (unsigned i = 0; i != Size; ++i) { 12900b57cec5SDimitry Andric MDNode *MD = MetadataList.getMDNodeFwdRefOrNull(Record[i]); 12910b57cec5SDimitry Andric if (!MD) 12920b57cec5SDimitry Andric return error("Invalid named metadata: expect fwd ref to MDNode"); 12930b57cec5SDimitry Andric NMD->addOperand(MD); 12940b57cec5SDimitry Andric } 12950b57cec5SDimitry Andric break; 12960b57cec5SDimitry Andric } 12970b57cec5SDimitry Andric case bitc::METADATA_OLD_FN_NODE: { 12985ffd83dbSDimitry Andric // Deprecated, but still needed to read old bitcode files. 12990b57cec5SDimitry Andric // This is a LocalAsMetadata record, the only type of function-local 13000b57cec5SDimitry Andric // metadata. 13010b57cec5SDimitry Andric if (Record.size() % 2 == 1) 13020b57cec5SDimitry Andric return error("Invalid record"); 13030b57cec5SDimitry Andric 13040b57cec5SDimitry Andric // If this isn't a LocalAsMetadata record, we're dropping it. This used 13050b57cec5SDimitry Andric // to be legal, but there's no upgrade path. 13060b57cec5SDimitry Andric auto dropRecord = [&] { 1307bdd1243dSDimitry Andric MetadataList.assignValue(MDNode::get(Context, std::nullopt), 1308bdd1243dSDimitry Andric NextMetadataNo); 13090b57cec5SDimitry Andric NextMetadataNo++; 13100b57cec5SDimitry Andric }; 13110b57cec5SDimitry Andric if (Record.size() != 2) { 13120b57cec5SDimitry Andric dropRecord(); 13130b57cec5SDimitry Andric break; 13140b57cec5SDimitry Andric } 13150b57cec5SDimitry Andric 131681ad6265SDimitry Andric unsigned TyID = Record[0]; 1317bdd1243dSDimitry Andric Type *Ty = Callbacks.GetTypeByID(TyID); 13180b57cec5SDimitry Andric if (Ty->isMetadataTy() || Ty->isVoidTy()) { 13190b57cec5SDimitry Andric dropRecord(); 13200b57cec5SDimitry Andric break; 13210b57cec5SDimitry Andric } 13220b57cec5SDimitry Andric 1323fcaf7f86SDimitry Andric Value *V = ValueList.getValueFwdRef(Record[1], Ty, TyID, 1324fcaf7f86SDimitry Andric /*ConstExprInsertBB*/ nullptr); 1325fcaf7f86SDimitry Andric if (!V) 1326fcaf7f86SDimitry Andric return error("Invalid value reference from old fn metadata"); 1327fcaf7f86SDimitry Andric 1328fcaf7f86SDimitry Andric MetadataList.assignValue(LocalAsMetadata::get(V), NextMetadataNo); 13290b57cec5SDimitry Andric NextMetadataNo++; 13300b57cec5SDimitry Andric break; 13310b57cec5SDimitry Andric } 13320b57cec5SDimitry Andric case bitc::METADATA_OLD_NODE: { 13335ffd83dbSDimitry Andric // Deprecated, but still needed to read old bitcode files. 13340b57cec5SDimitry Andric if (Record.size() % 2 == 1) 13350b57cec5SDimitry Andric return error("Invalid record"); 13360b57cec5SDimitry Andric 13370b57cec5SDimitry Andric unsigned Size = Record.size(); 13380b57cec5SDimitry Andric SmallVector<Metadata *, 8> Elts; 13390b57cec5SDimitry Andric for (unsigned i = 0; i != Size; i += 2) { 134081ad6265SDimitry Andric unsigned TyID = Record[i]; 1341bdd1243dSDimitry Andric Type *Ty = Callbacks.GetTypeByID(TyID); 13420b57cec5SDimitry Andric if (!Ty) 13430b57cec5SDimitry Andric return error("Invalid record"); 13440b57cec5SDimitry Andric if (Ty->isMetadataTy()) 13450b57cec5SDimitry Andric Elts.push_back(getMD(Record[i + 1])); 13460b57cec5SDimitry Andric else if (!Ty->isVoidTy()) { 1347fcaf7f86SDimitry Andric Value *V = ValueList.getValueFwdRef(Record[i + 1], Ty, TyID, 1348fcaf7f86SDimitry Andric /*ConstExprInsertBB*/ nullptr); 1349fcaf7f86SDimitry Andric if (!V) 1350fcaf7f86SDimitry Andric return error("Invalid value reference from old metadata"); 1351bdd1243dSDimitry Andric Metadata *MD = ValueAsMetadata::get(V); 13520b57cec5SDimitry Andric assert(isa<ConstantAsMetadata>(MD) && 13530b57cec5SDimitry Andric "Expected non-function-local metadata"); 1354bdd1243dSDimitry Andric callMDTypeCallback(&MD, TyID); 13550b57cec5SDimitry Andric Elts.push_back(MD); 13560b57cec5SDimitry Andric } else 13570b57cec5SDimitry Andric Elts.push_back(nullptr); 13580b57cec5SDimitry Andric } 13590b57cec5SDimitry Andric MetadataList.assignValue(MDNode::get(Context, Elts), NextMetadataNo); 13600b57cec5SDimitry Andric NextMetadataNo++; 13610b57cec5SDimitry Andric break; 13620b57cec5SDimitry Andric } 13630b57cec5SDimitry Andric case bitc::METADATA_VALUE: { 13640b57cec5SDimitry Andric if (Record.size() != 2) 13650b57cec5SDimitry Andric return error("Invalid record"); 13660b57cec5SDimitry Andric 136781ad6265SDimitry Andric unsigned TyID = Record[0]; 1368bdd1243dSDimitry Andric Type *Ty = Callbacks.GetTypeByID(TyID); 13690b57cec5SDimitry Andric if (Ty->isMetadataTy() || Ty->isVoidTy()) 13700b57cec5SDimitry Andric return error("Invalid record"); 13710b57cec5SDimitry Andric 1372fcaf7f86SDimitry Andric Value *V = ValueList.getValueFwdRef(Record[1], Ty, TyID, 1373fcaf7f86SDimitry Andric /*ConstExprInsertBB*/ nullptr); 1374fcaf7f86SDimitry Andric if (!V) 1375fcaf7f86SDimitry Andric return error("Invalid value reference from metadata"); 1376fcaf7f86SDimitry Andric 1377bdd1243dSDimitry Andric Metadata *MD = ValueAsMetadata::get(V); 1378bdd1243dSDimitry Andric callMDTypeCallback(&MD, TyID); 1379bdd1243dSDimitry Andric MetadataList.assignValue(MD, NextMetadataNo); 13800b57cec5SDimitry Andric NextMetadataNo++; 13810b57cec5SDimitry Andric break; 13820b57cec5SDimitry Andric } 13830b57cec5SDimitry Andric case bitc::METADATA_DISTINCT_NODE: 13840b57cec5SDimitry Andric IsDistinct = true; 1385bdd1243dSDimitry Andric [[fallthrough]]; 13860b57cec5SDimitry Andric case bitc::METADATA_NODE: { 13870b57cec5SDimitry Andric SmallVector<Metadata *, 8> Elts; 13880b57cec5SDimitry Andric Elts.reserve(Record.size()); 13890b57cec5SDimitry Andric for (unsigned ID : Record) 13900b57cec5SDimitry Andric Elts.push_back(getMDOrNull(ID)); 13910b57cec5SDimitry Andric MetadataList.assignValue(IsDistinct ? MDNode::getDistinct(Context, Elts) 13920b57cec5SDimitry Andric : MDNode::get(Context, Elts), 13930b57cec5SDimitry Andric NextMetadataNo); 13940b57cec5SDimitry Andric NextMetadataNo++; 13950b57cec5SDimitry Andric break; 13960b57cec5SDimitry Andric } 13970b57cec5SDimitry Andric case bitc::METADATA_LOCATION: { 13980b57cec5SDimitry Andric if (Record.size() != 5 && Record.size() != 6) 13990b57cec5SDimitry Andric return error("Invalid record"); 14000b57cec5SDimitry Andric 14010b57cec5SDimitry Andric IsDistinct = Record[0]; 14020b57cec5SDimitry Andric unsigned Line = Record[1]; 14030b57cec5SDimitry Andric unsigned Column = Record[2]; 14040b57cec5SDimitry Andric Metadata *Scope = getMD(Record[3]); 14050b57cec5SDimitry Andric Metadata *InlinedAt = getMDOrNull(Record[4]); 14060b57cec5SDimitry Andric bool ImplicitCode = Record.size() == 6 && Record[5]; 14070b57cec5SDimitry Andric MetadataList.assignValue( 14080b57cec5SDimitry Andric GET_OR_DISTINCT(DILocation, (Context, Line, Column, Scope, InlinedAt, 14090b57cec5SDimitry Andric ImplicitCode)), 14100b57cec5SDimitry Andric NextMetadataNo); 14110b57cec5SDimitry Andric NextMetadataNo++; 14120b57cec5SDimitry Andric break; 14130b57cec5SDimitry Andric } 14140b57cec5SDimitry Andric case bitc::METADATA_GENERIC_DEBUG: { 14150b57cec5SDimitry Andric if (Record.size() < 4) 14160b57cec5SDimitry Andric return error("Invalid record"); 14170b57cec5SDimitry Andric 14180b57cec5SDimitry Andric IsDistinct = Record[0]; 14190b57cec5SDimitry Andric unsigned Tag = Record[1]; 14200b57cec5SDimitry Andric unsigned Version = Record[2]; 14210b57cec5SDimitry Andric 14220b57cec5SDimitry Andric if (Tag >= 1u << 16 || Version != 0) 14230b57cec5SDimitry Andric return error("Invalid record"); 14240b57cec5SDimitry Andric 14250b57cec5SDimitry Andric auto *Header = getMDString(Record[3]); 14260b57cec5SDimitry Andric SmallVector<Metadata *, 8> DwarfOps; 14270b57cec5SDimitry Andric for (unsigned I = 4, E = Record.size(); I != E; ++I) 14280b57cec5SDimitry Andric DwarfOps.push_back(getMDOrNull(Record[I])); 14290b57cec5SDimitry Andric MetadataList.assignValue( 14300b57cec5SDimitry Andric GET_OR_DISTINCT(GenericDINode, (Context, Tag, Header, DwarfOps)), 14310b57cec5SDimitry Andric NextMetadataNo); 14320b57cec5SDimitry Andric NextMetadataNo++; 14330b57cec5SDimitry Andric break; 14340b57cec5SDimitry Andric } 14350b57cec5SDimitry Andric case bitc::METADATA_SUBRANGE: { 14360b57cec5SDimitry Andric Metadata *Val = nullptr; 14370b57cec5SDimitry Andric // Operand 'count' is interpreted as: 14380b57cec5SDimitry Andric // - Signed integer (version 0) 14390b57cec5SDimitry Andric // - Metadata node (version 1) 14405ffd83dbSDimitry Andric // Operand 'lowerBound' is interpreted as: 14415ffd83dbSDimitry Andric // - Signed integer (version 0 and 1) 14425ffd83dbSDimitry Andric // - Metadata node (version 2) 14435ffd83dbSDimitry Andric // Operands 'upperBound' and 'stride' are interpreted as: 14445ffd83dbSDimitry Andric // - Metadata node (version 2) 14450b57cec5SDimitry Andric switch (Record[0] >> 1) { 14460b57cec5SDimitry Andric case 0: 14470b57cec5SDimitry Andric Val = GET_OR_DISTINCT(DISubrange, 14485ffd83dbSDimitry Andric (Context, Record[1], unrotateSign(Record[2]))); 14490b57cec5SDimitry Andric break; 14500b57cec5SDimitry Andric case 1: 14510b57cec5SDimitry Andric Val = GET_OR_DISTINCT(DISubrange, (Context, getMDOrNull(Record[1]), 14525ffd83dbSDimitry Andric unrotateSign(Record[2]))); 14535ffd83dbSDimitry Andric break; 14545ffd83dbSDimitry Andric case 2: 14555ffd83dbSDimitry Andric Val = GET_OR_DISTINCT( 14565ffd83dbSDimitry Andric DISubrange, (Context, getMDOrNull(Record[1]), getMDOrNull(Record[2]), 14575ffd83dbSDimitry Andric getMDOrNull(Record[3]), getMDOrNull(Record[4]))); 14580b57cec5SDimitry Andric break; 14590b57cec5SDimitry Andric default: 14600b57cec5SDimitry Andric return error("Invalid record: Unsupported version of DISubrange"); 14610b57cec5SDimitry Andric } 14620b57cec5SDimitry Andric 14630b57cec5SDimitry Andric MetadataList.assignValue(Val, NextMetadataNo); 14640b57cec5SDimitry Andric IsDistinct = Record[0] & 1; 14650b57cec5SDimitry Andric NextMetadataNo++; 14660b57cec5SDimitry Andric break; 14670b57cec5SDimitry Andric } 1468e8d8bef9SDimitry Andric case bitc::METADATA_GENERIC_SUBRANGE: { 1469e8d8bef9SDimitry Andric Metadata *Val = nullptr; 1470e8d8bef9SDimitry Andric Val = GET_OR_DISTINCT(DIGenericSubrange, 1471e8d8bef9SDimitry Andric (Context, getMDOrNull(Record[1]), 1472e8d8bef9SDimitry Andric getMDOrNull(Record[2]), getMDOrNull(Record[3]), 1473e8d8bef9SDimitry Andric getMDOrNull(Record[4]))); 1474e8d8bef9SDimitry Andric 1475e8d8bef9SDimitry Andric MetadataList.assignValue(Val, NextMetadataNo); 1476e8d8bef9SDimitry Andric IsDistinct = Record[0] & 1; 1477e8d8bef9SDimitry Andric NextMetadataNo++; 1478e8d8bef9SDimitry Andric break; 1479e8d8bef9SDimitry Andric } 14800b57cec5SDimitry Andric case bitc::METADATA_ENUMERATOR: { 14815ffd83dbSDimitry Andric if (Record.size() < 3) 14820b57cec5SDimitry Andric return error("Invalid record"); 14830b57cec5SDimitry Andric 14840b57cec5SDimitry Andric IsDistinct = Record[0] & 1; 14850b57cec5SDimitry Andric bool IsUnsigned = Record[0] & 2; 14865ffd83dbSDimitry Andric bool IsBigInt = Record[0] & 4; 14875ffd83dbSDimitry Andric APInt Value; 14885ffd83dbSDimitry Andric 14895ffd83dbSDimitry Andric if (IsBigInt) { 14905ffd83dbSDimitry Andric const uint64_t BitWidth = Record[1]; 14915ffd83dbSDimitry Andric const size_t NumWords = Record.size() - 3; 1492bdd1243dSDimitry Andric Value = readWideAPInt(ArrayRef(&Record[3], NumWords), BitWidth); 14935ffd83dbSDimitry Andric } else 14945ffd83dbSDimitry Andric Value = APInt(64, unrotateSign(Record[1]), !IsUnsigned); 14955ffd83dbSDimitry Andric 14960b57cec5SDimitry Andric MetadataList.assignValue( 14975ffd83dbSDimitry Andric GET_OR_DISTINCT(DIEnumerator, 14985ffd83dbSDimitry Andric (Context, Value, IsUnsigned, getMDString(Record[2]))), 14990b57cec5SDimitry Andric NextMetadataNo); 15000b57cec5SDimitry Andric NextMetadataNo++; 15010b57cec5SDimitry Andric break; 15020b57cec5SDimitry Andric } 15030b57cec5SDimitry Andric case bitc::METADATA_BASIC_TYPE: { 15040b57cec5SDimitry Andric if (Record.size() < 6 || Record.size() > 7) 15050b57cec5SDimitry Andric return error("Invalid record"); 15060b57cec5SDimitry Andric 15070b57cec5SDimitry Andric IsDistinct = Record[0]; 1508349cc55cSDimitry Andric DINode::DIFlags Flags = (Record.size() > 6) 1509349cc55cSDimitry Andric ? static_cast<DINode::DIFlags>(Record[6]) 1510349cc55cSDimitry Andric : DINode::FlagZero; 15110b57cec5SDimitry Andric 15120b57cec5SDimitry Andric MetadataList.assignValue( 15130b57cec5SDimitry Andric GET_OR_DISTINCT(DIBasicType, 15140b57cec5SDimitry Andric (Context, Record[1], getMDString(Record[2]), Record[3], 15150b57cec5SDimitry Andric Record[4], Record[5], Flags)), 15160b57cec5SDimitry Andric NextMetadataNo); 15170b57cec5SDimitry Andric NextMetadataNo++; 15180b57cec5SDimitry Andric break; 15190b57cec5SDimitry Andric } 1520e8d8bef9SDimitry Andric case bitc::METADATA_STRING_TYPE: { 152104eeddc0SDimitry Andric if (Record.size() > 9 || Record.size() < 8) 1522e8d8bef9SDimitry Andric return error("Invalid record"); 1523e8d8bef9SDimitry Andric 1524e8d8bef9SDimitry Andric IsDistinct = Record[0]; 152504eeddc0SDimitry Andric bool SizeIs8 = Record.size() == 8; 152604eeddc0SDimitry Andric // StringLocationExp (i.e. Record[5]) is added at a later time 152704eeddc0SDimitry Andric // than the other fields. The code here enables backward compatibility. 152804eeddc0SDimitry Andric Metadata *StringLocationExp = SizeIs8 ? nullptr : getMDOrNull(Record[5]); 152904eeddc0SDimitry Andric unsigned Offset = SizeIs8 ? 5 : 6; 1530e8d8bef9SDimitry Andric MetadataList.assignValue( 1531e8d8bef9SDimitry Andric GET_OR_DISTINCT(DIStringType, 1532e8d8bef9SDimitry Andric (Context, Record[1], getMDString(Record[2]), 1533e8d8bef9SDimitry Andric getMDOrNull(Record[3]), getMDOrNull(Record[4]), 153404eeddc0SDimitry Andric StringLocationExp, Record[Offset], Record[Offset + 1], 153504eeddc0SDimitry Andric Record[Offset + 2])), 1536e8d8bef9SDimitry Andric NextMetadataNo); 1537e8d8bef9SDimitry Andric NextMetadataNo++; 1538e8d8bef9SDimitry Andric break; 1539e8d8bef9SDimitry Andric } 15400b57cec5SDimitry Andric case bitc::METADATA_DERIVED_TYPE: { 1541349cc55cSDimitry Andric if (Record.size() < 12 || Record.size() > 14) 15420b57cec5SDimitry Andric return error("Invalid record"); 15430b57cec5SDimitry Andric 15440b57cec5SDimitry Andric // DWARF address space is encoded as N->getDWARFAddressSpace() + 1. 0 means 15450b57cec5SDimitry Andric // that there is no DWARF address space associated with DIDerivedType. 1546bdd1243dSDimitry Andric std::optional<unsigned> DWARFAddressSpace; 15470b57cec5SDimitry Andric if (Record.size() > 12 && Record[12]) 15480b57cec5SDimitry Andric DWARFAddressSpace = Record[12] - 1; 15490b57cec5SDimitry Andric 1550349cc55cSDimitry Andric Metadata *Annotations = nullptr; 1551349cc55cSDimitry Andric if (Record.size() > 13 && Record[13]) 1552349cc55cSDimitry Andric Annotations = getMDOrNull(Record[13]); 1553349cc55cSDimitry Andric 15540b57cec5SDimitry Andric IsDistinct = Record[0]; 15550b57cec5SDimitry Andric DINode::DIFlags Flags = static_cast<DINode::DIFlags>(Record[10]); 15560b57cec5SDimitry Andric MetadataList.assignValue( 15570b57cec5SDimitry Andric GET_OR_DISTINCT(DIDerivedType, 15580b57cec5SDimitry Andric (Context, Record[1], getMDString(Record[2]), 15590b57cec5SDimitry Andric getMDOrNull(Record[3]), Record[4], 15600b57cec5SDimitry Andric getDITypeRefOrNull(Record[5]), 15610b57cec5SDimitry Andric getDITypeRefOrNull(Record[6]), Record[7], Record[8], 15620b57cec5SDimitry Andric Record[9], DWARFAddressSpace, Flags, 1563349cc55cSDimitry Andric getDITypeRefOrNull(Record[11]), Annotations)), 15640b57cec5SDimitry Andric NextMetadataNo); 15650b57cec5SDimitry Andric NextMetadataNo++; 15660b57cec5SDimitry Andric break; 15670b57cec5SDimitry Andric } 15680b57cec5SDimitry Andric case bitc::METADATA_COMPOSITE_TYPE: { 1569349cc55cSDimitry Andric if (Record.size() < 16 || Record.size() > 22) 15700b57cec5SDimitry Andric return error("Invalid record"); 15710b57cec5SDimitry Andric 15720b57cec5SDimitry Andric // If we have a UUID and this is not a forward declaration, lookup the 15730b57cec5SDimitry Andric // mapping. 15740b57cec5SDimitry Andric IsDistinct = Record[0] & 0x1; 15750b57cec5SDimitry Andric bool IsNotUsedInTypeRef = Record[0] >= 2; 15760b57cec5SDimitry Andric unsigned Tag = Record[1]; 15770b57cec5SDimitry Andric MDString *Name = getMDString(Record[2]); 15780b57cec5SDimitry Andric Metadata *File = getMDOrNull(Record[3]); 15790b57cec5SDimitry Andric unsigned Line = Record[4]; 15800b57cec5SDimitry Andric Metadata *Scope = getDITypeRefOrNull(Record[5]); 15810b57cec5SDimitry Andric Metadata *BaseType = nullptr; 15820b57cec5SDimitry Andric uint64_t SizeInBits = Record[7]; 15830b57cec5SDimitry Andric if (Record[8] > (uint64_t)std::numeric_limits<uint32_t>::max()) 15840b57cec5SDimitry Andric return error("Alignment value is too large"); 15850b57cec5SDimitry Andric uint32_t AlignInBits = Record[8]; 15860b57cec5SDimitry Andric uint64_t OffsetInBits = 0; 15870b57cec5SDimitry Andric DINode::DIFlags Flags = static_cast<DINode::DIFlags>(Record[10]); 15880b57cec5SDimitry Andric Metadata *Elements = nullptr; 15890b57cec5SDimitry Andric unsigned RuntimeLang = Record[12]; 15900b57cec5SDimitry Andric Metadata *VTableHolder = nullptr; 15910b57cec5SDimitry Andric Metadata *TemplateParams = nullptr; 15920b57cec5SDimitry Andric Metadata *Discriminator = nullptr; 15935ffd83dbSDimitry Andric Metadata *DataLocation = nullptr; 1594e8d8bef9SDimitry Andric Metadata *Associated = nullptr; 1595e8d8bef9SDimitry Andric Metadata *Allocated = nullptr; 1596e8d8bef9SDimitry Andric Metadata *Rank = nullptr; 1597349cc55cSDimitry Andric Metadata *Annotations = nullptr; 15980b57cec5SDimitry Andric auto *Identifier = getMDString(Record[15]); 15990b57cec5SDimitry Andric // If this module is being parsed so that it can be ThinLTO imported 16000b57cec5SDimitry Andric // into another module, composite types only need to be imported 16010b57cec5SDimitry Andric // as type declarations (unless full type definitions requested). 16020b57cec5SDimitry Andric // Create type declarations up front to save memory. Also, buildODRType 16030b57cec5SDimitry Andric // handles the case where this is type ODRed with a definition needed 16040b57cec5SDimitry Andric // by the importing module, in which case the existing definition is 16050b57cec5SDimitry Andric // used. 16060b57cec5SDimitry Andric if (IsImporting && !ImportFullTypeDefinitions && Identifier && 16070b57cec5SDimitry Andric (Tag == dwarf::DW_TAG_enumeration_type || 16080b57cec5SDimitry Andric Tag == dwarf::DW_TAG_class_type || 16090b57cec5SDimitry Andric Tag == dwarf::DW_TAG_structure_type || 16100b57cec5SDimitry Andric Tag == dwarf::DW_TAG_union_type)) { 16110b57cec5SDimitry Andric Flags = Flags | DINode::FlagFwdDecl; 161281ad6265SDimitry Andric if (Name) { 161381ad6265SDimitry Andric // This is a hack around preserving template parameters for simplified 161481ad6265SDimitry Andric // template names - it should probably be replaced with a 161581ad6265SDimitry Andric // DICompositeType flag specifying whether template parameters are 161681ad6265SDimitry Andric // required on declarations of this type. 161781ad6265SDimitry Andric StringRef NameStr = Name->getString(); 161881ad6265SDimitry Andric if (!NameStr.contains('<') || NameStr.startswith("_STN|")) 161981ad6265SDimitry Andric TemplateParams = getMDOrNull(Record[14]); 162081ad6265SDimitry Andric } 16210b57cec5SDimitry Andric } else { 16220b57cec5SDimitry Andric BaseType = getDITypeRefOrNull(Record[6]); 16230b57cec5SDimitry Andric OffsetInBits = Record[9]; 16240b57cec5SDimitry Andric Elements = getMDOrNull(Record[11]); 16250b57cec5SDimitry Andric VTableHolder = getDITypeRefOrNull(Record[13]); 16260b57cec5SDimitry Andric TemplateParams = getMDOrNull(Record[14]); 16270b57cec5SDimitry Andric if (Record.size() > 16) 16280b57cec5SDimitry Andric Discriminator = getMDOrNull(Record[16]); 16295ffd83dbSDimitry Andric if (Record.size() > 17) 16305ffd83dbSDimitry Andric DataLocation = getMDOrNull(Record[17]); 1631e8d8bef9SDimitry Andric if (Record.size() > 19) { 1632e8d8bef9SDimitry Andric Associated = getMDOrNull(Record[18]); 1633e8d8bef9SDimitry Andric Allocated = getMDOrNull(Record[19]); 1634e8d8bef9SDimitry Andric } 1635e8d8bef9SDimitry Andric if (Record.size() > 20) { 1636e8d8bef9SDimitry Andric Rank = getMDOrNull(Record[20]); 1637e8d8bef9SDimitry Andric } 1638349cc55cSDimitry Andric if (Record.size() > 21) { 1639349cc55cSDimitry Andric Annotations = getMDOrNull(Record[21]); 1640349cc55cSDimitry Andric } 16410b57cec5SDimitry Andric } 16420b57cec5SDimitry Andric DICompositeType *CT = nullptr; 16430b57cec5SDimitry Andric if (Identifier) 16440b57cec5SDimitry Andric CT = DICompositeType::buildODRType( 16450b57cec5SDimitry Andric Context, *Identifier, Tag, Name, File, Line, Scope, BaseType, 16460b57cec5SDimitry Andric SizeInBits, AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang, 1647e8d8bef9SDimitry Andric VTableHolder, TemplateParams, Discriminator, DataLocation, Associated, 1648349cc55cSDimitry Andric Allocated, Rank, Annotations); 16490b57cec5SDimitry Andric 16500b57cec5SDimitry Andric // Create a node if we didn't get a lazy ODR type. 16510b57cec5SDimitry Andric if (!CT) 16520b57cec5SDimitry Andric CT = GET_OR_DISTINCT(DICompositeType, 16530b57cec5SDimitry Andric (Context, Tag, Name, File, Line, Scope, BaseType, 16540b57cec5SDimitry Andric SizeInBits, AlignInBits, OffsetInBits, Flags, 16550b57cec5SDimitry Andric Elements, RuntimeLang, VTableHolder, TemplateParams, 1656e8d8bef9SDimitry Andric Identifier, Discriminator, DataLocation, Associated, 1657349cc55cSDimitry Andric Allocated, Rank, Annotations)); 16580b57cec5SDimitry Andric if (!IsNotUsedInTypeRef && Identifier) 16590b57cec5SDimitry Andric MetadataList.addTypeRef(*Identifier, *cast<DICompositeType>(CT)); 16600b57cec5SDimitry Andric 16610b57cec5SDimitry Andric MetadataList.assignValue(CT, NextMetadataNo); 16620b57cec5SDimitry Andric NextMetadataNo++; 16630b57cec5SDimitry Andric break; 16640b57cec5SDimitry Andric } 16650b57cec5SDimitry Andric case bitc::METADATA_SUBROUTINE_TYPE: { 16660b57cec5SDimitry Andric if (Record.size() < 3 || Record.size() > 4) 16670b57cec5SDimitry Andric return error("Invalid record"); 16680b57cec5SDimitry Andric bool IsOldTypeRefArray = Record[0] < 2; 16690b57cec5SDimitry Andric unsigned CC = (Record.size() > 3) ? Record[3] : 0; 16700b57cec5SDimitry Andric 16710b57cec5SDimitry Andric IsDistinct = Record[0] & 0x1; 16720b57cec5SDimitry Andric DINode::DIFlags Flags = static_cast<DINode::DIFlags>(Record[1]); 16730b57cec5SDimitry Andric Metadata *Types = getMDOrNull(Record[2]); 16740b57cec5SDimitry Andric if (LLVM_UNLIKELY(IsOldTypeRefArray)) 16750b57cec5SDimitry Andric Types = MetadataList.upgradeTypeRefArray(Types); 16760b57cec5SDimitry Andric 16770b57cec5SDimitry Andric MetadataList.assignValue( 16780b57cec5SDimitry Andric GET_OR_DISTINCT(DISubroutineType, (Context, Flags, CC, Types)), 16790b57cec5SDimitry Andric NextMetadataNo); 16800b57cec5SDimitry Andric NextMetadataNo++; 16810b57cec5SDimitry Andric break; 16820b57cec5SDimitry Andric } 16830b57cec5SDimitry Andric 16840b57cec5SDimitry Andric case bitc::METADATA_MODULE: { 1685e8d8bef9SDimitry Andric if (Record.size() < 5 || Record.size() > 9) 16860b57cec5SDimitry Andric return error("Invalid record"); 16870b57cec5SDimitry Andric 1688e8d8bef9SDimitry Andric unsigned Offset = Record.size() >= 8 ? 2 : 1; 16890b57cec5SDimitry Andric IsDistinct = Record[0]; 16900b57cec5SDimitry Andric MetadataList.assignValue( 16915ffd83dbSDimitry Andric GET_OR_DISTINCT( 16925ffd83dbSDimitry Andric DIModule, 1693e8d8bef9SDimitry Andric (Context, Record.size() >= 8 ? getMDOrNull(Record[1]) : nullptr, 16945ffd83dbSDimitry Andric getMDOrNull(Record[0 + Offset]), getMDString(Record[1 + Offset]), 16955ffd83dbSDimitry Andric getMDString(Record[2 + Offset]), getMDString(Record[3 + Offset]), 16965ffd83dbSDimitry Andric getMDString(Record[4 + Offset]), 1697e8d8bef9SDimitry Andric Record.size() <= 7 ? 0 : Record[7], 1698e8d8bef9SDimitry Andric Record.size() <= 8 ? false : Record[8])), 16990b57cec5SDimitry Andric NextMetadataNo); 17000b57cec5SDimitry Andric NextMetadataNo++; 17010b57cec5SDimitry Andric break; 17020b57cec5SDimitry Andric } 17030b57cec5SDimitry Andric 17040b57cec5SDimitry Andric case bitc::METADATA_FILE: { 17050b57cec5SDimitry Andric if (Record.size() != 3 && Record.size() != 5 && Record.size() != 6) 17060b57cec5SDimitry Andric return error("Invalid record"); 17070b57cec5SDimitry Andric 17080b57cec5SDimitry Andric IsDistinct = Record[0]; 1709bdd1243dSDimitry Andric std::optional<DIFile::ChecksumInfo<MDString *>> Checksum; 17100b57cec5SDimitry Andric // The BitcodeWriter writes null bytes into Record[3:4] when the Checksum 17110b57cec5SDimitry Andric // is not present. This matches up with the old internal representation, 17120b57cec5SDimitry Andric // and the old encoding for CSK_None in the ChecksumKind. The new 17130b57cec5SDimitry Andric // representation reserves the value 0 in the ChecksumKind to continue to 17140b57cec5SDimitry Andric // encode None in a backwards-compatible way. 17150b57cec5SDimitry Andric if (Record.size() > 4 && Record[3] && Record[4]) 17160b57cec5SDimitry Andric Checksum.emplace(static_cast<DIFile::ChecksumKind>(Record[3]), 17170b57cec5SDimitry Andric getMDString(Record[4])); 17180b57cec5SDimitry Andric MetadataList.assignValue( 1719bdd1243dSDimitry Andric GET_OR_DISTINCT(DIFile, 1720bdd1243dSDimitry Andric (Context, getMDString(Record[1]), 1721bdd1243dSDimitry Andric getMDString(Record[2]), Checksum, 1722bdd1243dSDimitry Andric Record.size() > 5 ? getMDString(Record[5]) : nullptr)), 17230b57cec5SDimitry Andric NextMetadataNo); 17240b57cec5SDimitry Andric NextMetadataNo++; 17250b57cec5SDimitry Andric break; 17260b57cec5SDimitry Andric } 17270b57cec5SDimitry Andric case bitc::METADATA_COMPILE_UNIT: { 17285ffd83dbSDimitry Andric if (Record.size() < 14 || Record.size() > 22) 17290b57cec5SDimitry Andric return error("Invalid record"); 17300b57cec5SDimitry Andric 17310b57cec5SDimitry Andric // Ignore Record[0], which indicates whether this compile unit is 17320b57cec5SDimitry Andric // distinct. It's always distinct. 17330b57cec5SDimitry Andric IsDistinct = true; 17340b57cec5SDimitry Andric auto *CU = DICompileUnit::getDistinct( 17350b57cec5SDimitry Andric Context, Record[1], getMDOrNull(Record[2]), getMDString(Record[3]), 17360b57cec5SDimitry Andric Record[4], getMDString(Record[5]), Record[6], getMDString(Record[7]), 17370b57cec5SDimitry Andric Record[8], getMDOrNull(Record[9]), getMDOrNull(Record[10]), 17380b57cec5SDimitry Andric getMDOrNull(Record[12]), getMDOrNull(Record[13]), 17390b57cec5SDimitry Andric Record.size() <= 15 ? nullptr : getMDOrNull(Record[15]), 17400b57cec5SDimitry Andric Record.size() <= 14 ? 0 : Record[14], 17410b57cec5SDimitry Andric Record.size() <= 16 ? true : Record[16], 17420b57cec5SDimitry Andric Record.size() <= 17 ? false : Record[17], 17430b57cec5SDimitry Andric Record.size() <= 18 ? 0 : Record[18], 174404eeddc0SDimitry Andric Record.size() <= 19 ? false : Record[19], 17455ffd83dbSDimitry Andric Record.size() <= 20 ? nullptr : getMDString(Record[20]), 17465ffd83dbSDimitry Andric Record.size() <= 21 ? nullptr : getMDString(Record[21])); 17470b57cec5SDimitry Andric 17480b57cec5SDimitry Andric MetadataList.assignValue(CU, NextMetadataNo); 17490b57cec5SDimitry Andric NextMetadataNo++; 17500b57cec5SDimitry Andric 17510b57cec5SDimitry Andric // Move the Upgrade the list of subprograms. 17520b57cec5SDimitry Andric if (Metadata *SPs = getMDOrNullWithoutPlaceholders(Record[11])) 17530b57cec5SDimitry Andric CUSubprograms.push_back({CU, SPs}); 17540b57cec5SDimitry Andric break; 17550b57cec5SDimitry Andric } 17560b57cec5SDimitry Andric case bitc::METADATA_SUBPROGRAM: { 17570b57cec5SDimitry Andric if (Record.size() < 18 || Record.size() > 21) 17580b57cec5SDimitry Andric return error("Invalid record"); 17590b57cec5SDimitry Andric 17600b57cec5SDimitry Andric bool HasSPFlags = Record[0] & 4; 17610b57cec5SDimitry Andric 17620b57cec5SDimitry Andric DINode::DIFlags Flags; 17630b57cec5SDimitry Andric DISubprogram::DISPFlags SPFlags; 17640b57cec5SDimitry Andric if (!HasSPFlags) 17650b57cec5SDimitry Andric Flags = static_cast<DINode::DIFlags>(Record[11 + 2]); 17660b57cec5SDimitry Andric else { 17670b57cec5SDimitry Andric Flags = static_cast<DINode::DIFlags>(Record[11]); 17680b57cec5SDimitry Andric SPFlags = static_cast<DISubprogram::DISPFlags>(Record[9]); 17690b57cec5SDimitry Andric } 17700b57cec5SDimitry Andric 17710b57cec5SDimitry Andric // Support for old metadata when 17720b57cec5SDimitry Andric // subprogram specific flags are placed in DIFlags. 17730b57cec5SDimitry Andric const unsigned DIFlagMainSubprogram = 1 << 21; 17740b57cec5SDimitry Andric bool HasOldMainSubprogramFlag = Flags & DIFlagMainSubprogram; 17750b57cec5SDimitry Andric if (HasOldMainSubprogramFlag) 17760b57cec5SDimitry Andric // Remove old DIFlagMainSubprogram from DIFlags. 17770b57cec5SDimitry Andric // Note: This assumes that any future use of bit 21 defaults to it 17780b57cec5SDimitry Andric // being 0. 17790b57cec5SDimitry Andric Flags &= ~static_cast<DINode::DIFlags>(DIFlagMainSubprogram); 17800b57cec5SDimitry Andric 17810b57cec5SDimitry Andric if (HasOldMainSubprogramFlag && HasSPFlags) 17820b57cec5SDimitry Andric SPFlags |= DISubprogram::SPFlagMainSubprogram; 17830b57cec5SDimitry Andric else if (!HasSPFlags) 17840b57cec5SDimitry Andric SPFlags = DISubprogram::toSPFlags( 17850b57cec5SDimitry Andric /*IsLocalToUnit=*/Record[7], /*IsDefinition=*/Record[8], 17860b57cec5SDimitry Andric /*IsOptimized=*/Record[14], /*Virtuality=*/Record[11], 178704eeddc0SDimitry Andric /*IsMainSubprogram=*/HasOldMainSubprogramFlag); 17880b57cec5SDimitry Andric 17890b57cec5SDimitry Andric // All definitions should be distinct. 17900b57cec5SDimitry Andric IsDistinct = (Record[0] & 1) || (SPFlags & DISubprogram::SPFlagDefinition); 17910b57cec5SDimitry Andric // Version 1 has a Function as Record[15]. 17920b57cec5SDimitry Andric // Version 2 has removed Record[15]. 17930b57cec5SDimitry Andric // Version 3 has the Unit as Record[15]. 17940b57cec5SDimitry Andric // Version 4 added thisAdjustment. 17950b57cec5SDimitry Andric // Version 5 repacked flags into DISPFlags, changing many element numbers. 17960b57cec5SDimitry Andric bool HasUnit = Record[0] & 2; 17970b57cec5SDimitry Andric if (!HasSPFlags && HasUnit && Record.size() < 19) 17980b57cec5SDimitry Andric return error("Invalid record"); 17990b57cec5SDimitry Andric if (HasSPFlags && !HasUnit) 18000b57cec5SDimitry Andric return error("Invalid record"); 18010b57cec5SDimitry Andric // Accommodate older formats. 18020b57cec5SDimitry Andric bool HasFn = false; 18030b57cec5SDimitry Andric bool HasThisAdj = true; 18040b57cec5SDimitry Andric bool HasThrownTypes = true; 1805349cc55cSDimitry Andric bool HasAnnotations = false; 180681ad6265SDimitry Andric bool HasTargetFuncName = false; 18070b57cec5SDimitry Andric unsigned OffsetA = 0; 18080b57cec5SDimitry Andric unsigned OffsetB = 0; 18090b57cec5SDimitry Andric if (!HasSPFlags) { 18100b57cec5SDimitry Andric OffsetA = 2; 18110b57cec5SDimitry Andric OffsetB = 2; 18120b57cec5SDimitry Andric if (Record.size() >= 19) { 18130b57cec5SDimitry Andric HasFn = !HasUnit; 18140b57cec5SDimitry Andric OffsetB++; 18150b57cec5SDimitry Andric } 18160b57cec5SDimitry Andric HasThisAdj = Record.size() >= 20; 18170b57cec5SDimitry Andric HasThrownTypes = Record.size() >= 21; 1818349cc55cSDimitry Andric } else { 1819349cc55cSDimitry Andric HasAnnotations = Record.size() >= 19; 182081ad6265SDimitry Andric HasTargetFuncName = Record.size() >= 20; 18210b57cec5SDimitry Andric } 18220b57cec5SDimitry Andric Metadata *CUorFn = getMDOrNull(Record[12 + OffsetB]); 18230b57cec5SDimitry Andric DISubprogram *SP = GET_OR_DISTINCT( 18240b57cec5SDimitry Andric DISubprogram, 18250b57cec5SDimitry Andric (Context, 18260b57cec5SDimitry Andric getDITypeRefOrNull(Record[1]), // scope 18270b57cec5SDimitry Andric getMDString(Record[2]), // name 18280b57cec5SDimitry Andric getMDString(Record[3]), // linkageName 18290b57cec5SDimitry Andric getMDOrNull(Record[4]), // file 18300b57cec5SDimitry Andric Record[5], // line 18310b57cec5SDimitry Andric getMDOrNull(Record[6]), // type 18320b57cec5SDimitry Andric Record[7 + OffsetA], // scopeLine 18330b57cec5SDimitry Andric getDITypeRefOrNull(Record[8 + OffsetA]), // containingType 18340b57cec5SDimitry Andric Record[10 + OffsetA], // virtualIndex 18350b57cec5SDimitry Andric HasThisAdj ? Record[16 + OffsetB] : 0, // thisAdjustment 18360b57cec5SDimitry Andric Flags, // flags 18370b57cec5SDimitry Andric SPFlags, // SPFlags 18380b57cec5SDimitry Andric HasUnit ? CUorFn : nullptr, // unit 18390b57cec5SDimitry Andric getMDOrNull(Record[13 + OffsetB]), // templateParams 18400b57cec5SDimitry Andric getMDOrNull(Record[14 + OffsetB]), // declaration 18410b57cec5SDimitry Andric getMDOrNull(Record[15 + OffsetB]), // retainedNodes 18420b57cec5SDimitry Andric HasThrownTypes ? getMDOrNull(Record[17 + OffsetB]) 1843349cc55cSDimitry Andric : nullptr, // thrownTypes 1844349cc55cSDimitry Andric HasAnnotations ? getMDOrNull(Record[18 + OffsetB]) 184581ad6265SDimitry Andric : nullptr, // annotations 184681ad6265SDimitry Andric HasTargetFuncName ? getMDString(Record[19 + OffsetB]) 184781ad6265SDimitry Andric : nullptr // targetFuncName 18480b57cec5SDimitry Andric )); 18490b57cec5SDimitry Andric MetadataList.assignValue(SP, NextMetadataNo); 18500b57cec5SDimitry Andric NextMetadataNo++; 18510b57cec5SDimitry Andric 18520b57cec5SDimitry Andric // Upgrade sp->function mapping to function->sp mapping. 18530b57cec5SDimitry Andric if (HasFn) { 18540b57cec5SDimitry Andric if (auto *CMD = dyn_cast_or_null<ConstantAsMetadata>(CUorFn)) 18550b57cec5SDimitry Andric if (auto *F = dyn_cast<Function>(CMD->getValue())) { 18560b57cec5SDimitry Andric if (F->isMaterializable()) 18570b57cec5SDimitry Andric // Defer until materialized; unmaterialized functions may not have 18580b57cec5SDimitry Andric // metadata. 18590b57cec5SDimitry Andric FunctionsWithSPs[F] = SP; 18600b57cec5SDimitry Andric else if (!F->empty()) 18610b57cec5SDimitry Andric F->setSubprogram(SP); 18620b57cec5SDimitry Andric } 18630b57cec5SDimitry Andric } 18640b57cec5SDimitry Andric break; 18650b57cec5SDimitry Andric } 18660b57cec5SDimitry Andric case bitc::METADATA_LEXICAL_BLOCK: { 18670b57cec5SDimitry Andric if (Record.size() != 5) 18680b57cec5SDimitry Andric return error("Invalid record"); 18690b57cec5SDimitry Andric 18700b57cec5SDimitry Andric IsDistinct = Record[0]; 18710b57cec5SDimitry Andric MetadataList.assignValue( 18720b57cec5SDimitry Andric GET_OR_DISTINCT(DILexicalBlock, 18730b57cec5SDimitry Andric (Context, getMDOrNull(Record[1]), 18740b57cec5SDimitry Andric getMDOrNull(Record[2]), Record[3], Record[4])), 18750b57cec5SDimitry Andric NextMetadataNo); 18760b57cec5SDimitry Andric NextMetadataNo++; 18770b57cec5SDimitry Andric break; 18780b57cec5SDimitry Andric } 18790b57cec5SDimitry Andric case bitc::METADATA_LEXICAL_BLOCK_FILE: { 18800b57cec5SDimitry Andric if (Record.size() != 4) 18810b57cec5SDimitry Andric return error("Invalid record"); 18820b57cec5SDimitry Andric 18830b57cec5SDimitry Andric IsDistinct = Record[0]; 18840b57cec5SDimitry Andric MetadataList.assignValue( 18850b57cec5SDimitry Andric GET_OR_DISTINCT(DILexicalBlockFile, 18860b57cec5SDimitry Andric (Context, getMDOrNull(Record[1]), 18870b57cec5SDimitry Andric getMDOrNull(Record[2]), Record[3])), 18880b57cec5SDimitry Andric NextMetadataNo); 18890b57cec5SDimitry Andric NextMetadataNo++; 18900b57cec5SDimitry Andric break; 18910b57cec5SDimitry Andric } 18920b57cec5SDimitry Andric case bitc::METADATA_COMMON_BLOCK: { 18930b57cec5SDimitry Andric IsDistinct = Record[0] & 1; 18940b57cec5SDimitry Andric MetadataList.assignValue( 18950b57cec5SDimitry Andric GET_OR_DISTINCT(DICommonBlock, 18960b57cec5SDimitry Andric (Context, getMDOrNull(Record[1]), 18970b57cec5SDimitry Andric getMDOrNull(Record[2]), getMDString(Record[3]), 18980b57cec5SDimitry Andric getMDOrNull(Record[4]), Record[5])), 18990b57cec5SDimitry Andric NextMetadataNo); 19000b57cec5SDimitry Andric NextMetadataNo++; 19010b57cec5SDimitry Andric break; 19020b57cec5SDimitry Andric } 19030b57cec5SDimitry Andric case bitc::METADATA_NAMESPACE: { 19040b57cec5SDimitry Andric // Newer versions of DINamespace dropped file and line. 19050b57cec5SDimitry Andric MDString *Name; 19060b57cec5SDimitry Andric if (Record.size() == 3) 19070b57cec5SDimitry Andric Name = getMDString(Record[2]); 19080b57cec5SDimitry Andric else if (Record.size() == 5) 19090b57cec5SDimitry Andric Name = getMDString(Record[3]); 19100b57cec5SDimitry Andric else 19110b57cec5SDimitry Andric return error("Invalid record"); 19120b57cec5SDimitry Andric 19130b57cec5SDimitry Andric IsDistinct = Record[0] & 1; 19140b57cec5SDimitry Andric bool ExportSymbols = Record[0] & 2; 19150b57cec5SDimitry Andric MetadataList.assignValue( 19160b57cec5SDimitry Andric GET_OR_DISTINCT(DINamespace, 19170b57cec5SDimitry Andric (Context, getMDOrNull(Record[1]), Name, ExportSymbols)), 19180b57cec5SDimitry Andric NextMetadataNo); 19190b57cec5SDimitry Andric NextMetadataNo++; 19200b57cec5SDimitry Andric break; 19210b57cec5SDimitry Andric } 19220b57cec5SDimitry Andric case bitc::METADATA_MACRO: { 19230b57cec5SDimitry Andric if (Record.size() != 5) 19240b57cec5SDimitry Andric return error("Invalid record"); 19250b57cec5SDimitry Andric 19260b57cec5SDimitry Andric IsDistinct = Record[0]; 19270b57cec5SDimitry Andric MetadataList.assignValue( 19280b57cec5SDimitry Andric GET_OR_DISTINCT(DIMacro, 19290b57cec5SDimitry Andric (Context, Record[1], Record[2], getMDString(Record[3]), 19300b57cec5SDimitry Andric getMDString(Record[4]))), 19310b57cec5SDimitry Andric NextMetadataNo); 19320b57cec5SDimitry Andric NextMetadataNo++; 19330b57cec5SDimitry Andric break; 19340b57cec5SDimitry Andric } 19350b57cec5SDimitry Andric case bitc::METADATA_MACRO_FILE: { 19360b57cec5SDimitry Andric if (Record.size() != 5) 19370b57cec5SDimitry Andric return error("Invalid record"); 19380b57cec5SDimitry Andric 19390b57cec5SDimitry Andric IsDistinct = Record[0]; 19400b57cec5SDimitry Andric MetadataList.assignValue( 19410b57cec5SDimitry Andric GET_OR_DISTINCT(DIMacroFile, 19420b57cec5SDimitry Andric (Context, Record[1], Record[2], getMDOrNull(Record[3]), 19430b57cec5SDimitry Andric getMDOrNull(Record[4]))), 19440b57cec5SDimitry Andric NextMetadataNo); 19450b57cec5SDimitry Andric NextMetadataNo++; 19460b57cec5SDimitry Andric break; 19470b57cec5SDimitry Andric } 19480b57cec5SDimitry Andric case bitc::METADATA_TEMPLATE_TYPE: { 19495ffd83dbSDimitry Andric if (Record.size() < 3 || Record.size() > 4) 19500b57cec5SDimitry Andric return error("Invalid record"); 19510b57cec5SDimitry Andric 19520b57cec5SDimitry Andric IsDistinct = Record[0]; 19535ffd83dbSDimitry Andric MetadataList.assignValue( 19545ffd83dbSDimitry Andric GET_OR_DISTINCT(DITemplateTypeParameter, 19550b57cec5SDimitry Andric (Context, getMDString(Record[1]), 19565ffd83dbSDimitry Andric getDITypeRefOrNull(Record[2]), 19575ffd83dbSDimitry Andric (Record.size() == 4) ? getMDOrNull(Record[3]) 19585ffd83dbSDimitry Andric : getMDOrNull(false))), 19590b57cec5SDimitry Andric NextMetadataNo); 19600b57cec5SDimitry Andric NextMetadataNo++; 19610b57cec5SDimitry Andric break; 19620b57cec5SDimitry Andric } 19630b57cec5SDimitry Andric case bitc::METADATA_TEMPLATE_VALUE: { 19645ffd83dbSDimitry Andric if (Record.size() < 5 || Record.size() > 6) 19650b57cec5SDimitry Andric return error("Invalid record"); 19660b57cec5SDimitry Andric 19670b57cec5SDimitry Andric IsDistinct = Record[0]; 19685ffd83dbSDimitry Andric 19690b57cec5SDimitry Andric MetadataList.assignValue( 19705ffd83dbSDimitry Andric GET_OR_DISTINCT( 19715ffd83dbSDimitry Andric DITemplateValueParameter, 19720b57cec5SDimitry Andric (Context, Record[1], getMDString(Record[2]), 19730b57cec5SDimitry Andric getDITypeRefOrNull(Record[3]), 19745ffd83dbSDimitry Andric (Record.size() == 6) ? getMDOrNull(Record[4]) : getMDOrNull(false), 19755ffd83dbSDimitry Andric (Record.size() == 6) ? getMDOrNull(Record[5]) 19765ffd83dbSDimitry Andric : getMDOrNull(Record[4]))), 19770b57cec5SDimitry Andric NextMetadataNo); 19780b57cec5SDimitry Andric NextMetadataNo++; 19790b57cec5SDimitry Andric break; 19800b57cec5SDimitry Andric } 19810b57cec5SDimitry Andric case bitc::METADATA_GLOBAL_VAR: { 19820b57cec5SDimitry Andric if (Record.size() < 11 || Record.size() > 13) 19830b57cec5SDimitry Andric return error("Invalid record"); 19840b57cec5SDimitry Andric 19850b57cec5SDimitry Andric IsDistinct = Record[0] & 1; 19860b57cec5SDimitry Andric unsigned Version = Record[0] >> 1; 19870b57cec5SDimitry Andric 19880b57cec5SDimitry Andric if (Version == 2) { 1989349cc55cSDimitry Andric Metadata *Annotations = nullptr; 1990349cc55cSDimitry Andric if (Record.size() > 12) 1991349cc55cSDimitry Andric Annotations = getMDOrNull(Record[12]); 1992349cc55cSDimitry Andric 19930b57cec5SDimitry Andric MetadataList.assignValue( 1994349cc55cSDimitry Andric GET_OR_DISTINCT(DIGlobalVariable, 1995349cc55cSDimitry Andric (Context, getMDOrNull(Record[1]), 1996349cc55cSDimitry Andric getMDString(Record[2]), getMDString(Record[3]), 1997349cc55cSDimitry Andric getMDOrNull(Record[4]), Record[5], 19980b57cec5SDimitry Andric getDITypeRefOrNull(Record[6]), Record[7], Record[8], 1999349cc55cSDimitry Andric getMDOrNull(Record[9]), getMDOrNull(Record[10]), 2000349cc55cSDimitry Andric Record[11], Annotations)), 20010b57cec5SDimitry Andric NextMetadataNo); 20020b57cec5SDimitry Andric 20030b57cec5SDimitry Andric NextMetadataNo++; 20040b57cec5SDimitry Andric } else if (Version == 1) { 20050b57cec5SDimitry Andric // No upgrade necessary. A null field will be introduced to indicate 20060b57cec5SDimitry Andric // that no parameter information is available. 20070b57cec5SDimitry Andric MetadataList.assignValue( 2008349cc55cSDimitry Andric GET_OR_DISTINCT( 2009349cc55cSDimitry Andric DIGlobalVariable, 2010349cc55cSDimitry Andric (Context, getMDOrNull(Record[1]), getMDString(Record[2]), 2011349cc55cSDimitry Andric getMDString(Record[3]), getMDOrNull(Record[4]), Record[5], 20120b57cec5SDimitry Andric getDITypeRefOrNull(Record[6]), Record[7], Record[8], 2013349cc55cSDimitry Andric getMDOrNull(Record[10]), nullptr, Record[11], nullptr)), 20140b57cec5SDimitry Andric NextMetadataNo); 20150b57cec5SDimitry Andric 20160b57cec5SDimitry Andric NextMetadataNo++; 20170b57cec5SDimitry Andric } else if (Version == 0) { 20180b57cec5SDimitry Andric // Upgrade old metadata, which stored a global variable reference or a 20190b57cec5SDimitry Andric // ConstantInt here. 20200b57cec5SDimitry Andric NeedUpgradeToDIGlobalVariableExpression = true; 20210b57cec5SDimitry Andric Metadata *Expr = getMDOrNull(Record[9]); 20220b57cec5SDimitry Andric uint32_t AlignInBits = 0; 20230b57cec5SDimitry Andric if (Record.size() > 11) { 20240b57cec5SDimitry Andric if (Record[11] > (uint64_t)std::numeric_limits<uint32_t>::max()) 20250b57cec5SDimitry Andric return error("Alignment value is too large"); 20260b57cec5SDimitry Andric AlignInBits = Record[11]; 20270b57cec5SDimitry Andric } 20280b57cec5SDimitry Andric GlobalVariable *Attach = nullptr; 20290b57cec5SDimitry Andric if (auto *CMD = dyn_cast_or_null<ConstantAsMetadata>(Expr)) { 20300b57cec5SDimitry Andric if (auto *GV = dyn_cast<GlobalVariable>(CMD->getValue())) { 20310b57cec5SDimitry Andric Attach = GV; 20320b57cec5SDimitry Andric Expr = nullptr; 20330b57cec5SDimitry Andric } else if (auto *CI = dyn_cast<ConstantInt>(CMD->getValue())) { 20340b57cec5SDimitry Andric Expr = DIExpression::get(Context, 20350b57cec5SDimitry Andric {dwarf::DW_OP_constu, CI->getZExtValue(), 20360b57cec5SDimitry Andric dwarf::DW_OP_stack_value}); 20370b57cec5SDimitry Andric } else { 20380b57cec5SDimitry Andric Expr = nullptr; 20390b57cec5SDimitry Andric } 20400b57cec5SDimitry Andric } 20410b57cec5SDimitry Andric DIGlobalVariable *DGV = GET_OR_DISTINCT( 20420b57cec5SDimitry Andric DIGlobalVariable, 20430b57cec5SDimitry Andric (Context, getMDOrNull(Record[1]), getMDString(Record[2]), 20440b57cec5SDimitry Andric getMDString(Record[3]), getMDOrNull(Record[4]), Record[5], 20450b57cec5SDimitry Andric getDITypeRefOrNull(Record[6]), Record[7], Record[8], 2046349cc55cSDimitry Andric getMDOrNull(Record[10]), nullptr, AlignInBits, nullptr)); 20470b57cec5SDimitry Andric 20480b57cec5SDimitry Andric DIGlobalVariableExpression *DGVE = nullptr; 20490b57cec5SDimitry Andric if (Attach || Expr) 20500b57cec5SDimitry Andric DGVE = DIGlobalVariableExpression::getDistinct( 20510b57cec5SDimitry Andric Context, DGV, Expr ? Expr : DIExpression::get(Context, {})); 20520b57cec5SDimitry Andric if (Attach) 20530b57cec5SDimitry Andric Attach->addDebugInfo(DGVE); 20540b57cec5SDimitry Andric 20550b57cec5SDimitry Andric auto *MDNode = Expr ? cast<Metadata>(DGVE) : cast<Metadata>(DGV); 20560b57cec5SDimitry Andric MetadataList.assignValue(MDNode, NextMetadataNo); 20570b57cec5SDimitry Andric NextMetadataNo++; 20580b57cec5SDimitry Andric } else 20590b57cec5SDimitry Andric return error("Invalid record"); 20600b57cec5SDimitry Andric 20610b57cec5SDimitry Andric break; 20620b57cec5SDimitry Andric } 2063bdd1243dSDimitry Andric case bitc::METADATA_ASSIGN_ID: { 2064bdd1243dSDimitry Andric if (Record.size() != 1) 2065bdd1243dSDimitry Andric return error("Invalid DIAssignID record."); 2066bdd1243dSDimitry Andric 2067bdd1243dSDimitry Andric IsDistinct = Record[0] & 1; 2068bdd1243dSDimitry Andric if (!IsDistinct) 2069bdd1243dSDimitry Andric return error("Invalid DIAssignID record. Must be distinct"); 2070bdd1243dSDimitry Andric 2071bdd1243dSDimitry Andric MetadataList.assignValue(DIAssignID::getDistinct(Context), NextMetadataNo); 2072bdd1243dSDimitry Andric NextMetadataNo++; 2073bdd1243dSDimitry Andric break; 2074bdd1243dSDimitry Andric } 20750b57cec5SDimitry Andric case bitc::METADATA_LOCAL_VAR: { 20760b57cec5SDimitry Andric // 10th field is for the obseleted 'inlinedAt:' field. 20770b57cec5SDimitry Andric if (Record.size() < 8 || Record.size() > 10) 20780b57cec5SDimitry Andric return error("Invalid record"); 20790b57cec5SDimitry Andric 20800b57cec5SDimitry Andric IsDistinct = Record[0] & 1; 20810b57cec5SDimitry Andric bool HasAlignment = Record[0] & 2; 20820b57cec5SDimitry Andric // 2nd field used to be an artificial tag, either DW_TAG_auto_variable or 20830b57cec5SDimitry Andric // DW_TAG_arg_variable, if we have alignment flag encoded it means, that 20840b57cec5SDimitry Andric // this is newer version of record which doesn't have artificial tag. 20850b57cec5SDimitry Andric bool HasTag = !HasAlignment && Record.size() > 8; 20860b57cec5SDimitry Andric DINode::DIFlags Flags = static_cast<DINode::DIFlags>(Record[7 + HasTag]); 20870b57cec5SDimitry Andric uint32_t AlignInBits = 0; 2088349cc55cSDimitry Andric Metadata *Annotations = nullptr; 20890b57cec5SDimitry Andric if (HasAlignment) { 2090349cc55cSDimitry Andric if (Record[8] > (uint64_t)std::numeric_limits<uint32_t>::max()) 20910b57cec5SDimitry Andric return error("Alignment value is too large"); 2092349cc55cSDimitry Andric AlignInBits = Record[8]; 2093349cc55cSDimitry Andric if (Record.size() > 9) 2094349cc55cSDimitry Andric Annotations = getMDOrNull(Record[9]); 20950b57cec5SDimitry Andric } 2096349cc55cSDimitry Andric 20970b57cec5SDimitry Andric MetadataList.assignValue( 20980b57cec5SDimitry Andric GET_OR_DISTINCT(DILocalVariable, 20990b57cec5SDimitry Andric (Context, getMDOrNull(Record[1 + HasTag]), 21000b57cec5SDimitry Andric getMDString(Record[2 + HasTag]), 21010b57cec5SDimitry Andric getMDOrNull(Record[3 + HasTag]), Record[4 + HasTag], 21020b57cec5SDimitry Andric getDITypeRefOrNull(Record[5 + HasTag]), 2103349cc55cSDimitry Andric Record[6 + HasTag], Flags, AlignInBits, Annotations)), 21040b57cec5SDimitry Andric NextMetadataNo); 21050b57cec5SDimitry Andric NextMetadataNo++; 21060b57cec5SDimitry Andric break; 21070b57cec5SDimitry Andric } 21080b57cec5SDimitry Andric case bitc::METADATA_LABEL: { 21090b57cec5SDimitry Andric if (Record.size() != 5) 21100b57cec5SDimitry Andric return error("Invalid record"); 21110b57cec5SDimitry Andric 21120b57cec5SDimitry Andric IsDistinct = Record[0] & 1; 21130b57cec5SDimitry Andric MetadataList.assignValue( 2114349cc55cSDimitry Andric GET_OR_DISTINCT(DILabel, (Context, getMDOrNull(Record[1]), 21150b57cec5SDimitry Andric getMDString(Record[2]), 21160b57cec5SDimitry Andric getMDOrNull(Record[3]), Record[4])), 21170b57cec5SDimitry Andric NextMetadataNo); 21180b57cec5SDimitry Andric NextMetadataNo++; 21190b57cec5SDimitry Andric break; 21200b57cec5SDimitry Andric } 21210b57cec5SDimitry Andric case bitc::METADATA_EXPRESSION: { 21220b57cec5SDimitry Andric if (Record.size() < 1) 21230b57cec5SDimitry Andric return error("Invalid record"); 21240b57cec5SDimitry Andric 21250b57cec5SDimitry Andric IsDistinct = Record[0] & 1; 21260b57cec5SDimitry Andric uint64_t Version = Record[0] >> 1; 21270b57cec5SDimitry Andric auto Elts = MutableArrayRef<uint64_t>(Record).slice(1); 21280b57cec5SDimitry Andric 21290b57cec5SDimitry Andric SmallVector<uint64_t, 6> Buffer; 21300b57cec5SDimitry Andric if (Error Err = upgradeDIExpression(Version, Elts, Buffer)) 21310b57cec5SDimitry Andric return Err; 21320b57cec5SDimitry Andric 2133349cc55cSDimitry Andric MetadataList.assignValue(GET_OR_DISTINCT(DIExpression, (Context, Elts)), 2134349cc55cSDimitry Andric NextMetadataNo); 21350b57cec5SDimitry Andric NextMetadataNo++; 21360b57cec5SDimitry Andric break; 21370b57cec5SDimitry Andric } 21380b57cec5SDimitry Andric case bitc::METADATA_GLOBAL_VAR_EXPR: { 21390b57cec5SDimitry Andric if (Record.size() != 3) 21400b57cec5SDimitry Andric return error("Invalid record"); 21410b57cec5SDimitry Andric 21420b57cec5SDimitry Andric IsDistinct = Record[0]; 21430b57cec5SDimitry Andric Metadata *Expr = getMDOrNull(Record[2]); 21440b57cec5SDimitry Andric if (!Expr) 21450b57cec5SDimitry Andric Expr = DIExpression::get(Context, {}); 21460b57cec5SDimitry Andric MetadataList.assignValue( 21470b57cec5SDimitry Andric GET_OR_DISTINCT(DIGlobalVariableExpression, 21480b57cec5SDimitry Andric (Context, getMDOrNull(Record[1]), Expr)), 21490b57cec5SDimitry Andric NextMetadataNo); 21500b57cec5SDimitry Andric NextMetadataNo++; 21510b57cec5SDimitry Andric break; 21520b57cec5SDimitry Andric } 21530b57cec5SDimitry Andric case bitc::METADATA_OBJC_PROPERTY: { 21540b57cec5SDimitry Andric if (Record.size() != 8) 21550b57cec5SDimitry Andric return error("Invalid record"); 21560b57cec5SDimitry Andric 21570b57cec5SDimitry Andric IsDistinct = Record[0]; 21580b57cec5SDimitry Andric MetadataList.assignValue( 21590b57cec5SDimitry Andric GET_OR_DISTINCT(DIObjCProperty, 21600b57cec5SDimitry Andric (Context, getMDString(Record[1]), 21610b57cec5SDimitry Andric getMDOrNull(Record[2]), Record[3], 21620b57cec5SDimitry Andric getMDString(Record[4]), getMDString(Record[5]), 21630b57cec5SDimitry Andric Record[6], getDITypeRefOrNull(Record[7]))), 21640b57cec5SDimitry Andric NextMetadataNo); 21650b57cec5SDimitry Andric NextMetadataNo++; 21660b57cec5SDimitry Andric break; 21670b57cec5SDimitry Andric } 21680b57cec5SDimitry Andric case bitc::METADATA_IMPORTED_ENTITY: { 216981ad6265SDimitry Andric if (Record.size() < 6 || Record.size() > 8) 217081ad6265SDimitry Andric return error("Invalid DIImportedEntity record"); 21710b57cec5SDimitry Andric 21720b57cec5SDimitry Andric IsDistinct = Record[0]; 2173349cc55cSDimitry Andric bool HasFile = (Record.size() >= 7); 2174349cc55cSDimitry Andric bool HasElements = (Record.size() >= 8); 21750b57cec5SDimitry Andric MetadataList.assignValue( 21760b57cec5SDimitry Andric GET_OR_DISTINCT(DIImportedEntity, 21770b57cec5SDimitry Andric (Context, Record[1], getMDOrNull(Record[2]), 21780b57cec5SDimitry Andric getDITypeRefOrNull(Record[3]), 21790b57cec5SDimitry Andric HasFile ? getMDOrNull(Record[6]) : nullptr, 2180349cc55cSDimitry Andric HasFile ? Record[4] : 0, getMDString(Record[5]), 2181349cc55cSDimitry Andric HasElements ? getMDOrNull(Record[7]) : nullptr)), 21820b57cec5SDimitry Andric NextMetadataNo); 21830b57cec5SDimitry Andric NextMetadataNo++; 21840b57cec5SDimitry Andric break; 21850b57cec5SDimitry Andric } 21860b57cec5SDimitry Andric case bitc::METADATA_STRING_OLD: { 21870b57cec5SDimitry Andric std::string String(Record.begin(), Record.end()); 21880b57cec5SDimitry Andric 21890b57cec5SDimitry Andric // Test for upgrading !llvm.loop. 21900b57cec5SDimitry Andric HasSeenOldLoopTags |= mayBeOldLoopAttachmentTag(String); 21910b57cec5SDimitry Andric ++NumMDStringLoaded; 21920b57cec5SDimitry Andric Metadata *MD = MDString::get(Context, String); 21930b57cec5SDimitry Andric MetadataList.assignValue(MD, NextMetadataNo); 21940b57cec5SDimitry Andric NextMetadataNo++; 21950b57cec5SDimitry Andric break; 21960b57cec5SDimitry Andric } 21970b57cec5SDimitry Andric case bitc::METADATA_STRINGS: { 21980b57cec5SDimitry Andric auto CreateNextMDString = [&](StringRef Str) { 21990b57cec5SDimitry Andric ++NumMDStringLoaded; 22000b57cec5SDimitry Andric MetadataList.assignValue(MDString::get(Context, Str), NextMetadataNo); 22010b57cec5SDimitry Andric NextMetadataNo++; 22020b57cec5SDimitry Andric }; 22030b57cec5SDimitry Andric if (Error Err = parseMetadataStrings(Record, Blob, CreateNextMDString)) 22040b57cec5SDimitry Andric return Err; 22050b57cec5SDimitry Andric break; 22060b57cec5SDimitry Andric } 22070b57cec5SDimitry Andric case bitc::METADATA_GLOBAL_DECL_ATTACHMENT: { 22080b57cec5SDimitry Andric if (Record.size() % 2 == 0) 22090b57cec5SDimitry Andric return error("Invalid record"); 22100b57cec5SDimitry Andric unsigned ValueID = Record[0]; 22110b57cec5SDimitry Andric if (ValueID >= ValueList.size()) 22120b57cec5SDimitry Andric return error("Invalid record"); 22130b57cec5SDimitry Andric if (auto *GO = dyn_cast<GlobalObject>(ValueList[ValueID])) 22140b57cec5SDimitry Andric if (Error Err = parseGlobalObjectAttachment( 22150b57cec5SDimitry Andric *GO, ArrayRef<uint64_t>(Record).slice(1))) 22160b57cec5SDimitry Andric return Err; 22170b57cec5SDimitry Andric break; 22180b57cec5SDimitry Andric } 22190b57cec5SDimitry Andric case bitc::METADATA_KIND: { 22200b57cec5SDimitry Andric // Support older bitcode files that had METADATA_KIND records in a 22210b57cec5SDimitry Andric // block with METADATA_BLOCK_ID. 22220b57cec5SDimitry Andric if (Error Err = parseMetadataKindRecord(Record)) 22230b57cec5SDimitry Andric return Err; 22240b57cec5SDimitry Andric break; 22250b57cec5SDimitry Andric } 2226fe6060f1SDimitry Andric case bitc::METADATA_ARG_LIST: { 2227fe6060f1SDimitry Andric SmallVector<ValueAsMetadata *, 4> Elts; 2228fe6060f1SDimitry Andric Elts.reserve(Record.size()); 2229fe6060f1SDimitry Andric for (uint64_t Elt : Record) { 2230fe6060f1SDimitry Andric Metadata *MD = getMD(Elt); 2231fe6060f1SDimitry Andric if (isa<MDNode>(MD) && cast<MDNode>(MD)->isTemporary()) 2232fe6060f1SDimitry Andric return error( 2233fe6060f1SDimitry Andric "Invalid record: DIArgList should not contain forward refs"); 2234fe6060f1SDimitry Andric if (!isa<ValueAsMetadata>(MD)) 2235fe6060f1SDimitry Andric return error("Invalid record"); 2236fe6060f1SDimitry Andric Elts.push_back(cast<ValueAsMetadata>(MD)); 2237fe6060f1SDimitry Andric } 2238fe6060f1SDimitry Andric 2239fe6060f1SDimitry Andric MetadataList.assignValue(DIArgList::get(Context, Elts), NextMetadataNo); 2240fe6060f1SDimitry Andric NextMetadataNo++; 2241fe6060f1SDimitry Andric break; 2242fe6060f1SDimitry Andric } 22430b57cec5SDimitry Andric } 22440b57cec5SDimitry Andric return Error::success(); 22450b57cec5SDimitry Andric #undef GET_OR_DISTINCT 22460b57cec5SDimitry Andric } 22470b57cec5SDimitry Andric 22480b57cec5SDimitry Andric Error MetadataLoader::MetadataLoaderImpl::parseMetadataStrings( 22490b57cec5SDimitry Andric ArrayRef<uint64_t> Record, StringRef Blob, 22500b57cec5SDimitry Andric function_ref<void(StringRef)> CallBack) { 22510b57cec5SDimitry Andric // All the MDStrings in the block are emitted together in a single 22520b57cec5SDimitry Andric // record. The strings are concatenated and stored in a blob along with 22530b57cec5SDimitry Andric // their sizes. 22540b57cec5SDimitry Andric if (Record.size() != 2) 22550b57cec5SDimitry Andric return error("Invalid record: metadata strings layout"); 22560b57cec5SDimitry Andric 22570b57cec5SDimitry Andric unsigned NumStrings = Record[0]; 22580b57cec5SDimitry Andric unsigned StringsOffset = Record[1]; 22590b57cec5SDimitry Andric if (!NumStrings) 22600b57cec5SDimitry Andric return error("Invalid record: metadata strings with no strings"); 22610b57cec5SDimitry Andric if (StringsOffset > Blob.size()) 22620b57cec5SDimitry Andric return error("Invalid record: metadata strings corrupt offset"); 22630b57cec5SDimitry Andric 22640b57cec5SDimitry Andric StringRef Lengths = Blob.slice(0, StringsOffset); 22650b57cec5SDimitry Andric SimpleBitstreamCursor R(Lengths); 22660b57cec5SDimitry Andric 22670b57cec5SDimitry Andric StringRef Strings = Blob.drop_front(StringsOffset); 22680b57cec5SDimitry Andric do { 22690b57cec5SDimitry Andric if (R.AtEndOfStream()) 22700b57cec5SDimitry Andric return error("Invalid record: metadata strings bad length"); 22710b57cec5SDimitry Andric 2272349cc55cSDimitry Andric uint32_t Size; 2273349cc55cSDimitry Andric if (Error E = R.ReadVBR(6).moveInto(Size)) 2274349cc55cSDimitry Andric return E; 22750b57cec5SDimitry Andric if (Strings.size() < Size) 22760b57cec5SDimitry Andric return error("Invalid record: metadata strings truncated chars"); 22770b57cec5SDimitry Andric 22780b57cec5SDimitry Andric CallBack(Strings.slice(0, Size)); 22790b57cec5SDimitry Andric Strings = Strings.drop_front(Size); 22800b57cec5SDimitry Andric } while (--NumStrings); 22810b57cec5SDimitry Andric 22820b57cec5SDimitry Andric return Error::success(); 22830b57cec5SDimitry Andric } 22840b57cec5SDimitry Andric 22850b57cec5SDimitry Andric Error MetadataLoader::MetadataLoaderImpl::parseGlobalObjectAttachment( 22860b57cec5SDimitry Andric GlobalObject &GO, ArrayRef<uint64_t> Record) { 22870b57cec5SDimitry Andric assert(Record.size() % 2 == 0); 22880b57cec5SDimitry Andric for (unsigned I = 0, E = Record.size(); I != E; I += 2) { 22890b57cec5SDimitry Andric auto K = MDKindMap.find(Record[I]); 22900b57cec5SDimitry Andric if (K == MDKindMap.end()) 22910b57cec5SDimitry Andric return error("Invalid ID"); 2292e8d8bef9SDimitry Andric MDNode *MD = 2293e8d8bef9SDimitry Andric dyn_cast_or_null<MDNode>(getMetadataFwdRefOrLoad(Record[I + 1])); 22940b57cec5SDimitry Andric if (!MD) 22950b57cec5SDimitry Andric return error("Invalid metadata attachment: expect fwd ref to MDNode"); 22960b57cec5SDimitry Andric GO.addMetadata(K->second, *MD); 22970b57cec5SDimitry Andric } 22980b57cec5SDimitry Andric return Error::success(); 22990b57cec5SDimitry Andric } 23000b57cec5SDimitry Andric 23010b57cec5SDimitry Andric /// Parse metadata attachments. 23020b57cec5SDimitry Andric Error MetadataLoader::MetadataLoaderImpl::parseMetadataAttachment( 230381ad6265SDimitry Andric Function &F, ArrayRef<Instruction *> InstructionList) { 23040b57cec5SDimitry Andric if (Error Err = Stream.EnterSubBlock(bitc::METADATA_ATTACHMENT_ID)) 23050b57cec5SDimitry Andric return Err; 23060b57cec5SDimitry Andric 23070b57cec5SDimitry Andric SmallVector<uint64_t, 64> Record; 23080b57cec5SDimitry Andric PlaceholderQueue Placeholders; 23090b57cec5SDimitry Andric 23100b57cec5SDimitry Andric while (true) { 2311349cc55cSDimitry Andric BitstreamEntry Entry; 2312349cc55cSDimitry Andric if (Error E = Stream.advanceSkippingSubblocks().moveInto(Entry)) 2313349cc55cSDimitry Andric return E; 23140b57cec5SDimitry Andric 23150b57cec5SDimitry Andric switch (Entry.Kind) { 23160b57cec5SDimitry Andric case BitstreamEntry::SubBlock: // Handled for us already. 23170b57cec5SDimitry Andric case BitstreamEntry::Error: 23180b57cec5SDimitry Andric return error("Malformed block"); 23190b57cec5SDimitry Andric case BitstreamEntry::EndBlock: 23200b57cec5SDimitry Andric resolveForwardRefsAndPlaceholders(Placeholders); 23210b57cec5SDimitry Andric return Error::success(); 23220b57cec5SDimitry Andric case BitstreamEntry::Record: 23230b57cec5SDimitry Andric // The interesting case. 23240b57cec5SDimitry Andric break; 23250b57cec5SDimitry Andric } 23260b57cec5SDimitry Andric 23270b57cec5SDimitry Andric // Read a metadata attachment record. 23280b57cec5SDimitry Andric Record.clear(); 23290b57cec5SDimitry Andric ++NumMDRecordLoaded; 23300b57cec5SDimitry Andric Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record); 23310b57cec5SDimitry Andric if (!MaybeRecord) 23320b57cec5SDimitry Andric return MaybeRecord.takeError(); 23330b57cec5SDimitry Andric switch (MaybeRecord.get()) { 23340b57cec5SDimitry Andric default: // Default behavior: ignore. 23350b57cec5SDimitry Andric break; 23360b57cec5SDimitry Andric case bitc::METADATA_ATTACHMENT: { 23370b57cec5SDimitry Andric unsigned RecordLength = Record.size(); 23380b57cec5SDimitry Andric if (Record.empty()) 23390b57cec5SDimitry Andric return error("Invalid record"); 23400b57cec5SDimitry Andric if (RecordLength % 2 == 0) { 23410b57cec5SDimitry Andric // A function attachment. 23420b57cec5SDimitry Andric if (Error Err = parseGlobalObjectAttachment(F, Record)) 23430b57cec5SDimitry Andric return Err; 23440b57cec5SDimitry Andric continue; 23450b57cec5SDimitry Andric } 23460b57cec5SDimitry Andric 23470b57cec5SDimitry Andric // An instruction attachment. 23480b57cec5SDimitry Andric Instruction *Inst = InstructionList[Record[0]]; 23490b57cec5SDimitry Andric for (unsigned i = 1; i != RecordLength; i = i + 2) { 23500b57cec5SDimitry Andric unsigned Kind = Record[i]; 23510b57cec5SDimitry Andric DenseMap<unsigned, unsigned>::iterator I = MDKindMap.find(Kind); 23520b57cec5SDimitry Andric if (I == MDKindMap.end()) 23530b57cec5SDimitry Andric return error("Invalid ID"); 23540b57cec5SDimitry Andric if (I->second == LLVMContext::MD_tbaa && StripTBAA) 23550b57cec5SDimitry Andric continue; 23560b57cec5SDimitry Andric 23570b57cec5SDimitry Andric auto Idx = Record[i + 1]; 23580b57cec5SDimitry Andric if (Idx < (MDStringRef.size() + GlobalMetadataBitPosIndex.size()) && 23590b57cec5SDimitry Andric !MetadataList.lookup(Idx)) { 23600b57cec5SDimitry Andric // Load the attachment if it is in the lazy-loadable range and hasn't 23610b57cec5SDimitry Andric // been loaded yet. 23620b57cec5SDimitry Andric lazyLoadOneMetadata(Idx, Placeholders); 23630b57cec5SDimitry Andric resolveForwardRefsAndPlaceholders(Placeholders); 23640b57cec5SDimitry Andric } 23650b57cec5SDimitry Andric 23660b57cec5SDimitry Andric Metadata *Node = MetadataList.getMetadataFwdRef(Idx); 23670b57cec5SDimitry Andric if (isa<LocalAsMetadata>(Node)) 23680b57cec5SDimitry Andric // Drop the attachment. This used to be legal, but there's no 23690b57cec5SDimitry Andric // upgrade path. 23700b57cec5SDimitry Andric break; 23710b57cec5SDimitry Andric MDNode *MD = dyn_cast_or_null<MDNode>(Node); 23720b57cec5SDimitry Andric if (!MD) 23730b57cec5SDimitry Andric return error("Invalid metadata attachment"); 23740b57cec5SDimitry Andric 23750b57cec5SDimitry Andric if (HasSeenOldLoopTags && I->second == LLVMContext::MD_loop) 23760b57cec5SDimitry Andric MD = upgradeInstructionLoopAttachment(*MD); 23770b57cec5SDimitry Andric 23780b57cec5SDimitry Andric if (I->second == LLVMContext::MD_tbaa) { 23790b57cec5SDimitry Andric assert(!MD->isTemporary() && "should load MDs before attachments"); 23800b57cec5SDimitry Andric MD = UpgradeTBAANode(*MD); 23810b57cec5SDimitry Andric } 23820b57cec5SDimitry Andric Inst->setMetadata(I->second, MD); 23830b57cec5SDimitry Andric } 23840b57cec5SDimitry Andric break; 23850b57cec5SDimitry Andric } 23860b57cec5SDimitry Andric } 23870b57cec5SDimitry Andric } 23880b57cec5SDimitry Andric } 23890b57cec5SDimitry Andric 23900b57cec5SDimitry Andric /// Parse a single METADATA_KIND record, inserting result in MDKindMap. 23910b57cec5SDimitry Andric Error MetadataLoader::MetadataLoaderImpl::parseMetadataKindRecord( 23920b57cec5SDimitry Andric SmallVectorImpl<uint64_t> &Record) { 23930b57cec5SDimitry Andric if (Record.size() < 2) 23940b57cec5SDimitry Andric return error("Invalid record"); 23950b57cec5SDimitry Andric 23960b57cec5SDimitry Andric unsigned Kind = Record[0]; 23970b57cec5SDimitry Andric SmallString<8> Name(Record.begin() + 1, Record.end()); 23980b57cec5SDimitry Andric 23990b57cec5SDimitry Andric unsigned NewKind = TheModule.getMDKindID(Name.str()); 24000b57cec5SDimitry Andric if (!MDKindMap.insert(std::make_pair(Kind, NewKind)).second) 24010b57cec5SDimitry Andric return error("Conflicting METADATA_KIND records"); 24020b57cec5SDimitry Andric return Error::success(); 24030b57cec5SDimitry Andric } 24040b57cec5SDimitry Andric 24050b57cec5SDimitry Andric /// Parse the metadata kinds out of the METADATA_KIND_BLOCK. 24060b57cec5SDimitry Andric Error MetadataLoader::MetadataLoaderImpl::parseMetadataKinds() { 24070b57cec5SDimitry Andric if (Error Err = Stream.EnterSubBlock(bitc::METADATA_KIND_BLOCK_ID)) 24080b57cec5SDimitry Andric return Err; 24090b57cec5SDimitry Andric 24100b57cec5SDimitry Andric SmallVector<uint64_t, 64> Record; 24110b57cec5SDimitry Andric 24120b57cec5SDimitry Andric // Read all the records. 24130b57cec5SDimitry Andric while (true) { 2414349cc55cSDimitry Andric BitstreamEntry Entry; 2415349cc55cSDimitry Andric if (Error E = Stream.advanceSkippingSubblocks().moveInto(Entry)) 2416349cc55cSDimitry Andric return E; 24170b57cec5SDimitry Andric 24180b57cec5SDimitry Andric switch (Entry.Kind) { 24190b57cec5SDimitry Andric case BitstreamEntry::SubBlock: // Handled for us already. 24200b57cec5SDimitry Andric case BitstreamEntry::Error: 24210b57cec5SDimitry Andric return error("Malformed block"); 24220b57cec5SDimitry Andric case BitstreamEntry::EndBlock: 24230b57cec5SDimitry Andric return Error::success(); 24240b57cec5SDimitry Andric case BitstreamEntry::Record: 24250b57cec5SDimitry Andric // The interesting case. 24260b57cec5SDimitry Andric break; 24270b57cec5SDimitry Andric } 24280b57cec5SDimitry Andric 24290b57cec5SDimitry Andric // Read a record. 24300b57cec5SDimitry Andric Record.clear(); 24310b57cec5SDimitry Andric ++NumMDRecordLoaded; 24320b57cec5SDimitry Andric Expected<unsigned> MaybeCode = Stream.readRecord(Entry.ID, Record); 24330b57cec5SDimitry Andric if (!MaybeCode) 24340b57cec5SDimitry Andric return MaybeCode.takeError(); 24350b57cec5SDimitry Andric switch (MaybeCode.get()) { 24360b57cec5SDimitry Andric default: // Default behavior: ignore. 24370b57cec5SDimitry Andric break; 24380b57cec5SDimitry Andric case bitc::METADATA_KIND: { 24390b57cec5SDimitry Andric if (Error Err = parseMetadataKindRecord(Record)) 24400b57cec5SDimitry Andric return Err; 24410b57cec5SDimitry Andric break; 24420b57cec5SDimitry Andric } 24430b57cec5SDimitry Andric } 24440b57cec5SDimitry Andric } 24450b57cec5SDimitry Andric } 24460b57cec5SDimitry Andric 24470b57cec5SDimitry Andric MetadataLoader &MetadataLoader::operator=(MetadataLoader &&RHS) { 24480b57cec5SDimitry Andric Pimpl = std::move(RHS.Pimpl); 24490b57cec5SDimitry Andric return *this; 24500b57cec5SDimitry Andric } 24510b57cec5SDimitry Andric MetadataLoader::MetadataLoader(MetadataLoader &&RHS) 24520b57cec5SDimitry Andric : Pimpl(std::move(RHS.Pimpl)) {} 24530b57cec5SDimitry Andric 24540b57cec5SDimitry Andric MetadataLoader::~MetadataLoader() = default; 24550b57cec5SDimitry Andric MetadataLoader::MetadataLoader(BitstreamCursor &Stream, Module &TheModule, 24560b57cec5SDimitry Andric BitcodeReaderValueList &ValueList, 24570b57cec5SDimitry Andric bool IsImporting, 2458bdd1243dSDimitry Andric MetadataLoaderCallbacks Callbacks) 24598bcb0991SDimitry Andric : Pimpl(std::make_unique<MetadataLoaderImpl>( 2460bdd1243dSDimitry Andric Stream, TheModule, ValueList, std::move(Callbacks), IsImporting)) {} 24610b57cec5SDimitry Andric 24620b57cec5SDimitry Andric Error MetadataLoader::parseMetadata(bool ModuleLevel) { 24630b57cec5SDimitry Andric return Pimpl->parseMetadata(ModuleLevel); 24640b57cec5SDimitry Andric } 24650b57cec5SDimitry Andric 24660b57cec5SDimitry Andric bool MetadataLoader::hasFwdRefs() const { return Pimpl->hasFwdRefs(); } 24670b57cec5SDimitry Andric 24680b57cec5SDimitry Andric /// Return the given metadata, creating a replaceable forward reference if 24690b57cec5SDimitry Andric /// necessary. 24700b57cec5SDimitry Andric Metadata *MetadataLoader::getMetadataFwdRefOrLoad(unsigned Idx) { 24710b57cec5SDimitry Andric return Pimpl->getMetadataFwdRefOrLoad(Idx); 24720b57cec5SDimitry Andric } 24730b57cec5SDimitry Andric 24740b57cec5SDimitry Andric DISubprogram *MetadataLoader::lookupSubprogramForFunction(Function *F) { 24750b57cec5SDimitry Andric return Pimpl->lookupSubprogramForFunction(F); 24760b57cec5SDimitry Andric } 24770b57cec5SDimitry Andric 24780b57cec5SDimitry Andric Error MetadataLoader::parseMetadataAttachment( 247981ad6265SDimitry Andric Function &F, ArrayRef<Instruction *> InstructionList) { 24800b57cec5SDimitry Andric return Pimpl->parseMetadataAttachment(F, InstructionList); 24810b57cec5SDimitry Andric } 24820b57cec5SDimitry Andric 24830b57cec5SDimitry Andric Error MetadataLoader::parseMetadataKinds() { 24840b57cec5SDimitry Andric return Pimpl->parseMetadataKinds(); 24850b57cec5SDimitry Andric } 24860b57cec5SDimitry Andric 24870b57cec5SDimitry Andric void MetadataLoader::setStripTBAA(bool StripTBAA) { 24880b57cec5SDimitry Andric return Pimpl->setStripTBAA(StripTBAA); 24890b57cec5SDimitry Andric } 24900b57cec5SDimitry Andric 24910b57cec5SDimitry Andric bool MetadataLoader::isStrippingTBAA() { return Pimpl->isStrippingTBAA(); } 24920b57cec5SDimitry Andric 24930b57cec5SDimitry Andric unsigned MetadataLoader::size() const { return Pimpl->size(); } 24940b57cec5SDimitry Andric void MetadataLoader::shrinkTo(unsigned N) { return Pimpl->shrinkTo(N); } 24950b57cec5SDimitry Andric 24960b57cec5SDimitry Andric void MetadataLoader::upgradeDebugIntrinsics(Function &F) { 24970b57cec5SDimitry Andric return Pimpl->upgradeDebugIntrinsics(F); 24980b57cec5SDimitry Andric } 2499