@@ -50,7 +50,8 @@ static bool seemsUseful(SILInstruction *I) {
50
50
// side-effects, they can be DCE'ed if they do not have useful
51
51
// dependencies/reverse dependencies
52
52
if (isa<BeginAccessInst>(I) || isa<CopyValueInst>(I) ||
53
- isa<DestroyValueInst>(I) || isa<EndLifetimeInst>(I))
53
+ isa<DestroyValueInst>(I) || isa<EndLifetimeInst>(I) ||
54
+ isa<EndBorrowInst>(I))
54
55
return false ;
55
56
56
57
// A load [copy] is okay to be DCE'ed if there are no useful dependencies
@@ -125,13 +126,12 @@ class DCE {
125
126
llvm::DenseMap<SILValue, SmallPtrSet<SILInstruction *, 4 >>
126
127
ReverseDependencies;
127
128
128
- // reborrowDependencies tracks the dependency of a reborrowed phiArg with its
129
- // renamed base value.
130
- // A reborrowed phiArg may have a new base value, if it's original base value
131
- // was also passed as a branch operand. The renamed base value should then be
132
- // live if the reborrow phiArg was also live.
129
+ // guaranteedPhiDependencies tracks the dependency of a reborrowed phiArg with
130
+ // its renamed base value. A reborrowed phiArg may have a new base value, if
131
+ // it's original base value was also passed as a branch operand. The renamed
132
+ // base value should then be live if the reborrow phiArg was also live.
133
133
using BaseValueSet = SmallPtrSet<SILValue, 8 >;
134
- llvm::DenseMap<SILPhiArgument *, BaseValueSet> reborrowDependencies ;
134
+ llvm::DenseMap<SILPhiArgument *, BaseValueSet> guaranteedPhiDependencies ;
135
135
136
136
// / Tracks if the pass changed branches.
137
137
bool BranchesChanged = false ;
@@ -143,9 +143,9 @@ class DCE {
143
143
// / Record a reverse dependency from \p from to \p to meaning \p to is live
144
144
// / if \p from is also live.
145
145
void addReverseDependency (SILValue from, SILInstruction *to);
146
- // / Starting from \p borrowInst find all reborrow dependency of its reborrows
147
- // / with their renamed base values.
148
- void findReborrowDependencies (BeginBorrowInst *borrowInst );
146
+ // / Starting from \p borrow find all reborrow and guaranteed phi dependencies
147
+ // / along with their base values.
148
+ void findGuaranteedPhiDependencies (BorrowedValue borrow );
149
149
bool removeDead ();
150
150
151
151
void computeLevelNumbers (PostDomTreeNode *root);
@@ -288,7 +288,6 @@ void DCE::markLive() {
288
288
auto *borrowInst = cast<BeginBorrowInst>(&I);
289
289
if (borrowInst->getOperand ()->getOwnershipKind () ==
290
290
OwnershipKind::Guaranteed) {
291
- markInstructionLive (borrowInst);
292
291
// Visit the end_borrows of all the borrow scopes that this
293
292
// begin_borrow could be borrowing.
294
293
SmallVector<SILValue, 4 > roots;
@@ -301,8 +300,8 @@ void DCE::markLive() {
301
300
}
302
301
continue ;
303
302
}
304
- // Populate reborrowDependencies for this borrow
305
- findReborrowDependencies ( borrowInst);
303
+ // Populate guaranteedPhiDependencies for this borrow
304
+ findGuaranteedPhiDependencies ( BorrowedValue ( borrowInst) );
306
305
// Don't optimize a borrow scope if it is lexical or has a pointer
307
306
// escape.
308
307
if (borrowInst->isLexical () ||
@@ -315,6 +314,10 @@ void DCE::markLive() {
315
314
}
316
315
break ;
317
316
}
317
+ case SILInstructionKind::LoadBorrowInst: {
318
+ findGuaranteedPhiDependencies (BorrowedValue (cast<LoadBorrowInst>(&I)));
319
+ break ;
320
+ }
318
321
default :
319
322
if (seemsUseful (&I))
320
323
markInstructionLive (&I);
@@ -337,19 +340,23 @@ void DCE::addReverseDependency(SILValue from, SILInstruction *to) {
337
340
ReverseDependencies[from].insert (to);
338
341
}
339
342
340
- void DCE::findReborrowDependencies (BeginBorrowInst *borrowInst) {
341
- LLVM_DEBUG (llvm::dbgs () << " Finding reborrow dependencies of " << borrowInst
342
- << " \n " );
343
- BorrowingOperand initialScopedOperand (&borrowInst->getOperandRef ());
344
- auto visitReborrowBaseValuePair = [&](SILPhiArgument *phiArg,
345
- SILValue baseValue) {
346
- reborrowDependencies[phiArg].insert (baseValue);
343
+ void DCE::findGuaranteedPhiDependencies (BorrowedValue borrow) {
344
+ assert (borrow.kind == BorrowedValueKind::BeginBorrow ||
345
+ borrow.kind == BorrowedValueKind::LoadBorrow);
346
+ LLVM_DEBUG (llvm::dbgs () << " Finding @guaranteed phi dependencies of "
347
+ << borrow << " \n " );
348
+ auto visitDependentPhiBaseValuePair = [&](SILPhiArgument *phiArg,
349
+ SILValue baseValue) {
350
+ guaranteedPhiDependencies[phiArg].insert (baseValue);
347
351
};
348
- // Find all reborrow dependencies starting from \p borrowInst and populate
349
- // them in reborrowDependencies
350
- findTransitiveReborrowBaseValuePairs (initialScopedOperand,
351
- borrowInst->getOperand (),
352
- visitReborrowBaseValuePair);
352
+ // Find all dependencies starting from \p borrowInst and populate
353
+ // them in guaranteedPhiDependencies
354
+ if (borrow.kind == BorrowedValueKind::BeginBorrow) {
355
+ visitExtendedReborrowPhiBaseValuePairs (cast<BeginBorrowInst>(borrow.value ),
356
+ visitDependentPhiBaseValuePair);
357
+ }
358
+ visitExtendedGuaranteedForwardingPhiBaseValuePairs (
359
+ borrow, visitDependentPhiBaseValuePair);
353
360
}
354
361
355
362
// Mark as live the terminator argument at index ArgIndex in Pred that
@@ -429,7 +436,7 @@ void DCE::propagateLiveBlockArgument(SILArgument *Arg) {
429
436
}
430
437
431
438
if (auto *phi = dyn_cast<SILPhiArgument>(Arg)) {
432
- for (auto depVal : reborrowDependencies .lookup (phi)) {
439
+ for (auto depVal : guaranteedPhiDependencies .lookup (phi)) {
433
440
markValueLive (depVal);
434
441
}
435
442
}
0 commit comments