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