1*9880d681SAndroid Build Coastguard Worker //===- ObjCARC.h - ObjC ARC Optimization --------------*- C++ -*-----------===// 2*9880d681SAndroid Build Coastguard Worker // 3*9880d681SAndroid Build Coastguard Worker // The LLVM Compiler Infrastructure 4*9880d681SAndroid Build Coastguard Worker // 5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source 6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details. 7*9880d681SAndroid Build Coastguard Worker // 8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===// 9*9880d681SAndroid Build Coastguard Worker /// \file 10*9880d681SAndroid Build Coastguard Worker /// This file defines common definitions/declarations used by the ObjC ARC 11*9880d681SAndroid Build Coastguard Worker /// Optimizer. ARC stands for Automatic Reference Counting and is a system for 12*9880d681SAndroid Build Coastguard Worker /// managing reference counts for objects in Objective C. 13*9880d681SAndroid Build Coastguard Worker /// 14*9880d681SAndroid Build Coastguard Worker /// WARNING: This file knows about certain library functions. It recognizes them 15*9880d681SAndroid Build Coastguard Worker /// by name, and hardwires knowledge of their semantics. 16*9880d681SAndroid Build Coastguard Worker /// 17*9880d681SAndroid Build Coastguard Worker /// WARNING: This file knows about how certain Objective-C library functions are 18*9880d681SAndroid Build Coastguard Worker /// used. Naive LLVM IR transformations which would otherwise be 19*9880d681SAndroid Build Coastguard Worker /// behavior-preserving may break these assumptions. 20*9880d681SAndroid Build Coastguard Worker /// 21*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===// 22*9880d681SAndroid Build Coastguard Worker 23*9880d681SAndroid Build Coastguard Worker #ifndef LLVM_LIB_TRANSFORMS_OBJCARC_OBJCARC_H 24*9880d681SAndroid Build Coastguard Worker #define LLVM_LIB_TRANSFORMS_OBJCARC_OBJCARC_H 25*9880d681SAndroid Build Coastguard Worker 26*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/StringSwitch.h" 27*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/AliasAnalysis.h" 28*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/ObjCARCAnalysisUtils.h" 29*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/ObjCARCInstKind.h" 30*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/Passes.h" 31*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/ValueTracking.h" 32*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/CallSite.h" 33*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/InstIterator.h" 34*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Module.h" 35*9880d681SAndroid Build Coastguard Worker #include "llvm/Pass.h" 36*9880d681SAndroid Build Coastguard Worker #include "llvm/Transforms/ObjCARC.h" 37*9880d681SAndroid Build Coastguard Worker #include "llvm/Transforms/Utils/Local.h" 38*9880d681SAndroid Build Coastguard Worker 39*9880d681SAndroid Build Coastguard Worker namespace llvm { 40*9880d681SAndroid Build Coastguard Worker class raw_ostream; 41*9880d681SAndroid Build Coastguard Worker } 42*9880d681SAndroid Build Coastguard Worker 43*9880d681SAndroid Build Coastguard Worker namespace llvm { 44*9880d681SAndroid Build Coastguard Worker namespace objcarc { 45*9880d681SAndroid Build Coastguard Worker 46*9880d681SAndroid Build Coastguard Worker /// \brief Erase the given instruction. 47*9880d681SAndroid Build Coastguard Worker /// 48*9880d681SAndroid Build Coastguard Worker /// Many ObjC calls return their argument verbatim, 49*9880d681SAndroid Build Coastguard Worker /// so if it's such a call and the return value has users, replace them with the 50*9880d681SAndroid Build Coastguard Worker /// argument value. 51*9880d681SAndroid Build Coastguard Worker /// EraseInstruction(Instruction * CI)52*9880d681SAndroid Build Coastguard Workerstatic inline void EraseInstruction(Instruction *CI) { 53*9880d681SAndroid Build Coastguard Worker Value *OldArg = cast<CallInst>(CI)->getArgOperand(0); 54*9880d681SAndroid Build Coastguard Worker 55*9880d681SAndroid Build Coastguard Worker bool Unused = CI->use_empty(); 56*9880d681SAndroid Build Coastguard Worker 57*9880d681SAndroid Build Coastguard Worker if (!Unused) { 58*9880d681SAndroid Build Coastguard Worker // Replace the return value with the argument. 59*9880d681SAndroid Build Coastguard Worker assert((IsForwarding(GetBasicARCInstKind(CI)) || 60*9880d681SAndroid Build Coastguard Worker (IsNoopOnNull(GetBasicARCInstKind(CI)) && 61*9880d681SAndroid Build Coastguard Worker isa<ConstantPointerNull>(OldArg))) && 62*9880d681SAndroid Build Coastguard Worker "Can't delete non-forwarding instruction with users!"); 63*9880d681SAndroid Build Coastguard Worker CI->replaceAllUsesWith(OldArg); 64*9880d681SAndroid Build Coastguard Worker } 65*9880d681SAndroid Build Coastguard Worker 66*9880d681SAndroid Build Coastguard Worker CI->eraseFromParent(); 67*9880d681SAndroid Build Coastguard Worker 68*9880d681SAndroid Build Coastguard Worker if (Unused) 69*9880d681SAndroid Build Coastguard Worker RecursivelyDeleteTriviallyDeadInstructions(OldArg); 70*9880d681SAndroid Build Coastguard Worker } 71*9880d681SAndroid Build Coastguard Worker 72*9880d681SAndroid Build Coastguard Worker } // end namespace objcarc 73*9880d681SAndroid Build Coastguard Worker } // end namespace llvm 74*9880d681SAndroid Build Coastguard Worker 75*9880d681SAndroid Build Coastguard Worker #endif 76