forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMemoryBehavior.cpp
316 lines (268 loc) · 12.6 KB
/
MemoryBehavior.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
//===--- MemoryBehavior.cpp -----------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
#define DEBUG_TYPE "sil-membehavior"
#include "swift/SILAnalysis/AliasAnalysis.h"
#include "swift/SILAnalysis/SideEffectAnalysis.h"
#include "swift/SILAnalysis/ValueTracking.h"
#include "swift/SIL/SILVisitor.h"
#include "llvm/Support/Debug.h"
using namespace swift;
//===----------------------------------------------------------------------===//
// Memory Behavior Implementation
//===----------------------------------------------------------------------===//
namespace {
using MemBehavior = SILInstruction::MemoryBehavior;
/// Visitor that determines the memory behavior of an instruction relative to a
/// specific SILValue (i.e. can the instruction cause the value to be read,
/// etc.).
class MemoryBehaviorVisitor
: public SILInstructionVisitor<MemoryBehaviorVisitor, MemBehavior> {
AliasAnalysis *AA;
SideEffectAnalysis *SEA;
/// The value we are attempting to discover memory behavior relative to.
SILValue V;
/// The SILType of the value.
Optional<SILType> TypedAccessTy;
/// Should we treat instructions that increment ref counts as None instead of
/// MayHaveSideEffects.
RetainObserveKind InspectionMode;
public:
MemoryBehaviorVisitor(AliasAnalysis *AA, SideEffectAnalysis *SEA, SILValue V,
RetainObserveKind IgnoreRefCountIncs)
: AA(AA), SEA(SEA), V(V), InspectionMode(IgnoreRefCountIncs) {}
SILType getValueTBAAType() {
if (!TypedAccessTy)
TypedAccessTy = computeTBAAType(V);
return *TypedAccessTy;
}
MemBehavior visitValueBase(ValueBase *V) {
llvm_unreachable("unimplemented");
}
MemBehavior visitSILInstruction(SILInstruction *Inst) {
// If we do not have any more information, just use the general memory
// behavior implementation.
auto Behavior = Inst->getMemoryBehavior();
bool ReadOnlyAccess = isLetPointer(V);
// If this is a regular read-write access then return the computed memory
// behavior.
if (!ReadOnlyAccess)
return Behavior;
// If this is a read-only access to 'let variable' then we can strip away
// the write access.
switch (Behavior) {
case MemBehavior::MayHaveSideEffects: return MemBehavior::MayRead;
case MemBehavior::MayReadWrite: return MemBehavior::MayRead;
case MemBehavior::MayWrite: return MemBehavior::None;
default: return Behavior;
}
}
MemBehavior visitLoadInst(LoadInst *LI);
MemBehavior visitStoreInst(StoreInst *SI);
MemBehavior visitApplyInst(ApplyInst *AI);
MemBehavior visitTryApplyInst(TryApplyInst *AI);
MemBehavior visitBuiltinInst(BuiltinInst *BI);
MemBehavior visitStrongReleaseInst(StrongReleaseInst *BI);
MemBehavior visitUnownedReleaseInst(UnownedReleaseInst *BI);
MemBehavior visitReleaseValueInst(ReleaseValueInst *BI);
// Instructions which are none if our SILValue does not alias one of its
// arguments. If we can not prove such a thing, return the relevant memory
// behavior.
#define OPERANDALIAS_MEMBEHAVIOR_INST(Name) \
MemBehavior visit##Name(Name *I) { \
for (Operand &Op : I->getAllOperands()) { \
if (!AA->isNoAlias(Op.get(), V)) { \
DEBUG(llvm::dbgs() << " " #Name \
" does alias inst. Returning Normal behavior.\n"); \
return I->getMemoryBehavior(); \
} \
} \
\
DEBUG(llvm::dbgs() << " " #Name " does not alias inst. Returning " \
"None.\n"); \
return MemBehavior::None; \
}
OPERANDALIAS_MEMBEHAVIOR_INST(InjectEnumAddrInst)
OPERANDALIAS_MEMBEHAVIOR_INST(UncheckedTakeEnumDataAddrInst)
OPERANDALIAS_MEMBEHAVIOR_INST(InitExistentialAddrInst)
OPERANDALIAS_MEMBEHAVIOR_INST(DeinitExistentialAddrInst)
OPERANDALIAS_MEMBEHAVIOR_INST(DeallocStackInst)
OPERANDALIAS_MEMBEHAVIOR_INST(FixLifetimeInst)
#undef OPERANDALIAS_MEMBEHAVIOR_INST
// Override simple behaviors where MayHaveSideEffects is too general and
// encompasses other behavior that is not read/write/ref count decrement
// behavior we care about.
#define SIMPLE_MEMBEHAVIOR_INST(Name, Behavior) \
MemBehavior visit##Name(Name *I) { return MemBehavior::Behavior; }
SIMPLE_MEMBEHAVIOR_INST(CondFailInst, None)
#undef SIMPLE_MEMBEHAVIOR_INST
// If we are asked to treat ref count increments as being inert, return None
// for these.
//
// FIXME: Once we separate the notion of ref counts from reading/writing
// memory this will be unnecessary.
#define REFCOUNTINC_MEMBEHAVIOR_INST(Name) \
MemBehavior visit##Name(Name *I) { \
if (InspectionMode == RetainObserveKind::IgnoreRetains) \
return MemBehavior::None; \
return I->getMemoryBehavior(); \
}
REFCOUNTINC_MEMBEHAVIOR_INST(StrongRetainInst)
REFCOUNTINC_MEMBEHAVIOR_INST(StrongRetainAutoreleasedInst)
REFCOUNTINC_MEMBEHAVIOR_INST(StrongRetainUnownedInst)
REFCOUNTINC_MEMBEHAVIOR_INST(UnownedRetainInst)
REFCOUNTINC_MEMBEHAVIOR_INST(RetainValueInst)
#undef REFCOUNTINC_MEMBEHAVIOR_INST
};
} // end anonymous namespace
MemBehavior MemoryBehaviorVisitor::visitLoadInst(LoadInst *LI) {
if (AA->isNoAlias(LI->getOperand(), V, computeTBAAType(LI->getOperand()),
getValueTBAAType())) {
DEBUG(llvm::dbgs() << " Load Operand does not alias inst. Returning "
"None.\n");
return MemBehavior::None;
}
DEBUG(llvm::dbgs() << " Could not prove that load inst does not alias "
"pointer. Returning may read.");
return MemBehavior::MayRead;
}
MemBehavior MemoryBehaviorVisitor::visitStoreInst(StoreInst *SI) {
// No store besides the initialization of a "let"-variable
// can have any effect on the value of this "let" variable.
if (isLetPointer(V) && SI->getDest() != V)
return MemBehavior::None;
// If the store dest cannot alias the pointer in question, then the
// specified value can not be modified by the store.
if (AA->isNoAlias(SI->getDest(), V, computeTBAAType(SI->getDest()),
getValueTBAAType())) {
DEBUG(llvm::dbgs() << " Store Dst does not alias inst. Returning "
"None.\n");
return MemBehavior::None;
}
// Otherwise, a store just writes.
DEBUG(llvm::dbgs() << " Could not prove store does not alias inst. "
"Returning MayWrite.\n");
return MemBehavior::MayWrite;
}
MemBehavior MemoryBehaviorVisitor::visitBuiltinInst(BuiltinInst *BI) {
// If our callee is not a builtin, be conservative and return may have side
// effects.
if (!BI) {
return MemBehavior::MayHaveSideEffects;
}
// If the builtin is read none, it does not read or write memory.
if (!BI->mayReadOrWriteMemory()) {
DEBUG(llvm::dbgs() << " Found apply of read none builtin. Returning"
" None.\n");
return MemBehavior::None;
}
// If the builtin is side effect free, then it can only read memory.
if (!BI->mayHaveSideEffects()) {
DEBUG(llvm::dbgs() << " Found apply of side effect free builtin. "
"Returning MayRead.\n");
return MemBehavior::MayRead;
}
// FIXME: If the value (or any other values from the instruction that the
// value comes from) that we are tracking does not escape and we don't alias
// any of the arguments of the apply inst, we should be ok.
// Otherwise be conservative and return that we may have side effects.
DEBUG(llvm::dbgs() << " Found apply of side effect builtin. "
"Returning MayHaveSideEffects.\n");
return MemBehavior::MayHaveSideEffects;
}
MemBehavior MemoryBehaviorVisitor::visitTryApplyInst(TryApplyInst *AI) {
MemBehavior Behavior = MemBehavior::MayHaveSideEffects;
// If it is an allocstack which does not escape, tryapply instruction can not
// read/modify the memory.
if (auto *ASI = dyn_cast<AllocStackInst>(getUnderlyingObject(V)))
if (isNonEscapingLocalObject(ASI->getAddressResult()))
Behavior = MemBehavior::None;
// Otherwise be conservative and return that we may have side effects.
DEBUG(llvm::dbgs() << " Found tryapply, returning " << Behavior << '\n');
return Behavior;
}
MemBehavior MemoryBehaviorVisitor::visitApplyInst(ApplyInst *AI) {
SideEffectAnalysis::FunctionEffects ApplyEffects;
SEA->getEffects(ApplyEffects, AI);
MemBehavior Behavior = MemBehavior::None;
// We can ignore mayTrap().
if (ApplyEffects.mayReadRC() ||
(InspectionMode == RetainObserveKind::ObserveRetains &&
ApplyEffects.mayAllocObjects())) {
Behavior = MemBehavior::MayHaveSideEffects;
} else {
auto &GlobalEffects = ApplyEffects.getGlobalEffects();
Behavior = GlobalEffects.getMemBehavior(InspectionMode);
// Check all parameter effects.
for (unsigned Idx = 0, End = AI->getNumArguments();
Idx < End && Behavior < MemBehavior::MayHaveSideEffects; ++Idx) {
auto &ArgEffect = ApplyEffects.getParameterEffects()[Idx];
auto ArgBehavior = ArgEffect.getMemBehavior(InspectionMode);
if (ArgBehavior > Behavior) {
SILValue Arg = AI->getArgument(Idx);
// We only consider the argument effects if the argument aliases V.
if (!Arg.getType().isAddress() ||
!AA->isNoAlias(Arg, V, computeTBAAType(Arg), getValueTBAAType())) {
Behavior = ArgBehavior;
}
}
}
}
if (Behavior > MemBehavior::MayRead && isLetPointer(V))
Behavior = MemBehavior::MayRead;
// If it is an allocstack which does not escape, apply instruction can not
// read/modify the memory.
if (auto *ASI = dyn_cast<AllocStackInst>(getUnderlyingObject(V)))
if (isNonEscapingLocalObject(ASI->getAddressResult())) {
Behavior = MemBehavior::None;
}
DEBUG(llvm::dbgs() << " Found apply, returning " << Behavior << '\n');
return Behavior;
}
MemBehavior
MemoryBehaviorVisitor::visitStrongReleaseInst(StrongReleaseInst *SI) {
// Need to make sure that the allocated memory does not escape.
// AllocBox to stack does not check for whether the address of promoted
// allocstack can escape.
//
// TODO: come up with a test case which shows isNonEscapingLocalObject is
// necessary.
if (AllocStackInst *ASI = dyn_cast<AllocStackInst>(getUnderlyingObject(V)))
if (isNonEscapingLocalObject(ASI->getAddressResult()))
return MemBehavior::None;
return MemBehavior::MayHaveSideEffects;
}
MemBehavior
MemoryBehaviorVisitor::visitUnownedReleaseInst(UnownedReleaseInst *SI) {
// Need to make sure that the allocated memory does not escape.
if (AllocStackInst *ASI = dyn_cast<AllocStackInst>(getUnderlyingObject(V)))
if (isNonEscapingLocalObject(ASI->getAddressResult()))
return MemBehavior::None;
return MemBehavior::MayHaveSideEffects;
}
MemBehavior MemoryBehaviorVisitor::visitReleaseValueInst(ReleaseValueInst *SI) {
// Need to make sure that the allocated memory does not escape.
if (AllocStackInst *ASI = dyn_cast<AllocStackInst>(getUnderlyingObject(V)))
if (isNonEscapingLocalObject(ASI->getAddressResult()))
return MemBehavior::None;
return MemBehavior::MayHaveSideEffects;
}
//===----------------------------------------------------------------------===//
// Top Level Entrypoint
//===----------------------------------------------------------------------===//
MemBehavior
AliasAnalysis::computeMemoryBehavior(SILInstruction *Inst, SILValue V,
RetainObserveKind InspectionMode) {
DEBUG(llvm::dbgs() << "GET MEMORY BEHAVIOR FOR:\n " << *Inst << " "
<< *V.getDef());
assert(SEA && "SideEffectsAnalysis must be initialized!");
return MemoryBehaviorVisitor(this, SEA, V, InspectionMode).visit(Inst);
}