Path: blob/main/contrib/llvm-project/llvm/lib/Analysis/AliasSetTracker.cpp
35233 views
//===- AliasSetTracker.cpp - Alias Sets Tracker implementation-------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file implements the AliasSetTracker and AliasSet classes.9//10//===----------------------------------------------------------------------===//1112#include "llvm/Analysis/AliasSetTracker.h"13#include "llvm/ADT/SetVector.h"14#include "llvm/ADT/StringExtras.h"15#include "llvm/Analysis/AliasAnalysis.h"16#include "llvm/Analysis/GuardUtils.h"17#include "llvm/Analysis/MemoryLocation.h"18#include "llvm/Config/llvm-config.h"19#include "llvm/IR/Function.h"20#include "llvm/IR/InstIterator.h"21#include "llvm/IR/Instructions.h"22#include "llvm/IR/IntrinsicInst.h"23#include "llvm/IR/PassManager.h"24#include "llvm/IR/PatternMatch.h"25#include "llvm/IR/Value.h"26#include "llvm/InitializePasses.h"27#include "llvm/Pass.h"28#include "llvm/Support/AtomicOrdering.h"29#include "llvm/Support/CommandLine.h"30#include "llvm/Support/Compiler.h"31#include "llvm/Support/Debug.h"32#include "llvm/Support/ErrorHandling.h"33#include "llvm/Support/raw_ostream.h"3435using namespace llvm;3637static cl::opt<unsigned> SaturationThreshold(38"alias-set-saturation-threshold", cl::Hidden, cl::init(250),39cl::desc("The maximum total number of memory locations alias "40"sets may contain before degradation"));4142/// mergeSetIn - Merge the specified alias set into this alias set.43void AliasSet::mergeSetIn(AliasSet &AS, AliasSetTracker &AST,44BatchAAResults &BatchAA) {45assert(!AS.Forward && "Alias set is already forwarding!");46assert(!Forward && "This set is a forwarding set!!");4748// Update the alias and access types of this set...49Access |= AS.Access;50Alias |= AS.Alias;5152if (Alias == SetMustAlias) {53// Check that these two merged sets really are must aliases. If we cannot54// find a must-alias pair between them, this set becomes a may alias.55if (!any_of(MemoryLocs, [&](const MemoryLocation &MemLoc) {56return any_of(AS.MemoryLocs, [&](const MemoryLocation &ASMemLoc) {57return BatchAA.isMustAlias(MemLoc, ASMemLoc);58});59}))60Alias = SetMayAlias;61}6263// Merge the list of constituent memory locations...64if (MemoryLocs.empty()) {65std::swap(MemoryLocs, AS.MemoryLocs);66} else {67append_range(MemoryLocs, AS.MemoryLocs);68AS.MemoryLocs.clear();69}7071bool ASHadUnknownInsts = !AS.UnknownInsts.empty();72if (UnknownInsts.empty()) { // Merge call sites...73if (ASHadUnknownInsts) {74std::swap(UnknownInsts, AS.UnknownInsts);75addRef();76}77} else if (ASHadUnknownInsts) {78llvm::append_range(UnknownInsts, AS.UnknownInsts);79AS.UnknownInsts.clear();80}8182AS.Forward = this; // Forward across AS now...83addRef(); // AS is now pointing to us...8485if (ASHadUnknownInsts)86AS.dropRef(AST);87}8889void AliasSetTracker::removeAliasSet(AliasSet *AS) {90if (AliasSet *Fwd = AS->Forward) {91Fwd->dropRef(*this);92AS->Forward = nullptr;93} else // Update TotalAliasSetSize only if not forwarding.94TotalAliasSetSize -= AS->size();9596AliasSets.erase(AS);97// If we've removed the saturated alias set, set saturated marker back to98// nullptr and ensure this tracker is empty.99if (AS == AliasAnyAS) {100AliasAnyAS = nullptr;101assert(AliasSets.empty() && "Tracker not empty");102}103}104105void AliasSet::removeFromTracker(AliasSetTracker &AST) {106assert(RefCount == 0 && "Cannot remove non-dead alias set from tracker!");107AST.removeAliasSet(this);108}109110void AliasSet::addMemoryLocation(AliasSetTracker &AST,111const MemoryLocation &MemLoc,112bool KnownMustAlias) {113if (isMustAlias() && !KnownMustAlias) {114// If we cannot find a must-alias with any of the existing MemoryLocs, we115// must downgrade to may-alias.116if (!any_of(MemoryLocs, [&](const MemoryLocation &ASMemLoc) {117return AST.getAliasAnalysis().isMustAlias(MemLoc, ASMemLoc);118}))119Alias = SetMayAlias;120}121122// Add it to the end of the list...123MemoryLocs.push_back(MemLoc);124125AST.TotalAliasSetSize++;126}127128void AliasSet::addUnknownInst(Instruction *I, BatchAAResults &AA) {129if (UnknownInsts.empty())130addRef();131UnknownInsts.emplace_back(I);132133// Guards are marked as modifying memory for control flow modelling purposes,134// but don't actually modify any specific memory location.135using namespace PatternMatch;136bool MayWriteMemory = I->mayWriteToMemory() && !isGuard(I) &&137!(I->use_empty() && match(I, m_Intrinsic<Intrinsic::invariant_start>()));138if (!MayWriteMemory) {139Alias = SetMayAlias;140Access |= RefAccess;141return;142}143144// FIXME: This should use mod/ref information to make this not suck so bad145Alias = SetMayAlias;146Access = ModRefAccess;147}148149/// aliasesMemoryLocation - If the specified memory location "may" (or must)150/// alias one of the members in the set return the appropriate AliasResult.151/// Otherwise return NoAlias.152///153AliasResult AliasSet::aliasesMemoryLocation(const MemoryLocation &MemLoc,154BatchAAResults &AA) const {155if (AliasAny)156return AliasResult::MayAlias;157158// Check all of the memory locations in the set...159for (const auto &ASMemLoc : MemoryLocs) {160AliasResult AR = AA.alias(MemLoc, ASMemLoc);161if (AR != AliasResult::NoAlias)162return AR;163}164165// Check the unknown instructions...166for (Instruction *Inst : UnknownInsts)167if (isModOrRefSet(AA.getModRefInfo(Inst, MemLoc)))168return AliasResult::MayAlias;169170return AliasResult::NoAlias;171}172173ModRefInfo AliasSet::aliasesUnknownInst(const Instruction *Inst,174BatchAAResults &AA) const {175176if (AliasAny)177return ModRefInfo::ModRef;178179if (!Inst->mayReadOrWriteMemory())180return ModRefInfo::NoModRef;181182for (Instruction *UnknownInst : UnknownInsts) {183const auto *C1 = dyn_cast<CallBase>(UnknownInst);184const auto *C2 = dyn_cast<CallBase>(Inst);185if (!C1 || !C2 || isModOrRefSet(AA.getModRefInfo(C1, C2)) ||186isModOrRefSet(AA.getModRefInfo(C2, C1))) {187// TODO: Could be more precise, but not really useful right now.188return ModRefInfo::ModRef;189}190}191192ModRefInfo MR = ModRefInfo::NoModRef;193for (const auto &ASMemLoc : MemoryLocs) {194MR |= AA.getModRefInfo(Inst, ASMemLoc);195if (isModAndRefSet(MR))196return MR;197}198199return MR;200}201202AliasSet::PointerVector AliasSet::getPointers() const {203SmallSetVector<const Value *, 8> Pointers;204for (const MemoryLocation &MemLoc : MemoryLocs)205Pointers.insert(MemLoc.Ptr);206return Pointers.takeVector();207}208209void AliasSetTracker::clear() {210PointerMap.clear();211AliasSets.clear();212}213214/// mergeAliasSetsForMemoryLocation - Given a memory location, merge all alias215/// sets that may alias it. Return the unified set, or nullptr if no aliasing216/// set was found. A known existing alias set for the pointer value of the217/// memory location can be passed in (or nullptr if not available). MustAliasAll218/// is updated to true/false if the memory location is found to MustAlias all219/// the sets it merged.220AliasSet *AliasSetTracker::mergeAliasSetsForMemoryLocation(221const MemoryLocation &MemLoc, AliasSet *PtrAS, bool &MustAliasAll) {222AliasSet *FoundSet = nullptr;223MustAliasAll = true;224for (AliasSet &AS : llvm::make_early_inc_range(*this)) {225if (AS.Forward)226continue;227228// An alias set that already contains a memory location with the same229// pointer value is directly assumed to MustAlias; we bypass the AA query in230// this case.231// Note: it is not guaranteed that AA would always provide the same result;232// a known exception are undef pointer values, where alias(undef, undef) is233// NoAlias, while we treat it as MustAlias.234if (&AS != PtrAS) {235AliasResult AR = AS.aliasesMemoryLocation(MemLoc, AA);236if (AR == AliasResult::NoAlias)237continue;238239if (AR != AliasResult::MustAlias)240MustAliasAll = false;241}242243if (!FoundSet) {244// If this is the first alias set ptr can go into, remember it.245FoundSet = &AS;246} else {247// Otherwise, we must merge the sets.248FoundSet->mergeSetIn(AS, *this, AA);249}250}251252return FoundSet;253}254255AliasSet *AliasSetTracker::findAliasSetForUnknownInst(Instruction *Inst) {256AliasSet *FoundSet = nullptr;257for (AliasSet &AS : llvm::make_early_inc_range(*this)) {258if (AS.Forward || !isModOrRefSet(AS.aliasesUnknownInst(Inst, AA)))259continue;260if (!FoundSet) {261// If this is the first alias set ptr can go into, remember it.262FoundSet = &AS;263} else {264// Otherwise, we must merge the sets.265FoundSet->mergeSetIn(AS, *this, AA);266}267}268return FoundSet;269}270271AliasSet &AliasSetTracker::getAliasSetFor(const MemoryLocation &MemLoc) {272// The alias sets are indexed with a map from the memory locations' pointer273// values. If the memory location is already registered, we can find it in the274// alias set associated with its pointer.275AliasSet *&MapEntry = PointerMap[MemLoc.Ptr];276if (MapEntry) {277collapseForwardingIn(MapEntry);278if (is_contained(MapEntry->MemoryLocs, MemLoc))279return *MapEntry;280}281282AliasSet *AS;283bool MustAliasAll = false;284if (AliasAnyAS) {285// At this point, the AST is saturated, so we only have one active alias286// set. That means we already know which alias set we want to return, and287// just need to add the memory location to that set to keep the data288// structure consistent.289// This, of course, means that we will never need a merge here.290AS = AliasAnyAS;291} else if (AliasSet *AliasAS = mergeAliasSetsForMemoryLocation(292MemLoc, MapEntry, MustAliasAll)) {293// Add it to the alias set it aliases.294AS = AliasAS;295} else {296// Otherwise create a new alias set to hold the new memory location.297AliasSets.push_back(AS = new AliasSet());298MustAliasAll = true;299}300301// Register memory location in selected alias set.302AS->addMemoryLocation(*this, MemLoc, MustAliasAll);303// Register selected alias set in pointer map (or ensure it is consistent with304// earlier map entry after taking into account new merging).305if (MapEntry) {306collapseForwardingIn(MapEntry);307assert(MapEntry == AS && "Memory locations with same pointer value cannot "308"be in different alias sets");309} else {310AS->addRef();311MapEntry = AS;312}313return *AS;314}315316void AliasSetTracker::add(const MemoryLocation &Loc) {317addMemoryLocation(Loc, AliasSet::NoAccess);318}319320void AliasSetTracker::add(LoadInst *LI) {321if (isStrongerThanMonotonic(LI->getOrdering()))322return addUnknown(LI);323addMemoryLocation(MemoryLocation::get(LI), AliasSet::RefAccess);324}325326void AliasSetTracker::add(StoreInst *SI) {327if (isStrongerThanMonotonic(SI->getOrdering()))328return addUnknown(SI);329addMemoryLocation(MemoryLocation::get(SI), AliasSet::ModAccess);330}331332void AliasSetTracker::add(VAArgInst *VAAI) {333addMemoryLocation(MemoryLocation::get(VAAI), AliasSet::ModRefAccess);334}335336void AliasSetTracker::add(AnyMemSetInst *MSI) {337addMemoryLocation(MemoryLocation::getForDest(MSI), AliasSet::ModAccess);338}339340void AliasSetTracker::add(AnyMemTransferInst *MTI) {341addMemoryLocation(MemoryLocation::getForDest(MTI), AliasSet::ModAccess);342addMemoryLocation(MemoryLocation::getForSource(MTI), AliasSet::RefAccess);343}344345void AliasSetTracker::addUnknown(Instruction *Inst) {346if (isa<DbgInfoIntrinsic>(Inst))347return; // Ignore DbgInfo Intrinsics.348349if (auto *II = dyn_cast<IntrinsicInst>(Inst)) {350// These intrinsics will show up as affecting memory, but they are just351// markers.352switch (II->getIntrinsicID()) {353default:354break;355// FIXME: Add lifetime/invariant intrinsics (See: PR30807).356case Intrinsic::allow_runtime_check:357case Intrinsic::allow_ubsan_check:358case Intrinsic::assume:359case Intrinsic::experimental_noalias_scope_decl:360case Intrinsic::sideeffect:361case Intrinsic::pseudoprobe:362return;363}364}365if (!Inst->mayReadOrWriteMemory())366return; // doesn't alias anything367368if (AliasSet *AS = findAliasSetForUnknownInst(Inst)) {369AS->addUnknownInst(Inst, AA);370return;371}372AliasSets.push_back(new AliasSet());373AliasSets.back().addUnknownInst(Inst, AA);374}375376void AliasSetTracker::add(Instruction *I) {377// Dispatch to one of the other add methods.378if (LoadInst *LI = dyn_cast<LoadInst>(I))379return add(LI);380if (StoreInst *SI = dyn_cast<StoreInst>(I))381return add(SI);382if (VAArgInst *VAAI = dyn_cast<VAArgInst>(I))383return add(VAAI);384if (AnyMemSetInst *MSI = dyn_cast<AnyMemSetInst>(I))385return add(MSI);386if (AnyMemTransferInst *MTI = dyn_cast<AnyMemTransferInst>(I))387return add(MTI);388389// Handle all calls with known mod/ref sets genericall390if (auto *Call = dyn_cast<CallBase>(I))391if (Call->onlyAccessesArgMemory()) {392auto getAccessFromModRef = [](ModRefInfo MRI) {393if (isRefSet(MRI) && isModSet(MRI))394return AliasSet::ModRefAccess;395else if (isModSet(MRI))396return AliasSet::ModAccess;397else if (isRefSet(MRI))398return AliasSet::RefAccess;399else400return AliasSet::NoAccess;401};402403ModRefInfo CallMask = AA.getMemoryEffects(Call).getModRef();404405// Some intrinsics are marked as modifying memory for control flow406// modelling purposes, but don't actually modify any specific memory407// location.408using namespace PatternMatch;409if (Call->use_empty() &&410match(Call, m_Intrinsic<Intrinsic::invariant_start>()))411CallMask &= ModRefInfo::Ref;412413for (auto IdxArgPair : enumerate(Call->args())) {414int ArgIdx = IdxArgPair.index();415const Value *Arg = IdxArgPair.value();416if (!Arg->getType()->isPointerTy())417continue;418MemoryLocation ArgLoc =419MemoryLocation::getForArgument(Call, ArgIdx, nullptr);420ModRefInfo ArgMask = AA.getArgModRefInfo(Call, ArgIdx);421ArgMask &= CallMask;422if (!isNoModRef(ArgMask))423addMemoryLocation(ArgLoc, getAccessFromModRef(ArgMask));424}425return;426}427428return addUnknown(I);429}430431void AliasSetTracker::add(BasicBlock &BB) {432for (auto &I : BB)433add(&I);434}435436void AliasSetTracker::add(const AliasSetTracker &AST) {437assert(&AA == &AST.AA &&438"Merging AliasSetTracker objects with different Alias Analyses!");439440// Loop over all of the alias sets in AST, adding the members contained441// therein into the current alias sets. This can cause alias sets to be442// merged together in the current AST.443for (const AliasSet &AS : AST) {444if (AS.Forward)445continue; // Ignore forwarding alias sets446447// If there are any call sites in the alias set, add them to this AST.448for (Instruction *Inst : AS.UnknownInsts)449add(Inst);450451// Loop over all of the memory locations in this alias set.452for (const MemoryLocation &ASMemLoc : AS.MemoryLocs)453addMemoryLocation(ASMemLoc, (AliasSet::AccessLattice)AS.Access);454}455}456457AliasSet &AliasSetTracker::mergeAllAliasSets() {458assert(!AliasAnyAS && (TotalAliasSetSize > SaturationThreshold) &&459"Full merge should happen once, when the saturation threshold is "460"reached");461462// Collect all alias sets, so that we can drop references with impunity463// without worrying about iterator invalidation.464std::vector<AliasSet *> ASVector;465ASVector.reserve(SaturationThreshold);466for (AliasSet &AS : *this)467ASVector.push_back(&AS);468469// Copy all instructions and memory locations into a new set, and forward all470// other sets to it.471AliasSets.push_back(new AliasSet());472AliasAnyAS = &AliasSets.back();473AliasAnyAS->Alias = AliasSet::SetMayAlias;474AliasAnyAS->Access = AliasSet::ModRefAccess;475AliasAnyAS->AliasAny = true;476477for (auto *Cur : ASVector) {478// If Cur was already forwarding, just forward to the new AS instead.479AliasSet *FwdTo = Cur->Forward;480if (FwdTo) {481Cur->Forward = AliasAnyAS;482AliasAnyAS->addRef();483FwdTo->dropRef(*this);484continue;485}486487// Otherwise, perform the actual merge.488AliasAnyAS->mergeSetIn(*Cur, *this, AA);489}490491return *AliasAnyAS;492}493494AliasSet &AliasSetTracker::addMemoryLocation(MemoryLocation Loc,495AliasSet::AccessLattice E) {496AliasSet &AS = getAliasSetFor(Loc);497AS.Access |= E;498499if (!AliasAnyAS && (TotalAliasSetSize > SaturationThreshold)) {500// The AST is now saturated. From here on, we conservatively consider all501// elements to alias each-other.502return mergeAllAliasSets();503}504505return AS;506}507508//===----------------------------------------------------------------------===//509// AliasSet/AliasSetTracker Printing Support510//===----------------------------------------------------------------------===//511512void AliasSet::print(raw_ostream &OS) const {513OS << " AliasSet[" << (const void*)this << ", " << RefCount << "] ";514OS << (Alias == SetMustAlias ? "must" : "may") << " alias, ";515switch (Access) {516case NoAccess: OS << "No access "; break;517case RefAccess: OS << "Ref "; break;518case ModAccess: OS << "Mod "; break;519case ModRefAccess: OS << "Mod/Ref "; break;520default: llvm_unreachable("Bad value for Access!");521}522if (Forward)523OS << " forwarding to " << (void*)Forward;524525if (!MemoryLocs.empty()) {526ListSeparator LS;527OS << "Memory locations: ";528for (const MemoryLocation &MemLoc : MemoryLocs) {529OS << LS;530MemLoc.Ptr->printAsOperand(OS << "(");531if (MemLoc.Size == LocationSize::afterPointer())532OS << ", unknown after)";533else if (MemLoc.Size == LocationSize::beforeOrAfterPointer())534OS << ", unknown before-or-after)";535else536OS << ", " << MemLoc.Size << ")";537}538}539if (!UnknownInsts.empty()) {540ListSeparator LS;541OS << "\n " << UnknownInsts.size() << " Unknown instructions: ";542for (Instruction *I : UnknownInsts) {543OS << LS;544if (I->hasName())545I->printAsOperand(OS);546else547I->print(OS);548}549}550OS << "\n";551}552553void AliasSetTracker::print(raw_ostream &OS) const {554OS << "Alias Set Tracker: " << AliasSets.size();555if (AliasAnyAS)556OS << " (Saturated)";557OS << " alias sets for " << PointerMap.size() << " pointer values.\n";558for (const AliasSet &AS : *this)559AS.print(OS);560OS << "\n";561}562563#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)564LLVM_DUMP_METHOD void AliasSet::dump() const { print(dbgs()); }565LLVM_DUMP_METHOD void AliasSetTracker::dump() const { print(dbgs()); }566#endif567568//===----------------------------------------------------------------------===//569// AliasSetPrinter Pass570//===----------------------------------------------------------------------===//571572AliasSetsPrinterPass::AliasSetsPrinterPass(raw_ostream &OS) : OS(OS) {}573574PreservedAnalyses AliasSetsPrinterPass::run(Function &F,575FunctionAnalysisManager &AM) {576auto &AA = AM.getResult<AAManager>(F);577BatchAAResults BatchAA(AA);578AliasSetTracker Tracker(BatchAA);579OS << "Alias sets for function '" << F.getName() << "':\n";580for (Instruction &I : instructions(F))581Tracker.add(&I);582Tracker.print(OS);583return PreservedAnalyses::all();584}585586587