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