1023d2bbbbedc6ed991b11381a987673133be2c81Michael Gottesman//===- ObjCARCUtil.cpp - ObjC ARC Optimization ----------------------------===//
264437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman//
364437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman//                     The LLVM Compiler Infrastructure
464437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman//
564437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman// This file is distributed under the University of Illinois Open Source
664437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman// License. See LICENSE.TXT for details.
764437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman//
864437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman//===----------------------------------------------------------------------===//
964437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman/// \file
1064437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman/// This file defines several utility functions used by various ARC
1164437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman/// optimizations which are IMHO too big to be in a header file.
1264437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman///
1364437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman/// WARNING: This file knows about certain library functions. It recognizes them
1464437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman/// by name, and hardwires knowledge of their semantics.
1564437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman///
1664437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman/// WARNING: This file knows about how certain Objective-C library functions are
1764437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman/// used. Naive LLVM IR transformations which would otherwise be
1864437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman/// behavior-preserving may break these assumptions.
1964437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman///
2064437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman//===----------------------------------------------------------------------===//
2164437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman
2264437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman#include "ObjCARC.h"
233a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman#include "llvm/IR/Intrinsics.h"
243a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman
2564437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesmanusing namespace llvm;
2664437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesmanusing namespace llvm::objcarc;
2764437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman
2864437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesmanraw_ostream &llvm::objcarc::operator<<(raw_ostream &OS,
2964437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman                                       const InstructionClass Class) {
3064437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman  switch (Class) {
3164437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman  case IC_Retain:
3264437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman    return OS << "IC_Retain";
3364437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman  case IC_RetainRV:
3464437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman    return OS << "IC_RetainRV";
3564437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman  case IC_RetainBlock:
3664437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman    return OS << "IC_RetainBlock";
3764437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman  case IC_Release:
3864437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman    return OS << "IC_Release";
3964437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman  case IC_Autorelease:
4064437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman    return OS << "IC_Autorelease";
4164437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman  case IC_AutoreleaseRV:
4264437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman    return OS << "IC_AutoreleaseRV";
4364437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman  case IC_AutoreleasepoolPush:
4464437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman    return OS << "IC_AutoreleasepoolPush";
4564437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman  case IC_AutoreleasepoolPop:
4664437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman    return OS << "IC_AutoreleasepoolPop";
4764437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman  case IC_NoopCast:
4864437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman    return OS << "IC_NoopCast";
4964437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman  case IC_FusedRetainAutorelease:
5064437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman    return OS << "IC_FusedRetainAutorelease";
5164437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman  case IC_FusedRetainAutoreleaseRV:
5264437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman    return OS << "IC_FusedRetainAutoreleaseRV";
5364437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman  case IC_LoadWeakRetained:
5464437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman    return OS << "IC_LoadWeakRetained";
5564437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman  case IC_StoreWeak:
5664437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman    return OS << "IC_StoreWeak";
5764437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman  case IC_InitWeak:
5864437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman    return OS << "IC_InitWeak";
5964437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman  case IC_LoadWeak:
6064437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman    return OS << "IC_LoadWeak";
6164437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman  case IC_MoveWeak:
6264437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman    return OS << "IC_MoveWeak";
6364437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman  case IC_CopyWeak:
6464437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman    return OS << "IC_CopyWeak";
6564437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman  case IC_DestroyWeak:
6664437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman    return OS << "IC_DestroyWeak";
6764437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman  case IC_StoreStrong:
6864437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman    return OS << "IC_StoreStrong";
6964437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman  case IC_CallOrUser:
7064437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman    return OS << "IC_CallOrUser";
7164437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman  case IC_Call:
7264437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman    return OS << "IC_Call";
7364437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman  case IC_User:
7464437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman    return OS << "IC_User";
751f9c4407c0e66f0c473ed5d6e3abcedda3a838c9John McCall  case IC_IntrinsicUser:
761f9c4407c0e66f0c473ed5d6e3abcedda3a838c9John McCall    return OS << "IC_IntrinsicUser";
7764437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman  case IC_None:
7864437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman    return OS << "IC_None";
7964437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman  }
8064437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman  llvm_unreachable("Unknown instruction class!");
8164437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman}
8264437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman
8364437ead05db4f5d28e697058180ae575c3cf75bMichael GottesmanInstructionClass llvm::objcarc::GetFunctionClass(const Function *F) {
8464437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman  Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
8564437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman
861f9c4407c0e66f0c473ed5d6e3abcedda3a838c9John McCall  // No (mandatory) arguments.
8764437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman  if (AI == AE)
8864437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman    return StringSwitch<InstructionClass>(F->getName())
8964437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman      .Case("objc_autoreleasePoolPush",  IC_AutoreleasepoolPush)
901f9c4407c0e66f0c473ed5d6e3abcedda3a838c9John McCall      .Case("clang.arc.use", IC_IntrinsicUser)
9164437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman      .Default(IC_CallOrUser);
9264437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman
9364437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman  // One argument.
9464437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman  const Argument *A0 = AI++;
9564437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman  if (AI == AE)
9664437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman    // Argument is a pointer.
9764437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman    if (PointerType *PTy = dyn_cast<PointerType>(A0->getType())) {
9864437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman      Type *ETy = PTy->getElementType();
9964437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman      // Argument is i8*.
10064437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman      if (ETy->isIntegerTy(8))
10164437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman        return StringSwitch<InstructionClass>(F->getName())
10264437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman          .Case("objc_retain",                IC_Retain)
10364437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman          .Case("objc_retainAutoreleasedReturnValue", IC_RetainRV)
10464437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman          .Case("objc_retainBlock",           IC_RetainBlock)
10564437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman          .Case("objc_release",               IC_Release)
10664437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman          .Case("objc_autorelease",           IC_Autorelease)
10764437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman          .Case("objc_autoreleaseReturnValue", IC_AutoreleaseRV)
10864437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman          .Case("objc_autoreleasePoolPop",    IC_AutoreleasepoolPop)
10964437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman          .Case("objc_retainedObject",        IC_NoopCast)
11064437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman          .Case("objc_unretainedObject",      IC_NoopCast)
11164437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman          .Case("objc_unretainedPointer",     IC_NoopCast)
11264437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman          .Case("objc_retain_autorelease",    IC_FusedRetainAutorelease)
11364437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman          .Case("objc_retainAutorelease",     IC_FusedRetainAutorelease)
11464437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman          .Case("objc_retainAutoreleaseReturnValue",IC_FusedRetainAutoreleaseRV)
1150a80c1240284c764c0f9cd043a2a18ff4152a203Michael Gottesman          .Case("objc_sync_enter", IC_User)
1160a80c1240284c764c0f9cd043a2a18ff4152a203Michael Gottesman          .Case("objc_sync_exit", IC_User)
11764437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman          .Default(IC_CallOrUser);
11864437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman
11964437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman      // Argument is i8**
12064437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman      if (PointerType *Pte = dyn_cast<PointerType>(ETy))
12164437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman        if (Pte->getElementType()->isIntegerTy(8))
12264437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman          return StringSwitch<InstructionClass>(F->getName())
12364437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman            .Case("objc_loadWeakRetained",      IC_LoadWeakRetained)
12464437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman            .Case("objc_loadWeak",              IC_LoadWeak)
12564437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman            .Case("objc_destroyWeak",           IC_DestroyWeak)
12664437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman            .Default(IC_CallOrUser);
12764437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman    }
12864437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman
12964437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman  // Two arguments, first is i8**.
13064437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman  const Argument *A1 = AI++;
13164437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman  if (AI == AE)
13264437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman    if (PointerType *PTy = dyn_cast<PointerType>(A0->getType()))
13364437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman      if (PointerType *Pte = dyn_cast<PointerType>(PTy->getElementType()))
13464437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman        if (Pte->getElementType()->isIntegerTy(8))
13564437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman          if (PointerType *PTy1 = dyn_cast<PointerType>(A1->getType())) {
13664437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman            Type *ETy1 = PTy1->getElementType();
13764437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman            // Second argument is i8*
13864437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman            if (ETy1->isIntegerTy(8))
13964437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman              return StringSwitch<InstructionClass>(F->getName())
14064437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman                .Case("objc_storeWeak",             IC_StoreWeak)
14164437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman                .Case("objc_initWeak",              IC_InitWeak)
14264437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman                .Case("objc_storeStrong",           IC_StoreStrong)
14364437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman                .Default(IC_CallOrUser);
14464437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman            // Second argument is i8**.
14564437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman            if (PointerType *Pte1 = dyn_cast<PointerType>(ETy1))
14664437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman              if (Pte1->getElementType()->isIntegerTy(8))
14764437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman                return StringSwitch<InstructionClass>(F->getName())
14864437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman                  .Case("objc_moveWeak",              IC_MoveWeak)
14964437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman                  .Case("objc_copyWeak",              IC_CopyWeak)
15026dbfb6a7882bc8f5d9364b4bec7ef1d339dd327Michael Gottesman                  // Ignore annotation calls. This is important to stop the
15126dbfb6a7882bc8f5d9364b4bec7ef1d339dd327Michael Gottesman                  // optimizer from treating annotations as uses which would
15226dbfb6a7882bc8f5d9364b4bec7ef1d339dd327Michael Gottesman                  // make the state of the pointers they are attempting to
15326dbfb6a7882bc8f5d9364b4bec7ef1d339dd327Michael Gottesman                  // elucidate to be incorrect.
15426dbfb6a7882bc8f5d9364b4bec7ef1d339dd327Michael Gottesman                  .Case("llvm.arc.annotation.topdown.bbstart", IC_None)
15526dbfb6a7882bc8f5d9364b4bec7ef1d339dd327Michael Gottesman                  .Case("llvm.arc.annotation.topdown.bbend", IC_None)
15626dbfb6a7882bc8f5d9364b4bec7ef1d339dd327Michael Gottesman                  .Case("llvm.arc.annotation.bottomup.bbstart", IC_None)
15726dbfb6a7882bc8f5d9364b4bec7ef1d339dd327Michael Gottesman                  .Case("llvm.arc.annotation.bottomup.bbend", IC_None)
15864437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman                  .Default(IC_CallOrUser);
15964437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman          }
16064437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman
16164437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman  // Anything else.
16264437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman  return IC_CallOrUser;
16364437ead05db4f5d28e697058180ae575c3cf75bMichael Gottesman}
1643a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman
1653a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman/// \brief Determine what kind of construct V is.
1663a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael GottesmanInstructionClass
1673a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesmanllvm::objcarc::GetInstructionClass(const Value *V) {
1683a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman  if (const Instruction *I = dyn_cast<Instruction>(V)) {
1693a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman    // Any instruction other than bitcast and gep with a pointer operand have a
1703a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman    // use of an objc pointer. Bitcasts, GEPs, Selects, PHIs transfer a pointer
1713a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman    // to a subsequent use, rather than using it themselves, in this sense.
1723a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman    // As a short cut, several other opcodes are known to have no pointer
1733a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman    // operands of interest. And ret is never followed by a release, so it's
1743a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman    // not interesting to examine.
1753a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman    switch (I->getOpcode()) {
1763a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman    case Instruction::Call: {
1773a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman      const CallInst *CI = cast<CallInst>(I);
1783a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman      // Check for calls to special functions.
1793a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman      if (const Function *F = CI->getCalledFunction()) {
1803a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman        InstructionClass Class = GetFunctionClass(F);
1813a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman        if (Class != IC_CallOrUser)
1823a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman          return Class;
1833a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman
1843a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman        // None of the intrinsic functions do objc_release. For intrinsics, the
1853a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman        // only question is whether or not they may be users.
1863a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman        switch (F->getIntrinsicID()) {
1873a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman        case Intrinsic::returnaddress: case Intrinsic::frameaddress:
1883a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman        case Intrinsic::stacksave: case Intrinsic::stackrestore:
1893a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman        case Intrinsic::vastart: case Intrinsic::vacopy: case Intrinsic::vaend:
1903a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman        case Intrinsic::objectsize: case Intrinsic::prefetch:
1913a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman        case Intrinsic::stackprotector:
1923a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman        case Intrinsic::eh_return_i32: case Intrinsic::eh_return_i64:
1933a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman        case Intrinsic::eh_typeid_for: case Intrinsic::eh_dwarf_cfa:
1943a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman        case Intrinsic::eh_sjlj_lsda: case Intrinsic::eh_sjlj_functioncontext:
1953a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman        case Intrinsic::init_trampoline: case Intrinsic::adjust_trampoline:
1963a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman        case Intrinsic::lifetime_start: case Intrinsic::lifetime_end:
1973a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman        case Intrinsic::invariant_start: case Intrinsic::invariant_end:
1983a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman        // Don't let dbg info affect our results.
1993a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman        case Intrinsic::dbg_declare: case Intrinsic::dbg_value:
2003a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman          // Short cut: Some intrinsics obviously don't use ObjC pointers.
2013a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman          return IC_None;
2023a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman        default:
2033a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman          break;
2043a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman        }
2053a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman      }
2063a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman      return GetCallSiteClass(CI);
2073a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman    }
2083a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman    case Instruction::Invoke:
2093a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman      return GetCallSiteClass(cast<InvokeInst>(I));
2103a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman    case Instruction::BitCast:
2113a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman    case Instruction::GetElementPtr:
2123a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman    case Instruction::Select: case Instruction::PHI:
2133a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman    case Instruction::Ret: case Instruction::Br:
2143a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman    case Instruction::Switch: case Instruction::IndirectBr:
2153a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman    case Instruction::Alloca: case Instruction::VAArg:
2163a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman    case Instruction::Add: case Instruction::FAdd:
2173a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman    case Instruction::Sub: case Instruction::FSub:
2183a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman    case Instruction::Mul: case Instruction::FMul:
2193a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman    case Instruction::SDiv: case Instruction::UDiv: case Instruction::FDiv:
2203a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman    case Instruction::SRem: case Instruction::URem: case Instruction::FRem:
2213a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman    case Instruction::Shl: case Instruction::LShr: case Instruction::AShr:
2223a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman    case Instruction::And: case Instruction::Or: case Instruction::Xor:
2233a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman    case Instruction::SExt: case Instruction::ZExt: case Instruction::Trunc:
2243a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman    case Instruction::IntToPtr: case Instruction::FCmp:
2253a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman    case Instruction::FPTrunc: case Instruction::FPExt:
2263a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman    case Instruction::FPToUI: case Instruction::FPToSI:
2273a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman    case Instruction::UIToFP: case Instruction::SIToFP:
2283a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman    case Instruction::InsertElement: case Instruction::ExtractElement:
2293a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman    case Instruction::ShuffleVector:
2303a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman    case Instruction::ExtractValue:
2313a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman      break;
2323a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman    case Instruction::ICmp:
2333a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman      // Comparing a pointer with null, or any other constant, isn't an
2343a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman      // interesting use, because we don't care what the pointer points to, or
2353a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman      // about the values of any other dynamic reference-counted pointers.
2363a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman      if (IsPotentialRetainableObjPtr(I->getOperand(1)))
2373a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman        return IC_User;
2383a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman      break;
2393a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman    default:
2403a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman      // For anything else, check all the operands.
2413a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman      // Note that this includes both operands of a Store: while the first
2423a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman      // operand isn't actually being dereferenced, it is being stored to
2433a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman      // memory where we can no longer track who might read it and dereference
2443a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman      // it, so we have to consider it potentially used.
2453a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman      for (User::const_op_iterator OI = I->op_begin(), OE = I->op_end();
2463a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman           OI != OE; ++OI)
2473a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman        if (IsPotentialRetainableObjPtr(*OI))
2483a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman          return IC_User;
2493a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman    }
2503a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman  }
2513a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman
2523a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman  // Otherwise, it's totally inert for ARC purposes.
2533a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman  return IC_None;
2543a57c37964adfbbf83b4b309a2ceda43ba6d8231Michael Gottesman}
255