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 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 = MemoryLocation::get(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 = MemoryLocation::get(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 = MemoryLocation::get(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).second)
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).second)
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 R.addAttribute(Attribute::ReadOnly)
709 .addAttribute(Attribute::ReadNone);
710 for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) {
711 Argument *A = ArgumentSCC[i]->Definition;
712 // Clear out existing readonly/readnone attributes
713 A->removeAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, R));
714 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
715 ReadAttr == Attribute::ReadOnly ? ++NumReadOnlyArg : ++NumReadNoneArg;
724 /// IsFunctionMallocLike - A function is malloc-like if it returns either null
725 /// or a pointer that doesn't alias any other pointer visible to the caller.
726 bool FunctionAttrs::IsFunctionMallocLike(Function *F,
727 SmallPtrSet<Function*, 8> &SCCNodes) const {
728 SmallSetVector<Value *, 8> FlowsToReturn;
729 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I)
730 if (ReturnInst *Ret = dyn_cast<ReturnInst>(I->getTerminator()))
731 FlowsToReturn.insert(Ret->getReturnValue());
733 for (unsigned i = 0; i != FlowsToReturn.size(); ++i) {
734 Value *RetVal = FlowsToReturn[i];
736 if (Constant *C = dyn_cast<Constant>(RetVal)) {
737 if (!C->isNullValue() && !isa<UndefValue>(C))
743 if (isa<Argument>(RetVal))
746 if (Instruction *RVI = dyn_cast<Instruction>(RetVal))
747 switch (RVI->getOpcode()) {
748 // Extend the analysis by looking upwards.
749 case Instruction::BitCast:
750 case Instruction::GetElementPtr:
751 case Instruction::AddrSpaceCast:
752 FlowsToReturn.insert(RVI->getOperand(0));
754 case Instruction::Select: {
755 SelectInst *SI = cast<SelectInst>(RVI);
756 FlowsToReturn.insert(SI->getTrueValue());
757 FlowsToReturn.insert(SI->getFalseValue());
760 case Instruction::PHI: {
761 PHINode *PN = cast<PHINode>(RVI);
762 for (Value *IncValue : PN->incoming_values())
763 FlowsToReturn.insert(IncValue);
767 // Check whether the pointer came from an allocation.
768 case Instruction::Alloca:
770 case Instruction::Call:
771 case Instruction::Invoke: {
773 if (CS.paramHasAttr(0, Attribute::NoAlias))
775 if (CS.getCalledFunction() &&
776 SCCNodes.count(CS.getCalledFunction()))
780 return false; // Did not come from an allocation.
783 if (PointerMayBeCaptured(RetVal, false, /*StoreCaptures=*/false))
790 /// AddNoAliasAttrs - Deduce noalias attributes for the SCC.
791 bool FunctionAttrs::AddNoAliasAttrs(const CallGraphSCC &SCC) {
792 SmallPtrSet<Function*, 8> SCCNodes;
794 // Fill SCCNodes with the elements of the SCC. Used for quickly
795 // looking up whether a given CallGraphNode is in this SCC.
796 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I)
797 SCCNodes.insert((*I)->getFunction());
799 // Check each function in turn, determining which functions return noalias
801 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
802 Function *F = (*I)->getFunction();
804 if (!F || F->hasFnAttribute(Attribute::OptimizeNone))
805 // External node or node we don't want to optimize - skip it;
809 if (F->doesNotAlias(0))
812 // Definitions with weak linkage may be overridden at linktime, so
813 // treat them like declarations.
814 if (F->isDeclaration() || F->mayBeOverridden())
817 // We annotate noalias return values, which are only applicable to
819 if (!F->getReturnType()->isPointerTy())
822 if (!IsFunctionMallocLike(F, SCCNodes))
826 bool MadeChange = false;
827 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
828 Function *F = (*I)->getFunction();
829 if (F->doesNotAlias(0) || !F->getReturnType()->isPointerTy())
832 F->setDoesNotAlias(0);
840 /// inferPrototypeAttributes - Analyze the name and prototype of the
841 /// given function and set any applicable attributes. Returns true
842 /// if any attributes were set and false otherwise.
843 bool FunctionAttrs::inferPrototypeAttributes(Function &F) {
844 if (F.hasFnAttribute(Attribute::OptimizeNone))
847 FunctionType *FTy = F.getFunctionType();
848 LibFunc::Func TheLibFunc;
849 if (!(TLI->getLibFunc(F.getName(), TheLibFunc) && TLI->has(TheLibFunc)))
852 switch (TheLibFunc) {
853 case LibFunc::strlen:
854 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
856 setOnlyReadsMemory(F);
858 setDoesNotCapture(F, 1);
860 case LibFunc::strchr:
861 case LibFunc::strrchr:
862 if (FTy->getNumParams() != 2 ||
863 !FTy->getParamType(0)->isPointerTy() ||
864 !FTy->getParamType(1)->isIntegerTy())
866 setOnlyReadsMemory(F);
869 case LibFunc::strtol:
870 case LibFunc::strtod:
871 case LibFunc::strtof:
872 case LibFunc::strtoul:
873 case LibFunc::strtoll:
874 case LibFunc::strtold:
875 case LibFunc::strtoull:
876 if (FTy->getNumParams() < 2 ||
877 !FTy->getParamType(1)->isPointerTy())
880 setDoesNotCapture(F, 2);
881 setOnlyReadsMemory(F, 1);
883 case LibFunc::strcpy:
884 case LibFunc::stpcpy:
885 case LibFunc::strcat:
886 case LibFunc::strncat:
887 case LibFunc::strncpy:
888 case LibFunc::stpncpy:
889 if (FTy->getNumParams() < 2 ||
890 !FTy->getParamType(1)->isPointerTy())
893 setDoesNotCapture(F, 2);
894 setOnlyReadsMemory(F, 2);
896 case LibFunc::strxfrm:
897 if (FTy->getNumParams() != 3 ||
898 !FTy->getParamType(0)->isPointerTy() ||
899 !FTy->getParamType(1)->isPointerTy())
902 setDoesNotCapture(F, 1);
903 setDoesNotCapture(F, 2);
904 setOnlyReadsMemory(F, 2);
906 case LibFunc::strcmp: //0,1
907 case LibFunc::strspn: // 0,1
908 case LibFunc::strncmp: // 0,1
909 case LibFunc::strcspn: //0,1
910 case LibFunc::strcoll: //0,1
911 case LibFunc::strcasecmp: // 0,1
912 case LibFunc::strncasecmp: //
913 if (FTy->getNumParams() < 2 ||
914 !FTy->getParamType(0)->isPointerTy() ||
915 !FTy->getParamType(1)->isPointerTy())
917 setOnlyReadsMemory(F);
919 setDoesNotCapture(F, 1);
920 setDoesNotCapture(F, 2);
922 case LibFunc::strstr:
923 case LibFunc::strpbrk:
924 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
926 setOnlyReadsMemory(F);
928 setDoesNotCapture(F, 2);
930 case LibFunc::strtok:
931 case LibFunc::strtok_r:
932 if (FTy->getNumParams() < 2 || !FTy->getParamType(1)->isPointerTy())
935 setDoesNotCapture(F, 2);
936 setOnlyReadsMemory(F, 2);
939 if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy())
942 setDoesNotCapture(F, 1);
943 setOnlyReadsMemory(F, 1);
945 case LibFunc::setbuf:
946 case LibFunc::setvbuf:
947 if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy())
950 setDoesNotCapture(F, 1);
952 case LibFunc::strdup:
953 case LibFunc::strndup:
954 if (FTy->getNumParams() < 1 || !FTy->getReturnType()->isPointerTy() ||
955 !FTy->getParamType(0)->isPointerTy())
958 setDoesNotAlias(F, 0);
959 setDoesNotCapture(F, 1);
960 setOnlyReadsMemory(F, 1);
963 case LibFunc::statvfs:
964 if (FTy->getNumParams() < 2 ||
965 !FTy->getParamType(0)->isPointerTy() ||
966 !FTy->getParamType(1)->isPointerTy())
969 setDoesNotCapture(F, 1);
970 setDoesNotCapture(F, 2);
971 setOnlyReadsMemory(F, 1);
973 case LibFunc::sscanf:
974 if (FTy->getNumParams() < 2 ||
975 !FTy->getParamType(0)->isPointerTy() ||
976 !FTy->getParamType(1)->isPointerTy())
979 setDoesNotCapture(F, 1);
980 setDoesNotCapture(F, 2);
981 setOnlyReadsMemory(F, 1);
982 setOnlyReadsMemory(F, 2);
984 case LibFunc::sprintf:
985 if (FTy->getNumParams() < 2 ||
986 !FTy->getParamType(0)->isPointerTy() ||
987 !FTy->getParamType(1)->isPointerTy())
990 setDoesNotCapture(F, 1);
991 setDoesNotCapture(F, 2);
992 setOnlyReadsMemory(F, 2);
994 case LibFunc::snprintf:
995 if (FTy->getNumParams() != 3 ||
996 !FTy->getParamType(0)->isPointerTy() ||
997 !FTy->getParamType(2)->isPointerTy())
1000 setDoesNotCapture(F, 1);
1001 setDoesNotCapture(F, 3);
1002 setOnlyReadsMemory(F, 3);
1004 case LibFunc::setitimer:
1005 if (FTy->getNumParams() != 3 ||
1006 !FTy->getParamType(1)->isPointerTy() ||
1007 !FTy->getParamType(2)->isPointerTy())
1010 setDoesNotCapture(F, 2);
1011 setDoesNotCapture(F, 3);
1012 setOnlyReadsMemory(F, 2);
1014 case LibFunc::system:
1015 if (FTy->getNumParams() != 1 ||
1016 !FTy->getParamType(0)->isPointerTy())
1018 // May throw; "system" is a valid pthread cancellation point.
1019 setDoesNotCapture(F, 1);
1020 setOnlyReadsMemory(F, 1);
1022 case LibFunc::malloc:
1023 if (FTy->getNumParams() != 1 ||
1024 !FTy->getReturnType()->isPointerTy())
1027 setDoesNotAlias(F, 0);
1029 case LibFunc::memcmp:
1030 if (FTy->getNumParams() != 3 ||
1031 !FTy->getParamType(0)->isPointerTy() ||
1032 !FTy->getParamType(1)->isPointerTy())
1034 setOnlyReadsMemory(F);
1036 setDoesNotCapture(F, 1);
1037 setDoesNotCapture(F, 2);
1039 case LibFunc::memchr:
1040 case LibFunc::memrchr:
1041 if (FTy->getNumParams() != 3)
1043 setOnlyReadsMemory(F);
1047 case LibFunc::modff:
1048 case LibFunc::modfl:
1049 if (FTy->getNumParams() < 2 ||
1050 !FTy->getParamType(1)->isPointerTy())
1053 setDoesNotCapture(F, 2);
1055 case LibFunc::memcpy:
1056 case LibFunc::memccpy:
1057 case LibFunc::memmove:
1058 if (FTy->getNumParams() < 2 ||
1059 !FTy->getParamType(1)->isPointerTy())
1062 setDoesNotCapture(F, 2);
1063 setOnlyReadsMemory(F, 2);
1065 case LibFunc::memalign:
1066 if (!FTy->getReturnType()->isPointerTy())
1068 setDoesNotAlias(F, 0);
1070 case LibFunc::mkdir:
1071 if (FTy->getNumParams() == 0 ||
1072 !FTy->getParamType(0)->isPointerTy())
1075 setDoesNotCapture(F, 1);
1076 setOnlyReadsMemory(F, 1);
1078 case LibFunc::mktime:
1079 if (FTy->getNumParams() == 0 ||
1080 !FTy->getParamType(0)->isPointerTy())
1083 setDoesNotCapture(F, 1);
1085 case LibFunc::realloc:
1086 if (FTy->getNumParams() != 2 ||
1087 !FTy->getParamType(0)->isPointerTy() ||
1088 !FTy->getReturnType()->isPointerTy())
1091 setDoesNotAlias(F, 0);
1092 setDoesNotCapture(F, 1);
1095 if (FTy->getNumParams() != 3 ||
1096 !FTy->getParamType(1)->isPointerTy())
1098 // May throw; "read" is a valid pthread cancellation point.
1099 setDoesNotCapture(F, 2);
1101 case LibFunc::rewind:
1102 if (FTy->getNumParams() < 1 ||
1103 !FTy->getParamType(0)->isPointerTy())
1106 setDoesNotCapture(F, 1);
1108 case LibFunc::rmdir:
1109 case LibFunc::remove:
1110 case LibFunc::realpath:
1111 if (FTy->getNumParams() < 1 ||
1112 !FTy->getParamType(0)->isPointerTy())
1115 setDoesNotCapture(F, 1);
1116 setOnlyReadsMemory(F, 1);
1118 case LibFunc::rename:
1119 if (FTy->getNumParams() < 2 ||
1120 !FTy->getParamType(0)->isPointerTy() ||
1121 !FTy->getParamType(1)->isPointerTy())
1124 setDoesNotCapture(F, 1);
1125 setDoesNotCapture(F, 2);
1126 setOnlyReadsMemory(F, 1);
1127 setOnlyReadsMemory(F, 2);
1129 case LibFunc::readlink:
1130 if (FTy->getNumParams() < 2 ||
1131 !FTy->getParamType(0)->isPointerTy() ||
1132 !FTy->getParamType(1)->isPointerTy())
1135 setDoesNotCapture(F, 1);
1136 setDoesNotCapture(F, 2);
1137 setOnlyReadsMemory(F, 1);
1139 case LibFunc::write:
1140 if (FTy->getNumParams() != 3 || !FTy->getParamType(1)->isPointerTy())
1142 // May throw; "write" is a valid pthread cancellation point.
1143 setDoesNotCapture(F, 2);
1144 setOnlyReadsMemory(F, 2);
1146 case LibFunc::bcopy:
1147 if (FTy->getNumParams() != 3 ||
1148 !FTy->getParamType(0)->isPointerTy() ||
1149 !FTy->getParamType(1)->isPointerTy())
1152 setDoesNotCapture(F, 1);
1153 setDoesNotCapture(F, 2);
1154 setOnlyReadsMemory(F, 1);
1157 if (FTy->getNumParams() != 3 ||
1158 !FTy->getParamType(0)->isPointerTy() ||
1159 !FTy->getParamType(1)->isPointerTy())
1162 setOnlyReadsMemory(F);
1163 setDoesNotCapture(F, 1);
1164 setDoesNotCapture(F, 2);
1166 case LibFunc::bzero:
1167 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy())
1170 setDoesNotCapture(F, 1);
1172 case LibFunc::calloc:
1173 if (FTy->getNumParams() != 2 ||
1174 !FTy->getReturnType()->isPointerTy())
1177 setDoesNotAlias(F, 0);
1179 case LibFunc::chmod:
1180 case LibFunc::chown:
1181 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1184 setDoesNotCapture(F, 1);
1185 setOnlyReadsMemory(F, 1);
1187 case LibFunc::ctermid:
1188 case LibFunc::clearerr:
1189 case LibFunc::closedir:
1190 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1193 setDoesNotCapture(F, 1);
1198 case LibFunc::atoll:
1199 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1202 setOnlyReadsMemory(F);
1203 setDoesNotCapture(F, 1);
1205 case LibFunc::access:
1206 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy())
1209 setDoesNotCapture(F, 1);
1210 setOnlyReadsMemory(F, 1);
1212 case LibFunc::fopen:
1213 if (FTy->getNumParams() != 2 ||
1214 !FTy->getReturnType()->isPointerTy() ||
1215 !FTy->getParamType(0)->isPointerTy() ||
1216 !FTy->getParamType(1)->isPointerTy())
1219 setDoesNotAlias(F, 0);
1220 setDoesNotCapture(F, 1);
1221 setDoesNotCapture(F, 2);
1222 setOnlyReadsMemory(F, 1);
1223 setOnlyReadsMemory(F, 2);
1225 case LibFunc::fdopen:
1226 if (FTy->getNumParams() != 2 ||
1227 !FTy->getReturnType()->isPointerTy() ||
1228 !FTy->getParamType(1)->isPointerTy())
1231 setDoesNotAlias(F, 0);
1232 setDoesNotCapture(F, 2);
1233 setOnlyReadsMemory(F, 2);
1237 case LibFunc::fseek:
1238 case LibFunc::ftell:
1239 case LibFunc::fgetc:
1240 case LibFunc::fseeko:
1241 case LibFunc::ftello:
1242 case LibFunc::fileno:
1243 case LibFunc::fflush:
1244 case LibFunc::fclose:
1245 case LibFunc::fsetpos:
1246 case LibFunc::flockfile:
1247 case LibFunc::funlockfile:
1248 case LibFunc::ftrylockfile:
1249 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1252 setDoesNotCapture(F, 1);
1254 case LibFunc::ferror:
1255 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1258 setDoesNotCapture(F, 1);
1259 setOnlyReadsMemory(F);
1261 case LibFunc::fputc:
1262 case LibFunc::fstat:
1263 case LibFunc::frexp:
1264 case LibFunc::frexpf:
1265 case LibFunc::frexpl:
1266 case LibFunc::fstatvfs:
1267 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1270 setDoesNotCapture(F, 2);
1272 case LibFunc::fgets:
1273 if (FTy->getNumParams() != 3 ||
1274 !FTy->getParamType(0)->isPointerTy() ||
1275 !FTy->getParamType(2)->isPointerTy())
1278 setDoesNotCapture(F, 3);
1280 case LibFunc::fread:
1281 if (FTy->getNumParams() != 4 ||
1282 !FTy->getParamType(0)->isPointerTy() ||
1283 !FTy->getParamType(3)->isPointerTy())
1286 setDoesNotCapture(F, 1);
1287 setDoesNotCapture(F, 4);
1289 case LibFunc::fwrite:
1290 if (FTy->getNumParams() != 4 ||
1291 !FTy->getParamType(0)->isPointerTy() ||
1292 !FTy->getParamType(3)->isPointerTy())
1295 setDoesNotCapture(F, 1);
1296 setDoesNotCapture(F, 4);
1298 case LibFunc::fputs:
1299 if (FTy->getNumParams() < 2 ||
1300 !FTy->getParamType(0)->isPointerTy() ||
1301 !FTy->getParamType(1)->isPointerTy())
1304 setDoesNotCapture(F, 1);
1305 setDoesNotCapture(F, 2);
1306 setOnlyReadsMemory(F, 1);
1308 case LibFunc::fscanf:
1309 case LibFunc::fprintf:
1310 if (FTy->getNumParams() < 2 ||
1311 !FTy->getParamType(0)->isPointerTy() ||
1312 !FTy->getParamType(1)->isPointerTy())
1315 setDoesNotCapture(F, 1);
1316 setDoesNotCapture(F, 2);
1317 setOnlyReadsMemory(F, 2);
1319 case LibFunc::fgetpos:
1320 if (FTy->getNumParams() < 2 ||
1321 !FTy->getParamType(0)->isPointerTy() ||
1322 !FTy->getParamType(1)->isPointerTy())
1325 setDoesNotCapture(F, 1);
1326 setDoesNotCapture(F, 2);
1329 case LibFunc::getlogin_r:
1330 case LibFunc::getc_unlocked:
1331 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1334 setDoesNotCapture(F, 1);
1336 case LibFunc::getenv:
1337 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1340 setOnlyReadsMemory(F);
1341 setDoesNotCapture(F, 1);
1344 case LibFunc::getchar:
1347 case LibFunc::getitimer:
1348 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1351 setDoesNotCapture(F, 2);
1353 case LibFunc::getpwnam:
1354 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1357 setDoesNotCapture(F, 1);
1358 setOnlyReadsMemory(F, 1);
1360 case LibFunc::ungetc:
1361 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1364 setDoesNotCapture(F, 2);
1366 case LibFunc::uname:
1367 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1370 setDoesNotCapture(F, 1);
1372 case LibFunc::unlink:
1373 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1376 setDoesNotCapture(F, 1);
1377 setOnlyReadsMemory(F, 1);
1379 case LibFunc::unsetenv:
1380 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1383 setDoesNotCapture(F, 1);
1384 setOnlyReadsMemory(F, 1);
1386 case LibFunc::utime:
1387 case LibFunc::utimes:
1388 if (FTy->getNumParams() != 2 ||
1389 !FTy->getParamType(0)->isPointerTy() ||
1390 !FTy->getParamType(1)->isPointerTy())
1393 setDoesNotCapture(F, 1);
1394 setDoesNotCapture(F, 2);
1395 setOnlyReadsMemory(F, 1);
1396 setOnlyReadsMemory(F, 2);
1399 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1402 setDoesNotCapture(F, 2);
1405 case LibFunc::printf:
1406 case LibFunc::perror:
1407 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1410 setDoesNotCapture(F, 1);
1411 setOnlyReadsMemory(F, 1);
1413 case LibFunc::pread:
1414 if (FTy->getNumParams() != 4 || !FTy->getParamType(1)->isPointerTy())
1416 // May throw; "pread" is a valid pthread cancellation point.
1417 setDoesNotCapture(F, 2);
1419 case LibFunc::pwrite:
1420 if (FTy->getNumParams() != 4 || !FTy->getParamType(1)->isPointerTy())
1422 // May throw; "pwrite" is a valid pthread cancellation point.
1423 setDoesNotCapture(F, 2);
1424 setOnlyReadsMemory(F, 2);
1426 case LibFunc::putchar:
1429 case LibFunc::popen:
1430 if (FTy->getNumParams() != 2 ||
1431 !FTy->getReturnType()->isPointerTy() ||
1432 !FTy->getParamType(0)->isPointerTy() ||
1433 !FTy->getParamType(1)->isPointerTy())
1436 setDoesNotAlias(F, 0);
1437 setDoesNotCapture(F, 1);
1438 setDoesNotCapture(F, 2);
1439 setOnlyReadsMemory(F, 1);
1440 setOnlyReadsMemory(F, 2);
1442 case LibFunc::pclose:
1443 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1446 setDoesNotCapture(F, 1);
1448 case LibFunc::vscanf:
1449 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1452 setDoesNotCapture(F, 1);
1453 setOnlyReadsMemory(F, 1);
1455 case LibFunc::vsscanf:
1456 if (FTy->getNumParams() != 3 ||
1457 !FTy->getParamType(1)->isPointerTy() ||
1458 !FTy->getParamType(2)->isPointerTy())
1461 setDoesNotCapture(F, 1);
1462 setDoesNotCapture(F, 2);
1463 setOnlyReadsMemory(F, 1);
1464 setOnlyReadsMemory(F, 2);
1466 case LibFunc::vfscanf:
1467 if (FTy->getNumParams() != 3 ||
1468 !FTy->getParamType(1)->isPointerTy() ||
1469 !FTy->getParamType(2)->isPointerTy())
1472 setDoesNotCapture(F, 1);
1473 setDoesNotCapture(F, 2);
1474 setOnlyReadsMemory(F, 2);
1476 case LibFunc::valloc:
1477 if (!FTy->getReturnType()->isPointerTy())
1480 setDoesNotAlias(F, 0);
1482 case LibFunc::vprintf:
1483 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy())
1486 setDoesNotCapture(F, 1);
1487 setOnlyReadsMemory(F, 1);
1489 case LibFunc::vfprintf:
1490 case LibFunc::vsprintf:
1491 if (FTy->getNumParams() != 3 ||
1492 !FTy->getParamType(0)->isPointerTy() ||
1493 !FTy->getParamType(1)->isPointerTy())
1496 setDoesNotCapture(F, 1);
1497 setDoesNotCapture(F, 2);
1498 setOnlyReadsMemory(F, 2);
1500 case LibFunc::vsnprintf:
1501 if (FTy->getNumParams() != 4 ||
1502 !FTy->getParamType(0)->isPointerTy() ||
1503 !FTy->getParamType(2)->isPointerTy())
1506 setDoesNotCapture(F, 1);
1507 setDoesNotCapture(F, 3);
1508 setOnlyReadsMemory(F, 3);
1511 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy())
1513 // May throw; "open" is a valid pthread cancellation point.
1514 setDoesNotCapture(F, 1);
1515 setOnlyReadsMemory(F, 1);
1517 case LibFunc::opendir:
1518 if (FTy->getNumParams() != 1 ||
1519 !FTy->getReturnType()->isPointerTy() ||
1520 !FTy->getParamType(0)->isPointerTy())
1523 setDoesNotAlias(F, 0);
1524 setDoesNotCapture(F, 1);
1525 setOnlyReadsMemory(F, 1);
1527 case LibFunc::tmpfile:
1528 if (!FTy->getReturnType()->isPointerTy())
1531 setDoesNotAlias(F, 0);
1533 case LibFunc::times:
1534 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1537 setDoesNotCapture(F, 1);
1539 case LibFunc::htonl:
1540 case LibFunc::htons:
1541 case LibFunc::ntohl:
1542 case LibFunc::ntohs:
1544 setDoesNotAccessMemory(F);
1546 case LibFunc::lstat:
1547 if (FTy->getNumParams() != 2 ||
1548 !FTy->getParamType(0)->isPointerTy() ||
1549 !FTy->getParamType(1)->isPointerTy())
1552 setDoesNotCapture(F, 1);
1553 setDoesNotCapture(F, 2);
1554 setOnlyReadsMemory(F, 1);
1556 case LibFunc::lchown:
1557 if (FTy->getNumParams() != 3 || !FTy->getParamType(0)->isPointerTy())
1560 setDoesNotCapture(F, 1);
1561 setOnlyReadsMemory(F, 1);
1563 case LibFunc::qsort:
1564 if (FTy->getNumParams() != 4 || !FTy->getParamType(3)->isPointerTy())
1566 // May throw; places call through function pointer.
1567 setDoesNotCapture(F, 4);
1569 case LibFunc::dunder_strdup:
1570 case LibFunc::dunder_strndup:
1571 if (FTy->getNumParams() < 1 ||
1572 !FTy->getReturnType()->isPointerTy() ||
1573 !FTy->getParamType(0)->isPointerTy())
1576 setDoesNotAlias(F, 0);
1577 setDoesNotCapture(F, 1);
1578 setOnlyReadsMemory(F, 1);
1580 case LibFunc::dunder_strtok_r:
1581 if (FTy->getNumParams() != 3 ||
1582 !FTy->getParamType(1)->isPointerTy())
1585 setDoesNotCapture(F, 2);
1586 setOnlyReadsMemory(F, 2);
1588 case LibFunc::under_IO_getc:
1589 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1592 setDoesNotCapture(F, 1);
1594 case LibFunc::under_IO_putc:
1595 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1598 setDoesNotCapture(F, 2);
1600 case LibFunc::dunder_isoc99_scanf:
1601 if (FTy->getNumParams() < 1 ||
1602 !FTy->getParamType(0)->isPointerTy())
1605 setDoesNotCapture(F, 1);
1606 setOnlyReadsMemory(F, 1);
1608 case LibFunc::stat64:
1609 case LibFunc::lstat64:
1610 case LibFunc::statvfs64:
1611 if (FTy->getNumParams() < 1 ||
1612 !FTy->getParamType(0)->isPointerTy() ||
1613 !FTy->getParamType(1)->isPointerTy())
1616 setDoesNotCapture(F, 1);
1617 setDoesNotCapture(F, 2);
1618 setOnlyReadsMemory(F, 1);
1620 case LibFunc::dunder_isoc99_sscanf:
1621 if (FTy->getNumParams() < 1 ||
1622 !FTy->getParamType(0)->isPointerTy() ||
1623 !FTy->getParamType(1)->isPointerTy())
1626 setDoesNotCapture(F, 1);
1627 setDoesNotCapture(F, 2);
1628 setOnlyReadsMemory(F, 1);
1629 setOnlyReadsMemory(F, 2);
1631 case LibFunc::fopen64:
1632 if (FTy->getNumParams() != 2 ||
1633 !FTy->getReturnType()->isPointerTy() ||
1634 !FTy->getParamType(0)->isPointerTy() ||
1635 !FTy->getParamType(1)->isPointerTy())
1638 setDoesNotAlias(F, 0);
1639 setDoesNotCapture(F, 1);
1640 setDoesNotCapture(F, 2);
1641 setOnlyReadsMemory(F, 1);
1642 setOnlyReadsMemory(F, 2);
1644 case LibFunc::fseeko64:
1645 case LibFunc::ftello64:
1646 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1649 setDoesNotCapture(F, 1);
1651 case LibFunc::tmpfile64:
1652 if (!FTy->getReturnType()->isPointerTy())
1655 setDoesNotAlias(F, 0);
1657 case LibFunc::fstat64:
1658 case LibFunc::fstatvfs64:
1659 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1662 setDoesNotCapture(F, 2);
1664 case LibFunc::open64:
1665 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy())
1667 // May throw; "open" is a valid pthread cancellation point.
1668 setDoesNotCapture(F, 1);
1669 setOnlyReadsMemory(F, 1);
1671 case LibFunc::gettimeofday:
1672 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy() ||
1673 !FTy->getParamType(1)->isPointerTy())
1675 // Currently some platforms have the restrict keyword on the arguments to
1676 // gettimeofday. To be conservative, do not add noalias to gettimeofday's
1679 setDoesNotCapture(F, 1);
1680 setDoesNotCapture(F, 2);
1683 // Didn't mark any attributes.
1690 /// annotateLibraryCalls - Adds attributes to well-known standard library
1691 /// call declarations.
1692 bool FunctionAttrs::annotateLibraryCalls(const CallGraphSCC &SCC) {
1693 bool MadeChange = false;
1695 // Check each function in turn annotating well-known library function
1696 // declarations with attributes.
1697 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
1698 Function *F = (*I)->getFunction();
1700 if (F && F->isDeclaration())
1701 MadeChange |= inferPrototypeAttributes(*F);
1707 bool FunctionAttrs::runOnSCC(CallGraphSCC &SCC) {
1708 AA = &getAnalysis<AliasAnalysis>();
1709 TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
1711 bool Changed = annotateLibraryCalls(SCC);
1712 Changed |= AddReadAttrs(SCC);
1713 Changed |= AddArgumentAttrs(SCC);
1714 Changed |= AddNoAliasAttrs(SCC);