1 //===- FunctionAttrs.cpp - Pass which marks functions attributes ----------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 ///
10 /// \file
11 /// This file implements interprocedural passes which walk the
12 /// call-graph deducing and/or propagating function attributes.
13 ///
14 //===----------------------------------------------------------------------===//
15 
16 #include "llvm/Transforms/IPO/FunctionAttrs.h"
17 #include "llvm/Transforms/IPO.h"
18 #include "llvm/ADT/SCCIterator.h"
19 #include "llvm/ADT/SetVector.h"
20 #include "llvm/ADT/SmallSet.h"
21 #include "llvm/ADT/Statistic.h"
22 #include "llvm/ADT/StringSwitch.h"
23 #include "llvm/Analysis/AliasAnalysis.h"
24 #include "llvm/Analysis/AssumptionCache.h"
25 #include "llvm/Analysis/BasicAliasAnalysis.h"
26 #include "llvm/Analysis/CallGraph.h"
27 #include "llvm/Analysis/CallGraphSCCPass.h"
28 #include "llvm/Analysis/CaptureTracking.h"
29 #include "llvm/Analysis/TargetLibraryInfo.h"
30 #include "llvm/Analysis/ValueTracking.h"
31 #include "llvm/IR/GlobalVariable.h"
32 #include "llvm/IR/InstIterator.h"
33 #include "llvm/IR/IntrinsicInst.h"
34 #include "llvm/IR/LLVMContext.h"
35 #include "llvm/Support/Debug.h"
36 #include "llvm/Support/raw_ostream.h"
37 #include "llvm/Analysis/TargetLibraryInfo.h"
38 using namespace llvm;
39 
40 #define DEBUG_TYPE "functionattrs"
41 
42 STATISTIC(NumReadNone, "Number of functions marked readnone");
43 STATISTIC(NumReadOnly, "Number of functions marked readonly");
44 STATISTIC(NumNoCapture, "Number of arguments marked nocapture");
45 STATISTIC(NumReadNoneArg, "Number of arguments marked readnone");
46 STATISTIC(NumReadOnlyArg, "Number of arguments marked readonly");
47 STATISTIC(NumNoAlias, "Number of function returns marked noalias");
48 STATISTIC(NumNonNullReturn, "Number of function returns marked nonnull");
49 STATISTIC(NumNoRecurse, "Number of functions marked as norecurse");
50 
51 namespace {
52 typedef SmallSetVector<Function *, 8> SCCNodeSet;
53 }
54 
55 namespace {
56 /// The three kinds of memory access relevant to 'readonly' and
57 /// 'readnone' attributes.
58 enum MemoryAccessKind {
59   MAK_ReadNone = 0,
60   MAK_ReadOnly = 1,
61   MAK_MayWrite = 2
62 };
63 }
64 
65 static MemoryAccessKind checkFunctionMemoryAccess(Function &F, AAResults &AAR,
66                                                   const SCCNodeSet &SCCNodes) {
67   FunctionModRefBehavior MRB = AAR.getModRefBehavior(&F);
68   if (MRB == FMRB_DoesNotAccessMemory)
69     // Already perfect!
70     return MAK_ReadNone;
71 
72   // Definitions with weak linkage may be overridden at linktime with
73   // something that writes memory, so treat them like declarations.
74   if (F.isDeclaration() || F.mayBeOverridden()) {
75     if (AliasAnalysis::onlyReadsMemory(MRB))
76       return MAK_ReadOnly;
77 
78     // Conservatively assume it writes to memory.
79     return MAK_MayWrite;
80   }
81 
82   // Scan the function body for instructions that may read or write memory.
83   bool ReadsMemory = false;
84   for (inst_iterator II = inst_begin(F), E = inst_end(F); II != E; ++II) {
85     Instruction *I = &*II;
86 
87     // Some instructions can be ignored even if they read or write memory.
88     // Detect these now, skipping to the next instruction if one is found.
89     CallSite CS(cast<Value>(I));
90     if (CS) {
91       // Ignore calls to functions in the same SCC, as long as the call sites
92       // don't have operand bundles.  Calls with operand bundles are allowed to
93       // have memory effects not described by the memory effects of the call
94       // target.
95       if (!CS.hasOperandBundles() && CS.getCalledFunction() &&
96           SCCNodes.count(CS.getCalledFunction()))
97         continue;
98       FunctionModRefBehavior MRB = AAR.getModRefBehavior(CS);
99 
100       // If the call doesn't access memory, we're done.
101       if (!(MRB & MRI_ModRef))
102         continue;
103 
104       if (!AliasAnalysis::onlyAccessesArgPointees(MRB)) {
105         // The call could access any memory. If that includes writes, give up.
106         if (MRB & MRI_Mod)
107           return MAK_MayWrite;
108         // If it reads, note it.
109         if (MRB & MRI_Ref)
110           ReadsMemory = true;
111         continue;
112       }
113 
114       // Check whether all pointer arguments point to local memory, and
115       // ignore calls that only access local memory.
116       for (CallSite::arg_iterator CI = CS.arg_begin(), CE = CS.arg_end();
117            CI != CE; ++CI) {
118         Value *Arg = *CI;
119         if (!Arg->getType()->isPtrOrPtrVectorTy())
120           continue;
121 
122         AAMDNodes AAInfo;
123         I->getAAMetadata(AAInfo);
124         MemoryLocation Loc(Arg, MemoryLocation::UnknownSize, AAInfo);
125 
126         // Skip accesses to local or constant memory as they don't impact the
127         // externally visible mod/ref behavior.
128         if (AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true))
129           continue;
130 
131         if (MRB & MRI_Mod)
132           // Writes non-local memory.  Give up.
133           return MAK_MayWrite;
134         if (MRB & MRI_Ref)
135           // Ok, it reads non-local memory.
136           ReadsMemory = true;
137       }
138       continue;
139     } else if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
140       // Ignore non-volatile loads from local memory. (Atomic is okay here.)
141       if (!LI->isVolatile()) {
142         MemoryLocation Loc = MemoryLocation::get(LI);
143         if (AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true))
144           continue;
145       }
146     } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
147       // Ignore non-volatile stores to local memory. (Atomic is okay here.)
148       if (!SI->isVolatile()) {
149         MemoryLocation Loc = MemoryLocation::get(SI);
150         if (AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true))
151           continue;
152       }
153     } else if (VAArgInst *VI = dyn_cast<VAArgInst>(I)) {
154       // Ignore vaargs on local memory.
155       MemoryLocation Loc = MemoryLocation::get(VI);
156       if (AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true))
157         continue;
158     }
159 
160     // Any remaining instructions need to be taken seriously!  Check if they
161     // read or write memory.
162     if (I->mayWriteToMemory())
163       // Writes memory.  Just give up.
164       return MAK_MayWrite;
165 
166     // If this instruction may read memory, remember that.
167     ReadsMemory |= I->mayReadFromMemory();
168   }
169 
170   return ReadsMemory ? MAK_ReadOnly : MAK_ReadNone;
171 }
172 
173 /// Deduce readonly/readnone attributes for the SCC.
174 template <typename AARGetterT>
175 static bool addReadAttrs(const SCCNodeSet &SCCNodes, AARGetterT AARGetter) {
176   // Check if any of the functions in the SCC read or write memory.  If they
177   // write memory then they can't be marked readnone or readonly.
178   bool ReadsMemory = false;
179   for (Function *F : SCCNodes) {
180     // Call the callable parameter to look up AA results for this function.
181     AAResults &AAR = AARGetter(*F);
182 
183     switch (checkFunctionMemoryAccess(*F, AAR, SCCNodes)) {
184     case MAK_MayWrite:
185       return false;
186     case MAK_ReadOnly:
187       ReadsMemory = true;
188       break;
189     case MAK_ReadNone:
190       // Nothing to do!
191       break;
192     }
193   }
194 
195   // Success!  Functions in this SCC do not access memory, or only read memory.
196   // Give them the appropriate attribute.
197   bool MadeChange = false;
198   for (Function *F : SCCNodes) {
199     if (F->doesNotAccessMemory())
200       // Already perfect!
201       continue;
202 
203     if (F->onlyReadsMemory() && ReadsMemory)
204       // No change.
205       continue;
206 
207     MadeChange = true;
208 
209     // Clear out any existing attributes.
210     AttrBuilder B;
211     B.addAttribute(Attribute::ReadOnly).addAttribute(Attribute::ReadNone);
212     F->removeAttributes(
213         AttributeSet::FunctionIndex,
214         AttributeSet::get(F->getContext(), AttributeSet::FunctionIndex, B));
215 
216     // Add in the new attribute.
217     F->addAttribute(AttributeSet::FunctionIndex,
218                     ReadsMemory ? Attribute::ReadOnly : Attribute::ReadNone);
219 
220     if (ReadsMemory)
221       ++NumReadOnly;
222     else
223       ++NumReadNone;
224   }
225 
226   return MadeChange;
227 }
228 
229 namespace {
230 /// For a given pointer Argument, this retains a list of Arguments of functions
231 /// in the same SCC that the pointer data flows into. We use this to build an
232 /// SCC of the arguments.
233 struct ArgumentGraphNode {
234   Argument *Definition;
235   SmallVector<ArgumentGraphNode *, 4> Uses;
236 };
237 
238 class ArgumentGraph {
239   // We store pointers to ArgumentGraphNode objects, so it's important that
240   // that they not move around upon insert.
241   typedef std::map<Argument *, ArgumentGraphNode> ArgumentMapTy;
242 
243   ArgumentMapTy ArgumentMap;
244 
245   // There is no root node for the argument graph, in fact:
246   //   void f(int *x, int *y) { if (...) f(x, y); }
247   // is an example where the graph is disconnected. The SCCIterator requires a
248   // single entry point, so we maintain a fake ("synthetic") root node that
249   // uses every node. Because the graph is directed and nothing points into
250   // the root, it will not participate in any SCCs (except for its own).
251   ArgumentGraphNode SyntheticRoot;
252 
253 public:
254   ArgumentGraph() { SyntheticRoot.Definition = nullptr; }
255 
256   typedef SmallVectorImpl<ArgumentGraphNode *>::iterator iterator;
257 
258   iterator begin() { return SyntheticRoot.Uses.begin(); }
259   iterator end() { return SyntheticRoot.Uses.end(); }
260   ArgumentGraphNode *getEntryNode() { return &SyntheticRoot; }
261 
262   ArgumentGraphNode *operator[](Argument *A) {
263     ArgumentGraphNode &Node = ArgumentMap[A];
264     Node.Definition = A;
265     SyntheticRoot.Uses.push_back(&Node);
266     return &Node;
267   }
268 };
269 
270 /// This tracker checks whether callees are in the SCC, and if so it does not
271 /// consider that a capture, instead adding it to the "Uses" list and
272 /// continuing with the analysis.
273 struct ArgumentUsesTracker : public CaptureTracker {
274   ArgumentUsesTracker(const SCCNodeSet &SCCNodes)
275       : Captured(false), SCCNodes(SCCNodes) {}
276 
277   void tooManyUses() override { Captured = true; }
278 
279   bool captured(const Use *U) override {
280     CallSite CS(U->getUser());
281     if (!CS.getInstruction()) {
282       Captured = true;
283       return true;
284     }
285 
286     Function *F = CS.getCalledFunction();
287     if (!F || F->isDeclaration() || F->mayBeOverridden() ||
288         !SCCNodes.count(F)) {
289       Captured = true;
290       return true;
291     }
292 
293     // Note: the callee and the two successor blocks *follow* the argument
294     // operands.  This means there is no need to adjust UseIndex to account for
295     // these.
296 
297     unsigned UseIndex =
298         std::distance(const_cast<const Use *>(CS.arg_begin()), U);
299 
300     assert(UseIndex < CS.data_operands_size() &&
301            "Indirect function calls should have been filtered above!");
302 
303     if (UseIndex >= CS.getNumArgOperands()) {
304       // Data operand, but not a argument operand -- must be a bundle operand
305       assert(CS.hasOperandBundles() && "Must be!");
306 
307       // CaptureTracking told us that we're being captured by an operand bundle
308       // use.  In this case it does not matter if the callee is within our SCC
309       // or not -- we've been captured in some unknown way, and we have to be
310       // conservative.
311       Captured = true;
312       return true;
313     }
314 
315     if (UseIndex >= F->arg_size()) {
316       assert(F->isVarArg() && "More params than args in non-varargs call");
317       Captured = true;
318       return true;
319     }
320 
321     Uses.push_back(&*std::next(F->arg_begin(), UseIndex));
322     return false;
323   }
324 
325   bool Captured; // True only if certainly captured (used outside our SCC).
326   SmallVector<Argument *, 4> Uses; // Uses within our SCC.
327 
328   const SCCNodeSet &SCCNodes;
329 };
330 }
331 
332 namespace llvm {
333 template <> struct GraphTraits<ArgumentGraphNode *> {
334   typedef ArgumentGraphNode NodeType;
335   typedef SmallVectorImpl<ArgumentGraphNode *>::iterator ChildIteratorType;
336 
337   static inline NodeType *getEntryNode(NodeType *A) { return A; }
338   static inline ChildIteratorType child_begin(NodeType *N) {
339     return N->Uses.begin();
340   }
341   static inline ChildIteratorType child_end(NodeType *N) {
342     return N->Uses.end();
343   }
344 };
345 template <>
346 struct GraphTraits<ArgumentGraph *> : public GraphTraits<ArgumentGraphNode *> {
347   static NodeType *getEntryNode(ArgumentGraph *AG) {
348     return AG->getEntryNode();
349   }
350   static ChildIteratorType nodes_begin(ArgumentGraph *AG) {
351     return AG->begin();
352   }
353   static ChildIteratorType nodes_end(ArgumentGraph *AG) { return AG->end(); }
354 };
355 }
356 
357 /// Returns Attribute::None, Attribute::ReadOnly or Attribute::ReadNone.
358 static Attribute::AttrKind
359 determinePointerReadAttrs(Argument *A,
360                           const SmallPtrSet<Argument *, 8> &SCCNodes) {
361 
362   SmallVector<Use *, 32> Worklist;
363   SmallSet<Use *, 32> Visited;
364 
365   // inalloca arguments are always clobbered by the call.
366   if (A->hasInAllocaAttr())
367     return Attribute::None;
368 
369   bool IsRead = false;
370   // We don't need to track IsWritten. If A is written to, return immediately.
371 
372   for (Use &U : A->uses()) {
373     Visited.insert(&U);
374     Worklist.push_back(&U);
375   }
376 
377   while (!Worklist.empty()) {
378     Use *U = Worklist.pop_back_val();
379     Instruction *I = cast<Instruction>(U->getUser());
380 
381     switch (I->getOpcode()) {
382     case Instruction::BitCast:
383     case Instruction::GetElementPtr:
384     case Instruction::PHI:
385     case Instruction::Select:
386     case Instruction::AddrSpaceCast:
387       // The original value is not read/written via this if the new value isn't.
388       for (Use &UU : I->uses())
389         if (Visited.insert(&UU).second)
390           Worklist.push_back(&UU);
391       break;
392 
393     case Instruction::Call:
394     case Instruction::Invoke: {
395       bool Captures = true;
396 
397       if (I->getType()->isVoidTy())
398         Captures = false;
399 
400       auto AddUsersToWorklistIfCapturing = [&] {
401         if (Captures)
402           for (Use &UU : I->uses())
403             if (Visited.insert(&UU).second)
404               Worklist.push_back(&UU);
405       };
406 
407       CallSite CS(I);
408       if (CS.doesNotAccessMemory()) {
409         AddUsersToWorklistIfCapturing();
410         continue;
411       }
412 
413       Function *F = CS.getCalledFunction();
414       if (!F) {
415         if (CS.onlyReadsMemory()) {
416           IsRead = true;
417           AddUsersToWorklistIfCapturing();
418           continue;
419         }
420         return Attribute::None;
421       }
422 
423       // Note: the callee and the two successor blocks *follow* the argument
424       // operands.  This means there is no need to adjust UseIndex to account
425       // for these.
426 
427       unsigned UseIndex = std::distance(CS.arg_begin(), U);
428 
429       // U cannot be the callee operand use: since we're exploring the
430       // transitive uses of an Argument, having such a use be a callee would
431       // imply the CallSite is an indirect call or invoke; and we'd take the
432       // early exit above.
433       assert(UseIndex < CS.data_operands_size() &&
434              "Data operand use expected!");
435 
436       bool IsOperandBundleUse = UseIndex >= CS.getNumArgOperands();
437 
438       if (UseIndex >= F->arg_size() && !IsOperandBundleUse) {
439         assert(F->isVarArg() && "More params than args in non-varargs call");
440         return Attribute::None;
441       }
442 
443       Captures &= !CS.doesNotCapture(UseIndex);
444 
445       // Since the optimizer (by design) cannot see the data flow corresponding
446       // to a operand bundle use, these cannot participate in the optimistic SCC
447       // analysis.  Instead, we model the operand bundle uses as arguments in
448       // call to a function external to the SCC.
449       if (!SCCNodes.count(&*std::next(F->arg_begin(), UseIndex)) ||
450           IsOperandBundleUse) {
451 
452         // The accessors used on CallSite here do the right thing for calls and
453         // invokes with operand bundles.
454 
455         if (!CS.onlyReadsMemory() && !CS.onlyReadsMemory(UseIndex))
456           return Attribute::None;
457         if (!CS.doesNotAccessMemory(UseIndex))
458           IsRead = true;
459       }
460 
461       AddUsersToWorklistIfCapturing();
462       break;
463     }
464 
465     case Instruction::Load:
466       IsRead = true;
467       break;
468 
469     case Instruction::ICmp:
470     case Instruction::Ret:
471       break;
472 
473     default:
474       return Attribute::None;
475     }
476   }
477 
478   return IsRead ? Attribute::ReadOnly : Attribute::ReadNone;
479 }
480 
481 /// Deduce nocapture attributes for the SCC.
482 static bool addArgumentAttrs(const SCCNodeSet &SCCNodes) {
483   bool Changed = false;
484 
485   ArgumentGraph AG;
486 
487   AttrBuilder B;
488   B.addAttribute(Attribute::NoCapture);
489 
490   // Check each function in turn, determining which pointer arguments are not
491   // captured.
492   for (Function *F : SCCNodes) {
493     // Definitions with weak linkage may be overridden at linktime with
494     // something that captures pointers, so treat them like declarations.
495     if (F->isDeclaration() || F->mayBeOverridden())
496       continue;
497 
498     // Functions that are readonly (or readnone) and nounwind and don't return
499     // a value can't capture arguments. Don't analyze them.
500     if (F->onlyReadsMemory() && F->doesNotThrow() &&
501         F->getReturnType()->isVoidTy()) {
502       for (Function::arg_iterator A = F->arg_begin(), E = F->arg_end(); A != E;
503            ++A) {
504         if (A->getType()->isPointerTy() && !A->hasNoCaptureAttr()) {
505           A->addAttr(AttributeSet::get(F->getContext(), A->getArgNo() + 1, B));
506           ++NumNoCapture;
507           Changed = true;
508         }
509       }
510       continue;
511     }
512 
513     for (Function::arg_iterator A = F->arg_begin(), E = F->arg_end(); A != E;
514          ++A) {
515       if (!A->getType()->isPointerTy())
516         continue;
517       bool HasNonLocalUses = false;
518       if (!A->hasNoCaptureAttr()) {
519         ArgumentUsesTracker Tracker(SCCNodes);
520         PointerMayBeCaptured(&*A, &Tracker);
521         if (!Tracker.Captured) {
522           if (Tracker.Uses.empty()) {
523             // If it's trivially not captured, mark it nocapture now.
524             A->addAttr(
525                 AttributeSet::get(F->getContext(), A->getArgNo() + 1, B));
526             ++NumNoCapture;
527             Changed = true;
528           } else {
529             // If it's not trivially captured and not trivially not captured,
530             // then it must be calling into another function in our SCC. Save
531             // its particulars for Argument-SCC analysis later.
532             ArgumentGraphNode *Node = AG[&*A];
533             for (SmallVectorImpl<Argument *>::iterator
534                      UI = Tracker.Uses.begin(),
535                      UE = Tracker.Uses.end();
536                  UI != UE; ++UI) {
537               Node->Uses.push_back(AG[*UI]);
538               if (*UI != &*A)
539                 HasNonLocalUses = true;
540             }
541           }
542         }
543         // Otherwise, it's captured. Don't bother doing SCC analysis on it.
544       }
545       if (!HasNonLocalUses && !A->onlyReadsMemory()) {
546         // Can we determine that it's readonly/readnone without doing an SCC?
547         // Note that we don't allow any calls at all here, or else our result
548         // will be dependent on the iteration order through the functions in the
549         // SCC.
550         SmallPtrSet<Argument *, 8> Self;
551         Self.insert(&*A);
552         Attribute::AttrKind R = determinePointerReadAttrs(&*A, Self);
553         if (R != Attribute::None) {
554           AttrBuilder B;
555           B.addAttribute(R);
556           A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
557           Changed = true;
558           R == Attribute::ReadOnly ? ++NumReadOnlyArg : ++NumReadNoneArg;
559         }
560       }
561     }
562   }
563 
564   // The graph we've collected is partial because we stopped scanning for
565   // argument uses once we solved the argument trivially. These partial nodes
566   // show up as ArgumentGraphNode objects with an empty Uses list, and for
567   // these nodes the final decision about whether they capture has already been
568   // made.  If the definition doesn't have a 'nocapture' attribute by now, it
569   // captures.
570 
571   for (scc_iterator<ArgumentGraph *> I = scc_begin(&AG); !I.isAtEnd(); ++I) {
572     const std::vector<ArgumentGraphNode *> &ArgumentSCC = *I;
573     if (ArgumentSCC.size() == 1) {
574       if (!ArgumentSCC[0]->Definition)
575         continue; // synthetic root node
576 
577       // eg. "void f(int* x) { if (...) f(x); }"
578       if (ArgumentSCC[0]->Uses.size() == 1 &&
579           ArgumentSCC[0]->Uses[0] == ArgumentSCC[0]) {
580         Argument *A = ArgumentSCC[0]->Definition;
581         A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
582         ++NumNoCapture;
583         Changed = true;
584       }
585       continue;
586     }
587 
588     bool SCCCaptured = false;
589     for (auto I = ArgumentSCC.begin(), E = ArgumentSCC.end();
590          I != E && !SCCCaptured; ++I) {
591       ArgumentGraphNode *Node = *I;
592       if (Node->Uses.empty()) {
593         if (!Node->Definition->hasNoCaptureAttr())
594           SCCCaptured = true;
595       }
596     }
597     if (SCCCaptured)
598       continue;
599 
600     SmallPtrSet<Argument *, 8> ArgumentSCCNodes;
601     // Fill ArgumentSCCNodes with the elements of the ArgumentSCC.  Used for
602     // quickly looking up whether a given Argument is in this ArgumentSCC.
603     for (auto I = ArgumentSCC.begin(), E = ArgumentSCC.end(); I != E; ++I) {
604       ArgumentSCCNodes.insert((*I)->Definition);
605     }
606 
607     for (auto I = ArgumentSCC.begin(), E = ArgumentSCC.end();
608          I != E && !SCCCaptured; ++I) {
609       ArgumentGraphNode *N = *I;
610       for (SmallVectorImpl<ArgumentGraphNode *>::iterator UI = N->Uses.begin(),
611                                                           UE = N->Uses.end();
612            UI != UE; ++UI) {
613         Argument *A = (*UI)->Definition;
614         if (A->hasNoCaptureAttr() || ArgumentSCCNodes.count(A))
615           continue;
616         SCCCaptured = true;
617         break;
618       }
619     }
620     if (SCCCaptured)
621       continue;
622 
623     for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) {
624       Argument *A = ArgumentSCC[i]->Definition;
625       A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
626       ++NumNoCapture;
627       Changed = true;
628     }
629 
630     // We also want to compute readonly/readnone. With a small number of false
631     // negatives, we can assume that any pointer which is captured isn't going
632     // to be provably readonly or readnone, since by definition we can't
633     // analyze all uses of a captured pointer.
634     //
635     // The false negatives happen when the pointer is captured by a function
636     // that promises readonly/readnone behaviour on the pointer, then the
637     // pointer's lifetime ends before anything that writes to arbitrary memory.
638     // Also, a readonly/readnone pointer may be returned, but returning a
639     // pointer is capturing it.
640 
641     Attribute::AttrKind ReadAttr = Attribute::ReadNone;
642     for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) {
643       Argument *A = ArgumentSCC[i]->Definition;
644       Attribute::AttrKind K = determinePointerReadAttrs(A, ArgumentSCCNodes);
645       if (K == Attribute::ReadNone)
646         continue;
647       if (K == Attribute::ReadOnly) {
648         ReadAttr = Attribute::ReadOnly;
649         continue;
650       }
651       ReadAttr = K;
652       break;
653     }
654 
655     if (ReadAttr != Attribute::None) {
656       AttrBuilder B, R;
657       B.addAttribute(ReadAttr);
658       R.addAttribute(Attribute::ReadOnly).addAttribute(Attribute::ReadNone);
659       for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) {
660         Argument *A = ArgumentSCC[i]->Definition;
661         // Clear out existing readonly/readnone attributes
662         A->removeAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, R));
663         A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
664         ReadAttr == Attribute::ReadOnly ? ++NumReadOnlyArg : ++NumReadNoneArg;
665         Changed = true;
666       }
667     }
668   }
669 
670   return Changed;
671 }
672 
673 /// Tests whether a function is "malloc-like".
674 ///
675 /// A function is "malloc-like" if it returns either null or a pointer that
676 /// doesn't alias any other pointer visible to the caller.
677 static bool isFunctionMallocLike(Function *F, const SCCNodeSet &SCCNodes) {
678   SmallSetVector<Value *, 8> FlowsToReturn;
679   for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I)
680     if (ReturnInst *Ret = dyn_cast<ReturnInst>(I->getTerminator()))
681       FlowsToReturn.insert(Ret->getReturnValue());
682 
683   for (unsigned i = 0; i != FlowsToReturn.size(); ++i) {
684     Value *RetVal = FlowsToReturn[i];
685 
686     if (Constant *C = dyn_cast<Constant>(RetVal)) {
687       if (!C->isNullValue() && !isa<UndefValue>(C))
688         return false;
689 
690       continue;
691     }
692 
693     if (isa<Argument>(RetVal))
694       return false;
695 
696     if (Instruction *RVI = dyn_cast<Instruction>(RetVal))
697       switch (RVI->getOpcode()) {
698       // Extend the analysis by looking upwards.
699       case Instruction::BitCast:
700       case Instruction::GetElementPtr:
701       case Instruction::AddrSpaceCast:
702         FlowsToReturn.insert(RVI->getOperand(0));
703         continue;
704       case Instruction::Select: {
705         SelectInst *SI = cast<SelectInst>(RVI);
706         FlowsToReturn.insert(SI->getTrueValue());
707         FlowsToReturn.insert(SI->getFalseValue());
708         continue;
709       }
710       case Instruction::PHI: {
711         PHINode *PN = cast<PHINode>(RVI);
712         for (Value *IncValue : PN->incoming_values())
713           FlowsToReturn.insert(IncValue);
714         continue;
715       }
716 
717       // Check whether the pointer came from an allocation.
718       case Instruction::Alloca:
719         break;
720       case Instruction::Call:
721       case Instruction::Invoke: {
722         CallSite CS(RVI);
723         if (CS.paramHasAttr(0, Attribute::NoAlias))
724           break;
725         if (CS.getCalledFunction() && SCCNodes.count(CS.getCalledFunction()))
726           break;
727       } // fall-through
728       default:
729         return false; // Did not come from an allocation.
730       }
731 
732     if (PointerMayBeCaptured(RetVal, false, /*StoreCaptures=*/false))
733       return false;
734   }
735 
736   return true;
737 }
738 
739 /// Deduce noalias attributes for the SCC.
740 static bool addNoAliasAttrs(const SCCNodeSet &SCCNodes) {
741   // Check each function in turn, determining which functions return noalias
742   // pointers.
743   for (Function *F : SCCNodes) {
744     // Already noalias.
745     if (F->doesNotAlias(0))
746       continue;
747 
748     // Definitions with weak linkage may be overridden at linktime, so
749     // treat them like declarations.
750     if (F->isDeclaration() || F->mayBeOverridden())
751       return false;
752 
753     // We annotate noalias return values, which are only applicable to
754     // pointer types.
755     if (!F->getReturnType()->isPointerTy())
756       continue;
757 
758     if (!isFunctionMallocLike(F, SCCNodes))
759       return false;
760   }
761 
762   bool MadeChange = false;
763   for (Function *F : SCCNodes) {
764     if (F->doesNotAlias(0) || !F->getReturnType()->isPointerTy())
765       continue;
766 
767     F->setDoesNotAlias(0);
768     ++NumNoAlias;
769     MadeChange = true;
770   }
771 
772   return MadeChange;
773 }
774 
775 /// Tests whether this function is known to not return null.
776 ///
777 /// Requires that the function returns a pointer.
778 ///
779 /// Returns true if it believes the function will not return a null, and sets
780 /// \p Speculative based on whether the returned conclusion is a speculative
781 /// conclusion due to SCC calls.
782 static bool isReturnNonNull(Function *F, const SCCNodeSet &SCCNodes,
783                             const TargetLibraryInfo &TLI, bool &Speculative) {
784   assert(F->getReturnType()->isPointerTy() &&
785          "nonnull only meaningful on pointer types");
786   Speculative = false;
787 
788   SmallSetVector<Value *, 8> FlowsToReturn;
789   for (BasicBlock &BB : *F)
790     if (auto *Ret = dyn_cast<ReturnInst>(BB.getTerminator()))
791       FlowsToReturn.insert(Ret->getReturnValue());
792 
793   for (unsigned i = 0; i != FlowsToReturn.size(); ++i) {
794     Value *RetVal = FlowsToReturn[i];
795 
796     // If this value is locally known to be non-null, we're good
797     if (isKnownNonNull(RetVal, &TLI))
798       continue;
799 
800     // Otherwise, we need to look upwards since we can't make any local
801     // conclusions.
802     Instruction *RVI = dyn_cast<Instruction>(RetVal);
803     if (!RVI)
804       return false;
805     switch (RVI->getOpcode()) {
806     // Extend the analysis by looking upwards.
807     case Instruction::BitCast:
808     case Instruction::GetElementPtr:
809     case Instruction::AddrSpaceCast:
810       FlowsToReturn.insert(RVI->getOperand(0));
811       continue;
812     case Instruction::Select: {
813       SelectInst *SI = cast<SelectInst>(RVI);
814       FlowsToReturn.insert(SI->getTrueValue());
815       FlowsToReturn.insert(SI->getFalseValue());
816       continue;
817     }
818     case Instruction::PHI: {
819       PHINode *PN = cast<PHINode>(RVI);
820       for (int i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
821         FlowsToReturn.insert(PN->getIncomingValue(i));
822       continue;
823     }
824     case Instruction::Call:
825     case Instruction::Invoke: {
826       CallSite CS(RVI);
827       Function *Callee = CS.getCalledFunction();
828       // A call to a node within the SCC is assumed to return null until
829       // proven otherwise
830       if (Callee && SCCNodes.count(Callee)) {
831         Speculative = true;
832         continue;
833       }
834       return false;
835     }
836     default:
837       return false; // Unknown source, may be null
838     };
839     llvm_unreachable("should have either continued or returned");
840   }
841 
842   return true;
843 }
844 
845 /// Deduce nonnull attributes for the SCC.
846 static bool addNonNullAttrs(const SCCNodeSet &SCCNodes,
847                             const TargetLibraryInfo &TLI) {
848   // Speculative that all functions in the SCC return only nonnull
849   // pointers.  We may refute this as we analyze functions.
850   bool SCCReturnsNonNull = true;
851 
852   bool MadeChange = false;
853 
854   // Check each function in turn, determining which functions return nonnull
855   // pointers.
856   for (Function *F : SCCNodes) {
857     // Already nonnull.
858     if (F->getAttributes().hasAttribute(AttributeSet::ReturnIndex,
859                                         Attribute::NonNull))
860       continue;
861 
862     // Definitions with weak linkage may be overridden at linktime, so
863     // treat them like declarations.
864     if (F->isDeclaration() || F->mayBeOverridden())
865       return false;
866 
867     // We annotate nonnull return values, which are only applicable to
868     // pointer types.
869     if (!F->getReturnType()->isPointerTy())
870       continue;
871 
872     bool Speculative = false;
873     if (isReturnNonNull(F, SCCNodes, TLI, Speculative)) {
874       if (!Speculative) {
875         // Mark the function eagerly since we may discover a function
876         // which prevents us from speculating about the entire SCC
877         DEBUG(dbgs() << "Eagerly marking " << F->getName() << " as nonnull\n");
878         F->addAttribute(AttributeSet::ReturnIndex, Attribute::NonNull);
879         ++NumNonNullReturn;
880         MadeChange = true;
881       }
882       continue;
883     }
884     // At least one function returns something which could be null, can't
885     // speculate any more.
886     SCCReturnsNonNull = false;
887   }
888 
889   if (SCCReturnsNonNull) {
890     for (Function *F : SCCNodes) {
891       if (F->getAttributes().hasAttribute(AttributeSet::ReturnIndex,
892                                           Attribute::NonNull) ||
893           !F->getReturnType()->isPointerTy())
894         continue;
895 
896       DEBUG(dbgs() << "SCC marking " << F->getName() << " as nonnull\n");
897       F->addAttribute(AttributeSet::ReturnIndex, Attribute::NonNull);
898       ++NumNonNullReturn;
899       MadeChange = true;
900     }
901   }
902 
903   return MadeChange;
904 }
905 
906 /// Removes convergent attributes where we can prove that none of the SCC's
907 /// callees are themselves convergent.  Returns true if successful at removing
908 /// the attribute.
909 static bool removeConvergentAttrs(const SCCNodeSet &SCCNodes) {
910   // Determines whether a function can be made non-convergent, ignoring all
911   // other functions in SCC.  (A function can *actually* be made non-convergent
912   // only if all functions in its SCC can be made convergent.)
913   auto CanRemoveConvergent = [&](Function *F) {
914     if (!F->isConvergent())
915       return true;
916 
917     // Can't remove convergent from declarations.
918     if (F->isDeclaration())
919       return false;
920 
921     for (Instruction &I : instructions(*F))
922       if (auto CS = CallSite(&I)) {
923         // Can't remove convergent if any of F's callees -- ignoring functions
924         // in the SCC itself -- are convergent. This needs to consider both
925         // function calls and intrinsic calls. We also assume indirect calls
926         // might call a convergent function.
927         // FIXME: We should revisit this when we put convergent onto calls
928         // instead of functions so that indirect calls which should be
929         // convergent are required to be marked as such.
930         Function *Callee = CS.getCalledFunction();
931         if (!Callee || (SCCNodes.count(Callee) == 0 && Callee->isConvergent()))
932           return false;
933       }
934 
935     return true;
936   };
937 
938   // We can remove the convergent attr from functions in the SCC if they all
939   // can be made non-convergent (because they call only non-convergent
940   // functions, other than each other).
941   if (!llvm::all_of(SCCNodes, CanRemoveConvergent))
942     return false;
943 
944   // If we got here, all of the SCC's callees are non-convergent. Therefore all
945   // of the SCC's functions can be marked as non-convergent.
946   for (Function *F : SCCNodes) {
947     if (F->isConvergent())
948       DEBUG(dbgs() << "Removing convergent attr from " << F->getName() << "\n");
949     F->setNotConvergent();
950   }
951   return true;
952 }
953 
954 static bool setDoesNotRecurse(Function &F) {
955   if (F.doesNotRecurse())
956     return false;
957   F.setDoesNotRecurse();
958   ++NumNoRecurse;
959   return true;
960 }
961 
962 static bool addNoRecurseAttrs(const SCCNodeSet &SCCNodes) {
963   // Try and identify functions that do not recurse.
964 
965   // If the SCC contains multiple nodes we know for sure there is recursion.
966   if (SCCNodes.size() != 1)
967     return false;
968 
969   Function *F = *SCCNodes.begin();
970   if (!F || F->isDeclaration() || F->doesNotRecurse())
971     return false;
972 
973   // If all of the calls in F are identifiable and are to norecurse functions, F
974   // is norecurse. This check also detects self-recursion as F is not currently
975   // marked norecurse, so any called from F to F will not be marked norecurse.
976   for (Instruction &I : instructions(*F))
977     if (auto CS = CallSite(&I)) {
978       Function *Callee = CS.getCalledFunction();
979       if (!Callee || Callee == F || !Callee->doesNotRecurse())
980         // Function calls a potentially recursive function.
981         return false;
982     }
983 
984   // Every call was to a non-recursive function other than this function, and
985   // we have no indirect recursion as the SCC size is one. This function cannot
986   // recurse.
987   return setDoesNotRecurse(*F);
988 }
989 
990 PreservedAnalyses
991 PostOrderFunctionAttrsPass::run(LazyCallGraph::SCC &C, CGSCCAnalysisManager *AM) {
992   Module &M = *C.begin()->getFunction().getParent();
993   const ModuleAnalysisManager &MAM =
994       AM->getResult<ModuleAnalysisManagerCGSCCProxy>(C).getManager();
995   FunctionAnalysisManager &FAM =
996       AM->getResult<FunctionAnalysisManagerCGSCCProxy>(C).getManager();
997 
998   // FIXME: Need some way to make it more reasonable to assume that this is
999   // always cached.
1000   TargetLibraryInfo &TLI = *MAM.getCachedResult<TargetLibraryAnalysis>(M);
1001 
1002   // We pass a lambda into functions to wire them up to the analysis manager
1003   // for getting function analyses.
1004   auto AARGetter = [&](Function &F) -> AAResults & {
1005     return FAM.getResult<AAManager>(F);
1006   };
1007 
1008   // Fill SCCNodes with the elements of the SCC. Also track whether there are
1009   // any external or opt-none nodes that will prevent us from optimizing any
1010   // part of the SCC.
1011   SCCNodeSet SCCNodes;
1012   bool HasUnknownCall = false;
1013   for (LazyCallGraph::Node &N : C) {
1014     Function &F = N.getFunction();
1015     if (F.hasFnAttribute(Attribute::OptimizeNone)) {
1016       // Treat any function we're trying not to optimize as if it were an
1017       // indirect call and omit it from the node set used below.
1018       HasUnknownCall = true;
1019       continue;
1020     }
1021     // Track whether any functions in this SCC have an unknown call edge.
1022     // Note: if this is ever a performance hit, we can common it with
1023     // subsequent routines which also do scans over the instructions of the
1024     // function.
1025     if (!HasUnknownCall)
1026       for (Instruction &I : instructions(F))
1027         if (auto CS = CallSite(&I))
1028           if (!CS.getCalledFunction()) {
1029             HasUnknownCall = true;
1030             break;
1031           }
1032 
1033     SCCNodes.insert(&F);
1034   }
1035 
1036   bool Changed = false;
1037   Changed |= addReadAttrs(SCCNodes, AARGetter);
1038   Changed |= addArgumentAttrs(SCCNodes);
1039 
1040   // If we have no external nodes participating in the SCC, we can deduce some
1041   // more precise attributes as well.
1042   if (!HasUnknownCall) {
1043     Changed |= addNoAliasAttrs(SCCNodes);
1044     Changed |= addNonNullAttrs(SCCNodes, TLI);
1045     Changed |= removeConvergentAttrs(SCCNodes);
1046     Changed |= addNoRecurseAttrs(SCCNodes);
1047   }
1048 
1049   return Changed ? PreservedAnalyses::none() : PreservedAnalyses::all();
1050 }
1051 
1052 namespace {
1053 struct PostOrderFunctionAttrsLegacyPass : public CallGraphSCCPass {
1054   static char ID; // Pass identification, replacement for typeid
1055   PostOrderFunctionAttrsLegacyPass() : CallGraphSCCPass(ID) {
1056     initializePostOrderFunctionAttrsLegacyPassPass(*PassRegistry::getPassRegistry());
1057   }
1058 
1059   bool runOnSCC(CallGraphSCC &SCC) override;
1060 
1061   void getAnalysisUsage(AnalysisUsage &AU) const override {
1062     AU.setPreservesCFG();
1063     AU.addRequired<AssumptionCacheTracker>();
1064     AU.addRequired<TargetLibraryInfoWrapperPass>();
1065     addUsedAAAnalyses(AU);
1066     CallGraphSCCPass::getAnalysisUsage(AU);
1067   }
1068 
1069 private:
1070   TargetLibraryInfo *TLI;
1071 };
1072 }
1073 
1074 char PostOrderFunctionAttrsLegacyPass::ID = 0;
1075 INITIALIZE_PASS_BEGIN(PostOrderFunctionAttrsLegacyPass, "functionattrs",
1076                       "Deduce function attributes", false, false)
1077 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
1078 INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
1079 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
1080 INITIALIZE_PASS_END(PostOrderFunctionAttrsLegacyPass, "functionattrs",
1081                     "Deduce function attributes", false, false)
1082 
1083 Pass *llvm::createPostOrderFunctionAttrsLegacyPass() { return new PostOrderFunctionAttrsLegacyPass(); }
1084 
1085 bool PostOrderFunctionAttrsLegacyPass::runOnSCC(CallGraphSCC &SCC) {
1086   TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
1087   bool Changed = false;
1088 
1089   // We compute dedicated AA results for each function in the SCC as needed. We
1090   // use a lambda referencing external objects so that they live long enough to
1091   // be queried, but we re-use them each time.
1092   Optional<BasicAAResult> BAR;
1093   Optional<AAResults> AAR;
1094   auto AARGetter = [&](Function &F) -> AAResults & {
1095     BAR.emplace(createLegacyPMBasicAAResult(*this, F));
1096     AAR.emplace(createLegacyPMAAResults(*this, F, *BAR));
1097     return *AAR;
1098   };
1099 
1100   // Fill SCCNodes with the elements of the SCC. Used for quickly looking up
1101   // whether a given CallGraphNode is in this SCC. Also track whether there are
1102   // any external or opt-none nodes that will prevent us from optimizing any
1103   // part of the SCC.
1104   SCCNodeSet SCCNodes;
1105   bool ExternalNode = false;
1106   for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
1107     Function *F = (*I)->getFunction();
1108     if (!F || F->hasFnAttribute(Attribute::OptimizeNone)) {
1109       // External node or function we're trying not to optimize - we both avoid
1110       // transform them and avoid leveraging information they provide.
1111       ExternalNode = true;
1112       continue;
1113     }
1114 
1115     SCCNodes.insert(F);
1116   }
1117 
1118   Changed |= addReadAttrs(SCCNodes, AARGetter);
1119   Changed |= addArgumentAttrs(SCCNodes);
1120 
1121   // If we have no external nodes participating in the SCC, we can deduce some
1122   // more precise attributes as well.
1123   if (!ExternalNode) {
1124     Changed |= addNoAliasAttrs(SCCNodes);
1125     Changed |= addNonNullAttrs(SCCNodes, *TLI);
1126     Changed |= removeConvergentAttrs(SCCNodes);
1127     Changed |= addNoRecurseAttrs(SCCNodes);
1128   }
1129 
1130   return Changed;
1131 }
1132 
1133 namespace {
1134 /// A pass to do RPO deduction and propagation of function attributes.
1135 ///
1136 /// This pass provides a general RPO or "top down" propagation of
1137 /// function attributes. For a few (rare) cases, we can deduce significantly
1138 /// more about function attributes by working in RPO, so this pass
1139 /// provides the compliment to the post-order pass above where the majority of
1140 /// deduction is performed.
1141 // FIXME: Currently there is no RPO CGSCC pass structure to slide into and so
1142 // this is a boring module pass, but eventually it should be an RPO CGSCC pass
1143 // when such infrastructure is available.
1144 struct ReversePostOrderFunctionAttrs : public ModulePass {
1145   static char ID; // Pass identification, replacement for typeid
1146   ReversePostOrderFunctionAttrs() : ModulePass(ID) {
1147     initializeReversePostOrderFunctionAttrsPass(*PassRegistry::getPassRegistry());
1148   }
1149 
1150   bool runOnModule(Module &M) override;
1151 
1152   void getAnalysisUsage(AnalysisUsage &AU) const override {
1153     AU.setPreservesCFG();
1154     AU.addRequired<CallGraphWrapperPass>();
1155   }
1156 };
1157 }
1158 
1159 char ReversePostOrderFunctionAttrs::ID = 0;
1160 INITIALIZE_PASS_BEGIN(ReversePostOrderFunctionAttrs, "rpo-functionattrs",
1161                       "Deduce function attributes in RPO", false, false)
1162 INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
1163 INITIALIZE_PASS_END(ReversePostOrderFunctionAttrs, "rpo-functionattrs",
1164                     "Deduce function attributes in RPO", false, false)
1165 
1166 Pass *llvm::createReversePostOrderFunctionAttrsPass() {
1167   return new ReversePostOrderFunctionAttrs();
1168 }
1169 
1170 static bool addNoRecurseAttrsTopDown(Function &F) {
1171   // We check the preconditions for the function prior to calling this to avoid
1172   // the cost of building up a reversible post-order list. We assert them here
1173   // to make sure none of the invariants this relies on were violated.
1174   assert(!F.isDeclaration() && "Cannot deduce norecurse without a definition!");
1175   assert(!F.doesNotRecurse() &&
1176          "This function has already been deduced as norecurs!");
1177   assert(F.hasInternalLinkage() &&
1178          "Can only do top-down deduction for internal linkage functions!");
1179 
1180   // If F is internal and all of its uses are calls from a non-recursive
1181   // functions, then none of its calls could in fact recurse without going
1182   // through a function marked norecurse, and so we can mark this function too
1183   // as norecurse. Note that the uses must actually be calls -- otherwise
1184   // a pointer to this function could be returned from a norecurse function but
1185   // this function could be recursively (indirectly) called. Note that this
1186   // also detects if F is directly recursive as F is not yet marked as
1187   // a norecurse function.
1188   for (auto *U : F.users()) {
1189     auto *I = dyn_cast<Instruction>(U);
1190     if (!I)
1191       return false;
1192     CallSite CS(I);
1193     if (!CS || !CS.getParent()->getParent()->doesNotRecurse())
1194       return false;
1195   }
1196   return setDoesNotRecurse(F);
1197 }
1198 
1199 bool ReversePostOrderFunctionAttrs::runOnModule(Module &M) {
1200   // We only have a post-order SCC traversal (because SCCs are inherently
1201   // discovered in post-order), so we accumulate them in a vector and then walk
1202   // it in reverse. This is simpler than using the RPO iterator infrastructure
1203   // because we need to combine SCC detection and the PO walk of the call
1204   // graph. We can also cheat egregiously because we're primarily interested in
1205   // synthesizing norecurse and so we can only save the singular SCCs as SCCs
1206   // with multiple functions in them will clearly be recursive.
1207   auto &CG = getAnalysis<CallGraphWrapperPass>().getCallGraph();
1208   SmallVector<Function *, 16> Worklist;
1209   for (scc_iterator<CallGraph *> I = scc_begin(&CG); !I.isAtEnd(); ++I) {
1210     if (I->size() != 1)
1211       continue;
1212 
1213     Function *F = I->front()->getFunction();
1214     if (F && !F->isDeclaration() && !F->doesNotRecurse() &&
1215         F->hasInternalLinkage())
1216       Worklist.push_back(F);
1217   }
1218 
1219   bool Changed = false;
1220   for (auto *F : reverse(Worklist))
1221     Changed |= addNoRecurseAttrsTopDown(*F);
1222 
1223   return Changed;
1224 }
1225