Skip to content

Commit eb62b55

Browse files
Merge pull request #64980 from nate-chandler/dce/live-destroy_value
[DCE] Destroys of live values are live.
2 parents 612eeff + 38cbc6f commit eb62b55

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

lib/SILOptimizer/Transforms/DeadCodeElimination.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,8 @@ void DCE::markLive() {
280280
if (phi && (phi->isLexical() || hasPointerEscape(phi))) {
281281
markInstructionLive(&I);
282282
}
283+
// The instruction is live only if it's operand value is also live
284+
addReverseDependency(I.getOperand(0), &I);
283285
break;
284286
}
285287
case SILInstructionKind::EndBorrowInst: {
@@ -680,7 +682,8 @@ bool DCE::removeDead() {
680682
}
681683
LLVM_DEBUG(llvm::dbgs() << "Replacing branch: ");
682684
LLVM_DEBUG(Inst->dump());
683-
LLVM_DEBUG(llvm::dbgs() << "with jump to: BB" << postDom->getDebugID());
685+
LLVM_DEBUG(llvm::dbgs()
686+
<< "with jump to: BB" << postDom->getDebugID() << "\n");
684687

685688
replaceBranchWithJump(Inst, postDom);
686689
Inst->eraseFromParent();

test/SILOptimizer/dead_code_elimination_ossa.sil

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@ import Swift
88
public struct S {
99
var o: AnyObject
1010
}
11+
12+
typealias Int1 = Builtin.Int1
13+
14+
class C {}
15+
16+
struct CAndBit {
17+
var c: C
18+
var bit: Int1
19+
}
20+
1121
sil @dummy : $@convention(thin) () -> ()
1222

1323
// CHECK-LABEL: sil [ossa] @dead1 :
@@ -362,3 +372,38 @@ bb0(%0 : @guaranteed $S):
362372
%8 = tuple ()
363373
return %8 : $()
364374
}
375+
376+
// CHECK-LABEL: sil [ossa] @dce_destroy_of_live_copy : {{.*}} {
377+
// CHECK: [[COPY:%[^,]+]] = copy_value
378+
// CHECK: cond_br {{%[^,]+}}, [[LEFT:bb[0-9]+]], [[RIGHT:bb[0-9]+]]
379+
// CHECK: [[LEFT]]:
380+
// CHECK: destroy_value [[COPY]]
381+
// CHECK: br [[EXIT:bb[0-9]+]]
382+
// CHECK: [[RIGHT]]:
383+
// CHECK: destroy_value [[COPY]]
384+
// CHECK: br [[EXIT]]
385+
// CHECK: [[EXIT]]:
386+
// CHECK-LABEL: } // end sil function 'dce_destroy_of_live_copy'
387+
sil [ossa] @dce_destroy_of_live_copy : $@convention(thin) (Int1) -> () {
388+
entry(%condition : $Int1):
389+
%instance = apply undef() : $@convention(thin) () -> (@owned C)
390+
%borrow = begin_borrow %instance : $C
391+
%aggregate = struct $CAndBit(%borrow : $C, %condition : $Int1)
392+
%copy = copy_value %aggregate : $CAndBit
393+
end_borrow %borrow : $C
394+
%borrow2 = begin_borrow %copy : $CAndBit
395+
%bit = struct_extract %borrow2 : $CAndBit, #CAndBit.bit
396+
end_borrow %borrow2 : $CAndBit
397+
apply undef(%bit) : $@convention(thin) (Int1) -> ()
398+
cond_br %condition, left, right
399+
left:
400+
destroy_value %copy : $CAndBit
401+
br exit
402+
right:
403+
destroy_value %copy : $CAndBit
404+
br exit
405+
exit:
406+
destroy_value %instance : $C
407+
%retval = tuple ()
408+
return %retval : $()
409+
}

0 commit comments

Comments
 (0)