LLVM API Documentation
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 // If the values are Selects with the same condition, we can do a more precise 00036 // check: just check for relations between the values on corresponding arms. 00037 if (const SelectInst *SB = dyn_cast<SelectInst>(B)) 00038 if (A->getCondition() == SB->getCondition()) 00039 return related(A->getTrueValue(), SB->getTrueValue()) || 00040 related(A->getFalseValue(), SB->getFalseValue()); 00041 00042 // Check both arms of the Select node individually. 00043 return related(A->getTrueValue(), B) || 00044 related(A->getFalseValue(), B); 00045 } 00046 00047 bool ProvenanceAnalysis::relatedPHI(const PHINode *A, 00048 const Value *B) { 00049 // If the values are PHIs in the same block, we can do a more precise as well 00050 // as efficient check: just check for relations between the values on 00051 // corresponding edges. 00052 if (const PHINode *PNB = dyn_cast<PHINode>(B)) 00053 if (PNB->getParent() == A->getParent()) { 00054 for (unsigned i = 0, e = A->getNumIncomingValues(); i != e; ++i) 00055 if (related(A->getIncomingValue(i), 00056 PNB->getIncomingValueForBlock(A->getIncomingBlock(i)))) 00057 return true; 00058 return false; 00059 } 00060 00061 // Check each unique source of the PHI node against B. 00062 SmallPtrSet<const Value *, 4> UniqueSrc; 00063 for (unsigned i = 0, e = A->getNumIncomingValues(); i != e; ++i) { 00064 const Value *PV1 = A->getIncomingValue(i); 00065 if (UniqueSrc.insert(PV1) && related(PV1, B)) 00066 return true; 00067 } 00068 00069 // All of the arms checked out. 00070 return false; 00071 } 00072 00073 /// Test if the value of P, or any value covered by its provenance, is ever 00074 /// stored within the function (not counting callees). 00075 static bool IsStoredObjCPointer(const Value *P) { 00076 SmallPtrSet<const Value *, 8> Visited; 00077 SmallVector<const Value *, 8> Worklist; 00078 Worklist.push_back(P); 00079 Visited.insert(P); 00080 do { 00081 P = Worklist.pop_back_val(); 00082 for (const Use &U : P->uses()) { 00083 const User *Ur = U.getUser(); 00084 if (isa<StoreInst>(Ur)) { 00085 if (U.getOperandNo() == 0) 00086 // The pointer is stored. 00087 return true; 00088 // The pointed is stored through. 00089 continue; 00090 } 00091 if (isa<CallInst>(Ur)) 00092 // The pointer is passed as an argument, ignore this. 00093 continue; 00094 if (isa<PtrToIntInst>(P)) 00095 // Assume the worst. 00096 return true; 00097 if (Visited.insert(Ur)) 00098 Worklist.push_back(Ur); 00099 } 00100 } while (!Worklist.empty()); 00101 00102 // Everything checked out. 00103 return false; 00104 } 00105 00106 bool ProvenanceAnalysis::relatedCheck(const Value *A, 00107 const Value *B) { 00108 // Skip past provenance pass-throughs. 00109 A = GetUnderlyingObjCPtr(A); 00110 B = GetUnderlyingObjCPtr(B); 00111 00112 // Quick check. 00113 if (A == B) 00114 return true; 00115 00116 // Ask regular AliasAnalysis, for a first approximation. 00117 switch (AA->alias(A, B)) { 00118 case AliasAnalysis::NoAlias: 00119 return false; 00120 case AliasAnalysis::MustAlias: 00121 case AliasAnalysis::PartialAlias: 00122 return true; 00123 case AliasAnalysis::MayAlias: 00124 break; 00125 } 00126 00127 bool AIsIdentified = IsObjCIdentifiedObject(A); 00128 bool BIsIdentified = IsObjCIdentifiedObject(B); 00129 00130 // An ObjC-Identified object can't alias a load if it is never locally stored. 00131 if (AIsIdentified) { 00132 // Check for an obvious escape. 00133 if (isa<LoadInst>(B)) 00134 return IsStoredObjCPointer(A); 00135 if (BIsIdentified) { 00136 // Check for an obvious escape. 00137 if (isa<LoadInst>(A)) 00138 return IsStoredObjCPointer(B); 00139 // Both pointers are identified and escapes aren't an evident problem. 00140 return false; 00141 } 00142 } else if (BIsIdentified) { 00143 // Check for an obvious escape. 00144 if (isa<LoadInst>(A)) 00145 return IsStoredObjCPointer(B); 00146 } 00147 00148 // Special handling for PHI and Select. 00149 if (const PHINode *PN = dyn_cast<PHINode>(A)) 00150 return relatedPHI(PN, B); 00151 if (const PHINode *PN = dyn_cast<PHINode>(B)) 00152 return relatedPHI(PN, A); 00153 if (const SelectInst *S = dyn_cast<SelectInst>(A)) 00154 return relatedSelect(S, B); 00155 if (const SelectInst *S = dyn_cast<SelectInst>(B)) 00156 return relatedSelect(S, A); 00157 00158 // Conservative. 00159 return true; 00160 } 00161 00162 bool ProvenanceAnalysis::related(const Value *A, 00163 const Value *B) { 00164 // Begin by inserting a conservative value into the map. If the insertion 00165 // fails, we have the answer already. If it succeeds, leave it there until we 00166 // compute the real answer to guard against recursive queries. 00167 if (A > B) std::swap(A, B); 00168 std::pair<CachedResultsTy::iterator, bool> Pair = 00169 CachedResults.insert(std::make_pair(ValuePairTy(A, B), true)); 00170 if (!Pair.second) 00171 return Pair.first->second; 00172 00173 bool Result = relatedCheck(A, B); 00174 CachedResults[ValuePairTy(A, B)] = Result; 00175 return Result; 00176 }