blob: 4bd5fd1acd4c39ba1af2a5e85a192610e58603dc [file] [log] [blame]
Michael Gottesman778138e2013-01-29 03:03:03 +00001//===- DependencyAnalysis.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//===----------------------------------------------------------------------===//
9/// \file
10///
11/// This file defines special dependency analysis routines used in Objective C
12/// ARC Optimizations.
13///
14/// WARNING: This file knows about certain library functions. It recognizes them
15/// by name, and hardwires knowledge of their semantics.
16///
17/// WARNING: This file knows about how certain Objective-C library functions are
18/// used. Naive LLVM IR transformations which would otherwise be
19/// behavior-preserving may break these assumptions.
20///
21//===----------------------------------------------------------------------===//
22
Michael Gottesman778138e2013-01-29 03:03:03 +000023#include "DependencyAnalysis.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000024#include "ObjCARC.h"
Michael Gottesman278266f2013-01-29 04:20:52 +000025#include "ProvenanceAnalysis.h"
Chandler Carruth1305dc32014-03-04 11:45:46 +000026#include "llvm/IR/CFG.h"
Michael Gottesman778138e2013-01-29 03:03:03 +000027
28using namespace llvm;
29using namespace llvm::objcarc;
30
Chandler Carruth964daaa2014-04-22 02:55:47 +000031#define DEBUG_TYPE "objc-arc-dependency"
32
Michael Gottesman778138e2013-01-29 03:03:03 +000033/// Test whether the given instruction can result in a reference count
34/// modification (positive or negative) for the pointer's object.
Michael Gottesman6f729fa2015-02-19 19:51:32 +000035bool llvm::objcarc::CanAlterRefCount(const Instruction *Inst, const Value *Ptr,
36 ProvenanceAnalysis &PA,
37 ARCInstKind Class) {
Michael Gottesman778138e2013-01-29 03:03:03 +000038 switch (Class) {
Michael Gottesman6f729fa2015-02-19 19:51:32 +000039 case ARCInstKind::Autorelease:
40 case ARCInstKind::AutoreleaseRV:
41 case ARCInstKind::IntrinsicUser:
42 case ARCInstKind::User:
Michael Gottesman778138e2013-01-29 03:03:03 +000043 // These operations never directly modify a reference count.
44 return false;
45 default: break;
46 }
47
Chandler Carruth363ac682019-01-07 05:42:51 +000048 const auto *Call = cast<CallBase>(Inst);
Michael Gottesman778138e2013-01-29 03:03:03 +000049
50 // See if AliasAnalysis can help us with the call.
Chandler Carruth363ac682019-01-07 05:42:51 +000051 FunctionModRefBehavior MRB = PA.getAA()->getModRefBehavior(Call);
Michael Gottesman778138e2013-01-29 03:03:03 +000052 if (AliasAnalysis::onlyReadsMemory(MRB))
53 return false;
54 if (AliasAnalysis::onlyAccessesArgPointees(MRB)) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +000055 const DataLayout &DL = Inst->getModule()->getDataLayout();
Chandler Carruth363ac682019-01-07 05:42:51 +000056 for (const Value *Op : Call->args()) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +000057 if (IsPotentialRetainableObjPtr(Op, *PA.getAA()) &&
58 PA.related(Ptr, Op, DL))
Michael Gottesman778138e2013-01-29 03:03:03 +000059 return true;
60 }
61 return false;
62 }
63
64 // Assume the worst.
65 return true;
66}
67
Michael Gottesman5ab64de2015-02-20 00:02:45 +000068bool llvm::objcarc::CanDecrementRefCount(const Instruction *Inst,
69 const Value *Ptr,
70 ProvenanceAnalysis &PA,
71 ARCInstKind Class) {
72 // First perform a quick check if Class can not touch ref counts.
73 if (!CanDecrementRefCount(Class))
74 return false;
75
76 // Otherwise, just use CanAlterRefCount for now.
77 return CanAlterRefCount(Inst, Ptr, PA, Class);
78}
79
Michael Gottesman778138e2013-01-29 03:03:03 +000080/// Test whether the given instruction can "use" the given pointer's object in a
81/// way that requires the reference count to be positive.
Michael Gottesman6f729fa2015-02-19 19:51:32 +000082bool llvm::objcarc::CanUse(const Instruction *Inst, const Value *Ptr,
83 ProvenanceAnalysis &PA, ARCInstKind Class) {
84 // ARCInstKind::Call operations (as opposed to
85 // ARCInstKind::CallOrUser) never "use" objc pointers.
86 if (Class == ARCInstKind::Call)
Michael Gottesman778138e2013-01-29 03:03:03 +000087 return false;
88
Mehdi Aminia28d91d2015-03-10 02:37:25 +000089 const DataLayout &DL = Inst->getModule()->getDataLayout();
90
Michael Gottesman778138e2013-01-29 03:03:03 +000091 // Consider various instructions which may have pointer arguments which are
92 // not "uses".
93 if (const ICmpInst *ICI = dyn_cast<ICmpInst>(Inst)) {
94 // Comparing a pointer with null, or any other constant, isn't really a use,
95 // because we don't care what the pointer points to, or about the values
96 // of any other dynamic reference-counted pointers.
97 if (!IsPotentialRetainableObjPtr(ICI->getOperand(1), *PA.getAA()))
98 return false;
Benjamin Kramer3a09ef62015-04-10 14:50:08 +000099 } else if (auto CS = ImmutableCallSite(Inst)) {
Michael Gottesman778138e2013-01-29 03:03:03 +0000100 // For calls, just check the arguments (and not the callee operand).
101 for (ImmutableCallSite::arg_iterator OI = CS.arg_begin(),
102 OE = CS.arg_end(); OI != OE; ++OI) {
103 const Value *Op = *OI;
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000104 if (IsPotentialRetainableObjPtr(Op, *PA.getAA()) &&
105 PA.related(Ptr, Op, DL))
Michael Gottesman778138e2013-01-29 03:03:03 +0000106 return true;
107 }
108 return false;
109 } else if (const StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
110 // Special-case stores, because we don't care about the stored value, just
111 // the store address.
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000112 const Value *Op = GetUnderlyingObjCPtr(SI->getPointerOperand(), DL);
Michael Gottesman778138e2013-01-29 03:03:03 +0000113 // If we can't tell what the underlying object was, assume there is a
114 // dependence.
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000115 return IsPotentialRetainableObjPtr(Op, *PA.getAA()) &&
116 PA.related(Op, Ptr, DL);
Michael Gottesman778138e2013-01-29 03:03:03 +0000117 }
118
119 // Check each operand for a match.
120 for (User::const_op_iterator OI = Inst->op_begin(), OE = Inst->op_end();
121 OI != OE; ++OI) {
122 const Value *Op = *OI;
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000123 if (IsPotentialRetainableObjPtr(Op, *PA.getAA()) && PA.related(Ptr, Op, DL))
Michael Gottesman778138e2013-01-29 03:03:03 +0000124 return true;
125 }
126 return false;
127}
128
129/// Test if there can be dependencies on Inst through Arg. This function only
130/// tests dependencies relevant for removing pairs of calls.
131bool
132llvm::objcarc::Depends(DependenceKind Flavor, Instruction *Inst,
133 const Value *Arg, ProvenanceAnalysis &PA) {
134 // If we've reached the definition of Arg, stop.
135 if (Inst == Arg)
136 return true;
137
138 switch (Flavor) {
139 case NeedsPositiveRetainCount: {
Michael Gottesman6f729fa2015-02-19 19:51:32 +0000140 ARCInstKind Class = GetARCInstKind(Inst);
Michael Gottesman778138e2013-01-29 03:03:03 +0000141 switch (Class) {
Michael Gottesman6f729fa2015-02-19 19:51:32 +0000142 case ARCInstKind::AutoreleasepoolPop:
143 case ARCInstKind::AutoreleasepoolPush:
144 case ARCInstKind::None:
Michael Gottesman778138e2013-01-29 03:03:03 +0000145 return false;
146 default:
147 return CanUse(Inst, Arg, PA, Class);
148 }
149 }
150
151 case AutoreleasePoolBoundary: {
Michael Gottesman6f729fa2015-02-19 19:51:32 +0000152 ARCInstKind Class = GetARCInstKind(Inst);
Michael Gottesman778138e2013-01-29 03:03:03 +0000153 switch (Class) {
Michael Gottesman6f729fa2015-02-19 19:51:32 +0000154 case ARCInstKind::AutoreleasepoolPop:
155 case ARCInstKind::AutoreleasepoolPush:
Michael Gottesman778138e2013-01-29 03:03:03 +0000156 // These mark the end and begin of an autorelease pool scope.
157 return true;
158 default:
159 // Nothing else does this.
160 return false;
161 }
162 }
163
164 case CanChangeRetainCount: {
Michael Gottesman6f729fa2015-02-19 19:51:32 +0000165 ARCInstKind Class = GetARCInstKind(Inst);
Michael Gottesman778138e2013-01-29 03:03:03 +0000166 switch (Class) {
Michael Gottesman6f729fa2015-02-19 19:51:32 +0000167 case ARCInstKind::AutoreleasepoolPop:
Michael Gottesman778138e2013-01-29 03:03:03 +0000168 // Conservatively assume this can decrement any count.
169 return true;
Michael Gottesman6f729fa2015-02-19 19:51:32 +0000170 case ARCInstKind::AutoreleasepoolPush:
171 case ARCInstKind::None:
Michael Gottesman778138e2013-01-29 03:03:03 +0000172 return false;
173 default:
174 return CanAlterRefCount(Inst, Arg, PA, Class);
175 }
176 }
177
178 case RetainAutoreleaseDep:
Michael Gottesman6f729fa2015-02-19 19:51:32 +0000179 switch (GetBasicARCInstKind(Inst)) {
180 case ARCInstKind::AutoreleasepoolPop:
181 case ARCInstKind::AutoreleasepoolPush:
Michael Gottesman778138e2013-01-29 03:03:03 +0000182 // Don't merge an objc_autorelease with an objc_retain inside a different
183 // autoreleasepool scope.
184 return true;
Michael Gottesman6f729fa2015-02-19 19:51:32 +0000185 case ARCInstKind::Retain:
186 case ARCInstKind::RetainRV:
Michael Gottesman778138e2013-01-29 03:03:03 +0000187 // Check for a retain of the same pointer for merging.
Michael Gottesmane5ad66f2015-02-19 00:42:38 +0000188 return GetArgRCIdentityRoot(Inst) == Arg;
Michael Gottesman778138e2013-01-29 03:03:03 +0000189 default:
190 // Nothing else matters for objc_retainAutorelease formation.
191 return false;
192 }
193
194 case RetainAutoreleaseRVDep: {
Michael Gottesman6f729fa2015-02-19 19:51:32 +0000195 ARCInstKind Class = GetBasicARCInstKind(Inst);
Michael Gottesman778138e2013-01-29 03:03:03 +0000196 switch (Class) {
Michael Gottesman6f729fa2015-02-19 19:51:32 +0000197 case ARCInstKind::Retain:
198 case ARCInstKind::RetainRV:
Michael Gottesman778138e2013-01-29 03:03:03 +0000199 // Check for a retain of the same pointer for merging.
Michael Gottesmane5ad66f2015-02-19 00:42:38 +0000200 return GetArgRCIdentityRoot(Inst) == Arg;
Michael Gottesman778138e2013-01-29 03:03:03 +0000201 default:
202 // Anything that can autorelease interrupts
203 // retainAutoreleaseReturnValue formation.
204 return CanInterruptRV(Class);
205 }
206 }
207
208 case RetainRVDep:
Michael Gottesman6f729fa2015-02-19 19:51:32 +0000209 return CanInterruptRV(GetBasicARCInstKind(Inst));
Michael Gottesman778138e2013-01-29 03:03:03 +0000210 }
211
212 llvm_unreachable("Invalid dependence flavor");
213}
214
215/// Walk up the CFG from StartPos (which is in StartBB) and find local and
216/// non-local dependencies on Arg.
217///
218/// TODO: Cache results?
219void
220llvm::objcarc::FindDependencies(DependenceKind Flavor,
221 const Value *Arg,
222 BasicBlock *StartBB, Instruction *StartInst,
Craig Topper71b7b682014-08-21 05:55:13 +0000223 SmallPtrSetImpl<Instruction *> &DependingInsts,
224 SmallPtrSetImpl<const BasicBlock *> &Visited,
Michael Gottesman778138e2013-01-29 03:03:03 +0000225 ProvenanceAnalysis &PA) {
Duncan P. N. Exon Smith1e59a662015-10-19 23:20:14 +0000226 BasicBlock::iterator StartPos = StartInst->getIterator();
Michael Gottesman778138e2013-01-29 03:03:03 +0000227
228 SmallVector<std::pair<BasicBlock *, BasicBlock::iterator>, 4> Worklist;
229 Worklist.push_back(std::make_pair(StartBB, StartPos));
230 do {
231 std::pair<BasicBlock *, BasicBlock::iterator> Pair =
232 Worklist.pop_back_val();
233 BasicBlock *LocalStartBB = Pair.first;
234 BasicBlock::iterator LocalStartPos = Pair.second;
235 BasicBlock::iterator StartBBBegin = LocalStartBB->begin();
236 for (;;) {
237 if (LocalStartPos == StartBBBegin) {
238 pred_iterator PI(LocalStartBB), PE(LocalStartBB, false);
239 if (PI == PE)
240 // If we've reached the function entry, produce a null dependence.
Craig Topperf40110f2014-04-25 05:29:35 +0000241 DependingInsts.insert(nullptr);
Michael Gottesman778138e2013-01-29 03:03:03 +0000242 else
243 // Add the predecessors to the worklist.
244 do {
245 BasicBlock *PredBB = *PI;
David Blaikie70573dc2014-11-19 07:49:26 +0000246 if (Visited.insert(PredBB).second)
Michael Gottesman778138e2013-01-29 03:03:03 +0000247 Worklist.push_back(std::make_pair(PredBB, PredBB->end()));
248 } while (++PI != PE);
249 break;
250 }
251
Duncan P. N. Exon Smith1e59a662015-10-19 23:20:14 +0000252 Instruction *Inst = &*--LocalStartPos;
Michael Gottesman778138e2013-01-29 03:03:03 +0000253 if (Depends(Flavor, Inst, Arg, PA)) {
254 DependingInsts.insert(Inst);
255 break;
256 }
257 }
258 } while (!Worklist.empty());
259
260 // Determine whether the original StartBB post-dominates all of the blocks we
261 // visited. If not, insert a sentinal indicating that most optimizations are
262 // not safe.
Craig Topper46276792014-08-24 23:23:06 +0000263 for (const BasicBlock *BB : Visited) {
Michael Gottesman778138e2013-01-29 03:03:03 +0000264 if (BB == StartBB)
265 continue;
Chandler Carruthc1e3ee22018-10-18 00:38:34 +0000266 for (const BasicBlock *Succ : successors(BB))
Michael Gottesman778138e2013-01-29 03:03:03 +0000267 if (Succ != StartBB && !Visited.count(Succ)) {
268 DependingInsts.insert(reinterpret_cast<Instruction *>(-1));
269 return;
270 }
Michael Gottesman778138e2013-01-29 03:03:03 +0000271 }
272}