LLVM  6.0.0svn
Classes | Namespaces | Macros | Functions | Variables
FunctionAttrs.cpp File Reference

This file implements interprocedural passes which walk the call-graph deducing and/or propagating function attributes. More...

#include "llvm/Transforms/IPO/FunctionAttrs.h"
#include "llvm/ADT/SCCIterator.h"
#include "llvm/ADT/SetVector.h"
#include "llvm/ADT/SmallSet.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/ADT/StringSwitch.h"
#include "llvm/Analysis/AliasAnalysis.h"
#include "llvm/Analysis/AssumptionCache.h"
#include "llvm/Analysis/BasicAliasAnalysis.h"
#include "llvm/Analysis/CallGraph.h"
#include "llvm/Analysis/CallGraphSCCPass.h"
#include "llvm/Analysis/CaptureTracking.h"
#include "llvm/Analysis/TargetLibraryInfo.h"
#include "llvm/Analysis/ValueTracking.h"
#include "llvm/IR/GlobalVariable.h"
#include "llvm/IR/InstIterator.h"
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Transforms/IPO.h"
Include dependency graph for FunctionAttrs.cpp:

Go to the source code of this file.

Classes

struct  llvm::GraphTraits< ArgumentGraphNode * >
 
struct  llvm::GraphTraits< ArgumentGraph * >
 

Namespaces

 llvm
 Compute iterated dominance frontiers using a linear time algorithm.
 

Macros

#define DEBUG_TYPE   "functionattrs"
 

Functions

 STATISTIC (NumReadNone, "Number of functions marked readnone")
 
 STATISTIC (NumReadOnly, "Number of functions marked readonly")
 
 STATISTIC (NumNoCapture, "Number of arguments marked nocapture")
 
 STATISTIC (NumReturned, "Number of arguments marked returned")
 
 STATISTIC (NumReadNoneArg, "Number of arguments marked readnone")
 
 STATISTIC (NumReadOnlyArg, "Number of arguments marked readonly")
 
 STATISTIC (NumNoAlias, "Number of function returns marked noalias")
 
 STATISTIC (NumNonNullReturn, "Number of function returns marked nonnull")
 
 STATISTIC (NumNoRecurse, "Number of functions marked as norecurse")
 
static MemoryAccessKind checkFunctionMemoryAccess (Function &F, bool ThisBody, AAResults &AAR, const SCCNodeSet &SCCNodes)
 Returns the memory access attribute for function F using AAR for AA results, where SCCNodes is the current SCC. More...
 
template<typename AARGetterT >
static bool addReadAttrs (const SCCNodeSet &SCCNodes, AARGetterT &&AARGetter)
 Deduce readonly/readnone attributes for the SCC. More...
 
static Attribute::AttrKind determinePointerReadAttrs (Argument *A, const SmallPtrSet< Argument *, 8 > &SCCNodes)
 Returns Attribute::None, Attribute::ReadOnly or Attribute::ReadNone. More...
 
static bool addArgumentReturnedAttrs (const SCCNodeSet &SCCNodes)
 Deduce returned attributes for the SCC. More...
 
static bool addArgumentAttrsFromCallsites (Function &F)
 If a callsite has arguments that are also arguments to the parent function, try to propagate attributes from the callsite's arguments to the parent's arguments. More...
 
static bool addArgumentAttrs (const SCCNodeSet &SCCNodes)
 Deduce nocapture attributes for the SCC. More...
 
static bool isFunctionMallocLike (Function *F, const SCCNodeSet &SCCNodes)
 Tests whether a function is "malloc-like". More...
 
static bool addNoAliasAttrs (const SCCNodeSet &SCCNodes)
 Deduce noalias attributes for the SCC. More...
 
static bool isReturnNonNull (Function *F, const SCCNodeSet &SCCNodes, bool &Speculative)
 Tests whether this function is known to not return null. More...
 
static bool addNonNullAttrs (const SCCNodeSet &SCCNodes)
 Deduce nonnull attributes for the SCC. More...
 
static bool removeConvergentAttrs (const SCCNodeSet &SCCNodes)
 Remove the convergent attribute from all functions in the SCC if every callsite within the SCC is not convergent (except for calls to functions within the SCC). More...
 
