LLVM API Documentation

ObjCARCUtil.cpp
Go to the documentation of this file.
00001 //===- ObjCARCUtil.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 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           .Case("objc_sync_enter", IC_User)
00116           .Case("objc_sync_exit", IC_User)
00117           .Default(IC_CallOrUser);
00118 
00119       // Argument is i8**
00120       if (PointerType *Pte = dyn_cast<PointerType>(ETy))
00121         if (Pte->getElementType()->isIntegerTy(8))
00122           return StringSwitch<InstructionClass>(F->getName())
00123             .Case("objc_loadWeakRetained",      IC_LoadWeakRetained)
00124             .Case("objc_loadWeak",              IC_LoadWeak)
00125             .Case("objc_destroyWeak",           IC_DestroyWeak)
00126             .Default(IC_CallOrUser);
00127     }
00128 
00129   // Two arguments, first is i8**.
00130   const Argument *A1 = AI++;
00131   if (AI == AE)
00132     if (PointerType *PTy = dyn_cast<PointerType>(A0->getType()))
00133       if (PointerType *Pte = dyn_cast<PointerType>(PTy->getElementType()))
00134         if (Pte->getElementType()->isIntegerTy(8))
00135           if (PointerType *PTy1 = dyn_cast<PointerType>(A1->getType())) {
00136             Type *ETy1 = PTy1->getElementType();
00137             // Second argument is i8*
00138             if (ETy1->isIntegerTy(8))
00139               return StringSwitch<InstructionClass>(F->getName())
00140                 .Case("objc_storeWeak",             IC_StoreWeak)
00141                 .Case("objc_initWeak",              IC_InitWeak)
00142                 .Case("objc_storeStrong",           IC_StoreStrong)
00143                 .Default(IC_CallOrUser);
00144             // Second argument is i8**.
00145             if (PointerType *Pte1 = dyn_cast<PointerType>(ETy1))
00146               if (Pte1->getElementType()->isIntegerTy(8))
00147                 return StringSwitch<InstructionClass>(F->getName())
00148                   .Case("objc_moveWeak",              IC_MoveWeak)
00149                   .Case("objc_copyWeak",              IC_CopyWeak)
00150                   // Ignore annotation calls. This is important to stop the
00151                   // optimizer from treating annotations as uses which would
00152                   // make the state of the pointers they are attempting to
00153                   // elucidate to be incorrect.
00154                   .Case("llvm.arc.annotation.topdown.bbstart", IC_None)
00155                   .Case("llvm.arc.annotation.topdown.bbend", IC_None)
00156                   .Case("llvm.arc.annotation.bottomup.bbstart", IC_None)
00157                   .Case("llvm.arc.annotation.bottomup.bbend", IC_None)
00158                   .Default(IC_CallOrUser);
00159           }
00160 
00161   // Anything else.
00162   return IC_CallOrUser;
00163 }
00164 
00165 /// \brief Determine what kind of construct V is.
00166 InstructionClass
00167 llvm::objcarc::GetInstructionClass(const Value *V) {
00168   if (const Instruction *I = dyn_cast<Instruction>(V)) {
00169     // Any instruction other than bitcast and gep with a pointer operand have a
00170     // use of an objc pointer. Bitcasts, GEPs, Selects, PHIs transfer a pointer
00171     // to a subsequent use, rather than using it themselves, in this sense.
00172     // As a short cut, several other opcodes are known to have no pointer
00173     // operands of interest. And ret is never followed by a release, so it's
00174     // not interesting to examine.
00175     switch (I->getOpcode()) {
00176     case Instruction::Call: {
00177       const CallInst *CI = cast<CallInst>(I);
00178       // Check for calls to special functions.
00179       if (const Function *F = CI->getCalledFunction()) {
00180         InstructionClass Class = GetFunctionClass(F);
00181         if (Class != IC_CallOrUser)
00182           return Class;
00183 
00184         // None of the intrinsic functions do objc_release. For intrinsics, the
00185         // only question is whether or not they may be users.
00186         switch (F->getIntrinsicID()) {
00187         case Intrinsic::returnaddress: case Intrinsic::frameaddress:
00188         case Intrinsic::stacksave: case Intrinsic::stackrestore:
00189         case Intrinsic::vastart: case Intrinsic::vacopy: case Intrinsic::vaend:
00190         case Intrinsic::objectsize: case Intrinsic::prefetch:
00191         case Intrinsic::stackprotector:
00192         case Intrinsic::eh_return_i32: case Intrinsic::eh_return_i64:
00193         case Intrinsic::eh_typeid_for: case Intrinsic::eh_dwarf_cfa:
00194         case Intrinsic::eh_sjlj_lsda: case Intrinsic::eh_sjlj_functioncontext:
00195         case Intrinsic::init_trampoline: case Intrinsic::adjust_trampoline:
00196         case Intrinsic::lifetime_start: case Intrinsic::lifetime_end:
00197         case Intrinsic::invariant_start: case Intrinsic::invariant_end:
00198         // Don't let dbg info affect our results.
00199         case Intrinsic::dbg_declare: case Intrinsic::dbg_value:
00200           // Short cut: Some intrinsics obviously don't use ObjC pointers.
00201           return IC_None;
00202         default:
00203           break;
00204         }
00205       }
00206       return GetCallSiteClass(CI);
00207     }
00208     case Instruction::Invoke:
00209       return GetCallSiteClass(cast<InvokeInst>(I));
00210     case Instruction::BitCast:
00211     case Instruction::GetElementPtr:
00212     case Instruction::Select: case Instruction::PHI:
00213     case Instruction::Ret: case Instruction::Br:
00214     case Instruction::Switch: case Instruction::IndirectBr:
00215     case Instruction::Alloca: case Instruction::VAArg:
00216     case Instruction::Add: case Instruction::FAdd:
00217     case Instruction::Sub: case Instruction::FSub:
00218     case Instruction::Mul: case Instruction::FMul:
00219     case Instruction::SDiv: case Instruction::UDiv: case Instruction::FDiv:
00220     case Instruction::SRem: case Instruction::URem: case Instruction::FRem:
00221     case Instruction::Shl: case Instruction::LShr: case Instruction::AShr:
00222     case Instruction::And: case Instruction::Or: case Instruction::Xor:
00223     case Instruction::SExt: case Instruction::ZExt: case Instruction::Trunc:
00224     case Instruction::IntToPtr: case Instruction::FCmp:
00225     case Instruction::FPTrunc: case Instruction::FPExt:
00226     case Instruction::FPToUI: case Instruction::FPToSI:
00227     case Instruction::UIToFP: case Instruction::SIToFP:
00228     case Instruction::InsertElement: case Instruction::ExtractElement:
00229     case Instruction::ShuffleVector:
00230     case Instruction::ExtractValue:
00231       break;
00232     case Instruction::ICmp:
00233       // Comparing a pointer with null, or any other constant, isn't an
00234       // interesting use, because we don't care what the pointer points to, or
00235       // about the values of any other dynamic reference-counted pointers.
00236       if (IsPotentialRetainableObjPtr(I->getOperand(1)))
00237         return IC_User;
00238       break;
00239     default:
00240       // For anything else, check all the operands.
00241       // Note that this includes both operands of a Store: while the first
00242       // operand isn't actually being dereferenced, it is being stored to
00243       // memory where we can no longer track who might read it and dereference
00244       // it, so we have to consider it potentially used.
00245       for (User::const_op_iterator OI = I->op_begin(), OE = I->op_end();
00246            OI != OE; ++OI)
00247         if (IsPotentialRetainableObjPtr(*OI))
00248           return IC_User;
00249     }
00250   }
00251 
00252   // Otherwise, it's totally inert for ARC purposes.
00253   return IC_None;
00254 }