ModuleSlotTracker.h 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. //===-- llvm/IR/ModuleSlotTracker.h -----------------------------*- C++ -*-===//
  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. #ifndef LLVM_IR_MODULESLOTTRACKER_H
  10. #define LLVM_IR_MODULESLOTTRACKER_H
  11. #include <memory>
  12. namespace llvm {
  13. class Module;
  14. class Function;
  15. class SlotTracker;
  16. /// Manage lifetime of a slot tracker for printing IR.
  17. ///
  18. /// Wrapper around the \a SlotTracker used internally by \a AsmWriter. This
  19. /// class allows callers to share the cost of incorporating the metadata in a
  20. /// module or a function.
  21. ///
  22. /// If the IR changes from underneath \a ModuleSlotTracker, strings like
  23. /// "<badref>" will be printed, or, worse, the wrong slots entirely.
  24. class ModuleSlotTracker {
  25. /// Storage for a slot tracker.
  26. std::unique_ptr<SlotTracker> MachineStorage;
  27. const Module *M = nullptr;
  28. const Function *F = nullptr;
  29. SlotTracker *Machine = nullptr;
  30. public:
  31. /// Wrap a preinitialized SlotTracker.
  32. ModuleSlotTracker(SlotTracker &Machine, const Module *M,
  33. const Function *F = nullptr);
  34. /// Construct a slot tracker from a module.
  35. ///
  36. /// If \a M is \c nullptr, uses a null slot tracker. Otherwise, initializes
  37. /// a slot tracker, and initializes all metadata slots. \c
  38. /// ShouldInitializeAllMetadata defaults to true because this is expected to
  39. /// be shared between multiple callers, and otherwise MDNode references will
  40. /// not match up.
  41. explicit ModuleSlotTracker(const Module *M,
  42. bool ShouldInitializeAllMetadata = true);
  43. /// Destructor to clean up storage.
  44. ~ModuleSlotTracker();
  45. SlotTracker *getMachine() const { return Machine; }
  46. const Module *getModule() const { return M; }
  47. const Function *getCurrentFunction() const { return F; }
  48. /// Incorporate the given function.
  49. ///
  50. /// Purge the currently incorporated function and incorporate \c F. If \c F
  51. /// is currently incorporated, this is a no-op.
  52. void incorporateFunction(const Function &F);
  53. };
  54. } // end namespace llvm
  55. #endif