LLVM API Documentation

Defines | Enumerations | Functions | Variables
ObjCARCOpts.cpp File Reference
#include "ObjCARC.h"
#include "ARCRuntimeEntryPoints.h"
#include "DependencyAnalysis.h"
#include "ObjCARCAliasAnalysis.h"
#include "ProvenanceAnalysis.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/DenseSet.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/IR/CFG.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/CommandLine.h"
Include dependency graph for ObjCARCOpts.cpp:

Go to the source code of this file.

Defines

#define DEBUG_TYPE   "objc-arc-opts"
#define ARC_ANNOTATIONS
#define ANNOTATE_BOTTOMUP(inst, ptr, old, new)
#define ANNOTATE_TOPDOWN(inst, ptr, old, new)
#define ANNOTATE_BB(_states, _bb, _name, _type, _direction)
#define ANNOTATE_BOTTOMUP_BBSTART(_states, _basicblock)
#define ANNOTATE_BOTTOMUP_BBEND(_states, _basicblock)
#define ANNOTATE_TOPDOWN_BBSTART(_states, _basicblock)
#define ANNOTATE_TOPDOWN_BBEND(_states, _basicblock)

Enumerations

enum  Sequence
 A sequence of states that a pointer may go through in which an objc_retain and objc_release are actually needed. More...

Functions

static const ValueFindSingleUseIdentifiedObject (const Value *Arg)
 This is similar to StripPointerCastsAndObjCCalls but it stops as soon as it finds a value with multiple uses.
static bool AreAnyUnderlyingObjectsAnAlloca (const Value *V)
 STATISTIC (NumNoops,"Number of no-op objc calls eliminated")
 STATISTIC (NumPartialNoops,"Number of partially no-op objc calls eliminated")
 STATISTIC (NumAutoreleases,"Number of autoreleases converted to releases")
 STATISTIC (NumRets,"Number of return value forwarding ""retain+autoreleases eliminated")
 STATISTIC (NumRRs,"Number of retain+release paths eliminated")
 STATISTIC (NumPeeps,"Number of calls peephole-optimized")
 STATISTIC (NumRetainsBeforeOpt,"Number of retains before optimization")
 STATISTIC (NumReleasesBeforeOpt,"Number of releases before optimization")
 STATISTIC (NumRetainsAfterOpt,"Number of retains after optimization")
 STATISTIC (NumReleasesAfterOpt,"Number of releases after optimization")
