1 //===- FunctionAttrs.cpp - Pass which marks functions attributes ----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements a simple interprocedural pass which walks the
11 // call-graph, looking for functions which do not access or only read
12 // non-local memory, and marking them readnone/readonly. It does the
13 // same with function arguments independently, marking them readonly/
14 // readnone/nocapture. Finally, well-known library call declarations
15 // are marked with all attributes that are consistent with the
16 // function's standard definition. This pass is implemented as a
17 // bottom-up traversal of the call-graph.
19 //===----------------------------------------------------------------------===//
21 #include "llvm/Transforms/IPO.h"
22 #include "llvm/ADT/SCCIterator.h"
23 #include "llvm/ADT/SetVector.h"
24 #include "llvm/ADT/SmallSet.h"
25 #include "llvm/ADT/Statistic.h"
26 #include "llvm/ADT/StringSwitch.h"
27 #include "llvm/Analysis/AliasAnalysis.h"
28 #include "llvm/Analysis/AssumptionCache.h"
29 #include "llvm/Analysis/BasicAliasAnalysis.h"
30 #include "llvm/Analysis/CallGraph.h"
31 #include "llvm/Analysis/CallGraphSCCPass.h"
32 #include "llvm/Analysis/CaptureTracking.h"
33 #include "llvm/Analysis/TargetLibraryInfo.h"
34 #include "llvm/Analysis/ValueTracking.h"
35 #include "llvm/IR/GlobalVariable.h"
36 #include "llvm/IR/InstIterator.h"
37 #include "llvm/IR/IntrinsicInst.h"
38 #include "llvm/IR/LLVMContext.h"
39 #include "llvm/Support/Debug.h"
40 #include "llvm/Support/raw_ostream.h"
41 #include "llvm/Analysis/TargetLibraryInfo.h"
44 #define DEBUG_TYPE "functionattrs"
46 STATISTIC(NumReadNone, "Number of functions marked readnone");
47 STATISTIC(NumReadOnly, "Number of functions marked readonly");
48 STATISTIC(NumNoCapture, "Number of arguments marked nocapture");
49 STATISTIC(NumReadNoneArg, "Number of arguments marked readnone");
50 STATISTIC(NumReadOnlyArg, "Number of arguments marked readonly");
51 STATISTIC(NumNoAlias, "Number of function returns marked noalias");
52 STATISTIC(NumNonNullReturn, "Number of function returns marked nonnull");
53 STATISTIC(NumNoRecurse, "Number of functions marked as norecurse");
56 typedef SmallSetVector<Function *, 8> SCCNodeSet;
60 struct FunctionAttrs : public CallGraphSCCPass {
61 static char ID; // Pass identification, replacement for typeid
62 FunctionAttrs() : CallGraphSCCPass(ID) {
63 initializeFunctionAttrsPass(*PassRegistry::getPassRegistry());
66 bool runOnSCC(CallGraphSCC &SCC) override;
67 bool doInitialization(CallGraph &CG) override {
71 bool doFinalization(CallGraph &CG) override;
73 void getAnalysisUsage(AnalysisUsage &AU) const override {
75 AU.addRequired<AssumptionCacheTracker>();
76 AU.addRequired<TargetLibraryInfoWrapperPass>();
77 CallGraphSCCPass::getAnalysisUsage(AU);
81 TargetLibraryInfo *TLI;
82 SmallVector<WeakVH,16> Revisit;
86 char FunctionAttrs::ID = 0;
87 INITIALIZE_PASS_BEGIN(FunctionAttrs, "functionattrs",
88 "Deduce function attributes", false, false)
89 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
90 INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
91 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
92 INITIALIZE_PASS_END(FunctionAttrs, "functionattrs",
93 "Deduce function attributes", false, false)
95 Pass *llvm::createFunctionAttrsPass() { return new FunctionAttrs(); }
98 /// The three kinds of memory access relevant to 'readonly' and
99 /// 'readnone' attributes.
100 enum MemoryAccessKind {
107 static MemoryAccessKind checkFunctionMemoryAccess(Function &F, AAResults &AAR,
108 const SCCNodeSet &SCCNodes) {
109 FunctionModRefBehavior MRB = AAR.getModRefBehavior(&F);
110 if (MRB == FMRB_DoesNotAccessMemory)
114 // Definitions with weak linkage may be overridden at linktime with
115 // something that writes memory, so treat them like declarations.
116 if (F.isDeclaration() || F.mayBeOverridden()) {
117 if (AliasAnalysis::onlyReadsMemory(MRB))
120 // Conservatively assume it writes to memory.
124 // Scan the function body for instructions that may read or write memory.
125 bool ReadsMemory = false;
126 for (inst_iterator II = inst_begin(F), E = inst_end(F); II != E; ++II) {
127 Instruction *I = &*II;
129 // Some instructions can be ignored even if they read or write memory.
130 // Detect these now, skipping to the next instruction if one is found.
131 CallSite CS(cast<Value>(I));
133 // Ignore calls to functions in the same SCC.
134 if (CS.getCalledFunction() && SCCNodes.count(CS.getCalledFunction()))
136 FunctionModRefBehavior MRB = AAR.getModRefBehavior(CS);
138 // If the call doesn't access memory, we're done.
139 if (!(MRB & MRI_ModRef))
142 if (!AliasAnalysis::onlyAccessesArgPointees(MRB)) {
143 // The call could access any memory. If that includes writes, give up.
146 // If it reads, note it.
152 // Check whether all pointer arguments point to local memory, and
153 // ignore calls that only access local memory.
154 for (CallSite::arg_iterator CI = CS.arg_begin(), CE = CS.arg_end();
157 if (!Arg->getType()->isPtrOrPtrVectorTy())
161 I->getAAMetadata(AAInfo);
162 MemoryLocation Loc(Arg, MemoryLocation::UnknownSize, AAInfo);
164 // Skip accesses to local or constant memory as they don't impact the
165 // externally visible mod/ref behavior.
166 if (AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true))
170 // Writes non-local memory. Give up.
173 // Ok, it reads non-local memory.
177 } else if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
178 // Ignore non-volatile loads from local memory. (Atomic is okay here.)
179 if (!LI->isVolatile()) {
180 MemoryLocation Loc = MemoryLocation::get(LI);
181 if (AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true))
184 } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
185 // Ignore non-volatile stores to local memory. (Atomic is okay here.)
186 if (!SI->isVolatile()) {
187 MemoryLocation Loc = MemoryLocation::get(SI);
188 if (AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true))
191 } else if (VAArgInst *VI = dyn_cast<VAArgInst>(I)) {
192 // Ignore vaargs on local memory.
193 MemoryLocation Loc = MemoryLocation::get(VI);
194 if (AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true))
198 // Any remaining instructions need to be taken seriously! Check if they
199 // read or write memory.
200 if (I->mayWriteToMemory())
201 // Writes memory. Just give up.
204 // If this instruction may read memory, remember that.
205 ReadsMemory |= I->mayReadFromMemory();
208 return ReadsMemory ? MAK_ReadOnly : MAK_ReadNone;
211 /// Deduce readonly/readnone attributes for the SCC.
212 template <typename AARGetterT>
213 static bool addReadAttrs(const SCCNodeSet &SCCNodes, AARGetterT AARGetter) {
214 // Check if any of the functions in the SCC read or write memory. If they
215 // write memory then they can't be marked readnone or readonly.
216 bool ReadsMemory = false;
217 for (Function *F : SCCNodes) {
218 // Call the callable parameter to look up AA results for this function.
219 AAResults &AAR = AARGetter(*F);
221 switch (checkFunctionMemoryAccess(*F, AAR, SCCNodes)) {
233 // Success! Functions in this SCC do not access memory, or only read memory.
234 // Give them the appropriate attribute.
235 bool MadeChange = false;
236 for (Function *F : SCCNodes) {
237 if (F->doesNotAccessMemory())
241 if (F->onlyReadsMemory() && ReadsMemory)
247 // Clear out any existing attributes.
249 B.addAttribute(Attribute::ReadOnly).addAttribute(Attribute::ReadNone);
251 AttributeSet::FunctionIndex,
252 AttributeSet::get(F->getContext(), AttributeSet::FunctionIndex, B));
254 // Add in the new attribute.
255 F->addAttribute(AttributeSet::FunctionIndex,
256 ReadsMemory ? Attribute::ReadOnly : Attribute::ReadNone);
268 /// For a given pointer Argument, this retains a list of Arguments of functions
269 /// in the same SCC that the pointer data flows into. We use this to build an
270 /// SCC of the arguments.
271 struct ArgumentGraphNode {
272 Argument *Definition;
273 SmallVector<ArgumentGraphNode *, 4> Uses;
276 class ArgumentGraph {
277 // We store pointers to ArgumentGraphNode objects, so it's important that
278 // that they not move around upon insert.
279 typedef std::map<Argument *, ArgumentGraphNode> ArgumentMapTy;
281 ArgumentMapTy ArgumentMap;
283 // There is no root node for the argument graph, in fact:
284 // void f(int *x, int *y) { if (...) f(x, y); }
285 // is an example where the graph is disconnected. The SCCIterator requires a
286 // single entry point, so we maintain a fake ("synthetic") root node that
287 // uses every node. Because the graph is directed and nothing points into
288 // the root, it will not participate in any SCCs (except for its own).
289 ArgumentGraphNode SyntheticRoot;
292 ArgumentGraph() { SyntheticRoot.Definition = nullptr; }
294 typedef SmallVectorImpl<ArgumentGraphNode *>::iterator iterator;
296 iterator begin() { return SyntheticRoot.Uses.begin(); }
297 iterator end() { return SyntheticRoot.Uses.end(); }
298 ArgumentGraphNode *getEntryNode() { return &SyntheticRoot; }
300 ArgumentGraphNode *operator[](Argument *A) {
301 ArgumentGraphNode &Node = ArgumentMap[A];
303 SyntheticRoot.Uses.push_back(&Node);
308 /// This tracker checks whether callees are in the SCC, and if so it does not
309 /// consider that a capture, instead adding it to the "Uses" list and
310 /// continuing with the analysis.
311 struct ArgumentUsesTracker : public CaptureTracker {
312 ArgumentUsesTracker(const SCCNodeSet &SCCNodes)
313 : Captured(false), SCCNodes(SCCNodes) {}
315 void tooManyUses() override { Captured = true; }
317 bool captured(const Use *U) override {
318 CallSite CS(U->getUser());
319 if (!CS.getInstruction()) {
324 Function *F = CS.getCalledFunction();
325 if (!F || F->isDeclaration() || F->mayBeOverridden() ||
326 !SCCNodes.count(F)) {
331 // Note: the callee and the two successor blocks *follow* the argument
332 // operands. This means there is no need to adjust UseIndex to account for
336 std::distance(const_cast<const Use *>(CS.arg_begin()), U);
338 assert(UseIndex < CS.data_operands_size() &&
339 "Indirect function calls should have been filtered above!");
341 if (UseIndex >= CS.getNumArgOperands()) {
342 // Data operand, but not a argument operand -- must be a bundle operand
343 assert(CS.hasOperandBundles() && "Must be!");
345 // CaptureTracking told us that we're being captured by an operand bundle
346 // use. In this case it does not matter if the callee is within our SCC
347 // or not -- we've been captured in some unknown way, and we have to be
353 if (UseIndex >= F->arg_size()) {
354 assert(F->isVarArg() && "More params than args in non-varargs call");
359 Uses.push_back(&*std::next(F->arg_begin(), UseIndex));
363 bool Captured; // True only if certainly captured (used outside our SCC).
364 SmallVector<Argument *, 4> Uses; // Uses within our SCC.
366 const SCCNodeSet &SCCNodes;
371 template <> struct GraphTraits<ArgumentGraphNode *> {
372 typedef ArgumentGraphNode NodeType;
373 typedef SmallVectorImpl<ArgumentGraphNode *>::iterator ChildIteratorType;
375 static inline NodeType *getEntryNode(NodeType *A) { return A; }
376 static inline ChildIteratorType child_begin(NodeType *N) {
377 return N->Uses.begin();
379 static inline ChildIteratorType child_end(NodeType *N) {
380 return N->Uses.end();
384 struct GraphTraits<ArgumentGraph *> : public GraphTraits<ArgumentGraphNode *> {
385 static NodeType *getEntryNode(ArgumentGraph *AG) {
386 return AG->getEntryNode();
388 static ChildIteratorType nodes_begin(ArgumentGraph *AG) {
391 static ChildIteratorType nodes_end(ArgumentGraph *AG) { return AG->end(); }
395 /// Returns Attribute::None, Attribute::ReadOnly or Attribute::ReadNone.
396 static Attribute::AttrKind
397 determinePointerReadAttrs(Argument *A,
398 const SmallPtrSet<Argument *, 8> &SCCNodes) {
400 SmallVector<Use *, 32> Worklist;
401 SmallSet<Use *, 32> Visited;
403 // inalloca arguments are always clobbered by the call.
404 if (A->hasInAllocaAttr())
405 return Attribute::None;
408 // We don't need to track IsWritten. If A is written to, return immediately.
410 for (Use &U : A->uses()) {
412 Worklist.push_back(&U);
415 while (!Worklist.empty()) {
416 Use *U = Worklist.pop_back_val();
417 Instruction *I = cast<Instruction>(U->getUser());
419 switch (I->getOpcode()) {
420 case Instruction::BitCast:
421 case Instruction::GetElementPtr:
422 case Instruction::PHI:
423 case Instruction::Select:
424 case Instruction::AddrSpaceCast:
425 // The original value is not read/written via this if the new value isn't.
426 for (Use &UU : I->uses())
427 if (Visited.insert(&UU).second)
428 Worklist.push_back(&UU);
431 case Instruction::Call:
432 case Instruction::Invoke: {
433 bool Captures = true;
435 if (I->getType()->isVoidTy())
438 auto AddUsersToWorklistIfCapturing = [&] {
440 for (Use &UU : I->uses())
441 if (Visited.insert(&UU).second)
442 Worklist.push_back(&UU);
446 if (CS.doesNotAccessMemory()) {
447 AddUsersToWorklistIfCapturing();
451 Function *F = CS.getCalledFunction();
453 if (CS.onlyReadsMemory()) {
455 AddUsersToWorklistIfCapturing();
458 return Attribute::None;
461 // Note: the callee and the two successor blocks *follow* the argument
462 // operands. This means there is no need to adjust UseIndex to account
465 unsigned UseIndex = std::distance(CS.arg_begin(), U);
467 // U cannot be the callee operand use: since we're exploring the
468 // transitive uses of an Argument, having such a use be a callee would
469 // imply the CallSite is an indirect call or invoke; and we'd take the
471 assert(UseIndex < CS.data_operands_size() &&
472 "Data operand use expected!");
474 bool IsOperandBundleUse = UseIndex >= CS.getNumArgOperands();
476 if (UseIndex >= F->arg_size() && !IsOperandBundleUse) {
477 assert(F->isVarArg() && "More params than args in non-varargs call");
478 return Attribute::None;
481 Captures &= !CS.doesNotCapture(UseIndex);
483 // Since the optimizer (by design) cannot see the data flow corresponding
484 // to a operand bundle use, these cannot participate in the optimistic SCC
485 // analysis. Instead, we model the operand bundle uses as arguments in
486 // call to a function external to the SCC.
487 if (!SCCNodes.count(&*std::next(F->arg_begin(), UseIndex)) ||
488 IsOperandBundleUse) {
490 // The accessors used on CallSite here do the right thing for calls and
491 // invokes with operand bundles.
493 if (!CS.onlyReadsMemory() && !CS.onlyReadsMemory(UseIndex))
494 return Attribute::None;
495 if (!CS.doesNotAccessMemory(UseIndex))
499 AddUsersToWorklistIfCapturing();
503 case Instruction::Load:
507 case Instruction::ICmp:
508 case Instruction::Ret:
512 return Attribute::None;
516 return IsRead ? Attribute::ReadOnly : Attribute::ReadNone;
519 /// Deduce nocapture attributes for the SCC.
520 static bool addArgumentAttrs(const SCCNodeSet &SCCNodes) {
521 bool Changed = false;
526 B.addAttribute(Attribute::NoCapture);
528 // Check each function in turn, determining which pointer arguments are not
530 for (Function *F : SCCNodes) {
531 // Definitions with weak linkage may be overridden at linktime with
532 // something that captures pointers, so treat them like declarations.
533 if (F->isDeclaration() || F->mayBeOverridden())
536 // Functions that are readonly (or readnone) and nounwind and don't return
537 // a value can't capture arguments. Don't analyze them.
538 if (F->onlyReadsMemory() && F->doesNotThrow() &&
539 F->getReturnType()->isVoidTy()) {
540 for (Function::arg_iterator A = F->arg_begin(), E = F->arg_end(); A != E;
542 if (A->getType()->isPointerTy() && !A->hasNoCaptureAttr()) {
543 A->addAttr(AttributeSet::get(F->getContext(), A->getArgNo() + 1, B));
551 for (Function::arg_iterator A = F->arg_begin(), E = F->arg_end(); A != E;
553 if (!A->getType()->isPointerTy())
555 bool HasNonLocalUses = false;
556 if (!A->hasNoCaptureAttr()) {
557 ArgumentUsesTracker Tracker(SCCNodes);
558 PointerMayBeCaptured(&*A, &Tracker);
559 if (!Tracker.Captured) {
560 if (Tracker.Uses.empty()) {
561 // If it's trivially not captured, mark it nocapture now.
563 AttributeSet::get(F->getContext(), A->getArgNo() + 1, B));
567 // If it's not trivially captured and not trivially not captured,
568 // then it must be calling into another function in our SCC. Save
569 // its particulars for Argument-SCC analysis later.
570 ArgumentGraphNode *Node = AG[&*A];
571 for (SmallVectorImpl<Argument *>::iterator
572 UI = Tracker.Uses.begin(),
573 UE = Tracker.Uses.end();
575 Node->Uses.push_back(AG[*UI]);
577 HasNonLocalUses = true;
581 // Otherwise, it's captured. Don't bother doing SCC analysis on it.
583 if (!HasNonLocalUses && !A->onlyReadsMemory()) {
584 // Can we determine that it's readonly/readnone without doing an SCC?
585 // Note that we don't allow any calls at all here, or else our result
586 // will be dependent on the iteration order through the functions in the
588 SmallPtrSet<Argument *, 8> Self;
590 Attribute::AttrKind R = determinePointerReadAttrs(&*A, Self);
591 if (R != Attribute::None) {
594 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
596 R == Attribute::ReadOnly ? ++NumReadOnlyArg : ++NumReadNoneArg;
602 // The graph we've collected is partial because we stopped scanning for
603 // argument uses once we solved the argument trivially. These partial nodes
604 // show up as ArgumentGraphNode objects with an empty Uses list, and for
605 // these nodes the final decision about whether they capture has already been
606 // made. If the definition doesn't have a 'nocapture' attribute by now, it
609 for (scc_iterator<ArgumentGraph *> I = scc_begin(&AG); !I.isAtEnd(); ++I) {
610 const std::vector<ArgumentGraphNode *> &ArgumentSCC = *I;
611 if (ArgumentSCC.size() == 1) {
612 if (!ArgumentSCC[0]->Definition)
613 continue; // synthetic root node
615 // eg. "void f(int* x) { if (...) f(x); }"
616 if (ArgumentSCC[0]->Uses.size() == 1 &&
617 ArgumentSCC[0]->Uses[0] == ArgumentSCC[0]) {
618 Argument *A = ArgumentSCC[0]->Definition;
619 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
626 bool SCCCaptured = false;
627 for (auto I = ArgumentSCC.begin(), E = ArgumentSCC.end();
628 I != E && !SCCCaptured; ++I) {
629 ArgumentGraphNode *Node = *I;
630 if (Node->Uses.empty()) {
631 if (!Node->Definition->hasNoCaptureAttr())
638 SmallPtrSet<Argument *, 8> ArgumentSCCNodes;
639 // Fill ArgumentSCCNodes with the elements of the ArgumentSCC. Used for
640 // quickly looking up whether a given Argument is in this ArgumentSCC.
641 for (auto I = ArgumentSCC.begin(), E = ArgumentSCC.end(); I != E; ++I) {
642 ArgumentSCCNodes.insert((*I)->Definition);
645 for (auto I = ArgumentSCC.begin(), E = ArgumentSCC.end();
646 I != E && !SCCCaptured; ++I) {
647 ArgumentGraphNode *N = *I;
648 for (SmallVectorImpl<ArgumentGraphNode *>::iterator UI = N->Uses.begin(),
651 Argument *A = (*UI)->Definition;
652 if (A->hasNoCaptureAttr() || ArgumentSCCNodes.count(A))
661 for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) {
662 Argument *A = ArgumentSCC[i]->Definition;
663 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
668 // We also want to compute readonly/readnone. With a small number of false
669 // negatives, we can assume that any pointer which is captured isn't going
670 // to be provably readonly or readnone, since by definition we can't
671 // analyze all uses of a captured pointer.
673 // The false negatives happen when the pointer is captured by a function
674 // that promises readonly/readnone behaviour on the pointer, then the
675 // pointer's lifetime ends before anything that writes to arbitrary memory.
676 // Also, a readonly/readnone pointer may be returned, but returning a
677 // pointer is capturing it.
679 Attribute::AttrKind ReadAttr = Attribute::ReadNone;
680 for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) {
681 Argument *A = ArgumentSCC[i]->Definition;
682 Attribute::AttrKind K = determinePointerReadAttrs(A, ArgumentSCCNodes);
683 if (K == Attribute::ReadNone)
685 if (K == Attribute::ReadOnly) {
686 ReadAttr = Attribute::ReadOnly;
693 if (ReadAttr != Attribute::None) {
695 B.addAttribute(ReadAttr);
696 R.addAttribute(Attribute::ReadOnly).addAttribute(Attribute::ReadNone);
697 for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) {
698 Argument *A = ArgumentSCC[i]->Definition;
699 // Clear out existing readonly/readnone attributes
700 A->removeAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, R));
701 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
702 ReadAttr == Attribute::ReadOnly ? ++NumReadOnlyArg : ++NumReadNoneArg;
711 /// Tests whether a function is "malloc-like".
713 /// A function is "malloc-like" if it returns either null or a pointer that
714 /// doesn't alias any other pointer visible to the caller.
715 static bool isFunctionMallocLike(Function *F, const SCCNodeSet &SCCNodes) {
716 SmallSetVector<Value *, 8> FlowsToReturn;
717 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I)
718 if (ReturnInst *Ret = dyn_cast<ReturnInst>(I->getTerminator()))
719 FlowsToReturn.insert(Ret->getReturnValue());
721 for (unsigned i = 0; i != FlowsToReturn.size(); ++i) {
722 Value *RetVal = FlowsToReturn[i];
724 if (Constant *C = dyn_cast<Constant>(RetVal)) {
725 if (!C->isNullValue() && !isa<UndefValue>(C))
731 if (isa<Argument>(RetVal))
734 if (Instruction *RVI = dyn_cast<Instruction>(RetVal))
735 switch (RVI->getOpcode()) {
736 // Extend the analysis by looking upwards.
737 case Instruction::BitCast:
738 case Instruction::GetElementPtr:
739 case Instruction::AddrSpaceCast:
740 FlowsToReturn.insert(RVI->getOperand(0));
742 case Instruction::Select: {
743 SelectInst *SI = cast<SelectInst>(RVI);
744 FlowsToReturn.insert(SI->getTrueValue());
745 FlowsToReturn.insert(SI->getFalseValue());
748 case Instruction::PHI: {
749 PHINode *PN = cast<PHINode>(RVI);
750 for (Value *IncValue : PN->incoming_values())
751 FlowsToReturn.insert(IncValue);
755 // Check whether the pointer came from an allocation.
756 case Instruction::Alloca:
758 case Instruction::Call:
759 case Instruction::Invoke: {
761 if (CS.paramHasAttr(0, Attribute::NoAlias))
763 if (CS.getCalledFunction() && SCCNodes.count(CS.getCalledFunction()))
767 return false; // Did not come from an allocation.
770 if (PointerMayBeCaptured(RetVal, false, /*StoreCaptures=*/false))
777 /// Deduce noalias attributes for the SCC.
778 static bool addNoAliasAttrs(const SCCNodeSet &SCCNodes) {
779 // Check each function in turn, determining which functions return noalias
781 for (Function *F : SCCNodes) {
783 if (F->doesNotAlias(0))
786 // Definitions with weak linkage may be overridden at linktime, so
787 // treat them like declarations.
788 if (F->isDeclaration() || F->mayBeOverridden())
791 // We annotate noalias return values, which are only applicable to
793 if (!F->getReturnType()->isPointerTy())
796 if (!isFunctionMallocLike(F, SCCNodes))
800 bool MadeChange = false;
801 for (Function *F : SCCNodes) {
802 if (F->doesNotAlias(0) || !F->getReturnType()->isPointerTy())
805 F->setDoesNotAlias(0);
813 /// Tests whether this function is known to not return null.
815 /// Requires that the function returns a pointer.
817 /// Returns true if it believes the function will not return a null, and sets
818 /// \p Speculative based on whether the returned conclusion is a speculative
819 /// conclusion due to SCC calls.
820 static bool isReturnNonNull(Function *F, const SCCNodeSet &SCCNodes,
821 const TargetLibraryInfo &TLI, bool &Speculative) {
822 assert(F->getReturnType()->isPointerTy() &&
823 "nonnull only meaningful on pointer types");
826 SmallSetVector<Value *, 8> FlowsToReturn;
827 for (BasicBlock &BB : *F)
828 if (auto *Ret = dyn_cast<ReturnInst>(BB.getTerminator()))
829 FlowsToReturn.insert(Ret->getReturnValue());
831 for (unsigned i = 0; i != FlowsToReturn.size(); ++i) {
832 Value *RetVal = FlowsToReturn[i];
834 // If this value is locally known to be non-null, we're good
835 if (isKnownNonNull(RetVal, &TLI))
838 // Otherwise, we need to look upwards since we can't make any local
840 Instruction *RVI = dyn_cast<Instruction>(RetVal);
843 switch (RVI->getOpcode()) {
844 // Extend the analysis by looking upwards.
845 case Instruction::BitCast:
846 case Instruction::GetElementPtr:
847 case Instruction::AddrSpaceCast:
848 FlowsToReturn.insert(RVI->getOperand(0));
850 case Instruction::Select: {
851 SelectInst *SI = cast<SelectInst>(RVI);
852 FlowsToReturn.insert(SI->getTrueValue());
853 FlowsToReturn.insert(SI->getFalseValue());
856 case Instruction::PHI: {
857 PHINode *PN = cast<PHINode>(RVI);
858 for (int i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
859 FlowsToReturn.insert(PN->getIncomingValue(i));
862 case Instruction::Call:
863 case Instruction::Invoke: {
865 Function *Callee = CS.getCalledFunction();
866 // A call to a node within the SCC is assumed to return null until
868 if (Callee && SCCNodes.count(Callee)) {
875 return false; // Unknown source, may be null
877 llvm_unreachable("should have either continued or returned");
883 /// Deduce nonnull attributes for the SCC.
884 static bool addNonNullAttrs(const SCCNodeSet &SCCNodes,
885 const TargetLibraryInfo &TLI) {
886 // Speculative that all functions in the SCC return only nonnull
887 // pointers. We may refute this as we analyze functions.
888 bool SCCReturnsNonNull = true;
890 bool MadeChange = false;
892 // Check each function in turn, determining which functions return nonnull
894 for (Function *F : SCCNodes) {
896 if (F->getAttributes().hasAttribute(AttributeSet::ReturnIndex,
900 // Definitions with weak linkage may be overridden at linktime, so
901 // treat them like declarations.
902 if (F->isDeclaration() || F->mayBeOverridden())
905 // We annotate nonnull return values, which are only applicable to
907 if (!F->getReturnType()->isPointerTy())
910 bool Speculative = false;
911 if (isReturnNonNull(F, SCCNodes, TLI, Speculative)) {
913 // Mark the function eagerly since we may discover a function
914 // which prevents us from speculating about the entire SCC
915 DEBUG(dbgs() << "Eagerly marking " << F->getName() << " as nonnull\n");
916 F->addAttribute(AttributeSet::ReturnIndex, Attribute::NonNull);
922 // At least one function returns something which could be null, can't
923 // speculate any more.
924 SCCReturnsNonNull = false;
927 if (SCCReturnsNonNull) {
928 for (Function *F : SCCNodes) {
929 if (F->getAttributes().hasAttribute(AttributeSet::ReturnIndex,
930 Attribute::NonNull) ||
931 !F->getReturnType()->isPointerTy())
934 DEBUG(dbgs() << "SCC marking " << F->getName() << " as nonnull\n");
935 F->addAttribute(AttributeSet::ReturnIndex, Attribute::NonNull);
944 static bool setDoesNotRecurse(Function &F) {
945 if (F.doesNotRecurse())
947 F.setDoesNotRecurse();
952 static bool addNoRecurseAttrs(const CallGraphSCC &SCC,
953 SmallVectorImpl<WeakVH> &Revisit) {
954 // Try and identify functions that do not recurse.
956 // If the SCC contains multiple nodes we know for sure there is recursion.
957 if (!SCC.isSingular())
960 const CallGraphNode *CGN = *SCC.begin();
961 Function *F = CGN->getFunction();
962 if (!F || F->isDeclaration() || F->doesNotRecurse())
965 // If all of the calls in F are identifiable and are to norecurse functions, F
966 // is norecurse. This check also detects self-recursion as F is not currently
967 // marked norecurse, so any called from F to F will not be marked norecurse.
968 if (std::all_of(CGN->begin(), CGN->end(),
969 [](const CallGraphNode::CallRecord &CR) {
970 Function *F = CR.second->getFunction();
971 return F && F->doesNotRecurse();
973 // Function calls a potentially recursive function.
974 return setDoesNotRecurse(*F);
976 // We know that F is not obviously recursive, but we haven't been able to
977 // prove that it doesn't actually recurse. Add it to the Revisit list to try
978 // again top-down later.
979 Revisit.push_back(F);
983 static bool addNoRecurseAttrsTopDownOnly(Function *F) {
984 // If F is internal and all uses are in norecurse functions, then F is also
986 if (F->doesNotRecurse())
988 if (F->hasInternalLinkage()) {
989 for (auto *U : F->users())
990 if (auto *I = dyn_cast<Instruction>(U)) {
991 if (!I->getParent()->getParent()->doesNotRecurse())
996 return setDoesNotRecurse(*F);
1001 bool FunctionAttrs::runOnSCC(CallGraphSCC &SCC) {
1002 TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
1003 bool Changed = false;
1005 // We compute dedicated AA results for each function in the SCC as needed. We
1006 // use a lambda referencing external objects so that they live long enough to
1007 // be queried, but we re-use them each time.
1008 Optional<BasicAAResult> BAR;
1009 Optional<AAResults> AAR;
1010 auto AARGetter = [&](Function &F) -> AAResults & {
1011 BAR.emplace(createLegacyPMBasicAAResult(*this, F));
1012 AAR.emplace(createLegacyPMAAResults(*this, F, *BAR));
1016 // Fill SCCNodes with the elements of the SCC. Used for quickly looking up
1017 // whether a given CallGraphNode is in this SCC. Also track whether there are
1018 // any external or opt-none nodes that will prevent us from optimizing any
1020 SCCNodeSet SCCNodes;
1021 bool ExternalNode = false;
1022 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
1023 Function *F = (*I)->getFunction();
1024 if (!F || F->hasFnAttribute(Attribute::OptimizeNone)) {
1025 // External node or function we're trying not to optimize - we both avoid
1026 // transform them and avoid leveraging information they provide.
1027 ExternalNode = true;
1034 Changed |= addReadAttrs(SCCNodes, AARGetter);
1035 Changed |= addArgumentAttrs(SCCNodes);
1037 // If we have no external nodes participating in the SCC, we can deduce some
1038 // more precise attributes as well.
1039 if (!ExternalNode) {
1040 Changed |= addNoAliasAttrs(SCCNodes);
1041 Changed |= addNonNullAttrs(SCCNodes, *TLI);
1044 Changed |= addNoRecurseAttrs(SCC, Revisit);
1048 bool FunctionAttrs::doFinalization(CallGraph &CG) {
1049 bool Changed = false;
1050 // When iterating over SCCs we visit functions in a bottom-up fashion. Some of
1051 // the rules we have for identifying norecurse functions work best with a
1052 // top-down walk, so look again at all the functions we previously marked as
1053 // worth revisiting, in top-down order.
1054 for (auto &F : reverse(Revisit))
1056 Changed |= addNoRecurseAttrsTopDownOnly(cast<Function>((Value*)F));