//===--- PostOrderAnalysis.h - SIL POT and RPOT Analysis --------*- C++ -*-===// // // This source file is part of the Swift.org open source project // // Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors // Licensed under Apache License v2.0 with Runtime Library Exception // // See https://swift.org/LICENSE.txt for license information // See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors // //===----------------------------------------------------------------------===// #ifndef SWIFT_SILOPTIMIZER_ANALYSIS_POSTORDERANALYSIS_H #define SWIFT_SILOPTIMIZER_ANALYSIS_POSTORDERANALYSIS_H #include "swift/SILOptimizer/Analysis/Analysis.h" #include "swift/Basic/Range.h" #include "swift/SIL/CFG.h" #include "swift/SIL/SILBasicBlock.h" #include "swift/SIL/SILFunction.h" #include "llvm/ADT/PostOrderIterator.h" #include "llvm/ADT/iterator_range.h" #include "llvm/ADT/DenseMap.h" #include namespace swift { class SILBasicBlock; class SILFunction; class PostOrderFunctionInfo { std::vector PostOrder; llvm::DenseMap BBToPOMap; public: PostOrderFunctionInfo(SILFunction *F) { for (auto *BB : make_range(po_begin(F), po_end(F))) { BBToPOMap[BB] = PostOrder.size(); PostOrder.push_back(BB); } } using iterator = decltype(PostOrder)::iterator; using const_iterator = decltype(PostOrder)::const_iterator; using reverse_iterator = decltype(PostOrder)::reverse_iterator; using const_reverse_iterator = decltype(PostOrder)::const_reverse_iterator; using range = iterator_range; using const_range = iterator_range; using reverse_range = iterator_range; using const_reverse_range = iterator_range; range getPostOrder() { return make_range(PostOrder.begin(), PostOrder.end()); } const_range getPostOrder() const { return make_range(PostOrder.begin(), PostOrder.end()); } reverse_range getReversePostOrder() { return make_range(PostOrder.rbegin(), PostOrder.rend()); } const_reverse_range getReversePostOrder() const { return make_range(PostOrder.rbegin(), PostOrder.rend()); } using enumerated_range = EnumeratorRange; enumerated_range getEnumeratedPostOrder() { return enumerate(PostOrder); } using reverse_enumerated_range = EnumeratorRange; reverse_enumerated_range getEnumeratedReversePostOrder() { return enumerate(PostOrder.rbegin(), PostOrder.rend()); } unsigned size() const { return PostOrder.size(); } Optional getPONumber(SILBasicBlock *BB) const { auto Iter = BBToPOMap.find(BB); if (Iter != BBToPOMap.end()) return Iter->second; return None; } Optional getRPONumber(SILBasicBlock *BB) const { auto Iter = BBToPOMap.find(BB); if (Iter != BBToPOMap.end()) return PostOrder.size() - Iter->second - 1; return None; } }; /// This class is a simple wrapper around the POT iterator provided by LLVM. It /// lazily re-evaluates the post order when it is invalidated so that we do not /// reform the post order over and over again (it can be expensive). class PostOrderAnalysis : public FunctionAnalysisBase { protected: virtual PostOrderFunctionInfo *newFunctionAnalysis(SILFunction *F) override { return new PostOrderFunctionInfo(F); } virtual bool shouldInvalidate(SILAnalysis::InvalidationKind K) override { return K & InvalidationKind::Branches; } public: PostOrderAnalysis() : FunctionAnalysisBase(AnalysisKind::PostOrder) {} // This is a cache and shouldn't be copied around. PostOrderAnalysis(const PostOrderAnalysis &) = delete; PostOrderAnalysis &operator=(const PostOrderAnalysis &) = delete; static bool classof(const SILAnalysis *S) { return S->getKind() == AnalysisKind::PostOrder; } }; } // end namespace swift #endif