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 #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/CallGraph.h"
28 #include "llvm/Analysis/CallGraphSCCPass.h"
29 #include "llvm/Analysis/CaptureTracking.h"
30 #include "llvm/IR/GlobalVariable.h"
31 #include "llvm/IR/InstIterator.h"
32 #include "llvm/IR/IntrinsicInst.h"
33 #include "llvm/IR/LLVMContext.h"
34 #include "llvm/Target/TargetLibraryInfo.h"
37 #define DEBUG_TYPE "functionattrs"
39 STATISTIC(NumReadNone, "Number of functions marked readnone");
40 STATISTIC(NumReadOnly, "Number of functions marked readonly");
41 STATISTIC(NumNoCapture, "Number of arguments marked nocapture");
42 STATISTIC(NumReadNoneArg, "Number of arguments marked readnone");
43 STATISTIC(NumReadOnlyArg, "Number of arguments marked readonly");
44 STATISTIC(NumNoAlias, "Number of function returns marked noalias");
45 STATISTIC(NumAnnotated, "Number of attributes added to library functions");
48 struct FunctionAttrs : public CallGraphSCCPass {
49 static char ID; // Pass identification, replacement for typeid
50 FunctionAttrs() : CallGraphSCCPass(ID), AA(nullptr) {
51 initializeFunctionAttrsPass(*PassRegistry::getPassRegistry());
54 // runOnSCC - Analyze the SCC, performing the transformation if possible.
55 bool runOnSCC(CallGraphSCC &SCC) override;
57 // AddReadAttrs - Deduce readonly/readnone attributes for the SCC.
58 bool AddReadAttrs(const CallGraphSCC &SCC);
60 // AddArgumentAttrs - Deduce nocapture attributes for the SCC.
61 bool AddArgumentAttrs(const CallGraphSCC &SCC);
63 // IsFunctionMallocLike - Does this function allocate new memory?
64 bool IsFunctionMallocLike(Function *F,
65 SmallPtrSet<Function*, 8> &) const;
67 // AddNoAliasAttrs - Deduce noalias attributes for the SCC.
68 bool AddNoAliasAttrs(const CallGraphSCC &SCC);
70 // Utility methods used by inferPrototypeAttributes to add attributes
71 // and maintain annotation statistics.
73 void setDoesNotAccessMemory(Function &F) {
74 if (!F.doesNotAccessMemory()) {
75 F.setDoesNotAccessMemory();
80 void setOnlyReadsMemory(Function &F) {
81 if (!F.onlyReadsMemory()) {
82 F.setOnlyReadsMemory();
87 void setDoesNotThrow(Function &F) {
88 if (!F.doesNotThrow()) {
94 void setDoesNotCapture(Function &F, unsigned n) {
95 if (!F.doesNotCapture(n)) {
96 F.setDoesNotCapture(n);
101 void setOnlyReadsMemory(Function &F, unsigned n) {
102 if (!F.onlyReadsMemory(n)) {
103 F.setOnlyReadsMemory(n);
108 void setDoesNotAlias(Function &F, unsigned n) {
109 if (!F.doesNotAlias(n)) {
110 F.setDoesNotAlias(n);
115 // inferPrototypeAttributes - Analyze the name and prototype of the
116 // given function and set any applicable attributes. Returns true
117 // if any attributes were set and false otherwise.
118 bool inferPrototypeAttributes(Function &F);
120 // annotateLibraryCalls - Adds attributes to well-known standard library
121 // call declarations.
122 bool annotateLibraryCalls(const CallGraphSCC &SCC);
124 void getAnalysisUsage(AnalysisUsage &AU) const override {
125 AU.setPreservesCFG();
126 AU.addRequired<AliasAnalysis>();
127 AU.addRequired<TargetLibraryInfo>();
128 CallGraphSCCPass::getAnalysisUsage(AU);
133 TargetLibraryInfo *TLI;
137 char FunctionAttrs::ID = 0;
138 INITIALIZE_PASS_BEGIN(FunctionAttrs, "functionattrs",
139 "Deduce function attributes", false, false)
140 INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
141 INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
142 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfo)
143 INITIALIZE_PASS_END(FunctionAttrs, "functionattrs",
144 "Deduce function attributes", false, false)
146 Pass *llvm::createFunctionAttrsPass() { return new FunctionAttrs(); }
149 /// AddReadAttrs - Deduce readonly/readnone attributes for the SCC.
150 bool FunctionAttrs::AddReadAttrs(const CallGraphSCC &SCC) {
151 SmallPtrSet<Function*, 8> SCCNodes;
153 // Fill SCCNodes with the elements of the SCC. Used for quickly
154 // looking up whether a given CallGraphNode is in this SCC.
155 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I)
156 SCCNodes.insert((*I)->getFunction());
158 // Check if any of the functions in the SCC read or write memory. If they
159 // write memory then they can't be marked readnone or readonly.
160 bool ReadsMemory = false;
161 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
162 Function *F = (*I)->getFunction();
165 // External node - may write memory. Just give up.
168 AliasAnalysis::ModRefBehavior MRB = AA->getModRefBehavior(F);
169 if (MRB == AliasAnalysis::DoesNotAccessMemory)
173 // Definitions with weak linkage may be overridden at linktime with
174 // something that writes memory, so treat them like declarations.
175 if (F->isDeclaration() || F->mayBeOverridden()) {
176 if (!AliasAnalysis::onlyReadsMemory(MRB))
177 // May write memory. Just give up.
184 // Scan the function body for instructions that may read or write memory.
185 for (inst_iterator II = inst_begin(F), E = inst_end(F); II != E; ++II) {
186 Instruction *I = &*II;
188 // Some instructions can be ignored even if they read or write memory.
189 // Detect these now, skipping to the next instruction if one is found.
190 CallSite CS(cast<Value>(I));
192 // Ignore calls to functions in the same SCC.
193 if (CS.getCalledFunction() && SCCNodes.count(CS.getCalledFunction()))
195 AliasAnalysis::ModRefBehavior MRB = AA->getModRefBehavior(CS);
196 // If the call doesn't access arbitrary memory, we may be able to
197 // figure out something.
198 if (AliasAnalysis::onlyAccessesArgPointees(MRB)) {
199 // If the call does access argument pointees, check each argument.
200 if (AliasAnalysis::doesAccessArgPointees(MRB))
201 // Check whether all pointer arguments point to local memory, and
202 // ignore calls that only access local memory.
203 for (CallSite::arg_iterator CI = CS.arg_begin(), CE = CS.arg_end();
206 if (Arg->getType()->isPointerTy()) {
207 AliasAnalysis::Location Loc(Arg,
208 AliasAnalysis::UnknownSize,
209 I->getMetadata(LLVMContext::MD_tbaa));
210 if (!AA->pointsToConstantMemory(Loc, /*OrLocal=*/true)) {
211 if (MRB & AliasAnalysis::Mod)
212 // Writes non-local memory. Give up.
214 if (MRB & AliasAnalysis::Ref)
215 // Ok, it reads non-local memory.
222 // The call could access any memory. If that includes writes, give up.
223 if (MRB & AliasAnalysis::Mod)
225 // If it reads, note it.
226 if (MRB & AliasAnalysis::Ref)
229 } else if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
230 // Ignore non-volatile loads from local memory. (Atomic is okay here.)
231 if (!LI->isVolatile()) {
232 AliasAnalysis::Location Loc = AA->getLocation(LI);
233 if (AA->pointsToConstantMemory(Loc, /*OrLocal=*/true))
236 } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
237 // Ignore non-volatile stores to local memory. (Atomic is okay here.)
238 if (!SI->isVolatile()) {
239 AliasAnalysis::Location Loc = AA->getLocation(SI);
240 if (AA->pointsToConstantMemory(Loc, /*OrLocal=*/true))
243 } else if (VAArgInst *VI = dyn_cast<VAArgInst>(I)) {
244 // Ignore vaargs on local memory.
245 AliasAnalysis::Location Loc = AA->getLocation(VI);
246 if (AA->pointsToConstantMemory(Loc, /*OrLocal=*/true))
250 // Any remaining instructions need to be taken seriously! Check if they
251 // read or write memory.
252 if (I->mayWriteToMemory())
253 // Writes memory. Just give up.
256 // If this instruction may read memory, remember that.
257 ReadsMemory |= I->mayReadFromMemory();
261 // Success! Functions in this SCC do not access memory, or only read memory.
262 // Give them the appropriate attribute.
263 bool MadeChange = false;
264 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
265 Function *F = (*I)->getFunction();
267 if (F->doesNotAccessMemory())
271 if (F->onlyReadsMemory() && ReadsMemory)
277 // Clear out any existing attributes.
279 B.addAttribute(Attribute::ReadOnly)
280 .addAttribute(Attribute::ReadNone);
281 F->removeAttributes(AttributeSet::FunctionIndex,
282 AttributeSet::get(F->getContext(),
283 AttributeSet::FunctionIndex, B));
285 // Add in the new attribute.
286 F->addAttribute(AttributeSet::FunctionIndex,
287 ReadsMemory ? Attribute::ReadOnly : Attribute::ReadNone);
299 // For a given pointer Argument, this retains a list of Arguments of functions
300 // in the same SCC that the pointer data flows into. We use this to build an
301 // SCC of the arguments.
302 struct ArgumentGraphNode {
303 Argument *Definition;
304 SmallVector<ArgumentGraphNode*, 4> Uses;
307 class ArgumentGraph {
308 // We store pointers to ArgumentGraphNode objects, so it's important that
309 // that they not move around upon insert.
310 typedef std::map<Argument*, ArgumentGraphNode> ArgumentMapTy;
312 ArgumentMapTy ArgumentMap;
314 // There is no root node for the argument graph, in fact:
315 // void f(int *x, int *y) { if (...) f(x, y); }
316 // is an example where the graph is disconnected. The SCCIterator requires a
317 // single entry point, so we maintain a fake ("synthetic") root node that
318 // uses every node. Because the graph is directed and nothing points into
319 // the root, it will not participate in any SCCs (except for its own).
320 ArgumentGraphNode SyntheticRoot;
323 ArgumentGraph() { SyntheticRoot.Definition = nullptr; }
325 typedef SmallVectorImpl<ArgumentGraphNode*>::iterator iterator;
327 iterator begin() { return SyntheticRoot.Uses.begin(); }
328 iterator end() { return SyntheticRoot.Uses.end(); }
329 ArgumentGraphNode *getEntryNode() { return &SyntheticRoot; }
331 ArgumentGraphNode *operator[](Argument *A) {
332 ArgumentGraphNode &Node = ArgumentMap[A];
334 SyntheticRoot.Uses.push_back(&Node);
339 // This tracker checks whether callees are in the SCC, and if so it does not
340 // consider that a capture, instead adding it to the "Uses" list and
341 // continuing with the analysis.
342 struct ArgumentUsesTracker : public CaptureTracker {
343 ArgumentUsesTracker(const SmallPtrSet<Function*, 8> &SCCNodes)
344 : Captured(false), SCCNodes(SCCNodes) {}
346 void tooManyUses() override { Captured = true; }
348 bool captured(const Use *U) override {
349 CallSite CS(U->getUser());
350 if (!CS.getInstruction()) { Captured = true; return true; }
352 Function *F = CS.getCalledFunction();
353 if (!F || !SCCNodes.count(F)) { Captured = true; return true; }
356 Function::arg_iterator AI = F->arg_begin(), AE = F->arg_end();
357 for (CallSite::arg_iterator PI = CS.arg_begin(), PE = CS.arg_end();
358 PI != PE; ++PI, ++AI) {
360 assert(F->isVarArg() && "More params than args in non-varargs call");
370 assert(Found && "Capturing call-site captured nothing?");
375 bool Captured; // True only if certainly captured (used outside our SCC).
376 SmallVector<Argument*, 4> Uses; // Uses within our SCC.
378 const SmallPtrSet<Function*, 8> &SCCNodes;
383 template<> struct GraphTraits<ArgumentGraphNode*> {
384 typedef ArgumentGraphNode NodeType;
385 typedef SmallVectorImpl<ArgumentGraphNode*>::iterator ChildIteratorType;
387 static inline NodeType *getEntryNode(NodeType *A) { return A; }
388 static inline ChildIteratorType child_begin(NodeType *N) {
389 return N->Uses.begin();
391 static inline ChildIteratorType child_end(NodeType *N) {
392 return N->Uses.end();
395 template<> struct GraphTraits<ArgumentGraph*>
396 : public GraphTraits<ArgumentGraphNode*> {
397 static NodeType *getEntryNode(ArgumentGraph *AG) {
398 return AG->getEntryNode();
400 static ChildIteratorType nodes_begin(ArgumentGraph *AG) {
403 static ChildIteratorType nodes_end(ArgumentGraph *AG) {
409 // Returns Attribute::None, Attribute::ReadOnly or Attribute::ReadNone.
410 static Attribute::AttrKind
411 determinePointerReadAttrs(Argument *A,
412 const SmallPtrSet<Argument*, 8> &SCCNodes) {
414 SmallVector<Use*, 32> Worklist;
415 SmallSet<Use*, 32> Visited;
418 // inalloca arguments are always clobbered by the call.
419 if (A->hasInAllocaAttr())
420 return Attribute::None;
423 // We don't need to track IsWritten. If A is written to, return immediately.
425 for (Use &U : A->uses()) {
427 return Attribute::None;
430 Worklist.push_back(&U);
433 while (!Worklist.empty()) {
434 Use *U = Worklist.pop_back_val();
435 Instruction *I = cast<Instruction>(U->getUser());
438 switch (I->getOpcode()) {
439 case Instruction::BitCast:
440 case Instruction::GetElementPtr:
441 case Instruction::PHI:
442 case Instruction::Select:
443 case Instruction::AddrSpaceCast:
444 // The original value is not read/written via this if the new value isn't.
445 for (Use &UU : I->uses())
446 if (Visited.insert(&UU))
447 Worklist.push_back(&UU);
450 case Instruction::Call:
451 case Instruction::Invoke: {
453 if (CS.doesNotAccessMemory())
456 Function *F = CS.getCalledFunction();
458 if (CS.onlyReadsMemory()) {
462 return Attribute::None;
465 Function::arg_iterator AI = F->arg_begin(), AE = F->arg_end();
466 CallSite::arg_iterator B = CS.arg_begin(), E = CS.arg_end();
467 for (CallSite::arg_iterator A = B; A != E; ++A, ++AI) {
470 assert(F->isVarArg() &&
471 "More params than args in non-varargs call.");
472 return Attribute::None;
474 if (SCCNodes.count(AI))
476 if (!CS.onlyReadsMemory() && !CS.onlyReadsMemory(A - B))
477 return Attribute::None;
478 if (!CS.doesNotAccessMemory(A - B))
485 case Instruction::Load:
489 case Instruction::ICmp:
490 case Instruction::Ret:
494 return Attribute::None;
498 return IsRead ? Attribute::ReadOnly : Attribute::ReadNone;
501 /// AddArgumentAttrs - Deduce nocapture attributes for the SCC.
502 bool FunctionAttrs::AddArgumentAttrs(const CallGraphSCC &SCC) {
503 bool Changed = false;
505 SmallPtrSet<Function*, 8> SCCNodes;
507 // Fill SCCNodes with the elements of the SCC. Used for quickly
508 // looking up whether a given CallGraphNode is in this SCC.
509 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
510 Function *F = (*I)->getFunction();
511 if (F && !F->isDeclaration() && !F->mayBeOverridden())
518 B.addAttribute(Attribute::NoCapture);
520 // Check each function in turn, determining which pointer arguments are not
522 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
523 Function *F = (*I)->getFunction();
526 // External node - only a problem for arguments that we pass to it.
529 // Definitions with weak linkage may be overridden at linktime with
530 // something that captures pointers, so treat them like declarations.
531 if (F->isDeclaration() || F->mayBeOverridden())
534 // Functions that are readonly (or readnone) and nounwind and don't return
535 // a value can't capture arguments. Don't analyze them.
536 if (F->onlyReadsMemory() && F->doesNotThrow() &&
537 F->getReturnType()->isVoidTy()) {
538 for (Function::arg_iterator A = F->arg_begin(), E = F->arg_end();
540 if (A->getType()->isPointerTy() && !A->hasNoCaptureAttr()) {
541 A->addAttr(AttributeSet::get(F->getContext(), A->getArgNo() + 1, B));
549 for (Function::arg_iterator A = F->arg_begin(), E = F->arg_end();
551 if (!A->getType()->isPointerTy()) continue;
552 bool HasNonLocalUses = false;
553 if (!A->hasNoCaptureAttr()) {
554 ArgumentUsesTracker Tracker(SCCNodes);
555 PointerMayBeCaptured(A, &Tracker);
556 if (!Tracker.Captured) {
557 if (Tracker.Uses.empty()) {
558 // If it's trivially not captured, mark it nocapture now.
559 A->addAttr(AttributeSet::get(F->getContext(), A->getArgNo()+1, B));
563 // If it's not trivially captured and not trivially not captured,
564 // then it must be calling into another function in our SCC. Save
565 // its particulars for Argument-SCC analysis later.
566 ArgumentGraphNode *Node = AG[A];
567 for (SmallVectorImpl<Argument*>::iterator UI = Tracker.Uses.begin(),
568 UE = Tracker.Uses.end(); UI != UE; ++UI) {
569 Node->Uses.push_back(AG[*UI]);
571 HasNonLocalUses = true;
575 // Otherwise, it's captured. Don't bother doing SCC analysis on it.
577 if (!HasNonLocalUses && !A->onlyReadsMemory()) {
578 // Can we determine that it's readonly/readnone without doing an SCC?
579 // Note that we don't allow any calls at all here, or else our result
580 // will be dependent on the iteration order through the functions in the
582 SmallPtrSet<Argument*, 8> Self;
584 Attribute::AttrKind R = determinePointerReadAttrs(A, Self);
585 if (R != Attribute::None) {
588 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
590 R == Attribute::ReadOnly ? ++NumReadOnlyArg : ++NumReadNoneArg;
596 // The graph we've collected is partial because we stopped scanning for
597 // argument uses once we solved the argument trivially. These partial nodes
598 // show up as ArgumentGraphNode objects with an empty Uses list, and for
599 // these nodes the final decision about whether they capture has already been
600 // made. If the definition doesn't have a 'nocapture' attribute by now, it
603 for (scc_iterator<ArgumentGraph*> I = scc_begin(&AG); !I.isAtEnd(); ++I) {
604 std::vector<ArgumentGraphNode*> &ArgumentSCC = *I;
605 if (ArgumentSCC.size() == 1) {
606 if (!ArgumentSCC[0]->Definition) continue; // synthetic root node
608 // eg. "void f(int* x) { if (...) f(x); }"
609 if (ArgumentSCC[0]->Uses.size() == 1 &&
610 ArgumentSCC[0]->Uses[0] == ArgumentSCC[0]) {
611 Argument *A = ArgumentSCC[0]->Definition;
612 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
619 bool SCCCaptured = false;
620 for (std::vector<ArgumentGraphNode*>::iterator I = ArgumentSCC.begin(),
621 E = ArgumentSCC.end(); I != E && !SCCCaptured; ++I) {
622 ArgumentGraphNode *Node = *I;
623 if (Node->Uses.empty()) {
624 if (!Node->Definition->hasNoCaptureAttr())
628 if (SCCCaptured) continue;
630 SmallPtrSet<Argument*, 8> ArgumentSCCNodes;
631 // Fill ArgumentSCCNodes with the elements of the ArgumentSCC. Used for
632 // quickly looking up whether a given Argument is in this ArgumentSCC.
633 for (std::vector<ArgumentGraphNode*>::iterator I = ArgumentSCC.begin(),
634 E = ArgumentSCC.end(); I != E; ++I) {
635 ArgumentSCCNodes.insert((*I)->Definition);
638 for (std::vector<ArgumentGraphNode*>::iterator I = ArgumentSCC.begin(),
639 E = ArgumentSCC.end(); I != E && !SCCCaptured; ++I) {
640 ArgumentGraphNode *N = *I;
641 for (SmallVectorImpl<ArgumentGraphNode*>::iterator UI = N->Uses.begin(),
642 UE = N->Uses.end(); UI != UE; ++UI) {
643 Argument *A = (*UI)->Definition;
644 if (A->hasNoCaptureAttr() || ArgumentSCCNodes.count(A))
650 if (SCCCaptured) continue;
652 for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) {
653 Argument *A = ArgumentSCC[i]->Definition;
654 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
659 // We also want to compute readonly/readnone. With a small number of false
660 // negatives, we can assume that any pointer which is captured isn't going
661 // to be provably readonly or readnone, since by definition we can't
662 // analyze all uses of a captured pointer.
664 // The false negatives happen when the pointer is captured by a function
665 // that promises readonly/readnone behaviour on the pointer, then the
666 // pointer's lifetime ends before anything that writes to arbitrary memory.
667 // Also, a readonly/readnone pointer may be returned, but returning a
668 // pointer is capturing it.
670 Attribute::AttrKind ReadAttr = Attribute::ReadNone;
671 for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) {
672 Argument *A = ArgumentSCC[i]->Definition;
673 Attribute::AttrKind K = determinePointerReadAttrs(A, ArgumentSCCNodes);
674 if (K == Attribute::ReadNone)
676 if (K == Attribute::ReadOnly) {
677 ReadAttr = Attribute::ReadOnly;
684 if (ReadAttr != Attribute::None) {
686 B.addAttribute(ReadAttr);
687 for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) {
688 Argument *A = ArgumentSCC[i]->Definition;
689 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
690 ReadAttr == Attribute::ReadOnly ? ++NumReadOnlyArg : ++NumReadNoneArg;
699 /// IsFunctionMallocLike - A function is malloc-like if it returns either null
700 /// or a pointer that doesn't alias any other pointer visible to the caller.
701 bool FunctionAttrs::IsFunctionMallocLike(Function *F,
702 SmallPtrSet<Function*, 8> &SCCNodes) const {
703 SmallSetVector<Value *, 8> FlowsToReturn;
704 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I)
705 if (ReturnInst *Ret = dyn_cast<ReturnInst>(I->getTerminator()))
706 FlowsToReturn.insert(Ret->getReturnValue());
708 for (unsigned i = 0; i != FlowsToReturn.size(); ++i) {
709 Value *RetVal = FlowsToReturn[i];
711 if (Constant *C = dyn_cast<Constant>(RetVal)) {
712 if (!C->isNullValue() && !isa<UndefValue>(C))
718 if (isa<Argument>(RetVal))
721 if (Instruction *RVI = dyn_cast<Instruction>(RetVal))
722 switch (RVI->getOpcode()) {
723 // Extend the analysis by looking upwards.
724 case Instruction::BitCast:
725 case Instruction::GetElementPtr:
726 case Instruction::AddrSpaceCast:
727 FlowsToReturn.insert(RVI->getOperand(0));
729 case Instruction::Select: {
730 SelectInst *SI = cast<SelectInst>(RVI);
731 FlowsToReturn.insert(SI->getTrueValue());
732 FlowsToReturn.insert(SI->getFalseValue());
735 case Instruction::PHI: {
736 PHINode *PN = cast<PHINode>(RVI);
737 for (int i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
738 FlowsToReturn.insert(PN->getIncomingValue(i));
742 // Check whether the pointer came from an allocation.
743 case Instruction::Alloca:
745 case Instruction::Call:
746 case Instruction::Invoke: {
748 if (CS.paramHasAttr(0, Attribute::NoAlias))
750 if (CS.getCalledFunction() &&
751 SCCNodes.count(CS.getCalledFunction()))
755 return false; // Did not come from an allocation.
758 if (PointerMayBeCaptured(RetVal, false, /*StoreCaptures=*/false))
765 /// AddNoAliasAttrs - Deduce noalias attributes for the SCC.
766 bool FunctionAttrs::AddNoAliasAttrs(const CallGraphSCC &SCC) {
767 SmallPtrSet<Function*, 8> SCCNodes;
769 // Fill SCCNodes with the elements of the SCC. Used for quickly
770 // looking up whether a given CallGraphNode is in this SCC.
771 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I)
772 SCCNodes.insert((*I)->getFunction());
774 // Check each function in turn, determining which functions return noalias
776 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
777 Function *F = (*I)->getFunction();
780 // External node - skip it;
784 if (F->doesNotAlias(0))
787 // Definitions with weak linkage may be overridden at linktime, so
788 // treat them like declarations.
789 if (F->isDeclaration() || F->mayBeOverridden())
792 // We annotate noalias return values, which are only applicable to
794 if (!F->getReturnType()->isPointerTy())
797 if (!IsFunctionMallocLike(F, SCCNodes))
801 bool MadeChange = false;
802 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
803 Function *F = (*I)->getFunction();
804 if (F->doesNotAlias(0) || !F->getReturnType()->isPointerTy())
807 F->setDoesNotAlias(0);
815 /// inferPrototypeAttributes - Analyze the name and prototype of the
816 /// given function and set any applicable attributes. Returns true
817 /// if any attributes were set and false otherwise.
818 bool FunctionAttrs::inferPrototypeAttributes(Function &F) {
819 FunctionType *FTy = F.getFunctionType();
820 LibFunc::Func TheLibFunc;
821 if (!(TLI->getLibFunc(F.getName(), TheLibFunc) && TLI->has(TheLibFunc)))
824 switch (TheLibFunc) {
825 case LibFunc::strlen:
826 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
828 setOnlyReadsMemory(F);
830 setDoesNotCapture(F, 1);
832 case LibFunc::strchr:
833 case LibFunc::strrchr:
834 if (FTy->getNumParams() != 2 ||
835 !FTy->getParamType(0)->isPointerTy() ||
836 !FTy->getParamType(1)->isIntegerTy())
838 setOnlyReadsMemory(F);
841 case LibFunc::strtol:
842 case LibFunc::strtod:
843 case LibFunc::strtof:
844 case LibFunc::strtoul:
845 case LibFunc::strtoll:
846 case LibFunc::strtold:
847 case LibFunc::strtoull:
848 if (FTy->getNumParams() < 2 ||
849 !FTy->getParamType(1)->isPointerTy())
852 setDoesNotCapture(F, 2);
853 setOnlyReadsMemory(F, 1);
855 case LibFunc::strcpy:
856 case LibFunc::stpcpy:
857 case LibFunc::strcat:
858 case LibFunc::strncat:
859 case LibFunc::strncpy:
860 case LibFunc::stpncpy:
861 if (FTy->getNumParams() < 2 ||
862 !FTy->getParamType(1)->isPointerTy())
865 setDoesNotCapture(F, 2);
866 setOnlyReadsMemory(F, 2);
868 case LibFunc::strxfrm:
869 if (FTy->getNumParams() != 3 ||
870 !FTy->getParamType(0)->isPointerTy() ||
871 !FTy->getParamType(1)->isPointerTy())
874 setDoesNotCapture(F, 1);
875 setDoesNotCapture(F, 2);
876 setOnlyReadsMemory(F, 2);
878 case LibFunc::strcmp: //0,1
879 case LibFunc::strspn: // 0,1
880 case LibFunc::strncmp: // 0,1
881 case LibFunc::strcspn: //0,1
882 case LibFunc::strcoll: //0,1
883 case LibFunc::strcasecmp: // 0,1
884 case LibFunc::strncasecmp: //
885 if (FTy->getNumParams() < 2 ||
886 !FTy->getParamType(0)->isPointerTy() ||
887 !FTy->getParamType(1)->isPointerTy())
889 setOnlyReadsMemory(F);
891 setDoesNotCapture(F, 1);
892 setDoesNotCapture(F, 2);
894 case LibFunc::strstr:
895 case LibFunc::strpbrk:
896 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
898 setOnlyReadsMemory(F);
900 setDoesNotCapture(F, 2);
902 case LibFunc::strtok:
903 case LibFunc::strtok_r:
904 if (FTy->getNumParams() < 2 || !FTy->getParamType(1)->isPointerTy())
907 setDoesNotCapture(F, 2);
908 setOnlyReadsMemory(F, 2);
911 if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy())
914 setDoesNotCapture(F, 1);
915 setOnlyReadsMemory(F, 1);
917 case LibFunc::setbuf:
918 case LibFunc::setvbuf:
919 if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy())
922 setDoesNotCapture(F, 1);
924 case LibFunc::strdup:
925 case LibFunc::strndup:
926 if (FTy->getNumParams() < 1 || !FTy->getReturnType()->isPointerTy() ||
927 !FTy->getParamType(0)->isPointerTy())
930 setDoesNotAlias(F, 0);
931 setDoesNotCapture(F, 1);
932 setOnlyReadsMemory(F, 1);
935 case LibFunc::statvfs:
936 if (FTy->getNumParams() < 2 ||
937 !FTy->getParamType(0)->isPointerTy() ||
938 !FTy->getParamType(1)->isPointerTy())
941 setDoesNotCapture(F, 1);
942 setDoesNotCapture(F, 2);
943 setOnlyReadsMemory(F, 1);
945 case LibFunc::sscanf:
946 if (FTy->getNumParams() < 2 ||
947 !FTy->getParamType(0)->isPointerTy() ||
948 !FTy->getParamType(1)->isPointerTy())
951 setDoesNotCapture(F, 1);
952 setDoesNotCapture(F, 2);
953 setOnlyReadsMemory(F, 1);
954 setOnlyReadsMemory(F, 2);
956 case LibFunc::sprintf:
957 if (FTy->getNumParams() < 2 ||
958 !FTy->getParamType(0)->isPointerTy() ||
959 !FTy->getParamType(1)->isPointerTy())
962 setDoesNotCapture(F, 1);
963 setDoesNotCapture(F, 2);
964 setOnlyReadsMemory(F, 2);
966 case LibFunc::snprintf:
967 if (FTy->getNumParams() != 3 ||
968 !FTy->getParamType(0)->isPointerTy() ||
969 !FTy->getParamType(2)->isPointerTy())
972 setDoesNotCapture(F, 1);
973 setDoesNotCapture(F, 3);
974 setOnlyReadsMemory(F, 3);
976 case LibFunc::setitimer:
977 if (FTy->getNumParams() != 3 ||
978 !FTy->getParamType(1)->isPointerTy() ||
979 !FTy->getParamType(2)->isPointerTy())
982 setDoesNotCapture(F, 2);
983 setDoesNotCapture(F, 3);
984 setOnlyReadsMemory(F, 2);
986 case LibFunc::system:
987 if (FTy->getNumParams() != 1 ||
988 !FTy->getParamType(0)->isPointerTy())
990 // May throw; "system" is a valid pthread cancellation point.
991 setDoesNotCapture(F, 1);
992 setOnlyReadsMemory(F, 1);
994 case LibFunc::malloc:
995 if (FTy->getNumParams() != 1 ||
996 !FTy->getReturnType()->isPointerTy())
999 setDoesNotAlias(F, 0);
1001 case LibFunc::memcmp:
1002 if (FTy->getNumParams() != 3 ||
1003 !FTy->getParamType(0)->isPointerTy() ||
1004 !FTy->getParamType(1)->isPointerTy())
1006 setOnlyReadsMemory(F);
1008 setDoesNotCapture(F, 1);
1009 setDoesNotCapture(F, 2);
1011 case LibFunc::memchr:
1012 case LibFunc::memrchr:
1013 if (FTy->getNumParams() != 3)
1015 setOnlyReadsMemory(F);
1019 case LibFunc::modff:
1020 case LibFunc::modfl:
1021 if (FTy->getNumParams() < 2 ||
1022 !FTy->getParamType(1)->isPointerTy())
1025 setDoesNotCapture(F, 2);
1027 case LibFunc::memcpy:
1028 case LibFunc::memccpy:
1029 case LibFunc::memmove:
1030 if (FTy->getNumParams() < 2 ||
1031 !FTy->getParamType(1)->isPointerTy())
1034 setDoesNotCapture(F, 2);
1035 setOnlyReadsMemory(F, 2);
1037 case LibFunc::memalign:
1038 if (!FTy->getReturnType()->isPointerTy())
1040 setDoesNotAlias(F, 0);
1042 case LibFunc::mkdir:
1043 if (FTy->getNumParams() == 0 ||
1044 !FTy->getParamType(0)->isPointerTy())
1047 setDoesNotCapture(F, 1);
1048 setOnlyReadsMemory(F, 1);
1050 case LibFunc::mktime:
1051 if (FTy->getNumParams() == 0 ||
1052 !FTy->getParamType(0)->isPointerTy())
1055 setDoesNotCapture(F, 1);
1057 case LibFunc::realloc:
1058 if (FTy->getNumParams() != 2 ||
1059 !FTy->getParamType(0)->isPointerTy() ||
1060 !FTy->getReturnType()->isPointerTy())
1063 setDoesNotAlias(F, 0);
1064 setDoesNotCapture(F, 1);
1067 if (FTy->getNumParams() != 3 ||
1068 !FTy->getParamType(1)->isPointerTy())
1070 // May throw; "read" is a valid pthread cancellation point.
1071 setDoesNotCapture(F, 2);
1073 case LibFunc::rewind:
1074 if (FTy->getNumParams() < 1 ||
1075 !FTy->getParamType(0)->isPointerTy())
1078 setDoesNotCapture(F, 1);
1080 case LibFunc::rmdir:
1081 case LibFunc::remove:
1082 case LibFunc::realpath:
1083 if (FTy->getNumParams() < 1 ||
1084 !FTy->getParamType(0)->isPointerTy())
1087 setDoesNotCapture(F, 1);
1088 setOnlyReadsMemory(F, 1);
1090 case LibFunc::rename:
1091 if (FTy->getNumParams() < 2 ||
1092 !FTy->getParamType(0)->isPointerTy() ||
1093 !FTy->getParamType(1)->isPointerTy())
1096 setDoesNotCapture(F, 1);
1097 setDoesNotCapture(F, 2);
1098 setOnlyReadsMemory(F, 1);
1099 setOnlyReadsMemory(F, 2);
1101 case LibFunc::readlink:
1102 if (FTy->getNumParams() < 2 ||
1103 !FTy->getParamType(0)->isPointerTy() ||
1104 !FTy->getParamType(1)->isPointerTy())
1107 setDoesNotCapture(F, 1);
1108 setDoesNotCapture(F, 2);
1109 setOnlyReadsMemory(F, 1);
1111 case LibFunc::write:
1112 if (FTy->getNumParams() != 3 || !FTy->getParamType(1)->isPointerTy())
1114 // May throw; "write" is a valid pthread cancellation point.
1115 setDoesNotCapture(F, 2);
1116 setOnlyReadsMemory(F, 2);
1118 case LibFunc::bcopy:
1119 if (FTy->getNumParams() != 3 ||
1120 !FTy->getParamType(0)->isPointerTy() ||
1121 !FTy->getParamType(1)->isPointerTy())
1124 setDoesNotCapture(F, 1);
1125 setDoesNotCapture(F, 2);
1126 setOnlyReadsMemory(F, 1);
1129 if (FTy->getNumParams() != 3 ||
1130 !FTy->getParamType(0)->isPointerTy() ||
1131 !FTy->getParamType(1)->isPointerTy())
1134 setOnlyReadsMemory(F);
1135 setDoesNotCapture(F, 1);
1136 setDoesNotCapture(F, 2);
1138 case LibFunc::bzero:
1139 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy())
1142 setDoesNotCapture(F, 1);
1144 case LibFunc::calloc:
1145 if (FTy->getNumParams() != 2 ||
1146 !FTy->getReturnType()->isPointerTy())
1149 setDoesNotAlias(F, 0);
1151 case LibFunc::chmod:
1152 case LibFunc::chown:
1153 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1156 setDoesNotCapture(F, 1);
1157 setOnlyReadsMemory(F, 1);
1159 case LibFunc::ctermid:
1160 case LibFunc::clearerr:
1161 case LibFunc::closedir:
1162 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1165 setDoesNotCapture(F, 1);
1170 case LibFunc::atoll:
1171 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1174 setOnlyReadsMemory(F);
1175 setDoesNotCapture(F, 1);
1177 case LibFunc::access:
1178 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy())
1181 setDoesNotCapture(F, 1);
1182 setOnlyReadsMemory(F, 1);
1184 case LibFunc::fopen:
1185 if (FTy->getNumParams() != 2 ||
1186 !FTy->getReturnType()->isPointerTy() ||
1187 !FTy->getParamType(0)->isPointerTy() ||
1188 !FTy->getParamType(1)->isPointerTy())
1191 setDoesNotAlias(F, 0);
1192 setDoesNotCapture(F, 1);
1193 setDoesNotCapture(F, 2);
1194 setOnlyReadsMemory(F, 1);
1195 setOnlyReadsMemory(F, 2);
1197 case LibFunc::fdopen:
1198 if (FTy->getNumParams() != 2 ||
1199 !FTy->getReturnType()->isPointerTy() ||
1200 !FTy->getParamType(1)->isPointerTy())
1203 setDoesNotAlias(F, 0);
1204 setDoesNotCapture(F, 2);
1205 setOnlyReadsMemory(F, 2);
1209 case LibFunc::fseek:
1210 case LibFunc::ftell:
1211 case LibFunc::fgetc:
1212 case LibFunc::fseeko:
1213 case LibFunc::ftello:
1214 case LibFunc::fileno:
1215 case LibFunc::fflush:
1216 case LibFunc::fclose:
1217 case LibFunc::fsetpos:
1218 case LibFunc::flockfile:
1219 case LibFunc::funlockfile:
1220 case LibFunc::ftrylockfile:
1221 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1224 setDoesNotCapture(F, 1);
1226 case LibFunc::ferror:
1227 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1230 setDoesNotCapture(F, 1);
1231 setOnlyReadsMemory(F);
1233 case LibFunc::fputc:
1234 case LibFunc::fstat:
1235 case LibFunc::frexp:
1236 case LibFunc::frexpf:
1237 case LibFunc::frexpl:
1238 case LibFunc::fstatvfs:
1239 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1242 setDoesNotCapture(F, 2);
1244 case LibFunc::fgets:
1245 if (FTy->getNumParams() != 3 ||
1246 !FTy->getParamType(0)->isPointerTy() ||
1247 !FTy->getParamType(2)->isPointerTy())
1250 setDoesNotCapture(F, 3);
1252 case LibFunc::fread:
1253 if (FTy->getNumParams() != 4 ||
1254 !FTy->getParamType(0)->isPointerTy() ||
1255 !FTy->getParamType(3)->isPointerTy())
1258 setDoesNotCapture(F, 1);
1259 setDoesNotCapture(F, 4);
1261 case LibFunc::fwrite:
1262 if (FTy->getNumParams() != 4 ||
1263 !FTy->getParamType(0)->isPointerTy() ||
1264 !FTy->getParamType(3)->isPointerTy())
1267 setDoesNotCapture(F, 1);
1268 setDoesNotCapture(F, 4);
1270 case LibFunc::fputs:
1271 if (FTy->getNumParams() < 2 ||
1272 !FTy->getParamType(0)->isPointerTy() ||
1273 !FTy->getParamType(1)->isPointerTy())
1276 setDoesNotCapture(F, 1);
1277 setDoesNotCapture(F, 2);
1278 setOnlyReadsMemory(F, 1);
1280 case LibFunc::fscanf:
1281 case LibFunc::fprintf:
1282 if (FTy->getNumParams() < 2 ||
1283 !FTy->getParamType(0)->isPointerTy() ||
1284 !FTy->getParamType(1)->isPointerTy())
1287 setDoesNotCapture(F, 1);
1288 setDoesNotCapture(F, 2);
1289 setOnlyReadsMemory(F, 2);
1291 case LibFunc::fgetpos:
1292 if (FTy->getNumParams() < 2 ||
1293 !FTy->getParamType(0)->isPointerTy() ||
1294 !FTy->getParamType(1)->isPointerTy())
1297 setDoesNotCapture(F, 1);
1298 setDoesNotCapture(F, 2);
1301 case LibFunc::getlogin_r:
1302 case LibFunc::getc_unlocked:
1303 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1306 setDoesNotCapture(F, 1);
1308 case LibFunc::getenv:
1309 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1312 setOnlyReadsMemory(F);
1313 setDoesNotCapture(F, 1);
1316 case LibFunc::getchar:
1319 case LibFunc::getitimer:
1320 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1323 setDoesNotCapture(F, 2);
1325 case LibFunc::getpwnam:
1326 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1329 setDoesNotCapture(F, 1);
1330 setOnlyReadsMemory(F, 1);
1332 case LibFunc::ungetc:
1333 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1336 setDoesNotCapture(F, 2);
1338 case LibFunc::uname:
1339 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1342 setDoesNotCapture(F, 1);
1344 case LibFunc::unlink:
1345 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1348 setDoesNotCapture(F, 1);
1349 setOnlyReadsMemory(F, 1);
1351 case LibFunc::unsetenv:
1352 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1355 setDoesNotCapture(F, 1);
1356 setOnlyReadsMemory(F, 1);
1358 case LibFunc::utime:
1359 case LibFunc::utimes:
1360 if (FTy->getNumParams() != 2 ||
1361 !FTy->getParamType(0)->isPointerTy() ||
1362 !FTy->getParamType(1)->isPointerTy())
1365 setDoesNotCapture(F, 1);
1366 setDoesNotCapture(F, 2);
1367 setOnlyReadsMemory(F, 1);
1368 setOnlyReadsMemory(F, 2);
1371 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1374 setDoesNotCapture(F, 2);
1377 case LibFunc::printf:
1378 case LibFunc::perror:
1379 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1382 setDoesNotCapture(F, 1);
1383 setOnlyReadsMemory(F, 1);
1385 case LibFunc::pread:
1386 if (FTy->getNumParams() != 4 || !FTy->getParamType(1)->isPointerTy())
1388 // May throw; "pread" is a valid pthread cancellation point.
1389 setDoesNotCapture(F, 2);
1391 case LibFunc::pwrite:
1392 if (FTy->getNumParams() != 4 || !FTy->getParamType(1)->isPointerTy())
1394 // May throw; "pwrite" is a valid pthread cancellation point.
1395 setDoesNotCapture(F, 2);
1396 setOnlyReadsMemory(F, 2);
1398 case LibFunc::putchar:
1401 case LibFunc::popen:
1402 if (FTy->getNumParams() != 2 ||
1403 !FTy->getReturnType()->isPointerTy() ||
1404 !FTy->getParamType(0)->isPointerTy() ||
1405 !FTy->getParamType(1)->isPointerTy())
1408 setDoesNotAlias(F, 0);
1409 setDoesNotCapture(F, 1);
1410 setDoesNotCapture(F, 2);
1411 setOnlyReadsMemory(F, 1);
1412 setOnlyReadsMemory(F, 2);
1414 case LibFunc::pclose:
1415 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1418 setDoesNotCapture(F, 1);
1420 case LibFunc::vscanf:
1421 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1424 setDoesNotCapture(F, 1);
1425 setOnlyReadsMemory(F, 1);
1427 case LibFunc::vsscanf:
1428 if (FTy->getNumParams() != 3 ||
1429 !FTy->getParamType(1)->isPointerTy() ||
1430 !FTy->getParamType(2)->isPointerTy())
1433 setDoesNotCapture(F, 1);
1434 setDoesNotCapture(F, 2);
1435 setOnlyReadsMemory(F, 1);
1436 setOnlyReadsMemory(F, 2);
1438 case LibFunc::vfscanf:
1439 if (FTy->getNumParams() != 3 ||
1440 !FTy->getParamType(1)->isPointerTy() ||
1441 !FTy->getParamType(2)->isPointerTy())
1444 setDoesNotCapture(F, 1);
1445 setDoesNotCapture(F, 2);
1446 setOnlyReadsMemory(F, 2);
1448 case LibFunc::valloc:
1449 if (!FTy->getReturnType()->isPointerTy())
1452 setDoesNotAlias(F, 0);
1454 case LibFunc::vprintf:
1455 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy())
1458 setDoesNotCapture(F, 1);
1459 setOnlyReadsMemory(F, 1);
1461 case LibFunc::vfprintf:
1462 case LibFunc::vsprintf:
1463 if (FTy->getNumParams() != 3 ||
1464 !FTy->getParamType(0)->isPointerTy() ||
1465 !FTy->getParamType(1)->isPointerTy())
1468 setDoesNotCapture(F, 1);
1469 setDoesNotCapture(F, 2);
1470 setOnlyReadsMemory(F, 2);
1472 case LibFunc::vsnprintf:
1473 if (FTy->getNumParams() != 4 ||
1474 !FTy->getParamType(0)->isPointerTy() ||
1475 !FTy->getParamType(2)->isPointerTy())
1478 setDoesNotCapture(F, 1);
1479 setDoesNotCapture(F, 3);
1480 setOnlyReadsMemory(F, 3);
1483 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy())
1485 // May throw; "open" is a valid pthread cancellation point.
1486 setDoesNotCapture(F, 1);
1487 setOnlyReadsMemory(F, 1);
1489 case LibFunc::opendir:
1490 if (FTy->getNumParams() != 1 ||
1491 !FTy->getReturnType()->isPointerTy() ||
1492 !FTy->getParamType(0)->isPointerTy())
1495 setDoesNotAlias(F, 0);
1496 setDoesNotCapture(F, 1);
1497 setOnlyReadsMemory(F, 1);
1499 case LibFunc::tmpfile:
1500 if (!FTy->getReturnType()->isPointerTy())
1503 setDoesNotAlias(F, 0);
1505 case LibFunc::times:
1506 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1509 setDoesNotCapture(F, 1);
1511 case LibFunc::htonl:
1512 case LibFunc::htons:
1513 case LibFunc::ntohl:
1514 case LibFunc::ntohs:
1516 setDoesNotAccessMemory(F);
1518 case LibFunc::lstat:
1519 if (FTy->getNumParams() != 2 ||
1520 !FTy->getParamType(0)->isPointerTy() ||
1521 !FTy->getParamType(1)->isPointerTy())
1524 setDoesNotCapture(F, 1);
1525 setDoesNotCapture(F, 2);
1526 setOnlyReadsMemory(F, 1);
1528 case LibFunc::lchown:
1529 if (FTy->getNumParams() != 3 || !FTy->getParamType(0)->isPointerTy())
1532 setDoesNotCapture(F, 1);
1533 setOnlyReadsMemory(F, 1);
1535 case LibFunc::qsort:
1536 if (FTy->getNumParams() != 4 || !FTy->getParamType(3)->isPointerTy())
1538 // May throw; places call through function pointer.
1539 setDoesNotCapture(F, 4);
1541 case LibFunc::dunder_strdup:
1542 case LibFunc::dunder_strndup:
1543 if (FTy->getNumParams() < 1 ||
1544 !FTy->getReturnType()->isPointerTy() ||
1545 !FTy->getParamType(0)->isPointerTy())
1548 setDoesNotAlias(F, 0);
1549 setDoesNotCapture(F, 1);
1550 setOnlyReadsMemory(F, 1);
1552 case LibFunc::dunder_strtok_r:
1553 if (FTy->getNumParams() != 3 ||
1554 !FTy->getParamType(1)->isPointerTy())
1557 setDoesNotCapture(F, 2);
1558 setOnlyReadsMemory(F, 2);
1560 case LibFunc::under_IO_getc:
1561 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1564 setDoesNotCapture(F, 1);
1566 case LibFunc::under_IO_putc:
1567 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1570 setDoesNotCapture(F, 2);
1572 case LibFunc::dunder_isoc99_scanf:
1573 if (FTy->getNumParams() < 1 ||
1574 !FTy->getParamType(0)->isPointerTy())
1577 setDoesNotCapture(F, 1);
1578 setOnlyReadsMemory(F, 1);
1580 case LibFunc::stat64:
1581 case LibFunc::lstat64:
1582 case LibFunc::statvfs64:
1583 if (FTy->getNumParams() < 1 ||
1584 !FTy->getParamType(0)->isPointerTy() ||
1585 !FTy->getParamType(1)->isPointerTy())
1588 setDoesNotCapture(F, 1);
1589 setDoesNotCapture(F, 2);
1590 setOnlyReadsMemory(F, 1);
1592 case LibFunc::dunder_isoc99_sscanf:
1593 if (FTy->getNumParams() < 1 ||
1594 !FTy->getParamType(0)->isPointerTy() ||
1595 !FTy->getParamType(1)->isPointerTy())
1598 setDoesNotCapture(F, 1);
1599 setDoesNotCapture(F, 2);
1600 setOnlyReadsMemory(F, 1);
1601 setOnlyReadsMemory(F, 2);
1603 case LibFunc::fopen64:
1604 if (FTy->getNumParams() != 2 ||
1605 !FTy->getReturnType()->isPointerTy() ||
1606 !FTy->getParamType(0)->isPointerTy() ||
1607 !FTy->getParamType(1)->isPointerTy())
1610 setDoesNotAlias(F, 0);
1611 setDoesNotCapture(F, 1);
1612 setDoesNotCapture(F, 2);
1613 setOnlyReadsMemory(F, 1);
1614 setOnlyReadsMemory(F, 2);
1616 case LibFunc::fseeko64:
1617 case LibFunc::ftello64:
1618 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1621 setDoesNotCapture(F, 1);
1623 case LibFunc::tmpfile64:
1624 if (!FTy->getReturnType()->isPointerTy())
1627 setDoesNotAlias(F, 0);
1629 case LibFunc::fstat64:
1630 case LibFunc::fstatvfs64:
1631 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1634 setDoesNotCapture(F, 2);
1636 case LibFunc::open64:
1637 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy())
1639 // May throw; "open" is a valid pthread cancellation point.
1640 setDoesNotCapture(F, 1);
1641 setOnlyReadsMemory(F, 1);
1643 case LibFunc::gettimeofday:
1644 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy() ||
1645 !FTy->getParamType(1)->isPointerTy())
1647 // Currently some platforms have the restrict keyword on the arguments to
1648 // gettimeofday. To be conservative, do not add noalias to gettimeofday's
1651 setDoesNotCapture(F, 1);
1652 setDoesNotCapture(F, 2);
1655 // Didn't mark any attributes.
1662 /// annotateLibraryCalls - Adds attributes to well-known standard library
1663 /// call declarations.
1664 bool FunctionAttrs::annotateLibraryCalls(const CallGraphSCC &SCC) {
1665 bool MadeChange = false;
1667 // Check each function in turn annotating well-known library function
1668 // declarations with attributes.
1669 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
1670 Function *F = (*I)->getFunction();
1672 if (F && F->isDeclaration())
1673 MadeChange |= inferPrototypeAttributes(*F);
1679 bool FunctionAttrs::runOnSCC(CallGraphSCC &SCC) {
1680 AA = &getAnalysis<AliasAnalysis>();
1681 TLI = &getAnalysis<TargetLibraryInfo>();
1683 bool Changed = annotateLibraryCalls(SCC);
1684 Changed |= AddReadAttrs(SCC);
1685 Changed |= AddArgumentAttrs(SCC);
1686 Changed |= AddNoAliasAttrs(SCC);