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/ADT/StringSwitch.h"
27 #include "llvm/Analysis/AliasAnalysis.h"
28 #include "llvm/Analysis/AssumptionCache.h"
29 #include "llvm/Analysis/BasicAliasAnalysis.h"
30 #include "llvm/Analysis/CallGraph.h"
31 #include "llvm/Analysis/CallGraphSCCPass.h"
32 #include "llvm/Analysis/CaptureTracking.h"
33 #include "llvm/Analysis/TargetLibraryInfo.h"
34 #include "llvm/Analysis/ValueTracking.h"
35 #include "llvm/IR/GlobalVariable.h"
36 #include "llvm/IR/InstIterator.h"
37 #include "llvm/IR/IntrinsicInst.h"
38 #include "llvm/IR/LLVMContext.h"
39 #include "llvm/Support/Debug.h"
40 #include "llvm/Support/raw_ostream.h"
41 #include "llvm/Analysis/TargetLibraryInfo.h"
44 #define DEBUG_TYPE "functionattrs"
46 STATISTIC(NumReadNone, "Number of functions marked readnone");
47 STATISTIC(NumReadOnly, "Number of functions marked readonly");
48 STATISTIC(NumNoCapture, "Number of arguments marked nocapture");
49 STATISTIC(NumReadNoneArg, "Number of arguments marked readnone");
50 STATISTIC(NumReadOnlyArg, "Number of arguments marked readonly");
51 STATISTIC(NumNoAlias, "Number of function returns marked noalias");
52 STATISTIC(NumNonNullReturn, "Number of function returns marked nonnull");
53 STATISTIC(NumAnnotated, "Number of attributes added to library functions");
54 STATISTIC(NumNoRecurse, "Number of functions marked as norecurse");
57 typedef SmallSetVector<Function *, 8> SCCNodeSet;
61 struct FunctionAttrs : public CallGraphSCCPass {
62 static char ID; // Pass identification, replacement for typeid
63 FunctionAttrs() : CallGraphSCCPass(ID) {
64 initializeFunctionAttrsPass(*PassRegistry::getPassRegistry());
67 bool runOnSCC(CallGraphSCC &SCC) override;
68 bool doInitialization(CallGraph &CG) override {
72 bool doFinalization(CallGraph &CG) override;
74 void getAnalysisUsage(AnalysisUsage &AU) const override {
76 AU.addRequired<AssumptionCacheTracker>();
77 AU.addRequired<TargetLibraryInfoWrapperPass>();
78 CallGraphSCCPass::getAnalysisUsage(AU);
82 TargetLibraryInfo *TLI;
83 SmallVector<WeakVH,16> Revisit;
87 char FunctionAttrs::ID = 0;
88 INITIALIZE_PASS_BEGIN(FunctionAttrs, "functionattrs",
89 "Deduce function attributes", false, false)
90 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
91 INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
92 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
93 INITIALIZE_PASS_END(FunctionAttrs, "functionattrs",
94 "Deduce function attributes", false, false)
96 Pass *llvm::createFunctionAttrsPass() { return new FunctionAttrs(); }
99 /// The three kinds of memory access relevant to 'readonly' and
100 /// 'readnone' attributes.
101 enum MemoryAccessKind {
108 static MemoryAccessKind checkFunctionMemoryAccess(Function &F, AAResults &AAR,
109 const SCCNodeSet &SCCNodes) {
110 FunctionModRefBehavior MRB = AAR.getModRefBehavior(&F);
111 if (MRB == FMRB_DoesNotAccessMemory)
115 // Definitions with weak linkage may be overridden at linktime with
116 // something that writes memory, so treat them like declarations.
117 if (F.isDeclaration() || F.mayBeOverridden()) {
118 if (AliasAnalysis::onlyReadsMemory(MRB))
121 // Conservatively assume it writes to memory.
125 // Scan the function body for instructions that may read or write memory.
126 bool ReadsMemory = false;
127 for (inst_iterator II = inst_begin(F), E = inst_end(F); II != E; ++II) {
128 Instruction *I = &*II;
130 // Some instructions can be ignored even if they read or write memory.
131 // Detect these now, skipping to the next instruction if one is found.
132 CallSite CS(cast<Value>(I));
134 // Ignore calls to functions in the same SCC.
135 if (CS.getCalledFunction() && SCCNodes.count(CS.getCalledFunction()))
137 FunctionModRefBehavior MRB = AAR.getModRefBehavior(CS);
139 // If the call doesn't access memory, we're done.
140 if (!(MRB & MRI_ModRef))
143 if (!AliasAnalysis::onlyAccessesArgPointees(MRB)) {
144 // The call could access any memory. If that includes writes, give up.
147 // If it reads, note it.
153 // Check whether all pointer arguments point to local memory, and
154 // ignore calls that only access local memory.
155 for (CallSite::arg_iterator CI = CS.arg_begin(), CE = CS.arg_end();
158 if (!Arg->getType()->isPtrOrPtrVectorTy())
162 I->getAAMetadata(AAInfo);
163 MemoryLocation Loc(Arg, MemoryLocation::UnknownSize, AAInfo);
165 // Skip accesses to local or constant memory as they don't impact the
166 // externally visible mod/ref behavior.
167 if (AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true))
171 // Writes non-local memory. Give up.
174 // Ok, it reads non-local memory.
178 } else if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
179 // Ignore non-volatile loads from local memory. (Atomic is okay here.)
180 if (!LI->isVolatile()) {
181 MemoryLocation Loc = MemoryLocation::get(LI);
182 if (AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true))
185 } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
186 // Ignore non-volatile stores to local memory. (Atomic is okay here.)
187 if (!SI->isVolatile()) {
188 MemoryLocation Loc = MemoryLocation::get(SI);
189 if (AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true))
192 } else if (VAArgInst *VI = dyn_cast<VAArgInst>(I)) {
193 // Ignore vaargs on local memory.
194 MemoryLocation Loc = MemoryLocation::get(VI);
195 if (AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true))
199 // Any remaining instructions need to be taken seriously! Check if they
200 // read or write memory.
201 if (I->mayWriteToMemory())
202 // Writes memory. Just give up.
205 // If this instruction may read memory, remember that.
206 ReadsMemory |= I->mayReadFromMemory();
209 return ReadsMemory ? MAK_ReadOnly : MAK_ReadNone;
212 /// Deduce readonly/readnone attributes for the SCC.
213 template <typename AARGetterT>
214 static bool addReadAttrs(const SCCNodeSet &SCCNodes, AARGetterT AARGetter) {
215 // Check if any of the functions in the SCC read or write memory. If they
216 // write memory then they can't be marked readnone or readonly.
217 bool ReadsMemory = false;
218 for (Function *F : SCCNodes) {
219 // Call the callable parameter to look up AA results for this function.
220 AAResults &AAR = AARGetter(*F);
222 switch (checkFunctionMemoryAccess(*F, AAR, SCCNodes)) {
234 // Success! Functions in this SCC do not access memory, or only read memory.
235 // Give them the appropriate attribute.
236 bool MadeChange = false;
237 for (Function *F : SCCNodes) {
238 if (F->doesNotAccessMemory())
242 if (F->onlyReadsMemory() && ReadsMemory)
248 // Clear out any existing attributes.
250 B.addAttribute(Attribute::ReadOnly).addAttribute(Attribute::ReadNone);
252 AttributeSet::FunctionIndex,
253 AttributeSet::get(F->getContext(), AttributeSet::FunctionIndex, B));
255 // Add in the new attribute.
256 F->addAttribute(AttributeSet::FunctionIndex,
257 ReadsMemory ? Attribute::ReadOnly : Attribute::ReadNone);
269 /// For a given pointer Argument, this retains a list of Arguments of functions
270 /// in the same SCC that the pointer data flows into. We use this to build an
271 /// SCC of the arguments.
272 struct ArgumentGraphNode {
273 Argument *Definition;
274 SmallVector<ArgumentGraphNode *, 4> Uses;
277 class ArgumentGraph {
278 // We store pointers to ArgumentGraphNode objects, so it's important that
279 // that they not move around upon insert.
280 typedef std::map<Argument *, ArgumentGraphNode> ArgumentMapTy;
282 ArgumentMapTy ArgumentMap;
284 // There is no root node for the argument graph, in fact:
285 // void f(int *x, int *y) { if (...) f(x, y); }
286 // is an example where the graph is disconnected. The SCCIterator requires a
287 // single entry point, so we maintain a fake ("synthetic") root node that
288 // uses every node. Because the graph is directed and nothing points into
289 // the root, it will not participate in any SCCs (except for its own).
290 ArgumentGraphNode SyntheticRoot;
293 ArgumentGraph() { SyntheticRoot.Definition = nullptr; }
295 typedef SmallVectorImpl<ArgumentGraphNode *>::iterator iterator;
297 iterator begin() { return SyntheticRoot.Uses.begin(); }
298 iterator end() { return SyntheticRoot.Uses.end(); }
299 ArgumentGraphNode *getEntryNode() { return &SyntheticRoot; }
301 ArgumentGraphNode *operator[](Argument *A) {
302 ArgumentGraphNode &Node = ArgumentMap[A];
304 SyntheticRoot.Uses.push_back(&Node);
309 /// This tracker checks whether callees are in the SCC, and if so it does not
310 /// consider that a capture, instead adding it to the "Uses" list and
311 /// continuing with the analysis.
312 struct ArgumentUsesTracker : public CaptureTracker {
313 ArgumentUsesTracker(const SCCNodeSet &SCCNodes)
314 : Captured(false), SCCNodes(SCCNodes) {}
316 void tooManyUses() override { Captured = true; }
318 bool captured(const Use *U) override {
319 CallSite CS(U->getUser());
320 if (!CS.getInstruction()) {
325 Function *F = CS.getCalledFunction();
326 if (!F || F->isDeclaration() || F->mayBeOverridden() ||
327 !SCCNodes.count(F)) {
332 // Note: the callee and the two successor blocks *follow* the argument
333 // operands. This means there is no need to adjust UseIndex to account for
337 std::distance(const_cast<const Use *>(CS.arg_begin()), U);
339 assert(UseIndex < CS.data_operands_size() &&
340 "Indirect function calls should have been filtered above!");
342 if (UseIndex >= CS.getNumArgOperands()) {
343 // Data operand, but not a argument operand -- must be a bundle operand
344 assert(CS.hasOperandBundles() && "Must be!");
346 // CaptureTracking told us that we're being captured by an operand bundle
347 // use. In this case it does not matter if the callee is within our SCC
348 // or not -- we've been captured in some unknown way, and we have to be
354 if (UseIndex >= F->arg_size()) {
355 assert(F->isVarArg() && "More params than args in non-varargs call");
360 Uses.push_back(&*std::next(F->arg_begin(), UseIndex));
364 bool Captured; // True only if certainly captured (used outside our SCC).
365 SmallVector<Argument *, 4> Uses; // Uses within our SCC.
367 const SCCNodeSet &SCCNodes;
372 template <> struct GraphTraits<ArgumentGraphNode *> {
373 typedef ArgumentGraphNode NodeType;
374 typedef SmallVectorImpl<ArgumentGraphNode *>::iterator ChildIteratorType;
376 static inline NodeType *getEntryNode(NodeType *A) { return A; }
377 static inline ChildIteratorType child_begin(NodeType *N) {
378 return N->Uses.begin();
380 static inline ChildIteratorType child_end(NodeType *N) {
381 return N->Uses.end();
385 struct GraphTraits<ArgumentGraph *> : public GraphTraits<ArgumentGraphNode *> {
386 static NodeType *getEntryNode(ArgumentGraph *AG) {
387 return AG->getEntryNode();
389 static ChildIteratorType nodes_begin(ArgumentGraph *AG) {
392 static ChildIteratorType nodes_end(ArgumentGraph *AG) { return AG->end(); }
396 /// Returns Attribute::None, Attribute::ReadOnly or Attribute::ReadNone.
397 static Attribute::AttrKind
398 determinePointerReadAttrs(Argument *A,
399 const SmallPtrSet<Argument *, 8> &SCCNodes) {
401 SmallVector<Use *, 32> Worklist;
402 SmallSet<Use *, 32> Visited;
404 // inalloca arguments are always clobbered by the call.
405 if (A->hasInAllocaAttr())
406 return Attribute::None;
409 // We don't need to track IsWritten. If A is written to, return immediately.
411 for (Use &U : A->uses()) {
413 Worklist.push_back(&U);
416 while (!Worklist.empty()) {
417 Use *U = Worklist.pop_back_val();
418 Instruction *I = cast<Instruction>(U->getUser());
420 switch (I->getOpcode()) {
421 case Instruction::BitCast:
422 case Instruction::GetElementPtr:
423 case Instruction::PHI:
424 case Instruction::Select:
425 case Instruction::AddrSpaceCast:
426 // The original value is not read/written via this if the new value isn't.
427 for (Use &UU : I->uses())
428 if (Visited.insert(&UU).second)
429 Worklist.push_back(&UU);
432 case Instruction::Call:
433 case Instruction::Invoke: {
434 bool Captures = true;
436 if (I->getType()->isVoidTy())
439 auto AddUsersToWorklistIfCapturing = [&] {
441 for (Use &UU : I->uses())
442 if (Visited.insert(&UU).second)
443 Worklist.push_back(&UU);
447 if (CS.doesNotAccessMemory()) {
448 AddUsersToWorklistIfCapturing();
452 Function *F = CS.getCalledFunction();
454 if (CS.onlyReadsMemory()) {
456 AddUsersToWorklistIfCapturing();
459 return Attribute::None;
462 // Note: the callee and the two successor blocks *follow* the argument
463 // operands. This means there is no need to adjust UseIndex to account
466 unsigned UseIndex = std::distance(CS.arg_begin(), U);
468 // U cannot be the callee operand use: since we're exploring the
469 // transitive uses of an Argument, having such a use be a callee would
470 // imply the CallSite is an indirect call or invoke; and we'd take the
472 assert(UseIndex < CS.data_operands_size() &&
473 "Data operand use expected!");
475 bool IsOperandBundleUse = UseIndex >= CS.getNumArgOperands();
477 if (UseIndex >= F->arg_size() && !IsOperandBundleUse) {
478 assert(F->isVarArg() && "More params than args in non-varargs call");
479 return Attribute::None;
482 Captures &= !CS.doesNotCapture(UseIndex);
484 // Since the optimizer (by design) cannot see the data flow corresponding
485 // to a operand bundle use, these cannot participate in the optimistic SCC
486 // analysis. Instead, we model the operand bundle uses as arguments in
487 // call to a function external to the SCC.
488 if (!SCCNodes.count(&*std::next(F->arg_begin(), UseIndex)) ||
489 IsOperandBundleUse) {
491 // The accessors used on CallSite here do the right thing for calls and
492 // invokes with operand bundles.
494 if (!CS.onlyReadsMemory() && !CS.onlyReadsMemory(UseIndex))
495 return Attribute::None;
496 if (!CS.doesNotAccessMemory(UseIndex))
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 /// Deduce nocapture attributes for the SCC.
521 static bool addArgumentAttrs(const SCCNodeSet &SCCNodes) {
522 bool Changed = false;
527 B.addAttribute(Attribute::NoCapture);
529 // Check each function in turn, determining which pointer arguments are not
531 for (Function *F : SCCNodes) {
532 // Definitions with weak linkage may be overridden at linktime with
533 // something that captures pointers, so treat them like declarations.
534 if (F->isDeclaration() || F->mayBeOverridden())
537 // Functions that are readonly (or readnone) and nounwind and don't return
538 // a value can't capture arguments. Don't analyze them.
539 if (F->onlyReadsMemory() && F->doesNotThrow() &&
540 F->getReturnType()->isVoidTy()) {
541 for (Function::arg_iterator A = F->arg_begin(), E = F->arg_end(); A != E;
543 if (A->getType()->isPointerTy() && !A->hasNoCaptureAttr()) {
544 A->addAttr(AttributeSet::get(F->getContext(), A->getArgNo() + 1, B));
552 for (Function::arg_iterator A = F->arg_begin(), E = F->arg_end(); A != E;
554 if (!A->getType()->isPointerTy())
556 bool HasNonLocalUses = false;
557 if (!A->hasNoCaptureAttr()) {
558 ArgumentUsesTracker Tracker(SCCNodes);
559 PointerMayBeCaptured(&*A, &Tracker);
560 if (!Tracker.Captured) {
561 if (Tracker.Uses.empty()) {
562 // If it's trivially not captured, mark it nocapture now.
564 AttributeSet::get(F->getContext(), A->getArgNo() + 1, B));
568 // If it's not trivially captured and not trivially not captured,
569 // then it must be calling into another function in our SCC. Save
570 // its particulars for Argument-SCC analysis later.
571 ArgumentGraphNode *Node = AG[&*A];
572 for (SmallVectorImpl<Argument *>::iterator
573 UI = Tracker.Uses.begin(),
574 UE = Tracker.Uses.end();
576 Node->Uses.push_back(AG[*UI]);
578 HasNonLocalUses = true;
582 // Otherwise, it's captured. Don't bother doing SCC analysis on it.
584 if (!HasNonLocalUses && !A->onlyReadsMemory()) {
585 // Can we determine that it's readonly/readnone without doing an SCC?
586 // Note that we don't allow any calls at all here, or else our result
587 // will be dependent on the iteration order through the functions in the
589 SmallPtrSet<Argument *, 8> Self;
591 Attribute::AttrKind R = determinePointerReadAttrs(&*A, Self);
592 if (R != Attribute::None) {
595 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
597 R == Attribute::ReadOnly ? ++NumReadOnlyArg : ++NumReadNoneArg;
603 // The graph we've collected is partial because we stopped scanning for
604 // argument uses once we solved the argument trivially. These partial nodes
605 // show up as ArgumentGraphNode objects with an empty Uses list, and for
606 // these nodes the final decision about whether they capture has already been
607 // made. If the definition doesn't have a 'nocapture' attribute by now, it
610 for (scc_iterator<ArgumentGraph *> I = scc_begin(&AG); !I.isAtEnd(); ++I) {
611 const std::vector<ArgumentGraphNode *> &ArgumentSCC = *I;
612 if (ArgumentSCC.size() == 1) {
613 if (!ArgumentSCC[0]->Definition)
614 continue; // synthetic root node
616 // eg. "void f(int* x) { if (...) f(x); }"
617 if (ArgumentSCC[0]->Uses.size() == 1 &&
618 ArgumentSCC[0]->Uses[0] == ArgumentSCC[0]) {
619 Argument *A = ArgumentSCC[0]->Definition;
620 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
627 bool SCCCaptured = false;
628 for (auto I = ArgumentSCC.begin(), E = ArgumentSCC.end();
629 I != E && !SCCCaptured; ++I) {
630 ArgumentGraphNode *Node = *I;
631 if (Node->Uses.empty()) {
632 if (!Node->Definition->hasNoCaptureAttr())
639 SmallPtrSet<Argument *, 8> ArgumentSCCNodes;
640 // Fill ArgumentSCCNodes with the elements of the ArgumentSCC. Used for
641 // quickly looking up whether a given Argument is in this ArgumentSCC.
642 for (auto I = ArgumentSCC.begin(), E = ArgumentSCC.end(); I != E; ++I) {
643 ArgumentSCCNodes.insert((*I)->Definition);
646 for (auto I = ArgumentSCC.begin(), E = ArgumentSCC.end();
647 I != E && !SCCCaptured; ++I) {
648 ArgumentGraphNode *N = *I;
649 for (SmallVectorImpl<ArgumentGraphNode *>::iterator UI = N->Uses.begin(),
652 Argument *A = (*UI)->Definition;
653 if (A->hasNoCaptureAttr() || ArgumentSCCNodes.count(A))
662 for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) {
663 Argument *A = ArgumentSCC[i]->Definition;
664 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
669 // We also want to compute readonly/readnone. With a small number of false
670 // negatives, we can assume that any pointer which is captured isn't going
671 // to be provably readonly or readnone, since by definition we can't
672 // analyze all uses of a captured pointer.
674 // The false negatives happen when the pointer is captured by a function
675 // that promises readonly/readnone behaviour on the pointer, then the
676 // pointer's lifetime ends before anything that writes to arbitrary memory.
677 // Also, a readonly/readnone pointer may be returned, but returning a
678 // pointer is capturing it.
680 Attribute::AttrKind ReadAttr = Attribute::ReadNone;
681 for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) {
682 Argument *A = ArgumentSCC[i]->Definition;
683 Attribute::AttrKind K = determinePointerReadAttrs(A, ArgumentSCCNodes);
684 if (K == Attribute::ReadNone)
686 if (K == Attribute::ReadOnly) {
687 ReadAttr = Attribute::ReadOnly;
694 if (ReadAttr != Attribute::None) {
696 B.addAttribute(ReadAttr);
697 R.addAttribute(Attribute::ReadOnly).addAttribute(Attribute::ReadNone);
698 for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) {
699 Argument *A = ArgumentSCC[i]->Definition;
700 // Clear out existing readonly/readnone attributes
701 A->removeAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, R));
702 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
703 ReadAttr == Attribute::ReadOnly ? ++NumReadOnlyArg : ++NumReadNoneArg;
712 /// Tests whether a function is "malloc-like".
714 /// A function is "malloc-like" if it returns either null or a pointer that
715 /// doesn't alias any other pointer visible to the caller.
716 static bool isFunctionMallocLike(Function *F, const SCCNodeSet &SCCNodes) {
717 SmallSetVector<Value *, 8> FlowsToReturn;
718 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I)
719 if (ReturnInst *Ret = dyn_cast<ReturnInst>(I->getTerminator()))
720 FlowsToReturn.insert(Ret->getReturnValue());
722 for (unsigned i = 0; i != FlowsToReturn.size(); ++i) {
723 Value *RetVal = FlowsToReturn[i];
725 if (Constant *C = dyn_cast<Constant>(RetVal)) {
726 if (!C->isNullValue() && !isa<UndefValue>(C))
732 if (isa<Argument>(RetVal))
735 if (Instruction *RVI = dyn_cast<Instruction>(RetVal))
736 switch (RVI->getOpcode()) {
737 // Extend the analysis by looking upwards.
738 case Instruction::BitCast:
739 case Instruction::GetElementPtr:
740 case Instruction::AddrSpaceCast:
741 FlowsToReturn.insert(RVI->getOperand(0));
743 case Instruction::Select: {
744 SelectInst *SI = cast<SelectInst>(RVI);
745 FlowsToReturn.insert(SI->getTrueValue());
746 FlowsToReturn.insert(SI->getFalseValue());
749 case Instruction::PHI: {
750 PHINode *PN = cast<PHINode>(RVI);
751 for (Value *IncValue : PN->incoming_values())
752 FlowsToReturn.insert(IncValue);
756 // Check whether the pointer came from an allocation.
757 case Instruction::Alloca:
759 case Instruction::Call:
760 case Instruction::Invoke: {
762 if (CS.paramHasAttr(0, Attribute::NoAlias))
764 if (CS.getCalledFunction() && SCCNodes.count(CS.getCalledFunction()))
768 return false; // Did not come from an allocation.
771 if (PointerMayBeCaptured(RetVal, false, /*StoreCaptures=*/false))
778 /// Deduce noalias attributes for the SCC.
779 static bool addNoAliasAttrs(const SCCNodeSet &SCCNodes) {
780 // Check each function in turn, determining which functions return noalias
782 for (Function *F : SCCNodes) {
784 if (F->doesNotAlias(0))
787 // Definitions with weak linkage may be overridden at linktime, so
788 // treat them like declarations.
789 if (F->isDeclaration() || F->mayBeOverridden())
792 // We annotate noalias return values, which are only applicable to
794 if (!F->getReturnType()->isPointerTy())
797 if (!isFunctionMallocLike(F, SCCNodes))
801 bool MadeChange = false;
802 for (Function *F : SCCNodes) {
803 if (F->doesNotAlias(0) || !F->getReturnType()->isPointerTy())
806 F->setDoesNotAlias(0);
814 /// Tests whether this function is known to not return null.
816 /// Requires that the function returns a pointer.
818 /// Returns true if it believes the function will not return a null, and sets
819 /// \p Speculative based on whether the returned conclusion is a speculative
820 /// conclusion due to SCC calls.
821 static bool isReturnNonNull(Function *F, const SCCNodeSet &SCCNodes,
822 const TargetLibraryInfo &TLI, bool &Speculative) {
823 assert(F->getReturnType()->isPointerTy() &&
824 "nonnull only meaningful on pointer types");
827 SmallSetVector<Value *, 8> FlowsToReturn;
828 for (BasicBlock &BB : *F)
829 if (auto *Ret = dyn_cast<ReturnInst>(BB.getTerminator()))
830 FlowsToReturn.insert(Ret->getReturnValue());
832 for (unsigned i = 0; i != FlowsToReturn.size(); ++i) {
833 Value *RetVal = FlowsToReturn[i];
835 // If this value is locally known to be non-null, we're good
836 if (isKnownNonNull(RetVal, &TLI))
839 // Otherwise, we need to look upwards since we can't make any local
841 Instruction *RVI = dyn_cast<Instruction>(RetVal);
844 switch (RVI->getOpcode()) {
845 // Extend the analysis by looking upwards.
846 case Instruction::BitCast:
847 case Instruction::GetElementPtr:
848 case Instruction::AddrSpaceCast:
849 FlowsToReturn.insert(RVI->getOperand(0));
851 case Instruction::Select: {
852 SelectInst *SI = cast<SelectInst>(RVI);
853 FlowsToReturn.insert(SI->getTrueValue());
854 FlowsToReturn.insert(SI->getFalseValue());
857 case Instruction::PHI: {
858 PHINode *PN = cast<PHINode>(RVI);
859 for (int i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
860 FlowsToReturn.insert(PN->getIncomingValue(i));
863 case Instruction::Call:
864 case Instruction::Invoke: {
866 Function *Callee = CS.getCalledFunction();
867 // A call to a node within the SCC is assumed to return null until
869 if (Callee && SCCNodes.count(Callee)) {
876 return false; // Unknown source, may be null
878 llvm_unreachable("should have either continued or returned");
884 /// Deduce nonnull attributes for the SCC.
885 static bool addNonNullAttrs(const SCCNodeSet &SCCNodes,
886 const TargetLibraryInfo &TLI) {
887 // Speculative that all functions in the SCC return only nonnull
888 // pointers. We may refute this as we analyze functions.
889 bool SCCReturnsNonNull = true;
891 bool MadeChange = false;
893 // Check each function in turn, determining which functions return nonnull
895 for (Function *F : SCCNodes) {
897 if (F->getAttributes().hasAttribute(AttributeSet::ReturnIndex,
901 // Definitions with weak linkage may be overridden at linktime, so
902 // treat them like declarations.
903 if (F->isDeclaration() || F->mayBeOverridden())
906 // We annotate nonnull return values, which are only applicable to
908 if (!F->getReturnType()->isPointerTy())
911 bool Speculative = false;
912 if (isReturnNonNull(F, SCCNodes, TLI, Speculative)) {
914 // Mark the function eagerly since we may discover a function
915 // which prevents us from speculating about the entire SCC
916 DEBUG(dbgs() << "Eagerly marking " << F->getName() << " as nonnull\n");
917 F->addAttribute(AttributeSet::ReturnIndex, Attribute::NonNull);
923 // At least one function returns something which could be null, can't
924 // speculate any more.
925 SCCReturnsNonNull = false;
928 if (SCCReturnsNonNull) {
929 for (Function *F : SCCNodes) {
930 if (F->getAttributes().hasAttribute(AttributeSet::ReturnIndex,
931 Attribute::NonNull) ||
932 !F->getReturnType()->isPointerTy())
935 DEBUG(dbgs() << "SCC marking " << F->getName() << " as nonnull\n");
936 F->addAttribute(AttributeSet::ReturnIndex, Attribute::NonNull);
945 static void setDoesNotAccessMemory(Function &F) {
946 if (!F.doesNotAccessMemory()) {
947 F.setDoesNotAccessMemory();
952 static void setOnlyReadsMemory(Function &F) {
953 if (!F.onlyReadsMemory()) {
954 F.setOnlyReadsMemory();
959 static void setDoesNotThrow(Function &F) {
960 if (!F.doesNotThrow()) {
966 static void setDoesNotCapture(Function &F, unsigned n) {
967 if (!F.doesNotCapture(n)) {
968 F.setDoesNotCapture(n);
973 static void setOnlyReadsMemory(Function &F, unsigned n) {
974 if (!F.onlyReadsMemory(n)) {
975 F.setOnlyReadsMemory(n);
980 static void setDoesNotAlias(Function &F, unsigned n) {
981 if (!F.doesNotAlias(n)) {
982 F.setDoesNotAlias(n);
987 static bool setDoesNotRecurse(Function &F) {
988 if (F.doesNotRecurse())
990 F.setDoesNotRecurse();
995 /// Analyze the name and prototype of the given function and set any applicable
998 /// Returns true if any attributes were set and false otherwise.
999 static bool inferPrototypeAttributes(Function &F, const TargetLibraryInfo &TLI) {
1000 if (F.hasFnAttribute(Attribute::OptimizeNone))
1003 FunctionType *FTy = F.getFunctionType();
1004 LibFunc::Func TheLibFunc;
1005 if (!(TLI.getLibFunc(F.getName(), TheLibFunc) && TLI.has(TheLibFunc)))
1008 switch (TheLibFunc) {
1009 case LibFunc::strlen:
1010 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1012 setOnlyReadsMemory(F);
1014 setDoesNotCapture(F, 1);
1016 case LibFunc::strchr:
1017 case LibFunc::strrchr:
1018 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy() ||
1019 !FTy->getParamType(1)->isIntegerTy())
1021 setOnlyReadsMemory(F);
1024 case LibFunc::strtol:
1025 case LibFunc::strtod:
1026 case LibFunc::strtof:
1027 case LibFunc::strtoul:
1028 case LibFunc::strtoll:
1029 case LibFunc::strtold:
1030 case LibFunc::strtoull:
1031 if (FTy->getNumParams() < 2 || !FTy->getParamType(1)->isPointerTy())
1034 setDoesNotCapture(F, 2);
1035 setOnlyReadsMemory(F, 1);
1037 case LibFunc::strcpy:
1038 case LibFunc::stpcpy:
1039 case LibFunc::strcat:
1040 case LibFunc::strncat:
1041 case LibFunc::strncpy:
1042 case LibFunc::stpncpy:
1043 if (FTy->getNumParams() < 2 || !FTy->getParamType(1)->isPointerTy())
1046 setDoesNotCapture(F, 2);
1047 setOnlyReadsMemory(F, 2);
1049 case LibFunc::strxfrm:
1050 if (FTy->getNumParams() != 3 || !FTy->getParamType(0)->isPointerTy() ||
1051 !FTy->getParamType(1)->isPointerTy())
1054 setDoesNotCapture(F, 1);
1055 setDoesNotCapture(F, 2);
1056 setOnlyReadsMemory(F, 2);
1058 case LibFunc::strcmp: // 0,1
1059 case LibFunc::strspn: // 0,1
1060 case LibFunc::strncmp: // 0,1
1061 case LibFunc::strcspn: // 0,1
1062 case LibFunc::strcoll: // 0,1
1063 case LibFunc::strcasecmp: // 0,1
1064 case LibFunc::strncasecmp: //
1065 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy() ||
1066 !FTy->getParamType(1)->isPointerTy())
1068 setOnlyReadsMemory(F);
1070 setDoesNotCapture(F, 1);
1071 setDoesNotCapture(F, 2);
1073 case LibFunc::strstr:
1074 case LibFunc::strpbrk:
1075 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1077 setOnlyReadsMemory(F);
1079 setDoesNotCapture(F, 2);
1081 case LibFunc::strtok:
1082 case LibFunc::strtok_r:
1083 if (FTy->getNumParams() < 2 || !FTy->getParamType(1)->isPointerTy())
1086 setDoesNotCapture(F, 2);
1087 setOnlyReadsMemory(F, 2);
1089 case LibFunc::scanf:
1090 if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy())
1093 setDoesNotCapture(F, 1);
1094 setOnlyReadsMemory(F, 1);
1096 case LibFunc::setbuf:
1097 case LibFunc::setvbuf:
1098 if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy())
1101 setDoesNotCapture(F, 1);
1103 case LibFunc::strdup:
1104 case LibFunc::strndup:
1105 if (FTy->getNumParams() < 1 || !FTy->getReturnType()->isPointerTy() ||
1106 !FTy->getParamType(0)->isPointerTy())
1109 setDoesNotAlias(F, 0);
1110 setDoesNotCapture(F, 1);
1111 setOnlyReadsMemory(F, 1);
1114 case LibFunc::statvfs:
1115 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy() ||
1116 !FTy->getParamType(1)->isPointerTy())
1119 setDoesNotCapture(F, 1);
1120 setDoesNotCapture(F, 2);
1121 setOnlyReadsMemory(F, 1);
1123 case LibFunc::sscanf:
1124 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy() ||
1125 !FTy->getParamType(1)->isPointerTy())
1128 setDoesNotCapture(F, 1);
1129 setDoesNotCapture(F, 2);
1130 setOnlyReadsMemory(F, 1);
1131 setOnlyReadsMemory(F, 2);
1133 case LibFunc::sprintf:
1134 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy() ||
1135 !FTy->getParamType(1)->isPointerTy())
1138 setDoesNotCapture(F, 1);
1139 setDoesNotCapture(F, 2);
1140 setOnlyReadsMemory(F, 2);
1142 case LibFunc::snprintf:
1143 if (FTy->getNumParams() != 3 || !FTy->getParamType(0)->isPointerTy() ||
1144 !FTy->getParamType(2)->isPointerTy())
1147 setDoesNotCapture(F, 1);
1148 setDoesNotCapture(F, 3);
1149 setOnlyReadsMemory(F, 3);
1151 case LibFunc::setitimer:
1152 if (FTy->getNumParams() != 3 || !FTy->getParamType(1)->isPointerTy() ||
1153 !FTy->getParamType(2)->isPointerTy())
1156 setDoesNotCapture(F, 2);
1157 setDoesNotCapture(F, 3);
1158 setOnlyReadsMemory(F, 2);
1160 case LibFunc::system:
1161 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1163 // May throw; "system" is a valid pthread cancellation point.
1164 setDoesNotCapture(F, 1);
1165 setOnlyReadsMemory(F, 1);
1167 case LibFunc::malloc:
1168 if (FTy->getNumParams() != 1 || !FTy->getReturnType()->isPointerTy())
1171 setDoesNotAlias(F, 0);
1173 case LibFunc::memcmp:
1174 if (FTy->getNumParams() != 3 || !FTy->getParamType(0)->isPointerTy() ||
1175 !FTy->getParamType(1)->isPointerTy())
1177 setOnlyReadsMemory(F);
1179 setDoesNotCapture(F, 1);
1180 setDoesNotCapture(F, 2);
1182 case LibFunc::memchr:
1183 case LibFunc::memrchr:
1184 if (FTy->getNumParams() != 3)
1186 setOnlyReadsMemory(F);
1190 case LibFunc::modff:
1191 case LibFunc::modfl:
1192 if (FTy->getNumParams() < 2 || !FTy->getParamType(1)->isPointerTy())
1195 setDoesNotCapture(F, 2);
1197 case LibFunc::memcpy:
1198 case LibFunc::memccpy:
1199 case LibFunc::memmove:
1200 if (FTy->getNumParams() < 2 || !FTy->getParamType(1)->isPointerTy())
1203 setDoesNotCapture(F, 2);
1204 setOnlyReadsMemory(F, 2);
1206 case LibFunc::memalign:
1207 if (!FTy->getReturnType()->isPointerTy())
1209 setDoesNotAlias(F, 0);
1211 case LibFunc::mkdir:
1212 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1215 setDoesNotCapture(F, 1);
1216 setOnlyReadsMemory(F, 1);
1218 case LibFunc::mktime:
1219 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1222 setDoesNotCapture(F, 1);
1224 case LibFunc::realloc:
1225 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy() ||
1226 !FTy->getReturnType()->isPointerTy())
1229 setDoesNotAlias(F, 0);
1230 setDoesNotCapture(F, 1);
1233 if (FTy->getNumParams() != 3 || !FTy->getParamType(1)->isPointerTy())
1235 // May throw; "read" is a valid pthread cancellation point.
1236 setDoesNotCapture(F, 2);
1238 case LibFunc::rewind:
1239 if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy())
1242 setDoesNotCapture(F, 1);
1244 case LibFunc::rmdir:
1245 case LibFunc::remove:
1246 case LibFunc::realpath:
1247 if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy())
1250 setDoesNotCapture(F, 1);
1251 setOnlyReadsMemory(F, 1);
1253 case LibFunc::rename:
1254 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy() ||
1255 !FTy->getParamType(1)->isPointerTy())
1258 setDoesNotCapture(F, 1);
1259 setDoesNotCapture(F, 2);
1260 setOnlyReadsMemory(F, 1);
1261 setOnlyReadsMemory(F, 2);
1263 case LibFunc::readlink:
1264 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy() ||
1265 !FTy->getParamType(1)->isPointerTy())
1268 setDoesNotCapture(F, 1);
1269 setDoesNotCapture(F, 2);
1270 setOnlyReadsMemory(F, 1);
1272 case LibFunc::write:
1273 if (FTy->getNumParams() != 3 || !FTy->getParamType(1)->isPointerTy())
1275 // May throw; "write" is a valid pthread cancellation point.
1276 setDoesNotCapture(F, 2);
1277 setOnlyReadsMemory(F, 2);
1279 case LibFunc::bcopy:
1280 if (FTy->getNumParams() != 3 || !FTy->getParamType(0)->isPointerTy() ||
1281 !FTy->getParamType(1)->isPointerTy())
1284 setDoesNotCapture(F, 1);
1285 setDoesNotCapture(F, 2);
1286 setOnlyReadsMemory(F, 1);
1289 if (FTy->getNumParams() != 3 || !FTy->getParamType(0)->isPointerTy() ||
1290 !FTy->getParamType(1)->isPointerTy())
1293 setOnlyReadsMemory(F);
1294 setDoesNotCapture(F, 1);
1295 setDoesNotCapture(F, 2);
1297 case LibFunc::bzero:
1298 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy())
1301 setDoesNotCapture(F, 1);
1303 case LibFunc::calloc:
1304 if (FTy->getNumParams() != 2 || !FTy->getReturnType()->isPointerTy())
1307 setDoesNotAlias(F, 0);
1309 case LibFunc::chmod:
1310 case LibFunc::chown:
1311 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1314 setDoesNotCapture(F, 1);
1315 setOnlyReadsMemory(F, 1);
1317 case LibFunc::ctermid:
1318 case LibFunc::clearerr:
1319 case LibFunc::closedir:
1320 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1323 setDoesNotCapture(F, 1);
1328 case LibFunc::atoll:
1329 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1332 setOnlyReadsMemory(F);
1333 setDoesNotCapture(F, 1);
1335 case LibFunc::access:
1336 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy())
1339 setDoesNotCapture(F, 1);
1340 setOnlyReadsMemory(F, 1);
1342 case LibFunc::fopen:
1343 if (FTy->getNumParams() != 2 || !FTy->getReturnType()->isPointerTy() ||
1344 !FTy->getParamType(0)->isPointerTy() ||
1345 !FTy->getParamType(1)->isPointerTy())
1348 setDoesNotAlias(F, 0);
1349 setDoesNotCapture(F, 1);
1350 setDoesNotCapture(F, 2);
1351 setOnlyReadsMemory(F, 1);
1352 setOnlyReadsMemory(F, 2);
1354 case LibFunc::fdopen:
1355 if (FTy->getNumParams() != 2 || !FTy->getReturnType()->isPointerTy() ||
1356 !FTy->getParamType(1)->isPointerTy())
1359 setDoesNotAlias(F, 0);
1360 setDoesNotCapture(F, 2);
1361 setOnlyReadsMemory(F, 2);
1365 case LibFunc::fseek:
1366 case LibFunc::ftell:
1367 case LibFunc::fgetc:
1368 case LibFunc::fseeko:
1369 case LibFunc::ftello:
1370 case LibFunc::fileno:
1371 case LibFunc::fflush:
1372 case LibFunc::fclose:
1373 case LibFunc::fsetpos:
1374 case LibFunc::flockfile:
1375 case LibFunc::funlockfile:
1376 case LibFunc::ftrylockfile:
1377 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1380 setDoesNotCapture(F, 1);
1382 case LibFunc::ferror:
1383 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1386 setDoesNotCapture(F, 1);
1387 setOnlyReadsMemory(F);
1389 case LibFunc::fputc:
1390 case LibFunc::fstat:
1391 case LibFunc::frexp:
1392 case LibFunc::frexpf:
1393 case LibFunc::frexpl:
1394 case LibFunc::fstatvfs:
1395 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1398 setDoesNotCapture(F, 2);
1400 case LibFunc::fgets:
1401 if (FTy->getNumParams() != 3 || !FTy->getParamType(0)->isPointerTy() ||
1402 !FTy->getParamType(2)->isPointerTy())
1405 setDoesNotCapture(F, 3);
1407 case LibFunc::fread:
1408 if (FTy->getNumParams() != 4 || !FTy->getParamType(0)->isPointerTy() ||
1409 !FTy->getParamType(3)->isPointerTy())
1412 setDoesNotCapture(F, 1);
1413 setDoesNotCapture(F, 4);
1415 case LibFunc::fwrite:
1416 if (FTy->getNumParams() != 4 || !FTy->getParamType(0)->isPointerTy() ||
1417 !FTy->getParamType(3)->isPointerTy())
1420 setDoesNotCapture(F, 1);
1421 setDoesNotCapture(F, 4);
1423 case LibFunc::fputs:
1424 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy() ||
1425 !FTy->getParamType(1)->isPointerTy())
1428 setDoesNotCapture(F, 1);
1429 setDoesNotCapture(F, 2);
1430 setOnlyReadsMemory(F, 1);
1432 case LibFunc::fscanf:
1433 case LibFunc::fprintf:
1434 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy() ||
1435 !FTy->getParamType(1)->isPointerTy())
1438 setDoesNotCapture(F, 1);
1439 setDoesNotCapture(F, 2);
1440 setOnlyReadsMemory(F, 2);
1442 case LibFunc::fgetpos:
1443 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy() ||
1444 !FTy->getParamType(1)->isPointerTy())
1447 setDoesNotCapture(F, 1);
1448 setDoesNotCapture(F, 2);
1451 case LibFunc::getlogin_r:
1452 case LibFunc::getc_unlocked:
1453 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1456 setDoesNotCapture(F, 1);
1458 case LibFunc::getenv:
1459 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1462 setOnlyReadsMemory(F);
1463 setDoesNotCapture(F, 1);
1466 case LibFunc::getchar:
1469 case LibFunc::getitimer:
1470 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1473 setDoesNotCapture(F, 2);
1475 case LibFunc::getpwnam:
1476 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1479 setDoesNotCapture(F, 1);
1480 setOnlyReadsMemory(F, 1);
1482 case LibFunc::ungetc:
1483 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1486 setDoesNotCapture(F, 2);
1488 case LibFunc::uname:
1489 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1492 setDoesNotCapture(F, 1);
1494 case LibFunc::unlink:
1495 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1498 setDoesNotCapture(F, 1);
1499 setOnlyReadsMemory(F, 1);
1501 case LibFunc::unsetenv:
1502 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1505 setDoesNotCapture(F, 1);
1506 setOnlyReadsMemory(F, 1);
1508 case LibFunc::utime:
1509 case LibFunc::utimes:
1510 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy() ||
1511 !FTy->getParamType(1)->isPointerTy())
1514 setDoesNotCapture(F, 1);
1515 setDoesNotCapture(F, 2);
1516 setOnlyReadsMemory(F, 1);
1517 setOnlyReadsMemory(F, 2);
1520 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1523 setDoesNotCapture(F, 2);
1526 case LibFunc::printf:
1527 case LibFunc::perror:
1528 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1531 setDoesNotCapture(F, 1);
1532 setOnlyReadsMemory(F, 1);
1534 case LibFunc::pread:
1535 if (FTy->getNumParams() != 4 || !FTy->getParamType(1)->isPointerTy())
1537 // May throw; "pread" is a valid pthread cancellation point.
1538 setDoesNotCapture(F, 2);
1540 case LibFunc::pwrite:
1541 if (FTy->getNumParams() != 4 || !FTy->getParamType(1)->isPointerTy())
1543 // May throw; "pwrite" is a valid pthread cancellation point.
1544 setDoesNotCapture(F, 2);
1545 setOnlyReadsMemory(F, 2);
1547 case LibFunc::putchar:
1550 case LibFunc::popen:
1551 if (FTy->getNumParams() != 2 || !FTy->getReturnType()->isPointerTy() ||
1552 !FTy->getParamType(0)->isPointerTy() ||
1553 !FTy->getParamType(1)->isPointerTy())
1556 setDoesNotAlias(F, 0);
1557 setDoesNotCapture(F, 1);
1558 setDoesNotCapture(F, 2);
1559 setOnlyReadsMemory(F, 1);
1560 setOnlyReadsMemory(F, 2);
1562 case LibFunc::pclose:
1563 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1566 setDoesNotCapture(F, 1);
1568 case LibFunc::vscanf:
1569 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1572 setDoesNotCapture(F, 1);
1573 setOnlyReadsMemory(F, 1);
1575 case LibFunc::vsscanf:
1576 if (FTy->getNumParams() != 3 || !FTy->getParamType(1)->isPointerTy() ||
1577 !FTy->getParamType(2)->isPointerTy())
1580 setDoesNotCapture(F, 1);
1581 setDoesNotCapture(F, 2);
1582 setOnlyReadsMemory(F, 1);
1583 setOnlyReadsMemory(F, 2);
1585 case LibFunc::vfscanf:
1586 if (FTy->getNumParams() != 3 || !FTy->getParamType(1)->isPointerTy() ||
1587 !FTy->getParamType(2)->isPointerTy())
1590 setDoesNotCapture(F, 1);
1591 setDoesNotCapture(F, 2);
1592 setOnlyReadsMemory(F, 2);
1594 case LibFunc::valloc:
1595 if (!FTy->getReturnType()->isPointerTy())
1598 setDoesNotAlias(F, 0);
1600 case LibFunc::vprintf:
1601 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy())
1604 setDoesNotCapture(F, 1);
1605 setOnlyReadsMemory(F, 1);
1607 case LibFunc::vfprintf:
1608 case LibFunc::vsprintf:
1609 if (FTy->getNumParams() != 3 || !FTy->getParamType(0)->isPointerTy() ||
1610 !FTy->getParamType(1)->isPointerTy())
1613 setDoesNotCapture(F, 1);
1614 setDoesNotCapture(F, 2);
1615 setOnlyReadsMemory(F, 2);
1617 case LibFunc::vsnprintf:
1618 if (FTy->getNumParams() != 4 || !FTy->getParamType(0)->isPointerTy() ||
1619 !FTy->getParamType(2)->isPointerTy())
1622 setDoesNotCapture(F, 1);
1623 setDoesNotCapture(F, 3);
1624 setOnlyReadsMemory(F, 3);
1627 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy())
1629 // May throw; "open" is a valid pthread cancellation point.
1630 setDoesNotCapture(F, 1);
1631 setOnlyReadsMemory(F, 1);
1633 case LibFunc::opendir:
1634 if (FTy->getNumParams() != 1 || !FTy->getReturnType()->isPointerTy() ||
1635 !FTy->getParamType(0)->isPointerTy())
1638 setDoesNotAlias(F, 0);
1639 setDoesNotCapture(F, 1);
1640 setOnlyReadsMemory(F, 1);
1642 case LibFunc::tmpfile:
1643 if (!FTy->getReturnType()->isPointerTy())
1646 setDoesNotAlias(F, 0);
1648 case LibFunc::times:
1649 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1652 setDoesNotCapture(F, 1);
1654 case LibFunc::htonl:
1655 case LibFunc::htons:
1656 case LibFunc::ntohl:
1657 case LibFunc::ntohs:
1659 setDoesNotAccessMemory(F);
1661 case LibFunc::lstat:
1662 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy() ||
1663 !FTy->getParamType(1)->isPointerTy())
1666 setDoesNotCapture(F, 1);
1667 setDoesNotCapture(F, 2);
1668 setOnlyReadsMemory(F, 1);
1670 case LibFunc::lchown:
1671 if (FTy->getNumParams() != 3 || !FTy->getParamType(0)->isPointerTy())
1674 setDoesNotCapture(F, 1);
1675 setOnlyReadsMemory(F, 1);
1677 case LibFunc::qsort:
1678 if (FTy->getNumParams() != 4 || !FTy->getParamType(3)->isPointerTy())
1680 // May throw; places call through function pointer.
1681 setDoesNotCapture(F, 4);
1683 case LibFunc::dunder_strdup:
1684 case LibFunc::dunder_strndup:
1685 if (FTy->getNumParams() < 1 || !FTy->getReturnType()->isPointerTy() ||
1686 !FTy->getParamType(0)->isPointerTy())
1689 setDoesNotAlias(F, 0);
1690 setDoesNotCapture(F, 1);
1691 setOnlyReadsMemory(F, 1);
1693 case LibFunc::dunder_strtok_r:
1694 if (FTy->getNumParams() != 3 || !FTy->getParamType(1)->isPointerTy())
1697 setDoesNotCapture(F, 2);
1698 setOnlyReadsMemory(F, 2);
1700 case LibFunc::under_IO_getc:
1701 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1704 setDoesNotCapture(F, 1);
1706 case LibFunc::under_IO_putc:
1707 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1710 setDoesNotCapture(F, 2);
1712 case LibFunc::dunder_isoc99_scanf:
1713 if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy())
1716 setDoesNotCapture(F, 1);
1717 setOnlyReadsMemory(F, 1);
1719 case LibFunc::stat64:
1720 case LibFunc::lstat64:
1721 case LibFunc::statvfs64:
1722 if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy() ||
1723 !FTy->getParamType(1)->isPointerTy())
1726 setDoesNotCapture(F, 1);
1727 setDoesNotCapture(F, 2);
1728 setOnlyReadsMemory(F, 1);
1730 case LibFunc::dunder_isoc99_sscanf:
1731 if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy() ||
1732 !FTy->getParamType(1)->isPointerTy())
1735 setDoesNotCapture(F, 1);
1736 setDoesNotCapture(F, 2);
1737 setOnlyReadsMemory(F, 1);
1738 setOnlyReadsMemory(F, 2);
1740 case LibFunc::fopen64:
1741 if (FTy->getNumParams() != 2 || !FTy->getReturnType()->isPointerTy() ||
1742 !FTy->getParamType(0)->isPointerTy() ||
1743 !FTy->getParamType(1)->isPointerTy())
1746 setDoesNotAlias(F, 0);
1747 setDoesNotCapture(F, 1);
1748 setDoesNotCapture(F, 2);
1749 setOnlyReadsMemory(F, 1);
1750 setOnlyReadsMemory(F, 2);
1752 case LibFunc::fseeko64:
1753 case LibFunc::ftello64:
1754 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1757 setDoesNotCapture(F, 1);
1759 case LibFunc::tmpfile64:
1760 if (!FTy->getReturnType()->isPointerTy())
1763 setDoesNotAlias(F, 0);
1765 case LibFunc::fstat64:
1766 case LibFunc::fstatvfs64:
1767 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1770 setDoesNotCapture(F, 2);
1772 case LibFunc::open64:
1773 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy())
1775 // May throw; "open" is a valid pthread cancellation point.
1776 setDoesNotCapture(F, 1);
1777 setOnlyReadsMemory(F, 1);
1779 case LibFunc::gettimeofday:
1780 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy() ||
1781 !FTy->getParamType(1)->isPointerTy())
1783 // Currently some platforms have the restrict keyword on the arguments to
1784 // gettimeofday. To be conservative, do not add noalias to gettimeofday's
1787 setDoesNotCapture(F, 1);
1788 setDoesNotCapture(F, 2);
1791 // Didn't mark any attributes.
1798 static bool addNoRecurseAttrs(const CallGraphSCC &SCC,
1799 SmallVectorImpl<WeakVH> &Revisit) {
1800 // Try and identify functions that do not recurse.
1802 // If the SCC contains multiple nodes we know for sure there is recursion.
1803 if (!SCC.isSingular())
1806 const CallGraphNode *CGN = *SCC.begin();
1807 Function *F = CGN->getFunction();
1808 if (!F || F->isDeclaration() || F->doesNotRecurse())
1811 // If all of the calls in F are identifiable and are to norecurse functions, F
1812 // is norecurse. This check also detects self-recursion as F is not currently
1813 // marked norecurse, so any called from F to F will not be marked norecurse.
1814 if (std::all_of(CGN->begin(), CGN->end(),
1815 [](const CallGraphNode::CallRecord &CR) {
1816 Function *F = CR.second->getFunction();
1817 return F && F->doesNotRecurse();
1819 // Function calls a potentially recursive function.
1820 return setDoesNotRecurse(*F);
1822 // We know that F is not obviously recursive, but we haven't been able to
1823 // prove that it doesn't actually recurse. Add it to the Revisit list to try
1824 // again top-down later.
1825 Revisit.push_back(F);
1829 static bool addNoRecurseAttrsTopDownOnly(Function *F) {
1830 // If F is internal and all uses are in norecurse functions, then F is also
1832 if (F->doesNotRecurse())
1834 if (F->hasInternalLinkage()) {
1835 for (auto *U : F->users())
1836 if (auto *I = dyn_cast<Instruction>(U)) {
1837 if (!I->getParent()->getParent()->doesNotRecurse())
1842 return setDoesNotRecurse(*F);
1847 bool FunctionAttrs::runOnSCC(CallGraphSCC &SCC) {
1848 TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
1849 bool Changed = false;
1851 // We compute dedicated AA results for each function in the SCC as needed. We
1852 // use a lambda referencing external objects so that they live long enough to
1853 // be queried, but we re-use them each time.
1854 Optional<BasicAAResult> BAR;
1855 Optional<AAResults> AAR;
1856 auto AARGetter = [&](Function &F) -> AAResults & {
1857 BAR.emplace(createLegacyPMBasicAAResult(*this, F));
1858 AAR.emplace(createLegacyPMAAResults(*this, F, *BAR));
1862 // Fill SCCNodes with the elements of the SCC. Used for quickly looking up
1863 // whether a given CallGraphNode is in this SCC. Also track whether there are
1864 // any external or opt-none nodes that will prevent us from optimizing any
1866 SCCNodeSet SCCNodes;
1867 bool ExternalNode = false;
1868 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
1869 Function *F = (*I)->getFunction();
1870 if (!F || F->hasFnAttribute(Attribute::OptimizeNone)) {
1871 // External node or function we're trying not to optimize - we both avoid
1872 // transform them and avoid leveraging information they provide.
1873 ExternalNode = true;
1877 // When initially processing functions, also infer their prototype
1878 // attributes if they are declarations.
1879 if (F->isDeclaration())
1880 Changed |= inferPrototypeAttributes(*F, *TLI);
1885 Changed |= addReadAttrs(SCCNodes, AARGetter);
1886 Changed |= addArgumentAttrs(SCCNodes);
1888 // If we have no external nodes participating in the SCC, we can infer some
1889 // more precise attributes as well.
1890 if (!ExternalNode) {
1891 Changed |= addNoAliasAttrs(SCCNodes);
1892 Changed |= addNonNullAttrs(SCCNodes, *TLI);
1895 Changed |= addNoRecurseAttrs(SCC, Revisit);
1899 bool FunctionAttrs::doFinalization(CallGraph &CG) {
1900 bool Changed = false;
1901 // When iterating over SCCs we visit functions in a bottom-up fashion. Some of
1902 // the rules we have for identifying norecurse functions work best with a
1903 // top-down walk, so look again at all the functions we previously marked as
1904 // worth revisiting, in top-down order.
1905 for (auto &F : reverse(Revisit))
1907 Changed |= addNoRecurseAttrsTopDownOnly(cast<Function>((Value*)F));