10b57cec5SDimitry Andric //===- LoopAnalysisManager.cpp - Loop analysis management -----------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric
90b57cec5SDimitry Andric #include "llvm/Analysis/LoopAnalysisManager.h"
10*af732203SDimitry Andric #include "llvm/Analysis/AssumptionCache.h"
110b57cec5SDimitry Andric #include "llvm/Analysis/BasicAliasAnalysis.h"
120b57cec5SDimitry Andric #include "llvm/Analysis/GlobalsModRef.h"
130b57cec5SDimitry Andric #include "llvm/Analysis/LoopInfo.h"
140b57cec5SDimitry Andric #include "llvm/Analysis/MemorySSA.h"
150b57cec5SDimitry Andric #include "llvm/Analysis/ScalarEvolution.h"
160b57cec5SDimitry Andric #include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
170b57cec5SDimitry Andric #include "llvm/IR/Dominators.h"
185ffd83dbSDimitry Andric #include "llvm/IR/PassManagerImpl.h"
190b57cec5SDimitry Andric
200b57cec5SDimitry Andric using namespace llvm;
210b57cec5SDimitry Andric
220b57cec5SDimitry Andric namespace llvm {
230b57cec5SDimitry Andric // Explicit template instantiations and specialization definitions for core
240b57cec5SDimitry Andric // template typedefs.
250b57cec5SDimitry Andric template class AllAnalysesOn<Loop>;
260b57cec5SDimitry Andric template class AnalysisManager<Loop, LoopStandardAnalysisResults &>;
270b57cec5SDimitry Andric template class InnerAnalysisManagerProxy<LoopAnalysisManager, Function>;
280b57cec5SDimitry Andric template class OuterAnalysisManagerProxy<FunctionAnalysisManager, Loop,
290b57cec5SDimitry Andric LoopStandardAnalysisResults &>;
300b57cec5SDimitry Andric
invalidate(Function & F,const PreservedAnalyses & PA,FunctionAnalysisManager::Invalidator & Inv)310b57cec5SDimitry Andric bool LoopAnalysisManagerFunctionProxy::Result::invalidate(
320b57cec5SDimitry Andric Function &F, const PreservedAnalyses &PA,
330b57cec5SDimitry Andric FunctionAnalysisManager::Invalidator &Inv) {
340b57cec5SDimitry Andric // First compute the sequence of IR units covered by this proxy. We will want
350b57cec5SDimitry Andric // to visit this in postorder, but because this is a tree structure we can do
360b57cec5SDimitry Andric // this by building a preorder sequence and walking it backwards. We also
370b57cec5SDimitry Andric // want siblings in forward program order to match the LoopPassManager so we
380b57cec5SDimitry Andric // get the preorder with siblings reversed.
390b57cec5SDimitry Andric SmallVector<Loop *, 4> PreOrderLoops = LI->getLoopsInReverseSiblingPreorder();
400b57cec5SDimitry Andric
410b57cec5SDimitry Andric // If this proxy or the loop info is going to be invalidated, we also need
420b57cec5SDimitry Andric // to clear all the keys coming from that analysis. We also completely blow
430b57cec5SDimitry Andric // away the loop analyses if any of the standard analyses provided by the
440b57cec5SDimitry Andric // loop pass manager go away so that loop analyses can freely use these
450b57cec5SDimitry Andric // without worrying about declaring dependencies on them etc.
460b57cec5SDimitry Andric // FIXME: It isn't clear if this is the right tradeoff. We could instead make
470b57cec5SDimitry Andric // loop analyses declare any dependencies on these and use the more general
480b57cec5SDimitry Andric // invalidation logic below to act on that.
490b57cec5SDimitry Andric auto PAC = PA.getChecker<LoopAnalysisManagerFunctionProxy>();
500b57cec5SDimitry Andric bool invalidateMemorySSAAnalysis = false;
518bcb0991SDimitry Andric if (MSSAUsed)
520b57cec5SDimitry Andric invalidateMemorySSAAnalysis = Inv.invalidate<MemorySSAAnalysis>(F, PA);
530b57cec5SDimitry Andric if (!(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>()) ||
540b57cec5SDimitry Andric Inv.invalidate<AAManager>(F, PA) ||
550b57cec5SDimitry Andric Inv.invalidate<AssumptionAnalysis>(F, PA) ||
560b57cec5SDimitry Andric Inv.invalidate<DominatorTreeAnalysis>(F, PA) ||
570b57cec5SDimitry Andric Inv.invalidate<LoopAnalysis>(F, PA) ||
580b57cec5SDimitry Andric Inv.invalidate<ScalarEvolutionAnalysis>(F, PA) ||
590b57cec5SDimitry Andric invalidateMemorySSAAnalysis) {
600b57cec5SDimitry Andric // Note that the LoopInfo may be stale at this point, however the loop
610b57cec5SDimitry Andric // objects themselves remain the only viable keys that could be in the
620b57cec5SDimitry Andric // analysis manager's cache. So we just walk the keys and forcibly clear
630b57cec5SDimitry Andric // those results. Note that the order doesn't matter here as this will just
640b57cec5SDimitry Andric // directly destroy the results without calling methods on them.
650b57cec5SDimitry Andric for (Loop *L : PreOrderLoops) {
660b57cec5SDimitry Andric // NB! `L` may not be in a good enough state to run Loop::getName.
670b57cec5SDimitry Andric InnerAM->clear(*L, "<possibly invalidated loop>");
680b57cec5SDimitry Andric }
690b57cec5SDimitry Andric
700b57cec5SDimitry Andric // We also need to null out the inner AM so that when the object gets
710b57cec5SDimitry Andric // destroyed as invalid we don't try to clear the inner AM again. At that
720b57cec5SDimitry Andric // point we won't be able to reliably walk the loops for this function and
730b57cec5SDimitry Andric // only clear results associated with those loops the way we do here.
740b57cec5SDimitry Andric // FIXME: Making InnerAM null at this point isn't very nice. Most analyses
750b57cec5SDimitry Andric // try to remain valid during invalidation. Maybe we should add an
760b57cec5SDimitry Andric // `IsClean` flag?
770b57cec5SDimitry Andric InnerAM = nullptr;
780b57cec5SDimitry Andric
790b57cec5SDimitry Andric // Now return true to indicate this *is* invalid and a fresh proxy result
800b57cec5SDimitry Andric // needs to be built. This is especially important given the null InnerAM.
810b57cec5SDimitry Andric return true;
820b57cec5SDimitry Andric }
830b57cec5SDimitry Andric
840b57cec5SDimitry Andric // Directly check if the relevant set is preserved so we can short circuit
850b57cec5SDimitry Andric // invalidating loops.
860b57cec5SDimitry Andric bool AreLoopAnalysesPreserved =
870b57cec5SDimitry Andric PA.allAnalysesInSetPreserved<AllAnalysesOn<Loop>>();
880b57cec5SDimitry Andric
890b57cec5SDimitry Andric // Since we have a valid LoopInfo we can actually leave the cached results in
900b57cec5SDimitry Andric // the analysis manager associated with the Loop keys, but we need to
910b57cec5SDimitry Andric // propagate any necessary invalidation logic into them. We'd like to
920b57cec5SDimitry Andric // invalidate things in roughly the same order as they were put into the
930b57cec5SDimitry Andric // cache and so we walk the preorder list in reverse to form a valid
940b57cec5SDimitry Andric // postorder.
950b57cec5SDimitry Andric for (Loop *L : reverse(PreOrderLoops)) {
960b57cec5SDimitry Andric Optional<PreservedAnalyses> InnerPA;
970b57cec5SDimitry Andric
980b57cec5SDimitry Andric // Check to see whether the preserved set needs to be adjusted based on
990b57cec5SDimitry Andric // function-level analysis invalidation triggering deferred invalidation
1000b57cec5SDimitry Andric // for this loop.
1010b57cec5SDimitry Andric if (auto *OuterProxy =
1020b57cec5SDimitry Andric InnerAM->getCachedResult<FunctionAnalysisManagerLoopProxy>(*L))
1030b57cec5SDimitry Andric for (const auto &OuterInvalidationPair :
1040b57cec5SDimitry Andric OuterProxy->getOuterInvalidations()) {
1050b57cec5SDimitry Andric AnalysisKey *OuterAnalysisID = OuterInvalidationPair.first;
1060b57cec5SDimitry Andric const auto &InnerAnalysisIDs = OuterInvalidationPair.second;
1070b57cec5SDimitry Andric if (Inv.invalidate(OuterAnalysisID, F, PA)) {
1080b57cec5SDimitry Andric if (!InnerPA)
1090b57cec5SDimitry Andric InnerPA = PA;
1100b57cec5SDimitry Andric for (AnalysisKey *InnerAnalysisID : InnerAnalysisIDs)
1110b57cec5SDimitry Andric InnerPA->abandon(InnerAnalysisID);
1120b57cec5SDimitry Andric }
1130b57cec5SDimitry Andric }
1140b57cec5SDimitry Andric
1150b57cec5SDimitry Andric // Check if we needed a custom PA set. If so we'll need to run the inner
1160b57cec5SDimitry Andric // invalidation.
1170b57cec5SDimitry Andric if (InnerPA) {
1180b57cec5SDimitry Andric InnerAM->invalidate(*L, *InnerPA);
1190b57cec5SDimitry Andric continue;
1200b57cec5SDimitry Andric }
1210b57cec5SDimitry Andric
1220b57cec5SDimitry Andric // Otherwise we only need to do invalidation if the original PA set didn't
1230b57cec5SDimitry Andric // preserve all Loop analyses.
1240b57cec5SDimitry Andric if (!AreLoopAnalysesPreserved)
1250b57cec5SDimitry Andric InnerAM->invalidate(*L, PA);
1260b57cec5SDimitry Andric }
1270b57cec5SDimitry Andric
1280b57cec5SDimitry Andric // Return false to indicate that this result is still a valid proxy.
1290b57cec5SDimitry Andric return false;
1300b57cec5SDimitry Andric }
1310b57cec5SDimitry Andric
1320b57cec5SDimitry Andric template <>
1330b57cec5SDimitry Andric LoopAnalysisManagerFunctionProxy::Result
run(Function & F,FunctionAnalysisManager & AM)1340b57cec5SDimitry Andric LoopAnalysisManagerFunctionProxy::run(Function &F,
1350b57cec5SDimitry Andric FunctionAnalysisManager &AM) {
1360b57cec5SDimitry Andric return Result(*InnerAM, AM.getResult<LoopAnalysis>(F));
1370b57cec5SDimitry Andric }
1380b57cec5SDimitry Andric }
1390b57cec5SDimitry Andric
getLoopPassPreservedAnalyses()1400b57cec5SDimitry Andric PreservedAnalyses llvm::getLoopPassPreservedAnalyses() {
1410b57cec5SDimitry Andric PreservedAnalyses PA;
1420b57cec5SDimitry Andric PA.preserve<DominatorTreeAnalysis>();
1430b57cec5SDimitry Andric PA.preserve<LoopAnalysis>();
1440b57cec5SDimitry Andric PA.preserve<LoopAnalysisManagerFunctionProxy>();
1450b57cec5SDimitry Andric PA.preserve<ScalarEvolutionAnalysis>();
1460b57cec5SDimitry Andric return PA;
1470b57cec5SDimitry Andric }
148