llvm.org GIT mirror llvm / c5266b5
[PM] Port memdep to the new pass manager. This is a fairly straightforward port to the new pass manager with one exception. It removes a very questionable use of releaseMemory() in the old pass to invalidate its caches between runs on a function. I don't think this is really guaranteed to be safe. I've just used the more direct port to the new PM to address this by nuking the results object each time the pass runs. While this could cause some minor malloc traffic increase, I don't expect the compile time performance hit to be noticable, and it makes the correctness and other aspects of the pass much easier to reason about. In some cases, it may make things faster by making the sets and maps smaller with better locality. Indeed, the measurements collected by Bruno (thanks!!!) show mostly compile time improvements. There is sadly very limited testing at this point as there are only two tests of memdep, and both rely on GVN. I'll be porting GVN next and that will exercise this heavily though. Differential Revision: http://reviews.llvm.org/D17962 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@263082 91177308-0d34-0410-b5e6-96231b3b80d8 Chandler Carruth 3 years ago
15 changed file(s) with 177 addition(s) and 140 deletion(s). Raw diff Collapse all Expand all
1919 #include "llvm/ADT/SmallPtrSet.h"
2020 #include "llvm/Analysis/AliasAnalysis.h"
2121 #include "llvm/IR/BasicBlock.h"
22 #include "llvm/IR/PassManager.h"
2223 #include "llvm/IR/PredIteratorCache.h"
2324 #include "llvm/IR/ValueHandle.h"
2425 #include "llvm/Pass.h"
2930 class Instruction;
3031 class CallSite;
3132 class AssumptionCache;
32 class MemoryDependenceAnalysis;
33 class MemoryDependenceResults;
3334 class PredIteratorCache;
3435 class DominatorTree;
3536 class PHITransAddr;
183184 bool operator>(const MemDepResult &M) const { return Value > M.Value; }
184185
185186 private:
186 friend class MemoryDependenceAnalysis;
187 friend class MemoryDependenceResults;
187188
188189 /// Tests if this is a MemDepResult in its dirty/invalid. state.
189190 bool isDirty() const { return Value.is(); }
250251 Value *getAddress() const { return Address; }
251252 };
252253
253 /// Determines, for a given memory operation, what preceding memory operations
254 /// it depends on.
255 ///
256 /// It builds on alias analysis information, and tries to provide a lazy,
257 /// caching interface to a common kind of alias information query.
254 /// Provides a lazy, caching interface for making common memory aliasing
255 /// information queries, backed by LLVM's alias analysis passes.
258256 ///
259257 /// The dependency information returned is somewhat unusual, but is pragmatic.
260258 /// If queried about a store or call that might modify memory, the analysis
265263 /// b) they load from *must-aliased* pointers. Returning a dependence on
266264 /// must-alias'd pointers instead of all pointers interacts well with the
267265 /// internal caching mechanism.
268 class MemoryDependenceAnalysis : public FunctionPass {
266 class MemoryDependenceResults {
269267 // A map from instructions to their dependency.
270268 typedef DenseMap LocalDepMapType;
271269 LocalDepMapType LocalDeps;
339337 ReverseDepMapType ReverseNonLocalDeps;
340338
341339 /// Current AA implementation, just a cache.
342 AliasAnalysis *AA;
340 AliasAnalysis &AA;
341 AssumptionCache ∾
342 const TargetLibraryInfo &TLI;
343343 DominatorTree *DT;
344 AssumptionCache *AC;
345 const TargetLibraryInfo *TLI;
346344 PredIteratorCache PredCache;
347345
348346 public:
349 MemoryDependenceAnalysis();
350 ~MemoryDependenceAnalysis() override;
351 static char ID;
352
353 /// Pass Implementation stuff. This doesn't do any analysis eagerly.
354 bool runOnFunction(Function &) override;
355
356 /// Clean up memory in between runs
357 void releaseMemory() override;
358
359 /// Does not modify anything. It uses Value Numbering and Alias Analysis.
360 void getAnalysisUsage(AnalysisUsage &AU) const override;
347 MemoryDependenceResults(AliasAnalysis &AA, AssumptionCache &AC,
348 const TargetLibraryInfo &TLI,
349 DominatorTree *DT = nullptr)
350 : AA(AA), AC(AC), TLI(TLI), DT(DT) {}
361351
362352 /// Returns the instruction on which a memory operation depends.
363353 ///
451441 int64_t MemLocOffs,
452442 unsigned MemLocSize,
453443 const LoadInst *LI);
444
445 /// Release memory in caches.
446 void releaseMemory();
454447
455448 private:
456449 MemDepResult getCallSiteDependencyFrom(CallSite C, bool isReadOnlyCall,
473466 void verifyRemoved(Instruction *Inst) const;
474467 };
475468
469 /// An analysis that produces \c MemoryDependenceResults for a function.
470 ///
471 /// This is essentially a no-op because the results are computed entirely
472 /// lazily.
473 struct MemoryDependenceAnalysis : AnalysisBase {
474 typedef MemoryDependenceResults Result;
475
476 MemoryDependenceResults run(Function &F, AnalysisManager *AM);
477 };
478
479 /// A wrapper analysis pass for the legacy pass manager that exposes a \c
480 /// MemoryDepnedenceResults instance.
481 class MemoryDependenceWrapperPass : public FunctionPass {
482 Optional MemDep;
483 public:
484 MemoryDependenceWrapperPass();
485 ~MemoryDependenceWrapperPass() override;
486 static char ID;
487
488 /// Pass Implementation stuff. This doesn't do any analysis eagerly.
489 bool runOnFunction(Function &) override;
490
491 /// Clean up memory in between runs
492 void releaseMemory() override;
493
494 /// Does not modify anything. It uses Value Numbering and Alias Analysis.
495 void getAnalysisUsage(AnalysisUsage &AU) const override;
496
497 MemoryDependenceResults &getMemDep() { return *MemDep; }
498 };
499
476500 } // End llvm namespace
477501
478502 #endif
210210 void initializeMemCpyOptPass(PassRegistry&);
211211 void initializeMemDepPrinterPass(PassRegistry&);
212212 void initializeMemDerefPrinterPass(PassRegistry&);
213 void initializeMemoryDependenceAnalysisPass(PassRegistry&);
213 void initializeMemoryDependenceWrapperPassPass(PassRegistry&);
214214 void initializeMemorySSALazyPass(PassRegistry&);
215215 void initializeMemorySSAPrinterPassPass(PassRegistry&);
216216 void initializeMergedLoadStoreMotionPass(PassRegistry &);
2121
2222 namespace llvm {
2323
24 class MemoryDependenceAnalysis;
24 class MemoryDependenceResults;
2525 class DominatorTree;
2626 class LoopInfo;
2727 class Instruction;
3939 /// when all entries to the PHI nodes in a block are guaranteed equal, such as
4040 /// when the block has exactly one predecessor.
4141 void FoldSingleEntryPHINodes(BasicBlock *BB,
42 MemoryDependenceAnalysis *MemDep = nullptr);
42 MemoryDependenceResults *MemDep = nullptr);
4343
4444 /// DeleteDeadPHIs - Examine each PHI in the given block and delete it if it
4545 /// is dead. Also recursively delete any operands that become dead as
5252 /// if possible. The return value indicates success or failure.
5353 bool MergeBlockIntoPredecessor(BasicBlock *BB, DominatorTree *DT = nullptr,
5454 LoopInfo *LI = nullptr,
55 MemoryDependenceAnalysis *MemDep = nullptr);
55 MemoryDependenceResults *MemDep = nullptr);
5656
5757 // ReplaceInstWithValue - Replace all uses of an instruction (specified by BI)
5858 // with a value, then remove and delete the original instruction.
5656 initializeLoopInfoWrapperPassPass(Registry);
5757 initializeMemDepPrinterPass(Registry);
5858 initializeMemDerefPrinterPass(Registry);
59 initializeMemoryDependenceAnalysisPass(Registry);
59 initializeMemoryDependenceWrapperPassPass(Registry);
6060 initializeModuleDebugInfoPrinterPass(Registry);
6161 initializeObjCARCAAWrapperPassPass(Registry);
6262 initializePostDominatorTreeWrapperPassPass(Registry);
4949
5050 void getAnalysisUsage(AnalysisUsage &AU) const override {
5151 AU.addRequiredTransitive();
52 AU.addRequiredTransitiveAnalysis>();
52 AU.addRequiredTransitiveWrapperPass>();
5353 AU.setPreservesAll();
5454 }
5555
7878 char MemDepPrinter::ID = 0;
7979 INITIALIZE_PASS_BEGIN(MemDepPrinter, "print-memdeps",
8080 "Print MemDeps of function", false, true)
81 INITIALIZE_PASS_DEPENDENCY(MemoryDependenceAnalysis)
81 INITIALIZE_PASS_DEPENDENCY(MemoryDependenceWrapperPass)
8282 INITIALIZE_PASS_END(MemDepPrinter, "print-memdeps",
8383 "Print MemDeps of function", false, true)
8484
9191
9292 bool MemDepPrinter::runOnFunction(Function &F) {
9393 this->F = &F;
94 MemoryDependenceAnalysis &MDA = getAnalysis();
94 MemoryDependenceResults &MDA = getAnalysis().getMemDep();
9595
9696 // All this code uses non-const interfaces because MemDep is not
9797 // const-friendly, though nothing is actually modified.
106106 Deps[Inst].insert(std::make_pair(getInstTypePair(Res),
107107 static_cast(nullptr)));
108108 } else if (auto CS = CallSite(Inst)) {
109 const MemoryDependenceAnalysis::NonLocalDepInfo &NLDI =
109 const MemoryDependenceResults::NonLocalDepInfo &NLDI =
110110 MDA.getNonLocalCallDependency(CS);
111111
112112 DepSet &InstDeps = Deps[Inst];
113 for (MemoryDependenceAnalysis::NonLocalDepInfo::const_iterator
113 for (MemoryDependenceResults::NonLocalDepInfo::const_iterator
114114 I = NLDI.begin(), E = NLDI.end(); I != E; ++I) {
115115 const MemDepResult &Res = I->getResult();
116116 InstDeps.insert(std::make_pair(getInstTypePair(Res), I->getBB()));
6262
6363 // Limit on the number of memdep results to process.
6464 static const unsigned int NumResultsLimit = 100;
65
66 char MemoryDependenceAnalysis::ID = 0;
67
68 INITIALIZE_PASS_BEGIN(MemoryDependenceAnalysis, "memdep",
69 "Memory Dependence Analysis", false, true)
70 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
71 INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
72 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
73 INITIALIZE_PASS_END(MemoryDependenceAnalysis, "memdep",
74 "Memory Dependence Analysis", false, true)
75
76 MemoryDependenceAnalysis::MemoryDependenceAnalysis() : FunctionPass(ID) {
77 initializeMemoryDependenceAnalysisPass(*PassRegistry::getPassRegistry());
78 }
79 MemoryDependenceAnalysis::~MemoryDependenceAnalysis() {}
80
81 /// Clean up memory in between runs
82 void MemoryDependenceAnalysis::releaseMemory() {
83 LocalDeps.clear();
84 NonLocalDeps.clear();
85 NonLocalPointerDeps.clear();
86 ReverseLocalDeps.clear();
87 ReverseNonLocalDeps.clear();
88 ReverseNonLocalPtrDeps.clear();
89 PredCache.clear();
90 }
91
92 void MemoryDependenceAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
93 AU.setPreservesAll();
94 AU.addRequired();
95 AU.addRequiredTransitive();
96 AU.addRequiredTransitive();
97 }
98
99 bool MemoryDependenceAnalysis::runOnFunction(Function &F) {
100 AA = &getAnalysis().getAAResults();
101 AC = &getAnalysis().getAssumptionCache(F);
102 DominatorTreeWrapperPass *DTWP =
103 getAnalysisIfAvailable();
104 DT = DTWP ? &DTWP->getDomTree() : nullptr;
105 TLI = &getAnalysis().getTLI();
106 return false;
107 }
10865
10966 /// This is a helper function that removes Val from 'Inst's set in ReverseMap.
11067 ///
203160 }
204161
205162 /// Private helper for finding the local dependencies of a call site.
206 MemDepResult MemoryDependenceAnalysis::getCallSiteDependencyFrom(
163 MemDepResult MemoryDependenceResults::getCallSiteDependencyFrom(
207164 CallSite CS, bool isReadOnlyCall, BasicBlock::iterator ScanIt,
208165 BasicBlock *BB) {
209166 unsigned Limit = BlockScanLimit;
220177
221178 // If this inst is a memory op, get the pointer it accessed
222179 MemoryLocation Loc;
223 ModRefInfo MR = GetLocation(Inst, Loc, *TLI);
180 ModRefInfo MR = GetLocation(Inst, Loc, TLI);
224181 if (Loc.Ptr) {
225182 // A simple instruction.
226 if (AA->getModRefInfo(CS, Loc) != MRI_NoModRef)
183 if (AA.getModRefInfo(CS, Loc) != MRI_NoModRef)
227184 return MemDepResult::getClobber(Inst);
228185 continue;
229186 }
233190 if (isa(Inst))
234191 continue;
235192 // If these two calls do not interfere, look past it.
236 switch (AA->getModRefInfo(CS, InstCS)) {
193 switch (AA.getModRefInfo(CS, InstCS)) {
237194 case MRI_NoModRef:
238195 // If the two calls are the same, return InstCS as a Def, so that
239196 // CS can be found redundant and eliminated.
277234 if (!MemLocBase)
278235 MemLocBase = GetPointerBaseWithConstantOffset(MemLoc.Ptr, MemLocOffs, DL);
279236
280 unsigned Size = MemoryDependenceAnalysis::getLoadLoadClobberFullWidthSize(
237 unsigned Size = MemoryDependenceResults::getLoadLoadClobberFullWidthSize(
281238 MemLocBase, MemLocOffs, MemLoc.Size, LI);
282239 return Size != 0;
283240 }
284241
285 unsigned MemoryDependenceAnalysis::getLoadLoadClobberFullWidthSize(
242 unsigned MemoryDependenceResults::getLoadLoadClobberFullWidthSize(
286243 const Value *MemLocBase, int64_t MemLocOffs, unsigned MemLocSize,
287244 const LoadInst *LI) {
288245 // We can only extend simple integer loads.
367324 return false;
368325 }
369326
370 MemDepResult MemoryDependenceAnalysis::getPointerDependencyFrom(
327 MemDepResult MemoryDependenceResults::getPointerDependencyFrom(
371328 const MemoryLocation &MemLoc, bool isLoad, BasicBlock::iterator ScanIt,
372329 BasicBlock *BB, Instruction *QueryInst) {
373330
384341 }
385342
386343 MemDepResult
387 MemoryDependenceAnalysis::getInvariantGroupPointerDependency(LoadInst *LI,
344 MemoryDependenceResults::getInvariantGroupPointerDependency(LoadInst *LI,
388345 BasicBlock *BB) {
389346 Value *LoadOperand = LI->getPointerOperand();
390347 // It's is not safe to walk the use list of global value, because function
436393 return Result;
437394 }
438395
439 MemDepResult MemoryDependenceAnalysis::getSimplePointerDependencyFrom(
396 MemDepResult MemoryDependenceResults::getSimplePointerDependencyFrom(
440397 const MemoryLocation &MemLoc, bool isLoad, BasicBlock::iterator ScanIt,
441398 BasicBlock *BB, Instruction *QueryInst) {
442399
530487 // pointer, not on query pointers that are indexed off of them. It'd
531488 // be nice to handle that at some point (the right approach is to use
532489 // GetPointerBaseWithConstantOffset).
533 if (AA->isMustAlias(MemoryLocation(II->getArgOperand(1)), MemLoc))
490 if (AA.isMustAlias(MemoryLocation(II->getArgOperand(1)), MemLoc))
534491 return MemDepResult::getDef(II);
535492 continue;
536493 }
571528 MemoryLocation LoadLoc = MemoryLocation::get(LI);
572529
573530 // If we found a pointer, check if it could be the same as our pointer.
574 AliasResult R = AA->alias(LoadLoc, MemLoc);
531 AliasResult R = AA.alias(LoadLoc, MemLoc);
575532
576533 if (isLoad) {
577534 if (R == NoAlias) {
615572 continue;
616573
617574 // Stores don't alias loads from read-only memory.
618 if (AA->pointsToConstantMemory(LoadLoc))
575 if (AA.pointsToConstantMemory(LoadLoc))
619576 continue;
620577
621578 // Stores depend on may/must aliased loads.
646603 // If alias analysis can tell that this store is guaranteed to not modify
647604 // the query pointer, ignore it. Use getModRefInfo to handle cases where
648605 // the query pointer points to constant memory etc.
649 if (AA->getModRefInfo(SI, MemLoc) == MRI_NoModRef)
606 if (AA.getModRefInfo(SI, MemLoc) == MRI_NoModRef)
650607 continue;
651608
652609 // Ok, this store might clobber the query pointer. Check to see if it is
654611 MemoryLocation StoreLoc = MemoryLocation::get(SI);
655612
656613 // If we found a pointer, check if it could be the same as our pointer.
657 AliasResult R = AA->alias(StoreLoc, MemLoc);
614 AliasResult R = AA.alias(StoreLoc, MemLoc);
658615
659616 if (R == NoAlias)
660617 continue;
671628 // turn into undef. Note that we can bypass the allocation itself when
672629 // looking for a clobber in many cases; that's an alias property and is
673630 // handled by BasicAA.
674 if (isa(Inst) || isNoAliasFn(Inst, TLI)) {
631 if (isa(Inst) || isNoAliasFn(Inst, &TLI)) {
675632 const Value *AccessPtr = GetUnderlyingObject(MemLoc.Ptr, DL);
676 if (AccessPtr == Inst || AA->isMustAlias(Inst, AccessPtr))
633 if (AccessPtr == Inst || AA.isMustAlias(Inst, AccessPtr))
677634 return MemDepResult::getDef(Inst);
678635 }
679636
681638 continue;
682639
683640 // See if this instruction (e.g. a call or vaarg) mod/ref's the pointer.
684 ModRefInfo MR = AA->getModRefInfo(Inst, MemLoc);
641 ModRefInfo MR = AA.getModRefInfo(Inst, MemLoc);
685642 // If necessary, perform additional analysis.
686643 if (MR == MRI_ModRef)
687 MR = AA->callCapturesBefore(Inst, MemLoc, DT, &OBB);
644 MR = AA.callCapturesBefore(Inst, MemLoc, DT, &OBB);
688645 switch (MR) {
689646 case MRI_NoModRef:
690647 // If the call has no effect on the queried pointer, just ignore it.
709666 return MemDepResult::getNonFuncLocal();
710667 }
711668
712 MemDepResult MemoryDependenceAnalysis::getDependency(Instruction *QueryInst) {
669 MemDepResult MemoryDependenceResults::getDependency(Instruction *QueryInst) {
713670 Instruction *ScanPos = QueryInst;
714671
715672 // Check for a cached result
740697 LocalCache = MemDepResult::getNonFuncLocal();
741698 } else {
742699 MemoryLocation MemLoc;
743 ModRefInfo MR = GetLocation(QueryInst, MemLoc, *TLI);
700 ModRefInfo MR = GetLocation(QueryInst, MemLoc, TLI);
744701 if (MemLoc.Ptr) {
745702 // If we can do a pointer scan, make it happen.
746703 bool isLoad = !(MR & MRI_Mod);
751708 MemLoc, isLoad, ScanPos->getIterator(), QueryParent, QueryInst);
752709 } else if (isa(QueryInst) || isa(QueryInst)) {
753710 CallSite QueryCS(QueryInst);
754 bool isReadOnly = AA->onlyReadsMemory(QueryCS);
711 bool isReadOnly = AA.onlyReadsMemory(QueryCS);
755712 LocalCache = getCallSiteDependencyFrom(
756713 QueryCS, isReadOnly, ScanPos->getIterator(), QueryParent);
757714 } else
769726 #ifndef NDEBUG
770727 /// This method is used when -debug is specified to verify that cache arrays
771728 /// are properly kept sorted.
772 static void AssertSorted(MemoryDependenceAnalysis::NonLocalDepInfo &Cache,
729 static void AssertSorted(MemoryDependenceResults::NonLocalDepInfo &Cache,
773730 int Count = -1) {
774731 if (Count == -1)
775732 Count = Cache.size();
778735 }
779736 #endif
780737
781 const MemoryDependenceAnalysis::NonLocalDepInfo &
782 MemoryDependenceAnalysis::getNonLocalCallDependency(CallSite QueryCS) {
738 const MemoryDependenceResults::NonLocalDepInfo &
739 MemoryDependenceResults::getNonLocalCallDependency(CallSite QueryCS) {
783740 assert(getDependency(QueryCS.getInstruction()).isNonLocal() &&
784741 "getNonLocalCallDependency should only be used on calls with "
785742 "non-local deps!");
820777 }
821778
822779 // isReadonlyCall - If this is a read-only call, we can be more aggressive.
823 bool isReadonlyCall = AA->onlyReadsMemory(QueryCS);
780 bool isReadonlyCall = AA.onlyReadsMemory(QueryCS);
824781
825782 SmallPtrSet Visited;
826783
909866 return Cache;
910867 }
911868
912 void MemoryDependenceAnalysis::getNonLocalPointerDependency(
869 void MemoryDependenceResults::getNonLocalPointerDependency(
913870 Instruction *QueryInst, SmallVectorImpl &Result) {
914871 const MemoryLocation Loc = MemoryLocation::get(QueryInst);
915872 bool isLoad = isa(QueryInst);
942899 return;
943900 }
944901 const DataLayout &DL = FromBB->getModule()->getDataLayout();
945 PHITransAddr Address(const_cast(Loc.Ptr), DL, AC);
902 PHITransAddr Address(const_cast(Loc.Ptr), DL, &AC);
946903
947904 // This is the set of blocks we've inspected, and the pointer we consider in
948905 // each block. Because of critical edges, we currently bail out if querying
962919 /// info if available).
963920 ///
964921 /// If we do a lookup, add the result to the cache.
965 MemDepResult MemoryDependenceAnalysis::GetNonLocalInfoForBlock(
922 MemDepResult MemoryDependenceResults::GetNonLocalInfoForBlock(
966923 Instruction *QueryInst, const MemoryLocation &Loc, bool isLoad,
967924 BasicBlock *BB, NonLocalDepInfo *Cache, unsigned NumSortedEntries) {
968925
1032989 ///
1033990 /// This is optimized for the case when only a few entries are added.
1034991 static void
1035 SortNonLocalDepInfoCache(MemoryDependenceAnalysis::NonLocalDepInfo &Cache,
992 SortNonLocalDepInfoCache(MemoryDependenceResults::NonLocalDepInfo &Cache,
1036993 unsigned NumSortedEntries) {
1037994 switch (Cache.size() - NumSortedEntries) {
1038995 case 0:
1042999 // Two new entries, insert the last one into place.
10431000 NonLocalDepEntry Val = Cache.back();
10441001 Cache.pop_back();
1045 MemoryDependenceAnalysis::NonLocalDepInfo::iterator Entry =
1002 MemoryDependenceResults::NonLocalDepInfo::iterator Entry =
10461003 std::upper_bound(Cache.begin(), Cache.end() - 1, Val);
10471004 Cache.insert(Entry, Val);
10481005 // FALL THROUGH.
10521009 if (Cache.size() != 1) {
10531010 NonLocalDepEntry Val = Cache.back();
10541011 Cache.pop_back();
1055 MemoryDependenceAnalysis::NonLocalDepInfo::iterator Entry =
1012 MemoryDependenceResults::NonLocalDepInfo::iterator Entry =
10561013 std::upper_bound(Cache.begin(), Cache.end(), Val);
10571014 Cache.insert(Entry, Val);
10581015 }
10771034 /// This function returns true on success, or false to indicate that it could
10781035 /// not compute dependence information for some reason. This should be treated
10791036 /// as a clobber dependence on the first instruction in the predecessor block.
1080 bool MemoryDependenceAnalysis::getNonLocalPointerDepFromBB(
1037 bool MemoryDependenceResults::getNonLocalPointerDepFromBB(
10811038 Instruction *QueryInst, const PHITransAddr &Pointer,
10821039 const MemoryLocation &Loc, bool isLoad, BasicBlock *StartBB,
10831040 SmallVectorImpl &Result,
14581415 }
14591416
14601417 /// If P exists in CachedNonLocalPointerInfo, remove it.
1461 void MemoryDependenceAnalysis::RemoveCachedNonLocalPointerDependencies(
1418 void MemoryDependenceResults::RemoveCachedNonLocalPointerDependencies(
14621419 ValueIsLoadPair P) {
14631420 CachedNonLocalPointerInfo::iterator It = NonLocalPointerDeps.find(P);
14641421 if (It == NonLocalPointerDeps.end())
14821439 NonLocalPointerDeps.erase(It);
14831440 }
14841441
1485 void MemoryDependenceAnalysis::invalidateCachedPointerInfo(Value *Ptr) {
1442 void MemoryDependenceResults::invalidateCachedPointerInfo(Value *Ptr) {
14861443 // If Ptr isn't really a pointer, just ignore it.
14871444 if (!Ptr->getType()->isPointerTy())
14881445 return;
14921449 RemoveCachedNonLocalPointerDependencies(ValueIsLoadPair(Ptr, true));
14931450 }
14941451
1495 void MemoryDependenceAnalysis::invalidateCachedPredecessors() {
1452 void MemoryDependenceResults::invalidateCachedPredecessors() {
14961453 PredCache.clear();
14971454 }
14981455
1499 void MemoryDependenceAnalysis::removeInstruction(Instruction *RemInst) {
1456 void MemoryDependenceResults::removeInstruction(Instruction *RemInst) {
15001457 // Walk through the Non-local dependencies, removing this one as the value
15011458 // for any cached queries.
15021459 NonLocalDepMapType::iterator NLDI = NonLocalDeps.find(RemInst);
16581615 /// structures.
16591616 ///
16601617 /// This function verifies by asserting in debug builds.
1661 void MemoryDependenceAnalysis::verifyRemoved(Instruction *D) const {
1618 void MemoryDependenceResults::verifyRemoved(Instruction *D) const {
16621619 #ifndef NDEBUG
16631620 for (const auto &DepKV : LocalDeps) {
16641621 assert(DepKV.first != D && "Inst occurs in data structures");
17001657 }
17011658 #endif
17021659 }
1660
1661 MemoryDependenceResults
1662 MemoryDependenceAnalysis::run(Function &F, AnalysisManager *AM) {
1663 auto &AA = AM->getResult(F);
1664 auto &AC = AM->getResult(F);
1665 auto &TLI = AM->getResult(F);
1666 auto *DT = AM->getCachedResult(F);
1667 return MemoryDependenceResults(AA, AC, TLI, DT);
1668 }
1669
1670 char MemoryDependenceWrapperPass::ID = 0;
1671
1672 INITIALIZE_PASS_BEGIN(MemoryDependenceWrapperPass, "memdep",
1673 "Memory Dependence Analysis", false, true)
1674 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
1675 INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
1676 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
1677 INITIALIZE_PASS_END(MemoryDependenceWrapperPass, "memdep",
1678 "Memory Dependence Analysis", false, true)
1679
1680 MemoryDependenceWrapperPass::MemoryDependenceWrapperPass() : FunctionPass(ID) {
1681 initializeMemoryDependenceWrapperPassPass(*PassRegistry::getPassRegistry());
1682 }
1683 MemoryDependenceWrapperPass::~MemoryDependenceWrapperPass() {}
1684
1685 void MemoryDependenceWrapperPass::releaseMemory() {
1686 MemDep.reset();
1687 }
1688
1689 void MemoryDependenceWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
1690 AU.setPreservesAll();
1691 AU.addRequired();
1692 AU.addRequiredTransitive();
1693 AU.addRequiredTransitive();
1694 }
1695
1696 bool MemoryDependenceWrapperPass::runOnFunction(Function &F) {
1697 auto &AA = getAnalysis().getAAResults();
1698 auto &AC = getAnalysis().getAssumptionCache(F);
1699 auto &TLI = getAnalysis().getTLI();
1700 auto *DTWP = getAnalysisIfAvailable();
1701 MemDep.emplace(AA, AC, TLI, DTWP ? &DTWP->getDomTree() : nullptr);
1702 return false;
1703 }
1704
5858 AU.addPreserved();
5959 AU.addPreserved();
6060 AU.addPreserved();
61 AU.addPreservedAnalysis>();
61 AU.addPreservedWrapperPass>();
6262 AU.addPreserved();
6363 AU.addPreserved();
6464 AU.addPreserved();
2525 #include "llvm/Analysis/DominanceFrontier.h"
2626 #include "llvm/Analysis/LazyCallGraph.h"
2727 #include "llvm/Analysis/LoopInfo.h"
28 #include "llvm/Analysis/MemoryDependenceAnalysis.h"
2829 #include "llvm/Analysis/PostDominators.h"
2930 #include "llvm/Analysis/RegionInfo.h"
3031 #include "llvm/Analysis/ScalarEvolution.h"
5959 FUNCTION_ANALYSIS("postdomtree", PostDominatorTreeAnalysis())
6060 FUNCTION_ANALYSIS("domfrontier", DominanceFrontierAnalysis())
6161 FUNCTION_ANALYSIS("loops", LoopAnalysis())
62 FUNCTION_ANALYSIS("memdep", MemoryDependenceAnalysis())
6263 FUNCTION_ANALYSIS("regions", RegionInfoAnalysis())
6364 FUNCTION_ANALYSIS("no-op-function", NoOpFunctionAnalysis())
6465 FUNCTION_ANALYSIS("scalar-evolution", ScalarEvolutionAnalysis())
4747 namespace {
4848 struct DSE : public FunctionPass {
4949 AliasAnalysis *AA;
50 MemoryDependenceAnalysis *MD;
50 MemoryDependenceResults *MD;
5151 DominatorTree *DT;
5252 const TargetLibraryInfo *TLI;
5353
6161 return false;
6262
6363 AA = &getAnalysis().getAAResults();
64 MD = &getAnalysisAnalysis>();
64 MD = &getAnalysisWrapperPass>().getMemDep();
6565 DT = &getAnalysis().getDomTree();
6666 TLI = &getAnalysis().getTLI();
6767
8888 AU.setPreservesCFG();
8989 AU.addRequired();
9090 AU.addRequired();
91 AU.addRequiredAnalysis>();
91 AU.addRequiredWrapperPass>();
9292 AU.addRequired();
9393 AU.addPreserved();
9494 AU.addPreserved();
95 AU.addPreservedAnalysis>();
95 AU.addPreservedWrapperPass>();
9696 }
9797 };
9898 }
102102 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
103103 INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
104104 INITIALIZE_PASS_DEPENDENCY(GlobalsAAWrapperPass)
105 INITIALIZE_PASS_DEPENDENCY(MemoryDependenceAnalysis)
105 INITIALIZE_PASS_DEPENDENCY(MemoryDependenceWrapperPass)
106106 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
107107 INITIALIZE_PASS_END(DSE, "dse", "Dead Store Elimination", false, false)
108108
119119 /// If ValueSet is non-null, remove any deleted instructions from it as well.
120120 ///
121121 static void DeleteDeadInstruction(Instruction *I,
122 MemoryDependenceAnalysis &MD,
122 MemoryDependenceResults &MD,
123123 const TargetLibraryInfo &TLI,
124124 SmallSetVector *ValueSet = nullptr) {
125125 SmallVector NowDeadInsts;
111111 DenseMap valueNumbering;
112112 DenseMap expressionNumbering;
113113 AliasAnalysis *AA;
114 MemoryDependenceAnalysis *MD;
114 MemoryDependenceResults *MD;
115115 DominatorTree *DT;
116116
117117 uint32_t nextValueNumber;
134134 void erase(Value *v);
135135 void setAliasAnalysis(AliasAnalysis* A) { AA = A; }
136136 AliasAnalysis *getAliasAnalysis() const { return AA; }
137 void setMemDep(MemoryDependenceAnalysis* M) { MD = M; }
137 void setMemDep(MemoryDependenceResults* M) { MD = M; }
138138 void setDomTree(DominatorTree* D) { DT = D; }
139139 uint32_t getNextUnusedValueNumber() { return nextValueNumber; }
140140 void verifyRemoved(const Value *) const;
331331 }
332332
333333 // Non-local case.
334 const MemoryDependenceAnalysis::NonLocalDepInfo &deps =
334 const MemoryDependenceResults::NonLocalDepInfo &deps =
335335 MD->getNonLocalCallDependency(CallSite(C));
336336 // FIXME: Move the checking logic to MemDep!
337337 CallInst* cdep = nullptr;
624624
625625 class GVN : public FunctionPass {
626626 bool NoLoads;
627 MemoryDependenceAnalysis *MD;
627 MemoryDependenceResults *MD;
628628 DominatorTree *DT;
629629 const TargetLibraryInfo *TLI;
630630 AssumptionCache *AC;
670670
671671 DominatorTree &getDominatorTree() const { return *DT; }
672672 AliasAnalysis *getAliasAnalysis() const { return VN.getAliasAnalysis(); }
673 MemoryDependenceAnalysis &getMemDep() const { return *MD; }
673 MemoryDependenceResults &getMemDep() const { return *MD; }
674674 private:
675675 /// Push a new Value to the LeaderTable onto the list for its value number.
676676 void addToLeaderTable(uint32_t N, Value *V, const BasicBlock *BB) {
726726 AU.addRequired();
727727 AU.addRequired();
728728 if (!NoLoads)
729 AU.addRequiredAnalysis>();
729 AU.addRequiredWrapperPass>();
730730 AU.addRequired();
731731
732732 AU.addPreserved();
784784
785785 INITIALIZE_PASS_BEGIN(GVN, "gvn", "Global Value Numbering", false, false)
786786 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
787 INITIALIZE_PASS_DEPENDENCY(MemoryDependenceAnalysis)
787 INITIALIZE_PASS_DEPENDENCY(MemoryDependenceWrapperPass)
788788 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
789789 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
790790 INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
11041104 GetPointerBaseWithConstantOffset(LoadPtr, LoadOffs, DL);
11051105 unsigned LoadSize = DL.getTypeStoreSize(LoadTy);
11061106
1107 unsigned Size = MemoryDependenceAnalysis::getLoadLoadClobberFullWidthSize(
1107 unsigned Size = MemoryDependenceResults::getLoadLoadClobberFullWidthSize(
11081108 LoadBase, LoadOffs, LoadSize, DepLI);
11091109 if (Size == 0) return -1;
11101110
23572357 return false;
23582358
23592359 if (!NoLoads)
2360 MD = &getAnalysisAnalysis>();
2360 MD = &getAnalysisWrapperPass>().getMemDep();
23612361 DT = &getAnalysis().getDomTree();
23622362 AC = &getAnalysis().getAssumptionCache(F);
23632363 TLI = &getAnalysis().getTLI();
305305
306306 namespace {
307307 class MemCpyOpt : public FunctionPass {
308 MemoryDependenceAnalysis *MD;
308 MemoryDependenceResults *MD;
309309 TargetLibraryInfo *TLI;
310310 public:
311311 static char ID; // Pass identification, replacement for typeid
323323 AU.setPreservesCFG();
324324 AU.addRequired();
325325 AU.addRequired();
326 AU.addRequiredAnalysis>();
326 AU.addRequiredWrapperPass>();
327327 AU.addRequired();
328328 AU.addRequired();
329329 AU.addPreserved();
330 AU.addPreservedAnalysis>();
330 AU.addPreservedWrapperPass>();
331331 }
332332
333333 // Helper functions
357357 false, false)
358358 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
359359 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
360 INITIALIZE_PASS_DEPENDENCY(MemoryDependenceAnalysis)
360 INITIALIZE_PASS_DEPENDENCY(MemoryDependenceWrapperPass)
361361 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
362362 INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
363363 INITIALIZE_PASS_DEPENDENCY(GlobalsAAWrapperPass)
12831283 return false;
12841284
12851285 bool MadeChange = false;
1286 MD = &getAnalysisAnalysis>();
1286 MD = &getAnalysisWrapperPass>().getMemDep();
12871287 TLI = &getAnalysis().getTLI();
12881288
12891289 // If we don't have at least memset and memcpy, there is little point of doing
103103 namespace {
104104 class MergedLoadStoreMotion : public FunctionPass {
105105 AliasAnalysis *AA;
106 MemoryDependenceAnalysis *MD;
106 MemoryDependenceResults *MD;
107107
108108 public:
109109 static char ID; // Pass identification, replacement for typeid
121121 AU.addRequired();
122122 AU.addRequired();
123123 AU.addPreserved();
124 AU.addPreservedAnalysis>();
124 AU.addPreservedWrapperPass>();
125125 }
126126
127127 // Helper routines
169169
170170 INITIALIZE_PASS_BEGIN(MergedLoadStoreMotion, "mldst-motion",
171171 "MergedLoadStoreMotion", false, false)
172 INITIALIZE_PASS_DEPENDENCY(MemoryDependenceAnalysis)
172 INITIALIZE_PASS_DEPENDENCY(MemoryDependenceWrapperPass)
173173 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
174174 INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
175175 INITIALIZE_PASS_DEPENDENCY(GlobalsAAWrapperPass)
564564 /// \brief Run the transformation for each function
565565 ///
566566 bool MergedLoadStoreMotion::runOnFunction(Function &F) {
567 MD = getAnalysisIfAvailables>();
567 auto *MDWP = getAnalysisIfAvailables>();
568 MD = MDWP ? &MDWP->getMemDep() : nullptr;
568569 AA = &getAnalysis().getAAResults();
569570
570571 bool Changed = false;
6565 /// when all entries to the PHI nodes in a block are guaranteed equal, such as
6666 /// when the block has exactly one predecessor.
6767 void llvm::FoldSingleEntryPHINodes(BasicBlock *BB,
68 MemoryDependenceAnalysis *MemDep) {
68 MemoryDependenceResults *MemDep) {
6969 if (!isa(BB->begin())) return;
7070
7171 while (PHINode *PN = dyn_cast(BB->begin())) {
106106 /// if possible. The return value indicates success or failure.
107107 bool llvm::MergeBlockIntoPredecessor(BasicBlock *BB, DominatorTree *DT,
108108 LoopInfo *LI,
109 MemoryDependenceAnalysis *MemDep) {
109 MemoryDependenceResults *MemDep) {
110110 // Don't merge away blocks who have their address taken.
111111 if (BB->hasAddressTaken()) return false;
112112
314314 ; CHECK-AA: Running analysis: BasicAA
315315 ; CHECK-AA: Finished llvm::Module pass manager run
316316
317 ; RUN: opt -disable-output -disable-verify -debug-pass-manager %s 2>&1 \
318 ; RUN: -passes='require' \
319 ; RUN: | FileCheck %s --check-prefix=CHECK-MEMDEP
320 ; CHECK-MEMDEP: Starting llvm::Module pass manager run
321 ; CHECK-MEMDEP: Running pass: RequireAnalysisPass
322 ; CHECK-MEMDEP: Running analysis: MemoryDependenceAnalysis
323 ; CHECK-MEMDEP: Finished llvm::Module pass manager run
324
317325 ; RUN: opt -disable-output -disable-verify -debug-pass-manager \
318326 ; RUN: -passes='default' %s 2>&1 \
319327 ; RUN: | FileCheck %s --check-prefix=CHECK-O2