2013-01-28 09:35:51 +08:00
|
|
|
//===- ObjCARCOpts.cpp - ObjC ARC Optimization ----------------------------===//
|
2011-06-16 07:37:01 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2013-01-14 08:35:14 +08:00
|
|
|
/// \file
|
|
|
|
/// This file defines ObjC ARC optimizations. ARC stands for Automatic
|
|
|
|
/// Reference Counting and is a system for managing reference counts for objects
|
|
|
|
/// in Objective C.
|
|
|
|
///
|
|
|
|
/// The optimizations performed include elimination of redundant, partially
|
|
|
|
/// redundant, and inconsequential reference count operations, elimination of
|
2013-02-07 12:12:57 +08:00
|
|
|
/// redundant weak pointer operations, and numerous minor simplifications.
|
2013-01-14 08:35:14 +08:00
|
|
|
///
|
|
|
|
/// 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.
|
|
|
|
///
|
2011-06-16 07:37:01 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2013-01-28 11:28:38 +08:00
|
|
|
#include "ObjCARC.h"
|
2013-07-06 09:39:23 +08:00
|
|
|
#include "ARCRuntimeEntryPoints.h"
|
2015-03-24 03:32:43 +08:00
|
|
|
#include "BlotMapVector.h"
|
2013-01-29 12:20:52 +08:00
|
|
|
#include "DependencyAnalysis.h"
|
2013-01-29 11:03:03 +08:00
|
|
|
#include "ProvenanceAnalysis.h"
|
2015-03-06 07:29:03 +08:00
|
|
|
#include "PtrState.h"
|
2011-06-16 07:37:01 +08:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
2013-05-25 04:44:02 +08:00
|
|
|
#include "llvm/ADT/DenseSet.h"
|
2013-01-28 12:12:07 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2013-01-29 11:03:03 +08:00
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
2013-01-29 12:20:52 +08:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
2015-08-20 16:06:03 +08:00
|
|
|
#include "llvm/Analysis/ObjCARCAliasAnalysis.h"
|
2014-03-04 19:45:46 +08:00
|
|
|
#include "llvm/IR/CFG.h"
|
2013-03-26 08:42:09 +08:00
|
|
|
#include "llvm/IR/IRBuilder.h"
|
2013-01-29 12:20:52 +08:00
|
|
|
#include "llvm/IR/LLVMContext.h"
|
2013-01-29 12:51:59 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
2013-01-29 17:09:27 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2013-01-28 12:12:07 +08:00
|
|
|
|
2011-06-16 07:37:01 +08:00
|
|
|
using namespace llvm;
|
2013-01-28 11:28:38 +08:00
|
|
|
using namespace llvm::objcarc;
|
2011-06-16 07:37:01 +08:00
|
|
|
|
2014-04-22 10:55:47 +08:00
|
|
|
#define DEBUG_TYPE "objc-arc-opts"
|
|
|
|
|
2013-01-14 08:35:14 +08:00
|
|
|
/// \defgroup ARCUtilities Utility declarations/definitions specific to ARC.
|
|
|
|
/// @{
|
2011-06-16 07:37:01 +08:00
|
|
|
|
2015-02-19 08:42:38 +08:00
|
|
|
/// \brief This is similar to GetRCIdentityRoot but it stops as soon
|
2013-01-14 08:35:14 +08:00
|
|
|
/// as it finds a value with multiple uses.
|
2011-06-16 07:37:01 +08:00
|
|
|
static const Value *FindSingleUseIdentifiedObject(const Value *Arg) {
|
|
|
|
if (Arg->hasOneUse()) {
|
|
|
|
if (const BitCastInst *BC = dyn_cast<BitCastInst>(Arg))
|
|
|
|
return FindSingleUseIdentifiedObject(BC->getOperand(0));
|
|
|
|
if (const GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Arg))
|
|
|
|
if (GEP->hasAllZeroIndices())
|
|
|
|
return FindSingleUseIdentifiedObject(GEP->getPointerOperand());
|
2015-02-20 03:51:32 +08:00
|
|
|
if (IsForwarding(GetBasicARCInstKind(Arg)))
|
2011-06-16 07:37:01 +08:00
|
|
|
return FindSingleUseIdentifiedObject(
|
|
|
|
cast<CallInst>(Arg)->getArgOperand(0));
|
|
|
|
if (!IsObjCIdentifiedObject(Arg))
|
2014-04-25 13:29:35 +08:00
|
|
|
return nullptr;
|
2011-06-16 07:37:01 +08:00
|
|
|
return Arg;
|
|
|
|
}
|
|
|
|
|
2012-05-09 07:39:44 +08:00
|
|
|
// If we found an identifiable object but it has multiple uses, but they are
|
|
|
|
// trivial uses, we can still consider this to be a single-use value.
|
2011-06-16 07:37:01 +08:00
|
|
|
if (IsObjCIdentifiedObject(Arg)) {
|
2014-03-09 11:16:01 +08:00
|
|
|
for (const User *U : Arg->users())
|
2015-02-19 08:42:38 +08:00
|
|
|
if (!U->use_empty() || GetRCIdentityRoot(U) != Arg)
|
2014-04-25 13:29:35 +08:00
|
|
|
return nullptr;
|
2011-06-16 07:37:01 +08:00
|
|
|
|
|
|
|
return Arg;
|
|
|
|
}
|
|
|
|
|
2014-04-25 13:29:35 +08:00
|
|
|
return nullptr;
|
2011-06-16 07:37:01 +08:00
|
|
|
}
|
|
|
|
|
2013-05-14 07:49:42 +08:00
|
|
|
/// This is a wrapper around getUnderlyingObjCPtr along the lines of
|
|
|
|
/// GetUnderlyingObjects except that it returns early when it sees the first
|
|
|
|
/// alloca.
|
2015-03-10 10:37:25 +08:00
|
|
|
static inline bool AreAnyUnderlyingObjectsAnAlloca(const Value *V,
|
|
|
|
const DataLayout &DL) {
|
2013-05-14 07:49:42 +08:00
|
|
|
SmallPtrSet<const Value *, 4> Visited;
|
|
|
|
SmallVector<const Value *, 4> Worklist;
|
|
|
|
Worklist.push_back(V);
|
|
|
|
do {
|
|
|
|
const Value *P = Worklist.pop_back_val();
|
2015-03-10 10:37:25 +08:00
|
|
|
P = GetUnderlyingObjCPtr(P, DL);
|
2013-05-14 14:40:10 +08:00
|
|
|
|
2013-05-14 07:49:42 +08:00
|
|
|
if (isa<AllocaInst>(P))
|
|
|
|
return true;
|
2013-05-14 14:40:10 +08:00
|
|
|
|
2014-11-19 15:49:26 +08:00
|
|
|
if (!Visited.insert(P).second)
|
2013-05-14 07:49:42 +08:00
|
|
|
continue;
|
2013-05-14 14:40:10 +08:00
|
|
|
|
2013-05-14 07:49:42 +08:00
|
|
|
if (const SelectInst *SI = dyn_cast<const SelectInst>(P)) {
|
|
|
|
Worklist.push_back(SI->getTrueValue());
|
|
|
|
Worklist.push_back(SI->getFalseValue());
|
|
|
|
continue;
|
|
|
|
}
|
2013-05-14 14:40:10 +08:00
|
|
|
|
2013-05-14 07:49:42 +08:00
|
|
|
if (const PHINode *PN = dyn_cast<const PHINode>(P)) {
|
2015-05-13 04:05:31 +08:00
|
|
|
for (Value *IncValue : PN->incoming_values())
|
|
|
|
Worklist.push_back(IncValue);
|
2013-05-14 07:49:42 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} while (!Worklist.empty());
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-01-14 08:35:14 +08:00
|
|
|
/// @}
|
|
|
|
///
|
|
|
|
/// \defgroup ARCOpt ARC Optimization.
|
|
|
|
/// @{
|
2011-06-16 07:37:01 +08:00
|
|
|
|
|
|
|
// TODO: On code like this:
|
|
|
|
//
|
|
|
|
// objc_retain(%x)
|
|
|
|
// stuff_that_cannot_release()
|
|
|
|
// objc_autorelease(%x)
|
|
|
|
// stuff_that_cannot_release()
|
|
|
|
// objc_retain(%x)
|
|
|
|
// stuff_that_cannot_release()
|
|
|
|
// objc_autorelease(%x)
|
|
|
|
//
|
|
|
|
// The second retain and autorelease can be deleted.
|
|
|
|
|
|
|
|
// TODO: It should be possible to delete
|
|
|
|
// objc_autoreleasePoolPush and objc_autoreleasePoolPop
|
|
|
|
// pairs if nothing is actually autoreleased between them. Also, autorelease
|
|
|
|
// calls followed by objc_autoreleasePoolPop calls (perhaps in ObjC++ code
|
|
|
|
// after inlining) can be turned into plain release calls.
|
|
|
|
|
|
|
|
// TODO: Critical-edge splitting. If the optimial insertion point is
|
|
|
|
// a critical edge, the current algorithm has to fail, because it doesn't
|
|
|
|
// know how to split edges. It should be possible to make the optimizer
|
|
|
|
// think in terms of edges, rather than blocks, and then split critical
|
|
|
|
// edges on demand.
|
|
|
|
|
|
|
|
// TODO: OptimizeSequences could generalized to be Interprocedural.
|
|
|
|
|
|
|
|
// TODO: Recognize that a bunch of other objc runtime calls have
|
|
|
|
// non-escaping arguments and non-releasing arguments, and may be
|
|
|
|
// non-autoreleasing.
|
|
|
|
|
|
|
|
// TODO: Sink autorelease calls as far as possible. Unfortunately we
|
|
|
|
// usually can't sink them past other calls, which would be the main
|
|
|
|
// case where it would be useful.
|
|
|
|
|
2011-08-19 08:26:36 +08:00
|
|
|
// TODO: The pointer returned from objc_loadWeakRetained is retained.
|
|
|
|
|
|
|
|
// TODO: Delete release+retain pairs (rare).
|
2011-06-21 07:20:43 +08:00
|
|
|
|
2011-06-16 07:37:01 +08:00
|
|
|
STATISTIC(NumNoops, "Number of no-op objc calls eliminated");
|
|
|
|
STATISTIC(NumPartialNoops, "Number of partially no-op objc calls eliminated");
|
|
|
|
STATISTIC(NumAutoreleases,"Number of autoreleases converted to releases");
|
|
|
|
STATISTIC(NumRets, "Number of return value forwarding "
|
2013-05-16 01:43:03 +08:00
|
|
|
"retain+autoreleases eliminated");
|
2011-06-16 07:37:01 +08:00
|
|
|
STATISTIC(NumRRs, "Number of retain+release paths eliminated");
|
|
|
|
STATISTIC(NumPeeps, "Number of calls peephole-optimized");
|
2013-05-14 05:10:49 +08:00
|
|
|
#ifndef NDEBUG
|
2013-04-29 14:16:57 +08:00
|
|
|
STATISTIC(NumRetainsBeforeOpt,
|
2013-05-16 01:43:03 +08:00
|
|
|
"Number of retains before optimization");
|
2013-04-29 14:16:57 +08:00
|
|
|
STATISTIC(NumReleasesBeforeOpt,
|
2013-05-16 01:43:03 +08:00
|
|
|
"Number of releases before optimization");
|
2013-04-29 14:16:57 +08:00
|
|
|
STATISTIC(NumRetainsAfterOpt,
|
2013-05-16 01:43:03 +08:00
|
|
|
"Number of retains after optimization");
|
2013-04-29 14:16:57 +08:00
|
|
|
STATISTIC(NumReleasesAfterOpt,
|
2013-05-16 01:43:03 +08:00
|
|
|
"Number of releases after optimization");
|
2013-04-29 15:29:08 +08:00
|
|
|
#endif
|
2011-06-16 07:37:01 +08:00
|
|
|
|
|
|
|
namespace {
|
2013-01-14 08:35:14 +08:00
|
|
|
/// \brief Per-BasicBlock state.
|
2011-06-16 07:37:01 +08:00
|
|
|
class BBState {
|
2013-01-14 08:35:14 +08:00
|
|
|
/// The number of unique control paths from the entry which can reach this
|
|
|
|
/// block.
|
2011-06-16 07:37:01 +08:00
|
|
|
unsigned TopDownPathCount;
|
|
|
|
|
2013-01-14 08:35:14 +08:00
|
|
|
/// The number of unique control paths to exits from this block.
|
2011-06-16 07:37:01 +08:00
|
|
|
unsigned BottomUpPathCount;
|
|
|
|
|
2013-01-14 08:35:14 +08:00
|
|
|
/// The top-down traversal uses this to record information known about a
|
|
|
|
/// pointer at the bottom of each block.
|
2015-03-06 08:34:36 +08:00
|
|
|
BlotMapVector<const Value *, TopDownPtrState> PerPtrTopDown;
|
2011-06-16 07:37:01 +08:00
|
|
|
|
2013-01-14 08:35:14 +08:00
|
|
|
/// The bottom-up traversal uses this to record information known about a
|
|
|
|
/// pointer at the top of each block.
|
2015-03-06 08:34:36 +08:00
|
|
|
BlotMapVector<const Value *, BottomUpPtrState> PerPtrBottomUp;
|
2011-06-16 07:37:01 +08:00
|
|
|
|
2013-01-14 08:35:14 +08:00
|
|
|
/// Effective predecessors of the current block ignoring ignorable edges and
|
|
|
|
/// ignored backedges.
|
2012-04-25 06:53:18 +08:00
|
|
|
SmallVector<BasicBlock *, 2> Preds;
|
2015-03-06 08:34:36 +08:00
|
|
|
|
2013-01-14 08:35:14 +08:00
|
|
|
/// Effective successors of the current block ignoring ignorable edges and
|
|
|
|
/// ignored backedges.
|
2012-04-25 06:53:18 +08:00
|
|
|
SmallVector<BasicBlock *, 2> Succs;
|
|
|
|
|
2011-06-16 07:37:01 +08:00
|
|
|
public:
|
2013-08-10 07:22:27 +08:00
|
|
|
static const unsigned OverflowOccurredValue;
|
|
|
|
|
|
|
|
BBState() : TopDownPathCount(0), BottomUpPathCount(0) { }
|
2011-06-16 07:37:01 +08:00
|
|
|
|
2015-03-06 08:34:36 +08:00
|
|
|
typedef decltype(PerPtrTopDown)::iterator top_down_ptr_iterator;
|
|
|
|
typedef decltype(PerPtrTopDown)::const_iterator const_top_down_ptr_iterator;
|
2011-06-16 07:37:01 +08:00
|
|
|
|
2015-03-06 08:34:36 +08:00
|
|
|
top_down_ptr_iterator top_down_ptr_begin() { return PerPtrTopDown.begin(); }
|
|
|
|
top_down_ptr_iterator top_down_ptr_end() { return PerPtrTopDown.end(); }
|
|
|
|
const_top_down_ptr_iterator top_down_ptr_begin() const {
|
2011-06-16 07:37:01 +08:00
|
|
|
return PerPtrTopDown.begin();
|
|
|
|
}
|
2015-03-06 08:34:36 +08:00
|
|
|
const_top_down_ptr_iterator top_down_ptr_end() const {
|
2011-06-16 07:37:01 +08:00
|
|
|
return PerPtrTopDown.end();
|
|
|
|
}
|
2015-03-16 15:02:39 +08:00
|
|
|
bool hasTopDownPtrs() const {
|
|
|
|
return !PerPtrTopDown.empty();
|
|
|
|
}
|
2011-06-16 07:37:01 +08:00
|
|
|
|
2015-03-06 08:34:36 +08:00
|
|
|
typedef decltype(PerPtrBottomUp)::iterator bottom_up_ptr_iterator;
|
|
|
|
typedef decltype(
|
|
|
|
PerPtrBottomUp)::const_iterator const_bottom_up_ptr_iterator;
|
|
|
|
|
|
|
|
bottom_up_ptr_iterator bottom_up_ptr_begin() {
|
2011-06-16 07:37:01 +08:00
|
|
|
return PerPtrBottomUp.begin();
|
|
|
|
}
|
2015-03-06 08:34:36 +08:00
|
|
|
bottom_up_ptr_iterator bottom_up_ptr_end() { return PerPtrBottomUp.end(); }
|
|
|
|
const_bottom_up_ptr_iterator bottom_up_ptr_begin() const {
|
|
|
|
return PerPtrBottomUp.begin();
|
|
|
|
}
|
|
|
|
const_bottom_up_ptr_iterator bottom_up_ptr_end() const {
|
2011-06-16 07:37:01 +08:00
|
|
|
return PerPtrBottomUp.end();
|
|
|
|
}
|
2015-03-16 15:02:39 +08:00
|
|
|
bool hasBottomUpPtrs() const {
|
|
|
|
return !PerPtrBottomUp.empty();
|
|
|
|
}
|
2011-06-16 07:37:01 +08:00
|
|
|
|
2013-01-14 08:35:14 +08:00
|
|
|
/// Mark this block as being an entry block, which has one path from the
|
|
|
|
/// entry by definition.
|
2011-06-16 07:37:01 +08:00
|
|
|
void SetAsEntry() { TopDownPathCount = 1; }
|
|
|
|
|
2013-01-14 08:35:14 +08:00
|
|
|
/// Mark this block as being an exit block, which has one path to an exit by
|
|
|
|
/// definition.
|
2011-06-16 07:37:01 +08:00
|
|
|
void SetAsExit() { BottomUpPathCount = 1; }
|
|
|
|
|
2013-05-14 03:40:39 +08:00
|
|
|
/// Attempt to find the PtrState object describing the top down state for
|
|
|
|
/// pointer Arg. Return a new initialized PtrState describing the top down
|
|
|
|
/// state for Arg if we do not find one.
|
2015-03-06 08:34:36 +08:00
|
|
|
TopDownPtrState &getPtrTopDownState(const Value *Arg) {
|
2011-06-16 07:37:01 +08:00
|
|
|
return PerPtrTopDown[Arg];
|
|
|
|
}
|
|
|
|
|
2013-05-14 03:40:39 +08:00
|
|
|
/// Attempt to find the PtrState object describing the bottom up state for
|
|
|
|
/// pointer Arg. Return a new initialized PtrState describing the bottom up
|
|
|
|
/// state for Arg if we do not find one.
|
2015-03-06 08:34:36 +08:00
|
|
|
BottomUpPtrState &getPtrBottomUpState(const Value *Arg) {
|
2011-06-16 07:37:01 +08:00
|
|
|
return PerPtrBottomUp[Arg];
|
|
|
|
}
|
|
|
|
|
2013-05-14 07:49:42 +08:00
|
|
|
/// Attempt to find the PtrState object describing the bottom up state for
|
|
|
|
/// pointer Arg.
|
2015-03-06 08:34:36 +08:00
|
|
|
bottom_up_ptr_iterator findPtrBottomUpState(const Value *Arg) {
|
2013-05-14 07:49:42 +08:00
|
|
|
return PerPtrBottomUp.find(Arg);
|
|
|
|
}
|
|
|
|
|
2011-06-16 07:37:01 +08:00
|
|
|
void clearBottomUpPointers() {
|
2011-08-05 02:40:26 +08:00
|
|
|
PerPtrBottomUp.clear();
|
2011-06-16 07:37:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void clearTopDownPointers() {
|
|
|
|
PerPtrTopDown.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void InitFromPred(const BBState &Other);
|
|
|
|
void InitFromSucc(const BBState &Other);
|
|
|
|
void MergePred(const BBState &Other);
|
|
|
|
void MergeSucc(const BBState &Other);
|
|
|
|
|
2013-06-07 14:16:49 +08:00
|
|
|
/// Compute the number of possible unique paths from an entry to an exit
|
2013-01-14 08:35:14 +08:00
|
|
|
/// which pass through this block. This is only valid after both the
|
|
|
|
/// top-down and bottom-up traversals are complete.
|
2013-06-07 14:16:49 +08:00
|
|
|
///
|
2014-01-25 01:20:08 +08:00
|
|
|
/// Returns true if overflow occurred. Returns false if overflow did not
|
2013-06-07 14:16:49 +08:00
|
|
|
/// occur.
|
|
|
|
bool GetAllPathCountWithOverflow(unsigned &PathCount) const {
|
2013-08-10 07:22:27 +08:00
|
|
|
if (TopDownPathCount == OverflowOccurredValue ||
|
|
|
|
BottomUpPathCount == OverflowOccurredValue)
|
|
|
|
return true;
|
2013-06-07 14:16:49 +08:00
|
|
|
unsigned long long Product =
|
|
|
|
(unsigned long long)TopDownPathCount*BottomUpPathCount;
|
2014-01-25 01:20:08 +08:00
|
|
|
// Overflow occurred if any of the upper bits of Product are set or if all
|
2013-08-10 07:22:27 +08:00
|
|
|
// the lower bits of Product are all set.
|
|
|
|
return (Product >> 32) ||
|
|
|
|
((PathCount = Product) == OverflowOccurredValue);
|
2011-06-16 07:37:01 +08:00
|
|
|
}
|
2011-08-12 08:26:31 +08:00
|
|
|
|
2012-04-25 06:53:18 +08:00
|
|
|
// Specialized CFG utilities.
|
2012-04-28 02:56:31 +08:00
|
|
|
typedef SmallVectorImpl<BasicBlock *>::const_iterator edge_iterator;
|
2013-08-08 07:56:34 +08:00
|
|
|
edge_iterator pred_begin() const { return Preds.begin(); }
|
|
|
|
edge_iterator pred_end() const { return Preds.end(); }
|
|
|
|
edge_iterator succ_begin() const { return Succs.begin(); }
|
|
|
|
edge_iterator succ_end() const { return Succs.end(); }
|
2012-04-25 06:53:18 +08:00
|
|
|
|
|
|
|
void addSucc(BasicBlock *Succ) { Succs.push_back(Succ); }
|
|
|
|
void addPred(BasicBlock *Pred) { Preds.push_back(Pred); }
|
|
|
|
|
|
|
|
bool isExit() const { return Succs.empty(); }
|
2011-06-16 07:37:01 +08:00
|
|
|
};
|
2013-08-10 07:22:27 +08:00
|
|
|
|
|
|
|
const unsigned BBState::OverflowOccurredValue = 0xffffffff;
|
2015-06-23 17:49:53 +08:00
|
|
|
}
|
2011-06-16 07:37:01 +08:00
|
|
|
|
2015-03-16 15:02:39 +08:00
|
|
|
namespace llvm {
|
2015-03-16 16:00:27 +08:00
|
|
|
raw_ostream &operator<<(raw_ostream &OS,
|
|
|
|
BBState &BBState) LLVM_ATTRIBUTE_UNUSED;
|
2015-03-16 15:02:39 +08:00
|
|
|
}
|
|
|
|
|
2011-06-16 07:37:01 +08:00
|
|
|
void BBState::InitFromPred(const BBState &Other) {
|
|
|
|
PerPtrTopDown = Other.PerPtrTopDown;
|
|
|
|
TopDownPathCount = Other.TopDownPathCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BBState::InitFromSucc(const BBState &Other) {
|
|
|
|
PerPtrBottomUp = Other.PerPtrBottomUp;
|
|
|
|
BottomUpPathCount = Other.BottomUpPathCount;
|
|
|
|
}
|
|
|
|
|
2013-01-14 08:35:14 +08:00
|
|
|
/// The top-down traversal uses this to merge information about predecessors to
|
|
|
|
/// form the initial state for a new block.
|
2011-06-16 07:37:01 +08:00
|
|
|
void BBState::MergePred(const BBState &Other) {
|
2013-08-10 07:22:27 +08:00
|
|
|
if (TopDownPathCount == OverflowOccurredValue)
|
|
|
|
return;
|
|
|
|
|
2011-06-16 07:37:01 +08:00
|
|
|
// Other.TopDownPathCount can be 0, in which case it is either dead or a
|
|
|
|
// loop backedge. Loop backedges are special.
|
|
|
|
TopDownPathCount += Other.TopDownPathCount;
|
|
|
|
|
2013-08-10 07:22:27 +08:00
|
|
|
// In order to be consistent, we clear the top down pointers when by adding
|
|
|
|
// TopDownPathCount becomes OverflowOccurredValue even though "true" overflow
|
2014-01-25 01:20:08 +08:00
|
|
|
// has not occurred.
|
2013-08-10 07:22:27 +08:00
|
|
|
if (TopDownPathCount == OverflowOccurredValue) {
|
|
|
|
clearTopDownPointers();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-01-14 09:47:53 +08:00
|
|
|
// Check for overflow. If we have overflow, fall back to conservative
|
|
|
|
// behavior.
|
2012-09-13 04:45:17 +08:00
|
|
|
if (TopDownPathCount < Other.TopDownPathCount) {
|
2013-08-10 07:22:27 +08:00
|
|
|
TopDownPathCount = OverflowOccurredValue;
|
2012-09-13 04:45:17 +08:00
|
|
|
clearTopDownPointers();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-06-16 07:37:01 +08:00
|
|
|
// For each entry in the other set, if our set has an entry with the same key,
|
|
|
|
// merge the entries. Otherwise, copy the entry and merge it with an empty
|
|
|
|
// entry.
|
2015-03-06 07:29:06 +08:00
|
|
|
for (auto MI = Other.top_down_ptr_begin(), ME = Other.top_down_ptr_end();
|
|
|
|
MI != ME; ++MI) {
|
2015-03-06 08:34:36 +08:00
|
|
|
auto Pair = PerPtrTopDown.insert(*MI);
|
|
|
|
Pair.first->second.Merge(Pair.second ? TopDownPtrState() : MI->second,
|
2011-06-16 07:37:01 +08:00
|
|
|
/*TopDown=*/true);
|
|
|
|
}
|
|
|
|
|
2011-08-12 05:06:32 +08:00
|
|
|
// For each entry in our set, if the other set doesn't have an entry with the
|
2011-06-16 07:37:01 +08:00
|
|
|
// same key, force it to merge with an empty entry.
|
2015-03-06 07:29:06 +08:00
|
|
|
for (auto MI = top_down_ptr_begin(), ME = top_down_ptr_end(); MI != ME; ++MI)
|
2011-06-16 07:37:01 +08:00
|
|
|
if (Other.PerPtrTopDown.find(MI->first) == Other.PerPtrTopDown.end())
|
2015-03-06 08:34:36 +08:00
|
|
|
MI->second.Merge(TopDownPtrState(), /*TopDown=*/true);
|
2011-06-16 07:37:01 +08:00
|
|
|
}
|
|
|
|
|
2013-01-14 08:35:14 +08:00
|
|
|
/// The bottom-up traversal uses this to merge information about successors to
|
|
|
|
/// form the initial state for a new block.
|
2011-06-16 07:37:01 +08:00
|
|
|
void BBState::MergeSucc(const BBState &Other) {
|
2013-08-10 07:22:27 +08:00
|
|
|
if (BottomUpPathCount == OverflowOccurredValue)
|
|
|
|
return;
|
|
|
|
|
2011-06-16 07:37:01 +08:00
|
|
|
// Other.BottomUpPathCount can be 0, in which case it is either dead or a
|
|
|
|
// loop backedge. Loop backedges are special.
|
|
|
|
BottomUpPathCount += Other.BottomUpPathCount;
|
|
|
|
|
2013-08-10 07:22:27 +08:00
|
|
|
// In order to be consistent, we clear the top down pointers when by adding
|
|
|
|
// BottomUpPathCount becomes OverflowOccurredValue even though "true" overflow
|
2014-01-25 01:20:08 +08:00
|
|
|
// has not occurred.
|
2013-08-10 07:22:27 +08:00
|
|
|
if (BottomUpPathCount == OverflowOccurredValue) {
|
|
|
|
clearBottomUpPointers();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-01-14 09:47:53 +08:00
|
|
|
// Check for overflow. If we have overflow, fall back to conservative
|
|
|
|
// behavior.
|
2012-09-13 04:45:17 +08:00
|
|
|
if (BottomUpPathCount < Other.BottomUpPathCount) {
|
2013-08-10 07:22:27 +08:00
|
|
|
BottomUpPathCount = OverflowOccurredValue;
|
2012-09-13 04:45:17 +08:00
|
|
|
clearBottomUpPointers();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-06-16 07:37:01 +08:00
|
|
|
// For each entry in the other set, if our set has an entry with the
|
|
|
|
// same key, merge the entries. Otherwise, copy the entry and merge
|
|
|
|
// it with an empty entry.
|
2015-03-06 07:29:06 +08:00
|
|
|
for (auto MI = Other.bottom_up_ptr_begin(), ME = Other.bottom_up_ptr_end();
|
|
|
|
MI != ME; ++MI) {
|
2015-03-06 08:34:36 +08:00
|
|
|
auto Pair = PerPtrBottomUp.insert(*MI);
|
|
|
|
Pair.first->second.Merge(Pair.second ? BottomUpPtrState() : MI->second,
|
2011-06-16 07:37:01 +08:00
|
|
|
/*TopDown=*/false);
|
|
|
|
}
|
|
|
|
|
2011-08-12 05:06:32 +08:00
|
|
|
// For each entry in our set, if the other set doesn't have an entry
|
2011-06-16 07:37:01 +08:00
|
|
|
// with the same key, force it to merge with an empty entry.
|
2015-03-06 07:29:06 +08:00
|
|
|
for (auto MI = bottom_up_ptr_begin(), ME = bottom_up_ptr_end(); MI != ME;
|
|
|
|
++MI)
|
2011-06-16 07:37:01 +08:00
|
|
|
if (Other.PerPtrBottomUp.find(MI->first) == Other.PerPtrBottomUp.end())
|
2015-03-06 08:34:36 +08:00
|
|
|
MI->second.Merge(BottomUpPtrState(), /*TopDown=*/false);
|
2011-06-16 07:37:01 +08:00
|
|
|
}
|
|
|
|
|
2015-03-16 15:02:39 +08:00
|
|
|
raw_ostream &llvm::operator<<(raw_ostream &OS, BBState &BBInfo) {
|
|
|
|
// Dump the pointers we are tracking.
|
|
|
|
OS << " TopDown State:\n";
|
|
|
|
if (!BBInfo.hasTopDownPtrs()) {
|
|
|
|
DEBUG(llvm::dbgs() << " NONE!\n");
|
|
|
|
} else {
|
|
|
|
for (auto I = BBInfo.top_down_ptr_begin(), E = BBInfo.top_down_ptr_end();
|
|
|
|
I != E; ++I) {
|
|
|
|
const PtrState &P = I->second;
|
|
|
|
OS << " Ptr: " << *I->first
|
|
|
|
<< "\n KnownSafe: " << (P.IsKnownSafe()?"true":"false")
|
|
|
|
<< "\n ImpreciseRelease: "
|
|
|
|
<< (P.IsTrackingImpreciseReleases()?"true":"false") << "\n"
|
|
|
|
<< " HasCFGHazards: "
|
|
|
|
<< (P.IsCFGHazardAfflicted()?"true":"false") << "\n"
|
|
|
|
<< " KnownPositive: "
|
|
|
|
<< (P.HasKnownPositiveRefCount()?"true":"false") << "\n"
|
|
|
|
<< " Seq: "
|
|
|
|
<< P.GetSeq() << "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
OS << " BottomUp State:\n";
|
|
|
|
if (!BBInfo.hasBottomUpPtrs()) {
|
|
|
|
DEBUG(llvm::dbgs() << " NONE!\n");
|
|
|
|
} else {
|
|
|
|
for (auto I = BBInfo.bottom_up_ptr_begin(), E = BBInfo.bottom_up_ptr_end();
|
|
|
|
I != E; ++I) {
|
|
|
|
const PtrState &P = I->second;
|
|
|
|
OS << " Ptr: " << *I->first
|
|
|
|
<< "\n KnownSafe: " << (P.IsKnownSafe()?"true":"false")
|
|
|
|
<< "\n ImpreciseRelease: "
|
|
|
|
<< (P.IsTrackingImpreciseReleases()?"true":"false") << "\n"
|
|
|
|
<< " HasCFGHazards: "
|
|
|
|
<< (P.IsCFGHazardAfflicted()?"true":"false") << "\n"
|
|
|
|
<< " KnownPositive: "
|
|
|
|
<< (P.HasKnownPositiveRefCount()?"true":"false") << "\n"
|
|
|
|
<< " Seq: "
|
|
|
|
<< P.GetSeq() << "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return OS;
|
|
|
|
}
|
|
|
|
|
2011-06-16 07:37:01 +08:00
|
|
|
namespace {
|
2015-03-06 08:34:33 +08:00
|
|
|
|
2013-01-14 08:35:14 +08:00
|
|
|
/// \brief The main ARC optimization pass.
|
2011-06-16 07:37:01 +08:00
|
|
|
class ObjCARCOpt : public FunctionPass {
|
|
|
|
bool Changed;
|
|
|
|
ProvenanceAnalysis PA;
|
2015-03-06 08:34:33 +08:00
|
|
|
|
|
|
|
/// A cache of references to runtime entry point constants.
|
2013-07-06 09:39:23 +08:00
|
|
|
ARCRuntimeEntryPoints EP;
|
2011-06-16 07:37:01 +08:00
|
|
|
|
2015-03-06 08:34:33 +08:00
|
|
|
/// A cache of MDKinds that can be passed into other functions to propagate
|
|
|
|
/// MDKind identifiers.
|
|
|
|
ARCMDKindCache MDKindCache;
|
|
|
|
|
2013-05-25 04:44:02 +08:00
|
|
|
// This is used to track if a pointer is stored into an alloca.
|
|
|
|
DenseSet<const Value *> MultiOwnersSet;
|
|
|
|
|
2013-01-14 08:35:14 +08:00
|
|
|
/// A flag indicating whether this optimization pass should run.
|
2011-06-21 07:20:43 +08:00
|
|
|
bool Run;
|
|
|
|
|
2015-08-09 02:27:36 +08:00
|
|
|
/// Flags which determine whether each of the interesting runtime functions
|
2013-01-14 08:35:14 +08:00
|
|
|
/// is in fact used in the current function.
|
2011-06-16 07:37:01 +08:00
|
|
|
unsigned UsedInThisFunction;
|
|
|
|
|
|
|
|
bool OptimizeRetainRVCall(Function &F, Instruction *RetainRV);
|
2013-01-12 09:25:19 +08:00
|
|
|
void OptimizeAutoreleaseRVCall(Function &F, Instruction *AutoreleaseRV,
|
2015-02-20 03:51:32 +08:00
|
|
|
ARCInstKind &Class);
|
2011-06-16 07:37:01 +08:00
|
|
|
void OptimizeIndividualCalls(Function &F);
|
|
|
|
|
|
|
|
void CheckForCFGHazards(const BasicBlock *BB,
|
|
|
|
DenseMap<const BasicBlock *, BBState> &BBStates,
|
|
|
|
BBState &MyStates) const;
|
2015-03-06 07:28:58 +08:00
|
|
|
bool VisitInstructionBottomUp(Instruction *Inst, BasicBlock *BB,
|
|
|
|
BlotMapVector<Value *, RRInfo> &Retains,
|
2012-03-23 02:24:56 +08:00
|
|
|
BBState &MyStates);
|
2011-06-16 07:37:01 +08:00
|
|
|
bool VisitBottomUp(BasicBlock *BB,
|
|
|
|
DenseMap<const BasicBlock *, BBState> &BBStates,
|
2015-03-06 07:28:58 +08:00
|
|
|
BlotMapVector<Value *, RRInfo> &Retains);
|
2012-03-23 02:24:56 +08:00
|
|
|
bool VisitInstructionTopDown(Instruction *Inst,
|
|
|
|
DenseMap<Value *, RRInfo> &Releases,
|
|
|
|
BBState &MyStates);
|
2011-06-16 07:37:01 +08:00
|
|
|
bool VisitTopDown(BasicBlock *BB,
|
|
|
|
DenseMap<const BasicBlock *, BBState> &BBStates,
|
|
|
|
DenseMap<Value *, RRInfo> &Releases);
|
2015-03-06 07:28:58 +08:00
|
|
|
bool Visit(Function &F, DenseMap<const BasicBlock *, BBState> &BBStates,
|
|
|
|
BlotMapVector<Value *, RRInfo> &Retains,
|
2011-06-16 07:37:01 +08:00
|
|
|
DenseMap<Value *, RRInfo> &Releases);
|
|
|
|
|
|
|
|
void MoveCalls(Value *Arg, RRInfo &RetainsToMove, RRInfo &ReleasesToMove,
|
2015-03-06 07:28:58 +08:00
|
|
|
BlotMapVector<Value *, RRInfo> &Retains,
|
2011-06-16 07:37:01 +08:00
|
|
|
DenseMap<Value *, RRInfo> &Releases,
|
2015-03-06 07:28:58 +08:00
|
|
|
SmallVectorImpl<Instruction *> &DeadInsts, Module *M);
|
2011-06-16 07:37:01 +08:00
|
|
|
|
2015-03-16 15:02:30 +08:00
|
|
|
bool
|
|
|
|
PairUpRetainsAndReleases(DenseMap<const BasicBlock *, BBState> &BBStates,
|
|
|
|
BlotMapVector<Value *, RRInfo> &Retains,
|
|
|
|
DenseMap<Value *, RRInfo> &Releases, Module *M,
|
|
|
|
SmallVectorImpl<Instruction *> &NewRetains,
|
|
|
|
SmallVectorImpl<Instruction *> &NewReleases,
|
|
|
|
SmallVectorImpl<Instruction *> &DeadInsts,
|
|
|
|
RRInfo &RetainsToMove, RRInfo &ReleasesToMove,
|
|
|
|
Value *Arg, bool KnownSafe,
|
|
|
|
bool &AnyPairsCompletelyEliminated);
|
2013-01-23 05:49:00 +08:00
|
|
|
|
2011-06-16 07:37:01 +08:00
|
|
|
bool PerformCodePlacement(DenseMap<const BasicBlock *, BBState> &BBStates,
|
2015-03-06 07:28:58 +08:00
|
|
|
BlotMapVector<Value *, RRInfo> &Retains,
|
|
|
|
DenseMap<Value *, RRInfo> &Releases, Module *M);
|
2011-06-16 07:37:01 +08:00
|
|
|
|
|
|
|
void OptimizeWeakCalls(Function &F);
|
|
|
|
|
|
|
|
bool OptimizeSequences(Function &F);
|
|
|
|
|
|
|
|
void OptimizeReturns(Function &F);
|
|
|
|
|
2013-04-29 14:16:57 +08:00
|
|
|
#ifndef NDEBUG
|
|
|
|
void GatherStatistics(Function &F, bool AfterOptimization = false);
|
|
|
|
#endif
|
|
|
|
|
2014-03-05 17:10:37 +08:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override;
|
|
|
|
bool doInitialization(Module &M) override;
|
|
|
|
bool runOnFunction(Function &F) override;
|
|
|
|
void releaseMemory() override;
|
2011-06-16 07:37:01 +08:00
|
|
|
|
|
|
|
public:
|
|
|
|
static char ID;
|
|
|
|
ObjCARCOpt() : FunctionPass(ID) {
|
|
|
|
initializeObjCARCOptPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
|
|
|
};
|
2015-06-23 17:49:53 +08:00
|
|
|
}
|
2011-06-16 07:37:01 +08:00
|
|
|
|
|
|
|
char ObjCARCOpt::ID = 0;
|
|
|
|
INITIALIZE_PASS_BEGIN(ObjCARCOpt,
|
|
|
|
"objc-arc", "ObjC ARC optimization", false, false)
|
[PM/AA] Rebuild LLVM's alias analysis infrastructure in a way compatible
with the new pass manager, and no longer relying on analysis groups.
This builds essentially a ground-up new AA infrastructure stack for
LLVM. The core ideas are the same that are used throughout the new pass
manager: type erased polymorphism and direct composition. The design is
as follows:
- FunctionAAResults is a type-erasing alias analysis results aggregation
interface to walk a single query across a range of results from
different alias analyses. Currently this is function-specific as we
always assume that aliasing queries are *within* a function.
- AAResultBase is a CRTP utility providing stub implementations of
various parts of the alias analysis result concept, notably in several
cases in terms of other more general parts of the interface. This can
be used to implement only a narrow part of the interface rather than
the entire interface. This isn't really ideal, this logic should be
hoisted into FunctionAAResults as currently it will cause
a significant amount of redundant work, but it faithfully models the
behavior of the prior infrastructure.
- All the alias analysis passes are ported to be wrapper passes for the
legacy PM and new-style analysis passes for the new PM with a shared
result object. In some cases (most notably CFL), this is an extremely
naive approach that we should revisit when we can specialize for the
new pass manager.
- BasicAA has been restructured to reflect that it is much more
fundamentally a function analysis because it uses dominator trees and
loop info that need to be constructed for each function.
All of the references to getting alias analysis results have been
updated to use the new aggregation interface. All the preservation and
other pass management code has been updated accordingly.
The way the FunctionAAResultsWrapperPass works is to detect the
available alias analyses when run, and add them to the results object.
This means that we should be able to continue to respect when various
passes are added to the pipeline, for example adding CFL or adding TBAA
passes should just cause their results to be available and to get folded
into this. The exception to this rule is BasicAA which really needs to
be a function pass due to using dominator trees and loop info. As
a consequence, the FunctionAAResultsWrapperPass directly depends on
BasicAA and always includes it in the aggregation.
This has significant implications for preserving analyses. Generally,
most passes shouldn't bother preserving FunctionAAResultsWrapperPass
because rebuilding the results just updates the set of known AA passes.
The exception to this rule are LoopPass instances which need to preserve
all the function analyses that the loop pass manager will end up
needing. This means preserving both BasicAAWrapperPass and the
aggregating FunctionAAResultsWrapperPass.
Now, when preserving an alias analysis, you do so by directly preserving
that analysis. This is only necessary for non-immutable-pass-provided
alias analyses though, and there are only three of interest: BasicAA,
GlobalsAA (formerly GlobalsModRef), and SCEVAA. Usually BasicAA is
preserved when needed because it (like DominatorTree and LoopInfo) is
marked as a CFG-only pass. I've expanded GlobalsAA into the preserved
set everywhere we previously were preserving all of AliasAnalysis, and
I've added SCEVAA in the intersection of that with where we preserve
SCEV itself.
One significant challenge to all of this is that the CGSCC passes were
actually using the alias analysis implementations by taking advantage of
a pretty amazing set of loop holes in the old pass manager's analysis
management code which allowed analysis groups to slide through in many
cases. Moving away from analysis groups makes this problem much more
obvious. To fix it, I've leveraged the flexibility the design of the new
PM components provides to just directly construct the relevant alias
analyses for the relevant functions in the IPO passes that need them.
This is a bit hacky, but should go away with the new pass manager, and
is already in many ways cleaner than the prior state.
Another significant challenge is that various facilities of the old
alias analysis infrastructure just don't fit any more. The most
significant of these is the alias analysis 'counter' pass. That pass
relied on the ability to snoop on AA queries at different points in the
analysis group chain. Instead, I'm planning to build printing
functionality directly into the aggregation layer. I've not included
that in this patch merely to keep it smaller.
Note that all of this needs a nearly complete rewrite of the AA
documentation. I'm planning to do that, but I'd like to make sure the
new design settles, and to flesh out a bit more of what it looks like in
the new pass manager first.
Differential Revision: http://reviews.llvm.org/D12080
llvm-svn: 247167
2015-09-10 01:55:00 +08:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(ObjCARCAAWrapperPass)
|
2011-06-16 07:37:01 +08:00
|
|
|
INITIALIZE_PASS_END(ObjCARCOpt,
|
|
|
|
"objc-arc", "ObjC ARC optimization", false, false)
|
|
|
|
|
|
|
|
Pass *llvm::createObjCARCOptPass() {
|
|
|
|
return new ObjCARCOpt();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ObjCARCOpt::getAnalysisUsage(AnalysisUsage &AU) const {
|
[PM/AA] Rebuild LLVM's alias analysis infrastructure in a way compatible
with the new pass manager, and no longer relying on analysis groups.
This builds essentially a ground-up new AA infrastructure stack for
LLVM. The core ideas are the same that are used throughout the new pass
manager: type erased polymorphism and direct composition. The design is
as follows:
- FunctionAAResults is a type-erasing alias analysis results aggregation
interface to walk a single query across a range of results from
different alias analyses. Currently this is function-specific as we
always assume that aliasing queries are *within* a function.
- AAResultBase is a CRTP utility providing stub implementations of
various parts of the alias analysis result concept, notably in several
cases in terms of other more general parts of the interface. This can
be used to implement only a narrow part of the interface rather than
the entire interface. This isn't really ideal, this logic should be
hoisted into FunctionAAResults as currently it will cause
a significant amount of redundant work, but it faithfully models the
behavior of the prior infrastructure.
- All the alias analysis passes are ported to be wrapper passes for the
legacy PM and new-style analysis passes for the new PM with a shared
result object. In some cases (most notably CFL), this is an extremely
naive approach that we should revisit when we can specialize for the
new pass manager.
- BasicAA has been restructured to reflect that it is much more
fundamentally a function analysis because it uses dominator trees and
loop info that need to be constructed for each function.
All of the references to getting alias analysis results have been
updated to use the new aggregation interface. All the preservation and
other pass management code has been updated accordingly.
The way the FunctionAAResultsWrapperPass works is to detect the
available alias analyses when run, and add them to the results object.
This means that we should be able to continue to respect when various
passes are added to the pipeline, for example adding CFL or adding TBAA
passes should just cause their results to be available and to get folded
into this. The exception to this rule is BasicAA which really needs to
be a function pass due to using dominator trees and loop info. As
a consequence, the FunctionAAResultsWrapperPass directly depends on
BasicAA and always includes it in the aggregation.
This has significant implications for preserving analyses. Generally,
most passes shouldn't bother preserving FunctionAAResultsWrapperPass
because rebuilding the results just updates the set of known AA passes.
The exception to this rule are LoopPass instances which need to preserve
all the function analyses that the loop pass manager will end up
needing. This means preserving both BasicAAWrapperPass and the
aggregating FunctionAAResultsWrapperPass.
Now, when preserving an alias analysis, you do so by directly preserving
that analysis. This is only necessary for non-immutable-pass-provided
alias analyses though, and there are only three of interest: BasicAA,
GlobalsAA (formerly GlobalsModRef), and SCEVAA. Usually BasicAA is
preserved when needed because it (like DominatorTree and LoopInfo) is
marked as a CFG-only pass. I've expanded GlobalsAA into the preserved
set everywhere we previously were preserving all of AliasAnalysis, and
I've added SCEVAA in the intersection of that with where we preserve
SCEV itself.
One significant challenge to all of this is that the CGSCC passes were
actually using the alias analysis implementations by taking advantage of
a pretty amazing set of loop holes in the old pass manager's analysis
management code which allowed analysis groups to slide through in many
cases. Moving away from analysis groups makes this problem much more
obvious. To fix it, I've leveraged the flexibility the design of the new
PM components provides to just directly construct the relevant alias
analyses for the relevant functions in the IPO passes that need them.
This is a bit hacky, but should go away with the new pass manager, and
is already in many ways cleaner than the prior state.
Another significant challenge is that various facilities of the old
alias analysis infrastructure just don't fit any more. The most
significant of these is the alias analysis 'counter' pass. That pass
relied on the ability to snoop on AA queries at different points in the
analysis group chain. Instead, I'm planning to build printing
functionality directly into the aggregation layer. I've not included
that in this patch merely to keep it smaller.
Note that all of this needs a nearly complete rewrite of the AA
documentation. I'm planning to do that, but I'd like to make sure the
new design settles, and to flesh out a bit more of what it looks like in
the new pass manager first.
Differential Revision: http://reviews.llvm.org/D12080
llvm-svn: 247167
2015-09-10 01:55:00 +08:00
|
|
|
AU.addRequired<ObjCARCAAWrapperPass>();
|
|
|
|
AU.addRequired<AAResultsWrapperPass>();
|
2011-06-16 07:37:01 +08:00
|
|
|
// ARC optimization doesn't currently split critical edges.
|
|
|
|
AU.setPreservesCFG();
|
|
|
|
}
|
|
|
|
|
2013-01-14 08:35:14 +08:00
|
|
|
/// Turn objc_retainAutoreleasedReturnValue into objc_retain if the operand is
|
|
|
|
/// not a return value. Or, if it can be paired with an
|
|
|
|
/// objc_autoreleaseReturnValue, delete the pair and return true.
|
2011-06-16 07:37:01 +08:00
|
|
|
bool
|
|
|
|
ObjCARCOpt::OptimizeRetainRVCall(Function &F, Instruction *RetainRV) {
|
2012-03-24 02:09:00 +08:00
|
|
|
// Check for the argument being from an immediately preceding call or invoke.
|
2015-02-19 08:42:38 +08:00
|
|
|
const Value *Arg = GetArgRCIdentityRoot(RetainRV);
|
2012-04-28 02:56:31 +08:00
|
|
|
ImmutableCallSite CS(Arg);
|
|
|
|
if (const Instruction *Call = CS.getInstruction()) {
|
2011-06-16 07:37:01 +08:00
|
|
|
if (Call->getParent() == RetainRV->getParent()) {
|
2015-10-20 07:20:14 +08:00
|
|
|
BasicBlock::const_iterator I(Call);
|
2011-06-16 07:37:01 +08:00
|
|
|
++I;
|
2015-10-20 07:20:14 +08:00
|
|
|
while (IsNoopInstruction(&*I))
|
|
|
|
++I;
|
2011-06-16 07:37:01 +08:00
|
|
|
if (&*I == RetainRV)
|
|
|
|
return false;
|
2012-04-28 02:56:31 +08:00
|
|
|
} else if (const InvokeInst *II = dyn_cast<InvokeInst>(Call)) {
|
2012-03-24 02:09:00 +08:00
|
|
|
BasicBlock *RetainRVParent = RetainRV->getParent();
|
|
|
|
if (II->getNormalDest() == RetainRVParent) {
|
2012-04-28 02:56:31 +08:00
|
|
|
BasicBlock::const_iterator I = RetainRVParent->begin();
|
2015-10-20 07:20:14 +08:00
|
|
|
while (IsNoopInstruction(&*I))
|
|
|
|
++I;
|
2012-03-24 02:09:00 +08:00
|
|
|
if (&*I == RetainRV)
|
|
|
|
return false;
|
|
|
|
}
|
2011-06-16 07:37:01 +08:00
|
|
|
}
|
2012-03-24 02:09:00 +08:00
|
|
|
}
|
2011-06-16 07:37:01 +08:00
|
|
|
|
|
|
|
// Check for being preceded by an objc_autoreleaseReturnValue on the same
|
|
|
|
// pointer. In this case, we can delete the pair.
|
2015-10-20 07:20:14 +08:00
|
|
|
BasicBlock::iterator I = RetainRV->getIterator(),
|
|
|
|
Begin = RetainRV->getParent()->begin();
|
2011-06-16 07:37:01 +08:00
|
|
|
if (I != Begin) {
|
2015-10-20 07:20:14 +08:00
|
|
|
do
|
|
|
|
--I;
|
|
|
|
while (I != Begin && IsNoopInstruction(&*I));
|
|
|
|
if (GetBasicARCInstKind(&*I) == ARCInstKind::AutoreleaseRV &&
|
|
|
|
GetArgRCIdentityRoot(&*I) == Arg) {
|
2011-06-16 07:37:01 +08:00
|
|
|
Changed = true;
|
|
|
|
++NumPeeps;
|
2013-01-08 05:26:07 +08:00
|
|
|
|
2013-04-06 02:10:41 +08:00
|
|
|
DEBUG(dbgs() << "Erasing autoreleaseRV,retainRV pair: " << *I << "\n"
|
|
|
|
<< "Erasing " << *RetainRV << "\n");
|
2013-01-08 05:26:07 +08:00
|
|
|
|
2015-10-20 07:20:14 +08:00
|
|
|
EraseInstruction(&*I);
|
2011-06-16 07:37:01 +08:00
|
|
|
EraseInstruction(RetainRV);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Turn it to a plain objc_retain.
|
|
|
|
Changed = true;
|
|
|
|
++NumPeeps;
|
2013-01-08 05:26:07 +08:00
|
|
|
|
2013-04-06 02:10:41 +08:00
|
|
|
DEBUG(dbgs() << "Transforming objc_retainAutoreleasedReturnValue => "
|
2013-01-06 01:55:42 +08:00
|
|
|
"objc_retain since the operand is not a return value.\n"
|
2013-04-06 02:10:41 +08:00
|
|
|
"Old = " << *RetainRV << "\n");
|
2013-01-08 05:26:07 +08:00
|
|
|
|
2015-03-16 15:02:24 +08:00
|
|
|
Constant *NewDecl = EP.get(ARCRuntimeEntryPointKind::Retain);
|
2013-07-06 09:39:23 +08:00
|
|
|
cast<CallInst>(RetainRV)->setCalledFunction(NewDecl);
|
2013-01-06 01:55:42 +08:00
|
|
|
|
2013-04-06 02:10:41 +08:00
|
|
|
DEBUG(dbgs() << "New = " << *RetainRV << "\n");
|
2013-01-06 01:55:42 +08:00
|
|
|
|
2011-06-16 07:37:01 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-01-14 08:35:14 +08:00
|
|
|
/// Turn objc_autoreleaseReturnValue into objc_autorelease if the result is not
|
|
|
|
/// used as a return value.
|
2015-02-20 03:51:32 +08:00
|
|
|
void ObjCARCOpt::OptimizeAutoreleaseRVCall(Function &F,
|
|
|
|
Instruction *AutoreleaseRV,
|
|
|
|
ARCInstKind &Class) {
|
2011-06-16 07:37:01 +08:00
|
|
|
// Check for a return of the pointer value.
|
2015-02-19 08:42:38 +08:00
|
|
|
const Value *Ptr = GetArgRCIdentityRoot(AutoreleaseRV);
|
2011-08-12 08:36:31 +08:00
|
|
|
SmallVector<const Value *, 2> Users;
|
|
|
|
Users.push_back(Ptr);
|
|
|
|
do {
|
|
|
|
Ptr = Users.pop_back_val();
|
2014-03-09 11:16:01 +08:00
|
|
|
for (const User *U : Ptr->users()) {
|
2015-02-20 03:51:32 +08:00
|
|
|
if (isa<ReturnInst>(U) || GetBasicARCInstKind(U) == ARCInstKind::RetainRV)
|
2011-08-12 08:36:31 +08:00
|
|
|
return;
|
2014-03-09 11:16:01 +08:00
|
|
|
if (isa<BitCastInst>(U))
|
|
|
|
Users.push_back(U);
|
2011-08-12 08:36:31 +08:00
|
|
|
}
|
|
|
|
} while (!Users.empty());
|
2011-06-16 07:37:01 +08:00
|
|
|
|
|
|
|
Changed = true;
|
|
|
|
++NumPeeps;
|
2013-01-07 05:07:11 +08:00
|
|
|
|
2013-04-06 02:10:41 +08:00
|
|
|
DEBUG(dbgs() << "Transforming objc_autoreleaseReturnValue => "
|
2013-01-07 05:07:11 +08:00
|
|
|
"objc_autorelease since its operand is not used as a return "
|
|
|
|
"value.\n"
|
2013-04-06 02:10:41 +08:00
|
|
|
"Old = " << *AutoreleaseRV << "\n");
|
2013-01-07 05:07:11 +08:00
|
|
|
|
2013-01-12 09:25:15 +08:00
|
|
|
CallInst *AutoreleaseRVCI = cast<CallInst>(AutoreleaseRV);
|
2015-03-16 15:02:24 +08:00
|
|
|
Constant *NewDecl = EP.get(ARCRuntimeEntryPointKind::Autorelease);
|
2013-07-06 09:39:23 +08:00
|
|
|
AutoreleaseRVCI->setCalledFunction(NewDecl);
|
2013-01-12 09:25:15 +08:00
|
|
|
AutoreleaseRVCI->setTailCall(false); // Never tail call objc_autorelease.
|
2015-02-20 03:51:32 +08:00
|
|
|
Class = ARCInstKind::Autorelease;
|
2013-01-08 05:26:07 +08:00
|
|
|
|
2013-04-06 02:10:41 +08:00
|
|
|
DEBUG(dbgs() << "New: " << *AutoreleaseRV << "\n");
|
2013-01-08 05:26:07 +08:00
|
|
|
|
2011-06-16 07:37:01 +08:00
|
|
|
}
|
|
|
|
|
2013-01-14 08:35:14 +08:00
|
|
|
/// Visit each call, one at a time, and make simplifications without doing any
|
|
|
|
/// additional analysis.
|
2011-06-16 07:37:01 +08:00
|
|
|
void ObjCARCOpt::OptimizeIndividualCalls(Function &F) {
|
2013-04-06 02:10:41 +08:00
|
|
|
DEBUG(dbgs() << "\n== ObjCARCOpt::OptimizeIndividualCalls ==\n");
|
2011-06-16 07:37:01 +08:00
|
|
|
// Reset all the flags in preparation for recomputing them.
|
|
|
|
UsedInThisFunction = 0;
|
|
|
|
|
|
|
|
// Visit all objc_* calls in F.
|
|
|
|
for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ) {
|
|
|
|
Instruction *Inst = &*I++;
|
2013-01-02 00:05:48 +08:00
|
|
|
|
2015-02-20 03:51:32 +08:00
|
|
|
ARCInstKind Class = GetBasicARCInstKind(Inst);
|
2011-06-16 07:37:01 +08:00
|
|
|
|
2013-04-06 02:10:41 +08:00
|
|
|
DEBUG(dbgs() << "Visiting: Class: " << Class << "; " << *Inst << "\n");
|
2013-01-18 02:32:34 +08:00
|
|
|
|
2011-06-16 07:37:01 +08:00
|
|
|
switch (Class) {
|
|
|
|
default: break;
|
|
|
|
|
|
|
|
// Delete no-op casts. These function calls have special semantics, but
|
|
|
|
// the semantics are entirely implemented via lowering in the front-end,
|
|
|
|
// so by the time they reach the optimizer, they are just no-op calls
|
|
|
|
// which return their argument.
|
|
|
|
//
|
|
|
|
// There are gray areas here, as the ability to cast reference-counted
|
|
|
|
// pointers to raw void* and back allows code to break ARC assumptions,
|
|
|
|
// however these are currently considered to be unimportant.
|
2015-02-20 03:51:32 +08:00
|
|
|
case ARCInstKind::NoopCast:
|
2011-06-16 07:37:01 +08:00
|
|
|
Changed = true;
|
|
|
|
++NumNoops;
|
2013-04-06 02:10:41 +08:00
|
|
|
DEBUG(dbgs() << "Erasing no-op cast: " << *Inst << "\n");
|
2011-06-16 07:37:01 +08:00
|
|
|
EraseInstruction(Inst);
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// If the pointer-to-weak-pointer is null, it's undefined behavior.
|
2015-02-20 03:51:32 +08:00
|
|
|
case ARCInstKind::StoreWeak:
|
|
|
|
case ARCInstKind::LoadWeak:
|
|
|
|
case ARCInstKind::LoadWeakRetained:
|
|
|
|
case ARCInstKind::InitWeak:
|
|
|
|
case ARCInstKind::DestroyWeak: {
|
2011-06-16 07:37:01 +08:00
|
|
|
CallInst *CI = cast<CallInst>(Inst);
|
2013-03-25 17:27:43 +08:00
|
|
|
if (IsNullOrUndef(CI->getArgOperand(0))) {
|
2012-04-14 02:57:48 +08:00
|
|
|
Changed = true;
|
2011-07-18 12:54:35 +08:00
|
|
|
Type *Ty = CI->getArgOperand(0)->getType();
|
2011-06-16 07:37:01 +08:00
|
|
|
new StoreInst(UndefValue::get(cast<PointerType>(Ty)->getElementType()),
|
|
|
|
Constant::getNullValue(Ty),
|
|
|
|
CI);
|
2013-01-08 05:26:07 +08:00
|
|
|
llvm::Value *NewValue = UndefValue::get(CI->getType());
|
2013-04-06 02:10:41 +08:00
|
|
|
DEBUG(dbgs() << "A null pointer-to-weak-pointer is undefined behavior."
|
|
|
|
"\nOld = " << *CI << "\nNew = " << *NewValue << "\n");
|
2013-01-07 05:54:30 +08:00
|
|
|
CI->replaceAllUsesWith(NewValue);
|
2011-06-16 07:37:01 +08:00
|
|
|
CI->eraseFromParent();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2015-02-20 03:51:32 +08:00
|
|
|
case ARCInstKind::CopyWeak:
|
|
|
|
case ARCInstKind::MoveWeak: {
|
2011-06-16 07:37:01 +08:00
|
|
|
CallInst *CI = cast<CallInst>(Inst);
|
2013-03-25 17:27:43 +08:00
|
|
|
if (IsNullOrUndef(CI->getArgOperand(0)) ||
|
|
|
|
IsNullOrUndef(CI->getArgOperand(1))) {
|
2012-04-14 02:57:48 +08:00
|
|
|
Changed = true;
|
2011-07-18 12:54:35 +08:00
|
|
|
Type *Ty = CI->getArgOperand(0)->getType();
|
2011-06-16 07:37:01 +08:00
|
|
|
new StoreInst(UndefValue::get(cast<PointerType>(Ty)->getElementType()),
|
|
|
|
Constant::getNullValue(Ty),
|
|
|
|
CI);
|
2013-01-07 05:54:30 +08:00
|
|
|
|
|
|
|
llvm::Value *NewValue = UndefValue::get(CI->getType());
|
2013-04-06 02:10:41 +08:00
|
|
|
DEBUG(dbgs() << "A null pointer-to-weak-pointer is undefined behavior."
|
|
|
|
"\nOld = " << *CI << "\nNew = " << *NewValue << "\n");
|
2013-01-08 05:26:07 +08:00
|
|
|
|
2013-01-07 05:54:30 +08:00
|
|
|
CI->replaceAllUsesWith(NewValue);
|
2011-06-16 07:37:01 +08:00
|
|
|
CI->eraseFromParent();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2015-02-20 03:51:32 +08:00
|
|
|
case ARCInstKind::RetainRV:
|
2011-06-16 07:37:01 +08:00
|
|
|
if (OptimizeRetainRVCall(F, Inst))
|
|
|
|
continue;
|
|
|
|
break;
|
2015-02-20 03:51:32 +08:00
|
|
|
case ARCInstKind::AutoreleaseRV:
|
2013-01-12 09:25:19 +08:00
|
|
|
OptimizeAutoreleaseRVCall(F, Inst, Class);
|
2011-06-16 07:37:01 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-04-03 10:57:24 +08:00
|
|
|
// objc_autorelease(x) -> objc_release(x) if x is otherwise unused.
|
2011-06-16 07:37:01 +08:00
|
|
|
if (IsAutorelease(Class) && Inst->use_empty()) {
|
|
|
|
CallInst *Call = cast<CallInst>(Inst);
|
|
|
|
const Value *Arg = Call->getArgOperand(0);
|
|
|
|
Arg = FindSingleUseIdentifiedObject(Arg);
|
|
|
|
if (Arg) {
|
|
|
|
Changed = true;
|
|
|
|
++NumAutoreleases;
|
|
|
|
|
|
|
|
// Create the declaration lazily.
|
|
|
|
LLVMContext &C = Inst->getContext();
|
2013-07-06 09:41:35 +08:00
|
|
|
|
2015-03-16 15:02:24 +08:00
|
|
|
Constant *Decl = EP.get(ARCRuntimeEntryPointKind::Release);
|
2013-07-06 09:39:23 +08:00
|
|
|
CallInst *NewCall = CallInst::Create(Decl, Call->getArgOperand(0), "",
|
|
|
|
Call);
|
2015-03-16 15:02:27 +08:00
|
|
|
NewCall->setMetadata(MDKindCache.get(ARCMDKindID::ImpreciseRelease),
|
2015-03-06 08:34:33 +08:00
|
|
|
MDNode::get(C, None));
|
2013-01-08 05:26:07 +08:00
|
|
|
|
2013-04-06 02:10:41 +08:00
|
|
|
DEBUG(dbgs() << "Replacing autorelease{,RV}(x) with objc_release(x) "
|
|
|
|
"since x is otherwise unused.\nOld: " << *Call << "\nNew: "
|
|
|
|
<< *NewCall << "\n");
|
2013-01-08 05:26:07 +08:00
|
|
|
|
2011-06-16 07:37:01 +08:00
|
|
|
EraseInstruction(Call);
|
|
|
|
Inst = NewCall;
|
2015-02-20 03:51:32 +08:00
|
|
|
Class = ARCInstKind::Release;
|
2011-06-16 07:37:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// For functions which can never be passed stack arguments, add
|
|
|
|
// a tail keyword.
|
|
|
|
if (IsAlwaysTail(Class)) {
|
|
|
|
Changed = true;
|
2013-04-06 02:10:41 +08:00
|
|
|
DEBUG(dbgs() << "Adding tail keyword to function since it can never be "
|
|
|
|
"passed stack args: " << *Inst << "\n");
|
2011-06-16 07:37:01 +08:00
|
|
|
cast<CallInst>(Inst)->setTailCall();
|
|
|
|
}
|
|
|
|
|
2013-01-12 09:25:15 +08:00
|
|
|
// Ensure that functions that can never have a "tail" keyword due to the
|
|
|
|
// semantics of ARC truly do not do so.
|
|
|
|
if (IsNeverTail(Class)) {
|
|
|
|
Changed = true;
|
2013-04-06 02:10:41 +08:00
|
|
|
DEBUG(dbgs() << "Removing tail keyword from function: " << *Inst <<
|
2013-01-12 09:25:15 +08:00
|
|
|
"\n");
|
|
|
|
cast<CallInst>(Inst)->setTailCall(false);
|
|
|
|
}
|
|
|
|
|
2011-06-16 07:37:01 +08:00
|
|
|
// Set nounwind as needed.
|
|
|
|
if (IsNoThrow(Class)) {
|
|
|
|
Changed = true;
|
2013-04-06 02:10:41 +08:00
|
|
|
DEBUG(dbgs() << "Found no throw class. Setting nounwind on: " << *Inst
|
|
|
|
<< "\n");
|
2011-06-16 07:37:01 +08:00
|
|
|
cast<CallInst>(Inst)->setDoesNotThrow();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!IsNoopOnNull(Class)) {
|
2015-02-20 03:51:32 +08:00
|
|
|
UsedInThisFunction |= 1 << unsigned(Class);
|
2011-06-16 07:37:01 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-02-19 08:42:38 +08:00
|
|
|
const Value *Arg = GetArgRCIdentityRoot(Inst);
|
2011-06-16 07:37:01 +08:00
|
|
|
|
|
|
|
// ARC calls with null are no-ops. Delete them.
|
2013-03-25 17:27:43 +08:00
|
|
|
if (IsNullOrUndef(Arg)) {
|
2011-06-16 07:37:01 +08:00
|
|
|
Changed = true;
|
|
|
|
++NumNoops;
|
2013-04-06 02:10:41 +08:00
|
|
|
DEBUG(dbgs() << "ARC calls with null are no-ops. Erasing: " << *Inst
|
|
|
|
<< "\n");
|
2011-06-16 07:37:01 +08:00
|
|
|
EraseInstruction(Inst);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Keep track of which of retain, release, autorelease, and retain_block
|
|
|
|
// are actually present in this function.
|
2015-02-20 03:51:32 +08:00
|
|
|
UsedInThisFunction |= 1 << unsigned(Class);
|
2011-06-16 07:37:01 +08:00
|
|
|
|
|
|
|
// If Arg is a PHI, and one or more incoming values to the
|
|
|
|
// PHI are null, and the call is control-equivalent to the PHI, and there
|
|
|
|
// are no relevant side effects between the PHI and the call, the call
|
|
|
|
// could be pushed up to just those paths with non-null incoming values.
|
|
|
|
// For now, don't bother splitting critical edges for this.
|
|
|
|
SmallVector<std::pair<Instruction *, const Value *>, 4> Worklist;
|
|
|
|
Worklist.push_back(std::make_pair(Inst, Arg));
|
|
|
|
do {
|
|
|
|
std::pair<Instruction *, const Value *> Pair = Worklist.pop_back_val();
|
|
|
|
Inst = Pair.first;
|
|
|
|
Arg = Pair.second;
|
|
|
|
|
|
|
|
const PHINode *PN = dyn_cast<PHINode>(Arg);
|
|
|
|
if (!PN) continue;
|
|
|
|
|
|
|
|
// Determine if the PHI has any null operands, or any incoming
|
|
|
|
// critical edges.
|
|
|
|
bool HasNull = false;
|
|
|
|
bool HasCriticalEdges = false;
|
|
|
|
for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
|
|
|
|
Value *Incoming =
|
2015-02-19 08:42:38 +08:00
|
|
|
GetRCIdentityRoot(PN->getIncomingValue(i));
|
2013-03-25 17:27:43 +08:00
|
|
|
if (IsNullOrUndef(Incoming))
|
2011-06-16 07:37:01 +08:00
|
|
|
HasNull = true;
|
|
|
|
else if (cast<TerminatorInst>(PN->getIncomingBlock(i)->back())
|
|
|
|
.getNumSuccessors() != 1) {
|
|
|
|
HasCriticalEdges = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// If we have null operands and no critical edges, optimize.
|
|
|
|
if (!HasCriticalEdges && HasNull) {
|
|
|
|
SmallPtrSet<Instruction *, 4> DependingInstructions;
|
|
|
|
SmallPtrSet<const BasicBlock *, 4> Visited;
|
|
|
|
|
|
|
|
// Check that there is nothing that cares about the reference
|
|
|
|
// count between the call and the phi.
|
2012-04-13 08:59:57 +08:00
|
|
|
switch (Class) {
|
2015-02-20 03:51:32 +08:00
|
|
|
case ARCInstKind::Retain:
|
|
|
|
case ARCInstKind::RetainBlock:
|
2012-04-13 08:59:57 +08:00
|
|
|
// These can always be moved up.
|
|
|
|
break;
|
2015-02-20 03:51:32 +08:00
|
|
|
case ARCInstKind::Release:
|
2012-05-09 07:39:44 +08:00
|
|
|
// These can't be moved across things that care about the retain
|
|
|
|
// count.
|
2012-04-13 08:59:57 +08:00
|
|
|
FindDependencies(NeedsPositiveRetainCount, Arg,
|
|
|
|
Inst->getParent(), Inst,
|
|
|
|
DependingInstructions, Visited, PA);
|
|
|
|
break;
|
2015-02-20 03:51:32 +08:00
|
|
|
case ARCInstKind::Autorelease:
|
2012-04-13 08:59:57 +08:00
|
|
|
// These can't be moved across autorelease pool scope boundaries.
|
|
|
|
FindDependencies(AutoreleasePoolBoundary, Arg,
|
|
|
|
Inst->getParent(), Inst,
|
|
|
|
DependingInstructions, Visited, PA);
|
|
|
|
break;
|
2015-02-20 03:51:32 +08:00
|
|
|
case ARCInstKind::RetainRV:
|
|
|
|
case ARCInstKind::AutoreleaseRV:
|
2012-04-13 08:59:57 +08:00
|
|
|
// Don't move these; the RV optimization depends on the autoreleaseRV
|
|
|
|
// being tail called, and the retainRV being immediately after a call
|
|
|
|
// (which might still happen if we get lucky with codegen layout, but
|
|
|
|
// it's not worth taking the chance).
|
|
|
|
continue;
|
|
|
|
default:
|
|
|
|
llvm_unreachable("Invalid dependence flavor");
|
|
|
|
}
|
|
|
|
|
2011-06-16 07:37:01 +08:00
|
|
|
if (DependingInstructions.size() == 1 &&
|
|
|
|
*DependingInstructions.begin() == PN) {
|
|
|
|
Changed = true;
|
|
|
|
++NumPartialNoops;
|
|
|
|
// Clone the call into each predecessor that has a non-null value.
|
|
|
|
CallInst *CInst = cast<CallInst>(Inst);
|
2011-07-18 12:54:35 +08:00
|
|
|
Type *ParamTy = CInst->getArgOperand(0)->getType();
|
2011-06-16 07:37:01 +08:00
|
|
|
for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
|
|
|
|
Value *Incoming =
|
2015-02-19 08:42:38 +08:00
|
|
|
GetRCIdentityRoot(PN->getIncomingValue(i));
|
2013-03-25 17:27:43 +08:00
|
|
|
if (!IsNullOrUndef(Incoming)) {
|
2011-06-16 07:37:01 +08:00
|
|
|
CallInst *Clone = cast<CallInst>(CInst->clone());
|
|
|
|
Value *Op = PN->getIncomingValue(i);
|
|
|
|
Instruction *InsertPos = &PN->getIncomingBlock(i)->back();
|
|
|
|
if (Op->getType() != ParamTy)
|
|
|
|
Op = new BitCastInst(Op, ParamTy, "", InsertPos);
|
|
|
|
Clone->setArgOperand(0, Op);
|
|
|
|
Clone->insertBefore(InsertPos);
|
2013-01-10 03:23:24 +08:00
|
|
|
|
2013-04-06 02:10:41 +08:00
|
|
|
DEBUG(dbgs() << "Cloning "
|
2013-01-10 03:23:24 +08:00
|
|
|
<< *CInst << "\n"
|
2013-04-06 02:10:41 +08:00
|
|
|
"And inserting clone at " << *InsertPos << "\n");
|
2011-06-16 07:37:01 +08:00
|
|
|
Worklist.push_back(std::make_pair(Clone, Incoming));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Erase the original call.
|
2013-01-10 03:23:24 +08:00
|
|
|
DEBUG(dbgs() << "Erasing: " << *CInst << "\n");
|
2011-06-16 07:37:01 +08:00
|
|
|
EraseInstruction(CInst);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (!Worklist.empty());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-18 13:39:45 +08:00
|
|
|
/// If we have a top down pointer in the S_Use state, make sure that there are
|
|
|
|
/// no CFG hazards by checking the states of various bottom up pointers.
|
|
|
|
static void CheckForUseCFGHazard(const Sequence SuccSSeq,
|
|
|
|
const bool SuccSRRIKnownSafe,
|
2015-03-06 08:34:36 +08:00
|
|
|
TopDownPtrState &S,
|
2013-04-18 13:39:45 +08:00
|
|
|
bool &SomeSuccHasSame,
|
|
|
|
bool &AllSuccsHaveSame,
|
2013-05-25 04:44:05 +08:00
|
|
|
bool &NotAllSeqEqualButKnownSafe,
|
2013-04-18 13:39:45 +08:00
|
|
|
bool &ShouldContinue) {
|
|
|
|
switch (SuccSSeq) {
|
|
|
|
case S_CanRelease: {
|
2013-06-21 14:59:02 +08:00
|
|
|
if (!S.IsKnownSafe() && !SuccSRRIKnownSafe) {
|
2013-04-18 13:39:45 +08:00
|
|
|
S.ClearSequenceProgress();
|
|
|
|
break;
|
|
|
|
}
|
2013-06-22 03:12:36 +08:00
|
|
|
S.SetCFGHazardAfflicted(true);
|
2013-04-18 13:39:45 +08:00
|
|
|
ShouldContinue = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case S_Use:
|
|
|
|
SomeSuccHasSame = true;
|
|
|
|
break;
|
|
|
|
case S_Stop:
|
|
|
|
case S_Release:
|
|
|
|
case S_MovableRelease:
|
2013-06-21 14:59:02 +08:00
|
|
|
if (!S.IsKnownSafe() && !SuccSRRIKnownSafe)
|
2013-04-18 13:39:45 +08:00
|
|
|
AllSuccsHaveSame = false;
|
2013-05-25 04:44:05 +08:00
|
|
|
else
|
|
|
|
NotAllSeqEqualButKnownSafe = true;
|
2013-04-18 13:39:45 +08:00
|
|
|
break;
|
|
|
|
case S_Retain:
|
|
|
|
llvm_unreachable("bottom-up pointer in retain state!");
|
|
|
|
case S_None:
|
|
|
|
llvm_unreachable("This should have been handled earlier.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// If we have a Top Down pointer in the S_CanRelease state, make sure that
|
|
|
|
/// there are no CFG hazards by checking the states of various bottom up
|
|
|
|
/// pointers.
|
|
|
|
static void CheckForCanReleaseCFGHazard(const Sequence SuccSSeq,
|
|
|
|
const bool SuccSRRIKnownSafe,
|
2015-03-06 08:34:36 +08:00
|
|
|
TopDownPtrState &S,
|
2013-04-18 13:39:45 +08:00
|
|
|
bool &SomeSuccHasSame,
|
2013-05-25 04:44:05 +08:00
|
|
|
bool &AllSuccsHaveSame,
|
|
|
|
bool &NotAllSeqEqualButKnownSafe) {
|
2013-04-18 13:39:45 +08:00
|
|
|
switch (SuccSSeq) {
|
|
|
|
case S_CanRelease:
|
|
|
|
SomeSuccHasSame = true;
|
|
|
|
break;
|
|
|
|
case S_Stop:
|
|
|
|
case S_Release:
|
|
|
|
case S_MovableRelease:
|
|
|
|
case S_Use:
|
2013-06-21 14:59:02 +08:00
|
|
|
if (!S.IsKnownSafe() && !SuccSRRIKnownSafe)
|
2013-04-18 13:39:45 +08:00
|
|
|
AllSuccsHaveSame = false;
|
2013-05-25 04:44:05 +08:00
|
|
|
else
|
|
|
|
NotAllSeqEqualButKnownSafe = true;
|
2013-04-18 13:39:45 +08:00
|
|
|
break;
|
|
|
|
case S_Retain:
|
|
|
|
llvm_unreachable("bottom-up pointer in retain state!");
|
|
|
|
case S_None:
|
|
|
|
llvm_unreachable("This should have been handled earlier.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-14 08:35:14 +08:00
|
|
|
/// Check for critical edges, loop boundaries, irreducible control flow, or
|
|
|
|
/// other CFG structures where moving code across the edge would result in it
|
|
|
|
/// being executed more.
|
2011-06-16 07:37:01 +08:00
|
|
|
void
|
|
|
|
ObjCARCOpt::CheckForCFGHazards(const BasicBlock *BB,
|
|
|
|
DenseMap<const BasicBlock *, BBState> &BBStates,
|
|
|
|
BBState &MyStates) const {
|
|
|
|
// If any top-down local-use or possible-dec has a succ which is earlier in
|
|
|
|
// the sequence, forget it.
|
2015-03-06 08:34:36 +08:00
|
|
|
for (auto I = MyStates.top_down_ptr_begin(), E = MyStates.top_down_ptr_end();
|
|
|
|
I != E; ++I) {
|
|
|
|
TopDownPtrState &S = I->second;
|
2013-04-18 13:39:45 +08:00
|
|
|
const Sequence Seq = I->second.GetSeq();
|
|
|
|
|
|
|
|
// We only care about S_Retain, S_CanRelease, and S_Use.
|
|
|
|
if (Seq == S_None)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Make sure that if extra top down states are added in the future that this
|
|
|
|
// code is updated to handle it.
|
|
|
|
assert((Seq == S_Retain || Seq == S_CanRelease || Seq == S_Use) &&
|
|
|
|
"Unknown top down sequence state.");
|
|
|
|
|
|
|
|
const Value *Arg = I->first;
|
|
|
|
const TerminatorInst *TI = cast<TerminatorInst>(&BB->back());
|
|
|
|
bool SomeSuccHasSame = false;
|
|
|
|
bool AllSuccsHaveSame = true;
|
2013-05-25 04:44:05 +08:00
|
|
|
bool NotAllSeqEqualButKnownSafe = false;
|
2013-04-18 13:39:45 +08:00
|
|
|
|
|
|
|
succ_const_iterator SI(TI), SE(TI, false);
|
|
|
|
|
|
|
|
for (; SI != SE; ++SI) {
|
|
|
|
// If VisitBottomUp has pointer information for this successor, take
|
|
|
|
// what we know about it.
|
|
|
|
const DenseMap<const BasicBlock *, BBState>::iterator BBI =
|
|
|
|
BBStates.find(*SI);
|
|
|
|
assert(BBI != BBStates.end());
|
2015-03-06 08:34:36 +08:00
|
|
|
const BottomUpPtrState &SuccS = BBI->second.getPtrBottomUpState(Arg);
|
2013-04-18 13:39:45 +08:00
|
|
|
const Sequence SuccSSeq = SuccS.GetSeq();
|
|
|
|
|
|
|
|
// If bottom up, the pointer is in an S_None state, clear the sequence
|
|
|
|
// progress since the sequence in the bottom up state finished
|
|
|
|
// suggesting a mismatch in between retains/releases. This is true for
|
|
|
|
// all three cases that we are handling here: S_Retain, S_Use, and
|
|
|
|
// S_CanRelease.
|
|
|
|
if (SuccSSeq == S_None) {
|
2011-08-12 08:26:31 +08:00
|
|
|
S.ClearSequenceProgress();
|
2013-04-18 13:39:45 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we have S_Use or S_CanRelease, perform our check for cfg hazard
|
|
|
|
// checks.
|
2013-06-21 14:59:02 +08:00
|
|
|
const bool SuccSRRIKnownSafe = SuccS.IsKnownSafe();
|
2013-04-18 13:39:45 +08:00
|
|
|
|
|
|
|
// *NOTE* We do not use Seq from above here since we are allowing for
|
|
|
|
// S.GetSeq() to change while we are visiting basic blocks.
|
|
|
|
switch(S.GetSeq()) {
|
|
|
|
case S_Use: {
|
|
|
|
bool ShouldContinue = false;
|
2013-05-25 04:44:05 +08:00
|
|
|
CheckForUseCFGHazard(SuccSSeq, SuccSRRIKnownSafe, S, SomeSuccHasSame,
|
|
|
|
AllSuccsHaveSame, NotAllSeqEqualButKnownSafe,
|
2013-04-18 13:39:45 +08:00
|
|
|
ShouldContinue);
|
|
|
|
if (ShouldContinue)
|
2011-08-12 08:26:31 +08:00
|
|
|
continue;
|
2013-04-18 13:39:45 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case S_CanRelease: {
|
2013-05-25 04:44:05 +08:00
|
|
|
CheckForCanReleaseCFGHazard(SuccSSeq, SuccSRRIKnownSafe, S,
|
|
|
|
SomeSuccHasSame, AllSuccsHaveSame,
|
|
|
|
NotAllSeqEqualButKnownSafe);
|
2013-04-18 13:39:45 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case S_Retain:
|
|
|
|
case S_None:
|
|
|
|
case S_Stop:
|
|
|
|
case S_Release:
|
|
|
|
case S_MovableRelease:
|
|
|
|
break;
|
2011-08-12 08:26:31 +08:00
|
|
|
}
|
2011-06-16 07:37:01 +08:00
|
|
|
}
|
2013-04-18 13:39:45 +08:00
|
|
|
|
|
|
|
// If the state at the other end of any of the successor edges
|
|
|
|
// matches the current state, require all edges to match. This
|
|
|
|
// guards against loops in the middle of a sequence.
|
2013-05-25 04:44:05 +08:00
|
|
|
if (SomeSuccHasSame && !AllSuccsHaveSame) {
|
2013-04-18 13:39:45 +08:00
|
|
|
S.ClearSequenceProgress();
|
2013-05-25 04:44:05 +08:00
|
|
|
} else if (NotAllSeqEqualButKnownSafe) {
|
|
|
|
// If we would have cleared the state foregoing the fact that we are known
|
|
|
|
// safe, stop code motion. This is because whether or not it is safe to
|
|
|
|
// remove RR pairs via KnownSafe is an orthogonal concept to whether we
|
|
|
|
// are allowed to perform code motion.
|
2013-06-22 03:12:36 +08:00
|
|
|
S.SetCFGHazardAfflicted(true);
|
2013-05-25 04:44:05 +08:00
|
|
|
}
|
2013-04-18 13:39:45 +08:00
|
|
|
}
|
2011-06-16 07:37:01 +08:00
|
|
|
}
|
|
|
|
|
2015-03-06 07:28:58 +08:00
|
|
|
bool ObjCARCOpt::VisitInstructionBottomUp(
|
|
|
|
Instruction *Inst, BasicBlock *BB, BlotMapVector<Value *, RRInfo> &Retains,
|
|
|
|
BBState &MyStates) {
|
2012-03-23 02:24:56 +08:00
|
|
|
bool NestingDetected = false;
|
2015-02-20 03:51:32 +08:00
|
|
|
ARCInstKind Class = GetARCInstKind(Inst);
|
2014-04-25 13:29:35 +08:00
|
|
|
const Value *Arg = nullptr;
|
2013-04-06 07:46:45 +08:00
|
|
|
|
2015-03-16 15:02:39 +08:00
|
|
|
DEBUG(dbgs() << " Class: " << Class << "\n");
|
2013-04-06 07:46:45 +08:00
|
|
|
|
2012-03-23 02:24:56 +08:00
|
|
|
switch (Class) {
|
2015-02-20 03:51:32 +08:00
|
|
|
case ARCInstKind::Release: {
|
2015-02-19 08:42:38 +08:00
|
|
|
Arg = GetArgRCIdentityRoot(Inst);
|
2012-03-23 02:24:56 +08:00
|
|
|
|
2015-03-06 08:34:36 +08:00
|
|
|
BottomUpPtrState &S = MyStates.getPtrBottomUpState(Arg);
|
2015-03-06 08:34:39 +08:00
|
|
|
NestingDetected |= S.InitBottomUp(MDKindCache, Inst);
|
2012-03-23 02:24:56 +08:00
|
|
|
break;
|
|
|
|
}
|
2015-02-20 03:51:32 +08:00
|
|
|
case ARCInstKind::RetainBlock:
|
2013-03-29 04:11:19 +08:00
|
|
|
// In OptimizeIndividualCalls, we have strength reduced all optimizable
|
|
|
|
// objc_retainBlocks to objc_retains. Thus at this point any
|
|
|
|
// objc_retainBlocks that we see are not optimizable.
|
|
|
|
break;
|
2015-02-20 03:51:32 +08:00
|
|
|
case ARCInstKind::Retain:
|
|
|
|
case ARCInstKind::RetainRV: {
|
2015-02-19 08:42:38 +08:00
|
|
|
Arg = GetArgRCIdentityRoot(Inst);
|
2015-03-06 08:34:36 +08:00
|
|
|
BottomUpPtrState &S = MyStates.getPtrBottomUpState(Arg);
|
2015-03-06 08:34:42 +08:00
|
|
|
if (S.MatchWithRetain()) {
|
|
|
|
// Don't do retain+release tracking for ARCInstKind::RetainRV, because
|
|
|
|
// it's better to let it remain as the first instruction after a call.
|
2015-03-16 15:02:39 +08:00
|
|
|
if (Class != ARCInstKind::RetainRV) {
|
|
|
|
DEBUG(llvm::dbgs() << " Matching with: " << *Inst << "\n");
|
2013-06-22 03:44:30 +08:00
|
|
|
Retains[Inst] = S.GetRRInfo();
|
2015-03-16 15:02:39 +08:00
|
|
|
}
|
2012-03-23 02:24:56 +08:00
|
|
|
S.ClearSequenceProgress();
|
|
|
|
}
|
2013-04-06 06:54:32 +08:00
|
|
|
// A retain moving bottom up can be a use.
|
|
|
|
break;
|
2012-03-23 02:24:56 +08:00
|
|
|
}
|
2015-02-20 03:51:32 +08:00
|
|
|
case ARCInstKind::AutoreleasepoolPop:
|
2012-03-23 02:24:56 +08:00
|
|
|
// Conservatively, clear MyStates for all known pointers.
|
|
|
|
MyStates.clearBottomUpPointers();
|
|
|
|
return NestingDetected;
|
2015-02-20 03:51:32 +08:00
|
|
|
case ARCInstKind::AutoreleasepoolPush:
|
|
|
|
case ARCInstKind::None:
|
2012-03-23 02:24:56 +08:00
|
|
|
// These are irrelevant.
|
|
|
|
return NestingDetected;
|
2015-02-20 03:51:32 +08:00
|
|
|
case ARCInstKind::User:
|
2013-05-14 07:49:42 +08:00
|
|
|
// If we have a store into an alloca of a pointer we are tracking, the
|
|
|
|
// pointer has multiple owners implying that we must be more conservative.
|
|
|
|
//
|
|
|
|
// This comes up in the context of a pointer being ``KnownSafe''. In the
|
2014-01-25 01:20:08 +08:00
|
|
|
// presence of a block being initialized, the frontend will emit the
|
2013-05-14 07:49:42 +08:00
|
|
|
// objc_retain on the original pointer and the release on the pointer loaded
|
|
|
|
// from the alloca. The optimizer will through the provenance analysis
|
|
|
|
// realize that the two are related, but since we only require KnownSafe in
|
|
|
|
// one direction, will match the inner retain on the original pointer with
|
|
|
|
// the guard release on the original pointer. This is fixed by ensuring that
|
2014-01-25 01:20:08 +08:00
|
|
|
// in the presence of allocas we only unconditionally remove pointers if
|
2013-05-14 07:49:42 +08:00
|
|
|
// both our retain and our release are KnownSafe.
|
|
|
|
if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
|
2015-03-10 10:37:25 +08:00
|
|
|
const DataLayout &DL = BB->getModule()->getDataLayout();
|
|
|
|
if (AreAnyUnderlyingObjectsAnAlloca(SI->getPointerOperand(), DL)) {
|
2015-03-06 08:34:36 +08:00
|
|
|
auto I = MyStates.findPtrBottomUpState(
|
|
|
|
GetRCIdentityRoot(SI->getValueOperand()));
|
2013-05-14 07:49:42 +08:00
|
|
|
if (I != MyStates.bottom_up_ptr_end())
|
2013-05-25 04:44:02 +08:00
|
|
|
MultiOwnersSet.insert(I->first);
|
2013-05-14 07:49:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2012-03-23 02:24:56 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Consider any other possible effects of this instruction on each
|
|
|
|
// pointer being tracked.
|
2015-03-06 08:34:36 +08:00
|
|
|
for (auto MI = MyStates.bottom_up_ptr_begin(),
|
|
|
|
ME = MyStates.bottom_up_ptr_end();
|
|
|
|
MI != ME; ++MI) {
|
2012-03-23 02:24:56 +08:00
|
|
|
const Value *Ptr = MI->first;
|
|
|
|
if (Ptr == Arg)
|
|
|
|
continue; // Handled above.
|
2015-03-06 08:34:36 +08:00
|
|
|
BottomUpPtrState &S = MI->second;
|
2012-03-23 02:24:56 +08:00
|
|
|
|
2015-03-06 10:07:12 +08:00
|
|
|
if (S.HandlePotentialAlterRefCount(Inst, Ptr, PA, Class))
|
|
|
|
continue;
|
2012-03-23 02:24:56 +08:00
|
|
|
|
2015-03-06 10:07:12 +08:00
|
|
|
S.HandlePotentialUse(BB, Inst, Ptr, PA, Class);
|
2012-03-23 02:24:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return NestingDetected;
|
|
|
|
}
|
|
|
|
|
2015-03-06 07:28:58 +08:00
|
|
|
bool ObjCARCOpt::VisitBottomUp(BasicBlock *BB,
|
|
|
|
DenseMap<const BasicBlock *, BBState> &BBStates,
|
|
|
|
BlotMapVector<Value *, RRInfo> &Retains) {
|
2013-04-06 02:10:41 +08:00
|
|
|
|
|
|
|
DEBUG(dbgs() << "\n== ObjCARCOpt::VisitBottomUp ==\n");
|
2013-04-06 07:46:45 +08:00
|
|
|
|
2011-06-16 07:37:01 +08:00
|
|
|
bool NestingDetected = false;
|
|
|
|
BBState &MyStates = BBStates[BB];
|
|
|
|
|
|
|
|
// Merge the states from each successor to compute the initial state
|
|
|
|
// for the current block.
|
2012-08-28 02:31:36 +08:00
|
|
|
BBState::edge_iterator SI(MyStates.succ_begin()),
|
|
|
|
SE(MyStates.succ_end());
|
|
|
|
if (SI != SE) {
|
2012-04-25 06:53:18 +08:00
|
|
|
const BasicBlock *Succ = *SI;
|
|
|
|
DenseMap<const BasicBlock *, BBState>::iterator I = BBStates.find(Succ);
|
|
|
|
assert(I != BBStates.end());
|
|
|
|
MyStates.InitFromSucc(I->second);
|
|
|
|
++SI;
|
|
|
|
for (; SI != SE; ++SI) {
|
|
|
|
Succ = *SI;
|
|
|
|
I = BBStates.find(Succ);
|
|
|
|
assert(I != BBStates.end());
|
|
|
|
MyStates.MergeSucc(I->second);
|
|
|
|
}
|
2013-03-29 13:13:07 +08:00
|
|
|
}
|
2013-03-26 08:42:09 +08:00
|
|
|
|
2015-03-16 15:02:39 +08:00
|
|
|
DEBUG(llvm::dbgs() << "Before:\n" << BBStates[BB] << "\n"
|
|
|
|
<< "Performing Dataflow:\n");
|
|
|
|
|
2011-06-16 07:37:01 +08:00
|
|
|
// Visit all the instructions, bottom-up.
|
|
|
|
for (BasicBlock::iterator I = BB->end(), E = BB->begin(); I != E; --I) {
|
2015-10-20 07:20:14 +08:00
|
|
|
Instruction *Inst = &*std::prev(I);
|
2012-03-24 01:47:54 +08:00
|
|
|
|
|
|
|
// Invoke instructions are visited as part of their successors (below).
|
|
|
|
if (isa<InvokeInst>(Inst))
|
|
|
|
continue;
|
|
|
|
|
2015-03-16 15:02:39 +08:00
|
|
|
DEBUG(dbgs() << " Visiting " << *Inst << "\n");
|
2013-01-13 15:00:51 +08:00
|
|
|
|
2012-03-24 01:47:54 +08:00
|
|
|
NestingDetected |= VisitInstructionBottomUp(Inst, BB, Retains, MyStates);
|
|
|
|
}
|
|
|
|
|
2012-04-28 02:56:31 +08:00
|
|
|
// If there's a predecessor with an invoke, visit the invoke as if it were
|
|
|
|
// part of this block, since we can't insert code after an invoke in its own
|
|
|
|
// block, and we don't want to split critical edges.
|
2012-04-25 06:53:18 +08:00
|
|
|
for (BBState::edge_iterator PI(MyStates.pred_begin()),
|
|
|
|
PE(MyStates.pred_end()); PI != PE; ++PI) {
|
2012-03-24 01:47:54 +08:00
|
|
|
BasicBlock *Pred = *PI;
|
2012-04-28 02:56:31 +08:00
|
|
|
if (InvokeInst *II = dyn_cast<InvokeInst>(&Pred->back()))
|
|
|
|
NestingDetected |= VisitInstructionBottomUp(II, BB, Retains, MyStates);
|
2012-03-23 02:24:56 +08:00
|
|
|
}
|
2011-06-16 07:37:01 +08:00
|
|
|
|
2015-03-16 15:02:39 +08:00
|
|
|
DEBUG(llvm::dbgs() << "\nFinal State:\n" << BBStates[BB] << "\n");
|
|
|
|
|
2012-03-23 02:24:56 +08:00
|
|
|
return NestingDetected;
|
|
|
|
}
|
2011-06-16 07:37:01 +08:00
|
|
|
|
2012-03-23 02:24:56 +08:00
|
|
|
bool
|
|
|
|
ObjCARCOpt::VisitInstructionTopDown(Instruction *Inst,
|
|
|
|
DenseMap<Value *, RRInfo> &Releases,
|
|
|
|
BBState &MyStates) {
|
|
|
|
bool NestingDetected = false;
|
2015-02-20 03:51:32 +08:00
|
|
|
ARCInstKind Class = GetARCInstKind(Inst);
|
2014-04-25 13:29:35 +08:00
|
|
|
const Value *Arg = nullptr;
|
2011-06-16 07:37:01 +08:00
|
|
|
|
2015-03-16 15:02:39 +08:00
|
|
|
DEBUG(llvm::dbgs() << " Class: " << Class << "\n");
|
|
|
|
|
2012-03-23 02:24:56 +08:00
|
|
|
switch (Class) {
|
2015-02-20 03:51:32 +08:00
|
|
|
case ARCInstKind::RetainBlock:
|
2013-03-29 04:11:19 +08:00
|
|
|
// In OptimizeIndividualCalls, we have strength reduced all optimizable
|
|
|
|
// objc_retainBlocks to objc_retains. Thus at this point any
|
2015-03-06 08:34:42 +08:00
|
|
|
// objc_retainBlocks that we see are not optimizable. We need to break since
|
|
|
|
// a retain can be a potential use.
|
2013-03-29 04:11:19 +08:00
|
|
|
break;
|
2015-02-20 03:51:32 +08:00
|
|
|
case ARCInstKind::Retain:
|
|
|
|
case ARCInstKind::RetainRV: {
|
2015-02-19 08:42:38 +08:00
|
|
|
Arg = GetArgRCIdentityRoot(Inst);
|
2015-03-06 08:34:36 +08:00
|
|
|
TopDownPtrState &S = MyStates.getPtrTopDownState(Arg);
|
2015-03-06 08:34:39 +08:00
|
|
|
NestingDetected |= S.InitTopDown(Class, Inst);
|
2015-08-09 02:27:36 +08:00
|
|
|
// A retain can be a potential use; proceed to the generic checking
|
2012-07-24 03:27:31 +08:00
|
|
|
// code below.
|
|
|
|
break;
|
2012-03-23 02:24:56 +08:00
|
|
|
}
|
2015-02-20 03:51:32 +08:00
|
|
|
case ARCInstKind::Release: {
|
2015-02-19 08:42:38 +08:00
|
|
|
Arg = GetArgRCIdentityRoot(Inst);
|
2015-03-06 08:34:36 +08:00
|
|
|
TopDownPtrState &S = MyStates.getPtrTopDownState(Arg);
|
2015-03-06 08:34:42 +08:00
|
|
|
// Try to form a tentative pair in between this release instruction and the
|
|
|
|
// top down pointers that we are tracking.
|
|
|
|
if (S.MatchWithRelease(MDKindCache, Inst)) {
|
|
|
|
// If we succeed, copy S's RRInfo into the Release -> {Retain Set
|
|
|
|
// Map}. Then we clear S.
|
2015-03-16 15:02:39 +08:00
|
|
|
DEBUG(llvm::dbgs() << " Matching with: " << *Inst << "\n");
|
2013-06-22 03:44:30 +08:00
|
|
|
Releases[Inst] = S.GetRRInfo();
|
2012-03-23 02:24:56 +08:00
|
|
|
S.ClearSequenceProgress();
|
2011-06-16 07:37:01 +08:00
|
|
|
}
|
2012-03-23 02:24:56 +08:00
|
|
|
break;
|
|
|
|
}
|
2015-02-20 03:51:32 +08:00
|
|
|
case ARCInstKind::AutoreleasepoolPop:
|
2012-03-23 02:24:56 +08:00
|
|
|
// Conservatively, clear MyStates for all known pointers.
|
|
|
|
MyStates.clearTopDownPointers();
|
2015-03-06 08:34:42 +08:00
|
|
|
return false;
|
2015-02-20 03:51:32 +08:00
|
|
|
case ARCInstKind::AutoreleasepoolPush:
|
|
|
|
case ARCInstKind::None:
|
2015-03-06 08:34:42 +08:00
|
|
|
// These can not be uses of
|
|
|
|
return false;
|
2012-03-23 02:24:56 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2011-06-16 07:37:01 +08:00
|
|
|
|
2012-03-23 02:24:56 +08:00
|
|
|
// Consider any other possible effects of this instruction on each
|
|
|
|
// pointer being tracked.
|
2015-03-06 08:34:36 +08:00
|
|
|
for (auto MI = MyStates.top_down_ptr_begin(),
|
|
|
|
ME = MyStates.top_down_ptr_end();
|
|
|
|
MI != ME; ++MI) {
|
2012-03-23 02:24:56 +08:00
|
|
|
const Value *Ptr = MI->first;
|
|
|
|
if (Ptr == Arg)
|
|
|
|
continue; // Handled above.
|
2015-03-06 08:34:36 +08:00
|
|
|
TopDownPtrState &S = MI->second;
|
2015-03-06 10:07:12 +08:00
|
|
|
if (S.HandlePotentialAlterRefCount(Inst, Ptr, PA, Class))
|
|
|
|
continue;
|
2012-03-23 02:24:56 +08:00
|
|
|
|
2015-03-06 10:07:12 +08:00
|
|
|
S.HandlePotentialUse(Inst, Ptr, PA, Class);
|
2011-06-16 07:37:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return NestingDetected;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ObjCARCOpt::VisitTopDown(BasicBlock *BB,
|
|
|
|
DenseMap<const BasicBlock *, BBState> &BBStates,
|
|
|
|
DenseMap<Value *, RRInfo> &Releases) {
|
2013-04-06 02:10:41 +08:00
|
|
|
DEBUG(dbgs() << "\n== ObjCARCOpt::VisitTopDown ==\n");
|
2011-06-16 07:37:01 +08:00
|
|
|
bool NestingDetected = false;
|
|
|
|
BBState &MyStates = BBStates[BB];
|
|
|
|
|
|
|
|
// Merge the states from each predecessor to compute the initial state
|
|
|
|
// for the current block.
|
2012-08-28 02:31:36 +08:00
|
|
|
BBState::edge_iterator PI(MyStates.pred_begin()),
|
|
|
|
PE(MyStates.pred_end());
|
|
|
|
if (PI != PE) {
|
2012-04-25 06:53:18 +08:00
|
|
|
const BasicBlock *Pred = *PI;
|
|
|
|
DenseMap<const BasicBlock *, BBState>::iterator I = BBStates.find(Pred);
|
|
|
|
assert(I != BBStates.end());
|
|
|
|
MyStates.InitFromPred(I->second);
|
|
|
|
++PI;
|
|
|
|
for (; PI != PE; ++PI) {
|
|
|
|
Pred = *PI;
|
|
|
|
I = BBStates.find(Pred);
|
|
|
|
assert(I != BBStates.end());
|
|
|
|
MyStates.MergePred(I->second);
|
|
|
|
}
|
|
|
|
}
|
2011-06-16 07:37:01 +08:00
|
|
|
|
2015-03-16 15:02:39 +08:00
|
|
|
DEBUG(llvm::dbgs() << "Before:\n" << BBStates[BB] << "\n"
|
|
|
|
<< "Performing Dataflow:\n");
|
|
|
|
|
2011-06-16 07:37:01 +08:00
|
|
|
// Visit all the instructions, top-down.
|
2015-10-20 07:20:14 +08:00
|
|
|
for (Instruction &Inst : *BB) {
|
|
|
|
DEBUG(dbgs() << " Visiting " << Inst << "\n");
|
2013-01-13 15:00:51 +08:00
|
|
|
|
2015-10-20 07:20:14 +08:00
|
|
|
NestingDetected |= VisitInstructionTopDown(&Inst, Releases, MyStates);
|
2011-06-16 07:37:01 +08:00
|
|
|
}
|
2013-04-04 07:07:45 +08:00
|
|
|
|
2015-03-16 15:02:39 +08:00
|
|
|
DEBUG(llvm::dbgs() << "\nState Before Checking for CFG Hazards:\n"
|
|
|
|
<< BBStates[BB] << "\n\n");
|
2011-06-16 07:37:01 +08:00
|
|
|
CheckForCFGHazards(BB, BBStates, MyStates);
|
2015-03-16 15:02:39 +08:00
|
|
|
DEBUG(llvm::dbgs() << "Final State:\n" << BBStates[BB] << "\n");
|
2011-06-16 07:37:01 +08:00
|
|
|
return NestingDetected;
|
|
|
|
}
|
|
|
|
|
2011-12-13 03:42:25 +08:00
|
|
|
static void
|
|
|
|
ComputePostOrders(Function &F,
|
|
|
|
SmallVectorImpl<BasicBlock *> &PostOrder,
|
2012-04-25 06:53:18 +08:00
|
|
|
SmallVectorImpl<BasicBlock *> &ReverseCFGPostOrder,
|
|
|
|
unsigned NoObjCARCExceptionsMDKind,
|
|
|
|
DenseMap<const BasicBlock *, BBState> &BBStates) {
|
2013-01-14 08:35:14 +08:00
|
|
|
/// The visited set, for doing DFS walks.
|
2011-08-12 08:24:29 +08:00
|
|
|
SmallPtrSet<BasicBlock *, 16> Visited;
|
2011-12-13 03:42:25 +08:00
|
|
|
|
|
|
|
// Do DFS, computing the PostOrder.
|
|
|
|
SmallPtrSet<BasicBlock *, 16> OnStack;
|
|
|
|
SmallVector<std::pair<BasicBlock *, succ_iterator>, 16> SuccStack;
|
2012-04-25 06:53:18 +08:00
|
|
|
|
|
|
|
// Functions always have exactly one entry block, and we don't have
|
|
|
|
// any other block that we treat like an entry block.
|
2011-12-13 03:42:25 +08:00
|
|
|
BasicBlock *EntryBB = &F.getEntryBlock();
|
2012-05-09 07:39:44 +08:00
|
|
|
BBState &MyStates = BBStates[EntryBB];
|
|
|
|
MyStates.SetAsEntry();
|
|
|
|
TerminatorInst *EntryTI = cast<TerminatorInst>(&EntryBB->back());
|
|
|
|
SuccStack.push_back(std::make_pair(EntryBB, succ_iterator(EntryTI)));
|
2011-12-13 03:42:25 +08:00
|
|
|
Visited.insert(EntryBB);
|
|
|
|
OnStack.insert(EntryBB);
|
|
|
|
do {
|
|
|
|
dfs_next_succ:
|
2012-04-25 06:53:18 +08:00
|
|
|
BasicBlock *CurrBB = SuccStack.back().first;
|
|
|
|
TerminatorInst *TI = cast<TerminatorInst>(&CurrBB->back());
|
|
|
|
succ_iterator SE(TI, false);
|
2012-05-09 07:39:44 +08:00
|
|
|
|
2012-04-25 06:53:18 +08:00
|
|
|
while (SuccStack.back().second != SE) {
|
|
|
|
BasicBlock *SuccBB = *SuccStack.back().second++;
|
2014-11-19 15:49:26 +08:00
|
|
|
if (Visited.insert(SuccBB).second) {
|
2012-05-09 07:39:44 +08:00
|
|
|
TerminatorInst *TI = cast<TerminatorInst>(&SuccBB->back());
|
|
|
|
SuccStack.push_back(std::make_pair(SuccBB, succ_iterator(TI)));
|
2012-04-25 06:53:18 +08:00
|
|
|
BBStates[CurrBB].addSucc(SuccBB);
|
2012-05-09 07:39:44 +08:00
|
|
|
BBState &SuccStates = BBStates[SuccBB];
|
|
|
|
SuccStates.addPred(CurrBB);
|
2012-04-25 06:53:18 +08:00
|
|
|
OnStack.insert(SuccBB);
|
2011-12-13 03:42:25 +08:00
|
|
|
goto dfs_next_succ;
|
|
|
|
}
|
2012-04-25 06:53:18 +08:00
|
|
|
|
|
|
|
if (!OnStack.count(SuccBB)) {
|
|
|
|
BBStates[CurrBB].addSucc(SuccBB);
|
|
|
|
BBStates[SuccBB].addPred(CurrBB);
|
|
|
|
}
|
2011-12-13 03:42:25 +08:00
|
|
|
}
|
2012-04-25 06:53:18 +08:00
|
|
|
OnStack.erase(CurrBB);
|
|
|
|
PostOrder.push_back(CurrBB);
|
|
|
|
SuccStack.pop_back();
|
2011-12-13 03:42:25 +08:00
|
|
|
} while (!SuccStack.empty());
|
|
|
|
|
|
|
|
Visited.clear();
|
|
|
|
|
2012-04-25 06:53:18 +08:00
|
|
|
// Do reverse-CFG DFS, computing the reverse-CFG PostOrder.
|
|
|
|
// Functions may have many exits, and there also blocks which we treat
|
|
|
|
// as exits due to ignored edges.
|
|
|
|
SmallVector<std::pair<BasicBlock *, BBState::edge_iterator>, 16> PredStack;
|
2015-10-20 07:20:14 +08:00
|
|
|
for (BasicBlock &ExitBB : F) {
|
|
|
|
BBState &MyStates = BBStates[&ExitBB];
|
2012-04-25 06:53:18 +08:00
|
|
|
if (!MyStates.isExit())
|
|
|
|
continue;
|
2011-12-13 03:42:25 +08:00
|
|
|
|
2012-04-28 02:56:31 +08:00
|
|
|
MyStates.SetAsExit();
|
2012-04-25 06:53:18 +08:00
|
|
|
|
2015-10-20 07:20:14 +08:00
|
|
|
PredStack.push_back(std::make_pair(&ExitBB, MyStates.pred_begin()));
|
|
|
|
Visited.insert(&ExitBB);
|
2011-12-13 03:42:25 +08:00
|
|
|
while (!PredStack.empty()) {
|
|
|
|
reverse_dfs_next_succ:
|
2012-04-25 06:53:18 +08:00
|
|
|
BBState::edge_iterator PE = BBStates[PredStack.back().first].pred_end();
|
|
|
|
while (PredStack.back().second != PE) {
|
2011-12-13 03:42:25 +08:00
|
|
|
BasicBlock *BB = *PredStack.back().second++;
|
2014-11-19 15:49:26 +08:00
|
|
|
if (Visited.insert(BB).second) {
|
2012-04-25 06:53:18 +08:00
|
|
|
PredStack.push_back(std::make_pair(BB, BBStates[BB].pred_begin()));
|
2011-12-13 03:42:25 +08:00
|
|
|
goto reverse_dfs_next_succ;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ReverseCFGPostOrder.push_back(PredStack.pop_back_val().first);
|
|
|
|
}
|
2011-08-12 08:24:29 +08:00
|
|
|
}
|
2011-12-13 03:42:25 +08:00
|
|
|
}
|
|
|
|
|
2013-01-14 08:35:14 +08:00
|
|
|
// Visit the function both top-down and bottom-up.
|
2015-03-06 07:28:58 +08:00
|
|
|
bool ObjCARCOpt::Visit(Function &F,
|
|
|
|
DenseMap<const BasicBlock *, BBState> &BBStates,
|
|
|
|
BlotMapVector<Value *, RRInfo> &Retains,
|
|
|
|
DenseMap<Value *, RRInfo> &Releases) {
|
2011-12-13 03:42:25 +08:00
|
|
|
|
|
|
|
// Use reverse-postorder traversals, because we magically know that loops
|
|
|
|
// will be well behaved, i.e. they won't repeatedly call retain on a single
|
|
|
|
// pointer without doing a release. We can't use the ReversePostOrderTraversal
|
|
|
|
// class here because we want the reverse-CFG postorder to consider each
|
|
|
|
// function exit point, and we want to ignore selected cycle edges.
|
|
|
|
SmallVector<BasicBlock *, 16> PostOrder;
|
|
|
|
SmallVector<BasicBlock *, 16> ReverseCFGPostOrder;
|
2012-04-25 06:53:18 +08:00
|
|
|
ComputePostOrders(F, PostOrder, ReverseCFGPostOrder,
|
2015-03-16 15:02:27 +08:00
|
|
|
MDKindCache.get(ARCMDKindID::NoObjCARCExceptions),
|
|
|
|
BBStates);
|
2011-12-13 03:42:25 +08:00
|
|
|
|
|
|
|
// Use reverse-postorder on the reverse CFG for bottom-up.
|
2011-06-16 07:37:01 +08:00
|
|
|
bool BottomUpNestingDetected = false;
|
2011-08-19 05:27:42 +08:00
|
|
|
for (SmallVectorImpl<BasicBlock *>::const_reverse_iterator I =
|
2011-12-13 03:42:25 +08:00
|
|
|
ReverseCFGPostOrder.rbegin(), E = ReverseCFGPostOrder.rend();
|
|
|
|
I != E; ++I)
|
|
|
|
BottomUpNestingDetected |= VisitBottomUp(*I, BBStates, Retains);
|
2011-06-16 07:37:01 +08:00
|
|
|
|
2011-12-13 03:42:25 +08:00
|
|
|
// Use reverse-postorder for top-down.
|
2011-06-16 07:37:01 +08:00
|
|
|
bool TopDownNestingDetected = false;
|
2011-12-13 03:42:25 +08:00
|
|
|
for (SmallVectorImpl<BasicBlock *>::const_reverse_iterator I =
|
|
|
|
PostOrder.rbegin(), E = PostOrder.rend();
|
|
|
|
I != E; ++I)
|
|
|
|
TopDownNestingDetected |= VisitTopDown(*I, BBStates, Releases);
|
2011-06-16 07:37:01 +08:00
|
|
|
|
|
|
|
return TopDownNestingDetected && BottomUpNestingDetected;
|
|
|
|
}
|
|
|
|
|
2013-01-14 08:35:14 +08:00
|
|
|
/// Move the calls in RetainsToMove and ReleasesToMove.
|
2015-03-06 07:28:58 +08:00
|
|
|
void ObjCARCOpt::MoveCalls(Value *Arg, RRInfo &RetainsToMove,
|
2011-06-16 07:37:01 +08:00
|
|
|
RRInfo &ReleasesToMove,
|
2015-03-06 07:28:58 +08:00
|
|
|
BlotMapVector<Value *, RRInfo> &Retains,
|
2011-06-16 07:37:01 +08:00
|
|
|
DenseMap<Value *, RRInfo> &Releases,
|
2011-07-23 06:29:21 +08:00
|
|
|
SmallVectorImpl<Instruction *> &DeadInsts,
|
2013-04-06 07:46:45 +08:00
|
|
|
Module *M) {
|
2011-07-18 12:54:35 +08:00
|
|
|
Type *ArgTy = Arg->getType();
|
2011-07-23 06:29:21 +08:00
|
|
|
Type *ParamTy = PointerType::getUnqual(Type::getInt8Ty(ArgTy->getContext()));
|
2013-04-06 07:46:45 +08:00
|
|
|
|
2013-04-06 02:10:41 +08:00
|
|
|
DEBUG(dbgs() << "== ObjCARCOpt::MoveCalls ==\n");
|
2013-04-06 07:46:45 +08:00
|
|
|
|
2011-06-16 07:37:01 +08:00
|
|
|
// Insert the new retain and release calls.
|
2014-08-25 07:23:06 +08:00
|
|
|
for (Instruction *InsertPt : ReleasesToMove.ReverseInsertPts) {
|
2011-06-16 07:37:01 +08:00
|
|
|
Value *MyArg = ArgTy == ParamTy ? Arg :
|
|
|
|
new BitCastInst(Arg, ParamTy, "", InsertPt);
|
2015-03-16 15:02:24 +08:00
|
|
|
Constant *Decl = EP.get(ARCRuntimeEntryPointKind::Retain);
|
2013-07-06 09:39:23 +08:00
|
|
|
CallInst *Call = CallInst::Create(Decl, MyArg, "", InsertPt);
|
2011-06-16 07:37:01 +08:00
|
|
|
Call->setDoesNotThrow();
|
2013-03-29 07:08:44 +08:00
|
|
|
Call->setTailCall();
|
2013-03-29 13:13:07 +08:00
|
|
|
|
2013-04-21 08:30:50 +08:00
|
|
|
DEBUG(dbgs() << "Inserting new Retain: " << *Call << "\n"
|
2013-04-06 02:10:41 +08:00
|
|
|
"At insertion point: " << *InsertPt << "\n");
|
2011-06-16 07:37:01 +08:00
|
|
|
}
|
2014-08-25 07:23:06 +08:00
|
|
|
for (Instruction *InsertPt : RetainsToMove.ReverseInsertPts) {
|
2012-03-24 01:47:54 +08:00
|
|
|
Value *MyArg = ArgTy == ParamTy ? Arg :
|
|
|
|
new BitCastInst(Arg, ParamTy, "", InsertPt);
|
2015-03-16 15:02:24 +08:00
|
|
|
Constant *Decl = EP.get(ARCRuntimeEntryPointKind::Release);
|
2013-07-06 09:39:23 +08:00
|
|
|
CallInst *Call = CallInst::Create(Decl, MyArg, "", InsertPt);
|
2012-03-24 01:47:54 +08:00
|
|
|
// Attach a clang.imprecise_release metadata tag, if appropriate.
|
|
|
|
if (MDNode *M = ReleasesToMove.ReleaseMetadata)
|
2015-03-16 15:02:27 +08:00
|
|
|
Call->setMetadata(MDKindCache.get(ARCMDKindID::ImpreciseRelease), M);
|
2012-03-24 01:47:54 +08:00
|
|
|
Call->setDoesNotThrow();
|
|
|
|
if (ReleasesToMove.IsTailCallRelease)
|
|
|
|
Call->setTailCall();
|
2013-01-10 03:23:24 +08:00
|
|
|
|
2013-04-06 02:10:41 +08:00
|
|
|
DEBUG(dbgs() << "Inserting new Release: " << *Call << "\n"
|
|
|
|
"At insertion point: " << *InsertPt << "\n");
|
2011-06-16 07:37:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete the original retain and release calls.
|
2014-08-25 07:23:06 +08:00
|
|
|
for (Instruction *OrigRetain : RetainsToMove.Calls) {
|
2011-06-16 07:37:01 +08:00
|
|
|
Retains.blot(OrigRetain);
|
|
|
|
DeadInsts.push_back(OrigRetain);
|
2013-04-06 02:10:41 +08:00
|
|
|
DEBUG(dbgs() << "Deleting retain: " << *OrigRetain << "\n");
|
2011-06-16 07:37:01 +08:00
|
|
|
}
|
2014-08-25 07:23:06 +08:00
|
|
|
for (Instruction *OrigRelease : ReleasesToMove.Calls) {
|
2011-06-16 07:37:01 +08:00
|
|
|
Releases.erase(OrigRelease);
|
|
|
|
DeadInsts.push_back(OrigRelease);
|
2013-04-06 02:10:41 +08:00
|
|
|
DEBUG(dbgs() << "Deleting release: " << *OrigRelease << "\n");
|
2011-06-16 07:37:01 +08:00
|
|
|
}
|
2013-04-06 07:46:45 +08:00
|
|
|
|
2011-06-16 07:37:01 +08:00
|
|
|
}
|
|
|
|
|
2015-03-16 15:02:30 +08:00
|
|
|
bool ObjCARCOpt::PairUpRetainsAndReleases(
|
2015-03-06 07:28:58 +08:00
|
|
|
DenseMap<const BasicBlock *, BBState> &BBStates,
|
|
|
|
BlotMapVector<Value *, RRInfo> &Retains,
|
|
|
|
DenseMap<Value *, RRInfo> &Releases, Module *M,
|
|
|
|
SmallVectorImpl<Instruction *> &NewRetains,
|
|
|
|
SmallVectorImpl<Instruction *> &NewReleases,
|
|
|
|
SmallVectorImpl<Instruction *> &DeadInsts, RRInfo &RetainsToMove,
|
|
|
|
RRInfo &ReleasesToMove, Value *Arg, bool KnownSafe,
|
|
|
|
bool &AnyPairsCompletelyEliminated) {
|
2013-01-23 05:49:00 +08:00
|
|
|
// If a pair happens in a region where it is known that the reference count
|
2013-05-14 07:49:42 +08:00
|
|
|
// is already incremented, we can similarly ignore possible decrements unless
|
|
|
|
// we are dealing with a retainable object with multiple provenance sources.
|
2013-01-23 05:49:00 +08:00
|
|
|
bool KnownSafeTD = true, KnownSafeBU = true;
|
2013-05-14 07:49:42 +08:00
|
|
|
bool MultipleOwners = false;
|
2013-05-25 04:44:05 +08:00
|
|
|
bool CFGHazardAfflicted = false;
|
2013-01-23 05:49:00 +08:00
|
|
|
|
|
|
|
// Connect the dots between the top-down-collected RetainsToMove and
|
|
|
|
// bottom-up-collected ReleasesToMove to form sets of related calls.
|
|
|
|
// This is an iterative process so that we connect multiple releases
|
|
|
|
// to multiple retains if needed.
|
|
|
|
unsigned OldDelta = 0;
|
|
|
|
unsigned NewDelta = 0;
|
|
|
|
unsigned OldCount = 0;
|
|
|
|
unsigned NewCount = 0;
|
|
|
|
bool FirstRelease = true;
|
|
|
|
for (;;) {
|
|
|
|
for (SmallVectorImpl<Instruction *>::const_iterator
|
|
|
|
NI = NewRetains.begin(), NE = NewRetains.end(); NI != NE; ++NI) {
|
|
|
|
Instruction *NewRetain = *NI;
|
2015-03-06 10:10:03 +08:00
|
|
|
auto It = Retains.find(NewRetain);
|
2013-01-23 05:49:00 +08:00
|
|
|
assert(It != Retains.end());
|
|
|
|
const RRInfo &NewRetainRRI = It->second;
|
|
|
|
KnownSafeTD &= NewRetainRRI.KnownSafe;
|
2013-05-25 04:44:02 +08:00
|
|
|
MultipleOwners =
|
2015-02-19 08:42:38 +08:00
|
|
|
MultipleOwners || MultiOwnersSet.count(GetArgRCIdentityRoot(NewRetain));
|
2014-08-25 07:23:06 +08:00
|
|
|
for (Instruction *NewRetainRelease : NewRetainRRI.Calls) {
|
2015-03-06 10:10:03 +08:00
|
|
|
auto Jt = Releases.find(NewRetainRelease);
|
2013-01-23 05:49:00 +08:00
|
|
|
if (Jt == Releases.end())
|
|
|
|
return false;
|
|
|
|
const RRInfo &NewRetainReleaseRRI = Jt->second;
|
2013-11-06 00:02:40 +08:00
|
|
|
|
|
|
|
// If the release does not have a reference to the retain as well,
|
|
|
|
// something happened which is unaccounted for. Do not do anything.
|
|
|
|
//
|
|
|
|
// This can happen if we catch an additive overflow during path count
|
|
|
|
// merging.
|
|
|
|
if (!NewRetainReleaseRRI.Calls.count(NewRetain))
|
|
|
|
return false;
|
|
|
|
|
2014-11-19 15:49:26 +08:00
|
|
|
if (ReleasesToMove.Calls.insert(NewRetainRelease).second) {
|
2013-06-07 14:16:49 +08:00
|
|
|
|
|
|
|
// If we overflow when we compute the path count, don't remove/move
|
|
|
|
// anything.
|
|
|
|
const BBState &NRRBBState = BBStates[NewRetainRelease->getParent()];
|
2013-08-10 07:22:27 +08:00
|
|
|
unsigned PathCount = BBState::OverflowOccurredValue;
|
2013-06-07 14:16:49 +08:00
|
|
|
if (NRRBBState.GetAllPathCountWithOverflow(PathCount))
|
|
|
|
return false;
|
2013-08-10 07:22:27 +08:00
|
|
|
assert(PathCount != BBState::OverflowOccurredValue &&
|
|
|
|
"PathCount at this point can not be "
|
|
|
|
"OverflowOccurredValue.");
|
2013-06-07 14:16:49 +08:00
|
|
|
OldDelta -= PathCount;
|
2013-01-23 05:49:00 +08:00
|
|
|
|
|
|
|
// Merge the ReleaseMetadata and IsTailCallRelease values.
|
|
|
|
if (FirstRelease) {
|
|
|
|
ReleasesToMove.ReleaseMetadata =
|
|
|
|
NewRetainReleaseRRI.ReleaseMetadata;
|
|
|
|
ReleasesToMove.IsTailCallRelease =
|
|
|
|
NewRetainReleaseRRI.IsTailCallRelease;
|
|
|
|
FirstRelease = false;
|
|
|
|
} else {
|
|
|
|
if (ReleasesToMove.ReleaseMetadata !=
|
|
|
|
NewRetainReleaseRRI.ReleaseMetadata)
|
2014-04-25 13:29:35 +08:00
|
|
|
ReleasesToMove.ReleaseMetadata = nullptr;
|
2013-01-23 05:49:00 +08:00
|
|
|
if (ReleasesToMove.IsTailCallRelease !=
|
|
|
|
NewRetainReleaseRRI.IsTailCallRelease)
|
|
|
|
ReleasesToMove.IsTailCallRelease = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Collect the optimal insertion points.
|
|
|
|
if (!KnownSafe)
|
2014-08-25 07:23:06 +08:00
|
|
|
for (Instruction *RIP : NewRetainReleaseRRI.ReverseInsertPts) {
|
2014-11-19 15:49:26 +08:00
|
|
|
if (ReleasesToMove.ReverseInsertPts.insert(RIP).second) {
|
2013-06-07 14:16:49 +08:00
|
|
|
// If we overflow when we compute the path count, don't
|
|
|
|
// remove/move anything.
|
|
|
|
const BBState &RIPBBState = BBStates[RIP->getParent()];
|
2013-08-10 07:22:27 +08:00
|
|
|
PathCount = BBState::OverflowOccurredValue;
|
2013-06-07 14:16:49 +08:00
|
|
|
if (RIPBBState.GetAllPathCountWithOverflow(PathCount))
|
|
|
|
return false;
|
2013-08-10 07:22:27 +08:00
|
|
|
assert(PathCount != BBState::OverflowOccurredValue &&
|
|
|
|
"PathCount at this point can not be "
|
|
|
|
"OverflowOccurredValue.");
|
2013-06-07 14:16:49 +08:00
|
|
|
NewDelta -= PathCount;
|
|
|
|
}
|
2013-01-23 05:49:00 +08:00
|
|
|
}
|
|
|
|
NewReleases.push_back(NewRetainRelease);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
NewRetains.clear();
|
|
|
|
if (NewReleases.empty()) break;
|
|
|
|
|
|
|
|
// Back the other way.
|
|
|
|
for (SmallVectorImpl<Instruction *>::const_iterator
|
|
|
|
NI = NewReleases.begin(), NE = NewReleases.end(); NI != NE; ++NI) {
|
|
|
|
Instruction *NewRelease = *NI;
|
2015-03-06 10:10:03 +08:00
|
|
|
auto It = Releases.find(NewRelease);
|
2013-01-23 05:49:00 +08:00
|
|
|
assert(It != Releases.end());
|
|
|
|
const RRInfo &NewReleaseRRI = It->second;
|
|
|
|
KnownSafeBU &= NewReleaseRRI.KnownSafe;
|
2013-05-25 04:44:05 +08:00
|
|
|
CFGHazardAfflicted |= NewReleaseRRI.CFGHazardAfflicted;
|
2014-08-25 07:23:06 +08:00
|
|
|
for (Instruction *NewReleaseRetain : NewReleaseRRI.Calls) {
|
2015-03-06 10:10:03 +08:00
|
|
|
auto Jt = Retains.find(NewReleaseRetain);
|
2013-01-23 05:49:00 +08:00
|
|
|
if (Jt == Retains.end())
|
|
|
|
return false;
|
|
|
|
const RRInfo &NewReleaseRetainRRI = Jt->second;
|
2013-06-07 14:16:49 +08:00
|
|
|
|
2013-11-06 00:02:40 +08:00
|
|
|
// If the retain does not have a reference to the release as well,
|
|
|
|
// something happened which is unaccounted for. Do not do anything.
|
|
|
|
//
|
|
|
|
// This can happen if we catch an additive overflow during path count
|
|
|
|
// merging.
|
|
|
|
if (!NewReleaseRetainRRI.Calls.count(NewRelease))
|
|
|
|
return false;
|
|
|
|
|
2014-11-19 15:49:26 +08:00
|
|
|
if (RetainsToMove.Calls.insert(NewReleaseRetain).second) {
|
2013-06-07 14:16:49 +08:00
|
|
|
// If we overflow when we compute the path count, don't remove/move
|
|
|
|
// anything.
|
|
|
|
const BBState &NRRBBState = BBStates[NewReleaseRetain->getParent()];
|
2013-08-10 07:22:27 +08:00
|
|
|
unsigned PathCount = BBState::OverflowOccurredValue;
|
2013-06-07 14:16:49 +08:00
|
|
|
if (NRRBBState.GetAllPathCountWithOverflow(PathCount))
|
|
|
|
return false;
|
2013-08-10 07:22:27 +08:00
|
|
|
assert(PathCount != BBState::OverflowOccurredValue &&
|
|
|
|
"PathCount at this point can not be "
|
|
|
|
"OverflowOccurredValue.");
|
2013-01-23 05:49:00 +08:00
|
|
|
OldDelta += PathCount;
|
|
|
|
OldCount += PathCount;
|
|
|
|
|
|
|
|
// Collect the optimal insertion points.
|
|
|
|
if (!KnownSafe)
|
2014-08-25 07:23:06 +08:00
|
|
|
for (Instruction *RIP : NewReleaseRetainRRI.ReverseInsertPts) {
|
2014-11-19 15:49:26 +08:00
|
|
|
if (RetainsToMove.ReverseInsertPts.insert(RIP).second) {
|
2013-06-07 14:16:49 +08:00
|
|
|
// If we overflow when we compute the path count, don't
|
|
|
|
// remove/move anything.
|
|
|
|
const BBState &RIPBBState = BBStates[RIP->getParent()];
|
2013-08-10 07:22:27 +08:00
|
|
|
|
|
|
|
PathCount = BBState::OverflowOccurredValue;
|
2013-06-07 14:16:49 +08:00
|
|
|
if (RIPBBState.GetAllPathCountWithOverflow(PathCount))
|
|
|
|
return false;
|
2013-08-10 07:22:27 +08:00
|
|
|
assert(PathCount != BBState::OverflowOccurredValue &&
|
|
|
|
"PathCount at this point can not be "
|
|
|
|
"OverflowOccurredValue.");
|
2013-01-23 05:49:00 +08:00
|
|
|
NewDelta += PathCount;
|
|
|
|
NewCount += PathCount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
NewRetains.push_back(NewReleaseRetain);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
NewReleases.clear();
|
|
|
|
if (NewRetains.empty()) break;
|
|
|
|
}
|
|
|
|
|
[objc-arc] Make the ARC optimizer more conservative by forcing it to be non-safe in both direction, but mitigate the problem by noting that we just care if there was a further use.
The problem here is the infamous one direction known safe. I was
hesitant to turn it off before b/c of the potential for regressions
without an actual bug from users hitting the problem. This is that bug ;
).
The main performance impact of having known safe in both directions is
that often times it is very difficult to find two releases without a use
in-between them since we are so conservative with determining potential
uses. The one direction known safe gets around that problem by taking
advantage of many situations where we have two retains in a row,
allowing us to avoid that problem. That being said, the one direction
known safe is unsafe. Consider the following situation:
retain(x)
retain(x)
call(x)
call(x)
release(x)
Then we know the following about the reference count of x:
// rc(x) == N (for some N).
retain(x)
// rc(x) == N+1
retain(x)
// rc(x) == N+2
call A(x)
call B(x)
// rc(x) >= 1 (since we can not release a deallocated pointer).
release(x)
// rc(x) >= 0
That is all the information that we can know statically. That means that
we know that A(x), B(x) together can release (x) at most N+1 times. Lets
say that we remove the inner retain, release pair.
// rc(x) == N (for some N).
retain(x)
// rc(x) == N+1
call A(x)
call B(x)
// rc(x) >= 1
release(x)
// rc(x) >= 0
We knew before that A(x), B(x) could release x up to N+1 times meaning
that rc(x) may be zero at the release(x). That is not safe. On the other
hand, consider the following situation where we have a must use of
release(x) that x must be kept alive for after the release(x)**. Then we
know that:
// rc(x) == N (for some N).
retain(x)
// rc(x) == N+1
retain(x)
// rc(x) == N+2
call A(x)
call B(x)
// rc(x) >= 2 (since we know that we are going to release x and that that release can not be the last use of x).
release(x)
// rc(x) >= 1 (since we can not deallocate the pointer since we have a must use after x).
…
// rc(x) >= 1
use(x)
Thus we know that statically the calls to A(x), B(x) can together only
release rc(x) N times. Thus if we remove the inner retain, release pair:
// rc(x) == N (for some N).
retain(x)
// rc(x) == N+1
call A(x)
call B(x)
// rc(x) >= 1
…
// rc(x) >= 1
use(x)
We are still safe unless in the final … there are unbalanced retains,
releases which would have caused the program to blow up anyways even
before optimization occurred. The simplest form of must use is an
additional release that has not been paired up with any retain (if we
had paired the release with a retain and removed it we would not have
the additional use). This fits nicely into the ARC framework since
basically what you do is say that given any nested releases regardless
of what is in between, the inner release is known safe. This enables us to get
back the lost performance.
<rdar://problem/19023795>
llvm-svn: 232351
2015-03-16 15:02:36 +08:00
|
|
|
// We can only remove pointers if we are known safe in both directions.
|
|
|
|
bool UnconditionallySafe = KnownSafeTD && KnownSafeBU;
|
2013-05-14 07:49:42 +08:00
|
|
|
if (UnconditionallySafe) {
|
2013-01-23 05:49:00 +08:00
|
|
|
RetainsToMove.ReverseInsertPts.clear();
|
|
|
|
ReleasesToMove.ReverseInsertPts.clear();
|
|
|
|
NewCount = 0;
|
|
|
|
} else {
|
|
|
|
// Determine whether the new insertion points we computed preserve the
|
|
|
|
// balance of retain and release calls through the program.
|
|
|
|
// TODO: If the fully aggressive solution isn't valid, try to find a
|
|
|
|
// less aggressive solution which is.
|
|
|
|
if (NewDelta != 0)
|
|
|
|
return false;
|
2013-05-25 04:44:05 +08:00
|
|
|
|
|
|
|
// At this point, we are not going to remove any RR pairs, but we still are
|
|
|
|
// able to move RR pairs. If one of our pointers is afflicted with
|
|
|
|
// CFGHazards, we cannot perform such code motion so exit early.
|
|
|
|
const bool WillPerformCodeMotion = RetainsToMove.ReverseInsertPts.size() ||
|
|
|
|
ReleasesToMove.ReverseInsertPts.size();
|
|
|
|
if (CFGHazardAfflicted && WillPerformCodeMotion)
|
|
|
|
return false;
|
2013-01-23 05:49:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Determine whether the original call points are balanced in the retain and
|
|
|
|
// release calls through the program. If not, conservatively don't touch
|
|
|
|
// them.
|
|
|
|
// TODO: It's theoretically possible to do code motion in this case, as
|
|
|
|
// long as the existing imbalances are maintained.
|
|
|
|
if (OldDelta != 0)
|
|
|
|
return false;
|
2013-04-29 14:16:55 +08:00
|
|
|
|
2013-01-23 05:49:00 +08:00
|
|
|
Changed = true;
|
|
|
|
assert(OldCount != 0 && "Unreachable code?");
|
|
|
|
NumRRs += OldCount - NewCount;
|
|
|
|
// Set to true if we completely removed any RR pairs.
|
2013-01-23 05:53:43 +08:00
|
|
|
AnyPairsCompletelyEliminated = NewCount == 0;
|
2013-01-23 05:49:00 +08:00
|
|
|
|
|
|
|
// We can move calls!
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-01-14 08:35:14 +08:00
|
|
|
/// Identify pairings between the retains and releases, and delete and/or move
|
|
|
|
/// them.
|
2015-03-06 07:28:58 +08:00
|
|
|
bool ObjCARCOpt::PerformCodePlacement(
|
|
|
|
DenseMap<const BasicBlock *, BBState> &BBStates,
|
|
|
|
BlotMapVector<Value *, RRInfo> &Retains,
|
|
|
|
DenseMap<Value *, RRInfo> &Releases, Module *M) {
|
2013-04-06 02:10:41 +08:00
|
|
|
DEBUG(dbgs() << "\n== ObjCARCOpt::PerformCodePlacement ==\n");
|
|
|
|
|
2011-06-16 07:37:01 +08:00
|
|
|
bool AnyPairsCompletelyEliminated = false;
|
|
|
|
RRInfo RetainsToMove;
|
|
|
|
RRInfo ReleasesToMove;
|
|
|
|
SmallVector<Instruction *, 4> NewRetains;
|
|
|
|
SmallVector<Instruction *, 4> NewReleases;
|
|
|
|
SmallVector<Instruction *, 8> DeadInsts;
|
|
|
|
|
2012-04-14 02:57:48 +08:00
|
|
|
// Visit each retain.
|
2015-03-06 07:28:58 +08:00
|
|
|
for (BlotMapVector<Value *, RRInfo>::const_iterator I = Retains.begin(),
|
|
|
|
E = Retains.end();
|
|
|
|
I != E; ++I) {
|
2011-09-30 06:25:23 +08:00
|
|
|
Value *V = I->first;
|
2011-06-16 07:37:01 +08:00
|
|
|
if (!V) continue; // blotted
|
|
|
|
|
|
|
|
Instruction *Retain = cast<Instruction>(V);
|
2013-01-10 03:23:24 +08:00
|
|
|
|
2013-04-06 02:10:41 +08:00
|
|
|
DEBUG(dbgs() << "Visiting: " << *Retain << "\n");
|
2013-01-10 03:23:24 +08:00
|
|
|
|
2015-02-19 08:42:38 +08:00
|
|
|
Value *Arg = GetArgRCIdentityRoot(Retain);
|
2011-06-16 07:37:01 +08:00
|
|
|
|
2012-01-13 08:39:07 +08:00
|
|
|
// If the object being released is in static or stack storage, we know it's
|
2011-06-16 07:37:01 +08:00
|
|
|
// not being managed by ObjC reference counting, so we can delete pairs
|
|
|
|
// regardless of what possible decrements or uses lie between them.
|
2012-01-13 08:39:07 +08:00
|
|
|
bool KnownSafe = isa<Constant>(Arg) || isa<AllocaInst>(Arg);
|
2012-05-09 07:39:44 +08:00
|
|
|
|
2011-08-23 01:29:11 +08:00
|
|
|
// 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 (const LoadInst *LI = dyn_cast<LoadInst>(Arg))
|
|
|
|
if (const GlobalVariable *GV =
|
|
|
|
dyn_cast<GlobalVariable>(
|
2015-02-19 08:42:38 +08:00
|
|
|
GetRCIdentityRoot(LI->getPointerOperand())))
|
2011-08-23 01:29:11 +08:00
|
|
|
if (GV->isConstant())
|
|
|
|
KnownSafe = true;
|
|
|
|
|
2011-06-16 07:37:01 +08:00
|
|
|
// Connect the dots between the top-down-collected RetainsToMove and
|
|
|
|
// bottom-up-collected ReleasesToMove to form sets of related calls.
|
|
|
|
NewRetains.push_back(Retain);
|
2015-03-16 15:02:30 +08:00
|
|
|
bool PerformMoveCalls = PairUpRetainsAndReleases(
|
|
|
|
BBStates, Retains, Releases, M, NewRetains, NewReleases, DeadInsts,
|
|
|
|
RetainsToMove, ReleasesToMove, Arg, KnownSafe,
|
|
|
|
AnyPairsCompletelyEliminated);
|
2011-06-16 07:37:01 +08:00
|
|
|
|
2013-01-23 05:49:00 +08:00
|
|
|
if (PerformMoveCalls) {
|
|
|
|
// Ok, everything checks out and we're all set. Let's move/delete some
|
|
|
|
// code!
|
|
|
|
MoveCalls(Arg, RetainsToMove, ReleasesToMove,
|
|
|
|
Retains, Releases, DeadInsts, M);
|
|
|
|
}
|
2011-06-16 07:37:01 +08:00
|
|
|
|
2013-01-23 05:49:00 +08:00
|
|
|
// Clean up state for next retain.
|
2011-06-16 07:37:01 +08:00
|
|
|
NewReleases.clear();
|
|
|
|
NewRetains.clear();
|
|
|
|
RetainsToMove.clear();
|
|
|
|
ReleasesToMove.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now that we're done moving everything, we can delete the newly dead
|
|
|
|
// instructions, as we no longer need them as insert points.
|
|
|
|
while (!DeadInsts.empty())
|
|
|
|
EraseInstruction(DeadInsts.pop_back_val());
|
|
|
|
|
|
|
|
return AnyPairsCompletelyEliminated;
|
|
|
|
}
|
|
|
|
|
2013-01-14 08:35:14 +08:00
|
|
|
/// Weak pointer optimizations.
|
2011-06-16 07:37:01 +08:00
|
|
|
void ObjCARCOpt::OptimizeWeakCalls(Function &F) {
|
2013-04-06 02:10:41 +08:00
|
|
|
DEBUG(dbgs() << "\n== ObjCARCOpt::OptimizeWeakCalls ==\n");
|
2013-04-06 07:46:45 +08:00
|
|
|
|
2011-06-16 07:37:01 +08:00
|
|
|
// First, do memdep-style RLE and S2L optimizations. We can't use memdep
|
|
|
|
// itself because it uses AliasAnalysis and we need to do provenance
|
|
|
|
// queries instead.
|
|
|
|
for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ) {
|
|
|
|
Instruction *Inst = &*I++;
|
2013-01-02 00:05:48 +08:00
|
|
|
|
2013-04-06 02:10:41 +08:00
|
|
|
DEBUG(dbgs() << "Visiting: " << *Inst << "\n");
|
2013-01-02 00:05:48 +08:00
|
|
|
|
2015-02-20 03:51:32 +08:00
|
|
|
ARCInstKind Class = GetBasicARCInstKind(Inst);
|
|
|
|
if (Class != ARCInstKind::LoadWeak &&
|
|
|
|
Class != ARCInstKind::LoadWeakRetained)
|
2011-06-16 07:37:01 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// Delete objc_loadWeak calls with no users.
|
2015-02-20 03:51:32 +08:00
|
|
|
if (Class == ARCInstKind::LoadWeak && Inst->use_empty()) {
|
2011-06-16 07:37:01 +08:00
|
|
|
Inst->eraseFromParent();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: For now, just look for an earlier available version of this value
|
|
|
|
// within the same block. Theoretically, we could do memdep-style non-local
|
|
|
|
// analysis too, but that would want caching. A better approach would be to
|
|
|
|
// use the technique that EarlyCSE uses.
|
2014-03-02 20:27:27 +08:00
|
|
|
inst_iterator Current = std::prev(I);
|
2015-10-20 07:20:14 +08:00
|
|
|
BasicBlock *CurrentBB = &*Current.getBasicBlockIterator();
|
2011-06-16 07:37:01 +08:00
|
|
|
for (BasicBlock::iterator B = CurrentBB->begin(),
|
|
|
|
J = Current.getInstructionIterator();
|
|
|
|
J != B; --J) {
|
2014-03-02 20:27:27 +08:00
|
|
|
Instruction *EarlierInst = &*std::prev(J);
|
2015-02-20 03:51:32 +08:00
|
|
|
ARCInstKind EarlierClass = GetARCInstKind(EarlierInst);
|
2011-06-16 07:37:01 +08:00
|
|
|
switch (EarlierClass) {
|
2015-02-20 03:51:32 +08:00
|
|
|
case ARCInstKind::LoadWeak:
|
|
|
|
case ARCInstKind::LoadWeakRetained: {
|
2011-06-16 07:37:01 +08:00
|
|
|
// If this is loading from the same pointer, replace this load's value
|
|
|
|
// with that one.
|
|
|
|
CallInst *Call = cast<CallInst>(Inst);
|
|
|
|
CallInst *EarlierCall = cast<CallInst>(EarlierInst);
|
|
|
|
Value *Arg = Call->getArgOperand(0);
|
|
|
|
Value *EarlierArg = EarlierCall->getArgOperand(0);
|
|
|
|
switch (PA.getAA()->alias(Arg, EarlierArg)) {
|
2015-06-22 10:16:51 +08:00
|
|
|
case MustAlias:
|
2011-06-16 07:37:01 +08:00
|
|
|
Changed = true;
|
|
|
|
// If the load has a builtin retain, insert a plain retain for it.
|
2015-02-20 03:51:32 +08:00
|
|
|
if (Class == ARCInstKind::LoadWeakRetained) {
|
2015-03-16 15:02:24 +08:00
|
|
|
Constant *Decl = EP.get(ARCRuntimeEntryPointKind::Retain);
|
2013-07-06 09:39:23 +08:00
|
|
|
CallInst *CI = CallInst::Create(Decl, EarlierCall, "", Call);
|
2011-06-16 07:37:01 +08:00
|
|
|
CI->setTailCall();
|
|
|
|
}
|
|
|
|
// Zap the fully redundant load.
|
|
|
|
Call->replaceAllUsesWith(EarlierCall);
|
|
|
|
Call->eraseFromParent();
|
|
|
|
goto clobbered;
|
2015-06-22 10:16:51 +08:00
|
|
|
case MayAlias:
|
|
|
|
case PartialAlias:
|
2011-06-16 07:37:01 +08:00
|
|
|
goto clobbered;
|
2015-06-22 10:16:51 +08:00
|
|
|
case NoAlias:
|
2011-06-16 07:37:01 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2015-02-20 03:51:32 +08:00
|
|
|
case ARCInstKind::StoreWeak:
|
|
|
|
case ARCInstKind::InitWeak: {
|
2011-06-16 07:37:01 +08:00
|
|
|
// If this is storing to the same pointer and has the same size etc.
|
|
|
|
// replace this load's value with the stored value.
|
|
|
|
CallInst *Call = cast<CallInst>(Inst);
|
|
|
|
CallInst *EarlierCall = cast<CallInst>(EarlierInst);
|
|
|
|
Value *Arg = Call->getArgOperand(0);
|
|
|
|
Value *EarlierArg = EarlierCall->getArgOperand(0);
|
|
|
|
switch (PA.getAA()->alias(Arg, EarlierArg)) {
|
2015-06-22 10:16:51 +08:00
|
|
|
case MustAlias:
|
2011-06-16 07:37:01 +08:00
|
|
|
Changed = true;
|
|
|
|
// If the load has a builtin retain, insert a plain retain for it.
|
2015-02-20 03:51:32 +08:00
|
|
|
if (Class == ARCInstKind::LoadWeakRetained) {
|
2015-03-16 15:02:24 +08:00
|
|
|
Constant *Decl = EP.get(ARCRuntimeEntryPointKind::Retain);
|
2013-07-06 09:39:23 +08:00
|
|
|
CallInst *CI = CallInst::Create(Decl, EarlierCall, "", Call);
|
2011-06-16 07:37:01 +08:00
|
|
|
CI->setTailCall();
|
|
|
|
}
|
|
|
|
// Zap the fully redundant load.
|
|
|
|
Call->replaceAllUsesWith(EarlierCall->getArgOperand(1));
|
|
|
|
Call->eraseFromParent();
|
|
|
|
goto clobbered;
|
2015-06-22 10:16:51 +08:00
|
|
|
case MayAlias:
|
|
|
|
case PartialAlias:
|
2011-06-16 07:37:01 +08:00
|
|
|
goto clobbered;
|
2015-06-22 10:16:51 +08:00
|
|
|
case NoAlias:
|
2011-06-16 07:37:01 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2015-02-20 03:51:32 +08:00
|
|
|
case ARCInstKind::MoveWeak:
|
|
|
|
case ARCInstKind::CopyWeak:
|
2011-06-16 07:37:01 +08:00
|
|
|
// TOOD: Grab the copied value.
|
|
|
|
goto clobbered;
|
2015-02-20 03:51:32 +08:00
|
|
|
case ARCInstKind::AutoreleasepoolPush:
|
|
|
|
case ARCInstKind::None:
|
|
|
|
case ARCInstKind::IntrinsicUser:
|
|
|
|
case ARCInstKind::User:
|
2011-06-16 07:37:01 +08:00
|
|
|
// Weak pointers are only modified through the weak entry points
|
|
|
|
// (and arbitrary calls, which could call the weak entry points).
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// Anything else could modify the weak pointer.
|
|
|
|
goto clobbered;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
clobbered:;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Then, for each destroyWeak with an alloca operand, check to see if
|
|
|
|
// the alloca and all its users can be zapped.
|
|
|
|
for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ) {
|
|
|
|
Instruction *Inst = &*I++;
|
2015-02-20 03:51:32 +08:00
|
|
|
ARCInstKind Class = GetBasicARCInstKind(Inst);
|
|
|
|
if (Class != ARCInstKind::DestroyWeak)
|
2011-06-16 07:37:01 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
CallInst *Call = cast<CallInst>(Inst);
|
|
|
|
Value *Arg = Call->getArgOperand(0);
|
|
|
|
if (AllocaInst *Alloca = dyn_cast<AllocaInst>(Arg)) {
|
2014-03-09 11:16:01 +08:00
|
|
|
for (User *U : Alloca->users()) {
|
|
|
|
const Instruction *UserInst = cast<Instruction>(U);
|
2015-02-20 03:51:32 +08:00
|
|
|
switch (GetBasicARCInstKind(UserInst)) {
|
|
|
|
case ARCInstKind::InitWeak:
|
|
|
|
case ARCInstKind::StoreWeak:
|
|
|
|
case ARCInstKind::DestroyWeak:
|
2011-06-16 07:37:01 +08:00
|
|
|
continue;
|
|
|
|
default:
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Changed = true;
|
2014-03-09 11:16:01 +08:00
|
|
|
for (auto UI = Alloca->user_begin(), UE = Alloca->user_end(); UI != UE;) {
|
2011-06-16 07:37:01 +08:00
|
|
|
CallInst *UserInst = cast<CallInst>(*UI++);
|
2015-02-20 03:51:32 +08:00
|
|
|
switch (GetBasicARCInstKind(UserInst)) {
|
|
|
|
case ARCInstKind::InitWeak:
|
|
|
|
case ARCInstKind::StoreWeak:
|
2012-05-19 06:17:29 +08:00
|
|
|
// These functions return their second argument.
|
|
|
|
UserInst->replaceAllUsesWith(UserInst->getArgOperand(1));
|
|
|
|
break;
|
2015-02-20 03:51:32 +08:00
|
|
|
case ARCInstKind::DestroyWeak:
|
2012-05-19 06:17:29 +08:00
|
|
|
// No return value.
|
|
|
|
break;
|
|
|
|
default:
|
2012-05-22 01:41:28 +08:00
|
|
|
llvm_unreachable("alloca really is used!");
|
2012-05-19 06:17:29 +08:00
|
|
|
}
|
2011-06-16 07:37:01 +08:00
|
|
|
UserInst->eraseFromParent();
|
|
|
|
}
|
|
|
|
Alloca->eraseFromParent();
|
|
|
|
done:;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-14 08:35:14 +08:00
|
|
|
/// Identify program paths which execute sequences of retains and releases which
|
|
|
|
/// can be eliminated.
|
2011-06-16 07:37:01 +08:00
|
|
|
bool ObjCARCOpt::OptimizeSequences(Function &F) {
|
2013-05-23 10:35:21 +08:00
|
|
|
// Releases, Retains - These are used to store the results of the main flow
|
|
|
|
// analysis. These use Value* as the key instead of Instruction* so that the
|
|
|
|
// map stays valid when we get around to rewriting code and calls get
|
|
|
|
// replaced by arguments.
|
2011-06-16 07:37:01 +08:00
|
|
|
DenseMap<Value *, RRInfo> Releases;
|
2015-03-06 07:28:58 +08:00
|
|
|
BlotMapVector<Value *, RRInfo> Retains;
|
2011-06-16 07:37:01 +08:00
|
|
|
|
2013-05-23 10:35:21 +08:00
|
|
|
// This is used during the traversal of the function to track the
|
|
|
|
// states for each identified object at each block.
|
2011-06-16 07:37:01 +08:00
|
|
|
DenseMap<const BasicBlock *, BBState> BBStates;
|
|
|
|
|
|
|
|
// Analyze the CFG of the function, and all instructions.
|
|
|
|
bool NestingDetected = Visit(F, BBStates, Retains, Releases);
|
|
|
|
|
|
|
|
// Transform.
|
2013-05-25 04:44:02 +08:00
|
|
|
bool AnyPairsCompletelyEliminated = PerformCodePlacement(BBStates, Retains,
|
|
|
|
Releases,
|
|
|
|
F.getParent());
|
|
|
|
|
|
|
|
// Cleanup.
|
|
|
|
MultiOwnersSet.clear();
|
|
|
|
|
|
|
|
return AnyPairsCompletelyEliminated && NestingDetected;
|
2011-06-16 07:37:01 +08:00
|
|
|
}
|
|
|
|
|
2013-04-04 07:07:45 +08:00
|
|
|
/// Check if there is a dependent call earlier that does not have anything in
|
|
|
|
/// between the Retain and the call that can affect the reference count of their
|
|
|
|
/// shared pointer argument. Note that Retain need not be in BB.
|
2013-04-04 07:04:28 +08:00
|
|
|
static bool
|
|
|
|
HasSafePathToPredecessorCall(const Value *Arg, Instruction *Retain,
|
2014-08-21 13:55:13 +08:00
|
|
|
SmallPtrSetImpl<Instruction *> &DepInsts,
|
|
|
|
SmallPtrSetImpl<const BasicBlock *> &Visited,
|
2013-04-04 07:04:28 +08:00
|
|
|
ProvenanceAnalysis &PA) {
|
|
|
|
FindDependencies(CanChangeRetainCount, Arg, Retain->getParent(), Retain,
|
|
|
|
DepInsts, Visited, PA);
|
|
|
|
if (DepInsts.size() != 1)
|
|
|
|
return false;
|
2013-04-04 07:07:45 +08:00
|
|
|
|
2015-03-06 07:29:06 +08:00
|
|
|
auto *Call = dyn_cast_or_null<CallInst>(*DepInsts.begin());
|
2013-04-04 07:07:45 +08:00
|
|
|
|
2013-04-04 07:04:28 +08:00
|
|
|
// Check that the pointer is the return value of the call.
|
|
|
|
if (!Call || Arg != Call)
|
|
|
|
return false;
|
2013-04-04 07:07:45 +08:00
|
|
|
|
2013-04-04 07:04:28 +08:00
|
|
|
// Check that the call is a regular call.
|
2015-02-20 03:51:32 +08:00
|
|
|
ARCInstKind Class = GetBasicARCInstKind(Call);
|
|
|
|
if (Class != ARCInstKind::CallOrUser && Class != ARCInstKind::Call)
|
2013-04-04 07:04:28 +08:00
|
|
|
return false;
|
2013-04-04 07:07:45 +08:00
|
|
|
|
2013-04-04 07:04:28 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-04-04 07:16:05 +08:00
|
|
|
/// Find a dependent retain that precedes the given autorelease for which there
|
|
|
|
/// is nothing in between the two instructions that can affect the ref count of
|
|
|
|
/// Arg.
|
|
|
|
static CallInst *
|
|
|
|
FindPredecessorRetainWithSafePath(const Value *Arg, BasicBlock *BB,
|
|
|
|
Instruction *Autorelease,
|
2014-08-21 13:55:13 +08:00
|
|
|
SmallPtrSetImpl<Instruction *> &DepInsts,
|
|
|
|
SmallPtrSetImpl<const BasicBlock *> &Visited,
|
2013-04-04 07:16:05 +08:00
|
|
|
ProvenanceAnalysis &PA) {
|
|
|
|
FindDependencies(CanChangeRetainCount, Arg,
|
|
|
|
BB, Autorelease, DepInsts, Visited, PA);
|
|
|
|
if (DepInsts.size() != 1)
|
2014-04-25 13:29:35 +08:00
|
|
|
return nullptr;
|
2013-04-06 07:46:45 +08:00
|
|
|
|
2015-03-06 07:29:06 +08:00
|
|
|
auto *Retain = dyn_cast_or_null<CallInst>(*DepInsts.begin());
|
2013-04-06 07:46:45 +08:00
|
|
|
|
2013-04-04 07:16:05 +08:00
|
|
|
// Check that we found a retain with the same argument.
|
2015-02-20 03:51:32 +08:00
|
|
|
if (!Retain || !IsRetain(GetBasicARCInstKind(Retain)) ||
|
2015-02-19 08:42:38 +08:00
|
|
|
GetArgRCIdentityRoot(Retain) != Arg) {
|
2014-04-25 13:29:35 +08:00
|
|
|
return nullptr;
|
2013-04-04 07:16:05 +08:00
|
|
|
}
|
2013-04-06 07:46:45 +08:00
|
|
|
|
2013-04-04 07:16:05 +08:00
|
|
|
return Retain;
|
|
|
|
}
|
|
|
|
|
2013-04-04 07:39:14 +08:00
|
|
|
/// Look for an ``autorelease'' instruction dependent on Arg such that there are
|
|
|
|
/// no instructions dependent on Arg that need a positive ref count in between
|
|
|
|
/// the autorelease and the ret.
|
|
|
|
static CallInst *
|
|
|
|
FindPredecessorAutoreleaseWithSafePath(const Value *Arg, BasicBlock *BB,
|
|
|
|
ReturnInst *Ret,
|
2014-08-21 13:55:13 +08:00
|
|
|
SmallPtrSetImpl<Instruction *> &DepInsts,
|
|
|
|
SmallPtrSetImpl<const BasicBlock *> &V,
|
2013-04-04 07:39:14 +08:00
|
|
|
ProvenanceAnalysis &PA) {
|
|
|
|
FindDependencies(NeedsPositiveRetainCount, Arg,
|
|
|
|
BB, Ret, DepInsts, V, PA);
|
|
|
|
if (DepInsts.size() != 1)
|
2014-04-25 13:29:35 +08:00
|
|
|
return nullptr;
|
2013-04-06 07:46:45 +08:00
|
|
|
|
2015-03-06 07:29:06 +08:00
|
|
|
auto *Autorelease = dyn_cast_or_null<CallInst>(*DepInsts.begin());
|
2013-04-04 07:39:14 +08:00
|
|
|
if (!Autorelease)
|
2014-04-25 13:29:35 +08:00
|
|
|
return nullptr;
|
2015-02-20 03:51:32 +08:00
|
|
|
ARCInstKind AutoreleaseClass = GetBasicARCInstKind(Autorelease);
|
2013-04-04 07:39:14 +08:00
|
|
|
if (!IsAutorelease(AutoreleaseClass))
|
2014-04-25 13:29:35 +08:00
|
|
|
return nullptr;
|
2015-02-19 08:42:38 +08:00
|
|
|
if (GetArgRCIdentityRoot(Autorelease) != Arg)
|
2014-04-25 13:29:35 +08:00
|
|
|
return nullptr;
|
2013-04-06 07:46:45 +08:00
|
|
|
|
2013-04-04 07:39:14 +08:00
|
|
|
return Autorelease;
|
|
|
|
}
|
|
|
|
|
2013-01-14 08:35:14 +08:00
|
|
|
/// Look for this pattern:
|
2012-09-14 22:57:36 +08:00
|
|
|
/// \code
|
2011-06-16 07:37:01 +08:00
|
|
|
/// %call = call i8* @something(...)
|
|
|
|
/// %2 = call i8* @objc_retain(i8* %call)
|
|
|
|
/// %3 = call i8* @objc_autorelease(i8* %2)
|
|
|
|
/// ret i8* %3
|
2012-09-14 22:57:36 +08:00
|
|
|
/// \endcode
|
2011-06-16 07:37:01 +08:00
|
|
|
/// And delete the retain and autorelease.
|
|
|
|
void ObjCARCOpt::OptimizeReturns(Function &F) {
|
|
|
|
if (!F.getReturnType()->isPointerTy())
|
|
|
|
return;
|
2013-04-06 07:46:45 +08:00
|
|
|
|
2013-04-06 02:10:41 +08:00
|
|
|
DEBUG(dbgs() << "\n== ObjCARCOpt::OptimizeReturns ==\n");
|
2013-04-06 07:46:45 +08:00
|
|
|
|
2011-06-16 07:37:01 +08:00
|
|
|
SmallPtrSet<Instruction *, 4> DependingInstructions;
|
|
|
|
SmallPtrSet<const BasicBlock *, 4> Visited;
|
2015-10-20 07:20:14 +08:00
|
|
|
for (BasicBlock &BB: F) {
|
|
|
|
ReturnInst *Ret = dyn_cast<ReturnInst>(&BB.back());
|
2013-01-02 00:05:48 +08:00
|
|
|
|
2013-04-06 02:10:41 +08:00
|
|
|
DEBUG(dbgs() << "Visiting: " << *Ret << "\n");
|
2013-01-02 00:05:48 +08:00
|
|
|
|
2013-04-04 07:39:14 +08:00
|
|
|
if (!Ret)
|
|
|
|
continue;
|
2013-04-06 07:46:45 +08:00
|
|
|
|
2015-02-19 08:42:38 +08:00
|
|
|
const Value *Arg = GetRCIdentityRoot(Ret->getOperand(0));
|
2013-04-06 07:46:45 +08:00
|
|
|
|
2013-04-21 08:25:04 +08:00
|
|
|
// Look for an ``autorelease'' instruction that is a predecessor of Ret and
|
2013-04-04 07:39:14 +08:00
|
|
|
// dependent on Arg such that there are no instructions dependent on Arg
|
|
|
|
// that need a positive ref count in between the autorelease and Ret.
|
2015-10-20 07:20:14 +08:00
|
|
|
CallInst *Autorelease = FindPredecessorAutoreleaseWithSafePath(
|
|
|
|
Arg, &BB, Ret, DependingInstructions, Visited, PA);
|
2013-04-21 08:25:01 +08:00
|
|
|
DependingInstructions.clear();
|
|
|
|
Visited.clear();
|
|
|
|
|
|
|
|
if (!Autorelease)
|
|
|
|
continue;
|
2013-04-06 07:46:45 +08:00
|
|
|
|
2015-10-20 07:20:14 +08:00
|
|
|
CallInst *Retain = FindPredecessorRetainWithSafePath(
|
|
|
|
Arg, &BB, Autorelease, DependingInstructions, Visited, PA);
|
2011-06-16 07:37:01 +08:00
|
|
|
DependingInstructions.clear();
|
|
|
|
Visited.clear();
|
2013-04-21 08:25:01 +08:00
|
|
|
|
|
|
|
if (!Retain)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Check that there is nothing that can affect the reference count
|
|
|
|
// between the retain and the call. Note that Retain need not be in BB.
|
|
|
|
bool HasSafePathToCall = HasSafePathToPredecessorCall(Arg, Retain,
|
|
|
|
DependingInstructions,
|
|
|
|
Visited, PA);
|
|
|
|
DependingInstructions.clear();
|
|
|
|
Visited.clear();
|
|
|
|
|
|
|
|
if (!HasSafePathToCall)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// If so, we can zap the retain and autorelease.
|
|
|
|
Changed = true;
|
|
|
|
++NumRets;
|
|
|
|
DEBUG(dbgs() << "Erasing: " << *Retain << "\nErasing: "
|
|
|
|
<< *Autorelease << "\n");
|
|
|
|
EraseInstruction(Retain);
|
|
|
|
EraseInstruction(Autorelease);
|
2011-06-16 07:37:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-29 14:16:57 +08:00
|
|
|
#ifndef NDEBUG
|
|
|
|
void
|
|
|
|
ObjCARCOpt::GatherStatistics(Function &F, bool AfterOptimization) {
|
|
|
|
llvm::Statistic &NumRetains =
|
|
|
|
AfterOptimization? NumRetainsAfterOpt : NumRetainsBeforeOpt;
|
|
|
|
llvm::Statistic &NumReleases =
|
|
|
|
AfterOptimization? NumReleasesAfterOpt : NumReleasesBeforeOpt;
|
|
|
|
|
|
|
|
for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ) {
|
|
|
|
Instruction *Inst = &*I++;
|
2015-02-20 03:51:32 +08:00
|
|
|
switch (GetBasicARCInstKind(Inst)) {
|
2013-04-29 14:16:57 +08:00
|
|
|
default:
|
|
|
|
break;
|
2015-02-20 03:51:32 +08:00
|
|
|
case ARCInstKind::Retain:
|
2013-04-29 14:16:57 +08:00
|
|
|
++NumRetains;
|
|
|
|
break;
|
2015-02-20 03:51:32 +08:00
|
|
|
case ARCInstKind::Release:
|
2013-04-29 14:16:57 +08:00
|
|
|
++NumReleases;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-06-16 07:37:01 +08:00
|
|
|
bool ObjCARCOpt::doInitialization(Module &M) {
|
|
|
|
if (!EnableARCOpts)
|
|
|
|
return false;
|
|
|
|
|
2012-04-14 02:57:48 +08:00
|
|
|
// If nothing in the Module uses ARC, don't do anything.
|
2011-06-21 07:20:43 +08:00
|
|
|
Run = ModuleHasARC(M);
|
|
|
|
if (!Run)
|
|
|
|
return false;
|
|
|
|
|
2011-06-16 07:37:01 +08:00
|
|
|
// Intuitively, objc_retain and others are nocapture, however in practice
|
|
|
|
// they are not, because they return their argument value. And objc_release
|
2012-04-28 02:56:31 +08:00
|
|
|
// calls finalizers which can have arbitrary side effects.
|
2015-03-16 15:02:27 +08:00
|
|
|
MDKindCache.init(&M);
|
2011-06-16 07:37:01 +08:00
|
|
|
|
2013-07-06 09:39:23 +08:00
|
|
|
// Initialize our runtime entry point cache.
|
2015-03-16 15:02:27 +08:00
|
|
|
EP.init(&M);
|
2011-06-16 07:37:01 +08:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ObjCARCOpt::runOnFunction(Function &F) {
|
|
|
|
if (!EnableARCOpts)
|
|
|
|
return false;
|
|
|
|
|
2011-06-21 07:20:43 +08:00
|
|
|
// If nothing in the Module uses ARC, don't do anything.
|
|
|
|
if (!Run)
|
|
|
|
return false;
|
|
|
|
|
2011-06-16 07:37:01 +08:00
|
|
|
Changed = false;
|
|
|
|
|
2013-04-06 02:10:41 +08:00
|
|
|
DEBUG(dbgs() << "<<< ObjCARCOpt: Visiting Function: " << F.getName() << " >>>"
|
|
|
|
"\n");
|
2013-01-12 10:57:16 +08:00
|
|
|
|
[PM/AA] Rebuild LLVM's alias analysis infrastructure in a way compatible
with the new pass manager, and no longer relying on analysis groups.
This builds essentially a ground-up new AA infrastructure stack for
LLVM. The core ideas are the same that are used throughout the new pass
manager: type erased polymorphism and direct composition. The design is
as follows:
- FunctionAAResults is a type-erasing alias analysis results aggregation
interface to walk a single query across a range of results from
different alias analyses. Currently this is function-specific as we
always assume that aliasing queries are *within* a function.
- AAResultBase is a CRTP utility providing stub implementations of
various parts of the alias analysis result concept, notably in several
cases in terms of other more general parts of the interface. This can
be used to implement only a narrow part of the interface rather than
the entire interface. This isn't really ideal, this logic should be
hoisted into FunctionAAResults as currently it will cause
a significant amount of redundant work, but it faithfully models the
behavior of the prior infrastructure.
- All the alias analysis passes are ported to be wrapper passes for the
legacy PM and new-style analysis passes for the new PM with a shared
result object. In some cases (most notably CFL), this is an extremely
naive approach that we should revisit when we can specialize for the
new pass manager.
- BasicAA has been restructured to reflect that it is much more
fundamentally a function analysis because it uses dominator trees and
loop info that need to be constructed for each function.
All of the references to getting alias analysis results have been
updated to use the new aggregation interface. All the preservation and
other pass management code has been updated accordingly.
The way the FunctionAAResultsWrapperPass works is to detect the
available alias analyses when run, and add them to the results object.
This means that we should be able to continue to respect when various
passes are added to the pipeline, for example adding CFL or adding TBAA
passes should just cause their results to be available and to get folded
into this. The exception to this rule is BasicAA which really needs to
be a function pass due to using dominator trees and loop info. As
a consequence, the FunctionAAResultsWrapperPass directly depends on
BasicAA and always includes it in the aggregation.
This has significant implications for preserving analyses. Generally,
most passes shouldn't bother preserving FunctionAAResultsWrapperPass
because rebuilding the results just updates the set of known AA passes.
The exception to this rule are LoopPass instances which need to preserve
all the function analyses that the loop pass manager will end up
needing. This means preserving both BasicAAWrapperPass and the
aggregating FunctionAAResultsWrapperPass.
Now, when preserving an alias analysis, you do so by directly preserving
that analysis. This is only necessary for non-immutable-pass-provided
alias analyses though, and there are only three of interest: BasicAA,
GlobalsAA (formerly GlobalsModRef), and SCEVAA. Usually BasicAA is
preserved when needed because it (like DominatorTree and LoopInfo) is
marked as a CFG-only pass. I've expanded GlobalsAA into the preserved
set everywhere we previously were preserving all of AliasAnalysis, and
I've added SCEVAA in the intersection of that with where we preserve
SCEV itself.
One significant challenge to all of this is that the CGSCC passes were
actually using the alias analysis implementations by taking advantage of
a pretty amazing set of loop holes in the old pass manager's analysis
management code which allowed analysis groups to slide through in many
cases. Moving away from analysis groups makes this problem much more
obvious. To fix it, I've leveraged the flexibility the design of the new
PM components provides to just directly construct the relevant alias
analyses for the relevant functions in the IPO passes that need them.
This is a bit hacky, but should go away with the new pass manager, and
is already in many ways cleaner than the prior state.
Another significant challenge is that various facilities of the old
alias analysis infrastructure just don't fit any more. The most
significant of these is the alias analysis 'counter' pass. That pass
relied on the ability to snoop on AA queries at different points in the
analysis group chain. Instead, I'm planning to build printing
functionality directly into the aggregation layer. I've not included
that in this patch merely to keep it smaller.
Note that all of this needs a nearly complete rewrite of the AA
documentation. I'm planning to do that, but I'd like to make sure the
new design settles, and to flesh out a bit more of what it looks like in
the new pass manager first.
Differential Revision: http://reviews.llvm.org/D12080
llvm-svn: 247167
2015-09-10 01:55:00 +08:00
|
|
|
PA.setAA(&getAnalysis<AAResultsWrapperPass>().getAAResults());
|
2011-06-16 07:37:01 +08:00
|
|
|
|
2013-05-14 02:29:07 +08:00
|
|
|
#ifndef NDEBUG
|
|
|
|
if (AreStatisticsEnabled()) {
|
|
|
|
GatherStatistics(F, false);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-06-16 07:37:01 +08:00
|
|
|
// This pass performs several distinct transformations. As a compile-time aid
|
|
|
|
// when compiling code that isn't ObjC, skip these if the relevant ObjC
|
|
|
|
// library functions aren't declared.
|
|
|
|
|
2013-04-25 06:18:15 +08:00
|
|
|
// Preliminary optimizations. This also computes UsedInThisFunction.
|
2011-06-16 07:37:01 +08:00
|
|
|
OptimizeIndividualCalls(F);
|
|
|
|
|
|
|
|
// Optimizations for weak pointers.
|
2015-02-20 03:51:32 +08:00
|
|
|
if (UsedInThisFunction & ((1 << unsigned(ARCInstKind::LoadWeak)) |
|
|
|
|
(1 << unsigned(ARCInstKind::LoadWeakRetained)) |
|
|
|
|
(1 << unsigned(ARCInstKind::StoreWeak)) |
|
|
|
|
(1 << unsigned(ARCInstKind::InitWeak)) |
|
|
|
|
(1 << unsigned(ARCInstKind::CopyWeak)) |
|
|
|
|
(1 << unsigned(ARCInstKind::MoveWeak)) |
|
|
|
|
(1 << unsigned(ARCInstKind::DestroyWeak))))
|
2011-06-16 07:37:01 +08:00
|
|
|
OptimizeWeakCalls(F);
|
|
|
|
|
|
|
|
// Optimizations for retain+release pairs.
|
2015-02-20 03:51:32 +08:00
|
|
|
if (UsedInThisFunction & ((1 << unsigned(ARCInstKind::Retain)) |
|
|
|
|
(1 << unsigned(ARCInstKind::RetainRV)) |
|
|
|
|
(1 << unsigned(ARCInstKind::RetainBlock))))
|
|
|
|
if (UsedInThisFunction & (1 << unsigned(ARCInstKind::Release)))
|
2011-06-16 07:37:01 +08:00
|
|
|
// Run OptimizeSequences until it either stops making changes or
|
|
|
|
// no retain+release pair nesting is detected.
|
|
|
|
while (OptimizeSequences(F)) {}
|
|
|
|
|
|
|
|
// Optimizations if objc_autorelease is used.
|
2015-02-20 03:51:32 +08:00
|
|
|
if (UsedInThisFunction & ((1 << unsigned(ARCInstKind::Autorelease)) |
|
|
|
|
(1 << unsigned(ARCInstKind::AutoreleaseRV))))
|
2011-06-16 07:37:01 +08:00
|
|
|
OptimizeReturns(F);
|
|
|
|
|
2013-04-29 14:16:57 +08:00
|
|
|
// Gather statistics after optimization.
|
|
|
|
#ifndef NDEBUG
|
|
|
|
if (AreStatisticsEnabled()) {
|
|
|
|
GatherStatistics(F, true);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-01-12 10:57:16 +08:00
|
|
|
DEBUG(dbgs() << "\n");
|
|
|
|
|
2011-06-16 07:37:01 +08:00
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ObjCARCOpt::releaseMemory() {
|
|
|
|
PA.clear();
|
|
|
|
}
|
|
|
|
|
2013-01-14 08:35:14 +08:00
|
|
|
/// @}
|
|
|
|
///
|