10b57cec5SDimitry Andric //===-- Bitcode/Reader/MetadataLoader.h - Load Metadatas -------*- C++ -*-====// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric // 90b57cec5SDimitry Andric // This class handles loading Metadatas. 100b57cec5SDimitry Andric // 110b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 120b57cec5SDimitry Andric 130b57cec5SDimitry Andric #ifndef LLVM_LIB_BITCODE_READER_METADATALOADER_H 140b57cec5SDimitry Andric #define LLVM_LIB_BITCODE_READER_METADATALOADER_H 150b57cec5SDimitry Andric 160b57cec5SDimitry Andric #include "llvm/Support/Error.h" 170b57cec5SDimitry Andric 180b57cec5SDimitry Andric #include <functional> 190b57cec5SDimitry Andric #include <memory> 200b57cec5SDimitry Andric 210b57cec5SDimitry Andric namespace llvm { 22*5f757f3fSDimitry Andric class BasicBlock; 230b57cec5SDimitry Andric class BitcodeReaderValueList; 240b57cec5SDimitry Andric class BitstreamCursor; 250b57cec5SDimitry Andric class DISubprogram; 260b57cec5SDimitry Andric class Function; 270b57cec5SDimitry Andric class Instruction; 280b57cec5SDimitry Andric class Metadata; 290b57cec5SDimitry Andric class Module; 300b57cec5SDimitry Andric class Type; 3181ad6265SDimitry Andric template <typename T> class ArrayRef; 320b57cec5SDimitry Andric 33bdd1243dSDimitry Andric typedef std::function<Type *(unsigned)> GetTypeByIDTy; 34bdd1243dSDimitry Andric 35bdd1243dSDimitry Andric typedef std::function<unsigned(unsigned, unsigned)> GetContainedTypeIDTy; 36bdd1243dSDimitry Andric 37bdd1243dSDimitry Andric typedef std::function<void(Metadata **, unsigned, GetTypeByIDTy, 38bdd1243dSDimitry Andric GetContainedTypeIDTy)> 39bdd1243dSDimitry Andric MDTypeCallbackTy; 40bdd1243dSDimitry Andric 41bdd1243dSDimitry Andric struct MetadataLoaderCallbacks { 42bdd1243dSDimitry Andric GetTypeByIDTy GetTypeByID; 43bdd1243dSDimitry Andric GetContainedTypeIDTy GetContainedTypeID; 44bdd1243dSDimitry Andric std::optional<MDTypeCallbackTy> MDType; 45bdd1243dSDimitry Andric }; 46bdd1243dSDimitry Andric 470b57cec5SDimitry Andric /// Helper class that handles loading Metadatas and keeping them available. 480b57cec5SDimitry Andric class MetadataLoader { 490b57cec5SDimitry Andric class MetadataLoaderImpl; 500b57cec5SDimitry Andric std::unique_ptr<MetadataLoaderImpl> Pimpl; 510b57cec5SDimitry Andric Error parseMetadata(bool ModuleLevel); 520b57cec5SDimitry Andric 530b57cec5SDimitry Andric public: 540b57cec5SDimitry Andric ~MetadataLoader(); 550b57cec5SDimitry Andric MetadataLoader(BitstreamCursor &Stream, Module &TheModule, 560b57cec5SDimitry Andric BitcodeReaderValueList &ValueList, bool IsImporting, 57bdd1243dSDimitry Andric MetadataLoaderCallbacks Callbacks); 580b57cec5SDimitry Andric MetadataLoader &operator=(MetadataLoader &&); 590b57cec5SDimitry Andric MetadataLoader(MetadataLoader &&); 600b57cec5SDimitry Andric 610b57cec5SDimitry Andric // Parse a module metadata block 620b57cec5SDimitry Andric Error parseModuleMetadata() { return parseMetadata(true); } 630b57cec5SDimitry Andric 640b57cec5SDimitry Andric // Parse a function metadata block 650b57cec5SDimitry Andric Error parseFunctionMetadata() { return parseMetadata(false); } 660b57cec5SDimitry Andric 670b57cec5SDimitry Andric /// Set the mode to strip TBAA metadata on load. 680b57cec5SDimitry Andric void setStripTBAA(bool StripTBAA = true); 690b57cec5SDimitry Andric 700b57cec5SDimitry Andric /// Return true if the Loader is stripping TBAA metadata. 710b57cec5SDimitry Andric bool isStrippingTBAA(); 720b57cec5SDimitry Andric 730b57cec5SDimitry Andric // Return true there are remaining unresolved forward references. 740b57cec5SDimitry Andric bool hasFwdRefs() const; 750b57cec5SDimitry Andric 760b57cec5SDimitry Andric /// Return the given metadata, creating a replaceable forward reference if 770b57cec5SDimitry Andric /// necessary. 780b57cec5SDimitry Andric Metadata *getMetadataFwdRefOrLoad(unsigned Idx); 790b57cec5SDimitry Andric 800b57cec5SDimitry Andric /// Return the DISubprogram metadata for a Function if any, null otherwise. 810b57cec5SDimitry Andric DISubprogram *lookupSubprogramForFunction(Function *F); 820b57cec5SDimitry Andric 830b57cec5SDimitry Andric /// Parse a `METADATA_ATTACHMENT` block for a function. 8481ad6265SDimitry Andric Error parseMetadataAttachment(Function &F, 8581ad6265SDimitry Andric ArrayRef<Instruction *> InstructionList); 860b57cec5SDimitry Andric 870b57cec5SDimitry Andric /// Parse a `METADATA_KIND` block for the current module. 880b57cec5SDimitry Andric Error parseMetadataKinds(); 890b57cec5SDimitry Andric 900b57cec5SDimitry Andric unsigned size() const; 910b57cec5SDimitry Andric void shrinkTo(unsigned N); 920b57cec5SDimitry Andric 930b57cec5SDimitry Andric /// Perform bitcode upgrades on llvm.dbg.* calls. 940b57cec5SDimitry Andric void upgradeDebugIntrinsics(Function &F); 950b57cec5SDimitry Andric }; 960b57cec5SDimitry Andric } 970b57cec5SDimitry Andric 980b57cec5SDimitry Andric #endif // LLVM_LIB_BITCODE_READER_METADATALOADER_H 99