|
| 1 | +//===--- AccessSummaryAnalysis.h - SIL Access Summary Analysis --*- C++ -*-===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2017 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 |
| 14 | +// the formal accesses that a function makes to its address-type arguments. |
| 15 | +// These summaries are used to statically diagnose violations of exclusive |
| 16 | +// accesses for noescape closures. |
| 17 | +// |
| 18 | +//===----------------------------------------------------------------------===// |
| 19 | +#ifndef SWIFT_SILOPTIMIZER_ANALYSIS_ACCESS_SUMMARY_ANALYSIS_H_ |
| 20 | +#define SWIFT_SILOPTIMIZER_ANALYSIS_ACCESS_SUMMARY_ANALYSIS_H_ |
| 21 | + |
| 22 | +#include "swift/SIL/SILFunction.h" |
| 23 | +#include "swift/SIL/SILInstruction.h" |
| 24 | +#include "swift/SILOptimizer/Analysis/BottomUpIPAnalysis.h" |
| 25 | +#include "swift/SILOptimizer/Utils/IndexTrie.h" |
| 26 | +#include "llvm/ADT/DenseMap.h" |
| 27 | +#include "llvm/ADT/SmallVector.h" |
| 28 | + |
| 29 | +namespace swift { |
| 30 | + |
| 31 | +class AccessSummaryAnalysis : public BottomUpIPAnalysis { |
| 32 | +public: |
| 33 | + /// Summarizes the accesses that a function begins on an argument. |
| 34 | + class ArgumentSummary { |
| 35 | + private: |
| 36 | + /// The kind of access begun on the argument. |
| 37 | + /// 'None' means no access performed. |
| 38 | + Optional<SILAccessKind> Kind = None; |
| 39 | + |
| 40 | + /// The location of the access. Used for diagnostics. |
| 41 | + SILLocation AccessLoc = SILLocation((Expr *)nullptr); |
| 42 | + |
| 43 | + public: |
| 44 | + Optional<SILAccessKind> getAccessKind() const { return Kind; } |
| 45 | + |
| 46 | + SILLocation getAccessLoc() const { return AccessLoc; } |
| 47 | + |
| 48 | + /// The lattice operation on argument summaries. |
| 49 | + bool mergeWith(const ArgumentSummary &other); |
| 50 | + |
| 51 | + /// Merge in an access to the argument of the given kind at the given |
| 52 | + /// location. Returns true if the merge caused the summary to change. |
| 53 | + bool mergeWith(SILAccessKind otherKind, SILLocation otherLoc); |
| 54 | + |
| 55 | + /// Returns a description of the summary. For debugging and testing |
| 56 | + /// purposes. |
| 57 | + StringRef getDescription() const; |
| 58 | + }; |
| 59 | + |
| 60 | + /// Summarizes the accesses that a function begins on its arguments. |
| 61 | + class FunctionSummary { |
| 62 | + private: |
| 63 | + llvm::SmallVector<ArgumentSummary, 6> ArgAccesses; |
| 64 | + |
| 65 | + public: |
| 66 | + FunctionSummary(unsigned argCount) : ArgAccesses(argCount) {} |
| 67 | + |
| 68 | + /// Returns of summary of the the function accesses that argument at the |
| 69 | + /// given index. |
| 70 | + ArgumentSummary &getAccessForArgument(unsigned argument) { |
| 71 | + return ArgAccesses[argument]; |
| 72 | + } |
| 73 | + |
| 74 | + const ArgumentSummary &getAccessForArgument(unsigned argument) const { |
| 75 | + return ArgAccesses[argument]; |
| 76 | + } |
| 77 | + |
| 78 | + /// Returns the number of argument in the summary. |
| 79 | + unsigned getArgumentCount() const { return ArgAccesses.size(); } |
| 80 | + }; |
| 81 | + |
| 82 | + friend raw_ostream &operator<<(raw_ostream &os, |
| 83 | + const FunctionSummary &summary); |
| 84 | + |
| 85 | + class FunctionInfo; |
| 86 | + /// Records a flow of a caller's argument to a called function. |
| 87 | + /// This flow is used to iterate the interprocedural analysis to a fixpoint. |
| 88 | + struct ArgumentFlow { |
| 89 | + /// The index of the argument in the caller. |
| 90 | + const unsigned CallerArgumentIndex; |
| 91 | + |
| 92 | + /// The index of the argument in the callee. |
| 93 | + const unsigned CalleeArgumentIndex; |
| 94 | + |
| 95 | + FunctionInfo *const CalleeFunctionInfo; |
| 96 | + }; |
| 97 | + |
| 98 | + /// Records the summary and argument flows for a given function. |
| 99 | + /// Used by the BottomUpIPAnalysis to propagate information |
| 100 | + /// from callees to callers. |
| 101 | + class FunctionInfo : public FunctionInfoBase<FunctionInfo> { |
| 102 | + private: |
| 103 | + FunctionSummary FS; |
| 104 | + |
| 105 | + SILFunction *F; |
| 106 | + |
| 107 | + llvm::SmallVector<ArgumentFlow, 8> RecordedArgumentFlows; |
| 108 | + |
| 109 | + public: |
| 110 | + FunctionInfo(SILFunction *F) : FS(F->getArguments().size()), F(F) {} |
| 111 | + |
| 112 | + SILFunction *getFunction() const { return F; } |
| 113 | + |
| 114 | + ArrayRef<ArgumentFlow> getArgumentFlows() const { |
| 115 | + return RecordedArgumentFlows; |
| 116 | + } |
| 117 | + |
| 118 | + const FunctionSummary &getSummary() const { return FS; } |
| 119 | + FunctionSummary &getSummary() { return FS; } |
| 120 | + |
| 121 | + /// Record a flow of an argument in this function to a callee. |
| 122 | + void recordFlow(const ArgumentFlow &flow) { |
| 123 | + flow.CalleeFunctionInfo->addCaller(this, nullptr); |
| 124 | + RecordedArgumentFlows.push_back(flow); |
| 125 | + } |
| 126 | + }; |
| 127 | + |
| 128 | +private: |
| 129 | + /// Maps functions to the information the analysis keeps for each function. |
| 130 | + llvm::DenseMap<SILFunction *, FunctionInfo *> FunctionInfos; |
| 131 | + |
| 132 | + llvm::SpecificBumpPtrAllocator<FunctionInfo> Allocator; |
| 133 | + |
| 134 | + /// A trie of integer indices that gives pointer identity to a path of |
| 135 | + /// projections. This is shared between all functions in the module. |
| 136 | + IndexTrieNode *SubPathTrie; |
| 137 | + |
| 138 | +public: |
| 139 | + AccessSummaryAnalysis() : BottomUpIPAnalysis(AnalysisKind::AccessSummary) { |
| 140 | + SubPathTrie = new IndexTrieNode(); |
| 141 | + } |
| 142 | + |
| 143 | + ~AccessSummaryAnalysis() { |
| 144 | + delete SubPathTrie; |
| 145 | + } |
| 146 | + |
| 147 | + /// Returns a summary of the accesses performed by the given function. |
| 148 | + const FunctionSummary &getOrCreateSummary(SILFunction *Fn); |
| 149 | + |
| 150 | + IndexTrieNode *getSubPathTrieRoot() { |
| 151 | + return SubPathTrie; |
| 152 | + } |
| 153 | + |
| 154 | + virtual void initialize(SILPassManager *PM) override {} |
| 155 | + virtual void invalidate() override; |
| 156 | + virtual void invalidate(SILFunction *F, InvalidationKind K) override; |
| 157 | + virtual void notifyAddFunction(SILFunction *F) override {} |
| 158 | + virtual void notifyDeleteFunction(SILFunction *F) override { |
| 159 | + invalidate(F, InvalidationKind::Nothing); |
| 160 | + } |
| 161 | + virtual void invalidateFunctionTables() override {} |
| 162 | + |
| 163 | + static bool classof(const SILAnalysis *S) { |
| 164 | + return S->getKind() == AnalysisKind::AccessSummary; |
| 165 | + } |
| 166 | + |
| 167 | +private: |
| 168 | + typedef BottomUpFunctionOrder<FunctionInfo> FunctionOrder; |
| 169 | + |
| 170 | + /// Returns the BottomUpIPAnalysis information for the given function. |
| 171 | + FunctionInfo *getFunctionInfo(SILFunction *F); |
| 172 | + |
| 173 | + /// Summarizes the given function and iterates the interprocedural analysis |
| 174 | + /// to a fixpoint. |
| 175 | + void recompute(FunctionInfo *initial); |
| 176 | + |
| 177 | + /// Propagate the access summary from the argument of a called function |
| 178 | + /// to the caller. |
| 179 | + bool propagateFromCalleeToCaller(FunctionInfo *callerInfo, |
| 180 | + ArgumentFlow site); |
| 181 | + |
| 182 | + /// Summarize the given function and schedule it for interprocedural |
| 183 | + /// analysis. |
| 184 | + void processFunction(FunctionInfo *info, FunctionOrder &order); |
| 185 | + |
| 186 | + /// Summarize how the function uses the given argument. |
| 187 | + void processArgument(FunctionInfo *info, SILFunctionArgument *argment, |
| 188 | + ArgumentSummary &summary, FunctionOrder &order); |
| 189 | + |
| 190 | + /// Summarize a partial_apply instruction. |
| 191 | + void processPartialApply(FunctionInfo *callerInfo, |
| 192 | + unsigned callerArgumentIndex, |
| 193 | + PartialApplyInst *apply, |
| 194 | + Operand *applyArgumentOperand, FunctionOrder &order); |
| 195 | + |
| 196 | + /// Summarize apply or try_apply |
| 197 | + void processFullApply(FunctionInfo *callerInfo, unsigned callerArgumentIndex, |
| 198 | + FullApplySite apply, Operand *argumentOperand, |
| 199 | + FunctionOrder &order); |
| 200 | + |
| 201 | + /// Summarize a call site and schedule it for interprocedural analysis. |
| 202 | + void processCall(FunctionInfo *callerInfo, unsigned callerArgumentIndex, |
| 203 | + SILFunction *calledFunction, unsigned argumentIndex, |
| 204 | + FunctionOrder &order); |
| 205 | +}; |
| 206 | + |
| 207 | +} // end namespace swift |
| 208 | + |
| 209 | +#endif |
| 210 | + |
0 commit comments