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 //===----------------------------------------------------------------------===//
11 /// This file implements interprocedural passes which walk the
12 /// call-graph deducing and/or propagating function attributes.
14 //===----------------------------------------------------------------------===//
16 #include "llvm/Transforms/IPO/FunctionAttrs.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/SmallVector.h"
22 #include "llvm/ADT/Statistic.h"
23 #include "llvm/Analysis/AliasAnalysis.h"
24 #include "llvm/Analysis/AssumptionCache.h"
25 #include "llvm/Analysis/BasicAliasAnalysis.h"
26 #include "llvm/Analysis/CGSCCPassManager.h"
27 #include "llvm/Analysis/CallGraph.h"
28 #include "llvm/Analysis/CallGraphSCCPass.h"
29 #include "llvm/Analysis/CaptureTracking.h"
30 #include "llvm/Analysis/LazyCallGraph.h"
31 #include "llvm/Analysis/MemoryLocation.h"
32 #include "llvm/Analysis/ValueTracking.h"
33 #include "llvm/IR/Argument.h"
34 #include "llvm/IR/Attributes.h"
35 #include "llvm/IR/BasicBlock.h"
36 #include "llvm/IR/CallSite.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/Metadata.h"
45 #include "llvm/IR/PassManager.h"
46 #include "llvm/IR/Type.h"
47 #include "llvm/IR/Use.h"
48 #include "llvm/IR/User.h"
49 #include "llvm/IR/Value.h"
50 #include "llvm/Pass.h"
51 #include "llvm/Support/Casting.h"
52 #include "llvm/Support/CommandLine.h"
53 #include "llvm/Support/Compiler.h"
54 #include "llvm/Support/Debug.h"
55 #include "llvm/Support/ErrorHandling.h"
56 #include "llvm/Support/raw_ostream.h"
57 #include "llvm/Transforms/IPO.h"
65 #define DEBUG_TYPE "functionattrs"
67 STATISTIC(NumReadNone, "Number of functions marked readnone");
68 STATISTIC(NumReadOnly, "Number of functions marked readonly");
69 STATISTIC(NumNoCapture, "Number of arguments marked nocapture");
70 STATISTIC(NumReturned, "Number of arguments marked returned");
71 STATISTIC(NumReadNoneArg, "Number of arguments marked readnone");
72 STATISTIC(NumReadOnlyArg, "Number of arguments marked readonly");
73 STATISTIC(NumNoAlias, "Number of function returns marked noalias");
74 STATISTIC(NumNonNullReturn, "Number of function returns marked nonnull");
75 STATISTIC(NumNoRecurse, "Number of functions marked as norecurse");
76 STATISTIC(NumNoUnwind, "Number of functions marked as nounwind");
78 // FIXME: This is disabled by default to avoid exposing security vulnerabilities
79 // in C/C++ code compiled by clang:
80 // http://lists.llvm.org/pipermail/cfe-dev/2017-January/052066.html
81 static cl::opt<bool> EnableNonnullArgPropagation(
82 "enable-nonnull-arg-prop", cl::Hidden,
83 cl::desc("Try to propagate nonnull argument attributes from callsites to "
84 "caller functions."));
86 static cl::opt<bool> DisableNoUnwindInference(
87 "disable-nounwind-inference", cl::Hidden,
88 cl::desc("Stop inferring nounwind attribute during function-attrs pass"));
92 using SCCNodeSet = SmallSetVector<Function *, 8>;
94 } // end anonymous namespace
96 /// Returns the memory access attribute for function F using AAR for AA results,
97 /// where SCCNodes is the current SCC.
99 /// If ThisBody is true, this function may examine the function body and will
100 /// return a result pertaining to this copy of the function. If it is false, the
101 /// result will be based only on AA results for the function declaration; it
102 /// will be assumed that some other (perhaps less optimized) version of the
103 /// function may be selected at link time.
104 static MemoryAccessKind checkFunctionMemoryAccess(Function &F, bool ThisBody,
106 const SCCNodeSet &SCCNodes) {
107 FunctionModRefBehavior MRB = AAR.getModRefBehavior(&F);
108 if (MRB == FMRB_DoesNotAccessMemory)
113 if (AliasAnalysis::onlyReadsMemory(MRB))
116 // Conservatively assume it writes to memory.
120 // Scan the function body for instructions that may read or write memory.
121 bool ReadsMemory = false;
122 for (inst_iterator II = inst_begin(F), E = inst_end(F); II != E; ++II) {
123 Instruction *I = &*II;
125 // Some instructions can be ignored even if they read or write memory.
126 // Detect these now, skipping to the next instruction if one is found.
127 CallSite CS(cast<Value>(I));
129 // Ignore calls to functions in the same SCC, as long as the call sites
130 // don't have operand bundles. Calls with operand bundles are allowed to
131 // have memory effects not described by the memory effects of the call
133 if (!CS.hasOperandBundles() && CS.getCalledFunction() &&
134 SCCNodes.count(CS.getCalledFunction()))
136 FunctionModRefBehavior MRB = AAR.getModRefBehavior(CS);
137 ModRefInfo MRI = createModRefInfo(MRB);
139 // If the call doesn't access memory, we're done.
143 if (!AliasAnalysis::onlyAccessesArgPointees(MRB)) {
144 // The call could access any memory. If that includes writes, give up.
147 // If it reads, note it.
153 // Check whether all pointer arguments point to local memory, and
154 // ignore calls that only access local memory.
155 for (CallSite::arg_iterator CI = CS.arg_begin(), CE = CS.arg_end();
158 if (!Arg->getType()->isPtrOrPtrVectorTy())
162 I->getAAMetadata(AAInfo);
163 MemoryLocation Loc(Arg, MemoryLocation::UnknownSize, AAInfo);
165 // Skip accesses to local or constant memory as they don't impact the
166 // externally visible mod/ref behavior.
167 if (AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true))
171 // Writes non-local memory. Give up.
174 // Ok, it reads non-local memory.
178 } else if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
179 // Ignore non-volatile loads from local memory. (Atomic is okay here.)
180 if (!LI->isVolatile()) {
181 MemoryLocation Loc = MemoryLocation::get(LI);
182 if (AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true))
185 } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
186 // Ignore non-volatile stores to local memory. (Atomic is okay here.)
187 if (!SI->isVolatile()) {
188 MemoryLocation Loc = MemoryLocation::get(SI);
189 if (AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true))
192 } else if (VAArgInst *VI = dyn_cast<VAArgInst>(I)) {
193 // Ignore vaargs on local memory.
194 MemoryLocation Loc = MemoryLocation::get(VI);
195 if (AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true))
199 // Any remaining instructions need to be taken seriously! Check if they
200 // read or write memory.
201 if (I->mayWriteToMemory())
202 // Writes memory. Just give up.
205 // If this instruction may read memory, remember that.
206 ReadsMemory |= I->mayReadFromMemory();
209 return ReadsMemory ? MAK_ReadOnly : MAK_ReadNone;
212 MemoryAccessKind llvm::computeFunctionBodyMemoryAccess(Function &F,
214 return checkFunctionMemoryAccess(F, /*ThisBody=*/true, AAR, {});
217 /// Deduce readonly/readnone attributes for the SCC.
218 template <typename AARGetterT>
219 static bool addReadAttrs(const SCCNodeSet &SCCNodes, AARGetterT &&AARGetter) {
220 // Check if any of the functions in the SCC read or write memory. If they
221 // write memory then they can't be marked readnone or readonly.
222 bool ReadsMemory = false;
223 for (Function *F : SCCNodes) {
224 // Call the callable parameter to look up AA results for this function.
225 AAResults &AAR = AARGetter(*F);
227 // Non-exact function definitions may not be selected at link time, and an
228 // alternative version that writes to memory may be selected. See the
229 // comment on GlobalValue::isDefinitionExact for more details.
230 switch (checkFunctionMemoryAccess(*F, F->hasExactDefinition(),
243 // Success! Functions in this SCC do not access memory, or only read memory.
244 // Give them the appropriate attribute.
245 bool MadeChange = false;
246 for (Function *F : SCCNodes) {
247 if (F->doesNotAccessMemory())
251 if (F->onlyReadsMemory() && ReadsMemory)
257 // Clear out any existing attributes.
258 F->removeFnAttr(Attribute::ReadOnly);
259 F->removeFnAttr(Attribute::ReadNone);
261 // Add in the new attribute.
262 F->addFnAttr(ReadsMemory ? Attribute::ReadOnly : Attribute::ReadNone);
275 /// For a given pointer Argument, this retains a list of Arguments of functions
276 /// in the same SCC that the pointer data flows into. We use this to build an
277 /// SCC of the arguments.
278 struct ArgumentGraphNode {
279 Argument *Definition;
280 SmallVector<ArgumentGraphNode *, 4> Uses;
283 class ArgumentGraph {
284 // We store pointers to ArgumentGraphNode objects, so it's important that
285 // that they not move around upon insert.
286 using ArgumentMapTy = std::map<Argument *, ArgumentGraphNode>;
288 ArgumentMapTy ArgumentMap;
290 // There is no root node for the argument graph, in fact:
291 // void f(int *x, int *y) { if (...) f(x, y); }
292 // is an example where the graph is disconnected. The SCCIterator requires a
293 // single entry point, so we maintain a fake ("synthetic") root node that
294 // uses every node. Because the graph is directed and nothing points into
295 // the root, it will not participate in any SCCs (except for its own).
296 ArgumentGraphNode SyntheticRoot;
299 ArgumentGraph() { SyntheticRoot.Definition = nullptr; }
301 using iterator = SmallVectorImpl<ArgumentGraphNode *>::iterator;
303 iterator begin() { return SyntheticRoot.Uses.begin(); }
304 iterator end() { return SyntheticRoot.Uses.end(); }
305 ArgumentGraphNode *getEntryNode() { return &SyntheticRoot; }
307 ArgumentGraphNode *operator[](Argument *A) {
308 ArgumentGraphNode &Node = ArgumentMap[A];
310 SyntheticRoot.Uses.push_back(&Node);
315 /// This tracker checks whether callees are in the SCC, and if so it does not
316 /// consider that a capture, instead adding it to the "Uses" list and
317 /// continuing with the analysis.
318 struct ArgumentUsesTracker : public CaptureTracker {
319 ArgumentUsesTracker(const SCCNodeSet &SCCNodes) : SCCNodes(SCCNodes) {}
321 void tooManyUses() override { Captured = true; }
323 bool captured(const Use *U) override {
324 CallSite CS(U->getUser());
325 if (!CS.getInstruction()) {
330 Function *F = CS.getCalledFunction();
331 if (!F || !F->hasExactDefinition() || !SCCNodes.count(F)) {
336 // Note: the callee and the two successor blocks *follow* the argument
337 // operands. This means there is no need to adjust UseIndex to account for
341 std::distance(const_cast<const Use *>(CS.arg_begin()), U);
343 assert(UseIndex < CS.data_operands_size() &&
344 "Indirect function calls should have been filtered above!");
346 if (UseIndex >= CS.getNumArgOperands()) {
347 // Data operand, but not a argument operand -- must be a bundle operand
348 assert(CS.hasOperandBundles() && "Must be!");
350 // CaptureTracking told us that we're being captured by an operand bundle
351 // use. In this case it does not matter if the callee is within our SCC
352 // or not -- we've been captured in some unknown way, and we have to be
358 if (UseIndex >= F->arg_size()) {
359 assert(F->isVarArg() && "More params than args in non-varargs call");
364 Uses.push_back(&*std::next(F->arg_begin(), UseIndex));
368 // True only if certainly captured (used outside our SCC).
369 bool Captured = false;
371 // Uses within our SCC.
372 SmallVector<Argument *, 4> Uses;
374 const SCCNodeSet &SCCNodes;
377 } // end anonymous namespace
381 template <> struct GraphTraits<ArgumentGraphNode *> {
382 using NodeRef = ArgumentGraphNode *;
383 using ChildIteratorType = SmallVectorImpl<ArgumentGraphNode *>::iterator;
385 static NodeRef getEntryNode(NodeRef A) { return A; }
386 static ChildIteratorType child_begin(NodeRef N) { return N->Uses.begin(); }
387 static ChildIteratorType child_end(NodeRef N) { return N->Uses.end(); }
391 struct GraphTraits<ArgumentGraph *> : public GraphTraits<ArgumentGraphNode *> {
392 static NodeRef getEntryNode(ArgumentGraph *AG) { return AG->getEntryNode(); }
394 static ChildIteratorType nodes_begin(ArgumentGraph *AG) {
398 static ChildIteratorType nodes_end(ArgumentGraph *AG) { return AG->end(); }
401 } // end namespace llvm
403 /// Returns Attribute::None, Attribute::ReadOnly or Attribute::ReadNone.
404 static Attribute::AttrKind
405 determinePointerReadAttrs(Argument *A,
406 const SmallPtrSet<Argument *, 8> &SCCNodes) {
407 SmallVector<Use *, 32> Worklist;
408 SmallPtrSet<Use *, 32> Visited;
410 // inalloca arguments are always clobbered by the call.
411 if (A->hasInAllocaAttr())
412 return Attribute::None;
415 // We don't need to track IsWritten. If A is written to, return immediately.
417 for (Use &U : A->uses()) {
419 Worklist.push_back(&U);
422 while (!Worklist.empty()) {
423 Use *U = Worklist.pop_back_val();
424 Instruction *I = cast<Instruction>(U->getUser());
426 switch (I->getOpcode()) {
427 case Instruction::BitCast:
428 case Instruction::GetElementPtr:
429 case Instruction::PHI:
430 case Instruction::Select:
431 case Instruction::AddrSpaceCast:
432 // The original value is not read/written via this if the new value isn't.
433 for (Use &UU : I->uses())
434 if (Visited.insert(&UU).second)
435 Worklist.push_back(&UU);
438 case Instruction::Call:
439 case Instruction::Invoke: {
440 bool Captures = true;
442 if (I->getType()->isVoidTy())
445 auto AddUsersToWorklistIfCapturing = [&] {
447 for (Use &UU : I->uses())
448 if (Visited.insert(&UU).second)
449 Worklist.push_back(&UU);
453 if (CS.doesNotAccessMemory()) {
454 AddUsersToWorklistIfCapturing();
458 Function *F = CS.getCalledFunction();
460 if (CS.onlyReadsMemory()) {
462 AddUsersToWorklistIfCapturing();
465 return Attribute::None;
468 // Note: the callee and the two successor blocks *follow* the argument
469 // operands. This means there is no need to adjust UseIndex to account
472 unsigned UseIndex = std::distance(CS.arg_begin(), U);
474 // U cannot be the callee operand use: since we're exploring the
475 // transitive uses of an Argument, having such a use be a callee would
476 // imply the CallSite is an indirect call or invoke; and we'd take the
478 assert(UseIndex < CS.data_operands_size() &&
479 "Data operand use expected!");
481 bool IsOperandBundleUse = UseIndex >= CS.getNumArgOperands();
483 if (UseIndex >= F->arg_size() && !IsOperandBundleUse) {
484 assert(F->isVarArg() && "More params than args in non-varargs call");
485 return Attribute::None;
488 Captures &= !CS.doesNotCapture(UseIndex);
490 // Since the optimizer (by design) cannot see the data flow corresponding
491 // to a operand bundle use, these cannot participate in the optimistic SCC
492 // analysis. Instead, we model the operand bundle uses as arguments in
493 // call to a function external to the SCC.
494 if (IsOperandBundleUse ||
495 !SCCNodes.count(&*std::next(F->arg_begin(), UseIndex))) {
497 // The accessors used on CallSite here do the right thing for calls and
498 // invokes with operand bundles.
500 if (!CS.onlyReadsMemory() && !CS.onlyReadsMemory(UseIndex))
501 return Attribute::None;
502 if (!CS.doesNotAccessMemory(UseIndex))
506 AddUsersToWorklistIfCapturing();
510 case Instruction::Load:
511 // A volatile load has side effects beyond what readonly can be relied
513 if (cast<LoadInst>(I)->isVolatile())
514 return Attribute::None;
519 case Instruction::ICmp:
520 case Instruction::Ret:
524 return Attribute::None;
528 return IsRead ? Attribute::ReadOnly : Attribute::ReadNone;
531 /// Deduce returned attributes for the SCC.
532 static bool addArgumentReturnedAttrs(const SCCNodeSet &SCCNodes) {
533 bool Changed = false;
535 // Check each function in turn, determining if an argument is always returned.
536 for (Function *F : SCCNodes) {
537 // We can infer and propagate function attributes only when we know that the
538 // definition we'll get at link time is *exactly* the definition we see now.
539 // For more details, see GlobalValue::mayBeDerefined.
540 if (!F->hasExactDefinition())
543 if (F->getReturnType()->isVoidTy())
546 // There is nothing to do if an argument is already marked as 'returned'.
547 if (llvm::any_of(F->args(),
548 [](const Argument &Arg) { return Arg.hasReturnedAttr(); }))
551 auto FindRetArg = [&]() -> Value * {
552 Value *RetArg = nullptr;
553 for (BasicBlock &BB : *F)
554 if (auto *Ret = dyn_cast<ReturnInst>(BB.getTerminator())) {
555 // Note that stripPointerCasts should look through functions with
556 // returned arguments.
557 Value *RetVal = Ret->getReturnValue()->stripPointerCasts();
558 if (!isa<Argument>(RetVal) || RetVal->getType() != F->getReturnType())
563 else if (RetArg != RetVal)
570 if (Value *RetArg = FindRetArg()) {
571 auto *A = cast<Argument>(RetArg);
572 A->addAttr(Attribute::Returned);
581 /// If a callsite has arguments that are also arguments to the parent function,
582 /// try to propagate attributes from the callsite's arguments to the parent's
583 /// arguments. This may be important because inlining can cause information loss
584 /// when attribute knowledge disappears with the inlined call.
585 static bool addArgumentAttrsFromCallsites(Function &F) {
586 if (!EnableNonnullArgPropagation)
589 bool Changed = false;
591 // For an argument attribute to transfer from a callsite to the parent, the
592 // call must be guaranteed to execute every time the parent is called.
593 // Conservatively, just check for calls in the entry block that are guaranteed
595 // TODO: This could be enhanced by testing if the callsite post-dominates the
596 // entry block or by doing simple forward walks or backward walks to the
598 BasicBlock &Entry = F.getEntryBlock();
599 for (Instruction &I : Entry) {
600 if (auto CS = CallSite(&I)) {
601 if (auto *CalledFunc = CS.getCalledFunction()) {
602 for (auto &CSArg : CalledFunc->args()) {
603 if (!CSArg.hasNonNullAttr())
606 // If the non-null callsite argument operand is an argument to 'F'
607 // (the caller) and the call is guaranteed to execute, then the value
608 // must be non-null throughout 'F'.
609 auto *FArg = dyn_cast<Argument>(CS.getArgOperand(CSArg.getArgNo()));
610 if (FArg && !FArg->hasNonNullAttr()) {
611 FArg->addAttr(Attribute::NonNull);
617 if (!isGuaranteedToTransferExecutionToSuccessor(&I))
624 /// Deduce nocapture attributes for the SCC.
625 static bool addArgumentAttrs(const SCCNodeSet &SCCNodes) {
626 bool Changed = false;
630 // Check each function in turn, determining which pointer arguments are not
632 for (Function *F : SCCNodes) {
633 // We can infer and propagate function attributes only when we know that the
634 // definition we'll get at link time is *exactly* the definition we see now.
635 // For more details, see GlobalValue::mayBeDerefined.
636 if (!F->hasExactDefinition())
639 Changed |= addArgumentAttrsFromCallsites(*F);
641 // Functions that are readonly (or readnone) and nounwind and don't return
642 // a value can't capture arguments. Don't analyze them.
643 if (F->onlyReadsMemory() && F->doesNotThrow() &&
644 F->getReturnType()->isVoidTy()) {
645 for (Function::arg_iterator A = F->arg_begin(), E = F->arg_end(); A != E;
647 if (A->getType()->isPointerTy() && !A->hasNoCaptureAttr()) {
648 A->addAttr(Attribute::NoCapture);
656 for (Function::arg_iterator A = F->arg_begin(), E = F->arg_end(); A != E;
658 if (!A->getType()->isPointerTy())
660 bool HasNonLocalUses = false;
661 if (!A->hasNoCaptureAttr()) {
662 ArgumentUsesTracker Tracker(SCCNodes);
663 PointerMayBeCaptured(&*A, &Tracker);
664 if (!Tracker.Captured) {
665 if (Tracker.Uses.empty()) {
666 // If it's trivially not captured, mark it nocapture now.
667 A->addAttr(Attribute::NoCapture);
671 // If it's not trivially captured and not trivially not captured,
672 // then it must be calling into another function in our SCC. Save
673 // its particulars for Argument-SCC analysis later.
674 ArgumentGraphNode *Node = AG[&*A];
675 for (Argument *Use : Tracker.Uses) {
676 Node->Uses.push_back(AG[Use]);
678 HasNonLocalUses = true;
682 // Otherwise, it's captured. Don't bother doing SCC analysis on it.
684 if (!HasNonLocalUses && !A->onlyReadsMemory()) {
685 // Can we determine that it's readonly/readnone without doing an SCC?
686 // Note that we don't allow any calls at all here, or else our result
687 // will be dependent on the iteration order through the functions in the
689 SmallPtrSet<Argument *, 8> Self;
691 Attribute::AttrKind R = determinePointerReadAttrs(&*A, Self);
692 if (R != Attribute::None) {
695 R == Attribute::ReadOnly ? ++NumReadOnlyArg : ++NumReadNoneArg;
701 // The graph we've collected is partial because we stopped scanning for
702 // argument uses once we solved the argument trivially. These partial nodes
703 // show up as ArgumentGraphNode objects with an empty Uses list, and for
704 // these nodes the final decision about whether they capture has already been
705 // made. If the definition doesn't have a 'nocapture' attribute by now, it
708 for (scc_iterator<ArgumentGraph *> I = scc_begin(&AG); !I.isAtEnd(); ++I) {
709 const std::vector<ArgumentGraphNode *> &ArgumentSCC = *I;
710 if (ArgumentSCC.size() == 1) {
711 if (!ArgumentSCC[0]->Definition)
712 continue; // synthetic root node
714 // eg. "void f(int* x) { if (...) f(x); }"
715 if (ArgumentSCC[0]->Uses.size() == 1 &&
716 ArgumentSCC[0]->Uses[0] == ArgumentSCC[0]) {
717 Argument *A = ArgumentSCC[0]->Definition;
718 A->addAttr(Attribute::NoCapture);
725 bool SCCCaptured = false;
726 for (auto I = ArgumentSCC.begin(), E = ArgumentSCC.end();
727 I != E && !SCCCaptured; ++I) {
728 ArgumentGraphNode *Node = *I;
729 if (Node->Uses.empty()) {
730 if (!Node->Definition->hasNoCaptureAttr())
737 SmallPtrSet<Argument *, 8> ArgumentSCCNodes;
738 // Fill ArgumentSCCNodes with the elements of the ArgumentSCC. Used for
739 // quickly looking up whether a given Argument is in this ArgumentSCC.
740 for (ArgumentGraphNode *I : ArgumentSCC) {
741 ArgumentSCCNodes.insert(I->Definition);
744 for (auto I = ArgumentSCC.begin(), E = ArgumentSCC.end();
745 I != E && !SCCCaptured; ++I) {
746 ArgumentGraphNode *N = *I;
747 for (ArgumentGraphNode *Use : N->Uses) {
748 Argument *A = Use->Definition;
749 if (A->hasNoCaptureAttr() || ArgumentSCCNodes.count(A))
758 for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) {
759 Argument *A = ArgumentSCC[i]->Definition;
760 A->addAttr(Attribute::NoCapture);
765 // We also want to compute readonly/readnone. With a small number of false
766 // negatives, we can assume that any pointer which is captured isn't going
767 // to be provably readonly or readnone, since by definition we can't
768 // analyze all uses of a captured pointer.
770 // The false negatives happen when the pointer is captured by a function
771 // that promises readonly/readnone behaviour on the pointer, then the
772 // pointer's lifetime ends before anything that writes to arbitrary memory.
773 // Also, a readonly/readnone pointer may be returned, but returning a
774 // pointer is capturing it.
776 Attribute::AttrKind ReadAttr = Attribute::ReadNone;
777 for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) {
778 Argument *A = ArgumentSCC[i]->Definition;
779 Attribute::AttrKind K = determinePointerReadAttrs(A, ArgumentSCCNodes);
780 if (K == Attribute::ReadNone)
782 if (K == Attribute::ReadOnly) {
783 ReadAttr = Attribute::ReadOnly;
790 if (ReadAttr != Attribute::None) {
791 for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) {
792 Argument *A = ArgumentSCC[i]->Definition;
793 // Clear out existing readonly/readnone attributes
794 A->removeAttr(Attribute::ReadOnly);
795 A->removeAttr(Attribute::ReadNone);
796 A->addAttr(ReadAttr);
797 ReadAttr == Attribute::ReadOnly ? ++NumReadOnlyArg : ++NumReadNoneArg;
806 /// Tests whether a function is "malloc-like".
808 /// A function is "malloc-like" if it returns either null or a pointer that
809 /// doesn't alias any other pointer visible to the caller.
810 static bool isFunctionMallocLike(Function *F, const SCCNodeSet &SCCNodes) {
811 SmallSetVector<Value *, 8> FlowsToReturn;
812 for (BasicBlock &BB : *F)
813 if (ReturnInst *Ret = dyn_cast<ReturnInst>(BB.getTerminator()))
814 FlowsToReturn.insert(Ret->getReturnValue());
816 for (unsigned i = 0; i != FlowsToReturn.size(); ++i) {
817 Value *RetVal = FlowsToReturn[i];
819 if (Constant *C = dyn_cast<Constant>(RetVal)) {
820 if (!C->isNullValue() && !isa<UndefValue>(C))
826 if (isa<Argument>(RetVal))
829 if (Instruction *RVI = dyn_cast<Instruction>(RetVal))
830 switch (RVI->getOpcode()) {
831 // Extend the analysis by looking upwards.
832 case Instruction::BitCast:
833 case Instruction::GetElementPtr:
834 case Instruction::AddrSpaceCast:
835 FlowsToReturn.insert(RVI->getOperand(0));
837 case Instruction::Select: {
838 SelectInst *SI = cast<SelectInst>(RVI);
839 FlowsToReturn.insert(SI->getTrueValue());
840 FlowsToReturn.insert(SI->getFalseValue());
843 case Instruction::PHI: {
844 PHINode *PN = cast<PHINode>(RVI);
845 for (Value *IncValue : PN->incoming_values())
846 FlowsToReturn.insert(IncValue);
850 // Check whether the pointer came from an allocation.
851 case Instruction::Alloca:
853 case Instruction::Call:
854 case Instruction::Invoke: {
856 if (CS.hasRetAttr(Attribute::NoAlias))
858 if (CS.getCalledFunction() && SCCNodes.count(CS.getCalledFunction()))
863 return false; // Did not come from an allocation.
866 if (PointerMayBeCaptured(RetVal, false, /*StoreCaptures=*/false))
873 /// Deduce noalias attributes for the SCC.
874 static bool addNoAliasAttrs(const SCCNodeSet &SCCNodes) {
875 // Check each function in turn, determining which functions return noalias
877 for (Function *F : SCCNodes) {
879 if (F->returnDoesNotAlias())
882 // We can infer and propagate function attributes only when we know that the
883 // definition we'll get at link time is *exactly* the definition we see now.
884 // For more details, see GlobalValue::mayBeDerefined.
885 if (!F->hasExactDefinition())
888 // We annotate noalias return values, which are only applicable to
890 if (!F->getReturnType()->isPointerTy())
893 if (!isFunctionMallocLike(F, SCCNodes))
897 bool MadeChange = false;
898 for (Function *F : SCCNodes) {
899 if (F->returnDoesNotAlias() ||
900 !F->getReturnType()->isPointerTy())
903 F->setReturnDoesNotAlias();
911 /// Tests whether this function is known to not return null.
913 /// Requires that the function returns a pointer.
915 /// Returns true if it believes the function will not return a null, and sets
916 /// \p Speculative based on whether the returned conclusion is a speculative
917 /// conclusion due to SCC calls.
918 static bool isReturnNonNull(Function *F, const SCCNodeSet &SCCNodes,
920 assert(F->getReturnType()->isPointerTy() &&
921 "nonnull only meaningful on pointer types");
924 SmallSetVector<Value *, 8> FlowsToReturn;
925 for (BasicBlock &BB : *F)
926 if (auto *Ret = dyn_cast<ReturnInst>(BB.getTerminator()))
927 FlowsToReturn.insert(Ret->getReturnValue());
929 auto &DL = F->getParent()->getDataLayout();
931 for (unsigned i = 0; i != FlowsToReturn.size(); ++i) {
932 Value *RetVal = FlowsToReturn[i];
934 // If this value is locally known to be non-null, we're good
935 if (isKnownNonZero(RetVal, DL))
938 // Otherwise, we need to look upwards since we can't make any local
940 Instruction *RVI = dyn_cast<Instruction>(RetVal);
943 switch (RVI->getOpcode()) {
944 // Extend the analysis by looking upwards.
945 case Instruction::BitCast:
946 case Instruction::GetElementPtr:
947 case Instruction::AddrSpaceCast:
948 FlowsToReturn.insert(RVI->getOperand(0));
950 case Instruction::Select: {
951 SelectInst *SI = cast<SelectInst>(RVI);
952 FlowsToReturn.insert(SI->getTrueValue());
953 FlowsToReturn.insert(SI->getFalseValue());
956 case Instruction::PHI: {
957 PHINode *PN = cast<PHINode>(RVI);
958 for (int i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
959 FlowsToReturn.insert(PN->getIncomingValue(i));
962 case Instruction::Call:
963 case Instruction::Invoke: {
965 Function *Callee = CS.getCalledFunction();
966 // A call to a node within the SCC is assumed to return null until
968 if (Callee && SCCNodes.count(Callee)) {
975 return false; // Unknown source, may be null
977 llvm_unreachable("should have either continued or returned");
983 /// Deduce nonnull attributes for the SCC.
984 static bool addNonNullAttrs(const SCCNodeSet &SCCNodes) {
985 // Speculative that all functions in the SCC return only nonnull
986 // pointers. We may refute this as we analyze functions.
987 bool SCCReturnsNonNull = true;
989 bool MadeChange = false;
991 // Check each function in turn, determining which functions return nonnull
993 for (Function *F : SCCNodes) {
995 if (F->getAttributes().hasAttribute(AttributeList::ReturnIndex,
999 // We can infer and propagate function attributes only when we know that the
1000 // definition we'll get at link time is *exactly* the definition we see now.
1001 // For more details, see GlobalValue::mayBeDerefined.
1002 if (!F->hasExactDefinition())
1005 // We annotate nonnull return values, which are only applicable to
1007 if (!F->getReturnType()->isPointerTy())
1010 bool Speculative = false;
1011 if (isReturnNonNull(F, SCCNodes, Speculative)) {
1013 // Mark the function eagerly since we may discover a function
1014 // which prevents us from speculating about the entire SCC
1015 LLVM_DEBUG(dbgs() << "Eagerly marking " << F->getName()
1016 << " as nonnull\n");
1017 F->addAttribute(AttributeList::ReturnIndex, Attribute::NonNull);
1023 // At least one function returns something which could be null, can't
1024 // speculate any more.
1025 SCCReturnsNonNull = false;
1028 if (SCCReturnsNonNull) {
1029 for (Function *F : SCCNodes) {
1030 if (F->getAttributes().hasAttribute(AttributeList::ReturnIndex,
1031 Attribute::NonNull) ||
1032 !F->getReturnType()->isPointerTy())
1035 LLVM_DEBUG(dbgs() << "SCC marking " << F->getName() << " as nonnull\n");
1036 F->addAttribute(AttributeList::ReturnIndex, Attribute::NonNull);
1047 /// Collects a set of attribute inference requests and performs them all in one
1048 /// go on a single SCC Node. Inference involves scanning function bodies
1049 /// looking for instructions that violate attribute assumptions.
1050 /// As soon as all the bodies are fine we are free to set the attribute.
1051 /// Customization of inference for individual attributes is performed by
1052 /// providing a handful of predicates for each attribute.
1053 class AttributeInferer {
1055 /// Describes a request for inference of a single attribute.
1056 struct InferenceDescriptor {
1058 /// Returns true if this function does not have to be handled.
1059 /// General intent for this predicate is to provide an optimization
1060 /// for functions that do not need this attribute inference at all
1061 /// (say, for functions that already have the attribute).
1062 std::function<bool(const Function &)> SkipFunction;
1064 /// Returns true if this instruction violates attribute assumptions.
1065 std::function<bool(Instruction &)> InstrBreaksAttribute;
1067 /// Sets the inferred attribute for this function.
1068 std::function<void(Function &)> SetAttribute;
1070 /// Attribute we derive.
1071 Attribute::AttrKind AKind;
1073 /// If true, only "exact" definitions can be used to infer this attribute.
1074 /// See GlobalValue::isDefinitionExact.
1075 bool RequiresExactDefinition;
1077 InferenceDescriptor(Attribute::AttrKind AK,
1078 std::function<bool(const Function &)> SkipFunc,
1079 std::function<bool(Instruction &)> InstrScan,
1080 std::function<void(Function &)> SetAttr,
1082 : SkipFunction(SkipFunc), InstrBreaksAttribute(InstrScan),
1083 SetAttribute(SetAttr), AKind(AK),
1084 RequiresExactDefinition(ReqExactDef) {}
1088 SmallVector<InferenceDescriptor, 4> InferenceDescriptors;
1091 void registerAttrInference(InferenceDescriptor AttrInference) {
1092 InferenceDescriptors.push_back(AttrInference);
1095 bool run(const SCCNodeSet &SCCNodes);
1098 /// Perform all the requested attribute inference actions according to the
1099 /// attribute predicates stored before.
1100 bool AttributeInferer::run(const SCCNodeSet &SCCNodes) {
1101 SmallVector<InferenceDescriptor, 4> InferInSCC = InferenceDescriptors;
1102 // Go through all the functions in SCC and check corresponding attribute
1103 // assumptions for each of them. Attributes that are invalid for this SCC
1104 // will be removed from InferInSCC.
1105 for (Function *F : SCCNodes) {
1107 // No attributes whose assumptions are still valid - done.
1108 if (InferInSCC.empty())
1111 // Check if our attributes ever need scanning/can be scanned.
1112 llvm::erase_if(InferInSCC, [F](const InferenceDescriptor &ID) {
1113 if (ID.SkipFunction(*F))
1116 // Remove from further inference (invalidate) when visiting a function
1117 // that has no instructions to scan/has an unsuitable definition.
1118 return F->isDeclaration() ||
1119 (ID.RequiresExactDefinition && !F->hasExactDefinition());
1122 // For each attribute still in InferInSCC that doesn't explicitly skip F,
1123 // set up the F instructions scan to verify assumptions of the attribute.
1124 SmallVector<InferenceDescriptor, 4> InferInThisFunc;
1126 InferInSCC, std::back_inserter(InferInThisFunc),
1127 [F](const InferenceDescriptor &ID) { return !ID.SkipFunction(*F); });
1129 if (InferInThisFunc.empty())
1132 // Start instruction scan.
1133 for (Instruction &I : instructions(*F)) {
1134 llvm::erase_if(InferInThisFunc, [&](const InferenceDescriptor &ID) {
1135 if (!ID.InstrBreaksAttribute(I))
1137 // Remove attribute from further inference on any other functions
1138 // because attribute assumptions have just been violated.
1139 llvm::erase_if(InferInSCC, [&ID](const InferenceDescriptor &D) {
1140 return D.AKind == ID.AKind;
1142 // Remove attribute from the rest of current instruction scan.
1146 if (InferInThisFunc.empty())
1151 if (InferInSCC.empty())
1154 bool Changed = false;
1155 for (Function *F : SCCNodes)
1156 // At this point InferInSCC contains only functions that were either:
1157 // - explicitly skipped from scan/inference, or
1158 // - verified to have no instructions that break attribute assumptions.
1159 // Hence we just go and force the attribute for all non-skipped functions.
1160 for (auto &ID : InferInSCC) {
1161 if (ID.SkipFunction(*F))
1164 ID.SetAttribute(*F);
1169 } // end anonymous namespace
1171 /// Helper for non-Convergent inference predicate InstrBreaksAttribute.
1172 static bool InstrBreaksNonConvergent(Instruction &I,
1173 const SCCNodeSet &SCCNodes) {
1174 const CallSite CS(&I);
1175 // Breaks non-convergent assumption if CS is a convergent call to a function
1177 return CS && CS.isConvergent() && SCCNodes.count(CS.getCalledFunction()) == 0;
1180 /// Helper for NoUnwind inference predicate InstrBreaksAttribute.
1181 static bool InstrBreaksNonThrowing(Instruction &I, const SCCNodeSet &SCCNodes) {
1184 if (const auto *CI = dyn_cast<CallInst>(&I)) {
1185 if (Function *Callee = CI->getCalledFunction()) {
1186 // I is a may-throw call to a function inside our SCC. This doesn't
1187 // invalidate our current working assumption that the SCC is no-throw; we
1188 // just have to scan that other function.
1189 if (SCCNodes.count(Callee) > 0)
1196 /// Infer attributes from all functions in the SCC by scanning every
1197 /// instruction for compliance to the attribute assumptions. Currently it
1199 /// - removal of Convergent attribute
1200 /// - addition of NoUnwind attribute
1202 /// Returns true if any changes to function attributes were made.
1203 static bool inferAttrsFromFunctionBodies(const SCCNodeSet &SCCNodes) {
1205 AttributeInferer AI;
1207 // Request to remove the convergent attribute from all functions in the SCC
1208 // if every callsite within the SCC is not convergent (except for calls
1209 // to functions within the SCC).
1210 // Note: Removal of the attr from the callsites will happen in
1211 // InstCombineCalls separately.
1212 AI.registerAttrInference(AttributeInferer::InferenceDescriptor{
1213 Attribute::Convergent,
1214 // Skip non-convergent functions.
1215 [](const Function &F) { return !F.isConvergent(); },
1216 // Instructions that break non-convergent assumption.
1217 [SCCNodes](Instruction &I) {
1218 return InstrBreaksNonConvergent(I, SCCNodes);
1221 LLVM_DEBUG(dbgs() << "Removing convergent attr from fn " << F.getName()
1223 F.setNotConvergent();
1225 /* RequiresExactDefinition= */ false});
1227 if (!DisableNoUnwindInference)
1228 // Request to infer nounwind attribute for all the functions in the SCC if
1229 // every callsite within the SCC is not throwing (except for calls to
1230 // functions within the SCC). Note that nounwind attribute suffers from
1231 // derefinement - results may change depending on how functions are
1232 // optimized. Thus it can be inferred only from exact definitions.
1233 AI.registerAttrInference(AttributeInferer::InferenceDescriptor{
1234 Attribute::NoUnwind,
1235 // Skip non-throwing functions.
1236 [](const Function &F) { return F.doesNotThrow(); },
1237 // Instructions that break non-throwing assumption.
1238 [SCCNodes](Instruction &I) {
1239 return InstrBreaksNonThrowing(I, SCCNodes);
1243 << "Adding nounwind attr to fn " << F.getName() << "\n");
1244 F.setDoesNotThrow();
1247 /* RequiresExactDefinition= */ true});
1249 // Perform all the requested attribute inference actions.
1250 return AI.run(SCCNodes);
1253 static bool setDoesNotRecurse(Function &F) {
1254 if (F.doesNotRecurse())
1256 F.setDoesNotRecurse();
1261 static bool addNoRecurseAttrs(const SCCNodeSet &SCCNodes) {
1262 // Try and identify functions that do not recurse.
1264 // If the SCC contains multiple nodes we know for sure there is recursion.
1265 if (SCCNodes.size() != 1)
1268 Function *F = *SCCNodes.begin();
1269 if (!F || F->isDeclaration() || F->doesNotRecurse())
1272 // If all of the calls in F are identifiable and are to norecurse functions, F
1273 // is norecurse. This check also detects self-recursion as F is not currently
1274 // marked norecurse, so any called from F to F will not be marked norecurse.
1275 for (Instruction &I : instructions(*F))
1276 if (auto CS = CallSite(&I)) {
1277 Function *Callee = CS.getCalledFunction();
1278 if (!Callee || Callee == F || !Callee->doesNotRecurse())
1279 // Function calls a potentially recursive function.
1283 // Every call was to a non-recursive function other than this function, and
1284 // we have no indirect recursion as the SCC size is one. This function cannot
1286 return setDoesNotRecurse(*F);
1289 PreservedAnalyses PostOrderFunctionAttrsPass::run(LazyCallGraph::SCC &C,
1290 CGSCCAnalysisManager &AM,
1292 CGSCCUpdateResult &) {
1293 FunctionAnalysisManager &FAM =
1294 AM.getResult<FunctionAnalysisManagerCGSCCProxy>(C, CG).getManager();
1296 // We pass a lambda into functions to wire them up to the analysis manager
1297 // for getting function analyses.
1298 auto AARGetter = [&](Function &F) -> AAResults & {
1299 return FAM.getResult<AAManager>(F);
1302 // Fill SCCNodes with the elements of the SCC. Also track whether there are
1303 // any external or opt-none nodes that will prevent us from optimizing any
1305 SCCNodeSet SCCNodes;
1306 bool HasUnknownCall = false;
1307 for (LazyCallGraph::Node &N : C) {
1308 Function &F = N.getFunction();
1309 if (F.hasFnAttribute(Attribute::OptimizeNone) ||
1310 F.hasFnAttribute(Attribute::Naked)) {
1311 // Treat any function we're trying not to optimize as if it were an
1312 // indirect call and omit it from the node set used below.
1313 HasUnknownCall = true;
1316 // Track whether any functions in this SCC have an unknown call edge.
1317 // Note: if this is ever a performance hit, we can common it with
1318 // subsequent routines which also do scans over the instructions of the
1320 if (!HasUnknownCall)
1321 for (Instruction &I : instructions(F))
1322 if (auto CS = CallSite(&I))
1323 if (!CS.getCalledFunction()) {
1324 HasUnknownCall = true;
1328 SCCNodes.insert(&F);
1331 bool Changed = false;
1332 Changed |= addArgumentReturnedAttrs(SCCNodes);
1333 Changed |= addReadAttrs(SCCNodes, AARGetter);
1334 Changed |= addArgumentAttrs(SCCNodes);
1336 // If we have no external nodes participating in the SCC, we can deduce some
1337 // more precise attributes as well.
1338 if (!HasUnknownCall) {
1339 Changed |= addNoAliasAttrs(SCCNodes);
1340 Changed |= addNonNullAttrs(SCCNodes);
1341 Changed |= inferAttrsFromFunctionBodies(SCCNodes);
1342 Changed |= addNoRecurseAttrs(SCCNodes);
1345 return Changed ? PreservedAnalyses::none() : PreservedAnalyses::all();
1350 struct PostOrderFunctionAttrsLegacyPass : public CallGraphSCCPass {
1351 // Pass identification, replacement for typeid
1354 PostOrderFunctionAttrsLegacyPass() : CallGraphSCCPass(ID) {
1355 initializePostOrderFunctionAttrsLegacyPassPass(
1356 *PassRegistry::getPassRegistry());
1359 bool runOnSCC(CallGraphSCC &SCC) override;
1361 void getAnalysisUsage(AnalysisUsage &AU) const override {
1362 AU.setPreservesCFG();
1363 AU.addRequired<AssumptionCacheTracker>();
1364 getAAResultsAnalysisUsage(AU);
1365 CallGraphSCCPass::getAnalysisUsage(AU);
1369 } // end anonymous namespace
1371 char PostOrderFunctionAttrsLegacyPass::ID = 0;
1372 INITIALIZE_PASS_BEGIN(PostOrderFunctionAttrsLegacyPass, "functionattrs",
1373 "Deduce function attributes", false, false)
1374 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
1375 INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
1376 INITIALIZE_PASS_END(PostOrderFunctionAttrsLegacyPass, "functionattrs",
1377 "Deduce function attributes", false, false)
1379 Pass *llvm::createPostOrderFunctionAttrsLegacyPass() {
1380 return new PostOrderFunctionAttrsLegacyPass();
1383 template <typename AARGetterT>
1384 static bool runImpl(CallGraphSCC &SCC, AARGetterT AARGetter) {
1385 bool Changed = false;
1387 // Fill SCCNodes with the elements of the SCC. Used for quickly looking up
1388 // whether a given CallGraphNode is in this SCC. Also track whether there are
1389 // any external or opt-none nodes that will prevent us from optimizing any
1391 SCCNodeSet SCCNodes;
1392 bool ExternalNode = false;
1393 for (CallGraphNode *I : SCC) {
1394 Function *F = I->getFunction();
1395 if (!F || F->hasFnAttribute(Attribute::OptimizeNone) ||
1396 F->hasFnAttribute(Attribute::Naked)) {
1397 // External node or function we're trying not to optimize - we both avoid
1398 // transform them and avoid leveraging information they provide.
1399 ExternalNode = true;
1406 // Skip it if the SCC only contains optnone functions.
1407 if (SCCNodes.empty())
1410 Changed |= addArgumentReturnedAttrs(SCCNodes);
1411 Changed |= addReadAttrs(SCCNodes, AARGetter);
1412 Changed |= addArgumentAttrs(SCCNodes);
1414 // If we have no external nodes participating in the SCC, we can deduce some
1415 // more precise attributes as well.
1416 if (!ExternalNode) {
1417 Changed |= addNoAliasAttrs(SCCNodes);
1418 Changed |= addNonNullAttrs(SCCNodes);
1419 Changed |= inferAttrsFromFunctionBodies(SCCNodes);
1420 Changed |= addNoRecurseAttrs(SCCNodes);
1426 bool PostOrderFunctionAttrsLegacyPass::runOnSCC(CallGraphSCC &SCC) {
1429 return runImpl(SCC, LegacyAARGetter(*this));
1434 struct ReversePostOrderFunctionAttrsLegacyPass : public ModulePass {
1435 // Pass identification, replacement for typeid
1438 ReversePostOrderFunctionAttrsLegacyPass() : ModulePass(ID) {
1439 initializeReversePostOrderFunctionAttrsLegacyPassPass(
1440 *PassRegistry::getPassRegistry());
1443 bool runOnModule(Module &M) override;
1445 void getAnalysisUsage(AnalysisUsage &AU) const override {
1446 AU.setPreservesCFG();
1447 AU.addRequired<CallGraphWrapperPass>();
1448 AU.addPreserved<CallGraphWrapperPass>();
1452 } // end anonymous namespace
1454 char ReversePostOrderFunctionAttrsLegacyPass::ID = 0;
1456 INITIALIZE_PASS_BEGIN(ReversePostOrderFunctionAttrsLegacyPass, "rpo-functionattrs",
1457 "Deduce function attributes in RPO", false, false)
1458 INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
1459 INITIALIZE_PASS_END(ReversePostOrderFunctionAttrsLegacyPass, "rpo-functionattrs",
1460 "Deduce function attributes in RPO", false, false)
1462 Pass *llvm::createReversePostOrderFunctionAttrsPass() {
1463 return new ReversePostOrderFunctionAttrsLegacyPass();
1466 static bool addNoRecurseAttrsTopDown(Function &F) {
1467 // We check the preconditions for the function prior to calling this to avoid
1468 // the cost of building up a reversible post-order list. We assert them here
1469 // to make sure none of the invariants this relies on were violated.
1470 assert(!F.isDeclaration() && "Cannot deduce norecurse without a definition!");
1471 assert(!F.doesNotRecurse() &&
1472 "This function has already been deduced as norecurs!");
1473 assert(F.hasInternalLinkage() &&
1474 "Can only do top-down deduction for internal linkage functions!");
1476 // If F is internal and all of its uses are calls from a non-recursive
1477 // functions, then none of its calls could in fact recurse without going
1478 // through a function marked norecurse, and so we can mark this function too
1479 // as norecurse. Note that the uses must actually be calls -- otherwise
1480 // a pointer to this function could be returned from a norecurse function but
1481 // this function could be recursively (indirectly) called. Note that this
1482 // also detects if F is directly recursive as F is not yet marked as
1483 // a norecurse function.
1484 for (auto *U : F.users()) {
1485 auto *I = dyn_cast<Instruction>(U);
1489 if (!CS || !CS.getParent()->getParent()->doesNotRecurse())
1492 return setDoesNotRecurse(F);
1495 static bool deduceFunctionAttributeInRPO(Module &M, CallGraph &CG) {
1496 // We only have a post-order SCC traversal (because SCCs are inherently
1497 // discovered in post-order), so we accumulate them in a vector and then walk
1498 // it in reverse. This is simpler than using the RPO iterator infrastructure
1499 // because we need to combine SCC detection and the PO walk of the call
1500 // graph. We can also cheat egregiously because we're primarily interested in
1501 // synthesizing norecurse and so we can only save the singular SCCs as SCCs
1502 // with multiple functions in them will clearly be recursive.
1503 SmallVector<Function *, 16> Worklist;
1504 for (scc_iterator<CallGraph *> I = scc_begin(&CG); !I.isAtEnd(); ++I) {
1508 Function *F = I->front()->getFunction();
1509 if (F && !F->isDeclaration() && !F->doesNotRecurse() &&
1510 F->hasInternalLinkage())
1511 Worklist.push_back(F);
1514 bool Changed = false;
1515 for (auto *F : llvm::reverse(Worklist))
1516 Changed |= addNoRecurseAttrsTopDown(*F);
1521 bool ReversePostOrderFunctionAttrsLegacyPass::runOnModule(Module &M) {
1525 auto &CG = getAnalysis<CallGraphWrapperPass>().getCallGraph();
1527 return deduceFunctionAttributeInRPO(M, CG);
1531 ReversePostOrderFunctionAttrsPass::run(Module &M, ModuleAnalysisManager &AM) {
1532 auto &CG = AM.getResult<CallGraphAnalysis>(M);
1534 if (!deduceFunctionAttributeInRPO(M, CG))
1535 return PreservedAnalyses::all();
1537 PreservedAnalyses PA;
1538 PA.preserve<CallGraphAnalysis>();