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