https://gcc.gnu.org/g:94ce59ad335ac372afb4521e880add4cf8fc0607

commit r16-4444-g94ce59ad335ac372afb4521e880add4cf8fc0607
Author: Andrew Pinski <[email protected]>
Date:   Tue Oct 14 09:13:51 2025 -0700

    dce: Remove __builtin_stack_save during dce [PR122037]
    
    __builtin_stack_save can be removed when the lhs becomes unused
    as it is just recording the current StackPointer into another register.
    
    Bootstrapped and tested on x86_64-linux-gnu.
    
            PR tree-optimization/122037
    
    gcc/ChangeLog:
    
            * tree-ssa-dce.cc (eliminate_unnecessary_stmts): Remove
            __builtin_stack_save when the lhs is unused.
    
    gcc/testsuite/ChangeLog:
    
            * gcc.dg/tree-ssa/vla-1.c: New test.
    
    Signed-off-by: Andrew Pinski <[email protected]>

Diff:
---
 gcc/testsuite/gcc.dg/tree-ssa/vla-1.c | 15 +++++++++++++++
 gcc/tree-ssa-dce.cc                   |  5 ++++-
 2 files changed, 19 insertions(+), 1 deletion(-)

diff --git a/gcc/testsuite/gcc.dg/tree-ssa/vla-1.c 
b/gcc/testsuite/gcc.dg/tree-ssa/vla-1.c
new file mode 100644
index 000000000000..37b75142407d
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/vla-1.c
@@ -0,0 +1,15 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized -fdump-tree-cddce1-details" } */
+/* PR tree-optimization/122037 */
+
+void bar1 (char *, int) __attribute__((noreturn));
+void foo1 (int size)
+{
+  char temp[size];
+  temp[size-1] = '\0';
+  bar1 (temp, size);
+}
+
+/* The call to __builtin_stack_save should have been removed. */
+/* { dg-final { scan-tree-dump "Deleting : __builtin_stack_save" "cddce1" } } 
*/
+/* { dg-final { scan-tree-dump-not "__builtin_stack_save " "optimized" } } */
diff --git a/gcc/tree-ssa-dce.cc b/gcc/tree-ssa-dce.cc
index 438690883226..160c28cb807e 100644
--- a/gcc/tree-ssa-dce.cc
+++ b/gcc/tree-ssa-dce.cc
@@ -1682,9 +1682,12 @@ eliminate_unnecessary_stmts (bool aggressive)
                  update_stmt (call_stmt);
                  release_ssa_name (name);
 
+                 /* __builtin_stack_save without lhs is not needed.  */
+                 if (gimple_call_builtin_p (call_stmt, BUILT_IN_STACK_SAVE))
+                   remove_dead_stmt (&gsi, bb, to_remove_edges);
                  /* GOMP_SIMD_LANE (unless three argument) or ASAN_POISON
                     without lhs is not needed.  */
-                 if (gimple_call_internal_p (call_stmt))
+                 else if (gimple_call_internal_p (call_stmt))
                    switch (gimple_call_internal_fn (call_stmt))
                      {
                      case IFN_GOMP_SIMD_LANE:

Reply via email to