blob: e8f8fb6f3a7c009c7b6ce4b6db6e8f9435254dd9 [file] [log] [blame]
Michael Gottesman778138e2013-01-29 03:03:03 +00001//===- DependencyAnalysis.cpp - ObjC ARC Optimization ---------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Michael Gottesman778138e2013-01-29 03:03:03 +00006//
7//===----------------------------------------------------------------------===//
8/// \file
9///
10/// This file defines special dependency analysis routines used in Objective C
11/// ARC Optimizations.
12///
13/// WARNING: This file knows about certain library functions. It recognizes them
14/// by name, and hardwires knowledge of their semantics.
15///
16/// WARNING: This file knows about how certain Objective-C library functions are
17/// used. Naive LLVM IR transformations which would otherwise be
18/// behavior-preserving may break these assumptions.
19///
20//===----------------------------------------------------------------------===//
21
Michael Gottesman778138e2013-01-29 03:03:03 +000022#include "DependencyAnalysis.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000023#include "ObjCARC.h"
Michael Gottesman278266f2013-01-29 04:20:52 +000024#include "ProvenanceAnalysis.h"
Chandler Carruth1305dc32014-03-04 11:45:46 +000025#include "llvm/IR/CFG.h"
Michael Gottesman778138e2013-01-29 03:03:03 +000026
27using namespace llvm;
28using namespace llvm::objcarc;
29
Chandler Carruth964daaa2014-04-22 02:55:47 +000030#define DEBUG_TYPE "objc-arc-dependency"
31
Michael Gottesman778138e2013-01-29 03:03:03 +000032/// Test whether the given instruction can result in a reference count
33/// modification (positive or negative) for the pointer's object.
Michael Gottesman6f729fa2015-02-19 19:51:32 +000034bool llvm::objcarc::CanAlterRefCount(const Instruction *Inst, const Value *Ptr,
35 ProvenanceAnalysis &PA,
36 ARCInstKind Class) {
Michael Gottesman778138e2013-01-29 03:03:03 +000037 switch (Class) {
Michael Gottesman6f729fa2015-02-19 19:51:32 +000038 case ARCInstKind::Autorelease:
39 case ARCInstKind::AutoreleaseRV:
40 case ARCInstKind::IntrinsicUser:
41 case ARCInstKind::User:
Michael Gottesman778138e2013-01-29 03:03:03 +000042 // These operations never directly modify a reference count.
43 return false;
44 default: break;
45 }
46
Chandler Carruth363ac682019-01-07 05:42:51 +000047 const auto *Call = cast<CallBase>(Inst);
Michael Gottesman778138e2013-01-29 03:03:03 +000048
49 // See if AliasAnalysis can help us with the call.
Chandler Carruth363ac682019-01-07 05:42:51 +000050 FunctionModRefBehavior MRB = PA.getAA()->getModRefBehavior(Call);
Michael Gottesman778138e2013-01-29 03:03:03 +000051 if (AliasAnalysis::onlyReadsMemory(MRB))
52 return false;
53 if (AliasAnalysis::onlyAccessesArgPointees(MRB)) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +000054 const DataLayout &DL = Inst->getModule()->getDataLayout();
Chandler Carruth363ac682019-01-07 05:42:51 +000055 for (const Value *Op : Call->args()) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +000056 if (IsPotentialRetainableObjPtr(Op, *PA.getAA()) &&
57 PA.related(Ptr, Op, DL))
Michael Gottesman778138e2013-01-29 03:03:03 +000058 return true;
59 }
60 return false;
61 }
62
63 // Assume the worst.
64 return true;
65}
66
Michael Gottesman5ab64de2015-02-20 00:02:45 +000067bool llvm::objcarc::CanDecrementRefCount(const Instruction *Inst,
68 const Value *Ptr,
69 ProvenanceAnalysis &PA,
70 ARCInstKind Class) {
71 // First perform a quick check if Class can not touch ref counts.
72 if (!CanDecrementRefCount(Class))
73 return false;
74
75 // Otherwise, just use CanAlterRefCount for now.
76 return CanAlterRefCount(Inst, Ptr, PA, Class);
77}
78
Michael Gottesman778138e2013-01-29 03:03:03 +000079/// Test whether the given instruction can "use" the given pointer's object in a
80/// way that requires the reference count to be positive.
Michael Gottesman6f729fa2015-02-19 19:51:32 +000081bool llvm::objcarc::CanUse(const Instruction *Inst, const Value *Ptr,
82 ProvenanceAnalysis &PA, ARCInstKind Class) {
83 // ARCInstKind::Call operations (as opposed to
84 // ARCInstKind::CallOrUser) never "use" objc pointers.
85 if (Class == ARCInstKind::Call)
Michael Gottesman778138e2013-01-29 03:03:03 +000086 return false;
87
Mehdi Aminia28d91d2015-03-10 02:37:25 +000088 const DataLayout &DL = Inst->getModule()->getDataLayout();
89
Michael Gottesman778138e2013-01-29 03:03:03 +000090 // Consider various instructions which may have pointer arguments which are
91 // not "uses".
92 if (const ICmpInst *ICI = dyn_cast<ICmpInst>(Inst)) {
93 // Comparing a pointer with null, or any other constant, isn't really a use,
94 // because we don't care what the pointer points to, or about the values
95 // of any other dynamic reference-counted pointers.
96 if (!IsPotentialRetainableObjPtr(ICI->getOperand(1), *PA.getAA()))
97 return false;
Benjamin Kramer3a09ef62015-04-10 14:50:08 +000098 } else if (auto CS = ImmutableCallSite(Inst)) {
Michael Gottesman778138e2013-01-29 03:03:03 +000099 // For calls, just check the arguments (and not the callee operand).
100 for (ImmutableCallSite::arg_iterator OI = CS.arg_begin(),
101 OE = CS.arg_end(); OI != OE; ++OI) {
102 const Value *Op = *OI;
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000103 if (IsPotentialRetainableObjPtr(Op, *PA.getAA()) &&
104 PA.related(Ptr, Op, DL))
Michael Gottesman778138e2013-01-29 03:03:03 +0000105 return true;
106 }
107 return false;
108 } else if (const StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
109 // Special-case stores, because we don't care about the stored value, just
110 // the store address.
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000111 const Value *Op = GetUnderlyingObjCPtr(SI->getPointerOperand(), DL);
Michael Gottesman778138e2013-01-29 03:03:03 +0000112 // If we can't tell what the underlying object was, assume there is a
113 // dependence.
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000114 return IsPotentialRetainableObjPtr(Op, *PA.getAA()) &&
115 PA.related(Op, Ptr, DL);
Michael Gottesman778138e2013-01-29 03:03:03 +0000116 }
117
118 // Check each operand for a match.
119 for (User::const_op_iterator OI = Inst->op_begin(), OE = Inst->op_end();
120 OI != OE; ++OI) {
121 const Value *Op = *OI;
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000122 if (IsPotentialRetainableObjPtr(Op, *PA.getAA()) && PA.related(Ptr, Op, DL))
Michael Gottesman778138e2013-01-29 03:03:03 +0000123 return true;
124 }
125 return false;
126}
127
128/// Test if there can be dependencies on Inst through Arg. This function only
129/// tests dependencies relevant for removing pairs of calls.
130bool
131llvm::objcarc::Depends(DependenceKind Flavor, Instruction *Inst,
132 const Value *Arg, ProvenanceAnalysis &PA) {
133 // If we've reached the definition of Arg, stop.
134 if (Inst == Arg)
135 return true;
136
137 switch (Flavor) {
138 case NeedsPositiveRetainCount: {
Michael Gottesman6f729fa2015-02-19 19:51:32 +0000139 ARCInstKind Class = GetARCInstKind(Inst);
Michael Gottesman778138e2013-01-29 03:03:03 +0000140 switch (Class) {
Michael Gottesman6f729fa2015-02-19 19:51:32 +0000141 case ARCInstKind::AutoreleasepoolPop:
142 case ARCInstKind::AutoreleasepoolPush:
143 case ARCInstKind::None:
Michael Gottesman778138e2013-01-29 03:03:03 +0000144 return false;
145 default:
146 return CanUse(Inst, Arg, PA, Class);
147 }
148 }
149
150 case AutoreleasePoolBoundary: {
Michael Gottesman6f729fa2015-02-19 19:51:32 +0000151 ARCInstKind Class = GetARCInstKind(Inst);
Michael Gottesman778138e2013-01-29 03:03:03 +0000152 switch (Class) {
Michael Gottesman6f729fa2015-02-19 19:51:32 +0000153 case ARCInstKind::AutoreleasepoolPop:
154 case ARCInstKind::AutoreleasepoolPush:
Michael Gottesman778138e2013-01-29 03:03:03 +0000155 // These mark the end and begin of an autorelease pool scope.
156 return true;
157 default:
158 // Nothing else does this.
159 return false;
160 }
161 }
162
163 case CanChangeRetainCount: {
Michael Gottesman6f729fa2015-02-19 19:51:32 +0000164 ARCInstKind Class = GetARCInstKind(Inst);
Michael Gottesman778138e2013-01-29 03:03:03 +0000165 switch (Class) {
Michael Gottesman6f729fa2015-02-19 19:51:32 +0000166 case ARCInstKind::AutoreleasepoolPop:
Michael Gottesman778138e2013-01-29 03:03:03 +0000167 // Conservatively assume this can decrement any count.
168 return true;
Michael Gottesman6f729fa2015-02-19 19:51:32 +0000169 case ARCInstKind::AutoreleasepoolPush:
170 case ARCInstKind::None:
Michael Gottesman778138e2013-01-29 03:03:03 +0000171 return false;
172 default:
173 return CanAlterRefCount(Inst, Arg, PA, Class);
174 }
175 }
176
177 case RetainAutoreleaseDep:
Michael Gottesman6f729fa2015-02-19 19:51:32 +0000178 switch (GetBasicARCInstKind(Inst)) {
179 case ARCInstKind::AutoreleasepoolPop:
180 case ARCInstKind::AutoreleasepoolPush:
Michael Gottesman778138e2013-01-29 03:03:03 +0000181 // Don't merge an objc_autorelease with an objc_retain inside a different
182 // autoreleasepool scope.
183 return true;
Michael Gottesman6f729fa2015-02-19 19:51:32 +0000184 case ARCInstKind::Retain:
185 case ARCInstKind::RetainRV:
Michael Gottesman778138e2013-01-29 03:03:03 +0000186 // Check for a retain of the same pointer for merging.
Michael Gottesmane5ad66f2015-02-19 00:42:38 +0000187 return GetArgRCIdentityRoot(Inst) == Arg;
Michael Gottesman778138e2013-01-29 03:03:03 +0000188 default:
189 // Nothing else matters for objc_retainAutorelease formation.
190 return false;
191 }
192
193 case RetainAutoreleaseRVDep: {
Michael Gottesman6f729fa2015-02-19 19:51:32 +0000194 ARCInstKind Class = GetBasicARCInstKind(Inst);
Michael Gottesman778138e2013-01-29 03:03:03 +0000195 switch (Class) {
Michael Gottesman6f729fa2015-02-19 19:51:32 +0000196 case ARCInstKind::Retain:
197 case ARCInstKind::RetainRV:
Michael Gottesman778138e2013-01-29 03:03:03 +0000198 // Check for a retain of the same pointer for merging.
Michael Gottesmane5ad66f2015-02-19 00:42:38 +0000199 return GetArgRCIdentityRoot(Inst) == Arg;
Michael Gottesman778138e2013-01-29 03:03:03 +0000200 default:
201 // Anything that can autorelease interrupts
202 // retainAutoreleaseReturnValue formation.
203 return CanInterruptRV(Class);
204 }
205 }
206
207 case RetainRVDep:
Michael Gottesman6f729fa2015-02-19 19:51:32 +0000208 return CanInterruptRV(GetBasicARCInstKind(Inst));
Michael Gottesman778138e2013-01-29 03:03:03 +0000209 }
210
211 llvm_unreachable("Invalid dependence flavor");
212}
213
214/// Walk up the CFG from StartPos (which is in StartBB) and find local and
215/// non-local dependencies on Arg.
216///
217/// TODO: Cache results?
218void
219llvm::objcarc::FindDependencies(DependenceKind Flavor,
220 const Value *Arg,
221 BasicBlock *StartBB, Instruction *StartInst,
Craig Topper71b7b682014-08-21 05:55:13 +0000222 SmallPtrSetImpl<Instruction *> &DependingInsts,
223 SmallPtrSetImpl<const BasicBlock *> &Visited,
Michael Gottesman778138e2013-01-29 03:03:03 +0000224 ProvenanceAnalysis &PA) {
Duncan P. N. Exon Smith1e59a662015-10-19 23:20:14 +0000225 BasicBlock::iterator StartPos = StartInst->getIterator();
Michael Gottesman778138e2013-01-29 03:03:03 +0000226
227 SmallVector<std::pair<BasicBlock *, BasicBlock::iterator>, 4> Worklist;
228 Worklist.push_back(std::make_pair(StartBB, StartPos));
229 do {
230 std::pair<BasicBlock *, BasicBlock::iterator> Pair =
231 Worklist.pop_back_val();
232 BasicBlock *LocalStartBB = Pair.first;
233 BasicBlock::iterator LocalStartPos = Pair.second;
234 BasicBlock::iterator StartBBBegin = LocalStartBB->begin();
235 for (;;) {
236 if (LocalStartPos == StartBBBegin) {
237 pred_iterator PI(LocalStartBB), PE(LocalStartBB, false);
238 if (PI == PE)
239 // If we've reached the function entry, produce a null dependence.
Craig Topperf40110f2014-04-25 05:29:35 +0000240 DependingInsts.insert(nullptr);
Michael Gottesman778138e2013-01-29 03:03:03 +0000241 else
242 // Add the predecessors to the worklist.
243 do {
244 BasicBlock *PredBB = *PI;
David Blaikie70573dc2014-11-19 07:49:26 +0000245 if (Visited.insert(PredBB).second)
Michael Gottesman778138e2013-01-29 03:03:03 +0000246 Worklist.push_back(std::make_pair(PredBB, PredBB->end()));
247 } while (++PI != PE);
248 break;
249 }
250
Duncan P. N. Exon Smith1e59a662015-10-19 23:20:14 +0000251 Instruction *Inst = &*--LocalStartPos;
Michael Gottesman778138e2013-01-29 03:03:03 +0000252 if (Depends(Flavor, Inst, Arg, PA)) {
253 DependingInsts.insert(Inst);
254 break;
255 }
256 }
257 } while (!Worklist.empty());
258
259 // Determine whether the original StartBB post-dominates all of the blocks we
260 // visited. If not, insert a sentinal indicating that most optimizations are
261 // not safe.
Craig Topper46276792014-08-24 23:23:06 +0000262 for (const BasicBlock *BB : Visited) {
Michael Gottesman778138e2013-01-29 03:03:03 +0000263 if (BB == StartBB)
264 continue;
Chandler Carruthc1e3ee22018-10-18 00:38:34 +0000265 for (const BasicBlock *Succ : successors(BB))
Michael Gottesman778138e2013-01-29 03:03:03 +0000266 if (Succ != StartBB && !Visited.count(Succ)) {
267 DependingInsts.insert(reinterpret_cast<Instruction *>(-1));
268 return;
269 }
Michael Gottesman778138e2013-01-29 03:03:03 +0000270 }
271}