LLVM  mainline
ObjCARC.h
Go to the documentation of this file.
00001 //===- ObjCARC.h - ObjC ARC Optimization --------------*- C++ -*-----------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 /// \file
00010 /// This file defines common definitions/declarations used by the ObjC ARC
00011 /// Optimizer. ARC stands for Automatic Reference Counting and is a system for
00012 /// managing reference counts for objects in Objective C.
00013 ///
00014 /// WARNING: This file knows about certain library functions. It recognizes them
00015 /// by name, and hardwires knowledge of their semantics.
00016 ///
00017 /// WARNING: This file knows about how certain Objective-C library functions are
00018 /// used. Naive LLVM IR transformations which would otherwise be
00019 /// behavior-preserving may break these assumptions.
00020 ///
00021 //===----------------------------------------------------------------------===//
00022 
00023 #ifndef LLVM_LIB_TRANSFORMS_OBJCARC_OBJCARC_H
00024 #define LLVM_LIB_TRANSFORMS_OBJCARC_OBJCARC_H
00025 
00026 #include "llvm/ADT/StringSwitch.h"
00027 #include "llvm/ADT/Optional.h"
00028 #include "llvm/Analysis/AliasAnalysis.h"
00029 #include "llvm/Analysis/ObjCARCAnalysisUtils.h"
00030 #include "llvm/Analysis/ObjCARCInstKind.h"
00031 #include "llvm/Analysis/Passes.h"
00032 #include "llvm/Analysis/ValueTracking.h"
00033 #include "llvm/IR/CallSite.h"
00034 #include "llvm/IR/InstIterator.h"
00035 #include "llvm/IR/Module.h"
00036 #include "llvm/Pass.h"
00037 #include "llvm/Transforms/ObjCARC.h"
00038 #include "llvm/Transforms/Utils/Local.h"
00039 
00040 namespace llvm {
00041 class raw_ostream;
00042 }
00043 
00044 namespace llvm {
00045 namespace objcarc {
00046 
00047 /// \brief Erase the given instruction.
00048 ///
00049 /// Many ObjC calls return their argument verbatim,
00050 /// so if it's such a call and the return value has users, replace them with the
00051 /// argument value.
00052 ///
00053 static inline void EraseInstruction(Instruction *CI) {
00054   Value *OldArg = cast<CallInst>(CI)->getArgOperand(0);
00055 
00056   bool Unused = CI->use_empty();
00057 
00058   if (!Unused) {
00059     // Replace the return value with the argument.
00060     assert((IsForwarding(GetBasicARCInstKind(CI)) ||
00061             (IsNoopOnNull(GetBasicARCInstKind(CI)) &&
00062              isa<ConstantPointerNull>(OldArg))) &&
00063            "Can't delete non-forwarding instruction with users!");
00064     CI->replaceAllUsesWith(OldArg);
00065   }
00066 
00067   CI->eraseFromParent();
00068 
00069   if (Unused)
00070     RecursivelyDeleteTriviallyDeadInstructions(OldArg);
00071 }
00072 
00073 } // end namespace objcarc
00074 } // end namespace llvm
00075 
00076 #endif