[attrs] Split off the forced attributes utility into its own pass that
[oota-llvm.git] / lib / Transforms / IPO / FunctionAttrs.cpp
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 // This file implements a simple interprocedural pass which walks the
11 // call-graph, looking for functions which do not access or only read
12 // non-local memory, and marking them readnone/readonly.  It does the
13 // same with function arguments independently, marking them readonly/
14 // readnone/nocapture.  Finally, well-known library call declarations
15 // are marked with all attributes that are consistent with the
16 // function's standard definition. This pass is implemented as a
17 // bottom-up traversal of the call-graph.
18 //
19 //===----------------------------------------------------------------------===//
20
21 #include "llvm/Transforms/IPO.h"
22 #include "llvm/ADT/SCCIterator.h"
23 #include "llvm/ADT/SetVector.h"
24 #include "llvm/ADT/SmallSet.h"
25 #include "llvm/ADT/Statistic.h"
26 #include "llvm/ADT/StringSwitch.h"
27 #include "llvm/Analysis/AliasAnalysis.h"
28 #include "llvm/Analysis/AssumptionCache.h"
29 #include "llvm/Analysis/BasicAliasAnalysis.h"
30 #include "llvm/Analysis/CallGraph.h"
31 #include "llvm/Analysis/CallGraphSCCPass.h"
32 #include "llvm/Analysis/CaptureTracking.h"
33 #include "llvm/Analysis/TargetLibraryInfo.h"
34 #include "llvm/Analysis/ValueTracking.h"
35 #include "llvm/IR/GlobalVariable.h"
36 #include "llvm/IR/InstIterator.h"
37 #include "llvm/IR/IntrinsicInst.h"
38 #include "llvm/IR/LLVMContext.h"
39 #include "llvm/Support/Debug.h"
40 #include "llvm/Support/raw_ostream.h"
41 #include "llvm/Analysis/TargetLibraryInfo.h"
42 using namespace llvm;
43
44 #define DEBUG_TYPE "functionattrs"
45
46 STATISTIC(NumReadNone, "Number of functions marked readnone");
47 STATISTIC(NumReadOnly, "Number of functions marked readonly");
48 STATISTIC(NumNoCapture, "Number of arguments marked nocapture");
49 STATISTIC(NumReadNoneArg, "Number of arguments marked readnone");
50 STATISTIC(NumReadOnlyArg, "Number of arguments marked readonly");
51 STATISTIC(NumNoAlias, "Number of function returns marked noalias");
52 STATISTIC(NumNonNullReturn, "Number of function returns marked nonnull");
53 STATISTIC(NumAnnotated, "Number of attributes added to library functions");
54 STATISTIC(NumNoRecurse, "Number of functions marked as norecurse");
55
56 namespace {
57 typedef SmallSetVector<Function *, 8> SCCNodeSet;
58 }
59
60 namespace {
61 struct FunctionAttrs : public CallGraphSCCPass {
62   static char ID; // Pass identification, replacement for typeid
63   FunctionAttrs() : CallGraphSCCPass(ID) {
64     initializeFunctionAttrsPass(*PassRegistry::getPassRegistry());
65   }
66
67   bool runOnSCC(CallGraphSCC &SCC) override;
68   bool doInitialization(CallGraph &CG) override {
69     Revisit.clear();
70     return false;
71   }
72   bool doFinalization(CallGraph &CG) override;
73   
74   void getAnalysisUsage(AnalysisUsage &AU) const override {
75     AU.setPreservesCFG();
76     AU.addRequired<AssumptionCacheTracker>();
77     AU.addRequired<TargetLibraryInfoWrapperPass>();
78     CallGraphSCCPass::getAnalysisUsage(AU);
79   }
80
81 private:
82   TargetLibraryInfo *TLI;
83   SmallVector<WeakVH,16> Revisit;
84 };
85 }
86
87 char FunctionAttrs::ID = 0;
88 INITIALIZE_PASS_BEGIN(FunctionAttrs, "functionattrs",
89                       "Deduce function attributes", false, false)
90 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
91 INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
92 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
93 INITIALIZE_PASS_END(FunctionAttrs, "functionattrs",
94                     "Deduce function attributes", false, false)
95
96 Pass *llvm::createFunctionAttrsPass() { return new FunctionAttrs(); }
97
98 namespace {
99 /// The three kinds of memory access relevant to 'readonly' and
100 /// 'readnone' attributes.
101 enum MemoryAccessKind {
102   MAK_ReadNone = 0,
103   MAK_ReadOnly = 1,
104   MAK_MayWrite = 2
105 };
106 }
107
108 static MemoryAccessKind checkFunctionMemoryAccess(Function &F, AAResults &AAR,
109                                                   const SCCNodeSet &SCCNodes) {
110   FunctionModRefBehavior MRB = AAR.getModRefBehavior(&F);
111   if (MRB == FMRB_DoesNotAccessMemory)
112     // Already perfect!
113     return MAK_ReadNone;
114
115   // Definitions with weak linkage may be overridden at linktime with
116   // something that writes memory, so treat them like declarations.
117   if (F.isDeclaration() || F.mayBeOverridden()) {
118     if (AliasAnalysis::onlyReadsMemory(MRB))
119       return MAK_ReadOnly;
120
121     // Conservatively assume it writes to memory.
122     return MAK_MayWrite;
123   }
124
125   // Scan the function body for instructions that may read or write memory.
126   bool ReadsMemory = 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.
135       if (CS.getCalledFunction() && SCCNodes.count(CS.getCalledFunction()))
136         continue;
137       FunctionModRefBehavior MRB = AAR.getModRefBehavior(CS);
138
139       // If the call doesn't access memory, we're done.
140       if (!(MRB & MRI_ModRef))
141         continue;
142
143       if (!AliasAnalysis::onlyAccessesArgPointees(MRB)) {
144         // The call could access any memory. If that includes writes, give up.
145         if (MRB & MRI_Mod)
146           return MAK_MayWrite;
147         // If it reads, note it.
148         if (MRB & MRI_Ref)
149           ReadsMemory = true;
150         continue;
151       }
152
153       // Check whether all pointer arguments point to local memory, and
154       // ignore calls that only access local memory.
155       for (CallSite::arg_iterator CI = CS.arg_begin(), CE = CS.arg_end();
156            CI != CE; ++CI) {
157         Value *Arg = *CI;
158         if (!Arg->getType()->isPtrOrPtrVectorTy())
159           continue;
160
161         AAMDNodes AAInfo;
162         I->getAAMetadata(AAInfo);
163         MemoryLocation Loc(Arg, MemoryLocation::UnknownSize, AAInfo);
164
165         // Skip accesses to local or constant memory as they don't impact the
166         // externally visible mod/ref behavior.
167         if (AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true))
168           continue;
169
170         if (MRB & MRI_Mod)
171           // Writes non-local memory.  Give up.
172           return MAK_MayWrite;
173         if (MRB & MRI_Ref)
174           // Ok, it reads non-local memory.
175           ReadsMemory = true;
176       }
177       continue;
178     } else if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
179       // Ignore non-volatile loads from local memory. (Atomic is okay here.)
180       if (!LI->isVolatile()) {
181         MemoryLocation Loc = MemoryLocation::get(LI);
182         if (AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true))
183           continue;
184       }
185     } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
186       // Ignore non-volatile stores to local memory. (Atomic is okay here.)
187       if (!SI->isVolatile()) {
188         MemoryLocation Loc = MemoryLocation::get(SI);
189         if (AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true))
190           continue;
191       }
192     } else if (VAArgInst *VI = dyn_cast<VAArgInst>(I)) {
193       // Ignore vaargs on local memory.
194       MemoryLocation Loc = MemoryLocation::get(VI);
195       if (AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true))
196         continue;
197     }
198
199     // Any remaining instructions need to be taken seriously!  Check if they
200     // read or write memory.
201     if (I->mayWriteToMemory())
202       // Writes memory.  Just give up.
203       return MAK_MayWrite;
204
205     // If this instruction may read memory, remember that.
206     ReadsMemory |= I->mayReadFromMemory();
207   }
208
209   return ReadsMemory ? MAK_ReadOnly : MAK_ReadNone;
210 }
211
212 /// Deduce readonly/readnone attributes for the SCC.
213 template <typename AARGetterT>
214 static bool addReadAttrs(const SCCNodeSet &SCCNodes, AARGetterT AARGetter) {
215   // Check if any of the functions in the SCC read or write memory.  If they
216   // write memory then they can't be marked readnone or readonly.
217   bool ReadsMemory = false;
218   for (Function *F : SCCNodes) {
219     // Call the callable parameter to look up AA results for this function.
220     AAResults &AAR = AARGetter(*F);
221
222     switch (checkFunctionMemoryAccess(*F, AAR, SCCNodes)) {
223     case MAK_MayWrite:
224       return false;
225     case MAK_ReadOnly:
226       ReadsMemory = true;
227       break;
228     case MAK_ReadNone:
229       // Nothing to do!
230       break;
231     }
232   }
233
234   // Success!  Functions in this SCC do not access memory, or only read memory.
235   // Give them the appropriate attribute.
236   bool MadeChange = false;
237   for (Function *F : SCCNodes) {
238     if (F->doesNotAccessMemory())
239       // Already perfect!
240       continue;
241
242     if (F->onlyReadsMemory() && ReadsMemory)
243       // No change.
244       continue;
245
246     MadeChange = true;
247
248     // Clear out any existing attributes.
249     AttrBuilder B;
250     B.addAttribute(Attribute::ReadOnly).addAttribute(Attribute::ReadNone);
251     F->removeAttributes(
252         AttributeSet::FunctionIndex,
253         AttributeSet::get(F->getContext(), AttributeSet::FunctionIndex, B));
254
255     // Add in the new attribute.
256     F->addAttribute(AttributeSet::FunctionIndex,
257                     ReadsMemory ? Attribute::ReadOnly : Attribute::ReadNone);
258
259     if (ReadsMemory)
260       ++NumReadOnly;
261     else
262       ++NumReadNone;
263   }
264
265   return MadeChange;
266 }
267
268 namespace {
269 /// For a given pointer Argument, this retains a list of Arguments of functions
270 /// in the same SCC that the pointer data flows into. We use this to build an
271 /// SCC of the arguments.
272 struct ArgumentGraphNode {
273   Argument *Definition;
274   SmallVector<ArgumentGraphNode *, 4> Uses;
275 };
276
277 class ArgumentGraph {
278   // We store pointers to ArgumentGraphNode objects, so it's important that
279   // that they not move around upon insert.
280   typedef std::map<Argument *, ArgumentGraphNode> ArgumentMapTy;
281
282   ArgumentMapTy ArgumentMap;
283
284   // There is no root node for the argument graph, in fact:
285   //   void f(int *x, int *y) { if (...) f(x, y); }
286   // is an example where the graph is disconnected. The SCCIterator requires a
287   // single entry point, so we maintain a fake ("synthetic") root node that
288   // uses every node. Because the graph is directed and nothing points into
289   // the root, it will not participate in any SCCs (except for its own).
290   ArgumentGraphNode SyntheticRoot;
291
292 public:
293   ArgumentGraph() { SyntheticRoot.Definition = nullptr; }
294
295   typedef SmallVectorImpl<ArgumentGraphNode *>::iterator iterator;
296
297   iterator begin() { return SyntheticRoot.Uses.begin(); }
298   iterator end() { return SyntheticRoot.Uses.end(); }
299   ArgumentGraphNode *getEntryNode() { return &SyntheticRoot; }
300
301   ArgumentGraphNode *operator[](Argument *A) {
302     ArgumentGraphNode &Node = ArgumentMap[A];
303     Node.Definition = A;
304     SyntheticRoot.Uses.push_back(&Node);
305     return &Node;
306   }
307 };
308
309 /// This tracker checks whether callees are in the SCC, and if so it does not
310 /// consider that a capture, instead adding it to the "Uses" list and
311 /// continuing with the analysis.
312 struct ArgumentUsesTracker : public CaptureTracker {
313   ArgumentUsesTracker(const SCCNodeSet &SCCNodes)
314       : Captured(false), SCCNodes(SCCNodes) {}
315
316   void tooManyUses() override { Captured = true; }
317
318   bool captured(const Use *U) override {
319     CallSite CS(U->getUser());
320     if (!CS.getInstruction()) {
321       Captured = true;
322       return true;
323     }
324
325     Function *F = CS.getCalledFunction();
326     if (!F || F->isDeclaration() || F->mayBeOverridden() ||
327         !SCCNodes.count(F)) {
328       Captured = true;
329       return true;
330     }
331
332     // Note: the callee and the two successor blocks *follow* the argument
333     // operands.  This means there is no need to adjust UseIndex to account for
334     // these.
335
336     unsigned UseIndex =
337         std::distance(const_cast<const Use *>(CS.arg_begin()), U);
338
339     assert(UseIndex < CS.data_operands_size() &&
340            "Indirect function calls should have been filtered above!");
341
342     if (UseIndex >= CS.getNumArgOperands()) {
343       // Data operand, but not a argument operand -- must be a bundle operand
344       assert(CS.hasOperandBundles() && "Must be!");
345
346       // CaptureTracking told us that we're being captured by an operand bundle
347       // use.  In this case it does not matter if the callee is within our SCC
348       // or not -- we've been captured in some unknown way, and we have to be
349       // conservative.
350       Captured = true;
351       return true;
352     }
353
354     if (UseIndex >= F->arg_size()) {
355       assert(F->isVarArg() && "More params than args in non-varargs call");
356       Captured = true;
357       return true;
358     }
359
360     Uses.push_back(&*std::next(F->arg_begin(), UseIndex));
361     return false;
362   }
363
364   bool Captured; // True only if certainly captured (used outside our SCC).
365   SmallVector<Argument *, 4> Uses; // Uses within our SCC.
366
367   const SCCNodeSet &SCCNodes;
368 };
369 }
370
371 namespace llvm {
372 template <> struct GraphTraits<ArgumentGraphNode *> {
373   typedef ArgumentGraphNode NodeType;
374   typedef SmallVectorImpl<ArgumentGraphNode *>::iterator ChildIteratorType;
375
376   static inline NodeType *getEntryNode(NodeType *A) { return A; }
377   static inline ChildIteratorType child_begin(NodeType *N) {
378     return N->Uses.begin();
379   }
380   static inline ChildIteratorType child_end(NodeType *N) {
381     return N->Uses.end();
382   }
383 };
384 template <>
385 struct GraphTraits<ArgumentGraph *> : public GraphTraits<ArgumentGraphNode *> {
386   static NodeType *getEntryNode(ArgumentGraph *AG) {
387     return AG->getEntryNode();
388   }
389   static ChildIteratorType nodes_begin(ArgumentGraph *AG) {
390     return AG->begin();
391   }
392   static ChildIteratorType nodes_end(ArgumentGraph *AG) { return AG->end(); }
393 };
394 }
395
396 /// Returns Attribute::None, Attribute::ReadOnly or Attribute::ReadNone.
397 static Attribute::AttrKind
398 determinePointerReadAttrs(Argument *A,
399                           const SmallPtrSet<Argument *, 8> &SCCNodes) {
400
401   SmallVector<Use *, 32> Worklist;
402   SmallSet<Use *, 32> Visited;
403
404   // inalloca arguments are always clobbered by the call.
405   if (A->hasInAllocaAttr())
406     return Attribute::None;
407
408   bool IsRead = false;
409   // We don't need to track IsWritten. If A is written to, return immediately.
410
411   for (Use &U : A->uses()) {
412     Visited.insert(&U);
413     Worklist.push_back(&U);
414   }
415
416   while (!Worklist.empty()) {
417     Use *U = Worklist.pop_back_val();
418     Instruction *I = cast<Instruction>(U->getUser());
419
420     switch (I->getOpcode()) {
421     case Instruction::BitCast:
422     case Instruction::GetElementPtr:
423     case Instruction::PHI:
424     case Instruction::Select:
425     case Instruction::AddrSpaceCast:
426       // The original value is not read/written via this if the new value isn't.
427       for (Use &UU : I->uses())
428         if (Visited.insert(&UU).second)
429           Worklist.push_back(&UU);
430       break;
431
432     case Instruction::Call:
433     case Instruction::Invoke: {
434       bool Captures = true;
435
436       if (I->getType()->isVoidTy())
437         Captures = false;
438
439       auto AddUsersToWorklistIfCapturing = [&] {
440         if (Captures)
441           for (Use &UU : I->uses())
442             if (Visited.insert(&UU).second)
443               Worklist.push_back(&UU);
444       };
445
446       CallSite CS(I);
447       if (CS.doesNotAccessMemory()) {
448         AddUsersToWorklistIfCapturing();
449         continue;
450       }
451
452       Function *F = CS.getCalledFunction();
453       if (!F) {
454         if (CS.onlyReadsMemory()) {
455           IsRead = true;
456           AddUsersToWorklistIfCapturing();
457           continue;
458         }
459         return Attribute::None;
460       }
461
462       // Note: the callee and the two successor blocks *follow* the argument
463       // operands.  This means there is no need to adjust UseIndex to account
464       // for these.
465
466       unsigned UseIndex = std::distance(CS.arg_begin(), U);
467
468       // U cannot be the callee operand use: since we're exploring the
469       // transitive uses of an Argument, having such a use be a callee would
470       // imply the CallSite is an indirect call or invoke; and we'd take the
471       // early exit above.
472       assert(UseIndex < CS.data_operands_size() &&
473              "Data operand use expected!");
474
475       bool IsOperandBundleUse = UseIndex >= CS.getNumArgOperands();
476
477       if (UseIndex >= F->arg_size() && !IsOperandBundleUse) {
478         assert(F->isVarArg() && "More params than args in non-varargs call");
479         return Attribute::None;
480       }
481
482       Captures &= !CS.doesNotCapture(UseIndex);
483
484       // Since the optimizer (by design) cannot see the data flow corresponding
485       // to a operand bundle use, these cannot participate in the optimistic SCC
486       // analysis.  Instead, we model the operand bundle uses as arguments in
487       // call to a function external to the SCC.
488       if (!SCCNodes.count(&*std::next(F->arg_begin(), UseIndex)) ||
489           IsOperandBundleUse) {
490
491         // The accessors used on CallSite here do the right thing for calls and
492         // invokes with operand bundles.
493
494         if (!CS.onlyReadsMemory() && !CS.onlyReadsMemory(UseIndex))
495           return Attribute::None;
496         if (!CS.doesNotAccessMemory(UseIndex))
497           IsRead = true;
498       }
499
500       AddUsersToWorklistIfCapturing();
501       break;
502     }
503
504     case Instruction::Load:
505       IsRead = true;
506       break;
507
508     case Instruction::ICmp:
509     case Instruction::Ret:
510       break;
511
512     default:
513       return Attribute::None;
514     }
515   }
516
517   return IsRead ? Attribute::ReadOnly : Attribute::ReadNone;
518 }
519
520 /// Deduce nocapture attributes for the SCC.
521 static bool addArgumentAttrs(const SCCNodeSet &SCCNodes) {
522   bool Changed = false;
523
524   ArgumentGraph AG;
525
526   AttrBuilder B;
527   B.addAttribute(Attribute::NoCapture);
528
529   // Check each function in turn, determining which pointer arguments are not
530   // captured.
531   for (Function *F : SCCNodes) {
532     // Definitions with weak linkage may be overridden at linktime with
533     // something that captures pointers, so treat them like declarations.
534     if (F->isDeclaration() || F->mayBeOverridden())
535       continue;
536
537     // Functions that are readonly (or readnone) and nounwind and don't return
538     // a value can't capture arguments. Don't analyze them.
539     if (F->onlyReadsMemory() && F->doesNotThrow() &&
540         F->getReturnType()->isVoidTy()) {
541       for (Function::arg_iterator A = F->arg_begin(), E = F->arg_end(); A != E;
542            ++A) {
543         if (A->getType()->isPointerTy() && !A->hasNoCaptureAttr()) {
544           A->addAttr(AttributeSet::get(F->getContext(), A->getArgNo() + 1, B));
545           ++NumNoCapture;
546           Changed = true;
547         }
548       }
549       continue;
550     }
551
552     for (Function::arg_iterator A = F->arg_begin(), E = F->arg_end(); A != E;
553          ++A) {
554       if (!A->getType()->isPointerTy())
555         continue;
556       bool HasNonLocalUses = false;
557       if (!A->hasNoCaptureAttr()) {
558         ArgumentUsesTracker Tracker(SCCNodes);
559         PointerMayBeCaptured(&*A, &Tracker);
560         if (!Tracker.Captured) {
561           if (Tracker.Uses.empty()) {
562             // If it's trivially not captured, mark it nocapture now.
563             A->addAttr(
564                 AttributeSet::get(F->getContext(), A->getArgNo() + 1, B));
565             ++NumNoCapture;
566             Changed = true;
567           } else {
568             // If it's not trivially captured and not trivially not captured,
569             // then it must be calling into another function in our SCC. Save
570             // its particulars for Argument-SCC analysis later.
571             ArgumentGraphNode *Node = AG[&*A];
572             for (SmallVectorImpl<Argument *>::iterator
573                      UI = Tracker.Uses.begin(),
574                      UE = Tracker.Uses.end();
575                  UI != UE; ++UI) {
576               Node->Uses.push_back(AG[*UI]);
577               if (*UI != A)
578                 HasNonLocalUses = true;
579             }
580           }
581         }
582         // Otherwise, it's captured. Don't bother doing SCC analysis on it.
583       }
584       if (!HasNonLocalUses && !A->onlyReadsMemory()) {
585         // Can we determine that it's readonly/readnone without doing an SCC?
586         // Note that we don't allow any calls at all here, or else our result
587         // will be dependent on the iteration order through the functions in the
588         // SCC.
589         SmallPtrSet<Argument *, 8> Self;
590         Self.insert(&*A);
591         Attribute::AttrKind R = determinePointerReadAttrs(&*A, Self);
592         if (R != Attribute::None) {
593           AttrBuilder B;
594           B.addAttribute(R);
595           A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
596           Changed = true;
597           R == Attribute::ReadOnly ? ++NumReadOnlyArg : ++NumReadNoneArg;
598         }
599       }
600     }
601   }
602
603   // The graph we've collected is partial because we stopped scanning for
604   // argument uses once we solved the argument trivially. These partial nodes
605   // show up as ArgumentGraphNode objects with an empty Uses list, and for
606   // these nodes the final decision about whether they capture has already been
607   // made.  If the definition doesn't have a 'nocapture' attribute by now, it
608   // captures.
609
610   for (scc_iterator<ArgumentGraph *> I = scc_begin(&AG); !I.isAtEnd(); ++I) {
611     const std::vector<ArgumentGraphNode *> &ArgumentSCC = *I;
612     if (ArgumentSCC.size() == 1) {
613       if (!ArgumentSCC[0]->Definition)
614         continue; // synthetic root node
615
616       // eg. "void f(int* x) { if (...) f(x); }"
617       if (ArgumentSCC[0]->Uses.size() == 1 &&
618           ArgumentSCC[0]->Uses[0] == ArgumentSCC[0]) {
619         Argument *A = ArgumentSCC[0]->Definition;
620         A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
621         ++NumNoCapture;
622         Changed = true;
623       }
624       continue;
625     }
626
627     bool SCCCaptured = false;
628     for (auto I = ArgumentSCC.begin(), E = ArgumentSCC.end();
629          I != E && !SCCCaptured; ++I) {
630       ArgumentGraphNode *Node = *I;
631       if (Node->Uses.empty()) {
632         if (!Node->Definition->hasNoCaptureAttr())
633           SCCCaptured = true;
634       }
635     }
636     if (SCCCaptured)
637       continue;
638
639     SmallPtrSet<Argument *, 8> ArgumentSCCNodes;
640     // Fill ArgumentSCCNodes with the elements of the ArgumentSCC.  Used for
641     // quickly looking up whether a given Argument is in this ArgumentSCC.
642     for (auto I = ArgumentSCC.begin(), E = ArgumentSCC.end(); I != E; ++I) {
643       ArgumentSCCNodes.insert((*I)->Definition);
644     }
645
646     for (auto I = ArgumentSCC.begin(), E = ArgumentSCC.end();
647          I != E && !SCCCaptured; ++I) {
648       ArgumentGraphNode *N = *I;
649       for (SmallVectorImpl<ArgumentGraphNode *>::iterator UI = N->Uses.begin(),
650                                                           UE = N->Uses.end();
651            UI != UE; ++UI) {
652         Argument *A = (*UI)->Definition;
653         if (A->hasNoCaptureAttr() || ArgumentSCCNodes.count(A))
654           continue;
655         SCCCaptured = true;
656         break;
657       }
658     }
659     if (SCCCaptured)
660       continue;
661
662     for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) {
663       Argument *A = ArgumentSCC[i]->Definition;
664       A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
665       ++NumNoCapture;
666       Changed = true;
667     }
668
669     // We also want to compute readonly/readnone. With a small number of false
670     // negatives, we can assume that any pointer which is captured isn't going
671     // to be provably readonly or readnone, since by definition we can't
672     // analyze all uses of a captured pointer.
673     //
674     // The false negatives happen when the pointer is captured by a function
675     // that promises readonly/readnone behaviour on the pointer, then the
676     // pointer's lifetime ends before anything that writes to arbitrary memory.
677     // Also, a readonly/readnone pointer may be returned, but returning a
678     // pointer is capturing it.
679
680     Attribute::AttrKind ReadAttr = Attribute::ReadNone;
681     for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) {
682       Argument *A = ArgumentSCC[i]->Definition;
683       Attribute::AttrKind K = determinePointerReadAttrs(A, ArgumentSCCNodes);
684       if (K == Attribute::ReadNone)
685         continue;
686       if (K == Attribute::ReadOnly) {
687         ReadAttr = Attribute::ReadOnly;
688         continue;
689       }
690       ReadAttr = K;
691       break;
692     }
693
694     if (ReadAttr != Attribute::None) {
695       AttrBuilder B, R;
696       B.addAttribute(ReadAttr);
697       R.addAttribute(Attribute::ReadOnly).addAttribute(Attribute::ReadNone);
698       for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) {
699         Argument *A = ArgumentSCC[i]->Definition;
700         // Clear out existing readonly/readnone attributes
701         A->removeAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, R));
702         A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
703         ReadAttr == Attribute::ReadOnly ? ++NumReadOnlyArg : ++NumReadNoneArg;
704         Changed = true;
705       }
706     }
707   }
708
709   return Changed;
710 }
711
712 /// Tests whether a function is "malloc-like".
713 ///
714 /// A function is "malloc-like" if it returns either null or a pointer that
715 /// doesn't alias any other pointer visible to the caller.
716 static bool isFunctionMallocLike(Function *F, const SCCNodeSet &SCCNodes) {
717   SmallSetVector<Value *, 8> FlowsToReturn;
718   for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I)
719     if (ReturnInst *Ret = dyn_cast<ReturnInst>(I->getTerminator()))
720       FlowsToReturn.insert(Ret->getReturnValue());
721
722   for (unsigned i = 0; i != FlowsToReturn.size(); ++i) {
723     Value *RetVal = FlowsToReturn[i];
724
725     if (Constant *C = dyn_cast<Constant>(RetVal)) {
726       if (!C->isNullValue() && !isa<UndefValue>(C))
727         return false;
728
729       continue;
730     }
731
732     if (isa<Argument>(RetVal))
733       return false;
734
735     if (Instruction *RVI = dyn_cast<Instruction>(RetVal))
736       switch (RVI->getOpcode()) {
737       // Extend the analysis by looking upwards.
738       case Instruction::BitCast:
739       case Instruction::GetElementPtr:
740       case Instruction::AddrSpaceCast:
741         FlowsToReturn.insert(RVI->getOperand(0));
742         continue;
743       case Instruction::Select: {
744         SelectInst *SI = cast<SelectInst>(RVI);
745         FlowsToReturn.insert(SI->getTrueValue());
746         FlowsToReturn.insert(SI->getFalseValue());
747         continue;
748       }
749       case Instruction::PHI: {
750         PHINode *PN = cast<PHINode>(RVI);
751         for (Value *IncValue : PN->incoming_values())
752           FlowsToReturn.insert(IncValue);
753         continue;
754       }
755
756       // Check whether the pointer came from an allocation.
757       case Instruction::Alloca:
758         break;
759       case Instruction::Call:
760       case Instruction::Invoke: {
761         CallSite CS(RVI);
762         if (CS.paramHasAttr(0, Attribute::NoAlias))
763           break;
764         if (CS.getCalledFunction() && SCCNodes.count(CS.getCalledFunction()))
765           break;
766       } // fall-through
767       default:
768         return false; // Did not come from an allocation.
769       }
770
771     if (PointerMayBeCaptured(RetVal, false, /*StoreCaptures=*/false))
772       return false;
773   }
774
775   return true;
776 }
777
778 /// Deduce noalias attributes for the SCC.
779 static bool addNoAliasAttrs(const SCCNodeSet &SCCNodes) {
780   // Check each function in turn, determining which functions return noalias
781   // pointers.
782   for (Function *F : SCCNodes) {
783     // Already noalias.
784     if (F->doesNotAlias(0))
785       continue;
786
787     // Definitions with weak linkage may be overridden at linktime, so
788     // treat them like declarations.
789     if (F->isDeclaration() || F->mayBeOverridden())
790       return false;
791
792     // We annotate noalias return values, which are only applicable to
793     // pointer types.
794     if (!F->getReturnType()->isPointerTy())
795       continue;
796
797     if (!isFunctionMallocLike(F, SCCNodes))
798       return false;
799   }
800
801   bool MadeChange = false;
802   for (Function *F : SCCNodes) {
803     if (F->doesNotAlias(0) || !F->getReturnType()->isPointerTy())
804       continue;
805
806     F->setDoesNotAlias(0);
807     ++NumNoAlias;
808     MadeChange = true;
809   }
810
811   return MadeChange;
812 }
813
814 /// Tests whether this function is known to not return null.
815 ///
816 /// Requires that the function returns a pointer.
817 ///
818 /// Returns true if it believes the function will not return a null, and sets
819 /// \p Speculative based on whether the returned conclusion is a speculative
820 /// conclusion due to SCC calls.
821 static bool isReturnNonNull(Function *F, const SCCNodeSet &SCCNodes,
822                             const TargetLibraryInfo &TLI, bool &Speculative) {
823   assert(F->getReturnType()->isPointerTy() &&
824          "nonnull only meaningful on pointer types");
825   Speculative = false;
826
827   SmallSetVector<Value *, 8> FlowsToReturn;
828   for (BasicBlock &BB : *F)
829     if (auto *Ret = dyn_cast<ReturnInst>(BB.getTerminator()))
830       FlowsToReturn.insert(Ret->getReturnValue());
831
832   for (unsigned i = 0; i != FlowsToReturn.size(); ++i) {
833     Value *RetVal = FlowsToReturn[i];
834
835     // If this value is locally known to be non-null, we're good
836     if (isKnownNonNull(RetVal, &TLI))
837       continue;
838
839     // Otherwise, we need to look upwards since we can't make any local
840     // conclusions.
841     Instruction *RVI = dyn_cast<Instruction>(RetVal);
842     if (!RVI)
843       return false;
844     switch (RVI->getOpcode()) {
845     // Extend the analysis by looking upwards.
846     case Instruction::BitCast:
847     case Instruction::GetElementPtr:
848     case Instruction::AddrSpaceCast:
849       FlowsToReturn.insert(RVI->getOperand(0));
850       continue;
851     case Instruction::Select: {
852       SelectInst *SI = cast<SelectInst>(RVI);
853       FlowsToReturn.insert(SI->getTrueValue());
854       FlowsToReturn.insert(SI->getFalseValue());
855       continue;
856     }
857     case Instruction::PHI: {
858       PHINode *PN = cast<PHINode>(RVI);
859       for (int i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
860         FlowsToReturn.insert(PN->getIncomingValue(i));
861       continue;
862     }
863     case Instruction::Call:
864     case Instruction::Invoke: {
865       CallSite CS(RVI);
866       Function *Callee = CS.getCalledFunction();
867       // A call to a node within the SCC is assumed to return null until
868       // proven otherwise
869       if (Callee && SCCNodes.count(Callee)) {
870         Speculative = true;
871         continue;
872       }
873       return false;
874     }
875     default:
876       return false; // Unknown source, may be null
877     };
878     llvm_unreachable("should have either continued or returned");
879   }
880
881   return true;
882 }
883
884 /// Deduce nonnull attributes for the SCC.
885 static bool addNonNullAttrs(const SCCNodeSet &SCCNodes,
886                             const TargetLibraryInfo &TLI) {
887   // Speculative that all functions in the SCC return only nonnull
888   // pointers.  We may refute this as we analyze functions.
889   bool SCCReturnsNonNull = true;
890
891   bool MadeChange = false;
892
893   // Check each function in turn, determining which functions return nonnull
894   // pointers.
895   for (Function *F : SCCNodes) {
896     // Already nonnull.
897     if (F->getAttributes().hasAttribute(AttributeSet::ReturnIndex,
898                                         Attribute::NonNull))
899       continue;
900
901     // Definitions with weak linkage may be overridden at linktime, so
902     // treat them like declarations.
903     if (F->isDeclaration() || F->mayBeOverridden())
904       return false;
905
906     // We annotate nonnull return values, which are only applicable to
907     // pointer types.
908     if (!F->getReturnType()->isPointerTy())
909       continue;
910
911     bool Speculative = false;
912     if (isReturnNonNull(F, SCCNodes, TLI, Speculative)) {
913       if (!Speculative) {
914         // Mark the function eagerly since we may discover a function
915         // which prevents us from speculating about the entire SCC
916         DEBUG(dbgs() << "Eagerly marking " << F->getName() << " as nonnull\n");
917         F->addAttribute(AttributeSet::ReturnIndex, Attribute::NonNull);
918         ++NumNonNullReturn;
919         MadeChange = true;
920       }
921       continue;
922     }
923     // At least one function returns something which could be null, can't
924     // speculate any more.
925     SCCReturnsNonNull = false;
926   }
927
928   if (SCCReturnsNonNull) {
929     for (Function *F : SCCNodes) {
930       if (F->getAttributes().hasAttribute(AttributeSet::ReturnIndex,
931                                           Attribute::NonNull) ||
932           !F->getReturnType()->isPointerTy())
933         continue;
934
935       DEBUG(dbgs() << "SCC marking " << F->getName() << " as nonnull\n");
936       F->addAttribute(AttributeSet::ReturnIndex, Attribute::NonNull);
937       ++NumNonNullReturn;
938       MadeChange = true;
939     }
940   }
941
942   return MadeChange;
943 }
944
945 static void setDoesNotAccessMemory(Function &F) {
946   if (!F.doesNotAccessMemory()) {
947     F.setDoesNotAccessMemory();
948     ++NumAnnotated;
949   }
950 }
951
952 static void setOnlyReadsMemory(Function &F) {
953   if (!F.onlyReadsMemory()) {
954     F.setOnlyReadsMemory();
955     ++NumAnnotated;
956   }
957 }
958
959 static void setDoesNotThrow(Function &F) {
960   if (!F.doesNotThrow()) {
961     F.setDoesNotThrow();
962     ++NumAnnotated;
963   }
964 }
965
966 static void setDoesNotCapture(Function &F, unsigned n) {
967   if (!F.doesNotCapture(n)) {
968     F.setDoesNotCapture(n);
969     ++NumAnnotated;
970   }
971 }
972
973 static void setOnlyReadsMemory(Function &F, unsigned n) {
974   if (!F.onlyReadsMemory(n)) {
975     F.setOnlyReadsMemory(n);
976     ++NumAnnotated;
977   }
978 }
979
980 static void setDoesNotAlias(Function &F, unsigned n) {
981   if (!F.doesNotAlias(n)) {
982     F.setDoesNotAlias(n);
983     ++NumAnnotated;
984   }
985 }
986
987 static bool setDoesNotRecurse(Function &F) {
988   if (F.doesNotRecurse())
989     return false;
990   F.setDoesNotRecurse();
991   ++NumNoRecurse;
992   return true;
993 }
994
995 /// Analyze the name and prototype of the given function and set any applicable
996 /// attributes.
997 ///
998 /// Returns true if any attributes were set and false otherwise.
999 static bool inferPrototypeAttributes(Function &F, const TargetLibraryInfo &TLI) {
1000   if (F.hasFnAttribute(Attribute::OptimizeNone))
1001     return false;
1002
1003   FunctionType *FTy = F.getFunctionType();
1004   LibFunc::Func TheLibFunc;
1005   if (!(TLI.getLibFunc(F.getName(), TheLibFunc) && TLI.has(TheLibFunc)))
1006     return false;
1007
1008   switch (TheLibFunc) {
1009   case LibFunc::strlen:
1010     if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1011       return false;
1012     setOnlyReadsMemory(F);
1013     setDoesNotThrow(F);
1014     setDoesNotCapture(F, 1);
1015     break;
1016   case LibFunc::strchr:
1017   case LibFunc::strrchr:
1018     if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy() ||
1019         !FTy->getParamType(1)->isIntegerTy())
1020       return false;
1021     setOnlyReadsMemory(F);
1022     setDoesNotThrow(F);
1023     break;
1024   case LibFunc::strtol:
1025   case LibFunc::strtod:
1026   case LibFunc::strtof:
1027   case LibFunc::strtoul:
1028   case LibFunc::strtoll:
1029   case LibFunc::strtold:
1030   case LibFunc::strtoull:
1031     if (FTy->getNumParams() < 2 || !FTy->getParamType(1)->isPointerTy())
1032       return false;
1033     setDoesNotThrow(F);
1034     setDoesNotCapture(F, 2);
1035     setOnlyReadsMemory(F, 1);
1036     break;
1037   case LibFunc::strcpy:
1038   case LibFunc::stpcpy:
1039   case LibFunc::strcat:
1040   case LibFunc::strncat:
1041   case LibFunc::strncpy:
1042   case LibFunc::stpncpy:
1043     if (FTy->getNumParams() < 2 || !FTy->getParamType(1)->isPointerTy())
1044       return false;
1045     setDoesNotThrow(F);
1046     setDoesNotCapture(F, 2);
1047     setOnlyReadsMemory(F, 2);
1048     break;
1049   case LibFunc::strxfrm:
1050     if (FTy->getNumParams() != 3 || !FTy->getParamType(0)->isPointerTy() ||
1051         !FTy->getParamType(1)->isPointerTy())
1052       return false;
1053     setDoesNotThrow(F);
1054     setDoesNotCapture(F, 1);
1055     setDoesNotCapture(F, 2);
1056     setOnlyReadsMemory(F, 2);
1057     break;
1058   case LibFunc::strcmp: // 0,1
1059   case LibFunc::strspn:  // 0,1
1060   case LibFunc::strncmp: // 0,1
1061   case LibFunc::strcspn: // 0,1
1062   case LibFunc::strcoll: // 0,1
1063   case LibFunc::strcasecmp:  // 0,1
1064   case LibFunc::strncasecmp: //
1065     if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy() ||
1066         !FTy->getParamType(1)->isPointerTy())
1067       return false;
1068     setOnlyReadsMemory(F);
1069     setDoesNotThrow(F);
1070     setDoesNotCapture(F, 1);
1071     setDoesNotCapture(F, 2);
1072     break;
1073   case LibFunc::strstr:
1074   case LibFunc::strpbrk:
1075     if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1076       return false;
1077     setOnlyReadsMemory(F);
1078     setDoesNotThrow(F);
1079     setDoesNotCapture(F, 2);
1080     break;
1081   case LibFunc::strtok:
1082   case LibFunc::strtok_r:
1083     if (FTy->getNumParams() < 2 || !FTy->getParamType(1)->isPointerTy())
1084       return false;
1085     setDoesNotThrow(F);
1086     setDoesNotCapture(F, 2);
1087     setOnlyReadsMemory(F, 2);
1088     break;
1089   case LibFunc::scanf:
1090     if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy())
1091       return false;
1092     setDoesNotThrow(F);
1093     setDoesNotCapture(F, 1);
1094     setOnlyReadsMemory(F, 1);
1095     break;
1096   case LibFunc::setbuf:
1097   case LibFunc::setvbuf:
1098     if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy())
1099       return false;
1100     setDoesNotThrow(F);
1101     setDoesNotCapture(F, 1);
1102     break;
1103   case LibFunc::strdup:
1104   case LibFunc::strndup:
1105     if (FTy->getNumParams() < 1 || !FTy->getReturnType()->isPointerTy() ||
1106         !FTy->getParamType(0)->isPointerTy())
1107       return false;
1108     setDoesNotThrow(F);
1109     setDoesNotAlias(F, 0);
1110     setDoesNotCapture(F, 1);
1111     setOnlyReadsMemory(F, 1);
1112     break;
1113   case LibFunc::stat:
1114   case LibFunc::statvfs:
1115     if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy() ||
1116         !FTy->getParamType(1)->isPointerTy())
1117       return false;
1118     setDoesNotThrow(F);
1119     setDoesNotCapture(F, 1);
1120     setDoesNotCapture(F, 2);
1121     setOnlyReadsMemory(F, 1);
1122     break;
1123   case LibFunc::sscanf:
1124     if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy() ||
1125         !FTy->getParamType(1)->isPointerTy())
1126       return false;
1127     setDoesNotThrow(F);
1128     setDoesNotCapture(F, 1);
1129     setDoesNotCapture(F, 2);
1130     setOnlyReadsMemory(F, 1);
1131     setOnlyReadsMemory(F, 2);
1132     break;
1133   case LibFunc::sprintf:
1134     if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy() ||
1135         !FTy->getParamType(1)->isPointerTy())
1136       return false;
1137     setDoesNotThrow(F);
1138     setDoesNotCapture(F, 1);
1139     setDoesNotCapture(F, 2);
1140     setOnlyReadsMemory(F, 2);
1141     break;
1142   case LibFunc::snprintf:
1143     if (FTy->getNumParams() != 3 || !FTy->getParamType(0)->isPointerTy() ||
1144         !FTy->getParamType(2)->isPointerTy())
1145       return false;
1146     setDoesNotThrow(F);
1147     setDoesNotCapture(F, 1);
1148     setDoesNotCapture(F, 3);
1149     setOnlyReadsMemory(F, 3);
1150     break;
1151   case LibFunc::setitimer:
1152     if (FTy->getNumParams() != 3 || !FTy->getParamType(1)->isPointerTy() ||
1153         !FTy->getParamType(2)->isPointerTy())
1154       return false;
1155     setDoesNotThrow(F);
1156     setDoesNotCapture(F, 2);
1157     setDoesNotCapture(F, 3);
1158     setOnlyReadsMemory(F, 2);
1159     break;
1160   case LibFunc::system:
1161     if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1162       return false;
1163     // May throw; "system" is a valid pthread cancellation point.
1164     setDoesNotCapture(F, 1);
1165     setOnlyReadsMemory(F, 1);
1166     break;
1167   case LibFunc::malloc:
1168     if (FTy->getNumParams() != 1 || !FTy->getReturnType()->isPointerTy())
1169       return false;
1170     setDoesNotThrow(F);
1171     setDoesNotAlias(F, 0);
1172     break;
1173   case LibFunc::memcmp:
1174     if (FTy->getNumParams() != 3 || !FTy->getParamType(0)->isPointerTy() ||
1175         !FTy->getParamType(1)->isPointerTy())
1176       return false;
1177     setOnlyReadsMemory(F);
1178     setDoesNotThrow(F);
1179     setDoesNotCapture(F, 1);
1180     setDoesNotCapture(F, 2);
1181     break;
1182   case LibFunc::memchr:
1183   case LibFunc::memrchr:
1184     if (FTy->getNumParams() != 3)
1185       return false;
1186     setOnlyReadsMemory(F);
1187     setDoesNotThrow(F);
1188     break;
1189   case LibFunc::modf:
1190   case LibFunc::modff:
1191   case LibFunc::modfl:
1192     if (FTy->getNumParams() < 2 || !FTy->getParamType(1)->isPointerTy())
1193       return false;
1194     setDoesNotThrow(F);
1195     setDoesNotCapture(F, 2);
1196     break;
1197   case LibFunc::memcpy:
1198   case LibFunc::memccpy:
1199   case LibFunc::memmove:
1200     if (FTy->getNumParams() < 2 || !FTy->getParamType(1)->isPointerTy())
1201       return false;
1202     setDoesNotThrow(F);
1203     setDoesNotCapture(F, 2);
1204     setOnlyReadsMemory(F, 2);
1205     break;
1206   case LibFunc::memalign:
1207     if (!FTy->getReturnType()->isPointerTy())
1208       return false;
1209     setDoesNotAlias(F, 0);
1210     break;
1211   case LibFunc::mkdir:
1212     if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1213       return false;
1214     setDoesNotThrow(F);
1215     setDoesNotCapture(F, 1);
1216     setOnlyReadsMemory(F, 1);
1217     break;
1218   case LibFunc::mktime:
1219     if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1220       return false;
1221     setDoesNotThrow(F);
1222     setDoesNotCapture(F, 1);
1223     break;
1224   case LibFunc::realloc:
1225     if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy() ||
1226         !FTy->getReturnType()->isPointerTy())
1227       return false;
1228     setDoesNotThrow(F);
1229     setDoesNotAlias(F, 0);
1230     setDoesNotCapture(F, 1);
1231     break;
1232   case LibFunc::read:
1233     if (FTy->getNumParams() != 3 || !FTy->getParamType(1)->isPointerTy())
1234       return false;
1235     // May throw; "read" is a valid pthread cancellation point.
1236     setDoesNotCapture(F, 2);
1237     break;
1238   case LibFunc::rewind:
1239     if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy())
1240       return false;
1241     setDoesNotThrow(F);
1242     setDoesNotCapture(F, 1);
1243     break;
1244   case LibFunc::rmdir:
1245   case LibFunc::remove:
1246   case LibFunc::realpath:
1247     if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy())
1248       return false;
1249     setDoesNotThrow(F);
1250     setDoesNotCapture(F, 1);
1251     setOnlyReadsMemory(F, 1);
1252     break;
1253   case LibFunc::rename:
1254     if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy() ||
1255         !FTy->getParamType(1)->isPointerTy())
1256       return false;
1257     setDoesNotThrow(F);
1258     setDoesNotCapture(F, 1);
1259     setDoesNotCapture(F, 2);
1260     setOnlyReadsMemory(F, 1);
1261     setOnlyReadsMemory(F, 2);
1262     break;
1263   case LibFunc::readlink:
1264     if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy() ||
1265         !FTy->getParamType(1)->isPointerTy())
1266       return false;
1267     setDoesNotThrow(F);
1268     setDoesNotCapture(F, 1);
1269     setDoesNotCapture(F, 2);
1270     setOnlyReadsMemory(F, 1);
1271     break;
1272   case LibFunc::write:
1273     if (FTy->getNumParams() != 3 || !FTy->getParamType(1)->isPointerTy())
1274       return false;
1275     // May throw; "write" is a valid pthread cancellation point.
1276     setDoesNotCapture(F, 2);
1277     setOnlyReadsMemory(F, 2);
1278     break;
1279   case LibFunc::bcopy:
1280     if (FTy->getNumParams() != 3 || !FTy->getParamType(0)->isPointerTy() ||
1281         !FTy->getParamType(1)->isPointerTy())
1282       return false;
1283     setDoesNotThrow(F);
1284     setDoesNotCapture(F, 1);
1285     setDoesNotCapture(F, 2);
1286     setOnlyReadsMemory(F, 1);
1287     break;
1288   case LibFunc::bcmp:
1289     if (FTy->getNumParams() != 3 || !FTy->getParamType(0)->isPointerTy() ||
1290         !FTy->getParamType(1)->isPointerTy())
1291       return false;
1292     setDoesNotThrow(F);
1293     setOnlyReadsMemory(F);
1294     setDoesNotCapture(F, 1);
1295     setDoesNotCapture(F, 2);
1296     break;
1297   case LibFunc::bzero:
1298     if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy())
1299       return false;
1300     setDoesNotThrow(F);
1301     setDoesNotCapture(F, 1);
1302     break;
1303   case LibFunc::calloc:
1304     if (FTy->getNumParams() != 2 || !FTy->getReturnType()->isPointerTy())
1305       return false;
1306     setDoesNotThrow(F);
1307     setDoesNotAlias(F, 0);
1308     break;
1309   case LibFunc::chmod:
1310   case LibFunc::chown:
1311     if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1312       return false;
1313     setDoesNotThrow(F);
1314     setDoesNotCapture(F, 1);
1315     setOnlyReadsMemory(F, 1);
1316     break;
1317   case LibFunc::ctermid:
1318   case LibFunc::clearerr:
1319   case LibFunc::closedir:
1320     if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1321       return false;
1322     setDoesNotThrow(F);
1323     setDoesNotCapture(F, 1);
1324     break;
1325   case LibFunc::atoi:
1326   case LibFunc::atol:
1327   case LibFunc::atof:
1328   case LibFunc::atoll:
1329     if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1330       return false;
1331     setDoesNotThrow(F);
1332     setOnlyReadsMemory(F);
1333     setDoesNotCapture(F, 1);
1334     break;
1335   case LibFunc::access:
1336     if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy())
1337       return false;
1338     setDoesNotThrow(F);
1339     setDoesNotCapture(F, 1);
1340     setOnlyReadsMemory(F, 1);
1341     break;
1342   case LibFunc::fopen:
1343     if (FTy->getNumParams() != 2 || !FTy->getReturnType()->isPointerTy() ||
1344         !FTy->getParamType(0)->isPointerTy() ||
1345         !FTy->getParamType(1)->isPointerTy())
1346       return false;
1347     setDoesNotThrow(F);
1348     setDoesNotAlias(F, 0);
1349     setDoesNotCapture(F, 1);
1350     setDoesNotCapture(F, 2);
1351     setOnlyReadsMemory(F, 1);
1352     setOnlyReadsMemory(F, 2);
1353     break;
1354   case LibFunc::fdopen:
1355     if (FTy->getNumParams() != 2 || !FTy->getReturnType()->isPointerTy() ||
1356         !FTy->getParamType(1)->isPointerTy())
1357       return false;
1358     setDoesNotThrow(F);
1359     setDoesNotAlias(F, 0);
1360     setDoesNotCapture(F, 2);
1361     setOnlyReadsMemory(F, 2);
1362     break;
1363   case LibFunc::feof:
1364   case LibFunc::free:
1365   case LibFunc::fseek:
1366   case LibFunc::ftell:
1367   case LibFunc::fgetc:
1368   case LibFunc::fseeko:
1369   case LibFunc::ftello:
1370   case LibFunc::fileno:
1371   case LibFunc::fflush:
1372   case LibFunc::fclose:
1373   case LibFunc::fsetpos:
1374   case LibFunc::flockfile:
1375   case LibFunc::funlockfile:
1376   case LibFunc::ftrylockfile:
1377     if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1378       return false;
1379     setDoesNotThrow(F);
1380     setDoesNotCapture(F, 1);
1381     break;
1382   case LibFunc::ferror:
1383     if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1384       return false;
1385     setDoesNotThrow(F);
1386     setDoesNotCapture(F, 1);
1387     setOnlyReadsMemory(F);
1388     break;
1389   case LibFunc::fputc:
1390   case LibFunc::fstat:
1391   case LibFunc::frexp:
1392   case LibFunc::frexpf:
1393   case LibFunc::frexpl:
1394   case LibFunc::fstatvfs:
1395     if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1396       return false;
1397     setDoesNotThrow(F);
1398     setDoesNotCapture(F, 2);
1399     break;
1400   case LibFunc::fgets:
1401     if (FTy->getNumParams() != 3 || !FTy->getParamType(0)->isPointerTy() ||
1402         !FTy->getParamType(2)->isPointerTy())
1403       return false;
1404     setDoesNotThrow(F);
1405     setDoesNotCapture(F, 3);
1406     break;
1407   case LibFunc::fread:
1408     if (FTy->getNumParams() != 4 || !FTy->getParamType(0)->isPointerTy() ||
1409         !FTy->getParamType(3)->isPointerTy())
1410       return false;
1411     setDoesNotThrow(F);
1412     setDoesNotCapture(F, 1);
1413     setDoesNotCapture(F, 4);
1414     break;
1415   case LibFunc::fwrite:
1416     if (FTy->getNumParams() != 4 || !FTy->getParamType(0)->isPointerTy() ||
1417         !FTy->getParamType(3)->isPointerTy())
1418       return false;
1419     setDoesNotThrow(F);
1420     setDoesNotCapture(F, 1);
1421     setDoesNotCapture(F, 4);
1422     break;
1423   case LibFunc::fputs:
1424     if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy() ||
1425         !FTy->getParamType(1)->isPointerTy())
1426       return false;
1427     setDoesNotThrow(F);
1428     setDoesNotCapture(F, 1);
1429     setDoesNotCapture(F, 2);
1430     setOnlyReadsMemory(F, 1);
1431     break;
1432   case LibFunc::fscanf:
1433   case LibFunc::fprintf:
1434     if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy() ||
1435         !FTy->getParamType(1)->isPointerTy())
1436       return false;
1437     setDoesNotThrow(F);
1438     setDoesNotCapture(F, 1);
1439     setDoesNotCapture(F, 2);
1440     setOnlyReadsMemory(F, 2);
1441     break;
1442   case LibFunc::fgetpos:
1443     if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy() ||
1444         !FTy->getParamType(1)->isPointerTy())
1445       return false;
1446     setDoesNotThrow(F);
1447     setDoesNotCapture(F, 1);
1448     setDoesNotCapture(F, 2);
1449     break;
1450   case LibFunc::getc:
1451   case LibFunc::getlogin_r:
1452   case LibFunc::getc_unlocked:
1453     if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1454       return false;
1455     setDoesNotThrow(F);
1456     setDoesNotCapture(F, 1);
1457     break;
1458   case LibFunc::getenv:
1459     if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1460       return false;
1461     setDoesNotThrow(F);
1462     setOnlyReadsMemory(F);
1463     setDoesNotCapture(F, 1);
1464     break;
1465   case LibFunc::gets:
1466   case LibFunc::getchar:
1467     setDoesNotThrow(F);
1468     break;
1469   case LibFunc::getitimer:
1470     if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1471       return false;
1472     setDoesNotThrow(F);
1473     setDoesNotCapture(F, 2);
1474     break;
1475   case LibFunc::getpwnam:
1476     if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1477       return false;
1478     setDoesNotThrow(F);
1479     setDoesNotCapture(F, 1);
1480     setOnlyReadsMemory(F, 1);
1481     break;
1482   case LibFunc::ungetc:
1483     if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1484       return false;
1485     setDoesNotThrow(F);
1486     setDoesNotCapture(F, 2);
1487     break;
1488   case LibFunc::uname:
1489     if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1490       return false;
1491     setDoesNotThrow(F);
1492     setDoesNotCapture(F, 1);
1493     break;
1494   case LibFunc::unlink:
1495     if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1496       return false;
1497     setDoesNotThrow(F);
1498     setDoesNotCapture(F, 1);
1499     setOnlyReadsMemory(F, 1);
1500     break;
1501   case LibFunc::unsetenv:
1502     if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1503       return false;
1504     setDoesNotThrow(F);
1505     setDoesNotCapture(F, 1);
1506     setOnlyReadsMemory(F, 1);
1507     break;
1508   case LibFunc::utime:
1509   case LibFunc::utimes:
1510     if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy() ||
1511         !FTy->getParamType(1)->isPointerTy())
1512       return false;
1513     setDoesNotThrow(F);
1514     setDoesNotCapture(F, 1);
1515     setDoesNotCapture(F, 2);
1516     setOnlyReadsMemory(F, 1);
1517     setOnlyReadsMemory(F, 2);
1518     break;
1519   case LibFunc::putc:
1520     if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1521       return false;
1522     setDoesNotThrow(F);
1523     setDoesNotCapture(F, 2);
1524     break;
1525   case LibFunc::puts:
1526   case LibFunc::printf:
1527   case LibFunc::perror:
1528     if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1529       return false;
1530     setDoesNotThrow(F);
1531     setDoesNotCapture(F, 1);
1532     setOnlyReadsMemory(F, 1);
1533     break;
1534   case LibFunc::pread:
1535     if (FTy->getNumParams() != 4 || !FTy->getParamType(1)->isPointerTy())
1536       return false;
1537     // May throw; "pread" is a valid pthread cancellation point.
1538     setDoesNotCapture(F, 2);
1539     break;
1540   case LibFunc::pwrite:
1541     if (FTy->getNumParams() != 4 || !FTy->getParamType(1)->isPointerTy())
1542       return false;
1543     // May throw; "pwrite" is a valid pthread cancellation point.
1544     setDoesNotCapture(F, 2);
1545     setOnlyReadsMemory(F, 2);
1546     break;
1547   case LibFunc::putchar:
1548     setDoesNotThrow(F);
1549     break;
1550   case LibFunc::popen:
1551     if (FTy->getNumParams() != 2 || !FTy->getReturnType()->isPointerTy() ||
1552         !FTy->getParamType(0)->isPointerTy() ||
1553         !FTy->getParamType(1)->isPointerTy())
1554       return false;
1555     setDoesNotThrow(F);
1556     setDoesNotAlias(F, 0);
1557     setDoesNotCapture(F, 1);
1558     setDoesNotCapture(F, 2);
1559     setOnlyReadsMemory(F, 1);
1560     setOnlyReadsMemory(F, 2);
1561     break;
1562   case LibFunc::pclose:
1563     if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1564       return false;
1565     setDoesNotThrow(F);
1566     setDoesNotCapture(F, 1);
1567     break;
1568   case LibFunc::vscanf:
1569     if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1570       return false;
1571     setDoesNotThrow(F);
1572     setDoesNotCapture(F, 1);
1573     setOnlyReadsMemory(F, 1);
1574     break;
1575   case LibFunc::vsscanf:
1576     if (FTy->getNumParams() != 3 || !FTy->getParamType(1)->isPointerTy() ||
1577         !FTy->getParamType(2)->isPointerTy())
1578       return false;
1579     setDoesNotThrow(F);
1580     setDoesNotCapture(F, 1);
1581     setDoesNotCapture(F, 2);
1582     setOnlyReadsMemory(F, 1);
1583     setOnlyReadsMemory(F, 2);
1584     break;
1585   case LibFunc::vfscanf:
1586     if (FTy->getNumParams() != 3 || !FTy->getParamType(1)->isPointerTy() ||
1587         !FTy->getParamType(2)->isPointerTy())
1588       return false;
1589     setDoesNotThrow(F);
1590     setDoesNotCapture(F, 1);
1591     setDoesNotCapture(F, 2);
1592     setOnlyReadsMemory(F, 2);
1593     break;
1594   case LibFunc::valloc:
1595     if (!FTy->getReturnType()->isPointerTy())
1596       return false;
1597     setDoesNotThrow(F);
1598     setDoesNotAlias(F, 0);
1599     break;
1600   case LibFunc::vprintf:
1601     if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy())
1602       return false;
1603     setDoesNotThrow(F);
1604     setDoesNotCapture(F, 1);
1605     setOnlyReadsMemory(F, 1);
1606     break;
1607   case LibFunc::vfprintf:
1608   case LibFunc::vsprintf:
1609     if (FTy->getNumParams() != 3 || !FTy->getParamType(0)->isPointerTy() ||
1610         !FTy->getParamType(1)->isPointerTy())
1611       return false;
1612     setDoesNotThrow(F);
1613     setDoesNotCapture(F, 1);
1614     setDoesNotCapture(F, 2);
1615     setOnlyReadsMemory(F, 2);
1616     break;
1617   case LibFunc::vsnprintf:
1618     if (FTy->getNumParams() != 4 || !FTy->getParamType(0)->isPointerTy() ||
1619         !FTy->getParamType(2)->isPointerTy())
1620       return false;
1621     setDoesNotThrow(F);
1622     setDoesNotCapture(F, 1);
1623     setDoesNotCapture(F, 3);
1624     setOnlyReadsMemory(F, 3);
1625     break;
1626   case LibFunc::open:
1627     if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy())
1628       return false;
1629     // May throw; "open" is a valid pthread cancellation point.
1630     setDoesNotCapture(F, 1);
1631     setOnlyReadsMemory(F, 1);
1632     break;
1633   case LibFunc::opendir:
1634     if (FTy->getNumParams() != 1 || !FTy->getReturnType()->isPointerTy() ||
1635         !FTy->getParamType(0)->isPointerTy())
1636       return false;
1637     setDoesNotThrow(F);
1638     setDoesNotAlias(F, 0);
1639     setDoesNotCapture(F, 1);
1640     setOnlyReadsMemory(F, 1);
1641     break;
1642   case LibFunc::tmpfile:
1643     if (!FTy->getReturnType()->isPointerTy())
1644       return false;
1645     setDoesNotThrow(F);
1646     setDoesNotAlias(F, 0);
1647     break;
1648   case LibFunc::times:
1649     if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1650       return false;
1651     setDoesNotThrow(F);
1652     setDoesNotCapture(F, 1);
1653     break;
1654   case LibFunc::htonl:
1655   case LibFunc::htons:
1656   case LibFunc::ntohl:
1657   case LibFunc::ntohs:
1658     setDoesNotThrow(F);
1659     setDoesNotAccessMemory(F);
1660     break;
1661   case LibFunc::lstat:
1662     if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy() ||
1663         !FTy->getParamType(1)->isPointerTy())
1664       return false;
1665     setDoesNotThrow(F);
1666     setDoesNotCapture(F, 1);
1667     setDoesNotCapture(F, 2);
1668     setOnlyReadsMemory(F, 1);
1669     break;
1670   case LibFunc::lchown:
1671     if (FTy->getNumParams() != 3 || !FTy->getParamType(0)->isPointerTy())
1672       return false;
1673     setDoesNotThrow(F);
1674     setDoesNotCapture(F, 1);
1675     setOnlyReadsMemory(F, 1);
1676     break;
1677   case LibFunc::qsort:
1678     if (FTy->getNumParams() != 4 || !FTy->getParamType(3)->isPointerTy())
1679       return false;
1680     // May throw; places call through function pointer.
1681     setDoesNotCapture(F, 4);
1682     break;
1683   case LibFunc::dunder_strdup:
1684   case LibFunc::dunder_strndup:
1685     if (FTy->getNumParams() < 1 || !FTy->getReturnType()->isPointerTy() ||
1686         !FTy->getParamType(0)->isPointerTy())
1687       return false;
1688     setDoesNotThrow(F);
1689     setDoesNotAlias(F, 0);
1690     setDoesNotCapture(F, 1);
1691     setOnlyReadsMemory(F, 1);
1692     break;
1693   case LibFunc::dunder_strtok_r:
1694     if (FTy->getNumParams() != 3 || !FTy->getParamType(1)->isPointerTy())
1695       return false;
1696     setDoesNotThrow(F);
1697     setDoesNotCapture(F, 2);
1698     setOnlyReadsMemory(F, 2);
1699     break;
1700   case LibFunc::under_IO_getc:
1701     if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1702       return false;
1703     setDoesNotThrow(F);
1704     setDoesNotCapture(F, 1);
1705     break;
1706   case LibFunc::under_IO_putc:
1707     if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1708       return false;
1709     setDoesNotThrow(F);
1710     setDoesNotCapture(F, 2);
1711     break;
1712   case LibFunc::dunder_isoc99_scanf:
1713     if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy())
1714       return false;
1715     setDoesNotThrow(F);
1716     setDoesNotCapture(F, 1);
1717     setOnlyReadsMemory(F, 1);
1718     break;
1719   case LibFunc::stat64:
1720   case LibFunc::lstat64:
1721   case LibFunc::statvfs64:
1722     if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy() ||
1723         !FTy->getParamType(1)->isPointerTy())
1724       return false;
1725     setDoesNotThrow(F);
1726     setDoesNotCapture(F, 1);
1727     setDoesNotCapture(F, 2);
1728     setOnlyReadsMemory(F, 1);
1729     break;
1730   case LibFunc::dunder_isoc99_sscanf:
1731     if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy() ||
1732         !FTy->getParamType(1)->isPointerTy())
1733       return false;
1734     setDoesNotThrow(F);
1735     setDoesNotCapture(F, 1);
1736     setDoesNotCapture(F, 2);
1737     setOnlyReadsMemory(F, 1);
1738     setOnlyReadsMemory(F, 2);
1739     break;
1740   case LibFunc::fopen64:
1741     if (FTy->getNumParams() != 2 || !FTy->getReturnType()->isPointerTy() ||
1742         !FTy->getParamType(0)->isPointerTy() ||
1743         !FTy->getParamType(1)->isPointerTy())
1744       return false;
1745     setDoesNotThrow(F);
1746     setDoesNotAlias(F, 0);
1747     setDoesNotCapture(F, 1);
1748     setDoesNotCapture(F, 2);
1749     setOnlyReadsMemory(F, 1);
1750     setOnlyReadsMemory(F, 2);
1751     break;
1752   case LibFunc::fseeko64:
1753   case LibFunc::ftello64:
1754     if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1755       return false;
1756     setDoesNotThrow(F);
1757     setDoesNotCapture(F, 1);
1758     break;
1759   case LibFunc::tmpfile64:
1760     if (!FTy->getReturnType()->isPointerTy())
1761       return false;
1762     setDoesNotThrow(F);
1763     setDoesNotAlias(F, 0);
1764     break;
1765   case LibFunc::fstat64:
1766   case LibFunc::fstatvfs64:
1767     if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1768       return false;
1769     setDoesNotThrow(F);
1770     setDoesNotCapture(F, 2);
1771     break;
1772   case LibFunc::open64:
1773     if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy())
1774       return false;
1775     // May throw; "open" is a valid pthread cancellation point.
1776     setDoesNotCapture(F, 1);
1777     setOnlyReadsMemory(F, 1);
1778     break;
1779   case LibFunc::gettimeofday:
1780     if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy() ||
1781         !FTy->getParamType(1)->isPointerTy())
1782       return false;
1783     // Currently some platforms have the restrict keyword on the arguments to
1784     // gettimeofday. To be conservative, do not add noalias to gettimeofday's
1785     // arguments.
1786     setDoesNotThrow(F);
1787     setDoesNotCapture(F, 1);
1788     setDoesNotCapture(F, 2);
1789     break;
1790   default:
1791     // Didn't mark any attributes.
1792     return false;
1793   }
1794
1795   return true;
1796 }
1797
1798 static bool addNoRecurseAttrs(const CallGraphSCC &SCC,
1799                               SmallVectorImpl<WeakVH> &Revisit) {
1800   // Try and identify functions that do not recurse.
1801
1802   // If the SCC contains multiple nodes we know for sure there is recursion.
1803   if (!SCC.isSingular())
1804     return false;
1805
1806   const CallGraphNode *CGN = *SCC.begin();
1807   Function *F = CGN->getFunction();
1808   if (!F || F->isDeclaration() || F->doesNotRecurse())
1809     return false;
1810
1811   // If all of the calls in F are identifiable and are to norecurse functions, F
1812   // is norecurse. This check also detects self-recursion as F is not currently
1813   // marked norecurse, so any called from F to F will not be marked norecurse.
1814   if (std::all_of(CGN->begin(), CGN->end(),
1815                   [](const CallGraphNode::CallRecord &CR) {
1816                     Function *F = CR.second->getFunction();
1817                     return F && F->doesNotRecurse();
1818                   }))
1819     // Function calls a potentially recursive function.
1820     return setDoesNotRecurse(*F);
1821
1822   // We know that F is not obviously recursive, but we haven't been able to
1823   // prove that it doesn't actually recurse. Add it to the Revisit list to try
1824   // again top-down later.
1825   Revisit.push_back(F);
1826   return false;
1827 }
1828
1829 static bool addNoRecurseAttrsTopDownOnly(Function *F) {
1830   // If F is internal and all uses are in norecurse functions, then F is also
1831   // norecurse.
1832   if (F->doesNotRecurse())
1833     return false;
1834   if (F->hasInternalLinkage()) {
1835     for (auto *U : F->users())
1836       if (auto *I = dyn_cast<Instruction>(U)) {
1837         if (!I->getParent()->getParent()->doesNotRecurse())
1838           return false;
1839       } else {
1840         return false;
1841       }
1842     return setDoesNotRecurse(*F);
1843   }
1844   return false;
1845 }
1846
1847 bool FunctionAttrs::runOnSCC(CallGraphSCC &SCC) {
1848   TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
1849   bool Changed = false;
1850
1851   // We compute dedicated AA results for each function in the SCC as needed. We
1852   // use a lambda referencing external objects so that they live long enough to
1853   // be queried, but we re-use them each time.
1854   Optional<BasicAAResult> BAR;
1855   Optional<AAResults> AAR;
1856   auto AARGetter = [&](Function &F) -> AAResults & {
1857     BAR.emplace(createLegacyPMBasicAAResult(*this, F));
1858     AAR.emplace(createLegacyPMAAResults(*this, F, *BAR));
1859     return *AAR;
1860   };
1861
1862   // Fill SCCNodes with the elements of the SCC. Used for quickly looking up
1863   // whether a given CallGraphNode is in this SCC. Also track whether there are
1864   // any external or opt-none nodes that will prevent us from optimizing any
1865   // part of the SCC.
1866   SCCNodeSet SCCNodes;
1867   bool ExternalNode = false;
1868   for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
1869     Function *F = (*I)->getFunction();
1870     if (!F || F->hasFnAttribute(Attribute::OptimizeNone)) {
1871       // External node or function we're trying not to optimize - we both avoid
1872       // transform them and avoid leveraging information they provide.
1873       ExternalNode = true;
1874       continue;
1875     }
1876
1877     // When initially processing functions, also infer their prototype
1878     // attributes if they are declarations.
1879     if (F->isDeclaration())
1880       Changed |= inferPrototypeAttributes(*F, *TLI);
1881
1882     SCCNodes.insert(F);
1883   }
1884
1885   Changed |= addReadAttrs(SCCNodes, AARGetter);
1886   Changed |= addArgumentAttrs(SCCNodes);
1887
1888   // If we have no external nodes participating in the SCC, we can infer some
1889   // more precise attributes as well.
1890   if (!ExternalNode) {
1891     Changed |= addNoAliasAttrs(SCCNodes);
1892     Changed |= addNonNullAttrs(SCCNodes, *TLI);
1893   }
1894   
1895   Changed |= addNoRecurseAttrs(SCC, Revisit);
1896   return Changed;
1897 }
1898
1899 bool FunctionAttrs::doFinalization(CallGraph &CG) {
1900   bool Changed = false;
1901   // When iterating over SCCs we visit functions in a bottom-up fashion. Some of
1902   // the rules we have for identifying norecurse functions work best with a
1903   // top-down walk, so look again at all the functions we previously marked as
1904   // worth revisiting, in top-down order.
1905   for (auto &F : reverse(Revisit))
1906     if (F)
1907       Changed |= addNoRecurseAttrsTopDownOnly(cast<Function>((Value*)F));
1908   return Changed;
1909 }