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();
164 if (!F || F->hasFnAttribute(Attribute::OptimizeNone))
165 // External node or node we don't want to optimize - assume it may write
166 // memory and give up.
169 AliasAnalysis::ModRefBehavior MRB = AA->getModRefBehavior(F);
170 if (MRB == AliasAnalysis::DoesNotAccessMemory)
174 // Definitions with weak linkage may be overridden at linktime with
175 // something that writes memory, so treat them like declarations.
176 if (F->isDeclaration() || F->mayBeOverridden()) {
177 if (!AliasAnalysis::onlyReadsMemory(MRB))
178 // May write memory. Just give up.
185 // Scan the function body for instructions that may read or write memory.
186 for (inst_iterator II = inst_begin(F), E = inst_end(F); II != E; ++II) {
187 Instruction *I = &*II;
189 // Some instructions can be ignored even if they read or write memory.
190 // Detect these now, skipping to the next instruction if one is found.
191 CallSite CS(cast<Value>(I));
193 // Ignore calls to functions in the same SCC.
194 if (CS.getCalledFunction() && SCCNodes.count(CS.getCalledFunction()))
196 AliasAnalysis::ModRefBehavior MRB = AA->getModRefBehavior(CS);
197 // If the call doesn't access arbitrary memory, we may be able to
198 // figure out something.
199 if (AliasAnalysis::onlyAccessesArgPointees(MRB)) {
200 // If the call does access argument pointees, check each argument.
201 if (AliasAnalysis::doesAccessArgPointees(MRB))
202 // Check whether all pointer arguments point to local memory, and
203 // ignore calls that only access local memory.
204 for (CallSite::arg_iterator CI = CS.arg_begin(), CE = CS.arg_end();
207 if (Arg->getType()->isPointerTy()) {
209 I->getAAMetadata(AAInfo);
211 AliasAnalysis::Location Loc(Arg,
212 AliasAnalysis::UnknownSize, AAInfo);
213 if (!AA->pointsToConstantMemory(Loc, /*OrLocal=*/true)) {
214 if (MRB & AliasAnalysis::Mod)
215 // Writes non-local memory. Give up.
217 if (MRB & AliasAnalysis::Ref)
218 // Ok, it reads non-local memory.
225 // The call could access any memory. If that includes writes, give up.
226 if (MRB & AliasAnalysis::Mod)
228 // If it reads, note it.
229 if (MRB & AliasAnalysis::Ref)
232 } else if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
233 // Ignore non-volatile loads from local memory. (Atomic is okay here.)
234 if (!LI->isVolatile()) {
235 AliasAnalysis::Location Loc = AA->getLocation(LI);
236 if (AA->pointsToConstantMemory(Loc, /*OrLocal=*/true))
239 } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
240 // Ignore non-volatile stores to local memory. (Atomic is okay here.)
241 if (!SI->isVolatile()) {
242 AliasAnalysis::Location Loc = AA->getLocation(SI);
243 if (AA->pointsToConstantMemory(Loc, /*OrLocal=*/true))
246 } else if (VAArgInst *VI = dyn_cast<VAArgInst>(I)) {
247 // Ignore vaargs on local memory.
248 AliasAnalysis::Location Loc = AA->getLocation(VI);
249 if (AA->pointsToConstantMemory(Loc, /*OrLocal=*/true))
253 // Any remaining instructions need to be taken seriously! Check if they
254 // read or write memory.
255 if (I->mayWriteToMemory())
256 // Writes memory. Just give up.
259 // If this instruction may read memory, remember that.
260 ReadsMemory |= I->mayReadFromMemory();
264 // Success! Functions in this SCC do not access memory, or only read memory.
265 // Give them the appropriate attribute.
266 bool MadeChange = false;
267 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
268 Function *F = (*I)->getFunction();
270 if (F->doesNotAccessMemory())
274 if (F->onlyReadsMemory() && ReadsMemory)
280 // Clear out any existing attributes.
282 B.addAttribute(Attribute::ReadOnly)
283 .addAttribute(Attribute::ReadNone);
284 F->removeAttributes(AttributeSet::FunctionIndex,
285 AttributeSet::get(F->getContext(),
286 AttributeSet::FunctionIndex, B));
288 // Add in the new attribute.
289 F->addAttribute(AttributeSet::FunctionIndex,
290 ReadsMemory ? Attribute::ReadOnly : Attribute::ReadNone);
302 // For a given pointer Argument, this retains a list of Arguments of functions
303 // in the same SCC that the pointer data flows into. We use this to build an
304 // SCC of the arguments.
305 struct ArgumentGraphNode {
306 Argument *Definition;
307 SmallVector<ArgumentGraphNode*, 4> Uses;
310 class ArgumentGraph {
311 // We store pointers to ArgumentGraphNode objects, so it's important that
312 // that they not move around upon insert.
313 typedef std::map<Argument*, ArgumentGraphNode> ArgumentMapTy;
315 ArgumentMapTy ArgumentMap;
317 // There is no root node for the argument graph, in fact:
318 // void f(int *x, int *y) { if (...) f(x, y); }
319 // is an example where the graph is disconnected. The SCCIterator requires a
320 // single entry point, so we maintain a fake ("synthetic") root node that
321 // uses every node. Because the graph is directed and nothing points into
322 // the root, it will not participate in any SCCs (except for its own).
323 ArgumentGraphNode SyntheticRoot;
326 ArgumentGraph() { SyntheticRoot.Definition = nullptr; }
328 typedef SmallVectorImpl<ArgumentGraphNode*>::iterator iterator;
330 iterator begin() { return SyntheticRoot.Uses.begin(); }
331 iterator end() { return SyntheticRoot.Uses.end(); }
332 ArgumentGraphNode *getEntryNode() { return &SyntheticRoot; }
334 ArgumentGraphNode *operator[](Argument *A) {
335 ArgumentGraphNode &Node = ArgumentMap[A];
337 SyntheticRoot.Uses.push_back(&Node);
342 // This tracker checks whether callees are in the SCC, and if so it does not
343 // consider that a capture, instead adding it to the "Uses" list and
344 // continuing with the analysis.
345 struct ArgumentUsesTracker : public CaptureTracker {
346 ArgumentUsesTracker(const SmallPtrSet<Function*, 8> &SCCNodes)
347 : Captured(false), SCCNodes(SCCNodes) {}
349 void tooManyUses() override { Captured = true; }
351 bool captured(const Use *U) override {
352 CallSite CS(U->getUser());
353 if (!CS.getInstruction()) { Captured = true; return true; }
355 Function *F = CS.getCalledFunction();
356 if (!F || !SCCNodes.count(F)) { Captured = true; return true; }
359 Function::arg_iterator AI = F->arg_begin(), AE = F->arg_end();
360 for (CallSite::arg_iterator PI = CS.arg_begin(), PE = CS.arg_end();
361 PI != PE; ++PI, ++AI) {
363 assert(F->isVarArg() && "More params than args in non-varargs call");
373 assert(Found && "Capturing call-site captured nothing?");
378 bool Captured; // True only if certainly captured (used outside our SCC).
379 SmallVector<Argument*, 4> Uses; // Uses within our SCC.
381 const SmallPtrSet<Function*, 8> &SCCNodes;
386 template<> struct GraphTraits<ArgumentGraphNode*> {
387 typedef ArgumentGraphNode NodeType;
388 typedef SmallVectorImpl<ArgumentGraphNode*>::iterator ChildIteratorType;
390 static inline NodeType *getEntryNode(NodeType *A) { return A; }
391 static inline ChildIteratorType child_begin(NodeType *N) {
392 return N->Uses.begin();
394 static inline ChildIteratorType child_end(NodeType *N) {
395 return N->Uses.end();
398 template<> struct GraphTraits<ArgumentGraph*>
399 : public GraphTraits<ArgumentGraphNode*> {
400 static NodeType *getEntryNode(ArgumentGraph *AG) {
401 return AG->getEntryNode();
403 static ChildIteratorType nodes_begin(ArgumentGraph *AG) {
406 static ChildIteratorType nodes_end(ArgumentGraph *AG) {
412 // Returns Attribute::None, Attribute::ReadOnly or Attribute::ReadNone.
413 static Attribute::AttrKind
414 determinePointerReadAttrs(Argument *A,
415 const SmallPtrSet<Argument*, 8> &SCCNodes) {
417 SmallVector<Use*, 32> Worklist;
418 SmallSet<Use*, 32> Visited;
421 // inalloca arguments are always clobbered by the call.
422 if (A->hasInAllocaAttr())
423 return Attribute::None;
426 // We don't need to track IsWritten. If A is written to, return immediately.
428 for (Use &U : A->uses()) {
430 return Attribute::None;
433 Worklist.push_back(&U);
436 while (!Worklist.empty()) {
437 Use *U = Worklist.pop_back_val();
438 Instruction *I = cast<Instruction>(U->getUser());
441 switch (I->getOpcode()) {
442 case Instruction::BitCast:
443 case Instruction::GetElementPtr:
444 case Instruction::PHI:
445 case Instruction::Select:
446 case Instruction::AddrSpaceCast:
447 // The original value is not read/written via this if the new value isn't.
448 for (Use &UU : I->uses())
449 if (Visited.insert(&UU))
450 Worklist.push_back(&UU);
453 case Instruction::Call:
454 case Instruction::Invoke: {
455 bool Captures = true;
457 if (I->getType()->isVoidTy())
460 auto AddUsersToWorklistIfCapturing = [&] {
462 for (Use &UU : I->uses())
463 if (Visited.insert(&UU))
464 Worklist.push_back(&UU);
468 if (CS.doesNotAccessMemory()) {
469 AddUsersToWorklistIfCapturing();
473 Function *F = CS.getCalledFunction();
475 if (CS.onlyReadsMemory()) {
477 AddUsersToWorklistIfCapturing();
480 return Attribute::None;
483 Function::arg_iterator AI = F->arg_begin(), AE = F->arg_end();
484 CallSite::arg_iterator B = CS.arg_begin(), E = CS.arg_end();
485 for (CallSite::arg_iterator A = B; A != E; ++A, ++AI) {
488 assert(F->isVarArg() &&
489 "More params than args in non-varargs call.");
490 return Attribute::None;
492 Captures &= !CS.doesNotCapture(A - B);
493 if (SCCNodes.count(AI))
495 if (!CS.onlyReadsMemory() && !CS.onlyReadsMemory(A - B))
496 return Attribute::None;
497 if (!CS.doesNotAccessMemory(A - B))
501 AddUsersToWorklistIfCapturing();
505 case Instruction::Load:
509 case Instruction::ICmp:
510 case Instruction::Ret:
514 return Attribute::None;
518 return IsRead ? Attribute::ReadOnly : Attribute::ReadNone;
521 /// AddArgumentAttrs - Deduce nocapture attributes for the SCC.
522 bool FunctionAttrs::AddArgumentAttrs(const CallGraphSCC &SCC) {
523 bool Changed = false;
525 SmallPtrSet<Function*, 8> SCCNodes;
527 // Fill SCCNodes with the elements of the SCC. Used for quickly
528 // looking up whether a given CallGraphNode is in this SCC.
529 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
530 Function *F = (*I)->getFunction();
531 if (F && !F->isDeclaration() && !F->mayBeOverridden() &&
532 !F->hasFnAttribute(Attribute::OptimizeNone))
539 B.addAttribute(Attribute::NoCapture);
541 // Check each function in turn, determining which pointer arguments are not
543 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
544 Function *F = (*I)->getFunction();
546 if (!F || F->hasFnAttribute(Attribute::OptimizeNone))
547 // External node or function we're trying not to optimize - only a problem
548 // for arguments that we pass to it.
551 // Definitions with weak linkage may be overridden at linktime with
552 // something that captures pointers, so treat them like declarations.
553 if (F->isDeclaration() || F->mayBeOverridden())
556 // Functions that are readonly (or readnone) and nounwind and don't return
557 // a value can't capture arguments. Don't analyze them.
558 if (F->onlyReadsMemory() && F->doesNotThrow() &&
559 F->getReturnType()->isVoidTy()) {
560 for (Function::arg_iterator A = F->arg_begin(), E = F->arg_end();
562 if (A->getType()->isPointerTy() && !A->hasNoCaptureAttr()) {
563 A->addAttr(AttributeSet::get(F->getContext(), A->getArgNo() + 1, B));
571 for (Function::arg_iterator A = F->arg_begin(), E = F->arg_end();
573 if (!A->getType()->isPointerTy()) continue;
574 bool HasNonLocalUses = false;
575 if (!A->hasNoCaptureAttr()) {
576 ArgumentUsesTracker Tracker(SCCNodes);
577 PointerMayBeCaptured(A, &Tracker);
578 if (!Tracker.Captured) {
579 if (Tracker.Uses.empty()) {
580 // If it's trivially not captured, mark it nocapture now.
581 A->addAttr(AttributeSet::get(F->getContext(), A->getArgNo()+1, B));
585 // If it's not trivially captured and not trivially not captured,
586 // then it must be calling into another function in our SCC. Save
587 // its particulars for Argument-SCC analysis later.
588 ArgumentGraphNode *Node = AG[A];
589 for (SmallVectorImpl<Argument*>::iterator UI = Tracker.Uses.begin(),
590 UE = Tracker.Uses.end(); UI != UE; ++UI) {
591 Node->Uses.push_back(AG[*UI]);
593 HasNonLocalUses = true;
597 // Otherwise, it's captured. Don't bother doing SCC analysis on it.
599 if (!HasNonLocalUses && !A->onlyReadsMemory()) {
600 // Can we determine that it's readonly/readnone without doing an SCC?
601 // Note that we don't allow any calls at all here, or else our result
602 // will be dependent on the iteration order through the functions in the
604 SmallPtrSet<Argument*, 8> Self;
606 Attribute::AttrKind R = determinePointerReadAttrs(A, Self);
607 if (R != Attribute::None) {
610 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
612 R == Attribute::ReadOnly ? ++NumReadOnlyArg : ++NumReadNoneArg;
618 // The graph we've collected is partial because we stopped scanning for
619 // argument uses once we solved the argument trivially. These partial nodes
620 // show up as ArgumentGraphNode objects with an empty Uses list, and for
621 // these nodes the final decision about whether they capture has already been
622 // made. If the definition doesn't have a 'nocapture' attribute by now, it
625 for (scc_iterator<ArgumentGraph*> I = scc_begin(&AG); !I.isAtEnd(); ++I) {
626 const std::vector<ArgumentGraphNode *> &ArgumentSCC = *I;
627 if (ArgumentSCC.size() == 1) {
628 if (!ArgumentSCC[0]->Definition) continue; // synthetic root node
630 // eg. "void f(int* x) { if (...) f(x); }"
631 if (ArgumentSCC[0]->Uses.size() == 1 &&
632 ArgumentSCC[0]->Uses[0] == ArgumentSCC[0]) {
633 Argument *A = ArgumentSCC[0]->Definition;
634 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
641 bool SCCCaptured = false;
642 for (auto I = ArgumentSCC.begin(), E = ArgumentSCC.end();
643 I != E && !SCCCaptured; ++I) {
644 ArgumentGraphNode *Node = *I;
645 if (Node->Uses.empty()) {
646 if (!Node->Definition->hasNoCaptureAttr())
650 if (SCCCaptured) continue;
652 SmallPtrSet<Argument*, 8> ArgumentSCCNodes;
653 // Fill ArgumentSCCNodes with the elements of the ArgumentSCC. Used for
654 // quickly looking up whether a given Argument is in this ArgumentSCC.
655 for (auto I = ArgumentSCC.begin(), E = ArgumentSCC.end(); I != E; ++I) {
656 ArgumentSCCNodes.insert((*I)->Definition);
659 for (auto I = ArgumentSCC.begin(), E = ArgumentSCC.end();
660 I != E && !SCCCaptured; ++I) {
661 ArgumentGraphNode *N = *I;
662 for (SmallVectorImpl<ArgumentGraphNode*>::iterator UI = N->Uses.begin(),
663 UE = N->Uses.end(); UI != UE; ++UI) {
664 Argument *A = (*UI)->Definition;
665 if (A->hasNoCaptureAttr() || ArgumentSCCNodes.count(A))
671 if (SCCCaptured) continue;
673 for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) {
674 Argument *A = ArgumentSCC[i]->Definition;
675 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
680 // We also want to compute readonly/readnone. With a small number of false
681 // negatives, we can assume that any pointer which is captured isn't going
682 // to be provably readonly or readnone, since by definition we can't
683 // analyze all uses of a captured pointer.
685 // The false negatives happen when the pointer is captured by a function
686 // that promises readonly/readnone behaviour on the pointer, then the
687 // pointer's lifetime ends before anything that writes to arbitrary memory.
688 // Also, a readonly/readnone pointer may be returned, but returning a
689 // pointer is capturing it.
691 Attribute::AttrKind ReadAttr = Attribute::ReadNone;
692 for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) {
693 Argument *A = ArgumentSCC[i]->Definition;
694 Attribute::AttrKind K = determinePointerReadAttrs(A, ArgumentSCCNodes);
695 if (K == Attribute::ReadNone)
697 if (K == Attribute::ReadOnly) {
698 ReadAttr = Attribute::ReadOnly;
705 if (ReadAttr != Attribute::None) {
707 B.addAttribute(ReadAttr);
708 for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) {
709 Argument *A = ArgumentSCC[i]->Definition;
710 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
711 ReadAttr == Attribute::ReadOnly ? ++NumReadOnlyArg : ++NumReadNoneArg;
720 /// IsFunctionMallocLike - A function is malloc-like if it returns either null
721 /// or a pointer that doesn't alias any other pointer visible to the caller.
722 bool FunctionAttrs::IsFunctionMallocLike(Function *F,
723 SmallPtrSet<Function*, 8> &SCCNodes) const {
724 SmallSetVector<Value *, 8> FlowsToReturn;
725 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I)
726 if (ReturnInst *Ret = dyn_cast<ReturnInst>(I->getTerminator()))
727 FlowsToReturn.insert(Ret->getReturnValue());
729 for (unsigned i = 0; i != FlowsToReturn.size(); ++i) {
730 Value *RetVal = FlowsToReturn[i];
732 if (Constant *C = dyn_cast<Constant>(RetVal)) {
733 if (!C->isNullValue() && !isa<UndefValue>(C))
739 if (isa<Argument>(RetVal))
742 if (Instruction *RVI = dyn_cast<Instruction>(RetVal))
743 switch (RVI->getOpcode()) {
744 // Extend the analysis by looking upwards.
745 case Instruction::BitCast:
746 case Instruction::GetElementPtr:
747 case Instruction::AddrSpaceCast:
748 FlowsToReturn.insert(RVI->getOperand(0));
750 case Instruction::Select: {
751 SelectInst *SI = cast<SelectInst>(RVI);
752 FlowsToReturn.insert(SI->getTrueValue());
753 FlowsToReturn.insert(SI->getFalseValue());
756 case Instruction::PHI: {
757 PHINode *PN = cast<PHINode>(RVI);
758 for (int i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
759 FlowsToReturn.insert(PN->getIncomingValue(i));
763 // Check whether the pointer came from an allocation.
764 case Instruction::Alloca:
766 case Instruction::Call:
767 case Instruction::Invoke: {
769 if (CS.paramHasAttr(0, Attribute::NoAlias))
771 if (CS.getCalledFunction() &&
772 SCCNodes.count(CS.getCalledFunction()))
776 return false; // Did not come from an allocation.
779 if (PointerMayBeCaptured(RetVal, false, /*StoreCaptures=*/false))
786 /// AddNoAliasAttrs - Deduce noalias attributes for the SCC.
787 bool FunctionAttrs::AddNoAliasAttrs(const CallGraphSCC &SCC) {
788 SmallPtrSet<Function*, 8> SCCNodes;
790 // Fill SCCNodes with the elements of the SCC. Used for quickly
791 // looking up whether a given CallGraphNode is in this SCC.
792 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I)
793 SCCNodes.insert((*I)->getFunction());
795 // Check each function in turn, determining which functions return noalias
797 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
798 Function *F = (*I)->getFunction();
800 if (!F || F->hasFnAttribute(Attribute::OptimizeNone))
801 // External node or node we don't want to optimize - skip it;
805 if (F->doesNotAlias(0))
808 // Definitions with weak linkage may be overridden at linktime, so
809 // treat them like declarations.
810 if (F->isDeclaration() || F->mayBeOverridden())
813 // We annotate noalias return values, which are only applicable to
815 if (!F->getReturnType()->isPointerTy())
818 if (!IsFunctionMallocLike(F, SCCNodes))
822 bool MadeChange = false;
823 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
824 Function *F = (*I)->getFunction();
825 if (F->doesNotAlias(0) || !F->getReturnType()->isPointerTy())
828 F->setDoesNotAlias(0);
836 /// inferPrototypeAttributes - Analyze the name and prototype of the
837 /// given function and set any applicable attributes. Returns true
838 /// if any attributes were set and false otherwise.
839 bool FunctionAttrs::inferPrototypeAttributes(Function &F) {
840 if (F.hasFnAttribute(Attribute::OptimizeNone))
843 FunctionType *FTy = F.getFunctionType();
844 LibFunc::Func TheLibFunc;
845 if (!(TLI->getLibFunc(F.getName(), TheLibFunc) && TLI->has(TheLibFunc)))
848 switch (TheLibFunc) {
849 case LibFunc::strlen:
850 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
852 setOnlyReadsMemory(F);
854 setDoesNotCapture(F, 1);
856 case LibFunc::strchr:
857 case LibFunc::strrchr:
858 if (FTy->getNumParams() != 2 ||
859 !FTy->getParamType(0)->isPointerTy() ||
860 !FTy->getParamType(1)->isIntegerTy())
862 setOnlyReadsMemory(F);
865 case LibFunc::strtol:
866 case LibFunc::strtod:
867 case LibFunc::strtof:
868 case LibFunc::strtoul:
869 case LibFunc::strtoll:
870 case LibFunc::strtold:
871 case LibFunc::strtoull:
872 if (FTy->getNumParams() < 2 ||
873 !FTy->getParamType(1)->isPointerTy())
876 setDoesNotCapture(F, 2);
877 setOnlyReadsMemory(F, 1);
879 case LibFunc::strcpy:
880 case LibFunc::stpcpy:
881 case LibFunc::strcat:
882 case LibFunc::strncat:
883 case LibFunc::strncpy:
884 case LibFunc::stpncpy:
885 if (FTy->getNumParams() < 2 ||
886 !FTy->getParamType(1)->isPointerTy())
889 setDoesNotCapture(F, 2);
890 setOnlyReadsMemory(F, 2);
892 case LibFunc::strxfrm:
893 if (FTy->getNumParams() != 3 ||
894 !FTy->getParamType(0)->isPointerTy() ||
895 !FTy->getParamType(1)->isPointerTy())
898 setDoesNotCapture(F, 1);
899 setDoesNotCapture(F, 2);
900 setOnlyReadsMemory(F, 2);
902 case LibFunc::strcmp: //0,1
903 case LibFunc::strspn: // 0,1
904 case LibFunc::strncmp: // 0,1
905 case LibFunc::strcspn: //0,1
906 case LibFunc::strcoll: //0,1
907 case LibFunc::strcasecmp: // 0,1
908 case LibFunc::strncasecmp: //
909 if (FTy->getNumParams() < 2 ||
910 !FTy->getParamType(0)->isPointerTy() ||
911 !FTy->getParamType(1)->isPointerTy())
913 setOnlyReadsMemory(F);
915 setDoesNotCapture(F, 1);
916 setDoesNotCapture(F, 2);
918 case LibFunc::strstr:
919 case LibFunc::strpbrk:
920 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
922 setOnlyReadsMemory(F);
924 setDoesNotCapture(F, 2);
926 case LibFunc::strtok:
927 case LibFunc::strtok_r:
928 if (FTy->getNumParams() < 2 || !FTy->getParamType(1)->isPointerTy())
931 setDoesNotCapture(F, 2);
932 setOnlyReadsMemory(F, 2);
935 if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy())
938 setDoesNotCapture(F, 1);
939 setOnlyReadsMemory(F, 1);
941 case LibFunc::setbuf:
942 case LibFunc::setvbuf:
943 if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy())
946 setDoesNotCapture(F, 1);
948 case LibFunc::strdup:
949 case LibFunc::strndup:
950 if (FTy->getNumParams() < 1 || !FTy->getReturnType()->isPointerTy() ||
951 !FTy->getParamType(0)->isPointerTy())
954 setDoesNotAlias(F, 0);
955 setDoesNotCapture(F, 1);
956 setOnlyReadsMemory(F, 1);
959 case LibFunc::statvfs:
960 if (FTy->getNumParams() < 2 ||
961 !FTy->getParamType(0)->isPointerTy() ||
962 !FTy->getParamType(1)->isPointerTy())
965 setDoesNotCapture(F, 1);
966 setDoesNotCapture(F, 2);
967 setOnlyReadsMemory(F, 1);
969 case LibFunc::sscanf:
970 if (FTy->getNumParams() < 2 ||
971 !FTy->getParamType(0)->isPointerTy() ||
972 !FTy->getParamType(1)->isPointerTy())
975 setDoesNotCapture(F, 1);
976 setDoesNotCapture(F, 2);
977 setOnlyReadsMemory(F, 1);
978 setOnlyReadsMemory(F, 2);
980 case LibFunc::sprintf:
981 if (FTy->getNumParams() < 2 ||
982 !FTy->getParamType(0)->isPointerTy() ||
983 !FTy->getParamType(1)->isPointerTy())
986 setDoesNotCapture(F, 1);
987 setDoesNotCapture(F, 2);
988 setOnlyReadsMemory(F, 2);
990 case LibFunc::snprintf:
991 if (FTy->getNumParams() != 3 ||
992 !FTy->getParamType(0)->isPointerTy() ||
993 !FTy->getParamType(2)->isPointerTy())
996 setDoesNotCapture(F, 1);
997 setDoesNotCapture(F, 3);
998 setOnlyReadsMemory(F, 3);
1000 case LibFunc::setitimer:
1001 if (FTy->getNumParams() != 3 ||
1002 !FTy->getParamType(1)->isPointerTy() ||
1003 !FTy->getParamType(2)->isPointerTy())
1006 setDoesNotCapture(F, 2);
1007 setDoesNotCapture(F, 3);
1008 setOnlyReadsMemory(F, 2);
1010 case LibFunc::system:
1011 if (FTy->getNumParams() != 1 ||
1012 !FTy->getParamType(0)->isPointerTy())
1014 // May throw; "system" is a valid pthread cancellation point.
1015 setDoesNotCapture(F, 1);
1016 setOnlyReadsMemory(F, 1);
1018 case LibFunc::malloc:
1019 if (FTy->getNumParams() != 1 ||
1020 !FTy->getReturnType()->isPointerTy())
1023 setDoesNotAlias(F, 0);
1025 case LibFunc::memcmp:
1026 if (FTy->getNumParams() != 3 ||
1027 !FTy->getParamType(0)->isPointerTy() ||
1028 !FTy->getParamType(1)->isPointerTy())
1030 setOnlyReadsMemory(F);
1032 setDoesNotCapture(F, 1);
1033 setDoesNotCapture(F, 2);
1035 case LibFunc::memchr:
1036 case LibFunc::memrchr:
1037 if (FTy->getNumParams() != 3)
1039 setOnlyReadsMemory(F);
1043 case LibFunc::modff:
1044 case LibFunc::modfl:
1045 if (FTy->getNumParams() < 2 ||
1046 !FTy->getParamType(1)->isPointerTy())
1049 setDoesNotCapture(F, 2);
1051 case LibFunc::memcpy:
1052 case LibFunc::memccpy:
1053 case LibFunc::memmove:
1054 if (FTy->getNumParams() < 2 ||
1055 !FTy->getParamType(1)->isPointerTy())
1058 setDoesNotCapture(F, 2);
1059 setOnlyReadsMemory(F, 2);
1061 case LibFunc::memalign:
1062 if (!FTy->getReturnType()->isPointerTy())
1064 setDoesNotAlias(F, 0);
1066 case LibFunc::mkdir:
1067 if (FTy->getNumParams() == 0 ||
1068 !FTy->getParamType(0)->isPointerTy())
1071 setDoesNotCapture(F, 1);
1072 setOnlyReadsMemory(F, 1);
1074 case LibFunc::mktime:
1075 if (FTy->getNumParams() == 0 ||
1076 !FTy->getParamType(0)->isPointerTy())
1079 setDoesNotCapture(F, 1);
1081 case LibFunc::realloc:
1082 if (FTy->getNumParams() != 2 ||
1083 !FTy->getParamType(0)->isPointerTy() ||
1084 !FTy->getReturnType()->isPointerTy())
1087 setDoesNotAlias(F, 0);
1088 setDoesNotCapture(F, 1);
1091 if (FTy->getNumParams() != 3 ||
1092 !FTy->getParamType(1)->isPointerTy())
1094 // May throw; "read" is a valid pthread cancellation point.
1095 setDoesNotCapture(F, 2);
1097 case LibFunc::rewind:
1098 if (FTy->getNumParams() < 1 ||
1099 !FTy->getParamType(0)->isPointerTy())
1102 setDoesNotCapture(F, 1);
1104 case LibFunc::rmdir:
1105 case LibFunc::remove:
1106 case LibFunc::realpath:
1107 if (FTy->getNumParams() < 1 ||
1108 !FTy->getParamType(0)->isPointerTy())
1111 setDoesNotCapture(F, 1);
1112 setOnlyReadsMemory(F, 1);
1114 case LibFunc::rename:
1115 if (FTy->getNumParams() < 2 ||
1116 !FTy->getParamType(0)->isPointerTy() ||
1117 !FTy->getParamType(1)->isPointerTy())
1120 setDoesNotCapture(F, 1);
1121 setDoesNotCapture(F, 2);
1122 setOnlyReadsMemory(F, 1);
1123 setOnlyReadsMemory(F, 2);
1125 case LibFunc::readlink:
1126 if (FTy->getNumParams() < 2 ||
1127 !FTy->getParamType(0)->isPointerTy() ||
1128 !FTy->getParamType(1)->isPointerTy())
1131 setDoesNotCapture(F, 1);
1132 setDoesNotCapture(F, 2);
1133 setOnlyReadsMemory(F, 1);
1135 case LibFunc::write:
1136 if (FTy->getNumParams() != 3 || !FTy->getParamType(1)->isPointerTy())
1138 // May throw; "write" is a valid pthread cancellation point.
1139 setDoesNotCapture(F, 2);
1140 setOnlyReadsMemory(F, 2);
1142 case LibFunc::bcopy:
1143 if (FTy->getNumParams() != 3 ||
1144 !FTy->getParamType(0)->isPointerTy() ||
1145 !FTy->getParamType(1)->isPointerTy())
1148 setDoesNotCapture(F, 1);
1149 setDoesNotCapture(F, 2);
1150 setOnlyReadsMemory(F, 1);
1153 if (FTy->getNumParams() != 3 ||
1154 !FTy->getParamType(0)->isPointerTy() ||
1155 !FTy->getParamType(1)->isPointerTy())
1158 setOnlyReadsMemory(F);
1159 setDoesNotCapture(F, 1);
1160 setDoesNotCapture(F, 2);
1162 case LibFunc::bzero:
1163 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy())
1166 setDoesNotCapture(F, 1);
1168 case LibFunc::calloc:
1169 if (FTy->getNumParams() != 2 ||
1170 !FTy->getReturnType()->isPointerTy())
1173 setDoesNotAlias(F, 0);
1175 case LibFunc::chmod:
1176 case LibFunc::chown:
1177 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1180 setDoesNotCapture(F, 1);
1181 setOnlyReadsMemory(F, 1);
1183 case LibFunc::ctermid:
1184 case LibFunc::clearerr:
1185 case LibFunc::closedir:
1186 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1189 setDoesNotCapture(F, 1);
1194 case LibFunc::atoll:
1195 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1198 setOnlyReadsMemory(F);
1199 setDoesNotCapture(F, 1);
1201 case LibFunc::access:
1202 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy())
1205 setDoesNotCapture(F, 1);
1206 setOnlyReadsMemory(F, 1);
1208 case LibFunc::fopen:
1209 if (FTy->getNumParams() != 2 ||
1210 !FTy->getReturnType()->isPointerTy() ||
1211 !FTy->getParamType(0)->isPointerTy() ||
1212 !FTy->getParamType(1)->isPointerTy())
1215 setDoesNotAlias(F, 0);
1216 setDoesNotCapture(F, 1);
1217 setDoesNotCapture(F, 2);
1218 setOnlyReadsMemory(F, 1);
1219 setOnlyReadsMemory(F, 2);
1221 case LibFunc::fdopen:
1222 if (FTy->getNumParams() != 2 ||
1223 !FTy->getReturnType()->isPointerTy() ||
1224 !FTy->getParamType(1)->isPointerTy())
1227 setDoesNotAlias(F, 0);
1228 setDoesNotCapture(F, 2);
1229 setOnlyReadsMemory(F, 2);
1233 case LibFunc::fseek:
1234 case LibFunc::ftell:
1235 case LibFunc::fgetc:
1236 case LibFunc::fseeko:
1237 case LibFunc::ftello:
1238 case LibFunc::fileno:
1239 case LibFunc::fflush:
1240 case LibFunc::fclose:
1241 case LibFunc::fsetpos:
1242 case LibFunc::flockfile:
1243 case LibFunc::funlockfile:
1244 case LibFunc::ftrylockfile:
1245 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1248 setDoesNotCapture(F, 1);
1250 case LibFunc::ferror:
1251 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1254 setDoesNotCapture(F, 1);
1255 setOnlyReadsMemory(F);
1257 case LibFunc::fputc:
1258 case LibFunc::fstat:
1259 case LibFunc::frexp:
1260 case LibFunc::frexpf:
1261 case LibFunc::frexpl:
1262 case LibFunc::fstatvfs:
1263 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1266 setDoesNotCapture(F, 2);
1268 case LibFunc::fgets:
1269 if (FTy->getNumParams() != 3 ||
1270 !FTy->getParamType(0)->isPointerTy() ||
1271 !FTy->getParamType(2)->isPointerTy())
1274 setDoesNotCapture(F, 3);
1276 case LibFunc::fread:
1277 if (FTy->getNumParams() != 4 ||
1278 !FTy->getParamType(0)->isPointerTy() ||
1279 !FTy->getParamType(3)->isPointerTy())
1282 setDoesNotCapture(F, 1);
1283 setDoesNotCapture(F, 4);
1285 case LibFunc::fwrite:
1286 if (FTy->getNumParams() != 4 ||
1287 !FTy->getParamType(0)->isPointerTy() ||
1288 !FTy->getParamType(3)->isPointerTy())
1291 setDoesNotCapture(F, 1);
1292 setDoesNotCapture(F, 4);
1294 case LibFunc::fputs:
1295 if (FTy->getNumParams() < 2 ||
1296 !FTy->getParamType(0)->isPointerTy() ||
1297 !FTy->getParamType(1)->isPointerTy())
1300 setDoesNotCapture(F, 1);
1301 setDoesNotCapture(F, 2);
1302 setOnlyReadsMemory(F, 1);
1304 case LibFunc::fscanf:
1305 case LibFunc::fprintf:
1306 if (FTy->getNumParams() < 2 ||
1307 !FTy->getParamType(0)->isPointerTy() ||
1308 !FTy->getParamType(1)->isPointerTy())
1311 setDoesNotCapture(F, 1);
1312 setDoesNotCapture(F, 2);
1313 setOnlyReadsMemory(F, 2);
1315 case LibFunc::fgetpos:
1316 if (FTy->getNumParams() < 2 ||
1317 !FTy->getParamType(0)->isPointerTy() ||
1318 !FTy->getParamType(1)->isPointerTy())
1321 setDoesNotCapture(F, 1);
1322 setDoesNotCapture(F, 2);
1325 case LibFunc::getlogin_r:
1326 case LibFunc::getc_unlocked:
1327 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1330 setDoesNotCapture(F, 1);
1332 case LibFunc::getenv:
1333 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1336 setOnlyReadsMemory(F);
1337 setDoesNotCapture(F, 1);
1340 case LibFunc::getchar:
1343 case LibFunc::getitimer:
1344 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1347 setDoesNotCapture(F, 2);
1349 case LibFunc::getpwnam:
1350 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1353 setDoesNotCapture(F, 1);
1354 setOnlyReadsMemory(F, 1);
1356 case LibFunc::ungetc:
1357 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1360 setDoesNotCapture(F, 2);
1362 case LibFunc::uname:
1363 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1366 setDoesNotCapture(F, 1);
1368 case LibFunc::unlink:
1369 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1372 setDoesNotCapture(F, 1);
1373 setOnlyReadsMemory(F, 1);
1375 case LibFunc::unsetenv:
1376 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1379 setDoesNotCapture(F, 1);
1380 setOnlyReadsMemory(F, 1);
1382 case LibFunc::utime:
1383 case LibFunc::utimes:
1384 if (FTy->getNumParams() != 2 ||
1385 !FTy->getParamType(0)->isPointerTy() ||
1386 !FTy->getParamType(1)->isPointerTy())
1389 setDoesNotCapture(F, 1);
1390 setDoesNotCapture(F, 2);
1391 setOnlyReadsMemory(F, 1);
1392 setOnlyReadsMemory(F, 2);
1395 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1398 setDoesNotCapture(F, 2);
1401 case LibFunc::printf:
1402 case LibFunc::perror:
1403 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1406 setDoesNotCapture(F, 1);
1407 setOnlyReadsMemory(F, 1);
1409 case LibFunc::pread:
1410 if (FTy->getNumParams() != 4 || !FTy->getParamType(1)->isPointerTy())
1412 // May throw; "pread" is a valid pthread cancellation point.
1413 setDoesNotCapture(F, 2);
1415 case LibFunc::pwrite:
1416 if (FTy->getNumParams() != 4 || !FTy->getParamType(1)->isPointerTy())
1418 // May throw; "pwrite" is a valid pthread cancellation point.
1419 setDoesNotCapture(F, 2);
1420 setOnlyReadsMemory(F, 2);
1422 case LibFunc::putchar:
1425 case LibFunc::popen:
1426 if (FTy->getNumParams() != 2 ||
1427 !FTy->getReturnType()->isPointerTy() ||
1428 !FTy->getParamType(0)->isPointerTy() ||
1429 !FTy->getParamType(1)->isPointerTy())
1432 setDoesNotAlias(F, 0);
1433 setDoesNotCapture(F, 1);
1434 setDoesNotCapture(F, 2);
1435 setOnlyReadsMemory(F, 1);
1436 setOnlyReadsMemory(F, 2);
1438 case LibFunc::pclose:
1439 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1442 setDoesNotCapture(F, 1);
1444 case LibFunc::vscanf:
1445 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1448 setDoesNotCapture(F, 1);
1449 setOnlyReadsMemory(F, 1);
1451 case LibFunc::vsscanf:
1452 if (FTy->getNumParams() != 3 ||
1453 !FTy->getParamType(1)->isPointerTy() ||
1454 !FTy->getParamType(2)->isPointerTy())
1457 setDoesNotCapture(F, 1);
1458 setDoesNotCapture(F, 2);
1459 setOnlyReadsMemory(F, 1);
1460 setOnlyReadsMemory(F, 2);
1462 case LibFunc::vfscanf:
1463 if (FTy->getNumParams() != 3 ||
1464 !FTy->getParamType(1)->isPointerTy() ||
1465 !FTy->getParamType(2)->isPointerTy())
1468 setDoesNotCapture(F, 1);
1469 setDoesNotCapture(F, 2);
1470 setOnlyReadsMemory(F, 2);
1472 case LibFunc::valloc:
1473 if (!FTy->getReturnType()->isPointerTy())
1476 setDoesNotAlias(F, 0);
1478 case LibFunc::vprintf:
1479 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy())
1482 setDoesNotCapture(F, 1);
1483 setOnlyReadsMemory(F, 1);
1485 case LibFunc::vfprintf:
1486 case LibFunc::vsprintf:
1487 if (FTy->getNumParams() != 3 ||
1488 !FTy->getParamType(0)->isPointerTy() ||
1489 !FTy->getParamType(1)->isPointerTy())
1492 setDoesNotCapture(F, 1);
1493 setDoesNotCapture(F, 2);
1494 setOnlyReadsMemory(F, 2);
1496 case LibFunc::vsnprintf:
1497 if (FTy->getNumParams() != 4 ||
1498 !FTy->getParamType(0)->isPointerTy() ||
1499 !FTy->getParamType(2)->isPointerTy())
1502 setDoesNotCapture(F, 1);
1503 setDoesNotCapture(F, 3);
1504 setOnlyReadsMemory(F, 3);
1507 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy())
1509 // May throw; "open" is a valid pthread cancellation point.
1510 setDoesNotCapture(F, 1);
1511 setOnlyReadsMemory(F, 1);
1513 case LibFunc::opendir:
1514 if (FTy->getNumParams() != 1 ||
1515 !FTy->getReturnType()->isPointerTy() ||
1516 !FTy->getParamType(0)->isPointerTy())
1519 setDoesNotAlias(F, 0);
1520 setDoesNotCapture(F, 1);
1521 setOnlyReadsMemory(F, 1);
1523 case LibFunc::tmpfile:
1524 if (!FTy->getReturnType()->isPointerTy())
1527 setDoesNotAlias(F, 0);
1529 case LibFunc::times:
1530 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1533 setDoesNotCapture(F, 1);
1535 case LibFunc::htonl:
1536 case LibFunc::htons:
1537 case LibFunc::ntohl:
1538 case LibFunc::ntohs:
1540 setDoesNotAccessMemory(F);
1542 case LibFunc::lstat:
1543 if (FTy->getNumParams() != 2 ||
1544 !FTy->getParamType(0)->isPointerTy() ||
1545 !FTy->getParamType(1)->isPointerTy())
1548 setDoesNotCapture(F, 1);
1549 setDoesNotCapture(F, 2);
1550 setOnlyReadsMemory(F, 1);
1552 case LibFunc::lchown:
1553 if (FTy->getNumParams() != 3 || !FTy->getParamType(0)->isPointerTy())
1556 setDoesNotCapture(F, 1);
1557 setOnlyReadsMemory(F, 1);
1559 case LibFunc::qsort:
1560 if (FTy->getNumParams() != 4 || !FTy->getParamType(3)->isPointerTy())
1562 // May throw; places call through function pointer.
1563 setDoesNotCapture(F, 4);
1565 case LibFunc::dunder_strdup:
1566 case LibFunc::dunder_strndup:
1567 if (FTy->getNumParams() < 1 ||
1568 !FTy->getReturnType()->isPointerTy() ||
1569 !FTy->getParamType(0)->isPointerTy())
1572 setDoesNotAlias(F, 0);
1573 setDoesNotCapture(F, 1);
1574 setOnlyReadsMemory(F, 1);
1576 case LibFunc::dunder_strtok_r:
1577 if (FTy->getNumParams() != 3 ||
1578 !FTy->getParamType(1)->isPointerTy())
1581 setDoesNotCapture(F, 2);
1582 setOnlyReadsMemory(F, 2);
1584 case LibFunc::under_IO_getc:
1585 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1588 setDoesNotCapture(F, 1);
1590 case LibFunc::under_IO_putc:
1591 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1594 setDoesNotCapture(F, 2);
1596 case LibFunc::dunder_isoc99_scanf:
1597 if (FTy->getNumParams() < 1 ||
1598 !FTy->getParamType(0)->isPointerTy())
1601 setDoesNotCapture(F, 1);
1602 setOnlyReadsMemory(F, 1);
1604 case LibFunc::stat64:
1605 case LibFunc::lstat64:
1606 case LibFunc::statvfs64:
1607 if (FTy->getNumParams() < 1 ||
1608 !FTy->getParamType(0)->isPointerTy() ||
1609 !FTy->getParamType(1)->isPointerTy())
1612 setDoesNotCapture(F, 1);
1613 setDoesNotCapture(F, 2);
1614 setOnlyReadsMemory(F, 1);
1616 case LibFunc::dunder_isoc99_sscanf:
1617 if (FTy->getNumParams() < 1 ||
1618 !FTy->getParamType(0)->isPointerTy() ||
1619 !FTy->getParamType(1)->isPointerTy())
1622 setDoesNotCapture(F, 1);
1623 setDoesNotCapture(F, 2);
1624 setOnlyReadsMemory(F, 1);
1625 setOnlyReadsMemory(F, 2);
1627 case LibFunc::fopen64:
1628 if (FTy->getNumParams() != 2 ||
1629 !FTy->getReturnType()->isPointerTy() ||
1630 !FTy->getParamType(0)->isPointerTy() ||
1631 !FTy->getParamType(1)->isPointerTy())
1634 setDoesNotAlias(F, 0);
1635 setDoesNotCapture(F, 1);
1636 setDoesNotCapture(F, 2);
1637 setOnlyReadsMemory(F, 1);
1638 setOnlyReadsMemory(F, 2);
1640 case LibFunc::fseeko64:
1641 case LibFunc::ftello64:
1642 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1645 setDoesNotCapture(F, 1);
1647 case LibFunc::tmpfile64:
1648 if (!FTy->getReturnType()->isPointerTy())
1651 setDoesNotAlias(F, 0);
1653 case LibFunc::fstat64:
1654 case LibFunc::fstatvfs64:
1655 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1658 setDoesNotCapture(F, 2);
1660 case LibFunc::open64:
1661 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy())
1663 // May throw; "open" is a valid pthread cancellation point.
1664 setDoesNotCapture(F, 1);
1665 setOnlyReadsMemory(F, 1);
1667 case LibFunc::gettimeofday:
1668 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy() ||
1669 !FTy->getParamType(1)->isPointerTy())
1671 // Currently some platforms have the restrict keyword on the arguments to
1672 // gettimeofday. To be conservative, do not add noalias to gettimeofday's
1675 setDoesNotCapture(F, 1);
1676 setDoesNotCapture(F, 2);
1679 // Didn't mark any attributes.
1686 /// annotateLibraryCalls - Adds attributes to well-known standard library
1687 /// call declarations.
1688 bool FunctionAttrs::annotateLibraryCalls(const CallGraphSCC &SCC) {
1689 bool MadeChange = false;
1691 // Check each function in turn annotating well-known library function
1692 // declarations with attributes.
1693 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
1694 Function *F = (*I)->getFunction();
1696 if (F && F->isDeclaration())
1697 MadeChange |= inferPrototypeAttributes(*F);
1703 bool FunctionAttrs::runOnSCC(CallGraphSCC &SCC) {
1704 AA = &getAnalysis<AliasAnalysis>();
1705 TLI = &getAnalysis<TargetLibraryInfo>();
1707 bool Changed = annotateLibraryCalls(SCC);
1708 Changed |= AddReadAttrs(SCC);
1709 Changed |= AddArgumentAttrs(SCC);
1710 Changed |= AddNoAliasAttrs(SCC);