ModuleSlotTracker.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 <memory>
  16. namespace llvm {
  17. class Module;
  18. class Function;
  19. class SlotTracker;
  20. class Value;
  21. /// Manage lifetime of a slot tracker for printing IR.
  22. ///
  23. /// Wrapper around the \a SlotTracker used internally by \a AsmWriter. This
  24. /// class allows callers to share the cost of incorporating the metadata in a
  25. /// module or a function.
  26. ///
  27. /// If the IR changes from underneath \a ModuleSlotTracker, strings like
  28. /// "<badref>" will be printed, or, worse, the wrong slots entirely.
  29. class ModuleSlotTracker {
  30. /// Storage for a slot tracker.
  31. std::unique_ptr<SlotTracker> MachineStorage;
  32. bool ShouldCreateStorage = false;
  33. bool ShouldInitializeAllMetadata = false;
  34. const Module *M = nullptr;
  35. const Function *F = nullptr;
  36. SlotTracker *Machine = nullptr;
  37. public:
  38. /// Wrap a preinitialized SlotTracker.
  39. ModuleSlotTracker(SlotTracker &Machine, const Module *M,
  40. const Function *F = nullptr);
  41. /// Construct a slot tracker from a module.
  42. ///
  43. /// If \a M is \c nullptr, uses a null slot tracker. Otherwise, initializes
  44. /// a slot tracker, and initializes all metadata slots. \c
  45. /// ShouldInitializeAllMetadata defaults to true because this is expected to
  46. /// be shared between multiple callers, and otherwise MDNode references will
  47. /// not match up.
  48. explicit ModuleSlotTracker(const Module *M,
  49. bool ShouldInitializeAllMetadata = true);
  50. /// Destructor to clean up storage.
  51. ~ModuleSlotTracker();
  52. /// Lazily creates a slot tracker.
  53. SlotTracker *getMachine();
  54. const Module *getModule() const { return M; }
  55. const Function *getCurrentFunction() const { return F; }
  56. /// Incorporate the given function.
  57. ///
  58. /// Purge the currently incorporated function and incorporate \c F. If \c F
  59. /// is currently incorporated, this is a no-op.
  60. void incorporateFunction(const Function &F);
  61. /// Return the slot number of the specified local value.
  62. ///
  63. /// A function that defines this value should be incorporated prior to calling
  64. /// this method.
  65. /// Return -1 if the value is not in the function's SlotTracker.
  66. int getLocalSlot(const Value *V);
  67. };
  68. } // end namespace llvm
  69. #endif
  70. #ifdef __GNUC__
  71. #pragma GCC diagnostic pop
  72. #endif