1 //===- FunctionAttrs.cpp - Pass which marks functions attributes ----------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
10 /// This file implements interprocedural passes which walk the
11 /// call-graph deducing and/or propagating function attributes.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/Transforms/IPO/FunctionAttrs.h"
16 #include "llvm/ADT/SCCIterator.h"
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/ADT/SetVector.h"
19 #include "llvm/ADT/SmallPtrSet.h"
20 #include "llvm/ADT/SmallVector.h"
21 #include "llvm/ADT/Statistic.h"
22 #include "llvm/Analysis/AliasAnalysis.h"
23 #include "llvm/Analysis/AssumptionCache.h"
24 #include "llvm/Analysis/BasicAliasAnalysis.h"
25 #include "llvm/Analysis/CGSCCPassManager.h"
26 #include "llvm/Analysis/CallGraph.h"
27 #include "llvm/Analysis/CallGraphSCCPass.h"
28 #include "llvm/Analysis/CaptureTracking.h"
29 #include "llvm/Analysis/LazyCallGraph.h"
30 #include "llvm/Analysis/MemoryBuiltins.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/IntrinsicInst.h"
45 #include "llvm/IR/Metadata.h"
46 #include "llvm/IR/PassManager.h"
47 #include "llvm/IR/Type.h"
48 #include "llvm/IR/Use.h"
49 #include "llvm/IR/User.h"
50 #include "llvm/IR/Value.h"
51 #include "llvm/InitializePasses.h"
52 #include "llvm/Pass.h"
53 #include "llvm/Support/Casting.h"
54 #include "llvm/Support/CommandLine.h"
55 #include "llvm/Support/Compiler.h"
56 #include "llvm/Support/Debug.h"
57 #include "llvm/Support/ErrorHandling.h"
58 #include "llvm/Support/raw_ostream.h"
59 #include "llvm/Transforms/IPO.h"
67 #define DEBUG_TYPE "functionattrs"
69 STATISTIC(NumReadNone, "Number of functions marked readnone");
70 STATISTIC(NumReadOnly, "Number of functions marked readonly");
71 STATISTIC(NumWriteOnly, "Number of functions marked writeonly");
72 STATISTIC(NumNoCapture, "Number of arguments marked nocapture");
73 STATISTIC(NumReturned, "Number of arguments marked returned");
74 STATISTIC(NumReadNoneArg, "Number of arguments marked readnone");
75 STATISTIC(NumReadOnlyArg, "Number of arguments marked readonly");
76 STATISTIC(NumNoAlias, "Number of function returns marked noalias");
77 STATISTIC(NumNonNullReturn, "Number of function returns marked nonnull");
78 STATISTIC(NumNoRecurse, "Number of functions marked as norecurse");
79 STATISTIC(NumNoUnwind, "Number of functions marked as nounwind");
80 STATISTIC(NumNoFree, "Number of functions marked as nofree");
82 static cl::opt<bool> EnableNonnullArgPropagation(
83 "enable-nonnull-arg-prop", cl::init(true), cl::Hidden,
84 cl::desc("Try to propagate nonnull argument attributes from callsites to "
85 "caller functions."));
87 static cl::opt<bool> DisableNoUnwindInference(
88 "disable-nounwind-inference", cl::Hidden,
89 cl::desc("Stop inferring nounwind attribute during function-attrs pass"));
91 static cl::opt<bool> DisableNoFreeInference(
92 "disable-nofree-inference", cl::Hidden,
93 cl::desc("Stop inferring nofree attribute during function-attrs pass"));
97 using SCCNodeSet = SmallSetVector<Function *, 8>;
99 } // end anonymous namespace
101 /// Returns the memory access attribute for function F using AAR for AA results,
102 /// where SCCNodes is the current SCC.
104 /// If ThisBody is true, this function may examine the function body and will
105 /// return a result pertaining to this copy of the function. If it is false, the
106 /// result will be based only on AA results for the function declaration; it
107 /// will be assumed that some other (perhaps less optimized) version of the
108 /// function may be selected at link time.
109 static MemoryAccessKind checkFunctionMemoryAccess(Function &F, bool ThisBody,
111 const SCCNodeSet &SCCNodes) {
112 FunctionModRefBehavior MRB = AAR.getModRefBehavior(&F);
113 if (MRB == FMRB_DoesNotAccessMemory)
118 if (AliasAnalysis::onlyReadsMemory(MRB))
121 if (AliasAnalysis::doesNotReadMemory(MRB))
122 return MAK_WriteOnly;
124 // Conservatively assume it reads and writes to memory.
128 // Scan the function body for instructions that may read or write memory.
129 bool ReadsMemory = false;
130 bool WritesMemory = false;
131 for (inst_iterator II = inst_begin(F), E = inst_end(F); II != E; ++II) {
132 Instruction *I = &*II;
134 // Some instructions can be ignored even if they read or write memory.
135 // Detect these now, skipping to the next instruction if one is found.
136 if (auto *Call = dyn_cast<CallBase>(I)) {
137 // Ignore calls to functions in the same SCC, as long as the call sites
138 // don't have operand bundles. Calls with operand bundles are allowed to
139 // have memory effects not described by the memory effects of the call
141 if (!Call->hasOperandBundles() && Call->getCalledFunction() &&
142 SCCNodes.count(Call->getCalledFunction()))
144 FunctionModRefBehavior MRB = AAR.getModRefBehavior(Call);
145 ModRefInfo MRI = createModRefInfo(MRB);
147 // If the call doesn't access memory, we're done.
151 if (!AliasAnalysis::onlyAccessesArgPointees(MRB)) {
152 // The call could access any memory. If that includes writes, note it.
155 // If it reads, note it.
161 // Check whether all pointer arguments point to local memory, and
162 // ignore calls that only access local memory.
163 for (CallSite::arg_iterator CI = Call->arg_begin(), CE = Call->arg_end();
166 if (!Arg->getType()->isPtrOrPtrVectorTy())
170 I->getAAMetadata(AAInfo);
171 MemoryLocation Loc(Arg, LocationSize::unknown(), AAInfo);
173 // Skip accesses to local or constant memory as they don't impact the
174 // externally visible mod/ref behavior.
175 if (AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true))
179 // Writes non-local memory.
182 // Ok, it reads non-local memory.
186 } else if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
187 // Ignore non-volatile loads from local memory. (Atomic is okay here.)
188 if (!LI->isVolatile()) {
189 MemoryLocation Loc = MemoryLocation::get(LI);
190 if (AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true))
193 } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
194 // Ignore non-volatile stores to local memory. (Atomic is okay here.)
195 if (!SI->isVolatile()) {
196 MemoryLocation Loc = MemoryLocation::get(SI);
197 if (AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true))
200 } else if (VAArgInst *VI = dyn_cast<VAArgInst>(I)) {
201 // Ignore vaargs on local memory.
202 MemoryLocation Loc = MemoryLocation::get(VI);
203 if (AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true))
207 // Any remaining instructions need to be taken seriously! Check if they
208 // read or write memory.
210 // Writes memory, remember that.
211 WritesMemory |= I->mayWriteToMemory();
213 // If this instruction may read memory, remember that.
214 ReadsMemory |= I->mayReadFromMemory();
219 return MAK_WriteOnly;
224 return ReadsMemory ? MAK_ReadOnly : MAK_ReadNone;
227 MemoryAccessKind llvm::computeFunctionBodyMemoryAccess(Function &F,
229 return checkFunctionMemoryAccess(F, /*ThisBody=*/true, AAR, {});
232 /// Deduce readonly/readnone attributes for the SCC.
233 template <typename AARGetterT>
234 static bool addReadAttrs(const SCCNodeSet &SCCNodes, AARGetterT &&AARGetter) {
235 // Check if any of the functions in the SCC read or write memory. If they
236 // write memory then they can't be marked readnone or readonly.
237 bool ReadsMemory = false;
238 bool WritesMemory = false;
239 for (Function *F : SCCNodes) {
240 // Call the callable parameter to look up AA results for this function.
241 AAResults &AAR = AARGetter(*F);
243 // Non-exact function definitions may not be selected at link time, and an
244 // alternative version that writes to memory may be selected. See the
245 // comment on GlobalValue::isDefinitionExact for more details.
246 switch (checkFunctionMemoryAccess(*F, F->hasExactDefinition(),
262 // If the SCC contains both functions that read and functions that write, then
263 // we cannot add readonly attributes.
264 if (ReadsMemory && WritesMemory)
267 // Success! Functions in this SCC do not access memory, or only read memory.
268 // Give them the appropriate attribute.
269 bool MadeChange = false;
271 for (Function *F : SCCNodes) {
272 if (F->doesNotAccessMemory())
276 if (F->onlyReadsMemory() && ReadsMemory)
280 if (F->doesNotReadMemory() && WritesMemory)
285 // Clear out any existing attributes.
286 F->removeFnAttr(Attribute::ReadOnly);
287 F->removeFnAttr(Attribute::ReadNone);
288 F->removeFnAttr(Attribute::WriteOnly);
290 if (!WritesMemory && !ReadsMemory) {
291 // Clear out any "access range attributes" if readnone was deduced.
292 F->removeFnAttr(Attribute::ArgMemOnly);
293 F->removeFnAttr(Attribute::InaccessibleMemOnly);
294 F->removeFnAttr(Attribute::InaccessibleMemOrArgMemOnly);
297 // Add in the new attribute.
298 if (WritesMemory && !ReadsMemory)
299 F->addFnAttr(Attribute::WriteOnly);
301 F->addFnAttr(ReadsMemory ? Attribute::ReadOnly : Attribute::ReadNone);
303 if (WritesMemory && !ReadsMemory)
305 else if (ReadsMemory)
316 /// For a given pointer Argument, this retains a list of Arguments of functions
317 /// in the same SCC that the pointer data flows into. We use this to build an
318 /// SCC of the arguments.
319 struct ArgumentGraphNode {
320 Argument *Definition;
321 SmallVector<ArgumentGraphNode *, 4> Uses;
324 class ArgumentGraph {
325 // We store pointers to ArgumentGraphNode objects, so it's important that
326 // that they not move around upon insert.
327 using ArgumentMapTy = std::map<Argument *, ArgumentGraphNode>;
329 ArgumentMapTy ArgumentMap;
331 // There is no root node for the argument graph, in fact:
332 // void f(int *x, int *y) { if (...) f(x, y); }
333 // is an example where the graph is disconnected. The SCCIterator requires a
334 // single entry point, so we maintain a fake ("synthetic") root node that
335 // uses every node. Because the graph is directed and nothing points into
336 // the root, it will not participate in any SCCs (except for its own).
337 ArgumentGraphNode SyntheticRoot;
340 ArgumentGraph() { SyntheticRoot.Definition = nullptr; }
342 using iterator = SmallVectorImpl<ArgumentGraphNode *>::iterator;
344 iterator begin() { return SyntheticRoot.Uses.begin(); }
345 iterator end() { return SyntheticRoot.Uses.end(); }
346 ArgumentGraphNode *getEntryNode() { return &SyntheticRoot; }
348 ArgumentGraphNode *operator[](Argument *A) {
349 ArgumentGraphNode &Node = ArgumentMap[A];
351 SyntheticRoot.Uses.push_back(&Node);
356 /// This tracker checks whether callees are in the SCC, and if so it does not
357 /// consider that a capture, instead adding it to the "Uses" list and
358 /// continuing with the analysis.
359 struct ArgumentUsesTracker : public CaptureTracker {
360 ArgumentUsesTracker(const SCCNodeSet &SCCNodes) : SCCNodes(SCCNodes) {}
362 void tooManyUses() override { Captured = true; }
364 bool captured(const Use *U) override {
365 CallSite CS(U->getUser());
366 if (!CS.getInstruction()) {
371 Function *F = CS.getCalledFunction();
372 if (!F || !F->hasExactDefinition() || !SCCNodes.count(F)) {
377 // Note: the callee and the two successor blocks *follow* the argument
378 // operands. This means there is no need to adjust UseIndex to account for
382 std::distance(const_cast<const Use *>(CS.arg_begin()), U);
384 assert(UseIndex < CS.data_operands_size() &&
385 "Indirect function calls should have been filtered above!");
387 if (UseIndex >= CS.getNumArgOperands()) {
388 // Data operand, but not a argument operand -- must be a bundle operand
389 assert(CS.hasOperandBundles() && "Must be!");
391 // CaptureTracking told us that we're being captured by an operand bundle
392 // use. In this case it does not matter if the callee is within our SCC
393 // or not -- we've been captured in some unknown way, and we have to be
399 if (UseIndex >= F->arg_size()) {
400 assert(F->isVarArg() && "More params than args in non-varargs call");
405 Uses.push_back(&*std::next(F->arg_begin(), UseIndex));
409 // True only if certainly captured (used outside our SCC).
410 bool Captured = false;
412 // Uses within our SCC.
413 SmallVector<Argument *, 4> Uses;
415 const SCCNodeSet &SCCNodes;
418 } // end anonymous namespace
422 template <> struct GraphTraits<ArgumentGraphNode *> {
423 using NodeRef = ArgumentGraphNode *;
424 using ChildIteratorType = SmallVectorImpl<ArgumentGraphNode *>::iterator;
426 static NodeRef getEntryNode(NodeRef A) { return A; }
427 static ChildIteratorType child_begin(NodeRef N) { return N->Uses.begin(); }
428 static ChildIteratorType child_end(NodeRef N) { return N->Uses.end(); }
432 struct GraphTraits<ArgumentGraph *> : public GraphTraits<ArgumentGraphNode *> {
433 static NodeRef getEntryNode(ArgumentGraph *AG) { return AG->getEntryNode(); }
435 static ChildIteratorType nodes_begin(ArgumentGraph *AG) {
439 static ChildIteratorType nodes_end(ArgumentGraph *AG) { return AG->end(); }
442 } // end namespace llvm
444 /// Returns Attribute::None, Attribute::ReadOnly or Attribute::ReadNone.
445 static Attribute::AttrKind
446 determinePointerReadAttrs(Argument *A,
447 const SmallPtrSet<Argument *, 8> &SCCNodes) {
448 SmallVector<Use *, 32> Worklist;
449 SmallPtrSet<Use *, 32> Visited;
451 // inalloca arguments are always clobbered by the call.
452 if (A->hasInAllocaAttr())
453 return Attribute::None;
456 // We don't need to track IsWritten. If A is written to, return immediately.
458 for (Use &U : A->uses()) {
460 Worklist.push_back(&U);
463 while (!Worklist.empty()) {
464 Use *U = Worklist.pop_back_val();
465 Instruction *I = cast<Instruction>(U->getUser());
467 switch (I->getOpcode()) {
468 case Instruction::BitCast:
469 case Instruction::GetElementPtr:
470 case Instruction::PHI:
471 case Instruction::Select:
472 case Instruction::AddrSpaceCast:
473 // The original value is not read/written via this if the new value isn't.
474 for (Use &UU : I->uses())
475 if (Visited.insert(&UU).second)
476 Worklist.push_back(&UU);
479 case Instruction::Call:
480 case Instruction::Invoke: {
481 bool Captures = true;
483 if (I->getType()->isVoidTy())
486 auto AddUsersToWorklistIfCapturing = [&] {
488 for (Use &UU : I->uses())
489 if (Visited.insert(&UU).second)
490 Worklist.push_back(&UU);
494 if (CS.doesNotAccessMemory()) {
495 AddUsersToWorklistIfCapturing();
499 Function *F = CS.getCalledFunction();
501 if (CS.onlyReadsMemory()) {
503 AddUsersToWorklistIfCapturing();
506 return Attribute::None;
509 // Note: the callee and the two successor blocks *follow* the argument
510 // operands. This means there is no need to adjust UseIndex to account
513 unsigned UseIndex = std::distance(CS.arg_begin(), U);
515 // U cannot be the callee operand use: since we're exploring the
516 // transitive uses of an Argument, having such a use be a callee would
517 // imply the CallSite is an indirect call or invoke; and we'd take the
519 assert(UseIndex < CS.data_operands_size() &&
520 "Data operand use expected!");
522 bool IsOperandBundleUse = UseIndex >= CS.getNumArgOperands();
524 if (UseIndex >= F->arg_size() && !IsOperandBundleUse) {
525 assert(F->isVarArg() && "More params than args in non-varargs call");
526 return Attribute::None;
529 Captures &= !CS.doesNotCapture(UseIndex);
531 // Since the optimizer (by design) cannot see the data flow corresponding
532 // to a operand bundle use, these cannot participate in the optimistic SCC
533 // analysis. Instead, we model the operand bundle uses as arguments in
534 // call to a function external to the SCC.
535 if (IsOperandBundleUse ||
536 !SCCNodes.count(&*std::next(F->arg_begin(), UseIndex))) {
538 // The accessors used on CallSite here do the right thing for calls and
539 // invokes with operand bundles.
541 if (!CS.onlyReadsMemory() && !CS.onlyReadsMemory(UseIndex))
542 return Attribute::None;
543 if (!CS.doesNotAccessMemory(UseIndex))
547 AddUsersToWorklistIfCapturing();
551 case Instruction::Load:
552 // A volatile load has side effects beyond what readonly can be relied
554 if (cast<LoadInst>(I)->isVolatile())
555 return Attribute::None;
560 case Instruction::ICmp:
561 case Instruction::Ret:
565 return Attribute::None;
569 return IsRead ? Attribute::ReadOnly : Attribute::ReadNone;
572 /// Deduce returned attributes for the SCC.
573 static bool addArgumentReturnedAttrs(const SCCNodeSet &SCCNodes) {
574 bool Changed = false;
576 // Check each function in turn, determining if an argument is always returned.
577 for (Function *F : SCCNodes) {
578 // We can infer and propagate function attributes only when we know that the
579 // definition we'll get at link time is *exactly* the definition we see now.
580 // For more details, see GlobalValue::mayBeDerefined.
581 if (!F->hasExactDefinition())
584 if (F->getReturnType()->isVoidTy())
587 // There is nothing to do if an argument is already marked as 'returned'.
588 if (llvm::any_of(F->args(),
589 [](const Argument &Arg) { return Arg.hasReturnedAttr(); }))
592 auto FindRetArg = [&]() -> Value * {
593 Value *RetArg = nullptr;
594 for (BasicBlock &BB : *F)
595 if (auto *Ret = dyn_cast<ReturnInst>(BB.getTerminator())) {
596 // Note that stripPointerCasts should look through functions with
597 // returned arguments.
598 Value *RetVal = Ret->getReturnValue()->stripPointerCasts();
599 if (!isa<Argument>(RetVal) || RetVal->getType() != F->getReturnType())
604 else if (RetArg != RetVal)
611 if (Value *RetArg = FindRetArg()) {
612 auto *A = cast<Argument>(RetArg);
613 A->addAttr(Attribute::Returned);
622 /// If a callsite has arguments that are also arguments to the parent function,
623 /// try to propagate attributes from the callsite's arguments to the parent's
624 /// arguments. This may be important because inlining can cause information loss
625 /// when attribute knowledge disappears with the inlined call.
626 static bool addArgumentAttrsFromCallsites(Function &F) {
627 if (!EnableNonnullArgPropagation)
630 bool Changed = false;
632 // For an argument attribute to transfer from a callsite to the parent, the
633 // call must be guaranteed to execute every time the parent is called.
634 // Conservatively, just check for calls in the entry block that are guaranteed
636 // TODO: This could be enhanced by testing if the callsite post-dominates the
637 // entry block or by doing simple forward walks or backward walks to the
639 BasicBlock &Entry = F.getEntryBlock();
640 for (Instruction &I : Entry) {
641 if (auto CS = CallSite(&I)) {
642 if (auto *CalledFunc = CS.getCalledFunction()) {
643 for (auto &CSArg : CalledFunc->args()) {
644 if (!CSArg.hasNonNullAttr())
647 // If the non-null callsite argument operand is an argument to 'F'
648 // (the caller) and the call is guaranteed to execute, then the value
649 // must be non-null throughout 'F'.
650 auto *FArg = dyn_cast<Argument>(CS.getArgOperand(CSArg.getArgNo()));
651 if (FArg && !FArg->hasNonNullAttr()) {
652 FArg->addAttr(Attribute::NonNull);
658 if (!isGuaranteedToTransferExecutionToSuccessor(&I))
665 static bool addReadAttr(Argument *A, Attribute::AttrKind R) {
666 assert((R == Attribute::ReadOnly || R == Attribute::ReadNone)
667 && "Must be a Read attribute.");
668 assert(A && "Argument must not be null.");
670 // If the argument already has the attribute, nothing needs to be done.
671 if (A->hasAttribute(R))
674 // Otherwise, remove potentially conflicting attribute, add the new one,
675 // and update statistics.
676 A->removeAttr(Attribute::WriteOnly);
677 A->removeAttr(Attribute::ReadOnly);
678 A->removeAttr(Attribute::ReadNone);
680 R == Attribute::ReadOnly ? ++NumReadOnlyArg : ++NumReadNoneArg;
684 /// Deduce nocapture attributes for the SCC.
685 static bool addArgumentAttrs(const SCCNodeSet &SCCNodes) {
686 bool Changed = false;
690 // Check each function in turn, determining which pointer arguments are not
692 for (Function *F : SCCNodes) {
693 // We can infer and propagate function attributes only when we know that the
694 // definition we'll get at link time is *exactly* the definition we see now.
695 // For more details, see GlobalValue::mayBeDerefined.
696 if (!F->hasExactDefinition())
699 Changed |= addArgumentAttrsFromCallsites(*F);
701 // Functions that are readonly (or readnone) and nounwind and don't return
702 // a value can't capture arguments. Don't analyze them.
703 if (F->onlyReadsMemory() && F->doesNotThrow() &&
704 F->getReturnType()->isVoidTy()) {
705 for (Function::arg_iterator A = F->arg_begin(), E = F->arg_end(); A != E;
707 if (A->getType()->isPointerTy() && !A->hasNoCaptureAttr()) {
708 A->addAttr(Attribute::NoCapture);
716 for (Function::arg_iterator A = F->arg_begin(), E = F->arg_end(); A != E;
718 if (!A->getType()->isPointerTy())
720 bool HasNonLocalUses = false;
721 if (!A->hasNoCaptureAttr()) {
722 ArgumentUsesTracker Tracker(SCCNodes);
723 PointerMayBeCaptured(&*A, &Tracker);
724 if (!Tracker.Captured) {
725 if (Tracker.Uses.empty()) {
726 // If it's trivially not captured, mark it nocapture now.
727 A->addAttr(Attribute::NoCapture);
731 // If it's not trivially captured and not trivially not captured,
732 // then it must be calling into another function in our SCC. Save
733 // its particulars for Argument-SCC analysis later.
734 ArgumentGraphNode *Node = AG[&*A];
735 for (Argument *Use : Tracker.Uses) {
736 Node->Uses.push_back(AG[Use]);
738 HasNonLocalUses = true;
742 // Otherwise, it's captured. Don't bother doing SCC analysis on it.
744 if (!HasNonLocalUses && !A->onlyReadsMemory()) {
745 // Can we determine that it's readonly/readnone without doing an SCC?
746 // Note that we don't allow any calls at all here, or else our result
747 // will be dependent on the iteration order through the functions in the
749 SmallPtrSet<Argument *, 8> Self;
751 Attribute::AttrKind R = determinePointerReadAttrs(&*A, Self);
752 if (R != Attribute::None)
753 Changed = addReadAttr(A, R);
758 // The graph we've collected is partial because we stopped scanning for
759 // argument uses once we solved the argument trivially. These partial nodes
760 // show up as ArgumentGraphNode objects with an empty Uses list, and for
761 // these nodes the final decision about whether they capture has already been
762 // made. If the definition doesn't have a 'nocapture' attribute by now, it
765 for (scc_iterator<ArgumentGraph *> I = scc_begin(&AG); !I.isAtEnd(); ++I) {
766 const std::vector<ArgumentGraphNode *> &ArgumentSCC = *I;
767 if (ArgumentSCC.size() == 1) {
768 if (!ArgumentSCC[0]->Definition)
769 continue; // synthetic root node
771 // eg. "void f(int* x) { if (...) f(x); }"
772 if (ArgumentSCC[0]->Uses.size() == 1 &&
773 ArgumentSCC[0]->Uses[0] == ArgumentSCC[0]) {
774 Argument *A = ArgumentSCC[0]->Definition;
775 A->addAttr(Attribute::NoCapture);
782 bool SCCCaptured = false;
783 for (auto I = ArgumentSCC.begin(), E = ArgumentSCC.end();
784 I != E && !SCCCaptured; ++I) {
785 ArgumentGraphNode *Node = *I;
786 if (Node->Uses.empty()) {
787 if (!Node->Definition->hasNoCaptureAttr())
794 SmallPtrSet<Argument *, 8> ArgumentSCCNodes;
795 // Fill ArgumentSCCNodes with the elements of the ArgumentSCC. Used for
796 // quickly looking up whether a given Argument is in this ArgumentSCC.
797 for (ArgumentGraphNode *I : ArgumentSCC) {
798 ArgumentSCCNodes.insert(I->Definition);
801 for (auto I = ArgumentSCC.begin(), E = ArgumentSCC.end();
802 I != E && !SCCCaptured; ++I) {
803 ArgumentGraphNode *N = *I;
804 for (ArgumentGraphNode *Use : N->Uses) {
805 Argument *A = Use->Definition;
806 if (A->hasNoCaptureAttr() || ArgumentSCCNodes.count(A))
815 for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) {
816 Argument *A = ArgumentSCC[i]->Definition;
817 A->addAttr(Attribute::NoCapture);
822 // We also want to compute readonly/readnone. With a small number of false
823 // negatives, we can assume that any pointer which is captured isn't going
824 // to be provably readonly or readnone, since by definition we can't
825 // analyze all uses of a captured pointer.
827 // The false negatives happen when the pointer is captured by a function
828 // that promises readonly/readnone behaviour on the pointer, then the
829 // pointer's lifetime ends before anything that writes to arbitrary memory.
830 // Also, a readonly/readnone pointer may be returned, but returning a
831 // pointer is capturing it.
833 Attribute::AttrKind ReadAttr = Attribute::ReadNone;
834 for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) {
835 Argument *A = ArgumentSCC[i]->Definition;
836 Attribute::AttrKind K = determinePointerReadAttrs(A, ArgumentSCCNodes);
837 if (K == Attribute::ReadNone)
839 if (K == Attribute::ReadOnly) {
840 ReadAttr = Attribute::ReadOnly;
847 if (ReadAttr != Attribute::None) {
848 for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) {
849 Argument *A = ArgumentSCC[i]->Definition;
850 Changed = addReadAttr(A, ReadAttr);
858 /// Tests whether a function is "malloc-like".
860 /// A function is "malloc-like" if it returns either null or a pointer that
861 /// doesn't alias any other pointer visible to the caller.
862 static bool isFunctionMallocLike(Function *F, const SCCNodeSet &SCCNodes) {
863 SmallSetVector<Value *, 8> FlowsToReturn;
864 for (BasicBlock &BB : *F)
865 if (ReturnInst *Ret = dyn_cast<ReturnInst>(BB.getTerminator()))
866 FlowsToReturn.insert(Ret->getReturnValue());
868 for (unsigned i = 0; i != FlowsToReturn.size(); ++i) {
869 Value *RetVal = FlowsToReturn[i];
871 if (Constant *C = dyn_cast<Constant>(RetVal)) {
872 if (!C->isNullValue() && !isa<UndefValue>(C))
878 if (isa<Argument>(RetVal))
881 if (Instruction *RVI = dyn_cast<Instruction>(RetVal))
882 switch (RVI->getOpcode()) {
883 // Extend the analysis by looking upwards.
884 case Instruction::BitCast:
885 case Instruction::GetElementPtr:
886 case Instruction::AddrSpaceCast:
887 FlowsToReturn.insert(RVI->getOperand(0));
889 case Instruction::Select: {
890 SelectInst *SI = cast<SelectInst>(RVI);
891 FlowsToReturn.insert(SI->getTrueValue());
892 FlowsToReturn.insert(SI->getFalseValue());
895 case Instruction::PHI: {
896 PHINode *PN = cast<PHINode>(RVI);
897 for (Value *IncValue : PN->incoming_values())
898 FlowsToReturn.insert(IncValue);
902 // Check whether the pointer came from an allocation.
903 case Instruction::Alloca:
905 case Instruction::Call:
906 case Instruction::Invoke: {
908 if (CS.hasRetAttr(Attribute::NoAlias))
910 if (CS.getCalledFunction() && SCCNodes.count(CS.getCalledFunction()))
915 return false; // Did not come from an allocation.
918 if (PointerMayBeCaptured(RetVal, false, /*StoreCaptures=*/false))
925 /// Deduce noalias attributes for the SCC.
926 static bool addNoAliasAttrs(const SCCNodeSet &SCCNodes) {
927 // Check each function in turn, determining which functions return noalias
929 for (Function *F : SCCNodes) {
931 if (F->returnDoesNotAlias())
934 // We can infer and propagate function attributes only when we know that the
935 // definition we'll get at link time is *exactly* the definition we see now.
936 // For more details, see GlobalValue::mayBeDerefined.
937 if (!F->hasExactDefinition())
940 // We annotate noalias return values, which are only applicable to
942 if (!F->getReturnType()->isPointerTy())
945 if (!isFunctionMallocLike(F, SCCNodes))
949 bool MadeChange = false;
950 for (Function *F : SCCNodes) {
951 if (F->returnDoesNotAlias() ||
952 !F->getReturnType()->isPointerTy())
955 F->setReturnDoesNotAlias();
963 /// Tests whether this function is known to not return null.
965 /// Requires that the function returns a pointer.
967 /// Returns true if it believes the function will not return a null, and sets
968 /// \p Speculative based on whether the returned conclusion is a speculative
969 /// conclusion due to SCC calls.
970 static bool isReturnNonNull(Function *F, const SCCNodeSet &SCCNodes,
972 assert(F->getReturnType()->isPointerTy() &&
973 "nonnull only meaningful on pointer types");
976 SmallSetVector<Value *, 8> FlowsToReturn;
977 for (BasicBlock &BB : *F)
978 if (auto *Ret = dyn_cast<ReturnInst>(BB.getTerminator()))
979 FlowsToReturn.insert(Ret->getReturnValue());
981 auto &DL = F->getParent()->getDataLayout();
983 for (unsigned i = 0; i != FlowsToReturn.size(); ++i) {
984 Value *RetVal = FlowsToReturn[i];
986 // If this value is locally known to be non-null, we're good
987 if (isKnownNonZero(RetVal, DL))
990 // Otherwise, we need to look upwards since we can't make any local
992 Instruction *RVI = dyn_cast<Instruction>(RetVal);
995 switch (RVI->getOpcode()) {
996 // Extend the analysis by looking upwards.
997 case Instruction::BitCast:
998 case Instruction::GetElementPtr:
999 case Instruction::AddrSpaceCast:
1000 FlowsToReturn.insert(RVI->getOperand(0));
1002 case Instruction::Select: {
1003 SelectInst *SI = cast<SelectInst>(RVI);
1004 FlowsToReturn.insert(SI->getTrueValue());
1005 FlowsToReturn.insert(SI->getFalseValue());
1008 case Instruction::PHI: {
1009 PHINode *PN = cast<PHINode>(RVI);
1010 for (int i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
1011 FlowsToReturn.insert(PN->getIncomingValue(i));
1014 case Instruction::Call:
1015 case Instruction::Invoke: {
1017 Function *Callee = CS.getCalledFunction();
1018 // A call to a node within the SCC is assumed to return null until
1020 if (Callee && SCCNodes.count(Callee)) {
1027 return false; // Unknown source, may be null
1029 llvm_unreachable("should have either continued or returned");
1035 /// Deduce nonnull attributes for the SCC.
1036 static bool addNonNullAttrs(const SCCNodeSet &SCCNodes) {
1037 // Speculative that all functions in the SCC return only nonnull
1038 // pointers. We may refute this as we analyze functions.
1039 bool SCCReturnsNonNull = true;
1041 bool MadeChange = false;
1043 // Check each function in turn, determining which functions return nonnull
1045 for (Function *F : SCCNodes) {
1047 if (F->getAttributes().hasAttribute(AttributeList::ReturnIndex,
1048 Attribute::NonNull))
1051 // We can infer and propagate function attributes only when we know that the
1052 // definition we'll get at link time is *exactly* the definition we see now.
1053 // For more details, see GlobalValue::mayBeDerefined.
1054 if (!F->hasExactDefinition())
1057 // We annotate nonnull return values, which are only applicable to
1059 if (!F->getReturnType()->isPointerTy())
1062 bool Speculative = false;
1063 if (isReturnNonNull(F, SCCNodes, Speculative)) {
1065 // Mark the function eagerly since we may discover a function
1066 // which prevents us from speculating about the entire SCC
1067 LLVM_DEBUG(dbgs() << "Eagerly marking " << F->getName()
1068 << " as nonnull\n");
1069 F->addAttribute(AttributeList::ReturnIndex, Attribute::NonNull);
1075 // At least one function returns something which could be null, can't
1076 // speculate any more.
1077 SCCReturnsNonNull = false;
1080 if (SCCReturnsNonNull) {
1081 for (Function *F : SCCNodes) {
1082 if (F->getAttributes().hasAttribute(AttributeList::ReturnIndex,
1083 Attribute::NonNull) ||
1084 !F->getReturnType()->isPointerTy())
1087 LLVM_DEBUG(dbgs() << "SCC marking " << F->getName() << " as nonnull\n");
1088 F->addAttribute(AttributeList::ReturnIndex, Attribute::NonNull);
1099 /// Collects a set of attribute inference requests and performs them all in one
1100 /// go on a single SCC Node. Inference involves scanning function bodies
1101 /// looking for instructions that violate attribute assumptions.
1102 /// As soon as all the bodies are fine we are free to set the attribute.
1103 /// Customization of inference for individual attributes is performed by
1104 /// providing a handful of predicates for each attribute.
1105 class AttributeInferer {
1107 /// Describes a request for inference of a single attribute.
1108 struct InferenceDescriptor {
1110 /// Returns true if this function does not have to be handled.
1111 /// General intent for this predicate is to provide an optimization
1112 /// for functions that do not need this attribute inference at all
1113 /// (say, for functions that already have the attribute).
1114 std::function<bool(const Function &)> SkipFunction;
1116 /// Returns true if this instruction violates attribute assumptions.
1117 std::function<bool(Instruction &)> InstrBreaksAttribute;
1119 /// Sets the inferred attribute for this function.
1120 std::function<void(Function &)> SetAttribute;
1122 /// Attribute we derive.
1123 Attribute::AttrKind AKind;
1125 /// If true, only "exact" definitions can be used to infer this attribute.
1126 /// See GlobalValue::isDefinitionExact.
1127 bool RequiresExactDefinition;
1129 InferenceDescriptor(Attribute::AttrKind AK,
1130 std::function<bool(const Function &)> SkipFunc,
1131 std::function<bool(Instruction &)> InstrScan,
1132 std::function<void(Function &)> SetAttr,
1134 : SkipFunction(SkipFunc), InstrBreaksAttribute(InstrScan),
1135 SetAttribute(SetAttr), AKind(AK),
1136 RequiresExactDefinition(ReqExactDef) {}
1140 SmallVector<InferenceDescriptor, 4> InferenceDescriptors;
1143 void registerAttrInference(InferenceDescriptor AttrInference) {
1144 InferenceDescriptors.push_back(AttrInference);
1147 bool run(const SCCNodeSet &SCCNodes);
1150 /// Perform all the requested attribute inference actions according to the
1151 /// attribute predicates stored before.
1152 bool AttributeInferer::run(const SCCNodeSet &SCCNodes) {
1153 SmallVector<InferenceDescriptor, 4> InferInSCC = InferenceDescriptors;
1154 // Go through all the functions in SCC and check corresponding attribute
1155 // assumptions for each of them. Attributes that are invalid for this SCC
1156 // will be removed from InferInSCC.
1157 for (Function *F : SCCNodes) {
1159 // No attributes whose assumptions are still valid - done.
1160 if (InferInSCC.empty())
1163 // Check if our attributes ever need scanning/can be scanned.
1164 llvm::erase_if(InferInSCC, [F](const InferenceDescriptor &ID) {
1165 if (ID.SkipFunction(*F))
1168 // Remove from further inference (invalidate) when visiting a function
1169 // that has no instructions to scan/has an unsuitable definition.
1170 return F->isDeclaration() ||
1171 (ID.RequiresExactDefinition && !F->hasExactDefinition());
1174 // For each attribute still in InferInSCC that doesn't explicitly skip F,
1175 // set up the F instructions scan to verify assumptions of the attribute.
1176 SmallVector<InferenceDescriptor, 4> InferInThisFunc;
1178 InferInSCC, std::back_inserter(InferInThisFunc),
1179 [F](const InferenceDescriptor &ID) { return !ID.SkipFunction(*F); });
1181 if (InferInThisFunc.empty())
1184 // Start instruction scan.
1185 for (Instruction &I : instructions(*F)) {
1186 llvm::erase_if(InferInThisFunc, [&](const InferenceDescriptor &ID) {
1187 if (!ID.InstrBreaksAttribute(I))
1189 // Remove attribute from further inference on any other functions
1190 // because attribute assumptions have just been violated.
1191 llvm::erase_if(InferInSCC, [&ID](const InferenceDescriptor &D) {
1192 return D.AKind == ID.AKind;
1194 // Remove attribute from the rest of current instruction scan.
1198 if (InferInThisFunc.empty())
1203 if (InferInSCC.empty())
1206 bool Changed = false;
1207 for (Function *F : SCCNodes)
1208 // At this point InferInSCC contains only functions that were either:
1209 // - explicitly skipped from scan/inference, or
1210 // - verified to have no instructions that break attribute assumptions.
1211 // Hence we just go and force the attribute for all non-skipped functions.
1212 for (auto &ID : InferInSCC) {
1213 if (ID.SkipFunction(*F))
1216 ID.SetAttribute(*F);
1221 } // end anonymous namespace
1223 /// Helper for non-Convergent inference predicate InstrBreaksAttribute.
1224 static bool InstrBreaksNonConvergent(Instruction &I,
1225 const SCCNodeSet &SCCNodes) {
1226 const CallSite CS(&I);
1227 // Breaks non-convergent assumption if CS is a convergent call to a function
1229 return CS && CS.isConvergent() && SCCNodes.count(CS.getCalledFunction()) == 0;
1232 /// Helper for NoUnwind inference predicate InstrBreaksAttribute.
1233 static bool InstrBreaksNonThrowing(Instruction &I, const SCCNodeSet &SCCNodes) {
1236 if (const auto *CI = dyn_cast<CallInst>(&I)) {
1237 if (Function *Callee = CI->getCalledFunction()) {
1238 // I is a may-throw call to a function inside our SCC. This doesn't
1239 // invalidate our current working assumption that the SCC is no-throw; we
1240 // just have to scan that other function.
1241 if (SCCNodes.count(Callee) > 0)
1248 /// Helper for NoFree inference predicate InstrBreaksAttribute.
1249 static bool InstrBreaksNoFree(Instruction &I, const SCCNodeSet &SCCNodes) {
1254 Function *Callee = CS.getCalledFunction();
1258 if (Callee->doesNotFreeMemory())
1261 if (SCCNodes.count(Callee) > 0)
1267 /// Infer attributes from all functions in the SCC by scanning every
1268 /// instruction for compliance to the attribute assumptions. Currently it
1270 /// - removal of Convergent attribute
1271 /// - addition of NoUnwind attribute
1273 /// Returns true if any changes to function attributes were made.
1274 static bool inferAttrsFromFunctionBodies(const SCCNodeSet &SCCNodes) {
1276 AttributeInferer AI;
1278 // Request to remove the convergent attribute from all functions in the SCC
1279 // if every callsite within the SCC is not convergent (except for calls
1280 // to functions within the SCC).
1281 // Note: Removal of the attr from the callsites will happen in
1282 // InstCombineCalls separately.
1283 AI.registerAttrInference(AttributeInferer::InferenceDescriptor{
1284 Attribute::Convergent,
1285 // Skip non-convergent functions.
1286 [](const Function &F) { return !F.isConvergent(); },
1287 // Instructions that break non-convergent assumption.
1288 [SCCNodes](Instruction &I) {
1289 return InstrBreaksNonConvergent(I, SCCNodes);
1292 LLVM_DEBUG(dbgs() << "Removing convergent attr from fn " << F.getName()
1294 F.setNotConvergent();
1296 /* RequiresExactDefinition= */ false});
1298 if (!DisableNoUnwindInference)
1299 // Request to infer nounwind attribute for all the functions in the SCC if
1300 // every callsite within the SCC is not throwing (except for calls to
1301 // functions within the SCC). Note that nounwind attribute suffers from
1302 // derefinement - results may change depending on how functions are
1303 // optimized. Thus it can be inferred only from exact definitions.
1304 AI.registerAttrInference(AttributeInferer::InferenceDescriptor{
1305 Attribute::NoUnwind,
1306 // Skip non-throwing functions.
1307 [](const Function &F) { return F.doesNotThrow(); },
1308 // Instructions that break non-throwing assumption.
1309 [SCCNodes](Instruction &I) {
1310 return InstrBreaksNonThrowing(I, SCCNodes);
1314 << "Adding nounwind attr to fn " << F.getName() << "\n");
1315 F.setDoesNotThrow();
1318 /* RequiresExactDefinition= */ true});
1320 if (!DisableNoFreeInference)
1321 // Request to infer nofree attribute for all the functions in the SCC if
1322 // every callsite within the SCC does not directly or indirectly free
1323 // memory (except for calls to functions within the SCC). Note that nofree
1324 // attribute suffers from derefinement - results may change depending on
1325 // how functions are optimized. Thus it can be inferred only from exact
1327 AI.registerAttrInference(AttributeInferer::InferenceDescriptor{
1329 // Skip functions known not to free memory.
1330 [](const Function &F) { return F.doesNotFreeMemory(); },
1331 // Instructions that break non-deallocating assumption.
1332 [SCCNodes](Instruction &I) {
1333 return InstrBreaksNoFree(I, SCCNodes);
1337 << "Adding nofree attr to fn " << F.getName() << "\n");
1338 F.setDoesNotFreeMemory();
1341 /* RequiresExactDefinition= */ true});
1343 // Perform all the requested attribute inference actions.
1344 return AI.run(SCCNodes);
1347 static bool setDoesNotRecurse(Function &F) {
1348 if (F.doesNotRecurse())
1350 F.setDoesNotRecurse();
1355 static bool addNoRecurseAttrs(const SCCNodeSet &SCCNodes) {
1356 // Try and identify functions that do not recurse.
1358 // If the SCC contains multiple nodes we know for sure there is recursion.
1359 if (SCCNodes.size() != 1)
1362 Function *F = *SCCNodes.begin();
1363 if (!F || !F->hasExactDefinition() || F->doesNotRecurse())
1366 // If all of the calls in F are identifiable and are to norecurse functions, F
1367 // is norecurse. This check also detects self-recursion as F is not currently
1368 // marked norecurse, so any called from F to F will not be marked norecurse.
1370 for (auto &I : BB.instructionsWithoutDebug())
1371 if (auto CS = CallSite(&I)) {
1372 Function *Callee = CS.getCalledFunction();
1373 if (!Callee || Callee == F || !Callee->doesNotRecurse())
1374 // Function calls a potentially recursive function.
1378 // Every call was to a non-recursive function other than this function, and
1379 // we have no indirect recursion as the SCC size is one. This function cannot
1381 return setDoesNotRecurse(*F);
1384 template <typename AARGetterT>
1385 static bool deriveAttrsInPostOrder(SCCNodeSet &SCCNodes,
1386 AARGetterT &&AARGetter,
1387 bool HasUnknownCall) {
1388 bool Changed = false;
1390 // Bail if the SCC only contains optnone functions.
1391 if (SCCNodes.empty())
1394 Changed |= addArgumentReturnedAttrs(SCCNodes);
1395 Changed |= addReadAttrs(SCCNodes, AARGetter);
1396 Changed |= addArgumentAttrs(SCCNodes);
1398 // If we have no external nodes participating in the SCC, we can deduce some
1399 // more precise attributes as well.
1400 if (!HasUnknownCall) {
1401 Changed |= addNoAliasAttrs(SCCNodes);
1402 Changed |= addNonNullAttrs(SCCNodes);
1403 Changed |= inferAttrsFromFunctionBodies(SCCNodes);
1404 Changed |= addNoRecurseAttrs(SCCNodes);
1410 PreservedAnalyses PostOrderFunctionAttrsPass::run(LazyCallGraph::SCC &C,
1411 CGSCCAnalysisManager &AM,
1413 CGSCCUpdateResult &) {
1414 FunctionAnalysisManager &FAM =
1415 AM.getResult<FunctionAnalysisManagerCGSCCProxy>(C, CG).getManager();
1417 // We pass a lambda into functions to wire them up to the analysis manager
1418 // for getting function analyses.
1419 auto AARGetter = [&](Function &F) -> AAResults & {
1420 return FAM.getResult<AAManager>(F);
1423 // Fill SCCNodes with the elements of the SCC. Also track whether there are
1424 // any external or opt-none nodes that will prevent us from optimizing any
1426 SCCNodeSet SCCNodes;
1427 bool HasUnknownCall = false;
1428 for (LazyCallGraph::Node &N : C) {
1429 Function &F = N.getFunction();
1430 if (F.hasOptNone() || F.hasFnAttribute(Attribute::Naked)) {
1431 // Treat any function we're trying not to optimize as if it were an
1432 // indirect call and omit it from the node set used below.
1433 HasUnknownCall = true;
1436 // Track whether any functions in this SCC have an unknown call edge.
1437 // Note: if this is ever a performance hit, we can common it with
1438 // subsequent routines which also do scans over the instructions of the
1440 if (!HasUnknownCall)
1441 for (Instruction &I : instructions(F))
1442 if (auto CS = CallSite(&I))
1443 if (!CS.getCalledFunction()) {
1444 HasUnknownCall = true;
1448 SCCNodes.insert(&F);
1451 if (deriveAttrsInPostOrder(SCCNodes, AARGetter, HasUnknownCall))
1452 return PreservedAnalyses::none();
1454 return PreservedAnalyses::all();
1459 struct PostOrderFunctionAttrsLegacyPass : public CallGraphSCCPass {
1460 // Pass identification, replacement for typeid
1463 PostOrderFunctionAttrsLegacyPass() : CallGraphSCCPass(ID) {
1464 initializePostOrderFunctionAttrsLegacyPassPass(
1465 *PassRegistry::getPassRegistry());
1468 bool runOnSCC(CallGraphSCC &SCC) override;
1470 void getAnalysisUsage(AnalysisUsage &AU) const override {
1471 AU.setPreservesCFG();
1472 AU.addRequired<AssumptionCacheTracker>();
1473 getAAResultsAnalysisUsage(AU);
1474 CallGraphSCCPass::getAnalysisUsage(AU);
1478 } // end anonymous namespace
1480 char PostOrderFunctionAttrsLegacyPass::ID = 0;
1481 INITIALIZE_PASS_BEGIN(PostOrderFunctionAttrsLegacyPass, "functionattrs",
1482 "Deduce function attributes", false, false)
1483 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
1484 INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
1485 INITIALIZE_PASS_END(PostOrderFunctionAttrsLegacyPass, "functionattrs",
1486 "Deduce function attributes", false, false)
1488 Pass *llvm::createPostOrderFunctionAttrsLegacyPass() {
1489 return new PostOrderFunctionAttrsLegacyPass();
1492 template <typename AARGetterT>
1493 static bool runImpl(CallGraphSCC &SCC, AARGetterT AARGetter) {
1495 // Fill SCCNodes with the elements of the SCC. Used for quickly looking up
1496 // whether a given CallGraphNode is in this SCC. Also track whether there are
1497 // any external or opt-none nodes that will prevent us from optimizing any
1499 SCCNodeSet SCCNodes;
1500 bool ExternalNode = false;
1501 for (CallGraphNode *I : SCC) {
1502 Function *F = I->getFunction();
1503 if (!F || F->hasOptNone() || F->hasFnAttribute(Attribute::Naked)) {
1504 // External node or function we're trying not to optimize - we both avoid
1505 // transform them and avoid leveraging information they provide.
1506 ExternalNode = true;
1513 return deriveAttrsInPostOrder(SCCNodes, AARGetter, ExternalNode);
1516 bool PostOrderFunctionAttrsLegacyPass::runOnSCC(CallGraphSCC &SCC) {
1519 return runImpl(SCC, LegacyAARGetter(*this));
1524 struct ReversePostOrderFunctionAttrsLegacyPass : public ModulePass {
1525 // Pass identification, replacement for typeid
1528 ReversePostOrderFunctionAttrsLegacyPass() : ModulePass(ID) {
1529 initializeReversePostOrderFunctionAttrsLegacyPassPass(
1530 *PassRegistry::getPassRegistry());
1533 bool runOnModule(Module &M) override;
1535 void getAnalysisUsage(AnalysisUsage &AU) const override {
1536 AU.setPreservesCFG();
1537 AU.addRequired<CallGraphWrapperPass>();
1538 AU.addPreserved<CallGraphWrapperPass>();
1542 } // end anonymous namespace
1544 char ReversePostOrderFunctionAttrsLegacyPass::ID = 0;
1546 INITIALIZE_PASS_BEGIN(ReversePostOrderFunctionAttrsLegacyPass, "rpo-functionattrs",
1547 "Deduce function attributes in RPO", false, false)
1548 INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
1549 INITIALIZE_PASS_END(ReversePostOrderFunctionAttrsLegacyPass, "rpo-functionattrs",
1550 "Deduce function attributes in RPO", false, false)
1552 Pass *llvm::createReversePostOrderFunctionAttrsPass() {
1553 return new ReversePostOrderFunctionAttrsLegacyPass();
1556 static bool addNoRecurseAttrsTopDown(Function &F) {
1557 // We check the preconditions for the function prior to calling this to avoid
1558 // the cost of building up a reversible post-order list. We assert them here
1559 // to make sure none of the invariants this relies on were violated.
1560 assert(!F.isDeclaration() && "Cannot deduce norecurse without a definition!");
1561 assert(!F.doesNotRecurse() &&
1562 "This function has already been deduced as norecurs!");
1563 assert(F.hasInternalLinkage() &&
1564 "Can only do top-down deduction for internal linkage functions!");
1566 // If F is internal and all of its uses are calls from a non-recursive
1567 // functions, then none of its calls could in fact recurse without going
1568 // through a function marked norecurse, and so we can mark this function too
1569 // as norecurse. Note that the uses must actually be calls -- otherwise
1570 // a pointer to this function could be returned from a norecurse function but
1571 // this function could be recursively (indirectly) called. Note that this
1572 // also detects if F is directly recursive as F is not yet marked as
1573 // a norecurse function.
1574 for (auto *U : F.users()) {
1575 auto *I = dyn_cast<Instruction>(U);
1579 if (!CS || !CS.getParent()->getParent()->doesNotRecurse())
1582 return setDoesNotRecurse(F);
1585 static bool deduceFunctionAttributeInRPO(Module &M, CallGraph &CG) {
1586 // We only have a post-order SCC traversal (because SCCs are inherently
1587 // discovered in post-order), so we accumulate them in a vector and then walk
1588 // it in reverse. This is simpler than using the RPO iterator infrastructure
1589 // because we need to combine SCC detection and the PO walk of the call
1590 // graph. We can also cheat egregiously because we're primarily interested in
1591 // synthesizing norecurse and so we can only save the singular SCCs as SCCs
1592 // with multiple functions in them will clearly be recursive.
1593 SmallVector<Function *, 16> Worklist;
1594 for (scc_iterator<CallGraph *> I = scc_begin(&CG); !I.isAtEnd(); ++I) {
1598 Function *F = I->front()->getFunction();
1599 if (F && !F->isDeclaration() && !F->doesNotRecurse() &&
1600 F->hasInternalLinkage())
1601 Worklist.push_back(F);
1604 bool Changed = false;
1605 for (auto *F : llvm::reverse(Worklist))
1606 Changed |= addNoRecurseAttrsTopDown(*F);
1611 bool ReversePostOrderFunctionAttrsLegacyPass::runOnModule(Module &M) {
1615 auto &CG = getAnalysis<CallGraphWrapperPass>().getCallGraph();
1617 return deduceFunctionAttributeInRPO(M, CG);
1621 ReversePostOrderFunctionAttrsPass::run(Module &M, ModuleAnalysisManager &AM) {
1622 auto &CG = AM.getResult<CallGraphAnalysis>(M);
1624 if (!deduceFunctionAttributeInRPO(M, CG))
1625 return PreservedAnalyses::all();
1627 PreservedAnalyses PA;
1628 PA.preserve<CallGraphAnalysis>();