static Sequence MergeSeqs (Sequence A, Sequence B, bool TopDown)
static MDStringAppendMDNodeToSourcePtr (unsigned NodeId, Value *Ptr)
static std::string SequenceToString (Sequence A)
static MDStringSequenceToMDString (LLVMContext &Context, Sequence A)
static void AppendMDNodeToInstForPtr (unsigned NodeId, Instruction *Inst, Value *Ptr, MDString *PtrSourceMDNodeID, Sequence OldSeq, Sequence NewSeq)
static void GenerateARCBBEntranceAnnotation (const char *Name, BasicBlock *BB, Value *Ptr, Sequence Seq)
static void GenerateARCBBTerminatorAnnotation (const char *Name, BasicBlock *BB, Value *Ptr, Sequence Seq)
static void GenerateARCAnnotation (unsigned InstMDId, unsigned PtrMDId, Instruction *Inst, Value *Ptr, Sequence OldSeq, Sequence NewSeq)
 INITIALIZE_PASS_BEGIN (ObjCARCOpt,"objc-arc","ObjC ARC optimization", false, false) INITIALIZE_PASS_END(ObjCARCOpt
static void CheckForUseCFGHazard (const Sequence SuccSSeq, const bool SuccSRRIKnownSafe, PtrState &S, bool &SomeSuccHasSame, bool &AllSuccsHaveSame, bool &NotAllSeqEqualButKnownSafe, bool &ShouldContinue)
static void CheckForCanReleaseCFGHazard (const Sequence SuccSSeq, const bool SuccSRRIKnownSafe, PtrState &S, bool &SomeSuccHasSame, bool &AllSuccsHaveSame, bool &NotAllSeqEqualButKnownSafe)
static void ComputePostOrders (Function &F, SmallVectorImpl< BasicBlock * > &PostOrder, SmallVectorImpl< BasicBlock * > &ReverseCFGPostOrder, unsigned NoObjCARCExceptionsMDKind, DenseMap< const BasicBlock *, BBState > &BBStates)
static bool HasSafePathToPredecessorCall (const Value *Arg, Instruction *Retain, SmallPtrSetImpl< Instruction * > &DepInsts, SmallPtrSetImpl< const BasicBlock * > &Visited, ProvenanceAnalysis &PA)
static CallInstFindPredecessorRetainWithSafePath (const Value *Arg, BasicBlock *BB, Instruction *Autorelease, SmallPtrSetImpl< Instruction * > &DepInsts, SmallPtrSetImpl< const BasicBlock * > &Visited, ProvenanceAnalysis &PA)
static CallInstFindPredecessorAutoreleaseWithSafePath (const Value *Arg, BasicBlock *BB, ReturnInst *Ret, SmallPtrSetImpl< Instruction * > &DepInsts, SmallPtrSetImpl< const BasicBlock * > &V, ProvenanceAnalysis &PA)

Variables

static cl::opt< boolEnableARCAnnotations ("enable-objc-arc-annotations", cl::init(false), cl::desc("Enable emission of arc data flow analysis ""annotations"))
 Enable/disable ARC sequence annotations.
static cl::opt< boolDisableCheckForCFGHazards ("disable-objc-arc-checkforcfghazards", cl::init(false), cl::desc("Disable check for cfg hazards when ""annotating"))
static cl::opt< std::string > ARCAnnotationTargetIdentifier ("objc-arc-annotation-target-identifier", cl::init(""), cl::desc("filter out all data flow annotations ""but those that apply to the given ""target llvm identifier."))
objc arc
objc ObjC ARC optimization
objc ObjC ARC false

Detailed Description

This file defines ObjC ARC optimizations. ARC stands for Automatic Reference Counting and is a system for managing reference counts for objects in Objective C.

The optimizations performed include elimination of redundant, partially redundant, and inconsequential reference count operations, elimination of redundant weak pointer operations, and numerous minor simplifications.

WARNING: This file knows about certain library functions. It recognizes them by name, and hardwires knowledge of their semantics.

WARNING: This file knows about how certain Objective-C library functions are used. Naive LLVM IR transformations which would otherwise be behavior-preserving may break these assumptions.

Definition in file ObjCARCOpts.cpp.


Define Documentation

#define ANNOTATE_BB (   _states,
  _bb,
  _name,
  _type,
  _direction 
)
Value:
do {                                                                        \
    if (EnableARCAnnotations) {                                               \
      for(BBState::ptr_const_iterator I = (_states)._direction##_ptr_begin(), \
          E = (_states)._direction##_ptr_end(); I != E; ++I) {                \
        Value *Ptr = const_cast<Value*>(I->first);                            \
        Sequence Seq = I->second.GetSeq();                                    \
        GenerateARCBB ## _type ## Annotation(_name, (_bb), Ptr, Seq);         \
      }                                                                       \
    }                                                                         \
  } while (0)

Definition at line 1023 of file ObjCARCOpts.cpp.

#define ANNOTATE_BOTTOMUP (   inst,
  ptr,
  old,
  new 
)
Value:
GenerateARCAnnotation(ARCAnnotationBottomUpMDKind,                    \
                        ARCAnnotationProvenanceSourceMDKind, (inst),    \
                        const_cast<Value*>(ptr), (old), (new))

Use this macro to annotate a sequence state change when processing instructions bottom up,

Definition at line 1012 of file ObjCARCOpts.cpp.

#define ANNOTATE_BOTTOMUP_BBEND (   _states,
  _basicblock 
)
Value:
ANNOTATE_BB(_states, _basicblock, "llvm.arc.annotation.bottomup.bbend",   \
                Terminator, bottom_up)

Definition at line 1038 of file ObjCARCOpts.cpp.

#define ANNOTATE_BOTTOMUP_BBSTART (   _states,
  _basicblock 
)
Value:
ANNOTATE_BB(_states, _basicblock, "llvm.arc.annotation.bottomup.bbstart", \
                Entrance, bottom_up)

Definition at line 1035 of file ObjCARCOpts.cpp.

#define ANNOTATE_TOPDOWN (   inst,
  ptr,
  old,
  new 
)
Value:
GenerateARCAnnotation(ARCAnnotationTopDownMDKind,                     \
                        ARCAnnotationProvenanceSourceMDKind, (inst),    \
                        const_cast<Value*>(ptr), (old), (new))

Use this macro to annotate a sequence state change when processing instructions top down.

Definition at line 1018 of file ObjCARCOpts.cpp.

#define ANNOTATE_TOPDOWN_BBEND (   _states,
  _basicblock 
)
Value:
ANNOTATE_BB(_states, _basicblock, "llvm.arc.annotation.topdown.bbend",    \
                Terminator, top_down)

Definition at line 1044 of file ObjCARCOpts.cpp.

