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