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