#include "ARCRuntimeEntryPoints.h"
#include "DependencyAnalysis.h"
#include "ObjCARC.h"
#include "ProvenanceAnalysis.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/Analysis/AliasAnalysis.h"
#include "llvm/Analysis/EHPersonalities.h"
#include "llvm/Analysis/ObjCARCUtil.h"
#include "llvm/IR/Dominators.h"
#include "llvm/IR/InlineAsm.h"
#include "llvm/IR/InstIterator.h"
#include "llvm/IR/Operator.h"
#include "llvm/IR/PassManager.h"
#include "llvm/InitializePasses.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Transforms/ObjCARC.h"
using namespace llvm;
using namespace llvm::objcarc;
#define DEBUG_TYPE "objc-arc-contract"
STATISTIC(NumPeeps, "Number of calls peephole-optimized");
STATISTIC(NumStoreStrongs, "Number objc_storeStrong calls formed");
namespace {
class ObjCARCContract {
bool Changed;
bool CFGChanged;
AAResults *AA;
DominatorTree *DT;
ProvenanceAnalysis PA;
ARCRuntimeEntryPoints EP;
BundledRetainClaimRVs *BundledInsts = nullptr;
const MDString *RVInstMarker;
SmallPtrSet<CallInst *, 8> StoreStrongCalls;
bool tryToPeepholeInstruction(
Function &F, Instruction *Inst, inst_iterator &Iter,
bool &TailOkForStoreStrong,
const DenseMap<BasicBlock *, ColorVector> &BlockColors);
bool optimizeRetainCall(Function &F, Instruction *Retain);
bool contractAutorelease(Function &F, Instruction *Autorelease,
ARCInstKind Class);
void tryToContractReleaseIntoStoreStrong(
Instruction *Release, inst_iterator &Iter,
const DenseMap<BasicBlock *, ColorVector> &BlockColors);
public:
bool init(Module &M);
bool run(Function &F, AAResults *AA, DominatorTree *DT);
bool hasCFGChanged() const { return CFGChanged; }
};
class ObjCARCContractLegacyPass : public FunctionPass {
public:
void getAnalysisUsage(AnalysisUsage &AU) const override;
bool runOnFunction(Function &F) override;
static char ID;
ObjCARCContractLegacyPass() : FunctionPass(ID) {
initializeObjCARCContractLegacyPassPass(*PassRegistry::getPassRegistry());
}
};
}
bool ObjCARCContract::optimizeRetainCall(Function &F, Instruction *Retain) {
const auto *Call = dyn_cast<CallBase>(GetArgRCIdentityRoot(Retain));
if (!Call)
return false;
if (Call->getParent() != Retain->getParent())
return false;
BasicBlock::const_iterator I = ++Call->getIterator();
while (IsNoopInstruction(&*I))
++I;
if (&*I != Retain)
return false;
Changed = true;
++NumPeeps;
LLVM_DEBUG(
dbgs() << "Transforming objc_retain => "
"objc_retainAutoreleasedReturnValue since the operand is a "
"return value.\nOld: "
<< *Retain << "\n");
Function *Decl = EP.get(ARCRuntimeEntryPointKind::RetainRV);
cast<CallInst>(Retain)->setCalledFunction(Decl);
LLVM_DEBUG(dbgs() << "New: " << *Retain << "\n");
return true;
}
bool ObjCARCContract::contractAutorelease(Function &F, Instruction *Autorelease,
ARCInstKind Class) {
const Value *Arg = GetArgRCIdentityRoot(Autorelease);
DependenceKind DK = Class == ARCInstKind::AutoreleaseRV
? RetainAutoreleaseRVDep
: RetainAutoreleaseDep;
auto *Retain = dyn_cast_or_null<CallInst>(
findSingleDependency(DK, Arg, Autorelease->getParent(), Autorelease, PA));
if (!Retain || GetBasicARCInstKind(Retain) != ARCInstKind::Retain ||
GetArgRCIdentityRoot(Retain) != Arg)
return false;
Changed = true;
++NumPeeps;
LLVM_DEBUG(dbgs() << " Fusing retain/autorelease!\n"
" Autorelease:"
<< *Autorelease
<< "\n"
" Retain: "
<< *Retain << "\n");
Function *Decl = EP.get(Class == ARCInstKind::AutoreleaseRV
? ARCRuntimeEntryPointKind::RetainAutoreleaseRV
: ARCRuntimeEntryPointKind::RetainAutorelease);
Retain->setCalledFunction(Decl);
LLVM_DEBUG(dbgs() << " New RetainAutorelease: " << *Retain << "\n");
EraseInstruction(Autorelease);
return true;
}
static StoreInst *findSafeStoreForStoreStrongContraction(LoadInst *Load,
Instruction *Release,
ProvenanceAnalysis &PA,
AAResults *AA) {
StoreInst *Store = nullptr;
bool SawRelease = false;
MemoryLocation Loc = MemoryLocation::get(Load);
auto *LocPtr = Loc.Ptr->stripPointerCasts();
for (auto I = std::next(BasicBlock::iterator(Load)),
E = Load->getParent()->end();
I != E; ++I) {
if (Store && SawRelease)
break;
Instruction *Inst = &*I;
if (Inst == Release) {
SawRelease = true;
continue;
}
ARCInstKind Class = GetBasicARCInstKind(Inst);
if (Store) {
if (!CanUse(Inst, Load, PA, Class)) {
continue;
}
return nullptr;
}
if (IsRetain(Class))
continue;
if (!isModSet(AA->getModRefInfo(Inst, Loc)))
continue;
Store = dyn_cast<StoreInst>(Inst);
if (!Store || !Store->isSimple())
return nullptr;
if (Store->getPointerOperand()->stripPointerCasts() == LocPtr)
continue;
return nullptr;
}
if (!Store || !SawRelease)
return nullptr;
return Store;
}
static Instruction *
findRetainForStoreStrongContraction(Value *New, StoreInst *Store,
Instruction *Release,
ProvenanceAnalysis &PA) {
BasicBlock::iterator I = Store->getIterator();
BasicBlock::iterator Begin = Store->getParent()->begin();
while (I != Begin && GetBasicARCInstKind(&*I) != ARCInstKind::Retain) {
Instruction *Inst = &*I;
if (CanDecrementRefCount(Inst, New, PA) && Inst != Release)
return nullptr;
--I;
}
Instruction *Retain = &*I;
if (GetBasicARCInstKind(Retain) != ARCInstKind::Retain)
return nullptr;
if (GetArgRCIdentityRoot(Retain) != New)
return nullptr;
return Retain;
}
void ObjCARCContract::tryToContractReleaseIntoStoreStrong(
Instruction *Release, inst_iterator &Iter,
const DenseMap<BasicBlock *, ColorVector> &BlockColors) {
auto *Load = dyn_cast<LoadInst>(GetArgRCIdentityRoot(Release));
if (!Load || !Load->isSimple())
return;
BasicBlock *BB = Release->getParent();
if (Load->getParent() != BB)
return;
StoreInst *Store =
findSafeStoreForStoreStrongContraction(Load, Release, PA, AA);
if (!Store)
return;
Value *New = GetRCIdentityRoot(Store->getValueOperand());
Instruction *Retain =
findRetainForStoreStrongContraction(New, Store, Release, PA);
if (!Retain)
return;
Changed = true;
++NumStoreStrongs;
LLVM_DEBUG(
llvm::dbgs() << " Contracting retain, release into objc_storeStrong.\n"
<< " Old:\n"
<< " Store: " << *Store << "\n"
<< " Release: " << *Release << "\n"
<< " Retain: " << *Retain << "\n"
<< " Load: " << *Load << "\n");
LLVMContext &C = Release->getContext();
Type *I8X = PointerType::getUnqual(Type::getInt8Ty(C));
Type *I8XX = PointerType::getUnqual(I8X);
Value *Args[] = { Load->getPointerOperand(), New };
if (Args[0]->getType() != I8XX)
Args[0] = new BitCastInst(Args[0], I8XX, "", Store);
if (Args[1]->getType() != I8X)
Args[1] = new BitCastInst(Args[1], I8X, "", Store);
Function *Decl = EP.get(ARCRuntimeEntryPointKind::StoreStrong);
CallInst *StoreStrong =
objcarc::createCallInstWithColors(Decl, Args, "", Store, BlockColors);
StoreStrong->setDoesNotThrow();
StoreStrong->setDebugLoc(Store->getDebugLoc());
StoreStrongCalls.insert(StoreStrong);
LLVM_DEBUG(llvm::dbgs() << " New Store Strong: " << *StoreStrong
<< "\n");
if (&*Iter == Retain) ++Iter;
if (&*Iter == Store) ++Iter;
Store->eraseFromParent();
Release->eraseFromParent();
EraseInstruction(Retain);
if (Load->use_empty())
Load->eraseFromParent();
}
bool ObjCARCContract::tryToPeepholeInstruction(
Function &F, Instruction *Inst, inst_iterator &Iter,
bool &TailOkForStoreStrongs,
const DenseMap<BasicBlock *, ColorVector> &BlockColors) {
ARCInstKind Class = GetBasicARCInstKind(Inst);
switch (Class) {
case ARCInstKind::FusedRetainAutorelease:
case ARCInstKind::FusedRetainAutoreleaseRV:
return false;
case ARCInstKind::Autorelease:
case ARCInstKind::AutoreleaseRV:
return contractAutorelease(F, Inst, Class);
case ARCInstKind::Retain:
if (!optimizeRetainCall(F, Inst))
return false;
LLVM_FALLTHROUGH;
case ARCInstKind::RetainRV:
case ARCInstKind::UnsafeClaimRV: {
if (BundledInsts->contains(Inst))
return true;
if (!RVInstMarker)
return false;
BasicBlock::iterator BBI = Inst->getIterator();
BasicBlock *InstParent = Inst->getParent();
do {
if (BBI == InstParent->begin()) {
BasicBlock *Pred = InstParent->getSinglePredecessor();
if (!Pred)
goto decline_rv_optimization;
BBI = Pred->getTerminator()->getIterator();
break;
}
--BBI;
} while (IsNoopInstruction(&*BBI));
if (GetRCIdentityRoot(&*BBI) == GetArgRCIdentityRoot(Inst)) {
LLVM_DEBUG(dbgs() << "Adding inline asm marker for the return value "
"optimization.\n");
Changed = true;
InlineAsm *IA =
InlineAsm::get(FunctionType::get(Type::getVoidTy(Inst->getContext()),
false),
RVInstMarker->getString(),
"", true);
objcarc::createCallInstWithColors(IA, None, "", Inst, BlockColors);
}
decline_rv_optimization:
return false;
}
case ARCInstKind::InitWeak: {
CallInst *CI = cast<CallInst>(Inst);
if (IsNullOrUndef(CI->getArgOperand(1))) {
Value *Null = ConstantPointerNull::get(cast<PointerType>(CI->getType()));
Changed = true;
new StoreInst(Null, CI->getArgOperand(0), CI);
LLVM_DEBUG(dbgs() << "OBJCARCContract: Old = " << *CI << "\n"
<< " New = " << *Null << "\n");
CI->replaceAllUsesWith(Null);
CI->eraseFromParent();
}
return true;
}
case ARCInstKind::Release:
tryToContractReleaseIntoStoreStrong(Inst, Iter, BlockColors);
return true;
case ARCInstKind::User:
if (isa<AllocaInst>(Inst))
TailOkForStoreStrongs = false;
return true;
case ARCInstKind::IntrinsicUser:
Changed = true;
Inst->eraseFromParent();
return true;
default:
if (auto *CI = dyn_cast<CallInst>(Inst))
if (CI->getIntrinsicID() == Intrinsic::objc_clang_arc_noop_use) {
Changed = true;
CI->eraseFromParent();
}
return true;
}
}
bool ObjCARCContract::init(Module &M) {
EP.init(&M);
RVInstMarker = getRVInstMarker(M);
return false;
}
bool ObjCARCContract::run(Function &F, AAResults *A, DominatorTree *D) {
if (!EnableARCOpts)
return false;
Changed = CFGChanged = false;
AA = A;
DT = D;
PA.setAA(A);
BundledRetainClaimRVs BRV(true);
BundledInsts = &BRV;
std::pair<bool, bool> R = BundledInsts->insertAfterInvokes(F, DT);
Changed |= R.first;
CFGChanged |= R.second;
DenseMap<BasicBlock *, ColorVector> BlockColors;
if (F.hasPersonalityFn() &&
isScopedEHPersonality(classifyEHPersonality(F.getPersonalityFn())))
BlockColors = colorEHFunclets(F);
LLVM_DEBUG(llvm::dbgs() << "**** ObjCARC Contract ****\n");
bool TailOkForStoreStrongs =
!F.isVarArg() && !F.callsFunctionThatReturnsTwice();
for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E;) {
Instruction *Inst = &*I++;
LLVM_DEBUG(dbgs() << "Visiting: " << *Inst << "\n");
if (auto *CI = dyn_cast<CallInst>(Inst))
if (objcarc::hasAttachedCallOpBundle(CI)) {
BundledInsts->insertRVCallWithColors(&*I, CI, BlockColors);
--I;
Changed = true;
}
if (tryToPeepholeInstruction(F, Inst, I, TailOkForStoreStrongs,
BlockColors))
continue;
auto ReplaceArgUses = [Inst, this](Value *Arg) {
if (!isa<Instruction>(Arg) && !isa<Argument>(Arg))
return;
for (Value::use_iterator UI = Arg->use_begin(), UE = Arg->use_end();
UI != UE; ) {
Use &U = *UI++;
unsigned OperandNo = U.getOperandNo();
if (!DT->isReachableFromEntry(U) || !DT->dominates(Inst, U))
continue;
Changed = true;
Instruction *Replacement = Inst;
Type *UseTy = U.get()->getType();
if (PHINode *PHI = dyn_cast<PHINode>(U.getUser())) {
unsigned ValNo = PHINode::getIncomingValueNumForOperand(OperandNo);
BasicBlock *IncomingBB = PHI->getIncomingBlock(ValNo);
if (Replacement->getType() != UseTy) {
BasicBlock *InsertBB = IncomingBB;
while (isa<CatchSwitchInst>(InsertBB->getFirstNonPHI())) {
InsertBB = DT->getNode(InsertBB)->getIDom()->getBlock();
}
assert(DT->dominates(Inst, &InsertBB->back()) &&
"Invalid insertion point for bitcast");
Replacement =
new BitCastInst(Replacement, UseTy, "", &InsertBB->back());
}
for (unsigned i = 0, e = PHI->getNumIncomingValues(); i != e; ++i)
if (PHI->getIncomingBlock(i) == IncomingBB) {
if (UI != UE &&
&PHI->getOperandUse(
PHINode::getOperandNumForIncomingValue(i)) == &*UI)
++UI;
PHI->setIncomingValue(i, Replacement);
}
} else {
if (Replacement->getType() != UseTy)
Replacement = new BitCastInst(Replacement, UseTy, "",
cast<Instruction>(U.getUser()));
U.set(Replacement);
}
}
};
Value *Arg = cast<CallInst>(Inst)->getArgOperand(0);
Value *OrigArg = Arg;
for (;;) {
ReplaceArgUses(Arg);
if (const BitCastInst *BI = dyn_cast<BitCastInst>(Arg))
Arg = BI->getOperand(0);
else if (isa<GEPOperator>(Arg) &&
cast<GEPOperator>(Arg)->hasAllZeroIndices())
Arg = cast<GEPOperator>(Arg)->getPointerOperand();
else if (isa<GlobalAlias>(Arg) &&
!cast<GlobalAlias>(Arg)->isInterposable())
Arg = cast<GlobalAlias>(Arg)->getAliasee();
else {
if (PHINode *PN = dyn_cast<PHINode>(Arg)) {
SmallVector<Value *, 1> PHIList;
getEquivalentPHIs(*PN, PHIList);
for (Value *PHI : PHIList)
ReplaceArgUses(PHI);
}
break;
}
}
SmallVector<BitCastInst *, 2> BitCastUsers;
for (User *U : OrigArg->users())
if (auto *BC = dyn_cast<BitCastInst>(U))
BitCastUsers.push_back(BC);
while (!BitCastUsers.empty()) {
auto *BC = BitCastUsers.pop_back_val();
for (User *U : BC->users())
if (auto *B = dyn_cast<BitCastInst>(U))
BitCastUsers.push_back(B);
ReplaceArgUses(BC);
}
}
if (TailOkForStoreStrongs)
for (CallInst *CI : StoreStrongCalls)
CI->setTailCall();
StoreStrongCalls.clear();
return Changed;
}
char ObjCARCContractLegacyPass::ID = 0;
INITIALIZE_PASS_BEGIN(ObjCARCContractLegacyPass, "objc-arc-contract",
"ObjC ARC contraction", false, false)
INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
INITIALIZE_PASS_END(ObjCARCContractLegacyPass, "objc-arc-contract",
"ObjC ARC contraction", false, false)
void ObjCARCContractLegacyPass::getAnalysisUsage(AnalysisUsage &AU) const {
AU.addRequired<AAResultsWrapperPass>();
AU.addRequired<DominatorTreeWrapperPass>();
}
Pass *llvm::createObjCARCContractPass() {
return new ObjCARCContractLegacyPass();
}
bool ObjCARCContractLegacyPass::runOnFunction(Function &F) {
ObjCARCContract OCARCC;
OCARCC.init(*F.getParent());
auto *AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
auto *DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
return OCARCC.run(F, AA, DT);
}
PreservedAnalyses ObjCARCContractPass::run(Function &F,
FunctionAnalysisManager &AM) {
ObjCARCContract OCAC;
OCAC.init(*F.getParent());
bool Changed = OCAC.run(F, &AM.getResult<AAManager>(F),
&AM.getResult<DominatorTreeAnalysis>(F));
bool CFGChanged = OCAC.hasCFGChanged();
if (Changed) {
PreservedAnalyses PA;
if (!CFGChanged)
PA.preserveSet<CFGAnalyses>();
return PA;
}
return PreservedAnalyses::all();
}