LLVM API Documentation
00001 //===- ObjCARCExpand.cpp - ObjC ARC Optimization --------------------------===// 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 ObjC ARC optimizations. ARC stands for Automatic 00011 /// Reference Counting and is a system for managing reference counts for objects 00012 /// in Objective C. 00013 /// 00014 /// This specific file deals with early optimizations which perform certain 00015 /// cleanup operations. 00016 /// 00017 /// WARNING: This file knows about certain library functions. It recognizes them 00018 /// by name, and hardwires knowledge of their semantics. 00019 /// 00020 /// WARNING: This file knows about how certain Objective-C library functions are 00021 /// used. Naive LLVM IR transformations which would otherwise be 00022 /// behavior-preserving may break these assumptions. 00023 /// 00024 //===----------------------------------------------------------------------===// 00025 00026 #include "ObjCARC.h" 00027 #include "llvm/ADT/StringRef.h" 00028 #include "llvm/IR/Function.h" 00029 #include "llvm/IR/InstIterator.h" 00030 #include "llvm/IR/Instruction.h" 00031 #include "llvm/IR/Instructions.h" 00032 #include "llvm/IR/Value.h" 00033 #include "llvm/Pass.h" 00034 #include "llvm/PassAnalysisSupport.h" 00035 #include "llvm/PassRegistry.h" 00036 #include "llvm/PassSupport.h" 00037 #include "llvm/Support/Casting.h" 00038 #include "llvm/Support/Debug.h" 00039 #include "llvm/Support/raw_ostream.h" 00040 00041 #define DEBUG_TYPE "objc-arc-expand" 00042 00043 namespace llvm { 00044 class Module; 00045 } 00046 00047 using namespace llvm; 00048 using namespace llvm::objcarc; 00049 00050 namespace { 00051 /// \brief Early ARC transformations. 00052 class ObjCARCExpand : public FunctionPass { 00053 void getAnalysisUsage(AnalysisUsage &AU) const override; 00054 bool doInitialization(Module &M) override; 00055 bool runOnFunction(Function &F) override; 00056 00057 /// A flag indicating whether this optimization pass should run. 00058 bool Run; 00059 00060 public: 00061 static char ID; 00062 ObjCARCExpand() : FunctionPass(ID) { 00063 initializeObjCARCExpandPass(*PassRegistry::getPassRegistry()); 00064 } 00065 }; 00066 } 00067 00068 char ObjCARCExpand::ID = 0; 00069 INITIALIZE_PASS(ObjCARCExpand, 00070 "objc-arc-expand", "ObjC ARC expansion", false, false) 00071 00072 Pass *llvm::createObjCARCExpandPass() { 00073 return new ObjCARCExpand(); 00074 } 00075 00076 void ObjCARCExpand::getAnalysisUsage(AnalysisUsage &AU) const { 00077 AU.setPreservesCFG(); 00078 } 00079 00080 bool ObjCARCExpand::doInitialization(Module &M) { 00081 Run = ModuleHasARC(M); 00082 return false; 00083 } 00084 00085 bool ObjCARCExpand::runOnFunction(Function &F) { 00086 if (!EnableARCOpts) 00087 return false; 00088 00089 // If nothing in the Module uses ARC, don't do anything. 00090 if (!Run) 00091 return false; 00092 00093 bool Changed = false; 00094 00095 DEBUG(dbgs() << "ObjCARCExpand: Visiting Function: " << F.getName() << "\n"); 00096 00097 for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ++I) { 00098 Instruction *Inst = &*I; 00099 00100 DEBUG(dbgs() << "ObjCARCExpand: Visiting: " << *Inst << "\n"); 00101 00102 switch (GetBasicInstructionClass(Inst)) { 00103 case IC_Retain: 00104 case IC_RetainRV: 00105 case IC_Autorelease: 00106 case IC_AutoreleaseRV: 00107 case IC_FusedRetainAutorelease: 00108 case IC_FusedRetainAutoreleaseRV: { 00109 // These calls return their argument verbatim, as a low-level 00110 // optimization. However, this makes high-level optimizations 00111 // harder. Undo any uses of this optimization that the front-end 00112 // emitted here. We'll redo them in the contract pass. 00113 Changed = true; 00114 Value *Value = cast<CallInst>(Inst)->getArgOperand(0); 00115 DEBUG(dbgs() << "ObjCARCExpand: Old = " << *Inst << "\n" 00116 " New = " << *Value << "\n"); 00117 Inst->replaceAllUsesWith(Value); 00118 break; 00119 } 00120 default: 00121 break; 00122 } 00123 } 00124 00125 DEBUG(dbgs() << "ObjCARCExpand: Finished List.\n\n"); 00126 00127 return Changed; 00128 }