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 //===----------------------------------------------------------------------===//
11 /// This file implements interprocedural passes which walk the
12 /// call-graph deducing and/or propagating function attributes.
14 //===----------------------------------------------------------------------===//
16 #include "llvm/Transforms/IPO/FunctionAttrs.h"
17 #include "llvm/Transforms/IPO.h"
18 #include "llvm/ADT/SCCIterator.h"
19 #include "llvm/ADT/SetVector.h"
20 #include "llvm/ADT/SmallSet.h"
21 #include "llvm/ADT/Statistic.h"
22 #include "llvm/ADT/StringSwitch.h"
23 #include "llvm/Analysis/AliasAnalysis.h"
24 #include "llvm/Analysis/AssumptionCache.h"
25 #include "llvm/Analysis/BasicAliasAnalysis.h"
26 #include "llvm/Analysis/CallGraph.h"
27 #include "llvm/Analysis/CallGraphSCCPass.h"
28 #include "llvm/Analysis/CaptureTracking.h"
29 #include "llvm/Analysis/TargetLibraryInfo.h"
30 #include "llvm/Analysis/ValueTracking.h"
31 #include "llvm/IR/GlobalVariable.h"
32 #include "llvm/IR/InstIterator.h"
33 #include "llvm/IR/IntrinsicInst.h"
34 #include "llvm/IR/LLVMContext.h"
35 #include "llvm/Support/Debug.h"
36 #include "llvm/Support/raw_ostream.h"
37 #include "llvm/Analysis/TargetLibraryInfo.h"
40 #define DEBUG_TYPE "functionattrs"
42 STATISTIC(NumReadNone, "Number of functions marked readnone");
43 STATISTIC(NumReadOnly, "Number of functions marked readonly");
44 STATISTIC(NumNoCapture, "Number of arguments marked nocapture");
45 STATISTIC(NumReturned, "Number of arguments marked returned");
46 STATISTIC(NumReadNoneArg, "Number of arguments marked readnone");
47 STATISTIC(NumReadOnlyArg, "Number of arguments marked readonly");
48 STATISTIC(NumNoAlias, "Number of function returns marked noalias");
49 STATISTIC(NumNonNullReturn, "Number of function returns marked nonnull");
50 STATISTIC(NumNoRecurse, "Number of functions marked as norecurse");
52 // FIXME: This is disabled by default to avoid exposing security vulnerabilities
53 // in C/C++ code compiled by clang:
54 // http://lists.llvm.org/pipermail/cfe-dev/2017-January/052066.html
55 static cl::opt<bool> EnableNonnullArgPropagation(
56 "enable-nonnull-arg-prop", cl::Hidden,
57 cl::desc("Try to propagate nonnull argument attributes from callsites to "
58 "caller functions."));
61 typedef SmallSetVector<Function *, 8> SCCNodeSet;
64 /// Returns the memory access attribute for function F using AAR for AA results,
65 /// where SCCNodes is the current SCC.
67 /// If ThisBody is true, this function may examine the function body and will
68 /// return a result pertaining to this copy of the function. If it is false, the
69 /// result will be based only on AA results for the function declaration; it
70 /// will be assumed that some other (perhaps less optimized) version of the
71 /// function may be selected at link time.
72 static MemoryAccessKind checkFunctionMemoryAccess(Function &F, bool ThisBody,
74 const SCCNodeSet &SCCNodes) {
75 FunctionModRefBehavior MRB = AAR.getModRefBehavior(&F);
76 if (MRB == FMRB_DoesNotAccessMemory)
81 if (AliasAnalysis::onlyReadsMemory(MRB))
84 // Conservatively assume it writes to memory.
88 // Scan the function body for instructions that may read or write memory.
89 bool ReadsMemory = false;
90 for (inst_iterator II = inst_begin(F), E = inst_end(F); II != E; ++II) {
91 Instruction *I = &*II;
93 // Some instructions can be ignored even if they read or write memory.
94 // Detect these now, skipping to the next instruction if one is found.
95 CallSite CS(cast<Value>(I));
97 // Ignore calls to functions in the same SCC, as long as the call sites
98 // don't have operand bundles. Calls with operand bundles are allowed to
99 // have memory effects not described by the memory effects of the call
101 if (!CS.hasOperandBundles() && CS.getCalledFunction() &&
102 SCCNodes.count(CS.getCalledFunction()))
104 FunctionModRefBehavior MRB = AAR.getModRefBehavior(CS);
106 // If the call doesn't access memory, we're done.
107 if (!(MRB & MRI_ModRef))
110 if (!AliasAnalysis::onlyAccessesArgPointees(MRB)) {
111 // The call could access any memory. If that includes writes, give up.
114 // If it reads, note it.
120 // Check whether all pointer arguments point to local memory, and
121 // ignore calls that only access local memory.
122 for (CallSite::arg_iterator CI = CS.arg_begin(), CE = CS.arg_end();
125 if (!Arg->getType()->isPtrOrPtrVectorTy())
129 I->getAAMetadata(AAInfo);
130 MemoryLocation Loc(Arg, MemoryLocation::UnknownSize, AAInfo);
132 // Skip accesses to local or constant memory as they don't impact the
133 // externally visible mod/ref behavior.
134 if (AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true))
138 // Writes non-local memory. Give up.
141 // Ok, it reads non-local memory.
145 } else if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
146 // Ignore non-volatile loads from local memory. (Atomic is okay here.)
147 if (!LI->isVolatile()) {
148 MemoryLocation Loc = MemoryLocation::get(LI);
149 if (AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true))
152 } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
153 // Ignore non-volatile stores to local memory. (Atomic is okay here.)
154 if (!SI->isVolatile()) {
155 MemoryLocation Loc = MemoryLocation::get(SI);
156 if (AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true))
159 } else if (VAArgInst *VI = dyn_cast<VAArgInst>(I)) {
160 // Ignore vaargs on local memory.
161 MemoryLocation Loc = MemoryLocation::get(VI);
162 if (AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true))
166 // Any remaining instructions need to be taken seriously! Check if they
167 // read or write memory.
168 if (I->mayWriteToMemory())
169 // Writes memory. Just give up.
172 // If this instruction may read memory, remember that.
173 ReadsMemory |= I->mayReadFromMemory();
176 return ReadsMemory ? MAK_ReadOnly : MAK_ReadNone;
179 MemoryAccessKind llvm::computeFunctionBodyMemoryAccess(Function &F,
181 return checkFunctionMemoryAccess(F, /*ThisBody=*/true, AAR, {});
184 /// Deduce readonly/readnone attributes for the SCC.
185 template <typename AARGetterT>
186 static bool addReadAttrs(const SCCNodeSet &SCCNodes, AARGetterT &&AARGetter) {
187 // Check if any of the functions in the SCC read or write memory. If they
188 // write memory then they can't be marked readnone or readonly.
189 bool ReadsMemory = false;
190 for (Function *F : SCCNodes) {
191 // Call the callable parameter to look up AA results for this function.
192 AAResults &AAR = AARGetter(*F);
194 // Non-exact function definitions may not be selected at link time, and an
195 // alternative version that writes to memory may be selected. See the
196 // comment on GlobalValue::isDefinitionExact for more details.
197 switch (checkFunctionMemoryAccess(*F, F->hasExactDefinition(),
210 // Success! Functions in this SCC do not access memory, or only read memory.
211 // Give them the appropriate attribute.
212 bool MadeChange = false;
213 for (Function *F : SCCNodes) {
214 if (F->doesNotAccessMemory())
218 if (F->onlyReadsMemory() && ReadsMemory)
224 // Clear out any existing attributes.
225 F->removeFnAttr(Attribute::ReadOnly);
226 F->removeFnAttr(Attribute::ReadNone);
228 // Add in the new attribute.
229 F->addFnAttr(ReadsMemory ? Attribute::ReadOnly : Attribute::ReadNone);
241 /// For a given pointer Argument, this retains a list of Arguments of functions
242 /// in the same SCC that the pointer data flows into. We use this to build an
243 /// SCC of the arguments.
244 struct ArgumentGraphNode {
245 Argument *Definition;
246 SmallVector<ArgumentGraphNode *, 4> Uses;
249 class ArgumentGraph {
250 // We store pointers to ArgumentGraphNode objects, so it's important that
251 // that they not move around upon insert.
252 typedef std::map<Argument *, ArgumentGraphNode> ArgumentMapTy;
254 ArgumentMapTy ArgumentMap;
256 // There is no root node for the argument graph, in fact:
257 // void f(int *x, int *y) { if (...) f(x, y); }
258 // is an example where the graph is disconnected. The SCCIterator requires a
259 // single entry point, so we maintain a fake ("synthetic") root node that
260 // uses every node. Because the graph is directed and nothing points into
261 // the root, it will not participate in any SCCs (except for its own).
262 ArgumentGraphNode SyntheticRoot;
265 ArgumentGraph() { SyntheticRoot.Definition = nullptr; }
267 typedef SmallVectorImpl<ArgumentGraphNode *>::iterator iterator;
269 iterator begin() { return SyntheticRoot.Uses.begin(); }
270 iterator end() { return SyntheticRoot.Uses.end(); }
271 ArgumentGraphNode *getEntryNode() { return &SyntheticRoot; }
273 ArgumentGraphNode *operator[](Argument *A) {
274 ArgumentGraphNode &Node = ArgumentMap[A];
276 SyntheticRoot.Uses.push_back(&Node);
281 /// This tracker checks whether callees are in the SCC, and if so it does not
282 /// consider that a capture, instead adding it to the "Uses" list and
283 /// continuing with the analysis.
284 struct ArgumentUsesTracker : public CaptureTracker {
285 ArgumentUsesTracker(const SCCNodeSet &SCCNodes)
286 : Captured(false), SCCNodes(SCCNodes) {}
288 void tooManyUses() override { Captured = true; }
290 bool captured(const Use *U) override {
291 CallSite CS(U->getUser());
292 if (!CS.getInstruction()) {
297 Function *F = CS.getCalledFunction();
298 if (!F || !F->hasExactDefinition() || !SCCNodes.count(F)) {
303 // Note: the callee and the two successor blocks *follow* the argument
304 // operands. This means there is no need to adjust UseIndex to account for
308 std::distance(const_cast<const Use *>(CS.arg_begin()), U);
310 assert(UseIndex < CS.data_operands_size() &&
311 "Indirect function calls should have been filtered above!");
313 if (UseIndex >= CS.getNumArgOperands()) {
314 // Data operand, but not a argument operand -- must be a bundle operand
315 assert(CS.hasOperandBundles() && "Must be!");
317 // CaptureTracking told us that we're being captured by an operand bundle
318 // use. In this case it does not matter if the callee is within our SCC
319 // or not -- we've been captured in some unknown way, and we have to be
325 if (UseIndex >= F->arg_size()) {
326 assert(F->isVarArg() && "More params than args in non-varargs call");
331 Uses.push_back(&*std::next(F->arg_begin(), UseIndex));
335 bool Captured; // True only if certainly captured (used outside our SCC).
336 SmallVector<Argument *, 4> Uses; // Uses within our SCC.
338 const SCCNodeSet &SCCNodes;
343 template <> struct GraphTraits<ArgumentGraphNode *> {
344 typedef ArgumentGraphNode *NodeRef;
345 typedef SmallVectorImpl<ArgumentGraphNode *>::iterator ChildIteratorType;
347 static NodeRef getEntryNode(NodeRef A) { return A; }
348 static ChildIteratorType child_begin(NodeRef N) { return N->Uses.begin(); }
349 static ChildIteratorType child_end(NodeRef N) { return N->Uses.end(); }
352 struct GraphTraits<ArgumentGraph *> : public GraphTraits<ArgumentGraphNode *> {
353 static NodeRef getEntryNode(ArgumentGraph *AG) { return AG->getEntryNode(); }
354 static ChildIteratorType nodes_begin(ArgumentGraph *AG) {
357 static ChildIteratorType nodes_end(ArgumentGraph *AG) { return AG->end(); }
361 /// Returns Attribute::None, Attribute::ReadOnly or Attribute::ReadNone.
362 static Attribute::AttrKind
363 determinePointerReadAttrs(Argument *A,
364 const SmallPtrSet<Argument *, 8> &SCCNodes) {
366 SmallVector<Use *, 32> Worklist;
367 SmallSet<Use *, 32> Visited;
369 // inalloca arguments are always clobbered by the call.
370 if (A->hasInAllocaAttr())
371 return Attribute::None;
374 // We don't need to track IsWritten. If A is written to, return immediately.
376 for (Use &U : A->uses()) {
378 Worklist.push_back(&U);
381 while (!Worklist.empty()) {
382 Use *U = Worklist.pop_back_val();
383 Instruction *I = cast<Instruction>(U->getUser());
385 switch (I->getOpcode()) {
386 case Instruction::BitCast:
387 case Instruction::GetElementPtr:
388 case Instruction::PHI:
389 case Instruction::Select:
390 case Instruction::AddrSpaceCast:
391 // The original value is not read/written via this if the new value isn't.
392 for (Use &UU : I->uses())
393 if (Visited.insert(&UU).second)
394 Worklist.push_back(&UU);
397 case Instruction::Call:
398 case Instruction::Invoke: {
399 bool Captures = true;
401 if (I->getType()->isVoidTy())
404 auto AddUsersToWorklistIfCapturing = [&] {
406 for (Use &UU : I->uses())
407 if (Visited.insert(&UU).second)
408 Worklist.push_back(&UU);
412 if (CS.doesNotAccessMemory()) {
413 AddUsersToWorklistIfCapturing();
417 Function *F = CS.getCalledFunction();
419 if (CS.onlyReadsMemory()) {
421 AddUsersToWorklistIfCapturing();
424 return Attribute::None;
427 // Note: the callee and the two successor blocks *follow* the argument
428 // operands. This means there is no need to adjust UseIndex to account
431 unsigned UseIndex = std::distance(CS.arg_begin(), U);
433 // U cannot be the callee operand use: since we're exploring the
434 // transitive uses of an Argument, having such a use be a callee would
435 // imply the CallSite is an indirect call or invoke; and we'd take the
437 assert(UseIndex < CS.data_operands_size() &&
438 "Data operand use expected!");
440 bool IsOperandBundleUse = UseIndex >= CS.getNumArgOperands();
442 if (UseIndex >= F->arg_size() && !IsOperandBundleUse) {
443 assert(F->isVarArg() && "More params than args in non-varargs call");
444 return Attribute::None;
447 Captures &= !CS.doesNotCapture(UseIndex);
449 // Since the optimizer (by design) cannot see the data flow corresponding
450 // to a operand bundle use, these cannot participate in the optimistic SCC
451 // analysis. Instead, we model the operand bundle uses as arguments in
452 // call to a function external to the SCC.
453 if (IsOperandBundleUse ||
454 !SCCNodes.count(&*std::next(F->arg_begin(), UseIndex))) {
456 // The accessors used on CallSite here do the right thing for calls and
457 // invokes with operand bundles.
459 if (!CS.onlyReadsMemory() && !CS.onlyReadsMemory(UseIndex))
460 return Attribute::None;
461 if (!CS.doesNotAccessMemory(UseIndex))
465 AddUsersToWorklistIfCapturing();
469 case Instruction::Load:
470 // A volatile load has side effects beyond what readonly can be relied
472 if (cast<LoadInst>(I)->isVolatile())
473 return Attribute::None;
478 case Instruction::ICmp:
479 case Instruction::Ret:
483 return Attribute::None;
487 return IsRead ? Attribute::ReadOnly : Attribute::ReadNone;
490 /// Deduce returned attributes for the SCC.
491 static bool addArgumentReturnedAttrs(const SCCNodeSet &SCCNodes) {
492 bool Changed = false;
494 // Check each function in turn, determining if an argument is always returned.
495 for (Function *F : SCCNodes) {
496 // We can infer and propagate function attributes only when we know that the
497 // definition we'll get at link time is *exactly* the definition we see now.
498 // For more details, see GlobalValue::mayBeDerefined.
499 if (!F->hasExactDefinition())
502 if (F->getReturnType()->isVoidTy())
505 // There is nothing to do if an argument is already marked as 'returned'.
506 if (any_of(F->args(),
507 [](const Argument &Arg) { return Arg.hasReturnedAttr(); }))
510 auto FindRetArg = [&]() -> Value * {
511 Value *RetArg = nullptr;
512 for (BasicBlock &BB : *F)
513 if (auto *Ret = dyn_cast<ReturnInst>(BB.getTerminator())) {
514 // Note that stripPointerCasts should look through functions with
515 // returned arguments.
516 Value *RetVal = Ret->getReturnValue()->stripPointerCasts();
517 if (!isa<Argument>(RetVal) || RetVal->getType() != F->getReturnType())
522 else if (RetArg != RetVal)
529 if (Value *RetArg = FindRetArg()) {
530 auto *A = cast<Argument>(RetArg);
531 A->addAttr(Attribute::Returned);
540 /// If a callsite has arguments that are also arguments to the parent function,
541 /// try to propagate attributes from the callsite's arguments to the parent's
542 /// arguments. This may be important because inlining can cause information loss
543 /// when attribute knowledge disappears with the inlined call.
544 static bool addArgumentAttrsFromCallsites(Function &F) {
545 if (!EnableNonnullArgPropagation)
548 bool Changed = false;
550 // For an argument attribute to transfer from a callsite to the parent, the
551 // call must be guaranteed to execute every time the parent is called.
552 // Conservatively, just check for calls in the entry block that are guaranteed
554 // TODO: This could be enhanced by testing if the callsite post-dominates the
555 // entry block or by doing simple forward walks or backward walks to the
557 BasicBlock &Entry = F.getEntryBlock();
558 for (Instruction &I : Entry) {
559 if (auto CS = CallSite(&I)) {
560 if (auto *CalledFunc = CS.getCalledFunction()) {
561 for (auto &CSArg : CalledFunc->args()) {
562 if (!CSArg.hasNonNullAttr())
565 // If the non-null callsite argument operand is an argument to 'F'
566 // (the caller) and the call is guaranteed to execute, then the value
567 // must be non-null throughout 'F'.
568 auto *FArg = dyn_cast<Argument>(CS.getArgOperand(CSArg.getArgNo()));
569 if (FArg && !FArg->hasNonNullAttr()) {
570 FArg->addAttr(Attribute::NonNull);
576 if (!isGuaranteedToTransferExecutionToSuccessor(&I))
583 /// Deduce nocapture attributes for the SCC.
584 static bool addArgumentAttrs(const SCCNodeSet &SCCNodes) {
585 bool Changed = false;
589 // Check each function in turn, determining which pointer arguments are not
591 for (Function *F : SCCNodes) {
592 // We can infer and propagate function attributes only when we know that the
593 // definition we'll get at link time is *exactly* the definition we see now.
594 // For more details, see GlobalValue::mayBeDerefined.
595 if (!F->hasExactDefinition())
598 Changed |= addArgumentAttrsFromCallsites(*F);
600 // Functions that are readonly (or readnone) and nounwind and don't return
601 // a value can't capture arguments. Don't analyze them.
602 if (F->onlyReadsMemory() && F->doesNotThrow() &&
603 F->getReturnType()->isVoidTy()) {
604 for (Function::arg_iterator A = F->arg_begin(), E = F->arg_end(); A != E;
606 if (A->getType()->isPointerTy() && !A->hasNoCaptureAttr()) {
607 A->addAttr(Attribute::NoCapture);
615 for (Function::arg_iterator A = F->arg_begin(), E = F->arg_end(); A != E;
617 if (!A->getType()->isPointerTy())
619 bool HasNonLocalUses = false;
620 if (!A->hasNoCaptureAttr()) {
621 ArgumentUsesTracker Tracker(SCCNodes);
622 PointerMayBeCaptured(&*A, &Tracker);
623 if (!Tracker.Captured) {
624 if (Tracker.Uses.empty()) {
625 // If it's trivially not captured, mark it nocapture now.
626 A->addAttr(Attribute::NoCapture);
630 // If it's not trivially captured and not trivially not captured,
631 // then it must be calling into another function in our SCC. Save
632 // its particulars for Argument-SCC analysis later.
633 ArgumentGraphNode *Node = AG[&*A];
634 for (Argument *Use : Tracker.Uses) {
635 Node->Uses.push_back(AG[Use]);
637 HasNonLocalUses = true;
641 // Otherwise, it's captured. Don't bother doing SCC analysis on it.
643 if (!HasNonLocalUses && !A->onlyReadsMemory()) {
644 // Can we determine that it's readonly/readnone without doing an SCC?
645 // Note that we don't allow any calls at all here, or else our result
646 // will be dependent on the iteration order through the functions in the
648 SmallPtrSet<Argument *, 8> Self;
650 Attribute::AttrKind R = determinePointerReadAttrs(&*A, Self);
651 if (R != Attribute::None) {
654 R == Attribute::ReadOnly ? ++NumReadOnlyArg : ++NumReadNoneArg;
660 // The graph we've collected is partial because we stopped scanning for
661 // argument uses once we solved the argument trivially. These partial nodes
662 // show up as ArgumentGraphNode objects with an empty Uses list, and for
663 // these nodes the final decision about whether they capture has already been
664 // made. If the definition doesn't have a 'nocapture' attribute by now, it
667 for (scc_iterator<ArgumentGraph *> I = scc_begin(&AG); !I.isAtEnd(); ++I) {
668 const std::vector<ArgumentGraphNode *> &ArgumentSCC = *I;
669 if (ArgumentSCC.size() == 1) {
670 if (!ArgumentSCC[0]->Definition)
671 continue; // synthetic root node
673 // eg. "void f(int* x) { if (...) f(x); }"
674 if (ArgumentSCC[0]->Uses.size() == 1 &&
675 ArgumentSCC[0]->Uses[0] == ArgumentSCC[0]) {
676 Argument *A = ArgumentSCC[0]->Definition;
677 A->addAttr(Attribute::NoCapture);
684 bool SCCCaptured = false;
685 for (auto I = ArgumentSCC.begin(), E = ArgumentSCC.end();
686 I != E && !SCCCaptured; ++I) {
687 ArgumentGraphNode *Node = *I;
688 if (Node->Uses.empty()) {
689 if (!Node->Definition->hasNoCaptureAttr())
696 SmallPtrSet<Argument *, 8> ArgumentSCCNodes;
697 // Fill ArgumentSCCNodes with the elements of the ArgumentSCC. Used for
698 // quickly looking up whether a given Argument is in this ArgumentSCC.
699 for (ArgumentGraphNode *I : ArgumentSCC) {
700 ArgumentSCCNodes.insert(I->Definition);
703 for (auto I = ArgumentSCC.begin(), E = ArgumentSCC.end();
704 I != E && !SCCCaptured; ++I) {
705 ArgumentGraphNode *N = *I;
706 for (ArgumentGraphNode *Use : N->Uses) {
707 Argument *A = Use->Definition;
708 if (A->hasNoCaptureAttr() || ArgumentSCCNodes.count(A))
717 for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) {
718 Argument *A = ArgumentSCC[i]->Definition;
719 A->addAttr(Attribute::NoCapture);
724 // We also want to compute readonly/readnone. With a small number of false
725 // negatives, we can assume that any pointer which is captured isn't going
726 // to be provably readonly or readnone, since by definition we can't
727 // analyze all uses of a captured pointer.
729 // The false negatives happen when the pointer is captured by a function
730 // that promises readonly/readnone behaviour on the pointer, then the
731 // pointer's lifetime ends before anything that writes to arbitrary memory.
732 // Also, a readonly/readnone pointer may be returned, but returning a
733 // pointer is capturing it.
735 Attribute::AttrKind ReadAttr = Attribute::ReadNone;
736 for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) {
737 Argument *A = ArgumentSCC[i]->Definition;
738 Attribute::AttrKind K = determinePointerReadAttrs(A, ArgumentSCCNodes);
739 if (K == Attribute::ReadNone)
741 if (K == Attribute::ReadOnly) {
742 ReadAttr = Attribute::ReadOnly;
749 if (ReadAttr != Attribute::None) {
750 for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) {
751 Argument *A = ArgumentSCC[i]->Definition;
752 // Clear out existing readonly/readnone attributes
753 A->removeAttr(Attribute::ReadOnly);
754 A->removeAttr(Attribute::ReadNone);
755 A->addAttr(ReadAttr);
756 ReadAttr == Attribute::ReadOnly ? ++NumReadOnlyArg : ++NumReadNoneArg;
765 /// Tests whether a function is "malloc-like".
767 /// A function is "malloc-like" if it returns either null or a pointer that
768 /// doesn't alias any other pointer visible to the caller.
769 static bool isFunctionMallocLike(Function *F, const SCCNodeSet &SCCNodes) {
770 SmallSetVector<Value *, 8> FlowsToReturn;
771 for (BasicBlock &BB : *F)
772 if (ReturnInst *Ret = dyn_cast<ReturnInst>(BB.getTerminator()))
773 FlowsToReturn.insert(Ret->getReturnValue());
775 for (unsigned i = 0; i != FlowsToReturn.size(); ++i) {
776 Value *RetVal = FlowsToReturn[i];
778 if (Constant *C = dyn_cast<Constant>(RetVal)) {
779 if (!C->isNullValue() && !isa<UndefValue>(C))
785 if (isa<Argument>(RetVal))
788 if (Instruction *RVI = dyn_cast<Instruction>(RetVal))
789 switch (RVI->getOpcode()) {
790 // Extend the analysis by looking upwards.
791 case Instruction::BitCast:
792 case Instruction::GetElementPtr:
793 case Instruction::AddrSpaceCast:
794 FlowsToReturn.insert(RVI->getOperand(0));
796 case Instruction::Select: {
797 SelectInst *SI = cast<SelectInst>(RVI);
798 FlowsToReturn.insert(SI->getTrueValue());
799 FlowsToReturn.insert(SI->getFalseValue());
802 case Instruction::PHI: {
803 PHINode *PN = cast<PHINode>(RVI);
804 for (Value *IncValue : PN->incoming_values())
805 FlowsToReturn.insert(IncValue);
809 // Check whether the pointer came from an allocation.
810 case Instruction::Alloca:
812 case Instruction::Call:
813 case Instruction::Invoke: {
815 if (CS.hasRetAttr(Attribute::NoAlias))
817 if (CS.getCalledFunction() && SCCNodes.count(CS.getCalledFunction()))
822 return false; // Did not come from an allocation.
825 if (PointerMayBeCaptured(RetVal, false, /*StoreCaptures=*/false))
832 /// Deduce noalias attributes for the SCC.
833 static bool addNoAliasAttrs(const SCCNodeSet &SCCNodes) {
834 // Check each function in turn, determining which functions return noalias
836 for (Function *F : SCCNodes) {
838 if (F->returnDoesNotAlias())
841 // We can infer and propagate function attributes only when we know that the
842 // definition we'll get at link time is *exactly* the definition we see now.
843 // For more details, see GlobalValue::mayBeDerefined.
844 if (!F->hasExactDefinition())
847 // We annotate noalias return values, which are only applicable to
849 if (!F->getReturnType()->isPointerTy())
852 if (!isFunctionMallocLike(F, SCCNodes))
856 bool MadeChange = false;
857 for (Function *F : SCCNodes) {
858 if (F->returnDoesNotAlias() ||
859 !F->getReturnType()->isPointerTy())
862 F->setReturnDoesNotAlias();
870 /// Tests whether this function is known to not return null.
872 /// Requires that the function returns a pointer.
874 /// Returns true if it believes the function will not return a null, and sets
875 /// \p Speculative based on whether the returned conclusion is a speculative
876 /// conclusion due to SCC calls.
877 static bool isReturnNonNull(Function *F, const SCCNodeSet &SCCNodes,
879 assert(F->getReturnType()->isPointerTy() &&
880 "nonnull only meaningful on pointer types");
883 SmallSetVector<Value *, 8> FlowsToReturn;
884 for (BasicBlock &BB : *F)
885 if (auto *Ret = dyn_cast<ReturnInst>(BB.getTerminator()))
886 FlowsToReturn.insert(Ret->getReturnValue());
888 for (unsigned i = 0; i != FlowsToReturn.size(); ++i) {
889 Value *RetVal = FlowsToReturn[i];
891 // If this value is locally known to be non-null, we're good
892 if (isKnownNonNull(RetVal))
895 // Otherwise, we need to look upwards since we can't make any local
897 Instruction *RVI = dyn_cast<Instruction>(RetVal);
900 switch (RVI->getOpcode()) {
901 // Extend the analysis by looking upwards.
902 case Instruction::BitCast:
903 case Instruction::GetElementPtr:
904 case Instruction::AddrSpaceCast:
905 FlowsToReturn.insert(RVI->getOperand(0));
907 case Instruction::Select: {
908 SelectInst *SI = cast<SelectInst>(RVI);
909 FlowsToReturn.insert(SI->getTrueValue());
910 FlowsToReturn.insert(SI->getFalseValue());
913 case Instruction::PHI: {
914 PHINode *PN = cast<PHINode>(RVI);
915 for (int i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
916 FlowsToReturn.insert(PN->getIncomingValue(i));
919 case Instruction::Call:
920 case Instruction::Invoke: {
922 Function *Callee = CS.getCalledFunction();
923 // A call to a node within the SCC is assumed to return null until
925 if (Callee && SCCNodes.count(Callee)) {
932 return false; // Unknown source, may be null
934 llvm_unreachable("should have either continued or returned");
940 /// Deduce nonnull attributes for the SCC.
941 static bool addNonNullAttrs(const SCCNodeSet &SCCNodes) {
942 // Speculative that all functions in the SCC return only nonnull
943 // pointers. We may refute this as we analyze functions.
944 bool SCCReturnsNonNull = true;
946 bool MadeChange = false;
948 // Check each function in turn, determining which functions return nonnull
950 for (Function *F : SCCNodes) {
952 if (F->getAttributes().hasAttribute(AttributeList::ReturnIndex,
956 // We can infer and propagate function attributes only when we know that the
957 // definition we'll get at link time is *exactly* the definition we see now.
958 // For more details, see GlobalValue::mayBeDerefined.
959 if (!F->hasExactDefinition())
962 // We annotate nonnull return values, which are only applicable to
964 if (!F->getReturnType()->isPointerTy())
967 bool Speculative = false;
968 if (isReturnNonNull(F, SCCNodes, Speculative)) {
970 // Mark the function eagerly since we may discover a function
971 // which prevents us from speculating about the entire SCC
972 DEBUG(dbgs() << "Eagerly marking " << F->getName() << " as nonnull\n");
973 F->addAttribute(AttributeList::ReturnIndex, Attribute::NonNull);
979 // At least one function returns something which could be null, can't
980 // speculate any more.
981 SCCReturnsNonNull = false;
984 if (SCCReturnsNonNull) {
985 for (Function *F : SCCNodes) {
986 if (F->getAttributes().hasAttribute(AttributeList::ReturnIndex,
987 Attribute::NonNull) ||
988 !F->getReturnType()->isPointerTy())
991 DEBUG(dbgs() << "SCC marking " << F->getName() << " as nonnull\n");
992 F->addAttribute(AttributeList::ReturnIndex, Attribute::NonNull);
1001 /// Remove the convergent attribute from all functions in the SCC if every
1002 /// callsite within the SCC is not convergent (except for calls to functions
1003 /// within the SCC). Returns true if changes were made.
1004 static bool removeConvergentAttrs(const SCCNodeSet &SCCNodes) {
1005 // For every function in SCC, ensure that either
1006 // * it is not convergent, or
1007 // * we can remove its convergent attribute.
1008 bool HasConvergentFn = false;
1009 for (Function *F : SCCNodes) {
1010 if (!F->isConvergent()) continue;
1011 HasConvergentFn = true;
1013 // Can't remove convergent from function declarations.
1014 if (F->isDeclaration()) return false;
1016 // Can't remove convergent if any of our functions has a convergent call to a
1017 // function not in the SCC.
1018 for (Instruction &I : instructions(*F)) {
1020 // Bail if CS is a convergent call to a function not in the SCC.
1021 if (CS && CS.isConvergent() &&
1022 SCCNodes.count(CS.getCalledFunction()) == 0)
1027 // If the SCC doesn't have any convergent functions, we have nothing to do.
1028 if (!HasConvergentFn) return false;
1030 // If we got here, all of the calls the SCC makes to functions not in the SCC
1031 // are non-convergent. Therefore all of the SCC's functions can also be made
1032 // non-convergent. We'll remove the attr from the callsites in
1033 // InstCombineCalls.
1034 for (Function *F : SCCNodes) {
1035 if (!F->isConvergent()) continue;
1037 DEBUG(dbgs() << "Removing convergent attr from fn " << F->getName()
1039 F->setNotConvergent();
1044 static bool setDoesNotRecurse(Function &F) {
1045 if (F.doesNotRecurse())
1047 F.setDoesNotRecurse();
1052 static bool addNoRecurseAttrs(const SCCNodeSet &SCCNodes) {
1053 // Try and identify functions that do not recurse.
1055 // If the SCC contains multiple nodes we know for sure there is recursion.
1056 if (SCCNodes.size() != 1)
1059 Function *F = *SCCNodes.begin();
1060 if (!F || F->isDeclaration() || F->doesNotRecurse())
1063 // If all of the calls in F are identifiable and are to norecurse functions, F
1064 // is norecurse. This check also detects self-recursion as F is not currently
1065 // marked norecurse, so any called from F to F will not be marked norecurse.
1066 for (Instruction &I : instructions(*F))
1067 if (auto CS = CallSite(&I)) {
1068 Function *Callee = CS.getCalledFunction();
1069 if (!Callee || Callee == F || !Callee->doesNotRecurse())
1070 // Function calls a potentially recursive function.
1074 // Every call was to a non-recursive function other than this function, and
1075 // we have no indirect recursion as the SCC size is one. This function cannot
1077 return setDoesNotRecurse(*F);
1080 PreservedAnalyses PostOrderFunctionAttrsPass::run(LazyCallGraph::SCC &C,
1081 CGSCCAnalysisManager &AM,
1083 CGSCCUpdateResult &) {
1084 FunctionAnalysisManager &FAM =
1085 AM.getResult<FunctionAnalysisManagerCGSCCProxy>(C, CG).getManager();
1087 // We pass a lambda into functions to wire them up to the analysis manager
1088 // for getting function analyses.
1089 auto AARGetter = [&](Function &F) -> AAResults & {
1090 return FAM.getResult<AAManager>(F);
1093 // Fill SCCNodes with the elements of the SCC. Also track whether there are
1094 // any external or opt-none nodes that will prevent us from optimizing any
1096 SCCNodeSet SCCNodes;
1097 bool HasUnknownCall = false;
1098 for (LazyCallGraph::Node &N : C) {
1099 Function &F = N.getFunction();
1100 if (F.hasFnAttribute(Attribute::OptimizeNone)) {
1101 // Treat any function we're trying not to optimize as if it were an
1102 // indirect call and omit it from the node set used below.
1103 HasUnknownCall = true;
1106 // Track whether any functions in this SCC have an unknown call edge.
1107 // Note: if this is ever a performance hit, we can common it with
1108 // subsequent routines which also do scans over the instructions of the
1110 if (!HasUnknownCall)
1111 for (Instruction &I : instructions(F))
1112 if (auto CS = CallSite(&I))
1113 if (!CS.getCalledFunction()) {
1114 HasUnknownCall = true;
1118 SCCNodes.insert(&F);
1121 bool Changed = false;
1122 Changed |= addArgumentReturnedAttrs(SCCNodes);
1123 Changed |= addReadAttrs(SCCNodes, AARGetter);
1124 Changed |= addArgumentAttrs(SCCNodes);
1126 // If we have no external nodes participating in the SCC, we can deduce some
1127 // more precise attributes as well.
1128 if (!HasUnknownCall) {
1129 Changed |= addNoAliasAttrs(SCCNodes);
1130 Changed |= addNonNullAttrs(SCCNodes);
1131 Changed |= removeConvergentAttrs(SCCNodes);
1132 Changed |= addNoRecurseAttrs(SCCNodes);
1135 return Changed ? PreservedAnalyses::none() : PreservedAnalyses::all();
1139 struct PostOrderFunctionAttrsLegacyPass : public CallGraphSCCPass {
1140 static char ID; // Pass identification, replacement for typeid
1141 PostOrderFunctionAttrsLegacyPass() : CallGraphSCCPass(ID) {
1142 initializePostOrderFunctionAttrsLegacyPassPass(
1143 *PassRegistry::getPassRegistry());
1146 bool runOnSCC(CallGraphSCC &SCC) override;
1148 void getAnalysisUsage(AnalysisUsage &AU) const override {
1149 AU.setPreservesCFG();
1150 AU.addRequired<AssumptionCacheTracker>();
1151 getAAResultsAnalysisUsage(AU);
1152 CallGraphSCCPass::getAnalysisUsage(AU);
1157 char PostOrderFunctionAttrsLegacyPass::ID = 0;
1158 INITIALIZE_PASS_BEGIN(PostOrderFunctionAttrsLegacyPass, "functionattrs",
1159 "Deduce function attributes", false, false)
1160 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
1161 INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
1162 INITIALIZE_PASS_END(PostOrderFunctionAttrsLegacyPass, "functionattrs",
1163 "Deduce function attributes", false, false)
1165 Pass *llvm::createPostOrderFunctionAttrsLegacyPass() {
1166 return new PostOrderFunctionAttrsLegacyPass();
1169 template <typename AARGetterT>
1170 static bool runImpl(CallGraphSCC &SCC, AARGetterT AARGetter) {
1171 bool Changed = false;
1173 // Fill SCCNodes with the elements of the SCC. Used for quickly looking up
1174 // whether a given CallGraphNode is in this SCC. Also track whether there are
1175 // any external or opt-none nodes that will prevent us from optimizing any
1177 SCCNodeSet SCCNodes;
1178 bool ExternalNode = false;
1179 for (CallGraphNode *I : SCC) {
1180 Function *F = I->getFunction();
1181 if (!F || F->hasFnAttribute(Attribute::OptimizeNone)) {
1182 // External node or function we're trying not to optimize - we both avoid
1183 // transform them and avoid leveraging information they provide.
1184 ExternalNode = true;
1191 Changed |= addArgumentReturnedAttrs(SCCNodes);
1192 Changed |= addReadAttrs(SCCNodes, AARGetter);
1193 Changed |= addArgumentAttrs(SCCNodes);
1195 // If we have no external nodes participating in the SCC, we can deduce some
1196 // more precise attributes as well.
1197 if (!ExternalNode) {
1198 Changed |= addNoAliasAttrs(SCCNodes);
1199 Changed |= addNonNullAttrs(SCCNodes);
1200 Changed |= removeConvergentAttrs(SCCNodes);
1201 Changed |= addNoRecurseAttrs(SCCNodes);
1207 bool PostOrderFunctionAttrsLegacyPass::runOnSCC(CallGraphSCC &SCC) {
1210 return runImpl(SCC, LegacyAARGetter(*this));
1214 struct ReversePostOrderFunctionAttrsLegacyPass : public ModulePass {
1215 static char ID; // Pass identification, replacement for typeid
1216 ReversePostOrderFunctionAttrsLegacyPass() : ModulePass(ID) {
1217 initializeReversePostOrderFunctionAttrsLegacyPassPass(
1218 *PassRegistry::getPassRegistry());
1221 bool runOnModule(Module &M) override;
1223 void getAnalysisUsage(AnalysisUsage &AU) const override {
1224 AU.setPreservesCFG();
1225 AU.addRequired<CallGraphWrapperPass>();
1226 AU.addPreserved<CallGraphWrapperPass>();
1231 char ReversePostOrderFunctionAttrsLegacyPass::ID = 0;
1232 INITIALIZE_PASS_BEGIN(ReversePostOrderFunctionAttrsLegacyPass, "rpo-functionattrs",
1233 "Deduce function attributes in RPO", false, false)
1234 INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
1235 INITIALIZE_PASS_END(ReversePostOrderFunctionAttrsLegacyPass, "rpo-functionattrs",
1236 "Deduce function attributes in RPO", false, false)
1238 Pass *llvm::createReversePostOrderFunctionAttrsPass() {
1239 return new ReversePostOrderFunctionAttrsLegacyPass();
1242 static bool addNoRecurseAttrsTopDown(Function &F) {
1243 // We check the preconditions for the function prior to calling this to avoid
1244 // the cost of building up a reversible post-order list. We assert them here
1245 // to make sure none of the invariants this relies on were violated.
1246 assert(!F.isDeclaration() && "Cannot deduce norecurse without a definition!");
1247 assert(!F.doesNotRecurse() &&
1248 "This function has already been deduced as norecurs!");
1249 assert(F.hasInternalLinkage() &&
1250 "Can only do top-down deduction for internal linkage functions!");
1252 // If F is internal and all of its uses are calls from a non-recursive
1253 // functions, then none of its calls could in fact recurse without going
1254 // through a function marked norecurse, and so we can mark this function too
1255 // as norecurse. Note that the uses must actually be calls -- otherwise
1256 // a pointer to this function could be returned from a norecurse function but
1257 // this function could be recursively (indirectly) called. Note that this
1258 // also detects if F is directly recursive as F is not yet marked as
1259 // a norecurse function.
1260 for (auto *U : F.users()) {
1261 auto *I = dyn_cast<Instruction>(U);
1265 if (!CS || !CS.getParent()->getParent()->doesNotRecurse())
1268 return setDoesNotRecurse(F);
1271 static bool deduceFunctionAttributeInRPO(Module &M, CallGraph &CG) {
1272 // We only have a post-order SCC traversal (because SCCs are inherently
1273 // discovered in post-order), so we accumulate them in a vector and then walk
1274 // it in reverse. This is simpler than using the RPO iterator infrastructure
1275 // because we need to combine SCC detection and the PO walk of the call
1276 // graph. We can also cheat egregiously because we're primarily interested in
1277 // synthesizing norecurse and so we can only save the singular SCCs as SCCs
1278 // with multiple functions in them will clearly be recursive.
1279 SmallVector<Function *, 16> Worklist;
1280 for (scc_iterator<CallGraph *> I = scc_begin(&CG); !I.isAtEnd(); ++I) {
1284 Function *F = I->front()->getFunction();
1285 if (F && !F->isDeclaration() && !F->doesNotRecurse() &&
1286 F->hasInternalLinkage())
1287 Worklist.push_back(F);
1290 bool Changed = false;
1291 for (auto *F : reverse(Worklist))
1292 Changed |= addNoRecurseAttrsTopDown(*F);
1297 bool ReversePostOrderFunctionAttrsLegacyPass::runOnModule(Module &M) {
1301 auto &CG = getAnalysis<CallGraphWrapperPass>().getCallGraph();
1303 return deduceFunctionAttributeInRPO(M, CG);
1307 ReversePostOrderFunctionAttrsPass::run(Module &M, ModuleAnalysisManager &AM) {
1308 auto &CG = AM.getResult<CallGraphAnalysis>(M);
1310 if (!deduceFunctionAttributeInRPO(M, CG))
1311 return PreservedAnalyses::all();
1313 PreservedAnalyses PA;
1314 PA.preserve<CallGraphAnalysis>();