Michael Gottesman | 79d8d81 | 2013-01-28 01:35:51 +0000 | [diff] [blame] | 1 | //===- ObjCARCOpts.cpp - ObjC ARC Optimization ----------------------------===// |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 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 | //===----------------------------------------------------------------------===// |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 9 | /// \file |
| 10 | /// This file defines ObjC ARC optimizations. ARC stands for Automatic |
| 11 | /// Reference Counting and is a system for managing reference counts for objects |
| 12 | /// in Objective C. |
| 13 | /// |
| 14 | /// The optimizations performed include elimination of redundant, partially |
| 15 | /// redundant, and inconsequential reference count operations, elimination of |
| 16 | /// redundant weak pointer operations, pattern-matching and replacement of |
| 17 | /// low-level operations into higher-level operations, and numerous minor |
| 18 | /// simplifications. |
| 19 | /// |
| 20 | /// This file also defines a simple ARC-aware AliasAnalysis. |
| 21 | /// |
| 22 | /// WARNING: This file knows about certain library functions. It recognizes them |
| 23 | /// by name, and hardwires knowledge of their semantics. |
| 24 | /// |
| 25 | /// WARNING: This file knows about how certain Objective-C library functions are |
| 26 | /// used. Naive LLVM IR transformations which would otherwise be |
| 27 | /// behavior-preserving may break these assumptions. |
| 28 | /// |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 29 | //===----------------------------------------------------------------------===// |
| 30 | |
Michael Gottesman | 08904e3 | 2013-01-28 03:28:38 +0000 | [diff] [blame] | 31 | #define DEBUG_TYPE "objc-arc-opts" |
| 32 | #include "ObjCARC.h" |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 33 | #include "llvm/ADT/DenseMap.h" |
Michael Gottesman | f15c0bb | 2013-01-13 22:12:06 +0000 | [diff] [blame] | 34 | #include "llvm/ADT/SmallPtrSet.h" |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 35 | using namespace llvm; |
Michael Gottesman | 08904e3 | 2013-01-28 03:28:38 +0000 | [diff] [blame] | 36 | using namespace llvm::objcarc; |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 37 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 38 | /// \defgroup MiscUtils Miscellaneous utilities that are not ARC specific. |
| 39 | /// @{ |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 40 | |
| 41 | namespace { |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 42 | /// \brief An associative container with fast insertion-order (deterministic) |
| 43 | /// iteration over its elements. Plus the special blot operation. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 44 | template<class KeyT, class ValueT> |
| 45 | class MapVector { |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 46 | /// Map keys to indices in Vector. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 47 | typedef DenseMap<KeyT, size_t> MapTy; |
| 48 | MapTy Map; |
| 49 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 50 | typedef std::vector<std::pair<KeyT, ValueT> > VectorTy; |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 51 | /// Keys and values. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 52 | VectorTy Vector; |
| 53 | |
| 54 | public: |
| 55 | typedef typename VectorTy::iterator iterator; |
| 56 | typedef typename VectorTy::const_iterator const_iterator; |
| 57 | iterator begin() { return Vector.begin(); } |
| 58 | iterator end() { return Vector.end(); } |
| 59 | const_iterator begin() const { return Vector.begin(); } |
| 60 | const_iterator end() const { return Vector.end(); } |
| 61 | |
| 62 | #ifdef XDEBUG |
| 63 | ~MapVector() { |
| 64 | assert(Vector.size() >= Map.size()); // May differ due to blotting. |
| 65 | for (typename MapTy::const_iterator I = Map.begin(), E = Map.end(); |
| 66 | I != E; ++I) { |
| 67 | assert(I->second < Vector.size()); |
| 68 | assert(Vector[I->second].first == I->first); |
| 69 | } |
| 70 | for (typename VectorTy::const_iterator I = Vector.begin(), |
| 71 | E = Vector.end(); I != E; ++I) |
| 72 | assert(!I->first || |
| 73 | (Map.count(I->first) && |
| 74 | Map[I->first] == size_t(I - Vector.begin()))); |
| 75 | } |
| 76 | #endif |
| 77 | |
Dan Gohman | 55b0674 | 2012-03-02 01:13:53 +0000 | [diff] [blame] | 78 | ValueT &operator[](const KeyT &Arg) { |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 79 | std::pair<typename MapTy::iterator, bool> Pair = |
| 80 | Map.insert(std::make_pair(Arg, size_t(0))); |
| 81 | if (Pair.second) { |
Dan Gohman | 55b0674 | 2012-03-02 01:13:53 +0000 | [diff] [blame] | 82 | size_t Num = Vector.size(); |
| 83 | Pair.first->second = Num; |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 84 | Vector.push_back(std::make_pair(Arg, ValueT())); |
Dan Gohman | 55b0674 | 2012-03-02 01:13:53 +0000 | [diff] [blame] | 85 | return Vector[Num].second; |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 86 | } |
| 87 | return Vector[Pair.first->second].second; |
| 88 | } |
| 89 | |
| 90 | std::pair<iterator, bool> |
| 91 | insert(const std::pair<KeyT, ValueT> &InsertPair) { |
| 92 | std::pair<typename MapTy::iterator, bool> Pair = |
| 93 | Map.insert(std::make_pair(InsertPair.first, size_t(0))); |
| 94 | if (Pair.second) { |
Dan Gohman | 55b0674 | 2012-03-02 01:13:53 +0000 | [diff] [blame] | 95 | size_t Num = Vector.size(); |
| 96 | Pair.first->second = Num; |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 97 | Vector.push_back(InsertPair); |
Dan Gohman | 55b0674 | 2012-03-02 01:13:53 +0000 | [diff] [blame] | 98 | return std::make_pair(Vector.begin() + Num, true); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 99 | } |
| 100 | return std::make_pair(Vector.begin() + Pair.first->second, false); |
| 101 | } |
| 102 | |
Dan Gohman | 55b0674 | 2012-03-02 01:13:53 +0000 | [diff] [blame] | 103 | const_iterator find(const KeyT &Key) const { |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 104 | typename MapTy::const_iterator It = Map.find(Key); |
| 105 | if (It == Map.end()) return Vector.end(); |
| 106 | return Vector.begin() + It->second; |
| 107 | } |
| 108 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 109 | /// This is similar to erase, but instead of removing the element from the |
| 110 | /// vector, it just zeros out the key in the vector. This leaves iterators |
| 111 | /// intact, but clients must be prepared for zeroed-out keys when iterating. |
Dan Gohman | 55b0674 | 2012-03-02 01:13:53 +0000 | [diff] [blame] | 112 | void blot(const KeyT &Key) { |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 113 | typename MapTy::iterator It = Map.find(Key); |
| 114 | if (It == Map.end()) return; |
| 115 | Vector[It->second].first = KeyT(); |
| 116 | Map.erase(It); |
| 117 | } |
| 118 | |
| 119 | void clear() { |
| 120 | Map.clear(); |
| 121 | Vector.clear(); |
| 122 | } |
| 123 | }; |
| 124 | } |
| 125 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 126 | /// @} |
| 127 | /// |
| 128 | /// \defgroup ARCUtilities Utility declarations/definitions specific to ARC. |
| 129 | /// @{ |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 130 | |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 131 | #include "llvm/Analysis/ValueTracking.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 132 | #include "llvm/IR/Intrinsics.h" |
Dan Gohman | 41375a3 | 2012-05-08 23:39:44 +0000 | [diff] [blame] | 133 | #include "llvm/Support/CallSite.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 134 | #include "llvm/Transforms/Utils/Local.h" |
Dan Gohman | 41375a3 | 2012-05-08 23:39:44 +0000 | [diff] [blame] | 135 | |
Michael Gottesman | 5300cdd | 2013-01-27 06:19:48 +0000 | [diff] [blame] | 136 | /// \brief Test whether the given value is possible a retainable object pointer. |
| 137 | static bool IsPotentialRetainableObjPtr(const Value *Op) { |
| 138 | // Pointers to static or stack storage are not valid retainable object pointers. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 139 | if (isa<Constant>(Op) || isa<AllocaInst>(Op)) |
| 140 | return false; |
Michael Gottesman | 5300cdd | 2013-01-27 06:19:48 +0000 | [diff] [blame] | 141 | // Special arguments can not be a valid retainable object pointer. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 142 | if (const Argument *Arg = dyn_cast<Argument>(Op)) |
| 143 | if (Arg->hasByValAttr() || |
| 144 | Arg->hasNestAttr() || |
| 145 | Arg->hasStructRetAttr()) |
| 146 | return false; |
Dan Gohman | bd944b4 | 2011-12-14 19:10:53 +0000 | [diff] [blame] | 147 | // Only consider values with pointer types. |
Michael Gottesman | 5300cdd | 2013-01-27 06:19:48 +0000 | [diff] [blame] | 148 | // |
Dan Gohman | bd944b4 | 2011-12-14 19:10:53 +0000 | [diff] [blame] | 149 | // It seemes intuitive to exclude function pointer types as well, since |
Michael Gottesman | 5300cdd | 2013-01-27 06:19:48 +0000 | [diff] [blame] | 150 | // functions are never retainable object pointers, however clang occasionally |
| 151 | // bitcasts retainable object pointers to function-pointer type temporarily. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 152 | PointerType *Ty = dyn_cast<PointerType>(Op->getType()); |
Dan Gohman | bd944b4 | 2011-12-14 19:10:53 +0000 | [diff] [blame] | 153 | if (!Ty) |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 154 | return false; |
Michael Gottesman | 5300cdd | 2013-01-27 06:19:48 +0000 | [diff] [blame] | 155 | // Conservatively assume anything else is a potential retainable object pointer. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 156 | return true; |
| 157 | } |
| 158 | |
Michael Gottesman | 4385edf | 2013-01-14 01:47:53 +0000 | [diff] [blame] | 159 | /// \brief Helper for GetInstructionClass. Determines what kind of construct CS |
| 160 | /// is. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 161 | static InstructionClass GetCallSiteClass(ImmutableCallSite CS) { |
| 162 | for (ImmutableCallSite::arg_iterator I = CS.arg_begin(), E = CS.arg_end(); |
| 163 | I != E; ++I) |
Michael Gottesman | 5300cdd | 2013-01-27 06:19:48 +0000 | [diff] [blame] | 164 | if (IsPotentialRetainableObjPtr(*I)) |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 165 | return CS.onlyReadsMemory() ? IC_User : IC_CallOrUser; |
| 166 | |
| 167 | return CS.onlyReadsMemory() ? IC_None : IC_Call; |
| 168 | } |
| 169 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 170 | /// \brief Determine what kind of construct V is. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 171 | static InstructionClass GetInstructionClass(const Value *V) { |
| 172 | if (const Instruction *I = dyn_cast<Instruction>(V)) { |
| 173 | // Any instruction other than bitcast and gep with a pointer operand have a |
| 174 | // use of an objc pointer. Bitcasts, GEPs, Selects, PHIs transfer a pointer |
| 175 | // to a subsequent use, rather than using it themselves, in this sense. |
| 176 | // As a short cut, several other opcodes are known to have no pointer |
| 177 | // operands of interest. And ret is never followed by a release, so it's |
| 178 | // not interesting to examine. |
| 179 | switch (I->getOpcode()) { |
| 180 | case Instruction::Call: { |
| 181 | const CallInst *CI = cast<CallInst>(I); |
| 182 | // Check for calls to special functions. |
| 183 | if (const Function *F = CI->getCalledFunction()) { |
| 184 | InstructionClass Class = GetFunctionClass(F); |
| 185 | if (Class != IC_CallOrUser) |
| 186 | return Class; |
| 187 | |
| 188 | // None of the intrinsic functions do objc_release. For intrinsics, the |
| 189 | // only question is whether or not they may be users. |
| 190 | switch (F->getIntrinsicID()) { |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 191 | case Intrinsic::returnaddress: case Intrinsic::frameaddress: |
| 192 | case Intrinsic::stacksave: case Intrinsic::stackrestore: |
| 193 | case Intrinsic::vastart: case Intrinsic::vacopy: case Intrinsic::vaend: |
Dan Gohman | 41375a3 | 2012-05-08 23:39:44 +0000 | [diff] [blame] | 194 | case Intrinsic::objectsize: case Intrinsic::prefetch: |
| 195 | case Intrinsic::stackprotector: |
| 196 | case Intrinsic::eh_return_i32: case Intrinsic::eh_return_i64: |
| 197 | case Intrinsic::eh_typeid_for: case Intrinsic::eh_dwarf_cfa: |
| 198 | case Intrinsic::eh_sjlj_lsda: case Intrinsic::eh_sjlj_functioncontext: |
| 199 | case Intrinsic::init_trampoline: case Intrinsic::adjust_trampoline: |
| 200 | case Intrinsic::lifetime_start: case Intrinsic::lifetime_end: |
| 201 | case Intrinsic::invariant_start: case Intrinsic::invariant_end: |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 202 | // Don't let dbg info affect our results. |
| 203 | case Intrinsic::dbg_declare: case Intrinsic::dbg_value: |
| 204 | // Short cut: Some intrinsics obviously don't use ObjC pointers. |
| 205 | return IC_None; |
| 206 | default: |
Dan Gohman | 41375a3 | 2012-05-08 23:39:44 +0000 | [diff] [blame] | 207 | break; |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 208 | } |
| 209 | } |
| 210 | return GetCallSiteClass(CI); |
| 211 | } |
| 212 | case Instruction::Invoke: |
| 213 | return GetCallSiteClass(cast<InvokeInst>(I)); |
| 214 | case Instruction::BitCast: |
| 215 | case Instruction::GetElementPtr: |
| 216 | case Instruction::Select: case Instruction::PHI: |
| 217 | case Instruction::Ret: case Instruction::Br: |
| 218 | case Instruction::Switch: case Instruction::IndirectBr: |
| 219 | case Instruction::Alloca: case Instruction::VAArg: |
| 220 | case Instruction::Add: case Instruction::FAdd: |
| 221 | case Instruction::Sub: case Instruction::FSub: |
| 222 | case Instruction::Mul: case Instruction::FMul: |
| 223 | case Instruction::SDiv: case Instruction::UDiv: case Instruction::FDiv: |
| 224 | case Instruction::SRem: case Instruction::URem: case Instruction::FRem: |
| 225 | case Instruction::Shl: case Instruction::LShr: case Instruction::AShr: |
| 226 | case Instruction::And: case Instruction::Or: case Instruction::Xor: |
| 227 | case Instruction::SExt: case Instruction::ZExt: case Instruction::Trunc: |
| 228 | case Instruction::IntToPtr: case Instruction::FCmp: |
| 229 | case Instruction::FPTrunc: case Instruction::FPExt: |
| 230 | case Instruction::FPToUI: case Instruction::FPToSI: |
| 231 | case Instruction::UIToFP: case Instruction::SIToFP: |
| 232 | case Instruction::InsertElement: case Instruction::ExtractElement: |
| 233 | case Instruction::ShuffleVector: |
| 234 | case Instruction::ExtractValue: |
| 235 | break; |
| 236 | case Instruction::ICmp: |
| 237 | // Comparing a pointer with null, or any other constant, isn't an |
| 238 | // interesting use, because we don't care what the pointer points to, or |
| 239 | // about the values of any other dynamic reference-counted pointers. |
Michael Gottesman | 5300cdd | 2013-01-27 06:19:48 +0000 | [diff] [blame] | 240 | if (IsPotentialRetainableObjPtr(I->getOperand(1))) |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 241 | return IC_User; |
| 242 | break; |
| 243 | default: |
| 244 | // For anything else, check all the operands. |
Dan Gohman | 4b8e8ce | 2011-08-22 17:29:37 +0000 | [diff] [blame] | 245 | // Note that this includes both operands of a Store: while the first |
| 246 | // operand isn't actually being dereferenced, it is being stored to |
| 247 | // memory where we can no longer track who might read it and dereference |
| 248 | // it, so we have to consider it potentially used. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 249 | for (User::const_op_iterator OI = I->op_begin(), OE = I->op_end(); |
| 250 | OI != OE; ++OI) |
Michael Gottesman | 5300cdd | 2013-01-27 06:19:48 +0000 | [diff] [blame] | 251 | if (IsPotentialRetainableObjPtr(*OI)) |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 252 | return IC_User; |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | // Otherwise, it's totally inert for ARC purposes. |
| 257 | return IC_None; |
| 258 | } |
| 259 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 260 | /// \brief Test if the given class is objc_retain or equivalent. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 261 | static bool IsRetain(InstructionClass Class) { |
| 262 | return Class == IC_Retain || |
| 263 | Class == IC_RetainRV; |
| 264 | } |
| 265 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 266 | /// \brief Test if the given class is objc_autorelease or equivalent. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 267 | static bool IsAutorelease(InstructionClass Class) { |
| 268 | return Class == IC_Autorelease || |
| 269 | Class == IC_AutoreleaseRV; |
| 270 | } |
| 271 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 272 | /// \brief Test if the given class represents instructions which return their |
| 273 | /// argument verbatim. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 274 | static bool IsForwarding(InstructionClass Class) { |
| 275 | // objc_retainBlock technically doesn't always return its argument |
| 276 | // verbatim, but it doesn't matter for our purposes here. |
| 277 | return Class == IC_Retain || |
| 278 | Class == IC_RetainRV || |
| 279 | Class == IC_Autorelease || |
| 280 | Class == IC_AutoreleaseRV || |
| 281 | Class == IC_RetainBlock || |
| 282 | Class == IC_NoopCast; |
| 283 | } |
| 284 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 285 | /// \brief Test if the given class represents instructions which do nothing if |
| 286 | /// passed a null pointer. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 287 | static bool IsNoopOnNull(InstructionClass Class) { |
| 288 | return Class == IC_Retain || |
| 289 | Class == IC_RetainRV || |
| 290 | Class == IC_Release || |
| 291 | Class == IC_Autorelease || |
| 292 | Class == IC_AutoreleaseRV || |
| 293 | Class == IC_RetainBlock; |
| 294 | } |
| 295 | |
Michael Gottesman | 4385edf | 2013-01-14 01:47:53 +0000 | [diff] [blame] | 296 | /// \brief Test if the given class represents instructions which are always safe |
| 297 | /// to mark with the "tail" keyword. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 298 | static bool IsAlwaysTail(InstructionClass Class) { |
| 299 | // IC_RetainBlock may be given a stack argument. |
| 300 | return Class == IC_Retain || |
| 301 | Class == IC_RetainRV || |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 302 | Class == IC_AutoreleaseRV; |
| 303 | } |
| 304 | |
Michael Gottesman | c9656fa | 2013-01-12 01:25:15 +0000 | [diff] [blame] | 305 | /// \brief Test if the given class represents instructions which are never safe |
| 306 | /// to mark with the "tail" keyword. |
| 307 | static bool IsNeverTail(InstructionClass Class) { |
| 308 | /// It is never safe to tail call objc_autorelease since by tail calling |
| 309 | /// objc_autorelease, we also tail call -[NSObject autorelease] which supports |
| 310 | /// fast autoreleasing causing our object to be potentially reclaimed from the |
| 311 | /// autorelease pool which violates the semantics of __autoreleasing types in |
| 312 | /// ARC. |
| 313 | return Class == IC_Autorelease; |
| 314 | } |
| 315 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 316 | /// \brief Test if the given class represents instructions which are always safe |
| 317 | /// to mark with the nounwind attribute. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 318 | static bool IsNoThrow(InstructionClass Class) { |
Dan Gohman | fca43c2 | 2011-09-14 18:33:34 +0000 | [diff] [blame] | 319 | // objc_retainBlock is not nounwind because it calls user copy constructors |
| 320 | // which could theoretically throw. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 321 | return Class == IC_Retain || |
| 322 | Class == IC_RetainRV || |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 323 | Class == IC_Release || |
| 324 | Class == IC_Autorelease || |
| 325 | Class == IC_AutoreleaseRV || |
| 326 | Class == IC_AutoreleasepoolPush || |
| 327 | Class == IC_AutoreleasepoolPop; |
| 328 | } |
| 329 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 330 | /// \brief Erase the given instruction. |
| 331 | /// |
| 332 | /// Many ObjC calls return their argument verbatim, |
| 333 | /// so if it's such a call and the return value has users, replace them with the |
| 334 | /// argument value. |
| 335 | /// |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 336 | static void EraseInstruction(Instruction *CI) { |
| 337 | Value *OldArg = cast<CallInst>(CI)->getArgOperand(0); |
| 338 | |
| 339 | bool Unused = CI->use_empty(); |
| 340 | |
| 341 | if (!Unused) { |
| 342 | // Replace the return value with the argument. |
| 343 | assert(IsForwarding(GetBasicInstructionClass(CI)) && |
| 344 | "Can't delete non-forwarding instruction with users!"); |
| 345 | CI->replaceAllUsesWith(OldArg); |
| 346 | } |
| 347 | |
| 348 | CI->eraseFromParent(); |
| 349 | |
| 350 | if (Unused) |
| 351 | RecursivelyDeleteTriviallyDeadInstructions(OldArg); |
| 352 | } |
| 353 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 354 | /// \brief This is a wrapper around getUnderlyingObject which also knows how to |
| 355 | /// look through objc_retain and objc_autorelease calls, which we know to return |
| 356 | /// their argument verbatim. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 357 | static const Value *GetUnderlyingObjCPtr(const Value *V) { |
| 358 | for (;;) { |
| 359 | V = GetUnderlyingObject(V); |
| 360 | if (!IsForwarding(GetBasicInstructionClass(V))) |
| 361 | break; |
| 362 | V = cast<CallInst>(V)->getArgOperand(0); |
| 363 | } |
| 364 | |
| 365 | return V; |
| 366 | } |
| 367 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 368 | /// \brief This is a wrapper around Value::stripPointerCasts which also knows |
| 369 | /// how to look through objc_retain and objc_autorelease calls, which we know to |
| 370 | /// return their argument verbatim. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 371 | static const Value *StripPointerCastsAndObjCCalls(const Value *V) { |
| 372 | for (;;) { |
| 373 | V = V->stripPointerCasts(); |
| 374 | if (!IsForwarding(GetBasicInstructionClass(V))) |
| 375 | break; |
| 376 | V = cast<CallInst>(V)->getArgOperand(0); |
| 377 | } |
| 378 | return V; |
| 379 | } |
| 380 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 381 | /// \brief This is a wrapper around Value::stripPointerCasts which also knows |
| 382 | /// how to look through objc_retain and objc_autorelease calls, which we know to |
| 383 | /// return their argument verbatim. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 384 | static Value *StripPointerCastsAndObjCCalls(Value *V) { |
| 385 | for (;;) { |
| 386 | V = V->stripPointerCasts(); |
| 387 | if (!IsForwarding(GetBasicInstructionClass(V))) |
| 388 | break; |
| 389 | V = cast<CallInst>(V)->getArgOperand(0); |
| 390 | } |
| 391 | return V; |
| 392 | } |
| 393 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 394 | /// \brief Assuming the given instruction is one of the special calls such as |
| 395 | /// objc_retain or objc_release, return the argument value, stripped of no-op |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 396 | /// casts and forwarding calls. |
| 397 | static Value *GetObjCArg(Value *Inst) { |
| 398 | return StripPointerCastsAndObjCCalls(cast<CallInst>(Inst)->getArgOperand(0)); |
| 399 | } |
| 400 | |
Michael Gottesman | 87db3575 | 2013-01-18 23:02:45 +0000 | [diff] [blame] | 401 | /// \brief Return true if this value refers to a distinct and identifiable |
| 402 | /// object. |
| 403 | /// |
| 404 | /// This is similar to AliasAnalysis's isIdentifiedObject, except that it uses |
| 405 | /// special knowledge of ObjC conventions. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 406 | static bool IsObjCIdentifiedObject(const Value *V) { |
| 407 | // Assume that call results and arguments have their own "provenance". |
| 408 | // Constants (including GlobalVariables) and Allocas are never |
| 409 | // reference-counted. |
| 410 | if (isa<CallInst>(V) || isa<InvokeInst>(V) || |
| 411 | isa<Argument>(V) || isa<Constant>(V) || |
| 412 | isa<AllocaInst>(V)) |
| 413 | return true; |
| 414 | |
| 415 | if (const LoadInst *LI = dyn_cast<LoadInst>(V)) { |
| 416 | const Value *Pointer = |
| 417 | StripPointerCastsAndObjCCalls(LI->getPointerOperand()); |
| 418 | if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(Pointer)) { |
Dan Gohman | 56e1cef | 2011-08-22 17:29:11 +0000 | [diff] [blame] | 419 | // A constant pointer can't be pointing to an object on the heap. It may |
| 420 | // be reference-counted, but it won't be deleted. |
| 421 | if (GV->isConstant()) |
| 422 | return true; |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 423 | StringRef Name = GV->getName(); |
| 424 | // These special variables are known to hold values which are not |
| 425 | // reference-counted pointers. |
| 426 | if (Name.startswith("\01L_OBJC_SELECTOR_REFERENCES_") || |
| 427 | Name.startswith("\01L_OBJC_CLASSLIST_REFERENCES_") || |
| 428 | Name.startswith("\01L_OBJC_CLASSLIST_SUP_REFS_$_") || |
| 429 | Name.startswith("\01L_OBJC_METH_VAR_NAME_") || |
| 430 | Name.startswith("\01l_objc_msgSend_fixup_")) |
| 431 | return true; |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | return false; |
| 436 | } |
| 437 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 438 | /// \brief This is similar to StripPointerCastsAndObjCCalls but it stops as soon |
| 439 | /// as it finds a value with multiple uses. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 440 | static const Value *FindSingleUseIdentifiedObject(const Value *Arg) { |
| 441 | if (Arg->hasOneUse()) { |
| 442 | if (const BitCastInst *BC = dyn_cast<BitCastInst>(Arg)) |
| 443 | return FindSingleUseIdentifiedObject(BC->getOperand(0)); |
| 444 | if (const GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Arg)) |
| 445 | if (GEP->hasAllZeroIndices()) |
| 446 | return FindSingleUseIdentifiedObject(GEP->getPointerOperand()); |
| 447 | if (IsForwarding(GetBasicInstructionClass(Arg))) |
| 448 | return FindSingleUseIdentifiedObject( |
| 449 | cast<CallInst>(Arg)->getArgOperand(0)); |
| 450 | if (!IsObjCIdentifiedObject(Arg)) |
| 451 | return 0; |
| 452 | return Arg; |
| 453 | } |
| 454 | |
Dan Gohman | 41375a3 | 2012-05-08 23:39:44 +0000 | [diff] [blame] | 455 | // If we found an identifiable object but it has multiple uses, but they are |
| 456 | // trivial uses, we can still consider this to be a single-use value. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 457 | if (IsObjCIdentifiedObject(Arg)) { |
| 458 | for (Value::const_use_iterator UI = Arg->use_begin(), UE = Arg->use_end(); |
| 459 | UI != UE; ++UI) { |
| 460 | const User *U = *UI; |
| 461 | if (!U->use_empty() || StripPointerCastsAndObjCCalls(U) != Arg) |
| 462 | return 0; |
| 463 | } |
| 464 | |
| 465 | return Arg; |
| 466 | } |
| 467 | |
| 468 | return 0; |
| 469 | } |
| 470 | |
Michael Gottesman | 4385edf | 2013-01-14 01:47:53 +0000 | [diff] [blame] | 471 | /// \brief Test whether the given pointer, which is an Objective C block |
| 472 | /// pointer, does not "escape". |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 473 | /// |
| 474 | /// This differs from regular escape analysis in that a use as an |
| 475 | /// argument to a call is not considered an escape. |
| 476 | /// |
Dan Gohman | 728db49 | 2012-01-13 00:39:07 +0000 | [diff] [blame] | 477 | static bool DoesObjCBlockEscape(const Value *BlockPtr) { |
Michael Gottesman | 1a89fe5 | 2013-01-13 07:47:32 +0000 | [diff] [blame] | 478 | |
| 479 | DEBUG(dbgs() << "DoesObjCBlockEscape: Target: " << *BlockPtr << "\n"); |
| 480 | |
Dan Gohman | 728db49 | 2012-01-13 00:39:07 +0000 | [diff] [blame] | 481 | // Walk the def-use chains. |
| 482 | SmallVector<const Value *, 4> Worklist; |
| 483 | Worklist.push_back(BlockPtr); |
Michael Gottesman | f15c0bb | 2013-01-13 22:12:06 +0000 | [diff] [blame] | 484 | |
| 485 | // Ensure we do not visit any value twice. |
| 486 | SmallPtrSet<const Value *, 4> VisitedSet; |
| 487 | |
Dan Gohman | 728db49 | 2012-01-13 00:39:07 +0000 | [diff] [blame] | 488 | do { |
| 489 | const Value *V = Worklist.pop_back_val(); |
Michael Gottesman | 1a89fe5 | 2013-01-13 07:47:32 +0000 | [diff] [blame] | 490 | |
| 491 | DEBUG(dbgs() << "DoesObjCBlockEscape: Visiting: " << *V << "\n"); |
| 492 | |
Dan Gohman | 728db49 | 2012-01-13 00:39:07 +0000 | [diff] [blame] | 493 | for (Value::const_use_iterator UI = V->use_begin(), UE = V->use_end(); |
| 494 | UI != UE; ++UI) { |
| 495 | const User *UUser = *UI; |
Michael Gottesman | 1a89fe5 | 2013-01-13 07:47:32 +0000 | [diff] [blame] | 496 | |
| 497 | DEBUG(dbgs() << "DoesObjCBlockEscape: User: " << *UUser << "\n"); |
| 498 | |
Dan Gohman | 728db49 | 2012-01-13 00:39:07 +0000 | [diff] [blame] | 499 | // Special - Use by a call (callee or argument) is not considered |
| 500 | // to be an escape. |
Dan Gohman | e1e352a | 2012-04-13 18:28:58 +0000 | [diff] [blame] | 501 | switch (GetBasicInstructionClass(UUser)) { |
| 502 | case IC_StoreWeak: |
| 503 | case IC_InitWeak: |
| 504 | case IC_StoreStrong: |
| 505 | case IC_Autorelease: |
Michael Gottesman | 1a89fe5 | 2013-01-13 07:47:32 +0000 | [diff] [blame] | 506 | case IC_AutoreleaseRV: { |
| 507 | DEBUG(dbgs() << "DoesObjCBlockEscape: User copies pointer arguments. " |
| 508 | "Block Escapes!\n"); |
Dan Gohman | e1e352a | 2012-04-13 18:28:58 +0000 | [diff] [blame] | 509 | // These special functions make copies of their pointer arguments. |
| 510 | return true; |
Michael Gottesman | 1a89fe5 | 2013-01-13 07:47:32 +0000 | [diff] [blame] | 511 | } |
Dan Gohman | e1e352a | 2012-04-13 18:28:58 +0000 | [diff] [blame] | 512 | case IC_User: |
| 513 | case IC_None: |
| 514 | // Use by an instruction which copies the value is an escape if the |
| 515 | // result is an escape. |
| 516 | if (isa<BitCastInst>(UUser) || isa<GetElementPtrInst>(UUser) || |
| 517 | isa<PHINode>(UUser) || isa<SelectInst>(UUser)) { |
Michael Gottesman | f15c0bb | 2013-01-13 22:12:06 +0000 | [diff] [blame] | 518 | |
Michael Gottesman | e9145d3 | 2013-01-14 19:18:39 +0000 | [diff] [blame] | 519 | if (!VisitedSet.insert(UUser)) { |
Michael Gottesman | 4385edf | 2013-01-14 01:47:53 +0000 | [diff] [blame] | 520 | DEBUG(dbgs() << "DoesObjCBlockEscape: User copies value. Escapes " |
| 521 | "if result escapes. Adding to list.\n"); |
Michael Gottesman | f15c0bb | 2013-01-13 22:12:06 +0000 | [diff] [blame] | 522 | Worklist.push_back(UUser); |
| 523 | } else { |
| 524 | DEBUG(dbgs() << "DoesObjCBlockEscape: Already visited node.\n"); |
| 525 | } |
Dan Gohman | e1e352a | 2012-04-13 18:28:58 +0000 | [diff] [blame] | 526 | continue; |
| 527 | } |
| 528 | // Use by a load is not an escape. |
| 529 | if (isa<LoadInst>(UUser)) |
| 530 | continue; |
| 531 | // Use by a store is not an escape if the use is the address. |
| 532 | if (const StoreInst *SI = dyn_cast<StoreInst>(UUser)) |
| 533 | if (V != SI->getValueOperand()) |
| 534 | continue; |
| 535 | break; |
| 536 | default: |
| 537 | // Regular calls and other stuff are not considered escapes. |
Dan Gohman | 728db49 | 2012-01-13 00:39:07 +0000 | [diff] [blame] | 538 | continue; |
| 539 | } |
Dan Gohman | eb6e015 | 2012-02-13 22:57:02 +0000 | [diff] [blame] | 540 | // Otherwise, conservatively assume an escape. |
Michael Gottesman | 1a89fe5 | 2013-01-13 07:47:32 +0000 | [diff] [blame] | 541 | DEBUG(dbgs() << "DoesObjCBlockEscape: Assuming block escapes.\n"); |
Dan Gohman | 728db49 | 2012-01-13 00:39:07 +0000 | [diff] [blame] | 542 | return true; |
| 543 | } |
| 544 | } while (!Worklist.empty()); |
| 545 | |
| 546 | // No escapes found. |
Michael Gottesman | 1a89fe5 | 2013-01-13 07:47:32 +0000 | [diff] [blame] | 547 | DEBUG(dbgs() << "DoesObjCBlockEscape: Block does not escape.\n"); |
Dan Gohman | 728db49 | 2012-01-13 00:39:07 +0000 | [diff] [blame] | 548 | return false; |
| 549 | } |
| 550 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 551 | /// @} |
| 552 | /// |
Michael Gottesman | 4385edf | 2013-01-14 01:47:53 +0000 | [diff] [blame] | 553 | /// \defgroup ARCAA Extends alias analysis using ObjC specific knowledge. |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 554 | /// @{ |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 555 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 556 | namespace { |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 557 | /// \brief This is a simple alias analysis implementation that uses knowledge |
| 558 | /// of ARC constructs to answer queries. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 559 | /// |
| 560 | /// TODO: This class could be generalized to know about other ObjC-specific |
| 561 | /// tricks. Such as knowing that ivars in the non-fragile ABI are non-aliasing |
| 562 | /// even though their offsets are dynamic. |
| 563 | class ObjCARCAliasAnalysis : public ImmutablePass, |
| 564 | public AliasAnalysis { |
| 565 | public: |
| 566 | static char ID; // Class identification, replacement for typeinfo |
| 567 | ObjCARCAliasAnalysis() : ImmutablePass(ID) { |
| 568 | initializeObjCARCAliasAnalysisPass(*PassRegistry::getPassRegistry()); |
| 569 | } |
| 570 | |
| 571 | private: |
| 572 | virtual void initializePass() { |
| 573 | InitializeAliasAnalysis(this); |
| 574 | } |
| 575 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 576 | /// This method is used when a pass implements an analysis interface through |
| 577 | /// multiple inheritance. If needed, it should override this to adjust the |
| 578 | /// this pointer as needed for the specified pass info. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 579 | virtual void *getAdjustedAnalysisPointer(const void *PI) { |
| 580 | if (PI == &AliasAnalysis::ID) |
Dan Gohman | dae3349 | 2012-04-27 18:56:31 +0000 | [diff] [blame] | 581 | return static_cast<AliasAnalysis *>(this); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 582 | return this; |
| 583 | } |
| 584 | |
| 585 | virtual void getAnalysisUsage(AnalysisUsage &AU) const; |
| 586 | virtual AliasResult alias(const Location &LocA, const Location &LocB); |
| 587 | virtual bool pointsToConstantMemory(const Location &Loc, bool OrLocal); |
| 588 | virtual ModRefBehavior getModRefBehavior(ImmutableCallSite CS); |
| 589 | virtual ModRefBehavior getModRefBehavior(const Function *F); |
| 590 | virtual ModRefResult getModRefInfo(ImmutableCallSite CS, |
| 591 | const Location &Loc); |
| 592 | virtual ModRefResult getModRefInfo(ImmutableCallSite CS1, |
| 593 | ImmutableCallSite CS2); |
| 594 | }; |
| 595 | } // End of anonymous namespace |
| 596 | |
| 597 | // Register this pass... |
| 598 | char ObjCARCAliasAnalysis::ID = 0; |
| 599 | INITIALIZE_AG_PASS(ObjCARCAliasAnalysis, AliasAnalysis, "objc-arc-aa", |
| 600 | "ObjC-ARC-Based Alias Analysis", false, true, false) |
| 601 | |
| 602 | ImmutablePass *llvm::createObjCARCAliasAnalysisPass() { |
| 603 | return new ObjCARCAliasAnalysis(); |
| 604 | } |
| 605 | |
| 606 | void |
| 607 | ObjCARCAliasAnalysis::getAnalysisUsage(AnalysisUsage &AU) const { |
| 608 | AU.setPreservesAll(); |
| 609 | AliasAnalysis::getAnalysisUsage(AU); |
| 610 | } |
| 611 | |
| 612 | AliasAnalysis::AliasResult |
| 613 | ObjCARCAliasAnalysis::alias(const Location &LocA, const Location &LocB) { |
| 614 | if (!EnableARCOpts) |
| 615 | return AliasAnalysis::alias(LocA, LocB); |
| 616 | |
| 617 | // First, strip off no-ops, including ObjC-specific no-ops, and try making a |
| 618 | // precise alias query. |
| 619 | const Value *SA = StripPointerCastsAndObjCCalls(LocA.Ptr); |
| 620 | const Value *SB = StripPointerCastsAndObjCCalls(LocB.Ptr); |
| 621 | AliasResult Result = |
| 622 | AliasAnalysis::alias(Location(SA, LocA.Size, LocA.TBAATag), |
| 623 | Location(SB, LocB.Size, LocB.TBAATag)); |
| 624 | if (Result != MayAlias) |
| 625 | return Result; |
| 626 | |
| 627 | // If that failed, climb to the underlying object, including climbing through |
| 628 | // ObjC-specific no-ops, and try making an imprecise alias query. |
| 629 | const Value *UA = GetUnderlyingObjCPtr(SA); |
| 630 | const Value *UB = GetUnderlyingObjCPtr(SB); |
| 631 | if (UA != SA || UB != SB) { |
| 632 | Result = AliasAnalysis::alias(Location(UA), Location(UB)); |
| 633 | // We can't use MustAlias or PartialAlias results here because |
| 634 | // GetUnderlyingObjCPtr may return an offsetted pointer value. |
| 635 | if (Result == NoAlias) |
| 636 | return NoAlias; |
| 637 | } |
| 638 | |
| 639 | // If that failed, fail. We don't need to chain here, since that's covered |
| 640 | // by the earlier precise query. |
| 641 | return MayAlias; |
| 642 | } |
| 643 | |
| 644 | bool |
| 645 | ObjCARCAliasAnalysis::pointsToConstantMemory(const Location &Loc, |
| 646 | bool OrLocal) { |
| 647 | if (!EnableARCOpts) |
| 648 | return AliasAnalysis::pointsToConstantMemory(Loc, OrLocal); |
| 649 | |
| 650 | // First, strip off no-ops, including ObjC-specific no-ops, and try making |
| 651 | // a precise alias query. |
| 652 | const Value *S = StripPointerCastsAndObjCCalls(Loc.Ptr); |
| 653 | if (AliasAnalysis::pointsToConstantMemory(Location(S, Loc.Size, Loc.TBAATag), |
| 654 | OrLocal)) |
| 655 | return true; |
| 656 | |
| 657 | // If that failed, climb to the underlying object, including climbing through |
| 658 | // ObjC-specific no-ops, and try making an imprecise alias query. |
| 659 | const Value *U = GetUnderlyingObjCPtr(S); |
| 660 | if (U != S) |
| 661 | return AliasAnalysis::pointsToConstantMemory(Location(U), OrLocal); |
| 662 | |
| 663 | // If that failed, fail. We don't need to chain here, since that's covered |
| 664 | // by the earlier precise query. |
| 665 | return false; |
| 666 | } |
| 667 | |
| 668 | AliasAnalysis::ModRefBehavior |
| 669 | ObjCARCAliasAnalysis::getModRefBehavior(ImmutableCallSite CS) { |
| 670 | // We have nothing to do. Just chain to the next AliasAnalysis. |
| 671 | return AliasAnalysis::getModRefBehavior(CS); |
| 672 | } |
| 673 | |
| 674 | AliasAnalysis::ModRefBehavior |
| 675 | ObjCARCAliasAnalysis::getModRefBehavior(const Function *F) { |
| 676 | if (!EnableARCOpts) |
| 677 | return AliasAnalysis::getModRefBehavior(F); |
| 678 | |
| 679 | switch (GetFunctionClass(F)) { |
| 680 | case IC_NoopCast: |
| 681 | return DoesNotAccessMemory; |
| 682 | default: |
| 683 | break; |
| 684 | } |
| 685 | |
| 686 | return AliasAnalysis::getModRefBehavior(F); |
| 687 | } |
| 688 | |
| 689 | AliasAnalysis::ModRefResult |
| 690 | ObjCARCAliasAnalysis::getModRefInfo(ImmutableCallSite CS, const Location &Loc) { |
| 691 | if (!EnableARCOpts) |
| 692 | return AliasAnalysis::getModRefInfo(CS, Loc); |
| 693 | |
| 694 | switch (GetBasicInstructionClass(CS.getInstruction())) { |
| 695 | case IC_Retain: |
| 696 | case IC_RetainRV: |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 697 | case IC_Autorelease: |
| 698 | case IC_AutoreleaseRV: |
| 699 | case IC_NoopCast: |
| 700 | case IC_AutoreleasepoolPush: |
| 701 | case IC_FusedRetainAutorelease: |
| 702 | case IC_FusedRetainAutoreleaseRV: |
| 703 | // These functions don't access any memory visible to the compiler. |
Benjamin Kramer | bde9176 | 2012-06-02 10:20:22 +0000 | [diff] [blame] | 704 | // Note that this doesn't include objc_retainBlock, because it updates |
Dan Gohman | d4b5e3a | 2011-09-14 18:13:00 +0000 | [diff] [blame] | 705 | // pointers when it copies block data. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 706 | return NoModRef; |
| 707 | default: |
| 708 | break; |
| 709 | } |
| 710 | |
| 711 | return AliasAnalysis::getModRefInfo(CS, Loc); |
| 712 | } |
| 713 | |
| 714 | AliasAnalysis::ModRefResult |
| 715 | ObjCARCAliasAnalysis::getModRefInfo(ImmutableCallSite CS1, |
| 716 | ImmutableCallSite CS2) { |
| 717 | // TODO: Theoretically we could check for dependencies between objc_* calls |
| 718 | // and OnlyAccessesArgumentPointees calls or other well-behaved calls. |
| 719 | return AliasAnalysis::getModRefInfo(CS1, CS2); |
| 720 | } |
| 721 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 722 | /// @} |
| 723 | /// |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 724 | /// \defgroup ARCAPElim ARC Autorelease Pool Elimination. |
| 725 | /// @{ |
Dan Gohman | e7a243f | 2012-01-17 20:52:24 +0000 | [diff] [blame] | 726 | |
Dan Gohman | 41375a3 | 2012-05-08 23:39:44 +0000 | [diff] [blame] | 727 | #include "llvm/ADT/STLExtras.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 728 | #include "llvm/IR/Constants.h" |
Dan Gohman | 82041c2 | 2012-01-18 21:19:38 +0000 | [diff] [blame] | 729 | |
Dan Gohman | e7a243f | 2012-01-17 20:52:24 +0000 | [diff] [blame] | 730 | namespace { |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 731 | /// \brief Autorelease pool elimination. |
Dan Gohman | e7a243f | 2012-01-17 20:52:24 +0000 | [diff] [blame] | 732 | class ObjCARCAPElim : public ModulePass { |
| 733 | virtual void getAnalysisUsage(AnalysisUsage &AU) const; |
| 734 | virtual bool runOnModule(Module &M); |
| 735 | |
Dan Gohman | dae3349 | 2012-04-27 18:56:31 +0000 | [diff] [blame] | 736 | static bool MayAutorelease(ImmutableCallSite CS, unsigned Depth = 0); |
| 737 | static bool OptimizeBB(BasicBlock *BB); |
Dan Gohman | e7a243f | 2012-01-17 20:52:24 +0000 | [diff] [blame] | 738 | |
| 739 | public: |
| 740 | static char ID; |
| 741 | ObjCARCAPElim() : ModulePass(ID) { |
| 742 | initializeObjCARCAPElimPass(*PassRegistry::getPassRegistry()); |
| 743 | } |
| 744 | }; |
| 745 | } |
| 746 | |
| 747 | char ObjCARCAPElim::ID = 0; |
| 748 | INITIALIZE_PASS(ObjCARCAPElim, |
| 749 | "objc-arc-apelim", |
| 750 | "ObjC ARC autorelease pool elimination", |
| 751 | false, false) |
| 752 | |
| 753 | Pass *llvm::createObjCARCAPElimPass() { |
| 754 | return new ObjCARCAPElim(); |
| 755 | } |
| 756 | |
| 757 | void ObjCARCAPElim::getAnalysisUsage(AnalysisUsage &AU) const { |
| 758 | AU.setPreservesCFG(); |
| 759 | } |
| 760 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 761 | /// Interprocedurally determine if calls made by the given call site can |
| 762 | /// possibly produce autoreleases. |
Dan Gohman | dae3349 | 2012-04-27 18:56:31 +0000 | [diff] [blame] | 763 | bool ObjCARCAPElim::MayAutorelease(ImmutableCallSite CS, unsigned Depth) { |
| 764 | if (const Function *Callee = CS.getCalledFunction()) { |
Dan Gohman | e7a243f | 2012-01-17 20:52:24 +0000 | [diff] [blame] | 765 | if (Callee->isDeclaration() || Callee->mayBeOverridden()) |
| 766 | return true; |
Dan Gohman | dae3349 | 2012-04-27 18:56:31 +0000 | [diff] [blame] | 767 | for (Function::const_iterator I = Callee->begin(), E = Callee->end(); |
Dan Gohman | e7a243f | 2012-01-17 20:52:24 +0000 | [diff] [blame] | 768 | I != E; ++I) { |
Dan Gohman | dae3349 | 2012-04-27 18:56:31 +0000 | [diff] [blame] | 769 | const BasicBlock *BB = I; |
| 770 | for (BasicBlock::const_iterator J = BB->begin(), F = BB->end(); |
| 771 | J != F; ++J) |
| 772 | if (ImmutableCallSite JCS = ImmutableCallSite(J)) |
Dan Gohman | 8f12fae | 2012-01-18 21:24:45 +0000 | [diff] [blame] | 773 | // This recursion depth limit is arbitrary. It's just great |
| 774 | // enough to cover known interesting testcases. |
| 775 | if (Depth < 3 && |
| 776 | !JCS.onlyReadsMemory() && |
| 777 | MayAutorelease(JCS, Depth + 1)) |
Dan Gohman | e7a243f | 2012-01-17 20:52:24 +0000 | [diff] [blame] | 778 | return true; |
| 779 | } |
| 780 | return false; |
| 781 | } |
| 782 | |
| 783 | return true; |
| 784 | } |
| 785 | |
| 786 | bool ObjCARCAPElim::OptimizeBB(BasicBlock *BB) { |
| 787 | bool Changed = false; |
| 788 | |
| 789 | Instruction *Push = 0; |
| 790 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) { |
| 791 | Instruction *Inst = I++; |
| 792 | switch (GetBasicInstructionClass(Inst)) { |
| 793 | case IC_AutoreleasepoolPush: |
| 794 | Push = Inst; |
| 795 | break; |
| 796 | case IC_AutoreleasepoolPop: |
| 797 | // If this pop matches a push and nothing in between can autorelease, |
| 798 | // zap the pair. |
| 799 | if (Push && cast<CallInst>(Inst)->getArgOperand(0) == Push) { |
| 800 | Changed = true; |
Michael Gottesman | 4385edf | 2013-01-14 01:47:53 +0000 | [diff] [blame] | 801 | DEBUG(dbgs() << "ObjCARCAPElim::OptimizeBB: Zapping push pop " |
| 802 | "autorelease pair:\n" |
| 803 | " Pop: " << *Inst << "\n" |
Michael Gottesman | ef682c5 | 2013-01-03 08:09:17 +0000 | [diff] [blame] | 804 | << " Push: " << *Push << "\n"); |
Dan Gohman | e7a243f | 2012-01-17 20:52:24 +0000 | [diff] [blame] | 805 | Inst->eraseFromParent(); |
| 806 | Push->eraseFromParent(); |
| 807 | } |
| 808 | Push = 0; |
| 809 | break; |
| 810 | case IC_CallOrUser: |
Dan Gohman | dae3349 | 2012-04-27 18:56:31 +0000 | [diff] [blame] | 811 | if (MayAutorelease(ImmutableCallSite(Inst))) |
Dan Gohman | e7a243f | 2012-01-17 20:52:24 +0000 | [diff] [blame] | 812 | Push = 0; |
| 813 | break; |
| 814 | default: |
| 815 | break; |
| 816 | } |
| 817 | } |
| 818 | |
| 819 | return Changed; |
| 820 | } |
| 821 | |
| 822 | bool ObjCARCAPElim::runOnModule(Module &M) { |
| 823 | if (!EnableARCOpts) |
| 824 | return false; |
| 825 | |
| 826 | // If nothing in the Module uses ARC, don't do anything. |
| 827 | if (!ModuleHasARC(M)) |
| 828 | return false; |
| 829 | |
Dan Gohman | 82041c2 | 2012-01-18 21:19:38 +0000 | [diff] [blame] | 830 | // Find the llvm.global_ctors variable, as the first step in |
Dan Gohman | 670f937 | 2012-04-13 18:57:48 +0000 | [diff] [blame] | 831 | // identifying the global constructors. In theory, unnecessary autorelease |
| 832 | // pools could occur anywhere, but in practice it's pretty rare. Global |
| 833 | // ctors are a place where autorelease pools get inserted automatically, |
| 834 | // so it's pretty common for them to be unnecessary, and it's pretty |
| 835 | // profitable to eliminate them. |
Dan Gohman | 82041c2 | 2012-01-18 21:19:38 +0000 | [diff] [blame] | 836 | GlobalVariable *GV = M.getGlobalVariable("llvm.global_ctors"); |
| 837 | if (!GV) |
| 838 | return false; |
| 839 | |
| 840 | assert(GV->hasDefinitiveInitializer() && |
| 841 | "llvm.global_ctors is uncooperative!"); |
| 842 | |
Dan Gohman | e7a243f | 2012-01-17 20:52:24 +0000 | [diff] [blame] | 843 | bool Changed = false; |
| 844 | |
Dan Gohman | 82041c2 | 2012-01-18 21:19:38 +0000 | [diff] [blame] | 845 | // Dig the constructor functions out of GV's initializer. |
| 846 | ConstantArray *Init = cast<ConstantArray>(GV->getInitializer()); |
| 847 | for (User::op_iterator OI = Init->op_begin(), OE = Init->op_end(); |
| 848 | OI != OE; ++OI) { |
| 849 | Value *Op = *OI; |
| 850 | // llvm.global_ctors is an array of pairs where the second members |
| 851 | // are constructor functions. |
Dan Gohman | 22fbe8d | 2012-04-18 22:24:33 +0000 | [diff] [blame] | 852 | Function *F = dyn_cast<Function>(cast<ConstantStruct>(Op)->getOperand(1)); |
| 853 | // If the user used a constructor function with the wrong signature and |
| 854 | // it got bitcasted or whatever, look the other way. |
| 855 | if (!F) |
| 856 | continue; |
Dan Gohman | e7a243f | 2012-01-17 20:52:24 +0000 | [diff] [blame] | 857 | // Only look at function definitions. |
| 858 | if (F->isDeclaration()) |
| 859 | continue; |
Dan Gohman | e7a243f | 2012-01-17 20:52:24 +0000 | [diff] [blame] | 860 | // Only look at functions with one basic block. |
| 861 | if (llvm::next(F->begin()) != F->end()) |
| 862 | continue; |
| 863 | // Ok, a single-block constructor function definition. Try to optimize it. |
| 864 | Changed |= OptimizeBB(F->begin()); |
| 865 | } |
| 866 | |
| 867 | return Changed; |
| 868 | } |
| 869 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 870 | /// @} |
| 871 | /// |
| 872 | /// \defgroup ARCOpt ARC Optimization. |
| 873 | /// @{ |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 874 | |
| 875 | // TODO: On code like this: |
| 876 | // |
| 877 | // objc_retain(%x) |
| 878 | // stuff_that_cannot_release() |
| 879 | // objc_autorelease(%x) |
| 880 | // stuff_that_cannot_release() |
| 881 | // objc_retain(%x) |
| 882 | // stuff_that_cannot_release() |
| 883 | // objc_autorelease(%x) |
| 884 | // |
| 885 | // The second retain and autorelease can be deleted. |
| 886 | |
| 887 | // TODO: It should be possible to delete |
| 888 | // objc_autoreleasePoolPush and objc_autoreleasePoolPop |
| 889 | // pairs if nothing is actually autoreleased between them. Also, autorelease |
| 890 | // calls followed by objc_autoreleasePoolPop calls (perhaps in ObjC++ code |
| 891 | // after inlining) can be turned into plain release calls. |
| 892 | |
| 893 | // TODO: Critical-edge splitting. If the optimial insertion point is |
| 894 | // a critical edge, the current algorithm has to fail, because it doesn't |
| 895 | // know how to split edges. It should be possible to make the optimizer |
| 896 | // think in terms of edges, rather than blocks, and then split critical |
| 897 | // edges on demand. |
| 898 | |
| 899 | // TODO: OptimizeSequences could generalized to be Interprocedural. |
| 900 | |
| 901 | // TODO: Recognize that a bunch of other objc runtime calls have |
| 902 | // non-escaping arguments and non-releasing arguments, and may be |
| 903 | // non-autoreleasing. |
| 904 | |
| 905 | // TODO: Sink autorelease calls as far as possible. Unfortunately we |
| 906 | // usually can't sink them past other calls, which would be the main |
| 907 | // case where it would be useful. |
| 908 | |
Dan Gohman | b389401 | 2011-08-19 00:26:36 +0000 | [diff] [blame] | 909 | // TODO: The pointer returned from objc_loadWeakRetained is retained. |
| 910 | |
| 911 | // TODO: Delete release+retain pairs (rare). |
Dan Gohman | ceaac7c | 2011-06-20 23:20:43 +0000 | [diff] [blame] | 912 | |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 913 | #include "llvm/ADT/SmallPtrSet.h" |
| 914 | #include "llvm/ADT/Statistic.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 915 | #include "llvm/IR/LLVMContext.h" |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 916 | #include "llvm/Support/CFG.h" |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 917 | |
| 918 | STATISTIC(NumNoops, "Number of no-op objc calls eliminated"); |
| 919 | STATISTIC(NumPartialNoops, "Number of partially no-op objc calls eliminated"); |
| 920 | STATISTIC(NumAutoreleases,"Number of autoreleases converted to releases"); |
| 921 | STATISTIC(NumRets, "Number of return value forwarding " |
| 922 | "retain+autoreleaes eliminated"); |
| 923 | STATISTIC(NumRRs, "Number of retain+release paths eliminated"); |
| 924 | STATISTIC(NumPeeps, "Number of calls peephole-optimized"); |
| 925 | |
| 926 | namespace { |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 927 | /// \brief This is similar to BasicAliasAnalysis, and it uses many of the same |
| 928 | /// techniques, except it uses special ObjC-specific reasoning about pointer |
| 929 | /// relationships. |
Michael Gottesman | 12780c2d | 2013-01-24 21:35:00 +0000 | [diff] [blame] | 930 | /// |
| 931 | /// In this context ``Provenance'' is defined as the history of an object's |
| 932 | /// ownership. Thus ``Provenance Analysis'' is defined by using the notion of |
| 933 | /// an ``independent provenance source'' of a pointer to determine whether or |
| 934 | /// not two pointers have the same provenance source and thus could |
| 935 | /// potentially be related. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 936 | class ProvenanceAnalysis { |
| 937 | AliasAnalysis *AA; |
| 938 | |
| 939 | typedef std::pair<const Value *, const Value *> ValuePairTy; |
| 940 | typedef DenseMap<ValuePairTy, bool> CachedResultsTy; |
| 941 | CachedResultsTy CachedResults; |
| 942 | |
| 943 | bool relatedCheck(const Value *A, const Value *B); |
| 944 | bool relatedSelect(const SelectInst *A, const Value *B); |
| 945 | bool relatedPHI(const PHINode *A, const Value *B); |
| 946 | |
Craig Topper | b1d83e8 | 2012-09-18 02:01:41 +0000 | [diff] [blame] | 947 | void operator=(const ProvenanceAnalysis &) LLVM_DELETED_FUNCTION; |
| 948 | ProvenanceAnalysis(const ProvenanceAnalysis &) LLVM_DELETED_FUNCTION; |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 949 | |
| 950 | public: |
| 951 | ProvenanceAnalysis() {} |
| 952 | |
| 953 | void setAA(AliasAnalysis *aa) { AA = aa; } |
| 954 | |
| 955 | AliasAnalysis *getAA() const { return AA; } |
| 956 | |
| 957 | bool related(const Value *A, const Value *B); |
| 958 | |
| 959 | void clear() { |
| 960 | CachedResults.clear(); |
| 961 | } |
| 962 | }; |
| 963 | } |
| 964 | |
| 965 | bool ProvenanceAnalysis::relatedSelect(const SelectInst *A, const Value *B) { |
| 966 | // If the values are Selects with the same condition, we can do a more precise |
| 967 | // check: just check for relations between the values on corresponding arms. |
| 968 | if (const SelectInst *SB = dyn_cast<SelectInst>(B)) |
Dan Gohman | dae3349 | 2012-04-27 18:56:31 +0000 | [diff] [blame] | 969 | if (A->getCondition() == SB->getCondition()) |
| 970 | return related(A->getTrueValue(), SB->getTrueValue()) || |
| 971 | related(A->getFalseValue(), SB->getFalseValue()); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 972 | |
| 973 | // Check both arms of the Select node individually. |
Dan Gohman | dae3349 | 2012-04-27 18:56:31 +0000 | [diff] [blame] | 974 | return related(A->getTrueValue(), B) || |
| 975 | related(A->getFalseValue(), B); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 976 | } |
| 977 | |
| 978 | bool ProvenanceAnalysis::relatedPHI(const PHINode *A, const Value *B) { |
| 979 | // If the values are PHIs in the same block, we can do a more precise as well |
| 980 | // as efficient check: just check for relations between the values on |
| 981 | // corresponding edges. |
| 982 | if (const PHINode *PNB = dyn_cast<PHINode>(B)) |
| 983 | if (PNB->getParent() == A->getParent()) { |
| 984 | for (unsigned i = 0, e = A->getNumIncomingValues(); i != e; ++i) |
| 985 | if (related(A->getIncomingValue(i), |
| 986 | PNB->getIncomingValueForBlock(A->getIncomingBlock(i)))) |
| 987 | return true; |
| 988 | return false; |
| 989 | } |
| 990 | |
| 991 | // Check each unique source of the PHI node against B. |
| 992 | SmallPtrSet<const Value *, 4> UniqueSrc; |
| 993 | for (unsigned i = 0, e = A->getNumIncomingValues(); i != e; ++i) { |
| 994 | const Value *PV1 = A->getIncomingValue(i); |
| 995 | if (UniqueSrc.insert(PV1) && related(PV1, B)) |
| 996 | return true; |
| 997 | } |
| 998 | |
| 999 | // All of the arms checked out. |
| 1000 | return false; |
| 1001 | } |
| 1002 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 1003 | /// Test if the value of P, or any value covered by its provenance, is ever |
| 1004 | /// stored within the function (not counting callees). |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1005 | static bool isStoredObjCPointer(const Value *P) { |
| 1006 | SmallPtrSet<const Value *, 8> Visited; |
| 1007 | SmallVector<const Value *, 8> Worklist; |
| 1008 | Worklist.push_back(P); |
| 1009 | Visited.insert(P); |
| 1010 | do { |
| 1011 | P = Worklist.pop_back_val(); |
| 1012 | for (Value::const_use_iterator UI = P->use_begin(), UE = P->use_end(); |
| 1013 | UI != UE; ++UI) { |
| 1014 | const User *Ur = *UI; |
| 1015 | if (isa<StoreInst>(Ur)) { |
| 1016 | if (UI.getOperandNo() == 0) |
| 1017 | // The pointer is stored. |
| 1018 | return true; |
| 1019 | // The pointed is stored through. |
| 1020 | continue; |
| 1021 | } |
| 1022 | if (isa<CallInst>(Ur)) |
| 1023 | // The pointer is passed as an argument, ignore this. |
| 1024 | continue; |
| 1025 | if (isa<PtrToIntInst>(P)) |
| 1026 | // Assume the worst. |
| 1027 | return true; |
| 1028 | if (Visited.insert(Ur)) |
| 1029 | Worklist.push_back(Ur); |
| 1030 | } |
| 1031 | } while (!Worklist.empty()); |
| 1032 | |
| 1033 | // Everything checked out. |
| 1034 | return false; |
| 1035 | } |
| 1036 | |
| 1037 | bool ProvenanceAnalysis::relatedCheck(const Value *A, const Value *B) { |
| 1038 | // Skip past provenance pass-throughs. |
| 1039 | A = GetUnderlyingObjCPtr(A); |
| 1040 | B = GetUnderlyingObjCPtr(B); |
| 1041 | |
| 1042 | // Quick check. |
| 1043 | if (A == B) |
| 1044 | return true; |
| 1045 | |
| 1046 | // Ask regular AliasAnalysis, for a first approximation. |
| 1047 | switch (AA->alias(A, B)) { |
| 1048 | case AliasAnalysis::NoAlias: |
| 1049 | return false; |
| 1050 | case AliasAnalysis::MustAlias: |
| 1051 | case AliasAnalysis::PartialAlias: |
| 1052 | return true; |
| 1053 | case AliasAnalysis::MayAlias: |
| 1054 | break; |
| 1055 | } |
| 1056 | |
| 1057 | bool AIsIdentified = IsObjCIdentifiedObject(A); |
| 1058 | bool BIsIdentified = IsObjCIdentifiedObject(B); |
| 1059 | |
| 1060 | // An ObjC-Identified object can't alias a load if it is never locally stored. |
| 1061 | if (AIsIdentified) { |
Dan Gohman | df476e5 | 2012-09-04 23:16:20 +0000 | [diff] [blame] | 1062 | // Check for an obvious escape. |
| 1063 | if (isa<LoadInst>(B)) |
| 1064 | return isStoredObjCPointer(A); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1065 | if (BIsIdentified) { |
Dan Gohman | df476e5 | 2012-09-04 23:16:20 +0000 | [diff] [blame] | 1066 | // Check for an obvious escape. |
| 1067 | if (isa<LoadInst>(A)) |
| 1068 | return isStoredObjCPointer(B); |
| 1069 | // Both pointers are identified and escapes aren't an evident problem. |
| 1070 | return false; |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1071 | } |
Dan Gohman | df476e5 | 2012-09-04 23:16:20 +0000 | [diff] [blame] | 1072 | } else if (BIsIdentified) { |
| 1073 | // Check for an obvious escape. |
| 1074 | if (isa<LoadInst>(A)) |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1075 | return isStoredObjCPointer(B); |
| 1076 | } |
| 1077 | |
| 1078 | // Special handling for PHI and Select. |
| 1079 | if (const PHINode *PN = dyn_cast<PHINode>(A)) |
| 1080 | return relatedPHI(PN, B); |
| 1081 | if (const PHINode *PN = dyn_cast<PHINode>(B)) |
| 1082 | return relatedPHI(PN, A); |
| 1083 | if (const SelectInst *S = dyn_cast<SelectInst>(A)) |
| 1084 | return relatedSelect(S, B); |
| 1085 | if (const SelectInst *S = dyn_cast<SelectInst>(B)) |
| 1086 | return relatedSelect(S, A); |
| 1087 | |
| 1088 | // Conservative. |
| 1089 | return true; |
| 1090 | } |
| 1091 | |
| 1092 | bool ProvenanceAnalysis::related(const Value *A, const Value *B) { |
| 1093 | // Begin by inserting a conservative value into the map. If the insertion |
| 1094 | // fails, we have the answer already. If it succeeds, leave it there until we |
| 1095 | // compute the real answer to guard against recursive queries. |
| 1096 | if (A > B) std::swap(A, B); |
| 1097 | std::pair<CachedResultsTy::iterator, bool> Pair = |
| 1098 | CachedResults.insert(std::make_pair(ValuePairTy(A, B), true)); |
| 1099 | if (!Pair.second) |
| 1100 | return Pair.first->second; |
| 1101 | |
| 1102 | bool Result = relatedCheck(A, B); |
| 1103 | CachedResults[ValuePairTy(A, B)] = Result; |
| 1104 | return Result; |
| 1105 | } |
| 1106 | |
| 1107 | namespace { |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 1108 | /// \enum Sequence |
| 1109 | /// |
| 1110 | /// \brief A sequence of states that a pointer may go through in which an |
| 1111 | /// objc_retain and objc_release are actually needed. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1112 | enum Sequence { |
| 1113 | S_None, |
| 1114 | S_Retain, ///< objc_retain(x) |
| 1115 | S_CanRelease, ///< foo(x) -- x could possibly see a ref count decrement |
| 1116 | S_Use, ///< any use of x |
| 1117 | S_Stop, ///< like S_Release, but code motion is stopped |
| 1118 | S_Release, ///< objc_release(x) |
| 1119 | S_MovableRelease ///< objc_release(x), !clang.imprecise_release |
| 1120 | }; |
| 1121 | } |
| 1122 | |
| 1123 | static Sequence MergeSeqs(Sequence A, Sequence B, bool TopDown) { |
| 1124 | // The easy cases. |
| 1125 | if (A == B) |
| 1126 | return A; |
| 1127 | if (A == S_None || B == S_None) |
| 1128 | return S_None; |
| 1129 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1130 | if (A > B) std::swap(A, B); |
| 1131 | if (TopDown) { |
| 1132 | // Choose the side which is further along in the sequence. |
Dan Gohman | 1213027 | 2011-08-12 00:26:31 +0000 | [diff] [blame] | 1133 | if ((A == S_Retain || A == S_CanRelease) && |
| 1134 | (B == S_CanRelease || B == S_Use)) |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1135 | return B; |
| 1136 | } else { |
| 1137 | // Choose the side which is further along in the sequence. |
| 1138 | if ((A == S_Use || A == S_CanRelease) && |
Dan Gohman | 1213027 | 2011-08-12 00:26:31 +0000 | [diff] [blame] | 1139 | (B == S_Use || B == S_Release || B == S_Stop || B == S_MovableRelease)) |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1140 | return A; |
| 1141 | // If both sides are releases, choose the more conservative one. |
| 1142 | if (A == S_Stop && (B == S_Release || B == S_MovableRelease)) |
| 1143 | return A; |
| 1144 | if (A == S_Release && B == S_MovableRelease) |
| 1145 | return A; |
| 1146 | } |
| 1147 | |
| 1148 | return S_None; |
| 1149 | } |
| 1150 | |
| 1151 | namespace { |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 1152 | /// \brief Unidirectional information about either a |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1153 | /// retain-decrement-use-release sequence or release-use-decrement-retain |
| 1154 | /// reverese sequence. |
| 1155 | struct RRInfo { |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 1156 | /// After an objc_retain, the reference count of the referenced |
Dan Gohman | b389401 | 2011-08-19 00:26:36 +0000 | [diff] [blame] | 1157 | /// object is known to be positive. Similarly, before an objc_release, the |
| 1158 | /// reference count of the referenced object is known to be positive. If |
| 1159 | /// there are retain-release pairs in code regions where the retain count |
| 1160 | /// is known to be positive, they can be eliminated, regardless of any side |
| 1161 | /// effects between them. |
| 1162 | /// |
| 1163 | /// Also, a retain+release pair nested within another retain+release |
| 1164 | /// pair all on the known same pointer value can be eliminated, regardless |
| 1165 | /// of any intervening side effects. |
| 1166 | /// |
| 1167 | /// KnownSafe is true when either of these conditions is satisfied. |
| 1168 | bool KnownSafe; |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1169 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 1170 | /// True if the Calls are objc_retainBlock calls (as opposed to objc_retain |
| 1171 | /// calls). |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1172 | bool IsRetainBlock; |
| 1173 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 1174 | /// True of the objc_release calls are all marked with the "tail" keyword. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1175 | bool IsTailCallRelease; |
| 1176 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 1177 | /// If the Calls are objc_release calls and they all have a |
| 1178 | /// clang.imprecise_release tag, this is the metadata tag. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1179 | MDNode *ReleaseMetadata; |
| 1180 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 1181 | /// For a top-down sequence, the set of objc_retains or |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1182 | /// objc_retainBlocks. For bottom-up, the set of objc_releases. |
| 1183 | SmallPtrSet<Instruction *, 2> Calls; |
| 1184 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 1185 | /// The set of optimal insert positions for moving calls in the opposite |
| 1186 | /// sequence. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1187 | SmallPtrSet<Instruction *, 2> ReverseInsertPts; |
| 1188 | |
| 1189 | RRInfo() : |
Dan Gohman | 728db49 | 2012-01-13 00:39:07 +0000 | [diff] [blame] | 1190 | KnownSafe(false), IsRetainBlock(false), |
Dan Gohman | 62079b4 | 2012-04-25 00:50:46 +0000 | [diff] [blame] | 1191 | IsTailCallRelease(false), |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1192 | ReleaseMetadata(0) {} |
| 1193 | |
| 1194 | void clear(); |
| 1195 | }; |
| 1196 | } |
| 1197 | |
| 1198 | void RRInfo::clear() { |
Dan Gohman | b389401 | 2011-08-19 00:26:36 +0000 | [diff] [blame] | 1199 | KnownSafe = false; |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1200 | IsRetainBlock = false; |
| 1201 | IsTailCallRelease = false; |
| 1202 | ReleaseMetadata = 0; |
| 1203 | Calls.clear(); |
| 1204 | ReverseInsertPts.clear(); |
| 1205 | } |
| 1206 | |
| 1207 | namespace { |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 1208 | /// \brief This class summarizes several per-pointer runtime properties which |
| 1209 | /// are propogated through the flow graph. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1210 | class PtrState { |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 1211 | /// True if the reference count is known to be incremented. |
Dan Gohman | 62079b4 | 2012-04-25 00:50:46 +0000 | [diff] [blame] | 1212 | bool KnownPositiveRefCount; |
| 1213 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 1214 | /// True of we've seen an opportunity for partial RR elimination, such as |
| 1215 | /// pushing calls into a CFG triangle or into one side of a CFG diamond. |
Dan Gohman | 62079b4 | 2012-04-25 00:50:46 +0000 | [diff] [blame] | 1216 | bool Partial; |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1217 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 1218 | /// The current position in the sequence. |
Dan Gohman | 41375a3 | 2012-05-08 23:39:44 +0000 | [diff] [blame] | 1219 | Sequence Seq : 8; |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1220 | |
| 1221 | public: |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 1222 | /// Unidirectional information about the current sequence. |
| 1223 | /// |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1224 | /// TODO: Encapsulate this better. |
| 1225 | RRInfo RRI; |
| 1226 | |
Dan Gohman | df476e5 | 2012-09-04 23:16:20 +0000 | [diff] [blame] | 1227 | PtrState() : KnownPositiveRefCount(false), Partial(false), |
Dan Gohman | 41375a3 | 2012-05-08 23:39:44 +0000 | [diff] [blame] | 1228 | Seq(S_None) {} |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1229 | |
Dan Gohman | 62079b4 | 2012-04-25 00:50:46 +0000 | [diff] [blame] | 1230 | void SetKnownPositiveRefCount() { |
| 1231 | KnownPositiveRefCount = true; |
Dan Gohman | 1213027 | 2011-08-12 00:26:31 +0000 | [diff] [blame] | 1232 | } |
| 1233 | |
Dan Gohman | 62079b4 | 2012-04-25 00:50:46 +0000 | [diff] [blame] | 1234 | void ClearRefCount() { |
| 1235 | KnownPositiveRefCount = false; |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1236 | } |
| 1237 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1238 | bool IsKnownIncremented() const { |
Dan Gohman | 62079b4 | 2012-04-25 00:50:46 +0000 | [diff] [blame] | 1239 | return KnownPositiveRefCount; |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1240 | } |
| 1241 | |
| 1242 | void SetSeq(Sequence NewSeq) { |
| 1243 | Seq = NewSeq; |
| 1244 | } |
| 1245 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1246 | Sequence GetSeq() const { |
| 1247 | return Seq; |
| 1248 | } |
| 1249 | |
| 1250 | void ClearSequenceProgress() { |
Dan Gohman | 62079b4 | 2012-04-25 00:50:46 +0000 | [diff] [blame] | 1251 | ResetSequenceProgress(S_None); |
| 1252 | } |
| 1253 | |
| 1254 | void ResetSequenceProgress(Sequence NewSeq) { |
| 1255 | Seq = NewSeq; |
| 1256 | Partial = false; |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1257 | RRI.clear(); |
| 1258 | } |
| 1259 | |
| 1260 | void Merge(const PtrState &Other, bool TopDown); |
| 1261 | }; |
| 1262 | } |
| 1263 | |
| 1264 | void |
| 1265 | PtrState::Merge(const PtrState &Other, bool TopDown) { |
| 1266 | Seq = MergeSeqs(Seq, Other.Seq, TopDown); |
Dan Gohman | 62079b4 | 2012-04-25 00:50:46 +0000 | [diff] [blame] | 1267 | KnownPositiveRefCount = KnownPositiveRefCount && Other.KnownPositiveRefCount; |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1268 | |
| 1269 | // We can't merge a plain objc_retain with an objc_retainBlock. |
| 1270 | if (RRI.IsRetainBlock != Other.RRI.IsRetainBlock) |
| 1271 | Seq = S_None; |
| 1272 | |
Dan Gohman | 1736c14 | 2011-10-17 18:48:25 +0000 | [diff] [blame] | 1273 | // If we're not in a sequence (anymore), drop all associated state. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1274 | if (Seq == S_None) { |
Dan Gohman | 62079b4 | 2012-04-25 00:50:46 +0000 | [diff] [blame] | 1275 | Partial = false; |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1276 | RRI.clear(); |
Dan Gohman | 62079b4 | 2012-04-25 00:50:46 +0000 | [diff] [blame] | 1277 | } else if (Partial || Other.Partial) { |
Dan Gohman | 1736c14 | 2011-10-17 18:48:25 +0000 | [diff] [blame] | 1278 | // If we're doing a merge on a path that's previously seen a partial |
| 1279 | // merge, conservatively drop the sequence, to avoid doing partial |
| 1280 | // RR elimination. If the branch predicates for the two merge differ, |
| 1281 | // mixing them is unsafe. |
Dan Gohman | 62079b4 | 2012-04-25 00:50:46 +0000 | [diff] [blame] | 1282 | ClearSequenceProgress(); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1283 | } else { |
| 1284 | // Conservatively merge the ReleaseMetadata information. |
| 1285 | if (RRI.ReleaseMetadata != Other.RRI.ReleaseMetadata) |
| 1286 | RRI.ReleaseMetadata = 0; |
| 1287 | |
Dan Gohman | b389401 | 2011-08-19 00:26:36 +0000 | [diff] [blame] | 1288 | RRI.KnownSafe = RRI.KnownSafe && Other.RRI.KnownSafe; |
Dan Gohman | 41375a3 | 2012-05-08 23:39:44 +0000 | [diff] [blame] | 1289 | RRI.IsTailCallRelease = RRI.IsTailCallRelease && |
| 1290 | Other.RRI.IsTailCallRelease; |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1291 | RRI.Calls.insert(Other.RRI.Calls.begin(), Other.RRI.Calls.end()); |
Dan Gohman | 1736c14 | 2011-10-17 18:48:25 +0000 | [diff] [blame] | 1292 | |
| 1293 | // Merge the insert point sets. If there are any differences, |
| 1294 | // that makes this a partial merge. |
Dan Gohman | 41375a3 | 2012-05-08 23:39:44 +0000 | [diff] [blame] | 1295 | Partial = RRI.ReverseInsertPts.size() != Other.RRI.ReverseInsertPts.size(); |
Dan Gohman | 1736c14 | 2011-10-17 18:48:25 +0000 | [diff] [blame] | 1296 | for (SmallPtrSet<Instruction *, 2>::const_iterator |
| 1297 | I = Other.RRI.ReverseInsertPts.begin(), |
| 1298 | E = Other.RRI.ReverseInsertPts.end(); I != E; ++I) |
Dan Gohman | 62079b4 | 2012-04-25 00:50:46 +0000 | [diff] [blame] | 1299 | Partial |= RRI.ReverseInsertPts.insert(*I); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1300 | } |
| 1301 | } |
| 1302 | |
| 1303 | namespace { |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 1304 | /// \brief Per-BasicBlock state. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1305 | class BBState { |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 1306 | /// The number of unique control paths from the entry which can reach this |
| 1307 | /// block. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1308 | unsigned TopDownPathCount; |
| 1309 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 1310 | /// The number of unique control paths to exits from this block. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1311 | unsigned BottomUpPathCount; |
| 1312 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 1313 | /// A type for PerPtrTopDown and PerPtrBottomUp. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1314 | typedef MapVector<const Value *, PtrState> MapTy; |
| 1315 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 1316 | /// The top-down traversal uses this to record information known about a |
| 1317 | /// pointer at the bottom of each block. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1318 | MapTy PerPtrTopDown; |
| 1319 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 1320 | /// The bottom-up traversal uses this to record information known about a |
| 1321 | /// pointer at the top of each block. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1322 | MapTy PerPtrBottomUp; |
| 1323 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 1324 | /// Effective predecessors of the current block ignoring ignorable edges and |
| 1325 | /// ignored backedges. |
Dan Gohman | c24c66f | 2012-04-24 22:53:18 +0000 | [diff] [blame] | 1326 | SmallVector<BasicBlock *, 2> Preds; |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 1327 | /// Effective successors of the current block ignoring ignorable edges and |
| 1328 | /// ignored backedges. |
Dan Gohman | c24c66f | 2012-04-24 22:53:18 +0000 | [diff] [blame] | 1329 | SmallVector<BasicBlock *, 2> Succs; |
| 1330 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1331 | public: |
| 1332 | BBState() : TopDownPathCount(0), BottomUpPathCount(0) {} |
| 1333 | |
| 1334 | typedef MapTy::iterator ptr_iterator; |
| 1335 | typedef MapTy::const_iterator ptr_const_iterator; |
| 1336 | |
| 1337 | ptr_iterator top_down_ptr_begin() { return PerPtrTopDown.begin(); } |
| 1338 | ptr_iterator top_down_ptr_end() { return PerPtrTopDown.end(); } |
| 1339 | ptr_const_iterator top_down_ptr_begin() const { |
| 1340 | return PerPtrTopDown.begin(); |
| 1341 | } |
| 1342 | ptr_const_iterator top_down_ptr_end() const { |
| 1343 | return PerPtrTopDown.end(); |
| 1344 | } |
| 1345 | |
| 1346 | ptr_iterator bottom_up_ptr_begin() { return PerPtrBottomUp.begin(); } |
| 1347 | ptr_iterator bottom_up_ptr_end() { return PerPtrBottomUp.end(); } |
| 1348 | ptr_const_iterator bottom_up_ptr_begin() const { |
| 1349 | return PerPtrBottomUp.begin(); |
| 1350 | } |
| 1351 | ptr_const_iterator bottom_up_ptr_end() const { |
| 1352 | return PerPtrBottomUp.end(); |
| 1353 | } |
| 1354 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 1355 | /// Mark this block as being an entry block, which has one path from the |
| 1356 | /// entry by definition. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1357 | void SetAsEntry() { TopDownPathCount = 1; } |
| 1358 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 1359 | /// Mark this block as being an exit block, which has one path to an exit by |
| 1360 | /// definition. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1361 | void SetAsExit() { BottomUpPathCount = 1; } |
| 1362 | |
| 1363 | PtrState &getPtrTopDownState(const Value *Arg) { |
| 1364 | return PerPtrTopDown[Arg]; |
| 1365 | } |
| 1366 | |
| 1367 | PtrState &getPtrBottomUpState(const Value *Arg) { |
| 1368 | return PerPtrBottomUp[Arg]; |
| 1369 | } |
| 1370 | |
| 1371 | void clearBottomUpPointers() { |
Evan Cheng | e4df6a2 | 2011-08-04 18:40:26 +0000 | [diff] [blame] | 1372 | PerPtrBottomUp.clear(); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1373 | } |
| 1374 | |
| 1375 | void clearTopDownPointers() { |
| 1376 | PerPtrTopDown.clear(); |
| 1377 | } |
| 1378 | |
| 1379 | void InitFromPred(const BBState &Other); |
| 1380 | void InitFromSucc(const BBState &Other); |
| 1381 | void MergePred(const BBState &Other); |
| 1382 | void MergeSucc(const BBState &Other); |
| 1383 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 1384 | /// Return the number of possible unique paths from an entry to an exit |
| 1385 | /// which pass through this block. This is only valid after both the |
| 1386 | /// top-down and bottom-up traversals are complete. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1387 | unsigned GetAllPathCount() const { |
Dan Gohman | c24c66f | 2012-04-24 22:53:18 +0000 | [diff] [blame] | 1388 | assert(TopDownPathCount != 0); |
| 1389 | assert(BottomUpPathCount != 0); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1390 | return TopDownPathCount * BottomUpPathCount; |
| 1391 | } |
Dan Gohman | 1213027 | 2011-08-12 00:26:31 +0000 | [diff] [blame] | 1392 | |
Dan Gohman | c24c66f | 2012-04-24 22:53:18 +0000 | [diff] [blame] | 1393 | // Specialized CFG utilities. |
Dan Gohman | dae3349 | 2012-04-27 18:56:31 +0000 | [diff] [blame] | 1394 | typedef SmallVectorImpl<BasicBlock *>::const_iterator edge_iterator; |
Dan Gohman | c24c66f | 2012-04-24 22:53:18 +0000 | [diff] [blame] | 1395 | edge_iterator pred_begin() { return Preds.begin(); } |
| 1396 | edge_iterator pred_end() { return Preds.end(); } |
| 1397 | edge_iterator succ_begin() { return Succs.begin(); } |
| 1398 | edge_iterator succ_end() { return Succs.end(); } |
| 1399 | |
| 1400 | void addSucc(BasicBlock *Succ) { Succs.push_back(Succ); } |
| 1401 | void addPred(BasicBlock *Pred) { Preds.push_back(Pred); } |
| 1402 | |
| 1403 | bool isExit() const { return Succs.empty(); } |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1404 | }; |
| 1405 | } |
| 1406 | |
| 1407 | void BBState::InitFromPred(const BBState &Other) { |
| 1408 | PerPtrTopDown = Other.PerPtrTopDown; |
| 1409 | TopDownPathCount = Other.TopDownPathCount; |
| 1410 | } |
| 1411 | |
| 1412 | void BBState::InitFromSucc(const BBState &Other) { |
| 1413 | PerPtrBottomUp = Other.PerPtrBottomUp; |
| 1414 | BottomUpPathCount = Other.BottomUpPathCount; |
| 1415 | } |
| 1416 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 1417 | /// The top-down traversal uses this to merge information about predecessors to |
| 1418 | /// form the initial state for a new block. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1419 | void BBState::MergePred(const BBState &Other) { |
| 1420 | // Other.TopDownPathCount can be 0, in which case it is either dead or a |
| 1421 | // loop backedge. Loop backedges are special. |
| 1422 | TopDownPathCount += Other.TopDownPathCount; |
| 1423 | |
Michael Gottesman | 4385edf | 2013-01-14 01:47:53 +0000 | [diff] [blame] | 1424 | // Check for overflow. If we have overflow, fall back to conservative |
| 1425 | // behavior. |
Dan Gohman | 7c84dad | 2012-09-12 20:45:17 +0000 | [diff] [blame] | 1426 | if (TopDownPathCount < Other.TopDownPathCount) { |
| 1427 | clearTopDownPointers(); |
| 1428 | return; |
| 1429 | } |
| 1430 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1431 | // For each entry in the other set, if our set has an entry with the same key, |
| 1432 | // merge the entries. Otherwise, copy the entry and merge it with an empty |
| 1433 | // entry. |
| 1434 | for (ptr_const_iterator MI = Other.top_down_ptr_begin(), |
| 1435 | ME = Other.top_down_ptr_end(); MI != ME; ++MI) { |
| 1436 | std::pair<ptr_iterator, bool> Pair = PerPtrTopDown.insert(*MI); |
| 1437 | Pair.first->second.Merge(Pair.second ? PtrState() : MI->second, |
| 1438 | /*TopDown=*/true); |
| 1439 | } |
| 1440 | |
Dan Gohman | 7e315fc3 | 2011-08-11 21:06:32 +0000 | [diff] [blame] | 1441 | // For each entry in our set, if the other set doesn't have an entry with the |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1442 | // same key, force it to merge with an empty entry. |
| 1443 | for (ptr_iterator MI = top_down_ptr_begin(), |
| 1444 | ME = top_down_ptr_end(); MI != ME; ++MI) |
| 1445 | if (Other.PerPtrTopDown.find(MI->first) == Other.PerPtrTopDown.end()) |
| 1446 | MI->second.Merge(PtrState(), /*TopDown=*/true); |
| 1447 | } |
| 1448 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 1449 | /// The bottom-up traversal uses this to merge information about successors to |
| 1450 | /// form the initial state for a new block. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1451 | void BBState::MergeSucc(const BBState &Other) { |
| 1452 | // Other.BottomUpPathCount can be 0, in which case it is either dead or a |
| 1453 | // loop backedge. Loop backedges are special. |
| 1454 | BottomUpPathCount += Other.BottomUpPathCount; |
| 1455 | |
Michael Gottesman | 4385edf | 2013-01-14 01:47:53 +0000 | [diff] [blame] | 1456 | // Check for overflow. If we have overflow, fall back to conservative |
| 1457 | // behavior. |
Dan Gohman | 7c84dad | 2012-09-12 20:45:17 +0000 | [diff] [blame] | 1458 | if (BottomUpPathCount < Other.BottomUpPathCount) { |
| 1459 | clearBottomUpPointers(); |
| 1460 | return; |
| 1461 | } |
| 1462 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1463 | // For each entry in the other set, if our set has an entry with the |
| 1464 | // same key, merge the entries. Otherwise, copy the entry and merge |
| 1465 | // it with an empty entry. |
| 1466 | for (ptr_const_iterator MI = Other.bottom_up_ptr_begin(), |
| 1467 | ME = Other.bottom_up_ptr_end(); MI != ME; ++MI) { |
| 1468 | std::pair<ptr_iterator, bool> Pair = PerPtrBottomUp.insert(*MI); |
| 1469 | Pair.first->second.Merge(Pair.second ? PtrState() : MI->second, |
| 1470 | /*TopDown=*/false); |
| 1471 | } |
| 1472 | |
Dan Gohman | 7e315fc3 | 2011-08-11 21:06:32 +0000 | [diff] [blame] | 1473 | // For each entry in our set, if the other set doesn't have an entry |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1474 | // with the same key, force it to merge with an empty entry. |
| 1475 | for (ptr_iterator MI = bottom_up_ptr_begin(), |
| 1476 | ME = bottom_up_ptr_end(); MI != ME; ++MI) |
| 1477 | if (Other.PerPtrBottomUp.find(MI->first) == Other.PerPtrBottomUp.end()) |
| 1478 | MI->second.Merge(PtrState(), /*TopDown=*/false); |
| 1479 | } |
| 1480 | |
| 1481 | namespace { |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 1482 | /// \brief The main ARC optimization pass. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1483 | class ObjCARCOpt : public FunctionPass { |
| 1484 | bool Changed; |
| 1485 | ProvenanceAnalysis PA; |
| 1486 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 1487 | /// A flag indicating whether this optimization pass should run. |
Dan Gohman | ceaac7c | 2011-06-20 23:20:43 +0000 | [diff] [blame] | 1488 | bool Run; |
| 1489 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 1490 | /// Declarations for ObjC runtime functions, for use in creating calls to |
| 1491 | /// them. These are initialized lazily to avoid cluttering up the Module |
| 1492 | /// with unused declarations. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1493 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 1494 | /// Declaration for ObjC runtime function |
| 1495 | /// objc_retainAutoreleasedReturnValue. |
| 1496 | Constant *RetainRVCallee; |
| 1497 | /// Declaration for ObjC runtime function objc_autoreleaseReturnValue. |
| 1498 | Constant *AutoreleaseRVCallee; |
| 1499 | /// Declaration for ObjC runtime function objc_release. |
| 1500 | Constant *ReleaseCallee; |
| 1501 | /// Declaration for ObjC runtime function objc_retain. |
| 1502 | Constant *RetainCallee; |
| 1503 | /// Declaration for ObjC runtime function objc_retainBlock. |
| 1504 | Constant *RetainBlockCallee; |
| 1505 | /// Declaration for ObjC runtime function objc_autorelease. |
| 1506 | Constant *AutoreleaseCallee; |
| 1507 | |
| 1508 | /// Flags which determine whether each of the interesting runtine functions |
| 1509 | /// is in fact used in the current function. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1510 | unsigned UsedInThisFunction; |
| 1511 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 1512 | /// The Metadata Kind for clang.imprecise_release metadata. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1513 | unsigned ImpreciseReleaseMDKind; |
| 1514 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 1515 | /// The Metadata Kind for clang.arc.copy_on_escape metadata. |
Dan Gohman | a7107f9 | 2011-10-17 22:53:25 +0000 | [diff] [blame] | 1516 | unsigned CopyOnEscapeMDKind; |
| 1517 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 1518 | /// The Metadata Kind for clang.arc.no_objc_arc_exceptions metadata. |
Dan Gohman | 0155f30 | 2012-02-17 18:59:53 +0000 | [diff] [blame] | 1519 | unsigned NoObjCARCExceptionsMDKind; |
| 1520 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1521 | Constant *getRetainRVCallee(Module *M); |
| 1522 | Constant *getAutoreleaseRVCallee(Module *M); |
| 1523 | Constant *getReleaseCallee(Module *M); |
| 1524 | Constant *getRetainCallee(Module *M); |
Dan Gohman | 6320f52 | 2011-07-22 22:29:21 +0000 | [diff] [blame] | 1525 | Constant *getRetainBlockCallee(Module *M); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1526 | Constant *getAutoreleaseCallee(Module *M); |
| 1527 | |
Dan Gohman | 728db49 | 2012-01-13 00:39:07 +0000 | [diff] [blame] | 1528 | bool IsRetainBlockOptimizable(const Instruction *Inst); |
| 1529 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1530 | void OptimizeRetainCall(Function &F, Instruction *Retain); |
| 1531 | bool OptimizeRetainRVCall(Function &F, Instruction *RetainRV); |
Michael Gottesman | 556ff61 | 2013-01-12 01:25:19 +0000 | [diff] [blame] | 1532 | void OptimizeAutoreleaseRVCall(Function &F, Instruction *AutoreleaseRV, |
| 1533 | InstructionClass &Class); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1534 | void OptimizeIndividualCalls(Function &F); |
| 1535 | |
| 1536 | void CheckForCFGHazards(const BasicBlock *BB, |
| 1537 | DenseMap<const BasicBlock *, BBState> &BBStates, |
| 1538 | BBState &MyStates) const; |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 1539 | bool VisitInstructionBottomUp(Instruction *Inst, |
Dan Gohman | 5c70fad | 2012-03-23 17:47:54 +0000 | [diff] [blame] | 1540 | BasicBlock *BB, |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 1541 | MapVector<Value *, RRInfo> &Retains, |
| 1542 | BBState &MyStates); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1543 | bool VisitBottomUp(BasicBlock *BB, |
| 1544 | DenseMap<const BasicBlock *, BBState> &BBStates, |
| 1545 | MapVector<Value *, RRInfo> &Retains); |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 1546 | bool VisitInstructionTopDown(Instruction *Inst, |
| 1547 | DenseMap<Value *, RRInfo> &Releases, |
| 1548 | BBState &MyStates); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1549 | bool VisitTopDown(BasicBlock *BB, |
| 1550 | DenseMap<const BasicBlock *, BBState> &BBStates, |
| 1551 | DenseMap<Value *, RRInfo> &Releases); |
| 1552 | bool Visit(Function &F, |
| 1553 | DenseMap<const BasicBlock *, BBState> &BBStates, |
| 1554 | MapVector<Value *, RRInfo> &Retains, |
| 1555 | DenseMap<Value *, RRInfo> &Releases); |
| 1556 | |
| 1557 | void MoveCalls(Value *Arg, RRInfo &RetainsToMove, RRInfo &ReleasesToMove, |
| 1558 | MapVector<Value *, RRInfo> &Retains, |
| 1559 | DenseMap<Value *, RRInfo> &Releases, |
Dan Gohman | 6320f52 | 2011-07-22 22:29:21 +0000 | [diff] [blame] | 1560 | SmallVectorImpl<Instruction *> &DeadInsts, |
| 1561 | Module *M); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1562 | |
Michael Gottesman | 9de6f96 | 2013-01-22 21:49:00 +0000 | [diff] [blame] | 1563 | bool ConnectTDBUTraversals(DenseMap<const BasicBlock *, BBState> &BBStates, |
| 1564 | MapVector<Value *, RRInfo> &Retains, |
| 1565 | DenseMap<Value *, RRInfo> &Releases, |
| 1566 | Module *M, |
| 1567 | SmallVector<Instruction *, 4> &NewRetains, |
| 1568 | SmallVector<Instruction *, 4> &NewReleases, |
| 1569 | SmallVector<Instruction *, 8> &DeadInsts, |
| 1570 | RRInfo &RetainsToMove, |
| 1571 | RRInfo &ReleasesToMove, |
| 1572 | Value *Arg, |
| 1573 | bool KnownSafe, |
| 1574 | bool &AnyPairsCompletelyEliminated); |
| 1575 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1576 | bool PerformCodePlacement(DenseMap<const BasicBlock *, BBState> &BBStates, |
| 1577 | MapVector<Value *, RRInfo> &Retains, |
Dan Gohman | 6320f52 | 2011-07-22 22:29:21 +0000 | [diff] [blame] | 1578 | DenseMap<Value *, RRInfo> &Releases, |
| 1579 | Module *M); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1580 | |
| 1581 | void OptimizeWeakCalls(Function &F); |
| 1582 | |
| 1583 | bool OptimizeSequences(Function &F); |
| 1584 | |
| 1585 | void OptimizeReturns(Function &F); |
| 1586 | |
| 1587 | virtual void getAnalysisUsage(AnalysisUsage &AU) const; |
| 1588 | virtual bool doInitialization(Module &M); |
| 1589 | virtual bool runOnFunction(Function &F); |
| 1590 | virtual void releaseMemory(); |
| 1591 | |
| 1592 | public: |
| 1593 | static char ID; |
| 1594 | ObjCARCOpt() : FunctionPass(ID) { |
| 1595 | initializeObjCARCOptPass(*PassRegistry::getPassRegistry()); |
| 1596 | } |
| 1597 | }; |
| 1598 | } |
| 1599 | |
| 1600 | char ObjCARCOpt::ID = 0; |
| 1601 | INITIALIZE_PASS_BEGIN(ObjCARCOpt, |
| 1602 | "objc-arc", "ObjC ARC optimization", false, false) |
| 1603 | INITIALIZE_PASS_DEPENDENCY(ObjCARCAliasAnalysis) |
| 1604 | INITIALIZE_PASS_END(ObjCARCOpt, |
| 1605 | "objc-arc", "ObjC ARC optimization", false, false) |
| 1606 | |
| 1607 | Pass *llvm::createObjCARCOptPass() { |
| 1608 | return new ObjCARCOpt(); |
| 1609 | } |
| 1610 | |
| 1611 | void ObjCARCOpt::getAnalysisUsage(AnalysisUsage &AU) const { |
| 1612 | AU.addRequired<ObjCARCAliasAnalysis>(); |
| 1613 | AU.addRequired<AliasAnalysis>(); |
| 1614 | // ARC optimization doesn't currently split critical edges. |
| 1615 | AU.setPreservesCFG(); |
| 1616 | } |
| 1617 | |
Dan Gohman | 728db49 | 2012-01-13 00:39:07 +0000 | [diff] [blame] | 1618 | bool ObjCARCOpt::IsRetainBlockOptimizable(const Instruction *Inst) { |
| 1619 | // Without the magic metadata tag, we have to assume this might be an |
| 1620 | // objc_retainBlock call inserted to convert a block pointer to an id, |
| 1621 | // in which case it really is needed. |
| 1622 | if (!Inst->getMetadata(CopyOnEscapeMDKind)) |
| 1623 | return false; |
| 1624 | |
| 1625 | // If the pointer "escapes" (not including being used in a call), |
| 1626 | // the copy may be needed. |
| 1627 | if (DoesObjCBlockEscape(Inst)) |
| 1628 | return false; |
| 1629 | |
| 1630 | // Otherwise, it's not needed. |
| 1631 | return true; |
| 1632 | } |
| 1633 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1634 | Constant *ObjCARCOpt::getRetainRVCallee(Module *M) { |
| 1635 | if (!RetainRVCallee) { |
| 1636 | LLVMContext &C = M->getContext(); |
Jay Foad | b804a2b | 2011-07-12 14:06:48 +0000 | [diff] [blame] | 1637 | Type *I8X = PointerType::getUnqual(Type::getInt8Ty(C)); |
Dan Gohman | 41375a3 | 2012-05-08 23:39:44 +0000 | [diff] [blame] | 1638 | Type *Params[] = { I8X }; |
| 1639 | FunctionType *FTy = FunctionType::get(I8X, Params, /*isVarArg=*/false); |
Bill Wendling | 3d7b0b8 | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 1640 | AttributeSet Attribute = |
Bill Wendling | 09175b3 | 2013-01-22 21:15:51 +0000 | [diff] [blame] | 1641 | AttributeSet().addAttribute(M->getContext(), AttributeSet::FunctionIndex, |
| 1642 | Attribute::NoUnwind); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1643 | RetainRVCallee = |
| 1644 | M->getOrInsertFunction("objc_retainAutoreleasedReturnValue", FTy, |
Bill Wendling | 3d7b0b8 | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 1645 | Attribute); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1646 | } |
| 1647 | return RetainRVCallee; |
| 1648 | } |
| 1649 | |
| 1650 | Constant *ObjCARCOpt::getAutoreleaseRVCallee(Module *M) { |
| 1651 | if (!AutoreleaseRVCallee) { |
| 1652 | LLVMContext &C = M->getContext(); |
Jay Foad | b804a2b | 2011-07-12 14:06:48 +0000 | [diff] [blame] | 1653 | Type *I8X = PointerType::getUnqual(Type::getInt8Ty(C)); |
Dan Gohman | 41375a3 | 2012-05-08 23:39:44 +0000 | [diff] [blame] | 1654 | Type *Params[] = { I8X }; |
| 1655 | FunctionType *FTy = FunctionType::get(I8X, Params, /*isVarArg=*/false); |
Bill Wendling | 3d7b0b8 | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 1656 | AttributeSet Attribute = |
Bill Wendling | 09175b3 | 2013-01-22 21:15:51 +0000 | [diff] [blame] | 1657 | AttributeSet().addAttribute(M->getContext(), AttributeSet::FunctionIndex, |
| 1658 | Attribute::NoUnwind); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1659 | AutoreleaseRVCallee = |
| 1660 | M->getOrInsertFunction("objc_autoreleaseReturnValue", FTy, |
Bill Wendling | 3d7b0b8 | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 1661 | Attribute); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1662 | } |
| 1663 | return AutoreleaseRVCallee; |
| 1664 | } |
| 1665 | |
| 1666 | Constant *ObjCARCOpt::getReleaseCallee(Module *M) { |
| 1667 | if (!ReleaseCallee) { |
| 1668 | LLVMContext &C = M->getContext(); |
Dan Gohman | 41375a3 | 2012-05-08 23:39:44 +0000 | [diff] [blame] | 1669 | Type *Params[] = { PointerType::getUnqual(Type::getInt8Ty(C)) }; |
Bill Wendling | 3d7b0b8 | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 1670 | AttributeSet Attribute = |
Bill Wendling | 09175b3 | 2013-01-22 21:15:51 +0000 | [diff] [blame] | 1671 | AttributeSet().addAttribute(M->getContext(), AttributeSet::FunctionIndex, |
| 1672 | Attribute::NoUnwind); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1673 | ReleaseCallee = |
| 1674 | M->getOrInsertFunction( |
| 1675 | "objc_release", |
| 1676 | FunctionType::get(Type::getVoidTy(C), Params, /*isVarArg=*/false), |
Bill Wendling | 3d7b0b8 | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 1677 | Attribute); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1678 | } |
| 1679 | return ReleaseCallee; |
| 1680 | } |
| 1681 | |
| 1682 | Constant *ObjCARCOpt::getRetainCallee(Module *M) { |
| 1683 | if (!RetainCallee) { |
| 1684 | LLVMContext &C = M->getContext(); |
Dan Gohman | 41375a3 | 2012-05-08 23:39:44 +0000 | [diff] [blame] | 1685 | Type *Params[] = { PointerType::getUnqual(Type::getInt8Ty(C)) }; |
Bill Wendling | 3d7b0b8 | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 1686 | AttributeSet Attribute = |
Bill Wendling | 09175b3 | 2013-01-22 21:15:51 +0000 | [diff] [blame] | 1687 | AttributeSet().addAttribute(M->getContext(), AttributeSet::FunctionIndex, |
| 1688 | Attribute::NoUnwind); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1689 | RetainCallee = |
| 1690 | M->getOrInsertFunction( |
| 1691 | "objc_retain", |
| 1692 | FunctionType::get(Params[0], Params, /*isVarArg=*/false), |
Bill Wendling | 3d7b0b8 | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 1693 | Attribute); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1694 | } |
| 1695 | return RetainCallee; |
| 1696 | } |
| 1697 | |
Dan Gohman | 6320f52 | 2011-07-22 22:29:21 +0000 | [diff] [blame] | 1698 | Constant *ObjCARCOpt::getRetainBlockCallee(Module *M) { |
| 1699 | if (!RetainBlockCallee) { |
| 1700 | LLVMContext &C = M->getContext(); |
Dan Gohman | 41375a3 | 2012-05-08 23:39:44 +0000 | [diff] [blame] | 1701 | Type *Params[] = { PointerType::getUnqual(Type::getInt8Ty(C)) }; |
Dan Gohman | fca43c2 | 2011-09-14 18:33:34 +0000 | [diff] [blame] | 1702 | // objc_retainBlock is not nounwind because it calls user copy constructors |
| 1703 | // which could theoretically throw. |
Dan Gohman | 6320f52 | 2011-07-22 22:29:21 +0000 | [diff] [blame] | 1704 | RetainBlockCallee = |
| 1705 | M->getOrInsertFunction( |
| 1706 | "objc_retainBlock", |
| 1707 | FunctionType::get(Params[0], Params, /*isVarArg=*/false), |
Bill Wendling | e94d843 | 2012-12-07 23:16:57 +0000 | [diff] [blame] | 1708 | AttributeSet()); |
Dan Gohman | 6320f52 | 2011-07-22 22:29:21 +0000 | [diff] [blame] | 1709 | } |
| 1710 | return RetainBlockCallee; |
| 1711 | } |
| 1712 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1713 | Constant *ObjCARCOpt::getAutoreleaseCallee(Module *M) { |
| 1714 | if (!AutoreleaseCallee) { |
| 1715 | LLVMContext &C = M->getContext(); |
Dan Gohman | 41375a3 | 2012-05-08 23:39:44 +0000 | [diff] [blame] | 1716 | Type *Params[] = { PointerType::getUnqual(Type::getInt8Ty(C)) }; |
Bill Wendling | 3d7b0b8 | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 1717 | AttributeSet Attribute = |
Bill Wendling | 09175b3 | 2013-01-22 21:15:51 +0000 | [diff] [blame] | 1718 | AttributeSet().addAttribute(M->getContext(), AttributeSet::FunctionIndex, |
| 1719 | Attribute::NoUnwind); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1720 | AutoreleaseCallee = |
| 1721 | M->getOrInsertFunction( |
| 1722 | "objc_autorelease", |
| 1723 | FunctionType::get(Params[0], Params, /*isVarArg=*/false), |
Bill Wendling | 3d7b0b8 | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 1724 | Attribute); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1725 | } |
| 1726 | return AutoreleaseCallee; |
| 1727 | } |
| 1728 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 1729 | /// Test whether the given value is possible a reference-counted pointer, |
| 1730 | /// including tests which utilize AliasAnalysis. |
Michael Gottesman | 5300cdd | 2013-01-27 06:19:48 +0000 | [diff] [blame] | 1731 | static bool IsPotentialRetainableObjPtr(const Value *Op, AliasAnalysis &AA) { |
Dan Gohman | df476e5 | 2012-09-04 23:16:20 +0000 | [diff] [blame] | 1732 | // First make the rudimentary check. |
Michael Gottesman | 5300cdd | 2013-01-27 06:19:48 +0000 | [diff] [blame] | 1733 | if (!IsPotentialRetainableObjPtr(Op)) |
Dan Gohman | df476e5 | 2012-09-04 23:16:20 +0000 | [diff] [blame] | 1734 | return false; |
| 1735 | |
| 1736 | // Objects in constant memory are not reference-counted. |
| 1737 | if (AA.pointsToConstantMemory(Op)) |
| 1738 | return false; |
| 1739 | |
| 1740 | // Pointers in constant memory are not pointing to reference-counted objects. |
| 1741 | if (const LoadInst *LI = dyn_cast<LoadInst>(Op)) |
| 1742 | if (AA.pointsToConstantMemory(LI->getPointerOperand())) |
| 1743 | return false; |
| 1744 | |
| 1745 | // Otherwise assume the worst. |
| 1746 | return true; |
| 1747 | } |
| 1748 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 1749 | /// Test whether the given instruction can result in a reference count |
| 1750 | /// modification (positive or negative) for the pointer's object. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1751 | static bool |
| 1752 | CanAlterRefCount(const Instruction *Inst, const Value *Ptr, |
| 1753 | ProvenanceAnalysis &PA, InstructionClass Class) { |
| 1754 | switch (Class) { |
| 1755 | case IC_Autorelease: |
| 1756 | case IC_AutoreleaseRV: |
| 1757 | case IC_User: |
| 1758 | // These operations never directly modify a reference count. |
| 1759 | return false; |
| 1760 | default: break; |
| 1761 | } |
| 1762 | |
| 1763 | ImmutableCallSite CS = static_cast<const Value *>(Inst); |
| 1764 | assert(CS && "Only calls can alter reference counts!"); |
| 1765 | |
| 1766 | // See if AliasAnalysis can help us with the call. |
| 1767 | AliasAnalysis::ModRefBehavior MRB = PA.getAA()->getModRefBehavior(CS); |
| 1768 | if (AliasAnalysis::onlyReadsMemory(MRB)) |
| 1769 | return false; |
| 1770 | if (AliasAnalysis::onlyAccessesArgPointees(MRB)) { |
| 1771 | for (ImmutableCallSite::arg_iterator I = CS.arg_begin(), E = CS.arg_end(); |
| 1772 | I != E; ++I) { |
| 1773 | const Value *Op = *I; |
Michael Gottesman | 5300cdd | 2013-01-27 06:19:48 +0000 | [diff] [blame] | 1774 | if (IsPotentialRetainableObjPtr(Op, *PA.getAA()) && PA.related(Ptr, Op)) |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1775 | return true; |
| 1776 | } |
| 1777 | return false; |
| 1778 | } |
| 1779 | |
| 1780 | // Assume the worst. |
| 1781 | return true; |
| 1782 | } |
| 1783 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 1784 | /// Test whether the given instruction can "use" the given pointer's object in a |
| 1785 | /// way that requires the reference count to be positive. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1786 | static bool |
| 1787 | CanUse(const Instruction *Inst, const Value *Ptr, ProvenanceAnalysis &PA, |
| 1788 | InstructionClass Class) { |
| 1789 | // IC_Call operations (as opposed to IC_CallOrUser) never "use" objc pointers. |
| 1790 | if (Class == IC_Call) |
| 1791 | return false; |
| 1792 | |
| 1793 | // Consider various instructions which may have pointer arguments which are |
| 1794 | // not "uses". |
| 1795 | if (const ICmpInst *ICI = dyn_cast<ICmpInst>(Inst)) { |
| 1796 | // Comparing a pointer with null, or any other constant, isn't really a use, |
| 1797 | // because we don't care what the pointer points to, or about the values |
| 1798 | // of any other dynamic reference-counted pointers. |
Michael Gottesman | 5300cdd | 2013-01-27 06:19:48 +0000 | [diff] [blame] | 1799 | if (!IsPotentialRetainableObjPtr(ICI->getOperand(1), *PA.getAA())) |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1800 | return false; |
| 1801 | } else if (ImmutableCallSite CS = static_cast<const Value *>(Inst)) { |
| 1802 | // For calls, just check the arguments (and not the callee operand). |
| 1803 | for (ImmutableCallSite::arg_iterator OI = CS.arg_begin(), |
| 1804 | OE = CS.arg_end(); OI != OE; ++OI) { |
| 1805 | const Value *Op = *OI; |
Michael Gottesman | 5300cdd | 2013-01-27 06:19:48 +0000 | [diff] [blame] | 1806 | if (IsPotentialRetainableObjPtr(Op, *PA.getAA()) && PA.related(Ptr, Op)) |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1807 | return true; |
| 1808 | } |
| 1809 | return false; |
| 1810 | } else if (const StoreInst *SI = dyn_cast<StoreInst>(Inst)) { |
| 1811 | // Special-case stores, because we don't care about the stored value, just |
| 1812 | // the store address. |
| 1813 | const Value *Op = GetUnderlyingObjCPtr(SI->getPointerOperand()); |
| 1814 | // If we can't tell what the underlying object was, assume there is a |
| 1815 | // dependence. |
Michael Gottesman | 5300cdd | 2013-01-27 06:19:48 +0000 | [diff] [blame] | 1816 | return IsPotentialRetainableObjPtr(Op, *PA.getAA()) && PA.related(Op, Ptr); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1817 | } |
| 1818 | |
| 1819 | // Check each operand for a match. |
| 1820 | for (User::const_op_iterator OI = Inst->op_begin(), OE = Inst->op_end(); |
| 1821 | OI != OE; ++OI) { |
| 1822 | const Value *Op = *OI; |
Michael Gottesman | 5300cdd | 2013-01-27 06:19:48 +0000 | [diff] [blame] | 1823 | if (IsPotentialRetainableObjPtr(Op, *PA.getAA()) && PA.related(Ptr, Op)) |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1824 | return true; |
| 1825 | } |
| 1826 | return false; |
| 1827 | } |
| 1828 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 1829 | /// Test whether the given instruction can autorelease any pointer or cause an |
| 1830 | /// autoreleasepool pop. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1831 | static bool |
| 1832 | CanInterruptRV(InstructionClass Class) { |
| 1833 | switch (Class) { |
| 1834 | case IC_AutoreleasepoolPop: |
| 1835 | case IC_CallOrUser: |
| 1836 | case IC_Call: |
| 1837 | case IC_Autorelease: |
| 1838 | case IC_AutoreleaseRV: |
| 1839 | case IC_FusedRetainAutorelease: |
| 1840 | case IC_FusedRetainAutoreleaseRV: |
| 1841 | return true; |
| 1842 | default: |
| 1843 | return false; |
| 1844 | } |
| 1845 | } |
| 1846 | |
| 1847 | namespace { |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 1848 | /// \enum DependenceKind |
| 1849 | /// \brief Defines different dependence kinds among various ARC constructs. |
| 1850 | /// |
| 1851 | /// There are several kinds of dependence-like concepts in use here. |
| 1852 | /// |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1853 | enum DependenceKind { |
| 1854 | NeedsPositiveRetainCount, |
Dan Gohman | 8478d76 | 2012-04-13 00:59:57 +0000 | [diff] [blame] | 1855 | AutoreleasePoolBoundary, |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1856 | CanChangeRetainCount, |
| 1857 | RetainAutoreleaseDep, ///< Blocks objc_retainAutorelease. |
| 1858 | RetainAutoreleaseRVDep, ///< Blocks objc_retainAutoreleaseReturnValue. |
| 1859 | RetainRVDep ///< Blocks objc_retainAutoreleasedReturnValue. |
| 1860 | }; |
| 1861 | } |
| 1862 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 1863 | /// Test if there can be dependencies on Inst through Arg. This function only |
| 1864 | /// tests dependencies relevant for removing pairs of calls. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1865 | static bool |
| 1866 | Depends(DependenceKind Flavor, Instruction *Inst, const Value *Arg, |
| 1867 | ProvenanceAnalysis &PA) { |
| 1868 | // If we've reached the definition of Arg, stop. |
| 1869 | if (Inst == Arg) |
| 1870 | return true; |
| 1871 | |
| 1872 | switch (Flavor) { |
| 1873 | case NeedsPositiveRetainCount: { |
| 1874 | InstructionClass Class = GetInstructionClass(Inst); |
| 1875 | switch (Class) { |
| 1876 | case IC_AutoreleasepoolPop: |
| 1877 | case IC_AutoreleasepoolPush: |
| 1878 | case IC_None: |
| 1879 | return false; |
| 1880 | default: |
| 1881 | return CanUse(Inst, Arg, PA, Class); |
| 1882 | } |
| 1883 | } |
| 1884 | |
Dan Gohman | 8478d76 | 2012-04-13 00:59:57 +0000 | [diff] [blame] | 1885 | case AutoreleasePoolBoundary: { |
| 1886 | InstructionClass Class = GetInstructionClass(Inst); |
| 1887 | switch (Class) { |
| 1888 | case IC_AutoreleasepoolPop: |
| 1889 | case IC_AutoreleasepoolPush: |
| 1890 | // These mark the end and begin of an autorelease pool scope. |
| 1891 | return true; |
| 1892 | default: |
| 1893 | // Nothing else does this. |
| 1894 | return false; |
| 1895 | } |
| 1896 | } |
| 1897 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1898 | case CanChangeRetainCount: { |
| 1899 | InstructionClass Class = GetInstructionClass(Inst); |
| 1900 | switch (Class) { |
| 1901 | case IC_AutoreleasepoolPop: |
| 1902 | // Conservatively assume this can decrement any count. |
| 1903 | return true; |
| 1904 | case IC_AutoreleasepoolPush: |
| 1905 | case IC_None: |
| 1906 | return false; |
| 1907 | default: |
| 1908 | return CanAlterRefCount(Inst, Arg, PA, Class); |
| 1909 | } |
| 1910 | } |
| 1911 | |
| 1912 | case RetainAutoreleaseDep: |
| 1913 | switch (GetBasicInstructionClass(Inst)) { |
| 1914 | case IC_AutoreleasepoolPop: |
Dan Gohman | 8478d76 | 2012-04-13 00:59:57 +0000 | [diff] [blame] | 1915 | case IC_AutoreleasepoolPush: |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1916 | // Don't merge an objc_autorelease with an objc_retain inside a different |
| 1917 | // autoreleasepool scope. |
| 1918 | return true; |
| 1919 | case IC_Retain: |
| 1920 | case IC_RetainRV: |
| 1921 | // Check for a retain of the same pointer for merging. |
| 1922 | return GetObjCArg(Inst) == Arg; |
| 1923 | default: |
| 1924 | // Nothing else matters for objc_retainAutorelease formation. |
| 1925 | return false; |
| 1926 | } |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1927 | |
| 1928 | case RetainAutoreleaseRVDep: { |
| 1929 | InstructionClass Class = GetBasicInstructionClass(Inst); |
| 1930 | switch (Class) { |
| 1931 | case IC_Retain: |
| 1932 | case IC_RetainRV: |
| 1933 | // Check for a retain of the same pointer for merging. |
| 1934 | return GetObjCArg(Inst) == Arg; |
| 1935 | default: |
| 1936 | // Anything that can autorelease interrupts |
| 1937 | // retainAutoreleaseReturnValue formation. |
| 1938 | return CanInterruptRV(Class); |
| 1939 | } |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1940 | } |
| 1941 | |
| 1942 | case RetainRVDep: |
| 1943 | return CanInterruptRV(GetBasicInstructionClass(Inst)); |
| 1944 | } |
| 1945 | |
| 1946 | llvm_unreachable("Invalid dependence flavor"); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1947 | } |
| 1948 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 1949 | /// Walk up the CFG from StartPos (which is in StartBB) and find local and |
| 1950 | /// non-local dependencies on Arg. |
| 1951 | /// |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1952 | /// TODO: Cache results? |
| 1953 | static void |
| 1954 | FindDependencies(DependenceKind Flavor, |
| 1955 | const Value *Arg, |
| 1956 | BasicBlock *StartBB, Instruction *StartInst, |
| 1957 | SmallPtrSet<Instruction *, 4> &DependingInstructions, |
| 1958 | SmallPtrSet<const BasicBlock *, 4> &Visited, |
| 1959 | ProvenanceAnalysis &PA) { |
| 1960 | BasicBlock::iterator StartPos = StartInst; |
| 1961 | |
| 1962 | SmallVector<std::pair<BasicBlock *, BasicBlock::iterator>, 4> Worklist; |
| 1963 | Worklist.push_back(std::make_pair(StartBB, StartPos)); |
| 1964 | do { |
| 1965 | std::pair<BasicBlock *, BasicBlock::iterator> Pair = |
| 1966 | Worklist.pop_back_val(); |
| 1967 | BasicBlock *LocalStartBB = Pair.first; |
| 1968 | BasicBlock::iterator LocalStartPos = Pair.second; |
| 1969 | BasicBlock::iterator StartBBBegin = LocalStartBB->begin(); |
| 1970 | for (;;) { |
| 1971 | if (LocalStartPos == StartBBBegin) { |
| 1972 | pred_iterator PI(LocalStartBB), PE(LocalStartBB, false); |
| 1973 | if (PI == PE) |
| 1974 | // If we've reached the function entry, produce a null dependence. |
| 1975 | DependingInstructions.insert(0); |
| 1976 | else |
| 1977 | // Add the predecessors to the worklist. |
| 1978 | do { |
| 1979 | BasicBlock *PredBB = *PI; |
| 1980 | if (Visited.insert(PredBB)) |
| 1981 | Worklist.push_back(std::make_pair(PredBB, PredBB->end())); |
| 1982 | } while (++PI != PE); |
| 1983 | break; |
| 1984 | } |
| 1985 | |
| 1986 | Instruction *Inst = --LocalStartPos; |
| 1987 | if (Depends(Flavor, Inst, Arg, PA)) { |
| 1988 | DependingInstructions.insert(Inst); |
| 1989 | break; |
| 1990 | } |
| 1991 | } |
| 1992 | } while (!Worklist.empty()); |
| 1993 | |
| 1994 | // Determine whether the original StartBB post-dominates all of the blocks we |
| 1995 | // visited. If not, insert a sentinal indicating that most optimizations are |
| 1996 | // not safe. |
| 1997 | for (SmallPtrSet<const BasicBlock *, 4>::const_iterator I = Visited.begin(), |
| 1998 | E = Visited.end(); I != E; ++I) { |
| 1999 | const BasicBlock *BB = *I; |
| 2000 | if (BB == StartBB) |
| 2001 | continue; |
| 2002 | const TerminatorInst *TI = cast<TerminatorInst>(&BB->back()); |
| 2003 | for (succ_const_iterator SI(TI), SE(TI, false); SI != SE; ++SI) { |
| 2004 | const BasicBlock *Succ = *SI; |
| 2005 | if (Succ != StartBB && !Visited.count(Succ)) { |
| 2006 | DependingInstructions.insert(reinterpret_cast<Instruction *>(-1)); |
| 2007 | return; |
| 2008 | } |
| 2009 | } |
| 2010 | } |
| 2011 | } |
| 2012 | |
| 2013 | static bool isNullOrUndef(const Value *V) { |
| 2014 | return isa<ConstantPointerNull>(V) || isa<UndefValue>(V); |
| 2015 | } |
| 2016 | |
| 2017 | static bool isNoopInstruction(const Instruction *I) { |
| 2018 | return isa<BitCastInst>(I) || |
| 2019 | (isa<GetElementPtrInst>(I) && |
| 2020 | cast<GetElementPtrInst>(I)->hasAllZeroIndices()); |
| 2021 | } |
| 2022 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 2023 | /// Turn objc_retain into objc_retainAutoreleasedReturnValue if the operand is a |
| 2024 | /// return value. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2025 | void |
| 2026 | ObjCARCOpt::OptimizeRetainCall(Function &F, Instruction *Retain) { |
Dan Gohman | dae3349 | 2012-04-27 18:56:31 +0000 | [diff] [blame] | 2027 | ImmutableCallSite CS(GetObjCArg(Retain)); |
| 2028 | const Instruction *Call = CS.getInstruction(); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2029 | if (!Call) return; |
| 2030 | if (Call->getParent() != Retain->getParent()) return; |
| 2031 | |
| 2032 | // Check that the call is next to the retain. |
Dan Gohman | dae3349 | 2012-04-27 18:56:31 +0000 | [diff] [blame] | 2033 | BasicBlock::const_iterator I = Call; |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2034 | ++I; |
| 2035 | while (isNoopInstruction(I)) ++I; |
| 2036 | if (&*I != Retain) |
| 2037 | return; |
| 2038 | |
| 2039 | // Turn it to an objc_retainAutoreleasedReturnValue.. |
| 2040 | Changed = true; |
| 2041 | ++NumPeeps; |
Michael Gottesman | 10426b5 | 2013-01-07 21:26:07 +0000 | [diff] [blame] | 2042 | |
Michael Gottesman | 1e00ac6 | 2013-01-04 21:30:38 +0000 | [diff] [blame] | 2043 | DEBUG(dbgs() << "ObjCARCOpt::OptimizeRetainCall: Transforming " |
Michael Gottesman | 9f1be68 | 2013-01-12 03:45:49 +0000 | [diff] [blame] | 2044 | "objc_retain => objc_retainAutoreleasedReturnValue" |
| 2045 | " since the operand is a return value.\n" |
Michael Gottesman | 1e00ac6 | 2013-01-04 21:30:38 +0000 | [diff] [blame] | 2046 | " Old: " |
| 2047 | << *Retain << "\n"); |
Michael Gottesman | 10426b5 | 2013-01-07 21:26:07 +0000 | [diff] [blame] | 2048 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2049 | cast<CallInst>(Retain)->setCalledFunction(getRetainRVCallee(F.getParent())); |
Michael Gottesman | 1e00ac6 | 2013-01-04 21:30:38 +0000 | [diff] [blame] | 2050 | |
| 2051 | DEBUG(dbgs() << " New: " |
| 2052 | << *Retain << "\n"); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2053 | } |
| 2054 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 2055 | /// Turn objc_retainAutoreleasedReturnValue into objc_retain if the operand is |
| 2056 | /// not a return value. Or, if it can be paired with an |
| 2057 | /// objc_autoreleaseReturnValue, delete the pair and return true. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2058 | bool |
| 2059 | ObjCARCOpt::OptimizeRetainRVCall(Function &F, Instruction *RetainRV) { |
Dan Gohman | e3ed2b0 | 2012-03-23 18:09:00 +0000 | [diff] [blame] | 2060 | // Check for the argument being from an immediately preceding call or invoke. |
Dan Gohman | dae3349 | 2012-04-27 18:56:31 +0000 | [diff] [blame] | 2061 | const Value *Arg = GetObjCArg(RetainRV); |
| 2062 | ImmutableCallSite CS(Arg); |
| 2063 | if (const Instruction *Call = CS.getInstruction()) { |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2064 | if (Call->getParent() == RetainRV->getParent()) { |
Dan Gohman | dae3349 | 2012-04-27 18:56:31 +0000 | [diff] [blame] | 2065 | BasicBlock::const_iterator I = Call; |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2066 | ++I; |
| 2067 | while (isNoopInstruction(I)) ++I; |
| 2068 | if (&*I == RetainRV) |
| 2069 | return false; |
Dan Gohman | dae3349 | 2012-04-27 18:56:31 +0000 | [diff] [blame] | 2070 | } else if (const InvokeInst *II = dyn_cast<InvokeInst>(Call)) { |
Dan Gohman | e3ed2b0 | 2012-03-23 18:09:00 +0000 | [diff] [blame] | 2071 | BasicBlock *RetainRVParent = RetainRV->getParent(); |
| 2072 | if (II->getNormalDest() == RetainRVParent) { |
Dan Gohman | dae3349 | 2012-04-27 18:56:31 +0000 | [diff] [blame] | 2073 | BasicBlock::const_iterator I = RetainRVParent->begin(); |
Dan Gohman | e3ed2b0 | 2012-03-23 18:09:00 +0000 | [diff] [blame] | 2074 | while (isNoopInstruction(I)) ++I; |
| 2075 | if (&*I == RetainRV) |
| 2076 | return false; |
| 2077 | } |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2078 | } |
Dan Gohman | e3ed2b0 | 2012-03-23 18:09:00 +0000 | [diff] [blame] | 2079 | } |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2080 | |
| 2081 | // Check for being preceded by an objc_autoreleaseReturnValue on the same |
| 2082 | // pointer. In this case, we can delete the pair. |
| 2083 | BasicBlock::iterator I = RetainRV, Begin = RetainRV->getParent()->begin(); |
| 2084 | if (I != Begin) { |
| 2085 | do --I; while (I != Begin && isNoopInstruction(I)); |
| 2086 | if (GetBasicInstructionClass(I) == IC_AutoreleaseRV && |
| 2087 | GetObjCArg(I) == Arg) { |
| 2088 | Changed = true; |
| 2089 | ++NumPeeps; |
Michael Gottesman | 10426b5 | 2013-01-07 21:26:07 +0000 | [diff] [blame] | 2090 | |
Michael Gottesman | 5c32ce9 | 2013-01-05 17:55:35 +0000 | [diff] [blame] | 2091 | DEBUG(dbgs() << "ObjCARCOpt::OptimizeRetainRVCall: Erasing " << *I << "\n" |
| 2092 | << " Erasing " << *RetainRV |
| 2093 | << "\n"); |
Michael Gottesman | 10426b5 | 2013-01-07 21:26:07 +0000 | [diff] [blame] | 2094 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2095 | EraseInstruction(I); |
| 2096 | EraseInstruction(RetainRV); |
| 2097 | return true; |
| 2098 | } |
| 2099 | } |
| 2100 | |
| 2101 | // Turn it to a plain objc_retain. |
| 2102 | Changed = true; |
| 2103 | ++NumPeeps; |
Michael Gottesman | 10426b5 | 2013-01-07 21:26:07 +0000 | [diff] [blame] | 2104 | |
Michael Gottesman | def07bb | 2013-01-05 17:55:42 +0000 | [diff] [blame] | 2105 | DEBUG(dbgs() << "ObjCARCOpt::OptimizeRetainRVCall: Transforming " |
| 2106 | "objc_retainAutoreleasedReturnValue => " |
| 2107 | "objc_retain since the operand is not a return value.\n" |
| 2108 | " Old: " |
| 2109 | << *RetainRV << "\n"); |
Michael Gottesman | 10426b5 | 2013-01-07 21:26:07 +0000 | [diff] [blame] | 2110 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2111 | cast<CallInst>(RetainRV)->setCalledFunction(getRetainCallee(F.getParent())); |
Michael Gottesman | def07bb | 2013-01-05 17:55:42 +0000 | [diff] [blame] | 2112 | |
| 2113 | DEBUG(dbgs() << " New: " |
| 2114 | << *RetainRV << "\n"); |
| 2115 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2116 | return false; |
| 2117 | } |
| 2118 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 2119 | /// Turn objc_autoreleaseReturnValue into objc_autorelease if the result is not |
| 2120 | /// used as a return value. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2121 | void |
Michael Gottesman | 556ff61 | 2013-01-12 01:25:19 +0000 | [diff] [blame] | 2122 | ObjCARCOpt::OptimizeAutoreleaseRVCall(Function &F, Instruction *AutoreleaseRV, |
| 2123 | InstructionClass &Class) { |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2124 | // Check for a return of the pointer value. |
| 2125 | const Value *Ptr = GetObjCArg(AutoreleaseRV); |
Dan Gohman | 10a18d5 | 2011-08-12 00:36:31 +0000 | [diff] [blame] | 2126 | SmallVector<const Value *, 2> Users; |
| 2127 | Users.push_back(Ptr); |
| 2128 | do { |
| 2129 | Ptr = Users.pop_back_val(); |
| 2130 | for (Value::const_use_iterator UI = Ptr->use_begin(), UE = Ptr->use_end(); |
| 2131 | UI != UE; ++UI) { |
| 2132 | const User *I = *UI; |
| 2133 | if (isa<ReturnInst>(I) || GetBasicInstructionClass(I) == IC_RetainRV) |
| 2134 | return; |
| 2135 | if (isa<BitCastInst>(I)) |
| 2136 | Users.push_back(I); |
| 2137 | } |
| 2138 | } while (!Users.empty()); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2139 | |
| 2140 | Changed = true; |
| 2141 | ++NumPeeps; |
Michael Gottesman | 1bf6908 | 2013-01-06 21:07:11 +0000 | [diff] [blame] | 2142 | |
| 2143 | DEBUG(dbgs() << "ObjCARCOpt::OptimizeAutoreleaseRVCall: Transforming " |
| 2144 | "objc_autoreleaseReturnValue => " |
| 2145 | "objc_autorelease since its operand is not used as a return " |
| 2146 | "value.\n" |
| 2147 | " Old: " |
| 2148 | << *AutoreleaseRV << "\n"); |
| 2149 | |
Michael Gottesman | c9656fa | 2013-01-12 01:25:15 +0000 | [diff] [blame] | 2150 | CallInst *AutoreleaseRVCI = cast<CallInst>(AutoreleaseRV); |
| 2151 | AutoreleaseRVCI-> |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2152 | setCalledFunction(getAutoreleaseCallee(F.getParent())); |
Michael Gottesman | c9656fa | 2013-01-12 01:25:15 +0000 | [diff] [blame] | 2153 | AutoreleaseRVCI->setTailCall(false); // Never tail call objc_autorelease. |
Michael Gottesman | 556ff61 | 2013-01-12 01:25:19 +0000 | [diff] [blame] | 2154 | Class = IC_Autorelease; |
Michael Gottesman | 10426b5 | 2013-01-07 21:26:07 +0000 | [diff] [blame] | 2155 | |
Michael Gottesman | 1bf6908 | 2013-01-06 21:07:11 +0000 | [diff] [blame] | 2156 | DEBUG(dbgs() << " New: " |
| 2157 | << *AutoreleaseRV << "\n"); |
Michael Gottesman | 10426b5 | 2013-01-07 21:26:07 +0000 | [diff] [blame] | 2158 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2159 | } |
| 2160 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 2161 | /// Visit each call, one at a time, and make simplifications without doing any |
| 2162 | /// additional analysis. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2163 | void ObjCARCOpt::OptimizeIndividualCalls(Function &F) { |
| 2164 | // Reset all the flags in preparation for recomputing them. |
| 2165 | UsedInThisFunction = 0; |
| 2166 | |
| 2167 | // Visit all objc_* calls in F. |
| 2168 | for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ) { |
| 2169 | Instruction *Inst = &*I++; |
Michael Gottesman | 3f146e2 | 2013-01-01 16:05:48 +0000 | [diff] [blame] | 2170 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2171 | InstructionClass Class = GetBasicInstructionClass(Inst); |
| 2172 | |
Michael Gottesman | d359e06 | 2013-01-18 03:08:39 +0000 | [diff] [blame] | 2173 | DEBUG(dbgs() << "ObjCARCOpt::OptimizeIndividualCalls: Visiting: Class: " |
| 2174 | << Class << "; " << *Inst << "\n"); |
Michael Gottesman | 782e344 | 2013-01-17 18:32:34 +0000 | [diff] [blame] | 2175 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2176 | switch (Class) { |
| 2177 | default: break; |
| 2178 | |
| 2179 | // Delete no-op casts. These function calls have special semantics, but |
| 2180 | // the semantics are entirely implemented via lowering in the front-end, |
| 2181 | // so by the time they reach the optimizer, they are just no-op calls |
| 2182 | // which return their argument. |
| 2183 | // |
| 2184 | // There are gray areas here, as the ability to cast reference-counted |
| 2185 | // pointers to raw void* and back allows code to break ARC assumptions, |
| 2186 | // however these are currently considered to be unimportant. |
| 2187 | case IC_NoopCast: |
| 2188 | Changed = true; |
| 2189 | ++NumNoops; |
Michael Gottesman | dc042f0 | 2013-01-06 21:07:15 +0000 | [diff] [blame] | 2190 | DEBUG(dbgs() << "ObjCARCOpt::OptimizeIndividualCalls: Erasing no-op cast:" |
| 2191 | " " << *Inst << "\n"); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2192 | EraseInstruction(Inst); |
| 2193 | continue; |
| 2194 | |
| 2195 | // If the pointer-to-weak-pointer is null, it's undefined behavior. |
| 2196 | case IC_StoreWeak: |
| 2197 | case IC_LoadWeak: |
| 2198 | case IC_LoadWeakRetained: |
| 2199 | case IC_InitWeak: |
| 2200 | case IC_DestroyWeak: { |
| 2201 | CallInst *CI = cast<CallInst>(Inst); |
| 2202 | if (isNullOrUndef(CI->getArgOperand(0))) { |
Dan Gohman | 670f937 | 2012-04-13 18:57:48 +0000 | [diff] [blame] | 2203 | Changed = true; |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2204 | Type *Ty = CI->getArgOperand(0)->getType(); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2205 | new StoreInst(UndefValue::get(cast<PointerType>(Ty)->getElementType()), |
| 2206 | Constant::getNullValue(Ty), |
| 2207 | CI); |
Michael Gottesman | 10426b5 | 2013-01-07 21:26:07 +0000 | [diff] [blame] | 2208 | llvm::Value *NewValue = UndefValue::get(CI->getType()); |
Michael Gottesman | fec61c0 | 2013-01-06 21:54:30 +0000 | [diff] [blame] | 2209 | DEBUG(dbgs() << "ObjCARCOpt::OptimizeIndividualCalls: A null " |
| 2210 | "pointer-to-weak-pointer is undefined behavior.\n" |
| 2211 | " Old = " << *CI << |
| 2212 | "\n New = " << |
Michael Gottesman | 10426b5 | 2013-01-07 21:26:07 +0000 | [diff] [blame] | 2213 | *NewValue << "\n"); |
Michael Gottesman | fec61c0 | 2013-01-06 21:54:30 +0000 | [diff] [blame] | 2214 | CI->replaceAllUsesWith(NewValue); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2215 | CI->eraseFromParent(); |
| 2216 | continue; |
| 2217 | } |
| 2218 | break; |
| 2219 | } |
| 2220 | case IC_CopyWeak: |
| 2221 | case IC_MoveWeak: { |
| 2222 | CallInst *CI = cast<CallInst>(Inst); |
| 2223 | if (isNullOrUndef(CI->getArgOperand(0)) || |
| 2224 | isNullOrUndef(CI->getArgOperand(1))) { |
Dan Gohman | 670f937 | 2012-04-13 18:57:48 +0000 | [diff] [blame] | 2225 | Changed = true; |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2226 | Type *Ty = CI->getArgOperand(0)->getType(); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2227 | new StoreInst(UndefValue::get(cast<PointerType>(Ty)->getElementType()), |
| 2228 | Constant::getNullValue(Ty), |
| 2229 | CI); |
Michael Gottesman | fec61c0 | 2013-01-06 21:54:30 +0000 | [diff] [blame] | 2230 | |
| 2231 | llvm::Value *NewValue = UndefValue::get(CI->getType()); |
| 2232 | DEBUG(dbgs() << "ObjCARCOpt::OptimizeIndividualCalls: A null " |
| 2233 | "pointer-to-weak-pointer is undefined behavior.\n" |
| 2234 | " Old = " << *CI << |
| 2235 | "\n New = " << |
| 2236 | *NewValue << "\n"); |
Michael Gottesman | 10426b5 | 2013-01-07 21:26:07 +0000 | [diff] [blame] | 2237 | |
Michael Gottesman | fec61c0 | 2013-01-06 21:54:30 +0000 | [diff] [blame] | 2238 | CI->replaceAllUsesWith(NewValue); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2239 | CI->eraseFromParent(); |
| 2240 | continue; |
| 2241 | } |
| 2242 | break; |
| 2243 | } |
| 2244 | case IC_Retain: |
| 2245 | OptimizeRetainCall(F, Inst); |
| 2246 | break; |
| 2247 | case IC_RetainRV: |
| 2248 | if (OptimizeRetainRVCall(F, Inst)) |
| 2249 | continue; |
| 2250 | break; |
| 2251 | case IC_AutoreleaseRV: |
Michael Gottesman | 556ff61 | 2013-01-12 01:25:19 +0000 | [diff] [blame] | 2252 | OptimizeAutoreleaseRVCall(F, Inst, Class); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2253 | break; |
| 2254 | } |
| 2255 | |
| 2256 | // objc_autorelease(x) -> objc_release(x) if x is otherwise unused. |
| 2257 | if (IsAutorelease(Class) && Inst->use_empty()) { |
| 2258 | CallInst *Call = cast<CallInst>(Inst); |
| 2259 | const Value *Arg = Call->getArgOperand(0); |
| 2260 | Arg = FindSingleUseIdentifiedObject(Arg); |
| 2261 | if (Arg) { |
| 2262 | Changed = true; |
| 2263 | ++NumAutoreleases; |
| 2264 | |
| 2265 | // Create the declaration lazily. |
| 2266 | LLVMContext &C = Inst->getContext(); |
| 2267 | CallInst *NewCall = |
| 2268 | CallInst::Create(getReleaseCallee(F.getParent()), |
| 2269 | Call->getArgOperand(0), "", Call); |
| 2270 | NewCall->setMetadata(ImpreciseReleaseMDKind, |
| 2271 | MDNode::get(C, ArrayRef<Value *>())); |
Michael Gottesman | 10426b5 | 2013-01-07 21:26:07 +0000 | [diff] [blame] | 2272 | |
Michael Gottesman | a6a1dad | 2013-01-06 22:56:50 +0000 | [diff] [blame] | 2273 | DEBUG(dbgs() << "ObjCARCOpt::OptimizeIndividualCalls: Replacing " |
| 2274 | "objc_autorelease(x) with objc_release(x) since x is " |
| 2275 | "otherwise unused.\n" |
Michael Gottesman | 4bf6e75 | 2013-01-06 22:56:54 +0000 | [diff] [blame] | 2276 | " Old: " << *Call << |
Michael Gottesman | a6a1dad | 2013-01-06 22:56:50 +0000 | [diff] [blame] | 2277 | "\n New: " << |
| 2278 | *NewCall << "\n"); |
Michael Gottesman | 10426b5 | 2013-01-07 21:26:07 +0000 | [diff] [blame] | 2279 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2280 | EraseInstruction(Call); |
| 2281 | Inst = NewCall; |
| 2282 | Class = IC_Release; |
| 2283 | } |
| 2284 | } |
| 2285 | |
| 2286 | // For functions which can never be passed stack arguments, add |
| 2287 | // a tail keyword. |
| 2288 | if (IsAlwaysTail(Class)) { |
| 2289 | Changed = true; |
Michael Gottesman | 2d76331 | 2013-01-06 23:39:09 +0000 | [diff] [blame] | 2290 | DEBUG(dbgs() << "ObjCARCOpt::OptimizeIndividualCalls: Adding tail keyword" |
| 2291 | " to function since it can never be passed stack args: " << *Inst << |
| 2292 | "\n"); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2293 | cast<CallInst>(Inst)->setTailCall(); |
| 2294 | } |
| 2295 | |
Michael Gottesman | c9656fa | 2013-01-12 01:25:15 +0000 | [diff] [blame] | 2296 | // Ensure that functions that can never have a "tail" keyword due to the |
| 2297 | // semantics of ARC truly do not do so. |
| 2298 | if (IsNeverTail(Class)) { |
| 2299 | Changed = true; |
Michael Gottesman | 4385edf | 2013-01-14 01:47:53 +0000 | [diff] [blame] | 2300 | DEBUG(dbgs() << "ObjCARCOpt::OptimizeIndividualCalls: Removing tail " |
| 2301 | "keyword from function: " << *Inst << |
Michael Gottesman | c9656fa | 2013-01-12 01:25:15 +0000 | [diff] [blame] | 2302 | "\n"); |
| 2303 | cast<CallInst>(Inst)->setTailCall(false); |
| 2304 | } |
| 2305 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2306 | // Set nounwind as needed. |
| 2307 | if (IsNoThrow(Class)) { |
| 2308 | Changed = true; |
Michael Gottesman | 8800a51 | 2013-01-06 23:39:13 +0000 | [diff] [blame] | 2309 | DEBUG(dbgs() << "ObjCARCOpt::OptimizeIndividualCalls: Found no throw" |
| 2310 | " class. Setting nounwind on: " << *Inst << "\n"); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2311 | cast<CallInst>(Inst)->setDoesNotThrow(); |
| 2312 | } |
| 2313 | |
| 2314 | if (!IsNoopOnNull(Class)) { |
| 2315 | UsedInThisFunction |= 1 << Class; |
| 2316 | continue; |
| 2317 | } |
| 2318 | |
| 2319 | const Value *Arg = GetObjCArg(Inst); |
| 2320 | |
| 2321 | // ARC calls with null are no-ops. Delete them. |
| 2322 | if (isNullOrUndef(Arg)) { |
| 2323 | Changed = true; |
| 2324 | ++NumNoops; |
Michael Gottesman | 5b970e1 | 2013-01-07 00:04:52 +0000 | [diff] [blame] | 2325 | DEBUG(dbgs() << "ObjCARCOpt::OptimizeIndividualCalls: ARC calls with " |
| 2326 | " null are no-ops. Erasing: " << *Inst << "\n"); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2327 | EraseInstruction(Inst); |
| 2328 | continue; |
| 2329 | } |
| 2330 | |
| 2331 | // Keep track of which of retain, release, autorelease, and retain_block |
| 2332 | // are actually present in this function. |
| 2333 | UsedInThisFunction |= 1 << Class; |
| 2334 | |
| 2335 | // If Arg is a PHI, and one or more incoming values to the |
| 2336 | // PHI are null, and the call is control-equivalent to the PHI, and there |
| 2337 | // are no relevant side effects between the PHI and the call, the call |
| 2338 | // could be pushed up to just those paths with non-null incoming values. |
| 2339 | // For now, don't bother splitting critical edges for this. |
| 2340 | SmallVector<std::pair<Instruction *, const Value *>, 4> Worklist; |
| 2341 | Worklist.push_back(std::make_pair(Inst, Arg)); |
| 2342 | do { |
| 2343 | std::pair<Instruction *, const Value *> Pair = Worklist.pop_back_val(); |
| 2344 | Inst = Pair.first; |
| 2345 | Arg = Pair.second; |
| 2346 | |
| 2347 | const PHINode *PN = dyn_cast<PHINode>(Arg); |
| 2348 | if (!PN) continue; |
| 2349 | |
| 2350 | // Determine if the PHI has any null operands, or any incoming |
| 2351 | // critical edges. |
| 2352 | bool HasNull = false; |
| 2353 | bool HasCriticalEdges = false; |
| 2354 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { |
| 2355 | Value *Incoming = |
| 2356 | StripPointerCastsAndObjCCalls(PN->getIncomingValue(i)); |
| 2357 | if (isNullOrUndef(Incoming)) |
| 2358 | HasNull = true; |
| 2359 | else if (cast<TerminatorInst>(PN->getIncomingBlock(i)->back()) |
| 2360 | .getNumSuccessors() != 1) { |
| 2361 | HasCriticalEdges = true; |
| 2362 | break; |
| 2363 | } |
| 2364 | } |
| 2365 | // If we have null operands and no critical edges, optimize. |
| 2366 | if (!HasCriticalEdges && HasNull) { |
| 2367 | SmallPtrSet<Instruction *, 4> DependingInstructions; |
| 2368 | SmallPtrSet<const BasicBlock *, 4> Visited; |
| 2369 | |
| 2370 | // Check that there is nothing that cares about the reference |
| 2371 | // count between the call and the phi. |
Dan Gohman | 8478d76 | 2012-04-13 00:59:57 +0000 | [diff] [blame] | 2372 | switch (Class) { |
| 2373 | case IC_Retain: |
| 2374 | case IC_RetainBlock: |
| 2375 | // These can always be moved up. |
| 2376 | break; |
| 2377 | case IC_Release: |
Dan Gohman | 41375a3 | 2012-05-08 23:39:44 +0000 | [diff] [blame] | 2378 | // These can't be moved across things that care about the retain |
| 2379 | // count. |
Dan Gohman | 8478d76 | 2012-04-13 00:59:57 +0000 | [diff] [blame] | 2380 | FindDependencies(NeedsPositiveRetainCount, Arg, |
| 2381 | Inst->getParent(), Inst, |
| 2382 | DependingInstructions, Visited, PA); |
| 2383 | break; |
| 2384 | case IC_Autorelease: |
| 2385 | // These can't be moved across autorelease pool scope boundaries. |
| 2386 | FindDependencies(AutoreleasePoolBoundary, Arg, |
| 2387 | Inst->getParent(), Inst, |
| 2388 | DependingInstructions, Visited, PA); |
| 2389 | break; |
| 2390 | case IC_RetainRV: |
| 2391 | case IC_AutoreleaseRV: |
| 2392 | // Don't move these; the RV optimization depends on the autoreleaseRV |
| 2393 | // being tail called, and the retainRV being immediately after a call |
| 2394 | // (which might still happen if we get lucky with codegen layout, but |
| 2395 | // it's not worth taking the chance). |
| 2396 | continue; |
| 2397 | default: |
| 2398 | llvm_unreachable("Invalid dependence flavor"); |
| 2399 | } |
| 2400 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2401 | if (DependingInstructions.size() == 1 && |
| 2402 | *DependingInstructions.begin() == PN) { |
| 2403 | Changed = true; |
| 2404 | ++NumPartialNoops; |
| 2405 | // Clone the call into each predecessor that has a non-null value. |
| 2406 | CallInst *CInst = cast<CallInst>(Inst); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2407 | Type *ParamTy = CInst->getArgOperand(0)->getType(); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2408 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { |
| 2409 | Value *Incoming = |
| 2410 | StripPointerCastsAndObjCCalls(PN->getIncomingValue(i)); |
| 2411 | if (!isNullOrUndef(Incoming)) { |
| 2412 | CallInst *Clone = cast<CallInst>(CInst->clone()); |
| 2413 | Value *Op = PN->getIncomingValue(i); |
| 2414 | Instruction *InsertPos = &PN->getIncomingBlock(i)->back(); |
| 2415 | if (Op->getType() != ParamTy) |
| 2416 | Op = new BitCastInst(Op, ParamTy, "", InsertPos); |
| 2417 | Clone->setArgOperand(0, Op); |
| 2418 | Clone->insertBefore(InsertPos); |
Michael Gottesman | c189a39 | 2013-01-09 19:23:24 +0000 | [diff] [blame] | 2419 | |
| 2420 | DEBUG(dbgs() << "ObjCARCOpt::OptimizeIndividualCalls: Cloning " |
| 2421 | << *CInst << "\n" |
| 2422 | " And inserting " |
| 2423 | "clone at " << *InsertPos << "\n"); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2424 | Worklist.push_back(std::make_pair(Clone, Incoming)); |
| 2425 | } |
| 2426 | } |
| 2427 | // Erase the original call. |
Michael Gottesman | c189a39 | 2013-01-09 19:23:24 +0000 | [diff] [blame] | 2428 | DEBUG(dbgs() << "Erasing: " << *CInst << "\n"); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2429 | EraseInstruction(CInst); |
| 2430 | continue; |
| 2431 | } |
| 2432 | } |
| 2433 | } while (!Worklist.empty()); |
| 2434 | } |
Michael Gottesman | b24bdef | 2013-01-12 02:57:16 +0000 | [diff] [blame] | 2435 | DEBUG(dbgs() << "ObjCARCOpt::OptimizeIndividualCalls: Finished List.\n"); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2436 | } |
| 2437 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 2438 | /// Check for critical edges, loop boundaries, irreducible control flow, or |
| 2439 | /// other CFG structures where moving code across the edge would result in it |
| 2440 | /// being executed more. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2441 | void |
| 2442 | ObjCARCOpt::CheckForCFGHazards(const BasicBlock *BB, |
| 2443 | DenseMap<const BasicBlock *, BBState> &BBStates, |
| 2444 | BBState &MyStates) const { |
| 2445 | // If any top-down local-use or possible-dec has a succ which is earlier in |
| 2446 | // the sequence, forget it. |
Dan Gohman | 55b0674 | 2012-03-02 01:13:53 +0000 | [diff] [blame] | 2447 | for (BBState::ptr_iterator I = MyStates.top_down_ptr_begin(), |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2448 | E = MyStates.top_down_ptr_end(); I != E; ++I) |
| 2449 | switch (I->second.GetSeq()) { |
| 2450 | default: break; |
| 2451 | case S_Use: { |
| 2452 | const Value *Arg = I->first; |
| 2453 | const TerminatorInst *TI = cast<TerminatorInst>(&BB->back()); |
| 2454 | bool SomeSuccHasSame = false; |
| 2455 | bool AllSuccsHaveSame = true; |
Dan Gohman | 55b0674 | 2012-03-02 01:13:53 +0000 | [diff] [blame] | 2456 | PtrState &S = I->second; |
Dan Gohman | 0155f30 | 2012-02-17 18:59:53 +0000 | [diff] [blame] | 2457 | succ_const_iterator SI(TI), SE(TI, false); |
| 2458 | |
Dan Gohman | 0155f30 | 2012-02-17 18:59:53 +0000 | [diff] [blame] | 2459 | for (; SI != SE; ++SI) { |
Dan Gohman | 362eb69 | 2012-03-02 01:26:46 +0000 | [diff] [blame] | 2460 | Sequence SuccSSeq = S_None; |
| 2461 | bool SuccSRRIKnownSafe = false; |
Dan Gohman | 41375a3 | 2012-05-08 23:39:44 +0000 | [diff] [blame] | 2462 | // If VisitBottomUp has pointer information for this successor, take |
| 2463 | // what we know about it. |
Dan Gohman | dae3349 | 2012-04-27 18:56:31 +0000 | [diff] [blame] | 2464 | DenseMap<const BasicBlock *, BBState>::iterator BBI = |
| 2465 | BBStates.find(*SI); |
| 2466 | assert(BBI != BBStates.end()); |
| 2467 | const PtrState &SuccS = BBI->second.getPtrBottomUpState(Arg); |
| 2468 | SuccSSeq = SuccS.GetSeq(); |
| 2469 | SuccSRRIKnownSafe = SuccS.RRI.KnownSafe; |
Dan Gohman | 362eb69 | 2012-03-02 01:26:46 +0000 | [diff] [blame] | 2470 | switch (SuccSSeq) { |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2471 | case S_None: |
Dan Gohman | 1213027 | 2011-08-12 00:26:31 +0000 | [diff] [blame] | 2472 | case S_CanRelease: { |
Dan Gohman | 362eb69 | 2012-03-02 01:26:46 +0000 | [diff] [blame] | 2473 | if (!S.RRI.KnownSafe && !SuccSRRIKnownSafe) { |
Dan Gohman | 1213027 | 2011-08-12 00:26:31 +0000 | [diff] [blame] | 2474 | S.ClearSequenceProgress(); |
Dan Gohman | 362eb69 | 2012-03-02 01:26:46 +0000 | [diff] [blame] | 2475 | break; |
| 2476 | } |
Dan Gohman | 1213027 | 2011-08-12 00:26:31 +0000 | [diff] [blame] | 2477 | continue; |
| 2478 | } |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2479 | case S_Use: |
| 2480 | SomeSuccHasSame = true; |
| 2481 | break; |
| 2482 | case S_Stop: |
| 2483 | case S_Release: |
| 2484 | case S_MovableRelease: |
Dan Gohman | 362eb69 | 2012-03-02 01:26:46 +0000 | [diff] [blame] | 2485 | if (!S.RRI.KnownSafe && !SuccSRRIKnownSafe) |
Dan Gohman | 1213027 | 2011-08-12 00:26:31 +0000 | [diff] [blame] | 2486 | AllSuccsHaveSame = false; |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2487 | break; |
| 2488 | case S_Retain: |
| 2489 | llvm_unreachable("bottom-up pointer in retain state!"); |
| 2490 | } |
Dan Gohman | 1213027 | 2011-08-12 00:26:31 +0000 | [diff] [blame] | 2491 | } |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2492 | // If the state at the other end of any of the successor edges |
| 2493 | // matches the current state, require all edges to match. This |
| 2494 | // guards against loops in the middle of a sequence. |
| 2495 | if (SomeSuccHasSame && !AllSuccsHaveSame) |
Dan Gohman | 1213027 | 2011-08-12 00:26:31 +0000 | [diff] [blame] | 2496 | S.ClearSequenceProgress(); |
Dan Gohman | 04443706 | 2011-12-12 18:13:53 +0000 | [diff] [blame] | 2497 | break; |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2498 | } |
| 2499 | case S_CanRelease: { |
| 2500 | const Value *Arg = I->first; |
| 2501 | const TerminatorInst *TI = cast<TerminatorInst>(&BB->back()); |
| 2502 | bool SomeSuccHasSame = false; |
| 2503 | bool AllSuccsHaveSame = true; |
Dan Gohman | 55b0674 | 2012-03-02 01:13:53 +0000 | [diff] [blame] | 2504 | PtrState &S = I->second; |
Dan Gohman | 0155f30 | 2012-02-17 18:59:53 +0000 | [diff] [blame] | 2505 | succ_const_iterator SI(TI), SE(TI, false); |
| 2506 | |
Dan Gohman | 0155f30 | 2012-02-17 18:59:53 +0000 | [diff] [blame] | 2507 | for (; SI != SE; ++SI) { |
Dan Gohman | 362eb69 | 2012-03-02 01:26:46 +0000 | [diff] [blame] | 2508 | Sequence SuccSSeq = S_None; |
| 2509 | bool SuccSRRIKnownSafe = false; |
Dan Gohman | 41375a3 | 2012-05-08 23:39:44 +0000 | [diff] [blame] | 2510 | // If VisitBottomUp has pointer information for this successor, take |
| 2511 | // what we know about it. |
Dan Gohman | dae3349 | 2012-04-27 18:56:31 +0000 | [diff] [blame] | 2512 | DenseMap<const BasicBlock *, BBState>::iterator BBI = |
| 2513 | BBStates.find(*SI); |
| 2514 | assert(BBI != BBStates.end()); |
| 2515 | const PtrState &SuccS = BBI->second.getPtrBottomUpState(Arg); |
| 2516 | SuccSSeq = SuccS.GetSeq(); |
| 2517 | SuccSRRIKnownSafe = SuccS.RRI.KnownSafe; |
Dan Gohman | 362eb69 | 2012-03-02 01:26:46 +0000 | [diff] [blame] | 2518 | switch (SuccSSeq) { |
Dan Gohman | 1213027 | 2011-08-12 00:26:31 +0000 | [diff] [blame] | 2519 | case S_None: { |
Dan Gohman | 362eb69 | 2012-03-02 01:26:46 +0000 | [diff] [blame] | 2520 | if (!S.RRI.KnownSafe && !SuccSRRIKnownSafe) { |
Dan Gohman | 1213027 | 2011-08-12 00:26:31 +0000 | [diff] [blame] | 2521 | S.ClearSequenceProgress(); |
Dan Gohman | 362eb69 | 2012-03-02 01:26:46 +0000 | [diff] [blame] | 2522 | break; |
| 2523 | } |
Dan Gohman | 1213027 | 2011-08-12 00:26:31 +0000 | [diff] [blame] | 2524 | continue; |
| 2525 | } |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2526 | case S_CanRelease: |
| 2527 | SomeSuccHasSame = true; |
| 2528 | break; |
| 2529 | case S_Stop: |
| 2530 | case S_Release: |
| 2531 | case S_MovableRelease: |
| 2532 | case S_Use: |
Dan Gohman | 362eb69 | 2012-03-02 01:26:46 +0000 | [diff] [blame] | 2533 | if (!S.RRI.KnownSafe && !SuccSRRIKnownSafe) |
Dan Gohman | 1213027 | 2011-08-12 00:26:31 +0000 | [diff] [blame] | 2534 | AllSuccsHaveSame = false; |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2535 | break; |
| 2536 | case S_Retain: |
| 2537 | llvm_unreachable("bottom-up pointer in retain state!"); |
| 2538 | } |
Dan Gohman | 1213027 | 2011-08-12 00:26:31 +0000 | [diff] [blame] | 2539 | } |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2540 | // If the state at the other end of any of the successor edges |
| 2541 | // matches the current state, require all edges to match. This |
| 2542 | // guards against loops in the middle of a sequence. |
| 2543 | if (SomeSuccHasSame && !AllSuccsHaveSame) |
Dan Gohman | 1213027 | 2011-08-12 00:26:31 +0000 | [diff] [blame] | 2544 | S.ClearSequenceProgress(); |
Dan Gohman | 04443706 | 2011-12-12 18:13:53 +0000 | [diff] [blame] | 2545 | break; |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2546 | } |
| 2547 | } |
| 2548 | } |
| 2549 | |
| 2550 | bool |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 2551 | ObjCARCOpt::VisitInstructionBottomUp(Instruction *Inst, |
Dan Gohman | 5c70fad | 2012-03-23 17:47:54 +0000 | [diff] [blame] | 2552 | BasicBlock *BB, |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 2553 | MapVector<Value *, RRInfo> &Retains, |
| 2554 | BBState &MyStates) { |
| 2555 | bool NestingDetected = false; |
| 2556 | InstructionClass Class = GetInstructionClass(Inst); |
| 2557 | const Value *Arg = 0; |
| 2558 | |
| 2559 | switch (Class) { |
| 2560 | case IC_Release: { |
| 2561 | Arg = GetObjCArg(Inst); |
| 2562 | |
| 2563 | PtrState &S = MyStates.getPtrBottomUpState(Arg); |
| 2564 | |
| 2565 | // If we see two releases in a row on the same pointer. If so, make |
| 2566 | // a note, and we'll cicle back to revisit it after we've |
| 2567 | // hopefully eliminated the second release, which may allow us to |
| 2568 | // eliminate the first release too. |
| 2569 | // Theoretically we could implement removal of nested retain+release |
| 2570 | // pairs by making PtrState hold a stack of states, but this is |
| 2571 | // simple and avoids adding overhead for the non-nested case. |
Michael Gottesman | af2113f | 2013-01-13 07:00:51 +0000 | [diff] [blame] | 2572 | if (S.GetSeq() == S_Release || S.GetSeq() == S_MovableRelease) { |
| 2573 | DEBUG(dbgs() << "ObjCARCOpt::VisitInstructionBottomUp: Found nested " |
| 2574 | "releases (i.e. a release pair)\n"); |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 2575 | NestingDetected = true; |
Michael Gottesman | af2113f | 2013-01-13 07:00:51 +0000 | [diff] [blame] | 2576 | } |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 2577 | |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 2578 | MDNode *ReleaseMetadata = Inst->getMetadata(ImpreciseReleaseMDKind); |
Dan Gohman | 62079b4 | 2012-04-25 00:50:46 +0000 | [diff] [blame] | 2579 | S.ResetSequenceProgress(ReleaseMetadata ? S_MovableRelease : S_Release); |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 2580 | S.RRI.ReleaseMetadata = ReleaseMetadata; |
Dan Gohman | df476e5 | 2012-09-04 23:16:20 +0000 | [diff] [blame] | 2581 | S.RRI.KnownSafe = S.IsKnownIncremented(); |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 2582 | S.RRI.IsTailCallRelease = cast<CallInst>(Inst)->isTailCall(); |
| 2583 | S.RRI.Calls.insert(Inst); |
| 2584 | |
Dan Gohman | df476e5 | 2012-09-04 23:16:20 +0000 | [diff] [blame] | 2585 | S.SetKnownPositiveRefCount(); |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 2586 | break; |
| 2587 | } |
| 2588 | case IC_RetainBlock: |
| 2589 | // An objc_retainBlock call with just a use may need to be kept, |
| 2590 | // because it may be copying a block from the stack to the heap. |
| 2591 | if (!IsRetainBlockOptimizable(Inst)) |
| 2592 | break; |
| 2593 | // FALLTHROUGH |
| 2594 | case IC_Retain: |
| 2595 | case IC_RetainRV: { |
| 2596 | Arg = GetObjCArg(Inst); |
| 2597 | |
| 2598 | PtrState &S = MyStates.getPtrBottomUpState(Arg); |
Dan Gohman | 62079b4 | 2012-04-25 00:50:46 +0000 | [diff] [blame] | 2599 | S.SetKnownPositiveRefCount(); |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 2600 | |
| 2601 | switch (S.GetSeq()) { |
| 2602 | case S_Stop: |
| 2603 | case S_Release: |
| 2604 | case S_MovableRelease: |
| 2605 | case S_Use: |
| 2606 | S.RRI.ReverseInsertPts.clear(); |
| 2607 | // FALL THROUGH |
| 2608 | case S_CanRelease: |
| 2609 | // Don't do retain+release tracking for IC_RetainRV, because it's |
| 2610 | // better to let it remain as the first instruction after a call. |
| 2611 | if (Class != IC_RetainRV) { |
| 2612 | S.RRI.IsRetainBlock = Class == IC_RetainBlock; |
| 2613 | Retains[Inst] = S.RRI; |
| 2614 | } |
| 2615 | S.ClearSequenceProgress(); |
| 2616 | break; |
| 2617 | case S_None: |
| 2618 | break; |
| 2619 | case S_Retain: |
| 2620 | llvm_unreachable("bottom-up pointer in retain state!"); |
| 2621 | } |
| 2622 | return NestingDetected; |
| 2623 | } |
| 2624 | case IC_AutoreleasepoolPop: |
| 2625 | // Conservatively, clear MyStates for all known pointers. |
| 2626 | MyStates.clearBottomUpPointers(); |
| 2627 | return NestingDetected; |
| 2628 | case IC_AutoreleasepoolPush: |
| 2629 | case IC_None: |
| 2630 | // These are irrelevant. |
| 2631 | return NestingDetected; |
| 2632 | default: |
| 2633 | break; |
| 2634 | } |
| 2635 | |
| 2636 | // Consider any other possible effects of this instruction on each |
| 2637 | // pointer being tracked. |
| 2638 | for (BBState::ptr_iterator MI = MyStates.bottom_up_ptr_begin(), |
| 2639 | ME = MyStates.bottom_up_ptr_end(); MI != ME; ++MI) { |
| 2640 | const Value *Ptr = MI->first; |
| 2641 | if (Ptr == Arg) |
| 2642 | continue; // Handled above. |
| 2643 | PtrState &S = MI->second; |
| 2644 | Sequence Seq = S.GetSeq(); |
| 2645 | |
| 2646 | // Check for possible releases. |
| 2647 | if (CanAlterRefCount(Inst, Ptr, PA, Class)) { |
Dan Gohman | 62079b4 | 2012-04-25 00:50:46 +0000 | [diff] [blame] | 2648 | S.ClearRefCount(); |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 2649 | switch (Seq) { |
| 2650 | case S_Use: |
| 2651 | S.SetSeq(S_CanRelease); |
| 2652 | continue; |
| 2653 | case S_CanRelease: |
| 2654 | case S_Release: |
| 2655 | case S_MovableRelease: |
| 2656 | case S_Stop: |
| 2657 | case S_None: |
| 2658 | break; |
| 2659 | case S_Retain: |
| 2660 | llvm_unreachable("bottom-up pointer in retain state!"); |
| 2661 | } |
| 2662 | } |
| 2663 | |
| 2664 | // Check for possible direct uses. |
| 2665 | switch (Seq) { |
| 2666 | case S_Release: |
| 2667 | case S_MovableRelease: |
| 2668 | if (CanUse(Inst, Ptr, PA, Class)) { |
| 2669 | assert(S.RRI.ReverseInsertPts.empty()); |
Dan Gohman | 5c70fad | 2012-03-23 17:47:54 +0000 | [diff] [blame] | 2670 | // If this is an invoke instruction, we're scanning it as part of |
| 2671 | // one of its successor blocks, since we can't insert code after it |
| 2672 | // in its own block, and we don't want to split critical edges. |
| 2673 | if (isa<InvokeInst>(Inst)) |
| 2674 | S.RRI.ReverseInsertPts.insert(BB->getFirstInsertionPt()); |
| 2675 | else |
Francois Pichet | 4b9ab74 | 2012-03-24 01:36:37 +0000 | [diff] [blame] | 2676 | S.RRI.ReverseInsertPts.insert(llvm::next(BasicBlock::iterator(Inst))); |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 2677 | S.SetSeq(S_Use); |
| 2678 | } else if (Seq == S_Release && |
| 2679 | (Class == IC_User || Class == IC_CallOrUser)) { |
| 2680 | // Non-movable releases depend on any possible objc pointer use. |
| 2681 | S.SetSeq(S_Stop); |
| 2682 | assert(S.RRI.ReverseInsertPts.empty()); |
Dan Gohman | 5c70fad | 2012-03-23 17:47:54 +0000 | [diff] [blame] | 2683 | // As above; handle invoke specially. |
| 2684 | if (isa<InvokeInst>(Inst)) |
| 2685 | S.RRI.ReverseInsertPts.insert(BB->getFirstInsertionPt()); |
| 2686 | else |
Francois Pichet | 4b9ab74 | 2012-03-24 01:36:37 +0000 | [diff] [blame] | 2687 | S.RRI.ReverseInsertPts.insert(llvm::next(BasicBlock::iterator(Inst))); |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 2688 | } |
| 2689 | break; |
| 2690 | case S_Stop: |
| 2691 | if (CanUse(Inst, Ptr, PA, Class)) |
| 2692 | S.SetSeq(S_Use); |
| 2693 | break; |
| 2694 | case S_CanRelease: |
| 2695 | case S_Use: |
| 2696 | case S_None: |
| 2697 | break; |
| 2698 | case S_Retain: |
| 2699 | llvm_unreachable("bottom-up pointer in retain state!"); |
| 2700 | } |
| 2701 | } |
| 2702 | |
| 2703 | return NestingDetected; |
| 2704 | } |
| 2705 | |
| 2706 | bool |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2707 | ObjCARCOpt::VisitBottomUp(BasicBlock *BB, |
| 2708 | DenseMap<const BasicBlock *, BBState> &BBStates, |
| 2709 | MapVector<Value *, RRInfo> &Retains) { |
| 2710 | bool NestingDetected = false; |
| 2711 | BBState &MyStates = BBStates[BB]; |
| 2712 | |
| 2713 | // Merge the states from each successor to compute the initial state |
| 2714 | // for the current block. |
Dan Gohman | 10c82ce | 2012-08-27 18:31:36 +0000 | [diff] [blame] | 2715 | BBState::edge_iterator SI(MyStates.succ_begin()), |
| 2716 | SE(MyStates.succ_end()); |
| 2717 | if (SI != SE) { |
Dan Gohman | c24c66f | 2012-04-24 22:53:18 +0000 | [diff] [blame] | 2718 | const BasicBlock *Succ = *SI; |
| 2719 | DenseMap<const BasicBlock *, BBState>::iterator I = BBStates.find(Succ); |
| 2720 | assert(I != BBStates.end()); |
| 2721 | MyStates.InitFromSucc(I->second); |
| 2722 | ++SI; |
| 2723 | for (; SI != SE; ++SI) { |
| 2724 | Succ = *SI; |
| 2725 | I = BBStates.find(Succ); |
| 2726 | assert(I != BBStates.end()); |
| 2727 | MyStates.MergeSucc(I->second); |
| 2728 | } |
Dan Gohman | 0155f30 | 2012-02-17 18:59:53 +0000 | [diff] [blame] | 2729 | } |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2730 | |
| 2731 | // Visit all the instructions, bottom-up. |
| 2732 | for (BasicBlock::iterator I = BB->end(), E = BB->begin(); I != E; --I) { |
| 2733 | Instruction *Inst = llvm::prior(I); |
Dan Gohman | 5c70fad | 2012-03-23 17:47:54 +0000 | [diff] [blame] | 2734 | |
| 2735 | // Invoke instructions are visited as part of their successors (below). |
| 2736 | if (isa<InvokeInst>(Inst)) |
| 2737 | continue; |
| 2738 | |
Michael Gottesman | af2113f | 2013-01-13 07:00:51 +0000 | [diff] [blame] | 2739 | DEBUG(dbgs() << "ObjCARCOpt::VisitButtonUp: Visiting " << *Inst << "\n"); |
| 2740 | |
Dan Gohman | 5c70fad | 2012-03-23 17:47:54 +0000 | [diff] [blame] | 2741 | NestingDetected |= VisitInstructionBottomUp(Inst, BB, Retains, MyStates); |
| 2742 | } |
| 2743 | |
Dan Gohman | dae3349 | 2012-04-27 18:56:31 +0000 | [diff] [blame] | 2744 | // If there's a predecessor with an invoke, visit the invoke as if it were |
| 2745 | // part of this block, since we can't insert code after an invoke in its own |
| 2746 | // block, and we don't want to split critical edges. |
Dan Gohman | c24c66f | 2012-04-24 22:53:18 +0000 | [diff] [blame] | 2747 | for (BBState::edge_iterator PI(MyStates.pred_begin()), |
| 2748 | PE(MyStates.pred_end()); PI != PE; ++PI) { |
Dan Gohman | 5c70fad | 2012-03-23 17:47:54 +0000 | [diff] [blame] | 2749 | BasicBlock *Pred = *PI; |
Dan Gohman | dae3349 | 2012-04-27 18:56:31 +0000 | [diff] [blame] | 2750 | if (InvokeInst *II = dyn_cast<InvokeInst>(&Pred->back())) |
| 2751 | NestingDetected |= VisitInstructionBottomUp(II, BB, Retains, MyStates); |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 2752 | } |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2753 | |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 2754 | return NestingDetected; |
| 2755 | } |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2756 | |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 2757 | bool |
| 2758 | ObjCARCOpt::VisitInstructionTopDown(Instruction *Inst, |
| 2759 | DenseMap<Value *, RRInfo> &Releases, |
| 2760 | BBState &MyStates) { |
| 2761 | bool NestingDetected = false; |
| 2762 | InstructionClass Class = GetInstructionClass(Inst); |
| 2763 | const Value *Arg = 0; |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2764 | |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 2765 | switch (Class) { |
| 2766 | case IC_RetainBlock: |
| 2767 | // An objc_retainBlock call with just a use may need to be kept, |
| 2768 | // because it may be copying a block from the stack to the heap. |
| 2769 | if (!IsRetainBlockOptimizable(Inst)) |
| 2770 | break; |
| 2771 | // FALLTHROUGH |
| 2772 | case IC_Retain: |
| 2773 | case IC_RetainRV: { |
| 2774 | Arg = GetObjCArg(Inst); |
| 2775 | |
| 2776 | PtrState &S = MyStates.getPtrTopDownState(Arg); |
| 2777 | |
| 2778 | // Don't do retain+release tracking for IC_RetainRV, because it's |
| 2779 | // better to let it remain as the first instruction after a call. |
| 2780 | if (Class != IC_RetainRV) { |
| 2781 | // If we see two retains in a row on the same pointer. If so, make |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2782 | // a note, and we'll cicle back to revisit it after we've |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 2783 | // hopefully eliminated the second retain, which may allow us to |
| 2784 | // eliminate the first retain too. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2785 | // Theoretically we could implement removal of nested retain+release |
| 2786 | // pairs by making PtrState hold a stack of states, but this is |
| 2787 | // simple and avoids adding overhead for the non-nested case. |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 2788 | if (S.GetSeq() == S_Retain) |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2789 | NestingDetected = true; |
| 2790 | |
Dan Gohman | 62079b4 | 2012-04-25 00:50:46 +0000 | [diff] [blame] | 2791 | S.ResetSequenceProgress(S_Retain); |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 2792 | S.RRI.IsRetainBlock = Class == IC_RetainBlock; |
Dan Gohman | df476e5 | 2012-09-04 23:16:20 +0000 | [diff] [blame] | 2793 | S.RRI.KnownSafe = S.IsKnownIncremented(); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2794 | S.RRI.Calls.insert(Inst); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2795 | } |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2796 | |
Dan Gohman | df476e5 | 2012-09-04 23:16:20 +0000 | [diff] [blame] | 2797 | S.SetKnownPositiveRefCount(); |
Dan Gohman | f64ff8e | 2012-07-23 19:27:31 +0000 | [diff] [blame] | 2798 | |
| 2799 | // A retain can be a potential use; procede to the generic checking |
| 2800 | // code below. |
| 2801 | break; |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 2802 | } |
| 2803 | case IC_Release: { |
| 2804 | Arg = GetObjCArg(Inst); |
| 2805 | |
| 2806 | PtrState &S = MyStates.getPtrTopDownState(Arg); |
Dan Gohman | df476e5 | 2012-09-04 23:16:20 +0000 | [diff] [blame] | 2807 | S.ClearRefCount(); |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 2808 | |
| 2809 | switch (S.GetSeq()) { |
| 2810 | case S_Retain: |
| 2811 | case S_CanRelease: |
| 2812 | S.RRI.ReverseInsertPts.clear(); |
| 2813 | // FALL THROUGH |
| 2814 | case S_Use: |
| 2815 | S.RRI.ReleaseMetadata = Inst->getMetadata(ImpreciseReleaseMDKind); |
| 2816 | S.RRI.IsTailCallRelease = cast<CallInst>(Inst)->isTailCall(); |
| 2817 | Releases[Inst] = S.RRI; |
| 2818 | S.ClearSequenceProgress(); |
| 2819 | break; |
| 2820 | case S_None: |
| 2821 | break; |
| 2822 | case S_Stop: |
| 2823 | case S_Release: |
| 2824 | case S_MovableRelease: |
| 2825 | llvm_unreachable("top-down pointer in release state!"); |
| 2826 | } |
| 2827 | break; |
| 2828 | } |
| 2829 | case IC_AutoreleasepoolPop: |
| 2830 | // Conservatively, clear MyStates for all known pointers. |
| 2831 | MyStates.clearTopDownPointers(); |
| 2832 | return NestingDetected; |
| 2833 | case IC_AutoreleasepoolPush: |
| 2834 | case IC_None: |
| 2835 | // These are irrelevant. |
| 2836 | return NestingDetected; |
| 2837 | default: |
| 2838 | break; |
| 2839 | } |
| 2840 | |
| 2841 | // Consider any other possible effects of this instruction on each |
| 2842 | // pointer being tracked. |
| 2843 | for (BBState::ptr_iterator MI = MyStates.top_down_ptr_begin(), |
| 2844 | ME = MyStates.top_down_ptr_end(); MI != ME; ++MI) { |
| 2845 | const Value *Ptr = MI->first; |
| 2846 | if (Ptr == Arg) |
| 2847 | continue; // Handled above. |
| 2848 | PtrState &S = MI->second; |
| 2849 | Sequence Seq = S.GetSeq(); |
| 2850 | |
| 2851 | // Check for possible releases. |
| 2852 | if (CanAlterRefCount(Inst, Ptr, PA, Class)) { |
Dan Gohman | 62079b4 | 2012-04-25 00:50:46 +0000 | [diff] [blame] | 2853 | S.ClearRefCount(); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2854 | switch (Seq) { |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 2855 | case S_Retain: |
| 2856 | S.SetSeq(S_CanRelease); |
| 2857 | assert(S.RRI.ReverseInsertPts.empty()); |
| 2858 | S.RRI.ReverseInsertPts.insert(Inst); |
| 2859 | |
| 2860 | // One call can't cause a transition from S_Retain to S_CanRelease |
| 2861 | // and S_CanRelease to S_Use. If we've made the first transition, |
| 2862 | // we're done. |
| 2863 | continue; |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2864 | case S_Use: |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 2865 | case S_CanRelease: |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2866 | case S_None: |
| 2867 | break; |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 2868 | case S_Stop: |
| 2869 | case S_Release: |
| 2870 | case S_MovableRelease: |
| 2871 | llvm_unreachable("top-down pointer in release state!"); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2872 | } |
| 2873 | } |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 2874 | |
| 2875 | // Check for possible direct uses. |
| 2876 | switch (Seq) { |
| 2877 | case S_CanRelease: |
| 2878 | if (CanUse(Inst, Ptr, PA, Class)) |
| 2879 | S.SetSeq(S_Use); |
| 2880 | break; |
| 2881 | case S_Retain: |
| 2882 | case S_Use: |
| 2883 | case S_None: |
| 2884 | break; |
| 2885 | case S_Stop: |
| 2886 | case S_Release: |
| 2887 | case S_MovableRelease: |
| 2888 | llvm_unreachable("top-down pointer in release state!"); |
| 2889 | } |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2890 | } |
| 2891 | |
| 2892 | return NestingDetected; |
| 2893 | } |
| 2894 | |
| 2895 | bool |
| 2896 | ObjCARCOpt::VisitTopDown(BasicBlock *BB, |
| 2897 | DenseMap<const BasicBlock *, BBState> &BBStates, |
| 2898 | DenseMap<Value *, RRInfo> &Releases) { |
| 2899 | bool NestingDetected = false; |
| 2900 | BBState &MyStates = BBStates[BB]; |
| 2901 | |
| 2902 | // Merge the states from each predecessor to compute the initial state |
| 2903 | // for the current block. |
Dan Gohman | 10c82ce | 2012-08-27 18:31:36 +0000 | [diff] [blame] | 2904 | BBState::edge_iterator PI(MyStates.pred_begin()), |
| 2905 | PE(MyStates.pred_end()); |
| 2906 | if (PI != PE) { |
Dan Gohman | c24c66f | 2012-04-24 22:53:18 +0000 | [diff] [blame] | 2907 | const BasicBlock *Pred = *PI; |
| 2908 | DenseMap<const BasicBlock *, BBState>::iterator I = BBStates.find(Pred); |
| 2909 | assert(I != BBStates.end()); |
| 2910 | MyStates.InitFromPred(I->second); |
| 2911 | ++PI; |
| 2912 | for (; PI != PE; ++PI) { |
| 2913 | Pred = *PI; |
| 2914 | I = BBStates.find(Pred); |
| 2915 | assert(I != BBStates.end()); |
| 2916 | MyStates.MergePred(I->second); |
| 2917 | } |
Dan Gohman | c24c66f | 2012-04-24 22:53:18 +0000 | [diff] [blame] | 2918 | } |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2919 | |
| 2920 | // Visit all the instructions, top-down. |
| 2921 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) { |
| 2922 | Instruction *Inst = I; |
Michael Gottesman | af2113f | 2013-01-13 07:00:51 +0000 | [diff] [blame] | 2923 | |
| 2924 | DEBUG(dbgs() << "ObjCARCOpt::VisitTopDown: Visiting " << *Inst << "\n"); |
| 2925 | |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 2926 | NestingDetected |= VisitInstructionTopDown(Inst, Releases, MyStates); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2927 | } |
| 2928 | |
| 2929 | CheckForCFGHazards(BB, BBStates, MyStates); |
| 2930 | return NestingDetected; |
| 2931 | } |
| 2932 | |
Dan Gohman | a53a12c | 2011-12-12 19:42:25 +0000 | [diff] [blame] | 2933 | static void |
| 2934 | ComputePostOrders(Function &F, |
| 2935 | SmallVectorImpl<BasicBlock *> &PostOrder, |
Dan Gohman | c24c66f | 2012-04-24 22:53:18 +0000 | [diff] [blame] | 2936 | SmallVectorImpl<BasicBlock *> &ReverseCFGPostOrder, |
| 2937 | unsigned NoObjCARCExceptionsMDKind, |
| 2938 | DenseMap<const BasicBlock *, BBState> &BBStates) { |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 2939 | /// The visited set, for doing DFS walks. |
Dan Gohman | a53a12c | 2011-12-12 19:42:25 +0000 | [diff] [blame] | 2940 | SmallPtrSet<BasicBlock *, 16> Visited; |
| 2941 | |
| 2942 | // Do DFS, computing the PostOrder. |
| 2943 | SmallPtrSet<BasicBlock *, 16> OnStack; |
| 2944 | SmallVector<std::pair<BasicBlock *, succ_iterator>, 16> SuccStack; |
Dan Gohman | c24c66f | 2012-04-24 22:53:18 +0000 | [diff] [blame] | 2945 | |
| 2946 | // Functions always have exactly one entry block, and we don't have |
| 2947 | // any other block that we treat like an entry block. |
Dan Gohman | a53a12c | 2011-12-12 19:42:25 +0000 | [diff] [blame] | 2948 | BasicBlock *EntryBB = &F.getEntryBlock(); |
Dan Gohman | 41375a3 | 2012-05-08 23:39:44 +0000 | [diff] [blame] | 2949 | BBState &MyStates = BBStates[EntryBB]; |
| 2950 | MyStates.SetAsEntry(); |
| 2951 | TerminatorInst *EntryTI = cast<TerminatorInst>(&EntryBB->back()); |
| 2952 | SuccStack.push_back(std::make_pair(EntryBB, succ_iterator(EntryTI))); |
Dan Gohman | a53a12c | 2011-12-12 19:42:25 +0000 | [diff] [blame] | 2953 | Visited.insert(EntryBB); |
| 2954 | OnStack.insert(EntryBB); |
| 2955 | do { |
| 2956 | dfs_next_succ: |
Dan Gohman | c24c66f | 2012-04-24 22:53:18 +0000 | [diff] [blame] | 2957 | BasicBlock *CurrBB = SuccStack.back().first; |
| 2958 | TerminatorInst *TI = cast<TerminatorInst>(&CurrBB->back()); |
| 2959 | succ_iterator SE(TI, false); |
Dan Gohman | 41375a3 | 2012-05-08 23:39:44 +0000 | [diff] [blame] | 2960 | |
Dan Gohman | c24c66f | 2012-04-24 22:53:18 +0000 | [diff] [blame] | 2961 | while (SuccStack.back().second != SE) { |
| 2962 | BasicBlock *SuccBB = *SuccStack.back().second++; |
| 2963 | if (Visited.insert(SuccBB)) { |
Dan Gohman | 41375a3 | 2012-05-08 23:39:44 +0000 | [diff] [blame] | 2964 | TerminatorInst *TI = cast<TerminatorInst>(&SuccBB->back()); |
| 2965 | SuccStack.push_back(std::make_pair(SuccBB, succ_iterator(TI))); |
Dan Gohman | c24c66f | 2012-04-24 22:53:18 +0000 | [diff] [blame] | 2966 | BBStates[CurrBB].addSucc(SuccBB); |
Dan Gohman | 41375a3 | 2012-05-08 23:39:44 +0000 | [diff] [blame] | 2967 | BBState &SuccStates = BBStates[SuccBB]; |
| 2968 | SuccStates.addPred(CurrBB); |
Dan Gohman | c24c66f | 2012-04-24 22:53:18 +0000 | [diff] [blame] | 2969 | OnStack.insert(SuccBB); |
Dan Gohman | a53a12c | 2011-12-12 19:42:25 +0000 | [diff] [blame] | 2970 | goto dfs_next_succ; |
| 2971 | } |
Dan Gohman | c24c66f | 2012-04-24 22:53:18 +0000 | [diff] [blame] | 2972 | |
| 2973 | if (!OnStack.count(SuccBB)) { |
| 2974 | BBStates[CurrBB].addSucc(SuccBB); |
| 2975 | BBStates[SuccBB].addPred(CurrBB); |
| 2976 | } |
Dan Gohman | a53a12c | 2011-12-12 19:42:25 +0000 | [diff] [blame] | 2977 | } |
Dan Gohman | c24c66f | 2012-04-24 22:53:18 +0000 | [diff] [blame] | 2978 | OnStack.erase(CurrBB); |
| 2979 | PostOrder.push_back(CurrBB); |
| 2980 | SuccStack.pop_back(); |
Dan Gohman | a53a12c | 2011-12-12 19:42:25 +0000 | [diff] [blame] | 2981 | } while (!SuccStack.empty()); |
| 2982 | |
| 2983 | Visited.clear(); |
| 2984 | |
Dan Gohman | a53a12c | 2011-12-12 19:42:25 +0000 | [diff] [blame] | 2985 | // Do reverse-CFG DFS, computing the reverse-CFG PostOrder. |
Dan Gohman | c24c66f | 2012-04-24 22:53:18 +0000 | [diff] [blame] | 2986 | // Functions may have many exits, and there also blocks which we treat |
| 2987 | // as exits due to ignored edges. |
| 2988 | SmallVector<std::pair<BasicBlock *, BBState::edge_iterator>, 16> PredStack; |
| 2989 | for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) { |
| 2990 | BasicBlock *ExitBB = I; |
| 2991 | BBState &MyStates = BBStates[ExitBB]; |
| 2992 | if (!MyStates.isExit()) |
| 2993 | continue; |
| 2994 | |
Dan Gohman | dae3349 | 2012-04-27 18:56:31 +0000 | [diff] [blame] | 2995 | MyStates.SetAsExit(); |
Dan Gohman | c24c66f | 2012-04-24 22:53:18 +0000 | [diff] [blame] | 2996 | |
| 2997 | PredStack.push_back(std::make_pair(ExitBB, MyStates.pred_begin())); |
Dan Gohman | a53a12c | 2011-12-12 19:42:25 +0000 | [diff] [blame] | 2998 | Visited.insert(ExitBB); |
| 2999 | while (!PredStack.empty()) { |
| 3000 | reverse_dfs_next_succ: |
Dan Gohman | c24c66f | 2012-04-24 22:53:18 +0000 | [diff] [blame] | 3001 | BBState::edge_iterator PE = BBStates[PredStack.back().first].pred_end(); |
| 3002 | while (PredStack.back().second != PE) { |
Dan Gohman | a53a12c | 2011-12-12 19:42:25 +0000 | [diff] [blame] | 3003 | BasicBlock *BB = *PredStack.back().second++; |
Dan Gohman | a53a12c | 2011-12-12 19:42:25 +0000 | [diff] [blame] | 3004 | if (Visited.insert(BB)) { |
Dan Gohman | c24c66f | 2012-04-24 22:53:18 +0000 | [diff] [blame] | 3005 | PredStack.push_back(std::make_pair(BB, BBStates[BB].pred_begin())); |
Dan Gohman | a53a12c | 2011-12-12 19:42:25 +0000 | [diff] [blame] | 3006 | goto reverse_dfs_next_succ; |
| 3007 | } |
| 3008 | } |
| 3009 | ReverseCFGPostOrder.push_back(PredStack.pop_back_val().first); |
| 3010 | } |
| 3011 | } |
| 3012 | } |
| 3013 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 3014 | // Visit the function both top-down and bottom-up. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 3015 | bool |
| 3016 | ObjCARCOpt::Visit(Function &F, |
| 3017 | DenseMap<const BasicBlock *, BBState> &BBStates, |
| 3018 | MapVector<Value *, RRInfo> &Retains, |
| 3019 | DenseMap<Value *, RRInfo> &Releases) { |
Dan Gohman | a53a12c | 2011-12-12 19:42:25 +0000 | [diff] [blame] | 3020 | |
| 3021 | // Use reverse-postorder traversals, because we magically know that loops |
| 3022 | // will be well behaved, i.e. they won't repeatedly call retain on a single |
| 3023 | // pointer without doing a release. We can't use the ReversePostOrderTraversal |
| 3024 | // class here because we want the reverse-CFG postorder to consider each |
| 3025 | // function exit point, and we want to ignore selected cycle edges. |
| 3026 | SmallVector<BasicBlock *, 16> PostOrder; |
| 3027 | SmallVector<BasicBlock *, 16> ReverseCFGPostOrder; |
Dan Gohman | c24c66f | 2012-04-24 22:53:18 +0000 | [diff] [blame] | 3028 | ComputePostOrders(F, PostOrder, ReverseCFGPostOrder, |
| 3029 | NoObjCARCExceptionsMDKind, |
| 3030 | BBStates); |
Dan Gohman | a53a12c | 2011-12-12 19:42:25 +0000 | [diff] [blame] | 3031 | |
| 3032 | // Use reverse-postorder on the reverse CFG for bottom-up. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 3033 | bool BottomUpNestingDetected = false; |
Dan Gohman | c57b58c | 2011-08-18 21:27:42 +0000 | [diff] [blame] | 3034 | for (SmallVectorImpl<BasicBlock *>::const_reverse_iterator I = |
Dan Gohman | a53a12c | 2011-12-12 19:42:25 +0000 | [diff] [blame] | 3035 | ReverseCFGPostOrder.rbegin(), E = ReverseCFGPostOrder.rend(); |
| 3036 | I != E; ++I) |
| 3037 | BottomUpNestingDetected |= VisitBottomUp(*I, BBStates, Retains); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 3038 | |
Dan Gohman | a53a12c | 2011-12-12 19:42:25 +0000 | [diff] [blame] | 3039 | // Use reverse-postorder for top-down. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 3040 | bool TopDownNestingDetected = false; |
Dan Gohman | a53a12c | 2011-12-12 19:42:25 +0000 | [diff] [blame] | 3041 | for (SmallVectorImpl<BasicBlock *>::const_reverse_iterator I = |
| 3042 | PostOrder.rbegin(), E = PostOrder.rend(); |
| 3043 | I != E; ++I) |
| 3044 | TopDownNestingDetected |= VisitTopDown(*I, BBStates, Releases); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 3045 | |
| 3046 | return TopDownNestingDetected && BottomUpNestingDetected; |
| 3047 | } |
| 3048 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 3049 | /// Move the calls in RetainsToMove and ReleasesToMove. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 3050 | void ObjCARCOpt::MoveCalls(Value *Arg, |
| 3051 | RRInfo &RetainsToMove, |
| 3052 | RRInfo &ReleasesToMove, |
| 3053 | MapVector<Value *, RRInfo> &Retains, |
| 3054 | DenseMap<Value *, RRInfo> &Releases, |
Dan Gohman | 6320f52 | 2011-07-22 22:29:21 +0000 | [diff] [blame] | 3055 | SmallVectorImpl<Instruction *> &DeadInsts, |
| 3056 | Module *M) { |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3057 | Type *ArgTy = Arg->getType(); |
Dan Gohman | 6320f52 | 2011-07-22 22:29:21 +0000 | [diff] [blame] | 3058 | Type *ParamTy = PointerType::getUnqual(Type::getInt8Ty(ArgTy->getContext())); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 3059 | |
| 3060 | // Insert the new retain and release calls. |
| 3061 | for (SmallPtrSet<Instruction *, 2>::const_iterator |
| 3062 | PI = ReleasesToMove.ReverseInsertPts.begin(), |
| 3063 | PE = ReleasesToMove.ReverseInsertPts.end(); PI != PE; ++PI) { |
| 3064 | Instruction *InsertPt = *PI; |
| 3065 | Value *MyArg = ArgTy == ParamTy ? Arg : |
| 3066 | new BitCastInst(Arg, ParamTy, "", InsertPt); |
| 3067 | CallInst *Call = |
| 3068 | CallInst::Create(RetainsToMove.IsRetainBlock ? |
Dan Gohman | 6320f52 | 2011-07-22 22:29:21 +0000 | [diff] [blame] | 3069 | getRetainBlockCallee(M) : getRetainCallee(M), |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 3070 | MyArg, "", InsertPt); |
| 3071 | Call->setDoesNotThrow(); |
Dan Gohman | 728db49 | 2012-01-13 00:39:07 +0000 | [diff] [blame] | 3072 | if (RetainsToMove.IsRetainBlock) |
Dan Gohman | a7107f9 | 2011-10-17 22:53:25 +0000 | [diff] [blame] | 3073 | Call->setMetadata(CopyOnEscapeMDKind, |
| 3074 | MDNode::get(M->getContext(), ArrayRef<Value *>())); |
Dan Gohman | 728db49 | 2012-01-13 00:39:07 +0000 | [diff] [blame] | 3075 | else |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 3076 | Call->setTailCall(); |
Michael Gottesman | c189a39 | 2013-01-09 19:23:24 +0000 | [diff] [blame] | 3077 | |
| 3078 | DEBUG(dbgs() << "ObjCARCOpt::MoveCalls: Inserting new Release: " << *Call |
| 3079 | << "\n" |
| 3080 | " At insertion point: " << *InsertPt |
| 3081 | << "\n"); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 3082 | } |
| 3083 | for (SmallPtrSet<Instruction *, 2>::const_iterator |
| 3084 | PI = RetainsToMove.ReverseInsertPts.begin(), |
| 3085 | PE = RetainsToMove.ReverseInsertPts.end(); PI != PE; ++PI) { |
Dan Gohman | 5c70fad | 2012-03-23 17:47:54 +0000 | [diff] [blame] | 3086 | Instruction *InsertPt = *PI; |
| 3087 | Value *MyArg = ArgTy == ParamTy ? Arg : |
| 3088 | new BitCastInst(Arg, ParamTy, "", InsertPt); |
| 3089 | CallInst *Call = CallInst::Create(getReleaseCallee(M), MyArg, |
| 3090 | "", InsertPt); |
| 3091 | // Attach a clang.imprecise_release metadata tag, if appropriate. |
| 3092 | if (MDNode *M = ReleasesToMove.ReleaseMetadata) |
| 3093 | Call->setMetadata(ImpreciseReleaseMDKind, M); |
| 3094 | Call->setDoesNotThrow(); |
| 3095 | if (ReleasesToMove.IsTailCallRelease) |
| 3096 | Call->setTailCall(); |
Michael Gottesman | c189a39 | 2013-01-09 19:23:24 +0000 | [diff] [blame] | 3097 | |
| 3098 | DEBUG(dbgs() << "ObjCARCOpt::MoveCalls: Inserting new Retain: " << *Call |
| 3099 | << "\n" |
| 3100 | " At insertion point: " << *InsertPt |
| 3101 | << "\n"); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 3102 | } |
| 3103 | |
| 3104 | // Delete the original retain and release calls. |
| 3105 | for (SmallPtrSet<Instruction *, 2>::const_iterator |
| 3106 | AI = RetainsToMove.Calls.begin(), |
| 3107 | AE = RetainsToMove.Calls.end(); AI != AE; ++AI) { |
| 3108 | Instruction *OrigRetain = *AI; |
| 3109 | Retains.blot(OrigRetain); |
| 3110 | DeadInsts.push_back(OrigRetain); |
Michael Gottesman | c189a39 | 2013-01-09 19:23:24 +0000 | [diff] [blame] | 3111 | DEBUG(dbgs() << "ObjCARCOpt::MoveCalls: Deleting retain: " << *OrigRetain << |
| 3112 | "\n"); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 3113 | } |
| 3114 | for (SmallPtrSet<Instruction *, 2>::const_iterator |
| 3115 | AI = ReleasesToMove.Calls.begin(), |
| 3116 | AE = ReleasesToMove.Calls.end(); AI != AE; ++AI) { |
| 3117 | Instruction *OrigRelease = *AI; |
| 3118 | Releases.erase(OrigRelease); |
| 3119 | DeadInsts.push_back(OrigRelease); |
Michael Gottesman | c189a39 | 2013-01-09 19:23:24 +0000 | [diff] [blame] | 3120 | DEBUG(dbgs() << "ObjCARCOpt::MoveCalls: Deleting release: " << *OrigRelease |
| 3121 | << "\n"); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 3122 | } |
| 3123 | } |
| 3124 | |
Michael Gottesman | 9de6f96 | 2013-01-22 21:49:00 +0000 | [diff] [blame] | 3125 | bool |
| 3126 | ObjCARCOpt::ConnectTDBUTraversals(DenseMap<const BasicBlock *, BBState> |
| 3127 | &BBStates, |
| 3128 | MapVector<Value *, RRInfo> &Retains, |
| 3129 | DenseMap<Value *, RRInfo> &Releases, |
| 3130 | Module *M, |
| 3131 | SmallVector<Instruction *, 4> &NewRetains, |
| 3132 | SmallVector<Instruction *, 4> &NewReleases, |
| 3133 | SmallVector<Instruction *, 8> &DeadInsts, |
| 3134 | RRInfo &RetainsToMove, |
| 3135 | RRInfo &ReleasesToMove, |
| 3136 | Value *Arg, |
| 3137 | bool KnownSafe, |
| 3138 | bool &AnyPairsCompletelyEliminated) { |
| 3139 | // If a pair happens in a region where it is known that the reference count |
| 3140 | // is already incremented, we can similarly ignore possible decrements. |
| 3141 | bool KnownSafeTD = true, KnownSafeBU = true; |
| 3142 | |
| 3143 | // Connect the dots between the top-down-collected RetainsToMove and |
| 3144 | // bottom-up-collected ReleasesToMove to form sets of related calls. |
| 3145 | // This is an iterative process so that we connect multiple releases |
| 3146 | // to multiple retains if needed. |
| 3147 | unsigned OldDelta = 0; |
| 3148 | unsigned NewDelta = 0; |
| 3149 | unsigned OldCount = 0; |
| 3150 | unsigned NewCount = 0; |
| 3151 | bool FirstRelease = true; |
| 3152 | bool FirstRetain = true; |
| 3153 | for (;;) { |
| 3154 | for (SmallVectorImpl<Instruction *>::const_iterator |
| 3155 | NI = NewRetains.begin(), NE = NewRetains.end(); NI != NE; ++NI) { |
| 3156 | Instruction *NewRetain = *NI; |
| 3157 | MapVector<Value *, RRInfo>::const_iterator It = Retains.find(NewRetain); |
| 3158 | assert(It != Retains.end()); |
| 3159 | const RRInfo &NewRetainRRI = It->second; |
| 3160 | KnownSafeTD &= NewRetainRRI.KnownSafe; |
| 3161 | for (SmallPtrSet<Instruction *, 2>::const_iterator |
| 3162 | LI = NewRetainRRI.Calls.begin(), |
| 3163 | LE = NewRetainRRI.Calls.end(); LI != LE; ++LI) { |
| 3164 | Instruction *NewRetainRelease = *LI; |
| 3165 | DenseMap<Value *, RRInfo>::const_iterator Jt = |
| 3166 | Releases.find(NewRetainRelease); |
| 3167 | if (Jt == Releases.end()) |
| 3168 | return false; |
| 3169 | const RRInfo &NewRetainReleaseRRI = Jt->second; |
| 3170 | assert(NewRetainReleaseRRI.Calls.count(NewRetain)); |
| 3171 | if (ReleasesToMove.Calls.insert(NewRetainRelease)) { |
| 3172 | OldDelta -= |
| 3173 | BBStates[NewRetainRelease->getParent()].GetAllPathCount(); |
| 3174 | |
| 3175 | // Merge the ReleaseMetadata and IsTailCallRelease values. |
| 3176 | if (FirstRelease) { |
| 3177 | ReleasesToMove.ReleaseMetadata = |
| 3178 | NewRetainReleaseRRI.ReleaseMetadata; |
| 3179 | ReleasesToMove.IsTailCallRelease = |
| 3180 | NewRetainReleaseRRI.IsTailCallRelease; |
| 3181 | FirstRelease = false; |
| 3182 | } else { |
| 3183 | if (ReleasesToMove.ReleaseMetadata != |
| 3184 | NewRetainReleaseRRI.ReleaseMetadata) |
| 3185 | ReleasesToMove.ReleaseMetadata = 0; |
| 3186 | if (ReleasesToMove.IsTailCallRelease != |
| 3187 | NewRetainReleaseRRI.IsTailCallRelease) |
| 3188 | ReleasesToMove.IsTailCallRelease = false; |
| 3189 | } |
| 3190 | |
| 3191 | // Collect the optimal insertion points. |
| 3192 | if (!KnownSafe) |
| 3193 | for (SmallPtrSet<Instruction *, 2>::const_iterator |
| 3194 | RI = NewRetainReleaseRRI.ReverseInsertPts.begin(), |
| 3195 | RE = NewRetainReleaseRRI.ReverseInsertPts.end(); |
| 3196 | RI != RE; ++RI) { |
| 3197 | Instruction *RIP = *RI; |
| 3198 | if (ReleasesToMove.ReverseInsertPts.insert(RIP)) |
| 3199 | NewDelta -= BBStates[RIP->getParent()].GetAllPathCount(); |
| 3200 | } |
| 3201 | NewReleases.push_back(NewRetainRelease); |
| 3202 | } |
| 3203 | } |
| 3204 | } |
| 3205 | NewRetains.clear(); |
| 3206 | if (NewReleases.empty()) break; |
| 3207 | |
| 3208 | // Back the other way. |
| 3209 | for (SmallVectorImpl<Instruction *>::const_iterator |
| 3210 | NI = NewReleases.begin(), NE = NewReleases.end(); NI != NE; ++NI) { |
| 3211 | Instruction *NewRelease = *NI; |
| 3212 | DenseMap<Value *, RRInfo>::const_iterator It = |
| 3213 | Releases.find(NewRelease); |
| 3214 | assert(It != Releases.end()); |
| 3215 | const RRInfo &NewReleaseRRI = It->second; |
| 3216 | KnownSafeBU &= NewReleaseRRI.KnownSafe; |
| 3217 | for (SmallPtrSet<Instruction *, 2>::const_iterator |
| 3218 | LI = NewReleaseRRI.Calls.begin(), |
| 3219 | LE = NewReleaseRRI.Calls.end(); LI != LE; ++LI) { |
| 3220 | Instruction *NewReleaseRetain = *LI; |
| 3221 | MapVector<Value *, RRInfo>::const_iterator Jt = |
| 3222 | Retains.find(NewReleaseRetain); |
| 3223 | if (Jt == Retains.end()) |
| 3224 | return false; |
| 3225 | const RRInfo &NewReleaseRetainRRI = Jt->second; |
| 3226 | assert(NewReleaseRetainRRI.Calls.count(NewRelease)); |
| 3227 | if (RetainsToMove.Calls.insert(NewReleaseRetain)) { |
| 3228 | unsigned PathCount = |
| 3229 | BBStates[NewReleaseRetain->getParent()].GetAllPathCount(); |
| 3230 | OldDelta += PathCount; |
| 3231 | OldCount += PathCount; |
| 3232 | |
| 3233 | // Merge the IsRetainBlock values. |
| 3234 | if (FirstRetain) { |
| 3235 | RetainsToMove.IsRetainBlock = NewReleaseRetainRRI.IsRetainBlock; |
| 3236 | FirstRetain = false; |
| 3237 | } else if (ReleasesToMove.IsRetainBlock != |
| 3238 | NewReleaseRetainRRI.IsRetainBlock) |
| 3239 | // It's not possible to merge the sequences if one uses |
| 3240 | // objc_retain and the other uses objc_retainBlock. |
| 3241 | return false; |
| 3242 | |
| 3243 | // Collect the optimal insertion points. |
| 3244 | if (!KnownSafe) |
| 3245 | for (SmallPtrSet<Instruction *, 2>::const_iterator |
| 3246 | RI = NewReleaseRetainRRI.ReverseInsertPts.begin(), |
| 3247 | RE = NewReleaseRetainRRI.ReverseInsertPts.end(); |
| 3248 | RI != RE; ++RI) { |
| 3249 | Instruction *RIP = *RI; |
| 3250 | if (RetainsToMove.ReverseInsertPts.insert(RIP)) { |
| 3251 | PathCount = BBStates[RIP->getParent()].GetAllPathCount(); |
| 3252 | NewDelta += PathCount; |
| 3253 | NewCount += PathCount; |
| 3254 | } |
| 3255 | } |
| 3256 | NewRetains.push_back(NewReleaseRetain); |
| 3257 | } |
| 3258 | } |
| 3259 | } |
| 3260 | NewReleases.clear(); |
| 3261 | if (NewRetains.empty()) break; |
| 3262 | } |
| 3263 | |
| 3264 | // If the pointer is known incremented or nested, we can safely delete the |
| 3265 | // pair regardless of what's between them. |
| 3266 | if (KnownSafeTD || KnownSafeBU) { |
| 3267 | RetainsToMove.ReverseInsertPts.clear(); |
| 3268 | ReleasesToMove.ReverseInsertPts.clear(); |
| 3269 | NewCount = 0; |
| 3270 | } else { |
| 3271 | // Determine whether the new insertion points we computed preserve the |
| 3272 | // balance of retain and release calls through the program. |
| 3273 | // TODO: If the fully aggressive solution isn't valid, try to find a |
| 3274 | // less aggressive solution which is. |
| 3275 | if (NewDelta != 0) |
| 3276 | return false; |
| 3277 | } |
| 3278 | |
| 3279 | // Determine whether the original call points are balanced in the retain and |
| 3280 | // release calls through the program. If not, conservatively don't touch |
| 3281 | // them. |
| 3282 | // TODO: It's theoretically possible to do code motion in this case, as |
| 3283 | // long as the existing imbalances are maintained. |
| 3284 | if (OldDelta != 0) |
| 3285 | return false; |
| 3286 | |
| 3287 | Changed = true; |
| 3288 | assert(OldCount != 0 && "Unreachable code?"); |
| 3289 | NumRRs += OldCount - NewCount; |
Michael Gottesman | 9de6f96 | 2013-01-22 21:49:00 +0000 | [diff] [blame] | 3290 | // Set to true if we completely removed any RR pairs. |
Michael Gottesman | 8b5515f | 2013-01-22 21:53:43 +0000 | [diff] [blame] | 3291 | AnyPairsCompletelyEliminated = NewCount == 0; |
Michael Gottesman | 9de6f96 | 2013-01-22 21:49:00 +0000 | [diff] [blame] | 3292 | |
| 3293 | // We can move calls! |
| 3294 | return true; |
| 3295 | } |
| 3296 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 3297 | /// Identify pairings between the retains and releases, and delete and/or move |
| 3298 | /// them. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 3299 | bool |
| 3300 | ObjCARCOpt::PerformCodePlacement(DenseMap<const BasicBlock *, BBState> |
| 3301 | &BBStates, |
| 3302 | MapVector<Value *, RRInfo> &Retains, |
Dan Gohman | 6320f52 | 2011-07-22 22:29:21 +0000 | [diff] [blame] | 3303 | DenseMap<Value *, RRInfo> &Releases, |
| 3304 | Module *M) { |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 3305 | bool AnyPairsCompletelyEliminated = false; |
| 3306 | RRInfo RetainsToMove; |
| 3307 | RRInfo ReleasesToMove; |
| 3308 | SmallVector<Instruction *, 4> NewRetains; |
| 3309 | SmallVector<Instruction *, 4> NewReleases; |
| 3310 | SmallVector<Instruction *, 8> DeadInsts; |
| 3311 | |
Dan Gohman | 670f937 | 2012-04-13 18:57:48 +0000 | [diff] [blame] | 3312 | // Visit each retain. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 3313 | for (MapVector<Value *, RRInfo>::const_iterator I = Retains.begin(), |
Dan Gohman | 2053a5d | 2011-09-29 22:25:23 +0000 | [diff] [blame] | 3314 | E = Retains.end(); I != E; ++I) { |
| 3315 | Value *V = I->first; |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 3316 | if (!V) continue; // blotted |
| 3317 | |
| 3318 | Instruction *Retain = cast<Instruction>(V); |
Michael Gottesman | c189a39 | 2013-01-09 19:23:24 +0000 | [diff] [blame] | 3319 | |
| 3320 | DEBUG(dbgs() << "ObjCARCOpt::PerformCodePlacement: Visiting: " << *Retain |
| 3321 | << "\n"); |
| 3322 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 3323 | Value *Arg = GetObjCArg(Retain); |
| 3324 | |
Dan Gohman | 728db49 | 2012-01-13 00:39:07 +0000 | [diff] [blame] | 3325 | // If the object being released is in static or stack storage, we know it's |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 3326 | // not being managed by ObjC reference counting, so we can delete pairs |
| 3327 | // regardless of what possible decrements or uses lie between them. |
Dan Gohman | 728db49 | 2012-01-13 00:39:07 +0000 | [diff] [blame] | 3328 | bool KnownSafe = isa<Constant>(Arg) || isa<AllocaInst>(Arg); |
Dan Gohman | 41375a3 | 2012-05-08 23:39:44 +0000 | [diff] [blame] | 3329 | |
Dan Gohman | 56e1cef | 2011-08-22 17:29:11 +0000 | [diff] [blame] | 3330 | // A constant pointer can't be pointing to an object on the heap. It may |
| 3331 | // be reference-counted, but it won't be deleted. |
| 3332 | if (const LoadInst *LI = dyn_cast<LoadInst>(Arg)) |
| 3333 | if (const GlobalVariable *GV = |
| 3334 | dyn_cast<GlobalVariable>( |
| 3335 | StripPointerCastsAndObjCCalls(LI->getPointerOperand()))) |
| 3336 | if (GV->isConstant()) |
| 3337 | KnownSafe = true; |
| 3338 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 3339 | // Connect the dots between the top-down-collected RetainsToMove and |
| 3340 | // bottom-up-collected ReleasesToMove to form sets of related calls. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 3341 | NewRetains.push_back(Retain); |
Michael Gottesman | 9de6f96 | 2013-01-22 21:49:00 +0000 | [diff] [blame] | 3342 | bool PerformMoveCalls = |
| 3343 | ConnectTDBUTraversals(BBStates, Retains, Releases, M, NewRetains, |
| 3344 | NewReleases, DeadInsts, RetainsToMove, |
| 3345 | ReleasesToMove, Arg, KnownSafe, |
| 3346 | AnyPairsCompletelyEliminated); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 3347 | |
Michael Gottesman | 9de6f96 | 2013-01-22 21:49:00 +0000 | [diff] [blame] | 3348 | if (PerformMoveCalls) { |
| 3349 | // Ok, everything checks out and we're all set. Let's move/delete some |
| 3350 | // code! |
| 3351 | MoveCalls(Arg, RetainsToMove, ReleasesToMove, |
| 3352 | Retains, Releases, DeadInsts, M); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 3353 | } |
| 3354 | |
Michael Gottesman | 9de6f96 | 2013-01-22 21:49:00 +0000 | [diff] [blame] | 3355 | // Clean up state for next retain. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 3356 | NewReleases.clear(); |
| 3357 | NewRetains.clear(); |
| 3358 | RetainsToMove.clear(); |
| 3359 | ReleasesToMove.clear(); |
| 3360 | } |
| 3361 | |
| 3362 | // Now that we're done moving everything, we can delete the newly dead |
| 3363 | // instructions, as we no longer need them as insert points. |
| 3364 | while (!DeadInsts.empty()) |
| 3365 | EraseInstruction(DeadInsts.pop_back_val()); |
| 3366 | |
| 3367 | return AnyPairsCompletelyEliminated; |
| 3368 | } |
| 3369 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 3370 | /// Weak pointer optimizations. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 3371 | void ObjCARCOpt::OptimizeWeakCalls(Function &F) { |
| 3372 | // First, do memdep-style RLE and S2L optimizations. We can't use memdep |
| 3373 | // itself because it uses AliasAnalysis and we need to do provenance |
| 3374 | // queries instead. |
| 3375 | for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ) { |
| 3376 | Instruction *Inst = &*I++; |
Michael Gottesman | 3f146e2 | 2013-01-01 16:05:48 +0000 | [diff] [blame] | 3377 | |
Michael Gottesman | 9f848ae | 2013-01-04 21:29:57 +0000 | [diff] [blame] | 3378 | DEBUG(dbgs() << "ObjCARCOpt::OptimizeWeakCalls: Visiting: " << *Inst << |
Michael Gottesman | 3f146e2 | 2013-01-01 16:05:48 +0000 | [diff] [blame] | 3379 | "\n"); |
| 3380 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 3381 | InstructionClass Class = GetBasicInstructionClass(Inst); |
| 3382 | if (Class != IC_LoadWeak && Class != IC_LoadWeakRetained) |
| 3383 | continue; |
| 3384 | |
| 3385 | // Delete objc_loadWeak calls with no users. |
| 3386 | if (Class == IC_LoadWeak && Inst->use_empty()) { |
| 3387 | Inst->eraseFromParent(); |
| 3388 | continue; |
| 3389 | } |
| 3390 | |
| 3391 | // TODO: For now, just look for an earlier available version of this value |
| 3392 | // within the same block. Theoretically, we could do memdep-style non-local |
| 3393 | // analysis too, but that would want caching. A better approach would be to |
| 3394 | // use the technique that EarlyCSE uses. |
| 3395 | inst_iterator Current = llvm::prior(I); |
| 3396 | BasicBlock *CurrentBB = Current.getBasicBlockIterator(); |
| 3397 | for (BasicBlock::iterator B = CurrentBB->begin(), |
| 3398 | J = Current.getInstructionIterator(); |
| 3399 | J != B; --J) { |
| 3400 | Instruction *EarlierInst = &*llvm::prior(J); |
| 3401 | InstructionClass EarlierClass = GetInstructionClass(EarlierInst); |
| 3402 | switch (EarlierClass) { |
| 3403 | case IC_LoadWeak: |
| 3404 | case IC_LoadWeakRetained: { |
| 3405 | // If this is loading from the same pointer, replace this load's value |
| 3406 | // with that one. |
| 3407 | CallInst *Call = cast<CallInst>(Inst); |
| 3408 | CallInst *EarlierCall = cast<CallInst>(EarlierInst); |
| 3409 | Value *Arg = Call->getArgOperand(0); |
| 3410 | Value *EarlierArg = EarlierCall->getArgOperand(0); |
| 3411 | switch (PA.getAA()->alias(Arg, EarlierArg)) { |
| 3412 | case AliasAnalysis::MustAlias: |
| 3413 | Changed = true; |
| 3414 | // If the load has a builtin retain, insert a plain retain for it. |
| 3415 | if (Class == IC_LoadWeakRetained) { |
| 3416 | CallInst *CI = |
| 3417 | CallInst::Create(getRetainCallee(F.getParent()), EarlierCall, |
| 3418 | "", Call); |
| 3419 | CI->setTailCall(); |
| 3420 | } |
| 3421 | // Zap the fully redundant load. |
| 3422 | Call->replaceAllUsesWith(EarlierCall); |
| 3423 | Call->eraseFromParent(); |
| 3424 | goto clobbered; |
| 3425 | case AliasAnalysis::MayAlias: |
| 3426 | case AliasAnalysis::PartialAlias: |
| 3427 | goto clobbered; |
| 3428 | case AliasAnalysis::NoAlias: |
| 3429 | break; |
| 3430 | } |
| 3431 | break; |
| 3432 | } |
| 3433 | case IC_StoreWeak: |
| 3434 | case IC_InitWeak: { |
| 3435 | // If this is storing to the same pointer and has the same size etc. |
| 3436 | // replace this load's value with the stored value. |
| 3437 | CallInst *Call = cast<CallInst>(Inst); |
| 3438 | CallInst *EarlierCall = cast<CallInst>(EarlierInst); |
| 3439 | Value *Arg = Call->getArgOperand(0); |
| 3440 | Value *EarlierArg = EarlierCall->getArgOperand(0); |
| 3441 | switch (PA.getAA()->alias(Arg, EarlierArg)) { |
| 3442 | case AliasAnalysis::MustAlias: |
| 3443 | Changed = true; |
| 3444 | // If the load has a builtin retain, insert a plain retain for it. |
| 3445 | if (Class == IC_LoadWeakRetained) { |
| 3446 | CallInst *CI = |
| 3447 | CallInst::Create(getRetainCallee(F.getParent()), EarlierCall, |
| 3448 | "", Call); |
| 3449 | CI->setTailCall(); |
| 3450 | } |
| 3451 | // Zap the fully redundant load. |
| 3452 | Call->replaceAllUsesWith(EarlierCall->getArgOperand(1)); |
| 3453 | Call->eraseFromParent(); |
| 3454 | goto clobbered; |
| 3455 | case AliasAnalysis::MayAlias: |
| 3456 | case AliasAnalysis::PartialAlias: |
| 3457 | goto clobbered; |
| 3458 | case AliasAnalysis::NoAlias: |
| 3459 | break; |
| 3460 | } |
| 3461 | break; |
| 3462 | } |
| 3463 | case IC_MoveWeak: |
| 3464 | case IC_CopyWeak: |
| 3465 | // TOOD: Grab the copied value. |
| 3466 | goto clobbered; |
| 3467 | case IC_AutoreleasepoolPush: |
| 3468 | case IC_None: |
| 3469 | case IC_User: |
| 3470 | // Weak pointers are only modified through the weak entry points |
| 3471 | // (and arbitrary calls, which could call the weak entry points). |
| 3472 | break; |
| 3473 | default: |
| 3474 | // Anything else could modify the weak pointer. |
| 3475 | goto clobbered; |
| 3476 | } |
| 3477 | } |
| 3478 | clobbered:; |
| 3479 | } |
| 3480 | |
| 3481 | // Then, for each destroyWeak with an alloca operand, check to see if |
| 3482 | // the alloca and all its users can be zapped. |
| 3483 | for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ) { |
| 3484 | Instruction *Inst = &*I++; |
| 3485 | InstructionClass Class = GetBasicInstructionClass(Inst); |
| 3486 | if (Class != IC_DestroyWeak) |
| 3487 | continue; |
| 3488 | |
| 3489 | CallInst *Call = cast<CallInst>(Inst); |
| 3490 | Value *Arg = Call->getArgOperand(0); |
| 3491 | if (AllocaInst *Alloca = dyn_cast<AllocaInst>(Arg)) { |
| 3492 | for (Value::use_iterator UI = Alloca->use_begin(), |
| 3493 | UE = Alloca->use_end(); UI != UE; ++UI) { |
Dan Gohman | dae3349 | 2012-04-27 18:56:31 +0000 | [diff] [blame] | 3494 | const Instruction *UserInst = cast<Instruction>(*UI); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 3495 | switch (GetBasicInstructionClass(UserInst)) { |
| 3496 | case IC_InitWeak: |
| 3497 | case IC_StoreWeak: |
| 3498 | case IC_DestroyWeak: |
| 3499 | continue; |
| 3500 | default: |
| 3501 | goto done; |
| 3502 | } |
| 3503 | } |
| 3504 | Changed = true; |
| 3505 | for (Value::use_iterator UI = Alloca->use_begin(), |
| 3506 | UE = Alloca->use_end(); UI != UE; ) { |
| 3507 | CallInst *UserInst = cast<CallInst>(*UI++); |
Dan Gohman | 14862c3 | 2012-05-18 22:17:29 +0000 | [diff] [blame] | 3508 | switch (GetBasicInstructionClass(UserInst)) { |
| 3509 | case IC_InitWeak: |
| 3510 | case IC_StoreWeak: |
| 3511 | // These functions return their second argument. |
| 3512 | UserInst->replaceAllUsesWith(UserInst->getArgOperand(1)); |
| 3513 | break; |
| 3514 | case IC_DestroyWeak: |
| 3515 | // No return value. |
| 3516 | break; |
| 3517 | default: |
Dan Gohman | 9c97eea0 | 2012-05-21 17:41:28 +0000 | [diff] [blame] | 3518 | llvm_unreachable("alloca really is used!"); |
Dan Gohman | 14862c3 | 2012-05-18 22:17:29 +0000 | [diff] [blame] | 3519 | } |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 3520 | UserInst->eraseFromParent(); |
| 3521 | } |
| 3522 | Alloca->eraseFromParent(); |
| 3523 | done:; |
| 3524 | } |
| 3525 | } |
Michael Gottesman | 10426b5 | 2013-01-07 21:26:07 +0000 | [diff] [blame] | 3526 | |
Michael Gottesman | 9f848ae | 2013-01-04 21:29:57 +0000 | [diff] [blame] | 3527 | DEBUG(dbgs() << "ObjCARCOpt::OptimizeWeakCalls: Finished List.\n\n"); |
Michael Gottesman | 10426b5 | 2013-01-07 21:26:07 +0000 | [diff] [blame] | 3528 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 3529 | } |
| 3530 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 3531 | /// Identify program paths which execute sequences of retains and releases which |
| 3532 | /// can be eliminated. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 3533 | bool ObjCARCOpt::OptimizeSequences(Function &F) { |
| 3534 | /// Releases, Retains - These are used to store the results of the main flow |
| 3535 | /// analysis. These use Value* as the key instead of Instruction* so that the |
| 3536 | /// map stays valid when we get around to rewriting code and calls get |
| 3537 | /// replaced by arguments. |
| 3538 | DenseMap<Value *, RRInfo> Releases; |
| 3539 | MapVector<Value *, RRInfo> Retains; |
| 3540 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 3541 | /// This is used during the traversal of the function to track the |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 3542 | /// states for each identified object at each block. |
| 3543 | DenseMap<const BasicBlock *, BBState> BBStates; |
| 3544 | |
| 3545 | // Analyze the CFG of the function, and all instructions. |
| 3546 | bool NestingDetected = Visit(F, BBStates, Retains, Releases); |
| 3547 | |
| 3548 | // Transform. |
Dan Gohman | 6320f52 | 2011-07-22 22:29:21 +0000 | [diff] [blame] | 3549 | return PerformCodePlacement(BBStates, Retains, Releases, F.getParent()) && |
| 3550 | NestingDetected; |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 3551 | } |
| 3552 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 3553 | /// Look for this pattern: |
Dmitri Gribenko | 5485acd | 2012-09-14 14:57:36 +0000 | [diff] [blame] | 3554 | /// \code |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 3555 | /// %call = call i8* @something(...) |
| 3556 | /// %2 = call i8* @objc_retain(i8* %call) |
| 3557 | /// %3 = call i8* @objc_autorelease(i8* %2) |
| 3558 | /// ret i8* %3 |
Dmitri Gribenko | 5485acd | 2012-09-14 14:57:36 +0000 | [diff] [blame] | 3559 | /// \endcode |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 3560 | /// And delete the retain and autorelease. |
| 3561 | /// |
| 3562 | /// Otherwise if it's just this: |
Dmitri Gribenko | 5485acd | 2012-09-14 14:57:36 +0000 | [diff] [blame] | 3563 | /// \code |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 3564 | /// %3 = call i8* @objc_autorelease(i8* %2) |
| 3565 | /// ret i8* %3 |
Dmitri Gribenko | 5485acd | 2012-09-14 14:57:36 +0000 | [diff] [blame] | 3566 | /// \endcode |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 3567 | /// convert the autorelease to autoreleaseRV. |
| 3568 | void ObjCARCOpt::OptimizeReturns(Function &F) { |
| 3569 | if (!F.getReturnType()->isPointerTy()) |
| 3570 | return; |
| 3571 | |
| 3572 | SmallPtrSet<Instruction *, 4> DependingInstructions; |
| 3573 | SmallPtrSet<const BasicBlock *, 4> Visited; |
| 3574 | for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI) { |
| 3575 | BasicBlock *BB = FI; |
| 3576 | ReturnInst *Ret = dyn_cast<ReturnInst>(&BB->back()); |
Michael Gottesman | 3f146e2 | 2013-01-01 16:05:48 +0000 | [diff] [blame] | 3577 | |
Michael Gottesman | 9f848ae | 2013-01-04 21:29:57 +0000 | [diff] [blame] | 3578 | DEBUG(dbgs() << "ObjCARCOpt::OptimizeReturns: Visiting: " << *Ret << "\n"); |
Michael Gottesman | 3f146e2 | 2013-01-01 16:05:48 +0000 | [diff] [blame] | 3579 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 3580 | if (!Ret) continue; |
| 3581 | |
| 3582 | const Value *Arg = StripPointerCastsAndObjCCalls(Ret->getOperand(0)); |
| 3583 | FindDependencies(NeedsPositiveRetainCount, Arg, |
| 3584 | BB, Ret, DependingInstructions, Visited, PA); |
| 3585 | if (DependingInstructions.size() != 1) |
| 3586 | goto next_block; |
| 3587 | |
| 3588 | { |
| 3589 | CallInst *Autorelease = |
| 3590 | dyn_cast_or_null<CallInst>(*DependingInstructions.begin()); |
| 3591 | if (!Autorelease) |
| 3592 | goto next_block; |
Dan Gohman | 41375a3 | 2012-05-08 23:39:44 +0000 | [diff] [blame] | 3593 | InstructionClass AutoreleaseClass = GetBasicInstructionClass(Autorelease); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 3594 | if (!IsAutorelease(AutoreleaseClass)) |
| 3595 | goto next_block; |
| 3596 | if (GetObjCArg(Autorelease) != Arg) |
| 3597 | goto next_block; |
| 3598 | |
| 3599 | DependingInstructions.clear(); |
| 3600 | Visited.clear(); |
| 3601 | |
| 3602 | // Check that there is nothing that can affect the reference |
| 3603 | // count between the autorelease and the retain. |
| 3604 | FindDependencies(CanChangeRetainCount, Arg, |
| 3605 | BB, Autorelease, DependingInstructions, Visited, PA); |
| 3606 | if (DependingInstructions.size() != 1) |
| 3607 | goto next_block; |
| 3608 | |
| 3609 | { |
| 3610 | CallInst *Retain = |
| 3611 | dyn_cast_or_null<CallInst>(*DependingInstructions.begin()); |
| 3612 | |
| 3613 | // Check that we found a retain with the same argument. |
| 3614 | if (!Retain || |
| 3615 | !IsRetain(GetBasicInstructionClass(Retain)) || |
| 3616 | GetObjCArg(Retain) != Arg) |
| 3617 | goto next_block; |
| 3618 | |
| 3619 | DependingInstructions.clear(); |
| 3620 | Visited.clear(); |
| 3621 | |
| 3622 | // Convert the autorelease to an autoreleaseRV, since it's |
| 3623 | // returning the value. |
| 3624 | if (AutoreleaseClass == IC_Autorelease) { |
Michael Gottesman | a6cb018 | 2013-01-10 02:03:50 +0000 | [diff] [blame] | 3625 | DEBUG(dbgs() << "ObjCARCOpt::OptimizeReturns: Converting autorelease " |
| 3626 | "=> autoreleaseRV since it's returning a value.\n" |
| 3627 | " In: " << *Autorelease |
| 3628 | << "\n"); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 3629 | Autorelease->setCalledFunction(getAutoreleaseRVCallee(F.getParent())); |
Michael Gottesman | a6cb018 | 2013-01-10 02:03:50 +0000 | [diff] [blame] | 3630 | DEBUG(dbgs() << " Out: " << *Autorelease |
| 3631 | << "\n"); |
Michael Gottesman | c9656fa | 2013-01-12 01:25:15 +0000 | [diff] [blame] | 3632 | Autorelease->setTailCall(); // Always tail call autoreleaseRV. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 3633 | AutoreleaseClass = IC_AutoreleaseRV; |
| 3634 | } |
| 3635 | |
| 3636 | // Check that there is nothing that can affect the reference |
| 3637 | // count between the retain and the call. |
Dan Gohman | 4ac148d | 2011-09-29 22:27:34 +0000 | [diff] [blame] | 3638 | // Note that Retain need not be in BB. |
| 3639 | FindDependencies(CanChangeRetainCount, Arg, Retain->getParent(), Retain, |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 3640 | DependingInstructions, Visited, PA); |
| 3641 | if (DependingInstructions.size() != 1) |
| 3642 | goto next_block; |
| 3643 | |
| 3644 | { |
| 3645 | CallInst *Call = |
| 3646 | dyn_cast_or_null<CallInst>(*DependingInstructions.begin()); |
| 3647 | |
| 3648 | // Check that the pointer is the return value of the call. |
| 3649 | if (!Call || Arg != Call) |
| 3650 | goto next_block; |
| 3651 | |
| 3652 | // Check that the call is a regular call. |
| 3653 | InstructionClass Class = GetBasicInstructionClass(Call); |
| 3654 | if (Class != IC_CallOrUser && Class != IC_Call) |
| 3655 | goto next_block; |
| 3656 | |
| 3657 | // If so, we can zap the retain and autorelease. |
| 3658 | Changed = true; |
| 3659 | ++NumRets; |
Michael Gottesman | d61a3b2 | 2013-01-07 00:04:56 +0000 | [diff] [blame] | 3660 | DEBUG(dbgs() << "ObjCARCOpt::OptimizeReturns: Erasing: " << *Retain |
| 3661 | << "\n Erasing: " |
| 3662 | << *Autorelease << "\n"); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 3663 | EraseInstruction(Retain); |
| 3664 | EraseInstruction(Autorelease); |
| 3665 | } |
| 3666 | } |
| 3667 | } |
| 3668 | |
| 3669 | next_block: |
| 3670 | DependingInstructions.clear(); |
| 3671 | Visited.clear(); |
| 3672 | } |
Michael Gottesman | 10426b5 | 2013-01-07 21:26:07 +0000 | [diff] [blame] | 3673 | |
Michael Gottesman | 9f848ae | 2013-01-04 21:29:57 +0000 | [diff] [blame] | 3674 | DEBUG(dbgs() << "ObjCARCOpt::OptimizeReturns: Finished List.\n\n"); |
Michael Gottesman | 10426b5 | 2013-01-07 21:26:07 +0000 | [diff] [blame] | 3675 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 3676 | } |
| 3677 | |
| 3678 | bool ObjCARCOpt::doInitialization(Module &M) { |
| 3679 | if (!EnableARCOpts) |
| 3680 | return false; |
| 3681 | |
Dan Gohman | 670f937 | 2012-04-13 18:57:48 +0000 | [diff] [blame] | 3682 | // If nothing in the Module uses ARC, don't do anything. |
Dan Gohman | ceaac7c | 2011-06-20 23:20:43 +0000 | [diff] [blame] | 3683 | Run = ModuleHasARC(M); |
| 3684 | if (!Run) |
| 3685 | return false; |
| 3686 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 3687 | // Identify the imprecise release metadata kind. |
| 3688 | ImpreciseReleaseMDKind = |
| 3689 | M.getContext().getMDKindID("clang.imprecise_release"); |
Dan Gohman | a7107f9 | 2011-10-17 22:53:25 +0000 | [diff] [blame] | 3690 | CopyOnEscapeMDKind = |
| 3691 | M.getContext().getMDKindID("clang.arc.copy_on_escape"); |
Dan Gohman | 0155f30 | 2012-02-17 18:59:53 +0000 | [diff] [blame] | 3692 | NoObjCARCExceptionsMDKind = |
| 3693 | M.getContext().getMDKindID("clang.arc.no_objc_arc_exceptions"); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 3694 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 3695 | // Intuitively, objc_retain and others are nocapture, however in practice |
| 3696 | // they are not, because they return their argument value. And objc_release |
Dan Gohman | dae3349 | 2012-04-27 18:56:31 +0000 | [diff] [blame] | 3697 | // calls finalizers which can have arbitrary side effects. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 3698 | |
| 3699 | // These are initialized lazily. |
| 3700 | RetainRVCallee = 0; |
| 3701 | AutoreleaseRVCallee = 0; |
| 3702 | ReleaseCallee = 0; |
| 3703 | RetainCallee = 0; |
Dan Gohman | 6320f52 | 2011-07-22 22:29:21 +0000 | [diff] [blame] | 3704 | RetainBlockCallee = 0; |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 3705 | AutoreleaseCallee = 0; |
| 3706 | |
| 3707 | return false; |
| 3708 | } |
| 3709 | |
| 3710 | bool ObjCARCOpt::runOnFunction(Function &F) { |
| 3711 | if (!EnableARCOpts) |
| 3712 | return false; |
| 3713 | |
Dan Gohman | ceaac7c | 2011-06-20 23:20:43 +0000 | [diff] [blame] | 3714 | // If nothing in the Module uses ARC, don't do anything. |
| 3715 | if (!Run) |
| 3716 | return false; |
| 3717 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 3718 | Changed = false; |
| 3719 | |
Michael Gottesman | b24bdef | 2013-01-12 02:57:16 +0000 | [diff] [blame] | 3720 | DEBUG(dbgs() << "ObjCARCOpt: Visiting Function: " << F.getName() << "\n"); |
| 3721 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 3722 | PA.setAA(&getAnalysis<AliasAnalysis>()); |
| 3723 | |
| 3724 | // This pass performs several distinct transformations. As a compile-time aid |
| 3725 | // when compiling code that isn't ObjC, skip these if the relevant ObjC |
| 3726 | // library functions aren't declared. |
| 3727 | |
| 3728 | // Preliminary optimizations. This also computs UsedInThisFunction. |
| 3729 | OptimizeIndividualCalls(F); |
| 3730 | |
| 3731 | // Optimizations for weak pointers. |
| 3732 | if (UsedInThisFunction & ((1 << IC_LoadWeak) | |
| 3733 | (1 << IC_LoadWeakRetained) | |
| 3734 | (1 << IC_StoreWeak) | |
| 3735 | (1 << IC_InitWeak) | |
| 3736 | (1 << IC_CopyWeak) | |
| 3737 | (1 << IC_MoveWeak) | |
| 3738 | (1 << IC_DestroyWeak))) |
| 3739 | OptimizeWeakCalls(F); |
| 3740 | |
| 3741 | // Optimizations for retain+release pairs. |
| 3742 | if (UsedInThisFunction & ((1 << IC_Retain) | |
| 3743 | (1 << IC_RetainRV) | |
| 3744 | (1 << IC_RetainBlock))) |
| 3745 | if (UsedInThisFunction & (1 << IC_Release)) |
| 3746 | // Run OptimizeSequences until it either stops making changes or |
| 3747 | // no retain+release pair nesting is detected. |
| 3748 | while (OptimizeSequences(F)) {} |
| 3749 | |
| 3750 | // Optimizations if objc_autorelease is used. |
Dan Gohman | 41375a3 | 2012-05-08 23:39:44 +0000 | [diff] [blame] | 3751 | if (UsedInThisFunction & ((1 << IC_Autorelease) | |
| 3752 | (1 << IC_AutoreleaseRV))) |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 3753 | OptimizeReturns(F); |
| 3754 | |
Michael Gottesman | b24bdef | 2013-01-12 02:57:16 +0000 | [diff] [blame] | 3755 | DEBUG(dbgs() << "\n"); |
| 3756 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 3757 | return Changed; |
| 3758 | } |
| 3759 | |
| 3760 | void ObjCARCOpt::releaseMemory() { |
| 3761 | PA.clear(); |
| 3762 | } |
| 3763 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 3764 | /// @} |
| 3765 | /// |
| 3766 | /// \defgroup ARCContract ARC Contraction. |
| 3767 | /// @{ |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 3768 | |
| 3769 | // TODO: ObjCARCContract could insert PHI nodes when uses aren't |
| 3770 | // dominated by single calls. |
| 3771 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 3772 | #include "llvm/Analysis/Dominators.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 3773 | #include "llvm/IR/InlineAsm.h" |
| 3774 | #include "llvm/IR/Operator.h" |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 3775 | |
| 3776 | STATISTIC(NumStoreStrongs, "Number objc_storeStrong calls formed"); |
| 3777 | |
| 3778 | namespace { |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 3779 | /// \brief Late ARC optimizations |
| 3780 | /// |
| 3781 | /// These change the IR in a way that makes it difficult to be analyzed by |
| 3782 | /// ObjCARCOpt, so it's run late. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 3783 | class ObjCARCContract : public FunctionPass { |
| 3784 | bool Changed; |
| 3785 | AliasAnalysis *AA; |
| 3786 | DominatorTree *DT; |
| 3787 | ProvenanceAnalysis PA; |
| 3788 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 3789 | /// A flag indicating whether this optimization pass should run. |
Dan Gohman | ceaac7c | 2011-06-20 23:20:43 +0000 | [diff] [blame] | 3790 | bool Run; |
| 3791 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 3792 | /// Declarations for ObjC runtime functions, for use in creating calls to |
| 3793 | /// them. These are initialized lazily to avoid cluttering up the Module |
| 3794 | /// with unused declarations. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 3795 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 3796 | /// Declaration for objc_storeStrong(). |
| 3797 | Constant *StoreStrongCallee; |
| 3798 | /// Declaration for objc_retainAutorelease(). |
| 3799 | Constant *RetainAutoreleaseCallee; |
| 3800 | /// Declaration for objc_retainAutoreleaseReturnValue(). |
| 3801 | Constant *RetainAutoreleaseRVCallee; |
| 3802 | |
| 3803 | /// The inline asm string to insert between calls and RetainRV calls to make |
| 3804 | /// the optimization work on targets which need it. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 3805 | const MDString *RetainRVMarker; |
| 3806 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 3807 | /// The set of inserted objc_storeStrong calls. If at the end of walking the |
| 3808 | /// function we have found no alloca instructions, these calls can be marked |
| 3809 | /// "tail". |
Dan Gohman | 41375a3 | 2012-05-08 23:39:44 +0000 | [diff] [blame] | 3810 | SmallPtrSet<CallInst *, 8> StoreStrongCalls; |
Dan Gohman | 8ee108b | 2012-01-19 19:14:36 +0000 | [diff] [blame] | 3811 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 3812 | Constant *getStoreStrongCallee(Module *M); |
| 3813 | Constant *getRetainAutoreleaseCallee(Module *M); |
| 3814 | Constant *getRetainAutoreleaseRVCallee(Module *M); |
| 3815 | |
| 3816 | bool ContractAutorelease(Function &F, Instruction *Autorelease, |
| 3817 | InstructionClass Class, |
| 3818 | SmallPtrSet<Instruction *, 4> |
| 3819 | &DependingInstructions, |
| 3820 | SmallPtrSet<const BasicBlock *, 4> |
| 3821 | &Visited); |
| 3822 | |
| 3823 | void ContractRelease(Instruction *Release, |
| 3824 | inst_iterator &Iter); |
| 3825 | |
| 3826 | virtual void getAnalysisUsage(AnalysisUsage &AU) const; |
| 3827 | virtual bool doInitialization(Module &M); |
| 3828 | virtual bool runOnFunction(Function &F); |
| 3829 | |
| 3830 | public: |
| 3831 | static char ID; |
| 3832 | ObjCARCContract() : FunctionPass(ID) { |
| 3833 | initializeObjCARCContractPass(*PassRegistry::getPassRegistry()); |
| 3834 | } |
| 3835 | }; |
| 3836 | } |
| 3837 | |
| 3838 | char ObjCARCContract::ID = 0; |
| 3839 | INITIALIZE_PASS_BEGIN(ObjCARCContract, |
| 3840 | "objc-arc-contract", "ObjC ARC contraction", false, false) |
| 3841 | INITIALIZE_AG_DEPENDENCY(AliasAnalysis) |
| 3842 | INITIALIZE_PASS_DEPENDENCY(DominatorTree) |
| 3843 | INITIALIZE_PASS_END(ObjCARCContract, |
| 3844 | "objc-arc-contract", "ObjC ARC contraction", false, false) |
| 3845 | |
| 3846 | Pass *llvm::createObjCARCContractPass() { |
| 3847 | return new ObjCARCContract(); |
| 3848 | } |
| 3849 | |
| 3850 | void ObjCARCContract::getAnalysisUsage(AnalysisUsage &AU) const { |
| 3851 | AU.addRequired<AliasAnalysis>(); |
| 3852 | AU.addRequired<DominatorTree>(); |
| 3853 | AU.setPreservesCFG(); |
| 3854 | } |
| 3855 | |
| 3856 | Constant *ObjCARCContract::getStoreStrongCallee(Module *M) { |
| 3857 | if (!StoreStrongCallee) { |
| 3858 | LLVMContext &C = M->getContext(); |
Jay Foad | b804a2b | 2011-07-12 14:06:48 +0000 | [diff] [blame] | 3859 | Type *I8X = PointerType::getUnqual(Type::getInt8Ty(C)); |
| 3860 | Type *I8XX = PointerType::getUnqual(I8X); |
Dan Gohman | 41375a3 | 2012-05-08 23:39:44 +0000 | [diff] [blame] | 3861 | Type *Params[] = { I8XX, I8X }; |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 3862 | |
Bill Wendling | 09175b3 | 2013-01-22 21:15:51 +0000 | [diff] [blame] | 3863 | AttributeSet Attr = AttributeSet() |
| 3864 | .addAttribute(M->getContext(), AttributeSet::FunctionIndex, |
| 3865 | Attribute::NoUnwind) |
| 3866 | .addAttribute(M->getContext(), 1, Attribute::NoCapture); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 3867 | |
| 3868 | StoreStrongCallee = |
| 3869 | M->getOrInsertFunction( |
| 3870 | "objc_storeStrong", |
| 3871 | FunctionType::get(Type::getVoidTy(C), Params, /*isVarArg=*/false), |
Bill Wendling | 09175b3 | 2013-01-22 21:15:51 +0000 | [diff] [blame] | 3872 | Attr); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 3873 | } |
| 3874 | return StoreStrongCallee; |
| 3875 | } |
| 3876 | |
| 3877 | Constant *ObjCARCContract::getRetainAutoreleaseCallee(Module *M) { |
| 3878 | if (!RetainAutoreleaseCallee) { |
| 3879 | LLVMContext &C = M->getContext(); |
Jay Foad | b804a2b | 2011-07-12 14:06:48 +0000 | [diff] [blame] | 3880 | Type *I8X = PointerType::getUnqual(Type::getInt8Ty(C)); |
Dan Gohman | 41375a3 | 2012-05-08 23:39:44 +0000 | [diff] [blame] | 3881 | Type *Params[] = { I8X }; |
| 3882 | FunctionType *FTy = FunctionType::get(I8X, Params, /*isVarArg=*/false); |
Bill Wendling | 3d7b0b8 | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 3883 | AttributeSet Attribute = |
Bill Wendling | 09175b3 | 2013-01-22 21:15:51 +0000 | [diff] [blame] | 3884 | AttributeSet().addAttribute(M->getContext(), AttributeSet::FunctionIndex, |
| 3885 | Attribute::NoUnwind); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 3886 | RetainAutoreleaseCallee = |
Bill Wendling | 3d7b0b8 | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 3887 | M->getOrInsertFunction("objc_retainAutorelease", FTy, Attribute); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 3888 | } |
| 3889 | return RetainAutoreleaseCallee; |
| 3890 | } |
| 3891 | |
| 3892 | Constant *ObjCARCContract::getRetainAutoreleaseRVCallee(Module *M) { |
| 3893 | if (!RetainAutoreleaseRVCallee) { |
| 3894 | LLVMContext &C = M->getContext(); |
Jay Foad | b804a2b | 2011-07-12 14:06:48 +0000 | [diff] [blame] | 3895 | Type *I8X = PointerType::getUnqual(Type::getInt8Ty(C)); |
Dan Gohman | 41375a3 | 2012-05-08 23:39:44 +0000 | [diff] [blame] | 3896 | Type *Params[] = { I8X }; |
| 3897 | FunctionType *FTy = FunctionType::get(I8X, Params, /*isVarArg=*/false); |
Bill Wendling | 3d7b0b8 | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 3898 | AttributeSet Attribute = |
Bill Wendling | 09175b3 | 2013-01-22 21:15:51 +0000 | [diff] [blame] | 3899 | AttributeSet().addAttribute(M->getContext(), AttributeSet::FunctionIndex, |
| 3900 | Attribute::NoUnwind); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 3901 | RetainAutoreleaseRVCallee = |
| 3902 | M->getOrInsertFunction("objc_retainAutoreleaseReturnValue", FTy, |
Bill Wendling | 3d7b0b8 | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 3903 | Attribute); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 3904 | } |
| 3905 | return RetainAutoreleaseRVCallee; |
| 3906 | } |
| 3907 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 3908 | /// Merge an autorelease with a retain into a fused call. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 3909 | bool |
| 3910 | ObjCARCContract::ContractAutorelease(Function &F, Instruction *Autorelease, |
| 3911 | InstructionClass Class, |
| 3912 | SmallPtrSet<Instruction *, 4> |
| 3913 | &DependingInstructions, |
| 3914 | SmallPtrSet<const BasicBlock *, 4> |
| 3915 | &Visited) { |
| 3916 | const Value *Arg = GetObjCArg(Autorelease); |
| 3917 | |
| 3918 | // Check that there are no instructions between the retain and the autorelease |
| 3919 | // (such as an autorelease_pop) which may change the count. |
| 3920 | CallInst *Retain = 0; |
| 3921 | if (Class == IC_AutoreleaseRV) |
| 3922 | FindDependencies(RetainAutoreleaseRVDep, Arg, |
| 3923 | Autorelease->getParent(), Autorelease, |
| 3924 | DependingInstructions, Visited, PA); |
| 3925 | else |
| 3926 | FindDependencies(RetainAutoreleaseDep, Arg, |
| 3927 | Autorelease->getParent(), Autorelease, |
| 3928 | DependingInstructions, Visited, PA); |
| 3929 | |
| 3930 | Visited.clear(); |
| 3931 | if (DependingInstructions.size() != 1) { |
| 3932 | DependingInstructions.clear(); |
| 3933 | return false; |
| 3934 | } |
| 3935 | |
| 3936 | Retain = dyn_cast_or_null<CallInst>(*DependingInstructions.begin()); |
| 3937 | DependingInstructions.clear(); |
| 3938 | |
| 3939 | if (!Retain || |
| 3940 | GetBasicInstructionClass(Retain) != IC_Retain || |
| 3941 | GetObjCArg(Retain) != Arg) |
| 3942 | return false; |
| 3943 | |
| 3944 | Changed = true; |
| 3945 | ++NumPeeps; |
Michael Gottesman | 10426b5 | 2013-01-07 21:26:07 +0000 | [diff] [blame] | 3946 | |
Michael Gottesman | add0847 | 2013-01-07 00:31:26 +0000 | [diff] [blame] | 3947 | DEBUG(dbgs() << "ObjCARCContract::ContractAutorelease: Fusing " |
| 3948 | "retain/autorelease. Erasing: " << *Autorelease << "\n" |
| 3949 | " Old Retain: " |
| 3950 | << *Retain << "\n"); |
Michael Gottesman | 10426b5 | 2013-01-07 21:26:07 +0000 | [diff] [blame] | 3951 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 3952 | if (Class == IC_AutoreleaseRV) |
| 3953 | Retain->setCalledFunction(getRetainAutoreleaseRVCallee(F.getParent())); |
| 3954 | else |
| 3955 | Retain->setCalledFunction(getRetainAutoreleaseCallee(F.getParent())); |
Michael Gottesman | 10426b5 | 2013-01-07 21:26:07 +0000 | [diff] [blame] | 3956 | |
Michael Gottesman | add0847 | 2013-01-07 00:31:26 +0000 | [diff] [blame] | 3957 | DEBUG(dbgs() << " New Retain: " |
| 3958 | << *Retain << "\n"); |
Michael Gottesman | 10426b5 | 2013-01-07 21:26:07 +0000 | [diff] [blame] | 3959 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 3960 | EraseInstruction(Autorelease); |
| 3961 | return true; |
| 3962 | } |
| 3963 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 3964 | /// Attempt to merge an objc_release with a store, load, and objc_retain to form |
| 3965 | /// an objc_storeStrong. This can be a little tricky because the instructions |
| 3966 | /// don't always appear in order, and there may be unrelated intervening |
| 3967 | /// instructions. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 3968 | void ObjCARCContract::ContractRelease(Instruction *Release, |
| 3969 | inst_iterator &Iter) { |
| 3970 | LoadInst *Load = dyn_cast<LoadInst>(GetObjCArg(Release)); |
Eli Friedman | 7c5dc12 | 2011-09-12 20:23:13 +0000 | [diff] [blame] | 3971 | if (!Load || !Load->isSimple()) return; |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 3972 | |
| 3973 | // For now, require everything to be in one basic block. |
| 3974 | BasicBlock *BB = Release->getParent(); |
| 3975 | if (Load->getParent() != BB) return; |
| 3976 | |
Dan Gohman | 61708d3 | 2012-05-08 23:34:08 +0000 | [diff] [blame] | 3977 | // Walk down to find the store and the release, which may be in either order. |
Dan Gohman | f8b19d0 | 2012-05-09 23:08:33 +0000 | [diff] [blame] | 3978 | BasicBlock::iterator I = Load, End = BB->end(); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 3979 | ++I; |
| 3980 | AliasAnalysis::Location Loc = AA->getLocation(Load); |
Dan Gohman | 61708d3 | 2012-05-08 23:34:08 +0000 | [diff] [blame] | 3981 | StoreInst *Store = 0; |
| 3982 | bool SawRelease = false; |
| 3983 | for (; !Store || !SawRelease; ++I) { |
Dan Gohman | f8b19d0 | 2012-05-09 23:08:33 +0000 | [diff] [blame] | 3984 | if (I == End) |
| 3985 | return; |
| 3986 | |
Dan Gohman | 61708d3 | 2012-05-08 23:34:08 +0000 | [diff] [blame] | 3987 | Instruction *Inst = I; |
| 3988 | if (Inst == Release) { |
| 3989 | SawRelease = true; |
| 3990 | continue; |
| 3991 | } |
| 3992 | |
| 3993 | InstructionClass Class = GetBasicInstructionClass(Inst); |
| 3994 | |
| 3995 | // Unrelated retains are harmless. |
| 3996 | if (IsRetain(Class)) |
| 3997 | continue; |
| 3998 | |
| 3999 | if (Store) { |
| 4000 | // The store is the point where we're going to put the objc_storeStrong, |
| 4001 | // so make sure there are no uses after it. |
| 4002 | if (CanUse(Inst, Load, PA, Class)) |
| 4003 | return; |
| 4004 | } else if (AA->getModRefInfo(Inst, Loc) & AliasAnalysis::Mod) { |
| 4005 | // We are moving the load down to the store, so check for anything |
| 4006 | // else which writes to the memory between the load and the store. |
| 4007 | Store = dyn_cast<StoreInst>(Inst); |
| 4008 | if (!Store || !Store->isSimple()) return; |
| 4009 | if (Store->getPointerOperand() != Loc.Ptr) return; |
| 4010 | } |
| 4011 | } |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 4012 | |
| 4013 | Value *New = StripPointerCastsAndObjCCalls(Store->getValueOperand()); |
| 4014 | |
| 4015 | // Walk up to find the retain. |
| 4016 | I = Store; |
| 4017 | BasicBlock::iterator Begin = BB->begin(); |
| 4018 | while (I != Begin && GetBasicInstructionClass(I) != IC_Retain) |
| 4019 | --I; |
| 4020 | Instruction *Retain = I; |
| 4021 | if (GetBasicInstructionClass(Retain) != IC_Retain) return; |
| 4022 | if (GetObjCArg(Retain) != New) return; |
| 4023 | |
| 4024 | Changed = true; |
| 4025 | ++NumStoreStrongs; |
| 4026 | |
| 4027 | LLVMContext &C = Release->getContext(); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 4028 | Type *I8X = PointerType::getUnqual(Type::getInt8Ty(C)); |
| 4029 | Type *I8XX = PointerType::getUnqual(I8X); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 4030 | |
| 4031 | Value *Args[] = { Load->getPointerOperand(), New }; |
| 4032 | if (Args[0]->getType() != I8XX) |
| 4033 | Args[0] = new BitCastInst(Args[0], I8XX, "", Store); |
| 4034 | if (Args[1]->getType() != I8X) |
| 4035 | Args[1] = new BitCastInst(Args[1], I8X, "", Store); |
| 4036 | CallInst *StoreStrong = |
| 4037 | CallInst::Create(getStoreStrongCallee(BB->getParent()->getParent()), |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 4038 | Args, "", Store); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 4039 | StoreStrong->setDoesNotThrow(); |
| 4040 | StoreStrong->setDebugLoc(Store->getDebugLoc()); |
| 4041 | |
Dan Gohman | 8ee108b | 2012-01-19 19:14:36 +0000 | [diff] [blame] | 4042 | // We can't set the tail flag yet, because we haven't yet determined |
| 4043 | // whether there are any escaping allocas. Remember this call, so that |
| 4044 | // we can set the tail flag once we know it's safe. |
| 4045 | StoreStrongCalls.insert(StoreStrong); |
| 4046 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 4047 | if (&*Iter == Store) ++Iter; |
| 4048 | Store->eraseFromParent(); |
| 4049 | Release->eraseFromParent(); |
| 4050 | EraseInstruction(Retain); |
| 4051 | if (Load->use_empty()) |
| 4052 | Load->eraseFromParent(); |
| 4053 | } |
| 4054 | |
| 4055 | bool ObjCARCContract::doInitialization(Module &M) { |
Dan Gohman | 670f937 | 2012-04-13 18:57:48 +0000 | [diff] [blame] | 4056 | // If nothing in the Module uses ARC, don't do anything. |
Dan Gohman | ceaac7c | 2011-06-20 23:20:43 +0000 | [diff] [blame] | 4057 | Run = ModuleHasARC(M); |
| 4058 | if (!Run) |
| 4059 | return false; |
| 4060 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 4061 | // These are initialized lazily. |
| 4062 | StoreStrongCallee = 0; |
| 4063 | RetainAutoreleaseCallee = 0; |
| 4064 | RetainAutoreleaseRVCallee = 0; |
| 4065 | |
| 4066 | // Initialize RetainRVMarker. |
| 4067 | RetainRVMarker = 0; |
| 4068 | if (NamedMDNode *NMD = |
| 4069 | M.getNamedMetadata("clang.arc.retainAutoreleasedReturnValueMarker")) |
| 4070 | if (NMD->getNumOperands() == 1) { |
| 4071 | const MDNode *N = NMD->getOperand(0); |
| 4072 | if (N->getNumOperands() == 1) |
| 4073 | if (const MDString *S = dyn_cast<MDString>(N->getOperand(0))) |
| 4074 | RetainRVMarker = S; |
| 4075 | } |
| 4076 | |
| 4077 | return false; |
| 4078 | } |
| 4079 | |
| 4080 | bool ObjCARCContract::runOnFunction(Function &F) { |
| 4081 | if (!EnableARCOpts) |
| 4082 | return false; |
| 4083 | |
Dan Gohman | ceaac7c | 2011-06-20 23:20:43 +0000 | [diff] [blame] | 4084 | // If nothing in the Module uses ARC, don't do anything. |
| 4085 | if (!Run) |
| 4086 | return false; |
| 4087 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 4088 | Changed = false; |
| 4089 | AA = &getAnalysis<AliasAnalysis>(); |
| 4090 | DT = &getAnalysis<DominatorTree>(); |
| 4091 | |
| 4092 | PA.setAA(&getAnalysis<AliasAnalysis>()); |
| 4093 | |
Dan Gohman | 8ee108b | 2012-01-19 19:14:36 +0000 | [diff] [blame] | 4094 | // Track whether it's ok to mark objc_storeStrong calls with the "tail" |
| 4095 | // keyword. Be conservative if the function has variadic arguments. |
| 4096 | // It seems that functions which "return twice" are also unsafe for the |
| 4097 | // "tail" argument, because they are setjmp, which could need to |
| 4098 | // return to an earlier stack state. |
Dan Gohman | 41375a3 | 2012-05-08 23:39:44 +0000 | [diff] [blame] | 4099 | bool TailOkForStoreStrongs = !F.isVarArg() && |
| 4100 | !F.callsFunctionThatReturnsTwice(); |
Dan Gohman | 8ee108b | 2012-01-19 19:14:36 +0000 | [diff] [blame] | 4101 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 4102 | // For ObjC library calls which return their argument, replace uses of the |
| 4103 | // argument with uses of the call return value, if it dominates the use. This |
| 4104 | // reduces register pressure. |
| 4105 | SmallPtrSet<Instruction *, 4> DependingInstructions; |
| 4106 | SmallPtrSet<const BasicBlock *, 4> Visited; |
| 4107 | for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ) { |
| 4108 | Instruction *Inst = &*I++; |
Michael Gottesman | 10426b5 | 2013-01-07 21:26:07 +0000 | [diff] [blame] | 4109 | |
Michael Gottesman | 3f146e2 | 2013-01-01 16:05:48 +0000 | [diff] [blame] | 4110 | DEBUG(dbgs() << "ObjCARCContract: Visiting: " << *Inst << "\n"); |
Michael Gottesman | 10426b5 | 2013-01-07 21:26:07 +0000 | [diff] [blame] | 4111 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 4112 | // Only these library routines return their argument. In particular, |
| 4113 | // objc_retainBlock does not necessarily return its argument. |
| 4114 | InstructionClass Class = GetBasicInstructionClass(Inst); |
| 4115 | switch (Class) { |
| 4116 | case IC_Retain: |
| 4117 | case IC_FusedRetainAutorelease: |
| 4118 | case IC_FusedRetainAutoreleaseRV: |
| 4119 | break; |
| 4120 | case IC_Autorelease: |
| 4121 | case IC_AutoreleaseRV: |
| 4122 | if (ContractAutorelease(F, Inst, Class, DependingInstructions, Visited)) |
| 4123 | continue; |
| 4124 | break; |
| 4125 | case IC_RetainRV: { |
| 4126 | // If we're compiling for a target which needs a special inline-asm |
| 4127 | // marker to do the retainAutoreleasedReturnValue optimization, |
| 4128 | // insert it now. |
| 4129 | if (!RetainRVMarker) |
| 4130 | break; |
| 4131 | BasicBlock::iterator BBI = Inst; |
Dan Gohman | 5f725cd | 2012-06-25 19:47:37 +0000 | [diff] [blame] | 4132 | BasicBlock *InstParent = Inst->getParent(); |
| 4133 | |
| 4134 | // Step up to see if the call immediately precedes the RetainRV call. |
| 4135 | // If it's an invoke, we have to cross a block boundary. And we have |
| 4136 | // to carefully dodge no-op instructions. |
| 4137 | do { |
| 4138 | if (&*BBI == InstParent->begin()) { |
| 4139 | BasicBlock *Pred = InstParent->getSinglePredecessor(); |
| 4140 | if (!Pred) |
| 4141 | goto decline_rv_optimization; |
| 4142 | BBI = Pred->getTerminator(); |
| 4143 | break; |
| 4144 | } |
| 4145 | --BBI; |
| 4146 | } while (isNoopInstruction(BBI)); |
| 4147 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 4148 | if (&*BBI == GetObjCArg(Inst)) { |
Michael Gottesman | 00d1f96 | 2013-01-03 07:32:41 +0000 | [diff] [blame] | 4149 | DEBUG(dbgs() << "ObjCARCContract: Adding inline asm marker for " |
Michael Gottesman | 9f848ae | 2013-01-04 21:29:57 +0000 | [diff] [blame] | 4150 | "retainAutoreleasedReturnValue optimization.\n"); |
Dan Gohman | 670f937 | 2012-04-13 18:57:48 +0000 | [diff] [blame] | 4151 | Changed = true; |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 4152 | InlineAsm *IA = |
| 4153 | InlineAsm::get(FunctionType::get(Type::getVoidTy(Inst->getContext()), |
| 4154 | /*isVarArg=*/false), |
| 4155 | RetainRVMarker->getString(), |
| 4156 | /*Constraints=*/"", /*hasSideEffects=*/true); |
| 4157 | CallInst::Create(IA, "", Inst); |
| 4158 | } |
Dan Gohman | 5f725cd | 2012-06-25 19:47:37 +0000 | [diff] [blame] | 4159 | decline_rv_optimization: |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 4160 | break; |
| 4161 | } |
| 4162 | case IC_InitWeak: { |
| 4163 | // objc_initWeak(p, null) => *p = null |
| 4164 | CallInst *CI = cast<CallInst>(Inst); |
| 4165 | if (isNullOrUndef(CI->getArgOperand(1))) { |
| 4166 | Value *Null = |
| 4167 | ConstantPointerNull::get(cast<PointerType>(CI->getType())); |
| 4168 | Changed = true; |
| 4169 | new StoreInst(Null, CI->getArgOperand(0), CI); |
Michael Gottesman | 10426b5 | 2013-01-07 21:26:07 +0000 | [diff] [blame] | 4170 | |
Michael Gottesman | 416dc00 | 2013-01-03 07:32:53 +0000 | [diff] [blame] | 4171 | DEBUG(dbgs() << "OBJCARCContract: Old = " << *CI << "\n" |
| 4172 | << " New = " << *Null << "\n"); |
Michael Gottesman | 10426b5 | 2013-01-07 21:26:07 +0000 | [diff] [blame] | 4173 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 4174 | CI->replaceAllUsesWith(Null); |
| 4175 | CI->eraseFromParent(); |
| 4176 | } |
| 4177 | continue; |
| 4178 | } |
| 4179 | case IC_Release: |
| 4180 | ContractRelease(Inst, I); |
| 4181 | continue; |
Dan Gohman | 8ee108b | 2012-01-19 19:14:36 +0000 | [diff] [blame] | 4182 | case IC_User: |
| 4183 | // Be conservative if the function has any alloca instructions. |
| 4184 | // Technically we only care about escaping alloca instructions, |
| 4185 | // but this is sufficient to handle some interesting cases. |
| 4186 | if (isa<AllocaInst>(Inst)) |
| 4187 | TailOkForStoreStrongs = false; |
| 4188 | continue; |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 4189 | default: |
| 4190 | continue; |
| 4191 | } |
| 4192 | |
Michael Gottesman | 50ae5b2 | 2013-01-03 08:09:27 +0000 | [diff] [blame] | 4193 | DEBUG(dbgs() << "ObjCARCContract: Finished List.\n\n"); |
Michael Gottesman | 3f146e2 | 2013-01-01 16:05:48 +0000 | [diff] [blame] | 4194 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 4195 | // Don't use GetObjCArg because we don't want to look through bitcasts |
| 4196 | // and such; to do the replacement, the argument must have type i8*. |
| 4197 | const Value *Arg = cast<CallInst>(Inst)->getArgOperand(0); |
| 4198 | for (;;) { |
| 4199 | // If we're compiling bugpointed code, don't get in trouble. |
| 4200 | if (!isa<Instruction>(Arg) && !isa<Argument>(Arg)) |
| 4201 | break; |
| 4202 | // Look through the uses of the pointer. |
| 4203 | for (Value::const_use_iterator UI = Arg->use_begin(), UE = Arg->use_end(); |
| 4204 | UI != UE; ) { |
| 4205 | Use &U = UI.getUse(); |
| 4206 | unsigned OperandNo = UI.getOperandNo(); |
| 4207 | ++UI; // Increment UI now, because we may unlink its element. |
Dan Gohman | 670f937 | 2012-04-13 18:57:48 +0000 | [diff] [blame] | 4208 | |
| 4209 | // If the call's return value dominates a use of the call's argument |
| 4210 | // value, rewrite the use to use the return value. We check for |
| 4211 | // reachability here because an unreachable call is considered to |
| 4212 | // trivially dominate itself, which would lead us to rewriting its |
| 4213 | // argument in terms of its return value, which would lead to |
| 4214 | // infinite loops in GetObjCArg. |
Dan Gohman | 41375a3 | 2012-05-08 23:39:44 +0000 | [diff] [blame] | 4215 | if (DT->isReachableFromEntry(U) && DT->dominates(Inst, U)) { |
Rafael Espindola | f589278 | 2012-03-15 15:52:59 +0000 | [diff] [blame] | 4216 | Changed = true; |
| 4217 | Instruction *Replacement = Inst; |
| 4218 | Type *UseTy = U.get()->getType(); |
Dan Gohman | de8d2c4 | 2012-04-13 01:08:28 +0000 | [diff] [blame] | 4219 | if (PHINode *PHI = dyn_cast<PHINode>(U.getUser())) { |
Rafael Espindola | f589278 | 2012-03-15 15:52:59 +0000 | [diff] [blame] | 4220 | // For PHI nodes, insert the bitcast in the predecessor block. |
Dan Gohman | 41375a3 | 2012-05-08 23:39:44 +0000 | [diff] [blame] | 4221 | unsigned ValNo = PHINode::getIncomingValueNumForOperand(OperandNo); |
| 4222 | BasicBlock *BB = PHI->getIncomingBlock(ValNo); |
Rafael Espindola | f589278 | 2012-03-15 15:52:59 +0000 | [diff] [blame] | 4223 | if (Replacement->getType() != UseTy) |
| 4224 | Replacement = new BitCastInst(Replacement, UseTy, "", |
| 4225 | &BB->back()); |
Dan Gohman | 670f937 | 2012-04-13 18:57:48 +0000 | [diff] [blame] | 4226 | // While we're here, rewrite all edges for this PHI, rather |
| 4227 | // than just one use at a time, to minimize the number of |
| 4228 | // bitcasts we emit. |
Dan Gohman | dae3349 | 2012-04-27 18:56:31 +0000 | [diff] [blame] | 4229 | for (unsigned i = 0, e = PHI->getNumIncomingValues(); i != e; ++i) |
Rafael Espindola | f589278 | 2012-03-15 15:52:59 +0000 | [diff] [blame] | 4230 | if (PHI->getIncomingBlock(i) == BB) { |
| 4231 | // Keep the UI iterator valid. |
| 4232 | if (&PHI->getOperandUse( |
| 4233 | PHINode::getOperandNumForIncomingValue(i)) == |
| 4234 | &UI.getUse()) |
| 4235 | ++UI; |
| 4236 | PHI->setIncomingValue(i, Replacement); |
| 4237 | } |
| 4238 | } else { |
| 4239 | if (Replacement->getType() != UseTy) |
Dan Gohman | de8d2c4 | 2012-04-13 01:08:28 +0000 | [diff] [blame] | 4240 | Replacement = new BitCastInst(Replacement, UseTy, "", |
| 4241 | cast<Instruction>(U.getUser())); |
Rafael Espindola | f589278 | 2012-03-15 15:52:59 +0000 | [diff] [blame] | 4242 | U.set(Replacement); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 4243 | } |
Rafael Espindola | f589278 | 2012-03-15 15:52:59 +0000 | [diff] [blame] | 4244 | } |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 4245 | } |
| 4246 | |
Dan Gohman | dae3349 | 2012-04-27 18:56:31 +0000 | [diff] [blame] | 4247 | // If Arg is a no-op casted pointer, strip one level of casts and iterate. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 4248 | if (const BitCastInst *BI = dyn_cast<BitCastInst>(Arg)) |
| 4249 | Arg = BI->getOperand(0); |
| 4250 | else if (isa<GEPOperator>(Arg) && |
| 4251 | cast<GEPOperator>(Arg)->hasAllZeroIndices()) |
| 4252 | Arg = cast<GEPOperator>(Arg)->getPointerOperand(); |
| 4253 | else if (isa<GlobalAlias>(Arg) && |
| 4254 | !cast<GlobalAlias>(Arg)->mayBeOverridden()) |
| 4255 | Arg = cast<GlobalAlias>(Arg)->getAliasee(); |
| 4256 | else |
| 4257 | break; |
| 4258 | } |
| 4259 | } |
| 4260 | |
Dan Gohman | 8ee108b | 2012-01-19 19:14:36 +0000 | [diff] [blame] | 4261 | // If this function has no escaping allocas or suspicious vararg usage, |
| 4262 | // objc_storeStrong calls can be marked with the "tail" keyword. |
| 4263 | if (TailOkForStoreStrongs) |
Dan Gohman | 41375a3 | 2012-05-08 23:39:44 +0000 | [diff] [blame] | 4264 | for (SmallPtrSet<CallInst *, 8>::iterator I = StoreStrongCalls.begin(), |
Dan Gohman | 8ee108b | 2012-01-19 19:14:36 +0000 | [diff] [blame] | 4265 | E = StoreStrongCalls.end(); I != E; ++I) |
| 4266 | (*I)->setTailCall(); |
| 4267 | StoreStrongCalls.clear(); |
| 4268 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 4269 | return Changed; |
| 4270 | } |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 4271 | |
| 4272 | /// @} |
| 4273 | /// |