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/Analysis/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<TargetLibraryInfoWrapperPass>();
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(TargetLibraryInfoWrapperPass)
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 FunctionModRefBehavior MRB = AA->getModRefBehavior(F);
170 if (MRB == FMRB_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 FunctionModRefBehavior 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 MemoryLocation Loc(Arg, MemoryLocation::UnknownSize, AAInfo);
212 if (!AA->pointsToConstantMemory(Loc, /*OrLocal=*/true)) {
214 // Writes non-local memory. Give up.
217 // Ok, it reads non-local memory.
224 // The call could access any memory. If that includes writes, give up.
227 // If it reads, note it.
231 } else if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
232 // Ignore non-volatile loads from local memory. (Atomic is okay here.)
233 if (!LI->isVolatile()) {
234 MemoryLocation Loc = MemoryLocation::get(LI);
235 if (AA->pointsToConstantMemory(Loc, /*OrLocal=*/true))
238 } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
239 // Ignore non-volatile stores to local memory. (Atomic is okay here.)
240 if (!SI->isVolatile()) {
241 MemoryLocation Loc = MemoryLocation::get(SI);
242 if (AA->pointsToConstantMemory(Loc, /*OrLocal=*/true))
245 } else if (VAArgInst *VI = dyn_cast<VAArgInst>(I)) {
246 // Ignore vaargs on local memory.
247 MemoryLocation Loc = MemoryLocation::get(VI);
248 if (AA->pointsToConstantMemory(Loc, /*OrLocal=*/true))
252 // Any remaining instructions need to be taken seriously! Check if they
253 // read or write memory.
254 if (I->mayWriteToMemory())
255 // Writes memory. Just give up.
258 // If this instruction may read memory, remember that.
259 ReadsMemory |= I->mayReadFromMemory();
263 // Success! Functions in this SCC do not access memory, or only read memory.
264 // Give them the appropriate attribute.
265 bool MadeChange = false;
266 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
267 Function *F = (*I)->getFunction();
269 if (F->doesNotAccessMemory())
273 if (F->onlyReadsMemory() && ReadsMemory)
279 // Clear out any existing attributes.
281 B.addAttribute(Attribute::ReadOnly)
282 .addAttribute(Attribute::ReadNone);
283 F->removeAttributes(AttributeSet::FunctionIndex,
284 AttributeSet::get(F->getContext(),
285 AttributeSet::FunctionIndex, B));
287 // Add in the new attribute.
288 F->addAttribute(AttributeSet::FunctionIndex,
289 ReadsMemory ? Attribute::ReadOnly : Attribute::ReadNone);
301 // For a given pointer Argument, this retains a list of Arguments of functions
302 // in the same SCC that the pointer data flows into. We use this to build an
303 // SCC of the arguments.
304 struct ArgumentGraphNode {
305 Argument *Definition;
306 SmallVector<ArgumentGraphNode*, 4> Uses;
309 class ArgumentGraph {
310 // We store pointers to ArgumentGraphNode objects, so it's important that
311 // that they not move around upon insert.
312 typedef std::map<Argument*, ArgumentGraphNode> ArgumentMapTy;
314 ArgumentMapTy ArgumentMap;
316 // There is no root node for the argument graph, in fact:
317 // void f(int *x, int *y) { if (...) f(x, y); }
318 // is an example where the graph is disconnected. The SCCIterator requires a
319 // single entry point, so we maintain a fake ("synthetic") root node that
320 // uses every node. Because the graph is directed and nothing points into
321 // the root, it will not participate in any SCCs (except for its own).
322 ArgumentGraphNode SyntheticRoot;
325 ArgumentGraph() { SyntheticRoot.Definition = nullptr; }
327 typedef SmallVectorImpl<ArgumentGraphNode*>::iterator iterator;
329 iterator begin() { return SyntheticRoot.Uses.begin(); }
330 iterator end() { return SyntheticRoot.Uses.end(); }
331 ArgumentGraphNode *getEntryNode() { return &SyntheticRoot; }
333 ArgumentGraphNode *operator[](Argument *A) {
334 ArgumentGraphNode &Node = ArgumentMap[A];
336 SyntheticRoot.Uses.push_back(&Node);
341 // This tracker checks whether callees are in the SCC, and if so it does not
342 // consider that a capture, instead adding it to the "Uses" list and
343 // continuing with the analysis.
344 struct ArgumentUsesTracker : public CaptureTracker {
345 ArgumentUsesTracker(const SmallPtrSet<Function*, 8> &SCCNodes)
346 : Captured(false), SCCNodes(SCCNodes) {}
348 void tooManyUses() override { Captured = true; }
350 bool captured(const Use *U) override {
351 CallSite CS(U->getUser());
352 if (!CS.getInstruction()) { Captured = true; return true; }
354 Function *F = CS.getCalledFunction();
355 if (!F || !SCCNodes.count(F)) { Captured = true; return true; }
358 Function::arg_iterator AI = F->arg_begin(), AE = F->arg_end();
359 for (CallSite::arg_iterator PI = CS.arg_begin(), PE = CS.arg_end();
360 PI != PE; ++PI, ++AI) {
362 assert(F->isVarArg() && "More params than args in non-varargs call");
372 assert(Found && "Capturing call-site captured nothing?");
377 bool Captured; // True only if certainly captured (used outside our SCC).
378 SmallVector<Argument*, 4> Uses; // Uses within our SCC.
380 const SmallPtrSet<Function*, 8> &SCCNodes;
385 template<> struct GraphTraits<ArgumentGraphNode*> {
386 typedef ArgumentGraphNode NodeType;
387 typedef SmallVectorImpl<ArgumentGraphNode*>::iterator ChildIteratorType;
389 static inline NodeType *getEntryNode(NodeType *A) { return A; }
390 static inline ChildIteratorType child_begin(NodeType *N) {
391 return N->Uses.begin();
393 static inline ChildIteratorType child_end(NodeType *N) {
394 return N->Uses.end();
397 template<> struct GraphTraits<ArgumentGraph*>
398 : public GraphTraits<ArgumentGraphNode*> {
399 static NodeType *getEntryNode(ArgumentGraph *AG) {
400 return AG->getEntryNode();
402 static ChildIteratorType nodes_begin(ArgumentGraph *AG) {
405 static ChildIteratorType nodes_end(ArgumentGraph *AG) {
411 // Returns Attribute::None, Attribute::ReadOnly or Attribute::ReadNone.
412 static Attribute::AttrKind
413 determinePointerReadAttrs(Argument *A,
414 const SmallPtrSet<Argument*, 8> &SCCNodes) {
416 SmallVector<Use*, 32> Worklist;
417 SmallSet<Use*, 32> Visited;
420 // inalloca arguments are always clobbered by the call.
421 if (A->hasInAllocaAttr())
422 return Attribute::None;
425 // We don't need to track IsWritten. If A is written to, return immediately.
427 for (Use &U : A->uses()) {
429 return Attribute::None;
432 Worklist.push_back(&U);
435 while (!Worklist.empty()) {
436 Use *U = Worklist.pop_back_val();
437 Instruction *I = cast<Instruction>(U->getUser());
440 switch (I->getOpcode()) {
441 case Instruction::BitCast:
442 case Instruction::GetElementPtr:
443 case Instruction::PHI:
444 case Instruction::Select:
445 case Instruction::AddrSpaceCast:
446 // The original value is not read/written via this if the new value isn't.
447 for (Use &UU : I->uses())
448 if (Visited.insert(&UU).second)
449 Worklist.push_back(&UU);
452 case Instruction::Call:
453 case Instruction::Invoke: {
454 bool Captures = true;
456 if (I->getType()->isVoidTy())
459 auto AddUsersToWorklistIfCapturing = [&] {
461 for (Use &UU : I->uses())
462 if (Visited.insert(&UU).second)
463 Worklist.push_back(&UU);
467 if (CS.doesNotAccessMemory()) {
468 AddUsersToWorklistIfCapturing();
472 Function *F = CS.getCalledFunction();
474 if (CS.onlyReadsMemory()) {
476 AddUsersToWorklistIfCapturing();
479 return Attribute::None;
482 Function::arg_iterator AI = F->arg_begin(), AE = F->arg_end();
483 CallSite::arg_iterator B = CS.arg_begin(), E = CS.arg_end();
484 for (CallSite::arg_iterator A = B; A != E; ++A, ++AI) {
487 assert(F->isVarArg() &&
488 "More params than args in non-varargs call.");
489 return Attribute::None;
491 Captures &= !CS.doesNotCapture(A - B);
492 if (SCCNodes.count(AI))
494 if (!CS.onlyReadsMemory() && !CS.onlyReadsMemory(A - B))
495 return Attribute::None;
496 if (!CS.doesNotAccessMemory(A - B))
500 AddUsersToWorklistIfCapturing();
504 case Instruction::Load:
508 case Instruction::ICmp:
509 case Instruction::Ret:
513 return Attribute::None;
517 return IsRead ? Attribute::ReadOnly : Attribute::ReadNone;
520 /// AddArgumentAttrs - Deduce nocapture attributes for the SCC.
521 bool FunctionAttrs::AddArgumentAttrs(const CallGraphSCC &SCC) {
522 bool Changed = false;
524 SmallPtrSet<Function*, 8> SCCNodes;
526 // Fill SCCNodes with the elements of the SCC. Used for quickly
527 // looking up whether a given CallGraphNode is in this SCC.
528 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
529 Function *F = (*I)->getFunction();
530 if (F && !F->isDeclaration() && !F->mayBeOverridden() &&
531 !F->hasFnAttribute(Attribute::OptimizeNone))
538 B.addAttribute(Attribute::NoCapture);
540 // Check each function in turn, determining which pointer arguments are not
542 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
543 Function *F = (*I)->getFunction();
545 if (!F || F->hasFnAttribute(Attribute::OptimizeNone))
546 // External node or function we're trying not to optimize - only a problem
547 // for arguments that we pass to it.
550 // Definitions with weak linkage may be overridden at linktime with
551 // something that captures pointers, so treat them like declarations.
552 if (F->isDeclaration() || F->mayBeOverridden())
555 // Functions that are readonly (or readnone) and nounwind and don't return
556 // a value can't capture arguments. Don't analyze them.
557 if (F->onlyReadsMemory() && F->doesNotThrow() &&
558 F->getReturnType()->isVoidTy()) {
559 for (Function::arg_iterator A = F->arg_begin(), E = F->arg_end();
561 if (A->getType()->isPointerTy() && !A->hasNoCaptureAttr()) {
562 A->addAttr(AttributeSet::get(F->getContext(), A->getArgNo() + 1, B));
570 for (Function::arg_iterator A = F->arg_begin(), E = F->arg_end();
572 if (!A->getType()->isPointerTy()) continue;
573 bool HasNonLocalUses = false;
574 if (!A->hasNoCaptureAttr()) {
575 ArgumentUsesTracker Tracker(SCCNodes);
576 PointerMayBeCaptured(A, &Tracker);
577 if (!Tracker.Captured) {
578 if (Tracker.Uses.empty()) {
579 // If it's trivially not captured, mark it nocapture now.
580 A->addAttr(AttributeSet::get(F->getContext(), A->getArgNo()+1, B));
584 // If it's not trivially captured and not trivially not captured,
585 // then it must be calling into another function in our SCC. Save
586 // its particulars for Argument-SCC analysis later.
587 ArgumentGraphNode *Node = AG[A];
588 for (SmallVectorImpl<Argument*>::iterator UI = Tracker.Uses.begin(),
589 UE = Tracker.Uses.end(); UI != UE; ++UI) {
590 Node->Uses.push_back(AG[*UI]);
592 HasNonLocalUses = true;
596 // Otherwise, it's captured. Don't bother doing SCC analysis on it.
598 if (!HasNonLocalUses && !A->onlyReadsMemory()) {
599 // Can we determine that it's readonly/readnone without doing an SCC?
600 // Note that we don't allow any calls at all here, or else our result
601 // will be dependent on the iteration order through the functions in the
603 SmallPtrSet<Argument*, 8> Self;
605 Attribute::AttrKind R = determinePointerReadAttrs(A, Self);
606 if (R != Attribute::None) {
609 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
611 R == Attribute::ReadOnly ? ++NumReadOnlyArg : ++NumReadNoneArg;
617 // The graph we've collected is partial because we stopped scanning for
618 // argument uses once we solved the argument trivially. These partial nodes
619 // show up as ArgumentGraphNode objects with an empty Uses list, and for
620 // these nodes the final decision about whether they capture has already been
621 // made. If the definition doesn't have a 'nocapture' attribute by now, it
624 for (scc_iterator<ArgumentGraph*> I = scc_begin(&AG); !I.isAtEnd(); ++I) {
625 const std::vector<ArgumentGraphNode *> &ArgumentSCC = *I;
626 if (ArgumentSCC.size() == 1) {
627 if (!ArgumentSCC[0]->Definition) continue; // synthetic root node
629 // eg. "void f(int* x) { if (...) f(x); }"
630 if (ArgumentSCC[0]->Uses.size() == 1 &&
631 ArgumentSCC[0]->Uses[0] == ArgumentSCC[0]) {
632 Argument *A = ArgumentSCC[0]->Definition;
633 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
640 bool SCCCaptured = false;
641 for (auto I = ArgumentSCC.begin(), E = ArgumentSCC.end();
642 I != E && !SCCCaptured; ++I) {
643 ArgumentGraphNode *Node = *I;
644 if (Node->Uses.empty()) {
645 if (!Node->Definition->hasNoCaptureAttr())
649 if (SCCCaptured) continue;
651 SmallPtrSet<Argument*, 8> ArgumentSCCNodes;
652 // Fill ArgumentSCCNodes with the elements of the ArgumentSCC. Used for
653 // quickly looking up whether a given Argument is in this ArgumentSCC.
654 for (auto I = ArgumentSCC.begin(), E = ArgumentSCC.end(); I != E; ++I) {
655 ArgumentSCCNodes.insert((*I)->Definition);
658 for (auto I = ArgumentSCC.begin(), E = ArgumentSCC.end();
659 I != E && !SCCCaptured; ++I) {
660 ArgumentGraphNode *N = *I;
661 for (SmallVectorImpl<ArgumentGraphNode*>::iterator UI = N->Uses.begin(),
662 UE = N->Uses.end(); UI != UE; ++UI) {
663 Argument *A = (*UI)->Definition;
664 if (A->hasNoCaptureAttr() || ArgumentSCCNodes.count(A))
670 if (SCCCaptured) continue;
672 for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) {
673 Argument *A = ArgumentSCC[i]->Definition;
674 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
679 // We also want to compute readonly/readnone. With a small number of false
680 // negatives, we can assume that any pointer which is captured isn't going
681 // to be provably readonly or readnone, since by definition we can't
682 // analyze all uses of a captured pointer.
684 // The false negatives happen when the pointer is captured by a function
685 // that promises readonly/readnone behaviour on the pointer, then the
686 // pointer's lifetime ends before anything that writes to arbitrary memory.
687 // Also, a readonly/readnone pointer may be returned, but returning a
688 // pointer is capturing it.
690 Attribute::AttrKind ReadAttr = Attribute::ReadNone;
691 for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) {
692 Argument *A = ArgumentSCC[i]->Definition;
693 Attribute::AttrKind K = determinePointerReadAttrs(A, ArgumentSCCNodes);
694 if (K == Attribute::ReadNone)
696 if (K == Attribute::ReadOnly) {
697 ReadAttr = Attribute::ReadOnly;
704 if (ReadAttr != Attribute::None) {
706 B.addAttribute(ReadAttr);
707 R.addAttribute(Attribute::ReadOnly)
708 .addAttribute(Attribute::ReadNone);
709 for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) {
710 Argument *A = ArgumentSCC[i]->Definition;
711 // Clear out existing readonly/readnone attributes
712 A->removeAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, R));
713 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
714 ReadAttr == Attribute::ReadOnly ? ++NumReadOnlyArg : ++NumReadNoneArg;
723 /// IsFunctionMallocLike - A function is malloc-like if it returns either null
724 /// or a pointer that doesn't alias any other pointer visible to the caller.
725 bool FunctionAttrs::IsFunctionMallocLike(Function *F,
726 SmallPtrSet<Function*, 8> &SCCNodes) const {
727 SmallSetVector<Value *, 8> FlowsToReturn;
728 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I)
729 if (ReturnInst *Ret = dyn_cast<ReturnInst>(I->getTerminator()))
730 FlowsToReturn.insert(Ret->getReturnValue());
732 for (unsigned i = 0; i != FlowsToReturn.size(); ++i) {
733 Value *RetVal = FlowsToReturn[i];
735 if (Constant *C = dyn_cast<Constant>(RetVal)) {
736 if (!C->isNullValue() && !isa<UndefValue>(C))
742 if (isa<Argument>(RetVal))
745 if (Instruction *RVI = dyn_cast<Instruction>(RetVal))
746 switch (RVI->getOpcode()) {
747 // Extend the analysis by looking upwards.
748 case Instruction::BitCast:
749 case Instruction::GetElementPtr:
750 case Instruction::AddrSpaceCast:
751 FlowsToReturn.insert(RVI->getOperand(0));
753 case Instruction::Select: {
754 SelectInst *SI = cast<SelectInst>(RVI);
755 FlowsToReturn.insert(SI->getTrueValue());
756 FlowsToReturn.insert(SI->getFalseValue());
759 case Instruction::PHI: {
760 PHINode *PN = cast<PHINode>(RVI);
761 for (Value *IncValue : PN->incoming_values())
762 FlowsToReturn.insert(IncValue);
766 // Check whether the pointer came from an allocation.
767 case Instruction::Alloca:
769 case Instruction::Call:
770 case Instruction::Invoke: {
772 if (CS.paramHasAttr(0, Attribute::NoAlias))
774 if (CS.getCalledFunction() &&
775 SCCNodes.count(CS.getCalledFunction()))
779 return false; // Did not come from an allocation.
782 if (PointerMayBeCaptured(RetVal, false, /*StoreCaptures=*/false))
789 /// AddNoAliasAttrs - Deduce noalias attributes for the SCC.
790 bool FunctionAttrs::AddNoAliasAttrs(const CallGraphSCC &SCC) {
791 SmallPtrSet<Function*, 8> SCCNodes;
793 // Fill SCCNodes with the elements of the SCC. Used for quickly
794 // looking up whether a given CallGraphNode is in this SCC.
795 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I)
796 SCCNodes.insert((*I)->getFunction());
798 // Check each function in turn, determining which functions return noalias
800 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
801 Function *F = (*I)->getFunction();
803 if (!F || F->hasFnAttribute(Attribute::OptimizeNone))
804 // External node or node we don't want to optimize - skip it;
808 if (F->doesNotAlias(0))
811 // Definitions with weak linkage may be overridden at linktime, so
812 // treat them like declarations.
813 if (F->isDeclaration() || F->mayBeOverridden())
816 // We annotate noalias return values, which are only applicable to
818 if (!F->getReturnType()->isPointerTy())
821 if (!IsFunctionMallocLike(F, SCCNodes))
825 bool MadeChange = false;
826 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
827 Function *F = (*I)->getFunction();
828 if (F->doesNotAlias(0) || !F->getReturnType()->isPointerTy())
831 F->setDoesNotAlias(0);
839 /// inferPrototypeAttributes - Analyze the name and prototype of the
840 /// given function and set any applicable attributes. Returns true
841 /// if any attributes were set and false otherwise.
842 bool FunctionAttrs::inferPrototypeAttributes(Function &F) {
843 if (F.hasFnAttribute(Attribute::OptimizeNone))
846 FunctionType *FTy = F.getFunctionType();
847 LibFunc::Func TheLibFunc;
848 if (!(TLI->getLibFunc(F.getName(), TheLibFunc) && TLI->has(TheLibFunc)))
851 switch (TheLibFunc) {
852 case LibFunc::strlen:
853 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
855 setOnlyReadsMemory(F);
857 setDoesNotCapture(F, 1);
859 case LibFunc::strchr:
860 case LibFunc::strrchr:
861 if (FTy->getNumParams() != 2 ||
862 !FTy->getParamType(0)->isPointerTy() ||
863 !FTy->getParamType(1)->isIntegerTy())
865 setOnlyReadsMemory(F);
868 case LibFunc::strtol:
869 case LibFunc::strtod:
870 case LibFunc::strtof:
871 case LibFunc::strtoul:
872 case LibFunc::strtoll:
873 case LibFunc::strtold:
874 case LibFunc::strtoull:
875 if (FTy->getNumParams() < 2 ||
876 !FTy->getParamType(1)->isPointerTy())
879 setDoesNotCapture(F, 2);
880 setOnlyReadsMemory(F, 1);
882 case LibFunc::strcpy:
883 case LibFunc::stpcpy:
884 case LibFunc::strcat:
885 case LibFunc::strncat:
886 case LibFunc::strncpy:
887 case LibFunc::stpncpy:
888 if (FTy->getNumParams() < 2 ||
889 !FTy->getParamType(1)->isPointerTy())
892 setDoesNotCapture(F, 2);
893 setOnlyReadsMemory(F, 2);
895 case LibFunc::strxfrm:
896 if (FTy->getNumParams() != 3 ||
897 !FTy->getParamType(0)->isPointerTy() ||
898 !FTy->getParamType(1)->isPointerTy())
901 setDoesNotCapture(F, 1);
902 setDoesNotCapture(F, 2);
903 setOnlyReadsMemory(F, 2);
905 case LibFunc::strcmp: //0,1
906 case LibFunc::strspn: // 0,1
907 case LibFunc::strncmp: // 0,1
908 case LibFunc::strcspn: //0,1
909 case LibFunc::strcoll: //0,1
910 case LibFunc::strcasecmp: // 0,1
911 case LibFunc::strncasecmp: //
912 if (FTy->getNumParams() < 2 ||
913 !FTy->getParamType(0)->isPointerTy() ||
914 !FTy->getParamType(1)->isPointerTy())
916 setOnlyReadsMemory(F);
918 setDoesNotCapture(F, 1);
919 setDoesNotCapture(F, 2);
921 case LibFunc::strstr:
922 case LibFunc::strpbrk:
923 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
925 setOnlyReadsMemory(F);
927 setDoesNotCapture(F, 2);
929 case LibFunc::strtok:
930 case LibFunc::strtok_r:
931 if (FTy->getNumParams() < 2 || !FTy->getParamType(1)->isPointerTy())
934 setDoesNotCapture(F, 2);
935 setOnlyReadsMemory(F, 2);
938 if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy())
941 setDoesNotCapture(F, 1);
942 setOnlyReadsMemory(F, 1);
944 case LibFunc::setbuf:
945 case LibFunc::setvbuf:
946 if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy())
949 setDoesNotCapture(F, 1);
951 case LibFunc::strdup:
952 case LibFunc::strndup:
953 if (FTy->getNumParams() < 1 || !FTy->getReturnType()->isPointerTy() ||
954 !FTy->getParamType(0)->isPointerTy())
957 setDoesNotAlias(F, 0);
958 setDoesNotCapture(F, 1);
959 setOnlyReadsMemory(F, 1);
962 case LibFunc::statvfs:
963 if (FTy->getNumParams() < 2 ||
964 !FTy->getParamType(0)->isPointerTy() ||
965 !FTy->getParamType(1)->isPointerTy())
968 setDoesNotCapture(F, 1);
969 setDoesNotCapture(F, 2);
970 setOnlyReadsMemory(F, 1);
972 case LibFunc::sscanf:
973 if (FTy->getNumParams() < 2 ||
974 !FTy->getParamType(0)->isPointerTy() ||
975 !FTy->getParamType(1)->isPointerTy())
978 setDoesNotCapture(F, 1);
979 setDoesNotCapture(F, 2);
980 setOnlyReadsMemory(F, 1);
981 setOnlyReadsMemory(F, 2);
983 case LibFunc::sprintf:
984 if (FTy->getNumParams() < 2 ||
985 !FTy->getParamType(0)->isPointerTy() ||
986 !FTy->getParamType(1)->isPointerTy())
989 setDoesNotCapture(F, 1);
990 setDoesNotCapture(F, 2);
991 setOnlyReadsMemory(F, 2);
993 case LibFunc::snprintf:
994 if (FTy->getNumParams() != 3 ||
995 !FTy->getParamType(0)->isPointerTy() ||
996 !FTy->getParamType(2)->isPointerTy())
999 setDoesNotCapture(F, 1);
1000 setDoesNotCapture(F, 3);
1001 setOnlyReadsMemory(F, 3);
1003 case LibFunc::setitimer:
1004 if (FTy->getNumParams() != 3 ||
1005 !FTy->getParamType(1)->isPointerTy() ||
1006 !FTy->getParamType(2)->isPointerTy())
1009 setDoesNotCapture(F, 2);
1010 setDoesNotCapture(F, 3);
1011 setOnlyReadsMemory(F, 2);
1013 case LibFunc::system:
1014 if (FTy->getNumParams() != 1 ||
1015 !FTy->getParamType(0)->isPointerTy())
1017 // May throw; "system" is a valid pthread cancellation point.
1018 setDoesNotCapture(F, 1);
1019 setOnlyReadsMemory(F, 1);
1021 case LibFunc::malloc:
1022 if (FTy->getNumParams() != 1 ||
1023 !FTy->getReturnType()->isPointerTy())
1026 setDoesNotAlias(F, 0);
1028 case LibFunc::memcmp:
1029 if (FTy->getNumParams() != 3 ||
1030 !FTy->getParamType(0)->isPointerTy() ||
1031 !FTy->getParamType(1)->isPointerTy())
1033 setOnlyReadsMemory(F);
1035 setDoesNotCapture(F, 1);
1036 setDoesNotCapture(F, 2);
1038 case LibFunc::memchr:
1039 case LibFunc::memrchr:
1040 if (FTy->getNumParams() != 3)
1042 setOnlyReadsMemory(F);
1046 case LibFunc::modff:
1047 case LibFunc::modfl:
1048 if (FTy->getNumParams() < 2 ||
1049 !FTy->getParamType(1)->isPointerTy())
1052 setDoesNotCapture(F, 2);
1054 case LibFunc::memcpy:
1055 case LibFunc::memccpy:
1056 case LibFunc::memmove:
1057 if (FTy->getNumParams() < 2 ||
1058 !FTy->getParamType(1)->isPointerTy())
1061 setDoesNotCapture(F, 2);
1062 setOnlyReadsMemory(F, 2);
1064 case LibFunc::memalign:
1065 if (!FTy->getReturnType()->isPointerTy())
1067 setDoesNotAlias(F, 0);
1069 case LibFunc::mkdir:
1070 if (FTy->getNumParams() == 0 ||
1071 !FTy->getParamType(0)->isPointerTy())
1074 setDoesNotCapture(F, 1);
1075 setOnlyReadsMemory(F, 1);
1077 case LibFunc::mktime:
1078 if (FTy->getNumParams() == 0 ||
1079 !FTy->getParamType(0)->isPointerTy())
1082 setDoesNotCapture(F, 1);
1084 case LibFunc::realloc:
1085 if (FTy->getNumParams() != 2 ||
1086 !FTy->getParamType(0)->isPointerTy() ||
1087 !FTy->getReturnType()->isPointerTy())
1090 setDoesNotAlias(F, 0);
1091 setDoesNotCapture(F, 1);
1094 if (FTy->getNumParams() != 3 ||
1095 !FTy->getParamType(1)->isPointerTy())
1097 // May throw; "read" is a valid pthread cancellation point.
1098 setDoesNotCapture(F, 2);
1100 case LibFunc::rewind:
1101 if (FTy->getNumParams() < 1 ||
1102 !FTy->getParamType(0)->isPointerTy())
1105 setDoesNotCapture(F, 1);
1107 case LibFunc::rmdir:
1108 case LibFunc::remove:
1109 case LibFunc::realpath:
1110 if (FTy->getNumParams() < 1 ||
1111 !FTy->getParamType(0)->isPointerTy())
1114 setDoesNotCapture(F, 1);
1115 setOnlyReadsMemory(F, 1);
1117 case LibFunc::rename:
1118 if (FTy->getNumParams() < 2 ||
1119 !FTy->getParamType(0)->isPointerTy() ||
1120 !FTy->getParamType(1)->isPointerTy())
1123 setDoesNotCapture(F, 1);
1124 setDoesNotCapture(F, 2);
1125 setOnlyReadsMemory(F, 1);
1126 setOnlyReadsMemory(F, 2);
1128 case LibFunc::readlink:
1129 if (FTy->getNumParams() < 2 ||
1130 !FTy->getParamType(0)->isPointerTy() ||
1131 !FTy->getParamType(1)->isPointerTy())
1134 setDoesNotCapture(F, 1);
1135 setDoesNotCapture(F, 2);
1136 setOnlyReadsMemory(F, 1);
1138 case LibFunc::write:
1139 if (FTy->getNumParams() != 3 || !FTy->getParamType(1)->isPointerTy())
1141 // May throw; "write" is a valid pthread cancellation point.
1142 setDoesNotCapture(F, 2);
1143 setOnlyReadsMemory(F, 2);
1145 case LibFunc::bcopy:
1146 if (FTy->getNumParams() != 3 ||
1147 !FTy->getParamType(0)->isPointerTy() ||
1148 !FTy->getParamType(1)->isPointerTy())
1151 setDoesNotCapture(F, 1);
1152 setDoesNotCapture(F, 2);
1153 setOnlyReadsMemory(F, 1);
1156 if (FTy->getNumParams() != 3 ||
1157 !FTy->getParamType(0)->isPointerTy() ||
1158 !FTy->getParamType(1)->isPointerTy())
1161 setOnlyReadsMemory(F);
1162 setDoesNotCapture(F, 1);
1163 setDoesNotCapture(F, 2);
1165 case LibFunc::bzero:
1166 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy())
1169 setDoesNotCapture(F, 1);
1171 case LibFunc::calloc:
1172 if (FTy->getNumParams() != 2 ||
1173 !FTy->getReturnType()->isPointerTy())
1176 setDoesNotAlias(F, 0);
1178 case LibFunc::chmod:
1179 case LibFunc::chown:
1180 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1183 setDoesNotCapture(F, 1);
1184 setOnlyReadsMemory(F, 1);
1186 case LibFunc::ctermid:
1187 case LibFunc::clearerr:
1188 case LibFunc::closedir:
1189 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1192 setDoesNotCapture(F, 1);
1197 case LibFunc::atoll:
1198 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1201 setOnlyReadsMemory(F);
1202 setDoesNotCapture(F, 1);
1204 case LibFunc::access:
1205 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy())
1208 setDoesNotCapture(F, 1);
1209 setOnlyReadsMemory(F, 1);
1211 case LibFunc::fopen:
1212 if (FTy->getNumParams() != 2 ||
1213 !FTy->getReturnType()->isPointerTy() ||
1214 !FTy->getParamType(0)->isPointerTy() ||
1215 !FTy->getParamType(1)->isPointerTy())
1218 setDoesNotAlias(F, 0);
1219 setDoesNotCapture(F, 1);
1220 setDoesNotCapture(F, 2);
1221 setOnlyReadsMemory(F, 1);
1222 setOnlyReadsMemory(F, 2);
1224 case LibFunc::fdopen:
1225 if (FTy->getNumParams() != 2 ||
1226 !FTy->getReturnType()->isPointerTy() ||
1227 !FTy->getParamType(1)->isPointerTy())
1230 setDoesNotAlias(F, 0);
1231 setDoesNotCapture(F, 2);
1232 setOnlyReadsMemory(F, 2);
1236 case LibFunc::fseek:
1237 case LibFunc::ftell:
1238 case LibFunc::fgetc:
1239 case LibFunc::fseeko:
1240 case LibFunc::ftello:
1241 case LibFunc::fileno:
1242 case LibFunc::fflush:
1243 case LibFunc::fclose:
1244 case LibFunc::fsetpos:
1245 case LibFunc::flockfile:
1246 case LibFunc::funlockfile:
1247 case LibFunc::ftrylockfile:
1248 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1251 setDoesNotCapture(F, 1);
1253 case LibFunc::ferror:
1254 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1257 setDoesNotCapture(F, 1);
1258 setOnlyReadsMemory(F);
1260 case LibFunc::fputc:
1261 case LibFunc::fstat:
1262 case LibFunc::frexp:
1263 case LibFunc::frexpf:
1264 case LibFunc::frexpl:
1265 case LibFunc::fstatvfs:
1266 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1269 setDoesNotCapture(F, 2);
1271 case LibFunc::fgets:
1272 if (FTy->getNumParams() != 3 ||
1273 !FTy->getParamType(0)->isPointerTy() ||
1274 !FTy->getParamType(2)->isPointerTy())
1277 setDoesNotCapture(F, 3);
1279 case LibFunc::fread:
1280 if (FTy->getNumParams() != 4 ||
1281 !FTy->getParamType(0)->isPointerTy() ||
1282 !FTy->getParamType(3)->isPointerTy())
1285 setDoesNotCapture(F, 1);
1286 setDoesNotCapture(F, 4);
1288 case LibFunc::fwrite:
1289 if (FTy->getNumParams() != 4 ||
1290 !FTy->getParamType(0)->isPointerTy() ||
1291 !FTy->getParamType(3)->isPointerTy())
1294 setDoesNotCapture(F, 1);
1295 setDoesNotCapture(F, 4);
1297 case LibFunc::fputs:
1298 if (FTy->getNumParams() < 2 ||
1299 !FTy->getParamType(0)->isPointerTy() ||
1300 !FTy->getParamType(1)->isPointerTy())
1303 setDoesNotCapture(F, 1);
1304 setDoesNotCapture(F, 2);
1305 setOnlyReadsMemory(F, 1);
1307 case LibFunc::fscanf:
1308 case LibFunc::fprintf:
1309 if (FTy->getNumParams() < 2 ||
1310 !FTy->getParamType(0)->isPointerTy() ||
1311 !FTy->getParamType(1)->isPointerTy())
1314 setDoesNotCapture(F, 1);
1315 setDoesNotCapture(F, 2);
1316 setOnlyReadsMemory(F, 2);
1318 case LibFunc::fgetpos:
1319 if (FTy->getNumParams() < 2 ||
1320 !FTy->getParamType(0)->isPointerTy() ||
1321 !FTy->getParamType(1)->isPointerTy())
1324 setDoesNotCapture(F, 1);
1325 setDoesNotCapture(F, 2);
1328 case LibFunc::getlogin_r:
1329 case LibFunc::getc_unlocked:
1330 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1333 setDoesNotCapture(F, 1);
1335 case LibFunc::getenv:
1336 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1339 setOnlyReadsMemory(F);
1340 setDoesNotCapture(F, 1);
1343 case LibFunc::getchar:
1346 case LibFunc::getitimer:
1347 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1350 setDoesNotCapture(F, 2);
1352 case LibFunc::getpwnam:
1353 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1356 setDoesNotCapture(F, 1);
1357 setOnlyReadsMemory(F, 1);
1359 case LibFunc::ungetc:
1360 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1363 setDoesNotCapture(F, 2);
1365 case LibFunc::uname:
1366 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1369 setDoesNotCapture(F, 1);
1371 case LibFunc::unlink:
1372 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1375 setDoesNotCapture(F, 1);
1376 setOnlyReadsMemory(F, 1);
1378 case LibFunc::unsetenv:
1379 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1382 setDoesNotCapture(F, 1);
1383 setOnlyReadsMemory(F, 1);
1385 case LibFunc::utime:
1386 case LibFunc::utimes:
1387 if (FTy->getNumParams() != 2 ||
1388 !FTy->getParamType(0)->isPointerTy() ||
1389 !FTy->getParamType(1)->isPointerTy())
1392 setDoesNotCapture(F, 1);
1393 setDoesNotCapture(F, 2);
1394 setOnlyReadsMemory(F, 1);
1395 setOnlyReadsMemory(F, 2);
1398 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1401 setDoesNotCapture(F, 2);
1404 case LibFunc::printf:
1405 case LibFunc::perror:
1406 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1409 setDoesNotCapture(F, 1);
1410 setOnlyReadsMemory(F, 1);
1412 case LibFunc::pread:
1413 if (FTy->getNumParams() != 4 || !FTy->getParamType(1)->isPointerTy())
1415 // May throw; "pread" is a valid pthread cancellation point.
1416 setDoesNotCapture(F, 2);
1418 case LibFunc::pwrite:
1419 if (FTy->getNumParams() != 4 || !FTy->getParamType(1)->isPointerTy())
1421 // May throw; "pwrite" is a valid pthread cancellation point.
1422 setDoesNotCapture(F, 2);
1423 setOnlyReadsMemory(F, 2);
1425 case LibFunc::putchar:
1428 case LibFunc::popen:
1429 if (FTy->getNumParams() != 2 ||
1430 !FTy->getReturnType()->isPointerTy() ||
1431 !FTy->getParamType(0)->isPointerTy() ||
1432 !FTy->getParamType(1)->isPointerTy())
1435 setDoesNotAlias(F, 0);
1436 setDoesNotCapture(F, 1);
1437 setDoesNotCapture(F, 2);
1438 setOnlyReadsMemory(F, 1);
1439 setOnlyReadsMemory(F, 2);
1441 case LibFunc::pclose:
1442 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1445 setDoesNotCapture(F, 1);
1447 case LibFunc::vscanf:
1448 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1451 setDoesNotCapture(F, 1);
1452 setOnlyReadsMemory(F, 1);
1454 case LibFunc::vsscanf:
1455 if (FTy->getNumParams() != 3 ||
1456 !FTy->getParamType(1)->isPointerTy() ||
1457 !FTy->getParamType(2)->isPointerTy())
1460 setDoesNotCapture(F, 1);
1461 setDoesNotCapture(F, 2);
1462 setOnlyReadsMemory(F, 1);
1463 setOnlyReadsMemory(F, 2);
1465 case LibFunc::vfscanf:
1466 if (FTy->getNumParams() != 3 ||
1467 !FTy->getParamType(1)->isPointerTy() ||
1468 !FTy->getParamType(2)->isPointerTy())
1471 setDoesNotCapture(F, 1);
1472 setDoesNotCapture(F, 2);
1473 setOnlyReadsMemory(F, 2);
1475 case LibFunc::valloc:
1476 if (!FTy->getReturnType()->isPointerTy())
1479 setDoesNotAlias(F, 0);
1481 case LibFunc::vprintf:
1482 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy())
1485 setDoesNotCapture(F, 1);
1486 setOnlyReadsMemory(F, 1);
1488 case LibFunc::vfprintf:
1489 case LibFunc::vsprintf:
1490 if (FTy->getNumParams() != 3 ||
1491 !FTy->getParamType(0)->isPointerTy() ||
1492 !FTy->getParamType(1)->isPointerTy())
1495 setDoesNotCapture(F, 1);
1496 setDoesNotCapture(F, 2);
1497 setOnlyReadsMemory(F, 2);
1499 case LibFunc::vsnprintf:
1500 if (FTy->getNumParams() != 4 ||
1501 !FTy->getParamType(0)->isPointerTy() ||
1502 !FTy->getParamType(2)->isPointerTy())
1505 setDoesNotCapture(F, 1);
1506 setDoesNotCapture(F, 3);
1507 setOnlyReadsMemory(F, 3);
1510 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy())
1512 // May throw; "open" is a valid pthread cancellation point.
1513 setDoesNotCapture(F, 1);
1514 setOnlyReadsMemory(F, 1);
1516 case LibFunc::opendir:
1517 if (FTy->getNumParams() != 1 ||
1518 !FTy->getReturnType()->isPointerTy() ||
1519 !FTy->getParamType(0)->isPointerTy())
1522 setDoesNotAlias(F, 0);
1523 setDoesNotCapture(F, 1);
1524 setOnlyReadsMemory(F, 1);
1526 case LibFunc::tmpfile:
1527 if (!FTy->getReturnType()->isPointerTy())
1530 setDoesNotAlias(F, 0);
1532 case LibFunc::times:
1533 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1536 setDoesNotCapture(F, 1);
1538 case LibFunc::htonl:
1539 case LibFunc::htons:
1540 case LibFunc::ntohl:
1541 case LibFunc::ntohs:
1543 setDoesNotAccessMemory(F);
1545 case LibFunc::lstat:
1546 if (FTy->getNumParams() != 2 ||
1547 !FTy->getParamType(0)->isPointerTy() ||
1548 !FTy->getParamType(1)->isPointerTy())
1551 setDoesNotCapture(F, 1);
1552 setDoesNotCapture(F, 2);
1553 setOnlyReadsMemory(F, 1);
1555 case LibFunc::lchown:
1556 if (FTy->getNumParams() != 3 || !FTy->getParamType(0)->isPointerTy())
1559 setDoesNotCapture(F, 1);
1560 setOnlyReadsMemory(F, 1);
1562 case LibFunc::qsort:
1563 if (FTy->getNumParams() != 4 || !FTy->getParamType(3)->isPointerTy())
1565 // May throw; places call through function pointer.
1566 setDoesNotCapture(F, 4);
1568 case LibFunc::dunder_strdup:
1569 case LibFunc::dunder_strndup:
1570 if (FTy->getNumParams() < 1 ||
1571 !FTy->getReturnType()->isPointerTy() ||
1572 !FTy->getParamType(0)->isPointerTy())
1575 setDoesNotAlias(F, 0);
1576 setDoesNotCapture(F, 1);
1577 setOnlyReadsMemory(F, 1);
1579 case LibFunc::dunder_strtok_r:
1580 if (FTy->getNumParams() != 3 ||
1581 !FTy->getParamType(1)->isPointerTy())
1584 setDoesNotCapture(F, 2);
1585 setOnlyReadsMemory(F, 2);
1587 case LibFunc::under_IO_getc:
1588 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1591 setDoesNotCapture(F, 1);
1593 case LibFunc::under_IO_putc:
1594 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1597 setDoesNotCapture(F, 2);
1599 case LibFunc::dunder_isoc99_scanf:
1600 if (FTy->getNumParams() < 1 ||
1601 !FTy->getParamType(0)->isPointerTy())
1604 setDoesNotCapture(F, 1);
1605 setOnlyReadsMemory(F, 1);
1607 case LibFunc::stat64:
1608 case LibFunc::lstat64:
1609 case LibFunc::statvfs64:
1610 if (FTy->getNumParams() < 1 ||
1611 !FTy->getParamType(0)->isPointerTy() ||
1612 !FTy->getParamType(1)->isPointerTy())
1615 setDoesNotCapture(F, 1);
1616 setDoesNotCapture(F, 2);
1617 setOnlyReadsMemory(F, 1);
1619 case LibFunc::dunder_isoc99_sscanf:
1620 if (FTy->getNumParams() < 1 ||
1621 !FTy->getParamType(0)->isPointerTy() ||
1622 !FTy->getParamType(1)->isPointerTy())
1625 setDoesNotCapture(F, 1);
1626 setDoesNotCapture(F, 2);
1627 setOnlyReadsMemory(F, 1);
1628 setOnlyReadsMemory(F, 2);
1630 case LibFunc::fopen64:
1631 if (FTy->getNumParams() != 2 ||
1632 !FTy->getReturnType()->isPointerTy() ||
1633 !FTy->getParamType(0)->isPointerTy() ||
1634 !FTy->getParamType(1)->isPointerTy())
1637 setDoesNotAlias(F, 0);
1638 setDoesNotCapture(F, 1);
1639 setDoesNotCapture(F, 2);
1640 setOnlyReadsMemory(F, 1);
1641 setOnlyReadsMemory(F, 2);
1643 case LibFunc::fseeko64:
1644 case LibFunc::ftello64:
1645 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1648 setDoesNotCapture(F, 1);
1650 case LibFunc::tmpfile64:
1651 if (!FTy->getReturnType()->isPointerTy())
1654 setDoesNotAlias(F, 0);
1656 case LibFunc::fstat64:
1657 case LibFunc::fstatvfs64:
1658 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1661 setDoesNotCapture(F, 2);
1663 case LibFunc::open64:
1664 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy())
1666 // May throw; "open" is a valid pthread cancellation point.
1667 setDoesNotCapture(F, 1);
1668 setOnlyReadsMemory(F, 1);
1670 case LibFunc::gettimeofday:
1671 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy() ||
1672 !FTy->getParamType(1)->isPointerTy())
1674 // Currently some platforms have the restrict keyword on the arguments to
1675 // gettimeofday. To be conservative, do not add noalias to gettimeofday's
1678 setDoesNotCapture(F, 1);
1679 setDoesNotCapture(F, 2);
1682 // Didn't mark any attributes.
1689 /// annotateLibraryCalls - Adds attributes to well-known standard library
1690 /// call declarations.
1691 bool FunctionAttrs::annotateLibraryCalls(const CallGraphSCC &SCC) {
1692 bool MadeChange = false;
1694 // Check each function in turn annotating well-known library function
1695 // declarations with attributes.
1696 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
1697 Function *F = (*I)->getFunction();
1699 if (F && F->isDeclaration())
1700 MadeChange |= inferPrototypeAttributes(*F);
1706 bool FunctionAttrs::runOnSCC(CallGraphSCC &SCC) {
1707 AA = &getAnalysis<AliasAnalysis>();
1708 TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
1710 bool Changed = annotateLibraryCalls(SCC);
1711 Changed |= AddReadAttrs(SCC);
1712 Changed |= AddArgumentAttrs(SCC);
1713 Changed |= AddNoAliasAttrs(SCC);