Eugene Zelenko | 57bd5a0 | 2017-10-27 01:09:08 +0000 | [diff] [blame] | 1 | //===- PtrState.h - ARC State for a Ptr -------------------------*- C++ -*-===// |
Michael Gottesman | 68b91db | 2015-03-05 23:29:03 +0000 | [diff] [blame] | 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 | |
| 17 | #ifndef LLVM_LIB_TRANSFORMS_OBJCARC_PTRSTATE_H |
| 18 | #define LLVM_LIB_TRANSFORMS_OBJCARC_PTRSTATE_H |
| 19 | |
| 20 | #include "llvm/ADT/SmallPtrSet.h" |
Chandler Carruth | 0f79218 | 2015-08-20 08:06:03 +0000 | [diff] [blame] | 21 | #include "llvm/Analysis/ObjCARCInstKind.h" |
Eugene Zelenko | 57bd5a0 | 2017-10-27 01:09:08 +0000 | [diff] [blame] | 22 | #include "llvm/Support/Compiler.h" |
Michael Gottesman | 68b91db | 2015-03-05 23:29:03 +0000 | [diff] [blame] | 23 | |
| 24 | namespace llvm { |
Eugene Zelenko | 57bd5a0 | 2017-10-27 01:09:08 +0000 | [diff] [blame] | 25 | |
| 26 | class BasicBlock; |
| 27 | class Instruction; |
| 28 | class MDNode; |
| 29 | class raw_ostream; |
| 30 | class Value; |
| 31 | |
Michael Gottesman | 68b91db | 2015-03-05 23:29:03 +0000 | [diff] [blame] | 32 | namespace objcarc { |
| 33 | |
Michael Gottesman | 65cb737 | 2015-03-16 07:02:27 +0000 | [diff] [blame] | 34 | class ARCMDKindCache; |
Michael Gottesman | 16e6a20 | 2015-03-06 02:07:12 +0000 | [diff] [blame] | 35 | class ProvenanceAnalysis; |
Michael Gottesman | 4eae396 | 2015-03-06 00:34:39 +0000 | [diff] [blame] | 36 | |
Michael Gottesman | 68b91db | 2015-03-05 23:29:03 +0000 | [diff] [blame] | 37 | /// \enum Sequence |
| 38 | /// |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 39 | /// A sequence of states that a pointer may go through in which an |
Michael Gottesman | 68b91db | 2015-03-05 23:29:03 +0000 | [diff] [blame] | 40 | /// objc_retain and objc_release are actually needed. |
| 41 | enum Sequence { |
| 42 | S_None, |
| 43 | S_Retain, ///< objc_retain(x). |
| 44 | S_CanRelease, ///< foo(x) -- x could possibly see a ref count decrement. |
| 45 | S_Use, ///< any use of x. |
| 46 | S_Stop, ///< like S_Release, but code motion is stopped. |
| 47 | S_Release, ///< objc_release(x). |
| 48 | S_MovableRelease ///< objc_release(x), !clang.imprecise_release. |
| 49 | }; |
| 50 | |
| 51 | raw_ostream &operator<<(raw_ostream &OS, |
| 52 | const Sequence S) LLVM_ATTRIBUTE_UNUSED; |
| 53 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 54 | /// Unidirectional information about either a |
Michael Gottesman | 68b91db | 2015-03-05 23:29:03 +0000 | [diff] [blame] | 55 | /// retain-decrement-use-release sequence or release-use-decrement-retain |
| 56 | /// reverse sequence. |
| 57 | struct RRInfo { |
| 58 | /// After an objc_retain, the reference count of the referenced |
| 59 | /// object is known to be positive. Similarly, before an objc_release, the |
| 60 | /// reference count of the referenced object is known to be positive. If |
| 61 | /// there are retain-release pairs in code regions where the retain count |
| 62 | /// is known to be positive, they can be eliminated, regardless of any side |
| 63 | /// effects between them. |
| 64 | /// |
| 65 | /// Also, a retain+release pair nested within another retain+release |
| 66 | /// pair all on the known same pointer value can be eliminated, regardless |
| 67 | /// of any intervening side effects. |
| 68 | /// |
| 69 | /// KnownSafe is true when either of these conditions is satisfied. |
Eugene Zelenko | 57bd5a0 | 2017-10-27 01:09:08 +0000 | [diff] [blame] | 70 | bool KnownSafe = false; |
Michael Gottesman | 68b91db | 2015-03-05 23:29:03 +0000 | [diff] [blame] | 71 | |
| 72 | /// True of the objc_release calls are all marked with the "tail" keyword. |
Eugene Zelenko | 57bd5a0 | 2017-10-27 01:09:08 +0000 | [diff] [blame] | 73 | bool IsTailCallRelease = false; |
Michael Gottesman | 68b91db | 2015-03-05 23:29:03 +0000 | [diff] [blame] | 74 | |
| 75 | /// If the Calls are objc_release calls and they all have a |
| 76 | /// clang.imprecise_release tag, this is the metadata tag. |
Eugene Zelenko | 57bd5a0 | 2017-10-27 01:09:08 +0000 | [diff] [blame] | 77 | MDNode *ReleaseMetadata = nullptr; |
Michael Gottesman | 68b91db | 2015-03-05 23:29:03 +0000 | [diff] [blame] | 78 | |
| 79 | /// For a top-down sequence, the set of objc_retains or |
| 80 | /// objc_retainBlocks. For bottom-up, the set of objc_releases. |
| 81 | SmallPtrSet<Instruction *, 2> Calls; |
| 82 | |
| 83 | /// The set of optimal insert positions for moving calls in the opposite |
| 84 | /// sequence. |
| 85 | SmallPtrSet<Instruction *, 2> ReverseInsertPts; |
| 86 | |
| 87 | /// If this is true, we cannot perform code motion but can still remove |
| 88 | /// retain/release pairs. |
Eugene Zelenko | 57bd5a0 | 2017-10-27 01:09:08 +0000 | [diff] [blame] | 89 | bool CFGHazardAfflicted = false; |
Michael Gottesman | 68b91db | 2015-03-05 23:29:03 +0000 | [diff] [blame] | 90 | |
Eugene Zelenko | 57bd5a0 | 2017-10-27 01:09:08 +0000 | [diff] [blame] | 91 | RRInfo() = default; |
Michael Gottesman | 68b91db | 2015-03-05 23:29:03 +0000 | [diff] [blame] | 92 | |
| 93 | void clear(); |
| 94 | |
| 95 | /// Conservatively merge the two RRInfo. Returns true if a partial merge has |
| 96 | /// occurred, false otherwise. |
| 97 | bool Merge(const RRInfo &Other); |
| 98 | }; |
| 99 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 100 | /// This class summarizes several per-pointer runtime properties which |
Benjamin Kramer | df005cb | 2015-08-08 18:27:36 +0000 | [diff] [blame] | 101 | /// are propagated through the flow graph. |
Michael Gottesman | 68b91db | 2015-03-05 23:29:03 +0000 | [diff] [blame] | 102 | class PtrState { |
Michael Gottesman | feb138e | 2015-03-06 00:34:36 +0000 | [diff] [blame] | 103 | protected: |
Michael Gottesman | 68b91db | 2015-03-05 23:29:03 +0000 | [diff] [blame] | 104 | /// True if the reference count is known to be incremented. |
Eugene Zelenko | 57bd5a0 | 2017-10-27 01:09:08 +0000 | [diff] [blame] | 105 | bool KnownPositiveRefCount = false; |
Michael Gottesman | 68b91db | 2015-03-05 23:29:03 +0000 | [diff] [blame] | 106 | |
| 107 | /// True if we've seen an opportunity for partial RR elimination, such as |
| 108 | /// pushing calls into a CFG triangle or into one side of a CFG diamond. |
Eugene Zelenko | 57bd5a0 | 2017-10-27 01:09:08 +0000 | [diff] [blame] | 109 | bool Partial = false; |
Michael Gottesman | 68b91db | 2015-03-05 23:29:03 +0000 | [diff] [blame] | 110 | |
| 111 | /// The current position in the sequence. |
| 112 | unsigned char Seq : 8; |
| 113 | |
| 114 | /// Unidirectional information about the current sequence. |
| 115 | RRInfo RRI; |
| 116 | |
Eugene Zelenko | 57bd5a0 | 2017-10-27 01:09:08 +0000 | [diff] [blame] | 117 | PtrState() : Seq(S_None) {} |
Michael Gottesman | 68b91db | 2015-03-05 23:29:03 +0000 | [diff] [blame] | 118 | |
Michael Gottesman | feb138e | 2015-03-06 00:34:36 +0000 | [diff] [blame] | 119 | public: |
Michael Gottesman | 68b91db | 2015-03-05 23:29:03 +0000 | [diff] [blame] | 120 | bool IsKnownSafe() const { return RRI.KnownSafe; } |
| 121 | |
| 122 | void SetKnownSafe(const bool NewValue) { RRI.KnownSafe = NewValue; } |
| 123 | |
| 124 | bool IsTailCallRelease() const { return RRI.IsTailCallRelease; } |
| 125 | |
| 126 | void SetTailCallRelease(const bool NewValue) { |
| 127 | RRI.IsTailCallRelease = NewValue; |
| 128 | } |
| 129 | |
| 130 | bool IsTrackingImpreciseReleases() const { |
| 131 | return RRI.ReleaseMetadata != nullptr; |
| 132 | } |
| 133 | |
| 134 | const MDNode *GetReleaseMetadata() const { return RRI.ReleaseMetadata; } |
| 135 | |
| 136 | void SetReleaseMetadata(MDNode *NewValue) { RRI.ReleaseMetadata = NewValue; } |
| 137 | |
| 138 | bool IsCFGHazardAfflicted() const { return RRI.CFGHazardAfflicted; } |
| 139 | |
| 140 | void SetCFGHazardAfflicted(const bool NewValue) { |
| 141 | RRI.CFGHazardAfflicted = NewValue; |
| 142 | } |
| 143 | |
Michael Gottesman | d45907b | 2015-03-05 23:57:07 +0000 | [diff] [blame] | 144 | void SetKnownPositiveRefCount(); |
| 145 | void ClearKnownPositiveRefCount(); |
Michael Gottesman | 68b91db | 2015-03-05 23:29:03 +0000 | [diff] [blame] | 146 | |
| 147 | bool HasKnownPositiveRefCount() const { return KnownPositiveRefCount; } |
| 148 | |
Michael Gottesman | d45907b | 2015-03-05 23:57:07 +0000 | [diff] [blame] | 149 | void SetSeq(Sequence NewSeq); |
Michael Gottesman | 68b91db | 2015-03-05 23:29:03 +0000 | [diff] [blame] | 150 | |
| 151 | Sequence GetSeq() const { return static_cast<Sequence>(Seq); } |
| 152 | |
| 153 | void ClearSequenceProgress() { ResetSequenceProgress(S_None); } |
| 154 | |
Michael Gottesman | d45907b | 2015-03-05 23:57:07 +0000 | [diff] [blame] | 155 | void ResetSequenceProgress(Sequence NewSeq); |
Michael Gottesman | 68b91db | 2015-03-05 23:29:03 +0000 | [diff] [blame] | 156 | void Merge(const PtrState &Other, bool TopDown); |
| 157 | |
| 158 | void InsertCall(Instruction *I) { RRI.Calls.insert(I); } |
| 159 | |
| 160 | void InsertReverseInsertPt(Instruction *I) { RRI.ReverseInsertPts.insert(I); } |
| 161 | |
| 162 | void ClearReverseInsertPts() { RRI.ReverseInsertPts.clear(); } |
| 163 | |
| 164 | bool HasReverseInsertPts() const { return !RRI.ReverseInsertPts.empty(); } |
| 165 | |
| 166 | const RRInfo &GetRRInfo() const { return RRI; } |
| 167 | }; |
| 168 | |
Michael Gottesman | feb138e | 2015-03-06 00:34:36 +0000 | [diff] [blame] | 169 | struct BottomUpPtrState : PtrState { |
Eugene Zelenko | 57bd5a0 | 2017-10-27 01:09:08 +0000 | [diff] [blame] | 170 | BottomUpPtrState() = default; |
Michael Gottesman | 4eae396 | 2015-03-06 00:34:39 +0000 | [diff] [blame] | 171 | |
| 172 | /// (Re-)Initialize this bottom up pointer returning true if we detected a |
| 173 | /// pointer with nested releases. |
| 174 | bool InitBottomUp(ARCMDKindCache &Cache, Instruction *I); |
Michael Gottesman | 6080596 | 2015-03-06 00:34:42 +0000 | [diff] [blame] | 175 | |
| 176 | /// Return true if this set of releases can be paired with a release. Modifies |
Benjamin Kramer | df005cb | 2015-08-08 18:27:36 +0000 | [diff] [blame] | 177 | /// state appropriately to reflect that the matching occurred if it is |
Michael Gottesman | 6080596 | 2015-03-06 00:34:42 +0000 | [diff] [blame] | 178 | /// successful. |
| 179 | /// |
| 180 | /// It is assumed that one has already checked that the RCIdentity of the |
| 181 | /// retain and the RCIdentity of this ptr state are the same. |
| 182 | bool MatchWithRetain(); |
Michael Gottesman | 16e6a20 | 2015-03-06 02:07:12 +0000 | [diff] [blame] | 183 | |
| 184 | void HandlePotentialUse(BasicBlock *BB, Instruction *Inst, const Value *Ptr, |
| 185 | ProvenanceAnalysis &PA, ARCInstKind Class); |
| 186 | bool HandlePotentialAlterRefCount(Instruction *Inst, const Value *Ptr, |
| 187 | ProvenanceAnalysis &PA, ARCInstKind Class); |
Michael Gottesman | feb138e | 2015-03-06 00:34:36 +0000 | [diff] [blame] | 188 | }; |
| 189 | |
Michael Gottesman | feb138e | 2015-03-06 00:34:36 +0000 | [diff] [blame] | 190 | struct TopDownPtrState : PtrState { |
Eugene Zelenko | 57bd5a0 | 2017-10-27 01:09:08 +0000 | [diff] [blame] | 191 | TopDownPtrState() = default; |
Michael Gottesman | 4eae396 | 2015-03-06 00:34:39 +0000 | [diff] [blame] | 192 | |
| 193 | /// (Re-)Initialize this bottom up pointer returning true if we detected a |
| 194 | /// pointer with nested releases. |
| 195 | bool InitTopDown(ARCInstKind Kind, Instruction *I); |
Michael Gottesman | 6080596 | 2015-03-06 00:34:42 +0000 | [diff] [blame] | 196 | |
| 197 | /// Return true if this set of retains can be paired with the given |
| 198 | /// release. Modifies state appropriately to reflect that the matching |
Benjamin Kramer | df005cb | 2015-08-08 18:27:36 +0000 | [diff] [blame] | 199 | /// occurred. |
Michael Gottesman | 6080596 | 2015-03-06 00:34:42 +0000 | [diff] [blame] | 200 | bool MatchWithRelease(ARCMDKindCache &Cache, Instruction *Release); |
Michael Gottesman | 16e6a20 | 2015-03-06 02:07:12 +0000 | [diff] [blame] | 201 | |
| 202 | void HandlePotentialUse(Instruction *Inst, const Value *Ptr, |
| 203 | ProvenanceAnalysis &PA, ARCInstKind Class); |
| 204 | |
| 205 | bool HandlePotentialAlterRefCount(Instruction *Inst, const Value *Ptr, |
| 206 | ProvenanceAnalysis &PA, ARCInstKind Class); |
Michael Gottesman | feb138e | 2015-03-06 00:34:36 +0000 | [diff] [blame] | 207 | }; |
| 208 | |
Michael Gottesman | 68b91db | 2015-03-05 23:29:03 +0000 | [diff] [blame] | 209 | } // end namespace objcarc |
Eugene Zelenko | 57bd5a0 | 2017-10-27 01:09:08 +0000 | [diff] [blame] | 210 | |
Michael Gottesman | 68b91db | 2015-03-05 23:29:03 +0000 | [diff] [blame] | 211 | } // end namespace llvm |
| 212 | |
Eugene Zelenko | 57bd5a0 | 2017-10-27 01:09:08 +0000 | [diff] [blame] | 213 | #endif // LLVM_LIB_TRANSFORMS_OBJCARC_PTRSTATE_H |