Path: blob/main/contrib/llvm-project/llvm/lib/Transforms/IPO/FunctionAttrs.cpp
35266 views
//===- FunctionAttrs.cpp - Pass which marks functions attributes ----------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8/// \file9/// This file implements interprocedural passes which walk the10/// call-graph deducing and/or propagating function attributes.11//12//===----------------------------------------------------------------------===//1314#include "llvm/Transforms/IPO/FunctionAttrs.h"15#include "llvm/ADT/ArrayRef.h"16#include "llvm/ADT/DenseMap.h"17#include "llvm/ADT/SCCIterator.h"18#include "llvm/ADT/STLExtras.h"19#include "llvm/ADT/SetVector.h"20#include "llvm/ADT/SmallPtrSet.h"21#include "llvm/ADT/SmallSet.h"22#include "llvm/ADT/SmallVector.h"23#include "llvm/ADT/Statistic.h"24#include "llvm/Analysis/AssumptionCache.h"25#include "llvm/Analysis/BasicAliasAnalysis.h"26#include "llvm/Analysis/CFG.h"27#include "llvm/Analysis/CGSCCPassManager.h"28#include "llvm/Analysis/CallGraph.h"29#include "llvm/Analysis/CallGraphSCCPass.h"30#include "llvm/Analysis/CaptureTracking.h"31#include "llvm/Analysis/LazyCallGraph.h"32#include "llvm/Analysis/MemoryLocation.h"33#include "llvm/Analysis/ValueTracking.h"34#include "llvm/IR/Argument.h"35#include "llvm/IR/Attributes.h"36#include "llvm/IR/BasicBlock.h"37#include "llvm/IR/Constant.h"38#include "llvm/IR/Constants.h"39#include "llvm/IR/Function.h"40#include "llvm/IR/InstIterator.h"41#include "llvm/IR/InstrTypes.h"42#include "llvm/IR/Instruction.h"43#include "llvm/IR/Instructions.h"44#include "llvm/IR/IntrinsicInst.h"45#include "llvm/IR/Metadata.h"46#include "llvm/IR/ModuleSummaryIndex.h"47#include "llvm/IR/PassManager.h"48#include "llvm/IR/Type.h"49#include "llvm/IR/Use.h"50#include "llvm/IR/User.h"51#include "llvm/IR/Value.h"52#include "llvm/Support/Casting.h"53#include "llvm/Support/CommandLine.h"54#include "llvm/Support/Compiler.h"55#include "llvm/Support/Debug.h"56#include "llvm/Support/ErrorHandling.h"57#include "llvm/Support/raw_ostream.h"58#include "llvm/Transforms/IPO.h"59#include "llvm/Transforms/Utils/Local.h"60#include <cassert>61#include <iterator>62#include <map>63#include <optional>64#include <vector>6566using namespace llvm;6768#define DEBUG_TYPE "function-attrs"6970STATISTIC(NumMemoryAttr, "Number of functions with improved memory attribute");71STATISTIC(NumNoCapture, "Number of arguments marked nocapture");72STATISTIC(NumReturned, "Number of arguments marked returned");73STATISTIC(NumReadNoneArg, "Number of arguments marked readnone");74STATISTIC(NumReadOnlyArg, "Number of arguments marked readonly");75STATISTIC(NumWriteOnlyArg, "Number of arguments marked writeonly");76STATISTIC(NumNoAlias, "Number of function returns marked noalias");77STATISTIC(NumNonNullReturn, "Number of function returns marked nonnull");78STATISTIC(NumNoUndefReturn, "Number of function returns marked noundef");79STATISTIC(NumNoRecurse, "Number of functions marked as norecurse");80STATISTIC(NumNoUnwind, "Number of functions marked as nounwind");81STATISTIC(NumNoFree, "Number of functions marked as nofree");82STATISTIC(NumWillReturn, "Number of functions marked as willreturn");83STATISTIC(NumNoSync, "Number of functions marked as nosync");8485STATISTIC(NumThinLinkNoRecurse,86"Number of functions marked as norecurse during thinlink");87STATISTIC(NumThinLinkNoUnwind,88"Number of functions marked as nounwind during thinlink");8990static cl::opt<bool> EnableNonnullArgPropagation(91"enable-nonnull-arg-prop", cl::init(true), cl::Hidden,92cl::desc("Try to propagate nonnull argument attributes from callsites to "93"caller functions."));9495static cl::opt<bool> DisableNoUnwindInference(96"disable-nounwind-inference", cl::Hidden,97cl::desc("Stop inferring nounwind attribute during function-attrs pass"));9899static cl::opt<bool> DisableNoFreeInference(100"disable-nofree-inference", cl::Hidden,101cl::desc("Stop inferring nofree attribute during function-attrs pass"));102103static cl::opt<bool> DisableThinLTOPropagation(104"disable-thinlto-funcattrs", cl::init(true), cl::Hidden,105cl::desc("Don't propagate function-attrs in thinLTO"));106107namespace {108109using SCCNodeSet = SmallSetVector<Function *, 8>;110111} // end anonymous namespace112113static void addLocAccess(MemoryEffects &ME, const MemoryLocation &Loc,114ModRefInfo MR, AAResults &AAR) {115// Ignore accesses to known-invariant or local memory.116MR &= AAR.getModRefInfoMask(Loc, /*IgnoreLocal=*/true);117if (isNoModRef(MR))118return;119120const Value *UO = getUnderlyingObject(Loc.Ptr);121assert(!isa<AllocaInst>(UO) &&122"Should have been handled by getModRefInfoMask()");123if (isa<Argument>(UO)) {124ME |= MemoryEffects::argMemOnly(MR);125return;126}127128// If it's not an identified object, it might be an argument.129if (!isIdentifiedObject(UO))130ME |= MemoryEffects::argMemOnly(MR);131ME |= MemoryEffects(IRMemLocation::Other, MR);132}133134static void addArgLocs(MemoryEffects &ME, const CallBase *Call,135ModRefInfo ArgMR, AAResults &AAR) {136for (const Value *Arg : Call->args()) {137if (!Arg->getType()->isPtrOrPtrVectorTy())138continue;139140addLocAccess(ME,141MemoryLocation::getBeforeOrAfter(Arg, Call->getAAMetadata()),142ArgMR, AAR);143}144}145146/// Returns the memory access attribute for function F using AAR for AA results,147/// where SCCNodes is the current SCC.148///149/// If ThisBody is true, this function may examine the function body and will150/// return a result pertaining to this copy of the function. If it is false, the151/// result will be based only on AA results for the function declaration; it152/// will be assumed that some other (perhaps less optimized) version of the153/// function may be selected at link time.154///155/// The return value is split into two parts: Memory effects that always apply,156/// and additional memory effects that apply if any of the functions in the SCC157/// can access argmem.158static std::pair<MemoryEffects, MemoryEffects>159checkFunctionMemoryAccess(Function &F, bool ThisBody, AAResults &AAR,160const SCCNodeSet &SCCNodes) {161MemoryEffects OrigME = AAR.getMemoryEffects(&F);162if (OrigME.doesNotAccessMemory())163// Already perfect!164return {OrigME, MemoryEffects::none()};165166if (!ThisBody)167return {OrigME, MemoryEffects::none()};168169MemoryEffects ME = MemoryEffects::none();170// Additional locations accessed if the SCC accesses argmem.171MemoryEffects RecursiveArgME = MemoryEffects::none();172173// Inalloca and preallocated arguments are always clobbered by the call.174if (F.getAttributes().hasAttrSomewhere(Attribute::InAlloca) ||175F.getAttributes().hasAttrSomewhere(Attribute::Preallocated))176ME |= MemoryEffects::argMemOnly(ModRefInfo::ModRef);177178// Scan the function body for instructions that may read or write memory.179for (Instruction &I : instructions(F)) {180// Some instructions can be ignored even if they read or write memory.181// Detect these now, skipping to the next instruction if one is found.182if (auto *Call = dyn_cast<CallBase>(&I)) {183// We can optimistically ignore calls to functions in the same SCC, with184// two caveats:185// * Calls with operand bundles may have additional effects.186// * Argument memory accesses may imply additional effects depending on187// what the argument location is.188if (!Call->hasOperandBundles() && Call->getCalledFunction() &&189SCCNodes.count(Call->getCalledFunction())) {190// Keep track of which additional locations are accessed if the SCC191// turns out to access argmem.192addArgLocs(RecursiveArgME, Call, ModRefInfo::ModRef, AAR);193continue;194}195196MemoryEffects CallME = AAR.getMemoryEffects(Call);197198// If the call doesn't access memory, we're done.199if (CallME.doesNotAccessMemory())200continue;201202// A pseudo probe call shouldn't change any function attribute since it203// doesn't translate to a real instruction. It comes with a memory access204// tag to prevent itself being removed by optimizations and not block205// other instructions being optimized.206if (isa<PseudoProbeInst>(I))207continue;208209ME |= CallME.getWithoutLoc(IRMemLocation::ArgMem);210211// If the call accesses captured memory (currently part of "other") and212// an argument is captured (currently not tracked), then it may also213// access argument memory.214ModRefInfo OtherMR = CallME.getModRef(IRMemLocation::Other);215ME |= MemoryEffects::argMemOnly(OtherMR);216217// Check whether all pointer arguments point to local memory, and218// ignore calls that only access local memory.219ModRefInfo ArgMR = CallME.getModRef(IRMemLocation::ArgMem);220if (ArgMR != ModRefInfo::NoModRef)221addArgLocs(ME, Call, ArgMR, AAR);222continue;223}224225ModRefInfo MR = ModRefInfo::NoModRef;226if (I.mayWriteToMemory())227MR |= ModRefInfo::Mod;228if (I.mayReadFromMemory())229MR |= ModRefInfo::Ref;230if (MR == ModRefInfo::NoModRef)231continue;232233std::optional<MemoryLocation> Loc = MemoryLocation::getOrNone(&I);234if (!Loc) {235// If no location is known, conservatively assume anything can be236// accessed.237ME |= MemoryEffects(MR);238continue;239}240241// Volatile operations may access inaccessible memory.242if (I.isVolatile())243ME |= MemoryEffects::inaccessibleMemOnly(MR);244245addLocAccess(ME, *Loc, MR, AAR);246}247248return {OrigME & ME, RecursiveArgME};249}250251MemoryEffects llvm::computeFunctionBodyMemoryAccess(Function &F,252AAResults &AAR) {253return checkFunctionMemoryAccess(F, /*ThisBody=*/true, AAR, {}).first;254}255256/// Deduce readonly/readnone/writeonly attributes for the SCC.257template <typename AARGetterT>258static void addMemoryAttrs(const SCCNodeSet &SCCNodes, AARGetterT &&AARGetter,259SmallSet<Function *, 8> &Changed) {260MemoryEffects ME = MemoryEffects::none();261MemoryEffects RecursiveArgME = MemoryEffects::none();262for (Function *F : SCCNodes) {263// Call the callable parameter to look up AA results for this function.264AAResults &AAR = AARGetter(*F);265// Non-exact function definitions may not be selected at link time, and an266// alternative version that writes to memory may be selected. See the267// comment on GlobalValue::isDefinitionExact for more details.268auto [FnME, FnRecursiveArgME] =269checkFunctionMemoryAccess(*F, F->hasExactDefinition(), AAR, SCCNodes);270ME |= FnME;271RecursiveArgME |= FnRecursiveArgME;272// Reached bottom of the lattice, we will not be able to improve the result.273if (ME == MemoryEffects::unknown())274return;275}276277// If the SCC accesses argmem, add recursive accesses resulting from that.278ModRefInfo ArgMR = ME.getModRef(IRMemLocation::ArgMem);279if (ArgMR != ModRefInfo::NoModRef)280ME |= RecursiveArgME & MemoryEffects(ArgMR);281282for (Function *F : SCCNodes) {283MemoryEffects OldME = F->getMemoryEffects();284MemoryEffects NewME = ME & OldME;285if (NewME != OldME) {286++NumMemoryAttr;287F->setMemoryEffects(NewME);288// Remove conflicting writable attributes.289if (!isModSet(NewME.getModRef(IRMemLocation::ArgMem)))290for (Argument &A : F->args())291A.removeAttr(Attribute::Writable);292Changed.insert(F);293}294}295}296297// Compute definitive function attributes for a function taking into account298// prevailing definitions and linkage types299static FunctionSummary *calculatePrevailingSummary(300ValueInfo VI,301DenseMap<ValueInfo, FunctionSummary *> &CachedPrevailingSummary,302function_ref<bool(GlobalValue::GUID, const GlobalValueSummary *)>303IsPrevailing) {304305if (CachedPrevailingSummary.count(VI))306return CachedPrevailingSummary[VI];307308/// At this point, prevailing symbols have been resolved. The following leads309/// to returning a conservative result:310/// - Multiple instances with local linkage. Normally local linkage would be311/// unique per module312/// as the GUID includes the module path. We could have a guid alias if313/// there wasn't any distinguishing path when each file was compiled, but314/// that should be rare so we'll punt on those.315316/// These next 2 cases should not happen and will assert:317/// - Multiple instances with external linkage. This should be caught in318/// symbol resolution319/// - Non-existent FunctionSummary for Aliasee. This presents a hole in our320/// knowledge meaning we have to go conservative.321322/// Otherwise, we calculate attributes for a function as:323/// 1. If we have a local linkage, take its attributes. If there's somehow324/// multiple, bail and go conservative.325/// 2. If we have an external/WeakODR/LinkOnceODR linkage check that it is326/// prevailing, take its attributes.327/// 3. If we have a Weak/LinkOnce linkage the copies can have semantic328/// differences. However, if the prevailing copy is known it will be used329/// so take its attributes. If the prevailing copy is in a native file330/// all IR copies will be dead and propagation will go conservative.331/// 4. AvailableExternally summaries without a prevailing copy are known to332/// occur in a couple of circumstances:333/// a. An internal function gets imported due to its caller getting334/// imported, it becomes AvailableExternally but no prevailing335/// definition exists. Because it has to get imported along with its336/// caller the attributes will be captured by propagating on its337/// caller.338/// b. C++11 [temp.explicit]p10 can generate AvailableExternally339/// definitions of explicitly instanced template declarations340/// for inlining which are ultimately dropped from the TU. Since this341/// is localized to the TU the attributes will have already made it to342/// the callers.343/// These are edge cases and already captured by their callers so we344/// ignore these for now. If they become relevant to optimize in the345/// future this can be revisited.346/// 5. Otherwise, go conservative.347348CachedPrevailingSummary[VI] = nullptr;349FunctionSummary *Local = nullptr;350FunctionSummary *Prevailing = nullptr;351352for (const auto &GVS : VI.getSummaryList()) {353if (!GVS->isLive())354continue;355356FunctionSummary *FS = dyn_cast<FunctionSummary>(GVS->getBaseObject());357// Virtual and Unknown (e.g. indirect) calls require going conservative358if (!FS || FS->fflags().HasUnknownCall)359return nullptr;360361const auto &Linkage = GVS->linkage();362if (GlobalValue::isLocalLinkage(Linkage)) {363if (Local) {364LLVM_DEBUG(365dbgs()366<< "ThinLTO FunctionAttrs: Multiple Local Linkage, bailing on "367"function "368<< VI.name() << " from " << FS->modulePath() << ". Previous module "369<< Local->modulePath() << "\n");370return nullptr;371}372Local = FS;373} else if (GlobalValue::isExternalLinkage(Linkage)) {374assert(IsPrevailing(VI.getGUID(), GVS.get()));375Prevailing = FS;376break;377} else if (GlobalValue::isWeakODRLinkage(Linkage) ||378GlobalValue::isLinkOnceODRLinkage(Linkage) ||379GlobalValue::isWeakAnyLinkage(Linkage) ||380GlobalValue::isLinkOnceAnyLinkage(Linkage)) {381if (IsPrevailing(VI.getGUID(), GVS.get())) {382Prevailing = FS;383break;384}385} else if (GlobalValue::isAvailableExternallyLinkage(Linkage)) {386// TODO: Handle these cases if they become meaningful387continue;388}389}390391if (Local) {392assert(!Prevailing);393CachedPrevailingSummary[VI] = Local;394} else if (Prevailing) {395assert(!Local);396CachedPrevailingSummary[VI] = Prevailing;397}398399return CachedPrevailingSummary[VI];400}401402bool llvm::thinLTOPropagateFunctionAttrs(403ModuleSummaryIndex &Index,404function_ref<bool(GlobalValue::GUID, const GlobalValueSummary *)>405IsPrevailing) {406// TODO: implement addNoAliasAttrs once407// there's more information about the return type in the summary408if (DisableThinLTOPropagation)409return false;410411DenseMap<ValueInfo, FunctionSummary *> CachedPrevailingSummary;412bool Changed = false;413414auto PropagateAttributes = [&](std::vector<ValueInfo> &SCCNodes) {415// Assume we can propagate unless we discover otherwise416FunctionSummary::FFlags InferredFlags;417InferredFlags.NoRecurse = (SCCNodes.size() == 1);418InferredFlags.NoUnwind = true;419420for (auto &V : SCCNodes) {421FunctionSummary *CallerSummary =422calculatePrevailingSummary(V, CachedPrevailingSummary, IsPrevailing);423424// Function summaries can fail to contain information such as declarations425if (!CallerSummary)426return;427428if (CallerSummary->fflags().MayThrow)429InferredFlags.NoUnwind = false;430431for (const auto &Callee : CallerSummary->calls()) {432FunctionSummary *CalleeSummary = calculatePrevailingSummary(433Callee.first, CachedPrevailingSummary, IsPrevailing);434435if (!CalleeSummary)436return;437438if (!CalleeSummary->fflags().NoRecurse)439InferredFlags.NoRecurse = false;440441if (!CalleeSummary->fflags().NoUnwind)442InferredFlags.NoUnwind = false;443444if (!InferredFlags.NoUnwind && !InferredFlags.NoRecurse)445break;446}447}448449if (InferredFlags.NoUnwind || InferredFlags.NoRecurse) {450Changed = true;451for (auto &V : SCCNodes) {452if (InferredFlags.NoRecurse) {453LLVM_DEBUG(dbgs() << "ThinLTO FunctionAttrs: Propagated NoRecurse to "454<< V.name() << "\n");455++NumThinLinkNoRecurse;456}457458if (InferredFlags.NoUnwind) {459LLVM_DEBUG(dbgs() << "ThinLTO FunctionAttrs: Propagated NoUnwind to "460<< V.name() << "\n");461++NumThinLinkNoUnwind;462}463464for (const auto &S : V.getSummaryList()) {465if (auto *FS = dyn_cast<FunctionSummary>(S.get())) {466if (InferredFlags.NoRecurse)467FS->setNoRecurse();468469if (InferredFlags.NoUnwind)470FS->setNoUnwind();471}472}473}474}475};476477// Call propagation functions on each SCC in the Index478for (scc_iterator<ModuleSummaryIndex *> I = scc_begin(&Index); !I.isAtEnd();479++I) {480std::vector<ValueInfo> Nodes(*I);481PropagateAttributes(Nodes);482}483return Changed;484}485486namespace {487488/// For a given pointer Argument, this retains a list of Arguments of functions489/// in the same SCC that the pointer data flows into. We use this to build an490/// SCC of the arguments.491struct ArgumentGraphNode {492Argument *Definition;493SmallVector<ArgumentGraphNode *, 4> Uses;494};495496class ArgumentGraph {497// We store pointers to ArgumentGraphNode objects, so it's important that498// that they not move around upon insert.499using ArgumentMapTy = std::map<Argument *, ArgumentGraphNode>;500501ArgumentMapTy ArgumentMap;502503// There is no root node for the argument graph, in fact:504// void f(int *x, int *y) { if (...) f(x, y); }505// is an example where the graph is disconnected. The SCCIterator requires a506// single entry point, so we maintain a fake ("synthetic") root node that507// uses every node. Because the graph is directed and nothing points into508// the root, it will not participate in any SCCs (except for its own).509ArgumentGraphNode SyntheticRoot;510511public:512ArgumentGraph() { SyntheticRoot.Definition = nullptr; }513514using iterator = SmallVectorImpl<ArgumentGraphNode *>::iterator;515516iterator begin() { return SyntheticRoot.Uses.begin(); }517iterator end() { return SyntheticRoot.Uses.end(); }518ArgumentGraphNode *getEntryNode() { return &SyntheticRoot; }519520ArgumentGraphNode *operator[](Argument *A) {521ArgumentGraphNode &Node = ArgumentMap[A];522Node.Definition = A;523SyntheticRoot.Uses.push_back(&Node);524return &Node;525}526};527528/// This tracker checks whether callees are in the SCC, and if so it does not529/// consider that a capture, instead adding it to the "Uses" list and530/// continuing with the analysis.531struct ArgumentUsesTracker : public CaptureTracker {532ArgumentUsesTracker(const SCCNodeSet &SCCNodes) : SCCNodes(SCCNodes) {}533534void tooManyUses() override { Captured = true; }535536bool captured(const Use *U) override {537CallBase *CB = dyn_cast<CallBase>(U->getUser());538if (!CB) {539Captured = true;540return true;541}542543Function *F = CB->getCalledFunction();544if (!F || !F->hasExactDefinition() || !SCCNodes.count(F)) {545Captured = true;546return true;547}548549assert(!CB->isCallee(U) && "callee operand reported captured?");550const unsigned UseIndex = CB->getDataOperandNo(U);551if (UseIndex >= CB->arg_size()) {552// Data operand, but not a argument operand -- must be a bundle operand553assert(CB->hasOperandBundles() && "Must be!");554555// CaptureTracking told us that we're being captured by an operand bundle556// use. In this case it does not matter if the callee is within our SCC557// or not -- we've been captured in some unknown way, and we have to be558// conservative.559Captured = true;560return true;561}562563if (UseIndex >= F->arg_size()) {564assert(F->isVarArg() && "More params than args in non-varargs call");565Captured = true;566return true;567}568569Uses.push_back(&*std::next(F->arg_begin(), UseIndex));570return false;571}572573// True only if certainly captured (used outside our SCC).574bool Captured = false;575576// Uses within our SCC.577SmallVector<Argument *, 4> Uses;578579const SCCNodeSet &SCCNodes;580};581582} // end anonymous namespace583584namespace llvm {585586template <> struct GraphTraits<ArgumentGraphNode *> {587using NodeRef = ArgumentGraphNode *;588using ChildIteratorType = SmallVectorImpl<ArgumentGraphNode *>::iterator;589590static NodeRef getEntryNode(NodeRef A) { return A; }591static ChildIteratorType child_begin(NodeRef N) { return N->Uses.begin(); }592static ChildIteratorType child_end(NodeRef N) { return N->Uses.end(); }593};594595template <>596struct GraphTraits<ArgumentGraph *> : public GraphTraits<ArgumentGraphNode *> {597static NodeRef getEntryNode(ArgumentGraph *AG) { return AG->getEntryNode(); }598599static ChildIteratorType nodes_begin(ArgumentGraph *AG) {600return AG->begin();601}602603static ChildIteratorType nodes_end(ArgumentGraph *AG) { return AG->end(); }604};605606} // end namespace llvm607608/// Returns Attribute::None, Attribute::ReadOnly or Attribute::ReadNone.609static Attribute::AttrKind610determinePointerAccessAttrs(Argument *A,611const SmallPtrSet<Argument *, 8> &SCCNodes) {612SmallVector<Use *, 32> Worklist;613SmallPtrSet<Use *, 32> Visited;614615// inalloca arguments are always clobbered by the call.616if (A->hasInAllocaAttr() || A->hasPreallocatedAttr())617return Attribute::None;618619bool IsRead = false;620bool IsWrite = false;621622for (Use &U : A->uses()) {623Visited.insert(&U);624Worklist.push_back(&U);625}626627while (!Worklist.empty()) {628if (IsWrite && IsRead)629// No point in searching further..630return Attribute::None;631632Use *U = Worklist.pop_back_val();633Instruction *I = cast<Instruction>(U->getUser());634635switch (I->getOpcode()) {636case Instruction::BitCast:637case Instruction::GetElementPtr:638case Instruction::PHI:639case Instruction::Select:640case Instruction::AddrSpaceCast:641// The original value is not read/written via this if the new value isn't.642for (Use &UU : I->uses())643if (Visited.insert(&UU).second)644Worklist.push_back(&UU);645break;646647case Instruction::Call:648case Instruction::Invoke: {649CallBase &CB = cast<CallBase>(*I);650if (CB.isCallee(U)) {651IsRead = true;652// Note that indirect calls do not capture, see comment in653// CaptureTracking for context654continue;655}656657// Given we've explictily handled the callee operand above, what's left658// must be a data operand (e.g. argument or operand bundle)659const unsigned UseIndex = CB.getDataOperandNo(U);660661// Some intrinsics (for instance ptrmask) do not capture their results,662// but return results thas alias their pointer argument, and thus should663// be handled like GEP or addrspacecast above.664if (isIntrinsicReturningPointerAliasingArgumentWithoutCapturing(665&CB, /*MustPreserveNullness=*/false)) {666for (Use &UU : CB.uses())667if (Visited.insert(&UU).second)668Worklist.push_back(&UU);669} else if (!CB.doesNotCapture(UseIndex)) {670if (!CB.onlyReadsMemory())671// If the callee can save a copy into other memory, then simply672// scanning uses of the call is insufficient. We have no way673// of tracking copies of the pointer through memory to see674// if a reloaded copy is written to, thus we must give up.675return Attribute::None;676// Push users for processing once we finish this one677if (!I->getType()->isVoidTy())678for (Use &UU : I->uses())679if (Visited.insert(&UU).second)680Worklist.push_back(&UU);681}682683ModRefInfo ArgMR = CB.getMemoryEffects().getModRef(IRMemLocation::ArgMem);684if (isNoModRef(ArgMR))685continue;686687if (Function *F = CB.getCalledFunction())688if (CB.isArgOperand(U) && UseIndex < F->arg_size() &&689SCCNodes.count(F->getArg(UseIndex)))690// This is an argument which is part of the speculative SCC. Note691// that only operands corresponding to formal arguments of the callee692// can participate in the speculation.693break;694695// The accessors used on call site here do the right thing for calls and696// invokes with operand bundles.697if (CB.doesNotAccessMemory(UseIndex)) {698/* nop */699} else if (!isModSet(ArgMR) || CB.onlyReadsMemory(UseIndex)) {700IsRead = true;701} else if (!isRefSet(ArgMR) ||702CB.dataOperandHasImpliedAttr(UseIndex, Attribute::WriteOnly)) {703IsWrite = true;704} else {705return Attribute::None;706}707break;708}709710case Instruction::Load:711// A volatile load has side effects beyond what readonly can be relied712// upon.713if (cast<LoadInst>(I)->isVolatile())714return Attribute::None;715716IsRead = true;717break;718719case Instruction::Store:720if (cast<StoreInst>(I)->getValueOperand() == *U)721// untrackable capture722return Attribute::None;723724// A volatile store has side effects beyond what writeonly can be relied725// upon.726if (cast<StoreInst>(I)->isVolatile())727return Attribute::None;728729IsWrite = true;730break;731732case Instruction::ICmp:733case Instruction::Ret:734break;735736default:737return Attribute::None;738}739}740741if (IsWrite && IsRead)742return Attribute::None;743else if (IsRead)744return Attribute::ReadOnly;745else if (IsWrite)746return Attribute::WriteOnly;747else748return Attribute::ReadNone;749}750751/// Deduce returned attributes for the SCC.752static void addArgumentReturnedAttrs(const SCCNodeSet &SCCNodes,753SmallSet<Function *, 8> &Changed) {754// Check each function in turn, determining if an argument is always returned.755for (Function *F : SCCNodes) {756// We can infer and propagate function attributes only when we know that the757// definition we'll get at link time is *exactly* the definition we see now.758// For more details, see GlobalValue::mayBeDerefined.759if (!F->hasExactDefinition())760continue;761762if (F->getReturnType()->isVoidTy())763continue;764765// There is nothing to do if an argument is already marked as 'returned'.766if (F->getAttributes().hasAttrSomewhere(Attribute::Returned))767continue;768769auto FindRetArg = [&]() -> Argument * {770Argument *RetArg = nullptr;771for (BasicBlock &BB : *F)772if (auto *Ret = dyn_cast<ReturnInst>(BB.getTerminator())) {773// Note that stripPointerCasts should look through functions with774// returned arguments.775auto *RetVal =776dyn_cast<Argument>(Ret->getReturnValue()->stripPointerCasts());777if (!RetVal || RetVal->getType() != F->getReturnType())778return nullptr;779780if (!RetArg)781RetArg = RetVal;782else if (RetArg != RetVal)783return nullptr;784}785786return RetArg;787};788789if (Argument *RetArg = FindRetArg()) {790RetArg->addAttr(Attribute::Returned);791++NumReturned;792Changed.insert(F);793}794}795}796797/// If a callsite has arguments that are also arguments to the parent function,798/// try to propagate attributes from the callsite's arguments to the parent's799/// arguments. This may be important because inlining can cause information loss800/// when attribute knowledge disappears with the inlined call.801static bool addArgumentAttrsFromCallsites(Function &F) {802if (!EnableNonnullArgPropagation)803return false;804805bool Changed = false;806807// For an argument attribute to transfer from a callsite to the parent, the808// call must be guaranteed to execute every time the parent is called.809// Conservatively, just check for calls in the entry block that are guaranteed810// to execute.811// TODO: This could be enhanced by testing if the callsite post-dominates the812// entry block or by doing simple forward walks or backward walks to the813// callsite.814BasicBlock &Entry = F.getEntryBlock();815for (Instruction &I : Entry) {816if (auto *CB = dyn_cast<CallBase>(&I)) {817if (auto *CalledFunc = CB->getCalledFunction()) {818for (auto &CSArg : CalledFunc->args()) {819if (!CSArg.hasNonNullAttr(/* AllowUndefOrPoison */ false))820continue;821822// If the non-null callsite argument operand is an argument to 'F'823// (the caller) and the call is guaranteed to execute, then the value824// must be non-null throughout 'F'.825auto *FArg = dyn_cast<Argument>(CB->getArgOperand(CSArg.getArgNo()));826if (FArg && !FArg->hasNonNullAttr()) {827FArg->addAttr(Attribute::NonNull);828Changed = true;829}830}831}832}833if (!isGuaranteedToTransferExecutionToSuccessor(&I))834break;835}836837return Changed;838}839840static bool addAccessAttr(Argument *A, Attribute::AttrKind R) {841assert((R == Attribute::ReadOnly || R == Attribute::ReadNone ||842R == Attribute::WriteOnly)843&& "Must be an access attribute.");844assert(A && "Argument must not be null.");845846// If the argument already has the attribute, nothing needs to be done.847if (A->hasAttribute(R))848return false;849850// Otherwise, remove potentially conflicting attribute, add the new one,851// and update statistics.852A->removeAttr(Attribute::WriteOnly);853A->removeAttr(Attribute::ReadOnly);854A->removeAttr(Attribute::ReadNone);855// Remove conflicting writable attribute.856if (R == Attribute::ReadNone || R == Attribute::ReadOnly)857A->removeAttr(Attribute::Writable);858A->addAttr(R);859if (R == Attribute::ReadOnly)860++NumReadOnlyArg;861else if (R == Attribute::WriteOnly)862++NumWriteOnlyArg;863else864++NumReadNoneArg;865return true;866}867868/// Deduce nocapture attributes for the SCC.869static void addArgumentAttrs(const SCCNodeSet &SCCNodes,870SmallSet<Function *, 8> &Changed) {871ArgumentGraph AG;872873// Check each function in turn, determining which pointer arguments are not874// captured.875for (Function *F : SCCNodes) {876// We can infer and propagate function attributes only when we know that the877// definition we'll get at link time is *exactly* the definition we see now.878// For more details, see GlobalValue::mayBeDerefined.879if (!F->hasExactDefinition())880continue;881882if (addArgumentAttrsFromCallsites(*F))883Changed.insert(F);884885// Functions that are readonly (or readnone) and nounwind and don't return886// a value can't capture arguments. Don't analyze them.887if (F->onlyReadsMemory() && F->doesNotThrow() &&888F->getReturnType()->isVoidTy()) {889for (Argument &A : F->args()) {890if (A.getType()->isPointerTy() && !A.hasNoCaptureAttr()) {891A.addAttr(Attribute::NoCapture);892++NumNoCapture;893Changed.insert(F);894}895}896continue;897}898899for (Argument &A : F->args()) {900if (!A.getType()->isPointerTy())901continue;902bool HasNonLocalUses = false;903if (!A.hasNoCaptureAttr()) {904ArgumentUsesTracker Tracker(SCCNodes);905PointerMayBeCaptured(&A, &Tracker);906if (!Tracker.Captured) {907if (Tracker.Uses.empty()) {908// If it's trivially not captured, mark it nocapture now.909A.addAttr(Attribute::NoCapture);910++NumNoCapture;911Changed.insert(F);912} else {913// If it's not trivially captured and not trivially not captured,914// then it must be calling into another function in our SCC. Save915// its particulars for Argument-SCC analysis later.916ArgumentGraphNode *Node = AG[&A];917for (Argument *Use : Tracker.Uses) {918Node->Uses.push_back(AG[Use]);919if (Use != &A)920HasNonLocalUses = true;921}922}923}924// Otherwise, it's captured. Don't bother doing SCC analysis on it.925}926if (!HasNonLocalUses && !A.onlyReadsMemory()) {927// Can we determine that it's readonly/readnone/writeonly without doing928// an SCC? Note that we don't allow any calls at all here, or else our929// result will be dependent on the iteration order through the930// functions in the SCC.931SmallPtrSet<Argument *, 8> Self;932Self.insert(&A);933Attribute::AttrKind R = determinePointerAccessAttrs(&A, Self);934if (R != Attribute::None)935if (addAccessAttr(&A, R))936Changed.insert(F);937}938}939}940941// The graph we've collected is partial because we stopped scanning for942// argument uses once we solved the argument trivially. These partial nodes943// show up as ArgumentGraphNode objects with an empty Uses list, and for944// these nodes the final decision about whether they capture has already been945// made. If the definition doesn't have a 'nocapture' attribute by now, it946// captures.947948for (scc_iterator<ArgumentGraph *> I = scc_begin(&AG); !I.isAtEnd(); ++I) {949const std::vector<ArgumentGraphNode *> &ArgumentSCC = *I;950if (ArgumentSCC.size() == 1) {951if (!ArgumentSCC[0]->Definition)952continue; // synthetic root node953954// eg. "void f(int* x) { if (...) f(x); }"955if (ArgumentSCC[0]->Uses.size() == 1 &&956ArgumentSCC[0]->Uses[0] == ArgumentSCC[0]) {957Argument *A = ArgumentSCC[0]->Definition;958A->addAttr(Attribute::NoCapture);959++NumNoCapture;960Changed.insert(A->getParent());961962// Infer the access attributes given the new nocapture one963SmallPtrSet<Argument *, 8> Self;964Self.insert(&*A);965Attribute::AttrKind R = determinePointerAccessAttrs(&*A, Self);966if (R != Attribute::None)967addAccessAttr(A, R);968}969continue;970}971972bool SCCCaptured = false;973for (ArgumentGraphNode *Node : ArgumentSCC) {974if (Node->Uses.empty() && !Node->Definition->hasNoCaptureAttr()) {975SCCCaptured = true;976break;977}978}979if (SCCCaptured)980continue;981982SmallPtrSet<Argument *, 8> ArgumentSCCNodes;983// Fill ArgumentSCCNodes with the elements of the ArgumentSCC. Used for984// quickly looking up whether a given Argument is in this ArgumentSCC.985for (ArgumentGraphNode *I : ArgumentSCC) {986ArgumentSCCNodes.insert(I->Definition);987}988989for (ArgumentGraphNode *N : ArgumentSCC) {990for (ArgumentGraphNode *Use : N->Uses) {991Argument *A = Use->Definition;992if (A->hasNoCaptureAttr() || ArgumentSCCNodes.count(A))993continue;994SCCCaptured = true;995break;996}997if (SCCCaptured)998break;999}1000if (SCCCaptured)1001continue;10021003for (ArgumentGraphNode *N : ArgumentSCC) {1004Argument *A = N->Definition;1005A->addAttr(Attribute::NoCapture);1006++NumNoCapture;1007Changed.insert(A->getParent());1008}10091010// We also want to compute readonly/readnone/writeonly. With a small number1011// of false negatives, we can assume that any pointer which is captured1012// isn't going to be provably readonly or readnone, since by definition1013// we can't analyze all uses of a captured pointer.1014//1015// The false negatives happen when the pointer is captured by a function1016// that promises readonly/readnone behaviour on the pointer, then the1017// pointer's lifetime ends before anything that writes to arbitrary memory.1018// Also, a readonly/readnone pointer may be returned, but returning a1019// pointer is capturing it.10201021auto meetAccessAttr = [](Attribute::AttrKind A, Attribute::AttrKind B) {1022if (A == B)1023return A;1024if (A == Attribute::ReadNone)1025return B;1026if (B == Attribute::ReadNone)1027return A;1028return Attribute::None;1029};10301031Attribute::AttrKind AccessAttr = Attribute::ReadNone;1032for (ArgumentGraphNode *N : ArgumentSCC) {1033Argument *A = N->Definition;1034Attribute::AttrKind K = determinePointerAccessAttrs(A, ArgumentSCCNodes);1035AccessAttr = meetAccessAttr(AccessAttr, K);1036if (AccessAttr == Attribute::None)1037break;1038}10391040if (AccessAttr != Attribute::None) {1041for (ArgumentGraphNode *N : ArgumentSCC) {1042Argument *A = N->Definition;1043if (addAccessAttr(A, AccessAttr))1044Changed.insert(A->getParent());1045}1046}1047}1048}10491050/// Tests whether a function is "malloc-like".1051///1052/// A function is "malloc-like" if it returns either null or a pointer that1053/// doesn't alias any other pointer visible to the caller.1054static bool isFunctionMallocLike(Function *F, const SCCNodeSet &SCCNodes) {1055SmallSetVector<Value *, 8> FlowsToReturn;1056for (BasicBlock &BB : *F)1057if (ReturnInst *Ret = dyn_cast<ReturnInst>(BB.getTerminator()))1058FlowsToReturn.insert(Ret->getReturnValue());10591060for (unsigned i = 0; i != FlowsToReturn.size(); ++i) {1061Value *RetVal = FlowsToReturn[i];10621063if (Constant *C = dyn_cast<Constant>(RetVal)) {1064if (!C->isNullValue() && !isa<UndefValue>(C))1065return false;10661067continue;1068}10691070if (isa<Argument>(RetVal))1071return false;10721073if (Instruction *RVI = dyn_cast<Instruction>(RetVal))1074switch (RVI->getOpcode()) {1075// Extend the analysis by looking upwards.1076case Instruction::BitCast:1077case Instruction::GetElementPtr:1078case Instruction::AddrSpaceCast:1079FlowsToReturn.insert(RVI->getOperand(0));1080continue;1081case Instruction::Select: {1082SelectInst *SI = cast<SelectInst>(RVI);1083FlowsToReturn.insert(SI->getTrueValue());1084FlowsToReturn.insert(SI->getFalseValue());1085continue;1086}1087case Instruction::PHI: {1088PHINode *PN = cast<PHINode>(RVI);1089for (Value *IncValue : PN->incoming_values())1090FlowsToReturn.insert(IncValue);1091continue;1092}10931094// Check whether the pointer came from an allocation.1095case Instruction::Alloca:1096break;1097case Instruction::Call:1098case Instruction::Invoke: {1099CallBase &CB = cast<CallBase>(*RVI);1100if (CB.hasRetAttr(Attribute::NoAlias))1101break;1102if (CB.getCalledFunction() && SCCNodes.count(CB.getCalledFunction()))1103break;1104[[fallthrough]];1105}1106default:1107return false; // Did not come from an allocation.1108}11091110if (PointerMayBeCaptured(RetVal, false, /*StoreCaptures=*/false))1111return false;1112}11131114return true;1115}11161117/// Deduce noalias attributes for the SCC.1118static void addNoAliasAttrs(const SCCNodeSet &SCCNodes,1119SmallSet<Function *, 8> &Changed) {1120// Check each function in turn, determining which functions return noalias1121// pointers.1122for (Function *F : SCCNodes) {1123// Already noalias.1124if (F->returnDoesNotAlias())1125continue;11261127// We can infer and propagate function attributes only when we know that the1128// definition we'll get at link time is *exactly* the definition we see now.1129// For more details, see GlobalValue::mayBeDerefined.1130if (!F->hasExactDefinition())1131return;11321133// We annotate noalias return values, which are only applicable to1134// pointer types.1135if (!F->getReturnType()->isPointerTy())1136continue;11371138if (!isFunctionMallocLike(F, SCCNodes))1139return;1140}11411142for (Function *F : SCCNodes) {1143if (F->returnDoesNotAlias() ||1144!F->getReturnType()->isPointerTy())1145continue;11461147F->setReturnDoesNotAlias();1148++NumNoAlias;1149Changed.insert(F);1150}1151}11521153/// Tests whether this function is known to not return null.1154///1155/// Requires that the function returns a pointer.1156///1157/// Returns true if it believes the function will not return a null, and sets1158/// \p Speculative based on whether the returned conclusion is a speculative1159/// conclusion due to SCC calls.1160static bool isReturnNonNull(Function *F, const SCCNodeSet &SCCNodes,1161bool &Speculative) {1162assert(F->getReturnType()->isPointerTy() &&1163"nonnull only meaningful on pointer types");1164Speculative = false;11651166SmallSetVector<Value *, 8> FlowsToReturn;1167for (BasicBlock &BB : *F)1168if (auto *Ret = dyn_cast<ReturnInst>(BB.getTerminator()))1169FlowsToReturn.insert(Ret->getReturnValue());11701171auto &DL = F->getDataLayout();11721173for (unsigned i = 0; i != FlowsToReturn.size(); ++i) {1174Value *RetVal = FlowsToReturn[i];11751176// If this value is locally known to be non-null, we're good1177if (isKnownNonZero(RetVal, DL))1178continue;11791180// Otherwise, we need to look upwards since we can't make any local1181// conclusions.1182Instruction *RVI = dyn_cast<Instruction>(RetVal);1183if (!RVI)1184return false;1185switch (RVI->getOpcode()) {1186// Extend the analysis by looking upwards.1187case Instruction::BitCast:1188case Instruction::AddrSpaceCast:1189FlowsToReturn.insert(RVI->getOperand(0));1190continue;1191case Instruction::GetElementPtr:1192if (cast<GEPOperator>(RVI)->isInBounds()) {1193FlowsToReturn.insert(RVI->getOperand(0));1194continue;1195}1196return false;1197case Instruction::Select: {1198SelectInst *SI = cast<SelectInst>(RVI);1199FlowsToReturn.insert(SI->getTrueValue());1200FlowsToReturn.insert(SI->getFalseValue());1201continue;1202}1203case Instruction::PHI: {1204PHINode *PN = cast<PHINode>(RVI);1205for (int i = 0, e = PN->getNumIncomingValues(); i != e; ++i)1206FlowsToReturn.insert(PN->getIncomingValue(i));1207continue;1208}1209case Instruction::Call:1210case Instruction::Invoke: {1211CallBase &CB = cast<CallBase>(*RVI);1212Function *Callee = CB.getCalledFunction();1213// A call to a node within the SCC is assumed to return null until1214// proven otherwise1215if (Callee && SCCNodes.count(Callee)) {1216Speculative = true;1217continue;1218}1219return false;1220}1221default:1222return false; // Unknown source, may be null1223};1224llvm_unreachable("should have either continued or returned");1225}12261227return true;1228}12291230/// Deduce nonnull attributes for the SCC.1231static void addNonNullAttrs(const SCCNodeSet &SCCNodes,1232SmallSet<Function *, 8> &Changed) {1233// Speculative that all functions in the SCC return only nonnull1234// pointers. We may refute this as we analyze functions.1235bool SCCReturnsNonNull = true;12361237// Check each function in turn, determining which functions return nonnull1238// pointers.1239for (Function *F : SCCNodes) {1240// Already nonnull.1241if (F->getAttributes().hasRetAttr(Attribute::NonNull))1242continue;12431244// We can infer and propagate function attributes only when we know that the1245// definition we'll get at link time is *exactly* the definition we see now.1246// For more details, see GlobalValue::mayBeDerefined.1247if (!F->hasExactDefinition())1248return;12491250// We annotate nonnull return values, which are only applicable to1251// pointer types.1252if (!F->getReturnType()->isPointerTy())1253continue;12541255bool Speculative = false;1256if (isReturnNonNull(F, SCCNodes, Speculative)) {1257if (!Speculative) {1258// Mark the function eagerly since we may discover a function1259// which prevents us from speculating about the entire SCC1260LLVM_DEBUG(dbgs() << "Eagerly marking " << F->getName()1261<< " as nonnull\n");1262F->addRetAttr(Attribute::NonNull);1263++NumNonNullReturn;1264Changed.insert(F);1265}1266continue;1267}1268// At least one function returns something which could be null, can't1269// speculate any more.1270SCCReturnsNonNull = false;1271}12721273if (SCCReturnsNonNull) {1274for (Function *F : SCCNodes) {1275if (F->getAttributes().hasRetAttr(Attribute::NonNull) ||1276!F->getReturnType()->isPointerTy())1277continue;12781279LLVM_DEBUG(dbgs() << "SCC marking " << F->getName() << " as nonnull\n");1280F->addRetAttr(Attribute::NonNull);1281++NumNonNullReturn;1282Changed.insert(F);1283}1284}1285}12861287/// Deduce noundef attributes for the SCC.1288static void addNoUndefAttrs(const SCCNodeSet &SCCNodes,1289SmallSet<Function *, 8> &Changed) {1290// Check each function in turn, determining which functions return noundef1291// values.1292for (Function *F : SCCNodes) {1293// Already noundef.1294AttributeList Attrs = F->getAttributes();1295if (Attrs.hasRetAttr(Attribute::NoUndef))1296continue;12971298// We can infer and propagate function attributes only when we know that the1299// definition we'll get at link time is *exactly* the definition we see now.1300// For more details, see GlobalValue::mayBeDerefined.1301if (!F->hasExactDefinition())1302return;13031304// MemorySanitizer assumes that the definition and declaration of a1305// function will be consistent. A function with sanitize_memory attribute1306// should be skipped from inference.1307if (F->hasFnAttribute(Attribute::SanitizeMemory))1308continue;13091310if (F->getReturnType()->isVoidTy())1311continue;13121313const DataLayout &DL = F->getDataLayout();1314if (all_of(*F, [&](BasicBlock &BB) {1315if (auto *Ret = dyn_cast<ReturnInst>(BB.getTerminator())) {1316// TODO: perform context-sensitive analysis?1317Value *RetVal = Ret->getReturnValue();1318if (!isGuaranteedNotToBeUndefOrPoison(RetVal))1319return false;13201321// We know the original return value is not poison now, but it1322// could still be converted to poison by another return attribute.1323// Try to explicitly re-prove the relevant attributes.1324if (Attrs.hasRetAttr(Attribute::NonNull) &&1325!isKnownNonZero(RetVal, DL))1326return false;13271328if (MaybeAlign Align = Attrs.getRetAlignment())1329if (RetVal->getPointerAlignment(DL) < *Align)1330return false;13311332Attribute Attr = Attrs.getRetAttr(Attribute::Range);1333if (Attr.isValid() &&1334!Attr.getRange().contains(1335computeConstantRange(RetVal, /*ForSigned=*/false)))1336return false;1337}1338return true;1339})) {1340F->addRetAttr(Attribute::NoUndef);1341++NumNoUndefReturn;1342Changed.insert(F);1343}1344}1345}13461347namespace {13481349/// Collects a set of attribute inference requests and performs them all in one1350/// go on a single SCC Node. Inference involves scanning function bodies1351/// looking for instructions that violate attribute assumptions.1352/// As soon as all the bodies are fine we are free to set the attribute.1353/// Customization of inference for individual attributes is performed by1354/// providing a handful of predicates for each attribute.1355class AttributeInferer {1356public:1357/// Describes a request for inference of a single attribute.1358struct InferenceDescriptor {13591360/// Returns true if this function does not have to be handled.1361/// General intent for this predicate is to provide an optimization1362/// for functions that do not need this attribute inference at all1363/// (say, for functions that already have the attribute).1364std::function<bool(const Function &)> SkipFunction;13651366/// Returns true if this instruction violates attribute assumptions.1367std::function<bool(Instruction &)> InstrBreaksAttribute;13681369/// Sets the inferred attribute for this function.1370std::function<void(Function &)> SetAttribute;13711372/// Attribute we derive.1373Attribute::AttrKind AKind;13741375/// If true, only "exact" definitions can be used to infer this attribute.1376/// See GlobalValue::isDefinitionExact.1377bool RequiresExactDefinition;13781379InferenceDescriptor(Attribute::AttrKind AK,1380std::function<bool(const Function &)> SkipFunc,1381std::function<bool(Instruction &)> InstrScan,1382std::function<void(Function &)> SetAttr,1383bool ReqExactDef)1384: SkipFunction(SkipFunc), InstrBreaksAttribute(InstrScan),1385SetAttribute(SetAttr), AKind(AK),1386RequiresExactDefinition(ReqExactDef) {}1387};13881389private:1390SmallVector<InferenceDescriptor, 4> InferenceDescriptors;13911392public:1393void registerAttrInference(InferenceDescriptor AttrInference) {1394InferenceDescriptors.push_back(AttrInference);1395}13961397void run(const SCCNodeSet &SCCNodes, SmallSet<Function *, 8> &Changed);1398};13991400/// Perform all the requested attribute inference actions according to the1401/// attribute predicates stored before.1402void AttributeInferer::run(const SCCNodeSet &SCCNodes,1403SmallSet<Function *, 8> &Changed) {1404SmallVector<InferenceDescriptor, 4> InferInSCC = InferenceDescriptors;1405// Go through all the functions in SCC and check corresponding attribute1406// assumptions for each of them. Attributes that are invalid for this SCC1407// will be removed from InferInSCC.1408for (Function *F : SCCNodes) {14091410// No attributes whose assumptions are still valid - done.1411if (InferInSCC.empty())1412return;14131414// Check if our attributes ever need scanning/can be scanned.1415llvm::erase_if(InferInSCC, [F](const InferenceDescriptor &ID) {1416if (ID.SkipFunction(*F))1417return false;14181419// Remove from further inference (invalidate) when visiting a function1420// that has no instructions to scan/has an unsuitable definition.1421return F->isDeclaration() ||1422(ID.RequiresExactDefinition && !F->hasExactDefinition());1423});14241425// For each attribute still in InferInSCC that doesn't explicitly skip F,1426// set up the F instructions scan to verify assumptions of the attribute.1427SmallVector<InferenceDescriptor, 4> InferInThisFunc;1428llvm::copy_if(1429InferInSCC, std::back_inserter(InferInThisFunc),1430[F](const InferenceDescriptor &ID) { return !ID.SkipFunction(*F); });14311432if (InferInThisFunc.empty())1433continue;14341435// Start instruction scan.1436for (Instruction &I : instructions(*F)) {1437llvm::erase_if(InferInThisFunc, [&](const InferenceDescriptor &ID) {1438if (!ID.InstrBreaksAttribute(I))1439return false;1440// Remove attribute from further inference on any other functions1441// because attribute assumptions have just been violated.1442llvm::erase_if(InferInSCC, [&ID](const InferenceDescriptor &D) {1443return D.AKind == ID.AKind;1444});1445// Remove attribute from the rest of current instruction scan.1446return true;1447});14481449if (InferInThisFunc.empty())1450break;1451}1452}14531454if (InferInSCC.empty())1455return;14561457for (Function *F : SCCNodes)1458// At this point InferInSCC contains only functions that were either:1459// - explicitly skipped from scan/inference, or1460// - verified to have no instructions that break attribute assumptions.1461// Hence we just go and force the attribute for all non-skipped functions.1462for (auto &ID : InferInSCC) {1463if (ID.SkipFunction(*F))1464continue;1465Changed.insert(F);1466ID.SetAttribute(*F);1467}1468}14691470struct SCCNodesResult {1471SCCNodeSet SCCNodes;1472bool HasUnknownCall;1473};14741475} // end anonymous namespace14761477/// Helper for non-Convergent inference predicate InstrBreaksAttribute.1478static bool InstrBreaksNonConvergent(Instruction &I,1479const SCCNodeSet &SCCNodes) {1480const CallBase *CB = dyn_cast<CallBase>(&I);1481// Breaks non-convergent assumption if CS is a convergent call to a function1482// not in the SCC.1483return CB && CB->isConvergent() &&1484!SCCNodes.contains(CB->getCalledFunction());1485}14861487/// Helper for NoUnwind inference predicate InstrBreaksAttribute.1488static bool InstrBreaksNonThrowing(Instruction &I, const SCCNodeSet &SCCNodes) {1489if (!I.mayThrow(/* IncludePhaseOneUnwind */ true))1490return false;1491if (const auto *CI = dyn_cast<CallInst>(&I)) {1492if (Function *Callee = CI->getCalledFunction()) {1493// I is a may-throw call to a function inside our SCC. This doesn't1494// invalidate our current working assumption that the SCC is no-throw; we1495// just have to scan that other function.1496if (SCCNodes.contains(Callee))1497return false;1498}1499}1500return true;1501}15021503/// Helper for NoFree inference predicate InstrBreaksAttribute.1504static bool InstrBreaksNoFree(Instruction &I, const SCCNodeSet &SCCNodes) {1505CallBase *CB = dyn_cast<CallBase>(&I);1506if (!CB)1507return false;15081509if (CB->hasFnAttr(Attribute::NoFree))1510return false;15111512// Speculatively assume in SCC.1513if (Function *Callee = CB->getCalledFunction())1514if (SCCNodes.contains(Callee))1515return false;15161517return true;1518}15191520// Return true if this is an atomic which has an ordering stronger than1521// unordered. Note that this is different than the predicate we use in1522// Attributor. Here we chose to be conservative and consider monotonic1523// operations potentially synchronizing. We generally don't do much with1524// monotonic operations, so this is simply risk reduction.1525static bool isOrderedAtomic(Instruction *I) {1526if (!I->isAtomic())1527return false;15281529if (auto *FI = dyn_cast<FenceInst>(I))1530// All legal orderings for fence are stronger than monotonic.1531return FI->getSyncScopeID() != SyncScope::SingleThread;1532else if (isa<AtomicCmpXchgInst>(I) || isa<AtomicRMWInst>(I))1533return true;1534else if (auto *SI = dyn_cast<StoreInst>(I))1535return !SI->isUnordered();1536else if (auto *LI = dyn_cast<LoadInst>(I))1537return !LI->isUnordered();1538else {1539llvm_unreachable("unknown atomic instruction?");1540}1541}15421543static bool InstrBreaksNoSync(Instruction &I, const SCCNodeSet &SCCNodes) {1544// Volatile may synchronize1545if (I.isVolatile())1546return true;15471548// An ordered atomic may synchronize. (See comment about on monotonic.)1549if (isOrderedAtomic(&I))1550return true;15511552auto *CB = dyn_cast<CallBase>(&I);1553if (!CB)1554// Non call site cases covered by the two checks above1555return false;15561557if (CB->hasFnAttr(Attribute::NoSync))1558return false;15591560// Non volatile memset/memcpy/memmoves are nosync1561// NOTE: Only intrinsics with volatile flags should be handled here. All1562// others should be marked in Intrinsics.td.1563if (auto *MI = dyn_cast<MemIntrinsic>(&I))1564if (!MI->isVolatile())1565return false;15661567// Speculatively assume in SCC.1568if (Function *Callee = CB->getCalledFunction())1569if (SCCNodes.contains(Callee))1570return false;15711572return true;1573}15741575/// Attempt to remove convergent function attribute when possible.1576///1577/// Returns true if any changes to function attributes were made.1578static void inferConvergent(const SCCNodeSet &SCCNodes,1579SmallSet<Function *, 8> &Changed) {1580AttributeInferer AI;15811582// Request to remove the convergent attribute from all functions in the SCC1583// if every callsite within the SCC is not convergent (except for calls1584// to functions within the SCC).1585// Note: Removal of the attr from the callsites will happen in1586// InstCombineCalls separately.1587AI.registerAttrInference(AttributeInferer::InferenceDescriptor{1588Attribute::Convergent,1589// Skip non-convergent functions.1590[](const Function &F) { return !F.isConvergent(); },1591// Instructions that break non-convergent assumption.1592[SCCNodes](Instruction &I) {1593return InstrBreaksNonConvergent(I, SCCNodes);1594},1595[](Function &F) {1596LLVM_DEBUG(dbgs() << "Removing convergent attr from fn " << F.getName()1597<< "\n");1598F.setNotConvergent();1599},1600/* RequiresExactDefinition= */ false});1601// Perform all the requested attribute inference actions.1602AI.run(SCCNodes, Changed);1603}16041605/// Infer attributes from all functions in the SCC by scanning every1606/// instruction for compliance to the attribute assumptions.1607///1608/// Returns true if any changes to function attributes were made.1609static void inferAttrsFromFunctionBodies(const SCCNodeSet &SCCNodes,1610SmallSet<Function *, 8> &Changed) {1611AttributeInferer AI;16121613if (!DisableNoUnwindInference)1614// Request to infer nounwind attribute for all the functions in the SCC if1615// every callsite within the SCC is not throwing (except for calls to1616// functions within the SCC). Note that nounwind attribute suffers from1617// derefinement - results may change depending on how functions are1618// optimized. Thus it can be inferred only from exact definitions.1619AI.registerAttrInference(AttributeInferer::InferenceDescriptor{1620Attribute::NoUnwind,1621// Skip non-throwing functions.1622[](const Function &F) { return F.doesNotThrow(); },1623// Instructions that break non-throwing assumption.1624[&SCCNodes](Instruction &I) {1625return InstrBreaksNonThrowing(I, SCCNodes);1626},1627[](Function &F) {1628LLVM_DEBUG(dbgs()1629<< "Adding nounwind attr to fn " << F.getName() << "\n");1630F.setDoesNotThrow();1631++NumNoUnwind;1632},1633/* RequiresExactDefinition= */ true});16341635if (!DisableNoFreeInference)1636// Request to infer nofree attribute for all the functions in the SCC if1637// every callsite within the SCC does not directly or indirectly free1638// memory (except for calls to functions within the SCC). Note that nofree1639// attribute suffers from derefinement - results may change depending on1640// how functions are optimized. Thus it can be inferred only from exact1641// definitions.1642AI.registerAttrInference(AttributeInferer::InferenceDescriptor{1643Attribute::NoFree,1644// Skip functions known not to free memory.1645[](const Function &F) { return F.doesNotFreeMemory(); },1646// Instructions that break non-deallocating assumption.1647[&SCCNodes](Instruction &I) {1648return InstrBreaksNoFree(I, SCCNodes);1649},1650[](Function &F) {1651LLVM_DEBUG(dbgs()1652<< "Adding nofree attr to fn " << F.getName() << "\n");1653F.setDoesNotFreeMemory();1654++NumNoFree;1655},1656/* RequiresExactDefinition= */ true});16571658AI.registerAttrInference(AttributeInferer::InferenceDescriptor{1659Attribute::NoSync,1660// Skip already marked functions.1661[](const Function &F) { return F.hasNoSync(); },1662// Instructions that break nosync assumption.1663[&SCCNodes](Instruction &I) {1664return InstrBreaksNoSync(I, SCCNodes);1665},1666[](Function &F) {1667LLVM_DEBUG(dbgs()1668<< "Adding nosync attr to fn " << F.getName() << "\n");1669F.setNoSync();1670++NumNoSync;1671},1672/* RequiresExactDefinition= */ true});16731674// Perform all the requested attribute inference actions.1675AI.run(SCCNodes, Changed);1676}16771678static void addNoRecurseAttrs(const SCCNodeSet &SCCNodes,1679SmallSet<Function *, 8> &Changed) {1680// Try and identify functions that do not recurse.16811682// If the SCC contains multiple nodes we know for sure there is recursion.1683if (SCCNodes.size() != 1)1684return;16851686Function *F = *SCCNodes.begin();1687if (!F || !F->hasExactDefinition() || F->doesNotRecurse())1688return;16891690// If all of the calls in F are identifiable and are to norecurse functions, F1691// is norecurse. This check also detects self-recursion as F is not currently1692// marked norecurse, so any called from F to F will not be marked norecurse.1693for (auto &BB : *F)1694for (auto &I : BB.instructionsWithoutDebug())1695if (auto *CB = dyn_cast<CallBase>(&I)) {1696Function *Callee = CB->getCalledFunction();1697if (!Callee || Callee == F ||1698(!Callee->doesNotRecurse() &&1699!(Callee->isDeclaration() &&1700Callee->hasFnAttribute(Attribute::NoCallback))))1701// Function calls a potentially recursive function.1702return;1703}17041705// Every call was to a non-recursive function other than this function, and1706// we have no indirect recursion as the SCC size is one. This function cannot1707// recurse.1708F->setDoesNotRecurse();1709++NumNoRecurse;1710Changed.insert(F);1711}17121713static bool instructionDoesNotReturn(Instruction &I) {1714if (auto *CB = dyn_cast<CallBase>(&I))1715return CB->hasFnAttr(Attribute::NoReturn);1716return false;1717}17181719// A basic block can only return if it terminates with a ReturnInst and does not1720// contain calls to noreturn functions.1721static bool basicBlockCanReturn(BasicBlock &BB) {1722if (!isa<ReturnInst>(BB.getTerminator()))1723return false;1724return none_of(BB, instructionDoesNotReturn);1725}17261727// FIXME: this doesn't handle recursion.1728static bool canReturn(Function &F) {1729SmallVector<BasicBlock *, 16> Worklist;1730SmallPtrSet<BasicBlock *, 16> Visited;17311732Visited.insert(&F.front());1733Worklist.push_back(&F.front());17341735do {1736BasicBlock *BB = Worklist.pop_back_val();1737if (basicBlockCanReturn(*BB))1738return true;1739for (BasicBlock *Succ : successors(BB))1740if (Visited.insert(Succ).second)1741Worklist.push_back(Succ);1742} while (!Worklist.empty());17431744return false;1745}17461747// Set the noreturn function attribute if possible.1748static void addNoReturnAttrs(const SCCNodeSet &SCCNodes,1749SmallSet<Function *, 8> &Changed) {1750for (Function *F : SCCNodes) {1751if (!F || !F->hasExactDefinition() || F->hasFnAttribute(Attribute::Naked) ||1752F->doesNotReturn())1753continue;17541755if (!canReturn(*F)) {1756F->setDoesNotReturn();1757Changed.insert(F);1758}1759}1760}17611762static bool functionWillReturn(const Function &F) {1763// We can infer and propagate function attributes only when we know that the1764// definition we'll get at link time is *exactly* the definition we see now.1765// For more details, see GlobalValue::mayBeDerefined.1766if (!F.hasExactDefinition())1767return false;17681769// Must-progress function without side-effects must return.1770if (F.mustProgress() && F.onlyReadsMemory())1771return true;17721773// Can only analyze functions with a definition.1774if (F.isDeclaration())1775return false;17761777// Functions with loops require more sophisticated analysis, as the loop1778// may be infinite. For now, don't try to handle them.1779SmallVector<std::pair<const BasicBlock *, const BasicBlock *>> Backedges;1780FindFunctionBackedges(F, Backedges);1781if (!Backedges.empty())1782return false;17831784// If there are no loops, then the function is willreturn if all calls in1785// it are willreturn.1786return all_of(instructions(F), [](const Instruction &I) {1787return I.willReturn();1788});1789}17901791// Set the willreturn function attribute if possible.1792static void addWillReturn(const SCCNodeSet &SCCNodes,1793SmallSet<Function *, 8> &Changed) {1794for (Function *F : SCCNodes) {1795if (!F || F->willReturn() || !functionWillReturn(*F))1796continue;17971798F->setWillReturn();1799NumWillReturn++;1800Changed.insert(F);1801}1802}18031804static SCCNodesResult createSCCNodeSet(ArrayRef<Function *> Functions) {1805SCCNodesResult Res;1806Res.HasUnknownCall = false;1807for (Function *F : Functions) {1808if (!F || F->hasOptNone() || F->hasFnAttribute(Attribute::Naked) ||1809F->isPresplitCoroutine()) {1810// Treat any function we're trying not to optimize as if it were an1811// indirect call and omit it from the node set used below.1812Res.HasUnknownCall = true;1813continue;1814}1815// Track whether any functions in this SCC have an unknown call edge.1816// Note: if this is ever a performance hit, we can common it with1817// subsequent routines which also do scans over the instructions of the1818// function.1819if (!Res.HasUnknownCall) {1820for (Instruction &I : instructions(*F)) {1821if (auto *CB = dyn_cast<CallBase>(&I)) {1822if (!CB->getCalledFunction()) {1823Res.HasUnknownCall = true;1824break;1825}1826}1827}1828}1829Res.SCCNodes.insert(F);1830}1831return Res;1832}18331834template <typename AARGetterT>1835static SmallSet<Function *, 8>1836deriveAttrsInPostOrder(ArrayRef<Function *> Functions, AARGetterT &&AARGetter,1837bool ArgAttrsOnly) {1838SCCNodesResult Nodes = createSCCNodeSet(Functions);18391840// Bail if the SCC only contains optnone functions.1841if (Nodes.SCCNodes.empty())1842return {};18431844SmallSet<Function *, 8> Changed;1845if (ArgAttrsOnly) {1846addArgumentAttrs(Nodes.SCCNodes, Changed);1847return Changed;1848}18491850addArgumentReturnedAttrs(Nodes.SCCNodes, Changed);1851addMemoryAttrs(Nodes.SCCNodes, AARGetter, Changed);1852addArgumentAttrs(Nodes.SCCNodes, Changed);1853inferConvergent(Nodes.SCCNodes, Changed);1854addNoReturnAttrs(Nodes.SCCNodes, Changed);1855addWillReturn(Nodes.SCCNodes, Changed);1856addNoUndefAttrs(Nodes.SCCNodes, Changed);18571858// If we have no external nodes participating in the SCC, we can deduce some1859// more precise attributes as well.1860if (!Nodes.HasUnknownCall) {1861addNoAliasAttrs(Nodes.SCCNodes, Changed);1862addNonNullAttrs(Nodes.SCCNodes, Changed);1863inferAttrsFromFunctionBodies(Nodes.SCCNodes, Changed);1864addNoRecurseAttrs(Nodes.SCCNodes, Changed);1865}18661867// Finally, infer the maximal set of attributes from the ones we've inferred1868// above. This is handling the cases where one attribute on a signature1869// implies another, but for implementation reasons the inference rule for1870// the later is missing (or simply less sophisticated).1871for (Function *F : Nodes.SCCNodes)1872if (F)1873if (inferAttributesFromOthers(*F))1874Changed.insert(F);18751876return Changed;1877}18781879PreservedAnalyses PostOrderFunctionAttrsPass::run(LazyCallGraph::SCC &C,1880CGSCCAnalysisManager &AM,1881LazyCallGraph &CG,1882CGSCCUpdateResult &) {1883// Skip non-recursive functions if requested.1884// Only infer argument attributes for non-recursive functions, because1885// it can affect optimization behavior in conjunction with noalias.1886bool ArgAttrsOnly = false;1887if (C.size() == 1 && SkipNonRecursive) {1888LazyCallGraph::Node &N = *C.begin();1889if (!N->lookup(N))1890ArgAttrsOnly = true;1891}18921893FunctionAnalysisManager &FAM =1894AM.getResult<FunctionAnalysisManagerCGSCCProxy>(C, CG).getManager();18951896// We pass a lambda into functions to wire them up to the analysis manager1897// for getting function analyses.1898auto AARGetter = [&](Function &F) -> AAResults & {1899return FAM.getResult<AAManager>(F);1900};19011902SmallVector<Function *, 8> Functions;1903for (LazyCallGraph::Node &N : C) {1904Functions.push_back(&N.getFunction());1905}19061907auto ChangedFunctions =1908deriveAttrsInPostOrder(Functions, AARGetter, ArgAttrsOnly);1909if (ChangedFunctions.empty())1910return PreservedAnalyses::all();19111912// Invalidate analyses for modified functions so that we don't have to1913// invalidate all analyses for all functions in this SCC.1914PreservedAnalyses FuncPA;1915// We haven't changed the CFG for modified functions.1916FuncPA.preserveSet<CFGAnalyses>();1917for (Function *Changed : ChangedFunctions) {1918FAM.invalidate(*Changed, FuncPA);1919// Also invalidate any direct callers of changed functions since analyses1920// may care about attributes of direct callees. For example, MemorySSA cares1921// about whether or not a call's callee modifies memory and queries that1922// through function attributes.1923for (auto *U : Changed->users()) {1924if (auto *Call = dyn_cast<CallBase>(U)) {1925if (Call->getCalledFunction() == Changed)1926FAM.invalidate(*Call->getFunction(), FuncPA);1927}1928}1929}19301931PreservedAnalyses PA;1932// We have not added or removed functions.1933PA.preserve<FunctionAnalysisManagerCGSCCProxy>();1934// We already invalidated all relevant function analyses above.1935PA.preserveSet<AllAnalysesOn<Function>>();1936return PA;1937}19381939void PostOrderFunctionAttrsPass::printPipeline(1940raw_ostream &OS, function_ref<StringRef(StringRef)> MapClassName2PassName) {1941static_cast<PassInfoMixin<PostOrderFunctionAttrsPass> *>(this)->printPipeline(1942OS, MapClassName2PassName);1943if (SkipNonRecursive)1944OS << "<skip-non-recursive-function-attrs>";1945}19461947template <typename AARGetterT>1948static bool runImpl(CallGraphSCC &SCC, AARGetterT AARGetter) {1949SmallVector<Function *, 8> Functions;1950for (CallGraphNode *I : SCC) {1951Functions.push_back(I->getFunction());1952}19531954return !deriveAttrsInPostOrder(Functions, AARGetter).empty();1955}19561957static bool addNoRecurseAttrsTopDown(Function &F) {1958// We check the preconditions for the function prior to calling this to avoid1959// the cost of building up a reversible post-order list. We assert them here1960// to make sure none of the invariants this relies on were violated.1961assert(!F.isDeclaration() && "Cannot deduce norecurse without a definition!");1962assert(!F.doesNotRecurse() &&1963"This function has already been deduced as norecurs!");1964assert(F.hasInternalLinkage() &&1965"Can only do top-down deduction for internal linkage functions!");19661967// If F is internal and all of its uses are calls from a non-recursive1968// functions, then none of its calls could in fact recurse without going1969// through a function marked norecurse, and so we can mark this function too1970// as norecurse. Note that the uses must actually be calls -- otherwise1971// a pointer to this function could be returned from a norecurse function but1972// this function could be recursively (indirectly) called. Note that this1973// also detects if F is directly recursive as F is not yet marked as1974// a norecurse function.1975for (auto &U : F.uses()) {1976auto *I = dyn_cast<Instruction>(U.getUser());1977if (!I)1978return false;1979CallBase *CB = dyn_cast<CallBase>(I);1980if (!CB || !CB->isCallee(&U) ||1981!CB->getParent()->getParent()->doesNotRecurse())1982return false;1983}1984F.setDoesNotRecurse();1985++NumNoRecurse;1986return true;1987}19881989static bool deduceFunctionAttributeInRPO(Module &M, LazyCallGraph &CG) {1990// We only have a post-order SCC traversal (because SCCs are inherently1991// discovered in post-order), so we accumulate them in a vector and then walk1992// it in reverse. This is simpler than using the RPO iterator infrastructure1993// because we need to combine SCC detection and the PO walk of the call1994// graph. We can also cheat egregiously because we're primarily interested in1995// synthesizing norecurse and so we can only save the singular SCCs as SCCs1996// with multiple functions in them will clearly be recursive.19971998SmallVector<Function *, 16> Worklist;1999CG.buildRefSCCs();2000for (LazyCallGraph::RefSCC &RC : CG.postorder_ref_sccs()) {2001for (LazyCallGraph::SCC &SCC : RC) {2002if (SCC.size() != 1)2003continue;2004Function &F = SCC.begin()->getFunction();2005if (!F.isDeclaration() && !F.doesNotRecurse() && F.hasInternalLinkage())2006Worklist.push_back(&F);2007}2008}2009bool Changed = false;2010for (auto *F : llvm::reverse(Worklist))2011Changed |= addNoRecurseAttrsTopDown(*F);20122013return Changed;2014}20152016PreservedAnalyses2017ReversePostOrderFunctionAttrsPass::run(Module &M, ModuleAnalysisManager &AM) {2018auto &CG = AM.getResult<LazyCallGraphAnalysis>(M);20192020if (!deduceFunctionAttributeInRPO(M, CG))2021return PreservedAnalyses::all();20222023PreservedAnalyses PA;2024PA.preserve<LazyCallGraphAnalysis>();2025return PA;2026}202720282029