PtrState.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. //===--- PtrState.h - ARC State for a Ptr -------------------*- 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. //
  10. // This file contains declarations for the ARC state associated with a ptr. It
  11. // is only used by the ARC Sequence Dataflow computation. By separating this
  12. // from the actual dataflow, it is easier to consider the mechanics of the ARC
  13. // optimization separate from the actual predicates being used.
  14. //
  15. //===----------------------------------------------------------------------===//
  16. #ifndef LLVM_LIB_TRANSFORMS_OBJCARC_PTRSTATE_H
  17. #define LLVM_LIB_TRANSFORMS_OBJCARC_PTRSTATE_H
  18. #include "ARCInstKind.h"
  19. #include "llvm/ADT/SmallPtrSet.h"
  20. #include "llvm/IR/Instruction.h"
  21. #include "llvm/IR/Value.h"
  22. #include "llvm/Support/raw_ostream.h"
  23. #include "llvm/Support/Debug.h"
  24. namespace llvm {
  25. namespace objcarc {
  26. class ARCMDKindCache;
  27. class ProvenanceAnalysis;
  28. /// \enum Sequence
  29. ///
  30. /// \brief A sequence of states that a pointer may go through in which an
  31. /// objc_retain and objc_release are actually needed.
  32. enum Sequence {
  33. S_None,
  34. S_Retain, ///< objc_retain(x).
  35. S_CanRelease, ///< foo(x) -- x could possibly see a ref count decrement.
  36. S_Use, ///< any use of x.
  37. S_Stop, ///< like S_Release, but code motion is stopped.
  38. S_Release, ///< objc_release(x).
  39. S_MovableRelease ///< objc_release(x), !clang.imprecise_release.
  40. };
  41. raw_ostream &operator<<(raw_ostream &OS,
  42. const Sequence S) LLVM_ATTRIBUTE_UNUSED;
  43. /// \brief Unidirectional information about either a
  44. /// retain-decrement-use-release sequence or release-use-decrement-retain
  45. /// reverse sequence.
  46. struct RRInfo {
  47. /// After an objc_retain, the reference count of the referenced
  48. /// object is known to be positive. Similarly, before an objc_release, the
  49. /// reference count of the referenced object is known to be positive. If
  50. /// there are retain-release pairs in code regions where the retain count
  51. /// is known to be positive, they can be eliminated, regardless of any side
  52. /// effects between them.
  53. ///
  54. /// Also, a retain+release pair nested within another retain+release
  55. /// pair all on the known same pointer value can be eliminated, regardless
  56. /// of any intervening side effects.
  57. ///
  58. /// KnownSafe is true when either of these conditions is satisfied.
  59. bool KnownSafe;
  60. /// True of the objc_release calls are all marked with the "tail" keyword.
  61. bool IsTailCallRelease;
  62. /// If the Calls are objc_release calls and they all have a
  63. /// clang.imprecise_release tag, this is the metadata tag.
  64. MDNode *ReleaseMetadata;
  65. /// For a top-down sequence, the set of objc_retains or
  66. /// objc_retainBlocks. For bottom-up, the set of objc_releases.
  67. SmallPtrSet<Instruction *, 2> Calls;
  68. /// The set of optimal insert positions for moving calls in the opposite
  69. /// sequence.
  70. SmallPtrSet<Instruction *, 2> ReverseInsertPts;
  71. /// If this is true, we cannot perform code motion but can still remove
  72. /// retain/release pairs.
  73. bool CFGHazardAfflicted;
  74. RRInfo()
  75. : KnownSafe(false), IsTailCallRelease(false), ReleaseMetadata(nullptr),
  76. CFGHazardAfflicted(false) {}
  77. void clear();
  78. /// Conservatively merge the two RRInfo. Returns true if a partial merge has
  79. /// occurred, false otherwise.
  80. bool Merge(const RRInfo &Other);
  81. };
  82. /// \brief This class summarizes several per-pointer runtime properties which
  83. /// are propogated through the flow graph.
  84. class PtrState {
  85. protected:
  86. /// True if the reference count is known to be incremented.
  87. bool KnownPositiveRefCount;
  88. /// True if we've seen an opportunity for partial RR elimination, such as
  89. /// pushing calls into a CFG triangle or into one side of a CFG diamond.
  90. bool Partial;
  91. /// The current position in the sequence.
  92. unsigned char Seq : 8;
  93. /// Unidirectional information about the current sequence.
  94. RRInfo RRI;
  95. PtrState() : KnownPositiveRefCount(false), Partial(false), Seq(S_None) {}
  96. public:
  97. bool IsKnownSafe() const { return RRI.KnownSafe; }
  98. void SetKnownSafe(const bool NewValue) { RRI.KnownSafe = NewValue; }
  99. bool IsTailCallRelease() const { return RRI.IsTailCallRelease; }
  100. void SetTailCallRelease(const bool NewValue) {
  101. RRI.IsTailCallRelease = NewValue;
  102. }
  103. bool IsTrackingImpreciseReleases() const {
  104. return RRI.ReleaseMetadata != nullptr;
  105. }
  106. const MDNode *GetReleaseMetadata() const { return RRI.ReleaseMetadata; }
  107. void SetReleaseMetadata(MDNode *NewValue) { RRI.ReleaseMetadata = NewValue; }
  108. bool IsCFGHazardAfflicted() const { return RRI.CFGHazardAfflicted; }
  109. void SetCFGHazardAfflicted(const bool NewValue) {
  110. RRI.CFGHazardAfflicted = NewValue;
  111. }
  112. void SetKnownPositiveRefCount();
  113. void ClearKnownPositiveRefCount();
  114. bool HasKnownPositiveRefCount() const { return KnownPositiveRefCount; }
  115. void SetSeq(Sequence NewSeq);
  116. Sequence GetSeq() const { return static_cast<Sequence>(Seq); }
  117. void ClearSequenceProgress() { ResetSequenceProgress(S_None); }
  118. void ResetSequenceProgress(Sequence NewSeq);
  119. void Merge(const PtrState &Other, bool TopDown);
  120. void InsertCall(Instruction *I) { RRI.Calls.insert(I); }
  121. void InsertReverseInsertPt(Instruction *I) { RRI.ReverseInsertPts.insert(I); }
  122. void ClearReverseInsertPts() { RRI.ReverseInsertPts.clear(); }
  123. bool HasReverseInsertPts() const { return !RRI.ReverseInsertPts.empty(); }
  124. const RRInfo &GetRRInfo() const { return RRI; }
  125. };
  126. struct BottomUpPtrState : PtrState {
  127. BottomUpPtrState() : PtrState() {}
  128. /// (Re-)Initialize this bottom up pointer returning true if we detected a
  129. /// pointer with nested releases.
  130. bool InitBottomUp(ARCMDKindCache &Cache, Instruction *I);
  131. /// Return true if this set of releases can be paired with a release. Modifies
  132. /// state appropriately to reflect that the matching occured if it is
  133. /// successful.
  134. ///
  135. /// It is assumed that one has already checked that the RCIdentity of the
  136. /// retain and the RCIdentity of this ptr state are the same.
  137. bool MatchWithRetain();
  138. void HandlePotentialUse(BasicBlock *BB, Instruction *Inst, const Value *Ptr,
  139. ProvenanceAnalysis &PA, ARCInstKind Class);
  140. bool HandlePotentialAlterRefCount(Instruction *Inst, const Value *Ptr,
  141. ProvenanceAnalysis &PA, ARCInstKind Class);
  142. };
  143. struct TopDownPtrState : PtrState {
  144. TopDownPtrState() : PtrState() {}
  145. /// (Re-)Initialize this bottom up pointer returning true if we detected a
  146. /// pointer with nested releases.
  147. bool InitTopDown(ARCInstKind Kind, Instruction *I);
  148. /// Return true if this set of retains can be paired with the given
  149. /// release. Modifies state appropriately to reflect that the matching
  150. /// occured.
  151. bool MatchWithRelease(ARCMDKindCache &Cache, Instruction *Release);
  152. void HandlePotentialUse(Instruction *Inst, const Value *Ptr,
  153. ProvenanceAnalysis &PA, ARCInstKind Class);
  154. bool HandlePotentialAlterRefCount(Instruction *Inst, const Value *Ptr,
  155. ProvenanceAnalysis &PA, ARCInstKind Class);
  156. };
  157. } // end namespace objcarc
  158. } // end namespace llvm
  159. #endif