Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/SILOptimizer/SILCombiner/SILCombiner.h
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ class SILCombiner :
SILInstruction *visitUpcastInst(UpcastInst *UCI);
SILInstruction *visitLoadInst(LoadInst *LI);
SILInstruction *visitAllocStackInst(AllocStackInst *AS);
SILInstruction *visitAllocRefInst(AllocRefInst *AR);
SILInstruction *visitSwitchEnumAddrInst(SwitchEnumAddrInst *SEAI);
SILInstruction *visitInjectEnumAddrInst(InjectEnumAddrInst *IEAI);
SILInstruction *visitPointerToAddressInst(PointerToAddressInst *PTAI);
Expand Down
27 changes: 27 additions & 0 deletions lib/SILOptimizer/SILCombiner/SILCombinerMiscVisitors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,33 @@ SILInstruction *SILCombiner::visitAllocStackInst(AllocStackInst *AS) {
return eraseInstFromFunction(*AS);
}

SILInstruction *SILCombiner::visitAllocRefInst(AllocRefInst *AR) {
if (!AR)
return nullptr;
// Check if the only uses are deallocating stack or deallocating.
SmallPtrSet<SILInstruction *, 16> ToDelete;
bool HasNonRemovableUses = false;
for (auto UI = AR->use_begin(), UE = AR->use_end(); UI != UE;) {
auto *Op = *UI;
++UI;
auto *User = Op->getUser();
if (!isa<DeallocRefInst>(User) && !isa<SetDeallocatingInst>(User)) {
HasNonRemovableUses = true;
break;
}
ToDelete.insert(User);
}

if (HasNonRemovableUses)
return nullptr;

// Remove the instruction and all its uses.
for (auto *I : ToDelete)
eraseInstFromFunction(*I);
eraseInstFromFunction(*AR);
return nullptr;
}

SILInstruction *SILCombiner::visitLoadInst(LoadInst *LI) {
// (load (upcast-ptr %x)) -> (upcast-ref (load %x))
Builder.setCurrentDebugScope(LI->getDebugScope());
Expand Down
24 changes: 24 additions & 0 deletions test/SILOptimizer/sil_combine.sil
Original file line number Diff line number Diff line change
Expand Up @@ -3137,3 +3137,27 @@ bb0(%0 : $VV):
return %26 : $()
}

// CHECK-LABEL: sil @remove_unused_alloc_ref
// CHECK-NEXT: bb0
// CHECK-NEXT: %0 = tuple ()
// CHECK-NEXT: return %0 : $()
sil @remove_unused_alloc_ref : $@convention(thin) () -> () {
bb0:
%1 = alloc_ref $B
dealloc_ref %1 : $B
%3 = tuple ()
return %3 : $()
}

// CHECK-LABEL: sil @remove_unused_alloc_ref_stack
// CHECK-NEXT: bb0
// CHECK-NEXT: %0 = tuple ()
// CHECK-NEXT: return %0 : $()
sil @remove_unused_alloc_ref_stack : $@convention(thin) () -> () {
bb0:
%1 = alloc_ref [stack] $B
set_deallocating %1 : $B
dealloc_ref [stack] %1 : $B
%3 = tuple ()
return %3 : $()
}