2015-03-05 23:29:03 +00:00
|
|
|
//===--- PtrState.cpp -----------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "PtrState.h"
|
2015-03-06 02:07:12 +00:00
|
|
|
#include "DependencyAnalysis.h"
|
2015-03-23 19:32:43 +00:00
|
|
|
#include "ObjCARC.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2015-03-05 23:29:03 +00:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace llvm::objcarc;
|
|
|
|
|
2015-03-23 19:32:43 +00:00
|
|
|
#define DEBUG_TYPE "objc-arc-ptr-state"
|
|
|
|
|
2015-03-06 02:07:12 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Utility
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2015-03-05 23:57:07 +00:00
|
|
|
raw_ostream &llvm::objcarc::operator<<(raw_ostream &OS, const Sequence S) {
|
2015-03-05 23:29:03 +00:00
|
|
|
switch (S) {
|
|
|
|
case S_None:
|
|
|
|
return OS << "S_None";
|
|
|
|
case S_Retain:
|
|
|
|
return OS << "S_Retain";
|
|
|
|
case S_CanRelease:
|
|
|
|
return OS << "S_CanRelease";
|
|
|
|
case S_Use:
|
|
|
|
return OS << "S_Use";
|
|
|
|
case S_Release:
|
|
|
|
return OS << "S_Release";
|
|
|
|
case S_MovableRelease:
|
|
|
|
return OS << "S_MovableRelease";
|
|
|
|
case S_Stop:
|
|
|
|
return OS << "S_Stop";
|
|
|
|
}
|
|
|
|
llvm_unreachable("Unknown sequence type.");
|
|
|
|
}
|
|
|
|
|
2015-03-06 02:07:12 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Sequence
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2015-03-05 23:29:03 +00:00
|
|
|
static Sequence MergeSeqs(Sequence A, Sequence B, bool TopDown) {
|
|
|
|
// The easy cases.
|
|
|
|
if (A == B)
|
|
|
|
return A;
|
|
|
|
if (A == S_None || B == S_None)
|
|
|
|
return S_None;
|
|
|
|
|
|
|
|
if (A > B)
|
|
|
|
std::swap(A, B);
|
|
|
|
if (TopDown) {
|
|
|
|
// Choose the side which is further along in the sequence.
|
|
|
|
if ((A == S_Retain || A == S_CanRelease) &&
|
|
|
|
(B == S_CanRelease || B == S_Use))
|
|
|
|
return B;
|
|
|
|
} else {
|
|
|
|
// Choose the side which is further along in the sequence.
|
|
|
|
if ((A == S_Use || A == S_CanRelease) &&
|
|
|
|
(B == S_Use || B == S_Release || B == S_Stop || B == S_MovableRelease))
|
|
|
|
return A;
|
|
|
|
// If both sides are releases, choose the more conservative one.
|
|
|
|
if (A == S_Stop && (B == S_Release || B == S_MovableRelease))
|
|
|
|
return A;
|
|
|
|
if (A == S_Release && B == S_MovableRelease)
|
|
|
|
return A;
|
|
|
|
}
|
|
|
|
|
|
|
|
return S_None;
|
|
|
|
}
|
|
|
|
|
2015-03-06 02:07:12 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// RRInfo
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2015-03-05 23:29:03 +00:00
|
|
|
void RRInfo::clear() {
|
|
|
|
KnownSafe = false;
|
|
|
|
IsTailCallRelease = false;
|
|
|
|
ReleaseMetadata = nullptr;
|
|
|
|
Calls.clear();
|
|
|
|
ReverseInsertPts.clear();
|
|
|
|
CFGHazardAfflicted = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RRInfo::Merge(const RRInfo &Other) {
|
|
|
|
// Conservatively merge the ReleaseMetadata information.
|
|
|
|
if (ReleaseMetadata != Other.ReleaseMetadata)
|
|
|
|
ReleaseMetadata = nullptr;
|
|
|
|
|
|
|
|
// Conservatively merge the boolean state.
|
|
|
|
KnownSafe &= Other.KnownSafe;
|
|
|
|
IsTailCallRelease &= Other.IsTailCallRelease;
|
|
|
|
CFGHazardAfflicted |= Other.CFGHazardAfflicted;
|
|
|
|
|
|
|
|
// Merge the call sets.
|
|
|
|
Calls.insert(Other.Calls.begin(), Other.Calls.end());
|
|
|
|
|
|
|
|
// Merge the insert point sets. If there are any differences,
|
|
|
|
// that makes this a partial merge.
|
|
|
|
bool Partial = ReverseInsertPts.size() != Other.ReverseInsertPts.size();
|
|
|
|
for (Instruction *Inst : Other.ReverseInsertPts)
|
|
|
|
Partial |= ReverseInsertPts.insert(Inst).second;
|
|
|
|
return Partial;
|
|
|
|
}
|
|
|
|
|
2015-03-06 02:07:12 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// PtrState
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2015-03-05 23:57:07 +00:00
|
|
|
void PtrState::SetKnownPositiveRefCount() {
|
2015-03-16 07:02:39 +00:00
|
|
|
DEBUG(dbgs() << " Setting Known Positive.\n");
|
2015-03-05 23:57:07 +00:00
|
|
|
KnownPositiveRefCount = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PtrState::ClearKnownPositiveRefCount() {
|
2015-03-16 07:02:39 +00:00
|
|
|
DEBUG(dbgs() << " Clearing Known Positive.\n");
|
2015-03-05 23:57:07 +00:00
|
|
|
KnownPositiveRefCount = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PtrState::SetSeq(Sequence NewSeq) {
|
2015-03-16 07:02:39 +00:00
|
|
|
DEBUG(dbgs() << " Old: " << GetSeq() << "; New: " << NewSeq << "\n");
|
2015-03-05 23:57:07 +00:00
|
|
|
Seq = NewSeq;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PtrState::ResetSequenceProgress(Sequence NewSeq) {
|
2015-03-16 07:02:39 +00:00
|
|
|
DEBUG(dbgs() << " Resetting sequence progress.\n");
|
2015-03-05 23:57:07 +00:00
|
|
|
SetSeq(NewSeq);
|
|
|
|
Partial = false;
|
|
|
|
RRI.clear();
|
|
|
|
}
|
|
|
|
|
2015-03-05 23:29:03 +00:00
|
|
|
void PtrState::Merge(const PtrState &Other, bool TopDown) {
|
|
|
|
Seq = MergeSeqs(GetSeq(), Other.GetSeq(), TopDown);
|
|
|
|
KnownPositiveRefCount &= Other.KnownPositiveRefCount;
|
|
|
|
|
|
|
|
// If we're not in a sequence (anymore), drop all associated state.
|
|
|
|
if (Seq == S_None) {
|
|
|
|
Partial = false;
|
|
|
|
RRI.clear();
|
|
|
|
} else if (Partial || Other.Partial) {
|
|
|
|
// If we're doing a merge on a path that's previously seen a partial
|
|
|
|
// merge, conservatively drop the sequence, to avoid doing partial
|
|
|
|
// RR elimination. If the branch predicates for the two merge differ,
|
|
|
|
// mixing them is unsafe.
|
|
|
|
ClearSequenceProgress();
|
|
|
|
} else {
|
|
|
|
// Otherwise merge the other PtrState's RRInfo into our RRInfo. At this
|
|
|
|
// point, we know that currently we are not partial. Stash whether or not
|
|
|
|
// the merge operation caused us to undergo a partial merging of reverse
|
|
|
|
// insertion points.
|
|
|
|
Partial = RRI.Merge(Other.RRI);
|
|
|
|
}
|
|
|
|
}
|
2015-03-06 00:34:39 +00:00
|
|
|
|
2015-03-06 02:07:12 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// BottomUpPtrState
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2015-03-06 00:34:39 +00:00
|
|
|
bool BottomUpPtrState::InitBottomUp(ARCMDKindCache &Cache, Instruction *I) {
|
|
|
|
// If we see two releases in a row on the same pointer. If so, make
|
|
|
|
// a note, and we'll cicle back to revisit it after we've
|
|
|
|
// hopefully eliminated the second release, which may allow us to
|
|
|
|
// eliminate the first release too.
|
|
|
|
// Theoretically we could implement removal of nested retain+release
|
|
|
|
// pairs by making PtrState hold a stack of states, but this is
|
|
|
|
// simple and avoids adding overhead for the non-nested case.
|
|
|
|
bool NestingDetected = false;
|
|
|
|
if (GetSeq() == S_Release || GetSeq() == S_MovableRelease) {
|
2015-03-16 07:02:39 +00:00
|
|
|
DEBUG(dbgs() << " Found nested releases (i.e. a release pair)\n");
|
2015-03-06 00:34:39 +00:00
|
|
|
NestingDetected = true;
|
|
|
|
}
|
|
|
|
|
2015-03-16 07:02:27 +00:00
|
|
|
MDNode *ReleaseMetadata =
|
|
|
|
I->getMetadata(Cache.get(ARCMDKindID::ImpreciseRelease));
|
2015-03-06 00:34:39 +00:00
|
|
|
Sequence NewSeq = ReleaseMetadata ? S_MovableRelease : S_Release;
|
|
|
|
ResetSequenceProgress(NewSeq);
|
|
|
|
SetReleaseMetadata(ReleaseMetadata);
|
|
|
|
SetKnownSafe(HasKnownPositiveRefCount());
|
|
|
|
SetTailCallRelease(cast<CallInst>(I)->isTailCall());
|
|
|
|
InsertCall(I);
|
|
|
|
SetKnownPositiveRefCount();
|
|
|
|
return NestingDetected;
|
|
|
|
}
|
|
|
|
|
2015-03-06 00:34:42 +00:00
|
|
|
bool BottomUpPtrState::MatchWithRetain() {
|
|
|
|
SetKnownPositiveRefCount();
|
|
|
|
|
|
|
|
Sequence OldSeq = GetSeq();
|
|
|
|
switch (OldSeq) {
|
|
|
|
case S_Stop:
|
|
|
|
case S_Release:
|
|
|
|
case S_MovableRelease:
|
|
|
|
case S_Use:
|
|
|
|
// If OldSeq is not S_Use or OldSeq is S_Use and we are tracking an
|
|
|
|
// imprecise release, clear our reverse insertion points.
|
|
|
|
if (OldSeq != S_Use || IsTrackingImpreciseReleases())
|
|
|
|
ClearReverseInsertPts();
|
|
|
|
// FALL THROUGH
|
|
|
|
case S_CanRelease:
|
|
|
|
return true;
|
|
|
|
case S_None:
|
|
|
|
return false;
|
|
|
|
case S_Retain:
|
|
|
|
llvm_unreachable("bottom-up pointer in retain state!");
|
|
|
|
}
|
2015-03-06 07:49:14 +00:00
|
|
|
llvm_unreachable("Sequence unknown enum value");
|
2015-03-06 00:34:42 +00:00
|
|
|
}
|
|
|
|
|
2015-03-06 02:07:12 +00:00
|
|
|
bool BottomUpPtrState::HandlePotentialAlterRefCount(Instruction *Inst,
|
|
|
|
const Value *Ptr,
|
|
|
|
ProvenanceAnalysis &PA,
|
|
|
|
ARCInstKind Class) {
|
2015-03-16 07:02:39 +00:00
|
|
|
Sequence S = GetSeq();
|
2015-03-06 02:07:12 +00:00
|
|
|
|
|
|
|
// Check for possible releases.
|
|
|
|
if (!CanAlterRefCount(Inst, Ptr, PA, Class))
|
|
|
|
return false;
|
|
|
|
|
2015-03-16 07:02:39 +00:00
|
|
|
DEBUG(dbgs() << " CanAlterRefCount: Seq: " << S << "; " << *Ptr
|
|
|
|
<< "\n");
|
|
|
|
switch (S) {
|
2015-03-06 02:07:12 +00:00
|
|
|
case S_Use:
|
|
|
|
SetSeq(S_CanRelease);
|
|
|
|
return true;
|
|
|
|
case S_CanRelease:
|
|
|
|
case S_Release:
|
|
|
|
case S_MovableRelease:
|
|
|
|
case S_Stop:
|
|
|
|
case S_None:
|
|
|
|
return false;
|
|
|
|
case S_Retain:
|
|
|
|
llvm_unreachable("bottom-up pointer in retain state!");
|
|
|
|
}
|
2015-03-06 07:49:14 +00:00
|
|
|
llvm_unreachable("Sequence unknown enum value");
|
2015-03-06 02:07:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void BottomUpPtrState::HandlePotentialUse(BasicBlock *BB, Instruction *Inst,
|
|
|
|
const Value *Ptr,
|
|
|
|
ProvenanceAnalysis &PA,
|
|
|
|
ARCInstKind Class) {
|
|
|
|
// Check for possible direct uses.
|
|
|
|
switch (GetSeq()) {
|
|
|
|
case S_Release:
|
|
|
|
case S_MovableRelease:
|
|
|
|
if (CanUse(Inst, Ptr, PA, Class)) {
|
2015-03-16 07:02:39 +00:00
|
|
|
DEBUG(dbgs() << " CanUse: Seq: " << GetSeq() << "; " << *Ptr
|
|
|
|
<< "\n");
|
2015-03-06 02:07:12 +00:00
|
|
|
assert(!HasReverseInsertPts());
|
|
|
|
// If this is an invoke instruction, we're scanning it as part of
|
|
|
|
// one of its successor blocks, since we can't insert code after it
|
|
|
|
// in its own block, and we don't want to split critical edges.
|
|
|
|
if (isa<InvokeInst>(Inst))
|
|
|
|
InsertReverseInsertPt(BB->getFirstInsertionPt());
|
|
|
|
else
|
|
|
|
InsertReverseInsertPt(std::next(BasicBlock::iterator(Inst)));
|
|
|
|
SetSeq(S_Use);
|
|
|
|
} else if (Seq == S_Release && IsUser(Class)) {
|
2015-03-16 07:02:39 +00:00
|
|
|
DEBUG(dbgs() << " PreciseReleaseUse: Seq: " << GetSeq() << "; "
|
|
|
|
<< *Ptr << "\n");
|
2015-03-06 02:07:12 +00:00
|
|
|
// Non-movable releases depend on any possible objc pointer use.
|
|
|
|
SetSeq(S_Stop);
|
|
|
|
assert(!HasReverseInsertPts());
|
|
|
|
// As above; handle invoke specially.
|
|
|
|
if (isa<InvokeInst>(Inst))
|
|
|
|
InsertReverseInsertPt(BB->getFirstInsertionPt());
|
|
|
|
else
|
|
|
|
InsertReverseInsertPt(std::next(BasicBlock::iterator(Inst)));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case S_Stop:
|
|
|
|
if (CanUse(Inst, Ptr, PA, Class)) {
|
2015-03-16 07:02:39 +00:00
|
|
|
DEBUG(dbgs() << " PreciseStopUse: Seq: " << GetSeq() << "; "
|
|
|
|
<< *Ptr << "\n");
|
2015-03-06 02:07:12 +00:00
|
|
|
SetSeq(S_Use);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case S_CanRelease:
|
|
|
|
case S_Use:
|
|
|
|
case S_None:
|
|
|
|
break;
|
|
|
|
case S_Retain:
|
|
|
|
llvm_unreachable("bottom-up pointer in retain state!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// TopDownPtrState
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2015-03-06 00:34:39 +00:00
|
|
|
bool TopDownPtrState::InitTopDown(ARCInstKind Kind, Instruction *I) {
|
|
|
|
bool NestingDetected = false;
|
|
|
|
// Don't do retain+release tracking for ARCInstKind::RetainRV, because
|
|
|
|
// it's
|
|
|
|
// better to let it remain as the first instruction after a call.
|
|
|
|
if (Kind != ARCInstKind::RetainRV) {
|
|
|
|
// If we see two retains in a row on the same pointer. If so, make
|
|
|
|
// a note, and we'll cicle back to revisit it after we've
|
|
|
|
// hopefully eliminated the second retain, which may allow us to
|
|
|
|
// eliminate the first retain too.
|
|
|
|
// Theoretically we could implement removal of nested retain+release
|
|
|
|
// pairs by making PtrState hold a stack of states, but this is
|
|
|
|
// simple and avoids adding overhead for the non-nested case.
|
|
|
|
if (GetSeq() == S_Retain)
|
|
|
|
NestingDetected = true;
|
|
|
|
|
|
|
|
ResetSequenceProgress(S_Retain);
|
|
|
|
SetKnownSafe(HasKnownPositiveRefCount());
|
|
|
|
InsertCall(I);
|
|
|
|
}
|
|
|
|
|
|
|
|
SetKnownPositiveRefCount();
|
|
|
|
return NestingDetected;
|
|
|
|
}
|
2015-03-06 00:34:42 +00:00
|
|
|
|
|
|
|
bool TopDownPtrState::MatchWithRelease(ARCMDKindCache &Cache,
|
|
|
|
Instruction *Release) {
|
|
|
|
ClearKnownPositiveRefCount();
|
|
|
|
|
|
|
|
Sequence OldSeq = GetSeq();
|
|
|
|
|
2015-03-16 07:02:27 +00:00
|
|
|
MDNode *ReleaseMetadata =
|
|
|
|
Release->getMetadata(Cache.get(ARCMDKindID::ImpreciseRelease));
|
2015-03-06 00:34:42 +00:00
|
|
|
|
|
|
|
switch (OldSeq) {
|
|
|
|
case S_Retain:
|
|
|
|
case S_CanRelease:
|
|
|
|
if (OldSeq == S_Retain || ReleaseMetadata != nullptr)
|
|
|
|
ClearReverseInsertPts();
|
|
|
|
// FALL THROUGH
|
|
|
|
case S_Use:
|
|
|
|
SetReleaseMetadata(ReleaseMetadata);
|
|
|
|
SetTailCallRelease(cast<CallInst>(Release)->isTailCall());
|
|
|
|
return true;
|
|
|
|
case S_None:
|
|
|
|
return false;
|
|
|
|
case S_Stop:
|
|
|
|
case S_Release:
|
|
|
|
case S_MovableRelease:
|
|
|
|
llvm_unreachable("top-down pointer in bottom up state!");
|
|
|
|
}
|
2015-03-06 07:49:14 +00:00
|
|
|
llvm_unreachable("Sequence unknown enum value");
|
2015-03-06 00:34:42 +00:00
|
|
|
}
|
2015-03-06 02:07:12 +00:00
|
|
|
|
|
|
|
bool TopDownPtrState::HandlePotentialAlterRefCount(Instruction *Inst,
|
|
|
|
const Value *Ptr,
|
|
|
|
ProvenanceAnalysis &PA,
|
|
|
|
ARCInstKind Class) {
|
|
|
|
// Check for possible releases.
|
|
|
|
if (!CanAlterRefCount(Inst, Ptr, PA, Class))
|
|
|
|
return false;
|
|
|
|
|
2015-03-16 07:02:39 +00:00
|
|
|
DEBUG(dbgs() << " CanAlterRefCount: Seq: " << GetSeq() << "; " << *Ptr
|
|
|
|
<< "\n");
|
2015-03-06 02:07:12 +00:00
|
|
|
ClearKnownPositiveRefCount();
|
2015-03-16 07:02:39 +00:00
|
|
|
switch (GetSeq()) {
|
2015-03-06 02:07:12 +00:00
|
|
|
case S_Retain:
|
|
|
|
SetSeq(S_CanRelease);
|
|
|
|
assert(!HasReverseInsertPts());
|
|
|
|
InsertReverseInsertPt(Inst);
|
|
|
|
|
|
|
|
// One call can't cause a transition from S_Retain to S_CanRelease
|
|
|
|
// and S_CanRelease to S_Use. If we've made the first transition,
|
|
|
|
// we're done.
|
|
|
|
return true;
|
|
|
|
case S_Use:
|
|
|
|
case S_CanRelease:
|
|
|
|
case S_None:
|
|
|
|
return false;
|
|
|
|
case S_Stop:
|
|
|
|
case S_Release:
|
|
|
|
case S_MovableRelease:
|
|
|
|
llvm_unreachable("top-down pointer in release state!");
|
|
|
|
}
|
|
|
|
llvm_unreachable("covered switch is not covered!?");
|
|
|
|
}
|
|
|
|
|
|
|
|
void TopDownPtrState::HandlePotentialUse(Instruction *Inst, const Value *Ptr,
|
|
|
|
ProvenanceAnalysis &PA,
|
|
|
|
ARCInstKind Class) {
|
|
|
|
// Check for possible direct uses.
|
|
|
|
switch (GetSeq()) {
|
|
|
|
case S_CanRelease:
|
|
|
|
if (!CanUse(Inst, Ptr, PA, Class))
|
|
|
|
return;
|
2015-03-16 07:02:39 +00:00
|
|
|
DEBUG(dbgs() << " CanUse: Seq: " << GetSeq() << "; " << *Ptr
|
|
|
|
<< "\n");
|
2015-03-06 02:07:12 +00:00
|
|
|
SetSeq(S_Use);
|
|
|
|
return;
|
|
|
|
case S_Retain:
|
|
|
|
case S_Use:
|
|
|
|
case S_None:
|
|
|
|
return;
|
|
|
|
case S_Stop:
|
|
|
|
case S_Release:
|
|
|
|
case S_MovableRelease:
|
|
|
|
llvm_unreachable("top-down pointer in release state!");
|
|
|
|
}
|
|
|
|
}
|