blob: f537d44df1aeccac8196c869b92cb174b1414f88 [file] [log] [blame]
Michael Gottesman79d8d812013-01-28 01:35:51 +00001//===- ObjCARCOpts.cpp - ObjC ARC Optimization ----------------------------===//
John McCalld935e9c2011-06-15 23:37:01 +00002//
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 Gottesman97e3df02013-01-14 00:35:14 +00009/// \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 McCalld935e9c2011-06-15 23:37:01 +000029//===----------------------------------------------------------------------===//
30
Michael Gottesman08904e32013-01-28 03:28:38 +000031#define DEBUG_TYPE "objc-arc-opts"
32#include "ObjCARC.h"
John McCalld935e9c2011-06-15 23:37:01 +000033#include "llvm/ADT/DenseMap.h"
Michael Gottesmanf15c0bb2013-01-13 22:12:06 +000034#include "llvm/ADT/SmallPtrSet.h"
John McCalld935e9c2011-06-15 23:37:01 +000035using namespace llvm;
Michael Gottesman08904e32013-01-28 03:28:38 +000036using namespace llvm::objcarc;
John McCalld935e9c2011-06-15 23:37:01 +000037
Michael Gottesman97e3df02013-01-14 00:35:14 +000038/// \defgroup MiscUtils Miscellaneous utilities that are not ARC specific.
39/// @{
John McCalld935e9c2011-06-15 23:37:01 +000040
41namespace {
Michael Gottesman97e3df02013-01-14 00:35:14 +000042 /// \brief An associative container with fast insertion-order (deterministic)
43 /// iteration over its elements. Plus the special blot operation.
John McCalld935e9c2011-06-15 23:37:01 +000044 template<class KeyT, class ValueT>
45 class MapVector {
Michael Gottesman97e3df02013-01-14 00:35:14 +000046 /// Map keys to indices in Vector.
John McCalld935e9c2011-06-15 23:37:01 +000047 typedef DenseMap<KeyT, size_t> MapTy;
48 MapTy Map;
49
John McCalld935e9c2011-06-15 23:37:01 +000050 typedef std::vector<std::pair<KeyT, ValueT> > VectorTy;
Michael Gottesman97e3df02013-01-14 00:35:14 +000051 /// Keys and values.
John McCalld935e9c2011-06-15 23:37:01 +000052 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 Gohman55b06742012-03-02 01:13:53 +000078 ValueT &operator[](const KeyT &Arg) {
John McCalld935e9c2011-06-15 23:37:01 +000079 std::pair<typename MapTy::iterator, bool> Pair =
80 Map.insert(std::make_pair(Arg, size_t(0)));
81 if (Pair.second) {
Dan Gohman55b06742012-03-02 01:13:53 +000082 size_t Num = Vector.size();
83 Pair.first->second = Num;
John McCalld935e9c2011-06-15 23:37:01 +000084 Vector.push_back(std::make_pair(Arg, ValueT()));
Dan Gohman55b06742012-03-02 01:13:53 +000085 return Vector[Num].second;
John McCalld935e9c2011-06-15 23:37:01 +000086 }
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 Gohman55b06742012-03-02 01:13:53 +000095 size_t Num = Vector.size();
96 Pair.first->second = Num;
John McCalld935e9c2011-06-15 23:37:01 +000097 Vector.push_back(InsertPair);
Dan Gohman55b06742012-03-02 01:13:53 +000098 return std::make_pair(Vector.begin() + Num, true);
John McCalld935e9c2011-06-15 23:37:01 +000099 }
100 return std::make_pair(Vector.begin() + Pair.first->second, false);
101 }
102
Dan Gohman55b06742012-03-02 01:13:53 +0000103 const_iterator find(const KeyT &Key) const {
John McCalld935e9c2011-06-15 23:37:01 +0000104 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 Gottesman97e3df02013-01-14 00:35:14 +0000109 /// 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 Gohman55b06742012-03-02 01:13:53 +0000112 void blot(const KeyT &Key) {
John McCalld935e9c2011-06-15 23:37:01 +0000113 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 Gottesman97e3df02013-01-14 00:35:14 +0000126/// @}
127///
128/// \defgroup ARCUtilities Utility declarations/definitions specific to ARC.
129/// @{
John McCalld935e9c2011-06-15 23:37:01 +0000130
Chandler Carruthed0881b2012-12-03 16:50:05 +0000131#include "llvm/Analysis/ValueTracking.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +0000132#include "llvm/IR/Intrinsics.h"
Dan Gohman41375a32012-05-08 23:39:44 +0000133#include "llvm/Support/CallSite.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +0000134#include "llvm/Transforms/Utils/Local.h"
Dan Gohman41375a32012-05-08 23:39:44 +0000135
Michael Gottesman5300cdd2013-01-27 06:19:48 +0000136/// \brief Test whether the given value is possible a retainable object pointer.
137static bool IsPotentialRetainableObjPtr(const Value *Op) {
138 // Pointers to static or stack storage are not valid retainable object pointers.
John McCalld935e9c2011-06-15 23:37:01 +0000139 if (isa<Constant>(Op) || isa<AllocaInst>(Op))
140 return false;
Michael Gottesman5300cdd2013-01-27 06:19:48 +0000141 // Special arguments can not be a valid retainable object pointer.
John McCalld935e9c2011-06-15 23:37:01 +0000142 if (const Argument *Arg = dyn_cast<Argument>(Op))
143 if (Arg->hasByValAttr() ||
144 Arg->hasNestAttr() ||
145 Arg->hasStructRetAttr())
146 return false;
Dan Gohmanbd944b42011-12-14 19:10:53 +0000147 // Only consider values with pointer types.
Michael Gottesman5300cdd2013-01-27 06:19:48 +0000148 //
Dan Gohmanbd944b42011-12-14 19:10:53 +0000149 // It seemes intuitive to exclude function pointer types as well, since
Michael Gottesman5300cdd2013-01-27 06:19:48 +0000150 // functions are never retainable object pointers, however clang occasionally
151 // bitcasts retainable object pointers to function-pointer type temporarily.
Chris Lattner229907c2011-07-18 04:54:35 +0000152 PointerType *Ty = dyn_cast<PointerType>(Op->getType());
Dan Gohmanbd944b42011-12-14 19:10:53 +0000153 if (!Ty)
John McCalld935e9c2011-06-15 23:37:01 +0000154 return false;
Michael Gottesman5300cdd2013-01-27 06:19:48 +0000155 // Conservatively assume anything else is a potential retainable object pointer.
John McCalld935e9c2011-06-15 23:37:01 +0000156 return true;
157}
158
Michael Gottesman4385edf2013-01-14 01:47:53 +0000159/// \brief Helper for GetInstructionClass. Determines what kind of construct CS
160/// is.
John McCalld935e9c2011-06-15 23:37:01 +0000161static InstructionClass GetCallSiteClass(ImmutableCallSite CS) {
162 for (ImmutableCallSite::arg_iterator I = CS.arg_begin(), E = CS.arg_end();
163 I != E; ++I)
Michael Gottesman5300cdd2013-01-27 06:19:48 +0000164 if (IsPotentialRetainableObjPtr(*I))
John McCalld935e9c2011-06-15 23:37:01 +0000165 return CS.onlyReadsMemory() ? IC_User : IC_CallOrUser;
166
167 return CS.onlyReadsMemory() ? IC_None : IC_Call;
168}
169
Michael Gottesman97e3df02013-01-14 00:35:14 +0000170/// \brief Determine what kind of construct V is.
John McCalld935e9c2011-06-15 23:37:01 +0000171static 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 McCalld935e9c2011-06-15 23:37:01 +0000191 case Intrinsic::returnaddress: case Intrinsic::frameaddress:
192 case Intrinsic::stacksave: case Intrinsic::stackrestore:
193 case Intrinsic::vastart: case Intrinsic::vacopy: case Intrinsic::vaend:
Dan Gohman41375a32012-05-08 23:39:44 +0000194 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 McCalld935e9c2011-06-15 23:37:01 +0000202 // 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 Gohman41375a32012-05-08 23:39:44 +0000207 break;
John McCalld935e9c2011-06-15 23:37:01 +0000208 }
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 Gottesman5300cdd2013-01-27 06:19:48 +0000240 if (IsPotentialRetainableObjPtr(I->getOperand(1)))
John McCalld935e9c2011-06-15 23:37:01 +0000241 return IC_User;
242 break;
243 default:
244 // For anything else, check all the operands.
Dan Gohman4b8e8ce2011-08-22 17:29:37 +0000245 // 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 McCalld935e9c2011-06-15 23:37:01 +0000249 for (User::const_op_iterator OI = I->op_begin(), OE = I->op_end();
250 OI != OE; ++OI)
Michael Gottesman5300cdd2013-01-27 06:19:48 +0000251 if (IsPotentialRetainableObjPtr(*OI))
John McCalld935e9c2011-06-15 23:37:01 +0000252 return IC_User;
253 }
254 }
255
256 // Otherwise, it's totally inert for ARC purposes.
257 return IC_None;
258}
259
Michael Gottesman97e3df02013-01-14 00:35:14 +0000260/// \brief Test if the given class is objc_retain or equivalent.
John McCalld935e9c2011-06-15 23:37:01 +0000261static bool IsRetain(InstructionClass Class) {
262 return Class == IC_Retain ||
263 Class == IC_RetainRV;
264}
265
Michael Gottesman97e3df02013-01-14 00:35:14 +0000266/// \brief Test if the given class is objc_autorelease or equivalent.
John McCalld935e9c2011-06-15 23:37:01 +0000267static bool IsAutorelease(InstructionClass Class) {
268 return Class == IC_Autorelease ||
269 Class == IC_AutoreleaseRV;
270}
271
Michael Gottesman97e3df02013-01-14 00:35:14 +0000272/// \brief Test if the given class represents instructions which return their
273/// argument verbatim.
John McCalld935e9c2011-06-15 23:37:01 +0000274static 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 Gottesman97e3df02013-01-14 00:35:14 +0000285/// \brief Test if the given class represents instructions which do nothing if
286/// passed a null pointer.
John McCalld935e9c2011-06-15 23:37:01 +0000287static 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 Gottesman4385edf2013-01-14 01:47:53 +0000296/// \brief Test if the given class represents instructions which are always safe
297/// to mark with the "tail" keyword.
John McCalld935e9c2011-06-15 23:37:01 +0000298static bool IsAlwaysTail(InstructionClass Class) {
299 // IC_RetainBlock may be given a stack argument.
300 return Class == IC_Retain ||
301 Class == IC_RetainRV ||
John McCalld935e9c2011-06-15 23:37:01 +0000302 Class == IC_AutoreleaseRV;
303}
304
Michael Gottesmanc9656fa2013-01-12 01:25:15 +0000305/// \brief Test if the given class represents instructions which are never safe
306/// to mark with the "tail" keyword.
307static 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 Gottesman97e3df02013-01-14 00:35:14 +0000316/// \brief Test if the given class represents instructions which are always safe
317/// to mark with the nounwind attribute.
John McCalld935e9c2011-06-15 23:37:01 +0000318static bool IsNoThrow(InstructionClass Class) {
Dan Gohmanfca43c22011-09-14 18:33:34 +0000319 // objc_retainBlock is not nounwind because it calls user copy constructors
320 // which could theoretically throw.
John McCalld935e9c2011-06-15 23:37:01 +0000321 return Class == IC_Retain ||
322 Class == IC_RetainRV ||
John McCalld935e9c2011-06-15 23:37:01 +0000323 Class == IC_Release ||
324 Class == IC_Autorelease ||
325 Class == IC_AutoreleaseRV ||
326 Class == IC_AutoreleasepoolPush ||
327 Class == IC_AutoreleasepoolPop;
328}
329
Michael Gottesman97e3df02013-01-14 00:35:14 +0000330/// \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 McCalld935e9c2011-06-15 23:37:01 +0000336static 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 Gottesman97e3df02013-01-14 00:35:14 +0000354/// \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 McCalld935e9c2011-06-15 23:37:01 +0000357static 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 Gottesman97e3df02013-01-14 00:35:14 +0000368/// \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 McCalld935e9c2011-06-15 23:37:01 +0000371static 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 Gottesman97e3df02013-01-14 00:35:14 +0000381/// \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 McCalld935e9c2011-06-15 23:37:01 +0000384static 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 Gottesman97e3df02013-01-14 00:35:14 +0000394/// \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 McCalld935e9c2011-06-15 23:37:01 +0000396/// casts and forwarding calls.
397static Value *GetObjCArg(Value *Inst) {
398 return StripPointerCastsAndObjCCalls(cast<CallInst>(Inst)->getArgOperand(0));
399}
400
Michael Gottesman87db35752013-01-18 23:02:45 +0000401/// \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 McCalld935e9c2011-06-15 23:37:01 +0000406static 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 Gohman56e1cef2011-08-22 17:29:11 +0000419 // 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 McCalld935e9c2011-06-15 23:37:01 +0000423 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 Gottesman97e3df02013-01-14 00:35:14 +0000438/// \brief This is similar to StripPointerCastsAndObjCCalls but it stops as soon
439/// as it finds a value with multiple uses.
John McCalld935e9c2011-06-15 23:37:01 +0000440static 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 Gohman41375a32012-05-08 23:39:44 +0000455 // 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 McCalld935e9c2011-06-15 23:37:01 +0000457 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 Gottesman4385edf2013-01-14 01:47:53 +0000471/// \brief Test whether the given pointer, which is an Objective C block
472/// pointer, does not "escape".
Michael Gottesman97e3df02013-01-14 00:35:14 +0000473///
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 Gohman728db492012-01-13 00:39:07 +0000477static bool DoesObjCBlockEscape(const Value *BlockPtr) {
Michael Gottesman1a89fe52013-01-13 07:47:32 +0000478
479 DEBUG(dbgs() << "DoesObjCBlockEscape: Target: " << *BlockPtr << "\n");
480
Dan Gohman728db492012-01-13 00:39:07 +0000481 // Walk the def-use chains.
482 SmallVector<const Value *, 4> Worklist;
483 Worklist.push_back(BlockPtr);
Michael Gottesmanf15c0bb2013-01-13 22:12:06 +0000484
485 // Ensure we do not visit any value twice.
486 SmallPtrSet<const Value *, 4> VisitedSet;
487
Dan Gohman728db492012-01-13 00:39:07 +0000488 do {
489 const Value *V = Worklist.pop_back_val();
Michael Gottesman1a89fe52013-01-13 07:47:32 +0000490
491 DEBUG(dbgs() << "DoesObjCBlockEscape: Visiting: " << *V << "\n");
492
Dan Gohman728db492012-01-13 00:39:07 +0000493 for (Value::const_use_iterator UI = V->use_begin(), UE = V->use_end();
494 UI != UE; ++UI) {
495 const User *UUser = *UI;
Michael Gottesman1a89fe52013-01-13 07:47:32 +0000496
497 DEBUG(dbgs() << "DoesObjCBlockEscape: User: " << *UUser << "\n");
498
Dan Gohman728db492012-01-13 00:39:07 +0000499 // Special - Use by a call (callee or argument) is not considered
500 // to be an escape.
Dan Gohmane1e352a2012-04-13 18:28:58 +0000501 switch (GetBasicInstructionClass(UUser)) {
502 case IC_StoreWeak:
503 case IC_InitWeak:
504 case IC_StoreStrong:
505 case IC_Autorelease:
Michael Gottesman1a89fe52013-01-13 07:47:32 +0000506 case IC_AutoreleaseRV: {
507 DEBUG(dbgs() << "DoesObjCBlockEscape: User copies pointer arguments. "
508 "Block Escapes!\n");
Dan Gohmane1e352a2012-04-13 18:28:58 +0000509 // These special functions make copies of their pointer arguments.
510 return true;
Michael Gottesman1a89fe52013-01-13 07:47:32 +0000511 }
Dan Gohmane1e352a2012-04-13 18:28:58 +0000512 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 Gottesmanf15c0bb2013-01-13 22:12:06 +0000518
Michael Gottesmane9145d32013-01-14 19:18:39 +0000519 if (!VisitedSet.insert(UUser)) {
Michael Gottesman4385edf2013-01-14 01:47:53 +0000520 DEBUG(dbgs() << "DoesObjCBlockEscape: User copies value. Escapes "
521 "if result escapes. Adding to list.\n");
Michael Gottesmanf15c0bb2013-01-13 22:12:06 +0000522 Worklist.push_back(UUser);
523 } else {
524 DEBUG(dbgs() << "DoesObjCBlockEscape: Already visited node.\n");
525 }
Dan Gohmane1e352a2012-04-13 18:28:58 +0000526 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 Gohman728db492012-01-13 00:39:07 +0000538 continue;
539 }
Dan Gohmaneb6e0152012-02-13 22:57:02 +0000540 // Otherwise, conservatively assume an escape.
Michael Gottesman1a89fe52013-01-13 07:47:32 +0000541 DEBUG(dbgs() << "DoesObjCBlockEscape: Assuming block escapes.\n");
Dan Gohman728db492012-01-13 00:39:07 +0000542 return true;
543 }
544 } while (!Worklist.empty());
545
546 // No escapes found.
Michael Gottesman1a89fe52013-01-13 07:47:32 +0000547 DEBUG(dbgs() << "DoesObjCBlockEscape: Block does not escape.\n");
Dan Gohman728db492012-01-13 00:39:07 +0000548 return false;
549}
550
Michael Gottesman97e3df02013-01-14 00:35:14 +0000551/// @}
552///
Michael Gottesman4385edf2013-01-14 01:47:53 +0000553/// \defgroup ARCAA Extends alias analysis using ObjC specific knowledge.
Michael Gottesman97e3df02013-01-14 00:35:14 +0000554/// @{
John McCalld935e9c2011-06-15 23:37:01 +0000555
John McCalld935e9c2011-06-15 23:37:01 +0000556namespace {
Michael Gottesman97e3df02013-01-14 00:35:14 +0000557 /// \brief This is a simple alias analysis implementation that uses knowledge
558 /// of ARC constructs to answer queries.
John McCalld935e9c2011-06-15 23:37:01 +0000559 ///
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 Gottesman97e3df02013-01-14 00:35:14 +0000576 /// 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 McCalld935e9c2011-06-15 23:37:01 +0000579 virtual void *getAdjustedAnalysisPointer(const void *PI) {
580 if (PI == &AliasAnalysis::ID)
Dan Gohmandae33492012-04-27 18:56:31 +0000581 return static_cast<AliasAnalysis *>(this);
John McCalld935e9c2011-06-15 23:37:01 +0000582 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...
598char ObjCARCAliasAnalysis::ID = 0;
599INITIALIZE_AG_PASS(ObjCARCAliasAnalysis, AliasAnalysis, "objc-arc-aa",
600 "ObjC-ARC-Based Alias Analysis", false, true, false)
601
602ImmutablePass *llvm::createObjCARCAliasAnalysisPass() {
603 return new ObjCARCAliasAnalysis();
604}
605
606void
607ObjCARCAliasAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
608 AU.setPreservesAll();
609 AliasAnalysis::getAnalysisUsage(AU);
610}
611
612AliasAnalysis::AliasResult
613ObjCARCAliasAnalysis::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
644bool
645ObjCARCAliasAnalysis::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
668AliasAnalysis::ModRefBehavior
669ObjCARCAliasAnalysis::getModRefBehavior(ImmutableCallSite CS) {
670 // We have nothing to do. Just chain to the next AliasAnalysis.
671 return AliasAnalysis::getModRefBehavior(CS);
672}
673
674AliasAnalysis::ModRefBehavior
675ObjCARCAliasAnalysis::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
689AliasAnalysis::ModRefResult
690ObjCARCAliasAnalysis::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 McCalld935e9c2011-06-15 23:37:01 +0000697 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 Kramerbde91762012-06-02 10:20:22 +0000704 // Note that this doesn't include objc_retainBlock, because it updates
Dan Gohmand4b5e3a2011-09-14 18:13:00 +0000705 // pointers when it copies block data.
John McCalld935e9c2011-06-15 23:37:01 +0000706 return NoModRef;
707 default:
708 break;
709 }
710
711 return AliasAnalysis::getModRefInfo(CS, Loc);
712}
713
714AliasAnalysis::ModRefResult
715ObjCARCAliasAnalysis::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 Gottesman97e3df02013-01-14 00:35:14 +0000722/// @}
723///
Michael Gottesman97e3df02013-01-14 00:35:14 +0000724/// \defgroup ARCAPElim ARC Autorelease Pool Elimination.
725/// @{
Dan Gohmane7a243f2012-01-17 20:52:24 +0000726
Dan Gohman41375a32012-05-08 23:39:44 +0000727#include "llvm/ADT/STLExtras.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +0000728#include "llvm/IR/Constants.h"
Dan Gohman82041c22012-01-18 21:19:38 +0000729
Dan Gohmane7a243f2012-01-17 20:52:24 +0000730namespace {
Michael Gottesman97e3df02013-01-14 00:35:14 +0000731 /// \brief Autorelease pool elimination.
Dan Gohmane7a243f2012-01-17 20:52:24 +0000732 class ObjCARCAPElim : public ModulePass {
733 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
734 virtual bool runOnModule(Module &M);
735
Dan Gohmandae33492012-04-27 18:56:31 +0000736 static bool MayAutorelease(ImmutableCallSite CS, unsigned Depth = 0);
737 static bool OptimizeBB(BasicBlock *BB);
Dan Gohmane7a243f2012-01-17 20:52:24 +0000738
739 public:
740 static char ID;
741 ObjCARCAPElim() : ModulePass(ID) {
742 initializeObjCARCAPElimPass(*PassRegistry::getPassRegistry());
743 }
744 };
745}
746
747char ObjCARCAPElim::ID = 0;
748INITIALIZE_PASS(ObjCARCAPElim,
749 "objc-arc-apelim",
750 "ObjC ARC autorelease pool elimination",
751 false, false)
752
753Pass *llvm::createObjCARCAPElimPass() {
754 return new ObjCARCAPElim();
755}
756
757void ObjCARCAPElim::getAnalysisUsage(AnalysisUsage &AU) const {
758 AU.setPreservesCFG();
759}
760
Michael Gottesman97e3df02013-01-14 00:35:14 +0000761/// Interprocedurally determine if calls made by the given call site can
762/// possibly produce autoreleases.
Dan Gohmandae33492012-04-27 18:56:31 +0000763bool ObjCARCAPElim::MayAutorelease(ImmutableCallSite CS, unsigned Depth) {
764 if (const Function *Callee = CS.getCalledFunction()) {
Dan Gohmane7a243f2012-01-17 20:52:24 +0000765 if (Callee->isDeclaration() || Callee->mayBeOverridden())
766 return true;
Dan Gohmandae33492012-04-27 18:56:31 +0000767 for (Function::const_iterator I = Callee->begin(), E = Callee->end();
Dan Gohmane7a243f2012-01-17 20:52:24 +0000768 I != E; ++I) {
Dan Gohmandae33492012-04-27 18:56:31 +0000769 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 Gohman8f12fae2012-01-18 21:24:45 +0000773 // 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 Gohmane7a243f2012-01-17 20:52:24 +0000778 return true;
779 }
780 return false;
781 }
782
783 return true;
784}
785
786bool 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 Gottesman4385edf2013-01-14 01:47:53 +0000801 DEBUG(dbgs() << "ObjCARCAPElim::OptimizeBB: Zapping push pop "
802 "autorelease pair:\n"
803 " Pop: " << *Inst << "\n"
Michael Gottesmanef682c52013-01-03 08:09:17 +0000804 << " Push: " << *Push << "\n");
Dan Gohmane7a243f2012-01-17 20:52:24 +0000805 Inst->eraseFromParent();
806 Push->eraseFromParent();
807 }
808 Push = 0;
809 break;
810 case IC_CallOrUser:
Dan Gohmandae33492012-04-27 18:56:31 +0000811 if (MayAutorelease(ImmutableCallSite(Inst)))
Dan Gohmane7a243f2012-01-17 20:52:24 +0000812 Push = 0;
813 break;
814 default:
815 break;
816 }
817 }
818
819 return Changed;
820}
821
822bool 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 Gohman82041c22012-01-18 21:19:38 +0000830 // Find the llvm.global_ctors variable, as the first step in
Dan Gohman670f9372012-04-13 18:57:48 +0000831 // 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 Gohman82041c22012-01-18 21:19:38 +0000836 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 Gohmane7a243f2012-01-17 20:52:24 +0000843 bool Changed = false;
844
Dan Gohman82041c22012-01-18 21:19:38 +0000845 // 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 Gohman22fbe8d2012-04-18 22:24:33 +0000852 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 Gohmane7a243f2012-01-17 20:52:24 +0000857 // Only look at function definitions.
858 if (F->isDeclaration())
859 continue;
Dan Gohmane7a243f2012-01-17 20:52:24 +0000860 // 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 Gottesman97e3df02013-01-14 00:35:14 +0000870/// @}
871///
872/// \defgroup ARCOpt ARC Optimization.
873/// @{
John McCalld935e9c2011-06-15 23:37:01 +0000874
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 Gohmanb3894012011-08-19 00:26:36 +0000909// TODO: The pointer returned from objc_loadWeakRetained is retained.
910
911// TODO: Delete release+retain pairs (rare).
Dan Gohmanceaac7c2011-06-20 23:20:43 +0000912
Chandler Carruthed0881b2012-12-03 16:50:05 +0000913#include "llvm/ADT/SmallPtrSet.h"
914#include "llvm/ADT/Statistic.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +0000915#include "llvm/IR/LLVMContext.h"
John McCalld935e9c2011-06-15 23:37:01 +0000916#include "llvm/Support/CFG.h"
John McCalld935e9c2011-06-15 23:37:01 +0000917
918STATISTIC(NumNoops, "Number of no-op objc calls eliminated");
919STATISTIC(NumPartialNoops, "Number of partially no-op objc calls eliminated");
920STATISTIC(NumAutoreleases,"Number of autoreleases converted to releases");
921STATISTIC(NumRets, "Number of return value forwarding "
922 "retain+autoreleaes eliminated");
923STATISTIC(NumRRs, "Number of retain+release paths eliminated");
924STATISTIC(NumPeeps, "Number of calls peephole-optimized");
925
926namespace {
Michael Gottesman97e3df02013-01-14 00:35:14 +0000927 /// \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 Gottesman12780c2d2013-01-24 21:35:00 +0000930 ///
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 McCalld935e9c2011-06-15 23:37:01 +0000936 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 Topperb1d83e82012-09-18 02:01:41 +0000947 void operator=(const ProvenanceAnalysis &) LLVM_DELETED_FUNCTION;
948 ProvenanceAnalysis(const ProvenanceAnalysis &) LLVM_DELETED_FUNCTION;
John McCalld935e9c2011-06-15 23:37:01 +0000949
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
965bool 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 Gohmandae33492012-04-27 18:56:31 +0000969 if (A->getCondition() == SB->getCondition())
970 return related(A->getTrueValue(), SB->getTrueValue()) ||
971 related(A->getFalseValue(), SB->getFalseValue());
John McCalld935e9c2011-06-15 23:37:01 +0000972
973 // Check both arms of the Select node individually.
Dan Gohmandae33492012-04-27 18:56:31 +0000974 return related(A->getTrueValue(), B) ||
975 related(A->getFalseValue(), B);
John McCalld935e9c2011-06-15 23:37:01 +0000976}
977
978bool 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 Gottesman97e3df02013-01-14 00:35:14 +00001003/// Test if the value of P, or any value covered by its provenance, is ever
1004/// stored within the function (not counting callees).
John McCalld935e9c2011-06-15 23:37:01 +00001005static 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
1037bool 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 Gohmandf476e52012-09-04 23:16:20 +00001062 // Check for an obvious escape.
1063 if (isa<LoadInst>(B))
1064 return isStoredObjCPointer(A);
John McCalld935e9c2011-06-15 23:37:01 +00001065 if (BIsIdentified) {
Dan Gohmandf476e52012-09-04 23:16:20 +00001066 // 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 McCalld935e9c2011-06-15 23:37:01 +00001071 }
Dan Gohmandf476e52012-09-04 23:16:20 +00001072 } else if (BIsIdentified) {
1073 // Check for an obvious escape.
1074 if (isa<LoadInst>(A))
John McCalld935e9c2011-06-15 23:37:01 +00001075 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
1092bool 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
1107namespace {
Michael Gottesman97e3df02013-01-14 00:35:14 +00001108 /// \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 McCalld935e9c2011-06-15 23:37:01 +00001112 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
1123static 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 McCalld935e9c2011-06-15 23:37:01 +00001130 if (A > B) std::swap(A, B);
1131 if (TopDown) {
1132 // Choose the side which is further along in the sequence.
Dan Gohman12130272011-08-12 00:26:31 +00001133 if ((A == S_Retain || A == S_CanRelease) &&
1134 (B == S_CanRelease || B == S_Use))
John McCalld935e9c2011-06-15 23:37:01 +00001135 return B;
1136 } else {
1137 // Choose the side which is further along in the sequence.
1138 if ((A == S_Use || A == S_CanRelease) &&
Dan Gohman12130272011-08-12 00:26:31 +00001139 (B == S_Use || B == S_Release || B == S_Stop || B == S_MovableRelease))
John McCalld935e9c2011-06-15 23:37:01 +00001140 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
1151namespace {
Michael Gottesman97e3df02013-01-14 00:35:14 +00001152 /// \brief Unidirectional information about either a
John McCalld935e9c2011-06-15 23:37:01 +00001153 /// retain-decrement-use-release sequence or release-use-decrement-retain
1154 /// reverese sequence.
1155 struct RRInfo {
Michael Gottesman97e3df02013-01-14 00:35:14 +00001156 /// After an objc_retain, the reference count of the referenced
Dan Gohmanb3894012011-08-19 00:26:36 +00001157 /// 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 McCalld935e9c2011-06-15 23:37:01 +00001169
Michael Gottesman97e3df02013-01-14 00:35:14 +00001170 /// True if the Calls are objc_retainBlock calls (as opposed to objc_retain
1171 /// calls).
John McCalld935e9c2011-06-15 23:37:01 +00001172 bool IsRetainBlock;
1173
Michael Gottesman97e3df02013-01-14 00:35:14 +00001174 /// True of the objc_release calls are all marked with the "tail" keyword.
John McCalld935e9c2011-06-15 23:37:01 +00001175 bool IsTailCallRelease;
1176
Michael Gottesman97e3df02013-01-14 00:35:14 +00001177 /// If the Calls are objc_release calls and they all have a
1178 /// clang.imprecise_release tag, this is the metadata tag.
John McCalld935e9c2011-06-15 23:37:01 +00001179 MDNode *ReleaseMetadata;
1180
Michael Gottesman97e3df02013-01-14 00:35:14 +00001181 /// For a top-down sequence, the set of objc_retains or
John McCalld935e9c2011-06-15 23:37:01 +00001182 /// objc_retainBlocks. For bottom-up, the set of objc_releases.
1183 SmallPtrSet<Instruction *, 2> Calls;
1184
Michael Gottesman97e3df02013-01-14 00:35:14 +00001185 /// The set of optimal insert positions for moving calls in the opposite
1186 /// sequence.
John McCalld935e9c2011-06-15 23:37:01 +00001187 SmallPtrSet<Instruction *, 2> ReverseInsertPts;
1188
1189 RRInfo() :
Dan Gohman728db492012-01-13 00:39:07 +00001190 KnownSafe(false), IsRetainBlock(false),
Dan Gohman62079b42012-04-25 00:50:46 +00001191 IsTailCallRelease(false),
John McCalld935e9c2011-06-15 23:37:01 +00001192 ReleaseMetadata(0) {}
1193
1194 void clear();
1195 };
1196}
1197
1198void RRInfo::clear() {
Dan Gohmanb3894012011-08-19 00:26:36 +00001199 KnownSafe = false;
John McCalld935e9c2011-06-15 23:37:01 +00001200 IsRetainBlock = false;
1201 IsTailCallRelease = false;
1202 ReleaseMetadata = 0;
1203 Calls.clear();
1204 ReverseInsertPts.clear();
1205}
1206
1207namespace {
Michael Gottesman97e3df02013-01-14 00:35:14 +00001208 /// \brief This class summarizes several per-pointer runtime properties which
1209 /// are propogated through the flow graph.
John McCalld935e9c2011-06-15 23:37:01 +00001210 class PtrState {
Michael Gottesman97e3df02013-01-14 00:35:14 +00001211 /// True if the reference count is known to be incremented.
Dan Gohman62079b42012-04-25 00:50:46 +00001212 bool KnownPositiveRefCount;
1213
Michael Gottesman97e3df02013-01-14 00:35:14 +00001214 /// 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 Gohman62079b42012-04-25 00:50:46 +00001216 bool Partial;
John McCalld935e9c2011-06-15 23:37:01 +00001217
Michael Gottesman97e3df02013-01-14 00:35:14 +00001218 /// The current position in the sequence.
Dan Gohman41375a32012-05-08 23:39:44 +00001219 Sequence Seq : 8;
John McCalld935e9c2011-06-15 23:37:01 +00001220
1221 public:
Michael Gottesman97e3df02013-01-14 00:35:14 +00001222 /// Unidirectional information about the current sequence.
1223 ///
John McCalld935e9c2011-06-15 23:37:01 +00001224 /// TODO: Encapsulate this better.
1225 RRInfo RRI;
1226
Dan Gohmandf476e52012-09-04 23:16:20 +00001227 PtrState() : KnownPositiveRefCount(false), Partial(false),
Dan Gohman41375a32012-05-08 23:39:44 +00001228 Seq(S_None) {}
John McCalld935e9c2011-06-15 23:37:01 +00001229
Dan Gohman62079b42012-04-25 00:50:46 +00001230 void SetKnownPositiveRefCount() {
1231 KnownPositiveRefCount = true;
Dan Gohman12130272011-08-12 00:26:31 +00001232 }
1233
Dan Gohman62079b42012-04-25 00:50:46 +00001234 void ClearRefCount() {
1235 KnownPositiveRefCount = false;
John McCalld935e9c2011-06-15 23:37:01 +00001236 }
1237
John McCalld935e9c2011-06-15 23:37:01 +00001238 bool IsKnownIncremented() const {
Dan Gohman62079b42012-04-25 00:50:46 +00001239 return KnownPositiveRefCount;
John McCalld935e9c2011-06-15 23:37:01 +00001240 }
1241
1242 void SetSeq(Sequence NewSeq) {
1243 Seq = NewSeq;
1244 }
1245
John McCalld935e9c2011-06-15 23:37:01 +00001246 Sequence GetSeq() const {
1247 return Seq;
1248 }
1249
1250 void ClearSequenceProgress() {
Dan Gohman62079b42012-04-25 00:50:46 +00001251 ResetSequenceProgress(S_None);
1252 }
1253
1254 void ResetSequenceProgress(Sequence NewSeq) {
1255 Seq = NewSeq;
1256 Partial = false;
John McCalld935e9c2011-06-15 23:37:01 +00001257 RRI.clear();
1258 }
1259
1260 void Merge(const PtrState &Other, bool TopDown);
1261 };
1262}
1263
1264void
1265PtrState::Merge(const PtrState &Other, bool TopDown) {
1266 Seq = MergeSeqs(Seq, Other.Seq, TopDown);
Dan Gohman62079b42012-04-25 00:50:46 +00001267 KnownPositiveRefCount = KnownPositiveRefCount && Other.KnownPositiveRefCount;
John McCalld935e9c2011-06-15 23:37:01 +00001268
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 Gohman1736c142011-10-17 18:48:25 +00001273 // If we're not in a sequence (anymore), drop all associated state.
John McCalld935e9c2011-06-15 23:37:01 +00001274 if (Seq == S_None) {
Dan Gohman62079b42012-04-25 00:50:46 +00001275 Partial = false;
John McCalld935e9c2011-06-15 23:37:01 +00001276 RRI.clear();
Dan Gohman62079b42012-04-25 00:50:46 +00001277 } else if (Partial || Other.Partial) {
Dan Gohman1736c142011-10-17 18:48:25 +00001278 // 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 Gohman62079b42012-04-25 00:50:46 +00001282 ClearSequenceProgress();
John McCalld935e9c2011-06-15 23:37:01 +00001283 } else {
1284 // Conservatively merge the ReleaseMetadata information.
1285 if (RRI.ReleaseMetadata != Other.RRI.ReleaseMetadata)
1286 RRI.ReleaseMetadata = 0;
1287
Dan Gohmanb3894012011-08-19 00:26:36 +00001288 RRI.KnownSafe = RRI.KnownSafe && Other.RRI.KnownSafe;
Dan Gohman41375a32012-05-08 23:39:44 +00001289 RRI.IsTailCallRelease = RRI.IsTailCallRelease &&
1290 Other.RRI.IsTailCallRelease;
John McCalld935e9c2011-06-15 23:37:01 +00001291 RRI.Calls.insert(Other.RRI.Calls.begin(), Other.RRI.Calls.end());
Dan Gohman1736c142011-10-17 18:48:25 +00001292
1293 // Merge the insert point sets. If there are any differences,
1294 // that makes this a partial merge.
Dan Gohman41375a32012-05-08 23:39:44 +00001295 Partial = RRI.ReverseInsertPts.size() != Other.RRI.ReverseInsertPts.size();
Dan Gohman1736c142011-10-17 18:48:25 +00001296 for (SmallPtrSet<Instruction *, 2>::const_iterator
1297 I = Other.RRI.ReverseInsertPts.begin(),
1298 E = Other.RRI.ReverseInsertPts.end(); I != E; ++I)
Dan Gohman62079b42012-04-25 00:50:46 +00001299 Partial |= RRI.ReverseInsertPts.insert(*I);
John McCalld935e9c2011-06-15 23:37:01 +00001300 }
1301}
1302
1303namespace {
Michael Gottesman97e3df02013-01-14 00:35:14 +00001304 /// \brief Per-BasicBlock state.
John McCalld935e9c2011-06-15 23:37:01 +00001305 class BBState {
Michael Gottesman97e3df02013-01-14 00:35:14 +00001306 /// The number of unique control paths from the entry which can reach this
1307 /// block.
John McCalld935e9c2011-06-15 23:37:01 +00001308 unsigned TopDownPathCount;
1309
Michael Gottesman97e3df02013-01-14 00:35:14 +00001310 /// The number of unique control paths to exits from this block.
John McCalld935e9c2011-06-15 23:37:01 +00001311 unsigned BottomUpPathCount;
1312
Michael Gottesman97e3df02013-01-14 00:35:14 +00001313 /// A type for PerPtrTopDown and PerPtrBottomUp.
John McCalld935e9c2011-06-15 23:37:01 +00001314 typedef MapVector<const Value *, PtrState> MapTy;
1315
Michael Gottesman97e3df02013-01-14 00:35:14 +00001316 /// The top-down traversal uses this to record information known about a
1317 /// pointer at the bottom of each block.
John McCalld935e9c2011-06-15 23:37:01 +00001318 MapTy PerPtrTopDown;
1319
Michael Gottesman97e3df02013-01-14 00:35:14 +00001320 /// The bottom-up traversal uses this to record information known about a
1321 /// pointer at the top of each block.
John McCalld935e9c2011-06-15 23:37:01 +00001322 MapTy PerPtrBottomUp;
1323
Michael Gottesman97e3df02013-01-14 00:35:14 +00001324 /// Effective predecessors of the current block ignoring ignorable edges and
1325 /// ignored backedges.
Dan Gohmanc24c66f2012-04-24 22:53:18 +00001326 SmallVector<BasicBlock *, 2> Preds;
Michael Gottesman97e3df02013-01-14 00:35:14 +00001327 /// Effective successors of the current block ignoring ignorable edges and
1328 /// ignored backedges.
Dan Gohmanc24c66f2012-04-24 22:53:18 +00001329 SmallVector<BasicBlock *, 2> Succs;
1330
John McCalld935e9c2011-06-15 23:37:01 +00001331 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 Gottesman97e3df02013-01-14 00:35:14 +00001355 /// Mark this block as being an entry block, which has one path from the
1356 /// entry by definition.
John McCalld935e9c2011-06-15 23:37:01 +00001357 void SetAsEntry() { TopDownPathCount = 1; }
1358
Michael Gottesman97e3df02013-01-14 00:35:14 +00001359 /// Mark this block as being an exit block, which has one path to an exit by
1360 /// definition.
John McCalld935e9c2011-06-15 23:37:01 +00001361 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 Chenge4df6a22011-08-04 18:40:26 +00001372 PerPtrBottomUp.clear();
John McCalld935e9c2011-06-15 23:37:01 +00001373 }
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 Gottesman97e3df02013-01-14 00:35:14 +00001384 /// 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 McCalld935e9c2011-06-15 23:37:01 +00001387 unsigned GetAllPathCount() const {
Dan Gohmanc24c66f2012-04-24 22:53:18 +00001388 assert(TopDownPathCount != 0);
1389 assert(BottomUpPathCount != 0);
John McCalld935e9c2011-06-15 23:37:01 +00001390 return TopDownPathCount * BottomUpPathCount;
1391 }
Dan Gohman12130272011-08-12 00:26:31 +00001392
Dan Gohmanc24c66f2012-04-24 22:53:18 +00001393 // Specialized CFG utilities.
Dan Gohmandae33492012-04-27 18:56:31 +00001394 typedef SmallVectorImpl<BasicBlock *>::const_iterator edge_iterator;
Dan Gohmanc24c66f2012-04-24 22:53:18 +00001395 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 McCalld935e9c2011-06-15 23:37:01 +00001404 };
1405}
1406
1407void BBState::InitFromPred(const BBState &Other) {
1408 PerPtrTopDown = Other.PerPtrTopDown;
1409 TopDownPathCount = Other.TopDownPathCount;
1410}
1411
1412void BBState::InitFromSucc(const BBState &Other) {
1413 PerPtrBottomUp = Other.PerPtrBottomUp;
1414 BottomUpPathCount = Other.BottomUpPathCount;
1415}
1416
Michael Gottesman97e3df02013-01-14 00:35:14 +00001417/// The top-down traversal uses this to merge information about predecessors to
1418/// form the initial state for a new block.
John McCalld935e9c2011-06-15 23:37:01 +00001419void 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 Gottesman4385edf2013-01-14 01:47:53 +00001424 // Check for overflow. If we have overflow, fall back to conservative
1425 // behavior.
Dan Gohman7c84dad2012-09-12 20:45:17 +00001426 if (TopDownPathCount < Other.TopDownPathCount) {
1427 clearTopDownPointers();
1428 return;
1429 }
1430
John McCalld935e9c2011-06-15 23:37:01 +00001431 // 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 Gohman7e315fc32011-08-11 21:06:32 +00001441 // For each entry in our set, if the other set doesn't have an entry with the
John McCalld935e9c2011-06-15 23:37:01 +00001442 // 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 Gottesman97e3df02013-01-14 00:35:14 +00001449/// The bottom-up traversal uses this to merge information about successors to
1450/// form the initial state for a new block.
John McCalld935e9c2011-06-15 23:37:01 +00001451void 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 Gottesman4385edf2013-01-14 01:47:53 +00001456 // Check for overflow. If we have overflow, fall back to conservative
1457 // behavior.
Dan Gohman7c84dad2012-09-12 20:45:17 +00001458 if (BottomUpPathCount < Other.BottomUpPathCount) {
1459 clearBottomUpPointers();
1460 return;
1461 }
1462
John McCalld935e9c2011-06-15 23:37:01 +00001463 // 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 Gohman7e315fc32011-08-11 21:06:32 +00001473 // For each entry in our set, if the other set doesn't have an entry
John McCalld935e9c2011-06-15 23:37:01 +00001474 // 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
1481namespace {
Michael Gottesman97e3df02013-01-14 00:35:14 +00001482 /// \brief The main ARC optimization pass.
John McCalld935e9c2011-06-15 23:37:01 +00001483 class ObjCARCOpt : public FunctionPass {
1484 bool Changed;
1485 ProvenanceAnalysis PA;
1486
Michael Gottesman97e3df02013-01-14 00:35:14 +00001487 /// A flag indicating whether this optimization pass should run.
Dan Gohmanceaac7c2011-06-20 23:20:43 +00001488 bool Run;
1489
Michael Gottesman97e3df02013-01-14 00:35:14 +00001490 /// 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 McCalld935e9c2011-06-15 23:37:01 +00001493
Michael Gottesman97e3df02013-01-14 00:35:14 +00001494 /// 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 McCalld935e9c2011-06-15 23:37:01 +00001510 unsigned UsedInThisFunction;
1511
Michael Gottesman97e3df02013-01-14 00:35:14 +00001512 /// The Metadata Kind for clang.imprecise_release metadata.
John McCalld935e9c2011-06-15 23:37:01 +00001513 unsigned ImpreciseReleaseMDKind;
1514
Michael Gottesman97e3df02013-01-14 00:35:14 +00001515 /// The Metadata Kind for clang.arc.copy_on_escape metadata.
Dan Gohmana7107f92011-10-17 22:53:25 +00001516 unsigned CopyOnEscapeMDKind;
1517
Michael Gottesman97e3df02013-01-14 00:35:14 +00001518 /// The Metadata Kind for clang.arc.no_objc_arc_exceptions metadata.
Dan Gohman0155f302012-02-17 18:59:53 +00001519 unsigned NoObjCARCExceptionsMDKind;
1520
John McCalld935e9c2011-06-15 23:37:01 +00001521 Constant *getRetainRVCallee(Module *M);
1522 Constant *getAutoreleaseRVCallee(Module *M);
1523 Constant *getReleaseCallee(Module *M);
1524 Constant *getRetainCallee(Module *M);
Dan Gohman6320f522011-07-22 22:29:21 +00001525 Constant *getRetainBlockCallee(Module *M);
John McCalld935e9c2011-06-15 23:37:01 +00001526 Constant *getAutoreleaseCallee(Module *M);
1527
Dan Gohman728db492012-01-13 00:39:07 +00001528 bool IsRetainBlockOptimizable(const Instruction *Inst);
1529
John McCalld935e9c2011-06-15 23:37:01 +00001530 void OptimizeRetainCall(Function &F, Instruction *Retain);
1531 bool OptimizeRetainRVCall(Function &F, Instruction *RetainRV);
Michael Gottesman556ff612013-01-12 01:25:19 +00001532 void OptimizeAutoreleaseRVCall(Function &F, Instruction *AutoreleaseRV,
1533 InstructionClass &Class);
John McCalld935e9c2011-06-15 23:37:01 +00001534 void OptimizeIndividualCalls(Function &F);
1535
1536 void CheckForCFGHazards(const BasicBlock *BB,
1537 DenseMap<const BasicBlock *, BBState> &BBStates,
1538 BBState &MyStates) const;
Dan Gohman817a7c62012-03-22 18:24:56 +00001539 bool VisitInstructionBottomUp(Instruction *Inst,
Dan Gohman5c70fad2012-03-23 17:47:54 +00001540 BasicBlock *BB,
Dan Gohman817a7c62012-03-22 18:24:56 +00001541 MapVector<Value *, RRInfo> &Retains,
1542 BBState &MyStates);
John McCalld935e9c2011-06-15 23:37:01 +00001543 bool VisitBottomUp(BasicBlock *BB,
1544 DenseMap<const BasicBlock *, BBState> &BBStates,
1545 MapVector<Value *, RRInfo> &Retains);
Dan Gohman817a7c62012-03-22 18:24:56 +00001546 bool VisitInstructionTopDown(Instruction *Inst,
1547 DenseMap<Value *, RRInfo> &Releases,
1548 BBState &MyStates);
John McCalld935e9c2011-06-15 23:37:01 +00001549 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 Gohman6320f522011-07-22 22:29:21 +00001560 SmallVectorImpl<Instruction *> &DeadInsts,
1561 Module *M);
John McCalld935e9c2011-06-15 23:37:01 +00001562
Michael Gottesman9de6f962013-01-22 21:49:00 +00001563 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 McCalld935e9c2011-06-15 23:37:01 +00001576 bool PerformCodePlacement(DenseMap<const BasicBlock *, BBState> &BBStates,
1577 MapVector<Value *, RRInfo> &Retains,
Dan Gohman6320f522011-07-22 22:29:21 +00001578 DenseMap<Value *, RRInfo> &Releases,
1579 Module *M);
John McCalld935e9c2011-06-15 23:37:01 +00001580
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
1600char ObjCARCOpt::ID = 0;
1601INITIALIZE_PASS_BEGIN(ObjCARCOpt,
1602 "objc-arc", "ObjC ARC optimization", false, false)
1603INITIALIZE_PASS_DEPENDENCY(ObjCARCAliasAnalysis)
1604INITIALIZE_PASS_END(ObjCARCOpt,
1605 "objc-arc", "ObjC ARC optimization", false, false)
1606
1607Pass *llvm::createObjCARCOptPass() {
1608 return new ObjCARCOpt();
1609}
1610
1611void 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 Gohman728db492012-01-13 00:39:07 +00001618bool 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 McCalld935e9c2011-06-15 23:37:01 +00001634Constant *ObjCARCOpt::getRetainRVCallee(Module *M) {
1635 if (!RetainRVCallee) {
1636 LLVMContext &C = M->getContext();
Jay Foadb804a2b2011-07-12 14:06:48 +00001637 Type *I8X = PointerType::getUnqual(Type::getInt8Ty(C));
Dan Gohman41375a32012-05-08 23:39:44 +00001638 Type *Params[] = { I8X };
1639 FunctionType *FTy = FunctionType::get(I8X, Params, /*isVarArg=*/false);
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001640 AttributeSet Attribute =
Bill Wendling09175b32013-01-22 21:15:51 +00001641 AttributeSet().addAttribute(M->getContext(), AttributeSet::FunctionIndex,
1642 Attribute::NoUnwind);
John McCalld935e9c2011-06-15 23:37:01 +00001643 RetainRVCallee =
1644 M->getOrInsertFunction("objc_retainAutoreleasedReturnValue", FTy,
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001645 Attribute);
John McCalld935e9c2011-06-15 23:37:01 +00001646 }
1647 return RetainRVCallee;
1648}
1649
1650Constant *ObjCARCOpt::getAutoreleaseRVCallee(Module *M) {
1651 if (!AutoreleaseRVCallee) {
1652 LLVMContext &C = M->getContext();
Jay Foadb804a2b2011-07-12 14:06:48 +00001653 Type *I8X = PointerType::getUnqual(Type::getInt8Ty(C));
Dan Gohman41375a32012-05-08 23:39:44 +00001654 Type *Params[] = { I8X };
1655 FunctionType *FTy = FunctionType::get(I8X, Params, /*isVarArg=*/false);
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001656 AttributeSet Attribute =
Bill Wendling09175b32013-01-22 21:15:51 +00001657 AttributeSet().addAttribute(M->getContext(), AttributeSet::FunctionIndex,
1658 Attribute::NoUnwind);
John McCalld935e9c2011-06-15 23:37:01 +00001659 AutoreleaseRVCallee =
1660 M->getOrInsertFunction("objc_autoreleaseReturnValue", FTy,
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001661 Attribute);
John McCalld935e9c2011-06-15 23:37:01 +00001662 }
1663 return AutoreleaseRVCallee;
1664}
1665
1666Constant *ObjCARCOpt::getReleaseCallee(Module *M) {
1667 if (!ReleaseCallee) {
1668 LLVMContext &C = M->getContext();
Dan Gohman41375a32012-05-08 23:39:44 +00001669 Type *Params[] = { PointerType::getUnqual(Type::getInt8Ty(C)) };
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001670 AttributeSet Attribute =
Bill Wendling09175b32013-01-22 21:15:51 +00001671 AttributeSet().addAttribute(M->getContext(), AttributeSet::FunctionIndex,
1672 Attribute::NoUnwind);
John McCalld935e9c2011-06-15 23:37:01 +00001673 ReleaseCallee =
1674 M->getOrInsertFunction(
1675 "objc_release",
1676 FunctionType::get(Type::getVoidTy(C), Params, /*isVarArg=*/false),
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001677 Attribute);
John McCalld935e9c2011-06-15 23:37:01 +00001678 }
1679 return ReleaseCallee;
1680}
1681
1682Constant *ObjCARCOpt::getRetainCallee(Module *M) {
1683 if (!RetainCallee) {
1684 LLVMContext &C = M->getContext();
Dan Gohman41375a32012-05-08 23:39:44 +00001685 Type *Params[] = { PointerType::getUnqual(Type::getInt8Ty(C)) };
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001686 AttributeSet Attribute =
Bill Wendling09175b32013-01-22 21:15:51 +00001687 AttributeSet().addAttribute(M->getContext(), AttributeSet::FunctionIndex,
1688 Attribute::NoUnwind);
John McCalld935e9c2011-06-15 23:37:01 +00001689 RetainCallee =
1690 M->getOrInsertFunction(
1691 "objc_retain",
1692 FunctionType::get(Params[0], Params, /*isVarArg=*/false),
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001693 Attribute);
John McCalld935e9c2011-06-15 23:37:01 +00001694 }
1695 return RetainCallee;
1696}
1697
Dan Gohman6320f522011-07-22 22:29:21 +00001698Constant *ObjCARCOpt::getRetainBlockCallee(Module *M) {
1699 if (!RetainBlockCallee) {
1700 LLVMContext &C = M->getContext();
Dan Gohman41375a32012-05-08 23:39:44 +00001701 Type *Params[] = { PointerType::getUnqual(Type::getInt8Ty(C)) };
Dan Gohmanfca43c22011-09-14 18:33:34 +00001702 // objc_retainBlock is not nounwind because it calls user copy constructors
1703 // which could theoretically throw.
Dan Gohman6320f522011-07-22 22:29:21 +00001704 RetainBlockCallee =
1705 M->getOrInsertFunction(
1706 "objc_retainBlock",
1707 FunctionType::get(Params[0], Params, /*isVarArg=*/false),
Bill Wendlinge94d8432012-12-07 23:16:57 +00001708 AttributeSet());
Dan Gohman6320f522011-07-22 22:29:21 +00001709 }
1710 return RetainBlockCallee;
1711}
1712
John McCalld935e9c2011-06-15 23:37:01 +00001713Constant *ObjCARCOpt::getAutoreleaseCallee(Module *M) {
1714 if (!AutoreleaseCallee) {
1715 LLVMContext &C = M->getContext();
Dan Gohman41375a32012-05-08 23:39:44 +00001716 Type *Params[] = { PointerType::getUnqual(Type::getInt8Ty(C)) };
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001717 AttributeSet Attribute =
Bill Wendling09175b32013-01-22 21:15:51 +00001718 AttributeSet().addAttribute(M->getContext(), AttributeSet::FunctionIndex,
1719 Attribute::NoUnwind);
John McCalld935e9c2011-06-15 23:37:01 +00001720 AutoreleaseCallee =
1721 M->getOrInsertFunction(
1722 "objc_autorelease",
1723 FunctionType::get(Params[0], Params, /*isVarArg=*/false),
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001724 Attribute);
John McCalld935e9c2011-06-15 23:37:01 +00001725 }
1726 return AutoreleaseCallee;
1727}
1728
Michael Gottesman97e3df02013-01-14 00:35:14 +00001729/// Test whether the given value is possible a reference-counted pointer,
1730/// including tests which utilize AliasAnalysis.
Michael Gottesman5300cdd2013-01-27 06:19:48 +00001731static bool IsPotentialRetainableObjPtr(const Value *Op, AliasAnalysis &AA) {
Dan Gohmandf476e52012-09-04 23:16:20 +00001732 // First make the rudimentary check.
Michael Gottesman5300cdd2013-01-27 06:19:48 +00001733 if (!IsPotentialRetainableObjPtr(Op))
Dan Gohmandf476e52012-09-04 23:16:20 +00001734 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 Gottesman97e3df02013-01-14 00:35:14 +00001749/// Test whether the given instruction can result in a reference count
1750/// modification (positive or negative) for the pointer's object.
John McCalld935e9c2011-06-15 23:37:01 +00001751static bool
1752CanAlterRefCount(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 Gottesman5300cdd2013-01-27 06:19:48 +00001774 if (IsPotentialRetainableObjPtr(Op, *PA.getAA()) && PA.related(Ptr, Op))
John McCalld935e9c2011-06-15 23:37:01 +00001775 return true;
1776 }
1777 return false;
1778 }
1779
1780 // Assume the worst.
1781 return true;
1782}
1783
Michael Gottesman97e3df02013-01-14 00:35:14 +00001784/// 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 McCalld935e9c2011-06-15 23:37:01 +00001786static bool
1787CanUse(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 Gottesman5300cdd2013-01-27 06:19:48 +00001799 if (!IsPotentialRetainableObjPtr(ICI->getOperand(1), *PA.getAA()))
John McCalld935e9c2011-06-15 23:37:01 +00001800 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 Gottesman5300cdd2013-01-27 06:19:48 +00001806 if (IsPotentialRetainableObjPtr(Op, *PA.getAA()) && PA.related(Ptr, Op))
John McCalld935e9c2011-06-15 23:37:01 +00001807 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 Gottesman5300cdd2013-01-27 06:19:48 +00001816 return IsPotentialRetainableObjPtr(Op, *PA.getAA()) && PA.related(Op, Ptr);
John McCalld935e9c2011-06-15 23:37:01 +00001817 }
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 Gottesman5300cdd2013-01-27 06:19:48 +00001823 if (IsPotentialRetainableObjPtr(Op, *PA.getAA()) && PA.related(Ptr, Op))
John McCalld935e9c2011-06-15 23:37:01 +00001824 return true;
1825 }
1826 return false;
1827}
1828
Michael Gottesman97e3df02013-01-14 00:35:14 +00001829/// Test whether the given instruction can autorelease any pointer or cause an
1830/// autoreleasepool pop.
John McCalld935e9c2011-06-15 23:37:01 +00001831static bool
1832CanInterruptRV(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
1847namespace {
Michael Gottesman97e3df02013-01-14 00:35:14 +00001848 /// \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 McCalld935e9c2011-06-15 23:37:01 +00001853 enum DependenceKind {
1854 NeedsPositiveRetainCount,
Dan Gohman8478d762012-04-13 00:59:57 +00001855 AutoreleasePoolBoundary,
John McCalld935e9c2011-06-15 23:37:01 +00001856 CanChangeRetainCount,
1857 RetainAutoreleaseDep, ///< Blocks objc_retainAutorelease.
1858 RetainAutoreleaseRVDep, ///< Blocks objc_retainAutoreleaseReturnValue.
1859 RetainRVDep ///< Blocks objc_retainAutoreleasedReturnValue.
1860 };
1861}
1862
Michael Gottesman97e3df02013-01-14 00:35:14 +00001863/// Test if there can be dependencies on Inst through Arg. This function only
1864/// tests dependencies relevant for removing pairs of calls.
John McCalld935e9c2011-06-15 23:37:01 +00001865static bool
1866Depends(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 Gohman8478d762012-04-13 00:59:57 +00001885 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 McCalld935e9c2011-06-15 23:37:01 +00001898 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 Gohman8478d762012-04-13 00:59:57 +00001915 case IC_AutoreleasepoolPush:
John McCalld935e9c2011-06-15 23:37:01 +00001916 // 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 McCalld935e9c2011-06-15 23:37:01 +00001927
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 McCalld935e9c2011-06-15 23:37:01 +00001940 }
1941
1942 case RetainRVDep:
1943 return CanInterruptRV(GetBasicInstructionClass(Inst));
1944 }
1945
1946 llvm_unreachable("Invalid dependence flavor");
John McCalld935e9c2011-06-15 23:37:01 +00001947}
1948
Michael Gottesman97e3df02013-01-14 00:35:14 +00001949/// Walk up the CFG from StartPos (which is in StartBB) and find local and
1950/// non-local dependencies on Arg.
1951///
John McCalld935e9c2011-06-15 23:37:01 +00001952/// TODO: Cache results?
1953static void
1954FindDependencies(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
2013static bool isNullOrUndef(const Value *V) {
2014 return isa<ConstantPointerNull>(V) || isa<UndefValue>(V);
2015}
2016
2017static bool isNoopInstruction(const Instruction *I) {
2018 return isa<BitCastInst>(I) ||
2019 (isa<GetElementPtrInst>(I) &&
2020 cast<GetElementPtrInst>(I)->hasAllZeroIndices());
2021}
2022
Michael Gottesman97e3df02013-01-14 00:35:14 +00002023/// Turn objc_retain into objc_retainAutoreleasedReturnValue if the operand is a
2024/// return value.
John McCalld935e9c2011-06-15 23:37:01 +00002025void
2026ObjCARCOpt::OptimizeRetainCall(Function &F, Instruction *Retain) {
Dan Gohmandae33492012-04-27 18:56:31 +00002027 ImmutableCallSite CS(GetObjCArg(Retain));
2028 const Instruction *Call = CS.getInstruction();
John McCalld935e9c2011-06-15 23:37:01 +00002029 if (!Call) return;
2030 if (Call->getParent() != Retain->getParent()) return;
2031
2032 // Check that the call is next to the retain.
Dan Gohmandae33492012-04-27 18:56:31 +00002033 BasicBlock::const_iterator I = Call;
John McCalld935e9c2011-06-15 23:37:01 +00002034 ++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 Gottesman10426b52013-01-07 21:26:07 +00002042
Michael Gottesman1e00ac62013-01-04 21:30:38 +00002043 DEBUG(dbgs() << "ObjCARCOpt::OptimizeRetainCall: Transforming "
Michael Gottesman9f1be682013-01-12 03:45:49 +00002044 "objc_retain => objc_retainAutoreleasedReturnValue"
2045 " since the operand is a return value.\n"
Michael Gottesman1e00ac62013-01-04 21:30:38 +00002046 " Old: "
2047 << *Retain << "\n");
Michael Gottesman10426b52013-01-07 21:26:07 +00002048
John McCalld935e9c2011-06-15 23:37:01 +00002049 cast<CallInst>(Retain)->setCalledFunction(getRetainRVCallee(F.getParent()));
Michael Gottesman1e00ac62013-01-04 21:30:38 +00002050
2051 DEBUG(dbgs() << " New: "
2052 << *Retain << "\n");
John McCalld935e9c2011-06-15 23:37:01 +00002053}
2054
Michael Gottesman97e3df02013-01-14 00:35:14 +00002055/// 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 McCalld935e9c2011-06-15 23:37:01 +00002058bool
2059ObjCARCOpt::OptimizeRetainRVCall(Function &F, Instruction *RetainRV) {
Dan Gohmane3ed2b02012-03-23 18:09:00 +00002060 // Check for the argument being from an immediately preceding call or invoke.
Dan Gohmandae33492012-04-27 18:56:31 +00002061 const Value *Arg = GetObjCArg(RetainRV);
2062 ImmutableCallSite CS(Arg);
2063 if (const Instruction *Call = CS.getInstruction()) {
John McCalld935e9c2011-06-15 23:37:01 +00002064 if (Call->getParent() == RetainRV->getParent()) {
Dan Gohmandae33492012-04-27 18:56:31 +00002065 BasicBlock::const_iterator I = Call;
John McCalld935e9c2011-06-15 23:37:01 +00002066 ++I;
2067 while (isNoopInstruction(I)) ++I;
2068 if (&*I == RetainRV)
2069 return false;
Dan Gohmandae33492012-04-27 18:56:31 +00002070 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(Call)) {
Dan Gohmane3ed2b02012-03-23 18:09:00 +00002071 BasicBlock *RetainRVParent = RetainRV->getParent();
2072 if (II->getNormalDest() == RetainRVParent) {
Dan Gohmandae33492012-04-27 18:56:31 +00002073 BasicBlock::const_iterator I = RetainRVParent->begin();
Dan Gohmane3ed2b02012-03-23 18:09:00 +00002074 while (isNoopInstruction(I)) ++I;
2075 if (&*I == RetainRV)
2076 return false;
2077 }
John McCalld935e9c2011-06-15 23:37:01 +00002078 }
Dan Gohmane3ed2b02012-03-23 18:09:00 +00002079 }
John McCalld935e9c2011-06-15 23:37:01 +00002080
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 Gottesman10426b52013-01-07 21:26:07 +00002090
Michael Gottesman5c32ce92013-01-05 17:55:35 +00002091 DEBUG(dbgs() << "ObjCARCOpt::OptimizeRetainRVCall: Erasing " << *I << "\n"
2092 << " Erasing " << *RetainRV
2093 << "\n");
Michael Gottesman10426b52013-01-07 21:26:07 +00002094
John McCalld935e9c2011-06-15 23:37:01 +00002095 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 Gottesman10426b52013-01-07 21:26:07 +00002104
Michael Gottesmandef07bb2013-01-05 17:55:42 +00002105 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 Gottesman10426b52013-01-07 21:26:07 +00002110
John McCalld935e9c2011-06-15 23:37:01 +00002111 cast<CallInst>(RetainRV)->setCalledFunction(getRetainCallee(F.getParent()));
Michael Gottesmandef07bb2013-01-05 17:55:42 +00002112
2113 DEBUG(dbgs() << " New: "
2114 << *RetainRV << "\n");
2115
John McCalld935e9c2011-06-15 23:37:01 +00002116 return false;
2117}
2118
Michael Gottesman97e3df02013-01-14 00:35:14 +00002119/// Turn objc_autoreleaseReturnValue into objc_autorelease if the result is not
2120/// used as a return value.
John McCalld935e9c2011-06-15 23:37:01 +00002121void
Michael Gottesman556ff612013-01-12 01:25:19 +00002122ObjCARCOpt::OptimizeAutoreleaseRVCall(Function &F, Instruction *AutoreleaseRV,
2123 InstructionClass &Class) {
John McCalld935e9c2011-06-15 23:37:01 +00002124 // Check for a return of the pointer value.
2125 const Value *Ptr = GetObjCArg(AutoreleaseRV);
Dan Gohman10a18d52011-08-12 00:36:31 +00002126 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 McCalld935e9c2011-06-15 23:37:01 +00002139
2140 Changed = true;
2141 ++NumPeeps;
Michael Gottesman1bf69082013-01-06 21:07:11 +00002142
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 Gottesmanc9656fa2013-01-12 01:25:15 +00002150 CallInst *AutoreleaseRVCI = cast<CallInst>(AutoreleaseRV);
2151 AutoreleaseRVCI->
John McCalld935e9c2011-06-15 23:37:01 +00002152 setCalledFunction(getAutoreleaseCallee(F.getParent()));
Michael Gottesmanc9656fa2013-01-12 01:25:15 +00002153 AutoreleaseRVCI->setTailCall(false); // Never tail call objc_autorelease.
Michael Gottesman556ff612013-01-12 01:25:19 +00002154 Class = IC_Autorelease;
Michael Gottesman10426b52013-01-07 21:26:07 +00002155
Michael Gottesman1bf69082013-01-06 21:07:11 +00002156 DEBUG(dbgs() << " New: "
2157 << *AutoreleaseRV << "\n");
Michael Gottesman10426b52013-01-07 21:26:07 +00002158
John McCalld935e9c2011-06-15 23:37:01 +00002159}
2160
Michael Gottesman97e3df02013-01-14 00:35:14 +00002161/// Visit each call, one at a time, and make simplifications without doing any
2162/// additional analysis.
John McCalld935e9c2011-06-15 23:37:01 +00002163void 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 Gottesman3f146e22013-01-01 16:05:48 +00002170
John McCalld935e9c2011-06-15 23:37:01 +00002171 InstructionClass Class = GetBasicInstructionClass(Inst);
2172
Michael Gottesmand359e062013-01-18 03:08:39 +00002173 DEBUG(dbgs() << "ObjCARCOpt::OptimizeIndividualCalls: Visiting: Class: "
2174 << Class << "; " << *Inst << "\n");
Michael Gottesman782e3442013-01-17 18:32:34 +00002175
John McCalld935e9c2011-06-15 23:37:01 +00002176 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 Gottesmandc042f02013-01-06 21:07:15 +00002190 DEBUG(dbgs() << "ObjCARCOpt::OptimizeIndividualCalls: Erasing no-op cast:"
2191 " " << *Inst << "\n");
John McCalld935e9c2011-06-15 23:37:01 +00002192 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 Gohman670f9372012-04-13 18:57:48 +00002203 Changed = true;
Chris Lattner229907c2011-07-18 04:54:35 +00002204 Type *Ty = CI->getArgOperand(0)->getType();
John McCalld935e9c2011-06-15 23:37:01 +00002205 new StoreInst(UndefValue::get(cast<PointerType>(Ty)->getElementType()),
2206 Constant::getNullValue(Ty),
2207 CI);
Michael Gottesman10426b52013-01-07 21:26:07 +00002208 llvm::Value *NewValue = UndefValue::get(CI->getType());
Michael Gottesmanfec61c02013-01-06 21:54:30 +00002209 DEBUG(dbgs() << "ObjCARCOpt::OptimizeIndividualCalls: A null "
2210 "pointer-to-weak-pointer is undefined behavior.\n"
2211 " Old = " << *CI <<
2212 "\n New = " <<
Michael Gottesman10426b52013-01-07 21:26:07 +00002213 *NewValue << "\n");
Michael Gottesmanfec61c02013-01-06 21:54:30 +00002214 CI->replaceAllUsesWith(NewValue);
John McCalld935e9c2011-06-15 23:37:01 +00002215 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 Gohman670f9372012-04-13 18:57:48 +00002225 Changed = true;
Chris Lattner229907c2011-07-18 04:54:35 +00002226 Type *Ty = CI->getArgOperand(0)->getType();
John McCalld935e9c2011-06-15 23:37:01 +00002227 new StoreInst(UndefValue::get(cast<PointerType>(Ty)->getElementType()),
2228 Constant::getNullValue(Ty),
2229 CI);
Michael Gottesmanfec61c02013-01-06 21:54:30 +00002230
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 Gottesman10426b52013-01-07 21:26:07 +00002237
Michael Gottesmanfec61c02013-01-06 21:54:30 +00002238 CI->replaceAllUsesWith(NewValue);
John McCalld935e9c2011-06-15 23:37:01 +00002239 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 Gottesman556ff612013-01-12 01:25:19 +00002252 OptimizeAutoreleaseRVCall(F, Inst, Class);
John McCalld935e9c2011-06-15 23:37:01 +00002253 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 Gottesman10426b52013-01-07 21:26:07 +00002272
Michael Gottesmana6a1dad2013-01-06 22:56:50 +00002273 DEBUG(dbgs() << "ObjCARCOpt::OptimizeIndividualCalls: Replacing "
2274 "objc_autorelease(x) with objc_release(x) since x is "
2275 "otherwise unused.\n"
Michael Gottesman4bf6e752013-01-06 22:56:54 +00002276 " Old: " << *Call <<
Michael Gottesmana6a1dad2013-01-06 22:56:50 +00002277 "\n New: " <<
2278 *NewCall << "\n");
Michael Gottesman10426b52013-01-07 21:26:07 +00002279
John McCalld935e9c2011-06-15 23:37:01 +00002280 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 Gottesman2d763312013-01-06 23:39:09 +00002290 DEBUG(dbgs() << "ObjCARCOpt::OptimizeIndividualCalls: Adding tail keyword"
2291 " to function since it can never be passed stack args: " << *Inst <<
2292 "\n");
John McCalld935e9c2011-06-15 23:37:01 +00002293 cast<CallInst>(Inst)->setTailCall();
2294 }
2295
Michael Gottesmanc9656fa2013-01-12 01:25:15 +00002296 // 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 Gottesman4385edf2013-01-14 01:47:53 +00002300 DEBUG(dbgs() << "ObjCARCOpt::OptimizeIndividualCalls: Removing tail "
2301 "keyword from function: " << *Inst <<
Michael Gottesmanc9656fa2013-01-12 01:25:15 +00002302 "\n");
2303 cast<CallInst>(Inst)->setTailCall(false);
2304 }
2305
John McCalld935e9c2011-06-15 23:37:01 +00002306 // Set nounwind as needed.
2307 if (IsNoThrow(Class)) {
2308 Changed = true;
Michael Gottesman8800a512013-01-06 23:39:13 +00002309 DEBUG(dbgs() << "ObjCARCOpt::OptimizeIndividualCalls: Found no throw"
2310 " class. Setting nounwind on: " << *Inst << "\n");
John McCalld935e9c2011-06-15 23:37:01 +00002311 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 Gottesman5b970e12013-01-07 00:04:52 +00002325 DEBUG(dbgs() << "ObjCARCOpt::OptimizeIndividualCalls: ARC calls with "
2326 " null are no-ops. Erasing: " << *Inst << "\n");
John McCalld935e9c2011-06-15 23:37:01 +00002327 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 Gohman8478d762012-04-13 00:59:57 +00002372 switch (Class) {
2373 case IC_Retain:
2374 case IC_RetainBlock:
2375 // These can always be moved up.
2376 break;
2377 case IC_Release:
Dan Gohman41375a32012-05-08 23:39:44 +00002378 // These can't be moved across things that care about the retain
2379 // count.
Dan Gohman8478d762012-04-13 00:59:57 +00002380 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 McCalld935e9c2011-06-15 23:37:01 +00002401 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 Lattner229907c2011-07-18 04:54:35 +00002407 Type *ParamTy = CInst->getArgOperand(0)->getType();
John McCalld935e9c2011-06-15 23:37:01 +00002408 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 Gottesmanc189a392013-01-09 19:23:24 +00002419
2420 DEBUG(dbgs() << "ObjCARCOpt::OptimizeIndividualCalls: Cloning "
2421 << *CInst << "\n"
2422 " And inserting "
2423 "clone at " << *InsertPos << "\n");
John McCalld935e9c2011-06-15 23:37:01 +00002424 Worklist.push_back(std::make_pair(Clone, Incoming));
2425 }
2426 }
2427 // Erase the original call.
Michael Gottesmanc189a392013-01-09 19:23:24 +00002428 DEBUG(dbgs() << "Erasing: " << *CInst << "\n");
John McCalld935e9c2011-06-15 23:37:01 +00002429 EraseInstruction(CInst);
2430 continue;
2431 }
2432 }
2433 } while (!Worklist.empty());
2434 }
Michael Gottesmanb24bdef2013-01-12 02:57:16 +00002435 DEBUG(dbgs() << "ObjCARCOpt::OptimizeIndividualCalls: Finished List.\n");
John McCalld935e9c2011-06-15 23:37:01 +00002436}
2437
Michael Gottesman97e3df02013-01-14 00:35:14 +00002438/// 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 McCalld935e9c2011-06-15 23:37:01 +00002441void
2442ObjCARCOpt::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 Gohman55b06742012-03-02 01:13:53 +00002447 for (BBState::ptr_iterator I = MyStates.top_down_ptr_begin(),
John McCalld935e9c2011-06-15 23:37:01 +00002448 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 Gohman55b06742012-03-02 01:13:53 +00002456 PtrState &S = I->second;
Dan Gohman0155f302012-02-17 18:59:53 +00002457 succ_const_iterator SI(TI), SE(TI, false);
2458
Dan Gohman0155f302012-02-17 18:59:53 +00002459 for (; SI != SE; ++SI) {
Dan Gohman362eb692012-03-02 01:26:46 +00002460 Sequence SuccSSeq = S_None;
2461 bool SuccSRRIKnownSafe = false;
Dan Gohman41375a32012-05-08 23:39:44 +00002462 // If VisitBottomUp has pointer information for this successor, take
2463 // what we know about it.
Dan Gohmandae33492012-04-27 18:56:31 +00002464 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 Gohman362eb692012-03-02 01:26:46 +00002470 switch (SuccSSeq) {
John McCalld935e9c2011-06-15 23:37:01 +00002471 case S_None:
Dan Gohman12130272011-08-12 00:26:31 +00002472 case S_CanRelease: {
Dan Gohman362eb692012-03-02 01:26:46 +00002473 if (!S.RRI.KnownSafe && !SuccSRRIKnownSafe) {
Dan Gohman12130272011-08-12 00:26:31 +00002474 S.ClearSequenceProgress();
Dan Gohman362eb692012-03-02 01:26:46 +00002475 break;
2476 }
Dan Gohman12130272011-08-12 00:26:31 +00002477 continue;
2478 }
John McCalld935e9c2011-06-15 23:37:01 +00002479 case S_Use:
2480 SomeSuccHasSame = true;
2481 break;
2482 case S_Stop:
2483 case S_Release:
2484 case S_MovableRelease:
Dan Gohman362eb692012-03-02 01:26:46 +00002485 if (!S.RRI.KnownSafe && !SuccSRRIKnownSafe)
Dan Gohman12130272011-08-12 00:26:31 +00002486 AllSuccsHaveSame = false;
John McCalld935e9c2011-06-15 23:37:01 +00002487 break;
2488 case S_Retain:
2489 llvm_unreachable("bottom-up pointer in retain state!");
2490 }
Dan Gohman12130272011-08-12 00:26:31 +00002491 }
John McCalld935e9c2011-06-15 23:37:01 +00002492 // 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 Gohman12130272011-08-12 00:26:31 +00002496 S.ClearSequenceProgress();
Dan Gohman044437062011-12-12 18:13:53 +00002497 break;
John McCalld935e9c2011-06-15 23:37:01 +00002498 }
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 Gohman55b06742012-03-02 01:13:53 +00002504 PtrState &S = I->second;
Dan Gohman0155f302012-02-17 18:59:53 +00002505 succ_const_iterator SI(TI), SE(TI, false);
2506
Dan Gohman0155f302012-02-17 18:59:53 +00002507 for (; SI != SE; ++SI) {
Dan Gohman362eb692012-03-02 01:26:46 +00002508 Sequence SuccSSeq = S_None;
2509 bool SuccSRRIKnownSafe = false;
Dan Gohman41375a32012-05-08 23:39:44 +00002510 // If VisitBottomUp has pointer information for this successor, take
2511 // what we know about it.
Dan Gohmandae33492012-04-27 18:56:31 +00002512 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 Gohman362eb692012-03-02 01:26:46 +00002518 switch (SuccSSeq) {
Dan Gohman12130272011-08-12 00:26:31 +00002519 case S_None: {
Dan Gohman362eb692012-03-02 01:26:46 +00002520 if (!S.RRI.KnownSafe && !SuccSRRIKnownSafe) {
Dan Gohman12130272011-08-12 00:26:31 +00002521 S.ClearSequenceProgress();
Dan Gohman362eb692012-03-02 01:26:46 +00002522 break;
2523 }
Dan Gohman12130272011-08-12 00:26:31 +00002524 continue;
2525 }
John McCalld935e9c2011-06-15 23:37:01 +00002526 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 Gohman362eb692012-03-02 01:26:46 +00002533 if (!S.RRI.KnownSafe && !SuccSRRIKnownSafe)
Dan Gohman12130272011-08-12 00:26:31 +00002534 AllSuccsHaveSame = false;
John McCalld935e9c2011-06-15 23:37:01 +00002535 break;
2536 case S_Retain:
2537 llvm_unreachable("bottom-up pointer in retain state!");
2538 }
Dan Gohman12130272011-08-12 00:26:31 +00002539 }
John McCalld935e9c2011-06-15 23:37:01 +00002540 // 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 Gohman12130272011-08-12 00:26:31 +00002544 S.ClearSequenceProgress();
Dan Gohman044437062011-12-12 18:13:53 +00002545 break;
John McCalld935e9c2011-06-15 23:37:01 +00002546 }
2547 }
2548}
2549
2550bool
Dan Gohman817a7c62012-03-22 18:24:56 +00002551ObjCARCOpt::VisitInstructionBottomUp(Instruction *Inst,
Dan Gohman5c70fad2012-03-23 17:47:54 +00002552 BasicBlock *BB,
Dan Gohman817a7c62012-03-22 18:24:56 +00002553 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 Gottesmanaf2113f2013-01-13 07:00:51 +00002572 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 Gohman817a7c62012-03-22 18:24:56 +00002575 NestingDetected = true;
Michael Gottesmanaf2113f2013-01-13 07:00:51 +00002576 }
Dan Gohman817a7c62012-03-22 18:24:56 +00002577
Dan Gohman817a7c62012-03-22 18:24:56 +00002578 MDNode *ReleaseMetadata = Inst->getMetadata(ImpreciseReleaseMDKind);
Dan Gohman62079b42012-04-25 00:50:46 +00002579 S.ResetSequenceProgress(ReleaseMetadata ? S_MovableRelease : S_Release);
Dan Gohman817a7c62012-03-22 18:24:56 +00002580 S.RRI.ReleaseMetadata = ReleaseMetadata;
Dan Gohmandf476e52012-09-04 23:16:20 +00002581 S.RRI.KnownSafe = S.IsKnownIncremented();
Dan Gohman817a7c62012-03-22 18:24:56 +00002582 S.RRI.IsTailCallRelease = cast<CallInst>(Inst)->isTailCall();
2583 S.RRI.Calls.insert(Inst);
2584
Dan Gohmandf476e52012-09-04 23:16:20 +00002585 S.SetKnownPositiveRefCount();
Dan Gohman817a7c62012-03-22 18:24:56 +00002586 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 Gohman62079b42012-04-25 00:50:46 +00002599 S.SetKnownPositiveRefCount();
Dan Gohman817a7c62012-03-22 18:24:56 +00002600
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 Gohman62079b42012-04-25 00:50:46 +00002648 S.ClearRefCount();
Dan Gohman817a7c62012-03-22 18:24:56 +00002649 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 Gohman5c70fad2012-03-23 17:47:54 +00002670 // 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 Pichet4b9ab742012-03-24 01:36:37 +00002676 S.RRI.ReverseInsertPts.insert(llvm::next(BasicBlock::iterator(Inst)));
Dan Gohman817a7c62012-03-22 18:24:56 +00002677 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 Gohman5c70fad2012-03-23 17:47:54 +00002683 // As above; handle invoke specially.
2684 if (isa<InvokeInst>(Inst))
2685 S.RRI.ReverseInsertPts.insert(BB->getFirstInsertionPt());
2686 else
Francois Pichet4b9ab742012-03-24 01:36:37 +00002687 S.RRI.ReverseInsertPts.insert(llvm::next(BasicBlock::iterator(Inst)));
Dan Gohman817a7c62012-03-22 18:24:56 +00002688 }
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
2706bool
John McCalld935e9c2011-06-15 23:37:01 +00002707ObjCARCOpt::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 Gohman10c82ce2012-08-27 18:31:36 +00002715 BBState::edge_iterator SI(MyStates.succ_begin()),
2716 SE(MyStates.succ_end());
2717 if (SI != SE) {
Dan Gohmanc24c66f2012-04-24 22:53:18 +00002718 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 Gohman0155f302012-02-17 18:59:53 +00002729 }
John McCalld935e9c2011-06-15 23:37:01 +00002730
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 Gohman5c70fad2012-03-23 17:47:54 +00002734
2735 // Invoke instructions are visited as part of their successors (below).
2736 if (isa<InvokeInst>(Inst))
2737 continue;
2738
Michael Gottesmanaf2113f2013-01-13 07:00:51 +00002739 DEBUG(dbgs() << "ObjCARCOpt::VisitButtonUp: Visiting " << *Inst << "\n");
2740
Dan Gohman5c70fad2012-03-23 17:47:54 +00002741 NestingDetected |= VisitInstructionBottomUp(Inst, BB, Retains, MyStates);
2742 }
2743
Dan Gohmandae33492012-04-27 18:56:31 +00002744 // 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 Gohmanc24c66f2012-04-24 22:53:18 +00002747 for (BBState::edge_iterator PI(MyStates.pred_begin()),
2748 PE(MyStates.pred_end()); PI != PE; ++PI) {
Dan Gohman5c70fad2012-03-23 17:47:54 +00002749 BasicBlock *Pred = *PI;
Dan Gohmandae33492012-04-27 18:56:31 +00002750 if (InvokeInst *II = dyn_cast<InvokeInst>(&Pred->back()))
2751 NestingDetected |= VisitInstructionBottomUp(II, BB, Retains, MyStates);
Dan Gohman817a7c62012-03-22 18:24:56 +00002752 }
John McCalld935e9c2011-06-15 23:37:01 +00002753
Dan Gohman817a7c62012-03-22 18:24:56 +00002754 return NestingDetected;
2755}
John McCalld935e9c2011-06-15 23:37:01 +00002756
Dan Gohman817a7c62012-03-22 18:24:56 +00002757bool
2758ObjCARCOpt::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 McCalld935e9c2011-06-15 23:37:01 +00002764
Dan Gohman817a7c62012-03-22 18:24:56 +00002765 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 McCalld935e9c2011-06-15 23:37:01 +00002782 // a note, and we'll cicle back to revisit it after we've
Dan Gohman817a7c62012-03-22 18:24:56 +00002783 // hopefully eliminated the second retain, which may allow us to
2784 // eliminate the first retain too.
John McCalld935e9c2011-06-15 23:37:01 +00002785 // 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 Gohman817a7c62012-03-22 18:24:56 +00002788 if (S.GetSeq() == S_Retain)
John McCalld935e9c2011-06-15 23:37:01 +00002789 NestingDetected = true;
2790
Dan Gohman62079b42012-04-25 00:50:46 +00002791 S.ResetSequenceProgress(S_Retain);
Dan Gohman817a7c62012-03-22 18:24:56 +00002792 S.RRI.IsRetainBlock = Class == IC_RetainBlock;
Dan Gohmandf476e52012-09-04 23:16:20 +00002793 S.RRI.KnownSafe = S.IsKnownIncremented();
John McCalld935e9c2011-06-15 23:37:01 +00002794 S.RRI.Calls.insert(Inst);
John McCalld935e9c2011-06-15 23:37:01 +00002795 }
John McCalld935e9c2011-06-15 23:37:01 +00002796
Dan Gohmandf476e52012-09-04 23:16:20 +00002797 S.SetKnownPositiveRefCount();
Dan Gohmanf64ff8e2012-07-23 19:27:31 +00002798
2799 // A retain can be a potential use; procede to the generic checking
2800 // code below.
2801 break;
Dan Gohman817a7c62012-03-22 18:24:56 +00002802 }
2803 case IC_Release: {
2804 Arg = GetObjCArg(Inst);
2805
2806 PtrState &S = MyStates.getPtrTopDownState(Arg);
Dan Gohmandf476e52012-09-04 23:16:20 +00002807 S.ClearRefCount();
Dan Gohman817a7c62012-03-22 18:24:56 +00002808
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 Gohman62079b42012-04-25 00:50:46 +00002853 S.ClearRefCount();
John McCalld935e9c2011-06-15 23:37:01 +00002854 switch (Seq) {
Dan Gohman817a7c62012-03-22 18:24:56 +00002855 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 McCalld935e9c2011-06-15 23:37:01 +00002864 case S_Use:
Dan Gohman817a7c62012-03-22 18:24:56 +00002865 case S_CanRelease:
John McCalld935e9c2011-06-15 23:37:01 +00002866 case S_None:
2867 break;
Dan Gohman817a7c62012-03-22 18:24:56 +00002868 case S_Stop:
2869 case S_Release:
2870 case S_MovableRelease:
2871 llvm_unreachable("top-down pointer in release state!");
John McCalld935e9c2011-06-15 23:37:01 +00002872 }
2873 }
Dan Gohman817a7c62012-03-22 18:24:56 +00002874
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 McCalld935e9c2011-06-15 23:37:01 +00002890 }
2891
2892 return NestingDetected;
2893}
2894
2895bool
2896ObjCARCOpt::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 Gohman10c82ce2012-08-27 18:31:36 +00002904 BBState::edge_iterator PI(MyStates.pred_begin()),
2905 PE(MyStates.pred_end());
2906 if (PI != PE) {
Dan Gohmanc24c66f2012-04-24 22:53:18 +00002907 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 Gohmanc24c66f2012-04-24 22:53:18 +00002918 }
John McCalld935e9c2011-06-15 23:37:01 +00002919
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 Gottesmanaf2113f2013-01-13 07:00:51 +00002923
2924 DEBUG(dbgs() << "ObjCARCOpt::VisitTopDown: Visiting " << *Inst << "\n");
2925
Dan Gohman817a7c62012-03-22 18:24:56 +00002926 NestingDetected |= VisitInstructionTopDown(Inst, Releases, MyStates);
John McCalld935e9c2011-06-15 23:37:01 +00002927 }
2928
2929 CheckForCFGHazards(BB, BBStates, MyStates);
2930 return NestingDetected;
2931}
2932
Dan Gohmana53a12c2011-12-12 19:42:25 +00002933static void
2934ComputePostOrders(Function &F,
2935 SmallVectorImpl<BasicBlock *> &PostOrder,
Dan Gohmanc24c66f2012-04-24 22:53:18 +00002936 SmallVectorImpl<BasicBlock *> &ReverseCFGPostOrder,
2937 unsigned NoObjCARCExceptionsMDKind,
2938 DenseMap<const BasicBlock *, BBState> &BBStates) {
Michael Gottesman97e3df02013-01-14 00:35:14 +00002939 /// The visited set, for doing DFS walks.
Dan Gohmana53a12c2011-12-12 19:42:25 +00002940 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 Gohmanc24c66f2012-04-24 22:53:18 +00002945
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 Gohmana53a12c2011-12-12 19:42:25 +00002948 BasicBlock *EntryBB = &F.getEntryBlock();
Dan Gohman41375a32012-05-08 23:39:44 +00002949 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 Gohmana53a12c2011-12-12 19:42:25 +00002953 Visited.insert(EntryBB);
2954 OnStack.insert(EntryBB);
2955 do {
2956 dfs_next_succ:
Dan Gohmanc24c66f2012-04-24 22:53:18 +00002957 BasicBlock *CurrBB = SuccStack.back().first;
2958 TerminatorInst *TI = cast<TerminatorInst>(&CurrBB->back());
2959 succ_iterator SE(TI, false);
Dan Gohman41375a32012-05-08 23:39:44 +00002960
Dan Gohmanc24c66f2012-04-24 22:53:18 +00002961 while (SuccStack.back().second != SE) {
2962 BasicBlock *SuccBB = *SuccStack.back().second++;
2963 if (Visited.insert(SuccBB)) {
Dan Gohman41375a32012-05-08 23:39:44 +00002964 TerminatorInst *TI = cast<TerminatorInst>(&SuccBB->back());
2965 SuccStack.push_back(std::make_pair(SuccBB, succ_iterator(TI)));
Dan Gohmanc24c66f2012-04-24 22:53:18 +00002966 BBStates[CurrBB].addSucc(SuccBB);
Dan Gohman41375a32012-05-08 23:39:44 +00002967 BBState &SuccStates = BBStates[SuccBB];
2968 SuccStates.addPred(CurrBB);
Dan Gohmanc24c66f2012-04-24 22:53:18 +00002969 OnStack.insert(SuccBB);
Dan Gohmana53a12c2011-12-12 19:42:25 +00002970 goto dfs_next_succ;
2971 }
Dan Gohmanc24c66f2012-04-24 22:53:18 +00002972
2973 if (!OnStack.count(SuccBB)) {
2974 BBStates[CurrBB].addSucc(SuccBB);
2975 BBStates[SuccBB].addPred(CurrBB);
2976 }
Dan Gohmana53a12c2011-12-12 19:42:25 +00002977 }
Dan Gohmanc24c66f2012-04-24 22:53:18 +00002978 OnStack.erase(CurrBB);
2979 PostOrder.push_back(CurrBB);
2980 SuccStack.pop_back();
Dan Gohmana53a12c2011-12-12 19:42:25 +00002981 } while (!SuccStack.empty());
2982
2983 Visited.clear();
2984
Dan Gohmana53a12c2011-12-12 19:42:25 +00002985 // Do reverse-CFG DFS, computing the reverse-CFG PostOrder.
Dan Gohmanc24c66f2012-04-24 22:53:18 +00002986 // 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 Gohmandae33492012-04-27 18:56:31 +00002995 MyStates.SetAsExit();
Dan Gohmanc24c66f2012-04-24 22:53:18 +00002996
2997 PredStack.push_back(std::make_pair(ExitBB, MyStates.pred_begin()));
Dan Gohmana53a12c2011-12-12 19:42:25 +00002998 Visited.insert(ExitBB);
2999 while (!PredStack.empty()) {
3000 reverse_dfs_next_succ:
Dan Gohmanc24c66f2012-04-24 22:53:18 +00003001 BBState::edge_iterator PE = BBStates[PredStack.back().first].pred_end();
3002 while (PredStack.back().second != PE) {
Dan Gohmana53a12c2011-12-12 19:42:25 +00003003 BasicBlock *BB = *PredStack.back().second++;
Dan Gohmana53a12c2011-12-12 19:42:25 +00003004 if (Visited.insert(BB)) {
Dan Gohmanc24c66f2012-04-24 22:53:18 +00003005 PredStack.push_back(std::make_pair(BB, BBStates[BB].pred_begin()));
Dan Gohmana53a12c2011-12-12 19:42:25 +00003006 goto reverse_dfs_next_succ;
3007 }
3008 }
3009 ReverseCFGPostOrder.push_back(PredStack.pop_back_val().first);
3010 }
3011 }
3012}
3013
Michael Gottesman97e3df02013-01-14 00:35:14 +00003014// Visit the function both top-down and bottom-up.
John McCalld935e9c2011-06-15 23:37:01 +00003015bool
3016ObjCARCOpt::Visit(Function &F,
3017 DenseMap<const BasicBlock *, BBState> &BBStates,
3018 MapVector<Value *, RRInfo> &Retains,
3019 DenseMap<Value *, RRInfo> &Releases) {
Dan Gohmana53a12c2011-12-12 19:42:25 +00003020
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 Gohmanc24c66f2012-04-24 22:53:18 +00003028 ComputePostOrders(F, PostOrder, ReverseCFGPostOrder,
3029 NoObjCARCExceptionsMDKind,
3030 BBStates);
Dan Gohmana53a12c2011-12-12 19:42:25 +00003031
3032 // Use reverse-postorder on the reverse CFG for bottom-up.
John McCalld935e9c2011-06-15 23:37:01 +00003033 bool BottomUpNestingDetected = false;
Dan Gohmanc57b58c2011-08-18 21:27:42 +00003034 for (SmallVectorImpl<BasicBlock *>::const_reverse_iterator I =
Dan Gohmana53a12c2011-12-12 19:42:25 +00003035 ReverseCFGPostOrder.rbegin(), E = ReverseCFGPostOrder.rend();
3036 I != E; ++I)
3037 BottomUpNestingDetected |= VisitBottomUp(*I, BBStates, Retains);
John McCalld935e9c2011-06-15 23:37:01 +00003038
Dan Gohmana53a12c2011-12-12 19:42:25 +00003039 // Use reverse-postorder for top-down.
John McCalld935e9c2011-06-15 23:37:01 +00003040 bool TopDownNestingDetected = false;
Dan Gohmana53a12c2011-12-12 19:42:25 +00003041 for (SmallVectorImpl<BasicBlock *>::const_reverse_iterator I =
3042 PostOrder.rbegin(), E = PostOrder.rend();
3043 I != E; ++I)
3044 TopDownNestingDetected |= VisitTopDown(*I, BBStates, Releases);
John McCalld935e9c2011-06-15 23:37:01 +00003045
3046 return TopDownNestingDetected && BottomUpNestingDetected;
3047}
3048
Michael Gottesman97e3df02013-01-14 00:35:14 +00003049/// Move the calls in RetainsToMove and ReleasesToMove.
John McCalld935e9c2011-06-15 23:37:01 +00003050void ObjCARCOpt::MoveCalls(Value *Arg,
3051 RRInfo &RetainsToMove,
3052 RRInfo &ReleasesToMove,
3053 MapVector<Value *, RRInfo> &Retains,
3054 DenseMap<Value *, RRInfo> &Releases,
Dan Gohman6320f522011-07-22 22:29:21 +00003055 SmallVectorImpl<Instruction *> &DeadInsts,
3056 Module *M) {
Chris Lattner229907c2011-07-18 04:54:35 +00003057 Type *ArgTy = Arg->getType();
Dan Gohman6320f522011-07-22 22:29:21 +00003058 Type *ParamTy = PointerType::getUnqual(Type::getInt8Ty(ArgTy->getContext()));
John McCalld935e9c2011-06-15 23:37:01 +00003059
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 Gohman6320f522011-07-22 22:29:21 +00003069 getRetainBlockCallee(M) : getRetainCallee(M),
John McCalld935e9c2011-06-15 23:37:01 +00003070 MyArg, "", InsertPt);
3071 Call->setDoesNotThrow();
Dan Gohman728db492012-01-13 00:39:07 +00003072 if (RetainsToMove.IsRetainBlock)
Dan Gohmana7107f92011-10-17 22:53:25 +00003073 Call->setMetadata(CopyOnEscapeMDKind,
3074 MDNode::get(M->getContext(), ArrayRef<Value *>()));
Dan Gohman728db492012-01-13 00:39:07 +00003075 else
John McCalld935e9c2011-06-15 23:37:01 +00003076 Call->setTailCall();
Michael Gottesmanc189a392013-01-09 19:23:24 +00003077
3078 DEBUG(dbgs() << "ObjCARCOpt::MoveCalls: Inserting new Release: " << *Call
3079 << "\n"
3080 " At insertion point: " << *InsertPt
3081 << "\n");
John McCalld935e9c2011-06-15 23:37:01 +00003082 }
3083 for (SmallPtrSet<Instruction *, 2>::const_iterator
3084 PI = RetainsToMove.ReverseInsertPts.begin(),
3085 PE = RetainsToMove.ReverseInsertPts.end(); PI != PE; ++PI) {
Dan Gohman5c70fad2012-03-23 17:47:54 +00003086 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 Gottesmanc189a392013-01-09 19:23:24 +00003097
3098 DEBUG(dbgs() << "ObjCARCOpt::MoveCalls: Inserting new Retain: " << *Call
3099 << "\n"
3100 " At insertion point: " << *InsertPt
3101 << "\n");
John McCalld935e9c2011-06-15 23:37:01 +00003102 }
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 Gottesmanc189a392013-01-09 19:23:24 +00003111 DEBUG(dbgs() << "ObjCARCOpt::MoveCalls: Deleting retain: " << *OrigRetain <<
3112 "\n");
John McCalld935e9c2011-06-15 23:37:01 +00003113 }
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 Gottesmanc189a392013-01-09 19:23:24 +00003120 DEBUG(dbgs() << "ObjCARCOpt::MoveCalls: Deleting release: " << *OrigRelease
3121 << "\n");
John McCalld935e9c2011-06-15 23:37:01 +00003122 }
3123}
3124
Michael Gottesman9de6f962013-01-22 21:49:00 +00003125bool
3126ObjCARCOpt::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 Gottesman9de6f962013-01-22 21:49:00 +00003290 // Set to true if we completely removed any RR pairs.
Michael Gottesman8b5515f2013-01-22 21:53:43 +00003291 AnyPairsCompletelyEliminated = NewCount == 0;
Michael Gottesman9de6f962013-01-22 21:49:00 +00003292
3293 // We can move calls!
3294 return true;
3295}
3296
Michael Gottesman97e3df02013-01-14 00:35:14 +00003297/// Identify pairings between the retains and releases, and delete and/or move
3298/// them.
John McCalld935e9c2011-06-15 23:37:01 +00003299bool
3300ObjCARCOpt::PerformCodePlacement(DenseMap<const BasicBlock *, BBState>
3301 &BBStates,
3302 MapVector<Value *, RRInfo> &Retains,
Dan Gohman6320f522011-07-22 22:29:21 +00003303 DenseMap<Value *, RRInfo> &Releases,
3304 Module *M) {
John McCalld935e9c2011-06-15 23:37:01 +00003305 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 Gohman670f9372012-04-13 18:57:48 +00003312 // Visit each retain.
John McCalld935e9c2011-06-15 23:37:01 +00003313 for (MapVector<Value *, RRInfo>::const_iterator I = Retains.begin(),
Dan Gohman2053a5d2011-09-29 22:25:23 +00003314 E = Retains.end(); I != E; ++I) {
3315 Value *V = I->first;
John McCalld935e9c2011-06-15 23:37:01 +00003316 if (!V) continue; // blotted
3317
3318 Instruction *Retain = cast<Instruction>(V);
Michael Gottesmanc189a392013-01-09 19:23:24 +00003319
3320 DEBUG(dbgs() << "ObjCARCOpt::PerformCodePlacement: Visiting: " << *Retain
3321 << "\n");
3322
John McCalld935e9c2011-06-15 23:37:01 +00003323 Value *Arg = GetObjCArg(Retain);
3324
Dan Gohman728db492012-01-13 00:39:07 +00003325 // If the object being released is in static or stack storage, we know it's
John McCalld935e9c2011-06-15 23:37:01 +00003326 // not being managed by ObjC reference counting, so we can delete pairs
3327 // regardless of what possible decrements or uses lie between them.
Dan Gohman728db492012-01-13 00:39:07 +00003328 bool KnownSafe = isa<Constant>(Arg) || isa<AllocaInst>(Arg);
Dan Gohman41375a32012-05-08 23:39:44 +00003329
Dan Gohman56e1cef2011-08-22 17:29:11 +00003330 // 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 McCalld935e9c2011-06-15 23:37:01 +00003339 // Connect the dots between the top-down-collected RetainsToMove and
3340 // bottom-up-collected ReleasesToMove to form sets of related calls.
John McCalld935e9c2011-06-15 23:37:01 +00003341 NewRetains.push_back(Retain);
Michael Gottesman9de6f962013-01-22 21:49:00 +00003342 bool PerformMoveCalls =
3343 ConnectTDBUTraversals(BBStates, Retains, Releases, M, NewRetains,
3344 NewReleases, DeadInsts, RetainsToMove,
3345 ReleasesToMove, Arg, KnownSafe,
3346 AnyPairsCompletelyEliminated);
John McCalld935e9c2011-06-15 23:37:01 +00003347
Michael Gottesman9de6f962013-01-22 21:49:00 +00003348 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 McCalld935e9c2011-06-15 23:37:01 +00003353 }
3354
Michael Gottesman9de6f962013-01-22 21:49:00 +00003355 // Clean up state for next retain.
John McCalld935e9c2011-06-15 23:37:01 +00003356 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 Gottesman97e3df02013-01-14 00:35:14 +00003370/// Weak pointer optimizations.
John McCalld935e9c2011-06-15 23:37:01 +00003371void 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 Gottesman3f146e22013-01-01 16:05:48 +00003377
Michael Gottesman9f848ae2013-01-04 21:29:57 +00003378 DEBUG(dbgs() << "ObjCARCOpt::OptimizeWeakCalls: Visiting: " << *Inst <<
Michael Gottesman3f146e22013-01-01 16:05:48 +00003379 "\n");
3380
John McCalld935e9c2011-06-15 23:37:01 +00003381 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 Gohmandae33492012-04-27 18:56:31 +00003494 const Instruction *UserInst = cast<Instruction>(*UI);
John McCalld935e9c2011-06-15 23:37:01 +00003495 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 Gohman14862c32012-05-18 22:17:29 +00003508 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 Gohman9c97eea02012-05-21 17:41:28 +00003518 llvm_unreachable("alloca really is used!");
Dan Gohman14862c32012-05-18 22:17:29 +00003519 }
John McCalld935e9c2011-06-15 23:37:01 +00003520 UserInst->eraseFromParent();
3521 }
3522 Alloca->eraseFromParent();
3523 done:;
3524 }
3525 }
Michael Gottesman10426b52013-01-07 21:26:07 +00003526
Michael Gottesman9f848ae2013-01-04 21:29:57 +00003527 DEBUG(dbgs() << "ObjCARCOpt::OptimizeWeakCalls: Finished List.\n\n");
Michael Gottesman10426b52013-01-07 21:26:07 +00003528
John McCalld935e9c2011-06-15 23:37:01 +00003529}
3530
Michael Gottesman97e3df02013-01-14 00:35:14 +00003531/// Identify program paths which execute sequences of retains and releases which
3532/// can be eliminated.
John McCalld935e9c2011-06-15 23:37:01 +00003533bool 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 Gottesman97e3df02013-01-14 00:35:14 +00003541 /// This is used during the traversal of the function to track the
John McCalld935e9c2011-06-15 23:37:01 +00003542 /// 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 Gohman6320f522011-07-22 22:29:21 +00003549 return PerformCodePlacement(BBStates, Retains, Releases, F.getParent()) &&
3550 NestingDetected;
John McCalld935e9c2011-06-15 23:37:01 +00003551}
3552
Michael Gottesman97e3df02013-01-14 00:35:14 +00003553/// Look for this pattern:
Dmitri Gribenko5485acd2012-09-14 14:57:36 +00003554/// \code
John McCalld935e9c2011-06-15 23:37:01 +00003555/// %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 Gribenko5485acd2012-09-14 14:57:36 +00003559/// \endcode
John McCalld935e9c2011-06-15 23:37:01 +00003560/// And delete the retain and autorelease.
3561///
3562/// Otherwise if it's just this:
Dmitri Gribenko5485acd2012-09-14 14:57:36 +00003563/// \code
John McCalld935e9c2011-06-15 23:37:01 +00003564/// %3 = call i8* @objc_autorelease(i8* %2)
3565/// ret i8* %3
Dmitri Gribenko5485acd2012-09-14 14:57:36 +00003566/// \endcode
John McCalld935e9c2011-06-15 23:37:01 +00003567/// convert the autorelease to autoreleaseRV.
3568void 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 Gottesman3f146e22013-01-01 16:05:48 +00003577
Michael Gottesman9f848ae2013-01-04 21:29:57 +00003578 DEBUG(dbgs() << "ObjCARCOpt::OptimizeReturns: Visiting: " << *Ret << "\n");
Michael Gottesman3f146e22013-01-01 16:05:48 +00003579
John McCalld935e9c2011-06-15 23:37:01 +00003580 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 Gohman41375a32012-05-08 23:39:44 +00003593 InstructionClass AutoreleaseClass = GetBasicInstructionClass(Autorelease);
John McCalld935e9c2011-06-15 23:37:01 +00003594 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 Gottesmana6cb0182013-01-10 02:03:50 +00003625 DEBUG(dbgs() << "ObjCARCOpt::OptimizeReturns: Converting autorelease "
3626 "=> autoreleaseRV since it's returning a value.\n"
3627 " In: " << *Autorelease
3628 << "\n");
John McCalld935e9c2011-06-15 23:37:01 +00003629 Autorelease->setCalledFunction(getAutoreleaseRVCallee(F.getParent()));
Michael Gottesmana6cb0182013-01-10 02:03:50 +00003630 DEBUG(dbgs() << " Out: " << *Autorelease
3631 << "\n");
Michael Gottesmanc9656fa2013-01-12 01:25:15 +00003632 Autorelease->setTailCall(); // Always tail call autoreleaseRV.
John McCalld935e9c2011-06-15 23:37:01 +00003633 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 Gohman4ac148d2011-09-29 22:27:34 +00003638 // Note that Retain need not be in BB.
3639 FindDependencies(CanChangeRetainCount, Arg, Retain->getParent(), Retain,
John McCalld935e9c2011-06-15 23:37:01 +00003640 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 Gottesmand61a3b22013-01-07 00:04:56 +00003660 DEBUG(dbgs() << "ObjCARCOpt::OptimizeReturns: Erasing: " << *Retain
3661 << "\n Erasing: "
3662 << *Autorelease << "\n");
John McCalld935e9c2011-06-15 23:37:01 +00003663 EraseInstruction(Retain);
3664 EraseInstruction(Autorelease);
3665 }
3666 }
3667 }
3668
3669 next_block:
3670 DependingInstructions.clear();
3671 Visited.clear();
3672 }
Michael Gottesman10426b52013-01-07 21:26:07 +00003673
Michael Gottesman9f848ae2013-01-04 21:29:57 +00003674 DEBUG(dbgs() << "ObjCARCOpt::OptimizeReturns: Finished List.\n\n");
Michael Gottesman10426b52013-01-07 21:26:07 +00003675
John McCalld935e9c2011-06-15 23:37:01 +00003676}
3677
3678bool ObjCARCOpt::doInitialization(Module &M) {
3679 if (!EnableARCOpts)
3680 return false;
3681
Dan Gohman670f9372012-04-13 18:57:48 +00003682 // If nothing in the Module uses ARC, don't do anything.
Dan Gohmanceaac7c2011-06-20 23:20:43 +00003683 Run = ModuleHasARC(M);
3684 if (!Run)
3685 return false;
3686
John McCalld935e9c2011-06-15 23:37:01 +00003687 // Identify the imprecise release metadata kind.
3688 ImpreciseReleaseMDKind =
3689 M.getContext().getMDKindID("clang.imprecise_release");
Dan Gohmana7107f92011-10-17 22:53:25 +00003690 CopyOnEscapeMDKind =
3691 M.getContext().getMDKindID("clang.arc.copy_on_escape");
Dan Gohman0155f302012-02-17 18:59:53 +00003692 NoObjCARCExceptionsMDKind =
3693 M.getContext().getMDKindID("clang.arc.no_objc_arc_exceptions");
John McCalld935e9c2011-06-15 23:37:01 +00003694
John McCalld935e9c2011-06-15 23:37:01 +00003695 // Intuitively, objc_retain and others are nocapture, however in practice
3696 // they are not, because they return their argument value. And objc_release
Dan Gohmandae33492012-04-27 18:56:31 +00003697 // calls finalizers which can have arbitrary side effects.
John McCalld935e9c2011-06-15 23:37:01 +00003698
3699 // These are initialized lazily.
3700 RetainRVCallee = 0;
3701 AutoreleaseRVCallee = 0;
3702 ReleaseCallee = 0;
3703 RetainCallee = 0;
Dan Gohman6320f522011-07-22 22:29:21 +00003704 RetainBlockCallee = 0;
John McCalld935e9c2011-06-15 23:37:01 +00003705 AutoreleaseCallee = 0;
3706
3707 return false;
3708}
3709
3710bool ObjCARCOpt::runOnFunction(Function &F) {
3711 if (!EnableARCOpts)
3712 return false;
3713
Dan Gohmanceaac7c2011-06-20 23:20:43 +00003714 // If nothing in the Module uses ARC, don't do anything.
3715 if (!Run)
3716 return false;
3717
John McCalld935e9c2011-06-15 23:37:01 +00003718 Changed = false;
3719
Michael Gottesmanb24bdef2013-01-12 02:57:16 +00003720 DEBUG(dbgs() << "ObjCARCOpt: Visiting Function: " << F.getName() << "\n");
3721
John McCalld935e9c2011-06-15 23:37:01 +00003722 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 Gohman41375a32012-05-08 23:39:44 +00003751 if (UsedInThisFunction & ((1 << IC_Autorelease) |
3752 (1 << IC_AutoreleaseRV)))
John McCalld935e9c2011-06-15 23:37:01 +00003753 OptimizeReturns(F);
3754
Michael Gottesmanb24bdef2013-01-12 02:57:16 +00003755 DEBUG(dbgs() << "\n");
3756
John McCalld935e9c2011-06-15 23:37:01 +00003757 return Changed;
3758}
3759
3760void ObjCARCOpt::releaseMemory() {
3761 PA.clear();
3762}
3763
Michael Gottesman97e3df02013-01-14 00:35:14 +00003764/// @}
3765///
3766/// \defgroup ARCContract ARC Contraction.
3767/// @{
John McCalld935e9c2011-06-15 23:37:01 +00003768
3769// TODO: ObjCARCContract could insert PHI nodes when uses aren't
3770// dominated by single calls.
3771
John McCalld935e9c2011-06-15 23:37:01 +00003772#include "llvm/Analysis/Dominators.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +00003773#include "llvm/IR/InlineAsm.h"
3774#include "llvm/IR/Operator.h"
John McCalld935e9c2011-06-15 23:37:01 +00003775
3776STATISTIC(NumStoreStrongs, "Number objc_storeStrong calls formed");
3777
3778namespace {
Michael Gottesman97e3df02013-01-14 00:35:14 +00003779 /// \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 McCalld935e9c2011-06-15 23:37:01 +00003783 class ObjCARCContract : public FunctionPass {
3784 bool Changed;
3785 AliasAnalysis *AA;
3786 DominatorTree *DT;
3787 ProvenanceAnalysis PA;
3788
Michael Gottesman97e3df02013-01-14 00:35:14 +00003789 /// A flag indicating whether this optimization pass should run.
Dan Gohmanceaac7c2011-06-20 23:20:43 +00003790 bool Run;
3791
Michael Gottesman97e3df02013-01-14 00:35:14 +00003792 /// 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 McCalld935e9c2011-06-15 23:37:01 +00003795
Michael Gottesman97e3df02013-01-14 00:35:14 +00003796 /// 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 McCalld935e9c2011-06-15 23:37:01 +00003805 const MDString *RetainRVMarker;
3806
Michael Gottesman97e3df02013-01-14 00:35:14 +00003807 /// 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 Gohman41375a32012-05-08 23:39:44 +00003810 SmallPtrSet<CallInst *, 8> StoreStrongCalls;
Dan Gohman8ee108b2012-01-19 19:14:36 +00003811
John McCalld935e9c2011-06-15 23:37:01 +00003812 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
3838char ObjCARCContract::ID = 0;
3839INITIALIZE_PASS_BEGIN(ObjCARCContract,
3840 "objc-arc-contract", "ObjC ARC contraction", false, false)
3841INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
3842INITIALIZE_PASS_DEPENDENCY(DominatorTree)
3843INITIALIZE_PASS_END(ObjCARCContract,
3844 "objc-arc-contract", "ObjC ARC contraction", false, false)
3845
3846Pass *llvm::createObjCARCContractPass() {
3847 return new ObjCARCContract();
3848}
3849
3850void ObjCARCContract::getAnalysisUsage(AnalysisUsage &AU) const {
3851 AU.addRequired<AliasAnalysis>();
3852 AU.addRequired<DominatorTree>();
3853 AU.setPreservesCFG();
3854}
3855
3856Constant *ObjCARCContract::getStoreStrongCallee(Module *M) {
3857 if (!StoreStrongCallee) {
3858 LLVMContext &C = M->getContext();
Jay Foadb804a2b2011-07-12 14:06:48 +00003859 Type *I8X = PointerType::getUnqual(Type::getInt8Ty(C));
3860 Type *I8XX = PointerType::getUnqual(I8X);
Dan Gohman41375a32012-05-08 23:39:44 +00003861 Type *Params[] = { I8XX, I8X };
John McCalld935e9c2011-06-15 23:37:01 +00003862
Bill Wendling09175b32013-01-22 21:15:51 +00003863 AttributeSet Attr = AttributeSet()
3864 .addAttribute(M->getContext(), AttributeSet::FunctionIndex,
3865 Attribute::NoUnwind)
3866 .addAttribute(M->getContext(), 1, Attribute::NoCapture);
John McCalld935e9c2011-06-15 23:37:01 +00003867
3868 StoreStrongCallee =
3869 M->getOrInsertFunction(
3870 "objc_storeStrong",
3871 FunctionType::get(Type::getVoidTy(C), Params, /*isVarArg=*/false),
Bill Wendling09175b32013-01-22 21:15:51 +00003872 Attr);
John McCalld935e9c2011-06-15 23:37:01 +00003873 }
3874 return StoreStrongCallee;
3875}
3876
3877Constant *ObjCARCContract::getRetainAutoreleaseCallee(Module *M) {
3878 if (!RetainAutoreleaseCallee) {
3879 LLVMContext &C = M->getContext();
Jay Foadb804a2b2011-07-12 14:06:48 +00003880 Type *I8X = PointerType::getUnqual(Type::getInt8Ty(C));
Dan Gohman41375a32012-05-08 23:39:44 +00003881 Type *Params[] = { I8X };
3882 FunctionType *FTy = FunctionType::get(I8X, Params, /*isVarArg=*/false);
Bill Wendling3d7b0b82012-12-19 07:18:57 +00003883 AttributeSet Attribute =
Bill Wendling09175b32013-01-22 21:15:51 +00003884 AttributeSet().addAttribute(M->getContext(), AttributeSet::FunctionIndex,
3885 Attribute::NoUnwind);
John McCalld935e9c2011-06-15 23:37:01 +00003886 RetainAutoreleaseCallee =
Bill Wendling3d7b0b82012-12-19 07:18:57 +00003887 M->getOrInsertFunction("objc_retainAutorelease", FTy, Attribute);
John McCalld935e9c2011-06-15 23:37:01 +00003888 }
3889 return RetainAutoreleaseCallee;
3890}
3891
3892Constant *ObjCARCContract::getRetainAutoreleaseRVCallee(Module *M) {
3893 if (!RetainAutoreleaseRVCallee) {
3894 LLVMContext &C = M->getContext();
Jay Foadb804a2b2011-07-12 14:06:48 +00003895 Type *I8X = PointerType::getUnqual(Type::getInt8Ty(C));
Dan Gohman41375a32012-05-08 23:39:44 +00003896 Type *Params[] = { I8X };
3897 FunctionType *FTy = FunctionType::get(I8X, Params, /*isVarArg=*/false);
Bill Wendling3d7b0b82012-12-19 07:18:57 +00003898 AttributeSet Attribute =
Bill Wendling09175b32013-01-22 21:15:51 +00003899 AttributeSet().addAttribute(M->getContext(), AttributeSet::FunctionIndex,
3900 Attribute::NoUnwind);
John McCalld935e9c2011-06-15 23:37:01 +00003901 RetainAutoreleaseRVCallee =
3902 M->getOrInsertFunction("objc_retainAutoreleaseReturnValue", FTy,
Bill Wendling3d7b0b82012-12-19 07:18:57 +00003903 Attribute);
John McCalld935e9c2011-06-15 23:37:01 +00003904 }
3905 return RetainAutoreleaseRVCallee;
3906}
3907
Michael Gottesman97e3df02013-01-14 00:35:14 +00003908/// Merge an autorelease with a retain into a fused call.
John McCalld935e9c2011-06-15 23:37:01 +00003909bool
3910ObjCARCContract::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 Gottesman10426b52013-01-07 21:26:07 +00003946
Michael Gottesmanadd08472013-01-07 00:31:26 +00003947 DEBUG(dbgs() << "ObjCARCContract::ContractAutorelease: Fusing "
3948 "retain/autorelease. Erasing: " << *Autorelease << "\n"
3949 " Old Retain: "
3950 << *Retain << "\n");
Michael Gottesman10426b52013-01-07 21:26:07 +00003951
John McCalld935e9c2011-06-15 23:37:01 +00003952 if (Class == IC_AutoreleaseRV)
3953 Retain->setCalledFunction(getRetainAutoreleaseRVCallee(F.getParent()));
3954 else
3955 Retain->setCalledFunction(getRetainAutoreleaseCallee(F.getParent()));
Michael Gottesman10426b52013-01-07 21:26:07 +00003956
Michael Gottesmanadd08472013-01-07 00:31:26 +00003957 DEBUG(dbgs() << " New Retain: "
3958 << *Retain << "\n");
Michael Gottesman10426b52013-01-07 21:26:07 +00003959
John McCalld935e9c2011-06-15 23:37:01 +00003960 EraseInstruction(Autorelease);
3961 return true;
3962}
3963
Michael Gottesman97e3df02013-01-14 00:35:14 +00003964/// 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 McCalld935e9c2011-06-15 23:37:01 +00003968void ObjCARCContract::ContractRelease(Instruction *Release,
3969 inst_iterator &Iter) {
3970 LoadInst *Load = dyn_cast<LoadInst>(GetObjCArg(Release));
Eli Friedman7c5dc122011-09-12 20:23:13 +00003971 if (!Load || !Load->isSimple()) return;
John McCalld935e9c2011-06-15 23:37:01 +00003972
3973 // For now, require everything to be in one basic block.
3974 BasicBlock *BB = Release->getParent();
3975 if (Load->getParent() != BB) return;
3976
Dan Gohman61708d32012-05-08 23:34:08 +00003977 // Walk down to find the store and the release, which may be in either order.
Dan Gohmanf8b19d02012-05-09 23:08:33 +00003978 BasicBlock::iterator I = Load, End = BB->end();
John McCalld935e9c2011-06-15 23:37:01 +00003979 ++I;
3980 AliasAnalysis::Location Loc = AA->getLocation(Load);
Dan Gohman61708d32012-05-08 23:34:08 +00003981 StoreInst *Store = 0;
3982 bool SawRelease = false;
3983 for (; !Store || !SawRelease; ++I) {
Dan Gohmanf8b19d02012-05-09 23:08:33 +00003984 if (I == End)
3985 return;
3986
Dan Gohman61708d32012-05-08 23:34:08 +00003987 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 McCalld935e9c2011-06-15 23:37:01 +00004012
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 Lattner229907c2011-07-18 04:54:35 +00004028 Type *I8X = PointerType::getUnqual(Type::getInt8Ty(C));
4029 Type *I8XX = PointerType::getUnqual(I8X);
John McCalld935e9c2011-06-15 23:37:01 +00004030
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 Foad5bd375a2011-07-15 08:37:34 +00004038 Args, "", Store);
John McCalld935e9c2011-06-15 23:37:01 +00004039 StoreStrong->setDoesNotThrow();
4040 StoreStrong->setDebugLoc(Store->getDebugLoc());
4041
Dan Gohman8ee108b2012-01-19 19:14:36 +00004042 // 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 McCalld935e9c2011-06-15 23:37:01 +00004047 if (&*Iter == Store) ++Iter;
4048 Store->eraseFromParent();
4049 Release->eraseFromParent();
4050 EraseInstruction(Retain);
4051 if (Load->use_empty())
4052 Load->eraseFromParent();
4053}
4054
4055bool ObjCARCContract::doInitialization(Module &M) {
Dan Gohman670f9372012-04-13 18:57:48 +00004056 // If nothing in the Module uses ARC, don't do anything.
Dan Gohmanceaac7c2011-06-20 23:20:43 +00004057 Run = ModuleHasARC(M);
4058 if (!Run)
4059 return false;
4060
John McCalld935e9c2011-06-15 23:37:01 +00004061 // 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
4080bool ObjCARCContract::runOnFunction(Function &F) {
4081 if (!EnableARCOpts)
4082 return false;
4083
Dan Gohmanceaac7c2011-06-20 23:20:43 +00004084 // If nothing in the Module uses ARC, don't do anything.
4085 if (!Run)
4086 return false;
4087
John McCalld935e9c2011-06-15 23:37:01 +00004088 Changed = false;
4089 AA = &getAnalysis<AliasAnalysis>();
4090 DT = &getAnalysis<DominatorTree>();
4091
4092 PA.setAA(&getAnalysis<AliasAnalysis>());
4093
Dan Gohman8ee108b2012-01-19 19:14:36 +00004094 // 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 Gohman41375a32012-05-08 23:39:44 +00004099 bool TailOkForStoreStrongs = !F.isVarArg() &&
4100 !F.callsFunctionThatReturnsTwice();
Dan Gohman8ee108b2012-01-19 19:14:36 +00004101
John McCalld935e9c2011-06-15 23:37:01 +00004102 // 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 Gottesman10426b52013-01-07 21:26:07 +00004109
Michael Gottesman3f146e22013-01-01 16:05:48 +00004110 DEBUG(dbgs() << "ObjCARCContract: Visiting: " << *Inst << "\n");
Michael Gottesman10426b52013-01-07 21:26:07 +00004111
John McCalld935e9c2011-06-15 23:37:01 +00004112 // 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 Gohman5f725cd2012-06-25 19:47:37 +00004132 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 McCalld935e9c2011-06-15 23:37:01 +00004148 if (&*BBI == GetObjCArg(Inst)) {
Michael Gottesman00d1f962013-01-03 07:32:41 +00004149 DEBUG(dbgs() << "ObjCARCContract: Adding inline asm marker for "
Michael Gottesman9f848ae2013-01-04 21:29:57 +00004150 "retainAutoreleasedReturnValue optimization.\n");
Dan Gohman670f9372012-04-13 18:57:48 +00004151 Changed = true;
John McCalld935e9c2011-06-15 23:37:01 +00004152 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 Gohman5f725cd2012-06-25 19:47:37 +00004159 decline_rv_optimization:
John McCalld935e9c2011-06-15 23:37:01 +00004160 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 Gottesman10426b52013-01-07 21:26:07 +00004170
Michael Gottesman416dc002013-01-03 07:32:53 +00004171 DEBUG(dbgs() << "OBJCARCContract: Old = " << *CI << "\n"
4172 << " New = " << *Null << "\n");
Michael Gottesman10426b52013-01-07 21:26:07 +00004173
John McCalld935e9c2011-06-15 23:37:01 +00004174 CI->replaceAllUsesWith(Null);
4175 CI->eraseFromParent();
4176 }
4177 continue;
4178 }
4179 case IC_Release:
4180 ContractRelease(Inst, I);
4181 continue;
Dan Gohman8ee108b2012-01-19 19:14:36 +00004182 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 McCalld935e9c2011-06-15 23:37:01 +00004189 default:
4190 continue;
4191 }
4192
Michael Gottesman50ae5b22013-01-03 08:09:27 +00004193 DEBUG(dbgs() << "ObjCARCContract: Finished List.\n\n");
Michael Gottesman3f146e22013-01-01 16:05:48 +00004194
John McCalld935e9c2011-06-15 23:37:01 +00004195 // 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 Gohman670f9372012-04-13 18:57:48 +00004208
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 Gohman41375a32012-05-08 23:39:44 +00004215 if (DT->isReachableFromEntry(U) && DT->dominates(Inst, U)) {
Rafael Espindolaf5892782012-03-15 15:52:59 +00004216 Changed = true;
4217 Instruction *Replacement = Inst;
4218 Type *UseTy = U.get()->getType();
Dan Gohmande8d2c42012-04-13 01:08:28 +00004219 if (PHINode *PHI = dyn_cast<PHINode>(U.getUser())) {
Rafael Espindolaf5892782012-03-15 15:52:59 +00004220 // For PHI nodes, insert the bitcast in the predecessor block.
Dan Gohman41375a32012-05-08 23:39:44 +00004221 unsigned ValNo = PHINode::getIncomingValueNumForOperand(OperandNo);
4222 BasicBlock *BB = PHI->getIncomingBlock(ValNo);
Rafael Espindolaf5892782012-03-15 15:52:59 +00004223 if (Replacement->getType() != UseTy)
4224 Replacement = new BitCastInst(Replacement, UseTy, "",
4225 &BB->back());
Dan Gohman670f9372012-04-13 18:57:48 +00004226 // 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 Gohmandae33492012-04-27 18:56:31 +00004229 for (unsigned i = 0, e = PHI->getNumIncomingValues(); i != e; ++i)
Rafael Espindolaf5892782012-03-15 15:52:59 +00004230 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 Gohmande8d2c42012-04-13 01:08:28 +00004240 Replacement = new BitCastInst(Replacement, UseTy, "",
4241 cast<Instruction>(U.getUser()));
Rafael Espindolaf5892782012-03-15 15:52:59 +00004242 U.set(Replacement);
John McCalld935e9c2011-06-15 23:37:01 +00004243 }
Rafael Espindolaf5892782012-03-15 15:52:59 +00004244 }
John McCalld935e9c2011-06-15 23:37:01 +00004245 }
4246
Dan Gohmandae33492012-04-27 18:56:31 +00004247 // If Arg is a no-op casted pointer, strip one level of casts and iterate.
John McCalld935e9c2011-06-15 23:37:01 +00004248 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 Gohman8ee108b2012-01-19 19:14:36 +00004261 // 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 Gohman41375a32012-05-08 23:39:44 +00004264 for (SmallPtrSet<CallInst *, 8>::iterator I = StoreStrongCalls.begin(),
Dan Gohman8ee108b2012-01-19 19:14:36 +00004265 E = StoreStrongCalls.end(); I != E; ++I)
4266 (*I)->setTailCall();
4267 StoreStrongCalls.clear();
4268
John McCalld935e9c2011-06-15 23:37:01 +00004269 return Changed;
4270}
Michael Gottesman97e3df02013-01-14 00:35:14 +00004271
4272/// @}
4273///