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 IRPositions.emplace_back(IRP);
1337
1338 // Helper to determine if operand bundles on a call site are benign or
1339 // potentially problematic. We handle only llvm.assume for now.
1340 auto CanIgnoreOperandBundles = [](const CallBase &CB) {
1341 return (isa<IntrinsicInst>(CB) &&
1342 cast<IntrinsicInst>(CB).getIntrinsicID() == Intrinsic ::assume);
1343 };
1344
1345 const auto *CB = dyn_cast<CallBase>(&IRP.getAnchorValue());
1346 switch (IRP.getPositionKind()) {
1350 return;
1353 IRPositions.emplace_back(IRPosition::function(*IRP.getAnchorScope()));
1354 return;
1356 assert(CB && "Expected call site!");
1357 // TODO: We need to look at the operand bundles similar to the redirection
1358 // in CallBase.
1359 if (!CB->hasOperandBundles() || CanIgnoreOperandBundles(*CB))
1360 if (auto *Callee = dyn_cast_if_present<Function>(CB->getCalledOperand()))
1361 IRPositions.emplace_back(IRPosition::function(*Callee));
1362 return;
1364 assert(CB && "Expected call site!");
1365 // TODO: We need to look at the operand bundles similar to the redirection
1366 // in CallBase.
1367 if (!CB->hasOperandBundles() || CanIgnoreOperandBundles(*CB)) {
1368 if (auto *Callee =
1369 dyn_cast_if_present<Function>(CB->getCalledOperand())) {
1370 IRPositions.emplace_back(IRPosition::returned(*Callee));
1371 IRPositions.emplace_back(IRPosition::function(*Callee));
1372 for (const Argument &Arg : Callee->args())
1373 if (Arg.hasReturnedAttr()) {
1374 IRPositions.emplace_back(
1375 IRPosition::callsite_argument(*CB, Arg.getArgNo()));
1376 IRPositions.emplace_back(
1377 IRPosition::value(*CB->getArgOperand(Arg.getArgNo())));
1378 IRPositions.emplace_back(IRPosition::argument(Arg));
1379 }
1380 }
1381 }
1382 IRPositions.emplace_back(IRPosition::callsite_function(*CB));
1383 return;
1385 assert(CB && "Expected call site!");
1386 // TODO: We need to look at the operand bundles similar to the redirection
1387 // in CallBase.
1388 if (!CB->hasOperandBundles() || CanIgnoreOperandBundles(*CB)) {
1389 auto *Callee = dyn_cast_if_present<Function>(CB->getCalledOperand());
1390 if (Callee) {
1391 if (Argument *Arg = IRP.getAssociatedArgument())
1392 IRPositions.emplace_back(IRPosition::argument(*Arg));
1393 IRPositions.emplace_back(IRPosition::function(*Callee));
1394 }
1395 }
1396 IRPositions.emplace_back(IRPosition::value(IRP.getAssociatedValue()));
1397 return;
1398 }
1399 }
1400}
1401
1402void IRPosition::verify() {
1403#ifdef EXPENSIVE_CHECKS
1404 switch (getPositionKind()) {
1405 case IRP_INVALID:
1406 assert((CBContext == nullptr) &&
1407 "Invalid position must not have CallBaseContext!");
1408 assert(!Enc.getOpaqueValue() &&
1409 "Expected a nullptr for an invalid position!");
1410 return;
1411 case IRP_FLOAT:
1413 "Expected specialized kind for argument values!");
1414 return;
1415 case IRP_RETURNED:
1416 assert(isa<Function>(getAsValuePtr()) &&
1417 "Expected function for a 'returned' position!");
1418 assert(getAsValuePtr() == &getAssociatedValue() &&
1419 "Associated value mismatch!");
1420 return;
1422 assert((CBContext == nullptr) &&
1423 "'call site returned' position must not have CallBaseContext!");
1424 assert((isa<CallBase>(getAsValuePtr())) &&
1425 "Expected call base for 'call site returned' position!");
1426 assert(getAsValuePtr() == &getAssociatedValue() &&
1427 "Associated value mismatch!");
1428 return;
1429 case IRP_CALL_SITE:
1430 assert((CBContext == nullptr) &&
1431 "'call site function' position must not have CallBaseContext!");
1432 assert((isa<CallBase>(getAsValuePtr())) &&
1433 "Expected call base for 'call site function' position!");
1434 assert(getAsValuePtr() == &getAssociatedValue() &&
1435 "Associated value mismatch!");
1436 return;
1437 case IRP_FUNCTION:
1438 assert(isa<Function>(getAsValuePtr()) &&
1439 "Expected function for a 'function' position!");
1440 assert(getAsValuePtr() == &getAssociatedValue() &&
1441 "Associated value mismatch!");
1442 return;
1443 case IRP_ARGUMENT:
1444 assert(isa<Argument>(getAsValuePtr()) &&
1445 "Expected argument for a 'argument' position!");
1446 assert(getAsValuePtr() == &getAssociatedValue() &&
1447 "Associated value mismatch!");
1448 return;
1450 assert((CBContext == nullptr) &&
1451 "'call site argument' position must not have CallBaseContext!");
1452 Use *U = getAsUsePtr();
1453 (void)U; // Silence unused variable warning.
1454 assert(U && "Expected use for a 'call site argument' position!");
1455 assert(isa<CallBase>(U->getUser()) &&
1456 "Expected call base user for a 'call site argument' position!");
1457 assert(cast<CallBase>(U->getUser())->isArgOperand(U) &&
1458 "Expected call base argument operand for a 'call site argument' "
1459 "position");
1460 assert(cast<CallBase>(U->getUser())->getArgOperandNo(U) ==
1461 unsigned(getCallSiteArgNo()) &&
1462 "Argument number mismatch!");
1463 assert(U->get() == &getAssociatedValue() && "Associated value mismatch!");
1464 return;
1465 }
1466 }
1467#endif
1468}
1469
1470std::optional<Constant *>
1472 const AbstractAttribute &AA,
1473 bool &UsedAssumedInformation) {
1474 // First check all callbacks provided by outside AAs. If any of them returns
1475 // a non-null value that is different from the associated value, or
1476 // std::nullopt, we assume it's simplified.
1477 for (auto &CB : SimplificationCallbacks.lookup(IRP)) {
1478 std::optional<Value *> SimplifiedV = CB(IRP, &AA, UsedAssumedInformation);
1479 if (!SimplifiedV)
1480 return std::nullopt;
1481 if (isa_and_nonnull<Constant>(*SimplifiedV))
1482 return cast<Constant>(*SimplifiedV);
1483 return nullptr;
1484 }
1485 if (auto *C = dyn_cast<Constant>(&IRP.getAssociatedValue()))
1486 return C;
1488 if (getAssumedSimplifiedValues(IRP, &AA, Values,
1490 UsedAssumedInformation)) {
1491 if (Values.empty())
1492 return std::nullopt;
1493 if (auto *C = dyn_cast_or_null<Constant>(
1494 AAPotentialValues::getSingleValue(*this, AA, IRP, Values)))
1495 return C;
1496 }
1497 return nullptr;
1498}
1499
1501 const IRPosition &IRP, const AbstractAttribute *AA,
1502 bool &UsedAssumedInformation, AA::ValueScope S) {
1503 // First check all callbacks provided by outside AAs. If any of them returns
1504 // a non-null value that is different from the associated value, or
1505 // std::nullopt, we assume it's simplified.
1506 for (auto &CB : SimplificationCallbacks.lookup(IRP))
1507 return CB(IRP, AA, UsedAssumedInformation);
1508
1510 if (!getAssumedSimplifiedValues(IRP, AA, Values, S, UsedAssumedInformation))
1511 return &IRP.getAssociatedValue();
1512 if (Values.empty())
1513 return std::nullopt;
1514 if (AA)
1515 if (Value *V = AAPotentialValues::getSingleValue(*this, *AA, IRP, Values))
1516 return V;
1519 return nullptr;
1520 return &IRP.getAssociatedValue();
1521}
1522
1524 const IRPosition &InitialIRP, const AbstractAttribute *AA,
1526 bool &UsedAssumedInformation, bool RecurseForSelectAndPHI) {
1529 Worklist.push_back(InitialIRP);
1530 while (!Worklist.empty()) {
1531 const IRPosition &IRP = Worklist.pop_back_val();
1532
1533 // First check all callbacks provided by outside AAs. If any of them returns
1534 // a non-null value that is different from the associated value, or
1535 // std::nullopt, we assume it's simplified.
1536 int NV = Values.size();
1537 const auto &SimplificationCBs = SimplificationCallbacks.lookup(IRP);
1538 for (const auto &CB : SimplificationCBs) {
1539 std::optional<Value *> CBResult = CB(IRP, AA, UsedAssumedInformation);
1540 if (!CBResult.has_value())
1541 continue;
1542 Value *V = *CBResult;
1543 if (!V)
1544 return false;
1547 Values.push_back(AA::ValueAndContext{*V, nullptr});
1548 else
1549 return false;
1550 }
1551 if (SimplificationCBs.empty()) {
1552 // If no high-level/outside simplification occurred, use
1553 // AAPotentialValues.
1554 const auto *PotentialValuesAA =
1556 if (PotentialValuesAA &&
1557 PotentialValuesAA->getAssumedSimplifiedValues(*this, Values, S)) {
1558 UsedAssumedInformation |= !PotentialValuesAA->isAtFixpoint();
1559 } else if (IRP.getPositionKind() != IRPosition::IRP_RETURNED) {
1560 Values.push_back({IRP.getAssociatedValue(), IRP.getCtxI()});
1561 } else {
1562 // TODO: We could visit all returns and add the operands.
1563 return false;
1564 }
1565 }
1566
1567 if (!RecurseForSelectAndPHI)
1568 break;
1569
1570 for (int I = NV, E = Values.size(); I < E; ++I) {
1571 Value *V = Values[I].getValue();
1572 if (!isa<PHINode>(V) && !isa<SelectInst>(V))
1573 continue;
1574 if (!Seen.insert(V).second)
1575 continue;
1576 // Move the last element to this slot.
1577 Values[I] = Values[E - 1];
1578 // Eliminate the last slot, adjust the indices.
1579 Values.pop_back();
1580 --E;
1581 --I;
1582 // Add a new value (select or phi) to the worklist.
1583 Worklist.push_back(IRPosition::value(*V));
1584 }
1585 }
1586 return true;
1587}
1588
1590 std::optional<Value *> V, CallBase &CB, const AbstractAttribute &AA,
1591 bool &UsedAssumedInformation) {
1592 if (!V)
1593 return V;
1594 if (*V == nullptr || isa<Constant>(*V))
1595 return V;
1596 if (auto *Arg = dyn_cast<Argument>(*V))
1597 if (CB.getCalledOperand() == Arg->getParent() &&
1598 CB.arg_size() > Arg->getArgNo())
1599 if (!Arg->hasPointeeInMemoryValueAttr())
1600 return getAssumedSimplified(
1601 IRPosition::callsite_argument(CB, Arg->getArgNo()), AA,
1602 UsedAssumedInformation, AA::Intraprocedural);
1603 return nullptr;
1604}
1605
1607 // The abstract attributes are allocated via the BumpPtrAllocator Allocator,
1608 // thus we cannot delete them. We can, and want to, destruct them though.
1609 for (auto &It : AAMap) {
1610 AbstractAttribute *AA = It.getSecond();
1611 AA->~AbstractAttribute();
1612 }
1613}
1614
1616 const AAIsDead *FnLivenessAA,
1617 bool &UsedAssumedInformation,
1618 bool CheckBBLivenessOnly, DepClassTy DepClass) {
1619 if (!Configuration.UseLiveness)
1620 return false;
1621 const IRPosition &IRP = AA.getIRPosition();
1622 if (!Functions.count(IRP.getAnchorScope()))
1623 return false;
1624 return isAssumedDead(IRP, &AA, FnLivenessAA, UsedAssumedInformation,
1625 CheckBBLivenessOnly, DepClass);
1626}
1627
1629 const AbstractAttribute *QueryingAA,
1630 const AAIsDead *FnLivenessAA,
1631 bool &UsedAssumedInformation,
1632 bool CheckBBLivenessOnly, DepClassTy DepClass) {
1633 if (!Configuration.UseLiveness)
1634 return false;
1635 Instruction *UserI = dyn_cast<Instruction>(U.getUser());
1636 if (!UserI)
1637 return isAssumedDead(IRPosition::value(*U.get()), QueryingAA, FnLivenessAA,
1638 UsedAssumedInformation, CheckBBLivenessOnly, DepClass);
1639
1640 if (auto *CB = dyn_cast<CallBase>(UserI)) {
1641 // For call site argument uses we can check if the argument is
1642 // unused/dead.
1643 if (CB->isArgOperand(&U)) {
1644 const IRPosition &CSArgPos =
1645 IRPosition::callsite_argument(*CB, CB->getArgOperandNo(&U));
1646 return isAssumedDead(CSArgPos, QueryingAA, FnLivenessAA,
1647 UsedAssumedInformation, CheckBBLivenessOnly,
1648 DepClass);
1649 }
1650 } else if (ReturnInst *RI = dyn_cast<ReturnInst>(UserI)) {
1651 const IRPosition &RetPos = IRPosition::returned(*RI->getFunction());
1652 return isAssumedDead(RetPos, QueryingAA, FnLivenessAA,
1653 UsedAssumedInformation, CheckBBLivenessOnly, DepClass);
1654 } else if (PHINode *PHI = dyn_cast<PHINode>(UserI)) {
1655 BasicBlock *IncomingBB = PHI->getIncomingBlock(U);
1656 return isAssumedDead(*IncomingBB->getTerminator(), QueryingAA, FnLivenessAA,
1657 UsedAssumedInformation, CheckBBLivenessOnly, DepClass);
1658 } else if (StoreInst *SI = dyn_cast<StoreInst>(UserI)) {
1659 if (!CheckBBLivenessOnly && SI->getPointerOperand() != U.get()) {
1660 const IRPosition IRP = IRPosition::inst(*SI);
1661 const AAIsDead *IsDeadAA =
1663 if (IsDeadAA && IsDeadAA->isRemovableStore()) {
1664 if (QueryingAA)
1665 recordDependence(*IsDeadAA, *QueryingAA, DepClass);
1666 if (!IsDeadAA->isKnown(AAIsDead::IS_REMOVABLE))
1667 UsedAssumedInformation = true;
1668 return true;
1669 }
1670 }
1671 }
1672
1673 return isAssumedDead(IRPosition::inst(*UserI), QueryingAA, FnLivenessAA,
1674 UsedAssumedInformation, CheckBBLivenessOnly, DepClass);
1675}
1676
1678 const AbstractAttribute *QueryingAA,
1679 const AAIsDead *FnLivenessAA,
1680 bool &UsedAssumedInformation,
1681 bool CheckBBLivenessOnly, DepClassTy DepClass,
1682 bool CheckForDeadStore) {
1683 if (!Configuration.UseLiveness)
1684 return false;
1685 const IRPosition::CallBaseContext *CBCtx =
1686 QueryingAA ? QueryingAA->getCallBaseContext() : nullptr;
1687
1688 if (ManifestAddedBlocks.contains(I.getParent()))
1689 return false;
1690
1691 const Function &F = *I.getFunction();
1692 if (!FnLivenessAA || FnLivenessAA->getAnchorScope() != &F)
1693 FnLivenessAA = getOrCreateAAFor<AAIsDead>(IRPosition::function(F, CBCtx),
1694 QueryingAA, DepClassTy::NONE);
1695
1696 // Don't use recursive reasoning.
1697 if (!FnLivenessAA || QueryingAA == FnLivenessAA)
1698 return false;
1699
1700 // If we have a context instruction and a liveness AA we use it.
1701 if (CheckBBLivenessOnly ? FnLivenessAA->isAssumedDead(I.getParent())
1702 : FnLivenessAA->isAssumedDead(&I)) {
1703 if (QueryingAA)
1704 recordDependence(*FnLivenessAA, *QueryingAA, DepClass);
1705 if (!FnLivenessAA->isKnownDead(&I))
1706 UsedAssumedInformation = true;
1707 return true;
1708 }
1709
1710 if (CheckBBLivenessOnly)
1711 return false;
1712
1713 const IRPosition IRP = IRPosition::inst(I, CBCtx);
1714 const AAIsDead *IsDeadAA =
1716
1717 // Don't use recursive reasoning.
1718 if (!IsDeadAA || QueryingAA == IsDeadAA)
1719 return false;
1720
1721 if (IsDeadAA->isAssumedDead()) {
1722 if (QueryingAA)
1723 recordDependence(*IsDeadAA, *QueryingAA, DepClass);
1724 if (!IsDeadAA->isKnownDead())
1725 UsedAssumedInformation = true;
1726 return true;
1727 }
1728
1729 if (CheckForDeadStore && isa<StoreInst>(I) && IsDeadAA->isRemovableStore()) {
1730 if (QueryingAA)
1731 recordDependence(*IsDeadAA, *QueryingAA, DepClass);
1732 if (!IsDeadAA->isKnownDead())
1733 UsedAssumedInformation = true;
1734 return true;
1735 }
1736
1737 return false;
1738}
1739
1741 const AbstractAttribute *QueryingAA,
1742 const AAIsDead *FnLivenessAA,
1743 bool &UsedAssumedInformation,
1744 bool CheckBBLivenessOnly, DepClassTy DepClass) {
1745 if (!Configuration.UseLiveness)
1746 return false;
1747 // Don't check liveness for constants, e.g. functions, used as (floating)
1748 // values since the context instruction and such is here meaningless.
1751 return false;
1752 }
1753
1754 Instruction *CtxI = IRP.getCtxI();
1755 if (CtxI &&
1756 isAssumedDead(*CtxI, QueryingAA, FnLivenessAA, UsedAssumedInformation,
1757 /* CheckBBLivenessOnly */ true,
1758 CheckBBLivenessOnly ? DepClass : DepClassTy::OPTIONAL))
1759 return true;
1760
1761 if (CheckBBLivenessOnly)
1762 return false;
1763
1764 // If we haven't succeeded we query the specific liveness info for the IRP.
1765 const AAIsDead *IsDeadAA;
1767 IsDeadAA = getOrCreateAAFor<AAIsDead>(
1769 QueryingAA, DepClassTy::NONE);
1770 else
1771 IsDeadAA = getOrCreateAAFor<AAIsDead>(IRP, QueryingAA, DepClassTy::NONE);
1772
1773 // Don't use recursive reasoning.
1774 if (!IsDeadAA || QueryingAA == IsDeadAA)
1775 return false;
1776
1777 if (IsDeadAA->isAssumedDead()) {
1778 if (QueryingAA)
1779 recordDependence(*IsDeadAA, *QueryingAA, DepClass);
1780 if (!IsDeadAA->isKnownDead())
1781 UsedAssumedInformation = true;
1782 return true;
1783 }
1784
1785 return false;
1786}
1787
1789 const AbstractAttribute *QueryingAA,
1790 const AAIsDead *FnLivenessAA,
1791 DepClassTy DepClass) {
1792 if (!Configuration.UseLiveness)
1793 return false;
1794 const Function &F = *BB.getParent();
1795 if (!FnLivenessAA || FnLivenessAA->getAnchorScope() != &F)
1797 QueryingAA, DepClassTy::NONE);
1798
1799 // Don't use recursive reasoning.
1800 if (!FnLivenessAA || QueryingAA == FnLivenessAA)
1801 return false;
1802
1803 if (FnLivenessAA->isAssumedDead(&BB)) {
1804 if (QueryingAA)
1805 recordDependence(*FnLivenessAA, *QueryingAA, DepClass);
1806 return true;
1807 }
1808
1809 return false;
1810}
1811
1814 const AbstractAttribute &QueryingAA, const CallBase &CB) {
1815 if (const Function *Callee = dyn_cast<Function>(CB.getCalledOperand()))
1816 return Pred(Callee);
1817
1818 const auto *CallEdgesAA = getAAFor<AACallEdges>(
1820 if (!CallEdgesAA || CallEdgesAA->hasUnknownCallee())
1821 return false;
1822
1823 const auto &Callees = CallEdgesAA->getOptimisticEdges();
1824 return Pred(Callees.getArrayRef());
1825}
1826
1827bool canMarkAsVisited(const User *Usr) {
1828 return isa<PHINode>(Usr) || !isa<Instruction>(Usr);
1829}
1830
1832 function_ref<bool(const Use &, bool &)> Pred,
1833 const AbstractAttribute &QueryingAA, const Value &V,
1834 bool CheckBBLivenessOnly, DepClassTy LivenessDepClass,
1835 bool IgnoreDroppableUses,
1836 function_ref<bool(const Use &OldU, const Use &NewU)> EquivalentUseCB) {
1837
1838 // Check virtual uses first.
1839 for (VirtualUseCallbackTy &CB : VirtualUseCallbacks.lookup(&V))
1840 if (!CB(*this, &QueryingAA))
1841 return false;
1842
1843 if (isa<ConstantData>(V))
1844 return false;
1845
1846 // Check the trivial case first as it catches void values.
1847 if (V.use_empty())
1848 return true;
1849
1850 const IRPosition &IRP = QueryingAA.getIRPosition();
1853
1854 auto AddUsers = [&](const Value &V, const Use *OldUse) {
1855 for (const Use &UU : V.uses()) {
1856 if (OldUse && EquivalentUseCB && !EquivalentUseCB(*OldUse, UU)) {
1857 LLVM_DEBUG(dbgs() << "[Attributor] Potential copy was "
1858 "rejected by the equivalence call back: "
1859 << *UU << "!\n");
1860 return false;
1861 }
1862
1863 Worklist.push_back(&UU);
1864 }
1865 return true;
1866 };
1867
1868 AddUsers(V, /* OldUse */ nullptr);
1869
1870 LLVM_DEBUG(dbgs() << "[Attributor] Got " << Worklist.size()
1871 << " initial uses to check\n");
1872
1873 const Function *ScopeFn = IRP.getAnchorScope();
1874 const auto *LivenessAA =
1875 ScopeFn ? getAAFor<AAIsDead>(QueryingAA, IRPosition::function(*ScopeFn),
1877 : nullptr;
1878
1879 while (!Worklist.empty()) {
1880 const Use *U = Worklist.pop_back_val();
1881 if (canMarkAsVisited(U->getUser()) && !Visited.insert(U).second)
1882 continue;
1884 if (auto *Fn = dyn_cast<Function>(U->getUser()))
1885 dbgs() << "[Attributor] Check use: " << **U << " in " << Fn->getName()
1886 << "\n";
1887 else
1888 dbgs() << "[Attributor] Check use: " << **U << " in " << *U->getUser()
1889 << "\n";
1890 });
1891 bool UsedAssumedInformation = false;
1892 if (isAssumedDead(*U, &QueryingAA, LivenessAA, UsedAssumedInformation,
1893 CheckBBLivenessOnly, LivenessDepClass)) {
1895 dbgs() << "[Attributor] Dead use, skip!\n");
1896 continue;
1897 }
1898 if (IgnoreDroppableUses && U->getUser()->isDroppable()) {
1900 dbgs() << "[Attributor] Droppable user, skip!\n");
1901 continue;
1902 }
1903
1904 if (auto *SI = dyn_cast<StoreInst>(U->getUser())) {
1905 if (&SI->getOperandUse(0) == U) {
1906 if (!Visited.insert(U).second)
1907 continue;
1908 SmallSetVector<Value *, 4> PotentialCopies;
1910 *this, *SI, PotentialCopies, QueryingAA, UsedAssumedInformation,
1911 /* OnlyExact */ true)) {
1913 dbgs()
1914 << "[Attributor] Value is stored, continue with "
1915 << PotentialCopies.size()
1916 << " potential copies instead!\n");
1917 for (Value *PotentialCopy : PotentialCopies)
1918 if (!AddUsers(*PotentialCopy, U))
1919 return false;
1920 continue;
1921 }
1922 }
1923 }
1924
1925 bool Follow = false;
1926 if (!Pred(*U, Follow))
1927 return false;
1928 if (!Follow)
1929 continue;
1930
1931 User &Usr = *U->getUser();
1932 AddUsers(Usr, /* OldUse */ nullptr);
1933 }
1934
1935 return true;
1936}
1937
1939 const AbstractAttribute &QueryingAA,
1940 bool RequireAllCallSites,
1941 bool &UsedAssumedInformation) {
1942 // We can try to determine information from
1943 // the call sites. However, this is only possible all call sites are known,
1944 // hence the function has internal linkage.
1945 const IRPosition &IRP = QueryingAA.getIRPosition();
1946 const Function *AssociatedFunction = IRP.getAssociatedFunction();
1947 if (!AssociatedFunction) {
1948 LLVM_DEBUG(dbgs() << "[Attributor] No function associated with " << IRP
1949 << "\n");
1950 return false;
1951 }
1952
1953 return checkForAllCallSites(Pred, *AssociatedFunction, RequireAllCallSites,
1954 &QueryingAA, UsedAssumedInformation);
1955}
1956
1958 const Function &Fn,
1959 bool RequireAllCallSites,
1960 const AbstractAttribute *QueryingAA,
1961 bool &UsedAssumedInformation,
1962 bool CheckPotentiallyDead) {
1963 if (RequireAllCallSites && !Fn.hasLocalLinkage()) {
1964 LLVM_DEBUG(
1965 dbgs()
1966 << "[Attributor] Function " << Fn.getName()
1967 << " has no internal linkage, hence not all call sites are known\n");
1968 return false;
1969 }
1970 // Check virtual uses first.
1971 for (VirtualUseCallbackTy &CB : VirtualUseCallbacks.lookup(&Fn))
1972 if (!CB(*this, QueryingAA))
1973 return false;
1974
1976 for (unsigned u = 0; u < Uses.size(); ++u) {
1977 const Use &U = *Uses[u];
1979 if (auto *Fn = dyn_cast<Function>(U))
1980 dbgs() << "[Attributor] Check use: " << Fn->getName() << " in "
1981 << *U.getUser() << "\n";
1982 else
1983 dbgs() << "[Attributor] Check use: " << *U << " in " << *U.getUser()
1984 << "\n";
1985 });
1986 if (!CheckPotentiallyDead &&
1987 isAssumedDead(U, QueryingAA, nullptr, UsedAssumedInformation,
1988 /* CheckBBLivenessOnly */ true)) {
1990 dbgs() << "[Attributor] Dead use, skip!\n");
1991 continue;
1992 }
1993 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(U.getUser())) {
1994 if (CE->isCast() && CE->getType()->isPointerTy()) {
1996 dbgs() << "[Attributor] Use, is constant cast expression, add "
1997 << CE->getNumUses() << " uses of that expression instead!\n";
1998 });
1999 for (const Use &CEU : CE->uses())
2000 Uses.push_back(&CEU);
2001 continue;
2002 }
2003 }
2004
2005 AbstractCallSite ACS(&U);
2006 if (!ACS) {
2007 LLVM_DEBUG(dbgs() << "[Attributor] Function " << Fn.getName()
2008 << " has non call site use " << *U.get() << " in "
2009 << *U.getUser() << "\n");
2010 return false;
2011 }
2012
2013 const Use *EffectiveUse =
2014 ACS.isCallbackCall() ? &ACS.getCalleeUseForCallback() : &U;
2015 if (!ACS.isCallee(EffectiveUse)) {
2016 if (!RequireAllCallSites) {
2017 LLVM_DEBUG(dbgs() << "[Attributor] User " << *EffectiveUse->getUser()
2018 << " is not a call of " << Fn.getName()
2019 << ", skip use\n");
2020 continue;
2021 }
2022 LLVM_DEBUG(dbgs() << "[Attributor] User " << *EffectiveUse->getUser()
2023 << " is an invalid use of " << Fn.getName() << "\n");
2024 return false;
2025 }
2026
2027 // Make sure the arguments that can be matched between the call site and the
2028 // callee argee on their type. It is unlikely they do not and it doesn't
2029 // make sense for all attributes to know/care about this.
2030 assert(&Fn == ACS.getCalledFunction() && "Expected known callee");
2031 unsigned MinArgsParams =
2032 std::min(size_t(ACS.getNumArgOperands()), Fn.arg_size());
2033 for (unsigned u = 0; u < MinArgsParams; ++u) {
2034 Value *CSArgOp = ACS.getCallArgOperand(u);
2035 if (CSArgOp && Fn.getArg(u)->getType() != CSArgOp->getType()) {
2036 LLVM_DEBUG(
2037 dbgs() << "[Attributor] Call site / callee argument type mismatch ["
2038 << u << "@" << Fn.getName() << ": "
2039 << *Fn.getArg(u)->getType() << " vs. "
2040 << *ACS.getCallArgOperand(u)->getType() << "\n");
2041 return false;
2042 }
2043 }
2044
2045 if (Pred(ACS))
2046 continue;
2047
2048 LLVM_DEBUG(dbgs() << "[Attributor] Call site callback failed for "
2049 << *ACS.getInstruction() << "\n");
2050 return false;
2051 }
2052
2053 return true;
2054}
2055
2056bool Attributor::shouldPropagateCallBaseContext(const IRPosition &IRP) {
2057 // TODO: Maintain a cache of Values that are
2058 // on the pathway from a Argument to a Instruction that would effect the
2059 // liveness/return state etc.
2061}
2062
2064 const AbstractAttribute &QueryingAA,
2066 bool RecurseForSelectAndPHI) {
2067
2068 const IRPosition &IRP = QueryingAA.getIRPosition();
2069 const Function *AssociatedFunction = IRP.getAssociatedFunction();
2070 if (!AssociatedFunction)
2071 return false;
2072
2073 bool UsedAssumedInformation = false;
2076 IRPosition::returned(*AssociatedFunction), &QueryingAA, Values, S,
2077 UsedAssumedInformation, RecurseForSelectAndPHI))
2078 return false;
2079
2080 return llvm::all_of(Values, [&](const AA::ValueAndContext &VAC) {
2081 return Pred(*VAC.getValue());
2082 });
2083}
2084
2087 function_ref<bool(Instruction &)> Pred, const AbstractAttribute *QueryingAA,
2088 const AAIsDead *LivenessAA, ArrayRef<unsigned> Opcodes,
2089 bool &UsedAssumedInformation, bool CheckBBLivenessOnly = false,
2090 bool CheckPotentiallyDead = false) {
2091 for (unsigned Opcode : Opcodes) {
2092 // Check if we have instructions with this opcode at all first.
2093 auto *Insts = OpcodeInstMap.lookup(Opcode);
2094 if (!Insts)
2095 continue;
2096
2097 for (Instruction *I : *Insts) {
2098 // Skip dead instructions.
2099 if (A && !CheckPotentiallyDead &&
2100 A->isAssumedDead(IRPosition::inst(*I), QueryingAA, LivenessAA,
2101 UsedAssumedInformation, CheckBBLivenessOnly)) {
2103 dbgs() << "[Attributor] Instruction " << *I
2104 << " is potentially dead, skip!\n";);
2105 continue;
2106 }
2107
2108 if (!Pred(*I))
2109 return false;
2110 }
2111 }
2112 return true;
2113}
2114
2116 const Function *Fn,
2117 const AbstractAttribute *QueryingAA,
2118 ArrayRef<unsigned> Opcodes,
2119 bool &UsedAssumedInformation,
2120 bool CheckBBLivenessOnly,
2121 bool CheckPotentiallyDead) {
2122 // Since we need to provide instructions we have to have an exact definition.
2123 if (!Fn || Fn->isDeclaration())
2124 return false;
2125
2126 const IRPosition &QueryIRP = IRPosition::function(*Fn);
2127 const auto *LivenessAA =
2128 CheckPotentiallyDead && QueryingAA
2129 ? (getAAFor<AAIsDead>(*QueryingAA, QueryIRP, DepClassTy::NONE))
2130 : nullptr;
2131
2132 auto &OpcodeInstMap = InfoCache.getOpcodeInstMapForFunction(*Fn);
2133 if (!checkForAllInstructionsImpl(this, OpcodeInstMap, Pred, QueryingAA,
2134 LivenessAA, Opcodes, UsedAssumedInformation,
2135 CheckBBLivenessOnly, CheckPotentiallyDead))
2136 return false;
2137
2138 return true;
2139}
2140
2142 const AbstractAttribute &QueryingAA,
2143 ArrayRef<unsigned> Opcodes,
2144 bool &UsedAssumedInformation,
2145 bool CheckBBLivenessOnly,
2146 bool CheckPotentiallyDead) {
2147 const IRPosition &IRP = QueryingAA.getIRPosition();
2148 const Function *AssociatedFunction = IRP.getAssociatedFunction();
2149 return checkForAllInstructions(Pred, AssociatedFunction, &QueryingAA, Opcodes,
2150 UsedAssumedInformation, CheckBBLivenessOnly,
2151 CheckPotentiallyDead);
2152}
2153
2155 function_ref<bool(Instruction &)> Pred, AbstractAttribute &QueryingAA,
2156 bool &UsedAssumedInformation) {
2157 TimeTraceScope TS("checkForAllReadWriteInstructions");
2158
2159 const Function *AssociatedFunction =
2160 QueryingAA.getIRPosition().getAssociatedFunction();
2161 if (!AssociatedFunction)
2162 return false;
2163
2164 const IRPosition &QueryIRP = IRPosition::function(*AssociatedFunction);
2165 const auto *LivenessAA =
2166 getAAFor<AAIsDead>(QueryingAA, QueryIRP, DepClassTy::NONE);
2167
2168 for (Instruction *I :
2169 InfoCache.getReadOrWriteInstsForFunction(*AssociatedFunction)) {
2170 // Skip dead instructions.
2171 if (isAssumedDead(IRPosition::inst(*I), &QueryingAA, LivenessAA,
2172 UsedAssumedInformation))
2173 continue;
2174
2175 if (!Pred(*I))
2176 return false;
2177 }
2178
2179 return true;
2180}
2181
2182void Attributor::runTillFixpoint() {
2183 TimeTraceScope TimeScope("Attributor::runTillFixpoint");
2184 LLVM_DEBUG(dbgs() << "[Attributor] Identified and initialized "
2185 << DG.SyntheticRoot.Deps.size()
2186 << " abstract attributes.\n");
2187
2188 // Now that all abstract attributes are collected and initialized we start
2189 // the abstract analysis.
2190
2191 unsigned IterationCounter = 1;
2192 unsigned MaxIterations =
2193 Configuration.MaxFixpointIterations.value_or(SetFixpointIterations);
2194
2196 SetVector<AbstractAttribute *> Worklist, InvalidAAs;
2197 Worklist.insert_range(DG.SyntheticRoot);
2198
2199 do {
2200 // Remember the size to determine new attributes.
2201 size_t NumAAs = DG.SyntheticRoot.Deps.size();
2202 LLVM_DEBUG(dbgs() << "\n\n[Attributor] #Iteration: " << IterationCounter
2203 << ", Worklist size: " << Worklist.size() << "\n");
2204
2205 // For invalid AAs we can fix dependent AAs that have a required dependence,
2206 // thereby folding long dependence chains in a single step without the need
2207 // to run updates.
2208 for (unsigned u = 0; u < InvalidAAs.size(); ++u) {
2209 AbstractAttribute *InvalidAA = InvalidAAs[u];
2210
2211 // Check the dependences to fast track invalidation.
2213 dbgs() << "[Attributor] InvalidAA: " << *InvalidAA
2214 << " has " << InvalidAA->Deps.size()
2215 << " required & optional dependences\n");
2216 for (auto &DepIt : InvalidAA->Deps) {
2217 AbstractAttribute *DepAA = cast<AbstractAttribute>(DepIt.getPointer());
2218 if (DepIt.getInt() == unsigned(DepClassTy::OPTIONAL)) {
2220 dbgs() << " - recompute: " << *DepAA);
2221 Worklist.insert(DepAA);
2222 continue;
2223 }
2225 << " - invalidate: " << *DepAA);
2227 assert(DepAA->getState().isAtFixpoint() && "Expected fixpoint state!");
2228 if (!DepAA->getState().isValidState())
2229 InvalidAAs.insert(DepAA);
2230 else
2231 ChangedAAs.push_back(DepAA);
2232 }
2233 InvalidAA->Deps.clear();
2234 }
2235
2236 // Add all abstract attributes that are potentially dependent on one that
2237 // changed to the work list.
2238 for (AbstractAttribute *ChangedAA : ChangedAAs) {
2239 for (auto &DepIt : ChangedAA->Deps)
2240 Worklist.insert(cast<AbstractAttribute>(DepIt.getPointer()));
2241 ChangedAA->Deps.clear();
2242 }
2243
2244 LLVM_DEBUG(dbgs() << "[Attributor] #Iteration: " << IterationCounter
2245 << ", Worklist+Dependent size: " << Worklist.size()
2246 << "\n");
2247
2248 // Reset the changed and invalid set.
2249 ChangedAAs.clear();
2250 InvalidAAs.clear();
2251
2252 // Update all abstract attribute in the work list and record the ones that
2253 // changed.
2254 for (AbstractAttribute *AA : Worklist) {
2255 const auto &AAState = AA->getState();
2256 if (!AAState.isAtFixpoint())
2257 if (updateAA(*AA) == ChangeStatus::CHANGED)
2258 ChangedAAs.push_back(AA);
2259
2260 // Use the InvalidAAs vector to propagate invalid states fast transitively
2261 // without requiring updates.
2262 if (!AAState.isValidState())
2263 InvalidAAs.insert(AA);
2264 }
2265
2266 // Add attributes to the changed set if they have been created in the last
2267 // iteration.
2268 ChangedAAs.append(DG.SyntheticRoot.begin() + NumAAs,
2269 DG.SyntheticRoot.end());
2270
2271 // Reset the work list and repopulate with the changed abstract attributes.
2272 // Note that dependent ones are added above.
2273 Worklist.clear();
2274 Worklist.insert_range(ChangedAAs);
2275 Worklist.insert_range(QueryAAsAwaitingUpdate);
2276 QueryAAsAwaitingUpdate.clear();
2277
2278 } while (!Worklist.empty() && (IterationCounter++ < MaxIterations));
2279
2280 if (IterationCounter > MaxIterations && !Functions.empty()) {
2281 auto Remark = [&](OptimizationRemarkMissed ORM) {
2282 return ORM << "Attributor did not reach a fixpoint after "
2283 << ore::NV("Iterations", MaxIterations) << " iterations.";
2284 };
2285 Function *F = Functions.front();
2287 }
2288
2289 LLVM_DEBUG(dbgs() << "\n[Attributor] Fixpoint iteration done after: "
2290 << IterationCounter << "/" << MaxIterations
2291 << " iterations\n");
2292
2293 // Reset abstract arguments not settled in a sound fixpoint by now. This
2294 // happens when we stopped the fixpoint iteration early. Note that only the
2295 // ones marked as "changed" *and* the ones transitively depending on them
2296 // need to be reverted to a pessimistic state. Others might not be in a
2297 // fixpoint state but we can use the optimistic results for them anyway.
2298 SmallPtrSet<AbstractAttribute *, 32> Visited;
2299 for (unsigned u = 0; u < ChangedAAs.size(); u++) {
2300 AbstractAttribute *ChangedAA = ChangedAAs[u];
2301 if (!Visited.insert(ChangedAA).second)
2302 continue;
2303
2304 AbstractState &State = ChangedAA->getState();
2305 if (!State.isAtFixpoint()) {
2307
2308 NumAttributesTimedOut++;
2309 }
2310
2311 for (auto &DepIt : ChangedAA->Deps)
2312 ChangedAAs.push_back(cast<AbstractAttribute>(DepIt.getPointer()));
2313 ChangedAA->Deps.clear();
2314 }
2315
2316 LLVM_DEBUG({
2317 if (!Visited.empty())
2318 dbgs() << "\n[Attributor] Finalized " << Visited.size()
2319 << " abstract attributes.\n";
2320 });
2321}
2322
2324 assert(AA.isQueryAA() &&
2325 "Non-query AAs should not be required to register for updates!");
2326 QueryAAsAwaitingUpdate.insert(&AA);
2327}
2328
2329ChangeStatus Attributor::manifestAttributes() {
2330 TimeTraceScope TimeScope("Attributor::manifestAttributes");
2331 size_t NumFinalAAs = DG.SyntheticRoot.Deps.size();
2332
2333 unsigned NumManifested = 0;
2334 unsigned NumAtFixpoint = 0;
2335 ChangeStatus ManifestChange = ChangeStatus::UNCHANGED;
2336 for (auto &DepAA : DG.SyntheticRoot.Deps) {
2337 AbstractAttribute *AA = cast<AbstractAttribute>(DepAA.getPointer());
2338 AbstractState &State = AA->getState();
2339
2340 // If there is not already a fixpoint reached, we can now take the
2341 // optimistic state. This is correct because we enforced a pessimistic one
2342 // on abstract attributes that were transitively dependent on a changed one
2343 // already above.
2344 if (!State.isAtFixpoint())
2345 State.indicateOptimisticFixpoint();
2346
2347 // We must not manifest Attributes that use Callbase info.
2348 if (AA->hasCallBaseContext())
2349 continue;
2350 // If the state is invalid, we do not try to manifest it.
2351 if (!State.isValidState())
2352 continue;
2353
2354 if (AA->getCtxI() && !isRunOn(*AA->getAnchorScope()))
2355 continue;
2356
2357 // Skip dead code.
2358 bool UsedAssumedInformation = false;
2359 if (isAssumedDead(*AA, nullptr, UsedAssumedInformation,
2360 /* CheckBBLivenessOnly */ true))
2361 continue;
2362 // Check if the manifest debug counter that allows skipping manifestation of
2363 // AAs
2364 if (!DebugCounter::shouldExecute(ManifestDBGCounter))
2365 continue;
2366 // Manifest the state and record if we changed the IR.
2367 ChangeStatus LocalChange = AA->manifest(*this);
2368 if (LocalChange == ChangeStatus::CHANGED && AreStatisticsEnabled())
2369 AA->trackStatistics();
2370 LLVM_DEBUG(dbgs() << "[Attributor] Manifest " << LocalChange << " : " << *AA
2371 << "\n");
2372
2373 ManifestChange = ManifestChange | LocalChange;
2374
2375 NumAtFixpoint++;
2376 NumManifested += (LocalChange == ChangeStatus::CHANGED);
2377 }
2378
2379 (void)NumManifested;
2380 (void)NumAtFixpoint;
2381 LLVM_DEBUG(dbgs() << "\n[Attributor] Manifested " << NumManifested
2382 << " arguments while " << NumAtFixpoint
2383 << " were in a valid fixpoint state\n");
2384
2385 NumAttributesManifested += NumManifested;
2386 NumAttributesValidFixpoint += NumAtFixpoint;
2387
2388 (void)NumFinalAAs;
2389 if (NumFinalAAs != DG.SyntheticRoot.Deps.size()) {
2390 auto DepIt = DG.SyntheticRoot.Deps.begin();
2391 for (unsigned u = 0; u < NumFinalAAs; ++u)
2392 ++DepIt;
2393 for (unsigned u = NumFinalAAs; u < DG.SyntheticRoot.Deps.size();
2394 ++u, ++DepIt) {
2395 errs() << "Unexpected abstract attribute: "
2396 << cast<AbstractAttribute>(DepIt->getPointer()) << " :: "
2397 << cast<AbstractAttribute>(DepIt->getPointer())
2398 ->getIRPosition()
2399 .getAssociatedValue()
2400 << "\n";
2401 }
2402 llvm_unreachable("Expected the final number of abstract attributes to "
2403 "remain unchanged!");
2404 }
2405
2406 for (auto &It : AttrsMap) {
2407 AttributeList &AL = It.getSecond();
2408 const IRPosition &IRP =
2409 isa<Function>(It.getFirst())
2410 ? IRPosition::function(*cast<Function>(It.getFirst()))
2411 : IRPosition::callsite_function(*cast<CallBase>(It.getFirst()));
2412 IRP.setAttrList(AL);
2413 }
2414
2415 return ManifestChange;
2416}
2417
2418void Attributor::identifyDeadInternalFunctions() {
2419 // Early exit if we don't intend to delete functions.
2420 if (!Configuration.DeleteFns)
2421 return;
2422
2423 // To avoid triggering an assertion in the lazy call graph we will not delete
2424 // any internal library functions. We should modify the assertion though and
2425 // allow internals to be deleted.
2426 const auto *TLI =
2427 isModulePass()
2428 ? nullptr
2429 : getInfoCache().getTargetLibraryInfoForFunction(*Functions.back());
2430 LibFunc LF;
2431
2432 // Identify dead internal functions and delete them. This happens outside
2433 // the other fixpoint analysis as we might treat potentially dead functions
2434 // as live to lower the number of iterations. If they happen to be dead, the
2435 // below fixpoint loop will identify and eliminate them.
2436
2437 SmallVector<Function *, 8> InternalFns;
2438 for (Function *F : Functions)
2439 if (F->hasLocalLinkage() && (isModulePass() || !TLI->getLibFunc(*F, LF)))
2440 InternalFns.push_back(F);
2441
2442 SmallPtrSet<Function *, 8> LiveInternalFns;
2443 bool FoundLiveInternal = true;
2444 while (FoundLiveInternal) {
2445 FoundLiveInternal = false;
2446 for (Function *&F : InternalFns) {
2447 if (!F)
2448 continue;
2449
2450 bool UsedAssumedInformation = false;
2452 [&](AbstractCallSite ACS) {
2454 return ToBeDeletedFunctions.count(Callee) ||
2455 (Functions.count(Callee) && Callee->hasLocalLinkage() &&
2456 !LiveInternalFns.count(Callee));
2457 },
2458 *F, true, nullptr, UsedAssumedInformation)) {
2459 continue;
2460 }
2461
2462 LiveInternalFns.insert(F);
2463 F = nullptr;
2464 FoundLiveInternal = true;
2465 }
2466 }
2467
2468 for (Function *F : InternalFns)
2469 if (F)
2470 ToBeDeletedFunctions.insert(F);
2471}
2472
2473ChangeStatus Attributor::cleanupIR() {
2474 TimeTraceScope TimeScope("Attributor::cleanupIR");
2475 // Delete stuff at the end to avoid invalid references and a nice order.
2476 LLVM_DEBUG(dbgs() << "\n[Attributor] Delete/replace at least "
2477 << ToBeDeletedFunctions.size() << " functions and "
2478 << ToBeDeletedBlocks.size() << " blocks and "
2479 << ToBeDeletedInsts.size() << " instructions and "
2480 << ToBeChangedValues.size() << " values and "
2481 << ToBeChangedUses.size() << " uses. To insert "
2482 << ToBeChangedToUnreachableInsts.size()
2483 << " unreachables.\n"
2484 << "Preserve manifest added " << ManifestAddedBlocks.size()
2485 << " blocks\n");
2486
2488 SmallVector<Instruction *, 32> TerminatorsToFold;
2489
2490 auto ReplaceUse = [&](Use *U, Value *NewV) {
2491 Value *OldV = U->get();
2492
2493 // If we plan to replace NewV we need to update it at this point.
2494 do {
2495 const auto &Entry = ToBeChangedValues.lookup(NewV);
2496 if (!get<0>(Entry))
2497 break;
2498 NewV = get<0>(Entry);
2499 } while (true);
2500
2501 Instruction *I = dyn_cast<Instruction>(U->getUser());
2502 assert((!I || isRunOn(*I->getFunction())) &&
2503 "Cannot replace an instruction outside the current SCC!");
2504
2505 // Do not replace uses in returns if the value is a must-tail call we will
2506 // not delete.
2507 if (auto *RI = dyn_cast_or_null<ReturnInst>(I)) {
2508 if (auto *CI = dyn_cast<CallInst>(OldV->stripPointerCasts()))
2509 if (CI->isMustTailCall() && !ToBeDeletedInsts.count(CI))
2510 return;
2511 // If we rewrite a return and the new value is not an argument, strip the
2512 // `returned` attribute as it is wrong now.
2513 if (!isa<Argument>(NewV))
2514 for (auto &Arg : RI->getFunction()->args())
2515 Arg.removeAttr(Attribute::Returned);
2516 }
2517
2518 LLVM_DEBUG(dbgs() << "Use " << *NewV << " in " << *U->getUser()
2519 << " instead of " << *OldV << "\n");
2520 U->set(NewV);
2521
2522 if (Instruction *I = dyn_cast<Instruction>(OldV)) {
2523 CGModifiedFunctions.insert(I->getFunction());
2524 if (!isa<PHINode>(I) && !ToBeDeletedInsts.count(I) &&
2526 DeadInsts.push_back(I);
2527 }
2528 if (isa<UndefValue>(NewV) && isa<CallBase>(U->getUser())) {
2529 auto *CB = cast<CallBase>(U->getUser());
2530 if (CB->isArgOperand(U)) {
2531 unsigned Idx = CB->getArgOperandNo(U);
2532 CB->removeParamAttr(Idx, Attribute::NoUndef);
2533 auto *Callee = dyn_cast_if_present<Function>(CB->getCalledOperand());
2534 if (Callee && Callee->arg_size() > Idx)
2535 Callee->removeParamAttr(Idx, Attribute::NoUndef);
2536 }
2537 }
2538 if (isa<Constant>(NewV) && isa<CondBrInst>(U->getUser())) {
2539 Instruction *UserI = cast<Instruction>(U->getUser());
2540 if (isa<UndefValue>(NewV)) {
2541 ToBeChangedToUnreachableInsts.insert(UserI);
2542 } else {
2543 TerminatorsToFold.push_back(UserI);
2544 }
2545 }
2546 };
2547
2548 for (auto &It : ToBeChangedUses) {
2549 Use *U = It.first;
2550 Value *NewV = It.second;
2551 ReplaceUse(U, NewV);
2552 }
2553
2555 for (auto &It : ToBeChangedValues) {
2556 Value *OldV = It.first;
2557 auto [NewV, Done] = It.second;
2558 Uses.clear();
2559 for (auto &U : OldV->uses())
2560 if (Done || !U.getUser()->isDroppable())
2561 Uses.push_back(&U);
2562 for (Use *U : Uses) {
2563 if (auto *I = dyn_cast<Instruction>(U->getUser()))
2564 if (!isRunOn(*I->getFunction()))
2565 continue;
2566 ReplaceUse(U, NewV);
2567 }
2568 }
2569
2570 for (const auto &V : InvokeWithDeadSuccessor)
2571 if (InvokeInst *II = dyn_cast_or_null<InvokeInst>(V)) {
2572 assert(isRunOn(*II->getFunction()) &&
2573 "Cannot replace an invoke outside the current SCC!");
2574 bool UnwindBBIsDead = II->hasFnAttr(Attribute::NoUnwind);
2575 bool NormalBBIsDead = II->hasFnAttr(Attribute::NoReturn);
2576 bool Invoke2CallAllowed =
2578 assert((UnwindBBIsDead || NormalBBIsDead) &&
2579 "Invoke does not have dead successors!");
2580 BasicBlock *BB = II->getParent();
2581 BasicBlock *NormalDestBB = II->getNormalDest();
2582 if (UnwindBBIsDead) {
2583 Instruction *NormalNextIP = &NormalDestBB->front();
2584 if (Invoke2CallAllowed) {
2586 NormalNextIP = BB->getTerminator();
2587 }
2588 if (NormalBBIsDead)
2589 ToBeChangedToUnreachableInsts.insert(NormalNextIP);
2590 } else {
2591 assert(NormalBBIsDead && "Broken invariant!");
2592 if (!NormalDestBB->getUniquePredecessor())
2593 NormalDestBB = SplitBlockPredecessors(NormalDestBB, {BB}, ".dead");
2594 ToBeChangedToUnreachableInsts.insert(&NormalDestBB->front());
2595 }
2596 }
2597 for (Instruction *I : TerminatorsToFold) {
2598 assert(isRunOn(*I->getFunction()) &&
2599 "Cannot replace a terminator outside the current SCC!");
2600 CGModifiedFunctions.insert(I->getFunction());
2601 ConstantFoldTerminator(I->getParent());
2602 }
2603 for (const auto &V : ToBeChangedToUnreachableInsts)
2604 if (Instruction *I = dyn_cast_or_null<Instruction>(V)) {
2605 LLVM_DEBUG(dbgs() << "[Attributor] Change to unreachable: " << *I
2606 << "\n");
2607 assert(isRunOn(*I->getFunction()) &&
2608 "Cannot replace an instruction outside the current SCC!");
2609 CGModifiedFunctions.insert(I->getFunction());
2611 }
2612
2613 for (const auto &V : ToBeDeletedInsts) {
2614 if (Instruction *I = dyn_cast_or_null<Instruction>(V)) {
2616 isRunOn(*I->getFunction())) &&
2617 "Cannot delete an instruction outside the current SCC!");
2618 I->dropDroppableUses();
2619 CGModifiedFunctions.insert(I->getFunction());
2620 if (!I->getType()->isVoidTy())
2621 I->replaceAllUsesWith(UndefValue::get(I->getType()));
2623 DeadInsts.push_back(I);
2624 else
2625 I->eraseFromParent();
2626 }
2627 }
2628
2629 llvm::erase_if(DeadInsts, [&](WeakTrackingVH I) { return !I; });
2630
2631 LLVM_DEBUG({
2632 dbgs() << "[Attributor] DeadInsts size: " << DeadInsts.size() << "\n";
2633 for (auto &I : DeadInsts)
2634 if (I)
2635 dbgs() << " - " << *I << "\n";
2636 });
2637
2639
2640 if (unsigned NumDeadBlocks = ToBeDeletedBlocks.size()) {
2641 SmallVector<BasicBlock *, 8> ToBeDeletedBBs;
2642 ToBeDeletedBBs.reserve(NumDeadBlocks);
2643 for (BasicBlock *BB : ToBeDeletedBlocks) {
2644 assert(isRunOn(*BB->getParent()) &&
2645 "Cannot delete a block outside the current SCC!");
2646 CGModifiedFunctions.insert(BB->getParent());
2647 // Do not delete BBs added during manifests of AAs.
2648 if (ManifestAddedBlocks.contains(BB))
2649 continue;
2650 ToBeDeletedBBs.push_back(BB);
2651 }
2652 // Actually we do not delete the blocks but squash them into a single
2653 // unreachable but untangling branches that jump here is something we need
2654 // to do in a more generic way.
2655 detachDeadBlocks(ToBeDeletedBBs, nullptr);
2656 }
2657
2658 identifyDeadInternalFunctions();
2659
2660 // Rewrite the functions as requested during manifest.
2661 ChangeStatus ManifestChange = rewriteFunctionSignatures(CGModifiedFunctions);
2662
2663 for (Function *Fn : CGModifiedFunctions)
2664 if (!ToBeDeletedFunctions.count(Fn) && Functions.count(Fn))
2665 Configuration.CGUpdater.reanalyzeFunction(*Fn);
2666
2667 for (Function *Fn : ToBeDeletedFunctions) {
2668 if (!Functions.count(Fn))
2669 continue;
2670 Configuration.CGUpdater.removeFunction(*Fn);
2671 }
2672
2673 if (!ToBeChangedUses.empty())
2674 ManifestChange = ChangeStatus::CHANGED;
2675
2676 if (!ToBeChangedToUnreachableInsts.empty())
2677 ManifestChange = ChangeStatus::CHANGED;
2678
2679 if (!ToBeDeletedFunctions.empty())
2680 ManifestChange = ChangeStatus::CHANGED;
2681
2682 if (!ToBeDeletedBlocks.empty())
2683 ManifestChange = ChangeStatus::CHANGED;
2684
2685 if (!ToBeDeletedInsts.empty())
2686 ManifestChange = ChangeStatus::CHANGED;
2687
2688 if (!InvokeWithDeadSuccessor.empty())
2689 ManifestChange = ChangeStatus::CHANGED;
2690
2691 if (!DeadInsts.empty())
2692 ManifestChange = ChangeStatus::CHANGED;
2693
2694 NumFnDeleted += ToBeDeletedFunctions.size();
2695
2696 LLVM_DEBUG(dbgs() << "[Attributor] Deleted " << ToBeDeletedFunctions.size()
2697 << " functions after manifest.\n");
2698
2699#ifdef EXPENSIVE_CHECKS
2700 for (Function *F : Functions) {
2701 if (ToBeDeletedFunctions.count(F))
2702 continue;
2703 assert(!verifyFunction(*F, &errs()) && "Module verification failed!");
2704 }
2705#endif
2706
2707 return ManifestChange;
2708}
2709
2711 TimeTraceScope TimeScope("Attributor::run");
2712 AttributorCallGraph ACallGraph(*this);
2713
2714 if (PrintCallGraph)
2715 ACallGraph.populateAll();
2716
2717 Phase = AttributorPhase::UPDATE;
2718 runTillFixpoint();
2719
2720 // dump graphs on demand
2721 if (DumpDepGraph)
2722 DG.dumpGraph();
2723
2724 if (ViewDepGraph)
2725 DG.viewGraph();
2726
2728 DG.print();
2729
2730 Phase = AttributorPhase::MANIFEST;
2731 ChangeStatus ManifestChange = manifestAttributes();
2732
2733 Phase = AttributorPhase::CLEANUP;
2734 ChangeStatus CleanupChange = cleanupIR();
2735
2736 if (PrintCallGraph)
2737 ACallGraph.print();
2738
2739 return ManifestChange | CleanupChange;
2740}
2741
2742ChangeStatus Attributor::updateAA(AbstractAttribute &AA) {
2743 TimeTraceScope TimeScope("updateAA", [&]() {
2744 return AA.getName().str() +
2745 std::to_string(AA.getIRPosition().getPositionKind());
2746 });
2747 assert(Phase == AttributorPhase::UPDATE &&
2748 "We can update AA only in the update stage!");
2749
2750 // Use a new dependence vector for this update.
2751 DependenceVector DV;
2752 DependenceStack.push_back(&DV);
2753
2754 auto &AAState = AA.getState();
2756 bool UsedAssumedInformation = false;
2757 if (!isAssumedDead(AA, nullptr, UsedAssumedInformation,
2758 /* CheckBBLivenessOnly */ true))
2759 CS = AA.update(*this);
2760
2761 if (!AA.isQueryAA() && DV.empty() && !AA.getState().isAtFixpoint()) {
2762 // If the AA did not rely on outside information but changed, we run it
2763 // again to see if it found a fixpoint. Most AAs do but we don't require
2764 // them to. Hence, it might take the AA multiple iterations to get to a
2765 // fixpoint even if it does not rely on outside information, which is fine.
2767 if (CS == ChangeStatus::CHANGED)
2768 RerunCS = AA.update(*this);
2769
2770 // If the attribute did not change during the run or rerun, and it still did
2771 // not query any non-fix information, the state will not change and we can
2772 // indicate that right at this point.
2773 if (RerunCS == ChangeStatus::UNCHANGED && !AA.isQueryAA() && DV.empty())
2774 AAState.indicateOptimisticFixpoint();
2775 }
2776
2777 if (!AAState.isAtFixpoint())
2778 rememberDependences();
2779
2780 // Verify the stack was used properly, that is we pop the dependence vector we
2781 // put there earlier.
2782 DependenceVector *PoppedDV = DependenceStack.pop_back_val();
2783 (void)PoppedDV;
2784 assert(PoppedDV == &DV && "Inconsistent usage of the dependence stack!");
2785
2786 return CS;
2787}
2788
2790 assert(!F.isDeclaration() && "Cannot create a wrapper around a declaration!");
2791
2792 Module &M = *F.getParent();
2793 LLVMContext &Ctx = M.getContext();
2794 FunctionType *FnTy = F.getFunctionType();
2795
2796 Function *Wrapper =
2797 Function::Create(FnTy, F.getLinkage(), F.getAddressSpace(), F.getName());
2798 F.setName(""); // set the inside function anonymous
2799 M.getFunctionList().insert(F.getIterator(), Wrapper);
2800
2801 F.setLinkage(GlobalValue::InternalLinkage);
2802
2803 F.replaceAllUsesWith(Wrapper);
2804 assert(F.use_empty() && "Uses remained after wrapper was created!");
2805
2806 // Move the COMDAT section to the wrapper.
2807 // TODO: Check if we need to keep it for F as well.
2808 Wrapper->setComdat(F.getComdat());
2809 F.setComdat(nullptr);
2810
2811 // Copy all metadata and attributes but keep them on F as well.
2813 F.getAllMetadata(MDs);
2814 for (auto MDIt : MDs)
2815 Wrapper->addMetadata(MDIt.first, *MDIt.second);
2816 Wrapper->setAttributes(F.getAttributes());
2817
2818 // Create the call in the wrapper.
2819 BasicBlock *EntryBB = BasicBlock::Create(Ctx, "entry", Wrapper);
2820
2822 Argument *FArgIt = F.arg_begin();
2823 for (Argument &Arg : Wrapper->args()) {
2824 Args.push_back(&Arg);
2825 Arg.setName((FArgIt++)->getName());
2826 }
2827
2828 CallInst *CI = CallInst::Create(&F, Args, "", EntryBB);
2829 CI->setTailCall(true);
2830 CI->addFnAttr(Attribute::NoInline);
2831 ReturnInst::Create(Ctx, CI->getType()->isVoidTy() ? nullptr : CI, EntryBB);
2832
2833 NumFnShallowWrappersCreated++;
2834}
2835
2837 if (F.isDeclaration() || F.hasLocalLinkage() ||
2839 return false;
2840 return true;
2841}
2842
2844 if (!AllowDeepWrapper && !Force)
2845 return nullptr;
2846 if (!isInternalizable(F))
2847 return nullptr;
2848
2849 SmallPtrSet<Function *, 2> FnSet = {&F};
2850 DenseMap<Function *, Function *> InternalizedFns;
2851 internalizeFunctions(FnSet, InternalizedFns);
2852
2853 return InternalizedFns[&F];
2854}
2855
2858 for (Function *F : FnSet)
2860 return false;
2861
2862 FnMap.clear();
2863 // Generate the internalized version of each function.
2864 for (Function *F : FnSet) {
2865 Module &M = *F->getParent();
2866 FunctionType *FnTy = F->getFunctionType();
2867
2868 // Create a copy of the current function
2869 Function *Copied =
2870 Function::Create(FnTy, F->getLinkage(), F->getAddressSpace(),
2871 F->getName() + ".internalized");
2872 ValueToValueMapTy VMap;
2873 auto *NewFArgIt = Copied->arg_begin();
2874 for (auto &Arg : F->args()) {
2875 auto ArgName = Arg.getName();
2876 NewFArgIt->setName(ArgName);
2877 VMap[&Arg] = &(*NewFArgIt++);
2878 }
2880
2881 // Copy the body of the original function to the new one
2882 CloneFunctionInto(Copied, F, VMap,
2884
2885 // Set the linakage and visibility late as CloneFunctionInto has some
2886 // implicit requirements.
2889
2890 // Copy metadata
2892 F->getAllMetadata(MDs);
2893 for (auto MDIt : MDs)
2894 if (!Copied->hasMetadata())
2895 Copied->addMetadata(MDIt.first, *MDIt.second);
2896
2897 M.getFunctionList().insert(F->getIterator(), Copied);
2898 Copied->setDSOLocal(true);
2899 FnMap[F] = Copied;
2900 }
2901
2902 // Replace all uses of the old function with the new internalized function
2903 // unless the caller is a function that was just internalized.
2904 for (Function *F : FnSet) {
2905 auto &InternalizedFn = FnMap[F];
2906 auto IsNotInternalized = [&](Use &U) -> bool {
2907 if (auto *CB = dyn_cast<CallBase>(U.getUser()))
2908 return !FnMap.lookup(CB->getCaller());
2909 return false;
2910 };
2911 F->replaceUsesWithIf(InternalizedFn, IsNotInternalized);
2912 }
2913
2914 return true;
2915}
2916
2918 Argument &Arg, ArrayRef<Type *> ReplacementTypes) {
2919
2920 if (!Configuration.RewriteSignatures)
2921 return false;
2922
2923 Function *Fn = Arg.getParent();
2924 auto CallSiteCanBeChanged = [Fn](AbstractCallSite ACS) {
2925 // Forbid the call site to cast the function return type. If we need to
2926 // rewrite these functions we need to re-create a cast for the new call site
2927 // (if the old had uses).
2928 if (!ACS.getCalledFunction() ||
2929 ACS.getInstruction()->getType() !=
2931 return false;
2932 if (cast<CallBase>(ACS.getInstruction())->getCalledOperand()->getType() !=
2933 Fn->getType())
2934 return false;
2935 if (ACS.getNumArgOperands() != Fn->arg_size())
2936 return false;
2937 // Forbid must-tail calls for now.
2938 return !ACS.isCallbackCall() && !ACS.getInstruction()->isMustTailCall();
2939 };
2940
2941 // Avoid var-arg functions for now.
2942 if (Fn->isVarArg()) {
2943 LLVM_DEBUG(dbgs() << "[Attributor] Cannot rewrite var-args functions\n");
2944 return false;
2945 }
2946
2947 // Avoid functions with complicated argument passing semantics.
2948 AttributeList FnAttributeList = Fn->getAttributes();
2949 if (FnAttributeList.hasAttrSomewhere(Attribute::Nest) ||
2950 FnAttributeList.hasAttrSomewhere(Attribute::StructRet) ||
2951 FnAttributeList.hasAttrSomewhere(Attribute::InAlloca) ||
2952 FnAttributeList.hasAttrSomewhere(Attribute::Preallocated)) {
2953 LLVM_DEBUG(
2954 dbgs() << "[Attributor] Cannot rewrite due to complex attribute\n");
2955 return false;
2956 }
2957
2958 // Avoid callbacks for now.
2959 bool UsedAssumedInformation = false;
2960 if (!checkForAllCallSites(CallSiteCanBeChanged, *Fn, true, nullptr,
2961 UsedAssumedInformation,
2962 /* CheckPotentiallyDead */ true)) {
2963 LLVM_DEBUG(dbgs() << "[Attributor] Cannot rewrite all call sites\n");
2964 return false;
2965 }
2966
2967 auto InstPred = [](Instruction &I) {
2968 if (auto *CI = dyn_cast<CallInst>(&I))
2969 return !CI->isMustTailCall();
2970 return true;
2971 };
2972
2973 // Forbid must-tail calls for now.
2974 // TODO:
2975 auto &OpcodeInstMap = InfoCache.getOpcodeInstMapForFunction(*Fn);
2976 if (!checkForAllInstructionsImpl(nullptr, OpcodeInstMap, InstPred, nullptr,
2977 nullptr, {Instruction::Call},
2978 UsedAssumedInformation)) {
2979 LLVM_DEBUG(dbgs() << "[Attributor] Cannot rewrite due to instructions\n");
2980 return false;
2981 }
2982
2983 return true;
2984}
2985
2987 Argument &Arg, ArrayRef<Type *> ReplacementTypes,
2990 LLVM_DEBUG(dbgs() << "[Attributor] Register new rewrite of " << Arg << " in "
2991 << Arg.getParent()->getName() << " with "
2992 << ReplacementTypes.size() << " replacements\n");
2993 assert(isValidFunctionSignatureRewrite(Arg, ReplacementTypes) &&
2994 "Cannot register an invalid rewrite");
2995
2996 Function *Fn = Arg.getParent();
2998 ArgumentReplacementMap[Fn];
2999 if (ARIs.empty())
3000 ARIs.resize(Fn->arg_size());
3001
3002 // If we have a replacement already with less than or equal new arguments,
3003 // ignore this request.
3004 std::unique_ptr<ArgumentReplacementInfo> &ARI = ARIs[Arg.getArgNo()];
3005 if (ARI && ARI->getNumReplacementArgs() <= ReplacementTypes.size()) {
3006 LLVM_DEBUG(dbgs() << "[Attributor] Existing rewrite is preferred\n");
3007 return false;
3008 }
3009
3010 // If we have a replacement already but we like the new one better, delete
3011 // the old.
3012 ARI.reset();
3013
3014 LLVM_DEBUG(dbgs() << "[Attributor] Register new rewrite of " << Arg << " in "
3015 << Arg.getParent()->getName() << " with "
3016 << ReplacementTypes.size() << " replacements\n");
3017
3018 // Remember the replacement.
3019 ARI.reset(new ArgumentReplacementInfo(*this, Arg, ReplacementTypes,
3020 std::move(CalleeRepairCB),
3021 std::move(ACSRepairCB)));
3022
3023 return true;
3024}
3025
3026bool Attributor::shouldSeedAttribute(AbstractAttribute &AA) {
3027 bool Result = true;
3028#ifndef NDEBUG
3029 if (SeedAllowList.size() != 0)
3030 Result = llvm::is_contained(SeedAllowList, AA.getName());
3031 Function *Fn = AA.getAnchorScope();
3032 if (FunctionSeedAllowList.size() != 0 && Fn)
3034#endif
3035 return Result;
3036}
3037
3038ChangeStatus Attributor::rewriteFunctionSignatures(
3039 SmallSetVector<Function *, 8> &ModifiedFns) {
3041
3042 for (auto &It : ArgumentReplacementMap) {
3043 Function *OldFn = It.getFirst();
3044
3045 // Deleted functions do not require rewrites.
3046 if (!Functions.count(OldFn) || ToBeDeletedFunctions.count(OldFn))
3047 continue;
3048
3050 It.getSecond();
3051 assert(ARIs.size() == OldFn->arg_size() && "Inconsistent state!");
3052
3053 SmallVector<Type *, 16> NewArgumentTypes;
3054 SmallVector<AttributeSet, 16> NewArgumentAttributes;
3055
3056 // Collect replacement argument types and copy over existing attributes.
3057 AttributeList OldFnAttributeList = OldFn->getAttributes();
3058 for (Argument &Arg : OldFn->args()) {
3059 if (const std::unique_ptr<ArgumentReplacementInfo> &ARI =
3060 ARIs[Arg.getArgNo()]) {
3061 NewArgumentTypes.append(ARI->ReplacementTypes.begin(),
3062 ARI->ReplacementTypes.end());
3063 NewArgumentAttributes.append(ARI->getNumReplacementArgs(),
3064 AttributeSet());
3065 } else {
3066 NewArgumentTypes.push_back(Arg.getType());
3067 NewArgumentAttributes.push_back(
3068 OldFnAttributeList.getParamAttrs(Arg.getArgNo()));
3069 }
3070 }
3071
3072 uint64_t LargestVectorWidth = 0;
3073 for (auto *I : NewArgumentTypes)
3074 if (auto *VT = dyn_cast<llvm::VectorType>(I))
3075 LargestVectorWidth =
3076 std::max(LargestVectorWidth,
3077 VT->getPrimitiveSizeInBits().getKnownMinValue());
3078
3079 FunctionType *OldFnTy = OldFn->getFunctionType();
3080 Type *RetTy = OldFnTy->getReturnType();
3081
3082 // Construct the new function type using the new arguments types.
3083 FunctionType *NewFnTy =
3084 FunctionType::get(RetTy, NewArgumentTypes, OldFnTy->isVarArg());
3085
3086 LLVM_DEBUG(dbgs() << "[Attributor] Function rewrite '" << OldFn->getName()
3087 << "' from " << *OldFn->getFunctionType() << " to "
3088 << *NewFnTy << "\n");
3089
3090 // Create the new function body and insert it into the module.
3091 Function *NewFn = Function::Create(NewFnTy, OldFn->getLinkage(),
3092 OldFn->getAddressSpace(), "");
3093 Functions.insert(NewFn);
3094 OldFn->getParent()->getFunctionList().insert(OldFn->getIterator(), NewFn);
3095 NewFn->takeName(OldFn);
3096 NewFn->copyAttributesFrom(OldFn);
3097
3098 // Patch the pointer to LLVM function in debug info descriptor.
3099 NewFn->setSubprogram(OldFn->getSubprogram());
3100 OldFn->setSubprogram(nullptr);
3101
3102 // Recompute the parameter attributes list based on the new arguments for
3103 // the function.
3104 LLVMContext &Ctx = OldFn->getContext();
3105 NewFn->setAttributes(AttributeList::get(
3106 Ctx, OldFnAttributeList.getFnAttrs(), OldFnAttributeList.getRetAttrs(),
3107 NewArgumentAttributes));
3108 AttributeFuncs::updateMinLegalVectorWidthAttr(*NewFn, LargestVectorWidth);
3109
3110 // Remove argmem from the memory effects if we have no more pointer
3111 // arguments, or they are readnone.
3112 MemoryEffects ME = NewFn->getMemoryEffects();
3113 int ArgNo = -1;
3114 if (ME.doesAccessArgPointees() && all_of(NewArgumentTypes, [&](Type *T) {
3115 ++ArgNo;
3116 return !T->isPtrOrPtrVectorTy() ||
3117 NewFn->hasParamAttribute(ArgNo, Attribute::ReadNone);
3118 })) {
3120 }
3121
3122 // Since we have now created the new function, splice the body of the old
3123 // function right into the new function, leaving the old rotting hulk of the
3124 // function empty.
3125 NewFn->splice(NewFn->begin(), OldFn);
3126
3127 // Set of all "call-like" instructions that invoke the old function mapped
3128 // to their new replacements.
3130
3131 // Callback to create a new "call-like" instruction for a given one.
3132 auto CallSiteReplacementCreator = [&](AbstractCallSite ACS) {
3133 CallBase *OldCB = cast<CallBase>(ACS.getInstruction());
3134 const AttributeList &OldCallAttributeList = OldCB->getAttributes();
3135
3136 // Collect the new argument operands for the replacement call site.
3137 SmallVector<Value *, 16> NewArgOperands;
3138 SmallVector<AttributeSet, 16> NewArgOperandAttributes;
3139 for (unsigned OldArgNum = 0; OldArgNum < ARIs.size(); ++OldArgNum) {
3140 unsigned NewFirstArgNum = NewArgOperands.size();
3141 (void)NewFirstArgNum; // only used inside assert.
3142 if (const std::unique_ptr<ArgumentReplacementInfo> &ARI =
3143 ARIs[OldArgNum]) {
3144 if (ARI->ACSRepairCB)
3145 ARI->ACSRepairCB(*ARI, ACS, NewArgOperands);
3146 assert(ARI->getNumReplacementArgs() + NewFirstArgNum ==
3147 NewArgOperands.size() &&
3148 "ACS repair callback did not provide as many operand as new "
3149 "types were registered!");
3150 // TODO: Exose the attribute set to the ACS repair callback
3151 NewArgOperandAttributes.append(ARI->ReplacementTypes.size(),
3152 AttributeSet());
3153 } else {
3154 NewArgOperands.push_back(ACS.getCallArgOperand(OldArgNum));
3155 NewArgOperandAttributes.push_back(
3156 OldCallAttributeList.getParamAttrs(OldArgNum));
3157 }
3158 }
3159
3160 assert(NewArgOperands.size() == NewArgOperandAttributes.size() &&
3161 "Mismatch # argument operands vs. # argument operand attributes!");
3162 assert(NewArgOperands.size() == NewFn->arg_size() &&
3163 "Mismatch # argument operands vs. # function arguments!");
3164
3165 SmallVector<OperandBundleDef, 4> OperandBundleDefs;
3166 OldCB->getOperandBundlesAsDefs(OperandBundleDefs);
3167
3168 // Create a new call or invoke instruction to replace the old one.
3169 CallBase *NewCB;
3170 if (InvokeInst *II = dyn_cast<InvokeInst>(OldCB)) {
3171 NewCB = InvokeInst::Create(NewFn, II->getNormalDest(),
3172 II->getUnwindDest(), NewArgOperands,
3173 OperandBundleDefs, "", OldCB->getIterator());
3174 } else {
3175 auto *NewCI = CallInst::Create(NewFn, NewArgOperands, OperandBundleDefs,
3176 "", OldCB->getIterator());
3177 NewCI->setTailCallKind(cast<CallInst>(OldCB)->getTailCallKind());
3178 NewCB = NewCI;
3179 }
3180
3181 // Copy over various properties and the new attributes.
3182 NewCB->copyMetadata(*OldCB, {LLVMContext::MD_prof, LLVMContext::MD_dbg});
3183 NewCB->setCallingConv(OldCB->getCallingConv());
3184 NewCB->takeName(OldCB);
3185 NewCB->setAttributes(AttributeList::get(
3186 Ctx, OldCallAttributeList.getFnAttrs(),
3187 OldCallAttributeList.getRetAttrs(), NewArgOperandAttributes));
3188
3189 AttributeFuncs::updateMinLegalVectorWidthAttr(*NewCB->getCaller(),
3190 LargestVectorWidth);
3191
3192 CallSitePairs.push_back({OldCB, NewCB});
3193 return true;
3194 };
3195
3196 // Use the CallSiteReplacementCreator to create replacement call sites.
3197 bool UsedAssumedInformation = false;
3198 bool Success = checkForAllCallSites(CallSiteReplacementCreator, *OldFn,
3199 true, nullptr, UsedAssumedInformation,
3200 /* CheckPotentiallyDead */ true);
3201 (void)Success;
3202 assert(Success && "Assumed call site replacement to succeed!");
3203
3204 // Rewire the arguments.
3205 Argument *OldFnArgIt = OldFn->arg_begin();
3206 Argument *NewFnArgIt = NewFn->arg_begin();
3207 for (unsigned OldArgNum = 0; OldArgNum < ARIs.size();
3208 ++OldArgNum, ++OldFnArgIt) {
3209 if (const std::unique_ptr<ArgumentReplacementInfo> &ARI =
3210 ARIs[OldArgNum]) {
3211 if (ARI->CalleeRepairCB)
3212 ARI->CalleeRepairCB(*ARI, *NewFn, NewFnArgIt);
3213 if (ARI->ReplacementTypes.empty())
3214 OldFnArgIt->replaceAllUsesWith(
3215 PoisonValue::get(OldFnArgIt->getType()));
3216 NewFnArgIt += ARI->ReplacementTypes.size();
3217 } else {
3218 NewFnArgIt->takeName(&*OldFnArgIt);
3219 OldFnArgIt->replaceAllUsesWith(&*NewFnArgIt);
3220 ++NewFnArgIt;
3221 }
3222 }
3223
3224 // Eliminate the instructions *after* we visited all of them.
3225 for (auto &CallSitePair : CallSitePairs) {
3226 CallBase &OldCB = *CallSitePair.first;
3227 CallBase &NewCB = *CallSitePair.second;
3228 assert(OldCB.getType() == NewCB.getType() &&
3229 "Cannot handle call sites with different types!");
3230 ModifiedFns.insert(OldCB.getFunction());
3231 OldCB.replaceAllUsesWith(&NewCB);
3232 OldCB.eraseFromParent();
3233 }
3234
3235 // Replace the function in the call graph (if any).
3236 Configuration.CGUpdater.replaceFunctionWith(*OldFn, *NewFn);
3237
3238 // If the old function was modified and needed to be reanalyzed, the new one
3239 // does now.
3240 if (ModifiedFns.remove(OldFn))
3241 ModifiedFns.insert(NewFn);
3242
3244 }
3245
3246 return Changed;
3247}
3248
3249void InformationCache::initializeInformationCache(const Function &CF,
3250 FunctionInfo &FI) {
3251 // As we do not modify the function here we can remove the const
3252 // withouth breaking implicit assumptions. At the end of the day, we could
3253 // initialize the cache eagerly which would look the same to the users.
3254 Function &F = const_cast<Function &>(CF);
3255
3256 FI.IsKernel = F.hasFnAttribute("kernel");
3257
3258 // Walk all instructions to find interesting instructions that might be
3259 // queried by abstract attributes during their initialization or update.
3260 // This has to happen before we create attributes.
3261
3262 DenseMap<const Value *, std::optional<short>> AssumeUsesMap;
3263
3264 // Add \p V to the assume uses map which track the number of uses outside of
3265 // "visited" assumes. If no outside uses are left the value is added to the
3266 // assume only use vector.
3267 auto AddToAssumeUsesMap = [&](const Value &V) -> void {
3268 SmallVector<const Instruction *> Worklist;
3269 if (auto *I = dyn_cast<Instruction>(&V))
3270 Worklist.push_back(I);
3271 while (!Worklist.empty()) {
3272 const Instruction *I = Worklist.pop_back_val();
3273 std::optional<short> &NumUses = AssumeUsesMap[I];
3274 if (!NumUses)
3275 NumUses = I->getNumUses();
3276 NumUses = *NumUses - /* this assume */ 1;
3277 if (*NumUses != 0)
3278 continue;
3279 AssumeOnlyValues.insert(I);
3280 for (const Value *Op : I->operands())
3281 if (auto *OpI = dyn_cast<Instruction>(Op))
3282 Worklist.push_back(OpI);
3283 }
3284 };
3285
3286 for (Instruction &I : instructions(&F)) {
3287 bool IsInterestingOpcode = false;
3288
3289 // To allow easy access to all instructions in a function with a given
3290 // opcode we store them in the InfoCache. As not all opcodes are interesting
3291 // to concrete attributes we only cache the ones that are as identified in
3292 // the following switch.
3293 // Note: There are no concrete attributes now so this is initially empty.
3294 switch (I.getOpcode()) {
3295 default:
3296 assert(!isa<CallBase>(&I) &&
3297 "New call base instruction type needs to be known in the "
3298 "Attributor.");
3299 break;
3300 case Instruction::Call:
3301 // Calls are interesting on their own, additionally:
3302 // For `llvm.assume` calls we also fill the KnowledgeMap as we find them.
3303 // For `must-tail` calls we remember the caller and callee.
3304 if (auto *Assume = dyn_cast<AssumeInst>(&I)) {
3305 AssumeOnlyValues.insert(Assume);
3306 fillMapFromAssume(*Assume, KnowledgeMap);
3307 AddToAssumeUsesMap(*Assume->getArgOperand(0));
3308 } else if (cast<CallInst>(I).isMustTailCall()) {
3309 FI.ContainsMustTailCall = true;
3310 if (auto *Callee = dyn_cast_if_present<Function>(
3311 cast<CallInst>(I).getCalledOperand()))
3312 getFunctionInfo(*Callee).CalledViaMustTail = true;
3313 }
3314 [[fallthrough]];
3315 case Instruction::CallBr:
3316 case Instruction::Invoke:
3317 case Instruction::CleanupRet:
3318 case Instruction::CatchSwitch:
3319 case Instruction::AtomicRMW:
3320 case Instruction::AtomicCmpXchg:
3321 case Instruction::UncondBr:
3322 case Instruction::CondBr:
3323 case Instruction::Resume:
3324 case Instruction::Ret:
3325 case Instruction::Load:
3326 // The alignment of a pointer is interesting for loads.
3327 case Instruction::Store:
3328 // The alignment of a pointer is interesting for stores.
3329 case Instruction::Alloca:
3330 case Instruction::AddrSpaceCast:
3331 IsInterestingOpcode = true;
3332 }
3333 if (IsInterestingOpcode) {
3334 auto *&Insts = FI.OpcodeInstMap[I.getOpcode()];
3335 if (!Insts)
3336 Insts = new (Allocator) InstructionVectorTy();
3337 Insts->push_back(&I);
3338 }
3339 if (I.mayReadOrWriteMemory())
3340 FI.RWInsts.push_back(&I);
3341 }
3342
3343 if (F.hasFnAttribute(Attribute::AlwaysInline) &&
3344 isInlineViable(F).isSuccess())
3345 InlineableFunctions.insert(&F);
3346}
3347
3348InformationCache::FunctionInfo::~FunctionInfo() {
3349 // The instruction vectors are allocated using a BumpPtrAllocator, we need to
3350 // manually destroy them.
3351 for (auto &It : OpcodeInstMap)
3352 It.getSecond()->~InstructionVectorTy();
3353}
3354
3357 assert(A.isClosedWorldModule() && "Cannot see all indirect callees!");
3358 return IndirectlyCallableFunctions;
3359}
3360
3361std::optional<unsigned> InformationCache::getFlatAddressSpace() const {
3362 if (IsTargetGPU())
3363 return 0;
3364 return std::nullopt;
3365}
3366
3368 const AbstractAttribute &ToAA,
3369 DepClassTy DepClass) {
3370 if (DepClass == DepClassTy::NONE)
3371 return;
3372 // If we are outside of an update, thus before the actual fixpoint iteration
3373 // started (= when we create AAs), we do not track dependences because we will
3374 // put all AAs into the initial worklist anyway.
3375 if (DependenceStack.empty())
3376 return;
3377 if (FromAA.getState().isAtFixpoint())
3378 return;
3379 DependenceStack.back()->push_back({&FromAA, &ToAA, DepClass});
3380}
3381
3382void Attributor::rememberDependences() {
3383 assert(!DependenceStack.empty() && "No dependences to remember!");
3384
3385 for (DepInfo &DI : *DependenceStack.back()) {
3386 assert((DI.DepClass == DepClassTy::REQUIRED ||
3387 DI.DepClass == DepClassTy::OPTIONAL) &&
3388 "Expected required or optional dependence (1 bit)!");
3389 auto &DepAAs = const_cast<AbstractAttribute &>(*DI.FromAA).Deps;
3390 DepAAs.insert(AbstractAttribute::DepTy(
3391 const_cast<AbstractAttribute *>(DI.ToAA), unsigned(DI.DepClass)));
3392 }
3393}
3394
3395template <Attribute::AttrKind AK, typename AAType>
3396void Attributor::checkAndQueryIRAttr(const IRPosition &IRP, AttributeSet Attrs,
3397 bool SkipHasAttrCheck) {
3398 bool IsKnown;
3399 if (SkipHasAttrCheck || !Attrs.hasAttribute(AK))
3400 if (!Configuration.Allowed || Configuration.Allowed->count(&AAType::ID))
3401 if (!AA::hasAssumedIRAttr<AK>(*this, nullptr, IRP, DepClassTy::NONE,
3402 IsKnown))
3403 getOrCreateAAFor<AAType>(IRP);
3404}
3405
3407 assert(!F.isDeclaration());
3408
3409 if (!VisitedFunctions.insert(&F).second)
3410 return;
3411
3412 // In non-module runs we need to look at the call sites of a function to
3413 // determine if it is part of a must-tail call edge. This will influence what
3414 // attributes we can derive.
3415 InformationCache::FunctionInfo &FI = InfoCache.getFunctionInfo(F);
3416 if (!isModulePass() && !FI.CalledViaMustTail) {
3417 for (const Use &U : F.uses())
3418 if (const auto *CB = dyn_cast<CallBase>(U.getUser()))
3419 if (CB->isCallee(&U) && CB->isMustTailCall())
3420 FI.CalledViaMustTail = true;
3421 }
3422
3424 bool IsIPOAmendable = isFunctionIPOAmendable(F);
3425 auto Attrs = F.getAttributes();
3426 auto FnAttrs = Attrs.getFnAttrs();
3427
3428 // Check for dead BasicBlocks in every function.
3429 // We need dead instruction detection because we do not want to deal with
3430 // broken IR in which SSA rules do not apply.
3432
3433 // Every function might contain instructions that cause "undefined
3434 // behavior".
3436
3437 // Every function might be applicable for Heap-To-Stack conversion.
3440
3441 // Every function might be "must-progress".
3442 checkAndQueryIRAttr<Attribute::MustProgress, AAMustProgress>(FPos, FnAttrs);
3443
3444 // Every function might be "no-free".
3445 checkAndQueryIRAttr<Attribute::NoFree, AANoFree>(FPos, FnAttrs);
3446
3447 // Every function might be "will-return".
3448 checkAndQueryIRAttr<Attribute::WillReturn, AAWillReturn>(FPos, FnAttrs);
3449
3450 // Every function might be marked "nosync"
3451 checkAndQueryIRAttr<Attribute::NoSync, AANoSync>(FPos, FnAttrs);
3452
3453 // Everything that is visible from the outside (=function, argument, return
3454 // positions), cannot be changed if the function is not IPO amendable. We can
3455 // however analyse the code inside.
3456 if (IsIPOAmendable) {
3457
3458 // Every function can be nounwind.
3459 checkAndQueryIRAttr<Attribute::NoUnwind, AANoUnwind>(FPos, FnAttrs);
3460
3461 // Every function might be "no-return".
3462 checkAndQueryIRAttr<Attribute::NoReturn, AANoReturn>(FPos, FnAttrs);
3463
3464 // Every function might be "no-recurse".
3465 checkAndQueryIRAttr<Attribute::NoRecurse, AANoRecurse>(FPos, FnAttrs);
3466
3467 // Every function can be "non-convergent".
3468 if (Attrs.hasFnAttr(Attribute::Convergent))
3470
3471 // Every function might be "readnone/readonly/writeonly/...".
3473
3474 // Every function can be "readnone/argmemonly/inaccessiblememonly/...".
3476
3477 // Every function can track active assumptions.
3479
3480 // If we're not using a dynamic mode for float, there's nothing worthwhile
3481 // to infer. This misses the edge case denormal-fp-math="dynamic" and
3482 // denormal-fp-math-f32=something, but that likely has no real world use.
3483 DenormalMode Mode = F.getDenormalMode(APFloat::IEEEsingle());
3484 if (Mode.Input == DenormalMode::Dynamic ||
3485 Mode.Output == DenormalMode::Dynamic)
3487
3488 // Return attributes are only appropriate if the return type is non void.
3489 Type *ReturnType = F.getReturnType();
3490 if (!ReturnType->isVoidTy()) {
3492 AttributeSet RetAttrs = Attrs.getRetAttrs();
3493
3494 // Every returned value might be dead.
3496
3497 // Every function might be simplified.
3498 bool UsedAssumedInformation = false;
3499 getAssumedSimplified(RetPos, nullptr, UsedAssumedInformation,
3501
3502 // Every returned value might be marked noundef.
3503 checkAndQueryIRAttr<Attribute::NoUndef, AANoUndef>(RetPos, RetAttrs);
3504
3505 if (ReturnType->isPointerTy()) {
3506
3507 // Every function with pointer return type might be marked align.
3509
3510 // Every function with pointer return type might be marked nonnull.
3511 checkAndQueryIRAttr<Attribute::NonNull, AANonNull>(RetPos, RetAttrs);
3512
3513 // Every function with pointer return type might be marked noalias.
3514 checkAndQueryIRAttr<Attribute::NoAlias, AANoAlias>(RetPos, RetAttrs);
3515
3516 // Every function with pointer return type might be marked
3517 // dereferenceable.
3519 } else if (AttributeFuncs::isNoFPClassCompatibleType(ReturnType)) {
3521 }
3522 }
3523 }
3524
3525 for (Argument &Arg : F.args()) {
3526 IRPosition ArgPos = IRPosition::argument(Arg);
3527 auto ArgNo = Arg.getArgNo();
3528 AttributeSet ArgAttrs = Attrs.getParamAttrs(ArgNo);
3529
3530 if (!IsIPOAmendable) {
3531 if (Arg.getType()->isPointerTy())
3532 // Every argument with pointer type might be marked nofree.
3533 checkAndQueryIRAttr<Attribute::NoFree, AANoFree>(ArgPos, ArgAttrs);
3534 continue;
3535 }
3536
3537 // Every argument might be simplified. We have to go through the
3538 // Attributor interface though as outside AAs can register custom
3539 // simplification callbacks.
3540 bool UsedAssumedInformation = false;
3541 getAssumedSimplified(ArgPos, /* AA */ nullptr, UsedAssumedInformation,
3543
3544 // Every argument might be dead.
3546
3547 // Every argument might be marked noundef.
3548 checkAndQueryIRAttr<Attribute::NoUndef, AANoUndef>(ArgPos, ArgAttrs);
3549
3550 if (Arg.getType()->isPointerTy()) {
3551 // Every argument with pointer type might be marked nonnull.
3552 checkAndQueryIRAttr<Attribute::NonNull, AANonNull>(ArgPos, ArgAttrs);
3553
3554 // Every argument with pointer type might be marked noalias.
3555 checkAndQueryIRAttr<Attribute::NoAlias, AANoAlias>(ArgPos, ArgAttrs);
3556
3557 // Every argument with pointer type might be marked dereferenceable.
3559
3560 // Every argument with pointer type might be marked align.
3562
3563 // Every argument with pointer type might be marked nocapture.
3564 checkAndQueryIRAttr<Attribute::Captures, AANoCapture>(
3565 ArgPos, ArgAttrs, /*SkipHasAttrCheck=*/true);
3566
3567 // Every argument with pointer type might be marked
3568 // "readnone/readonly/writeonly/..."
3570
3571 // Every argument with pointer type might be marked nofree.
3572 checkAndQueryIRAttr<Attribute::NoFree, AANoFree>(ArgPos, ArgAttrs);
3573
3574 // Every argument with pointer type might be privatizable (or
3575 // promotable)
3577 } else if (AttributeFuncs::isNoFPClassCompatibleType(Arg.getType())) {
3579 }
3580 }
3581
3582 auto CallSitePred = [&](Instruction &I) -> bool {
3583 auto &CB = cast<CallBase>(I);
3584 IRPosition CBInstPos = IRPosition::inst(CB);
3586
3587 // Call sites might be dead if they do not have side effects and no live
3588 // users. The return value might be dead if there are no live users.
3589 getOrCreateAAFor<AAIsDead>(CBInstPos);
3590
3591 Function *Callee = dyn_cast_if_present<Function>(CB.getCalledOperand());
3592 // TODO: Even if the callee is not known now we might be able to simplify
3593 // the call/callee.
3594 if (!Callee) {
3596 return true;
3597 }
3598
3599 // Every call site can track active assumptions.
3601
3602 // Skip declarations except if annotations on their call sites were
3603 // explicitly requested.
3604 if (!AnnotateDeclarationCallSites && Callee->isDeclaration() &&
3605 !Callee->hasMetadata(LLVMContext::MD_callback))
3606 return true;
3607
3608 if (!Callee->getReturnType()->isVoidTy() && !CB.use_empty()) {
3610 bool UsedAssumedInformation = false;
3611 getAssumedSimplified(CBRetPos, nullptr, UsedAssumedInformation,
3613
3614 if (AttributeFuncs::isNoFPClassCompatibleType(Callee->getReturnType()))
3616 }
3617
3618 const AttributeList &CBAttrs = CBFnPos.getAttrList();
3619 for (int I = 0, E = CB.arg_size(); I < E; ++I) {
3620
3622 AttributeSet CBArgAttrs = CBAttrs.getParamAttrs(I);
3623
3624 // Every call site argument might be dead.
3626
3627 // Call site argument might be simplified. We have to go through the
3628 // Attributor interface though as outside AAs can register custom
3629 // simplification callbacks.
3630 bool UsedAssumedInformation = false;
3631 getAssumedSimplified(CBArgPos, /* AA */ nullptr, UsedAssumedInformation,
3633
3634 // Every call site argument might be marked "noundef".
3635 checkAndQueryIRAttr<Attribute::NoUndef, AANoUndef>(CBArgPos, CBArgAttrs);
3636
3637 Type *ArgTy = CB.getArgOperand(I)->getType();
3638
3639 if (!ArgTy->isPointerTy()) {
3640 if (AttributeFuncs::isNoFPClassCompatibleType(ArgTy))
3642
3643 continue;
3644 }
3645
3646 // Call site argument attribute "non-null".
3647 checkAndQueryIRAttr<Attribute::NonNull, AANonNull>(CBArgPos, CBArgAttrs);
3648
3649 // Call site argument attribute "captures(none)".
3650 checkAndQueryIRAttr<Attribute::Captures, AANoCapture>(
3651 CBArgPos, CBArgAttrs, /*SkipHasAttrCheck=*/true);
3652
3653 // Call site argument attribute "no-alias".
3654 checkAndQueryIRAttr<Attribute::NoAlias, AANoAlias>(CBArgPos, CBArgAttrs);
3655
3656 // Call site argument attribute "dereferenceable".
3658
3659 // Call site argument attribute "align".
3660 getOrCreateAAFor<AAAlign>(CBArgPos);
3661
3662 // Call site argument attribute
3663 // "readnone/readonly/writeonly/..."
3664 if (!CBAttrs.hasParamAttr(I, Attribute::ReadNone))
3666
3667 // Call site argument attribute "nofree".
3668 checkAndQueryIRAttr<Attribute::NoFree, AANoFree>(CBArgPos, CBArgAttrs);
3669 }
3670 return true;
3671 };
3672
3673 auto &OpcodeInstMap = InfoCache.getOpcodeInstMapForFunction(F);
3674 [[maybe_unused]] bool Success;
3675 bool UsedAssumedInformation = false;
3677 nullptr, OpcodeInstMap, CallSitePred, nullptr, nullptr,
3678 {(unsigned)Instruction::Invoke, (unsigned)Instruction::CallBr,
3679 (unsigned)Instruction::Call},
3680 UsedAssumedInformation);
3681 assert(Success && "Expected the check call to be successful!");
3682
3683 auto LoadStorePred = [&](Instruction &I) -> bool {
3684 if (auto *LI = dyn_cast<LoadInst>(&I)) {
3685 getOrCreateAAFor<AAAlign>(IRPosition::value(*LI->getPointerOperand()));
3686 if (SimplifyAllLoads)
3688 UsedAssumedInformation, AA::Intraprocedural);
3690 IRPosition::value(*LI->getPointerOperand()));
3692 IRPosition::value(*LI->getPointerOperand()));
3693 } else {
3694 auto &SI = cast<StoreInst>(I);
3696 getAssumedSimplified(IRPosition::value(*SI.getValueOperand()), nullptr,
3697 UsedAssumedInformation, AA::Intraprocedural);
3698 getOrCreateAAFor<AAAlign>(IRPosition::value(*SI.getPointerOperand()));
3700 IRPosition::value(*SI.getPointerOperand()));
3701 }
3702 return true;
3703 };
3705 nullptr, OpcodeInstMap, LoadStorePred, nullptr, nullptr,
3706 {(unsigned)Instruction::Load, (unsigned)Instruction::Store},
3707 UsedAssumedInformation);
3708 assert(Success && "Expected the check call to be successful!");
3709
3710 // AllocaInstPredicate
3711 auto AAAllocationInfoPred = [&](Instruction &I) -> bool {
3713 return true;
3714 };
3715
3717 nullptr, OpcodeInstMap, AAAllocationInfoPred, nullptr, nullptr,
3718 {(unsigned)Instruction::Alloca}, UsedAssumedInformation);
3719 assert(Success && "Expected the check call to be successful!");
3720}
3721
3723 if (CloseWorldAssumption.getNumOccurrences())
3724 return CloseWorldAssumption;
3725 return isModulePass() && Configuration.IsClosedWorldModule;
3726}
3727
3728/// Helpers to ease debugging through output streams and print calls.
3729///
3730///{
3732 return OS << (S == ChangeStatus::CHANGED ? "changed" : "unchanged");
3733}
3734
3736 switch (AP) {
3738 return OS << "inv";
3740 return OS << "flt";
3742 return OS << "fn_ret";
3744 return OS << "cs_ret";
3746 return OS << "fn";
3748 return OS << "cs";
3750 return OS << "arg";
3752 return OS << "cs_arg";
3753 }
3754 llvm_unreachable("Unknown attribute position!");
3755}
3756
3758 const Value &AV = Pos.getAssociatedValue();
3759 OS << "{" << Pos.getPositionKind() << ":" << AV.getName() << " ["
3760 << Pos.getAnchorValue().getName() << "@" << Pos.getCallSiteArgNo() << "]";
3761
3762 if (Pos.hasCallBaseContext())
3763 OS << "[cb_context:" << *Pos.getCallBaseContext() << "]";
3764 return OS << "}";
3765}
3766
3768 OS << "range-state(" << S.getBitWidth() << ")<";
3769 S.getKnown().print(OS);
3770 OS << " / ";
3771 S.getAssumed().print(OS);
3772 OS << ">";
3773
3774 return OS << static_cast<const AbstractState &>(S);
3775}
3776
3778 return OS << (!S.isValidState() ? "top" : (S.isAtFixpoint() ? "fix" : ""));
3779}
3780
3782 AA.print(OS);
3783 return OS;
3784}
3785
3788 OS << "set-state(< {";
3789 if (!S.isValidState())
3790 OS << "full-set";
3791 else {
3792 for (const auto &It : S.getAssumedSet())
3793 OS << It << ", ";
3794 if (S.undefIsContained())
3795 OS << "undef ";
3796 }
3797 OS << "} >)";
3798
3799 return OS;
3800}
3801
3803 const PotentialLLVMValuesState &S) {
3804 OS << "set-state(< {";
3805 if (!S.isValidState())
3806 OS << "full-set";
3807 else {
3808 for (const auto &It : S.getAssumedSet()) {
3809 if (auto *F = dyn_cast<Function>(It.first.getValue()))
3810 OS << "@" << F->getName() << "[" << int(It.second) << "], ";
3811 else
3812 OS << *It.first.getValue() << "[" << int(It.second) << "], ";
3813 }
3814 if (S.undefIsContained())
3815 OS << "undef ";
3816 }
3817 OS << "} >)";
3818
3819 return OS;
3820}
3821
3823 OS << "[";
3824 OS << getName();
3825 OS << "] for CtxI ";
3826
3827 if (auto *I = getCtxI()) {
3828 OS << "'";
3829 I->print(OS);
3830 OS << "'";
3831 } else
3832 OS << "<<null inst>>";
3833
3834 OS << " at position " << getIRPosition() << " with state " << getAsStr(A)
3835 << '\n';
3836}
3837
3839 print(OS);
3840
3841 for (const auto &DepAA : Deps) {
3842 auto *AA = DepAA.getPointer();
3843 OS << " updates ";
3844 AA->print(OS);
3845 }
3846
3847 OS << '\n';
3848}
3849
3851 const AAPointerInfo::Access &Acc) {
3852 OS << " [" << Acc.getKind() << "] " << *Acc.getRemoteInst();
3853 if (Acc.getLocalInst() != Acc.getRemoteInst())
3854 OS << " via " << *Acc.getLocalInst();
3855 if (Acc.getContent()) {
3856 if (*Acc.getContent())
3857 OS << " [" << **Acc.getContent() << "]";
3858 else
3859 OS << " [ <unknown> ]";
3860 }
3861 return OS;
3862}
3863///}
3864
3865/// ----------------------------------------------------------------------------
3866/// Pass (Manager) Boilerplate
3867/// ----------------------------------------------------------------------------
3868
3870 SetVector<Function *> &Functions,
3871 AnalysisGetter &AG,
3872 CallGraphUpdater &CGUpdater,
3873 bool DeleteFns, bool IsModulePass) {
3874 if (Functions.empty())
3875 return false;
3876
3877 LLVM_DEBUG({
3878 dbgs() << "[Attributor] Run on module with " << Functions.size()
3879 << " functions:\n";
3880 for (Function *Fn : Functions)
3881 dbgs() << " - " << Fn->getName() << "\n";
3882 });
3883
3884 // Create an Attributor and initially empty information cache that is filled
3885 // while we identify default attribute opportunities.
3886 AttributorConfig AC(CGUpdater);
3887 AC.IsModulePass = IsModulePass;
3888 AC.DeleteFns = DeleteFns;
3889
3890 /// Tracking callback for specialization of indirect calls.
3892 IndirectCalleeTrackingMap;
3893 if (MaxSpecializationPerCB.getNumOccurrences()) {
3894 AC.IndirectCalleeSpecializationCallback =
3895 [&](Attributor &, const AbstractAttribute &AA, CallBase &CB,
3896 Function &Callee, unsigned) {
3897 if (MaxSpecializationPerCB == 0)
3898 return false;
3899 auto &Set = IndirectCalleeTrackingMap[&CB];
3900 if (!Set)
3901 Set = std::make_unique<SmallPtrSet<Function *, 8>>();
3902 if (Set->size() >= MaxSpecializationPerCB)
3903 return Set->contains(&Callee);
3904 Set->insert(&Callee);
3905 return true;
3906 };
3907 }
3908
3909 Attributor A(Functions, InfoCache, AC);
3910
3911 // Create shallow wrappers for all functions that are not IPO amendable
3913 for (Function *F : Functions)
3914 if (!A.isFunctionIPOAmendable(*F))
3916
3917 // Internalize non-exact functions
3918 // TODO: for now we eagerly internalize functions without calculating the
3919 // cost, we need a cost interface to determine whether internalizing
3920 // a function is "beneficial"
3921 if (AllowDeepWrapper) {
3922 unsigned FunSize = Functions.size();
3923 for (unsigned u = 0; u < FunSize; u++) {
3924 Function *F = Functions[u];
3925 if (!F->isDeclaration() && !F->isDefinitionExact() && !F->use_empty() &&
3926 !GlobalValue::isInterposableLinkage(F->getLinkage())) {
3928 assert(NewF && "Could not internalize function.");
3929 Functions.insert(NewF);
3930
3931 // Update call graph
3932 CGUpdater.replaceFunctionWith(*F, *NewF);
3933 for (const Use &U : NewF->uses())
3934 if (CallBase *CB = dyn_cast<CallBase>(U.getUser())) {
3935 auto *CallerF = CB->getCaller();
3936 CGUpdater.reanalyzeFunction(*CallerF);
3937 }
3938 }
3939 }
3940 }
3941
3942 for (Function *F : Functions) {
3943 if (F->isDeclaration())
3944 continue;
3945
3946 if (F->hasExactDefinition())
3947 NumFnWithExactDefinition++;
3948 else
3949 NumFnWithoutExactDefinition++;
3950
3951 // We look at internal functions only on-demand but if any use is not a
3952 // direct call or outside the current set of analyzed functions, we have
3953 // to do it eagerly.
3954 if (F->hasLocalLinkage()) {
3955 if (llvm::all_of(F->uses(), [&Functions](const Use &U) {
3956 const auto *CB = dyn_cast<CallBase>(U.getUser());
3957 return CB && CB->isCallee(&U) &&
3958 Functions.count(const_cast<Function *>(CB->getCaller()));
3959 }))
3960 continue;
3961 }
3962
3963 // Populate the Attributor with abstract attribute opportunities in the
3964 // function and the information cache with IR information.
3965 A.identifyDefaultAbstractAttributes(*F);
3966 }
3967
3968 ChangeStatus Changed = A.run();
3969
3970 LLVM_DEBUG(dbgs() << "[Attributor] Done with " << Functions.size()
3971 << " functions, result: " << Changed << ".\n");
3973}
3974
3976 SetVector<Function *> &Functions,
3977 AnalysisGetter &AG,
3978 CallGraphUpdater &CGUpdater,
3980 bool IsModulePass) {
3981 if (Functions.empty())
3982 return false;
3983
3984 LLVM_DEBUG({
3985 dbgs() << "[AttributorLight] Run on module with " << Functions.size()
3986 << " functions:\n";
3987 for (Function *Fn : Functions)
3988 dbgs() << " - " << Fn->getName() << "\n";
3989 });
3990
3991 // Create an Attributor and initially empty information cache that is filled
3992 // while we identify default attribute opportunities.
3993 AttributorConfig AC(CGUpdater);
3994 AC.IsModulePass = IsModulePass;
3995 AC.DeleteFns = false;
3996 DenseSet<const char *> Allowed(
4003 AC.Allowed = &Allowed;
4004 AC.UseLiveness = false;
4005
4006 Attributor A(Functions, InfoCache, AC);
4007
4008 for (Function *F : Functions) {
4009 if (F->isDeclaration())
4010 continue;
4011
4012 if (F->hasExactDefinition())
4013 NumFnWithExactDefinition++;
4014 else
4015 NumFnWithoutExactDefinition++;
4016
4017 // We look at internal functions only on-demand but if any use is not a
4018 // direct call or outside the current set of analyzed functions, we have
4019 // to do it eagerly.
4020 if (AC.UseLiveness && F->hasLocalLinkage()) {
4021 if (llvm::all_of(F->uses(), [&Functions](const Use &U) {
4022 const auto *CB = dyn_cast<CallBase>(U.getUser());
4023 return CB && CB->isCallee(&U) &&
4024 Functions.count(const_cast<Function *>(CB->getCaller()));
4025 }))
4026 continue;
4027 }
4028
4029 // Populate the Attributor with abstract attribute opportunities in the
4030 // function and the information cache with IR information.
4031 A.identifyDefaultAbstractAttributes(*F);
4032 }
4033
4034 ChangeStatus Changed = A.run();
4035
4037 // Invalidate analyses for modified functions so that we don't have to
4038 // invalidate all analyses for all functions in this SCC.
4039 PreservedAnalyses FuncPA;
4040 // We haven't changed the CFG for modified functions.
4041 FuncPA.preserveSet<CFGAnalyses>();
4042 for (Function *Changed : A.getModifiedFunctions()) {
4043 FAM.invalidate(*Changed, FuncPA);
4044 // Also invalidate any direct callers of changed functions since analyses
4045 // may care about attributes of direct callees. For example, MemorySSA
4046 // cares about whether or not a call's callee modifies memory and queries
4047 // that through function attributes.
4048 for (auto *U : Changed->users()) {
4049 if (auto *Call = dyn_cast<CallBase>(U)) {
4050 if (Call->getCalledFunction() == Changed)
4051 FAM.invalidate(*Call->getFunction(), FuncPA);
4052 }
4053 }
4054 }
4055 }
4056 LLVM_DEBUG(dbgs() << "[Attributor] Done with " << Functions.size()
4057 << " functions, result: " << Changed << ".\n");
4059}
4060
4061void AADepGraph::viewGraph() { llvm::ViewGraph(this, "Dependency Graph"); }
4062
4064 static std::atomic<int> CallTimes;
4065 std::string Prefix;
4066
4067 if (!DepGraphDotFileNamePrefix.empty())
4069 else
4070 Prefix = "dep_graph";
4071 std::string Filename =
4072 Prefix + "_" + std::to_string(CallTimes.load()) + ".dot";
4073
4074 outs() << "Dependency graph dump to " << Filename << ".\n";
4075
4076 std::error_code EC;
4077
4079 if (!EC)
4080 llvm::WriteGraph(File, this);
4081
4082 CallTimes++;
4083}
4084
4086 for (auto DepAA : SyntheticRoot.Deps)
4087 cast<AbstractAttribute>(DepAA.getPointer())->printWithDeps(outs());
4088}
4089
4093 AnalysisGetter AG(FAM);
4094
4095 SetVector<Function *> Functions;
4096 for (Function &F : M)
4097 Functions.insert(&F);
4098
4099 CallGraphUpdater CGUpdater;
4100 BumpPtrAllocator Allocator;
4101 InformationCache InfoCache(M, AG, Allocator, /* CGSCC */ nullptr);
4102 if (runAttributorOnFunctions(InfoCache, Functions, AG, CGUpdater,
4103 /* DeleteFns */ true, /* IsModulePass */ true)) {
4104 // FIXME: Think about passes we will preserve and add them here.
4105 return PreservedAnalyses::none();
4106 }
4107 return PreservedAnalyses::all();
4108}
4109
4112 LazyCallGraph &CG,
4113 CGSCCUpdateResult &UR) {
4115 AM.getResult<FunctionAnalysisManagerCGSCCProxy>(C, CG).getManager();
4116 AnalysisGetter AG(FAM);
4117
4118 SetVector<Function *> Functions;
4119 for (LazyCallGraph::Node &N : C)
4120 Functions.insert(&N.getFunction());
4121
4122 if (Functions.empty())
4123 return PreservedAnalyses::all();
4124
4125 Module &M = *Functions.back()->getParent();
4126 CallGraphUpdater CGUpdater;
4127 CGUpdater.initialize(CG, C, AM, UR);
4128 BumpPtrAllocator Allocator;
4129 InformationCache InfoCache(M, AG, Allocator, /* CGSCC */ &Functions);
4130 if (runAttributorOnFunctions(InfoCache, Functions, AG, CGUpdater,
4131 /* DeleteFns */ false,
4132 /* IsModulePass */ false)) {
4133 // FIXME: Think about passes we will preserve and add them here.
4136 return PA;
4137 }
4138 return PreservedAnalyses::all();
4139}
4140
4145 AnalysisGetter AG(FAM, /* CachedOnly */ true);
4146
4147 SetVector<Function *> Functions;
4148 for (Function &F : M)
4149 Functions.insert(&F);
4150
4151 CallGraphUpdater CGUpdater;
4152 BumpPtrAllocator Allocator;
4153 InformationCache InfoCache(M, AG, Allocator, /* CGSCC */ nullptr);
4154 if (runAttributorLightOnFunctions(InfoCache, Functions, AG, CGUpdater, FAM,
4155 /* IsModulePass */ true)) {
4157 // We have not added or removed functions.
4159 // We already invalidated all relevant function analyses above.
4161 return PA;
4162 }
4163 return PreservedAnalyses::all();
4164}
4165
4168 LazyCallGraph &CG,
4169 CGSCCUpdateResult &UR) {
4171 AM.getResult<FunctionAnalysisManagerCGSCCProxy>(C, CG).getManager();
4172 AnalysisGetter AG(FAM);
4173
4174 SetVector<Function *> Functions;
4175 for (LazyCallGraph::Node &N : C)
4176 Functions.insert(&N.getFunction());
4177
4178 if (Functions.empty())
4179 return PreservedAnalyses::all();
4180
4181 Module &M = *Functions.back()->getParent();
4182 CallGraphUpdater CGUpdater;
4183 CGUpdater.initialize(CG, C, AM, UR);
4184 BumpPtrAllocator Allocator;
4185 InformationCache InfoCache(M, AG, Allocator, /* CGSCC */ &Functions);
4186 if (runAttributorLightOnFunctions(InfoCache, Functions, AG, CGUpdater, FAM,
4187 /* IsModulePass */ false)) {
4189 // We have not added or removed functions.
4191 // We already invalidated all relevant function analyses above.
4193 return PA;
4194 }
4195 return PreservedAnalyses::all();
4196}
4197namespace llvm {
4198
4215
4216template <>
4218 static NodeRef getEntryNode(AADepGraph *DG) { return DG->GetEntryNode(); }
4219
4222
4223 static nodes_iterator nodes_begin(AADepGraph *DG) { return DG->begin(); }
4224
4225 static nodes_iterator nodes_end(AADepGraph *DG) { return DG->end(); }
4226};
4227
4228template <> struct DOTGraphTraits<AADepGraph *> : public DefaultDOTGraphTraits {
4230
4231 static std::string getNodeLabel(const AADepGraphNode *Node,
4232 const AADepGraph *DG) {
4233 std::string AAString;
4234 raw_string_ostream O(AAString);
4235 Node->print(O);
4236 return AAString;
4237 }
4238};
4239
4240} // 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:270
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition Dominators.h:151
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:2605
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:2539
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:485
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:497
@ NONE
Do not track a dependence between source and target.
Definition Attributor.h:498
@ REQUIRED
The target cannot be valid if the source is not.
Definition Attributor.h:496
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:512
PointerIntPair< AADepGraphNode *, 1 > DepTy
Definition Attributor.h:506
The data structure for the dependency graph.
Definition Attributor.h:546
iterator begin()
Definition Attributor.h:561
LLVM_ABI void viewGraph()
AADepGraphNode SyntheticRoot
There is no root node for the dependency graph.
Definition Attributor.h:558
LLVM_ABI void print()
Print dependency graph.
iterator end()
Definition Attributor.h:562
LLVM_ABI void dumpGraph()
Dump graph to file.
AADepGraphNode * GetEntryNode()
Definition Attributor.h:559
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.
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:582
Function * getAssociatedFunction() const
Return the associated function, if any.
Definition Attributor.h:713
void setAttrList(const AttributeList &AttrList) const
Update the attributes associated with this function or call site scope.
Definition Attributor.h:849
unsigned getAttrIdx() const
Return the index in the attribute list for this position.
Definition Attributor.h:814
bool hasCallBaseContext() const
Check if the position has any call base context.
Definition Attributor.h:931
static const IRPosition callsite_returned(const CallBase &CB)
Create a position describing the returned value of CB.
Definition Attributor.h:650
static const IRPosition returned(const Function &F, const CallBaseContext *CBContext=nullptr)
Create a position describing the returned value of F.
Definition Attributor.h:632
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:606
CallBase CallBaseContext
Definition Attributor.h:585
AttributeList getAttrList() const
Return the attributes associated with this function or call site scope.
Definition Attributor.h:842
static const IRPosition inst(const Instruction &I, const CallBaseContext *CBContext=nullptr)
Create a position describing the instruction I.
Definition Attributor.h:618
static const IRPosition callsite_argument(const CallBase &CB, unsigned ArgNo)
Create a position describing the argument of CB at position ArgNo.
Definition Attributor.h:655
Kind
The positions we distinguish in the IR.
Definition Attributor.h:588
@ IRP_ARGUMENT
An attribute for a function argument.
Definition Attributor.h:596
@ IRP_RETURNED
An attribute for the function return value.
Definition Attributor.h:592
@ IRP_CALL_SITE
An attribute for a call site (function scope).
Definition Attributor.h:595
@ IRP_CALL_SITE_RETURNED
An attribute for a call site return value.
Definition Attributor.h:593
@ IRP_FUNCTION
An attribute for a function (scope).
Definition Attributor.h:594
@ IRP_FLOAT
A position that is not associated with a spot suitable for attributes.
Definition Attributor.h:590
@ IRP_CALL_SITE_ARGUMENT
An attribute for a call site argument.
Definition Attributor.h:597
@ IRP_INVALID
An invalid position.
Definition Attributor.h:589
Instruction * getCtxI() const
Return the context instruction, if any.
Definition Attributor.h:766
static const IRPosition argument(const Argument &Arg, const CallBaseContext *CBContext=nullptr)
Create a position describing the argument Arg.
Definition Attributor.h:639
static const IRPosition function(const Function &F, const CallBaseContext *CBContext=nullptr)
Create a position describing the function scope of F.
Definition Attributor.h:625
const CallBaseContext * getCallBaseContext() const
Get the call base context from the position.
Definition Attributor.h:928
Value & getAssociatedValue() const
Return the value this abstract attribute is associated with.
Definition Attributor.h:780
Value & getAnchorValue() const
Return the value this abstract attribute is anchored with.
Definition Attributor.h:699
Value * getAttrListAnchor() const
Return the value attributes are attached to.
Definition Attributor.h:835
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:809
Kind getPositionKind() const
Return the associated position kind.
Definition Attributor.h:878
static const IRPosition callsite_function(const CallBase &CB)
Create a position describing the function scope of CB.
Definition Attributor.h:645
Function * getAnchorScope() const
Return the Function surrounding the anchor value.
Definition Attributor.h:754
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.