An objc_retain can serve as a may-use for a different pointer.
[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 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 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
2833     // A retain can be a potential use; procede to the generic checking
2834     // code below.
2835     break;
2836   }
2837   case IC_Release: {
2838     Arg = GetObjCArg(Inst);
2839
2840     PtrState &S = MyStates.getPtrTopDownState(Arg);
2841     S.DecrementNestCount();
2842
2843     switch (S.GetSeq()) {
2844     case S_Retain:
2845     case S_CanRelease:
2846       S.RRI.ReverseInsertPts.clear();
2847       // FALL THROUGH
2848     case S_Use:
2849       S.RRI.ReleaseMetadata = Inst->getMetadata(ImpreciseReleaseMDKind);
2850       S.RRI.IsTailCallRelease = cast<CallInst>(Inst)->isTailCall();
2851       Releases[Inst] = S.RRI;
2852       S.ClearSequenceProgress();
2853       break;
2854     case S_None:
2855       break;
2856     case S_Stop:
2857     case S_Release:
2858     case S_MovableRelease:
2859       llvm_unreachable("top-down pointer in release state!");
2860     }
2861     break;
2862   }
2863   case IC_AutoreleasepoolPop:
2864     // Conservatively, clear MyStates for all known pointers.
2865     MyStates.clearTopDownPointers();
2866     return NestingDetected;
2867   case IC_AutoreleasepoolPush:
2868   case IC_None:
2869     // These are irrelevant.
2870     return NestingDetected;
2871   default:
2872     break;
2873   }
2874
2875   // Consider any other possible effects of this instruction on each
2876   // pointer being tracked.
2877   for (BBState::ptr_iterator MI = MyStates.top_down_ptr_begin(),
2878        ME = MyStates.top_down_ptr_end(); MI != ME; ++MI) {
2879     const Value *Ptr = MI->first;
2880     if (Ptr == Arg)
2881       continue; // Handled above.
2882     PtrState &S = MI->second;
2883     Sequence Seq = S.GetSeq();
2884
2885     // Check for possible releases.
2886     if (CanAlterRefCount(Inst, Ptr, PA, Class)) {
2887       S.ClearRefCount();
2888       switch (Seq) {
2889       case S_Retain:
2890         S.SetSeq(S_CanRelease);
2891         assert(S.RRI.ReverseInsertPts.empty());
2892         S.RRI.ReverseInsertPts.insert(Inst);
2893
2894         // One call can't cause a transition from S_Retain to S_CanRelease
2895         // and S_CanRelease to S_Use. If we've made the first transition,
2896         // we're done.
2897         continue;
2898       case S_Use:
2899       case S_CanRelease:
2900       case S_None:
2901         break;
2902       case S_Stop:
2903       case S_Release:
2904       case S_MovableRelease:
2905         llvm_unreachable("top-down pointer in release state!");
2906       }
2907     }
2908
2909     // Check for possible direct uses.
2910     switch (Seq) {
2911     case S_CanRelease:
2912       if (CanUse(Inst, Ptr, PA, Class))
2913         S.SetSeq(S_Use);
2914       break;
2915     case S_Retain:
2916     case S_Use:
2917     case S_None:
2918       break;
2919     case S_Stop:
2920     case S_Release:
2921     case S_MovableRelease:
2922       llvm_unreachable("top-down pointer in release state!");
2923     }
2924   }
2925
2926   return NestingDetected;
2927 }
2928
2929 bool
2930 ObjCARCOpt::VisitTopDown(BasicBlock *BB,
2931                          DenseMap<const BasicBlock *, BBState> &BBStates,
2932                          DenseMap<Value *, RRInfo> &Releases) {
2933   bool NestingDetected = false;
2934   BBState &MyStates = BBStates[BB];
2935
2936   // Merge the states from each predecessor to compute the initial state
2937   // for the current block.
2938   for (BBState::edge_iterator PI(MyStates.pred_begin()),
2939        PE(MyStates.pred_end()); PI != PE; ++PI) {
2940     const BasicBlock *Pred = *PI;
2941     DenseMap<const BasicBlock *, BBState>::iterator I = BBStates.find(Pred);
2942     assert(I != BBStates.end());
2943     MyStates.InitFromPred(I->second);
2944     ++PI;
2945     for (; PI != PE; ++PI) {
2946       Pred = *PI;
2947       I = BBStates.find(Pred);
2948       assert(I != BBStates.end());
2949       MyStates.MergePred(I->second);
2950     }
2951     break;
2952   }
2953
2954   // Visit all the instructions, top-down.
2955   for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
2956     Instruction *Inst = I;
2957     NestingDetected |= VisitInstructionTopDown(Inst, Releases, MyStates);
2958   }
2959
2960   CheckForCFGHazards(BB, BBStates, MyStates);
2961   return NestingDetected;
2962 }
2963
2964 static void
2965 ComputePostOrders(Function &F,
2966                   SmallVectorImpl<BasicBlock *> &PostOrder,
2967                   SmallVectorImpl<BasicBlock *> &ReverseCFGPostOrder,
2968                   unsigned NoObjCARCExceptionsMDKind,
2969                   DenseMap<const BasicBlock *, BBState> &BBStates) {
2970   /// Visited - The visited set, for doing DFS walks.
2971   SmallPtrSet<BasicBlock *, 16> Visited;
2972
2973   // Do DFS, computing the PostOrder.
2974   SmallPtrSet<BasicBlock *, 16> OnStack;
2975   SmallVector<std::pair<BasicBlock *, succ_iterator>, 16> SuccStack;
2976
2977   // Functions always have exactly one entry block, and we don't have
2978   // any other block that we treat like an entry block.
2979   BasicBlock *EntryBB = &F.getEntryBlock();
2980   BBState &MyStates = BBStates[EntryBB];
2981   MyStates.SetAsEntry();
2982   TerminatorInst *EntryTI = cast<TerminatorInst>(&EntryBB->back());
2983   SuccStack.push_back(std::make_pair(EntryBB, succ_iterator(EntryTI)));
2984   Visited.insert(EntryBB);
2985   OnStack.insert(EntryBB);
2986   do {
2987   dfs_next_succ:
2988     BasicBlock *CurrBB = SuccStack.back().first;
2989     TerminatorInst *TI = cast<TerminatorInst>(&CurrBB->back());
2990     succ_iterator SE(TI, false);
2991
2992     // If the terminator is an invoke marked with the
2993     // clang.arc.no_objc_arc_exceptions metadata, the unwind edge can be
2994     // ignored, for ARC purposes.
2995     if (isa<InvokeInst>(TI) && TI->getMetadata(NoObjCARCExceptionsMDKind))
2996       --SE;
2997
2998     while (SuccStack.back().second != SE) {
2999       BasicBlock *SuccBB = *SuccStack.back().second++;
3000       if (Visited.insert(SuccBB)) {
3001         TerminatorInst *TI = cast<TerminatorInst>(&SuccBB->back());
3002         SuccStack.push_back(std::make_pair(SuccBB, succ_iterator(TI)));
3003         BBStates[CurrBB].addSucc(SuccBB);
3004         BBState &SuccStates = BBStates[SuccBB];
3005         SuccStates.addPred(CurrBB);
3006         OnStack.insert(SuccBB);
3007         goto dfs_next_succ;
3008       }
3009
3010       if (!OnStack.count(SuccBB)) {
3011         BBStates[CurrBB].addSucc(SuccBB);
3012         BBStates[SuccBB].addPred(CurrBB);
3013       }
3014     }
3015     OnStack.erase(CurrBB);
3016     PostOrder.push_back(CurrBB);
3017     SuccStack.pop_back();
3018   } while (!SuccStack.empty());
3019
3020   Visited.clear();
3021
3022   // Do reverse-CFG DFS, computing the reverse-CFG PostOrder.
3023   // Functions may have many exits, and there also blocks which we treat
3024   // as exits due to ignored edges.
3025   SmallVector<std::pair<BasicBlock *, BBState::edge_iterator>, 16> PredStack;
3026   for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) {
3027     BasicBlock *ExitBB = I;
3028     BBState &MyStates = BBStates[ExitBB];
3029     if (!MyStates.isExit())
3030       continue;
3031
3032     MyStates.SetAsExit();
3033
3034     PredStack.push_back(std::make_pair(ExitBB, MyStates.pred_begin()));
3035     Visited.insert(ExitBB);
3036     while (!PredStack.empty()) {
3037     reverse_dfs_next_succ:
3038       BBState::edge_iterator PE = BBStates[PredStack.back().first].pred_end();
3039       while (PredStack.back().second != PE) {
3040         BasicBlock *BB = *PredStack.back().second++;
3041         if (Visited.insert(BB)) {
3042           PredStack.push_back(std::make_pair(BB, BBStates[BB].pred_begin()));
3043           goto reverse_dfs_next_succ;
3044         }
3045       }
3046       ReverseCFGPostOrder.push_back(PredStack.pop_back_val().first);
3047     }
3048   }
3049 }
3050
3051 // Visit - Visit the function both top-down and bottom-up.
3052 bool
3053 ObjCARCOpt::Visit(Function &F,
3054                   DenseMap<const BasicBlock *, BBState> &BBStates,
3055                   MapVector<Value *, RRInfo> &Retains,
3056                   DenseMap<Value *, RRInfo> &Releases) {
3057
3058   // Use reverse-postorder traversals, because we magically know that loops
3059   // will be well behaved, i.e. they won't repeatedly call retain on a single
3060   // pointer without doing a release. We can't use the ReversePostOrderTraversal
3061   // class here because we want the reverse-CFG postorder to consider each
3062   // function exit point, and we want to ignore selected cycle edges.
3063   SmallVector<BasicBlock *, 16> PostOrder;
3064   SmallVector<BasicBlock *, 16> ReverseCFGPostOrder;
3065   ComputePostOrders(F, PostOrder, ReverseCFGPostOrder,
3066                     NoObjCARCExceptionsMDKind,
3067                     BBStates);
3068
3069   // Use reverse-postorder on the reverse CFG for bottom-up.
3070   bool BottomUpNestingDetected = false;
3071   for (SmallVectorImpl<BasicBlock *>::const_reverse_iterator I =
3072        ReverseCFGPostOrder.rbegin(), E = ReverseCFGPostOrder.rend();
3073        I != E; ++I)
3074     BottomUpNestingDetected |= VisitBottomUp(*I, BBStates, Retains);
3075
3076   // Use reverse-postorder for top-down.
3077   bool TopDownNestingDetected = false;
3078   for (SmallVectorImpl<BasicBlock *>::const_reverse_iterator I =
3079        PostOrder.rbegin(), E = PostOrder.rend();
3080        I != E; ++I)
3081     TopDownNestingDetected |= VisitTopDown(*I, BBStates, Releases);
3082
3083   return TopDownNestingDetected && BottomUpNestingDetected;
3084 }
3085
3086 /// MoveCalls - Move the calls in RetainsToMove and ReleasesToMove.
3087 void ObjCARCOpt::MoveCalls(Value *Arg,
3088                            RRInfo &RetainsToMove,
3089                            RRInfo &ReleasesToMove,
3090                            MapVector<Value *, RRInfo> &Retains,
3091                            DenseMap<Value *, RRInfo> &Releases,
3092                            SmallVectorImpl<Instruction *> &DeadInsts,
3093                            Module *M) {
3094   Type *ArgTy = Arg->getType();
3095   Type *ParamTy = PointerType::getUnqual(Type::getInt8Ty(ArgTy->getContext()));
3096
3097   // Insert the new retain and release calls.
3098   for (SmallPtrSet<Instruction *, 2>::const_iterator
3099        PI = ReleasesToMove.ReverseInsertPts.begin(),
3100        PE = ReleasesToMove.ReverseInsertPts.end(); PI != PE; ++PI) {
3101     Instruction *InsertPt = *PI;
3102     Value *MyArg = ArgTy == ParamTy ? Arg :
3103                    new BitCastInst(Arg, ParamTy, "", InsertPt);
3104     CallInst *Call =
3105       CallInst::Create(RetainsToMove.IsRetainBlock ?
3106                          getRetainBlockCallee(M) : getRetainCallee(M),
3107                        MyArg, "", InsertPt);
3108     Call->setDoesNotThrow();
3109     if (RetainsToMove.IsRetainBlock)
3110       Call->setMetadata(CopyOnEscapeMDKind,
3111                         MDNode::get(M->getContext(), ArrayRef<Value *>()));
3112     else
3113       Call->setTailCall();
3114   }
3115   for (SmallPtrSet<Instruction *, 2>::const_iterator
3116        PI = RetainsToMove.ReverseInsertPts.begin(),
3117        PE = RetainsToMove.ReverseInsertPts.end(); PI != PE; ++PI) {
3118     Instruction *InsertPt = *PI;
3119     Value *MyArg = ArgTy == ParamTy ? Arg :
3120                    new BitCastInst(Arg, ParamTy, "", InsertPt);
3121     CallInst *Call = CallInst::Create(getReleaseCallee(M), MyArg,
3122                                       "", InsertPt);
3123     // Attach a clang.imprecise_release metadata tag, if appropriate.
3124     if (MDNode *M = ReleasesToMove.ReleaseMetadata)
3125       Call->setMetadata(ImpreciseReleaseMDKind, M);
3126     Call->setDoesNotThrow();
3127     if (ReleasesToMove.IsTailCallRelease)
3128       Call->setTailCall();
3129   }
3130
3131   // Delete the original retain and release calls.
3132   for (SmallPtrSet<Instruction *, 2>::const_iterator
3133        AI = RetainsToMove.Calls.begin(),
3134        AE = RetainsToMove.Calls.end(); AI != AE; ++AI) {
3135     Instruction *OrigRetain = *AI;
3136     Retains.blot(OrigRetain);
3137     DeadInsts.push_back(OrigRetain);
3138   }
3139   for (SmallPtrSet<Instruction *, 2>::const_iterator
3140        AI = ReleasesToMove.Calls.begin(),
3141        AE = ReleasesToMove.Calls.end(); AI != AE; ++AI) {
3142     Instruction *OrigRelease = *AI;
3143     Releases.erase(OrigRelease);
3144     DeadInsts.push_back(OrigRelease);
3145   }
3146 }
3147
3148 /// PerformCodePlacement - Identify pairings between the retains and releases,
3149 /// and delete and/or move them.
3150 bool
3151 ObjCARCOpt::PerformCodePlacement(DenseMap<const BasicBlock *, BBState>
3152                                    &BBStates,
3153                                  MapVector<Value *, RRInfo> &Retains,
3154                                  DenseMap<Value *, RRInfo> &Releases,
3155                                  Module *M) {
3156   bool AnyPairsCompletelyEliminated = false;
3157   RRInfo RetainsToMove;
3158   RRInfo ReleasesToMove;
3159   SmallVector<Instruction *, 4> NewRetains;
3160   SmallVector<Instruction *, 4> NewReleases;
3161   SmallVector<Instruction *, 8> DeadInsts;
3162
3163   // Visit each retain.
3164   for (MapVector<Value *, RRInfo>::const_iterator I = Retains.begin(),
3165        E = Retains.end(); I != E; ++I) {
3166     Value *V = I->first;
3167     if (!V) continue; // blotted
3168
3169     Instruction *Retain = cast<Instruction>(V);
3170     Value *Arg = GetObjCArg(Retain);
3171
3172     // If the object being released is in static or stack storage, we know it's
3173     // not being managed by ObjC reference counting, so we can delete pairs
3174     // regardless of what possible decrements or uses lie between them.
3175     bool KnownSafe = isa<Constant>(Arg) || isa<AllocaInst>(Arg);
3176
3177     // A constant pointer can't be pointing to an object on the heap. It may
3178     // be reference-counted, but it won't be deleted.
3179     if (const LoadInst *LI = dyn_cast<LoadInst>(Arg))
3180       if (const GlobalVariable *GV =
3181             dyn_cast<GlobalVariable>(
3182               StripPointerCastsAndObjCCalls(LI->getPointerOperand())))
3183         if (GV->isConstant())
3184           KnownSafe = true;
3185
3186     // If a pair happens in a region where it is known that the reference count
3187     // is already incremented, we can similarly ignore possible decrements.
3188     bool KnownSafeTD = true, KnownSafeBU = true;
3189
3190     // Connect the dots between the top-down-collected RetainsToMove and
3191     // bottom-up-collected ReleasesToMove to form sets of related calls.
3192     // This is an iterative process so that we connect multiple releases
3193     // to multiple retains if needed.
3194     unsigned OldDelta = 0;
3195     unsigned NewDelta = 0;
3196     unsigned OldCount = 0;
3197     unsigned NewCount = 0;
3198     bool FirstRelease = true;
3199     bool FirstRetain = true;
3200     NewRetains.push_back(Retain);
3201     for (;;) {
3202       for (SmallVectorImpl<Instruction *>::const_iterator
3203            NI = NewRetains.begin(), NE = NewRetains.end(); NI != NE; ++NI) {
3204         Instruction *NewRetain = *NI;
3205         MapVector<Value *, RRInfo>::const_iterator It = Retains.find(NewRetain);
3206         assert(It != Retains.end());
3207         const RRInfo &NewRetainRRI = It->second;
3208         KnownSafeTD &= NewRetainRRI.KnownSafe;
3209         for (SmallPtrSet<Instruction *, 2>::const_iterator
3210              LI = NewRetainRRI.Calls.begin(),
3211              LE = NewRetainRRI.Calls.end(); LI != LE; ++LI) {
3212           Instruction *NewRetainRelease = *LI;
3213           DenseMap<Value *, RRInfo>::const_iterator Jt =
3214             Releases.find(NewRetainRelease);
3215           if (Jt == Releases.end())
3216             goto next_retain;
3217           const RRInfo &NewRetainReleaseRRI = Jt->second;
3218           assert(NewRetainReleaseRRI.Calls.count(NewRetain));
3219           if (ReleasesToMove.Calls.insert(NewRetainRelease)) {
3220             OldDelta -=
3221               BBStates[NewRetainRelease->getParent()].GetAllPathCount();
3222
3223             // Merge the ReleaseMetadata and IsTailCallRelease values.
3224             if (FirstRelease) {
3225               ReleasesToMove.ReleaseMetadata =
3226                 NewRetainReleaseRRI.ReleaseMetadata;
3227               ReleasesToMove.IsTailCallRelease =
3228                 NewRetainReleaseRRI.IsTailCallRelease;
3229               FirstRelease = false;
3230             } else {
3231               if (ReleasesToMove.ReleaseMetadata !=
3232                     NewRetainReleaseRRI.ReleaseMetadata)
3233                 ReleasesToMove.ReleaseMetadata = 0;
3234               if (ReleasesToMove.IsTailCallRelease !=
3235                     NewRetainReleaseRRI.IsTailCallRelease)
3236                 ReleasesToMove.IsTailCallRelease = false;
3237             }
3238
3239             // Collect the optimal insertion points.
3240             if (!KnownSafe)
3241               for (SmallPtrSet<Instruction *, 2>::const_iterator
3242                    RI = NewRetainReleaseRRI.ReverseInsertPts.begin(),
3243                    RE = NewRetainReleaseRRI.ReverseInsertPts.end();
3244                    RI != RE; ++RI) {
3245                 Instruction *RIP = *RI;
3246                 if (ReleasesToMove.ReverseInsertPts.insert(RIP))
3247                   NewDelta -= BBStates[RIP->getParent()].GetAllPathCount();
3248               }
3249             NewReleases.push_back(NewRetainRelease);
3250           }
3251         }
3252       }
3253       NewRetains.clear();
3254       if (NewReleases.empty()) break;
3255
3256       // Back the other way.
3257       for (SmallVectorImpl<Instruction *>::const_iterator
3258            NI = NewReleases.begin(), NE = NewReleases.end(); NI != NE; ++NI) {
3259         Instruction *NewRelease = *NI;
3260         DenseMap<Value *, RRInfo>::const_iterator It =
3261           Releases.find(NewRelease);
3262         assert(It != Releases.end());
3263         const RRInfo &NewReleaseRRI = It->second;
3264         KnownSafeBU &= NewReleaseRRI.KnownSafe;
3265         for (SmallPtrSet<Instruction *, 2>::const_iterator
3266              LI = NewReleaseRRI.Calls.begin(),
3267              LE = NewReleaseRRI.Calls.end(); LI != LE; ++LI) {
3268           Instruction *NewReleaseRetain = *LI;
3269           MapVector<Value *, RRInfo>::const_iterator Jt =
3270             Retains.find(NewReleaseRetain);
3271           if (Jt == Retains.end())
3272             goto next_retain;
3273           const RRInfo &NewReleaseRetainRRI = Jt->second;
3274           assert(NewReleaseRetainRRI.Calls.count(NewRelease));
3275           if (RetainsToMove.Calls.insert(NewReleaseRetain)) {
3276             unsigned PathCount =
3277               BBStates[NewReleaseRetain->getParent()].GetAllPathCount();
3278             OldDelta += PathCount;
3279             OldCount += PathCount;
3280
3281             // Merge the IsRetainBlock values.
3282             if (FirstRetain) {
3283               RetainsToMove.IsRetainBlock = NewReleaseRetainRRI.IsRetainBlock;
3284               FirstRetain = false;
3285             } else if (ReleasesToMove.IsRetainBlock !=
3286                        NewReleaseRetainRRI.IsRetainBlock)
3287               // It's not possible to merge the sequences if one uses
3288               // objc_retain and the other uses objc_retainBlock.
3289               goto next_retain;
3290
3291             // Collect the optimal insertion points.
3292             if (!KnownSafe)
3293               for (SmallPtrSet<Instruction *, 2>::const_iterator
3294                    RI = NewReleaseRetainRRI.ReverseInsertPts.begin(),
3295                    RE = NewReleaseRetainRRI.ReverseInsertPts.end();
3296                    RI != RE; ++RI) {
3297                 Instruction *RIP = *RI;
3298                 if (RetainsToMove.ReverseInsertPts.insert(RIP)) {
3299                   PathCount = BBStates[RIP->getParent()].GetAllPathCount();
3300                   NewDelta += PathCount;
3301                   NewCount += PathCount;
3302                 }
3303               }
3304             NewRetains.push_back(NewReleaseRetain);
3305           }
3306         }
3307       }
3308       NewReleases.clear();
3309       if (NewRetains.empty()) break;
3310     }
3311
3312     // If the pointer is known incremented or nested, we can safely delete the
3313     // pair regardless of what's between them.
3314     if (KnownSafeTD || KnownSafeBU) {
3315       RetainsToMove.ReverseInsertPts.clear();
3316       ReleasesToMove.ReverseInsertPts.clear();
3317       NewCount = 0;
3318     } else {
3319       // Determine whether the new insertion points we computed preserve the
3320       // balance of retain and release calls through the program.
3321       // TODO: If the fully aggressive solution isn't valid, try to find a
3322       // less aggressive solution which is.
3323       if (NewDelta != 0)
3324         goto next_retain;
3325     }
3326
3327     // Determine whether the original call points are balanced in the retain and
3328     // release calls through the program. If not, conservatively don't touch
3329     // them.
3330     // TODO: It's theoretically possible to do code motion in this case, as
3331     // long as the existing imbalances are maintained.
3332     if (OldDelta != 0)
3333       goto next_retain;
3334
3335     // Ok, everything checks out and we're all set. Let's move some code!
3336     Changed = true;
3337     assert(OldCount != 0 && "Unreachable code?");
3338     AnyPairsCompletelyEliminated = NewCount == 0;
3339     NumRRs += OldCount - NewCount;
3340     MoveCalls(Arg, RetainsToMove, ReleasesToMove,
3341               Retains, Releases, DeadInsts, M);
3342
3343   next_retain:
3344     NewReleases.clear();
3345     NewRetains.clear();
3346     RetainsToMove.clear();
3347     ReleasesToMove.clear();
3348   }
3349
3350   // Now that we're done moving everything, we can delete the newly dead
3351   // instructions, as we no longer need them as insert points.
3352   while (!DeadInsts.empty())
3353     EraseInstruction(DeadInsts.pop_back_val());
3354
3355   return AnyPairsCompletelyEliminated;
3356 }
3357
3358 /// OptimizeWeakCalls - Weak pointer optimizations.
3359 void ObjCARCOpt::OptimizeWeakCalls(Function &F) {
3360   // First, do memdep-style RLE and S2L optimizations. We can't use memdep
3361   // itself because it uses AliasAnalysis and we need to do provenance
3362   // queries instead.
3363   for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ) {
3364     Instruction *Inst = &*I++;
3365     InstructionClass Class = GetBasicInstructionClass(Inst);
3366     if (Class != IC_LoadWeak && Class != IC_LoadWeakRetained)
3367       continue;
3368
3369     // Delete objc_loadWeak calls with no users.
3370     if (Class == IC_LoadWeak && Inst->use_empty()) {
3371       Inst->eraseFromParent();
3372       continue;
3373     }
3374
3375     // TODO: For now, just look for an earlier available version of this value
3376     // within the same block. Theoretically, we could do memdep-style non-local
3377     // analysis too, but that would want caching. A better approach would be to
3378     // use the technique that EarlyCSE uses.
3379     inst_iterator Current = llvm::prior(I);
3380     BasicBlock *CurrentBB = Current.getBasicBlockIterator();
3381     for (BasicBlock::iterator B = CurrentBB->begin(),
3382                               J = Current.getInstructionIterator();
3383          J != B; --J) {
3384       Instruction *EarlierInst = &*llvm::prior(J);
3385       InstructionClass EarlierClass = GetInstructionClass(EarlierInst);
3386       switch (EarlierClass) {
3387       case IC_LoadWeak:
3388       case IC_LoadWeakRetained: {
3389         // If this is loading from the same pointer, replace this load's value
3390         // with that one.
3391         CallInst *Call = cast<CallInst>(Inst);
3392         CallInst *EarlierCall = cast<CallInst>(EarlierInst);
3393         Value *Arg = Call->getArgOperand(0);
3394         Value *EarlierArg = EarlierCall->getArgOperand(0);
3395         switch (PA.getAA()->alias(Arg, EarlierArg)) {
3396         case AliasAnalysis::MustAlias:
3397           Changed = true;
3398           // If the load has a builtin retain, insert a plain retain for it.
3399           if (Class == IC_LoadWeakRetained) {
3400             CallInst *CI =
3401               CallInst::Create(getRetainCallee(F.getParent()), EarlierCall,
3402                                "", Call);
3403             CI->setTailCall();
3404           }
3405           // Zap the fully redundant load.
3406           Call->replaceAllUsesWith(EarlierCall);
3407           Call->eraseFromParent();
3408           goto clobbered;
3409         case AliasAnalysis::MayAlias:
3410         case AliasAnalysis::PartialAlias:
3411           goto clobbered;
3412         case AliasAnalysis::NoAlias:
3413           break;
3414         }
3415         break;
3416       }
3417       case IC_StoreWeak:
3418       case IC_InitWeak: {
3419         // If this is storing to the same pointer and has the same size etc.
3420         // replace this load's value with the stored value.
3421         CallInst *Call = cast<CallInst>(Inst);
3422         CallInst *EarlierCall = cast<CallInst>(EarlierInst);
3423         Value *Arg = Call->getArgOperand(0);
3424         Value *EarlierArg = EarlierCall->getArgOperand(0);
3425         switch (PA.getAA()->alias(Arg, EarlierArg)) {
3426         case AliasAnalysis::MustAlias:
3427           Changed = true;
3428           // If the load has a builtin retain, insert a plain retain for it.
3429           if (Class == IC_LoadWeakRetained) {
3430             CallInst *CI =
3431               CallInst::Create(getRetainCallee(F.getParent()), EarlierCall,
3432                                "", Call);
3433             CI->setTailCall();
3434           }
3435           // Zap the fully redundant load.
3436           Call->replaceAllUsesWith(EarlierCall->getArgOperand(1));
3437           Call->eraseFromParent();
3438           goto clobbered;
3439         case AliasAnalysis::MayAlias:
3440         case AliasAnalysis::PartialAlias:
3441           goto clobbered;
3442         case AliasAnalysis::NoAlias:
3443           break;
3444         }
3445         break;
3446       }
3447       case IC_MoveWeak:
3448       case IC_CopyWeak:
3449         // TOOD: Grab the copied value.
3450         goto clobbered;
3451       case IC_AutoreleasepoolPush:
3452       case IC_None:
3453       case IC_User:
3454         // Weak pointers are only modified through the weak entry points
3455         // (and arbitrary calls, which could call the weak entry points).
3456         break;
3457       default:
3458         // Anything else could modify the weak pointer.
3459         goto clobbered;
3460       }
3461     }
3462   clobbered:;
3463   }
3464
3465   // Then, for each destroyWeak with an alloca operand, check to see if
3466   // the alloca and all its users can be zapped.
3467   for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ) {
3468     Instruction *Inst = &*I++;
3469     InstructionClass Class = GetBasicInstructionClass(Inst);
3470     if (Class != IC_DestroyWeak)
3471       continue;
3472
3473     CallInst *Call = cast<CallInst>(Inst);
3474     Value *Arg = Call->getArgOperand(0);
3475     if (AllocaInst *Alloca = dyn_cast<AllocaInst>(Arg)) {
3476       for (Value::use_iterator UI = Alloca->use_begin(),
3477            UE = Alloca->use_end(); UI != UE; ++UI) {
3478         const Instruction *UserInst = cast<Instruction>(*UI);
3479         switch (GetBasicInstructionClass(UserInst)) {
3480         case IC_InitWeak:
3481         case IC_StoreWeak:
3482         case IC_DestroyWeak:
3483           continue;
3484         default:
3485           goto done;
3486         }
3487       }
3488       Changed = true;
3489       for (Value::use_iterator UI = Alloca->use_begin(),
3490            UE = Alloca->use_end(); UI != UE; ) {
3491         CallInst *UserInst = cast<CallInst>(*UI++);
3492         switch (GetBasicInstructionClass(UserInst)) {
3493         case IC_InitWeak:
3494         case IC_StoreWeak:
3495           // These functions return their second argument.
3496           UserInst->replaceAllUsesWith(UserInst->getArgOperand(1));
3497           break;
3498         case IC_DestroyWeak:
3499           // No return value.
3500           break;
3501         default:
3502           llvm_unreachable("alloca really is used!");
3503         }
3504         UserInst->eraseFromParent();
3505       }
3506       Alloca->eraseFromParent();
3507     done:;
3508     }
3509   }
3510 }
3511
3512 /// OptimizeSequences - Identify program paths which execute sequences of
3513 /// retains and releases which can be eliminated.
3514 bool ObjCARCOpt::OptimizeSequences(Function &F) {
3515   /// Releases, Retains - These are used to store the results of the main flow
3516   /// analysis. These use Value* as the key instead of Instruction* so that the
3517   /// map stays valid when we get around to rewriting code and calls get
3518   /// replaced by arguments.
3519   DenseMap<Value *, RRInfo> Releases;
3520   MapVector<Value *, RRInfo> Retains;
3521
3522   /// BBStates, This is used during the traversal of the function to track the
3523   /// states for each identified object at each block.
3524   DenseMap<const BasicBlock *, BBState> BBStates;
3525
3526   // Analyze the CFG of the function, and all instructions.
3527   bool NestingDetected = Visit(F, BBStates, Retains, Releases);
3528
3529   // Transform.
3530   return PerformCodePlacement(BBStates, Retains, Releases, F.getParent()) &&
3531          NestingDetected;
3532 }
3533
3534 /// OptimizeReturns - Look for this pattern:
3535 ///
3536 ///    %call = call i8* @something(...)
3537 ///    %2 = call i8* @objc_retain(i8* %call)
3538 ///    %3 = call i8* @objc_autorelease(i8* %2)
3539 ///    ret i8* %3
3540 ///
3541 /// And delete the retain and autorelease.
3542 ///
3543 /// Otherwise if it's just this:
3544 ///
3545 ///    %3 = call i8* @objc_autorelease(i8* %2)
3546 ///    ret i8* %3
3547 ///
3548 /// convert the autorelease to autoreleaseRV.
3549 void ObjCARCOpt::OptimizeReturns(Function &F) {
3550   if (!F.getReturnType()->isPointerTy())
3551     return;
3552
3553   SmallPtrSet<Instruction *, 4> DependingInstructions;
3554   SmallPtrSet<const BasicBlock *, 4> Visited;
3555   for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI) {
3556     BasicBlock *BB = FI;
3557     ReturnInst *Ret = dyn_cast<ReturnInst>(&BB->back());
3558     if (!Ret) continue;
3559
3560     const Value *Arg = StripPointerCastsAndObjCCalls(Ret->getOperand(0));
3561     FindDependencies(NeedsPositiveRetainCount, Arg,
3562                      BB, Ret, DependingInstructions, Visited, PA);
3563     if (DependingInstructions.size() != 1)
3564       goto next_block;
3565
3566     {
3567       CallInst *Autorelease =
3568         dyn_cast_or_null<CallInst>(*DependingInstructions.begin());
3569       if (!Autorelease)
3570         goto next_block;
3571       InstructionClass AutoreleaseClass = GetBasicInstructionClass(Autorelease);
3572       if (!IsAutorelease(AutoreleaseClass))
3573         goto next_block;
3574       if (GetObjCArg(Autorelease) != Arg)
3575         goto next_block;
3576
3577       DependingInstructions.clear();
3578       Visited.clear();
3579
3580       // Check that there is nothing that can affect the reference
3581       // count between the autorelease and the retain.
3582       FindDependencies(CanChangeRetainCount, Arg,
3583                        BB, Autorelease, DependingInstructions, Visited, PA);
3584       if (DependingInstructions.size() != 1)
3585         goto next_block;
3586
3587       {
3588         CallInst *Retain =
3589           dyn_cast_or_null<CallInst>(*DependingInstructions.begin());
3590
3591         // Check that we found a retain with the same argument.
3592         if (!Retain ||
3593             !IsRetain(GetBasicInstructionClass(Retain)) ||
3594             GetObjCArg(Retain) != Arg)
3595           goto next_block;
3596
3597         DependingInstructions.clear();
3598         Visited.clear();
3599
3600         // Convert the autorelease to an autoreleaseRV, since it's
3601         // returning the value.
3602         if (AutoreleaseClass == IC_Autorelease) {
3603           Autorelease->setCalledFunction(getAutoreleaseRVCallee(F.getParent()));
3604           AutoreleaseClass = IC_AutoreleaseRV;
3605         }
3606
3607         // Check that there is nothing that can affect the reference
3608         // count between the retain and the call.
3609         // Note that Retain need not be in BB.
3610         FindDependencies(CanChangeRetainCount, Arg, Retain->getParent(), Retain,
3611                          DependingInstructions, Visited, PA);
3612         if (DependingInstructions.size() != 1)
3613           goto next_block;
3614
3615         {
3616           CallInst *Call =
3617             dyn_cast_or_null<CallInst>(*DependingInstructions.begin());
3618
3619           // Check that the pointer is the return value of the call.
3620           if (!Call || Arg != Call)
3621             goto next_block;
3622
3623           // Check that the call is a regular call.
3624           InstructionClass Class = GetBasicInstructionClass(Call);
3625           if (Class != IC_CallOrUser && Class != IC_Call)
3626             goto next_block;
3627
3628           // If so, we can zap the retain and autorelease.
3629           Changed = true;
3630           ++NumRets;
3631           EraseInstruction(Retain);
3632           EraseInstruction(Autorelease);
3633         }
3634       }
3635     }
3636
3637   next_block:
3638     DependingInstructions.clear();
3639     Visited.clear();
3640   }
3641 }
3642
3643 bool ObjCARCOpt::doInitialization(Module &M) {
3644   if (!EnableARCOpts)
3645     return false;
3646
3647   // If nothing in the Module uses ARC, don't do anything.
3648   Run = ModuleHasARC(M);
3649   if (!Run)
3650     return false;
3651
3652   // Identify the imprecise release metadata kind.
3653   ImpreciseReleaseMDKind =
3654     M.getContext().getMDKindID("clang.imprecise_release");
3655   CopyOnEscapeMDKind =
3656     M.getContext().getMDKindID("clang.arc.copy_on_escape");
3657   NoObjCARCExceptionsMDKind =
3658     M.getContext().getMDKindID("clang.arc.no_objc_arc_exceptions");
3659
3660   // Intuitively, objc_retain and others are nocapture, however in practice
3661   // they are not, because they return their argument value. And objc_release
3662   // calls finalizers which can have arbitrary side effects.
3663
3664   // These are initialized lazily.
3665   RetainRVCallee = 0;
3666   AutoreleaseRVCallee = 0;
3667   ReleaseCallee = 0;
3668   RetainCallee = 0;
3669   RetainBlockCallee = 0;
3670   AutoreleaseCallee = 0;
3671
3672   return false;
3673 }
3674
3675 bool ObjCARCOpt::runOnFunction(Function &F) {
3676   if (!EnableARCOpts)
3677     return false;
3678
3679   // If nothing in the Module uses ARC, don't do anything.
3680   if (!Run)
3681     return false;
3682
3683   Changed = false;
3684
3685   PA.setAA(&getAnalysis<AliasAnalysis>());
3686
3687   // This pass performs several distinct transformations. As a compile-time aid
3688   // when compiling code that isn't ObjC, skip these if the relevant ObjC
3689   // library functions aren't declared.
3690
3691   // Preliminary optimizations. This also computs UsedInThisFunction.
3692   OptimizeIndividualCalls(F);
3693
3694   // Optimizations for weak pointers.
3695   if (UsedInThisFunction & ((1 << IC_LoadWeak) |
3696                             (1 << IC_LoadWeakRetained) |
3697                             (1 << IC_StoreWeak) |
3698                             (1 << IC_InitWeak) |
3699                             (1 << IC_CopyWeak) |
3700                             (1 << IC_MoveWeak) |
3701                             (1 << IC_DestroyWeak)))
3702     OptimizeWeakCalls(F);
3703
3704   // Optimizations for retain+release pairs.
3705   if (UsedInThisFunction & ((1 << IC_Retain) |
3706                             (1 << IC_RetainRV) |
3707                             (1 << IC_RetainBlock)))
3708     if (UsedInThisFunction & (1 << IC_Release))
3709       // Run OptimizeSequences until it either stops making changes or
3710       // no retain+release pair nesting is detected.
3711       while (OptimizeSequences(F)) {}
3712
3713   // Optimizations if objc_autorelease is used.
3714   if (UsedInThisFunction & ((1 << IC_Autorelease) |
3715                             (1 << IC_AutoreleaseRV)))
3716     OptimizeReturns(F);
3717
3718   return Changed;
3719 }
3720
3721 void ObjCARCOpt::releaseMemory() {
3722   PA.clear();
3723 }
3724
3725 //===----------------------------------------------------------------------===//
3726 // ARC contraction.
3727 //===----------------------------------------------------------------------===//
3728
3729 // TODO: ObjCARCContract could insert PHI nodes when uses aren't
3730 // dominated by single calls.
3731
3732 #include "llvm/Operator.h"
3733 #include "llvm/InlineAsm.h"
3734 #include "llvm/Analysis/Dominators.h"
3735
3736 STATISTIC(NumStoreStrongs, "Number objc_storeStrong calls formed");
3737
3738 namespace {
3739   /// ObjCARCContract - Late ARC optimizations.  These change the IR in a way
3740   /// that makes it difficult to be analyzed by ObjCARCOpt, so it's run late.
3741   class ObjCARCContract : public FunctionPass {
3742     bool Changed;
3743     AliasAnalysis *AA;
3744     DominatorTree *DT;
3745     ProvenanceAnalysis PA;
3746
3747     /// Run - A flag indicating whether this optimization pass should run.
3748     bool Run;
3749
3750     /// StoreStrongCallee, etc. - Declarations for ObjC runtime
3751     /// functions, for use in creating calls to them. These are initialized
3752     /// lazily to avoid cluttering up the Module with unused declarations.
3753     Constant *StoreStrongCallee,
3754              *RetainAutoreleaseCallee, *RetainAutoreleaseRVCallee;
3755
3756     /// RetainRVMarker - The inline asm string to insert between calls and
3757     /// RetainRV calls to make the optimization work on targets which need it.
3758     const MDString *RetainRVMarker;
3759
3760     /// StoreStrongCalls - The set of inserted objc_storeStrong calls. If
3761     /// at the end of walking the function we have found no alloca
3762     /// instructions, these calls can be marked "tail".
3763     SmallPtrSet<CallInst *, 8> StoreStrongCalls;
3764
3765     Constant *getStoreStrongCallee(Module *M);
3766     Constant *getRetainAutoreleaseCallee(Module *M);
3767     Constant *getRetainAutoreleaseRVCallee(Module *M);
3768
3769     bool ContractAutorelease(Function &F, Instruction *Autorelease,
3770                              InstructionClass Class,
3771                              SmallPtrSet<Instruction *, 4>
3772                                &DependingInstructions,
3773                              SmallPtrSet<const BasicBlock *, 4>
3774                                &Visited);
3775
3776     void ContractRelease(Instruction *Release,
3777                          inst_iterator &Iter);
3778
3779     virtual void getAnalysisUsage(AnalysisUsage &AU) const;
3780     virtual bool doInitialization(Module &M);
3781     virtual bool runOnFunction(Function &F);
3782
3783   public:
3784     static char ID;
3785     ObjCARCContract() : FunctionPass(ID) {
3786       initializeObjCARCContractPass(*PassRegistry::getPassRegistry());
3787     }
3788   };
3789 }
3790
3791 char ObjCARCContract::ID = 0;
3792 INITIALIZE_PASS_BEGIN(ObjCARCContract,
3793                       "objc-arc-contract", "ObjC ARC contraction", false, false)
3794 INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
3795 INITIALIZE_PASS_DEPENDENCY(DominatorTree)
3796 INITIALIZE_PASS_END(ObjCARCContract,
3797                     "objc-arc-contract", "ObjC ARC contraction", false, false)
3798
3799 Pass *llvm::createObjCARCContractPass() {
3800   return new ObjCARCContract();
3801 }
3802
3803 void ObjCARCContract::getAnalysisUsage(AnalysisUsage &AU) const {
3804   AU.addRequired<AliasAnalysis>();
3805   AU.addRequired<DominatorTree>();
3806   AU.setPreservesCFG();
3807 }
3808
3809 Constant *ObjCARCContract::getStoreStrongCallee(Module *M) {
3810   if (!StoreStrongCallee) {
3811     LLVMContext &C = M->getContext();
3812     Type *I8X = PointerType::getUnqual(Type::getInt8Ty(C));
3813     Type *I8XX = PointerType::getUnqual(I8X);
3814     Type *Params[] = { I8XX, I8X };
3815
3816     AttrListPtr Attributes = AttrListPtr()
3817       .addAttr(~0u, Attribute::NoUnwind)
3818       .addAttr(1, Attribute::NoCapture);
3819
3820     StoreStrongCallee =
3821       M->getOrInsertFunction(
3822         "objc_storeStrong",
3823         FunctionType::get(Type::getVoidTy(C), Params, /*isVarArg=*/false),
3824         Attributes);
3825   }
3826   return StoreStrongCallee;
3827 }
3828
3829 Constant *ObjCARCContract::getRetainAutoreleaseCallee(Module *M) {
3830   if (!RetainAutoreleaseCallee) {
3831     LLVMContext &C = M->getContext();
3832     Type *I8X = PointerType::getUnqual(Type::getInt8Ty(C));
3833     Type *Params[] = { I8X };
3834     FunctionType *FTy = FunctionType::get(I8X, Params, /*isVarArg=*/false);
3835     AttrListPtr Attributes = AttrListPtr().addAttr(~0u, Attribute::NoUnwind);
3836     RetainAutoreleaseCallee =
3837       M->getOrInsertFunction("objc_retainAutorelease", FTy, Attributes);
3838   }
3839   return RetainAutoreleaseCallee;
3840 }
3841
3842 Constant *ObjCARCContract::getRetainAutoreleaseRVCallee(Module *M) {
3843   if (!RetainAutoreleaseRVCallee) {
3844     LLVMContext &C = M->getContext();
3845     Type *I8X = PointerType::getUnqual(Type::getInt8Ty(C));
3846     Type *Params[] = { I8X };
3847     FunctionType *FTy = FunctionType::get(I8X, Params, /*isVarArg=*/false);
3848     AttrListPtr Attributes = AttrListPtr().addAttr(~0u, Attribute::NoUnwind);
3849     RetainAutoreleaseRVCallee =
3850       M->getOrInsertFunction("objc_retainAutoreleaseReturnValue", FTy,
3851                              Attributes);
3852   }
3853   return RetainAutoreleaseRVCallee;
3854 }
3855
3856 /// ContractAutorelease - Merge an autorelease with a retain into a fused call.
3857 bool
3858 ObjCARCContract::ContractAutorelease(Function &F, Instruction *Autorelease,
3859                                      InstructionClass Class,
3860                                      SmallPtrSet<Instruction *, 4>
3861                                        &DependingInstructions,
3862                                      SmallPtrSet<const BasicBlock *, 4>
3863                                        &Visited) {
3864   const Value *Arg = GetObjCArg(Autorelease);
3865
3866   // Check that there are no instructions between the retain and the autorelease
3867   // (such as an autorelease_pop) which may change the count.
3868   CallInst *Retain = 0;
3869   if (Class == IC_AutoreleaseRV)
3870     FindDependencies(RetainAutoreleaseRVDep, Arg,
3871                      Autorelease->getParent(), Autorelease,
3872                      DependingInstructions, Visited, PA);
3873   else
3874     FindDependencies(RetainAutoreleaseDep, Arg,
3875                      Autorelease->getParent(), Autorelease,
3876                      DependingInstructions, Visited, PA);
3877
3878   Visited.clear();
3879   if (DependingInstructions.size() != 1) {
3880     DependingInstructions.clear();
3881     return false;
3882   }
3883
3884   Retain = dyn_cast_or_null<CallInst>(*DependingInstructions.begin());
3885   DependingInstructions.clear();
3886
3887   if (!Retain ||
3888       GetBasicInstructionClass(Retain) != IC_Retain ||
3889       GetObjCArg(Retain) != Arg)
3890     return false;
3891
3892   Changed = true;
3893   ++NumPeeps;
3894
3895   if (Class == IC_AutoreleaseRV)
3896     Retain->setCalledFunction(getRetainAutoreleaseRVCallee(F.getParent()));
3897   else
3898     Retain->setCalledFunction(getRetainAutoreleaseCallee(F.getParent()));
3899
3900   EraseInstruction(Autorelease);
3901   return true;
3902 }
3903
3904 /// ContractRelease - Attempt to merge an objc_release with a store, load, and
3905 /// objc_retain to form an objc_storeStrong. This can be a little tricky because
3906 /// the instructions don't always appear in order, and there may be unrelated
3907 /// intervening instructions.
3908 void ObjCARCContract::ContractRelease(Instruction *Release,
3909                                       inst_iterator &Iter) {
3910   LoadInst *Load = dyn_cast<LoadInst>(GetObjCArg(Release));
3911   if (!Load || !Load->isSimple()) return;
3912
3913   // For now, require everything to be in one basic block.
3914   BasicBlock *BB = Release->getParent();
3915   if (Load->getParent() != BB) return;
3916
3917   // Walk down to find the store and the release, which may be in either order.
3918   BasicBlock::iterator I = Load, End = BB->end();
3919   ++I;
3920   AliasAnalysis::Location Loc = AA->getLocation(Load);
3921   StoreInst *Store = 0;
3922   bool SawRelease = false;
3923   for (; !Store || !SawRelease; ++I) {
3924     if (I == End)
3925       return;
3926
3927     Instruction *Inst = I;
3928     if (Inst == Release) {
3929       SawRelease = true;
3930       continue;
3931     }
3932
3933     InstructionClass Class = GetBasicInstructionClass(Inst);
3934
3935     // Unrelated retains are harmless.
3936     if (IsRetain(Class))
3937       continue;
3938
3939     if (Store) {
3940       // The store is the point where we're going to put the objc_storeStrong,
3941       // so make sure there are no uses after it.
3942       if (CanUse(Inst, Load, PA, Class))
3943         return;
3944     } else if (AA->getModRefInfo(Inst, Loc) & AliasAnalysis::Mod) {
3945       // We are moving the load down to the store, so check for anything
3946       // else which writes to the memory between the load and the store.
3947       Store = dyn_cast<StoreInst>(Inst);
3948       if (!Store || !Store->isSimple()) return;
3949       if (Store->getPointerOperand() != Loc.Ptr) return;
3950     }
3951   }
3952
3953   Value *New = StripPointerCastsAndObjCCalls(Store->getValueOperand());
3954
3955   // Walk up to find the retain.
3956   I = Store;
3957   BasicBlock::iterator Begin = BB->begin();
3958   while (I != Begin && GetBasicInstructionClass(I) != IC_Retain)
3959     --I;
3960   Instruction *Retain = I;
3961   if (GetBasicInstructionClass(Retain) != IC_Retain) return;
3962   if (GetObjCArg(Retain) != New) return;
3963
3964   Changed = true;
3965   ++NumStoreStrongs;
3966
3967   LLVMContext &C = Release->getContext();
3968   Type *I8X = PointerType::getUnqual(Type::getInt8Ty(C));
3969   Type *I8XX = PointerType::getUnqual(I8X);
3970
3971   Value *Args[] = { Load->getPointerOperand(), New };
3972   if (Args[0]->getType() != I8XX)
3973     Args[0] = new BitCastInst(Args[0], I8XX, "", Store);
3974   if (Args[1]->getType() != I8X)
3975     Args[1] = new BitCastInst(Args[1], I8X, "", Store);
3976   CallInst *StoreStrong =
3977     CallInst::Create(getStoreStrongCallee(BB->getParent()->getParent()),
3978                      Args, "", Store);
3979   StoreStrong->setDoesNotThrow();
3980   StoreStrong->setDebugLoc(Store->getDebugLoc());
3981
3982   // We can't set the tail flag yet, because we haven't yet determined
3983   // whether there are any escaping allocas. Remember this call, so that
3984   // we can set the tail flag once we know it's safe.
3985   StoreStrongCalls.insert(StoreStrong);
3986
3987   if (&*Iter == Store) ++Iter;
3988   Store->eraseFromParent();
3989   Release->eraseFromParent();
3990   EraseInstruction(Retain);
3991   if (Load->use_empty())
3992     Load->eraseFromParent();
3993 }
3994
3995 bool ObjCARCContract::doInitialization(Module &M) {
3996   // If nothing in the Module uses ARC, don't do anything.
3997   Run = ModuleHasARC(M);
3998   if (!Run)
3999     return false;
4000
4001   // These are initialized lazily.
4002   StoreStrongCallee = 0;
4003   RetainAutoreleaseCallee = 0;
4004   RetainAutoreleaseRVCallee = 0;
4005
4006   // Initialize RetainRVMarker.
4007   RetainRVMarker = 0;
4008   if (NamedMDNode *NMD =
4009         M.getNamedMetadata("clang.arc.retainAutoreleasedReturnValueMarker"))
4010     if (NMD->getNumOperands() == 1) {
4011       const MDNode *N = NMD->getOperand(0);
4012       if (N->getNumOperands() == 1)
4013         if (const MDString *S = dyn_cast<MDString>(N->getOperand(0)))
4014           RetainRVMarker = S;
4015     }
4016
4017   return false;
4018 }
4019
4020 bool ObjCARCContract::runOnFunction(Function &F) {
4021   if (!EnableARCOpts)
4022     return false;
4023
4024   // If nothing in the Module uses ARC, don't do anything.
4025   if (!Run)
4026     return false;
4027
4028   Changed = false;
4029   AA = &getAnalysis<AliasAnalysis>();
4030   DT = &getAnalysis<DominatorTree>();
4031
4032   PA.setAA(&getAnalysis<AliasAnalysis>());
4033
4034   // Track whether it's ok to mark objc_storeStrong calls with the "tail"
4035   // keyword. Be conservative if the function has variadic arguments.
4036   // It seems that functions which "return twice" are also unsafe for the
4037   // "tail" argument, because they are setjmp, which could need to
4038   // return to an earlier stack state.
4039   bool TailOkForStoreStrongs = !F.isVarArg() &&
4040                                !F.callsFunctionThatReturnsTwice();
4041
4042   // For ObjC library calls which return their argument, replace uses of the
4043   // argument with uses of the call return value, if it dominates the use. This
4044   // reduces register pressure.
4045   SmallPtrSet<Instruction *, 4> DependingInstructions;
4046   SmallPtrSet<const BasicBlock *, 4> Visited;
4047   for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ) {
4048     Instruction *Inst = &*I++;
4049
4050     // Only these library routines return their argument. In particular,
4051     // objc_retainBlock does not necessarily return its argument.
4052     InstructionClass Class = GetBasicInstructionClass(Inst);
4053     switch (Class) {
4054     case IC_Retain:
4055     case IC_FusedRetainAutorelease:
4056     case IC_FusedRetainAutoreleaseRV:
4057       break;
4058     case IC_Autorelease:
4059     case IC_AutoreleaseRV:
4060       if (ContractAutorelease(F, Inst, Class, DependingInstructions, Visited))
4061         continue;
4062       break;
4063     case IC_RetainRV: {
4064       // If we're compiling for a target which needs a special inline-asm
4065       // marker to do the retainAutoreleasedReturnValue optimization,
4066       // insert it now.
4067       if (!RetainRVMarker)
4068         break;
4069       BasicBlock::iterator BBI = Inst;
4070       BasicBlock *InstParent = Inst->getParent();
4071
4072       // Step up to see if the call immediately precedes the RetainRV call.
4073       // If it's an invoke, we have to cross a block boundary. And we have
4074       // to carefully dodge no-op instructions.
4075       do {
4076         if (&*BBI == InstParent->begin()) {
4077           BasicBlock *Pred = InstParent->getSinglePredecessor();
4078           if (!Pred)
4079             goto decline_rv_optimization;
4080           BBI = Pred->getTerminator();
4081           break;
4082         }
4083         --BBI;
4084       } while (isNoopInstruction(BBI));
4085
4086       if (&*BBI == GetObjCArg(Inst)) {
4087         Changed = true;
4088         InlineAsm *IA =
4089           InlineAsm::get(FunctionType::get(Type::getVoidTy(Inst->getContext()),
4090                                            /*isVarArg=*/false),
4091                          RetainRVMarker->getString(),
4092                          /*Constraints=*/"", /*hasSideEffects=*/true);
4093         CallInst::Create(IA, "", Inst);
4094       }
4095     decline_rv_optimization:
4096       break;
4097     }
4098     case IC_InitWeak: {
4099       // objc_initWeak(p, null) => *p = null
4100       CallInst *CI = cast<CallInst>(Inst);
4101       if (isNullOrUndef(CI->getArgOperand(1))) {
4102         Value *Null =
4103           ConstantPointerNull::get(cast<PointerType>(CI->getType()));
4104         Changed = true;
4105         new StoreInst(Null, CI->getArgOperand(0), CI);
4106         CI->replaceAllUsesWith(Null);
4107         CI->eraseFromParent();
4108       }
4109       continue;
4110     }
4111     case IC_Release:
4112       ContractRelease(Inst, I);
4113       continue;
4114     case IC_User:
4115       // Be conservative if the function has any alloca instructions.
4116       // Technically we only care about escaping alloca instructions,
4117       // but this is sufficient to handle some interesting cases.
4118       if (isa<AllocaInst>(Inst))
4119         TailOkForStoreStrongs = false;
4120       continue;
4121     default:
4122       continue;
4123     }
4124
4125     // Don't use GetObjCArg because we don't want to look through bitcasts
4126     // and such; to do the replacement, the argument must have type i8*.
4127     const Value *Arg = cast<CallInst>(Inst)->getArgOperand(0);
4128     for (;;) {
4129       // If we're compiling bugpointed code, don't get in trouble.
4130       if (!isa<Instruction>(Arg) && !isa<Argument>(Arg))
4131         break;
4132       // Look through the uses of the pointer.
4133       for (Value::const_use_iterator UI = Arg->use_begin(), UE = Arg->use_end();
4134            UI != UE; ) {
4135         Use &U = UI.getUse();
4136         unsigned OperandNo = UI.getOperandNo();
4137         ++UI; // Increment UI now, because we may unlink its element.
4138
4139         // If the call's return value dominates a use of the call's argument
4140         // value, rewrite the use to use the return value. We check for
4141         // reachability here because an unreachable call is considered to
4142         // trivially dominate itself, which would lead us to rewriting its
4143         // argument in terms of its return value, which would lead to
4144         // infinite loops in GetObjCArg.
4145         if (DT->isReachableFromEntry(U) && DT->dominates(Inst, U)) {
4146           Changed = true;
4147           Instruction *Replacement = Inst;
4148           Type *UseTy = U.get()->getType();
4149           if (PHINode *PHI = dyn_cast<PHINode>(U.getUser())) {
4150             // For PHI nodes, insert the bitcast in the predecessor block.
4151             unsigned ValNo = PHINode::getIncomingValueNumForOperand(OperandNo);
4152             BasicBlock *BB = PHI->getIncomingBlock(ValNo);
4153             if (Replacement->getType() != UseTy)
4154               Replacement = new BitCastInst(Replacement, UseTy, "",
4155                                             &BB->back());
4156             // While we're here, rewrite all edges for this PHI, rather
4157             // than just one use at a time, to minimize the number of
4158             // bitcasts we emit.
4159             for (unsigned i = 0, e = PHI->getNumIncomingValues(); i != e; ++i)
4160               if (PHI->getIncomingBlock(i) == BB) {
4161                 // Keep the UI iterator valid.
4162                 if (&PHI->getOperandUse(
4163                       PHINode::getOperandNumForIncomingValue(i)) ==
4164                     &UI.getUse())
4165                   ++UI;
4166                 PHI->setIncomingValue(i, Replacement);
4167               }
4168           } else {
4169             if (Replacement->getType() != UseTy)
4170               Replacement = new BitCastInst(Replacement, UseTy, "",
4171                                             cast<Instruction>(U.getUser()));
4172             U.set(Replacement);
4173           }
4174         }
4175       }
4176
4177       // If Arg is a no-op casted pointer, strip one level of casts and iterate.
4178       if (const BitCastInst *BI = dyn_cast<BitCastInst>(Arg))
4179         Arg = BI->getOperand(0);
4180       else if (isa<GEPOperator>(Arg) &&
4181                cast<GEPOperator>(Arg)->hasAllZeroIndices())
4182         Arg = cast<GEPOperator>(Arg)->getPointerOperand();
4183       else if (isa<GlobalAlias>(Arg) &&
4184                !cast<GlobalAlias>(Arg)->mayBeOverridden())
4185         Arg = cast<GlobalAlias>(Arg)->getAliasee();
4186       else
4187         break;
4188     }
4189   }
4190
4191   // If this function has no escaping allocas or suspicious vararg usage,
4192   // objc_storeStrong calls can be marked with the "tail" keyword.
4193   if (TailOkForStoreStrongs)
4194     for (SmallPtrSet<CallInst *, 8>::iterator I = StoreStrongCalls.begin(),
4195          E = StoreStrongCalls.end(); I != E; ++I)
4196       (*I)->setTailCall();
4197   StoreStrongCalls.clear();
4198
4199   return Changed;
4200 }