LLVM API Documentation
00001 //===- ObjCARCUtil.cpp - ObjC ARC Optimization --------*- mode: 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 several utility functions used by various ARC 00011 /// optimizations which are IMHO too big to be in a header file. 00012 /// 00013 /// WARNING: This file knows about certain library functions. It recognizes them 00014 /// by name, and hardwires knowledge of their semantics. 00015 /// 00016 /// WARNING: This file knows about how certain Objective-C library functions are 00017 /// used. Naive LLVM IR transformations which would otherwise be 00018 /// behavior-preserving may break these assumptions. 00019 /// 00020 //===----------------------------------------------------------------------===// 00021 00022 #include "ObjCARC.h" 00023 #include "llvm/IR/Intrinsics.h" 00024 00025 using namespace llvm; 00026 using namespace llvm::objcarc; 00027 00028 raw_ostream &llvm::objcarc::operator<<(raw_ostream &OS, 00029 const InstructionClass Class) { 00030 switch (Class) { 00031 case IC_Retain: 00032 return OS << "IC_Retain"; 00033 case IC_RetainRV: 00034 return OS << "IC_RetainRV"; 00035 case IC_RetainBlock: 00036 return OS << "IC_RetainBlock"; 00037 case IC_Release: 00038 return OS << "IC_Release"; 00039 case IC_Autorelease: 00040 return OS << "IC_Autorelease"; 00041 case IC_AutoreleaseRV: 00042 return OS << "IC_AutoreleaseRV"; 00043 case IC_AutoreleasepoolPush: 00044 return OS << "IC_AutoreleasepoolPush"; 00045 case IC_AutoreleasepoolPop: 00046 return OS << "IC_AutoreleasepoolPop"; 00047 case IC_NoopCast: 00048 return OS << "IC_NoopCast"; 00049 case IC_FusedRetainAutorelease: 00050 return OS << "IC_FusedRetainAutorelease"; 00051 case IC_FusedRetainAutoreleaseRV: 00052 return OS << "IC_FusedRetainAutoreleaseRV"; 00053 case IC_LoadWeakRetained: 00054 return OS << "IC_LoadWeakRetained"; 00055 case IC_StoreWeak: 00056 return OS << "IC_StoreWeak"; 00057 case IC_InitWeak: 00058 return OS << "IC_InitWeak"; 00059 case IC_LoadWeak: 00060 return OS << "IC_LoadWeak"; 00061 case IC_MoveWeak: 00062 return OS << "IC_MoveWeak"; 00063 case IC_CopyWeak: 00064 return OS << "IC_CopyWeak"; 00065 case IC_DestroyWeak: 00066 return OS << "IC_DestroyWeak"; 00067 case IC_StoreStrong: 00068 return OS << "IC_StoreStrong"; 00069 case IC_CallOrUser: 00070 return OS << "IC_CallOrUser"; 00071 case IC_Call: 00072 return OS << "IC_Call"; 00073 case IC_User: 00074 return OS << "IC_User"; 00075 case IC_IntrinsicUser: 00076 return OS << "IC_IntrinsicUser"; 00077 case IC_None: 00078 return OS << "IC_None"; 00079 } 00080 llvm_unreachable("Unknown instruction class!"); 00081 } 00082 00083 InstructionClass llvm::objcarc::GetFunctionClass(const Function *F) { 00084 Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end(); 00085 00086 // No (mandatory) arguments. 00087 if (AI == AE) 00088 return StringSwitch<InstructionClass>(F->getName()) 00089 .Case("objc_autoreleasePoolPush", IC_AutoreleasepoolPush) 00090 .Case("clang.arc.use", IC_IntrinsicUser) 00091 .Default(IC_CallOrUser); 00092 00093 // One argument. 00094 const Argument *A0 = AI++; 00095 if (AI == AE) 00096 // Argument is a pointer. 00097 if (PointerType *PTy = dyn_cast<PointerType>(A0->getType())) { 00098 Type *ETy = PTy->getElementType(); 00099 // Argument is i8*. 00100 if (ETy->isIntegerTy(8)) 00101 return StringSwitch<InstructionClass>(F->getName()) 00102 .Case("objc_retain", IC_Retain) 00103 .Case("objc_retainAutoreleasedReturnValue", IC_RetainRV) 00104 .Case("objc_retainBlock", IC_RetainBlock) 00105 .Case("objc_release", IC_Release) 00106 .Case("objc_autorelease", IC_Autorelease) 00107 .Case("objc_autoreleaseReturnValue", IC_AutoreleaseRV) 00108 .Case("objc_autoreleasePoolPop", IC_AutoreleasepoolPop) 00109 .Case("objc_retainedObject", IC_NoopCast) 00110 .Case("objc_unretainedObject", IC_NoopCast) 00111 .Case("objc_unretainedPointer", IC_NoopCast) 00112 .Case("objc_retain_autorelease", IC_FusedRetainAutorelease) 00113 .Case("objc_retainAutorelease", IC_FusedRetainAutorelease) 00114 .Case("objc_retainAutoreleaseReturnValue",IC_FusedRetainAutoreleaseRV) 00115 .Default(IC_CallOrUser); 00116 00117 // Argument is i8** 00118 if (PointerType *Pte = dyn_cast<PointerType>(ETy)) 00119 if (Pte->getElementType()->isIntegerTy(8)) 00120 return StringSwitch<InstructionClass>(F->getName()) 00121 .Case("objc_loadWeakRetained", IC_LoadWeakRetained) 00122 .Case("objc_loadWeak", IC_LoadWeak) 00123 .Case("objc_destroyWeak", IC_DestroyWeak) 00124 .Default(IC_CallOrUser); 00125 } 00126 00127 // Two arguments, first is i8**. 00128 const Argument *A1 = AI++; 00129 if (AI == AE) 00130 if (PointerType *PTy = dyn_cast<PointerType>(A0->getType())) 00131 if (PointerType *Pte = dyn_cast<PointerType>(PTy->getElementType())) 00132 if (Pte->getElementType()->isIntegerTy(8)) 00133 if (PointerType *PTy1 = dyn_cast<PointerType>(A1->getType())) { 00134 Type *ETy1 = PTy1->getElementType(); 00135 // Second argument is i8* 00136 if (ETy1->isIntegerTy(8)) 00137 return StringSwitch<InstructionClass>(F->getName()) 00138 .Case("objc_storeWeak", IC_StoreWeak) 00139 .Case("objc_initWeak", IC_InitWeak) 00140 .Case("objc_storeStrong", IC_StoreStrong) 00141 .Default(IC_CallOrUser); 00142 // Second argument is i8**. 00143 if (PointerType *Pte1 = dyn_cast<PointerType>(ETy1)) 00144 if (Pte1->getElementType()->isIntegerTy(8)) 00145 return StringSwitch<InstructionClass>(F->getName()) 00146 .Case("objc_moveWeak", IC_MoveWeak) 00147 .Case("objc_copyWeak", IC_CopyWeak) 00148 // Ignore annotation calls. This is important to stop the 00149 // optimizer from treating annotations as uses which would 00150 // make the state of the pointers they are attempting to 00151 // elucidate to be incorrect. 00152 .Case("llvm.arc.annotation.topdown.bbstart", IC_None) 00153 .Case("llvm.arc.annotation.topdown.bbend", IC_None) 00154 .Case("llvm.arc.annotation.bottomup.bbstart", IC_None) 00155 .Case("llvm.arc.annotation.bottomup.bbend", IC_None) 00156 .Default(IC_CallOrUser); 00157 } 00158 00159 // Anything else. 00160 return IC_CallOrUser; 00161 } 00162 00163 /// \brief Determine what kind of construct V is. 00164 InstructionClass 00165 llvm::objcarc::GetInstructionClass(const Value *V) { 00166 if (const Instruction *I = dyn_cast<Instruction>(V)) { 00167 // Any instruction other than bitcast and gep with a pointer operand have a 00168 // use of an objc pointer. Bitcasts, GEPs, Selects, PHIs transfer a pointer 00169 // to a subsequent use, rather than using it themselves, in this sense. 00170 // As a short cut, several other opcodes are known to have no pointer 00171 // operands of interest. And ret is never followed by a release, so it's 00172 // not interesting to examine. 00173 switch (I->getOpcode()) { 00174 case Instruction::Call: { 00175 const CallInst *CI = cast<CallInst>(I); 00176 // Check for calls to special functions. 00177 if (const Function *F = CI->getCalledFunction()) { 00178 InstructionClass Class = GetFunctionClass(F); 00179 if (Class != IC_CallOrUser) 00180 return Class; 00181 00182 // None of the intrinsic functions do objc_release. For intrinsics, the 00183 // only question is whether or not they may be users. 00184 switch (F->getIntrinsicID()) { 00185 case Intrinsic::returnaddress: case Intrinsic::frameaddress: 00186 case Intrinsic::stacksave: case Intrinsic::stackrestore: 00187 case Intrinsic::vastart: case Intrinsic::vacopy: case Intrinsic::vaend: 00188 case Intrinsic::objectsize: case Intrinsic::prefetch: 00189 case Intrinsic::stackprotector: 00190 case Intrinsic::eh_return_i32: case Intrinsic::eh_return_i64: 00191 case Intrinsic::eh_typeid_for: case Intrinsic::eh_dwarf_cfa: 00192 case Intrinsic::eh_sjlj_lsda: case Intrinsic::eh_sjlj_functioncontext: 00193 case Intrinsic::init_trampoline: case Intrinsic::adjust_trampoline: 00194 case Intrinsic::lifetime_start: case Intrinsic::lifetime_end: 00195 case Intrinsic::invariant_start: case Intrinsic::invariant_end: 00196 // Don't let dbg info affect our results. 00197 case Intrinsic::dbg_declare: case Intrinsic::dbg_value: 00198 // Short cut: Some intrinsics obviously don't use ObjC pointers. 00199 return IC_None; 00200 default: 00201 break; 00202 } 00203 } 00204 return GetCallSiteClass(CI); 00205 } 00206 case Instruction::Invoke: 00207 return GetCallSiteClass(cast<InvokeInst>(I)); 00208 case Instruction::BitCast: 00209 case Instruction::GetElementPtr: 00210 case Instruction::Select: case Instruction::PHI: 00211 case Instruction::Ret: case Instruction::Br: 00212 case Instruction::Switch: case Instruction::IndirectBr: 00213 case Instruction::Alloca: case Instruction::VAArg: 00214 case Instruction::Add: case Instruction::FAdd: 00215 case Instruction::Sub: case Instruction::FSub: 00216 case Instruction::Mul: case Instruction::FMul: 00217 case Instruction::SDiv: case Instruction::UDiv: case Instruction::FDiv: 00218 case Instruction::SRem: case Instruction::URem: case Instruction::FRem: 00219 case Instruction::Shl: case Instruction::LShr: case Instruction::AShr: 00220 case Instruction::And: case Instruction::Or: case Instruction::Xor: 00221 case Instruction::SExt: case Instruction::ZExt: case Instruction::Trunc: 00222 case Instruction::IntToPtr: case Instruction::FCmp: 00223 case Instruction::FPTrunc: case Instruction::FPExt: 00224 case Instruction::FPToUI: case Instruction::FPToSI: 00225 case Instruction::UIToFP: case Instruction::SIToFP: 00226 case Instruction::InsertElement: case Instruction::ExtractElement: 00227 case Instruction::ShuffleVector: 00228 case Instruction::ExtractValue: 00229 break; 00230 case Instruction::ICmp: 00231 // Comparing a pointer with null, or any other constant, isn't an 00232 // interesting use, because we don't care what the pointer points to, or 00233 // about the values of any other dynamic reference-counted pointers. 00234 if (IsPotentialRetainableObjPtr(I->getOperand(1))) 00235 return IC_User; 00236 break; 00237 default: 00238 // For anything else, check all the operands. 00239 // Note that this includes both operands of a Store: while the first 00240 // operand isn't actually being dereferenced, it is being stored to 00241 // memory where we can no longer track who might read it and dereference 00242 // it, so we have to consider it potentially used. 00243 for (User::const_op_iterator OI = I->op_begin(), OE = I->op_end(); 00244 OI != OE; ++OI) 00245 if (IsPotentialRetainableObjPtr(*OI)) 00246 return IC_User; 00247 } 00248 } 00249 00250 // Otherwise, it's totally inert for ARC purposes. 00251 return IC_None; 00252 }