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