LLVM 21.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::getWithCaptureInfo(A.getContext(),
1213 ++NumNoCapture;
1214 Changed.insert(F);
1215 }
1216 }
1217 continue;
1218 }
1219
1220 for (Argument &A : F->args()) {
1221 if (!A.getType()->isPointerTy())
1222 continue;
1223 bool HasNonLocalUses = false;
1224 if (!A.hasNoCaptureAttr()) {
1225 ArgumentUsesTracker Tracker(SCCNodes);
1226 PointerMayBeCaptured(&A, &Tracker);
1227 if (!Tracker.Captured) {
1228 if (Tracker.Uses.empty()) {
1229 // If it's trivially not captured, mark it nocapture now.
1230 A.addAttr(Attribute::getWithCaptureInfo(A.getContext(),
1232 ++NumNoCapture;
1233 Changed.insert(F);
1234 } else {
1235 // If it's not trivially captured and not trivially not captured,
1236 // then it must be calling into another function in our SCC. Save
1237 // its particulars for Argument-SCC analysis later.
1238 ArgumentGraphNode *Node = AG[&A];
1239 for (Argument *Use : Tracker.Uses) {
1240 Node->Uses.push_back(AG[Use]);
1241 if (Use != &A)
1242 HasNonLocalUses = true;
1243 }
1244 }
1245 }
1246 // Otherwise, it's captured. Don't bother doing SCC analysis on it.
1247 }
1248 if (!HasNonLocalUses && !A.onlyReadsMemory()) {
1249 // Can we determine that it's readonly/readnone/writeonly without doing
1250 // an SCC? Note that we don't allow any calls at all here, or else our
1251 // result will be dependent on the iteration order through the
1252 // functions in the SCC.
1254 Self.insert(&A);
1256 if (R != Attribute::None)
1257 if (addAccessAttr(&A, R))
1258 Changed.insert(F);
1259 }
1260 if (!SkipInitializes && !A.onlyReadsMemory()) {
1261 if (inferInitializes(A, *F))
1262 Changed.insert(F);
1263 }
1264 }
1265 }
1266
1267 // The graph we've collected is partial because we stopped scanning for
1268 // argument uses once we solved the argument trivially. These partial nodes
1269 // show up as ArgumentGraphNode objects with an empty Uses list, and for
1270 // these nodes the final decision about whether they capture has already been
1271 // made. If the definition doesn't have a 'nocapture' attribute by now, it
1272 // captures.
1273
1274 for (scc_iterator<ArgumentGraph *> I = scc_begin(&AG); !I.isAtEnd(); ++I) {
1275 const std::vector<ArgumentGraphNode *> &ArgumentSCC = *I;
1276 if (ArgumentSCC.size() == 1) {
1277 if (!ArgumentSCC[0]->Definition)
1278 continue; // synthetic root node
1279
1280 // eg. "void f(int* x) { if (...) f(x); }"
1281 if (ArgumentSCC[0]->Uses.size() == 1 &&
1282 ArgumentSCC[0]->Uses[0] == ArgumentSCC[0]) {
1283 Argument *A = ArgumentSCC[0]->Definition;
1284 A->addAttr(Attribute::getWithCaptureInfo(A->getContext(),
1286 ++NumNoCapture;
1287 Changed.insert(A->getParent());
1288
1289 // Infer the access attributes given the new nocapture one
1291 Self.insert(&*A);
1293 if (R != Attribute::None)
1294 addAccessAttr(A, R);
1295 }
1296 continue;
1297 }
1298
1299 SmallPtrSet<Argument *, 8> ArgumentSCCNodes;
1300 // Fill ArgumentSCCNodes with the elements of the ArgumentSCC. Used for
1301 // quickly looking up whether a given Argument is in this ArgumentSCC.
1302 for (ArgumentGraphNode *I : ArgumentSCC) {
1303 ArgumentSCCNodes.insert(I->Definition);
1304 }
1305
1306 bool SCCCaptured = false;
1307 for (ArgumentGraphNode *N : ArgumentSCC) {
1308 for (ArgumentGraphNode *Use : N->Uses) {
1309 Argument *A = Use->Definition;
1310 if (A->hasNoCaptureAttr() || ArgumentSCCNodes.count(A))
1311 continue;
1312 SCCCaptured = true;
1313 break;
1314 }
1315 if (SCCCaptured)
1316 break;
1317 }
1318 if (SCCCaptured)
1319 continue;
1320
1321 for (ArgumentGraphNode *N : ArgumentSCC) {
1322 Argument *A = N->Definition;
1323 A->addAttr(
1325 ++NumNoCapture;
1326 Changed.insert(A->getParent());
1327 }
1328
1329 // We also want to compute readonly/readnone/writeonly. With a small number
1330 // of false negatives, we can assume that any pointer which is captured
1331 // isn't going to be provably readonly or readnone, since by definition
1332 // we can't analyze all uses of a captured pointer.
1333 //
1334 // The false negatives happen when the pointer is captured by a function
1335 // that promises readonly/readnone behaviour on the pointer, then the
1336 // pointer's lifetime ends before anything that writes to arbitrary memory.
1337 // Also, a readonly/readnone pointer may be returned, but returning a
1338 // pointer is capturing it.
1339
1340 auto meetAccessAttr = [](Attribute::AttrKind A, Attribute::AttrKind B) {
1341 if (A == B)
1342 return A;
1343 if (A == Attribute::ReadNone)
1344 return B;
1345 if (B == Attribute::ReadNone)
1346 return A;
1347 return Attribute::None;
1348 };
1349
1350 Attribute::AttrKind AccessAttr = Attribute::ReadNone;
1351 for (ArgumentGraphNode *N : ArgumentSCC) {
1352 Argument *A = N->Definition;
1353 Attribute::AttrKind K = determinePointerAccessAttrs(A, ArgumentSCCNodes);
1354 AccessAttr = meetAccessAttr(AccessAttr, K);
1355 if (AccessAttr == Attribute::None)
1356 break;
1357 }
1358
1359 if (AccessAttr != Attribute::None) {
1360 for (ArgumentGraphNode *N : ArgumentSCC) {
1361 Argument *A = N->Definition;
1362 if (addAccessAttr(A, AccessAttr))
1363 Changed.insert(A->getParent());
1364 }
1365 }
1366 }
1367}
1368
1369/// Tests whether a function is "malloc-like".
1370///
1371/// A function is "malloc-like" if it returns either null or a pointer that
1372/// doesn't alias any other pointer visible to the caller.
1373static bool isFunctionMallocLike(Function *F, const SCCNodeSet &SCCNodes) {
1374 SmallSetVector<Value *, 8> FlowsToReturn;
1375 for (BasicBlock &BB : *F)
1376 if (ReturnInst *Ret = dyn_cast<ReturnInst>(BB.getTerminator()))
1377 FlowsToReturn.insert(Ret->getReturnValue());
1378
1379 for (unsigned i = 0; i != FlowsToReturn.size(); ++i) {
1380 Value *RetVal = FlowsToReturn[i];
1381
1382 if (Constant *C = dyn_cast<Constant>(RetVal)) {
1383 if (!C->isNullValue() && !isa<UndefValue>(C))
1384 return false;
1385
1386 continue;
1387 }
1388
1389 if (isa<Argument>(RetVal))
1390 return false;
1391
1392 if (Instruction *RVI = dyn_cast<Instruction>(RetVal))
1393 switch (RVI->getOpcode()) {
1394 // Extend the analysis by looking upwards.
1395 case Instruction::BitCast:
1396 case Instruction::GetElementPtr:
1397 case Instruction::AddrSpaceCast:
1398 FlowsToReturn.insert(RVI->getOperand(0));
1399 continue;
1400 case Instruction::Select: {
1401 SelectInst *SI = cast<SelectInst>(RVI);
1402 FlowsToReturn.insert(SI->getTrueValue());
1403 FlowsToReturn.insert(SI->getFalseValue());
1404 continue;
1405 }
1406 case Instruction::PHI: {
1407 PHINode *PN = cast<PHINode>(RVI);
1408 for (Value *IncValue : PN->incoming_values())
1409 FlowsToReturn.insert(IncValue);
1410 continue;
1411 }
1412
1413 // Check whether the pointer came from an allocation.
1414 case Instruction::Alloca:
1415 break;
1416 case Instruction::Call:
1417 case Instruction::Invoke: {
1418 CallBase &CB = cast<CallBase>(*RVI);
1419 if (CB.hasRetAttr(Attribute::NoAlias))
1420 break;
1421 if (CB.getCalledFunction() && SCCNodes.count(CB.getCalledFunction()))
1422 break;
1423 [[fallthrough]];
1424 }
1425 default:
1426 return false; // Did not come from an allocation.
1427 }
1428
1429 if (PointerMayBeCaptured(RetVal, false, /*StoreCaptures=*/false))
1430 return false;
1431 }
1432
1433 return true;
1434}
1435
1436/// Deduce noalias attributes for the SCC.
1437static void addNoAliasAttrs(const SCCNodeSet &SCCNodes,
1438 SmallSet<Function *, 8> &Changed) {
1439 // Check each function in turn, determining which functions return noalias
1440 // pointers.
1441 for (Function *F : SCCNodes) {
1442 // Already noalias.
1443 if (F->returnDoesNotAlias())
1444 continue;
1445
1446 // We can infer and propagate function attributes only when we know that the
1447 // definition we'll get at link time is *exactly* the definition we see now.
1448 // For more details, see GlobalValue::mayBeDerefined.
1449 if (!F->hasExactDefinition())
1450 return;
1451
1452 // We annotate noalias return values, which are only applicable to
1453 // pointer types.
1454 if (!F->getReturnType()->isPointerTy())
1455 continue;
1456
1457 if (!isFunctionMallocLike(F, SCCNodes))
1458 return;
1459 }
1460
1461 for (Function *F : SCCNodes) {
1462 if (F->returnDoesNotAlias() ||
1463 !F->getReturnType()->isPointerTy())
1464 continue;
1465
1466 F->setReturnDoesNotAlias();
1467 ++NumNoAlias;
1468 Changed.insert(F);
1469 }
1470}
1471
1472/// Tests whether this function is known to not return null.
1473///
1474/// Requires that the function returns a pointer.
1475///
1476/// Returns true if it believes the function will not return a null, and sets
1477/// \p Speculative based on whether the returned conclusion is a speculative
1478/// conclusion due to SCC calls.
1479static bool isReturnNonNull(Function *F, const SCCNodeSet &SCCNodes,
1480 bool &Speculative) {
1481 assert(F->getReturnType()->isPointerTy() &&
1482 "nonnull only meaningful on pointer types");
1483 Speculative = false;
1484
1485 SmallSetVector<Value *, 8> FlowsToReturn;
1486 for (BasicBlock &BB : *F)
1487 if (auto *Ret = dyn_cast<ReturnInst>(BB.getTerminator()))
1488 FlowsToReturn.insert(Ret->getReturnValue());
1489
1490 auto &DL = F->getDataLayout();
1491
1492 for (unsigned i = 0; i != FlowsToReturn.size(); ++i) {
1493 Value *RetVal = FlowsToReturn[i];
1494
1495 // If this value is locally known to be non-null, we're good
1496 if (isKnownNonZero(RetVal, DL))
1497 continue;
1498
1499 // Otherwise, we need to look upwards since we can't make any local
1500 // conclusions.
1501 Instruction *RVI = dyn_cast<Instruction>(RetVal);
1502 if (!RVI)
1503 return false;
1504 switch (RVI->getOpcode()) {
1505 // Extend the analysis by looking upwards.
1506 case Instruction::BitCast:
1507 case Instruction::AddrSpaceCast:
1508 FlowsToReturn.insert(RVI->getOperand(0));
1509 continue;
1510 case Instruction::GetElementPtr:
1511 if (cast<GEPOperator>(RVI)->isInBounds()) {
1512 FlowsToReturn.insert(RVI->getOperand(0));
1513 continue;
1514 }
1515 return false;
1516 case Instruction::Select: {
1517 SelectInst *SI = cast<SelectInst>(RVI);
1518 FlowsToReturn.insert(SI->getTrueValue());
1519 FlowsToReturn.insert(SI->getFalseValue());
1520 continue;
1521 }
1522 case Instruction::PHI: {
1523 PHINode *PN = cast<PHINode>(RVI);
1524 for (int i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
1525 FlowsToReturn.insert(PN->getIncomingValue(i));
1526 continue;
1527 }
1528 case Instruction::Call:
1529 case Instruction::Invoke: {
1530 CallBase &CB = cast<CallBase>(*RVI);
1531 Function *Callee = CB.getCalledFunction();
1532 // A call to a node within the SCC is assumed to return null until
1533 // proven otherwise
1534 if (Callee && SCCNodes.count(Callee)) {
1535 Speculative = true;
1536 continue;
1537 }
1538 return false;
1539 }
1540 default:
1541 return false; // Unknown source, may be null
1542 };
1543 llvm_unreachable("should have either continued or returned");
1544 }
1545
1546 return true;
1547}
1548
1549/// Deduce nonnull attributes for the SCC.
1550static void addNonNullAttrs(const SCCNodeSet &SCCNodes,
1551 SmallSet<Function *, 8> &Changed) {
1552 // Speculative that all functions in the SCC return only nonnull
1553 // pointers. We may refute this as we analyze functions.
1554 bool SCCReturnsNonNull = true;
1555
1556 // Check each function in turn, determining which functions return nonnull
1557 // pointers.
1558 for (Function *F : SCCNodes) {
1559 // Already nonnull.
1560 if (F->getAttributes().hasRetAttr(Attribute::NonNull))
1561 continue;
1562
1563 // We can infer and propagate function attributes only when we know that the
1564 // definition we'll get at link time is *exactly* the definition we see now.
1565 // For more details, see GlobalValue::mayBeDerefined.
1566 if (!F->hasExactDefinition())
1567 return;
1568
1569 // We annotate nonnull return values, which are only applicable to
1570 // pointer types.
1571 if (!F->getReturnType()->isPointerTy())
1572 continue;
1573
1574 bool Speculative = false;
1575 if (isReturnNonNull(F, SCCNodes, Speculative)) {
1576 if (!Speculative) {
1577 // Mark the function eagerly since we may discover a function
1578 // which prevents us from speculating about the entire SCC
1579 LLVM_DEBUG(dbgs() << "Eagerly marking " << F->getName()
1580 << " as nonnull\n");
1581 F->addRetAttr(Attribute::NonNull);
1582 ++NumNonNullReturn;
1583 Changed.insert(F);
1584 }
1585 continue;
1586 }
1587 // At least one function returns something which could be null, can't
1588 // speculate any more.
1589 SCCReturnsNonNull = false;
1590 }
1591
1592 if (SCCReturnsNonNull) {
1593 for (Function *F : SCCNodes) {
1594 if (F->getAttributes().hasRetAttr(Attribute::NonNull) ||
1595 !F->getReturnType()->isPointerTy())
1596 continue;
1597
1598 LLVM_DEBUG(dbgs() << "SCC marking " << F->getName() << " as nonnull\n");
1599 F->addRetAttr(Attribute::NonNull);
1600 ++NumNonNullReturn;
1601 Changed.insert(F);
1602 }
1603 }
1604}
1605
1606/// Deduce noundef attributes for the SCC.
1607static void addNoUndefAttrs(const SCCNodeSet &SCCNodes,
1608 SmallSet<Function *, 8> &Changed) {
1609 // Check each function in turn, determining which functions return noundef
1610 // values.
1611 for (Function *F : SCCNodes) {
1612 // Already noundef.
1613 AttributeList Attrs = F->getAttributes();
1614 if (Attrs.hasRetAttr(Attribute::NoUndef))
1615 continue;
1616
1617 // We can infer and propagate function attributes only when we know that the
1618 // definition we'll get at link time is *exactly* the definition we see now.
1619 // For more details, see GlobalValue::mayBeDerefined.
1620 if (!F->hasExactDefinition())
1621 return;
1622
1623 // MemorySanitizer assumes that the definition and declaration of a
1624 // function will be consistent. A function with sanitize_memory attribute
1625 // should be skipped from inference.
1626 if (F->hasFnAttribute(Attribute::SanitizeMemory))
1627 continue;
1628
1629 if (F->getReturnType()->isVoidTy())
1630 continue;
1631
1632 const DataLayout &DL = F->getDataLayout();
1633 if (all_of(*F, [&](BasicBlock &BB) {
1634 if (auto *Ret = dyn_cast<ReturnInst>(BB.getTerminator())) {
1635 // TODO: perform context-sensitive analysis?
1636 Value *RetVal = Ret->getReturnValue();
1637 if (!isGuaranteedNotToBeUndefOrPoison(RetVal))
1638 return false;
1639
1640 // We know the original return value is not poison now, but it
1641 // could still be converted to poison by another return attribute.
1642 // Try to explicitly re-prove the relevant attributes.
1643 if (Attrs.hasRetAttr(Attribute::NonNull) &&
1644 !isKnownNonZero(RetVal, DL))
1645 return false;
1646
1647 if (MaybeAlign Align = Attrs.getRetAlignment())
1648 if (RetVal->getPointerAlignment(DL) < *Align)
1649 return false;
1650
1651 Attribute Attr = Attrs.getRetAttr(Attribute::Range);
1652 if (Attr.isValid() &&
1653 !Attr.getRange().contains(
1654 computeConstantRange(RetVal, /*ForSigned=*/false)))
1655 return false;
1656 }
1657 return true;
1658 })) {
1659 F->addRetAttr(Attribute::NoUndef);
1660 ++NumNoUndefReturn;
1661 Changed.insert(F);
1662 }
1663 }
1664}
1665
1666namespace {
1667
1668/// Collects a set of attribute inference requests and performs them all in one
1669/// go on a single SCC Node. Inference involves scanning function bodies
1670/// looking for instructions that violate attribute assumptions.
1671/// As soon as all the bodies are fine we are free to set the attribute.
1672/// Customization of inference for individual attributes is performed by
1673/// providing a handful of predicates for each attribute.
1674class AttributeInferer {
1675public:
1676 /// Describes a request for inference of a single attribute.
1677 struct InferenceDescriptor {
1678
1679 /// Returns true if this function does not have to be handled.
1680 /// General intent for this predicate is to provide an optimization
1681 /// for functions that do not need this attribute inference at all
1682 /// (say, for functions that already have the attribute).
1683 std::function<bool(const Function &)> SkipFunction;
1684
1685 /// Returns true if this instruction violates attribute assumptions.
1686 std::function<bool(Instruction &)> InstrBreaksAttribute;
1687
1688 /// Sets the inferred attribute for this function.
1689 std::function<void(Function &)> SetAttribute;
1690
1691 /// Attribute we derive.
1692 Attribute::AttrKind AKind;
1693
1694 /// If true, only "exact" definitions can be used to infer this attribute.
1695 /// See GlobalValue::isDefinitionExact.
1696 bool RequiresExactDefinition;
1697
1698 InferenceDescriptor(Attribute::AttrKind AK,
1699 std::function<bool(const Function &)> SkipFunc,
1700 std::function<bool(Instruction &)> InstrScan,
1701 std::function<void(Function &)> SetAttr,
1702 bool ReqExactDef)
1703 : SkipFunction(SkipFunc), InstrBreaksAttribute(InstrScan),
1704 SetAttribute(SetAttr), AKind(AK),
1705 RequiresExactDefinition(ReqExactDef) {}
1706 };
1707
1708private:
1709 SmallVector<InferenceDescriptor, 4> InferenceDescriptors;
1710
1711public:
1712 void registerAttrInference(InferenceDescriptor AttrInference) {
1713 InferenceDescriptors.push_back(AttrInference);
1714 }
1715
1716 void run(const SCCNodeSet &SCCNodes, SmallSet<Function *, 8> &Changed);
1717};
1718
1719/// Perform all the requested attribute inference actions according to the
1720/// attribute predicates stored before.
1721void AttributeInferer::run(const SCCNodeSet &SCCNodes,
1722 SmallSet<Function *, 8> &Changed) {
1723 SmallVector<InferenceDescriptor, 4> InferInSCC = InferenceDescriptors;
1724 // Go through all the functions in SCC and check corresponding attribute
1725 // assumptions for each of them. Attributes that are invalid for this SCC
1726 // will be removed from InferInSCC.
1727 for (Function *F : SCCNodes) {
1728
1729 // No attributes whose assumptions are still valid - done.
1730 if (InferInSCC.empty())
1731 return;
1732
1733 // Check if our attributes ever need scanning/can be scanned.
1734 llvm::erase_if(InferInSCC, [F](const InferenceDescriptor &ID) {
1735 if (ID.SkipFunction(*F))
1736 return false;
1737
1738 // Remove from further inference (invalidate) when visiting a function
1739 // that has no instructions to scan/has an unsuitable definition.
1740 return F->isDeclaration() ||
1741 (ID.RequiresExactDefinition && !F->hasExactDefinition());
1742 });
1743
1744 // For each attribute still in InferInSCC that doesn't explicitly skip F,
1745 // set up the F instructions scan to verify assumptions of the attribute.
1748 InferInSCC, std::back_inserter(InferInThisFunc),
1749 [F](const InferenceDescriptor &ID) { return !ID.SkipFunction(*F); });
1750
1751 if (InferInThisFunc.empty())
1752 continue;
1753
1754 // Start instruction scan.
1755 for (Instruction &I : instructions(*F)) {
1756 llvm::erase_if(InferInThisFunc, [&](const InferenceDescriptor &ID) {
1757 if (!ID.InstrBreaksAttribute(I))
1758 return false;
1759 // Remove attribute from further inference on any other functions
1760 // because attribute assumptions have just been violated.
1761 llvm::erase_if(InferInSCC, [&ID](const InferenceDescriptor &D) {
1762 return D.AKind == ID.AKind;
1763 });
1764 // Remove attribute from the rest of current instruction scan.
1765 return true;
1766 });
1767
1768 if (InferInThisFunc.empty())
1769 break;
1770 }
1771 }
1772
1773 if (InferInSCC.empty())
1774 return;
1775
1776 for (Function *F : SCCNodes)
1777 // At this point InferInSCC contains only functions that were either:
1778 // - explicitly skipped from scan/inference, or
1779 // - verified to have no instructions that break attribute assumptions.
1780 // Hence we just go and force the attribute for all non-skipped functions.
1781 for (auto &ID : InferInSCC) {
1782 if (ID.SkipFunction(*F))
1783 continue;
1784 Changed.insert(F);
1785 ID.SetAttribute(*F);
1786 }
1787}
1788
1789struct SCCNodesResult {
1790 SCCNodeSet SCCNodes;
1791 bool HasUnknownCall;
1792};
1793
1794} // end anonymous namespace
1795
1796/// Helper for non-Convergent inference predicate InstrBreaksAttribute.
1798 const SCCNodeSet &SCCNodes) {
1799 const CallBase *CB = dyn_cast<CallBase>(&I);
1800 // Breaks non-convergent assumption if CS is a convergent call to a function
1801 // not in the SCC.
1802 return CB && CB->isConvergent() &&
1803 !SCCNodes.contains(CB->getCalledFunction());
1804}
1805
1806/// Helper for NoUnwind inference predicate InstrBreaksAttribute.
1807static bool InstrBreaksNonThrowing(Instruction &I, const SCCNodeSet &SCCNodes) {
1808 if (!I.mayThrow(/* IncludePhaseOneUnwind */ true))
1809 return false;
1810 if (const auto *CI = dyn_cast<CallInst>(&I)) {
1811 if (Function *Callee = CI->getCalledFunction()) {
1812 // I is a may-throw call to a function inside our SCC. This doesn't
1813 // invalidate our current working assumption that the SCC is no-throw; we
1814 // just have to scan that other function.
1815 if (SCCNodes.contains(Callee))
1816 return false;
1817 }
1818 }
1819 return true;
1820}
1821
1822/// Helper for NoFree inference predicate InstrBreaksAttribute.
1823static bool InstrBreaksNoFree(Instruction &I, const SCCNodeSet &SCCNodes) {
1824 CallBase *CB = dyn_cast<CallBase>(&I);
1825 if (!CB)
1826 return false;
1827
1828 if (CB->hasFnAttr(Attribute::NoFree))
1829 return false;
1830
1831 // Speculatively assume in SCC.
1832 if (Function *Callee = CB->getCalledFunction())
1833 if (SCCNodes.contains(Callee))
1834 return false;
1835
1836 return true;
1837}
1838
1839// Return true if this is an atomic which has an ordering stronger than
1840// unordered. Note that this is different than the predicate we use in
1841// Attributor. Here we chose to be conservative and consider monotonic
1842// operations potentially synchronizing. We generally don't do much with
1843// monotonic operations, so this is simply risk reduction.
1845 if (!I->isAtomic())
1846 return false;
1847
1848 if (auto *FI = dyn_cast<FenceInst>(I))
1849 // All legal orderings for fence are stronger than monotonic.
1850 return FI->getSyncScopeID() != SyncScope::SingleThread;
1851 else if (isa<AtomicCmpXchgInst>(I) || isa<AtomicRMWInst>(I))
1852 return true;
1853 else if (auto *SI = dyn_cast<StoreInst>(I))
1854 return !SI->isUnordered();
1855 else if (auto *LI = dyn_cast<LoadInst>(I))
1856 return !LI->isUnordered();
1857 else {
1858 llvm_unreachable("unknown atomic instruction?");
1859 }
1860}
1861
1862static bool InstrBreaksNoSync(Instruction &I, const SCCNodeSet &SCCNodes) {
1863 // Volatile may synchronize
1864 if (I.isVolatile())
1865 return true;
1866
1867 // An ordered atomic may synchronize. (See comment about on monotonic.)
1868 if (isOrderedAtomic(&I))
1869 return true;
1870
1871 auto *CB = dyn_cast<CallBase>(&I);
1872 if (!CB)
1873 // Non call site cases covered by the two checks above
1874 return false;
1875
1876 if (CB->hasFnAttr(Attribute::NoSync))
1877 return false;
1878
1879 // Non volatile memset/memcpy/memmoves are nosync
1880 // NOTE: Only intrinsics with volatile flags should be handled here. All
1881 // others should be marked in Intrinsics.td.
1882 if (auto *MI = dyn_cast<MemIntrinsic>(&I))
1883 if (!MI->isVolatile())
1884 return false;
1885
1886 // Speculatively assume in SCC.
1887 if (Function *Callee = CB->getCalledFunction())
1888 if (SCCNodes.contains(Callee))
1889 return false;
1890
1891 return true;
1892}
1893
1894/// Attempt to remove convergent function attribute when possible.
1895///
1896/// Returns true if any changes to function attributes were made.
1897static void inferConvergent(const SCCNodeSet &SCCNodes,
1898 SmallSet<Function *, 8> &Changed) {
1899 AttributeInferer AI;
1900
1901 // Request to remove the convergent attribute from all functions in the SCC
1902 // if every callsite within the SCC is not convergent (except for calls
1903 // to functions within the SCC).
1904 // Note: Removal of the attr from the callsites will happen in
1905 // InstCombineCalls separately.
1906 AI.registerAttrInference(AttributeInferer::InferenceDescriptor{
1907 Attribute::Convergent,
1908 // Skip non-convergent functions.
1909 [](const Function &F) { return !F.isConvergent(); },
1910 // Instructions that break non-convergent assumption.
1911 [SCCNodes](Instruction &I) {
1912 return InstrBreaksNonConvergent(I, SCCNodes);
1913 },
1914 [](Function &F) {
1915 LLVM_DEBUG(dbgs() << "Removing convergent attr from fn " << F.getName()
1916 << "\n");
1917 F.setNotConvergent();
1918 },
1919 /* RequiresExactDefinition= */ false});
1920 // Perform all the requested attribute inference actions.
1921 AI.run(SCCNodes, Changed);
1922}
1923
1924/// Infer attributes from all functions in the SCC by scanning every
1925/// instruction for compliance to the attribute assumptions.
1926///
1927/// Returns true if any changes to function attributes were made.
1928static void inferAttrsFromFunctionBodies(const SCCNodeSet &SCCNodes,
1929 SmallSet<Function *, 8> &Changed) {
1930 AttributeInferer AI;
1931
1933 // Request to infer nounwind attribute for all the functions in the SCC if
1934 // every callsite within the SCC is not throwing (except for calls to
1935 // functions within the SCC). Note that nounwind attribute suffers from
1936 // derefinement - results may change depending on how functions are
1937 // optimized. Thus it can be inferred only from exact definitions.
1938 AI.registerAttrInference(AttributeInferer::InferenceDescriptor{
1939 Attribute::NoUnwind,
1940 // Skip non-throwing functions.
1941 [](const Function &F) { return F.doesNotThrow(); },
1942 // Instructions that break non-throwing assumption.
1943 [&SCCNodes](Instruction &I) {
1944 return InstrBreaksNonThrowing(I, SCCNodes);
1945 },
1946 [](Function &F) {
1948 << "Adding nounwind attr to fn " << F.getName() << "\n");
1949 F.setDoesNotThrow();
1950 ++NumNoUnwind;
1951 },
1952 /* RequiresExactDefinition= */ true});
1953
1955 // Request to infer nofree attribute for all the functions in the SCC if
1956 // every callsite within the SCC does not directly or indirectly free
1957 // memory (except for calls to functions within the SCC). Note that nofree
1958 // attribute suffers from derefinement - results may change depending on
1959 // how functions are optimized. Thus it can be inferred only from exact
1960 // definitions.
1961 AI.registerAttrInference(AttributeInferer::InferenceDescriptor{
1962 Attribute::NoFree,
1963 // Skip functions known not to free memory.
1964 [](const Function &F) { return F.doesNotFreeMemory(); },
1965 // Instructions that break non-deallocating assumption.
1966 [&SCCNodes](Instruction &I) {
1967 return InstrBreaksNoFree(I, SCCNodes);
1968 },
1969 [](Function &F) {
1971 << "Adding nofree attr to fn " << F.getName() << "\n");
1972 F.setDoesNotFreeMemory();
1973 ++NumNoFree;
1974 },
1975 /* RequiresExactDefinition= */ true});
1976
1977 AI.registerAttrInference(AttributeInferer::InferenceDescriptor{
1978 Attribute::NoSync,
1979 // Skip already marked functions.
1980 [](const Function &F) { return F.hasNoSync(); },
1981 // Instructions that break nosync assumption.
1982 [&SCCNodes](Instruction &I) {
1983 return InstrBreaksNoSync(I, SCCNodes);
1984 },
1985 [](Function &F) {
1987 << "Adding nosync attr to fn " << F.getName() << "\n");
1988 F.setNoSync();
1989 ++NumNoSync;
1990 },
1991 /* RequiresExactDefinition= */ true});
1992
1993 // Perform all the requested attribute inference actions.
1994 AI.run(SCCNodes, Changed);
1995}
1996
1997static void addNoRecurseAttrs(const SCCNodeSet &SCCNodes,
1998 SmallSet<Function *, 8> &Changed) {
1999 // Try and identify functions that do not recurse.
2000
2001 // If the SCC contains multiple nodes we know for sure there is recursion.
2002 if (SCCNodes.size() != 1)
2003 return;
2004
2005 Function *F = *SCCNodes.begin();
2006 if (!F || !F->hasExactDefinition() || F->doesNotRecurse())
2007 return;
2008
2009 // If all of the calls in F are identifiable and are to norecurse functions, F
2010 // is norecurse. This check also detects self-recursion as F is not currently
2011 // marked norecurse, so any called from F to F will not be marked norecurse.
2012 for (auto &BB : *F)
2013 for (auto &I : BB.instructionsWithoutDebug())
2014 if (auto *CB = dyn_cast<CallBase>(&I)) {
2015 Function *Callee = CB->getCalledFunction();
2016 if (!Callee || Callee == F ||
2017 (!Callee->doesNotRecurse() &&
2018 !(Callee->isDeclaration() &&
2019 Callee->hasFnAttribute(Attribute::NoCallback))))
2020 // Function calls a potentially recursive function.
2021 return;
2022 }
2023
2024 // Every call was to a non-recursive function other than this function, and
2025 // we have no indirect recursion as the SCC size is one. This function cannot
2026 // recurse.
2027 F->setDoesNotRecurse();
2028 ++NumNoRecurse;
2029 Changed.insert(F);
2030}
2031
2033 if (auto *CB = dyn_cast<CallBase>(&I))
2034 return CB->hasFnAttr(Attribute::NoReturn);
2035 return false;
2036}
2037
2038// A basic block can only return if it terminates with a ReturnInst and does not
2039// contain calls to noreturn functions.
2041 if (!isa<ReturnInst>(BB.getTerminator()))
2042 return false;
2044}
2045
2046// FIXME: this doesn't handle recursion.
2047static bool canReturn(Function &F) {
2050
2051 Visited.insert(&F.front());
2052 Worklist.push_back(&F.front());
2053
2054 do {
2055 BasicBlock *BB = Worklist.pop_back_val();
2056 if (basicBlockCanReturn(*BB))
2057 return true;
2058 for (BasicBlock *Succ : successors(BB))
2059 if (Visited.insert(Succ).second)
2060 Worklist.push_back(Succ);
2061 } while (!Worklist.empty());
2062
2063 return false;
2064}
2065
2066
2067// Set the noreturn function attribute if possible.
2068static void addNoReturnAttrs(const SCCNodeSet &SCCNodes,
2069 SmallSet<Function *, 8> &Changed) {
2070 for (Function *F : SCCNodes) {
2071 if (!F || !F->hasExactDefinition() || F->hasFnAttribute(Attribute::Naked) ||
2072 F->doesNotReturn())
2073 continue;
2074
2075 if (!canReturn(*F)) {
2076 F->setDoesNotReturn();
2077 Changed.insert(F);
2078 }
2079 }
2080}
2081
2084 ColdPaths[&F.front()] = false;
2086 Jobs.push_back(&F.front());
2087
2088 while (!Jobs.empty()) {
2089 BasicBlock *BB = Jobs.pop_back_val();
2090
2091 // If block contains a cold callsite this path through the CG is cold.
2092 // Ignore whether the instructions actually are guaranteed to transfer
2093 // execution. Divergent behavior is considered unlikely.
2094 if (any_of(*BB, [](Instruction &I) {
2095 if (auto *CB = dyn_cast<CallBase>(&I))
2096 return CB->hasFnAttr(Attribute::Cold);
2097 return false;
2098 })) {
2099 ColdPaths[BB] = true;
2100 continue;
2101 }
2102
2103 auto Succs = successors(BB);
2104 // We found a path that doesn't go through any cold callsite.
2105 if (Succs.empty())
2106 return false;
2107
2108 // We didn't find a cold callsite in this BB, so check that all successors
2109 // contain a cold callsite (or that their successors do).
2110 // Potential TODO: We could use static branch hints to assume certain
2111 // successor paths are inherently cold, irrespective of if they contain a
2112 // cold callsite.
2113 for (BasicBlock *Succ : Succs) {
2114 // Start with false, this is necessary to ensure we don't turn loops into
2115 // cold.
2116 auto [Iter, Inserted] = ColdPaths.try_emplace(Succ, false);
2117 if (!Inserted) {
2118 if (Iter->second)
2119 continue;
2120 return false;
2121 }
2122 Jobs.push_back(Succ);
2123 }
2124 }
2125 return true;
2126}
2127
2128// Set the cold function attribute if possible.
2129static void addColdAttrs(const SCCNodeSet &SCCNodes,
2130 SmallSet<Function *, 8> &Changed) {
2131 for (Function *F : SCCNodes) {
2132 if (!F || !F->hasExactDefinition() || F->hasFnAttribute(Attribute::Naked) ||
2133 F->hasFnAttribute(Attribute::Cold) || F->hasFnAttribute(Attribute::Hot))
2134 continue;
2135
2136 // Potential TODO: We could add attribute `cold` on functions with `coldcc`.
2137 if (allPathsGoThroughCold(*F)) {
2138 F->addFnAttr(Attribute::Cold);
2139 ++NumCold;
2140 Changed.insert(F);
2141 continue;
2142 }
2143 }
2144}
2145
2146static bool functionWillReturn(const Function &F) {
2147 // We can infer and propagate function attributes only when we know that the
2148 // definition we'll get at link time is *exactly* the definition we see now.
2149 // For more details, see GlobalValue::mayBeDerefined.
2150 if (!F.hasExactDefinition())
2151 return false;
2152
2153 // Must-progress function without side-effects must return.
2154 if (F.mustProgress() && F.onlyReadsMemory())
2155 return true;
2156
2157 // Can only analyze functions with a definition.
2158 if (F.isDeclaration())
2159 return false;
2160
2161 // Functions with loops require more sophisticated analysis, as the loop
2162 // may be infinite. For now, don't try to handle them.
2164 FindFunctionBackedges(F, Backedges);
2165 if (!Backedges.empty())
2166 return false;
2167
2168 // If there are no loops, then the function is willreturn if all calls in
2169 // it are willreturn.
2170 return all_of(instructions(F), [](const Instruction &I) {
2171 return I.willReturn();
2172 });
2173}
2174
2175// Set the willreturn function attribute if possible.
2176static void addWillReturn(const SCCNodeSet &SCCNodes,
2177 SmallSet<Function *, 8> &Changed) {
2178 for (Function *F : SCCNodes) {
2179 if (!F || F->willReturn() || !functionWillReturn(*F))
2180 continue;
2181
2182 F->setWillReturn();
2183 NumWillReturn++;
2184 Changed.insert(F);
2185 }
2186}
2187
2188static SCCNodesResult createSCCNodeSet(ArrayRef<Function *> Functions) {
2189 SCCNodesResult Res;
2190 Res.HasUnknownCall = false;
2191 for (Function *F : Functions) {
2192 if (!F || F->hasOptNone() || F->hasFnAttribute(Attribute::Naked) ||
2193 F->isPresplitCoroutine()) {
2194 // Treat any function we're trying not to optimize as if it were an
2195 // indirect call and omit it from the node set used below.
2196 Res.HasUnknownCall = true;
2197 continue;
2198 }
2199 // Track whether any functions in this SCC have an unknown call edge.
2200 // Note: if this is ever a performance hit, we can common it with
2201 // subsequent routines which also do scans over the instructions of the
2202 // function.
2203 if (!Res.HasUnknownCall) {
2204 for (Instruction &I : instructions(*F)) {
2205 if (auto *CB = dyn_cast<CallBase>(&I)) {
2206 if (!CB->getCalledFunction()) {
2207 Res.HasUnknownCall = true;
2208 break;
2209 }
2210 }
2211 }
2212 }
2213 Res.SCCNodes.insert(F);
2214 }
2215 return Res;
2216}
2217
2218template <typename AARGetterT>
2220deriveAttrsInPostOrder(ArrayRef<Function *> Functions, AARGetterT &&AARGetter,
2221 bool ArgAttrsOnly) {
2222 SCCNodesResult Nodes = createSCCNodeSet(Functions);
2223
2224 // Bail if the SCC only contains optnone functions.
2225 if (Nodes.SCCNodes.empty())
2226 return {};
2227
2229 if (ArgAttrsOnly) {
2230 // ArgAttrsOnly means to only infer attributes that may aid optimizations
2231 // on the *current* function. "initializes" attribute is to aid
2232 // optimizations (like DSE) on the callers, so skip "initializes" here.
2233 addArgumentAttrs(Nodes.SCCNodes, Changed, /*SkipInitializes=*/true);
2234 return Changed;
2235 }
2236
2237 addArgumentReturnedAttrs(Nodes.SCCNodes, Changed);
2238 addMemoryAttrs(Nodes.SCCNodes, AARGetter, Changed);
2239 addArgumentAttrs(Nodes.SCCNodes, Changed, /*SkipInitializes=*/false);
2240 inferConvergent(Nodes.SCCNodes, Changed);
2241 addNoReturnAttrs(Nodes.SCCNodes, Changed);
2242 addColdAttrs(Nodes.SCCNodes, Changed);
2243 addWillReturn(Nodes.SCCNodes, Changed);
2244 addNoUndefAttrs(Nodes.SCCNodes, Changed);
2245
2246 // If we have no external nodes participating in the SCC, we can deduce some
2247 // more precise attributes as well.
2248 if (!Nodes.HasUnknownCall) {
2249 addNoAliasAttrs(Nodes.SCCNodes, Changed);
2250 addNonNullAttrs(Nodes.SCCNodes, Changed);
2251 inferAttrsFromFunctionBodies(Nodes.SCCNodes, Changed);
2252 addNoRecurseAttrs(Nodes.SCCNodes, Changed);
2253 }
2254
2255 // Finally, infer the maximal set of attributes from the ones we've inferred
2256 // above. This is handling the cases where one attribute on a signature
2257 // implies another, but for implementation reasons the inference rule for
2258 // the later is missing (or simply less sophisticated).
2259 for (Function *F : Nodes.SCCNodes)
2260 if (F)
2262 Changed.insert(F);
2263
2264 return Changed;
2265}
2266
2269 LazyCallGraph &CG,
2271 // Skip non-recursive functions if requested.
2272 // Only infer argument attributes for non-recursive functions, because
2273 // it can affect optimization behavior in conjunction with noalias.
2274 bool ArgAttrsOnly = false;
2275 if (C.size() == 1 && SkipNonRecursive) {
2276 LazyCallGraph::Node &N = *C.begin();
2277 if (!N->lookup(N))
2278 ArgAttrsOnly = true;
2279 }
2280
2282 AM.getResult<FunctionAnalysisManagerCGSCCProxy>(C, CG).getManager();
2283
2284 // We pass a lambda into functions to wire them up to the analysis manager
2285 // for getting function analyses.
2286 auto AARGetter = [&](Function &F) -> AAResults & {
2287 return FAM.getResult<AAManager>(F);
2288 };
2289
2291 for (LazyCallGraph::Node &N : C) {
2292 Functions.push_back(&N.getFunction());
2293 }
2294
2295 auto ChangedFunctions =
2296 deriveAttrsInPostOrder(Functions, AARGetter, ArgAttrsOnly);
2297 if (ChangedFunctions.empty())
2298 return PreservedAnalyses::all();
2299
2300 // Invalidate analyses for modified functions so that we don't have to
2301 // invalidate all analyses for all functions in this SCC.
2302 PreservedAnalyses FuncPA;
2303 // We haven't changed the CFG for modified functions.
2304 FuncPA.preserveSet<CFGAnalyses>();
2305 for (Function *Changed : ChangedFunctions) {
2306 FAM.invalidate(*Changed, FuncPA);
2307 // Also invalidate any direct callers of changed functions since analyses
2308 // may care about attributes of direct callees. For example, MemorySSA cares
2309 // about whether or not a call's callee modifies memory and queries that
2310 // through function attributes.
2311 for (auto *U : Changed->users()) {
2312 if (auto *Call = dyn_cast<CallBase>(U)) {
2313 if (Call->getCalledFunction() == Changed)
2314 FAM.invalidate(*Call->getFunction(), FuncPA);
2315 }
2316 }
2317 }
2318
2320 // We have not added or removed functions.
2322 // We already invalidated all relevant function analyses above.
2324 return PA;
2325}
2326
2328 raw_ostream &OS, function_ref<StringRef(StringRef)> MapClassName2PassName) {
2330 OS, MapClassName2PassName);
2331 if (SkipNonRecursive)
2332 OS << "<skip-non-recursive-function-attrs>";
2333}
2334
2335template <typename AARGetterT>
2336static bool runImpl(CallGraphSCC &SCC, AARGetterT AARGetter) {
2338 for (CallGraphNode *I : SCC) {
2339 Functions.push_back(I->getFunction());
2340 }
2341
2342 return !deriveAttrsInPostOrder(Functions, AARGetter).empty();
2343}
2344
2346 // We check the preconditions for the function prior to calling this to avoid
2347 // the cost of building up a reversible post-order list. We assert them here
2348 // to make sure none of the invariants this relies on were violated.
2349 assert(!F.isDeclaration() && "Cannot deduce norecurse without a definition!");
2350 assert(!F.doesNotRecurse() &&
2351 "This function has already been deduced as norecurs!");
2352 assert(F.hasInternalLinkage() &&
2353 "Can only do top-down deduction for internal linkage functions!");
2354
2355 // If F is internal and all of its uses are calls from a non-recursive
2356 // functions, then none of its calls could in fact recurse without going
2357 // through a function marked norecurse, and so we can mark this function too
2358 // as norecurse. Note that the uses must actually be calls -- otherwise
2359 // a pointer to this function could be returned from a norecurse function but
2360 // this function could be recursively (indirectly) called. Note that this
2361 // also detects if F is directly recursive as F is not yet marked as
2362 // a norecurse function.
2363 for (auto &U : F.uses()) {
2364 auto *I = dyn_cast<Instruction>(U.getUser());
2365 if (!I)
2366 return false;
2367 CallBase *CB = dyn_cast<CallBase>(I);
2368 if (!CB || !CB->isCallee(&U) ||
2369 !CB->getParent()->getParent()->doesNotRecurse())
2370 return false;
2371 }
2372 F.setDoesNotRecurse();
2373 ++NumNoRecurse;
2374 return true;
2375}
2376
2378 // We only have a post-order SCC traversal (because SCCs are inherently
2379 // discovered in post-order), so we accumulate them in a vector and then walk
2380 // it in reverse. This is simpler than using the RPO iterator infrastructure
2381 // because we need to combine SCC detection and the PO walk of the call
2382 // graph. We can also cheat egregiously because we're primarily interested in
2383 // synthesizing norecurse and so we can only save the singular SCCs as SCCs
2384 // with multiple functions in them will clearly be recursive.
2385
2387 CG.buildRefSCCs();
2388 for (LazyCallGraph::RefSCC &RC : CG.postorder_ref_sccs()) {
2389 for (LazyCallGraph::SCC &SCC : RC) {
2390 if (SCC.size() != 1)
2391 continue;
2392 Function &F = SCC.begin()->getFunction();
2393 if (!F.isDeclaration() && !F.doesNotRecurse() && F.hasInternalLinkage())
2394 Worklist.push_back(&F);
2395 }
2396 }
2397 bool Changed = false;
2398 for (auto *F : llvm::reverse(Worklist))
2399 Changed |= addNoRecurseAttrsTopDown(*F);
2400
2401 return Changed;
2402}
2403
2406 auto &CG = AM.getResult<LazyCallGraphAnalysis>(M);
2407
2408 if (!deduceFunctionAttributeInRPO(M, CG))
2409 return PreservedAnalyses::all();
2410
2413 return PA;
2414}
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:416
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
static Attribute getWithCaptureInfo(LLVMContext &Context, CaptureInfo CI)
Definition: Attributes.cpp:291
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:240
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:1680
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:1721
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:1629
bool dataOperandHasImpliedAttr(unsigned i, Attribute::AttrKind Kind) const
Return true if the data operand at index i has the attribute A.
Definition: InstrTypes.h:1657
bool isByValArgument(unsigned ArgNo) const
Determine whether this argument is passed by value.
Definition: InstrTypes.h:1685
bool onlyWritesMemory(unsigned OpNo) const
Definition: InstrTypes.h:1739
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:1727
Value * getArgOperand(unsigned i) const
Definition: InstrTypes.h:1286
bool isConvergent() const
Determine if the invoke is convergent.
Definition: InstrTypes.h:1943
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:1977
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.
static CaptureInfo none()
Create CaptureInfo that does not capture any components of the pointer.
Definition: ModRef.h:331
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:310
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:35
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:4313
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.