//===-------------- LoopAnalysis.h - SIL Loop Analysis -*- C++ -*----------===// // // This source file is part of the Swift.org open source project // // Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors // Licensed under Apache License v2.0 with Runtime Library Exception // // See http://swift.org/LICENSE.txt for license information // See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors // //===----------------------------------------------------------------------===// #ifndef SWIFT_SILANALYSIS_LOOPINFOANALYSIS_H #define SWIFT_SILANALYSIS_LOOPINFOANALYSIS_H #include "swift/SIL/CFG.h" #include "swift/SIL/LoopInfo.h" #include "swift/SIL/SILBasicBlock.h" #include "swift/SILAnalysis/Analysis.h" #include "llvm/ADT/DenseMap.h" namespace swift { class DominanceInfo; class SILLoop; class SILPassManager; } namespace swift { /// Computes natural loop information for SIL basic blocks. class SILLoopAnalysis : public SILAnalysis { using LoopInfoMap = llvm::DenseMap; LoopInfoMap LoopInfos; SILPassManager *PM; public: SILLoopAnalysis(SILModule *, SILPassManager *PM) : SILAnalysis(AnalysisKind::LoopInfo), PM(PM) {} virtual ~SILLoopAnalysis() { for (auto LI : LoopInfos) delete LI.second; } static bool classof(const SILAnalysis *S) { return S->getKind() == AnalysisKind::LoopInfo; } virtual void invalidate(InvalidationKind K) { if (K >= InvalidationKind::CFG) { for (auto LI : LoopInfos) delete LI.second; // Clear the maps. LoopInfos.clear(); } } virtual void invalidate(SILFunction* F, InvalidationKind K) { if (K >= InvalidationKind::CFG) { if (LoopInfos.count(F)) { delete LoopInfos[F]; LoopInfos.erase(F); } } } // Computes loop information for the function using dominance information or // returns a cached result if available. SILLoopInfo *getLoopInfo(SILFunction *F); /// Update the loop information with the passed analysis info. /// Takes ownership of the analysis info. void updateAnalysis(SILFunction *F, std::unique_ptr Info) { if (LoopInfos.count(F)) { assert(LoopInfos[F] != Info.get()); delete LoopInfos[F]; } LoopInfos[F] = Info.release(); } /// Release ownership of the dominance information for the function. The /// returned unique_ptr takes ownership of the object. std::unique_ptr preserveAnalysis(SILFunction *F) { assert(LoopInfos.count(F)); std::unique_ptr Info(LoopInfos[F]); LoopInfos.erase(F); return Info; } }; } // end namespace swift #endif