llvm-project
1828 строк · 71.6 Кб
1//===- MemoryDependenceAnalysis.cpp - Mem Deps Implementation -------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements an analysis that determines, for a given memory
10// operation, what preceding memory operations it depends on. It builds on
11// alias analysis information, and tries to provide a lazy, caching interface to
12// a common kind of alias information query.
13//
14//===----------------------------------------------------------------------===//
15
16#include "llvm/Analysis/MemoryDependenceAnalysis.h"17#include "llvm/ADT/DenseMap.h"18#include "llvm/ADT/STLExtras.h"19#include "llvm/ADT/SmallPtrSet.h"20#include "llvm/ADT/SmallVector.h"21#include "llvm/ADT/Statistic.h"22#include "llvm/Analysis/AliasAnalysis.h"23#include "llvm/Analysis/AssumptionCache.h"24#include "llvm/Analysis/MemoryBuiltins.h"25#include "llvm/Analysis/MemoryLocation.h"26#include "llvm/Analysis/PHITransAddr.h"27#include "llvm/Analysis/TargetLibraryInfo.h"28#include "llvm/Analysis/ValueTracking.h"29#include "llvm/IR/BasicBlock.h"30#include "llvm/IR/Dominators.h"31#include "llvm/IR/Function.h"32#include "llvm/IR/InstrTypes.h"33#include "llvm/IR/Instruction.h"34#include "llvm/IR/Instructions.h"35#include "llvm/IR/IntrinsicInst.h"36#include "llvm/IR/LLVMContext.h"37#include "llvm/IR/Metadata.h"38#include "llvm/IR/Module.h"39#include "llvm/IR/PredIteratorCache.h"40#include "llvm/IR/Type.h"41#include "llvm/IR/Use.h"42#include "llvm/IR/Value.h"43#include "llvm/InitializePasses.h"44#include "llvm/Pass.h"45#include "llvm/Support/AtomicOrdering.h"46#include "llvm/Support/Casting.h"47#include "llvm/Support/CommandLine.h"48#include "llvm/Support/Compiler.h"49#include "llvm/Support/Debug.h"50#include <algorithm>51#include <cassert>52#include <iterator>53#include <utility>54
55using namespace llvm;56
57#define DEBUG_TYPE "memdep"58
59STATISTIC(NumCacheNonLocal, "Number of fully cached non-local responses");60STATISTIC(NumCacheDirtyNonLocal, "Number of dirty cached non-local responses");61STATISTIC(NumUncacheNonLocal, "Number of uncached non-local responses");62
63STATISTIC(NumCacheNonLocalPtr,64"Number of fully cached non-local ptr responses");65STATISTIC(NumCacheDirtyNonLocalPtr,66"Number of cached, but dirty, non-local ptr responses");67STATISTIC(NumUncacheNonLocalPtr, "Number of uncached non-local ptr responses");68STATISTIC(NumCacheCompleteNonLocalPtr,69"Number of block queries that were completely cached");70
71// Limit for the number of instructions to scan in a block.
72
73static cl::opt<unsigned> BlockScanLimit(74"memdep-block-scan-limit", cl::Hidden, cl::init(100),75cl::desc("The number of instructions to scan in a block in memory "76"dependency analysis (default = 100)"));77
78static cl::opt<unsigned>79BlockNumberLimit("memdep-block-number-limit", cl::Hidden, cl::init(200),80cl::desc("The number of blocks to scan during memory "81"dependency analysis (default = 200)"));82
83// Limit on the number of memdep results to process.
84static const unsigned int NumResultsLimit = 100;85
86/// This is a helper function that removes Val from 'Inst's set in ReverseMap.
87///
88/// If the set becomes empty, remove Inst's entry.
89template <typename KeyTy>90static void91RemoveFromReverseMap(DenseMap<Instruction *, SmallPtrSet<KeyTy, 4>> &ReverseMap,92Instruction *Inst, KeyTy Val) {93typename DenseMap<Instruction *, SmallPtrSet<KeyTy, 4>>::iterator InstIt =94ReverseMap.find(Inst);95assert(InstIt != ReverseMap.end() && "Reverse map out of sync?");96bool Found = InstIt->second.erase(Val);97assert(Found && "Invalid reverse map!");98(void)Found;99if (InstIt->second.empty())100ReverseMap.erase(InstIt);101}
102
103/// If the given instruction references a specific memory location, fill in Loc
104/// with the details, otherwise set Loc.Ptr to null.
105///
106/// Returns a ModRefInfo value describing the general behavior of the
107/// instruction.
108static ModRefInfo GetLocation(const Instruction *Inst, MemoryLocation &Loc,109const TargetLibraryInfo &TLI) {110if (const LoadInst *LI = dyn_cast<LoadInst>(Inst)) {111if (LI->isUnordered()) {112Loc = MemoryLocation::get(LI);113return ModRefInfo::Ref;114}115if (LI->getOrdering() == AtomicOrdering::Monotonic) {116Loc = MemoryLocation::get(LI);117return ModRefInfo::ModRef;118}119Loc = MemoryLocation();120return ModRefInfo::ModRef;121}122
123if (const StoreInst *SI = dyn_cast<StoreInst>(Inst)) {124if (SI->isUnordered()) {125Loc = MemoryLocation::get(SI);126return ModRefInfo::Mod;127}128if (SI->getOrdering() == AtomicOrdering::Monotonic) {129Loc = MemoryLocation::get(SI);130return ModRefInfo::ModRef;131}132Loc = MemoryLocation();133return ModRefInfo::ModRef;134}135
136if (const VAArgInst *V = dyn_cast<VAArgInst>(Inst)) {137Loc = MemoryLocation::get(V);138return ModRefInfo::ModRef;139}140
141if (const CallBase *CB = dyn_cast<CallBase>(Inst)) {142if (Value *FreedOp = getFreedOperand(CB, &TLI)) {143// calls to free() deallocate the entire structure144Loc = MemoryLocation::getAfter(FreedOp);145return ModRefInfo::Mod;146}147}148
149if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(Inst)) {150switch (II->getIntrinsicID()) {151case Intrinsic::lifetime_start:152case Intrinsic::lifetime_end:153case Intrinsic::invariant_start:154Loc = MemoryLocation::getForArgument(II, 1, TLI);155// These intrinsics don't really modify the memory, but returning Mod156// will allow them to be handled conservatively.157return ModRefInfo::Mod;158case Intrinsic::invariant_end:159Loc = MemoryLocation::getForArgument(II, 2, TLI);160// These intrinsics don't really modify the memory, but returning Mod161// will allow them to be handled conservatively.162return ModRefInfo::Mod;163case Intrinsic::masked_load:164Loc = MemoryLocation::getForArgument(II, 0, TLI);165return ModRefInfo::Ref;166case Intrinsic::masked_store:167Loc = MemoryLocation::getForArgument(II, 1, TLI);168return ModRefInfo::Mod;169default:170break;171}172}173
174// Otherwise, just do the coarse-grained thing that always works.175if (Inst->mayWriteToMemory())176return ModRefInfo::ModRef;177if (Inst->mayReadFromMemory())178return ModRefInfo::Ref;179return ModRefInfo::NoModRef;180}
181
182/// Private helper for finding the local dependencies of a call site.
183MemDepResult MemoryDependenceResults::getCallDependencyFrom(184CallBase *Call, bool isReadOnlyCall, BasicBlock::iterator ScanIt,185BasicBlock *BB) {186unsigned Limit = getDefaultBlockScanLimit();187
188// Walk backwards through the block, looking for dependencies.189while (ScanIt != BB->begin()) {190Instruction *Inst = &*--ScanIt;191// Debug intrinsics don't cause dependences and should not affect Limit192if (isa<DbgInfoIntrinsic>(Inst))193continue;194
195// Limit the amount of scanning we do so we don't end up with quadratic196// running time on extreme testcases.197--Limit;198if (!Limit)199return MemDepResult::getUnknown();200
201// If this inst is a memory op, get the pointer it accessed202MemoryLocation Loc;203ModRefInfo MR = GetLocation(Inst, Loc, TLI);204if (Loc.Ptr) {205// A simple instruction.206if (isModOrRefSet(AA.getModRefInfo(Call, Loc)))207return MemDepResult::getClobber(Inst);208continue;209}210
211if (auto *CallB = dyn_cast<CallBase>(Inst)) {212// If these two calls do not interfere, look past it.213if (isNoModRef(AA.getModRefInfo(Call, CallB))) {214// If the two calls are the same, return Inst as a Def, so that215// Call can be found redundant and eliminated.216if (isReadOnlyCall && !isModSet(MR) &&217Call->isIdenticalToWhenDefined(CallB))218return MemDepResult::getDef(Inst);219
220// Otherwise if the two calls don't interact (e.g. CallB is readnone)221// keep scanning.222continue;223} else224return MemDepResult::getClobber(Inst);225}226
227// If we could not obtain a pointer for the instruction and the instruction228// touches memory then assume that this is a dependency.229if (isModOrRefSet(MR))230return MemDepResult::getClobber(Inst);231}232
233// No dependence found. If this is the entry block of the function, it is234// unknown, otherwise it is non-local.235if (BB != &BB->getParent()->getEntryBlock())236return MemDepResult::getNonLocal();237return MemDepResult::getNonFuncLocal();238}
239
240MemDepResult MemoryDependenceResults::getPointerDependencyFrom(241const MemoryLocation &MemLoc, bool isLoad, BasicBlock::iterator ScanIt,242BasicBlock *BB, Instruction *QueryInst, unsigned *Limit,243BatchAAResults &BatchAA) {244MemDepResult InvariantGroupDependency = MemDepResult::getUnknown();245if (QueryInst != nullptr) {246if (auto *LI = dyn_cast<LoadInst>(QueryInst)) {247InvariantGroupDependency = getInvariantGroupPointerDependency(LI, BB);248
249if (InvariantGroupDependency.isDef())250return InvariantGroupDependency;251}252}253MemDepResult SimpleDep = getSimplePointerDependencyFrom(254MemLoc, isLoad, ScanIt, BB, QueryInst, Limit, BatchAA);255if (SimpleDep.isDef())256return SimpleDep;257// Non-local invariant group dependency indicates there is non local Def258// (it only returns nonLocal if it finds nonLocal def), which is better than259// local clobber and everything else.260if (InvariantGroupDependency.isNonLocal())261return InvariantGroupDependency;262
263assert(InvariantGroupDependency.isUnknown() &&264"InvariantGroupDependency should be only unknown at this point");265return SimpleDep;266}
267
268MemDepResult MemoryDependenceResults::getPointerDependencyFrom(269const MemoryLocation &MemLoc, bool isLoad, BasicBlock::iterator ScanIt,270BasicBlock *BB, Instruction *QueryInst, unsigned *Limit) {271BatchAAResults BatchAA(AA, &EII);272return getPointerDependencyFrom(MemLoc, isLoad, ScanIt, BB, QueryInst, Limit,273BatchAA);274}
275
276MemDepResult
277MemoryDependenceResults::getInvariantGroupPointerDependency(LoadInst *LI,278BasicBlock *BB) {279
280if (!LI->hasMetadata(LLVMContext::MD_invariant_group))281return MemDepResult::getUnknown();282
283// Take the ptr operand after all casts and geps 0. This way we can search284// cast graph down only.285Value *LoadOperand = LI->getPointerOperand()->stripPointerCasts();286
287// It's is not safe to walk the use list of global value, because function288// passes aren't allowed to look outside their functions.289// FIXME: this could be fixed by filtering instructions from outside290// of current function.291if (isa<GlobalValue>(LoadOperand))292return MemDepResult::getUnknown();293
294// Queue to process all pointers that are equivalent to load operand.295SmallVector<const Value *, 8> LoadOperandsQueue;296LoadOperandsQueue.push_back(LoadOperand);297
298Instruction *ClosestDependency = nullptr;299// Order of instructions in uses list is unpredictible. In order to always300// get the same result, we will look for the closest dominance.301auto GetClosestDependency = [this](Instruction *Best, Instruction *Other) {302assert(Other && "Must call it with not null instruction");303if (Best == nullptr || DT.dominates(Best, Other))304return Other;305return Best;306};307
308// FIXME: This loop is O(N^2) because dominates can be O(n) and in worst case309// we will see all the instructions. This should be fixed in MSSA.310while (!LoadOperandsQueue.empty()) {311const Value *Ptr = LoadOperandsQueue.pop_back_val();312assert(Ptr && !isa<GlobalValue>(Ptr) &&313"Null or GlobalValue should not be inserted");314
315for (const Use &Us : Ptr->uses()) {316auto *U = dyn_cast<Instruction>(Us.getUser());317if (!U || U == LI || !DT.dominates(U, LI))318continue;319
320// Bitcast or gep with zeros are using Ptr. Add to queue to check it's321// users. U = bitcast Ptr322if (isa<BitCastInst>(U)) {323LoadOperandsQueue.push_back(U);324continue;325}326// Gep with zeros is equivalent to bitcast.327// FIXME: we are not sure if some bitcast should be canonicalized to gep 0328// or gep 0 to bitcast because of SROA, so there are 2 forms. When329// typeless pointers will be ready then both cases will be gone330// (and this BFS also won't be needed).331if (auto *GEP = dyn_cast<GetElementPtrInst>(U))332if (GEP->hasAllZeroIndices()) {333LoadOperandsQueue.push_back(U);334continue;335}336
337// If we hit load/store with the same invariant.group metadata (and the338// same pointer operand) we can assume that value pointed by pointer339// operand didn't change.340if ((isa<LoadInst>(U) ||341(isa<StoreInst>(U) &&342cast<StoreInst>(U)->getPointerOperand() == Ptr)) &&343U->hasMetadata(LLVMContext::MD_invariant_group))344ClosestDependency = GetClosestDependency(ClosestDependency, U);345}346}347
348if (!ClosestDependency)349return MemDepResult::getUnknown();350if (ClosestDependency->getParent() == BB)351return MemDepResult::getDef(ClosestDependency);352// Def(U) can't be returned here because it is non-local. If local353// dependency won't be found then return nonLocal counting that the354// user will call getNonLocalPointerDependency, which will return cached355// result.356NonLocalDefsCache.try_emplace(357LI, NonLocalDepResult(ClosestDependency->getParent(),358MemDepResult::getDef(ClosestDependency), nullptr));359ReverseNonLocalDefsCache[ClosestDependency].insert(LI);360return MemDepResult::getNonLocal();361}
362
363// Check if SI that may alias with MemLoc can be safely skipped. This is
364// possible in case if SI can only must alias or no alias with MemLoc (no
365// partial overlapping possible) and it writes the same value that MemLoc
366// contains now (it was loaded before this store and was not modified in
367// between).
368static bool canSkipClobberingStore(const StoreInst *SI,369const MemoryLocation &MemLoc,370Align MemLocAlign, BatchAAResults &BatchAA,371unsigned ScanLimit) {372if (!MemLoc.Size.hasValue())373return false;374if (MemoryLocation::get(SI).Size != MemLoc.Size)375return false;376if (MemLoc.Size.isScalable())377return false;378if (std::min(MemLocAlign, SI->getAlign()).value() <379MemLoc.Size.getValue().getKnownMinValue())380return false;381
382auto *LI = dyn_cast<LoadInst>(SI->getValueOperand());383if (!LI || LI->getParent() != SI->getParent())384return false;385if (BatchAA.alias(MemoryLocation::get(LI), MemLoc) != AliasResult::MustAlias)386return false;387unsigned NumVisitedInsts = 0;388for (const Instruction *I = LI; I != SI; I = I->getNextNonDebugInstruction())389if (++NumVisitedInsts > ScanLimit ||390isModSet(BatchAA.getModRefInfo(I, MemLoc)))391return false;392
393return true;394}
395
396MemDepResult MemoryDependenceResults::getSimplePointerDependencyFrom(397const MemoryLocation &MemLoc, bool isLoad, BasicBlock::iterator ScanIt,398BasicBlock *BB, Instruction *QueryInst, unsigned *Limit,399BatchAAResults &BatchAA) {400bool isInvariantLoad = false;401Align MemLocAlign =402MemLoc.Ptr->getPointerAlignment(BB->getDataLayout());403
404unsigned DefaultLimit = getDefaultBlockScanLimit();405if (!Limit)406Limit = &DefaultLimit;407
408// We must be careful with atomic accesses, as they may allow another thread409// to touch this location, clobbering it. We are conservative: if the410// QueryInst is not a simple (non-atomic) memory access, we automatically411// return getClobber.412// If it is simple, we know based on the results of413// "Compiler testing via a theory of sound optimisations in the C11/C++11414// memory model" in PLDI 2013, that a non-atomic location can only be415// clobbered between a pair of a release and an acquire action, with no416// access to the location in between.417// Here is an example for giving the general intuition behind this rule.418// In the following code:419// store x 0;420// release action; [1]421// acquire action; [4]422// %val = load x;423// It is unsafe to replace %val by 0 because another thread may be running:424// acquire action; [2]425// store x 42;426// release action; [3]427// with synchronization from 1 to 2 and from 3 to 4, resulting in %val428// being 42. A key property of this program however is that if either429// 1 or 4 were missing, there would be a race between the store of 42430// either the store of 0 or the load (making the whole program racy).431// The paper mentioned above shows that the same property is respected432// by every program that can detect any optimization of that kind: either433// it is racy (undefined) or there is a release followed by an acquire434// between the pair of accesses under consideration.435
436// If the load is invariant, we "know" that it doesn't alias *any* write. We437// do want to respect mustalias results since defs are useful for value438// forwarding, but any mayalias write can be assumed to be noalias.439// Arguably, this logic should be pushed inside AliasAnalysis itself.440if (isLoad && QueryInst)441if (LoadInst *LI = dyn_cast<LoadInst>(QueryInst)) {442if (LI->hasMetadata(LLVMContext::MD_invariant_load))443isInvariantLoad = true;444MemLocAlign = LI->getAlign();445}446
447// True for volatile instruction.448// For Load/Store return true if atomic ordering is stronger than AO,449// for other instruction just true if it can read or write to memory.450auto isComplexForReordering = [](Instruction * I, AtomicOrdering AO)->bool {451if (I->isVolatile())452return true;453if (auto *LI = dyn_cast<LoadInst>(I))454return isStrongerThan(LI->getOrdering(), AO);455if (auto *SI = dyn_cast<StoreInst>(I))456return isStrongerThan(SI->getOrdering(), AO);457return I->mayReadOrWriteMemory();458};459
460// Walk backwards through the basic block, looking for dependencies.461while (ScanIt != BB->begin()) {462Instruction *Inst = &*--ScanIt;463
464if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(Inst))465// Debug intrinsics don't (and can't) cause dependencies.466if (isa<DbgInfoIntrinsic>(II))467continue;468
469// Limit the amount of scanning we do so we don't end up with quadratic470// running time on extreme testcases.471--*Limit;472if (!*Limit)473return MemDepResult::getUnknown();474
475if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(Inst)) {476// If we reach a lifetime begin or end marker, then the query ends here477// because the value is undefined.478Intrinsic::ID ID = II->getIntrinsicID();479switch (ID) {480case Intrinsic::lifetime_start: {481// FIXME: This only considers queries directly on the invariant-tagged482// pointer, not on query pointers that are indexed off of them. It'd483// be nice to handle that at some point (the right approach is to use484// GetPointerBaseWithConstantOffset).485MemoryLocation ArgLoc = MemoryLocation::getAfter(II->getArgOperand(1));486if (BatchAA.isMustAlias(ArgLoc, MemLoc))487return MemDepResult::getDef(II);488continue;489}490case Intrinsic::masked_load:491case Intrinsic::masked_store: {492MemoryLocation Loc;493/*ModRefInfo MR =*/ GetLocation(II, Loc, TLI);494AliasResult R = BatchAA.alias(Loc, MemLoc);495if (R == AliasResult::NoAlias)496continue;497if (R == AliasResult::MustAlias)498return MemDepResult::getDef(II);499if (ID == Intrinsic::masked_load)500continue;501return MemDepResult::getClobber(II);502}503}504}505
506// Values depend on loads if the pointers are must aliased. This means507// that a load depends on another must aliased load from the same value.508// One exception is atomic loads: a value can depend on an atomic load that509// it does not alias with when this atomic load indicates that another510// thread may be accessing the location.511if (LoadInst *LI = dyn_cast<LoadInst>(Inst)) {512// While volatile access cannot be eliminated, they do not have to clobber513// non-aliasing locations, as normal accesses, for example, can be safely514// reordered with volatile accesses.515if (LI->isVolatile()) {516if (!QueryInst)517// Original QueryInst *may* be volatile518return MemDepResult::getClobber(LI);519if (QueryInst->isVolatile())520// Ordering required if QueryInst is itself volatile521return MemDepResult::getClobber(LI);522// Otherwise, volatile doesn't imply any special ordering523}524
525// Atomic loads have complications involved.526// A Monotonic (or higher) load is OK if the query inst is itself not527// atomic.528// FIXME: This is overly conservative.529if (LI->isAtomic() && isStrongerThanUnordered(LI->getOrdering())) {530if (!QueryInst ||531isComplexForReordering(QueryInst, AtomicOrdering::NotAtomic))532return MemDepResult::getClobber(LI);533if (LI->getOrdering() != AtomicOrdering::Monotonic)534return MemDepResult::getClobber(LI);535}536
537MemoryLocation LoadLoc = MemoryLocation::get(LI);538
539// If we found a pointer, check if it could be the same as our pointer.540AliasResult R = BatchAA.alias(LoadLoc, MemLoc);541
542if (R == AliasResult::NoAlias)543continue;544
545if (isLoad) {546// Must aliased loads are defs of each other.547if (R == AliasResult::MustAlias)548return MemDepResult::getDef(Inst);549
550// If we have a partial alias, then return this as a clobber for the551// client to handle.552if (R == AliasResult::PartialAlias && R.hasOffset()) {553ClobberOffsets[LI] = R.getOffset();554return MemDepResult::getClobber(Inst);555}556
557// Random may-alias loads don't depend on each other without a558// dependence.559continue;560}561
562// Stores don't alias loads from read-only memory.563if (!isModSet(BatchAA.getModRefInfoMask(LoadLoc)))564continue;565
566// Stores depend on may/must aliased loads.567return MemDepResult::getDef(Inst);568}569
570if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) {571// Atomic stores have complications involved.572// A Monotonic store is OK if the query inst is itself not atomic.573// FIXME: This is overly conservative.574if (!SI->isUnordered() && SI->isAtomic()) {575if (!QueryInst ||576isComplexForReordering(QueryInst, AtomicOrdering::Unordered))577return MemDepResult::getClobber(SI);578// Ok, if we are here the guard above guarantee us that579// QueryInst is a non-atomic or unordered load/store.580// SI is atomic with monotonic or release semantic (seq_cst for store581// is actually a release semantic plus total order over other seq_cst582// instructions, as soon as QueryInst is not seq_cst we can consider it583// as simple release semantic).584// Monotonic and Release semantic allows re-ordering before store585// so we are safe to go further and check the aliasing. It will prohibit586// re-ordering in case locations are may or must alias.587}588
589// While volatile access cannot be eliminated, they do not have to clobber590// non-aliasing locations, as normal accesses can for example be reordered591// with volatile accesses.592if (SI->isVolatile())593if (!QueryInst || QueryInst->isVolatile())594return MemDepResult::getClobber(SI);595
596// If alias analysis can tell that this store is guaranteed to not modify597// the query pointer, ignore it. Use getModRefInfo to handle cases where598// the query pointer points to constant memory etc.599if (!isModOrRefSet(BatchAA.getModRefInfo(SI, MemLoc)))600continue;601
602// Ok, this store might clobber the query pointer. Check to see if it is603// a must alias: in this case, we want to return this as a def.604// FIXME: Use ModRefInfo::Must bit from getModRefInfo call above.605MemoryLocation StoreLoc = MemoryLocation::get(SI);606
607// If we found a pointer, check if it could be the same as our pointer.608AliasResult R = BatchAA.alias(StoreLoc, MemLoc);609
610if (R == AliasResult::NoAlias)611continue;612if (R == AliasResult::MustAlias)613return MemDepResult::getDef(Inst);614if (isInvariantLoad)615continue;616if (canSkipClobberingStore(SI, MemLoc, MemLocAlign, BatchAA, *Limit))617continue;618return MemDepResult::getClobber(Inst);619}620
621// If this is an allocation, and if we know that the accessed pointer is to622// the allocation, return Def. This means that there is no dependence and623// the access can be optimized based on that. For example, a load could624// turn into undef. Note that we can bypass the allocation itself when625// looking for a clobber in many cases; that's an alias property and is626// handled by BasicAA.627if (isa<AllocaInst>(Inst) || isNoAliasCall(Inst)) {628const Value *AccessPtr = getUnderlyingObject(MemLoc.Ptr);629if (AccessPtr == Inst || BatchAA.isMustAlias(Inst, AccessPtr))630return MemDepResult::getDef(Inst);631}632
633// If we found a select instruction for MemLoc pointer, return it as Def634// dependency.635if (isa<SelectInst>(Inst) && MemLoc.Ptr == Inst)636return MemDepResult::getDef(Inst);637
638if (isInvariantLoad)639continue;640
641// A release fence requires that all stores complete before it, but does642// not prevent the reordering of following loads or stores 'before' the643// fence. As a result, we look past it when finding a dependency for644// loads. DSE uses this to find preceding stores to delete and thus we645// can't bypass the fence if the query instruction is a store.646if (FenceInst *FI = dyn_cast<FenceInst>(Inst))647if (isLoad && FI->getOrdering() == AtomicOrdering::Release)648continue;649
650// See if this instruction (e.g. a call or vaarg) mod/ref's the pointer.651switch (BatchAA.getModRefInfo(Inst, MemLoc)) {652case ModRefInfo::NoModRef:653// If the call has no effect on the queried pointer, just ignore it.654continue;655case ModRefInfo::Mod:656return MemDepResult::getClobber(Inst);657case ModRefInfo::Ref:658// If the call is known to never store to the pointer, and if this is a659// load query, we can safely ignore it (scan past it).660if (isLoad)661continue;662[[fallthrough]];663default:664// Otherwise, there is a potential dependence. Return a clobber.665return MemDepResult::getClobber(Inst);666}667}668
669// No dependence found. If this is the entry block of the function, it is670// unknown, otherwise it is non-local.671if (BB != &BB->getParent()->getEntryBlock())672return MemDepResult::getNonLocal();673return MemDepResult::getNonFuncLocal();674}
675
676MemDepResult MemoryDependenceResults::getDependency(Instruction *QueryInst) {677ClobberOffsets.clear();678Instruction *ScanPos = QueryInst;679
680// Check for a cached result681MemDepResult &LocalCache = LocalDeps[QueryInst];682
683// If the cached entry is non-dirty, just return it. Note that this depends684// on MemDepResult's default constructing to 'dirty'.685if (!LocalCache.isDirty())686return LocalCache;687
688// Otherwise, if we have a dirty entry, we know we can start the scan at that689// instruction, which may save us some work.690if (Instruction *Inst = LocalCache.getInst()) {691ScanPos = Inst;692
693RemoveFromReverseMap(ReverseLocalDeps, Inst, QueryInst);694}695
696BasicBlock *QueryParent = QueryInst->getParent();697
698// Do the scan.699if (BasicBlock::iterator(QueryInst) == QueryParent->begin()) {700// No dependence found. If this is the entry block of the function, it is701// unknown, otherwise it is non-local.702if (QueryParent != &QueryParent->getParent()->getEntryBlock())703LocalCache = MemDepResult::getNonLocal();704else705LocalCache = MemDepResult::getNonFuncLocal();706} else {707MemoryLocation MemLoc;708ModRefInfo MR = GetLocation(QueryInst, MemLoc, TLI);709if (MemLoc.Ptr) {710// If we can do a pointer scan, make it happen.711bool isLoad = !isModSet(MR);712if (auto *II = dyn_cast<IntrinsicInst>(QueryInst))713isLoad |= II->getIntrinsicID() == Intrinsic::lifetime_start;714
715LocalCache =716getPointerDependencyFrom(MemLoc, isLoad, ScanPos->getIterator(),717QueryParent, QueryInst, nullptr);718} else if (auto *QueryCall = dyn_cast<CallBase>(QueryInst)) {719bool isReadOnly = AA.onlyReadsMemory(QueryCall);720LocalCache = getCallDependencyFrom(QueryCall, isReadOnly,721ScanPos->getIterator(), QueryParent);722} else723// Non-memory instruction.724LocalCache = MemDepResult::getUnknown();725}726
727// Remember the result!728if (Instruction *I = LocalCache.getInst())729ReverseLocalDeps[I].insert(QueryInst);730
731return LocalCache;732}
733
734#ifndef NDEBUG735/// This method is used when -debug is specified to verify that cache arrays
736/// are properly kept sorted.
737static void AssertSorted(MemoryDependenceResults::NonLocalDepInfo &Cache,738int Count = -1) {739if (Count == -1)740Count = Cache.size();741assert(std::is_sorted(Cache.begin(), Cache.begin() + Count) &&742"Cache isn't sorted!");743}
744#endif745
746const MemoryDependenceResults::NonLocalDepInfo &747MemoryDependenceResults::getNonLocalCallDependency(CallBase *QueryCall) {748assert(getDependency(QueryCall).isNonLocal() &&749"getNonLocalCallDependency should only be used on calls with "750"non-local deps!");751PerInstNLInfo &CacheP = NonLocalDepsMap[QueryCall];752NonLocalDepInfo &Cache = CacheP.first;753
754// This is the set of blocks that need to be recomputed. In the cached case,755// this can happen due to instructions being deleted etc. In the uncached756// case, this starts out as the set of predecessors we care about.757SmallVector<BasicBlock *, 32> DirtyBlocks;758
759if (!Cache.empty()) {760// Okay, we have a cache entry. If we know it is not dirty, just return it761// with no computation.762if (!CacheP.second) {763++NumCacheNonLocal;764return Cache;765}766
767// If we already have a partially computed set of results, scan them to768// determine what is dirty, seeding our initial DirtyBlocks worklist.769for (auto &Entry : Cache)770if (Entry.getResult().isDirty())771DirtyBlocks.push_back(Entry.getBB());772
773// Sort the cache so that we can do fast binary search lookups below.774llvm::sort(Cache);775
776++NumCacheDirtyNonLocal;777} else {778// Seed DirtyBlocks with each of the preds of QueryInst's block.779BasicBlock *QueryBB = QueryCall->getParent();780append_range(DirtyBlocks, PredCache.get(QueryBB));781++NumUncacheNonLocal;782}783
784// isReadonlyCall - If this is a read-only call, we can be more aggressive.785bool isReadonlyCall = AA.onlyReadsMemory(QueryCall);786
787SmallPtrSet<BasicBlock *, 32> Visited;788
789unsigned NumSortedEntries = Cache.size();790LLVM_DEBUG(AssertSorted(Cache));791
792// Iterate while we still have blocks to update.793while (!DirtyBlocks.empty()) {794BasicBlock *DirtyBB = DirtyBlocks.pop_back_val();795
796// Already processed this block?797if (!Visited.insert(DirtyBB).second)798continue;799
800// Do a binary search to see if we already have an entry for this block in801// the cache set. If so, find it.802LLVM_DEBUG(AssertSorted(Cache, NumSortedEntries));803NonLocalDepInfo::iterator Entry =804std::upper_bound(Cache.begin(), Cache.begin() + NumSortedEntries,805NonLocalDepEntry(DirtyBB));806if (Entry != Cache.begin() && std::prev(Entry)->getBB() == DirtyBB)807--Entry;808
809NonLocalDepEntry *ExistingResult = nullptr;810if (Entry != Cache.begin() + NumSortedEntries &&811Entry->getBB() == DirtyBB) {812// If we already have an entry, and if it isn't already dirty, the block813// is done.814if (!Entry->getResult().isDirty())815continue;816
817// Otherwise, remember this slot so we can update the value.818ExistingResult = &*Entry;819}820
821// If the dirty entry has a pointer, start scanning from it so we don't have822// to rescan the entire block.823BasicBlock::iterator ScanPos = DirtyBB->end();824if (ExistingResult) {825if (Instruction *Inst = ExistingResult->getResult().getInst()) {826ScanPos = Inst->getIterator();827// We're removing QueryInst's use of Inst.828RemoveFromReverseMap<Instruction *>(ReverseNonLocalDeps, Inst,829QueryCall);830}831}832
833// Find out if this block has a local dependency for QueryInst.834MemDepResult Dep;835
836if (ScanPos != DirtyBB->begin()) {837Dep = getCallDependencyFrom(QueryCall, isReadonlyCall, ScanPos, DirtyBB);838} else if (DirtyBB != &DirtyBB->getParent()->getEntryBlock()) {839// No dependence found. If this is the entry block of the function, it is840// a clobber, otherwise it is unknown.841Dep = MemDepResult::getNonLocal();842} else {843Dep = MemDepResult::getNonFuncLocal();844}845
846// If we had a dirty entry for the block, update it. Otherwise, just add847// a new entry.848if (ExistingResult)849ExistingResult->setResult(Dep);850else851Cache.push_back(NonLocalDepEntry(DirtyBB, Dep));852
853// If the block has a dependency (i.e. it isn't completely transparent to854// the value), remember the association!855if (!Dep.isNonLocal()) {856// Keep the ReverseNonLocalDeps map up to date so we can efficiently857// update this when we remove instructions.858if (Instruction *Inst = Dep.getInst())859ReverseNonLocalDeps[Inst].insert(QueryCall);860} else {861
862// If the block *is* completely transparent to the load, we need to check863// the predecessors of this block. Add them to our worklist.864append_range(DirtyBlocks, PredCache.get(DirtyBB));865}866}867
868return Cache;869}
870
871void MemoryDependenceResults::getNonLocalPointerDependency(872Instruction *QueryInst, SmallVectorImpl<NonLocalDepResult> &Result) {873const MemoryLocation Loc = MemoryLocation::get(QueryInst);874bool isLoad = isa<LoadInst>(QueryInst);875BasicBlock *FromBB = QueryInst->getParent();876assert(FromBB);877
878assert(Loc.Ptr->getType()->isPointerTy() &&879"Can't get pointer deps of a non-pointer!");880Result.clear();881{882// Check if there is cached Def with invariant.group.883auto NonLocalDefIt = NonLocalDefsCache.find(QueryInst);884if (NonLocalDefIt != NonLocalDefsCache.end()) {885Result.push_back(NonLocalDefIt->second);886ReverseNonLocalDefsCache[NonLocalDefIt->second.getResult().getInst()]887.erase(QueryInst);888NonLocalDefsCache.erase(NonLocalDefIt);889return;890}891}892// This routine does not expect to deal with volatile instructions.893// Doing so would require piping through the QueryInst all the way through.894// TODO: volatiles can't be elided, but they can be reordered with other895// non-volatile accesses.896
897// We currently give up on any instruction which is ordered, but we do handle898// atomic instructions which are unordered.899// TODO: Handle ordered instructions900auto isOrdered = [](Instruction *Inst) {901if (LoadInst *LI = dyn_cast<LoadInst>(Inst)) {902return !LI->isUnordered();903} else if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) {904return !SI->isUnordered();905}906return false;907};908if (QueryInst->isVolatile() || isOrdered(QueryInst)) {909Result.push_back(NonLocalDepResult(FromBB, MemDepResult::getUnknown(),910const_cast<Value *>(Loc.Ptr)));911return;912}913const DataLayout &DL = FromBB->getDataLayout();914PHITransAddr Address(const_cast<Value *>(Loc.Ptr), DL, &AC);915
916// This is the set of blocks we've inspected, and the pointer we consider in917// each block. Because of critical edges, we currently bail out if querying918// a block with multiple different pointers. This can happen during PHI919// translation.920DenseMap<BasicBlock *, Value *> Visited;921if (getNonLocalPointerDepFromBB(QueryInst, Address, Loc, isLoad, FromBB,922Result, Visited, true))923return;924Result.clear();925Result.push_back(NonLocalDepResult(FromBB, MemDepResult::getUnknown(),926const_cast<Value *>(Loc.Ptr)));927}
928
929/// Compute the memdep value for BB with Pointer/PointeeSize using either
930/// cached information in Cache or by doing a lookup (which may use dirty cache
931/// info if available).
932///
933/// If we do a lookup, add the result to the cache.
934MemDepResult MemoryDependenceResults::getNonLocalInfoForBlock(935Instruction *QueryInst, const MemoryLocation &Loc, bool isLoad,936BasicBlock *BB, NonLocalDepInfo *Cache, unsigned NumSortedEntries,937BatchAAResults &BatchAA) {938
939bool isInvariantLoad = false;940
941if (LoadInst *LI = dyn_cast_or_null<LoadInst>(QueryInst))942isInvariantLoad = LI->getMetadata(LLVMContext::MD_invariant_load);943
944// Do a binary search to see if we already have an entry for this block in945// the cache set. If so, find it.946NonLocalDepInfo::iterator Entry = std::upper_bound(947Cache->begin(), Cache->begin() + NumSortedEntries, NonLocalDepEntry(BB));948if (Entry != Cache->begin() && (Entry - 1)->getBB() == BB)949--Entry;950
951NonLocalDepEntry *ExistingResult = nullptr;952if (Entry != Cache->begin() + NumSortedEntries && Entry->getBB() == BB)953ExistingResult = &*Entry;954
955// Use cached result for invariant load only if there is no dependency for non956// invariant load. In this case invariant load can not have any dependency as957// well.958if (ExistingResult && isInvariantLoad &&959!ExistingResult->getResult().isNonFuncLocal())960ExistingResult = nullptr;961
962// If we have a cached entry, and it is non-dirty, use it as the value for963// this dependency.964if (ExistingResult && !ExistingResult->getResult().isDirty()) {965++NumCacheNonLocalPtr;966return ExistingResult->getResult();967}968
969// Otherwise, we have to scan for the value. If we have a dirty cache970// entry, start scanning from its position, otherwise we scan from the end971// of the block.972BasicBlock::iterator ScanPos = BB->end();973if (ExistingResult && ExistingResult->getResult().getInst()) {974assert(ExistingResult->getResult().getInst()->getParent() == BB &&975"Instruction invalidated?");976++NumCacheDirtyNonLocalPtr;977ScanPos = ExistingResult->getResult().getInst()->getIterator();978
979// Eliminating the dirty entry from 'Cache', so update the reverse info.980ValueIsLoadPair CacheKey(Loc.Ptr, isLoad);981RemoveFromReverseMap(ReverseNonLocalPtrDeps, &*ScanPos, CacheKey);982} else {983++NumUncacheNonLocalPtr;984}985
986// Scan the block for the dependency.987MemDepResult Dep = getPointerDependencyFrom(Loc, isLoad, ScanPos, BB,988QueryInst, nullptr, BatchAA);989
990// Don't cache results for invariant load.991if (isInvariantLoad)992return Dep;993
994// If we had a dirty entry for the block, update it. Otherwise, just add995// a new entry.996if (ExistingResult)997ExistingResult->setResult(Dep);998else999Cache->push_back(NonLocalDepEntry(BB, Dep));1000
1001// If the block has a dependency (i.e. it isn't completely transparent to1002// the value), remember the reverse association because we just added it1003// to Cache!1004if (!Dep.isLocal())1005return Dep;1006
1007// Keep the ReverseNonLocalPtrDeps map up to date so we can efficiently1008// update MemDep when we remove instructions.1009Instruction *Inst = Dep.getInst();1010assert(Inst && "Didn't depend on anything?");1011ValueIsLoadPair CacheKey(Loc.Ptr, isLoad);1012ReverseNonLocalPtrDeps[Inst].insert(CacheKey);1013return Dep;1014}
1015
1016/// Sort the NonLocalDepInfo cache, given a certain number of elements in the
1017/// array that are already properly ordered.
1018///
1019/// This is optimized for the case when only a few entries are added.
1020static void1021SortNonLocalDepInfoCache(MemoryDependenceResults::NonLocalDepInfo &Cache,1022unsigned NumSortedEntries) {1023switch (Cache.size() - NumSortedEntries) {1024case 0:1025// done, no new entries.1026break;1027case 2: {1028// Two new entries, insert the last one into place.1029NonLocalDepEntry Val = Cache.back();1030Cache.pop_back();1031MemoryDependenceResults::NonLocalDepInfo::iterator Entry =1032std::upper_bound(Cache.begin(), Cache.end() - 1, Val);1033Cache.insert(Entry, Val);1034[[fallthrough]];1035}1036case 1:1037// One new entry, Just insert the new value at the appropriate position.1038if (Cache.size() != 1) {1039NonLocalDepEntry Val = Cache.back();1040Cache.pop_back();1041MemoryDependenceResults::NonLocalDepInfo::iterator Entry =1042llvm::upper_bound(Cache, Val);1043Cache.insert(Entry, Val);1044}1045break;1046default:1047// Added many values, do a full scale sort.1048llvm::sort(Cache);1049break;1050}1051}
1052
1053/// Perform a dependency query based on pointer/pointeesize starting at the end
1054/// of StartBB.
1055///
1056/// Add any clobber/def results to the results vector and keep track of which
1057/// blocks are visited in 'Visited'.
1058///
1059/// This has special behavior for the first block queries (when SkipFirstBlock
1060/// is true). In this special case, it ignores the contents of the specified
1061/// block and starts returning dependence info for its predecessors.
1062///
1063/// This function returns true on success, or false to indicate that it could
1064/// not compute dependence information for some reason. This should be treated
1065/// as a clobber dependence on the first instruction in the predecessor block.
1066bool MemoryDependenceResults::getNonLocalPointerDepFromBB(1067Instruction *QueryInst, const PHITransAddr &Pointer,1068const MemoryLocation &Loc, bool isLoad, BasicBlock *StartBB,1069SmallVectorImpl<NonLocalDepResult> &Result,1070DenseMap<BasicBlock *, Value *> &Visited, bool SkipFirstBlock,1071bool IsIncomplete) {1072// Look up the cached info for Pointer.1073ValueIsLoadPair CacheKey(Pointer.getAddr(), isLoad);1074
1075// Set up a temporary NLPI value. If the map doesn't yet have an entry for1076// CacheKey, this value will be inserted as the associated value. Otherwise,1077// it'll be ignored, and we'll have to check to see if the cached size and1078// aa tags are consistent with the current query.1079NonLocalPointerInfo InitialNLPI;1080InitialNLPI.Size = Loc.Size;1081InitialNLPI.AATags = Loc.AATags;1082
1083bool isInvariantLoad = false;1084if (LoadInst *LI = dyn_cast_or_null<LoadInst>(QueryInst))1085isInvariantLoad = LI->getMetadata(LLVMContext::MD_invariant_load);1086
1087// Get the NLPI for CacheKey, inserting one into the map if it doesn't1088// already have one.1089std::pair<CachedNonLocalPointerInfo::iterator, bool> Pair =1090NonLocalPointerDeps.insert(std::make_pair(CacheKey, InitialNLPI));1091NonLocalPointerInfo *CacheInfo = &Pair.first->second;1092
1093// If we already have a cache entry for this CacheKey, we may need to do some1094// work to reconcile the cache entry and the current query.1095// Invariant loads don't participate in caching. Thus no need to reconcile.1096if (!isInvariantLoad && !Pair.second) {1097if (CacheInfo->Size != Loc.Size) {1098bool ThrowOutEverything;1099if (CacheInfo->Size.hasValue() && Loc.Size.hasValue()) {1100// FIXME: We may be able to do better in the face of results with mixed1101// precision. We don't appear to get them in practice, though, so just1102// be conservative.1103ThrowOutEverything =1104CacheInfo->Size.isPrecise() != Loc.Size.isPrecise() ||1105!TypeSize::isKnownGE(CacheInfo->Size.getValue(),1106Loc.Size.getValue());1107} else {1108// For our purposes, unknown size > all others.1109ThrowOutEverything = !Loc.Size.hasValue();1110}1111
1112if (ThrowOutEverything) {1113// The query's Size is greater than the cached one. Throw out the1114// cached data and proceed with the query at the greater size.1115CacheInfo->Pair = BBSkipFirstBlockPair();1116CacheInfo->Size = Loc.Size;1117for (auto &Entry : CacheInfo->NonLocalDeps)1118if (Instruction *Inst = Entry.getResult().getInst())1119RemoveFromReverseMap(ReverseNonLocalPtrDeps, Inst, CacheKey);1120CacheInfo->NonLocalDeps.clear();1121// The cache is cleared (in the above line) so we will have lost1122// information about blocks we have already visited. We therefore must1123// assume that the cache information is incomplete.1124IsIncomplete = true;1125} else {1126// This query's Size is less than the cached one. Conservatively restart1127// the query using the greater size.1128return getNonLocalPointerDepFromBB(1129QueryInst, Pointer, Loc.getWithNewSize(CacheInfo->Size), isLoad,1130StartBB, Result, Visited, SkipFirstBlock, IsIncomplete);1131}1132}1133
1134// If the query's AATags are inconsistent with the cached one,1135// conservatively throw out the cached data and restart the query with1136// no tag if needed.1137if (CacheInfo->AATags != Loc.AATags) {1138if (CacheInfo->AATags) {1139CacheInfo->Pair = BBSkipFirstBlockPair();1140CacheInfo->AATags = AAMDNodes();1141for (auto &Entry : CacheInfo->NonLocalDeps)1142if (Instruction *Inst = Entry.getResult().getInst())1143RemoveFromReverseMap(ReverseNonLocalPtrDeps, Inst, CacheKey);1144CacheInfo->NonLocalDeps.clear();1145// The cache is cleared (in the above line) so we will have lost1146// information about blocks we have already visited. We therefore must1147// assume that the cache information is incomplete.1148IsIncomplete = true;1149}1150if (Loc.AATags)1151return getNonLocalPointerDepFromBB(1152QueryInst, Pointer, Loc.getWithoutAATags(), isLoad, StartBB, Result,1153Visited, SkipFirstBlock, IsIncomplete);1154}1155}1156
1157NonLocalDepInfo *Cache = &CacheInfo->NonLocalDeps;1158
1159// If we have valid cached information for exactly the block we are1160// investigating, just return it with no recomputation.1161// Don't use cached information for invariant loads since it is valid for1162// non-invariant loads only.1163if (!IsIncomplete && !isInvariantLoad &&1164CacheInfo->Pair == BBSkipFirstBlockPair(StartBB, SkipFirstBlock)) {1165// We have a fully cached result for this query then we can just return the1166// cached results and populate the visited set. However, we have to verify1167// that we don't already have conflicting results for these blocks. Check1168// to ensure that if a block in the results set is in the visited set that1169// it was for the same pointer query.1170if (!Visited.empty()) {1171for (auto &Entry : *Cache) {1172DenseMap<BasicBlock *, Value *>::iterator VI =1173Visited.find(Entry.getBB());1174if (VI == Visited.end() || VI->second == Pointer.getAddr())1175continue;1176
1177// We have a pointer mismatch in a block. Just return false, saying1178// that something was clobbered in this result. We could also do a1179// non-fully cached query, but there is little point in doing this.1180return false;1181}1182}1183
1184Value *Addr = Pointer.getAddr();1185for (auto &Entry : *Cache) {1186Visited.insert(std::make_pair(Entry.getBB(), Addr));1187if (Entry.getResult().isNonLocal()) {1188continue;1189}1190
1191if (DT.isReachableFromEntry(Entry.getBB())) {1192Result.push_back(1193NonLocalDepResult(Entry.getBB(), Entry.getResult(), Addr));1194}1195}1196++NumCacheCompleteNonLocalPtr;1197return true;1198}1199
1200// Otherwise, either this is a new block, a block with an invalid cache1201// pointer or one that we're about to invalidate by putting more info into1202// it than its valid cache info. If empty and not explicitly indicated as1203// incomplete, the result will be valid cache info, otherwise it isn't.1204//1205// Invariant loads don't affect cache in any way thus no need to update1206// CacheInfo as well.1207if (!isInvariantLoad) {1208if (!IsIncomplete && Cache->empty())1209CacheInfo->Pair = BBSkipFirstBlockPair(StartBB, SkipFirstBlock);1210else1211CacheInfo->Pair = BBSkipFirstBlockPair();1212}1213
1214SmallVector<BasicBlock *, 32> Worklist;1215Worklist.push_back(StartBB);1216
1217// PredList used inside loop.1218SmallVector<std::pair<BasicBlock *, PHITransAddr>, 16> PredList;1219
1220// Keep track of the entries that we know are sorted. Previously cached1221// entries will all be sorted. The entries we add we only sort on demand (we1222// don't insert every element into its sorted position). We know that we1223// won't get any reuse from currently inserted values, because we don't1224// revisit blocks after we insert info for them.1225unsigned NumSortedEntries = Cache->size();1226unsigned WorklistEntries = BlockNumberLimit;1227bool GotWorklistLimit = false;1228LLVM_DEBUG(AssertSorted(*Cache));1229
1230BatchAAResults BatchAA(AA, &EII);1231while (!Worklist.empty()) {1232BasicBlock *BB = Worklist.pop_back_val();1233
1234// If we do process a large number of blocks it becomes very expensive and1235// likely it isn't worth worrying about1236if (Result.size() > NumResultsLimit) {1237// Sort it now (if needed) so that recursive invocations of1238// getNonLocalPointerDepFromBB and other routines that could reuse the1239// cache value will only see properly sorted cache arrays.1240if (Cache && NumSortedEntries != Cache->size()) {1241SortNonLocalDepInfoCache(*Cache, NumSortedEntries);1242}1243// Since we bail out, the "Cache" set won't contain all of the1244// results for the query. This is ok (we can still use it to accelerate1245// specific block queries) but we can't do the fastpath "return all1246// results from the set". Clear out the indicator for this.1247CacheInfo->Pair = BBSkipFirstBlockPair();1248return false;1249}1250
1251// Skip the first block if we have it.1252if (!SkipFirstBlock) {1253// Analyze the dependency of *Pointer in FromBB. See if we already have1254// been here.1255assert(Visited.count(BB) && "Should check 'visited' before adding to WL");1256
1257// Get the dependency info for Pointer in BB. If we have cached1258// information, we will use it, otherwise we compute it.1259LLVM_DEBUG(AssertSorted(*Cache, NumSortedEntries));1260MemDepResult Dep = getNonLocalInfoForBlock(1261QueryInst, Loc, isLoad, BB, Cache, NumSortedEntries, BatchAA);1262
1263// If we got a Def or Clobber, add this to the list of results.1264if (!Dep.isNonLocal()) {1265if (DT.isReachableFromEntry(BB)) {1266Result.push_back(NonLocalDepResult(BB, Dep, Pointer.getAddr()));1267continue;1268}1269}1270}1271
1272// If 'Pointer' is an instruction defined in this block, then we need to do1273// phi translation to change it into a value live in the predecessor block.1274// If not, we just add the predecessors to the worklist and scan them with1275// the same Pointer.1276if (!Pointer.needsPHITranslationFromBlock(BB)) {1277SkipFirstBlock = false;1278SmallVector<BasicBlock *, 16> NewBlocks;1279for (BasicBlock *Pred : PredCache.get(BB)) {1280// Verify that we haven't looked at this block yet.1281std::pair<DenseMap<BasicBlock *, Value *>::iterator, bool> InsertRes =1282Visited.insert(std::make_pair(Pred, Pointer.getAddr()));1283if (InsertRes.second) {1284// First time we've looked at *PI.1285NewBlocks.push_back(Pred);1286continue;1287}1288
1289// If we have seen this block before, but it was with a different1290// pointer then we have a phi translation failure and we have to treat1291// this as a clobber.1292if (InsertRes.first->second != Pointer.getAddr()) {1293// Make sure to clean up the Visited map before continuing on to1294// PredTranslationFailure.1295for (auto *NewBlock : NewBlocks)1296Visited.erase(NewBlock);1297goto PredTranslationFailure;1298}1299}1300if (NewBlocks.size() > WorklistEntries) {1301// Make sure to clean up the Visited map before continuing on to1302// PredTranslationFailure.1303for (auto *NewBlock : NewBlocks)1304Visited.erase(NewBlock);1305GotWorklistLimit = true;1306goto PredTranslationFailure;1307}1308WorklistEntries -= NewBlocks.size();1309Worklist.append(NewBlocks.begin(), NewBlocks.end());1310continue;1311}1312
1313// We do need to do phi translation, if we know ahead of time we can't phi1314// translate this value, don't even try.1315if (!Pointer.isPotentiallyPHITranslatable())1316goto PredTranslationFailure;1317
1318// We may have added values to the cache list before this PHI translation.1319// If so, we haven't done anything to ensure that the cache remains sorted.1320// Sort it now (if needed) so that recursive invocations of1321// getNonLocalPointerDepFromBB and other routines that could reuse the cache1322// value will only see properly sorted cache arrays.1323if (Cache && NumSortedEntries != Cache->size()) {1324SortNonLocalDepInfoCache(*Cache, NumSortedEntries);1325NumSortedEntries = Cache->size();1326}1327Cache = nullptr;1328
1329PredList.clear();1330for (BasicBlock *Pred : PredCache.get(BB)) {1331PredList.push_back(std::make_pair(Pred, Pointer));1332
1333// Get the PHI translated pointer in this predecessor. This can fail if1334// not translatable, in which case the getAddr() returns null.1335PHITransAddr &PredPointer = PredList.back().second;1336Value *PredPtrVal =1337PredPointer.translateValue(BB, Pred, &DT, /*MustDominate=*/false);1338
1339// Check to see if we have already visited this pred block with another1340// pointer. If so, we can't do this lookup. This failure can occur1341// with PHI translation when a critical edge exists and the PHI node in1342// the successor translates to a pointer value different than the1343// pointer the block was first analyzed with.1344std::pair<DenseMap<BasicBlock *, Value *>::iterator, bool> InsertRes =1345Visited.insert(std::make_pair(Pred, PredPtrVal));1346
1347if (!InsertRes.second) {1348// We found the pred; take it off the list of preds to visit.1349PredList.pop_back();1350
1351// If the predecessor was visited with PredPtr, then we already did1352// the analysis and can ignore it.1353if (InsertRes.first->second == PredPtrVal)1354continue;1355
1356// Otherwise, the block was previously analyzed with a different1357// pointer. We can't represent the result of this case, so we just1358// treat this as a phi translation failure.1359
1360// Make sure to clean up the Visited map before continuing on to1361// PredTranslationFailure.1362for (const auto &Pred : PredList)1363Visited.erase(Pred.first);1364
1365goto PredTranslationFailure;1366}1367}1368
1369// Actually process results here; this need to be a separate loop to avoid1370// calling getNonLocalPointerDepFromBB for blocks we don't want to return1371// any results for. (getNonLocalPointerDepFromBB will modify our1372// datastructures in ways the code after the PredTranslationFailure label1373// doesn't expect.)1374for (auto &I : PredList) {1375BasicBlock *Pred = I.first;1376PHITransAddr &PredPointer = I.second;1377Value *PredPtrVal = PredPointer.getAddr();1378
1379bool CanTranslate = true;1380// If PHI translation was unable to find an available pointer in this1381// predecessor, then we have to assume that the pointer is clobbered in1382// that predecessor. We can still do PRE of the load, which would insert1383// a computation of the pointer in this predecessor.1384if (!PredPtrVal)1385CanTranslate = false;1386
1387// FIXME: it is entirely possible that PHI translating will end up with1388// the same value. Consider PHI translating something like:1389// X = phi [x, bb1], [y, bb2]. PHI translating for bb1 doesn't *need*1390// to recurse here, pedantically speaking.1391
1392// If getNonLocalPointerDepFromBB fails here, that means the cached1393// result conflicted with the Visited list; we have to conservatively1394// assume it is unknown, but this also does not block PRE of the load.1395if (!CanTranslate ||1396!getNonLocalPointerDepFromBB(QueryInst, PredPointer,1397Loc.getWithNewPtr(PredPtrVal), isLoad,1398Pred, Result, Visited)) {1399// Add the entry to the Result list.1400NonLocalDepResult Entry(Pred, MemDepResult::getUnknown(), PredPtrVal);1401Result.push_back(Entry);1402
1403// Since we had a phi translation failure, the cache for CacheKey won't1404// include all of the entries that we need to immediately satisfy future1405// queries. Mark this in NonLocalPointerDeps by setting the1406// BBSkipFirstBlockPair pointer to null. This requires reuse of the1407// cached value to do more work but not miss the phi trans failure.1408NonLocalPointerInfo &NLPI = NonLocalPointerDeps[CacheKey];1409NLPI.Pair = BBSkipFirstBlockPair();1410continue;1411}1412}1413
1414// Refresh the CacheInfo/Cache pointer so that it isn't invalidated.1415CacheInfo = &NonLocalPointerDeps[CacheKey];1416Cache = &CacheInfo->NonLocalDeps;1417NumSortedEntries = Cache->size();1418
1419// Since we did phi translation, the "Cache" set won't contain all of the1420// results for the query. This is ok (we can still use it to accelerate1421// specific block queries) but we can't do the fastpath "return all1422// results from the set" Clear out the indicator for this.1423CacheInfo->Pair = BBSkipFirstBlockPair();1424SkipFirstBlock = false;1425continue;1426
1427PredTranslationFailure:1428// The following code is "failure"; we can't produce a sane translation1429// for the given block. It assumes that we haven't modified any of1430// our datastructures while processing the current block.1431
1432if (!Cache) {1433// Refresh the CacheInfo/Cache pointer if it got invalidated.1434CacheInfo = &NonLocalPointerDeps[CacheKey];1435Cache = &CacheInfo->NonLocalDeps;1436NumSortedEntries = Cache->size();1437}1438
1439// Since we failed phi translation, the "Cache" set won't contain all of the1440// results for the query. This is ok (we can still use it to accelerate1441// specific block queries) but we can't do the fastpath "return all1442// results from the set". Clear out the indicator for this.1443CacheInfo->Pair = BBSkipFirstBlockPair();1444
1445// If *nothing* works, mark the pointer as unknown.1446//1447// If this is the magic first block, return this as a clobber of the whole1448// incoming value. Since we can't phi translate to one of the predecessors,1449// we have to bail out.1450if (SkipFirstBlock)1451return false;1452
1453// Results of invariant loads are not cached thus no need to update cached1454// information.1455if (!isInvariantLoad) {1456for (NonLocalDepEntry &I : llvm::reverse(*Cache)) {1457if (I.getBB() != BB)1458continue;1459
1460assert((GotWorklistLimit || I.getResult().isNonLocal() ||1461!DT.isReachableFromEntry(BB)) &&1462"Should only be here with transparent block");1463
1464I.setResult(MemDepResult::getUnknown());1465
1466
1467break;1468}1469}1470(void)GotWorklistLimit;1471// Go ahead and report unknown dependence.1472Result.push_back(1473NonLocalDepResult(BB, MemDepResult::getUnknown(), Pointer.getAddr()));1474}1475
1476// Okay, we're done now. If we added new values to the cache, re-sort it.1477SortNonLocalDepInfoCache(*Cache, NumSortedEntries);1478LLVM_DEBUG(AssertSorted(*Cache));1479return true;1480}
1481
1482/// If P exists in CachedNonLocalPointerInfo or NonLocalDefsCache, remove it.
1483void MemoryDependenceResults::removeCachedNonLocalPointerDependencies(1484ValueIsLoadPair P) {1485
1486// Most of the time this cache is empty.1487if (!NonLocalDefsCache.empty()) {1488auto it = NonLocalDefsCache.find(P.getPointer());1489if (it != NonLocalDefsCache.end()) {1490RemoveFromReverseMap(ReverseNonLocalDefsCache,1491it->second.getResult().getInst(), P.getPointer());1492NonLocalDefsCache.erase(it);1493}1494
1495if (auto *I = dyn_cast<Instruction>(P.getPointer())) {1496auto toRemoveIt = ReverseNonLocalDefsCache.find(I);1497if (toRemoveIt != ReverseNonLocalDefsCache.end()) {1498for (const auto *entry : toRemoveIt->second)1499NonLocalDefsCache.erase(entry);1500ReverseNonLocalDefsCache.erase(toRemoveIt);1501}1502}1503}1504
1505CachedNonLocalPointerInfo::iterator It = NonLocalPointerDeps.find(P);1506if (It == NonLocalPointerDeps.end())1507return;1508
1509// Remove all of the entries in the BB->val map. This involves removing1510// instructions from the reverse map.1511NonLocalDepInfo &PInfo = It->second.NonLocalDeps;1512
1513for (const NonLocalDepEntry &DE : PInfo) {1514Instruction *Target = DE.getResult().getInst();1515if (!Target)1516continue; // Ignore non-local dep results.1517assert(Target->getParent() == DE.getBB());1518
1519// Eliminating the dirty entry from 'Cache', so update the reverse info.1520RemoveFromReverseMap(ReverseNonLocalPtrDeps, Target, P);1521}1522
1523// Remove P from NonLocalPointerDeps (which deletes NonLocalDepInfo).1524NonLocalPointerDeps.erase(It);1525}
1526
1527void MemoryDependenceResults::invalidateCachedPointerInfo(Value *Ptr) {1528// If Ptr isn't really a pointer, just ignore it.1529if (!Ptr->getType()->isPointerTy())1530return;1531// Flush store info for the pointer.1532removeCachedNonLocalPointerDependencies(ValueIsLoadPair(Ptr, false));1533// Flush load info for the pointer.1534removeCachedNonLocalPointerDependencies(ValueIsLoadPair(Ptr, true));1535}
1536
1537void MemoryDependenceResults::invalidateCachedPredecessors() {1538PredCache.clear();1539}
1540
1541void MemoryDependenceResults::removeInstruction(Instruction *RemInst) {1542EII.removeInstruction(RemInst);1543
1544// Walk through the Non-local dependencies, removing this one as the value1545// for any cached queries.1546NonLocalDepMapType::iterator NLDI = NonLocalDepsMap.find(RemInst);1547if (NLDI != NonLocalDepsMap.end()) {1548NonLocalDepInfo &BlockMap = NLDI->second.first;1549for (auto &Entry : BlockMap)1550if (Instruction *Inst = Entry.getResult().getInst())1551RemoveFromReverseMap(ReverseNonLocalDeps, Inst, RemInst);1552NonLocalDepsMap.erase(NLDI);1553}1554
1555// If we have a cached local dependence query for this instruction, remove it.1556LocalDepMapType::iterator LocalDepEntry = LocalDeps.find(RemInst);1557if (LocalDepEntry != LocalDeps.end()) {1558// Remove us from DepInst's reverse set now that the local dep info is gone.1559if (Instruction *Inst = LocalDepEntry->second.getInst())1560RemoveFromReverseMap(ReverseLocalDeps, Inst, RemInst);1561
1562// Remove this local dependency info.1563LocalDeps.erase(LocalDepEntry);1564}1565
1566// If we have any cached dependencies on this instruction, remove1567// them.1568
1569// If the instruction is a pointer, remove it from both the load info and the1570// store info.1571if (RemInst->getType()->isPointerTy()) {1572removeCachedNonLocalPointerDependencies(ValueIsLoadPair(RemInst, false));1573removeCachedNonLocalPointerDependencies(ValueIsLoadPair(RemInst, true));1574} else {1575// Otherwise, if the instructions is in the map directly, it must be a load.1576// Remove it.1577auto toRemoveIt = NonLocalDefsCache.find(RemInst);1578if (toRemoveIt != NonLocalDefsCache.end()) {1579assert(isa<LoadInst>(RemInst) &&1580"only load instructions should be added directly");1581const Instruction *DepV = toRemoveIt->second.getResult().getInst();1582ReverseNonLocalDefsCache.find(DepV)->second.erase(RemInst);1583NonLocalDefsCache.erase(toRemoveIt);1584}1585}1586
1587// Loop over all of the things that depend on the instruction we're removing.1588SmallVector<std::pair<Instruction *, Instruction *>, 8> ReverseDepsToAdd;1589
1590// If we find RemInst as a clobber or Def in any of the maps for other values,1591// we need to replace its entry with a dirty version of the instruction after1592// it. If RemInst is a terminator, we use a null dirty value.1593//1594// Using a dirty version of the instruction after RemInst saves having to scan1595// the entire block to get to this point.1596MemDepResult NewDirtyVal;1597if (!RemInst->isTerminator())1598NewDirtyVal = MemDepResult::getDirty(&*++RemInst->getIterator());1599
1600ReverseDepMapType::iterator ReverseDepIt = ReverseLocalDeps.find(RemInst);1601if (ReverseDepIt != ReverseLocalDeps.end()) {1602// RemInst can't be the terminator if it has local stuff depending on it.1603assert(!ReverseDepIt->second.empty() && !RemInst->isTerminator() &&1604"Nothing can locally depend on a terminator");1605
1606for (Instruction *InstDependingOnRemInst : ReverseDepIt->second) {1607assert(InstDependingOnRemInst != RemInst &&1608"Already removed our local dep info");1609
1610LocalDeps[InstDependingOnRemInst] = NewDirtyVal;1611
1612// Make sure to remember that new things depend on NewDepInst.1613assert(NewDirtyVal.getInst() &&1614"There is no way something else can have "1615"a local dep on this if it is a terminator!");1616ReverseDepsToAdd.push_back(1617std::make_pair(NewDirtyVal.getInst(), InstDependingOnRemInst));1618}1619
1620ReverseLocalDeps.erase(ReverseDepIt);1621
1622// Add new reverse deps after scanning the set, to avoid invalidating the1623// 'ReverseDeps' reference.1624while (!ReverseDepsToAdd.empty()) {1625ReverseLocalDeps[ReverseDepsToAdd.back().first].insert(1626ReverseDepsToAdd.back().second);1627ReverseDepsToAdd.pop_back();1628}1629}1630
1631ReverseDepIt = ReverseNonLocalDeps.find(RemInst);1632if (ReverseDepIt != ReverseNonLocalDeps.end()) {1633for (Instruction *I : ReverseDepIt->second) {1634assert(I != RemInst && "Already removed NonLocalDep info for RemInst");1635
1636PerInstNLInfo &INLD = NonLocalDepsMap[I];1637// The information is now dirty!1638INLD.second = true;1639
1640for (auto &Entry : INLD.first) {1641if (Entry.getResult().getInst() != RemInst)1642continue;1643
1644// Convert to a dirty entry for the subsequent instruction.1645Entry.setResult(NewDirtyVal);1646
1647if (Instruction *NextI = NewDirtyVal.getInst())1648ReverseDepsToAdd.push_back(std::make_pair(NextI, I));1649}1650}1651
1652ReverseNonLocalDeps.erase(ReverseDepIt);1653
1654// Add new reverse deps after scanning the set, to avoid invalidating 'Set'1655while (!ReverseDepsToAdd.empty()) {1656ReverseNonLocalDeps[ReverseDepsToAdd.back().first].insert(1657ReverseDepsToAdd.back().second);1658ReverseDepsToAdd.pop_back();1659}1660}1661
1662// If the instruction is in ReverseNonLocalPtrDeps then it appears as a1663// value in the NonLocalPointerDeps info.1664ReverseNonLocalPtrDepTy::iterator ReversePtrDepIt =1665ReverseNonLocalPtrDeps.find(RemInst);1666if (ReversePtrDepIt != ReverseNonLocalPtrDeps.end()) {1667SmallVector<std::pair<Instruction *, ValueIsLoadPair>, 8>1668ReversePtrDepsToAdd;1669
1670for (ValueIsLoadPair P : ReversePtrDepIt->second) {1671assert(P.getPointer() != RemInst &&1672"Already removed NonLocalPointerDeps info for RemInst");1673
1674NonLocalDepInfo &NLPDI = NonLocalPointerDeps[P].NonLocalDeps;1675
1676// The cache is not valid for any specific block anymore.1677NonLocalPointerDeps[P].Pair = BBSkipFirstBlockPair();1678
1679// Update any entries for RemInst to use the instruction after it.1680for (auto &Entry : NLPDI) {1681if (Entry.getResult().getInst() != RemInst)1682continue;1683
1684// Convert to a dirty entry for the subsequent instruction.1685Entry.setResult(NewDirtyVal);1686
1687if (Instruction *NewDirtyInst = NewDirtyVal.getInst())1688ReversePtrDepsToAdd.push_back(std::make_pair(NewDirtyInst, P));1689}1690
1691// Re-sort the NonLocalDepInfo. Changing the dirty entry to its1692// subsequent value may invalidate the sortedness.1693llvm::sort(NLPDI);1694}1695
1696ReverseNonLocalPtrDeps.erase(ReversePtrDepIt);1697
1698while (!ReversePtrDepsToAdd.empty()) {1699ReverseNonLocalPtrDeps[ReversePtrDepsToAdd.back().first].insert(1700ReversePtrDepsToAdd.back().second);1701ReversePtrDepsToAdd.pop_back();1702}1703}1704
1705assert(!NonLocalDepsMap.count(RemInst) && "RemInst got reinserted?");1706LLVM_DEBUG(verifyRemoved(RemInst));1707}
1708
1709/// Verify that the specified instruction does not occur in our internal data
1710/// structures.
1711///
1712/// This function verifies by asserting in debug builds.
1713void MemoryDependenceResults::verifyRemoved(Instruction *D) const {1714#ifndef NDEBUG1715for (const auto &DepKV : LocalDeps) {1716assert(DepKV.first != D && "Inst occurs in data structures");1717assert(DepKV.second.getInst() != D && "Inst occurs in data structures");1718}1719
1720for (const auto &DepKV : NonLocalPointerDeps) {1721assert(DepKV.first.getPointer() != D && "Inst occurs in NLPD map key");1722for (const auto &Entry : DepKV.second.NonLocalDeps)1723assert(Entry.getResult().getInst() != D && "Inst occurs as NLPD value");1724}1725
1726for (const auto &DepKV : NonLocalDepsMap) {1727assert(DepKV.first != D && "Inst occurs in data structures");1728const PerInstNLInfo &INLD = DepKV.second;1729for (const auto &Entry : INLD.first)1730assert(Entry.getResult().getInst() != D &&1731"Inst occurs in data structures");1732}1733
1734for (const auto &DepKV : ReverseLocalDeps) {1735assert(DepKV.first != D && "Inst occurs in data structures");1736for (Instruction *Inst : DepKV.second)1737assert(Inst != D && "Inst occurs in data structures");1738}1739
1740for (const auto &DepKV : ReverseNonLocalDeps) {1741assert(DepKV.first != D && "Inst occurs in data structures");1742for (Instruction *Inst : DepKV.second)1743assert(Inst != D && "Inst occurs in data structures");1744}1745
1746for (const auto &DepKV : ReverseNonLocalPtrDeps) {1747assert(DepKV.first != D && "Inst occurs in rev NLPD map");1748
1749for (ValueIsLoadPair P : DepKV.second)1750assert(P != ValueIsLoadPair(D, false) && P != ValueIsLoadPair(D, true) &&1751"Inst occurs in ReverseNonLocalPtrDeps map");1752}1753#endif1754}
1755
1756AnalysisKey MemoryDependenceAnalysis::Key;1757
1758MemoryDependenceAnalysis::MemoryDependenceAnalysis()1759: DefaultBlockScanLimit(BlockScanLimit) {}1760
1761MemoryDependenceResults
1762MemoryDependenceAnalysis::run(Function &F, FunctionAnalysisManager &AM) {1763auto &AA = AM.getResult<AAManager>(F);1764auto &AC = AM.getResult<AssumptionAnalysis>(F);1765auto &TLI = AM.getResult<TargetLibraryAnalysis>(F);1766auto &DT = AM.getResult<DominatorTreeAnalysis>(F);1767return MemoryDependenceResults(AA, AC, TLI, DT, DefaultBlockScanLimit);1768}
1769
1770char MemoryDependenceWrapperPass::ID = 0;1771
1772INITIALIZE_PASS_BEGIN(MemoryDependenceWrapperPass, "memdep",1773"Memory Dependence Analysis", false, true)1774INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)1775INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)1776INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)1777INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)1778INITIALIZE_PASS_END(MemoryDependenceWrapperPass, "memdep",1779"Memory Dependence Analysis", false, true)1780
1781MemoryDependenceWrapperPass::MemoryDependenceWrapperPass() : FunctionPass(ID) {1782initializeMemoryDependenceWrapperPassPass(*PassRegistry::getPassRegistry());1783}
1784
1785MemoryDependenceWrapperPass::~MemoryDependenceWrapperPass() = default;1786
1787void MemoryDependenceWrapperPass::releaseMemory() {1788MemDep.reset();1789}
1790
1791void MemoryDependenceWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {1792AU.setPreservesAll();1793AU.addRequired<AssumptionCacheTracker>();1794AU.addRequired<DominatorTreeWrapperPass>();1795AU.addRequiredTransitive<AAResultsWrapperPass>();1796AU.addRequiredTransitive<TargetLibraryInfoWrapperPass>();1797}
1798
1799bool MemoryDependenceResults::invalidate(Function &F, const PreservedAnalyses &PA,1800FunctionAnalysisManager::Invalidator &Inv) {1801// Check whether our analysis is preserved.1802auto PAC = PA.getChecker<MemoryDependenceAnalysis>();1803if (!PAC.preserved() && !PAC.preservedSet<AllAnalysesOn<Function>>())1804// If not, give up now.1805return true;1806
1807// Check whether the analyses we depend on became invalid for any reason.1808if (Inv.invalidate<AAManager>(F, PA) ||1809Inv.invalidate<AssumptionAnalysis>(F, PA) ||1810Inv.invalidate<DominatorTreeAnalysis>(F, PA))1811return true;1812
1813// Otherwise this analysis result remains valid.1814return false;1815}
1816
1817unsigned MemoryDependenceResults::getDefaultBlockScanLimit() const {1818return DefaultBlockScanLimit;1819}
1820
1821bool MemoryDependenceWrapperPass::runOnFunction(Function &F) {1822auto &AA = getAnalysis<AAResultsWrapperPass>().getAAResults();1823auto &AC = getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);1824auto &TLI = getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F);1825auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();1826MemDep.emplace(AA, AC, TLI, DT, BlockScanLimit);1827return false;1828}
1829