[llvm] delete dead stack slots (#104) (PR #72633)

via llvm-commits llvm-commits at lists.llvm.org
Fri Nov 17 01:58:26 PST 2023


https://github.com/mohammed-nurulhoque created https://github.com/llvm/llvm-project/pull/72633

deletes slots that have lifetime markers and the lifetime ranges are empty.

>From df700d9436b94c4252c052a44c5e1ec650792fa6 Mon Sep 17 00:00:00 2001
From: "Mohammed.NurulHoque" <Mohammed.NurulHoque at imgtec.com>
Date: Thu, 19 May 2022 10:53:44 +0100
Subject: [PATCH] delete dead stack slots (#104)

deletes slots that have lifetime markers and the lifetime ranges are empty.
---
 llvm/lib/CodeGen/StackColoring.cpp            |  9 +++++-
 .../CodeGen/PowerPC/aix32-cc-abi-vaarg.ll     |  1 -
 .../CodeGen/PowerPC/aix64-cc-abi-vaarg.ll     |  8 ++---
 llvm/test/CodeGen/RISCV/dead-stack-slot.ll    | 32 +++++++++++++++++++
 4 files changed, 42 insertions(+), 8 deletions(-)
 create mode 100644 llvm/test/CodeGen/RISCV/dead-stack-slot.ll

diff --git a/llvm/lib/CodeGen/StackColoring.cpp b/llvm/lib/CodeGen/StackColoring.cpp
index 3d261688fa8c817..98429983eb2eb2d 100644
--- a/llvm/lib/CodeGen/StackColoring.cpp
+++ b/llvm/lib/CodeGen/StackColoring.cpp
@@ -1249,8 +1249,15 @@ bool StackColoring::runOnMachineFunction(MachineFunction &Func) {
 
   // Do not bother looking at empty intervals.
   for (unsigned I = 0; I < NumSlots; ++I) {
-    if (Intervals[SortedSlots[I]]->empty())
+    int Slot = SortedSlots[I];
+    if (Intervals[Slot]->empty()) {
+      if (InterestingSlots.test(Slot) && !ConservativeSlots.test(Slot)) {
+        RemovedSlots+=1;
+        ReducedSize += MFI->getObjectSize(Slot);
+        MFI->RemoveStackObject(Slot);
+      }
       SortedSlots[I] = -1;
+    }
   }
 
   // This is a simple greedy algorithm for merging allocas. First, sort the
diff --git a/llvm/test/CodeGen/PowerPC/aix32-cc-abi-vaarg.ll b/llvm/test/CodeGen/PowerPC/aix32-cc-abi-vaarg.ll
index bf66a1ed042d228..1b0a803734ae9f3 100644
--- a/llvm/test/CodeGen/PowerPC/aix32-cc-abi-vaarg.ll
+++ b/llvm/test/CodeGen/PowerPC/aix32-cc-abi-vaarg.ll
@@ -347,7 +347,6 @@ entry:
 
 ; 32BIT-LABEL:   stack:
 ; 32BIT-DAG:     - { id: 0, name: arg1, type: default, offset: 0, size: 4, alignment: 4,
-; 32BIT-DAG:     - { id: 1, name: arg2, type: default, offset: 0, size: 4, alignment: 4,
 ; 32BIT-DAG:     - { id: 2, name: '', type: default, offset: 0, size: 8, alignment: 8,
 ; 32BIT-DAG:     - { id: 3, name: '', type: default, offset: 0, size: 8, alignment: 8,
 
diff --git a/llvm/test/CodeGen/PowerPC/aix64-cc-abi-vaarg.ll b/llvm/test/CodeGen/PowerPC/aix64-cc-abi-vaarg.ll
index ccf89aac2d5408e..a8684fdfe1c5682 100644
--- a/llvm/test/CodeGen/PowerPC/aix64-cc-abi-vaarg.ll
+++ b/llvm/test/CodeGen/PowerPC/aix64-cc-abi-vaarg.ll
@@ -138,9 +138,7 @@
 ; 64BIT-LABEL:   fixedStack:
 ; 64BIT-DAG:     - { id: 0, type: default, offset: 112, size: 8, alignment: 16, stack-id: default,
 
-; 64BIT-LABEL:   stack:
-; 64BIT-DAG:     - { id: 0, name: arg1, type: default, offset: 0, size: 8, alignment: 8,
-; 64BIT-DAG:     - { id: 1, name: arg2, type: default, offset: 0, size: 8, alignment: 8,
+; 64BIT-LABEL:   stack: []
 
 ; 64BIT-LABEL:   body:             |
 ; 64BIT-DAG:     liveins: $x3, $x4, $x5, $x6, $x7, $x8, $x9, $x10
@@ -305,9 +303,7 @@
 ; 64BIT-LABEL:   fixedStack:
 ; 64BIT-DAG:       - { id: 0, type: default, offset: 152, size: 8
 
-; 64BIT-LABEL:   stack:
-; 64BIT-DAG:       - { id: 0, name: arg1, type: default, offset: 0, size: 8
-; 64BIT-DAG:       - { id: 1, name: arg2, type: default, offset: 0, size: 8
+; 64BIT-LABEL:   stack:           []
 
 ; 64BIT-LABEL:     body:             |
 ; 64BIT-DAG:       liveins: $f1, $f2, $f3, $f4, $f5, $f6, $f7, $f8, $f9, $f10, $f11, $f12, $f13
diff --git a/llvm/test/CodeGen/RISCV/dead-stack-slot.ll b/llvm/test/CodeGen/RISCV/dead-stack-slot.ll
new file mode 100644
index 000000000000000..fdba359edddaf84
--- /dev/null
+++ b/llvm/test/CodeGen/RISCV/dead-stack-slot.ll
@@ -0,0 +1,32 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
+; RUN: llc -mtriple=riscv32 -verify-machineinstrs < %s \
+; RUN:   | FileCheck %s -check-prefix=RV32I
+; RUN: llc -mtriple=riscv64 -verify-machineinstrs < %s \
+; RUN:   | FileCheck %s -check-prefix=RV64I
+
+; Remove the lifetime-marked alloca, but not the unmarked one.
+define dso_local signext i32 @f1() nounwind {
+; RV32I-LABEL: f1:
+; RV32I:       # %bb.0:
+; RV32I-NEXT:    addi sp, sp, -32
+; RV32I-NEXT:    li a0, 0
+; RV32I-NEXT:    addi sp, sp, 32
+; RV32I-NEXT:    ret
+;
+; RV64I-LABEL: f1:
+; RV64I:       # %bb.0:
+; RV64I-NEXT:    addi sp, sp, -32
+; RV64I-NEXT:    li a0, 0
+; RV64I-NEXT:    addi sp, sp, 32
+; RV64I-NEXT:    ret
+  %1 = alloca [32 x i8], align 4
+  %2 = alloca [32 x i8], align 4
+  %3 = getelementptr inbounds [32 x i8], [32 x i8]* %1, i64 0, i64 0
+  call void @llvm.lifetime.start.p0i8(i64 32, i8* nonnull %3)
+  call void @llvm.lifetime.end.p0i8(i64 32, i8* nonnull %3)
+  ret i32 0
+}
+
+declare void @llvm.lifetime.start.p0i8(i64 immarg, i8* nocapture)
+declare void @llvm.lifetime.end.p0i8(i64 immarg, i8* nocapture)
+



More information about the llvm-commits mailing list