Skip to content

Commit 85a5826

Browse files
committed
CastOptimizer: fix a small deficiency when optimizing checked_cast_addr_br instructions.
Handle destroy_addr of the destination. When replacing the cast, just remove such destroys. In real world SIL there will always be a destroy_addr in the success-branch of the cast. Not sure if this optimization could have ever kicked in for real world code.
1 parent 75a9f4f commit 85a5826

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

lib/SILOptimizer/Utils/CastOptimizer.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -873,7 +873,8 @@ SILInstruction *CastOptimizer::simplifyCheckedCastAddrBranchInst(
873873
if (ResultNotUsed) {
874874
for (auto Use : Dest->getUses()) {
875875
auto *User = Use->getUser();
876-
if (isa<DeallocStackInst>(User) || User == Inst)
876+
if (isa<DeallocStackInst>(User) || isa<DestroyAddrInst>(User) ||
877+
User == Inst)
877878
continue;
878879
ResultNotUsed = false;
879880
break;
@@ -906,6 +907,11 @@ SILInstruction *CastOptimizer::simplifyCheckedCastAddrBranchInst(
906907
auto &srcTL = Builder.getTypeLowering(Src->getType());
907908
srcTL.emitDestroyAddress(Builder, Loc, Src);
908909
}
910+
for (auto iter = Dest->use_begin(); iter != Dest->use_end();) {
911+
SILInstruction *user = (*iter++)->getUser();
912+
if (isa<DestroyAddrInst>(user))
913+
eraseInstAction(user);
914+
}
909915
eraseInstAction(Inst);
910916
Builder.setInsertionPoint(BB);
911917
auto *NewI = Builder.createBranch(Loc, SuccessBB);

test/SILOptimizer/simplify_cfg_ossa.sil

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1549,6 +1549,7 @@ bb0(%0 : $*IsQ):
15491549

15501550
bb1:
15511551
%m1 = integer_literal $Builtin.Int1, -1
1552+
destroy_addr %p : $*Q
15521553
br bb2(%m1 : $Builtin.Int1)
15531554

15541555
bb2(%5 : $Builtin.Int1):

0 commit comments

Comments
 (0)