Skip to content

Commit 08b306d

Browse files
[StackColoring] Delete dead stack slots (#75351)
deletes slots that have lifetime markers and the lifetime ranges are empty.
1 parent f94c410 commit 08b306d

File tree

6 files changed

+48
-32
lines changed

6 files changed

+48
-32
lines changed

llvm/lib/CodeGen/StackColoring.cpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -900,6 +900,15 @@ void StackColoring::remapInstructions(DenseMap<int, int> &SlotRemap) {
900900
unsigned FixedMemOp = 0;
901901
unsigned FixedDbg = 0;
902902

903+
// Remove debug information for deleted slots.
904+
erase_if(MF->getVariableDbgInfo(), [&](auto &VI) {
905+
if (!VI.inStackSlot())
906+
return false;
907+
int Slot = VI.getStackSlot();
908+
return Slot >= 0 && Intervals[Slot]->empty() &&
909+
InterestingSlots.test(Slot) && !ConservativeSlots.test(Slot);
910+
});
911+
903912
// Remap debug information that refers to stack slots.
904913
for (auto &VI : MF->getVariableDbgInfo()) {
905914
if (!VI.Var || !VI.inStackSlot())
@@ -1250,8 +1259,15 @@ bool StackColoring::runOnMachineFunction(MachineFunction &Func) {
12501259

12511260
// Do not bother looking at empty intervals.
12521261
for (unsigned I = 0; I < NumSlots; ++I) {
1253-
if (Intervals[SortedSlots[I]]->empty())
1262+
int Slot = SortedSlots[I];
1263+
if (Intervals[Slot]->empty()) {
1264+
if (InterestingSlots.test(Slot) && !ConservativeSlots.test(Slot)) {
1265+
RemovedSlots += 1;
1266+
ReducedSize += MFI->getObjectSize(Slot);
1267+
MFI->RemoveStackObject(Slot);
1268+
}
12541269
SortedSlots[I] = -1;
1270+
}
12551271
}
12561272

12571273
// This is a simple greedy algorithm for merging allocas. First, sort the
@@ -1339,7 +1355,7 @@ bool StackColoring::runOnMachineFunction(MachineFunction &Func) {
13391355

13401356
// Scan the entire function and update all machine operands that use frame
13411357
// indices to use the remapped frame index.
1342-
if (!SlotRemap.empty()) {
1358+
if (RemovedSlots > 0) {
13431359
expungeSlotMap(SlotRemap, NumSlots);
13441360
remapInstructions(SlotRemap);
13451361
}

llvm/test/CodeGen/PowerPC/aix32-cc-abi-vaarg.ll

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,6 @@ entry:
347347

348348
; 32BIT-LABEL: stack:
349349
; 32BIT-DAG: - { id: 0, name: arg1, type: default, offset: 0, size: 4, alignment: 4,
350-
; 32BIT-DAG: - { id: 1, name: arg2, type: default, offset: 0, size: 4, alignment: 4,
351350
; 32BIT-DAG: - { id: 2, name: '', type: default, offset: 0, size: 8, alignment: 8,
352351
; 32BIT-DAG: - { id: 3, name: '', type: default, offset: 0, size: 8, alignment: 8,
353352

llvm/test/CodeGen/PowerPC/aix64-cc-abi-vaarg.ll

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,7 @@
138138
; 64BIT-LABEL: fixedStack:
139139
; 64BIT-DAG: - { id: 0, type: default, offset: 112, size: 8, alignment: 16, stack-id: default,
140140

141-
; 64BIT-LABEL: stack:
142-
; 64BIT-DAG: - { id: 0, name: arg1, type: default, offset: 0, size: 8, alignment: 8,
143-
; 64BIT-DAG: - { id: 1, name: arg2, type: default, offset: 0, size: 8, alignment: 8,
141+
; 64BIT-LABEL: stack: []
144142

145143
; 64BIT-LABEL: body: |
146144
; 64BIT-DAG: liveins: $x3, $x4, $x5, $x6, $x7, $x8, $x9, $x10
@@ -305,9 +303,7 @@
305303
; 64BIT-LABEL: fixedStack:
306304
; 64BIT-DAG: - { id: 0, type: default, offset: 152, size: 8
307305

308-
; 64BIT-LABEL: stack:
309-
; 64BIT-DAG: - { id: 0, name: arg1, type: default, offset: 0, size: 8
310-
; 64BIT-DAG: - { id: 1, name: arg2, type: default, offset: 0, size: 8
306+
; 64BIT-LABEL: stack: []
311307

312308
; 64BIT-LABEL: body: |
313309
; 64BIT-DAG: liveins: $f1, $f2, $f3, $f4, $f5, $f6, $f7, $f8, $f9, $f10, $f11, $f12, $f13
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
2+
; RUN: llc -mtriple=riscv32 -verify-machineinstrs < %s \
3+
; RUN: | FileCheck %s
4+
; RUN: llc -mtriple=riscv64 -verify-machineinstrs < %s \
5+
; RUN: | FileCheck %s
6+
7+
; Remove the lifetime-marked alloca, but not the unmarked one.
8+
define signext i32 @f1() nounwind {
9+
; CHECK-LABEL: f1:
10+
; CHECK: # %bb.0:
11+
; CHECK-NEXT: addi sp, sp, -32
12+
; CHECK-NEXT: li a0, 0
13+
; CHECK-NEXT: addi sp, sp, 32
14+
; CHECK-NEXT: ret
15+
%1 = alloca [32 x i8], align 4
16+
%2 = alloca [32 x i8], align 4
17+
%3 = getelementptr inbounds [32 x i8], ptr %1, i64 0, i64 0
18+
call void @llvm.lifetime.start.p0(i64 32, ptr nonnull %3)
19+
call void @llvm.lifetime.end.p0(i64 32, ptr nonnull %3)
20+
ret i32 0
21+
}
22+
23+
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture)
24+
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture)
25+

llvm/test/CodeGen/X86/StackColoring-tbaa.mir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ body: |
5353
; CHECK: [[LEA64r:%[0-9]+]]:gr64 = nuw LEA64r %stack.1.agg, 1, $noreg, 24, $noreg
5454
; CHECK-NEXT: CMP8mi %stack.1.agg, 1, $noreg, 47, $noreg, 0, implicit-def $eflags :: (dereferenceable load (s8) from %ir.a22, !tbaa !2)
5555
; CHECK-NEXT: [[CMOV64rm:%[0-9]+]]:gr64 = CMOV64rm [[LEA64r]], %stack.1.agg, 1, $noreg, 24, $noreg, 8, implicit $eflags :: (dereferenceable load (s64) from %ir.a2)
56-
; CHECK-NEXT: [[MOV8rm:%[0-9]+]]:gr8 = MOV8rm killed [[CMOV64rm]], 1, $noreg, 16, $noreg :: (load (s8) from %ir.add.ptr.i, !tbaa !2)
56+
; CHECK-NEXT: [[MOV8rm:%[0-9]+]]:gr8 = MOV8rm killed [[CMOV64rm]], 1, $noreg, 16, $noreg :: (load (s8) from %ir.add.ptr.i)
5757
; CHECK-NEXT: $al = COPY [[MOV8rm]]
5858
; CHECK-NEXT: RET 0, $al
5959
LIFETIME_START %stack.0.padding

llvm/test/DebugInfo/COFF/lexicalblock.ll

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -63,32 +63,12 @@
6363
; CHECK: LocalSym {
6464
; CHECK: VarName: localA
6565
; CHECK: }
66-
; CHECK: LocalSym {
67-
; CHECK: VarName: localB
68-
; CHECK: }
69-
; CHECK: BlockSym {
70-
; CHECK: Kind: S_BLOCK32 {{.*}}
71-
; CHECK: BlockName:
72-
; CHECK: }
73-
; CHECK: ScopeEndSym {
74-
; CHECK: Kind: S_END {{.*}}
75-
; CHECK: }
76-
; CHECK: BlockSym {
77-
; CHECK: Kind: S_BLOCK32 {{.*}}
78-
; CHECK: BlockName:
79-
; CHECK: }
80-
; CHECK: ScopeEndSym {
81-
; CHECK: Kind: S_END {{.*}}
82-
; CHECK: }
8366
; CHECK: BlockSym {
8467
; CHECK: Kind: S_BLOCK32 {{.*}}
8568
; CHECK: BlockName:
8669
; CHECK: }
87-
; CHECK: ScopeEndSym {
88-
; CHECK: }
89-
; CHECK: BlockSym {
90-
; CHECK: Kind: S_BLOCK32 {{.*}}
91-
; CHECK: BlockName:
70+
; CHECK: LocalSym {
71+
; CHECK: VarName: localB
9272
; CHECK: }
9373
; CHECK: ScopeEndSym {
9474
; CHECK: Kind: S_END {{.*}}

0 commit comments

Comments
 (0)