14
14
// /
15
15
// / Contains optimizations that eliminate redundant copy values.
16
16
// /
17
+ // / FIXME: CanonicalizeOSSALifetime likely replaces everything this file.
18
+ // /
17
19
// ===----------------------------------------------------------------------===//
18
20
21
+ #define DEBUG_TYPE " sil-semantic-arc-opts"
22
+
19
23
#include " OwnershipPhiOperand.h"
20
24
#include " SemanticARCOptVisitor.h"
21
25
#include " swift/SIL/LinearLifetimeChecker.h"
@@ -61,6 +65,8 @@ using namespace swift::semanticarc;
61
65
// are within the borrow scope.
62
66
//
63
67
// TODO: This needs a better name.
68
+ //
69
+ // FIXME: CanonicalizeOSSALifetime replaces this once it supports borrows.
64
70
bool SemanticARCOptVisitor::performGuaranteedCopyValueOptimization (
65
71
CopyValueInst *cvi) {
66
72
// For now, do not run this optimization. This is just to be careful.
@@ -236,6 +242,7 @@ bool SemanticARCOptVisitor::performGuaranteedCopyValueOptimization(
236
242
237
243
// Otherwise, our copy must truly not be needed, o RAUW and convert to
238
244
// guaranteed!
245
+ LLVM_DEBUG (llvm::dbgs () << " Replace copy with guaranteed source: " << *cvi);
239
246
std::move (lr).convertToGuaranteedAndRAUW (cvi->getOperand (), getCallbacks ());
240
247
return true ;
241
248
}
@@ -246,13 +253,16 @@ bool SemanticARCOptVisitor::performGuaranteedCopyValueOptimization(
246
253
247
254
// / If cvi only has destroy value users, then cvi is a dead live range. Lets
248
255
// / eliminate all such dead live ranges.
256
+ // /
257
+ // / FIXME: CanonicalizeOSSALifetime replaces this.
249
258
bool SemanticARCOptVisitor::eliminateDeadLiveRangeCopyValue (
250
259
CopyValueInst *cvi) {
251
260
// This is a cheap optimization generally.
252
261
253
262
// See if we are lucky and have a simple case.
254
263
if (auto *op = cvi->getSingleUse ()) {
255
264
if (auto *dvi = dyn_cast<DestroyValueInst>(op->getUser ())) {
265
+ LLVM_DEBUG (llvm::dbgs () << " Erasing single-use copy: " << *cvi);
256
266
eraseInstruction (dvi);
257
267
eraseInstructionAndAddOperandsToWorklist (cvi);
258
268
return true ;
@@ -276,6 +286,7 @@ bool SemanticARCOptVisitor::eliminateDeadLiveRangeCopyValue(
276
286
}
277
287
278
288
// Now that we have a truly dead live range copy value, eliminate it!
289
+ LLVM_DEBUG (llvm::dbgs () << " Eliminate dead copy: " << *cvi);
279
290
while (!destroys.empty ()) {
280
291
eraseInstruction (destroys.pop_back_val ());
281
292
}
@@ -366,6 +377,8 @@ static bool tryJoinIfDestroyConsumingUseInSameBlock(
366
377
SmallPtrSet<SILInstruction *, 8 > visitedInsts;
367
378
if (!isUseBetweenInstAndBlockEnd (singleCVIConsumingUse->getUser (),
368
379
&dvi->getAllOperands ()[0 ], &visitedInsts)) {
380
+ LLVM_DEBUG (llvm::dbgs ()
381
+ << " Eliminate copy with useless lifetime: " << *cvi);
369
382
ctx.eraseInstruction (dvi);
370
383
ctx.eraseAndRAUWSingleValueInstruction (cvi, operand);
371
384
return true ;
@@ -474,6 +487,8 @@ static bool tryJoinIfDestroyConsumingUseInSameBlock(
474
487
}
475
488
476
489
// Ok, we now know that we can eliminate this value.
490
+ LLVM_DEBUG (llvm::dbgs ()
491
+ << " Eliminate borrowed copy with useless lifetime: " << *cvi);
477
492
ctx.eraseInstruction (dvi);
478
493
ctx.eraseAndRAUWSingleValueInstruction (cvi, operand);
479
494
return true ;
@@ -532,6 +547,7 @@ static bool tryJoiningIfCopyOperandHasSingleDestroyValue(
532
547
// post-dominate destroy_value and can eliminate the hand off traffic.
533
548
if (canJoinIfCopyDiesInFunctionExitingBlock (operand, dvi, cvi,
534
549
singleCVIConsumingUse)) {
550
+ LLVM_DEBUG (llvm::dbgs () << " Eliminate returned copy: " << *cvi);
535
551
ctx.eraseInstruction (dvi);
536
552
ctx.eraseAndRAUWSingleValueInstruction (cvi, operand);
537
553
return true ;
@@ -602,6 +618,8 @@ static bool tryJoiningIfCopyOperandHasSingleDestroyValue(
602
618
// interior pointers (e.x. project_box) are properly guarded by
603
619
// begin_borrow. Because of that we can not shrink lifetimes and instead rely on
604
620
// SILGen's correctness.
621
+ //
622
+ // FIXME: CanonicalizeOSSALifetime replaces this.
605
623
bool SemanticARCOptVisitor::tryJoiningCopyValueLiveRangeWithOperand (
606
624
CopyValueInst *cvi) {
607
625
// First do a quick check if our operand is owned. If it is not owned, we can
@@ -659,6 +677,8 @@ bool SemanticARCOptVisitor::tryJoiningCopyValueLiveRangeWithOperand(
659
677
// can optimize without any further analysis since we know we will not be
660
678
// shrinking lifetimes of owned values.
661
679
if (singleCVIConsumingUse == nullptr ) {
680
+ LLVM_DEBUG (llvm::dbgs ()
681
+ << " Eliminate multiply consumed live-out copy: " << *cvi);
662
682
eraseInstruction (dvi);
663
683
eraseAndRAUWSingleValueInstruction (cvi, operand);
664
684
return true ;
@@ -668,6 +688,7 @@ bool SemanticARCOptVisitor::tryJoiningCopyValueLiveRangeWithOperand(
668
688
// is not in the same block as our copy_value/destroy_value, it must be live
669
689
// out of the block and thus we are not shrinking any lifetimes.
670
690
if (singleCVIConsumingUse->getParentBlock () != cvi->getParent ()) {
691
+ LLVM_DEBUG (llvm::dbgs () << " Eliminate non-local live-out copy: " << *cvi);
671
692
eraseInstruction (dvi);
672
693
eraseAndRAUWSingleValueInstruction (cvi, operand);
673
694
return true ;
@@ -702,6 +723,8 @@ bool SemanticARCOptVisitor::tryJoiningCopyValueLiveRangeWithOperand(
702
723
703
724
// / Given an owned value that is completely enclosed within its parent owned
704
725
// / value and is not consumed, eliminate the copy.
726
+ // /
727
+ // / FIXME: CanonicalizeOSSALifetime replaces this.
705
728
bool SemanticARCOptVisitor::tryPerformOwnedCopyValueOptimization (
706
729
CopyValueInst *cvi) {
707
730
if (ctx.onlyGuaranteedOpts )
0 commit comments