LLVM 20.0.0git
FunctionAttrs.cpp
Go to the documentation of this file.
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
16#include "llvm/ADT/ArrayRef.h"
17#include "llvm/ADT/DenseMap.h"
20#include "llvm/ADT/STLExtras.h"
21#include "llvm/ADT/SetVector.h"
23#include "llvm/ADT/SmallSet.h"
25#include "llvm/ADT/Statistic.h"
28#include "llvm/Analysis/CFG.h"
36#include "llvm/IR/Argument.h"
37#include "llvm/IR/Attributes.h"
38#include "llvm/IR/BasicBlock.h"
39#include "llvm/IR/Constant.h"
41#include "llvm/IR/Constants.h"
42#include "llvm/IR/Function.h"
44#include "llvm/IR/InstrTypes.h"
45#include "llvm/IR/Instruction.h"
48#include "llvm/IR/Metadata.h"
50#include "llvm/IR/PassManager.h"
51#include "llvm/IR/Type.h"
52#include "llvm/IR/Use.h"
53#include "llvm/IR/User.h"
54#include "llvm/IR/Value.h"
58#include "llvm/Support/Debug.h"
61#include "llvm/Transforms/IPO.h"
63#include <cassert>
64#include <iterator>
65#include <map>
66#include <optional>
67#include <vector>
68
69using namespace llvm;
70
71#define DEBUG_TYPE "function-attrs"
72
73STATISTIC(NumMemoryAttr, "Number of functions with improved memory attribute");
74STATISTIC(NumNoCapture, "Number of arguments marked nocapture");
75STATISTIC(NumReturned, "Number of arguments marked returned");
76STATISTIC(NumReadNoneArg, "Number of arguments marked readnone");
77STATISTIC(NumReadOnlyArg, "Number of arguments marked readonly");
78STATISTIC(NumWriteOnlyArg, "Number of arguments marked writeonly");
79STATISTIC(NumNoAlias, "Number of function returns marked noalias");
80STATISTIC(NumNonNullReturn, "Number of function returns marked nonnull");
81STATISTIC(NumNoUndefReturn, "Number of function returns marked noundef");
82STATISTIC(NumNoRecurse, "Number of functions marked as norecurse");
83STATISTIC(NumNoUnwind, "Number of functions marked as nounwind");
84STATISTIC(NumNoFree, "Number of functions marked as nofree");
85STATISTIC(NumWillReturn, "Number of functions marked as willreturn");
86STATISTIC(NumNoSync, "Number of functions marked as nosync");
87STATISTIC(NumCold, "Number of functions marked as cold");
88
89STATISTIC(NumThinLinkNoRecurse,
90 "Number of functions marked as norecurse during thinlink");
91STATISTIC(NumThinLinkNoUnwind,
92 "Number of functions marked as nounwind during thinlink");
93
95 "enable-nonnull-arg-prop", cl::init(true), cl::Hidden,
96 cl::desc("Try to propagate nonnull argument attributes from callsites to "
97 "caller functions."));
98
100 "disable-nounwind-inference", cl::Hidden,
101 cl::desc("Stop inferring nounwind attribute during function-attrs pass"));
102
104 "disable-nofree-inference", cl::Hidden,
105 cl::desc("Stop inferring nofree attribute during function-attrs pass"));
106
108 "disable-thinlto-funcattrs", cl::init(true), cl::Hidden,
109 cl::desc("Don't propagate function-attrs in thinLTO"));
110
111namespace {
112
113using SCCNodeSet = SmallSetVector<Function *, 8>;
114
115} // end anonymous namespace
116
117static void addLocAccess(MemoryEffects &ME, const MemoryLocation &Loc,
118 ModRefInfo MR, AAResults &AAR) {
119 // Ignore accesses to known-invariant or local memory.
120 MR &= AAR.getModRefInfoMask(Loc, /*IgnoreLocal=*/true);
121 if (isNoModRef(MR))
122 return;
123
124 const Value *UO = getUnderlyingObjectAggressive(Loc.Ptr);
125 if (isa<AllocaInst>(UO))
126 return;
127 if (isa<Argument>(UO)) {
129 return;
130 }
131
132 // If it's not an identified object, it might be an argument.
133 if (!isIdentifiedObject(UO))
135 ME |= MemoryEffects(IRMemLocation::Other, MR);
136}
137
138static void addArgLocs(MemoryEffects &ME, const CallBase *Call,
139 ModRefInfo ArgMR, AAResults &AAR) {
140 for (const Value *Arg : Call->args()) {
141 if (!Arg->getType()->isPtrOrPtrVectorTy())
142 continue;
143
144 addLocAccess(ME,
145 MemoryLocation::getBeforeOrAfter(Arg, Call->getAAMetadata()),
146 ArgMR, AAR);
147 }
148}
149
150/// Returns the memory access attribute for function F using AAR for AA results,
151/// where SCCNodes is the current SCC.
152///
153/// If ThisBody is true, this function may examine the function body and will
154/// return a result pertaining to this copy of the function. If it is false, the
155/// result will be based only on AA results for the function declaration; it
156/// will be assumed that some other (perhaps less optimized) version of the
157/// function may be selected at link time.
158///
159/// The return value is split into two parts: Memory effects that always apply,
160/// and additional memory effects that apply if any of the functions in the SCC
161/// can access argmem.
162static std::pair<MemoryEffects, MemoryEffects>
164 const SCCNodeSet &SCCNodes) {
165 MemoryEffects OrigME = AAR.getMemoryEffects(&F);
166 if (OrigME.doesNotAccessMemory())
167 // Already perfect!
168 return {OrigME, MemoryEffects::none()};
169
170 if (!ThisBody)
171 return {OrigME, MemoryEffects::none()};
172
174 // Additional locations accessed if the SCC accesses argmem.
175 MemoryEffects RecursiveArgME = MemoryEffects::none();
176
177 // Inalloca and preallocated arguments are always clobbered by the call.
178 if (F.getAttributes().hasAttrSomewhere(Attribute::InAlloca) ||
179 F.getAttributes().hasAttrSomewhere(Attribute::Preallocated))
180 ME |= MemoryEffects::argMemOnly(ModRefInfo::ModRef);
181
182 // Scan the function body for instructions that may read or write memory.
183 for (Instruction &I : instructions(F)) {
184 // Some instructions can be ignored even if they read or write memory.
185 // Detect these now, skipping to the next instruction if one is found.
186 if (auto *Call = dyn_cast<CallBase>(&I)) {
187 // We can optimistically ignore calls to functions in the same SCC, with
188 // two caveats:
189 // * Calls with operand bundles may have additional effects.
190 // * Argument memory accesses may imply additional effects depending on
191 // what the argument location is.
192 if (!Call->hasOperandBundles() && Call->getCalledFunction() &&
193 SCCNodes.count(Call->getCalledFunction())) {
194 // Keep track of which additional locations are accessed if the SCC
195 // turns out to access argmem.
196 addArgLocs(RecursiveArgME, Call, ModRefInfo::ModRef, AAR);
197 continue;
198 }
199
200 MemoryEffects CallME = AAR.getMemoryEffects(Call);
201
202 // If the call doesn't access memory, we're done.
203 if (CallME.doesNotAccessMemory())
204 continue;
205
206 // A pseudo probe call shouldn't change any function attribute since it
207 // doesn't translate to a real instruction. It comes with a memory access
208 // tag to prevent itself being removed by optimizations and not block
209 // other instructions being optimized.
210 if (isa<PseudoProbeInst>(I))
211 continue;
212
213 ME |= CallME.getWithoutLoc(IRMemLocation::ArgMem);
214
215 // If the call accesses captured memory (currently part of "other") and
216 // an argument is captured (currently not tracked), then it may also
217 // access argument memory.
218 ModRefInfo OtherMR = CallME.getModRef(IRMemLocation::Other);
219 ME |= MemoryEffects::argMemOnly(OtherMR);
220
221 // Check whether all pointer arguments point to local memory, and
222 // ignore calls that only access local memory.
223 ModRefInfo ArgMR = CallME.getModRef(IRMemLocation::ArgMem);
224 if (ArgMR != ModRefInfo::NoModRef)
225 addArgLocs(ME, Call, ArgMR, AAR);
226 continue;
227 }
228
229 ModRefInfo MR = ModRefInfo::NoModRef;
230 if (I.mayWriteToMemory())
231 MR |= ModRefInfo::Mod;
232 if (I.mayReadFromMemory())
233 MR |= ModRefInfo::Ref;
234 if (MR == ModRefInfo::NoModRef)
235 continue;
236
237 std::optional<MemoryLocation> Loc = MemoryLocation::getOrNone(&I);
238 if (!Loc) {
239 // If no location is known, conservatively assume anything can be
240 // accessed.
241 ME |= MemoryEffects(MR);
242 continue;
243 }
244
245 // Volatile operations may access inaccessible memory.
246 if (I.isVolatile())
248
249 addLocAccess(ME, *Loc, MR, AAR);
250 }
251
252 return {OrigME & ME, RecursiveArgME};
253}
254
256 AAResults &AAR) {
257 return checkFunctionMemoryAccess(F, /*ThisBody=*/true, AAR, {}).first;
258}
259
260/// Deduce readonly/readnone/writeonly attributes for the SCC.
261template <typename AARGetterT>
262static void addMemoryAttrs(const SCCNodeSet &SCCNodes, AARGetterT &&AARGetter,
263 SmallSet<Function *, 8> &Changed) {
265 MemoryEffects RecursiveArgME = MemoryEffects::none();
266 for (Function *F : SCCNodes) {
267 // Call the callable parameter to look up AA results for this function.
268 AAResults &AAR = AARGetter(*F);
269 // Non-exact function definitions may not be selected at link time, and an
270 // alternative version that writes to memory may be selected. See the
271 // comment on GlobalValue::isDefinitionExact for more details.
272 auto [FnME, FnRecursiveArgME] =
273 checkFunctionMemoryAccess(*F, F->hasExactDefinition(), AAR, SCCNodes);
274 ME |= FnME;
275 RecursiveArgME |= FnRecursiveArgME;
276 // Reached bottom of the lattice, we will not be able to improve the result.
277 if (ME == MemoryEffects::unknown())
278 return;
279 }
280
281 // If the SCC accesses argmem, add recursive accesses resulting from that.
282 ModRefInfo ArgMR = ME.getModRef(IRMemLocation::ArgMem);
283 if (ArgMR != ModRefInfo::NoModRef)
284 ME |= RecursiveArgME & MemoryEffects(ArgMR);
285
286 for (Function *F : SCCNodes) {
287 MemoryEffects OldME = F->getMemoryEffects();
288 MemoryEffects NewME = ME & OldME;
289 if (NewME != OldME) {
290 ++NumMemoryAttr;
291 F->setMemoryEffects(NewME);
292 // Remove conflicting writable attributes.
293 if (!isModSet(NewME.getModRef(IRMemLocation::ArgMem)))
294 for (Argument &A : F->args())
295 A.removeAttr(Attribute::Writable);
296 Changed.insert(F);
297 }
298 }
299}
300
301// Compute definitive function attributes for a function taking into account
302// prevailing definitions and linkage types
304 ValueInfo VI,
305 DenseMap<ValueInfo, FunctionSummary *> &CachedPrevailingSummary,
307 IsPrevailing) {
308
309 if (CachedPrevailingSummary.count(VI))
310 return CachedPrevailingSummary[VI];
311
312 /// At this point, prevailing symbols have been resolved. The following leads
313 /// to returning a conservative result:
314 /// - Multiple instances with local linkage. Normally local linkage would be
315 /// unique per module
316 /// as the GUID includes the module path. We could have a guid alias if
317 /// there wasn't any distinguishing path when each file was compiled, but
318 /// that should be rare so we'll punt on those.
319
320 /// These next 2 cases should not happen and will assert:
321 /// - Multiple instances with external linkage. This should be caught in
322 /// symbol resolution
323 /// - Non-existent FunctionSummary for Aliasee. This presents a hole in our
324 /// knowledge meaning we have to go conservative.
325
326 /// Otherwise, we calculate attributes for a function as:
327 /// 1. If we have a local linkage, take its attributes. If there's somehow
328 /// multiple, bail and go conservative.
329 /// 2. If we have an external/WeakODR/LinkOnceODR linkage check that it is
330 /// prevailing, take its attributes.
331 /// 3. If we have a Weak/LinkOnce linkage the copies can have semantic
332 /// differences. However, if the prevailing copy is known it will be used
333 /// so take its attributes. If the prevailing copy is in a native file
334 /// all IR copies will be dead and propagation will go conservative.
335 /// 4. AvailableExternally summaries without a prevailing copy are known to
336 /// occur in a couple of circumstances:
337 /// a. An internal function gets imported due to its caller getting
338 /// imported, it becomes AvailableExternally but no prevailing
339 /// definition exists. Because it has to get imported along with its
340 /// caller the attributes will be captured by propagating on its
341 /// caller.
342 /// b. C++11 [temp.explicit]p10 can generate AvailableExternally
343 /// definitions of explicitly instanced template declarations
344 /// for inlining which are ultimately dropped from the TU. Since this
345 /// is localized to the TU the attributes will have already made it to
346 /// the callers.
347 /// These are edge cases and already captured by their callers so we
348 /// ignore these for now. If they become relevant to optimize in the
349 /// future this can be revisited.
350 /// 5. Otherwise, go conservative.
351
352 CachedPrevailingSummary[VI] = nullptr;
353 FunctionSummary *Local = nullptr;
354 FunctionSummary *Prevailing = nullptr;
355
356 for (const auto &GVS : VI.getSummaryList()) {
357 if (!GVS->isLive())
358 continue;
359
360 FunctionSummary *FS = dyn_cast<FunctionSummary>(GVS->getBaseObject());
361 // Virtual and Unknown (e.g. indirect) calls require going conservative
362 if (!FS || FS->fflags().HasUnknownCall)
363 return nullptr;
364
365 const auto &Linkage = GVS->linkage();
366 if (GlobalValue::isLocalLinkage(Linkage)) {
367 if (Local) {
369 dbgs()
370 << "ThinLTO FunctionAttrs: Multiple Local Linkage, bailing on "
371 "function "
372 << VI.name() << " from " << FS->modulePath() << ". Previous module "
373 << Local->modulePath() << "\n");
374 return nullptr;
375 }
376 Local = FS;
377 } else if (GlobalValue::isExternalLinkage(Linkage)) {
378 assert(IsPrevailing(VI.getGUID(), GVS.get()));
379 Prevailing = FS;
380 break;
381 } else if (GlobalValue::isWeakODRLinkage(Linkage) ||
385 if (IsPrevailing(VI.getGUID(), GVS.get())) {
386 Prevailing = FS;
387 break;
388 }
389 } else if (GlobalValue::isAvailableExternallyLinkage(Linkage)) {
390 // TODO: Handle these cases if they become meaningful
391 continue;
392 }
393 }
394
395 if (Local) {
396 assert(!Prevailing);
397 CachedPrevailingSummary[VI] = Local;
398 } else if (Prevailing) {
399 assert(!Local);
400 CachedPrevailingSummary[VI] = Prevailing;
401 }
402
403 return CachedPrevailingSummary[VI];
404}
405
407 ModuleSummaryIndex &Index,
409 IsPrevailing) {
410 // TODO: implement addNoAliasAttrs once
411 // there's more information about the return type in the summary
413 return false;
414
415 DenseMap<ValueInfo, FunctionSummary *> CachedPrevailingSummary;
416 bool Changed = false;
417
418 auto PropagateAttributes = [&](std::vector<ValueInfo> &SCCNodes) {
419 // Assume we can propagate unless we discover otherwise
420 FunctionSummary::FFlags InferredFlags;
421 InferredFlags.NoRecurse = (SCCNodes.size() == 1);
422 InferredFlags.NoUnwind = true;
423
424 for (auto &V : SCCNodes) {
425 FunctionSummary *CallerSummary =
426 calculatePrevailingSummary(V, CachedPrevailingSummary, IsPrevailing);
427
428 // Function summaries can fail to contain information such as declarations
429 if (!CallerSummary)
430 return;
431
432 if (CallerSummary->fflags().MayThrow)
433 InferredFlags.NoUnwind = false;
434
435 for (const auto &Callee : CallerSummary->calls()) {
437 Callee.first, CachedPrevailingSummary, IsPrevailing);
438
439 if (!CalleeSummary)
440 return;
441
442 if (!CalleeSummary->fflags().NoRecurse)
443 InferredFlags.NoRecurse = false;
444
445 if (!CalleeSummary->fflags().NoUnwind)
446 InferredFlags.NoUnwind = false;
447
448 if (!InferredFlags.NoUnwind && !InferredFlags.NoRecurse)
449 break;
450 }
451 }
452
453 if (InferredFlags.NoUnwind || InferredFlags.NoRecurse) {
454 Changed = true;
455 for (auto &V : SCCNodes) {
456 if (InferredFlags.NoRecurse) {
457 LLVM_DEBUG(dbgs() << "ThinLTO FunctionAttrs: Propagated NoRecurse to "
458 << V.name() << "\n");
459 ++NumThinLinkNoRecurse;
460 }
461
462 if (InferredFlags.NoUnwind) {
463 LLVM_DEBUG(dbgs() << "ThinLTO FunctionAttrs: Propagated NoUnwind to "
464 << V.name() << "\n");
465 ++NumThinLinkNoUnwind;
466 }
467
468 for (const auto &S : V.getSummaryList()) {
469 if (auto *FS = dyn_cast<FunctionSummary>(S.get())) {
470 if (InferredFlags.NoRecurse)
471 FS->setNoRecurse();
472
473 if (InferredFlags.NoUnwind)
474 FS->setNoUnwind();
475 }
476 }
477 }
478 }
479 };
480
481 // Call propagation functions on each SCC in the Index
482 for (scc_iterator<ModuleSummaryIndex *> I = scc_begin(&Index); !I.isAtEnd();
483 ++I) {
484 std::vector<ValueInfo> Nodes(*I);
485 PropagateAttributes(Nodes);
486 }
487 return Changed;
488}
489
490namespace {
491
492/// For a given pointer Argument, this retains a list of Arguments of functions
493/// in the same SCC that the pointer data flows into. We use this to build an
494/// SCC of the arguments.
495struct ArgumentGraphNode {
496 Argument *Definition;
498};
499
500class ArgumentGraph {
501 // We store pointers to ArgumentGraphNode objects, so it's important that
502 // that they not move around upon insert.
503 using ArgumentMapTy = std::map<Argument *, ArgumentGraphNode>;
504
505 ArgumentMapTy ArgumentMap;
506
507 // There is no root node for the argument graph, in fact:
508 // void f(int *x, int *y) { if (...) f(x, y); }
509 // is an example where the graph is disconnected. The SCCIterator requires a
510 // single entry point, so we maintain a fake ("synthetic") root node that
511 // uses every node. Because the graph is directed and nothing points into
512 // the root, it will not participate in any SCCs (except for its own).
513 ArgumentGraphNode SyntheticRoot;
514
515public:
516 ArgumentGraph() { SyntheticRoot.Definition = nullptr; }
517
519
520 iterator begin() { return SyntheticRoot.Uses.begin(); }
521 iterator end() { return SyntheticRoot.Uses.end(); }
522 ArgumentGraphNode *getEntryNode() { return &SyntheticRoot; }
523
524 ArgumentGraphNode *operator[](Argument *A) {
525 ArgumentGraphNode &Node = ArgumentMap[A];
526 Node.Definition = A;
527 SyntheticRoot.Uses.push_back(&Node);
528 return &Node;
529 }
530};
531
532/// This tracker checks whether callees are in the SCC, and if so it does not
533/// consider that a capture, instead adding it to the "Uses" list and
534/// continuing with the analysis.
535struct ArgumentUsesTracker : public CaptureTracker {
536 ArgumentUsesTracker(const SCCNodeSet &SCCNodes) : SCCNodes(SCCNodes) {}
537
538 void tooManyUses() override { Captured = true; }
539
540 bool captured(const Use *U) override {
541 CallBase *CB = dyn_cast<CallBase>(U->getUser());
542 if (!CB) {
543 Captured = true;
544 return true;
545 }
546
548 if (!F || !F->hasExactDefinition() || !SCCNodes.count(F)) {
549 Captured = true;
550 return true;
551 }
552
553 assert(!CB->isCallee(U) && "callee operand reported captured?");
554 const unsigned UseIndex = CB->getDataOperandNo(U);
555 if (UseIndex >= CB->arg_size()) {
556 // Data operand, but not a argument operand -- must be a bundle operand
557 assert(CB->hasOperandBundles() && "Must be!");
558
559 // CaptureTracking told us that we're being captured by an operand bundle
560 // use. In this case it does not matter if the callee is within our SCC
561 // or not -- we've been captured in some unknown way, and we have to be
562 // conservative.
563 Captured = true;
564 return true;
565 }
566
567 if (UseIndex >= F->arg_size()) {
568 assert(F->isVarArg() && "More params than args in non-varargs call");
569 Captured = true;
570 return true;
571 }
572
573 Uses.push_back(&*std::next(F->arg_begin(), UseIndex));
574 return false;
575 }
576
577 // True only if certainly captured (used outside our SCC).
578 bool Captured = false;
579
580 // Uses within our SCC.
582
583 const SCCNodeSet &SCCNodes;
584};
585
586/// A struct of argument use: a Use and the offset it accesses. This struct
587/// is to track uses inside function via GEP. If GEP has a non-constant index,
588/// the Offset field is nullopt.
589struct ArgumentUse {
590 Use *U;
591 std::optional<int64_t> Offset;
592};
593
594/// A struct of argument access info. "Unknown" accesses are the cases like
595/// unrecognized instructions, instructions that have more than one use of
596/// the argument, or volatile memory accesses. "WriteWithSideEffect" are call
597/// instructions that not only write an argument but also capture it.
598struct ArgumentAccessInfo {
599 enum class AccessType : uint8_t { Write, WriteWithSideEffect, Read, Unknown };
600 AccessType ArgAccessType;
601 ConstantRangeList AccessRanges;
602};
603
604/// A struct to wrap the argument use info per block.
605struct UsesPerBlockInfo {
607 bool HasWrites = false;
608 bool HasUnknownAccess = false;
609};
610
611/// A struct to summarize the argument use info in a function.
612struct ArgumentUsesSummary {
613 bool HasAnyWrite = false;
614 bool HasWriteOutsideEntryBB = false;
616};
617
618ArgumentAccessInfo getArgmentAccessInfo(const Instruction *I,
619 const ArgumentUse &ArgUse,
620 const DataLayout &DL) {
621 auto GetTypeAccessRange =
622 [&DL](Type *Ty,
623 std::optional<int64_t> Offset) -> std::optional<ConstantRange> {
624 auto TypeSize = DL.getTypeStoreSize(Ty);
625 if (!TypeSize.isScalable() && Offset) {
626 int64_t Size = TypeSize.getFixedValue();
627 return ConstantRange(APInt(64, *Offset, true),
628 APInt(64, *Offset + Size, true));
629 }
630 return std::nullopt;
631 };
632 auto GetConstantIntRange =
633 [](Value *Length,
634 std::optional<int64_t> Offset) -> std::optional<ConstantRange> {
635 auto *ConstantLength = dyn_cast<ConstantInt>(Length);
636 if (ConstantLength && Offset &&
637 ConstantLength->getValue().isStrictlyPositive()) {
638 return ConstantRange(
639 APInt(64, *Offset, true),
640 APInt(64, *Offset + ConstantLength->getSExtValue(), true));
641 }
642 return std::nullopt;
643 };
644 if (auto *SI = dyn_cast<StoreInst>(I)) {
645 if (SI->isSimple() && &SI->getOperandUse(1) == ArgUse.U) {
646 // Get the fixed type size of "SI". Since the access range of a write
647 // will be unioned, if "SI" doesn't have a fixed type size, we just set
648 // the access range to empty.
649 ConstantRangeList AccessRanges;
650 if (auto TypeAccessRange =
651 GetTypeAccessRange(SI->getAccessType(), ArgUse.Offset))
652 AccessRanges.insert(*TypeAccessRange);
653 return {ArgumentAccessInfo::AccessType::Write, std::move(AccessRanges)};
654 }
655 } else if (auto *LI = dyn_cast<LoadInst>(I)) {
656 if (LI->isSimple()) {
657 assert(&LI->getOperandUse(0) == ArgUse.U);
658 // Get the fixed type size of "LI". Different from Write, if "LI"
659 // doesn't have a fixed type size, we conservatively set as a clobber
660 // with an empty access range.
661 if (auto TypeAccessRange =
662 GetTypeAccessRange(LI->getAccessType(), ArgUse.Offset))
663 return {ArgumentAccessInfo::AccessType::Read, {*TypeAccessRange}};
664 }
665 } else if (auto *MemSet = dyn_cast<MemSetInst>(I)) {
666 if (!MemSet->isVolatile()) {
667 ConstantRangeList AccessRanges;
668 if (auto AccessRange =
669 GetConstantIntRange(MemSet->getLength(), ArgUse.Offset))
670 AccessRanges.insert(*AccessRange);
671 return {ArgumentAccessInfo::AccessType::Write, AccessRanges};
672 }
673 } else if (auto *MTI = dyn_cast<MemTransferInst>(I)) {
674 if (!MTI->isVolatile()) {
675 if (&MTI->getOperandUse(0) == ArgUse.U) {
676 ConstantRangeList AccessRanges;
677 if (auto AccessRange =
678 GetConstantIntRange(MTI->getLength(), ArgUse.Offset))
679 AccessRanges.insert(*AccessRange);
680 return {ArgumentAccessInfo::AccessType::Write, AccessRanges};
681 } else if (&MTI->getOperandUse(1) == ArgUse.U) {
682 if (auto AccessRange =
683 GetConstantIntRange(MTI->getLength(), ArgUse.Offset))
684 return {ArgumentAccessInfo::AccessType::Read, {*AccessRange}};
685 }
686 }
687 } else if (auto *CB = dyn_cast<CallBase>(I)) {
688 if (CB->isArgOperand(ArgUse.U) &&
689 !CB->isByValArgument(CB->getArgOperandNo(ArgUse.U))) {
690 unsigned ArgNo = CB->getArgOperandNo(ArgUse.U);
691 bool IsInitialize = CB->paramHasAttr(ArgNo, Attribute::Initializes);
692 if (IsInitialize && ArgUse.Offset) {
693 // Argument is a Write when parameter is writeonly/readnone
694 // and nocapture. Otherwise, it's a WriteWithSideEffect.
695 auto Access = CB->onlyWritesMemory(ArgNo) && CB->doesNotCapture(ArgNo)
696 ? ArgumentAccessInfo::AccessType::Write
697 : ArgumentAccessInfo::AccessType::WriteWithSideEffect;
698 ConstantRangeList AccessRanges;
699 Attribute Attr = CB->getParamAttr(ArgNo, Attribute::Initializes);
701 for (ConstantRange &CR : CBCRL)
702 AccessRanges.insert(ConstantRange(CR.getLower() + *ArgUse.Offset,
703 CR.getUpper() + *ArgUse.Offset));
704 return {Access, AccessRanges};
705 }
706 }
707 }
708 // Other unrecognized instructions are considered as unknown.
709 return {ArgumentAccessInfo::AccessType::Unknown, {}};
710}
711
712// Collect the uses of argument "A" in "F".
713ArgumentUsesSummary collectArgumentUsesPerBlock(Argument &A, Function &F) {
714 auto &DL = F.getParent()->getDataLayout();
715 unsigned PointerSize =
716 DL.getIndexSizeInBits(A.getType()->getPointerAddressSpace());
717 ArgumentUsesSummary Result;
718
719 BasicBlock &EntryBB = F.getEntryBlock();
721 for (Use &U : A.uses())
722 Worklist.push_back({&U, 0});
723
724 // Update "UsesPerBlock" with the block of "I" as key and "Info" as value.
725 // Return true if the block of "I" has write accesses after updating.
726 auto UpdateUseInfo = [&Result](Instruction *I, ArgumentAccessInfo Info) {
727 auto *BB = I->getParent();
728 auto &BBInfo = Result.UsesPerBlock[BB];
729 bool AlreadyVisitedInst = BBInfo.Insts.contains(I);
730 auto &IInfo = BBInfo.Insts[I];
731
732 // Instructions that have more than one use of the argument are considered
733 // as clobbers.
734 if (AlreadyVisitedInst) {
735 IInfo = {ArgumentAccessInfo::AccessType::Unknown, {}};
736 BBInfo.HasUnknownAccess = true;
737 return false;
738 }
739
740 IInfo = std::move(Info);
741 BBInfo.HasUnknownAccess |=
742 IInfo.ArgAccessType == ArgumentAccessInfo::AccessType::Unknown;
743 bool InfoHasWrites =
744 (IInfo.ArgAccessType == ArgumentAccessInfo::AccessType::Write ||
745 IInfo.ArgAccessType ==
746 ArgumentAccessInfo::AccessType::WriteWithSideEffect) &&
747 !IInfo.AccessRanges.empty();
748 BBInfo.HasWrites |= InfoHasWrites;
749 return InfoHasWrites;
750 };
751
752 // No need for a visited set because we don't look through phis, so there are
753 // no cycles.
754 while (!Worklist.empty()) {
755 ArgumentUse ArgUse = Worklist.pop_back_val();
756 User *U = ArgUse.U->getUser();
757 // Add GEP uses to worklist.
758 // If the GEP is not a constant GEP, set the ArgumentUse::Offset to nullopt.
759 if (auto *GEP = dyn_cast<GEPOperator>(U)) {
760 std::optional<int64_t> NewOffset = std::nullopt;
761 if (ArgUse.Offset) {
762 APInt Offset(PointerSize, 0);
763 if (GEP->accumulateConstantOffset(DL, Offset))
764 NewOffset = *ArgUse.Offset + Offset.getSExtValue();
765 }
766 for (Use &U : GEP->uses())
767 Worklist.push_back({&U, NewOffset});
768 continue;
769 }
770
771 auto *I = cast<Instruction>(U);
772 bool HasWrite = UpdateUseInfo(I, getArgmentAccessInfo(I, ArgUse, DL));
773
774 Result.HasAnyWrite |= HasWrite;
775
776 if (HasWrite && I->getParent() != &EntryBB)
777 Result.HasWriteOutsideEntryBB = true;
778 }
779 return Result;
780}
781
782} // end anonymous namespace
783
784namespace llvm {
785
786template <> struct GraphTraits<ArgumentGraphNode *> {
787 using NodeRef = ArgumentGraphNode *;
789
790 static NodeRef getEntryNode(NodeRef A) { return A; }
791 static ChildIteratorType child_begin(NodeRef N) { return N->Uses.begin(); }
792 static ChildIteratorType child_end(NodeRef N) { return N->Uses.end(); }
793};
794
795template <>
796struct GraphTraits<ArgumentGraph *> : public GraphTraits<ArgumentGraphNode *> {
797 static NodeRef getEntryNode(ArgumentGraph *AG) { return AG->getEntryNode(); }
798
799 static ChildIteratorType nodes_begin(ArgumentGraph *AG) {
800 return AG->begin();
801 }
802
803 static ChildIteratorType nodes_end(ArgumentGraph *AG) { return AG->end(); }
804};
805
806} // end namespace llvm
807
808/// Returns Attribute::None, Attribute::ReadOnly or Attribute::ReadNone.
811 const SmallPtrSet<Argument *, 8> &SCCNodes) {
812 SmallVector<Use *, 32> Worklist;
814
815 // inalloca arguments are always clobbered by the call.
816 if (A->hasInAllocaAttr() || A->hasPreallocatedAttr())
817 return Attribute::None;
818
819 bool IsRead = false;
820 bool IsWrite = false;
821
822 for (Use &U : A->uses()) {
823 Visited.insert(&U);
824 Worklist.push_back(&U);
825 }
826
827 while (!Worklist.empty()) {
828 if (IsWrite && IsRead)
829 // No point in searching further..
830 return Attribute::None;
831
832 Use *U = Worklist.pop_back_val();
833 Instruction *I = cast<Instruction>(U->getUser());
834
835 switch (I->getOpcode()) {
836 case Instruction::BitCast:
837 case Instruction::GetElementPtr:
838 case Instruction::PHI:
839 case Instruction::Select:
840 case Instruction::AddrSpaceCast:
841 // The original value is not read/written via this if the new value isn't.
842 for (Use &UU : I->uses())
843 if (Visited.insert(&UU).second)
844 Worklist.push_back(&UU);
845 break;
846
847 case Instruction::Call:
848 case Instruction::Invoke: {
849 CallBase &CB = cast<CallBase>(*I);
850 if (CB.isCallee(U)) {
851 IsRead = true;
852 // Note that indirect calls do not capture, see comment in
853 // CaptureTracking for context
854 continue;
855 }
856
857 // Given we've explicitly handled the callee operand above, what's left
858 // must be a data operand (e.g. argument or operand bundle)
859 const unsigned UseIndex = CB.getDataOperandNo(U);
860
861 // Some intrinsics (for instance ptrmask) do not capture their results,
862 // but return results thas alias their pointer argument, and thus should
863 // be handled like GEP or addrspacecast above.
865 &CB, /*MustPreserveNullness=*/false)) {
866 for (Use &UU : CB.uses())
867 if (Visited.insert(&UU).second)
868 Worklist.push_back(&UU);
869 } else if (!CB.doesNotCapture(UseIndex)) {
870 if (!CB.onlyReadsMemory())
871 // If the callee can save a copy into other memory, then simply
872 // scanning uses of the call is insufficient. We have no way
873 // of tracking copies of the pointer through memory to see
874 // if a reloaded copy is written to, thus we must give up.
875 return Attribute::None;
876 // Push users for processing once we finish this one
877 if (!I->getType()->isVoidTy())
878 for (Use &UU : I->uses())
879 if (Visited.insert(&UU).second)
880 Worklist.push_back(&UU);
881 }
882
884 if (isNoModRef(ArgMR))
885 continue;
886
887 if (Function *F = CB.getCalledFunction())
888 if (CB.isArgOperand(U) && UseIndex < F->arg_size() &&
889 SCCNodes.count(F->getArg(UseIndex)))
890 // This is an argument which is part of the speculative SCC. Note
891 // that only operands corresponding to formal arguments of the callee
892 // can participate in the speculation.
893 break;
894
895 // The accessors used on call site here do the right thing for calls and
896 // invokes with operand bundles.
897 if (CB.doesNotAccessMemory(UseIndex)) {
898 /* nop */
899 } else if (!isModSet(ArgMR) || CB.onlyReadsMemory(UseIndex)) {
900 IsRead = true;
901 } else if (!isRefSet(ArgMR) ||
902 CB.dataOperandHasImpliedAttr(UseIndex, Attribute::WriteOnly)) {
903 IsWrite = true;
904 } else {
905 return Attribute::None;
906 }
907 break;
908 }
909
910 case Instruction::Load:
911 // A volatile load has side effects beyond what readonly can be relied
912 // upon.
913 if (cast<LoadInst>(I)->isVolatile())
914 return Attribute::None;
915
916 IsRead = true;
917 break;
918
919 case Instruction::Store:
920 if (cast<StoreInst>(I)->getValueOperand() == *U)
921 // untrackable capture
922 return Attribute::None;
923
924 // A volatile store has side effects beyond what writeonly can be relied
925 // upon.
926 if (cast<StoreInst>(I)->isVolatile())
927 return Attribute::None;
928
929 IsWrite = true;
930 break;
931
932 case Instruction::ICmp:
933 case Instruction::Ret:
934 break;
935
936 default:
937 return Attribute::None;
938 }
939 }
940
941 if (IsWrite && IsRead)
942 return Attribute::None;
943 else if (IsRead)
944 return Attribute::ReadOnly;
945 else if (IsWrite)
946 return Attribute::WriteOnly;
947 else
948 return Attribute::ReadNone;
949}
950
951/// Deduce returned attributes for the SCC.
952static void addArgumentReturnedAttrs(const SCCNodeSet &SCCNodes,
953 SmallSet<Function *, 8> &Changed) {
954 // Check each function in turn, determining if an argument is always returned.
955 for (Function *F : SCCNodes) {
956 // We can infer and propagate function attributes only when we know that the
957 // definition we'll get at link time is *exactly* the definition we see now.
958 // For more details, see GlobalValue::mayBeDerefined.
959 if (!F->hasExactDefinition())
960 continue;
961
962 if (F->getReturnType()->isVoidTy())
963 continue;
964
965 // There is nothing to do if an argument is already marked as 'returned'.
966 if (F->getAttributes().hasAttrSomewhere(Attribute::Returned))
967 continue;
968
969 auto FindRetArg = [&]() -> Argument * {
970 Argument *RetArg = nullptr;
971 for (BasicBlock &BB : *F)
972 if (auto *Ret = dyn_cast<ReturnInst>(BB.getTerminator())) {
973 // Note that stripPointerCasts should look through functions with
974 // returned arguments.
975 auto *RetVal =
976 dyn_cast<Argument>(Ret->getReturnValue()->stripPointerCasts());
977 if (!RetVal || RetVal->getType() != F->getReturnType())
978 return nullptr;
979
980 if (!RetArg)
981 RetArg = RetVal;
982 else if (RetArg != RetVal)
983 return nullptr;
984 }
985
986 return RetArg;
987 };
988
989 if (Argument *RetArg = FindRetArg()) {
990 RetArg->addAttr(Attribute::Returned);
991 ++NumReturned;
992 Changed.insert(F);
993 }
994 }
995}
996
997/// If a callsite has arguments that are also arguments to the parent function,
998/// try to propagate attributes from the callsite's arguments to the parent's
999/// arguments. This may be important because inlining can cause information loss
1000/// when attribute knowledge disappears with the inlined call.
1003 return false;
1004
1005 bool Changed = false;
1006
1007 // For an argument attribute to transfer from a callsite to the parent, the
1008 // call must be guaranteed to execute every time the parent is called.
1009 // Conservatively, just check for calls in the entry block that are guaranteed
1010 // to execute.
1011 // TODO: This could be enhanced by testing if the callsite post-dominates the
1012 // entry block or by doing simple forward walks or backward walks to the
1013 // callsite.
1014 BasicBlock &Entry = F.getEntryBlock();
1015 for (Instruction &I : Entry) {
1016 if (auto *CB = dyn_cast<CallBase>(&I)) {
1017 if (auto *CalledFunc = CB->getCalledFunction()) {
1018 for (auto &CSArg : CalledFunc->args()) {
1019 if (!CSArg.hasNonNullAttr(/* AllowUndefOrPoison */ false))
1020 continue;
1021
1022 // If the non-null callsite argument operand is an argument to 'F'
1023 // (the caller) and the call is guaranteed to execute, then the value
1024 // must be non-null throughout 'F'.
1025 auto *FArg = dyn_cast<Argument>(CB->getArgOperand(CSArg.getArgNo()));
1026 if (FArg && !FArg->hasNonNullAttr()) {
1027 FArg->addAttr(Attribute::NonNull);
1028 Changed = true;
1029 }
1030 }
1031 }
1032 }
1034 break;
1035 }
1036
1037 return Changed;
1038}
1039
1041 assert((R == Attribute::ReadOnly || R == Attribute::ReadNone ||
1042 R == Attribute::WriteOnly)
1043 && "Must be an access attribute.");
1044 assert(A && "Argument must not be null.");
1045
1046 // If the argument already has the attribute, nothing needs to be done.
1047 if (A->hasAttribute(R))
1048 return false;
1049
1050 // Otherwise, remove potentially conflicting attribute, add the new one,
1051 // and update statistics.
1052 A->removeAttr(Attribute::WriteOnly);
1053 A->removeAttr(Attribute::ReadOnly);
1054 A->removeAttr(Attribute::ReadNone);
1055 // Remove conflicting writable attribute.
1056 if (R == Attribute::ReadNone || R == Attribute::ReadOnly)
1057 A->removeAttr(Attribute::Writable);
1058 A->addAttr(R);
1059 if (R == Attribute::ReadOnly)
1060 ++NumReadOnlyArg;
1061 else if (R == Attribute::WriteOnly)
1062 ++NumWriteOnlyArg;
1063 else
1064 ++NumReadNoneArg;
1065 return true;
1066}
1067
1069 auto ArgumentUses = collectArgumentUsesPerBlock(A, F);
1070 // No write anywhere in the function, bail.
1071 if (!ArgumentUses.HasAnyWrite)
1072 return false;
1073
1074 auto &UsesPerBlock = ArgumentUses.UsesPerBlock;
1075 BasicBlock &EntryBB = F.getEntryBlock();
1076 // A map to store the argument ranges initialized by a BasicBlock (including
1077 // its successors).
1079 // Visit the successors of "BB" block and the instructions in BB (post-order)
1080 // to get the argument ranges initialized by "BB" (including its successors).
1081 // The result will be cached in "Initialized".
1082 auto VisitBlock = [&](const BasicBlock *BB) -> ConstantRangeList {
1083 auto UPB = UsesPerBlock.find(BB);
1085
1086 // Start with intersection of successors.
1087 // If this block has any clobbering use, we're going to clear out the
1088 // ranges at some point in this block anyway, so don't bother looking at
1089 // successors.
1090 if (UPB == UsesPerBlock.end() || !UPB->second.HasUnknownAccess) {
1091 bool HasAddedSuccessor = false;
1092 for (auto *Succ : successors(BB)) {
1093 if (auto SuccI = Initialized.find(Succ); SuccI != Initialized.end()) {
1094 if (HasAddedSuccessor) {
1095 CRL = CRL.intersectWith(SuccI->second);
1096 } else {
1097 CRL = SuccI->second;
1098 HasAddedSuccessor = true;
1099 }
1100 } else {
1101 CRL = ConstantRangeList();
1102 break;
1103 }
1104 }
1105 }
1106
1107 if (UPB != UsesPerBlock.end()) {
1108 // Sort uses in this block by instruction order.
1110 append_range(Insts, UPB->second.Insts);
1111 sort(Insts, [](std::pair<Instruction *, ArgumentAccessInfo> &LHS,
1112 std::pair<Instruction *, ArgumentAccessInfo> &RHS) {
1113 return LHS.first->comesBefore(RHS.first);
1114 });
1115
1116 // From the end of the block to the beginning of the block, set
1117 // initializes ranges.
1118 for (auto &[_, Info] : reverse(Insts)) {
1119 if (Info.ArgAccessType == ArgumentAccessInfo::AccessType::Unknown ||
1120 Info.ArgAccessType ==
1121 ArgumentAccessInfo::AccessType::WriteWithSideEffect)
1122 CRL = ConstantRangeList();
1123 if (!Info.AccessRanges.empty()) {
1124 if (Info.ArgAccessType == ArgumentAccessInfo::AccessType::Write ||
1125 Info.ArgAccessType ==
1126 ArgumentAccessInfo::AccessType::WriteWithSideEffect) {
1127 CRL = CRL.unionWith(Info.AccessRanges);
1128 } else {
1129 assert(Info.ArgAccessType == ArgumentAccessInfo::AccessType::Read);
1130 for (const auto &ReadRange : Info.AccessRanges)
1131 CRL.subtract(ReadRange);
1132 }
1133 }
1134 }
1135 }
1136 return CRL;
1137 };
1138
1139 ConstantRangeList EntryCRL;
1140 // If all write instructions are in the EntryBB, or if the EntryBB has
1141 // a clobbering use, we only need to look at EntryBB.
1142 bool OnlyScanEntryBlock = !ArgumentUses.HasWriteOutsideEntryBB;
1143 if (!OnlyScanEntryBlock)
1144 if (auto EntryUPB = UsesPerBlock.find(&EntryBB);
1145 EntryUPB != UsesPerBlock.end())
1146 OnlyScanEntryBlock = EntryUPB->second.HasUnknownAccess;
1147 if (OnlyScanEntryBlock) {
1148 EntryCRL = VisitBlock(&EntryBB);
1149 if (EntryCRL.empty())
1150 return false;
1151 } else {
1152 // Now we have to go through CFG to get the initialized argument ranges
1153 // across blocks. With dominance and post-dominance, the initialized ranges
1154 // by a block include both accesses inside this block and accesses in its
1155 // (transitive) successors. So visit successors before predecessors with a
1156 // post-order walk of the blocks and memorize the results in "Initialized".
1157 for (const BasicBlock *BB : post_order(&F)) {
1158 ConstantRangeList CRL = VisitBlock(BB);
1159 if (!CRL.empty())
1160 Initialized[BB] = CRL;
1161 }
1162
1163 auto EntryCRLI = Initialized.find(&EntryBB);
1164 if (EntryCRLI == Initialized.end())
1165 return false;
1166
1167 EntryCRL = EntryCRLI->second;
1168 }
1169
1170 assert(!EntryCRL.empty() &&
1171 "should have bailed already if EntryCRL is empty");
1172
1173 if (A.hasAttribute(Attribute::Initializes)) {
1174 ConstantRangeList PreviousCRL =
1175 A.getAttribute(Attribute::Initializes).getValueAsConstantRangeList();
1176 if (PreviousCRL == EntryCRL)
1177 return false;
1178 EntryCRL = EntryCRL.unionWith(PreviousCRL);
1179 }
1180
1181 A.addAttr(Attribute::get(A.getContext(), Attribute::Initializes,
1182 EntryCRL.rangesRef()));
1183
1184 return true;
1185}
1186
1187/// Deduce nocapture attributes for the SCC.
1188static void addArgumentAttrs(const SCCNodeSet &SCCNodes,
1189 SmallSet<Function *, 8> &Changed,
1190 bool SkipInitializes) {
1191 ArgumentGraph AG;
1192
1193 // Check each function in turn, determining which pointer arguments are not
1194 // captured.
1195 for (Function *F : SCCNodes) {
1196 // We can infer and propagate function attributes only when we know that the
1197 // definition we'll get at link time is *exactly* the definition we see now.
1198 // For more details, see GlobalValue::mayBeDerefined.
1199 if (!F->hasExactDefinition())
1200 continue;
1201
1203 Changed.insert(F);
1204
1205 // Functions that are readonly (or readnone) and nounwind and don't return
1206 // a value can't capture arguments. Don't analyze them.
1207 if (F->onlyReadsMemory() && F->doesNotThrow() &&
1208 F->getReturnType()->isVoidTy()) {
1209 for (Argument &A : F->args()) {
1210 if (A.getType()->isPointerTy() && !A.hasNoCaptureAttr()) {
1211 A.addAttr(Attribute::NoCapture);
1212 ++NumNoCapture;
1213 Changed.insert(F);
1214 }
1215 }
1216 continue;
1217 }
1218
1219 for (Argument &A : F->args()) {
1220 if (!A.getType()->isPointerTy())
1221 continue;
1222 bool HasNonLocalUses = false;
1223 if (!A.hasNoCaptureAttr()) {
1224 ArgumentUsesTracker Tracker(SCCNodes);
1225 PointerMayBeCaptured(&A, &Tracker);
1226 if (!Tracker.Captured) {
1227 if (Tracker.Uses.empty()) {
1228 // If it's trivially not captured, mark it nocapture now.
1229 A.addAttr(Attribute::NoCapture);
1230 ++NumNoCapture;
1231 Changed.insert(F);
1232 } else {
1233 // If it's not trivially captured and not trivially not captured,
1234 // then it must be calling into another function in our SCC. Save
1235 // its particulars for Argument-SCC analysis later.
1236 ArgumentGraphNode *Node = AG[&A];
1237 for (Argument *Use : Tracker.Uses) {
1238 Node->Uses.push_back(AG[Use]);
1239 if (Use != &A)
1240 HasNonLocalUses = true;
1241 }
1242 }
1243 }
1244 // Otherwise, it's captured. Don't bother doing SCC analysis on it.
1245 }
1246 if (!HasNonLocalUses && !A.onlyReadsMemory()) {
1247 // Can we determine that it's readonly/readnone/writeonly without doing
1248 // an SCC? Note that we don't allow any calls at all here, or else our
1249 // result will be dependent on the iteration order through the
1250 // functions in the SCC.
1252 Self.insert(&A);
1254 if (R != Attribute::None)
1255 if (addAccessAttr(&A, R))
1256 Changed.insert(F);
1257 }
1258 if (!SkipInitializes && !A.onlyReadsMemory()) {
1259 if (inferInitializes(A, *F))
1260 Changed.insert(F);
1261 }
1262 }
1263 }
1264
1265 // The graph we've collected is partial because we stopped scanning for
1266 // argument uses once we solved the argument trivially. These partial nodes
1267 // show up as ArgumentGraphNode objects with an empty Uses list, and for
1268 // these nodes the final decision about whether they capture has already been
1269 // made. If the definition doesn't have a 'nocapture' attribute by now, it
1270 // captures.
1271
1272 for (scc_iterator<ArgumentGraph *> I = scc_begin(&AG); !I.isAtEnd(); ++I) {
1273 const std::vector<ArgumentGraphNode *> &ArgumentSCC = *I;
1274 if (ArgumentSCC.size() == 1) {
1275 if (!ArgumentSCC[0]->Definition)
1276 continue; // synthetic root node
1277
1278 // eg. "void f(int* x) { if (...) f(x); }"
1279 if (ArgumentSCC[0]->Uses.size() == 1 &&
1280 ArgumentSCC[0]->Uses[0] == ArgumentSCC[0]) {
1281 Argument *A = ArgumentSCC[0]->Definition;
1282 A->addAttr(Attribute::NoCapture);
1283 ++NumNoCapture;
1284 Changed.insert(A->getParent());
1285
1286 // Infer the access attributes given the new nocapture one
1288 Self.insert(&*A);
1290 if (R != Attribute::None)
1291 addAccessAttr(A, R);
1292 }
1293 continue;
1294 }
1295
1296 bool SCCCaptured = false;
1297 for (ArgumentGraphNode *Node : ArgumentSCC) {
1298 if (Node->Uses.empty() && !Node->Definition->hasNoCaptureAttr()) {
1299 SCCCaptured = true;
1300 break;
1301 }
1302 }
1303 if (SCCCaptured)
1304 continue;
1305
1306 SmallPtrSet<Argument *, 8> ArgumentSCCNodes;
1307 // Fill ArgumentSCCNodes with the elements of the ArgumentSCC. Used for
1308 // quickly looking up whether a given Argument is in this ArgumentSCC.
1309 for (ArgumentGraphNode *I : ArgumentSCC) {
1310 ArgumentSCCNodes.insert(I->Definition);
1311 }
1312
1313 for (ArgumentGraphNode *N : ArgumentSCC) {
1314 for (ArgumentGraphNode *Use : N->Uses) {
1315 Argument *A = Use->Definition;
1316 if (A->hasNoCaptureAttr() || ArgumentSCCNodes.count(A))
1317 continue;
1318 SCCCaptured = true;
1319 break;
1320 }
1321 if (SCCCaptured)
1322 break;
1323 }
1324 if (SCCCaptured)
1325 continue;
1326
1327 for (ArgumentGraphNode *N : ArgumentSCC) {
1328 Argument *A = N->Definition;
1329 A->addAttr(Attribute::NoCapture);
1330 ++NumNoCapture;
1331 Changed.insert(A->getParent());
1332 }
1333
1334 // We also want to compute readonly/readnone/writeonly. With a small number
1335 // of false negatives, we can assume that any pointer which is captured
1336 // isn't going to be provably readonly or readnone, since by definition
1337 // we can't analyze all uses of a captured pointer.
1338 //
1339 // The false negatives happen when the pointer is captured by a function
1340 // that promises readonly/readnone behaviour on the pointer, then the
1341 // pointer's lifetime ends before anything that writes to arbitrary memory.
1342 // Also, a readonly/readnone pointer may be returned, but returning a
1343 // pointer is capturing it.
1344
1345 auto meetAccessAttr = [](Attribute::AttrKind A, Attribute::AttrKind B) {
1346 if (A == B)
1347 return A;
1348 if (A == Attribute::ReadNone)
1349 return B;
1350 if (B == Attribute::ReadNone)
1351 return A;
1352 return Attribute::None;
1353 };
1354
1355 Attribute::AttrKind AccessAttr = Attribute::ReadNone;
1356 for (ArgumentGraphNode *N : ArgumentSCC) {
1357 Argument *A = N->Definition;
1358 Attribute::AttrKind K = determinePointerAccessAttrs(A, ArgumentSCCNodes);
1359 AccessAttr = meetAccessAttr(AccessAttr, K);
1360 if (AccessAttr == Attribute::None)
1361 break;
1362 }
1363
1364 if (AccessAttr != Attribute::None) {
1365 for (ArgumentGraphNode *N : ArgumentSCC) {
1366 Argument *A = N->Definition;
1367 if (addAccessAttr(A, AccessAttr))
1368 Changed.insert(A->getParent());
1369 }
1370 }
1371 }
1372}
1373
1374/// Tests whether a function is "malloc-like".
1375///
1376/// A function is "malloc-like" if it returns either null or a pointer that
1377/// doesn't alias any other pointer visible to the caller.
1378static bool isFunctionMallocLike(Function *F, const SCCNodeSet &SCCNodes) {
1379 SmallSetVector<Value *, 8> FlowsToReturn;
1380 for (BasicBlock &BB : *F)
1381 if (ReturnInst *Ret = dyn_cast<ReturnInst>(BB.getTerminator()))
1382 FlowsToReturn.insert(Ret->getReturnValue());
1383
1384 for (unsigned i = 0; i != FlowsToReturn.size(); ++i) {
1385 Value *RetVal = FlowsToReturn[i];
1386
1387 if (Constant *C = dyn_cast<Constant>(RetVal)) {
1388 if (!C->isNullValue() && !isa<UndefValue>(C))
1389 return false;
1390
1391 continue;
1392 }
1393
1394 if (isa<Argument>(RetVal))
1395 return false;
1396
1397 if (Instruction *RVI = dyn_cast<Instruction>(RetVal))
1398 switch (RVI->getOpcode()) {
1399 // Extend the analysis by looking upwards.
1400 case Instruction::BitCast:
1401 case Instruction::GetElementPtr:
1402 case Instruction::AddrSpaceCast:
1403 FlowsToReturn.insert(RVI->getOperand(0));
1404 continue;
1405 case Instruction::Select: {
1406 SelectInst *SI = cast<SelectInst>(RVI);
1407 FlowsToReturn.insert(SI->getTrueValue());
1408 FlowsToReturn.insert(SI->getFalseValue());
1409 continue;
1410 }
1411 case Instruction::PHI: {
1412 PHINode *PN = cast<PHINode>(RVI);
1413 for (Value *IncValue : PN->incoming_values())
1414 FlowsToReturn.insert(IncValue);
1415 continue;
1416 }
1417
1418 // Check whether the pointer came from an allocation.
1419 case Instruction::Alloca:
1420 break;
1421 case Instruction::Call:
1422 case Instruction::Invoke: {
1423 CallBase &CB = cast<CallBase>(*RVI);
1424 if (CB.hasRetAttr(Attribute::NoAlias))
1425 break;
1426 if (CB.getCalledFunction() && SCCNodes.count(CB.getCalledFunction()))
1427 break;
1428 [[fallthrough]];
1429 }
1430 default:
1431 return false; // Did not come from an allocation.
1432 }
1433
1434 if (PointerMayBeCaptured(RetVal, false, /*StoreCaptures=*/false))
1435 return false;
1436 }
1437
1438 return true;
1439}
1440
1441/// Deduce noalias attributes for the SCC.
1442static void addNoAliasAttrs(const SCCNodeSet &SCCNodes,
1443 SmallSet<Function *, 8> &Changed) {
1444 // Check each function in turn, determining which functions return noalias
1445 // pointers.
1446 for (Function *F : SCCNodes) {
1447 // Already noalias.
1448 if (F->returnDoesNotAlias())
1449 continue;
1450
1451 // We can infer and propagate function attributes only when we know that the
1452 // definition we'll get at link time is *exactly* the definition we see now.
1453 // For more details, see GlobalValue::mayBeDerefined.
1454 if (!F->hasExactDefinition())
1455 return;
1456
1457 // We annotate noalias return values, which are only applicable to
1458 // pointer types.
1459 if (!F->getReturnType()->isPointerTy())
1460 continue;
1461
1462 if (!isFunctionMallocLike(F, SCCNodes))
1463 return;
1464 }
1465
1466 for (Function *F : SCCNodes) {
1467 if (F->returnDoesNotAlias() ||
1468 !F->getReturnType()->isPointerTy())
1469 continue;
1470
1471 F->setReturnDoesNotAlias();
1472 ++NumNoAlias;
1473 Changed.insert(F);
1474 }
1475}
1476
1477/// Tests whether this function is known to not return null.
1478///
1479/// Requires that the function returns a pointer.
1480///
1481/// Returns true if it believes the function will not return a null, and sets
1482/// \p Speculative based on whether the returned conclusion is a speculative
1483/// conclusion due to SCC calls.
1484static bool isReturnNonNull(Function *F, const SCCNodeSet &SCCNodes,
1485 bool &Speculative) {
1486 assert(F->getReturnType()->isPointerTy() &&
1487 "nonnull only meaningful on pointer types");
1488 Speculative = false;
1489
1490 SmallSetVector<Value *, 8> FlowsToReturn;
1491 for (BasicBlock &BB : *F)
1492 if (auto *Ret = dyn_cast<ReturnInst>(BB.getTerminator()))
1493 FlowsToReturn.insert(Ret->getReturnValue());
1494
1495 auto &DL = F->getDataLayout();
1496
1497 for (unsigned i = 0; i != FlowsToReturn.size(); ++i) {
1498 Value *RetVal = FlowsToReturn[i];
1499
1500 // If this value is locally known to be non-null, we're good
1501 if (isKnownNonZero(RetVal, DL))
1502 continue;
1503
1504 // Otherwise, we need to look upwards since we can't make any local
1505 // conclusions.
1506 Instruction *RVI = dyn_cast<Instruction>(RetVal);
1507 if (!RVI)
1508 return false;
1509 switch (RVI->getOpcode()) {
1510 // Extend the analysis by looking upwards.
1511 case Instruction::BitCast:
1512 case Instruction::AddrSpaceCast:
1513 FlowsToReturn.insert(RVI->getOperand(0));
1514 continue;
1515 case Instruction::GetElementPtr:
1516 if (cast<GEPOperator>(RVI)->isInBounds()) {
1517 FlowsToReturn.insert(RVI->getOperand(0));
1518 continue;
1519 }
1520 return false;
1521 case Instruction::Select: {
1522 SelectInst *SI = cast<SelectInst>(RVI);
1523 FlowsToReturn.insert(SI->getTrueValue());
1524 FlowsToReturn.insert(SI->getFalseValue());
1525 continue;
1526 }
1527 case Instruction::PHI: {
1528 PHINode *PN = cast<PHINode>(RVI);
1529 for (int i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
1530 FlowsToReturn.insert(PN->getIncomingValue(i));
1531 continue;
1532 }
1533 case Instruction::Call:
1534 case Instruction::Invoke: {
1535 CallBase &CB = cast<CallBase>(*RVI);
1536 Function *Callee = CB.getCalledFunction();
1537 // A call to a node within the SCC is assumed to return null until
1538 // proven otherwise
1539 if (Callee && SCCNodes.count(Callee)) {
1540 Speculative = true;
1541 continue;
1542 }
1543 return false;
1544 }
1545 default:
1546 return false; // Unknown source, may be null
1547 };
1548 llvm_unreachable("should have either continued or returned");
1549 }
1550
1551 return true;
1552}
1553
1554/// Deduce nonnull attributes for the SCC.
1555static void addNonNullAttrs(const SCCNodeSet &SCCNodes,
1556 SmallSet<Function *, 8> &Changed) {
1557 // Speculative that all functions in the SCC return only nonnull
1558 // pointers. We may refute this as we analyze functions.
1559 bool SCCReturnsNonNull = true;
1560
1561 // Check each function in turn, determining which functions return nonnull
1562 // pointers.
1563 for (Function *F : SCCNodes) {
1564 // Already nonnull.
1565 if (F->getAttributes().hasRetAttr(Attribute::NonNull))
1566 continue;
1567
1568 // We can infer and propagate function attributes only when we know that the
1569 // definition we'll get at link time is *exactly* the definition we see now.
1570 // For more details, see GlobalValue::mayBeDerefined.
1571 if (!F->hasExactDefinition())
1572 return;
1573
1574 // We annotate nonnull return values, which are only applicable to
1575 // pointer types.
1576 if (!F->getReturnType()->isPointerTy())
1577 continue;
1578
1579 bool Speculative = false;
1580 if (isReturnNonNull(F, SCCNodes, Speculative)) {
1581 if (!Speculative) {
1582 // Mark the function eagerly since we may discover a function
1583 // which prevents us from speculating about the entire SCC
1584 LLVM_DEBUG(dbgs() << "Eagerly marking " << F->getName()
1585 << " as nonnull\n");
1586 F->addRetAttr(Attribute::NonNull);
1587 ++NumNonNullReturn;
1588 Changed.insert(F);
1589 }
1590 continue;
1591 }
1592 // At least one function returns something which could be null, can't
1593 // speculate any more.
1594 SCCReturnsNonNull = false;
1595 }
1596
1597 if (SCCReturnsNonNull) {
1598 for (Function *F : SCCNodes) {
1599 if (F->getAttributes().hasRetAttr(Attribute::NonNull) ||
1600 !F->getReturnType()->isPointerTy())
1601 continue;
1602
1603 LLVM_DEBUG(dbgs() << "SCC marking " << F->getName() << " as nonnull\n");
1604 F->addRetAttr(Attribute::NonNull);
1605 ++NumNonNullReturn;
1606 Changed.insert(F);
1607 }
1608 }
1609}
1610
1611/// Deduce noundef attributes for the SCC.
1612static void addNoUndefAttrs(const SCCNodeSet &SCCNodes,
1613 SmallSet<Function *, 8> &Changed) {
1614 // Check each function in turn, determining which functions return noundef
1615 // values.
1616 for (Function *F : SCCNodes) {
1617 // Already noundef.
1618 AttributeList Attrs = F->getAttributes();
1619 if (Attrs.hasRetAttr(Attribute::NoUndef))
1620 continue;
1621
1622 // We can infer and propagate function attributes only when we know that the
1623 // definition we'll get at link time is *exactly* the definition we see now.
1624 // For more details, see GlobalValue::mayBeDerefined.
1625 if (!F->hasExactDefinition())
1626 return;
1627
1628 // MemorySanitizer assumes that the definition and declaration of a
1629 // function will be consistent. A function with sanitize_memory attribute
1630 // should be skipped from inference.
1631 if (F->hasFnAttribute(Attribute::SanitizeMemory))
1632 continue;
1633
1634 if (F->getReturnType()->isVoidTy())
1635 continue;
1636
1637 const DataLayout &DL = F->getDataLayout();
1638 if (all_of(*F, [&](BasicBlock &BB) {
1639 if (auto *Ret = dyn_cast<ReturnInst>(BB.getTerminator())) {
1640 // TODO: perform context-sensitive analysis?
1641 Value *RetVal = Ret->getReturnValue();
1642 if (!isGuaranteedNotToBeUndefOrPoison(RetVal))
1643 return false;
1644
1645 // We know the original return value is not poison now, but it
1646 // could still be converted to poison by another return attribute.
1647 // Try to explicitly re-prove the relevant attributes.
1648 if (Attrs.hasRetAttr(Attribute::NonNull) &&
1649 !isKnownNonZero(RetVal, DL))
1650 return false;
1651
1652 if (MaybeAlign Align = Attrs.getRetAlignment())
1653 if (RetVal->getPointerAlignment(DL) < *Align)
1654 return false;
1655
1656 Attribute Attr = Attrs.getRetAttr(Attribute::Range);
1657 if (Attr.isValid() &&
1658 !Attr.getRange().contains(
1659 computeConstantRange(RetVal, /*ForSigned=*/false)))
1660 return false;
1661 }
1662 return true;
1663 })) {
1664 F->addRetAttr(Attribute::NoUndef);
1665 ++NumNoUndefReturn;
1666 Changed.insert(F);
1667 }
1668 }
1669}
1670
1671namespace {
1672
1673/// Collects a set of attribute inference requests and performs them all in one
1674/// go on a single SCC Node. Inference involves scanning function bodies
1675/// looking for instructions that violate attribute assumptions.
1676/// As soon as all the bodies are fine we are free to set the attribute.
1677/// Customization of inference for individual attributes is performed by
1678/// providing a handful of predicates for each attribute.
1679class AttributeInferer {
1680public:
1681 /// Describes a request for inference of a single attribute.
1682 struct InferenceDescriptor {
1683
1684 /// Returns true if this function does not have to be handled.
1685 /// General intent for this predicate is to provide an optimization
1686 /// for functions that do not need this attribute inference at all
1687 /// (say, for functions that already have the attribute).
1688 std::function<bool(const Function &)> SkipFunction;
1689
1690 /// Returns true if this instruction violates attribute assumptions.
1691 std::function<bool(Instruction &)> InstrBreaksAttribute;
1692
1693 /// Sets the inferred attribute for this function.
1694 std::function<void(Function &)> SetAttribute;
1695
1696 /// Attribute we derive.
1697 Attribute::AttrKind AKind;
1698
1699 /// If true, only "exact" definitions can be used to infer this attribute.
1700 /// See GlobalValue::isDefinitionExact.
1701 bool RequiresExactDefinition;
1702
1703 InferenceDescriptor(Attribute::AttrKind AK,
1704 std::function<bool(const Function &)> SkipFunc,
1705 std::function<bool(Instruction &)> InstrScan,
1706 std::function<void(Function &)> SetAttr,
1707 bool ReqExactDef)
1708 : SkipFunction(SkipFunc), InstrBreaksAttribute(InstrScan),
1709 SetAttribute(SetAttr), AKind(AK),
1710 RequiresExactDefinition(ReqExactDef) {}
1711 };
1712
1713private:
1714 SmallVector<InferenceDescriptor, 4> InferenceDescriptors;
1715
1716public:
1717 void registerAttrInference(InferenceDescriptor AttrInference) {
1718 InferenceDescriptors.push_back(AttrInference);
1719 }
1720
1721 void run(const SCCNodeSet &SCCNodes, SmallSet<Function *, 8> &Changed);
1722};
1723
1724/// Perform all the requested attribute inference actions according to the
1725/// attribute predicates stored before.
1726void AttributeInferer::run(const SCCNodeSet &SCCNodes,
1727 SmallSet<Function *, 8> &Changed) {
1728 SmallVector<InferenceDescriptor, 4> InferInSCC = InferenceDescriptors;
1729 // Go through all the functions in SCC and check corresponding attribute
1730 // assumptions for each of them. Attributes that are invalid for this SCC
1731 // will be removed from InferInSCC.
1732 for (Function *F : SCCNodes) {
1733
1734 // No attributes whose assumptions are still valid - done.
1735 if (InferInSCC.empty())
1736 return;
1737
1738 // Check if our attributes ever need scanning/can be scanned.
1739 llvm::erase_if(InferInSCC, [F](const InferenceDescriptor &ID) {
1740 if (ID.SkipFunction(*F))
1741 return false;
1742
1743 // Remove from further inference (invalidate) when visiting a function
1744 // that has no instructions to scan/has an unsuitable definition.
1745 return F->isDeclaration() ||
1746 (ID.RequiresExactDefinition && !F->hasExactDefinition());
1747 });
1748
1749 // For each attribute still in InferInSCC that doesn't explicitly skip F,
1750 // set up the F instructions scan to verify assumptions of the attribute.
1753 InferInSCC, std::back_inserter(InferInThisFunc),
1754 [F](const InferenceDescriptor &ID) { return !ID.SkipFunction(*F); });
1755
1756 if (InferInThisFunc.empty())
1757 continue;
1758
1759 // Start instruction scan.
1760 for (Instruction &I : instructions(*F)) {
1761 llvm::erase_if(InferInThisFunc, [&](const InferenceDescriptor &ID) {
1762 if (!ID.InstrBreaksAttribute(I))
1763 return false;
1764 // Remove attribute from further inference on any other functions
1765 // because attribute assumptions have just been violated.
1766 llvm::erase_if(InferInSCC, [&ID](const InferenceDescriptor &D) {
1767 return D.AKind == ID.AKind;
1768 });
1769 // Remove attribute from the rest of current instruction scan.
1770 return true;
1771 });
1772
1773 if (InferInThisFunc.empty())
1774 break;
1775 }
1776 }
1777
1778 if (InferInSCC.empty())
1779 return;
1780
1781 for (Function *F : SCCNodes)
1782 // At this point InferInSCC contains only functions that were either:
1783 // - explicitly skipped from scan/inference, or
1784 // - verified to have no instructions that break attribute assumptions.
1785 // Hence we just go and force the attribute for all non-skipped functions.
1786 for (auto &ID : InferInSCC) {
1787 if (ID.SkipFunction(*F))
1788 continue;
1789 Changed.insert(F);
1790 ID.SetAttribute(*F);
1791 }
1792}
1793
1794struct SCCNodesResult {
1795 SCCNodeSet SCCNodes;
1796 bool HasUnknownCall;
1797};
1798
1799} // end anonymous namespace
1800
1801/// Helper for non-Convergent inference predicate InstrBreaksAttribute.
1803 const SCCNodeSet &SCCNodes) {
1804 const CallBase *CB = dyn_cast<CallBase>(&I);
1805 // Breaks non-convergent assumption if CS is a convergent call to a function
1806 // not in the SCC.
1807 return CB && CB->isConvergent() &&
1808 !SCCNodes.contains(CB->getCalledFunction());
1809}
1810
1811/// Helper for NoUnwind inference predicate InstrBreaksAttribute.
1812static bool InstrBreaksNonThrowing(Instruction &I, const SCCNodeSet &SCCNodes) {
1813 if (!I.mayThrow(/* IncludePhaseOneUnwind */ true))
1814 return false;
1815 if (const auto *CI = dyn_cast<CallInst>(&I)) {
1816 if (Function *Callee = CI->getCalledFunction()) {
1817 // I is a may-throw call to a function inside our SCC. This doesn't
1818 // invalidate our current working assumption that the SCC is no-throw; we
1819 // just have to scan that other function.
1820 if (SCCNodes.contains(Callee))
1821 return false;
1822 }
1823 }
1824 return true;
1825}
1826
1827/// Helper for NoFree inference predicate InstrBreaksAttribute.
1828static bool InstrBreaksNoFree(Instruction &I, const SCCNodeSet &SCCNodes) {
1829 CallBase *CB = dyn_cast<CallBase>(&I);
1830 if (!CB)
1831 return false;
1832
1833 if (CB->hasFnAttr(Attribute::NoFree))
1834 return false;
1835
1836 // Speculatively assume in SCC.
1837 if (Function *Callee = CB->getCalledFunction())
1838 if (SCCNodes.contains(Callee))
1839 return false;
1840
1841 return true;
1842}
1843
1844// Return true if this is an atomic which has an ordering stronger than
1845// unordered. Note that this is different than the predicate we use in
1846// Attributor. Here we chose to be conservative and consider monotonic
1847// operations potentially synchronizing. We generally don't do much with
1848// monotonic operations, so this is simply risk reduction.
1850 if (!I->isAtomic())
1851 return false;
1852
1853 if (auto *FI = dyn_cast<FenceInst>(I))
1854 // All legal orderings for fence are stronger than monotonic.
1855 return FI->getSyncScopeID() != SyncScope::SingleThread;
1856 else if (isa<AtomicCmpXchgInst>(I) || isa<AtomicRMWInst>(I))
1857 return true;
1858 else if (auto *SI = dyn_cast<StoreInst>(I))
1859 return !SI->isUnordered();
1860 else if (auto *LI = dyn_cast<LoadInst>(I))
1861 return !LI->isUnordered();
1862 else {
1863 llvm_unreachable("unknown atomic instruction?");
1864 }
1865}
1866
1867static bool InstrBreaksNoSync(Instruction &I, const SCCNodeSet &SCCNodes) {
1868 // Volatile may synchronize
1869 if (I.isVolatile())
1870 return true;
1871
1872 // An ordered atomic may synchronize. (See comment about on monotonic.)
1873 if (isOrderedAtomic(&I))
1874 return true;
1875
1876 auto *CB = dyn_cast<CallBase>(&I);
1877 if (!CB)
1878 // Non call site cases covered by the two checks above
1879 return false;
1880
1881 if (CB->hasFnAttr(Attribute::NoSync))
1882 return false;
1883
1884 // Non volatile memset/memcpy/memmoves are nosync
1885 // NOTE: Only intrinsics with volatile flags should be handled here. All
1886 // others should be marked in Intrinsics.td.
1887 if (auto *MI = dyn_cast<MemIntrinsic>(&I))
1888 if (!MI->isVolatile())
1889 return false;
1890
1891 // Speculatively assume in SCC.
1892 if (Function *Callee = CB->getCalledFunction())
1893 if (SCCNodes.contains(Callee))
1894 return false;
1895
1896 return true;
1897}
1898
1899/// Attempt to remove convergent function attribute when possible.
1900///
1901/// Returns true if any changes to function attributes were made.
1902static void inferConvergent(const SCCNodeSet &SCCNodes,
1903 SmallSet<Function *, 8> &Changed) {
1904 AttributeInferer AI;
1905
1906 // Request to remove the convergent attribute from all functions in the SCC
1907 // if every callsite within the SCC is not convergent (except for calls
1908 // to functions within the SCC).
1909 // Note: Removal of the attr from the callsites will happen in
1910 // InstCombineCalls separately.
1911 AI.registerAttrInference(AttributeInferer::InferenceDescriptor{
1912 Attribute::Convergent,
1913 // Skip non-convergent functions.
1914 [](const Function &F) { return !F.isConvergent(); },
1915 // Instructions that break non-convergent assumption.
1916 [SCCNodes](Instruction &I) {
1917 return InstrBreaksNonConvergent(I, SCCNodes);
1918 },
1919 [](Function &F) {
1920 LLVM_DEBUG(dbgs() << "Removing convergent attr from fn " << F.getName()
1921 << "\n");
1922 F.setNotConvergent();
1923 },
1924 /* RequiresExactDefinition= */ false});
1925 // Perform all the requested attribute inference actions.
1926 AI.run(SCCNodes, Changed);
1927}
1928
1929/// Infer attributes from all functions in the SCC by scanning every
1930/// instruction for compliance to the attribute assumptions.
1931///
1932/// Returns true if any changes to function attributes were made.
1933static void inferAttrsFromFunctionBodies(const SCCNodeSet &SCCNodes,
1934 SmallSet<Function *, 8> &Changed) {
1935 AttributeInferer AI;
1936
1938 // Request to infer nounwind attribute for all the functions in the SCC if
1939 // every callsite within the SCC is not throwing (except for calls to
1940 // functions within the SCC). Note that nounwind attribute suffers from
1941 // derefinement - results may change depending on how functions are
1942 // optimized. Thus it can be inferred only from exact definitions.
1943 AI.registerAttrInference(AttributeInferer::InferenceDescriptor{
1944 Attribute::NoUnwind,
1945 // Skip non-throwing functions.
1946 [](const Function &F) { return F.doesNotThrow(); },
1947 // Instructions that break non-throwing assumption.
1948 [&SCCNodes](Instruction &I) {
1949 return InstrBreaksNonThrowing(I, SCCNodes);
1950 },
1951 [](Function &F) {
1953 << "Adding nounwind attr to fn " << F.getName() << "\n");
1954 F.setDoesNotThrow();
1955 ++NumNoUnwind;
1956 },
1957 /* RequiresExactDefinition= */ true});
1958
1960 // Request to infer nofree attribute for all the functions in the SCC if
1961 // every callsite within the SCC does not directly or indirectly free
1962 // memory (except for calls to functions within the SCC). Note that nofree
1963 // attribute suffers from derefinement - results may change depending on
1964 // how functions are optimized. Thus it can be inferred only from exact
1965 // definitions.
1966 AI.registerAttrInference(AttributeInferer::InferenceDescriptor{
1967 Attribute::NoFree,
1968 // Skip functions known not to free memory.
1969 [](const Function &F) { return F.doesNotFreeMemory(); },
1970 // Instructions that break non-deallocating assumption.
1971 [&SCCNodes](Instruction &I) {
1972 return InstrBreaksNoFree(I, SCCNodes);
1973 },
1974 [](Function &F) {
1976 << "Adding nofree attr to fn " << F.getName() << "\n");
1977 F.setDoesNotFreeMemory();
1978 ++NumNoFree;
1979 },
1980 /* RequiresExactDefinition= */ true});
1981
1982 AI.registerAttrInference(AttributeInferer::InferenceDescriptor{
1983 Attribute::NoSync,
1984 // Skip already marked functions.
1985 [](const Function &F) { return F.hasNoSync(); },
1986 // Instructions that break nosync assumption.
1987 [&SCCNodes](Instruction &I) {
1988 return InstrBreaksNoSync(I, SCCNodes);
1989 },
1990 [](Function &F) {
1992 << "Adding nosync attr to fn " << F.getName() << "\n");
1993 F.setNoSync();
1994 ++NumNoSync;
1995 },
1996 /* RequiresExactDefinition= */ true});
1997
1998 // Perform all the requested attribute inference actions.
1999 AI.run(SCCNodes, Changed);
2000}
2001
2002static void addNoRecurseAttrs(const SCCNodeSet &SCCNodes,
2003 SmallSet<Function *, 8> &Changed) {
2004 // Try and identify functions that do not recurse.
2005
2006 // If the SCC contains multiple nodes we know for sure there is recursion.
2007 if (SCCNodes.size() != 1)
2008 return;
2009
2010 Function *F = *SCCNodes.begin();
2011 if (!F || !F->hasExactDefinition() || F->doesNotRecurse())
2012 return;
2013
2014 // If all of the calls in F are identifiable and are to norecurse functions, F
2015 // is norecurse. This check also detects self-recursion as F is not currently
2016 // marked norecurse, so any called from F to F will not be marked norecurse.
2017 for (auto &BB : *F)
2018 for (auto &I : BB.instructionsWithoutDebug())
2019 if (auto *CB = dyn_cast<CallBase>(&I)) {
2020 Function *Callee = CB->getCalledFunction();
2021 if (!Callee || Callee == F ||
2022 (!Callee->doesNotRecurse() &&
2023 !(Callee->isDeclaration() &&
2024 Callee->hasFnAttribute(Attribute::NoCallback))))
2025 // Function calls a potentially recursive function.
2026 return;
2027 }
2028
2029 // Every call was to a non-recursive function other than this function, and
2030 // we have no indirect recursion as the SCC size is one. This function cannot
2031 // recurse.
2032 F->setDoesNotRecurse();
2033 ++NumNoRecurse;
2034 Changed.insert(F);
2035}
2036
2038 if (auto *CB = dyn_cast<CallBase>(&I))
2039 return CB->hasFnAttr(Attribute::NoReturn);
2040 return false;
2041}
2042
2043// A basic block can only return if it terminates with a ReturnInst and does not
2044// contain calls to noreturn functions.
2046 if (!isa<ReturnInst>(BB.getTerminator()))
2047 return false;
2049}
2050
2051// FIXME: this doesn't handle recursion.
2052static bool canReturn(Function &F) {
2055
2056 Visited.insert(&F.front());
2057 Worklist.push_back(&F.front());
2058
2059 do {
2060 BasicBlock *BB = Worklist.pop_back_val();
2061 if (basicBlockCanReturn(*BB))
2062 return true;
2063 for (BasicBlock *Succ : successors(BB))
2064 if (Visited.insert(Succ).second)
2065 Worklist.push_back(Succ);
2066 } while (!Worklist.empty());
2067
2068 return false;
2069}
2070
2071
2072// Set the noreturn function attribute if possible.
2073static void addNoReturnAttrs(const SCCNodeSet &SCCNodes,
2074 SmallSet<Function *, 8> &Changed) {
2075 for (Function *F : SCCNodes) {
2076 if (!F || !F->hasExactDefinition() || F->hasFnAttribute(Attribute::Naked) ||
2077 F->doesNotReturn())
2078 continue;
2079
2080 if (!canReturn(*F)) {
2081 F->setDoesNotReturn();
2082 Changed.insert(F);
2083 }
2084 }
2085}
2086
2089 ColdPaths[&F.front()] = false;
2091 Jobs.push_back(&F.front());
2092
2093 while (!Jobs.empty()) {
2094 BasicBlock *BB = Jobs.pop_back_val();
2095
2096 // If block contains a cold callsite this path through the CG is cold.
2097 // Ignore whether the instructions actually are guaranteed to transfer
2098 // execution. Divergent behavior is considered unlikely.
2099 if (any_of(*BB, [](Instruction &I) {
2100 if (auto *CB = dyn_cast<CallBase>(&I))
2101 return CB->hasFnAttr(Attribute::Cold);
2102 return false;
2103 })) {
2104 ColdPaths[BB] = true;
2105 continue;
2106 }
2107
2108 auto Succs = successors(BB);
2109 // We found a path that doesn't go through any cold callsite.
2110 if (Succs.empty())
2111 return false;
2112
2113 // We didn't find a cold callsite in this BB, so check that all successors
2114 // contain a cold callsite (or that their successors do).
2115 // Potential TODO: We could use static branch hints to assume certain
2116 // successor paths are inherently cold, irrespective of if they contain a
2117 // cold callsite.
2118 for (BasicBlock *Succ : Succs) {
2119 // Start with false, this is necessary to ensure we don't turn loops into
2120 // cold.
2121 auto [Iter, Inserted] = ColdPaths.try_emplace(Succ, false);
2122 if (!Inserted) {
2123 if (Iter->second)
2124 continue;
2125 return false;
2126 }
2127 Jobs.push_back(Succ);
2128 }
2129 }
2130 return true;
2131}
2132
2133// Set the cold function attribute if possible.
2134static void addColdAttrs(const SCCNodeSet &SCCNodes,
2135 SmallSet<Function *, 8> &Changed) {
2136 for (Function *F : SCCNodes) {
2137 if (!F || !F->hasExactDefinition() || F->hasFnAttribute(Attribute::Naked) ||
2138 F->hasFnAttribute(Attribute::Cold) || F->hasFnAttribute(Attribute::Hot))
2139 continue;
2140
2141 // Potential TODO: We could add attribute `cold` on functions with `coldcc`.
2142 if (allPathsGoThroughCold(*F)) {
2143 F->addFnAttr(Attribute::Cold);
2144 ++NumCold;
2145 Changed.insert(F);
2146 continue;
2147 }
2148 }
2149}
2150
2151static bool functionWillReturn(const Function &F) {
2152 // We can infer and propagate function attributes only when we know that the
2153 // definition we'll get at link time is *exactly* the definition we see now.
2154 // For more details, see GlobalValue::mayBeDerefined.
2155 if (!F.hasExactDefinition())
2156 return false;
2157
2158 // Must-progress function without side-effects must return.
2159 if (F.mustProgress() && F.onlyReadsMemory())
2160 return true;
2161
2162 // Can only analyze functions with a definition.
2163 if (F.isDeclaration())
2164 return false;
2165
2166 // Functions with loops require more sophisticated analysis, as the loop
2167 // may be infinite. For now, don't try to handle them.
2169 FindFunctionBackedges(F, Backedges);
2170 if (!Backedges.empty())
2171 return false;
2172
2173 // If there are no loops, then the function is willreturn if all calls in
2174 // it are willreturn.
2175 return all_of(instructions(F), [](const Instruction &I) {
2176 return I.willReturn();
2177 });
2178}
2179
2180// Set the willreturn function attribute if possible.
2181static void addWillReturn(const SCCNodeSet &SCCNodes,
2182 SmallSet<Function *, 8> &Changed) {
2183 for (Function *F : SCCNodes) {
2184 if (!F || F->willReturn() || !functionWillReturn(*F))
2185 continue;
2186
2187 F->setWillReturn();
2188 NumWillReturn++;
2189 Changed.insert(F);
2190 }
2191}
2192
2193static SCCNodesResult createSCCNodeSet(ArrayRef<Function *> Functions) {
2194 SCCNodesResult Res;
2195 Res.HasUnknownCall = false;
2196 for (Function *F : Functions) {
2197 if (!F || F->hasOptNone() || F->hasFnAttribute(Attribute::Naked) ||
2198 F->isPresplitCoroutine()) {
2199 // Treat any function we're trying not to optimize as if it were an
2200 // indirect call and omit it from the node set used below.
2201 Res.HasUnknownCall = true;
2202 continue;
2203 }
2204 // Track whether any functions in this SCC have an unknown call edge.
2205 // Note: if this is ever a performance hit, we can common it with
2206 // subsequent routines which also do scans over the instructions of the
2207 // function.
2208 if (!Res.HasUnknownCall) {
2209 for (Instruction &I : instructions(*F)) {
2210 if (auto *CB = dyn_cast<CallBase>(&I)) {
2211 if (!CB->getCalledFunction()) {
2212 Res.HasUnknownCall = true;
2213 break;
2214 }
2215 }
2216 }
2217 }
2218 Res.SCCNodes.insert(F);
2219 }
2220 return Res;
2221}
2222
2223template <typename AARGetterT>
2225deriveAttrsInPostOrder(ArrayRef<Function *> Functions, AARGetterT &&AARGetter,
2226 bool ArgAttrsOnly) {
2227 SCCNodesResult Nodes = createSCCNodeSet(Functions);
2228
2229 // Bail if the SCC only contains optnone functions.
2230 if (Nodes.SCCNodes.empty())
2231 return {};
2232
2234 if (ArgAttrsOnly) {
2235 // ArgAttrsOnly means to only infer attributes that may aid optimizations
2236 // on the *current* function. "initializes" attribute is to aid
2237 // optimizations (like DSE) on the callers, so skip "initializes" here.
2238 addArgumentAttrs(Nodes.SCCNodes, Changed, /*SkipInitializes=*/true);
2239 return Changed;
2240 }
2241
2242 addArgumentReturnedAttrs(Nodes.SCCNodes, Changed);
2243 addMemoryAttrs(Nodes.SCCNodes, AARGetter, Changed);
2244 addArgumentAttrs(Nodes.SCCNodes, Changed, /*SkipInitializes=*/false);
2245 inferConvergent(Nodes.SCCNodes, Changed);
2246 addNoReturnAttrs(Nodes.SCCNodes, Changed);
2247 addColdAttrs(Nodes.SCCNodes, Changed);
2248 addWillReturn(Nodes.SCCNodes, Changed);
2249 addNoUndefAttrs(Nodes.SCCNodes, Changed);
2250
2251 // If we have no external nodes participating in the SCC, we can deduce some
2252 // more precise attributes as well.
2253 if (!Nodes.HasUnknownCall) {
2254 addNoAliasAttrs(Nodes.SCCNodes, Changed);
2255 addNonNullAttrs(Nodes.SCCNodes, Changed);
2256 inferAttrsFromFunctionBodies(Nodes.SCCNodes, Changed);
2257 addNoRecurseAttrs(Nodes.SCCNodes, Changed);
2258 }
2259
2260 // Finally, infer the maximal set of attributes from the ones we've inferred
2261 // above. This is handling the cases where one attribute on a signature
2262 // implies another, but for implementation reasons the inference rule for
2263 // the later is missing (or simply less sophisticated).
2264 for (Function *F : Nodes.SCCNodes)
2265 if (F)
2267 Changed.insert(F);
2268
2269 return Changed;
2270}
2271
2274 LazyCallGraph &CG,
2276 // Skip non-recursive functions if requested.
2277 // Only infer argument attributes for non-recursive functions, because
2278 // it can affect optimization behavior in conjunction with noalias.
2279 bool ArgAttrsOnly = false;
2280 if (C.size() == 1 && SkipNonRecursive) {
2281 LazyCallGraph::Node &N = *C.begin();
2282 if (!N->lookup(N))
2283 ArgAttrsOnly = true;
2284 }
2285
2287 AM.getResult<FunctionAnalysisManagerCGSCCProxy>(C, CG).getManager();
2288
2289 // We pass a lambda into functions to wire them up to the analysis manager
2290 // for getting function analyses.
2291 auto AARGetter = [&](Function &F) -> AAResults & {
2292 return FAM.getResult<AAManager>(F);
2293 };
2294
2296 for (LazyCallGraph::Node &N : C) {
2297 Functions.push_back(&N.getFunction());
2298 }
2299
2300 auto ChangedFunctions =
2301 deriveAttrsInPostOrder(Functions, AARGetter, ArgAttrsOnly);
2302 if (ChangedFunctions.empty())
2303 return PreservedAnalyses::all();
2304
2305 // Invalidate analyses for modified functions so that we don't have to
2306 // invalidate all analyses for all functions in this SCC.
2307 PreservedAnalyses FuncPA;
2308 // We haven't changed the CFG for modified functions.
2309 FuncPA.preserveSet<CFGAnalyses>();
2310 for (Function *Changed : ChangedFunctions) {
2311 FAM.invalidate(*Changed, FuncPA);
2312 // Also invalidate any direct callers of changed functions since analyses
2313 // may care about attributes of direct callees. For example, MemorySSA cares
2314 // about whether or not a call's callee modifies memory and queries that
2315 // through function attributes.
2316 for (auto *U : Changed->users()) {
2317 if (auto *Call = dyn_cast<CallBase>(U)) {
2318 if (Call->getCalledFunction() == Changed)
2319 FAM.invalidate(*Call->getFunction(), FuncPA);
2320 }
2321 }
2322 }
2323
2325 // We have not added or removed functions.
2327 // We already invalidated all relevant function analyses above.
2329 return PA;
2330}
2331
2333 raw_ostream &OS, function_ref<StringRef(StringRef)> MapClassName2PassName) {
2335 OS, MapClassName2PassName);
2336 if (SkipNonRecursive)
2337 OS << "<skip-non-recursive-function-attrs>";
2338}
2339
2340template <typename AARGetterT>
2341static bool runImpl(CallGraphSCC &SCC, AARGetterT AARGetter) {
2343 for (CallGraphNode *I : SCC) {
2344 Functions.push_back(I->getFunction());
2345 }
2346
2347 return !deriveAttrsInPostOrder(Functions, AARGetter).empty();
2348}
2349
2351 // We check the preconditions for the function prior to calling this to avoid
2352 // the cost of building up a reversible post-order list. We assert them here
2353 // to make sure none of the invariants this relies on were violated.
2354 assert(!F.isDeclaration() && "Cannot deduce norecurse without a definition!");
2355 assert(!F.doesNotRecurse() &&
2356 "This function has already been deduced as norecurs!");
2357 assert(F.hasInternalLinkage() &&
2358 "Can only do top-down deduction for internal linkage functions!");
2359
2360 // If F is internal and all of its uses are calls from a non-recursive
2361 // functions, then none of its calls could in fact recurse without going
2362 // through a function marked norecurse, and so we can mark this function too
2363 // as norecurse. Note that the uses must actually be calls -- otherwise
2364 // a pointer to this function could be returned from a norecurse function but
2365 // this function could be recursively (indirectly) called. Note that this
2366 // also detects if F is directly recursive as F is not yet marked as
2367 // a norecurse function.
2368 for (auto &U : F.uses()) {
2369 auto *I = dyn_cast<Instruction>(U.getUser());
2370 if (!I)
2371 return false;
2372 CallBase *CB = dyn_cast<CallBase>(I);
2373 if (!CB || !CB->isCallee(&U) ||
2374 !CB->getParent()->getParent()->doesNotRecurse())
2375 return false;
2376 }
2377 F.setDoesNotRecurse();
2378 ++NumNoRecurse;
2379 return true;
2380}
2381
2383 // We only have a post-order SCC traversal (because SCCs are inherently
2384 // discovered in post-order), so we accumulate them in a vector and then walk
2385 // it in reverse. This is simpler than using the RPO iterator infrastructure
2386 // because we need to combine SCC detection and the PO walk of the call
2387 // graph. We can also cheat egregiously because we're primarily interested in
2388 // synthesizing norecurse and so we can only save the singular SCCs as SCCs
2389 // with multiple functions in them will clearly be recursive.
2390
2392 CG.buildRefSCCs();
2393 for (LazyCallGraph::RefSCC &RC : CG.postorder_ref_sccs()) {
2394 for (LazyCallGraph::SCC &SCC : RC) {
2395 if (SCC.size() != 1)
2396 continue;
2397 Function &F = SCC.begin()->getFunction();
2398 if (!F.isDeclaration() && !F.doesNotRecurse() && F.hasInternalLinkage())
2399 Worklist.push_back(&F);
2400 }
2401 }
2402 bool Changed = false;
2403 for (auto *F : llvm::reverse(Worklist))
2404 Changed |= addNoRecurseAttrsTopDown(*F);
2405
2406 return Changed;
2407}
2408
2411 auto &CG = AM.getResult<LazyCallGraphAnalysis>(M);
2412
2413 if (!deduceFunctionAttributeInRPO(M, CG))
2414 return PreservedAnalyses::all();
2415
2418 return PA;
2419}
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Expand Atomic instructions
This file contains the simple types necessary to represent the attributes associated with functions a...
This is the interface for LLVM's primary stateless and local alias analysis.
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
This header provides classes for managing passes over SCCs of the call graph.
Analysis containing CSE Info
Definition: CSEInfo.cpp:27
This file provides interfaces used to build and manipulate a call graph, which is a very useful tool ...
This file contains the declarations for the subclasses of Constant, which represent the different fla...
DXIL Resource Access
#define LLVM_DEBUG(...)
Definition: Debug.h:106
This file defines the DenseMap class.
uint64_t Size
static bool runImpl(Function &F, const TargetLowering &TLI)
static void addNoReturnAttrs(const SCCNodeSet &SCCNodes, SmallSet< Function *, 8 > &Changed)
static Attribute::AttrKind determinePointerAccessAttrs(Argument *A, const SmallPtrSet< Argument *, 8 > &SCCNodes)
Returns Attribute::None, Attribute::ReadOnly or Attribute::ReadNone.
static cl::opt< bool > DisableNoFreeInference("disable-nofree-inference", cl::Hidden, cl::desc("Stop inferring nofree attribute during function-attrs pass"))
static bool inferInitializes(Argument &A, Function &F)
static bool allPathsGoThroughCold(Function &F)
static bool addAccessAttr(Argument *A, Attribute::AttrKind R)
static FunctionSummary * calculatePrevailingSummary(ValueInfo VI, DenseMap< ValueInfo, FunctionSummary * > &CachedPrevailingSummary, function_ref< bool(GlobalValue::GUID, const GlobalValueSummary *)> IsPrevailing)
static bool addArgumentAttrsFromCallsites(Function &F)
If a callsite has arguments that are also arguments to the parent function, try to propagate attribut...
static void addNoUndefAttrs(const SCCNodeSet &SCCNodes, SmallSet< Function *, 8 > &Changed)
Deduce noundef attributes for the SCC.
static bool isOrderedAtomic(Instruction *I)
static void addArgLocs(MemoryEffects &ME, const CallBase *Call, ModRefInfo ArgMR, AAResults &AAR)
static bool isFunctionMallocLike(Function *F, const SCCNodeSet &SCCNodes)
Tests whether a function is "malloc-like".
static bool canReturn(Function &F)
static void addArgumentAttrs(const SCCNodeSet &SCCNodes, SmallSet< Function *, 8 > &Changed, bool SkipInitializes)
Deduce nocapture attributes for the SCC.
static cl::opt< bool > DisableNoUnwindInference("disable-nounwind-inference", cl::Hidden, cl::desc("Stop inferring nounwind attribute during function-attrs pass"))
static void inferAttrsFromFunctionBodies(const SCCNodeSet &SCCNodes, SmallSet< Function *, 8 > &Changed)
Infer attributes from all functions in the SCC by scanning every instruction for compliance to the at...
static std::pair< MemoryEffects, MemoryEffects > checkFunctionMemoryAccess(Function &F, bool ThisBody, AAResults &AAR, const SCCNodeSet &SCCNodes)
Returns the memory access attribute for function F using AAR for AA results, where SCCNodes is the cu...
static bool InstrBreaksNonThrowing(Instruction &I, const SCCNodeSet &SCCNodes)
Helper for NoUnwind inference predicate InstrBreaksAttribute.
static bool basicBlockCanReturn(BasicBlock &BB)
static void addColdAttrs(const SCCNodeSet &SCCNodes, SmallSet< Function *, 8 > &Changed)
static bool isReturnNonNull(Function *F, const SCCNodeSet &SCCNodes, bool &Speculative)
Tests whether this function is known to not return null.
static cl::opt< bool > EnableNonnullArgPropagation("enable-nonnull-arg-prop", cl::init(true), cl::Hidden, cl::desc("Try to propagate nonnull argument attributes from callsites to " "caller functions."))
static void addMemoryAttrs(const SCCNodeSet &SCCNodes, AARGetterT &&AARGetter, SmallSet< Function *, 8 > &Changed)
Deduce readonly/readnone/writeonly attributes for the SCC.
static void addNoRecurseAttrs(const SCCNodeSet &SCCNodes, SmallSet< Function *, 8 > &Changed)
static void inferConvergent(const SCCNodeSet &SCCNodes, SmallSet< Function *, 8 > &Changed)
Attempt to remove convergent function attribute when possible.
static bool InstrBreaksNoSync(Instruction &I, const SCCNodeSet &SCCNodes)
static bool deduceFunctionAttributeInRPO(Module &M, LazyCallGraph &CG)
static SmallSet< Function *, 8 > deriveAttrsInPostOrder(ArrayRef< Function * > Functions, AARGetterT &&AARGetter, bool ArgAttrsOnly)
static bool InstrBreaksNoFree(Instruction &I, const SCCNodeSet &SCCNodes)
Helper for NoFree inference predicate InstrBreaksAttribute.
static void addNoAliasAttrs(const SCCNodeSet &SCCNodes, SmallSet< Function *, 8 > &Changed)
Deduce noalias attributes for the SCC.
static bool addNoRecurseAttrsTopDown(Function &F)
static bool instructionDoesNotReturn(Instruction &I)
static void addWillReturn(const SCCNodeSet &SCCNodes, SmallSet< Function *, 8 > &Changed)
static void addLocAccess(MemoryEffects &ME, const MemoryLocation &Loc, ModRefInfo MR, AAResults &AAR)
static cl::opt< bool > DisableThinLTOPropagation("disable-thinlto-funcattrs", cl::init(true), cl::Hidden, cl::desc("Don't propagate function-attrs in thinLTO"))
static SCCNodesResult createSCCNodeSet(ArrayRef< Function * > Functions)
static bool InstrBreaksNonConvergent(Instruction &I, const SCCNodeSet &SCCNodes)
Helper for non-Convergent inference predicate InstrBreaksAttribute.
static void addArgumentReturnedAttrs(const SCCNodeSet &SCCNodes, SmallSet< Function *, 8 > &Changed)
Deduce returned attributes for the SCC.
static bool functionWillReturn(const Function &F)
static void addNonNullAttrs(const SCCNodeSet &SCCNodes, SmallSet< Function *, 8 > &Changed)
Deduce nonnull attributes for the SCC.
Provides passes for computing function attributes based on interprocedural analyses.
Hexagon Common GEP
#define _
IRTranslator LLVM IR MI
This header defines various interfaces for pass management in LLVM.
This defines the Use class.
Implements a lazy call graph analysis and related passes for the new pass manager.
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
This file provides utility analysis objects describing memory locations.
This file contains the declarations for metadata subclasses.
ModuleSummaryIndex.h This file contains the declarations the classes that hold the module index and s...
FunctionAnalysisManager FAM
This file builds on the ADT/GraphTraits.h file to build a generic graph post order iterator.
Remove Loads Into Fake Uses
This builds on the llvm/ADT/GraphTraits.h file to find the strongly connected components (SCCs) of a ...
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file contains some templates that are useful if you are working with the STL at all.
raw_pwrite_stream & OS
This file implements a set that has insertion order iteration characteristics.
This file defines the SmallPtrSet class.
This file defines the SmallSet class.
This file defines the SmallVector class.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Definition: Statistic.h:166
Value * RHS
Value * LHS
A manager for alias analyses.
ModRefInfo getModRefInfoMask(const MemoryLocation &Loc, bool IgnoreLocals=false)
Returns a bitmask that should be unconditionally applied to the ModRef info of a memory location.
MemoryEffects getMemoryEffects(const CallBase *Call)
Return the behavior of the given call site.
Class for arbitrary precision integers.
Definition: APInt.h:78
This templated class represents "all analyses that operate over <a particular IR unit>" (e....
Definition: Analysis.h:49
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:253
void invalidate(IRUnitT &IR, const PreservedAnalyses &PA)
Invalidate cached analyses for an IR unit.
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Definition: PassManager.h:410
This class represents an incoming formal argument to a Function.
Definition: Argument.h:31
void addAttr(Attribute::AttrKind Kind)
Definition: Function.cpp:331
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
static Attribute get(LLVMContext &Context, AttrKind Kind, uint64_t Val=0)
Return a uniquified Attribute object.
Definition: Attributes.cpp:95
ArrayRef< ConstantRange > getValueAsConstantRangeList() const
Return the attribute's value as a ConstantRange array.
Definition: Attributes.cpp:412
AttrKind
This enumeration lists the attributes that can be associated with parameters, function results,...
Definition: Attributes.h:86
@ None
No attributes have been set.
Definition: Attributes.h:88
LLVM Basic Block Representation.
Definition: BasicBlock.h:61
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
Definition: BasicBlock.h:239
Represents analyses that only rely on functions' control flow.
Definition: Analysis.h:72
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Definition: InstrTypes.h:1112
MemoryEffects getMemoryEffects() const
bool doesNotCapture(unsigned OpNo) const
Determine whether this data operand is not captured.
Definition: InstrTypes.h:1669
Function * getCalledFunction() const
Returns the function called, or null if this is an indirect function invocation or the function signa...
Definition: InstrTypes.h:1341
bool doesNotAccessMemory(unsigned OpNo) const
Definition: InstrTypes.h:1715
bool hasFnAttr(Attribute::AttrKind Kind) const
Determine whether this call has the given attribute.
Definition: InstrTypes.h:1451
bool hasRetAttr(Attribute::AttrKind Kind) const
Determine whether the return value has the given attribute.
Definition: InstrTypes.h:1573
unsigned getDataOperandNo(Value::const_user_iterator UI) const
Given a value use iterator, return the data operand corresponding to it.
Definition: InstrTypes.h:1249
bool paramHasAttr(unsigned ArgNo, Attribute::AttrKind Kind) const
Determine whether the argument or parameter has the given attribute.
Attribute getParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) const
Get the attribute of a given kind from a given arg.
Definition: InstrTypes.h:1621
bool dataOperandHasImpliedAttr(unsigned i, Attribute::AttrKind Kind) const
Return true if the data operand at index i has the attribute A.
Definition: InstrTypes.h:1649
bool isByValArgument(unsigned ArgNo) const
Determine whether this argument is passed by value.
Definition: InstrTypes.h:1679
bool onlyWritesMemory(unsigned OpNo) const
Definition: InstrTypes.h:1733
bool isCallee(Value::const_user_iterator UI) const
Determine whether the passed iterator points to the callee operand's Use.
Definition: InstrTypes.h:1352
bool onlyReadsMemory(unsigned OpNo) const
Definition: InstrTypes.h:1721
Value * getArgOperand(unsigned i) const
Definition: InstrTypes.h:1286
bool isConvergent() const
Determine if the invoke is convergent.
Definition: InstrTypes.h:1937
unsigned getArgOperandNo(const Use *U) const
Given a use for a arg operand, get the arg operand number that corresponds to it.
Definition: InstrTypes.h:1317
unsigned arg_size() const
Definition: InstrTypes.h:1284
bool isArgOperand(const Use *U) const
Definition: InstrTypes.h:1306
bool hasOperandBundles() const
Return true if this User has any operand bundles.
Definition: InstrTypes.h:1971
A node in the call graph for a module.
Definition: CallGraph.h:165
CallGraphSCC - This is a single SCC that a CallGraphSCCPass is run on.
This class represents a list of constant ranges.
void subtract(const ConstantRange &SubRange)
void insert(const ConstantRange &NewRange)
Insert a new range to Ranges and keep the list ordered.
bool empty() const
Return true if this list contains no members.
ArrayRef< ConstantRange > rangesRef() const
ConstantRangeList intersectWith(const ConstantRangeList &CRL) const
Return the range list that results from the intersection of this ConstantRangeList with another Const...
ConstantRangeList unionWith(const ConstantRangeList &CRL) const
Return the range list that results from the union of this ConstantRangeList with another ConstantRang...
This class represents a range of values.
Definition: ConstantRange.h:47
This is an important base class in LLVM.
Definition: Constant.h:42
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:63
iterator find(const_arg_type_t< KeyT > Val)
Definition: DenseMap.h:156
std::pair< iterator, bool > try_emplace(KeyT &&Key, Ts &&...Args)
Definition: DenseMap.h:226
size_type count(const_arg_type_t< KeyT > Val) const
Return 1 if the specified key is in the map, 0 otherwise.
Definition: DenseMap.h:152
iterator end()
Definition: DenseMap.h:84
A proxy from a FunctionAnalysisManager to an SCC.
Function summary information to aid decisions and implementation of importing.
ArrayRef< EdgeTy > calls() const
Return the list of <CalleeValueInfo, CalleeInfo> pairs.
FFlags fflags() const
Get function summary flags.
Function and variable summary information to aid decisions and implementation of importing.
static bool isWeakAnyLinkage(LinkageTypes Linkage)
Definition: GlobalValue.h:392
static bool isLinkOnceAnyLinkage(LinkageTypes Linkage)
Definition: GlobalValue.h:383
static bool isLocalLinkage(LinkageTypes Linkage)
Definition: GlobalValue.h:410
static bool isWeakODRLinkage(LinkageTypes Linkage)
Definition: GlobalValue.h:395
static bool isAvailableExternallyLinkage(LinkageTypes Linkage)
Definition: GlobalValue.h:380
static bool isExternalLinkage(LinkageTypes Linkage)
Definition: GlobalValue.h:377
static bool isLinkOnceODRLinkage(LinkageTypes Linkage)
Definition: GlobalValue.h:386
unsigned getOpcode() const
Returns a member of one of the enums like Instruction::Add.
Definition: Instruction.h:274
An analysis pass which computes the call graph for a module.
A node in the call graph.
A RefSCC of the call graph.
An SCC of the call graph.
A lazily constructed view of the call graph of a module.
iterator_range< postorder_ref_scc_iterator > postorder_ref_sccs()
MemoryEffectsBase getWithoutLoc(Location Loc) const
Get new MemoryEffectsBase with NoModRef on the given Loc.
Definition: ModRef.h:177
bool doesNotAccessMemory() const
Whether this function accesses no memory.
Definition: ModRef.h:192
static MemoryEffectsBase argMemOnly(ModRefInfo MR=ModRefInfo::ModRef)
Create MemoryEffectsBase that can only access argument memory.
Definition: ModRef.h:132
static MemoryEffectsBase inaccessibleMemOnly(ModRefInfo MR=ModRefInfo::ModRef)
Create MemoryEffectsBase that can only access inaccessible memory.
Definition: ModRef.h:138
ModRefInfo getModRef(Location Loc) const
Get ModRefInfo for the given Location.
Definition: ModRef.h:165
static MemoryEffectsBase none()
Create MemoryEffectsBase that cannot read or write any memory.
Definition: ModRef.h:117
static MemoryEffectsBase unknown()
Create MemoryEffectsBase that can read and write any memory.
Definition: ModRef.h:112
Representation for a specific memory location.
static MemoryLocation getBeforeOrAfter(const Value *Ptr, const AAMDNodes &AATags=AAMDNodes())
Return a location that may access any location before or after Ptr, while remaining within the underl...
const Value * Ptr
The address of the start of the location.
static std::optional< MemoryLocation > getOrNone(const Instruction *Inst)
Class to hold module path string table and global value map, and encapsulate methods for operating on...
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
op_range incoming_values()
Value * getIncomingValue(unsigned i) const
Return incoming value number x.
unsigned getNumIncomingValues() const
Return the number of incoming edges.
A set of analyses that are preserved following a run of a transformation pass.
Definition: Analysis.h:111
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: Analysis.h:117
void preserveSet()
Mark an analysis set as preserved.
Definition: Analysis.h:146
void preserve()
Mark an analysis as preserved.
Definition: Analysis.h:131
Return a value (possibly void), from a function.
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
This class represents the LLVM 'select' instruction.
size_type size() const
Determine the number of elements in the SetVector.
Definition: SetVector.h:98
bool insert(const value_type &X)
Insert a new element into the SetVector.
Definition: SetVector.h:162
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
Definition: SmallPtrSet.h:452
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Definition: SmallPtrSet.h:384
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Definition: SmallPtrSet.h:519
A SetVector that performs no allocations if smaller than a certain size.
Definition: SetVector.h:370
SmallSet - This maintains a set of unique values, optimizing for the case when the set is small (less...
Definition: SmallSet.h:132
std::pair< const_iterator, bool > insert(const T &V)
insert - Insert an element into the set if it isn't already there.
Definition: SmallSet.h:181
bool empty() const
Definition: SmallVector.h:81
typename SuperClass::iterator iterator
Definition: SmallVector.h:577
void push_back(const T &Elt)
Definition: SmallVector.h:413
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1196
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
A Use represents the edge between a Value definition and its users.
Definition: Use.h:43
Value * getOperand(unsigned i) const
Definition: User.h:228
LLVM Value Representation.
Definition: Value.h:74
iterator_range< use_iterator > uses()
Definition: Value.h:376
constexpr ScalarTy getFixedValue() const
Definition: TypeSize.h:202
constexpr bool isScalable() const
Returns whether the quantity is scaled by a runtime quantity (vscale).
Definition: TypeSize.h:171
An efficient, type-erasing, non-owning reference to a callable.
const ParentTy * getParent() const
Definition: ilist_node.h:32
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
Enumerate the SCCs of a directed graph in reverse topological order of the SCC DAG.
Definition: SCCIterator.h:49
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
@ SingleThread
Synchronized with respect to signal handlers executing in the same thread.
Definition: LLVMContext.h:54
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:443
PointerTypeMap run(const Module &M)
Compute the PointerTypeMap for the module M.
const_iterator begin(StringRef path LLVM_LIFETIME_BOUND, Style style=Style::native)
Get begin iterator over path.
Definition: Path.cpp:226
const_iterator end(StringRef path LLVM_LIFETIME_BOUND)
Get end iterator over path.
Definition: Path.cpp:235
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:480
@ Length
Definition: DWP.cpp:480
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1739
MemoryEffects computeFunctionBodyMemoryAccess(Function &F, AAResults &AAR)
Returns the memory access properties of this copy of the function.
@ Read
Definition: CodeGenData.h:107
@ Write
Definition: CodeGenData.h:108
auto successors(const MachineBasicBlock *BB)
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition: STLExtras.h:2115
scc_iterator< T > scc_begin(const T &G)
Construct the begin iterator for a deduced graph type T.
Definition: SCCIterator.h:233
iterator_range< po_iterator< T > > post_order(const T &G)
bool thinLTOPropagateFunctionAttrs(ModuleSummaryIndex &Index, function_ref< bool(GlobalValue::GUID, const GlobalValueSummary *)> isPrevailing)
Propagate function attributes for function summaries along the index's callgraph during thinlink.
OutputIt copy_if(R &&Range, OutputIt Out, UnaryPredicate P)
Provide wrappers to std::copy_if which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1785
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1746
auto reverse(ContainerTy &&C)
Definition: STLExtras.h:420
bool isIntrinsicReturningPointerAliasingArgumentWithoutCapturing(const CallBase *Call, bool MustPreserveNullness)
{launder,strip}.invariant.group returns pointer that aliases its argument, and it only captures point...
bool isModSet(const ModRefInfo MRI)
Definition: ModRef.h:48
void sort(IteratorTy Start, IteratorTy End)
Definition: STLExtras.h:1664
MemoryEffectsBase< IRMemLocation > MemoryEffects
Summary of how a function affects memory in the program.
Definition: ModRef.h:268
bool PointerMayBeCaptured(const Value *V, bool ReturnCaptures, bool StoreCaptures, unsigned MaxUsesToExplore=0)
PointerMayBeCaptured - Return true if this pointer value may be captured by the enclosing function (w...
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
bool none_of(R &&Range, UnaryPredicate P)
Provide wrappers to std::none_of which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1753
bool isKnownNonZero(const Value *V, const SimplifyQuery &Q, unsigned Depth=0)
Return true if the given value is known to be non-zero when defined.
ModRefInfo
Flags indicating whether a memory access modifies or references memory.
Definition: ModRef.h:27
@ ArgMem
Access to memory via argument pointers.
const Value * getUnderlyingObjectAggressive(const Value *V)
Like getUnderlyingObject(), but will try harder to find a single underlying object.
bool isGuaranteedToTransferExecutionToSuccessor(const Instruction *I)
Return true if this function can prove that the instruction I will always transfer execution to one o...
void erase_if(Container &C, UnaryPredicate P)
Provide a container algorithm similar to C++ Library Fundamentals v2's erase_if which is equivalent t...
Definition: STLExtras.h:2099
bool inferAttributesFromOthers(Function &F)
If we can infer one attribute from another on the declaration of a function, explicitly materialize t...
Definition: Local.cpp:4316
bool isNoModRef(const ModRefInfo MRI)
Definition: ModRef.h:39
void FindFunctionBackedges(const Function &F, SmallVectorImpl< std::pair< const BasicBlock *, const BasicBlock * > > &Result)
Analyze the specified function to find all of the loop backedges in the function and return them.
Definition: CFG.cpp:34
bool isIdentifiedObject(const Value *V)
Return true if this pointer refers to a distinct and identifiable object.
bool isRefSet(const ModRefInfo MRI)
Definition: ModRef.h:51
#define N
Support structure for SCC passes to communicate updates the call graph back to the CGSCC pass manager...
This callback is used in conjunction with PointerMayBeCaptured.
virtual void tooManyUses()=0
tooManyUses - The depth of traversal has breached a limit.
virtual bool captured(const Use *U)=0
captured - Information about the pointer was captured by the user of use U.
Flags specific to function summaries.
SmallVectorImpl< ArgumentGraphNode * >::iterator ChildIteratorType
static ChildIteratorType child_begin(NodeRef N)
static ChildIteratorType child_end(NodeRef N)
static NodeRef getEntryNode(NodeRef A)
static ChildIteratorType nodes_end(ArgumentGraph *AG)
static NodeRef getEntryNode(ArgumentGraph *AG)
static ChildIteratorType nodes_begin(ArgumentGraph *AG)
A CRTP mix-in to automatically provide informational APIs needed for passes.
Definition: PassManager.h:69
PreservedAnalyses run(LazyCallGraph::SCC &C, CGSCCAnalysisManager &AM, LazyCallGraph &CG, CGSCCUpdateResult &UR)
void printPipeline(raw_ostream &OS, function_ref< StringRef(StringRef)> MapClassName2PassName)
Struct that holds a reference to a particular GUID in a global value summary.