Home | History | Annotate | Download | only in Analysis
      1 //===- ARCInstKind.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 /// \file
     10 /// This file defines several utility functions used by various ARC
     11 /// optimizations which are IMHO too big to be in a header file.
     12 ///
     13 /// WARNING: This file knows about certain library functions. It recognizes them
     14 /// by name, and hardwires knowledge of their semantics.
     15 ///
     16 /// WARNING: This file knows about how certain Objective-C library functions are
     17 /// used. Naive LLVM IR transformations which would otherwise be
     18 /// behavior-preserving may break these assumptions.
     19 ///
     20 //===----------------------------------------------------------------------===//
     21 
     22 #include "llvm/Analysis/ObjCARCInstKind.h"
     23 #include "llvm/Analysis/ObjCARCAnalysisUtils.h"
     24 #include "llvm/ADT/StringSwitch.h"
     25 #include "llvm/IR/Intrinsics.h"
     26 
     27 using namespace llvm;
     28 using namespace llvm::objcarc;
     29 
     30 raw_ostream &llvm::objcarc::operator<<(raw_ostream &OS,
     31                                        const ARCInstKind Class) {
     32   switch (Class) {
     33   case ARCInstKind::Retain:
     34     return OS << "ARCInstKind::Retain";
     35   case ARCInstKind::RetainRV:
     36     return OS << "ARCInstKind::RetainRV";
     37   case ARCInstKind::ClaimRV:
     38     return OS << "ARCInstKind::ClaimRV";
     39   case ARCInstKind::RetainBlock:
     40     return OS << "ARCInstKind::RetainBlock";
     41   case ARCInstKind::Release:
     42     return OS << "ARCInstKind::Release";
     43   case ARCInstKind::Autorelease:
     44     return OS << "ARCInstKind::Autorelease";
     45   case ARCInstKind::AutoreleaseRV:
     46     return OS << "ARCInstKind::AutoreleaseRV";
     47   case ARCInstKind::AutoreleasepoolPush:
     48     return OS << "ARCInstKind::AutoreleasepoolPush";
     49   case ARCInstKind::AutoreleasepoolPop:
     50     return OS << "ARCInstKind::AutoreleasepoolPop";
     51   case ARCInstKind::NoopCast:
     52     return OS << "ARCInstKind::NoopCast";
     53   case ARCInstKind::FusedRetainAutorelease:
     54     return OS << "ARCInstKind::FusedRetainAutorelease";
     55   case ARCInstKind::FusedRetainAutoreleaseRV:
     56     return OS << "ARCInstKind::FusedRetainAutoreleaseRV";
     57   case ARCInstKind::LoadWeakRetained:
     58     return OS << "ARCInstKind::LoadWeakRetained";
     59   case ARCInstKind::StoreWeak:
     60     return OS << "ARCInstKind::StoreWeak";
     61   case ARCInstKind::InitWeak:
     62     return OS << "ARCInstKind::InitWeak";
     63   case ARCInstKind::LoadWeak:
     64     return OS << "ARCInstKind::LoadWeak";
     65   case ARCInstKind::MoveWeak:
     66     return OS << "ARCInstKind::MoveWeak";
     67   case ARCInstKind::CopyWeak:
     68     return OS << "ARCInstKind::CopyWeak";
     69   case ARCInstKind::DestroyWeak:
     70     return OS << "ARCInstKind::DestroyWeak";
     71   case ARCInstKind::StoreStrong:
     72     return OS << "ARCInstKind::StoreStrong";
     73   case ARCInstKind::CallOrUser:
     74     return OS << "ARCInstKind::CallOrUser";
     75   case ARCInstKind::Call:
     76     return OS << "ARCInstKind::Call";
     77   case ARCInstKind::User:
     78     return OS << "ARCInstKind::User";
     79   case ARCInstKind::IntrinsicUser:
     80     return OS << "ARCInstKind::IntrinsicUser";
     81   case ARCInstKind::None:
     82     return OS << "ARCInstKind::None";
     83   }
     84   llvm_unreachable("Unknown instruction class!");
     85 }
     86 
     87 ARCInstKind llvm::objcarc::GetFunctionClass(const Function *F) {
     88   Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
     89 
     90   // No (mandatory) arguments.
     91   if (AI == AE)
     92     return StringSwitch<ARCInstKind>(F->getName())
     93         .Case("objc_autoreleasePoolPush", ARCInstKind::AutoreleasepoolPush)
     94         .Case("clang.arc.use", ARCInstKind::IntrinsicUser)
     95         .Default(ARCInstKind::CallOrUser);
     96 
     97   // One argument.
     98   const Argument *A0 = &*AI++;
     99   if (AI == AE)
    100     // Argument is a pointer.
    101     if (PointerType *PTy = dyn_cast<PointerType>(A0->getType())) {
    102       Type *ETy = PTy->getElementType();
    103       // Argument is i8*.
    104       if (ETy->isIntegerTy(8))
    105         return StringSwitch<ARCInstKind>(F->getName())
    106             .Case("objc_retain", ARCInstKind::Retain)
    107             .Case("objc_retainAutoreleasedReturnValue", ARCInstKind::RetainRV)
    108             .Case("objc_unsafeClaimAutoreleasedReturnValue",
    109                   ARCInstKind::ClaimRV)
    110             .Case("objc_retainBlock", ARCInstKind::RetainBlock)
    111             .Case("objc_release", ARCInstKind::Release)
    112             .Case("objc_autorelease", ARCInstKind::Autorelease)
    113             .Case("objc_autoreleaseReturnValue", ARCInstKind::AutoreleaseRV)
    114             .Case("objc_autoreleasePoolPop", ARCInstKind::AutoreleasepoolPop)
    115             .Case("objc_retainedObject", ARCInstKind::NoopCast)
    116             .Case("objc_unretainedObject", ARCInstKind::NoopCast)
    117             .Case("objc_unretainedPointer", ARCInstKind::NoopCast)
    118             .Case("objc_retain_autorelease",
    119                   ARCInstKind::FusedRetainAutorelease)
    120             .Case("objc_retainAutorelease", ARCInstKind::FusedRetainAutorelease)
    121             .Case("objc_retainAutoreleaseReturnValue",
    122                   ARCInstKind::FusedRetainAutoreleaseRV)
    123             .Case("objc_sync_enter", ARCInstKind::User)
    124             .Case("objc_sync_exit", ARCInstKind::User)
    125             .Default(ARCInstKind::CallOrUser);
    126 
    127       // Argument is i8**
    128       if (PointerType *Pte = dyn_cast<PointerType>(ETy))
    129         if (Pte->getElementType()->isIntegerTy(8))
    130           return StringSwitch<ARCInstKind>(F->getName())
    131               .Case("objc_loadWeakRetained", ARCInstKind::LoadWeakRetained)
    132               .Case("objc_loadWeak", ARCInstKind::LoadWeak)
    133               .Case("objc_destroyWeak", ARCInstKind::DestroyWeak)
    134               .Default(ARCInstKind::CallOrUser);
    135     }
    136 
    137   // Two arguments, first is i8**.
    138   const Argument *A1 = &*AI++;
    139   if (AI == AE)
    140     if (PointerType *PTy = dyn_cast<PointerType>(A0->getType()))
    141       if (PointerType *Pte = dyn_cast<PointerType>(PTy->getElementType()))
    142         if (Pte->getElementType()->isIntegerTy(8))
    143           if (PointerType *PTy1 = dyn_cast<PointerType>(A1->getType())) {
    144             Type *ETy1 = PTy1->getElementType();
    145             // Second argument is i8*
    146             if (ETy1->isIntegerTy(8))
    147               return StringSwitch<ARCInstKind>(F->getName())
    148                   .Case("objc_storeWeak", ARCInstKind::StoreWeak)
    149                   .Case("objc_initWeak", ARCInstKind::InitWeak)
    150                   .Case("objc_storeStrong", ARCInstKind::StoreStrong)
    151                   .Default(ARCInstKind::CallOrUser);
    152             // Second argument is i8**.
    153             if (PointerType *Pte1 = dyn_cast<PointerType>(ETy1))
    154               if (Pte1->getElementType()->isIntegerTy(8))
    155                 return StringSwitch<ARCInstKind>(F->getName())
    156                     .Case("objc_moveWeak", ARCInstKind::MoveWeak)
    157                     .Case("objc_copyWeak", ARCInstKind::CopyWeak)
    158                     // Ignore annotation calls. This is important to stop the
    159                     // optimizer from treating annotations as uses which would
    160                     // make the state of the pointers they are attempting to
    161                     // elucidate to be incorrect.
    162                     .Case("llvm.arc.annotation.topdown.bbstart",
    163                           ARCInstKind::None)
    164                     .Case("llvm.arc.annotation.topdown.bbend",
    165                           ARCInstKind::None)
    166                     .Case("llvm.arc.annotation.bottomup.bbstart",
    167                           ARCInstKind::None)
    168                     .Case("llvm.arc.annotation.bottomup.bbend",
    169                           ARCInstKind::None)
    170                     .Default(ARCInstKind::CallOrUser);
    171           }
    172 
    173   // Anything else.
    174   return ARCInstKind::CallOrUser;
    175 }
    176 
    177 // A whitelist of intrinsics that we know do not use objc pointers or decrement
    178 // ref counts.
    179 static bool isInertIntrinsic(unsigned ID) {
    180   // TODO: Make this into a covered switch.
    181   switch (ID) {
    182   case Intrinsic::returnaddress:
    183   case Intrinsic::frameaddress:
    184   case Intrinsic::stacksave:
    185   case Intrinsic::stackrestore:
    186   case Intrinsic::vastart:
    187   case Intrinsic::vacopy:
    188   case Intrinsic::vaend:
    189   case Intrinsic::objectsize:
    190   case Intrinsic::prefetch:
    191   case Intrinsic::stackprotector:
    192   case Intrinsic::eh_return_i32:
    193   case Intrinsic::eh_return_i64:
    194   case Intrinsic::eh_typeid_for:
    195   case Intrinsic::eh_dwarf_cfa:
    196   case Intrinsic::eh_sjlj_lsda:
    197   case Intrinsic::eh_sjlj_functioncontext:
    198   case Intrinsic::init_trampoline:
    199   case Intrinsic::adjust_trampoline:
    200   case Intrinsic::lifetime_start:
    201   case Intrinsic::lifetime_end:
    202   case Intrinsic::invariant_start:
    203   case Intrinsic::invariant_end:
    204   // Don't let dbg info affect our results.
    205   case Intrinsic::dbg_declare:
    206   case Intrinsic::dbg_value:
    207     // Short cut: Some intrinsics obviously don't use ObjC pointers.
    208     return true;
    209   default:
    210     return false;
    211   }
    212 }
    213 
    214 // A whitelist of intrinsics that we know do not use objc pointers or decrement
    215 // ref counts.
    216 static bool isUseOnlyIntrinsic(unsigned ID) {
    217   // We are conservative and even though intrinsics are unlikely to touch
    218   // reference counts, we white list them for safety.
    219   //
    220   // TODO: Expand this into a covered switch. There is a lot more here.
    221   switch (ID) {
    222   case Intrinsic::memcpy:
    223   case Intrinsic::memmove:
    224   case Intrinsic::memset:
    225     return true;
    226   default:
    227     return false;
    228   }
    229 }
    230 
    231 /// \brief Determine what kind of construct V is.
    232 ARCInstKind llvm::objcarc::GetARCInstKind(const Value *V) {
    233   if (const Instruction *I = dyn_cast<Instruction>(V)) {
    234     // Any instruction other than bitcast and gep with a pointer operand have a
    235     // use of an objc pointer. Bitcasts, GEPs, Selects, PHIs transfer a pointer
    236     // to a subsequent use, rather than using it themselves, in this sense.
    237     // As a short cut, several other opcodes are known to have no pointer
    238     // operands of interest. And ret is never followed by a release, so it's
    239     // not interesting to examine.
    240     switch (I->getOpcode()) {
    241     case Instruction::Call: {
    242       const CallInst *CI = cast<CallInst>(I);
    243       // See if we have a function that we know something about.
    244       if (const Function *F = CI->getCalledFunction()) {
    245         ARCInstKind Class = GetFunctionClass(F);
    246         if (Class != ARCInstKind::CallOrUser)
    247           return Class;
    248         Intrinsic::ID ID = F->getIntrinsicID();
    249         if (isInertIntrinsic(ID))
    250           return ARCInstKind::None;
    251         if (isUseOnlyIntrinsic(ID))
    252           return ARCInstKind::User;
    253       }
    254 
    255       // Otherwise, be conservative.
    256       return GetCallSiteClass(CI);
    257     }
    258     case Instruction::Invoke:
    259       // Otherwise, be conservative.
    260       return GetCallSiteClass(cast<InvokeInst>(I));
    261     case Instruction::BitCast:
    262     case Instruction::GetElementPtr:
    263     case Instruction::Select:
    264     case Instruction::PHI:
    265     case Instruction::Ret:
    266     case Instruction::Br:
    267     case Instruction::Switch:
    268     case Instruction::IndirectBr:
    269     case Instruction::Alloca:
    270     case Instruction::VAArg:
    271     case Instruction::Add:
    272     case Instruction::FAdd:
    273     case Instruction::Sub:
    274     case Instruction::FSub:
    275     case Instruction::Mul:
    276     case Instruction::FMul:
    277     case Instruction::SDiv:
    278     case Instruction::UDiv:
    279     case Instruction::FDiv:
    280     case Instruction::SRem:
    281     case Instruction::URem:
    282     case Instruction::FRem:
    283     case Instruction::Shl:
    284     case Instruction::LShr:
    285     case Instruction::AShr:
    286     case Instruction::And:
    287     case Instruction::Or:
    288     case Instruction::Xor:
    289     case Instruction::SExt:
    290     case Instruction::ZExt:
    291     case Instruction::Trunc:
    292     case Instruction::IntToPtr:
    293     case Instruction::FCmp:
    294     case Instruction::FPTrunc:
    295     case Instruction::FPExt:
    296     case Instruction::FPToUI:
    297     case Instruction::FPToSI:
    298     case Instruction::UIToFP:
    299     case Instruction::SIToFP:
    300     case Instruction::InsertElement:
    301     case Instruction::ExtractElement:
    302     case Instruction::ShuffleVector:
    303     case Instruction::ExtractValue:
    304       break;
    305     case Instruction::ICmp:
    306       // Comparing a pointer with null, or any other constant, isn't an
    307       // interesting use, because we don't care what the pointer points to, or
    308       // about the values of any other dynamic reference-counted pointers.
    309       if (IsPotentialRetainableObjPtr(I->getOperand(1)))
    310         return ARCInstKind::User;
    311       break;
    312     default:
    313       // For anything else, check all the operands.
    314       // Note that this includes both operands of a Store: while the first
    315       // operand isn't actually being dereferenced, it is being stored to
    316       // memory where we can no longer track who might read it and dereference
    317       // it, so we have to consider it potentially used.
    318       for (User::const_op_iterator OI = I->op_begin(), OE = I->op_end();
    319            OI != OE; ++OI)
    320         if (IsPotentialRetainableObjPtr(*OI))
    321           return ARCInstKind::User;
    322     }
    323   }
    324 
    325   // Otherwise, it's totally inert for ARC purposes.
    326   return ARCInstKind::None;
    327 }
    328 
    329 /// \brief Test if the given class is a kind of user.
    330 bool llvm::objcarc::IsUser(ARCInstKind Class) {
    331   switch (Class) {
    332   case ARCInstKind::User:
    333   case ARCInstKind::CallOrUser:
    334   case ARCInstKind::IntrinsicUser:
    335     return true;
    336   case ARCInstKind::Retain:
    337   case ARCInstKind::RetainRV:
    338   case ARCInstKind::RetainBlock:
    339   case ARCInstKind::Release:
    340   case ARCInstKind::Autorelease:
    341   case ARCInstKind::AutoreleaseRV:
    342   case ARCInstKind::AutoreleasepoolPush:
    343   case ARCInstKind::AutoreleasepoolPop:
    344   case ARCInstKind::NoopCast:
    345   case ARCInstKind::FusedRetainAutorelease:
    346   case ARCInstKind::FusedRetainAutoreleaseRV:
    347   case ARCInstKind::LoadWeakRetained:
    348   case ARCInstKind::StoreWeak:
    349   case ARCInstKind::InitWeak:
    350   case ARCInstKind::LoadWeak:
    351   case ARCInstKind::MoveWeak:
    352   case ARCInstKind::CopyWeak:
    353   case ARCInstKind::DestroyWeak:
    354   case ARCInstKind::StoreStrong:
    355   case ARCInstKind::Call:
    356   case ARCInstKind::None:
    357   case ARCInstKind::ClaimRV:
    358     return false;
    359   }
    360   llvm_unreachable("covered switch isn't covered?");
    361 }
    362 
    363 /// \brief Test if the given class is objc_retain or equivalent.
    364 bool llvm::objcarc::IsRetain(ARCInstKind Class) {
    365   switch (Class) {
    366   case ARCInstKind::Retain:
    367   case ARCInstKind::RetainRV:
    368     return true;
    369   // I believe we treat retain block as not a retain since it can copy its
    370   // block.
    371   case ARCInstKind::RetainBlock:
    372   case ARCInstKind::Release:
    373   case ARCInstKind::Autorelease:
    374   case ARCInstKind::AutoreleaseRV:
    375   case ARCInstKind::AutoreleasepoolPush:
    376   case ARCInstKind::AutoreleasepoolPop:
    377   case ARCInstKind::NoopCast:
    378   case ARCInstKind::FusedRetainAutorelease:
    379   case ARCInstKind::FusedRetainAutoreleaseRV:
    380   case ARCInstKind::LoadWeakRetained:
    381   case ARCInstKind::StoreWeak:
    382   case ARCInstKind::InitWeak:
    383   case ARCInstKind::LoadWeak:
    384   case ARCInstKind::MoveWeak:
    385   case ARCInstKind::CopyWeak:
    386   case ARCInstKind::DestroyWeak:
    387   case ARCInstKind::StoreStrong:
    388   case ARCInstKind::IntrinsicUser:
    389   case ARCInstKind::CallOrUser:
    390   case ARCInstKind::Call:
    391   case ARCInstKind::User:
    392   case ARCInstKind::None:
    393   case ARCInstKind::ClaimRV:
    394     return false;
    395   }
    396   llvm_unreachable("covered switch isn't covered?");
    397 }
    398 
    399 /// \brief Test if the given class is objc_autorelease or equivalent.
    400 bool llvm::objcarc::IsAutorelease(ARCInstKind Class) {
    401   switch (Class) {
    402   case ARCInstKind::Autorelease:
    403   case ARCInstKind::AutoreleaseRV:
    404     return true;
    405   case ARCInstKind::Retain:
    406   case ARCInstKind::RetainRV:
    407   case ARCInstKind::ClaimRV:
    408   case ARCInstKind::RetainBlock:
    409   case ARCInstKind::Release:
    410   case ARCInstKind::AutoreleasepoolPush:
    411   case ARCInstKind::AutoreleasepoolPop:
    412   case ARCInstKind::NoopCast:
    413   case ARCInstKind::FusedRetainAutorelease:
    414   case ARCInstKind::FusedRetainAutoreleaseRV:
    415   case ARCInstKind::LoadWeakRetained:
    416   case ARCInstKind::StoreWeak:
    417   case ARCInstKind::InitWeak:
    418   case ARCInstKind::LoadWeak:
    419   case ARCInstKind::MoveWeak:
    420   case ARCInstKind::CopyWeak:
    421   case ARCInstKind::DestroyWeak:
    422   case ARCInstKind::StoreStrong:
    423   case ARCInstKind::IntrinsicUser:
    424   case ARCInstKind::CallOrUser:
    425   case ARCInstKind::Call:
    426   case ARCInstKind::User:
    427   case ARCInstKind::None:
    428     return false;
    429   }
    430   llvm_unreachable("covered switch isn't covered?");
    431 }
    432 
    433 /// \brief Test if the given class represents instructions which return their
    434 /// argument verbatim.
    435 bool llvm::objcarc::IsForwarding(ARCInstKind Class) {
    436   switch (Class) {
    437   case ARCInstKind::Retain:
    438   case ARCInstKind::RetainRV:
    439   case ARCInstKind::ClaimRV:
    440   case ARCInstKind::Autorelease:
    441   case ARCInstKind::AutoreleaseRV:
    442   case ARCInstKind::NoopCast:
    443     return true;
    444   case ARCInstKind::RetainBlock:
    445   case ARCInstKind::Release:
    446   case ARCInstKind::AutoreleasepoolPush:
    447   case ARCInstKind::AutoreleasepoolPop:
    448   case ARCInstKind::FusedRetainAutorelease:
    449   case ARCInstKind::FusedRetainAutoreleaseRV:
    450   case ARCInstKind::LoadWeakRetained:
    451   case ARCInstKind::StoreWeak:
    452   case ARCInstKind::InitWeak:
    453   case ARCInstKind::LoadWeak:
    454   case ARCInstKind::MoveWeak:
    455   case ARCInstKind::CopyWeak:
    456   case ARCInstKind::DestroyWeak:
    457   case ARCInstKind::StoreStrong:
    458   case ARCInstKind::IntrinsicUser:
    459   case ARCInstKind::CallOrUser:
    460   case ARCInstKind::Call:
    461   case ARCInstKind::User:
    462   case ARCInstKind::None:
    463     return false;
    464   }
    465   llvm_unreachable("covered switch isn't covered?");
    466 }
    467 
    468 /// \brief Test if the given class represents instructions which do nothing if
    469 /// passed a null pointer.
    470 bool llvm::objcarc::IsNoopOnNull(ARCInstKind Class) {
    471   switch (Class) {
    472   case ARCInstKind::Retain:
    473   case ARCInstKind::RetainRV:
    474   case ARCInstKind::ClaimRV:
    475   case ARCInstKind::Release:
    476   case ARCInstKind::Autorelease:
    477   case ARCInstKind::AutoreleaseRV:
    478   case ARCInstKind::RetainBlock:
    479     return true;
    480   case ARCInstKind::AutoreleasepoolPush:
    481   case ARCInstKind::AutoreleasepoolPop:
    482   case ARCInstKind::FusedRetainAutorelease:
    483   case ARCInstKind::FusedRetainAutoreleaseRV:
    484   case ARCInstKind::LoadWeakRetained:
    485   case ARCInstKind::StoreWeak:
    486   case ARCInstKind::InitWeak:
    487   case ARCInstKind::LoadWeak:
    488   case ARCInstKind::MoveWeak:
    489   case ARCInstKind::CopyWeak:
    490   case ARCInstKind::DestroyWeak:
    491   case ARCInstKind::StoreStrong:
    492   case ARCInstKind::IntrinsicUser:
    493   case ARCInstKind::CallOrUser:
    494   case ARCInstKind::Call:
    495   case ARCInstKind::User:
    496   case ARCInstKind::None:
    497   case ARCInstKind::NoopCast:
    498     return false;
    499   }
    500   llvm_unreachable("covered switch isn't covered?");
    501 }
    502 
    503 /// \brief Test if the given class represents instructions which are always safe
    504 /// to mark with the "tail" keyword.
    505 bool llvm::objcarc::IsAlwaysTail(ARCInstKind Class) {
    506   // ARCInstKind::RetainBlock may be given a stack argument.
    507   switch (Class) {
    508   case ARCInstKind::Retain:
    509   case ARCInstKind::RetainRV:
    510   case ARCInstKind::ClaimRV:
    511   case ARCInstKind::AutoreleaseRV:
    512     return true;
    513   case ARCInstKind::Release:
    514   case ARCInstKind::Autorelease:
    515   case ARCInstKind::RetainBlock:
    516   case ARCInstKind::AutoreleasepoolPush:
    517   case ARCInstKind::AutoreleasepoolPop:
    518   case ARCInstKind::FusedRetainAutorelease:
    519   case ARCInstKind::FusedRetainAutoreleaseRV:
    520   case ARCInstKind::LoadWeakRetained:
    521   case ARCInstKind::StoreWeak:
    522   case ARCInstKind::InitWeak:
    523   case ARCInstKind::LoadWeak:
    524   case ARCInstKind::MoveWeak:
    525   case ARCInstKind::CopyWeak:
    526   case ARCInstKind::DestroyWeak:
    527   case ARCInstKind::StoreStrong:
    528   case ARCInstKind::IntrinsicUser:
    529   case ARCInstKind::CallOrUser:
    530   case ARCInstKind::Call:
    531   case ARCInstKind::User:
    532   case ARCInstKind::None:
    533   case ARCInstKind::NoopCast:
    534     return false;
    535   }
    536   llvm_unreachable("covered switch isn't covered?");
    537 }
    538 
    539 /// \brief Test if the given class represents instructions which are never safe
    540 /// to mark with the "tail" keyword.
    541 bool llvm::objcarc::IsNeverTail(ARCInstKind Class) {
    542   /// It is never safe to tail call objc_autorelease since by tail calling
    543   /// objc_autorelease: fast autoreleasing causing our object to be potentially
    544   /// reclaimed from the autorelease pool which violates the semantics of
    545   /// __autoreleasing types in ARC.
    546   switch (Class) {
    547   case ARCInstKind::Autorelease:
    548     return true;
    549   case ARCInstKind::Retain:
    550   case ARCInstKind::RetainRV:
    551   case ARCInstKind::ClaimRV:
    552   case ARCInstKind::AutoreleaseRV:
    553   case ARCInstKind::Release:
    554   case ARCInstKind::RetainBlock:
    555   case ARCInstKind::AutoreleasepoolPush:
    556   case ARCInstKind::AutoreleasepoolPop:
    557   case ARCInstKind::FusedRetainAutorelease:
    558   case ARCInstKind::FusedRetainAutoreleaseRV:
    559   case ARCInstKind::LoadWeakRetained:
    560   case ARCInstKind::StoreWeak:
    561   case ARCInstKind::InitWeak:
    562   case ARCInstKind::LoadWeak:
    563   case ARCInstKind::MoveWeak:
    564   case ARCInstKind::CopyWeak:
    565   case ARCInstKind::DestroyWeak:
    566   case ARCInstKind::StoreStrong:
    567   case ARCInstKind::IntrinsicUser:
    568   case ARCInstKind::CallOrUser:
    569   case ARCInstKind::Call:
    570   case ARCInstKind::User:
    571   case ARCInstKind::None:
    572   case ARCInstKind::NoopCast:
    573     return false;
    574   }
    575   llvm_unreachable("covered switch isn't covered?");
    576 }
    577 
    578 /// \brief Test if the given class represents instructions which are always safe
    579 /// to mark with the nounwind attribute.
    580 bool llvm::objcarc::IsNoThrow(ARCInstKind Class) {
    581   // objc_retainBlock is not nounwind because it calls user copy constructors
    582   // which could theoretically throw.
    583   switch (Class) {
    584   case ARCInstKind::Retain:
    585   case ARCInstKind::RetainRV:
    586   case ARCInstKind::ClaimRV:
    587   case ARCInstKind::Release:
    588   case ARCInstKind::Autorelease:
    589   case ARCInstKind::AutoreleaseRV:
    590   case ARCInstKind::AutoreleasepoolPush:
    591   case ARCInstKind::AutoreleasepoolPop:
    592     return true;
    593   case ARCInstKind::RetainBlock:
    594   case ARCInstKind::FusedRetainAutorelease:
    595   case ARCInstKind::FusedRetainAutoreleaseRV:
    596   case ARCInstKind::LoadWeakRetained:
    597   case ARCInstKind::StoreWeak:
    598   case ARCInstKind::InitWeak:
    599   case ARCInstKind::LoadWeak:
    600   case ARCInstKind::MoveWeak:
    601   case ARCInstKind::CopyWeak:
    602   case ARCInstKind::DestroyWeak:
    603   case ARCInstKind::StoreStrong:
    604   case ARCInstKind::IntrinsicUser:
    605   case ARCInstKind::CallOrUser:
    606   case ARCInstKind::Call:
    607   case ARCInstKind::User:
    608   case ARCInstKind::None:
    609   case ARCInstKind::NoopCast:
    610     return false;
    611   }
    612   llvm_unreachable("covered switch isn't covered?");
    613 }
    614 
    615 /// Test whether the given instruction can autorelease any pointer or cause an
    616 /// autoreleasepool pop.
    617 ///
    618 /// This means that it *could* interrupt the RV optimization.
    619 bool llvm::objcarc::CanInterruptRV(ARCInstKind Class) {
    620   switch (Class) {
    621   case ARCInstKind::AutoreleasepoolPop:
    622   case ARCInstKind::CallOrUser:
    623   case ARCInstKind::Call:
    624   case ARCInstKind::Autorelease:
    625   case ARCInstKind::AutoreleaseRV:
    626   case ARCInstKind::FusedRetainAutorelease:
    627   case ARCInstKind::FusedRetainAutoreleaseRV:
    628     return true;
    629   case ARCInstKind::Retain:
    630   case ARCInstKind::RetainRV:
    631   case ARCInstKind::ClaimRV:
    632   case ARCInstKind::Release:
    633   case ARCInstKind::AutoreleasepoolPush:
    634   case ARCInstKind::RetainBlock:
    635   case ARCInstKind::LoadWeakRetained:
    636   case ARCInstKind::StoreWeak:
    637   case ARCInstKind::InitWeak:
    638   case ARCInstKind::LoadWeak:
    639   case ARCInstKind::MoveWeak:
    640   case ARCInstKind::CopyWeak:
    641   case ARCInstKind::DestroyWeak:
    642   case ARCInstKind::StoreStrong:
    643   case ARCInstKind::IntrinsicUser:
    644   case ARCInstKind::User:
    645   case ARCInstKind::None:
    646   case ARCInstKind::NoopCast:
    647     return false;
    648   }
    649   llvm_unreachable("covered switch isn't covered?");
    650 }
    651 
    652 bool llvm::objcarc::CanDecrementRefCount(ARCInstKind Kind) {
    653   switch (Kind) {
    654   case ARCInstKind::Retain:
    655   case ARCInstKind::RetainRV:
    656   case ARCInstKind::Autorelease:
    657   case ARCInstKind::AutoreleaseRV:
    658   case ARCInstKind::NoopCast:
    659   case ARCInstKind::FusedRetainAutorelease:
    660   case ARCInstKind::FusedRetainAutoreleaseRV:
    661   case ARCInstKind::IntrinsicUser:
    662   case ARCInstKind::User:
    663   case ARCInstKind::None:
    664     return false;
    665 
    666   // The cases below are conservative.
    667 
    668   // RetainBlock can result in user defined copy constructors being called
    669   // implying releases may occur.
    670   case ARCInstKind::RetainBlock:
    671   case ARCInstKind::Release:
    672   case ARCInstKind::AutoreleasepoolPush:
    673   case ARCInstKind::AutoreleasepoolPop:
    674   case ARCInstKind::LoadWeakRetained:
    675   case ARCInstKind::StoreWeak:
    676   case ARCInstKind::InitWeak:
    677   case ARCInstKind::LoadWeak:
    678   case ARCInstKind::MoveWeak:
    679   case ARCInstKind::CopyWeak:
    680   case ARCInstKind::DestroyWeak:
    681   case ARCInstKind::StoreStrong:
    682   case ARCInstKind::CallOrUser:
    683   case ARCInstKind::Call:
    684   case ARCInstKind::ClaimRV:
    685     return true;
    686   }
    687 
    688   llvm_unreachable("covered switch isn't covered?");
    689 }
    690