static bool setDoesNotRecurse (Function &F)
 
static bool addNoRecurseAttrs (const SCCNodeSet &SCCNodes)
 
 INITIALIZE_PASS_BEGIN (PostOrderFunctionAttrsLegacyPass, "functionattrs", "Deduce function attributes", false, false) INITIALIZE_PASS_END(PostOrderFunctionAttrsLegacyPass
 
template<typename AARGetterT >
static bool runImpl (CallGraphSCC &SCC, AARGetterT AARGetter)
 
 INITIALIZE_PASS_BEGIN (ReversePostOrderFunctionAttrsLegacyPass, "rpo-functionattrs", "Deduce function attributes in RPO", false, false) INITIALIZE_PASS_END(ReversePostOrderFunctionAttrsLegacyPass
 
static bool addNoRecurseAttrsTopDown (Function &F)
 
static bool deduceFunctionAttributeInRPO (Module &M, CallGraph &CG)
 

Variables

static cl::opt< boolEnableNonnullArgPropagation ("enable-nonnull-arg-prop", cl::Hidden, cl::desc("Try to propagate nonnull argument attributes from callsites to " "caller functions."))
 
 functionattrs
 
Deduce function attributes
 
Deduce function false
 
rpo Deduce function attributes in RPO
 

Detailed Description

This file implements interprocedural passes which walk the call-graph deducing and/or propagating function attributes.

Definition in file FunctionAttrs.cpp.

Macro Definition Documentation

◆ DEBUG_TYPE

#define DEBUG_TYPE   "functionattrs"

Definition at line 39 of file FunctionAttrs.cpp.

Function Documentation

◆ addArgumentAttrs()

static bool addArgumentAttrs ( const SCCNodeSet &  SCCNodes)
static

◆ addArgumentAttrsFromCallsites()

static bool addArgumentAttrsFromCallsites ( Function F)
static

If a callsite has arguments that are also arguments to the parent function, try to propagate attributes from the callsite's arguments to the parent's arguments.

This may be important because inlining can cause information loss when attribute knowledge disappears with the inlined call.

Definition at line 543 of file FunctionAttrs.cpp.

References llvm::dyn_cast(), EnableNonnullArgPropagation, llvm::Function::getEntryBlock(), I, and llvm::isGuaranteedToTransferExecutionToSuccessor().

Referenced by addArgumentAttrs().

◆ addArgumentReturnedAttrs()

static bool addArgumentReturnedAttrs ( const SCCNodeSet &  SCCNodes)
static

Deduce returned attributes for the SCC.

Definition at line 490 of file FunctionAttrs.cpp.

References A, llvm::any_of(), F, llvm::Value::getType(), and llvm::MipsISD::Ret.

Referenced by llvm::PostOrderFunctionAttrsPass::run(), and runImpl().

◆ addNoAliasAttrs()

static bool addNoAliasAttrs ( const SCCNodeSet &  SCCNodes)
static

Deduce noalias attributes for the SCC.

Definition at line 832 of file FunctionAttrs.cpp.

References F, and isFunctionMallocLike().

Referenced by llvm::PostOrderFunctionAttrsPass::run(), and runImpl().

◆ addNonNullAttrs()

static bool addNonNullAttrs ( const SCCNodeSet &  SCCNodes)
static

Deduce nonnull attributes for the SCC.

Definition at line 940 of file FunctionAttrs.cpp.

References llvm::dbgs(), DEBUG, F, isReturnNonNull(), and llvm::AttributeList::ReturnIndex.

Referenced by llvm::PostOrderFunctionAttrsPass::run(), and runImpl().

◆ addNoRecurseAttrs()

static bool addNoRecurseAttrs ( const SCCNodeSet &  SCCNodes)
static

◆ addNoRecurseAttrsTopDown()

static bool addNoRecurseAttrsTopDown ( Function F)
static

◆ addReadAttrs()

template<typename AARGetterT >
static bool addReadAttrs ( const SCCNodeSet &  SCCNodes,
AARGetterT &&  AARGetter 
)
static

◆ checkFunctionMemoryAccess()

static MemoryAccessKind checkFunctionMemoryAccess ( Function F,
bool  ThisBody,
AAResults AAR,
const SCCNodeSet &  SCCNodes 
)
static

Returns the memory access attribute for function F using AAR for AA results, where SCCNodes is the current SCC.

If ThisBody is true, this function may examine the function body and will return a result pertaining to this copy of the function. If it is false, the result will be based only on AA results for the function declaration; it will be assumed that some other (perhaps less optimized) version of the function may be selected at link time.

Definition at line 71 of file FunctionAttrs.cpp.

References llvm::CallSiteBase< FunTy, BBTy, ValTy, UserTy, UseTy, InstrTy, CallTy, InvokeTy, IterTy >::arg_begin(), llvm::CallSiteBase< FunTy, BBTy, ValTy, UserTy, UseTy, InstrTy, CallTy, InvokeTy, IterTy >::arg_end(), E, llvm::FMRB_DoesNotAccessMemory, llvm::MemoryLocation::get(), llvm::Instruction::getAAMetadata(), llvm::CallSiteBase< FunTy, BBTy, ValTy, UserTy, UseTy, InstrTy, CallTy, InvokeTy, IterTy >::getCalledFunction(), llvm::AAResults::getModRefBehavior(), llvm::Value::getType(), llvm::CallSiteBase< FunTy, BBTy, ValTy, UserTy, UseTy, InstrTy, CallTy, InvokeTy, IterTy >::hasOperandBundles(), I, llvm::inst_begin(), llvm::inst_end(), llvm::Type::isPtrOrPtrVectorTy(), llvm::MAK_MayWrite, llvm::MAK_ReadNone, llvm::MAK_ReadOnly, llvm::Instruction::mayReadFromMemory(), llvm::Instruction::mayWriteToMemory(), llvm::MRI_Mod, llvm::MRI_ModRef, llvm::MRI_Ref, llvm::AAResults::onlyAccessesArgPointees(), llvm::AAResults::onlyReadsMemory(), llvm::AAResults::pointsToConstantMemory(), SI, llvm::MemoryLocation::UnknownSize, and VI.

Referenced by addReadAttrs(), and llvm::computeFunctionBodyMemoryAccess().

◆ deduceFunctionAttributeInRPO()

static bool deduceFunctionAttributeInRPO ( Module M,
CallGraph CG 
)
static

◆ determinePointerReadAttrs()

static Attribute::AttrKind determinePointerReadAttrs ( Argument A,
const SmallPtrSet< Argument *, 8 > &  SCCNodes 
)
static

Returns Attribute::None, Attribute::ReadOnly or Attribute::ReadNone.

Definition at line 362 of file FunctionAttrs.cpp.

References llvm::CallSiteBase< FunTy, BBTy, ValTy, UserTy, UseTy, InstrTy, CallTy, InvokeTy, IterTy >::arg_begin(), llvm::Function::arg_begin(), llvm::Function::arg_size(), assert(), llvm::MCID::Call, llvm::SmallPtrSetImpl< PtrType >::count(), llvm::CallSiteBase< FunTy, BBTy, ValTy, UserTy, UseTy, InstrTy, CallTy, InvokeTy, IterTy >::data_operands_size(), llvm::CallSiteBase< FunTy, BBTy, ValTy, UserTy, UseTy, InstrTy, CallTy, InvokeTy, IterTy >::doesNotAccessMemory(), llvm::CallSiteBase< FunTy, BBTy, ValTy, UserTy, UseTy, InstrTy, CallTy, InvokeTy, IterTy >::doesNotCapture(), llvm::SmallVectorBase::empty(), F, llvm::CallSiteBase< FunTy, BBTy, ValTy, UserTy, UseTy, InstrTy, CallTy, InvokeTy, IterTy >::getCalledFunction(), llvm::CallSiteBase< FunTy, BBTy, ValTy, UserTy, UseTy, InstrTy, CallTy, InvokeTy, IterTy >::getNumArgOperands(), llvm::Instruction::getOpcode(), llvm::Value::getType(), llvm::Argument::hasInAllocaAttr(), I, llvm::SmallSet< T, N, C >::insert(), llvm::Function::isVarArg(), llvm::Type::isVoidTy(), isVolatile(), llvm::SPII::Load, llvm::Attribute::None, llvm::CallSiteBase< FunTy, BBTy, ValTy, UserTy, UseTy, InstrTy, CallTy, InvokeTy, IterTy >::onlyReadsMemory(), llvm::SmallVectorImpl< T >::pop_back_val(), llvm::SmallVectorTemplateBase< T, isPodLike< T >::value >::push_back(), llvm::MipsISD::Ret, llvm::MCID::Select, and llvm::Value::uses().

Referenced by addArgumentAttrs().

◆ INITIALIZE_PASS_BEGIN() [1/2]

INITIALIZE_PASS_BEGIN ( PostOrderFunctionAttrsLegacyPass  ,
"functionattrs"  ,
"Deduce function attributes ,
false  ,
false   
)

◆ INITIALIZE_PASS_BEGIN() [2/2]

INITIALIZE_PASS_BEGIN ( ReversePostOrderFunctionAttrsLegacyPass  ,
"rpo-functionattrs ,
"Deduce function attributes in RPO ,
false  ,
false   
)

◆ isFunctionMallocLike()

static bool isFunctionMallocLike ( Function F,
const SCCNodeSet &  SCCNodes 
)
static

◆ isReturnNonNull()

static bool isReturnNonNull ( Function F,
const SCCNodeSet &  SCCNodes,
bool Speculative 
)
static

◆ removeConvergentAttrs()

static bool removeConvergentAttrs ( const SCCNodeSet &  SCCNodes)
static

Remove the convergent attribute from all functions in the SCC if every callsite within the SCC is not convergent (except for calls to functions within the SCC).

Returns true if changes were made.

Definition at line 1003 of file FunctionAttrs.cpp.

References llvm::dbgs(), DEBUG, F, llvm::CallSiteBase< FunTy, BBTy, ValTy, UserTy, UseTy, InstrTy, CallTy, InvokeTy, IterTy >::getCalledFunction(), I, llvm::instructions(), and llvm::CallSiteBase< FunTy, BBTy, ValTy, UserTy, UseTy, InstrTy, CallTy, InvokeTy, IterTy >::isConvergent().

Referenced by llvm::PostOrderFunctionAttrsPass::run(), and runImpl().

◆ runImpl()

template<typename AARGetterT >
static bool runImpl ( CallGraphSCC SCC,
AARGetterT  AARGetter 
)
static

◆ setDoesNotRecurse()

static bool setDoesNotRecurse ( Function F)
static

◆ STATISTIC() [1/9]

STATISTIC ( NumReadNone  ,
"Number of functions marked readnone"   
)

◆ STATISTIC() [2/9]

STATISTIC ( NumReadOnly  ,
"Number of functions marked readonly"   
)

◆ STATISTIC() [3/9]

STATISTIC ( NumNoCapture  ,
"Number of arguments marked nocapture"   
)

◆ STATISTIC() [4/9]

STATISTIC ( NumReturned  ,
"Number of arguments marked returned"   
)

◆ STATISTIC() [5/9]

STATISTIC ( NumReadNoneArg  ,
"Number of arguments marked readnone"   
)

◆ STATISTIC() [6/9]

STATISTIC ( NumReadOnlyArg  ,
"Number of arguments marked readonly"   
)

◆ STATISTIC() [7/9]

STATISTIC ( NumNoAlias  ,
"Number of function returns marked noalias"   
)

◆ STATISTIC() [8/9]

STATISTIC ( NumNonNullReturn  ,
"Number of function returns marked nonnull"   
)

◆ STATISTIC() [9/9]

STATISTIC ( NumNoRecurse  ,
"Number of functions marked as norecurse"   
)

Variable Documentation

◆ attributes

Deduce function attributes

◆ EnableNonnullArgPropagation

cl::opt<bool> EnableNonnullArgPropagation("enable-nonnull-arg-prop", cl::Hidden, cl::desc("Try to propagate nonnull argument attributes from callsites to " "caller functions."))
static

◆ false

rpo Deduce function attributes in false

Definition at line 1161 of file FunctionAttrs.cpp.

◆ functionattrs

rpo functionattrs

Definition at line 1161 of file FunctionAttrs.cpp.

◆ RPO

rpo Deduce function attributes in RPO

Definition at line 1238 of file FunctionAttrs.cpp.

Referenced by isDebug(), isMinSize(), and isPushPop().