File tree Expand file tree Collapse file tree 3 files changed +52
-0
lines changed
lib/SILOptimizer/SILCombiner Expand file tree Collapse file tree 3 files changed +52
-0
lines changed Original file line number Diff line number Diff line change @@ -190,6 +190,7 @@ class SILCombiner :
190
190
SILInstruction *visitUpcastInst (UpcastInst *UCI);
191
191
SILInstruction *visitLoadInst (LoadInst *LI);
192
192
SILInstruction *visitAllocStackInst (AllocStackInst *AS);
193
+ SILInstruction *visitAllocRefInst (AllocRefInst *AR);
193
194
SILInstruction *visitSwitchEnumAddrInst (SwitchEnumAddrInst *SEAI);
194
195
SILInstruction *visitInjectEnumAddrInst (InjectEnumAddrInst *IEAI);
195
196
SILInstruction *visitPointerToAddressInst (PointerToAddressInst *PTAI);
Original file line number Diff line number Diff line change @@ -391,6 +391,33 @@ SILInstruction *SILCombiner::visitAllocStackInst(AllocStackInst *AS) {
391
391
return eraseInstFromFunction (*AS);
392
392
}
393
393
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
+
394
421
SILInstruction *SILCombiner::visitLoadInst (LoadInst *LI) {
395
422
// (load (upcast-ptr %x)) -> (upcast-ref (load %x))
396
423
Builder.setCurrentDebugScope (LI->getDebugScope ());
Original file line number Diff line number Diff line change @@ -3137,3 +3137,27 @@ bb0(%0 : $VV):
3137
3137
return %26 : $()
3138
3138
}
3139
3139
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
+ }
You can’t perform that action at this time.
0 commit comments