PtrState.h 7.2 KB

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