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(NumNoAlias, "Number of function returns marked noalias");
80 STATISTIC(NumNonNullReturn, "Number of function returns marked nonnull");
81 STATISTIC(NumNoRecurse, "Number of functions marked as norecurse");
82 STATISTIC(NumNoUnwind, "Number of functions marked as nounwind");
83 STATISTIC(NumNoFree, "Number of functions marked as nofree");
84 STATISTIC(NumWillReturn, "Number of functions marked as willreturn");
85 STATISTIC(NumNoSync, "Number of functions marked as nosync");
86 
87 STATISTIC(NumThinLinkNoRecurse,
88           "Number of functions marked as norecurse during thinlink");
89 STATISTIC(NumThinLinkNoUnwind,
90           "Number of functions marked as nounwind during thinlink");
91 
92 static cl::opt<bool> EnableNonnullArgPropagation(
93     "enable-nonnull-arg-prop", cl::init(true), cl::Hidden,
94     cl::desc("Try to propagate nonnull argument attributes from callsites to "
95              "caller functions."));
96 
97 static cl::opt<bool> DisableNoUnwindInference(
98     "disable-nounwind-inference", cl::Hidden,
99     cl::desc("Stop inferring nounwind attribute during function-attrs pass"));
100 
101 static cl::opt<bool> DisableNoFreeInference(
102     "disable-nofree-inference", cl::Hidden,
103     cl::desc("Stop inferring nofree attribute during function-attrs pass"));
104 
105 static cl::opt<bool> DisableThinLTOPropagation(
106     "disable-thinlto-funcattrs", cl::init(true), cl::Hidden,
107     cl::desc("Don't propagate function-attrs in thinLTO"));
108 
109 namespace {
110 
111 using SCCNodeSet = SmallSetVector<Function *, 8>;
112 
113 } // end anonymous namespace
114 
115 /// Returns the memory access attribute for function F using AAR for AA results,
116 /// where SCCNodes is the current SCC.
117 ///
118 /// If ThisBody is true, this function may examine the function body and will
119 /// return a result pertaining to this copy of the function. If it is false, the
120 /// result will be based only on AA results for the function declaration; it
121 /// will be assumed that some other (perhaps less optimized) version of the
122 /// function may be selected at link time.
123 static MemoryAccessKind checkFunctionMemoryAccess(Function &F, bool ThisBody,
124                                                   AAResults &AAR,
125                                                   const SCCNodeSet &SCCNodes) {
126   FunctionModRefBehavior MRB = AAR.getModRefBehavior(&F);
127   if (MRB == FMRB_DoesNotAccessMemory)
128     // Already perfect!
129     return MAK_ReadNone;
130 
131   if (!ThisBody) {
132     if (AliasAnalysis::onlyReadsMemory(MRB))
133       return MAK_ReadOnly;
134 
135     if (AliasAnalysis::doesNotReadMemory(MRB))
136       return MAK_WriteOnly;
137 
138     // Conservatively assume it reads and writes to memory.
139     return MAK_MayWrite;
140   }
141 
142   // Scan the function body for instructions that may read or write memory.
143   bool ReadsMemory = false;
144   bool WritesMemory = false;
145   for (inst_iterator II = inst_begin(F), E = inst_end(F); II != E; ++II) {
146     Instruction *I = &*II;
147 
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         continue;
180       }
181 
182       // Check whether all pointer arguments point to local memory, and
183       // ignore calls that only access local memory.
184       for (auto CI = Call->arg_begin(), CE = Call->arg_end(); CI != CE; ++CI) {
185         Value *Arg = *CI;
186         if (!Arg->getType()->isPtrOrPtrVectorTy())
187           continue;
188 
189         MemoryLocation Loc =
190             MemoryLocation::getBeforeOrAfter(Arg, I->getAAMetadata());
191 
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         if (isModSet(MRI))
198           // Writes non-local memory.
199           WritesMemory = true;
200         if (isRefSet(MRI))
201           // Ok, it reads non-local memory.
202           ReadsMemory = true;
203       }
204       continue;
205     } else if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
206       // Ignore non-volatile loads from local memory. (Atomic is okay here.)
207       if (!LI->isVolatile()) {
208         MemoryLocation Loc = MemoryLocation::get(LI);
209         if (AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true))
210           continue;
211       }
212     } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
213       // Ignore non-volatile stores to local memory. (Atomic is okay here.)
214       if (!SI->isVolatile()) {
215         MemoryLocation Loc = MemoryLocation::get(SI);
216         if (AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true))
217           continue;
218       }
219     } else if (VAArgInst *VI = dyn_cast<VAArgInst>(I)) {
220       // Ignore vaargs on local memory.
221       MemoryLocation Loc = MemoryLocation::get(VI);
222       if (AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true))
223         continue;
224     }
225 
226     // Any remaining instructions need to be taken seriously!  Check if they
227     // read or write memory.
228     //
229     // Writes memory, remember that.
230     WritesMemory |= I->mayWriteToMemory();
231 
232     // If this instruction may read memory, remember that.
233     ReadsMemory |= I->mayReadFromMemory();
234   }
235 
236   if (WritesMemory) {
237     if (!ReadsMemory)
238       return MAK_WriteOnly;
239     else
240       return MAK_MayWrite;
241   }
242 
243   return ReadsMemory ? MAK_ReadOnly : MAK_ReadNone;
244 }
245 
246 MemoryAccessKind llvm::computeFunctionBodyMemoryAccess(Function &F,
247                                                        AAResults &AAR) {
248   return checkFunctionMemoryAccess(F, /*ThisBody=*/true, AAR, {});
249 }
250 
251 /// Deduce readonly/readnone attributes for the SCC.
252 template <typename AARGetterT>
253 static void addReadAttrs(const SCCNodeSet &SCCNodes, AARGetterT &&AARGetter,
254                          SmallSet<Function *, 8> &Changed) {
255   // Check if any of the functions in the SCC read or write memory.  If they
256   // write memory then they can't be marked readnone or readonly.
257   bool ReadsMemory = false;
258   bool WritesMemory = false;
259   for (Function *F : SCCNodes) {
260     // Call the callable parameter to look up AA results for this function.
261     AAResults &AAR = AARGetter(*F);
262 
263     // Non-exact function definitions may not be selected at link time, and an
264     // alternative version that writes to memory may be selected.  See the
265     // comment on GlobalValue::isDefinitionExact for more details.
266     switch (checkFunctionMemoryAccess(*F, F->hasExactDefinition(),
267                                       AAR, SCCNodes)) {
268     case MAK_MayWrite:
269       return;
270     case MAK_ReadOnly:
271       ReadsMemory = true;
272       break;
273     case MAK_WriteOnly:
274       WritesMemory = true;
275       break;
276     case MAK_ReadNone:
277       // Nothing to do!
278       break;
279     }
280   }
281 
282   // If the SCC contains both functions that read and functions that write, then
283   // we cannot add readonly attributes.
284   if (ReadsMemory && WritesMemory)
285     return;
286 
287   // Success!  Functions in this SCC do not access memory, or only read memory.
288   // Give them the appropriate attribute.
289 
290   for (Function *F : SCCNodes) {
291     if (F->doesNotAccessMemory())
292       // Already perfect!
293       continue;
294 
295     if (F->onlyReadsMemory() && ReadsMemory)
296       // No change.
297       continue;
298 
299     if (F->doesNotReadMemory() && WritesMemory)
300       continue;
301 
302     Changed.insert(F);
303 
304     // Clear out any existing attributes.
305     AttrBuilder AttrsToRemove;
306     AttrsToRemove.addAttribute(Attribute::ReadOnly);
307     AttrsToRemove.addAttribute(Attribute::ReadNone);
308     AttrsToRemove.addAttribute(Attribute::WriteOnly);
309 
310     if (!WritesMemory && !ReadsMemory) {
311       // Clear out any "access range attributes" if readnone was deduced.
312       AttrsToRemove.addAttribute(Attribute::ArgMemOnly);
313       AttrsToRemove.addAttribute(Attribute::InaccessibleMemOnly);
314       AttrsToRemove.addAttribute(Attribute::InaccessibleMemOrArgMemOnly);
315     }
316     F->removeFnAttrs(AttrsToRemove);
317 
318     // Add in the new attribute.
319     if (WritesMemory && !ReadsMemory)
320       F->addFnAttr(Attribute::WriteOnly);
321     else
322       F->addFnAttr(ReadsMemory ? Attribute::ReadOnly : Attribute::ReadNone);
323 
324     if (WritesMemory && !ReadsMemory)
325       ++NumWriteOnly;
326     else if (ReadsMemory)
327       ++NumReadOnly;
328     else
329       ++NumReadNone;
330   }
331 }
332 
333 // Compute definitive function attributes for a function taking into account
334 // prevailing definitions and linkage types
335 static FunctionSummary *calculatePrevailingSummary(
336     ValueInfo VI,
337     DenseMap<ValueInfo, FunctionSummary *> &CachedPrevailingSummary,
338     function_ref<bool(GlobalValue::GUID, const GlobalValueSummary *)>
339         IsPrevailing) {
340 
341   if (CachedPrevailingSummary.count(VI))
342     return CachedPrevailingSummary[VI];
343 
344   /// At this point, prevailing symbols have been resolved. The following leads
345   /// to returning a conservative result:
346   /// - Multiple instances with local linkage. Normally local linkage would be
347   ///   unique per module
348   ///   as the GUID includes the module path. We could have a guid alias if
349   ///   there wasn't any distinguishing path when each file was compiled, but
350   ///   that should be rare so we'll punt on those.
351 
352   /// These next 2 cases should not happen and will assert:
353   /// - Multiple instances with external linkage. This should be caught in
354   ///   symbol resolution
355   /// - Non-existent FunctionSummary for Aliasee. This presents a hole in our
356   ///   knowledge meaning we have to go conservative.
357 
358   /// Otherwise, we calculate attributes for a function as:
359   ///   1. If we have a local linkage, take its attributes. If there's somehow
360   ///      multiple, bail and go conservative.
361   ///   2. If we have an external/WeakODR/LinkOnceODR linkage check that it is
362   ///      prevailing, take its attributes.
363   ///   3. If we have a Weak/LinkOnce linkage the copies can have semantic
364   ///      differences. However, if the prevailing copy is known it will be used
365   ///      so take its attributes. If the prevailing copy is in a native file
366   ///      all IR copies will be dead and propagation will go conservative.
367   ///   4. AvailableExternally summaries without a prevailing copy are known to
368   ///      occur in a couple of circumstances:
369   ///      a. An internal function gets imported due to its caller getting
370   ///         imported, it becomes AvailableExternally but no prevailing
371   ///         definition exists. Because it has to get imported along with its
372   ///         caller the attributes will be captured by propagating on its
373   ///         caller.
374   ///      b. C++11 [temp.explicit]p10 can generate AvailableExternally
375   ///         definitions of explicitly instanced template declarations
376   ///         for inlining which are ultimately dropped from the TU. Since this
377   ///         is localized to the TU the attributes will have already made it to
378   ///         the callers.
379   ///      These are edge cases and already captured by their callers so we
380   ///      ignore these for now. If they become relevant to optimize in the
381   ///      future this can be revisited.
382   ///   5. Otherwise, go conservative.
383 
384   CachedPrevailingSummary[VI] = nullptr;
385   FunctionSummary *Local = nullptr;
386   FunctionSummary *Prevailing = nullptr;
387 
388   for (const auto &GVS : VI.getSummaryList()) {
389     if (!GVS->isLive())
390       continue;
391 
392     FunctionSummary *FS = dyn_cast<FunctionSummary>(GVS->getBaseObject());
393     // Virtual and Unknown (e.g. indirect) calls require going conservative
394     if (!FS || FS->fflags().HasUnknownCall)
395       return nullptr;
396 
397     const auto &Linkage = GVS->linkage();
398     if (GlobalValue::isLocalLinkage(Linkage)) {
399       if (Local) {
400         LLVM_DEBUG(
401             dbgs()
402             << "ThinLTO FunctionAttrs: Multiple Local Linkage, bailing on "
403                "function "
404             << VI.name() << " from " << FS->modulePath() << ". Previous module "
405             << Local->modulePath() << "\n");
406         return nullptr;
407       }
408       Local = FS;
409     } else if (GlobalValue::isExternalLinkage(Linkage)) {
410       assert(IsPrevailing(VI.getGUID(), GVS.get()));
411       Prevailing = FS;
412       break;
413     } else if (GlobalValue::isWeakODRLinkage(Linkage) ||
414                GlobalValue::isLinkOnceODRLinkage(Linkage) ||
415                GlobalValue::isWeakAnyLinkage(Linkage) ||
416                GlobalValue::isLinkOnceAnyLinkage(Linkage)) {
417       if (IsPrevailing(VI.getGUID(), GVS.get())) {
418         Prevailing = FS;
419         break;
420       }
421     } else if (GlobalValue::isAvailableExternallyLinkage(Linkage)) {
422       // TODO: Handle these cases if they become meaningful
423       continue;
424     }
425   }
426 
427   if (Local) {
428     assert(!Prevailing);
429     CachedPrevailingSummary[VI] = Local;
430   } else if (Prevailing) {
431     assert(!Local);
432     CachedPrevailingSummary[VI] = Prevailing;
433   }
434 
435   return CachedPrevailingSummary[VI];
436 }
437 
438 bool llvm::thinLTOPropagateFunctionAttrs(
439     ModuleSummaryIndex &Index,
440     function_ref<bool(GlobalValue::GUID, const GlobalValueSummary *)>
441         IsPrevailing) {
442   // TODO: implement addNoAliasAttrs once
443   // there's more information about the return type in the summary
444   if (DisableThinLTOPropagation)
445     return false;
446 
447   DenseMap<ValueInfo, FunctionSummary *> CachedPrevailingSummary;
448   bool Changed = false;
449 
450   auto PropagateAttributes = [&](std::vector<ValueInfo> &SCCNodes) {
451     // Assume we can propagate unless we discover otherwise
452     FunctionSummary::FFlags InferredFlags;
453     InferredFlags.NoRecurse = (SCCNodes.size() == 1);
454     InferredFlags.NoUnwind = true;
455 
456     for (auto &V : SCCNodes) {
457       FunctionSummary *CallerSummary =
458           calculatePrevailingSummary(V, CachedPrevailingSummary, IsPrevailing);
459 
460       // Function summaries can fail to contain information such as declarations
461       if (!CallerSummary)
462         return;
463 
464       if (CallerSummary->fflags().MayThrow)
465         InferredFlags.NoUnwind = false;
466 
467       for (const auto &Callee : CallerSummary->calls()) {
468         FunctionSummary *CalleeSummary = calculatePrevailingSummary(
469             Callee.first, CachedPrevailingSummary, IsPrevailing);
470 
471         if (!CalleeSummary)
472           return;
473 
474         if (!CalleeSummary->fflags().NoRecurse)
475           InferredFlags.NoRecurse = false;
476 
477         if (!CalleeSummary->fflags().NoUnwind)
478           InferredFlags.NoUnwind = false;
479 
480         if (!InferredFlags.NoUnwind && !InferredFlags.NoRecurse)
481           break;
482       }
483     }
484 
485     if (InferredFlags.NoUnwind || InferredFlags.NoRecurse) {
486       Changed = true;
487       for (auto &V : SCCNodes) {
488         if (InferredFlags.NoRecurse) {
489           LLVM_DEBUG(dbgs() << "ThinLTO FunctionAttrs: Propagated NoRecurse to "
490                             << V.name() << "\n");
491           ++NumThinLinkNoRecurse;
492         }
493 
494         if (InferredFlags.NoUnwind) {
495           LLVM_DEBUG(dbgs() << "ThinLTO FunctionAttrs: Propagated NoUnwind to "
496                             << V.name() << "\n");
497           ++NumThinLinkNoUnwind;
498         }
499 
500         for (auto &S : V.getSummaryList()) {
501           if (auto *FS = dyn_cast<FunctionSummary>(S.get())) {
502             if (InferredFlags.NoRecurse)
503               FS->setNoRecurse();
504 
505             if (InferredFlags.NoUnwind)
506               FS->setNoUnwind();
507           }
508         }
509       }
510     }
511   };
512 
513   // Call propagation functions on each SCC in the Index
514   for (scc_iterator<ModuleSummaryIndex *> I = scc_begin(&Index); !I.isAtEnd();
515        ++I) {
516     std::vector<ValueInfo> Nodes(*I);
517     PropagateAttributes(Nodes);
518   }
519   return Changed;
520 }
521 
522 namespace {
523 
524 /// For a given pointer Argument, this retains a list of Arguments of functions
525 /// in the same SCC that the pointer data flows into. We use this to build an
526 /// SCC of the arguments.
527 struct ArgumentGraphNode {
528   Argument *Definition;
529   SmallVector<ArgumentGraphNode *, 4> Uses;
530 };
531 
532 class ArgumentGraph {
533   // We store pointers to ArgumentGraphNode objects, so it's important that
534   // that they not move around upon insert.
535   using ArgumentMapTy = std::map<Argument *, ArgumentGraphNode>;
536 
537   ArgumentMapTy ArgumentMap;
538 
539   // There is no root node for the argument graph, in fact:
540   //   void f(int *x, int *y) { if (...) f(x, y); }
541   // is an example where the graph is disconnected. The SCCIterator requires a
542   // single entry point, so we maintain a fake ("synthetic") root node that
543   // uses every node. Because the graph is directed and nothing points into
544   // the root, it will not participate in any SCCs (except for its own).
545   ArgumentGraphNode SyntheticRoot;
546 
547 public:
548   ArgumentGraph() { SyntheticRoot.Definition = nullptr; }
549 
550   using iterator = SmallVectorImpl<ArgumentGraphNode *>::iterator;
551 
552   iterator begin() { return SyntheticRoot.Uses.begin(); }
553   iterator end() { return SyntheticRoot.Uses.end(); }
554   ArgumentGraphNode *getEntryNode() { return &SyntheticRoot; }
555 
556   ArgumentGraphNode *operator[](Argument *A) {
557     ArgumentGraphNode &Node = ArgumentMap[A];
558     Node.Definition = A;
559     SyntheticRoot.Uses.push_back(&Node);
560     return &Node;
561   }
562 };
563 
564 /// This tracker checks whether callees are in the SCC, and if so it does not
565 /// consider that a capture, instead adding it to the "Uses" list and
566 /// continuing with the analysis.
567 struct ArgumentUsesTracker : public CaptureTracker {
568   ArgumentUsesTracker(const SCCNodeSet &SCCNodes) : SCCNodes(SCCNodes) {}
569 
570   void tooManyUses() override { Captured = true; }
571 
572   bool captured(const Use *U) override {
573     CallBase *CB = dyn_cast<CallBase>(U->getUser());
574     if (!CB) {
575       Captured = true;
576       return true;
577     }
578 
579     Function *F = CB->getCalledFunction();
580     if (!F || !F->hasExactDefinition() || !SCCNodes.count(F)) {
581       Captured = true;
582       return true;
583     }
584 
585     // Note: the callee and the two successor blocks *follow* the argument
586     // operands.  This means there is no need to adjust UseIndex to account for
587     // these.
588 
589     unsigned UseIndex =
590         std::distance(const_cast<const Use *>(CB->arg_begin()), U);
591 
592     assert(UseIndex < CB->data_operands_size() &&
593            "Indirect function calls should have been filtered above!");
594 
595     if (UseIndex >= CB->arg_size()) {
596       // Data operand, but not a argument operand -- must be a bundle operand
597       assert(CB->hasOperandBundles() && "Must be!");
598 
599       // CaptureTracking told us that we're being captured by an operand bundle
600       // use.  In this case it does not matter if the callee is within our SCC
601       // or not -- we've been captured in some unknown way, and we have to be
602       // conservative.
603       Captured = true;
604       return true;
605     }
606 
607     if (UseIndex >= F->arg_size()) {
608       assert(F->isVarArg() && "More params than args in non-varargs call");
609       Captured = true;
610       return true;
611     }
612 
613     Uses.push_back(&*std::next(F->arg_begin(), UseIndex));
614     return false;
615   }
616 
617   // True only if certainly captured (used outside our SCC).
618   bool Captured = false;
619 
620   // Uses within our SCC.
621   SmallVector<Argument *, 4> Uses;
622 
623   const SCCNodeSet &SCCNodes;
624 };
625 
626 } // end anonymous namespace
627 
628 namespace llvm {
629 
630 template <> struct GraphTraits<ArgumentGraphNode *> {
631   using NodeRef = ArgumentGraphNode *;
632   using ChildIteratorType = SmallVectorImpl<ArgumentGraphNode *>::iterator;
633 
634   static NodeRef getEntryNode(NodeRef A) { return A; }
635   static ChildIteratorType child_begin(NodeRef N) { return N->Uses.begin(); }
636   static ChildIteratorType child_end(NodeRef N) { return N->Uses.end(); }
637 };
638 
639 template <>
640 struct GraphTraits<ArgumentGraph *> : public GraphTraits<ArgumentGraphNode *> {
641   static NodeRef getEntryNode(ArgumentGraph *AG) { return AG->getEntryNode(); }
642 
643   static ChildIteratorType nodes_begin(ArgumentGraph *AG) {
644     return AG->begin();
645   }
646 
647   static ChildIteratorType nodes_end(ArgumentGraph *AG) { return AG->end(); }
648 };
649 
650 } // end namespace llvm
651 
652 /// Returns Attribute::None, Attribute::ReadOnly or Attribute::ReadNone.
653 static Attribute::AttrKind
654 determinePointerReadAttrs(Argument *A,
655                           const SmallPtrSet<Argument *, 8> &SCCNodes) {
656   SmallVector<Use *, 32> Worklist;
657   SmallPtrSet<Use *, 32> Visited;
658 
659   // inalloca arguments are always clobbered by the call.
660   if (A->hasInAllocaAttr() || A->hasPreallocatedAttr())
661     return Attribute::None;
662 
663   bool IsRead = false;
664   // We don't need to track IsWritten. If A is written to, return immediately.
665 
666   for (Use &U : A->uses()) {
667     Visited.insert(&U);
668     Worklist.push_back(&U);
669   }
670 
671   while (!Worklist.empty()) {
672     Use *U = Worklist.pop_back_val();
673     Instruction *I = cast<Instruction>(U->getUser());
674 
675     switch (I->getOpcode()) {
676     case Instruction::BitCast:
677     case Instruction::GetElementPtr:
678     case Instruction::PHI:
679     case Instruction::Select:
680     case Instruction::AddrSpaceCast:
681       // The original value is not read/written via this if the new value isn't.
682       for (Use &UU : I->uses())
683         if (Visited.insert(&UU).second)
684           Worklist.push_back(&UU);
685       break;
686 
687     case Instruction::Call:
688     case Instruction::Invoke: {
689       bool Captures = true;
690 
691       if (I->getType()->isVoidTy())
692         Captures = false;
693 
694       auto AddUsersToWorklistIfCapturing = [&] {
695         if (Captures)
696           for (Use &UU : I->uses())
697             if (Visited.insert(&UU).second)
698               Worklist.push_back(&UU);
699       };
700 
701       CallBase &CB = cast<CallBase>(*I);
702       if (CB.doesNotAccessMemory()) {
703         AddUsersToWorklistIfCapturing();
704         continue;
705       }
706 
707       Function *F = CB.getCalledFunction();
708       if (!F) {
709         if (CB.onlyReadsMemory()) {
710           IsRead = true;
711           AddUsersToWorklistIfCapturing();
712           continue;
713         }
714         return Attribute::None;
715       }
716 
717       // Note: the callee and the two successor blocks *follow* the argument
718       // operands.  This means there is no need to adjust UseIndex to account
719       // for these.
720 
721       unsigned UseIndex = std::distance(CB.arg_begin(), U);
722 
723       // U cannot be the callee operand use: since we're exploring the
724       // transitive uses of an Argument, having such a use be a callee would
725       // imply the call site is an indirect call or invoke; and we'd take the
726       // early exit above.
727       assert(UseIndex < CB.data_operands_size() &&
728              "Data operand use expected!");
729 
730       bool IsOperandBundleUse = UseIndex >= CB.arg_size();
731 
732       if (UseIndex >= F->arg_size() && !IsOperandBundleUse) {
733         assert(F->isVarArg() && "More params than args in non-varargs call");
734         return Attribute::None;
735       }
736 
737       Captures &= !CB.doesNotCapture(UseIndex);
738 
739       // Since the optimizer (by design) cannot see the data flow corresponding
740       // to a operand bundle use, these cannot participate in the optimistic SCC
741       // analysis.  Instead, we model the operand bundle uses as arguments in
742       // call to a function external to the SCC.
743       if (IsOperandBundleUse ||
744           !SCCNodes.count(&*std::next(F->arg_begin(), UseIndex))) {
745 
746         // The accessors used on call site here do the right thing for calls and
747         // invokes with operand bundles.
748 
749         if (!CB.onlyReadsMemory() && !CB.onlyReadsMemory(UseIndex))
750           return Attribute::None;
751         if (!CB.doesNotAccessMemory(UseIndex))
752           IsRead = true;
753       }
754 
755       AddUsersToWorklistIfCapturing();
756       break;
757     }
758 
759     case Instruction::Load:
760       // A volatile load has side effects beyond what readonly can be relied
761       // upon.
762       if (cast<LoadInst>(I)->isVolatile())
763         return Attribute::None;
764 
765       IsRead = true;
766       break;
767 
768     case Instruction::ICmp:
769     case Instruction::Ret:
770       break;
771 
772     default:
773       return Attribute::None;
774     }
775   }
776 
777   return IsRead ? Attribute::ReadOnly : Attribute::ReadNone;
778 }
779 
780 /// Deduce returned attributes for the SCC.
781 static void addArgumentReturnedAttrs(const SCCNodeSet &SCCNodes,
782                                      SmallSet<Function *, 8> &Changed) {
783   // Check each function in turn, determining if an argument is always returned.
784   for (Function *F : SCCNodes) {
785     // We can infer and propagate function attributes only when we know that the
786     // definition we'll get at link time is *exactly* the definition we see now.
787     // For more details, see GlobalValue::mayBeDerefined.
788     if (!F->hasExactDefinition())
789       continue;
790 
791     if (F->getReturnType()->isVoidTy())
792       continue;
793 
794     // There is nothing to do if an argument is already marked as 'returned'.
795     if (llvm::any_of(F->args(),
796                      [](const Argument &Arg) { return Arg.hasReturnedAttr(); }))
797       continue;
798 
799     auto FindRetArg = [&]() -> Value * {
800       Value *RetArg = nullptr;
801       for (BasicBlock &BB : *F)
802         if (auto *Ret = dyn_cast<ReturnInst>(BB.getTerminator())) {
803           // Note that stripPointerCasts should look through functions with
804           // returned arguments.
805           Value *RetVal = Ret->getReturnValue()->stripPointerCasts();
806           if (!isa<Argument>(RetVal) || RetVal->getType() != F->getReturnType())
807             return nullptr;
808 
809           if (!RetArg)
810             RetArg = RetVal;
811           else if (RetArg != RetVal)
812             return nullptr;
813         }
814 
815       return RetArg;
816     };
817 
818     if (Value *RetArg = FindRetArg()) {
819       auto *A = cast<Argument>(RetArg);
820       A->addAttr(Attribute::Returned);
821       ++NumReturned;
822       Changed.insert(F);
823     }
824   }
825 }
826 
827 /// If a callsite has arguments that are also arguments to the parent function,
828 /// try to propagate attributes from the callsite's arguments to the parent's
829 /// arguments. This may be important because inlining can cause information loss
830 /// when attribute knowledge disappears with the inlined call.
831 static bool addArgumentAttrsFromCallsites(Function &F) {
832   if (!EnableNonnullArgPropagation)
833     return false;
834 
835   bool Changed = false;
836 
837   // For an argument attribute to transfer from a callsite to the parent, the
838   // call must be guaranteed to execute every time the parent is called.
839   // Conservatively, just check for calls in the entry block that are guaranteed
840   // to execute.
841   // TODO: This could be enhanced by testing if the callsite post-dominates the
842   // entry block or by doing simple forward walks or backward walks to the
843   // callsite.
844   BasicBlock &Entry = F.getEntryBlock();
845   for (Instruction &I : Entry) {
846     if (auto *CB = dyn_cast<CallBase>(&I)) {
847       if (auto *CalledFunc = CB->getCalledFunction()) {
848         for (auto &CSArg : CalledFunc->args()) {
849           if (!CSArg.hasNonNullAttr(/* AllowUndefOrPoison */ false))
850             continue;
851 
852           // If the non-null callsite argument operand is an argument to 'F'
853           // (the caller) and the call is guaranteed to execute, then the value
854           // must be non-null throughout 'F'.
855           auto *FArg = dyn_cast<Argument>(CB->getArgOperand(CSArg.getArgNo()));
856           if (FArg && !FArg->hasNonNullAttr()) {
857             FArg->addAttr(Attribute::NonNull);
858             Changed = true;
859           }
860         }
861       }
862     }
863     if (!isGuaranteedToTransferExecutionToSuccessor(&I))
864       break;
865   }
866 
867   return Changed;
868 }
869 
870 static bool addReadAttr(Argument *A, Attribute::AttrKind R) {
871   assert((R == Attribute::ReadOnly || R == Attribute::ReadNone)
872          && "Must be a Read attribute.");
873   assert(A && "Argument must not be null.");
874 
875   // If the argument already has the attribute, nothing needs to be done.
876   if (A->hasAttribute(R))
877       return false;
878 
879   // Otherwise, remove potentially conflicting attribute, add the new one,
880   // and update statistics.
881   A->removeAttr(Attribute::WriteOnly);
882   A->removeAttr(Attribute::ReadOnly);
883   A->removeAttr(Attribute::ReadNone);
884   A->addAttr(R);
885   R == Attribute::ReadOnly ? ++NumReadOnlyArg : ++NumReadNoneArg;
886   return true;
887 }
888 
889 /// Deduce nocapture attributes for the SCC.
890 static void addArgumentAttrs(const SCCNodeSet &SCCNodes,
891                              SmallSet<Function *, 8> &Changed) {
892   ArgumentGraph AG;
893 
894   // Check each function in turn, determining which pointer arguments are not
895   // captured.
896   for (Function *F : SCCNodes) {
897     // We can infer and propagate function attributes only when we know that the
898     // definition we'll get at link time is *exactly* the definition we see now.
899     // For more details, see GlobalValue::mayBeDerefined.
900     if (!F->hasExactDefinition())
901       continue;
902 
903     if (addArgumentAttrsFromCallsites(*F))
904       Changed.insert(F);
905 
906     // Functions that are readonly (or readnone) and nounwind and don't return
907     // a value can't capture arguments. Don't analyze them.
908     if (F->onlyReadsMemory() && F->doesNotThrow() &&
909         F->getReturnType()->isVoidTy()) {
910       for (Function::arg_iterator A = F->arg_begin(), E = F->arg_end(); A != E;
911            ++A) {
912         if (A->getType()->isPointerTy() && !A->hasNoCaptureAttr()) {
913           A->addAttr(Attribute::NoCapture);
914           ++NumNoCapture;
915           Changed.insert(F);
916         }
917       }
918       continue;
919     }
920 
921     for (Function::arg_iterator A = F->arg_begin(), E = F->arg_end(); A != E;
922          ++A) {
923       if (!A->getType()->isPointerTy())
924         continue;
925       bool HasNonLocalUses = false;
926       if (!A->hasNoCaptureAttr()) {
927         ArgumentUsesTracker Tracker(SCCNodes);
928         PointerMayBeCaptured(&*A, &Tracker);
929         if (!Tracker.Captured) {
930           if (Tracker.Uses.empty()) {
931             // If it's trivially not captured, mark it nocapture now.
932             A->addAttr(Attribute::NoCapture);
933             ++NumNoCapture;
934             Changed.insert(F);
935           } else {
936             // If it's not trivially captured and not trivially not captured,
937             // then it must be calling into another function in our SCC. Save
938             // its particulars for Argument-SCC analysis later.
939             ArgumentGraphNode *Node = AG[&*A];
940             for (Argument *Use : Tracker.Uses) {
941               Node->Uses.push_back(AG[Use]);
942               if (Use != &*A)
943                 HasNonLocalUses = true;
944             }
945           }
946         }
947         // Otherwise, it's captured. Don't bother doing SCC analysis on it.
948       }
949       if (!HasNonLocalUses && !A->onlyReadsMemory()) {
950         // Can we determine that it's readonly/readnone without doing an SCC?
951         // Note that we don't allow any calls at all here, or else our result
952         // will be dependent on the iteration order through the functions in the
953         // SCC.
954         SmallPtrSet<Argument *, 8> Self;
955         Self.insert(&*A);
956         Attribute::AttrKind R = determinePointerReadAttrs(&*A, Self);
957         if (R != Attribute::None)
958           if (addReadAttr(A, R))
959             Changed.insert(F);
960       }
961     }
962   }
963 
964   // The graph we've collected is partial because we stopped scanning for
965   // argument uses once we solved the argument trivially. These partial nodes
966   // show up as ArgumentGraphNode objects with an empty Uses list, and for
967   // these nodes the final decision about whether they capture has already been
968   // made.  If the definition doesn't have a 'nocapture' attribute by now, it
969   // captures.
970 
971   for (scc_iterator<ArgumentGraph *> I = scc_begin(&AG); !I.isAtEnd(); ++I) {
972     const std::vector<ArgumentGraphNode *> &ArgumentSCC = *I;
973     if (ArgumentSCC.size() == 1) {
974       if (!ArgumentSCC[0]->Definition)
975         continue; // synthetic root node
976 
977       // eg. "void f(int* x) { if (...) f(x); }"
978       if (ArgumentSCC[0]->Uses.size() == 1 &&
979           ArgumentSCC[0]->Uses[0] == ArgumentSCC[0]) {
980         Argument *A = ArgumentSCC[0]->Definition;
981         A->addAttr(Attribute::NoCapture);
982         ++NumNoCapture;
983         Changed.insert(A->getParent());
984       }
985       continue;
986     }
987 
988     bool SCCCaptured = false;
989     for (auto I = ArgumentSCC.begin(), E = ArgumentSCC.end();
990          I != E && !SCCCaptured; ++I) {
991       ArgumentGraphNode *Node = *I;
992       if (Node->Uses.empty()) {
993         if (!Node->Definition->hasNoCaptureAttr())
994           SCCCaptured = true;
995       }
996     }
997     if (SCCCaptured)
998       continue;
999 
1000     SmallPtrSet<Argument *, 8> ArgumentSCCNodes;
1001     // Fill ArgumentSCCNodes with the elements of the ArgumentSCC.  Used for
1002     // quickly looking up whether a given Argument is in this ArgumentSCC.
1003     for (ArgumentGraphNode *I : ArgumentSCC) {
1004       ArgumentSCCNodes.insert(I->Definition);
1005     }
1006 
1007     for (auto I = ArgumentSCC.begin(), E = ArgumentSCC.end();
1008          I != E && !SCCCaptured; ++I) {
1009       ArgumentGraphNode *N = *I;
1010       for (ArgumentGraphNode *Use : N->Uses) {
1011         Argument *A = Use->Definition;
1012         if (A->hasNoCaptureAttr() || ArgumentSCCNodes.count(A))
1013           continue;
1014         SCCCaptured = true;
1015         break;
1016       }
1017     }
1018     if (SCCCaptured)
1019       continue;
1020 
1021     for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) {
1022       Argument *A = ArgumentSCC[i]->Definition;
1023       A->addAttr(Attribute::NoCapture);
1024       ++NumNoCapture;
1025       Changed.insert(A->getParent());
1026     }
1027 
1028     // We also want to compute readonly/readnone. With a small number of false
1029     // negatives, we can assume that any pointer which is captured isn't going
1030     // to be provably readonly or readnone, since by definition we can't
1031     // analyze all uses of a captured pointer.
1032     //
1033     // The false negatives happen when the pointer is captured by a function
1034     // that promises readonly/readnone behaviour on the pointer, then the
1035     // pointer's lifetime ends before anything that writes to arbitrary memory.
1036     // Also, a readonly/readnone pointer may be returned, but returning a
1037     // pointer is capturing it.
1038 
1039     Attribute::AttrKind ReadAttr = Attribute::ReadNone;
1040     for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) {
1041       Argument *A = ArgumentSCC[i]->Definition;
1042       Attribute::AttrKind K = determinePointerReadAttrs(A, ArgumentSCCNodes);
1043       if (K == Attribute::ReadNone)
1044         continue;
1045       if (K == Attribute::ReadOnly) {
1046         ReadAttr = Attribute::ReadOnly;
1047         continue;
1048       }
1049       ReadAttr = K;
1050       break;
1051     }
1052 
1053     if (ReadAttr != Attribute::None) {
1054       for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) {
1055         Argument *A = ArgumentSCC[i]->Definition;
1056         if (addReadAttr(A, ReadAttr))
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 // Set the noreturn function attribute if possible.
1604 static void addNoReturnAttrs(const SCCNodeSet &SCCNodes,
1605                              SmallSet<Function *, 8> &Changed) {
1606   for (Function *F : SCCNodes) {
1607     if (!F || !F->hasExactDefinition() || F->hasFnAttribute(Attribute::Naked) ||
1608         F->doesNotReturn())
1609       continue;
1610 
1611     // The function can return if any basic blocks can return.
1612     // FIXME: this doesn't handle recursion or unreachable blocks.
1613     if (none_of(*F, basicBlockCanReturn)) {
1614       F->setDoesNotReturn();
1615       Changed.insert(F);
1616     }
1617   }
1618 }
1619 
1620 static bool functionWillReturn(const Function &F) {
1621   // We can infer and propagate function attributes only when we know that the
1622   // definition we'll get at link time is *exactly* the definition we see now.
1623   // For more details, see GlobalValue::mayBeDerefined.
1624   if (!F.hasExactDefinition())
1625     return false;
1626 
1627   // Must-progress function without side-effects must return.
1628   if (F.mustProgress() && F.onlyReadsMemory())
1629     return true;
1630 
1631   // Can only analyze functions with a definition.
1632   if (F.isDeclaration())
1633     return false;
1634 
1635   // Functions with loops require more sophisticated analysis, as the loop
1636   // may be infinite. For now, don't try to handle them.
1637   SmallVector<std::pair<const BasicBlock *, const BasicBlock *>> Backedges;
1638   FindFunctionBackedges(F, Backedges);
1639   if (!Backedges.empty())
1640     return false;
1641 
1642   // If there are no loops, then the function is willreturn if all calls in
1643   // it are willreturn.
1644   return all_of(instructions(F), [](const Instruction &I) {
1645     return I.willReturn();
1646   });
1647 }
1648 
1649 // Set the willreturn function attribute if possible.
1650 static void addWillReturn(const SCCNodeSet &SCCNodes,
1651                           SmallSet<Function *, 8> &Changed) {
1652   for (Function *F : SCCNodes) {
1653     if (!F || F->willReturn() || !functionWillReturn(*F))
1654       continue;
1655 
1656     F->setWillReturn();
1657     NumWillReturn++;
1658     Changed.insert(F);
1659   }
1660 }
1661 
1662 // Return true if this is an atomic which has an ordering stronger than
1663 // unordered.  Note that this is different than the predicate we use in
1664 // Attributor.  Here we chose to be conservative and consider monotonic
1665 // operations potentially synchronizing.  We generally don't do much with
1666 // monotonic operations, so this is simply risk reduction.
1667 static bool isOrderedAtomic(Instruction *I) {
1668   if (!I->isAtomic())
1669     return false;
1670 
1671   if (auto *FI = dyn_cast<FenceInst>(I))
1672     // All legal orderings for fence are stronger than monotonic.
1673     return FI->getSyncScopeID() != SyncScope::SingleThread;
1674   else if (isa<AtomicCmpXchgInst>(I) || isa<AtomicRMWInst>(I))
1675     return true;
1676   else if (auto *SI = dyn_cast<StoreInst>(I))
1677     return !SI->isUnordered();
1678   else if (auto *LI = dyn_cast<LoadInst>(I))
1679     return !LI->isUnordered();
1680   else {
1681     llvm_unreachable("unknown atomic instruction?");
1682   }
1683 }
1684 
1685 static bool InstrBreaksNoSync(Instruction &I, const SCCNodeSet &SCCNodes) {
1686   // Volatile may synchronize
1687   if (I.isVolatile())
1688     return true;
1689 
1690   // An ordered atomic may synchronize.  (See comment about on monotonic.)
1691   if (isOrderedAtomic(&I))
1692     return true;
1693 
1694   auto *CB = dyn_cast<CallBase>(&I);
1695   if (!CB)
1696     // Non call site cases covered by the two checks above
1697     return false;
1698 
1699   if (CB->hasFnAttr(Attribute::NoSync))
1700     return false;
1701 
1702   // Non volatile memset/memcpy/memmoves are nosync
1703   // NOTE: Only intrinsics with volatile flags should be handled here.  All
1704   // others should be marked in Intrinsics.td.
1705   if (auto *MI = dyn_cast<MemIntrinsic>(&I))
1706     if (!MI->isVolatile())
1707       return false;
1708 
1709   // Speculatively assume in SCC.
1710   if (Function *Callee = CB->getCalledFunction())
1711     if (SCCNodes.contains(Callee))
1712       return false;
1713 
1714   return true;
1715 }
1716 
1717 // Infer the nosync attribute.
1718 static void addNoSyncAttr(const SCCNodeSet &SCCNodes,
1719                           SmallSet<Function *, 8> &Changed) {
1720   AttributeInferer AI;
1721   AI.registerAttrInference(AttributeInferer::InferenceDescriptor{
1722       Attribute::NoSync,
1723       // Skip already marked functions.
1724       [](const Function &F) { return F.hasNoSync(); },
1725       // Instructions that break nosync assumption.
1726       [&SCCNodes](Instruction &I) {
1727         return InstrBreaksNoSync(I, SCCNodes);
1728       },
1729       [](Function &F) {
1730         LLVM_DEBUG(dbgs()
1731                    << "Adding nosync attr to fn " << F.getName() << "\n");
1732         F.setNoSync();
1733         ++NumNoSync;
1734       },
1735       /* RequiresExactDefinition= */ true});
1736   AI.run(SCCNodes, Changed);
1737 }
1738 
1739 static SCCNodesResult createSCCNodeSet(ArrayRef<Function *> Functions) {
1740   SCCNodesResult Res;
1741   Res.HasUnknownCall = false;
1742   for (Function *F : Functions) {
1743     if (!F || F->hasOptNone() || F->hasFnAttribute(Attribute::Naked) ||
1744         F->isPresplitCoroutine()) {
1745       // Treat any function we're trying not to optimize as if it were an
1746       // indirect call and omit it from the node set used below.
1747       Res.HasUnknownCall = true;
1748       continue;
1749     }
1750     // Track whether any functions in this SCC have an unknown call edge.
1751     // Note: if this is ever a performance hit, we can common it with
1752     // subsequent routines which also do scans over the instructions of the
1753     // function.
1754     if (!Res.HasUnknownCall) {
1755       for (Instruction &I : instructions(*F)) {
1756         if (auto *CB = dyn_cast<CallBase>(&I)) {
1757           if (!CB->getCalledFunction()) {
1758             Res.HasUnknownCall = true;
1759             break;
1760           }
1761         }
1762       }
1763     }
1764     Res.SCCNodes.insert(F);
1765   }
1766   return Res;
1767 }
1768 
1769 template <typename AARGetterT>
1770 static SmallSet<Function *, 8>
1771 deriveAttrsInPostOrder(ArrayRef<Function *> Functions, AARGetterT &&AARGetter) {
1772   SCCNodesResult Nodes = createSCCNodeSet(Functions);
1773 
1774   // Bail if the SCC only contains optnone functions.
1775   if (Nodes.SCCNodes.empty())
1776     return {};
1777 
1778   SmallSet<Function *, 8> Changed;
1779 
1780   addArgumentReturnedAttrs(Nodes.SCCNodes, Changed);
1781   addReadAttrs(Nodes.SCCNodes, AARGetter, Changed);
1782   addArgumentAttrs(Nodes.SCCNodes, Changed);
1783   inferConvergent(Nodes.SCCNodes, Changed);
1784   addNoReturnAttrs(Nodes.SCCNodes, Changed);
1785   addWillReturn(Nodes.SCCNodes, Changed);
1786 
1787   // If we have no external nodes participating in the SCC, we can deduce some
1788   // more precise attributes as well.
1789   if (!Nodes.HasUnknownCall) {
1790     addNoAliasAttrs(Nodes.SCCNodes, Changed);
1791     addNonNullAttrs(Nodes.SCCNodes, Changed);
1792     inferAttrsFromFunctionBodies(Nodes.SCCNodes, Changed);
1793     addNoRecurseAttrs(Nodes.SCCNodes, Changed);
1794   }
1795 
1796   addNoSyncAttr(Nodes.SCCNodes, Changed);
1797 
1798   // Finally, infer the maximal set of attributes from the ones we've inferred
1799   // above.  This is handling the cases where one attribute on a signature
1800   // implies another, but for implementation reasons the inference rule for
1801   // the later is missing (or simply less sophisticated).
1802   for (Function *F : Nodes.SCCNodes)
1803     if (F)
1804       if (inferAttributesFromOthers(*F))
1805         Changed.insert(F);
1806 
1807   return Changed;
1808 }
1809 
1810 PreservedAnalyses PostOrderFunctionAttrsPass::run(LazyCallGraph::SCC &C,
1811                                                   CGSCCAnalysisManager &AM,
1812                                                   LazyCallGraph &CG,
1813                                                   CGSCCUpdateResult &) {
1814   FunctionAnalysisManager &FAM =
1815       AM.getResult<FunctionAnalysisManagerCGSCCProxy>(C, CG).getManager();
1816 
1817   // We pass a lambda into functions to wire them up to the analysis manager
1818   // for getting function analyses.
1819   auto AARGetter = [&](Function &F) -> AAResults & {
1820     return FAM.getResult<AAManager>(F);
1821   };
1822 
1823   SmallVector<Function *, 8> Functions;
1824   for (LazyCallGraph::Node &N : C) {
1825     Functions.push_back(&N.getFunction());
1826   }
1827 
1828   auto ChangedFunctions = deriveAttrsInPostOrder(Functions, AARGetter);
1829   if (ChangedFunctions.empty())
1830     return PreservedAnalyses::all();
1831 
1832   PreservedAnalyses PA;
1833   // We have not added or removed functions.
1834   PA.preserve<FunctionAnalysisManagerCGSCCProxy>();
1835   return PA;
1836 }
1837 
1838 namespace {
1839 
1840 struct PostOrderFunctionAttrsLegacyPass : public CallGraphSCCPass {
1841   // Pass identification, replacement for typeid
1842   static char ID;
1843 
1844   PostOrderFunctionAttrsLegacyPass() : CallGraphSCCPass(ID) {
1845     initializePostOrderFunctionAttrsLegacyPassPass(
1846         *PassRegistry::getPassRegistry());
1847   }
1848 
1849   bool runOnSCC(CallGraphSCC &SCC) override;
1850 
1851   void getAnalysisUsage(AnalysisUsage &AU) const override {
1852     AU.setPreservesCFG();
1853     AU.addRequired<AssumptionCacheTracker>();
1854     getAAResultsAnalysisUsage(AU);
1855     CallGraphSCCPass::getAnalysisUsage(AU);
1856   }
1857 };
1858 
1859 } // end anonymous namespace
1860 
1861 char PostOrderFunctionAttrsLegacyPass::ID = 0;
1862 INITIALIZE_PASS_BEGIN(PostOrderFunctionAttrsLegacyPass, "function-attrs",
1863                       "Deduce function attributes", false, false)
1864 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
1865 INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
1866 INITIALIZE_PASS_END(PostOrderFunctionAttrsLegacyPass, "function-attrs",
1867                     "Deduce function attributes", false, false)
1868 
1869 Pass *llvm::createPostOrderFunctionAttrsLegacyPass() {
1870   return new PostOrderFunctionAttrsLegacyPass();
1871 }
1872 
1873 template <typename AARGetterT>
1874 static bool runImpl(CallGraphSCC &SCC, AARGetterT AARGetter) {
1875   SmallVector<Function *, 8> Functions;
1876   for (CallGraphNode *I : SCC) {
1877     Functions.push_back(I->getFunction());
1878   }
1879 
1880   return !deriveAttrsInPostOrder(Functions, AARGetter).empty();
1881 }
1882 
1883 bool PostOrderFunctionAttrsLegacyPass::runOnSCC(CallGraphSCC &SCC) {
1884   if (skipSCC(SCC))
1885     return false;
1886   return runImpl(SCC, LegacyAARGetter(*this));
1887 }
1888 
1889 namespace {
1890 
1891 struct ReversePostOrderFunctionAttrsLegacyPass : public ModulePass {
1892   // Pass identification, replacement for typeid
1893   static char ID;
1894 
1895   ReversePostOrderFunctionAttrsLegacyPass() : ModulePass(ID) {
1896     initializeReversePostOrderFunctionAttrsLegacyPassPass(
1897         *PassRegistry::getPassRegistry());
1898   }
1899 
1900   bool runOnModule(Module &M) override;
1901 
1902   void getAnalysisUsage(AnalysisUsage &AU) const override {
1903     AU.setPreservesCFG();
1904     AU.addRequired<CallGraphWrapperPass>();
1905     AU.addPreserved<CallGraphWrapperPass>();
1906   }
1907 };
1908 
1909 } // end anonymous namespace
1910 
1911 char ReversePostOrderFunctionAttrsLegacyPass::ID = 0;
1912 
1913 INITIALIZE_PASS_BEGIN(ReversePostOrderFunctionAttrsLegacyPass,
1914                       "rpo-function-attrs", "Deduce function attributes in RPO",
1915                       false, false)
1916 INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
1917 INITIALIZE_PASS_END(ReversePostOrderFunctionAttrsLegacyPass,
1918                     "rpo-function-attrs", "Deduce function attributes in RPO",
1919                     false, false)
1920 
1921 Pass *llvm::createReversePostOrderFunctionAttrsPass() {
1922   return new ReversePostOrderFunctionAttrsLegacyPass();
1923 }
1924 
1925 static bool addNoRecurseAttrsTopDown(Function &F) {
1926   // We check the preconditions for the function prior to calling this to avoid
1927   // the cost of building up a reversible post-order list. We assert them here
1928   // to make sure none of the invariants this relies on were violated.
1929   assert(!F.isDeclaration() && "Cannot deduce norecurse without a definition!");
1930   assert(!F.doesNotRecurse() &&
1931          "This function has already been deduced as norecurs!");
1932   assert(F.hasInternalLinkage() &&
1933          "Can only do top-down deduction for internal linkage functions!");
1934 
1935   // If F is internal and all of its uses are calls from a non-recursive
1936   // functions, then none of its calls could in fact recurse without going
1937   // through a function marked norecurse, and so we can mark this function too
1938   // as norecurse. Note that the uses must actually be calls -- otherwise
1939   // a pointer to this function could be returned from a norecurse function but
1940   // this function could be recursively (indirectly) called. Note that this
1941   // also detects if F is directly recursive as F is not yet marked as
1942   // a norecurse function.
1943   for (auto *U : F.users()) {
1944     auto *I = dyn_cast<Instruction>(U);
1945     if (!I)
1946       return false;
1947     CallBase *CB = dyn_cast<CallBase>(I);
1948     if (!CB || !CB->getParent()->getParent()->doesNotRecurse())
1949       return false;
1950   }
1951   F.setDoesNotRecurse();
1952   ++NumNoRecurse;
1953   return true;
1954 }
1955 
1956 static bool deduceFunctionAttributeInRPO(Module &M, CallGraph &CG) {
1957   // We only have a post-order SCC traversal (because SCCs are inherently
1958   // discovered in post-order), so we accumulate them in a vector and then walk
1959   // it in reverse. This is simpler than using the RPO iterator infrastructure
1960   // because we need to combine SCC detection and the PO walk of the call
1961   // graph. We can also cheat egregiously because we're primarily interested in
1962   // synthesizing norecurse and so we can only save the singular SCCs as SCCs
1963   // with multiple functions in them will clearly be recursive.
1964   SmallVector<Function *, 16> Worklist;
1965   for (scc_iterator<CallGraph *> I = scc_begin(&CG); !I.isAtEnd(); ++I) {
1966     if (I->size() != 1)
1967       continue;
1968 
1969     Function *F = I->front()->getFunction();
1970     if (F && !F->isDeclaration() && !F->doesNotRecurse() &&
1971         F->hasInternalLinkage())
1972       Worklist.push_back(F);
1973   }
1974 
1975   bool Changed = false;
1976   for (auto *F : llvm::reverse(Worklist))
1977     Changed |= addNoRecurseAttrsTopDown(*F);
1978 
1979   return Changed;
1980 }
1981 
1982 bool ReversePostOrderFunctionAttrsLegacyPass::runOnModule(Module &M) {
1983   if (skipModule(M))
1984     return false;
1985 
1986   auto &CG = getAnalysis<CallGraphWrapperPass>().getCallGraph();
1987 
1988   return deduceFunctionAttributeInRPO(M, CG);
1989 }
1990 
1991 PreservedAnalyses
1992 ReversePostOrderFunctionAttrsPass::run(Module &M, ModuleAnalysisManager &AM) {
1993   auto &CG = AM.getResult<CallGraphAnalysis>(M);
1994 
1995   if (!deduceFunctionAttributeInRPO(M, CG))
1996     return PreservedAnalyses::all();
1997 
1998   PreservedAnalyses PA;
1999   PA.preserve<CallGraphAnalysis>();
2000   return PA;
2001 }
2002