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.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "llvm/Transforms/IPO/FunctionAttrs.h"
16 #include "llvm/ADT/ArrayRef.h"
17 #include "llvm/ADT/SCCIterator.h"
18 #include "llvm/ADT/STLExtras.h"
19 #include "llvm/ADT/SetVector.h"
20 #include "llvm/ADT/SmallPtrSet.h"
21 #include "llvm/ADT/SmallVector.h"
22 #include "llvm/ADT/Statistic.h"
23 #include "llvm/Analysis/AliasAnalysis.h"
24 #include "llvm/Analysis/AssumptionCache.h"
25 #include "llvm/Analysis/BasicAliasAnalysis.h"
26 #include "llvm/Analysis/CGSCCPassManager.h"
27 #include "llvm/Analysis/CallGraph.h"
28 #include "llvm/Analysis/CallGraphSCCPass.h"
29 #include "llvm/Analysis/CaptureTracking.h"
30 #include "llvm/Analysis/LazyCallGraph.h"
31 #include "llvm/Analysis/MemoryBuiltins.h"
32 #include "llvm/Analysis/MemoryLocation.h"
33 #include "llvm/Analysis/ValueTracking.h"
34 #include "llvm/IR/Argument.h"
35 #include "llvm/IR/Attributes.h"
36 #include "llvm/IR/BasicBlock.h"
37 #include "llvm/IR/Constant.h"
38 #include "llvm/IR/Constants.h"
39 #include "llvm/IR/Function.h"
40 #include "llvm/IR/InstIterator.h"
41 #include "llvm/IR/InstrTypes.h"
42 #include "llvm/IR/Instruction.h"
43 #include "llvm/IR/Instructions.h"
44 #include "llvm/IR/IntrinsicInst.h"
45 #include "llvm/IR/Metadata.h"
46 #include "llvm/IR/PassManager.h"
47 #include "llvm/IR/Type.h"
48 #include "llvm/IR/Use.h"
49 #include "llvm/IR/User.h"
50 #include "llvm/IR/Value.h"
51 #include "llvm/InitializePasses.h"
52 #include "llvm/Pass.h"
53 #include "llvm/Support/Casting.h"
54 #include "llvm/Support/CommandLine.h"
55 #include "llvm/Support/Compiler.h"
56 #include "llvm/Support/Debug.h"
57 #include "llvm/Support/ErrorHandling.h"
58 #include "llvm/Support/raw_ostream.h"
59 #include "llvm/Transforms/IPO.h"
60 #include <cassert>
61 #include <iterator>
62 #include <map>
63 #include <vector>
64
65 using namespace llvm;
66
67 #define DEBUG_TYPE "function-attrs"
68
69 STATISTIC(NumReadNone, "Number of functions marked readnone");
70 STATISTIC(NumReadOnly, "Number of functions marked readonly");
71 STATISTIC(NumWriteOnly, "Number of functions marked writeonly");
72 STATISTIC(NumNoCapture, "Number of arguments marked nocapture");
73 STATISTIC(NumReturned, "Number of arguments marked returned");
74 STATISTIC(NumReadNoneArg, "Number of arguments marked readnone");
75 STATISTIC(NumReadOnlyArg, "Number of arguments marked readonly");
76 STATISTIC(NumNoAlias, "Number of function returns marked noalias");
77 STATISTIC(NumNonNullReturn, "Number of function returns marked nonnull");
78 STATISTIC(NumNoRecurse, "Number of functions marked as norecurse");
79 STATISTIC(NumNoUnwind, "Number of functions marked as nounwind");
80 STATISTIC(NumNoFree, "Number of functions marked as nofree");
81
82 static cl::opt<bool> EnableNonnullArgPropagation(
83 "enable-nonnull-arg-prop", cl::init(true), cl::Hidden,
84 cl::desc("Try to propagate nonnull argument attributes from callsites to "
85 "caller functions."));
86
87 static cl::opt<bool> DisableNoUnwindInference(
88 "disable-nounwind-inference", cl::Hidden,
89 cl::desc("Stop inferring nounwind attribute during function-attrs pass"));
90
91 static cl::opt<bool> DisableNoFreeInference(
92 "disable-nofree-inference", cl::Hidden,
93 cl::desc("Stop inferring nofree attribute during function-attrs pass"));
94
95 namespace {
96
97 using SCCNodeSet = SmallSetVector<Function *, 8>;
98
99 } // end anonymous namespace
100
101 /// Returns the memory access attribute for function F using AAR for AA results,
102 /// where SCCNodes is the current SCC.
103 ///
104 /// If ThisBody is true, this function may examine the function body and will
105 /// return a result pertaining to this copy of the function. If it is false, the
106 /// result will be based only on AA results for the function declaration; it
107 /// will be assumed that some other (perhaps less optimized) version of the
108 /// function may be selected at link time.
checkFunctionMemoryAccess(Function & F,bool ThisBody,AAResults & AAR,const SCCNodeSet & SCCNodes)109 static MemoryAccessKind checkFunctionMemoryAccess(Function &F, bool ThisBody,
110 AAResults &AAR,
111 const SCCNodeSet &SCCNodes) {
112 FunctionModRefBehavior MRB = AAR.getModRefBehavior(&F);
113 if (MRB == FMRB_DoesNotAccessMemory)
114 // Already perfect!
115 return MAK_ReadNone;
116
117 if (!ThisBody) {
118 if (AliasAnalysis::onlyReadsMemory(MRB))
119 return MAK_ReadOnly;
120
121 if (AliasAnalysis::doesNotReadMemory(MRB))
122 return MAK_WriteOnly;
123
124 // Conservatively assume it reads and writes to memory.
125 return MAK_MayWrite;
126 }
127
128 // Scan the function body for instructions that may read or write memory.
129 bool ReadsMemory = false;
130 bool WritesMemory = false;
131 for (inst_iterator II = inst_begin(F), E = inst_end(F); II != E; ++II) {
132 Instruction *I = &*II;
133
134 // Some instructions can be ignored even if they read or write memory.
135 // Detect these now, skipping to the next instruction if one is found.
136 if (auto *Call = dyn_cast<CallBase>(I)) {
137 // Ignore calls to functions in the same SCC, as long as the call sites
138 // don't have operand bundles. Calls with operand bundles are allowed to
139 // have memory effects not described by the memory effects of the call
140 // target.
141 if (!Call->hasOperandBundles() && Call->getCalledFunction() &&
142 SCCNodes.count(Call->getCalledFunction()))
143 continue;
144 FunctionModRefBehavior MRB = AAR.getModRefBehavior(Call);
145 ModRefInfo MRI = createModRefInfo(MRB);
146
147 // If the call doesn't access memory, we're done.
148 if (isNoModRef(MRI))
149 continue;
150
151 if (!AliasAnalysis::onlyAccessesArgPointees(MRB)) {
152 // The call could access any memory. If that includes writes, note it.
153 if (isModSet(MRI))
154 WritesMemory = true;
155 // If it reads, note it.
156 if (isRefSet(MRI))
157 ReadsMemory = true;
158 continue;
159 }
160
161 // Check whether all pointer arguments point to local memory, and
162 // ignore calls that only access local memory.
163 for (auto CI = Call->arg_begin(), CE = Call->arg_end(); CI != CE; ++CI) {
164 Value *Arg = *CI;
165 if (!Arg->getType()->isPtrOrPtrVectorTy())
166 continue;
167
168 AAMDNodes AAInfo;
169 I->getAAMetadata(AAInfo);
170 MemoryLocation Loc = MemoryLocation::getBeforeOrAfter(Arg, AAInfo);
171
172 // Skip accesses to local or constant memory as they don't impact the
173 // externally visible mod/ref behavior.
174 if (AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true))
175 continue;
176
177 if (isModSet(MRI))
178 // Writes non-local memory.
179 WritesMemory = true;
180 if (isRefSet(MRI))
181 // Ok, it reads non-local memory.
182 ReadsMemory = true;
183 }
184 continue;
185 } else if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
186 // Ignore non-volatile loads from local memory. (Atomic is okay here.)
187 if (!LI->isVolatile()) {
188 MemoryLocation Loc = MemoryLocation::get(LI);
189 if (AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true))
190 continue;
191 }
192 } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
193 // Ignore non-volatile stores to local memory. (Atomic is okay here.)
194 if (!SI->isVolatile()) {
195 MemoryLocation Loc = MemoryLocation::get(SI);
196 if (AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true))
197 continue;
198 }
199 } else if (VAArgInst *VI = dyn_cast<VAArgInst>(I)) {
200 // Ignore vaargs on local memory.
201 MemoryLocation Loc = MemoryLocation::get(VI);
202 if (AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true))
203 continue;
204 }
205
206 // Any remaining instructions need to be taken seriously! Check if they
207 // read or write memory.
208 //
209 // Writes memory, remember that.
210 WritesMemory |= I->mayWriteToMemory();
211
212 // If this instruction may read memory, remember that.
213 ReadsMemory |= I->mayReadFromMemory();
214 }
215
216 if (WritesMemory) {
217 if (!ReadsMemory)
218 return MAK_WriteOnly;
219 else
220 return MAK_MayWrite;
221 }
222
223 return ReadsMemory ? MAK_ReadOnly : MAK_ReadNone;
224 }
225
computeFunctionBodyMemoryAccess(Function & F,AAResults & AAR)226 MemoryAccessKind llvm::computeFunctionBodyMemoryAccess(Function &F,
227 AAResults &AAR) {
228 return checkFunctionMemoryAccess(F, /*ThisBody=*/true, AAR, {});
229 }
230
231 /// Deduce readonly/readnone attributes for the SCC.
232 template <typename AARGetterT>
addReadAttrs(const SCCNodeSet & SCCNodes,AARGetterT && AARGetter)233 static bool addReadAttrs(const SCCNodeSet &SCCNodes, AARGetterT &&AARGetter) {
234 // Check if any of the functions in the SCC read or write memory. If they
235 // write memory then they can't be marked readnone or readonly.
236 bool ReadsMemory = false;
237 bool WritesMemory = false;
238 for (Function *F : SCCNodes) {
239 // Call the callable parameter to look up AA results for this function.
240 AAResults &AAR = AARGetter(*F);
241
242 // Non-exact function definitions may not be selected at link time, and an
243 // alternative version that writes to memory may be selected. See the
244 // comment on GlobalValue::isDefinitionExact for more details.
245 switch (checkFunctionMemoryAccess(*F, F->hasExactDefinition(),
246 AAR, SCCNodes)) {
247 case MAK_MayWrite:
248 return false;
249 case MAK_ReadOnly:
250 ReadsMemory = true;
251 break;
252 case MAK_WriteOnly:
253 WritesMemory = true;
254 break;
255 case MAK_ReadNone:
256 // Nothing to do!
257 break;
258 }
259 }
260
261 // If the SCC contains both functions that read and functions that write, then
262 // we cannot add readonly attributes.
263 if (ReadsMemory && WritesMemory)
264 return false;
265
266 // Success! Functions in this SCC do not access memory, or only read memory.
267 // Give them the appropriate attribute.
268 bool MadeChange = false;
269
270 for (Function *F : SCCNodes) {
271 if (F->doesNotAccessMemory())
272 // Already perfect!
273 continue;
274
275 if (F->onlyReadsMemory() && ReadsMemory)
276 // No change.
277 continue;
278
279 if (F->doesNotReadMemory() && WritesMemory)
280 continue;
281
282 MadeChange = true;
283
284 // Clear out any existing attributes.
285 AttrBuilder AttrsToRemove;
286 AttrsToRemove.addAttribute(Attribute::ReadOnly);
287 AttrsToRemove.addAttribute(Attribute::ReadNone);
288 AttrsToRemove.addAttribute(Attribute::WriteOnly);
289
290 if (!WritesMemory && !ReadsMemory) {
291 // Clear out any "access range attributes" if readnone was deduced.
292 AttrsToRemove.addAttribute(Attribute::ArgMemOnly);
293 AttrsToRemove.addAttribute(Attribute::InaccessibleMemOnly);
294 AttrsToRemove.addAttribute(Attribute::InaccessibleMemOrArgMemOnly);
295 }
296 F->removeAttributes(AttributeList::FunctionIndex, AttrsToRemove);
297
298 // Add in the new attribute.
299 if (WritesMemory && !ReadsMemory)
300 F->addFnAttr(Attribute::WriteOnly);
301 else
302 F->addFnAttr(ReadsMemory ? Attribute::ReadOnly : Attribute::ReadNone);
303
304 if (WritesMemory && !ReadsMemory)
305 ++NumWriteOnly;
306 else if (ReadsMemory)
307 ++NumReadOnly;
308 else
309 ++NumReadNone;
310 }
311
312 return MadeChange;
313 }
314
315 namespace {
316
317 /// For a given pointer Argument, this retains a list of Arguments of functions
318 /// in the same SCC that the pointer data flows into. We use this to build an
319 /// SCC of the arguments.
320 struct ArgumentGraphNode {
321 Argument *Definition;
322 SmallVector<ArgumentGraphNode *, 4> Uses;
323 };
324
325 class ArgumentGraph {
326 // We store pointers to ArgumentGraphNode objects, so it's important that
327 // that they not move around upon insert.
328 using ArgumentMapTy = std::map<Argument *, ArgumentGraphNode>;
329
330 ArgumentMapTy ArgumentMap;
331
332 // There is no root node for the argument graph, in fact:
333 // void f(int *x, int *y) { if (...) f(x, y); }
334 // is an example where the graph is disconnected. The SCCIterator requires a
335 // single entry point, so we maintain a fake ("synthetic") root node that
336 // uses every node. Because the graph is directed and nothing points into
337 // the root, it will not participate in any SCCs (except for its own).
338 ArgumentGraphNode SyntheticRoot;
339
340 public:
ArgumentGraph()341 ArgumentGraph() { SyntheticRoot.Definition = nullptr; }
342
343 using iterator = SmallVectorImpl<ArgumentGraphNode *>::iterator;
344
begin()345 iterator begin() { return SyntheticRoot.Uses.begin(); }
end()346 iterator end() { return SyntheticRoot.Uses.end(); }
getEntryNode()347 ArgumentGraphNode *getEntryNode() { return &SyntheticRoot; }
348
operator [](Argument * A)349 ArgumentGraphNode *operator[](Argument *A) {
350 ArgumentGraphNode &Node = ArgumentMap[A];
351 Node.Definition = A;
352 SyntheticRoot.Uses.push_back(&Node);
353 return &Node;
354 }
355 };
356
357 /// This tracker checks whether callees are in the SCC, and if so it does not
358 /// consider that a capture, instead adding it to the "Uses" list and
359 /// continuing with the analysis.
360 struct ArgumentUsesTracker : public CaptureTracker {
ArgumentUsesTracker__anon0331d7500211::ArgumentUsesTracker361 ArgumentUsesTracker(const SCCNodeSet &SCCNodes) : SCCNodes(SCCNodes) {}
362
tooManyUses__anon0331d7500211::ArgumentUsesTracker363 void tooManyUses() override { Captured = true; }
364
captured__anon0331d7500211::ArgumentUsesTracker365 bool captured(const Use *U) override {
366 CallBase *CB = dyn_cast<CallBase>(U->getUser());
367 if (!CB) {
368 Captured = true;
369 return true;
370 }
371
372 Function *F = CB->getCalledFunction();
373 if (!F || !F->hasExactDefinition() || !SCCNodes.count(F)) {
374 Captured = true;
375 return true;
376 }
377
378 // Note: the callee and the two successor blocks *follow* the argument
379 // operands. This means there is no need to adjust UseIndex to account for
380 // these.
381
382 unsigned UseIndex =
383 std::distance(const_cast<const Use *>(CB->arg_begin()), U);
384
385 assert(UseIndex < CB->data_operands_size() &&
386 "Indirect function calls should have been filtered above!");
387
388 if (UseIndex >= CB->getNumArgOperands()) {
389 // Data operand, but not a argument operand -- must be a bundle operand
390 assert(CB->hasOperandBundles() && "Must be!");
391
392 // CaptureTracking told us that we're being captured by an operand bundle
393 // use. In this case it does not matter if the callee is within our SCC
394 // or not -- we've been captured in some unknown way, and we have to be
395 // conservative.
396 Captured = true;
397 return true;
398 }
399
400 if (UseIndex >= F->arg_size()) {
401 assert(F->isVarArg() && "More params than args in non-varargs call");
402 Captured = true;
403 return true;
404 }
405
406 Uses.push_back(&*std::next(F->arg_begin(), UseIndex));
407 return false;
408 }
409
410 // True only if certainly captured (used outside our SCC).
411 bool Captured = false;
412
413 // Uses within our SCC.
414 SmallVector<Argument *, 4> Uses;
415
416 const SCCNodeSet &SCCNodes;
417 };
418
419 } // end anonymous namespace
420
421 namespace llvm {
422
423 template <> struct GraphTraits<ArgumentGraphNode *> {
424 using NodeRef = ArgumentGraphNode *;
425 using ChildIteratorType = SmallVectorImpl<ArgumentGraphNode *>::iterator;
426
getEntryNodellvm::GraphTraits427 static NodeRef getEntryNode(NodeRef A) { return A; }
child_beginllvm::GraphTraits428 static ChildIteratorType child_begin(NodeRef N) { return N->Uses.begin(); }
child_endllvm::GraphTraits429 static ChildIteratorType child_end(NodeRef N) { return N->Uses.end(); }
430 };
431
432 template <>
433 struct GraphTraits<ArgumentGraph *> : public GraphTraits<ArgumentGraphNode *> {
getEntryNodellvm::GraphTraits434 static NodeRef getEntryNode(ArgumentGraph *AG) { return AG->getEntryNode(); }
435
nodes_beginllvm::GraphTraits436 static ChildIteratorType nodes_begin(ArgumentGraph *AG) {
437 return AG->begin();
438 }
439
nodes_endllvm::GraphTraits440 static ChildIteratorType nodes_end(ArgumentGraph *AG) { return AG->end(); }
441 };
442
443 } // end namespace llvm
444
445 /// Returns Attribute::None, Attribute::ReadOnly or Attribute::ReadNone.
446 static Attribute::AttrKind
determinePointerReadAttrs(Argument * A,const SmallPtrSet<Argument *,8> & SCCNodes)447 determinePointerReadAttrs(Argument *A,
448 const SmallPtrSet<Argument *, 8> &SCCNodes) {
449 SmallVector<Use *, 32> Worklist;
450 SmallPtrSet<Use *, 32> Visited;
451
452 // inalloca arguments are always clobbered by the call.
453 if (A->hasInAllocaAttr() || A->hasPreallocatedAttr())
454 return Attribute::None;
455
456 bool IsRead = false;
457 // We don't need to track IsWritten. If A is written to, return immediately.
458
459 for (Use &U : A->uses()) {
460 Visited.insert(&U);
461 Worklist.push_back(&U);
462 }
463
464 while (!Worklist.empty()) {
465 Use *U = Worklist.pop_back_val();
466 Instruction *I = cast<Instruction>(U->getUser());
467
468 switch (I->getOpcode()) {
469 case Instruction::BitCast:
470 case Instruction::GetElementPtr:
471 case Instruction::PHI:
472 case Instruction::Select:
473 case Instruction::AddrSpaceCast:
474 // The original value is not read/written via this if the new value isn't.
475 for (Use &UU : I->uses())
476 if (Visited.insert(&UU).second)
477 Worklist.push_back(&UU);
478 break;
479
480 case Instruction::Call:
481 case Instruction::Invoke: {
482 bool Captures = true;
483
484 if (I->getType()->isVoidTy())
485 Captures = false;
486
487 auto AddUsersToWorklistIfCapturing = [&] {
488 if (Captures)
489 for (Use &UU : I->uses())
490 if (Visited.insert(&UU).second)
491 Worklist.push_back(&UU);
492 };
493
494 CallBase &CB = cast<CallBase>(*I);
495 if (CB.doesNotAccessMemory()) {
496 AddUsersToWorklistIfCapturing();
497 continue;
498 }
499
500 Function *F = CB.getCalledFunction();
501 if (!F) {
502 if (CB.onlyReadsMemory()) {
503 IsRead = true;
504 AddUsersToWorklistIfCapturing();
505 continue;
506 }
507 return Attribute::None;
508 }
509
510 // Note: the callee and the two successor blocks *follow* the argument
511 // operands. This means there is no need to adjust UseIndex to account
512 // for these.
513
514 unsigned UseIndex = std::distance(CB.arg_begin(), U);
515
516 // U cannot be the callee operand use: since we're exploring the
517 // transitive uses of an Argument, having such a use be a callee would
518 // imply the call site is an indirect call or invoke; and we'd take the
519 // early exit above.
520 assert(UseIndex < CB.data_operands_size() &&
521 "Data operand use expected!");
522
523 bool IsOperandBundleUse = UseIndex >= CB.getNumArgOperands();
524
525 if (UseIndex >= F->arg_size() && !IsOperandBundleUse) {
526 assert(F->isVarArg() && "More params than args in non-varargs call");
527 return Attribute::None;
528 }
529
530 Captures &= !CB.doesNotCapture(UseIndex);
531
532 // Since the optimizer (by design) cannot see the data flow corresponding
533 // to a operand bundle use, these cannot participate in the optimistic SCC
534 // analysis. Instead, we model the operand bundle uses as arguments in
535 // call to a function external to the SCC.
536 if (IsOperandBundleUse ||
537 !SCCNodes.count(&*std::next(F->arg_begin(), UseIndex))) {
538
539 // The accessors used on call site here do the right thing for calls and
540 // invokes with operand bundles.
541
542 if (!CB.onlyReadsMemory() && !CB.onlyReadsMemory(UseIndex))
543 return Attribute::None;
544 if (!CB.doesNotAccessMemory(UseIndex))
545 IsRead = true;
546 }
547
548 AddUsersToWorklistIfCapturing();
549 break;
550 }
551
552 case Instruction::Load:
553 // A volatile load has side effects beyond what readonly can be relied
554 // upon.
555 if (cast<LoadInst>(I)->isVolatile())
556 return Attribute::None;
557
558 IsRead = true;
559 break;
560
561 case Instruction::ICmp:
562 case Instruction::Ret:
563 break;
564
565 default:
566 return Attribute::None;
567 }
568 }
569
570 return IsRead ? Attribute::ReadOnly : Attribute::ReadNone;
571 }
572
573 /// Deduce returned attributes for the SCC.
addArgumentReturnedAttrs(const SCCNodeSet & SCCNodes)574 static bool addArgumentReturnedAttrs(const SCCNodeSet &SCCNodes) {
575 bool Changed = false;
576
577 // Check each function in turn, determining if an argument is always returned.
578 for (Function *F : SCCNodes) {
579 // We can infer and propagate function attributes only when we know that the
580 // definition we'll get at link time is *exactly* the definition we see now.
581 // For more details, see GlobalValue::mayBeDerefined.
582 if (!F->hasExactDefinition())
583 continue;
584
585 if (F->getReturnType()->isVoidTy())
586 continue;
587
588 // There is nothing to do if an argument is already marked as 'returned'.
589 if (llvm::any_of(F->args(),
590 [](const Argument &Arg) { return Arg.hasReturnedAttr(); }))
591 continue;
592
593 auto FindRetArg = [&]() -> Value * {
594 Value *RetArg = nullptr;
595 for (BasicBlock &BB : *F)
596 if (auto *Ret = dyn_cast<ReturnInst>(BB.getTerminator())) {
597 // Note that stripPointerCasts should look through functions with
598 // returned arguments.
599 Value *RetVal = Ret->getReturnValue()->stripPointerCasts();
600 if (!isa<Argument>(RetVal) || RetVal->getType() != F->getReturnType())
601 return nullptr;
602
603 if (!RetArg)
604 RetArg = RetVal;
605 else if (RetArg != RetVal)
606 return nullptr;
607 }
608
609 return RetArg;
610 };
611
612 if (Value *RetArg = FindRetArg()) {
613 auto *A = cast<Argument>(RetArg);
614 A->addAttr(Attribute::Returned);
615 ++NumReturned;
616 Changed = true;
617 }
618 }
619
620 return Changed;
621 }
622
623 /// If a callsite has arguments that are also arguments to the parent function,
624 /// try to propagate attributes from the callsite's arguments to the parent's
625 /// arguments. This may be important because inlining can cause information loss
626 /// when attribute knowledge disappears with the inlined call.
addArgumentAttrsFromCallsites(Function & F)627 static bool addArgumentAttrsFromCallsites(Function &F) {
628 if (!EnableNonnullArgPropagation)
629 return false;
630
631 bool Changed = false;
632
633 // For an argument attribute to transfer from a callsite to the parent, the
634 // call must be guaranteed to execute every time the parent is called.
635 // Conservatively, just check for calls in the entry block that are guaranteed
636 // to execute.
637 // TODO: This could be enhanced by testing if the callsite post-dominates the
638 // entry block or by doing simple forward walks or backward walks to the
639 // callsite.
640 BasicBlock &Entry = F.getEntryBlock();
641 for (Instruction &I : Entry) {
642 if (auto *CB = dyn_cast<CallBase>(&I)) {
643 if (auto *CalledFunc = CB->getCalledFunction()) {
644 for (auto &CSArg : CalledFunc->args()) {
645 if (!CSArg.hasNonNullAttr())
646 continue;
647
648 // If the non-null callsite argument operand is an argument to 'F'
649 // (the caller) and the call is guaranteed to execute, then the value
650 // must be non-null throughout 'F'.
651 auto *FArg = dyn_cast<Argument>(CB->getArgOperand(CSArg.getArgNo()));
652 if (FArg && !FArg->hasNonNullAttr()) {
653 FArg->addAttr(Attribute::NonNull);
654 Changed = true;
655 }
656 }
657 }
658 }
659 if (!isGuaranteedToTransferExecutionToSuccessor(&I))
660 break;
661 }
662
663 return Changed;
664 }
665
addReadAttr(Argument * A,Attribute::AttrKind R)666 static bool addReadAttr(Argument *A, Attribute::AttrKind R) {
667 assert((R == Attribute::ReadOnly || R == Attribute::ReadNone)
668 && "Must be a Read attribute.");
669 assert(A && "Argument must not be null.");
670
671 // If the argument already has the attribute, nothing needs to be done.
672 if (A->hasAttribute(R))
673 return false;
674
675 // Otherwise, remove potentially conflicting attribute, add the new one,
676 // and update statistics.
677 A->removeAttr(Attribute::WriteOnly);
678 A->removeAttr(Attribute::ReadOnly);
679 A->removeAttr(Attribute::ReadNone);
680 A->addAttr(R);
681 R == Attribute::ReadOnly ? ++NumReadOnlyArg : ++NumReadNoneArg;
682 return true;
683 }
684
685 /// Deduce nocapture attributes for the SCC.
addArgumentAttrs(const SCCNodeSet & SCCNodes)686 static bool addArgumentAttrs(const SCCNodeSet &SCCNodes) {
687 bool Changed = false;
688
689 ArgumentGraph AG;
690
691 // Check each function in turn, determining which pointer arguments are not
692 // captured.
693 for (Function *F : SCCNodes) {
694 // We can infer and propagate function attributes only when we know that the
695 // definition we'll get at link time is *exactly* the definition we see now.
696 // For more details, see GlobalValue::mayBeDerefined.
697 if (!F->hasExactDefinition())
698 continue;
699
700 Changed |= addArgumentAttrsFromCallsites(*F);
701
702 // Functions that are readonly (or readnone) and nounwind and don't return
703 // a value can't capture arguments. Don't analyze them.
704 if (F->onlyReadsMemory() && F->doesNotThrow() &&
705 F->getReturnType()->isVoidTy()) {
706 for (Function::arg_iterator A = F->arg_begin(), E = F->arg_end(); A != E;
707 ++A) {
708 if (A->getType()->isPointerTy() && !A->hasNoCaptureAttr()) {
709 A->addAttr(Attribute::NoCapture);
710 ++NumNoCapture;
711 Changed = true;
712 }
713 }
714 continue;
715 }
716
717 for (Function::arg_iterator A = F->arg_begin(), E = F->arg_end(); A != E;
718 ++A) {
719 if (!A->getType()->isPointerTy())
720 continue;
721 bool HasNonLocalUses = false;
722 if (!A->hasNoCaptureAttr()) {
723 ArgumentUsesTracker Tracker(SCCNodes);
724 PointerMayBeCaptured(&*A, &Tracker);
725 if (!Tracker.Captured) {
726 if (Tracker.Uses.empty()) {
727 // If it's trivially not captured, mark it nocapture now.
728 A->addAttr(Attribute::NoCapture);
729 ++NumNoCapture;
730 Changed = true;
731 } else {
732 // If it's not trivially captured and not trivially not captured,
733 // then it must be calling into another function in our SCC. Save
734 // its particulars for Argument-SCC analysis later.
735 ArgumentGraphNode *Node = AG[&*A];
736 for (Argument *Use : Tracker.Uses) {
737 Node->Uses.push_back(AG[Use]);
738 if (Use != &*A)
739 HasNonLocalUses = true;
740 }
741 }
742 }
743 // Otherwise, it's captured. Don't bother doing SCC analysis on it.
744 }
745 if (!HasNonLocalUses && !A->onlyReadsMemory()) {
746 // Can we determine that it's readonly/readnone without doing an SCC?
747 // Note that we don't allow any calls at all here, or else our result
748 // will be dependent on the iteration order through the functions in the
749 // SCC.
750 SmallPtrSet<Argument *, 8> Self;
751 Self.insert(&*A);
752 Attribute::AttrKind R = determinePointerReadAttrs(&*A, Self);
753 if (R != Attribute::None)
754 Changed = addReadAttr(A, R);
755 }
756 }
757 }
758
759 // The graph we've collected is partial because we stopped scanning for
760 // argument uses once we solved the argument trivially. These partial nodes
761 // show up as ArgumentGraphNode objects with an empty Uses list, and for
762 // these nodes the final decision about whether they capture has already been
763 // made. If the definition doesn't have a 'nocapture' attribute by now, it
764 // captures.
765
766 for (scc_iterator<ArgumentGraph *> I = scc_begin(&AG); !I.isAtEnd(); ++I) {
767 const std::vector<ArgumentGraphNode *> &ArgumentSCC = *I;
768 if (ArgumentSCC.size() == 1) {
769 if (!ArgumentSCC[0]->Definition)
770 continue; // synthetic root node
771
772 // eg. "void f(int* x) { if (...) f(x); }"
773 if (ArgumentSCC[0]->Uses.size() == 1 &&
774 ArgumentSCC[0]->Uses[0] == ArgumentSCC[0]) {
775 Argument *A = ArgumentSCC[0]->Definition;
776 A->addAttr(Attribute::NoCapture);
777 ++NumNoCapture;
778 Changed = true;
779 }
780 continue;
781 }
782
783 bool SCCCaptured = false;
784 for (auto I = ArgumentSCC.begin(), E = ArgumentSCC.end();
785 I != E && !SCCCaptured; ++I) {
786 ArgumentGraphNode *Node = *I;
787 if (Node->Uses.empty()) {
788 if (!Node->Definition->hasNoCaptureAttr())
789 SCCCaptured = true;
790 }
791 }
792 if (SCCCaptured)
793 continue;
794
795 SmallPtrSet<Argument *, 8> ArgumentSCCNodes;
796 // Fill ArgumentSCCNodes with the elements of the ArgumentSCC. Used for
797 // quickly looking up whether a given Argument is in this ArgumentSCC.
798 for (ArgumentGraphNode *I : ArgumentSCC) {
799 ArgumentSCCNodes.insert(I->Definition);
800 }
801
802 for (auto I = ArgumentSCC.begin(), E = ArgumentSCC.end();
803 I != E && !SCCCaptured; ++I) {
804 ArgumentGraphNode *N = *I;
805 for (ArgumentGraphNode *Use : N->Uses) {
806 Argument *A = Use->Definition;
807 if (A->hasNoCaptureAttr() || ArgumentSCCNodes.count(A))
808 continue;
809 SCCCaptured = true;
810 break;
811 }
812 }
813 if (SCCCaptured)
814 continue;
815
816 for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) {
817 Argument *A = ArgumentSCC[i]->Definition;
818 A->addAttr(Attribute::NoCapture);
819 ++NumNoCapture;
820 Changed = true;
821 }
822
823 // We also want to compute readonly/readnone. With a small number of false
824 // negatives, we can assume that any pointer which is captured isn't going
825 // to be provably readonly or readnone, since by definition we can't
826 // analyze all uses of a captured pointer.
827 //
828 // The false negatives happen when the pointer is captured by a function
829 // that promises readonly/readnone behaviour on the pointer, then the
830 // pointer's lifetime ends before anything that writes to arbitrary memory.
831 // Also, a readonly/readnone pointer may be returned, but returning a
832 // pointer is capturing it.
833
834 Attribute::AttrKind ReadAttr = Attribute::ReadNone;
835 for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) {
836 Argument *A = ArgumentSCC[i]->Definition;
837 Attribute::AttrKind K = determinePointerReadAttrs(A, ArgumentSCCNodes);
838 if (K == Attribute::ReadNone)
839 continue;
840 if (K == Attribute::ReadOnly) {
841 ReadAttr = Attribute::ReadOnly;
842 continue;
843 }
844 ReadAttr = K;
845 break;
846 }
847
848 if (ReadAttr != Attribute::None) {
849 for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) {
850 Argument *A = ArgumentSCC[i]->Definition;
851 Changed = addReadAttr(A, ReadAttr);
852 }
853 }
854 }
855
856 return Changed;
857 }
858
859 /// Tests whether a function is "malloc-like".
860 ///
861 /// A function is "malloc-like" if it returns either null or a pointer that
862 /// doesn't alias any other pointer visible to the caller.
isFunctionMallocLike(Function * F,const SCCNodeSet & SCCNodes)863 static bool isFunctionMallocLike(Function *F, const SCCNodeSet &SCCNodes) {
864 SmallSetVector<Value *, 8> FlowsToReturn;
865 for (BasicBlock &BB : *F)
866 if (ReturnInst *Ret = dyn_cast<ReturnInst>(BB.getTerminator()))
867 FlowsToReturn.insert(Ret->getReturnValue());
868
869 for (unsigned i = 0; i != FlowsToReturn.size(); ++i) {
870 Value *RetVal = FlowsToReturn[i];
871
872 if (Constant *C = dyn_cast<Constant>(RetVal)) {
873 if (!C->isNullValue() && !isa<UndefValue>(C))
874 return false;
875
876 continue;
877 }
878
879 if (isa<Argument>(RetVal))
880 return false;
881
882 if (Instruction *RVI = dyn_cast<Instruction>(RetVal))
883 switch (RVI->getOpcode()) {
884 // Extend the analysis by looking upwards.
885 case Instruction::BitCast:
886 case Instruction::GetElementPtr:
887 case Instruction::AddrSpaceCast:
888 FlowsToReturn.insert(RVI->getOperand(0));
889 continue;
890 case Instruction::Select: {
891 SelectInst *SI = cast<SelectInst>(RVI);
892 FlowsToReturn.insert(SI->getTrueValue());
893 FlowsToReturn.insert(SI->getFalseValue());
894 continue;
895 }
896 case Instruction::PHI: {
897 PHINode *PN = cast<PHINode>(RVI);
898 for (Value *IncValue : PN->incoming_values())
899 FlowsToReturn.insert(IncValue);
900 continue;
901 }
902
903 // Check whether the pointer came from an allocation.
904 case Instruction::Alloca:
905 break;
906 case Instruction::Call:
907 case Instruction::Invoke: {
908 CallBase &CB = cast<CallBase>(*RVI);
909 if (CB.hasRetAttr(Attribute::NoAlias))
910 break;
911 if (CB.getCalledFunction() && SCCNodes.count(CB.getCalledFunction()))
912 break;
913 LLVM_FALLTHROUGH;
914 }
915 default:
916 return false; // Did not come from an allocation.
917 }
918
919 if (PointerMayBeCaptured(RetVal, false, /*StoreCaptures=*/false))
920 return false;
921 }
922
923 return true;
924 }
925
926 /// Deduce noalias attributes for the SCC.
addNoAliasAttrs(const SCCNodeSet & SCCNodes)927 static bool addNoAliasAttrs(const SCCNodeSet &SCCNodes) {
928 // Check each function in turn, determining which functions return noalias
929 // pointers.
930 for (Function *F : SCCNodes) {
931 // Already noalias.
932 if (F->returnDoesNotAlias())
933 continue;
934
935 // We can infer and propagate function attributes only when we know that the
936 // definition we'll get at link time is *exactly* the definition we see now.
937 // For more details, see GlobalValue::mayBeDerefined.
938 if (!F->hasExactDefinition())
939 return false;
940
941 // We annotate noalias return values, which are only applicable to
942 // pointer types.
943 if (!F->getReturnType()->isPointerTy())
944 continue;
945
946 if (!isFunctionMallocLike(F, SCCNodes))
947 return false;
948 }
949
950 bool MadeChange = false;
951 for (Function *F : SCCNodes) {
952 if (F->returnDoesNotAlias() ||
953 !F->getReturnType()->isPointerTy())
954 continue;
955
956 F->setReturnDoesNotAlias();
957 ++NumNoAlias;
958 MadeChange = true;
959 }
960
961 return MadeChange;
962 }
963
964 /// Tests whether this function is known to not return null.
965 ///
966 /// Requires that the function returns a pointer.
967 ///
968 /// Returns true if it believes the function will not return a null, and sets
969 /// \p Speculative based on whether the returned conclusion is a speculative
970 /// conclusion due to SCC calls.
isReturnNonNull(Function * F,const SCCNodeSet & SCCNodes,bool & Speculative)971 static bool isReturnNonNull(Function *F, const SCCNodeSet &SCCNodes,
972 bool &Speculative) {
973 assert(F->getReturnType()->isPointerTy() &&
974 "nonnull only meaningful on pointer types");
975 Speculative = false;
976
977 SmallSetVector<Value *, 8> FlowsToReturn;
978 for (BasicBlock &BB : *F)
979 if (auto *Ret = dyn_cast<ReturnInst>(BB.getTerminator()))
980 FlowsToReturn.insert(Ret->getReturnValue());
981
982 auto &DL = F->getParent()->getDataLayout();
983
984 for (unsigned i = 0; i != FlowsToReturn.size(); ++i) {
985 Value *RetVal = FlowsToReturn[i];
986
987 // If this value is locally known to be non-null, we're good
988 if (isKnownNonZero(RetVal, DL))
989 continue;
990
991 // Otherwise, we need to look upwards since we can't make any local
992 // conclusions.
993 Instruction *RVI = dyn_cast<Instruction>(RetVal);
994 if (!RVI)
995 return false;
996 switch (RVI->getOpcode()) {
997 // Extend the analysis by looking upwards.
998 case Instruction::BitCast:
999 case Instruction::GetElementPtr:
1000 case Instruction::AddrSpaceCast:
1001 FlowsToReturn.insert(RVI->getOperand(0));
1002 continue;
1003 case Instruction::Select: {
1004 SelectInst *SI = cast<SelectInst>(RVI);
1005 FlowsToReturn.insert(SI->getTrueValue());
1006 FlowsToReturn.insert(SI->getFalseValue());
1007 continue;
1008 }
1009 case Instruction::PHI: {
1010 PHINode *PN = cast<PHINode>(RVI);
1011 for (int i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
1012 FlowsToReturn.insert(PN->getIncomingValue(i));
1013 continue;
1014 }
1015 case Instruction::Call:
1016 case Instruction::Invoke: {
1017 CallBase &CB = cast<CallBase>(*RVI);
1018 Function *Callee = CB.getCalledFunction();
1019 // A call to a node within the SCC is assumed to return null until
1020 // proven otherwise
1021 if (Callee && SCCNodes.count(Callee)) {
1022 Speculative = true;
1023 continue;
1024 }
1025 return false;
1026 }
1027 default:
1028 return false; // Unknown source, may be null
1029 };
1030 llvm_unreachable("should have either continued or returned");
1031 }
1032
1033 return true;
1034 }
1035
1036 /// Deduce nonnull attributes for the SCC.
addNonNullAttrs(const SCCNodeSet & SCCNodes)1037 static bool addNonNullAttrs(const SCCNodeSet &SCCNodes) {
1038 // Speculative that all functions in the SCC return only nonnull
1039 // pointers. We may refute this as we analyze functions.
1040 bool SCCReturnsNonNull = true;
1041
1042 bool MadeChange = false;
1043
1044 // Check each function in turn, determining which functions return nonnull
1045 // pointers.
1046 for (Function *F : SCCNodes) {
1047 // Already nonnull.
1048 if (F->getAttributes().hasAttribute(AttributeList::ReturnIndex,
1049 Attribute::NonNull))
1050 continue;
1051
1052 // We can infer and propagate function attributes only when we know that the
1053 // definition we'll get at link time is *exactly* the definition we see now.
1054 // For more details, see GlobalValue::mayBeDerefined.
1055 if (!F->hasExactDefinition())
1056 return false;
1057
1058 // We annotate nonnull return values, which are only applicable to
1059 // pointer types.
1060 if (!F->getReturnType()->isPointerTy())
1061 continue;
1062
1063 bool Speculative = false;
1064 if (isReturnNonNull(F, SCCNodes, Speculative)) {
1065 if (!Speculative) {
1066 // Mark the function eagerly since we may discover a function
1067 // which prevents us from speculating about the entire SCC
1068 LLVM_DEBUG(dbgs() << "Eagerly marking " << F->getName()
1069 << " as nonnull\n");
1070 F->addAttribute(AttributeList::ReturnIndex, Attribute::NonNull);
1071 ++NumNonNullReturn;
1072 MadeChange = true;
1073 }
1074 continue;
1075 }
1076 // At least one function returns something which could be null, can't
1077 // speculate any more.
1078 SCCReturnsNonNull = false;
1079 }
1080
1081 if (SCCReturnsNonNull) {
1082 for (Function *F : SCCNodes) {
1083 if (F->getAttributes().hasAttribute(AttributeList::ReturnIndex,
1084 Attribute::NonNull) ||
1085 !F->getReturnType()->isPointerTy())
1086 continue;
1087
1088 LLVM_DEBUG(dbgs() << "SCC marking " << F->getName() << " as nonnull\n");
1089 F->addAttribute(AttributeList::ReturnIndex, Attribute::NonNull);
1090 ++NumNonNullReturn;
1091 MadeChange = true;
1092 }
1093 }
1094
1095 return MadeChange;
1096 }
1097
1098 namespace {
1099
1100 /// Collects a set of attribute inference requests and performs them all in one
1101 /// go on a single SCC Node. Inference involves scanning function bodies
1102 /// looking for instructions that violate attribute assumptions.
1103 /// As soon as all the bodies are fine we are free to set the attribute.
1104 /// Customization of inference for individual attributes is performed by
1105 /// providing a handful of predicates for each attribute.
1106 class AttributeInferer {
1107 public:
1108 /// Describes a request for inference of a single attribute.
1109 struct InferenceDescriptor {
1110
1111 /// Returns true if this function does not have to be handled.
1112 /// General intent for this predicate is to provide an optimization
1113 /// for functions that do not need this attribute inference at all
1114 /// (say, for functions that already have the attribute).
1115 std::function<bool(const Function &)> SkipFunction;
1116
1117 /// Returns true if this instruction violates attribute assumptions.
1118 std::function<bool(Instruction &)> InstrBreaksAttribute;
1119
1120 /// Sets the inferred attribute for this function.
1121 std::function<void(Function &)> SetAttribute;
1122
1123 /// Attribute we derive.
1124 Attribute::AttrKind AKind;
1125
1126 /// If true, only "exact" definitions can be used to infer this attribute.
1127 /// See GlobalValue::isDefinitionExact.
1128 bool RequiresExactDefinition;
1129
InferenceDescriptor__anon0331d7500611::AttributeInferer::InferenceDescriptor1130 InferenceDescriptor(Attribute::AttrKind AK,
1131 std::function<bool(const Function &)> SkipFunc,
1132 std::function<bool(Instruction &)> InstrScan,
1133 std::function<void(Function &)> SetAttr,
1134 bool ReqExactDef)
1135 : SkipFunction(SkipFunc), InstrBreaksAttribute(InstrScan),
1136 SetAttribute(SetAttr), AKind(AK),
1137 RequiresExactDefinition(ReqExactDef) {}
1138 };
1139
1140 private:
1141 SmallVector<InferenceDescriptor, 4> InferenceDescriptors;
1142
1143 public:
registerAttrInference(InferenceDescriptor AttrInference)1144 void registerAttrInference(InferenceDescriptor AttrInference) {
1145 InferenceDescriptors.push_back(AttrInference);
1146 }
1147
1148 bool run(const SCCNodeSet &SCCNodes);
1149 };
1150
1151 /// Perform all the requested attribute inference actions according to the
1152 /// attribute predicates stored before.
run(const SCCNodeSet & SCCNodes)1153 bool AttributeInferer::run(const SCCNodeSet &SCCNodes) {
1154 SmallVector<InferenceDescriptor, 4> InferInSCC = InferenceDescriptors;
1155 // Go through all the functions in SCC and check corresponding attribute
1156 // assumptions for each of them. Attributes that are invalid for this SCC
1157 // will be removed from InferInSCC.
1158 for (Function *F : SCCNodes) {
1159
1160 // No attributes whose assumptions are still valid - done.
1161 if (InferInSCC.empty())
1162 return false;
1163
1164 // Check if our attributes ever need scanning/can be scanned.
1165 llvm::erase_if(InferInSCC, [F](const InferenceDescriptor &ID) {
1166 if (ID.SkipFunction(*F))
1167 return false;
1168
1169 // Remove from further inference (invalidate) when visiting a function
1170 // that has no instructions to scan/has an unsuitable definition.
1171 return F->isDeclaration() ||
1172 (ID.RequiresExactDefinition && !F->hasExactDefinition());
1173 });
1174
1175 // For each attribute still in InferInSCC that doesn't explicitly skip F,
1176 // set up the F instructions scan to verify assumptions of the attribute.
1177 SmallVector<InferenceDescriptor, 4> InferInThisFunc;
1178 llvm::copy_if(
1179 InferInSCC, std::back_inserter(InferInThisFunc),
1180 [F](const InferenceDescriptor &ID) { return !ID.SkipFunction(*F); });
1181
1182 if (InferInThisFunc.empty())
1183 continue;
1184
1185 // Start instruction scan.
1186 for (Instruction &I : instructions(*F)) {
1187 llvm::erase_if(InferInThisFunc, [&](const InferenceDescriptor &ID) {
1188 if (!ID.InstrBreaksAttribute(I))
1189 return false;
1190 // Remove attribute from further inference on any other functions
1191 // because attribute assumptions have just been violated.
1192 llvm::erase_if(InferInSCC, [&ID](const InferenceDescriptor &D) {
1193 return D.AKind == ID.AKind;
1194 });
1195 // Remove attribute from the rest of current instruction scan.
1196 return true;
1197 });
1198
1199 if (InferInThisFunc.empty())
1200 break;
1201 }
1202 }
1203
1204 if (InferInSCC.empty())
1205 return false;
1206
1207 bool Changed = false;
1208 for (Function *F : SCCNodes)
1209 // At this point InferInSCC contains only functions that were either:
1210 // - explicitly skipped from scan/inference, or
1211 // - verified to have no instructions that break attribute assumptions.
1212 // Hence we just go and force the attribute for all non-skipped functions.
1213 for (auto &ID : InferInSCC) {
1214 if (ID.SkipFunction(*F))
1215 continue;
1216 Changed = true;
1217 ID.SetAttribute(*F);
1218 }
1219 return Changed;
1220 }
1221
1222 struct SCCNodesResult {
1223 SCCNodeSet SCCNodes;
1224 bool HasUnknownCall;
1225 };
1226
1227 } // end anonymous namespace
1228
1229 /// Helper for non-Convergent inference predicate InstrBreaksAttribute.
InstrBreaksNonConvergent(Instruction & I,const SCCNodeSet & SCCNodes)1230 static bool InstrBreaksNonConvergent(Instruction &I,
1231 const SCCNodeSet &SCCNodes) {
1232 const CallBase *CB = dyn_cast<CallBase>(&I);
1233 // Breaks non-convergent assumption if CS is a convergent call to a function
1234 // not in the SCC.
1235 return CB && CB->isConvergent() &&
1236 SCCNodes.count(CB->getCalledFunction()) == 0;
1237 }
1238
1239 /// Helper for NoUnwind inference predicate InstrBreaksAttribute.
InstrBreaksNonThrowing(Instruction & I,const SCCNodeSet & SCCNodes)1240 static bool InstrBreaksNonThrowing(Instruction &I, const SCCNodeSet &SCCNodes) {
1241 if (!I.mayThrow())
1242 return false;
1243 if (const auto *CI = dyn_cast<CallInst>(&I)) {
1244 if (Function *Callee = CI->getCalledFunction()) {
1245 // I is a may-throw call to a function inside our SCC. This doesn't
1246 // invalidate our current working assumption that the SCC is no-throw; we
1247 // just have to scan that other function.
1248 if (SCCNodes.count(Callee) > 0)
1249 return false;
1250 }
1251 }
1252 return true;
1253 }
1254
1255 /// Helper for NoFree inference predicate InstrBreaksAttribute.
InstrBreaksNoFree(Instruction & I,const SCCNodeSet & SCCNodes)1256 static bool InstrBreaksNoFree(Instruction &I, const SCCNodeSet &SCCNodes) {
1257 CallBase *CB = dyn_cast<CallBase>(&I);
1258 if (!CB)
1259 return false;
1260
1261 Function *Callee = CB->getCalledFunction();
1262 if (!Callee)
1263 return true;
1264
1265 if (Callee->doesNotFreeMemory())
1266 return false;
1267
1268 if (SCCNodes.count(Callee) > 0)
1269 return false;
1270
1271 return true;
1272 }
1273
1274 /// Attempt to remove convergent function attribute when possible.
1275 ///
1276 /// Returns true if any changes to function attributes were made.
inferConvergent(const SCCNodeSet & SCCNodes)1277 static bool inferConvergent(const SCCNodeSet &SCCNodes) {
1278 AttributeInferer AI;
1279
1280 // Request to remove the convergent attribute from all functions in the SCC
1281 // if every callsite within the SCC is not convergent (except for calls
1282 // to functions within the SCC).
1283 // Note: Removal of the attr from the callsites will happen in
1284 // InstCombineCalls separately.
1285 AI.registerAttrInference(AttributeInferer::InferenceDescriptor{
1286 Attribute::Convergent,
1287 // Skip non-convergent functions.
1288 [](const Function &F) { return !F.isConvergent(); },
1289 // Instructions that break non-convergent assumption.
1290 [SCCNodes](Instruction &I) {
1291 return InstrBreaksNonConvergent(I, SCCNodes);
1292 },
1293 [](Function &F) {
1294 LLVM_DEBUG(dbgs() << "Removing convergent attr from fn " << F.getName()
1295 << "\n");
1296 F.setNotConvergent();
1297 },
1298 /* RequiresExactDefinition= */ false});
1299 // Perform all the requested attribute inference actions.
1300 return AI.run(SCCNodes);
1301 }
1302
1303 /// Infer attributes from all functions in the SCC by scanning every
1304 /// instruction for compliance to the attribute assumptions. Currently it
1305 /// does:
1306 /// - addition of NoUnwind attribute
1307 ///
1308 /// Returns true if any changes to function attributes were made.
inferAttrsFromFunctionBodies(const SCCNodeSet & SCCNodes)1309 static bool inferAttrsFromFunctionBodies(const SCCNodeSet &SCCNodes) {
1310 AttributeInferer AI;
1311
1312 if (!DisableNoUnwindInference)
1313 // Request to infer nounwind attribute for all the functions in the SCC if
1314 // every callsite within the SCC is not throwing (except for calls to
1315 // functions within the SCC). Note that nounwind attribute suffers from
1316 // derefinement - results may change depending on how functions are
1317 // optimized. Thus it can be inferred only from exact definitions.
1318 AI.registerAttrInference(AttributeInferer::InferenceDescriptor{
1319 Attribute::NoUnwind,
1320 // Skip non-throwing functions.
1321 [](const Function &F) { return F.doesNotThrow(); },
1322 // Instructions that break non-throwing assumption.
1323 [&SCCNodes](Instruction &I) {
1324 return InstrBreaksNonThrowing(I, SCCNodes);
1325 },
1326 [](Function &F) {
1327 LLVM_DEBUG(dbgs()
1328 << "Adding nounwind attr to fn " << F.getName() << "\n");
1329 F.setDoesNotThrow();
1330 ++NumNoUnwind;
1331 },
1332 /* RequiresExactDefinition= */ true});
1333
1334 if (!DisableNoFreeInference)
1335 // Request to infer nofree attribute for all the functions in the SCC if
1336 // every callsite within the SCC does not directly or indirectly free
1337 // memory (except for calls to functions within the SCC). Note that nofree
1338 // attribute suffers from derefinement - results may change depending on
1339 // how functions are optimized. Thus it can be inferred only from exact
1340 // definitions.
1341 AI.registerAttrInference(AttributeInferer::InferenceDescriptor{
1342 Attribute::NoFree,
1343 // Skip functions known not to free memory.
1344 [](const Function &F) { return F.doesNotFreeMemory(); },
1345 // Instructions that break non-deallocating assumption.
1346 [&SCCNodes](Instruction &I) {
1347 return InstrBreaksNoFree(I, SCCNodes);
1348 },
1349 [](Function &F) {
1350 LLVM_DEBUG(dbgs()
1351 << "Adding nofree attr to fn " << F.getName() << "\n");
1352 F.setDoesNotFreeMemory();
1353 ++NumNoFree;
1354 },
1355 /* RequiresExactDefinition= */ true});
1356
1357 // Perform all the requested attribute inference actions.
1358 return AI.run(SCCNodes);
1359 }
1360
addNoRecurseAttrs(const SCCNodeSet & SCCNodes)1361 static bool addNoRecurseAttrs(const SCCNodeSet &SCCNodes) {
1362 // Try and identify functions that do not recurse.
1363
1364 // If the SCC contains multiple nodes we know for sure there is recursion.
1365 if (SCCNodes.size() != 1)
1366 return false;
1367
1368 Function *F = *SCCNodes.begin();
1369 if (!F || !F->hasExactDefinition() || F->doesNotRecurse())
1370 return false;
1371
1372 // If all of the calls in F are identifiable and are to norecurse functions, F
1373 // is norecurse. This check also detects self-recursion as F is not currently
1374 // marked norecurse, so any called from F to F will not be marked norecurse.
1375 for (auto &BB : *F)
1376 for (auto &I : BB.instructionsWithoutDebug())
1377 if (auto *CB = dyn_cast<CallBase>(&I)) {
1378 Function *Callee = CB->getCalledFunction();
1379 if (!Callee || Callee == F || !Callee->doesNotRecurse())
1380 // Function calls a potentially recursive function.
1381 return false;
1382 }
1383
1384 // Every call was to a non-recursive function other than this function, and
1385 // we have no indirect recursion as the SCC size is one. This function cannot
1386 // recurse.
1387 F->setDoesNotRecurse();
1388 ++NumNoRecurse;
1389 return true;
1390 }
1391
createSCCNodeSet(ArrayRef<Function * > Functions)1392 static SCCNodesResult createSCCNodeSet(ArrayRef<Function *> Functions) {
1393 SCCNodesResult Res;
1394 Res.HasUnknownCall = false;
1395 for (Function *F : Functions) {
1396 if (!F || F->hasOptNone() || F->hasFnAttribute(Attribute::Naked)) {
1397 // Treat any function we're trying not to optimize as if it were an
1398 // indirect call and omit it from the node set used below.
1399 Res.HasUnknownCall = true;
1400 continue;
1401 }
1402 // Track whether any functions in this SCC have an unknown call edge.
1403 // Note: if this is ever a performance hit, we can common it with
1404 // subsequent routines which also do scans over the instructions of the
1405 // function.
1406 if (!Res.HasUnknownCall) {
1407 for (Instruction &I : instructions(*F)) {
1408 if (auto *CB = dyn_cast<CallBase>(&I)) {
1409 if (!CB->getCalledFunction()) {
1410 Res.HasUnknownCall = true;
1411 break;
1412 }
1413 }
1414 }
1415 }
1416 Res.SCCNodes.insert(F);
1417 }
1418 return Res;
1419 }
1420
1421 template <typename AARGetterT>
deriveAttrsInPostOrder(ArrayRef<Function * > Functions,AARGetterT && AARGetter)1422 static bool deriveAttrsInPostOrder(ArrayRef<Function *> Functions,
1423 AARGetterT &&AARGetter) {
1424 SCCNodesResult Nodes = createSCCNodeSet(Functions);
1425 bool Changed = false;
1426
1427 // Bail if the SCC only contains optnone functions.
1428 if (Nodes.SCCNodes.empty())
1429 return Changed;
1430
1431 Changed |= addArgumentReturnedAttrs(Nodes.SCCNodes);
1432 Changed |= addReadAttrs(Nodes.SCCNodes, AARGetter);
1433 Changed |= addArgumentAttrs(Nodes.SCCNodes);
1434 Changed |= inferConvergent(Nodes.SCCNodes);
1435
1436 // If we have no external nodes participating in the SCC, we can deduce some
1437 // more precise attributes as well.
1438 if (!Nodes.HasUnknownCall) {
1439 Changed |= addNoAliasAttrs(Nodes.SCCNodes);
1440 Changed |= addNonNullAttrs(Nodes.SCCNodes);
1441 Changed |= inferAttrsFromFunctionBodies(Nodes.SCCNodes);
1442 Changed |= addNoRecurseAttrs(Nodes.SCCNodes);
1443 }
1444
1445 return Changed;
1446 }
1447
run(LazyCallGraph::SCC & C,CGSCCAnalysisManager & AM,LazyCallGraph & CG,CGSCCUpdateResult &)1448 PreservedAnalyses PostOrderFunctionAttrsPass::run(LazyCallGraph::SCC &C,
1449 CGSCCAnalysisManager &AM,
1450 LazyCallGraph &CG,
1451 CGSCCUpdateResult &) {
1452 FunctionAnalysisManager &FAM =
1453 AM.getResult<FunctionAnalysisManagerCGSCCProxy>(C, CG).getManager();
1454
1455 // We pass a lambda into functions to wire them up to the analysis manager
1456 // for getting function analyses.
1457 auto AARGetter = [&](Function &F) -> AAResults & {
1458 return FAM.getResult<AAManager>(F);
1459 };
1460
1461 SmallVector<Function *, 8> Functions;
1462 for (LazyCallGraph::Node &N : C) {
1463 Functions.push_back(&N.getFunction());
1464 }
1465
1466 if (deriveAttrsInPostOrder(Functions, AARGetter))
1467 return PreservedAnalyses::none();
1468
1469 return PreservedAnalyses::all();
1470 }
1471
1472 namespace {
1473
1474 struct PostOrderFunctionAttrsLegacyPass : public CallGraphSCCPass {
1475 // Pass identification, replacement for typeid
1476 static char ID;
1477
PostOrderFunctionAttrsLegacyPass__anon0331d7501511::PostOrderFunctionAttrsLegacyPass1478 PostOrderFunctionAttrsLegacyPass() : CallGraphSCCPass(ID) {
1479 initializePostOrderFunctionAttrsLegacyPassPass(
1480 *PassRegistry::getPassRegistry());
1481 }
1482
1483 bool runOnSCC(CallGraphSCC &SCC) override;
1484
getAnalysisUsage__anon0331d7501511::PostOrderFunctionAttrsLegacyPass1485 void getAnalysisUsage(AnalysisUsage &AU) const override {
1486 AU.setPreservesCFG();
1487 AU.addRequired<AssumptionCacheTracker>();
1488 getAAResultsAnalysisUsage(AU);
1489 CallGraphSCCPass::getAnalysisUsage(AU);
1490 }
1491 };
1492
1493 } // end anonymous namespace
1494
1495 char PostOrderFunctionAttrsLegacyPass::ID = 0;
1496 INITIALIZE_PASS_BEGIN(PostOrderFunctionAttrsLegacyPass, "function-attrs",
1497 "Deduce function attributes", false, false)
INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)1498 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
1499 INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
1500 INITIALIZE_PASS_END(PostOrderFunctionAttrsLegacyPass, "function-attrs",
1501 "Deduce function attributes", false, false)
1502
1503 Pass *llvm::createPostOrderFunctionAttrsLegacyPass() {
1504 return new PostOrderFunctionAttrsLegacyPass();
1505 }
1506
1507 template <typename AARGetterT>
runImpl(CallGraphSCC & SCC,AARGetterT AARGetter)1508 static bool runImpl(CallGraphSCC &SCC, AARGetterT AARGetter) {
1509 SmallVector<Function *, 8> Functions;
1510 for (CallGraphNode *I : SCC) {
1511 Functions.push_back(I->getFunction());
1512 }
1513
1514 return deriveAttrsInPostOrder(Functions, AARGetter);
1515 }
1516
runOnSCC(CallGraphSCC & SCC)1517 bool PostOrderFunctionAttrsLegacyPass::runOnSCC(CallGraphSCC &SCC) {
1518 if (skipSCC(SCC))
1519 return false;
1520 return runImpl(SCC, LegacyAARGetter(*this));
1521 }
1522
1523 namespace {
1524
1525 struct ReversePostOrderFunctionAttrsLegacyPass : public ModulePass {
1526 // Pass identification, replacement for typeid
1527 static char ID;
1528
ReversePostOrderFunctionAttrsLegacyPass__anon0331d7501611::ReversePostOrderFunctionAttrsLegacyPass1529 ReversePostOrderFunctionAttrsLegacyPass() : ModulePass(ID) {
1530 initializeReversePostOrderFunctionAttrsLegacyPassPass(
1531 *PassRegistry::getPassRegistry());
1532 }
1533
1534 bool runOnModule(Module &M) override;
1535
getAnalysisUsage__anon0331d7501611::ReversePostOrderFunctionAttrsLegacyPass1536 void getAnalysisUsage(AnalysisUsage &AU) const override {
1537 AU.setPreservesCFG();
1538 AU.addRequired<CallGraphWrapperPass>();
1539 AU.addPreserved<CallGraphWrapperPass>();
1540 }
1541 };
1542
1543 } // end anonymous namespace
1544
1545 char ReversePostOrderFunctionAttrsLegacyPass::ID = 0;
1546
1547 INITIALIZE_PASS_BEGIN(ReversePostOrderFunctionAttrsLegacyPass,
1548 "rpo-function-attrs", "Deduce function attributes in RPO",
1549 false, false)
INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)1550 INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
1551 INITIALIZE_PASS_END(ReversePostOrderFunctionAttrsLegacyPass,
1552 "rpo-function-attrs", "Deduce function attributes in RPO",
1553 false, false)
1554
1555 Pass *llvm::createReversePostOrderFunctionAttrsPass() {
1556 return new ReversePostOrderFunctionAttrsLegacyPass();
1557 }
1558
addNoRecurseAttrsTopDown(Function & F)1559 static bool addNoRecurseAttrsTopDown(Function &F) {
1560 // We check the preconditions for the function prior to calling this to avoid
1561 // the cost of building up a reversible post-order list. We assert them here
1562 // to make sure none of the invariants this relies on were violated.
1563 assert(!F.isDeclaration() && "Cannot deduce norecurse without a definition!");
1564 assert(!F.doesNotRecurse() &&
1565 "This function has already been deduced as norecurs!");
1566 assert(F.hasInternalLinkage() &&
1567 "Can only do top-down deduction for internal linkage functions!");
1568
1569 // If F is internal and all of its uses are calls from a non-recursive
1570 // functions, then none of its calls could in fact recurse without going
1571 // through a function marked norecurse, and so we can mark this function too
1572 // as norecurse. Note that the uses must actually be calls -- otherwise
1573 // a pointer to this function could be returned from a norecurse function but
1574 // this function could be recursively (indirectly) called. Note that this
1575 // also detects if F is directly recursive as F is not yet marked as
1576 // a norecurse function.
1577 for (auto *U : F.users()) {
1578 auto *I = dyn_cast<Instruction>(U);
1579 if (!I)
1580 return false;
1581 CallBase *CB = dyn_cast<CallBase>(I);
1582 if (!CB || !CB->getParent()->getParent()->doesNotRecurse())
1583 return false;
1584 }
1585 F.setDoesNotRecurse();
1586 ++NumNoRecurse;
1587 return true;
1588 }
1589
deduceFunctionAttributeInRPO(Module & M,CallGraph & CG)1590 static bool deduceFunctionAttributeInRPO(Module &M, CallGraph &CG) {
1591 // We only have a post-order SCC traversal (because SCCs are inherently
1592 // discovered in post-order), so we accumulate them in a vector and then walk
1593 // it in reverse. This is simpler than using the RPO iterator infrastructure
1594 // because we need to combine SCC detection and the PO walk of the call
1595 // graph. We can also cheat egregiously because we're primarily interested in
1596 // synthesizing norecurse and so we can only save the singular SCCs as SCCs
1597 // with multiple functions in them will clearly be recursive.
1598 SmallVector<Function *, 16> Worklist;
1599 for (scc_iterator<CallGraph *> I = scc_begin(&CG); !I.isAtEnd(); ++I) {
1600 if (I->size() != 1)
1601 continue;
1602
1603 Function *F = I->front()->getFunction();
1604 if (F && !F->isDeclaration() && !F->doesNotRecurse() &&
1605 F->hasInternalLinkage())
1606 Worklist.push_back(F);
1607 }
1608
1609 bool Changed = false;
1610 for (auto *F : llvm::reverse(Worklist))
1611 Changed |= addNoRecurseAttrsTopDown(*F);
1612
1613 return Changed;
1614 }
1615
runOnModule(Module & M)1616 bool ReversePostOrderFunctionAttrsLegacyPass::runOnModule(Module &M) {
1617 if (skipModule(M))
1618 return false;
1619
1620 auto &CG = getAnalysis<CallGraphWrapperPass>().getCallGraph();
1621
1622 return deduceFunctionAttributeInRPO(M, CG);
1623 }
1624
1625 PreservedAnalyses
run(Module & M,ModuleAnalysisManager & AM)1626 ReversePostOrderFunctionAttrsPass::run(Module &M, ModuleAnalysisManager &AM) {
1627 auto &CG = AM.getResult<CallGraphAnalysis>(M);
1628
1629 if (!deduceFunctionAttributeInRPO(M, CG))
1630 return PreservedAnalyses::all();
1631
1632 PreservedAnalyses PA;
1633 PA.preserve<CallGraphAnalysis>();
1634 return PA;
1635 }
1636