Skip to content

Commit 0d24de7

Browse files
committed
[sil-combine] Add peephole: alloc_ref/set_deallocating/dealloc_ref -> remove these instructions
Discovered this missing peephole while looking at the performance of swiftlang#7420
1 parent beb9bcc commit 0d24de7

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

lib/SILOptimizer/SILCombiner/SILCombiner.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ class SILCombiner :
190190
SILInstruction *visitUpcastInst(UpcastInst *UCI);
191191
SILInstruction *visitLoadInst(LoadInst *LI);
192192
SILInstruction *visitAllocStackInst(AllocStackInst *AS);
193+
SILInstruction *visitAllocRefInst(AllocRefInst *AR);
193194
SILInstruction *visitSwitchEnumAddrInst(SwitchEnumAddrInst *SEAI);
194195
SILInstruction *visitInjectEnumAddrInst(InjectEnumAddrInst *IEAI);
195196
SILInstruction *visitPointerToAddressInst(PointerToAddressInst *PTAI);

lib/SILOptimizer/SILCombiner/SILCombinerMiscVisitors.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,33 @@ SILInstruction *SILCombiner::visitAllocStackInst(AllocStackInst *AS) {
391391
return eraseInstFromFunction(*AS);
392392
}
393393

394+
SILInstruction *SILCombiner::visitAllocRefInst(AllocRefInst *AR) {
395+
if (!AR)
396+
return nullptr;
397+
// Check if the only uses are deallocating stack or deallocating.
398+
SmallPtrSet<SILInstruction *, 16> ToDelete;
399+
bool HasNonRemovableUses = false;
400+
for (auto UI = AR->use_begin(), UE = AR->use_end(); UI != UE;) {
401+
auto *Op = *UI;
402+
++UI;
403+
auto *User = Op->getUser();
404+
if (!isa<DeallocRefInst>(User) && !isa<SetDeallocatingInst>(User)) {
405+
HasNonRemovableUses = true;
406+
break;
407+
}
408+
ToDelete.insert(User);
409+
}
410+
411+
if (HasNonRemovableUses)
412+
return nullptr;
413+
414+
// Remove the instruction and all its uses.
415+
for (auto *I : ToDelete)
416+
eraseInstFromFunction(*I);
417+
eraseInstFromFunction(*AR);
418+
return nullptr;
419+
}
420+
394421
SILInstruction *SILCombiner::visitLoadInst(LoadInst *LI) {
395422
// (load (upcast-ptr %x)) -> (upcast-ref (load %x))
396423
Builder.setCurrentDebugScope(LI->getDebugScope());

test/SILOptimizer/sil_combine.sil

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3137,3 +3137,27 @@ bb0(%0 : $VV):
31373137
return %26 : $()
31383138
}
31393139

3140+
// CHECK-LABEL: sil @remove_unused_alloc_ref
3141+
// CHECK-NEXT: bb0
3142+
// CHECK-NEXT: %0 = tuple ()
3143+
// CHECK-NEXT: return %0 : $()
3144+
sil @remove_unused_alloc_ref : $@convention(thin) () -> () {
3145+
bb0:
3146+
%1 = alloc_ref $B
3147+
dealloc_ref %1 : $B
3148+
%3 = tuple ()
3149+
return %3 : $()
3150+
}
3151+
3152+
// CHECK-LABEL: sil @remove_unused_alloc_ref_stack
3153+
// CHECK-NEXT: bb0
3154+
// CHECK-NEXT: %0 = tuple ()
3155+
// CHECK-NEXT: return %0 : $()
3156+
sil @remove_unused_alloc_ref_stack : $@convention(thin) () -> () {
3157+
bb0:
3158+
%1 = alloc_ref [stack] $B
3159+
set_deallocating %1 : $B
3160+
dealloc_ref [stack] %1 : $B
3161+
%3 = tuple ()
3162+
return %3 : $()
3163+
}

0 commit comments

Comments
 (0)