|
| 1 | +//===--- NonLocalAccessBlockAnalysis.h - Nonlocal end_access ----*- C++ -*-===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2021 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | +/// |
| 13 | +/// Cache the set of blocks that contain a non-local end_access, which is a rare |
| 14 | +/// occurrence. Optimizations that are driven by a known-use analysis, such as |
| 15 | +/// CanonicalOSSA, don't need to scan instructions that are unrelated to the SSA |
| 16 | +/// def-use graph. However, they may still need to be aware of unrelated access |
| 17 | +/// scope boundaries. By querying this analysis, they can avoid scanning all |
| 18 | +/// instructions just to deal with the extremely rare case of an end_access that |
| 19 | +/// spans blocks within the relevant SSA lifetime. |
| 20 | +/// |
| 21 | +/// By default, this analysis is invalidated whenever instructions or blocks are |
| 22 | +/// changed, but it should ideally be preserved by passes that invalidate |
| 23 | +/// instructions but don't create any new access scopes or move end_access |
| 24 | +/// across blocks, which is unusual. |
| 25 | +/// |
| 26 | +//===----------------------------------------------------------------------===// |
| 27 | + |
| 28 | +#ifndef SWIFT_SILOPTIMIZER_ANALYSIS_NONLOCALACCESSBLOCKS_H |
| 29 | +#define SWIFT_SILOPTIMIZER_ANALYSIS_NONLOCALACCESSBLOCKS_H |
| 30 | + |
| 31 | +#include "swift/Basic/Compiler.h" |
| 32 | +#include "swift/SILOptimizer/Analysis/Analysis.h" |
| 33 | +#include "llvm/ADT/SmallPtrSet.h" |
| 34 | + |
| 35 | +namespace swift { |
| 36 | + |
| 37 | +class SILBasicBlock; |
| 38 | +class SILFunction; |
| 39 | + |
| 40 | +class NonLocalAccessBlocks { |
| 41 | + friend class NonLocalAccessBlockAnalysis; |
| 42 | + |
| 43 | + SILFunction *function; |
| 44 | + llvm::SmallPtrSet<SILBasicBlock *, 4> accessBlocks; |
| 45 | + |
| 46 | +public: |
| 47 | + NonLocalAccessBlocks(SILFunction *function) : function(function) {} |
| 48 | + |
| 49 | + SILFunction *getFunction() const { return function; } |
| 50 | + |
| 51 | + bool containsNonLocalEndAccess(SILBasicBlock *block) const { |
| 52 | + return accessBlocks.count(block); |
| 53 | + } |
| 54 | + |
| 55 | + /// Perform NonLocalAccessBlockAnalysis for this function. Populate |
| 56 | + /// this->accessBlocks with all blocks containing a non-local end_access. |
| 57 | + void compute(); |
| 58 | +}; |
| 59 | + |
| 60 | +class NonLocalAccessBlockAnalysis |
| 61 | + : public FunctionAnalysisBase<NonLocalAccessBlocks> { |
| 62 | +public: |
| 63 | + static bool classof(const SILAnalysis *S) { |
| 64 | + return S->getKind() == SILAnalysisKind::NonLocalAccessBlock; |
| 65 | + } |
| 66 | + NonLocalAccessBlockAnalysis() |
| 67 | + : FunctionAnalysisBase<NonLocalAccessBlocks>( |
| 68 | + SILAnalysisKind::NonLocalAccessBlock) {} |
| 69 | + |
| 70 | + NonLocalAccessBlockAnalysis(const NonLocalAccessBlockAnalysis &) = delete; |
| 71 | + |
| 72 | + NonLocalAccessBlockAnalysis & |
| 73 | + operator=(const NonLocalAccessBlockAnalysis &) = delete; |
| 74 | + |
| 75 | +protected: |
| 76 | + virtual std::unique_ptr<NonLocalAccessBlocks> |
| 77 | + newFunctionAnalysis(SILFunction *function) override { |
| 78 | + auto result = std::make_unique<NonLocalAccessBlocks>(function); |
| 79 | + result->compute(); |
| 80 | + return result; |
| 81 | + } |
| 82 | + |
| 83 | + virtual bool shouldInvalidate(SILAnalysis::InvalidationKind kind) override { |
| 84 | + return kind & InvalidationKind::BranchesAndInstructions; |
| 85 | + } |
| 86 | + |
| 87 | + SWIFT_ASSERT_ONLY_DECL( |
| 88 | + virtual void verify(NonLocalAccessBlocks *accessBlocks) const override { |
| 89 | + NonLocalAccessBlocks checkAccessBlocks(accessBlocks->function); |
| 90 | + checkAccessBlocks.compute(); |
| 91 | + assert(checkAccessBlocks.accessBlocks == accessBlocks->accessBlocks); |
| 92 | + }) |
| 93 | +}; |
| 94 | + |
| 95 | +} // end namespace swift |
| 96 | + |
| 97 | +#endif |
0 commit comments