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 (Value::const_use_iterator UI = P->use_begin(), UE = P->use_end(); 00083 UI != UE; ++UI) { 00084 const User *Ur = *UI; 00085 if (isa<StoreInst>(Ur)) { 00086 if (UI.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)) 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, 00108 const Value *B) { 00109 // Skip past provenance pass-throughs. 00110 A = GetUnderlyingObjCPtr(A); 00111 B = GetUnderlyingObjCPtr(B); 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, 00164 const Value *B) { 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); 00175 CachedResults[ValuePairTy(A, B)] = Result; 00176 return Result; 00177 }