#include "DependencyAnalysis.h"
#include "ObjCARC.h"
#include "ProvenanceAnalysis.h"
#include "llvm/Analysis/AliasAnalysis.h"
#include "llvm/IR/CFG.h"
using namespace llvm;
using namespace llvm::objcarc;
#define DEBUG_TYPE "objc-arc-dependency"
bool llvm::objcarc::CanAlterRefCount(const Instruction *Inst, const Value *Ptr,
ProvenanceAnalysis &PA,
ARCInstKind Class) {
switch (Class) {
case ARCInstKind::Autorelease:
case ARCInstKind::AutoreleaseRV:
case ARCInstKind::IntrinsicUser:
case ARCInstKind::User:
return false;
default: break;
}
const auto *Call = cast<CallBase>(Inst);
FunctionModRefBehavior MRB = PA.getAA()->getModRefBehavior(Call);
if (AliasAnalysis::onlyReadsMemory(MRB))
return false;
if (AliasAnalysis::onlyAccessesArgPointees(MRB)) {
for (const Value *Op : Call->args()) {
if (IsPotentialRetainableObjPtr(Op, *PA.getAA()) && PA.related(Ptr, Op))
return true;
}
return false;
}
return true;
}
bool llvm::objcarc::CanDecrementRefCount(const Instruction *Inst,
const Value *Ptr,
ProvenanceAnalysis &PA,
ARCInstKind Class) {
if (!CanDecrementRefCount(Class))
return false;
return CanAlterRefCount(Inst, Ptr, PA, Class);
}
bool llvm::objcarc::CanUse(const Instruction *Inst, const Value *Ptr,
ProvenanceAnalysis &PA, ARCInstKind Class) {
if (Class == ARCInstKind::Call)
return false;
if (const ICmpInst *ICI = dyn_cast<ICmpInst>(Inst)) {
if (!IsPotentialRetainableObjPtr(ICI->getOperand(1), *PA.getAA()))
return false;
} else if (const auto *CS = dyn_cast<CallBase>(Inst)) {
for (const Value *Op : CS->args())
if (IsPotentialRetainableObjPtr(Op, *PA.getAA()) && PA.related(Ptr, Op))
return true;
return false;
} else if (const StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
const Value *Op = GetUnderlyingObjCPtr(SI->getPointerOperand());
return IsPotentialRetainableObjPtr(Op, *PA.getAA()) && PA.related(Op, Ptr);
}
for (const Use &U : Inst->operands()) {
const Value *Op = U;
if (IsPotentialRetainableObjPtr(Op, *PA.getAA()) && PA.related(Ptr, Op))
return true;
}
return false;
}
bool
llvm::objcarc::Depends(DependenceKind Flavor, Instruction *Inst,
const Value *Arg, ProvenanceAnalysis &PA) {
if (Inst == Arg)
return true;
switch (Flavor) {
case NeedsPositiveRetainCount: {
ARCInstKind Class = GetARCInstKind(Inst);
switch (Class) {
case ARCInstKind::AutoreleasepoolPop:
case ARCInstKind::AutoreleasepoolPush:
case ARCInstKind::None:
return false;
default:
return CanUse(Inst, Arg, PA, Class);
}
}
case AutoreleasePoolBoundary: {
ARCInstKind Class = GetARCInstKind(Inst);
switch (Class) {
case ARCInstKind::AutoreleasepoolPop:
case ARCInstKind::AutoreleasepoolPush:
return true;
default:
return false;
}
}
case CanChangeRetainCount: {
ARCInstKind Class = GetARCInstKind(Inst);
switch (Class) {
case ARCInstKind::AutoreleasepoolPop:
return true;
case ARCInstKind::AutoreleasepoolPush:
case ARCInstKind::None:
return false;
default:
return CanAlterRefCount(Inst, Arg, PA, Class);
}
}
case RetainAutoreleaseDep:
switch (GetBasicARCInstKind(Inst)) {
case ARCInstKind::AutoreleasepoolPop:
case ARCInstKind::AutoreleasepoolPush:
return true;
case ARCInstKind::Retain:
case ARCInstKind::RetainRV:
return GetArgRCIdentityRoot(Inst) == Arg;
default:
return false;
}
case RetainAutoreleaseRVDep: {
ARCInstKind Class = GetBasicARCInstKind(Inst);
switch (Class) {
case ARCInstKind::Retain:
case ARCInstKind::RetainRV:
return GetArgRCIdentityRoot(Inst) == Arg;
default:
return CanInterruptRV(Class);
}
}
}
llvm_unreachable("Invalid dependence flavor");
}
static bool findDependencies(DependenceKind Flavor, const Value *Arg,
BasicBlock *StartBB, Instruction *StartInst,
SmallPtrSetImpl<Instruction *> &DependingInsts,
ProvenanceAnalysis &PA) {
BasicBlock::iterator StartPos = StartInst->getIterator();
SmallPtrSet<const BasicBlock *, 4> Visited;
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)
return false;
do {
BasicBlock *PredBB = *PI;
if (Visited.insert(PredBB).second)
Worklist.push_back(std::make_pair(PredBB, PredBB->end()));
} while (++PI != PE);
break;
}
Instruction *Inst = &*--LocalStartPos;
if (Depends(Flavor, Inst, Arg, PA)) {
DependingInsts.insert(Inst);
break;
}
}
} while (!Worklist.empty());
for (const BasicBlock *BB : Visited) {
if (BB == StartBB)
continue;
for (const BasicBlock *Succ : successors(BB))
if (Succ != StartBB && !Visited.count(Succ))
return false;
}
return true;
}
llvm::Instruction *llvm::objcarc::findSingleDependency(DependenceKind Flavor,
const Value *Arg,
BasicBlock *StartBB,
Instruction *StartInst,
ProvenanceAnalysis &PA) {
SmallPtrSet<Instruction *, 4> DependingInsts;
if (!findDependencies(Flavor, Arg, StartBB, StartInst, DependingInsts, PA) ||
DependingInsts.size() != 1)
return nullptr;
return *DependingInsts.begin();
}