MetadataTracking.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. //===- llvm/IR/MetadataTracking.h - Metadata tracking ---------------------===//
  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. //
  10. // Low-level functions to enable tracking of metadata that could RAUW.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_IR_METADATATRACKING_H
  14. #define LLVM_IR_METADATATRACKING_H
  15. #include "llvm/ADT/PointerUnion.h"
  16. #include "llvm/Support/Casting.h"
  17. #include <type_traits>
  18. namespace llvm {
  19. class Metadata;
  20. class MetadataAsValue;
  21. /// \brief API for tracking metadata references through RAUW and deletion.
  22. ///
  23. /// Shared API for updating \a Metadata pointers in subclasses that support
  24. /// RAUW.
  25. ///
  26. /// This API is not meant to be used directly. See \a TrackingMDRef for a
  27. /// user-friendly tracking reference.
  28. class MetadataTracking {
  29. public:
  30. /// \brief Track the reference to metadata.
  31. ///
  32. /// Register \c MD with \c *MD, if the subclass supports tracking. If \c *MD
  33. /// gets RAUW'ed, \c MD will be updated to the new address. If \c *MD gets
  34. /// deleted, \c MD will be set to \c nullptr.
  35. ///
  36. /// If tracking isn't supported, \c *MD will not change.
  37. ///
  38. /// \return true iff tracking is supported by \c MD.
  39. static bool track(Metadata *&MD) {
  40. return track(&MD, *MD, static_cast<Metadata *>(nullptr));
  41. }
  42. /// \brief Track the reference to metadata for \a Metadata.
  43. ///
  44. /// As \a track(Metadata*&), but with support for calling back to \c Owner to
  45. /// tell it that its operand changed. This could trigger \c Owner being
  46. /// re-uniqued.
  47. static bool track(void *Ref, Metadata &MD, Metadata &Owner) {
  48. return track(Ref, MD, &Owner);
  49. }
  50. /// \brief Track the reference to metadata for \a MetadataAsValue.
  51. ///
  52. /// As \a track(Metadata*&), but with support for calling back to \c Owner to
  53. /// tell it that its operand changed. This could trigger \c Owner being
  54. /// re-uniqued.
  55. static bool track(void *Ref, Metadata &MD, MetadataAsValue &Owner) {
  56. return track(Ref, MD, &Owner);
  57. }
  58. /// \brief Stop tracking a reference to metadata.
  59. ///
  60. /// Stops \c *MD from tracking \c MD.
  61. static void untrack(Metadata *&MD) { untrack(&MD, *MD); }
  62. static void untrack(void *Ref, Metadata &MD);
  63. /// \brief Move tracking from one reference to another.
  64. ///
  65. /// Semantically equivalent to \c untrack(MD) followed by \c track(New),
  66. /// except that ownership callbacks are maintained.
  67. ///
  68. /// Note: it is an error if \c *MD does not equal \c New.
  69. ///
  70. /// \return true iff tracking is supported by \c MD.
  71. static bool retrack(Metadata *&MD, Metadata *&New) {
  72. return retrack(&MD, *MD, &New);
  73. }
  74. static bool retrack(void *Ref, Metadata &MD, void *New);
  75. /// \brief Check whether metadata is replaceable.
  76. static bool isReplaceable(const Metadata &MD);
  77. typedef PointerUnion<MetadataAsValue *, Metadata *> OwnerTy;
  78. private:
  79. /// \brief Track a reference to metadata for an owner.
  80. ///
  81. /// Generalized version of tracking.
  82. static bool track(void *Ref, Metadata &MD, OwnerTy Owner);
  83. };
  84. } // end namespace llvm
  85. #endif