LLVM
15.0.0git
|
#include "llvm/Transforms/IPO/FunctionAttrs.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/SCCIterator.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SetVector.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/SmallSet.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/Analysis/AssumptionCache.h"
#include "llvm/Analysis/BasicAliasAnalysis.h"
#include "llvm/Analysis/CFG.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/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/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/ModuleSummaryIndex.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 "llvm/Transforms/Utils/Local.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 is an optimization pass for GlobalISel generic memory operations. | |
Macros | |
#define | DEBUG_TYPE "function-attrs" |
Functions | |
STATISTIC (NumArgMemOnly, "Number of functions marked argmemonly") | |
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 (NumWriteOnlyArg, "Number of arguments marked writeonly") | |
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") | |
STATISTIC (NumWillReturn, "Number of functions marked as willreturn") | |
STATISTIC (NumNoSync, "Number of functions marked as nosync") | |
STATISTIC (NumThinLinkNoRecurse, "Number of functions marked as norecurse during thinlink") | |
STATISTIC (NumThinLinkNoUnwind, "Number of functions marked as nounwind during thinlink") | |
static FunctionModRefBehavior | 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 void | addMemoryAttrs (const SCCNodeSet &SCCNodes, AARGetterT &&AARGetter, SmallSet< Function *, 8 > &Changed) |
Deduce readonly/readnone/writeonly attributes for the SCC. More... | |
static FunctionSummary * | calculatePrevailingSummary (ValueInfo VI, DenseMap< ValueInfo, FunctionSummary * > &CachedPrevailingSummary, function_ref< bool(GlobalValue::GUID, const GlobalValueSummary *)> IsPrevailing) |
static Attribute::AttrKind | determinePointerAccessAttrs (Argument *A, const SmallPtrSet< Argument *, 8 > &SCCNodes) |
Returns Attribute::None, Attribute::ReadOnly or Attribute::ReadNone. More... | |
static void | addArgumentReturnedAttrs (const SCCNodeSet &SCCNodes, SmallSet< Function *, 8 > &Changed) |
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 | addAccessAttr (Argument *A, Attribute::AttrKind R) |
static void | addArgumentAttrs (const SCCNodeSet &SCCNodes, SmallSet< Function *, 8 > &Changed) |
Deduce nocapture attributes for the SCC. More... | |
static bool | isFunctionMallocLike (Function *F, const SCCNodeSet &SCCNodes) |
Tests whether a function is "malloc-like". More... | |
static void | addNoAliasAttrs (const SCCNodeSet &SCCNodes, SmallSet< Function *, 8 > &Changed) |
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 void | addNonNullAttrs (const SCCNodeSet &SCCNodes, SmallSet< Function *, 8 > &Changed) |
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 void | inferConvergent (const SCCNodeSet &SCCNodes, SmallSet< Function *, 8 > &Changed) |
Attempt to remove convergent function attribute when possible. More... | |
static void | inferAttrsFromFunctionBodies (const SCCNodeSet &SCCNodes, SmallSet< Function *, 8 > &Changed) |
Infer attributes from all functions in the SCC by scanning every instruction for compliance to the attribute assumptions. More... | |
static void | addNoRecurseAttrs (const SCCNodeSet &SCCNodes, SmallSet< Function *, 8 > &Changed) |
static bool | instructionDoesNotReturn (Instruction &I) |
static bool | basicBlockCanReturn (BasicBlock &BB) |
static bool | canReturn (Function &F) |
static void | addNoReturnAttrs (const SCCNodeSet &SCCNodes, SmallSet< Function *, 8 > &Changed) |
static bool | functionWillReturn (const Function &F) |
static void | addWillReturn (const SCCNodeSet &SCCNodes, SmallSet< Function *, 8 > &Changed) |
static bool | isOrderedAtomic (Instruction *I) |
static bool | InstrBreaksNoSync (Instruction &I, const SCCNodeSet &SCCNodes) |
static void | addNoSyncAttr (const SCCNodeSet &SCCNodes, SmallSet< Function *, 8 > &Changed) |
static SCCNodesResult | createSCCNodeSet (ArrayRef< Function * > Functions) |
template<typename AARGetterT > | |
static SmallSet< Function *, 8 > | deriveAttrsInPostOrder (ArrayRef< Function * > Functions, AARGetterT &&AARGetter) |
INITIALIZE_PASS_BEGIN (PostOrderFunctionAttrsLegacyPass, "function-attrs", "Deduce function attributes", false, false) INITIALIZE_PASS_END(PostOrderFunctionAttrsLegacyPass | |
template<typename AARGetterT > | |
static bool | runImpl (CallGraphSCC &SCC, AARGetterT AARGetter) |
INITIALIZE_PASS_BEGIN (ReversePostOrderFunctionAttrsLegacyPass, "rpo-function-attrs", "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")) |
static cl::opt< bool > | DisableThinLTOPropagation ("disable-thinlto-funcattrs", cl::init(true), cl::Hidden, cl::desc("Don't propagate function-attrs in thinLTO")) |
function | attrs |
function Deduce function | attributes |
function Deduce function | false |
rpo function 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 "function-attrs" |
Definition at line 70 of file FunctionAttrs.cpp.
|
static |
Definition at line 888 of file FunctionAttrs.cpp.
References A, assert(), and llvm::RISCVFenceField::R.
|
static |
Deduce nocapture attributes for the SCC.
Definition at line 914 of file FunctionAttrs.cpp.
References A, addArgumentAttrsFromCallsites(), E, F, llvm::SmallSet< T, N, C >::insert(), and llvm::PointerMayBeCaptured().
Referenced by deriveAttrsInPostOrder().
|
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 849 of file FunctionAttrs.cpp.
References EnableNonnullArgPropagation.
Referenced by addArgumentAttrs().
|
static |
Deduce returned attributes for the SCC.
Definition at line 799 of file FunctionAttrs.cpp.
References A, llvm::any_of(), Arg, BB, F, llvm::Value::getType(), llvm::SmallSet< T, N, C >::insert(), and llvm::MipsISD::Ret.
Referenced by deriveAttrsInPostOrder().
|
static |
Deduce readonly/readnone/writeonly attributes for the SCC.
Definition at line 263 of file FunctionAttrs.cpp.
References llvm::AttributeMask::addAttribute(), assert(), checkFunctionMemoryAccess(), llvm::createModRefInfo(), F, llvm::FMRB_DoesNotAccessMemory, llvm::SmallSet< T, N, C >::insert(), llvm::isModSet(), llvm::isRefSet(), and llvm::AAResults::onlyAccessesArgPointees().
Referenced by deriveAttrsInPostOrder().
|
static |
Deduce noalias attributes for the SCC.
Definition at line 1166 of file FunctionAttrs.cpp.
References F, llvm::SmallSet< T, N, C >::insert(), and isFunctionMallocLike().
Referenced by deriveAttrsInPostOrder().
|
static |
Deduce nonnull attributes for the SCC.
Definition at line 1274 of file FunctionAttrs.cpp.
References llvm::dbgs(), F, llvm::SmallSet< T, N, C >::insert(), isReturnNonNull(), and LLVM_DEBUG.
Referenced by deriveAttrsInPostOrder().
|
static |
Definition at line 1592 of file FunctionAttrs.cpp.
References BB, Callee, F, llvm::CallBase::getCalledFunction(), I, and llvm::SmallSet< T, N, C >::insert().
Referenced by deriveAttrsInPostOrder().
|
static |
Definition at line 1999 of file FunctionAttrs.cpp.
References assert(), llvm::Function::doesNotRecurse(), F, llvm::Instruction::getParent(), llvm::BasicBlock::getParent(), and I.
Referenced by deduceFunctionAttributeInRPO().
|
static |
Definition at line 1659 of file FunctionAttrs.cpp.
References canReturn(), F, and llvm::SmallSet< T, N, C >::insert().
Referenced by deriveAttrsInPostOrder().
|
static |
Definition at line 1771 of file FunctionAttrs.cpp.
References llvm::dbgs(), F, I, InstrBreaksNoSync(), and LLVM_DEBUG.
Referenced by deriveAttrsInPostOrder().
|
static |
Definition at line 1703 of file FunctionAttrs.cpp.
References F, functionWillReturn(), and llvm::SmallSet< T, N, C >::insert().
Referenced by deriveAttrsInPostOrder().
|
static |
Definition at line 1632 of file FunctionAttrs.cpp.
References BB, instructionDoesNotReturn(), and llvm::none_of().
Referenced by canReturn().
|
static |
At this point, prevailing symbols have been resolved. The following leads to returning a conservative result:
These next 2 cases should not happen and will assert:
Otherwise, we calculate attributes for a function as:
Definition at line 355 of file FunctionAttrs.cpp.
References assert(), llvm::DenseMapBase< DenseMap< KeyT, ValueT, DenseMapInfo< KeyT >, llvm::detail::DenseMapPair< KeyT, ValueT > >, KeyT, ValueT, DenseMapInfo< KeyT >, llvm::detail::DenseMapPair< KeyT, ValueT > >::count(), llvm::dbgs(), llvm::X86AS::FS, llvm::GlobalValue::isAvailableExternallyLinkage(), llvm::GlobalValue::isExternalLinkage(), llvm::GlobalValue::isLinkOnceAnyLinkage(), llvm::GlobalValue::isLinkOnceODRLinkage(), llvm::GlobalValue::isLocalLinkage(), llvm::GlobalValue::isWeakAnyLinkage(), llvm::GlobalValue::isWeakODRLinkage(), LLVM_DEBUG, and VI.
Referenced by llvm::thinLTOPropagateFunctionAttrs().
|
static |
Definition at line 1639 of file FunctionAttrs.cpp.
References basicBlockCanReturn(), BB, F, llvm::SmallPtrSetImpl< PtrType >::insert(), llvm::SmallVectorImpl< T >::pop_back_val(), and llvm::successors().
Referenced by addNoReturnAttrs().
|
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 126 of file FunctionAttrs.cpp.
References Arg, llvm::createModRefInfo(), F, llvm::FMRB_DoesNotAccessMemory, llvm::FMRL_Anywhere, llvm::FMRL_ArgumentPointees, llvm::MemoryLocation::get(), llvm::MemoryLocation::getBeforeOrAfter(), llvm::AAResults::getModRefBehavior(), llvm::getUnderlyingObject(), I, llvm::instructions(), llvm::isModSet(), llvm::isNoModRef(), llvm::isRefSet(), llvm::Mod, MRI, llvm::AAResults::onlyAccessesArgPointees(), llvm::AAResults::pointsToConstantMemory(), llvm::MemoryLocation::Ptr, llvm::Ref, SI, and VI.
Referenced by addMemoryAttrs(), and llvm::computeFunctionBodyMemoryAccess().
Definition at line 1792 of file FunctionAttrs.cpp.
References F, llvm::CallBase::getCalledFunction(), I, and llvm::instructions().
Referenced by deriveAttrsInPostOrder().
Definition at line 2030 of file FunctionAttrs.cpp.
References addNoRecurseAttrsTopDown(), F, I, llvm::reverse(), and llvm::scc_begin().
Referenced by llvm::ReversePostOrderFunctionAttrsPass::run().
|
static |
Definition at line 1824 of file FunctionAttrs.cpp.
References addArgumentAttrs(), addArgumentReturnedAttrs(), addMemoryAttrs(), addNoAliasAttrs(), addNonNullAttrs(), addNoRecurseAttrs(), addNoReturnAttrs(), addNoSyncAttr(), addWillReturn(), createSCCNodeSet(), F, llvm::inferAttributesFromOthers(), inferAttrsFromFunctionBodies(), inferConvergent(), and llvm::SmallSet< T, N, C >::insert().
Referenced by llvm::PostOrderFunctionAttrsPass::run(), and runImpl().
|
static |
Returns Attribute::None, Attribute::ReadOnly or Attribute::ReadNone.
Definition at line 666 of file FunctionAttrs.cpp.
References A, llvm::MCID::Call, llvm::SmallPtrSetImpl< PtrType >::count(), llvm::CallBase::dataOperandHasImpliedAttr(), llvm::CallBase::doesNotAccessMemory(), llvm::CallBase::doesNotCapture(), F, llvm::CallBase::getCalledFunction(), llvm::CallBase::getDataOperandNo(), llvm::CallBase::hasFnAttr(), I, llvm::SmallPtrSetImpl< PtrType >::insert(), llvm::CallBase::isArgOperand(), llvm::CallBase::isCallee(), llvm::SPII::Load, llvm::Attribute::None, llvm::CallBase::onlyReadsMemory(), llvm::SmallVectorImpl< T >::pop_back_val(), llvm::MipsISD::Ret, llvm::MCID::Select, and llvm::SPII::Store.
Definition at line 1673 of file FunctionAttrs.cpp.
References llvm::all_of(), F, llvm::FindFunctionBackedges(), I, and llvm::instructions().
Referenced by addWillReturn().
|
static |
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 1539 of file FunctionAttrs.cpp.
References llvm::dbgs(), DisableNoFreeInference, DisableNoUnwindInference, F, I, InstrBreaksNoFree(), InstrBreaksNonThrowing(), and LLVM_DEBUG.
Referenced by deriveAttrsInPostOrder().
|
static |
Attempt to remove convergent function attribute when possible.
Returns true if any changes to function attributes were made.
Definition at line 1506 of file FunctionAttrs.cpp.
References llvm::MCID::Convergent, llvm::dbgs(), F, I, InstrBreaksNonConvergent(), and LLVM_DEBUG.
Referenced by deriveAttrsInPostOrder().
INITIALIZE_PASS_BEGIN | ( | PostOrderFunctionAttrsLegacyPass | , |
"function-attrs" | , | ||
"Deduce function attributes" | , | ||
false | , | ||
false | |||
) |
INITIALIZE_PASS_BEGIN | ( | ReversePostOrderFunctionAttrsLegacyPass | , |
"rpo-function-attrs" | , | ||
"Deduce function attributes in RPO" | , | ||
false | , | ||
false | |||
) |
|
static |
Helper for NoFree inference predicate InstrBreaksAttribute.
Definition at line 1487 of file FunctionAttrs.cpp.
References Callee, llvm::CallBase::getCalledFunction(), llvm::CallBase::hasFnAttr(), and I.
Referenced by inferAttrsFromFunctionBodies().
|
static |
Helper for non-Convergent inference predicate InstrBreaksAttribute.
Definition at line 1461 of file FunctionAttrs.cpp.
References llvm::CallBase::getCalledFunction(), I, and llvm::CallBase::isConvergent().
Referenced by inferConvergent().
|
static |
Helper for NoUnwind inference predicate InstrBreaksAttribute.
Definition at line 1471 of file FunctionAttrs.cpp.
Referenced by inferAttrsFromFunctionBodies().
|
static |
Definition at line 1738 of file FunctionAttrs.cpp.
References Callee, llvm::CallBase::getCalledFunction(), llvm::CallBase::hasFnAttr(), I, isOrderedAtomic(), and MI.
Referenced by addNoSyncAttr().
|
static |
Definition at line 1624 of file FunctionAttrs.cpp.
References llvm::CallBase::hasFnAttr(), and I.
Referenced by basicBlockCanReturn().
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 1102 of file FunctionAttrs.cpp.
References BB, llvm::MCID::Call, F, llvm::CallBase::getCalledFunction(), llvm::CallBase::hasRetAttr(), i, llvm::PHINode::incoming_values(), llvm::SetVector< T, SmallVector< T, N >, SmallDenseSet< T, N > >::insert(), LLVM_FALLTHROUGH, llvm::PointerMayBeCaptured(), llvm::MipsISD::Ret, llvm::MCID::Select, SI, and llvm::SetVector< T, SmallVector< T, N >, SmallDenseSet< T, N > >::size().
Referenced by addNoAliasAttrs().
|
static |
Definition at line 1720 of file FunctionAttrs.cpp.
References I, llvm_unreachable, SI, and llvm::SyncScope::SingleThread.
Referenced by InstrBreaksNoSync().
|
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 1208 of file FunctionAttrs.cpp.
References assert(), BB, llvm::MCID::Call, Callee, DL, llvm::numbers::e, F, llvm::CallBase::getCalledFunction(), llvm::PHINode::getIncomingValue(), llvm::PHINode::getNumIncomingValues(), llvm::Instruction::getOpcode(), llvm::User::getOperand(), i, llvm::SetVector< T, SmallVector< T, N >, SmallDenseSet< T, N > >::insert(), llvm::isKnownNonZero(), llvm_unreachable, llvm::MipsISD::Ret, llvm::MCID::Select, SI, and llvm::SetVector< T, SmallVector< T, N >, SmallDenseSet< T, N > >::size().
Referenced by addNonNullAttrs().
|
static |
Definition at line 1948 of file FunctionAttrs.cpp.
References deriveAttrsInPostOrder(), I, and llvm::AMDGPU::CPol::SCC.
Definition at line 1941 of file FunctionAttrs.cpp.
rpo function attrs |
Definition at line 1940 of file FunctionAttrs.cpp.
|
static |
Referenced by inferAttrsFromFunctionBodies().
|
static |
Referenced by inferAttrsFromFunctionBodies().
|
static |
Referenced by llvm::thinLTOPropagateFunctionAttrs().
|
static |
Referenced by addArgumentAttrsFromCallsites().
rpo function Deduce function attributes in false |
Definition at line 1941 of file FunctionAttrs.cpp.
rpo function Deduce function attributes in RPO |
Definition at line 1992 of file FunctionAttrs.cpp.