1b915e9e0SDimitry Andric //===-- Bitcode/Reader/MetadataLoader.h - Load Metadatas -------*- C++ -*-====// 2b915e9e0SDimitry Andric // 3e6d15924SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4e6d15924SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5e6d15924SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6b915e9e0SDimitry Andric // 7b915e9e0SDimitry Andric //===----------------------------------------------------------------------===// 8b915e9e0SDimitry Andric // 9b915e9e0SDimitry Andric // This class handles loading Metadatas. 10b915e9e0SDimitry Andric // 11b915e9e0SDimitry Andric //===----------------------------------------------------------------------===// 12b915e9e0SDimitry Andric 13b915e9e0SDimitry Andric #ifndef LLVM_LIB_BITCODE_READER_METADATALOADER_H 14b915e9e0SDimitry Andric #define LLVM_LIB_BITCODE_READER_METADATALOADER_H 15b915e9e0SDimitry Andric 16b915e9e0SDimitry Andric #include "llvm/Support/Error.h" 17b915e9e0SDimitry Andric 18b915e9e0SDimitry Andric #include <functional> 19b915e9e0SDimitry Andric #include <memory> 20b915e9e0SDimitry Andric 21b915e9e0SDimitry Andric namespace llvm { 22b1c73532SDimitry Andric class BasicBlock; 23b915e9e0SDimitry Andric class BitcodeReaderValueList; 24b915e9e0SDimitry Andric class BitstreamCursor; 25b915e9e0SDimitry Andric class DISubprogram; 26b915e9e0SDimitry Andric class Function; 27b915e9e0SDimitry Andric class Instruction; 28b915e9e0SDimitry Andric class Metadata; 29b915e9e0SDimitry Andric class Module; 30b915e9e0SDimitry Andric class Type; 31145449b1SDimitry Andric template <typename T> class ArrayRef; 32b915e9e0SDimitry Andric 33e3b55780SDimitry Andric typedef std::function<Type *(unsigned)> GetTypeByIDTy; 34e3b55780SDimitry Andric 35e3b55780SDimitry Andric typedef std::function<unsigned(unsigned, unsigned)> GetContainedTypeIDTy; 36e3b55780SDimitry Andric 37e3b55780SDimitry Andric typedef std::function<void(Metadata **, unsigned, GetTypeByIDTy, 38e3b55780SDimitry Andric GetContainedTypeIDTy)> 39e3b55780SDimitry Andric MDTypeCallbackTy; 40e3b55780SDimitry Andric 41e3b55780SDimitry Andric struct MetadataLoaderCallbacks { 42e3b55780SDimitry Andric GetTypeByIDTy GetTypeByID; 43e3b55780SDimitry Andric GetContainedTypeIDTy GetContainedTypeID; 44e3b55780SDimitry Andric std::optional<MDTypeCallbackTy> MDType; 45e3b55780SDimitry Andric }; 46e3b55780SDimitry Andric 47b915e9e0SDimitry Andric /// Helper class that handles loading Metadatas and keeping them available. 48b915e9e0SDimitry Andric class MetadataLoader { 49b915e9e0SDimitry Andric class MetadataLoaderImpl; 50b915e9e0SDimitry Andric std::unique_ptr<MetadataLoaderImpl> Pimpl; 51b915e9e0SDimitry Andric Error parseMetadata(bool ModuleLevel); 52b915e9e0SDimitry Andric 53b915e9e0SDimitry Andric public: 54b915e9e0SDimitry Andric ~MetadataLoader(); 55b915e9e0SDimitry Andric MetadataLoader(BitstreamCursor &Stream, Module &TheModule, 56b915e9e0SDimitry Andric BitcodeReaderValueList &ValueList, bool IsImporting, 57e3b55780SDimitry Andric MetadataLoaderCallbacks Callbacks); 58b915e9e0SDimitry Andric MetadataLoader &operator=(MetadataLoader &&); 59b915e9e0SDimitry Andric MetadataLoader(MetadataLoader &&); 60b915e9e0SDimitry Andric 61b915e9e0SDimitry Andric // Parse a module metadata block parseModuleMetadata()62b915e9e0SDimitry Andric Error parseModuleMetadata() { return parseMetadata(true); } 63b915e9e0SDimitry Andric 64b915e9e0SDimitry Andric // Parse a function metadata block parseFunctionMetadata()65b915e9e0SDimitry Andric Error parseFunctionMetadata() { return parseMetadata(false); } 66b915e9e0SDimitry Andric 67b915e9e0SDimitry Andric /// Set the mode to strip TBAA metadata on load. 68b915e9e0SDimitry Andric void setStripTBAA(bool StripTBAA = true); 69b915e9e0SDimitry Andric 70b915e9e0SDimitry Andric /// Return true if the Loader is stripping TBAA metadata. 71b915e9e0SDimitry Andric bool isStrippingTBAA(); 72b915e9e0SDimitry Andric 73b915e9e0SDimitry Andric // Return true there are remaining unresolved forward references. 74b915e9e0SDimitry Andric bool hasFwdRefs() const; 75b915e9e0SDimitry Andric 76b915e9e0SDimitry Andric /// Return the given metadata, creating a replaceable forward reference if 77b915e9e0SDimitry Andric /// necessary. 7802a33680SDimitry Andric Metadata *getMetadataFwdRefOrLoad(unsigned Idx); 79b915e9e0SDimitry Andric 80d8e91e46SDimitry Andric /// Return the DISubprogram metadata for a Function if any, null otherwise. 81b915e9e0SDimitry Andric DISubprogram *lookupSubprogramForFunction(Function *F); 82b915e9e0SDimitry Andric 83b915e9e0SDimitry Andric /// Parse a `METADATA_ATTACHMENT` block for a function. 84145449b1SDimitry Andric Error parseMetadataAttachment(Function &F, 85145449b1SDimitry Andric ArrayRef<Instruction *> InstructionList); 86b915e9e0SDimitry Andric 87b915e9e0SDimitry Andric /// Parse a `METADATA_KIND` block for the current module. 88b915e9e0SDimitry Andric Error parseMetadataKinds(); 89b915e9e0SDimitry Andric 90b915e9e0SDimitry Andric unsigned size() const; 91b915e9e0SDimitry Andric void shrinkTo(unsigned N); 92d99dafe2SDimitry Andric 93d99dafe2SDimitry Andric /// Perform bitcode upgrades on llvm.dbg.* calls. 94d99dafe2SDimitry Andric void upgradeDebugIntrinsics(Function &F); 95b915e9e0SDimitry Andric }; 96b915e9e0SDimitry Andric } 97b915e9e0SDimitry Andric 98b915e9e0SDimitry Andric #endif // LLVM_LIB_BITCODE_READER_METADATALOADER_H 99