2013-07-10 18:49:00 +00:00
|
|
|
//===- ObjCARC.h - ObjC ARC Optimization --------------*- C++ -*-----------===//
|
2013-01-28 03:28:38 +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 common definitions/declarations used by the ObjC ARC
|
|
|
|
/// Optimizer. ARC stands for Automatic Reference Counting and is a system for
|
|
|
|
/// managing reference counts for objects in Objective C.
|
|
|
|
///
|
|
|
|
/// 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.
|
|
|
|
///
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2014-08-13 16:26:38 +00:00
|
|
|
#ifndef LLVM_LIB_TRANSFORMS_OBJCARC_OBJCARC_H
|
|
|
|
#define LLVM_LIB_TRANSFORMS_OBJCARC_OBJCARC_H
|
2013-01-28 03:28:38 +00:00
|
|
|
|
|
|
|
#include "llvm/ADT/StringSwitch.h"
|
2015-03-16 07:02:27 +00:00
|
|
|
#include "llvm/ADT/Optional.h"
|
2013-01-28 03:28:38 +00:00
|
|
|
#include "llvm/Analysis/AliasAnalysis.h"
|
|
|
|
#include "llvm/Analysis/Passes.h"
|
2013-01-28 05:51:54 +00:00
|
|
|
#include "llvm/Analysis/ValueTracking.h"
|
2014-03-04 11:01:28 +00:00
|
|
|
#include "llvm/IR/CallSite.h"
|
2014-03-04 10:30:26 +00:00
|
|
|
#include "llvm/IR/InstIterator.h"
|
2013-01-28 03:28:38 +00:00
|
|
|
#include "llvm/IR/Module.h"
|
|
|
|
#include "llvm/Pass.h"
|
|
|
|
#include "llvm/Transforms/ObjCARC.h"
|
2013-01-29 03:03:03 +00:00
|
|
|
#include "llvm/Transforms/Utils/Local.h"
|
2015-02-19 19:51:32 +00:00
|
|
|
#include "ARCInstKind.h"
|
2013-01-28 03:28:38 +00:00
|
|
|
|
2013-01-29 04:51:59 +00:00
|
|
|
namespace llvm {
|
|
|
|
class raw_ostream;
|
|
|
|
}
|
|
|
|
|
2013-01-28 03:28:38 +00:00
|
|
|
namespace llvm {
|
|
|
|
namespace objcarc {
|
|
|
|
|
|
|
|
/// \brief A handy option to enable/disable all ARC Optimizations.
|
|
|
|
extern bool EnableARCOpts;
|
|
|
|
|
|
|
|
/// \brief Test if the given module looks interesting to run ARC optimization
|
|
|
|
/// on.
|
|
|
|
static inline bool ModuleHasARC(const Module &M) {
|
|
|
|
return
|
|
|
|
M.getNamedValue("objc_retain") ||
|
|
|
|
M.getNamedValue("objc_release") ||
|
|
|
|
M.getNamedValue("objc_autorelease") ||
|
|
|
|
M.getNamedValue("objc_retainAutoreleasedReturnValue") ||
|
|
|
|
M.getNamedValue("objc_retainBlock") ||
|
|
|
|
M.getNamedValue("objc_autoreleaseReturnValue") ||
|
|
|
|
M.getNamedValue("objc_autoreleasePoolPush") ||
|
|
|
|
M.getNamedValue("objc_loadWeakRetained") ||
|
|
|
|
M.getNamedValue("objc_loadWeak") ||
|
|
|
|
M.getNamedValue("objc_destroyWeak") ||
|
|
|
|
M.getNamedValue("objc_storeWeak") ||
|
|
|
|
M.getNamedValue("objc_initWeak") ||
|
|
|
|
M.getNamedValue("objc_moveWeak") ||
|
|
|
|
M.getNamedValue("objc_copyWeak") ||
|
|
|
|
M.getNamedValue("objc_retainedObject") ||
|
|
|
|
M.getNamedValue("objc_unretainedObject") ||
|
2013-03-29 21:15:23 +00:00
|
|
|
M.getNamedValue("objc_unretainedPointer") ||
|
|
|
|
M.getNamedValue("clang.arc.use");
|
2013-01-28 03:28:38 +00:00
|
|
|
}
|
|
|
|
|
2013-01-28 05:51:54 +00:00
|
|
|
/// \brief This is a wrapper around getUnderlyingObject which also knows how to
|
|
|
|
/// look through objc_retain and objc_autorelease calls, which we know to return
|
|
|
|
/// their argument verbatim.
|
2015-03-10 02:37:25 +00:00
|
|
|
static inline const Value *GetUnderlyingObjCPtr(const Value *V,
|
|
|
|
const DataLayout &DL) {
|
2013-01-28 05:51:54 +00:00
|
|
|
for (;;) {
|
2015-03-10 02:37:25 +00:00
|
|
|
V = GetUnderlyingObject(V, DL);
|
2015-02-19 19:51:32 +00:00
|
|
|
if (!IsForwarding(GetBasicARCInstKind(V)))
|
2013-01-28 05:51:54 +00:00
|
|
|
break;
|
|
|
|
V = cast<CallInst>(V)->getArgOperand(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return V;
|
|
|
|
}
|
|
|
|
|
2015-02-19 00:42:38 +00:00
|
|
|
/// The RCIdentity root of a value \p V is a dominating value U for which
|
|
|
|
/// retaining or releasing U is equivalent to retaining or releasing V. In other
|
|
|
|
/// words, ARC operations on \p V are equivalent to ARC operations on \p U.
|
|
|
|
///
|
|
|
|
/// We use this in the ARC optimizer to make it easier to match up ARC
|
|
|
|
/// operations by always mapping ARC operations to RCIdentityRoots instead of
|
|
|
|
/// pointers themselves.
|
|
|
|
///
|
|
|
|
/// The two ways that we see RCIdentical values in ObjC are via:
|
|
|
|
///
|
|
|
|
/// 1. PointerCasts
|
|
|
|
/// 2. Forwarding Calls that return their argument verbatim.
|
|
|
|
///
|
|
|
|
/// Thus this function strips off pointer casts and forwarding calls. *NOTE*
|
|
|
|
/// This implies that two RCIdentical values must alias.
|
|
|
|
static inline const Value *GetRCIdentityRoot(const Value *V) {
|
2013-01-28 05:51:54 +00:00
|
|
|
for (;;) {
|
|
|
|
V = V->stripPointerCasts();
|
2015-02-19 19:51:32 +00:00
|
|
|
if (!IsForwarding(GetBasicARCInstKind(V)))
|
2013-01-28 05:51:54 +00:00
|
|
|
break;
|
|
|
|
V = cast<CallInst>(V)->getArgOperand(0);
|
|
|
|
}
|
|
|
|
return V;
|
|
|
|
}
|
|
|
|
|
2015-02-19 00:42:38 +00:00
|
|
|
/// Helper which calls const Value *GetRCIdentityRoot(const Value *V) and just
|
|
|
|
/// casts away the const of the result. For documentation about what an
|
|
|
|
/// RCIdentityRoot (and by extension GetRCIdentityRoot is) look at that
|
|
|
|
/// function.
|
|
|
|
static inline Value *GetRCIdentityRoot(Value *V) {
|
|
|
|
return const_cast<Value *>(GetRCIdentityRoot((const Value *)V));
|
2013-01-28 05:51:54 +00:00
|
|
|
}
|
|
|
|
|
2013-01-29 03:03:03 +00:00
|
|
|
/// \brief Assuming the given instruction is one of the special calls such as
|
2015-02-19 00:42:38 +00:00
|
|
|
/// objc_retain or objc_release, return the RCIdentity root of the argument of
|
|
|
|
/// the call.
|
|
|
|
static inline Value *GetArgRCIdentityRoot(Value *Inst) {
|
|
|
|
return GetRCIdentityRoot(cast<CallInst>(Inst)->getArgOperand(0));
|
2013-01-29 03:03:03 +00:00
|
|
|
}
|
|
|
|
|
2013-03-25 09:27:43 +00:00
|
|
|
static inline bool IsNullOrUndef(const Value *V) {
|
2013-01-29 03:03:03 +00:00
|
|
|
return isa<ConstantPointerNull>(V) || isa<UndefValue>(V);
|
|
|
|
}
|
|
|
|
|
2013-03-25 09:27:43 +00:00
|
|
|
static inline bool IsNoopInstruction(const Instruction *I) {
|
2013-01-29 03:03:03 +00:00
|
|
|
return isa<BitCastInst>(I) ||
|
|
|
|
(isa<GetElementPtrInst>(I) &&
|
|
|
|
cast<GetElementPtrInst>(I)->hasAllZeroIndices());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// \brief Erase the given instruction.
|
|
|
|
///
|
|
|
|
/// Many ObjC calls return their argument verbatim,
|
|
|
|
/// so if it's such a call and the return value has users, replace them with the
|
|
|
|
/// argument value.
|
|
|
|
///
|
|
|
|
static inline void EraseInstruction(Instruction *CI) {
|
|
|
|
Value *OldArg = cast<CallInst>(CI)->getArgOperand(0);
|
|
|
|
|
|
|
|
bool Unused = CI->use_empty();
|
|
|
|
|
|
|
|
if (!Unused) {
|
|
|
|
// Replace the return value with the argument.
|
2015-02-19 19:51:32 +00:00
|
|
|
assert((IsForwarding(GetBasicARCInstKind(CI)) ||
|
|
|
|
(IsNoopOnNull(GetBasicARCInstKind(CI)) &&
|
2013-07-08 23:30:23 +00:00
|
|
|
isa<ConstantPointerNull>(OldArg))) &&
|
2013-01-29 03:03:03 +00:00
|
|
|
"Can't delete non-forwarding instruction with users!");
|
|
|
|
CI->replaceAllUsesWith(OldArg);
|
|
|
|
}
|
|
|
|
|
|
|
|
CI->eraseFromParent();
|
|
|
|
|
|
|
|
if (Unused)
|
|
|
|
RecursivelyDeleteTriviallyDeadInstructions(OldArg);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Test whether the given value is possible a retainable object pointer.
|
|
|
|
static inline bool IsPotentialRetainableObjPtr(const Value *Op) {
|
2013-01-29 04:58:30 +00:00
|
|
|
// Pointers to static or stack storage are not valid retainable object
|
|
|
|
// pointers.
|
2013-01-29 03:03:03 +00:00
|
|
|
if (isa<Constant>(Op) || isa<AllocaInst>(Op))
|
|
|
|
return false;
|
|
|
|
// Special arguments can not be a valid retainable object pointer.
|
|
|
|
if (const Argument *Arg = dyn_cast<Argument>(Op))
|
|
|
|
if (Arg->hasByValAttr() ||
|
2014-01-28 02:38:36 +00:00
|
|
|
Arg->hasInAllocaAttr() ||
|
2013-01-29 03:03:03 +00:00
|
|
|
Arg->hasNestAttr() ||
|
|
|
|
Arg->hasStructRetAttr())
|
|
|
|
return false;
|
|
|
|
// Only consider values with pointer types.
|
|
|
|
//
|
|
|
|
// It seemes intuitive to exclude function pointer types as well, since
|
|
|
|
// functions are never retainable object pointers, however clang occasionally
|
|
|
|
// bitcasts retainable object pointers to function-pointer type temporarily.
|
|
|
|
PointerType *Ty = dyn_cast<PointerType>(Op->getType());
|
|
|
|
if (!Ty)
|
|
|
|
return false;
|
2013-01-29 04:58:30 +00:00
|
|
|
// Conservatively assume anything else is a potential retainable object
|
|
|
|
// pointer.
|
2013-01-29 03:03:03 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool IsPotentialRetainableObjPtr(const Value *Op,
|
|
|
|
AliasAnalysis &AA) {
|
|
|
|
// First make the rudimentary check.
|
|
|
|
if (!IsPotentialRetainableObjPtr(Op))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Objects in constant memory are not reference-counted.
|
|
|
|
if (AA.pointsToConstantMemory(Op))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Pointers in constant memory are not pointing to reference-counted objects.
|
|
|
|
if (const LoadInst *LI = dyn_cast<LoadInst>(Op))
|
|
|
|
if (AA.pointsToConstantMemory(LI->getPointerOperand()))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Otherwise assume the worst.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-02-19 19:51:32 +00:00
|
|
|
/// \brief Helper for GetARCInstKind. Determines what kind of construct CS
|
2013-01-29 03:03:03 +00:00
|
|
|
/// is.
|
2015-02-19 19:51:32 +00:00
|
|
|
static inline ARCInstKind GetCallSiteClass(ImmutableCallSite CS) {
|
2013-01-29 03:03:03 +00:00
|
|
|
for (ImmutableCallSite::arg_iterator I = CS.arg_begin(), E = CS.arg_end();
|
|
|
|
I != E; ++I)
|
|
|
|
if (IsPotentialRetainableObjPtr(*I))
|
2015-02-19 19:51:32 +00:00
|
|
|
return CS.onlyReadsMemory() ? ARCInstKind::User : ARCInstKind::CallOrUser;
|
2013-01-29 03:03:03 +00:00
|
|
|
|
2015-02-19 19:51:32 +00:00
|
|
|
return CS.onlyReadsMemory() ? ARCInstKind::None : ARCInstKind::Call;
|
2013-01-29 03:03:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Return true if this value refers to a distinct and identifiable
|
|
|
|
/// object.
|
|
|
|
///
|
|
|
|
/// This is similar to AliasAnalysis's isIdentifiedObject, except that it uses
|
|
|
|
/// special knowledge of ObjC conventions.
|
|
|
|
static inline bool IsObjCIdentifiedObject(const Value *V) {
|
|
|
|
// Assume that call results and arguments have their own "provenance".
|
|
|
|
// Constants (including GlobalVariables) and Allocas are never
|
|
|
|
// reference-counted.
|
|
|
|
if (isa<CallInst>(V) || isa<InvokeInst>(V) ||
|
|
|
|
isa<Argument>(V) || isa<Constant>(V) ||
|
|
|
|
isa<AllocaInst>(V))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (const LoadInst *LI = dyn_cast<LoadInst>(V)) {
|
|
|
|
const Value *Pointer =
|
2015-02-19 00:42:38 +00:00
|
|
|
GetRCIdentityRoot(LI->getPointerOperand());
|
2013-01-29 03:03:03 +00:00
|
|
|
if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(Pointer)) {
|
|
|
|
// A constant pointer can't be pointing to an object on the heap. It may
|
|
|
|
// be reference-counted, but it won't be deleted.
|
|
|
|
if (GV->isConstant())
|
|
|
|
return true;
|
|
|
|
StringRef Name = GV->getName();
|
|
|
|
// These special variables are known to hold values which are not
|
|
|
|
// reference-counted pointers.
|
2014-11-17 02:28:27 +00:00
|
|
|
if (Name.startswith("\01l_objc_msgSend_fixup_"))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
StringRef Section = GV->getSection();
|
|
|
|
if (Section.find("__message_refs") != StringRef::npos ||
|
|
|
|
Section.find("__objc_classrefs") != StringRef::npos ||
|
|
|
|
Section.find("__objc_superrefs") != StringRef::npos ||
|
|
|
|
Section.find("__objc_methname") != StringRef::npos ||
|
|
|
|
Section.find("__cstring") != StringRef::npos)
|
2013-01-29 03:03:03 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2013-01-28 05:51:54 +00:00
|
|
|
|
2015-03-16 07:02:27 +00:00
|
|
|
enum class ARCMDKindID {
|
|
|
|
ImpreciseRelease,
|
|
|
|
CopyOnEscape,
|
|
|
|
NoObjCARCExceptions,
|
|
|
|
};
|
|
|
|
|
2015-03-06 00:34:33 +00:00
|
|
|
/// A cache of MDKinds used by various ARC optimizations.
|
2015-03-16 07:02:27 +00:00
|
|
|
class ARCMDKindCache {
|
|
|
|
Module *M;
|
|
|
|
|
2015-03-06 00:34:33 +00:00
|
|
|
/// The Metadata Kind for clang.imprecise_release metadata.
|
2015-03-16 07:02:27 +00:00
|
|
|
llvm::Optional<unsigned> ImpreciseReleaseMDKind;
|
2015-03-06 00:34:33 +00:00
|
|
|
|
|
|
|
/// The Metadata Kind for clang.arc.copy_on_escape metadata.
|
2015-03-16 07:02:27 +00:00
|
|
|
llvm::Optional<unsigned> CopyOnEscapeMDKind;
|
2015-03-06 00:34:33 +00:00
|
|
|
|
|
|
|
/// The Metadata Kind for clang.arc.no_objc_arc_exceptions metadata.
|
2015-03-16 07:02:27 +00:00
|
|
|
llvm::Optional<unsigned> NoObjCARCExceptionsMDKind;
|
|
|
|
|
|
|
|
public:
|
|
|
|
void init(Module *Mod) {
|
|
|
|
M = Mod;
|
|
|
|
ImpreciseReleaseMDKind = NoneType::None;
|
|
|
|
CopyOnEscapeMDKind = NoneType::None;
|
|
|
|
NoObjCARCExceptionsMDKind = NoneType::None;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned get(ARCMDKindID ID) {
|
|
|
|
switch (ID) {
|
|
|
|
case ARCMDKindID::ImpreciseRelease:
|
|
|
|
if (!ImpreciseReleaseMDKind)
|
|
|
|
ImpreciseReleaseMDKind =
|
|
|
|
M->getContext().getMDKindID("clang.imprecise_release");
|
|
|
|
return *ImpreciseReleaseMDKind;
|
|
|
|
case ARCMDKindID::CopyOnEscape:
|
|
|
|
if (!CopyOnEscapeMDKind)
|
|
|
|
CopyOnEscapeMDKind =
|
|
|
|
M->getContext().getMDKindID("clang.arc.copy_on_escape");
|
|
|
|
return *CopyOnEscapeMDKind;
|
|
|
|
case ARCMDKindID::NoObjCARCExceptions:
|
|
|
|
if (!NoObjCARCExceptionsMDKind)
|
|
|
|
NoObjCARCExceptionsMDKind =
|
|
|
|
M->getContext().getMDKindID("clang.arc.no_objc_arc_exceptions");
|
|
|
|
return *NoObjCARCExceptionsMDKind;
|
|
|
|
}
|
2015-03-16 07:46:34 +00:00
|
|
|
llvm_unreachable("Covered switch isn't covered?!");
|
2015-03-16 07:02:27 +00:00
|
|
|
}
|
2015-03-06 00:34:33 +00:00
|
|
|
};
|
|
|
|
|
2013-01-28 03:28:38 +00:00
|
|
|
} // end namespace objcarc
|
|
|
|
} // end namespace llvm
|
|
|
|
|
2014-08-13 16:26:38 +00:00
|
|
|
#endif
|