LCOV - code coverage report
Current view: top level - lib/Analysis - LoopAnalysisManager.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 43 43 100.0 %
Date: 2017-09-14 15:23:50 Functions: 3 3 100.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.13