1 //===- ProvenanceAnalysis.cpp - ObjC ARC Optimization ---------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 /// \file 10 /// 11 /// This file defines a special form of Alias Analysis called ``Provenance 12 /// Analysis''. The word ``provenance'' refers to the history of the ownership 13 /// of an object. Thus ``Provenance Analysis'' is an analysis which attempts to 14 /// use various techniques to determine if locally 15 /// 16 /// WARNING: This file knows about certain library functions. It recognizes them 17 /// by name, and hardwires knowledge of their semantics. 18 /// 19 /// WARNING: This file knows about how certain Objective-C library functions are 20 /// used. Naive LLVM IR transformations which would otherwise be 21 /// behavior-preserving may break these assumptions. 22 // 23 //===----------------------------------------------------------------------===// 24 25 #include "ProvenanceAnalysis.h" 26 #include "llvm/ADT/SmallPtrSet.h" 27 #include "llvm/ADT/SmallVector.h" 28 #include "llvm/Analysis/AliasAnalysis.h" 29 #include "llvm/Analysis/ObjCARCAnalysisUtils.h" 30 #include "llvm/IR/Instructions.h" 31 #include "llvm/IR/Use.h" 32 #include "llvm/IR/User.h" 33 #include "llvm/IR/Value.h" 34 #include "llvm/Support/Casting.h" 35 #include <utility> 36 37 using namespace llvm; 38 using namespace llvm::objcarc; 39 40 bool ProvenanceAnalysis::relatedSelect(const SelectInst *A, 41 const Value *B) { 42 // If the values are Selects with the same condition, we can do a more precise 43 // check: just check for relations between the values on corresponding arms. 44 if (const SelectInst *SB = dyn_cast<SelectInst>(B)) 45 if (A->getCondition() == SB->getCondition()) 46 return related(A->getTrueValue(), SB->getTrueValue()) || 47 related(A->getFalseValue(), SB->getFalseValue()); 48 49 // Check both arms of the Select node individually. 50 return related(A->getTrueValue(), B) || related(A->getFalseValue(), B); 51 } 52 53 bool ProvenanceAnalysis::relatedPHI(const PHINode *A, 54 const Value *B) { 55 // If the values are PHIs in the same block, we can do a more precise as well 56 // as efficient check: just check for relations between the values on 57 // corresponding edges. 58 if (const PHINode *PNB = dyn_cast<PHINode>(B)) 59 if (PNB->getParent() == A->getParent()) { 60 for (unsigned i = 0, e = A->getNumIncomingValues(); i != e; ++i) 61 if (related(A->getIncomingValue(i), 62 PNB->getIncomingValueForBlock(A->getIncomingBlock(i)))) 63 return true; 64 return false; 65 } 66 67 // Check each unique source of the PHI node against B. 68 SmallPtrSet<const Value *, 4> UniqueSrc; 69 for (Value *PV1 : A->incoming_values()) { 70 if (UniqueSrc.insert(PV1).second && related(PV1, B)) 71 return true; 72 } 73 74 // All of the arms checked out. 75 return false; 76 } 77 78 /// Test if the value of P, or any value covered by its provenance, is ever 79 /// stored within the function (not counting callees). 80 static bool IsStoredObjCPointer(const Value *P) { 81 SmallPtrSet<const Value *, 8> Visited; 82 SmallVector<const Value *, 8> Worklist; 83 Worklist.push_back(P); 84 Visited.insert(P); 85 do { 86 P = Worklist.pop_back_val(); 87 for (const Use &U : P->uses()) { 88 const User *Ur = U.getUser(); 89 if (isa<StoreInst>(Ur)) { 90 if (U.getOperandNo() == 0) 91 // The pointer is stored. 92 return true; 93 // The pointed is stored through. 94 continue; 95 } 96 if (isa<CallInst>(Ur)) 97 // The pointer is passed as an argument, ignore this. 98 continue; 99 if (isa<PtrToIntInst>(P)) 100 // Assume the worst. 101 return true; 102 if (Visited.insert(Ur).second) 103 Worklist.push_back(Ur); 104 } 105 } while (!Worklist.empty()); 106 107 // Everything checked out. 108 return false; 109 } 110 111 bool ProvenanceAnalysis::relatedCheck(const Value *A, const Value *B) { 112 // Ask regular AliasAnalysis, for a first approximation. 113 switch (AA->alias(A, B)) { 114 case AliasResult::NoAlias: 115 return false; 116 case AliasResult::MustAlias: 117 case AliasResult::PartialAlias: 118 return true; 119 case AliasResult::MayAlias: 120 break; 121 } 122 123 bool AIsIdentified = IsObjCIdentifiedObject(A); 124 bool BIsIdentified = IsObjCIdentifiedObject(B); 125 126 // An ObjC-Identified object can't alias a load if it is never locally stored. 127 if (AIsIdentified) { 128 // Check for an obvious escape. 129 if (isa<LoadInst>(B)) 130 return IsStoredObjCPointer(A); 131 if (BIsIdentified) { 132 // Check for an obvious escape. 133 if (isa<LoadInst>(A)) 134 return IsStoredObjCPointer(B); 135 // Both pointers are identified and escapes aren't an evident problem. 136 return false; 137 } 138 } else if (BIsIdentified) { 139 // Check for an obvious escape. 140 if (isa<LoadInst>(A)) 141 return IsStoredObjCPointer(B); 142 } 143 144 // Special handling for PHI and Select. 145 if (const PHINode *PN = dyn_cast<PHINode>(A)) 146 return relatedPHI(PN, B); 147 if (const PHINode *PN = dyn_cast<PHINode>(B)) 148 return relatedPHI(PN, A); 149 if (const SelectInst *S = dyn_cast<SelectInst>(A)) 150 return relatedSelect(S, B); 151 if (const SelectInst *S = dyn_cast<SelectInst>(B)) 152 return relatedSelect(S, A); 153 154 // Conservative. 155 return true; 156 } 157 158 bool ProvenanceAnalysis::related(const Value *A, const Value *B) { 159 A = GetUnderlyingObjCPtrCached(A, UnderlyingObjCPtrCache); 160 B = GetUnderlyingObjCPtrCached(B, UnderlyingObjCPtrCache); 161 162 // Quick check. 163 if (A == B) 164 return true; 165 166 // Begin by inserting a conservative value into the map. If the insertion 167 // fails, we have the answer already. If it succeeds, leave it there until we 168 // compute the real answer to guard against recursive queries. 169 std::pair<CachedResultsTy::iterator, bool> Pair = 170 CachedResults.insert(std::make_pair(ValuePairTy(A, B), true)); 171 if (!Pair.second) 172 return Pair.first->second; 173 174 bool Result = relatedCheck(A, B); 175 CachedResults[ValuePairTy(A, B)] = Result; 176 return Result; 177 } 178