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