summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorDan Gohman <gohman@apple.com>2011-10-17 22:53:25 +0000
committerDan Gohman <gohman@apple.com>2011-10-17 22:53:25 +0000
commita974beaa1f63394a67c38c66ff0f39a759c7998f (patch)
tree4e9f7d7952796d1c49af0fd679f0108320d0bb44 /lib
parentfa1ee880520d8da1168278c65575241487f871df (diff)
downloadllvm-a974beaa1f63394a67c38c66ff0f39a759c7998f.tar.gz
llvm-a974beaa1f63394a67c38c66ff0f39a759c7998f.tar.bz2
llvm-a974beaa1f63394a67c38c66ff0f39a759c7998f.tar.xz
Teach the ARC optimizer about the !clang.arc.copy_on_escape metadata
tag on objc_retainBlock calls, which indicates that they may be optimized away. rdar://10211286. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@142298 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib')
-rw-r--r--lib/Transforms/Scalar/ObjCARC.cpp52
1 files changed, 40 insertions, 12 deletions
diff --git a/lib/Transforms/Scalar/ObjCARC.cpp b/lib/Transforms/Scalar/ObjCARC.cpp
index 4e068d51c8..80f5f014c8 100644
--- a/lib/Transforms/Scalar/ObjCARC.cpp
+++ b/lib/Transforms/Scalar/ObjCARC.cpp
@@ -1154,6 +1154,10 @@ namespace {
/// opposed to objc_retain calls).
bool IsRetainBlock;
+ /// CopyOnEscape - True if this the Calls are objc_retainBlock calls
+ /// which all have the !clang.arc.copy_on_escape metadata.
+ bool CopyOnEscape;
+
/// IsTailCallRelease - True of the objc_release calls are all marked
/// with the "tail" keyword.
bool IsTailCallRelease;
@@ -1176,8 +1180,8 @@ namespace {
SmallPtrSet<Instruction *, 2> ReverseInsertPts;
RRInfo() :
- KnownSafe(false), IsRetainBlock(false), IsTailCallRelease(false),
- Partial(false),
+ KnownSafe(false), IsRetainBlock(false), CopyOnEscape(false),
+ IsTailCallRelease(false), Partial(false),
ReleaseMetadata(0) {}
void clear();
@@ -1187,6 +1191,7 @@ namespace {
void RRInfo::clear() {
KnownSafe = false;
IsRetainBlock = false;
+ CopyOnEscape = false;
IsTailCallRelease = false;
Partial = false;
ReleaseMetadata = 0;
@@ -1294,6 +1299,7 @@ PtrState::Merge(const PtrState &Other, bool TopDown) {
if (RRI.ReleaseMetadata != Other.RRI.ReleaseMetadata)
RRI.ReleaseMetadata = 0;
+ RRI.CopyOnEscape = RRI.CopyOnEscape && Other.RRI.CopyOnEscape;
RRI.KnownSafe = RRI.KnownSafe && Other.RRI.KnownSafe;
RRI.IsTailCallRelease = RRI.IsTailCallRelease && Other.RRI.IsTailCallRelease;
RRI.Calls.insert(Other.RRI.Calls.begin(), Other.RRI.Calls.end());
@@ -1482,6 +1488,10 @@ namespace {
/// metadata.
unsigned ImpreciseReleaseMDKind;
+ /// CopyOnEscape - The Metadata Kind for clang.arc.copy_on_escape
+ /// metadata.
+ unsigned CopyOnEscapeMDKind;
+
Constant *getRetainRVCallee(Module *M);
Constant *getAutoreleaseRVCallee(Module *M);
Constant *getReleaseCallee(Module *M);
@@ -2360,9 +2370,12 @@ ObjCARCOpt::VisitBottomUp(BasicBlock *BB,
S.SetAtLeastOneRefCount();
S.DecrementNestCount();
- // An objc_retainBlock call with just a use still needs to be kept,
- // because it may be copying a block from the stack to the heap.
- if (Class == IC_RetainBlock && S.GetSeq() == S_Use)
+ // An non-copy-on-escape objc_retainBlock call with just a use still
+ // needs to be kept, because it may be copying a block from the stack
+ // to the heap.
+ if (Class == IC_RetainBlock &&
+ !Inst->getMetadata(CopyOnEscapeMDKind) &&
+ S.GetSeq() == S_Use)
S.SetSeq(S_CanRelease);
switch (S.GetSeq()) {
@@ -2377,6 +2390,8 @@ ObjCARCOpt::VisitBottomUp(BasicBlock *BB,
// better to let it remain as the first instruction after a call.
if (Class != IC_RetainRV) {
S.RRI.IsRetainBlock = Class == IC_RetainBlock;
+ if (S.RRI.IsRetainBlock)
+ S.RRI.CopyOnEscape = !!Inst->getMetadata(CopyOnEscapeMDKind);
Retains[Inst] = S.RRI;
}
S.ClearSequenceProgress();
@@ -2527,6 +2542,8 @@ ObjCARCOpt::VisitTopDown(BasicBlock *BB,
S.SetSeq(S_Retain);
S.RRI.clear();
S.RRI.IsRetainBlock = Class == IC_RetainBlock;
+ if (S.RRI.IsRetainBlock)
+ S.RRI.CopyOnEscape = !!Inst->getMetadata(CopyOnEscapeMDKind);
// Don't check S.IsKnownIncremented() here because it's not
// sufficient.
S.RRI.KnownSafe = S.IsKnownNested();
@@ -2618,10 +2635,11 @@ ObjCARCOpt::VisitTopDown(BasicBlock *BB,
S.SetSeq(S_Use);
break;
case S_Retain:
- // An objc_retainBlock call may be responsible for copying the block
- // data from the stack to the heap. Model this by moving it straight
- // from S_Retain to S_Use.
+ // A non-copy-on-scape objc_retainBlock call may be responsible for
+ // copying the block data from the stack to the heap. Model this by
+ // moving it straight from S_Retain to S_Use.
if (S.RRI.IsRetainBlock &&
+ !S.RRI.CopyOnEscape &&
CanUse(Inst, Ptr, PA, Class)) {
assert(S.RRI.ReverseInsertPts.empty());
S.RRI.ReverseInsertPts.insert(Inst);
@@ -2713,6 +2731,9 @@ void ObjCARCOpt::MoveCalls(Value *Arg,
getRetainBlockCallee(M) : getRetainCallee(M),
MyArg, "", InsertPt);
Call->setDoesNotThrow();
+ if (RetainsToMove.CopyOnEscape)
+ Call->setMetadata(CopyOnEscapeMDKind,
+ MDNode::get(M->getContext(), ArrayRef<Value *>()));
if (!RetainsToMove.IsRetainBlock)
Call->setTailCall();
}
@@ -2792,10 +2813,11 @@ ObjCARCOpt::PerformCodePlacement(DenseMap<const BasicBlock *, BBState>
// regardless of what possible decrements or uses lie between them.
bool KnownSafe = isa<Constant>(Arg);
- // Same for stack storage, unless this is an objc_retainBlock call,
- // which is responsible for copying the block data from the stack to
- // the heap.
- if (!I->second.IsRetainBlock && isa<AllocaInst>(Arg))
+ // Same for stack storage, unless this is a non-copy-on-escape
+ // objc_retainBlock call, which is responsible for copying the block data
+ // from the stack to the heap.
+ if ((!I->second.IsRetainBlock || I->second.CopyOnEscape) &&
+ isa<AllocaInst>(Arg))
KnownSafe = true;
// A constant pointer can't be pointing to an object on the heap. It may
@@ -2905,6 +2927,7 @@ ObjCARCOpt::PerformCodePlacement(DenseMap<const BasicBlock *, BBState>
// Merge the IsRetainBlock values.
if (FirstRetain) {
RetainsToMove.IsRetainBlock = NewReleaseRetainRRI.IsRetainBlock;
+ RetainsToMove.CopyOnEscape = NewReleaseRetainRRI.CopyOnEscape;
FirstRetain = false;
} else if (ReleasesToMove.IsRetainBlock !=
NewReleaseRetainRRI.IsRetainBlock)
@@ -2912,6 +2935,9 @@ ObjCARCOpt::PerformCodePlacement(DenseMap<const BasicBlock *, BBState>
// objc_retain and the other uses objc_retainBlock.
goto next_retain;
+ // Merge the CopyOnEscape values.
+ RetainsToMove.CopyOnEscape &= NewReleaseRetainRRI.CopyOnEscape;
+
// Collect the optimal insertion points.
if (!KnownSafe)
for (SmallPtrSet<Instruction *, 2>::const_iterator
@@ -3265,6 +3291,8 @@ bool ObjCARCOpt::doInitialization(Module &M) {
// Identify the imprecise release metadata kind.
ImpreciseReleaseMDKind =
M.getContext().getMDKindID("clang.imprecise_release");
+ CopyOnEscapeMDKind =
+ M.getContext().getMDKindID("clang.arc.copy_on_escape");
// Intuitively, objc_retain and others are nocapture, however in practice
// they are not, because they return their argument value. And objc_release