xref: /freebsd-src/contrib/llvm-project/llvm/lib/Transforms/ObjCARC/ProvenanceAnalysis.cpp (revision 06c3fb2749bda94cb5201f81ffdb8fa6c3161b2e)
10b57cec5SDimitry Andric //===- ProvenanceAnalysis.cpp - ObjC ARC Optimization ---------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric /// \file
100b57cec5SDimitry Andric ///
110b57cec5SDimitry Andric /// This file defines a special form of Alias Analysis called ``Provenance
120b57cec5SDimitry Andric /// Analysis''. The word ``provenance'' refers to the history of the ownership
130b57cec5SDimitry Andric /// of an object. Thus ``Provenance Analysis'' is an analysis which attempts to
140b57cec5SDimitry Andric /// use various techniques to determine if locally
150b57cec5SDimitry Andric ///
160b57cec5SDimitry Andric /// WARNING: This file knows about certain library functions. It recognizes them
170b57cec5SDimitry Andric /// by name, and hardwires knowledge of their semantics.
180b57cec5SDimitry Andric ///
190b57cec5SDimitry Andric /// WARNING: This file knows about how certain Objective-C library functions are
200b57cec5SDimitry Andric /// used. Naive LLVM IR transformations which would otherwise be
210b57cec5SDimitry Andric /// behavior-preserving may break these assumptions.
220b57cec5SDimitry Andric //
230b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
240b57cec5SDimitry Andric 
250b57cec5SDimitry Andric #include "ProvenanceAnalysis.h"
260b57cec5SDimitry Andric #include "llvm/ADT/SmallPtrSet.h"
270b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h"
280b57cec5SDimitry Andric #include "llvm/Analysis/AliasAnalysis.h"
290b57cec5SDimitry Andric #include "llvm/Analysis/ObjCARCAnalysisUtils.h"
300b57cec5SDimitry Andric #include "llvm/IR/Instructions.h"
310b57cec5SDimitry Andric #include "llvm/IR/Module.h"
320b57cec5SDimitry Andric #include "llvm/IR/Use.h"
330b57cec5SDimitry Andric #include "llvm/IR/User.h"
340b57cec5SDimitry Andric #include "llvm/IR/Value.h"
350b57cec5SDimitry Andric #include "llvm/Support/Casting.h"
360b57cec5SDimitry Andric #include <utility>
370b57cec5SDimitry Andric 
380b57cec5SDimitry Andric using namespace llvm;
390b57cec5SDimitry Andric using namespace llvm::objcarc;
400b57cec5SDimitry Andric 
relatedSelect(const SelectInst * A,const Value * B)410b57cec5SDimitry Andric bool ProvenanceAnalysis::relatedSelect(const SelectInst *A,
420b57cec5SDimitry Andric                                        const Value *B) {
430b57cec5SDimitry Andric   // If the values are Selects with the same condition, we can do a more precise
440b57cec5SDimitry Andric   // check: just check for relations between the values on corresponding arms.
45*06c3fb27SDimitry Andric   if (const SelectInst *SB = dyn_cast<SelectInst>(B))
460b57cec5SDimitry Andric     if (A->getCondition() == SB->getCondition())
47e8d8bef9SDimitry Andric       return related(A->getTrueValue(), SB->getTrueValue()) ||
48e8d8bef9SDimitry Andric              related(A->getFalseValue(), SB->getFalseValue());
490b57cec5SDimitry Andric 
500b57cec5SDimitry Andric   // Check both arms of the Select node individually.
51e8d8bef9SDimitry Andric   return related(A->getTrueValue(), B) || related(A->getFalseValue(), B);
520b57cec5SDimitry Andric }
530b57cec5SDimitry Andric 
relatedPHI(const PHINode * A,const Value * B)540b57cec5SDimitry Andric bool ProvenanceAnalysis::relatedPHI(const PHINode *A,
550b57cec5SDimitry Andric                                     const Value *B) {
56*06c3fb27SDimitry Andric   // If the values are PHIs in the same block, we can do a more precise as well
57*06c3fb27SDimitry Andric   // as efficient check: just check for relations between the values on
580b57cec5SDimitry Andric   // corresponding edges.
59*06c3fb27SDimitry Andric   if (const PHINode *PNB = dyn_cast<PHINode>(B))
600b57cec5SDimitry Andric     if (PNB->getParent() == A->getParent()) {
610b57cec5SDimitry Andric       for (unsigned i = 0, e = A->getNumIncomingValues(); i != e; ++i)
620b57cec5SDimitry Andric         if (related(A->getIncomingValue(i),
63e8d8bef9SDimitry Andric                     PNB->getIncomingValueForBlock(A->getIncomingBlock(i))))
640b57cec5SDimitry Andric           return true;
650b57cec5SDimitry Andric       return false;
660b57cec5SDimitry Andric     }
670b57cec5SDimitry Andric 
68*06c3fb27SDimitry Andric   // Check each unique source of the PHI node against B.
69*06c3fb27SDimitry Andric   SmallPtrSet<const Value *, 4> UniqueSrc;
70*06c3fb27SDimitry Andric   for (Value *PV1 : A->incoming_values()) {
71*06c3fb27SDimitry Andric     if (UniqueSrc.insert(PV1).second && related(PV1, B))
72*06c3fb27SDimitry Andric       return true;
730b57cec5SDimitry Andric   }
740b57cec5SDimitry Andric 
75*06c3fb27SDimitry Andric   // All of the arms checked out.
76*06c3fb27SDimitry Andric   return false;
770b57cec5SDimitry Andric }
780b57cec5SDimitry Andric 
790b57cec5SDimitry Andric /// Test if the value of P, or any value covered by its provenance, is ever
800b57cec5SDimitry Andric /// stored within the function (not counting callees).
IsStoredObjCPointer(const Value * P)810b57cec5SDimitry Andric static bool IsStoredObjCPointer(const Value *P) {
820b57cec5SDimitry Andric   SmallPtrSet<const Value *, 8> Visited;
830b57cec5SDimitry Andric   SmallVector<const Value *, 8> Worklist;
840b57cec5SDimitry Andric   Worklist.push_back(P);
850b57cec5SDimitry Andric   Visited.insert(P);
860b57cec5SDimitry Andric   do {
870b57cec5SDimitry Andric     P = Worklist.pop_back_val();
880b57cec5SDimitry Andric     for (const Use &U : P->uses()) {
890b57cec5SDimitry Andric       const User *Ur = U.getUser();
900b57cec5SDimitry Andric       if (isa<StoreInst>(Ur)) {
910b57cec5SDimitry Andric         if (U.getOperandNo() == 0)
920b57cec5SDimitry Andric           // The pointer is stored.
930b57cec5SDimitry Andric           return true;
940b57cec5SDimitry Andric         // The pointed is stored through.
950b57cec5SDimitry Andric         continue;
960b57cec5SDimitry Andric       }
970b57cec5SDimitry Andric       if (isa<CallInst>(Ur))
980b57cec5SDimitry Andric         // The pointer is passed as an argument, ignore this.
990b57cec5SDimitry Andric         continue;
1000b57cec5SDimitry Andric       if (isa<PtrToIntInst>(P))
1010b57cec5SDimitry Andric         // Assume the worst.
1020b57cec5SDimitry Andric         return true;
1030b57cec5SDimitry Andric       if (Visited.insert(Ur).second)
1040b57cec5SDimitry Andric         Worklist.push_back(Ur);
1050b57cec5SDimitry Andric     }
1060b57cec5SDimitry Andric   } while (!Worklist.empty());
1070b57cec5SDimitry Andric 
1080b57cec5SDimitry Andric   // Everything checked out.
1090b57cec5SDimitry Andric   return false;
1100b57cec5SDimitry Andric }
1110b57cec5SDimitry Andric 
relatedCheck(const Value * A,const Value * B)112e8d8bef9SDimitry Andric bool ProvenanceAnalysis::relatedCheck(const Value *A, const Value *B) {
1130b57cec5SDimitry Andric   // Ask regular AliasAnalysis, for a first approximation.
1140b57cec5SDimitry Andric   switch (AA->alias(A, B)) {
115fe6060f1SDimitry Andric   case AliasResult::NoAlias:
1160b57cec5SDimitry Andric     return false;
117fe6060f1SDimitry Andric   case AliasResult::MustAlias:
118fe6060f1SDimitry Andric   case AliasResult::PartialAlias:
1190b57cec5SDimitry Andric     return true;
120fe6060f1SDimitry Andric   case AliasResult::MayAlias:
1210b57cec5SDimitry Andric     break;
1220b57cec5SDimitry Andric   }
1230b57cec5SDimitry Andric 
1240b57cec5SDimitry Andric   bool AIsIdentified = IsObjCIdentifiedObject(A);
1250b57cec5SDimitry Andric   bool BIsIdentified = IsObjCIdentifiedObject(B);
1260b57cec5SDimitry Andric 
1270b57cec5SDimitry Andric   // An ObjC-Identified object can't alias a load if it is never locally stored.
128*06c3fb27SDimitry Andric   if (AIsIdentified) {
1290b57cec5SDimitry Andric     // Check for an obvious escape.
130*06c3fb27SDimitry Andric     if (isa<LoadInst>(B))
131*06c3fb27SDimitry Andric       return IsStoredObjCPointer(A);
132*06c3fb27SDimitry Andric     if (BIsIdentified) {
133*06c3fb27SDimitry Andric       // Check for an obvious escape.
134*06c3fb27SDimitry Andric       if (isa<LoadInst>(A))
135*06c3fb27SDimitry Andric         return IsStoredObjCPointer(B);
136bdd1243dSDimitry Andric       // Both pointers are identified and escapes aren't an evident problem.
137bdd1243dSDimitry Andric       return false;
138*06c3fb27SDimitry Andric     }
139*06c3fb27SDimitry Andric   } else if (BIsIdentified) {
140*06c3fb27SDimitry Andric     // Check for an obvious escape.
141*06c3fb27SDimitry Andric     if (isa<LoadInst>(A))
142*06c3fb27SDimitry Andric       return IsStoredObjCPointer(B);
143*06c3fb27SDimitry Andric   }
1440b57cec5SDimitry Andric 
1450b57cec5SDimitry Andric    // Special handling for PHI and Select.
1460b57cec5SDimitry Andric   if (const PHINode *PN = dyn_cast<PHINode>(A))
1470b57cec5SDimitry Andric     return relatedPHI(PN, B);
1480b57cec5SDimitry Andric   if (const PHINode *PN = dyn_cast<PHINode>(B))
1490b57cec5SDimitry Andric     return relatedPHI(PN, A);
1500b57cec5SDimitry Andric   if (const SelectInst *S = dyn_cast<SelectInst>(A))
1510b57cec5SDimitry Andric     return relatedSelect(S, B);
1520b57cec5SDimitry Andric   if (const SelectInst *S = dyn_cast<SelectInst>(B))
1530b57cec5SDimitry Andric     return relatedSelect(S, A);
1540b57cec5SDimitry Andric 
1550b57cec5SDimitry Andric   // Conservative.
1560b57cec5SDimitry Andric   return true;
1570b57cec5SDimitry Andric }
1580b57cec5SDimitry Andric 
related(const Value * A,const Value * B)159e8d8bef9SDimitry Andric bool ProvenanceAnalysis::related(const Value *A, const Value *B) {
160e8d8bef9SDimitry Andric   A = GetUnderlyingObjCPtrCached(A, UnderlyingObjCPtrCache);
161e8d8bef9SDimitry Andric   B = GetUnderlyingObjCPtrCached(B, UnderlyingObjCPtrCache);
1620b57cec5SDimitry Andric 
1630b57cec5SDimitry Andric   // Quick check.
1640b57cec5SDimitry Andric   if (A == B)
1650b57cec5SDimitry Andric     return true;
1660b57cec5SDimitry Andric 
1670b57cec5SDimitry Andric   // Begin by inserting a conservative value into the map. If the insertion
1680b57cec5SDimitry Andric   // fails, we have the answer already. If it succeeds, leave it there until we
1690b57cec5SDimitry Andric   // compute the real answer to guard against recursive queries.
1700b57cec5SDimitry Andric   std::pair<CachedResultsTy::iterator, bool> Pair =
1710b57cec5SDimitry Andric     CachedResults.insert(std::make_pair(ValuePairTy(A, B), true));
1720b57cec5SDimitry Andric   if (!Pair.second)
1730b57cec5SDimitry Andric     return Pair.first->second;
1740b57cec5SDimitry Andric 
175e8d8bef9SDimitry Andric   bool Result = relatedCheck(A, B);
1760b57cec5SDimitry Andric   CachedResults[ValuePairTy(A, B)] = Result;
1770b57cec5SDimitry Andric   return Result;
1780b57cec5SDimitry Andric }
179