1 //===- LoopAnalysisManager.cpp - Loop analysis management -----------------===// 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 #include "llvm/Analysis/LoopAnalysisManager.h" 10 #include "llvm/Analysis/BasicAliasAnalysis.h" 11 #include "llvm/Analysis/GlobalsModRef.h" 12 #include "llvm/Analysis/LoopInfo.h" 13 #include "llvm/Analysis/MemorySSA.h" 14 #include "llvm/Analysis/ScalarEvolution.h" 15 #include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h" 16 #include "llvm/IR/Dominators.h" 17 #include "llvm/IR/PassManagerImpl.h" 18 19 using namespace llvm; 20 21 namespace llvm { 22 // Explicit template instantiations and specialization definitions for core 23 // template typedefs. 24 template class AllAnalysesOn<Loop>; 25 template class AnalysisManager<Loop, LoopStandardAnalysisResults &>; 26 template class InnerAnalysisManagerProxy<LoopAnalysisManager, Function>; 27 template class OuterAnalysisManagerProxy<FunctionAnalysisManager, Loop, 28 LoopStandardAnalysisResults &>; 29 30 bool LoopAnalysisManagerFunctionProxy::Result::invalidate( 31 Function &F, const PreservedAnalyses &PA, 32 FunctionAnalysisManager::Invalidator &Inv) { 33 // First compute the sequence of IR units covered by this proxy. We will want 34 // to visit this in postorder, but because this is a tree structure we can do 35 // this by building a preorder sequence and walking it backwards. We also 36 // want siblings in forward program order to match the LoopPassManager so we 37 // get the preorder with siblings reversed. 38 SmallVector<Loop *, 4> PreOrderLoops = LI->getLoopsInReverseSiblingPreorder(); 39 40 // If this proxy or the loop info is going to be invalidated, we also need 41 // to clear all the keys coming from that analysis. We also completely blow 42 // away the loop analyses if any of the standard analyses provided by the 43 // loop pass manager go away so that loop analyses can freely use these 44 // without worrying about declaring dependencies on them etc. 45 // FIXME: It isn't clear if this is the right tradeoff. We could instead make 46 // loop analyses declare any dependencies on these and use the more general 47 // invalidation logic below to act on that. 48 auto PAC = PA.getChecker<LoopAnalysisManagerFunctionProxy>(); 49 bool invalidateMemorySSAAnalysis = false; 50 if (MSSAUsed) 51 invalidateMemorySSAAnalysis = Inv.invalidate<MemorySSAAnalysis>(F, PA); 52 if (!(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>()) || 53 Inv.invalidate<AAManager>(F, PA) || 54 Inv.invalidate<AssumptionAnalysis>(F, PA) || 55 Inv.invalidate<DominatorTreeAnalysis>(F, PA) || 56 Inv.invalidate<LoopAnalysis>(F, PA) || 57 Inv.invalidate<ScalarEvolutionAnalysis>(F, PA) || 58 invalidateMemorySSAAnalysis) { 59 // Note that the LoopInfo may be stale at this point, however the loop 60 // objects themselves remain the only viable keys that could be in the 61 // analysis manager's cache. So we just walk the keys and forcibly clear 62 // those results. Note that the order doesn't matter here as this will just 63 // directly destroy the results without calling methods on them. 64 for (Loop *L : PreOrderLoops) { 65 // NB! `L` may not be in a good enough state to run Loop::getName. 66 InnerAM->clear(*L, "<possibly invalidated loop>"); 67 } 68 69 // We also need to null out the inner AM so that when the object gets 70 // destroyed as invalid we don't try to clear the inner AM again. At that 71 // point we won't be able to reliably walk the loops for this function and 72 // only clear results associated with those loops the way we do here. 73 // FIXME: Making InnerAM null at this point isn't very nice. Most analyses 74 // try to remain valid during invalidation. Maybe we should add an 75 // `IsClean` flag? 76 InnerAM = nullptr; 77 78 // Now return true to indicate this *is* invalid and a fresh proxy result 79 // needs to be built. This is especially important given the null InnerAM. 80 return true; 81 } 82 83 // Directly check if the relevant set is preserved so we can short circuit 84 // invalidating loops. 85 bool AreLoopAnalysesPreserved = 86 PA.allAnalysesInSetPreserved<AllAnalysesOn<Loop>>(); 87 88 // Since we have a valid LoopInfo we can actually leave the cached results in 89 // the analysis manager associated with the Loop keys, but we need to 90 // propagate any necessary invalidation logic into them. We'd like to 91 // invalidate things in roughly the same order as they were put into the 92 // cache and so we walk the preorder list in reverse to form a valid 93 // postorder. 94 for (Loop *L : reverse(PreOrderLoops)) { 95 Optional<PreservedAnalyses> InnerPA; 96 97 // Check to see whether the preserved set needs to be adjusted based on 98 // function-level analysis invalidation triggering deferred invalidation 99 // for this loop. 100 if (auto *OuterProxy = 101 InnerAM->getCachedResult<FunctionAnalysisManagerLoopProxy>(*L)) 102 for (const auto &OuterInvalidationPair : 103 OuterProxy->getOuterInvalidations()) { 104 AnalysisKey *OuterAnalysisID = OuterInvalidationPair.first; 105 const auto &InnerAnalysisIDs = OuterInvalidationPair.second; 106 if (Inv.invalidate(OuterAnalysisID, F, PA)) { 107 if (!InnerPA) 108 InnerPA = PA; 109 for (AnalysisKey *InnerAnalysisID : InnerAnalysisIDs) 110 InnerPA->abandon(InnerAnalysisID); 111 } 112 } 113 114 // Check if we needed a custom PA set. If so we'll need to run the inner 115 // invalidation. 116 if (InnerPA) { 117 InnerAM->invalidate(*L, *InnerPA); 118 continue; 119 } 120 121 // Otherwise we only need to do invalidation if the original PA set didn't 122 // preserve all Loop analyses. 123 if (!AreLoopAnalysesPreserved) 124 InnerAM->invalidate(*L, PA); 125 } 126 127 // Return false to indicate that this result is still a valid proxy. 128 return false; 129 } 130 131 template <> 132 LoopAnalysisManagerFunctionProxy::Result 133 LoopAnalysisManagerFunctionProxy::run(Function &F, 134 FunctionAnalysisManager &AM) { 135 return Result(*InnerAM, AM.getResult<LoopAnalysis>(F)); 136 } 137 } 138 139 PreservedAnalyses llvm::getLoopPassPreservedAnalyses() { 140 PreservedAnalyses PA; 141 PA.preserve<DominatorTreeAnalysis>(); 142 PA.preserve<LoopAnalysis>(); 143 PA.preserve<LoopAnalysisManagerFunctionProxy>(); 144 PA.preserve<ScalarEvolutionAnalysis>(); 145 // FIXME: What we really want to do here is preserve an AA category, but that 146 // concept doesn't exist yet. 147 PA.preserve<AAManager>(); 148 PA.preserve<BasicAA>(); 149 PA.preserve<GlobalsAA>(); 150 PA.preserve<SCEVAA>(); 151 return PA; 152 } 153