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