2013-12-22 00:04:03 +00:00
|
|
|
//===- ObjCARCAliasAnalysis.cpp - ObjC ARC Optimization -------------------===//
|
2013-04-08 18:41:23 +00:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
/// \file
|
|
|
|
/// This file defines a simple ARC-aware AliasAnalysis using special knowledge
|
|
|
|
/// of Objective C to enhance other optimization passes which rely on the Alias
|
|
|
|
/// Analysis infrastructure.
|
|
|
|
///
|
|
|
|
/// 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.
|
|
|
|
///
|
2015-12-30 11:46:15 +00:00
|
|
|
/// TODO: Theoretically we could check for dependencies between objc_* calls
|
|
|
|
/// and FMRB_OnlyAccessesArgumentPointees calls or other well-behaved calls.
|
|
|
|
///
|
2013-04-08 18:41:23 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2015-12-30 11:46:15 +00:00
|
|
|
#include "llvm/Analysis/ObjCARCAliasAnalysis.h"
|
|
|
|
#include "llvm/Analysis/ObjCARCAnalysisUtils.h"
|
|
|
|
#include "llvm/IR/Function.h"
|
2013-04-08 18:41:23 +00:00
|
|
|
#include "llvm/IR/Instruction.h"
|
2015-12-30 11:46:15 +00:00
|
|
|
#include "llvm/IR/Value.h"
|
2013-04-08 18:41:23 +00:00
|
|
|
#include "llvm/InitializePasses.h"
|
|
|
|
#include "llvm/PassAnalysisSupport.h"
|
|
|
|
#include "llvm/PassSupport.h"
|
|
|
|
|
2014-11-24 09:08:18 +00:00
|
|
|
#define DEBUG_TYPE "objc-arc-aa"
|
|
|
|
|
2013-04-08 18:41:23 +00:00
|
|
|
using namespace llvm;
|
|
|
|
using namespace llvm::objcarc;
|
|
|
|
|
2015-12-30 11:46:15 +00:00
|
|
|
AliasResult ObjCARCAAResult::alias(const MemoryLocation &LocA,
|
|
|
|
const MemoryLocation &LocB) {
|
2013-04-08 18:41:23 +00:00
|
|
|
if (!EnableARCOpts)
|
2015-12-30 11:46:15 +00:00
|
|
|
return AAResultBase::alias(LocA, LocB);
|
2013-04-08 18:41:23 +00:00
|
|
|
|
|
|
|
// First, strip off no-ops, including ObjC-specific no-ops, and try making a
|
|
|
|
// precise alias query.
|
2015-05-27 18:44:32 +00:00
|
|
|
const Value *SA = GetRCIdentityRoot(LocA.Ptr);
|
|
|
|
const Value *SB = GetRCIdentityRoot(LocB.Ptr);
|
2013-04-08 18:41:23 +00:00
|
|
|
AliasResult Result =
|
2015-12-30 11:46:15 +00:00
|
|
|
AAResultBase::alias(MemoryLocation(SA, LocA.Size, LocA.AATags),
|
|
|
|
MemoryLocation(SB, LocB.Size, LocB.AATags));
|
2013-04-08 18:41:23 +00:00
|
|
|
if (Result != MayAlias)
|
|
|
|
return Result;
|
|
|
|
|
|
|
|
// If that failed, climb to the underlying object, including climbing through
|
|
|
|
// ObjC-specific no-ops, and try making an imprecise alias query.
|
2015-12-30 11:46:15 +00:00
|
|
|
const Value *UA = GetUnderlyingObjCPtr(SA, DL);
|
|
|
|
const Value *UB = GetUnderlyingObjCPtr(SB, DL);
|
2013-04-08 18:41:23 +00:00
|
|
|
if (UA != SA || UB != SB) {
|
2015-12-30 11:46:15 +00:00
|
|
|
Result = AAResultBase::alias(MemoryLocation(UA), MemoryLocation(UB));
|
2013-04-08 18:41:23 +00:00
|
|
|
// We can't use MustAlias or PartialAlias results here because
|
|
|
|
// GetUnderlyingObjCPtr may return an offsetted pointer value.
|
|
|
|
if (Result == NoAlias)
|
|
|
|
return NoAlias;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If that failed, fail. We don't need to chain here, since that's covered
|
|
|
|
// by the earlier precise query.
|
|
|
|
return MayAlias;
|
|
|
|
}
|
|
|
|
|
2015-12-30 11:46:15 +00:00
|
|
|
bool ObjCARCAAResult::pointsToConstantMemory(const MemoryLocation &Loc,
|
|
|
|
bool OrLocal) {
|
2013-04-08 18:41:23 +00:00
|
|
|
if (!EnableARCOpts)
|
2015-12-30 11:46:15 +00:00
|
|
|
return AAResultBase::pointsToConstantMemory(Loc, OrLocal);
|
2013-04-08 18:41:23 +00:00
|
|
|
|
|
|
|
// First, strip off no-ops, including ObjC-specific no-ops, and try making
|
|
|
|
// a precise alias query.
|
2015-05-27 18:44:32 +00:00
|
|
|
const Value *S = GetRCIdentityRoot(Loc.Ptr);
|
2015-12-30 11:46:15 +00:00
|
|
|
if (AAResultBase::pointsToConstantMemory(
|
2015-06-21 13:59:01 +00:00
|
|
|
MemoryLocation(S, Loc.Size, Loc.AATags), OrLocal))
|
2013-04-08 18:41:23 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
// If that failed, climb to the underlying object, including climbing through
|
|
|
|
// ObjC-specific no-ops, and try making an imprecise alias query.
|
2015-12-30 11:46:15 +00:00
|
|
|
const Value *U = GetUnderlyingObjCPtr(S, DL);
|
2013-04-08 18:41:23 +00:00
|
|
|
if (U != S)
|
2015-12-30 11:46:15 +00:00
|
|
|
return AAResultBase::pointsToConstantMemory(MemoryLocation(U), OrLocal);
|
2013-04-08 18:41:23 +00:00
|
|
|
|
|
|
|
// If that failed, fail. We don't need to chain here, since that's covered
|
|
|
|
// by the earlier precise query.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-12-30 11:46:15 +00:00
|
|
|
FunctionModRefBehavior ObjCARCAAResult::getModRefBehavior(const Function *F) {
|
2013-04-08 18:41:23 +00:00
|
|
|
if (!EnableARCOpts)
|
2015-12-30 11:46:15 +00:00
|
|
|
return AAResultBase::getModRefBehavior(F);
|
2013-04-08 18:41:23 +00:00
|
|
|
|
|
|
|
switch (GetFunctionClass(F)) {
|
2015-05-27 18:44:32 +00:00
|
|
|
case ARCInstKind::NoopCast:
|
2015-12-30 11:46:15 +00:00
|
|
|
return FMRB_DoesNotAccessMemory;
|
2013-04-08 18:41:23 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-12-30 11:46:15 +00:00
|
|
|
return AAResultBase::getModRefBehavior(F);
|
2013-04-08 18:41:23 +00:00
|
|
|
}
|
|
|
|
|
2015-12-30 11:46:15 +00:00
|
|
|
ModRefInfo ObjCARCAAResult::getModRefInfo(ImmutableCallSite CS,
|
|
|
|
const MemoryLocation &Loc) {
|
2013-04-08 18:41:23 +00:00
|
|
|
if (!EnableARCOpts)
|
2015-12-30 11:46:15 +00:00
|
|
|
return AAResultBase::getModRefInfo(CS, Loc);
|
2013-04-08 18:41:23 +00:00
|
|
|
|
2015-05-27 18:44:32 +00:00
|
|
|
switch (GetBasicARCInstKind(CS.getInstruction())) {
|
|
|
|
case ARCInstKind::Retain:
|
|
|
|
case ARCInstKind::RetainRV:
|
|
|
|
case ARCInstKind::Autorelease:
|
|
|
|
case ARCInstKind::AutoreleaseRV:
|
|
|
|
case ARCInstKind::NoopCast:
|
|
|
|
case ARCInstKind::AutoreleasepoolPush:
|
|
|
|
case ARCInstKind::FusedRetainAutorelease:
|
|
|
|
case ARCInstKind::FusedRetainAutoreleaseRV:
|
2013-04-08 18:41:23 +00:00
|
|
|
// These functions don't access any memory visible to the compiler.
|
|
|
|
// Note that this doesn't include objc_retainBlock, because it updates
|
|
|
|
// pointers when it copies block data.
|
2015-12-30 11:46:15 +00:00
|
|
|
return MRI_NoModRef;
|
2013-04-08 18:41:23 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-12-30 11:46:15 +00:00
|
|
|
return AAResultBase::getModRefInfo(CS, Loc);
|
|
|
|
}
|
|
|
|
|
2017-01-02 19:17:04 +00:00
|
|
|
ObjCARCAAResult ObjCARCAA::run(Function &F, FunctionAnalysisManager &AM) {
|
2016-07-23 20:41:05 +00:00
|
|
|
return ObjCARCAAResult(F.getParent()->getDataLayout());
|
2015-12-30 11:46:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
char ObjCARCAAWrapperPass::ID = 0;
|
2016-07-23 20:41:05 +00:00
|
|
|
INITIALIZE_PASS(ObjCARCAAWrapperPass, "objc-arc-aa",
|
|
|
|
"ObjC-ARC-Based Alias Analysis", false, true)
|
2015-12-30 11:46:15 +00:00
|
|
|
|
|
|
|
ImmutablePass *llvm::createObjCARCAAWrapperPass() {
|
|
|
|
return new ObjCARCAAWrapperPass();
|
|
|
|
}
|
|
|
|
|
|
|
|
ObjCARCAAWrapperPass::ObjCARCAAWrapperPass() : ImmutablePass(ID) {
|
|
|
|
initializeObjCARCAAWrapperPassPass(*PassRegistry::getPassRegistry());
|
2013-04-08 18:41:23 +00:00
|
|
|
}
|
|
|
|
|
2015-12-30 11:46:15 +00:00
|
|
|
bool ObjCARCAAWrapperPass::doInitialization(Module &M) {
|
2016-07-23 20:41:05 +00:00
|
|
|
Result.reset(new ObjCARCAAResult(M.getDataLayout()));
|
2015-12-30 11:46:15 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ObjCARCAAWrapperPass::doFinalization(Module &M) {
|
|
|
|
Result.reset();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ObjCARCAAWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.setPreservesAll();
|
2013-04-08 18:41:23 +00:00
|
|
|
}
|