-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathRCStateTransitionVisitors.cpp
391 lines (319 loc) · 14.4 KB
/
RCStateTransitionVisitors.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
//===--- RCStateTransitionVisitors.cpp ------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 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
//
//===----------------------------------------------------------------------===//
#define DEBUG_TYPE "arc-sequence-opts"
#include "RCStateTransitionVisitors.h"
#include "ARCBBState.h"
#include "swift/SILOptimizer/Analysis/ARCAnalysis.h"
#include "swift/SILOptimizer/Analysis/RCIdentityAnalysis.h"
#include "llvm/Support/Debug.h"
using namespace swift;
namespace {
using ARCBBState = ARCSequenceDataflowEvaluator::ARCBBState;
} // end anonymous namespace
//===----------------------------------------------------------------------===//
// Utilities
//===----------------------------------------------------------------------===//
/// Return true if this instruction is the epilogue release for the \p Arg.
/// false otherwise.
static bool isOwnedArgumentEpilogueRelease(SILInstruction *I, SILValue Arg,
EpilogueARCFunctionInfo *EAFI) {
auto Releases =
EAFI->computeEpilogueARCInstructions(
EpilogueARCContext::EpilogueARCKind::Release, Arg);
return Releases.size() && Releases.count(I);
}
static bool isGuaranteedSafetyByEpilogueRelease(SILInstruction *I, SILValue Arg,
EpilogueARCFunctionInfo *EAFI) {
auto Releases =
EAFI->computeEpilogueARCInstructions(
EpilogueARCContext::EpilogueARCKind::Release, Arg);
return Releases.size() && !Releases.count(I);
}
//===----------------------------------------------------------------------===//
// BottomUpRCStateTransitionVisitor
//===----------------------------------------------------------------------===//
template <class ARCState>
BottomUpDataflowRCStateVisitor<ARCState>::BottomUpDataflowRCStateVisitor(
RCIdentityFunctionInfo *RCFI, EpilogueARCFunctionInfo *EAFI,
ARCState &State, bool FreezeOwnedArgEpilogueReleases,
IncToDecStateMapTy &IncToDecStateMap,
ImmutablePointerSetFactory<SILInstruction> &SetFactory)
: RCFI(RCFI), EAFI(EAFI), DataflowState(State),
FreezeOwnedArgEpilogueReleases(FreezeOwnedArgEpilogueReleases),
IncToDecStateMap(IncToDecStateMap), SetFactory(SetFactory) {}
template <class ARCState>
typename BottomUpDataflowRCStateVisitor<ARCState>::DataflowResult
BottomUpDataflowRCStateVisitor<ARCState>::
visitAutoreleasePoolCall(SILNode *N) {
DataflowState.clear();
// We just cleared our BB State so we have no more possible effects.
return DataflowResult(RCStateTransitionDataflowResultKind::NoEffects);
}
// private helper method since C++ does not have extensions... *sigh*.
//
// TODO: This needs a better name.
template <class ARCState>
static bool isKnownSafe(BottomUpDataflowRCStateVisitor<ARCState> *State,
SILInstruction *I, SILValue Op) {
// If we are running with 'frozen' owned arg releases, check if we have a
// frozen use in the side table. If so, this release must be known safe.
if (State->FreezeOwnedArgEpilogueReleases)
if (isGuaranteedSafetyByEpilogueRelease(I, Op, State->EAFI))
return true;
// A guaranteed function argument is guaranteed to outlive the function we are
// processing. So bottom up for such a parameter, we are always known safe.
if (auto *Arg = dyn_cast<SILFunctionArgument>(Op)) {
if (Arg->hasConvention(SILArgumentConvention::Direct_Guaranteed)) {
return true;
}
}
// If Op is a load from an in_guaranteed parameter, it is guaranteed as well.
if (auto *LI = dyn_cast<LoadInst>(Op)) {
SILValue RCIdentity = State->RCFI->getRCIdentityRoot(LI->getOperand());
if (auto *Arg = dyn_cast<SILFunctionArgument>(RCIdentity)) {
if (Arg->hasConvention(SILArgumentConvention::Indirect_In_Guaranteed)) {
return true;
}
}
}
return false;
}
template <class ARCState>
typename BottomUpDataflowRCStateVisitor<ARCState>::DataflowResult
BottomUpDataflowRCStateVisitor<ARCState>::visitStrongDecrement(SILNode *N) {
auto *I = dyn_cast<SILInstruction>(N);
if (!I)
return DataflowResult();
SILValue Op = RCFI->getRCIdentityRoot(I->getOperand(0));
// If this instruction is a post dominating release, skip it so we don't pair
// it up with anything. Do make sure that it does not effect any other
// instructions.
if (FreezeOwnedArgEpilogueReleases && isOwnedArgumentEpilogueRelease(I, Op, EAFI))
return DataflowResult(Op);
BottomUpRefCountState &State = DataflowState.getBottomUpRefCountState(Op);
bool NestingDetected = State.initWithMutatorInst(SetFactory.get(I), RCFI);
if (isKnownSafe(this, I, Op)) {
State.updateKnownSafe(true);
}
LLVM_DEBUG(llvm::dbgs() << " REF COUNT DECREMENT! Known Safe: "
<< (State.isKnownSafe() ? "yes" : "no") << "\n");
// Continue on to see if our reference decrement could potentially affect
// any other pointers via a use or a decrement.
return DataflowResult(Op, NestingDetected);
}
template <class ARCState>
typename BottomUpDataflowRCStateVisitor<ARCState>::DataflowResult
BottomUpDataflowRCStateVisitor<ARCState>::visitStrongIncrement(SILNode *N) {
auto *I = dyn_cast<SILInstruction>(N);
if (!I)
return DataflowResult();
// Look up the state associated with its operand...
SILValue Op = RCFI->getRCIdentityRoot(I->getOperand(0));
auto &RefCountState = DataflowState.getBottomUpRefCountState(Op);
LLVM_DEBUG(llvm::dbgs() << " REF COUNT INCREMENT!\n");
// If we find a state initialized with a matching increment, pair this
// decrement with a copy of the ref count state and then clear the ref
// count state in preparation for any future pairs we may see on the same
// pointer.
if (RefCountState.isRefCountInstMatchedToTrackedInstruction(I)) {
// Copy the current value of ref count state into the result map.
IncToDecStateMap[I] = RefCountState;
LLVM_DEBUG(llvm::dbgs() << " MATCHING DECREMENT:"
<< RefCountState.getRCRoot());
// Clear the ref count state so it can be used for future pairs we may
// see.
RefCountState.clear();
}
#ifndef NDEBUG
else {
if (RefCountState.isTrackingRefCountInst()) {
LLVM_DEBUG(llvm::dbgs() << " FAILED MATCH DECREMENT:"
<< RefCountState.getRCRoot());
} else {
LLVM_DEBUG(llvm::dbgs() << " FAILED MATCH DECREMENT. Not tracking a "
"decrement.\n");
}
}
#endif
return DataflowResult(Op);
}
//===----------------------------------------------------------------------===//
// TopDownDataflowRCStateVisitor
//===----------------------------------------------------------------------===//
template <class ARCState>
TopDownDataflowRCStateVisitor<ARCState>::TopDownDataflowRCStateVisitor(
RCIdentityFunctionInfo *RCFI, ARCState &DataflowState,
DecToIncStateMapTy &DecToIncStateMap,
ImmutablePointerSetFactory<SILInstruction> &SetFactory)
: RCFI(RCFI), DataflowState(DataflowState),
DecToIncStateMap(DecToIncStateMap), SetFactory(SetFactory) {}
template <class ARCState>
typename TopDownDataflowRCStateVisitor<ARCState>::DataflowResult
TopDownDataflowRCStateVisitor<ARCState>::
visitAutoreleasePoolCall(SILNode *N) {
DataflowState.clear();
// We just cleared our BB State so we have no more possible effects.
return DataflowResult(RCStateTransitionDataflowResultKind::NoEffects);
}
template <class ARCState>
typename TopDownDataflowRCStateVisitor<ARCState>::DataflowResult
TopDownDataflowRCStateVisitor<ARCState>::visitStrongDecrement(SILNode *N) {
auto *I = dyn_cast<SILInstruction>(N);
if (!I)
return DataflowResult();
// Look up the state associated with I's operand...
SILValue Op = RCFI->getRCIdentityRoot(I->getOperand(0));
auto &RefCountState = DataflowState.getTopDownRefCountState(Op);
LLVM_DEBUG(llvm::dbgs() << " REF COUNT DECREMENT!\n");
// If we are tracking an increment on the ref count root associated with
// the decrement and the decrement matches, pair this decrement with a
// copy of the increment state and then clear the original increment state
// so that we are ready to process further values.
if (RefCountState.isRefCountInstMatchedToTrackedInstruction(I)) {
// Copy the current value of ref count state into the result map.
DecToIncStateMap[I] = RefCountState;
LLVM_DEBUG(llvm::dbgs() << " MATCHING INCREMENT:\n"
<< RefCountState.getRCRoot());
// Clear the ref count state in preparation for more pairs.
RefCountState.clear();
}
#if NDEBUG
else {
if (RefCountState.isTrackingRefCountInst()) {
LLVM_DEBUG(llvm::dbgs() << " FAILED MATCH INCREMENT:\n"
<< RefCountState.getValue());
} else {
LLVM_DEBUG(llvm::dbgs() << " FAILED MATCH. NO INCREMENT.\n");
}
}
#endif
// Otherwise we continue processing the reference count decrement to see if
// the decrement can affect any other pointers that we are tracking.
return DataflowResult(Op);
}
template <class ARCState>
typename TopDownDataflowRCStateVisitor<ARCState>::DataflowResult
TopDownDataflowRCStateVisitor<ARCState>::visitStrongIncrement(SILNode *N) {
auto *I = dyn_cast<SILInstruction>(N);
if (!I)
return DataflowResult();
// Map the increment's operand to a newly initialized or reinitialized ref
// count state and continue...
SILValue Op = RCFI->getRCIdentityRoot(I->getOperand(0));
auto &State = DataflowState.getTopDownRefCountState(Op);
bool NestingDetected = State.initWithMutatorInst(SetFactory.get(I), RCFI);
LLVM_DEBUG(llvm::dbgs() << " REF COUNT INCREMENT! Known Safe: "
<< (State.isKnownSafe() ? "yes" : "no") << "\n");
// Continue processing in case this increment could be a CanUse for a
// different pointer.
return DataflowResult(Op, NestingDetected);
}
template <class ARCState>
typename TopDownDataflowRCStateVisitor<ARCState>::DataflowResult
TopDownDataflowRCStateVisitor<ARCState>::visitStrongEntranceArgument(
SILFunctionArgument *Arg) {
LLVM_DEBUG(llvm::dbgs() << "VISITING ENTRANCE ARGUMENT: " << *Arg);
if (!Arg->hasConvention(SILArgumentConvention::Direct_Owned)) {
LLVM_DEBUG(llvm::dbgs() << " Not owned! Bailing!\n");
return DataflowResult();
}
LLVM_DEBUG(llvm::dbgs() << " Initializing state.\n");
auto &State = DataflowState.getTopDownRefCountState(Arg);
State.initWithArg(Arg);
return DataflowResult();
}
template <class ARCState>
typename TopDownDataflowRCStateVisitor<ARCState>::DataflowResult
TopDownDataflowRCStateVisitor<ARCState>::
visitStrongEntranceApply(ApplyInst *AI) {
LLVM_DEBUG(llvm::dbgs() << "VISITING ENTRANCE APPLY: " << *AI);
// We should have checked earlier that AI has an owned result value. To
// prevent mistakes, assert that here.
#ifndef NDEBUG
bool hasOwnedResult = false;
for (auto result : AI->getSubstCalleeConv().getDirectSILResults()) {
if (result.getConvention() == ResultConvention::Owned)
hasOwnedResult = true;
}
assert(hasOwnedResult && "Expected AI to be Owned here");
#endif
// Otherwise, return a dataflow result containing a +1.
LLVM_DEBUG(llvm::dbgs() << " Initializing state.\n");
auto &State = DataflowState.getTopDownRefCountState(AI);
State.initWithEntranceInst(SetFactory.get(AI), AI);
return DataflowResult(AI);
}
template <class ARCState>
typename TopDownDataflowRCStateVisitor<ARCState>::DataflowResult
TopDownDataflowRCStateVisitor<ARCState>::visitStrongEntrancePartialApply(
PartialApplyInst *PAI) {
LLVM_DEBUG(llvm::dbgs() << "VISITING ENTRANCE PARTIAL APPLY: " << *PAI);
// Rreturn a dataflow result containing a +1.
LLVM_DEBUG(llvm::dbgs() << " Initializing state.\n");
auto &State = DataflowState.getTopDownRefCountState(PAI);
State.initWithEntranceInst(SetFactory.get(PAI), PAI);
return DataflowResult(PAI);
}
template <class ARCState>
typename TopDownDataflowRCStateVisitor<ARCState>::DataflowResult
TopDownDataflowRCStateVisitor<ARCState>::
visitStrongEntranceAllocRef(AllocRefInst *ARI) {
// Alloc refs always introduce new references at +1.
TopDownRefCountState &State = DataflowState.getTopDownRefCountState(ARI);
State.initWithEntranceInst(SetFactory.get(ARI), ARI);
return DataflowResult(ARI);
}
template <class ARCState>
typename TopDownDataflowRCStateVisitor<ARCState>::DataflowResult
TopDownDataflowRCStateVisitor<ARCState>::
visitStrongEntranceAllocRefDynamic(AllocRefDynamicInst *ARI) {
// Alloc ref dynamic always introduce references at +1.
auto &State = DataflowState.getTopDownRefCountState(ARI);
State.initWithEntranceInst(SetFactory.get(ARI), ARI);
return DataflowResult(ARI);
}
template <class ARCState>
typename TopDownDataflowRCStateVisitor<ARCState>::DataflowResult
TopDownDataflowRCStateVisitor<ARCState>::
visitStrongAllocBox(AllocBoxInst *ABI) {
// Alloc box introduces a ref count of +1 on its container.
auto &State = DataflowState.getTopDownRefCountState(ABI);
State.initWithEntranceInst(SetFactory.get(ABI), ABI);
return DataflowResult(ABI);
}
template <class ARCState>
typename TopDownDataflowRCStateVisitor<ARCState>::DataflowResult
TopDownDataflowRCStateVisitor<ARCState>::
visitStrongEntrance(SILNode *N) {
if (auto *Arg = dyn_cast<SILFunctionArgument>(N))
return visitStrongEntranceArgument(Arg);
if (auto *AI = dyn_cast<ApplyInst>(N))
return visitStrongEntranceApply(AI);
if (auto *ARI = dyn_cast<AllocRefInst>(N))
return visitStrongEntranceAllocRef(ARI);
if (auto *ARI = dyn_cast<AllocRefDynamicInst>(N))
return visitStrongEntranceAllocRefDynamic(ARI);
if (auto *ABI = dyn_cast<AllocBoxInst>(N))
return visitStrongAllocBox(ABI);
if (auto *PAI = dyn_cast<PartialApplyInst>(N))
return visitStrongEntrancePartialApply(PAI);
return DataflowResult();
}
//===----------------------------------------------------------------------===//
// Template Instantiation
//===----------------------------------------------------------------------===//
namespace swift {
template class BottomUpDataflowRCStateVisitor<ARCBBState>;
template class BottomUpDataflowRCStateVisitor<ARCRegionState>;
template class TopDownDataflowRCStateVisitor<ARCBBState>;
template class TopDownDataflowRCStateVisitor<ARCRegionState>;
} // namespace swift