LLVM  mainline
ProvenanceAnalysis.cpp
Go to the documentation of this file.
00001 //===- ProvenanceAnalysis.cpp - ObjC ARC Optimization ---------------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 /// \file
00010 ///
00011 /// This file defines a special form of Alias Analysis called ``Provenance
00012 /// Analysis''. The word ``provenance'' refers to the history of the ownership
00013 /// of an object. Thus ``Provenance Analysis'' is an analysis which attempts to
00014 /// use various techniques to determine if locally
00015 ///
00016 /// WARNING: This file knows about certain library functions. It recognizes them
00017 /// by name, and hardwires knowledge of their semantics.
00018 ///
00019 /// WARNING: This file knows about how certain Objective-C library functions are
00020 /// used. Naive LLVM IR transformations which would otherwise be
00021 /// behavior-preserving may break these assumptions.
00022 ///
00023 //===----------------------------------------------------------------------===//
00024 
00025 #include "ObjCARC.h"
00026 #include "ProvenanceAnalysis.h"
00027 #include "llvm/ADT/STLExtras.h"
00028 #include "llvm/ADT/SmallPtrSet.h"
00029 
00030 using namespace llvm;
00031 using namespace llvm::objcarc;
00032 
00033 bool ProvenanceAnalysis::relatedSelect(const SelectInst *A,
00034                                        const Value *B) {
00035   const DataLayout &DL = A->getModule()->getDataLayout();
00036   // If the values are Selects with the same condition, we can do a more precise
00037   // check: just check for relations between the values on corresponding arms.
00038   if (const SelectInst *SB = dyn_cast<SelectInst>(B))
00039     if (A->getCondition() == SB->getCondition())
00040       return related(A->getTrueValue(), SB->getTrueValue(), DL) ||
00041              related(A->getFalseValue(), SB->getFalseValue(), DL);
00042 
00043   // Check both arms of the Select node individually.
00044   return related(A->getTrueValue(), B, DL) ||
00045          related(A->getFalseValue(), B, DL);
00046 }
00047 
00048 bool ProvenanceAnalysis::relatedPHI(const PHINode *A,
00049                                     const Value *B) {
00050   const DataLayout &DL = A->getModule()->getDataLayout();
00051   // If the values are PHIs in the same block, we can do a more precise as well
00052   // as efficient check: just check for relations between the values on
00053   // corresponding edges.
00054   if (const PHINode *PNB = dyn_cast<PHINode>(B))
00055     if (PNB->getParent() == A->getParent()) {
00056       for (unsigned i = 0, e = A->getNumIncomingValues(); i != e; ++i)
00057         if (related(A->getIncomingValue(i),
00058                     PNB->getIncomingValueForBlock(A->getIncomingBlock(i)), DL))
00059           return true;
00060       return false;
00061     }
00062 
00063   // Check each unique source of the PHI node against B.
00064   SmallPtrSet<const Value *, 4> UniqueSrc;
00065   for (Value *PV1 : A->incoming_values()) {
00066     if (UniqueSrc.insert(PV1).second && related(PV1, B, DL))
00067       return true;
00068   }
00069 
00070   // All of the arms checked out.
00071   return false;
00072 }
00073 
00074 /// Test if the value of P, or any value covered by its provenance, is ever
00075 /// stored within the function (not counting callees).
00076 static bool IsStoredObjCPointer(const Value *P) {
00077   SmallPtrSet<const Value *, 8> Visited;
00078   SmallVector<const Value *, 8> Worklist;
00079   Worklist.push_back(P);
00080   Visited.insert(P);
00081   do {
00082     P = Worklist.pop_back_val();
00083     for (const Use &U : P->uses()) {
00084       const User *Ur = U.getUser();
00085       if (isa<StoreInst>(Ur)) {
00086         if (U.getOperandNo() == 0)
00087           // The pointer is stored.
00088           return true;
00089         // The pointed is stored through.
00090         continue;
00091       }
00092       if (isa<CallInst>(Ur))
00093         // The pointer is passed as an argument, ignore this.
00094         continue;
00095       if (isa<PtrToIntInst>(P))
00096         // Assume the worst.
00097         return true;
00098       if (Visited.insert(Ur).second)
00099         Worklist.push_back(Ur);
00100     }
00101   } while (!Worklist.empty());
00102 
00103   // Everything checked out.
00104   return false;
00105 }
00106 
00107 bool ProvenanceAnalysis::relatedCheck(const Value *A, const Value *B,
00108                                       const DataLayout &DL) {
00109   // Skip past provenance pass-throughs.
00110   A = GetUnderlyingObjCPtr(A, DL);
00111   B = GetUnderlyingObjCPtr(B, DL);
00112 
00113   // Quick check.
00114   if (A == B)
00115     return true;
00116 
00117   // Ask regular AliasAnalysis, for a first approximation.
00118   switch (AA->alias(A, B)) {
00119   case AliasAnalysis::NoAlias:
00120     return false;
00121   case AliasAnalysis::MustAlias:
00122   case AliasAnalysis::PartialAlias:
00123     return true;
00124   case AliasAnalysis::MayAlias:
00125     break;
00126   }
00127 
00128   bool AIsIdentified = IsObjCIdentifiedObject(A);
00129   bool BIsIdentified = IsObjCIdentifiedObject(B);
00130 
00131   // An ObjC-Identified object can't alias a load if it is never locally stored.
00132   if (AIsIdentified) {
00133     // Check for an obvious escape.
00134     if (isa<LoadInst>(B))
00135       return IsStoredObjCPointer(A);
00136     if (BIsIdentified) {
00137       // Check for an obvious escape.
00138       if (isa<LoadInst>(A))
00139         return IsStoredObjCPointer(B);
00140       // Both pointers are identified and escapes aren't an evident problem.
00141       return false;
00142     }
00143   } else if (BIsIdentified) {
00144     // Check for an obvious escape.
00145     if (isa<LoadInst>(A))
00146       return IsStoredObjCPointer(B);
00147   }
00148 
00149    // Special handling for PHI and Select.
00150   if (const PHINode *PN = dyn_cast<PHINode>(A))
00151     return relatedPHI(PN, B);
00152   if (const PHINode *PN = dyn_cast<PHINode>(B))
00153     return relatedPHI(PN, A);
00154   if (const SelectInst *S = dyn_cast<SelectInst>(A))
00155     return relatedSelect(S, B);
00156   if (const SelectInst *S = dyn_cast<SelectInst>(B))
00157     return relatedSelect(S, A);
00158 
00159   // Conservative.
00160   return true;
00161 }
00162 
00163 bool ProvenanceAnalysis::related(const Value *A, const Value *B,
00164                                  const DataLayout &DL) {
00165   // Begin by inserting a conservative value into the map. If the insertion
00166   // fails, we have the answer already. If it succeeds, leave it there until we
00167   // compute the real answer to guard against recursive queries.
00168   if (A > B) std::swap(A, B);
00169   std::pair<CachedResultsTy::iterator, bool> Pair =
00170     CachedResults.insert(std::make_pair(ValuePairTy(A, B), true));
00171   if (!Pair.second)
00172     return Pair.first->second;
00173 
00174   bool Result = relatedCheck(A, B, DL);
00175   CachedResults[ValuePairTy(A, B)] = Result;
00176   return Result;
00177 }