blob: f89fc8eb62aa2bdc797f64619ea2a64ec3cf09de [file] [log] [blame]
Michael Gottesman778138e2013-01-29 03:03:03 +00001//===- ProvenanceAnalysis.cpp - ObjC ARC Optimization ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Eugene Zelenko57bd5a02017-10-27 01:09:08 +00009//
Michael Gottesman778138e2013-01-29 03:03:03 +000010/// \file
11///
12/// This file defines a special form of Alias Analysis called ``Provenance
13/// Analysis''. The word ``provenance'' refers to the history of the ownership
14/// of an object. Thus ``Provenance Analysis'' is an analysis which attempts to
15/// use various techniques to determine if locally
16///
17/// WARNING: This file knows about certain library functions. It recognizes them
18/// by name, and hardwires knowledge of their semantics.
19///
20/// WARNING: This file knows about how certain Objective-C library functions are
21/// used. Naive LLVM IR transformations which would otherwise be
22/// behavior-preserving may break these assumptions.
Eugene Zelenko57bd5a02017-10-27 01:09:08 +000023//
Michael Gottesman778138e2013-01-29 03:03:03 +000024//===----------------------------------------------------------------------===//
25
Michael Gottesman778138e2013-01-29 03:03:03 +000026#include "ProvenanceAnalysis.h"
Michael Gottesman278266f2013-01-29 04:20:52 +000027#include "llvm/ADT/SmallPtrSet.h"
Eugene Zelenko57bd5a02017-10-27 01:09:08 +000028#include "llvm/ADT/SmallVector.h"
29#include "llvm/Analysis/AliasAnalysis.h"
30#include "llvm/Analysis/ObjCARCAnalysisUtils.h"
31#include "llvm/IR/Instructions.h"
32#include "llvm/IR/Module.h"
33#include "llvm/IR/Use.h"
34#include "llvm/IR/User.h"
35#include "llvm/IR/Value.h"
36#include "llvm/Support/Casting.h"
37#include <utility>
Michael Gottesman778138e2013-01-29 03:03:03 +000038
39using namespace llvm;
40using namespace llvm::objcarc;
41
42bool ProvenanceAnalysis::relatedSelect(const SelectInst *A,
43 const Value *B) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +000044 const DataLayout &DL = A->getModule()->getDataLayout();
Michael Gottesman778138e2013-01-29 03:03:03 +000045 // If the values are Selects with the same condition, we can do a more precise
46 // check: just check for relations between the values on corresponding arms.
47 if (const SelectInst *SB = dyn_cast<SelectInst>(B))
48 if (A->getCondition() == SB->getCondition())
Mehdi Aminia28d91d2015-03-10 02:37:25 +000049 return related(A->getTrueValue(), SB->getTrueValue(), DL) ||
50 related(A->getFalseValue(), SB->getFalseValue(), DL);
Michael Gottesman778138e2013-01-29 03:03:03 +000051
52 // Check both arms of the Select node individually.
Mehdi Aminia28d91d2015-03-10 02:37:25 +000053 return related(A->getTrueValue(), B, DL) ||
54 related(A->getFalseValue(), B, DL);
Michael Gottesman778138e2013-01-29 03:03:03 +000055}
56
57bool ProvenanceAnalysis::relatedPHI(const PHINode *A,
58 const Value *B) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +000059 const DataLayout &DL = A->getModule()->getDataLayout();
Michael Gottesman778138e2013-01-29 03:03:03 +000060 // If the values are PHIs in the same block, we can do a more precise as well
61 // as efficient check: just check for relations between the values on
62 // corresponding edges.
63 if (const PHINode *PNB = dyn_cast<PHINode>(B))
64 if (PNB->getParent() == A->getParent()) {
65 for (unsigned i = 0, e = A->getNumIncomingValues(); i != e; ++i)
66 if (related(A->getIncomingValue(i),
Mehdi Aminia28d91d2015-03-10 02:37:25 +000067 PNB->getIncomingValueForBlock(A->getIncomingBlock(i)), DL))
Michael Gottesman778138e2013-01-29 03:03:03 +000068 return true;
69 return false;
70 }
71
72 // Check each unique source of the PHI node against B.
73 SmallPtrSet<const Value *, 4> UniqueSrc;
Pete Cooper833f34d2015-05-12 20:05:31 +000074 for (Value *PV1 : A->incoming_values()) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +000075 if (UniqueSrc.insert(PV1).second && related(PV1, B, DL))
Michael Gottesman778138e2013-01-29 03:03:03 +000076 return true;
77 }
78
79 // All of the arms checked out.
80 return false;
81}
82
83/// Test if the value of P, or any value covered by its provenance, is ever
84/// stored within the function (not counting callees).
Michael Gottesman27029f42013-02-12 23:35:08 +000085static bool IsStoredObjCPointer(const Value *P) {
Michael Gottesman778138e2013-01-29 03:03:03 +000086 SmallPtrSet<const Value *, 8> Visited;
87 SmallVector<const Value *, 8> Worklist;
88 Worklist.push_back(P);
89 Visited.insert(P);
90 do {
91 P = Worklist.pop_back_val();
Chandler Carruthcdf47882014-03-09 03:16:01 +000092 for (const Use &U : P->uses()) {
93 const User *Ur = U.getUser();
Michael Gottesman778138e2013-01-29 03:03:03 +000094 if (isa<StoreInst>(Ur)) {
Chandler Carruthcdf47882014-03-09 03:16:01 +000095 if (U.getOperandNo() == 0)
Michael Gottesman778138e2013-01-29 03:03:03 +000096 // The pointer is stored.
97 return true;
98 // The pointed is stored through.
99 continue;
100 }
101 if (isa<CallInst>(Ur))
102 // The pointer is passed as an argument, ignore this.
103 continue;
104 if (isa<PtrToIntInst>(P))
105 // Assume the worst.
106 return true;
David Blaikie70573dc2014-11-19 07:49:26 +0000107 if (Visited.insert(Ur).second)
Michael Gottesman778138e2013-01-29 03:03:03 +0000108 Worklist.push_back(Ur);
109 }
110 } while (!Worklist.empty());
111
112 // Everything checked out.
113 return false;
114}
115
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000116bool ProvenanceAnalysis::relatedCheck(const Value *A, const Value *B,
117 const DataLayout &DL) {
Michael Gottesman778138e2013-01-29 03:03:03 +0000118 // Skip past provenance pass-throughs.
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000119 A = GetUnderlyingObjCPtr(A, DL);
120 B = GetUnderlyingObjCPtr(B, DL);
Michael Gottesman778138e2013-01-29 03:03:03 +0000121
122 // Quick check.
123 if (A == B)
124 return true;
125
126 // Ask regular AliasAnalysis, for a first approximation.
127 switch (AA->alias(A, B)) {
Chandler Carruthc3f49eb2015-06-22 02:16:51 +0000128 case NoAlias:
Michael Gottesman778138e2013-01-29 03:03:03 +0000129 return false;
Chandler Carruthc3f49eb2015-06-22 02:16:51 +0000130 case MustAlias:
131 case PartialAlias:
Michael Gottesman778138e2013-01-29 03:03:03 +0000132 return true;
Chandler Carruthc3f49eb2015-06-22 02:16:51 +0000133 case MayAlias:
Michael Gottesman778138e2013-01-29 03:03:03 +0000134 break;
135 }
136
137 bool AIsIdentified = IsObjCIdentifiedObject(A);
138 bool BIsIdentified = IsObjCIdentifiedObject(B);
139
140 // An ObjC-Identified object can't alias a load if it is never locally stored.
141 if (AIsIdentified) {
142 // Check for an obvious escape.
143 if (isa<LoadInst>(B))
Michael Gottesman27029f42013-02-12 23:35:08 +0000144 return IsStoredObjCPointer(A);
Michael Gottesman778138e2013-01-29 03:03:03 +0000145 if (BIsIdentified) {
146 // Check for an obvious escape.
147 if (isa<LoadInst>(A))
Michael Gottesman27029f42013-02-12 23:35:08 +0000148 return IsStoredObjCPointer(B);
Michael Gottesman778138e2013-01-29 03:03:03 +0000149 // Both pointers are identified and escapes aren't an evident problem.
150 return false;
151 }
152 } else if (BIsIdentified) {
153 // Check for an obvious escape.
154 if (isa<LoadInst>(A))
Michael Gottesman27029f42013-02-12 23:35:08 +0000155 return IsStoredObjCPointer(B);
Michael Gottesman778138e2013-01-29 03:03:03 +0000156 }
157
158 // Special handling for PHI and Select.
159 if (const PHINode *PN = dyn_cast<PHINode>(A))
160 return relatedPHI(PN, B);
161 if (const PHINode *PN = dyn_cast<PHINode>(B))
162 return relatedPHI(PN, A);
163 if (const SelectInst *S = dyn_cast<SelectInst>(A))
164 return relatedSelect(S, B);
165 if (const SelectInst *S = dyn_cast<SelectInst>(B))
166 return relatedSelect(S, A);
167
168 // Conservative.
169 return true;
170}
171
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000172bool ProvenanceAnalysis::related(const Value *A, const Value *B,
173 const DataLayout &DL) {
Michael Gottesman778138e2013-01-29 03:03:03 +0000174 // Begin by inserting a conservative value into the map. If the insertion
175 // fails, we have the answer already. If it succeeds, leave it there until we
176 // compute the real answer to guard against recursive queries.
177 if (A > B) std::swap(A, B);
178 std::pair<CachedResultsTy::iterator, bool> Pair =
179 CachedResults.insert(std::make_pair(ValuePairTy(A, B), true));
180 if (!Pair.second)
181 return Pair.first->second;
182
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000183 bool Result = relatedCheck(A, B, DL);
Michael Gottesman778138e2013-01-29 03:03:03 +0000184 CachedResults[ValuePairTy(A, B)] = Result;
185 return Result;
186}