[llvm-exegesis] [NFC] Fixing typo.
[llvm-complete.git] / lib / Transforms / IPO / FunctionAttrs.cpp
blob77e98ecd9103cfa7d4bd10294fe808985c53bb91
1 //===- FunctionAttrs.cpp - Pass which marks functions attributes ----------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 /// \file
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/MemoryLocation.h"
31 #include "llvm/Analysis/ValueTracking.h"
32 #include "llvm/IR/Argument.h"
33 #include "llvm/IR/Attributes.h"
34 #include "llvm/IR/BasicBlock.h"
35 #include "llvm/IR/CallSite.h"
36 #include "llvm/IR/Constant.h"
37 #include "llvm/IR/Constants.h"
38 #include "llvm/IR/Function.h"
39 #include "llvm/IR/InstIterator.h"
40 #include "llvm/IR/InstrTypes.h"
41 #include "llvm/IR/Instruction.h"
42 #include "llvm/IR/Instructions.h"
43 #include "llvm/IR/IntrinsicInst.h"
44 #include "llvm/IR/Metadata.h"
45 #include "llvm/IR/PassManager.h"
46 #include "llvm/IR/Type.h"
47 #include "llvm/IR/Use.h"
48 #include "llvm/IR/User.h"
49 #include "llvm/IR/Value.h"
50 #include "llvm/Pass.h"
51 #include "llvm/Support/Casting.h"
52 #include "llvm/Support/CommandLine.h"
53 #include "llvm/Support/Compiler.h"
54 #include "llvm/Support/Debug.h"
55 #include "llvm/Support/ErrorHandling.h"
56 #include "llvm/Support/raw_ostream.h"
57 #include "llvm/Transforms/IPO.h"
58 #include <cassert>
59 #include <iterator>
60 #include <map>
61 #include <vector>
63 using namespace llvm;
65 #define DEBUG_TYPE "functionattrs"
67 STATISTIC(NumReadNone, "Number of functions marked readnone");
68 STATISTIC(NumReadOnly, "Number of functions marked readonly");
69 STATISTIC(NumWriteOnly, "Number of functions marked writeonly");
70 STATISTIC(NumNoCapture, "Number of arguments marked nocapture");
71 STATISTIC(NumReturned, "Number of arguments marked returned");
72 STATISTIC(NumReadNoneArg, "Number of arguments marked readnone");
73 STATISTIC(NumReadOnlyArg, "Number of arguments marked readonly");
74 STATISTIC(NumNoAlias, "Number of function returns marked noalias");
75 STATISTIC(NumNonNullReturn, "Number of function returns marked nonnull");
76 STATISTIC(NumNoRecurse, "Number of functions marked as norecurse");
77 STATISTIC(NumNoUnwind, "Number of functions marked as nounwind");
79 // FIXME: This is disabled by default to avoid exposing security vulnerabilities
80 // in C/C++ code compiled by clang:
81 // http://lists.llvm.org/pipermail/cfe-dev/2017-January/052066.html
82 static cl::opt<bool> EnableNonnullArgPropagation(
83 "enable-nonnull-arg-prop", cl::Hidden,
84 cl::desc("Try to propagate nonnull argument attributes from callsites to "
85 "caller functions."));
87 static cl::opt<bool> DisableNoUnwindInference(
88 "disable-nounwind-inference", cl::Hidden,
89 cl::desc("Stop inferring nounwind attribute during function-attrs pass"));
91 namespace {
93 using SCCNodeSet = SmallSetVector<Function *, 8>;
95 } // end anonymous namespace
97 /// Returns the memory access attribute for function F using AAR for AA results,
98 /// where SCCNodes is the current SCC.
99 ///
100 /// If ThisBody is true, this function may examine the function body and will
101 /// return a result pertaining to this copy of the function. If it is false, the
102 /// result will be based only on AA results for the function declaration; it
103 /// will be assumed that some other (perhaps less optimized) version of the
104 /// function may be selected at link time.
105 static MemoryAccessKind checkFunctionMemoryAccess(Function &F, bool ThisBody,
106 AAResults &AAR,
107 const SCCNodeSet &SCCNodes) {
108 FunctionModRefBehavior MRB = AAR.getModRefBehavior(&F);
109 if (MRB == FMRB_DoesNotAccessMemory)
110 // Already perfect!
111 return MAK_ReadNone;
113 if (!ThisBody) {
114 if (AliasAnalysis::onlyReadsMemory(MRB))
115 return MAK_ReadOnly;
117 if (AliasAnalysis::doesNotReadMemory(MRB))
118 return MAK_WriteOnly;
120 // Conservatively assume it reads and writes to memory.
121 return MAK_MayWrite;
124 // Scan the function body for instructions that may read or write memory.
125 bool ReadsMemory = false;
126 bool WritesMemory = 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 if (auto *Call = dyn_cast<CallBase>(I)) {
133 // Ignore calls to functions in the same SCC, as long as the call sites
134 // don't have operand bundles. Calls with operand bundles are allowed to
135 // have memory effects not described by the memory effects of the call
136 // target.
137 if (!Call->hasOperandBundles() && Call->getCalledFunction() &&
138 SCCNodes.count(Call->getCalledFunction()))
139 continue;
140 FunctionModRefBehavior MRB = AAR.getModRefBehavior(Call);
141 ModRefInfo MRI = createModRefInfo(MRB);
143 // If the call doesn't access memory, we're done.
144 if (isNoModRef(MRI))
145 continue;
147 if (!AliasAnalysis::onlyAccessesArgPointees(MRB)) {
148 // The call could access any memory. If that includes writes, note it.
149 if (isModSet(MRI))
150 WritesMemory = true;
151 // If it reads, note it.
152 if (isRefSet(MRI))
153 ReadsMemory = true;
154 continue;
157 // Check whether all pointer arguments point to local memory, and
158 // ignore calls that only access local memory.
159 for (CallSite::arg_iterator CI = Call->arg_begin(), CE = Call->arg_end();
160 CI != CE; ++CI) {
161 Value *Arg = *CI;
162 if (!Arg->getType()->isPtrOrPtrVectorTy())
163 continue;
165 AAMDNodes AAInfo;
166 I->getAAMetadata(AAInfo);
167 MemoryLocation Loc(Arg, LocationSize::unknown(), AAInfo);
169 // Skip accesses to local or constant memory as they don't impact the
170 // externally visible mod/ref behavior.
171 if (AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true))
172 continue;
174 if (isModSet(MRI))
175 // Writes non-local memory.
176 WritesMemory = true;
177 if (isRefSet(MRI))
178 // Ok, it reads non-local memory.
179 ReadsMemory = true;
181 continue;
182 } else if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
183 // Ignore non-volatile loads from local memory. (Atomic is okay here.)
184 if (!LI->isVolatile()) {
185 MemoryLocation Loc = MemoryLocation::get(LI);
186 if (AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true))
187 continue;
189 } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
190 // Ignore non-volatile stores to local memory. (Atomic is okay here.)
191 if (!SI->isVolatile()) {
192 MemoryLocation Loc = MemoryLocation::get(SI);
193 if (AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true))
194 continue;
196 } else if (VAArgInst *VI = dyn_cast<VAArgInst>(I)) {
197 // Ignore vaargs on local memory.
198 MemoryLocation Loc = MemoryLocation::get(VI);
199 if (AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true))
200 continue;
203 // Any remaining instructions need to be taken seriously! Check if they
204 // read or write memory.
206 // Writes memory, remember that.
207 WritesMemory |= I->mayWriteToMemory();
209 // If this instruction may read memory, remember that.
210 ReadsMemory |= I->mayReadFromMemory();
213 if (WritesMemory) {
214 if (!ReadsMemory)
215 return MAK_WriteOnly;
216 else
217 return MAK_MayWrite;
220 return ReadsMemory ? MAK_ReadOnly : MAK_ReadNone;
223 MemoryAccessKind llvm::computeFunctionBodyMemoryAccess(Function &F,
224 AAResults &AAR) {
225 return checkFunctionMemoryAccess(F, /*ThisBody=*/true, AAR, {});
228 /// Deduce readonly/readnone attributes for the SCC.
229 template <typename AARGetterT>
230 static bool addReadAttrs(const SCCNodeSet &SCCNodes, AARGetterT &&AARGetter) {
231 // Check if any of the functions in the SCC read or write memory. If they
232 // write memory then they can't be marked readnone or readonly.
233 bool ReadsMemory = false;
234 bool WritesMemory = false;
235 for (Function *F : SCCNodes) {
236 // Call the callable parameter to look up AA results for this function.
237 AAResults &AAR = AARGetter(*F);
239 // Non-exact function definitions may not be selected at link time, and an
240 // alternative version that writes to memory may be selected. See the
241 // comment on GlobalValue::isDefinitionExact for more details.
242 switch (checkFunctionMemoryAccess(*F, F->hasExactDefinition(),
243 AAR, SCCNodes)) {
244 case MAK_MayWrite:
245 return false;
246 case MAK_ReadOnly:
247 ReadsMemory = true;
248 break;
249 case MAK_WriteOnly:
250 WritesMemory = true;
251 break;
252 case MAK_ReadNone:
253 // Nothing to do!
254 break;
258 // Success! Functions in this SCC do not access memory, or only read memory.
259 // Give them the appropriate attribute.
260 bool MadeChange = false;
262 assert(!(ReadsMemory && WritesMemory) &&
263 "Function marked read-only and write-only");
264 for (Function *F : SCCNodes) {
265 if (F->doesNotAccessMemory())
266 // Already perfect!
267 continue;
269 if (F->onlyReadsMemory() && ReadsMemory)
270 // No change.
271 continue;
273 if (F->doesNotReadMemory() && WritesMemory)
274 continue;
276 MadeChange = true;
278 // Clear out any existing attributes.
279 F->removeFnAttr(Attribute::ReadOnly);
280 F->removeFnAttr(Attribute::ReadNone);
281 F->removeFnAttr(Attribute::WriteOnly);
283 if (!WritesMemory && !ReadsMemory) {
284 // Clear out any "access range attributes" if readnone was deduced.
285 F->removeFnAttr(Attribute::ArgMemOnly);
286 F->removeFnAttr(Attribute::InaccessibleMemOnly);
287 F->removeFnAttr(Attribute::InaccessibleMemOrArgMemOnly);
290 // Add in the new attribute.
291 if (WritesMemory && !ReadsMemory)
292 F->addFnAttr(Attribute::WriteOnly);
293 else
294 F->addFnAttr(ReadsMemory ? Attribute::ReadOnly : Attribute::ReadNone);
296 if (WritesMemory && !ReadsMemory)
297 ++NumWriteOnly;
298 else if (ReadsMemory)
299 ++NumReadOnly;
300 else
301 ++NumReadNone;
304 return MadeChange;
307 namespace {
309 /// For a given pointer Argument, this retains a list of Arguments of functions
310 /// in the same SCC that the pointer data flows into. We use this to build an
311 /// SCC of the arguments.
312 struct ArgumentGraphNode {
313 Argument *Definition;
314 SmallVector<ArgumentGraphNode *, 4> Uses;
317 class ArgumentGraph {
318 // We store pointers to ArgumentGraphNode objects, so it's important that
319 // that they not move around upon insert.
320 using ArgumentMapTy = std::map<Argument *, ArgumentGraphNode>;
322 ArgumentMapTy ArgumentMap;
324 // There is no root node for the argument graph, in fact:
325 // void f(int *x, int *y) { if (...) f(x, y); }
326 // is an example where the graph is disconnected. The SCCIterator requires a
327 // single entry point, so we maintain a fake ("synthetic") root node that
328 // uses every node. Because the graph is directed and nothing points into
329 // the root, it will not participate in any SCCs (except for its own).
330 ArgumentGraphNode SyntheticRoot;
332 public:
333 ArgumentGraph() { SyntheticRoot.Definition = nullptr; }
335 using iterator = SmallVectorImpl<ArgumentGraphNode *>::iterator;
337 iterator begin() { return SyntheticRoot.Uses.begin(); }
338 iterator end() { return SyntheticRoot.Uses.end(); }
339 ArgumentGraphNode *getEntryNode() { return &SyntheticRoot; }
341 ArgumentGraphNode *operator[](Argument *A) {
342 ArgumentGraphNode &Node = ArgumentMap[A];
343 Node.Definition = A;
344 SyntheticRoot.Uses.push_back(&Node);
345 return &Node;
349 /// This tracker checks whether callees are in the SCC, and if so it does not
350 /// consider that a capture, instead adding it to the "Uses" list and
351 /// continuing with the analysis.
352 struct ArgumentUsesTracker : public CaptureTracker {
353 ArgumentUsesTracker(const SCCNodeSet &SCCNodes) : SCCNodes(SCCNodes) {}
355 void tooManyUses() override { Captured = true; }
357 bool captured(const Use *U) override {
358 CallSite CS(U->getUser());
359 if (!CS.getInstruction()) {
360 Captured = true;
361 return true;
364 Function *F = CS.getCalledFunction();
365 if (!F || !F->hasExactDefinition() || !SCCNodes.count(F)) {
366 Captured = true;
367 return true;
370 // Note: the callee and the two successor blocks *follow* the argument
371 // operands. This means there is no need to adjust UseIndex to account for
372 // these.
374 unsigned UseIndex =
375 std::distance(const_cast<const Use *>(CS.arg_begin()), U);
377 assert(UseIndex < CS.data_operands_size() &&
378 "Indirect function calls should have been filtered above!");
380 if (UseIndex >= CS.getNumArgOperands()) {
381 // Data operand, but not a argument operand -- must be a bundle operand
382 assert(CS.hasOperandBundles() && "Must be!");
384 // CaptureTracking told us that we're being captured by an operand bundle
385 // use. In this case it does not matter if the callee is within our SCC
386 // or not -- we've been captured in some unknown way, and we have to be
387 // conservative.
388 Captured = true;
389 return true;
392 if (UseIndex >= F->arg_size()) {
393 assert(F->isVarArg() && "More params than args in non-varargs call");
394 Captured = true;
395 return true;
398 Uses.push_back(&*std::next(F->arg_begin(), UseIndex));
399 return false;
402 // True only if certainly captured (used outside our SCC).
403 bool Captured = false;
405 // Uses within our SCC.
406 SmallVector<Argument *, 4> Uses;
408 const SCCNodeSet &SCCNodes;
411 } // end anonymous namespace
413 namespace llvm {
415 template <> struct GraphTraits<ArgumentGraphNode *> {
416 using NodeRef = ArgumentGraphNode *;
417 using ChildIteratorType = SmallVectorImpl<ArgumentGraphNode *>::iterator;
419 static NodeRef getEntryNode(NodeRef A) { return A; }
420 static ChildIteratorType child_begin(NodeRef N) { return N->Uses.begin(); }
421 static ChildIteratorType child_end(NodeRef N) { return N->Uses.end(); }
424 template <>
425 struct GraphTraits<ArgumentGraph *> : public GraphTraits<ArgumentGraphNode *> {
426 static NodeRef getEntryNode(ArgumentGraph *AG) { return AG->getEntryNode(); }
428 static ChildIteratorType nodes_begin(ArgumentGraph *AG) {
429 return AG->begin();
432 static ChildIteratorType nodes_end(ArgumentGraph *AG) { return AG->end(); }
435 } // end namespace llvm
437 /// Returns Attribute::None, Attribute::ReadOnly or Attribute::ReadNone.
438 static Attribute::AttrKind
439 determinePointerReadAttrs(Argument *A,
440 const SmallPtrSet<Argument *, 8> &SCCNodes) {
441 SmallVector<Use *, 32> Worklist;
442 SmallPtrSet<Use *, 32> Visited;
444 // inalloca arguments are always clobbered by the call.
445 if (A->hasInAllocaAttr())
446 return Attribute::None;
448 bool IsRead = false;
449 // We don't need to track IsWritten. If A is written to, return immediately.
451 for (Use &U : A->uses()) {
452 Visited.insert(&U);
453 Worklist.push_back(&U);
456 while (!Worklist.empty()) {
457 Use *U = Worklist.pop_back_val();
458 Instruction *I = cast<Instruction>(U->getUser());
460 switch (I->getOpcode()) {
461 case Instruction::BitCast:
462 case Instruction::GetElementPtr:
463 case Instruction::PHI:
464 case Instruction::Select:
465 case Instruction::AddrSpaceCast:
466 // The original value is not read/written via this if the new value isn't.
467 for (Use &UU : I->uses())
468 if (Visited.insert(&UU).second)
469 Worklist.push_back(&UU);
470 break;
472 case Instruction::Call:
473 case Instruction::Invoke: {
474 bool Captures = true;
476 if (I->getType()->isVoidTy())
477 Captures = false;
479 auto AddUsersToWorklistIfCapturing = [&] {
480 if (Captures)
481 for (Use &UU : I->uses())
482 if (Visited.insert(&UU).second)
483 Worklist.push_back(&UU);
486 CallSite CS(I);
487 if (CS.doesNotAccessMemory()) {
488 AddUsersToWorklistIfCapturing();
489 continue;
492 Function *F = CS.getCalledFunction();
493 if (!F) {
494 if (CS.onlyReadsMemory()) {
495 IsRead = true;
496 AddUsersToWorklistIfCapturing();
497 continue;
499 return Attribute::None;
502 // Note: the callee and the two successor blocks *follow* the argument
503 // operands. This means there is no need to adjust UseIndex to account
504 // for these.
506 unsigned UseIndex = std::distance(CS.arg_begin(), U);
508 // U cannot be the callee operand use: since we're exploring the
509 // transitive uses of an Argument, having such a use be a callee would
510 // imply the CallSite is an indirect call or invoke; and we'd take the
511 // early exit above.
512 assert(UseIndex < CS.data_operands_size() &&
513 "Data operand use expected!");
515 bool IsOperandBundleUse = UseIndex >= CS.getNumArgOperands();
517 if (UseIndex >= F->arg_size() && !IsOperandBundleUse) {
518 assert(F->isVarArg() && "More params than args in non-varargs call");
519 return Attribute::None;
522 Captures &= !CS.doesNotCapture(UseIndex);
524 // Since the optimizer (by design) cannot see the data flow corresponding
525 // to a operand bundle use, these cannot participate in the optimistic SCC
526 // analysis. Instead, we model the operand bundle uses as arguments in
527 // call to a function external to the SCC.
528 if (IsOperandBundleUse ||
529 !SCCNodes.count(&*std::next(F->arg_begin(), UseIndex))) {
531 // The accessors used on CallSite here do the right thing for calls and
532 // invokes with operand bundles.
534 if (!CS.onlyReadsMemory() && !CS.onlyReadsMemory(UseIndex))
535 return Attribute::None;
536 if (!CS.doesNotAccessMemory(UseIndex))
537 IsRead = true;
540 AddUsersToWorklistIfCapturing();
541 break;
544 case Instruction::Load:
545 // A volatile load has side effects beyond what readonly can be relied
546 // upon.
547 if (cast<LoadInst>(I)->isVolatile())
548 return Attribute::None;
550 IsRead = true;
551 break;
553 case Instruction::ICmp:
554 case Instruction::Ret:
555 break;
557 default:
558 return Attribute::None;
562 return IsRead ? Attribute::ReadOnly : Attribute::ReadNone;
565 /// Deduce returned attributes for the SCC.
566 static bool addArgumentReturnedAttrs(const SCCNodeSet &SCCNodes) {
567 bool Changed = false;
569 // Check each function in turn, determining if an argument is always returned.
570 for (Function *F : SCCNodes) {
571 // We can infer and propagate function attributes only when we know that the
572 // definition we'll get at link time is *exactly* the definition we see now.
573 // For more details, see GlobalValue::mayBeDerefined.
574 if (!F->hasExactDefinition())
575 continue;
577 if (F->getReturnType()->isVoidTy())
578 continue;
580 // There is nothing to do if an argument is already marked as 'returned'.
581 if (llvm::any_of(F->args(),
582 [](const Argument &Arg) { return Arg.hasReturnedAttr(); }))
583 continue;
585 auto FindRetArg = [&]() -> Value * {
586 Value *RetArg = nullptr;
587 for (BasicBlock &BB : *F)
588 if (auto *Ret = dyn_cast<ReturnInst>(BB.getTerminator())) {
589 // Note that stripPointerCasts should look through functions with
590 // returned arguments.
591 Value *RetVal = Ret->getReturnValue()->stripPointerCasts();
592 if (!isa<Argument>(RetVal) || RetVal->getType() != F->getReturnType())
593 return nullptr;
595 if (!RetArg)
596 RetArg = RetVal;
597 else if (RetArg != RetVal)
598 return nullptr;
601 return RetArg;
604 if (Value *RetArg = FindRetArg()) {
605 auto *A = cast<Argument>(RetArg);
606 A->addAttr(Attribute::Returned);
607 ++NumReturned;
608 Changed = true;
612 return Changed;
615 /// If a callsite has arguments that are also arguments to the parent function,
616 /// try to propagate attributes from the callsite's arguments to the parent's
617 /// arguments. This may be important because inlining can cause information loss
618 /// when attribute knowledge disappears with the inlined call.
619 static bool addArgumentAttrsFromCallsites(Function &F) {
620 if (!EnableNonnullArgPropagation)
621 return false;
623 bool Changed = false;
625 // For an argument attribute to transfer from a callsite to the parent, the
626 // call must be guaranteed to execute every time the parent is called.
627 // Conservatively, just check for calls in the entry block that are guaranteed
628 // to execute.
629 // TODO: This could be enhanced by testing if the callsite post-dominates the
630 // entry block or by doing simple forward walks or backward walks to the
631 // callsite.
632 BasicBlock &Entry = F.getEntryBlock();
633 for (Instruction &I : Entry) {
634 if (auto CS = CallSite(&I)) {
635 if (auto *CalledFunc = CS.getCalledFunction()) {
636 for (auto &CSArg : CalledFunc->args()) {
637 if (!CSArg.hasNonNullAttr())
638 continue;
640 // If the non-null callsite argument operand is an argument to 'F'
641 // (the caller) and the call is guaranteed to execute, then the value
642 // must be non-null throughout 'F'.
643 auto *FArg = dyn_cast<Argument>(CS.getArgOperand(CSArg.getArgNo()));
644 if (FArg && !FArg->hasNonNullAttr()) {
645 FArg->addAttr(Attribute::NonNull);
646 Changed = true;
651 if (!isGuaranteedToTransferExecutionToSuccessor(&I))
652 break;
655 return Changed;
658 /// Deduce nocapture attributes for the SCC.
659 static bool addArgumentAttrs(const SCCNodeSet &SCCNodes) {
660 bool Changed = false;
662 ArgumentGraph AG;
664 // Check each function in turn, determining which pointer arguments are not
665 // captured.
666 for (Function *F : SCCNodes) {
667 // We can infer and propagate function attributes only when we know that the
668 // definition we'll get at link time is *exactly* the definition we see now.
669 // For more details, see GlobalValue::mayBeDerefined.
670 if (!F->hasExactDefinition())
671 continue;
673 Changed |= addArgumentAttrsFromCallsites(*F);
675 // Functions that are readonly (or readnone) and nounwind and don't return
676 // a value can't capture arguments. Don't analyze them.
677 if (F->onlyReadsMemory() && F->doesNotThrow() &&
678 F->getReturnType()->isVoidTy()) {
679 for (Function::arg_iterator A = F->arg_begin(), E = F->arg_end(); A != E;
680 ++A) {
681 if (A->getType()->isPointerTy() && !A->hasNoCaptureAttr()) {
682 A->addAttr(Attribute::NoCapture);
683 ++NumNoCapture;
684 Changed = true;
687 continue;
690 for (Function::arg_iterator A = F->arg_begin(), E = F->arg_end(); A != E;
691 ++A) {
692 if (!A->getType()->isPointerTy())
693 continue;
694 bool HasNonLocalUses = false;
695 if (!A->hasNoCaptureAttr()) {
696 ArgumentUsesTracker Tracker(SCCNodes);
697 PointerMayBeCaptured(&*A, &Tracker);
698 if (!Tracker.Captured) {
699 if (Tracker.Uses.empty()) {
700 // If it's trivially not captured, mark it nocapture now.
701 A->addAttr(Attribute::NoCapture);
702 ++NumNoCapture;
703 Changed = true;
704 } else {
705 // If it's not trivially captured and not trivially not captured,
706 // then it must be calling into another function in our SCC. Save
707 // its particulars for Argument-SCC analysis later.
708 ArgumentGraphNode *Node = AG[&*A];
709 for (Argument *Use : Tracker.Uses) {
710 Node->Uses.push_back(AG[Use]);
711 if (Use != &*A)
712 HasNonLocalUses = true;
716 // Otherwise, it's captured. Don't bother doing SCC analysis on it.
718 if (!HasNonLocalUses && !A->onlyReadsMemory()) {
719 // Can we determine that it's readonly/readnone without doing an SCC?
720 // Note that we don't allow any calls at all here, or else our result
721 // will be dependent on the iteration order through the functions in the
722 // SCC.
723 SmallPtrSet<Argument *, 8> Self;
724 Self.insert(&*A);
725 Attribute::AttrKind R = determinePointerReadAttrs(&*A, Self);
726 if (R != Attribute::None) {
727 A->addAttr(R);
728 Changed = true;
729 R == Attribute::ReadOnly ? ++NumReadOnlyArg : ++NumReadNoneArg;
735 // The graph we've collected is partial because we stopped scanning for
736 // argument uses once we solved the argument trivially. These partial nodes
737 // show up as ArgumentGraphNode objects with an empty Uses list, and for
738 // these nodes the final decision about whether they capture has already been
739 // made. If the definition doesn't have a 'nocapture' attribute by now, it
740 // captures.
742 for (scc_iterator<ArgumentGraph *> I = scc_begin(&AG); !I.isAtEnd(); ++I) {
743 const std::vector<ArgumentGraphNode *> &ArgumentSCC = *I;
744 if (ArgumentSCC.size() == 1) {
745 if (!ArgumentSCC[0]->Definition)
746 continue; // synthetic root node
748 // eg. "void f(int* x) { if (...) f(x); }"
749 if (ArgumentSCC[0]->Uses.size() == 1 &&
750 ArgumentSCC[0]->Uses[0] == ArgumentSCC[0]) {
751 Argument *A = ArgumentSCC[0]->Definition;
752 A->addAttr(Attribute::NoCapture);
753 ++NumNoCapture;
754 Changed = true;
756 continue;
759 bool SCCCaptured = false;
760 for (auto I = ArgumentSCC.begin(), E = ArgumentSCC.end();
761 I != E && !SCCCaptured; ++I) {
762 ArgumentGraphNode *Node = *I;
763 if (Node->Uses.empty()) {
764 if (!Node->Definition->hasNoCaptureAttr())
765 SCCCaptured = true;
768 if (SCCCaptured)
769 continue;
771 SmallPtrSet<Argument *, 8> ArgumentSCCNodes;
772 // Fill ArgumentSCCNodes with the elements of the ArgumentSCC. Used for
773 // quickly looking up whether a given Argument is in this ArgumentSCC.
774 for (ArgumentGraphNode *I : ArgumentSCC) {
775 ArgumentSCCNodes.insert(I->Definition);
778 for (auto I = ArgumentSCC.begin(), E = ArgumentSCC.end();
779 I != E && !SCCCaptured; ++I) {
780 ArgumentGraphNode *N = *I;
781 for (ArgumentGraphNode *Use : N->Uses) {
782 Argument *A = Use->Definition;
783 if (A->hasNoCaptureAttr() || ArgumentSCCNodes.count(A))
784 continue;
785 SCCCaptured = true;
786 break;
789 if (SCCCaptured)
790 continue;
792 for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) {
793 Argument *A = ArgumentSCC[i]->Definition;
794 A->addAttr(Attribute::NoCapture);
795 ++NumNoCapture;
796 Changed = true;
799 // We also want to compute readonly/readnone. With a small number of false
800 // negatives, we can assume that any pointer which is captured isn't going
801 // to be provably readonly or readnone, since by definition we can't
802 // analyze all uses of a captured pointer.
804 // The false negatives happen when the pointer is captured by a function
805 // that promises readonly/readnone behaviour on the pointer, then the
806 // pointer's lifetime ends before anything that writes to arbitrary memory.
807 // Also, a readonly/readnone pointer may be returned, but returning a
808 // pointer is capturing it.
810 Attribute::AttrKind ReadAttr = Attribute::ReadNone;
811 for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) {
812 Argument *A = ArgumentSCC[i]->Definition;
813 Attribute::AttrKind K = determinePointerReadAttrs(A, ArgumentSCCNodes);
814 if (K == Attribute::ReadNone)
815 continue;
816 if (K == Attribute::ReadOnly) {
817 ReadAttr = Attribute::ReadOnly;
818 continue;
820 ReadAttr = K;
821 break;
824 if (ReadAttr != Attribute::None) {
825 for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) {
826 Argument *A = ArgumentSCC[i]->Definition;
827 // Clear out existing readonly/readnone attributes
828 A->removeAttr(Attribute::ReadOnly);
829 A->removeAttr(Attribute::ReadNone);
830 A->addAttr(ReadAttr);
831 ReadAttr == Attribute::ReadOnly ? ++NumReadOnlyArg : ++NumReadNoneArg;
832 Changed = true;
837 return Changed;
840 /// Tests whether a function is "malloc-like".
842 /// A function is "malloc-like" if it returns either null or a pointer that
843 /// doesn't alias any other pointer visible to the caller.
844 static bool isFunctionMallocLike(Function *F, const SCCNodeSet &SCCNodes) {
845 SmallSetVector<Value *, 8> FlowsToReturn;
846 for (BasicBlock &BB : *F)
847 if (ReturnInst *Ret = dyn_cast<ReturnInst>(BB.getTerminator()))
848 FlowsToReturn.insert(Ret->getReturnValue());
850 for (unsigned i = 0; i != FlowsToReturn.size(); ++i) {
851 Value *RetVal = FlowsToReturn[i];
853 if (Constant *C = dyn_cast<Constant>(RetVal)) {
854 if (!C->isNullValue() && !isa<UndefValue>(C))
855 return false;
857 continue;
860 if (isa<Argument>(RetVal))
861 return false;
863 if (Instruction *RVI = dyn_cast<Instruction>(RetVal))
864 switch (RVI->getOpcode()) {
865 // Extend the analysis by looking upwards.
866 case Instruction::BitCast:
867 case Instruction::GetElementPtr:
868 case Instruction::AddrSpaceCast:
869 FlowsToReturn.insert(RVI->getOperand(0));
870 continue;
871 case Instruction::Select: {
872 SelectInst *SI = cast<SelectInst>(RVI);
873 FlowsToReturn.insert(SI->getTrueValue());
874 FlowsToReturn.insert(SI->getFalseValue());
875 continue;
877 case Instruction::PHI: {
878 PHINode *PN = cast<PHINode>(RVI);
879 for (Value *IncValue : PN->incoming_values())
880 FlowsToReturn.insert(IncValue);
881 continue;
884 // Check whether the pointer came from an allocation.
885 case Instruction::Alloca:
886 break;
887 case Instruction::Call:
888 case Instruction::Invoke: {
889 CallSite CS(RVI);
890 if (CS.hasRetAttr(Attribute::NoAlias))
891 break;
892 if (CS.getCalledFunction() && SCCNodes.count(CS.getCalledFunction()))
893 break;
894 LLVM_FALLTHROUGH;
896 default:
897 return false; // Did not come from an allocation.
900 if (PointerMayBeCaptured(RetVal, false, /*StoreCaptures=*/false))
901 return false;
904 return true;
907 /// Deduce noalias attributes for the SCC.
908 static bool addNoAliasAttrs(const SCCNodeSet &SCCNodes) {
909 // Check each function in turn, determining which functions return noalias
910 // pointers.
911 for (Function *F : SCCNodes) {
912 // Already noalias.
913 if (F->returnDoesNotAlias())
914 continue;
916 // We can infer and propagate function attributes only when we know that the
917 // definition we'll get at link time is *exactly* the definition we see now.
918 // For more details, see GlobalValue::mayBeDerefined.
919 if (!F->hasExactDefinition())
920 return false;
922 // We annotate noalias return values, which are only applicable to
923 // pointer types.
924 if (!F->getReturnType()->isPointerTy())
925 continue;
927 if (!isFunctionMallocLike(F, SCCNodes))
928 return false;
931 bool MadeChange = false;
932 for (Function *F : SCCNodes) {
933 if (F->returnDoesNotAlias() ||
934 !F->getReturnType()->isPointerTy())
935 continue;
937 F->setReturnDoesNotAlias();
938 ++NumNoAlias;
939 MadeChange = true;
942 return MadeChange;
945 /// Tests whether this function is known to not return null.
947 /// Requires that the function returns a pointer.
949 /// Returns true if it believes the function will not return a null, and sets
950 /// \p Speculative based on whether the returned conclusion is a speculative
951 /// conclusion due to SCC calls.
952 static bool isReturnNonNull(Function *F, const SCCNodeSet &SCCNodes,
953 bool &Speculative) {
954 assert(F->getReturnType()->isPointerTy() &&
955 "nonnull only meaningful on pointer types");
956 Speculative = false;
958 SmallSetVector<Value *, 8> FlowsToReturn;
959 for (BasicBlock &BB : *F)
960 if (auto *Ret = dyn_cast<ReturnInst>(BB.getTerminator()))
961 FlowsToReturn.insert(Ret->getReturnValue());
963 auto &DL = F->getParent()->getDataLayout();
965 for (unsigned i = 0; i != FlowsToReturn.size(); ++i) {
966 Value *RetVal = FlowsToReturn[i];
968 // If this value is locally known to be non-null, we're good
969 if (isKnownNonZero(RetVal, DL))
970 continue;
972 // Otherwise, we need to look upwards since we can't make any local
973 // conclusions.
974 Instruction *RVI = dyn_cast<Instruction>(RetVal);
975 if (!RVI)
976 return false;
977 switch (RVI->getOpcode()) {
978 // Extend the analysis by looking upwards.
979 case Instruction::BitCast:
980 case Instruction::GetElementPtr:
981 case Instruction::AddrSpaceCast:
982 FlowsToReturn.insert(RVI->getOperand(0));
983 continue;
984 case Instruction::Select: {
985 SelectInst *SI = cast<SelectInst>(RVI);
986 FlowsToReturn.insert(SI->getTrueValue());
987 FlowsToReturn.insert(SI->getFalseValue());
988 continue;
990 case Instruction::PHI: {
991 PHINode *PN = cast<PHINode>(RVI);
992 for (int i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
993 FlowsToReturn.insert(PN->getIncomingValue(i));
994 continue;
996 case Instruction::Call:
997 case Instruction::Invoke: {
998 CallSite CS(RVI);
999 Function *Callee = CS.getCalledFunction();
1000 // A call to a node within the SCC is assumed to return null until
1001 // proven otherwise
1002 if (Callee && SCCNodes.count(Callee)) {
1003 Speculative = true;
1004 continue;
1006 return false;
1008 default:
1009 return false; // Unknown source, may be null
1011 llvm_unreachable("should have either continued or returned");
1014 return true;
1017 /// Deduce nonnull attributes for the SCC.
1018 static bool addNonNullAttrs(const SCCNodeSet &SCCNodes) {
1019 // Speculative that all functions in the SCC return only nonnull
1020 // pointers. We may refute this as we analyze functions.
1021 bool SCCReturnsNonNull = true;
1023 bool MadeChange = false;
1025 // Check each function in turn, determining which functions return nonnull
1026 // pointers.
1027 for (Function *F : SCCNodes) {
1028 // Already nonnull.
1029 if (F->getAttributes().hasAttribute(AttributeList::ReturnIndex,
1030 Attribute::NonNull))
1031 continue;
1033 // We can infer and propagate function attributes only when we know that the
1034 // definition we'll get at link time is *exactly* the definition we see now.
1035 // For more details, see GlobalValue::mayBeDerefined.
1036 if (!F->hasExactDefinition())
1037 return false;
1039 // We annotate nonnull return values, which are only applicable to
1040 // pointer types.
1041 if (!F->getReturnType()->isPointerTy())
1042 continue;
1044 bool Speculative = false;
1045 if (isReturnNonNull(F, SCCNodes, Speculative)) {
1046 if (!Speculative) {
1047 // Mark the function eagerly since we may discover a function
1048 // which prevents us from speculating about the entire SCC
1049 LLVM_DEBUG(dbgs() << "Eagerly marking " << F->getName()
1050 << " as nonnull\n");
1051 F->addAttribute(AttributeList::ReturnIndex, Attribute::NonNull);
1052 ++NumNonNullReturn;
1053 MadeChange = true;
1055 continue;
1057 // At least one function returns something which could be null, can't
1058 // speculate any more.
1059 SCCReturnsNonNull = false;
1062 if (SCCReturnsNonNull) {
1063 for (Function *F : SCCNodes) {
1064 if (F->getAttributes().hasAttribute(AttributeList::ReturnIndex,
1065 Attribute::NonNull) ||
1066 !F->getReturnType()->isPointerTy())
1067 continue;
1069 LLVM_DEBUG(dbgs() << "SCC marking " << F->getName() << " as nonnull\n");
1070 F->addAttribute(AttributeList::ReturnIndex, Attribute::NonNull);
1071 ++NumNonNullReturn;
1072 MadeChange = true;
1076 return MadeChange;
1079 namespace {
1081 /// Collects a set of attribute inference requests and performs them all in one
1082 /// go on a single SCC Node. Inference involves scanning function bodies
1083 /// looking for instructions that violate attribute assumptions.
1084 /// As soon as all the bodies are fine we are free to set the attribute.
1085 /// Customization of inference for individual attributes is performed by
1086 /// providing a handful of predicates for each attribute.
1087 class AttributeInferer {
1088 public:
1089 /// Describes a request for inference of a single attribute.
1090 struct InferenceDescriptor {
1092 /// Returns true if this function does not have to be handled.
1093 /// General intent for this predicate is to provide an optimization
1094 /// for functions that do not need this attribute inference at all
1095 /// (say, for functions that already have the attribute).
1096 std::function<bool(const Function &)> SkipFunction;
1098 /// Returns true if this instruction violates attribute assumptions.
1099 std::function<bool(Instruction &)> InstrBreaksAttribute;
1101 /// Sets the inferred attribute for this function.
1102 std::function<void(Function &)> SetAttribute;
1104 /// Attribute we derive.
1105 Attribute::AttrKind AKind;
1107 /// If true, only "exact" definitions can be used to infer this attribute.
1108 /// See GlobalValue::isDefinitionExact.
1109 bool RequiresExactDefinition;
1111 InferenceDescriptor(Attribute::AttrKind AK,
1112 std::function<bool(const Function &)> SkipFunc,
1113 std::function<bool(Instruction &)> InstrScan,
1114 std::function<void(Function &)> SetAttr,
1115 bool ReqExactDef)
1116 : SkipFunction(SkipFunc), InstrBreaksAttribute(InstrScan),
1117 SetAttribute(SetAttr), AKind(AK),
1118 RequiresExactDefinition(ReqExactDef) {}
1121 private:
1122 SmallVector<InferenceDescriptor, 4> InferenceDescriptors;
1124 public:
1125 void registerAttrInference(InferenceDescriptor AttrInference) {
1126 InferenceDescriptors.push_back(AttrInference);
1129 bool run(const SCCNodeSet &SCCNodes);
1132 /// Perform all the requested attribute inference actions according to the
1133 /// attribute predicates stored before.
1134 bool AttributeInferer::run(const SCCNodeSet &SCCNodes) {
1135 SmallVector<InferenceDescriptor, 4> InferInSCC = InferenceDescriptors;
1136 // Go through all the functions in SCC and check corresponding attribute
1137 // assumptions for each of them. Attributes that are invalid for this SCC
1138 // will be removed from InferInSCC.
1139 for (Function *F : SCCNodes) {
1141 // No attributes whose assumptions are still valid - done.
1142 if (InferInSCC.empty())
1143 return false;
1145 // Check if our attributes ever need scanning/can be scanned.
1146 llvm::erase_if(InferInSCC, [F](const InferenceDescriptor &ID) {
1147 if (ID.SkipFunction(*F))
1148 return false;
1150 // Remove from further inference (invalidate) when visiting a function
1151 // that has no instructions to scan/has an unsuitable definition.
1152 return F->isDeclaration() ||
1153 (ID.RequiresExactDefinition && !F->hasExactDefinition());
1156 // For each attribute still in InferInSCC that doesn't explicitly skip F,
1157 // set up the F instructions scan to verify assumptions of the attribute.
1158 SmallVector<InferenceDescriptor, 4> InferInThisFunc;
1159 llvm::copy_if(
1160 InferInSCC, std::back_inserter(InferInThisFunc),
1161 [F](const InferenceDescriptor &ID) { return !ID.SkipFunction(*F); });
1163 if (InferInThisFunc.empty())
1164 continue;
1166 // Start instruction scan.
1167 for (Instruction &I : instructions(*F)) {
1168 llvm::erase_if(InferInThisFunc, [&](const InferenceDescriptor &ID) {
1169 if (!ID.InstrBreaksAttribute(I))
1170 return false;
1171 // Remove attribute from further inference on any other functions
1172 // because attribute assumptions have just been violated.
1173 llvm::erase_if(InferInSCC, [&ID](const InferenceDescriptor &D) {
1174 return D.AKind == ID.AKind;
1176 // Remove attribute from the rest of current instruction scan.
1177 return true;
1180 if (InferInThisFunc.empty())
1181 break;
1185 if (InferInSCC.empty())
1186 return false;
1188 bool Changed = false;
1189 for (Function *F : SCCNodes)
1190 // At this point InferInSCC contains only functions that were either:
1191 // - explicitly skipped from scan/inference, or
1192 // - verified to have no instructions that break attribute assumptions.
1193 // Hence we just go and force the attribute for all non-skipped functions.
1194 for (auto &ID : InferInSCC) {
1195 if (ID.SkipFunction(*F))
1196 continue;
1197 Changed = true;
1198 ID.SetAttribute(*F);
1200 return Changed;
1203 } // end anonymous namespace
1205 /// Helper for non-Convergent inference predicate InstrBreaksAttribute.
1206 static bool InstrBreaksNonConvergent(Instruction &I,
1207 const SCCNodeSet &SCCNodes) {
1208 const CallSite CS(&I);
1209 // Breaks non-convergent assumption if CS is a convergent call to a function
1210 // not in the SCC.
1211 return CS && CS.isConvergent() && SCCNodes.count(CS.getCalledFunction()) == 0;
1214 /// Helper for NoUnwind inference predicate InstrBreaksAttribute.
1215 static bool InstrBreaksNonThrowing(Instruction &I, const SCCNodeSet &SCCNodes) {
1216 if (!I.mayThrow())
1217 return false;
1218 if (const auto *CI = dyn_cast<CallInst>(&I)) {
1219 if (Function *Callee = CI->getCalledFunction()) {
1220 // I is a may-throw call to a function inside our SCC. This doesn't
1221 // invalidate our current working assumption that the SCC is no-throw; we
1222 // just have to scan that other function.
1223 if (SCCNodes.count(Callee) > 0)
1224 return false;
1227 return true;
1230 /// Infer attributes from all functions in the SCC by scanning every
1231 /// instruction for compliance to the attribute assumptions. Currently it
1232 /// does:
1233 /// - removal of Convergent attribute
1234 /// - addition of NoUnwind attribute
1236 /// Returns true if any changes to function attributes were made.
1237 static bool inferAttrsFromFunctionBodies(const SCCNodeSet &SCCNodes) {
1239 AttributeInferer AI;
1241 // Request to remove the convergent attribute from all functions in the SCC
1242 // if every callsite within the SCC is not convergent (except for calls
1243 // to functions within the SCC).
1244 // Note: Removal of the attr from the callsites will happen in
1245 // InstCombineCalls separately.
1246 AI.registerAttrInference(AttributeInferer::InferenceDescriptor{
1247 Attribute::Convergent,
1248 // Skip non-convergent functions.
1249 [](const Function &F) { return !F.isConvergent(); },
1250 // Instructions that break non-convergent assumption.
1251 [SCCNodes](Instruction &I) {
1252 return InstrBreaksNonConvergent(I, SCCNodes);
1254 [](Function &F) {
1255 LLVM_DEBUG(dbgs() << "Removing convergent attr from fn " << F.getName()
1256 << "\n");
1257 F.setNotConvergent();
1259 /* RequiresExactDefinition= */ false});
1261 if (!DisableNoUnwindInference)
1262 // Request to infer nounwind attribute for all the functions in the SCC if
1263 // every callsite within the SCC is not throwing (except for calls to
1264 // functions within the SCC). Note that nounwind attribute suffers from
1265 // derefinement - results may change depending on how functions are
1266 // optimized. Thus it can be inferred only from exact definitions.
1267 AI.registerAttrInference(AttributeInferer::InferenceDescriptor{
1268 Attribute::NoUnwind,
1269 // Skip non-throwing functions.
1270 [](const Function &F) { return F.doesNotThrow(); },
1271 // Instructions that break non-throwing assumption.
1272 [SCCNodes](Instruction &I) {
1273 return InstrBreaksNonThrowing(I, SCCNodes);
1275 [](Function &F) {
1276 LLVM_DEBUG(dbgs()
1277 << "Adding nounwind attr to fn " << F.getName() << "\n");
1278 F.setDoesNotThrow();
1279 ++NumNoUnwind;
1281 /* RequiresExactDefinition= */ true});
1283 // Perform all the requested attribute inference actions.
1284 return AI.run(SCCNodes);
1287 static bool setDoesNotRecurse(Function &F) {
1288 if (F.doesNotRecurse())
1289 return false;
1290 F.setDoesNotRecurse();
1291 ++NumNoRecurse;
1292 return true;
1295 static bool addNoRecurseAttrs(const SCCNodeSet &SCCNodes) {
1296 // Try and identify functions that do not recurse.
1298 // If the SCC contains multiple nodes we know for sure there is recursion.
1299 if (SCCNodes.size() != 1)
1300 return false;
1302 Function *F = *SCCNodes.begin();
1303 if (!F || F->isDeclaration() || F->doesNotRecurse())
1304 return false;
1306 // If all of the calls in F are identifiable and are to norecurse functions, F
1307 // is norecurse. This check also detects self-recursion as F is not currently
1308 // marked norecurse, so any called from F to F will not be marked norecurse.
1309 for (auto &BB : *F)
1310 for (auto &I : BB.instructionsWithoutDebug())
1311 if (auto CS = CallSite(&I)) {
1312 Function *Callee = CS.getCalledFunction();
1313 if (!Callee || Callee == F || !Callee->doesNotRecurse())
1314 // Function calls a potentially recursive function.
1315 return false;
1318 // Every call was to a non-recursive function other than this function, and
1319 // we have no indirect recursion as the SCC size is one. This function cannot
1320 // recurse.
1321 return setDoesNotRecurse(*F);
1324 template <typename AARGetterT>
1325 static bool deriveAttrsInPostOrder(SCCNodeSet &SCCNodes, AARGetterT &&AARGetter,
1326 bool HasUnknownCall) {
1327 bool Changed = false;
1329 // Bail if the SCC only contains optnone functions.
1330 if (SCCNodes.empty())
1331 return Changed;
1333 Changed |= addArgumentReturnedAttrs(SCCNodes);
1334 Changed |= addReadAttrs(SCCNodes, AARGetter);
1335 Changed |= addArgumentAttrs(SCCNodes);
1337 // If we have no external nodes participating in the SCC, we can deduce some
1338 // more precise attributes as well.
1339 if (!HasUnknownCall) {
1340 Changed |= addNoAliasAttrs(SCCNodes);
1341 Changed |= addNonNullAttrs(SCCNodes);
1342 Changed |= inferAttrsFromFunctionBodies(SCCNodes);
1343 Changed |= addNoRecurseAttrs(SCCNodes);
1346 return Changed;
1349 PreservedAnalyses PostOrderFunctionAttrsPass::run(LazyCallGraph::SCC &C,
1350 CGSCCAnalysisManager &AM,
1351 LazyCallGraph &CG,
1352 CGSCCUpdateResult &) {
1353 FunctionAnalysisManager &FAM =
1354 AM.getResult<FunctionAnalysisManagerCGSCCProxy>(C, CG).getManager();
1356 // We pass a lambda into functions to wire them up to the analysis manager
1357 // for getting function analyses.
1358 auto AARGetter = [&](Function &F) -> AAResults & {
1359 return FAM.getResult<AAManager>(F);
1362 // Fill SCCNodes with the elements of the SCC. Also track whether there are
1363 // any external or opt-none nodes that will prevent us from optimizing any
1364 // part of the SCC.
1365 SCCNodeSet SCCNodes;
1366 bool HasUnknownCall = false;
1367 for (LazyCallGraph::Node &N : C) {
1368 Function &F = N.getFunction();
1369 if (F.hasFnAttribute(Attribute::OptimizeNone) ||
1370 F.hasFnAttribute(Attribute::Naked)) {
1371 // Treat any function we're trying not to optimize as if it were an
1372 // indirect call and omit it from the node set used below.
1373 HasUnknownCall = true;
1374 continue;
1376 // Track whether any functions in this SCC have an unknown call edge.
1377 // Note: if this is ever a performance hit, we can common it with
1378 // subsequent routines which also do scans over the instructions of the
1379 // function.
1380 if (!HasUnknownCall)
1381 for (Instruction &I : instructions(F))
1382 if (auto CS = CallSite(&I))
1383 if (!CS.getCalledFunction()) {
1384 HasUnknownCall = true;
1385 break;
1388 SCCNodes.insert(&F);
1391 if (deriveAttrsInPostOrder(SCCNodes, AARGetter, HasUnknownCall))
1392 return PreservedAnalyses::none();
1394 return PreservedAnalyses::all();
1397 namespace {
1399 struct PostOrderFunctionAttrsLegacyPass : public CallGraphSCCPass {
1400 // Pass identification, replacement for typeid
1401 static char ID;
1403 PostOrderFunctionAttrsLegacyPass() : CallGraphSCCPass(ID) {
1404 initializePostOrderFunctionAttrsLegacyPassPass(
1405 *PassRegistry::getPassRegistry());
1408 bool runOnSCC(CallGraphSCC &SCC) override;
1410 void getAnalysisUsage(AnalysisUsage &AU) const override {
1411 AU.setPreservesCFG();
1412 AU.addRequired<AssumptionCacheTracker>();
1413 getAAResultsAnalysisUsage(AU);
1414 CallGraphSCCPass::getAnalysisUsage(AU);
1418 } // end anonymous namespace
1420 char PostOrderFunctionAttrsLegacyPass::ID = 0;
1421 INITIALIZE_PASS_BEGIN(PostOrderFunctionAttrsLegacyPass, "functionattrs",
1422 "Deduce function attributes", false, false)
1423 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
1424 INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
1425 INITIALIZE_PASS_END(PostOrderFunctionAttrsLegacyPass, "functionattrs",
1426 "Deduce function attributes", false, false)
1428 Pass *llvm::createPostOrderFunctionAttrsLegacyPass() {
1429 return new PostOrderFunctionAttrsLegacyPass();
1432 template <typename AARGetterT>
1433 static bool runImpl(CallGraphSCC &SCC, AARGetterT AARGetter) {
1435 // Fill SCCNodes with the elements of the SCC. Used for quickly looking up
1436 // whether a given CallGraphNode is in this SCC. Also track whether there are
1437 // any external or opt-none nodes that will prevent us from optimizing any
1438 // part of the SCC.
1439 SCCNodeSet SCCNodes;
1440 bool ExternalNode = false;
1441 for (CallGraphNode *I : SCC) {
1442 Function *F = I->getFunction();
1443 if (!F || F->hasFnAttribute(Attribute::OptimizeNone) ||
1444 F->hasFnAttribute(Attribute::Naked)) {
1445 // External node or function we're trying not to optimize - we both avoid
1446 // transform them and avoid leveraging information they provide.
1447 ExternalNode = true;
1448 continue;
1451 SCCNodes.insert(F);
1454 return deriveAttrsInPostOrder(SCCNodes, AARGetter, ExternalNode);
1457 bool PostOrderFunctionAttrsLegacyPass::runOnSCC(CallGraphSCC &SCC) {
1458 if (skipSCC(SCC))
1459 return false;
1460 return runImpl(SCC, LegacyAARGetter(*this));
1463 namespace {
1465 struct ReversePostOrderFunctionAttrsLegacyPass : public ModulePass {
1466 // Pass identification, replacement for typeid
1467 static char ID;
1469 ReversePostOrderFunctionAttrsLegacyPass() : ModulePass(ID) {
1470 initializeReversePostOrderFunctionAttrsLegacyPassPass(
1471 *PassRegistry::getPassRegistry());
1474 bool runOnModule(Module &M) override;
1476 void getAnalysisUsage(AnalysisUsage &AU) const override {
1477 AU.setPreservesCFG();
1478 AU.addRequired<CallGraphWrapperPass>();
1479 AU.addPreserved<CallGraphWrapperPass>();
1483 } // end anonymous namespace
1485 char ReversePostOrderFunctionAttrsLegacyPass::ID = 0;
1487 INITIALIZE_PASS_BEGIN(ReversePostOrderFunctionAttrsLegacyPass, "rpo-functionattrs",
1488 "Deduce function attributes in RPO", false, false)
1489 INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
1490 INITIALIZE_PASS_END(ReversePostOrderFunctionAttrsLegacyPass, "rpo-functionattrs",
1491 "Deduce function attributes in RPO", false, false)
1493 Pass *llvm::createReversePostOrderFunctionAttrsPass() {
1494 return new ReversePostOrderFunctionAttrsLegacyPass();
1497 static bool addNoRecurseAttrsTopDown(Function &F) {
1498 // We check the preconditions for the function prior to calling this to avoid
1499 // the cost of building up a reversible post-order list. We assert them here
1500 // to make sure none of the invariants this relies on were violated.
1501 assert(!F.isDeclaration() && "Cannot deduce norecurse without a definition!");
1502 assert(!F.doesNotRecurse() &&
1503 "This function has already been deduced as norecurs!");
1504 assert(F.hasInternalLinkage() &&
1505 "Can only do top-down deduction for internal linkage functions!");
1507 // If F is internal and all of its uses are calls from a non-recursive
1508 // functions, then none of its calls could in fact recurse without going
1509 // through a function marked norecurse, and so we can mark this function too
1510 // as norecurse. Note that the uses must actually be calls -- otherwise
1511 // a pointer to this function could be returned from a norecurse function but
1512 // this function could be recursively (indirectly) called. Note that this
1513 // also detects if F is directly recursive as F is not yet marked as
1514 // a norecurse function.
1515 for (auto *U : F.users()) {
1516 auto *I = dyn_cast<Instruction>(U);
1517 if (!I)
1518 return false;
1519 CallSite CS(I);
1520 if (!CS || !CS.getParent()->getParent()->doesNotRecurse())
1521 return false;
1523 return setDoesNotRecurse(F);
1526 static bool deduceFunctionAttributeInRPO(Module &M, CallGraph &CG) {
1527 // We only have a post-order SCC traversal (because SCCs are inherently
1528 // discovered in post-order), so we accumulate them in a vector and then walk
1529 // it in reverse. This is simpler than using the RPO iterator infrastructure
1530 // because we need to combine SCC detection and the PO walk of the call
1531 // graph. We can also cheat egregiously because we're primarily interested in
1532 // synthesizing norecurse and so we can only save the singular SCCs as SCCs
1533 // with multiple functions in them will clearly be recursive.
1534 SmallVector<Function *, 16> Worklist;
1535 for (scc_iterator<CallGraph *> I = scc_begin(&CG); !I.isAtEnd(); ++I) {
1536 if (I->size() != 1)
1537 continue;
1539 Function *F = I->front()->getFunction();
1540 if (F && !F->isDeclaration() && !F->doesNotRecurse() &&
1541 F->hasInternalLinkage())
1542 Worklist.push_back(F);
1545 bool Changed = false;
1546 for (auto *F : llvm::reverse(Worklist))
1547 Changed |= addNoRecurseAttrsTopDown(*F);
1549 return Changed;
1552 bool ReversePostOrderFunctionAttrsLegacyPass::runOnModule(Module &M) {
1553 if (skipModule(M))
1554 return false;
1556 auto &CG = getAnalysis<CallGraphWrapperPass>().getCallGraph();
1558 return deduceFunctionAttributeInRPO(M, CG);
1561 PreservedAnalyses
1562 ReversePostOrderFunctionAttrsPass::run(Module &M, ModuleAnalysisManager &AM) {
1563 auto &CG = AM.getResult<CallGraphAnalysis>(M);
1565 if (!deduceFunctionAttributeInRPO(M, CG))
1566 return PreservedAnalyses::all();
1568 PreservedAnalyses PA;
1569 PA.preserve<CallGraphAnalysis>();
1570 return PA;