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