#define ANNOTATE_TOPDOWN_BBSTART (   _states,
  _basicblock 
)
Value:
ANNOTATE_BB(_states, _basicblock, "llvm.arc.annotation.topdown.bbstart",  \
                Entrance, top_down)

Definition at line 1041 of file ObjCARCOpts.cpp.

#define ARC_ANNOTATIONS

Definition at line 783 of file ObjCARCOpts.cpp.

#define DEBUG_TYPE   "objc-arc-opts"

Definition at line 46 of file ObjCARCOpts.cpp.


Enumeration Type Documentation

enum Sequence

A sequence of states that a pointer may go through in which an objc_retain and objc_release are actually needed.

Definition at line 271 of file ObjCARCOpts.cpp.


Function Documentation

static void AppendMDNodeToInstForPtr ( unsigned  NodeId,
Instruction Inst,
Value Ptr,
MDString PtrSourceMDNodeID,
Sequence  OldSeq,
Sequence  NewSeq 
) [static]

A simple function to generate a MDNode which describes the change in state for Value *Ptr caused by Instruction *Inst.

Definition at line 870 of file ObjCARCOpts.cpp.

References llvm::MDString::get(), and SequenceToString().

static MDString* AppendMDNodeToSourcePtr ( unsigned  NodeId,
Value Ptr 
) [static]

This function appends a unique ARCAnnotationProvenanceSourceMDKind id to an instruction so that we can track backwards when post processing via the llvm arc annotation processor tool. If the function is an

Definition at line 812 of file ObjCARCOpts.cpp.

static bool AreAnyUnderlyingObjectsAnAlloca ( const Value V) [inline, static]

This is a wrapper around getUnderlyingObjCPtr along the lines of GetUnderlyingObjects except that it returns early when it sees the first alloca.

Definition at line 177 of file ObjCARCOpts.cpp.

References llvm::SmallVectorBase::empty(), llvm::objcarc::GetUnderlyingObjCPtr(), llvm::SmallPtrSetImpl< PtrType >::insert(), P, llvm::SmallVectorImpl< T >::pop_back_val(), and llvm::SmallVectorTemplateBase< T, isPodLike >::push_back().

static void CheckForCanReleaseCFGHazard ( const Sequence  SuccSSeq,
const bool  SuccSRRIKnownSafe,
PtrState &  S,
bool SomeSuccHasSame,
bool AllSuccsHaveSame,
bool NotAllSeqEqualButKnownSafe 
) [static]

If we have a Top Down pointer in the S_CanRelease state, make sure that there are no CFG hazards by checking the states of various bottom up pointers.

Definition at line 1582 of file ObjCARCOpts.cpp.

static void CheckForUseCFGHazard ( const Sequence  SuccSSeq,
const bool  SuccSRRIKnownSafe,
PtrState &  S,
bool SomeSuccHasSame,
bool AllSuccsHaveSame,
bool NotAllSeqEqualButKnownSafe,
bool ShouldContinue 
) [static]

If we have a top down pointer in the S_Use state, make sure that there are no CFG hazards by checking the states of various bottom up pointers.

Definition at line 1544 of file ObjCARCOpts.cpp.

static void ComputePostOrders ( Function F,
SmallVectorImpl< BasicBlock * > &  PostOrder,
SmallVectorImpl< BasicBlock * > &  ReverseCFGPostOrder,
unsigned  NoObjCARCExceptionsMDKind,
DenseMap< const BasicBlock *, BBState > &  BBStates 
) [static]

The visited set, for doing DFS walks.

Definition at line 2162 of file ObjCARCOpts.cpp.

static CallInst* FindPredecessorAutoreleaseWithSafePath ( const Value Arg,
BasicBlock BB,
ReturnInst Ret,
SmallPtrSetImpl< Instruction * > &  DepInsts,
SmallPtrSetImpl< const BasicBlock * > &  V,
ProvenanceAnalysis PA 
) [static]

Look for an ``autorelease'' instruction dependent on Arg such that there are no instructions dependent on Arg that need a positive ref count in between the autorelease and the ret.

Definition at line 2870 of file ObjCARCOpts.cpp.

static CallInst* FindPredecessorRetainWithSafePath ( const Value Arg,
BasicBlock BB,
Instruction Autorelease,
SmallPtrSetImpl< Instruction * > &  DepInsts,
SmallPtrSetImpl< const BasicBlock * > &  Visited,
ProvenanceAnalysis PA 
) [static]

Find a dependent retain that precedes the given autorelease for which there is nothing in between the two instructions that can affect the ref count of Arg.

Definition at line 2843 of file ObjCARCOpts.cpp.

static const Value* FindSingleUseIdentifiedObject ( const Value Arg) [static]

