-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathGlobalLoopARCSequenceDataflow.cpp
349 lines (296 loc) · 12.6 KB
/
GlobalLoopARCSequenceDataflow.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
//===--- GlobalLoopARCSequenceDataflow.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 "GlobalLoopARCSequenceDataflow.h"
#include "ARCRegionState.h"
#include "RCStateTransitionVisitors.h"
#include "swift/SILOptimizer/Analysis/ARCAnalysis.h"
#include "swift/SILOptimizer/Analysis/PostOrderAnalysis.h"
#include "swift/SILOptimizer/Analysis/RCIdentityAnalysis.h"
#include "swift/Basic/Assertions.h"
#include "swift/SIL/SILInstruction.h"
#include "swift/SIL/SILFunction.h"
#include "swift/SIL/SILSuccessor.h"
#include "swift/SIL/CFG.h"
#include "swift/SIL/SILModule.h"
#include "llvm/ADT/PostOrderIterator.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/Support/Debug.h"
using namespace swift;
//===----------------------------------------------------------------------===//
// Utility
//===----------------------------------------------------------------------===//
/// Returns true if it is defined to perform a bottom up from \p Succ to \p
/// Pred.
///
/// This is interesting because in such cases, we must pessimistically assume
/// that we are merging in the empty set from Succ into Pred or vis-a-versa.
static bool isDefinedMerge(const LoopRegion *Succ, const LoopRegion *Pred) {
// If the predecessor region is an unknown control flow edge tail, the
// dataflow that enters into the region bottom up is undefined in our model.
if (Pred->isUnknownControlFlowEdgeTail())
return false;
// If the successor region is an unknown control flow edge head, the dataflow
// that leaves the region bottom up is considered to be undefined in our
// model.
if (Succ->isUnknownControlFlowEdgeHead())
return false;
// Otherwise it is defined to perform the merge.
return true;
}
//===----------------------------------------------------------------------===//
// Top Down Dataflow
//===----------------------------------------------------------------------===//
void LoopARCSequenceDataflowEvaluator::mergePredecessors(
const LoopRegion *Region, ARCRegionState &State) {
bool HasAtLeastOnePred = false;
for (unsigned PredID : Region->getPreds()) {
auto *PredRegion = LRFI->getRegion(PredID);
auto &PredState = getARCState(PredRegion);
LLVM_DEBUG(llvm::dbgs() << " Merging Pred: " << PredID << "\n");
// If this merge is undefined due to unknown control flow, assume that the
// empty set is flowing into this block so clear all state and exit early.
if (!isDefinedMerge(Region, PredRegion)) {
State.clear();
break;
}
if (HasAtLeastOnePred) {
State.mergePredTopDown(PredState);
continue;
}
State.initPredTopDown(PredState);
HasAtLeastOnePred = true;
}
}
bool LoopARCSequenceDataflowEvaluator::processLoopTopDown(const LoopRegion *R) {
assert(!R->isBlock() && "Expecting to process a non-block region");
LLVM_DEBUG(llvm::dbgs() << "Processing Loop#: " << R->getID() << "\n");
bool NestingDetected = false;
// For each RegionID in our reverse post order...
for (unsigned SubregionIndex : R->getSubregions()) {
auto *Subregion = LRFI->getRegion(SubregionIndex);
auto &SubregionData = getARCState(Subregion);
// This will always succeed since we have an entry for each BB in our RPOT.
LLVM_DEBUG(llvm::dbgs() << "Processing Subregion#: " << SubregionIndex
<< "\n");
// Ignore blocks that allow leaks.
if (SubregionData.allowsLeaks()) {
LLVM_DEBUG(llvm::dbgs() << "Skipping leaking BB.\n");
continue;
}
LLVM_DEBUG(llvm::dbgs() << "Merging Predecessors for subregion!\n");
mergePredecessors(Subregion, SubregionData);
// Then perform the dataflow.
NestingDetected |= SubregionData.processTopDown(
AA, RCFI, LRFI, UnmatchedRefCountInsts, DecToIncStateMap,
RegionStateInfo, SetFactory);
}
return NestingDetected;
}
//===----------------------------------------------------------------------===//
// Bottom Up Dataflow
//===----------------------------------------------------------------------===//
void LoopARCSequenceDataflowEvaluator::mergeSuccessors(const LoopRegion *Region,
ARCRegionState &State) {
bool HasAtLeastOneSucc = false;
for (unsigned SuccID : Region->getLocalSuccs()) {
auto *SuccRegion = LRFI->getRegion(SuccID);
auto &SuccState = getARCState(SuccRegion);
LLVM_DEBUG(llvm::dbgs() << " Merging Local Succ: " << SuccID << "\n");
// If this merge is undefined due to unknown control flow, assume that the
// empty set is flowing into this block so clear all state and exit early.
if (!isDefinedMerge(SuccRegion, Region)) {
State.clear();
break;
}
// If this successor allows for leaks, skip it. This can only happen at the
// function level scope. Otherwise, the block with the unreachable
// terminator will be a non-local successor.
//
// At some point we will expand this to check for regions that are post
// dominated by unreachables.
if (SuccState.allowsLeaks())
continue;
if (HasAtLeastOneSucc) {
State.mergeSuccBottomUp(SuccState);
continue;
}
State.initSuccBottomUp(SuccState);
HasAtLeastOneSucc = true;
}
for (unsigned SuccID : Region->getNonLocalSuccs()) {
auto *SuccRegion = LRFI->getRegionForNonLocalSuccessor(Region, SuccID);
auto &SuccState = getARCState(SuccRegion);
LLVM_DEBUG(llvm::dbgs() << " Merging Non Local Succs: " << SuccID
<< "\n");
// Check if this block is post dominated by ARC unreachable
// blocks. Otherwise we clear all state.
//
// TODO: We just check the block itself for now.
if (SuccState.allowsLeaks()) {
LLVM_DEBUG(llvm::dbgs() << " Allows leaks skipping\n");
continue;
}
// Otherwise, we treat it as unknown control flow.
LLVM_DEBUG(llvm::dbgs() << " Clearing state b/c of early exit\n");
State.clear();
break;
}
}
/// Analyze a single BB for refcount inc/dec instructions.
///
/// If anything was found it will be added to DecToIncStateMap.
///
/// NestingDetected will be set to indicate that the block needs to be
/// reanalyzed if code motion occurs.
///
/// An epilogue release is a release that post dominates all other uses of a
/// pointer in a function that implies that the pointer is alive up to that
/// point. We "freeze" (i.e. do not attempt to remove or move) such releases if
/// FreezeOwnedArgEpilogueReleases is set. This is useful since in certain cases
/// due to dataflow issues, we cannot properly propagate the last use
/// information. Instead we run an extra iteration of the ARC optimizer with
/// this enabled in a side table so the information gets propagated everywhere in
/// the CFG.
bool LoopARCSequenceDataflowEvaluator::processLoopBottomUp(
const LoopRegion *R, bool FreezeOwnedArgEpilogueReleases) {
bool NestingDetected = false;
// For each BB in our post order...
for (unsigned SubregionIndex : R->getReverseSubregions()) {
auto *Subregion = LRFI->getRegion(SubregionIndex);
auto &SubregionData = getARCState(Subregion);
// This will always succeed since we have an entry for each BB in our post
// order.
LLVM_DEBUG(llvm::dbgs()
<< "Processing Subregion#: " << SubregionIndex << "\n");
LLVM_DEBUG(llvm::dbgs() << "Merging Successors!\n");
mergeSuccessors(Subregion, SubregionData);
// Then perform the region optimization.
NestingDetected |= SubregionData.processBottomUp(
AA, RCFI, EAFI, LRFI, FreezeOwnedArgEpilogueReleases,
UnmatchedRefCountInsts, IncToDecStateMap, RegionStateInfo,
SetFactory);
}
return NestingDetected;
}
//===----------------------------------------------------------------------===//
// Top Level ARC Sequence Dataflow Evaluator
//===----------------------------------------------------------------------===//
LoopARCSequenceDataflowEvaluator::LoopARCSequenceDataflowEvaluator(
SILFunction &F, AliasAnalysis *AA, LoopRegionFunctionInfo *LRFI,
SILLoopInfo *SLI, RCIdentityFunctionInfo *RCFI,
EpilogueARCFunctionInfo *EAFI,
ProgramTerminationFunctionInfo *PTFI,
BlotMapVector<SILInstruction *, TopDownRefCountState> &DecToIncStateMap,
BlotMapVector<SILInstruction *, BottomUpRefCountState> &IncToDecStateMap)
: Allocator(), SetFactory(Allocator), F(F), AA(AA), LRFI(LRFI), SLI(SLI),
RCFI(RCFI), EAFI(EAFI), DecToIncStateMap(DecToIncStateMap),
IncToDecStateMap(IncToDecStateMap) {
for (auto *R : LRFI->getRegions()) {
bool AllowsLeaks = false;
if (R->isBlock())
AllowsLeaks |= PTFI->isProgramTerminatingBlock(R->getBlock());
RegionStateInfo[R] = new (Allocator) ARCRegionState(R, AllowsLeaks);
}
}
LoopARCSequenceDataflowEvaluator::~LoopARCSequenceDataflowEvaluator() {
for (auto P : RegionStateInfo) {
P.second->~ARCRegionState();
}
}
bool LoopARCSequenceDataflowEvaluator::runOnLoop(
const LoopRegion *R, bool FreezeOwnedArgEpilogueReleases,
bool RecomputePostDomReleases) {
LLVM_DEBUG(llvm::dbgs() << "Run on region:\n");
LLVM_DEBUG(R->dump(true));
bool NestingDetected = processLoopBottomUp(R, FreezeOwnedArgEpilogueReleases);
NestingDetected |= processLoopTopDown(R);
LLVM_DEBUG(
llvm::dbgs() << "*** Bottom-Up and Top-Down analysis results ***\n");
LLVM_DEBUG(dumpDataflowResults());
return NestingDetected;
}
void LoopARCSequenceDataflowEvaluator::dumpDataflowResults() {
llvm::dbgs() << "IncToDecStateMap:\n";
for (auto it : IncToDecStateMap) {
if (!it.has_value())
continue;
auto instAndState = it.value();
llvm::dbgs() << "Increment: ";
instAndState.first->dump();
instAndState.second.dump();
}
llvm::dbgs() << "DecToIncStateMap:\n";
for (auto it : DecToIncStateMap) {
if (!it.has_value())
continue;
auto instAndState = it.value();
llvm::dbgs() << "Decrement: ";
instAndState.first->dump();
instAndState.second.dump();
}
}
void LoopARCSequenceDataflowEvaluator::summarizeLoop(const LoopRegion *R) {
RegionStateInfo[R]->summarize(LRFI, RegionStateInfo);
}
void LoopARCSequenceDataflowEvaluator::summarizeSubregionBlocks(
const LoopRegion *R) {
for (unsigned SubregionID : R->getSubregions()) {
auto *Subregion = LRFI->getRegion(SubregionID);
if (!Subregion->isBlock())
continue;
RegionStateInfo[Subregion]->summarizeBlock(Subregion->getBlock());
}
}
void LoopARCSequenceDataflowEvaluator::clearLoopState(const LoopRegion *R) {
for (unsigned SubregionID : R->getSubregions())
getARCState(LRFI->getRegion(SubregionID)).clear();
}
void LoopARCSequenceDataflowEvaluator::addInterestingInst(SILInstruction *I) {
auto *Region = LRFI->getRegion(I->getParent());
RegionStateInfo[Region]->addInterestingInst(I);
}
void LoopARCSequenceDataflowEvaluator::removeInterestingInst(
SILInstruction *I) {
auto *Region = LRFI->getRegion(I->getParent());
RegionStateInfo[Region]->removeInterestingInst(I);
}
// Compute if a RefCountInst was unmatched and populate in the persistent
// UnmatchedRefCountInsts.
// This can be done by looking up the RefCountInst in IncToDecStateMap or
// DecToIncStateMap. If the StrongIncrement was matched to a StrongDecrement,
// it will be found in IncToDecStateMap. If the StrongDecrement was matched to
// a StrongIncrement, it will be found in DecToIncStateMap.
void LoopARCSequenceDataflowEvaluator::saveMatchingInfo(const LoopRegion *R) {
if (R->isFunction())
return;
for (unsigned SubregionID : R->getSubregions()) {
auto *Subregion = LRFI->getRegion(SubregionID);
if (!Subregion->isBlock())
continue;
auto *RegionState = RegionStateInfo[Subregion];
for (auto Inst : RegionState->getSummarizedInterestingInsts()) {
if (isa<StrongRetainInst>(Inst) || isa<RetainValueInst>(Inst)) {
// unmatched if not found in IncToDecStateMap
if (IncToDecStateMap.find(Inst) == IncToDecStateMap.end())
UnmatchedRefCountInsts.insert(Inst);
continue;
}
if (isa<StrongReleaseInst>(Inst) || isa<ReleaseValueInst>(Inst)) {
// unmatched if not found in DecToIncStateMap
if (DecToIncStateMap.find(Inst) == DecToIncStateMap.end())
UnmatchedRefCountInsts.insert(Inst);
continue;
}
}
}
}