2013-01-29 11:03:03 +08:00
|
|
|
//===- ProvenanceAnalysis.cpp - ObjC ARC Optimization ---------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2017-10-27 09:09:08 +08:00
|
|
|
//
|
2013-01-29 11:03:03 +08:00
|
|
|
/// \file
|
|
|
|
///
|
|
|
|
/// This file defines a special form of Alias Analysis called ``Provenance
|
|
|
|
/// Analysis''. The word ``provenance'' refers to the history of the ownership
|
|
|
|
/// of an object. Thus ``Provenance Analysis'' is an analysis which attempts to
|
|
|
|
/// use various techniques to determine if locally
|
|
|
|
///
|
|
|
|
/// 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.
|
2017-10-27 09:09:08 +08:00
|
|
|
//
|
2013-01-29 11:03:03 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "ProvenanceAnalysis.h"
|
2013-01-29 12:20:52 +08:00
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
2017-10-27 09:09:08 +08:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
|
|
|
#include "llvm/Analysis/AliasAnalysis.h"
|
|
|
|
#include "llvm/Analysis/ObjCARCAnalysisUtils.h"
|
|
|
|
#include "llvm/IR/Instructions.h"
|
|
|
|
#include "llvm/IR/Module.h"
|
|
|
|
#include "llvm/IR/Use.h"
|
|
|
|
#include "llvm/IR/User.h"
|
|
|
|
#include "llvm/IR/Value.h"
|
|
|
|
#include "llvm/Support/Casting.h"
|
|
|
|
#include <utility>
|
2013-01-29 11:03:03 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace llvm::objcarc;
|
|
|
|
|
|
|
|
bool ProvenanceAnalysis::relatedSelect(const SelectInst *A,
|
|
|
|
const Value *B) {
|
2015-03-10 10:37:25 +08:00
|
|
|
const DataLayout &DL = A->getModule()->getDataLayout();
|
2013-01-29 11:03:03 +08:00
|
|
|
// If the values are Selects with the same condition, we can do a more precise
|
|
|
|
// check: just check for relations between the values on corresponding arms.
|
|
|
|
if (const SelectInst *SB = dyn_cast<SelectInst>(B))
|
|
|
|
if (A->getCondition() == SB->getCondition())
|
2015-03-10 10:37:25 +08:00
|
|
|
return related(A->getTrueValue(), SB->getTrueValue(), DL) ||
|
|
|
|
related(A->getFalseValue(), SB->getFalseValue(), DL);
|
2013-01-29 11:03:03 +08:00
|
|
|
|
|
|
|
// Check both arms of the Select node individually.
|
2015-03-10 10:37:25 +08:00
|
|
|
return related(A->getTrueValue(), B, DL) ||
|
|
|
|
related(A->getFalseValue(), B, DL);
|
2013-01-29 11:03:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ProvenanceAnalysis::relatedPHI(const PHINode *A,
|
|
|
|
const Value *B) {
|
2015-03-10 10:37:25 +08:00
|
|
|
const DataLayout &DL = A->getModule()->getDataLayout();
|
2013-01-29 11:03:03 +08:00
|
|
|
// If the values are PHIs in the same block, we can do a more precise as well
|
|
|
|
// as efficient check: just check for relations between the values on
|
|
|
|
// corresponding edges.
|
|
|
|
if (const PHINode *PNB = dyn_cast<PHINode>(B))
|
|
|
|
if (PNB->getParent() == A->getParent()) {
|
|
|
|
for (unsigned i = 0, e = A->getNumIncomingValues(); i != e; ++i)
|
|
|
|
if (related(A->getIncomingValue(i),
|
2015-03-10 10:37:25 +08:00
|
|
|
PNB->getIncomingValueForBlock(A->getIncomingBlock(i)), DL))
|
2013-01-29 11:03:03 +08:00
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check each unique source of the PHI node against B.
|
|
|
|
SmallPtrSet<const Value *, 4> UniqueSrc;
|
2015-05-13 04:05:31 +08:00
|
|
|
for (Value *PV1 : A->incoming_values()) {
|
2015-03-10 10:37:25 +08:00
|
|
|
if (UniqueSrc.insert(PV1).second && related(PV1, B, DL))
|
2013-01-29 11:03:03 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// All of the arms checked out.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Test if the value of P, or any value covered by its provenance, is ever
|
|
|
|
/// stored within the function (not counting callees).
|
2013-02-13 07:35:08 +08:00
|
|
|
static bool IsStoredObjCPointer(const Value *P) {
|
2013-01-29 11:03:03 +08:00
|
|
|
SmallPtrSet<const Value *, 8> Visited;
|
|
|
|
SmallVector<const Value *, 8> Worklist;
|
|
|
|
Worklist.push_back(P);
|
|
|
|
Visited.insert(P);
|
|
|
|
do {
|
|
|
|
P = Worklist.pop_back_val();
|
2014-03-09 11:16:01 +08:00
|
|
|
for (const Use &U : P->uses()) {
|
|
|
|
const User *Ur = U.getUser();
|
2013-01-29 11:03:03 +08:00
|
|
|
if (isa<StoreInst>(Ur)) {
|
2014-03-09 11:16:01 +08:00
|
|
|
if (U.getOperandNo() == 0)
|
2013-01-29 11:03:03 +08:00
|
|
|
// The pointer is stored.
|
|
|
|
return true;
|
|
|
|
// The pointed is stored through.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (isa<CallInst>(Ur))
|
|
|
|
// The pointer is passed as an argument, ignore this.
|
|
|
|
continue;
|
|
|
|
if (isa<PtrToIntInst>(P))
|
|
|
|
// Assume the worst.
|
|
|
|
return true;
|
2014-11-19 15:49:26 +08:00
|
|
|
if (Visited.insert(Ur).second)
|
2013-01-29 11:03:03 +08:00
|
|
|
Worklist.push_back(Ur);
|
|
|
|
}
|
|
|
|
} while (!Worklist.empty());
|
|
|
|
|
|
|
|
// Everything checked out.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-03-10 10:37:25 +08:00
|
|
|
bool ProvenanceAnalysis::relatedCheck(const Value *A, const Value *B,
|
|
|
|
const DataLayout &DL) {
|
2013-01-29 11:03:03 +08:00
|
|
|
// Ask regular AliasAnalysis, for a first approximation.
|
|
|
|
switch (AA->alias(A, B)) {
|
2015-06-22 10:16:51 +08:00
|
|
|
case NoAlias:
|
2013-01-29 11:03:03 +08:00
|
|
|
return false;
|
2015-06-22 10:16:51 +08:00
|
|
|
case MustAlias:
|
|
|
|
case PartialAlias:
|
2013-01-29 11:03:03 +08:00
|
|
|
return true;
|
2015-06-22 10:16:51 +08:00
|
|
|
case MayAlias:
|
2013-01-29 11:03:03 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AIsIdentified = IsObjCIdentifiedObject(A);
|
|
|
|
bool BIsIdentified = IsObjCIdentifiedObject(B);
|
|
|
|
|
|
|
|
// An ObjC-Identified object can't alias a load if it is never locally stored.
|
|
|
|
if (AIsIdentified) {
|
|
|
|
// Check for an obvious escape.
|
|
|
|
if (isa<LoadInst>(B))
|
2013-02-13 07:35:08 +08:00
|
|
|
return IsStoredObjCPointer(A);
|
2013-01-29 11:03:03 +08:00
|
|
|
if (BIsIdentified) {
|
|
|
|
// Check for an obvious escape.
|
|
|
|
if (isa<LoadInst>(A))
|
2013-02-13 07:35:08 +08:00
|
|
|
return IsStoredObjCPointer(B);
|
2013-01-29 11:03:03 +08:00
|
|
|
// Both pointers are identified and escapes aren't an evident problem.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else if (BIsIdentified) {
|
|
|
|
// Check for an obvious escape.
|
|
|
|
if (isa<LoadInst>(A))
|
2013-02-13 07:35:08 +08:00
|
|
|
return IsStoredObjCPointer(B);
|
2013-01-29 11:03:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Special handling for PHI and Select.
|
|
|
|
if (const PHINode *PN = dyn_cast<PHINode>(A))
|
|
|
|
return relatedPHI(PN, B);
|
|
|
|
if (const PHINode *PN = dyn_cast<PHINode>(B))
|
|
|
|
return relatedPHI(PN, A);
|
|
|
|
if (const SelectInst *S = dyn_cast<SelectInst>(A))
|
|
|
|
return relatedSelect(S, B);
|
|
|
|
if (const SelectInst *S = dyn_cast<SelectInst>(B))
|
|
|
|
return relatedSelect(S, A);
|
|
|
|
|
|
|
|
// Conservative.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-03-10 10:37:25 +08:00
|
|
|
bool ProvenanceAnalysis::related(const Value *A, const Value *B,
|
|
|
|
const DataLayout &DL) {
|
2018-03-13 04:36:25 +08:00
|
|
|
A = GetUnderlyingObjCPtrCached(A, DL, UnderlyingObjCPtrCache);
|
|
|
|
B = GetUnderlyingObjCPtrCached(B, DL, UnderlyingObjCPtrCache);
|
|
|
|
|
|
|
|
// Quick check.
|
|
|
|
if (A == B)
|
|
|
|
return true;
|
|
|
|
|
2013-01-29 11:03:03 +08:00
|
|
|
// Begin by inserting a conservative value into the map. If the insertion
|
|
|
|
// fails, we have the answer already. If it succeeds, leave it there until we
|
|
|
|
// compute the real answer to guard against recursive queries.
|
|
|
|
if (A > B) std::swap(A, B);
|
|
|
|
std::pair<CachedResultsTy::iterator, bool> Pair =
|
|
|
|
CachedResults.insert(std::make_pair(ValuePairTy(A, B), true));
|
|
|
|
if (!Pair.second)
|
|
|
|
return Pair.first->second;
|
|
|
|
|
2015-03-10 10:37:25 +08:00
|
|
|
bool Result = relatedCheck(A, B, DL);
|
2013-01-29 11:03:03 +08:00
|
|
|
CachedResults[ValuePairTy(A, B)] = Result;
|
|
|
|
return Result;
|
|
|
|
}
|