1 //===- FunctionAttrs.cpp - Pass which marks functions attributes ----------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 /// \file
10 /// This file implements interprocedural passes which walk the
11 /// call-graph deducing and/or propagating function attributes.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "llvm/Transforms/IPO/FunctionAttrs.h"
16 #include "llvm/ADT/ArrayRef.h"
17 #include "llvm/ADT/DenseMap.h"
18 #include "llvm/ADT/SCCIterator.h"
19 #include "llvm/ADT/STLExtras.h"
20 #include "llvm/ADT/SetVector.h"
21 #include "llvm/ADT/SmallPtrSet.h"
22 #include "llvm/ADT/SmallSet.h"
23 #include "llvm/ADT/SmallVector.h"
24 #include "llvm/ADT/Statistic.h"
25 #include "llvm/Analysis/AssumptionCache.h"
26 #include "llvm/Analysis/BasicAliasAnalysis.h"
27 #include "llvm/Analysis/CFG.h"
28 #include "llvm/Analysis/CGSCCPassManager.h"
29 #include "llvm/Analysis/CallGraph.h"
30 #include "llvm/Analysis/CallGraphSCCPass.h"
31 #include "llvm/Analysis/CaptureTracking.h"
32 #include "llvm/Analysis/LazyCallGraph.h"
33 #include "llvm/Analysis/MemoryBuiltins.h"
34 #include "llvm/Analysis/MemoryLocation.h"
35 #include "llvm/Analysis/ValueTracking.h"
36 #include "llvm/IR/Argument.h"
37 #include "llvm/IR/Attributes.h"
38 #include "llvm/IR/BasicBlock.h"
39 #include "llvm/IR/Constant.h"
40 #include "llvm/IR/Constants.h"
41 #include "llvm/IR/Function.h"
42 #include "llvm/IR/InstIterator.h"
43 #include "llvm/IR/InstrTypes.h"
44 #include "llvm/IR/Instruction.h"
45 #include "llvm/IR/Instructions.h"
46 #include "llvm/IR/IntrinsicInst.h"
47 #include "llvm/IR/Metadata.h"
48 #include "llvm/IR/PassManager.h"
49 #include "llvm/IR/Type.h"
50 #include "llvm/IR/Use.h"
51 #include "llvm/IR/User.h"
52 #include "llvm/IR/Value.h"
53 #include "llvm/InitializePasses.h"
54 #include "llvm/Pass.h"
55 #include "llvm/Support/Casting.h"
56 #include "llvm/Support/CommandLine.h"
57 #include "llvm/Support/Compiler.h"
58 #include "llvm/Support/Debug.h"
59 #include "llvm/Support/ErrorHandling.h"
60 #include "llvm/Support/raw_ostream.h"
61 #include "llvm/Transforms/IPO.h"
62 #include "llvm/Transforms/Utils/Local.h"
63 #include <cassert>
64 #include <iterator>
65 #include <map>
66 #include <vector>
67 
68 using namespace llvm;
69 
70 #define DEBUG_TYPE "function-attrs"
71 
72 STATISTIC(NumReadNone, "Number of functions marked readnone");
73 STATISTIC(NumReadOnly, "Number of functions marked readonly");
74 STATISTIC(NumWriteOnly, "Number of functions marked writeonly");
75 STATISTIC(NumNoCapture, "Number of arguments marked nocapture");
76 STATISTIC(NumReturned, "Number of arguments marked returned");
77 STATISTIC(NumReadNoneArg, "Number of arguments marked readnone");
78 STATISTIC(NumReadOnlyArg, "Number of arguments marked readonly");
79 STATISTIC(NumWriteOnlyArg, "Number of arguments marked writeonly");
80 STATISTIC(NumNoAlias, "Number of function returns marked noalias");
81 STATISTIC(NumNonNullReturn, "Number of function returns marked nonnull");
82 STATISTIC(NumNoRecurse, "Number of functions marked as norecurse");
83 STATISTIC(NumNoUnwind, "Number of functions marked as nounwind");
84 STATISTIC(NumNoFree, "Number of functions marked as nofree");
85 STATISTIC(NumWillReturn, "Number of functions marked as willreturn");
86 STATISTIC(NumNoSync, "Number of functions marked as nosync");
87 
88 STATISTIC(NumThinLinkNoRecurse,
89           "Number of functions marked as norecurse during thinlink");
90 STATISTIC(NumThinLinkNoUnwind,
91           "Number of functions marked as nounwind during thinlink");
92 
93 static cl::opt<bool> EnableNonnullArgPropagation(
94     "enable-nonnull-arg-prop", cl::init(true), cl::Hidden,
95     cl::desc("Try to propagate nonnull argument attributes from callsites to "
96              "caller functions."));
97 
98 static cl::opt<bool> DisableNoUnwindInference(
99     "disable-nounwind-inference", cl::Hidden,
100     cl::desc("Stop inferring nounwind attribute during function-attrs pass"));
101 
102 static cl::opt<bool> DisableNoFreeInference(
103     "disable-nofree-inference", cl::Hidden,
104     cl::desc("Stop inferring nofree attribute during function-attrs pass"));
105 
106 static cl::opt<bool> DisableThinLTOPropagation(
107     "disable-thinlto-funcattrs", cl::init(true), cl::Hidden,
108     cl::desc("Don't propagate function-attrs in thinLTO"));
109 
110 namespace {
111 
112 using SCCNodeSet = SmallSetVector<Function *, 8>;
113 
114 } // end anonymous namespace
115 
116 /// Returns the memory access attribute for function F using AAR for AA results,
117 /// where SCCNodes is the current SCC.
118 ///
119 /// If ThisBody is true, this function may examine the function body and will
120 /// return a result pertaining to this copy of the function. If it is false, the
121 /// result will be based only on AA results for the function declaration; it
122 /// will be assumed that some other (perhaps less optimized) version of the
123 /// function may be selected at link time.
124 static FunctionModRefBehavior
125 checkFunctionMemoryAccess(Function &F, bool ThisBody, AAResults &AAR,
126                           const SCCNodeSet &SCCNodes) {
127   FunctionModRefBehavior MRB = AAR.getModRefBehavior(&F);
128   if (MRB == FMRB_DoesNotAccessMemory)
129     // Already perfect!
130     return MRB;
131 
132   if (!ThisBody)
133     return MRB;
134 
135   // Scan the function body for instructions that may read or write memory.
136   bool ReadsMemory = false;
137   bool WritesMemory = false;
138   for (Instruction &I : instructions(F)) {
139     // Some instructions can be ignored even if they read or write memory.
140     // Detect these now, skipping to the next instruction if one is found.
141     if (auto *Call = dyn_cast<CallBase>(&I)) {
142       // Ignore calls to functions in the same SCC, as long as the call sites
143       // don't have operand bundles.  Calls with operand bundles are allowed to
144       // have memory effects not described by the memory effects of the call
145       // target.
146       if (!Call->hasOperandBundles() && Call->getCalledFunction() &&
147           SCCNodes.count(Call->getCalledFunction()))
148         continue;
149       FunctionModRefBehavior MRB = AAR.getModRefBehavior(Call);
150       ModRefInfo MRI = createModRefInfo(MRB);
151 
152       // If the call doesn't access memory, we're done.
153       if (isNoModRef(MRI))
154         continue;
155 
156       // A pseudo probe call shouldn't change any function attribute since it
157       // doesn't translate to a real instruction. It comes with a memory access
158       // tag to prevent itself being removed by optimizations and not block
159       // other instructions being optimized.
160       if (isa<PseudoProbeInst>(I))
161         continue;
162 
163       if (!AliasAnalysis::onlyAccessesArgPointees(MRB)) {
164         // The call could access any memory. If that includes writes, note it.
165         if (isModSet(MRI))
166           WritesMemory = true;
167         // If it reads, note it.
168         if (isRefSet(MRI))
169           ReadsMemory = true;
170         continue;
171       }
172 
173       // Check whether all pointer arguments point to local memory, and
174       // ignore calls that only access local memory.
175       for (const Use &U : Call->args()) {
176         const Value *Arg = U;
177         if (!Arg->getType()->isPtrOrPtrVectorTy())
178           continue;
179 
180         MemoryLocation Loc =
181             MemoryLocation::getBeforeOrAfter(Arg, I.getAAMetadata());
182 
183         // Skip accesses to local or constant memory as they don't impact the
184         // externally visible mod/ref behavior.
185         if (AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true))
186           continue;
187 
188         if (isModSet(MRI))
189           // Writes non-local memory.
190           WritesMemory = true;
191         if (isRefSet(MRI))
192           // Ok, it reads non-local memory.
193           ReadsMemory = true;
194       }
195       continue;
196     } else if (LoadInst *LI = dyn_cast<LoadInst>(&I)) {
197       // Ignore non-volatile loads from local memory. (Atomic is okay here.)
198       if (!LI->isVolatile()) {
199         MemoryLocation Loc = MemoryLocation::get(LI);
200         if (AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true))
201           continue;
202       }
203     } else if (StoreInst *SI = dyn_cast<StoreInst>(&I)) {
204       // Ignore non-volatile stores to local memory. (Atomic is okay here.)
205       if (!SI->isVolatile()) {
206         MemoryLocation Loc = MemoryLocation::get(SI);
207         if (AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true))
208           continue;
209       }
210     } else if (VAArgInst *VI = dyn_cast<VAArgInst>(&I)) {
211       // Ignore vaargs on local memory.
212       MemoryLocation Loc = MemoryLocation::get(VI);
213       if (AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true))
214         continue;
215     }
216 
217     // Any remaining instructions need to be taken seriously!  Check if they
218     // read or write memory.
219     //
220     // Writes memory, remember that.
221     WritesMemory |= I.mayWriteToMemory();
222 
223     // If this instruction may read memory, remember that.
224     ReadsMemory |= I.mayReadFromMemory();
225   }
226 
227   if (WritesMemory) {
228     if (!ReadsMemory)
229       return FMRB_OnlyWritesMemory;
230     else
231       return FMRB_UnknownModRefBehavior;
232   }
233 
234   return ReadsMemory ? FMRB_OnlyReadsMemory : FMRB_DoesNotAccessMemory;
235 }
236 
237 FunctionModRefBehavior llvm::computeFunctionBodyMemoryAccess(Function &F,
238                                                              AAResults &AAR) {
239   return checkFunctionMemoryAccess(F, /*ThisBody=*/true, AAR, {});
240 }
241 
242 /// Deduce readonly/readnone/writeonly attributes for the SCC.
243 template <typename AARGetterT>
244 static void addMemoryAttrs(const SCCNodeSet &SCCNodes, AARGetterT &&AARGetter,
245                            SmallSet<Function *, 8> &Changed) {
246   // Check if any of the functions in the SCC read or write memory.  If they
247   // write memory then they can't be marked readnone or readonly.
248   bool ReadsMemory = false;
249   bool WritesMemory = false;
250   for (Function *F : SCCNodes) {
251     // Call the callable parameter to look up AA results for this function.
252     AAResults &AAR = AARGetter(*F);
253 
254     // Non-exact function definitions may not be selected at link time, and an
255     // alternative version that writes to memory may be selected.  See the
256     // comment on GlobalValue::isDefinitionExact for more details.
257     FunctionModRefBehavior FMRB =
258         checkFunctionMemoryAccess(*F, F->hasExactDefinition(), AAR, SCCNodes);
259     if (isModAndRefSet(createModRefInfo(FMRB)))
260       return;
261     if (FMRB == FMRB_DoesNotAccessMemory)
262       continue;
263     ReadsMemory |= AliasAnalysis::onlyReadsMemory(FMRB);
264     WritesMemory |= AliasAnalysis::onlyWritesMemory(FMRB);
265   }
266 
267   // If the SCC contains both functions that read and functions that write, then
268   // we cannot add readonly attributes.
269   if (ReadsMemory && WritesMemory)
270     return;
271 
272   // Success!  Functions in this SCC do not access memory, or only read memory.
273   // Give them the appropriate attribute.
274 
275   for (Function *F : SCCNodes) {
276     if (F->doesNotAccessMemory())
277       // Already perfect!
278       continue;
279 
280     if (F->onlyReadsMemory() && ReadsMemory)
281       // No change.
282       continue;
283 
284     if (F->onlyWritesMemory() && WritesMemory)
285       continue;
286 
287     Changed.insert(F);
288 
289     // Clear out any existing attributes.
290     AttributeMask AttrsToRemove;
291     AttrsToRemove.addAttribute(Attribute::ReadOnly);
292     AttrsToRemove.addAttribute(Attribute::ReadNone);
293     AttrsToRemove.addAttribute(Attribute::WriteOnly);
294 
295     if (!WritesMemory && !ReadsMemory) {
296       // Clear out any "access range attributes" if readnone was deduced.
297       AttrsToRemove.addAttribute(Attribute::ArgMemOnly);
298       AttrsToRemove.addAttribute(Attribute::InaccessibleMemOnly);
299       AttrsToRemove.addAttribute(Attribute::InaccessibleMemOrArgMemOnly);
300     }
301     F->removeFnAttrs(AttrsToRemove);
302 
303     // Add in the new attribute.
304     if (WritesMemory && !ReadsMemory)
305       F->addFnAttr(Attribute::WriteOnly);
306     else
307       F->addFnAttr(ReadsMemory ? Attribute::ReadOnly : Attribute::ReadNone);
308 
309     if (WritesMemory && !ReadsMemory)
310       ++NumWriteOnly;
311     else if (ReadsMemory)
312       ++NumReadOnly;
313     else
314       ++NumReadNone;
315   }
316 }
317 
318 // Compute definitive function attributes for a function taking into account
319 // prevailing definitions and linkage types
320 static FunctionSummary *calculatePrevailingSummary(
321     ValueInfo VI,
322     DenseMap<ValueInfo, FunctionSummary *> &CachedPrevailingSummary,
323     function_ref<bool(GlobalValue::GUID, const GlobalValueSummary *)>
324         IsPrevailing) {
325 
326   if (CachedPrevailingSummary.count(VI))
327     return CachedPrevailingSummary[VI];
328 
329   /// At this point, prevailing symbols have been resolved. The following leads
330   /// to returning a conservative result:
331   /// - Multiple instances with local linkage. Normally local linkage would be
332   ///   unique per module
333   ///   as the GUID includes the module path. We could have a guid alias if
334   ///   there wasn't any distinguishing path when each file was compiled, but
335   ///   that should be rare so we'll punt on those.
336 
337   /// These next 2 cases should not happen and will assert:
338   /// - Multiple instances with external linkage. This should be caught in
339   ///   symbol resolution
340   /// - Non-existent FunctionSummary for Aliasee. This presents a hole in our
341   ///   knowledge meaning we have to go conservative.
342 
343   /// Otherwise, we calculate attributes for a function as:
344   ///   1. If we have a local linkage, take its attributes. If there's somehow
345   ///      multiple, bail and go conservative.
346   ///   2. If we have an external/WeakODR/LinkOnceODR linkage check that it is
347   ///      prevailing, take its attributes.
348   ///   3. If we have a Weak/LinkOnce linkage the copies can have semantic
349   ///      differences. However, if the prevailing copy is known it will be used
350   ///      so take its attributes. If the prevailing copy is in a native file
351   ///      all IR copies will be dead and propagation will go conservative.
352   ///   4. AvailableExternally summaries without a prevailing copy are known to
353   ///      occur in a couple of circumstances:
354   ///      a. An internal function gets imported due to its caller getting
355   ///         imported, it becomes AvailableExternally but no prevailing
356   ///         definition exists. Because it has to get imported along with its
357   ///         caller the attributes will be captured by propagating on its
358   ///         caller.
359   ///      b. C++11 [temp.explicit]p10 can generate AvailableExternally
360   ///         definitions of explicitly instanced template declarations
361   ///         for inlining which are ultimately dropped from the TU. Since this
362   ///         is localized to the TU the attributes will have already made it to
363   ///         the callers.
364   ///      These are edge cases and already captured by their callers so we
365   ///      ignore these for now. If they become relevant to optimize in the
366   ///      future this can be revisited.
367   ///   5. Otherwise, go conservative.
368 
369   CachedPrevailingSummary[VI] = nullptr;
370   FunctionSummary *Local = nullptr;
371   FunctionSummary *Prevailing = nullptr;
372 
373   for (const auto &GVS : VI.getSummaryList()) {
374     if (!GVS->isLive())
375       continue;
376 
377     FunctionSummary *FS = dyn_cast<FunctionSummary>(GVS->getBaseObject());
378     // Virtual and Unknown (e.g. indirect) calls require going conservative
379     if (!FS || FS->fflags().HasUnknownCall)
380       return nullptr;
381 
382     const auto &Linkage = GVS->linkage();
383     if (GlobalValue::isLocalLinkage(Linkage)) {
384       if (Local) {
385         LLVM_DEBUG(
386             dbgs()
387             << "ThinLTO FunctionAttrs: Multiple Local Linkage, bailing on "
388                "function "
389             << VI.name() << " from " << FS->modulePath() << ". Previous module "
390             << Local->modulePath() << "\n");
391         return nullptr;
392       }
393       Local = FS;
394     } else if (GlobalValue::isExternalLinkage(Linkage)) {
395       assert(IsPrevailing(VI.getGUID(), GVS.get()));
396       Prevailing = FS;
397       break;
398     } else if (GlobalValue::isWeakODRLinkage(Linkage) ||
399                GlobalValue::isLinkOnceODRLinkage(Linkage) ||
400                GlobalValue::isWeakAnyLinkage(Linkage) ||
401                GlobalValue::isLinkOnceAnyLinkage(Linkage)) {
402       if (IsPrevailing(VI.getGUID(), GVS.get())) {
403         Prevailing = FS;
404         break;
405       }
406     } else if (GlobalValue::isAvailableExternallyLinkage(Linkage)) {
407       // TODO: Handle these cases if they become meaningful
408       continue;
409     }
410   }
411 
412   if (Local) {
413     assert(!Prevailing);
414     CachedPrevailingSummary[VI] = Local;
415   } else if (Prevailing) {
416     assert(!Local);
417     CachedPrevailingSummary[VI] = Prevailing;
418   }
419 
420   return CachedPrevailingSummary[VI];
421 }
422 
423 bool llvm::thinLTOPropagateFunctionAttrs(
424     ModuleSummaryIndex &Index,
425     function_ref<bool(GlobalValue::GUID, const GlobalValueSummary *)>
426         IsPrevailing) {
427   // TODO: implement addNoAliasAttrs once
428   // there's more information about the return type in the summary
429   if (DisableThinLTOPropagation)
430     return false;
431 
432   DenseMap<ValueInfo, FunctionSummary *> CachedPrevailingSummary;
433   bool Changed = false;
434 
435   auto PropagateAttributes = [&](std::vector<ValueInfo> &SCCNodes) {
436     // Assume we can propagate unless we discover otherwise
437     FunctionSummary::FFlags InferredFlags;
438     InferredFlags.NoRecurse = (SCCNodes.size() == 1);
439     InferredFlags.NoUnwind = true;
440 
441     for (auto &V : SCCNodes) {
442       FunctionSummary *CallerSummary =
443           calculatePrevailingSummary(V, CachedPrevailingSummary, IsPrevailing);
444 
445       // Function summaries can fail to contain information such as declarations
446       if (!CallerSummary)
447         return;
448 
449       if (CallerSummary->fflags().MayThrow)
450         InferredFlags.NoUnwind = false;
451 
452       for (const auto &Callee : CallerSummary->calls()) {
453         FunctionSummary *CalleeSummary = calculatePrevailingSummary(
454             Callee.first, CachedPrevailingSummary, IsPrevailing);
455 
456         if (!CalleeSummary)
457           return;
458 
459         if (!CalleeSummary->fflags().NoRecurse)
460           InferredFlags.NoRecurse = false;
461 
462         if (!CalleeSummary->fflags().NoUnwind)
463           InferredFlags.NoUnwind = false;
464 
465         if (!InferredFlags.NoUnwind && !InferredFlags.NoRecurse)
466           break;
467       }
468     }
469 
470     if (InferredFlags.NoUnwind || InferredFlags.NoRecurse) {
471       Changed = true;
472       for (auto &V : SCCNodes) {
473         if (InferredFlags.NoRecurse) {
474           LLVM_DEBUG(dbgs() << "ThinLTO FunctionAttrs: Propagated NoRecurse to "
475                             << V.name() << "\n");
476           ++NumThinLinkNoRecurse;
477         }
478 
479         if (InferredFlags.NoUnwind) {
480           LLVM_DEBUG(dbgs() << "ThinLTO FunctionAttrs: Propagated NoUnwind to "
481                             << V.name() << "\n");
482           ++NumThinLinkNoUnwind;
483         }
484 
485         for (auto &S : V.getSummaryList()) {
486           if (auto *FS = dyn_cast<FunctionSummary>(S.get())) {
487             if (InferredFlags.NoRecurse)
488               FS->setNoRecurse();
489 
490             if (InferredFlags.NoUnwind)
491               FS->setNoUnwind();
492           }
493         }
494       }
495     }
496   };
497 
498   // Call propagation functions on each SCC in the Index
499   for (scc_iterator<ModuleSummaryIndex *> I = scc_begin(&Index); !I.isAtEnd();
500        ++I) {
501     std::vector<ValueInfo> Nodes(*I);
502     PropagateAttributes(Nodes);
503   }
504   return Changed;
505 }
506 
507 namespace {
508 
509 /// For a given pointer Argument, this retains a list of Arguments of functions
510 /// in the same SCC that the pointer data flows into. We use this to build an
511 /// SCC of the arguments.
512 struct ArgumentGraphNode {
513   Argument *Definition;
514   SmallVector<ArgumentGraphNode *, 4> Uses;
515 };
516 
517 class ArgumentGraph {
518   // We store pointers to ArgumentGraphNode objects, so it's important that
519   // that they not move around upon insert.
520   using ArgumentMapTy = std::map<Argument *, ArgumentGraphNode>;
521 
522   ArgumentMapTy ArgumentMap;
523 
524   // There is no root node for the argument graph, in fact:
525   //   void f(int *x, int *y) { if (...) f(x, y); }
526   // is an example where the graph is disconnected. The SCCIterator requires a
527   // single entry point, so we maintain a fake ("synthetic") root node that
528   // uses every node. Because the graph is directed and nothing points into
529   // the root, it will not participate in any SCCs (except for its own).
530   ArgumentGraphNode SyntheticRoot;
531 
532 public:
533   ArgumentGraph() { SyntheticRoot.Definition = nullptr; }
534 
535   using iterator = SmallVectorImpl<ArgumentGraphNode *>::iterator;
536 
537   iterator begin() { return SyntheticRoot.Uses.begin(); }
538   iterator end() { return SyntheticRoot.Uses.end(); }
539   ArgumentGraphNode *getEntryNode() { return &SyntheticRoot; }
540 
541   ArgumentGraphNode *operator[](Argument *A) {
542     ArgumentGraphNode &Node = ArgumentMap[A];
543     Node.Definition = A;
544     SyntheticRoot.Uses.push_back(&Node);
545     return &Node;
546   }
547 };
548 
549 /// This tracker checks whether callees are in the SCC, and if so it does not
550 /// consider that a capture, instead adding it to the "Uses" list and
551 /// continuing with the analysis.
552 struct ArgumentUsesTracker : public CaptureTracker {
553   ArgumentUsesTracker(const SCCNodeSet &SCCNodes) : SCCNodes(SCCNodes) {}
554 
555   void tooManyUses() override { Captured = true; }
556 
557   bool captured(const Use *U) override {
558     CallBase *CB = dyn_cast<CallBase>(U->getUser());
559     if (!CB) {
560       Captured = true;
561       return true;
562     }
563 
564     Function *F = CB->getCalledFunction();
565     if (!F || !F->hasExactDefinition() || !SCCNodes.count(F)) {
566       Captured = true;
567       return true;
568     }
569 
570     assert(!CB->isCallee(U) && "callee operand reported captured?");
571     const unsigned UseIndex = CB->getDataOperandNo(U);
572     if (UseIndex >= CB->arg_size()) {
573       // Data operand, but not a argument operand -- must be a bundle operand
574       assert(CB->hasOperandBundles() && "Must be!");
575 
576       // CaptureTracking told us that we're being captured by an operand bundle
577       // use.  In this case it does not matter if the callee is within our SCC
578       // or not -- we've been captured in some unknown way, and we have to be
579       // conservative.
580       Captured = true;
581       return true;
582     }
583 
584     if (UseIndex >= F->arg_size()) {
585       assert(F->isVarArg() && "More params than args in non-varargs call");
586       Captured = true;
587       return true;
588     }
589 
590     Uses.push_back(&*std::next(F->arg_begin(), UseIndex));
591     return false;
592   }
593 
594   // True only if certainly captured (used outside our SCC).
595   bool Captured = false;
596 
597   // Uses within our SCC.
598   SmallVector<Argument *, 4> Uses;
599 
600   const SCCNodeSet &SCCNodes;
601 };
602 
603 } // end anonymous namespace
604 
605 namespace llvm {
606 
607 template <> struct GraphTraits<ArgumentGraphNode *> {
608   using NodeRef = ArgumentGraphNode *;
609   using ChildIteratorType = SmallVectorImpl<ArgumentGraphNode *>::iterator;
610 
611   static NodeRef getEntryNode(NodeRef A) { return A; }
612   static ChildIteratorType child_begin(NodeRef N) { return N->Uses.begin(); }
613   static ChildIteratorType child_end(NodeRef N) { return N->Uses.end(); }
614 };
615 
616 template <>
617 struct GraphTraits<ArgumentGraph *> : public GraphTraits<ArgumentGraphNode *> {
618   static NodeRef getEntryNode(ArgumentGraph *AG) { return AG->getEntryNode(); }
619 
620   static ChildIteratorType nodes_begin(ArgumentGraph *AG) {
621     return AG->begin();
622   }
623 
624   static ChildIteratorType nodes_end(ArgumentGraph *AG) { return AG->end(); }
625 };
626 
627 } // end namespace llvm
628 
629 /// Returns Attribute::None, Attribute::ReadOnly or Attribute::ReadNone.
630 static Attribute::AttrKind
631 determinePointerAccessAttrs(Argument *A,
632                             const SmallPtrSet<Argument *, 8> &SCCNodes) {
633   SmallVector<Use *, 32> Worklist;
634   SmallPtrSet<Use *, 32> Visited;
635 
636   // inalloca arguments are always clobbered by the call.
637   if (A->hasInAllocaAttr() || A->hasPreallocatedAttr())
638     return Attribute::None;
639 
640   bool IsRead = false;
641   bool IsWrite = false;
642 
643   for (Use &U : A->uses()) {
644     Visited.insert(&U);
645     Worklist.push_back(&U);
646   }
647 
648   while (!Worklist.empty()) {
649     if (IsWrite && IsRead)
650       // No point in searching further..
651       return Attribute::None;
652 
653     Use *U = Worklist.pop_back_val();
654     Instruction *I = cast<Instruction>(U->getUser());
655 
656     switch (I->getOpcode()) {
657     case Instruction::BitCast:
658     case Instruction::GetElementPtr:
659     case Instruction::PHI:
660     case Instruction::Select:
661     case Instruction::AddrSpaceCast:
662       // The original value is not read/written via this if the new value isn't.
663       for (Use &UU : I->uses())
664         if (Visited.insert(&UU).second)
665           Worklist.push_back(&UU);
666       break;
667 
668     case Instruction::Call:
669     case Instruction::Invoke: {
670       CallBase &CB = cast<CallBase>(*I);
671       if (CB.isCallee(U)) {
672         IsRead = true;
673         // Note that indirect calls do not capture, see comment in
674         // CaptureTracking for context
675         continue;
676       }
677 
678       // Given we've explictily handled the callee operand above, what's left
679       // must be a data operand (e.g. argument or operand bundle)
680       const unsigned UseIndex = CB.getDataOperandNo(U);
681 
682       if (!CB.doesNotCapture(UseIndex)) {
683         if (!CB.onlyReadsMemory())
684           // If the callee can save a copy into other memory, then simply
685           // scanning uses of the call is insufficient.  We have no way
686           // of tracking copies of the pointer through memory to see
687           // if a reloaded copy is written to, thus we must give up.
688           return Attribute::None;
689         // Push users for processing once we finish this one
690         if (!I->getType()->isVoidTy())
691           for (Use &UU : I->uses())
692             if (Visited.insert(&UU).second)
693               Worklist.push_back(&UU);
694       }
695 
696       if (CB.doesNotAccessMemory())
697         continue;
698 
699       if (Function *F = CB.getCalledFunction())
700         if (CB.isArgOperand(U) && UseIndex < F->arg_size() &&
701             SCCNodes.count(F->getArg(UseIndex)))
702           // This is an argument which is part of the speculative SCC.  Note
703           // that only operands corresponding to formal arguments of the callee
704           // can participate in the speculation.
705           break;
706 
707       // The accessors used on call site here do the right thing for calls and
708       // invokes with operand bundles.
709       if (CB.doesNotAccessMemory(UseIndex)) {
710         /* nop */
711       } else if (CB.onlyReadsMemory() || CB.onlyReadsMemory(UseIndex)) {
712         IsRead = true;
713       } else if (CB.hasFnAttr(Attribute::WriteOnly) ||
714                  CB.dataOperandHasImpliedAttr(UseIndex, Attribute::WriteOnly)) {
715         IsWrite = true;
716       } else {
717         return Attribute::None;
718       }
719       break;
720     }
721 
722     case Instruction::Load:
723       // A volatile load has side effects beyond what readonly can be relied
724       // upon.
725       if (cast<LoadInst>(I)->isVolatile())
726         return Attribute::None;
727 
728       IsRead = true;
729       break;
730 
731     case Instruction::Store:
732       if (cast<StoreInst>(I)->getValueOperand() == *U)
733         // untrackable capture
734         return Attribute::None;
735 
736       // A volatile store has side effects beyond what writeonly can be relied
737       // upon.
738       if (cast<StoreInst>(I)->isVolatile())
739         return Attribute::None;
740 
741       IsWrite = true;
742       break;
743 
744     case Instruction::ICmp:
745     case Instruction::Ret:
746       break;
747 
748     default:
749       return Attribute::None;
750     }
751   }
752 
753   if (IsWrite && IsRead)
754     return Attribute::None;
755   else if (IsRead)
756     return Attribute::ReadOnly;
757   else if (IsWrite)
758     return Attribute::WriteOnly;
759   else
760     return Attribute::ReadNone;
761 }
762 
763 /// Deduce returned attributes for the SCC.
764 static void addArgumentReturnedAttrs(const SCCNodeSet &SCCNodes,
765                                      SmallSet<Function *, 8> &Changed) {
766   // Check each function in turn, determining if an argument is always returned.
767   for (Function *F : SCCNodes) {
768     // We can infer and propagate function attributes only when we know that the
769     // definition we'll get at link time is *exactly* the definition we see now.
770     // For more details, see GlobalValue::mayBeDerefined.
771     if (!F->hasExactDefinition())
772       continue;
773 
774     if (F->getReturnType()->isVoidTy())
775       continue;
776 
777     // There is nothing to do if an argument is already marked as 'returned'.
778     if (llvm::any_of(F->args(),
779                      [](const Argument &Arg) { return Arg.hasReturnedAttr(); }))
780       continue;
781 
782     auto FindRetArg = [&]() -> Value * {
783       Value *RetArg = nullptr;
784       for (BasicBlock &BB : *F)
785         if (auto *Ret = dyn_cast<ReturnInst>(BB.getTerminator())) {
786           // Note that stripPointerCasts should look through functions with
787           // returned arguments.
788           Value *RetVal = Ret->getReturnValue()->stripPointerCasts();
789           if (!isa<Argument>(RetVal) || RetVal->getType() != F->getReturnType())
790             return nullptr;
791 
792           if (!RetArg)
793             RetArg = RetVal;
794           else if (RetArg != RetVal)
795             return nullptr;
796         }
797 
798       return RetArg;
799     };
800 
801     if (Value *RetArg = FindRetArg()) {
802       auto *A = cast<Argument>(RetArg);
803       A->addAttr(Attribute::Returned);
804       ++NumReturned;
805       Changed.insert(F);
806     }
807   }
808 }
809 
810 /// If a callsite has arguments that are also arguments to the parent function,
811 /// try to propagate attributes from the callsite's arguments to the parent's
812 /// arguments. This may be important because inlining can cause information loss
813 /// when attribute knowledge disappears with the inlined call.
814 static bool addArgumentAttrsFromCallsites(Function &F) {
815   if (!EnableNonnullArgPropagation)
816     return false;
817 
818   bool Changed = false;
819 
820   // For an argument attribute to transfer from a callsite to the parent, the
821   // call must be guaranteed to execute every time the parent is called.
822   // Conservatively, just check for calls in the entry block that are guaranteed
823   // to execute.
824   // TODO: This could be enhanced by testing if the callsite post-dominates the
825   // entry block or by doing simple forward walks or backward walks to the
826   // callsite.
827   BasicBlock &Entry = F.getEntryBlock();
828   for (Instruction &I : Entry) {
829     if (auto *CB = dyn_cast<CallBase>(&I)) {
830       if (auto *CalledFunc = CB->getCalledFunction()) {
831         for (auto &CSArg : CalledFunc->args()) {
832           if (!CSArg.hasNonNullAttr(/* AllowUndefOrPoison */ false))
833             continue;
834 
835           // If the non-null callsite argument operand is an argument to 'F'
836           // (the caller) and the call is guaranteed to execute, then the value
837           // must be non-null throughout 'F'.
838           auto *FArg = dyn_cast<Argument>(CB->getArgOperand(CSArg.getArgNo()));
839           if (FArg && !FArg->hasNonNullAttr()) {
840             FArg->addAttr(Attribute::NonNull);
841             Changed = true;
842           }
843         }
844       }
845     }
846     if (!isGuaranteedToTransferExecutionToSuccessor(&I))
847       break;
848   }
849 
850   return Changed;
851 }
852 
853 static bool addAccessAttr(Argument *A, Attribute::AttrKind R) {
854   assert((R == Attribute::ReadOnly || R == Attribute::ReadNone ||
855           R == Attribute::WriteOnly)
856          && "Must be an access attribute.");
857   assert(A && "Argument must not be null.");
858 
859   // If the argument already has the attribute, nothing needs to be done.
860   if (A->hasAttribute(R))
861       return false;
862 
863   // Otherwise, remove potentially conflicting attribute, add the new one,
864   // and update statistics.
865   A->removeAttr(Attribute::WriteOnly);
866   A->removeAttr(Attribute::ReadOnly);
867   A->removeAttr(Attribute::ReadNone);
868   A->addAttr(R);
869   if (R == Attribute::ReadOnly)
870     ++NumReadOnlyArg;
871   else if (R == Attribute::WriteOnly)
872     ++NumWriteOnlyArg;
873   else
874     ++NumReadNoneArg;
875   return true;
876 }
877 
878 /// Deduce nocapture attributes for the SCC.
879 static void addArgumentAttrs(const SCCNodeSet &SCCNodes,
880                              SmallSet<Function *, 8> &Changed) {
881   ArgumentGraph AG;
882 
883   // Check each function in turn, determining which pointer arguments are not
884   // captured.
885   for (Function *F : SCCNodes) {
886     // We can infer and propagate function attributes only when we know that the
887     // definition we'll get at link time is *exactly* the definition we see now.
888     // For more details, see GlobalValue::mayBeDerefined.
889     if (!F->hasExactDefinition())
890       continue;
891 
892     if (addArgumentAttrsFromCallsites(*F))
893       Changed.insert(F);
894 
895     // Functions that are readonly (or readnone) and nounwind and don't return
896     // a value can't capture arguments. Don't analyze them.
897     if (F->onlyReadsMemory() && F->doesNotThrow() &&
898         F->getReturnType()->isVoidTy()) {
899       for (Function::arg_iterator A = F->arg_begin(), E = F->arg_end(); A != E;
900            ++A) {
901         if (A->getType()->isPointerTy() && !A->hasNoCaptureAttr()) {
902           A->addAttr(Attribute::NoCapture);
903           ++NumNoCapture;
904           Changed.insert(F);
905         }
906       }
907       continue;
908     }
909 
910     for (Function::arg_iterator A = F->arg_begin(), E = F->arg_end(); A != E;
911          ++A) {
912       if (!A->getType()->isPointerTy())
913         continue;
914       bool HasNonLocalUses = false;
915       if (!A->hasNoCaptureAttr()) {
916         ArgumentUsesTracker Tracker(SCCNodes);
917         PointerMayBeCaptured(&*A, &Tracker);
918         if (!Tracker.Captured) {
919           if (Tracker.Uses.empty()) {
920             // If it's trivially not captured, mark it nocapture now.
921             A->addAttr(Attribute::NoCapture);
922             ++NumNoCapture;
923             Changed.insert(F);
924           } else {
925             // If it's not trivially captured and not trivially not captured,
926             // then it must be calling into another function in our SCC. Save
927             // its particulars for Argument-SCC analysis later.
928             ArgumentGraphNode *Node = AG[&*A];
929             for (Argument *Use : Tracker.Uses) {
930               Node->Uses.push_back(AG[Use]);
931               if (Use != &*A)
932                 HasNonLocalUses = true;
933             }
934           }
935         }
936         // Otherwise, it's captured. Don't bother doing SCC analysis on it.
937       }
938       if (!HasNonLocalUses && !A->onlyReadsMemory()) {
939         // Can we determine that it's readonly/readnone/writeonly without doing
940         // an SCC? Note that we don't allow any calls at all here, or else our
941         // result will be dependent on the iteration order through the
942         // functions in the SCC.
943         SmallPtrSet<Argument *, 8> Self;
944         Self.insert(&*A);
945         Attribute::AttrKind R = determinePointerAccessAttrs(&*A, Self);
946         if (R != Attribute::None)
947           if (addAccessAttr(A, R))
948             Changed.insert(F);
949       }
950     }
951   }
952 
953   // The graph we've collected is partial because we stopped scanning for
954   // argument uses once we solved the argument trivially. These partial nodes
955   // show up as ArgumentGraphNode objects with an empty Uses list, and for
956   // these nodes the final decision about whether they capture has already been
957   // made.  If the definition doesn't have a 'nocapture' attribute by now, it
958   // captures.
959 
960   for (scc_iterator<ArgumentGraph *> I = scc_begin(&AG); !I.isAtEnd(); ++I) {
961     const std::vector<ArgumentGraphNode *> &ArgumentSCC = *I;
962     if (ArgumentSCC.size() == 1) {
963       if (!ArgumentSCC[0]->Definition)
964         continue; // synthetic root node
965 
966       // eg. "void f(int* x) { if (...) f(x); }"
967       if (ArgumentSCC[0]->Uses.size() == 1 &&
968           ArgumentSCC[0]->Uses[0] == ArgumentSCC[0]) {
969         Argument *A = ArgumentSCC[0]->Definition;
970         A->addAttr(Attribute::NoCapture);
971         ++NumNoCapture;
972         Changed.insert(A->getParent());
973 
974         // Infer the access attributes given the new nocapture one
975         SmallPtrSet<Argument *, 8> Self;
976         Self.insert(&*A);
977         Attribute::AttrKind R = determinePointerAccessAttrs(&*A, Self);
978         if (R != Attribute::None)
979           addAccessAttr(A, R);
980       }
981       continue;
982     }
983 
984     bool SCCCaptured = false;
985     for (auto I = ArgumentSCC.begin(), E = ArgumentSCC.end();
986          I != E && !SCCCaptured; ++I) {
987       ArgumentGraphNode *Node = *I;
988       if (Node->Uses.empty()) {
989         if (!Node->Definition->hasNoCaptureAttr())
990           SCCCaptured = true;
991       }
992     }
993     if (SCCCaptured)
994       continue;
995 
996     SmallPtrSet<Argument *, 8> ArgumentSCCNodes;
997     // Fill ArgumentSCCNodes with the elements of the ArgumentSCC.  Used for
998     // quickly looking up whether a given Argument is in this ArgumentSCC.
999     for (ArgumentGraphNode *I : ArgumentSCC) {
1000       ArgumentSCCNodes.insert(I->Definition);
1001     }
1002 
1003     for (auto I = ArgumentSCC.begin(), E = ArgumentSCC.end();
1004          I != E && !SCCCaptured; ++I) {
1005       ArgumentGraphNode *N = *I;
1006       for (ArgumentGraphNode *Use : N->Uses) {
1007         Argument *A = Use->Definition;
1008         if (A->hasNoCaptureAttr() || ArgumentSCCNodes.count(A))
1009           continue;
1010         SCCCaptured = true;
1011         break;
1012       }
1013     }
1014     if (SCCCaptured)
1015       continue;
1016 
1017     for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) {
1018       Argument *A = ArgumentSCC[i]->Definition;
1019       A->addAttr(Attribute::NoCapture);
1020       ++NumNoCapture;
1021       Changed.insert(A->getParent());
1022     }
1023 
1024     // We also want to compute readonly/readnone/writeonly. With a small number
1025     // of false negatives, we can assume that any pointer which is captured
1026     // isn't going to be provably readonly or readnone, since by definition
1027     // we can't analyze all uses of a captured pointer.
1028     //
1029     // The false negatives happen when the pointer is captured by a function
1030     // that promises readonly/readnone behaviour on the pointer, then the
1031     // pointer's lifetime ends before anything that writes to arbitrary memory.
1032     // Also, a readonly/readnone pointer may be returned, but returning a
1033     // pointer is capturing it.
1034 
1035     auto meetAccessAttr = [](Attribute::AttrKind A, Attribute::AttrKind B) {
1036       if (A == B)
1037         return A;
1038       if (A == Attribute::ReadNone)
1039         return B;
1040       if (B == Attribute::ReadNone)
1041         return A;
1042       return Attribute::None;
1043     };
1044 
1045     Attribute::AttrKind AccessAttr = Attribute::ReadNone;
1046     for (unsigned i = 0, e = ArgumentSCC.size();
1047          i != e && AccessAttr != Attribute::None; ++i) {
1048       Argument *A = ArgumentSCC[i]->Definition;
1049       Attribute::AttrKind K = determinePointerAccessAttrs(A, ArgumentSCCNodes);
1050       AccessAttr = meetAccessAttr(AccessAttr, K);
1051     }
1052 
1053     if (AccessAttr != Attribute::None) {
1054       for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) {
1055         Argument *A = ArgumentSCC[i]->Definition;
1056         if (addAccessAttr(A, AccessAttr))
1057           Changed.insert(A->getParent());
1058       }
1059     }
1060   }
1061 }
1062 
1063 /// Tests whether a function is "malloc-like".
1064 ///
1065 /// A function is "malloc-like" if it returns either null or a pointer that
1066 /// doesn't alias any other pointer visible to the caller.
1067 static bool isFunctionMallocLike(Function *F, const SCCNodeSet &SCCNodes) {
1068   SmallSetVector<Value *, 8> FlowsToReturn;
1069   for (BasicBlock &BB : *F)
1070     if (ReturnInst *Ret = dyn_cast<ReturnInst>(BB.getTerminator()))
1071       FlowsToReturn.insert(Ret->getReturnValue());
1072 
1073   for (unsigned i = 0; i != FlowsToReturn.size(); ++i) {
1074     Value *RetVal = FlowsToReturn[i];
1075 
1076     if (Constant *C = dyn_cast<Constant>(RetVal)) {
1077       if (!C->isNullValue() && !isa<UndefValue>(C))
1078         return false;
1079 
1080       continue;
1081     }
1082 
1083     if (isa<Argument>(RetVal))
1084       return false;
1085 
1086     if (Instruction *RVI = dyn_cast<Instruction>(RetVal))
1087       switch (RVI->getOpcode()) {
1088       // Extend the analysis by looking upwards.
1089       case Instruction::BitCast:
1090       case Instruction::GetElementPtr:
1091       case Instruction::AddrSpaceCast:
1092         FlowsToReturn.insert(RVI->getOperand(0));
1093         continue;
1094       case Instruction::Select: {
1095         SelectInst *SI = cast<SelectInst>(RVI);
1096         FlowsToReturn.insert(SI->getTrueValue());
1097         FlowsToReturn.insert(SI->getFalseValue());
1098         continue;
1099       }
1100       case Instruction::PHI: {
1101         PHINode *PN = cast<PHINode>(RVI);
1102         for (Value *IncValue : PN->incoming_values())
1103           FlowsToReturn.insert(IncValue);
1104         continue;
1105       }
1106 
1107       // Check whether the pointer came from an allocation.
1108       case Instruction::Alloca:
1109         break;
1110       case Instruction::Call:
1111       case Instruction::Invoke: {
1112         CallBase &CB = cast<CallBase>(*RVI);
1113         if (CB.hasRetAttr(Attribute::NoAlias))
1114           break;
1115         if (CB.getCalledFunction() && SCCNodes.count(CB.getCalledFunction()))
1116           break;
1117         LLVM_FALLTHROUGH;
1118       }
1119       default:
1120         return false; // Did not come from an allocation.
1121       }
1122 
1123     if (PointerMayBeCaptured(RetVal, false, /*StoreCaptures=*/false))
1124       return false;
1125   }
1126 
1127   return true;
1128 }
1129 
1130 /// Deduce noalias attributes for the SCC.
1131 static void addNoAliasAttrs(const SCCNodeSet &SCCNodes,
1132                             SmallSet<Function *, 8> &Changed) {
1133   // Check each function in turn, determining which functions return noalias
1134   // pointers.
1135   for (Function *F : SCCNodes) {
1136     // Already noalias.
1137     if (F->returnDoesNotAlias())
1138       continue;
1139 
1140     // We can infer and propagate function attributes only when we know that the
1141     // definition we'll get at link time is *exactly* the definition we see now.
1142     // For more details, see GlobalValue::mayBeDerefined.
1143     if (!F->hasExactDefinition())
1144       return;
1145 
1146     // We annotate noalias return values, which are only applicable to
1147     // pointer types.
1148     if (!F->getReturnType()->isPointerTy())
1149       continue;
1150 
1151     if (!isFunctionMallocLike(F, SCCNodes))
1152       return;
1153   }
1154 
1155   for (Function *F : SCCNodes) {
1156     if (F->returnDoesNotAlias() ||
1157         !F->getReturnType()->isPointerTy())
1158       continue;
1159 
1160     F->setReturnDoesNotAlias();
1161     ++NumNoAlias;
1162     Changed.insert(F);
1163   }
1164 }
1165 
1166 /// Tests whether this function is known to not return null.
1167 ///
1168 /// Requires that the function returns a pointer.
1169 ///
1170 /// Returns true if it believes the function will not return a null, and sets
1171 /// \p Speculative based on whether the returned conclusion is a speculative
1172 /// conclusion due to SCC calls.
1173 static bool isReturnNonNull(Function *F, const SCCNodeSet &SCCNodes,
1174                             bool &Speculative) {
1175   assert(F->getReturnType()->isPointerTy() &&
1176          "nonnull only meaningful on pointer types");
1177   Speculative = false;
1178 
1179   SmallSetVector<Value *, 8> FlowsToReturn;
1180   for (BasicBlock &BB : *F)
1181     if (auto *Ret = dyn_cast<ReturnInst>(BB.getTerminator()))
1182       FlowsToReturn.insert(Ret->getReturnValue());
1183 
1184   auto &DL = F->getParent()->getDataLayout();
1185 
1186   for (unsigned i = 0; i != FlowsToReturn.size(); ++i) {
1187     Value *RetVal = FlowsToReturn[i];
1188 
1189     // If this value is locally known to be non-null, we're good
1190     if (isKnownNonZero(RetVal, DL))
1191       continue;
1192 
1193     // Otherwise, we need to look upwards since we can't make any local
1194     // conclusions.
1195     Instruction *RVI = dyn_cast<Instruction>(RetVal);
1196     if (!RVI)
1197       return false;
1198     switch (RVI->getOpcode()) {
1199     // Extend the analysis by looking upwards.
1200     case Instruction::BitCast:
1201     case Instruction::GetElementPtr:
1202     case Instruction::AddrSpaceCast:
1203       FlowsToReturn.insert(RVI->getOperand(0));
1204       continue;
1205     case Instruction::Select: {
1206       SelectInst *SI = cast<SelectInst>(RVI);
1207       FlowsToReturn.insert(SI->getTrueValue());
1208       FlowsToReturn.insert(SI->getFalseValue());
1209       continue;
1210     }
1211     case Instruction::PHI: {
1212       PHINode *PN = cast<PHINode>(RVI);
1213       for (int i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
1214         FlowsToReturn.insert(PN->getIncomingValue(i));
1215       continue;
1216     }
1217     case Instruction::Call:
1218     case Instruction::Invoke: {
1219       CallBase &CB = cast<CallBase>(*RVI);
1220       Function *Callee = CB.getCalledFunction();
1221       // A call to a node within the SCC is assumed to return null until
1222       // proven otherwise
1223       if (Callee && SCCNodes.count(Callee)) {
1224         Speculative = true;
1225         continue;
1226       }
1227       return false;
1228     }
1229     default:
1230       return false; // Unknown source, may be null
1231     };
1232     llvm_unreachable("should have either continued or returned");
1233   }
1234 
1235   return true;
1236 }
1237 
1238 /// Deduce nonnull attributes for the SCC.
1239 static void addNonNullAttrs(const SCCNodeSet &SCCNodes,
1240                             SmallSet<Function *, 8> &Changed) {
1241   // Speculative that all functions in the SCC return only nonnull
1242   // pointers.  We may refute this as we analyze functions.
1243   bool SCCReturnsNonNull = true;
1244 
1245   // Check each function in turn, determining which functions return nonnull
1246   // pointers.
1247   for (Function *F : SCCNodes) {
1248     // Already nonnull.
1249     if (F->getAttributes().hasRetAttr(Attribute::NonNull))
1250       continue;
1251 
1252     // We can infer and propagate function attributes only when we know that the
1253     // definition we'll get at link time is *exactly* the definition we see now.
1254     // For more details, see GlobalValue::mayBeDerefined.
1255     if (!F->hasExactDefinition())
1256       return;
1257 
1258     // We annotate nonnull return values, which are only applicable to
1259     // pointer types.
1260     if (!F->getReturnType()->isPointerTy())
1261       continue;
1262 
1263     bool Speculative = false;
1264     if (isReturnNonNull(F, SCCNodes, Speculative)) {
1265       if (!Speculative) {
1266         // Mark the function eagerly since we may discover a function
1267         // which prevents us from speculating about the entire SCC
1268         LLVM_DEBUG(dbgs() << "Eagerly marking " << F->getName()
1269                           << " as nonnull\n");
1270         F->addRetAttr(Attribute::NonNull);
1271         ++NumNonNullReturn;
1272         Changed.insert(F);
1273       }
1274       continue;
1275     }
1276     // At least one function returns something which could be null, can't
1277     // speculate any more.
1278     SCCReturnsNonNull = false;
1279   }
1280 
1281   if (SCCReturnsNonNull) {
1282     for (Function *F : SCCNodes) {
1283       if (F->getAttributes().hasRetAttr(Attribute::NonNull) ||
1284           !F->getReturnType()->isPointerTy())
1285         continue;
1286 
1287       LLVM_DEBUG(dbgs() << "SCC marking " << F->getName() << " as nonnull\n");
1288       F->addRetAttr(Attribute::NonNull);
1289       ++NumNonNullReturn;
1290       Changed.insert(F);
1291     }
1292   }
1293 }
1294 
1295 namespace {
1296 
1297 /// Collects a set of attribute inference requests and performs them all in one
1298 /// go on a single SCC Node. Inference involves scanning function bodies
1299 /// looking for instructions that violate attribute assumptions.
1300 /// As soon as all the bodies are fine we are free to set the attribute.
1301 /// Customization of inference for individual attributes is performed by
1302 /// providing a handful of predicates for each attribute.
1303 class AttributeInferer {
1304 public:
1305   /// Describes a request for inference of a single attribute.
1306   struct InferenceDescriptor {
1307 
1308     /// Returns true if this function does not have to be handled.
1309     /// General intent for this predicate is to provide an optimization
1310     /// for functions that do not need this attribute inference at all
1311     /// (say, for functions that already have the attribute).
1312     std::function<bool(const Function &)> SkipFunction;
1313 
1314     /// Returns true if this instruction violates attribute assumptions.
1315     std::function<bool(Instruction &)> InstrBreaksAttribute;
1316 
1317     /// Sets the inferred attribute for this function.
1318     std::function<void(Function &)> SetAttribute;
1319 
1320     /// Attribute we derive.
1321     Attribute::AttrKind AKind;
1322 
1323     /// If true, only "exact" definitions can be used to infer this attribute.
1324     /// See GlobalValue::isDefinitionExact.
1325     bool RequiresExactDefinition;
1326 
1327     InferenceDescriptor(Attribute::AttrKind AK,
1328                         std::function<bool(const Function &)> SkipFunc,
1329                         std::function<bool(Instruction &)> InstrScan,
1330                         std::function<void(Function &)> SetAttr,
1331                         bool ReqExactDef)
1332         : SkipFunction(SkipFunc), InstrBreaksAttribute(InstrScan),
1333           SetAttribute(SetAttr), AKind(AK),
1334           RequiresExactDefinition(ReqExactDef) {}
1335   };
1336 
1337 private:
1338   SmallVector<InferenceDescriptor, 4> InferenceDescriptors;
1339 
1340 public:
1341   void registerAttrInference(InferenceDescriptor AttrInference) {
1342     InferenceDescriptors.push_back(AttrInference);
1343   }
1344 
1345   void run(const SCCNodeSet &SCCNodes, SmallSet<Function *, 8> &Changed);
1346 };
1347 
1348 /// Perform all the requested attribute inference actions according to the
1349 /// attribute predicates stored before.
1350 void AttributeInferer::run(const SCCNodeSet &SCCNodes,
1351                            SmallSet<Function *, 8> &Changed) {
1352   SmallVector<InferenceDescriptor, 4> InferInSCC = InferenceDescriptors;
1353   // Go through all the functions in SCC and check corresponding attribute
1354   // assumptions for each of them. Attributes that are invalid for this SCC
1355   // will be removed from InferInSCC.
1356   for (Function *F : SCCNodes) {
1357 
1358     // No attributes whose assumptions are still valid - done.
1359     if (InferInSCC.empty())
1360       return;
1361 
1362     // Check if our attributes ever need scanning/can be scanned.
1363     llvm::erase_if(InferInSCC, [F](const InferenceDescriptor &ID) {
1364       if (ID.SkipFunction(*F))
1365         return false;
1366 
1367       // Remove from further inference (invalidate) when visiting a function
1368       // that has no instructions to scan/has an unsuitable definition.
1369       return F->isDeclaration() ||
1370              (ID.RequiresExactDefinition && !F->hasExactDefinition());
1371     });
1372 
1373     // For each attribute still in InferInSCC that doesn't explicitly skip F,
1374     // set up the F instructions scan to verify assumptions of the attribute.
1375     SmallVector<InferenceDescriptor, 4> InferInThisFunc;
1376     llvm::copy_if(
1377         InferInSCC, std::back_inserter(InferInThisFunc),
1378         [F](const InferenceDescriptor &ID) { return !ID.SkipFunction(*F); });
1379 
1380     if (InferInThisFunc.empty())
1381       continue;
1382 
1383     // Start instruction scan.
1384     for (Instruction &I : instructions(*F)) {
1385       llvm::erase_if(InferInThisFunc, [&](const InferenceDescriptor &ID) {
1386         if (!ID.InstrBreaksAttribute(I))
1387           return false;
1388         // Remove attribute from further inference on any other functions
1389         // because attribute assumptions have just been violated.
1390         llvm::erase_if(InferInSCC, [&ID](const InferenceDescriptor &D) {
1391           return D.AKind == ID.AKind;
1392         });
1393         // Remove attribute from the rest of current instruction scan.
1394         return true;
1395       });
1396 
1397       if (InferInThisFunc.empty())
1398         break;
1399     }
1400   }
1401 
1402   if (InferInSCC.empty())
1403     return;
1404 
1405   for (Function *F : SCCNodes)
1406     // At this point InferInSCC contains only functions that were either:
1407     //   - explicitly skipped from scan/inference, or
1408     //   - verified to have no instructions that break attribute assumptions.
1409     // Hence we just go and force the attribute for all non-skipped functions.
1410     for (auto &ID : InferInSCC) {
1411       if (ID.SkipFunction(*F))
1412         continue;
1413       Changed.insert(F);
1414       ID.SetAttribute(*F);
1415     }
1416 }
1417 
1418 struct SCCNodesResult {
1419   SCCNodeSet SCCNodes;
1420   bool HasUnknownCall;
1421 };
1422 
1423 } // end anonymous namespace
1424 
1425 /// Helper for non-Convergent inference predicate InstrBreaksAttribute.
1426 static bool InstrBreaksNonConvergent(Instruction &I,
1427                                      const SCCNodeSet &SCCNodes) {
1428   const CallBase *CB = dyn_cast<CallBase>(&I);
1429   // Breaks non-convergent assumption if CS is a convergent call to a function
1430   // not in the SCC.
1431   return CB && CB->isConvergent() &&
1432          !SCCNodes.contains(CB->getCalledFunction());
1433 }
1434 
1435 /// Helper for NoUnwind inference predicate InstrBreaksAttribute.
1436 static bool InstrBreaksNonThrowing(Instruction &I, const SCCNodeSet &SCCNodes) {
1437   if (!I.mayThrow())
1438     return false;
1439   if (const auto *CI = dyn_cast<CallInst>(&I)) {
1440     if (Function *Callee = CI->getCalledFunction()) {
1441       // I is a may-throw call to a function inside our SCC. This doesn't
1442       // invalidate our current working assumption that the SCC is no-throw; we
1443       // just have to scan that other function.
1444       if (SCCNodes.contains(Callee))
1445         return false;
1446     }
1447   }
1448   return true;
1449 }
1450 
1451 /// Helper for NoFree inference predicate InstrBreaksAttribute.
1452 static bool InstrBreaksNoFree(Instruction &I, const SCCNodeSet &SCCNodes) {
1453   CallBase *CB = dyn_cast<CallBase>(&I);
1454   if (!CB)
1455     return false;
1456 
1457   if (CB->hasFnAttr(Attribute::NoFree))
1458     return false;
1459 
1460   // Speculatively assume in SCC.
1461   if (Function *Callee = CB->getCalledFunction())
1462     if (SCCNodes.contains(Callee))
1463       return false;
1464 
1465   return true;
1466 }
1467 
1468 /// Attempt to remove convergent function attribute when possible.
1469 ///
1470 /// Returns true if any changes to function attributes were made.
1471 static void inferConvergent(const SCCNodeSet &SCCNodes,
1472                             SmallSet<Function *, 8> &Changed) {
1473   AttributeInferer AI;
1474 
1475   // Request to remove the convergent attribute from all functions in the SCC
1476   // if every callsite within the SCC is not convergent (except for calls
1477   // to functions within the SCC).
1478   // Note: Removal of the attr from the callsites will happen in
1479   // InstCombineCalls separately.
1480   AI.registerAttrInference(AttributeInferer::InferenceDescriptor{
1481       Attribute::Convergent,
1482       // Skip non-convergent functions.
1483       [](const Function &F) { return !F.isConvergent(); },
1484       // Instructions that break non-convergent assumption.
1485       [SCCNodes](Instruction &I) {
1486         return InstrBreaksNonConvergent(I, SCCNodes);
1487       },
1488       [](Function &F) {
1489         LLVM_DEBUG(dbgs() << "Removing convergent attr from fn " << F.getName()
1490                           << "\n");
1491         F.setNotConvergent();
1492       },
1493       /* RequiresExactDefinition= */ false});
1494   // Perform all the requested attribute inference actions.
1495   AI.run(SCCNodes, Changed);
1496 }
1497 
1498 /// Infer attributes from all functions in the SCC by scanning every
1499 /// instruction for compliance to the attribute assumptions. Currently it
1500 /// does:
1501 ///   - addition of NoUnwind attribute
1502 ///
1503 /// Returns true if any changes to function attributes were made.
1504 static void inferAttrsFromFunctionBodies(const SCCNodeSet &SCCNodes,
1505                                          SmallSet<Function *, 8> &Changed) {
1506   AttributeInferer AI;
1507 
1508   if (!DisableNoUnwindInference)
1509     // Request to infer nounwind attribute for all the functions in the SCC if
1510     // every callsite within the SCC is not throwing (except for calls to
1511     // functions within the SCC). Note that nounwind attribute suffers from
1512     // derefinement - results may change depending on how functions are
1513     // optimized. Thus it can be inferred only from exact definitions.
1514     AI.registerAttrInference(AttributeInferer::InferenceDescriptor{
1515         Attribute::NoUnwind,
1516         // Skip non-throwing functions.
1517         [](const Function &F) { return F.doesNotThrow(); },
1518         // Instructions that break non-throwing assumption.
1519         [&SCCNodes](Instruction &I) {
1520           return InstrBreaksNonThrowing(I, SCCNodes);
1521         },
1522         [](Function &F) {
1523           LLVM_DEBUG(dbgs()
1524                      << "Adding nounwind attr to fn " << F.getName() << "\n");
1525           F.setDoesNotThrow();
1526           ++NumNoUnwind;
1527         },
1528         /* RequiresExactDefinition= */ true});
1529 
1530   if (!DisableNoFreeInference)
1531     // Request to infer nofree attribute for all the functions in the SCC if
1532     // every callsite within the SCC does not directly or indirectly free
1533     // memory (except for calls to functions within the SCC). Note that nofree
1534     // attribute suffers from derefinement - results may change depending on
1535     // how functions are optimized. Thus it can be inferred only from exact
1536     // definitions.
1537     AI.registerAttrInference(AttributeInferer::InferenceDescriptor{
1538         Attribute::NoFree,
1539         // Skip functions known not to free memory.
1540         [](const Function &F) { return F.doesNotFreeMemory(); },
1541         // Instructions that break non-deallocating assumption.
1542         [&SCCNodes](Instruction &I) {
1543           return InstrBreaksNoFree(I, SCCNodes);
1544         },
1545         [](Function &F) {
1546           LLVM_DEBUG(dbgs()
1547                      << "Adding nofree attr to fn " << F.getName() << "\n");
1548           F.setDoesNotFreeMemory();
1549           ++NumNoFree;
1550         },
1551         /* RequiresExactDefinition= */ true});
1552 
1553   // Perform all the requested attribute inference actions.
1554   AI.run(SCCNodes, Changed);
1555 }
1556 
1557 static void addNoRecurseAttrs(const SCCNodeSet &SCCNodes,
1558                               SmallSet<Function *, 8> &Changed) {
1559   // Try and identify functions that do not recurse.
1560 
1561   // If the SCC contains multiple nodes we know for sure there is recursion.
1562   if (SCCNodes.size() != 1)
1563     return;
1564 
1565   Function *F = *SCCNodes.begin();
1566   if (!F || !F->hasExactDefinition() || F->doesNotRecurse())
1567     return;
1568 
1569   // If all of the calls in F are identifiable and are to norecurse functions, F
1570   // is norecurse. This check also detects self-recursion as F is not currently
1571   // marked norecurse, so any called from F to F will not be marked norecurse.
1572   for (auto &BB : *F)
1573     for (auto &I : BB.instructionsWithoutDebug())
1574       if (auto *CB = dyn_cast<CallBase>(&I)) {
1575         Function *Callee = CB->getCalledFunction();
1576         if (!Callee || Callee == F || !Callee->doesNotRecurse())
1577           // Function calls a potentially recursive function.
1578           return;
1579       }
1580 
1581   // Every call was to a non-recursive function other than this function, and
1582   // we have no indirect recursion as the SCC size is one. This function cannot
1583   // recurse.
1584   F->setDoesNotRecurse();
1585   ++NumNoRecurse;
1586   Changed.insert(F);
1587 }
1588 
1589 static bool instructionDoesNotReturn(Instruction &I) {
1590   if (auto *CB = dyn_cast<CallBase>(&I))
1591     return CB->hasFnAttr(Attribute::NoReturn);
1592   return false;
1593 }
1594 
1595 // A basic block can only return if it terminates with a ReturnInst and does not
1596 // contain calls to noreturn functions.
1597 static bool basicBlockCanReturn(BasicBlock &BB) {
1598   if (!isa<ReturnInst>(BB.getTerminator()))
1599     return false;
1600   return none_of(BB, instructionDoesNotReturn);
1601 }
1602 
1603 // FIXME: this doesn't handle recursion.
1604 static bool canReturn(Function &F) {
1605   SmallVector<BasicBlock *, 16> Worklist;
1606   SmallPtrSet<BasicBlock *, 16> Visited;
1607 
1608   Visited.insert(&F.front());
1609   Worklist.push_back(&F.front());
1610 
1611   do {
1612     BasicBlock *BB = Worklist.pop_back_val();
1613     if (basicBlockCanReturn(*BB))
1614       return true;
1615     for (BasicBlock *Succ : successors(BB))
1616       if (Visited.insert(Succ).second)
1617         Worklist.push_back(Succ);
1618   } while (!Worklist.empty());
1619 
1620   return false;
1621 }
1622 
1623 // Set the noreturn function attribute if possible.
1624 static void addNoReturnAttrs(const SCCNodeSet &SCCNodes,
1625                              SmallSet<Function *, 8> &Changed) {
1626   for (Function *F : SCCNodes) {
1627     if (!F || !F->hasExactDefinition() || F->hasFnAttribute(Attribute::Naked) ||
1628         F->doesNotReturn())
1629       continue;
1630 
1631     if (!canReturn(*F)) {
1632       F->setDoesNotReturn();
1633       Changed.insert(F);
1634     }
1635   }
1636 }
1637 
1638 static bool functionWillReturn(const Function &F) {
1639   // We can infer and propagate function attributes only when we know that the
1640   // definition we'll get at link time is *exactly* the definition we see now.
1641   // For more details, see GlobalValue::mayBeDerefined.
1642   if (!F.hasExactDefinition())
1643     return false;
1644 
1645   // Must-progress function without side-effects must return.
1646   if (F.mustProgress() && F.onlyReadsMemory())
1647     return true;
1648 
1649   // Can only analyze functions with a definition.
1650   if (F.isDeclaration())
1651     return false;
1652 
1653   // Functions with loops require more sophisticated analysis, as the loop
1654   // may be infinite. For now, don't try to handle them.
1655   SmallVector<std::pair<const BasicBlock *, const BasicBlock *>> Backedges;
1656   FindFunctionBackedges(F, Backedges);
1657   if (!Backedges.empty())
1658     return false;
1659 
1660   // If there are no loops, then the function is willreturn if all calls in
1661   // it are willreturn.
1662   return all_of(instructions(F), [](const Instruction &I) {
1663     return I.willReturn();
1664   });
1665 }
1666 
1667 // Set the willreturn function attribute if possible.
1668 static void addWillReturn(const SCCNodeSet &SCCNodes,
1669                           SmallSet<Function *, 8> &Changed) {
1670   for (Function *F : SCCNodes) {
1671     if (!F || F->willReturn() || !functionWillReturn(*F))
1672       continue;
1673 
1674     F->setWillReturn();
1675     NumWillReturn++;
1676     Changed.insert(F);
1677   }
1678 }
1679 
1680 // Return true if this is an atomic which has an ordering stronger than
1681 // unordered.  Note that this is different than the predicate we use in
1682 // Attributor.  Here we chose to be conservative and consider monotonic
1683 // operations potentially synchronizing.  We generally don't do much with
1684 // monotonic operations, so this is simply risk reduction.
1685 static bool isOrderedAtomic(Instruction *I) {
1686   if (!I->isAtomic())
1687     return false;
1688 
1689   if (auto *FI = dyn_cast<FenceInst>(I))
1690     // All legal orderings for fence are stronger than monotonic.
1691     return FI->getSyncScopeID() != SyncScope::SingleThread;
1692   else if (isa<AtomicCmpXchgInst>(I) || isa<AtomicRMWInst>(I))
1693     return true;
1694   else if (auto *SI = dyn_cast<StoreInst>(I))
1695     return !SI->isUnordered();
1696   else if (auto *LI = dyn_cast<LoadInst>(I))
1697     return !LI->isUnordered();
1698   else {
1699     llvm_unreachable("unknown atomic instruction?");
1700   }
1701 }
1702 
1703 static bool InstrBreaksNoSync(Instruction &I, const SCCNodeSet &SCCNodes) {
1704   // Volatile may synchronize
1705   if (I.isVolatile())
1706     return true;
1707 
1708   // An ordered atomic may synchronize.  (See comment about on monotonic.)
1709   if (isOrderedAtomic(&I))
1710     return true;
1711 
1712   auto *CB = dyn_cast<CallBase>(&I);
1713   if (!CB)
1714     // Non call site cases covered by the two checks above
1715     return false;
1716 
1717   if (CB->hasFnAttr(Attribute::NoSync))
1718     return false;
1719 
1720   // Non volatile memset/memcpy/memmoves are nosync
1721   // NOTE: Only intrinsics with volatile flags should be handled here.  All
1722   // others should be marked in Intrinsics.td.
1723   if (auto *MI = dyn_cast<MemIntrinsic>(&I))
1724     if (!MI->isVolatile())
1725       return false;
1726 
1727   // Speculatively assume in SCC.
1728   if (Function *Callee = CB->getCalledFunction())
1729     if (SCCNodes.contains(Callee))
1730       return false;
1731 
1732   return true;
1733 }
1734 
1735 // Infer the nosync attribute.
1736 static void addNoSyncAttr(const SCCNodeSet &SCCNodes,
1737                           SmallSet<Function *, 8> &Changed) {
1738   AttributeInferer AI;
1739   AI.registerAttrInference(AttributeInferer::InferenceDescriptor{
1740       Attribute::NoSync,
1741       // Skip already marked functions.
1742       [](const Function &F) { return F.hasNoSync(); },
1743       // Instructions that break nosync assumption.
1744       [&SCCNodes](Instruction &I) {
1745         return InstrBreaksNoSync(I, SCCNodes);
1746       },
1747       [](Function &F) {
1748         LLVM_DEBUG(dbgs()
1749                    << "Adding nosync attr to fn " << F.getName() << "\n");
1750         F.setNoSync();
1751         ++NumNoSync;
1752       },
1753       /* RequiresExactDefinition= */ true});
1754   AI.run(SCCNodes, Changed);
1755 }
1756 
1757 static SCCNodesResult createSCCNodeSet(ArrayRef<Function *> Functions) {
1758   SCCNodesResult Res;
1759   Res.HasUnknownCall = false;
1760   for (Function *F : Functions) {
1761     if (!F || F->hasOptNone() || F->hasFnAttribute(Attribute::Naked) ||
1762         F->isPresplitCoroutine()) {
1763       // Treat any function we're trying not to optimize as if it were an
1764       // indirect call and omit it from the node set used below.
1765       Res.HasUnknownCall = true;
1766       continue;
1767     }
1768     // Track whether any functions in this SCC have an unknown call edge.
1769     // Note: if this is ever a performance hit, we can common it with
1770     // subsequent routines which also do scans over the instructions of the
1771     // function.
1772     if (!Res.HasUnknownCall) {
1773       for (Instruction &I : instructions(*F)) {
1774         if (auto *CB = dyn_cast<CallBase>(&I)) {
1775           if (!CB->getCalledFunction()) {
1776             Res.HasUnknownCall = true;
1777             break;
1778           }
1779         }
1780       }
1781     }
1782     Res.SCCNodes.insert(F);
1783   }
1784   return Res;
1785 }
1786 
1787 template <typename AARGetterT>
1788 static SmallSet<Function *, 8>
1789 deriveAttrsInPostOrder(ArrayRef<Function *> Functions, AARGetterT &&AARGetter) {
1790   SCCNodesResult Nodes = createSCCNodeSet(Functions);
1791 
1792   // Bail if the SCC only contains optnone functions.
1793   if (Nodes.SCCNodes.empty())
1794     return {};
1795 
1796   SmallSet<Function *, 8> Changed;
1797 
1798   addArgumentReturnedAttrs(Nodes.SCCNodes, Changed);
1799   addMemoryAttrs(Nodes.SCCNodes, AARGetter, Changed);
1800   addArgumentAttrs(Nodes.SCCNodes, Changed);
1801   inferConvergent(Nodes.SCCNodes, Changed);
1802   addNoReturnAttrs(Nodes.SCCNodes, Changed);
1803   addWillReturn(Nodes.SCCNodes, Changed);
1804 
1805   // If we have no external nodes participating in the SCC, we can deduce some
1806   // more precise attributes as well.
1807   if (!Nodes.HasUnknownCall) {
1808     addNoAliasAttrs(Nodes.SCCNodes, Changed);
1809     addNonNullAttrs(Nodes.SCCNodes, Changed);
1810     inferAttrsFromFunctionBodies(Nodes.SCCNodes, Changed);
1811     addNoRecurseAttrs(Nodes.SCCNodes, Changed);
1812   }
1813 
1814   addNoSyncAttr(Nodes.SCCNodes, Changed);
1815 
1816   // Finally, infer the maximal set of attributes from the ones we've inferred
1817   // above.  This is handling the cases where one attribute on a signature
1818   // implies another, but for implementation reasons the inference rule for
1819   // the later is missing (or simply less sophisticated).
1820   for (Function *F : Nodes.SCCNodes)
1821     if (F)
1822       if (inferAttributesFromOthers(*F))
1823         Changed.insert(F);
1824 
1825   return Changed;
1826 }
1827 
1828 PreservedAnalyses PostOrderFunctionAttrsPass::run(LazyCallGraph::SCC &C,
1829                                                   CGSCCAnalysisManager &AM,
1830                                                   LazyCallGraph &CG,
1831                                                   CGSCCUpdateResult &) {
1832   FunctionAnalysisManager &FAM =
1833       AM.getResult<FunctionAnalysisManagerCGSCCProxy>(C, CG).getManager();
1834 
1835   // We pass a lambda into functions to wire them up to the analysis manager
1836   // for getting function analyses.
1837   auto AARGetter = [&](Function &F) -> AAResults & {
1838     return FAM.getResult<AAManager>(F);
1839   };
1840 
1841   SmallVector<Function *, 8> Functions;
1842   for (LazyCallGraph::Node &N : C) {
1843     Functions.push_back(&N.getFunction());
1844   }
1845 
1846   auto ChangedFunctions = deriveAttrsInPostOrder(Functions, AARGetter);
1847   if (ChangedFunctions.empty())
1848     return PreservedAnalyses::all();
1849 
1850   // Invalidate analyses for modified functions so that we don't have to
1851   // invalidate all analyses for all functions in this SCC.
1852   PreservedAnalyses FuncPA;
1853   // We haven't changed the CFG for modified functions.
1854   FuncPA.preserveSet<CFGAnalyses>();
1855   for (Function *Changed : ChangedFunctions) {
1856     FAM.invalidate(*Changed, FuncPA);
1857     // Also invalidate any direct callers of changed functions since analyses
1858     // may care about attributes of direct callees. For example, MemorySSA cares
1859     // about whether or not a call's callee modifies memory and queries that
1860     // through function attributes.
1861     for (auto *U : Changed->users()) {
1862       if (auto *Call = dyn_cast<CallBase>(U)) {
1863         if (Call->getCalledFunction() == Changed)
1864           FAM.invalidate(*Call->getFunction(), FuncPA);
1865       }
1866     }
1867   }
1868 
1869   PreservedAnalyses PA;
1870   // We have not added or removed functions.
1871   PA.preserve<FunctionAnalysisManagerCGSCCProxy>();
1872   // We already invalidated all relevant function analyses above.
1873   PA.preserveSet<AllAnalysesOn<Function>>();
1874   return PA;
1875 }
1876 
1877 namespace {
1878 
1879 struct PostOrderFunctionAttrsLegacyPass : public CallGraphSCCPass {
1880   // Pass identification, replacement for typeid
1881   static char ID;
1882 
1883   PostOrderFunctionAttrsLegacyPass() : CallGraphSCCPass(ID) {
1884     initializePostOrderFunctionAttrsLegacyPassPass(
1885         *PassRegistry::getPassRegistry());
1886   }
1887 
1888   bool runOnSCC(CallGraphSCC &SCC) override;
1889 
1890   void getAnalysisUsage(AnalysisUsage &AU) const override {
1891     AU.setPreservesCFG();
1892     AU.addRequired<AssumptionCacheTracker>();
1893     getAAResultsAnalysisUsage(AU);
1894     CallGraphSCCPass::getAnalysisUsage(AU);
1895   }
1896 };
1897 
1898 } // end anonymous namespace
1899 
1900 char PostOrderFunctionAttrsLegacyPass::ID = 0;
1901 INITIALIZE_PASS_BEGIN(PostOrderFunctionAttrsLegacyPass, "function-attrs",
1902                       "Deduce function attributes", false, false)
1903 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
1904 INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
1905 INITIALIZE_PASS_END(PostOrderFunctionAttrsLegacyPass, "function-attrs",
1906                     "Deduce function attributes", false, false)
1907 
1908 Pass *llvm::createPostOrderFunctionAttrsLegacyPass() {
1909   return new PostOrderFunctionAttrsLegacyPass();
1910 }
1911 
1912 template <typename AARGetterT>
1913 static bool runImpl(CallGraphSCC &SCC, AARGetterT AARGetter) {
1914   SmallVector<Function *, 8> Functions;
1915   for (CallGraphNode *I : SCC) {
1916     Functions.push_back(I->getFunction());
1917   }
1918 
1919   return !deriveAttrsInPostOrder(Functions, AARGetter).empty();
1920 }
1921 
1922 bool PostOrderFunctionAttrsLegacyPass::runOnSCC(CallGraphSCC &SCC) {
1923   if (skipSCC(SCC))
1924     return false;
1925   return runImpl(SCC, LegacyAARGetter(*this));
1926 }
1927 
1928 namespace {
1929 
1930 struct ReversePostOrderFunctionAttrsLegacyPass : public ModulePass {
1931   // Pass identification, replacement for typeid
1932   static char ID;
1933 
1934   ReversePostOrderFunctionAttrsLegacyPass() : ModulePass(ID) {
1935     initializeReversePostOrderFunctionAttrsLegacyPassPass(
1936         *PassRegistry::getPassRegistry());
1937   }
1938 
1939   bool runOnModule(Module &M) override;
1940 
1941   void getAnalysisUsage(AnalysisUsage &AU) const override {
1942     AU.setPreservesCFG();
1943     AU.addRequired<CallGraphWrapperPass>();
1944     AU.addPreserved<CallGraphWrapperPass>();
1945   }
1946 };
1947 
1948 } // end anonymous namespace
1949 
1950 char ReversePostOrderFunctionAttrsLegacyPass::ID = 0;
1951 
1952 INITIALIZE_PASS_BEGIN(ReversePostOrderFunctionAttrsLegacyPass,
1953                       "rpo-function-attrs", "Deduce function attributes in RPO",
1954                       false, false)
1955 INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
1956 INITIALIZE_PASS_END(ReversePostOrderFunctionAttrsLegacyPass,
1957                     "rpo-function-attrs", "Deduce function attributes in RPO",
1958                     false, false)
1959 
1960 Pass *llvm::createReversePostOrderFunctionAttrsPass() {
1961   return new ReversePostOrderFunctionAttrsLegacyPass();
1962 }
1963 
1964 static bool addNoRecurseAttrsTopDown(Function &F) {
1965   // We check the preconditions for the function prior to calling this to avoid
1966   // the cost of building up a reversible post-order list. We assert them here
1967   // to make sure none of the invariants this relies on were violated.
1968   assert(!F.isDeclaration() && "Cannot deduce norecurse without a definition!");
1969   assert(!F.doesNotRecurse() &&
1970          "This function has already been deduced as norecurs!");
1971   assert(F.hasInternalLinkage() &&
1972          "Can only do top-down deduction for internal linkage functions!");
1973 
1974   // If F is internal and all of its uses are calls from a non-recursive
1975   // functions, then none of its calls could in fact recurse without going
1976   // through a function marked norecurse, and so we can mark this function too
1977   // as norecurse. Note that the uses must actually be calls -- otherwise
1978   // a pointer to this function could be returned from a norecurse function but
1979   // this function could be recursively (indirectly) called. Note that this
1980   // also detects if F is directly recursive as F is not yet marked as
1981   // a norecurse function.
1982   for (auto *U : F.users()) {
1983     auto *I = dyn_cast<Instruction>(U);
1984     if (!I)
1985       return false;
1986     CallBase *CB = dyn_cast<CallBase>(I);
1987     if (!CB || !CB->getParent()->getParent()->doesNotRecurse())
1988       return false;
1989   }
1990   F.setDoesNotRecurse();
1991   ++NumNoRecurse;
1992   return true;
1993 }
1994 
1995 static bool deduceFunctionAttributeInRPO(Module &M, CallGraph &CG) {
1996   // We only have a post-order SCC traversal (because SCCs are inherently
1997   // discovered in post-order), so we accumulate them in a vector and then walk
1998   // it in reverse. This is simpler than using the RPO iterator infrastructure
1999   // because we need to combine SCC detection and the PO walk of the call
2000   // graph. We can also cheat egregiously because we're primarily interested in
2001   // synthesizing norecurse and so we can only save the singular SCCs as SCCs
2002   // with multiple functions in them will clearly be recursive.
2003   SmallVector<Function *, 16> Worklist;
2004   for (scc_iterator<CallGraph *> I = scc_begin(&CG); !I.isAtEnd(); ++I) {
2005     if (I->size() != 1)
2006       continue;
2007 
2008     Function *F = I->front()->getFunction();
2009     if (F && !F->isDeclaration() && !F->doesNotRecurse() &&
2010         F->hasInternalLinkage())
2011       Worklist.push_back(F);
2012   }
2013 
2014   bool Changed = false;
2015   for (auto *F : llvm::reverse(Worklist))
2016     Changed |= addNoRecurseAttrsTopDown(*F);
2017 
2018   return Changed;
2019 }
2020 
2021 bool ReversePostOrderFunctionAttrsLegacyPass::runOnModule(Module &M) {
2022   if (skipModule(M))
2023     return false;
2024 
2025   auto &CG = getAnalysis<CallGraphWrapperPass>().getCallGraph();
2026 
2027   return deduceFunctionAttributeInRPO(M, CG);
2028 }
2029 
2030 PreservedAnalyses
2031 ReversePostOrderFunctionAttrsPass::run(Module &M, ModuleAnalysisManager &AM) {
2032   auto &CG = AM.getResult<CallGraphAnalysis>(M);
2033 
2034   if (!deduceFunctionAttributeInRPO(M, CG))
2035     return PreservedAnalyses::all();
2036 
2037   PreservedAnalyses PA;
2038   PA.preserve<CallGraphAnalysis>();
2039   return PA;
2040 }
2041