LLVM 23.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
200namespace {
201/// NVPTX/AMDGPU address space values (shared between both targets)
202enum class NVPTXAMDGPUAddressSpace : unsigned {
203 Generic = 0,
204 Global = 1,
205 Shared = 3,
206 Constant = 4,
207 Local = 5,
208};
209
210/// SPIRV address space values (StorageClass)
211enum class SPIRVAddressSpace : unsigned {
212 Local = 0, // Function (private/local)
213 Global = 1, // CrossWorkgroup (global)
214 Constant = 2, // UniformConstant (constant)
215 Shared = 3, // Workgroup (shared)
216 Generic = 4, // Generic
217};
218} // namespace
219
220bool AA::isGPU(const Module &M) {
221 Triple T(M.getTargetTriple());
222 return T.isGPU();
223}
224
225bool AA::isGPUGenericAddressSpace(const Module &M, unsigned AS) {
226 assert(AA::isGPU(M) && "Only callable on GPU targets");
227 Triple T(M.getTargetTriple());
228
229 if (T.isSPIRV())
230 return AS == static_cast<unsigned>(SPIRVAddressSpace::Generic);
231
232 return AS == static_cast<unsigned>(NVPTXAMDGPUAddressSpace::Generic);
233}
234
235bool AA::isGPUGlobalAddressSpace(const Module &M, unsigned AS) {
236 assert(AA::isGPU(M) && "Only callable on GPU targets");
237 Triple T(M.getTargetTriple());
238
239 if (T.isSPIRV())
240 return AS == static_cast<unsigned>(SPIRVAddressSpace::Global);
241
242 return AS == static_cast<unsigned>(NVPTXAMDGPUAddressSpace::Global);
243}
244
245bool AA::isGPUSharedAddressSpace(const Module &M, unsigned AS) {
246 assert(AA::isGPU(M) && "Only callable on GPU targets");
247 Triple T(M.getTargetTriple());
248
249 if (T.isSPIRV())
250 return AS == static_cast<unsigned>(SPIRVAddressSpace::Shared);
251
252 return AS == static_cast<unsigned>(NVPTXAMDGPUAddressSpace::Shared);
253}
254
255bool AA::isGPUConstantAddressSpace(const Module &M, unsigned AS) {
256 assert(AA::isGPU(M) && "Only callable on GPU targets");
257 Triple T(M.getTargetTriple());
258
259 if (T.isSPIRV())
260 return AS == static_cast<unsigned>(SPIRVAddressSpace::Constant);
261
262 return AS == static_cast<unsigned>(NVPTXAMDGPUAddressSpace::Constant);
263}
264
265bool AA::isGPULocalAddressSpace(const Module &M, unsigned AS) {
266 assert(AA::isGPU(M) && "Only callable on GPU targets");
267 Triple T(M.getTargetTriple());
268
269 if (T.isSPIRV())
270 return AS == static_cast<unsigned>(SPIRVAddressSpace::Local);
271
272 return AS == static_cast<unsigned>(NVPTXAMDGPUAddressSpace::Local);
273}
274
276 const AbstractAttribute &QueryingAA) {
277 // We are looking for volatile instructions or non-relaxed atomics.
278 if (const auto *CB = dyn_cast<CallBase>(&I)) {
279 if (CB->hasFnAttr(Attribute::NoSync))
280 return true;
281
282 // Non-convergent and readnone imply nosync.
283 if (!CB->isConvergent() && !CB->mayReadOrWriteMemory())
284 return true;
285
286 bool IsKnownNoSync;
288 A, &QueryingAA, IRPosition::callsite_function(*CB),
289 DepClassTy::OPTIONAL, IsKnownNoSync);
290 }
291
292 if (!I.mayReadOrWriteMemory())
293 return true;
294
296}
297
299 const Value &V, bool ForAnalysisOnly) {
300 // TODO: See the AAInstanceInfo class comment.
301 if (!ForAnalysisOnly)
302 return false;
303 auto *InstanceInfoAA = A.getAAFor<AAInstanceInfo>(
305 return InstanceInfoAA && InstanceInfoAA->isAssumedUniqueForAnalysis();
306}
307
308Constant *
310 Value &Obj, Type &Ty, const TargetLibraryInfo *TLI,
311 const DataLayout &DL, AA::RangeTy *RangePtr) {
312 if (Constant *Init = getInitialValueOfAllocation(&Obj, TLI, &Ty))
313 return Init;
314 auto *GV = dyn_cast<GlobalVariable>(&Obj);
315 if (!GV)
316 return nullptr;
317
318 bool UsedAssumedInformation = false;
319 Constant *Initializer = nullptr;
320 if (A.hasGlobalVariableSimplificationCallback(*GV)) {
321 auto AssumedGV = A.getAssumedInitializerFromCallBack(
322 *GV, &QueryingAA, UsedAssumedInformation);
323 Initializer = *AssumedGV;
324 if (!Initializer)
325 return nullptr;
326 } else {
327 if (!GV->hasLocalLinkage()) {
328 // Externally visible global that's either non-constant,
329 // or a constant with an uncertain initializer.
330 if (!GV->hasDefinitiveInitializer() || !GV->isConstant())
331 return nullptr;
332 }
333
334 // Globals with local linkage are always initialized.
335 assert(!GV->hasLocalLinkage() || GV->hasInitializer());
336
337 if (!Initializer)
338 Initializer = GV->getInitializer();
339 }
340
341 if (RangePtr && !RangePtr->offsetOrSizeAreUnknown()) {
342 int64_t StorageSize = DL.getTypeStoreSize(&Ty);
343 if (StorageSize != RangePtr->Size)
344 return nullptr;
345 APInt Offset = APInt(64, RangePtr->Offset);
346 return ConstantFoldLoadFromConst(Initializer, &Ty, Offset, DL);
347 }
348
349 return ConstantFoldLoadFromUniformValue(Initializer, &Ty, DL);
350}
351
352bool AA::isValidInScope(const Value &V, const Function *Scope) {
353 if (isa<Constant>(V))
354 return true;
355 if (auto *I = dyn_cast<Instruction>(&V))
356 return I->getFunction() == Scope;
357 if (auto *A = dyn_cast<Argument>(&V))
358 return A->getParent() == Scope;
359 return false;
360}
361
363 InformationCache &InfoCache) {
364 if (isa<Constant>(VAC.getValue()) || VAC.getValue() == VAC.getCtxI())
365 return true;
366 const Function *Scope = nullptr;
367 const Instruction *CtxI = VAC.getCtxI();
368 if (CtxI)
369 Scope = CtxI->getFunction();
370 if (auto *A = dyn_cast<Argument>(VAC.getValue()))
371 return A->getParent() == Scope;
372 if (auto *I = dyn_cast<Instruction>(VAC.getValue())) {
373 if (I->getFunction() == Scope) {
374 if (const DominatorTree *DT =
376 *Scope))
377 return DT->dominates(I, CtxI);
378 // Local dominance check mostly for the old PM passes.
379 if (CtxI && I->getParent() == CtxI->getParent())
380 return llvm::any_of(
381 make_range(I->getIterator(), I->getParent()->end()),
382 [&](const Instruction &AfterI) { return &AfterI == CtxI; });
383 }
384 }
385 return false;
386}
387
389 if (V.getType() == &Ty)
390 return &V;
391 if (isa<PoisonValue>(V))
392 return PoisonValue::get(&Ty);
393 if (isa<UndefValue>(V))
394 return UndefValue::get(&Ty);
395 if (auto *C = dyn_cast<Constant>(&V)) {
396 if (C->isNullValue() && !Ty.isPtrOrPtrVectorTy())
397 return Constant::getNullValue(&Ty);
398 if (C->getType()->isPointerTy() && Ty.isPointerTy())
399 return ConstantExpr::getPointerCast(C, &Ty);
400 if (C->getType()->getPrimitiveSizeInBits() >= Ty.getPrimitiveSizeInBits()) {
401 if (C->getType()->isIntegerTy() && Ty.isIntegerTy())
402 return ConstantExpr::getTrunc(C, &Ty, /* OnlyIfReduced */ true);
403 if (C->getType()->isFloatingPointTy() && Ty.isFloatingPointTy())
404 return ConstantFoldCastInstruction(Instruction::FPTrunc, C, &Ty);
405 }
406 }
407 return nullptr;
408}
409
410std::optional<Value *>
411AA::combineOptionalValuesInAAValueLatice(const std::optional<Value *> &A,
412 const std::optional<Value *> &B,
413 Type *Ty) {
414 if (A == B)
415 return A;
416 if (!B)
417 return A;
418 if (*B == nullptr)
419 return nullptr;
420 if (!A)
421 return Ty ? getWithType(**B, *Ty) : nullptr;
422 if (*A == nullptr)
423 return nullptr;
424 if (!Ty)
425 Ty = (*A)->getType();
427 return getWithType(**B, *Ty);
428 if (isa<UndefValue>(*B))
429 return A;
430 if (*A && *B && *A == getWithType(**B, *Ty))
431 return A;
432 return nullptr;
433}
434
435template <bool IsLoad, typename Ty>
437 Attributor &A, Ty &I, SmallSetVector<Value *, 4> &PotentialCopies,
438 SmallSetVector<Instruction *, 4> *PotentialValueOrigins,
439 const AbstractAttribute &QueryingAA, bool &UsedAssumedInformation,
440 bool OnlyExact) {
441 LLVM_DEBUG(dbgs() << "Trying to determine the potential copies of " << I
442 << " (only exact: " << OnlyExact << ")\n";);
443
444 Value &Ptr = *I.getPointerOperand();
445 // Containers to remember the pointer infos and new copies while we are not
446 // sure that we can find all of them. If we abort we want to avoid spurious
447 // dependences and potential copies in the provided container.
451
452 const auto *TLI =
453 A.getInfoCache().getTargetLibraryInfoForFunction(*I.getFunction());
454
455 auto Pred = [&](Value &Obj) {
456 LLVM_DEBUG(dbgs() << "Visit underlying object " << Obj << "\n");
457 if (isa<UndefValue>(&Obj))
458 return true;
459 if (isa<ConstantPointerNull>(&Obj)) {
460 // A null pointer access can be undefined but any offset from null may
461 // be OK. We do not try to optimize the latter.
462 if (!NullPointerIsDefined(I.getFunction(),
463 Ptr.getType()->getPointerAddressSpace()) &&
464 A.getAssumedSimplified(Ptr, QueryingAA, UsedAssumedInformation,
465 AA::Interprocedural) == &Obj)
466 return true;
468 dbgs() << "Underlying object is a valid nullptr, giving up.\n";);
469 return false;
470 }
471 // TODO: Use assumed noalias return.
472 if (!isa<AllocaInst>(&Obj) && !isa<GlobalVariable>(&Obj) &&
473 !(IsLoad ? isAllocationFn(&Obj, TLI) : isNoAliasCall(&Obj))) {
474 LLVM_DEBUG(dbgs() << "Underlying object is not supported yet: " << Obj
475 << "\n";);
476 return false;
477 }
478 if (auto *GV = dyn_cast<GlobalVariable>(&Obj))
479 if (!GV->hasLocalLinkage() &&
480 !(GV->isConstant() && GV->hasInitializer())) {
481 LLVM_DEBUG(dbgs() << "Underlying object is global with external "
482 "linkage, not supported yet: "
483 << Obj << "\n";);
484 return false;
485 }
486
487 bool NullOnly = true;
488 bool NullRequired = false;
489 auto CheckForNullOnlyAndUndef = [&](std::optional<Value *> V,
490 bool IsExact) {
491 if (!V || *V == nullptr)
492 NullOnly = false;
493 else if (isa<UndefValue>(*V))
494 /* No op */;
495 else if (isa<Constant>(*V) && cast<Constant>(*V)->isNullValue())
496 NullRequired = !IsExact;
497 else
498 NullOnly = false;
499 };
500
501 auto AdjustWrittenValueType = [&](const AAPointerInfo::Access &Acc,
502 Value &V) {
503 Value *AdjV = AA::getWithType(V, *I.getType());
504 if (!AdjV) {
505 LLVM_DEBUG(dbgs() << "Underlying object written but stored value "
506 "cannot be converted to read type: "
507 << *Acc.getRemoteInst() << " : " << *I.getType()
508 << "\n";);
509 }
510 return AdjV;
511 };
512
513 auto SkipCB = [&](const AAPointerInfo::Access &Acc) {
514 if ((IsLoad && !Acc.isWriteOrAssumption()) || (!IsLoad && !Acc.isRead()))
515 return true;
516 if (IsLoad) {
518 return true;
519 if (PotentialValueOrigins && !isa<AssumeInst>(Acc.getRemoteInst()))
520 return false;
521 if (!Acc.isWrittenValueUnknown())
522 if (Value *V = AdjustWrittenValueType(Acc, *Acc.getWrittenValue()))
523 if (NewCopies.count(V)) {
524 NewCopyOrigins.insert(Acc.getRemoteInst());
525 return true;
526 }
527 if (auto *SI = dyn_cast<StoreInst>(Acc.getRemoteInst()))
528 if (Value *V = AdjustWrittenValueType(Acc, *SI->getValueOperand()))
529 if (NewCopies.count(V)) {
530 NewCopyOrigins.insert(Acc.getRemoteInst());
531 return true;
532 }
533 }
534 return false;
535 };
536
537 auto CheckAccess = [&](const AAPointerInfo::Access &Acc, bool IsExact) {
538 if ((IsLoad && !Acc.isWriteOrAssumption()) || (!IsLoad && !Acc.isRead()))
539 return true;
540 if (IsLoad && Acc.isWrittenValueYetUndetermined())
541 return true;
542 CheckForNullOnlyAndUndef(Acc.getContent(), IsExact);
543 if (OnlyExact && !IsExact && !NullOnly &&
545 LLVM_DEBUG(dbgs() << "Non exact access " << *Acc.getRemoteInst()
546 << ", abort!\n");
547 return false;
548 }
549 if (NullRequired && !NullOnly) {
550 LLVM_DEBUG(dbgs() << "Required all `null` accesses due to non exact "
551 "one, however found non-null one: "
552 << *Acc.getRemoteInst() << ", abort!\n");
553 return false;
554 }
555 if (IsLoad) {
556 assert(isa<LoadInst>(I) && "Expected load or store instruction only!");
557 if (!Acc.isWrittenValueUnknown()) {
558 Value *V = AdjustWrittenValueType(Acc, *Acc.getWrittenValue());
559 if (!V)
560 return false;
561 NewCopies.insert(V);
562 if (PotentialValueOrigins)
563 NewCopyOrigins.insert(Acc.getRemoteInst());
564 return true;
565 }
566 auto *SI = dyn_cast<StoreInst>(Acc.getRemoteInst());
567 if (!SI) {
568 LLVM_DEBUG(dbgs() << "Underlying object written through a non-store "
569 "instruction not supported yet: "
570 << *Acc.getRemoteInst() << "\n";);
571 return false;
572 }
573 Value *V = AdjustWrittenValueType(Acc, *SI->getValueOperand());
574 if (!V)
575 return false;
576 NewCopies.insert(V);
577 if (PotentialValueOrigins)
578 NewCopyOrigins.insert(SI);
579 } else {
580 assert(isa<StoreInst>(I) && "Expected load or store instruction only!");
581 auto *LI = dyn_cast<LoadInst>(Acc.getRemoteInst());
582 if (!LI && OnlyExact) {
583 LLVM_DEBUG(dbgs() << "Underlying object read through a non-load "
584 "instruction not supported yet: "
585 << *Acc.getRemoteInst() << "\n";);
586 return false;
587 }
588 NewCopies.insert(Acc.getRemoteInst());
589 }
590 return true;
591 };
592
593 // If the value has been written to we don't need the initial value of the
594 // object.
595 bool HasBeenWrittenTo = false;
596
598 auto *PI = A.getAAFor<AAPointerInfo>(QueryingAA, IRPosition::value(Obj),
600 if (!PI || !PI->forallInterferingAccesses(
601 A, QueryingAA, I,
602 /* FindInterferingWrites */ IsLoad,
603 /* FindInterferingReads */ !IsLoad, CheckAccess,
604 HasBeenWrittenTo, Range, SkipCB)) {
606 dbgs()
607 << "Failed to verify all interfering accesses for underlying object: "
608 << Obj << "\n");
609 return false;
610 }
611
612 if (IsLoad && !HasBeenWrittenTo && !Range.isUnassigned()) {
613 const DataLayout &DL = A.getDataLayout();
614 Value *InitialValue = AA::getInitialValueForObj(
615 A, QueryingAA, Obj, *I.getType(), TLI, DL, &Range);
616 if (!InitialValue) {
617 LLVM_DEBUG(dbgs() << "Could not determine required initial value of "
618 "underlying object, abort!\n");
619 return false;
620 }
621 CheckForNullOnlyAndUndef(InitialValue, /* IsExact */ true);
622 if (NullRequired && !NullOnly) {
623 LLVM_DEBUG(dbgs() << "Non exact access but initial value that is not "
624 "null or undef, abort!\n");
625 return false;
626 }
627
628 NewCopies.insert(InitialValue);
629 if (PotentialValueOrigins)
630 NewCopyOrigins.insert(nullptr);
631 }
632
633 PIs.push_back(PI);
634
635 return true;
636 };
637
638 const auto *AAUO = A.getAAFor<AAUnderlyingObjects>(
639 QueryingAA, IRPosition::value(Ptr), DepClassTy::OPTIONAL);
640 if (!AAUO || !AAUO->forallUnderlyingObjects(Pred)) {
642 dbgs() << "Underlying objects stored into could not be determined\n";);
643 return false;
644 }
645
646 // Only if we were successful collection all potential copies we record
647 // dependences (on non-fix AAPointerInfo AAs). We also only then modify the
648 // given PotentialCopies container.
649 for (const auto *PI : PIs) {
650 if (!PI->getState().isAtFixpoint())
651 UsedAssumedInformation = true;
652 A.recordDependence(*PI, QueryingAA, DepClassTy::OPTIONAL);
653 }
654 PotentialCopies.insert_range(NewCopies);
655 if (PotentialValueOrigins)
656 PotentialValueOrigins->insert_range(NewCopyOrigins);
657
658 return true;
659}
660
662 Attributor &A, LoadInst &LI, SmallSetVector<Value *, 4> &PotentialValues,
663 SmallSetVector<Instruction *, 4> &PotentialValueOrigins,
664 const AbstractAttribute &QueryingAA, bool &UsedAssumedInformation,
665 bool OnlyExact) {
666 return getPotentialCopiesOfMemoryValue</* IsLoad */ true>(
667 A, LI, PotentialValues, &PotentialValueOrigins, QueryingAA,
668 UsedAssumedInformation, OnlyExact);
669}
670
673 const AbstractAttribute &QueryingAA, bool &UsedAssumedInformation,
674 bool OnlyExact) {
675 return getPotentialCopiesOfMemoryValue</* IsLoad */ false>(
676 A, SI, PotentialCopies, nullptr, QueryingAA, UsedAssumedInformation,
677 OnlyExact);
678}
679
681 const AbstractAttribute &QueryingAA,
682 bool RequireReadNone, bool &IsKnown) {
683 if (RequireReadNone) {
685 A, &QueryingAA, IRP, DepClassTy::OPTIONAL, IsKnown,
686 /* IgnoreSubsumingPositions */ true))
687 return true;
689 A, &QueryingAA, IRP, DepClassTy::OPTIONAL, IsKnown,
690 /* IgnoreSubsumingPositions */ true))
691 return true;
692
695 const auto *MemLocAA =
696 A.getAAFor<AAMemoryLocation>(QueryingAA, IRP, DepClassTy::NONE);
697 if (MemLocAA && MemLocAA->isAssumedReadNone()) {
698 IsKnown = MemLocAA->isKnownReadNone();
699 if (!IsKnown)
700 A.recordDependence(*MemLocAA, QueryingAA, DepClassTy::OPTIONAL);
701 return true;
702 }
703 }
704
705 const auto *MemBehaviorAA =
706 A.getAAFor<AAMemoryBehavior>(QueryingAA, IRP, DepClassTy::NONE);
707 if (MemBehaviorAA &&
708 (MemBehaviorAA->isAssumedReadNone() ||
709 (!RequireReadNone && MemBehaviorAA->isAssumedReadOnly()))) {
710 IsKnown = RequireReadNone ? MemBehaviorAA->isKnownReadNone()
711 : MemBehaviorAA->isKnownReadOnly();
712 if (!IsKnown)
713 A.recordDependence(*MemBehaviorAA, QueryingAA, DepClassTy::OPTIONAL);
714 return true;
715 }
716
717 return false;
718}
719
721 const AbstractAttribute &QueryingAA, bool &IsKnown) {
722 return isAssumedReadOnlyOrReadNone(A, IRP, QueryingAA,
723 /* RequireReadNone */ false, IsKnown);
724}
726 const AbstractAttribute &QueryingAA, bool &IsKnown) {
727 return isAssumedReadOnlyOrReadNone(A, IRP, QueryingAA,
728 /* RequireReadNone */ true, IsKnown);
729}
730
731static bool
733 const Instruction *ToI, const Function &ToFn,
734 const AbstractAttribute &QueryingAA,
735 const AA::InstExclusionSetTy *ExclusionSet,
736 std::function<bool(const Function &F)> GoBackwardsCB) {
738 dbgs() << "[AA] isPotentiallyReachable @" << ToFn.getName() << " from "
739 << FromI << " [GBCB: " << bool(GoBackwardsCB) << "][#ExS: "
740 << (ExclusionSet ? std::to_string(ExclusionSet->size()) : "none")
741 << "]\n";
742 if (ExclusionSet)
743 for (auto *ES : *ExclusionSet)
744 dbgs() << *ES << "\n";
745 });
746
747 // We know kernels (generally) cannot be called from within the module. Thus,
748 // for reachability we would need to step back from a kernel which would allow
749 // us to reach anything anyway. Even if a kernel is invoked from another
750 // kernel, values like allocas and shared memory are not accessible. We
751 // implicitly check for this situation to avoid costly lookups.
752 if (GoBackwardsCB && &ToFn != FromI.getFunction() &&
753 !GoBackwardsCB(*FromI.getFunction()) && A.getInfoCache().isKernel(ToFn) &&
754 A.getInfoCache().isKernel(*FromI.getFunction())) {
755 LLVM_DEBUG(dbgs() << "[AA] assume kernel cannot be reached from within the "
756 "module; success\n";);
757 return false;
758 }
759
760 // If we can go arbitrarily backwards we will eventually reach an entry point
761 // that can reach ToI. Only if a set of blocks through which we cannot go is
762 // provided, or once we track internal functions not accessible from the
763 // outside, it makes sense to perform backwards analysis in the absence of a
764 // GoBackwardsCB.
765 if (!GoBackwardsCB && !ExclusionSet) {
766 LLVM_DEBUG(dbgs() << "[AA] check @" << ToFn.getName() << " from " << FromI
767 << " is not checked backwards and does not have an "
768 "exclusion set, abort\n");
769 return true;
770 }
771
774 Worklist.push_back(&FromI);
775
776 while (!Worklist.empty()) {
777 const Instruction *CurFromI = Worklist.pop_back_val();
778 if (!Visited.insert(CurFromI).second)
779 continue;
780
781 const Function *FromFn = CurFromI->getFunction();
782 if (FromFn == &ToFn) {
783 if (!ToI)
784 return true;
785 LLVM_DEBUG(dbgs() << "[AA] check " << *ToI << " from " << *CurFromI
786 << " intraprocedurally\n");
787 const auto *ReachabilityAA = A.getAAFor<AAIntraFnReachability>(
788 QueryingAA, IRPosition::function(ToFn), DepClassTy::OPTIONAL);
789 bool Result = !ReachabilityAA || ReachabilityAA->isAssumedReachable(
790 A, *CurFromI, *ToI, ExclusionSet);
791 LLVM_DEBUG(dbgs() << "[AA] " << *CurFromI << " "
792 << (Result ? "can potentially " : "cannot ") << "reach "
793 << *ToI << " [Intra]\n");
794 if (Result)
795 return true;
796 }
797
798 bool Result = true;
799 if (!ToFn.isDeclaration() && ToI) {
800 const auto *ToReachabilityAA = A.getAAFor<AAIntraFnReachability>(
801 QueryingAA, IRPosition::function(ToFn), DepClassTy::OPTIONAL);
802 const Instruction &EntryI = ToFn.getEntryBlock().front();
803 Result = !ToReachabilityAA || ToReachabilityAA->isAssumedReachable(
804 A, EntryI, *ToI, ExclusionSet);
805 LLVM_DEBUG(dbgs() << "[AA] Entry " << EntryI << " of @" << ToFn.getName()
806 << " " << (Result ? "can potentially " : "cannot ")
807 << "reach @" << *ToI << " [ToFn]\n");
808 }
809
810 if (Result) {
811 // The entry of the ToFn can reach the instruction ToI. If the current
812 // instruction is already known to reach the ToFn.
813 const auto *FnReachabilityAA = A.getAAFor<AAInterFnReachability>(
814 QueryingAA, IRPosition::function(*FromFn), DepClassTy::OPTIONAL);
815 Result = !FnReachabilityAA || FnReachabilityAA->instructionCanReach(
816 A, *CurFromI, ToFn, ExclusionSet);
817 LLVM_DEBUG(dbgs() << "[AA] " << *CurFromI << " in @" << FromFn->getName()
818 << " " << (Result ? "can potentially " : "cannot ")
819 << "reach @" << ToFn.getName() << " [FromFn]\n");
820 if (Result)
821 return true;
822 }
823
824 // TODO: Check assumed nounwind.
825 const auto *ReachabilityAA = A.getAAFor<AAIntraFnReachability>(
826 QueryingAA, IRPosition::function(*FromFn), DepClassTy::OPTIONAL);
827 auto ReturnInstCB = [&](Instruction &Ret) {
828 bool Result = !ReachabilityAA || ReachabilityAA->isAssumedReachable(
829 A, *CurFromI, Ret, ExclusionSet);
830 LLVM_DEBUG(dbgs() << "[AA][Ret] " << *CurFromI << " "
831 << (Result ? "can potentially " : "cannot ") << "reach "
832 << Ret << " [Intra]\n");
833 return !Result;
834 };
835
836 // Check if we can reach returns.
837 bool UsedAssumedInformation = false;
838 if (A.checkForAllInstructions(ReturnInstCB, FromFn, &QueryingAA,
839 {Instruction::Ret}, UsedAssumedInformation)) {
840 LLVM_DEBUG(dbgs() << "[AA] No return is reachable, done\n");
841 continue;
842 }
843
844 if (!GoBackwardsCB) {
845 LLVM_DEBUG(dbgs() << "[AA] check @" << ToFn.getName() << " from " << FromI
846 << " is not checked backwards, abort\n");
847 return true;
848 }
849
850 // If we do not go backwards from the FromFn we are done here and so far we
851 // could not find a way to reach ToFn/ToI.
852 if (!GoBackwardsCB(*FromFn))
853 continue;
854
855 LLVM_DEBUG(dbgs() << "Stepping backwards to the call sites of @"
856 << FromFn->getName() << "\n");
857
858 auto CheckCallSite = [&](AbstractCallSite ACS) {
859 CallBase *CB = ACS.getInstruction();
860 if (!CB)
861 return false;
862
863 if (isa<InvokeInst>(CB))
864 return false;
865
866 Instruction *Inst = CB->getNextNode();
867 Worklist.push_back(Inst);
868 return true;
869 };
870
871 Result = !A.checkForAllCallSites(CheckCallSite, *FromFn,
872 /* RequireAllCallSites */ true,
873 &QueryingAA, UsedAssumedInformation);
874 if (Result) {
875 LLVM_DEBUG(dbgs() << "[AA] stepping back to call sites from " << *CurFromI
876 << " in @" << FromFn->getName()
877 << " failed, give up\n");
878 return true;
879 }
880
881 LLVM_DEBUG(dbgs() << "[AA] stepped back to call sites from " << *CurFromI
882 << " in @" << FromFn->getName()
883 << " worklist size is: " << Worklist.size() << "\n");
884 }
885 return false;
886}
887
889 Attributor &A, const Instruction &FromI, const Instruction &ToI,
890 const AbstractAttribute &QueryingAA,
891 const AA::InstExclusionSetTy *ExclusionSet,
892 std::function<bool(const Function &F)> GoBackwardsCB) {
893 const Function *ToFn = ToI.getFunction();
894 return ::isPotentiallyReachable(A, FromI, &ToI, *ToFn, QueryingAA,
895 ExclusionSet, GoBackwardsCB);
896}
897
899 Attributor &A, const Instruction &FromI, const Function &ToFn,
900 const AbstractAttribute &QueryingAA,
901 const AA::InstExclusionSetTy *ExclusionSet,
902 std::function<bool(const Function &F)> GoBackwardsCB) {
903 return ::isPotentiallyReachable(A, FromI, /* ToI */ nullptr, ToFn, QueryingAA,
904 ExclusionSet, GoBackwardsCB);
905}
906
908 const AbstractAttribute &QueryingAA) {
909 if (isa<UndefValue>(Obj))
910 return true;
911 if (isa<AllocaInst>(Obj)) {
912 InformationCache &InfoCache = A.getInfoCache();
913 if (!InfoCache.stackIsAccessibleByOtherThreads()) {
915 dbgs() << "[AA] Object '" << Obj
916 << "' is thread local; stack objects are thread local.\n");
917 return true;
918 }
919 bool IsKnownNoCapture;
920 bool IsAssumedNoCapture = AA::hasAssumedIRAttr<Attribute::Captures>(
921 A, &QueryingAA, IRPosition::value(Obj), DepClassTy::OPTIONAL,
922 IsKnownNoCapture);
923 LLVM_DEBUG(dbgs() << "[AA] Object '" << Obj << "' is "
924 << (IsAssumedNoCapture ? "" : "not") << " thread local; "
925 << (IsAssumedNoCapture ? "non-" : "")
926 << "captured stack object.\n");
927 return IsAssumedNoCapture;
928 }
929 if (auto *GV = dyn_cast<GlobalVariable>(&Obj)) {
930 if (GV->isConstant()) {
931 LLVM_DEBUG(dbgs() << "[AA] Object '" << Obj
932 << "' is thread local; constant global\n");
933 return true;
934 }
935 if (GV->isThreadLocal()) {
936 LLVM_DEBUG(dbgs() << "[AA] Object '" << Obj
937 << "' is thread local; thread local global\n");
938 return true;
939 }
940 }
941
942 if (A.getInfoCache().IsTargetGPU()) {
943 if (AA::isGPULocalAddressSpace(A.getInfoCache().getModule(),
944 Obj.getType()->getPointerAddressSpace())) {
945 LLVM_DEBUG(dbgs() << "[AA] Object '" << Obj
946 << "' is thread local; GPU local memory\n");
947 return true;
948 }
950 A.getInfoCache().getModule(),
951 Obj.getType()->getPointerAddressSpace())) {
952 LLVM_DEBUG(dbgs() << "[AA] Object '" << Obj
953 << "' is thread local; GPU constant memory\n");
954 return true;
955 }
956 }
957
958 LLVM_DEBUG(dbgs() << "[AA] Object '" << Obj << "' is not thread local\n");
959 return false;
960}
961
963 const AbstractAttribute &QueryingAA) {
964 if (!I.mayHaveSideEffects() && !I.mayReadFromMemory())
965 return false;
966
968
969 auto AddLocationPtr = [&](std::optional<MemoryLocation> Loc) {
970 if (!Loc || !Loc->Ptr) {
972 dbgs() << "[AA] Access to unknown location; -> requires barriers\n");
973 return false;
974 }
975 Ptrs.insert(Loc->Ptr);
976 return true;
977 };
978
979 if (const MemIntrinsic *MI = dyn_cast<MemIntrinsic>(&I)) {
980 if (!AddLocationPtr(MemoryLocation::getForDest(MI)))
981 return true;
983 if (!AddLocationPtr(MemoryLocation::getForSource(MTI)))
984 return true;
985 } else if (!AddLocationPtr(MemoryLocation::getOrNone(&I)))
986 return true;
987
988 return isPotentiallyAffectedByBarrier(A, Ptrs.getArrayRef(), QueryingAA, &I);
989}
990
993 const AbstractAttribute &QueryingAA,
994 const Instruction *CtxI) {
995 for (const Value *Ptr : Ptrs) {
996 if (!Ptr) {
997 LLVM_DEBUG(dbgs() << "[AA] nullptr; -> requires barriers\n");
998 return true;
999 }
1000
1001 auto Pred = [&](Value &Obj) {
1002 if (AA::isAssumedThreadLocalObject(A, Obj, QueryingAA))
1003 return true;
1004 LLVM_DEBUG(dbgs() << "[AA] Access to '" << Obj << "' via '" << *Ptr
1005 << "'; -> requires barrier\n");
1006 return false;
1007 };
1008
1009 const auto *UnderlyingObjsAA = A.getAAFor<AAUnderlyingObjects>(
1010 QueryingAA, IRPosition::value(*Ptr), DepClassTy::OPTIONAL);
1011 if (!UnderlyingObjsAA || !UnderlyingObjsAA->forallUnderlyingObjects(Pred))
1012 return true;
1013 }
1014 return false;
1015}
1016
1017/// Return true if \p New is equal or worse than \p Old.
1018static bool isEqualOrWorse(const Attribute &New, const Attribute &Old) {
1019 if (!Old.isIntAttribute())
1020 return true;
1021
1022 return Old.getValueAsInt() >= New.getValueAsInt();
1023}
1024
1025/// Return true if the information provided by \p Attr was added to the
1026/// attribute set \p AttrSet. This is only the case if it was not already
1027/// present in \p AttrSet.
1028static bool addIfNotExistent(LLVMContext &Ctx, const Attribute &Attr,
1029 AttributeSet AttrSet, bool ForceReplace,
1030 AttrBuilder &AB) {
1031
1032 if (Attr.isEnumAttribute()) {
1033 Attribute::AttrKind Kind = Attr.getKindAsEnum();
1034 if (AttrSet.hasAttribute(Kind))
1035 return false;
1036 AB.addAttribute(Kind);
1037 return true;
1038 }
1039 if (Attr.isStringAttribute()) {
1040 StringRef Kind = Attr.getKindAsString();
1041 if (AttrSet.hasAttribute(Kind)) {
1042 if (!ForceReplace)
1043 return false;
1044 }
1045 AB.addAttribute(Kind, Attr.getValueAsString());
1046 return true;
1047 }
1048 if (Attr.isIntAttribute()) {
1049 Attribute::AttrKind Kind = Attr.getKindAsEnum();
1050 if (!ForceReplace && Kind == Attribute::Memory) {
1051 MemoryEffects ME = Attr.getMemoryEffects() & AttrSet.getMemoryEffects();
1052 if (ME == AttrSet.getMemoryEffects())
1053 return false;
1054 AB.addMemoryAttr(ME);
1055 return true;
1056 }
1057 if (AttrSet.hasAttribute(Kind)) {
1058 if (!ForceReplace && isEqualOrWorse(Attr, AttrSet.getAttribute(Kind)))
1059 return false;
1060 }
1061 AB.addAttribute(Attr);
1062 return true;
1063 }
1064 if (Attr.isConstantRangeAttribute()) {
1065 Attribute::AttrKind Kind = Attr.getKindAsEnum();
1066 if (!ForceReplace && AttrSet.hasAttribute(Kind))
1067 return false;
1068 AB.addAttribute(Attr);
1069 return true;
1070 }
1071
1072 llvm_unreachable("Expected enum or string attribute!");
1073}
1074
1077 return cast<Argument>(&getAnchorValue());
1078
1079 // Not an Argument and no argument number means this is not a call site
1080 // argument, thus we cannot find a callback argument to return.
1081 int ArgNo = getCallSiteArgNo();
1082 if (ArgNo < 0)
1083 return nullptr;
1084
1085 // Use abstract call sites to make the connection between the call site
1086 // values and the ones in callbacks. If a callback was found that makes use
1087 // of the underlying call site operand, we want the corresponding callback
1088 // callee argument and not the direct callee argument.
1089 std::optional<Argument *> CBCandidateArg;
1090 SmallVector<const Use *, 4> CallbackUses;
1091 const auto &CB = cast<CallBase>(getAnchorValue());
1092 AbstractCallSite::getCallbackUses(CB, CallbackUses);
1093 for (const Use *U : CallbackUses) {
1094 AbstractCallSite ACS(U);
1095 assert(ACS && ACS.isCallbackCall());
1096 if (!ACS.getCalledFunction())
1097 continue;
1098
1099 for (unsigned u = 0, e = ACS.getNumArgOperands(); u < e; u++) {
1100
1101 // Test if the underlying call site operand is argument number u of the
1102 // callback callee.
1103 if (ACS.getCallArgOperandNo(u) != ArgNo)
1104 continue;
1105
1106 assert(ACS.getCalledFunction()->arg_size() > u &&
1107 "ACS mapped into var-args arguments!");
1108 if (CBCandidateArg) {
1109 CBCandidateArg = nullptr;
1110 break;
1111 }
1112 CBCandidateArg = ACS.getCalledFunction()->getArg(u);
1113 }
1114 }
1115
1116 // If we found a unique callback candidate argument, return it.
1117 if (CBCandidateArg && *CBCandidateArg)
1118 return *CBCandidateArg;
1119
1120 // If no callbacks were found, or none used the underlying call site operand
1121 // exclusively, use the direct callee argument if available.
1122 auto *Callee = dyn_cast_if_present<Function>(CB.getCalledOperand());
1123 if (Callee && Callee->arg_size() > unsigned(ArgNo))
1124 return Callee->getArg(ArgNo);
1125
1126 return nullptr;
1127}
1128
1131 if (getState().isAtFixpoint())
1132 return HasChanged;
1133
1134 LLVM_DEBUG(dbgs() << "[Attributor] Update: " << *this << "\n");
1135
1136 HasChanged = updateImpl(A);
1137
1138 LLVM_DEBUG(dbgs() << "[Attributor] Update " << HasChanged << " " << *this
1139 << "\n");
1140
1141 return HasChanged;
1142}
1143
1145 InformationCache &InfoCache,
1146 AttributorConfig Configuration)
1147 : Allocator(InfoCache.Allocator), Functions(Functions),
1148 InfoCache(InfoCache), Configuration(Configuration) {
1149 if (!isClosedWorldModule())
1150 return;
1151 for (Function *Fn : Functions)
1152 if (Fn->hasAddressTaken(/*PutOffender=*/nullptr,
1153 /*IgnoreCallbackUses=*/false,
1154 /*IgnoreAssumeLikeCalls=*/true,
1155 /*IgnoreLLVMUsed=*/true,
1156 /*IgnoreARCAttachedCall=*/false,
1157 /*IgnoreCastedDirectCall=*/true))
1158 InfoCache.IndirectlyCallableFunctions.push_back(Fn);
1159}
1160
1165 "Did expect a valid position!");
1168 if (!Explorer)
1169 return false;
1170
1171 Value &AssociatedValue = IRP.getAssociatedValue();
1172
1173 const Assume2KnowledgeMap &A2K =
1174 getInfoCache().getKnowledgeMap().lookup({&AssociatedValue, AK});
1175
1176 // Check if we found any potential assume use, if not we don't need to create
1177 // explorer iterators.
1178 if (A2K.empty())
1179 return false;
1180
1181 LLVMContext &Ctx = AssociatedValue.getContext();
1182 unsigned AttrsSize = Attrs.size();
1183 auto EIt = Explorer->begin(IRP.getCtxI()),
1184 EEnd = Explorer->end(IRP.getCtxI());
1185 for (const auto &It : A2K)
1186 if (Explorer->findInContextOf(It.first, EIt, EEnd))
1187 Attrs.push_back(Attribute::get(Ctx, AK, It.second.Max));
1188 return AttrsSize != Attrs.size();
1189}
1190
1191template <typename DescTy>
1193Attributor::updateAttrMap(const IRPosition &IRP, ArrayRef<DescTy> AttrDescs,
1194 function_ref<bool(const DescTy &, AttributeSet,
1195 AttributeMask &, AttrBuilder &)>
1196 CB) {
1197 if (AttrDescs.empty())
1199 switch (IRP.getPositionKind()) {
1203 default:
1204 break;
1205 };
1206
1207 AttributeList AL = IRP.getAttrList();
1208 Value *AttrListAnchor = IRP.getAttrListAnchor();
1209 auto [Iter, Inserted] = AttrsMap.insert({AttrListAnchor, AL});
1210 if (!Inserted)
1211 AL = Iter->second;
1212
1213 LLVMContext &Ctx = IRP.getAnchorValue().getContext();
1214 auto AttrIdx = IRP.getAttrIdx();
1215 AttributeSet AS = AL.getAttributes(AttrIdx);
1216 AttributeMask AM;
1217 AttrBuilder AB(Ctx);
1218
1220 for (const DescTy &AttrDesc : AttrDescs)
1221 if (CB(AttrDesc, AS, AM, AB))
1222 HasChanged = ChangeStatus::CHANGED;
1223
1224 if (HasChanged == ChangeStatus::UNCHANGED)
1226
1227 AL = AL.removeAttributesAtIndex(Ctx, AttrIdx, AM);
1228 AL = AL.addAttributesAtIndex(Ctx, AttrIdx, AB);
1229
1230 Iter->second = AL;
1231 return HasChanged;
1232}
1233
1236 bool IgnoreSubsumingPositions,
1237 Attribute::AttrKind ImpliedAttributeKind) {
1238 bool Implied = false;
1239 bool HasAttr = false;
1240 auto HasAttrCB = [&](const Attribute::AttrKind &Kind, AttributeSet AttrSet,
1241 AttributeMask &, AttrBuilder &) {
1242 if (AttrSet.hasAttribute(Kind)) {
1243 Implied |= Kind != ImpliedAttributeKind;
1244 HasAttr = true;
1245 }
1246 return false;
1247 };
1248 for (const IRPosition &EquivIRP : SubsumingPositionIterator(IRP)) {
1249 updateAttrMap<Attribute::AttrKind>(EquivIRP, AttrKinds, HasAttrCB);
1250 if (HasAttr)
1251 break;
1252 // The first position returned by the SubsumingPositionIterator is
1253 // always the position itself. If we ignore subsuming positions we
1254 // are done after the first iteration.
1255 if (IgnoreSubsumingPositions)
1256 break;
1257 Implied = true;
1258 }
1259 if (!HasAttr) {
1260 Implied = true;
1262 for (Attribute::AttrKind AK : AttrKinds)
1263 if (getAttrsFromAssumes(IRP, AK, Attrs)) {
1264 HasAttr = true;
1265 break;
1266 }
1267 }
1268
1269 // Check if we should manifest the implied attribute kind at the IRP.
1270 if (ImpliedAttributeKind != Attribute::None && HasAttr && Implied)
1272 ImpliedAttributeKind)});
1273 return HasAttr;
1274}
1275
1279 bool IgnoreSubsumingPositions) {
1280 auto CollectAttrCB = [&](const Attribute::AttrKind &Kind,
1281 AttributeSet AttrSet, AttributeMask &,
1282 AttrBuilder &) {
1283 if (AttrSet.hasAttribute(Kind))
1284 Attrs.push_back(AttrSet.getAttribute(Kind));
1285 return false;
1286 };
1287 for (const IRPosition &EquivIRP : SubsumingPositionIterator(IRP)) {
1288 updateAttrMap<Attribute::AttrKind>(EquivIRP, AttrKinds, CollectAttrCB);
1289 // The first position returned by the SubsumingPositionIterator is
1290 // always the position itself. If we ignore subsuming positions we
1291 // are done after the first iteration.
1292 if (IgnoreSubsumingPositions)
1293 break;
1294 }
1295 for (Attribute::AttrKind AK : AttrKinds)
1296 getAttrsFromAssumes(IRP, AK, Attrs);
1297}
1298
1301 auto RemoveAttrCB = [&](const Attribute::AttrKind &Kind, AttributeSet AttrSet,
1302 AttributeMask &AM, AttrBuilder &) {
1303 if (!AttrSet.hasAttribute(Kind))
1304 return false;
1305 AM.addAttribute(Kind);
1306 return true;
1307 };
1308 return updateAttrMap<Attribute::AttrKind>(IRP, AttrKinds, RemoveAttrCB);
1309}
1310
1312 ArrayRef<StringRef> Attrs) {
1313 auto RemoveAttrCB = [&](StringRef Attr, AttributeSet AttrSet,
1314 AttributeMask &AM, AttrBuilder &) -> bool {
1315 if (!AttrSet.hasAttribute(Attr))
1316 return false;
1317 AM.addAttribute(Attr);
1318 return true;
1319 };
1320
1321 return updateAttrMap<StringRef>(IRP, Attrs, RemoveAttrCB);
1322}
1323
1325 ArrayRef<Attribute> Attrs,
1326 bool ForceReplace) {
1327 LLVMContext &Ctx = IRP.getAnchorValue().getContext();
1328 auto AddAttrCB = [&](const Attribute &Attr, AttributeSet AttrSet,
1329 AttributeMask &, AttrBuilder &AB) {
1330 return addIfNotExistent(Ctx, Attr, AttrSet, ForceReplace, AB);
1331 };
1332 return updateAttrMap<Attribute>(IRP, Attrs, AddAttrCB);
1333}
1334
1336
1338 IRPositions.emplace_back(IRP);
1339
1340 // Helper to determine if operand bundles on a call site are benign or
1341 // potentially problematic. We handle only llvm.assume for now.
1342 auto CanIgnoreOperandBundles = [](const CallBase &CB) {
1343 return (isa<IntrinsicInst>(CB) &&
1344 cast<IntrinsicInst>(CB).getIntrinsicID() == Intrinsic ::assume);
1345 };
1346
1347 const auto *CB = dyn_cast<CallBase>(&IRP.getAnchorValue());
1348 switch (IRP.getPositionKind()) {
1352 return;
1355 IRPositions.emplace_back(IRPosition::function(*IRP.getAnchorScope()));
1356 return;
1358 assert(CB && "Expected call site!");
1359 // TODO: We need to look at the operand bundles similar to the redirection
1360 // in CallBase.
1361 if (!CB->hasOperandBundles() || CanIgnoreOperandBundles(*CB))
1362 if (auto *Callee = dyn_cast_if_present<Function>(CB->getCalledOperand()))
1363 IRPositions.emplace_back(IRPosition::function(*Callee));
1364 return;
1366 assert(CB && "Expected call site!");
1367 // TODO: We need to look at the operand bundles similar to the redirection
1368 // in CallBase.
1369 if (!CB->hasOperandBundles() || CanIgnoreOperandBundles(*CB)) {
1370 if (auto *Callee =
1371 dyn_cast_if_present<Function>(CB->getCalledOperand())) {
1372 IRPositions.emplace_back(IRPosition::returned(*Callee));
1373 IRPositions.emplace_back(IRPosition::function(*Callee));
1374 for (const Argument &Arg : Callee->args())
1375 if (Arg.hasReturnedAttr()) {
1376 IRPositions.emplace_back(
1377 IRPosition::callsite_argument(*CB, Arg.getArgNo()));
1378 IRPositions.emplace_back(
1379 IRPosition::value(*CB->getArgOperand(Arg.getArgNo())));
1380 IRPositions.emplace_back(IRPosition::argument(Arg));
1381 }
1382 }
1383 }
1384 IRPositions.emplace_back(IRPosition::callsite_function(*CB));
1385 return;
1387 assert(CB && "Expected call site!");
1388 // TODO: We need to look at the operand bundles similar to the redirection
1389 // in CallBase.
1390 if (!CB->hasOperandBundles() || CanIgnoreOperandBundles(*CB)) {
1391 auto *Callee = dyn_cast_if_present<Function>(CB->getCalledOperand());
1392 if (Callee) {
1393 if (Argument *Arg = IRP.getAssociatedArgument())
1394 IRPositions.emplace_back(IRPosition::argument(*Arg));
1395 IRPositions.emplace_back(IRPosition::function(*Callee));
1396 }
1397 }
1398 IRPositions.emplace_back(IRPosition::value(IRP.getAssociatedValue()));
1399 return;
1400 }
1401 }
1402}
1403
1404void IRPosition::verify() {
1405#ifdef EXPENSIVE_CHECKS
1406 switch (getPositionKind()) {
1407 case IRP_INVALID:
1408 assert((CBContext == nullptr) &&
1409 "Invalid position must not have CallBaseContext!");
1410 assert(!Enc.getOpaqueValue() &&
1411 "Expected a nullptr for an invalid position!");
1412 return;
1413 case IRP_FLOAT:
1415 "Expected specialized kind for argument values!");
1416 return;
1417 case IRP_RETURNED:
1418 assert(isa<Function>(getAsValuePtr()) &&
1419 "Expected function for a 'returned' position!");
1420 assert(getAsValuePtr() == &getAssociatedValue() &&
1421 "Associated value mismatch!");
1422 return;
1424 assert((CBContext == nullptr) &&
1425 "'call site returned' position must not have CallBaseContext!");
1426 assert((isa<CallBase>(getAsValuePtr())) &&
1427 "Expected call base for 'call site returned' position!");
1428 assert(getAsValuePtr() == &getAssociatedValue() &&
1429 "Associated value mismatch!");
1430 return;
1431 case IRP_CALL_SITE:
1432 assert((CBContext == nullptr) &&
1433 "'call site function' position must not have CallBaseContext!");
1434 assert((isa<CallBase>(getAsValuePtr())) &&
1435 "Expected call base for 'call site function' position!");
1436 assert(getAsValuePtr() == &getAssociatedValue() &&
1437 "Associated value mismatch!");
1438 return;
1439 case IRP_FUNCTION:
1440 assert(isa<Function>(getAsValuePtr()) &&
1441 "Expected function for a 'function' position!");
1442 assert(getAsValuePtr() == &getAssociatedValue() &&
1443 "Associated value mismatch!");
1444 return;
1445 case IRP_ARGUMENT:
1446 assert(isa<Argument>(getAsValuePtr()) &&
1447 "Expected argument for a 'argument' position!");
1448 assert(getAsValuePtr() == &getAssociatedValue() &&
1449 "Associated value mismatch!");
1450 return;
1452 assert((CBContext == nullptr) &&
1453 "'call site argument' position must not have CallBaseContext!");
1454 Use *U = getAsUsePtr();
1455 (void)U; // Silence unused variable warning.
1456 assert(U && "Expected use for a 'call site argument' position!");
1457 assert(isa<CallBase>(U->getUser()) &&
1458 "Expected call base user for a 'call site argument' position!");
1459 assert(cast<CallBase>(U->getUser())->isArgOperand(U) &&
1460 "Expected call base argument operand for a 'call site argument' "
1461 "position");
1462 assert(cast<CallBase>(U->getUser())->getArgOperandNo(U) ==
1463 unsigned(getCallSiteArgNo()) &&
1464 "Argument number mismatch!");
1465 assert(U->get() == &getAssociatedValue() && "Associated value mismatch!");
1466 return;
1467 }
1468 }
1469#endif
1470}
1471
1472std::optional<Constant *>
1474 const AbstractAttribute &AA,
1475 bool &UsedAssumedInformation) {
1476 // First check all callbacks provided by outside AAs. If any of them returns
1477 // a non-null value that is different from the associated value, or
1478 // std::nullopt, we assume it's simplified.
1479 for (auto &CB : SimplificationCallbacks.lookup(IRP)) {
1480 std::optional<Value *> SimplifiedV = CB(IRP, &AA, UsedAssumedInformation);
1481 if (!SimplifiedV)
1482 return std::nullopt;
1483 if (isa_and_nonnull<Constant>(*SimplifiedV))
1484 return cast<Constant>(*SimplifiedV);
1485 return nullptr;
1486 }
1487 if (auto *C = dyn_cast<Constant>(&IRP.getAssociatedValue()))
1488 return C;
1490 if (getAssumedSimplifiedValues(IRP, &AA, Values,
1492 UsedAssumedInformation)) {
1493 if (Values.empty())
1494 return std::nullopt;
1495 if (auto *C = dyn_cast_or_null<Constant>(
1496 AAPotentialValues::getSingleValue(*this, AA, IRP, Values)))
1497 return C;
1498 }
1499 return nullptr;
1500}
1501
1503 const IRPosition &IRP, const AbstractAttribute *AA,
1504 bool &UsedAssumedInformation, AA::ValueScope S) {
1505 // First check all callbacks provided by outside AAs. If any of them returns
1506 // a non-null value that is different from the associated value, or
1507 // std::nullopt, we assume it's simplified.
1508 for (auto &CB : SimplificationCallbacks.lookup(IRP))
1509 return CB(IRP, AA, UsedAssumedInformation);
1510
1512 if (!getAssumedSimplifiedValues(IRP, AA, Values, S, UsedAssumedInformation))
1513 return &IRP.getAssociatedValue();
1514 if (Values.empty())
1515 return std::nullopt;
1516 if (AA)
1517 if (Value *V = AAPotentialValues::getSingleValue(*this, *AA, IRP, Values))
1518 return V;
1521 return nullptr;
1522 return &IRP.getAssociatedValue();
1523}
1524
1526 const IRPosition &InitialIRP, const AbstractAttribute *AA,
1528 bool &UsedAssumedInformation, bool RecurseForSelectAndPHI) {
1531 Worklist.push_back(InitialIRP);
1532 while (!Worklist.empty()) {
1533 const IRPosition &IRP = Worklist.pop_back_val();
1534
1535 // First check all callbacks provided by outside AAs. If any of them returns
1536 // a non-null value that is different from the associated value, or
1537 // std::nullopt, we assume it's simplified.
1538 int NV = Values.size();
1539 const auto &SimplificationCBs = SimplificationCallbacks.lookup(IRP);
1540 for (const auto &CB : SimplificationCBs) {
1541 std::optional<Value *> CBResult = CB(IRP, AA, UsedAssumedInformation);
1542 if (!CBResult.has_value())
1543 continue;
1544 Value *V = *CBResult;
1545 if (!V)
1546 return false;
1549 Values.push_back(AA::ValueAndContext{*V, nullptr});
1550 else
1551 return false;
1552 }
1553 if (SimplificationCBs.empty()) {
1554 // If no high-level/outside simplification occurred, use
1555 // AAPotentialValues.
1556 const auto *PotentialValuesAA =
1558 if (PotentialValuesAA &&
1559 PotentialValuesAA->getAssumedSimplifiedValues(*this, Values, S)) {
1560 UsedAssumedInformation |= !PotentialValuesAA->isAtFixpoint();
1561 } else if (IRP.getPositionKind() != IRPosition::IRP_RETURNED) {
1562 Values.push_back({IRP.getAssociatedValue(), IRP.getCtxI()});
1563 } else {
1564 // TODO: We could visit all returns and add the operands.
1565 return false;
1566 }
1567 }
1568
1569 if (!RecurseForSelectAndPHI)
1570 break;
1571
1572 for (int I = NV, E = Values.size(); I < E; ++I) {
1573 Value *V = Values[I].getValue();
1574 if (!isa<PHINode>(V) && !isa<SelectInst>(V))
1575 continue;
1576 if (!Seen.insert(V).second)
1577 continue;
1578 // Move the last element to this slot.
1579 Values[I] = Values[E - 1];
1580 // Eliminate the last slot, adjust the indices.
1581 Values.pop_back();
1582 --E;
1583 --I;
1584 // Add a new value (select or phi) to the worklist.
1585 Worklist.push_back(IRPosition::value(*V));
1586 }
1587 }
1588 return true;
1589}
1590
1592 std::optional<Value *> V, CallBase &CB, const AbstractAttribute &AA,
1593 bool &UsedAssumedInformation) {
1594 if (!V)
1595 return V;
1596 if (*V == nullptr || isa<Constant>(*V))
1597 return V;
1598 if (auto *Arg = dyn_cast<Argument>(*V))
1599 if (CB.getCalledOperand() == Arg->getParent() &&
1600 CB.arg_size() > Arg->getArgNo())
1601 if (!Arg->hasPointeeInMemoryValueAttr())
1602 return getAssumedSimplified(
1603 IRPosition::callsite_argument(CB, Arg->getArgNo()), AA,
1604 UsedAssumedInformation, AA::Intraprocedural);
1605 return nullptr;
1606}
1607
1609 // The abstract attributes are allocated via the BumpPtrAllocator Allocator,
1610 // thus we cannot delete them. We can, and want to, destruct them though.
1611 for (auto &It : AAMap) {
1612 AbstractAttribute *AA = It.getSecond();
1613 AA->~AbstractAttribute();
1614 }
1615}
1616
1618 const AAIsDead *FnLivenessAA,
1619 bool &UsedAssumedInformation,
1620 bool CheckBBLivenessOnly, DepClassTy DepClass) {
1621 if (!Configuration.UseLiveness)
1622 return false;
1623 const IRPosition &IRP = AA.getIRPosition();
1624 if (!Functions.count(IRP.getAnchorScope()))
1625 return false;
1626 return isAssumedDead(IRP, &AA, FnLivenessAA, UsedAssumedInformation,
1627 CheckBBLivenessOnly, DepClass);
1628}
1629
1631 const AbstractAttribute *QueryingAA,
1632 const AAIsDead *FnLivenessAA,
1633 bool &UsedAssumedInformation,
1634 bool CheckBBLivenessOnly, DepClassTy DepClass) {
1635 if (!Configuration.UseLiveness)
1636 return false;
1637 Instruction *UserI = dyn_cast<Instruction>(U.getUser());
1638 if (!UserI)
1639 return isAssumedDead(IRPosition::value(*U.get()), QueryingAA, FnLivenessAA,
1640 UsedAssumedInformation, CheckBBLivenessOnly, DepClass);
1641
1642 if (auto *CB = dyn_cast<CallBase>(UserI)) {
1643 // For call site argument uses we can check if the argument is
1644 // unused/dead.
1645 if (CB->isArgOperand(&U)) {
1646 const IRPosition &CSArgPos =
1647 IRPosition::callsite_argument(*CB, CB->getArgOperandNo(&U));
1648 return isAssumedDead(CSArgPos, QueryingAA, FnLivenessAA,
1649 UsedAssumedInformation, CheckBBLivenessOnly,
1650 DepClass);
1651 }
1652 } else if (ReturnInst *RI = dyn_cast<ReturnInst>(UserI)) {
1653 const IRPosition &RetPos = IRPosition::returned(*RI->getFunction());
1654 return isAssumedDead(RetPos, QueryingAA, FnLivenessAA,
1655 UsedAssumedInformation, CheckBBLivenessOnly, DepClass);
1656 } else if (PHINode *PHI = dyn_cast<PHINode>(UserI)) {
1657 BasicBlock *IncomingBB = PHI->getIncomingBlock(U);
1658 return isAssumedDead(*IncomingBB->getTerminator(), QueryingAA, FnLivenessAA,
1659 UsedAssumedInformation, CheckBBLivenessOnly, DepClass);
1660 } else if (StoreInst *SI = dyn_cast<StoreInst>(UserI)) {
1661 if (!CheckBBLivenessOnly && SI->getPointerOperand() != U.get()) {
1662 const IRPosition IRP = IRPosition::inst(*SI);
1663 const AAIsDead *IsDeadAA =
1665 if (IsDeadAA && IsDeadAA->isRemovableStore()) {
1666 if (QueryingAA)
1667 recordDependence(*IsDeadAA, *QueryingAA, DepClass);
1668 if (!IsDeadAA->isKnown(AAIsDead::IS_REMOVABLE))
1669 UsedAssumedInformation = true;
1670 return true;
1671 }
1672 }
1673 }
1674
1675 return isAssumedDead(IRPosition::inst(*UserI), QueryingAA, FnLivenessAA,
1676 UsedAssumedInformation, CheckBBLivenessOnly, DepClass);
1677}
1678
1680 const AbstractAttribute *QueryingAA,
1681 const AAIsDead *FnLivenessAA,
1682 bool &UsedAssumedInformation,
1683 bool CheckBBLivenessOnly, DepClassTy DepClass,
1684 bool CheckForDeadStore) {
1685 if (!Configuration.UseLiveness)
1686 return false;
1687 const IRPosition::CallBaseContext *CBCtx =
1688 QueryingAA ? QueryingAA->getCallBaseContext() : nullptr;
1689
1690 if (ManifestAddedBlocks.contains(I.getParent()))
1691 return false;
1692
1693 const Function &F = *I.getFunction();
1694 if (!FnLivenessAA || FnLivenessAA->getAnchorScope() != &F)
1695 FnLivenessAA = getOrCreateAAFor<AAIsDead>(IRPosition::function(F, CBCtx),
1696 QueryingAA, DepClassTy::NONE);
1697
1698 // Don't use recursive reasoning.
1699 if (!FnLivenessAA || QueryingAA == FnLivenessAA)
1700 return false;
1701
1702 // If we have a context instruction and a liveness AA we use it.
1703 if (CheckBBLivenessOnly ? FnLivenessAA->isAssumedDead(I.getParent())
1704 : FnLivenessAA->isAssumedDead(&I)) {
1705 if (QueryingAA)
1706 recordDependence(*FnLivenessAA, *QueryingAA, DepClass);
1707 if (!FnLivenessAA->isKnownDead(&I))
1708 UsedAssumedInformation = true;
1709 return true;
1710 }
1711
1712 if (CheckBBLivenessOnly)
1713 return false;
1714
1715 const IRPosition IRP = IRPosition::inst(I, CBCtx);
1716 const AAIsDead *IsDeadAA =
1718
1719 // Don't use recursive reasoning.
1720 if (!IsDeadAA || QueryingAA == IsDeadAA)
1721 return false;
1722
1723 if (IsDeadAA->isAssumedDead()) {
1724 if (QueryingAA)
1725 recordDependence(*IsDeadAA, *QueryingAA, DepClass);
1726 if (!IsDeadAA->isKnownDead())
1727 UsedAssumedInformation = true;
1728 return true;
1729 }
1730
1731 if (CheckForDeadStore && isa<StoreInst>(I) && IsDeadAA->isRemovableStore()) {
1732 if (QueryingAA)
1733 recordDependence(*IsDeadAA, *QueryingAA, DepClass);
1734 if (!IsDeadAA->isKnownDead())
1735 UsedAssumedInformation = true;
1736 return true;
1737 }
1738
1739 return false;
1740}
1741
1743 const AbstractAttribute *QueryingAA,
1744 const AAIsDead *FnLivenessAA,
1745 bool &UsedAssumedInformation,
1746 bool CheckBBLivenessOnly, DepClassTy DepClass) {
1747 if (!Configuration.UseLiveness)
1748 return false;
1749 // Don't check liveness for constants, e.g. functions, used as (floating)
1750 // values since the context instruction and such is here meaningless.
1753 return false;
1754 }
1755
1756 Instruction *CtxI = IRP.getCtxI();
1757 if (CtxI &&
1758 isAssumedDead(*CtxI, QueryingAA, FnLivenessAA, UsedAssumedInformation,
1759 /* CheckBBLivenessOnly */ true,
1760 CheckBBLivenessOnly ? DepClass : DepClassTy::OPTIONAL))
1761 return true;
1762
1763 if (CheckBBLivenessOnly)
1764 return false;
1765
1766 // If we haven't succeeded we query the specific liveness info for the IRP.
1767 const AAIsDead *IsDeadAA;
1769 IsDeadAA = getOrCreateAAFor<AAIsDead>(
1771 QueryingAA, DepClassTy::NONE);
1772 else
1773 IsDeadAA = getOrCreateAAFor<AAIsDead>(IRP, QueryingAA, DepClassTy::NONE);
1774
1775 // Don't use recursive reasoning.
1776 if (!IsDeadAA || QueryingAA == IsDeadAA)
1777 return false;
1778
1779 if (IsDeadAA->isAssumedDead()) {
1780 if (QueryingAA)
1781 recordDependence(*IsDeadAA, *QueryingAA, DepClass);
1782 if (!IsDeadAA->isKnownDead())
1783 UsedAssumedInformation = true;
1784 return true;
1785 }
1786
1787 return false;
1788}
1789
1791 const AbstractAttribute *QueryingAA,
1792 const AAIsDead *FnLivenessAA,
1793 DepClassTy DepClass) {
1794 if (!Configuration.UseLiveness)
1795 return false;
1796 const Function &F = *BB.getParent();
1797 if (!FnLivenessAA || FnLivenessAA->getAnchorScope() != &F)
1799 QueryingAA, DepClassTy::NONE);
1800
1801 // Don't use recursive reasoning.
1802 if (!FnLivenessAA || QueryingAA == FnLivenessAA)
1803 return false;
1804
1805 if (FnLivenessAA->isAssumedDead(&BB)) {
1806 if (QueryingAA)
1807 recordDependence(*FnLivenessAA, *QueryingAA, DepClass);
1808 return true;
1809 }
1810
1811 return false;
1812}
1813
1816 const AbstractAttribute &QueryingAA, const CallBase &CB) {
1817 if (const Function *Callee = dyn_cast<Function>(CB.getCalledOperand()))
1818 return Pred(Callee);
1819
1820 const auto *CallEdgesAA = getAAFor<AACallEdges>(
1822 if (!CallEdgesAA || CallEdgesAA->hasUnknownCallee())
1823 return false;
1824
1825 const auto &Callees = CallEdgesAA->getOptimisticEdges();
1826 return Pred(Callees.getArrayRef());
1827}
1828
1829bool canMarkAsVisited(const User *Usr) {
1830 return isa<PHINode>(Usr) || !isa<Instruction>(Usr);
1831}
1832
1834 function_ref<bool(const Use &, bool &)> Pred,
1835 const AbstractAttribute &QueryingAA, const Value &V,
1836 bool CheckBBLivenessOnly, DepClassTy LivenessDepClass,
1837 bool IgnoreDroppableUses,
1838 function_ref<bool(const Use &OldU, const Use &NewU)> EquivalentUseCB) {
1839
1840 // Check virtual uses first.
1841 for (VirtualUseCallbackTy &CB : VirtualUseCallbacks.lookup(&V))
1842 if (!CB(*this, &QueryingAA))
1843 return false;
1844
1845 if (isa<ConstantData>(V))
1846 return false;
1847
1848 // Check the trivial case first as it catches void values.
1849 if (V.use_empty())
1850 return true;
1851
1852 const IRPosition &IRP = QueryingAA.getIRPosition();
1855
1856 auto AddUsers = [&](const Value &V, const Use *OldUse) {
1857 for (const Use &UU : V.uses()) {
1858 if (OldUse && EquivalentUseCB && !EquivalentUseCB(*OldUse, UU)) {
1859 LLVM_DEBUG(dbgs() << "[Attributor] Potential copy was "
1860 "rejected by the equivalence call back: "
1861 << *UU << "!\n");
1862 return false;
1863 }
1864
1865 Worklist.push_back(&UU);
1866 }
1867 return true;
1868 };
1869
1870 AddUsers(V, /* OldUse */ nullptr);
1871
1872 LLVM_DEBUG(dbgs() << "[Attributor] Got " << Worklist.size()
1873 << " initial uses to check\n");
1874
1875 const Function *ScopeFn = IRP.getAnchorScope();
1876 const auto *LivenessAA =
1877 ScopeFn ? getAAFor<AAIsDead>(QueryingAA, IRPosition::function(*ScopeFn),
1879 : nullptr;
1880
1881 while (!Worklist.empty()) {
1882 const Use *U = Worklist.pop_back_val();
1883 if (canMarkAsVisited(U->getUser()) && !Visited.insert(U).second)
1884 continue;
1886 if (auto *Fn = dyn_cast<Function>(U->getUser()))
1887 dbgs() << "[Attributor] Check use: " << **U << " in " << Fn->getName()
1888 << "\n";
1889 else
1890 dbgs() << "[Attributor] Check use: " << **U << " in " << *U->getUser()
1891 << "\n";
1892 });
1893 bool UsedAssumedInformation = false;
1894 if (isAssumedDead(*U, &QueryingAA, LivenessAA, UsedAssumedInformation,
1895 CheckBBLivenessOnly, LivenessDepClass)) {
1897 dbgs() << "[Attributor] Dead use, skip!\n");
1898 continue;
1899 }
1900 if (IgnoreDroppableUses && U->getUser()->isDroppable()) {
1902 dbgs() << "[Attributor] Droppable user, skip!\n");
1903 continue;
1904 }
1905
1906 if (auto *SI = dyn_cast<StoreInst>(U->getUser())) {
1907 if (&SI->getOperandUse(0) == U) {
1908 if (!Visited.insert(U).second)
1909 continue;
1910 SmallSetVector<Value *, 4> PotentialCopies;
1912 *this, *SI, PotentialCopies, QueryingAA, UsedAssumedInformation,
1913 /* OnlyExact */ true)) {
1915 dbgs()
1916 << "[Attributor] Value is stored, continue with "
1917 << PotentialCopies.size()
1918 << " potential copies instead!\n");
1919 for (Value *PotentialCopy : PotentialCopies)
1920 if (!AddUsers(*PotentialCopy, U))
1921 return false;
1922 continue;
1923 }
1924 }
1925 }
1926
1927 bool Follow = false;
1928 if (!Pred(*U, Follow))
1929 return false;
1930 if (!Follow)
1931 continue;
1932
1933 User &Usr = *U->getUser();
1934 AddUsers(Usr, /* OldUse */ nullptr);
1935 }
1936
1937 return true;
1938}
1939
1941 const AbstractAttribute &QueryingAA,
1942 bool RequireAllCallSites,
1943 bool &UsedAssumedInformation) {
1944 // We can try to determine information from
1945 // the call sites. However, this is only possible all call sites are known,
1946 // hence the function has internal linkage.
1947 const IRPosition &IRP = QueryingAA.getIRPosition();
1948 const Function *AssociatedFunction = IRP.getAssociatedFunction();
1949 if (!AssociatedFunction) {
1950 LLVM_DEBUG(dbgs() << "[Attributor] No function associated with " << IRP
1951 << "\n");
1952 return false;
1953 }
1954
1955 return checkForAllCallSites(Pred, *AssociatedFunction, RequireAllCallSites,
1956 &QueryingAA, UsedAssumedInformation);
1957}
1958
1960 const Function &Fn,
1961 bool RequireAllCallSites,
1962 const AbstractAttribute *QueryingAA,
1963 bool &UsedAssumedInformation,
1964 bool CheckPotentiallyDead) {
1965 if (RequireAllCallSites && !Fn.hasLocalLinkage()) {
1966 LLVM_DEBUG(
1967 dbgs()
1968 << "[Attributor] Function " << Fn.getName()
1969 << " has no internal linkage, hence not all call sites are known\n");
1970 return false;
1971 }
1972 // Check virtual uses first.
1973 for (VirtualUseCallbackTy &CB : VirtualUseCallbacks.lookup(&Fn))
1974 if (!CB(*this, QueryingAA))
1975 return false;
1976
1978 for (unsigned u = 0; u < Uses.size(); ++u) {
1979 const Use &U = *Uses[u];
1981 if (auto *Fn = dyn_cast<Function>(U))
1982 dbgs() << "[Attributor] Check use: " << Fn->getName() << " in "
1983 << *U.getUser() << "\n";
1984 else
1985 dbgs() << "[Attributor] Check use: " << *U << " in " << *U.getUser()
1986 << "\n";
1987 });
1988 if (!CheckPotentiallyDead &&
1989 isAssumedDead(U, QueryingAA, nullptr, UsedAssumedInformation,
1990 /* CheckBBLivenessOnly */ true)) {
1992 dbgs() << "[Attributor] Dead use, skip!\n");
1993 continue;
1994 }
1995 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(U.getUser())) {
1996 if (CE->isCast() && CE->getType()->isPointerTy()) {
1998 dbgs() << "[Attributor] Use, is constant cast expression, add "
1999 << CE->getNumUses() << " uses of that expression instead!\n";
2000 });
2001 for (const Use &CEU : CE->uses())
2002 Uses.push_back(&CEU);
2003 continue;
2004 }
2005 }
2006
2007 AbstractCallSite ACS(&U);
2008 if (!ACS) {
2009 LLVM_DEBUG(dbgs() << "[Attributor] Function " << Fn.getName()
2010 << " has non call site use " << *U.get() << " in "
2011 << *U.getUser() << "\n");
2012 return false;
2013 }
2014
2015 const Use *EffectiveUse =
2016 ACS.isCallbackCall() ? &ACS.getCalleeUseForCallback() : &U;
2017 if (!ACS.isCallee(EffectiveUse)) {
2018 if (!RequireAllCallSites) {
2019 LLVM_DEBUG(dbgs() << "[Attributor] User " << *EffectiveUse->getUser()
2020 << " is not a call of " << Fn.getName()
2021 << ", skip use\n");
2022 continue;
2023 }
2024 LLVM_DEBUG(dbgs() << "[Attributor] User " << *EffectiveUse->getUser()
2025 << " is an invalid use of " << Fn.getName() << "\n");
2026 return false;
2027 }
2028
2029 // Make sure the arguments that can be matched between the call site and the
2030 // callee argee on their type. It is unlikely they do not and it doesn't
2031 // make sense for all attributes to know/care about this.
2032 assert(&Fn == ACS.getCalledFunction() && "Expected known callee");
2033 unsigned MinArgsParams =
2034 std::min(size_t(ACS.getNumArgOperands()), Fn.arg_size());
2035 for (unsigned u = 0; u < MinArgsParams; ++u) {
2036 Value *CSArgOp = ACS.getCallArgOperand(u);
2037 if (CSArgOp && Fn.getArg(u)->getType() != CSArgOp->getType()) {
2038 LLVM_DEBUG(
2039 dbgs() << "[Attributor] Call site / callee argument type mismatch ["
2040 << u << "@" << Fn.getName() << ": "
2041 << *Fn.getArg(u)->getType() << " vs. "
2042 << *ACS.getCallArgOperand(u)->getType() << "\n");
2043 return false;
2044 }
2045 }
2046
2047 if (Pred(ACS))
2048 continue;
2049
2050 LLVM_DEBUG(dbgs() << "[Attributor] Call site callback failed for "
2051 << *ACS.getInstruction() << "\n");
2052 return false;
2053 }
2054
2055 return true;
2056}
2057
2058bool Attributor::shouldPropagateCallBaseContext(const IRPosition &IRP) {
2059 // TODO: Maintain a cache of Values that are
2060 // on the pathway from a Argument to a Instruction that would effect the
2061 // liveness/return state etc.
2063}
2064
2066 const AbstractAttribute &QueryingAA,
2068 bool RecurseForSelectAndPHI) {
2069
2070 const IRPosition &IRP = QueryingAA.getIRPosition();
2071 const Function *AssociatedFunction = IRP.getAssociatedFunction();
2072 if (!AssociatedFunction)
2073 return false;
2074
2075 bool UsedAssumedInformation = false;
2078 IRPosition::returned(*AssociatedFunction), &QueryingAA, Values, S,
2079 UsedAssumedInformation, RecurseForSelectAndPHI))
2080 return false;
2081
2082 return llvm::all_of(Values, [&](const AA::ValueAndContext &VAC) {
2083 return Pred(*VAC.getValue());
2084 });
2085}
2086
2089 function_ref<bool(Instruction &)> Pred, const AbstractAttribute *QueryingAA,
2090 const AAIsDead *LivenessAA, ArrayRef<unsigned> Opcodes,
2091 bool &UsedAssumedInformation, bool CheckBBLivenessOnly = false,
2092 bool CheckPotentiallyDead = false) {
2093 for (unsigned Opcode : Opcodes) {
2094 // Check if we have instructions with this opcode at all first.
2095 auto *Insts = OpcodeInstMap.lookup(Opcode);
2096 if (!Insts)
2097 continue;
2098
2099 for (Instruction *I : *Insts) {
2100 // Skip dead instructions.
2101 if (A && !CheckPotentiallyDead &&
2102 A->isAssumedDead(IRPosition::inst(*I), QueryingAA, LivenessAA,
2103 UsedAssumedInformation, CheckBBLivenessOnly)) {
2105 dbgs() << "[Attributor] Instruction " << *I
2106 << " is potentially dead, skip!\n";);
2107 continue;
2108 }
2109
2110 if (!Pred(*I))
2111 return false;
2112 }
2113 }
2114 return true;
2115}
2116
2118 const Function *Fn,
2119 const AbstractAttribute *QueryingAA,
2120 ArrayRef<unsigned> Opcodes,
2121 bool &UsedAssumedInformation,
2122 bool CheckBBLivenessOnly,
2123 bool CheckPotentiallyDead) {
2124 // Since we need to provide instructions we have to have an exact definition.
2125 if (!Fn || Fn->isDeclaration())
2126 return false;
2127
2128 const IRPosition &QueryIRP = IRPosition::function(*Fn);
2129 const auto *LivenessAA =
2130 CheckPotentiallyDead && QueryingAA
2131 ? (getAAFor<AAIsDead>(*QueryingAA, QueryIRP, DepClassTy::NONE))
2132 : nullptr;
2133
2134 auto &OpcodeInstMap = InfoCache.getOpcodeInstMapForFunction(*Fn);
2135 if (!checkForAllInstructionsImpl(this, OpcodeInstMap, Pred, QueryingAA,
2136 LivenessAA, Opcodes, UsedAssumedInformation,
2137 CheckBBLivenessOnly, CheckPotentiallyDead))
2138 return false;
2139
2140 return true;
2141}
2142
2144 const AbstractAttribute &QueryingAA,
2145 ArrayRef<unsigned> Opcodes,
2146 bool &UsedAssumedInformation,
2147 bool CheckBBLivenessOnly,
2148 bool CheckPotentiallyDead) {
2149 const IRPosition &IRP = QueryingAA.getIRPosition();
2150 const Function *AssociatedFunction = IRP.getAssociatedFunction();
2151 return checkForAllInstructions(Pred, AssociatedFunction, &QueryingAA, Opcodes,
2152 UsedAssumedInformation, CheckBBLivenessOnly,
2153 CheckPotentiallyDead);
2154}
2155
2157 function_ref<bool(Instruction &)> Pred, AbstractAttribute &QueryingAA,
2158 bool &UsedAssumedInformation) {
2159 TimeTraceScope TS("checkForAllReadWriteInstructions");
2160
2161 const Function *AssociatedFunction =
2162 QueryingAA.getIRPosition().getAssociatedFunction();
2163 if (!AssociatedFunction)
2164 return false;
2165
2166 const IRPosition &QueryIRP = IRPosition::function(*AssociatedFunction);
2167 const auto *LivenessAA =
2168 getAAFor<AAIsDead>(QueryingAA, QueryIRP, DepClassTy::NONE);
2169
2170 for (Instruction *I :
2171 InfoCache.getReadOrWriteInstsForFunction(*AssociatedFunction)) {
2172 // Skip dead instructions.
2173 if (isAssumedDead(IRPosition::inst(*I), &QueryingAA, LivenessAA,
2174 UsedAssumedInformation))
2175 continue;
2176
2177 if (!Pred(*I))
2178 return false;
2179 }
2180
2181 return true;
2182}
2183
2184void Attributor::runTillFixpoint() {
2185 TimeTraceScope TimeScope("Attributor::runTillFixpoint");
2186 LLVM_DEBUG(dbgs() << "[Attributor] Identified and initialized "
2187 << DG.SyntheticRoot.Deps.size()
2188 << " abstract attributes.\n");
2189
2190 // Now that all abstract attributes are collected and initialized we start
2191 // the abstract analysis.
2192
2193 unsigned IterationCounter = 1;
2194 unsigned MaxIterations =
2195 Configuration.MaxFixpointIterations.value_or(SetFixpointIterations);
2196
2198 SetVector<AbstractAttribute *> Worklist, InvalidAAs;
2199 Worklist.insert_range(DG.SyntheticRoot);
2200
2201 do {
2202 // Remember the size to determine new attributes.
2203 size_t NumAAs = DG.SyntheticRoot.Deps.size();
2204 LLVM_DEBUG(dbgs() << "\n\n[Attributor] #Iteration: " << IterationCounter
2205 << ", Worklist size: " << Worklist.size() << "\n");
2206
2207 // For invalid AAs we can fix dependent AAs that have a required dependence,
2208 // thereby folding long dependence chains in a single step without the need
2209 // to run updates.
2210 for (unsigned u = 0; u < InvalidAAs.size(); ++u) {
2211 AbstractAttribute *InvalidAA = InvalidAAs[u];
2212
2213 // Check the dependences to fast track invalidation.
2215 dbgs() << "[Attributor] InvalidAA: " << *InvalidAA
2216 << " has " << InvalidAA->Deps.size()
2217 << " required & optional dependences\n");
2218 for (auto &DepIt : InvalidAA->Deps) {
2219 AbstractAttribute *DepAA = cast<AbstractAttribute>(DepIt.getPointer());
2220 if (DepIt.getInt() == unsigned(DepClassTy::OPTIONAL)) {
2222 dbgs() << " - recompute: " << *DepAA);
2223 Worklist.insert(DepAA);
2224 continue;
2225 }
2227 << " - invalidate: " << *DepAA);
2229 assert(DepAA->getState().isAtFixpoint() && "Expected fixpoint state!");
2230 if (!DepAA->getState().isValidState())
2231 InvalidAAs.insert(DepAA);
2232 else
2233 ChangedAAs.push_back(DepAA);
2234 }
2235 InvalidAA->Deps.clear();
2236 }
2237
2238 // Add all abstract attributes that are potentially dependent on one that
2239 // changed to the work list.
2240 for (AbstractAttribute *ChangedAA : ChangedAAs) {
2241 for (auto &DepIt : ChangedAA->Deps)
2242 Worklist.insert(cast<AbstractAttribute>(DepIt.getPointer()));
2243 ChangedAA->Deps.clear();
2244 }
2245
2246 LLVM_DEBUG(dbgs() << "[Attributor] #Iteration: " << IterationCounter
2247 << ", Worklist+Dependent size: " << Worklist.size()
2248 << "\n");
2249
2250 // Reset the changed and invalid set.
2251 ChangedAAs.clear();
2252 InvalidAAs.clear();
2253
2254 // Update all abstract attribute in the work list and record the ones that
2255 // changed.
2256 for (AbstractAttribute *AA : Worklist) {
2257 const auto &AAState = AA->getState();
2258 if (!AAState.isAtFixpoint())
2259 if (updateAA(*AA) == ChangeStatus::CHANGED)
2260 ChangedAAs.push_back(AA);
2261
2262 // Use the InvalidAAs vector to propagate invalid states fast transitively
2263 // without requiring updates.
2264 if (!AAState.isValidState())
2265 InvalidAAs.insert(AA);
2266 }
2267
2268 // Add attributes to the changed set if they have been created in the last
2269 // iteration.
2270 ChangedAAs.append(DG.SyntheticRoot.begin() + NumAAs,
2271 DG.SyntheticRoot.end());
2272
2273 // Reset the work list and repopulate with the changed abstract attributes.
2274 // Note that dependent ones are added above.
2275 Worklist.clear();
2276 Worklist.insert_range(ChangedAAs);
2277 Worklist.insert_range(QueryAAsAwaitingUpdate);
2278 QueryAAsAwaitingUpdate.clear();
2279
2280 } while (!Worklist.empty() && (IterationCounter++ < MaxIterations));
2281
2282 if (IterationCounter > MaxIterations && !Functions.empty()) {
2283 auto Remark = [&](OptimizationRemarkMissed ORM) {
2284 return ORM << "Attributor did not reach a fixpoint after "
2285 << ore::NV("Iterations", MaxIterations) << " iterations.";
2286 };
2287 Function *F = Functions.front();
2289 }
2290
2291 LLVM_DEBUG(dbgs() << "\n[Attributor] Fixpoint iteration done after: "
2292 << IterationCounter << "/" << MaxIterations
2293 << " iterations\n");
2294
2295 // Reset abstract arguments not settled in a sound fixpoint by now. This
2296 // happens when we stopped the fixpoint iteration early. Note that only the
2297 // ones marked as "changed" *and* the ones transitively depending on them
2298 // need to be reverted to a pessimistic state. Others might not be in a
2299 // fixpoint state but we can use the optimistic results for them anyway.
2300 SmallPtrSet<AbstractAttribute *, 32> Visited;
2301 for (unsigned u = 0; u < ChangedAAs.size(); u++) {
2302 AbstractAttribute *ChangedAA = ChangedAAs[u];
2303 if (!Visited.insert(ChangedAA).second)
2304 continue;
2305
2306 AbstractState &State = ChangedAA->getState();
2307 if (!State.isAtFixpoint()) {
2309
2310 NumAttributesTimedOut++;
2311 }
2312
2313 for (auto &DepIt : ChangedAA->Deps)
2314 ChangedAAs.push_back(cast<AbstractAttribute>(DepIt.getPointer()));
2315 ChangedAA->Deps.clear();
2316 }
2317
2318 LLVM_DEBUG({
2319 if (!Visited.empty())
2320 dbgs() << "\n[Attributor] Finalized " << Visited.size()
2321 << " abstract attributes.\n";
2322 });
2323}
2324
2326 assert(AA.isQueryAA() &&
2327 "Non-query AAs should not be required to register for updates!");
2328 QueryAAsAwaitingUpdate.insert(&AA);
2329}
2330
2331ChangeStatus Attributor::manifestAttributes() {
2332 TimeTraceScope TimeScope("Attributor::manifestAttributes");
2333 size_t NumFinalAAs = DG.SyntheticRoot.Deps.size();
2334
2335 unsigned NumManifested = 0;
2336 unsigned NumAtFixpoint = 0;
2337 ChangeStatus ManifestChange = ChangeStatus::UNCHANGED;
2338 for (auto &DepAA : DG.SyntheticRoot.Deps) {
2339 AbstractAttribute *AA = cast<AbstractAttribute>(DepAA.getPointer());
2340 AbstractState &State = AA->getState();
2341
2342 // If there is not already a fixpoint reached, we can now take the
2343 // optimistic state. This is correct because we enforced a pessimistic one
2344 // on abstract attributes that were transitively dependent on a changed one
2345 // already above.
2346 if (!State.isAtFixpoint())
2347 State.indicateOptimisticFixpoint();
2348
2349 // We must not manifest Attributes that use Callbase info.
2350 if (AA->hasCallBaseContext())
2351 continue;
2352 // If the state is invalid, we do not try to manifest it.
2353 if (!State.isValidState())
2354 continue;
2355
2356 if (AA->getCtxI() && !isRunOn(*AA->getAnchorScope()))
2357 continue;
2358
2359 // Skip dead code.
2360 bool UsedAssumedInformation = false;
2361 if (isAssumedDead(*AA, nullptr, UsedAssumedInformation,
2362 /* CheckBBLivenessOnly */ true))
2363 continue;
2364 // Check if the manifest debug counter that allows skipping manifestation of
2365 // AAs
2366 if (!DebugCounter::shouldExecute(ManifestDBGCounter))
2367 continue;
2368 // Manifest the state and record if we changed the IR.
2369 ChangeStatus LocalChange = AA->manifest(*this);
2370 if (LocalChange == ChangeStatus::CHANGED && AreStatisticsEnabled())
2371 AA->trackStatistics();
2372 LLVM_DEBUG(dbgs() << "[Attributor] Manifest " << LocalChange << " : " << *AA
2373 << "\n");
2374
2375 ManifestChange = ManifestChange | LocalChange;
2376
2377 NumAtFixpoint++;
2378 NumManifested += (LocalChange == ChangeStatus::CHANGED);
2379 }
2380
2381 (void)NumManifested;
2382 (void)NumAtFixpoint;
2383 LLVM_DEBUG(dbgs() << "\n[Attributor] Manifested " << NumManifested
2384 << " arguments while " << NumAtFixpoint
2385 << " were in a valid fixpoint state\n");
2386
2387 NumAttributesManifested += NumManifested;
2388 NumAttributesValidFixpoint += NumAtFixpoint;
2389
2390 (void)NumFinalAAs;
2391 if (NumFinalAAs != DG.SyntheticRoot.Deps.size()) {
2392 auto DepIt = DG.SyntheticRoot.Deps.begin();
2393 for (unsigned u = 0; u < NumFinalAAs; ++u)
2394 ++DepIt;
2395 for (unsigned u = NumFinalAAs; u < DG.SyntheticRoot.Deps.size();
2396 ++u, ++DepIt) {
2397 errs() << "Unexpected abstract attribute: "
2398 << cast<AbstractAttribute>(DepIt->getPointer()) << " :: "
2399 << cast<AbstractAttribute>(DepIt->getPointer())
2400 ->getIRPosition()
2401 .getAssociatedValue()
2402 << "\n";
2403 }
2404 llvm_unreachable("Expected the final number of abstract attributes to "
2405 "remain unchanged!");
2406 }
2407
2408 for (auto &It : AttrsMap) {
2409 AttributeList &AL = It.getSecond();
2410 const IRPosition &IRP =
2411 isa<Function>(It.getFirst())
2412 ? IRPosition::function(*cast<Function>(It.getFirst()))
2413 : IRPosition::callsite_function(*cast<CallBase>(It.getFirst()));
2414 IRP.setAttrList(AL);
2415 }
2416
2417 return ManifestChange;
2418}
2419
2420void Attributor::identifyDeadInternalFunctions() {
2421 // Early exit if we don't intend to delete functions.
2422 if (!Configuration.DeleteFns)
2423 return;
2424
2425 // To avoid triggering an assertion in the lazy call graph we will not delete
2426 // any internal library functions. We should modify the assertion though and
2427 // allow internals to be deleted.
2428 const auto *TLI =
2429 isModulePass()
2430 ? nullptr
2431 : getInfoCache().getTargetLibraryInfoForFunction(*Functions.back());
2432 LibFunc LF;
2433
2434 // Identify dead internal functions and delete them. This happens outside
2435 // the other fixpoint analysis as we might treat potentially dead functions
2436 // as live to lower the number of iterations. If they happen to be dead, the
2437 // below fixpoint loop will identify and eliminate them.
2438
2439 SmallVector<Function *, 8> InternalFns;
2440 for (Function *F : Functions)
2441 if (F->hasLocalLinkage() && (isModulePass() || !TLI->getLibFunc(*F, LF)))
2442 InternalFns.push_back(F);
2443
2444 SmallPtrSet<Function *, 8> LiveInternalFns;
2445 bool FoundLiveInternal = true;
2446 while (FoundLiveInternal) {
2447 FoundLiveInternal = false;
2448 for (Function *&F : InternalFns) {
2449 if (!F)
2450 continue;
2451
2452 bool UsedAssumedInformation = false;
2454 [&](AbstractCallSite ACS) {
2456 return ToBeDeletedFunctions.count(Callee) ||
2457 (Functions.count(Callee) && Callee->hasLocalLinkage() &&
2458 !LiveInternalFns.count(Callee));
2459 },
2460 *F, true, nullptr, UsedAssumedInformation)) {
2461 continue;
2462 }
2463
2464 LiveInternalFns.insert(F);
2465 F = nullptr;
2466 FoundLiveInternal = true;
2467 }
2468 }
2469
2470 for (Function *F : InternalFns)
2471 if (F)
2472 ToBeDeletedFunctions.insert(F);
2473}
2474
2475ChangeStatus Attributor::cleanupIR() {
2476 TimeTraceScope TimeScope("Attributor::cleanupIR");
2477 // Delete stuff at the end to avoid invalid references and a nice order.
2478 LLVM_DEBUG(dbgs() << "\n[Attributor] Delete/replace at least "
2479 << ToBeDeletedFunctions.size() << " functions and "
2480 << ToBeDeletedBlocks.size() << " blocks and "
2481 << ToBeDeletedInsts.size() << " instructions and "
2482 << ToBeChangedValues.size() << " values and "
2483 << ToBeChangedUses.size() << " uses. To insert "
2484 << ToBeChangedToUnreachableInsts.size()
2485 << " unreachables.\n"
2486 << "Preserve manifest added " << ManifestAddedBlocks.size()
2487 << " blocks\n");
2488
2490 SmallVector<Instruction *, 32> TerminatorsToFold;
2491
2492 auto ReplaceUse = [&](Use *U, Value *NewV) {
2493 Value *OldV = U->get();
2494
2495 // If we plan to replace NewV we need to update it at this point.
2496 do {
2497 const auto &Entry = ToBeChangedValues.lookup(NewV);
2498 if (!get<0>(Entry))
2499 break;
2500 NewV = get<0>(Entry);
2501 } while (true);
2502
2503 Instruction *I = dyn_cast<Instruction>(U->getUser());
2504 assert((!I || isRunOn(*I->getFunction())) &&
2505 "Cannot replace an instruction outside the current SCC!");
2506
2507 // Do not replace uses in returns if the value is a must-tail call we will
2508 // not delete.
2509 if (auto *RI = dyn_cast_or_null<ReturnInst>(I)) {
2510 if (auto *CI = dyn_cast<CallInst>(OldV->stripPointerCasts()))
2511 if (CI->isMustTailCall() && !ToBeDeletedInsts.count(CI))
2512 return;
2513 // If we rewrite a return and the new value is not an argument, strip the
2514 // `returned` attribute as it is wrong now.
2515 if (!isa<Argument>(NewV))
2516 for (auto &Arg : RI->getFunction()->args())
2517 Arg.removeAttr(Attribute::Returned);
2518 }
2519
2520 LLVM_DEBUG(dbgs() << "Use " << *NewV << " in " << *U->getUser()
2521 << " instead of " << *OldV << "\n");
2522 U->set(NewV);
2523
2524 if (Instruction *I = dyn_cast<Instruction>(OldV)) {
2525 CGModifiedFunctions.insert(I->getFunction());
2526 if (!isa<PHINode>(I) && !ToBeDeletedInsts.count(I) &&
2528 DeadInsts.push_back(I);
2529 }
2530 if (isa<UndefValue>(NewV) && isa<CallBase>(U->getUser())) {
2531 auto *CB = cast<CallBase>(U->getUser());
2532 if (CB->isArgOperand(U)) {
2533 unsigned Idx = CB->getArgOperandNo(U);
2534 CB->removeParamAttr(Idx, Attribute::NoUndef);
2535 auto *Callee = dyn_cast_if_present<Function>(CB->getCalledOperand());
2536 if (Callee && Callee->arg_size() > Idx)
2537 Callee->removeParamAttr(Idx, Attribute::NoUndef);
2538 }
2539 }
2540 if (isa<Constant>(NewV) && isa<CondBrInst>(U->getUser())) {
2541 Instruction *UserI = cast<Instruction>(U->getUser());
2542 if (isa<UndefValue>(NewV)) {
2543 ToBeChangedToUnreachableInsts.insert(UserI);
2544 } else {
2545 TerminatorsToFold.push_back(UserI);
2546 }
2547 }
2548 };
2549
2550 for (auto &It : ToBeChangedUses) {
2551 Use *U = It.first;
2552 Value *NewV = It.second;
2553 ReplaceUse(U, NewV);
2554 }
2555
2557 for (auto &It : ToBeChangedValues) {
2558 Value *OldV = It.first;
2559 auto [NewV, Done] = It.second;
2560 Uses.clear();
2561 for (auto &U : OldV->uses())
2562 if (Done || !U.getUser()->isDroppable())
2563 Uses.push_back(&U);
2564 for (Use *U : Uses) {
2565 if (auto *I = dyn_cast<Instruction>(U->getUser()))
2566 if (!isRunOn(*I->getFunction()))
2567 continue;
2568 ReplaceUse(U, NewV);
2569 }
2570 }
2571
2572 for (const auto &V : InvokeWithDeadSuccessor)
2573 if (InvokeInst *II = dyn_cast_or_null<InvokeInst>(V)) {
2574 assert(isRunOn(*II->getFunction()) &&
2575 "Cannot replace an invoke outside the current SCC!");
2576 bool UnwindBBIsDead = II->hasFnAttr(Attribute::NoUnwind);
2577 bool NormalBBIsDead = II->hasFnAttr(Attribute::NoReturn);
2578 bool Invoke2CallAllowed =
2580 assert((UnwindBBIsDead || NormalBBIsDead) &&
2581 "Invoke does not have dead successors!");
2582 BasicBlock *BB = II->getParent();
2583 BasicBlock *NormalDestBB = II->getNormalDest();
2584 if (UnwindBBIsDead) {
2585 Instruction *NormalNextIP = &NormalDestBB->front();
2586 if (Invoke2CallAllowed) {
2588 NormalNextIP = BB->getTerminator();
2589 }
2590 if (NormalBBIsDead)
2591 ToBeChangedToUnreachableInsts.insert(NormalNextIP);
2592 } else {
2593 assert(NormalBBIsDead && "Broken invariant!");
2594 if (!NormalDestBB->getUniquePredecessor())
2595 NormalDestBB = SplitBlockPredecessors(NormalDestBB, {BB}, ".dead");
2596 ToBeChangedToUnreachableInsts.insert(&NormalDestBB->front());
2597 }
2598 }
2599 for (Instruction *I : TerminatorsToFold) {
2600 assert(isRunOn(*I->getFunction()) &&
2601 "Cannot replace a terminator outside the current SCC!");
2602 CGModifiedFunctions.insert(I->getFunction());
2603 ConstantFoldTerminator(I->getParent());
2604 }
2605 for (const auto &V : ToBeChangedToUnreachableInsts)
2606 if (Instruction *I = dyn_cast_or_null<Instruction>(V)) {
2607 LLVM_DEBUG(dbgs() << "[Attributor] Change to unreachable: " << *I
2608 << "\n");
2609 assert(isRunOn(*I->getFunction()) &&
2610 "Cannot replace an instruction outside the current SCC!");
2611 CGModifiedFunctions.insert(I->getFunction());
2613 }
2614
2615 for (const auto &V : ToBeDeletedInsts) {
2616 if (Instruction *I = dyn_cast_or_null<Instruction>(V)) {
2618 isRunOn(*I->getFunction())) &&
2619 "Cannot delete an instruction outside the current SCC!");
2620 I->dropDroppableUses();
2621 CGModifiedFunctions.insert(I->getFunction());
2622 if (!I->getType()->isVoidTy())
2623 I->replaceAllUsesWith(UndefValue::get(I->getType()));
2625 DeadInsts.push_back(I);
2626 else
2627 I->eraseFromParent();
2628 }
2629 }
2630
2631 llvm::erase_if(DeadInsts, [&](WeakTrackingVH I) { return !I; });
2632
2633 LLVM_DEBUG({
2634 dbgs() << "[Attributor] DeadInsts size: " << DeadInsts.size() << "\n";
2635 for (auto &I : DeadInsts)
2636 if (I)
2637 dbgs() << " - " << *I << "\n";
2638 });
2639
2641
2642 if (unsigned NumDeadBlocks = ToBeDeletedBlocks.size()) {
2643 SmallVector<BasicBlock *, 8> ToBeDeletedBBs;
2644 ToBeDeletedBBs.reserve(NumDeadBlocks);
2645 for (BasicBlock *BB : ToBeDeletedBlocks) {
2646 assert(isRunOn(*BB->getParent()) &&
2647 "Cannot delete a block outside the current SCC!");
2648 CGModifiedFunctions.insert(BB->getParent());
2649 // Do not delete BBs added during manifests of AAs.
2650 if (ManifestAddedBlocks.contains(BB))
2651 continue;
2652 ToBeDeletedBBs.push_back(BB);
2653 }
2654 // Actually we do not delete the blocks but squash them into a single
2655 // unreachable but untangling branches that jump here is something we need
2656 // to do in a more generic way.
2657 detachDeadBlocks(ToBeDeletedBBs, nullptr);
2658 }
2659
2660 identifyDeadInternalFunctions();
2661
2662 // Rewrite the functions as requested during manifest.
2663 ChangeStatus ManifestChange = rewriteFunctionSignatures(CGModifiedFunctions);
2664
2665 for (Function *Fn : CGModifiedFunctions)
2666 if (!ToBeDeletedFunctions.count(Fn) && Functions.count(Fn))
2667 Configuration.CGUpdater.reanalyzeFunction(*Fn);
2668
2669 for (Function *Fn : ToBeDeletedFunctions) {
2670 if (!Functions.count(Fn))
2671 continue;
2672 Configuration.CGUpdater.removeFunction(*Fn);
2673 }
2674
2675 if (!ToBeChangedUses.empty())
2676 ManifestChange = ChangeStatus::CHANGED;
2677
2678 if (!ToBeChangedToUnreachableInsts.empty())
2679 ManifestChange = ChangeStatus::CHANGED;
2680
2681 if (!ToBeDeletedFunctions.empty())
2682 ManifestChange = ChangeStatus::CHANGED;
2683
2684 if (!ToBeDeletedBlocks.empty())
2685 ManifestChange = ChangeStatus::CHANGED;
2686
2687 if (!ToBeDeletedInsts.empty())
2688 ManifestChange = ChangeStatus::CHANGED;
2689
2690 if (!InvokeWithDeadSuccessor.empty())
2691 ManifestChange = ChangeStatus::CHANGED;
2692
2693 if (!DeadInsts.empty())
2694 ManifestChange = ChangeStatus::CHANGED;
2695
2696 NumFnDeleted += ToBeDeletedFunctions.size();
2697
2698 LLVM_DEBUG(dbgs() << "[Attributor] Deleted " << ToBeDeletedFunctions.size()
2699 << " functions after manifest.\n");
2700
2701#ifdef EXPENSIVE_CHECKS
2702 for (Function *F : Functions) {
2703 if (ToBeDeletedFunctions.count(F))
2704 continue;
2705 assert(!verifyFunction(*F, &errs()) && "Module verification failed!");
2706 }
2707#endif
2708
2709 return ManifestChange;
2710}
2711
2713 TimeTraceScope TimeScope("Attributor::run");
2714 AttributorCallGraph ACallGraph(*this);
2715
2716 if (PrintCallGraph)
2717 ACallGraph.populateAll();
2718
2719 Phase = AttributorPhase::UPDATE;
2720 runTillFixpoint();
2721
2722 // dump graphs on demand
2723 if (DumpDepGraph)
2724 DG.dumpGraph();
2725
2726 if (ViewDepGraph)
2727 DG.viewGraph();
2728
2730 DG.print();
2731
2732 Phase = AttributorPhase::MANIFEST;
2733 ChangeStatus ManifestChange = manifestAttributes();
2734
2735 Phase = AttributorPhase::CLEANUP;
2736 ChangeStatus CleanupChange = cleanupIR();
2737
2738 if (PrintCallGraph)
2739 ACallGraph.print();
2740
2741 return ManifestChange | CleanupChange;
2742}
2743
2744ChangeStatus Attributor::updateAA(AbstractAttribute &AA) {
2745 TimeTraceScope TimeScope("updateAA", [&]() {
2746 return AA.getName().str() +
2747 std::to_string(AA.getIRPosition().getPositionKind());
2748 });
2749 assert(Phase == AttributorPhase::UPDATE &&
2750 "We can update AA only in the update stage!");
2751
2752 // Use a new dependence vector for this update.
2753 DependenceVector DV;
2754 DependenceStack.push_back(&DV);
2755
2756 auto &AAState = AA.getState();
2758 bool UsedAssumedInformation = false;
2759 if (!isAssumedDead(AA, nullptr, UsedAssumedInformation,
2760 /* CheckBBLivenessOnly */ true))
2761 CS = AA.update(*this);
2762
2763 if (!AA.isQueryAA() && DV.empty() && !AA.getState().isAtFixpoint()) {
2764 // If the AA did not rely on outside information but changed, we run it
2765 // again to see if it found a fixpoint. Most AAs do but we don't require
2766 // them to. Hence, it might take the AA multiple iterations to get to a
2767 // fixpoint even if it does not rely on outside information, which is fine.
2769 if (CS == ChangeStatus::CHANGED)
2770 RerunCS = AA.update(*this);
2771
2772 // If the attribute did not change during the run or rerun, and it still did
2773 // not query any non-fix information, the state will not change and we can
2774 // indicate that right at this point.
2775 if (RerunCS == ChangeStatus::UNCHANGED && !AA.isQueryAA() && DV.empty())
2776 AAState.indicateOptimisticFixpoint();
2777 }
2778
2779 if (!AAState.isAtFixpoint())
2780 rememberDependences();
2781
2782 // Verify the stack was used properly, that is we pop the dependence vector we
2783 // put there earlier.
2784 DependenceVector *PoppedDV = DependenceStack.pop_back_val();
2785 (void)PoppedDV;
2786 assert(PoppedDV == &DV && "Inconsistent usage of the dependence stack!");
2787
2788 return CS;
2789}
2790
2792 assert(!F.isDeclaration() && "Cannot create a wrapper around a declaration!");
2793
2794 Module &M = *F.getParent();
2795 LLVMContext &Ctx = M.getContext();
2796 FunctionType *FnTy = F.getFunctionType();
2797
2798 Function *Wrapper =
2799 Function::Create(FnTy, F.getLinkage(), F.getAddressSpace(), F.getName());
2800 F.setName(""); // set the inside function anonymous
2801 M.getFunctionList().insert(F.getIterator(), Wrapper);
2802
2803 F.setLinkage(GlobalValue::InternalLinkage);
2804
2805 F.replaceAllUsesWith(Wrapper);
2806 assert(F.use_empty() && "Uses remained after wrapper was created!");
2807
2808 // Move the COMDAT section to the wrapper.
2809 // TODO: Check if we need to keep it for F as well.
2810 Wrapper->setComdat(F.getComdat());
2811 F.setComdat(nullptr);
2812
2813 // Copy all metadata and attributes but keep them on F as well.
2815 F.getAllMetadata(MDs);
2816 for (auto MDIt : MDs)
2817 Wrapper->addMetadata(MDIt.first, *MDIt.second);
2818 Wrapper->setAttributes(F.getAttributes());
2819
2820 // Create the call in the wrapper.
2821 BasicBlock *EntryBB = BasicBlock::Create(Ctx, "entry", Wrapper);
2822
2824 Argument *FArgIt = F.arg_begin();
2825 for (Argument &Arg : Wrapper->args()) {
2826 Args.push_back(&Arg);
2827 Arg.setName((FArgIt++)->getName());
2828 }
2829
2830 CallInst *CI = CallInst::Create(&F, Args, "", EntryBB);
2831 CI->setTailCall(true);
2832 CI->addFnAttr(Attribute::NoInline);
2833 ReturnInst::Create(Ctx, CI->getType()->isVoidTy() ? nullptr : CI, EntryBB);
2834
2835 NumFnShallowWrappersCreated++;
2836}
2837
2839 if (F.isDeclaration() || F.hasLocalLinkage() ||
2841 return false;
2842 return true;
2843}
2844
2846 if (!AllowDeepWrapper && !Force)
2847 return nullptr;
2848 if (!isInternalizable(F))
2849 return nullptr;
2850
2851 SmallPtrSet<Function *, 2> FnSet = {&F};
2852 DenseMap<Function *, Function *> InternalizedFns;
2853 internalizeFunctions(FnSet, InternalizedFns);
2854
2855 return InternalizedFns[&F];
2856}
2857
2860 for (Function *F : FnSet)
2862 return false;
2863
2864 FnMap.clear();
2865 // Generate the internalized version of each function.
2866 for (Function *F : FnSet) {
2867 Module &M = *F->getParent();
2868 FunctionType *FnTy = F->getFunctionType();
2869
2870 // Create a copy of the current function
2871 Function *Copied =
2872 Function::Create(FnTy, F->getLinkage(), F->getAddressSpace(),
2873 F->getName() + ".internalized");
2874 ValueToValueMapTy VMap;
2875 auto *NewFArgIt = Copied->arg_begin();
2876 for (auto &Arg : F->args()) {
2877 auto ArgName = Arg.getName();
2878 NewFArgIt->setName(ArgName);
2879 VMap[&Arg] = &(*NewFArgIt++);
2880 }
2882
2883 // Copy the body of the original function to the new one
2884 CloneFunctionInto(Copied, F, VMap,
2886
2887 // Set the linakage and visibility late as CloneFunctionInto has some
2888 // implicit requirements.
2891
2892 // Copy metadata
2894 F->getAllMetadata(MDs);
2895 for (auto MDIt : MDs)
2896 if (!Copied->hasMetadata())
2897 Copied->addMetadata(MDIt.first, *MDIt.second);
2898
2899 M.getFunctionList().insert(F->getIterator(), Copied);
2900 Copied->setDSOLocal(true);
2901 FnMap[F] = Copied;
2902 }
2903
2904 // Replace all uses of the old function with the new internalized function
2905 // unless the caller is a function that was just internalized.
2906 for (Function *F : FnSet) {
2907 auto &InternalizedFn = FnMap[F];
2908 auto IsNotInternalized = [&](Use &U) -> bool {
2909 if (auto *CB = dyn_cast<CallBase>(U.getUser()))
2910 return !FnMap.lookup(CB->getCaller());
2911 return false;
2912 };
2913 F->replaceUsesWithIf(InternalizedFn, IsNotInternalized);
2914 }
2915
2916 return true;
2917}
2918
2920 Argument &Arg, ArrayRef<Type *> ReplacementTypes) {
2921
2922 if (!Configuration.RewriteSignatures)
2923 return false;
2924
2925 Function *Fn = Arg.getParent();
2926 auto CallSiteCanBeChanged = [Fn](AbstractCallSite ACS) {
2927 // Forbid the call site to cast the function return type. If we need to
2928 // rewrite these functions we need to re-create a cast for the new call site
2929 // (if the old had uses).
2930 if (!ACS.getCalledFunction() ||
2931 ACS.getInstruction()->getType() !=
2933 return false;
2934 if (cast<CallBase>(ACS.getInstruction())->getCalledOperand()->getType() !=
2935 Fn->getType())
2936 return false;
2937 if (ACS.getNumArgOperands() != Fn->arg_size())
2938 return false;
2939 // Forbid must-tail calls for now.
2940 return !ACS.isCallbackCall() && !ACS.getInstruction()->isMustTailCall();
2941 };
2942
2943 // Avoid var-arg functions for now.
2944 if (Fn->isVarArg()) {
2945 LLVM_DEBUG(dbgs() << "[Attributor] Cannot rewrite var-args functions\n");
2946 return false;
2947 }
2948
2949 // Avoid functions with complicated argument passing semantics.
2950 AttributeList FnAttributeList = Fn->getAttributes();
2951 if (FnAttributeList.hasAttrSomewhere(Attribute::Nest) ||
2952 FnAttributeList.hasAttrSomewhere(Attribute::StructRet) ||
2953 FnAttributeList.hasAttrSomewhere(Attribute::InAlloca) ||
2954 FnAttributeList.hasAttrSomewhere(Attribute::Preallocated)) {
2955 LLVM_DEBUG(
2956 dbgs() << "[Attributor] Cannot rewrite due to complex attribute\n");
2957 return false;
2958 }
2959
2960 // Avoid callbacks for now.
2961 bool UsedAssumedInformation = false;
2962 if (!checkForAllCallSites(CallSiteCanBeChanged, *Fn, true, nullptr,
2963 UsedAssumedInformation,
2964 /* CheckPotentiallyDead */ true)) {
2965 LLVM_DEBUG(dbgs() << "[Attributor] Cannot rewrite all call sites\n");
2966 return false;
2967 }
2968
2969 auto InstPred = [](Instruction &I) {
2970 if (auto *CI = dyn_cast<CallInst>(&I))
2971 return !CI->isMustTailCall();
2972 return true;
2973 };
2974
2975 // Forbid must-tail calls for now.
2976 // TODO:
2977 auto &OpcodeInstMap = InfoCache.getOpcodeInstMapForFunction(*Fn);
2978 if (!checkForAllInstructionsImpl(nullptr, OpcodeInstMap, InstPred, nullptr,
2979 nullptr, {Instruction::Call},
2980 UsedAssumedInformation)) {
2981 LLVM_DEBUG(dbgs() << "[Attributor] Cannot rewrite due to instructions\n");
2982 return false;
2983 }
2984
2985 return true;
2986}
2987
2989 Argument &Arg, ArrayRef<Type *> ReplacementTypes,
2992 LLVM_DEBUG(dbgs() << "[Attributor] Register new rewrite of " << Arg << " in "
2993 << Arg.getParent()->getName() << " with "
2994 << ReplacementTypes.size() << " replacements\n");
2995 assert(isValidFunctionSignatureRewrite(Arg, ReplacementTypes) &&
2996 "Cannot register an invalid rewrite");
2997
2998 Function *Fn = Arg.getParent();
3000 ArgumentReplacementMap[Fn];
3001 if (ARIs.empty())
3002 ARIs.resize(Fn->arg_size());
3003
3004 // If we have a replacement already with less than or equal new arguments,
3005 // ignore this request.
3006 std::unique_ptr<ArgumentReplacementInfo> &ARI = ARIs[Arg.getArgNo()];
3007 if (ARI && ARI->getNumReplacementArgs() <= ReplacementTypes.size()) {
3008 LLVM_DEBUG(dbgs() << "[Attributor] Existing rewrite is preferred\n");
3009 return false;
3010 }
3011
3012 // If we have a replacement already but we like the new one better, delete
3013 // the old.
3014 ARI.reset();
3015
3016 LLVM_DEBUG(dbgs() << "[Attributor] Register new rewrite of " << Arg << " in "
3017 << Arg.getParent()->getName() << " with "
3018 << ReplacementTypes.size() << " replacements\n");
3019
3020 // Remember the replacement.
3021 ARI.reset(new ArgumentReplacementInfo(*this, Arg, ReplacementTypes,
3022 std::move(CalleeRepairCB),
3023 std::move(ACSRepairCB)));
3024
3025 return true;
3026}
3027
3028bool Attributor::shouldSeedAttribute(AbstractAttribute &AA) {
3029 bool Result = true;
3030#ifndef NDEBUG
3031 if (SeedAllowList.size() != 0)
3032 Result = llvm::is_contained(SeedAllowList, AA.getName());
3033 Function *Fn = AA.getAnchorScope();
3034 if (FunctionSeedAllowList.size() != 0 && Fn)
3036#endif
3037 return Result;
3038}
3039
3040ChangeStatus Attributor::rewriteFunctionSignatures(
3041 SmallSetVector<Function *, 8> &ModifiedFns) {
3043
3044 for (auto &It : ArgumentReplacementMap) {
3045 Function *OldFn = It.getFirst();
3046
3047 // Deleted functions do not require rewrites.
3048 if (!Functions.count(OldFn) || ToBeDeletedFunctions.count(OldFn))
3049 continue;
3050
3052 It.getSecond();
3053 assert(ARIs.size() == OldFn->arg_size() && "Inconsistent state!");
3054
3055 SmallVector<Type *, 16> NewArgumentTypes;
3056 SmallVector<AttributeSet, 16> NewArgumentAttributes;
3057
3058 // Collect replacement argument types and copy over existing attributes.
3059 AttributeList OldFnAttributeList = OldFn->getAttributes();
3060 for (Argument &Arg : OldFn->args()) {
3061 if (const std::unique_ptr<ArgumentReplacementInfo> &ARI =
3062 ARIs[Arg.getArgNo()]) {
3063 NewArgumentTypes.append(ARI->ReplacementTypes.begin(),
3064 ARI->ReplacementTypes.end());
3065 NewArgumentAttributes.append(ARI->getNumReplacementArgs(),
3066 AttributeSet());
3067 } else {
3068 NewArgumentTypes.push_back(Arg.getType());
3069 NewArgumentAttributes.push_back(
3070 OldFnAttributeList.getParamAttrs(Arg.getArgNo()));
3071 }
3072 }
3073
3074 uint64_t LargestVectorWidth = 0;
3075 for (auto *I : NewArgumentTypes)
3076 if (auto *VT = dyn_cast<llvm::VectorType>(I))
3077 LargestVectorWidth =
3078 std::max(LargestVectorWidth,
3079 VT->getPrimitiveSizeInBits().getKnownMinValue());
3080
3081 FunctionType *OldFnTy = OldFn->getFunctionType();
3082 Type *RetTy = OldFnTy->getReturnType();
3083
3084 // Construct the new function type using the new arguments types.
3085 FunctionType *NewFnTy =
3086 FunctionType::get(RetTy, NewArgumentTypes, OldFnTy->isVarArg());
3087
3088 LLVM_DEBUG(dbgs() << "[Attributor] Function rewrite '" << OldFn->getName()
3089 << "' from " << *OldFn->getFunctionType() << " to "
3090 << *NewFnTy << "\n");
3091
3092 // Create the new function body and insert it into the module.
3093 Function *NewFn = Function::Create(NewFnTy, OldFn->getLinkage(),
3094 OldFn->getAddressSpace(), "");
3095 Functions.insert(NewFn);
3096 OldFn->getParent()->getFunctionList().insert(OldFn->getIterator(), NewFn);
3097 NewFn->takeName(OldFn);
3098 NewFn->copyAttributesFrom(OldFn);
3099
3100 // Patch the pointer to LLVM function in debug info descriptor.
3101 NewFn->setSubprogram(OldFn->getSubprogram());
3102 OldFn->setSubprogram(nullptr);
3103
3104 // Recompute the parameter attributes list based on the new arguments for
3105 // the function.
3106 LLVMContext &Ctx = OldFn->getContext();
3107 NewFn->setAttributes(AttributeList::get(
3108 Ctx, OldFnAttributeList.getFnAttrs(), OldFnAttributeList.getRetAttrs(),
3109 NewArgumentAttributes));
3110 AttributeFuncs::updateMinLegalVectorWidthAttr(*NewFn, LargestVectorWidth);
3111
3112 // Remove argmem from the memory effects if we have no more pointer
3113 // arguments, or they are readnone.
3114 MemoryEffects ME = NewFn->getMemoryEffects();
3115 int ArgNo = -1;
3116 if (ME.doesAccessArgPointees() && all_of(NewArgumentTypes, [&](Type *T) {
3117 ++ArgNo;
3118 return !T->isPtrOrPtrVectorTy() ||
3119 NewFn->hasParamAttribute(ArgNo, Attribute::ReadNone);
3120 })) {
3122 }
3123
3124 // Since we have now created the new function, splice the body of the old
3125 // function right into the new function, leaving the old rotting hulk of the
3126 // function empty.
3127 NewFn->splice(NewFn->begin(), OldFn);
3128
3129 // Set of all "call-like" instructions that invoke the old function mapped
3130 // to their new replacements.
3132
3133 // Callback to create a new "call-like" instruction for a given one.
3134 auto CallSiteReplacementCreator = [&](AbstractCallSite ACS) {
3135 CallBase *OldCB = cast<CallBase>(ACS.getInstruction());
3136 const AttributeList &OldCallAttributeList = OldCB->getAttributes();
3137
3138 // Collect the new argument operands for the replacement call site.
3139 SmallVector<Value *, 16> NewArgOperands;
3140 SmallVector<AttributeSet, 16> NewArgOperandAttributes;
3141 for (unsigned OldArgNum = 0; OldArgNum < ARIs.size(); ++OldArgNum) {
3142 unsigned NewFirstArgNum = NewArgOperands.size();
3143 (void)NewFirstArgNum; // only used inside assert.
3144 if (const std::unique_ptr<ArgumentReplacementInfo> &ARI =
3145 ARIs[OldArgNum]) {
3146 if (ARI->ACSRepairCB)
3147 ARI->ACSRepairCB(*ARI, ACS, NewArgOperands);
3148 assert(ARI->getNumReplacementArgs() + NewFirstArgNum ==
3149 NewArgOperands.size() &&
3150 "ACS repair callback did not provide as many operand as new "
3151 "types were registered!");
3152 // TODO: Exose the attribute set to the ACS repair callback
3153 NewArgOperandAttributes.append(ARI->ReplacementTypes.size(),
3154 AttributeSet());
3155 } else {
3156 NewArgOperands.push_back(ACS.getCallArgOperand(OldArgNum));
3157 NewArgOperandAttributes.push_back(
3158 OldCallAttributeList.getParamAttrs(OldArgNum));
3159 }
3160 }
3161
3162 assert(NewArgOperands.size() == NewArgOperandAttributes.size() &&
3163 "Mismatch # argument operands vs. # argument operand attributes!");
3164 assert(NewArgOperands.size() == NewFn->arg_size() &&
3165 "Mismatch # argument operands vs. # function arguments!");
3166
3167 SmallVector<OperandBundleDef, 4> OperandBundleDefs;
3168 OldCB->getOperandBundlesAsDefs(OperandBundleDefs);
3169
3170 // Create a new call or invoke instruction to replace the old one.
3171 CallBase *NewCB;
3172 if (InvokeInst *II = dyn_cast<InvokeInst>(OldCB)) {
3173 NewCB = InvokeInst::Create(NewFn, II->getNormalDest(),
3174 II->getUnwindDest(), NewArgOperands,
3175 OperandBundleDefs, "", OldCB->getIterator());
3176 } else {
3177 auto *NewCI = CallInst::Create(NewFn, NewArgOperands, OperandBundleDefs,
3178 "", OldCB->getIterator());
3179 NewCI->setTailCallKind(cast<CallInst>(OldCB)->getTailCallKind());
3180 NewCB = NewCI;
3181 }
3182
3183 // Copy over various properties and the new attributes.
3184 NewCB->copyMetadata(*OldCB, {LLVMContext::MD_prof, LLVMContext::MD_dbg});
3185 NewCB->setCallingConv(OldCB->getCallingConv());
3186 NewCB->takeName(OldCB);
3187 NewCB->setAttributes(AttributeList::get(
3188 Ctx, OldCallAttributeList.getFnAttrs(),
3189 OldCallAttributeList.getRetAttrs(), NewArgOperandAttributes));
3190
3191 AttributeFuncs::updateMinLegalVectorWidthAttr(*NewCB->getCaller(),
3192 LargestVectorWidth);
3193
3194 CallSitePairs.push_back({OldCB, NewCB});
3195 return true;
3196 };
3197
3198 // Use the CallSiteReplacementCreator to create replacement call sites.
3199 bool UsedAssumedInformation = false;
3200 bool Success = checkForAllCallSites(CallSiteReplacementCreator, *OldFn,
3201 true, nullptr, UsedAssumedInformation,
3202 /* CheckPotentiallyDead */ true);
3203 (void)Success;
3204 assert(Success && "Assumed call site replacement to succeed!");
3205
3206 // Rewire the arguments.
3207 Argument *OldFnArgIt = OldFn->arg_begin();
3208 Argument *NewFnArgIt = NewFn->arg_begin();
3209 for (unsigned OldArgNum = 0; OldArgNum < ARIs.size();
3210 ++OldArgNum, ++OldFnArgIt) {
3211 if (const std::unique_ptr<ArgumentReplacementInfo> &ARI =
3212 ARIs[OldArgNum]) {
3213 if (ARI->CalleeRepairCB)
3214 ARI->CalleeRepairCB(*ARI, *NewFn, NewFnArgIt);
3215 if (ARI->ReplacementTypes.empty())
3216 OldFnArgIt->replaceAllUsesWith(
3217 PoisonValue::get(OldFnArgIt->getType()));
3218 NewFnArgIt += ARI->ReplacementTypes.size();
3219 } else {
3220 NewFnArgIt->takeName(&*OldFnArgIt);
3221 OldFnArgIt->replaceAllUsesWith(&*NewFnArgIt);
3222 ++NewFnArgIt;
3223 }
3224 }
3225
3226 // Eliminate the instructions *after* we visited all of them.
3227 for (auto &CallSitePair : CallSitePairs) {
3228 CallBase &OldCB = *CallSitePair.first;
3229 CallBase &NewCB = *CallSitePair.second;
3230 assert(OldCB.getType() == NewCB.getType() &&
3231 "Cannot handle call sites with different types!");
3232 ModifiedFns.insert(OldCB.getFunction());
3233 OldCB.replaceAllUsesWith(&NewCB);
3234 OldCB.eraseFromParent();
3235 }
3236
3237 // Replace the function in the call graph (if any).
3238 Configuration.CGUpdater.replaceFunctionWith(*OldFn, *NewFn);
3239
3240 // If the old function was modified and needed to be reanalyzed, the new one
3241 // does now.
3242 if (ModifiedFns.remove(OldFn))
3243 ModifiedFns.insert(NewFn);
3244
3246 }
3247
3248 return Changed;
3249}
3250
3251void InformationCache::initializeInformationCache(const Function &CF,
3252 FunctionInfo &FI) {
3253 // As we do not modify the function here we can remove the const
3254 // withouth breaking implicit assumptions. At the end of the day, we could
3255 // initialize the cache eagerly which would look the same to the users.
3256 Function &F = const_cast<Function &>(CF);
3257
3258 FI.IsKernel = F.hasFnAttribute("kernel");
3259
3260 // Walk all instructions to find interesting instructions that might be
3261 // queried by abstract attributes during their initialization or update.
3262 // This has to happen before we create attributes.
3263
3264 DenseMap<const Value *, std::optional<short>> AssumeUsesMap;
3265
3266 // Add \p V to the assume uses map which track the number of uses outside of
3267 // "visited" assumes. If no outside uses are left the value is added to the
3268 // assume only use vector.
3269 auto AddToAssumeUsesMap = [&](const Value &V) -> void {
3270 SmallVector<const Instruction *> Worklist;
3271 if (auto *I = dyn_cast<Instruction>(&V))
3272 Worklist.push_back(I);
3273 while (!Worklist.empty()) {
3274 const Instruction *I = Worklist.pop_back_val();
3275 std::optional<short> &NumUses = AssumeUsesMap[I];
3276 if (!NumUses)
3277 NumUses = I->getNumUses();
3278 NumUses = *NumUses - /* this assume */ 1;
3279 if (*NumUses != 0)
3280 continue;
3281 AssumeOnlyValues.insert(I);
3282 for (const Value *Op : I->operands())
3283 if (auto *OpI = dyn_cast<Instruction>(Op))
3284 Worklist.push_back(OpI);
3285 }
3286 };
3287
3288 for (Instruction &I : instructions(&F)) {
3289 bool IsInterestingOpcode = false;
3290
3291 // To allow easy access to all instructions in a function with a given
3292 // opcode we store them in the InfoCache. As not all opcodes are interesting
3293 // to concrete attributes we only cache the ones that are as identified in
3294 // the following switch.
3295 // Note: There are no concrete attributes now so this is initially empty.
3296 switch (I.getOpcode()) {
3297 default:
3298 assert(!isa<CallBase>(&I) &&
3299 "New call base instruction type needs to be known in the "
3300 "Attributor.");
3301 break;
3302 case Instruction::Call:
3303 // Calls are interesting on their own, additionally:
3304 // For `llvm.assume` calls we also fill the KnowledgeMap as we find them.
3305 // For `must-tail` calls we remember the caller and callee.
3306 if (auto *Assume = dyn_cast<AssumeInst>(&I)) {
3307 AssumeOnlyValues.insert(Assume);
3308 fillMapFromAssume(*Assume, KnowledgeMap);
3309 AddToAssumeUsesMap(*Assume->getArgOperand(0));
3310 } else if (cast<CallInst>(I).isMustTailCall()) {
3311 FI.ContainsMustTailCall = true;
3312 if (auto *Callee = dyn_cast_if_present<Function>(
3313 cast<CallInst>(I).getCalledOperand()))
3314 getFunctionInfo(*Callee).CalledViaMustTail = true;
3315 }
3316 [[fallthrough]];
3317 case Instruction::CallBr:
3318 case Instruction::Invoke:
3319 case Instruction::CleanupRet:
3320 case Instruction::CatchSwitch:
3321 case Instruction::AtomicRMW:
3322 case Instruction::AtomicCmpXchg:
3323 case Instruction::UncondBr:
3324 case Instruction::CondBr:
3325 case Instruction::Resume:
3326 case Instruction::Ret:
3327 case Instruction::Load:
3328 // The alignment of a pointer is interesting for loads.
3329 case Instruction::Store:
3330 // The alignment of a pointer is interesting for stores.
3331 case Instruction::Alloca:
3332 case Instruction::AddrSpaceCast:
3333 IsInterestingOpcode = true;
3334 }
3335 if (IsInterestingOpcode) {
3336 auto *&Insts = FI.OpcodeInstMap[I.getOpcode()];
3337 if (!Insts)
3338 Insts = new (Allocator) InstructionVectorTy();
3339 Insts->push_back(&I);
3340 }
3341 if (I.mayReadOrWriteMemory())
3342 FI.RWInsts.push_back(&I);
3343 }
3344
3345 if (F.hasFnAttribute(Attribute::AlwaysInline) &&
3346 isInlineViable(F).isSuccess())
3347 InlineableFunctions.insert(&F);
3348}
3349
3350InformationCache::FunctionInfo::~FunctionInfo() {
3351 // The instruction vectors are allocated using a BumpPtrAllocator, we need to
3352 // manually destroy them.
3353 for (auto &It : OpcodeInstMap)
3354 It.getSecond()->~InstructionVectorTy();
3355}
3356
3359 assert(A.isClosedWorldModule() && "Cannot see all indirect callees!");
3360 return IndirectlyCallableFunctions;
3361}
3362
3363std::optional<unsigned> InformationCache::getFlatAddressSpace() const {
3364 if (IsTargetGPU())
3365 return 0;
3366 return std::nullopt;
3367}
3368
3370 const AbstractAttribute &ToAA,
3371 DepClassTy DepClass) {
3372 if (DepClass == DepClassTy::NONE)
3373 return;
3374 // If we are outside of an update, thus before the actual fixpoint iteration
3375 // started (= when we create AAs), we do not track dependences because we will
3376 // put all AAs into the initial worklist anyway.
3377 if (DependenceStack.empty())
3378 return;
3379 if (FromAA.getState().isAtFixpoint())
3380 return;
3381 DependenceStack.back()->push_back({&FromAA, &ToAA, DepClass});
3382}
3383
3384void Attributor::rememberDependences() {
3385 assert(!DependenceStack.empty() && "No dependences to remember!");
3386
3387 for (DepInfo &DI : *DependenceStack.back()) {
3388 assert((DI.DepClass == DepClassTy::REQUIRED ||
3389 DI.DepClass == DepClassTy::OPTIONAL) &&
3390 "Expected required or optional dependence (1 bit)!");
3391 auto &DepAAs = const_cast<AbstractAttribute &>(*DI.FromAA).Deps;
3392 DepAAs.insert(AbstractAttribute::DepTy(
3393 const_cast<AbstractAttribute *>(DI.ToAA), unsigned(DI.DepClass)));
3394 }
3395}
3396
3397template <Attribute::AttrKind AK, typename AAType>
3398void Attributor::checkAndQueryIRAttr(const IRPosition &IRP, AttributeSet Attrs,
3399 bool SkipHasAttrCheck) {
3400 bool IsKnown;
3401 if (SkipHasAttrCheck || !Attrs.hasAttribute(AK))
3402 if (!Configuration.Allowed || Configuration.Allowed->count(&AAType::ID))
3403 if (!AA::hasAssumedIRAttr<AK>(*this, nullptr, IRP, DepClassTy::NONE,
3404 IsKnown))
3405 getOrCreateAAFor<AAType>(IRP);
3406}
3407
3409 assert(!F.isDeclaration());
3410
3411 if (!VisitedFunctions.insert(&F).second)
3412 return;
3413
3414 // In non-module runs we need to look at the call sites of a function to
3415 // determine if it is part of a must-tail call edge. This will influence what
3416 // attributes we can derive.
3417 InformationCache::FunctionInfo &FI = InfoCache.getFunctionInfo(F);
3418 if (!isModulePass() && !FI.CalledViaMustTail) {
3419 for (const Use &U : F.uses())
3420 if (const auto *CB = dyn_cast<CallBase>(U.getUser()))
3421 if (CB->isCallee(&U) && CB->isMustTailCall())
3422 FI.CalledViaMustTail = true;
3423 }
3424
3426 bool IsIPOAmendable = isFunctionIPOAmendable(F);
3427 auto Attrs = F.getAttributes();
3428 auto FnAttrs = Attrs.getFnAttrs();
3429
3430 // Check for dead BasicBlocks in every function.
3431 // We need dead instruction detection because we do not want to deal with
3432 // broken IR in which SSA rules do not apply.
3434
3435 // Every function might contain instructions that cause "undefined
3436 // behavior".
3438
3439 // Every function might be applicable for Heap-To-Stack conversion.
3442
3443 // Every function might be "must-progress".
3444 checkAndQueryIRAttr<Attribute::MustProgress, AAMustProgress>(FPos, FnAttrs);
3445
3446 // Every function might be "no-free".
3447 checkAndQueryIRAttr<Attribute::NoFree, AANoFree>(FPos, FnAttrs);
3448
3449 // Every function might be "will-return".
3450 checkAndQueryIRAttr<Attribute::WillReturn, AAWillReturn>(FPos, FnAttrs);
3451
3452 // Every function might be marked "nosync"
3453 checkAndQueryIRAttr<Attribute::NoSync, AANoSync>(FPos, FnAttrs);
3454
3455 // Everything that is visible from the outside (=function, argument, return
3456 // positions), cannot be changed if the function is not IPO amendable. We can
3457 // however analyse the code inside.
3458 if (IsIPOAmendable) {
3459
3460 // Every function can be nounwind.
3461 checkAndQueryIRAttr<Attribute::NoUnwind, AANoUnwind>(FPos, FnAttrs);
3462
3463 // Every function might be "no-return".
3464 checkAndQueryIRAttr<Attribute::NoReturn, AANoReturn>(FPos, FnAttrs);
3465
3466 // Every function might be "no-recurse".
3467 checkAndQueryIRAttr<Attribute::NoRecurse, AANoRecurse>(FPos, FnAttrs);
3468
3469 // Every function can be "non-convergent".
3470 if (Attrs.hasFnAttr(Attribute::Convergent))
3472
3473 // Every function might be "readnone/readonly/writeonly/...".
3475
3476 // Every function can be "readnone/argmemonly/inaccessiblememonly/...".
3478
3479 // Every function can track active assumptions.
3481
3482 // If we're not using a dynamic mode for float, there's nothing worthwhile
3483 // to infer. This misses the edge case denormal-fp-math="dynamic" and
3484 // denormal-fp-math-f32=something, but that likely has no real world use.
3485 DenormalMode Mode = F.getDenormalMode(APFloat::IEEEsingle());
3486 if (Mode.Input == DenormalMode::Dynamic ||
3487 Mode.Output == DenormalMode::Dynamic)
3489
3490 // Return attributes are only appropriate if the return type is non void.
3491 Type *ReturnType = F.getReturnType();
3492 if (!ReturnType->isVoidTy()) {
3494 AttributeSet RetAttrs = Attrs.getRetAttrs();
3495
3496 // Every returned value might be dead.
3498
3499 // Every function might be simplified.
3500 bool UsedAssumedInformation = false;
3501 getAssumedSimplified(RetPos, nullptr, UsedAssumedInformation,
3503
3504 // Every returned value might be marked noundef.
3505 checkAndQueryIRAttr<Attribute::NoUndef, AANoUndef>(RetPos, RetAttrs);
3506
3507 if (ReturnType->isPointerTy()) {
3508
3509 // Every function with pointer return type might be marked align.
3511
3512 // Every function with pointer return type might be marked nonnull.
3513 checkAndQueryIRAttr<Attribute::NonNull, AANonNull>(RetPos, RetAttrs);
3514
3515 // Every function with pointer return type might be marked noalias.
3516 checkAndQueryIRAttr<Attribute::NoAlias, AANoAlias>(RetPos, RetAttrs);
3517
3518 // Every function with pointer return type might be marked
3519 // dereferenceable.
3521 } else if (AttributeFuncs::isNoFPClassCompatibleType(ReturnType)) {
3523 }
3524 }
3525 }
3526
3527 for (Argument &Arg : F.args()) {
3528 IRPosition ArgPos = IRPosition::argument(Arg);
3529 auto ArgNo = Arg.getArgNo();
3530 AttributeSet ArgAttrs = Attrs.getParamAttrs(ArgNo);
3531
3532 if (!IsIPOAmendable) {
3533 if (Arg.getType()->isPointerTy())
3534 // Every argument with pointer type might be marked nofree.
3535 checkAndQueryIRAttr<Attribute::NoFree, AANoFree>(ArgPos, ArgAttrs);
3536 continue;
3537 }
3538
3539 // Every argument might be simplified. We have to go through the
3540 // Attributor interface though as outside AAs can register custom
3541 // simplification callbacks.
3542 bool UsedAssumedInformation = false;
3543 getAssumedSimplified(ArgPos, /* AA */ nullptr, UsedAssumedInformation,
3545
3546 // Every argument might be dead.
3548
3549 // Every argument might be marked noundef.
3550 checkAndQueryIRAttr<Attribute::NoUndef, AANoUndef>(ArgPos, ArgAttrs);
3551
3552 if (Arg.getType()->isPointerTy()) {
3553 // Every argument with pointer type might be marked nonnull.
3554 checkAndQueryIRAttr<Attribute::NonNull, AANonNull>(ArgPos, ArgAttrs);
3555
3556 // Every argument with pointer type might be marked noalias.
3557 checkAndQueryIRAttr<Attribute::NoAlias, AANoAlias>(ArgPos, ArgAttrs);
3558
3559 // Every argument with pointer type might be marked dereferenceable.
3561
3562 // Every argument with pointer type might be marked align.
3564
3565 // Every argument with pointer type might be marked nocapture.
3566 checkAndQueryIRAttr<Attribute::Captures, AANoCapture>(
3567 ArgPos, ArgAttrs, /*SkipHasAttrCheck=*/true);
3568
3569 // Every argument with pointer type might be marked
3570 // "readnone/readonly/writeonly/..."
3572
3573 // Every argument with pointer type might be marked nofree.
3574 checkAndQueryIRAttr<Attribute::NoFree, AANoFree>(ArgPos, ArgAttrs);
3575
3576 // Every argument with pointer type might be privatizable (or
3577 // promotable)
3579 } else if (AttributeFuncs::isNoFPClassCompatibleType(Arg.getType())) {
3581 }
3582 }
3583
3584 auto CallSitePred = [&](Instruction &I) -> bool {
3585 auto &CB = cast<CallBase>(I);
3586 IRPosition CBInstPos = IRPosition::inst(CB);
3588
3589 // Call sites might be dead if they do not have side effects and no live
3590 // users. The return value might be dead if there are no live users.
3591 getOrCreateAAFor<AAIsDead>(CBInstPos);
3592
3593 Function *Callee = dyn_cast_if_present<Function>(CB.getCalledOperand());
3594 // TODO: Even if the callee is not known now we might be able to simplify
3595 // the call/callee.
3596 if (!Callee) {
3598 return true;
3599 }
3600
3601 // Every call site can track active assumptions.
3603
3604 // Skip declarations except if annotations on their call sites were
3605 // explicitly requested.
3606 if (!AnnotateDeclarationCallSites && Callee->isDeclaration() &&
3607 !Callee->hasMetadata(LLVMContext::MD_callback))
3608 return true;
3609
3610 if (!Callee->getReturnType()->isVoidTy() && !CB.use_empty()) {
3612 bool UsedAssumedInformation = false;
3613 getAssumedSimplified(CBRetPos, nullptr, UsedAssumedInformation,
3615
3616 if (AttributeFuncs::isNoFPClassCompatibleType(Callee->getReturnType()))
3618 }
3619
3620 const AttributeList &CBAttrs = CBFnPos.getAttrList();
3621 for (int I = 0, E = CB.arg_size(); I < E; ++I) {
3622
3624 AttributeSet CBArgAttrs = CBAttrs.getParamAttrs(I);
3625
3626 // Every call site argument might be dead.
3628
3629 // Call site argument might be simplified. We have to go through the
3630 // Attributor interface though as outside AAs can register custom
3631 // simplification callbacks.
3632 bool UsedAssumedInformation = false;
3633 getAssumedSimplified(CBArgPos, /* AA */ nullptr, UsedAssumedInformation,
3635
3636 // Every call site argument might be marked "noundef".
3637 checkAndQueryIRAttr<Attribute::NoUndef, AANoUndef>(CBArgPos, CBArgAttrs);
3638
3639 Type *ArgTy = CB.getArgOperand(I)->getType();
3640
3641 if (!ArgTy->isPointerTy()) {
3642 if (AttributeFuncs::isNoFPClassCompatibleType(ArgTy))
3644
3645 continue;
3646 }
3647
3648 // Call site argument attribute "non-null".
3649 checkAndQueryIRAttr<Attribute::NonNull, AANonNull>(CBArgPos, CBArgAttrs);
3650
3651 // Call site argument attribute "captures(none)".
3652 checkAndQueryIRAttr<Attribute::Captures, AANoCapture>(
3653 CBArgPos, CBArgAttrs, /*SkipHasAttrCheck=*/true);
3654
3655 // Call site argument attribute "no-alias".
3656 checkAndQueryIRAttr<Attribute::NoAlias, AANoAlias>(CBArgPos, CBArgAttrs);
3657
3658 // Call site argument attribute "dereferenceable".
3660
3661 // Call site argument attribute "align".
3662 getOrCreateAAFor<AAAlign>(CBArgPos);
3663
3664 // Call site argument attribute
3665 // "readnone/readonly/writeonly/..."
3666 if (!CBAttrs.hasParamAttr(I, Attribute::ReadNone))
3668
3669 // Call site argument attribute "nofree".
3670 checkAndQueryIRAttr<Attribute::NoFree, AANoFree>(CBArgPos, CBArgAttrs);
3671 }
3672 return true;
3673 };
3674
3675 auto &OpcodeInstMap = InfoCache.getOpcodeInstMapForFunction(F);
3676 [[maybe_unused]] bool Success;
3677 bool UsedAssumedInformation = false;
3679 nullptr, OpcodeInstMap, CallSitePred, nullptr, nullptr,
3680 {(unsigned)Instruction::Invoke, (unsigned)Instruction::CallBr,
3681 (unsigned)Instruction::Call},
3682 UsedAssumedInformation);
3683 assert(Success && "Expected the check call to be successful!");
3684
3685 auto LoadStorePred = [&](Instruction &I) -> bool {
3686 if (auto *LI = dyn_cast<LoadInst>(&I)) {
3687 getOrCreateAAFor<AAAlign>(IRPosition::value(*LI->getPointerOperand()));
3688 if (SimplifyAllLoads)
3690 UsedAssumedInformation, AA::Intraprocedural);
3692 IRPosition::value(*LI->getPointerOperand()));
3694 IRPosition::value(*LI->getPointerOperand()));
3695 } else {
3696 auto &SI = cast<StoreInst>(I);
3698 getAssumedSimplified(IRPosition::value(*SI.getValueOperand()), nullptr,
3699 UsedAssumedInformation, AA::Intraprocedural);
3700 getOrCreateAAFor<AAAlign>(IRPosition::value(*SI.getPointerOperand()));
3702 IRPosition::value(*SI.getPointerOperand()));
3703 }
3704 return true;
3705 };
3707 nullptr, OpcodeInstMap, LoadStorePred, nullptr, nullptr,
3708 {(unsigned)Instruction::Load, (unsigned)Instruction::Store},
3709 UsedAssumedInformation);
3710 assert(Success && "Expected the check call to be successful!");
3711
3712 // AllocaInstPredicate
3713 auto AAAllocationInfoPred = [&](Instruction &I) -> bool {
3715 return true;
3716 };
3717
3719 nullptr, OpcodeInstMap, AAAllocationInfoPred, nullptr, nullptr,
3720 {(unsigned)Instruction::Alloca}, UsedAssumedInformation);
3721 assert(Success && "Expected the check call to be successful!");
3722}
3723
3725 if (CloseWorldAssumption.getNumOccurrences())
3726 return CloseWorldAssumption;
3727 return isModulePass() && Configuration.IsClosedWorldModule;
3728}
3729
3730/// Helpers to ease debugging through output streams and print calls.
3731///
3732///{
3734 return OS << (S == ChangeStatus::CHANGED ? "changed" : "unchanged");
3735}
3736
3738 switch (AP) {
3740 return OS << "inv";
3742 return OS << "flt";
3744 return OS << "fn_ret";
3746 return OS << "cs_ret";
3748 return OS << "fn";
3750 return OS << "cs";
3752 return OS << "arg";
3754 return OS << "cs_arg";
3755 }
3756 llvm_unreachable("Unknown attribute position!");
3757}
3758
3760 const Value &AV = Pos.getAssociatedValue();
3761 OS << "{" << Pos.getPositionKind() << ":" << AV.getName() << " ["
3762 << Pos.getAnchorValue().getName() << "@" << Pos.getCallSiteArgNo() << "]";
3763
3764 if (Pos.hasCallBaseContext())
3765 OS << "[cb_context:" << *Pos.getCallBaseContext() << "]";
3766 return OS << "}";
3767}
3768
3770 OS << "range-state(" << S.getBitWidth() << ")<";
3771 S.getKnown().print(OS);
3772 OS << " / ";
3773 S.getAssumed().print(OS);
3774 OS << ">";
3775
3776 return OS << static_cast<const AbstractState &>(S);
3777}
3778
3780 return OS << (!S.isValidState() ? "top" : (S.isAtFixpoint() ? "fix" : ""));
3781}
3782
3784 AA.print(OS);
3785 return OS;
3786}
3787
3790 OS << "set-state(< {";
3791 if (!S.isValidState())
3792 OS << "full-set";
3793 else {
3794 for (const auto &It : S.getAssumedSet())
3795 OS << It << ", ";
3796 if (S.undefIsContained())
3797 OS << "undef ";
3798 }
3799 OS << "} >)";
3800
3801 return OS;
3802}
3803
3805 const PotentialLLVMValuesState &S) {
3806 OS << "set-state(< {";
3807 if (!S.isValidState())
3808 OS << "full-set";
3809 else {
3810 for (const auto &It : S.getAssumedSet()) {
3811 if (auto *F = dyn_cast<Function>(It.first.getValue()))
3812 OS << "@" << F->getName() << "[" << int(It.second) << "], ";
3813 else
3814 OS << *It.first.getValue() << "[" << int(It.second) << "], ";
3815 }
3816 if (S.undefIsContained())
3817 OS << "undef ";
3818 }
3819 OS << "} >)";
3820
3821 return OS;
3822}
3823
3825 OS << "[";
3826 OS << getName();
3827 OS << "] for CtxI ";
3828
3829 if (auto *I = getCtxI()) {
3830 OS << "'";
3831 I->print(OS);
3832 OS << "'";
3833 } else
3834 OS << "<<null inst>>";
3835
3836 OS << " at position " << getIRPosition() << " with state " << getAsStr(A)
3837 << '\n';
3838}
3839
3841 print(OS);
3842
3843 for (const auto &DepAA : Deps) {
3844 auto *AA = DepAA.getPointer();
3845 OS << " updates ";
3846 AA->print(OS);
3847 }
3848
3849 OS << '\n';
3850}
3851
3853 const AAPointerInfo::Access &Acc) {
3854 OS << " [" << Acc.getKind() << "] " << *Acc.getRemoteInst();
3855 if (Acc.getLocalInst() != Acc.getRemoteInst())
3856 OS << " via " << *Acc.getLocalInst();
3857 if (Acc.getContent()) {
3858 if (*Acc.getContent())
3859 OS << " [" << **Acc.getContent() << "]";
3860 else
3861 OS << " [ <unknown> ]";
3862 }
3863 return OS;
3864}
3865///}
3866
3867/// ----------------------------------------------------------------------------
3868/// Pass (Manager) Boilerplate
3869/// ----------------------------------------------------------------------------
3870
3872 SetVector<Function *> &Functions,
3873 AnalysisGetter &AG,
3874 CallGraphUpdater &CGUpdater,
3875 bool DeleteFns, bool IsModulePass) {
3876 if (Functions.empty())
3877 return false;
3878
3879 LLVM_DEBUG({
3880 dbgs() << "[Attributor] Run on module with " << Functions.size()
3881 << " functions:\n";
3882 for (Function *Fn : Functions)
3883 dbgs() << " - " << Fn->getName() << "\n";
3884 });
3885
3886 // Create an Attributor and initially empty information cache that is filled
3887 // while we identify default attribute opportunities.
3888 AttributorConfig AC(CGUpdater);
3889 AC.IsModulePass = IsModulePass;
3890 AC.DeleteFns = DeleteFns;
3891
3892 /// Tracking callback for specialization of indirect calls.
3894 IndirectCalleeTrackingMap;
3895 if (MaxSpecializationPerCB.getNumOccurrences()) {
3896 AC.IndirectCalleeSpecializationCallback =
3897 [&](Attributor &, const AbstractAttribute &AA, CallBase &CB,
3898 Function &Callee, unsigned) {
3899 if (MaxSpecializationPerCB == 0)
3900 return false;
3901 auto &Set = IndirectCalleeTrackingMap[&CB];
3902 if (!Set)
3903 Set = std::make_unique<SmallPtrSet<Function *, 8>>();
3904 if (Set->size() >= MaxSpecializationPerCB)
3905 return Set->contains(&Callee);
3906 Set->insert(&Callee);
3907 return true;
3908 };
3909 }
3910
3911 Attributor A(Functions, InfoCache, AC);
3912
3913 // Create shallow wrappers for all functions that are not IPO amendable
3915 for (Function *F : Functions)
3916 if (!A.isFunctionIPOAmendable(*F))
3918
3919 // Internalize non-exact functions
3920 // TODO: for now we eagerly internalize functions without calculating the
3921 // cost, we need a cost interface to determine whether internalizing
3922 // a function is "beneficial"
3923 if (AllowDeepWrapper) {
3924 unsigned FunSize = Functions.size();
3925 for (unsigned u = 0; u < FunSize; u++) {
3926 Function *F = Functions[u];
3927 if (!F->isDeclaration() && !F->isDefinitionExact() && !F->use_empty() &&
3928 !GlobalValue::isInterposableLinkage(F->getLinkage())) {
3930 assert(NewF && "Could not internalize function.");
3931 Functions.insert(NewF);
3932
3933 // Update call graph
3934 CGUpdater.replaceFunctionWith(*F, *NewF);
3935 for (const Use &U : NewF->uses())
3936 if (CallBase *CB = dyn_cast<CallBase>(U.getUser())) {
3937 auto *CallerF = CB->getCaller();
3938 CGUpdater.reanalyzeFunction(*CallerF);
3939 }
3940 }
3941 }
3942 }
3943
3944 for (Function *F : Functions) {
3945 if (F->isDeclaration())
3946 continue;
3947
3948 if (F->hasExactDefinition())
3949 NumFnWithExactDefinition++;
3950 else
3951 NumFnWithoutExactDefinition++;
3952
3953 // We look at internal functions only on-demand but if any use is not a
3954 // direct call or outside the current set of analyzed functions, we have
3955 // to do it eagerly.
3956 if (F->hasLocalLinkage()) {
3957 if (llvm::all_of(F->uses(), [&Functions](const Use &U) {
3958 const auto *CB = dyn_cast<CallBase>(U.getUser());
3959 return CB && CB->isCallee(&U) &&
3960 Functions.count(const_cast<Function *>(CB->getCaller()));
3961 }))
3962 continue;
3963 }
3964
3965 // Populate the Attributor with abstract attribute opportunities in the
3966 // function and the information cache with IR information.
3967 A.identifyDefaultAbstractAttributes(*F);
3968 }
3969
3970 ChangeStatus Changed = A.run();
3971
3972 LLVM_DEBUG(dbgs() << "[Attributor] Done with " << Functions.size()
3973 << " functions, result: " << Changed << ".\n");
3975}
3976
3978 SetVector<Function *> &Functions,
3979 AnalysisGetter &AG,
3980 CallGraphUpdater &CGUpdater,
3982 bool IsModulePass) {
3983 if (Functions.empty())
3984 return false;
3985
3986 LLVM_DEBUG({
3987 dbgs() << "[AttributorLight] Run on module with " << Functions.size()
3988 << " functions:\n";
3989 for (Function *Fn : Functions)
3990 dbgs() << " - " << Fn->getName() << "\n";
3991 });
3992
3993 // Create an Attributor and initially empty information cache that is filled
3994 // while we identify default attribute opportunities.
3995 AttributorConfig AC(CGUpdater);
3996 AC.IsModulePass = IsModulePass;
3997 AC.DeleteFns = false;
3998 DenseSet<const char *> Allowed(
4005 AC.Allowed = &Allowed;
4006 AC.UseLiveness = false;
4007
4008 Attributor A(Functions, InfoCache, AC);
4009
4010 for (Function *F : Functions) {
4011 if (F->isDeclaration())
4012 continue;
4013
4014 if (F->hasExactDefinition())
4015 NumFnWithExactDefinition++;
4016 else
4017 NumFnWithoutExactDefinition++;
4018
4019 // We look at internal functions only on-demand but if any use is not a
4020 // direct call or outside the current set of analyzed functions, we have
4021 // to do it eagerly.
4022 if (AC.UseLiveness && F->hasLocalLinkage()) {
4023 if (llvm::all_of(F->uses(), [&Functions](const Use &U) {
4024 const auto *CB = dyn_cast<CallBase>(U.getUser());
4025 return CB && CB->isCallee(&U) &&
4026 Functions.count(const_cast<Function *>(CB->getCaller()));
4027 }))
4028 continue;
4029 }
4030
4031 // Populate the Attributor with abstract attribute opportunities in the
4032 // function and the information cache with IR information.
4033 A.identifyDefaultAbstractAttributes(*F);
4034 }
4035
4036 ChangeStatus Changed = A.run();
4037
4039 // Invalidate analyses for modified functions so that we don't have to
4040 // invalidate all analyses for all functions in this SCC.
4041 PreservedAnalyses FuncPA;
4042 // We haven't changed the CFG for modified functions.
4043 FuncPA.preserveSet<CFGAnalyses>();
4044 for (Function *Changed : A.getModifiedFunctions()) {
4045 FAM.invalidate(*Changed, FuncPA);
4046 // Also invalidate any direct callers of changed functions since analyses
4047 // may care about attributes of direct callees. For example, MemorySSA
4048 // cares about whether or not a call's callee modifies memory and queries
4049 // that through function attributes.
4050 for (auto *U : Changed->users()) {
4051 if (auto *Call = dyn_cast<CallBase>(U)) {
4052 if (Call->getCalledFunction() == Changed)
4053 FAM.invalidate(*Call->getFunction(), FuncPA);
4054 }
4055 }
4056 }
4057 }
4058 LLVM_DEBUG(dbgs() << "[Attributor] Done with " << Functions.size()
4059 << " functions, result: " << Changed << ".\n");
4061}
4062
4063void AADepGraph::viewGraph() { llvm::ViewGraph(this, "Dependency Graph"); }
4064
4066 static std::atomic<int> CallTimes;
4067 std::string Prefix;
4068
4069 if (!DepGraphDotFileNamePrefix.empty())
4071 else
4072 Prefix = "dep_graph";
4073 std::string Filename =
4074 Prefix + "_" + std::to_string(CallTimes.load()) + ".dot";
4075
4076 outs() << "Dependency graph dump to " << Filename << ".\n";
4077
4078 std::error_code EC;
4079
4081 if (!EC)
4082 llvm::WriteGraph(File, this);
4083
4084 CallTimes++;
4085}
4086
4088 for (auto DepAA : SyntheticRoot.Deps)
4089 cast<AbstractAttribute>(DepAA.getPointer())->printWithDeps(outs());
4090}
4091
4095 AnalysisGetter AG(FAM);
4096
4097 SetVector<Function *> Functions;
4098 for (Function &F : M)
4099 Functions.insert(&F);
4100
4101 CallGraphUpdater CGUpdater;
4102 BumpPtrAllocator Allocator;
4103 InformationCache InfoCache(M, AG, Allocator, /* CGSCC */ nullptr);
4104 if (runAttributorOnFunctions(InfoCache, Functions, AG, CGUpdater,
4105 /* DeleteFns */ true, /* IsModulePass */ true)) {
4106 // FIXME: Think about passes we will preserve and add them here.
4107 return PreservedAnalyses::none();
4108 }
4109 return PreservedAnalyses::all();
4110}
4111
4114 LazyCallGraph &CG,
4115 CGSCCUpdateResult &UR) {
4117 AM.getResult<FunctionAnalysisManagerCGSCCProxy>(C, CG).getManager();
4118 AnalysisGetter AG(FAM);
4119
4120 SetVector<Function *> Functions;
4121 for (LazyCallGraph::Node &N : C)
4122 Functions.insert(&N.getFunction());
4123
4124 if (Functions.empty())
4125 return PreservedAnalyses::all();
4126
4127 Module &M = *Functions.back()->getParent();
4128 CallGraphUpdater CGUpdater;
4129 CGUpdater.initialize(CG, C, AM, UR);
4130 BumpPtrAllocator Allocator;
4131 InformationCache InfoCache(M, AG, Allocator, /* CGSCC */ &Functions);
4132 if (runAttributorOnFunctions(InfoCache, Functions, AG, CGUpdater,
4133 /* DeleteFns */ false,
4134 /* IsModulePass */ false)) {
4135 // FIXME: Think about passes we will preserve and add them here.
4138 return PA;
4139 }
4140 return PreservedAnalyses::all();
4141}
4142
4147 AnalysisGetter AG(FAM, /* CachedOnly */ true);
4148
4149 SetVector<Function *> Functions;
4150 for (Function &F : M)
4151 Functions.insert(&F);
4152
4153 CallGraphUpdater CGUpdater;
4154 BumpPtrAllocator Allocator;
4155 InformationCache InfoCache(M, AG, Allocator, /* CGSCC */ nullptr);
4156 if (runAttributorLightOnFunctions(InfoCache, Functions, AG, CGUpdater, FAM,
4157 /* IsModulePass */ true)) {
4159 // We have not added or removed functions.
4161 // We already invalidated all relevant function analyses above.
4163 return PA;
4164 }
4165 return PreservedAnalyses::all();
4166}
4167
4170 LazyCallGraph &CG,
4171 CGSCCUpdateResult &UR) {
4173 AM.getResult<FunctionAnalysisManagerCGSCCProxy>(C, CG).getManager();
4174 AnalysisGetter AG(FAM);
4175
4176 SetVector<Function *> Functions;
4177 for (LazyCallGraph::Node &N : C)
4178 Functions.insert(&N.getFunction());
4179
4180 if (Functions.empty())
4181 return PreservedAnalyses::all();
4182
4183 Module &M = *Functions.back()->getParent();
4184 CallGraphUpdater CGUpdater;
4185 CGUpdater.initialize(CG, C, AM, UR);
4186 BumpPtrAllocator Allocator;
4187 InformationCache InfoCache(M, AG, Allocator, /* CGSCC */ &Functions);
4188 if (runAttributorLightOnFunctions(InfoCache, Functions, AG, CGUpdater, FAM,
4189 /* IsModulePass */ false)) {
4191 // We have not added or removed functions.
4193 // We already invalidated all relevant function analyses above.
4195 return PA;
4196 }
4197 return PreservedAnalyses::all();
4198}
4199namespace llvm {
4200
4217
4218template <>
4220 static NodeRef getEntryNode(AADepGraph *DG) { return DG->GetEntryNode(); }
4221
4224
4225 static nodes_iterator nodes_begin(AADepGraph *DG) { return DG->begin(); }
4226
4227 static nodes_iterator nodes_end(AADepGraph *DG) { return DG->end(); }
4228};
4229
4230template <> struct DOTGraphTraits<AADepGraph *> : public DefaultDOTGraphTraits {
4232
4233 static std::string getNodeLabel(const AADepGraphNode *Node,
4234 const AADepGraph *DG) {
4235 std::string AAString;
4236 raw_string_ostream O(AAString);
4237 Node->print(O);
4238 return AAString;
4239 }
4240};
4241
4242} // end namespace llvm
aarch64 falkor hwpf fix Falkor HW Prefetch Fix Late Phase
static unsigned getIntrinsicID(const SDNode *N)
@ Generic
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:54
#define I(x, y, z)
Definition MD5.cpp:57
#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
static constexpr StringLiteral Filename
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:171
#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:72
void print(OutputBuffer &OB) const
static const fltSemantics & IEEEsingle()
Definition APFloat.h:296
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
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
size_t size() const
Get the array size.
Definition ArrayRef.h:141
bool empty() const
Check if the array is empty.
Definition ArrayRef.h:136
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:407
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:105
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:124
@ None
No attributes have been set.
Definition Attributes.h:126
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:484
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; assumes that the block is well-formed.
Definition BasicBlock.h:237
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:1310
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:64
static bool shouldExecute(CounterInfo &Counter)
ValueT lookup(const_arg_type_t< KeyT > Val) const
Return the entry for the specified key, or a default constructed value if no such entry exists.
Definition DenseMap.h:252
bool empty() const
Definition DenseMap.h:173
Implements a dense probed hash-table based set.
Definition DenseSet.h:289
Analysis pass which computes a DominatorTree.
Definition Dominators.h:274
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition Dominators.h:155
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:168
void splice(Function::iterator ToIt, Function *FromF)
Transfer all blocks from FromF to this function at ToIt.
Definition Function.h:761
const BasicBlock & getEntryBlock() const
Definition Function.h:809
FunctionType * getFunctionType() const
Returns the FunctionType for me.
Definition Function.h:211
iterator_range< arg_iterator > args()
Definition Function.h:892
DISubprogram * getSubprogram() const
Get the attached subprogram.
MemoryEffects getMemoryEffects() const
Definition Function.cpp:859
bool hasParamAttribute(unsigned ArgNo, Attribute::AttrKind Kind) const
check if an attributes is in the list of attributes.
Definition Function.cpp:740
AttributeList getAttributes() const
Return the attribute list for this Function.
Definition Function.h:354
iterator begin()
Definition Function.h:853
arg_iterator arg_begin()
Definition Function.h:868
void setAttributes(AttributeList Attrs)
Set the attribute list for this Function.
Definition Function.h:357
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function.
Definition Function.cpp:358
size_t arg_size() const
Definition Function.h:901
Type * getReturnType() const
Returns the type of the ret val.
Definition Function.h:216
void setMemoryEffects(MemoryEffects ME)
Definition Function.cpp:862
Argument * getArg(unsigned i) const
Definition Function.h:886
bool isVarArg() const
isVarArg - Return true if this function takes a variable number of arguments.
Definition Function.h:229
void copyAttributesFrom(const Function *Src)
copyAttributesFrom - copy all additional attributes (those not needed to create a Function) from the ...
Definition Function.cpp:843
bool hasMetadata() const
Return true if this GlobalObject has any metadata attached to it.
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:337
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:143
bool doesAccessArgPointees() const
Whether this function may access argument memory.
Definition ModRef.h:260
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:598
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:57
ArrayRef< value_type > getArrayRef() const
Definition SetVector.h:91
bool remove(const value_type &X)
Remove an item from the set vector.
Definition SetVector.h:181
size_type size() const
Determine the number of elements in the SetVector.
Definition SetVector.h:103
void insert_range(Range &&R)
Definition SetVector.h:176
size_type count(const_arg_type key) const
Count the number of elements of a given key in the SetVector.
Definition SetVector.h:262
typename vector_type::const_iterator iterator
Definition SetVector.h:72
void clear()
Completely clear the SetVector.
Definition SetVector.h:267
iterator begin()
Get an iterator to the beginning of the SetVector.
Definition SetVector.h:106
bool insert(const value_type &X)
Insert a new element into the SetVector.
Definition SetVector.h:151
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:339
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.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
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:46
bool isPointerTy() const
True if this is an instance of PointerType.
Definition Type.h:282
bool isVoidTy() const
Return true if this is 'void'.
Definition Type.h:141
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:255
LLVM_ABI void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition Value.cpp:552
LLVMContext & getContext() const
All values hold a context through their type.
Definition Value.h:258
LLVM_ABI const Value * stripPointerCasts() const
Strip off pointer casts, all-zero GEPs and address space casts.
Definition Value.cpp:712
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:318
LLVM_ABI void takeName(Value *V)
Transfer the name from V to this value.
Definition Value.cpp:399
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:123
NodeTy * getNextNode()
Get the next node, or nullptr for the list tail.
Definition ilist_node.h:348
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 isGPUConstantAddressSpace(const Module &M, unsigned AS)
Check if the given address space AS corresponds to a GPU constant address space for the target triple...
LLVM_ABI bool isGPUGenericAddressSpace(const Module &M, unsigned AS)
Check if the given address space AS corresponds to a GPU generic address space for the target triple ...
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 isGPUSharedAddressSpace(const Module &M, unsigned AS)
Check if the given address space AS corresponds to a GPU shared address space for the target triple i...
LLVM_ABI bool isGPULocalAddressSpace(const Module &M, unsigned AS)
Check if the given address space AS corresponds to a GPU local/private address space for the target t...
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:194
@ Intraprocedural
Definition Attributor.h:195
@ Interprocedural
Definition Attributor.h:196
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 isGPUGlobalAddressSpace(const Module &M, unsigned AS)
Check if the given address space AS corresponds to a GPU global address space for the target triple i...
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)
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:804
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:558
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:1738
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:535
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:2152
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:643
@ 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:2614
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:732
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:356
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:676
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:753
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:1745
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:403
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:209
LLVM_ABI bool AreStatisticsEnabled()
Check if statistics are enabled.
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:547
@ 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:2548
LLVM_ABI raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
@ Global
Append to llvm.global_dtors.
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)
LLVM_ABI bool isPotentiallyReachable(const Instruction *From, const Instruction *To, const SmallPtrSetImpl< BasicBlock * > *ExclusionSet=nullptr, const DominatorTree *DT=nullptr, const LoopInfo *LI=nullptr, const CycleInfo *CI=nullptr)
Determine whether instruction 'To' is reachable from 'From', without passing through any blocks in Ex...
Definition CFG.cpp:335
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:559
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:2191
iterator_range< pointer_iterator< WrappedIteratorT > > make_pointer_range(RangeT &&Range)
Definition iterator.h:368
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:1946
ChangeStatus
{
Definition Attributor.h:497
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:509
@ NONE
Do not track a dependence between source and target.
Definition Attributor.h:510
@ REQUIRED
The target cannot be valid if the source is not.
Definition Attributor.h:508
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
APInt operator|(APInt a, const APInt &b)
Definition APInt.h:2172
BumpPtrAllocatorImpl<> BumpPtrAllocator
The standard BumpPtrAllocator which just uses the default template parameters.
Definition Allocator.h:383
AnalysisManager< Module > ModuleAnalysisManager
Convenience typedef for the Module analysis manager.
Definition MIRParser.h:39
#define N
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)
DepSetTy Deps
Set of dependency graph nodes which should be updated if this one is updated.
Definition Attributor.h:524
PointerIntPair< AADepGraphNode *, 1 > DepTy
Definition Attributor.h:518
The data structure for the dependency graph.
Definition Attributor.h:558
iterator begin()
Definition Attributor.h:573
LLVM_ABI void viewGraph()
AADepGraphNode SyntheticRoot
There is no root node for the dependency graph.
Definition Attributor.h:570
LLVM_ABI void print()
Print dependency graph.
iterator end()
Definition Attributor.h:574
LLVM_ABI void dumpGraph()
Dump graph to file.
AADepGraphNode * GetEntryNode()
Definition Attributor.h:571
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 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:253
bool offsetOrSizeAreUnknown() const
Return true if offset or size are unknown.
Definition Attributor.h:262
Value * getValue() const
Definition Attributor.h:206
const Instruction * getCtxI() const
Definition Attributor.h:207
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:594
Function * getAssociatedFunction() const
Return the associated function, if any.
Definition Attributor.h:725
void setAttrList(const AttributeList &AttrList) const
Update the attributes associated with this function or call site scope.
Definition Attributor.h:861
unsigned getAttrIdx() const
Return the index in the attribute list for this position.
Definition Attributor.h:826
bool hasCallBaseContext() const
Check if the position has any call base context.
Definition Attributor.h:943
static const IRPosition callsite_returned(const CallBase &CB)
Create a position describing the returned value of CB.
Definition Attributor.h:662
static const IRPosition returned(const Function &F, const CallBaseContext *CBContext=nullptr)
Create a position describing the returned value of F.
Definition Attributor.h:644
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:618
CallBase CallBaseContext
Definition Attributor.h:597
AttributeList getAttrList() const
Return the attributes associated with this function or call site scope.
Definition Attributor.h:854
static const IRPosition inst(const Instruction &I, const CallBaseContext *CBContext=nullptr)
Create a position describing the instruction I.
Definition Attributor.h:630
static const IRPosition callsite_argument(const CallBase &CB, unsigned ArgNo)
Create a position describing the argument of CB at position ArgNo.
Definition Attributor.h:667
Kind
The positions we distinguish in the IR.
Definition Attributor.h:600
@ IRP_ARGUMENT
An attribute for a function argument.
Definition Attributor.h:608
@ IRP_RETURNED
An attribute for the function return value.
Definition Attributor.h:604
@ IRP_CALL_SITE
An attribute for a call site (function scope).
Definition Attributor.h:607
@ IRP_CALL_SITE_RETURNED
An attribute for a call site return value.
Definition Attributor.h:605
@ IRP_FUNCTION
An attribute for a function (scope).
Definition Attributor.h:606
@ IRP_FLOAT
A position that is not associated with a spot suitable for attributes.
Definition Attributor.h:602
@ IRP_CALL_SITE_ARGUMENT
An attribute for a call site argument.
Definition Attributor.h:609
@ IRP_INVALID
An invalid position.
Definition Attributor.h:601
Instruction * getCtxI() const
Return the context instruction, if any.
Definition Attributor.h:778
static const IRPosition argument(const Argument &Arg, const CallBaseContext *CBContext=nullptr)
Create a position describing the argument Arg.
Definition Attributor.h:651
static LLVM_ABI const IRPosition EmptyKey
Special DenseMap key values.
Definition Attributor.h:948
static const IRPosition function(const Function &F, const CallBaseContext *CBContext=nullptr)
Create a position describing the function scope of F.
Definition Attributor.h:637
const CallBaseContext * getCallBaseContext() const
Get the call base context from the position.
Definition Attributor.h:940
Value & getAssociatedValue() const
Return the value this abstract attribute is associated with.
Definition Attributor.h:792
Value & getAnchorValue() const
Return the value this abstract attribute is anchored with.
Definition Attributor.h:711
Value * getAttrListAnchor() const
Return the value attributes are attached to.
Definition Attributor.h:847
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:821
Kind getPositionKind() const
Return the associated position kind.
Definition Attributor.h:890
static const IRPosition callsite_function(const CallBase &CB)
Create a position describing the function scope of CB.
Definition Attributor.h:657
Function * getAnchorScope() const
Return the Function surrounding the anchor value.
Definition Attributor.h:766
Data structure to hold cached (LLVM-IR) information.
bool IsTargetGPU() const
Return true if the target is a GPU.
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.