Skip to content

Commit 9492619

Browse files
committed
[CopyPropagation] Delete dead copy_values.
When canonicalizing an owned value's lifetime, also check whether the value is dead. If it is, track it for deletion. In particular, this eliminates dead copy_values.
1 parent c6de808 commit 9492619

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

lib/SILOptimizer/Transforms/CopyPropagation.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,9 @@ void CopyPropagation::run() {
559559
while (!defWorklist.ownedValues.empty()) {
560560
SILValue def = defWorklist.ownedValues.pop_back_val();
561561
canonicalizer.canonicalizeValueLifetime(def);
562+
// Copies of borrowed values may be dead.
563+
if (auto *inst = def->getDefiningInstruction())
564+
deleter.trackIfDead(inst);
562565
}
563566
// Recursively cleanup dead defs after removing uses.
564567
deleter.cleanupDeadInstructions();

test/SILOptimizer/copy_propagation.sil

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,10 @@ bb1:
701701
destroy_value %copy : $AnyObject
702702
%obj = load [copy] %access : $*AnyObject
703703
end_access %access : $*AnyObject
704+
// Use %obj so it doesn't get deleted because it's unused a trigger a cascade
705+
// of deletions.
706+
%f2 = function_ref @takeGuaranteedAnyObject : $@convention(thin) (@guaranteed AnyObject) -> ()
707+
apply %f2(%obj) : $@convention(thin) (@guaranteed AnyObject) -> ()
704708
destroy_value %obj : $AnyObject
705709
destroy_value %box : ${ var AnyObject }
706710
%v = tuple ()
@@ -876,3 +880,24 @@ entry(%instance : @owned $C):
876880
%retval = tuple ()
877881
return %retval : $()
878882
}
883+
884+
// Verify that a dead copy_value is deleted.
885+
// CHECK-LABEL: sil [ossa] @delete_dead_reborrow_copy : {{.*}} {
886+
// CHECK-NOT: copy_value
887+
// CHECK-LABEL: } // end sil function 'delete_dead_reborrow_copy'
888+
sil [ossa] @delete_dead_reborrow_copy : $@convention(thin) (@owned X) -> () {
889+
bb0(%instance : @owned $X):
890+
%lifetime = begin_borrow %instance : $X
891+
br bb1(%lifetime : $X)
892+
893+
bb1(%reborrow : @guaranteed $X):
894+
%dead_copy = copy_value %reborrow : $X
895+
end_borrow %reborrow : $X
896+
destroy_value %dead_copy : $X
897+
br exit
898+
899+
exit:
900+
destroy_value %instance : $X
901+
%retval = tuple ()
902+
return %retval : $()
903+
}

0 commit comments

Comments
 (0)