Skip to content

Commit f9d2276

Browse files
committed
Merge pull request #2391 from trentxintong/InsertPoint
Remove code motion in ASO.
2 parents 8866f61 + 6d6a82f commit f9d2276

File tree

10 files changed

+122
-323
lines changed

10 files changed

+122
-323
lines changed

lib/SILOptimizer/ARC/ARCBBState.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,6 @@ void ARCBBState::mergePredTopDown(ARCBBState &PredBBState) {
127127
PtrToTopDownState.blot(RefCountedValue);
128128
continue;
129129
}
130-
131-
DEBUG(llvm::dbgs() << " Partial: "
132-
<< (RefCountState.isPartial() ? "yes" : "no") << "\n");
133130
}
134131
}
135132

lib/SILOptimizer/ARC/ARCMatchingSet.cpp

Lines changed: 36 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@
3333

3434
using namespace swift;
3535

36-
llvm::cl::opt<bool> DisableARCRRMotion("disable-code-motion-arc", llvm::cl::init(true));
37-
3836
//===----------------------------------------------------------------------===//
3937
// ARC Matching Set Builder
4038
//===----------------------------------------------------------------------===//
@@ -43,7 +41,7 @@ llvm::cl::opt<bool> DisableARCRRMotion("disable-code-motion-arc", llvm::cl::init
4341
/// were known safe.
4442
Optional<MatchingSetFlags>
4543
ARCMatchingSetBuilder::matchIncrementsToDecrements() {
46-
MatchingSetFlags Flags = {true, false};
44+
MatchingSetFlags Flags = {true, true};
4745

4846
// For each increment in our list of new increments.
4947
//
@@ -70,19 +68,16 @@ ARCMatchingSetBuilder::matchIncrementsToDecrements() {
7068
}
7169

7270
// We need to be known safe over all increments/decrements we are matching
73-
// up
74-
// to ignore insertion points.
71+
// up to ignore insertion points.
7572
bool BUIsKnownSafe = (*BURefCountState)->second.isKnownSafe();
7673
DEBUG(llvm::dbgs() << " KNOWNSAFE: "
7774
<< (BUIsKnownSafe ? "true" : "false") << "\n");
7875
Flags.KnownSafe &= BUIsKnownSafe;
7976

80-
// We can only move instructions if we know that we are not partial. We can
81-
// still delete instructions in such cases though.
82-
bool BUIsPartial = (*BURefCountState)->second.isPartial();
83-
DEBUG(llvm::dbgs() << " PARTIAL: "
84-
<< (BUIsPartial ? "true" : "false") << "\n");
85-
Flags.Partial |= BUIsPartial;
77+
bool BUCodeMotionSafe = (*BURefCountState)->second.isCodeMotionSafe();
78+
DEBUG(llvm::dbgs() << " KNOWNSAFE: "
79+
<< (BUIsKnownSafe ? "true" : "false") << "\n");
80+
Flags.CodeMotionSafe &= BUCodeMotionSafe;
8681

8782
// Now that we know we have an inst, grab the decrement.
8883
for (auto DecIter : (*BURefCountState)->second.getInstructions()) {
@@ -103,8 +98,7 @@ ARCMatchingSetBuilder::matchIncrementsToDecrements() {
10398
"decrement.\n");
10499

105100
// Make sure the increment we are looking at is also matched to our
106-
// decrement.
107-
// Otherwise bail.
101+
// decrement. Otherwise bail.
108102
if (!(*TDRefCountState)->second.isTrackingRefCountInst() ||
109103
!(*TDRefCountState)->second.containsInstruction(Increment)) {
110104
DEBUG(
@@ -121,10 +115,6 @@ ARCMatchingSetBuilder::matchIncrementsToDecrements() {
121115
continue;
122116
}
123117

124-
// Collect the increment insertion point if it has one.
125-
for (auto InsertPt : (*TDRefCountState)->second.getInsertPts()) {
126-
MatchSet.IncrementInsertPts.insert(InsertPt);
127-
}
128118
NewDecrements.push_back(Decrement);
129119
}
130120
}
@@ -134,7 +124,7 @@ ARCMatchingSetBuilder::matchIncrementsToDecrements() {
134124

135125
Optional<MatchingSetFlags>
136126
ARCMatchingSetBuilder::matchDecrementsToIncrements() {
137-
MatchingSetFlags Flags = {true, false};
127+
MatchingSetFlags Flags = {true, true};
138128

139129
// For each increment in our list of new increments.
140130
//
@@ -161,19 +151,16 @@ ARCMatchingSetBuilder::matchDecrementsToIncrements() {
161151
}
162152

163153
// We need to be known safe over all increments/decrements we are matching
164-
// up
165-
// to ignore insertion points.
154+
// up to ignore insertion points.
166155
bool TDIsKnownSafe = (*TDRefCountState)->second.isKnownSafe();
167156
DEBUG(llvm::dbgs() << " KNOWNSAFE: "
168157
<< (TDIsKnownSafe ? "true" : "false") << "\n");
169158
Flags.KnownSafe &= TDIsKnownSafe;
170159

171-
// We can only move instructions if we know that we are not partial. We can
172-
// still delete instructions in such cases though.
173-
bool TDIsPartial = (*TDRefCountState)->second.isPartial();
174-
DEBUG(llvm::dbgs() << " PARTIAL: "
175-
<< (TDIsPartial ? "true" : "false") << "\n");
176-
Flags.Partial |= TDIsPartial;
160+
bool TDCodeMotionSafe = (*TDRefCountState)->second.isCodeMotionSafe();
161+
DEBUG(llvm::dbgs() << " KNOWNSAFE: "
162+
<< (TDIsKnownSafe ? "true" : "false") << "\n");
163+
Flags.CodeMotionSafe &= TDCodeMotionSafe;
177164

178165
// Now that we know we have an inst, grab the decrement.
179166
for (auto IncIter : (*TDRefCountState)->second.getInstructions()) {
@@ -213,10 +200,6 @@ ARCMatchingSetBuilder::matchDecrementsToIncrements() {
213200
continue;
214201
}
215202

216-
// Collect the decrement insertion point if we have one.
217-
for (auto InsertPtIter : (*BURefCountState)->second.getInsertPts()) {
218-
MatchSet.DecrementInsertPts.insert(InsertPtIter);
219-
}
220203
NewIncrements.push_back(Increment);
221204
}
222205
}
@@ -231,13 +214,13 @@ ARCMatchingSetBuilder::matchDecrementsToIncrements() {
231214
bool ARCMatchingSetBuilder::matchUpIncDecSetsForPtr() {
232215
bool KnownSafeTD = true;
233216
bool KnownSafeBU = true;
234-
bool Partial = false;
217+
bool CodeMotionSafeTD = true;
218+
bool CodeMotionSafeBU = true;
235219

236220
while (true) {
237221
DEBUG(llvm::dbgs() << "Attempting to match up increments -> decrements:\n");
238222
// For each increment in our list of new increments, attempt to match them
239-
// up
240-
// with decrements and gather the insertion points of the decrements.
223+
// up with decrements and gather the insertion points of the decrements.
241224
auto Result = matchIncrementsToDecrements();
242225
if (!Result) {
243226
DEBUG(llvm::dbgs() << " FAILED TO MATCH INCREMENTS -> DECREMENTS!\n");
@@ -247,10 +230,11 @@ bool ARCMatchingSetBuilder::matchUpIncDecSetsForPtr() {
247230
DEBUG(llvm::dbgs() << " NOT KNOWN SAFE!\n");
248231
KnownSafeTD = false;
249232
}
250-
if (Result->Partial) {
251-
DEBUG(llvm::dbgs() << " IS PARTIAL!\n");
252-
Partial = true;
233+
if (!Result->CodeMotionSafe) {
234+
DEBUG(llvm::dbgs() << " NOT CODE MOTION SAFE!\n");
235+
CodeMotionSafeTD = false;
253236
}
237+
254238
NewIncrements.clear();
255239

256240
// If we do not have any decrements to attempt to match up with, bail.
@@ -267,9 +251,9 @@ bool ARCMatchingSetBuilder::matchUpIncDecSetsForPtr() {
267251
DEBUG(llvm::dbgs() << " NOT KNOWN SAFE!\n");
268252
KnownSafeBU = false;
269253
}
270-
if (Result->Partial) {
271-
DEBUG(llvm::dbgs() << " IS PARTIAL!\n");
272-
Partial = true;
254+
if (!Result->CodeMotionSafe) {
255+
DEBUG(llvm::dbgs() << " NOT CODE MOTION SAFE!\n");
256+
CodeMotionSafeBU = false;
273257
}
274258
NewDecrements.clear();
275259

@@ -278,42 +262,29 @@ bool ARCMatchingSetBuilder::matchUpIncDecSetsForPtr() {
278262
break;
279263
}
280264

265+
// There is no way we can get a top-down code motion but not a bottom-up, or vice
266+
// versa.
267+
assert(CodeMotionSafeTD == CodeMotionSafeBU && "Asymmetric code motion safety");
268+
281269
bool UnconditionallySafe = (KnownSafeTD && KnownSafeBU);
282-
if (UnconditionallySafe) {
283-
DEBUG(llvm::dbgs() << "UNCONDITIONALLY SAFE! DELETING INSTS.\n");
284-
MatchSet.IncrementInsertPts.clear();
285-
MatchSet.DecrementInsertPts.clear();
270+
bool CodeMotionSafe = (CodeMotionSafeTD && CodeMotionSafeBU);
271+
if (UnconditionallySafe || CodeMotionSafe) {
272+
DEBUG(llvm::dbgs() << "UNCONDITIONALLY OR CODE MOTION SAFE! DELETING INSTS.\n");
286273
} else {
287-
DEBUG(llvm::dbgs() << "NOT UNCONDITIONALLY SAFE!\n");
288-
}
289-
290-
bool HaveIncInsertPts = !MatchSet.IncrementInsertPts.empty();
291-
bool HaveDecInsertPts = !MatchSet.DecrementInsertPts.empty();
292-
293-
// We should not have the case which retains have to be anchored topdown and
294-
// releases do not have to be bottomup, or vice-versa.
295-
assert(HaveIncInsertPts == HaveDecInsertPts &&
296-
"Asymmetric insertion points for retains and releases");
297-
298-
bool CodeMotionBlocked = HaveIncInsertPts || HaveDecInsertPts;
299-
if (DisableARCRRMotion && CodeMotionBlocked && !UnconditionallySafe) {
300-
DEBUG(llvm::dbgs() << "Code motion blocked. Bailing!\n");
274+
DEBUG(llvm::dbgs() << "NOT UNCONDITIONALLY SAFE AND CODE MOTION BLOCKED!\n");
301275
return false;
302276
}
303277

304-
// If we have insertion points and partial merges, return false to avoid
305-
// control dependency issues.
306-
if ((HaveIncInsertPts || HaveDecInsertPts) && Partial) {
307-
DEBUG(llvm::dbgs() << "Found partial merge and insert pts. Bailing!\n");
308-
return false;
309-
}
278+
// Make sure we always have increments and decrements in the match set.
279+
assert(MatchSet.Increments.empty() == MatchSet.Decrements.empty() &&
280+
"Match set without increments or decrements");
310281

311282
// If we do not have any insertion points but we do have increments, we must
312283
// be eliminating pairs.
313-
if (!HaveIncInsertPts && !MatchSet.Increments.empty())
284+
if (!MatchSet.Increments.empty())
314285
MatchedPair = true;
315286

316287
// Success!
317-
DEBUG(llvm::dbgs() << "SUCCESS! We can move, remove things.\n");
288+
DEBUG(llvm::dbgs() << "SUCCESS! We can remove things.\n");
318289
return true;
319290
}

lib/SILOptimizer/ARC/ARCMatchingSet.h

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -43,21 +43,9 @@ struct ARCMatchingSet {
4343
/// The set of reference count increments that were paired.
4444
llvm::SetVector<SILInstruction *> Increments;
4545

46-
/// An insertion point for an increment means the earliest point in the
47-
/// program after the increment has occurred that the increment can be moved
48-
/// to
49-
/// without moving the increment over an instruction that may decrement a
50-
/// reference count.
51-
llvm::SetVector<SILInstruction *> IncrementInsertPts;
52-
5346
/// The set of reference count decrements that were paired.
5447
llvm::SetVector<SILInstruction *> Decrements;
5548

56-
/// An insertion point for a decrement means the latest point in the program
57-
/// before the decrement that the optimizer conservatively assumes that a
58-
/// reference counted value could be used.
59-
llvm::SetVector<SILInstruction *> DecrementInsertPts;
60-
6149
// This is a data structure that cannot be moved or copied.
6250
ARCMatchingSet() = default;
6351
ARCMatchingSet(const ARCMatchingSet &) = delete;
@@ -68,15 +56,13 @@ struct ARCMatchingSet {
6856
void clear() {
6957
Ptr = SILValue();
7058
Increments.clear();
71-
IncrementInsertPts.clear();
7259
Decrements.clear();
73-
DecrementInsertPts.clear();
7460
}
7561
};
7662

7763
struct MatchingSetFlags {
7864
bool KnownSafe;
79-
bool Partial;
65+
bool CodeMotionSafe;
8066
};
8167
static_assert(std::is_pod<MatchingSetFlags>::value,
8268
"MatchingSetFlags should be a pod.");

lib/SILOptimizer/ARC/ARCRegionState.cpp

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,6 @@ void ARCRegionState::mergePredTopDown(ARCRegionState &PredRegionState) {
148148
PtrToTopDownState.blot(RefCountedValue);
149149
continue;
150150
}
151-
152-
DEBUG(llvm::dbgs() << " Partial: "
153-
<< (RefCountState.isPartial() ? "yes" : "no") << "\n");
154151
}
155152
}
156153

@@ -202,7 +199,6 @@ static bool isARCSignificantTerminator(TermInst *TI) {
202199
void ARCRegionState::processBlockBottomUpPredTerminators(
203200
const LoopRegion *R, AliasAnalysis *AA, LoopRegionFunctionInfo *LRFI,
204201
ImmutablePointerSetFactory<SILInstruction> &SetFactory) {
205-
auto &BB = *R->getBlock();
206202
llvm::TinyPtrVector<SILInstruction *> PredTerminators;
207203
for (unsigned PredID : R->getPreds()) {
208204
auto *PredRegion = LRFI->getRegion(PredID);
@@ -215,13 +211,12 @@ void ARCRegionState::processBlockBottomUpPredTerminators(
215211
PredTerminators.push_back(TermInst);
216212
}
217213

218-
auto *InsertPt = &*BB.begin();
219214
for (auto &OtherState : getBottomupStates()) {
220215
// If the other state's value is blotted, skip it.
221216
if (!OtherState.hasValue())
222217
continue;
223218

224-
OtherState->second.updateForPredTerminators(PredTerminators, InsertPt,
219+
OtherState->second.updateForPredTerminators(PredTerminators,
225220
SetFactory, AA);
226221
}
227222
}
@@ -266,8 +261,6 @@ static bool processBlockBottomUpInsts(
266261
// that the instruction "visits".
267262
SILValue Op = Result.RCIdentity;
268263

269-
auto *InsertPt = &*std::next(I->getIterator());
270-
271264
// For all other (reference counted value, ref count state) we are
272265
// tracking...
273266
for (auto &OtherState : State.getBottomupStates()) {
@@ -280,7 +273,7 @@ static bool processBlockBottomUpInsts(
280273
if (Op && OtherState->first == Op)
281274
continue;
282275

283-
OtherState->second.updateForSameLoopInst(I, InsertPt, SetFactory, AA);
276+
OtherState->second.updateForSameLoopInst(I, SetFactory, AA);
284277
}
285278
}
286279

@@ -372,8 +365,7 @@ bool ARCRegionState::processLoopBottomUp(
372365
continue;
373366

374367
for (auto *I : State->getSummarizedInterestingInsts())
375-
OtherState->second.updateForDifferentLoopInst(I, InsertPts, SetFactory,
376-
AA);
368+
OtherState->second.updateForDifferentLoopInst(I, SetFactory, AA);
377369
}
378370

379371
return false;
@@ -460,7 +452,7 @@ bool ARCRegionState::processBlockTopDown(
460452
if (Op && OtherState->first == Op)
461453
continue;
462454

463-
OtherState->second.updateForSameLoopInst(I, I, SetFactory, AA);
455+
OtherState->second.updateForSameLoopInst(I, SetFactory, AA);
464456
}
465457
}
466458

@@ -485,18 +477,14 @@ bool ARCRegionState::processLoopTopDown(
485477
assert(PredRegion->isBlock() && "Expected the predecessor region to be a "
486478
"block");
487479

488-
// Our insert point is going to be the terminator inst.
489-
SILInstruction *InsertPt = PredRegion->getBlock()->getTerminator();
490-
491480
// For each state that we are currently tracking, apply our summarized
492481
// instructions to it.
493482
for (auto &OtherState : getTopDownStates()) {
494483
if (!OtherState.hasValue())
495484
continue;
496485

497486
for (auto *I : State->getSummarizedInterestingInsts())
498-
OtherState->second.updateForDifferentLoopInst(I, InsertPt, SetFactory,
499-
AA);
487+
OtherState->second.updateForDifferentLoopInst(I, SetFactory, AA);
500488
}
501489

502490
return false;

0 commit comments

Comments
 (0)