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