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