ModuleSlotTracker.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===-- llvm/IR/ModuleSlotTracker.h -----------------------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_IR_MODULESLOTTRACKER_H
  14. #define LLVM_IR_MODULESLOTTRACKER_H
  15. #include <functional>
  16. #include <memory>
  17. #include <utility>
  18. #include <vector>
  19. namespace llvm {
  20. class Module;
  21. class Function;
  22. class SlotTracker;
  23. class Value;
  24. class MDNode;
  25. /// Abstract interface of slot tracker storage.
  26. class AbstractSlotTrackerStorage {
  27. public:
  28. virtual ~AbstractSlotTrackerStorage();
  29. virtual unsigned getNextMetadataSlot() = 0;
  30. virtual void createMetadataSlot(const MDNode *) = 0;
  31. virtual int getMetadataSlot(const MDNode *) = 0;
  32. };
  33. /// Manage lifetime of a slot tracker for printing IR.
  34. ///
  35. /// Wrapper around the \a SlotTracker used internally by \a AsmWriter. This
  36. /// class allows callers to share the cost of incorporating the metadata in a
  37. /// module or a function.
  38. ///
  39. /// If the IR changes from underneath \a ModuleSlotTracker, strings like
  40. /// "<badref>" will be printed, or, worse, the wrong slots entirely.
  41. class ModuleSlotTracker {
  42. /// Storage for a slot tracker.
  43. std::unique_ptr<SlotTracker> MachineStorage;
  44. bool ShouldCreateStorage = false;
  45. bool ShouldInitializeAllMetadata = false;
  46. const Module *M = nullptr;
  47. const Function *F = nullptr;
  48. SlotTracker *Machine = nullptr;
  49. std::function<void(AbstractSlotTrackerStorage *, const Module *, bool)>
  50. ProcessModuleHookFn;
  51. std::function<void(AbstractSlotTrackerStorage *, const Function *, bool)>
  52. ProcessFunctionHookFn;
  53. public:
  54. /// Wrap a preinitialized SlotTracker.
  55. ModuleSlotTracker(SlotTracker &Machine, const Module *M,
  56. const Function *F = nullptr);
  57. /// Construct a slot tracker from a module.
  58. ///
  59. /// If \a M is \c nullptr, uses a null slot tracker. Otherwise, initializes
  60. /// a slot tracker, and initializes all metadata slots. \c
  61. /// ShouldInitializeAllMetadata defaults to true because this is expected to
  62. /// be shared between multiple callers, and otherwise MDNode references will
  63. /// not match up.
  64. explicit ModuleSlotTracker(const Module *M,
  65. bool ShouldInitializeAllMetadata = true);
  66. /// Destructor to clean up storage.
  67. virtual ~ModuleSlotTracker();
  68. /// Lazily creates a slot tracker.
  69. SlotTracker *getMachine();
  70. const Module *getModule() const { return M; }
  71. const Function *getCurrentFunction() const { return F; }
  72. /// Incorporate the given function.
  73. ///
  74. /// Purge the currently incorporated function and incorporate \c F. If \c F
  75. /// is currently incorporated, this is a no-op.
  76. void incorporateFunction(const Function &F);
  77. /// Return the slot number of the specified local value.
  78. ///
  79. /// A function that defines this value should be incorporated prior to calling
  80. /// this method.
  81. /// Return -1 if the value is not in the function's SlotTracker.
  82. int getLocalSlot(const Value *V);
  83. void setProcessHook(
  84. std::function<void(AbstractSlotTrackerStorage *, const Module *, bool)>);
  85. void setProcessHook(std::function<void(AbstractSlotTrackerStorage *,
  86. const Function *, bool)>);
  87. using MachineMDNodeListType =
  88. std::vector<std::pair<unsigned, const MDNode *>>;
  89. void collectMDNodes(MachineMDNodeListType &L, unsigned LB, unsigned UB) const;
  90. };
  91. } // end namespace llvm
  92. #endif
  93. #ifdef __GNUC__
  94. #pragma GCC diagnostic pop
  95. #endif