1 //===- FunctionAttrs.cpp - Pass which marks functions attributes ----------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 ///
10 /// \file
11 /// This file implements interprocedural passes which walk the
12 /// call-graph deducing and/or propagating function attributes.
13 ///
14 //===----------------------------------------------------------------------===//
15 
16 #include "llvm/Transforms/IPO/FunctionAttrs.h"
17 #include "llvm/Transforms/IPO.h"
18 #include "llvm/ADT/SCCIterator.h"
19 #include "llvm/ADT/SetVector.h"
20 #include "llvm/ADT/SmallSet.h"
21 #include "llvm/ADT/Statistic.h"
22 #include "llvm/ADT/StringSwitch.h"
23 #include "llvm/Analysis/AliasAnalysis.h"
24 #include "llvm/Analysis/AssumptionCache.h"
25 #include "llvm/Analysis/BasicAliasAnalysis.h"
26 #include "llvm/Analysis/CallGraph.h"
27 #include "llvm/Analysis/CallGraphSCCPass.h"
28 #include "llvm/Analysis/CaptureTracking.h"
29 #include "llvm/Analysis/TargetLibraryInfo.h"
30 #include "llvm/Analysis/ValueTracking.h"
31 #include "llvm/IR/GlobalVariable.h"
32 #include "llvm/IR/InstIterator.h"
33 #include "llvm/IR/IntrinsicInst.h"
34 #include "llvm/IR/LLVMContext.h"
35 #include "llvm/Support/Debug.h"
36 #include "llvm/Support/raw_ostream.h"
37 #include "llvm/Analysis/TargetLibraryInfo.h"
38 using namespace llvm;
39 
40 #define DEBUG_TYPE "functionattrs"
41 
42 STATISTIC(NumReadNone, "Number of functions marked readnone");
43 STATISTIC(NumReadOnly, "Number of functions marked readonly");
44 STATISTIC(NumNoCapture, "Number of arguments marked nocapture");
45 STATISTIC(NumReturned, "Number of arguments marked returned");
46 STATISTIC(NumReadNoneArg, "Number of arguments marked readnone");
47 STATISTIC(NumReadOnlyArg, "Number of arguments marked readonly");
48 STATISTIC(NumNoAlias, "Number of function returns marked noalias");
49 STATISTIC(NumNonNullReturn, "Number of function returns marked nonnull");
50 STATISTIC(NumNoRecurse, "Number of functions marked as norecurse");
51 
52 namespace {
53 typedef SmallSetVector<Function *, 8> SCCNodeSet;
54 }
55 
56 namespace {
57 /// The three kinds of memory access relevant to 'readonly' and
58 /// 'readnone' attributes.
59 enum MemoryAccessKind {
60   MAK_ReadNone = 0,
61   MAK_ReadOnly = 1,
62   MAK_MayWrite = 2
63 };
64 }
65 
66 static MemoryAccessKind checkFunctionMemoryAccess(Function &F, AAResults &AAR,
67                                                   const SCCNodeSet &SCCNodes) {
68   FunctionModRefBehavior MRB = AAR.getModRefBehavior(&F);
69   if (MRB == FMRB_DoesNotAccessMemory)
70     // Already perfect!
71     return MAK_ReadNone;
72 
73   // Non-exact function definitions may not be selected at link time, and an
74   // alternative version that writes to memory may be selected.  See the comment
75   // on GlobalValue::isDefinitionExact for more details.
76   if (!F.hasExactDefinition()) {
77     if (AliasAnalysis::onlyReadsMemory(MRB))
78       return MAK_ReadOnly;
79 
80     // Conservatively assume it writes to memory.
81     return MAK_MayWrite;
82   }
83 
84   // Scan the function body for instructions that may read or write memory.
85   bool ReadsMemory = false;
86   for (inst_iterator II = inst_begin(F), E = inst_end(F); II != E; ++II) {
87     Instruction *I = &*II;
88 
89     // Some instructions can be ignored even if they read or write memory.
90     // Detect these now, skipping to the next instruction if one is found.
91     CallSite CS(cast<Value>(I));
92     if (CS) {
93       // Ignore calls to functions in the same SCC, as long as the call sites
94       // don't have operand bundles.  Calls with operand bundles are allowed to
95       // have memory effects not described by the memory effects of the call
96       // target.
97       if (!CS.hasOperandBundles() && CS.getCalledFunction() &&
98           SCCNodes.count(CS.getCalledFunction()))
99         continue;
100       FunctionModRefBehavior MRB = AAR.getModRefBehavior(CS);
101 
102       // If the call doesn't access memory, we're done.
103       if (!(MRB & MRI_ModRef))
104         continue;
105 
106       if (!AliasAnalysis::onlyAccessesArgPointees(MRB)) {
107         // The call could access any memory. If that includes writes, give up.
108         if (MRB & MRI_Mod)
109           return MAK_MayWrite;
110         // If it reads, note it.
111         if (MRB & MRI_Ref)
112           ReadsMemory = true;
113         continue;
114       }
115 
116       // Check whether all pointer arguments point to local memory, and
117       // ignore calls that only access local memory.
118       for (CallSite::arg_iterator CI = CS.arg_begin(), CE = CS.arg_end();
119            CI != CE; ++CI) {
120         Value *Arg = *CI;
121         if (!Arg->getType()->isPtrOrPtrVectorTy())
122           continue;
123 
124         AAMDNodes AAInfo;
125         I->getAAMetadata(AAInfo);
126         MemoryLocation Loc(Arg, MemoryLocation::UnknownSize, AAInfo);
127 
128         // Skip accesses to local or constant memory as they don't impact the
129         // externally visible mod/ref behavior.
130         if (AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true))
131           continue;
132 
133         if (MRB & MRI_Mod)
134           // Writes non-local memory.  Give up.
135           return MAK_MayWrite;
136         if (MRB & MRI_Ref)
137           // Ok, it reads non-local memory.
138           ReadsMemory = true;
139       }
140       continue;
141     } else if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
142       // Ignore non-volatile loads from local memory. (Atomic is okay here.)
143       if (!LI->isVolatile()) {
144         MemoryLocation Loc = MemoryLocation::get(LI);
145         if (AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true))
146           continue;
147       }
148     } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
149       // Ignore non-volatile stores to local memory. (Atomic is okay here.)
150       if (!SI->isVolatile()) {
151         MemoryLocation Loc = MemoryLocation::get(SI);
152         if (AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true))
153           continue;
154       }
155     } else if (VAArgInst *VI = dyn_cast<VAArgInst>(I)) {
156       // Ignore vaargs on local memory.
157       MemoryLocation Loc = MemoryLocation::get(VI);
158       if (AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true))
159         continue;
160     }
161 
162     // Any remaining instructions need to be taken seriously!  Check if they
163     // read or write memory.
164     if (I->mayWriteToMemory())
165       // Writes memory.  Just give up.
166       return MAK_MayWrite;
167 
168     // If this instruction may read memory, remember that.
169     ReadsMemory |= I->mayReadFromMemory();
170   }
171 
172   return ReadsMemory ? MAK_ReadOnly : MAK_ReadNone;
173 }
174 
175 /// Deduce readonly/readnone attributes for the SCC.
176 template <typename AARGetterT>
177 static bool addReadAttrs(const SCCNodeSet &SCCNodes, AARGetterT AARGetter) {
178   // Check if any of the functions in the SCC read or write memory.  If they
179   // write memory then they can't be marked readnone or readonly.
180   bool ReadsMemory = false;
181   for (Function *F : SCCNodes) {
182     // Call the callable parameter to look up AA results for this function.
183     AAResults &AAR = AARGetter(*F);
184 
185     switch (checkFunctionMemoryAccess(*F, AAR, SCCNodes)) {
186     case MAK_MayWrite:
187       return false;
188     case MAK_ReadOnly:
189       ReadsMemory = true;
190       break;
191     case MAK_ReadNone:
192       // Nothing to do!
193       break;
194     }
195   }
196 
197   // Success!  Functions in this SCC do not access memory, or only read memory.
198   // Give them the appropriate attribute.
199   bool MadeChange = false;
200   for (Function *F : SCCNodes) {
201     if (F->doesNotAccessMemory())
202       // Already perfect!
203       continue;
204 
205     if (F->onlyReadsMemory() && ReadsMemory)
206       // No change.
207       continue;
208 
209     MadeChange = true;
210 
211     // Clear out any existing attributes.
212     AttrBuilder B;
213     B.addAttribute(Attribute::ReadOnly).addAttribute(Attribute::ReadNone);
214     F->removeAttributes(
215         AttributeSet::FunctionIndex,
216         AttributeSet::get(F->getContext(), AttributeSet::FunctionIndex, B));
217 
218     // Add in the new attribute.
219     F->addAttribute(AttributeSet::FunctionIndex,
220                     ReadsMemory ? Attribute::ReadOnly : Attribute::ReadNone);
221 
222     if (ReadsMemory)
223       ++NumReadOnly;
224     else
225       ++NumReadNone;
226   }
227 
228   return MadeChange;
229 }
230 
231 namespace {
232 /// For a given pointer Argument, this retains a list of Arguments of functions
233 /// in the same SCC that the pointer data flows into. We use this to build an
234 /// SCC of the arguments.
235 struct ArgumentGraphNode {
236   Argument *Definition;
237   SmallVector<ArgumentGraphNode *, 4> Uses;
238 };
239 
240 class ArgumentGraph {
241   // We store pointers to ArgumentGraphNode objects, so it's important that
242   // that they not move around upon insert.
243   typedef std::map<Argument *, ArgumentGraphNode> ArgumentMapTy;
244 
245   ArgumentMapTy ArgumentMap;
246 
247   // There is no root node for the argument graph, in fact:
248   //   void f(int *x, int *y) { if (...) f(x, y); }
249   // is an example where the graph is disconnected. The SCCIterator requires a
250   // single entry point, so we maintain a fake ("synthetic") root node that
251   // uses every node. Because the graph is directed and nothing points into
252   // the root, it will not participate in any SCCs (except for its own).
253   ArgumentGraphNode SyntheticRoot;
254 
255 public:
256   ArgumentGraph() { SyntheticRoot.Definition = nullptr; }
257 
258   typedef SmallVectorImpl<ArgumentGraphNode *>::iterator iterator;
259 
260   iterator begin() { return SyntheticRoot.Uses.begin(); }
261   iterator end() { return SyntheticRoot.Uses.end(); }
262   ArgumentGraphNode *getEntryNode() { return &SyntheticRoot; }
263 
264   ArgumentGraphNode *operator[](Argument *A) {
265     ArgumentGraphNode &Node = ArgumentMap[A];
266     Node.Definition = A;
267     SyntheticRoot.Uses.push_back(&Node);
268     return &Node;
269   }
270 };
271 
272 /// This tracker checks whether callees are in the SCC, and if so it does not
273 /// consider that a capture, instead adding it to the "Uses" list and
274 /// continuing with the analysis.
275 struct ArgumentUsesTracker : public CaptureTracker {
276   ArgumentUsesTracker(const SCCNodeSet &SCCNodes)
277       : Captured(false), SCCNodes(SCCNodes) {}
278 
279   void tooManyUses() override { Captured = true; }
280 
281   bool captured(const Use *U) override {
282     CallSite CS(U->getUser());
283     if (!CS.getInstruction()) {
284       Captured = true;
285       return true;
286     }
287 
288     Function *F = CS.getCalledFunction();
289     if (!F || !F->hasExactDefinition() || !SCCNodes.count(F)) {
290       Captured = true;
291       return true;
292     }
293 
294     // Note: the callee and the two successor blocks *follow* the argument
295     // operands.  This means there is no need to adjust UseIndex to account for
296     // these.
297 
298     unsigned UseIndex =
299         std::distance(const_cast<const Use *>(CS.arg_begin()), U);
300 
301     assert(UseIndex < CS.data_operands_size() &&
302            "Indirect function calls should have been filtered above!");
303 
304     if (UseIndex >= CS.getNumArgOperands()) {
305       // Data operand, but not a argument operand -- must be a bundle operand
306       assert(CS.hasOperandBundles() && "Must be!");
307 
308       // CaptureTracking told us that we're being captured by an operand bundle
309       // use.  In this case it does not matter if the callee is within our SCC
310       // or not -- we've been captured in some unknown way, and we have to be
311       // conservative.
312       Captured = true;
313       return true;
314     }
315 
316     if (UseIndex >= F->arg_size()) {
317       assert(F->isVarArg() && "More params than args in non-varargs call");
318       Captured = true;
319       return true;
320     }
321 
322     Uses.push_back(&*std::next(F->arg_begin(), UseIndex));
323     return false;
324   }
325 
326   bool Captured; // True only if certainly captured (used outside our SCC).
327   SmallVector<Argument *, 4> Uses; // Uses within our SCC.
328 
329   const SCCNodeSet &SCCNodes;
330 };
331 }
332 
333 namespace llvm {
334 template <> struct GraphTraits<ArgumentGraphNode *> {
335   typedef ArgumentGraphNode NodeType;
336   typedef ArgumentGraphNode *NodeRef;
337   typedef SmallVectorImpl<ArgumentGraphNode *>::iterator ChildIteratorType;
338 
339   static inline NodeType *getEntryNode(NodeType *A) { return A; }
340   static inline ChildIteratorType child_begin(NodeType *N) {
341     return N->Uses.begin();
342   }
343   static inline ChildIteratorType child_end(NodeType *N) {
344     return N->Uses.end();
345   }
346 };
347 template <>
348 struct GraphTraits<ArgumentGraph *> : public GraphTraits<ArgumentGraphNode *> {
349   static NodeType *getEntryNode(ArgumentGraph *AG) {
350     return AG->getEntryNode();
351   }
352   static ChildIteratorType nodes_begin(ArgumentGraph *AG) {
353     return AG->begin();
354   }
355   static ChildIteratorType nodes_end(ArgumentGraph *AG) { return AG->end(); }
356 };
357 }
358 
359 /// Returns Attribute::None, Attribute::ReadOnly or Attribute::ReadNone.
360 static Attribute::AttrKind
361 determinePointerReadAttrs(Argument *A,
362                           const SmallPtrSet<Argument *, 8> &SCCNodes) {
363 
364   SmallVector<Use *, 32> Worklist;
365   SmallSet<Use *, 32> Visited;
366 
367   // inalloca arguments are always clobbered by the call.
368   if (A->hasInAllocaAttr())
369     return Attribute::None;
370 
371   bool IsRead = false;
372   // We don't need to track IsWritten. If A is written to, return immediately.
373 
374   for (Use &U : A->uses()) {
375     Visited.insert(&U);
376     Worklist.push_back(&U);
377   }
378 
379   while (!Worklist.empty()) {
380     Use *U = Worklist.pop_back_val();
381     Instruction *I = cast<Instruction>(U->getUser());
382 
383     switch (I->getOpcode()) {
384     case Instruction::BitCast:
385     case Instruction::GetElementPtr:
386     case Instruction::PHI:
387     case Instruction::Select:
388     case Instruction::AddrSpaceCast:
389       // The original value is not read/written via this if the new value isn't.
390       for (Use &UU : I->uses())
391         if (Visited.insert(&UU).second)
392           Worklist.push_back(&UU);
393       break;
394 
395     case Instruction::Call:
396     case Instruction::Invoke: {
397       bool Captures = true;
398 
399       if (I->getType()->isVoidTy())
400         Captures = false;
401 
402       auto AddUsersToWorklistIfCapturing = [&] {
403         if (Captures)
404           for (Use &UU : I->uses())
405             if (Visited.insert(&UU).second)
406               Worklist.push_back(&UU);
407       };
408 
409       CallSite CS(I);
410       if (CS.doesNotAccessMemory()) {
411         AddUsersToWorklistIfCapturing();
412         continue;
413       }
414 
415       Function *F = CS.getCalledFunction();
416       if (!F) {
417         if (CS.onlyReadsMemory()) {
418           IsRead = true;
419           AddUsersToWorklistIfCapturing();
420           continue;
421         }
422         return Attribute::None;
423       }
424 
425       // Note: the callee and the two successor blocks *follow* the argument
426       // operands.  This means there is no need to adjust UseIndex to account
427       // for these.
428 
429       unsigned UseIndex = std::distance(CS.arg_begin(), U);
430 
431       // U cannot be the callee operand use: since we're exploring the
432       // transitive uses of an Argument, having such a use be a callee would
433       // imply the CallSite is an indirect call or invoke; and we'd take the
434       // early exit above.
435       assert(UseIndex < CS.data_operands_size() &&
436              "Data operand use expected!");
437 
438       bool IsOperandBundleUse = UseIndex >= CS.getNumArgOperands();
439 
440       if (UseIndex >= F->arg_size() && !IsOperandBundleUse) {
441         assert(F->isVarArg() && "More params than args in non-varargs call");
442         return Attribute::None;
443       }
444 
445       Captures &= !CS.doesNotCapture(UseIndex);
446 
447       // Since the optimizer (by design) cannot see the data flow corresponding
448       // to a operand bundle use, these cannot participate in the optimistic SCC
449       // analysis.  Instead, we model the operand bundle uses as arguments in
450       // call to a function external to the SCC.
451       if (IsOperandBundleUse ||
452           !SCCNodes.count(&*std::next(F->arg_begin(), UseIndex))) {
453 
454         // The accessors used on CallSite here do the right thing for calls and
455         // invokes with operand bundles.
456 
457         if (!CS.onlyReadsMemory() && !CS.onlyReadsMemory(UseIndex))
458           return Attribute::None;
459         if (!CS.doesNotAccessMemory(UseIndex))
460           IsRead = true;
461       }
462 
463       AddUsersToWorklistIfCapturing();
464       break;
465     }
466 
467     case Instruction::Load:
468       // A volatile load has side effects beyond what readonly can be relied
469       // upon.
470       if (cast<LoadInst>(I)->isVolatile())
471         return Attribute::None;
472 
473       IsRead = true;
474       break;
475 
476     case Instruction::ICmp:
477     case Instruction::Ret:
478       break;
479 
480     default:
481       return Attribute::None;
482     }
483   }
484 
485   return IsRead ? Attribute::ReadOnly : Attribute::ReadNone;
486 }
487 
488 /// Deduce returned attributes for the SCC.
489 static bool addArgumentReturnedAttrs(const SCCNodeSet &SCCNodes) {
490   bool Changed = false;
491 
492   AttrBuilder B;
493   B.addAttribute(Attribute::Returned);
494 
495   // Check each function in turn, determining if an argument is always returned.
496   for (Function *F : SCCNodes) {
497     // We can infer and propagate function attributes only when we know that the
498     // definition we'll get at link time is *exactly* the definition we see now.
499     // For more details, see GlobalValue::mayBeDerefined.
500     if (!F->hasExactDefinition())
501       continue;
502 
503     if (F->getReturnType()->isVoidTy())
504       continue;
505 
506     auto FindRetArg = [&]() -> Value * {
507       Value *RetArg = nullptr;
508       for (BasicBlock &BB : *F)
509         if (auto *Ret = dyn_cast<ReturnInst>(BB.getTerminator())) {
510           // Note that stripPointerCasts should look through functions with
511           // returned arguments.
512           Value *RetVal = Ret->getReturnValue()->stripPointerCasts();
513           if (!isa<Argument>(RetVal) || RetVal->getType() != F->getReturnType())
514             return nullptr;
515 
516           if (!RetArg)
517             RetArg = RetVal;
518           else if (RetArg != RetVal)
519             return nullptr;
520         }
521 
522       return RetArg;
523     };
524 
525     if (Value *RetArg = FindRetArg()) {
526       auto *A = cast<Argument>(RetArg);
527       A->addAttr(AttributeSet::get(F->getContext(), A->getArgNo() + 1, B));
528       ++NumReturned;
529       Changed = true;
530     }
531   }
532 
533   return Changed;
534 }
535 
536 /// Deduce nocapture attributes for the SCC.
537 static bool addArgumentAttrs(const SCCNodeSet &SCCNodes) {
538   bool Changed = false;
539 
540   ArgumentGraph AG;
541 
542   AttrBuilder B;
543   B.addAttribute(Attribute::NoCapture);
544 
545   // Check each function in turn, determining which pointer arguments are not
546   // captured.
547   for (Function *F : SCCNodes) {
548     // We can infer and propagate function attributes only when we know that the
549     // definition we'll get at link time is *exactly* the definition we see now.
550     // For more details, see GlobalValue::mayBeDerefined.
551     if (!F->hasExactDefinition())
552       continue;
553 
554     // Functions that are readonly (or readnone) and nounwind and don't return
555     // a value can't capture arguments. Don't analyze them.
556     if (F->onlyReadsMemory() && F->doesNotThrow() &&
557         F->getReturnType()->isVoidTy()) {
558       for (Function::arg_iterator A = F->arg_begin(), E = F->arg_end(); A != E;
559            ++A) {
560         if (A->getType()->isPointerTy() && !A->hasNoCaptureAttr()) {
561           A->addAttr(AttributeSet::get(F->getContext(), A->getArgNo() + 1, B));
562           ++NumNoCapture;
563           Changed = true;
564         }
565       }
566       continue;
567     }
568 
569     for (Function::arg_iterator A = F->arg_begin(), E = F->arg_end(); A != E;
570          ++A) {
571       if (!A->getType()->isPointerTy())
572         continue;
573       bool HasNonLocalUses = false;
574       if (!A->hasNoCaptureAttr()) {
575         ArgumentUsesTracker Tracker(SCCNodes);
576         PointerMayBeCaptured(&*A, &Tracker);
577         if (!Tracker.Captured) {
578           if (Tracker.Uses.empty()) {
579             // If it's trivially not captured, mark it nocapture now.
580             A->addAttr(
581                 AttributeSet::get(F->getContext(), A->getArgNo() + 1, B));
582             ++NumNoCapture;
583             Changed = true;
584           } else {
585             // If it's not trivially captured and not trivially not captured,
586             // then it must be calling into another function in our SCC. Save
587             // its particulars for Argument-SCC analysis later.
588             ArgumentGraphNode *Node = AG[&*A];
589             for (Argument *Use : Tracker.Uses) {
590               Node->Uses.push_back(AG[Use]);
591               if (Use != &*A)
592                 HasNonLocalUses = true;
593             }
594           }
595         }
596         // Otherwise, it's captured. Don't bother doing SCC analysis on it.
597       }
598       if (!HasNonLocalUses && !A->onlyReadsMemory()) {
599         // Can we determine that it's readonly/readnone without doing an SCC?
600         // Note that we don't allow any calls at all here, or else our result
601         // will be dependent on the iteration order through the functions in the
602         // SCC.
603         SmallPtrSet<Argument *, 8> Self;
604         Self.insert(&*A);
605         Attribute::AttrKind R = determinePointerReadAttrs(&*A, Self);
606         if (R != Attribute::None) {
607           AttrBuilder B;
608           B.addAttribute(R);
609           A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
610           Changed = true;
611           R == Attribute::ReadOnly ? ++NumReadOnlyArg : ++NumReadNoneArg;
612         }
613       }
614     }
615   }
616 
617   // The graph we've collected is partial because we stopped scanning for
618   // argument uses once we solved the argument trivially. These partial nodes
619   // show up as ArgumentGraphNode objects with an empty Uses list, and for
620   // these nodes the final decision about whether they capture has already been
621   // made.  If the definition doesn't have a 'nocapture' attribute by now, it
622   // captures.
623 
624   for (scc_iterator<ArgumentGraph *> I = scc_begin(&AG); !I.isAtEnd(); ++I) {
625     const std::vector<ArgumentGraphNode *> &ArgumentSCC = *I;
626     if (ArgumentSCC.size() == 1) {
627       if (!ArgumentSCC[0]->Definition)
628         continue; // synthetic root node
629 
630       // eg. "void f(int* x) { if (...) f(x); }"
631       if (ArgumentSCC[0]->Uses.size() == 1 &&
632           ArgumentSCC[0]->Uses[0] == ArgumentSCC[0]) {
633         Argument *A = ArgumentSCC[0]->Definition;
634         A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
635         ++NumNoCapture;
636         Changed = true;
637       }
638       continue;
639     }
640 
641     bool SCCCaptured = false;
642     for (auto I = ArgumentSCC.begin(), E = ArgumentSCC.end();
643          I != E && !SCCCaptured; ++I) {
644       ArgumentGraphNode *Node = *I;
645       if (Node->Uses.empty()) {
646         if (!Node->Definition->hasNoCaptureAttr())
647           SCCCaptured = true;
648       }
649     }
650     if (SCCCaptured)
651       continue;
652 
653     SmallPtrSet<Argument *, 8> ArgumentSCCNodes;
654     // Fill ArgumentSCCNodes with the elements of the ArgumentSCC.  Used for
655     // quickly looking up whether a given Argument is in this ArgumentSCC.
656     for (ArgumentGraphNode *I : ArgumentSCC) {
657       ArgumentSCCNodes.insert(I->Definition);
658     }
659 
660     for (auto I = ArgumentSCC.begin(), E = ArgumentSCC.end();
661          I != E && !SCCCaptured; ++I) {
662       ArgumentGraphNode *N = *I;
663       for (ArgumentGraphNode *Use : N->Uses) {
664         Argument *A = Use->Definition;
665         if (A->hasNoCaptureAttr() || ArgumentSCCNodes.count(A))
666           continue;
667         SCCCaptured = true;
668         break;
669       }
670     }
671     if (SCCCaptured)
672       continue;
673 
674     for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) {
675       Argument *A = ArgumentSCC[i]->Definition;
676       A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
677       ++NumNoCapture;
678       Changed = true;
679     }
680 
681     // We also want to compute readonly/readnone. With a small number of false
682     // negatives, we can assume that any pointer which is captured isn't going
683     // to be provably readonly or readnone, since by definition we can't
684     // analyze all uses of a captured pointer.
685     //
686     // The false negatives happen when the pointer is captured by a function
687     // that promises readonly/readnone behaviour on the pointer, then the
688     // pointer's lifetime ends before anything that writes to arbitrary memory.
689     // Also, a readonly/readnone pointer may be returned, but returning a
690     // pointer is capturing it.
691 
692     Attribute::AttrKind ReadAttr = Attribute::ReadNone;
693     for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) {
694       Argument *A = ArgumentSCC[i]->Definition;
695       Attribute::AttrKind K = determinePointerReadAttrs(A, ArgumentSCCNodes);
696       if (K == Attribute::ReadNone)
697         continue;
698       if (K == Attribute::ReadOnly) {
699         ReadAttr = Attribute::ReadOnly;
700         continue;
701       }
702       ReadAttr = K;
703       break;
704     }
705 
706     if (ReadAttr != Attribute::None) {
707       AttrBuilder B, R;
708       B.addAttribute(ReadAttr);
709       R.addAttribute(Attribute::ReadOnly).addAttribute(Attribute::ReadNone);
710       for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) {
711         Argument *A = ArgumentSCC[i]->Definition;
712         // Clear out existing readonly/readnone attributes
713         A->removeAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, R));
714         A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
715         ReadAttr == Attribute::ReadOnly ? ++NumReadOnlyArg : ++NumReadNoneArg;
716         Changed = true;
717       }
718     }
719   }
720 
721   return Changed;
722 }
723 
724 /// Tests whether a function is "malloc-like".
725 ///
726 /// A function is "malloc-like" if it returns either null or a pointer that
727 /// doesn't alias any other pointer visible to the caller.
728 static bool isFunctionMallocLike(Function *F, const SCCNodeSet &SCCNodes) {
729   SmallSetVector<Value *, 8> FlowsToReturn;
730   for (BasicBlock &BB : *F)
731     if (ReturnInst *Ret = dyn_cast<ReturnInst>(BB.getTerminator()))
732       FlowsToReturn.insert(Ret->getReturnValue());
733 
734   for (unsigned i = 0; i != FlowsToReturn.size(); ++i) {
735     Value *RetVal = FlowsToReturn[i];
736 
737     if (Constant *C = dyn_cast<Constant>(RetVal)) {
738       if (!C->isNullValue() && !isa<UndefValue>(C))
739         return false;
740 
741       continue;
742     }
743 
744     if (isa<Argument>(RetVal))
745       return false;
746 
747     if (Instruction *RVI = dyn_cast<Instruction>(RetVal))
748       switch (RVI->getOpcode()) {
749       // Extend the analysis by looking upwards.
750       case Instruction::BitCast:
751       case Instruction::GetElementPtr:
752       case Instruction::AddrSpaceCast:
753         FlowsToReturn.insert(RVI->getOperand(0));
754         continue;
755       case Instruction::Select: {
756         SelectInst *SI = cast<SelectInst>(RVI);
757         FlowsToReturn.insert(SI->getTrueValue());
758         FlowsToReturn.insert(SI->getFalseValue());
759         continue;
760       }
761       case Instruction::PHI: {
762         PHINode *PN = cast<PHINode>(RVI);
763         for (Value *IncValue : PN->incoming_values())
764           FlowsToReturn.insert(IncValue);
765         continue;
766       }
767 
768       // Check whether the pointer came from an allocation.
769       case Instruction::Alloca:
770         break;
771       case Instruction::Call:
772       case Instruction::Invoke: {
773         CallSite CS(RVI);
774         if (CS.paramHasAttr(0, Attribute::NoAlias))
775           break;
776         if (CS.getCalledFunction() && SCCNodes.count(CS.getCalledFunction()))
777           break;
778         LLVM_FALLTHROUGH;
779       }
780       default:
781         return false; // Did not come from an allocation.
782       }
783 
784     if (PointerMayBeCaptured(RetVal, false, /*StoreCaptures=*/false))
785       return false;
786   }
787 
788   return true;
789 }
790 
791 /// Deduce noalias attributes for the SCC.
792 static bool addNoAliasAttrs(const SCCNodeSet &SCCNodes) {
793   // Check each function in turn, determining which functions return noalias
794   // pointers.
795   for (Function *F : SCCNodes) {
796     // Already noalias.
797     if (F->doesNotAlias(0))
798       continue;
799 
800     // We can infer and propagate function attributes only when we know that the
801     // definition we'll get at link time is *exactly* the definition we see now.
802     // For more details, see GlobalValue::mayBeDerefined.
803     if (!F->hasExactDefinition())
804       return false;
805 
806     // We annotate noalias return values, which are only applicable to
807     // pointer types.
808     if (!F->getReturnType()->isPointerTy())
809       continue;
810 
811     if (!isFunctionMallocLike(F, SCCNodes))
812       return false;
813   }
814 
815   bool MadeChange = false;
816   for (Function *F : SCCNodes) {
817     if (F->doesNotAlias(0) || !F->getReturnType()->isPointerTy())
818       continue;
819 
820     F->setDoesNotAlias(0);
821     ++NumNoAlias;
822     MadeChange = true;
823   }
824 
825   return MadeChange;
826 }
827 
828 /// Tests whether this function is known to not return null.
829 ///
830 /// Requires that the function returns a pointer.
831 ///
832 /// Returns true if it believes the function will not return a null, and sets
833 /// \p Speculative based on whether the returned conclusion is a speculative
834 /// conclusion due to SCC calls.
835 static bool isReturnNonNull(Function *F, const SCCNodeSet &SCCNodes,
836                             bool &Speculative) {
837   assert(F->getReturnType()->isPointerTy() &&
838          "nonnull only meaningful on pointer types");
839   Speculative = false;
840 
841   SmallSetVector<Value *, 8> FlowsToReturn;
842   for (BasicBlock &BB : *F)
843     if (auto *Ret = dyn_cast<ReturnInst>(BB.getTerminator()))
844       FlowsToReturn.insert(Ret->getReturnValue());
845 
846   for (unsigned i = 0; i != FlowsToReturn.size(); ++i) {
847     Value *RetVal = FlowsToReturn[i];
848 
849     // If this value is locally known to be non-null, we're good
850     if (isKnownNonNull(RetVal))
851       continue;
852 
853     // Otherwise, we need to look upwards since we can't make any local
854     // conclusions.
855     Instruction *RVI = dyn_cast<Instruction>(RetVal);
856     if (!RVI)
857       return false;
858     switch (RVI->getOpcode()) {
859     // Extend the analysis by looking upwards.
860     case Instruction::BitCast:
861     case Instruction::GetElementPtr:
862     case Instruction::AddrSpaceCast:
863       FlowsToReturn.insert(RVI->getOperand(0));
864       continue;
865     case Instruction::Select: {
866       SelectInst *SI = cast<SelectInst>(RVI);
867       FlowsToReturn.insert(SI->getTrueValue());
868       FlowsToReturn.insert(SI->getFalseValue());
869       continue;
870     }
871     case Instruction::PHI: {
872       PHINode *PN = cast<PHINode>(RVI);
873       for (int i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
874         FlowsToReturn.insert(PN->getIncomingValue(i));
875       continue;
876     }
877     case Instruction::Call:
878     case Instruction::Invoke: {
879       CallSite CS(RVI);
880       Function *Callee = CS.getCalledFunction();
881       // A call to a node within the SCC is assumed to return null until
882       // proven otherwise
883       if (Callee && SCCNodes.count(Callee)) {
884         Speculative = true;
885         continue;
886       }
887       return false;
888     }
889     default:
890       return false; // Unknown source, may be null
891     };
892     llvm_unreachable("should have either continued or returned");
893   }
894 
895   return true;
896 }
897 
898 /// Deduce nonnull attributes for the SCC.
899 static bool addNonNullAttrs(const SCCNodeSet &SCCNodes) {
900   // Speculative that all functions in the SCC return only nonnull
901   // pointers.  We may refute this as we analyze functions.
902   bool SCCReturnsNonNull = true;
903 
904   bool MadeChange = false;
905 
906   // Check each function in turn, determining which functions return nonnull
907   // pointers.
908   for (Function *F : SCCNodes) {
909     // Already nonnull.
910     if (F->getAttributes().hasAttribute(AttributeSet::ReturnIndex,
911                                         Attribute::NonNull))
912       continue;
913 
914     // We can infer and propagate function attributes only when we know that the
915     // definition we'll get at link time is *exactly* the definition we see now.
916     // For more details, see GlobalValue::mayBeDerefined.
917     if (!F->hasExactDefinition())
918       return false;
919 
920     // We annotate nonnull return values, which are only applicable to
921     // pointer types.
922     if (!F->getReturnType()->isPointerTy())
923       continue;
924 
925     bool Speculative = false;
926     if (isReturnNonNull(F, SCCNodes, Speculative)) {
927       if (!Speculative) {
928         // Mark the function eagerly since we may discover a function
929         // which prevents us from speculating about the entire SCC
930         DEBUG(dbgs() << "Eagerly marking " << F->getName() << " as nonnull\n");
931         F->addAttribute(AttributeSet::ReturnIndex, Attribute::NonNull);
932         ++NumNonNullReturn;
933         MadeChange = true;
934       }
935       continue;
936     }
937     // At least one function returns something which could be null, can't
938     // speculate any more.
939     SCCReturnsNonNull = false;
940   }
941 
942   if (SCCReturnsNonNull) {
943     for (Function *F : SCCNodes) {
944       if (F->getAttributes().hasAttribute(AttributeSet::ReturnIndex,
945                                           Attribute::NonNull) ||
946           !F->getReturnType()->isPointerTy())
947         continue;
948 
949       DEBUG(dbgs() << "SCC marking " << F->getName() << " as nonnull\n");
950       F->addAttribute(AttributeSet::ReturnIndex, Attribute::NonNull);
951       ++NumNonNullReturn;
952       MadeChange = true;
953     }
954   }
955 
956   return MadeChange;
957 }
958 
959 /// Remove the convergent attribute from all functions in the SCC if every
960 /// callsite within the SCC is not convergent (except for calls to functions
961 /// within the SCC).  Returns true if changes were made.
962 static bool removeConvergentAttrs(const SCCNodeSet &SCCNodes) {
963   // For every function in SCC, ensure that either
964   //  * it is not convergent, or
965   //  * we can remove its convergent attribute.
966   bool HasConvergentFn = false;
967   for (Function *F : SCCNodes) {
968     if (!F->isConvergent()) continue;
969     HasConvergentFn = true;
970 
971     // Can't remove convergent from function declarations.
972     if (F->isDeclaration()) return false;
973 
974     // Can't remove convergent if any of our functions has a convergent call to a
975     // function not in the SCC.
976     for (Instruction &I : instructions(*F)) {
977       CallSite CS(&I);
978       // Bail if CS is a convergent call to a function not in the SCC.
979       if (CS && CS.isConvergent() &&
980           SCCNodes.count(CS.getCalledFunction()) == 0)
981         return false;
982     }
983   }
984 
985   // If the SCC doesn't have any convergent functions, we have nothing to do.
986   if (!HasConvergentFn) return false;
987 
988   // If we got here, all of the calls the SCC makes to functions not in the SCC
989   // are non-convergent.  Therefore all of the SCC's functions can also be made
990   // non-convergent.  We'll remove the attr from the callsites in
991   // InstCombineCalls.
992   for (Function *F : SCCNodes) {
993     if (!F->isConvergent()) continue;
994 
995     DEBUG(dbgs() << "Removing convergent attr from fn " << F->getName()
996                  << "\n");
997     F->setNotConvergent();
998   }
999   return true;
1000 }
1001 
1002 static bool setDoesNotRecurse(Function &F) {
1003   if (F.doesNotRecurse())
1004     return false;
1005   F.setDoesNotRecurse();
1006   ++NumNoRecurse;
1007   return true;
1008 }
1009 
1010 static bool addNoRecurseAttrs(const SCCNodeSet &SCCNodes) {
1011   // Try and identify functions that do not recurse.
1012 
1013   // If the SCC contains multiple nodes we know for sure there is recursion.
1014   if (SCCNodes.size() != 1)
1015     return false;
1016 
1017   Function *F = *SCCNodes.begin();
1018   if (!F || F->isDeclaration() || F->doesNotRecurse())
1019     return false;
1020 
1021   // If all of the calls in F are identifiable and are to norecurse functions, F
1022   // is norecurse. This check also detects self-recursion as F is not currently
1023   // marked norecurse, so any called from F to F will not be marked norecurse.
1024   for (Instruction &I : instructions(*F))
1025     if (auto CS = CallSite(&I)) {
1026       Function *Callee = CS.getCalledFunction();
1027       if (!Callee || Callee == F || !Callee->doesNotRecurse())
1028         // Function calls a potentially recursive function.
1029         return false;
1030     }
1031 
1032   // Every call was to a non-recursive function other than this function, and
1033   // we have no indirect recursion as the SCC size is one. This function cannot
1034   // recurse.
1035   return setDoesNotRecurse(*F);
1036 }
1037 
1038 PreservedAnalyses PostOrderFunctionAttrsPass::run(LazyCallGraph::SCC &C,
1039                                                   CGSCCAnalysisManager &AM) {
1040   FunctionAnalysisManager &FAM =
1041       AM.getResult<FunctionAnalysisManagerCGSCCProxy>(C).getManager();
1042 
1043   // We pass a lambda into functions to wire them up to the analysis manager
1044   // for getting function analyses.
1045   auto AARGetter = [&](Function &F) -> AAResults & {
1046     return FAM.getResult<AAManager>(F);
1047   };
1048 
1049   // Fill SCCNodes with the elements of the SCC. Also track whether there are
1050   // any external or opt-none nodes that will prevent us from optimizing any
1051   // part of the SCC.
1052   SCCNodeSet SCCNodes;
1053   bool HasUnknownCall = false;
1054   for (LazyCallGraph::Node &N : C) {
1055     Function &F = N.getFunction();
1056     if (F.hasFnAttribute(Attribute::OptimizeNone)) {
1057       // Treat any function we're trying not to optimize as if it were an
1058       // indirect call and omit it from the node set used below.
1059       HasUnknownCall = true;
1060       continue;
1061     }
1062     // Track whether any functions in this SCC have an unknown call edge.
1063     // Note: if this is ever a performance hit, we can common it with
1064     // subsequent routines which also do scans over the instructions of the
1065     // function.
1066     if (!HasUnknownCall)
1067       for (Instruction &I : instructions(F))
1068         if (auto CS = CallSite(&I))
1069           if (!CS.getCalledFunction()) {
1070             HasUnknownCall = true;
1071             break;
1072           }
1073 
1074     SCCNodes.insert(&F);
1075   }
1076 
1077   bool Changed = false;
1078   Changed |= addArgumentReturnedAttrs(SCCNodes);
1079   Changed |= addReadAttrs(SCCNodes, AARGetter);
1080   Changed |= addArgumentAttrs(SCCNodes);
1081 
1082   // If we have no external nodes participating in the SCC, we can deduce some
1083   // more precise attributes as well.
1084   if (!HasUnknownCall) {
1085     Changed |= addNoAliasAttrs(SCCNodes);
1086     Changed |= addNonNullAttrs(SCCNodes);
1087     Changed |= removeConvergentAttrs(SCCNodes);
1088     Changed |= addNoRecurseAttrs(SCCNodes);
1089   }
1090 
1091   return Changed ? PreservedAnalyses::none() : PreservedAnalyses::all();
1092 }
1093 
1094 namespace {
1095 struct PostOrderFunctionAttrsLegacyPass : public CallGraphSCCPass {
1096   static char ID; // Pass identification, replacement for typeid
1097   PostOrderFunctionAttrsLegacyPass() : CallGraphSCCPass(ID) {
1098     initializePostOrderFunctionAttrsLegacyPassPass(*PassRegistry::getPassRegistry());
1099   }
1100 
1101   bool runOnSCC(CallGraphSCC &SCC) override;
1102 
1103   void getAnalysisUsage(AnalysisUsage &AU) const override {
1104     AU.setPreservesCFG();
1105     AU.addRequired<AssumptionCacheTracker>();
1106     getAAResultsAnalysisUsage(AU);
1107     CallGraphSCCPass::getAnalysisUsage(AU);
1108   }
1109 };
1110 }
1111 
1112 char PostOrderFunctionAttrsLegacyPass::ID = 0;
1113 INITIALIZE_PASS_BEGIN(PostOrderFunctionAttrsLegacyPass, "functionattrs",
1114                       "Deduce function attributes", false, false)
1115 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
1116 INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
1117 INITIALIZE_PASS_END(PostOrderFunctionAttrsLegacyPass, "functionattrs",
1118                     "Deduce function attributes", false, false)
1119 
1120 Pass *llvm::createPostOrderFunctionAttrsLegacyPass() { return new PostOrderFunctionAttrsLegacyPass(); }
1121 
1122 template <typename AARGetterT>
1123 static bool runImpl(CallGraphSCC &SCC, AARGetterT AARGetter) {
1124   bool Changed = false;
1125 
1126   // Fill SCCNodes with the elements of the SCC. Used for quickly looking up
1127   // whether a given CallGraphNode is in this SCC. Also track whether there are
1128   // any external or opt-none nodes that will prevent us from optimizing any
1129   // part of the SCC.
1130   SCCNodeSet SCCNodes;
1131   bool ExternalNode = false;
1132   for (CallGraphNode *I : SCC) {
1133     Function *F = I->getFunction();
1134     if (!F || F->hasFnAttribute(Attribute::OptimizeNone)) {
1135       // External node or function we're trying not to optimize - we both avoid
1136       // transform them and avoid leveraging information they provide.
1137       ExternalNode = true;
1138       continue;
1139     }
1140 
1141     SCCNodes.insert(F);
1142   }
1143 
1144   Changed |= addArgumentReturnedAttrs(SCCNodes);
1145   Changed |= addReadAttrs(SCCNodes, AARGetter);
1146   Changed |= addArgumentAttrs(SCCNodes);
1147 
1148   // If we have no external nodes participating in the SCC, we can deduce some
1149   // more precise attributes as well.
1150   if (!ExternalNode) {
1151     Changed |= addNoAliasAttrs(SCCNodes);
1152     Changed |= addNonNullAttrs(SCCNodes);
1153     Changed |= removeConvergentAttrs(SCCNodes);
1154     Changed |= addNoRecurseAttrs(SCCNodes);
1155   }
1156 
1157   return Changed;
1158 }
1159 
1160 bool PostOrderFunctionAttrsLegacyPass::runOnSCC(CallGraphSCC &SCC) {
1161   if (skipSCC(SCC))
1162     return false;
1163 
1164   // We compute dedicated AA results for each function in the SCC as needed. We
1165   // use a lambda referencing external objects so that they live long enough to
1166   // be queried, but we re-use them each time.
1167   Optional<BasicAAResult> BAR;
1168   Optional<AAResults> AAR;
1169   auto AARGetter = [&](Function &F) -> AAResults & {
1170     BAR.emplace(createLegacyPMBasicAAResult(*this, F));
1171     AAR.emplace(createLegacyPMAAResults(*this, F, *BAR));
1172     return *AAR;
1173   };
1174 
1175   return runImpl(SCC, AARGetter);
1176 }
1177 
1178 namespace {
1179 struct ReversePostOrderFunctionAttrsLegacyPass : public ModulePass {
1180   static char ID; // Pass identification, replacement for typeid
1181   ReversePostOrderFunctionAttrsLegacyPass() : ModulePass(ID) {
1182     initializeReversePostOrderFunctionAttrsLegacyPassPass(*PassRegistry::getPassRegistry());
1183   }
1184 
1185   bool runOnModule(Module &M) override;
1186 
1187   void getAnalysisUsage(AnalysisUsage &AU) const override {
1188     AU.setPreservesCFG();
1189     AU.addRequired<CallGraphWrapperPass>();
1190     AU.addPreserved<CallGraphWrapperPass>();
1191   }
1192 };
1193 }
1194 
1195 char ReversePostOrderFunctionAttrsLegacyPass::ID = 0;
1196 INITIALIZE_PASS_BEGIN(ReversePostOrderFunctionAttrsLegacyPass, "rpo-functionattrs",
1197                       "Deduce function attributes in RPO", false, false)
1198 INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
1199 INITIALIZE_PASS_END(ReversePostOrderFunctionAttrsLegacyPass, "rpo-functionattrs",
1200                     "Deduce function attributes in RPO", false, false)
1201 
1202 Pass *llvm::createReversePostOrderFunctionAttrsPass() {
1203   return new ReversePostOrderFunctionAttrsLegacyPass();
1204 }
1205 
1206 static bool addNoRecurseAttrsTopDown(Function &F) {
1207   // We check the preconditions for the function prior to calling this to avoid
1208   // the cost of building up a reversible post-order list. We assert them here
1209   // to make sure none of the invariants this relies on were violated.
1210   assert(!F.isDeclaration() && "Cannot deduce norecurse without a definition!");
1211   assert(!F.doesNotRecurse() &&
1212          "This function has already been deduced as norecurs!");
1213   assert(F.hasInternalLinkage() &&
1214          "Can only do top-down deduction for internal linkage functions!");
1215 
1216   // If F is internal and all of its uses are calls from a non-recursive
1217   // functions, then none of its calls could in fact recurse without going
1218   // through a function marked norecurse, and so we can mark this function too
1219   // as norecurse. Note that the uses must actually be calls -- otherwise
1220   // a pointer to this function could be returned from a norecurse function but
1221   // this function could be recursively (indirectly) called. Note that this
1222   // also detects if F is directly recursive as F is not yet marked as
1223   // a norecurse function.
1224   for (auto *U : F.users()) {
1225     auto *I = dyn_cast<Instruction>(U);
1226     if (!I)
1227       return false;
1228     CallSite CS(I);
1229     if (!CS || !CS.getParent()->getParent()->doesNotRecurse())
1230       return false;
1231   }
1232   return setDoesNotRecurse(F);
1233 }
1234 
1235 static bool deduceFunctionAttributeInRPO(Module &M, CallGraph &CG) {
1236   // We only have a post-order SCC traversal (because SCCs are inherently
1237   // discovered in post-order), so we accumulate them in a vector and then walk
1238   // it in reverse. This is simpler than using the RPO iterator infrastructure
1239   // because we need to combine SCC detection and the PO walk of the call
1240   // graph. We can also cheat egregiously because we're primarily interested in
1241   // synthesizing norecurse and so we can only save the singular SCCs as SCCs
1242   // with multiple functions in them will clearly be recursive.
1243   SmallVector<Function *, 16> Worklist;
1244   for (scc_iterator<CallGraph *> I = scc_begin(&CG); !I.isAtEnd(); ++I) {
1245     if (I->size() != 1)
1246       continue;
1247 
1248     Function *F = I->front()->getFunction();
1249     if (F && !F->isDeclaration() && !F->doesNotRecurse() &&
1250         F->hasInternalLinkage())
1251       Worklist.push_back(F);
1252   }
1253 
1254   bool Changed = false;
1255   for (auto *F : reverse(Worklist))
1256     Changed |= addNoRecurseAttrsTopDown(*F);
1257 
1258   return Changed;
1259 }
1260 
1261 bool ReversePostOrderFunctionAttrsLegacyPass::runOnModule(Module &M) {
1262   if (skipModule(M))
1263     return false;
1264 
1265   auto &CG = getAnalysis<CallGraphWrapperPass>().getCallGraph();
1266 
1267   return deduceFunctionAttributeInRPO(M, CG);
1268 }
1269 
1270 PreservedAnalyses
1271 ReversePostOrderFunctionAttrsPass::run(Module &M, ModuleAnalysisManager &AM) {
1272   auto &CG = AM.getResult<CallGraphAnalysis>(M);
1273 
1274   bool Changed = deduceFunctionAttributeInRPO(M, CG);
1275 
1276   // CallGraphAnalysis holds AssertingVH and must be invalidated eagerly so
1277   // that other passes don't delete stuff from under it.
1278   // FIXME: We need to invalidate this to avoid PR28400. Is there a better
1279   // solution?
1280   AM.invalidate<CallGraphAnalysis>(M);
1281 
1282   if (!Changed)
1283     return PreservedAnalyses::all();
1284   PreservedAnalyses PA;
1285   PA.preserve<CallGraphAnalysis>();
1286   return PA;
1287 }
1288