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 //===----------------------------------------------------------------------===//
13 using namespace llvm::objcarc;
15 raw_ostream &operator<<(raw_ostream &OS, const Sequence S) {
18 return OS << "S_None";
20 return OS << "S_Retain";
22 return OS << "S_CanRelease";
26 return OS << "S_Release";
27 case S_MovableRelease:
28 return OS << "S_MovableRelease";
30 return OS << "S_Stop";
32 llvm_unreachable("Unknown sequence type.");
35 static Sequence MergeSeqs(Sequence A, Sequence B, bool TopDown) {
39 if (A == S_None || B == S_None)
45 // Choose the side which is further along in the sequence.
46 if ((A == S_Retain || A == S_CanRelease) &&
47 (B == S_CanRelease || B == S_Use))
50 // Choose the side which is further along in the sequence.
51 if ((A == S_Use || A == S_CanRelease) &&
52 (B == S_Use || B == S_Release || B == S_Stop || B == S_MovableRelease))
54 // If both sides are releases, choose the more conservative one.
55 if (A == S_Stop && (B == S_Release || B == S_MovableRelease))
57 if (A == S_Release && B == S_MovableRelease)
64 void RRInfo::clear() {
66 IsTailCallRelease = false;
67 ReleaseMetadata = nullptr;
69 ReverseInsertPts.clear();
70 CFGHazardAfflicted = false;
73 bool RRInfo::Merge(const RRInfo &Other) {
74 // Conservatively merge the ReleaseMetadata information.
75 if (ReleaseMetadata != Other.ReleaseMetadata)
76 ReleaseMetadata = nullptr;
78 // Conservatively merge the boolean state.
79 KnownSafe &= Other.KnownSafe;
80 IsTailCallRelease &= Other.IsTailCallRelease;
81 CFGHazardAfflicted |= Other.CFGHazardAfflicted;
83 // Merge the call sets.
84 Calls.insert(Other.Calls.begin(), Other.Calls.end());
86 // Merge the insert point sets. If there are any differences,
87 // that makes this a partial merge.
88 bool Partial = ReverseInsertPts.size() != Other.ReverseInsertPts.size();
89 for (Instruction *Inst : Other.ReverseInsertPts)
90 Partial |= ReverseInsertPts.insert(Inst).second;
94 void PtrState::Merge(const PtrState &Other, bool TopDown) {
95 Seq = MergeSeqs(GetSeq(), Other.GetSeq(), TopDown);
96 KnownPositiveRefCount &= Other.KnownPositiveRefCount;
98 // If we're not in a sequence (anymore), drop all associated state.
102 } else if (Partial || Other.Partial) {
103 // If we're doing a merge on a path that's previously seen a partial
104 // merge, conservatively drop the sequence, to avoid doing partial
105 // RR elimination. If the branch predicates for the two merge differ,
106 // mixing them is unsafe.
107 ClearSequenceProgress();
109 // Otherwise merge the other PtrState's RRInfo into our RRInfo. At this
110 // point, we know that currently we are not partial. Stash whether or not
111 // the merge operation caused us to undergo a partial merging of reverse
113 Partial = RRI.Merge(Other.RRI);