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