|
LLVM
10.0.0
|
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/STLExtras.h"#include "llvm/ADT/SetVector.h"#include "llvm/ADT/SmallPtrSet.h"#include "llvm/ADT/SmallVector.h"#include "llvm/ADT/Statistic.h"#include "llvm/Analysis/AliasAnalysis.h"#include "llvm/Analysis/AssumptionCache.h"#include "llvm/Analysis/BasicAliasAnalysis.h"#include "llvm/Analysis/CGSCCPassManager.h"#include "llvm/Analysis/CallGraph.h"#include "llvm/Analysis/CallGraphSCCPass.h"#include "llvm/Analysis/CaptureTracking.h"#include "llvm/Analysis/LazyCallGraph.h"#include "llvm/Analysis/MemoryBuiltins.h"#include "llvm/Analysis/MemoryLocation.h"#include "llvm/Analysis/ValueTracking.h"#include "llvm/IR/Argument.h"#include "llvm/IR/Attributes.h"#include "llvm/IR/BasicBlock.h"#include "llvm/IR/CallSite.h"#include "llvm/IR/Constant.h"#include "llvm/IR/Constants.h"#include "llvm/IR/Function.h"#include "llvm/IR/InstIterator.h"#include "llvm/IR/InstrTypes.h"#include "llvm/IR/Instruction.h"#include "llvm/IR/Instructions.h"#include "llvm/IR/IntrinsicInst.h"#include "llvm/IR/Metadata.h"#include "llvm/IR/PassManager.h"#include "llvm/IR/Type.h"#include "llvm/IR/Use.h"#include "llvm/IR/User.h"#include "llvm/IR/Value.h"#include "llvm/InitializePasses.h"#include "llvm/Pass.h"#include "llvm/Support/Casting.h"#include "llvm/Support/CommandLine.h"#include "llvm/Support/Compiler.h"#include "llvm/Support/Debug.h"#include "llvm/Support/ErrorHandling.h"#include "llvm/Support/raw_ostream.h"#include "llvm/Transforms/IPO.h"#include <cassert>#include <iterator>#include <map>#include <vector>Go to the source code of this file.
Classes | |
| struct | llvm::GraphTraits< ArgumentGraphNode * > |
| struct | llvm::GraphTraits< ArgumentGraph * > |
Namespaces | |
| llvm | |
| This class represents lattice values for constants. | |
Macros | |
| #define | DEBUG_TYPE "functionattrs" |
Functions | |
| STATISTIC (NumReadNone, "Number of functions marked readnone") | |
| STATISTIC (NumReadOnly, "Number of functions marked readonly") | |
| STATISTIC (NumWriteOnly, "Number of functions marked writeonly") | |
| 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") | |
| STATISTIC (NumNoUnwind, "Number of functions marked as nounwind") | |
| STATISTIC (NumNoFree, "Number of functions marked as nofree") | |
| 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 | addReadAttr (Argument *A, Attribute::AttrKind R) |
| 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 | InstrBreaksNonConvergent (Instruction &I, const SCCNodeSet &SCCNodes) |
| Helper for non-Convergent inference predicate InstrBreaksAttribute. More... | |
| static bool | InstrBreaksNonThrowing (Instruction &I, const SCCNodeSet &SCCNodes) |
| Helper for NoUnwind inference predicate InstrBreaksAttribute. More... | |
| static bool | InstrBreaksNoFree (Instruction &I, const SCCNodeSet &SCCNodes) |
| Helper for NoFree inference predicate InstrBreaksAttribute. More... | |
| static bool | inferAttrsFromFunctionBodies (const SCCNodeSet &SCCNodes) |
| Infer attributes from all functions in the SCC by scanning every instruction for compliance to the attribute assumptions. More... | |
| static bool | setDoesNotRecurse (Function &F) |
| static bool | addNoRecurseAttrs (const SCCNodeSet &SCCNodes) |
| template<typename AARGetterT > | |
| static bool | deriveAttrsInPostOrder (SCCNodeSet &SCCNodes, AARGetterT &&AARGetter, bool HasUnknownCall) |
| 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< bool > | EnableNonnullArgPropagation ("enable-nonnull-arg-prop", cl::init(true), cl::Hidden, cl::desc("Try to propagate nonnull argument attributes from callsites to " "caller functions.")) |
| static cl::opt< bool > | DisableNoUnwindInference ("disable-nounwind-inference", cl::Hidden, cl::desc("Stop inferring nounwind attribute during function-attrs pass")) |
| static cl::opt< bool > | DisableNoFreeInference ("disable-nofree-inference", cl::Hidden, cl::desc("Stop inferring nofree attribute during function-attrs pass")) |
| functionattrs | |
| Deduce function | attributes |
| Deduce function | false |
| rpo Deduce function attributes in | RPO |
This file implements interprocedural passes which walk the call-graph deducing and/or propagating function attributes.
Definition in file FunctionAttrs.cpp.
| #define DEBUG_TYPE "functionattrs" |
Definition at line 67 of file FunctionAttrs.cpp.
Deduce nocapture attributes for the SCC.
Definition at line 685 of file FunctionAttrs.cpp.
References A, addArgumentAttrsFromCallsites(), E, F(), and llvm::PointerMayBeCaptured().
Referenced by deriveAttrsInPostOrder().
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 626 of file FunctionAttrs.cpp.
References llvm::dyn_cast(), EnableNonnullArgPropagation, llvm::Function::getEntryBlock(), I, and llvm::isGuaranteedToTransferExecutionToSuccessor().
Referenced by addArgumentAttrs().
Deduce returned attributes for the SCC.
Definition at line 573 of file FunctionAttrs.cpp.
References A, llvm::any_of(), Arg, F(), llvm::Value::getType(), and llvm::MipsISD::Ret.
Referenced by deriveAttrsInPostOrder().
Deduce noalias attributes for the SCC.
Definition at line 926 of file FunctionAttrs.cpp.
References F(), and isFunctionMallocLike().
Referenced by deriveAttrsInPostOrder().
Deduce nonnull attributes for the SCC.
Definition at line 1036 of file FunctionAttrs.cpp.
References llvm::copy_if(), D, llvm::dbgs(), llvm::SmallVectorBase::empty(), llvm::erase_if(), F(), function, I, llvm::instructions(), isReturnNonNull(), LLVM_DEBUG, llvm::SmallVectorTemplateBase< T >::push_back(), and llvm::AttributeList::ReturnIndex.
Referenced by deriveAttrsInPostOrder().
Definition at line 1355 of file FunctionAttrs.cpp.
References Callee, llvm::Function::doesNotRecurse(), F(), llvm::GlobalValue::hasExactDefinition(), I, and setDoesNotRecurse().
Referenced by deriveAttrsInPostOrder().
Definition at line 1556 of file FunctionAttrs.cpp.
References assert(), llvm::Function::doesNotRecurse(), llvm::dyn_cast(), llvm::CallSiteBase< FunTy, BBTy, ValTy, UserTy, UseTy, InstrTy, CallTy, InvokeTy, CallBrTy, IterTy >::getParent(), llvm::BasicBlock::getParent(), llvm::GlobalValue::hasInternalLinkage(), I, llvm::GlobalValue::isDeclaration(), setDoesNotRecurse(), and llvm::Value::users().
Referenced by deduceFunctionAttributeInRPO().
|
static |
Definition at line 665 of file FunctionAttrs.cpp.
References llvm::Argument::addAttr(), assert(), llvm::Argument::hasAttribute(), and llvm::Argument::removeAttr().
|
static |
Deduce readonly/readnone attributes for the SCC.
Definition at line 234 of file FunctionAttrs.cpp.
References llvm::sys::path::begin(), checkFunctionMemoryAccess(), llvm::sys::path::end(), F(), llvm::MAK_MayWrite, llvm::MAK_ReadNone, llvm::MAK_ReadOnly, and llvm::MAK_WriteOnly.
Referenced by deriveAttrsInPostOrder().
|
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 109 of file FunctionAttrs.cpp.
References Arg, llvm::createModRefInfo(), llvm::AAResults::doesNotReadMemory(), E, llvm::FMRB_DoesNotAccessMemory, llvm::MemoryLocation::get(), llvm::Instruction::getAAMetadata(), llvm::AAResults::getModRefBehavior(), llvm::Value::getType(), I, llvm::inst_begin(), llvm::inst_end(), llvm::isModSet(), llvm::isNoModRef(), llvm::Type::isPtrOrPtrVectorTy(), llvm::isRefSet(), llvm::MAK_MayWrite, llvm::MAK_ReadNone, llvm::MAK_ReadOnly, llvm::MAK_WriteOnly, llvm::Instruction::mayReadFromMemory(), llvm::Instruction::mayWriteToMemory(), MRI, llvm::AAResults::onlyAccessesArgPointees(), llvm::AAResults::onlyReadsMemory(), llvm::AAResults::pointsToConstantMemory(), SI, llvm::LocationSize::unknown(), and VI.
Referenced by addReadAttrs(), and llvm::computeFunctionBodyMemoryAccess().
Definition at line 1585 of file FunctionAttrs.cpp.
References addNoRecurseAttrsTopDown(), llvm::Function::doesNotRecurse(), F(), llvm::GlobalValue::hasInternalLinkage(), I, llvm::GlobalValue::isDeclaration(), llvm::SmallVectorTemplateBase< T >::push_back(), llvm::reverse(), and llvm::scc_begin().
Referenced by llvm::ReversePostOrderFunctionAttrsPass::run().
|
static |
Definition at line 1385 of file FunctionAttrs.cpp.
References addArgumentAttrs(), addArgumentReturnedAttrs(), addNoAliasAttrs(), addNonNullAttrs(), addNoRecurseAttrs(), addReadAttrs(), and inferAttrsFromFunctionBodies().
Referenced by llvm::PostOrderFunctionAttrsPass::run(), and runImpl().
|
static |
Returns Attribute::None, Attribute::ReadOnly or Attribute::ReadNone.
Definition at line 446 of file FunctionAttrs.cpp.
References llvm::CallSiteBase< FunTy, BBTy, ValTy, UserTy, UseTy, InstrTy, CallTy, InvokeTy, CallBrTy, 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, CallBrTy, IterTy >::data_operands_size(), llvm::CallSiteBase< FunTy, BBTy, ValTy, UserTy, UseTy, InstrTy, CallTy, InvokeTy, CallBrTy, IterTy >::doesNotAccessMemory(), llvm::CallSiteBase< FunTy, BBTy, ValTy, UserTy, UseTy, InstrTy, CallTy, InvokeTy, CallBrTy, IterTy >::doesNotCapture(), llvm::SmallVectorBase::empty(), F(), llvm::CallSiteBase< FunTy, BBTy, ValTy, UserTy, UseTy, InstrTy, CallTy, InvokeTy, CallBrTy, IterTy >::getCalledFunction(), llvm::CallSiteBase< FunTy, BBTy, ValTy, UserTy, UseTy, InstrTy, CallTy, InvokeTy, CallBrTy, IterTy >::getNumArgOperands(), llvm::Instruction::getOpcode(), llvm::Value::getType(), llvm::Argument::hasInAllocaAttr(), I, llvm::SmallPtrSetImpl< PtrType >::insert(), llvm::Function::isVarArg(), llvm::Type::isVoidTy(), isVolatile(), llvm::SPII::Load, llvm::Attribute::None, llvm::CallSiteBase< FunTy, BBTy, ValTy, UserTy, UseTy, InstrTy, CallTy, InvokeTy, CallBrTy, IterTy >::onlyReadsMemory(), llvm::SmallVectorImpl< T >::pop_back_val(), llvm::SmallVectorTemplateBase< T >::push_back(), llvm::MipsISD::Ret, llvm::MCID::Select, and llvm::Value::uses().
Infer attributes from all functions in the SCC by scanning every instruction for compliance to the attribute assumptions.
Currently it does:
Returns true if any changes to function attributes were made.
Definition at line 1274 of file FunctionAttrs.cpp.
References llvm::MCID::Convergent, llvm::dbgs(), DisableNoFreeInference, DisableNoUnwindInference, F(), I, InstrBreaksNoFree(), InstrBreaksNonConvergent(), InstrBreaksNonThrowing(), and LLVM_DEBUG.
Referenced by deriveAttrsInPostOrder().
| INITIALIZE_PASS_BEGIN | ( | PostOrderFunctionAttrsLegacyPass | , |
| "functionattrs" | , | ||
| "Deduce function attributes" | , | ||
| false | , | ||
| false | |||
| ) |
Referenced by llvm::PostOrderFunctionAttrsPass::run(), and runImpl().
| INITIALIZE_PASS_BEGIN | ( | ReversePostOrderFunctionAttrsLegacyPass | , |
| "rpo-functionattrs" | , | ||
| "Deduce function attributes in RPO" | , | ||
| false | , | ||
| false | |||
| ) |
|
static |
Helper for NoFree inference predicate InstrBreaksAttribute.
Definition at line 1249 of file FunctionAttrs.cpp.
References Callee, llvm::Function::doesNotFreeMemory(), and llvm::CallSiteBase< FunTy, BBTy, ValTy, UserTy, UseTy, InstrTy, CallTy, InvokeTy, CallBrTy, IterTy >::getCalledFunction().
Referenced by inferAttrsFromFunctionBodies().
|
static |
Helper for non-Convergent inference predicate InstrBreaksAttribute.
Definition at line 1224 of file FunctionAttrs.cpp.
References llvm::CallSiteBase< FunTy, BBTy, ValTy, UserTy, UseTy, InstrTy, CallTy, InvokeTy, CallBrTy, IterTy >::getCalledFunction(), and llvm::CallSiteBase< FunTy, BBTy, ValTy, UserTy, UseTy, InstrTy, CallTy, InvokeTy, CallBrTy, IterTy >::isConvergent().
Referenced by inferAttrsFromFunctionBodies().
|
static |
Helper for NoUnwind inference predicate InstrBreaksAttribute.
Definition at line 1233 of file FunctionAttrs.cpp.
References Callee, and llvm::Instruction::mayThrow().
Referenced by inferAttrsFromFunctionBodies().
Tests whether a function is "malloc-like".
A function is "malloc-like" if it returns either null or a pointer that doesn't alias any other pointer visible to the caller.
Definition at line 862 of file FunctionAttrs.cpp.
References C, llvm::MCID::Call, llvm::CallSiteBase< FunTy, BBTy, ValTy, UserTy, UseTy, InstrTy, CallTy, InvokeTy, CallBrTy, IterTy >::getCalledFunction(), llvm::SelectInst::getFalseValue(), llvm::SelectInst::getTrueValue(), llvm::CallSiteBase< FunTy, BBTy, ValTy, UserTy, UseTy, InstrTy, CallTy, InvokeTy, CallBrTy, IterTy >::hasRetAttr(), llvm::PHINode::incoming_values(), llvm::SetVector< T, SmallVector< T, N >, SmallDenseSet< T, N > >::insert(), LLVM_FALLTHROUGH, llvm::NoAlias, llvm::PointerMayBeCaptured(), llvm::MipsISD::Ret, llvm::MCID::Select, SI, and llvm::SetVector< T, SmallVector< T, N >, SmallDenseSet< T, N > >::size().
Referenced by addNoAliasAttrs().
|
static |
Tests whether this function is known to not return null.
Requires that the function returns a pointer.
Returns true if it believes the function will not return a null, and sets Speculative based on whether the returned conclusion is a speculative conclusion due to SCC calls.
Definition at line 970 of file FunctionAttrs.cpp.
References assert(), llvm::MCID::Call, Callee, llvm::dyn_cast(), llvm::numbers::e, llvm::CallSiteBase< FunTy, BBTy, ValTy, UserTy, UseTy, InstrTy, CallTy, InvokeTy, CallBrTy, IterTy >::getCalledFunction(), llvm::SelectInst::getFalseValue(), llvm::PHINode::getIncomingValue(), llvm::PHINode::getNumIncomingValues(), llvm::Instruction::getOpcode(), llvm::User::getOperand(), llvm::Function::getReturnType(), llvm::SelectInst::getTrueValue(), llvm::SetVector< T, SmallVector< T, N >, SmallDenseSet< T, N > >::insert(), llvm::isKnownNonZero(), llvm::Type::isPointerTy(), llvm_unreachable, llvm::MipsISD::Ret, llvm::MCID::Select, SI, and llvm::SetVector< T, SmallVector< T, N >, SmallDenseSet< T, N > >::size().
Referenced by addNonNullAttrs(), and llvm::CallBase::getDereferenceableOrNullBytes().
|
static |
Definition at line 1493 of file FunctionAttrs.cpp.
References llvm::AnalysisUsage::addPreserved(), llvm::AnalysisUsage::addRequired(), deriveAttrsInPostOrder(), F(), llvm::PassRegistry::getPassRegistry(), llvm::Function::hasFnAttribute(), llvm::Function::hasOptNone(), I, INITIALIZE_PASS_BEGIN(), INITIALIZE_PASS_DEPENDENCY, llvm::initializeReversePostOrderFunctionAttrsLegacyPassPass(), and llvm::AnalysisUsage::setPreservesCFG().
Definition at line 1347 of file FunctionAttrs.cpp.
References llvm::Function::doesNotRecurse(), and llvm::Function::setDoesNotRecurse().
Referenced by addNoRecurseAttrs(), and addNoRecurseAttrsTopDown().
| STATISTIC | ( | NumReadNone | , |
| "Number of functions marked readnone" | |||
| ) |
| STATISTIC | ( | NumReadOnly | , |
| "Number of functions marked readonly" | |||
| ) |
| STATISTIC | ( | NumWriteOnly | , |
| "Number of functions marked writeonly" | |||
| ) |
| 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" | |||
| ) |
| STATISTIC | ( | NumNoUnwind | , |
| "Number of functions marked as nounwind" | |||
| ) |
| STATISTIC | ( | NumNoFree | , |
| "Number of functions marked as nofree" | |||
| ) |
| Deduce function attributes |
Definition at line 1485 of file FunctionAttrs.cpp.
|
static |
Referenced by inferAttrsFromFunctionBodies().
|
static |
Referenced by inferAttrsFromFunctionBodies().
|
static |
Referenced by addArgumentAttrsFromCallsites().
| rpo Deduce function attributes in false |
Definition at line 1485 of file FunctionAttrs.cpp.
| rpo functionattrs |
Definition at line 1485 of file FunctionAttrs.cpp.
| rpo Deduce function attributes in RPO |
Definition at line 1549 of file FunctionAttrs.cpp.
Referenced by getInnermostLoopFor(), isDebug(), isMinSize(), and isPushPop().
1.8.13