1 //===- FunctionAttrs.cpp - Pass which marks functions attributes ----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
19 //===----------------------------------------------------------------------===//
21 #define DEBUG_TYPE "functionattrs"
22 #include "llvm/Transforms/IPO.h"
23 #include "llvm/ADT/SCCIterator.h"
24 #include "llvm/ADT/SetVector.h"
25 #include "llvm/ADT/SmallSet.h"
26 #include "llvm/ADT/Statistic.h"
27 #include "llvm/Analysis/AliasAnalysis.h"
28 #include "llvm/Analysis/CallGraph.h"
29 #include "llvm/Analysis/CallGraphSCCPass.h"
30 #include "llvm/Analysis/CaptureTracking.h"
31 #include "llvm/IR/GlobalVariable.h"
32 #include "llvm/IR/IntrinsicInst.h"
33 #include "llvm/IR/LLVMContext.h"
34 #include "llvm/Support/InstIterator.h"
35 #include "llvm/Target/TargetLibraryInfo.h"
38 STATISTIC(NumReadNone, "Number of functions marked readnone");
39 STATISTIC(NumReadOnly, "Number of functions marked readonly");
40 STATISTIC(NumNoCapture, "Number of arguments marked nocapture");
41 STATISTIC(NumReadNoneArg, "Number of arguments marked readnone");
42 STATISTIC(NumReadOnlyArg, "Number of arguments marked readonly");
43 STATISTIC(NumNoAlias, "Number of function returns marked noalias");
44 STATISTIC(NumAnnotated, "Number of attributes added to library functions");
47 struct FunctionAttrs : public CallGraphSCCPass {
48 static char ID; // Pass identification, replacement for typeid
49 FunctionAttrs() : CallGraphSCCPass(ID), AA(0) {
50 initializeFunctionAttrsPass(*PassRegistry::getPassRegistry());
53 // runOnSCC - Analyze the SCC, performing the transformation if possible.
54 bool runOnSCC(CallGraphSCC &SCC);
56 // AddReadAttrs - Deduce readonly/readnone attributes for the SCC.
57 bool AddReadAttrs(const CallGraphSCC &SCC);
59 // AddArgumentAttrs - Deduce nocapture attributes for the SCC.
60 bool AddArgumentAttrs(const CallGraphSCC &SCC);
62 // IsFunctionMallocLike - Does this function allocate new memory?
63 bool IsFunctionMallocLike(Function *F,
64 SmallPtrSet<Function*, 8> &) const;
66 // AddNoAliasAttrs - Deduce noalias attributes for the SCC.
67 bool AddNoAliasAttrs(const CallGraphSCC &SCC);
69 // Utility methods used by inferPrototypeAttributes to add attributes
70 // and maintain annotation statistics.
72 void setDoesNotAccessMemory(Function &F) {
73 if (!F.doesNotAccessMemory()) {
74 F.setDoesNotAccessMemory();
79 void setOnlyReadsMemory(Function &F) {
80 if (!F.onlyReadsMemory()) {
81 F.setOnlyReadsMemory();
86 void setDoesNotThrow(Function &F) {
87 if (!F.doesNotThrow()) {
93 void setDoesNotCapture(Function &F, unsigned n) {
94 if (!F.doesNotCapture(n)) {
95 F.setDoesNotCapture(n);
100 void setOnlyReadsMemory(Function &F, unsigned n) {
101 if (!F.onlyReadsMemory(n)) {
102 F.setOnlyReadsMemory(n);
107 void setDoesNotAlias(Function &F, unsigned n) {
108 if (!F.doesNotAlias(n)) {
109 F.setDoesNotAlias(n);
114 // inferPrototypeAttributes - Analyze the name and prototype of the
115 // given function and set any applicable attributes. Returns true
116 // if any attributes were set and false otherwise.
117 bool inferPrototypeAttributes(Function &F);
119 // annotateLibraryCalls - Adds attributes to well-known standard library
120 // call declarations.
121 bool annotateLibraryCalls(const CallGraphSCC &SCC);
123 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
124 AU.setPreservesCFG();
125 AU.addRequired<AliasAnalysis>();
126 AU.addRequired<TargetLibraryInfo>();
127 CallGraphSCCPass::getAnalysisUsage(AU);
132 TargetLibraryInfo *TLI;
136 char FunctionAttrs::ID = 0;
137 INITIALIZE_PASS_BEGIN(FunctionAttrs, "functionattrs",
138 "Deduce function attributes", false, false)
139 INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
140 INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
141 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfo)
142 INITIALIZE_PASS_END(FunctionAttrs, "functionattrs",
143 "Deduce function attributes", false, false)
145 Pass *llvm::createFunctionAttrsPass() { return new FunctionAttrs(); }
148 /// AddReadAttrs - Deduce readonly/readnone attributes for the SCC.
149 bool FunctionAttrs::AddReadAttrs(const CallGraphSCC &SCC) {
150 SmallPtrSet<Function*, 8> SCCNodes;
152 // Fill SCCNodes with the elements of the SCC. Used for quickly
153 // looking up whether a given CallGraphNode is in this SCC.
154 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I)
155 SCCNodes.insert((*I)->getFunction());
157 // Check if any of the functions in the SCC read or write memory. If they
158 // write memory then they can't be marked readnone or readonly.
159 bool ReadsMemory = false;
160 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
161 Function *F = (*I)->getFunction();
164 // External node - may write memory. Just give up.
167 AliasAnalysis::ModRefBehavior MRB = AA->getModRefBehavior(F);
168 if (MRB == AliasAnalysis::DoesNotAccessMemory)
172 // Definitions with weak linkage may be overridden at linktime with
173 // something that writes memory, so treat them like declarations.
174 if (F->isDeclaration() || F->mayBeOverridden()) {
175 if (!AliasAnalysis::onlyReadsMemory(MRB))
176 // May write memory. Just give up.
183 // Scan the function body for instructions that may read or write memory.
184 for (inst_iterator II = inst_begin(F), E = inst_end(F); II != E; ++II) {
185 Instruction *I = &*II;
187 // Some instructions can be ignored even if they read or write memory.
188 // Detect these now, skipping to the next instruction if one is found.
189 CallSite CS(cast<Value>(I));
191 // Ignore calls to functions in the same SCC.
192 if (CS.getCalledFunction() && SCCNodes.count(CS.getCalledFunction()))
194 AliasAnalysis::ModRefBehavior MRB = AA->getModRefBehavior(CS);
195 // If the call doesn't access arbitrary memory, we may be able to
196 // figure out something.
197 if (AliasAnalysis::onlyAccessesArgPointees(MRB)) {
198 // If the call does access argument pointees, check each argument.
199 if (AliasAnalysis::doesAccessArgPointees(MRB))
200 // Check whether all pointer arguments point to local memory, and
201 // ignore calls that only access local memory.
202 for (CallSite::arg_iterator CI = CS.arg_begin(), CE = CS.arg_end();
205 if (Arg->getType()->isPointerTy()) {
206 AliasAnalysis::Location Loc(Arg,
207 AliasAnalysis::UnknownSize,
208 I->getMetadata(LLVMContext::MD_tbaa));
209 if (!AA->pointsToConstantMemory(Loc, /*OrLocal=*/true)) {
210 if (MRB & AliasAnalysis::Mod)
211 // Writes non-local memory. Give up.
213 if (MRB & AliasAnalysis::Ref)
214 // Ok, it reads non-local memory.
221 // The call could access any memory. If that includes writes, give up.
222 if (MRB & AliasAnalysis::Mod)
224 // If it reads, note it.
225 if (MRB & AliasAnalysis::Ref)
228 } else if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
229 // Ignore non-volatile loads from local memory. (Atomic is okay here.)
230 if (!LI->isVolatile()) {
231 AliasAnalysis::Location Loc = AA->getLocation(LI);
232 if (AA->pointsToConstantMemory(Loc, /*OrLocal=*/true))
235 } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
236 // Ignore non-volatile stores to local memory. (Atomic is okay here.)
237 if (!SI->isVolatile()) {
238 AliasAnalysis::Location Loc = AA->getLocation(SI);
239 if (AA->pointsToConstantMemory(Loc, /*OrLocal=*/true))
242 } else if (VAArgInst *VI = dyn_cast<VAArgInst>(I)) {
243 // Ignore vaargs on local memory.
244 AliasAnalysis::Location Loc = AA->getLocation(VI);
245 if (AA->pointsToConstantMemory(Loc, /*OrLocal=*/true))
249 // Any remaining instructions need to be taken seriously! Check if they
250 // read or write memory.
251 if (I->mayWriteToMemory())
252 // Writes memory. Just give up.
255 // If this instruction may read memory, remember that.
256 ReadsMemory |= I->mayReadFromMemory();
260 // Success! Functions in this SCC do not access memory, or only read memory.
261 // Give them the appropriate attribute.
262 bool MadeChange = false;
263 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
264 Function *F = (*I)->getFunction();
266 if (F->doesNotAccessMemory())
270 if (F->onlyReadsMemory() && ReadsMemory)
276 // Clear out any existing attributes.
278 B.addAttribute(Attribute::ReadOnly)
279 .addAttribute(Attribute::ReadNone);
280 F->removeAttributes(AttributeSet::FunctionIndex,
281 AttributeSet::get(F->getContext(),
282 AttributeSet::FunctionIndex, B));
284 // Add in the new attribute.
285 F->addAttribute(AttributeSet::FunctionIndex,
286 ReadsMemory ? Attribute::ReadOnly : Attribute::ReadNone);
298 // For a given pointer Argument, this retains a list of Arguments of functions
299 // in the same SCC that the pointer data flows into. We use this to build an
300 // SCC of the arguments.
301 struct ArgumentGraphNode {
302 Argument *Definition;
303 SmallVector<ArgumentGraphNode*, 4> Uses;
306 class ArgumentGraph {
307 // We store pointers to ArgumentGraphNode objects, so it's important that
308 // that they not move around upon insert.
309 typedef std::map<Argument*, ArgumentGraphNode> ArgumentMapTy;
311 ArgumentMapTy ArgumentMap;
313 // There is no root node for the argument graph, in fact:
314 // void f(int *x, int *y) { if (...) f(x, y); }
315 // is an example where the graph is disconnected. The SCCIterator requires a
316 // single entry point, so we maintain a fake ("synthetic") root node that
317 // uses every node. Because the graph is directed and nothing points into
318 // the root, it will not participate in any SCCs (except for its own).
319 ArgumentGraphNode SyntheticRoot;
322 ArgumentGraph() { SyntheticRoot.Definition = 0; }
324 typedef SmallVectorImpl<ArgumentGraphNode*>::iterator iterator;
326 iterator begin() { return SyntheticRoot.Uses.begin(); }
327 iterator end() { return SyntheticRoot.Uses.end(); }
328 ArgumentGraphNode *getEntryNode() { return &SyntheticRoot; }
330 ArgumentGraphNode *operator[](Argument *A) {
331 ArgumentGraphNode &Node = ArgumentMap[A];
333 SyntheticRoot.Uses.push_back(&Node);
338 // This tracker checks whether callees are in the SCC, and if so it does not
339 // consider that a capture, instead adding it to the "Uses" list and
340 // continuing with the analysis.
341 struct ArgumentUsesTracker : public CaptureTracker {
342 ArgumentUsesTracker(const SmallPtrSet<Function*, 8> &SCCNodes)
343 : Captured(false), SCCNodes(SCCNodes) {}
345 void tooManyUses() { Captured = true; }
347 bool captured(Use *U) {
348 CallSite CS(U->getUser());
349 if (!CS.getInstruction()) { Captured = true; return true; }
351 Function *F = CS.getCalledFunction();
352 if (!F || !SCCNodes.count(F)) { Captured = true; return true; }
355 Function::arg_iterator AI = F->arg_begin(), AE = F->arg_end();
356 for (CallSite::arg_iterator PI = CS.arg_begin(), PE = CS.arg_end();
357 PI != PE; ++PI, ++AI) {
359 assert(F->isVarArg() && "More params than args in non-varargs call");
369 assert(Found && "Capturing call-site captured nothing?");
374 bool Captured; // True only if certainly captured (used outside our SCC).
375 SmallVector<Argument*, 4> Uses; // Uses within our SCC.
377 const SmallPtrSet<Function*, 8> &SCCNodes;
382 template<> struct GraphTraits<ArgumentGraphNode*> {
383 typedef ArgumentGraphNode NodeType;
384 typedef SmallVectorImpl<ArgumentGraphNode*>::iterator ChildIteratorType;
386 static inline NodeType *getEntryNode(NodeType *A) { return A; }
387 static inline ChildIteratorType child_begin(NodeType *N) {
388 return N->Uses.begin();
390 static inline ChildIteratorType child_end(NodeType *N) {
391 return N->Uses.end();
394 template<> struct GraphTraits<ArgumentGraph*>
395 : public GraphTraits<ArgumentGraphNode*> {
396 static NodeType *getEntryNode(ArgumentGraph *AG) {
397 return AG->getEntryNode();
399 static ChildIteratorType nodes_begin(ArgumentGraph *AG) {
402 static ChildIteratorType nodes_end(ArgumentGraph *AG) {
408 // Returns Attribute::None, Attribute::ReadOnly or Attribute::ReadNone.
409 static Attribute::AttrKind
410 determinePointerReadAttrs(Argument *A,
411 const SmallPtrSet<Argument*, 8> &SCCNodes) {
413 SmallVector<Use*, 32> Worklist;
414 SmallSet<Use*, 32> Visited;
417 // inalloca arguments are always clobbered by the call.
418 if (A->hasInAllocaAttr())
419 return Attribute::None;
422 // We don't need to track IsWritten. If A is written to, return immediately.
424 for (Value::use_iterator UI = A->use_begin(), UE = A->use_end();
427 return Attribute::None;
429 Use *U = &UI.getUse();
431 Worklist.push_back(U);
434 while (!Worklist.empty()) {
435 Use *U = Worklist.pop_back_val();
436 Instruction *I = cast<Instruction>(U->getUser());
439 switch (I->getOpcode()) {
440 case Instruction::BitCast:
441 case Instruction::GetElementPtr:
442 case Instruction::PHI:
443 case Instruction::Select:
444 case Instruction::AddrSpaceCast:
445 // The original value is not read/written via this if the new value isn't.
446 for (Instruction::use_iterator UI = I->use_begin(), UE = I->use_end();
448 Use *U = &UI.getUse();
449 if (Visited.insert(U))
450 Worklist.push_back(U);
454 case Instruction::Call:
455 case Instruction::Invoke: {
457 if (CS.doesNotAccessMemory())
460 Function *F = CS.getCalledFunction();
462 if (CS.onlyReadsMemory()) {
466 return Attribute::None;
469 Function::arg_iterator AI = F->arg_begin(), AE = F->arg_end();
470 CallSite::arg_iterator B = CS.arg_begin(), E = CS.arg_end();
471 for (CallSite::arg_iterator A = B; A != E; ++A, ++AI) {
474 assert(F->isVarArg() &&
475 "More params than args in non-varargs call.");
476 return Attribute::None;
478 if (SCCNodes.count(AI))
480 if (!CS.onlyReadsMemory() && !CS.onlyReadsMemory(A - B))
481 return Attribute::None;
482 if (!CS.doesNotAccessMemory(A - B))
489 case Instruction::Load:
493 case Instruction::ICmp:
494 case Instruction::Ret:
498 return Attribute::None;
502 return IsRead ? Attribute::ReadOnly : Attribute::ReadNone;
505 /// AddArgumentAttrs - Deduce nocapture attributes for the SCC.
506 bool FunctionAttrs::AddArgumentAttrs(const CallGraphSCC &SCC) {
507 bool Changed = false;
509 SmallPtrSet<Function*, 8> SCCNodes;
511 // Fill SCCNodes with the elements of the SCC. Used for quickly
512 // looking up whether a given CallGraphNode is in this SCC.
513 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
514 Function *F = (*I)->getFunction();
515 if (F && !F->isDeclaration() && !F->mayBeOverridden())
522 B.addAttribute(Attribute::NoCapture);
524 // Check each function in turn, determining which pointer arguments are not
526 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
527 Function *F = (*I)->getFunction();
530 // External node - only a problem for arguments that we pass to it.
533 // Definitions with weak linkage may be overridden at linktime with
534 // something that captures pointers, so treat them like declarations.
535 if (F->isDeclaration() || F->mayBeOverridden())
538 // Functions that are readonly (or readnone) and nounwind and don't return
539 // a value can't capture arguments. Don't analyze them.
540 if (F->onlyReadsMemory() && F->doesNotThrow() &&
541 F->getReturnType()->isVoidTy()) {
542 for (Function::arg_iterator A = F->arg_begin(), E = F->arg_end();
544 if (A->getType()->isPointerTy() && !A->hasNoCaptureAttr()) {
545 A->addAttr(AttributeSet::get(F->getContext(), A->getArgNo() + 1, B));
553 for (Function::arg_iterator A = F->arg_begin(), E = F->arg_end();
555 if (!A->getType()->isPointerTy()) continue;
556 bool HasNonLocalUses = false;
557 if (!A->hasNoCaptureAttr()) {
558 ArgumentUsesTracker Tracker(SCCNodes);
559 PointerMayBeCaptured(A, &Tracker);
560 if (!Tracker.Captured) {
561 if (Tracker.Uses.empty()) {
562 // If it's trivially not captured, mark it nocapture now.
563 A->addAttr(AttributeSet::get(F->getContext(), A->getArgNo()+1, B));
567 // If it's not trivially captured and not trivially not captured,
568 // then it must be calling into another function in our SCC. Save
569 // its particulars for Argument-SCC analysis later.
570 ArgumentGraphNode *Node = AG[A];
571 for (SmallVectorImpl<Argument*>::iterator UI = Tracker.Uses.begin(),
572 UE = Tracker.Uses.end(); UI != UE; ++UI) {
573 Node->Uses.push_back(AG[*UI]);
575 HasNonLocalUses = true;
579 // Otherwise, it's captured. Don't bother doing SCC analysis on it.
581 if (!HasNonLocalUses && !A->onlyReadsMemory()) {
582 // Can we determine that it's readonly/readnone without doing an SCC?
583 // Note that we don't allow any calls at all here, or else our result
584 // will be dependent on the iteration order through the functions in the
586 SmallPtrSet<Argument*, 8> Self;
588 Attribute::AttrKind R = determinePointerReadAttrs(A, Self);
589 if (R != Attribute::None) {
592 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
594 R == Attribute::ReadOnly ? ++NumReadOnlyArg : ++NumReadNoneArg;
600 // The graph we've collected is partial because we stopped scanning for
601 // argument uses once we solved the argument trivially. These partial nodes
602 // show up as ArgumentGraphNode objects with an empty Uses list, and for
603 // these nodes the final decision about whether they capture has already been
604 // made. If the definition doesn't have a 'nocapture' attribute by now, it
607 for (scc_iterator<ArgumentGraph*> I = scc_begin(&AG), E = scc_end(&AG);
609 std::vector<ArgumentGraphNode*> &ArgumentSCC = *I;
610 if (ArgumentSCC.size() == 1) {
611 if (!ArgumentSCC[0]->Definition) continue; // synthetic root node
613 // eg. "void f(int* x) { if (...) f(x); }"
614 if (ArgumentSCC[0]->Uses.size() == 1 &&
615 ArgumentSCC[0]->Uses[0] == ArgumentSCC[0]) {
616 Argument *A = ArgumentSCC[0]->Definition;
617 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
624 bool SCCCaptured = false;
625 for (std::vector<ArgumentGraphNode*>::iterator I = ArgumentSCC.begin(),
626 E = ArgumentSCC.end(); I != E && !SCCCaptured; ++I) {
627 ArgumentGraphNode *Node = *I;
628 if (Node->Uses.empty()) {
629 if (!Node->Definition->hasNoCaptureAttr())
633 if (SCCCaptured) continue;
635 SmallPtrSet<Argument*, 8> ArgumentSCCNodes;
636 // Fill ArgumentSCCNodes with the elements of the ArgumentSCC. Used for
637 // quickly looking up whether a given Argument is in this ArgumentSCC.
638 for (std::vector<ArgumentGraphNode*>::iterator I = ArgumentSCC.begin(),
639 E = ArgumentSCC.end(); I != E; ++I) {
640 ArgumentSCCNodes.insert((*I)->Definition);
643 for (std::vector<ArgumentGraphNode*>::iterator I = ArgumentSCC.begin(),
644 E = ArgumentSCC.end(); I != E && !SCCCaptured; ++I) {
645 ArgumentGraphNode *N = *I;
646 for (SmallVectorImpl<ArgumentGraphNode*>::iterator UI = N->Uses.begin(),
647 UE = N->Uses.end(); UI != UE; ++UI) {
648 Argument *A = (*UI)->Definition;
649 if (A->hasNoCaptureAttr() || ArgumentSCCNodes.count(A))
655 if (SCCCaptured) continue;
657 for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) {
658 Argument *A = ArgumentSCC[i]->Definition;
659 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
664 // We also want to compute readonly/readnone. With a small number of false
665 // negatives, we can assume that any pointer which is captured isn't going
666 // to be provably readonly or readnone, since by definition we can't
667 // analyze all uses of a captured pointer.
669 // The false negatives happen when the pointer is captured by a function
670 // that promises readonly/readnone behaviour on the pointer, then the
671 // pointer's lifetime ends before anything that writes to arbitrary memory.
672 // Also, a readonly/readnone pointer may be returned, but returning a
673 // pointer is capturing it.
675 Attribute::AttrKind ReadAttr = Attribute::ReadNone;
676 for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) {
677 Argument *A = ArgumentSCC[i]->Definition;
678 Attribute::AttrKind K = determinePointerReadAttrs(A, ArgumentSCCNodes);
679 if (K == Attribute::ReadNone)
681 if (K == Attribute::ReadOnly) {
682 ReadAttr = Attribute::ReadOnly;
689 if (ReadAttr != Attribute::None) {
691 B.addAttribute(ReadAttr);
692 for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) {
693 Argument *A = ArgumentSCC[i]->Definition;
694 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
695 ReadAttr == Attribute::ReadOnly ? ++NumReadOnlyArg : ++NumReadNoneArg;
704 /// IsFunctionMallocLike - A function is malloc-like if it returns either null
705 /// or a pointer that doesn't alias any other pointer visible to the caller.
706 bool FunctionAttrs::IsFunctionMallocLike(Function *F,
707 SmallPtrSet<Function*, 8> &SCCNodes) const {
708 SmallSetVector<Value *, 8> FlowsToReturn;
709 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I)
710 if (ReturnInst *Ret = dyn_cast<ReturnInst>(I->getTerminator()))
711 FlowsToReturn.insert(Ret->getReturnValue());
713 for (unsigned i = 0; i != FlowsToReturn.size(); ++i) {
714 Value *RetVal = FlowsToReturn[i];
716 if (Constant *C = dyn_cast<Constant>(RetVal)) {
717 if (!C->isNullValue() && !isa<UndefValue>(C))
723 if (isa<Argument>(RetVal))
726 if (Instruction *RVI = dyn_cast<Instruction>(RetVal))
727 switch (RVI->getOpcode()) {
728 // Extend the analysis by looking upwards.
729 case Instruction::BitCast:
730 case Instruction::GetElementPtr:
731 case Instruction::AddrSpaceCast:
732 FlowsToReturn.insert(RVI->getOperand(0));
734 case Instruction::Select: {
735 SelectInst *SI = cast<SelectInst>(RVI);
736 FlowsToReturn.insert(SI->getTrueValue());
737 FlowsToReturn.insert(SI->getFalseValue());
740 case Instruction::PHI: {
741 PHINode *PN = cast<PHINode>(RVI);
742 for (int i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
743 FlowsToReturn.insert(PN->getIncomingValue(i));
747 // Check whether the pointer came from an allocation.
748 case Instruction::Alloca:
750 case Instruction::Call:
751 case Instruction::Invoke: {
753 if (CS.paramHasAttr(0, Attribute::NoAlias))
755 if (CS.getCalledFunction() &&
756 SCCNodes.count(CS.getCalledFunction()))
760 return false; // Did not come from an allocation.
763 if (PointerMayBeCaptured(RetVal, false, /*StoreCaptures=*/false))
770 /// AddNoAliasAttrs - Deduce noalias attributes for the SCC.
771 bool FunctionAttrs::AddNoAliasAttrs(const CallGraphSCC &SCC) {
772 SmallPtrSet<Function*, 8> SCCNodes;
774 // Fill SCCNodes with the elements of the SCC. Used for quickly
775 // looking up whether a given CallGraphNode is in this SCC.
776 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I)
777 SCCNodes.insert((*I)->getFunction());
779 // Check each function in turn, determining which functions return noalias
781 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
782 Function *F = (*I)->getFunction();
785 // External node - skip it;
789 if (F->doesNotAlias(0))
792 // Definitions with weak linkage may be overridden at linktime, so
793 // treat them like declarations.
794 if (F->isDeclaration() || F->mayBeOverridden())
797 // We annotate noalias return values, which are only applicable to
799 if (!F->getReturnType()->isPointerTy())
802 if (!IsFunctionMallocLike(F, SCCNodes))
806 bool MadeChange = false;
807 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
808 Function *F = (*I)->getFunction();
809 if (F->doesNotAlias(0) || !F->getReturnType()->isPointerTy())
812 F->setDoesNotAlias(0);
820 /// inferPrototypeAttributes - Analyze the name and prototype of the
821 /// given function and set any applicable attributes. Returns true
822 /// if any attributes were set and false otherwise.
823 bool FunctionAttrs::inferPrototypeAttributes(Function &F) {
824 FunctionType *FTy = F.getFunctionType();
825 LibFunc::Func TheLibFunc;
826 if (!(TLI->getLibFunc(F.getName(), TheLibFunc) && TLI->has(TheLibFunc)))
829 switch (TheLibFunc) {
830 case LibFunc::strlen:
831 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
833 setOnlyReadsMemory(F);
835 setDoesNotCapture(F, 1);
837 case LibFunc::strchr:
838 case LibFunc::strrchr:
839 if (FTy->getNumParams() != 2 ||
840 !FTy->getParamType(0)->isPointerTy() ||
841 !FTy->getParamType(1)->isIntegerTy())
843 setOnlyReadsMemory(F);
846 case LibFunc::strtol:
847 case LibFunc::strtod:
848 case LibFunc::strtof:
849 case LibFunc::strtoul:
850 case LibFunc::strtoll:
851 case LibFunc::strtold:
852 case LibFunc::strtoull:
853 if (FTy->getNumParams() < 2 ||
854 !FTy->getParamType(1)->isPointerTy())
857 setDoesNotCapture(F, 2);
858 setOnlyReadsMemory(F, 1);
860 case LibFunc::strcpy:
861 case LibFunc::stpcpy:
862 case LibFunc::strcat:
863 case LibFunc::strncat:
864 case LibFunc::strncpy:
865 case LibFunc::stpncpy:
866 if (FTy->getNumParams() < 2 ||
867 !FTy->getParamType(1)->isPointerTy())
870 setDoesNotCapture(F, 2);
871 setOnlyReadsMemory(F, 2);
873 case LibFunc::strxfrm:
874 if (FTy->getNumParams() != 3 ||
875 !FTy->getParamType(0)->isPointerTy() ||
876 !FTy->getParamType(1)->isPointerTy())
879 setDoesNotCapture(F, 1);
880 setDoesNotCapture(F, 2);
881 setOnlyReadsMemory(F, 2);
883 case LibFunc::strcmp: //0,1
884 case LibFunc::strspn: // 0,1
885 case LibFunc::strncmp: // 0,1
886 case LibFunc::strcspn: //0,1
887 case LibFunc::strcoll: //0,1
888 case LibFunc::strcasecmp: // 0,1
889 case LibFunc::strncasecmp: //
890 if (FTy->getNumParams() < 2 ||
891 !FTy->getParamType(0)->isPointerTy() ||
892 !FTy->getParamType(1)->isPointerTy())
894 setOnlyReadsMemory(F);
896 setDoesNotCapture(F, 1);
897 setDoesNotCapture(F, 2);
899 case LibFunc::strstr:
900 case LibFunc::strpbrk:
901 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
903 setOnlyReadsMemory(F);
905 setDoesNotCapture(F, 2);
907 case LibFunc::strtok:
908 case LibFunc::strtok_r:
909 if (FTy->getNumParams() < 2 || !FTy->getParamType(1)->isPointerTy())
912 setDoesNotCapture(F, 2);
913 setOnlyReadsMemory(F, 2);
916 if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy())
919 setDoesNotCapture(F, 1);
920 setOnlyReadsMemory(F, 1);
922 case LibFunc::setbuf:
923 case LibFunc::setvbuf:
924 if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy())
927 setDoesNotCapture(F, 1);
929 case LibFunc::strdup:
930 case LibFunc::strndup:
931 if (FTy->getNumParams() < 1 || !FTy->getReturnType()->isPointerTy() ||
932 !FTy->getParamType(0)->isPointerTy())
935 setDoesNotAlias(F, 0);
936 setDoesNotCapture(F, 1);
937 setOnlyReadsMemory(F, 1);
940 case LibFunc::statvfs:
941 if (FTy->getNumParams() < 2 ||
942 !FTy->getParamType(0)->isPointerTy() ||
943 !FTy->getParamType(1)->isPointerTy())
946 setDoesNotCapture(F, 1);
947 setDoesNotCapture(F, 2);
948 setOnlyReadsMemory(F, 1);
950 case LibFunc::sscanf:
951 if (FTy->getNumParams() < 2 ||
952 !FTy->getParamType(0)->isPointerTy() ||
953 !FTy->getParamType(1)->isPointerTy())
956 setDoesNotCapture(F, 1);
957 setDoesNotCapture(F, 2);
958 setOnlyReadsMemory(F, 1);
959 setOnlyReadsMemory(F, 2);
961 case LibFunc::sprintf:
962 if (FTy->getNumParams() < 2 ||
963 !FTy->getParamType(0)->isPointerTy() ||
964 !FTy->getParamType(1)->isPointerTy())
967 setDoesNotCapture(F, 1);
968 setDoesNotCapture(F, 2);
969 setOnlyReadsMemory(F, 2);
971 case LibFunc::snprintf:
972 if (FTy->getNumParams() != 3 ||
973 !FTy->getParamType(0)->isPointerTy() ||
974 !FTy->getParamType(2)->isPointerTy())
977 setDoesNotCapture(F, 1);
978 setDoesNotCapture(F, 3);
979 setOnlyReadsMemory(F, 3);
981 case LibFunc::setitimer:
982 if (FTy->getNumParams() != 3 ||
983 !FTy->getParamType(1)->isPointerTy() ||
984 !FTy->getParamType(2)->isPointerTy())
987 setDoesNotCapture(F, 2);
988 setDoesNotCapture(F, 3);
989 setOnlyReadsMemory(F, 2);
991 case LibFunc::system:
992 if (FTy->getNumParams() != 1 ||
993 !FTy->getParamType(0)->isPointerTy())
995 // May throw; "system" is a valid pthread cancellation point.
996 setDoesNotCapture(F, 1);
997 setOnlyReadsMemory(F, 1);
999 case LibFunc::malloc:
1000 if (FTy->getNumParams() != 1 ||
1001 !FTy->getReturnType()->isPointerTy())
1004 setDoesNotAlias(F, 0);
1006 case LibFunc::memcmp:
1007 if (FTy->getNumParams() != 3 ||
1008 !FTy->getParamType(0)->isPointerTy() ||
1009 !FTy->getParamType(1)->isPointerTy())
1011 setOnlyReadsMemory(F);
1013 setDoesNotCapture(F, 1);
1014 setDoesNotCapture(F, 2);
1016 case LibFunc::memchr:
1017 case LibFunc::memrchr:
1018 if (FTy->getNumParams() != 3)
1020 setOnlyReadsMemory(F);
1024 case LibFunc::modff:
1025 case LibFunc::modfl:
1026 if (FTy->getNumParams() < 2 ||
1027 !FTy->getParamType(1)->isPointerTy())
1030 setDoesNotCapture(F, 2);
1032 case LibFunc::memcpy:
1033 case LibFunc::memccpy:
1034 case LibFunc::memmove:
1035 if (FTy->getNumParams() < 2 ||
1036 !FTy->getParamType(1)->isPointerTy())
1039 setDoesNotCapture(F, 2);
1040 setOnlyReadsMemory(F, 2);
1042 case LibFunc::memalign:
1043 if (!FTy->getReturnType()->isPointerTy())
1045 setDoesNotAlias(F, 0);
1047 case LibFunc::mkdir:
1048 if (FTy->getNumParams() == 0 ||
1049 !FTy->getParamType(0)->isPointerTy())
1052 setDoesNotCapture(F, 1);
1053 setOnlyReadsMemory(F, 1);
1055 case LibFunc::mktime:
1056 if (FTy->getNumParams() == 0 ||
1057 !FTy->getParamType(0)->isPointerTy())
1060 setDoesNotCapture(F, 1);
1062 case LibFunc::realloc:
1063 if (FTy->getNumParams() != 2 ||
1064 !FTy->getParamType(0)->isPointerTy() ||
1065 !FTy->getReturnType()->isPointerTy())
1068 setDoesNotAlias(F, 0);
1069 setDoesNotCapture(F, 1);
1072 if (FTy->getNumParams() != 3 ||
1073 !FTy->getParamType(1)->isPointerTy())
1075 // May throw; "read" is a valid pthread cancellation point.
1076 setDoesNotCapture(F, 2);
1078 case LibFunc::rewind:
1079 if (FTy->getNumParams() < 1 ||
1080 !FTy->getParamType(0)->isPointerTy())
1083 setDoesNotCapture(F, 1);
1085 case LibFunc::rmdir:
1086 case LibFunc::remove:
1087 case LibFunc::realpath:
1088 if (FTy->getNumParams() < 1 ||
1089 !FTy->getParamType(0)->isPointerTy())
1092 setDoesNotCapture(F, 1);
1093 setOnlyReadsMemory(F, 1);
1095 case LibFunc::rename:
1096 if (FTy->getNumParams() < 2 ||
1097 !FTy->getParamType(0)->isPointerTy() ||
1098 !FTy->getParamType(1)->isPointerTy())
1101 setDoesNotCapture(F, 1);
1102 setDoesNotCapture(F, 2);
1103 setOnlyReadsMemory(F, 1);
1104 setOnlyReadsMemory(F, 2);
1106 case LibFunc::readlink:
1107 if (FTy->getNumParams() < 2 ||
1108 !FTy->getParamType(0)->isPointerTy() ||
1109 !FTy->getParamType(1)->isPointerTy())
1112 setDoesNotCapture(F, 1);
1113 setDoesNotCapture(F, 2);
1114 setOnlyReadsMemory(F, 1);
1116 case LibFunc::write:
1117 if (FTy->getNumParams() != 3 || !FTy->getParamType(1)->isPointerTy())
1119 // May throw; "write" is a valid pthread cancellation point.
1120 setDoesNotCapture(F, 2);
1121 setOnlyReadsMemory(F, 2);
1123 case LibFunc::bcopy:
1124 if (FTy->getNumParams() != 3 ||
1125 !FTy->getParamType(0)->isPointerTy() ||
1126 !FTy->getParamType(1)->isPointerTy())
1129 setDoesNotCapture(F, 1);
1130 setDoesNotCapture(F, 2);
1131 setOnlyReadsMemory(F, 1);
1134 if (FTy->getNumParams() != 3 ||
1135 !FTy->getParamType(0)->isPointerTy() ||
1136 !FTy->getParamType(1)->isPointerTy())
1139 setOnlyReadsMemory(F);
1140 setDoesNotCapture(F, 1);
1141 setDoesNotCapture(F, 2);
1143 case LibFunc::bzero:
1144 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy())
1147 setDoesNotCapture(F, 1);
1149 case LibFunc::calloc:
1150 if (FTy->getNumParams() != 2 ||
1151 !FTy->getReturnType()->isPointerTy())
1154 setDoesNotAlias(F, 0);
1156 case LibFunc::chmod:
1157 case LibFunc::chown:
1158 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1161 setDoesNotCapture(F, 1);
1162 setOnlyReadsMemory(F, 1);
1164 case LibFunc::ctermid:
1165 case LibFunc::clearerr:
1166 case LibFunc::closedir:
1167 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1170 setDoesNotCapture(F, 1);
1175 case LibFunc::atoll:
1176 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1179 setOnlyReadsMemory(F);
1180 setDoesNotCapture(F, 1);
1182 case LibFunc::access:
1183 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy())
1186 setDoesNotCapture(F, 1);
1187 setOnlyReadsMemory(F, 1);
1189 case LibFunc::fopen:
1190 if (FTy->getNumParams() != 2 ||
1191 !FTy->getReturnType()->isPointerTy() ||
1192 !FTy->getParamType(0)->isPointerTy() ||
1193 !FTy->getParamType(1)->isPointerTy())
1196 setDoesNotAlias(F, 0);
1197 setDoesNotCapture(F, 1);
1198 setDoesNotCapture(F, 2);
1199 setOnlyReadsMemory(F, 1);
1200 setOnlyReadsMemory(F, 2);
1202 case LibFunc::fdopen:
1203 if (FTy->getNumParams() != 2 ||
1204 !FTy->getReturnType()->isPointerTy() ||
1205 !FTy->getParamType(1)->isPointerTy())
1208 setDoesNotAlias(F, 0);
1209 setDoesNotCapture(F, 2);
1210 setOnlyReadsMemory(F, 2);
1214 case LibFunc::fseek:
1215 case LibFunc::ftell:
1216 case LibFunc::fgetc:
1217 case LibFunc::fseeko:
1218 case LibFunc::ftello:
1219 case LibFunc::fileno:
1220 case LibFunc::fflush:
1221 case LibFunc::fclose:
1222 case LibFunc::fsetpos:
1223 case LibFunc::flockfile:
1224 case LibFunc::funlockfile:
1225 case LibFunc::ftrylockfile:
1226 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1229 setDoesNotCapture(F, 1);
1231 case LibFunc::ferror:
1232 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1235 setDoesNotCapture(F, 1);
1236 setOnlyReadsMemory(F);
1238 case LibFunc::fputc:
1239 case LibFunc::fstat:
1240 case LibFunc::frexp:
1241 case LibFunc::frexpf:
1242 case LibFunc::frexpl:
1243 case LibFunc::fstatvfs:
1244 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1247 setDoesNotCapture(F, 2);
1249 case LibFunc::fgets:
1250 if (FTy->getNumParams() != 3 ||
1251 !FTy->getParamType(0)->isPointerTy() ||
1252 !FTy->getParamType(2)->isPointerTy())
1255 setDoesNotCapture(F, 3);
1257 case LibFunc::fread:
1258 if (FTy->getNumParams() != 4 ||
1259 !FTy->getParamType(0)->isPointerTy() ||
1260 !FTy->getParamType(3)->isPointerTy())
1263 setDoesNotCapture(F, 1);
1264 setDoesNotCapture(F, 4);
1266 case LibFunc::fwrite:
1267 if (FTy->getNumParams() != 4 ||
1268 !FTy->getParamType(0)->isPointerTy() ||
1269 !FTy->getParamType(3)->isPointerTy())
1272 setDoesNotCapture(F, 1);
1273 setDoesNotCapture(F, 4);
1275 case LibFunc::fputs:
1276 if (FTy->getNumParams() < 2 ||
1277 !FTy->getParamType(0)->isPointerTy() ||
1278 !FTy->getParamType(1)->isPointerTy())
1281 setDoesNotCapture(F, 1);
1282 setDoesNotCapture(F, 2);
1283 setOnlyReadsMemory(F, 1);
1285 case LibFunc::fscanf:
1286 case LibFunc::fprintf:
1287 if (FTy->getNumParams() < 2 ||
1288 !FTy->getParamType(0)->isPointerTy() ||
1289 !FTy->getParamType(1)->isPointerTy())
1292 setDoesNotCapture(F, 1);
1293 setDoesNotCapture(F, 2);
1294 setOnlyReadsMemory(F, 2);
1296 case LibFunc::fgetpos:
1297 if (FTy->getNumParams() < 2 ||
1298 !FTy->getParamType(0)->isPointerTy() ||
1299 !FTy->getParamType(1)->isPointerTy())
1302 setDoesNotCapture(F, 1);
1303 setDoesNotCapture(F, 2);
1306 case LibFunc::getlogin_r:
1307 case LibFunc::getc_unlocked:
1308 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1311 setDoesNotCapture(F, 1);
1313 case LibFunc::getenv:
1314 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1317 setOnlyReadsMemory(F);
1318 setDoesNotCapture(F, 1);
1321 case LibFunc::getchar:
1324 case LibFunc::getitimer:
1325 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1328 setDoesNotCapture(F, 2);
1330 case LibFunc::getpwnam:
1331 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1334 setDoesNotCapture(F, 1);
1335 setOnlyReadsMemory(F, 1);
1337 case LibFunc::ungetc:
1338 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1341 setDoesNotCapture(F, 2);
1343 case LibFunc::uname:
1344 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1347 setDoesNotCapture(F, 1);
1349 case LibFunc::unlink:
1350 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1353 setDoesNotCapture(F, 1);
1354 setOnlyReadsMemory(F, 1);
1356 case LibFunc::unsetenv:
1357 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1360 setDoesNotCapture(F, 1);
1361 setOnlyReadsMemory(F, 1);
1363 case LibFunc::utime:
1364 case LibFunc::utimes:
1365 if (FTy->getNumParams() != 2 ||
1366 !FTy->getParamType(0)->isPointerTy() ||
1367 !FTy->getParamType(1)->isPointerTy())
1370 setDoesNotCapture(F, 1);
1371 setDoesNotCapture(F, 2);
1372 setOnlyReadsMemory(F, 1);
1373 setOnlyReadsMemory(F, 2);
1376 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1379 setDoesNotCapture(F, 2);
1382 case LibFunc::printf:
1383 case LibFunc::perror:
1384 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1387 setDoesNotCapture(F, 1);
1388 setOnlyReadsMemory(F, 1);
1390 case LibFunc::pread:
1391 if (FTy->getNumParams() != 4 || !FTy->getParamType(1)->isPointerTy())
1393 // May throw; "pread" is a valid pthread cancellation point.
1394 setDoesNotCapture(F, 2);
1396 case LibFunc::pwrite:
1397 if (FTy->getNumParams() != 4 || !FTy->getParamType(1)->isPointerTy())
1399 // May throw; "pwrite" is a valid pthread cancellation point.
1400 setDoesNotCapture(F, 2);
1401 setOnlyReadsMemory(F, 2);
1403 case LibFunc::putchar:
1406 case LibFunc::popen:
1407 if (FTy->getNumParams() != 2 ||
1408 !FTy->getReturnType()->isPointerTy() ||
1409 !FTy->getParamType(0)->isPointerTy() ||
1410 !FTy->getParamType(1)->isPointerTy())
1413 setDoesNotAlias(F, 0);
1414 setDoesNotCapture(F, 1);
1415 setDoesNotCapture(F, 2);
1416 setOnlyReadsMemory(F, 1);
1417 setOnlyReadsMemory(F, 2);
1419 case LibFunc::pclose:
1420 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1423 setDoesNotCapture(F, 1);
1425 case LibFunc::vscanf:
1426 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1429 setDoesNotCapture(F, 1);
1430 setOnlyReadsMemory(F, 1);
1432 case LibFunc::vsscanf:
1433 if (FTy->getNumParams() != 3 ||
1434 !FTy->getParamType(1)->isPointerTy() ||
1435 !FTy->getParamType(2)->isPointerTy())
1438 setDoesNotCapture(F, 1);
1439 setDoesNotCapture(F, 2);
1440 setOnlyReadsMemory(F, 1);
1441 setOnlyReadsMemory(F, 2);
1443 case LibFunc::vfscanf:
1444 if (FTy->getNumParams() != 3 ||
1445 !FTy->getParamType(1)->isPointerTy() ||
1446 !FTy->getParamType(2)->isPointerTy())
1449 setDoesNotCapture(F, 1);
1450 setDoesNotCapture(F, 2);
1451 setOnlyReadsMemory(F, 2);
1453 case LibFunc::valloc:
1454 if (!FTy->getReturnType()->isPointerTy())
1457 setDoesNotAlias(F, 0);
1459 case LibFunc::vprintf:
1460 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy())
1463 setDoesNotCapture(F, 1);
1464 setOnlyReadsMemory(F, 1);
1466 case LibFunc::vfprintf:
1467 case LibFunc::vsprintf:
1468 if (FTy->getNumParams() != 3 ||
1469 !FTy->getParamType(0)->isPointerTy() ||
1470 !FTy->getParamType(1)->isPointerTy())
1473 setDoesNotCapture(F, 1);
1474 setDoesNotCapture(F, 2);
1475 setOnlyReadsMemory(F, 2);
1477 case LibFunc::vsnprintf:
1478 if (FTy->getNumParams() != 4 ||
1479 !FTy->getParamType(0)->isPointerTy() ||
1480 !FTy->getParamType(2)->isPointerTy())
1483 setDoesNotCapture(F, 1);
1484 setDoesNotCapture(F, 3);
1485 setOnlyReadsMemory(F, 3);
1488 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy())
1490 // May throw; "open" is a valid pthread cancellation point.
1491 setDoesNotCapture(F, 1);
1492 setOnlyReadsMemory(F, 1);
1494 case LibFunc::opendir:
1495 if (FTy->getNumParams() != 1 ||
1496 !FTy->getReturnType()->isPointerTy() ||
1497 !FTy->getParamType(0)->isPointerTy())
1500 setDoesNotAlias(F, 0);
1501 setDoesNotCapture(F, 1);
1502 setOnlyReadsMemory(F, 1);
1504 case LibFunc::tmpfile:
1505 if (!FTy->getReturnType()->isPointerTy())
1508 setDoesNotAlias(F, 0);
1510 case LibFunc::times:
1511 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1514 setDoesNotCapture(F, 1);
1516 case LibFunc::htonl:
1517 case LibFunc::htons:
1518 case LibFunc::ntohl:
1519 case LibFunc::ntohs:
1521 setDoesNotAccessMemory(F);
1523 case LibFunc::lstat:
1524 if (FTy->getNumParams() != 2 ||
1525 !FTy->getParamType(0)->isPointerTy() ||
1526 !FTy->getParamType(1)->isPointerTy())
1529 setDoesNotCapture(F, 1);
1530 setDoesNotCapture(F, 2);
1531 setOnlyReadsMemory(F, 1);
1533 case LibFunc::lchown:
1534 if (FTy->getNumParams() != 3 || !FTy->getParamType(0)->isPointerTy())
1537 setDoesNotCapture(F, 1);
1538 setOnlyReadsMemory(F, 1);
1540 case LibFunc::qsort:
1541 if (FTy->getNumParams() != 4 || !FTy->getParamType(3)->isPointerTy())
1543 // May throw; places call through function pointer.
1544 setDoesNotCapture(F, 4);
1546 case LibFunc::dunder_strdup:
1547 case LibFunc::dunder_strndup:
1548 if (FTy->getNumParams() < 1 ||
1549 !FTy->getReturnType()->isPointerTy() ||
1550 !FTy->getParamType(0)->isPointerTy())
1553 setDoesNotAlias(F, 0);
1554 setDoesNotCapture(F, 1);
1555 setOnlyReadsMemory(F, 1);
1557 case LibFunc::dunder_strtok_r:
1558 if (FTy->getNumParams() != 3 ||
1559 !FTy->getParamType(1)->isPointerTy())
1562 setDoesNotCapture(F, 2);
1563 setOnlyReadsMemory(F, 2);
1565 case LibFunc::under_IO_getc:
1566 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1569 setDoesNotCapture(F, 1);
1571 case LibFunc::under_IO_putc:
1572 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1575 setDoesNotCapture(F, 2);
1577 case LibFunc::dunder_isoc99_scanf:
1578 if (FTy->getNumParams() < 1 ||
1579 !FTy->getParamType(0)->isPointerTy())
1582 setDoesNotCapture(F, 1);
1583 setOnlyReadsMemory(F, 1);
1585 case LibFunc::stat64:
1586 case LibFunc::lstat64:
1587 case LibFunc::statvfs64:
1588 if (FTy->getNumParams() < 1 ||
1589 !FTy->getParamType(0)->isPointerTy() ||
1590 !FTy->getParamType(1)->isPointerTy())
1593 setDoesNotCapture(F, 1);
1594 setDoesNotCapture(F, 2);
1595 setOnlyReadsMemory(F, 1);
1597 case LibFunc::dunder_isoc99_sscanf:
1598 if (FTy->getNumParams() < 1 ||
1599 !FTy->getParamType(0)->isPointerTy() ||
1600 !FTy->getParamType(1)->isPointerTy())
1603 setDoesNotCapture(F, 1);
1604 setDoesNotCapture(F, 2);
1605 setOnlyReadsMemory(F, 1);
1606 setOnlyReadsMemory(F, 2);
1608 case LibFunc::fopen64:
1609 if (FTy->getNumParams() != 2 ||
1610 !FTy->getReturnType()->isPointerTy() ||
1611 !FTy->getParamType(0)->isPointerTy() ||
1612 !FTy->getParamType(1)->isPointerTy())
1615 setDoesNotAlias(F, 0);
1616 setDoesNotCapture(F, 1);
1617 setDoesNotCapture(F, 2);
1618 setOnlyReadsMemory(F, 1);
1619 setOnlyReadsMemory(F, 2);
1621 case LibFunc::fseeko64:
1622 case LibFunc::ftello64:
1623 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1626 setDoesNotCapture(F, 1);
1628 case LibFunc::tmpfile64:
1629 if (!FTy->getReturnType()->isPointerTy())
1632 setDoesNotAlias(F, 0);
1634 case LibFunc::fstat64:
1635 case LibFunc::fstatvfs64:
1636 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1639 setDoesNotCapture(F, 2);
1641 case LibFunc::open64:
1642 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy())
1644 // May throw; "open" is a valid pthread cancellation point.
1645 setDoesNotCapture(F, 1);
1646 setOnlyReadsMemory(F, 1);
1648 case LibFunc::gettimeofday:
1649 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy() ||
1650 !FTy->getParamType(1)->isPointerTy())
1652 // Currently some platforms have the restrict keyword on the arguments to
1653 // gettimeofday. To be conservative, do not add noalias to gettimeofday's
1656 setDoesNotCapture(F, 1);
1657 setDoesNotCapture(F, 2);
1659 // Didn't mark any attributes.
1666 /// annotateLibraryCalls - Adds attributes to well-known standard library
1667 /// call declarations.
1668 bool FunctionAttrs::annotateLibraryCalls(const CallGraphSCC &SCC) {
1669 bool MadeChange = false;
1671 // Check each function in turn annotating well-known library function
1672 // declarations with attributes.
1673 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
1674 Function *F = (*I)->getFunction();
1676 if (F != 0 && F->isDeclaration())
1677 MadeChange |= inferPrototypeAttributes(*F);
1683 bool FunctionAttrs::runOnSCC(CallGraphSCC &SCC) {
1684 AA = &getAnalysis<AliasAnalysis>();
1685 TLI = &getAnalysis<TargetLibraryInfo>();
1687 bool Changed = annotateLibraryCalls(SCC);
1688 Changed |= AddReadAttrs(SCC);
1689 Changed |= AddArgumentAttrs(SCC);
1690 Changed |= AddNoAliasAttrs(SCC);