Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 1 | //===- MetadataLoader.cpp - Internal BitcodeReader implementation ---------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "MetadataLoader.h" |
| 11 | #include "ValueList.h" |
| 12 | |
| 13 | #include "llvm/ADT/APFloat.h" |
| 14 | #include "llvm/ADT/APInt.h" |
| 15 | #include "llvm/ADT/ArrayRef.h" |
| 16 | #include "llvm/ADT/DenseMap.h" |
| 17 | #include "llvm/ADT/None.h" |
| 18 | #include "llvm/ADT/STLExtras.h" |
| 19 | #include "llvm/ADT/SmallString.h" |
| 20 | #include "llvm/ADT/SmallVector.h" |
| 21 | #include "llvm/ADT/StringRef.h" |
| 22 | #include "llvm/ADT/Triple.h" |
| 23 | #include "llvm/ADT/Twine.h" |
| 24 | #include "llvm/Bitcode/BitcodeReader.h" |
| 25 | #include "llvm/Bitcode/BitstreamReader.h" |
| 26 | #include "llvm/Bitcode/LLVMBitCodes.h" |
| 27 | #include "llvm/IR/Argument.h" |
| 28 | #include "llvm/IR/Attributes.h" |
| 29 | #include "llvm/IR/AutoUpgrade.h" |
| 30 | #include "llvm/IR/BasicBlock.h" |
| 31 | #include "llvm/IR/CallSite.h" |
| 32 | #include "llvm/IR/CallingConv.h" |
| 33 | #include "llvm/IR/Comdat.h" |
| 34 | #include "llvm/IR/Constant.h" |
| 35 | #include "llvm/IR/Constants.h" |
| 36 | #include "llvm/IR/DebugInfo.h" |
| 37 | #include "llvm/IR/DebugInfoMetadata.h" |
| 38 | #include "llvm/IR/DebugLoc.h" |
| 39 | #include "llvm/IR/DerivedTypes.h" |
| 40 | #include "llvm/IR/DiagnosticInfo.h" |
| 41 | #include "llvm/IR/DiagnosticPrinter.h" |
| 42 | #include "llvm/IR/Function.h" |
| 43 | #include "llvm/IR/GVMaterializer.h" |
| 44 | #include "llvm/IR/GlobalAlias.h" |
| 45 | #include "llvm/IR/GlobalIFunc.h" |
| 46 | #include "llvm/IR/GlobalIndirectSymbol.h" |
| 47 | #include "llvm/IR/GlobalObject.h" |
| 48 | #include "llvm/IR/GlobalValue.h" |
| 49 | #include "llvm/IR/GlobalVariable.h" |
| 50 | #include "llvm/IR/InlineAsm.h" |
| 51 | #include "llvm/IR/InstrTypes.h" |
| 52 | #include "llvm/IR/Instruction.h" |
| 53 | #include "llvm/IR/Instructions.h" |
| 54 | #include "llvm/IR/Intrinsics.h" |
| 55 | #include "llvm/IR/LLVMContext.h" |
| 56 | #include "llvm/IR/Module.h" |
| 57 | #include "llvm/IR/ModuleSummaryIndex.h" |
| 58 | #include "llvm/IR/OperandTraits.h" |
| 59 | #include "llvm/IR/Operator.h" |
| 60 | #include "llvm/IR/TrackingMDRef.h" |
| 61 | #include "llvm/IR/Type.h" |
| 62 | #include "llvm/IR/ValueHandle.h" |
| 63 | #include "llvm/Support/AtomicOrdering.h" |
| 64 | #include "llvm/Support/Casting.h" |
| 65 | #include "llvm/Support/CommandLine.h" |
| 66 | #include "llvm/Support/Compiler.h" |
| 67 | #include "llvm/Support/Debug.h" |
| 68 | #include "llvm/Support/Error.h" |
| 69 | #include "llvm/Support/ErrorHandling.h" |
| 70 | #include "llvm/Support/ManagedStatic.h" |
| 71 | #include "llvm/Support/MemoryBuffer.h" |
| 72 | #include "llvm/Support/raw_ostream.h" |
| 73 | #include <algorithm> |
| 74 | #include <cassert> |
| 75 | #include <cstddef> |
| 76 | #include <cstdint> |
| 77 | #include <deque> |
| 78 | #include <limits> |
| 79 | #include <map> |
| 80 | #include <memory> |
| 81 | #include <string> |
| 82 | #include <system_error> |
| 83 | #include <tuple> |
| 84 | #include <utility> |
| 85 | #include <vector> |
| 86 | |
| 87 | using namespace llvm; |
| 88 | |
| 89 | namespace { |
| 90 | |
| 91 | static int64_t unrotateSign(uint64_t U) { return U & 1 ? ~(U >> 1) : U >> 1; } |
| 92 | |
| 93 | class BitcodeReaderMetadataList { |
| 94 | unsigned NumFwdRefs; |
| 95 | bool AnyFwdRefs; |
| 96 | unsigned MinFwdRef; |
| 97 | unsigned MaxFwdRef; |
| 98 | |
| 99 | /// Array of metadata references. |
| 100 | /// |
| 101 | /// Don't use std::vector here. Some versions of libc++ copy (instead of |
| 102 | /// move) on resize, and TrackingMDRef is very expensive to copy. |
| 103 | SmallVector<TrackingMDRef, 1> MetadataPtrs; |
| 104 | |
| 105 | /// Structures for resolving old type refs. |
| 106 | struct { |
| 107 | SmallDenseMap<MDString *, TempMDTuple, 1> Unknown; |
| 108 | SmallDenseMap<MDString *, DICompositeType *, 1> Final; |
| 109 | SmallDenseMap<MDString *, DICompositeType *, 1> FwdDecls; |
| 110 | SmallVector<std::pair<TrackingMDRef, TempMDTuple>, 1> Arrays; |
| 111 | } OldTypeRefs; |
| 112 | |
| 113 | LLVMContext &Context; |
| 114 | |
| 115 | public: |
| 116 | BitcodeReaderMetadataList(LLVMContext &C) |
| 117 | : NumFwdRefs(0), AnyFwdRefs(false), Context(C) {} |
| 118 | |
| 119 | // vector compatibility methods |
| 120 | unsigned size() const { return MetadataPtrs.size(); } |
| 121 | void resize(unsigned N) { MetadataPtrs.resize(N); } |
| 122 | void push_back(Metadata *MD) { MetadataPtrs.emplace_back(MD); } |
| 123 | void clear() { MetadataPtrs.clear(); } |
| 124 | Metadata *back() const { return MetadataPtrs.back(); } |
| 125 | void pop_back() { MetadataPtrs.pop_back(); } |
| 126 | bool empty() const { return MetadataPtrs.empty(); } |
| 127 | |
| 128 | Metadata *operator[](unsigned i) const { |
| 129 | assert(i < MetadataPtrs.size()); |
| 130 | return MetadataPtrs[i]; |
| 131 | } |
| 132 | |
| 133 | Metadata *lookup(unsigned I) const { |
| 134 | if (I < MetadataPtrs.size()) |
| 135 | return MetadataPtrs[I]; |
| 136 | return nullptr; |
| 137 | } |
| 138 | |
| 139 | void shrinkTo(unsigned N) { |
| 140 | assert(N <= size() && "Invalid shrinkTo request!"); |
| 141 | assert(!AnyFwdRefs && "Unexpected forward refs"); |
| 142 | MetadataPtrs.resize(N); |
| 143 | } |
| 144 | |
| 145 | /// Return the given metadata, creating a replaceable forward reference if |
| 146 | /// necessary. |
| 147 | Metadata *getMetadataFwdRef(unsigned Idx); |
| 148 | |
| 149 | /// Return the the given metadata only if it is fully resolved. |
| 150 | /// |
| 151 | /// Gives the same result as \a lookup(), unless \a MDNode::isResolved() |
| 152 | /// would give \c false. |
| 153 | Metadata *getMetadataIfResolved(unsigned Idx); |
| 154 | |
| 155 | MDNode *getMDNodeFwdRefOrNull(unsigned Idx); |
| 156 | void assignValue(Metadata *MD, unsigned Idx); |
| 157 | void tryToResolveCycles(); |
| 158 | bool hasFwdRefs() const { return AnyFwdRefs; } |
| 159 | |
| 160 | /// Upgrade a type that had an MDString reference. |
| 161 | void addTypeRef(MDString &UUID, DICompositeType &CT); |
| 162 | |
| 163 | /// Upgrade a type that had an MDString reference. |
| 164 | Metadata *upgradeTypeRef(Metadata *MaybeUUID); |
| 165 | |
| 166 | /// Upgrade a type ref array that may have MDString references. |
| 167 | Metadata *upgradeTypeRefArray(Metadata *MaybeTuple); |
| 168 | |
| 169 | private: |
| 170 | Metadata *resolveTypeRefArray(Metadata *MaybeTuple); |
| 171 | }; |
| 172 | |
| 173 | void BitcodeReaderMetadataList::assignValue(Metadata *MD, unsigned Idx) { |
| 174 | if (Idx == size()) { |
| 175 | push_back(MD); |
| 176 | return; |
| 177 | } |
| 178 | |
| 179 | if (Idx >= size()) |
| 180 | resize(Idx + 1); |
| 181 | |
| 182 | TrackingMDRef &OldMD = MetadataPtrs[Idx]; |
| 183 | if (!OldMD) { |
| 184 | OldMD.reset(MD); |
| 185 | return; |
| 186 | } |
| 187 | |
| 188 | // If there was a forward reference to this value, replace it. |
| 189 | TempMDTuple PrevMD(cast<MDTuple>(OldMD.get())); |
| 190 | PrevMD->replaceAllUsesWith(MD); |
| 191 | --NumFwdRefs; |
| 192 | } |
| 193 | |
| 194 | Metadata *BitcodeReaderMetadataList::getMetadataFwdRef(unsigned Idx) { |
| 195 | if (Idx >= size()) |
| 196 | resize(Idx + 1); |
| 197 | |
| 198 | if (Metadata *MD = MetadataPtrs[Idx]) |
| 199 | return MD; |
| 200 | |
| 201 | // Track forward refs to be resolved later. |
| 202 | if (AnyFwdRefs) { |
| 203 | MinFwdRef = std::min(MinFwdRef, Idx); |
| 204 | MaxFwdRef = std::max(MaxFwdRef, Idx); |
| 205 | } else { |
| 206 | AnyFwdRefs = true; |
| 207 | MinFwdRef = MaxFwdRef = Idx; |
| 208 | } |
| 209 | ++NumFwdRefs; |
| 210 | |
| 211 | // Create and return a placeholder, which will later be RAUW'd. |
| 212 | Metadata *MD = MDNode::getTemporary(Context, None).release(); |
| 213 | MetadataPtrs[Idx].reset(MD); |
| 214 | return MD; |
| 215 | } |
| 216 | |
| 217 | Metadata *BitcodeReaderMetadataList::getMetadataIfResolved(unsigned Idx) { |
| 218 | Metadata *MD = lookup(Idx); |
| 219 | if (auto *N = dyn_cast_or_null<MDNode>(MD)) |
| 220 | if (!N->isResolved()) |
| 221 | return nullptr; |
| 222 | return MD; |
| 223 | } |
| 224 | |
| 225 | MDNode *BitcodeReaderMetadataList::getMDNodeFwdRefOrNull(unsigned Idx) { |
| 226 | return dyn_cast_or_null<MDNode>(getMetadataFwdRef(Idx)); |
| 227 | } |
| 228 | |
| 229 | void BitcodeReaderMetadataList::tryToResolveCycles() { |
| 230 | if (NumFwdRefs) |
| 231 | // Still forward references... can't resolve cycles. |
| 232 | return; |
| 233 | |
| 234 | bool DidReplaceTypeRefs = false; |
| 235 | |
| 236 | // Give up on finding a full definition for any forward decls that remain. |
| 237 | for (const auto &Ref : OldTypeRefs.FwdDecls) |
| 238 | OldTypeRefs.Final.insert(Ref); |
| 239 | OldTypeRefs.FwdDecls.clear(); |
| 240 | |
| 241 | // Upgrade from old type ref arrays. In strange cases, this could add to |
| 242 | // OldTypeRefs.Unknown. |
| 243 | for (const auto &Array : OldTypeRefs.Arrays) { |
| 244 | DidReplaceTypeRefs = true; |
| 245 | Array.second->replaceAllUsesWith(resolveTypeRefArray(Array.first.get())); |
| 246 | } |
| 247 | OldTypeRefs.Arrays.clear(); |
| 248 | |
| 249 | // Replace old string-based type refs with the resolved node, if possible. |
| 250 | // If we haven't seen the node, leave it to the verifier to complain about |
| 251 | // the invalid string reference. |
| 252 | for (const auto &Ref : OldTypeRefs.Unknown) { |
| 253 | DidReplaceTypeRefs = true; |
| 254 | if (DICompositeType *CT = OldTypeRefs.Final.lookup(Ref.first)) |
| 255 | Ref.second->replaceAllUsesWith(CT); |
| 256 | else |
| 257 | Ref.second->replaceAllUsesWith(Ref.first); |
| 258 | } |
| 259 | OldTypeRefs.Unknown.clear(); |
| 260 | |
| 261 | // Make sure all the upgraded types are resolved. |
| 262 | if (DidReplaceTypeRefs) { |
| 263 | AnyFwdRefs = true; |
| 264 | MinFwdRef = 0; |
| 265 | MaxFwdRef = MetadataPtrs.size() - 1; |
| 266 | } |
| 267 | |
| 268 | if (!AnyFwdRefs) |
| 269 | // Nothing to do. |
| 270 | return; |
| 271 | |
| 272 | // Resolve any cycles. |
| 273 | for (unsigned I = MinFwdRef, E = MaxFwdRef + 1; I != E; ++I) { |
| 274 | auto &MD = MetadataPtrs[I]; |
| 275 | auto *N = dyn_cast_or_null<MDNode>(MD); |
| 276 | if (!N) |
| 277 | continue; |
| 278 | |
| 279 | assert(!N->isTemporary() && "Unexpected forward reference"); |
| 280 | N->resolveCycles(); |
| 281 | } |
| 282 | |
| 283 | // Make sure we return early again until there's another forward ref. |
| 284 | AnyFwdRefs = false; |
| 285 | } |
| 286 | |
| 287 | void BitcodeReaderMetadataList::addTypeRef(MDString &UUID, |
| 288 | DICompositeType &CT) { |
| 289 | assert(CT.getRawIdentifier() == &UUID && "Mismatched UUID"); |
| 290 | if (CT.isForwardDecl()) |
| 291 | OldTypeRefs.FwdDecls.insert(std::make_pair(&UUID, &CT)); |
| 292 | else |
| 293 | OldTypeRefs.Final.insert(std::make_pair(&UUID, &CT)); |
| 294 | } |
| 295 | |
| 296 | Metadata *BitcodeReaderMetadataList::upgradeTypeRef(Metadata *MaybeUUID) { |
| 297 | auto *UUID = dyn_cast_or_null<MDString>(MaybeUUID); |
| 298 | if (LLVM_LIKELY(!UUID)) |
| 299 | return MaybeUUID; |
| 300 | |
| 301 | if (auto *CT = OldTypeRefs.Final.lookup(UUID)) |
| 302 | return CT; |
| 303 | |
| 304 | auto &Ref = OldTypeRefs.Unknown[UUID]; |
| 305 | if (!Ref) |
| 306 | Ref = MDNode::getTemporary(Context, None); |
| 307 | return Ref.get(); |
| 308 | } |
| 309 | |
| 310 | Metadata *BitcodeReaderMetadataList::upgradeTypeRefArray(Metadata *MaybeTuple) { |
| 311 | auto *Tuple = dyn_cast_or_null<MDTuple>(MaybeTuple); |
| 312 | if (!Tuple || Tuple->isDistinct()) |
| 313 | return MaybeTuple; |
| 314 | |
| 315 | // Look through the array immediately if possible. |
| 316 | if (!Tuple->isTemporary()) |
| 317 | return resolveTypeRefArray(Tuple); |
| 318 | |
| 319 | // Create and return a placeholder to use for now. Eventually |
| 320 | // resolveTypeRefArrays() will be resolve this forward reference. |
| 321 | OldTypeRefs.Arrays.emplace_back( |
| 322 | std::piecewise_construct, std::forward_as_tuple(Tuple), |
| 323 | std::forward_as_tuple(MDTuple::getTemporary(Context, None))); |
| 324 | return OldTypeRefs.Arrays.back().second.get(); |
| 325 | } |
| 326 | |
| 327 | Metadata *BitcodeReaderMetadataList::resolveTypeRefArray(Metadata *MaybeTuple) { |
| 328 | auto *Tuple = dyn_cast_or_null<MDTuple>(MaybeTuple); |
| 329 | if (!Tuple || Tuple->isDistinct()) |
| 330 | return MaybeTuple; |
| 331 | |
| 332 | // Look through the DITypeRefArray, upgrading each DITypeRef. |
| 333 | SmallVector<Metadata *, 32> Ops; |
| 334 | Ops.reserve(Tuple->getNumOperands()); |
| 335 | for (Metadata *MD : Tuple->operands()) |
| 336 | Ops.push_back(upgradeTypeRef(MD)); |
| 337 | |
| 338 | return MDTuple::get(Context, Ops); |
| 339 | } |
| 340 | |
| 341 | namespace { |
| 342 | |
| 343 | class PlaceholderQueue { |
| 344 | // Placeholders would thrash around when moved, so store in a std::deque |
| 345 | // instead of some sort of vector. |
| 346 | std::deque<DistinctMDOperandPlaceholder> PHs; |
| 347 | |
| 348 | public: |
| 349 | DistinctMDOperandPlaceholder &getPlaceholderOp(unsigned ID); |
| 350 | void flush(BitcodeReaderMetadataList &MetadataList); |
| 351 | }; |
| 352 | |
| 353 | } // end anonymous namespace |
| 354 | |
| 355 | DistinctMDOperandPlaceholder &PlaceholderQueue::getPlaceholderOp(unsigned ID) { |
| 356 | PHs.emplace_back(ID); |
| 357 | return PHs.back(); |
| 358 | } |
| 359 | |
| 360 | void PlaceholderQueue::flush(BitcodeReaderMetadataList &MetadataList) { |
| 361 | while (!PHs.empty()) { |
| 362 | PHs.front().replaceUseWith( |
| 363 | MetadataList.getMetadataFwdRef(PHs.front().getID())); |
| 364 | PHs.pop_front(); |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | } // anonynous namespace |
| 369 | |
| 370 | class MetadataLoader::MetadataLoaderImpl { |
| 371 | BitcodeReaderMetadataList MetadataList; |
| 372 | BitcodeReaderValueList &ValueList; |
| 373 | BitstreamCursor &Stream; |
| 374 | LLVMContext &Context; |
| 375 | Module &TheModule; |
| 376 | std::function<Type *(unsigned)> getTypeByID; |
| 377 | |
| 378 | /// Functions that need to be matched with subprograms when upgrading old |
| 379 | /// metadata. |
| 380 | SmallDenseMap<Function *, DISubprogram *, 16> FunctionsWithSPs; |
| 381 | |
| 382 | // Map the bitcode's custom MDKind ID to the Module's MDKind ID. |
| 383 | DenseMap<unsigned, unsigned> MDKindMap; |
| 384 | |
| 385 | bool HasSeenOldLoopTags = false; |
| 386 | |
| 387 | Error parseMetadataStrings(ArrayRef<uint64_t> Record, StringRef Blob, |
| 388 | unsigned &NextMetadataNo); |
| 389 | Error parseGlobalObjectAttachment(GlobalObject &GO, |
| 390 | ArrayRef<uint64_t> Record); |
| 391 | Error parseMetadataKindRecord(SmallVectorImpl<uint64_t> &Record); |
| 392 | |
| 393 | public: |
| 394 | MetadataLoaderImpl(BitstreamCursor &Stream, Module &TheModule, |
| 395 | BitcodeReaderValueList &ValueList, |
| 396 | std::function<Type *(unsigned)> getTypeByID) |
| 397 | : MetadataList(TheModule.getContext()), ValueList(ValueList), |
| 398 | Stream(Stream), Context(TheModule.getContext()), TheModule(TheModule), |
| 399 | getTypeByID(getTypeByID) {} |
| 400 | |
| 401 | Error parseMetadata(bool ModuleLevel); |
| 402 | |
| 403 | bool hasFwdRefs() const { return MetadataList.hasFwdRefs(); } |
| 404 | Metadata *getMetadataFwdRef(unsigned Idx) { |
| 405 | return MetadataList.getMetadataFwdRef(Idx); |
| 406 | } |
| 407 | |
| 408 | MDNode *getMDNodeFwdRefOrNull(unsigned Idx) { |
| 409 | return MetadataList.getMDNodeFwdRefOrNull(Idx); |
| 410 | } |
| 411 | |
| 412 | DISubprogram *lookupSubprogramForFunction(Function *F) { |
| 413 | return FunctionsWithSPs.lookup(F); |
| 414 | } |
| 415 | |
| 416 | bool hasSeenOldLoopTags() { return HasSeenOldLoopTags; } |
| 417 | |
| 418 | Error parseMetadataAttachment( |
| 419 | Function &F, const SmallVectorImpl<Instruction *> &InstructionList); |
| 420 | |
| 421 | Error parseMetadataKinds(); |
| 422 | |
| 423 | unsigned size() const { return MetadataList.size(); } |
| 424 | void shrinkTo(unsigned N) { MetadataList.shrinkTo(N); } |
| 425 | }; |
| 426 | |
| 427 | Error error(const Twine &Message) { |
| 428 | return make_error<StringError>( |
| 429 | Message, make_error_code(BitcodeError::CorruptedBitcode)); |
| 430 | } |
| 431 | |
| 432 | /// Parse a METADATA_BLOCK. If ModuleLevel is true then we are parsing |
| 433 | /// module level metadata. |
| 434 | Error MetadataLoader::MetadataLoaderImpl::parseMetadata(bool ModuleLevel) { |
| 435 | if (!ModuleLevel && MetadataList.hasFwdRefs()) |
| 436 | return error("Invalid metadata: fwd refs into function blocks"); |
| 437 | |
| 438 | if (Stream.EnterSubBlock(bitc::METADATA_BLOCK_ID)) |
| 439 | return error("Invalid record"); |
| 440 | |
| 441 | unsigned NextMetadataNo = MetadataList.size(); |
| 442 | std::vector<std::pair<DICompileUnit *, Metadata *>> CUSubprograms; |
| 443 | SmallVector<uint64_t, 64> Record; |
| 444 | |
| 445 | PlaceholderQueue Placeholders; |
| 446 | bool IsDistinct; |
| 447 | auto getMD = [&](unsigned ID) -> Metadata * { |
| 448 | if (!IsDistinct) |
| 449 | return MetadataList.getMetadataFwdRef(ID); |
| 450 | if (auto *MD = MetadataList.getMetadataIfResolved(ID)) |
| 451 | return MD; |
| 452 | return &Placeholders.getPlaceholderOp(ID); |
| 453 | }; |
| 454 | auto getMDOrNull = [&](unsigned ID) -> Metadata * { |
| 455 | if (ID) |
| 456 | return getMD(ID - 1); |
| 457 | return nullptr; |
| 458 | }; |
| 459 | auto getMDOrNullWithoutPlaceholders = [&](unsigned ID) -> Metadata * { |
| 460 | if (ID) |
| 461 | return MetadataList.getMetadataFwdRef(ID - 1); |
| 462 | return nullptr; |
| 463 | }; |
| 464 | auto getMDString = [&](unsigned ID) -> MDString * { |
| 465 | // This requires that the ID is not really a forward reference. In |
| 466 | // particular, the MDString must already have been resolved. |
| 467 | return cast_or_null<MDString>(getMDOrNull(ID)); |
| 468 | }; |
| 469 | |
| 470 | // Support for old type refs. |
| 471 | auto getDITypeRefOrNull = [&](unsigned ID) { |
| 472 | return MetadataList.upgradeTypeRef(getMDOrNull(ID)); |
| 473 | }; |
| 474 | |
| 475 | #define GET_OR_DISTINCT(CLASS, ARGS) \ |
| 476 | (IsDistinct ? CLASS::getDistinct ARGS : CLASS::get ARGS) |
| 477 | |
| 478 | // Read all the records. |
| 479 | while (true) { |
| 480 | BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); |
| 481 | |
| 482 | switch (Entry.Kind) { |
| 483 | case BitstreamEntry::SubBlock: // Handled for us already. |
| 484 | case BitstreamEntry::Error: |
| 485 | return error("Malformed block"); |
| 486 | case BitstreamEntry::EndBlock: |
| 487 | // Upgrade old-style CU <-> SP pointers to point from SP to CU. |
| 488 | for (auto CU_SP : CUSubprograms) |
| 489 | if (auto *SPs = dyn_cast_or_null<MDTuple>(CU_SP.second)) |
| 490 | for (auto &Op : SPs->operands()) |
| 491 | if (auto *SP = dyn_cast_or_null<MDNode>(Op)) |
| 492 | SP->replaceOperandWith(7, CU_SP.first); |
| 493 | |
| 494 | MetadataList.tryToResolveCycles(); |
| 495 | Placeholders.flush(MetadataList); |
| 496 | return Error::success(); |
| 497 | case BitstreamEntry::Record: |
| 498 | // The interesting case. |
| 499 | break; |
| 500 | } |
| 501 | |
| 502 | // Read a record. |
| 503 | Record.clear(); |
| 504 | StringRef Blob; |
| 505 | unsigned Code = Stream.readRecord(Entry.ID, Record, &Blob); |
| 506 | IsDistinct = false; |
| 507 | switch (Code) { |
| 508 | default: // Default behavior: ignore. |
| 509 | break; |
| 510 | case bitc::METADATA_NAME: { |
| 511 | // Read name of the named metadata. |
| 512 | SmallString<8> Name(Record.begin(), Record.end()); |
| 513 | Record.clear(); |
| 514 | Code = Stream.ReadCode(); |
| 515 | |
| 516 | unsigned NextBitCode = Stream.readRecord(Code, Record); |
| 517 | if (NextBitCode != bitc::METADATA_NAMED_NODE) |
| 518 | return error("METADATA_NAME not followed by METADATA_NAMED_NODE"); |
| 519 | |
| 520 | // Read named metadata elements. |
| 521 | unsigned Size = Record.size(); |
| 522 | NamedMDNode *NMD = TheModule.getOrInsertNamedMetadata(Name); |
| 523 | for (unsigned i = 0; i != Size; ++i) { |
| 524 | MDNode *MD = MetadataList.getMDNodeFwdRefOrNull(Record[i]); |
| 525 | if (!MD) |
| 526 | return error("Invalid record"); |
| 527 | NMD->addOperand(MD); |
| 528 | } |
| 529 | break; |
| 530 | } |
| 531 | case bitc::METADATA_OLD_FN_NODE: { |
| 532 | // FIXME: Remove in 4.0. |
| 533 | // This is a LocalAsMetadata record, the only type of function-local |
| 534 | // metadata. |
| 535 | if (Record.size() % 2 == 1) |
| 536 | return error("Invalid record"); |
| 537 | |
| 538 | // If this isn't a LocalAsMetadata record, we're dropping it. This used |
| 539 | // to be legal, but there's no upgrade path. |
| 540 | auto dropRecord = [&] { |
| 541 | MetadataList.assignValue(MDNode::get(Context, None), NextMetadataNo++); |
| 542 | }; |
| 543 | if (Record.size() != 2) { |
| 544 | dropRecord(); |
| 545 | break; |
| 546 | } |
| 547 | |
| 548 | Type *Ty = getTypeByID(Record[0]); |
| 549 | if (Ty->isMetadataTy() || Ty->isVoidTy()) { |
| 550 | dropRecord(); |
| 551 | break; |
| 552 | } |
| 553 | |
| 554 | MetadataList.assignValue( |
| 555 | LocalAsMetadata::get(ValueList.getValueFwdRef(Record[1], Ty)), |
| 556 | NextMetadataNo++); |
| 557 | break; |
| 558 | } |
| 559 | case bitc::METADATA_OLD_NODE: { |
| 560 | // FIXME: Remove in 4.0. |
| 561 | if (Record.size() % 2 == 1) |
| 562 | return error("Invalid record"); |
| 563 | |
| 564 | unsigned Size = Record.size(); |
| 565 | SmallVector<Metadata *, 8> Elts; |
| 566 | for (unsigned i = 0; i != Size; i += 2) { |
| 567 | Type *Ty = getTypeByID(Record[i]); |
| 568 | if (!Ty) |
| 569 | return error("Invalid record"); |
| 570 | if (Ty->isMetadataTy()) |
| 571 | Elts.push_back(getMD(Record[i + 1])); |
| 572 | else if (!Ty->isVoidTy()) { |
| 573 | auto *MD = |
| 574 | ValueAsMetadata::get(ValueList.getValueFwdRef(Record[i + 1], Ty)); |
| 575 | assert(isa<ConstantAsMetadata>(MD) && |
| 576 | "Expected non-function-local metadata"); |
| 577 | Elts.push_back(MD); |
| 578 | } else |
| 579 | Elts.push_back(nullptr); |
| 580 | } |
| 581 | MetadataList.assignValue(MDNode::get(Context, Elts), NextMetadataNo++); |
| 582 | break; |
| 583 | } |
| 584 | case bitc::METADATA_VALUE: { |
| 585 | if (Record.size() != 2) |
| 586 | return error("Invalid record"); |
| 587 | |
| 588 | Type *Ty = getTypeByID(Record[0]); |
| 589 | if (Ty->isMetadataTy() || Ty->isVoidTy()) |
| 590 | return error("Invalid record"); |
| 591 | |
| 592 | MetadataList.assignValue( |
| 593 | ValueAsMetadata::get(ValueList.getValueFwdRef(Record[1], Ty)), |
| 594 | NextMetadataNo++); |
| 595 | break; |
| 596 | } |
| 597 | case bitc::METADATA_DISTINCT_NODE: |
| 598 | IsDistinct = true; |
| 599 | LLVM_FALLTHROUGH; |
| 600 | case bitc::METADATA_NODE: { |
| 601 | SmallVector<Metadata *, 8> Elts; |
| 602 | Elts.reserve(Record.size()); |
| 603 | for (unsigned ID : Record) |
| 604 | Elts.push_back(getMDOrNull(ID)); |
| 605 | MetadataList.assignValue(IsDistinct ? MDNode::getDistinct(Context, Elts) |
| 606 | : MDNode::get(Context, Elts), |
| 607 | NextMetadataNo++); |
| 608 | break; |
| 609 | } |
| 610 | case bitc::METADATA_LOCATION: { |
| 611 | if (Record.size() != 5) |
| 612 | return error("Invalid record"); |
| 613 | |
| 614 | IsDistinct = Record[0]; |
| 615 | unsigned Line = Record[1]; |
| 616 | unsigned Column = Record[2]; |
| 617 | Metadata *Scope = getMD(Record[3]); |
| 618 | Metadata *InlinedAt = getMDOrNull(Record[4]); |
| 619 | MetadataList.assignValue( |
| 620 | GET_OR_DISTINCT(DILocation, |
| 621 | (Context, Line, Column, Scope, InlinedAt)), |
| 622 | NextMetadataNo++); |
| 623 | break; |
| 624 | } |
| 625 | case bitc::METADATA_GENERIC_DEBUG: { |
| 626 | if (Record.size() < 4) |
| 627 | return error("Invalid record"); |
| 628 | |
| 629 | IsDistinct = Record[0]; |
| 630 | unsigned Tag = Record[1]; |
| 631 | unsigned Version = Record[2]; |
| 632 | |
| 633 | if (Tag >= 1u << 16 || Version != 0) |
| 634 | return error("Invalid record"); |
| 635 | |
| 636 | auto *Header = getMDString(Record[3]); |
| 637 | SmallVector<Metadata *, 8> DwarfOps; |
| 638 | for (unsigned I = 4, E = Record.size(); I != E; ++I) |
| 639 | DwarfOps.push_back(getMDOrNull(Record[I])); |
| 640 | MetadataList.assignValue( |
| 641 | GET_OR_DISTINCT(GenericDINode, (Context, Tag, Header, DwarfOps)), |
| 642 | NextMetadataNo++); |
| 643 | break; |
| 644 | } |
| 645 | case bitc::METADATA_SUBRANGE: { |
| 646 | if (Record.size() != 3) |
| 647 | return error("Invalid record"); |
| 648 | |
| 649 | IsDistinct = Record[0]; |
| 650 | MetadataList.assignValue( |
| 651 | GET_OR_DISTINCT(DISubrange, |
| 652 | (Context, Record[1], unrotateSign(Record[2]))), |
| 653 | NextMetadataNo++); |
| 654 | break; |
| 655 | } |
| 656 | case bitc::METADATA_ENUMERATOR: { |
| 657 | if (Record.size() != 3) |
| 658 | return error("Invalid record"); |
| 659 | |
| 660 | IsDistinct = Record[0]; |
| 661 | MetadataList.assignValue( |
| 662 | GET_OR_DISTINCT(DIEnumerator, (Context, unrotateSign(Record[1]), |
| 663 | getMDString(Record[2]))), |
| 664 | NextMetadataNo++); |
| 665 | break; |
| 666 | } |
| 667 | case bitc::METADATA_BASIC_TYPE: { |
| 668 | if (Record.size() != 6) |
| 669 | return error("Invalid record"); |
| 670 | |
| 671 | IsDistinct = Record[0]; |
| 672 | MetadataList.assignValue( |
| 673 | GET_OR_DISTINCT(DIBasicType, |
| 674 | (Context, Record[1], getMDString(Record[2]), |
| 675 | Record[3], Record[4], Record[5])), |
| 676 | NextMetadataNo++); |
| 677 | break; |
| 678 | } |
| 679 | case bitc::METADATA_DERIVED_TYPE: { |
| 680 | if (Record.size() != 12) |
| 681 | return error("Invalid record"); |
| 682 | |
| 683 | IsDistinct = Record[0]; |
| 684 | DINode::DIFlags Flags = static_cast<DINode::DIFlags>(Record[10]); |
| 685 | MetadataList.assignValue( |
| 686 | GET_OR_DISTINCT(DIDerivedType, |
| 687 | (Context, Record[1], getMDString(Record[2]), |
| 688 | getMDOrNull(Record[3]), Record[4], |
| 689 | getDITypeRefOrNull(Record[5]), |
| 690 | getDITypeRefOrNull(Record[6]), Record[7], Record[8], |
| 691 | Record[9], Flags, getDITypeRefOrNull(Record[11]))), |
| 692 | NextMetadataNo++); |
| 693 | break; |
| 694 | } |
| 695 | case bitc::METADATA_COMPOSITE_TYPE: { |
| 696 | if (Record.size() != 16) |
| 697 | return error("Invalid record"); |
| 698 | |
| 699 | // If we have a UUID and this is not a forward declaration, lookup the |
| 700 | // mapping. |
| 701 | IsDistinct = Record[0] & 0x1; |
| 702 | bool IsNotUsedInTypeRef = Record[0] >= 2; |
| 703 | unsigned Tag = Record[1]; |
| 704 | MDString *Name = getMDString(Record[2]); |
| 705 | Metadata *File = getMDOrNull(Record[3]); |
| 706 | unsigned Line = Record[4]; |
| 707 | Metadata *Scope = getDITypeRefOrNull(Record[5]); |
| 708 | Metadata *BaseType = getDITypeRefOrNull(Record[6]); |
| 709 | uint64_t SizeInBits = Record[7]; |
| 710 | if (Record[8] > (uint64_t)std::numeric_limits<uint32_t>::max()) |
| 711 | return error("Alignment value is too large"); |
| 712 | uint32_t AlignInBits = Record[8]; |
| 713 | uint64_t OffsetInBits = Record[9]; |
| 714 | DINode::DIFlags Flags = static_cast<DINode::DIFlags>(Record[10]); |
| 715 | Metadata *Elements = getMDOrNull(Record[11]); |
| 716 | unsigned RuntimeLang = Record[12]; |
| 717 | Metadata *VTableHolder = getDITypeRefOrNull(Record[13]); |
| 718 | Metadata *TemplateParams = getMDOrNull(Record[14]); |
| 719 | auto *Identifier = getMDString(Record[15]); |
| 720 | DICompositeType *CT = nullptr; |
| 721 | if (Identifier) |
| 722 | CT = DICompositeType::buildODRType( |
| 723 | Context, *Identifier, Tag, Name, File, Line, Scope, BaseType, |
| 724 | SizeInBits, AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang, |
| 725 | VTableHolder, TemplateParams); |
| 726 | |
| 727 | // Create a node if we didn't get a lazy ODR type. |
| 728 | if (!CT) |
| 729 | CT = GET_OR_DISTINCT(DICompositeType, |
| 730 | (Context, Tag, Name, File, Line, Scope, BaseType, |
| 731 | SizeInBits, AlignInBits, OffsetInBits, Flags, |
| 732 | Elements, RuntimeLang, VTableHolder, |
| 733 | TemplateParams, Identifier)); |
| 734 | if (!IsNotUsedInTypeRef && Identifier) |
| 735 | MetadataList.addTypeRef(*Identifier, *cast<DICompositeType>(CT)); |
| 736 | |
| 737 | MetadataList.assignValue(CT, NextMetadataNo++); |
| 738 | break; |
| 739 | } |
| 740 | case bitc::METADATA_SUBROUTINE_TYPE: { |
| 741 | if (Record.size() < 3 || Record.size() > 4) |
| 742 | return error("Invalid record"); |
| 743 | bool IsOldTypeRefArray = Record[0] < 2; |
| 744 | unsigned CC = (Record.size() > 3) ? Record[3] : 0; |
| 745 | |
| 746 | IsDistinct = Record[0] & 0x1; |
| 747 | DINode::DIFlags Flags = static_cast<DINode::DIFlags>(Record[1]); |
| 748 | Metadata *Types = getMDOrNull(Record[2]); |
| 749 | if (LLVM_UNLIKELY(IsOldTypeRefArray)) |
| 750 | Types = MetadataList.upgradeTypeRefArray(Types); |
| 751 | |
| 752 | MetadataList.assignValue( |
| 753 | GET_OR_DISTINCT(DISubroutineType, (Context, Flags, CC, Types)), |
| 754 | NextMetadataNo++); |
| 755 | break; |
| 756 | } |
| 757 | |
| 758 | case bitc::METADATA_MODULE: { |
| 759 | if (Record.size() != 6) |
| 760 | return error("Invalid record"); |
| 761 | |
| 762 | IsDistinct = Record[0]; |
| 763 | MetadataList.assignValue( |
| 764 | GET_OR_DISTINCT(DIModule, |
| 765 | (Context, getMDOrNull(Record[1]), |
| 766 | getMDString(Record[2]), getMDString(Record[3]), |
| 767 | getMDString(Record[4]), getMDString(Record[5]))), |
| 768 | NextMetadataNo++); |
| 769 | break; |
| 770 | } |
| 771 | |
| 772 | case bitc::METADATA_FILE: { |
| 773 | if (Record.size() != 3) |
| 774 | return error("Invalid record"); |
| 775 | |
| 776 | IsDistinct = Record[0]; |
| 777 | MetadataList.assignValue( |
| 778 | GET_OR_DISTINCT(DIFile, (Context, getMDString(Record[1]), |
| 779 | getMDString(Record[2]))), |
| 780 | NextMetadataNo++); |
| 781 | break; |
| 782 | } |
| 783 | case bitc::METADATA_COMPILE_UNIT: { |
| 784 | if (Record.size() < 14 || Record.size() > 17) |
| 785 | return error("Invalid record"); |
| 786 | |
| 787 | // Ignore Record[0], which indicates whether this compile unit is |
| 788 | // distinct. It's always distinct. |
| 789 | IsDistinct = true; |
| 790 | auto *CU = DICompileUnit::getDistinct( |
| 791 | Context, Record[1], getMDOrNull(Record[2]), getMDString(Record[3]), |
| 792 | Record[4], getMDString(Record[5]), Record[6], getMDString(Record[7]), |
| 793 | Record[8], getMDOrNull(Record[9]), getMDOrNull(Record[10]), |
| 794 | getMDOrNull(Record[12]), getMDOrNull(Record[13]), |
| 795 | Record.size() <= 15 ? nullptr : getMDOrNull(Record[15]), |
| 796 | Record.size() <= 14 ? 0 : Record[14], |
| 797 | Record.size() <= 16 ? true : Record[16]); |
| 798 | |
| 799 | MetadataList.assignValue(CU, NextMetadataNo++); |
| 800 | |
| 801 | // Move the Upgrade the list of subprograms. |
| 802 | if (Metadata *SPs = getMDOrNullWithoutPlaceholders(Record[11])) |
| 803 | CUSubprograms.push_back({CU, SPs}); |
| 804 | break; |
| 805 | } |
| 806 | case bitc::METADATA_SUBPROGRAM: { |
| 807 | if (Record.size() < 18 || Record.size() > 20) |
| 808 | return error("Invalid record"); |
| 809 | |
| 810 | IsDistinct = |
| 811 | (Record[0] & 1) || Record[8]; // All definitions should be distinct. |
| 812 | // Version 1 has a Function as Record[15]. |
| 813 | // Version 2 has removed Record[15]. |
| 814 | // Version 3 has the Unit as Record[15]. |
| 815 | // Version 4 added thisAdjustment. |
| 816 | bool HasUnit = Record[0] >= 2; |
| 817 | if (HasUnit && Record.size() < 19) |
| 818 | return error("Invalid record"); |
| 819 | Metadata *CUorFn = getMDOrNull(Record[15]); |
| 820 | unsigned Offset = Record.size() >= 19 ? 1 : 0; |
| 821 | bool HasFn = Offset && !HasUnit; |
| 822 | bool HasThisAdj = Record.size() >= 20; |
| 823 | DISubprogram *SP = GET_OR_DISTINCT( |
| 824 | DISubprogram, (Context, |
| 825 | getDITypeRefOrNull(Record[1]), // scope |
| 826 | getMDString(Record[2]), // name |
| 827 | getMDString(Record[3]), // linkageName |
| 828 | getMDOrNull(Record[4]), // file |
| 829 | Record[5], // line |
| 830 | getMDOrNull(Record[6]), // type |
| 831 | Record[7], // isLocal |
| 832 | Record[8], // isDefinition |
| 833 | Record[9], // scopeLine |
| 834 | getDITypeRefOrNull(Record[10]), // containingType |
| 835 | Record[11], // virtuality |
| 836 | Record[12], // virtualIndex |
| 837 | HasThisAdj ? Record[19] : 0, // thisAdjustment |
| 838 | static_cast<DINode::DIFlags>(Record[13] // flags |
| 839 | ), |
| 840 | Record[14], // isOptimized |
| 841 | HasUnit ? CUorFn : nullptr, // unit |
| 842 | getMDOrNull(Record[15 + Offset]), // templateParams |
| 843 | getMDOrNull(Record[16 + Offset]), // declaration |
| 844 | getMDOrNull(Record[17 + Offset]) // variables |
| 845 | )); |
| 846 | MetadataList.assignValue(SP, NextMetadataNo++); |
| 847 | |
| 848 | // Upgrade sp->function mapping to function->sp mapping. |
| 849 | if (HasFn) { |
| 850 | if (auto *CMD = dyn_cast_or_null<ConstantAsMetadata>(CUorFn)) |
| 851 | if (auto *F = dyn_cast<Function>(CMD->getValue())) { |
| 852 | if (F->isMaterializable()) |
| 853 | // Defer until materialized; unmaterialized functions may not have |
| 854 | // metadata. |
| 855 | FunctionsWithSPs[F] = SP; |
| 856 | else if (!F->empty()) |
| 857 | F->setSubprogram(SP); |
| 858 | } |
| 859 | } |
| 860 | break; |
| 861 | } |
| 862 | case bitc::METADATA_LEXICAL_BLOCK: { |
| 863 | if (Record.size() != 5) |
| 864 | return error("Invalid record"); |
| 865 | |
| 866 | IsDistinct = Record[0]; |
| 867 | MetadataList.assignValue( |
| 868 | GET_OR_DISTINCT(DILexicalBlock, |
| 869 | (Context, getMDOrNull(Record[1]), |
| 870 | getMDOrNull(Record[2]), Record[3], Record[4])), |
| 871 | NextMetadataNo++); |
| 872 | break; |
| 873 | } |
| 874 | case bitc::METADATA_LEXICAL_BLOCK_FILE: { |
| 875 | if (Record.size() != 4) |
| 876 | return error("Invalid record"); |
| 877 | |
| 878 | IsDistinct = Record[0]; |
| 879 | MetadataList.assignValue( |
| 880 | GET_OR_DISTINCT(DILexicalBlockFile, |
| 881 | (Context, getMDOrNull(Record[1]), |
| 882 | getMDOrNull(Record[2]), Record[3])), |
| 883 | NextMetadataNo++); |
| 884 | break; |
| 885 | } |
| 886 | case bitc::METADATA_NAMESPACE: { |
| 887 | if (Record.size() != 5) |
| 888 | return error("Invalid record"); |
| 889 | |
| 890 | IsDistinct = Record[0] & 1; |
| 891 | bool ExportSymbols = Record[0] & 2; |
| 892 | MetadataList.assignValue( |
| 893 | GET_OR_DISTINCT(DINamespace, |
| 894 | (Context, getMDOrNull(Record[1]), |
| 895 | getMDOrNull(Record[2]), getMDString(Record[3]), |
| 896 | Record[4], ExportSymbols)), |
| 897 | NextMetadataNo++); |
| 898 | break; |
| 899 | } |
| 900 | case bitc::METADATA_MACRO: { |
| 901 | if (Record.size() != 5) |
| 902 | return error("Invalid record"); |
| 903 | |
| 904 | IsDistinct = Record[0]; |
| 905 | MetadataList.assignValue( |
| 906 | GET_OR_DISTINCT(DIMacro, |
| 907 | (Context, Record[1], Record[2], |
| 908 | getMDString(Record[3]), getMDString(Record[4]))), |
| 909 | NextMetadataNo++); |
| 910 | break; |
| 911 | } |
| 912 | case bitc::METADATA_MACRO_FILE: { |
| 913 | if (Record.size() != 5) |
| 914 | return error("Invalid record"); |
| 915 | |
| 916 | IsDistinct = Record[0]; |
| 917 | MetadataList.assignValue( |
| 918 | GET_OR_DISTINCT(DIMacroFile, |
| 919 | (Context, Record[1], Record[2], |
| 920 | getMDOrNull(Record[3]), getMDOrNull(Record[4]))), |
| 921 | NextMetadataNo++); |
| 922 | break; |
| 923 | } |
| 924 | case bitc::METADATA_TEMPLATE_TYPE: { |
| 925 | if (Record.size() != 3) |
| 926 | return error("Invalid record"); |
| 927 | |
| 928 | IsDistinct = Record[0]; |
| 929 | MetadataList.assignValue(GET_OR_DISTINCT(DITemplateTypeParameter, |
| 930 | (Context, getMDString(Record[1]), |
| 931 | getDITypeRefOrNull(Record[2]))), |
| 932 | NextMetadataNo++); |
| 933 | break; |
| 934 | } |
| 935 | case bitc::METADATA_TEMPLATE_VALUE: { |
| 936 | if (Record.size() != 5) |
| 937 | return error("Invalid record"); |
| 938 | |
| 939 | IsDistinct = Record[0]; |
| 940 | MetadataList.assignValue( |
| 941 | GET_OR_DISTINCT(DITemplateValueParameter, |
| 942 | (Context, Record[1], getMDString(Record[2]), |
| 943 | getDITypeRefOrNull(Record[3]), |
| 944 | getMDOrNull(Record[4]))), |
| 945 | NextMetadataNo++); |
| 946 | break; |
| 947 | } |
| 948 | case bitc::METADATA_GLOBAL_VAR: { |
| 949 | if (Record.size() < 11 || Record.size() > 12) |
| 950 | return error("Invalid record"); |
| 951 | |
| 952 | IsDistinct = Record[0]; |
| 953 | |
| 954 | // Upgrade old metadata, which stored a global variable reference or a |
| 955 | // ConstantInt here. |
| 956 | Metadata *Expr = getMDOrNull(Record[9]); |
| 957 | uint32_t AlignInBits = 0; |
| 958 | if (Record.size() > 11) { |
| 959 | if (Record[11] > (uint64_t)std::numeric_limits<uint32_t>::max()) |
| 960 | return error("Alignment value is too large"); |
| 961 | AlignInBits = Record[11]; |
| 962 | } |
| 963 | GlobalVariable *Attach = nullptr; |
| 964 | if (auto *CMD = dyn_cast_or_null<ConstantAsMetadata>(Expr)) { |
| 965 | if (auto *GV = dyn_cast<GlobalVariable>(CMD->getValue())) { |
| 966 | Attach = GV; |
| 967 | Expr = nullptr; |
| 968 | } else if (auto *CI = dyn_cast<ConstantInt>(CMD->getValue())) { |
| 969 | Expr = DIExpression::get(Context, |
| 970 | {dwarf::DW_OP_constu, CI->getZExtValue(), |
| 971 | dwarf::DW_OP_stack_value}); |
| 972 | } else { |
| 973 | Expr = nullptr; |
| 974 | } |
| 975 | } |
| 976 | |
| 977 | DIGlobalVariable *DGV = GET_OR_DISTINCT( |
| 978 | DIGlobalVariable, |
| 979 | (Context, getMDOrNull(Record[1]), getMDString(Record[2]), |
| 980 | getMDString(Record[3]), getMDOrNull(Record[4]), Record[5], |
Adrian Prantl | 74a835c | 2016-12-16 04:25:54 +0000 | [diff] [blame] | 981 | getDITypeRefOrNull(Record[6]), Record[7], Record[8], |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 982 | getMDOrNull(Record[10]), AlignInBits)); |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 983 | |
Adrian Prantl | 74a835c | 2016-12-16 04:25:54 +0000 | [diff] [blame] | 984 | if (Expr || Attach) { |
| 985 | auto *DGVE = DIGlobalVariableExpression::getDistinct(Context, DGV, Expr); |
| 986 | MetadataList.assignValue(DGVE, NextMetadataNo++); |
| 987 | if (Attach) |
| 988 | Attach->addDebugInfo(DGVE); |
| 989 | } else |
| 990 | MetadataList.assignValue(DGV, NextMetadataNo++); |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 991 | |
| 992 | break; |
| 993 | } |
| 994 | case bitc::METADATA_LOCAL_VAR: { |
| 995 | // 10th field is for the obseleted 'inlinedAt:' field. |
| 996 | if (Record.size() < 8 || Record.size() > 10) |
| 997 | return error("Invalid record"); |
| 998 | |
| 999 | IsDistinct = Record[0] & 1; |
| 1000 | bool HasAlignment = Record[0] & 2; |
| 1001 | // 2nd field used to be an artificial tag, either DW_TAG_auto_variable or |
| 1002 | // DW_TAG_arg_variable, if we have alignment flag encoded it means, that |
| 1003 | // this is newer version of record which doesn't have artifical tag. |
| 1004 | bool HasTag = !HasAlignment && Record.size() > 8; |
| 1005 | DINode::DIFlags Flags = static_cast<DINode::DIFlags>(Record[7 + HasTag]); |
| 1006 | uint32_t AlignInBits = 0; |
| 1007 | if (HasAlignment) { |
| 1008 | if (Record[8 + HasTag] > (uint64_t)std::numeric_limits<uint32_t>::max()) |
| 1009 | return error("Alignment value is too large"); |
| 1010 | AlignInBits = Record[8 + HasTag]; |
| 1011 | } |
| 1012 | MetadataList.assignValue( |
| 1013 | GET_OR_DISTINCT(DILocalVariable, |
| 1014 | (Context, getMDOrNull(Record[1 + HasTag]), |
| 1015 | getMDString(Record[2 + HasTag]), |
| 1016 | getMDOrNull(Record[3 + HasTag]), Record[4 + HasTag], |
| 1017 | getDITypeRefOrNull(Record[5 + HasTag]), |
| 1018 | Record[6 + HasTag], Flags, AlignInBits)), |
| 1019 | NextMetadataNo++); |
| 1020 | break; |
| 1021 | } |
| 1022 | case bitc::METADATA_EXPRESSION: { |
| 1023 | if (Record.size() < 1) |
| 1024 | return error("Invalid record"); |
| 1025 | |
| 1026 | IsDistinct = Record[0] & 1; |
| 1027 | bool HasOpFragment = Record[0] & 2; |
| 1028 | auto Elts = MutableArrayRef<uint64_t>(Record).slice(1); |
| 1029 | if (!HasOpFragment) |
| 1030 | if (unsigned N = Elts.size()) |
| 1031 | if (N >= 3 && Elts[N - 3] == dwarf::DW_OP_bit_piece) |
| 1032 | Elts[N - 3] = dwarf::DW_OP_LLVM_fragment; |
| 1033 | |
| 1034 | MetadataList.assignValue( |
| 1035 | GET_OR_DISTINCT(DIExpression, |
| 1036 | (Context, makeArrayRef(Record).slice(1))), |
| 1037 | NextMetadataNo++); |
| 1038 | break; |
| 1039 | } |
Adrian Prantl | 74a835c | 2016-12-16 04:25:54 +0000 | [diff] [blame] | 1040 | case bitc::METADATA_GLOBAL_VAR_EXPR: { |
| 1041 | if (Record.size() != 3) |
| 1042 | return error("Invalid record"); |
| 1043 | |
| 1044 | IsDistinct = Record[0]; |
| 1045 | MetadataList.assignValue(GET_OR_DISTINCT(DIGlobalVariableExpression, |
| 1046 | (Context, getMDOrNull(Record[1]), |
| 1047 | getMDOrNull(Record[2]))), |
| 1048 | NextMetadataNo++); |
| 1049 | break; |
| 1050 | } |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 1051 | case bitc::METADATA_OBJC_PROPERTY: { |
| 1052 | if (Record.size() != 8) |
| 1053 | return error("Invalid record"); |
| 1054 | |
| 1055 | IsDistinct = Record[0]; |
| 1056 | MetadataList.assignValue( |
| 1057 | GET_OR_DISTINCT(DIObjCProperty, |
| 1058 | (Context, getMDString(Record[1]), |
| 1059 | getMDOrNull(Record[2]), Record[3], |
| 1060 | getMDString(Record[4]), getMDString(Record[5]), |
| 1061 | Record[6], getDITypeRefOrNull(Record[7]))), |
| 1062 | NextMetadataNo++); |
| 1063 | break; |
| 1064 | } |
| 1065 | case bitc::METADATA_IMPORTED_ENTITY: { |
| 1066 | if (Record.size() != 6) |
| 1067 | return error("Invalid record"); |
| 1068 | |
| 1069 | IsDistinct = Record[0]; |
| 1070 | MetadataList.assignValue( |
| 1071 | GET_OR_DISTINCT(DIImportedEntity, |
| 1072 | (Context, Record[1], getMDOrNull(Record[2]), |
| 1073 | getDITypeRefOrNull(Record[3]), Record[4], |
| 1074 | getMDString(Record[5]))), |
| 1075 | NextMetadataNo++); |
| 1076 | break; |
| 1077 | } |
| 1078 | case bitc::METADATA_STRING_OLD: { |
| 1079 | std::string String(Record.begin(), Record.end()); |
| 1080 | |
| 1081 | // Test for upgrading !llvm.loop. |
| 1082 | HasSeenOldLoopTags |= mayBeOldLoopAttachmentTag(String); |
| 1083 | |
| 1084 | Metadata *MD = MDString::get(Context, String); |
| 1085 | MetadataList.assignValue(MD, NextMetadataNo++); |
| 1086 | break; |
| 1087 | } |
| 1088 | case bitc::METADATA_STRINGS: |
| 1089 | if (Error Err = parseMetadataStrings(Record, Blob, NextMetadataNo)) |
| 1090 | return Err; |
| 1091 | break; |
| 1092 | case bitc::METADATA_GLOBAL_DECL_ATTACHMENT: { |
| 1093 | if (Record.size() % 2 == 0) |
| 1094 | return error("Invalid record"); |
| 1095 | unsigned ValueID = Record[0]; |
| 1096 | if (ValueID >= ValueList.size()) |
| 1097 | return error("Invalid record"); |
| 1098 | if (auto *GO = dyn_cast<GlobalObject>(ValueList[ValueID])) |
| 1099 | if (Error Err = parseGlobalObjectAttachment( |
| 1100 | *GO, ArrayRef<uint64_t>(Record).slice(1))) |
| 1101 | return Err; |
| 1102 | break; |
| 1103 | } |
| 1104 | case bitc::METADATA_KIND: { |
| 1105 | // Support older bitcode files that had METADATA_KIND records in a |
| 1106 | // block with METADATA_BLOCK_ID. |
| 1107 | if (Error Err = parseMetadataKindRecord(Record)) |
| 1108 | return Err; |
| 1109 | break; |
| 1110 | } |
| 1111 | } |
| 1112 | } |
| 1113 | #undef GET_OR_DISTINCT |
| 1114 | } |
| 1115 | |
| 1116 | Error MetadataLoader::MetadataLoaderImpl::parseMetadataStrings( |
| 1117 | ArrayRef<uint64_t> Record, StringRef Blob, unsigned &NextMetadataNo) { |
| 1118 | // All the MDStrings in the block are emitted together in a single |
| 1119 | // record. The strings are concatenated and stored in a blob along with |
| 1120 | // their sizes. |
| 1121 | if (Record.size() != 2) |
| 1122 | return error("Invalid record: metadata strings layout"); |
| 1123 | |
| 1124 | unsigned NumStrings = Record[0]; |
| 1125 | unsigned StringsOffset = Record[1]; |
| 1126 | if (!NumStrings) |
| 1127 | return error("Invalid record: metadata strings with no strings"); |
| 1128 | if (StringsOffset > Blob.size()) |
| 1129 | return error("Invalid record: metadata strings corrupt offset"); |
| 1130 | |
| 1131 | StringRef Lengths = Blob.slice(0, StringsOffset); |
| 1132 | SimpleBitstreamCursor R(Lengths); |
| 1133 | |
| 1134 | StringRef Strings = Blob.drop_front(StringsOffset); |
| 1135 | do { |
| 1136 | if (R.AtEndOfStream()) |
| 1137 | return error("Invalid record: metadata strings bad length"); |
| 1138 | |
| 1139 | unsigned Size = R.ReadVBR(6); |
| 1140 | if (Strings.size() < Size) |
| 1141 | return error("Invalid record: metadata strings truncated chars"); |
| 1142 | |
| 1143 | MetadataList.assignValue(MDString::get(Context, Strings.slice(0, Size)), |
| 1144 | NextMetadataNo++); |
| 1145 | Strings = Strings.drop_front(Size); |
| 1146 | } while (--NumStrings); |
| 1147 | |
| 1148 | return Error::success(); |
| 1149 | } |
| 1150 | |
| 1151 | Error MetadataLoader::MetadataLoaderImpl::parseGlobalObjectAttachment( |
| 1152 | GlobalObject &GO, ArrayRef<uint64_t> Record) { |
| 1153 | assert(Record.size() % 2 == 0); |
| 1154 | for (unsigned I = 0, E = Record.size(); I != E; I += 2) { |
| 1155 | auto K = MDKindMap.find(Record[I]); |
| 1156 | if (K == MDKindMap.end()) |
| 1157 | return error("Invalid ID"); |
| 1158 | MDNode *MD = MetadataList.getMDNodeFwdRefOrNull(Record[I + 1]); |
| 1159 | if (!MD) |
| 1160 | return error("Invalid metadata attachment"); |
| 1161 | GO.addMetadata(K->second, *MD); |
| 1162 | } |
| 1163 | return Error::success(); |
| 1164 | } |
| 1165 | |
| 1166 | /// Parse metadata attachments. |
| 1167 | Error MetadataLoader::MetadataLoaderImpl::parseMetadataAttachment( |
| 1168 | Function &F, const SmallVectorImpl<Instruction *> &InstructionList) { |
| 1169 | if (Stream.EnterSubBlock(bitc::METADATA_ATTACHMENT_ID)) |
| 1170 | return error("Invalid record"); |
| 1171 | |
| 1172 | SmallVector<uint64_t, 64> Record; |
| 1173 | |
| 1174 | while (true) { |
| 1175 | BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); |
| 1176 | |
| 1177 | switch (Entry.Kind) { |
| 1178 | case BitstreamEntry::SubBlock: // Handled for us already. |
| 1179 | case BitstreamEntry::Error: |
| 1180 | return error("Malformed block"); |
| 1181 | case BitstreamEntry::EndBlock: |
| 1182 | return Error::success(); |
| 1183 | case BitstreamEntry::Record: |
| 1184 | // The interesting case. |
| 1185 | break; |
| 1186 | } |
| 1187 | |
| 1188 | // Read a metadata attachment record. |
| 1189 | Record.clear(); |
| 1190 | switch (Stream.readRecord(Entry.ID, Record)) { |
| 1191 | default: // Default behavior: ignore. |
| 1192 | break; |
| 1193 | case bitc::METADATA_ATTACHMENT: { |
| 1194 | unsigned RecordLength = Record.size(); |
| 1195 | if (Record.empty()) |
| 1196 | return error("Invalid record"); |
| 1197 | if (RecordLength % 2 == 0) { |
| 1198 | // A function attachment. |
| 1199 | if (Error Err = parseGlobalObjectAttachment(F, Record)) |
| 1200 | return Err; |
| 1201 | continue; |
| 1202 | } |
| 1203 | |
| 1204 | // An instruction attachment. |
| 1205 | Instruction *Inst = InstructionList[Record[0]]; |
| 1206 | for (unsigned i = 1; i != RecordLength; i = i + 2) { |
| 1207 | unsigned Kind = Record[i]; |
| 1208 | DenseMap<unsigned, unsigned>::iterator I = MDKindMap.find(Kind); |
| 1209 | if (I == MDKindMap.end()) |
| 1210 | return error("Invalid ID"); |
| 1211 | Metadata *Node = MetadataList.getMetadataFwdRef(Record[i + 1]); |
| 1212 | if (isa<LocalAsMetadata>(Node)) |
| 1213 | // Drop the attachment. This used to be legal, but there's no |
| 1214 | // upgrade path. |
| 1215 | break; |
| 1216 | MDNode *MD = dyn_cast_or_null<MDNode>(Node); |
| 1217 | if (!MD) |
| 1218 | return error("Invalid metadata attachment"); |
| 1219 | |
| 1220 | if (HasSeenOldLoopTags && I->second == LLVMContext::MD_loop) |
| 1221 | MD = upgradeInstructionLoopAttachment(*MD); |
| 1222 | |
| 1223 | if (I->second == LLVMContext::MD_tbaa) { |
| 1224 | assert(!MD->isTemporary() && "should load MDs before attachments"); |
| 1225 | MD = UpgradeTBAANode(*MD); |
| 1226 | } |
| 1227 | Inst->setMetadata(I->second, MD); |
| 1228 | } |
| 1229 | break; |
| 1230 | } |
| 1231 | } |
| 1232 | } |
| 1233 | } |
| 1234 | |
| 1235 | /// Parse a single METADATA_KIND record, inserting result in MDKindMap. |
| 1236 | Error MetadataLoader::MetadataLoaderImpl::parseMetadataKindRecord( |
| 1237 | SmallVectorImpl<uint64_t> &Record) { |
| 1238 | if (Record.size() < 2) |
| 1239 | return error("Invalid record"); |
| 1240 | |
| 1241 | unsigned Kind = Record[0]; |
| 1242 | SmallString<8> Name(Record.begin() + 1, Record.end()); |
| 1243 | |
| 1244 | unsigned NewKind = TheModule.getMDKindID(Name.str()); |
| 1245 | if (!MDKindMap.insert(std::make_pair(Kind, NewKind)).second) |
| 1246 | return error("Conflicting METADATA_KIND records"); |
| 1247 | return Error::success(); |
| 1248 | } |
| 1249 | |
| 1250 | /// Parse the metadata kinds out of the METADATA_KIND_BLOCK. |
| 1251 | Error MetadataLoader::MetadataLoaderImpl::parseMetadataKinds() { |
| 1252 | if (Stream.EnterSubBlock(bitc::METADATA_KIND_BLOCK_ID)) |
| 1253 | return error("Invalid record"); |
| 1254 | |
| 1255 | SmallVector<uint64_t, 64> Record; |
| 1256 | |
| 1257 | // Read all the records. |
| 1258 | while (true) { |
| 1259 | BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); |
| 1260 | |
| 1261 | switch (Entry.Kind) { |
| 1262 | case BitstreamEntry::SubBlock: // Handled for us already. |
| 1263 | case BitstreamEntry::Error: |
| 1264 | return error("Malformed block"); |
| 1265 | case BitstreamEntry::EndBlock: |
| 1266 | return Error::success(); |
| 1267 | case BitstreamEntry::Record: |
| 1268 | // The interesting case. |
| 1269 | break; |
| 1270 | } |
| 1271 | |
| 1272 | // Read a record. |
| 1273 | Record.clear(); |
| 1274 | unsigned Code = Stream.readRecord(Entry.ID, Record); |
| 1275 | switch (Code) { |
| 1276 | default: // Default behavior: ignore. |
| 1277 | break; |
| 1278 | case bitc::METADATA_KIND: { |
| 1279 | if (Error Err = parseMetadataKindRecord(Record)) |
| 1280 | return Err; |
| 1281 | break; |
| 1282 | } |
| 1283 | } |
| 1284 | } |
| 1285 | } |
| 1286 | |
| 1287 | MetadataLoader &MetadataLoader::operator=(MetadataLoader &&RHS) { |
| 1288 | Pimpl = std::move(RHS.Pimpl); |
| 1289 | return *this; |
| 1290 | } |
| 1291 | MetadataLoader::MetadataLoader(MetadataLoader &&RHS) |
| 1292 | : Pimpl(std::move(RHS.Pimpl)) {} |
| 1293 | |
| 1294 | MetadataLoader::~MetadataLoader() = default; |
| 1295 | MetadataLoader::MetadataLoader(BitstreamCursor &Stream, Module &TheModule, |
| 1296 | BitcodeReaderValueList &ValueList, |
| 1297 | std::function<Type *(unsigned)> getTypeByID) |
Nico Weber | b3901bd | 2016-12-12 22:46:40 +0000 | [diff] [blame] | 1298 | : Pimpl(llvm::make_unique<MetadataLoaderImpl>(Stream, TheModule, ValueList, |
| 1299 | getTypeByID)) {} |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 1300 | |
| 1301 | Error MetadataLoader::parseMetadata(bool ModuleLevel) { |
| 1302 | return Pimpl->parseMetadata(ModuleLevel); |
| 1303 | } |
| 1304 | |
| 1305 | bool MetadataLoader::hasFwdRefs() const { return Pimpl->hasFwdRefs(); } |
| 1306 | |
| 1307 | /// Return the given metadata, creating a replaceable forward reference if |
| 1308 | /// necessary. |
| 1309 | Metadata *MetadataLoader::getMetadataFwdRef(unsigned Idx) { |
| 1310 | return Pimpl->getMetadataFwdRef(Idx); |
| 1311 | } |
| 1312 | |
| 1313 | MDNode *MetadataLoader::getMDNodeFwdRefOrNull(unsigned Idx) { |
| 1314 | return Pimpl->getMDNodeFwdRefOrNull(Idx); |
| 1315 | } |
| 1316 | |
| 1317 | DISubprogram *MetadataLoader::lookupSubprogramForFunction(Function *F) { |
| 1318 | return Pimpl->lookupSubprogramForFunction(F); |
| 1319 | } |
| 1320 | |
| 1321 | Error MetadataLoader::parseMetadataAttachment( |
| 1322 | Function &F, const SmallVectorImpl<Instruction *> &InstructionList) { |
| 1323 | return Pimpl->parseMetadataAttachment(F, InstructionList); |
| 1324 | } |
| 1325 | |
| 1326 | Error MetadataLoader::parseMetadataKinds() { |
| 1327 | return Pimpl->parseMetadataKinds(); |
| 1328 | } |
| 1329 | |
| 1330 | unsigned MetadataLoader::size() const { return Pimpl->size(); } |
| 1331 | void MetadataLoader::shrinkTo(unsigned N) { return Pimpl->shrinkTo(N); } |