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/SCCIterator.h"
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/ADT/SetVector.h"
19 #include "llvm/ADT/SmallPtrSet.h"
20 #include "llvm/ADT/SmallVector.h"
21 #include "llvm/ADT/Statistic.h"
22 #include "llvm/Analysis/AliasAnalysis.h"
23 #include "llvm/Analysis/AssumptionCache.h"
24 #include "llvm/Analysis/BasicAliasAnalysis.h"
25 #include "llvm/Analysis/CGSCCPassManager.h"
26 #include "llvm/Analysis/CallGraph.h"
27 #include "llvm/Analysis/CallGraphSCCPass.h"
28 #include "llvm/Analysis/CaptureTracking.h"
29 #include "llvm/Analysis/LazyCallGraph.h"
30 #include "llvm/Analysis/MemoryBuiltins.h"
31 #include "llvm/Analysis/MemoryLocation.h"
32 #include "llvm/Analysis/ValueTracking.h"
33 #include "llvm/IR/Argument.h"
34 #include "llvm/IR/Attributes.h"
35 #include "llvm/IR/BasicBlock.h"
36 #include "llvm/IR/CallSite.h"
37 #include "llvm/IR/Constant.h"
38 #include "llvm/IR/Constants.h"
39 #include "llvm/IR/Function.h"
40 #include "llvm/IR/InstIterator.h"
41 #include "llvm/IR/InstrTypes.h"
42 #include "llvm/IR/Instruction.h"
43 #include "llvm/IR/Instructions.h"
44 #include "llvm/IR/IntrinsicInst.h"
45 #include "llvm/IR/Metadata.h"
46 #include "llvm/IR/PassManager.h"
47 #include "llvm/IR/Type.h"
48 #include "llvm/IR/Use.h"
49 #include "llvm/IR/User.h"
50 #include "llvm/IR/Value.h"
51 #include "llvm/Pass.h"
52 #include "llvm/Support/Casting.h"
53 #include "llvm/Support/CommandLine.h"
54 #include "llvm/Support/Compiler.h"
55 #include "llvm/Support/Debug.h"
56 #include "llvm/Support/ErrorHandling.h"
57 #include "llvm/Support/raw_ostream.h"
58 #include "llvm/Transforms/IPO.h"
59 #include <cassert>
60 #include <iterator>
61 #include <map>
62 #include <vector>
63 
64 using namespace llvm;
65 
66 #define DEBUG_TYPE "functionattrs"
67 
68 STATISTIC(NumReadNone, "Number of functions marked readnone");
69 STATISTIC(NumReadOnly, "Number of functions marked readonly");
70 STATISTIC(NumWriteOnly, "Number of functions marked writeonly");
71 STATISTIC(NumNoCapture, "Number of arguments marked nocapture");
72 STATISTIC(NumReturned, "Number of arguments marked returned");
73 STATISTIC(NumReadNoneArg, "Number of arguments marked readnone");
74 STATISTIC(NumReadOnlyArg, "Number of arguments marked readonly");
75 STATISTIC(NumNoAlias, "Number of function returns marked noalias");
76 STATISTIC(NumNonNullReturn, "Number of function returns marked nonnull");
77 STATISTIC(NumNoRecurse, "Number of functions marked as norecurse");
78 STATISTIC(NumNoUnwind, "Number of functions marked as nounwind");
79 STATISTIC(NumNoFree, "Number of functions marked as nofree");
80 
81 // FIXME: This is disabled by default to avoid exposing security vulnerabilities
82 // in C/C++ code compiled by clang:
83 // http://lists.llvm.org/pipermail/cfe-dev/2017-January/052066.html
84 static cl::opt<bool> EnableNonnullArgPropagation(
85     "enable-nonnull-arg-prop", cl::Hidden,
86     cl::desc("Try to propagate nonnull argument attributes from callsites to "
87              "caller functions."));
88 
89 static cl::opt<bool> DisableNoUnwindInference(
90     "disable-nounwind-inference", cl::Hidden,
91     cl::desc("Stop inferring nounwind attribute during function-attrs pass"));
92 
93 static cl::opt<bool> DisableNoFreeInference(
94     "disable-nofree-inference", cl::Hidden,
95     cl::desc("Stop inferring nofree attribute during function-attrs pass"));
96 
97 namespace {
98 
99 using SCCNodeSet = SmallSetVector<Function *, 8>;
100 
101 } // end anonymous namespace
102 
103 /// Returns the memory access attribute for function F using AAR for AA results,
104 /// where SCCNodes is the current SCC.
105 ///
106 /// If ThisBody is true, this function may examine the function body and will
107 /// return a result pertaining to this copy of the function. If it is false, the
108 /// result will be based only on AA results for the function declaration; it
109 /// will be assumed that some other (perhaps less optimized) version of the
110 /// function may be selected at link time.
111 static MemoryAccessKind checkFunctionMemoryAccess(Function &F, bool ThisBody,
112                                                   AAResults &AAR,
113                                                   const SCCNodeSet &SCCNodes) {
114   FunctionModRefBehavior MRB = AAR.getModRefBehavior(&F);
115   if (MRB == FMRB_DoesNotAccessMemory)
116     // Already perfect!
117     return MAK_ReadNone;
118 
119   if (!ThisBody) {
120     if (AliasAnalysis::onlyReadsMemory(MRB))
121       return MAK_ReadOnly;
122 
123     if (AliasAnalysis::doesNotReadMemory(MRB))
124       return MAK_WriteOnly;
125 
126     // Conservatively assume it reads and writes to memory.
127     return MAK_MayWrite;
128   }
129 
130   // Scan the function body for instructions that may read or write memory.
131   bool ReadsMemory = false;
132   bool WritesMemory = false;
133   for (inst_iterator II = inst_begin(F), E = inst_end(F); II != E; ++II) {
134     Instruction *I = &*II;
135 
136     // Some instructions can be ignored even if they read or write memory.
137     // Detect these now, skipping to the next instruction if one is found.
138     if (auto *Call = dyn_cast<CallBase>(I)) {
139       // Ignore calls to functions in the same SCC, as long as the call sites
140       // don't have operand bundles.  Calls with operand bundles are allowed to
141       // have memory effects not described by the memory effects of the call
142       // target.
143       if (!Call->hasOperandBundles() && Call->getCalledFunction() &&
144           SCCNodes.count(Call->getCalledFunction()))
145         continue;
146       FunctionModRefBehavior MRB = AAR.getModRefBehavior(Call);
147       ModRefInfo MRI = createModRefInfo(MRB);
148 
149       // If the call doesn't access memory, we're done.
150       if (isNoModRef(MRI))
151         continue;
152 
153       if (!AliasAnalysis::onlyAccessesArgPointees(MRB)) {
154         // The call could access any memory. If that includes writes, note it.
155         if (isModSet(MRI))
156           WritesMemory = true;
157         // If it reads, note it.
158         if (isRefSet(MRI))
159           ReadsMemory = true;
160         continue;
161       }
162 
163       // Check whether all pointer arguments point to local memory, and
164       // ignore calls that only access local memory.
165       for (CallSite::arg_iterator CI = Call->arg_begin(), CE = Call->arg_end();
166            CI != CE; ++CI) {
167         Value *Arg = *CI;
168         if (!Arg->getType()->isPtrOrPtrVectorTy())
169           continue;
170 
171         AAMDNodes AAInfo;
172         I->getAAMetadata(AAInfo);
173         MemoryLocation Loc(Arg, LocationSize::unknown(), AAInfo);
174 
175         // Skip accesses to local or constant memory as they don't impact the
176         // externally visible mod/ref behavior.
177         if (AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true))
178           continue;
179 
180         if (isModSet(MRI))
181           // Writes non-local memory.
182           WritesMemory = true;
183         if (isRefSet(MRI))
184           // Ok, it reads non-local memory.
185           ReadsMemory = true;
186       }
187       continue;
188     } else if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
189       // Ignore non-volatile loads from local memory. (Atomic is okay here.)
190       if (!LI->isVolatile()) {
191         MemoryLocation Loc = MemoryLocation::get(LI);
192         if (AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true))
193           continue;
194       }
195     } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
196       // Ignore non-volatile stores to local memory. (Atomic is okay here.)
197       if (!SI->isVolatile()) {
198         MemoryLocation Loc = MemoryLocation::get(SI);
199         if (AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true))
200           continue;
201       }
202     } else if (VAArgInst *VI = dyn_cast<VAArgInst>(I)) {
203       // Ignore vaargs on local memory.
204       MemoryLocation Loc = MemoryLocation::get(VI);
205       if (AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true))
206         continue;
207     }
208 
209     // Any remaining instructions need to be taken seriously!  Check if they
210     // read or write memory.
211     //
212     // Writes memory, remember that.
213     WritesMemory |= I->mayWriteToMemory();
214 
215     // If this instruction may read memory, remember that.
216     ReadsMemory |= I->mayReadFromMemory();
217   }
218 
219   if (WritesMemory) {
220     if (!ReadsMemory)
221       return MAK_WriteOnly;
222     else
223       return MAK_MayWrite;
224   }
225 
226   return ReadsMemory ? MAK_ReadOnly : MAK_ReadNone;
227 }
228 
229 MemoryAccessKind llvm::computeFunctionBodyMemoryAccess(Function &F,
230                                                        AAResults &AAR) {
231   return checkFunctionMemoryAccess(F, /*ThisBody=*/true, AAR, {});
232 }
233 
234 /// Deduce readonly/readnone attributes for the SCC.
235 template <typename AARGetterT>
236 static bool addReadAttrs(const SCCNodeSet &SCCNodes, AARGetterT &&AARGetter) {
237   // Check if any of the functions in the SCC read or write memory.  If they
238   // write memory then they can't be marked readnone or readonly.
239   bool ReadsMemory = false;
240   bool WritesMemory = false;
241   for (Function *F : SCCNodes) {
242     // Call the callable parameter to look up AA results for this function.
243     AAResults &AAR = AARGetter(*F);
244 
245     // Non-exact function definitions may not be selected at link time, and an
246     // alternative version that writes to memory may be selected.  See the
247     // comment on GlobalValue::isDefinitionExact for more details.
248     switch (checkFunctionMemoryAccess(*F, F->hasExactDefinition(),
249                                       AAR, SCCNodes)) {
250     case MAK_MayWrite:
251       return false;
252     case MAK_ReadOnly:
253       ReadsMemory = true;
254       break;
255     case MAK_WriteOnly:
256       WritesMemory = true;
257       break;
258     case MAK_ReadNone:
259       // Nothing to do!
260       break;
261     }
262   }
263 
264   // Success!  Functions in this SCC do not access memory, or only read memory.
265   // Give them the appropriate attribute.
266   bool MadeChange = false;
267 
268   assert(!(ReadsMemory && WritesMemory) &&
269           "Function marked read-only and write-only");
270   for (Function *F : SCCNodes) {
271     if (F->doesNotAccessMemory())
272       // Already perfect!
273       continue;
274 
275     if (F->onlyReadsMemory() && ReadsMemory)
276       // No change.
277       continue;
278 
279     if (F->doesNotReadMemory() && WritesMemory)
280       continue;
281 
282     MadeChange = true;
283 
284     // Clear out any existing attributes.
285     F->removeFnAttr(Attribute::ReadOnly);
286     F->removeFnAttr(Attribute::ReadNone);
287     F->removeFnAttr(Attribute::WriteOnly);
288 
289     if (!WritesMemory && !ReadsMemory) {
290       // Clear out any "access range attributes" if readnone was deduced.
291       F->removeFnAttr(Attribute::ArgMemOnly);
292       F->removeFnAttr(Attribute::InaccessibleMemOnly);
293       F->removeFnAttr(Attribute::InaccessibleMemOrArgMemOnly);
294     }
295 
296     // Add in the new attribute.
297     if (WritesMemory && !ReadsMemory)
298       F->addFnAttr(Attribute::WriteOnly);
299     else
300       F->addFnAttr(ReadsMemory ? Attribute::ReadOnly : Attribute::ReadNone);
301 
302     if (WritesMemory && !ReadsMemory)
303       ++NumWriteOnly;
304     else if (ReadsMemory)
305       ++NumReadOnly;
306     else
307       ++NumReadNone;
308   }
309 
310   return MadeChange;
311 }
312 
313 namespace {
314 
315 /// For a given pointer Argument, this retains a list of Arguments of functions
316 /// in the same SCC that the pointer data flows into. We use this to build an
317 /// SCC of the arguments.
318 struct ArgumentGraphNode {
319   Argument *Definition;
320   SmallVector<ArgumentGraphNode *, 4> Uses;
321 };
322 
323 class ArgumentGraph {
324   // We store pointers to ArgumentGraphNode objects, so it's important that
325   // that they not move around upon insert.
326   using ArgumentMapTy = std::map<Argument *, ArgumentGraphNode>;
327 
328   ArgumentMapTy ArgumentMap;
329 
330   // There is no root node for the argument graph, in fact:
331   //   void f(int *x, int *y) { if (...) f(x, y); }
332   // is an example where the graph is disconnected. The SCCIterator requires a
333   // single entry point, so we maintain a fake ("synthetic") root node that
334   // uses every node. Because the graph is directed and nothing points into
335   // the root, it will not participate in any SCCs (except for its own).
336   ArgumentGraphNode SyntheticRoot;
337 
338 public:
339   ArgumentGraph() { SyntheticRoot.Definition = nullptr; }
340 
341   using iterator = SmallVectorImpl<ArgumentGraphNode *>::iterator;
342 
343   iterator begin() { return SyntheticRoot.Uses.begin(); }
344   iterator end() { return SyntheticRoot.Uses.end(); }
345   ArgumentGraphNode *getEntryNode() { return &SyntheticRoot; }
346 
347   ArgumentGraphNode *operator[](Argument *A) {
348     ArgumentGraphNode &Node = ArgumentMap[A];
349     Node.Definition = A;
350     SyntheticRoot.Uses.push_back(&Node);
351     return &Node;
352   }
353 };
354 
355 /// This tracker checks whether callees are in the SCC, and if so it does not
356 /// consider that a capture, instead adding it to the "Uses" list and
357 /// continuing with the analysis.
358 struct ArgumentUsesTracker : public CaptureTracker {
359   ArgumentUsesTracker(const SCCNodeSet &SCCNodes) : SCCNodes(SCCNodes) {}
360 
361   void tooManyUses() override { Captured = true; }
362 
363   bool captured(const Use *U) override {
364     CallSite CS(U->getUser());
365     if (!CS.getInstruction()) {
366       Captured = true;
367       return true;
368     }
369 
370     Function *F = CS.getCalledFunction();
371     if (!F || !F->hasExactDefinition() || !SCCNodes.count(F)) {
372       Captured = true;
373       return true;
374     }
375 
376     // Note: the callee and the two successor blocks *follow* the argument
377     // operands.  This means there is no need to adjust UseIndex to account for
378     // these.
379 
380     unsigned UseIndex =
381         std::distance(const_cast<const Use *>(CS.arg_begin()), U);
382 
383     assert(UseIndex < CS.data_operands_size() &&
384            "Indirect function calls should have been filtered above!");
385 
386     if (UseIndex >= CS.getNumArgOperands()) {
387       // Data operand, but not a argument operand -- must be a bundle operand
388       assert(CS.hasOperandBundles() && "Must be!");
389 
390       // CaptureTracking told us that we're being captured by an operand bundle
391       // use.  In this case it does not matter if the callee is within our SCC
392       // or not -- we've been captured in some unknown way, and we have to be
393       // conservative.
394       Captured = true;
395       return true;
396     }
397 
398     if (UseIndex >= F->arg_size()) {
399       assert(F->isVarArg() && "More params than args in non-varargs call");
400       Captured = true;
401       return true;
402     }
403 
404     Uses.push_back(&*std::next(F->arg_begin(), UseIndex));
405     return false;
406   }
407 
408   // True only if certainly captured (used outside our SCC).
409   bool Captured = false;
410 
411   // Uses within our SCC.
412   SmallVector<Argument *, 4> Uses;
413 
414   const SCCNodeSet &SCCNodes;
415 };
416 
417 } // end anonymous namespace
418 
419 namespace llvm {
420 
421 template <> struct GraphTraits<ArgumentGraphNode *> {
422   using NodeRef = ArgumentGraphNode *;
423   using ChildIteratorType = SmallVectorImpl<ArgumentGraphNode *>::iterator;
424 
425   static NodeRef getEntryNode(NodeRef A) { return A; }
426   static ChildIteratorType child_begin(NodeRef N) { return N->Uses.begin(); }
427   static ChildIteratorType child_end(NodeRef N) { return N->Uses.end(); }
428 };
429 
430 template <>
431 struct GraphTraits<ArgumentGraph *> : public GraphTraits<ArgumentGraphNode *> {
432   static NodeRef getEntryNode(ArgumentGraph *AG) { return AG->getEntryNode(); }
433 
434   static ChildIteratorType nodes_begin(ArgumentGraph *AG) {
435     return AG->begin();
436   }
437 
438   static ChildIteratorType nodes_end(ArgumentGraph *AG) { return AG->end(); }
439 };
440 
441 } // end namespace llvm
442 
443 /// Returns Attribute::None, Attribute::ReadOnly or Attribute::ReadNone.
444 static Attribute::AttrKind
445 determinePointerReadAttrs(Argument *A,
446                           const SmallPtrSet<Argument *, 8> &SCCNodes) {
447   SmallVector<Use *, 32> Worklist;
448   SmallPtrSet<Use *, 32> Visited;
449 
450   // inalloca arguments are always clobbered by the call.
451   if (A->hasInAllocaAttr())
452     return Attribute::None;
453 
454   bool IsRead = false;
455   // We don't need to track IsWritten. If A is written to, return immediately.
456 
457   for (Use &U : A->uses()) {
458     Visited.insert(&U);
459     Worklist.push_back(&U);
460   }
461 
462   while (!Worklist.empty()) {
463     Use *U = Worklist.pop_back_val();
464     Instruction *I = cast<Instruction>(U->getUser());
465 
466     switch (I->getOpcode()) {
467     case Instruction::BitCast:
468     case Instruction::GetElementPtr:
469     case Instruction::PHI:
470     case Instruction::Select:
471     case Instruction::AddrSpaceCast:
472       // The original value is not read/written via this if the new value isn't.
473       for (Use &UU : I->uses())
474         if (Visited.insert(&UU).second)
475           Worklist.push_back(&UU);
476       break;
477 
478     case Instruction::Call:
479     case Instruction::Invoke: {
480       bool Captures = true;
481 
482       if (I->getType()->isVoidTy())
483         Captures = false;
484 
485       auto AddUsersToWorklistIfCapturing = [&] {
486         if (Captures)
487           for (Use &UU : I->uses())
488             if (Visited.insert(&UU).second)
489               Worklist.push_back(&UU);
490       };
491 
492       CallSite CS(I);
493       if (CS.doesNotAccessMemory()) {
494         AddUsersToWorklistIfCapturing();
495         continue;
496       }
497 
498       Function *F = CS.getCalledFunction();
499       if (!F) {
500         if (CS.onlyReadsMemory()) {
501           IsRead = true;
502           AddUsersToWorklistIfCapturing();
503           continue;
504         }
505         return Attribute::None;
506       }
507 
508       // Note: the callee and the two successor blocks *follow* the argument
509       // operands.  This means there is no need to adjust UseIndex to account
510       // for these.
511 
512       unsigned UseIndex = std::distance(CS.arg_begin(), U);
513 
514       // U cannot be the callee operand use: since we're exploring the
515       // transitive uses of an Argument, having such a use be a callee would
516       // imply the CallSite is an indirect call or invoke; and we'd take the
517       // early exit above.
518       assert(UseIndex < CS.data_operands_size() &&
519              "Data operand use expected!");
520 
521       bool IsOperandBundleUse = UseIndex >= CS.getNumArgOperands();
522 
523       if (UseIndex >= F->arg_size() && !IsOperandBundleUse) {
524         assert(F->isVarArg() && "More params than args in non-varargs call");
525         return Attribute::None;
526       }
527 
528       Captures &= !CS.doesNotCapture(UseIndex);
529 
530       // Since the optimizer (by design) cannot see the data flow corresponding
531       // to a operand bundle use, these cannot participate in the optimistic SCC
532       // analysis.  Instead, we model the operand bundle uses as arguments in
533       // call to a function external to the SCC.
534       if (IsOperandBundleUse ||
535           !SCCNodes.count(&*std::next(F->arg_begin(), UseIndex))) {
536 
537         // The accessors used on CallSite here do the right thing for calls and
538         // invokes with operand bundles.
539 
540         if (!CS.onlyReadsMemory() && !CS.onlyReadsMemory(UseIndex))
541           return Attribute::None;
542         if (!CS.doesNotAccessMemory(UseIndex))
543           IsRead = true;
544       }
545 
546       AddUsersToWorklistIfCapturing();
547       break;
548     }
549 
550     case Instruction::Load:
551       // A volatile load has side effects beyond what readonly can be relied
552       // upon.
553       if (cast<LoadInst>(I)->isVolatile())
554         return Attribute::None;
555 
556       IsRead = true;
557       break;
558 
559     case Instruction::ICmp:
560     case Instruction::Ret:
561       break;
562 
563     default:
564       return Attribute::None;
565     }
566   }
567 
568   return IsRead ? Attribute::ReadOnly : Attribute::ReadNone;
569 }
570 
571 /// Deduce returned attributes for the SCC.
572 static bool addArgumentReturnedAttrs(const SCCNodeSet &SCCNodes) {
573   bool Changed = false;
574 
575   // Check each function in turn, determining if an argument is always returned.
576   for (Function *F : SCCNodes) {
577     // We can infer and propagate function attributes only when we know that the
578     // definition we'll get at link time is *exactly* the definition we see now.
579     // For more details, see GlobalValue::mayBeDerefined.
580     if (!F->hasExactDefinition())
581       continue;
582 
583     if (F->getReturnType()->isVoidTy())
584       continue;
585 
586     // There is nothing to do if an argument is already marked as 'returned'.
587     if (llvm::any_of(F->args(),
588                      [](const Argument &Arg) { return Arg.hasReturnedAttr(); }))
589       continue;
590 
591     auto FindRetArg = [&]() -> Value * {
592       Value *RetArg = nullptr;
593       for (BasicBlock &BB : *F)
594         if (auto *Ret = dyn_cast<ReturnInst>(BB.getTerminator())) {
595           // Note that stripPointerCasts should look through functions with
596           // returned arguments.
597           Value *RetVal = Ret->getReturnValue()->stripPointerCasts();
598           if (!isa<Argument>(RetVal) || RetVal->getType() != F->getReturnType())
599             return nullptr;
600 
601           if (!RetArg)
602             RetArg = RetVal;
603           else if (RetArg != RetVal)
604             return nullptr;
605         }
606 
607       return RetArg;
608     };
609 
610     if (Value *RetArg = FindRetArg()) {
611       auto *A = cast<Argument>(RetArg);
612       A->addAttr(Attribute::Returned);
613       ++NumReturned;
614       Changed = true;
615     }
616   }
617 
618   return Changed;
619 }
620 
621 /// If a callsite has arguments that are also arguments to the parent function,
622 /// try to propagate attributes from the callsite's arguments to the parent's
623 /// arguments. This may be important because inlining can cause information loss
624 /// when attribute knowledge disappears with the inlined call.
625 static bool addArgumentAttrsFromCallsites(Function &F) {
626   if (!EnableNonnullArgPropagation)
627     return false;
628 
629   bool Changed = false;
630 
631   // For an argument attribute to transfer from a callsite to the parent, the
632   // call must be guaranteed to execute every time the parent is called.
633   // Conservatively, just check for calls in the entry block that are guaranteed
634   // to execute.
635   // TODO: This could be enhanced by testing if the callsite post-dominates the
636   // entry block or by doing simple forward walks or backward walks to the
637   // callsite.
638   BasicBlock &Entry = F.getEntryBlock();
639   for (Instruction &I : Entry) {
640     if (auto CS = CallSite(&I)) {
641       if (auto *CalledFunc = CS.getCalledFunction()) {
642         for (auto &CSArg : CalledFunc->args()) {
643           if (!CSArg.hasNonNullAttr())
644             continue;
645 
646           // If the non-null callsite argument operand is an argument to 'F'
647           // (the caller) and the call is guaranteed to execute, then the value
648           // must be non-null throughout 'F'.
649           auto *FArg = dyn_cast<Argument>(CS.getArgOperand(CSArg.getArgNo()));
650           if (FArg && !FArg->hasNonNullAttr()) {
651             FArg->addAttr(Attribute::NonNull);
652             Changed = true;
653           }
654         }
655       }
656     }
657     if (!isGuaranteedToTransferExecutionToSuccessor(&I))
658       break;
659   }
660 
661   return Changed;
662 }
663 
664 /// Deduce nocapture attributes for the SCC.
665 static bool addArgumentAttrs(const SCCNodeSet &SCCNodes) {
666   bool Changed = false;
667 
668   ArgumentGraph AG;
669 
670   // Check each function in turn, determining which pointer arguments are not
671   // captured.
672   for (Function *F : SCCNodes) {
673     // We can infer and propagate function attributes only when we know that the
674     // definition we'll get at link time is *exactly* the definition we see now.
675     // For more details, see GlobalValue::mayBeDerefined.
676     if (!F->hasExactDefinition())
677       continue;
678 
679     Changed |= addArgumentAttrsFromCallsites(*F);
680 
681     // Functions that are readonly (or readnone) and nounwind and don't return
682     // a value can't capture arguments. Don't analyze them.
683     if (F->onlyReadsMemory() && F->doesNotThrow() &&
684         F->getReturnType()->isVoidTy()) {
685       for (Function::arg_iterator A = F->arg_begin(), E = F->arg_end(); A != E;
686            ++A) {
687         if (A->getType()->isPointerTy() && !A->hasNoCaptureAttr()) {
688           A->addAttr(Attribute::NoCapture);
689           ++NumNoCapture;
690           Changed = true;
691         }
692       }
693       continue;
694     }
695 
696     for (Function::arg_iterator A = F->arg_begin(), E = F->arg_end(); A != E;
697          ++A) {
698       if (!A->getType()->isPointerTy())
699         continue;
700       bool HasNonLocalUses = false;
701       if (!A->hasNoCaptureAttr()) {
702         ArgumentUsesTracker Tracker(SCCNodes);
703         PointerMayBeCaptured(&*A, &Tracker);
704         if (!Tracker.Captured) {
705           if (Tracker.Uses.empty()) {
706             // If it's trivially not captured, mark it nocapture now.
707             A->addAttr(Attribute::NoCapture);
708             ++NumNoCapture;
709             Changed = true;
710           } else {
711             // If it's not trivially captured and not trivially not captured,
712             // then it must be calling into another function in our SCC. Save
713             // its particulars for Argument-SCC analysis later.
714             ArgumentGraphNode *Node = AG[&*A];
715             for (Argument *Use : Tracker.Uses) {
716               Node->Uses.push_back(AG[Use]);
717               if (Use != &*A)
718                 HasNonLocalUses = true;
719             }
720           }
721         }
722         // Otherwise, it's captured. Don't bother doing SCC analysis on it.
723       }
724       if (!HasNonLocalUses && !A->onlyReadsMemory()) {
725         // Can we determine that it's readonly/readnone without doing an SCC?
726         // Note that we don't allow any calls at all here, or else our result
727         // will be dependent on the iteration order through the functions in the
728         // SCC.
729         SmallPtrSet<Argument *, 8> Self;
730         Self.insert(&*A);
731         Attribute::AttrKind R = determinePointerReadAttrs(&*A, Self);
732         if (R != Attribute::None) {
733           A->addAttr(R);
734           Changed = true;
735           R == Attribute::ReadOnly ? ++NumReadOnlyArg : ++NumReadNoneArg;
736         }
737       }
738     }
739   }
740 
741   // The graph we've collected is partial because we stopped scanning for
742   // argument uses once we solved the argument trivially. These partial nodes
743   // show up as ArgumentGraphNode objects with an empty Uses list, and for
744   // these nodes the final decision about whether they capture has already been
745   // made.  If the definition doesn't have a 'nocapture' attribute by now, it
746   // captures.
747 
748   for (scc_iterator<ArgumentGraph *> I = scc_begin(&AG); !I.isAtEnd(); ++I) {
749     const std::vector<ArgumentGraphNode *> &ArgumentSCC = *I;
750     if (ArgumentSCC.size() == 1) {
751       if (!ArgumentSCC[0]->Definition)
752         continue; // synthetic root node
753 
754       // eg. "void f(int* x) { if (...) f(x); }"
755       if (ArgumentSCC[0]->Uses.size() == 1 &&
756           ArgumentSCC[0]->Uses[0] == ArgumentSCC[0]) {
757         Argument *A = ArgumentSCC[0]->Definition;
758         A->addAttr(Attribute::NoCapture);
759         ++NumNoCapture;
760         Changed = true;
761       }
762       continue;
763     }
764 
765     bool SCCCaptured = false;
766     for (auto I = ArgumentSCC.begin(), E = ArgumentSCC.end();
767          I != E && !SCCCaptured; ++I) {
768       ArgumentGraphNode *Node = *I;
769       if (Node->Uses.empty()) {
770         if (!Node->Definition->hasNoCaptureAttr())
771           SCCCaptured = true;
772       }
773     }
774     if (SCCCaptured)
775       continue;
776 
777     SmallPtrSet<Argument *, 8> ArgumentSCCNodes;
778     // Fill ArgumentSCCNodes with the elements of the ArgumentSCC.  Used for
779     // quickly looking up whether a given Argument is in this ArgumentSCC.
780     for (ArgumentGraphNode *I : ArgumentSCC) {
781       ArgumentSCCNodes.insert(I->Definition);
782     }
783 
784     for (auto I = ArgumentSCC.begin(), E = ArgumentSCC.end();
785          I != E && !SCCCaptured; ++I) {
786       ArgumentGraphNode *N = *I;
787       for (ArgumentGraphNode *Use : N->Uses) {
788         Argument *A = Use->Definition;
789         if (A->hasNoCaptureAttr() || ArgumentSCCNodes.count(A))
790           continue;
791         SCCCaptured = true;
792         break;
793       }
794     }
795     if (SCCCaptured)
796       continue;
797 
798     for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) {
799       Argument *A = ArgumentSCC[i]->Definition;
800       A->addAttr(Attribute::NoCapture);
801       ++NumNoCapture;
802       Changed = true;
803     }
804 
805     // We also want to compute readonly/readnone. With a small number of false
806     // negatives, we can assume that any pointer which is captured isn't going
807     // to be provably readonly or readnone, since by definition we can't
808     // analyze all uses of a captured pointer.
809     //
810     // The false negatives happen when the pointer is captured by a function
811     // that promises readonly/readnone behaviour on the pointer, then the
812     // pointer's lifetime ends before anything that writes to arbitrary memory.
813     // Also, a readonly/readnone pointer may be returned, but returning a
814     // pointer is capturing it.
815 
816     Attribute::AttrKind ReadAttr = Attribute::ReadNone;
817     for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) {
818       Argument *A = ArgumentSCC[i]->Definition;
819       Attribute::AttrKind K = determinePointerReadAttrs(A, ArgumentSCCNodes);
820       if (K == Attribute::ReadNone)
821         continue;
822       if (K == Attribute::ReadOnly) {
823         ReadAttr = Attribute::ReadOnly;
824         continue;
825       }
826       ReadAttr = K;
827       break;
828     }
829 
830     if (ReadAttr != Attribute::None) {
831       for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) {
832         Argument *A = ArgumentSCC[i]->Definition;
833         // Clear out existing readonly/readnone attributes
834         A->removeAttr(Attribute::ReadOnly);
835         A->removeAttr(Attribute::ReadNone);
836         A->addAttr(ReadAttr);
837         ReadAttr == Attribute::ReadOnly ? ++NumReadOnlyArg : ++NumReadNoneArg;
838         Changed = true;
839       }
840     }
841   }
842 
843   return Changed;
844 }
845 
846 /// Tests whether a function is "malloc-like".
847 ///
848 /// A function is "malloc-like" if it returns either null or a pointer that
849 /// doesn't alias any other pointer visible to the caller.
850 static bool isFunctionMallocLike(Function *F, const SCCNodeSet &SCCNodes) {
851   SmallSetVector<Value *, 8> FlowsToReturn;
852   for (BasicBlock &BB : *F)
853     if (ReturnInst *Ret = dyn_cast<ReturnInst>(BB.getTerminator()))
854       FlowsToReturn.insert(Ret->getReturnValue());
855 
856   for (unsigned i = 0; i != FlowsToReturn.size(); ++i) {
857     Value *RetVal = FlowsToReturn[i];
858 
859     if (Constant *C = dyn_cast<Constant>(RetVal)) {
860       if (!C->isNullValue() && !isa<UndefValue>(C))
861         return false;
862 
863       continue;
864     }
865 
866     if (isa<Argument>(RetVal))
867       return false;
868 
869     if (Instruction *RVI = dyn_cast<Instruction>(RetVal))
870       switch (RVI->getOpcode()) {
871       // Extend the analysis by looking upwards.
872       case Instruction::BitCast:
873       case Instruction::GetElementPtr:
874       case Instruction::AddrSpaceCast:
875         FlowsToReturn.insert(RVI->getOperand(0));
876         continue;
877       case Instruction::Select: {
878         SelectInst *SI = cast<SelectInst>(RVI);
879         FlowsToReturn.insert(SI->getTrueValue());
880         FlowsToReturn.insert(SI->getFalseValue());
881         continue;
882       }
883       case Instruction::PHI: {
884         PHINode *PN = cast<PHINode>(RVI);
885         for (Value *IncValue : PN->incoming_values())
886           FlowsToReturn.insert(IncValue);
887         continue;
888       }
889 
890       // Check whether the pointer came from an allocation.
891       case Instruction::Alloca:
892         break;
893       case Instruction::Call:
894       case Instruction::Invoke: {
895         CallSite CS(RVI);
896         if (CS.hasRetAttr(Attribute::NoAlias))
897           break;
898         if (CS.getCalledFunction() && SCCNodes.count(CS.getCalledFunction()))
899           break;
900         LLVM_FALLTHROUGH;
901       }
902       default:
903         return false; // Did not come from an allocation.
904       }
905 
906     if (PointerMayBeCaptured(RetVal, false, /*StoreCaptures=*/false))
907       return false;
908   }
909 
910   return true;
911 }
912 
913 /// Deduce noalias attributes for the SCC.
914 static bool addNoAliasAttrs(const SCCNodeSet &SCCNodes) {
915   // Check each function in turn, determining which functions return noalias
916   // pointers.
917   for (Function *F : SCCNodes) {
918     // Already noalias.
919     if (F->returnDoesNotAlias())
920       continue;
921 
922     // We can infer and propagate function attributes only when we know that the
923     // definition we'll get at link time is *exactly* the definition we see now.
924     // For more details, see GlobalValue::mayBeDerefined.
925     if (!F->hasExactDefinition())
926       return false;
927 
928     // We annotate noalias return values, which are only applicable to
929     // pointer types.
930     if (!F->getReturnType()->isPointerTy())
931       continue;
932 
933     if (!isFunctionMallocLike(F, SCCNodes))
934       return false;
935   }
936 
937   bool MadeChange = false;
938   for (Function *F : SCCNodes) {
939     if (F->returnDoesNotAlias() ||
940         !F->getReturnType()->isPointerTy())
941       continue;
942 
943     F->setReturnDoesNotAlias();
944     ++NumNoAlias;
945     MadeChange = true;
946   }
947 
948   return MadeChange;
949 }
950 
951 /// Tests whether this function is known to not return null.
952 ///
953 /// Requires that the function returns a pointer.
954 ///
955 /// Returns true if it believes the function will not return a null, and sets
956 /// \p Speculative based on whether the returned conclusion is a speculative
957 /// conclusion due to SCC calls.
958 static bool isReturnNonNull(Function *F, const SCCNodeSet &SCCNodes,
959                             bool &Speculative) {
960   assert(F->getReturnType()->isPointerTy() &&
961          "nonnull only meaningful on pointer types");
962   Speculative = false;
963 
964   SmallSetVector<Value *, 8> FlowsToReturn;
965   for (BasicBlock &BB : *F)
966     if (auto *Ret = dyn_cast<ReturnInst>(BB.getTerminator()))
967       FlowsToReturn.insert(Ret->getReturnValue());
968 
969   auto &DL = F->getParent()->getDataLayout();
970 
971   for (unsigned i = 0; i != FlowsToReturn.size(); ++i) {
972     Value *RetVal = FlowsToReturn[i];
973 
974     // If this value is locally known to be non-null, we're good
975     if (isKnownNonZero(RetVal, DL))
976       continue;
977 
978     // Otherwise, we need to look upwards since we can't make any local
979     // conclusions.
980     Instruction *RVI = dyn_cast<Instruction>(RetVal);
981     if (!RVI)
982       return false;
983     switch (RVI->getOpcode()) {
984     // Extend the analysis by looking upwards.
985     case Instruction::BitCast:
986     case Instruction::GetElementPtr:
987     case Instruction::AddrSpaceCast:
988       FlowsToReturn.insert(RVI->getOperand(0));
989       continue;
990     case Instruction::Select: {
991       SelectInst *SI = cast<SelectInst>(RVI);
992       FlowsToReturn.insert(SI->getTrueValue());
993       FlowsToReturn.insert(SI->getFalseValue());
994       continue;
995     }
996     case Instruction::PHI: {
997       PHINode *PN = cast<PHINode>(RVI);
998       for (int i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
999         FlowsToReturn.insert(PN->getIncomingValue(i));
1000       continue;
1001     }
1002     case Instruction::Call:
1003     case Instruction::Invoke: {
1004       CallSite CS(RVI);
1005       Function *Callee = CS.getCalledFunction();
1006       // A call to a node within the SCC is assumed to return null until
1007       // proven otherwise
1008       if (Callee && SCCNodes.count(Callee)) {
1009         Speculative = true;
1010         continue;
1011       }
1012       return false;
1013     }
1014     default:
1015       return false; // Unknown source, may be null
1016     };
1017     llvm_unreachable("should have either continued or returned");
1018   }
1019 
1020   return true;
1021 }
1022 
1023 /// Deduce nonnull attributes for the SCC.
1024 static bool addNonNullAttrs(const SCCNodeSet &SCCNodes) {
1025   // Speculative that all functions in the SCC return only nonnull
1026   // pointers.  We may refute this as we analyze functions.
1027   bool SCCReturnsNonNull = true;
1028 
1029   bool MadeChange = false;
1030 
1031   // Check each function in turn, determining which functions return nonnull
1032   // pointers.
1033   for (Function *F : SCCNodes) {
1034     // Already nonnull.
1035     if (F->getAttributes().hasAttribute(AttributeList::ReturnIndex,
1036                                         Attribute::NonNull))
1037       continue;
1038 
1039     // We can infer and propagate function attributes only when we know that the
1040     // definition we'll get at link time is *exactly* the definition we see now.
1041     // For more details, see GlobalValue::mayBeDerefined.
1042     if (!F->hasExactDefinition())
1043       return false;
1044 
1045     // We annotate nonnull return values, which are only applicable to
1046     // pointer types.
1047     if (!F->getReturnType()->isPointerTy())
1048       continue;
1049 
1050     bool Speculative = false;
1051     if (isReturnNonNull(F, SCCNodes, Speculative)) {
1052       if (!Speculative) {
1053         // Mark the function eagerly since we may discover a function
1054         // which prevents us from speculating about the entire SCC
1055         LLVM_DEBUG(dbgs() << "Eagerly marking " << F->getName()
1056                           << " as nonnull\n");
1057         F->addAttribute(AttributeList::ReturnIndex, Attribute::NonNull);
1058         ++NumNonNullReturn;
1059         MadeChange = true;
1060       }
1061       continue;
1062     }
1063     // At least one function returns something which could be null, can't
1064     // speculate any more.
1065     SCCReturnsNonNull = false;
1066   }
1067 
1068   if (SCCReturnsNonNull) {
1069     for (Function *F : SCCNodes) {
1070       if (F->getAttributes().hasAttribute(AttributeList::ReturnIndex,
1071                                           Attribute::NonNull) ||
1072           !F->getReturnType()->isPointerTy())
1073         continue;
1074 
1075       LLVM_DEBUG(dbgs() << "SCC marking " << F->getName() << " as nonnull\n");
1076       F->addAttribute(AttributeList::ReturnIndex, Attribute::NonNull);
1077       ++NumNonNullReturn;
1078       MadeChange = true;
1079     }
1080   }
1081 
1082   return MadeChange;
1083 }
1084 
1085 namespace {
1086 
1087 /// Collects a set of attribute inference requests and performs them all in one
1088 /// go on a single SCC Node. Inference involves scanning function bodies
1089 /// looking for instructions that violate attribute assumptions.
1090 /// As soon as all the bodies are fine we are free to set the attribute.
1091 /// Customization of inference for individual attributes is performed by
1092 /// providing a handful of predicates for each attribute.
1093 class AttributeInferer {
1094 public:
1095   /// Describes a request for inference of a single attribute.
1096   struct InferenceDescriptor {
1097 
1098     /// Returns true if this function does not have to be handled.
1099     /// General intent for this predicate is to provide an optimization
1100     /// for functions that do not need this attribute inference at all
1101     /// (say, for functions that already have the attribute).
1102     std::function<bool(const Function &)> SkipFunction;
1103 
1104     /// Returns true if this instruction violates attribute assumptions.
1105     std::function<bool(Instruction &)> InstrBreaksAttribute;
1106 
1107     /// Sets the inferred attribute for this function.
1108     std::function<void(Function &)> SetAttribute;
1109 
1110     /// Attribute we derive.
1111     Attribute::AttrKind AKind;
1112 
1113     /// If true, only "exact" definitions can be used to infer this attribute.
1114     /// See GlobalValue::isDefinitionExact.
1115     bool RequiresExactDefinition;
1116 
1117     InferenceDescriptor(Attribute::AttrKind AK,
1118                         std::function<bool(const Function &)> SkipFunc,
1119                         std::function<bool(Instruction &)> InstrScan,
1120                         std::function<void(Function &)> SetAttr,
1121                         bool ReqExactDef)
1122         : SkipFunction(SkipFunc), InstrBreaksAttribute(InstrScan),
1123           SetAttribute(SetAttr), AKind(AK),
1124           RequiresExactDefinition(ReqExactDef) {}
1125   };
1126 
1127 private:
1128   SmallVector<InferenceDescriptor, 4> InferenceDescriptors;
1129 
1130 public:
1131   void registerAttrInference(InferenceDescriptor AttrInference) {
1132     InferenceDescriptors.push_back(AttrInference);
1133   }
1134 
1135   bool run(const SCCNodeSet &SCCNodes);
1136 };
1137 
1138 /// Perform all the requested attribute inference actions according to the
1139 /// attribute predicates stored before.
1140 bool AttributeInferer::run(const SCCNodeSet &SCCNodes) {
1141   SmallVector<InferenceDescriptor, 4> InferInSCC = InferenceDescriptors;
1142   // Go through all the functions in SCC and check corresponding attribute
1143   // assumptions for each of them. Attributes that are invalid for this SCC
1144   // will be removed from InferInSCC.
1145   for (Function *F : SCCNodes) {
1146 
1147     // No attributes whose assumptions are still valid - done.
1148     if (InferInSCC.empty())
1149       return false;
1150 
1151     // Check if our attributes ever need scanning/can be scanned.
1152     llvm::erase_if(InferInSCC, [F](const InferenceDescriptor &ID) {
1153       if (ID.SkipFunction(*F))
1154         return false;
1155 
1156       // Remove from further inference (invalidate) when visiting a function
1157       // that has no instructions to scan/has an unsuitable definition.
1158       return F->isDeclaration() ||
1159              (ID.RequiresExactDefinition && !F->hasExactDefinition());
1160     });
1161 
1162     // For each attribute still in InferInSCC that doesn't explicitly skip F,
1163     // set up the F instructions scan to verify assumptions of the attribute.
1164     SmallVector<InferenceDescriptor, 4> InferInThisFunc;
1165     llvm::copy_if(
1166         InferInSCC, std::back_inserter(InferInThisFunc),
1167         [F](const InferenceDescriptor &ID) { return !ID.SkipFunction(*F); });
1168 
1169     if (InferInThisFunc.empty())
1170       continue;
1171 
1172     // Start instruction scan.
1173     for (Instruction &I : instructions(*F)) {
1174       llvm::erase_if(InferInThisFunc, [&](const InferenceDescriptor &ID) {
1175         if (!ID.InstrBreaksAttribute(I))
1176           return false;
1177         // Remove attribute from further inference on any other functions
1178         // because attribute assumptions have just been violated.
1179         llvm::erase_if(InferInSCC, [&ID](const InferenceDescriptor &D) {
1180           return D.AKind == ID.AKind;
1181         });
1182         // Remove attribute from the rest of current instruction scan.
1183         return true;
1184       });
1185 
1186       if (InferInThisFunc.empty())
1187         break;
1188     }
1189   }
1190 
1191   if (InferInSCC.empty())
1192     return false;
1193 
1194   bool Changed = false;
1195   for (Function *F : SCCNodes)
1196     // At this point InferInSCC contains only functions that were either:
1197     //   - explicitly skipped from scan/inference, or
1198     //   - verified to have no instructions that break attribute assumptions.
1199     // Hence we just go and force the attribute for all non-skipped functions.
1200     for (auto &ID : InferInSCC) {
1201       if (ID.SkipFunction(*F))
1202         continue;
1203       Changed = true;
1204       ID.SetAttribute(*F);
1205     }
1206   return Changed;
1207 }
1208 
1209 } // end anonymous namespace
1210 
1211 /// Helper for non-Convergent inference predicate InstrBreaksAttribute.
1212 static bool InstrBreaksNonConvergent(Instruction &I,
1213                                      const SCCNodeSet &SCCNodes) {
1214   const CallSite CS(&I);
1215   // Breaks non-convergent assumption if CS is a convergent call to a function
1216   // not in the SCC.
1217   return CS && CS.isConvergent() && SCCNodes.count(CS.getCalledFunction()) == 0;
1218 }
1219 
1220 /// Helper for NoUnwind inference predicate InstrBreaksAttribute.
1221 static bool InstrBreaksNonThrowing(Instruction &I, const SCCNodeSet &SCCNodes) {
1222   if (!I.mayThrow())
1223     return false;
1224   if (const auto *CI = dyn_cast<CallInst>(&I)) {
1225     if (Function *Callee = CI->getCalledFunction()) {
1226       // I is a may-throw call to a function inside our SCC. This doesn't
1227       // invalidate our current working assumption that the SCC is no-throw; we
1228       // just have to scan that other function.
1229       if (SCCNodes.count(Callee) > 0)
1230         return false;
1231     }
1232   }
1233   return true;
1234 }
1235 
1236 /// Helper for NoFree inference predicate InstrBreaksAttribute.
1237 static bool InstrBreaksNoFree(Instruction &I, const SCCNodeSet &SCCNodes) {
1238   CallSite CS(&I);
1239   if (!CS)
1240     return false;
1241 
1242   Function *Callee = CS.getCalledFunction();
1243   if (!Callee)
1244     return true;
1245 
1246   if (Callee->doesNotFreeMemory())
1247     return false;
1248 
1249   if (SCCNodes.count(Callee) > 0)
1250     return false;
1251 
1252   return true;
1253 }
1254 
1255 /// Infer attributes from all functions in the SCC by scanning every
1256 /// instruction for compliance to the attribute assumptions. Currently it
1257 /// does:
1258 ///   - removal of Convergent attribute
1259 ///   - addition of NoUnwind attribute
1260 ///
1261 /// Returns true if any changes to function attributes were made.
1262 static bool inferAttrsFromFunctionBodies(const SCCNodeSet &SCCNodes) {
1263 
1264   AttributeInferer AI;
1265 
1266   // Request to remove the convergent attribute from all functions in the SCC
1267   // if every callsite within the SCC is not convergent (except for calls
1268   // to functions within the SCC).
1269   // Note: Removal of the attr from the callsites will happen in
1270   // InstCombineCalls separately.
1271   AI.registerAttrInference(AttributeInferer::InferenceDescriptor{
1272       Attribute::Convergent,
1273       // Skip non-convergent functions.
1274       [](const Function &F) { return !F.isConvergent(); },
1275       // Instructions that break non-convergent assumption.
1276       [SCCNodes](Instruction &I) {
1277         return InstrBreaksNonConvergent(I, SCCNodes);
1278       },
1279       [](Function &F) {
1280         LLVM_DEBUG(dbgs() << "Removing convergent attr from fn " << F.getName()
1281                           << "\n");
1282         F.setNotConvergent();
1283       },
1284       /* RequiresExactDefinition= */ false});
1285 
1286   if (!DisableNoUnwindInference)
1287     // Request to infer nounwind attribute for all the functions in the SCC if
1288     // every callsite within the SCC is not throwing (except for calls to
1289     // functions within the SCC). Note that nounwind attribute suffers from
1290     // derefinement - results may change depending on how functions are
1291     // optimized. Thus it can be inferred only from exact definitions.
1292     AI.registerAttrInference(AttributeInferer::InferenceDescriptor{
1293         Attribute::NoUnwind,
1294         // Skip non-throwing functions.
1295         [](const Function &F) { return F.doesNotThrow(); },
1296         // Instructions that break non-throwing assumption.
1297         [SCCNodes](Instruction &I) {
1298           return InstrBreaksNonThrowing(I, SCCNodes);
1299         },
1300         [](Function &F) {
1301           LLVM_DEBUG(dbgs()
1302                      << "Adding nounwind attr to fn " << F.getName() << "\n");
1303           F.setDoesNotThrow();
1304           ++NumNoUnwind;
1305         },
1306         /* RequiresExactDefinition= */ true});
1307 
1308   if (!DisableNoFreeInference)
1309     // Request to infer nofree attribute for all the functions in the SCC if
1310     // every callsite within the SCC does not directly or indirectly free
1311     // memory (except for calls to functions within the SCC). Note that nofree
1312     // attribute suffers from derefinement - results may change depending on
1313     // how functions are optimized. Thus it can be inferred only from exact
1314     // definitions.
1315     AI.registerAttrInference(AttributeInferer::InferenceDescriptor{
1316         Attribute::NoFree,
1317         // Skip functions known not to free memory.
1318         [](const Function &F) { return F.doesNotFreeMemory(); },
1319         // Instructions that break non-deallocating assumption.
1320         [SCCNodes](Instruction &I) {
1321           return InstrBreaksNoFree(I, SCCNodes);
1322         },
1323         [](Function &F) {
1324           LLVM_DEBUG(dbgs()
1325                      << "Adding nofree attr to fn " << F.getName() << "\n");
1326           F.setDoesNotFreeMemory();
1327           ++NumNoFree;
1328         },
1329         /* RequiresExactDefinition= */ true});
1330 
1331   // Perform all the requested attribute inference actions.
1332   return AI.run(SCCNodes);
1333 }
1334 
1335 static bool setDoesNotRecurse(Function &F) {
1336   if (F.doesNotRecurse())
1337     return false;
1338   F.setDoesNotRecurse();
1339   ++NumNoRecurse;
1340   return true;
1341 }
1342 
1343 static bool addNoRecurseAttrs(const SCCNodeSet &SCCNodes) {
1344   // Try and identify functions that do not recurse.
1345 
1346   // If the SCC contains multiple nodes we know for sure there is recursion.
1347   if (SCCNodes.size() != 1)
1348     return false;
1349 
1350   Function *F = *SCCNodes.begin();
1351   if (!F || !F->hasExactDefinition() || F->doesNotRecurse())
1352     return false;
1353 
1354   // If all of the calls in F are identifiable and are to norecurse functions, F
1355   // is norecurse. This check also detects self-recursion as F is not currently
1356   // marked norecurse, so any called from F to F will not be marked norecurse.
1357   for (auto &BB : *F)
1358     for (auto &I : BB.instructionsWithoutDebug())
1359       if (auto CS = CallSite(&I)) {
1360         Function *Callee = CS.getCalledFunction();
1361         if (!Callee || Callee == F || !Callee->doesNotRecurse())
1362           // Function calls a potentially recursive function.
1363           return false;
1364       }
1365 
1366   // Every call was to a non-recursive function other than this function, and
1367   // we have no indirect recursion as the SCC size is one. This function cannot
1368   // recurse.
1369   return setDoesNotRecurse(*F);
1370 }
1371 
1372 template <typename AARGetterT>
1373 static bool deriveAttrsInPostOrder(SCCNodeSet &SCCNodes,
1374                                    AARGetterT &&AARGetter,
1375                                    bool HasUnknownCall) {
1376   bool Changed = false;
1377 
1378   // Bail if the SCC only contains optnone functions.
1379   if (SCCNodes.empty())
1380     return Changed;
1381 
1382   Changed |= addArgumentReturnedAttrs(SCCNodes);
1383   Changed |= addReadAttrs(SCCNodes, AARGetter);
1384   Changed |= addArgumentAttrs(SCCNodes);
1385 
1386   // If we have no external nodes participating in the SCC, we can deduce some
1387   // more precise attributes as well.
1388   if (!HasUnknownCall) {
1389     Changed |= addNoAliasAttrs(SCCNodes);
1390     Changed |= addNonNullAttrs(SCCNodes);
1391     Changed |= inferAttrsFromFunctionBodies(SCCNodes);
1392     Changed |= addNoRecurseAttrs(SCCNodes);
1393   }
1394 
1395   return Changed;
1396 }
1397 
1398 PreservedAnalyses PostOrderFunctionAttrsPass::run(LazyCallGraph::SCC &C,
1399                                                   CGSCCAnalysisManager &AM,
1400                                                   LazyCallGraph &CG,
1401                                                   CGSCCUpdateResult &) {
1402   FunctionAnalysisManager &FAM =
1403       AM.getResult<FunctionAnalysisManagerCGSCCProxy>(C, CG).getManager();
1404 
1405   // We pass a lambda into functions to wire them up to the analysis manager
1406   // for getting function analyses.
1407   auto AARGetter = [&](Function &F) -> AAResults & {
1408     return FAM.getResult<AAManager>(F);
1409   };
1410 
1411   // Fill SCCNodes with the elements of the SCC. Also track whether there are
1412   // any external or opt-none nodes that will prevent us from optimizing any
1413   // part of the SCC.
1414   SCCNodeSet SCCNodes;
1415   bool HasUnknownCall = false;
1416   for (LazyCallGraph::Node &N : C) {
1417     Function &F = N.getFunction();
1418     if (F.hasOptNone() || F.hasFnAttribute(Attribute::Naked)) {
1419       // Treat any function we're trying not to optimize as if it were an
1420       // indirect call and omit it from the node set used below.
1421       HasUnknownCall = true;
1422       continue;
1423     }
1424     // Track whether any functions in this SCC have an unknown call edge.
1425     // Note: if this is ever a performance hit, we can common it with
1426     // subsequent routines which also do scans over the instructions of the
1427     // function.
1428     if (!HasUnknownCall)
1429       for (Instruction &I : instructions(F))
1430         if (auto CS = CallSite(&I))
1431           if (!CS.getCalledFunction()) {
1432             HasUnknownCall = true;
1433             break;
1434           }
1435 
1436     SCCNodes.insert(&F);
1437   }
1438 
1439   if (deriveAttrsInPostOrder(SCCNodes, AARGetter, HasUnknownCall))
1440     return PreservedAnalyses::none();
1441 
1442   return PreservedAnalyses::all();
1443 }
1444 
1445 namespace {
1446 
1447 struct PostOrderFunctionAttrsLegacyPass : public CallGraphSCCPass {
1448   // Pass identification, replacement for typeid
1449   static char ID;
1450 
1451   PostOrderFunctionAttrsLegacyPass() : CallGraphSCCPass(ID) {
1452     initializePostOrderFunctionAttrsLegacyPassPass(
1453         *PassRegistry::getPassRegistry());
1454   }
1455 
1456   bool runOnSCC(CallGraphSCC &SCC) override;
1457 
1458   void getAnalysisUsage(AnalysisUsage &AU) const override {
1459     AU.setPreservesCFG();
1460     AU.addRequired<AssumptionCacheTracker>();
1461     getAAResultsAnalysisUsage(AU);
1462     CallGraphSCCPass::getAnalysisUsage(AU);
1463   }
1464 };
1465 
1466 } // end anonymous namespace
1467 
1468 char PostOrderFunctionAttrsLegacyPass::ID = 0;
1469 INITIALIZE_PASS_BEGIN(PostOrderFunctionAttrsLegacyPass, "functionattrs",
1470                       "Deduce function attributes", false, false)
1471 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
1472 INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
1473 INITIALIZE_PASS_END(PostOrderFunctionAttrsLegacyPass, "functionattrs",
1474                     "Deduce function attributes", false, false)
1475 
1476 Pass *llvm::createPostOrderFunctionAttrsLegacyPass() {
1477   return new PostOrderFunctionAttrsLegacyPass();
1478 }
1479 
1480 template <typename AARGetterT>
1481 static bool runImpl(CallGraphSCC &SCC, AARGetterT AARGetter) {
1482 
1483   // Fill SCCNodes with the elements of the SCC. Used for quickly looking up
1484   // whether a given CallGraphNode is in this SCC. Also track whether there are
1485   // any external or opt-none nodes that will prevent us from optimizing any
1486   // part of the SCC.
1487   SCCNodeSet SCCNodes;
1488   bool ExternalNode = false;
1489   for (CallGraphNode *I : SCC) {
1490     Function *F = I->getFunction();
1491     if (!F || F->hasOptNone() || F->hasFnAttribute(Attribute::Naked)) {
1492       // External node or function we're trying not to optimize - we both avoid
1493       // transform them and avoid leveraging information they provide.
1494       ExternalNode = true;
1495       continue;
1496     }
1497 
1498     SCCNodes.insert(F);
1499   }
1500 
1501   return deriveAttrsInPostOrder(SCCNodes, AARGetter, ExternalNode);
1502 }
1503 
1504 bool PostOrderFunctionAttrsLegacyPass::runOnSCC(CallGraphSCC &SCC) {
1505   if (skipSCC(SCC))
1506     return false;
1507   return runImpl(SCC, LegacyAARGetter(*this));
1508 }
1509 
1510 namespace {
1511 
1512 struct ReversePostOrderFunctionAttrsLegacyPass : public ModulePass {
1513   // Pass identification, replacement for typeid
1514   static char ID;
1515 
1516   ReversePostOrderFunctionAttrsLegacyPass() : ModulePass(ID) {
1517     initializeReversePostOrderFunctionAttrsLegacyPassPass(
1518         *PassRegistry::getPassRegistry());
1519   }
1520 
1521   bool runOnModule(Module &M) override;
1522 
1523   void getAnalysisUsage(AnalysisUsage &AU) const override {
1524     AU.setPreservesCFG();
1525     AU.addRequired<CallGraphWrapperPass>();
1526     AU.addPreserved<CallGraphWrapperPass>();
1527   }
1528 };
1529 
1530 } // end anonymous namespace
1531 
1532 char ReversePostOrderFunctionAttrsLegacyPass::ID = 0;
1533 
1534 INITIALIZE_PASS_BEGIN(ReversePostOrderFunctionAttrsLegacyPass, "rpo-functionattrs",
1535                       "Deduce function attributes in RPO", false, false)
1536 INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
1537 INITIALIZE_PASS_END(ReversePostOrderFunctionAttrsLegacyPass, "rpo-functionattrs",
1538                     "Deduce function attributes in RPO", false, false)
1539 
1540 Pass *llvm::createReversePostOrderFunctionAttrsPass() {
1541   return new ReversePostOrderFunctionAttrsLegacyPass();
1542 }
1543 
1544 static bool addNoRecurseAttrsTopDown(Function &F) {
1545   // We check the preconditions for the function prior to calling this to avoid
1546   // the cost of building up a reversible post-order list. We assert them here
1547   // to make sure none of the invariants this relies on were violated.
1548   assert(!F.isDeclaration() && "Cannot deduce norecurse without a definition!");
1549   assert(!F.doesNotRecurse() &&
1550          "This function has already been deduced as norecurs!");
1551   assert(F.hasInternalLinkage() &&
1552          "Can only do top-down deduction for internal linkage functions!");
1553 
1554   // If F is internal and all of its uses are calls from a non-recursive
1555   // functions, then none of its calls could in fact recurse without going
1556   // through a function marked norecurse, and so we can mark this function too
1557   // as norecurse. Note that the uses must actually be calls -- otherwise
1558   // a pointer to this function could be returned from a norecurse function but
1559   // this function could be recursively (indirectly) called. Note that this
1560   // also detects if F is directly recursive as F is not yet marked as
1561   // a norecurse function.
1562   for (auto *U : F.users()) {
1563     auto *I = dyn_cast<Instruction>(U);
1564     if (!I)
1565       return false;
1566     CallSite CS(I);
1567     if (!CS || !CS.getParent()->getParent()->doesNotRecurse())
1568       return false;
1569   }
1570   return setDoesNotRecurse(F);
1571 }
1572 
1573 static bool deduceFunctionAttributeInRPO(Module &M, CallGraph &CG) {
1574   // We only have a post-order SCC traversal (because SCCs are inherently
1575   // discovered in post-order), so we accumulate them in a vector and then walk
1576   // it in reverse. This is simpler than using the RPO iterator infrastructure
1577   // because we need to combine SCC detection and the PO walk of the call
1578   // graph. We can also cheat egregiously because we're primarily interested in
1579   // synthesizing norecurse and so we can only save the singular SCCs as SCCs
1580   // with multiple functions in them will clearly be recursive.
1581   SmallVector<Function *, 16> Worklist;
1582   for (scc_iterator<CallGraph *> I = scc_begin(&CG); !I.isAtEnd(); ++I) {
1583     if (I->size() != 1)
1584       continue;
1585 
1586     Function *F = I->front()->getFunction();
1587     if (F && !F->isDeclaration() && !F->doesNotRecurse() &&
1588         F->hasInternalLinkage())
1589       Worklist.push_back(F);
1590   }
1591 
1592   bool Changed = false;
1593   for (auto *F : llvm::reverse(Worklist))
1594     Changed |= addNoRecurseAttrsTopDown(*F);
1595 
1596   return Changed;
1597 }
1598 
1599 bool ReversePostOrderFunctionAttrsLegacyPass::runOnModule(Module &M) {
1600   if (skipModule(M))
1601     return false;
1602 
1603   auto &CG = getAnalysis<CallGraphWrapperPass>().getCallGraph();
1604 
1605   return deduceFunctionAttributeInRPO(M, CG);
1606 }
1607 
1608 PreservedAnalyses
1609 ReversePostOrderFunctionAttrsPass::run(Module &M, ModuleAnalysisManager &AM) {
1610   auto &CG = AM.getResult<CallGraphAnalysis>(M);
1611 
1612   if (!deduceFunctionAttributeInRPO(M, CG))
1613     return PreservedAnalyses::all();
1614 
1615   PreservedAnalyses PA;
1616   PA.preserve<CallGraphAnalysis>();
1617   return PA;
1618 }
1619