|
| 1 | +//===--- AccessedStorageAnalysis.h - Accessed Storage Analysis --*- C++ -*-===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2018 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 | +// This file implements an interprocedural analysis pass that summarizes the |
| 14 | +// dynamically enforced formal accesses within a function. These summaries are |
| 15 | +// used by AccessEnforcementOpts to locally fold access scopes and remove |
| 16 | +// dynamic checks based on whole module analysis. |
| 17 | +// |
| 18 | +// Note: This can be easily augmented to simultaneously compute |
| 19 | +// FunctionSideEffects by adding FunctionSideEffects as a member of |
| 20 | +// FunctionAccessedStorage. However, currently, the only use of |
| 21 | +// FunctionAccessedStorage is local to summarizeFunction(). |
| 22 | +// |
| 23 | +//===----------------------------------------------------------------------===// |
| 24 | +#ifndef SWIFT_SILOPTIMIZER_ANALYSIS_ACCESSED_STORAGE_ANALYSIS_H_ |
| 25 | +#define SWIFT_SILOPTIMIZER_ANALYSIS_ACCESSED_STORAGE_ANALYSIS_H_ |
| 26 | + |
| 27 | +#include "swift/SIL/MemAccessUtils.h" |
| 28 | +#include "swift/SIL/SILFunction.h" |
| 29 | +#include "swift/SIL/SILInstruction.h" |
| 30 | +#include "swift/SILOptimizer/Analysis/SideEffectAnalysis.h" |
| 31 | + |
| 32 | +namespace swift { |
| 33 | + |
| 34 | +/// Information about a formal access within a function pertaining to a |
| 35 | +/// particular AccessedStorage location. |
| 36 | +struct StorageAccessInfo { |
| 37 | + SILAccessKind accessKind; |
| 38 | + bool noNestedConflict = false; |
| 39 | + |
| 40 | + StorageAccessInfo() {} |
| 41 | + |
| 42 | + template <typename B> |
| 43 | + explicit StorageAccessInfo(B *beginAccess) |
| 44 | + : accessKind(beginAccess->getAccessKind()), |
| 45 | + noNestedConflict(beginAccess->hasNoNestedConflict()) {} |
| 46 | + |
| 47 | + bool mergeFrom(const StorageAccessInfo &RHS); |
| 48 | +}; |
| 49 | + |
| 50 | +/// The per-function result of AccessedStorageAnalysis. |
| 51 | +/// |
| 52 | +/// Maps each unique AccessedStorage location to StorageAccessInfo. |
| 53 | +/// |
| 54 | +/// Any unidentified accesses are summarized as a single unidentifiedAccess |
| 55 | +/// property. |
| 56 | +class FunctionAccessedStorage { |
| 57 | + using AccessedStorageMap = |
| 58 | + llvm::SmallDenseMap<AccessedStorage, StorageAccessInfo, 8>; |
| 59 | + |
| 60 | + AccessedStorageMap storageAccessMap; |
| 61 | + Optional<SILAccessKind> unidentifiedAccess; |
| 62 | + |
| 63 | +public: |
| 64 | + FunctionAccessedStorage() {} |
| 65 | + |
| 66 | + void clear() { |
| 67 | + storageAccessMap.clear(); |
| 68 | + unidentifiedAccess = None; |
| 69 | + } |
| 70 | + |
| 71 | + /// Sets the most conservative effects, if we don't know anything about the |
| 72 | + /// function. |
| 73 | + void setWorstEffects() { |
| 74 | + storageAccessMap.clear(); |
| 75 | + unidentifiedAccess = SILAccessKind::Modify; |
| 76 | + } |
| 77 | + |
| 78 | + /// Summarize the given function's effects using this FunctionAccessedStorage |
| 79 | + /// object. |
| 80 | + // |
| 81 | + // Return true if the function's' effects have been fully summarized without |
| 82 | + // visiting it's body. |
| 83 | + bool summarizeFunction(SILFunction *F); |
| 84 | + |
| 85 | + /// Summarize the callee side effects of a call instruction using this |
| 86 | + /// FunctionAccessedStorage object without analyzing the callee function |
| 87 | + /// bodies or scheduling the callees for bottom-up propagation. |
| 88 | + /// |
| 89 | + /// The side effects are represented from the callee's perspective. Parameter |
| 90 | + /// effects are not translated into information on the caller's argument, and |
| 91 | + /// local effects are not dropped. |
| 92 | + /// |
| 93 | + /// Return true if this call-site's effects are summarized without visiting |
| 94 | + /// the callee. |
| 95 | + /// |
| 96 | + /// TODO: Summarize ArraySemanticsCall accesses. |
| 97 | + bool summarizeCall(FullApplySite fullApply) { |
| 98 | + assert(storageAccessMap.empty() && "expected uninitialized results."); |
| 99 | + return false; |
| 100 | + } |
| 101 | + |
| 102 | + /// Merge effects directly from \p RHS. |
| 103 | + bool mergeFrom(const FunctionAccessedStorage &RHS); |
| 104 | + |
| 105 | + /// Merge the effects represented in calleeAccess into this |
| 106 | + /// FunctionAccessedStorage object. calleeAccess must correspond to at least |
| 107 | + /// one callee at the apply site `fullApply`. Merging drops any local effects, |
| 108 | + /// and translates parameter effects into effects on the caller-side |
| 109 | + /// arguments. |
| 110 | + /// |
| 111 | + /// The full caller-side effects at a call site can be obtained with |
| 112 | + /// AccessedStorageAnalysis::getCallSiteEffects(). |
| 113 | + bool mergeFromApply(const FunctionAccessedStorage &calleeAccess, |
| 114 | + FullApplySite fullApply); |
| 115 | + |
| 116 | + /// Analyze the side-effects of a single SIL instruction \p I. |
| 117 | + /// Visited callees are added to \p BottomUpOrder until \p RecursionDepth |
| 118 | + /// reaches MaxRecursionDepth. |
| 119 | + void analyzeInstruction(SILInstruction *I); |
| 120 | + |
| 121 | + /// Does any of the accesses represented by this FunctionAccessedStorage |
| 122 | + /// object conflict with the given access kind and storage. |
| 123 | + bool mayConflictWith(SILAccessKind otherAccessKind, |
| 124 | + const AccessedStorage &otherStorage); |
| 125 | + |
| 126 | + void print(raw_ostream &os) const; |
| 127 | + void dump() const; |
| 128 | + |
| 129 | +protected: |
| 130 | + bool updateUnidentifiedAccess(SILAccessKind accessKind); |
| 131 | + |
| 132 | + bool mergeAccesses( |
| 133 | + const FunctionAccessedStorage &RHS, |
| 134 | + std::function<AccessedStorage(const AccessedStorage &)> transformStorage); |
| 135 | + |
| 136 | + template <typename B> void visitBeginAccess(B *beginAccess); |
| 137 | +}; |
| 138 | + |
| 139 | +/// Summarizes the dynamic accesses performed within a function and its |
| 140 | +/// callees. |
| 141 | +/// |
| 142 | +/// When summarizing multiple accesses, a Read access indicates that all |
| 143 | +/// accesses are read only. A Write access indicates potential reads and writes. |
| 144 | +/// |
| 145 | +/// Unidentified accesses are not recorded individually. Rather, the function is |
| 146 | +/// marked as potentially executing unidentified reads or writes. An incomplete |
| 147 | +/// function, without a known callee set, is considered to have unidentified |
| 148 | +/// writes. |
| 149 | +class AccessedStorageAnalysis |
| 150 | + : public GenericFunctionEffectAnalysis<FunctionAccessedStorage> { |
| 151 | +public: |
| 152 | + AccessedStorageAnalysis() |
| 153 | + : GenericFunctionEffectAnalysis<FunctionAccessedStorage>( |
| 154 | + AnalysisKind::AccessedStorage) {} |
| 155 | + |
| 156 | + static bool classof(const SILAnalysis *S) { |
| 157 | + return S->getKind() == AnalysisKind::AccessedStorage; |
| 158 | + } |
| 159 | +}; |
| 160 | + |
| 161 | +} // end namespace swift |
| 162 | + |
| 163 | +#endif // SWIFT_SILOPTIMIZER_ANALYSIS_ACCESSED_STORAGE_ANALYSIS_H_ |
0 commit comments