1 //===--- PtrState.cpp -----------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #define DEBUG_TYPE "objc-arc-ptr-state"
11 #include "llvm/Support/Debug.h"
14 #include "DependencyAnalysis.h"
17 using namespace llvm::objcarc;
19 //===----------------------------------------------------------------------===//
21 //===----------------------------------------------------------------------===//
23 raw_ostream &llvm::objcarc::operator<<(raw_ostream &OS, const Sequence S) {
26 return OS << "S_None";
28 return OS << "S_Retain";
30 return OS << "S_CanRelease";
34 return OS << "S_Release";
35 case S_MovableRelease:
36 return OS << "S_MovableRelease";
38 return OS << "S_Stop";
40 llvm_unreachable("Unknown sequence type.");
43 //===----------------------------------------------------------------------===//
45 //===----------------------------------------------------------------------===//
47 static Sequence MergeSeqs(Sequence A, Sequence B, bool TopDown) {
51 if (A == S_None || B == S_None)
57 // Choose the side which is further along in the sequence.
58 if ((A == S_Retain || A == S_CanRelease) &&
59 (B == S_CanRelease || B == S_Use))
62 // Choose the side which is further along in the sequence.
63 if ((A == S_Use || A == S_CanRelease) &&
64 (B == S_Use || B == S_Release || B == S_Stop || B == S_MovableRelease))
66 // If both sides are releases, choose the more conservative one.
67 if (A == S_Stop && (B == S_Release || B == S_MovableRelease))
69 if (A == S_Release && B == S_MovableRelease)
76 //===----------------------------------------------------------------------===//
78 //===----------------------------------------------------------------------===//
80 void RRInfo::clear() {
82 IsTailCallRelease = false;
83 ReleaseMetadata = nullptr;
85 ReverseInsertPts.clear();
86 CFGHazardAfflicted = false;
89 bool RRInfo::Merge(const RRInfo &Other) {
90 // Conservatively merge the ReleaseMetadata information.
91 if (ReleaseMetadata != Other.ReleaseMetadata)
92 ReleaseMetadata = nullptr;
94 // Conservatively merge the boolean state.
95 KnownSafe &= Other.KnownSafe;
96 IsTailCallRelease &= Other.IsTailCallRelease;
97 CFGHazardAfflicted |= Other.CFGHazardAfflicted;
99 // Merge the call sets.
100 Calls.insert(Other.Calls.begin(), Other.Calls.end());
102 // Merge the insert point sets. If there are any differences,
103 // that makes this a partial merge.
104 bool Partial = ReverseInsertPts.size() != Other.ReverseInsertPts.size();
105 for (Instruction *Inst : Other.ReverseInsertPts)
106 Partial |= ReverseInsertPts.insert(Inst).second;
110 //===----------------------------------------------------------------------===//
112 //===----------------------------------------------------------------------===//
114 void PtrState::SetKnownPositiveRefCount() {
115 DEBUG(dbgs() << "Setting Known Positive.\n");
116 KnownPositiveRefCount = true;
119 void PtrState::ClearKnownPositiveRefCount() {
120 DEBUG(dbgs() << "Clearing Known Positive.\n");
121 KnownPositiveRefCount = false;
124 void PtrState::SetSeq(Sequence NewSeq) {
125 DEBUG(dbgs() << "Old: " << Seq << "; New: " << NewSeq << "\n");
129 void PtrState::ResetSequenceProgress(Sequence NewSeq) {
130 DEBUG(dbgs() << "Resetting sequence progress.\n");
136 void PtrState::Merge(const PtrState &Other, bool TopDown) {
137 Seq = MergeSeqs(GetSeq(), Other.GetSeq(), TopDown);
138 KnownPositiveRefCount &= Other.KnownPositiveRefCount;
140 // If we're not in a sequence (anymore), drop all associated state.
144 } else if (Partial || Other.Partial) {
145 // If we're doing a merge on a path that's previously seen a partial
146 // merge, conservatively drop the sequence, to avoid doing partial
147 // RR elimination. If the branch predicates for the two merge differ,
148 // mixing them is unsafe.
149 ClearSequenceProgress();
151 // Otherwise merge the other PtrState's RRInfo into our RRInfo. At this
152 // point, we know that currently we are not partial. Stash whether or not
153 // the merge operation caused us to undergo a partial merging of reverse
155 Partial = RRI.Merge(Other.RRI);
159 //===----------------------------------------------------------------------===//
161 //===----------------------------------------------------------------------===//
163 bool BottomUpPtrState::InitBottomUp(ARCMDKindCache &Cache, Instruction *I) {
164 // If we see two releases in a row on the same pointer. If so, make
165 // a note, and we'll cicle back to revisit it after we've
166 // hopefully eliminated the second release, which may allow us to
167 // eliminate the first release too.
168 // Theoretically we could implement removal of nested retain+release
169 // pairs by making PtrState hold a stack of states, but this is
170 // simple and avoids adding overhead for the non-nested case.
171 bool NestingDetected = false;
172 if (GetSeq() == S_Release || GetSeq() == S_MovableRelease) {
173 DEBUG(dbgs() << "Found nested releases (i.e. a release pair)\n");
174 NestingDetected = true;
177 MDNode *ReleaseMetadata = I->getMetadata(Cache.ImpreciseReleaseMDKind);
178 Sequence NewSeq = ReleaseMetadata ? S_MovableRelease : S_Release;
179 ResetSequenceProgress(NewSeq);
180 SetReleaseMetadata(ReleaseMetadata);
181 SetKnownSafe(HasKnownPositiveRefCount());
182 SetTailCallRelease(cast<CallInst>(I)->isTailCall());
184 SetKnownPositiveRefCount();
185 return NestingDetected;
188 bool BottomUpPtrState::MatchWithRetain() {
189 SetKnownPositiveRefCount();
191 Sequence OldSeq = GetSeq();
195 case S_MovableRelease:
197 // If OldSeq is not S_Use or OldSeq is S_Use and we are tracking an
198 // imprecise release, clear our reverse insertion points.
199 if (OldSeq != S_Use || IsTrackingImpreciseReleases())
200 ClearReverseInsertPts();
207 llvm_unreachable("bottom-up pointer in retain state!");
211 bool BottomUpPtrState::HandlePotentialAlterRefCount(Instruction *Inst,
213 ProvenanceAnalysis &PA,
215 Sequence Seq = GetSeq();
217 // Check for possible releases.
218 if (!CanAlterRefCount(Inst, Ptr, PA, Class))
221 DEBUG(dbgs() << "CanAlterRefCount: Seq: " << Seq << "; " << *Ptr << "\n");
222 ClearKnownPositiveRefCount();
225 SetSeq(S_CanRelease);
229 case S_MovableRelease:
234 llvm_unreachable("bottom-up pointer in retain state!");
238 void BottomUpPtrState::HandlePotentialUse(BasicBlock *BB, Instruction *Inst,
240 ProvenanceAnalysis &PA,
242 // Check for possible direct uses.
245 case S_MovableRelease:
246 if (CanUse(Inst, Ptr, PA, Class)) {
247 DEBUG(dbgs() << "CanUse: Seq: " << Seq << "; " << *Ptr << "\n");
248 assert(!HasReverseInsertPts());
249 // If this is an invoke instruction, we're scanning it as part of
250 // one of its successor blocks, since we can't insert code after it
251 // in its own block, and we don't want to split critical edges.
252 if (isa<InvokeInst>(Inst))
253 InsertReverseInsertPt(BB->getFirstInsertionPt());
255 InsertReverseInsertPt(std::next(BasicBlock::iterator(Inst)));
257 } else if (Seq == S_Release && IsUser(Class)) {
258 DEBUG(dbgs() << "PreciseReleaseUse: Seq: " << Seq << "; " << *Ptr
260 // Non-movable releases depend on any possible objc pointer use.
262 assert(!HasReverseInsertPts());
263 // As above; handle invoke specially.
264 if (isa<InvokeInst>(Inst))
265 InsertReverseInsertPt(BB->getFirstInsertionPt());
267 InsertReverseInsertPt(std::next(BasicBlock::iterator(Inst)));
271 if (CanUse(Inst, Ptr, PA, Class)) {
272 DEBUG(dbgs() << "PreciseStopUse: Seq: " << Seq << "; " << *Ptr << "\n");
281 llvm_unreachable("bottom-up pointer in retain state!");
285 //===----------------------------------------------------------------------===//
287 //===----------------------------------------------------------------------===//
289 bool TopDownPtrState::InitTopDown(ARCInstKind Kind, Instruction *I) {
290 bool NestingDetected = false;
291 // Don't do retain+release tracking for ARCInstKind::RetainRV, because
293 // better to let it remain as the first instruction after a call.
294 if (Kind != ARCInstKind::RetainRV) {
295 // If we see two retains in a row on the same pointer. If so, make
296 // a note, and we'll cicle back to revisit it after we've
297 // hopefully eliminated the second retain, which may allow us to
298 // eliminate the first retain too.
299 // Theoretically we could implement removal of nested retain+release
300 // pairs by making PtrState hold a stack of states, but this is
301 // simple and avoids adding overhead for the non-nested case.
302 if (GetSeq() == S_Retain)
303 NestingDetected = true;
305 ResetSequenceProgress(S_Retain);
306 SetKnownSafe(HasKnownPositiveRefCount());
310 SetKnownPositiveRefCount();
311 return NestingDetected;
314 bool TopDownPtrState::MatchWithRelease(ARCMDKindCache &Cache,
315 Instruction *Release) {
316 ClearKnownPositiveRefCount();
318 Sequence OldSeq = GetSeq();
320 MDNode *ReleaseMetadata = Release->getMetadata(Cache.ImpreciseReleaseMDKind);
325 if (OldSeq == S_Retain || ReleaseMetadata != nullptr)
326 ClearReverseInsertPts();
329 SetReleaseMetadata(ReleaseMetadata);
330 SetTailCallRelease(cast<CallInst>(Release)->isTailCall());
336 case S_MovableRelease:
337 llvm_unreachable("top-down pointer in bottom up state!");
341 bool TopDownPtrState::HandlePotentialAlterRefCount(Instruction *Inst,
343 ProvenanceAnalysis &PA,
345 // Check for possible releases.
346 if (!CanAlterRefCount(Inst, Ptr, PA, Class))
349 DEBUG(dbgs() << "CanAlterRefCount: Seq: " << Seq << "; " << *Ptr << "\n");
350 ClearKnownPositiveRefCount();
353 SetSeq(S_CanRelease);
354 assert(!HasReverseInsertPts());
355 InsertReverseInsertPt(Inst);
357 // One call can't cause a transition from S_Retain to S_CanRelease
358 // and S_CanRelease to S_Use. If we've made the first transition,
367 case S_MovableRelease:
368 llvm_unreachable("top-down pointer in release state!");
370 llvm_unreachable("covered switch is not covered!?");
373 void TopDownPtrState::HandlePotentialUse(Instruction *Inst, const Value *Ptr,
374 ProvenanceAnalysis &PA,
376 // Check for possible direct uses.
379 if (!CanUse(Inst, Ptr, PA, Class))
381 DEBUG(dbgs() << "CanUse: Seq: " << Seq << "; " << *Ptr << "\n");
390 case S_MovableRelease:
391 llvm_unreachable("top-down pointer in release state!");