1249259Sdim//===- ObjCARCExpand.cpp - ObjC ARC Optimization --------------------------===// 2249259Sdim// 3249259Sdim// The LLVM Compiler Infrastructure 4249259Sdim// 5249259Sdim// This file is distributed under the University of Illinois Open Source 6249259Sdim// License. See LICENSE.TXT for details. 7249259Sdim// 8249259Sdim//===----------------------------------------------------------------------===// 9249259Sdim/// \file 10249259Sdim/// This file defines ObjC ARC optimizations. ARC stands for Automatic 11249259Sdim/// Reference Counting and is a system for managing reference counts for objects 12249259Sdim/// in Objective C. 13249259Sdim/// 14249259Sdim/// This specific file deals with early optimizations which perform certain 15249259Sdim/// cleanup operations. 16249259Sdim/// 17249259Sdim/// WARNING: This file knows about certain library functions. It recognizes them 18249259Sdim/// by name, and hardwires knowledge of their semantics. 19249259Sdim/// 20249259Sdim/// WARNING: This file knows about how certain Objective-C library functions are 21249259Sdim/// used. Naive LLVM IR transformations which would otherwise be 22249259Sdim/// behavior-preserving may break these assumptions. 23249259Sdim/// 24249259Sdim//===----------------------------------------------------------------------===// 25249259Sdim 26249259Sdim#define DEBUG_TYPE "objc-arc-expand" 27249259Sdim 28249259Sdim#include "ObjCARC.h" 29249259Sdim#include "llvm/ADT/StringRef.h" 30249259Sdim#include "llvm/IR/Function.h" 31249259Sdim#include "llvm/IR/Instruction.h" 32249259Sdim#include "llvm/IR/Instructions.h" 33249259Sdim#include "llvm/IR/Value.h" 34249259Sdim#include "llvm/Pass.h" 35249259Sdim#include "llvm/PassAnalysisSupport.h" 36249259Sdim#include "llvm/PassRegistry.h" 37249259Sdim#include "llvm/PassSupport.h" 38249259Sdim#include "llvm/Support/Casting.h" 39249259Sdim#include "llvm/Support/Debug.h" 40249259Sdim#include "llvm/Support/InstIterator.h" 41249259Sdim#include "llvm/Support/raw_ostream.h" 42249259Sdim 43249259Sdimnamespace llvm { 44249259Sdim class Module; 45249259Sdim} 46249259Sdim 47249259Sdimusing namespace llvm; 48249259Sdimusing namespace llvm::objcarc; 49249259Sdim 50249259Sdimnamespace { 51249259Sdim /// \brief Early ARC transformations. 52249259Sdim class ObjCARCExpand : public FunctionPass { 53249259Sdim virtual void getAnalysisUsage(AnalysisUsage &AU) const; 54249259Sdim virtual bool doInitialization(Module &M); 55249259Sdim virtual bool runOnFunction(Function &F); 56249259Sdim 57249259Sdim /// A flag indicating whether this optimization pass should run. 58249259Sdim bool Run; 59249259Sdim 60249259Sdim public: 61249259Sdim static char ID; 62249259Sdim ObjCARCExpand() : FunctionPass(ID) { 63249259Sdim initializeObjCARCExpandPass(*PassRegistry::getPassRegistry()); 64249259Sdim } 65249259Sdim }; 66249259Sdim} 67249259Sdim 68249259Sdimchar ObjCARCExpand::ID = 0; 69249259SdimINITIALIZE_PASS(ObjCARCExpand, 70249259Sdim "objc-arc-expand", "ObjC ARC expansion", false, false) 71249259Sdim 72249259SdimPass *llvm::createObjCARCExpandPass() { 73249259Sdim return new ObjCARCExpand(); 74249259Sdim} 75249259Sdim 76249259Sdimvoid ObjCARCExpand::getAnalysisUsage(AnalysisUsage &AU) const { 77249259Sdim AU.setPreservesCFG(); 78249259Sdim} 79249259Sdim 80249259Sdimbool ObjCARCExpand::doInitialization(Module &M) { 81249259Sdim Run = ModuleHasARC(M); 82249259Sdim return false; 83249259Sdim} 84249259Sdim 85249259Sdimbool ObjCARCExpand::runOnFunction(Function &F) { 86249259Sdim if (!EnableARCOpts) 87249259Sdim return false; 88249259Sdim 89249259Sdim // If nothing in the Module uses ARC, don't do anything. 90249259Sdim if (!Run) 91249259Sdim return false; 92249259Sdim 93249259Sdim bool Changed = false; 94249259Sdim 95249259Sdim DEBUG(dbgs() << "ObjCARCExpand: Visiting Function: " << F.getName() << "\n"); 96249259Sdim 97249259Sdim for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ++I) { 98249259Sdim Instruction *Inst = &*I; 99249259Sdim 100249259Sdim DEBUG(dbgs() << "ObjCARCExpand: Visiting: " << *Inst << "\n"); 101249259Sdim 102249259Sdim switch (GetBasicInstructionClass(Inst)) { 103249259Sdim case IC_Retain: 104249259Sdim case IC_RetainRV: 105249259Sdim case IC_Autorelease: 106249259Sdim case IC_AutoreleaseRV: 107249259Sdim case IC_FusedRetainAutorelease: 108249259Sdim case IC_FusedRetainAutoreleaseRV: { 109249259Sdim // These calls return their argument verbatim, as a low-level 110249259Sdim // optimization. However, this makes high-level optimizations 111249259Sdim // harder. Undo any uses of this optimization that the front-end 112249259Sdim // emitted here. We'll redo them in the contract pass. 113249259Sdim Changed = true; 114249259Sdim Value *Value = cast<CallInst>(Inst)->getArgOperand(0); 115249259Sdim DEBUG(dbgs() << "ObjCARCExpand: Old = " << *Inst << "\n" 116249259Sdim " New = " << *Value << "\n"); 117249259Sdim Inst->replaceAllUsesWith(Value); 118249259Sdim break; 119249259Sdim } 120249259Sdim default: 121249259Sdim break; 122249259Sdim } 123249259Sdim } 124249259Sdim 125249259Sdim DEBUG(dbgs() << "ObjCARCExpand: Finished List.\n\n"); 126249259Sdim 127249259Sdim return Changed; 128249259Sdim} 129