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