blob: e3920b9e1d2baf275001c71c4e1ce6ac05acca09 [file] [log] [blame]
//===- Attributor.cpp - Module-wide attribute deduction -------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file implements an interprocedural pass that deduces and/or propagates
// attributes. This is done in an abstract interpretation style fixpoint
// iteration. See the Attributor.h file comment and the class descriptions in
// that file for more information.
//
//===----------------------------------------------------------------------===//
#include "llvm/Transforms/IPO/Attributor.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/PointerIntPair.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/Analysis/AliasAnalysis.h"
#include "llvm/Analysis/CallGraph.h"
#include "llvm/Analysis/CallGraphSCCPass.h"
#include "llvm/Analysis/InlineCost.h"
#include "llvm/Analysis/MemoryBuiltins.h"
#include "llvm/Analysis/MustExecute.h"
#include "llvm/IR/AttributeMask.h"
#include "llvm/IR/Attributes.h"
#include "llvm/IR/Constant.h"
#include "llvm/IR/ConstantFold.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/GlobalValue.h"
#include "llvm/IR/GlobalVariable.h"
#include "llvm/IR/Instruction.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/ValueHandle.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/DebugCounter.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/GraphWriter.h"
#include "llvm/Support/ModRef.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
#include "llvm/Transforms/Utils/Cloning.h"
#include "llvm/Transforms/Utils/Local.h"
#include <cstdint>
#include <memory>
#ifdef EXPENSIVE_CHECKS
#include "llvm/IR/Verifier.h"
#endif
#include <cassert>
#include <optional>
#include <string>
using namespace llvm;
#define DEBUG_TYPE "attributor"
#define VERBOSE_DEBUG_TYPE DEBUG_TYPE "-verbose"
DEBUG_COUNTER(ManifestDBGCounter, "attributor-manifest",
"Determine what attributes are manifested in the IR");
STATISTIC(NumFnDeleted, "Number of function deleted");
STATISTIC(NumFnWithExactDefinition,
"Number of functions with exact definitions");
STATISTIC(NumFnWithoutExactDefinition,
"Number of functions without exact definitions");
STATISTIC(NumFnShallowWrappersCreated, "Number of shallow wrappers created");
STATISTIC(NumAttributesTimedOut,
"Number of abstract attributes timed out before fixpoint");
STATISTIC(NumAttributesValidFixpoint,
"Number of abstract attributes in a valid fixpoint state");
STATISTIC(NumAttributesManifested,
"Number of abstract attributes manifested in IR");
// TODO: Determine a good default value.
//
// In the LLVM-TS and SPEC2006, 32 seems to not induce compile time overheads
// (when run with the first 5 abstract attributes). The results also indicate
// that we never reach 32 iterations but always find a fixpoint sooner.
//
// This will become more evolved once we perform two interleaved fixpoint
// iterations: bottom-up and top-down.
static cl::opt<unsigned>
SetFixpointIterations("attributor-max-iterations", cl::Hidden,
cl::desc("Maximal number of fixpoint iterations."),
cl::init(32));
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<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));
unsigned llvm::MaxInitializationChainLength;
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<bool> EnableHeapToStack("enable-heap-to-stack-conversion",
cl::init(true), cl::Hidden);
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));
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));
// These options can only used for debug builds.
#ifndef NDEBUG
static cl::list<std::string>
SeedAllowList("attributor-seed-allow-list", cl::Hidden,
cl::desc("Comma seperated list of attribute names that are "
"allowed to be seeded."),
cl::CommaSeparated);
static cl::list<std::string> FunctionSeedAllowList(
"attributor-function-seed-allow-list", cl::Hidden,
cl::desc("Comma seperated list of function names that are "
"allowed to be seeded."),
cl::CommaSeparated);
#endif
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<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> ViewDepGraph("attributor-view-dep-graph", cl::Hidden,
cl::desc("View the dependency graph."),
cl::init(false));
static cl::opt<bool> PrintDependencies("attributor-print-dep", cl::Hidden,
cl::desc("Print attribute dependencies"),
cl::init(false));
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>
PrintCallGraph("attributor-print-call-graph", cl::Hidden,
cl::desc("Print Attributor's internal call graph"),
cl::init(false));
static cl::opt<bool> SimplifyAllLoads("attributor-simplify-all-loads",
cl::Hidden,
cl::desc("Try to simplify all loads."),
cl::init(true));
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."));
/// Logic operators for the change status enum class.
///
///{
ChangeStatus llvm::operator|(ChangeStatus L, ChangeStatus R) {
return L == ChangeStatus::CHANGED ? L : R;
}
ChangeStatus &llvm::operator|=(ChangeStatus &L, ChangeStatus R) {
L = L | R;
return L;
}
ChangeStatus llvm::operator&(ChangeStatus L, ChangeStatus R) {
return L == ChangeStatus::UNCHANGED ? L : R;
}
ChangeStatus &llvm::operator&=(ChangeStatus &L, ChangeStatus R) {
L = L & R;
return L;
}
///}
bool AA::isGPU(const Module &M) {
Triple T(M.getTargetTriple());
return T.isAMDGPU() || T.isNVPTX();
}
bool AA::isNoSyncInst(Attributor &A, const Instruction &I,
const AbstractAttribute &QueryingAA) {
// We are looking for volatile instructions or non-relaxed atomics.
if (const auto *CB = dyn_cast<CallBase>(&I)) {
if (CB->hasFnAttr(Attribute::NoSync))
return true;
// Non-convergent and readnone imply nosync.
if (!CB->isConvergent() && !CB->mayReadOrWriteMemory())
return true;
if (AANoSync::isNoSyncIntrinsic(&I))
return true;
bool IsKnownNoSync;
return AA::hasAssumedIRAttr<Attribute::NoSync>(
A, &QueryingAA, IRPosition::callsite_function(*CB),
DepClassTy::OPTIONAL, IsKnownNoSync);
}
if (!I.mayReadOrWriteMemory())
return true;
return !I.isVolatile() && !AANoSync::isNonRelaxedAtomic(&I);
}
bool AA::isDynamicallyUnique(Attributor &A, const AbstractAttribute &QueryingAA,
const Value &V, bool ForAnalysisOnly) {
// TODO: See the AAInstanceInfo class comment.
if (!ForAnalysisOnly)
return false;
auto *InstanceInfoAA = A.getAAFor<AAInstanceInfo>(
QueryingAA, IRPosition::value(V), DepClassTy::OPTIONAL);
return InstanceInfoAA && InstanceInfoAA->isAssumedUniqueForAnalysis();
}
Constant *
AA::getInitialValueForObj(Attributor &A, const AbstractAttribute &QueryingAA,
Value &Obj, Type &Ty, const TargetLibraryInfo *TLI,
const DataLayout &DL, AA::RangeTy *RangePtr) {
if (isa<AllocaInst>(Obj))
return UndefValue::get(&Ty);
if (Constant *Init = getInitialValueOfAllocation(&Obj, TLI, &Ty))
return Init;
auto *GV = dyn_cast<GlobalVariable>(&Obj);
if (!GV)
return nullptr;
bool UsedAssumedInformation = false;
Constant *Initializer = nullptr;
if (A.hasGlobalVariableSimplificationCallback(*GV)) {
auto AssumedGV = A.getAssumedInitializerFromCallBack(
*GV, &QueryingAA, UsedAssumedInformation);
Initializer = *AssumedGV;
if (!Initializer)
return nullptr;
} else {
if (!GV->hasLocalLinkage() &&
(GV->isInterposable() || !(GV->isConstant() && GV->hasInitializer())))
return nullptr;
if (!GV->hasInitializer())
return UndefValue::get(&Ty);
if (!Initializer)
Initializer = GV->getInitializer();
}
if (RangePtr && !RangePtr->offsetOrSizeAreUnknown()) {
APInt Offset = APInt(64, RangePtr->Offset);
return ConstantFoldLoadFromConst(Initializer, &Ty, Offset, DL);
}
return ConstantFoldLoadFromUniformValue(Initializer, &Ty, DL);
}
bool AA::isValidInScope(const Value &V, const Function *Scope) {
if (isa<Constant>(V))
return true;
if (auto *I = dyn_cast<Instruction>(&V))
return I->getFunction() == Scope;
if (auto *A = dyn_cast<Argument>(&V))
return A->getParent() == Scope;
return false;
}
bool AA::isValidAtPosition(const AA::ValueAndContext &VAC,
InformationCache &InfoCache) {
if (isa<Constant>(VAC.getValue()) || VAC.getValue() == VAC.getCtxI())
return true;
const Function *Scope = nullptr;
const Instruction *CtxI = VAC.getCtxI();
if (CtxI)
Scope = CtxI->getFunction();
if (auto *A = dyn_cast<Argument>(VAC.getValue()))
return A->getParent() == Scope;
if (auto *I = dyn_cast<Instruction>(VAC.getValue())) {
if (I->getFunction() == Scope) {
if (const DominatorTree *DT =
InfoCache.getAnalysisResultForFunction<DominatorTreeAnalysis>(
*Scope))
return DT->dominates(I, CtxI);
// Local dominance check mostly for the old PM passes.
if (CtxI && I->getParent() == CtxI->getParent())
return llvm::any_of(
make_range(I->getIterator(), I->getParent()->end()),
[&](const Instruction &AfterI) { return &AfterI == CtxI; });
}
}
return false;
}
Value *AA::getWithType(Value &V, Type &Ty) {
if (V.getType() == &Ty)
return &V;
if (isa<PoisonValue>(V))
return PoisonValue::get(&Ty);
if (isa<UndefValue>(V))
return UndefValue::get(&Ty);
if (auto *C = dyn_cast<Constant>(&V)) {
if (C->isNullValue())
return Constant::getNullValue(&Ty);
if (C->getType()->isPointerTy() && Ty.isPointerTy())
return ConstantExpr::getPointerCast(C, &Ty);
if (C->getType()->getPrimitiveSizeInBits() >= Ty.getPrimitiveSizeInBits()) {
if (C->getType()->isIntegerTy() && Ty.isIntegerTy())
return ConstantExpr::getTrunc(C, &Ty, /* OnlyIfReduced */ true);
if (C->getType()->isFloatingPointTy() && Ty.isFloatingPointTy())
return ConstantFoldCastInstruction(Instruction::FPTrunc, C, &Ty);
}
}
return nullptr;
}
std::optional<Value *>
AA::combineOptionalValuesInAAValueLatice(const std::optional<Value *> &A,
const std::optional<Value *> &B,
Type *Ty) {
if (A == B)
return A;
if (!B)
return A;
if (*B == nullptr)
return nullptr;
if (!A)
return Ty ? getWithType(**B, *Ty) : nullptr;
if (*A == nullptr)
return nullptr;
if (!Ty)
Ty = (*A)->getType();
if (isa_and_nonnull<UndefValue>(*A))
return getWithType(**B, *Ty);
if (isa<UndefValue>(*B))
return A;
if (*A && *B && *A == getWithType(**B, *Ty))
return A;
return nullptr;
}
template <bool IsLoad, typename Ty>
static bool getPotentialCopiesOfMemoryValue(
Attributor &A, Ty &I, SmallSetVector<Value *, 4> &PotentialCopies,
SmallSetVector<Instruction *, 4> *PotentialValueOrigins,
const AbstractAttribute &QueryingAA, bool &UsedAssumedInformation,
bool OnlyExact) {
LLVM_DEBUG(dbgs() << "Trying to determine the potential copies of " << I
<< " (only exact: " << OnlyExact << ")\n";);
Value &Ptr = *I.getPointerOperand();
// Containers to remember the pointer infos and new copies while we are not
// sure that we can find all of them. If we abort we want to avoid spurious
// dependences and potential copies in the provided container.
SmallVector<const AAPointerInfo *> PIs;
SmallSetVector<Value *, 8> NewCopies;
SmallSetVector<Instruction *, 8> NewCopyOrigins;
const auto *TLI =
A.getInfoCache().getTargetLibraryInfoForFunction(*I.getFunction());
auto Pred = [&](Value &Obj) {
LLVM_DEBUG(dbgs() << "Visit underlying object " << Obj << "\n");
if (isa<UndefValue>(&Obj))
return true;
if (isa<ConstantPointerNull>(&Obj)) {
// A null pointer access can be undefined but any offset from null may
// be OK. We do not try to optimize the latter.
if (!NullPointerIsDefined(I.getFunction(),
Ptr.getType()->getPointerAddressSpace()) &&
A.getAssumedSimplified(Ptr, QueryingAA, UsedAssumedInformation,
AA::Interprocedural) == &Obj)
return true;
LLVM_DEBUG(
dbgs() << "Underlying object is a valid nullptr, giving up.\n";);
return false;
}
// TODO: Use assumed noalias return.
if (!isa<AllocaInst>(&Obj) && !isa<GlobalVariable>(&Obj) &&
!(IsLoad ? isAllocationFn(&Obj, TLI) : isNoAliasCall(&Obj))) {
LLVM_DEBUG(dbgs() << "Underlying object is not supported yet: " << Obj
<< "\n";);
return false;
}
if (auto *GV = dyn_cast<GlobalVariable>(&Obj))
if (!GV->hasLocalLinkage() &&
!(GV->isConstant() && GV->hasInitializer())) {
LLVM_DEBUG(dbgs() << "Underlying object is global with external "
"linkage, not supported yet: "
<< Obj << "\n";);
return false;
}
bool NullOnly = true;
bool NullRequired = false;
auto CheckForNullOnlyAndUndef = [&](std::optional<Value *> V,
bool IsExact) {
if (!V || *V == nullptr)
NullOnly = false;
else if (isa<UndefValue>(*V))
/* No op */;
else if (isa<Constant>(*V) && cast<Constant>(*V)->isNullValue())
NullRequired = !IsExact;
else
NullOnly = false;
};
auto AdjustWrittenValueType = [&](const AAPointerInfo::Access &Acc,
Value &V) {
Value *AdjV = AA::getWithType(V, *I.getType());
if (!AdjV) {
LLVM_DEBUG(dbgs() << "Underlying object written but stored value "
"cannot be converted to read type: "
<< *Acc.getRemoteInst() << " : " << *I.getType()
<< "\n";);
}
return AdjV;
};
auto SkipCB = [&](const AAPointerInfo::Access &Acc) {
if ((IsLoad && !Acc.isWriteOrAssumption()) || (!IsLoad && !Acc.isRead()))
return true;
if (IsLoad) {
if (Acc.isWrittenValueYetUndetermined())
return true;
if (PotentialValueOrigins && !isa<AssumeInst>(Acc.getRemoteInst()))
return false;
if (!Acc.isWrittenValueUnknown())
if (Value *V = AdjustWrittenValueType(Acc, *Acc.getWrittenValue()))
if (NewCopies.count(V)) {
NewCopyOrigins.insert(Acc.getRemoteInst());
return true;
}
if (auto *SI = dyn_cast<StoreInst>(Acc.getRemoteInst()))
if (Value *V = AdjustWrittenValueType(Acc, *SI->getValueOperand()))
if (NewCopies.count(V)) {
NewCopyOrigins.insert(Acc.getRemoteInst());
return true;
}
}
return false;
};
auto CheckAccess = [&](const AAPointerInfo::Access &Acc, bool IsExact) {
if ((IsLoad && !Acc.isWriteOrAssumption()) || (!IsLoad && !Acc.isRead()))
return true;
if (IsLoad && Acc.isWrittenValueYetUndetermined())
return true;
CheckForNullOnlyAndUndef(Acc.getContent(), IsExact);
if (OnlyExact && !IsExact && !NullOnly &&
!isa_and_nonnull<UndefValue>(Acc.getWrittenValue())) {
LLVM_DEBUG(dbgs() << "Non exact access " << *Acc.getRemoteInst()
<< ", abort!\n");
return false;
}
if (NullRequired && !NullOnly) {
LLVM_DEBUG(dbgs() << "Required all `null` accesses due to non exact "
"one, however found non-null one: "
<< *Acc.getRemoteInst() << ", abort!\n");
return false;
}
if (IsLoad) {
assert(isa<LoadInst>(I) && "Expected load or store instruction only!");
if (!Acc.isWrittenValueUnknown()) {
Value *V = AdjustWrittenValueType(Acc, *Acc.getWrittenValue());
if (!V)
return false;
NewCopies.insert(V);
if (PotentialValueOrigins)
NewCopyOrigins.insert(Acc.getRemoteInst());
return true;
}
auto *SI = dyn_cast<StoreInst>(Acc.getRemoteInst());
if (!SI) {
LLVM_DEBUG(dbgs() << "Underlying object written through a non-store "
"instruction not supported yet: "
<< *Acc.getRemoteInst() << "\n";);
return false;
}
Value *V = AdjustWrittenValueType(Acc, *SI->getValueOperand());
if (!V)
return false;
NewCopies.insert(V);
if (PotentialValueOrigins)
NewCopyOrigins.insert(SI);
} else {
assert(isa<StoreInst>(I) && "Expected load or store instruction only!");
auto *LI = dyn_cast<LoadInst>(Acc.getRemoteInst());
if (!LI && OnlyExact) {
LLVM_DEBUG(dbgs() << "Underlying object read through a non-load "
"instruction not supported yet: "
<< *Acc.getRemoteInst() << "\n";);
return false;
}
NewCopies.insert(Acc.getRemoteInst());
}
return true;
};
// If the value has been written to we don't need the initial value of the
// object.
bool HasBeenWrittenTo = false;
AA::RangeTy Range;
auto *PI = A.getAAFor<AAPointerInfo>(QueryingAA, IRPosition::value(Obj),
DepClassTy::NONE);
if (!PI || !PI->forallInterferingAccesses(
A, QueryingAA, I,
/* FindInterferingWrites */ IsLoad,
/* FindInterferingReads */ !IsLoad, CheckAccess,
HasBeenWrittenTo, Range, SkipCB)) {
LLVM_DEBUG(
dbgs()
<< "Failed to verify all interfering accesses for underlying object: "
<< Obj << "\n");
return false;
}
if (IsLoad && !HasBeenWrittenTo && !Range.isUnassigned()) {
const DataLayout &DL = A.getDataLayout();
Value *InitialValue = AA::getInitialValueForObj(
A, QueryingAA, Obj, *I.getType(), TLI, DL, &Range);
if (!InitialValue) {
LLVM_DEBUG(dbgs() << "Could not determine required initial value of "
"underlying object, abort!\n");
return false;
}
CheckForNullOnlyAndUndef(InitialValue, /* IsExact */ true);
if (NullRequired && !NullOnly) {
LLVM_DEBUG(dbgs() << "Non exact access but initial value that is not "
"null or undef, abort!\n");
return false;
}
NewCopies.insert(InitialValue);
if (PotentialValueOrigins)
NewCopyOrigins.insert(nullptr);
}
PIs.push_back(PI);
return true;
};
const auto *AAUO = A.getAAFor<AAUnderlyingObjects>(
QueryingAA, IRPosition::value(Ptr), DepClassTy::OPTIONAL);
if (!AAUO || !AAUO->forallUnderlyingObjects(Pred)) {
LLVM_DEBUG(
dbgs() << "Underlying objects stored into could not be determined\n";);
return false;
}
// Only if we were successful collection all potential copies we record
// dependences (on non-fix AAPointerInfo AAs). We also only then modify the
// given PotentialCopies container.
for (const auto *PI : PIs) {
if (!PI->getState().isAtFixpoint())
UsedAssumedInformation = true;
A.recordDependence(*PI, QueryingAA, DepClassTy::OPTIONAL);
}
PotentialCopies.insert(NewCopies.begin(), NewCopies.end());
if (PotentialValueOrigins)
PotentialValueOrigins->insert(NewCopyOrigins.begin(), NewCopyOrigins.end());
return true;
}
bool AA::getPotentiallyLoadedValues(
Attributor &A, LoadInst &LI, SmallSetVector<Value *, 4> &PotentialValues,
SmallSetVector<Instruction *, 4> &PotentialValueOrigins,
const AbstractAttribute &QueryingAA, bool &UsedAssumedInformation,
bool OnlyExact) {
return getPotentialCopiesOfMemoryValue</* IsLoad */ true>(
A, LI, PotentialValues, &PotentialValueOrigins, QueryingAA,
UsedAssumedInformation, OnlyExact);
}
bool AA::getPotentialCopiesOfStoredValue(
Attributor &A, StoreInst &SI, SmallSetVector<Value *, 4> &PotentialCopies,
const AbstractAttribute &QueryingAA, bool &UsedAssumedInformation,
bool OnlyExact) {
return getPotentialCopiesOfMemoryValue</* IsLoad */ false>(
A, SI, PotentialCopies, nullptr, QueryingAA, UsedAssumedInformation,
OnlyExact);
}
static bool isAssumedReadOnlyOrReadNone(Attributor &A, const IRPosition &IRP,
const AbstractAttribute &QueryingAA,
bool RequireReadNone, bool &IsKnown) {
if (RequireReadNone) {
if (AA::hasAssumedIRAttr<Attribute::ReadNone>(
A, &QueryingAA, IRP, DepClassTy::OPTIONAL, IsKnown,
/* IgnoreSubsumingPositions */ true))
return true;
} else if (AA::hasAssumedIRAttr<Attribute::ReadOnly>(
A, &QueryingAA, IRP, DepClassTy::OPTIONAL, IsKnown,
/* IgnoreSubsumingPositions */ true))
return true;
IRPosition::Kind Kind = IRP.getPositionKind();
if (Kind == IRPosition::IRP_FUNCTION || Kind == IRPosition::IRP_CALL_SITE) {
const auto *MemLocAA =
A.getAAFor<AAMemoryLocation>(QueryingAA, IRP, DepClassTy::NONE);
if (MemLocAA && MemLocAA->isAssumedReadNone()) {
IsKnown = MemLocAA->isKnownReadNone();
if (!IsKnown)
A.recordDependence(*MemLocAA, QueryingAA, DepClassTy::OPTIONAL);
return true;
}
}
const auto *MemBehaviorAA =
A.getAAFor<AAMemoryBehavior>(QueryingAA, IRP, DepClassTy::NONE);
if (MemBehaviorAA &&
(MemBehaviorAA->isAssumedReadNone() ||
(!RequireReadNone && MemBehaviorAA->isAssumedReadOnly()))) {
IsKnown = RequireReadNone ? MemBehaviorAA->isKnownReadNone()
: MemBehaviorAA->isKnownReadOnly();
if (!IsKnown)
A.recordDependence(*MemBehaviorAA, QueryingAA, DepClassTy::OPTIONAL);
return true;
}
return false;
}
bool AA::isAssumedReadOnly(Attributor &A, const IRPosition &IRP,
const AbstractAttribute &QueryingAA, bool &IsKnown) {
return isAssumedReadOnlyOrReadNone(A, IRP, QueryingAA,
/* RequireReadNone */ false, IsKnown);
}
bool AA::isAssumedReadNone(Attributor &A, const IRPosition &IRP,
const AbstractAttribute &QueryingAA, bool &IsKnown) {
return isAssumedReadOnlyOrReadNone(A, IRP, QueryingAA,
/* RequireReadNone */ true, IsKnown);
}
static bool
isPotentiallyReachable(Attributor &A, const Instruction &FromI,
const Instruction *ToI, const Function &ToFn,
const AbstractAttribute &QueryingAA,
const AA::InstExclusionSetTy *ExclusionSet,
std::function<bool(const Function &F)> GoBackwardsCB) {
DEBUG_WITH_TYPE(VERBOSE_DEBUG_TYPE, {
dbgs() << "[AA] isPotentiallyReachable @" << ToFn.getName() << " from "
<< FromI << " [GBCB: " << bool(GoBackwardsCB) << "][#ExS: "
<< (ExclusionSet ? std::to_string(ExclusionSet->size()) : "none")
<< "]\n";
if (ExclusionSet)
for (auto *ES : *ExclusionSet)
dbgs() << *ES << "\n";
});
// We know kernels (generally) cannot be called from within the module. Thus,
// for reachability we would need to step back from a kernel which would allow
// us to reach anything anyway. Even if a kernel is invoked from another
// kernel, values like allocas and shared memory are not accessible. We
// implicitly check for this situation to avoid costly lookups.
if (GoBackwardsCB && &ToFn != FromI.getFunction() &&
!GoBackwardsCB(*FromI.getFunction()) && ToFn.hasFnAttribute("kernel") &&
FromI.getFunction()->hasFnAttribute("kernel")) {
LLVM_DEBUG(dbgs() << "[AA] assume kernel cannot be reached from within the "
"module; success\n";);
return false;
}
// If we can go arbitrarily backwards we will eventually reach an entry point
// that can reach ToI. Only if a set of blocks through which we cannot go is
// provided, or once we track internal functions not accessible from the
// outside, it makes sense to perform backwards analysis in the absence of a
// GoBackwardsCB.
if (!GoBackwardsCB && !ExclusionSet) {
LLVM_DEBUG(dbgs() << "[AA] check @" << ToFn.getName() << " from " << FromI
<< " is not checked backwards and does not have an "
"exclusion set, abort\n");
return true;
}
SmallPtrSet<const Instruction *, 8> Visited;
SmallVector<const Instruction *> Worklist;
Worklist.push_back(&FromI);
while (!Worklist.empty()) {
const Instruction *CurFromI = Worklist.pop_back_val();
if (!Visited.insert(CurFromI).second)
continue;
const Function *FromFn = CurFromI->getFunction();
if (FromFn == &ToFn) {
if (!ToI)
return true;
LLVM_DEBUG(dbgs() << "[AA] check " << *ToI << " from " << *CurFromI
<< " intraprocedurally\n");
const auto *ReachabilityAA = A.getAAFor<AAIntraFnReachability>(
QueryingAA, IRPosition::function(ToFn), DepClassTy::OPTIONAL);
bool Result = !ReachabilityAA || ReachabilityAA->isAssumedReachable(
A, *CurFromI, *ToI, ExclusionSet);
LLVM_DEBUG(dbgs() << "[AA] " << *CurFromI << " "
<< (Result ? "can potentially " : "cannot ") << "reach "
<< *ToI << " [Intra]\n");
if (Result)
return true;
}
bool Result = true;
if (!ToFn.isDeclaration() && ToI) {
const auto *ToReachabilityAA = A.getAAFor<AAIntraFnReachability>(
QueryingAA, IRPosition::function(ToFn), DepClassTy::OPTIONAL);
const Instruction &EntryI = ToFn.getEntryBlock().front();
Result = !ToReachabilityAA || ToReachabilityAA->isAssumedReachable(
A, EntryI, *ToI, ExclusionSet);
LLVM_DEBUG(dbgs() << "[AA] Entry " << EntryI << " of @" << ToFn.getName()
<< " " << (Result ? "can potentially " : "cannot ")
<< "reach @" << *ToI << " [ToFn]\n");
}
if (Result) {
// The entry of the ToFn can reach the instruction ToI. If the current
// instruction is already known to reach the ToFn.
const auto *FnReachabilityAA = A.getAAFor<AAInterFnReachability>(
QueryingAA, IRPosition::function(*FromFn), DepClassTy::OPTIONAL);
Result = !FnReachabilityAA || FnReachabilityAA->instructionCanReach(
A, *CurFromI, ToFn, ExclusionSet);
LLVM_DEBUG(dbgs() << "[AA] " << *CurFromI << " in @" << FromFn->getName()
<< " " << (Result ? "can potentially " : "cannot ")
<< "reach @" << ToFn.getName() << " [FromFn]\n");
if (Result)
return true;
}
// TODO: Check assumed nounwind.
const auto *ReachabilityAA = A.getAAFor<AAIntraFnReachability>(
QueryingAA, IRPosition::function(*FromFn), DepClassTy::OPTIONAL);
auto ReturnInstCB = [&](Instruction &Ret) {
bool Result = !ReachabilityAA || ReachabilityAA->isAssumedReachable(
A, *CurFromI, Ret, ExclusionSet);
LLVM_DEBUG(dbgs() << "[AA][Ret] " << *CurFromI << " "
<< (Result ? "can potentially " : "cannot ") << "reach "
<< Ret << " [Intra]\n");
return !Result;
};
// Check if we can reach returns.
bool UsedAssumedInformation = false;
if (A.checkForAllInstructions(ReturnInstCB, FromFn, &QueryingAA,
{Instruction::Ret}, UsedAssumedInformation)) {
LLVM_DEBUG(dbgs() << "[AA] No return is reachable, done\n");
continue;
}
if (!GoBackwardsCB) {
LLVM_DEBUG(dbgs() << "[AA] check @" << ToFn.getName() << " from " << FromI
<< " is not checked backwards, abort\n");
return true;
}
// If we do not go backwards from the FromFn we are done here and so far we
// could not find a way to reach ToFn/ToI.
if (!GoBackwardsCB(*FromFn))
continue;
LLVM_DEBUG(dbgs() << "Stepping backwards to the call sites of @"
<< FromFn->getName() << "\n");
auto CheckCallSite = [&](AbstractCallSite ACS) {
CallBase *CB = ACS.getInstruction();
if (!CB)
return false;
if (isa<InvokeInst>(CB))
return false;
Instruction *Inst = CB->getNextNonDebugInstruction();
Worklist.push_back(Inst);
return true;
};
Result = !A.checkForAllCallSites(CheckCallSite, *FromFn,
/* RequireAllCallSites */ true,
&QueryingAA, UsedAssumedInformation);
if (Result) {
LLVM_DEBUG(dbgs() << "[AA] stepping back to call sites from " << *CurFromI
<< " in @" << FromFn->getName()
<< " failed, give up\n");
return true;
}
LLVM_DEBUG(dbgs() << "[AA] stepped back to call sites from " << *CurFromI
<< " in @" << FromFn->getName()
<< " worklist size is: " << Worklist.size() << "\n");
}
return false;
}
bool AA::isPotentiallyReachable(
Attributor &A, const Instruction &FromI, const Instruction &ToI,
const AbstractAttribute &QueryingAA,
const AA::InstExclusionSetTy *ExclusionSet,
std::function<bool(const Function &F)> GoBackwardsCB) {
const Function *ToFn = ToI.getFunction();
return ::isPotentiallyReachable(A, FromI, &ToI, *ToFn, QueryingAA,
ExclusionSet, GoBackwardsCB);
}
bool AA::isPotentiallyReachable(
Attributor &A, const Instruction &FromI, const Function &ToFn,
const AbstractAttribute &QueryingAA,
const AA::InstExclusionSetTy *ExclusionSet,
std::function<bool(const Function &F)> GoBackwardsCB) {
return ::isPotentiallyReachable(A, FromI, /* ToI */ nullptr, ToFn, QueryingAA,
ExclusionSet, GoBackwardsCB);
}
bool AA::isAssumedThreadLocalObject(Attributor &A, Value &Obj,
const AbstractAttribute &QueryingAA) {
if (isa<UndefValue>(Obj))
return true;
if (isa<AllocaInst>(Obj)) {
InformationCache &InfoCache = A.getInfoCache();
if (!InfoCache.stackIsAccessibleByOtherThreads()) {
LLVM_DEBUG(
dbgs() << "[AA] Object '" << Obj
<< "' is thread local; stack objects are thread local.\n");
return true;
}
bool IsKnownNoCapture;
bool IsAssumedNoCapture = AA::hasAssumedIRAttr<Attribute::NoCapture>(
A, &QueryingAA, IRPosition::value(Obj), DepClassTy::OPTIONAL,
IsKnownNoCapture);
LLVM_DEBUG(dbgs() << "[AA] Object '" << Obj << "' is "
<< (IsAssumedNoCapture ? "" : "not") << " thread local; "
<< (IsAssumedNoCapture ? "non-" : "")
<< "captured stack object.\n");
return IsAssumedNoCapture;
}
if (auto *GV = dyn_cast<GlobalVariable>(&Obj)) {
if (GV->isConstant()) {
LLVM_DEBUG(dbgs() << "[AA] Object '" << Obj
<< "' is thread local; constant global\n");
return true;
}
if (GV->isThreadLocal()) {
LLVM_DEBUG(dbgs() << "[AA] Object '" << Obj
<< "' is thread local; thread local global\n");
return true;
}
}
if (A.getInfoCache().targetIsGPU()) {
if (Obj.getType()->getPointerAddressSpace() ==
(int)AA::GPUAddressSpace::Local) {
LLVM_DEBUG(dbgs() << "[AA] Object '" << Obj
<< "' is thread local; GPU local memory\n");
return true;
}
if (Obj.getType()->getPointerAddressSpace() ==
(int)AA::GPUAddressSpace::Constant) {
LLVM_DEBUG(dbgs() << "[AA] Object '" << Obj
<< "' is thread local; GPU constant memory\n");
return true;
}
}
LLVM_DEBUG(dbgs() << "[AA] Object '" << Obj << "' is not thread local\n");
return false;
}
bool AA::isPotentiallyAffectedByBarrier(Attributor &A, const Instruction &I,
const AbstractAttribute &QueryingAA) {
if (!I.mayHaveSideEffects() && !I.mayReadFromMemory())
return false;
SmallSetVector<const Value *, 8> Ptrs;
auto AddLocationPtr = [&](std::optional<MemoryLocation> Loc) {
if (!Loc || !Loc->Ptr) {
LLVM_DEBUG(
dbgs() << "[AA] Access to unknown location; -> requires barriers\n");
return false;
}
Ptrs.insert(Loc->Ptr);
return true;
};
if (const MemIntrinsic *MI = dyn_cast<MemIntrinsic>(&I)) {
if (!AddLocationPtr(MemoryLocation::getForDest(MI)))
return true;
if (const MemTransferInst *MTI = dyn_cast<MemTransferInst>(&I))
if (!AddLocationPtr(MemoryLocation::getForSource(MTI)))
return true;
} else if (!AddLocationPtr(MemoryLocation::getOrNone(&I)))
return true;
return isPotentiallyAffectedByBarrier(A, Ptrs.getArrayRef(), QueryingAA, &I);
}
bool AA::isPotentiallyAffectedByBarrier(Attributor &A,
ArrayRef<const Value *> Ptrs,
const AbstractAttribute &QueryingAA,
const Instruction *CtxI) {
for (const Value *Ptr : Ptrs) {
if (!Ptr) {
LLVM_DEBUG(dbgs() << "[AA] nullptr; -> requires barriers\n");
return true;
}
auto Pred = [&](Value &Obj) {
if (AA::isAssumedThreadLocalObject(A, Obj, QueryingAA))
return true;
LLVM_DEBUG(dbgs() << "[AA] Access to '" << Obj << "' via '" << *Ptr
<< "'; -> requires barrier\n");
return false;
};
const auto *UnderlyingObjsAA = A.getAAFor<AAUnderlyingObjects>(
QueryingAA, IRPosition::value(*Ptr), DepClassTy::OPTIONAL);
if (!UnderlyingObjsAA || !UnderlyingObjsAA->forallUnderlyingObjects(Pred))
return true;
}
return false;
}
/// Return true if \p New is equal or worse than \p Old.
static bool isEqualOrWorse(const Attribute &New, const Attribute &Old) {
if (!Old.isIntAttribute())
return true;
return Old.getValueAsInt() >= New.getValueAsInt();
}
/// Return true if the information provided by \p Attr was added to the
/// attribute set \p AttrSet. This is only the case if it was not already
/// present in \p AttrSet.
static bool addIfNotExistent(LLVMContext &Ctx, const Attribute &Attr,
AttributeSet AttrSet, bool ForceReplace,
AttrBuilder &AB) {
if (Attr.isEnumAttribute()) {
Attribute::AttrKind Kind = Attr.getKindAsEnum();
if (AttrSet.hasAttribute(Kind))
return false;
AB.addAttribute(Kind);
return true;
}
if (Attr.isStringAttribute()) {
StringRef Kind = Attr.getKindAsString();
if (AttrSet.hasAttribute(Kind)) {
if (!ForceReplace)
return false;
}
AB.addAttribute(Kind, Attr.getValueAsString());
return true;
}
if (Attr.isIntAttribute()) {
Attribute::AttrKind Kind = Attr.getKindAsEnum();
if (!ForceReplace && Kind == Attribute::Memory) {
MemoryEffects ME = Attr.getMemoryEffects() & AttrSet.getMemoryEffects();
if (ME == AttrSet.getMemoryEffects())
return false;
AB.addMemoryAttr(ME);
return true;
}
if (AttrSet.hasAttribute(Kind)) {
if (!ForceReplace && isEqualOrWorse(Attr, AttrSet.getAttribute(Kind)))
return false;
}
AB.addAttribute(Attr);
return true;
}
llvm_unreachable("Expected enum or string attribute!");
}
Argument *IRPosition::getAssociatedArgument() const {
if (getPositionKind() == IRP_ARGUMENT)
return cast<Argument>(&getAnchorValue());
// Not an Argument and no argument number means this is not a call site
// argument, thus we cannot find a callback argument to return.
int ArgNo = getCallSiteArgNo();
if (ArgNo < 0)
return nullptr;
// Use abstract call sites to make the connection between the call site
// values and the ones in callbacks. If a callback was found that makes use
// of the underlying call site operand, we want the corresponding callback
// callee argument and not the direct callee argument.
std::optional<Argument *> CBCandidateArg;
SmallVector<const Use *, 4> CallbackUses;
const auto &CB = cast<CallBase>(getAnchorValue());
AbstractCallSite::getCallbackUses(CB, CallbackUses);
for (const Use *U : CallbackUses) {
AbstractCallSite ACS(U);
assert(ACS && ACS.isCallbackCall());
if (!ACS.getCalledFunction())
continue;
for (unsigned u = 0, e = ACS.getNumArgOperands(); u < e; u++) {
// Test if the underlying call site operand is argument number u of the
// callback callee.
if (ACS.getCallArgOperandNo(u) != ArgNo)
continue;
assert(ACS.getCalledFunction()->arg_size() > u &&
"ACS mapped into var-args arguments!");
if (CBCandidateArg) {
CBCandidateArg = nullptr;
break;
}
CBCandidateArg = ACS.getCalledFunction()->getArg(u);
}
}
// If we found a unique callback candidate argument, return it.
if (CBCandidateArg && *CBCandidateArg)
return *CBCandidateArg;
// If no callbacks were found, or none used the underlying call site operand
// exclusively, use the direct callee argument if available.
auto *Callee = dyn_cast_if_present<Function>(CB.getCalledOperand());
if (Callee && Callee->arg_size() > unsigned(ArgNo))
return Callee->getArg(ArgNo);
return nullptr;
}
ChangeStatus AbstractAttribute::update(Attributor &A) {
ChangeStatus HasChanged = ChangeStatus::UNCHANGED;
if (getState().isAtFixpoint())
return HasChanged;
LLVM_DEBUG(dbgs() << "[Attributor] Update: " << *this << "\n");
HasChanged = updateImpl(A);
LLVM_DEBUG(dbgs() << "[Attributor] Update " << HasChanged << " " << *this
<< "\n");
return HasChanged;
}
Attributor::Attributor(SetVector<Function *> &Functions,
InformationCache &InfoCache,
AttributorConfig Configuration)
: Allocator(InfoCache.Allocator), Functions(Functions),
InfoCache(InfoCache), Configuration(Configuration) {
if (!isClosedWorldModule())
return;
for (Function *Fn : Functions)
if (Fn->hasAddressTaken(/*PutOffender=*/nullptr,
/*IgnoreCallbackUses=*/false,
/*IgnoreAssumeLikeCalls=*/true,
/*IgnoreLLVMUsed=*/true,
/*IgnoreARCAttachedCall=*/false,
/*IgnoreCastedDirectCall=*/true))
InfoCache.IndirectlyCallableFunctions.push_back(Fn);
}
bool Attributor::getAttrsFromAssumes(const IRPosition &IRP,
Attribute::AttrKind AK,
SmallVectorImpl<Attribute> &Attrs) {
assert(IRP.getPositionKind() != IRPosition::IRP_INVALID &&
"Did expect a valid position!");
MustBeExecutedContextExplorer *Explorer =
getInfoCache().getMustBeExecutedContextExplorer();
if (!Explorer)
return false;
Value &AssociatedValue = IRP.getAssociatedValue();
const Assume2KnowledgeMap &A2K =
getInfoCache().getKnowledgeMap().lookup({&AssociatedValue, AK});
// Check if we found any potential assume use, if not we don't need to create
// explorer iterators.
if (A2K.empty())
return false;
LLVMContext &Ctx = AssociatedValue.getContext();
unsigned AttrsSize = Attrs.size();
auto EIt = Explorer->begin(IRP.getCtxI()),
EEnd = Explorer->end(IRP.getCtxI());
for (const auto &It : A2K)
if (Explorer->findInContextOf(It.first, EIt, EEnd))
Attrs.push_back(Attribute::get(Ctx, AK, It.second.Max));
return AttrsSize != Attrs.size();
}
template <typename DescTy>
ChangeStatus
Attributor::updateAttrMap(const IRPosition &IRP, ArrayRef<DescTy> AttrDescs,
function_ref<bool(const DescTy &, AttributeSet,
AttributeMask &, AttrBuilder &)>
CB) {
if (AttrDescs.empty())
return ChangeStatus::UNCHANGED;
switch (IRP.getPositionKind()) {
case IRPosition::IRP_FLOAT:
case IRPosition::IRP_INVALID:
return ChangeStatus::UNCHANGED;
default:
break;
};
AttributeList AL;
Value *AttrListAnchor = IRP.getAttrListAnchor();
auto It = AttrsMap.find(AttrListAnchor);
if (It == AttrsMap.end())
AL = IRP.getAttrList();
else
AL = It->getSecond();
LLVMContext &Ctx = IRP.getAnchorValue().getContext();
auto AttrIdx = IRP.getAttrIdx();
AttributeSet AS = AL.getAttributes(AttrIdx);
AttributeMask AM;
AttrBuilder AB(Ctx);
ChangeStatus HasChanged = ChangeStatus::UNCHANGED;
for (const DescTy &AttrDesc : AttrDescs)
if (CB(AttrDesc, AS, AM, AB))
HasChanged = ChangeStatus::CHANGED;
if (HasChanged == ChangeStatus::UNCHANGED)
return ChangeStatus::UNCHANGED;
AL = AL.removeAttributesAtIndex(Ctx, AttrIdx, AM);
AL = AL.addAttributesAtIndex(Ctx, AttrIdx, AB);
AttrsMap[AttrListAnchor] = AL;
return ChangeStatus::CHANGED;
}
bool Attributor::hasAttr(const IRPosition &IRP,
ArrayRef<Attribute::AttrKind> AttrKinds,
bool IgnoreSubsumingPositions,
Attribute::AttrKind ImpliedAttributeKind) {
bool Implied = false;
bool HasAttr = false;
auto HasAttrCB = [&](const Attribute::AttrKind &Kind, AttributeSet AttrSet,
AttributeMask &, AttrBuilder &) {
if (AttrSet.hasAttribute(Kind)) {
Implied |= Kind != ImpliedAttributeKind;
HasAttr = true;
}
return false;
};
for (const IRPosition &EquivIRP : SubsumingPositionIterator(IRP)) {
updateAttrMap<Attribute::AttrKind>(EquivIRP, AttrKinds, HasAttrCB);
if (HasAttr)
break;
// The first position returned by the SubsumingPositionIterator is
// always the position itself. If we ignore subsuming positions we
// are done after the first iteration.
if (IgnoreSubsumingPositions)
break;
Implied = true;
}
if (!HasAttr) {
Implied = true;
SmallVector<Attribute> Attrs;
for (Attribute::AttrKind AK : AttrKinds)
if (getAttrsFromAssumes(IRP, AK, Attrs)) {
HasAttr = true;
break;
}
}
// Check if we should manifest the implied attribute kind at the IRP.
if (ImpliedAttributeKind != Attribute::None && HasAttr && Implied)
manifestAttrs(IRP, {Attribute::get(IRP.getAnchorValue().getContext(),
ImpliedAttributeKind)});
return HasAttr;
}
void Attributor::getAttrs(const IRPosition &IRP,
ArrayRef<Attribute::AttrKind> AttrKinds,
SmallVectorImpl<Attribute> &Attrs,
bool IgnoreSubsumingPositions) {
auto CollectAttrCB = [&](const Attribute::AttrKind &Kind,
AttributeSet AttrSet, AttributeMask &,
AttrBuilder &) {
if (AttrSet.hasAttribute(Kind))
Attrs.push_back(AttrSet.getAttribute(Kind));
return false;
};
for (const IRPosition &EquivIRP : SubsumingPositionIterator(IRP)) {
updateAttrMap<Attribute::AttrKind>(EquivIRP, AttrKinds, CollectAttrCB);
// The first position returned by the SubsumingPositionIterator is
// always the position itself. If we ignore subsuming positions we
// are done after the first iteration.
if (IgnoreSubsumingPositions)
break;
}
for (Attribute::AttrKind AK : AttrKinds)
getAttrsFromAssumes(IRP, AK, Attrs);
}
ChangeStatus Attributor::removeAttrs(const IRPosition &IRP,
ArrayRef<Attribute::AttrKind> AttrKinds) {
auto RemoveAttrCB = [&](const Attribute::AttrKind &Kind, AttributeSet AttrSet,
AttributeMask &AM, AttrBuilder &) {
if (!AttrSet.hasAttribute(Kind))
return false;
AM.addAttribute(Kind);
return true;
};
return updateAttrMap<Attribute::AttrKind>(IRP, AttrKinds, RemoveAttrCB);
}
ChangeStatus Attributor::removeAttrs(const IRPosition &IRP,
ArrayRef<StringRef> Attrs) {
auto RemoveAttrCB = [&](StringRef Attr, AttributeSet AttrSet,
AttributeMask &AM, AttrBuilder &) -> bool {
if (!AttrSet.hasAttribute(Attr))
return false;
AM.addAttribute(Attr);
return true;
};
return updateAttrMap<StringRef>(IRP, Attrs, RemoveAttrCB);
}
ChangeStatus Attributor::manifestAttrs(const IRPosition &IRP,
ArrayRef<Attribute> Attrs,
bool ForceReplace) {
LLVMContext &Ctx = IRP.getAnchorValue().getContext();
auto AddAttrCB = [&](const Attribute &Attr, AttributeSet AttrSet,
AttributeMask &, AttrBuilder &AB) {
return addIfNotExistent(Ctx, Attr, AttrSet, ForceReplace, AB);
};
return updateAttrMap<Attribute>(IRP, Attrs, AddAttrCB);
}
const IRPosition IRPosition::EmptyKey(DenseMapInfo<void *>::getEmptyKey());
const IRPosition
IRPosition::TombstoneKey(DenseMapInfo<void *>::getTombstoneKey());
SubsumingPositionIterator::SubsumingPositionIterator(const IRPosition &IRP) {
IRPositions.emplace_back(IRP);
// Helper to determine if operand bundles on a call site are benign or
// potentially problematic. We handle only llvm.assume for now.
auto CanIgnoreOperandBundles = [](const CallBase &CB) {
return (isa<IntrinsicInst>(CB) &&
cast<IntrinsicInst>(CB).getIntrinsicID() == Intrinsic ::assume);
};
const auto *CB = dyn_cast<CallBase>(&IRP.getAnchorValue());
switch (IRP.getPositionKind()) {
case IRPosition::IRP_INVALID:
case IRPosition::IRP_FLOAT:
case IRPosition::IRP_FUNCTION:
return;
case IRPosition::IRP_ARGUMENT:
case IRPosition::IRP_RETURNED:
IRPositions.emplace_back(IRPosition::function(*IRP.getAnchorScope()));
return;
case IRPosition::IRP_CALL_SITE:
assert(CB && "Expected call site!");
// TODO: We need to look at the operand bundles similar to the redirection
// in CallBase.
if (!CB->hasOperandBundles() || CanIgnoreOperandBundles(*CB))
if (auto *Callee = dyn_cast_if_present<Function>(CB->getCalledOperand()))
IRPositions.emplace_back(IRPosition::function(*Callee));
return;
case IRPosition::IRP_CALL_SITE_RETURNED:
assert(CB && "Expected call site!");
// TODO: We need to look at the operand bundles similar to the redirection
// in CallBase.
if (!CB->hasOperandBundles() || CanIgnoreOperandBundles(*CB)) {
if (auto *Callee =
dyn_cast_if_present<Function>(CB->getCalledOperand())) {
IRPositions.emplace_back(IRPosition::returned(*Callee));
IRPositions.emplace_back(IRPosition::function(*Callee));
for (const Argument &Arg : Callee->args())
if (Arg.hasReturnedAttr()) {
IRPositions.emplace_back(
IRPosition::callsite_argument(*CB, Arg.getArgNo()));
IRPositions.emplace_back(
IRPosition::value(*CB->getArgOperand(Arg.getArgNo())));
IRPositions.emplace_back(IRPosition::argument(Arg));
}
}
}
IRPositions.emplace_back(IRPosition::callsite_function(*CB));
return;
case IRPosition::IRP_CALL_SITE_ARGUMENT: {
assert(CB && "Expected call site!");
// TODO: We need to look at the operand bundles similar to the redirection
// in CallBase.
if (!CB->hasOperandBundles() || CanIgnoreOperandBundles(*CB)) {
auto *Callee = dyn_cast_if_present<Function>(CB->getCalledOperand());
if (Callee) {
if (Argument *Arg = IRP.getAssociatedArgument())
IRPositions.emplace_back(IRPosition::argument(*Arg));
IRPositions.emplace_back(IRPosition::function(*Callee));
}
}
IRPositions.emplace_back(IRPosition::value(IRP.getAssociatedValue()));
return;
}
}
}
void IRPosition::verify() {
#ifdef EXPENSIVE_CHECKS
switch (getPositionKind()) {
case IRP_INVALID:
assert((CBContext == nullptr) &&
"Invalid position must not have CallBaseContext!");
assert(!Enc.getOpaqueValue() &&
"Expected a nullptr for an invalid position!");
return;
case IRP_FLOAT:
assert((!isa<Argument>(&getAssociatedValue())) &&
"Expected specialized kind for argument values!");
return;
case IRP_RETURNED:
assert(isa<Function>(getAsValuePtr()) &&
"Expected function for a 'returned' position!");
assert(getAsValuePtr() == &getAssociatedValue() &&
"Associated value mismatch!");
return;
case IRP_CALL_SITE_RETURNED:
assert((CBContext == nullptr) &&
"'call site returned' position must not have CallBaseContext!");
assert((isa<CallBase>(getAsValuePtr())) &&
"Expected call base for 'call site returned' position!");
assert(getAsValuePtr() == &getAssociatedValue() &&
"Associated value mismatch!");
return;
case IRP_CALL_SITE:
assert((CBContext == nullptr) &&
"'call site function' position must not have CallBaseContext!");
assert((isa<CallBase>(getAsValuePtr())) &&
"Expected call base for 'call site function' position!");
assert(getAsValuePtr() == &getAssociatedValue() &&
"Associated value mismatch!");
return;
case IRP_FUNCTION:
assert(isa<Function>(getAsValuePtr()) &&
"Expected function for a 'function' position!");
assert(getAsValuePtr() == &getAssociatedValue() &&
"Associated value mismatch!");
return;
case IRP_ARGUMENT:
assert(isa<Argument>(getAsValuePtr()) &&
"Expected argument for a 'argument' position!");
assert(getAsValuePtr() == &getAssociatedValue() &&
"Associated value mismatch!");
return;
case IRP_CALL_SITE_ARGUMENT: {
assert((CBContext == nullptr) &&
"'call site argument' position must not have CallBaseContext!");
Use *U = getAsUsePtr();
(void)U; // Silence unused variable warning.
assert(U && "Expected use for a 'call site argument' position!");
assert(isa<CallBase>(U->getUser()) &&
"Expected call base user for a 'call site argument' position!");
assert(cast<CallBase>(U->getUser())->isArgOperand(U) &&
"Expected call base argument operand for a 'call site argument' "
"position");
assert(cast<CallBase>(U->getUser())->getArgOperandNo(U) ==
unsigned(getCallSiteArgNo()) &&
"Argument number mismatch!");
assert(U->get() == &getAssociatedValue() && "Associated value mismatch!");
return;
}
}
#endif
}
std::optional<Constant *>
Attributor::getAssumedConstant(const IRPosition &IRP,
const AbstractAttribute &AA,
bool &UsedAssumedInformation) {
// First check all callbacks provided by outside AAs. If any of them returns
// a non-null value that is different from the associated value, or
// std::nullopt, we assume it's simplified.
for (auto &CB : SimplificationCallbacks.lookup(IRP)) {
std::optional<Value *> SimplifiedV = CB(IRP, &AA, UsedAssumedInformation);
if (!SimplifiedV)
return std::nullopt;
if (isa_and_nonnull<Constant>(*SimplifiedV))
return cast<Constant>(*SimplifiedV);
return nullptr;
}
if (auto *C = dyn_cast<Constant>(&IRP.getAssociatedValue()))
return C;
SmallVector<AA::ValueAndContext> Values;
if (getAssumedSimplifiedValues(IRP, &AA, Values,
AA::ValueScope::Interprocedural,
UsedAssumedInformation)) {
if (Values.empty())
return std::nullopt;
if (auto *C = dyn_cast_or_null<Constant>(
AAPotentialValues::getSingleValue(*this, AA, IRP, Values)))
return C;
}
return nullptr;
}
std::optional<Value *> Attributor::getAssumedSimplified(
const IRPosition &IRP, const AbstractAttribute *AA,
bool &UsedAssumedInformation, AA::ValueScope S) {
// First check all callbacks provided by outside AAs. If any of them returns
// a non-null value that is different from the associated value, or
// std::nullopt, we assume it's simplified.
for (auto &CB : SimplificationCallbacks.lookup(IRP))
return CB(IRP, AA, UsedAssumedInformation);
SmallVector<AA::ValueAndContext> Values;
if (!getAssumedSimplifiedValues(IRP, AA, Values, S, UsedAssumedInformation))
return &IRP.getAssociatedValue();
if (Values.empty())
return std::nullopt;
if (AA)
if (Value *V = AAPotentialValues::getSingleValue(*this, *AA, IRP, Values))
return V;
if (IRP.getPositionKind() == IRPosition::IRP_RETURNED ||
IRP.getPositionKind() == IRPosition::IRP_CALL_SITE_RETURNED)
return nullptr;
return &IRP.getAssociatedValue();
}
bool Attributor::getAssumedSimplifiedValues(
const IRPosition &InitialIRP, const AbstractAttribute *AA,
SmallVectorImpl<AA::ValueAndContext> &Values, AA::ValueScope S,
bool &UsedAssumedInformation, bool RecurseForSelectAndPHI) {
SmallPtrSet<Value *, 8> Seen;
SmallVector<IRPosition, 8> Worklist;
Worklist.push_back(InitialIRP);
while (!Worklist.empty()) {
const IRPosition &IRP = Worklist.pop_back_val();
// First check all callbacks provided by outside AAs. If any of them returns
// a non-null value that is different from the associated value, or
// std::nullopt, we assume it's simplified.
int NV = Values.size();
const auto &SimplificationCBs = SimplificationCallbacks.lookup(IRP);
for (const auto &CB : SimplificationCBs) {
std::optional<Value *> CBResult = CB(IRP, AA, UsedAssumedInformation);
if (!CBResult.has_value())
continue;
Value *V = *CBResult;
if (!V)
return false;
if ((S & AA::ValueScope::Interprocedural) ||
AA::isValidInScope(*V, IRP.getAnchorScope()))
Values.push_back(AA::ValueAndContext{*V, nullptr});
else
return false;
}
if (SimplificationCBs.empty()) {
// If no high-level/outside simplification occurred, use
// AAPotentialValues.
const auto *PotentialValuesAA =
getOrCreateAAFor<AAPotentialValues>(IRP, AA, DepClassTy::OPTIONAL);
if (PotentialValuesAA && PotentialValuesAA->getAssumedSimplifiedValues(*this, Values, S)) {
UsedAssumedInformation |= !PotentialValuesAA->isAtFixpoint();
} else if (IRP.getPositionKind() != IRPosition::IRP_RETURNED) {
Values.push_back({IRP.getAssociatedValue(), IRP.getCtxI()});
} else {
// TODO: We could visit all returns and add the operands.
return false;
}
}
if (!RecurseForSelectAndPHI)
break;
for (int I = NV, E = Values.size(); I < E; ++I) {
Value *V = Values[I].getValue();
if (!isa<PHINode>(V) && !isa<SelectInst>(V))
continue;
if (!Seen.insert(V).second)
continue;
// Move the last element to this slot.
Values[I] = Values[E - 1];
// Eliminate the last slot, adjust the indices.
Values.pop_back();
--E;
--I;
// Add a new value (select or phi) to the worklist.
Worklist.push_back(IRPosition::value(*V));
}
}
return true;
}
std::optional<Value *> Attributor::translateArgumentToCallSiteContent(
std::optional<Value *> V, CallBase &CB, const AbstractAttribute &AA,
bool &UsedAssumedInformation) {
if (!V)
return V;
if (*V == nullptr || isa<Constant>(*V))
return V;
if (auto *Arg = dyn_cast<Argument>(*V))
if (CB.getCalledOperand() == Arg->getParent() &&
CB.arg_size() > Arg->getArgNo())
if (!Arg->hasPointeeInMemoryValueAttr())
return getAssumedSimplified(
IRPosition::callsite_argument(CB, Arg->getArgNo()), AA,
UsedAssumedInformation, AA::Intraprocedural);
return nullptr;
}
Attributor::~Attributor() {
// The abstract attributes are allocated via the BumpPtrAllocator Allocator,
// thus we cannot delete them. We can, and want to, destruct them though.
for (auto &It : AAMap) {
AbstractAttribute *AA = It.getSecond();
AA->~AbstractAttribute();
}
}
bool Attributor::isAssumedDead(const AbstractAttribute &AA,
const AAIsDead *FnLivenessAA,
bool &UsedAssumedInformation,
bool CheckBBLivenessOnly, DepClassTy DepClass) {
if (!Configuration.UseLiveness)
return false;
const IRPosition &IRP = AA.getIRPosition();
if (!Functions.count(IRP.getAnchorScope()))
return false;
return isAssumedDead(IRP, &AA, FnLivenessAA, UsedAssumedInformation,
CheckBBLivenessOnly, DepClass);
}
bool Attributor::isAssumedDead(const Use &U,
const AbstractAttribute *QueryingAA,
const AAIsDead *FnLivenessAA,
bool &UsedAssumedInformation,
bool CheckBBLivenessOnly, DepClassTy DepClass) {
if (!Configuration.UseLiveness)
return false;
Instruction *UserI = dyn_cast<Instruction>(U.getUser());
if (!UserI)
return isAssumedDead(IRPosition::value(*U.get()), QueryingAA, FnLivenessAA,
UsedAssumedInformation, CheckBBLivenessOnly, DepClass);
if (auto *CB = dyn_cast<CallBase>(UserI)) {
// For call site argument uses we can check if the argument is
// unused/dead.
if (CB->isArgOperand(&U)) {
const IRPosition &CSArgPos =
IRPosition::callsite_argument(*CB, CB->getArgOperandNo(&U));
return isAssumedDead(CSArgPos, QueryingAA, FnLivenessAA,
UsedAssumedInformation, CheckBBLivenessOnly,
DepClass);
}
} else if (ReturnInst *RI = dyn_cast<ReturnInst>(UserI)) {
const IRPosition &RetPos = IRPosition::returned(*RI->getFunction());
return isAssumedDead(RetPos, QueryingAA, FnLivenessAA,
UsedAssumedInformation, CheckBBLivenessOnly, DepClass);
} else if (PHINode *PHI = dyn_cast<PHINode>(UserI)) {
BasicBlock *IncomingBB = PHI->getIncomingBlock(U);
return isAssumedDead(*IncomingBB->getTerminator(), QueryingAA, FnLivenessAA,
UsedAssumedInformation, CheckBBLivenessOnly, DepClass);
} else if (StoreInst *SI = dyn_cast<StoreInst>(UserI)) {
if (!CheckBBLivenessOnly && SI->getPointerOperand() != U.get()) {
const IRPosition IRP = IRPosition::inst(*SI);
const AAIsDead *IsDeadAA =
getOrCreateAAFor<AAIsDead>(IRP, QueryingAA, DepClassTy::NONE);
if (IsDeadAA && IsDeadAA->isRemovableStore()) {
if (QueryingAA)
recordDependence(*IsDeadAA, *QueryingAA, DepClass);
if (!IsDeadAA->isKnown(AAIsDead::IS_REMOVABLE))
UsedAssumedInformation = true;
return true;
}
}
}
return isAssumedDead(IRPosition::inst(*UserI), QueryingAA, FnLivenessAA,
UsedAssumedInformation, CheckBBLivenessOnly, DepClass);
}
bool Attributor::isAssumedDead(const Instruction &I,
const AbstractAttribute *QueryingAA,
const AAIsDead *FnLivenessAA,
bool &UsedAssumedInformation,
bool CheckBBLivenessOnly, DepClassTy DepClass,
bool CheckForDeadStore) {
if (!Configuration.UseLiveness)
return false;
const IRPosition::CallBaseContext *CBCtx =
QueryingAA ? QueryingAA->getCallBaseContext() : nullptr;
if (ManifestAddedBlocks.contains(I.getParent()))
return false;
const Function &F = *I.getFunction();
if (!FnLivenessAA || FnLivenessAA->getAnchorScope() != &F)
FnLivenessAA = getOrCreateAAFor<AAIsDead>(IRPosition::function(F, CBCtx),
QueryingAA, DepClassTy::NONE);
// Don't use recursive reasoning.
if (!FnLivenessAA || QueryingAA == FnLivenessAA)
return false;
// If we have a context instruction and a liveness AA we use it.
if (CheckBBLivenessOnly ? FnLivenessAA->isAssumedDead(I.getParent())
: FnLivenessAA->isAssumedDead(&I)) {
if (QueryingAA)
recordDependence(*FnLivenessAA, *QueryingAA, DepClass);
if (!FnLivenessAA->isKnownDead(&I))
UsedAssumedInformation = true;
return true;
}
if (CheckBBLivenessOnly)
return false;
const IRPosition IRP = IRPosition::inst(I, CBCtx);
const AAIsDead *IsDeadAA =
getOrCreateAAFor<AAIsDead>(IRP, QueryingAA, DepClassTy::NONE);
// Don't use recursive reasoning.
if (!IsDeadAA || QueryingAA == IsDeadAA)
return false;
if (IsDeadAA->isAssumedDead()) {
if (QueryingAA)
recordDependence(*IsDeadAA, *QueryingAA, DepClass);
if (!IsDeadAA->isKnownDead())
UsedAssumedInformation = true;
return true;
}
if (CheckForDeadStore && isa<StoreInst>(I) && IsDeadAA->isRemovableStore()) {
if (QueryingAA)
recordDependence(*IsDeadAA, *QueryingAA, DepClass);
if (!IsDeadAA->isKnownDead())
UsedAssumedInformation = true;
return true;
}
return false;
}
bool Attributor::isAssumedDead(const IRPosition &IRP,
const AbstractAttribute *QueryingAA,
const AAIsDead *FnLivenessAA,
bool &UsedAssumedInformation,
bool CheckBBLivenessOnly, DepClassTy DepClass) {
if (!Configuration.UseLiveness)
return false;
// Don't check liveness for constants, e.g. functions, used as (floating)
// values since the context instruction and such is here meaningless.
if (IRP.getPositionKind() == IRPosition::IRP_FLOAT &&
isa<Constant>(IRP.getAssociatedValue())) {
return false;
}
Instruction *CtxI = IRP.getCtxI();
if (CtxI &&
isAssumedDead(*CtxI, QueryingAA, FnLivenessAA, UsedAssumedInformation,
/* CheckBBLivenessOnly */ true,
CheckBBLivenessOnly ? DepClass : DepClassTy::OPTIONAL))
return true;
if (CheckBBLivenessOnly)
return false;
// If we haven't succeeded we query the specific liveness info for the IRP.
const AAIsDead *IsDeadAA;
if (IRP.getPositionKind() == IRPosition::IRP_CALL_SITE)
IsDeadAA = getOrCreateAAFor<AAIsDead>(
IRPosition::callsite_returned(cast<CallBase>(IRP.getAssociatedValue())),
QueryingAA, DepClassTy::NONE);
else
IsDeadAA = getOrCreateAAFor<AAIsDead>(IRP, QueryingAA, DepClassTy::NONE);
// Don't use recursive reasoning.
if (!IsDeadAA || QueryingAA == IsDeadAA)
return false;
if (IsDeadAA->isAssumedDead()) {
if (QueryingAA)
recordDependence(*IsDeadAA, *QueryingAA, DepClass);
if (!IsDeadAA->isKnownDead())
UsedAssumedInformation = true;
return true;
}
return false;
}
bool Attributor::isAssumedDead(const BasicBlock &BB,
const AbstractAttribute *QueryingAA,
const AAIsDead *FnLivenessAA,
DepClassTy DepClass) {
if (!Configuration.UseLiveness)
return false;
const Function &F = *BB.getParent();
if (!FnLivenessAA || FnLivenessAA->getAnchorScope() != &F)
FnLivenessAA = getOrCreateAAFor<AAIsDead>(IRPosition::function(F),
QueryingAA, DepClassTy::NONE);
// Don't use recursive reasoning.
if (!FnLivenessAA || QueryingAA == FnLivenessAA)
return false;
if (FnLivenessAA->isAssumedDead(&BB)) {
if (QueryingAA)
recordDependence(*FnLivenessAA, *QueryingAA, DepClass);
return true;
}
return false;
}
bool Attributor::checkForAllCallees(
function_ref<bool(ArrayRef<const Function *>)> Pred,
const AbstractAttribute &QueryingAA, const CallBase &CB) {
if (const Function *Callee = dyn_cast<Function>(CB.getCalledOperand()))
return Pred(Callee);
const auto *CallEdgesAA = getAAFor<AACallEdges>(
QueryingAA, IRPosition::callsite_function(CB), DepClassTy::OPTIONAL);
if (!CallEdgesAA || CallEdgesAA->hasUnknownCallee())
return false;
const auto &Callees = CallEdgesAA->getOptimisticEdges();
return Pred(Callees.getArrayRef());
}
bool Attributor::checkForAllUses(
function_ref<bool(const Use &, bool &)> Pred,
const AbstractAttribute &QueryingAA, const Value &V,
bool CheckBBLivenessOnly, DepClassTy LivenessDepClass,
bool IgnoreDroppableUses,
function_ref<bool(const Use &OldU, const Use &NewU)> EquivalentUseCB) {
// Check virtual uses first.
for (VirtualUseCallbackTy &CB : VirtualUseCallbacks.lookup(&V))
if (!CB(*this, &QueryingAA))
return false;
// Check the trivial case first as it catches void values.
if (V.use_empty())
return true;
const IRPosition &IRP = QueryingAA.getIRPosition();
SmallVector<const Use *, 16> Worklist;
SmallPtrSet<const Use *, 16> Visited;
auto AddUsers = [&](const Value &V, const Use *OldUse) {
for (const Use &UU : V.uses()) {
if (OldUse && EquivalentUseCB && !EquivalentUseCB(*OldUse, UU)) {
LLVM_DEBUG(dbgs() << "[Attributor] Potential copy was "
"rejected by the equivalence call back: "
<< *UU << "!\n");
return false;
}
Worklist.push_back(&UU);
}
return true;
};
AddUsers(V, /* OldUse */ nullptr);
LLVM_DEBUG(dbgs() << "[Attributor] Got " << Worklist.size()
<< " initial uses to check\n");
const Function *ScopeFn = IRP.getAnchorScope();
const auto *LivenessAA =
ScopeFn ? getAAFor<AAIsDead>(QueryingAA, IRPosition::function(*ScopeFn),
DepClassTy::NONE)
: nullptr;
while (!Worklist.empty()) {
const Use *U = Worklist.pop_back_val();
if (isa<PHINode>(U->getUser()) && !Visited.insert(U).second)
continue;
DEBUG_WITH_TYPE(VERBOSE_DEBUG_TYPE, {
if (auto *Fn = dyn_cast<Function>(U->getUser()))
dbgs() << "[Attributor] Check use: " << **U << " in " << Fn->getName()
<< "\n";
else
dbgs() << "[Attributor] Check use: " << **U << " in " << *U->getUser()
<< "\n";
});
bool UsedAssumedInformation = false;
if (isAssumedDead(*U, &QueryingAA, LivenessAA, UsedAssumedInformation,
CheckBBLivenessOnly, LivenessDepClass)) {
DEBUG_WITH_TYPE(VERBOSE_DEBUG_TYPE,
dbgs() << "[Attributor] Dead use, skip!\n");
continue;
}
if (IgnoreDroppableUses && U->getUser()->isDroppable()) {
DEBUG_WITH_TYPE(VERBOSE_DEBUG_TYPE,
dbgs() << "[Attributor] Droppable user, skip!\n");
continue;
}
if (auto *SI = dyn_cast<StoreInst>(U->getUser())) {
if (&SI->getOperandUse(0) == U) {
if (!Visited.insert(U).second)
continue;
SmallSetVector<Value *, 4> PotentialCopies;
if (AA::getPotentialCopiesOfStoredValue(
*this, *SI, PotentialCopies, QueryingAA, UsedAssumedInformation,
/* OnlyExact */ true)) {
DEBUG_WITH_TYPE(VERBOSE_DEBUG_TYPE,
dbgs()
<< "[Attributor] Value is stored, continue with "
<< PotentialCopies.size()
<< " potential copies instead!\n");
for (Value *PotentialCopy : PotentialCopies)
if (!AddUsers(*PotentialCopy, U))
return false;
continue;
}
}
}
bool Follow = false;
if (!Pred(*U, Follow))
return false;
if (!Follow)
continue;
User &Usr = *U->getUser();
AddUsers(Usr, /* OldUse */ nullptr);
auto *RI = dyn_cast<ReturnInst>(&Usr);
if (!RI)
continue;
Function &F = *RI->getFunction();
auto CallSitePred = [&](AbstractCallSite ACS) {
return AddUsers(*ACS.getInstruction(), U);
};
if (!checkForAllCallSites(CallSitePred, F, /* RequireAllCallSites */ true,
&QueryingAA, UsedAssumedInformation)) {
LLVM_DEBUG(dbgs() << "[Attributor] Could not follow return instruction "
"to all call sites: "
<< *RI << "\n");
return false;
}
}
return true;
}
bool Attributor::checkForAllCallSites(function_ref<bool(AbstractCallSite)> Pred,
const AbstractAttribute &QueryingAA,
bool RequireAllCallSites,
bool &UsedAssumedInformation) {
// We can try to determine information from
// the call sites. However, this is only possible all call sites are known,
// hence the function has internal linkage.
const IRPosition &IRP = QueryingAA.getIRPosition();
const Function *AssociatedFunction = IRP.getAssociatedFunction();
if (!AssociatedFunction) {
LLVM_DEBUG(dbgs() << "[Attributor] No function associated with " << IRP
<< "\n");
return false;
}
return checkForAllCallSites(Pred, *AssociatedFunction, RequireAllCallSites,
&QueryingAA, UsedAssumedInformation);
}
bool Attributor::checkForAllCallSites(function_ref<bool(AbstractCallSite)> Pred,
const Function &Fn,
bool RequireAllCallSites,
const AbstractAttribute *QueryingAA,
bool &UsedAssumedInformation,
bool CheckPotentiallyDead) {
if (RequireAllCallSites && !Fn.hasLocalLinkage()) {
LLVM_DEBUG(
dbgs()
<< "[Attributor] Function " << Fn.getName()
<< " has no internal linkage, hence not all call sites are known\n");
return false;
}
// Check virtual uses first.
for (VirtualUseCallbackTy &CB : VirtualUseCallbacks.lookup(&Fn))
if (!CB(*this, QueryingAA))
return false;
SmallVector<const Use *, 8> Uses(make_pointer_range(Fn.uses()));
for (unsigned u = 0; u < Uses.size(); ++u) {
const Use &U = *Uses[u];
DEBUG_WITH_TYPE(VERBOSE_DEBUG_TYPE, {
if (auto *Fn = dyn_cast<Function>(U))
dbgs() << "[Attributor] Check use: " << Fn->getName() << " in "
<< *U.getUser() << "\n";
else
dbgs() << "[Attributor] Check use: " << *U << " in " << *U.getUser()
<< "\n";
});
if (!CheckPotentiallyDead &&
isAssumedDead(U, QueryingAA, nullptr, UsedAssumedInformation,
/* CheckBBLivenessOnly */ true)) {
DEBUG_WITH_TYPE(VERBOSE_DEBUG_TYPE,
dbgs() << "[Attributor] Dead use, skip!\n");
continue;
}
if (ConstantExpr *CE = dyn_cast<ConstantExpr>(U.getUser())) {
if (CE->isCast() && CE->getType()->isPointerTy()) {
DEBUG_WITH_TYPE(VERBOSE_DEBUG_TYPE, {
dbgs() << "[Attributor] Use, is constant cast expression, add "
<< CE->getNumUses() << " uses of that expression instead!\n";
});
for (const Use &CEU : CE->uses())
Uses.push_back(&CEU);
continue;
}
}
AbstractCallSite ACS(&U);
if (!ACS) {
LLVM_DEBUG(dbgs() << "[Attributor] Function " << Fn.getName()
<< " has non call site use " << *U.get() << " in "
<< *U.getUser() << "\n");
// BlockAddress users are allowed.
if (isa<BlockAddress>(U.getUser()))
continue;
return false;
}
const Use *EffectiveUse =
ACS.isCallbackCall() ? &ACS.getCalleeUseForCallback() : &U;
if (!ACS.isCallee(EffectiveUse)) {
if (!RequireAllCallSites) {
LLVM_DEBUG(dbgs() << "[Attributor] User " << *EffectiveUse->getUser()
<< " is not a call of " << Fn.getName()
<< ", skip use\n");
continue;
}
LLVM_DEBUG(dbgs() << "[Attributor] User " << *EffectiveUse->getUser()
<< " is an invalid use of " << Fn.getName() << "\n");
return false;
}
// Make sure the arguments that can be matched between the call site and the
// callee argee on their type. It is unlikely they do not and it doesn't
// make sense for all attributes to know/care about this.
assert(&Fn == ACS.getCalledFunction() && "Expected known callee");
unsigned MinArgsParams =
std::min(size_t(ACS.getNumArgOperands()), Fn.arg_size());
for (unsigned u = 0; u < MinArgsParams; ++u) {
Value *CSArgOp = ACS.getCallArgOperand(u);
if (CSArgOp && Fn.getArg(u)->getType() != CSArgOp->getType()) {
LLVM_DEBUG(
dbgs() << "[Attributor] Call site / callee argument type mismatch ["
<< u << "@" << Fn.getName() << ": "
<< *Fn.getArg(u)->getType() << " vs. "
<< *ACS.getCallArgOperand(u)->getType() << "\n");
return false;
}
}
if (Pred(ACS))
continue;
LLVM_DEBUG(dbgs() << "[Attributor] Call site callback failed for "
<< *ACS.getInstruction() << "\n");
return false;
}
return true;
}
bool Attributor::shouldPropagateCallBaseContext(const IRPosition &IRP) {
// TODO: Maintain a cache of Values that are
// on the pathway from a Argument to a Instruction that would effect the
// liveness/return state etc.
return EnableCallSiteSpecific;
}
bool Attributor::checkForAllReturnedValues(function_ref<bool(Value &)> Pred,
const AbstractAttribute &QueryingAA,
AA::ValueScope S,
bool RecurseForSelectAndPHI) {
const IRPosition &IRP = QueryingAA.getIRPosition();
const Function *AssociatedFunction = IRP.getAssociatedFunction();
if (!AssociatedFunction)
return false;
bool UsedAssumedInformation = false;
SmallVector<AA::ValueAndContext> Values;
if (!getAssumedSimplifiedValues(
IRPosition::returned(*AssociatedFunction), &QueryingAA, Values, S,
UsedAssumedInformation, RecurseForSelectAndPHI))
return false;
return llvm::all_of(Values, [&](const AA::ValueAndContext &VAC) {
return Pred(*VAC.getValue());
});
}
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) {
for (unsigned Opcode : Opcodes) {
// Check if we have instructions with this opcode at all first.
auto *Insts = OpcodeInstMap.lookup(Opcode);
if (!Insts)
continue;
for (Instruction *I : *Insts) {
// Skip dead instructions.
if (A && !CheckPotentiallyDead &&
A->isAssumedDead(IRPosition::inst(*I), QueryingAA, LivenessAA,
UsedAssumedInformation, CheckBBLivenessOnly)) {
DEBUG_WITH_TYPE(VERBOSE_DEBUG_TYPE,
dbgs() << "[Attributor] Instruction " << *I
<< " is potentially dead, skip!\n";);
continue;
}
if (!Pred(*I))
return false;
}
}
return true;
}
bool Attributor::checkForAllInstructions(function_ref<bool(Instruction &)> Pred,
const Function *Fn,
const AbstractAttribute *QueryingAA,
ArrayRef<unsigned> Opcodes,
bool &UsedAssumedInformation,
bool CheckBBLivenessOnly,
bool CheckPotentiallyDead) {
// Since we need to provide instructions we have to have an exact definition.
if (!Fn || Fn->isDeclaration())
return false;
const IRPosition &QueryIRP = IRPosition::function(*Fn);
const auto *LivenessAA =
CheckPotentiallyDead && QueryingAA
? (getAAFor<AAIsDead>(*QueryingAA, QueryIRP, DepClassTy::NONE))
: nullptr;
auto &OpcodeInstMap = InfoCache.getOpcodeInstMapForFunction(*Fn);
if (!checkForAllInstructionsImpl(this, OpcodeInstMap, Pred, QueryingAA,
LivenessAA, Opcodes, UsedAssumedInformation,
CheckBBLivenessOnly, CheckPotentiallyDead))
return false;
return true;
}
bool Attributor::checkForAllInstructions(function_ref<bool(Instruction &)> Pred,
const AbstractAttribute &QueryingAA,
ArrayRef<unsigned> Opcodes,
bool &UsedAssumedInformation,
bool CheckBBLivenessOnly,
bool CheckPotentiallyDead) {
const IRPosition &IRP = QueryingAA.getIRPosition();
const Function *AssociatedFunction = IRP.getAssociatedFunction();
return checkForAllInstructions(Pred, AssociatedFunction, &QueryingAA, Opcodes,
UsedAssumedInformation, CheckBBLivenessOnly,
CheckPotentiallyDead);
}
bool Attributor::checkForAllReadWriteInstructions(
function_ref<bool(Instruction &)> Pred, AbstractAttribute &QueryingAA,
bool &UsedAssumedInformation) {
TimeTraceScope TS("checkForAllReadWriteInstructions");
const Function *AssociatedFunction =
QueryingAA.getIRPosition().getAssociatedFunction();
if (!AssociatedFunction)
return false;
const IRPosition &QueryIRP = IRPosition::function(*AssociatedFunction);
const auto *LivenessAA =
getAAFor<AAIsDead>(QueryingAA, QueryIRP, DepClassTy::NONE);
for (Instruction *I :
InfoCache.getReadOrWriteInstsForFunction(*AssociatedFunction)) {
// Skip dead instructions.
if (isAssumedDead(IRPosition::inst(*I), &QueryingAA, LivenessAA,
UsedAssumedInformation))
continue;
if (!Pred(*I))
return false;
}
return true;
}
void Attributor::runTillFixpoint() {
TimeTraceScope TimeScope("Attributor::runTillFixpoint");
LLVM_DEBUG(dbgs() << "[Attributor] Identified and initialized "
<< DG.SyntheticRoot.Deps.size()
<< " abstract attributes.\n");
// Now that all abstract attributes are collected and initialized we start
// the abstract analysis.
unsigned IterationCounter = 1;
unsigned MaxIterations =
Configuration.MaxFixpointIterations.value_or(SetFixpointIterations);
SmallVector<AbstractAttribute *, 32> ChangedAAs;
SetVector<AbstractAttribute *> Worklist, InvalidAAs;
Worklist.insert(DG.SyntheticRoot.begin(), DG.SyntheticRoot.end());
do {
// Remember the size to determine new attributes.
size_t NumAAs = DG.SyntheticRoot.Deps.size();
LLVM_DEBUG(dbgs() << "\n\n[Attributor] #Iteration: " << IterationCounter
<< ", Worklist size: " << Worklist.size() << "\n");
// For invalid AAs we can fix dependent AAs that have a required dependence,
// thereby folding long dependence chains in a single step without the need
// to run updates.
for (unsigned u = 0; u < InvalidAAs.size(); ++u) {
AbstractAttribute *InvalidAA = InvalidAAs[u];
// Check the dependences to fast track invalidation.
DEBUG_WITH_TYPE(VERBOSE_DEBUG_TYPE,
dbgs() << "[Attributor] InvalidAA: " << *InvalidAA
<< " has " << InvalidAA->Deps.size()
<< " required & optional dependences\n");
for (auto &DepIt : InvalidAA->Deps) {
AbstractAttribute *DepAA = cast<AbstractAttribute>(DepIt.getPointer());
if (DepIt.getInt() == unsigned(DepClassTy::OPTIONAL)) {
DEBUG_WITH_TYPE(VERBOSE_DEBUG_TYPE,
dbgs() << " - recompute: " << *DepAA);
Worklist.insert(DepAA);
continue;
}
DEBUG_WITH_TYPE(VERBOSE_DEBUG_TYPE, dbgs()
<< " - invalidate: " << *DepAA);
DepAA->getState().indicatePessimisticFixpoint();
assert(DepAA->getState().isAtFixpoint() && "Expected fixpoint state!");
if (!DepAA->getState().isValidState())
InvalidAAs.insert(DepAA);
else
ChangedAAs.push_back(DepAA);
}
InvalidAA->Deps.clear();
}
// Add all abstract attributes that are potentially dependent on one that
// changed to the work list.
for (AbstractAttribute *ChangedAA : ChangedAAs) {
for (auto &DepIt : ChangedAA->Deps)
Worklist.insert(cast<AbstractAttribute>(DepIt.getPointer()));
ChangedAA->Deps.clear();
}
LLVM_DEBUG(dbgs() << "[Attributor] #Iteration: " << IterationCounter
<< ", Worklist+Dependent size: " << Worklist.size()
<< "\n");
// Reset the changed and invalid set.
ChangedAAs.clear();
InvalidAAs.clear();
// Update all abstract attribute in the work list and record the ones that
// changed.
for (AbstractAttribute *AA : Worklist) {
const auto &AAState = AA->getState();
if (!AAState.isAtFixpoint())
if (updateAA(*AA) == ChangeStatus::CHANGED)
ChangedAAs.push_back(AA);
// Use the InvalidAAs vector to propagate invalid states fast transitively
// without requiring updates.
if (!AAState.isValidState())
InvalidAAs.insert(AA);
}
// Add attributes to the changed set if they have been created in the last
// iteration.
ChangedAAs.append(DG.SyntheticRoot.begin() + NumAAs,
DG.SyntheticRoot.end());
// Reset the work list and repopulate with the changed abstract attributes.
// Note that dependent ones are added above.
Worklist.clear();
Worklist.insert(ChangedAAs.begin(), ChangedAAs.end());
Worklist.insert(QueryAAsAwaitingUpdate.begin(),
QueryAAsAwaitingUpdate.end());
QueryAAsAwaitingUpdate.clear();
} while (!Worklist.empty() && (IterationCounter++ < MaxIterations));
if (IterationCounter > MaxIterations && !Functions.empty()) {
auto Remark = [&](OptimizationRemarkMissed ORM) {
return ORM << "Attributor did not reach a fixpoint after "
<< ore::NV("Iterations", MaxIterations) << " iterations.";
};
Function *F = Functions.front();
emitRemark<OptimizationRemarkMissed>(F, "FixedPoint", Remark);
}
LLVM_DEBUG(dbgs() << "\n[Attributor] Fixpoint iteration done after: "
<< IterationCounter << "/" << MaxIterations
<< " iterations\n");
// Reset abstract arguments not settled in a sound fixpoint by now. This
// happens when we stopped the fixpoint iteration early. Note that only the
// ones marked as "changed" *and* the ones transitively depending on them
// need to be reverted to a pessimistic state. Others might not be in a
// fixpoint state but we can use the optimistic results for them anyway.
SmallPtrSet<AbstractAttribute *, 32> Visited;
for (unsigned u = 0; u < ChangedAAs.size(); u++) {
AbstractAttribute *ChangedAA = ChangedAAs[u];
if (!Visited.insert(ChangedAA).second)
continue;
AbstractState &State = ChangedAA->getState();
if (!State.isAtFixpoint()) {
State.indicatePessimisticFixpoint();
NumAttributesTimedOut++;
}
for (auto &DepIt : ChangedAA->Deps)
ChangedAAs.push_back(cast<AbstractAttribute>(DepIt.getPointer()));
ChangedAA->Deps.clear();
}
LLVM_DEBUG({
if (!Visited.empty())
dbgs() << "\n[Attributor] Finalized " << Visited.size()
<< " abstract attributes.\n";
});
}
void Attributor::registerForUpdate(AbstractAttribute &AA) {
assert(AA.isQueryAA() &&
"Non-query AAs should not be required to register for updates!");
QueryAAsAwaitingUpdate.insert(&AA);
}
ChangeStatus Attributor::manifestAttributes() {
TimeTraceScope TimeScope("Attributor::manifestAttributes");
size_t NumFinalAAs = DG.SyntheticRoot.Deps.size();
unsigned NumManifested = 0;
unsigned NumAtFixpoint = 0;
ChangeStatus ManifestChange = ChangeStatus::UNCHANGED;
for (auto &DepAA : DG.SyntheticRoot.Deps) {
AbstractAttribute *AA = cast<AbstractAttribute>(DepAA.getPointer());
AbstractState &State = AA->getState();
// If there is not already a fixpoint reached, we can now take the
// optimistic state. This is correct because we enforced a pessimistic one
// on abstract attributes that were transitively dependent on a changed one
// already above.
if (!State.isAtFixpoint())
State.indicateOptimisticFixpoint();
// We must not manifest Attributes that use Callbase info.
if (AA->hasCallBaseContext())
continue;
// If the state is invalid, we do not try to manifest it.
if (!State.isValidState())
continue;
if (AA->getCtxI() && !isRunOn(*AA->getAnchorScope()))
continue;
// Skip dead code.
bool UsedAssumedInformation = false;
if (isAssumedDead(*AA, nullptr, UsedAssumedInformation,
/* CheckBBLivenessOnly */ true))
continue;
// Check if the manifest debug counter that allows skipping manifestation of
// AAs
if (!DebugCounter::shouldExecute(ManifestDBGCounter))
continue;
// Manifest the state and record if we changed the IR.
ChangeStatus LocalChange = AA->manifest(*this);
if (LocalChange == ChangeStatus::CHANGED && AreStatisticsEnabled())
AA->trackStatistics();
LLVM_DEBUG(dbgs() << "[Attributor] Manifest " << LocalChange << " : " << *AA
<< "\n");
ManifestChange = ManifestChange | LocalChange;
NumAtFixpoint++;
NumManifested += (LocalChange == ChangeStatus::CHANGED);
}
(void)NumManifested;
(void)NumAtFixpoint;
LLVM_DEBUG(dbgs() << "\n[Attributor] Manifested " << NumManifested
<< " arguments while " << NumAtFixpoint
<< " were in a valid fixpoint state\n");
NumAttributesManifested += NumManifested;
NumAttributesValidFixpoint += NumAtFixpoint;
(void)NumFinalAAs;
if (NumFinalAAs != DG.SyntheticRoot.Deps.size()) {
auto DepIt = DG.SyntheticRoot.Deps.begin();
for (unsigned u = 0; u < NumFinalAAs; ++u)
++DepIt;
for (unsigned u = NumFinalAAs; u < DG.SyntheticRoot.Deps.size();
++u, ++DepIt) {
errs() << "Unexpected abstract attribute: "
<< cast<AbstractAttribute>(DepIt->getPointer()) << " :: "
<< cast<AbstractAttribute>(DepIt->getPointer())
->getIRPosition()
.getAssociatedValue()
<< "\n";
}
llvm_unreachable("Expected the final number of abstract attributes to "
"remain unchanged!");
}
for (auto &It : AttrsMap) {
AttributeList &AL = It.getSecond();
const IRPosition &IRP =
isa<Function>(It.getFirst())
? IRPosition::function(*cast<Function>(It.getFirst()))
: IRPosition::callsite_function(*cast<CallBase>(It.getFirst()));
IRP.setAttrList(AL);
}
return ManifestChange;
}
void Attributor::identifyDeadInternalFunctions() {
// Early exit if we don't intend to delete functions.
if (!Configuration.DeleteFns)
return;
// To avoid triggering an assertion in the lazy call graph we will not delete
// any internal library functions. We should modify the assertion though and
// allow internals to be deleted.
const auto *TLI =
isModulePass()
? nullptr
: getInfoCache().getTargetLibraryInfoForFunction(*Functions.back());
LibFunc LF;
// Identify dead internal functions and delete them. This happens outside
// the other fixpoint analysis as we might treat potentially dead functions
// as live to lower the number of iterations. If they happen to be dead, the
// below fixpoint loop will identify and eliminate them.
SmallVector<Function *, 8> InternalFns;
for (Function *F : Functions)
if (F->hasLocalLinkage() && (isModulePass() || !TLI->getLibFunc(*F, LF)))
InternalFns.push_back(F);
SmallPtrSet<Function *, 8> LiveInternalFns;
bool FoundLiveInternal = true;
while (FoundLiveInternal) {
FoundLiveInternal = false;
for (unsigned u = 0, e = InternalFns.size(); u < e; ++u) {
Function *F = InternalFns[u];
if (!F)
continue;
bool UsedAssumedInformation = false;
if (checkForAllCallSites(
[&](AbstractCallSite ACS) {
Function *Callee = ACS.getInstruction()->getFunction();
return ToBeDeletedFunctions.count(Callee) ||
(Functions.count(Callee) && Callee->hasLocalLinkage() &&
!LiveInternalFns.count(Callee));
},
*F, true, nullptr, UsedAssumedInformation)) {
continue;
}
LiveInternalFns.insert(F);
InternalFns[u] = nullptr;
FoundLiveInternal = true;
}
}
for (unsigned u = 0, e = InternalFns.size(); u < e; ++u)
if (Function *F = InternalFns[u])
ToBeDeletedFunctions.insert(F);
}
ChangeStatus Attributor::cleanupIR() {
TimeTraceScope TimeScope("Attributor::cleanupIR");
// Delete stuff at the end to avoid invalid references and a nice order.
LLVM_DEBUG(dbgs() << "\n[Attributor] Delete/replace at least "
<< ToBeDeletedFunctions.size() << " functions and "
<< ToBeDeletedBlocks.size() << " blocks and "
<< ToBeDeletedInsts.size() << " instructions and "
<< ToBeChangedValues.size() << " values and "
<< ToBeChangedUses.size() << " uses. To insert "
<< ToBeChangedToUnreachableInsts.size()
<< " unreachables.\n"
<< "Preserve manifest added " << ManifestAddedBlocks.size()
<< " blocks\n");
SmallVector<WeakTrackingVH, 32> DeadInsts;
SmallVector<Instruction *, 32> TerminatorsToFold;
auto ReplaceUse = [&](Use *U, Value *NewV) {
Value *OldV = U->get();
// If we plan to replace NewV we need to update it at this point.
do {
const auto &Entry = ToBeChangedValues.lookup(NewV);
if (!get<0>(Entry))
break;
NewV = get<0>(Entry);
} while (true);
Instruction *I = dyn_cast<Instruction>(U->getUser());
assert((!I || isRunOn(*I->getFunction())) &&
"Cannot replace an instruction outside the current SCC!");
// Do not replace uses in returns if the value is a must-tail call we will
// not delete.
if (auto *RI = dyn_cast_or_null<ReturnInst>(I)) {
if (auto *CI = dyn_cast<CallInst>(OldV->stripPointerCasts()))
if (CI->isMustTailCall() && !ToBeDeletedInsts.count(CI))
return;
// If we rewrite a return and the new value is not an argument, strip the
// `returned` attribute as it is wrong now.
if (!isa<Argument>(NewV))
for (auto &Arg : RI->getFunction()->args())
Arg.removeAttr(Attribute::Returned);
}
LLVM_DEBUG(dbgs() << "Use " << *NewV << " in " << *U->getUser()
<< " instead of " << *OldV << "\n");
U->set(NewV);
if (Instruction *I = dyn_cast<Instruction>(OldV)) {
CGModifiedFunctions.insert(I->getFunction());
if (!isa<PHINode>(I) && !ToBeDeletedInsts.count(I) &&
isInstructionTriviallyDead(I))
DeadInsts.push_back(I);
}
if (isa<UndefValue>(NewV) && isa<CallBase>(U->getUser())) {
auto *CB = cast<CallBase>(U->getUser());
if (CB->isArgOperand(U)) {
unsigned Idx = CB->getArgOperandNo(U);
CB->removeParamAttr(Idx, Attribute::NoUndef);
auto *Callee = dyn_cast_if_present<Function>(CB->getCalledOperand());
if (Callee && Callee->arg_size() > Idx)
Callee->removeParamAttr(Idx, Attribute::NoUndef);
}
}
if (isa<Constant>(NewV) && isa<BranchInst>(U->getUser())) {
Instruction *UserI = cast<Instruction>(U->getUser());
if (isa<UndefValue>(NewV)) {
ToBeChangedToUnreachableInsts.insert(UserI);
} else {
TerminatorsToFold.push_back(UserI);
}
}
};
for (auto &It : ToBeChangedUses) {
Use *U = It.first;
Value *NewV = It.second;
ReplaceUse(U, NewV);
}
SmallVector<Use *, 4> Uses;
for (auto &It : ToBeChangedValues) {
Value *OldV = It.first;
auto [NewV, Done] = It.second;
Uses.clear();
for (auto &U : OldV->uses())
if (Done || !U.getUser()->isDroppable())
Uses.push_back(&U);
for (Use *U : Uses) {
if (auto *I = dyn_cast<Instruction>(U->getUser()))
if (!isRunOn(*I->getFunction()))
continue;
ReplaceUse(U, NewV);
}
}
for (const auto &V : InvokeWithDeadSuccessor)
if (InvokeInst *II = dyn_cast_or_null<InvokeInst>(V)) {
assert(isRunOn(*II->getFunction()) &&
"Cannot replace an invoke outside the current SCC!");
bool UnwindBBIsDead = II->hasFnAttr(Attribute::NoUnwind);
bool NormalBBIsDead = II->hasFnAttr(Attribute::NoReturn);
bool Invoke2CallAllowed =
!AAIsDead::mayCatchAsynchronousExceptions(*II->getFunction());
assert((UnwindBBIsDead || NormalBBIsDead) &&
"Invoke does not have dead successors!");
BasicBlock *BB = II->getParent();
BasicBlock *NormalDestBB = II->getNormalDest();
if (UnwindBBIsDead) {
Instruction *NormalNextIP = &NormalDestBB->front();
if (Invoke2CallAllowed) {
changeToCall(II);
NormalNextIP = BB->getTerminator();
}
if (NormalBBIsDead)
ToBeChangedToUnreachableInsts.insert(NormalNextIP);
} else {
assert(NormalBBIsDead && "Broken invariant!");
if (!NormalDestBB->getUniquePredecessor())
NormalDestBB = SplitBlockPredecessors(NormalDestBB, {BB}, ".dead");
ToBeChangedToUnreachableInsts.insert(&NormalDestBB->front());
}
}
for (Instruction *I : TerminatorsToFold) {
assert(isRunOn(*I->getFunction()) &&
"Cannot replace a terminator outside the current SCC!");