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"
15 using namespace llvm::objcarc;
17 raw_ostream &llvm::objcarc::operator<<(raw_ostream &OS, const Sequence S) {
20 return OS << "S_None";
22 return OS << "S_Retain";
24 return OS << "S_CanRelease";
28 return OS << "S_Release";
29 case S_MovableRelease:
30 return OS << "S_MovableRelease";
32 return OS << "S_Stop";
34 llvm_unreachable("Unknown sequence type.");
37 static Sequence MergeSeqs(Sequence A, Sequence B, bool TopDown) {
41 if (A == S_None || B == S_None)
47 // Choose the side which is further along in the sequence.
48 if ((A == S_Retain || A == S_CanRelease) &&
49 (B == S_CanRelease || B == S_Use))
52 // Choose the side which is further along in the sequence.
53 if ((A == S_Use || A == S_CanRelease) &&
54 (B == S_Use || B == S_Release || B == S_Stop || B == S_MovableRelease))
56 // If both sides are releases, choose the more conservative one.
57 if (A == S_Stop && (B == S_Release || B == S_MovableRelease))
59 if (A == S_Release && B == S_MovableRelease)
66 void RRInfo::clear() {
68 IsTailCallRelease = false;
69 ReleaseMetadata = nullptr;
71 ReverseInsertPts.clear();
72 CFGHazardAfflicted = false;
75 bool RRInfo::Merge(const RRInfo &Other) {
76 // Conservatively merge the ReleaseMetadata information.
77 if (ReleaseMetadata != Other.ReleaseMetadata)
78 ReleaseMetadata = nullptr;
80 // Conservatively merge the boolean state.
81 KnownSafe &= Other.KnownSafe;
82 IsTailCallRelease &= Other.IsTailCallRelease;
83 CFGHazardAfflicted |= Other.CFGHazardAfflicted;
85 // Merge the call sets.
86 Calls.insert(Other.Calls.begin(), Other.Calls.end());
88 // Merge the insert point sets. If there are any differences,
89 // that makes this a partial merge.
90 bool Partial = ReverseInsertPts.size() != Other.ReverseInsertPts.size();
91 for (Instruction *Inst : Other.ReverseInsertPts)
92 Partial |= ReverseInsertPts.insert(Inst).second;
96 void PtrState::SetKnownPositiveRefCount() {
97 DEBUG(dbgs() << "Setting Known Positive.\n");
98 KnownPositiveRefCount = true;
101 void PtrState::ClearKnownPositiveRefCount() {
102 DEBUG(dbgs() << "Clearing Known Positive.\n");
103 KnownPositiveRefCount = false;
106 void PtrState::SetSeq(Sequence NewSeq) {
107 DEBUG(dbgs() << "Old: " << Seq << "; New: " << NewSeq << "\n");
111 void PtrState::ResetSequenceProgress(Sequence NewSeq) {
112 DEBUG(dbgs() << "Resetting sequence progress.\n");
118 void PtrState::Merge(const PtrState &Other, bool TopDown) {
119 Seq = MergeSeqs(GetSeq(), Other.GetSeq(), TopDown);
120 KnownPositiveRefCount &= Other.KnownPositiveRefCount;
122 // If we're not in a sequence (anymore), drop all associated state.
126 } else if (Partial || Other.Partial) {
127 // If we're doing a merge on a path that's previously seen a partial
128 // merge, conservatively drop the sequence, to avoid doing partial
129 // RR elimination. If the branch predicates for the two merge differ,
130 // mixing them is unsafe.
131 ClearSequenceProgress();
133 // Otherwise merge the other PtrState's RRInfo into our RRInfo. At this
134 // point, we know that currently we are not partial. Stash whether or not
135 // the merge operation caused us to undergo a partial merging of reverse
137 Partial = RRI.Merge(Other.RRI);