LLVM 22.0.0git
Attributor.cpp
Go to the documentation of this file.
1//===- Attributor.cpp - Module-wide attribute deduction -------------------===//
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// This file implements an interprocedural pass that deduces and/or propagates
10// attributes. This is done in an abstract interpretation style fixpoint
11// iteration. See the Attributor.h file comment and the class descriptions in
12// that file for more information.
13//
14//===----------------------------------------------------------------------===//
15
17
18#include "llvm/ADT/ArrayRef.h"
20#include "llvm/ADT/STLExtras.h"
22#include "llvm/ADT/Statistic.h"
29#include "llvm/IR/Attributes.h"
30#include "llvm/IR/Constant.h"
32#include "llvm/IR/Constants.h"
33#include "llvm/IR/DataLayout.h"
34#include "llvm/IR/GlobalValue.h"
36#include "llvm/IR/Instruction.h"
39#include "llvm/IR/LLVMContext.h"
40#include "llvm/IR/ValueHandle.h"
43#include "llvm/Support/Debug.h"
47#include "llvm/Support/ModRef.h"
52#include <cstdint>
53#include <memory>
54
55#ifdef EXPENSIVE_CHECKS
56#include "llvm/IR/Verifier.h"
57#endif
58
59#include <cassert>
60#include <optional>
61#include <string>
62
63using namespace llvm;
64
65#define DEBUG_TYPE "attributor"
66#define VERBOSE_DEBUG_TYPE DEBUG_TYPE "-verbose"
67
68DEBUG_COUNTER(ManifestDBGCounter, "attributor-manifest",
69 "Determine what attributes are manifested in the IR");
70
71STATISTIC(NumFnDeleted, "Number of function deleted");
72STATISTIC(NumFnWithExactDefinition,
73 "Number of functions with exact definitions");
74STATISTIC(NumFnWithoutExactDefinition,
75 "Number of functions without exact definitions");
76STATISTIC(NumFnShallowWrappersCreated, "Number of shallow wrappers created");
77STATISTIC(NumAttributesTimedOut,
78 "Number of abstract attributes timed out before fixpoint");
79STATISTIC(NumAttributesValidFixpoint,
80 "Number of abstract attributes in a valid fixpoint state");
81STATISTIC(NumAttributesManifested,
82 "Number of abstract attributes manifested in IR");
83
84// TODO: Determine a good default value.
85//
86// In the LLVM-TS and SPEC2006, 32 seems to not induce compile time overheads
87// (when run with the first 5 abstract attributes). The results also indicate
88// that we never reach 32 iterations but always find a fixpoint sooner.
89//
90// This will become more evolved once we perform two interleaved fixpoint
91// iterations: bottom-up and top-down.
93 SetFixpointIterations("attributor-max-iterations", cl::Hidden,
94 cl::desc("Maximal number of fixpoint iterations."),
95 cl::init(32));
96
98 MaxSpecializationPerCB("attributor-max-specializations-per-call-base",
100 cl::desc("Maximal number of callees specialized for "
101 "a call base"),
102 cl::init(UINT32_MAX));
103
105 "attributor-max-initialization-chain-length", cl::Hidden,
106 cl::desc(
107 "Maximal number of chained initializations (to avoid stack overflows)"),
110
112 "attributor-annotate-decl-cs", cl::Hidden,
113 cl::desc("Annotate call sites of function declarations."), cl::init(false));
114
115static cl::opt<bool> EnableHeapToStack("enable-heap-to-stack-conversion",
116 cl::init(true), cl::Hidden);
117
118static cl::opt<bool>
119 AllowShallowWrappers("attributor-allow-shallow-wrappers", cl::Hidden,
120 cl::desc("Allow the Attributor to create shallow "
121 "wrappers for non-exact definitions."),
122 cl::init(false));
123
124static cl::opt<bool>
125 AllowDeepWrapper("attributor-allow-deep-wrappers", cl::Hidden,
126 cl::desc("Allow the Attributor to use IP information "
127 "derived from non-exact functions via cloning"),
128 cl::init(false));
129
130// These options can only used for debug builds.
131#ifndef NDEBUG
133 SeedAllowList("attributor-seed-allow-list", cl::Hidden,
134 cl::desc("Comma separated list of attribute names that are "
135 "allowed to be seeded."),
137
139 "attributor-function-seed-allow-list", cl::Hidden,
140 cl::desc("Comma separated list of function names that are "
141 "allowed to be seeded."),
143#endif
144
145static cl::opt<bool>
146 DumpDepGraph("attributor-dump-dep-graph", cl::Hidden,
147 cl::desc("Dump the dependency graph to dot files."),
148 cl::init(false));
149
151 "attributor-depgraph-dot-filename-prefix", cl::Hidden,
152 cl::desc("The prefix used for the CallGraph dot file names."));
153
154static cl::opt<bool> ViewDepGraph("attributor-view-dep-graph", cl::Hidden,
155 cl::desc("View the dependency graph."),
156 cl::init(false));
157
158static cl::opt<bool> PrintDependencies("attributor-print-dep", cl::Hidden,
159 cl::desc("Print attribute dependencies"),
160 cl::init(false));
161
163 "attributor-enable-call-site-specific-deduction", cl::Hidden,
164 cl::desc("Allow the Attributor to do call site specific analysis"),
165 cl::init(false));
166
167static cl::opt<bool>
168 PrintCallGraph("attributor-print-call-graph", cl::Hidden,
169 cl::desc("Print Attributor's internal call graph"),
170 cl::init(false));
171
172static cl::opt<bool> SimplifyAllLoads("attributor-simplify-all-loads",
174 cl::desc("Try to simplify all loads."),
175 cl::init(true));
176
178 "attributor-assume-closed-world", cl::Hidden,
179 cl::desc("Should a closed world be assumed, or not. Default if not set."));
180
181/// Logic operators for the change status enum class.
182///
183///{
188 L = L | R;
189 return L;
190}
195 L = L & R;
196 return L;
197}
198///}
199
200bool AA::isGPU(const Module &M) {
201 Triple T(M.getTargetTriple());
202 return T.isGPU();
203}
204
206 const AbstractAttribute &QueryingAA) {
207 // We are looking for volatile instructions or non-relaxed atomics.
208 if (const auto *CB = dyn_cast<CallBase>(&I)) {
209 if (CB->hasFnAttr(Attribute::NoSync))
210 return true;
211
212 // Non-convergent and readnone imply nosync.
213 if (!CB->isConvergent() && !CB->mayReadOrWriteMemory())
214 return true;
215
217 return true;
218
219 bool IsKnownNoSync;
221 A, &QueryingAA, IRPosition::callsite_function(*CB),
222 DepClassTy::OPTIONAL, IsKnownNoSync);
223 }
224
225 if (!I.mayReadOrWriteMemory())
226 return true;
227
228 return !I.isVolatile() && !AANoSync::isNonRelaxedAtomic(&I);
229}
230
232 const Value &V, bool ForAnalysisOnly) {
233 // TODO: See the AAInstanceInfo class comment.
234 if (!ForAnalysisOnly)
235 return false;
236 auto *InstanceInfoAA = A.getAAFor<AAInstanceInfo>(
238 return InstanceInfoAA && InstanceInfoAA->isAssumedUniqueForAnalysis();
239}
240
241Constant *
243 Value &Obj, Type &Ty, const TargetLibraryInfo *TLI,
244 const DataLayout &DL, AA::RangeTy *RangePtr) {
245 if (Constant *Init = getInitialValueOfAllocation(&Obj, TLI, &Ty))
246 return Init;
247 auto *GV = dyn_cast<GlobalVariable>(&Obj);
248 if (!GV)
249 return nullptr;
250
251 bool UsedAssumedInformation = false;
252 Constant *Initializer = nullptr;
253 if (A.hasGlobalVariableSimplificationCallback(*GV)) {
254 auto AssumedGV = A.getAssumedInitializerFromCallBack(
255 *GV, &QueryingAA, UsedAssumedInformation);
256 Initializer = *AssumedGV;
257 if (!Initializer)
258 return nullptr;
259 } else {
260 if (!GV->hasLocalLinkage()) {
261 // Externally visible global that's either non-constant,
262 // or a constant with an uncertain initializer.
263 if (!GV->hasDefinitiveInitializer() || !GV->isConstant())
264 return nullptr;
265 }
266
267 // Globals with local linkage are always initialized.
268 assert(!GV->hasLocalLinkage() || GV->hasInitializer());
269
270 if (!Initializer)
271 Initializer = GV->getInitializer();
272 }
273
274 if (RangePtr && !RangePtr->offsetOrSizeAreUnknown()) {
275 APInt Offset = APInt(64, RangePtr->Offset);
276 return ConstantFoldLoadFromConst(Initializer, &Ty, Offset, DL);
277 }
278
279 return ConstantFoldLoadFromUniformValue(Initializer, &Ty, DL);
280}
281
282bool AA::isValidInScope(const Value &V, const Function *Scope) {
283 if (isa<Constant>(V))
284 return true;
285 if (auto *I = dyn_cast<Instruction>(&V))
286 return I->getFunction() == Scope;
287 if (auto *A = dyn_cast<Argument>(&V))
288 return A->getParent() == Scope;
289 return false;
290}
291
293 InformationCache &InfoCache) {
294 if (isa<Constant>(VAC.getValue()) || VAC.getValue() == VAC.getCtxI())
295 return true;
296 const Function *Scope = nullptr;
297 const Instruction *CtxI = VAC.getCtxI();
298 if (CtxI)
299 Scope = CtxI->getFunction();
300 if (auto *A = dyn_cast<Argument>(VAC.getValue()))
301 return A->getParent() == Scope;
302 if (auto *I = dyn_cast<Instruction>(VAC.getValue())) {
303 if (I->getFunction() == Scope) {
304 if (const DominatorTree *DT =
306 *Scope))
307 return DT->dominates(I, CtxI);
308 // Local dominance check mostly for the old PM passes.
309 if (CtxI && I->getParent() == CtxI->getParent())
310 return llvm::any_of(
311 make_range(I->getIterator(), I->getParent()->end()),
312 [&](const Instruction &AfterI) { return &AfterI == CtxI; });
313 }
314 }
315 return false;
316}
317
319 if (V.getType() == &Ty)
320 return &V;
321 if (isa<PoisonValue>(V))
322 return PoisonValue::get(&Ty);
323 if (isa<UndefValue>(V))
324 return UndefValue::get(&Ty);
325 if (auto *C = dyn_cast<Constant>(&V)) {
326 if (C->isNullValue() && !Ty.isPtrOrPtrVectorTy())
327 return Constant::getNullValue(&Ty);
328 if (C->getType()->isPointerTy() && Ty.isPointerTy())
329 return ConstantExpr::getPointerCast(C, &Ty);
330 if (C->getType()->getPrimitiveSizeInBits() >= Ty.getPrimitiveSizeInBits()) {
331 if (C->getType()->isIntegerTy() && Ty.isIntegerTy())
332 return ConstantExpr::getTrunc(C, &Ty, /* OnlyIfReduced */ true);
333 if (C->getType()->isFloatingPointTy() && Ty.isFloatingPointTy())
334 return ConstantFoldCastInstruction(Instruction::FPTrunc, C, &Ty);
335 }
336 }
337 return nullptr;
338}
339
340std::optional<Value *>
341AA::combineOptionalValuesInAAValueLatice(const std::optional<Value *> &A,
342 const std::optional<Value *> &B,
343 Type *Ty) {
344 if (A == B)
345 return A;
346 if (!B)
347 return A;
348 if (*B == nullptr)
349 return nullptr;
350 if (!A)
351 return Ty ? getWithType(**B, *Ty) : nullptr;
352 if (*A == nullptr)
353 return nullptr;
354 if (!Ty)
355 Ty = (*A)->getType();
357 return getWithType(**B, *Ty);
358 if (isa<UndefValue>(*B))
359 return A;
360 if (*A && *B && *A == getWithType(**B, *Ty))
361 return A;
362 return nullptr;
363}
364
365template <bool IsLoad, typename Ty>
367 Attributor &A, Ty &I, SmallSetVector<Value *, 4> &PotentialCopies,
368 SmallSetVector<Instruction *, 4> *PotentialValueOrigins,
369 const AbstractAttribute &QueryingAA, bool &UsedAssumedInformation,
370 bool OnlyExact) {
371 LLVM_DEBUG(dbgs() << "Trying to determine the potential copies of " << I
372 << " (only exact: " << OnlyExact << ")\n";);
373
374 Value &Ptr = *I.getPointerOperand();
375 // Containers to remember the pointer infos and new copies while we are not
376 // sure that we can find all of them. If we abort we want to avoid spurious
377 // dependences and potential copies in the provided container.
381
382 const auto *TLI =
383 A.getInfoCache().getTargetLibraryInfoForFunction(*I.getFunction());
384
385 auto Pred = [&](Value &Obj) {
386 LLVM_DEBUG(dbgs() << "Visit underlying object " << Obj << "\n");
387 if (isa<UndefValue>(&Obj))
388 return true;
389 if (isa<ConstantPointerNull>(&Obj)) {
390 // A null pointer access can be undefined but any offset from null may
391 // be OK. We do not try to optimize the latter.
392 if (!NullPointerIsDefined(I.getFunction(),
393 Ptr.getType()->getPointerAddressSpace()) &&
394 A.getAssumedSimplified(Ptr, QueryingAA, UsedAssumedInformation,
395 AA::Interprocedural) == &Obj)
396 return true;
398 dbgs() << "Underlying object is a valid nullptr, giving up.\n";);
399 return false;
400 }
401 // TODO: Use assumed noalias return.
402 if (!isa<AllocaInst>(&Obj) && !isa<GlobalVariable>(&Obj) &&
403 !(IsLoad ? isAllocationFn(&Obj, TLI) : isNoAliasCall(&Obj))) {
404 LLVM_DEBUG(dbgs() << "Underlying object is not supported yet: " << Obj
405 << "\n";);
406 return false;
407 }
408 if (auto *GV = dyn_cast<GlobalVariable>(&Obj))
409 if (!GV->hasLocalLinkage() &&
410 !(GV->isConstant() && GV->hasInitializer())) {
411 LLVM_DEBUG(dbgs() << "Underlying object is global with external "
412 "linkage, not supported yet: "
413 << Obj << "\n";);
414 return false;
415 }
416
417 bool NullOnly = true;
418 bool NullRequired = false;
419 auto CheckForNullOnlyAndUndef = [&](std::optional<Value *> V,
420 bool IsExact) {
421 if (!V || *V == nullptr)
422 NullOnly = false;
423 else if (isa<UndefValue>(*V))
424 /* No op */;
425 else if (isa<Constant>(*V) && cast<Constant>(*V)->isNullValue())
426 NullRequired = !IsExact;
427 else
428 NullOnly = false;
429 };
430
431 auto AdjustWrittenValueType = [&](const AAPointerInfo::Access &Acc,
432 Value &V) {
433 Value *AdjV = AA::getWithType(V, *I.getType());
434 if (!AdjV) {
435 LLVM_DEBUG(dbgs() << "Underlying object written but stored value "
436 "cannot be converted to read type: "
437 << *Acc.getRemoteInst() << " : " << *I.getType()
438 << "\n";);
439 }
440 return AdjV;
441 };
442
443 auto SkipCB = [&](const AAPointerInfo::Access &Acc) {
444 if ((IsLoad && !Acc.isWriteOrAssumption()) || (!IsLoad && !Acc.isRead()))
445 return true;
446 if (IsLoad) {
448 return true;
449 if (PotentialValueOrigins && !isa<AssumeInst>(Acc.getRemoteInst()))
450 return false;
451 if (!Acc.isWrittenValueUnknown())
452 if (Value *V = AdjustWrittenValueType(Acc, *Acc.getWrittenValue()))
453 if (NewCopies.count(V)) {
454 NewCopyOrigins.insert(Acc.getRemoteInst());
455 return true;
456 }
457 if (auto *SI = dyn_cast<StoreInst>(Acc.getRemoteInst()))
458 if (Value *V = AdjustWrittenValueType(Acc, *SI->getValueOperand()))
459 if (NewCopies.count(V)) {
460 NewCopyOrigins.insert(Acc.getRemoteInst());
461 return true;
462 }
463 }
464 return false;
465 };
466
467 auto CheckAccess = [&](const AAPointerInfo::Access &Acc, bool IsExact) {
468 if ((IsLoad && !Acc.isWriteOrAssumption()) || (!IsLoad && !Acc.isRead()))
469 return true;
470 if (IsLoad && Acc.isWrittenValueYetUndetermined())
471 return true;
472 CheckForNullOnlyAndUndef(Acc.getContent(), IsExact);
473 if (OnlyExact && !IsExact && !NullOnly &&
475 LLVM_DEBUG(dbgs() << "Non exact access " << *Acc.getRemoteInst()
476 << ", abort!\n");
477 return false;
478 }
479 if (NullRequired && !NullOnly) {
480 LLVM_DEBUG(dbgs() << "Required all `null` accesses due to non exact "
481 "one, however found non-null one: "
482 << *Acc.getRemoteInst() << ", abort!\n");
483 return false;
484 }
485 if (IsLoad) {
486 assert(isa<LoadInst>(I) && "Expected load or store instruction only!");
487 if (!Acc.isWrittenValueUnknown()) {
488 Value *V = AdjustWrittenValueType(Acc, *Acc.getWrittenValue());
489 if (!V)
490 return false;
491 NewCopies.insert(V);
492 if (PotentialValueOrigins)
493 NewCopyOrigins.insert(Acc.getRemoteInst());
494 return true;
495 }
496 auto *SI = dyn_cast<StoreInst>(Acc.getRemoteInst());
497 if (!SI) {
498 LLVM_DEBUG(dbgs() << "Underlying object written through a non-store "
499 "instruction not supported yet: "
500 << *Acc.getRemoteInst() << "\n";);
501 return false;
502 }
503 Value *V = AdjustWrittenValueType(Acc, *SI->getValueOperand());
504 if (!V)
505 return false;
506 NewCopies.insert(V);
507 if (PotentialValueOrigins)
508 NewCopyOrigins.insert(SI);
509 } else {
510 assert(isa<StoreInst>(I) && "Expected load or store instruction only!");
511 auto *LI = dyn_cast<LoadInst>(Acc.getRemoteInst());
512 if (!LI && OnlyExact) {
513 LLVM_DEBUG(dbgs() << "Underlying object read through a non-load "
514 "instruction not supported yet: "
515 << *Acc.getRemoteInst() << "\n";);
516 return false;
517 }
518 NewCopies.insert(Acc.getRemoteInst());
519 }
520 return true;
521 };
522
523 // If the value has been written to we don't need the initial value of the
524 // object.
525 bool HasBeenWrittenTo = false;
526
528 auto *PI = A.getAAFor<AAPointerInfo>(QueryingAA, IRPosition::value(Obj),
530 if (!PI || !PI->forallInterferingAccesses(
531 A, QueryingAA, I,
532 /* FindInterferingWrites */ IsLoad,
533 /* FindInterferingReads */ !IsLoad, CheckAccess,
534 HasBeenWrittenTo, Range, SkipCB)) {
536 dbgs()
537 << "Failed to verify all interfering accesses for underlying object: "
538 << Obj << "\n");
539 return false;
540 }
541
542 if (IsLoad && !HasBeenWrittenTo && !Range.isUnassigned()) {
543 const DataLayout &DL = A.getDataLayout();
544 Value *InitialValue = AA::getInitialValueForObj(
545 A, QueryingAA, Obj, *I.getType(), TLI, DL, &Range);
546 if (!InitialValue) {
547 LLVM_DEBUG(dbgs() << "Could not determine required initial value of "
548 "underlying object, abort!\n");
549 return false;
550 }
551 CheckForNullOnlyAndUndef(InitialValue, /* IsExact */ true);
552 if (NullRequired && !NullOnly) {
553 LLVM_DEBUG(dbgs() << "Non exact access but initial value that is not "
554 "null or undef, abort!\n");
555 return false;
556 }
557
558 NewCopies.insert(InitialValue);
559 if (PotentialValueOrigins)
560 NewCopyOrigins.insert(nullptr);
561 }
562
563 PIs.push_back(PI);
564
565 return true;
566 };
567
568 const auto *AAUO = A.getAAFor<AAUnderlyingObjects>(
570 if (!AAUO || !AAUO->forallUnderlyingObjects(Pred)) {
572 dbgs() << "Underlying objects stored into could not be determined\n";);
573 return false;
574 }
575
576 // Only if we were successful collection all potential copies we record
577 // dependences (on non-fix AAPointerInfo AAs). We also only then modify the
578 // given PotentialCopies container.
579 for (const auto *PI : PIs) {
580 if (!PI->getState().isAtFixpoint())
581 UsedAssumedInformation = true;
582 A.recordDependence(*PI, QueryingAA, DepClassTy::OPTIONAL);
583 }
584 PotentialCopies.insert_range(NewCopies);
585 if (PotentialValueOrigins)
586 PotentialValueOrigins->insert_range(NewCopyOrigins);
587
588 return true;
589}
590
592 Attributor &A, LoadInst &LI, SmallSetVector<Value *, 4> &PotentialValues,
593 SmallSetVector<Instruction *, 4> &PotentialValueOrigins,
594 const AbstractAttribute &QueryingAA, bool &UsedAssumedInformation,
595 bool OnlyExact) {
596 return getPotentialCopiesOfMemoryValue</* IsLoad */ true>(
597 A, LI, PotentialValues, &PotentialValueOrigins, QueryingAA,
598 UsedAssumedInformation, OnlyExact);
599}
600
603 const AbstractAttribute &QueryingAA, bool &UsedAssumedInformation,
604 bool OnlyExact) {
605 return getPotentialCopiesOfMemoryValue</* IsLoad */ false>(
606 A, SI, PotentialCopies, nullptr, QueryingAA, UsedAssumedInformation,
607 OnlyExact);
608}
609
611 const AbstractAttribute &QueryingAA,
612 bool RequireReadNone, bool &IsKnown) {
613 if (RequireReadNone) {
615 A, &QueryingAA, IRP, DepClassTy::OPTIONAL, IsKnown,
616 /* IgnoreSubsumingPositions */ true))
617 return true;
619 A, &QueryingAA, IRP, DepClassTy::OPTIONAL, IsKnown,
620 /* IgnoreSubsumingPositions */ true))
621 return true;
622
625 const auto *MemLocAA =
626 A.getAAFor<AAMemoryLocation>(QueryingAA, IRP, DepClassTy::NONE);
627 if (MemLocAA && MemLocAA->isAssumedReadNone()) {
628 IsKnown = MemLocAA->isKnownReadNone();
629 if (!IsKnown)
630 A.recordDependence(*MemLocAA, QueryingAA, DepClassTy::OPTIONAL);
631 return true;
632 }
633 }
634
635 const auto *MemBehaviorAA =
636 A.getAAFor<AAMemoryBehavior>(QueryingAA, IRP, DepClassTy::NONE);
637 if (MemBehaviorAA &&
638 (MemBehaviorAA->isAssumedReadNone() ||
639 (!RequireReadNone && MemBehaviorAA->isAssumedReadOnly()))) {
640 IsKnown = RequireReadNone ? MemBehaviorAA->isKnownReadNone()
641 : MemBehaviorAA->isKnownReadOnly();
642 if (!IsKnown)
643 A.recordDependence(*MemBehaviorAA, QueryingAA, DepClassTy::OPTIONAL);
644 return true;
645 }
646
647 return false;
648}
649
651 const AbstractAttribute &QueryingAA, bool &IsKnown) {
652 return isAssumedReadOnlyOrReadNone(A, IRP, QueryingAA,
653 /* RequireReadNone */ false, IsKnown);
654}
656 const AbstractAttribute &QueryingAA, bool &IsKnown) {
657 return isAssumedReadOnlyOrReadNone(A, IRP, QueryingAA,
658 /* RequireReadNone */ true, IsKnown);
659}
660
661static bool
663 const Instruction *ToI, const Function &ToFn,
664 const AbstractAttribute &QueryingAA,
665 const AA::InstExclusionSetTy *ExclusionSet,
666 std::function<bool(const Function &F)> GoBackwardsCB) {
668 dbgs() << "[AA] isPotentiallyReachable @" << ToFn.getName() << " from "
669 << FromI << " [GBCB: " << bool(GoBackwardsCB) << "][#ExS: "
670 << (ExclusionSet ? std::to_string(ExclusionSet->size()) : "none")
671 << "]\n";
672 if (ExclusionSet)
673 for (auto *ES : *ExclusionSet)
674 dbgs() << *ES << "\n";
675 });
676
677 // We know kernels (generally) cannot be called from within the module. Thus,
678 // for reachability we would need to step back from a kernel which would allow
679 // us to reach anything anyway. Even if a kernel is invoked from another
680 // kernel, values like allocas and shared memory are not accessible. We
681 // implicitly check for this situation to avoid costly lookups.
682 if (GoBackwardsCB && &ToFn != FromI.getFunction() &&
683 !GoBackwardsCB(*FromI.getFunction()) && A.getInfoCache().isKernel(ToFn) &&
684 A.getInfoCache().isKernel(*FromI.getFunction())) {
685 LLVM_DEBUG(dbgs() << "[AA] assume kernel cannot be reached from within the "
686 "module; success\n";);
687 return false;
688 }
689
690 // If we can go arbitrarily backwards we will eventually reach an entry point
691 // that can reach ToI. Only if a set of blocks through which we cannot go is
692 // provided, or once we track internal functions not accessible from the
693 // outside, it makes sense to perform backwards analysis in the absence of a
694 // GoBackwardsCB.
695 if (!GoBackwardsCB && !ExclusionSet) {
696 LLVM_DEBUG(dbgs() << "[AA] check @" << ToFn.getName() << " from " << FromI
697 << " is not checked backwards and does not have an "
698 "exclusion set, abort\n");
699 return true;
700 }
701
704 Worklist.push_back(&FromI);
705
706 while (!Worklist.empty()) {
707 const Instruction *CurFromI = Worklist.pop_back_val();
708 if (!Visited.insert(CurFromI).second)
709 continue;
710
711 const Function *FromFn = CurFromI->getFunction();
712 if (FromFn == &ToFn) {
713 if (!ToI)
714 return true;
715 LLVM_DEBUG(dbgs() << "[AA] check " << *ToI << " from " << *CurFromI
716 << " intraprocedurally\n");
717 const auto *ReachabilityAA = A.getAAFor<AAIntraFnReachability>(
718 QueryingAA, IRPosition::function(ToFn), DepClassTy::OPTIONAL);
719 bool Result = !ReachabilityAA || ReachabilityAA->isAssumedReachable(
720 A, *CurFromI, *ToI, ExclusionSet);
721 LLVM_DEBUG(dbgs() << "[AA] " << *CurFromI << " "
722 << (Result ? "can potentially " : "cannot ") << "reach "
723 << *ToI << " [Intra]\n");
724 if (Result)
725 return true;
726 }
727
728 bool Result = true;
729 if (!ToFn.isDeclaration() && ToI) {
730 const auto *ToReachabilityAA = A.getAAFor<AAIntraFnReachability>(
731 QueryingAA, IRPosition::function(ToFn), DepClassTy::OPTIONAL);
732 const Instruction &EntryI = ToFn.getEntryBlock().front();
733 Result = !ToReachabilityAA || ToReachabilityAA->isAssumedReachable(
734 A, EntryI, *ToI, ExclusionSet);
735 LLVM_DEBUG(dbgs() << "[AA] Entry " << EntryI << " of @" << ToFn.getName()
736 << " " << (Result ? "can potentially " : "cannot ")
737 << "reach @" << *ToI << " [ToFn]\n");
738 }
739
740 if (Result) {
741 // The entry of the ToFn can reach the instruction ToI. If the current
742 // instruction is already known to reach the ToFn.
743 const auto *FnReachabilityAA = A.getAAFor<AAInterFnReachability>(
744 QueryingAA, IRPosition::function(*FromFn), DepClassTy::OPTIONAL);
745 Result = !FnReachabilityAA || FnReachabilityAA->instructionCanReach(
746 A, *CurFromI, ToFn, ExclusionSet);
747 LLVM_DEBUG(dbgs() << "[AA] " << *CurFromI << " in @" << FromFn->getName()
748 << " " << (Result ? "can potentially " : "cannot ")
749 << "reach @" << ToFn.getName() << " [FromFn]\n");
750 if (Result)
751 return true;
752 }
753
754 // TODO: Check assumed nounwind.
755 const auto *ReachabilityAA = A.getAAFor<AAIntraFnReachability>(
756 QueryingAA, IRPosition::function(*FromFn), DepClassTy::OPTIONAL);
757 auto ReturnInstCB = [&](Instruction &Ret) {
758 bool Result = !ReachabilityAA || ReachabilityAA->isAssumedReachable(
759 A, *CurFromI, Ret, ExclusionSet);
760 LLVM_DEBUG(dbgs() << "[AA][Ret] " << *CurFromI << " "
761 << (Result ? "can potentially " : "cannot ") << "reach "
762 << Ret << " [Intra]\n");
763 return !Result;
764 };
765
766 // Check if we can reach returns.
767 bool UsedAssumedInformation = false;
768 if (A.checkForAllInstructions(ReturnInstCB, FromFn, &QueryingAA,
769 {Instruction::Ret}, UsedAssumedInformation)) {
770 LLVM_DEBUG(dbgs() << "[AA] No return is reachable, done\n");
771 continue;
772 }
773
774 if (!GoBackwardsCB) {
775 LLVM_DEBUG(dbgs() << "[AA] check @" << ToFn.getName() << " from " << FromI
776 << " is not checked backwards, abort\n");
777 return true;
778 }
779
780 // If we do not go backwards from the FromFn we are done here and so far we
781 // could not find a way to reach ToFn/ToI.
782 if (!GoBackwardsCB(*FromFn))
783 continue;
784
785 LLVM_DEBUG(dbgs() << "Stepping backwards to the call sites of @"
786 << FromFn->getName() << "\n");
787
788 auto CheckCallSite = [&](AbstractCallSite ACS) {
789 CallBase *CB = ACS.getInstruction();
790 if (!CB)
791 return false;
792
793 if (isa<InvokeInst>(CB))
794 return false;
795
796 Instruction *Inst = CB->getNextNode();
797 Worklist.push_back(Inst);
798 return true;
799 };
800
801 Result = !A.checkForAllCallSites(CheckCallSite, *FromFn,
802 /* RequireAllCallSites */ true,
803 &QueryingAA, UsedAssumedInformation);
804 if (Result) {
805 LLVM_DEBUG(dbgs() << "[AA] stepping back to call sites from " << *CurFromI
806 << " in @" << FromFn->getName()
807 << " failed, give up\n");
808 return true;
809 }
810
811 LLVM_DEBUG(dbgs() << "[AA] stepped back to call sites from " << *CurFromI
812 << " in @" << FromFn->getName()
813 << " worklist size is: " << Worklist.size() << "\n");
814 }
815 return false;
816}
817
819 Attributor &A, const Instruction &FromI, const Instruction &ToI,
820 const AbstractAttribute &QueryingAA,
821 const AA::InstExclusionSetTy *ExclusionSet,
822 std::function<bool(const Function &F)> GoBackwardsCB) {
823 const Function *ToFn = ToI.getFunction();
824 return ::isPotentiallyReachable(A, FromI, &ToI, *ToFn, QueryingAA,
825 ExclusionSet, GoBackwardsCB);
826}
827
829 Attributor &A, const Instruction &FromI, const Function &ToFn,
830 const AbstractAttribute &QueryingAA,
831 const AA::InstExclusionSetTy *ExclusionSet,
832 std::function<bool(const Function &F)> GoBackwardsCB) {
833 return ::isPotentiallyReachable(A, FromI, /* ToI */ nullptr, ToFn, QueryingAA,
834 ExclusionSet, GoBackwardsCB);
835}
836
838 const AbstractAttribute &QueryingAA) {
839 if (isa<UndefValue>(Obj))
840 return true;
841 if (isa<AllocaInst>(Obj)) {
842 InformationCache &InfoCache = A.getInfoCache();
843 if (!InfoCache.stackIsAccessibleByOtherThreads()) {
845 dbgs() << "[AA] Object '" << Obj
846 << "' is thread local; stack objects are thread local.\n");
847 return true;
848 }
849 bool IsKnownNoCapture;
850 bool IsAssumedNoCapture = AA::hasAssumedIRAttr<Attribute::Captures>(
851 A, &QueryingAA, IRPosition::value(Obj), DepClassTy::OPTIONAL,
852 IsKnownNoCapture);
853 LLVM_DEBUG(dbgs() << "[AA] Object '" << Obj << "' is "
854 << (IsAssumedNoCapture ? "" : "not") << " thread local; "
855 << (IsAssumedNoCapture ? "non-" : "")
856 << "captured stack object.\n");
857 return IsAssumedNoCapture;
858 }
859 if (auto *GV = dyn_cast<GlobalVariable>(&Obj)) {
860 if (GV->isConstant()) {
861 LLVM_DEBUG(dbgs() << "[AA] Object '" << Obj
862 << "' is thread local; constant global\n");
863 return true;
864 }
865 if (GV->isThreadLocal()) {
866 LLVM_DEBUG(dbgs() << "[AA] Object '" << Obj
867 << "' is thread local; thread local global\n");
868 return true;
869 }
870 }
871
872 if (A.getInfoCache().targetIsGPU()) {
873 if (Obj.getType()->getPointerAddressSpace() ==
875 LLVM_DEBUG(dbgs() << "[AA] Object '" << Obj
876 << "' is thread local; GPU local memory\n");
877 return true;
878 }
879 if (Obj.getType()->getPointerAddressSpace() ==
881 LLVM_DEBUG(dbgs() << "[AA] Object '" << Obj
882 << "' is thread local; GPU constant memory\n");
883 return true;
884 }
885 }
886
887 LLVM_DEBUG(dbgs() << "[AA] Object '" << Obj << "' is not thread local\n");
888 return false;
889}
890
892 const AbstractAttribute &QueryingAA) {
893 if (!I.mayHaveSideEffects() && !I.mayReadFromMemory())
894 return false;
895
897
898 auto AddLocationPtr = [&](std::optional<MemoryLocation> Loc) {
899 if (!Loc || !Loc->Ptr) {
901 dbgs() << "[AA] Access to unknown location; -> requires barriers\n");
902 return false;
903 }
904 Ptrs.insert(Loc->Ptr);
905 return true;
906 };
907
908 if (const MemIntrinsic *MI = dyn_cast<MemIntrinsic>(&I)) {
909 if (!AddLocationPtr(MemoryLocation::getForDest(MI)))
910 return true;
912 if (!AddLocationPtr(MemoryLocation::getForSource(MTI)))
913 return true;
914 } else if (!AddLocationPtr(MemoryLocation::getOrNone(&I)))
915 return true;
916
917 return isPotentiallyAffectedByBarrier(A, Ptrs.getArrayRef(), QueryingAA, &I);
918}
919
922 const AbstractAttribute &QueryingAA,
923 const Instruction *CtxI) {
924 for (const Value *Ptr : Ptrs) {
925 if (!Ptr) {
926 LLVM_DEBUG(dbgs() << "[AA] nullptr; -> requires barriers\n");
927 return true;
928 }
929
930 auto Pred = [&](Value &Obj) {
931 if (AA::isAssumedThreadLocalObject(A, Obj, QueryingAA))
932 return true;
933 LLVM_DEBUG(dbgs() << "[AA] Access to '" << Obj << "' via '" << *Ptr
934 << "'; -> requires barrier\n");
935 return false;
936 };
937
938 const auto *UnderlyingObjsAA = A.getAAFor<AAUnderlyingObjects>(
940 if (!UnderlyingObjsAA || !UnderlyingObjsAA->forallUnderlyingObjects(Pred))
941 return true;
942 }
943 return false;
944}
945
946/// Return true if \p New is equal or worse than \p Old.
947static bool isEqualOrWorse(const Attribute &New, const Attribute &Old) {
948 if (!Old.isIntAttribute())
949 return true;
950
951 return Old.getValueAsInt() >= New.getValueAsInt();
952}
953
954/// Return true if the information provided by \p Attr was added to the
955/// attribute set \p AttrSet. This is only the case if it was not already
956/// present in \p AttrSet.
957static bool addIfNotExistent(LLVMContext &Ctx, const Attribute &Attr,
958 AttributeSet AttrSet, bool ForceReplace,
959 AttrBuilder &AB) {
960
961 if (Attr.isEnumAttribute()) {
963 if (AttrSet.hasAttribute(Kind))
964 return false;
965 AB.addAttribute(Kind);
966 return true;
967 }
968 if (Attr.isStringAttribute()) {
969 StringRef Kind = Attr.getKindAsString();
970 if (AttrSet.hasAttribute(Kind)) {
971 if (!ForceReplace)
972 return false;
973 }
974 AB.addAttribute(Kind, Attr.getValueAsString());
975 return true;
976 }
977 if (Attr.isIntAttribute()) {
979 if (!ForceReplace && Kind == Attribute::Memory) {
980 MemoryEffects ME = Attr.getMemoryEffects() & AttrSet.getMemoryEffects();
981 if (ME == AttrSet.getMemoryEffects())
982 return false;
983 AB.addMemoryAttr(ME);
984 return true;
985 }
986 if (AttrSet.hasAttribute(Kind)) {
987 if (!ForceReplace && isEqualOrWorse(Attr, AttrSet.getAttribute(Kind)))
988 return false;
989 }
990 AB.addAttribute(Attr);
991 return true;
992 }
993 if (Attr.isConstantRangeAttribute()) {
995 if (!ForceReplace && AttrSet.hasAttribute(Kind))
996 return false;
997 AB.addAttribute(Attr);
998 return true;
999 }
1000
1001 llvm_unreachable("Expected enum or string attribute!");
1002}
1003
1006 return cast<Argument>(&getAnchorValue());
1007
1008 // Not an Argument and no argument number means this is not a call site
1009 // argument, thus we cannot find a callback argument to return.
1010 int ArgNo = getCallSiteArgNo();
1011 if (ArgNo < 0)
1012 return nullptr;
1013
1014 // Use abstract call sites to make the connection between the call site
1015 // values and the ones in callbacks. If a callback was found that makes use
1016 // of the underlying call site operand, we want the corresponding callback
1017 // callee argument and not the direct callee argument.
1018 std::optional<Argument *> CBCandidateArg;
1019 SmallVector<const Use *, 4> CallbackUses;
1020 const auto &CB = cast<CallBase>(getAnchorValue());
1021 AbstractCallSite::getCallbackUses(CB, CallbackUses);
1022 for (const Use *U : CallbackUses) {
1023 AbstractCallSite ACS(U);
1024 assert(ACS && ACS.isCallbackCall());
1025 if (!ACS.getCalledFunction())
1026 continue;
1027
1028 for (unsigned u = 0, e = ACS.getNumArgOperands(); u < e; u++) {
1029
1030 // Test if the underlying call site operand is argument number u of the
1031 // callback callee.
1032 if (ACS.getCallArgOperandNo(u) != ArgNo)
1033 continue;
1034
1035 assert(ACS.getCalledFunction()->arg_size() > u &&
1036 "ACS mapped into var-args arguments!");
1037 if (CBCandidateArg) {
1038 CBCandidateArg = nullptr;
1039 break;
1040 }
1041 CBCandidateArg = ACS.getCalledFunction()->getArg(u);
1042 }
1043 }
1044
1045 // If we found a unique callback candidate argument, return it.
1046 if (CBCandidateArg && *CBCandidateArg)
1047 return *CBCandidateArg;
1048
1049 // If no callbacks were found, or none used the underlying call site operand
1050 // exclusively, use the direct callee argument if available.
1051 auto *Callee = dyn_cast_if_present<Function>(CB.getCalledOperand());
1052 if (Callee && Callee->arg_size() > unsigned(ArgNo))
1053 return Callee->getArg(ArgNo);
1054
1055 return nullptr;
1056}
1057
1060 if (getState().isAtFixpoint())
1061 return HasChanged;
1062
1063 LLVM_DEBUG(dbgs() << "[Attributor] Update: " << *this << "\n");
1064
1065 HasChanged = updateImpl(A);
1066
1067 LLVM_DEBUG(dbgs() << "[Attributor] Update " << HasChanged << " " << *this
1068 << "\n");
1069
1070 return HasChanged;
1071}
1072
1074 InformationCache &InfoCache,
1075 AttributorConfig Configuration)
1076 : Allocator(InfoCache.Allocator), Functions(Functions),
1077 InfoCache(InfoCache), Configuration(Configuration) {
1078 if (!isClosedWorldModule())
1079 return;
1080 for (Function *Fn : Functions)
1081 if (Fn->hasAddressTaken(/*PutOffender=*/nullptr,
1082 /*IgnoreCallbackUses=*/false,
1083 /*IgnoreAssumeLikeCalls=*/true,
1084 /*IgnoreLLVMUsed=*/true,
1085 /*IgnoreARCAttachedCall=*/false,
1086 /*IgnoreCastedDirectCall=*/true))
1087 InfoCache.IndirectlyCallableFunctions.push_back(Fn);
1088}
1089
1094 "Did expect a valid position!");
1097 if (!Explorer)
1098 return false;
1099
1100 Value &AssociatedValue = IRP.getAssociatedValue();
1101
1102 const Assume2KnowledgeMap &A2K =
1103 getInfoCache().getKnowledgeMap().lookup({&AssociatedValue, AK});
1104
1105 // Check if we found any potential assume use, if not we don't need to create
1106 // explorer iterators.
1107 if (A2K.empty())
1108 return false;
1109
1110 LLVMContext &Ctx = AssociatedValue.getContext();
1111 unsigned AttrsSize = Attrs.size();
1112 auto EIt = Explorer->begin(IRP.getCtxI()),
1113 EEnd = Explorer->end(IRP.getCtxI());
1114 for (const auto &It : A2K)
1115 if (Explorer->findInContextOf(It.first, EIt, EEnd))
1116 Attrs.push_back(Attribute::get(Ctx, AK, It.second.Max));
1117 return AttrsSize != Attrs.size();
1118}
1119
1120template <typename DescTy>
1122Attributor::updateAttrMap(const IRPosition &IRP, ArrayRef<DescTy> AttrDescs,
1123 function_ref<bool(const DescTy &, AttributeSet,
1124 AttributeMask &, AttrBuilder &)>
1125 CB) {
1126 if (AttrDescs.empty())
1128 switch (IRP.getPositionKind()) {
1132 default:
1133 break;
1134 };
1135
1136 AttributeList AL;
1137 Value *AttrListAnchor = IRP.getAttrListAnchor();
1138 auto It = AttrsMap.find(AttrListAnchor);
1139 if (It == AttrsMap.end())
1140 AL = IRP.getAttrList();
1141 else
1142 AL = It->getSecond();
1143
1144 LLVMContext &Ctx = IRP.getAnchorValue().getContext();
1145 auto AttrIdx = IRP.getAttrIdx();
1146 AttributeSet AS = AL.getAttributes(AttrIdx);
1147 AttributeMask AM;
1148 AttrBuilder AB(Ctx);
1149
1151 for (const DescTy &AttrDesc : AttrDescs)
1152 if (CB(AttrDesc, AS, AM, AB))
1153 HasChanged = ChangeStatus::CHANGED;
1154
1155 if (HasChanged == ChangeStatus::UNCHANGED)
1157
1158 AL = AL.removeAttributesAtIndex(Ctx, AttrIdx, AM);
1159 AL = AL.addAttributesAtIndex(Ctx, AttrIdx, AB);
1160 AttrsMap[AttrListAnchor] = AL;
1161 return ChangeStatus::CHANGED;
1162}
1163
1166 bool IgnoreSubsumingPositions,
1167 Attribute::AttrKind ImpliedAttributeKind) {
1168 bool Implied = false;
1169 bool HasAttr = false;
1170 auto HasAttrCB = [&](const Attribute::AttrKind &Kind, AttributeSet AttrSet,
1171 AttributeMask &, AttrBuilder &) {
1172 if (AttrSet.hasAttribute(Kind)) {
1173 Implied |= Kind != ImpliedAttributeKind;
1174 HasAttr = true;
1175 }
1176 return false;
1177 };
1178 for (const IRPosition &EquivIRP : SubsumingPositionIterator(IRP)) {
1179 updateAttrMap<Attribute::AttrKind>(EquivIRP, AttrKinds, HasAttrCB);
1180 if (HasAttr)
1181 break;
1182 // The first position returned by the SubsumingPositionIterator is
1183 // always the position itself. If we ignore subsuming positions we
1184 // are done after the first iteration.
1185 if (IgnoreSubsumingPositions)
1186 break;
1187 Implied = true;
1188 }
1189 if (!HasAttr) {
1190 Implied = true;
1192 for (Attribute::AttrKind AK : AttrKinds)
1193 if (getAttrsFromAssumes(IRP, AK, Attrs)) {
1194 HasAttr = true;
1195 break;
1196 }
1197 }
1198
1199 // Check if we should manifest the implied attribute kind at the IRP.
1200 if (ImpliedAttributeKind != Attribute::None && HasAttr && Implied)
1202 ImpliedAttributeKind)});
1203 return HasAttr;
1204}
1205
1209 bool IgnoreSubsumingPositions) {
1210 auto CollectAttrCB = [&](const Attribute::AttrKind &Kind,
1211 AttributeSet AttrSet, AttributeMask &,
1212 AttrBuilder &) {
1213 if (AttrSet.hasAttribute(Kind))
1214 Attrs.push_back(AttrSet.getAttribute(Kind));
1215 return false;
1216 };
1217 for (const IRPosition &EquivIRP : SubsumingPositionIterator(IRP)) {
1218 updateAttrMap<Attribute::AttrKind>(EquivIRP, AttrKinds, CollectAttrCB);
1219 // The first position returned by the SubsumingPositionIterator is
1220 // always the position itself. If we ignore subsuming positions we
1221 // are done after the first iteration.
1222 if (IgnoreSubsumingPositions)
1223 break;
1224 }
1225 for (Attribute::AttrKind AK : AttrKinds)
1226 getAttrsFromAssumes(IRP, AK, Attrs);
1227}
1228
1231 auto RemoveAttrCB = [&](const Attribute::AttrKind &Kind, AttributeSet AttrSet,
1232 AttributeMask &AM, AttrBuilder &) {
1233 if (!AttrSet.hasAttribute(Kind))
1234 return false;
1235 AM.addAttribute(Kind);
1236 return true;
1237 };
1238 return updateAttrMap<Attribute::AttrKind>(IRP, AttrKinds, RemoveAttrCB);
1239}
1240
1242 ArrayRef<StringRef> Attrs) {
1243 auto RemoveAttrCB = [&](StringRef Attr, AttributeSet AttrSet,
1244 AttributeMask &AM, AttrBuilder &) -> bool {
1245 if (!AttrSet.hasAttribute(Attr))
1246 return false;
1247 AM.addAttribute(Attr);
1248 return true;
1249 };
1250
1251 return updateAttrMap<StringRef>(IRP, Attrs, RemoveAttrCB);
1252}
1253
1255 ArrayRef<Attribute> Attrs,
1256 bool ForceReplace) {
1257 LLVMContext &Ctx = IRP.getAnchorValue().getContext();
1258 auto AddAttrCB = [&](const Attribute &Attr, AttributeSet AttrSet,
1259 AttributeMask &, AttrBuilder &AB) {
1260 return addIfNotExistent(Ctx, Attr, AttrSet, ForceReplace, AB);
1261 };
1262 return updateAttrMap<Attribute>(IRP, Attrs, AddAttrCB);
1263}
1264
1266const IRPosition
1268
1270 IRPositions.emplace_back(IRP);
1271
1272 // Helper to determine if operand bundles on a call site are benign or
1273 // potentially problematic. We handle only llvm.assume for now.
1274 auto CanIgnoreOperandBundles = [](const CallBase &CB) {
1275 return (isa<IntrinsicInst>(CB) &&
1276 cast<IntrinsicInst>(CB).getIntrinsicID() == Intrinsic ::assume);
1277 };
1278
1279 const auto *CB = dyn_cast<CallBase>(&IRP.getAnchorValue());
1280 switch (IRP.getPositionKind()) {
1284 return;
1287 IRPositions.emplace_back(IRPosition::function(*IRP.getAnchorScope()));
1288 return;
1290 assert(CB && "Expected call site!");
1291 // TODO: We need to look at the operand bundles similar to the redirection
1292 // in CallBase.
1293 if (!CB->hasOperandBundles() || CanIgnoreOperandBundles(*CB))
1294 if (auto *Callee = dyn_cast_if_present<Function>(CB->getCalledOperand()))
1295 IRPositions.emplace_back(IRPosition::function(*Callee));
1296 return;
1298 assert(CB && "Expected call site!");
1299 // TODO: We need to look at the operand bundles similar to the redirection
1300 // in CallBase.
1301 if (!CB->hasOperandBundles() || CanIgnoreOperandBundles(*CB)) {
1302 if (auto *Callee =
1303 dyn_cast_if_present<Function>(CB->getCalledOperand())) {
1304 IRPositions.emplace_back(IRPosition::returned(*Callee));
1305 IRPositions.emplace_back(IRPosition::function(*Callee));
1306 for (const Argument &Arg : Callee->args())
1307 if (Arg.hasReturnedAttr()) {
1308 IRPositions.emplace_back(
1309 IRPosition::callsite_argument(*CB, Arg.getArgNo()));
1310 IRPositions.emplace_back(
1311 IRPosition::value(*CB->getArgOperand(Arg.getArgNo())));
1312 IRPositions.emplace_back(IRPosition::argument(Arg));
1313 }
1314 }
1315 }
1316 IRPositions.emplace_back(IRPosition::callsite_function(*CB));
1317 return;
1319 assert(CB && "Expected call site!");
1320 // TODO: We need to look at the operand bundles similar to the redirection
1321 // in CallBase.
1322 if (!CB->hasOperandBundles() || CanIgnoreOperandBundles(*CB)) {
1323 auto *Callee = dyn_cast_if_present<Function>(CB->getCalledOperand());
1324 if (Callee) {
1325 if (Argument *Arg = IRP.getAssociatedArgument())
1326 IRPositions.emplace_back(IRPosition::argument(*Arg));
1327 IRPositions.emplace_back(IRPosition::function(*Callee));
1328 }
1329 }
1330 IRPositions.emplace_back(IRPosition::value(IRP.getAssociatedValue()));
1331 return;
1332 }
1333 }
1334}
1335
1336void IRPosition::verify() {
1337#ifdef EXPENSIVE_CHECKS
1338 switch (getPositionKind()) {
1339 case IRP_INVALID:
1340 assert((CBContext == nullptr) &&
1341 "Invalid position must not have CallBaseContext!");
1342 assert(!Enc.getOpaqueValue() &&
1343 "Expected a nullptr for an invalid position!");
1344 return;
1345 case IRP_FLOAT:
1347 "Expected specialized kind for argument values!");
1348 return;
1349 case IRP_RETURNED:
1350 assert(isa<Function>(getAsValuePtr()) &&
1351 "Expected function for a 'returned' position!");
1352 assert(getAsValuePtr() == &getAssociatedValue() &&
1353 "Associated value mismatch!");
1354 return;
1356 assert((CBContext == nullptr) &&
1357 "'call site returned' position must not have CallBaseContext!");
1358 assert((isa<CallBase>(getAsValuePtr())) &&
1359 "Expected call base for 'call site returned' position!");
1360 assert(getAsValuePtr() == &getAssociatedValue() &&
1361 "Associated value mismatch!");
1362 return;
1363 case IRP_CALL_SITE:
1364 assert((CBContext == nullptr) &&
1365 "'call site function' position must not have CallBaseContext!");
1366 assert((isa<CallBase>(getAsValuePtr())) &&
1367 "Expected call base for 'call site function' position!");
1368 assert(getAsValuePtr() == &getAssociatedValue() &&
1369 "Associated value mismatch!");
1370 return;
1371 case IRP_FUNCTION:
1372 assert(isa<Function>(getAsValuePtr()) &&
1373 "Expected function for a 'function' position!");
1374 assert(getAsValuePtr() == &getAssociatedValue() &&
1375 "Associated value mismatch!");
1376 return;
1377 case IRP_ARGUMENT:
1378 assert(isa<Argument>(getAsValuePtr()) &&
1379 "Expected argument for a 'argument' position!");
1380 assert(getAsValuePtr() == &getAssociatedValue() &&
1381 "Associated value mismatch!");
1382 return;
1384 assert((CBContext == nullptr) &&
1385 "'call site argument' position must not have CallBaseContext!");
1386 Use *U = getAsUsePtr();
1387 (void)U; // Silence unused variable warning.
1388 assert(U && "Expected use for a 'call site argument' position!");
1389 assert(isa<CallBase>(U->getUser()) &&
1390 "Expected call base user for a 'call site argument' position!");
1391 assert(cast<CallBase>(U->getUser())->isArgOperand(U) &&
1392 "Expected call base argument operand for a 'call site argument' "
1393 "position");
1394 assert(cast<CallBase>(U->getUser())->getArgOperandNo(U) ==
1395 unsigned(getCallSiteArgNo()) &&
1396 "Argument number mismatch!");
1397 assert(U->get() == &getAssociatedValue() && "Associated value mismatch!");
1398 return;
1399 }
1400 }
1401#endif
1402}
1403
1404std::optional<Constant *>
1406 const AbstractAttribute &AA,
1407 bool &UsedAssumedInformation) {
1408 // First check all callbacks provided by outside AAs. If any of them returns
1409 // a non-null value that is different from the associated value, or
1410 // std::nullopt, we assume it's simplified.
1411 for (auto &CB : SimplificationCallbacks.lookup(IRP)) {
1412 std::optional<Value *> SimplifiedV = CB(IRP, &AA, UsedAssumedInformation);
1413 if (!SimplifiedV)
1414 return std::nullopt;
1415 if (isa_and_nonnull<Constant>(*SimplifiedV))
1416 return cast<Constant>(*SimplifiedV);
1417 return nullptr;
1418 }
1419 if (auto *C = dyn_cast<Constant>(&IRP.getAssociatedValue()))
1420 return C;
1422 if (getAssumedSimplifiedValues(IRP, &AA, Values,
1424 UsedAssumedInformation)) {
1425 if (Values.empty())
1426 return std::nullopt;
1427 if (auto *C = dyn_cast_or_null<Constant>(
1428 AAPotentialValues::getSingleValue(*this, AA, IRP, Values)))
1429 return C;
1430 }
1431 return nullptr;
1432}
1433
1435 const IRPosition &IRP, const AbstractAttribute *AA,
1436 bool &UsedAssumedInformation, AA::ValueScope S) {
1437 // First check all callbacks provided by outside AAs. If any of them returns
1438 // a non-null value that is different from the associated value, or
1439 // std::nullopt, we assume it's simplified.
1440 for (auto &CB : SimplificationCallbacks.lookup(IRP))
1441 return CB(IRP, AA, UsedAssumedInformation);
1442
1444 if (!getAssumedSimplifiedValues(IRP, AA, Values, S, UsedAssumedInformation))
1445 return &IRP.getAssociatedValue();
1446 if (Values.empty())
1447 return std::nullopt;
1448 if (AA)
1449 if (Value *V = AAPotentialValues::getSingleValue(*this, *AA, IRP, Values))
1450 return V;
1453 return nullptr;
1454 return &IRP.getAssociatedValue();
1455}
1456
1458 const IRPosition &InitialIRP, const AbstractAttribute *AA,
1460 bool &UsedAssumedInformation, bool RecurseForSelectAndPHI) {
1463 Worklist.push_back(InitialIRP);
1464 while (!Worklist.empty()) {
1465 const IRPosition &IRP = Worklist.pop_back_val();
1466
1467 // First check all callbacks provided by outside AAs. If any of them returns
1468 // a non-null value that is different from the associated value, or
1469 // std::nullopt, we assume it's simplified.
1470 int NV = Values.size();
1471 const auto &SimplificationCBs = SimplificationCallbacks.lookup(IRP);
1472 for (const auto &CB : SimplificationCBs) {
1473 std::optional<Value *> CBResult = CB(IRP, AA, UsedAssumedInformation);
1474 if (!CBResult.has_value())
1475 continue;
1476 Value *V = *CBResult;
1477 if (!V)
1478 return false;
1481 Values.push_back(AA::ValueAndContext{*V, nullptr});
1482 else
1483 return false;
1484 }
1485 if (SimplificationCBs.empty()) {
1486 // If no high-level/outside simplification occurred, use
1487 // AAPotentialValues.
1488 const auto *PotentialValuesAA =
1490 if (PotentialValuesAA &&
1491 PotentialValuesAA->getAssumedSimplifiedValues(*this, Values, S)) {
1492 UsedAssumedInformation |= !PotentialValuesAA->isAtFixpoint();
1493 } else if (IRP.getPositionKind() != IRPosition::IRP_RETURNED) {
1494 Values.push_back({IRP.getAssociatedValue(), IRP.getCtxI()});
1495 } else {
1496 // TODO: We could visit all returns and add the operands.
1497 return false;
1498 }
1499 }
1500
1501 if (!RecurseForSelectAndPHI)
1502 break;
1503
1504 for (int I = NV, E = Values.size(); I < E; ++I) {
1505 Value *V = Values[I].getValue();
1506 if (!isa<PHINode>(V) && !isa<SelectInst>(V))
1507 continue;
1508 if (!Seen.insert(V).second)
1509 continue;
1510 // Move the last element to this slot.
1511 Values[I] = Values[E - 1];
1512 // Eliminate the last slot, adjust the indices.
1513 Values.pop_back();
1514 --E;
1515 --I;
1516 // Add a new value (select or phi) to the worklist.
1517 Worklist.push_back(IRPosition::value(*V));
1518 }
1519 }
1520 return true;
1521}
1522
1524 std::optional<Value *> V, CallBase &CB, const AbstractAttribute &AA,
1525 bool &UsedAssumedInformation) {
1526 if (!V)
1527 return V;
1528 if (*V == nullptr || isa<Constant>(*V))
1529 return V;
1530 if (auto *Arg = dyn_cast<Argument>(*V))
1531 if (CB.getCalledOperand() == Arg->getParent() &&
1532 CB.arg_size() > Arg->getArgNo())
1533 if (!Arg->hasPointeeInMemoryValueAttr())
1534 return getAssumedSimplified(
1535 IRPosition::callsite_argument(CB, Arg->getArgNo()), AA,
1536 UsedAssumedInformation, AA::Intraprocedural);
1537 return nullptr;
1538}
1539
1541 // The abstract attributes are allocated via the BumpPtrAllocator Allocator,
1542 // thus we cannot delete them. We can, and want to, destruct them though.
1543 for (auto &It : AAMap) {
1544 AbstractAttribute *AA = It.getSecond();
1545 AA->~AbstractAttribute();
1546 }
1547}
1548
1550 const AAIsDead *FnLivenessAA,
1551 bool &UsedAssumedInformation,
1552 bool CheckBBLivenessOnly, DepClassTy DepClass) {
1553 if (!Configuration.UseLiveness)
1554 return false;
1555 const IRPosition &IRP = AA.getIRPosition();
1556 if (!Functions.count(IRP.getAnchorScope()))
1557 return false;
1558 return isAssumedDead(IRP, &AA, FnLivenessAA, UsedAssumedInformation,
1559 CheckBBLivenessOnly, DepClass);
1560}
1561
1563 const AbstractAttribute *QueryingAA,
1564 const AAIsDead *FnLivenessAA,
1565 bool &UsedAssumedInformation,
1566 bool CheckBBLivenessOnly, DepClassTy DepClass) {
1567 if (!Configuration.UseLiveness)
1568 return false;
1569 Instruction *UserI = dyn_cast<Instruction>(U.getUser());
1570 if (!UserI)
1571 return isAssumedDead(IRPosition::value(*U.get()), QueryingAA, FnLivenessAA,
1572 UsedAssumedInformation, CheckBBLivenessOnly, DepClass);
1573
1574 if (auto *CB = dyn_cast<CallBase>(UserI)) {
1575 // For call site argument uses we can check if the argument is
1576 // unused/dead.
1577 if (CB->isArgOperand(&U)) {
1578 const IRPosition &CSArgPos =
1579 IRPosition::callsite_argument(*CB, CB->getArgOperandNo(&U));
1580 return isAssumedDead(CSArgPos, QueryingAA, FnLivenessAA,
1581 UsedAssumedInformation, CheckBBLivenessOnly,
1582 DepClass);
1583 }
1584 } else if (ReturnInst *RI = dyn_cast<ReturnInst>(UserI)) {
1585 const IRPosition &RetPos = IRPosition::returned(*RI->getFunction());
1586 return isAssumedDead(RetPos, QueryingAA, FnLivenessAA,
1587 UsedAssumedInformation, CheckBBLivenessOnly, DepClass);
1588 } else if (PHINode *PHI = dyn_cast<PHINode>(UserI)) {
1589 BasicBlock *IncomingBB = PHI->getIncomingBlock(U);
1590 return isAssumedDead(*IncomingBB->getTerminator(), QueryingAA, FnLivenessAA,
1591 UsedAssumedInformation, CheckBBLivenessOnly, DepClass);
1592 } else if (StoreInst *SI = dyn_cast<StoreInst>(UserI)) {
1593 if (!CheckBBLivenessOnly && SI->getPointerOperand() != U.get()) {
1594 const IRPosition IRP = IRPosition::inst(*SI);
1595 const AAIsDead *IsDeadAA =
1597 if (IsDeadAA && IsDeadAA->isRemovableStore()) {
1598 if (QueryingAA)
1599 recordDependence(*IsDeadAA, *QueryingAA, DepClass);
1600 if (!IsDeadAA->isKnown(AAIsDead::IS_REMOVABLE))
1601 UsedAssumedInformation = true;
1602 return true;
1603 }
1604 }
1605 }
1606
1607 return isAssumedDead(IRPosition::inst(*UserI), QueryingAA, FnLivenessAA,
1608 UsedAssumedInformation, CheckBBLivenessOnly, DepClass);
1609}
1610
1612 const AbstractAttribute *QueryingAA,
1613 const AAIsDead *FnLivenessAA,
1614 bool &UsedAssumedInformation,
1615 bool CheckBBLivenessOnly, DepClassTy DepClass,
1616 bool CheckForDeadStore) {
1617 if (!Configuration.UseLiveness)
1618 return false;
1619 const IRPosition::CallBaseContext *CBCtx =
1620 QueryingAA ? QueryingAA->getCallBaseContext() : nullptr;
1621
1622 if (ManifestAddedBlocks.contains(I.getParent()))
1623 return false;
1624
1625 const Function &F = *I.getFunction();
1626 if (!FnLivenessAA || FnLivenessAA->getAnchorScope() != &F)
1627 FnLivenessAA = getOrCreateAAFor<AAIsDead>(IRPosition::function(F, CBCtx),
1628 QueryingAA, DepClassTy::NONE);
1629
1630 // Don't use recursive reasoning.
1631 if (!FnLivenessAA || QueryingAA == FnLivenessAA)
1632 return false;
1633
1634 // If we have a context instruction and a liveness AA we use it.
1635 if (CheckBBLivenessOnly ? FnLivenessAA->isAssumedDead(I.getParent())
1636 : FnLivenessAA->isAssumedDead(&I)) {
1637 if (QueryingAA)
1638 recordDependence(*FnLivenessAA, *QueryingAA, DepClass);
1639 if (!FnLivenessAA->isKnownDead(&I))
1640 UsedAssumedInformation = true;
1641 return true;
1642 }
1643
1644 if (CheckBBLivenessOnly)
1645 return false;
1646
1647 const IRPosition IRP = IRPosition::inst(I, CBCtx);
1648 const AAIsDead *IsDeadAA =
1650
1651 // Don't use recursive reasoning.
1652 if (!IsDeadAA || QueryingAA == IsDeadAA)
1653 return false;
1654
1655 if (IsDeadAA->isAssumedDead()) {
1656 if (QueryingAA)
1657 recordDependence(*IsDeadAA, *QueryingAA, DepClass);
1658 if (!IsDeadAA->isKnownDead())
1659 UsedAssumedInformation = true;
1660 return true;
1661 }
1662
1663 if (CheckForDeadStore && isa<StoreInst>(I) && IsDeadAA->isRemovableStore()) {
1664 if (QueryingAA)
1665 recordDependence(*IsDeadAA, *QueryingAA, DepClass);
1666 if (!IsDeadAA->isKnownDead())
1667 UsedAssumedInformation = true;
1668 return true;
1669 }
1670
1671 return false;
1672}
1673
1675 const AbstractAttribute *QueryingAA,
1676 const AAIsDead *FnLivenessAA,
1677 bool &UsedAssumedInformation,
1678 bool CheckBBLivenessOnly, DepClassTy DepClass) {
1679 if (!Configuration.UseLiveness)
1680 return false;
1681 // Don't check liveness for constants, e.g. functions, used as (floating)
1682 // values since the context instruction and such is here meaningless.
1685 return false;
1686 }
1687
1688 Instruction *CtxI = IRP.getCtxI();
1689 if (CtxI &&
1690 isAssumedDead(*CtxI, QueryingAA, FnLivenessAA, UsedAssumedInformation,
1691 /* CheckBBLivenessOnly */ true,
1692 CheckBBLivenessOnly ? DepClass : DepClassTy::OPTIONAL))
1693 return true;
1694
1695 if (CheckBBLivenessOnly)
1696 return false;
1697
1698 // If we haven't succeeded we query the specific liveness info for the IRP.
1699 const AAIsDead *IsDeadAA;
1701 IsDeadAA = getOrCreateAAFor<AAIsDead>(
1703 QueryingAA, DepClassTy::NONE);
1704 else
1705 IsDeadAA = getOrCreateAAFor<AAIsDead>(IRP, QueryingAA, DepClassTy::NONE);
1706
1707 // Don't use recursive reasoning.
1708 if (!IsDeadAA || QueryingAA == IsDeadAA)
1709 return false;
1710
1711 if (IsDeadAA->isAssumedDead()) {
1712 if (QueryingAA)
1713 recordDependence(*IsDeadAA, *QueryingAA, DepClass);
1714 if (!IsDeadAA->isKnownDead())
1715 UsedAssumedInformation = true;
1716 return true;
1717 }
1718
1719 return false;
1720}
1721
1723 const AbstractAttribute *QueryingAA,
1724 const AAIsDead *FnLivenessAA,
1725 DepClassTy DepClass) {
1726 if (!Configuration.UseLiveness)
1727 return false;
1728 const Function &F = *BB.getParent();
1729 if (!FnLivenessAA || FnLivenessAA->getAnchorScope() != &F)
1731 QueryingAA, DepClassTy::NONE);
1732
1733 // Don't use recursive reasoning.
1734 if (!FnLivenessAA || QueryingAA == FnLivenessAA)
1735 return false;
1736
1737 if (FnLivenessAA->isAssumedDead(&BB)) {
1738 if (QueryingAA)
1739 recordDependence(*FnLivenessAA, *QueryingAA, DepClass);
1740 return true;
1741 }
1742
1743 return false;
1744}
1745
1748 const AbstractAttribute &QueryingAA, const CallBase &CB) {
1749 if (const Function *Callee = dyn_cast<Function>(CB.getCalledOperand()))
1750 return Pred(Callee);
1751
1752 const auto *CallEdgesAA = getAAFor<AACallEdges>(
1754 if (!CallEdgesAA || CallEdgesAA->hasUnknownCallee())
1755 return false;
1756
1757 const auto &Callees = CallEdgesAA->getOptimisticEdges();
1758 return Pred(Callees.getArrayRef());
1759}
1760
1761bool canMarkAsVisited(const User *Usr) {
1762 return isa<PHINode>(Usr) || !isa<Instruction>(Usr);
1763}
1764
1766 function_ref<bool(const Use &, bool &)> Pred,
1767 const AbstractAttribute &QueryingAA, const Value &V,
1768 bool CheckBBLivenessOnly, DepClassTy LivenessDepClass,
1769 bool IgnoreDroppableUses,
1770 function_ref<bool(const Use &OldU, const Use &NewU)> EquivalentUseCB) {
1771
1772 // Check virtual uses first.
1773 for (VirtualUseCallbackTy &CB : VirtualUseCallbacks.lookup(&V))
1774 if (!CB(*this, &QueryingAA))
1775 return false;
1776
1777 if (isa<ConstantData>(V))
1778 return false;
1779
1780 // Check the trivial case first as it catches void values.
1781 if (V.use_empty())
1782 return true;
1783
1784 const IRPosition &IRP = QueryingAA.getIRPosition();
1787
1788 auto AddUsers = [&](const Value &V, const Use *OldUse) {
1789 for (const Use &UU : V.uses()) {
1790 if (OldUse && EquivalentUseCB && !EquivalentUseCB(*OldUse, UU)) {
1791 LLVM_DEBUG(dbgs() << "[Attributor] Potential copy was "
1792 "rejected by the equivalence call back: "
1793 << *UU << "!\n");
1794 return false;
1795 }
1796
1797 Worklist.push_back(&UU);
1798 }
1799 return true;
1800 };
1801
1802 AddUsers(V, /* OldUse */ nullptr);
1803
1804 LLVM_DEBUG(dbgs() << "[Attributor] Got " << Worklist.size()
1805 << " initial uses to check\n");
1806
1807 const Function *ScopeFn = IRP.getAnchorScope();
1808 const auto *LivenessAA =
1809 ScopeFn ? getAAFor<AAIsDead>(QueryingAA, IRPosition::function(*ScopeFn),
1811 : nullptr;
1812
1813 while (!Worklist.empty()) {
1814 const Use *U = Worklist.pop_back_val();
1815 if (canMarkAsVisited(U->getUser()) && !Visited.insert(U).second)
1816 continue;
1818 if (auto *Fn = dyn_cast<Function>(U->getUser()))
1819 dbgs() << "[Attributor] Check use: " << **U << " in " << Fn->getName()
1820 << "\n";
1821 else
1822 dbgs() << "[Attributor] Check use: " << **U << " in " << *U->getUser()
1823 << "\n";
1824 });
1825 bool UsedAssumedInformation = false;
1826 if (isAssumedDead(*U, &QueryingAA, LivenessAA, UsedAssumedInformation,
1827 CheckBBLivenessOnly, LivenessDepClass)) {
1829 dbgs() << "[Attributor] Dead use, skip!\n");
1830 continue;
1831 }
1832 if (IgnoreDroppableUses && U->getUser()->isDroppable()) {
1834 dbgs() << "[Attributor] Droppable user, skip!\n");
1835 continue;
1836 }
1837
1838 if (auto *SI = dyn_cast<StoreInst>(U->getUser())) {
1839 if (&SI->getOperandUse(0) == U) {
1840 if (!Visited.insert(U).second)
1841 continue;
1842 SmallSetVector<Value *, 4> PotentialCopies;
1844 *this, *SI, PotentialCopies, QueryingAA, UsedAssumedInformation,
1845 /* OnlyExact */ true)) {
1847 dbgs()
1848 << "[Attributor] Value is stored, continue with "
1849 << PotentialCopies.size()
1850 << " potential copies instead!\n");
1851 for (Value *PotentialCopy : PotentialCopies)
1852 if (!AddUsers(*PotentialCopy, U))
1853 return false;
1854 continue;
1855 }
1856 }
1857 }
1858
1859 bool Follow = false;
1860 if (!Pred(*U, Follow))
1861 return false;
1862 if (!Follow)
1863 continue;
1864
1865 User &Usr = *U->getUser();
1866 AddUsers(Usr, /* OldUse */ nullptr);
1867 }
1868
1869 return true;
1870}
1871
1873 const AbstractAttribute &QueryingAA,
1874 bool RequireAllCallSites,
1875 bool &UsedAssumedInformation) {
1876 // We can try to determine information from
1877 // the call sites. However, this is only possible all call sites are known,
1878 // hence the function has internal linkage.
1879 const IRPosition &IRP = QueryingAA.getIRPosition();
1880 const Function *AssociatedFunction = IRP.getAssociatedFunction();
1881 if (!AssociatedFunction) {
1882 LLVM_DEBUG(dbgs() << "[Attributor] No function associated with " << IRP
1883 << "\n");
1884 return false;
1885 }
1886
1887 return checkForAllCallSites(Pred, *AssociatedFunction, RequireAllCallSites,
1888 &QueryingAA, UsedAssumedInformation);
1889}
1890
1892 const Function &Fn,
1893 bool RequireAllCallSites,
1894 const AbstractAttribute *QueryingAA,
1895 bool &UsedAssumedInformation,
1896 bool CheckPotentiallyDead) {
1897 if (RequireAllCallSites && !Fn.hasLocalLinkage()) {
1898 LLVM_DEBUG(
1899 dbgs()
1900 << "[Attributor] Function " << Fn.getName()
1901 << " has no internal linkage, hence not all call sites are known\n");
1902 return false;
1903 }
1904 // Check virtual uses first.
1905 for (VirtualUseCallbackTy &CB : VirtualUseCallbacks.lookup(&Fn))
1906 if (!CB(*this, QueryingAA))
1907 return false;
1908
1910 for (unsigned u = 0; u < Uses.size(); ++u) {
1911 const Use &U = *Uses[u];
1913 if (auto *Fn = dyn_cast<Function>(U))
1914 dbgs() << "[Attributor] Check use: " << Fn->getName() << " in "
1915 << *U.getUser() << "\n";
1916 else
1917 dbgs() << "[Attributor] Check use: " << *U << " in " << *U.getUser()
1918 << "\n";
1919 });
1920 if (!CheckPotentiallyDead &&
1921 isAssumedDead(U, QueryingAA, nullptr, UsedAssumedInformation,
1922 /* CheckBBLivenessOnly */ true)) {
1924 dbgs() << "[Attributor] Dead use, skip!\n");
1925 continue;
1926 }
1927 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(U.getUser())) {
1928 if (CE->isCast() && CE->getType()->isPointerTy()) {
1930 dbgs() << "[Attributor] Use, is constant cast expression, add "
1931 << CE->getNumUses() << " uses of that expression instead!\n";
1932 });
1933 for (const Use &CEU : CE->uses())
1934 Uses.push_back(&CEU);
1935 continue;
1936 }
1937 }
1938
1939 AbstractCallSite ACS(&U);
1940 if (!ACS) {
1941 LLVM_DEBUG(dbgs() << "[Attributor] Function " << Fn.getName()
1942 << " has non call site use " << *U.get() << " in "
1943 << *U.getUser() << "\n");
1944 return false;
1945 }
1946
1947 const Use *EffectiveUse =
1948 ACS.isCallbackCall() ? &ACS.getCalleeUseForCallback() : &U;
1949 if (!ACS.isCallee(EffectiveUse)) {
1950 if (!RequireAllCallSites) {
1951 LLVM_DEBUG(dbgs() << "[Attributor] User " << *EffectiveUse->getUser()
1952 << " is not a call of " << Fn.getName()
1953 << ", skip use\n");
1954 continue;
1955 }
1956 LLVM_DEBUG(dbgs() << "[Attributor] User " << *EffectiveUse->getUser()
1957 << " is an invalid use of " << Fn.getName() << "\n");
1958 return false;
1959 }
1960
1961 // Make sure the arguments that can be matched between the call site and the
1962 // callee argee on their type. It is unlikely they do not and it doesn't
1963 // make sense for all attributes to know/care about this.
1964 assert(&Fn == ACS.getCalledFunction() && "Expected known callee");
1965 unsigned MinArgsParams =
1966 std::min(size_t(ACS.getNumArgOperands()), Fn.arg_size());
1967 for (unsigned u = 0; u < MinArgsParams; ++u) {
1968 Value *CSArgOp = ACS.getCallArgOperand(u);
1969 if (CSArgOp && Fn.getArg(u)->getType() != CSArgOp->getType()) {
1970 LLVM_DEBUG(
1971 dbgs() << "[Attributor] Call site / callee argument type mismatch ["
1972 << u << "@" << Fn.getName() << ": "
1973 << *Fn.getArg(u)->getType() << " vs. "
1974 << *ACS.getCallArgOperand(u)->getType() << "\n");
1975 return false;
1976 }
1977 }
1978
1979 if (Pred(ACS))
1980 continue;
1981
1982 LLVM_DEBUG(dbgs() << "[Attributor] Call site callback failed for "
1983 << *ACS.getInstruction() << "\n");
1984 return false;
1985 }
1986
1987 return true;
1988}
1989
1990bool Attributor::shouldPropagateCallBaseContext(const IRPosition &IRP) {
1991 // TODO: Maintain a cache of Values that are
1992 // on the pathway from a Argument to a Instruction that would effect the
1993 // liveness/return state etc.
1995}
1996
1998 const AbstractAttribute &QueryingAA,
2000 bool RecurseForSelectAndPHI) {
2001
2002 const IRPosition &IRP = QueryingAA.getIRPosition();
2003 const Function *AssociatedFunction = IRP.getAssociatedFunction();
2004 if (!AssociatedFunction)
2005 return false;
2006
2007 bool UsedAssumedInformation = false;
2010 IRPosition::returned(*AssociatedFunction), &QueryingAA, Values, S,
2011 UsedAssumedInformation, RecurseForSelectAndPHI))
2012 return false;
2013
2014 return llvm::all_of(Values, [&](const AA::ValueAndContext &VAC) {
2015 return Pred(*VAC.getValue());
2016 });
2017}
2018
2021 function_ref<bool(Instruction &)> Pred, const AbstractAttribute *QueryingAA,
2022 const AAIsDead *LivenessAA, ArrayRef<unsigned> Opcodes,
2023 bool &UsedAssumedInformation, bool CheckBBLivenessOnly = false,
2024 bool CheckPotentiallyDead = false) {
2025 for (unsigned Opcode : Opcodes) {
2026 // Check if we have instructions with this opcode at all first.
2027 auto *Insts = OpcodeInstMap.lookup(Opcode);
2028 if (!Insts)
2029 continue;
2030
2031 for (Instruction *I : *Insts) {
2032 // Skip dead instructions.
2033 if (A && !CheckPotentiallyDead &&
2034 A->isAssumedDead(IRPosition::inst(*I), QueryingAA, LivenessAA,
2035 UsedAssumedInformation, CheckBBLivenessOnly)) {
2037 dbgs() << "[Attributor] Instruction " << *I
2038 << " is potentially dead, skip!\n";);
2039 continue;
2040 }
2041
2042 if (!Pred(*I))
2043 return false;
2044 }
2045 }
2046 return true;
2047}
2048
2050 const Function *Fn,
2051 const AbstractAttribute *QueryingAA,
2052 ArrayRef<unsigned> Opcodes,
2053 bool &UsedAssumedInformation,
2054 bool CheckBBLivenessOnly,
2055 bool CheckPotentiallyDead) {
2056 // Since we need to provide instructions we have to have an exact definition.
2057 if (!Fn || Fn->isDeclaration())
2058 return false;
2059
2060 const IRPosition &QueryIRP = IRPosition::function(*Fn);
2061 const auto *LivenessAA =
2062 CheckPotentiallyDead && QueryingAA
2063 ? (getAAFor<AAIsDead>(*QueryingAA, QueryIRP, DepClassTy::NONE))
2064 : nullptr;
2065
2066 auto &OpcodeInstMap = InfoCache.getOpcodeInstMapForFunction(*Fn);
2067 if (!checkForAllInstructionsImpl(this, OpcodeInstMap, Pred, QueryingAA,
2068 LivenessAA, Opcodes, UsedAssumedInformation,
2069 CheckBBLivenessOnly, CheckPotentiallyDead))
2070 return false;
2071
2072 return true;
2073}
2074
2076 const AbstractAttribute &QueryingAA,
2077 ArrayRef<unsigned> Opcodes,
2078 bool &UsedAssumedInformation,
2079 bool CheckBBLivenessOnly,
2080 bool CheckPotentiallyDead) {
2081 const IRPosition &IRP = QueryingAA.getIRPosition();
2082 const Function *AssociatedFunction = IRP.getAssociatedFunction();
2083 return checkForAllInstructions(Pred, AssociatedFunction, &QueryingAA, Opcodes,
2084 UsedAssumedInformation, CheckBBLivenessOnly,
2085 CheckPotentiallyDead);
2086}
2087
2089 function_ref<bool(Instruction &)> Pred, AbstractAttribute &QueryingAA,
2090 bool &UsedAssumedInformation) {
2091 TimeTraceScope TS("checkForAllReadWriteInstructions");
2092
2093 const Function *AssociatedFunction =
2094 QueryingAA.getIRPosition().getAssociatedFunction();
2095 if (!AssociatedFunction)
2096 return false;
2097
2098 const IRPosition &QueryIRP = IRPosition::function(*AssociatedFunction);
2099 const auto *LivenessAA =
2100 getAAFor<AAIsDead>(QueryingAA, QueryIRP, DepClassTy::NONE);
2101
2102 for (Instruction *I :
2103 InfoCache.getReadOrWriteInstsForFunction(*AssociatedFunction)) {
2104 // Skip dead instructions.
2105 if (isAssumedDead(IRPosition::inst(*I), &QueryingAA, LivenessAA,
2106 UsedAssumedInformation))
2107 continue;
2108
2109 if (!Pred(*I))
2110 return false;
2111 }
2112
2113 return true;
2114}
2115
2116void Attributor::runTillFixpoint() {
2117 TimeTraceScope TimeScope("Attributor::runTillFixpoint");
2118 LLVM_DEBUG(dbgs() << "[Attributor] Identified and initialized "
2119 << DG.SyntheticRoot.Deps.size()
2120 << " abstract attributes.\n");
2121
2122 // Now that all abstract attributes are collected and initialized we start
2123 // the abstract analysis.
2124
2125 unsigned IterationCounter = 1;
2126 unsigned MaxIterations =
2127 Configuration.MaxFixpointIterations.value_or(SetFixpointIterations);
2128
2130 SetVector<AbstractAttribute *> Worklist, InvalidAAs;
2131 Worklist.insert_range(DG.SyntheticRoot);
2132
2133 do {
2134 // Remember the size to determine new attributes.
2135 size_t NumAAs = DG.SyntheticRoot.Deps.size();
2136 LLVM_DEBUG(dbgs() << "\n\n[Attributor] #Iteration: " << IterationCounter
2137 << ", Worklist size: " << Worklist.size() << "\n");
2138
2139 // For invalid AAs we can fix dependent AAs that have a required dependence,
2140 // thereby folding long dependence chains in a single step without the need
2141 // to run updates.
2142 for (unsigned u = 0; u < InvalidAAs.size(); ++u) {
2143 AbstractAttribute *InvalidAA = InvalidAAs[u];
2144
2145 // Check the dependences to fast track invalidation.
2147 dbgs() << "[Attributor] InvalidAA: " << *InvalidAA
2148 << " has " << InvalidAA->Deps.size()
2149 << " required & optional dependences\n");
2150 for (auto &DepIt : InvalidAA->Deps) {
2151 AbstractAttribute *DepAA = cast<AbstractAttribute>(DepIt.getPointer());
2152 if (DepIt.getInt() == unsigned(DepClassTy::OPTIONAL)) {
2154 dbgs() << " - recompute: " << *DepAA);
2155 Worklist.insert(DepAA);
2156 continue;
2157 }
2159 << " - invalidate: " << *DepAA);
2161 assert(DepAA->getState().isAtFixpoint() && "Expected fixpoint state!");
2162 if (!DepAA->getState().isValidState())
2163 InvalidAAs.insert(DepAA);
2164 else
2165 ChangedAAs.push_back(DepAA);
2166 }
2167 InvalidAA->Deps.clear();
2168 }
2169
2170 // Add all abstract attributes that are potentially dependent on one that
2171 // changed to the work list.
2172 for (AbstractAttribute *ChangedAA : ChangedAAs) {
2173 for (auto &DepIt : ChangedAA->Deps)
2174 Worklist.insert(cast<AbstractAttribute>(DepIt.getPointer()));
2175 ChangedAA->Deps.clear();
2176 }
2177
2178 LLVM_DEBUG(dbgs() << "[Attributor] #Iteration: " << IterationCounter
2179 << ", Worklist+Dependent size: " << Worklist.size()
2180 << "\n");
2181
2182 // Reset the changed and invalid set.
2183 ChangedAAs.clear();
2184 InvalidAAs.clear();
2185
2186 // Update all abstract attribute in the work list and record the ones that
2187 // changed.
2188 for (AbstractAttribute *AA : Worklist) {
2189 const auto &AAState = AA->getState();
2190 if (!AAState.isAtFixpoint())
2191 if (updateAA(*AA) == ChangeStatus::CHANGED)
2192 ChangedAAs.push_back(AA);
2193
2194 // Use the InvalidAAs vector to propagate invalid states fast transitively
2195 // without requiring updates.
2196 if (!AAState.isValidState())
2197 InvalidAAs.insert(AA);
2198 }
2199
2200 // Add attributes to the changed set if they have been created in the last
2201 // iteration.
2202 ChangedAAs.append(DG.SyntheticRoot.begin() + NumAAs,
2203 DG.SyntheticRoot.end());
2204
2205 // Reset the work list and repopulate with the changed abstract attributes.
2206 // Note that dependent ones are added above.
2207 Worklist.clear();
2208 Worklist.insert_range(ChangedAAs);
2209 Worklist.insert_range(QueryAAsAwaitingUpdate);
2210 QueryAAsAwaitingUpdate.clear();
2211
2212 } while (!Worklist.empty() && (IterationCounter++ < MaxIterations));
2213
2214 if (IterationCounter > MaxIterations && !Functions.empty()) {
2215 auto Remark = [&](OptimizationRemarkMissed ORM) {
2216 return ORM << "Attributor did not reach a fixpoint after "
2217 << ore::NV("Iterations", MaxIterations) << " iterations.";
2218 };
2219 Function *F = Functions.front();
2221 }
2222
2223 LLVM_DEBUG(dbgs() << "\n[Attributor] Fixpoint iteration done after: "
2224 << IterationCounter << "/" << MaxIterations
2225 << " iterations\n");
2226
2227 // Reset abstract arguments not settled in a sound fixpoint by now. This
2228 // happens when we stopped the fixpoint iteration early. Note that only the
2229 // ones marked as "changed" *and* the ones transitively depending on them
2230 // need to be reverted to a pessimistic state. Others might not be in a
2231 // fixpoint state but we can use the optimistic results for them anyway.
2232 SmallPtrSet<AbstractAttribute *, 32> Visited;
2233 for (unsigned u = 0; u < ChangedAAs.size(); u++) {
2234 AbstractAttribute *ChangedAA = ChangedAAs[u];
2235 if (!Visited.insert(ChangedAA).second)
2236 continue;
2237
2238 AbstractState &State = ChangedAA->getState();
2239 if (!State.isAtFixpoint()) {
2241
2242 NumAttributesTimedOut++;
2243 }
2244
2245 for (auto &DepIt : ChangedAA->Deps)
2246 ChangedAAs.push_back(cast<AbstractAttribute>(DepIt.getPointer()));
2247 ChangedAA->Deps.clear();
2248 }
2249
2250 LLVM_DEBUG({
2251 if (!Visited.empty())
2252 dbgs() << "\n[Attributor] Finalized " << Visited.size()
2253 << " abstract attributes.\n";
2254 });
2255}
2256
2258 assert(AA.isQueryAA() &&
2259 "Non-query AAs should not be required to register for updates!");
2260 QueryAAsAwaitingUpdate.insert(&AA);
2261}
2262
2263ChangeStatus Attributor::manifestAttributes() {
2264 TimeTraceScope TimeScope("Attributor::manifestAttributes");
2265 size_t NumFinalAAs = DG.SyntheticRoot.Deps.size();
2266
2267 unsigned NumManifested = 0;
2268 unsigned NumAtFixpoint = 0;
2269 ChangeStatus ManifestChange = ChangeStatus::UNCHANGED;
2270 for (auto &DepAA : DG.SyntheticRoot.Deps) {
2271 AbstractAttribute *AA = cast<AbstractAttribute>(DepAA.getPointer());
2272 AbstractState &State = AA->getState();
2273
2274 // If there is not already a fixpoint reached, we can now take the
2275 // optimistic state. This is correct because we enforced a pessimistic one
2276 // on abstract attributes that were transitively dependent on a changed one
2277 // already above.
2278 if (!State.isAtFixpoint())
2279 State.indicateOptimisticFixpoint();
2280
2281 // We must not manifest Attributes that use Callbase info.
2282 if (AA->hasCallBaseContext())
2283 continue;
2284 // If the state is invalid, we do not try to manifest it.
2285 if (!State.isValidState())
2286 continue;
2287
2288 if (AA->getCtxI() && !isRunOn(*AA->getAnchorScope()))
2289 continue;
2290
2291 // Skip dead code.
2292 bool UsedAssumedInformation = false;
2293 if (isAssumedDead(*AA, nullptr, UsedAssumedInformation,
2294 /* CheckBBLivenessOnly */ true))
2295 continue;
2296 // Check if the manifest debug counter that allows skipping manifestation of
2297 // AAs
2298 if (!DebugCounter::shouldExecute(ManifestDBGCounter))
2299 continue;
2300 // Manifest the state and record if we changed the IR.
2301 ChangeStatus LocalChange = AA->manifest(*this);
2302 if (LocalChange == ChangeStatus::CHANGED && AreStatisticsEnabled())
2303 AA->trackStatistics();
2304 LLVM_DEBUG(dbgs() << "[Attributor] Manifest " << LocalChange << " : " << *AA
2305 << "\n");
2306
2307 ManifestChange = ManifestChange | LocalChange;
2308
2309 NumAtFixpoint++;
2310 NumManifested += (LocalChange == ChangeStatus::CHANGED);
2311 }
2312
2313 (void)NumManifested;
2314 (void)NumAtFixpoint;
2315 LLVM_DEBUG(dbgs() << "\n[Attributor] Manifested " << NumManifested
2316 << " arguments while " << NumAtFixpoint
2317 << " were in a valid fixpoint state\n");
2318
2319 NumAttributesManifested += NumManifested;
2320 NumAttributesValidFixpoint += NumAtFixpoint;
2321
2322 (void)NumFinalAAs;
2323 if (NumFinalAAs != DG.SyntheticRoot.Deps.size()) {
2324 auto DepIt = DG.SyntheticRoot.Deps.begin();
2325 for (unsigned u = 0; u < NumFinalAAs; ++u)
2326 ++DepIt;
2327 for (unsigned u = NumFinalAAs; u < DG.SyntheticRoot.Deps.size();
2328 ++u, ++DepIt) {
2329 errs() << "Unexpected abstract attribute: "
2330 << cast<AbstractAttribute>(DepIt->getPointer()) << " :: "
2331 << cast<AbstractAttribute>(DepIt->getPointer())
2332 ->getIRPosition()
2333 .getAssociatedValue()
2334 << "\n";
2335 }
2336 llvm_unreachable("Expected the final number of abstract attributes to "
2337 "remain unchanged!");
2338 }
2339
2340 for (auto &It : AttrsMap) {
2341 AttributeList &AL = It.getSecond();
2342 const IRPosition &IRP =
2343 isa<Function>(It.getFirst())
2344 ? IRPosition::function(*cast<Function>(It.getFirst()))
2345 : IRPosition::callsite_function(*cast<CallBase>(It.getFirst()));
2346 IRP.setAttrList(AL);
2347 }
2348
2349 return ManifestChange;
2350}
2351
2352void Attributor::identifyDeadInternalFunctions() {
2353 // Early exit if we don't intend to delete functions.
2354 if (!Configuration.DeleteFns)
2355 return;
2356
2357 // To avoid triggering an assertion in the lazy call graph we will not delete
2358 // any internal library functions. We should modify the assertion though and
2359 // allow internals to be deleted.
2360 const auto *TLI =
2361 isModulePass()
2362 ? nullptr
2363 : getInfoCache().getTargetLibraryInfoForFunction(*Functions.back());
2364 LibFunc LF;
2365
2366 // Identify dead internal functions and delete them. This happens outside
2367 // the other fixpoint analysis as we might treat potentially dead functions
2368 // as live to lower the number of iterations. If they happen to be dead, the
2369 // below fixpoint loop will identify and eliminate them.
2370
2371 SmallVector<Function *, 8> InternalFns;
2372 for (Function *F : Functions)
2373 if (F->hasLocalLinkage() && (isModulePass() || !TLI->getLibFunc(*F, LF)))
2374 InternalFns.push_back(F);
2375
2376 SmallPtrSet<Function *, 8> LiveInternalFns;
2377 bool FoundLiveInternal = true;
2378 while (FoundLiveInternal) {
2379 FoundLiveInternal = false;
2380 for (Function *&F : InternalFns) {
2381 if (!F)
2382 continue;
2383
2384 bool UsedAssumedInformation = false;
2386 [&](AbstractCallSite ACS) {
2388 return ToBeDeletedFunctions.count(Callee) ||
2389 (Functions.count(Callee) && Callee->hasLocalLinkage() &&
2390 !LiveInternalFns.count(Callee));
2391 },
2392 *F, true, nullptr, UsedAssumedInformation)) {
2393 continue;
2394 }
2395
2396 LiveInternalFns.insert(F);
2397 F = nullptr;
2398 FoundLiveInternal = true;
2399 }
2400 }
2401
2402 for (Function *F : InternalFns)
2403 if (F)
2404 ToBeDeletedFunctions.insert(F);
2405}
2406
2407ChangeStatus Attributor::cleanupIR() {
2408 TimeTraceScope TimeScope("Attributor::cleanupIR");
2409 // Delete stuff at the end to avoid invalid references and a nice order.
2410 LLVM_DEBUG(dbgs() << "\n[Attributor] Delete/replace at least "
2411 << ToBeDeletedFunctions.size() << " functions and "
2412 << ToBeDeletedBlocks.size() << " blocks and "
2413 << ToBeDeletedInsts.size() << " instructions and "
2414 << ToBeChangedValues.size() << " values and "
2415 << ToBeChangedUses.size() << " uses. To insert "
2416 << ToBeChangedToUnreachableInsts.size()
2417 << " unreachables.\n"
2418 << "Preserve manifest added " << ManifestAddedBlocks.size()
2419 << " blocks\n");
2420
2422 SmallVector<Instruction *, 32> TerminatorsToFold;
2423
2424 auto ReplaceUse = [&](Use *U, Value *NewV) {
2425 Value *OldV = U->get();
2426
2427 // If we plan to replace NewV we need to update it at this point.
2428 do {
2429 const auto &Entry = ToBeChangedValues.lookup(NewV);
2430 if (!get<0>(Entry))
2431 break;
2432 NewV = get<0>(Entry);
2433 } while (true);
2434
2435 Instruction *I = dyn_cast<Instruction>(U->getUser());
2436 assert((!I || isRunOn(*I->getFunction())) &&
2437 "Cannot replace an instruction outside the current SCC!");
2438
2439 // Do not replace uses in returns if the value is a must-tail call we will
2440 // not delete.
2441 if (auto *RI = dyn_cast_or_null<ReturnInst>(I)) {
2442 if (auto *CI = dyn_cast<CallInst>(OldV->stripPointerCasts()))
2443 if (CI->isMustTailCall() && !ToBeDeletedInsts.count(CI))
2444 return;
2445 // If we rewrite a return and the new value is not an argument, strip the
2446 // `returned` attribute as it is wrong now.
2447 if (!isa<Argument>(NewV))
2448 for (auto &Arg : RI->getFunction()->args())
2449 Arg.removeAttr(Attribute::Returned);
2450 }
2451
2452 LLVM_DEBUG(dbgs() << "Use " << *NewV << " in " << *U->getUser()
2453 << " instead of " << *OldV << "\n");
2454 U->set(NewV);
2455
2456 if (Instruction *I = dyn_cast<Instruction>(OldV)) {
2457 CGModifiedFunctions.insert(I->getFunction());
2458 if (!isa<PHINode>(I) && !ToBeDeletedInsts.count(I) &&
2460 DeadInsts.push_back(I);
2461 }
2462 if (isa<UndefValue>(NewV) && isa<CallBase>(U->getUser())) {
2463 auto *CB = cast<CallBase>(U->getUser());
2464 if (CB->isArgOperand(U)) {
2465 unsigned Idx = CB->getArgOperandNo(U);
2466 CB->removeParamAttr(Idx, Attribute::NoUndef);
2467 auto *Callee = dyn_cast_if_present<Function>(CB->getCalledOperand());
2468 if (Callee && Callee->arg_size() > Idx)
2469 Callee->removeParamAttr(Idx, Attribute::NoUndef);
2470 }
2471 }
2472 if (isa<Constant>(NewV) && isa<BranchInst>(U->getUser())) {
2473 Instruction *UserI = cast<Instruction>(U->getUser());
2474 if (isa<UndefValue>(NewV)) {
2475 ToBeChangedToUnreachableInsts.insert(UserI);
2476 } else {
2477 TerminatorsToFold.push_back(UserI);
2478 }
2479 }
2480 };
2481
2482 for (auto &It : ToBeChangedUses) {
2483 Use *U = It.first;
2484 Value *NewV = It.second;
2485 ReplaceUse(U, NewV);
2486 }
2487
2489 for (auto &It : ToBeChangedValues) {
2490 Value *OldV = It.first;
2491 auto [NewV, Done] = It.second;
2492 Uses.clear();
2493 for (auto &U : OldV->uses())
2494 if (Done || !U.getUser()->isDroppable())
2495 Uses.push_back(&U);
2496 for (Use *U : Uses) {
2497 if (auto *I = dyn_cast<Instruction>(U->getUser()))
2498 if (!isRunOn(*I->getFunction()))
2499 continue;
2500 ReplaceUse(U, NewV);
2501 }
2502 }
2503
2504 for (const auto &V : InvokeWithDeadSuccessor)
2505 if (InvokeInst *II = dyn_cast_or_null<InvokeInst>(V)) {
2506 assert(isRunOn(*II->getFunction()) &&
2507 "Cannot replace an invoke outside the current SCC!");
2508 bool UnwindBBIsDead = II->hasFnAttr(Attribute::NoUnwind);
2509 bool NormalBBIsDead = II->hasFnAttr(Attribute::NoReturn);
2510 bool Invoke2CallAllowed =
2512 assert((UnwindBBIsDead || NormalBBIsDead) &&
2513 "Invoke does not have dead successors!");
2514 BasicBlock *BB = II->getParent();
2515 BasicBlock *NormalDestBB = II->getNormalDest();
2516 if (UnwindBBIsDead) {
2517 Instruction *NormalNextIP = &NormalDestBB->front();
2518 if (Invoke2CallAllowed) {
2520 NormalNextIP = BB->getTerminator();
2521 }
2522 if (NormalBBIsDead)
2523 ToBeChangedToUnreachableInsts.insert(NormalNextIP);
2524 } else {
2525 assert(NormalBBIsDead && "Broken invariant!");
2526 if (!NormalDestBB->getUniquePredecessor())
2527 NormalDestBB = SplitBlockPredecessors(NormalDestBB, {BB}, ".dead");
2528 ToBeChangedToUnreachableInsts.insert(&NormalDestBB->front());
2529 }
2530 }
2531 for (Instruction *I : TerminatorsToFold) {
2532 assert(isRunOn(*I->getFunction()) &&
2533 "Cannot replace a terminator outside the current SCC!");
2534 CGModifiedFunctions.insert(I->getFunction());
2535 ConstantFoldTerminator(I->getParent());
2536 }
2537 for (const auto &V : ToBeChangedToUnreachableInsts)
2538 if (Instruction *I = dyn_cast_or_null<Instruction>(V)) {
2539 LLVM_DEBUG(dbgs() << "[Attributor] Change to unreachable: " << *I
2540 << "\n");
2541 assert(isRunOn(*I->getFunction()) &&
2542 "Cannot replace an instruction outside the current SCC!");
2543 CGModifiedFunctions.insert(I->getFunction());
2545 }
2546
2547 for (const auto &V : ToBeDeletedInsts) {
2548 if (Instruction *I = dyn_cast_or_null<Instruction>(V)) {
2550 isRunOn(*I->getFunction())) &&
2551 "Cannot delete an instruction outside the current SCC!");
2552 I->dropDroppableUses();
2553 CGModifiedFunctions.insert(I->getFunction());
2554 if (!I->getType()->isVoidTy())
2555 I->replaceAllUsesWith(UndefValue::get(I->getType()));
2557 DeadInsts.push_back(I);
2558 else
2559 I->eraseFromParent();
2560 }
2561 }
2562
2563 llvm::erase_if(DeadInsts, [&](WeakTrackingVH I) { return !I; });
2564
2565 LLVM_DEBUG({
2566 dbgs() << "[Attributor] DeadInsts size: " << DeadInsts.size() << "\n";
2567 for (auto &I : DeadInsts)
2568 if (I)
2569 dbgs() << " - " << *I << "\n";
2570 });
2571
2573
2574 if (unsigned NumDeadBlocks = ToBeDeletedBlocks.size()) {
2575 SmallVector<BasicBlock *, 8> ToBeDeletedBBs;
2576 ToBeDeletedBBs.reserve(NumDeadBlocks);
2577 for (BasicBlock *BB : ToBeDeletedBlocks) {
2578 assert(isRunOn(*BB->getParent()) &&
2579 "Cannot delete a block outside the current SCC!");
2580 CGModifiedFunctions.insert(BB->getParent());
2581 // Do not delete BBs added during manifests of AAs.
2582 if (ManifestAddedBlocks.contains(BB))
2583 continue;
2584 ToBeDeletedBBs.push_back(BB);
2585 }
2586 // Actually we do not delete the blocks but squash them into a single
2587 // unreachable but untangling branches that jump here is something we need
2588 // to do in a more generic way.
2589 detachDeadBlocks(ToBeDeletedBBs, nullptr);
2590 }
2591
2592 identifyDeadInternalFunctions();
2593
2594 // Rewrite the functions as requested during manifest.
2595 ChangeStatus ManifestChange = rewriteFunctionSignatures(CGModifiedFunctions);
2596
2597 for (Function *Fn : CGModifiedFunctions)
2598 if (!ToBeDeletedFunctions.count(Fn) && Functions.count(Fn))
2599 Configuration.CGUpdater.reanalyzeFunction(*Fn);
2600
2601 for (Function *Fn : ToBeDeletedFunctions) {
2602 if (!Functions.count(Fn))
2603 continue;
2604 Configuration.CGUpdater.removeFunction(*Fn);
2605 }
2606
2607 if (!ToBeChangedUses.empty())
2608 ManifestChange = ChangeStatus::CHANGED;
2609
2610 if (!ToBeChangedToUnreachableInsts.empty())
2611 ManifestChange = ChangeStatus::CHANGED;
2612
2613 if (!ToBeDeletedFunctions.empty())
2614 ManifestChange = ChangeStatus::CHANGED;
2615
2616 if (!ToBeDeletedBlocks.empty())
2617 ManifestChange = ChangeStatus::CHANGED;
2618
2619 if (!ToBeDeletedInsts.empty())
2620 ManifestChange = ChangeStatus::CHANGED;
2621
2622 if (!InvokeWithDeadSuccessor.empty())
2623 ManifestChange = ChangeStatus::CHANGED;
2624
2625 if (!DeadInsts.empty())
2626 ManifestChange = ChangeStatus::CHANGED;
2627
2628 NumFnDeleted += ToBeDeletedFunctions.size();
2629
2630 LLVM_DEBUG(dbgs() << "[Attributor] Deleted " << ToBeDeletedFunctions.size()
2631 << " functions after manifest.\n");
2632
2633#ifdef EXPENSIVE_CHECKS
2634 for (Function *F : Functions) {
2635 if (ToBeDeletedFunctions.count(F))
2636 continue;
2637 assert(!verifyFunction(*F, &errs()) && "Module verification failed!");
2638 }
2639#endif
2640
2641 return ManifestChange;
2642}
2643
2645 TimeTraceScope TimeScope("Attributor::run");
2646 AttributorCallGraph ACallGraph(*this);
2647
2648 if (PrintCallGraph)
2649 ACallGraph.populateAll();
2650
2651 Phase = AttributorPhase::UPDATE;
2652 runTillFixpoint();
2653
2654 // dump graphs on demand
2655 if (DumpDepGraph)
2656 DG.dumpGraph();
2657
2658 if (ViewDepGraph)
2659 DG.viewGraph();
2660
2662 DG.print();
2663
2664 Phase = AttributorPhase::MANIFEST;
2665 ChangeStatus ManifestChange = manifestAttributes();
2666
2667 Phase = AttributorPhase::CLEANUP;
2668 ChangeStatus CleanupChange = cleanupIR();
2669
2670 if (PrintCallGraph)
2671 ACallGraph.print();
2672
2673 return ManifestChange | CleanupChange;
2674}
2675
2676ChangeStatus Attributor::updateAA(AbstractAttribute &AA) {
2677 TimeTraceScope TimeScope("updateAA", [&]() {
2678 return AA.getName().str() +
2679 std::to_string(AA.getIRPosition().getPositionKind());
2680 });
2681 assert(Phase == AttributorPhase::UPDATE &&
2682 "We can update AA only in the update stage!");
2683
2684 // Use a new dependence vector for this update.
2685 DependenceVector DV;
2686 DependenceStack.push_back(&DV);
2687
2688 auto &AAState = AA.getState();
2690 bool UsedAssumedInformation = false;
2691 if (!isAssumedDead(AA, nullptr, UsedAssumedInformation,
2692 /* CheckBBLivenessOnly */ true))
2693 CS = AA.update(*this);
2694
2695 if (!AA.isQueryAA() && DV.empty() && !AA.getState().isAtFixpoint()) {
2696 // If the AA did not rely on outside information but changed, we run it
2697 // again to see if it found a fixpoint. Most AAs do but we don't require
2698 // them to. Hence, it might take the AA multiple iterations to get to a
2699 // fixpoint even if it does not rely on outside information, which is fine.
2701 if (CS == ChangeStatus::CHANGED)
2702 RerunCS = AA.update(*this);
2703
2704 // If the attribute did not change during the run or rerun, and it still did
2705 // not query any non-fix information, the state will not change and we can
2706 // indicate that right at this point.
2707 if (RerunCS == ChangeStatus::UNCHANGED && !AA.isQueryAA() && DV.empty())
2708 AAState.indicateOptimisticFixpoint();
2709 }
2710
2711 if (!AAState.isAtFixpoint())
2712 rememberDependences();
2713
2714 // Verify the stack was used properly, that is we pop the dependence vector we
2715 // put there earlier.
2716 DependenceVector *PoppedDV = DependenceStack.pop_back_val();
2717 (void)PoppedDV;
2718 assert(PoppedDV == &DV && "Inconsistent usage of the dependence stack!");
2719
2720 return CS;
2721}
2722
2724 assert(!F.isDeclaration() && "Cannot create a wrapper around a declaration!");
2725
2726 Module &M = *F.getParent();
2727 LLVMContext &Ctx = M.getContext();
2728 FunctionType *FnTy = F.getFunctionType();
2729
2730 Function *Wrapper =
2731 Function::Create(FnTy, F.getLinkage(), F.getAddressSpace(), F.getName());
2732 F.setName(""); // set the inside function anonymous
2733 M.getFunctionList().insert(F.getIterator(), Wrapper);
2734
2735 F.setLinkage(GlobalValue::InternalLinkage);
2736
2737 F.replaceAllUsesWith(Wrapper);
2738 assert(F.use_empty() && "Uses remained after wrapper was created!");
2739
2740 // Move the COMDAT section to the wrapper.
2741 // TODO: Check if we need to keep it for F as well.
2742 Wrapper->setComdat(F.getComdat());
2743 F.setComdat(nullptr);
2744
2745 // Copy all metadata and attributes but keep them on F as well.
2747 F.getAllMetadata(MDs);
2748 for (auto MDIt : MDs)
2749 Wrapper->addMetadata(MDIt.first, *MDIt.second);
2750 Wrapper->setAttributes(F.getAttributes());
2751
2752 // Create the call in the wrapper.
2753 BasicBlock *EntryBB = BasicBlock::Create(Ctx, "entry", Wrapper);
2754
2756 Argument *FArgIt = F.arg_begin();
2757 for (Argument &Arg : Wrapper->args()) {
2758 Args.push_back(&Arg);
2759 Arg.setName((FArgIt++)->getName());
2760 }
2761
2762 CallInst *CI = CallInst::Create(&F, Args, "", EntryBB);
2763 CI->setTailCall(true);
2764 CI->addFnAttr(Attribute::NoInline);
2765 ReturnInst::Create(Ctx, CI->getType()->isVoidTy() ? nullptr : CI, EntryBB);
2766
2767 NumFnShallowWrappersCreated++;
2768}
2769
2771 if (F.isDeclaration() || F.hasLocalLinkage() ||
2773 return false;
2774 return true;
2775}
2776
2778 if (!AllowDeepWrapper && !Force)
2779 return nullptr;
2780 if (!isInternalizable(F))
2781 return nullptr;
2782
2783 SmallPtrSet<Function *, 2> FnSet = {&F};
2784 DenseMap<Function *, Function *> InternalizedFns;
2785 internalizeFunctions(FnSet, InternalizedFns);
2786
2787 return InternalizedFns[&F];
2788}
2789
2792 for (Function *F : FnSet)
2794 return false;
2795
2796 FnMap.clear();
2797 // Generate the internalized version of each function.
2798 for (Function *F : FnSet) {
2799 Module &M = *F->getParent();
2800 FunctionType *FnTy = F->getFunctionType();
2801
2802 // Create a copy of the current function
2803 Function *Copied =
2804 Function::Create(FnTy, F->getLinkage(), F->getAddressSpace(),
2805 F->getName() + ".internalized");
2806 ValueToValueMapTy VMap;
2807 auto *NewFArgIt = Copied->arg_begin();
2808 for (auto &Arg : F->args()) {
2809 auto ArgName = Arg.getName();
2810 NewFArgIt->setName(ArgName);
2811 VMap[&Arg] = &(*NewFArgIt++);
2812 }
2814
2815 // Copy the body of the original function to the new one
2816 CloneFunctionInto(Copied, F, VMap,
2818
2819 // Set the linakage and visibility late as CloneFunctionInto has some
2820 // implicit requirements.
2823
2824 // Copy metadata
2826 F->getAllMetadata(MDs);
2827 for (auto MDIt : MDs)
2828 if (!Copied->hasMetadata())
2829 Copied->addMetadata(MDIt.first, *MDIt.second);
2830
2831 M.getFunctionList().insert(F->getIterator(), Copied);
2832 Copied->setDSOLocal(true);
2833 FnMap[F] = Copied;
2834 }
2835
2836 // Replace all uses of the old function with the new internalized function
2837 // unless the caller is a function that was just internalized.
2838 for (Function *F : FnSet) {
2839 auto &InternalizedFn = FnMap[F];
2840 auto IsNotInternalized = [&](Use &U) -> bool {
2841 if (auto *CB = dyn_cast<CallBase>(U.getUser()))
2842 return !FnMap.lookup(CB->getCaller());
2843 return false;
2844 };
2845 F->replaceUsesWithIf(InternalizedFn, IsNotInternalized);
2846 }
2847
2848 return true;
2849}
2850
2852 Argument &Arg, ArrayRef<Type *> ReplacementTypes) {
2853
2854 if (!Configuration.RewriteSignatures)
2855 return false;
2856
2857 Function *Fn = Arg.getParent();
2858 auto CallSiteCanBeChanged = [Fn](AbstractCallSite ACS) {
2859 // Forbid the call site to cast the function return type. If we need to
2860 // rewrite these functions we need to re-create a cast for the new call site
2861 // (if the old had uses).
2862 if (!ACS.getCalledFunction() ||
2863 ACS.getInstruction()->getType() !=
2865 return false;
2866 if (cast<CallBase>(ACS.getInstruction())->getCalledOperand()->getType() !=
2867 Fn->getType())
2868 return false;
2869 if (ACS.getNumArgOperands() != Fn->arg_size())
2870 return false;
2871 // Forbid must-tail calls for now.
2872 return !ACS.isCallbackCall() && !ACS.getInstruction()->isMustTailCall();
2873 };
2874
2875 // Avoid var-arg functions for now.
2876 if (Fn->isVarArg()) {
2877 LLVM_DEBUG(dbgs() << "[Attributor] Cannot rewrite var-args functions\n");
2878 return false;
2879 }
2880
2881 // Avoid functions with complicated argument passing semantics.
2882 AttributeList FnAttributeList = Fn->getAttributes();
2883 if (FnAttributeList.hasAttrSomewhere(Attribute::Nest) ||
2884 FnAttributeList.hasAttrSomewhere(Attribute::StructRet) ||
2885 FnAttributeList.hasAttrSomewhere(Attribute::InAlloca) ||
2886 FnAttributeList.hasAttrSomewhere(Attribute::Preallocated)) {
2887 LLVM_DEBUG(
2888 dbgs() << "[Attributor] Cannot rewrite due to complex attribute\n");
2889 return false;
2890 }
2891
2892 // Avoid callbacks for now.
2893 bool UsedAssumedInformation = false;
2894 if (!checkForAllCallSites(CallSiteCanBeChanged, *Fn, true, nullptr,
2895 UsedAssumedInformation,
2896 /* CheckPotentiallyDead */ true)) {
2897 LLVM_DEBUG(dbgs() << "[Attributor] Cannot rewrite all call sites\n");
2898 return false;
2899 }
2900
2901 auto InstPred = [](Instruction &I) {
2902 if (auto *CI = dyn_cast<CallInst>(&I))
2903 return !CI->isMustTailCall();
2904 return true;
2905 };
2906
2907 // Forbid must-tail calls for now.
2908 // TODO:
2909 auto &OpcodeInstMap = InfoCache.getOpcodeInstMapForFunction(*Fn);
2910 if (!checkForAllInstructionsImpl(nullptr, OpcodeInstMap, InstPred, nullptr,
2911 nullptr, {Instruction::Call},
2912 UsedAssumedInformation)) {
2913 LLVM_DEBUG(dbgs() << "[Attributor] Cannot rewrite due to instructions\n");
2914 return false;
2915 }
2916
2917 return true;
2918}
2919
2921 Argument &Arg, ArrayRef<Type *> ReplacementTypes,
2924 LLVM_DEBUG(dbgs() << "[Attributor] Register new rewrite of " << Arg << " in "
2925 << Arg.getParent()->getName() << " with "
2926 << ReplacementTypes.size() << " replacements\n");
2927 assert(isValidFunctionSignatureRewrite(Arg, ReplacementTypes) &&
2928 "Cannot register an invalid rewrite");
2929
2930 Function *Fn = Arg.getParent();
2932 ArgumentReplacementMap[Fn];
2933 if (ARIs.empty())
2934 ARIs.resize(Fn->arg_size());
2935
2936 // If we have a replacement already with less than or equal new arguments,
2937 // ignore this request.
2938 std::unique_ptr<ArgumentReplacementInfo> &ARI = ARIs[Arg.getArgNo()];
2939 if (ARI && ARI->getNumReplacementArgs() <= ReplacementTypes.size()) {
2940 LLVM_DEBUG(dbgs() << "[Attributor] Existing rewrite is preferred\n");
2941 return false;
2942 }
2943
2944 // If we have a replacement already but we like the new one better, delete
2945 // the old.
2946 ARI.reset();
2947
2948 LLVM_DEBUG(dbgs() << "[Attributor] Register new rewrite of " << Arg << " in "
2949 << Arg.getParent()->getName() << " with "
2950 << ReplacementTypes.size() << " replacements\n");
2951
2952 // Remember the replacement.
2953 ARI.reset(new ArgumentReplacementInfo(*this, Arg, ReplacementTypes,
2954 std::move(CalleeRepairCB),
2955 std::move(ACSRepairCB)));
2956
2957 return true;
2958}
2959
2960bool Attributor::shouldSeedAttribute(AbstractAttribute &AA) {
2961 bool Result = true;
2962#ifndef NDEBUG
2963 if (SeedAllowList.size() != 0)
2964 Result = llvm::is_contained(SeedAllowList, AA.getName());
2965 Function *Fn = AA.getAnchorScope();
2966 if (FunctionSeedAllowList.size() != 0 && Fn)
2968#endif
2969 return Result;
2970}
2971
2972ChangeStatus Attributor::rewriteFunctionSignatures(
2973 SmallSetVector<Function *, 8> &ModifiedFns) {
2975
2976 for (auto &It : ArgumentReplacementMap) {
2977 Function *OldFn = It.getFirst();
2978
2979 // Deleted functions do not require rewrites.
2980 if (!Functions.count(OldFn) || ToBeDeletedFunctions.count(OldFn))
2981 continue;
2982
2984 It.getSecond();
2985 assert(ARIs.size() == OldFn->arg_size() && "Inconsistent state!");
2986
2987 SmallVector<Type *, 16> NewArgumentTypes;
2988 SmallVector<AttributeSet, 16> NewArgumentAttributes;
2989
2990 // Collect replacement argument types and copy over existing attributes.
2991 AttributeList OldFnAttributeList = OldFn->getAttributes();
2992 for (Argument &Arg : OldFn->args()) {
2993 if (const std::unique_ptr<ArgumentReplacementInfo> &ARI =
2994 ARIs[Arg.getArgNo()]) {
2995 NewArgumentTypes.append(ARI->ReplacementTypes.begin(),
2996 ARI->ReplacementTypes.end());
2997 NewArgumentAttributes.append(ARI->getNumReplacementArgs(),
2998 AttributeSet());
2999 } else {
3000 NewArgumentTypes.push_back(Arg.getType());
3001 NewArgumentAttributes.push_back(
3002 OldFnAttributeList.getParamAttrs(Arg.getArgNo()));
3003 }
3004 }
3005
3006 uint64_t LargestVectorWidth = 0;
3007 for (auto *I : NewArgumentTypes)
3008 if (auto *VT = dyn_cast<llvm::VectorType>(I))
3009 LargestVectorWidth =
3010 std::max(LargestVectorWidth,
3011 VT->getPrimitiveSizeInBits().getKnownMinValue());
3012
3013 FunctionType *OldFnTy = OldFn->getFunctionType();
3014 Type *RetTy = OldFnTy->getReturnType();
3015
3016 // Construct the new function type using the new arguments types.
3017 FunctionType *NewFnTy =
3018 FunctionType::get(RetTy, NewArgumentTypes, OldFnTy->isVarArg());
3019
3020 LLVM_DEBUG(dbgs() << "[Attributor] Function rewrite '" << OldFn->getName()
3021 << "' from " << *OldFn->getFunctionType() << " to "
3022 << *NewFnTy << "\n");
3023
3024 // Create the new function body and insert it into the module.
3025 Function *NewFn = Function::Create(NewFnTy, OldFn->getLinkage(),
3026 OldFn->getAddressSpace(), "");
3027 Functions.insert(NewFn);
3028 OldFn->getParent()->getFunctionList().insert(OldFn->getIterator(), NewFn);
3029 NewFn->takeName(OldFn);
3030 NewFn->copyAttributesFrom(OldFn);
3031
3032 // Patch the pointer to LLVM function in debug info descriptor.
3033 NewFn->setSubprogram(OldFn->getSubprogram());
3034 OldFn->setSubprogram(nullptr);
3035
3036 // Recompute the parameter attributes list based on the new arguments for
3037 // the function.
3038 LLVMContext &Ctx = OldFn->getContext();
3039 NewFn->setAttributes(AttributeList::get(
3040 Ctx, OldFnAttributeList.getFnAttrs(), OldFnAttributeList.getRetAttrs(),
3041 NewArgumentAttributes));
3042 AttributeFuncs::updateMinLegalVectorWidthAttr(*NewFn, LargestVectorWidth);
3043
3044 // Remove argmem from the memory effects if we have no more pointer
3045 // arguments, or they are readnone.
3046 MemoryEffects ME = NewFn->getMemoryEffects();
3047 int ArgNo = -1;
3048 if (ME.doesAccessArgPointees() && all_of(NewArgumentTypes, [&](Type *T) {
3049 ++ArgNo;
3050 return !T->isPtrOrPtrVectorTy() ||
3051 NewFn->hasParamAttribute(ArgNo, Attribute::ReadNone);
3052 })) {
3054 }
3055
3056 // Since we have now created the new function, splice the body of the old
3057 // function right into the new function, leaving the old rotting hulk of the
3058 // function empty.
3059 NewFn->splice(NewFn->begin(), OldFn);
3060
3061 // Set of all "call-like" instructions that invoke the old function mapped
3062 // to their new replacements.
3064
3065 // Callback to create a new "call-like" instruction for a given one.
3066 auto CallSiteReplacementCreator = [&](AbstractCallSite ACS) {
3067 CallBase *OldCB = cast<CallBase>(ACS.getInstruction());
3068 const AttributeList &OldCallAttributeList = OldCB->getAttributes();
3069
3070 // Collect the new argument operands for the replacement call site.
3071 SmallVector<Value *, 16> NewArgOperands;
3072 SmallVector<AttributeSet, 16> NewArgOperandAttributes;
3073 for (unsigned OldArgNum = 0; OldArgNum < ARIs.size(); ++OldArgNum) {
3074 unsigned NewFirstArgNum = NewArgOperands.size();
3075 (void)NewFirstArgNum; // only used inside assert.
3076 if (const std::unique_ptr<ArgumentReplacementInfo> &ARI =
3077 ARIs[OldArgNum]) {
3078 if (ARI->ACSRepairCB)
3079 ARI->ACSRepairCB(*ARI, ACS, NewArgOperands);
3080 assert(ARI->getNumReplacementArgs() + NewFirstArgNum ==
3081 NewArgOperands.size() &&
3082 "ACS repair callback did not provide as many operand as new "
3083 "types were registered!");
3084 // TODO: Exose the attribute set to the ACS repair callback
3085 NewArgOperandAttributes.append(ARI->ReplacementTypes.size(),
3086 AttributeSet());
3087 } else {
3088 NewArgOperands.push_back(ACS.getCallArgOperand(OldArgNum));
3089 NewArgOperandAttributes.push_back(
3090 OldCallAttributeList.getParamAttrs(OldArgNum));
3091 }
3092 }
3093
3094 assert(NewArgOperands.size() == NewArgOperandAttributes.size() &&
3095 "Mismatch # argument operands vs. # argument operand attributes!");
3096 assert(NewArgOperands.size() == NewFn->arg_size() &&
3097 "Mismatch # argument operands vs. # function arguments!");
3098
3099 SmallVector<OperandBundleDef, 4> OperandBundleDefs;
3100 OldCB->getOperandBundlesAsDefs(OperandBundleDefs);
3101
3102 // Create a new call or invoke instruction to replace the old one.
3103 CallBase *NewCB;
3104 if (InvokeInst *II = dyn_cast<InvokeInst>(OldCB)) {
3105 NewCB = InvokeInst::Create(NewFn, II->getNormalDest(),
3106 II->getUnwindDest(), NewArgOperands,
3107 OperandBundleDefs, "", OldCB->getIterator());
3108 } else {
3109 auto *NewCI = CallInst::Create(NewFn, NewArgOperands, OperandBundleDefs,
3110 "", OldCB->getIterator());
3111 NewCI->setTailCallKind(cast<CallInst>(OldCB)->getTailCallKind());
3112 NewCB = NewCI;
3113 }
3114
3115 // Copy over various properties and the new attributes.
3116 NewCB->copyMetadata(*OldCB, {LLVMContext::MD_prof, LLVMContext::MD_dbg});
3117 NewCB->setCallingConv(OldCB->getCallingConv());
3118 NewCB->takeName(OldCB);
3119 NewCB->setAttributes(AttributeList::get(
3120 Ctx, OldCallAttributeList.getFnAttrs(),
3121 OldCallAttributeList.getRetAttrs(), NewArgOperandAttributes));
3122
3123 AttributeFuncs::updateMinLegalVectorWidthAttr(*NewCB->getCaller(),
3124 LargestVectorWidth);
3125
3126 CallSitePairs.push_back({OldCB, NewCB});
3127 return true;
3128 };
3129
3130 // Use the CallSiteReplacementCreator to create replacement call sites.
3131 bool UsedAssumedInformation = false;
3132 bool Success = checkForAllCallSites(CallSiteReplacementCreator, *OldFn,
3133 true, nullptr, UsedAssumedInformation,
3134 /* CheckPotentiallyDead */ true);
3135 (void)Success;
3136 assert(Success && "Assumed call site replacement to succeed!");
3137
3138 // Rewire the arguments.
3139 Argument *OldFnArgIt = OldFn->arg_begin();
3140 Argument *NewFnArgIt = NewFn->arg_begin();
3141 for (unsigned OldArgNum = 0; OldArgNum < ARIs.size();
3142 ++OldArgNum, ++OldFnArgIt) {
3143 if (const std::unique_ptr<ArgumentReplacementInfo> &ARI =
3144 ARIs[OldArgNum]) {
3145 if (ARI->CalleeRepairCB)
3146 ARI->CalleeRepairCB(*ARI, *NewFn, NewFnArgIt);
3147 if (ARI->ReplacementTypes.empty())
3148 OldFnArgIt->replaceAllUsesWith(
3149 PoisonValue::get(OldFnArgIt->getType()));
3150 NewFnArgIt += ARI->ReplacementTypes.size();
3151 } else {
3152 NewFnArgIt->takeName(&*OldFnArgIt);
3153 OldFnArgIt->replaceAllUsesWith(&*NewFnArgIt);
3154 ++NewFnArgIt;
3155 }
3156 }
3157
3158 // Eliminate the instructions *after* we visited all of them.
3159 for (auto &CallSitePair : CallSitePairs) {
3160 CallBase &OldCB = *CallSitePair.first;
3161 CallBase &NewCB = *CallSitePair.second;
3162 assert(OldCB.getType() == NewCB.getType() &&
3163 "Cannot handle call sites with different types!");
3164 ModifiedFns.insert(OldCB.getFunction());
3165 OldCB.replaceAllUsesWith(&NewCB);
3166 OldCB.eraseFromParent();
3167 }
3168
3169 // Replace the function in the call graph (if any).
3170 Configuration.CGUpdater.replaceFunctionWith(*OldFn, *NewFn);
3171
3172 // If the old function was modified and needed to be reanalyzed, the new one
3173 // does now.
3174 if (ModifiedFns.remove(OldFn))
3175 ModifiedFns.insert(NewFn);
3176
3178 }
3179
3180 return Changed;
3181}
3182
3183void InformationCache::initializeInformationCache(const Function &CF,
3184 FunctionInfo &FI) {
3185 // As we do not modify the function here we can remove the const
3186 // withouth breaking implicit assumptions. At the end of the day, we could
3187 // initialize the cache eagerly which would look the same to the users.
3188 Function &F = const_cast<Function &>(CF);
3189
3190 FI.IsKernel = F.hasFnAttribute("kernel");
3191
3192 // Walk all instructions to find interesting instructions that might be
3193 // queried by abstract attributes during their initialization or update.
3194 // This has to happen before we create attributes.
3195
3196 DenseMap<const Value *, std::optional<short>> AssumeUsesMap;
3197
3198 // Add \p V to the assume uses map which track the number of uses outside of
3199 // "visited" assumes. If no outside uses are left the value is added to the
3200 // assume only use vector.
3201 auto AddToAssumeUsesMap = [&](const Value &V) -> void {
3202 SmallVector<const Instruction *> Worklist;
3203 if (auto *I = dyn_cast<Instruction>(&V))
3204 Worklist.push_back(I);
3205 while (!Worklist.empty()) {
3206 const Instruction *I = Worklist.pop_back_val();
3207 std::optional<short> &NumUses = AssumeUsesMap[I];
3208 if (!NumUses)
3209 NumUses = I->getNumUses();
3210 NumUses = *NumUses - /* this assume */ 1;
3211 if (*NumUses != 0)
3212 continue;
3213 AssumeOnlyValues.insert(I);
3214 for (const Value *Op : I->operands())
3215 if (auto *OpI = dyn_cast<Instruction>(Op))
3216 Worklist.push_back(OpI);
3217 }
3218 };
3219
3220 for (Instruction &I : instructions(&F)) {
3221 bool IsInterestingOpcode = false;
3222
3223 // To allow easy access to all instructions in a function with a given
3224 // opcode we store them in the InfoCache. As not all opcodes are interesting
3225 // to concrete attributes we only cache the ones that are as identified in
3226 // the following switch.
3227 // Note: There are no concrete attributes now so this is initially empty.
3228 switch (I.getOpcode()) {
3229 default:
3230 assert(!isa<CallBase>(&I) &&
3231 "New call base instruction type needs to be known in the "
3232 "Attributor.");
3233 break;
3234 case Instruction::Call:
3235 // Calls are interesting on their own, additionally:
3236 // For `llvm.assume` calls we also fill the KnowledgeMap as we find them.
3237 // For `must-tail` calls we remember the caller and callee.
3238 if (auto *Assume = dyn_cast<AssumeInst>(&I)) {
3239 AssumeOnlyValues.insert(Assume);
3240 fillMapFromAssume(*Assume, KnowledgeMap);
3241 AddToAssumeUsesMap(*Assume->getArgOperand(0));
3242 } else if (cast<CallInst>(I).isMustTailCall()) {
3243 FI.ContainsMustTailCall = true;
3244 if (auto *Callee = dyn_cast_if_present<Function>(
3245 cast<CallInst>(I).getCalledOperand()))
3246 getFunctionInfo(*Callee).CalledViaMustTail = true;
3247 }
3248 [[fallthrough]];
3249 case Instruction::CallBr:
3250 case Instruction::Invoke:
3251 case Instruction::CleanupRet:
3252 case Instruction::CatchSwitch:
3253 case Instruction::AtomicRMW:
3254 case Instruction::AtomicCmpXchg:
3255 case Instruction::Br:
3256 case Instruction::Resume:
3257 case Instruction::Ret:
3258 case Instruction::Load:
3259 // The alignment of a pointer is interesting for loads.
3260 case Instruction::Store:
3261 // The alignment of a pointer is interesting for stores.
3262 case Instruction::Alloca:
3263 case Instruction::AddrSpaceCast:
3264 IsInterestingOpcode = true;
3265 }
3266 if (IsInterestingOpcode) {
3267 auto *&Insts = FI.OpcodeInstMap[I.getOpcode()];
3268 if (!Insts)
3269 Insts = new (Allocator) InstructionVectorTy();
3270 Insts->push_back(&I);
3271 }
3272 if (I.mayReadOrWriteMemory())
3273 FI.RWInsts.push_back(&I);
3274 }
3275
3276 if (F.hasFnAttribute(Attribute::AlwaysInline) &&
3277 isInlineViable(F).isSuccess())
3278 InlineableFunctions.insert(&F);
3279}
3280
3281InformationCache::FunctionInfo::~FunctionInfo() {
3282 // The instruction vectors are allocated using a BumpPtrAllocator, we need to
3283 // manually destroy them.
3284 for (auto &It : OpcodeInstMap)
3285 It.getSecond()->~InstructionVectorTy();
3286}
3287
3290 assert(A.isClosedWorldModule() && "Cannot see all indirect callees!");
3291 return IndirectlyCallableFunctions;
3292}
3293
3294std::optional<unsigned> InformationCache::getFlatAddressSpace() const {
3295 if (TargetTriple.isGPU())
3296 return 0;
3297 return std::nullopt;
3298}
3299
3301 const AbstractAttribute &ToAA,
3302 DepClassTy DepClass) {
3303 if (DepClass == DepClassTy::NONE)
3304 return;
3305 // If we are outside of an update, thus before the actual fixpoint iteration
3306 // started (= when we create AAs), we do not track dependences because we will
3307 // put all AAs into the initial worklist anyway.
3308 if (DependenceStack.empty())
3309 return;
3310 if (FromAA.getState().isAtFixpoint())
3311 return;
3312 DependenceStack.back()->push_back({&FromAA, &ToAA, DepClass});
3313}
3314
3315void Attributor::rememberDependences() {
3316 assert(!DependenceStack.empty() && "No dependences to remember!");
3317
3318 for (DepInfo &DI : *DependenceStack.back()) {
3319 assert((DI.DepClass == DepClassTy::REQUIRED ||
3320 DI.DepClass == DepClassTy::OPTIONAL) &&
3321 "Expected required or optional dependence (1 bit)!");
3322 auto &DepAAs = const_cast<AbstractAttribute &>(*DI.FromAA).Deps;
3323 DepAAs.insert(AbstractAttribute::DepTy(
3324 const_cast<AbstractAttribute *>(DI.ToAA), unsigned(DI.DepClass)));
3325 }
3326}
3327
3328template <Attribute::AttrKind AK, typename AAType>
3329void Attributor::checkAndQueryIRAttr(const IRPosition &IRP, AttributeSet Attrs,
3330 bool SkipHasAttrCheck) {
3331 bool IsKnown;
3332 if (SkipHasAttrCheck || !Attrs.hasAttribute(AK))
3333 if (!Configuration.Allowed || Configuration.Allowed->count(&AAType::ID))
3334 if (!AA::hasAssumedIRAttr<AK>(*this, nullptr, IRP, DepClassTy::NONE,
3335 IsKnown))
3336 getOrCreateAAFor<AAType>(IRP);
3337}
3338
3340 if (!VisitedFunctions.insert(&F).second)
3341 return;
3342 if (F.isDeclaration())
3343 return;
3344
3345 // In non-module runs we need to look at the call sites of a function to
3346 // determine if it is part of a must-tail call edge. This will influence what
3347 // attributes we can derive.
3348 InformationCache::FunctionInfo &FI = InfoCache.getFunctionInfo(F);
3349 if (!isModulePass() && !FI.CalledViaMustTail) {
3350 for (const Use &U : F.uses())
3351 if (const auto *CB = dyn_cast<CallBase>(U.getUser()))
3352 if (CB->isCallee(&U) && CB->isMustTailCall())
3353 FI.CalledViaMustTail = true;
3354 }
3355
3357 bool IsIPOAmendable = isFunctionIPOAmendable(F);
3358 auto Attrs = F.getAttributes();
3359 auto FnAttrs = Attrs.getFnAttrs();
3360
3361 // Check for dead BasicBlocks in every function.
3362 // We need dead instruction detection because we do not want to deal with
3363 // broken IR in which SSA rules do not apply.
3365
3366 // Every function might contain instructions that cause "undefined
3367 // behavior".
3369
3370 // Every function might be applicable for Heap-To-Stack conversion.
3373
3374 // Every function might be "must-progress".
3375 checkAndQueryIRAttr<Attribute::MustProgress, AAMustProgress>(FPos, FnAttrs);
3376
3377 // Every function might be "no-free".
3378 checkAndQueryIRAttr<Attribute::NoFree, AANoFree>(FPos, FnAttrs);
3379
3380 // Every function might be "will-return".
3381 checkAndQueryIRAttr<Attribute::WillReturn, AAWillReturn>(FPos, FnAttrs);
3382
3383 // Every function might be marked "nosync"
3384 checkAndQueryIRAttr<Attribute::NoSync, AANoSync>(FPos, FnAttrs);
3385
3386 // Everything that is visible from the outside (=function, argument, return
3387 // positions), cannot be changed if the function is not IPO amendable. We can
3388 // however analyse the code inside.
3389 if (IsIPOAmendable) {
3390
3391 // Every function can be nounwind.
3392 checkAndQueryIRAttr<Attribute::NoUnwind, AANoUnwind>(FPos, FnAttrs);
3393
3394 // Every function might be "no-return".
3395 checkAndQueryIRAttr<Attribute::NoReturn, AANoReturn>(FPos, FnAttrs);
3396
3397 // Every function might be "no-recurse".
3398 checkAndQueryIRAttr<Attribute::NoRecurse, AANoRecurse>(FPos, FnAttrs);
3399
3400 // Every function can be "non-convergent".
3401 if (Attrs.hasFnAttr(Attribute::Convergent))
3403
3404 // Every function might be "readnone/readonly/writeonly/...".
3406
3407 // Every function can be "readnone/argmemonly/inaccessiblememonly/...".
3409
3410 // Every function can track active assumptions.
3412
3413 // If we're not using a dynamic mode for float, there's nothing worthwhile
3414 // to infer. This misses the edge case denormal-fp-math="dynamic" and
3415 // denormal-fp-math-f32=something, but that likely has no real world use.
3416 DenormalMode Mode = F.getDenormalMode(APFloat::IEEEsingle());
3417 if (Mode.Input == DenormalMode::Dynamic ||
3418 Mode.Output == DenormalMode::Dynamic)
3420
3421 // Return attributes are only appropriate if the return type is non void.
3422 Type *ReturnType = F.getReturnType();
3423 if (!ReturnType->isVoidTy()) {
3425 AttributeSet RetAttrs = Attrs.getRetAttrs();
3426
3427 // Every returned value might be dead.
3429
3430 // Every function might be simplified.
3431 bool UsedAssumedInformation = false;
3432 getAssumedSimplified(RetPos, nullptr, UsedAssumedInformation,
3434
3435 // Every returned value might be marked noundef.
3436 checkAndQueryIRAttr<Attribute::NoUndef, AANoUndef>(RetPos, RetAttrs);
3437
3438 if (ReturnType->isPointerTy()) {
3439
3440 // Every function with pointer return type might be marked align.
3442
3443 // Every function with pointer return type might be marked nonnull.
3444 checkAndQueryIRAttr<Attribute::NonNull, AANonNull>(RetPos, RetAttrs);
3445
3446 // Every function with pointer return type might be marked noalias.
3447 checkAndQueryIRAttr<Attribute::NoAlias, AANoAlias>(RetPos, RetAttrs);
3448
3449 // Every function with pointer return type might be marked
3450 // dereferenceable.
3452 } else if (AttributeFuncs::isNoFPClassCompatibleType(ReturnType)) {
3454 }
3455 }
3456 }
3457
3458 for (Argument &Arg : F.args()) {
3459 IRPosition ArgPos = IRPosition::argument(Arg);
3460 auto ArgNo = Arg.getArgNo();
3461 AttributeSet ArgAttrs = Attrs.getParamAttrs(ArgNo);
3462
3463 if (!IsIPOAmendable) {
3464 if (Arg.getType()->isPointerTy())
3465 // Every argument with pointer type might be marked nofree.
3466 checkAndQueryIRAttr<Attribute::NoFree, AANoFree>(ArgPos, ArgAttrs);
3467 continue;
3468 }
3469
3470 // Every argument might be simplified. We have to go through the
3471 // Attributor interface though as outside AAs can register custom
3472 // simplification callbacks.
3473 bool UsedAssumedInformation = false;
3474 getAssumedSimplified(ArgPos, /* AA */ nullptr, UsedAssumedInformation,
3476
3477 // Every argument might be dead.
3479
3480 // Every argument might be marked noundef.
3481 checkAndQueryIRAttr<Attribute::NoUndef, AANoUndef>(ArgPos, ArgAttrs);
3482
3483 if (Arg.getType()->isPointerTy()) {
3484 // Every argument with pointer type might be marked nonnull.
3485 checkAndQueryIRAttr<Attribute::NonNull, AANonNull>(ArgPos, ArgAttrs);
3486
3487 // Every argument with pointer type might be marked noalias.
3488 checkAndQueryIRAttr<Attribute::NoAlias, AANoAlias>(ArgPos, ArgAttrs);
3489
3490 // Every argument with pointer type might be marked dereferenceable.
3492
3493 // Every argument with pointer type might be marked align.
3495
3496 // Every argument with pointer type might be marked nocapture.
3497 checkAndQueryIRAttr<Attribute::Captures, AANoCapture>(
3498 ArgPos, ArgAttrs, /*SkipHasAttrCheck=*/true);
3499
3500 // Every argument with pointer type might be marked
3501 // "readnone/readonly/writeonly/..."
3503
3504 // Every argument with pointer type might be marked nofree.
3505 checkAndQueryIRAttr<Attribute::NoFree, AANoFree>(ArgPos, ArgAttrs);
3506
3507 // Every argument with pointer type might be privatizable (or
3508 // promotable)
3510 } else if (AttributeFuncs::isNoFPClassCompatibleType(Arg.getType())) {
3512 }
3513 }
3514
3515 auto CallSitePred = [&](Instruction &I) -> bool {
3516 auto &CB = cast<CallBase>(I);
3517 IRPosition CBInstPos = IRPosition::inst(CB);
3519
3520 // Call sites might be dead if they do not have side effects and no live
3521 // users. The return value might be dead if there are no live users.
3522 getOrCreateAAFor<AAIsDead>(CBInstPos);
3523
3524 Function *Callee = dyn_cast_if_present<Function>(CB.getCalledOperand());
3525 // TODO: Even if the callee is not known now we might be able to simplify
3526 // the call/callee.
3527 if (!Callee) {
3529 return true;
3530 }
3531
3532 // Every call site can track active assumptions.
3534
3535 // Skip declarations except if annotations on their call sites were
3536 // explicitly requested.
3537 if (!AnnotateDeclarationCallSites && Callee->isDeclaration() &&
3538 !Callee->hasMetadata(LLVMContext::MD_callback))
3539 return true;
3540
3541 if (!Callee->getReturnType()->isVoidTy() && !CB.use_empty()) {
3543 bool UsedAssumedInformation = false;
3544 getAssumedSimplified(CBRetPos, nullptr, UsedAssumedInformation,
3546
3547 if (AttributeFuncs::isNoFPClassCompatibleType(Callee->getReturnType()))
3549 }
3550
3551 const AttributeList &CBAttrs = CBFnPos.getAttrList();
3552 for (int I = 0, E = CB.arg_size(); I < E; ++I) {
3553
3555 AttributeSet CBArgAttrs = CBAttrs.getParamAttrs(I);
3556
3557 // Every call site argument might be dead.
3559
3560 // Call site argument might be simplified. We have to go through the
3561 // Attributor interface though as outside AAs can register custom
3562 // simplification callbacks.
3563 bool UsedAssumedInformation = false;
3564 getAssumedSimplified(CBArgPos, /* AA */ nullptr, UsedAssumedInformation,
3566
3567 // Every call site argument might be marked "noundef".
3568 checkAndQueryIRAttr<Attribute::NoUndef, AANoUndef>(CBArgPos, CBArgAttrs);
3569
3570 Type *ArgTy = CB.getArgOperand(I)->getType();
3571
3572 if (!ArgTy->isPointerTy()) {
3573 if (AttributeFuncs::isNoFPClassCompatibleType(ArgTy))
3575
3576 continue;
3577 }
3578
3579 // Call site argument attribute "non-null".
3580 checkAndQueryIRAttr<Attribute::NonNull, AANonNull>(CBArgPos, CBArgAttrs);
3581
3582 // Call site argument attribute "captures(none)".
3583 checkAndQueryIRAttr<Attribute::Captures, AANoCapture>(
3584 CBArgPos, CBArgAttrs, /*SkipHasAttrCheck=*/true);
3585
3586 // Call site argument attribute "no-alias".
3587 checkAndQueryIRAttr<Attribute::NoAlias, AANoAlias>(CBArgPos, CBArgAttrs);
3588
3589 // Call site argument attribute "dereferenceable".
3591
3592 // Call site argument attribute "align".
3593 getOrCreateAAFor<AAAlign>(CBArgPos);
3594
3595 // Call site argument attribute
3596 // "readnone/readonly/writeonly/..."
3597 if (!CBAttrs.hasParamAttr(I, Attribute::ReadNone))
3599
3600 // Call site argument attribute "nofree".
3601 checkAndQueryIRAttr<Attribute::NoFree, AANoFree>(CBArgPos, CBArgAttrs);
3602 }
3603 return true;
3604 };
3605
3606 auto &OpcodeInstMap = InfoCache.getOpcodeInstMapForFunction(F);
3607 [[maybe_unused]] bool Success;
3608 bool UsedAssumedInformation = false;
3610 nullptr, OpcodeInstMap, CallSitePred, nullptr, nullptr,
3611 {(unsigned)Instruction::Invoke, (unsigned)Instruction::CallBr,
3612 (unsigned)Instruction::Call},
3613 UsedAssumedInformation);
3614 assert(Success && "Expected the check call to be successful!");
3615
3616 auto LoadStorePred = [&](Instruction &I) -> bool {
3617 if (auto *LI = dyn_cast<LoadInst>(&I)) {
3618 getOrCreateAAFor<AAAlign>(IRPosition::value(*LI->getPointerOperand()));
3619 if (SimplifyAllLoads)
3621 UsedAssumedInformation, AA::Intraprocedural);
3623 IRPosition::value(*LI->getPointerOperand()));
3625 IRPosition::value(*LI->getPointerOperand()));
3626 } else {
3627 auto &SI = cast<StoreInst>(I);
3629 getAssumedSimplified(IRPosition::value(*SI.getValueOperand()), nullptr,
3630 UsedAssumedInformation, AA::Intraprocedural);
3631 getOrCreateAAFor<AAAlign>(IRPosition::value(*SI.getPointerOperand()));
3633 IRPosition::value(*SI.getPointerOperand()));
3634 }
3635 return true;
3636 };
3638 nullptr, OpcodeInstMap, LoadStorePred, nullptr, nullptr,
3639 {(unsigned)Instruction::Load, (unsigned)Instruction::Store},
3640 UsedAssumedInformation);
3641 assert(Success && "Expected the check call to be successful!");
3642
3643 // AllocaInstPredicate
3644 auto AAAllocationInfoPred = [&](Instruction &I) -> bool {
3646 return true;
3647 };
3648
3650 nullptr, OpcodeInstMap, AAAllocationInfoPred, nullptr, nullptr,
3651 {(unsigned)Instruction::Alloca}, UsedAssumedInformation);
3652 assert(Success && "Expected the check call to be successful!");
3653}
3654
3656 if (CloseWorldAssumption.getNumOccurrences())
3657 return CloseWorldAssumption;
3658 return isModulePass() && Configuration.IsClosedWorldModule;
3659}
3660
3661/// Helpers to ease debugging through output streams and print calls.
3662///
3663///{
3665 return OS << (S == ChangeStatus::CHANGED ? "changed" : "unchanged");
3666}
3667
3669 switch (AP) {
3671 return OS << "inv";
3673 return OS << "flt";
3675 return OS << "fn_ret";
3677 return OS << "cs_ret";
3679 return OS << "fn";
3681 return OS << "cs";
3683 return OS << "arg";
3685 return OS << "cs_arg";
3686 }
3687 llvm_unreachable("Unknown attribute position!");
3688}
3689
3691 const Value &AV = Pos.getAssociatedValue();
3692 OS << "{" << Pos.getPositionKind() << ":" << AV.getName() << " ["
3693 << Pos.getAnchorValue().getName() << "@" << Pos.getCallSiteArgNo() << "]";
3694
3695 if (Pos.hasCallBaseContext())
3696 OS << "[cb_context:" << *Pos.getCallBaseContext() << "]";
3697 return OS << "}";
3698}
3699
3701 OS << "range-state(" << S.getBitWidth() << ")<";
3702 S.getKnown().print(OS);
3703 OS << " / ";
3704 S.getAssumed().print(OS);
3705 OS << ">";
3706
3707 return OS << static_cast<const AbstractState &>(S);
3708}
3709
3711 return OS << (!S.isValidState() ? "top" : (S.isAtFixpoint() ? "fix" : ""));
3712}
3713
3715 AA.print(OS);
3716 return OS;
3717}
3718
3721 OS << "set-state(< {";
3722 if (!S.isValidState())
3723 OS << "full-set";
3724 else {
3725 for (const auto &It : S.getAssumedSet())
3726 OS << It << ", ";
3727 if (S.undefIsContained())
3728 OS << "undef ";
3729 }
3730 OS << "} >)";
3731
3732 return OS;
3733}
3734
3736 const PotentialLLVMValuesState &S) {
3737 OS << "set-state(< {";
3738 if (!S.isValidState())
3739 OS << "full-set";
3740 else {
3741 for (const auto &It : S.getAssumedSet()) {
3742 if (auto *F = dyn_cast<Function>(It.first.getValue()))
3743 OS << "@" << F->getName() << "[" << int(It.second) << "], ";
3744 else
3745 OS << *It.first.getValue() << "[" << int(It.second) << "], ";
3746 }
3747 if (S.undefIsContained())
3748 OS << "undef ";
3749 }
3750 OS << "} >)";
3751
3752 return OS;
3753}
3754
3756 OS << "[";
3757 OS << getName();
3758 OS << "] for CtxI ";
3759
3760 if (auto *I = getCtxI()) {
3761 OS << "'";
3762 I->print(OS);
3763 OS << "'";
3764 } else
3765 OS << "<<null inst>>";
3766
3767 OS << " at position " << getIRPosition() << " with state " << getAsStr(A)
3768 << '\n';
3769}
3770
3772 print(OS);
3773
3774 for (const auto &DepAA : Deps) {
3775 auto *AA = DepAA.getPointer();
3776 OS << " updates ";
3777 AA->print(OS);
3778 }
3779
3780 OS << '\n';
3781}
3782
3784 const AAPointerInfo::Access &Acc) {
3785 OS << " [" << Acc.getKind() << "] " << *Acc.getRemoteInst();
3786 if (Acc.getLocalInst() != Acc.getRemoteInst())
3787 OS << " via " << *Acc.getLocalInst();
3788 if (Acc.getContent()) {
3789 if (*Acc.getContent())
3790 OS << " [" << **Acc.getContent() << "]";
3791 else
3792 OS << " [ <unknown> ]";
3793 }
3794 return OS;
3795}
3796///}
3797
3798/// ----------------------------------------------------------------------------
3799/// Pass (Manager) Boilerplate
3800/// ----------------------------------------------------------------------------
3801
3803 SetVector<Function *> &Functions,
3804 AnalysisGetter &AG,
3805 CallGraphUpdater &CGUpdater,
3806 bool DeleteFns, bool IsModulePass) {
3807 if (Functions.empty())
3808 return false;
3809
3810 LLVM_DEBUG({
3811 dbgs() << "[Attributor] Run on module with " << Functions.size()
3812 << " functions:\n";
3813 for (Function *Fn : Functions)
3814 dbgs() << " - " << Fn->getName() << "\n";
3815 });
3816
3817 // Create an Attributor and initially empty information cache that is filled
3818 // while we identify default attribute opportunities.
3819 AttributorConfig AC(CGUpdater);
3820 AC.IsModulePass = IsModulePass;
3821 AC.DeleteFns = DeleteFns;
3822
3823 /// Tracking callback for specialization of indirect calls.
3825 IndirectCalleeTrackingMap;
3826 if (MaxSpecializationPerCB.getNumOccurrences()) {
3827 AC.IndirectCalleeSpecializationCallback =
3828 [&](Attributor &, const AbstractAttribute &AA, CallBase &CB,
3829 Function &Callee, unsigned) {
3830 if (MaxSpecializationPerCB == 0)
3831 return false;
3832 auto &Set = IndirectCalleeTrackingMap[&CB];
3833 if (!Set)
3834 Set = std::make_unique<SmallPtrSet<Function *, 8>>();
3835 if (Set->size() >= MaxSpecializationPerCB)
3836 return Set->contains(&Callee);
3837 Set->insert(&Callee);
3838 return true;
3839 };
3840 }
3841
3842 Attributor A(Functions, InfoCache, AC);
3843
3844 // Create shallow wrappers for all functions that are not IPO amendable
3846 for (Function *F : Functions)
3847 if (!A.isFunctionIPOAmendable(*F))
3849
3850 // Internalize non-exact functions
3851 // TODO: for now we eagerly internalize functions without calculating the
3852 // cost, we need a cost interface to determine whether internalizing
3853 // a function is "beneficial"
3854 if (AllowDeepWrapper) {
3855 unsigned FunSize = Functions.size();
3856 for (unsigned u = 0; u < FunSize; u++) {
3857 Function *F = Functions[u];
3858 if (!F->isDeclaration() && !F->isDefinitionExact() && !F->use_empty() &&
3859 !GlobalValue::isInterposableLinkage(F->getLinkage())) {
3861 assert(NewF && "Could not internalize function.");
3862 Functions.insert(NewF);
3863
3864 // Update call graph
3865 CGUpdater.replaceFunctionWith(*F, *NewF);
3866 for (const Use &U : NewF->uses())
3867 if (CallBase *CB = dyn_cast<CallBase>(U.getUser())) {
3868 auto *CallerF = CB->getCaller();
3869 CGUpdater.reanalyzeFunction(*CallerF);
3870 }
3871 }
3872 }
3873 }
3874
3875 for (Function *F : Functions) {
3876 if (F->hasExactDefinition())
3877 NumFnWithExactDefinition++;
3878 else
3879 NumFnWithoutExactDefinition++;
3880
3881 // We look at internal functions only on-demand but if any use is not a
3882 // direct call or outside the current set of analyzed functions, we have
3883 // to do it eagerly.
3884 if (F->hasLocalLinkage()) {
3885 if (llvm::all_of(F->uses(), [&Functions](const Use &U) {
3886 const auto *CB = dyn_cast<CallBase>(U.getUser());
3887 return CB && CB->isCallee(&U) &&
3888 Functions.count(const_cast<Function *>(CB->getCaller()));
3889 }))
3890 continue;
3891 }
3892
3893 // Populate the Attributor with abstract attribute opportunities in the
3894 // function and the information cache with IR information.
3895 A.identifyDefaultAbstractAttributes(*F);
3896 }
3897
3898 ChangeStatus Changed = A.run();
3899
3900 LLVM_DEBUG(dbgs() << "[Attributor] Done with " << Functions.size()
3901 << " functions, result: " << Changed << ".\n");
3903}
3904
3906 SetVector<Function *> &Functions,
3907 AnalysisGetter &AG,
3908 CallGraphUpdater &CGUpdater,
3910 bool IsModulePass) {
3911 if (Functions.empty())
3912 return false;
3913
3914 LLVM_DEBUG({
3915 dbgs() << "[AttributorLight] Run on module with " << Functions.size()
3916 << " functions:\n";
3917 for (Function *Fn : Functions)
3918 dbgs() << " - " << Fn->getName() << "\n";
3919 });
3920
3921 // Create an Attributor and initially empty information cache that is filled
3922 // while we identify default attribute opportunities.
3923 AttributorConfig AC(CGUpdater);
3924 AC.IsModulePass = IsModulePass;
3925 AC.DeleteFns = false;
3926 DenseSet<const char *> Allowed(
3932 AC.Allowed = &Allowed;
3933 AC.UseLiveness = false;
3934
3935 Attributor A(Functions, InfoCache, AC);
3936
3937 for (Function *F : Functions) {
3938 if (F->hasExactDefinition())
3939 NumFnWithExactDefinition++;
3940 else
3941 NumFnWithoutExactDefinition++;
3942
3943 // We look at internal functions only on-demand but if any use is not a
3944 // direct call or outside the current set of analyzed functions, we have
3945 // to do it eagerly.
3946 if (AC.UseLiveness && F->hasLocalLinkage()) {
3947 if (llvm::all_of(F->uses(), [&Functions](const Use &U) {
3948 const auto *CB = dyn_cast<CallBase>(U.getUser());
3949 return CB && CB->isCallee(&U) &&
3950 Functions.count(const_cast<Function *>(CB->getCaller()));
3951 }))
3952 continue;
3953 }
3954
3955 // Populate the Attributor with abstract attribute opportunities in the
3956 // function and the information cache with IR information.
3957 A.identifyDefaultAbstractAttributes(*F);
3958 }
3959
3960 ChangeStatus Changed = A.run();
3961
3963 // Invalidate analyses for modified functions so that we don't have to
3964 // invalidate all analyses for all functions in this SCC.
3965 PreservedAnalyses FuncPA;
3966 // We haven't changed the CFG for modified functions.
3967 FuncPA.preserveSet<CFGAnalyses>();
3968 for (Function *Changed : A.getModifiedFunctions()) {
3969 FAM.invalidate(*Changed, FuncPA);
3970 // Also invalidate any direct callers of changed functions since analyses
3971 // may care about attributes of direct callees. For example, MemorySSA
3972 // cares about whether or not a call's callee modifies memory and queries
3973 // that through function attributes.
3974 for (auto *U : Changed->users()) {
3975 if (auto *Call = dyn_cast<CallBase>(U)) {
3976 if (Call->getCalledFunction() == Changed)
3977 FAM.invalidate(*Call->getFunction(), FuncPA);
3978 }
3979 }
3980 }
3981 }
3982 LLVM_DEBUG(dbgs() << "[Attributor] Done with " << Functions.size()
3983 << " functions, result: " << Changed << ".\n");
3985}
3986
3987void AADepGraph::viewGraph() { llvm::ViewGraph(this, "Dependency Graph"); }
3988
3990 static std::atomic<int> CallTimes;
3991 std::string Prefix;
3992
3993 if (!DepGraphDotFileNamePrefix.empty())
3995 else
3996 Prefix = "dep_graph";
3997 std::string Filename =
3998 Prefix + "_" + std::to_string(CallTimes.load()) + ".dot";
3999
4000 outs() << "Dependency graph dump to " << Filename << ".\n";
4001
4002 std::error_code EC;
4003
4004 raw_fd_ostream File(Filename, EC, sys::fs::OF_TextWithCRLF);
4005 if (!EC)
4006 llvm::WriteGraph(File, this);
4007
4008 CallTimes++;
4009}
4010
4012 for (auto DepAA : SyntheticRoot.Deps)
4013 cast<AbstractAttribute>(DepAA.getPointer())->printWithDeps(outs());
4014}
4015
4019 AnalysisGetter AG(FAM);
4020
4021 SetVector<Function *> Functions;
4022 for (Function &F : M)
4023 Functions.insert(&F);
4024
4025 CallGraphUpdater CGUpdater;
4026 BumpPtrAllocator Allocator;
4027 InformationCache InfoCache(M, AG, Allocator, /* CGSCC */ nullptr);
4028 if (runAttributorOnFunctions(InfoCache, Functions, AG, CGUpdater,
4029 /* DeleteFns */ true, /* IsModulePass */ true)) {
4030 // FIXME: Think about passes we will preserve and add them here.
4031 return PreservedAnalyses::none();
4032 }
4033 return PreservedAnalyses::all();
4034}
4035
4038 LazyCallGraph &CG,
4039 CGSCCUpdateResult &UR) {
4041 AM.getResult<FunctionAnalysisManagerCGSCCProxy>(C, CG).getManager();
4042 AnalysisGetter AG(FAM);
4043
4044 SetVector<Function *> Functions;
4045 for (LazyCallGraph::Node &N : C)
4046 Functions.insert(&N.getFunction());
4047
4048 if (Functions.empty())
4049 return PreservedAnalyses::all();
4050
4051 Module &M = *Functions.back()->getParent();
4052 CallGraphUpdater CGUpdater;
4053 CGUpdater.initialize(CG, C, AM, UR);
4054 BumpPtrAllocator Allocator;
4055 InformationCache InfoCache(M, AG, Allocator, /* CGSCC */ &Functions);
4056 if (runAttributorOnFunctions(InfoCache, Functions, AG, CGUpdater,
4057 /* DeleteFns */ false,
4058 /* IsModulePass */ false)) {
4059 // FIXME: Think about passes we will preserve and add them here.
4062 return PA;
4063 }
4064 return PreservedAnalyses::all();
4065}
4066
4071 AnalysisGetter AG(FAM, /* CachedOnly */ true);
4072
4073 SetVector<Function *> Functions;
4074 for (Function &F : M)
4075 Functions.insert(&F);
4076
4077 CallGraphUpdater CGUpdater;
4078 BumpPtrAllocator Allocator;
4079 InformationCache InfoCache(M, AG, Allocator, /* CGSCC */ nullptr);
4080 if (runAttributorLightOnFunctions(InfoCache, Functions, AG, CGUpdater, FAM,
4081 /* IsModulePass */ true)) {
4083 // We have not added or removed functions.
4085 // We already invalidated all relevant function analyses above.
4087 return PA;
4088 }
4089 return PreservedAnalyses::all();
4090}
4091
4094 LazyCallGraph &CG,
4095 CGSCCUpdateResult &UR) {
4097 AM.getResult<FunctionAnalysisManagerCGSCCProxy>(C, CG).getManager();
4098 AnalysisGetter AG(FAM);
4099
4100 SetVector<Function *> Functions;
4101 for (LazyCallGraph::Node &N : C)
4102 Functions.insert(&N.getFunction());
4103
4104 if (Functions.empty())
4105 return PreservedAnalyses::all();
4106
4107 Module &M = *Functions.back()->getParent();
4108 CallGraphUpdater CGUpdater;
4109 CGUpdater.initialize(CG, C, AM, UR);
4110 BumpPtrAllocator Allocator;
4111 InformationCache InfoCache(M, AG, Allocator, /* CGSCC */ &Functions);
4112 if (runAttributorLightOnFunctions(InfoCache, Functions, AG, CGUpdater, FAM,
4113 /* IsModulePass */ false)) {
4115 // We have not added or removed functions.
4117 // We already invalidated all relevant function analyses above.
4119 return PA;
4120 }
4121 return PreservedAnalyses::all();
4122}
4123namespace llvm {
4124
4141
4142template <>
4144 static NodeRef getEntryNode(AADepGraph *DG) { return DG->GetEntryNode(); }
4145
4148
4149 static nodes_iterator nodes_begin(AADepGraph *DG) { return DG->begin(); }
4150
4151 static nodes_iterator nodes_end(AADepGraph *DG) { return DG->end(); }
4152};
4153
4154template <> struct DOTGraphTraits<AADepGraph *> : public DefaultDOTGraphTraits {
4156
4157 static std::string getNodeLabel(const AADepGraphNode *Node,
4158 const AADepGraph *DG) {
4159 std::string AAString;
4160 raw_string_ostream O(AAString);
4161 Node->print(O);
4162 return AAString;
4163 }
4164};
4165
4166} // end namespace llvm
aarch64 falkor hwpf fix Falkor HW Prefetch Fix Late Phase
static unsigned getIntrinsicID(const SDNode *N)
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
amdgpu aa AMDGPU Address space based Alias Analysis Wrapper
Rewrite undef for PHI
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Expand Atomic instructions
This file contains the simple types necessary to represent the attributes associated with functions a...
static cl::opt< bool > AllowShallowWrappers("attributor-allow-shallow-wrappers", cl::Hidden, cl::desc("Allow the Attributor to create shallow " "wrappers for non-exact definitions."), cl::init(false))
bool canMarkAsVisited(const User *Usr)
#define VERBOSE_DEBUG_TYPE
static cl::opt< bool > EnableHeapToStack("enable-heap-to-stack-conversion", cl::init(true), cl::Hidden)
static cl::list< std::string > SeedAllowList("attributor-seed-allow-list", cl::Hidden, cl::desc("Comma separated list of attribute names that are " "allowed to be seeded."), cl::CommaSeparated)
static bool runAttributorOnFunctions(InformationCache &InfoCache, SetVector< Function * > &Functions, AnalysisGetter &AG, CallGraphUpdater &CGUpdater, bool DeleteFns, bool IsModulePass)
}
static bool getPotentialCopiesOfMemoryValue(Attributor &A, Ty &I, SmallSetVector< Value *, 4 > &PotentialCopies, SmallSetVector< Instruction *, 4 > *PotentialValueOrigins, const AbstractAttribute &QueryingAA, bool &UsedAssumedInformation, bool OnlyExact)
static bool runAttributorLightOnFunctions(InformationCache &InfoCache, SetVector< Function * > &Functions, AnalysisGetter &AG, CallGraphUpdater &CGUpdater, FunctionAnalysisManager &FAM, bool IsModulePass)
static cl::opt< unsigned, true > MaxInitializationChainLengthX("attributor-max-initialization-chain-length", cl::Hidden, cl::desc("Maximal number of chained initializations (to avoid stack overflows)"), cl::location(MaxInitializationChainLength), cl::init(1024))
static cl::opt< unsigned > MaxSpecializationPerCB("attributor-max-specializations-per-call-base", cl::Hidden, cl::desc("Maximal number of callees specialized for " "a call base"), cl::init(UINT32_MAX))
static cl::opt< bool > SimplifyAllLoads("attributor-simplify-all-loads", cl::Hidden, cl::desc("Try to simplify all loads."), cl::init(true))
static bool addIfNotExistent(LLVMContext &Ctx, const Attribute &Attr, AttributeSet AttrSet, bool ForceReplace, AttrBuilder &AB)
Return true if the information provided by Attr was added to the attribute set AttrSet.
static cl::opt< bool > ViewDepGraph("attributor-view-dep-graph", cl::Hidden, cl::desc("View the dependency graph."), cl::init(false))
static bool isEqualOrWorse(const Attribute &New, const Attribute &Old)
Return true if New is equal or worse than Old.
static cl::opt< bool > AllowDeepWrapper("attributor-allow-deep-wrappers", cl::Hidden, cl::desc("Allow the Attributor to use IP information " "derived from non-exact functions via cloning"), cl::init(false))
static cl::opt< bool > DumpDepGraph("attributor-dump-dep-graph", cl::Hidden, cl::desc("Dump the dependency graph to dot files."), cl::init(false))
static cl::opt< bool > PrintCallGraph("attributor-print-call-graph", cl::Hidden, cl::desc("Print Attributor's internal call graph"), cl::init(false))
static bool checkForAllInstructionsImpl(Attributor *A, InformationCache::OpcodeInstMapTy &OpcodeInstMap, function_ref< bool(Instruction &)> Pred, const AbstractAttribute *QueryingAA, const AAIsDead *LivenessAA, ArrayRef< unsigned > Opcodes, bool &UsedAssumedInformation, bool CheckBBLivenessOnly=false, bool CheckPotentiallyDead=false)
static cl::opt< bool > PrintDependencies("attributor-print-dep", cl::Hidden, cl::desc("Print attribute dependencies"), cl::init(false))
static bool isAssumedReadOnlyOrReadNone(Attributor &A, const IRPosition &IRP, const AbstractAttribute &QueryingAA, bool RequireReadNone, bool &IsKnown)
static cl::opt< std::string > DepGraphDotFileNamePrefix("attributor-depgraph-dot-filename-prefix", cl::Hidden, cl::desc("The prefix used for the CallGraph dot file names."))
static cl::opt< bool > AnnotateDeclarationCallSites("attributor-annotate-decl-cs", cl::Hidden, cl::desc("Annotate call sites of function declarations."), cl::init(false))
static cl::opt< unsigned > SetFixpointIterations("attributor-max-iterations", cl::Hidden, cl::desc("Maximal number of fixpoint iterations."), cl::init(32))
static cl::list< std::string > FunctionSeedAllowList("attributor-function-seed-allow-list", cl::Hidden, cl::desc("Comma separated list of function names that are " "allowed to be seeded."), cl::CommaSeparated)
static cl::opt< bool > EnableCallSiteSpecific("attributor-enable-call-site-specific-deduction", cl::Hidden, cl::desc("Allow the Attributor to do call site specific analysis"), cl::init(false))
static cl::opt< bool > CloseWorldAssumption("attributor-assume-closed-world", cl::Hidden, cl::desc("Should a closed world be assumed, or not. Default if not set."))
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
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...
This file provides an implementation of debug counters.
#define DEBUG_COUNTER(VARNAME, COUNTERNAME, DESC)
IRTranslator LLVM IR MI
#define F(x, y, z)
Definition MD5.cpp:55
#define I(x, y, z)
Definition MD5.cpp:58
#define T
Contains a collection of routines for determining if a given instruction is guaranteed to execute if ...
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
uint64_t IntrinsicInst * II
FunctionAnalysisManager FAM
This file defines the PointerIntPair class.
static StringRef getName(Value *V)
Remove Loads Into Fake Uses
This file contains some templates that are useful if you are working with the STL at all.
This file defines the SmallPtrSet 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:167
#define LLVM_DEBUG(...)
Definition Debug.h:119
#define DEBUG_WITH_TYPE(TYPE,...)
DEBUG_WITH_TYPE macro - This macro should be used by passes to emit debug information.
Definition Debug.h:77
void print(OutputBuffer &OB) const
Class for arbitrary precision integers.
Definition APInt.h:78
CallBase * getInstruction() const
Return the underlying instruction.
bool isCallbackCall() const
Return true if this ACS represents a callback call.
const Use & getCalleeUseForCallback() const
Return the use of the callee value in the underlying instruction.
static LLVM_ABI void getCallbackUses(const CallBase &CB, SmallVectorImpl< const Use * > &CallbackUses)
Add operand uses of CB that represent callback uses into CallbackUses.
bool isCallee(Value::const_user_iterator UI) const
Return true if UI is the use that defines the callee of this ACS.
Value * getCallArgOperand(Argument &Arg) const
Return the operand of the underlying instruction associated with Arg.
int getCallArgOperandNo(Argument &Arg) const
Return the operand index of the underlying instruction associated with Arg.
unsigned getNumArgOperands() const
Return the number of parameters of the callee.
Function * getCalledFunction() const
Return the function being called if this is a direct call, otherwise return null (if it's an indirect...
This templated class represents "all analyses that operate over <aparticular IR unit>" (e....
Definition Analysis.h:50
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
This class represents an incoming formal argument to a Function.
Definition Argument.h:32
const Function * getParent() const
Definition Argument.h:44
unsigned getArgNo() const
Return the index of this formal argument in its containing function.
Definition Argument.h:50
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:41
size_t size() const
size - Get the array size.
Definition ArrayRef.h:147
bool empty() const
empty - Check if the array is empty.
Definition ArrayRef.h:142
This class stores enough information to efficiently remove some attributes from an existing AttrBuild...
This class holds the attributes for a particular argument, parameter, function, or return value.
Definition Attributes.h:361
LLVM_ABI MemoryEffects getMemoryEffects() const
LLVM_ABI bool hasAttribute(Attribute::AttrKind Kind) const
Return true if the attribute exists in this set.
LLVM_ABI Attribute getAttribute(Attribute::AttrKind Kind) const
Return the attribute object.
Functions, function parameters, and return types can have attributes to indicate how they should be t...
Definition Attributes.h:69
LLVM_ABI bool isStringAttribute() const
Return true if the attribute is a string (target-dependent) attribute.
LLVM_ABI bool isEnumAttribute() const
Return true if the attribute is an Attribute::AttrKind type.
LLVM_ABI bool isIntAttribute() const
Return true if the attribute is an integer attribute.
LLVM_ABI uint64_t getValueAsInt() const
Return the attribute's value as an integer.
LLVM_ABI bool isConstantRangeAttribute() const
Return true if the attribute is a ConstantRange attribute.
LLVM_ABI StringRef getKindAsString() const
Return the attribute's kind as a string.
static LLVM_ABI Attribute get(LLVMContext &Context, AttrKind Kind, uint64_t Val=0)
Return a uniquified Attribute object.
LLVM_ABI Attribute::AttrKind getKindAsEnum() const
Return the attribute's kind as an enum (Attribute::AttrKind).
LLVM_ABI MemoryEffects getMemoryEffects() const
Returns memory effects.
LLVM_ABI StringRef getValueAsString() const
Return the attribute's value as a string.
AttrKind
This enumeration lists the attributes that can be associated with parameters, function results,...
Definition Attributes.h:88
@ None
No attributes have been set.
Definition Attributes.h:90
LLVM Basic Block Representation.
Definition BasicBlock.h:62
const Function * getParent() const
Return the enclosing method, or null if none.
Definition BasicBlock.h:213
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr)
Creates a new BasicBlock.
Definition BasicBlock.h:206
const Instruction & front() const
Definition BasicBlock.h:482
LLVM_ABI const BasicBlock * getUniquePredecessor() const
Return the predecessor of this block if it has a unique predecessor block.
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:233
Represents analyses that only rely on functions' control flow.
Definition Analysis.h:73
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
void setCallingConv(CallingConv::ID CC)
void addFnAttr(Attribute::AttrKind Kind)
Adds the attribute to the function.
LLVM_ABI void getOperandBundlesAsDefs(SmallVectorImpl< OperandBundleDef > &Defs) const
Return the list of operand bundles attached to this instruction as a vector of OperandBundleDefs.
CallingConv::ID getCallingConv() const
LLVM_ABI bool isMustTailCall() const
Tests if this call site must be tail call optimized.
Value * getCalledOperand() const
void setAttributes(AttributeList A)
Set the attributes for this call.
unsigned arg_size() const
AttributeList getAttributes() const
Return the attributes for this call.
LLVM_ABI Function * getCaller()
Helper to get the caller (the parent function).
Wrapper to unify "old style" CallGraph and "new style" LazyCallGraph.
LLVM_ABI void replaceFunctionWith(Function &OldFn, Function &NewFn)
Replace OldFn in the call graph (and SCC) with NewFn.
LLVM_ABI void reanalyzeFunction(Function &Fn)
After an CGSCC pass changes a function in ways that affect the call graph, this method can be called ...
void initialize(LazyCallGraph &LCG, LazyCallGraph::SCC &SCC, CGSCCAnalysisManager &AM, CGSCCUpdateResult &UR)
Initializers for usage outside of a CGSCC pass, inside a CGSCC pass in the old and new pass manager (...
This class represents a function call, abstracting a target machine's calling convention.
static CallInst * Create(FunctionType *Ty, Value *F, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
void setTailCall(bool IsTc=true)
A constant value that is initialized with an expression using other constant values.
Definition Constants.h:1120
static LLVM_ABI Constant * getPointerCast(Constant *C, Type *Ty)
Create a BitCast, AddrSpaceCast, or a PtrToInt cast constant expression.
static LLVM_ABI Constant * getTrunc(Constant *C, Type *Ty, bool OnlyIfReduced=false)
LLVM_ABI void print(raw_ostream &OS) const
Print out the bounds to a stream.
This is an important base class in LLVM.
Definition Constant.h:43
static LLVM_ABI Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:63
static bool shouldExecute(unsigned CounterName)
ValueT lookup(const_arg_type_t< KeyT > Val) const
lookup - Return the entry for the specified key, or a default constructed value if no such entry exis...
Definition DenseMap.h:187
bool empty() const
Definition DenseMap.h:107
Implements a dense probed hash-table based set.
Definition DenseSet.h:261
Analysis pass which computes a DominatorTree.
Definition Dominators.h:284
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition Dominators.h:165
A proxy from a FunctionAnalysisManager to an SCC.
Class to represent function types.
static LLVM_ABI FunctionType * get(Type *Result, ArrayRef< Type * > Params, bool isVarArg)
This static method is the primary way of constructing a FunctionType.
void setSubprogram(DISubprogram *SP)
Set the attached subprogram.
static Function * Create(FunctionType *Ty, LinkageTypes Linkage, unsigned AddrSpace, const Twine &N="", Module *M=nullptr)
Definition Function.h:166
void splice(Function::iterator ToIt, Function *FromF)
Transfer all blocks from FromF to this function at ToIt.
Definition Function.h:759
const BasicBlock & getEntryBlock() const
Definition Function.h:807
FunctionType * getFunctionType() const
Returns the FunctionType for me.
Definition Function.h:209
iterator_range< arg_iterator > args()
Definition Function.h:890
DISubprogram * getSubprogram() const
Get the attached subprogram.
MemoryEffects getMemoryEffects() const
Definition Function.cpp:872
bool hasParamAttribute(unsigned ArgNo, Attribute::AttrKind Kind) const
check if an attributes is in the list of attributes.
Definition Function.cpp:739
AttributeList getAttributes() const
Return the attribute list for this Function.
Definition Function.h:352
iterator begin()
Definition Function.h:851
arg_iterator arg_begin()
Definition Function.h:866
void setAttributes(AttributeList Attrs)
Set the attribute list for this Function.
Definition Function.h:355
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function.
Definition Function.cpp:359
size_t arg_size() const
Definition Function.h:899
Type * getReturnType() const
Returns the type of the ret val.
Definition Function.h:214
void setMemoryEffects(MemoryEffects ME)
Definition Function.cpp:875
Argument * getArg(unsigned i) const
Definition Function.h:884
bool isVarArg() const
isVarArg - Return true if this function takes a variable number of arguments.
Definition Function.h:227
void copyAttributesFrom(const Function *Src)
copyAttributesFrom - copy all additional attributes (those not needed to create a Function) from the ...
Definition Function.cpp:856
bool hasMetadata() const
Return true if this value has any metadata attached to it.
Definition Value.h:602
LLVM_ABI void addMetadata(unsigned KindID, MDNode &MD)
Add a metadata attachment.
LLVM_ABI bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
Definition Globals.cpp:316
LinkageTypes getLinkage() const
bool hasLocalLinkage() const
void setLinkage(LinkageTypes LT)
unsigned getAddressSpace() const
Module * getParent()
Get the module that this global value is contained inside of...
void setDSOLocal(bool Local)
PointerType * getType() const
Global values are always pointers.
@ DefaultVisibility
The GV is visible.
Definition GlobalValue.h:68
void setVisibility(VisibilityTypes V)
static bool isInterposableLinkage(LinkageTypes Linkage)
Whether the definition of this global may be replaced by something non-equivalent at link time.
@ PrivateLinkage
Like Internal, but omit from symbol table.
Definition GlobalValue.h:61
@ InternalLinkage
Rename collisions when linking (static functions).
Definition GlobalValue.h:60
LLVM_ABI InstListType::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
LLVM_ABI const Function * getFunction() const
Return the function this instruction belongs to.
LLVM_ABI void copyMetadata(const Instruction &SrcInst, ArrayRef< unsigned > WL=ArrayRef< unsigned >())
Copy metadata from SrcInst to this instruction.
static InvokeInst * Create(FunctionType *Ty, Value *Func, BasicBlock *IfNormal, BasicBlock *IfException, ArrayRef< Value * > Args, const Twine &NameStr, InsertPosition InsertBefore=nullptr)
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:68
A node in the call graph.
An SCC of the call graph.
A lazily constructed view of the call graph of a module.
An instruction for reading from memory.
This is the common base class for memset/memcpy/memmove.
This class wraps the llvm.memcpy/memmove intrinsics.
static MemoryEffectsBase argMemOnly(ModRefInfo MR=ModRefInfo::ModRef)
Definition ModRef.h:135
bool doesAccessArgPointees() const
Whether this function may access argument memory.
Definition ModRef.h:229
static LLVM_ABI MemoryLocation getForSource(const MemTransferInst *MTI)
Return a location representing the source of a memory transfer.
static LLVM_ABI MemoryLocation getForDest(const MemIntrinsic *MI)
Return a location representing the destination of a memory set or transfer.
static LLVM_ABI std::optional< MemoryLocation > getOrNone(const Instruction *Inst)
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
const FunctionListType & getFunctionList() const
Get the Module's list of functions (constant).
Definition Module.h:596
PointerIntPair - This class implements a pair of a pointer and small integer.
void * getOpaqueValue() const
PointerTy getPointer() const
static LLVM_ABI PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
Definition Analysis.h:115
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
PreservedAnalyses & preserveSet()
Mark an analysis set as preserved.
Definition Analysis.h:151
PreservedAnalyses & preserve()
Mark an analysis as preserved.
Definition Analysis.h:132
Return a value (possibly void), from a function.
static ReturnInst * Create(LLVMContext &C, Value *retVal=nullptr, InsertPosition InsertBefore=nullptr)
A vector that has set insertion semantics.
Definition SetVector.h:59
ArrayRef< value_type > getArrayRef() const
Definition SetVector.h:90
bool remove(const value_type &X)
Remove an item from the set vector.
Definition SetVector.h:198
size_type size() const
Determine the number of elements in the SetVector.
Definition SetVector.h:104
void insert_range(Range &&R)
Definition SetVector.h:193
typename vector_type::const_iterator iterator
Definition SetVector.h:71
void clear()
Completely clear the SetVector.
Definition SetVector.h:284
size_type count(const key_type &key) const
Count the number of elements of a given key in the SetVector.
Definition SetVector.h:279
iterator begin()
Get an iterator to the beginning of the SetVector.
Definition SetVector.h:109
bool insert(const value_type &X)
Insert a new element into the SetVector.
Definition SetVector.h:168
size_type size() const
Definition SmallPtrSet.h:99
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
A SetVector that performs no allocations if smaller than a certain size.
Definition SetVector.h:356
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void reserve(size_type N)
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
void resize(size_type N)
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
An instruction for storing to memory.
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
A visitor class for IR positions.
LLVM_ABI SubsumingPositionIterator(const IRPosition &IRP)
Provides information about what library functions are available for the current target.
The TimeTraceScope is a helper class to call the begin and end functions of the time trace profiler.
Triple - Helper class for working with autoconf configuration names.
Definition Triple.h:47
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:45
bool isPointerTy() const
True if this is an instance of PointerType.
Definition Type.h:267
LLVM_ABI unsigned getPointerAddressSpace() const
Get the address space of this pointer or pointer vector type.
bool isVoidTy() const
Return true if this is 'void'.
Definition Type.h:139
static LLVM_ABI UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
A Use represents the edge between a Value definition and its users.
Definition Use.h:35
LLVM Value Representation.
Definition Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:256
LLVM_ABI void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition Value.cpp:546
LLVM_ABI const Value * stripPointerCasts() const
Strip off pointer casts, all-zero GEPs and address space casts.
Definition Value.cpp:701
LLVM_ABI LLVMContext & getContext() const
All values hold a context through their type.
Definition Value.cpp:1101
iterator_range< use_iterator > uses()
Definition Value.h:380
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
Definition Value.cpp:322
LLVM_ABI void takeName(Value *V)
Transfer the name from V to this value.
Definition Value.cpp:396
An efficient, type-erasing, non-owning reference to a callable.
const ParentTy * getParent() const
Definition ilist_node.h:34
self_iterator getIterator()
Definition ilist_node.h:134
NodeTy * getNextNode()
Get the next node, or nullptr for the list tail.
Definition ilist_node.h:359
iterator insert(iterator where, pointer New)
Definition ilist.h:165
A raw_ostream that writes to a file descriptor.
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
A raw_ostream that writes to an std::string.
CallInst * Call
Changed
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Abstract Attribute helper functions.
Definition Attributor.h:165
LLVM_ABI bool isAssumedReadNone(Attributor &A, const IRPosition &IRP, const AbstractAttribute &QueryingAA, bool &IsKnown)
Return true if IRP is readnone.
LLVM_ABI bool isAssumedReadOnly(Attributor &A, const IRPosition &IRP, const AbstractAttribute &QueryingAA, bool &IsKnown)
Return true if IRP is readonly.
LLVM_ABI std::optional< Value * > combineOptionalValuesInAAValueLatice(const std::optional< Value * > &A, const std::optional< Value * > &B, Type *Ty)
Return the combination of A and B such that the result is a possible value of both.
LLVM_ABI bool isValidAtPosition(const ValueAndContext &VAC, InformationCache &InfoCache)
Return true if the value of VAC is a valid at the position of VAC, that is a constant,...
LLVM_ABI bool isAssumedThreadLocalObject(Attributor &A, Value &Obj, const AbstractAttribute &QueryingAA)
Return true if Obj is assumed to be a thread local object.
LLVM_ABI bool isDynamicallyUnique(Attributor &A, const AbstractAttribute &QueryingAA, const Value &V, bool ForAnalysisOnly=true)
Return true if V is dynamically unique, that is, there are no two "instances" of V at runtime with di...
LLVM_ABI bool getPotentialCopiesOfStoredValue(Attributor &A, StoreInst &SI, SmallSetVector< Value *, 4 > &PotentialCopies, const AbstractAttribute &QueryingAA, bool &UsedAssumedInformation, bool OnlyExact=false)
Collect all potential values of the one stored by SI into PotentialCopies.
LLVM_ABI bool isPotentiallyAffectedByBarrier(Attributor &A, const Instruction &I, const AbstractAttribute &QueryingAA)
Return true if I is potentially affected by a barrier.
SmallPtrSet< Instruction *, 4 > InstExclusionSetTy
Definition Attributor.h:166
LLVM_ABI bool isGPU(const Module &M)
Return true iff M target a GPU (and we can use GPU AS reasoning).
LLVM_ABI Constant * getInitialValueForObj(Attributor &A, const AbstractAttribute &QueryingAA, Value &Obj, Type &Ty, const TargetLibraryInfo *TLI, const DataLayout &DL, RangeTy *RangePtr=nullptr)
Return the initial value of Obj with type Ty if that is a constant.
ValueScope
Flags to distinguish intra-procedural queries from potentially inter-procedural queries.
Definition Attributor.h:182
@ Intraprocedural
Definition Attributor.h:183
@ Interprocedural
Definition Attributor.h:184
LLVM_ABI bool isValidInScope(const Value &V, const Function *Scope)
Return true if V is a valid value in Scope, that is a constant or an instruction/argument of Scope.
LLVM_ABI bool isPotentiallyReachable(Attributor &A, const Instruction &FromI, const Instruction &ToI, const AbstractAttribute &QueryingAA, const AA::InstExclusionSetTy *ExclusionSet=nullptr, std::function< bool(const Function &F)> GoBackwardsCB=nullptr)
Return true if ToI is potentially reachable from FromI without running into any instruction in Exclus...
LLVM_ABI bool isNoSyncInst(Attributor &A, const Instruction &I, const AbstractAttribute &QueryingAA)
Return true if I is a nosync instruction.
bool hasAssumedIRAttr(Attributor &A, const AbstractAttribute *QueryingAA, const IRPosition &IRP, DepClassTy DepClass, bool &IsKnown, bool IgnoreSubsumingPositions=false, const AAType **AAPtr=nullptr)
Helper to avoid creating an AA for IR Attributes that might already be set.
LLVM_ABI bool getPotentiallyLoadedValues(Attributor &A, LoadInst &LI, SmallSetVector< Value *, 4 > &PotentialValues, SmallSetVector< Instruction *, 4 > &PotentialValueOrigins, const AbstractAttribute &QueryingAA, bool &UsedAssumedInformation, bool OnlyExact=false)
Collect all potential values LI could read into PotentialValues.
LLVM_ABI Value * getWithType(Value &V, Type &Ty)
Try to convert V to type Ty without introducing new instructions.
constexpr char Attrs[]
Key for Kernel::Metadata::mAttrs.
@ Entry
Definition COFF.h:862
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
@ BasicBlock
Various leaf nodes.
Definition ISDOpcodes.h:81
initializer< Ty > init(const Ty &Val)
LocationClass< Ty > location(Ty &L)
@ Assume
Do not drop type tests (default).
DiagnosticInfoOptimizationBase::Argument NV
NodeAddr< UseNode * > Use
Definition RDFGraph.h:385
friend class Instruction
Iterator for Instructions in a `BasicBlock.
Definition BasicBlock.h:73
@ OF_TextWithCRLF
The file should be opened in text mode and use a carriage linefeed '\r '.
Definition FileSystem.h:771
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:477
FunctionAddr VTableAddr Value
Definition InstrProf.h:137
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:1727
LLVM_ABI Constant * getInitialValueOfAllocation(const Value *V, const TargetLibraryInfo *TLI, Type *Ty)
If this is a call to an allocation function that initializes memory to a fixed value,...
Printable print(const GCNRegPressure &RP, const GCNSubtarget *ST=nullptr, unsigned DynamicVGPRBlockSize=0)
LLVM_ABI bool RecursivelyDeleteTriviallyDeadInstructions(Value *V, const TargetLibraryInfo *TLI=nullptr, MemorySSAUpdater *MSSAU=nullptr, std::function< void(Value *)> AboutToDeleteCallback=std::function< void(Value *)>())
If the specified value is a trivially dead instruction, delete it.
Definition Local.cpp:533
LLVM_ABI unsigned MaxInitializationChainLength
The value passed to the line option that defines the maximal initialization chain length.
LLVM_ABI bool ConstantFoldTerminator(BasicBlock *BB, bool DeleteDeadConditions=false, const TargetLibraryInfo *TLI=nullptr, DomTreeUpdater *DTU=nullptr)
If a terminator instruction is predicated on a constant value, convert it into an unconditional branc...
Definition Local.cpp:134
APInt operator&(APInt a, const APInt &b)
Definition APInt.h:2123
LLVM_ABI void detachDeadBlocks(ArrayRef< BasicBlock * > BBs, SmallVectorImpl< DominatorTree::UpdateType > *Updates, bool KeepOneInputPHIs=false)
Replace contents of every block in BBs with single unreachable instruction.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:649
@ Done
Definition Threading.h:60
DenseMap< AssumeInst *, MinMax > Assume2KnowledgeMap
A mapping from intrinsics (=llvm.assume calls) to a value range (=knowledge) that is encoded in them.
LLVM_ABI bool verifyFunction(const Function &F, raw_ostream *OS=nullptr)
Check a function for errors, useful for use when debugging a pass.
LLVM_ABI CallInst * changeToCall(InvokeInst *II, DomTreeUpdater *DTU=nullptr)
This function converts the specified invoke into a normal call.
Definition Local.cpp:2579
LLVM_ABI raw_fd_ostream & outs()
This returns a reference to a raw_fd_ostream for standard output.
auto dyn_cast_if_present(const Y &Val)
dyn_cast_if_present<X> - Functionally identical to dyn_cast, except that a null (or none in the case ...
Definition Casting.h:738
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
InnerAnalysisManagerProxy< FunctionAnalysisManager, Module > FunctionAnalysisManagerModuleProxy
Provide the FunctionAnalysisManager to Module proxy.
LLVM_ABI bool isNoAliasCall(const Value *V)
Return true if this pointer is returned by a noalias function.
MemoryEffectsBase< IRMemLocation > MemoryEffects
Summary of how a function affects memory in the program.
Definition ModRef.h:296
raw_ostream & WriteGraph(raw_ostream &O, const GraphType &G, bool ShortNames=false, const Twine &Title="")
bool isa_and_nonnull(const Y &Val)
Definition Casting.h:682
AnalysisManager< LazyCallGraph::SCC, LazyCallGraph & > CGSCCAnalysisManager
The CGSCC analysis manager.
LLVM_ABI InlineResult isInlineViable(Function &Callee)
Check if it is mechanically possible to inline the function Callee, based on the contents of the func...
auto dyn_cast_or_null(const Y &Val)
Definition Casting.h:759
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:1734
LLVM_ABI bool isInstructionTriviallyDead(Instruction *I, const TargetLibraryInfo *TLI=nullptr)
Return true if the result produced by the instruction is not used, and the instruction will return.
Definition Local.cpp:402
LLVM_ABI Constant * ConstantFoldLoadFromUniformValue(Constant *C, Type *Ty, const DataLayout &DL)
If C is a uniform value where all bits are the same (either all zero, all ones, all undef or all pois...
PotentialValuesState< std::pair< AA::ValueAndContext, AA::ValueScope > > PotentialLLVMValuesState
decltype(auto) get(const PointerIntPair< PointerTy, IntBits, IntType, PtrTraits, Info > &Pair)
LLVM_ABI bool NullPointerIsDefined(const Function *F, unsigned AS=0)
Check whether null pointer dereferencing is considered undefined behavior for a given function or an ...
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:207
LLVM_ABI bool AreStatisticsEnabled()
Check if statistics are enabled.
BumpPtrAllocatorImpl BumpPtrAllocator
The standard BumpPtrAllocator which just uses the default template parameters.
Definition Allocator.h:383
LLVM_ABI Constant * ConstantFoldLoadFromConst(Constant *C, Type *Ty, const APInt &Offset, const DataLayout &DL)
Extract value of C at the given Offset reinterpreted as Ty.
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:548
@ Success
The lock was released successfully.
LLVM_ABI unsigned changeToUnreachable(Instruction *I, bool PreserveLCSSA=false, DomTreeUpdater *DTU=nullptr, MemorySSAUpdater *MSSAU=nullptr)
Insert an unreachable instruction before the specified instruction, making it and the rest of the cod...
Definition Local.cpp:2513
LLVM_ABI raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
LLVM_ABI BasicBlock * SplitBlockPredecessors(BasicBlock *BB, ArrayRef< BasicBlock * > Preds, const char *Suffix, DominatorTree *DT, LoopInfo *LI=nullptr, MemorySSAUpdater *MSSAU=nullptr, bool PreserveLCSSA=false)
This method introduces at least one new basic block into the function and moves some of the predecess...
PotentialValuesState< APInt > PotentialConstantIntValuesState
bool operator&=(SparseBitVector< ElementSize > *LHS, const SparseBitVector< ElementSize > &RHS)
DWARFExpression::Operation Op
void ViewGraph(const GraphType &G, const Twine &Name, bool ShortNames=false, const Twine &Title="", GraphProgram::Name Program=GraphProgram::DOT)
ViewGraph - Emit a dot graph, run 'dot', run gv on the postscript file, then cleanup.
raw_ostream & operator<<(raw_ostream &OS, const APFixedPoint &FX)
ArrayRef(const T &OneElt) -> ArrayRef< T >
ValueMap< const Value *, WeakTrackingVH > ValueToValueMapTy
LLVM_ABI void CloneFunctionInto(Function *NewFunc, const Function *OldFunc, ValueToValueMapTy &VMap, CloneFunctionChangeType Changes, SmallVectorImpl< ReturnInst * > &Returns, const char *NameSuffix="", ClonedCodeInfo *CodeInfo=nullptr, ValueMapTypeRemapper *TypeMapper=nullptr, ValueMaterializer *Materializer=nullptr)
Clone OldFunc into NewFunc, transforming the old arguments into references to VMap values.
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:565
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:2122
iterator_range< pointer_iterator< WrappedIteratorT > > make_pointer_range(RangeT &&Range)
Definition iterator.h:363
LLVM_ABI bool isAllocationFn(const Value *V, const TargetLibraryInfo *TLI)
Tests if a value is a call or invoke to a library function that allocates or reallocates memory (eith...
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition STLExtras.h:1899
ChangeStatus
{
Definition Attributor.h:496
LLVM_ABI void fillMapFromAssume(AssumeInst &Assume, RetainedKnowledgeMap &Result)
Insert into the map all the informations contained in the operand bundles of the llvm....
bool operator|=(SparseBitVector< ElementSize > &LHS, const SparseBitVector< ElementSize > *RHS)
LLVM_ABI Constant * ConstantFoldCastInstruction(unsigned opcode, Constant *V, Type *DestTy)
@ OPTIONAL
The target may be valid if the source is not.
Definition Attributor.h:508
@ NONE
Do not track a dependence between source and target.
Definition Attributor.h:509
@ REQUIRED
The target cannot be valid if the source is not.
Definition Attributor.h:507
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
APInt operator|(APInt a, const APInt &b)
Definition APInt.h:2143
AnalysisManager< Module > ModuleAnalysisManager
Convenience typedef for the Module analysis manager.
Definition MIRParser.h:39
LLVM_ABI bool isPotentiallyReachable(const Instruction *From, const Instruction *To, const SmallPtrSetImpl< BasicBlock * > *ExclusionSet=nullptr, const DominatorTree *DT=nullptr, const LoopInfo *LI=nullptr)
Determine whether instruction 'To' is reachable from 'From', without passing through any blocks in Ex...
Definition CFG.cpp:282
#define N
static LLVM_ABI const char ID
Unique ID (due to the unique address)
DepSetTy Deps
Set of dependency graph nodes which should be updated if this one is updated.
Definition Attributor.h:523
PointerIntPair< AADepGraphNode *, 1 > DepTy
Definition Attributor.h:517
The data structure for the dependency graph.
Definition Attributor.h:557
iterator begin()
Definition Attributor.h:572
LLVM_ABI void viewGraph()
AADepGraphNode SyntheticRoot
There is no root node for the dependency graph.
Definition Attributor.h:569
LLVM_ABI void print()
Print dependency graph.
iterator end()
Definition Attributor.h:573
LLVM_ABI void dumpGraph()
Dump graph to file.
AADepGraphNode * GetEntryNode()
Definition Attributor.h:570
An abstract interface to track if a value leaves it's defining function instance.
bool isAssumedUniqueForAnalysis() const
Return true if we assume that the underlying value is unique in its scope wrt.
An abstract Attribute for computing reachability between functions.
static LLVM_ABI const char ID
Unique ID (due to the unique address)
An abstract interface to determine reachability of point A to B.
static LLVM_ABI const char ID
Unique ID (due to the unique address)
An abstract interface for liveness abstract attribute.
virtual bool isKnownDead() const =0
Returns true if the underlying value is known dead.
virtual bool isAssumedDead() const =0
The query functions are protected such that other attributes need to go through the Attributor interf...
virtual bool isRemovableStore() const
Return true if the underlying value is a store that is known to be removable.
static bool mayCatchAsynchronousExceptions(const Function &F)
Determine if F might catch asynchronous exceptions.
An abstract interface for memory access kind related attributes (readnone/readonly/writeonly).
static LLVM_ABI const char ID
Unique ID (due to the unique address)
An abstract interface for all memory location attributes (readnone/argmemonly/inaccessiblememonly/ina...
static LLVM_ABI const char ID
Unique ID (due to the unique address)
static LLVM_ABI const char ID
Unique ID (due to the unique address)
static LLVM_ABI const char ID
Unique ID (due to the unique address)
static LLVM_ABI const char ID
Unique ID (due to the unique address)
static LLVM_ABI const char ID
Unique ID (due to the unique address)
static LLVM_ABI const char ID
Unique ID (due to the unique address)
static LLVM_ABI const char ID
Unique ID (due to the unique address)
static LLVM_ABI const char ID
Unique ID (due to the unique address)
static LLVM_ABI bool isNonRelaxedAtomic(const Instruction *I)
Helper function used to determine whether an instruction is non-relaxed atomic.
static LLVM_ABI bool isNoSyncIntrinsic(const Instruction *I)
Helper function specific for intrinsics which are potentially volatile.
static LLVM_ABI const char ID
Unique ID (due to the unique address)
static LLVM_ABI const char ID
Unique ID (due to the unique address)
An access description.
bool isWrittenValueUnknown() const
Return true if the value written cannot be determined at all.
std::optional< Value * > getContent() const
Return the written value which can be llvm::null if it is not yet determined.
bool isWriteOrAssumption() const
Return true if this is a write access.
bool isRead() const
Return true if this is a read access.
Value * getWrittenValue() const
Return the value writen, if any.
Instruction * getLocalInst() const
Return the instruction that causes the access with respect to the local scope of the associated attri...
Instruction * getRemoteInst() const
Return the actual instruction that causes the access.
bool isWrittenValueYetUndetermined() const
Return true if the value written is not known yet.
AccessKind getKind() const
Return the access kind.
An abstract interface for struct information.
static LLVM_ABI Value * getSingleValue(Attributor &A, const AbstractAttribute &AA, const IRPosition &IRP, SmallVectorImpl< AA::ValueAndContext > &Values)
Extract the single value in Values if any.
An abstract attribute for getting all assumption underlying objects.
static LLVM_ABI const char ID
Unique ID (due to the unique address)
static LLVM_ABI const char ID
Unique ID (due to the unique address)
Helper to represent an access offset and size, with logic to deal with uncertainty and check for over...
Definition Attributor.h:241
bool offsetOrSizeAreUnknown() const
Return true if offset or size are unknown.
Definition Attributor.h:250
static LLVM_ABI const fltSemantics & IEEEsingle() LLVM_READNONE
Definition APFloat.cpp:266
Base struct for all "concrete attribute" deductions.
ChangeStatus update(Attributor &A)
Hook for the Attributor to trigger an update of the internal state.
friend struct Attributor
}
virtual void printWithDeps(raw_ostream &OS) const
void print(raw_ostream &OS) const
Helper functions, for debug purposes only.
virtual StateType & getState()=0
Return the internal abstract state for inspection.
virtual const std::string getAsStr(Attributor *A) const =0
This function should return the "summarized" assumed state as string.
virtual ChangeStatus updateImpl(Attributor &A)=0
The actual update/transfer function which has to be implemented by the derived classes.
const IRPosition & getIRPosition() const
Return an IR position, see struct IRPosition.
An interface to query the internal state of an abstract attribute.
virtual ChangeStatus indicatePessimisticFixpoint()=0
Indicate that the abstract state should converge to the pessimistic state.
virtual bool isAtFixpoint() const =0
Return if this abstract state is fixed, thus does not need to be updated if information changes as it...
virtual bool isValidState() const =0
Return if this abstract state is in a valid state.
Wrapper for FunctionAnalysisManager.
LLVM_ABI PreservedAnalyses run(LazyCallGraph::SCC &C, CGSCCAnalysisManager &AM, LazyCallGraph &CG, CGSCCUpdateResult &UR)
void populateAll() const
Force populate the entire call graph.
Configuration for the Attributor.
std::optional< unsigned > MaxFixpointIterations
Maximum number of iterations to run until fixpoint.
LLVM_ABI PreservedAnalyses run(LazyCallGraph::SCC &C, CGSCCAnalysisManager &AM, LazyCallGraph &CG, CGSCCUpdateResult &UR)
LLVM_ABI PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
LLVM_ABI PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
Helper struct used in the communication between an abstract attribute (AA) that wants to change the s...
std::function< void( const ArgumentReplacementInfo &, Function &, Function::arg_iterator)> CalleeRepairCBTy
Callee repair callback type.
std::function< void(const ArgumentReplacementInfo &, AbstractCallSite, SmallVectorImpl< Value * > &)> ACSRepairCBTy
Abstract call site (ACS) repair callback type.
The fixpoint analysis framework that orchestrates the attribute deduction.
LLVM_ABI bool registerFunctionSignatureRewrite(Argument &Arg, ArrayRef< Type * > ReplacementTypes, ArgumentReplacementInfo::CalleeRepairCBTy &&CalleeRepairCB, ArgumentReplacementInfo::ACSRepairCBTy &&ACSRepairCB)
Register a rewrite for a function signature.
LLVM_ABI ~Attributor()
LLVM_ABI bool checkForAllCallees(function_ref< bool(ArrayRef< const Function * > Callees)> Pred, const AbstractAttribute &QueryingAA, const CallBase &CB)
Check Pred on all potential Callees of CB.
bool isModulePass() const
Return true if this is a module pass, false otherwise.
LLVM_ABI bool isValidFunctionSignatureRewrite(Argument &Arg, ArrayRef< Type * > ReplacementTypes)
Check if we can rewrite a function signature.
static LLVM_ABI bool isInternalizable(Function &F)
Returns true if the function F can be internalized.
LLVM_ABI ChangeStatus removeAttrs(const IRPosition &IRP, ArrayRef< Attribute::AttrKind > AttrKinds)
Remove all AttrKinds attached to IRP.
void emitRemark(Instruction *I, StringRef RemarkName, RemarkCallBack &&RemarkCB) const
Emit a remark generically.
bool isRunOn(Function &Fn) const
Return true if we derive attributes for Fn.
LLVM_ABI bool isAssumedDead(const AbstractAttribute &AA, const AAIsDead *LivenessAA, bool &UsedAssumedInformation, bool CheckBBLivenessOnly=false, DepClassTy DepClass=DepClassTy::OPTIONAL)
Return true if AA (or its context instruction) is assumed dead.
LLVM_ABI bool checkForAllInstructions(function_ref< bool(Instruction &)> Pred, const Function *Fn, const AbstractAttribute *QueryingAA, ArrayRef< unsigned > Opcodes, bool &UsedAssumedInformation, bool CheckBBLivenessOnly=false, bool CheckPotentiallyDead=false)
Check Pred on all instructions in Fn with an opcode present in Opcodes.
LLVM_ABI void recordDependence(const AbstractAttribute &FromAA, const AbstractAttribute &ToAA, DepClassTy DepClass)
Explicitly record a dependence from FromAA to ToAA, that is if FromAA changes ToAA should be updated ...
static LLVM_ABI void createShallowWrapper(Function &F)
Create a shallow wrapper for F such that F has internal linkage afterwards.
const AAType * getAAFor(const AbstractAttribute &QueryingAA, const IRPosition &IRP, DepClassTy DepClass)
Lookup an abstract attribute of type AAType at position IRP.
std::optional< Value * > getAssumedSimplified(const IRPosition &IRP, const AbstractAttribute &AA, bool &UsedAssumedInformation, AA::ValueScope S)
If V is assumed simplified, return it, if it is unclear yet, return std::nullopt, otherwise return nu...
static LLVM_ABI Function * internalizeFunction(Function &F, bool Force=false)
Make another copy of the function F such that the copied version has internal linkage afterwards and ...
bool isFunctionIPOAmendable(const Function &F)
Determine whether the function F is IPO amendable.
const AAType * getOrCreateAAFor(IRPosition IRP, const AbstractAttribute *QueryingAA, DepClassTy DepClass, bool ForceUpdate=false, bool UpdateAfterInit=true)
The version of getAAFor that allows to omit a querying abstract attribute.
LLVM_ABI bool checkForAllReadWriteInstructions(function_ref< bool(Instruction &)> Pred, AbstractAttribute &QueryingAA, bool &UsedAssumedInformation)
Check Pred on all Read/Write instructions.
LLVM_ABI bool checkForAllReturnedValues(function_ref< bool(Value &)> Pred, const AbstractAttribute &QueryingAA, AA::ValueScope S=AA::ValueScope::Intraprocedural, bool RecurseForSelectAndPHI=true)
Check Pred on all values potentially returned by the function associated with QueryingAA.
LLVM_ABI bool isClosedWorldModule() const
Return true if the module contains the whole world, thus, no outside functions exist.
LLVM_ABI std::optional< Constant * > getAssumedConstant(const IRPosition &IRP, const AbstractAttribute &AA, bool &UsedAssumedInformation)
If IRP is assumed to be a constant, return it, if it is unclear yet, return std::nullopt,...
LLVM_ABI Attributor(SetVector< Function * > &Functions, InformationCache &InfoCache, AttributorConfig Configuration)
Constructor.
LLVM_ABI void getAttrs(const IRPosition &IRP, ArrayRef< Attribute::AttrKind > AKs, SmallVectorImpl< Attribute > &Attrs, bool IgnoreSubsumingPositions=false)
Return the attributes of any kind in AKs existing in the IR at a position that will affect this one.
InformationCache & getInfoCache()
Return the internal information cache.
LLVM_ABI std::optional< Value * > translateArgumentToCallSiteContent(std::optional< Value * > V, CallBase &CB, const AbstractAttribute &AA, bool &UsedAssumedInformation)
Translate V from the callee context into the call site context.
LLVM_ABI bool checkForAllUses(function_ref< bool(const Use &, bool &)> Pred, const AbstractAttribute &QueryingAA, const Value &V, bool CheckBBLivenessOnly=false, DepClassTy LivenessDepClass=DepClassTy::OPTIONAL, bool IgnoreDroppableUses=true, function_ref< bool(const Use &OldU, const Use &NewU)> EquivalentUseCB=nullptr)
Check Pred on all (transitive) uses of V.
LLVM_ABI ChangeStatus manifestAttrs(const IRPosition &IRP, ArrayRef< Attribute > DeducedAttrs, bool ForceReplace=false)
Attach DeducedAttrs to IRP, if ForceReplace is set we do this even if the same attribute kind was alr...
LLVM_ABI bool hasAttr(const IRPosition &IRP, ArrayRef< Attribute::AttrKind > AKs, bool IgnoreSubsumingPositions=false, Attribute::AttrKind ImpliedAttributeKind=Attribute::None)
Return true if any kind in AKs existing in the IR at a position that will affect this one.
LLVM_ABI void registerForUpdate(AbstractAttribute &AA)
Allows a query AA to request an update if a new query was received.
std::function< bool(Attributor &, const AbstractAttribute *)> VirtualUseCallbackTy
LLVM_ABI void identifyDefaultAbstractAttributes(Function &F)
Determine opportunities to derive 'default' attributes in F and create abstract attribute objects for...
LLVM_ABI bool getAssumedSimplifiedValues(const IRPosition &IRP, const AbstractAttribute *AA, SmallVectorImpl< AA::ValueAndContext > &Values, AA::ValueScope S, bool &UsedAssumedInformation, bool RecurseForSelectAndPHI=true)
Try to simplify IRP and in the scope S.
BumpPtrAllocator & Allocator
The allocator used to allocate memory, e.g. for AbstractAttributes.
LLVM_ABI ChangeStatus run()
Run the analyses until a fixpoint is reached or enforced (timeout).
static LLVM_ABI bool internalizeFunctions(SmallPtrSetImpl< Function * > &FnSet, DenseMap< Function *, Function * > &FnMap)
Make copies of each function in the set FnSet such that the copied version has internal linkage after...
LLVM_ABI bool checkForAllCallSites(function_ref< bool(AbstractCallSite)> Pred, const AbstractAttribute &QueryingAA, bool RequireAllCallSites, bool &UsedAssumedInformation)
Check Pred on all function call sites.
LLVM_ABI bool getAttrsFromAssumes(const IRPosition &IRP, Attribute::AttrKind AK, SmallVectorImpl< Attribute > &Attrs)
Return the attributes of kind AK existing in the IR as operand bundles of an llvm....
bool isKnown(base_t BitsEncoding=BestState) const
Return true if the bits set in BitsEncoding are "known bits".
Support structure for SCC passes to communicate updates the call graph back to the CGSCC pass manager...
static std::string getNodeLabel(const AADepGraphNode *Node, const AADepGraph *DG)
DefaultDOTGraphTraits(bool simple=false)
Represent subnormal handling kind for floating point instruction inputs and outputs.
@ Dynamic
Denormals have unknown treatment.
An information struct used to provide DenseMap with the various necessary components for a given valu...
static NodeRef DepGetVal(const DepTy &DT)
PointerIntPair< AADepGraphNode *, 1 > DepTy
static ChildIteratorType child_end(NodeRef N)
static NodeRef getEntryNode(AADepGraphNode *DGN)
mapped_iterator< AADepGraphNode::DepSetTy::iterator, decltype(&DepGetVal)> ChildIteratorType
PointerIntPair< AADepGraphNode *, 1 > EdgeRef
static ChildIteratorType child_begin(NodeRef N)
AADepGraphNode::DepSetTy::iterator ChildEdgeIteratorType
static NodeRef getEntryNode(AADepGraph *DG)
mapped_iterator< AADepGraphNode::DepSetTy::iterator, decltype(&DepGetVal)> nodes_iterator
static nodes_iterator nodes_begin(AADepGraph *DG)
static nodes_iterator nodes_end(AADepGraph *DG)
typename AADepGraph *::UnknownGraphTypeError NodeRef
Definition GraphTraits.h:95
Helper to describe and deal with positions in the LLVM-IR.
Definition Attributor.h:593
Function * getAssociatedFunction() const
Return the associated function, if any.
Definition Attributor.h:724
void setAttrList(const AttributeList &AttrList) const
Update the attributes associated with this function or call site scope.
Definition Attributor.h:860
unsigned getAttrIdx() const
Return the index in the attribute list for this position.
Definition Attributor.h:825
bool hasCallBaseContext() const
Check if the position has any call base context.
Definition Attributor.h:942
static const IRPosition callsite_returned(const CallBase &CB)
Create a position describing the returned value of CB.
Definition Attributor.h:661
static const IRPosition returned(const Function &F, const CallBaseContext *CBContext=nullptr)
Create a position describing the returned value of F.
Definition Attributor.h:643
LLVM_ABI Argument * getAssociatedArgument() const
Return the associated argument, if any.
static const IRPosition value(const Value &V, const CallBaseContext *CBContext=nullptr)
Create a position describing the value of V.
Definition Attributor.h:617
CallBase CallBaseContext
Definition Attributor.h:596
AttributeList getAttrList() const
Return the attributes associated with this function or call site scope.
Definition Attributor.h:853
static const IRPosition inst(const Instruction &I, const CallBaseContext *CBContext=nullptr)
Create a position describing the instruction I.
Definition Attributor.h:629
static const IRPosition callsite_argument(const CallBase &CB, unsigned ArgNo)
Create a position describing the argument of CB at position ArgNo.
Definition Attributor.h:666
static LLVM_ABI const IRPosition TombstoneKey
Definition Attributor.h:948
Kind
The positions we distinguish in the IR.
Definition Attributor.h:599
@ IRP_ARGUMENT
An attribute for a function argument.
Definition Attributor.h:607
@ IRP_RETURNED
An attribute for the function return value.
Definition Attributor.h:603
@ IRP_CALL_SITE
An attribute for a call site (function scope).
Definition Attributor.h:606
@ IRP_CALL_SITE_RETURNED
An attribute for a call site return value.
Definition Attributor.h:604
@ IRP_FUNCTION
An attribute for a function (scope).
Definition Attributor.h:605
@ IRP_FLOAT
A position that is not associated with a spot suitable for attributes.
Definition Attributor.h:601
@ IRP_CALL_SITE_ARGUMENT
An attribute for a call site argument.
Definition Attributor.h:608
@ IRP_INVALID
An invalid position.
Definition Attributor.h:600
Instruction * getCtxI() const
Return the context instruction, if any.
Definition Attributor.h:777
static const IRPosition argument(const Argument &Arg, const CallBaseContext *CBContext=nullptr)
Create a position describing the argument Arg.
Definition Attributor.h:650
static LLVM_ABI const IRPosition EmptyKey
Special DenseMap key values.
Definition Attributor.h:947
static const IRPosition function(const Function &F, const CallBaseContext *CBContext=nullptr)
Create a position describing the function scope of F.
Definition Attributor.h:636
const CallBaseContext * getCallBaseContext() const
Get the call base context from the position.
Definition Attributor.h:939
Value & getAssociatedValue() const
Return the value this abstract attribute is associated with.
Definition Attributor.h:791
Value & getAnchorValue() const
Return the value this abstract attribute is anchored with.
Definition Attributor.h:710
Value * getAttrListAnchor() const
Return the value attributes are attached to.
Definition Attributor.h:846
int getCallSiteArgNo() const
Return the call site argument number of the associated value if it is an argument or call site argume...
Definition Attributor.h:820
Kind getPositionKind() const
Return the associated position kind.
Definition Attributor.h:889
static const IRPosition callsite_function(const CallBase &CB)
Create a position describing the function scope of CB.
Definition Attributor.h:656
Function * getAnchorScope() const
Return the Function surrounding the anchor value.
Definition Attributor.h:765
Data structure to hold cached (LLVM-IR) information.
friend struct Attributor
Give the Attributor access to the members so Attributor::identifyDefaultAbstractAttributes(....
bool stackIsAccessibleByOtherThreads()
Return true if the stack (llvm::Alloca) can be accessed by other threads.
MustBeExecutedContextExplorer * getMustBeExecutedContextExplorer()
Return MustBeExecutedContextExplorer.
TargetLibraryInfo * getTargetLibraryInfoForFunction(const Function &F)
Return TargetLibraryInfo for function F.
LLVM_ABI std::optional< unsigned > getFlatAddressSpace() const
Return the flat address space if the associated target has.
DenseMap< unsigned, InstructionVectorTy * > OpcodeInstMapTy
A map type from opcodes to instructions with this opcode.
const RetainedKnowledgeMap & getKnowledgeMap() const
Return the map conaining all the knowledge we have from llvm.assumes.
LLVM_ABI ArrayRef< Function * > getIndirectlyCallableFunctions(Attributor &A) const
Return all functions that might be called indirectly, only valid for closed world modules (see isClos...
SmallVector< Instruction *, 8 > InstructionVectorTy
A vector type to hold instructions.
AP::Result * getAnalysisResultForFunction(const Function &F, bool CachedOnly=false)
Return the analysis result from a pass AP for function F.
State for an integer range.
ConstantRange getKnown() const
Return the known state encoding.
ConstantRange getAssumed() const
Return the assumed state encoding.
uint32_t getBitWidth() const
Return associated values' bit width.
A "must be executed context" for a given program point PP is the set of instructions,...
iterator & end()
Return an universal end iterator.
bool findInContextOf(const Instruction *I, const Instruction *PP)
Helper to look for I in the context of PP.
iterator & begin(const Instruction *PP)
Return an iterator to explore the context around PP.
bool undefIsContained() const
Returns whether this state contains an undef value or not.
bool isValidState() const override
See AbstractState::isValidState(...)
const SetTy & getAssumedSet() const
Return this set.