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