1 //===- FunctionAttrs.cpp - Pass which marks functions attributes ----------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
10 /// This file implements interprocedural passes which walk the
11 /// call-graph deducing and/or propagating function attributes.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/Transforms/IPO/FunctionAttrs.h"
16 #include "llvm/ADT/SCCIterator.h"
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/ADT/SetVector.h"
19 #include "llvm/ADT/SmallPtrSet.h"
20 #include "llvm/ADT/SmallVector.h"
21 #include "llvm/ADT/Statistic.h"
22 #include "llvm/Analysis/AliasAnalysis.h"
23 #include "llvm/Analysis/AssumptionCache.h"
24 #include "llvm/Analysis/BasicAliasAnalysis.h"
25 #include "llvm/Analysis/CGSCCPassManager.h"
26 #include "llvm/Analysis/CallGraph.h"
27 #include "llvm/Analysis/CallGraphSCCPass.h"
28 #include "llvm/Analysis/CaptureTracking.h"
29 #include "llvm/Analysis/LazyCallGraph.h"
30 #include "llvm/Analysis/MemoryBuiltins.h"
31 #include "llvm/Analysis/MemoryLocation.h"
32 #include "llvm/Analysis/ValueTracking.h"
33 #include "llvm/IR/Argument.h"
34 #include "llvm/IR/Attributes.h"
35 #include "llvm/IR/BasicBlock.h"
36 #include "llvm/IR/CallSite.h"
37 #include "llvm/IR/Constant.h"
38 #include "llvm/IR/Constants.h"
39 #include "llvm/IR/Function.h"
40 #include "llvm/IR/InstIterator.h"
41 #include "llvm/IR/InstrTypes.h"
42 #include "llvm/IR/Instruction.h"
43 #include "llvm/IR/Instructions.h"
44 #include "llvm/IR/IntrinsicInst.h"
45 #include "llvm/IR/Metadata.h"
46 #include "llvm/IR/PassManager.h"
47 #include "llvm/IR/Type.h"
48 #include "llvm/IR/Use.h"
49 #include "llvm/IR/User.h"
50 #include "llvm/IR/Value.h"
51 #include "llvm/Pass.h"
52 #include "llvm/Support/Casting.h"
53 #include "llvm/Support/CommandLine.h"
54 #include "llvm/Support/Compiler.h"
55 #include "llvm/Support/Debug.h"
56 #include "llvm/Support/ErrorHandling.h"
57 #include "llvm/Support/raw_ostream.h"
58 #include "llvm/Transforms/IPO.h"
66 #define DEBUG_TYPE "functionattrs"
68 STATISTIC(NumReadNone
, "Number of functions marked readnone");
69 STATISTIC(NumReadOnly
, "Number of functions marked readonly");
70 STATISTIC(NumWriteOnly
, "Number of functions marked writeonly");
71 STATISTIC(NumNoCapture
, "Number of arguments marked nocapture");
72 STATISTIC(NumReturned
, "Number of arguments marked returned");
73 STATISTIC(NumReadNoneArg
, "Number of arguments marked readnone");
74 STATISTIC(NumReadOnlyArg
, "Number of arguments marked readonly");
75 STATISTIC(NumNoAlias
, "Number of function returns marked noalias");
76 STATISTIC(NumNonNullReturn
, "Number of function returns marked nonnull");
77 STATISTIC(NumNoRecurse
, "Number of functions marked as norecurse");
78 STATISTIC(NumNoUnwind
, "Number of functions marked as nounwind");
79 STATISTIC(NumNoFree
, "Number of functions marked as nofree");
81 // FIXME: This is disabled by default to avoid exposing security vulnerabilities
82 // in C/C++ code compiled by clang:
83 // http://lists.llvm.org/pipermail/cfe-dev/2017-January/052066.html
84 static cl::opt
<bool> EnableNonnullArgPropagation(
85 "enable-nonnull-arg-prop", cl::Hidden
,
86 cl::desc("Try to propagate nonnull argument attributes from callsites to "
87 "caller functions."));
89 static cl::opt
<bool> DisableNoUnwindInference(
90 "disable-nounwind-inference", cl::Hidden
,
91 cl::desc("Stop inferring nounwind attribute during function-attrs pass"));
93 static cl::opt
<bool> DisableNoFreeInference(
94 "disable-nofree-inference", cl::Hidden
,
95 cl::desc("Stop inferring nofree attribute during function-attrs pass"));
99 using SCCNodeSet
= SmallSetVector
<Function
*, 8>;
101 } // end anonymous namespace
103 /// Returns the memory access attribute for function F using AAR for AA results,
104 /// where SCCNodes is the current SCC.
106 /// If ThisBody is true, this function may examine the function body and will
107 /// return a result pertaining to this copy of the function. If it is false, the
108 /// result will be based only on AA results for the function declaration; it
109 /// will be assumed that some other (perhaps less optimized) version of the
110 /// function may be selected at link time.
111 static MemoryAccessKind
checkFunctionMemoryAccess(Function
&F
, bool ThisBody
,
113 const SCCNodeSet
&SCCNodes
) {
114 FunctionModRefBehavior MRB
= AAR
.getModRefBehavior(&F
);
115 if (MRB
== FMRB_DoesNotAccessMemory
)
120 if (AliasAnalysis::onlyReadsMemory(MRB
))
123 if (AliasAnalysis::doesNotReadMemory(MRB
))
124 return MAK_WriteOnly
;
126 // Conservatively assume it reads and writes to memory.
130 // Scan the function body for instructions that may read or write memory.
131 bool ReadsMemory
= false;
132 bool WritesMemory
= false;
133 for (inst_iterator II
= inst_begin(F
), E
= inst_end(F
); II
!= E
; ++II
) {
134 Instruction
*I
= &*II
;
136 // Some instructions can be ignored even if they read or write memory.
137 // Detect these now, skipping to the next instruction if one is found.
138 if (auto *Call
= dyn_cast
<CallBase
>(I
)) {
139 // Ignore calls to functions in the same SCC, as long as the call sites
140 // don't have operand bundles. Calls with operand bundles are allowed to
141 // have memory effects not described by the memory effects of the call
143 if (!Call
->hasOperandBundles() && Call
->getCalledFunction() &&
144 SCCNodes
.count(Call
->getCalledFunction()))
146 FunctionModRefBehavior MRB
= AAR
.getModRefBehavior(Call
);
147 ModRefInfo MRI
= createModRefInfo(MRB
);
149 // If the call doesn't access memory, we're done.
153 if (!AliasAnalysis::onlyAccessesArgPointees(MRB
)) {
154 // The call could access any memory. If that includes writes, note it.
157 // If it reads, note it.
163 // Check whether all pointer arguments point to local memory, and
164 // ignore calls that only access local memory.
165 for (CallSite::arg_iterator CI
= Call
->arg_begin(), CE
= Call
->arg_end();
168 if (!Arg
->getType()->isPtrOrPtrVectorTy())
172 I
->getAAMetadata(AAInfo
);
173 MemoryLocation
Loc(Arg
, LocationSize::unknown(), AAInfo
);
175 // Skip accesses to local or constant memory as they don't impact the
176 // externally visible mod/ref behavior.
177 if (AAR
.pointsToConstantMemory(Loc
, /*OrLocal=*/true))
181 // Writes non-local memory.
184 // Ok, it reads non-local memory.
188 } else if (LoadInst
*LI
= dyn_cast
<LoadInst
>(I
)) {
189 // Ignore non-volatile loads from local memory. (Atomic is okay here.)
190 if (!LI
->isVolatile()) {
191 MemoryLocation Loc
= MemoryLocation::get(LI
);
192 if (AAR
.pointsToConstantMemory(Loc
, /*OrLocal=*/true))
195 } else if (StoreInst
*SI
= dyn_cast
<StoreInst
>(I
)) {
196 // Ignore non-volatile stores to local memory. (Atomic is okay here.)
197 if (!SI
->isVolatile()) {
198 MemoryLocation Loc
= MemoryLocation::get(SI
);
199 if (AAR
.pointsToConstantMemory(Loc
, /*OrLocal=*/true))
202 } else if (VAArgInst
*VI
= dyn_cast
<VAArgInst
>(I
)) {
203 // Ignore vaargs on local memory.
204 MemoryLocation Loc
= MemoryLocation::get(VI
);
205 if (AAR
.pointsToConstantMemory(Loc
, /*OrLocal=*/true))
209 // Any remaining instructions need to be taken seriously! Check if they
210 // read or write memory.
212 // Writes memory, remember that.
213 WritesMemory
|= I
->mayWriteToMemory();
215 // If this instruction may read memory, remember that.
216 ReadsMemory
|= I
->mayReadFromMemory();
221 return MAK_WriteOnly
;
226 return ReadsMemory
? MAK_ReadOnly
: MAK_ReadNone
;
229 MemoryAccessKind
llvm::computeFunctionBodyMemoryAccess(Function
&F
,
231 return checkFunctionMemoryAccess(F
, /*ThisBody=*/true, AAR
, {});
234 /// Deduce readonly/readnone attributes for the SCC.
235 template <typename AARGetterT
>
236 static bool addReadAttrs(const SCCNodeSet
&SCCNodes
, AARGetterT
&&AARGetter
) {
237 // Check if any of the functions in the SCC read or write memory. If they
238 // write memory then they can't be marked readnone or readonly.
239 bool ReadsMemory
= false;
240 bool WritesMemory
= false;
241 for (Function
*F
: SCCNodes
) {
242 // Call the callable parameter to look up AA results for this function.
243 AAResults
&AAR
= AARGetter(*F
);
245 // Non-exact function definitions may not be selected at link time, and an
246 // alternative version that writes to memory may be selected. See the
247 // comment on GlobalValue::isDefinitionExact for more details.
248 switch (checkFunctionMemoryAccess(*F
, F
->hasExactDefinition(),
264 // If the SCC contains both functions that read and functions that write, then
265 // we cannot add readonly attributes.
266 if (ReadsMemory
&& WritesMemory
)
269 // Success! Functions in this SCC do not access memory, or only read memory.
270 // Give them the appropriate attribute.
271 bool MadeChange
= false;
273 for (Function
*F
: SCCNodes
) {
274 if (F
->doesNotAccessMemory())
278 if (F
->onlyReadsMemory() && ReadsMemory
)
282 if (F
->doesNotReadMemory() && WritesMemory
)
287 // Clear out any existing attributes.
288 F
->removeFnAttr(Attribute::ReadOnly
);
289 F
->removeFnAttr(Attribute::ReadNone
);
290 F
->removeFnAttr(Attribute::WriteOnly
);
292 if (!WritesMemory
&& !ReadsMemory
) {
293 // Clear out any "access range attributes" if readnone was deduced.
294 F
->removeFnAttr(Attribute::ArgMemOnly
);
295 F
->removeFnAttr(Attribute::InaccessibleMemOnly
);
296 F
->removeFnAttr(Attribute::InaccessibleMemOrArgMemOnly
);
299 // Add in the new attribute.
300 if (WritesMemory
&& !ReadsMemory
)
301 F
->addFnAttr(Attribute::WriteOnly
);
303 F
->addFnAttr(ReadsMemory
? Attribute::ReadOnly
: Attribute::ReadNone
);
305 if (WritesMemory
&& !ReadsMemory
)
307 else if (ReadsMemory
)
318 /// For a given pointer Argument, this retains a list of Arguments of functions
319 /// in the same SCC that the pointer data flows into. We use this to build an
320 /// SCC of the arguments.
321 struct ArgumentGraphNode
{
322 Argument
*Definition
;
323 SmallVector
<ArgumentGraphNode
*, 4> Uses
;
326 class ArgumentGraph
{
327 // We store pointers to ArgumentGraphNode objects, so it's important that
328 // that they not move around upon insert.
329 using ArgumentMapTy
= std::map
<Argument
*, ArgumentGraphNode
>;
331 ArgumentMapTy ArgumentMap
;
333 // There is no root node for the argument graph, in fact:
334 // void f(int *x, int *y) { if (...) f(x, y); }
335 // is an example where the graph is disconnected. The SCCIterator requires a
336 // single entry point, so we maintain a fake ("synthetic") root node that
337 // uses every node. Because the graph is directed and nothing points into
338 // the root, it will not participate in any SCCs (except for its own).
339 ArgumentGraphNode SyntheticRoot
;
342 ArgumentGraph() { SyntheticRoot
.Definition
= nullptr; }
344 using iterator
= SmallVectorImpl
<ArgumentGraphNode
*>::iterator
;
346 iterator
begin() { return SyntheticRoot
.Uses
.begin(); }
347 iterator
end() { return SyntheticRoot
.Uses
.end(); }
348 ArgumentGraphNode
*getEntryNode() { return &SyntheticRoot
; }
350 ArgumentGraphNode
*operator[](Argument
*A
) {
351 ArgumentGraphNode
&Node
= ArgumentMap
[A
];
353 SyntheticRoot
.Uses
.push_back(&Node
);
358 /// This tracker checks whether callees are in the SCC, and if so it does not
359 /// consider that a capture, instead adding it to the "Uses" list and
360 /// continuing with the analysis.
361 struct ArgumentUsesTracker
: public CaptureTracker
{
362 ArgumentUsesTracker(const SCCNodeSet
&SCCNodes
) : SCCNodes(SCCNodes
) {}
364 void tooManyUses() override
{ Captured
= true; }
366 bool captured(const Use
*U
) override
{
367 CallSite
CS(U
->getUser());
368 if (!CS
.getInstruction()) {
373 Function
*F
= CS
.getCalledFunction();
374 if (!F
|| !F
->hasExactDefinition() || !SCCNodes
.count(F
)) {
379 // Note: the callee and the two successor blocks *follow* the argument
380 // operands. This means there is no need to adjust UseIndex to account for
384 std::distance(const_cast<const Use
*>(CS
.arg_begin()), U
);
386 assert(UseIndex
< CS
.data_operands_size() &&
387 "Indirect function calls should have been filtered above!");
389 if (UseIndex
>= CS
.getNumArgOperands()) {
390 // Data operand, but not a argument operand -- must be a bundle operand
391 assert(CS
.hasOperandBundles() && "Must be!");
393 // CaptureTracking told us that we're being captured by an operand bundle
394 // use. In this case it does not matter if the callee is within our SCC
395 // or not -- we've been captured in some unknown way, and we have to be
401 if (UseIndex
>= F
->arg_size()) {
402 assert(F
->isVarArg() && "More params than args in non-varargs call");
407 Uses
.push_back(&*std::next(F
->arg_begin(), UseIndex
));
411 // True only if certainly captured (used outside our SCC).
412 bool Captured
= false;
414 // Uses within our SCC.
415 SmallVector
<Argument
*, 4> Uses
;
417 const SCCNodeSet
&SCCNodes
;
420 } // end anonymous namespace
424 template <> struct GraphTraits
<ArgumentGraphNode
*> {
425 using NodeRef
= ArgumentGraphNode
*;
426 using ChildIteratorType
= SmallVectorImpl
<ArgumentGraphNode
*>::iterator
;
428 static NodeRef
getEntryNode(NodeRef A
) { return A
; }
429 static ChildIteratorType
child_begin(NodeRef N
) { return N
->Uses
.begin(); }
430 static ChildIteratorType
child_end(NodeRef N
) { return N
->Uses
.end(); }
434 struct GraphTraits
<ArgumentGraph
*> : public GraphTraits
<ArgumentGraphNode
*> {
435 static NodeRef
getEntryNode(ArgumentGraph
*AG
) { return AG
->getEntryNode(); }
437 static ChildIteratorType
nodes_begin(ArgumentGraph
*AG
) {
441 static ChildIteratorType
nodes_end(ArgumentGraph
*AG
) { return AG
->end(); }
444 } // end namespace llvm
446 /// Returns Attribute::None, Attribute::ReadOnly or Attribute::ReadNone.
447 static Attribute::AttrKind
448 determinePointerReadAttrs(Argument
*A
,
449 const SmallPtrSet
<Argument
*, 8> &SCCNodes
) {
450 SmallVector
<Use
*, 32> Worklist
;
451 SmallPtrSet
<Use
*, 32> Visited
;
453 // inalloca arguments are always clobbered by the call.
454 if (A
->hasInAllocaAttr())
455 return Attribute::None
;
458 // We don't need to track IsWritten. If A is written to, return immediately.
460 for (Use
&U
: A
->uses()) {
462 Worklist
.push_back(&U
);
465 while (!Worklist
.empty()) {
466 Use
*U
= Worklist
.pop_back_val();
467 Instruction
*I
= cast
<Instruction
>(U
->getUser());
469 switch (I
->getOpcode()) {
470 case Instruction::BitCast
:
471 case Instruction::GetElementPtr
:
472 case Instruction::PHI
:
473 case Instruction::Select
:
474 case Instruction::AddrSpaceCast
:
475 // The original value is not read/written via this if the new value isn't.
476 for (Use
&UU
: I
->uses())
477 if (Visited
.insert(&UU
).second
)
478 Worklist
.push_back(&UU
);
481 case Instruction::Call
:
482 case Instruction::Invoke
: {
483 bool Captures
= true;
485 if (I
->getType()->isVoidTy())
488 auto AddUsersToWorklistIfCapturing
= [&] {
490 for (Use
&UU
: I
->uses())
491 if (Visited
.insert(&UU
).second
)
492 Worklist
.push_back(&UU
);
496 if (CS
.doesNotAccessMemory()) {
497 AddUsersToWorklistIfCapturing();
501 Function
*F
= CS
.getCalledFunction();
503 if (CS
.onlyReadsMemory()) {
505 AddUsersToWorklistIfCapturing();
508 return Attribute::None
;
511 // Note: the callee and the two successor blocks *follow* the argument
512 // operands. This means there is no need to adjust UseIndex to account
515 unsigned UseIndex
= std::distance(CS
.arg_begin(), U
);
517 // U cannot be the callee operand use: since we're exploring the
518 // transitive uses of an Argument, having such a use be a callee would
519 // imply the CallSite is an indirect call or invoke; and we'd take the
521 assert(UseIndex
< CS
.data_operands_size() &&
522 "Data operand use expected!");
524 bool IsOperandBundleUse
= UseIndex
>= CS
.getNumArgOperands();
526 if (UseIndex
>= F
->arg_size() && !IsOperandBundleUse
) {
527 assert(F
->isVarArg() && "More params than args in non-varargs call");
528 return Attribute::None
;
531 Captures
&= !CS
.doesNotCapture(UseIndex
);
533 // Since the optimizer (by design) cannot see the data flow corresponding
534 // to a operand bundle use, these cannot participate in the optimistic SCC
535 // analysis. Instead, we model the operand bundle uses as arguments in
536 // call to a function external to the SCC.
537 if (IsOperandBundleUse
||
538 !SCCNodes
.count(&*std::next(F
->arg_begin(), UseIndex
))) {
540 // The accessors used on CallSite here do the right thing for calls and
541 // invokes with operand bundles.
543 if (!CS
.onlyReadsMemory() && !CS
.onlyReadsMemory(UseIndex
))
544 return Attribute::None
;
545 if (!CS
.doesNotAccessMemory(UseIndex
))
549 AddUsersToWorklistIfCapturing();
553 case Instruction::Load
:
554 // A volatile load has side effects beyond what readonly can be relied
556 if (cast
<LoadInst
>(I
)->isVolatile())
557 return Attribute::None
;
562 case Instruction::ICmp
:
563 case Instruction::Ret
:
567 return Attribute::None
;
571 return IsRead
? Attribute::ReadOnly
: Attribute::ReadNone
;
574 /// Deduce returned attributes for the SCC.
575 static bool addArgumentReturnedAttrs(const SCCNodeSet
&SCCNodes
) {
576 bool Changed
= false;
578 // Check each function in turn, determining if an argument is always returned.
579 for (Function
*F
: SCCNodes
) {
580 // We can infer and propagate function attributes only when we know that the
581 // definition we'll get at link time is *exactly* the definition we see now.
582 // For more details, see GlobalValue::mayBeDerefined.
583 if (!F
->hasExactDefinition())
586 if (F
->getReturnType()->isVoidTy())
589 // There is nothing to do if an argument is already marked as 'returned'.
590 if (llvm::any_of(F
->args(),
591 [](const Argument
&Arg
) { return Arg
.hasReturnedAttr(); }))
594 auto FindRetArg
= [&]() -> Value
* {
595 Value
*RetArg
= nullptr;
596 for (BasicBlock
&BB
: *F
)
597 if (auto *Ret
= dyn_cast
<ReturnInst
>(BB
.getTerminator())) {
598 // Note that stripPointerCasts should look through functions with
599 // returned arguments.
600 Value
*RetVal
= Ret
->getReturnValue()->stripPointerCasts();
601 if (!isa
<Argument
>(RetVal
) || RetVal
->getType() != F
->getReturnType())
606 else if (RetArg
!= RetVal
)
613 if (Value
*RetArg
= FindRetArg()) {
614 auto *A
= cast
<Argument
>(RetArg
);
615 A
->addAttr(Attribute::Returned
);
624 /// If a callsite has arguments that are also arguments to the parent function,
625 /// try to propagate attributes from the callsite's arguments to the parent's
626 /// arguments. This may be important because inlining can cause information loss
627 /// when attribute knowledge disappears with the inlined call.
628 static bool addArgumentAttrsFromCallsites(Function
&F
) {
629 if (!EnableNonnullArgPropagation
)
632 bool Changed
= false;
634 // For an argument attribute to transfer from a callsite to the parent, the
635 // call must be guaranteed to execute every time the parent is called.
636 // Conservatively, just check for calls in the entry block that are guaranteed
638 // TODO: This could be enhanced by testing if the callsite post-dominates the
639 // entry block or by doing simple forward walks or backward walks to the
641 BasicBlock
&Entry
= F
.getEntryBlock();
642 for (Instruction
&I
: Entry
) {
643 if (auto CS
= CallSite(&I
)) {
644 if (auto *CalledFunc
= CS
.getCalledFunction()) {
645 for (auto &CSArg
: CalledFunc
->args()) {
646 if (!CSArg
.hasNonNullAttr())
649 // If the non-null callsite argument operand is an argument to 'F'
650 // (the caller) and the call is guaranteed to execute, then the value
651 // must be non-null throughout 'F'.
652 auto *FArg
= dyn_cast
<Argument
>(CS
.getArgOperand(CSArg
.getArgNo()));
653 if (FArg
&& !FArg
->hasNonNullAttr()) {
654 FArg
->addAttr(Attribute::NonNull
);
660 if (!isGuaranteedToTransferExecutionToSuccessor(&I
))
667 static bool addReadAttr(Argument
*A
, Attribute::AttrKind R
) {
668 assert((R
== Attribute::ReadOnly
|| R
== Attribute::ReadNone
)
669 && "Must be a Read attribute.");
670 assert(A
&& "Argument must not be null.");
672 // If the argument already has the attribute, nothing needs to be done.
673 if (A
->hasAttribute(R
))
676 // Otherwise, remove potentially conflicting attribute, add the new one,
677 // and update statistics.
678 A
->removeAttr(Attribute::WriteOnly
);
679 A
->removeAttr(Attribute::ReadOnly
);
680 A
->removeAttr(Attribute::ReadNone
);
682 R
== Attribute::ReadOnly
? ++NumReadOnlyArg
: ++NumReadNoneArg
;
686 /// Deduce nocapture attributes for the SCC.
687 static bool addArgumentAttrs(const SCCNodeSet
&SCCNodes
) {
688 bool Changed
= false;
692 // Check each function in turn, determining which pointer arguments are not
694 for (Function
*F
: SCCNodes
) {
695 // We can infer and propagate function attributes only when we know that the
696 // definition we'll get at link time is *exactly* the definition we see now.
697 // For more details, see GlobalValue::mayBeDerefined.
698 if (!F
->hasExactDefinition())
701 Changed
|= addArgumentAttrsFromCallsites(*F
);
703 // Functions that are readonly (or readnone) and nounwind and don't return
704 // a value can't capture arguments. Don't analyze them.
705 if (F
->onlyReadsMemory() && F
->doesNotThrow() &&
706 F
->getReturnType()->isVoidTy()) {
707 for (Function::arg_iterator A
= F
->arg_begin(), E
= F
->arg_end(); A
!= E
;
709 if (A
->getType()->isPointerTy() && !A
->hasNoCaptureAttr()) {
710 A
->addAttr(Attribute::NoCapture
);
718 for (Function::arg_iterator A
= F
->arg_begin(), E
= F
->arg_end(); A
!= E
;
720 if (!A
->getType()->isPointerTy())
722 bool HasNonLocalUses
= false;
723 if (!A
->hasNoCaptureAttr()) {
724 ArgumentUsesTracker
Tracker(SCCNodes
);
725 PointerMayBeCaptured(&*A
, &Tracker
);
726 if (!Tracker
.Captured
) {
727 if (Tracker
.Uses
.empty()) {
728 // If it's trivially not captured, mark it nocapture now.
729 A
->addAttr(Attribute::NoCapture
);
733 // If it's not trivially captured and not trivially not captured,
734 // then it must be calling into another function in our SCC. Save
735 // its particulars for Argument-SCC analysis later.
736 ArgumentGraphNode
*Node
= AG
[&*A
];
737 for (Argument
*Use
: Tracker
.Uses
) {
738 Node
->Uses
.push_back(AG
[Use
]);
740 HasNonLocalUses
= true;
744 // Otherwise, it's captured. Don't bother doing SCC analysis on it.
746 if (!HasNonLocalUses
&& !A
->onlyReadsMemory()) {
747 // Can we determine that it's readonly/readnone without doing an SCC?
748 // Note that we don't allow any calls at all here, or else our result
749 // will be dependent on the iteration order through the functions in the
751 SmallPtrSet
<Argument
*, 8> Self
;
753 Attribute::AttrKind R
= determinePointerReadAttrs(&*A
, Self
);
754 if (R
!= Attribute::None
)
755 Changed
= addReadAttr(A
, R
);
760 // The graph we've collected is partial because we stopped scanning for
761 // argument uses once we solved the argument trivially. These partial nodes
762 // show up as ArgumentGraphNode objects with an empty Uses list, and for
763 // these nodes the final decision about whether they capture has already been
764 // made. If the definition doesn't have a 'nocapture' attribute by now, it
767 for (scc_iterator
<ArgumentGraph
*> I
= scc_begin(&AG
); !I
.isAtEnd(); ++I
) {
768 const std::vector
<ArgumentGraphNode
*> &ArgumentSCC
= *I
;
769 if (ArgumentSCC
.size() == 1) {
770 if (!ArgumentSCC
[0]->Definition
)
771 continue; // synthetic root node
773 // eg. "void f(int* x) { if (...) f(x); }"
774 if (ArgumentSCC
[0]->Uses
.size() == 1 &&
775 ArgumentSCC
[0]->Uses
[0] == ArgumentSCC
[0]) {
776 Argument
*A
= ArgumentSCC
[0]->Definition
;
777 A
->addAttr(Attribute::NoCapture
);
784 bool SCCCaptured
= false;
785 for (auto I
= ArgumentSCC
.begin(), E
= ArgumentSCC
.end();
786 I
!= E
&& !SCCCaptured
; ++I
) {
787 ArgumentGraphNode
*Node
= *I
;
788 if (Node
->Uses
.empty()) {
789 if (!Node
->Definition
->hasNoCaptureAttr())
796 SmallPtrSet
<Argument
*, 8> ArgumentSCCNodes
;
797 // Fill ArgumentSCCNodes with the elements of the ArgumentSCC. Used for
798 // quickly looking up whether a given Argument is in this ArgumentSCC.
799 for (ArgumentGraphNode
*I
: ArgumentSCC
) {
800 ArgumentSCCNodes
.insert(I
->Definition
);
803 for (auto I
= ArgumentSCC
.begin(), E
= ArgumentSCC
.end();
804 I
!= E
&& !SCCCaptured
; ++I
) {
805 ArgumentGraphNode
*N
= *I
;
806 for (ArgumentGraphNode
*Use
: N
->Uses
) {
807 Argument
*A
= Use
->Definition
;
808 if (A
->hasNoCaptureAttr() || ArgumentSCCNodes
.count(A
))
817 for (unsigned i
= 0, e
= ArgumentSCC
.size(); i
!= e
; ++i
) {
818 Argument
*A
= ArgumentSCC
[i
]->Definition
;
819 A
->addAttr(Attribute::NoCapture
);
824 // We also want to compute readonly/readnone. With a small number of false
825 // negatives, we can assume that any pointer which is captured isn't going
826 // to be provably readonly or readnone, since by definition we can't
827 // analyze all uses of a captured pointer.
829 // The false negatives happen when the pointer is captured by a function
830 // that promises readonly/readnone behaviour on the pointer, then the
831 // pointer's lifetime ends before anything that writes to arbitrary memory.
832 // Also, a readonly/readnone pointer may be returned, but returning a
833 // pointer is capturing it.
835 Attribute::AttrKind ReadAttr
= Attribute::ReadNone
;
836 for (unsigned i
= 0, e
= ArgumentSCC
.size(); i
!= e
; ++i
) {
837 Argument
*A
= ArgumentSCC
[i
]->Definition
;
838 Attribute::AttrKind K
= determinePointerReadAttrs(A
, ArgumentSCCNodes
);
839 if (K
== Attribute::ReadNone
)
841 if (K
== Attribute::ReadOnly
) {
842 ReadAttr
= Attribute::ReadOnly
;
849 if (ReadAttr
!= Attribute::None
) {
850 for (unsigned i
= 0, e
= ArgumentSCC
.size(); i
!= e
; ++i
) {
851 Argument
*A
= ArgumentSCC
[i
]->Definition
;
852 Changed
= addReadAttr(A
, ReadAttr
);
860 /// Tests whether a function is "malloc-like".
862 /// A function is "malloc-like" if it returns either null or a pointer that
863 /// doesn't alias any other pointer visible to the caller.
864 static bool isFunctionMallocLike(Function
*F
, const SCCNodeSet
&SCCNodes
) {
865 SmallSetVector
<Value
*, 8> FlowsToReturn
;
866 for (BasicBlock
&BB
: *F
)
867 if (ReturnInst
*Ret
= dyn_cast
<ReturnInst
>(BB
.getTerminator()))
868 FlowsToReturn
.insert(Ret
->getReturnValue());
870 for (unsigned i
= 0; i
!= FlowsToReturn
.size(); ++i
) {
871 Value
*RetVal
= FlowsToReturn
[i
];
873 if (Constant
*C
= dyn_cast
<Constant
>(RetVal
)) {
874 if (!C
->isNullValue() && !isa
<UndefValue
>(C
))
880 if (isa
<Argument
>(RetVal
))
883 if (Instruction
*RVI
= dyn_cast
<Instruction
>(RetVal
))
884 switch (RVI
->getOpcode()) {
885 // Extend the analysis by looking upwards.
886 case Instruction::BitCast
:
887 case Instruction::GetElementPtr
:
888 case Instruction::AddrSpaceCast
:
889 FlowsToReturn
.insert(RVI
->getOperand(0));
891 case Instruction::Select
: {
892 SelectInst
*SI
= cast
<SelectInst
>(RVI
);
893 FlowsToReturn
.insert(SI
->getTrueValue());
894 FlowsToReturn
.insert(SI
->getFalseValue());
897 case Instruction::PHI
: {
898 PHINode
*PN
= cast
<PHINode
>(RVI
);
899 for (Value
*IncValue
: PN
->incoming_values())
900 FlowsToReturn
.insert(IncValue
);
904 // Check whether the pointer came from an allocation.
905 case Instruction::Alloca
:
907 case Instruction::Call
:
908 case Instruction::Invoke
: {
910 if (CS
.hasRetAttr(Attribute::NoAlias
))
912 if (CS
.getCalledFunction() && SCCNodes
.count(CS
.getCalledFunction()))
917 return false; // Did not come from an allocation.
920 if (PointerMayBeCaptured(RetVal
, false, /*StoreCaptures=*/false))
927 /// Deduce noalias attributes for the SCC.
928 static bool addNoAliasAttrs(const SCCNodeSet
&SCCNodes
) {
929 // Check each function in turn, determining which functions return noalias
931 for (Function
*F
: SCCNodes
) {
933 if (F
->returnDoesNotAlias())
936 // We can infer and propagate function attributes only when we know that the
937 // definition we'll get at link time is *exactly* the definition we see now.
938 // For more details, see GlobalValue::mayBeDerefined.
939 if (!F
->hasExactDefinition())
942 // We annotate noalias return values, which are only applicable to
944 if (!F
->getReturnType()->isPointerTy())
947 if (!isFunctionMallocLike(F
, SCCNodes
))
951 bool MadeChange
= false;
952 for (Function
*F
: SCCNodes
) {
953 if (F
->returnDoesNotAlias() ||
954 !F
->getReturnType()->isPointerTy())
957 F
->setReturnDoesNotAlias();
965 /// Tests whether this function is known to not return null.
967 /// Requires that the function returns a pointer.
969 /// Returns true if it believes the function will not return a null, and sets
970 /// \p Speculative based on whether the returned conclusion is a speculative
971 /// conclusion due to SCC calls.
972 static bool isReturnNonNull(Function
*F
, const SCCNodeSet
&SCCNodes
,
974 assert(F
->getReturnType()->isPointerTy() &&
975 "nonnull only meaningful on pointer types");
978 SmallSetVector
<Value
*, 8> FlowsToReturn
;
979 for (BasicBlock
&BB
: *F
)
980 if (auto *Ret
= dyn_cast
<ReturnInst
>(BB
.getTerminator()))
981 FlowsToReturn
.insert(Ret
->getReturnValue());
983 auto &DL
= F
->getParent()->getDataLayout();
985 for (unsigned i
= 0; i
!= FlowsToReturn
.size(); ++i
) {
986 Value
*RetVal
= FlowsToReturn
[i
];
988 // If this value is locally known to be non-null, we're good
989 if (isKnownNonZero(RetVal
, DL
))
992 // Otherwise, we need to look upwards since we can't make any local
994 Instruction
*RVI
= dyn_cast
<Instruction
>(RetVal
);
997 switch (RVI
->getOpcode()) {
998 // Extend the analysis by looking upwards.
999 case Instruction::BitCast
:
1000 case Instruction::GetElementPtr
:
1001 case Instruction::AddrSpaceCast
:
1002 FlowsToReturn
.insert(RVI
->getOperand(0));
1004 case Instruction::Select
: {
1005 SelectInst
*SI
= cast
<SelectInst
>(RVI
);
1006 FlowsToReturn
.insert(SI
->getTrueValue());
1007 FlowsToReturn
.insert(SI
->getFalseValue());
1010 case Instruction::PHI
: {
1011 PHINode
*PN
= cast
<PHINode
>(RVI
);
1012 for (int i
= 0, e
= PN
->getNumIncomingValues(); i
!= e
; ++i
)
1013 FlowsToReturn
.insert(PN
->getIncomingValue(i
));
1016 case Instruction::Call
:
1017 case Instruction::Invoke
: {
1019 Function
*Callee
= CS
.getCalledFunction();
1020 // A call to a node within the SCC is assumed to return null until
1022 if (Callee
&& SCCNodes
.count(Callee
)) {
1029 return false; // Unknown source, may be null
1031 llvm_unreachable("should have either continued or returned");
1037 /// Deduce nonnull attributes for the SCC.
1038 static bool addNonNullAttrs(const SCCNodeSet
&SCCNodes
) {
1039 // Speculative that all functions in the SCC return only nonnull
1040 // pointers. We may refute this as we analyze functions.
1041 bool SCCReturnsNonNull
= true;
1043 bool MadeChange
= false;
1045 // Check each function in turn, determining which functions return nonnull
1047 for (Function
*F
: SCCNodes
) {
1049 if (F
->getAttributes().hasAttribute(AttributeList::ReturnIndex
,
1050 Attribute::NonNull
))
1053 // We can infer and propagate function attributes only when we know that the
1054 // definition we'll get at link time is *exactly* the definition we see now.
1055 // For more details, see GlobalValue::mayBeDerefined.
1056 if (!F
->hasExactDefinition())
1059 // We annotate nonnull return values, which are only applicable to
1061 if (!F
->getReturnType()->isPointerTy())
1064 bool Speculative
= false;
1065 if (isReturnNonNull(F
, SCCNodes
, Speculative
)) {
1067 // Mark the function eagerly since we may discover a function
1068 // which prevents us from speculating about the entire SCC
1069 LLVM_DEBUG(dbgs() << "Eagerly marking " << F
->getName()
1070 << " as nonnull\n");
1071 F
->addAttribute(AttributeList::ReturnIndex
, Attribute::NonNull
);
1077 // At least one function returns something which could be null, can't
1078 // speculate any more.
1079 SCCReturnsNonNull
= false;
1082 if (SCCReturnsNonNull
) {
1083 for (Function
*F
: SCCNodes
) {
1084 if (F
->getAttributes().hasAttribute(AttributeList::ReturnIndex
,
1085 Attribute::NonNull
) ||
1086 !F
->getReturnType()->isPointerTy())
1089 LLVM_DEBUG(dbgs() << "SCC marking " << F
->getName() << " as nonnull\n");
1090 F
->addAttribute(AttributeList::ReturnIndex
, Attribute::NonNull
);
1101 /// Collects a set of attribute inference requests and performs them all in one
1102 /// go on a single SCC Node. Inference involves scanning function bodies
1103 /// looking for instructions that violate attribute assumptions.
1104 /// As soon as all the bodies are fine we are free to set the attribute.
1105 /// Customization of inference for individual attributes is performed by
1106 /// providing a handful of predicates for each attribute.
1107 class AttributeInferer
{
1109 /// Describes a request for inference of a single attribute.
1110 struct InferenceDescriptor
{
1112 /// Returns true if this function does not have to be handled.
1113 /// General intent for this predicate is to provide an optimization
1114 /// for functions that do not need this attribute inference at all
1115 /// (say, for functions that already have the attribute).
1116 std::function
<bool(const Function
&)> SkipFunction
;
1118 /// Returns true if this instruction violates attribute assumptions.
1119 std::function
<bool(Instruction
&)> InstrBreaksAttribute
;
1121 /// Sets the inferred attribute for this function.
1122 std::function
<void(Function
&)> SetAttribute
;
1124 /// Attribute we derive.
1125 Attribute::AttrKind AKind
;
1127 /// If true, only "exact" definitions can be used to infer this attribute.
1128 /// See GlobalValue::isDefinitionExact.
1129 bool RequiresExactDefinition
;
1131 InferenceDescriptor(Attribute::AttrKind AK
,
1132 std::function
<bool(const Function
&)> SkipFunc
,
1133 std::function
<bool(Instruction
&)> InstrScan
,
1134 std::function
<void(Function
&)> SetAttr
,
1136 : SkipFunction(SkipFunc
), InstrBreaksAttribute(InstrScan
),
1137 SetAttribute(SetAttr
), AKind(AK
),
1138 RequiresExactDefinition(ReqExactDef
) {}
1142 SmallVector
<InferenceDescriptor
, 4> InferenceDescriptors
;
1145 void registerAttrInference(InferenceDescriptor AttrInference
) {
1146 InferenceDescriptors
.push_back(AttrInference
);
1149 bool run(const SCCNodeSet
&SCCNodes
);
1152 /// Perform all the requested attribute inference actions according to the
1153 /// attribute predicates stored before.
1154 bool AttributeInferer::run(const SCCNodeSet
&SCCNodes
) {
1155 SmallVector
<InferenceDescriptor
, 4> InferInSCC
= InferenceDescriptors
;
1156 // Go through all the functions in SCC and check corresponding attribute
1157 // assumptions for each of them. Attributes that are invalid for this SCC
1158 // will be removed from InferInSCC.
1159 for (Function
*F
: SCCNodes
) {
1161 // No attributes whose assumptions are still valid - done.
1162 if (InferInSCC
.empty())
1165 // Check if our attributes ever need scanning/can be scanned.
1166 llvm::erase_if(InferInSCC
, [F
](const InferenceDescriptor
&ID
) {
1167 if (ID
.SkipFunction(*F
))
1170 // Remove from further inference (invalidate) when visiting a function
1171 // that has no instructions to scan/has an unsuitable definition.
1172 return F
->isDeclaration() ||
1173 (ID
.RequiresExactDefinition
&& !F
->hasExactDefinition());
1176 // For each attribute still in InferInSCC that doesn't explicitly skip F,
1177 // set up the F instructions scan to verify assumptions of the attribute.
1178 SmallVector
<InferenceDescriptor
, 4> InferInThisFunc
;
1180 InferInSCC
, std::back_inserter(InferInThisFunc
),
1181 [F
](const InferenceDescriptor
&ID
) { return !ID
.SkipFunction(*F
); });
1183 if (InferInThisFunc
.empty())
1186 // Start instruction scan.
1187 for (Instruction
&I
: instructions(*F
)) {
1188 llvm::erase_if(InferInThisFunc
, [&](const InferenceDescriptor
&ID
) {
1189 if (!ID
.InstrBreaksAttribute(I
))
1191 // Remove attribute from further inference on any other functions
1192 // because attribute assumptions have just been violated.
1193 llvm::erase_if(InferInSCC
, [&ID
](const InferenceDescriptor
&D
) {
1194 return D
.AKind
== ID
.AKind
;
1196 // Remove attribute from the rest of current instruction scan.
1200 if (InferInThisFunc
.empty())
1205 if (InferInSCC
.empty())
1208 bool Changed
= false;
1209 for (Function
*F
: SCCNodes
)
1210 // At this point InferInSCC contains only functions that were either:
1211 // - explicitly skipped from scan/inference, or
1212 // - verified to have no instructions that break attribute assumptions.
1213 // Hence we just go and force the attribute for all non-skipped functions.
1214 for (auto &ID
: InferInSCC
) {
1215 if (ID
.SkipFunction(*F
))
1218 ID
.SetAttribute(*F
);
1223 } // end anonymous namespace
1225 /// Helper for non-Convergent inference predicate InstrBreaksAttribute.
1226 static bool InstrBreaksNonConvergent(Instruction
&I
,
1227 const SCCNodeSet
&SCCNodes
) {
1228 const CallSite
CS(&I
);
1229 // Breaks non-convergent assumption if CS is a convergent call to a function
1231 return CS
&& CS
.isConvergent() && SCCNodes
.count(CS
.getCalledFunction()) == 0;
1234 /// Helper for NoUnwind inference predicate InstrBreaksAttribute.
1235 static bool InstrBreaksNonThrowing(Instruction
&I
, const SCCNodeSet
&SCCNodes
) {
1238 if (const auto *CI
= dyn_cast
<CallInst
>(&I
)) {
1239 if (Function
*Callee
= CI
->getCalledFunction()) {
1240 // I is a may-throw call to a function inside our SCC. This doesn't
1241 // invalidate our current working assumption that the SCC is no-throw; we
1242 // just have to scan that other function.
1243 if (SCCNodes
.count(Callee
) > 0)
1250 /// Helper for NoFree inference predicate InstrBreaksAttribute.
1251 static bool InstrBreaksNoFree(Instruction
&I
, const SCCNodeSet
&SCCNodes
) {
1256 Function
*Callee
= CS
.getCalledFunction();
1260 if (Callee
->doesNotFreeMemory())
1263 if (SCCNodes
.count(Callee
) > 0)
1269 /// Infer attributes from all functions in the SCC by scanning every
1270 /// instruction for compliance to the attribute assumptions. Currently it
1272 /// - removal of Convergent attribute
1273 /// - addition of NoUnwind attribute
1275 /// Returns true if any changes to function attributes were made.
1276 static bool inferAttrsFromFunctionBodies(const SCCNodeSet
&SCCNodes
) {
1278 AttributeInferer AI
;
1280 // Request to remove the convergent attribute from all functions in the SCC
1281 // if every callsite within the SCC is not convergent (except for calls
1282 // to functions within the SCC).
1283 // Note: Removal of the attr from the callsites will happen in
1284 // InstCombineCalls separately.
1285 AI
.registerAttrInference(AttributeInferer::InferenceDescriptor
{
1286 Attribute::Convergent
,
1287 // Skip non-convergent functions.
1288 [](const Function
&F
) { return !F
.isConvergent(); },
1289 // Instructions that break non-convergent assumption.
1290 [SCCNodes
](Instruction
&I
) {
1291 return InstrBreaksNonConvergent(I
, SCCNodes
);
1294 LLVM_DEBUG(dbgs() << "Removing convergent attr from fn " << F
.getName()
1296 F
.setNotConvergent();
1298 /* RequiresExactDefinition= */ false});
1300 if (!DisableNoUnwindInference
)
1301 // Request to infer nounwind attribute for all the functions in the SCC if
1302 // every callsite within the SCC is not throwing (except for calls to
1303 // functions within the SCC). Note that nounwind attribute suffers from
1304 // derefinement - results may change depending on how functions are
1305 // optimized. Thus it can be inferred only from exact definitions.
1306 AI
.registerAttrInference(AttributeInferer::InferenceDescriptor
{
1307 Attribute::NoUnwind
,
1308 // Skip non-throwing functions.
1309 [](const Function
&F
) { return F
.doesNotThrow(); },
1310 // Instructions that break non-throwing assumption.
1311 [SCCNodes
](Instruction
&I
) {
1312 return InstrBreaksNonThrowing(I
, SCCNodes
);
1316 << "Adding nounwind attr to fn " << F
.getName() << "\n");
1317 F
.setDoesNotThrow();
1320 /* RequiresExactDefinition= */ true});
1322 if (!DisableNoFreeInference
)
1323 // Request to infer nofree attribute for all the functions in the SCC if
1324 // every callsite within the SCC does not directly or indirectly free
1325 // memory (except for calls to functions within the SCC). Note that nofree
1326 // attribute suffers from derefinement - results may change depending on
1327 // how functions are optimized. Thus it can be inferred only from exact
1329 AI
.registerAttrInference(AttributeInferer::InferenceDescriptor
{
1331 // Skip functions known not to free memory.
1332 [](const Function
&F
) { return F
.doesNotFreeMemory(); },
1333 // Instructions that break non-deallocating assumption.
1334 [SCCNodes
](Instruction
&I
) {
1335 return InstrBreaksNoFree(I
, SCCNodes
);
1339 << "Adding nofree attr to fn " << F
.getName() << "\n");
1340 F
.setDoesNotFreeMemory();
1343 /* RequiresExactDefinition= */ true});
1345 // Perform all the requested attribute inference actions.
1346 return AI
.run(SCCNodes
);
1349 static bool setDoesNotRecurse(Function
&F
) {
1350 if (F
.doesNotRecurse())
1352 F
.setDoesNotRecurse();
1357 static bool addNoRecurseAttrs(const SCCNodeSet
&SCCNodes
) {
1358 // Try and identify functions that do not recurse.
1360 // If the SCC contains multiple nodes we know for sure there is recursion.
1361 if (SCCNodes
.size() != 1)
1364 Function
*F
= *SCCNodes
.begin();
1365 if (!F
|| !F
->hasExactDefinition() || F
->doesNotRecurse())
1368 // If all of the calls in F are identifiable and are to norecurse functions, F
1369 // is norecurse. This check also detects self-recursion as F is not currently
1370 // marked norecurse, so any called from F to F will not be marked norecurse.
1372 for (auto &I
: BB
.instructionsWithoutDebug())
1373 if (auto CS
= CallSite(&I
)) {
1374 Function
*Callee
= CS
.getCalledFunction();
1375 if (!Callee
|| Callee
== F
|| !Callee
->doesNotRecurse())
1376 // Function calls a potentially recursive function.
1380 // Every call was to a non-recursive function other than this function, and
1381 // we have no indirect recursion as the SCC size is one. This function cannot
1383 return setDoesNotRecurse(*F
);
1386 template <typename AARGetterT
>
1387 static bool deriveAttrsInPostOrder(SCCNodeSet
&SCCNodes
,
1388 AARGetterT
&&AARGetter
,
1389 bool HasUnknownCall
) {
1390 bool Changed
= false;
1392 // Bail if the SCC only contains optnone functions.
1393 if (SCCNodes
.empty())
1396 Changed
|= addArgumentReturnedAttrs(SCCNodes
);
1397 Changed
|= addReadAttrs(SCCNodes
, AARGetter
);
1398 Changed
|= addArgumentAttrs(SCCNodes
);
1400 // If we have no external nodes participating in the SCC, we can deduce some
1401 // more precise attributes as well.
1402 if (!HasUnknownCall
) {
1403 Changed
|= addNoAliasAttrs(SCCNodes
);
1404 Changed
|= addNonNullAttrs(SCCNodes
);
1405 Changed
|= inferAttrsFromFunctionBodies(SCCNodes
);
1406 Changed
|= addNoRecurseAttrs(SCCNodes
);
1412 PreservedAnalyses
PostOrderFunctionAttrsPass::run(LazyCallGraph::SCC
&C
,
1413 CGSCCAnalysisManager
&AM
,
1415 CGSCCUpdateResult
&) {
1416 FunctionAnalysisManager
&FAM
=
1417 AM
.getResult
<FunctionAnalysisManagerCGSCCProxy
>(C
, CG
).getManager();
1419 // We pass a lambda into functions to wire them up to the analysis manager
1420 // for getting function analyses.
1421 auto AARGetter
= [&](Function
&F
) -> AAResults
& {
1422 return FAM
.getResult
<AAManager
>(F
);
1425 // Fill SCCNodes with the elements of the SCC. Also track whether there are
1426 // any external or opt-none nodes that will prevent us from optimizing any
1428 SCCNodeSet SCCNodes
;
1429 bool HasUnknownCall
= false;
1430 for (LazyCallGraph::Node
&N
: C
) {
1431 Function
&F
= N
.getFunction();
1432 if (F
.hasOptNone() || F
.hasFnAttribute(Attribute::Naked
)) {
1433 // Treat any function we're trying not to optimize as if it were an
1434 // indirect call and omit it from the node set used below.
1435 HasUnknownCall
= true;
1438 // Track whether any functions in this SCC have an unknown call edge.
1439 // Note: if this is ever a performance hit, we can common it with
1440 // subsequent routines which also do scans over the instructions of the
1442 if (!HasUnknownCall
)
1443 for (Instruction
&I
: instructions(F
))
1444 if (auto CS
= CallSite(&I
))
1445 if (!CS
.getCalledFunction()) {
1446 HasUnknownCall
= true;
1450 SCCNodes
.insert(&F
);
1453 if (deriveAttrsInPostOrder(SCCNodes
, AARGetter
, HasUnknownCall
))
1454 return PreservedAnalyses::none();
1456 return PreservedAnalyses::all();
1461 struct PostOrderFunctionAttrsLegacyPass
: public CallGraphSCCPass
{
1462 // Pass identification, replacement for typeid
1465 PostOrderFunctionAttrsLegacyPass() : CallGraphSCCPass(ID
) {
1466 initializePostOrderFunctionAttrsLegacyPassPass(
1467 *PassRegistry::getPassRegistry());
1470 bool runOnSCC(CallGraphSCC
&SCC
) override
;
1472 void getAnalysisUsage(AnalysisUsage
&AU
) const override
{
1473 AU
.setPreservesCFG();
1474 AU
.addRequired
<AssumptionCacheTracker
>();
1475 getAAResultsAnalysisUsage(AU
);
1476 CallGraphSCCPass::getAnalysisUsage(AU
);
1480 } // end anonymous namespace
1482 char PostOrderFunctionAttrsLegacyPass::ID
= 0;
1483 INITIALIZE_PASS_BEGIN(PostOrderFunctionAttrsLegacyPass
, "functionattrs",
1484 "Deduce function attributes", false, false)
1485 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker
)
1486 INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass
)
1487 INITIALIZE_PASS_END(PostOrderFunctionAttrsLegacyPass
, "functionattrs",
1488 "Deduce function attributes", false, false)
1490 Pass
*llvm::createPostOrderFunctionAttrsLegacyPass() {
1491 return new PostOrderFunctionAttrsLegacyPass();
1494 template <typename AARGetterT
>
1495 static bool runImpl(CallGraphSCC
&SCC
, AARGetterT AARGetter
) {
1497 // Fill SCCNodes with the elements of the SCC. Used for quickly looking up
1498 // whether a given CallGraphNode is in this SCC. Also track whether there are
1499 // any external or opt-none nodes that will prevent us from optimizing any
1501 SCCNodeSet SCCNodes
;
1502 bool ExternalNode
= false;
1503 for (CallGraphNode
*I
: SCC
) {
1504 Function
*F
= I
->getFunction();
1505 if (!F
|| F
->hasOptNone() || F
->hasFnAttribute(Attribute::Naked
)) {
1506 // External node or function we're trying not to optimize - we both avoid
1507 // transform them and avoid leveraging information they provide.
1508 ExternalNode
= true;
1515 return deriveAttrsInPostOrder(SCCNodes
, AARGetter
, ExternalNode
);
1518 bool PostOrderFunctionAttrsLegacyPass::runOnSCC(CallGraphSCC
&SCC
) {
1521 return runImpl(SCC
, LegacyAARGetter(*this));
1526 struct ReversePostOrderFunctionAttrsLegacyPass
: public ModulePass
{
1527 // Pass identification, replacement for typeid
1530 ReversePostOrderFunctionAttrsLegacyPass() : ModulePass(ID
) {
1531 initializeReversePostOrderFunctionAttrsLegacyPassPass(
1532 *PassRegistry::getPassRegistry());
1535 bool runOnModule(Module
&M
) override
;
1537 void getAnalysisUsage(AnalysisUsage
&AU
) const override
{
1538 AU
.setPreservesCFG();
1539 AU
.addRequired
<CallGraphWrapperPass
>();
1540 AU
.addPreserved
<CallGraphWrapperPass
>();
1544 } // end anonymous namespace
1546 char ReversePostOrderFunctionAttrsLegacyPass::ID
= 0;
1548 INITIALIZE_PASS_BEGIN(ReversePostOrderFunctionAttrsLegacyPass
, "rpo-functionattrs",
1549 "Deduce function attributes in RPO", false, false)
1550 INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass
)
1551 INITIALIZE_PASS_END(ReversePostOrderFunctionAttrsLegacyPass
, "rpo-functionattrs",
1552 "Deduce function attributes in RPO", false, false)
1554 Pass
*llvm::createReversePostOrderFunctionAttrsPass() {
1555 return new ReversePostOrderFunctionAttrsLegacyPass();
1558 static bool addNoRecurseAttrsTopDown(Function
&F
) {
1559 // We check the preconditions for the function prior to calling this to avoid
1560 // the cost of building up a reversible post-order list. We assert them here
1561 // to make sure none of the invariants this relies on were violated.
1562 assert(!F
.isDeclaration() && "Cannot deduce norecurse without a definition!");
1563 assert(!F
.doesNotRecurse() &&
1564 "This function has already been deduced as norecurs!");
1565 assert(F
.hasInternalLinkage() &&
1566 "Can only do top-down deduction for internal linkage functions!");
1568 // If F is internal and all of its uses are calls from a non-recursive
1569 // functions, then none of its calls could in fact recurse without going
1570 // through a function marked norecurse, and so we can mark this function too
1571 // as norecurse. Note that the uses must actually be calls -- otherwise
1572 // a pointer to this function could be returned from a norecurse function but
1573 // this function could be recursively (indirectly) called. Note that this
1574 // also detects if F is directly recursive as F is not yet marked as
1575 // a norecurse function.
1576 for (auto *U
: F
.users()) {
1577 auto *I
= dyn_cast
<Instruction
>(U
);
1581 if (!CS
|| !CS
.getParent()->getParent()->doesNotRecurse())
1584 return setDoesNotRecurse(F
);
1587 static bool deduceFunctionAttributeInRPO(Module
&M
, CallGraph
&CG
) {
1588 // We only have a post-order SCC traversal (because SCCs are inherently
1589 // discovered in post-order), so we accumulate them in a vector and then walk
1590 // it in reverse. This is simpler than using the RPO iterator infrastructure
1591 // because we need to combine SCC detection and the PO walk of the call
1592 // graph. We can also cheat egregiously because we're primarily interested in
1593 // synthesizing norecurse and so we can only save the singular SCCs as SCCs
1594 // with multiple functions in them will clearly be recursive.
1595 SmallVector
<Function
*, 16> Worklist
;
1596 for (scc_iterator
<CallGraph
*> I
= scc_begin(&CG
); !I
.isAtEnd(); ++I
) {
1600 Function
*F
= I
->front()->getFunction();
1601 if (F
&& !F
->isDeclaration() && !F
->doesNotRecurse() &&
1602 F
->hasInternalLinkage())
1603 Worklist
.push_back(F
);
1606 bool Changed
= false;
1607 for (auto *F
: llvm::reverse(Worklist
))
1608 Changed
|= addNoRecurseAttrsTopDown(*F
);
1613 bool ReversePostOrderFunctionAttrsLegacyPass::runOnModule(Module
&M
) {
1617 auto &CG
= getAnalysis
<CallGraphWrapperPass
>().getCallGraph();
1619 return deduceFunctionAttributeInRPO(M
, CG
);
1623 ReversePostOrderFunctionAttrsPass::run(Module
&M
, ModuleAnalysisManager
&AM
) {
1624 auto &CG
= AM
.getResult
<CallGraphAnalysis
>(M
);
1626 if (!deduceFunctionAttributeInRPO(M
, CG
))
1627 return PreservedAnalyses::all();
1629 PreservedAnalyses PA
;
1630 PA
.preserve
<CallGraphAnalysis
>();