MetadataLoader.h 3.0 KB

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