Skip to content

Commit e59bb8c

Browse files
committed
[loop-arc] Teach ARC how to summarize subregions but do not wire it up to the analysis yet.
rdar://22238729
1 parent f7f1df4 commit e59bb8c

File tree

3 files changed

+85
-6
lines changed

3 files changed

+85
-6
lines changed

lib/SILAnalysis/ARC/ARCRegionState.cpp

+50-6
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,6 @@ ARCRegionState::ARCRegionState(LoopRegion *R)
3030
AllowsLeaks = isARCInertTrapBB(R->getBlock());
3131
}
3232

33-
//===---
34-
// Utility
35-
//
36-
3733
//===---
3834
// Bottom Up Merge
3935
//
@@ -160,7 +156,7 @@ void ARCRegionState::mergePredTopDown(ARCRegionState &PredRegionState) {
160156
}
161157

162158
//===---
163-
// Bottom Up Block Visitor
159+
// Bottom Up Dataflow
164160
//
165161

166162
bool ARCRegionState::processBlockBottomUp(
@@ -243,7 +239,7 @@ bool ARCRegionState::processBottomUp(
243239
}
244240

245241
//===---
246-
// Top Down Block Visitor
242+
// Top Down Dataflow
247243
//
248244

249245
bool ARCRegionState::processBlockTopDown(
@@ -327,3 +323,51 @@ bool ARCRegionState::processTopDown(
327323

328324
return processBlockTopDown(*R->getBlock(), AA, RCIA, DecToIncStateMap);
329325
}
326+
327+
//===---
328+
// Summary
329+
//
330+
331+
void ARCRegionState::summarizeBlock(SILBasicBlock *BB) {
332+
SummarizedInterestingInsts.clear();
333+
334+
for (auto &I : *BB)
335+
if (!canNeverUseValues(&I) || !canNeverDecrementRefCounts(&I))
336+
SummarizedInterestingInsts.push_back(&I);
337+
}
338+
339+
void ARCRegionState::summarizeLoop(
340+
const LoopRegion *R, LoopRegionFunctionInfo *LRFI,
341+
llvm::DenseMap<const LoopRegion *, ARCRegionState *> &RegionStateInfo) {
342+
SummarizedInterestingInsts.clear();
343+
for (unsigned SubregionID : R->getSubregions()) {
344+
LoopRegion *Subregion = LRFI->getRegion(SubregionID);
345+
ARCRegionState *SubregionState = RegionStateInfo[Subregion];
346+
std::copy(SubregionState->summarizedinterestinginsts_begin(),
347+
SubregionState->summarizedinterestinginsts_end(),
348+
std::back_inserter(SummarizedInterestingInsts));
349+
}
350+
}
351+
352+
void ARCRegionState::summarize(
353+
LoopRegionFunctionInfo *LRFI,
354+
llvm::DenseMap<const LoopRegion *, ARCRegionState *> &RegionStateInfo) {
355+
const LoopRegion *R = getRegion();
356+
357+
// We do not need to summarize a function since it is the outermost loop.
358+
if (R->isFunction())
359+
return;
360+
361+
assert(R->isLoop() && "Expected to be called on a loop");
362+
// Make sure that all subregions that are blocked are summarized. We know that
363+
// all subloops have already been summarized.
364+
for (unsigned SubregionID : R->getSubregions()) {
365+
auto *Subregion = LRFI->getRegion(SubregionID);
366+
if (!Subregion->isBlock())
367+
continue;
368+
auto *SubregionState = RegionStateInfo[Subregion];
369+
SubregionState->summarizeBlock(Subregion->getBlock());
370+
}
371+
372+
summarizeLoop(R, LRFI, RegionStateInfo);
373+
}

lib/SILAnalysis/ARC/ARCRegionState.h

+34
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,16 @@ class ARCRegionState {
5454
/// advantage of this to ignore control flow.
5555
bool AllowsLeaks;
5656

57+
/// A list of instructions contained in this region that can either use or
58+
/// decrement reference counts.
59+
///
60+
/// This is flow insensitive since we just add all of the potential
61+
/// users/decrements in subregions without caring if there is only one along a
62+
/// path. This is for simplicity in the first iteration.
63+
///
64+
/// TODO: This needs a better name.
65+
llvm::SmallVector<SILInstruction *, 4> SummarizedInterestingInsts;
66+
5767
public:
5868
ARCRegionState(LoopRegion *R);
5969

@@ -119,6 +129,22 @@ class ARCRegionState {
119129
clearBottomUpState();
120130
}
121131

132+
using const_summarizedinterestinginsts_iterator =
133+
decltype(SummarizedInterestingInsts)::const_iterator;
134+
const_summarizedinterestinginsts_iterator
135+
summarizedinterestinginsts_begin() const {
136+
return SummarizedInterestingInsts.begin();
137+
}
138+
const_summarizedinterestinginsts_iterator
139+
summarizedinterestinginsts_end() const {
140+
return SummarizedInterestingInsts.end();
141+
}
142+
iterator_range<const_summarizedinterestinginsts_iterator>
143+
getSummarizedDecrements() const {
144+
return {summarizedinterestinginsts_begin(),
145+
summarizedinterestinginsts_end()};
146+
}
147+
122148
/// Returns a reference to the basic block that we are tracking.
123149
const LoopRegion *getRegion() const { return Region.get(); }
124150

@@ -158,6 +184,10 @@ class ARCRegionState {
158184
ConsumedArgToEpilogueReleaseMatcher &ConsumedArgToReleaseMap,
159185
BlotMapVector<SILInstruction *, BottomUpRefCountState> &IncToDecStateMap);
160186

187+
void summarize(
188+
LoopRegionFunctionInfo *LRFI,
189+
llvm::DenseMap<const LoopRegion *, ARCRegionState *> &RegionStateInfo);
190+
161191
private:
162192
bool processBlockBottomUp(
163193
SILBasicBlock &BB, AliasAnalysis *AA, RCIdentityFunctionInfo *RCIA,
@@ -172,6 +202,10 @@ class ARCRegionState {
172202
BlotMapVector<SILInstruction *, TopDownRefCountState> &DecToIncStateMap);
173203
bool processLoopTopDown();
174204

205+
void summarizeBlock(SILBasicBlock *BB);
206+
void summarizeLoop(
207+
const LoopRegion *R, LoopRegionFunctionInfo *LRFI,
208+
llvm::DenseMap<const LoopRegion *, ARCRegionState *> &RegionStateInfo);
175209
};
176210

177211
} // end swift namespace

lib/SILAnalysis/ARC/GlobalLoopARCSequenceDataflow.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@ bool LoopARCSequenceDataflowEvaluator::runOnLoop(
264264
const LoopRegion *R, bool FreezeOwnedArgEpilogueReleases) {
265265
bool NestingDetected = processLoopBottomUp(R, FreezeOwnedArgEpilogueReleases);
266266
NestingDetected |= processLoopTopDown(R);
267+
RegionStateInfo[R]->summarize(LRFI, RegionStateInfo);
267268
return NestingDetected;
268269
}
269270

0 commit comments

Comments
 (0)