This is similar to StripPointerCastsAndObjCCalls but it stops as soon as it finds a value with multiple uses.

Definition at line 146 of file ObjCARCOpts.cpp.

References llvm::objcarc::GetBasicInstructionClass(), llvm::Value::hasOneUse(), llvm::objcarc::IsForwarding(), llvm::objcarc::IsObjCIdentifiedObject(), llvm::objcarc::StripPointerCastsAndObjCCalls(), and llvm::Value::users().

static void GenerateARCAnnotation ( unsigned  InstMDId,
unsigned  PtrMDId,
Instruction Inst,
Value Ptr,
Sequence  OldSeq,
Sequence  NewSeq 
) [static]

Adds a source annotation to pointer and a state change annotation to Inst referencing the source annotation and the old/new state of pointer.

Definition at line 972 of file ObjCARCOpts.cpp.

static void GenerateARCBBEntranceAnnotation ( const char *  Name,
BasicBlock BB,
Value Ptr,
Sequence  Seq 
) [static]

Add to the beginning of the basic block llvm.ptr.annotations which show the state of a pointer at the entrance to a basic block.

Definition at line 889 of file ObjCARCOpts.cpp.

static void GenerateARCBBTerminatorAnnotation ( const char *  Name,
BasicBlock BB,
Value Ptr,
Sequence  Seq 
) [static]

Add to the end of the basic block llvm.ptr.annotations which show the state of the pointer at the bottom of the basic block.

Definition at line 931 of file ObjCARCOpts.cpp.

static bool HasSafePathToPredecessorCall ( const Value Arg,
Instruction Retain,
SmallPtrSetImpl< Instruction * > &  DepInsts,
SmallPtrSetImpl< const BasicBlock * > &  Visited,
ProvenanceAnalysis PA 
) [static]

Check if there is a dependent call earlier that does not have anything in between the Retain and the call that can affect the reference count of their shared pointer argument. Note that Retain need not be in BB.

Definition at line 2815 of file ObjCARCOpts.cpp.

INITIALIZE_PASS_BEGIN ( ObjCARCOpt  ,
"objc-arc ,
"ObjC ARC optimization ,
false  ,
false   
)
static Sequence MergeSeqs ( Sequence  A,
Sequence  B,
bool  TopDown 
) [static]

Definition at line 304 of file ObjCARCOpts.cpp.

static MDString* SequenceToMDString ( LLVMContext Context,
Sequence  A 
) [static]

Helper function to change a Sequence into a String object using our overload for raw_ostream so we only have printing code in one location.

Definition at line 863 of file ObjCARCOpts.cpp.

static std::string SequenceToString ( Sequence  A) [static]

Definition at line 854 of file ObjCARCOpts.cpp.

Referenced by AppendMDNodeToInstForPtr().

STATISTIC ( NumNoops  ,
"Number of no-op objc calls eliminated"   
)
STATISTIC ( NumPartialNoops  ,
"Number of partially no-op objc calls eliminated"   
)
STATISTIC ( NumAutoreleases  ,
"Number of autoreleases converted to releases"   
)
STATISTIC ( NumRets  ,
"Number of return value forwarding ""retain+autoreleases eliminated"   
)
STATISTIC ( NumRRs  ,
"Number of retain+release paths eliminated"   
)
STATISTIC ( NumPeeps  ,
"Number of calls peephole-optimized"   
)
STATISTIC ( NumRetainsBeforeOpt  ,
"Number of retains before optimization  
)
STATISTIC ( NumReleasesBeforeOpt  ,
"Number of releases before optimization  
)
STATISTIC ( NumRetainsAfterOpt  ,
"Number of retains after optimization  
)
STATISTIC ( NumReleasesAfterOpt  ,
"Number of releases after optimization  
)

Variable Documentation

objc arc

Definition at line 1171 of file ObjCARCOpts.cpp.

cl::opt<std::string> ARCAnnotationTargetIdentifier("objc-arc-annotation-target-identifier", cl::init(""), cl::desc("filter out all data flow annotations ""but those that apply to the given ""target llvm identifier.")) [static]
cl::opt<bool> DisableCheckForCFGHazards("disable-objc-arc-checkforcfghazards", cl::init(false), cl::desc("Disable check for cfg hazards when ""annotating")) [static]
cl::opt<bool> EnableARCAnnotations("enable-objc-arc-annotations", cl::init(false), cl::desc("Enable emission of arc data flow analysis ""annotations")) [static]

Enable/disable ARC sequence annotations.

objc ObjC ARC false

Definition at line 1171 of file ObjCARCOpts.cpp.

objc ObjC ARC optimization

Definition at line 1171 of file ObjCARCOpts.cpp.