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