2013-01-29 11:03:03 +08:00
|
|
|
//===- DependencyAnalysis.cpp - ObjC ARC Optimization ---------------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2013-01-29 11:03:03 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
/// \file
|
|
|
|
///
|
|
|
|
/// This file defines special dependency analysis routines used in Objective C
|
|
|
|
/// ARC Optimizations.
|
|
|
|
///
|
|
|
|
/// WARNING: This file knows about certain library functions. It recognizes them
|
|
|
|
/// by name, and hardwires knowledge of their semantics.
|
|
|
|
///
|
|
|
|
/// WARNING: This file knows about how certain Objective-C library functions are
|
|
|
|
/// used. Naive LLVM IR transformations which would otherwise be
|
|
|
|
/// behavior-preserving may break these assumptions.
|
|
|
|
///
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "DependencyAnalysis.h"
|
2017-06-06 19:49:48 +08:00
|
|
|
#include "ObjCARC.h"
|
2013-01-29 12:20:52 +08:00
|
|
|
#include "ProvenanceAnalysis.h"
|
2014-03-04 19:45:46 +08:00
|
|
|
#include "llvm/IR/CFG.h"
|
2013-01-29 11:03:03 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace llvm::objcarc;
|
|
|
|
|
2014-04-22 10:55:47 +08:00
|
|
|
#define DEBUG_TYPE "objc-arc-dependency"
|
|
|
|
|
2013-01-29 11:03:03 +08:00
|
|
|
/// Test whether the given instruction can result in a reference count
|
|
|
|
/// modification (positive or negative) for the pointer's object.
|
2015-02-20 03:51:32 +08:00
|
|
|
bool llvm::objcarc::CanAlterRefCount(const Instruction *Inst, const Value *Ptr,
|
|
|
|
ProvenanceAnalysis &PA,
|
|
|
|
ARCInstKind Class) {
|
2013-01-29 11:03:03 +08:00
|
|
|
switch (Class) {
|
2015-02-20 03:51:32 +08:00
|
|
|
case ARCInstKind::Autorelease:
|
|
|
|
case ARCInstKind::AutoreleaseRV:
|
|
|
|
case ARCInstKind::IntrinsicUser:
|
|
|
|
case ARCInstKind::User:
|
2013-01-29 11:03:03 +08:00
|
|
|
// These operations never directly modify a reference count.
|
|
|
|
return false;
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
|
2019-01-07 13:42:51 +08:00
|
|
|
const auto *Call = cast<CallBase>(Inst);
|
2013-01-29 11:03:03 +08:00
|
|
|
|
|
|
|
// See if AliasAnalysis can help us with the call.
|
2019-01-07 13:42:51 +08:00
|
|
|
FunctionModRefBehavior MRB = PA.getAA()->getModRefBehavior(Call);
|
2013-01-29 11:03:03 +08:00
|
|
|
if (AliasAnalysis::onlyReadsMemory(MRB))
|
|
|
|
return false;
|
|
|
|
if (AliasAnalysis::onlyAccessesArgPointees(MRB)) {
|
2015-03-10 10:37:25 +08:00
|
|
|
const DataLayout &DL = Inst->getModule()->getDataLayout();
|
2019-01-07 13:42:51 +08:00
|
|
|
for (const Value *Op : Call->args()) {
|
2015-03-10 10:37:25 +08:00
|
|
|
if (IsPotentialRetainableObjPtr(Op, *PA.getAA()) &&
|
|
|
|
PA.related(Ptr, Op, DL))
|
2013-01-29 11:03:03 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assume the worst.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-02-20 08:02:45 +08:00
|
|
|
bool llvm::objcarc::CanDecrementRefCount(const Instruction *Inst,
|
|
|
|
const Value *Ptr,
|
|
|
|
ProvenanceAnalysis &PA,
|
|
|
|
ARCInstKind Class) {
|
|
|
|
// First perform a quick check if Class can not touch ref counts.
|
|
|
|
if (!CanDecrementRefCount(Class))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Otherwise, just use CanAlterRefCount for now.
|
|
|
|
return CanAlterRefCount(Inst, Ptr, PA, Class);
|
|
|
|
}
|
|
|
|
|
2013-01-29 11:03:03 +08:00
|
|
|
/// Test whether the given instruction can "use" the given pointer's object in a
|
|
|
|
/// way that requires the reference count to be positive.
|
2015-02-20 03:51:32 +08:00
|
|
|
bool llvm::objcarc::CanUse(const Instruction *Inst, const Value *Ptr,
|
|
|
|
ProvenanceAnalysis &PA, ARCInstKind Class) {
|
|
|
|
// ARCInstKind::Call operations (as opposed to
|
|
|
|
// ARCInstKind::CallOrUser) never "use" objc pointers.
|
|
|
|
if (Class == ARCInstKind::Call)
|
2013-01-29 11:03:03 +08:00
|
|
|
return false;
|
|
|
|
|
2015-03-10 10:37:25 +08:00
|
|
|
const DataLayout &DL = Inst->getModule()->getDataLayout();
|
|
|
|
|
2013-01-29 11:03:03 +08:00
|
|
|
// Consider various instructions which may have pointer arguments which are
|
|
|
|
// not "uses".
|
|
|
|
if (const ICmpInst *ICI = dyn_cast<ICmpInst>(Inst)) {
|
|
|
|
// Comparing a pointer with null, or any other constant, isn't really a use,
|
|
|
|
// because we don't care what the pointer points to, or about the values
|
|
|
|
// of any other dynamic reference-counted pointers.
|
|
|
|
if (!IsPotentialRetainableObjPtr(ICI->getOperand(1), *PA.getAA()))
|
|
|
|
return false;
|
2015-04-10 22:50:08 +08:00
|
|
|
} else if (auto CS = ImmutableCallSite(Inst)) {
|
2013-01-29 11:03:03 +08:00
|
|
|
// For calls, just check the arguments (and not the callee operand).
|
|
|
|
for (ImmutableCallSite::arg_iterator OI = CS.arg_begin(),
|
|
|
|
OE = CS.arg_end(); OI != OE; ++OI) {
|
|
|
|
const Value *Op = *OI;
|
2015-03-10 10:37:25 +08:00
|
|
|
if (IsPotentialRetainableObjPtr(Op, *PA.getAA()) &&
|
|
|
|
PA.related(Ptr, Op, DL))
|
2013-01-29 11:03:03 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
} else if (const StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
|
|
|
|
// Special-case stores, because we don't care about the stored value, just
|
|
|
|
// the store address.
|
2015-03-10 10:37:25 +08:00
|
|
|
const Value *Op = GetUnderlyingObjCPtr(SI->getPointerOperand(), DL);
|
2013-01-29 11:03:03 +08:00
|
|
|
// If we can't tell what the underlying object was, assume there is a
|
|
|
|
// dependence.
|
2015-03-10 10:37:25 +08:00
|
|
|
return IsPotentialRetainableObjPtr(Op, *PA.getAA()) &&
|
|
|
|
PA.related(Op, Ptr, DL);
|
2013-01-29 11:03:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check each operand for a match.
|
|
|
|
for (User::const_op_iterator OI = Inst->op_begin(), OE = Inst->op_end();
|
|
|
|
OI != OE; ++OI) {
|
|
|
|
const Value *Op = *OI;
|
2015-03-10 10:37:25 +08:00
|
|
|
if (IsPotentialRetainableObjPtr(Op, *PA.getAA()) && PA.related(Ptr, Op, DL))
|
2013-01-29 11:03:03 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Test if there can be dependencies on Inst through Arg. This function only
|
|
|
|
/// tests dependencies relevant for removing pairs of calls.
|
|
|
|
bool
|
|
|
|
llvm::objcarc::Depends(DependenceKind Flavor, Instruction *Inst,
|
|
|
|
const Value *Arg, ProvenanceAnalysis &PA) {
|
|
|
|
// If we've reached the definition of Arg, stop.
|
|
|
|
if (Inst == Arg)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
switch (Flavor) {
|
|
|
|
case NeedsPositiveRetainCount: {
|
2015-02-20 03:51:32 +08:00
|
|
|
ARCInstKind Class = GetARCInstKind(Inst);
|
2013-01-29 11:03:03 +08:00
|
|
|
switch (Class) {
|
2015-02-20 03:51:32 +08:00
|
|
|
case ARCInstKind::AutoreleasepoolPop:
|
|
|
|
case ARCInstKind::AutoreleasepoolPush:
|
|
|
|
case ARCInstKind::None:
|
2013-01-29 11:03:03 +08:00
|
|
|
return false;
|
|
|
|
default:
|
|
|
|
return CanUse(Inst, Arg, PA, Class);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
case AutoreleasePoolBoundary: {
|
2015-02-20 03:51:32 +08:00
|
|
|
ARCInstKind Class = GetARCInstKind(Inst);
|
2013-01-29 11:03:03 +08:00
|
|
|
switch (Class) {
|
2015-02-20 03:51:32 +08:00
|
|
|
case ARCInstKind::AutoreleasepoolPop:
|
|
|
|
case ARCInstKind::AutoreleasepoolPush:
|
2013-01-29 11:03:03 +08:00
|
|
|
// These mark the end and begin of an autorelease pool scope.
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
// Nothing else does this.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
case CanChangeRetainCount: {
|
2015-02-20 03:51:32 +08:00
|
|
|
ARCInstKind Class = GetARCInstKind(Inst);
|
2013-01-29 11:03:03 +08:00
|
|
|
switch (Class) {
|
2015-02-20 03:51:32 +08:00
|
|
|
case ARCInstKind::AutoreleasepoolPop:
|
2013-01-29 11:03:03 +08:00
|
|
|
// Conservatively assume this can decrement any count.
|
|
|
|
return true;
|
2015-02-20 03:51:32 +08:00
|
|
|
case ARCInstKind::AutoreleasepoolPush:
|
|
|
|
case ARCInstKind::None:
|
2013-01-29 11:03:03 +08:00
|
|
|
return false;
|
|
|
|
default:
|
|
|
|
return CanAlterRefCount(Inst, Arg, PA, Class);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
case RetainAutoreleaseDep:
|
2015-02-20 03:51:32 +08:00
|
|
|
switch (GetBasicARCInstKind(Inst)) {
|
|
|
|
case ARCInstKind::AutoreleasepoolPop:
|
|
|
|
case ARCInstKind::AutoreleasepoolPush:
|
2013-01-29 11:03:03 +08:00
|
|
|
// Don't merge an objc_autorelease with an objc_retain inside a different
|
|
|
|
// autoreleasepool scope.
|
|
|
|
return true;
|
2015-02-20 03:51:32 +08:00
|
|
|
case ARCInstKind::Retain:
|
|
|
|
case ARCInstKind::RetainRV:
|
2013-01-29 11:03:03 +08:00
|
|
|
// Check for a retain of the same pointer for merging.
|
2015-02-19 08:42:38 +08:00
|
|
|
return GetArgRCIdentityRoot(Inst) == Arg;
|
2013-01-29 11:03:03 +08:00
|
|
|
default:
|
|
|
|
// Nothing else matters for objc_retainAutorelease formation.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
case RetainAutoreleaseRVDep: {
|
2015-02-20 03:51:32 +08:00
|
|
|
ARCInstKind Class = GetBasicARCInstKind(Inst);
|
2013-01-29 11:03:03 +08:00
|
|
|
switch (Class) {
|
2015-02-20 03:51:32 +08:00
|
|
|
case ARCInstKind::Retain:
|
|
|
|
case ARCInstKind::RetainRV:
|
2013-01-29 11:03:03 +08:00
|
|
|
// Check for a retain of the same pointer for merging.
|
2015-02-19 08:42:38 +08:00
|
|
|
return GetArgRCIdentityRoot(Inst) == Arg;
|
2013-01-29 11:03:03 +08:00
|
|
|
default:
|
|
|
|
// Anything that can autorelease interrupts
|
|
|
|
// retainAutoreleaseReturnValue formation.
|
|
|
|
return CanInterruptRV(Class);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
case RetainRVDep:
|
2015-02-20 03:51:32 +08:00
|
|
|
return CanInterruptRV(GetBasicARCInstKind(Inst));
|
2013-01-29 11:03:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
llvm_unreachable("Invalid dependence flavor");
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Walk up the CFG from StartPos (which is in StartBB) and find local and
|
|
|
|
/// non-local dependencies on Arg.
|
|
|
|
///
|
|
|
|
/// TODO: Cache results?
|
|
|
|
void
|
|
|
|
llvm::objcarc::FindDependencies(DependenceKind Flavor,
|
|
|
|
const Value *Arg,
|
|
|
|
BasicBlock *StartBB, Instruction *StartInst,
|
2014-08-21 13:55:13 +08:00
|
|
|
SmallPtrSetImpl<Instruction *> &DependingInsts,
|
|
|
|
SmallPtrSetImpl<const BasicBlock *> &Visited,
|
2013-01-29 11:03:03 +08:00
|
|
|
ProvenanceAnalysis &PA) {
|
2015-10-20 07:20:14 +08:00
|
|
|
BasicBlock::iterator StartPos = StartInst->getIterator();
|
2013-01-29 11:03:03 +08:00
|
|
|
|
|
|
|
SmallVector<std::pair<BasicBlock *, BasicBlock::iterator>, 4> Worklist;
|
|
|
|
Worklist.push_back(std::make_pair(StartBB, StartPos));
|
|
|
|
do {
|
|
|
|
std::pair<BasicBlock *, BasicBlock::iterator> Pair =
|
|
|
|
Worklist.pop_back_val();
|
|
|
|
BasicBlock *LocalStartBB = Pair.first;
|
|
|
|
BasicBlock::iterator LocalStartPos = Pair.second;
|
|
|
|
BasicBlock::iterator StartBBBegin = LocalStartBB->begin();
|
|
|
|
for (;;) {
|
|
|
|
if (LocalStartPos == StartBBBegin) {
|
|
|
|
pred_iterator PI(LocalStartBB), PE(LocalStartBB, false);
|
|
|
|
if (PI == PE)
|
|
|
|
// If we've reached the function entry, produce a null dependence.
|
2014-04-25 13:29:35 +08:00
|
|
|
DependingInsts.insert(nullptr);
|
2013-01-29 11:03:03 +08:00
|
|
|
else
|
|
|
|
// Add the predecessors to the worklist.
|
|
|
|
do {
|
|
|
|
BasicBlock *PredBB = *PI;
|
2014-11-19 15:49:26 +08:00
|
|
|
if (Visited.insert(PredBB).second)
|
2013-01-29 11:03:03 +08:00
|
|
|
Worklist.push_back(std::make_pair(PredBB, PredBB->end()));
|
|
|
|
} while (++PI != PE);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-10-20 07:20:14 +08:00
|
|
|
Instruction *Inst = &*--LocalStartPos;
|
2013-01-29 11:03:03 +08:00
|
|
|
if (Depends(Flavor, Inst, Arg, PA)) {
|
|
|
|
DependingInsts.insert(Inst);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (!Worklist.empty());
|
|
|
|
|
|
|
|
// Determine whether the original StartBB post-dominates all of the blocks we
|
|
|
|
// visited. If not, insert a sentinal indicating that most optimizations are
|
|
|
|
// not safe.
|
2014-08-25 07:23:06 +08:00
|
|
|
for (const BasicBlock *BB : Visited) {
|
2013-01-29 11:03:03 +08:00
|
|
|
if (BB == StartBB)
|
|
|
|
continue;
|
2018-10-18 08:38:34 +08:00
|
|
|
for (const BasicBlock *Succ : successors(BB))
|
2013-01-29 11:03:03 +08:00
|
|
|
if (Succ != StartBB && !Visited.count(Succ)) {
|
|
|
|
DependingInsts.insert(reinterpret_cast<Instruction *>(-1));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|