Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 1 | //===- 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 Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 23 | #include "DependencyAnalysis.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 24 | #include "ObjCARC.h" |
Michael Gottesman | 278266f | 2013-01-29 04:20:52 +0000 | [diff] [blame] | 25 | #include "ProvenanceAnalysis.h" |
Chandler Carruth | 1305dc3 | 2014-03-04 11:45:46 +0000 | [diff] [blame] | 26 | #include "llvm/IR/CFG.h" |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 27 | |
| 28 | using namespace llvm; |
| 29 | using namespace llvm::objcarc; |
| 30 | |
Chandler Carruth | 964daaa | 2014-04-22 02:55:47 +0000 | [diff] [blame] | 31 | #define DEBUG_TYPE "objc-arc-dependency" |
| 32 | |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 33 | /// Test whether the given instruction can result in a reference count |
| 34 | /// modification (positive or negative) for the pointer's object. |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 35 | bool llvm::objcarc::CanAlterRefCount(const Instruction *Inst, const Value *Ptr, |
| 36 | ProvenanceAnalysis &PA, |
| 37 | ARCInstKind Class) { |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 38 | switch (Class) { |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 39 | case ARCInstKind::Autorelease: |
| 40 | case ARCInstKind::AutoreleaseRV: |
| 41 | case ARCInstKind::IntrinsicUser: |
| 42 | case ARCInstKind::User: |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 43 | // These operations never directly modify a reference count. |
| 44 | return false; |
| 45 | default: break; |
| 46 | } |
| 47 | |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 48 | const auto *Call = cast<CallBase>(Inst); |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 49 | |
| 50 | // See if AliasAnalysis can help us with the call. |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 51 | FunctionModRefBehavior MRB = PA.getAA()->getModRefBehavior(Call); |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 52 | if (AliasAnalysis::onlyReadsMemory(MRB)) |
| 53 | return false; |
| 54 | if (AliasAnalysis::onlyAccessesArgPointees(MRB)) { |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 55 | const DataLayout &DL = Inst->getModule()->getDataLayout(); |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 56 | for (const Value *Op : Call->args()) { |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 57 | if (IsPotentialRetainableObjPtr(Op, *PA.getAA()) && |
| 58 | PA.related(Ptr, Op, DL)) |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 59 | return true; |
| 60 | } |
| 61 | return false; |
| 62 | } |
| 63 | |
| 64 | // Assume the worst. |
| 65 | return true; |
| 66 | } |
| 67 | |
Michael Gottesman | 5ab64de | 2015-02-20 00:02:45 +0000 | [diff] [blame] | 68 | bool 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 Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 80 | /// 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 Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 82 | bool 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 Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 87 | return false; |
| 88 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 89 | const DataLayout &DL = Inst->getModule()->getDataLayout(); |
| 90 | |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 91 | // 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 Kramer | 3a09ef6 | 2015-04-10 14:50:08 +0000 | [diff] [blame] | 99 | } else if (auto CS = ImmutableCallSite(Inst)) { |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 100 | // 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 Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 104 | if (IsPotentialRetainableObjPtr(Op, *PA.getAA()) && |
| 105 | PA.related(Ptr, Op, DL)) |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 106 | 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 Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 112 | const Value *Op = GetUnderlyingObjCPtr(SI->getPointerOperand(), DL); |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 113 | // If we can't tell what the underlying object was, assume there is a |
| 114 | // dependence. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 115 | return IsPotentialRetainableObjPtr(Op, *PA.getAA()) && |
| 116 | PA.related(Op, Ptr, DL); |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 117 | } |
| 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 Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 123 | if (IsPotentialRetainableObjPtr(Op, *PA.getAA()) && PA.related(Ptr, Op, DL)) |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 124 | 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. |
| 131 | bool |
| 132 | llvm::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 Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 140 | ARCInstKind Class = GetARCInstKind(Inst); |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 141 | switch (Class) { |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 142 | case ARCInstKind::AutoreleasepoolPop: |
| 143 | case ARCInstKind::AutoreleasepoolPush: |
| 144 | case ARCInstKind::None: |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 145 | return false; |
| 146 | default: |
| 147 | return CanUse(Inst, Arg, PA, Class); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | case AutoreleasePoolBoundary: { |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 152 | ARCInstKind Class = GetARCInstKind(Inst); |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 153 | switch (Class) { |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 154 | case ARCInstKind::AutoreleasepoolPop: |
| 155 | case ARCInstKind::AutoreleasepoolPush: |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 156 | // 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 Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 165 | ARCInstKind Class = GetARCInstKind(Inst); |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 166 | switch (Class) { |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 167 | case ARCInstKind::AutoreleasepoolPop: |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 168 | // Conservatively assume this can decrement any count. |
| 169 | return true; |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 170 | case ARCInstKind::AutoreleasepoolPush: |
| 171 | case ARCInstKind::None: |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 172 | return false; |
| 173 | default: |
| 174 | return CanAlterRefCount(Inst, Arg, PA, Class); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | case RetainAutoreleaseDep: |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 179 | switch (GetBasicARCInstKind(Inst)) { |
| 180 | case ARCInstKind::AutoreleasepoolPop: |
| 181 | case ARCInstKind::AutoreleasepoolPush: |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 182 | // Don't merge an objc_autorelease with an objc_retain inside a different |
| 183 | // autoreleasepool scope. |
| 184 | return true; |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 185 | case ARCInstKind::Retain: |
| 186 | case ARCInstKind::RetainRV: |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 187 | // Check for a retain of the same pointer for merging. |
Michael Gottesman | e5ad66f | 2015-02-19 00:42:38 +0000 | [diff] [blame] | 188 | return GetArgRCIdentityRoot(Inst) == Arg; |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 189 | default: |
| 190 | // Nothing else matters for objc_retainAutorelease formation. |
| 191 | return false; |
| 192 | } |
| 193 | |
| 194 | case RetainAutoreleaseRVDep: { |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 195 | ARCInstKind Class = GetBasicARCInstKind(Inst); |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 196 | switch (Class) { |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 197 | case ARCInstKind::Retain: |
| 198 | case ARCInstKind::RetainRV: |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 199 | // Check for a retain of the same pointer for merging. |
Michael Gottesman | e5ad66f | 2015-02-19 00:42:38 +0000 | [diff] [blame] | 200 | return GetArgRCIdentityRoot(Inst) == Arg; |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 201 | default: |
| 202 | // Anything that can autorelease interrupts |
| 203 | // retainAutoreleaseReturnValue formation. |
| 204 | return CanInterruptRV(Class); |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | case RetainRVDep: |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 209 | return CanInterruptRV(GetBasicARCInstKind(Inst)); |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 210 | } |
| 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? |
| 219 | void |
| 220 | llvm::objcarc::FindDependencies(DependenceKind Flavor, |
| 221 | const Value *Arg, |
| 222 | BasicBlock *StartBB, Instruction *StartInst, |
Craig Topper | 71b7b68 | 2014-08-21 05:55:13 +0000 | [diff] [blame] | 223 | SmallPtrSetImpl<Instruction *> &DependingInsts, |
| 224 | SmallPtrSetImpl<const BasicBlock *> &Visited, |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 225 | ProvenanceAnalysis &PA) { |
Duncan P. N. Exon Smith | 1e59a66 | 2015-10-19 23:20:14 +0000 | [diff] [blame] | 226 | BasicBlock::iterator StartPos = StartInst->getIterator(); |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 227 | |
| 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 Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 241 | DependingInsts.insert(nullptr); |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 242 | else |
| 243 | // Add the predecessors to the worklist. |
| 244 | do { |
| 245 | BasicBlock *PredBB = *PI; |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 246 | if (Visited.insert(PredBB).second) |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 247 | Worklist.push_back(std::make_pair(PredBB, PredBB->end())); |
| 248 | } while (++PI != PE); |
| 249 | break; |
| 250 | } |
| 251 | |
Duncan P. N. Exon Smith | 1e59a66 | 2015-10-19 23:20:14 +0000 | [diff] [blame] | 252 | Instruction *Inst = &*--LocalStartPos; |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 253 | 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 Topper | 4627679 | 2014-08-24 23:23:06 +0000 | [diff] [blame] | 263 | for (const BasicBlock *BB : Visited) { |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 264 | if (BB == StartBB) |
| 265 | continue; |
Chandler Carruth | c1e3ee2 | 2018-10-18 00:38:34 +0000 | [diff] [blame] | 266 | for (const BasicBlock *Succ : successors(BB)) |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 267 | if (Succ != StartBB && !Visited.count(Succ)) { |
| 268 | DependingInsts.insert(reinterpret_cast<Instruction *>(-1)); |
| 269 | return; |
| 270 | } |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 271 | } |
| 272 | } |