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 <andrew.pinski@oss.qualcomm.com>
This commit is contained in:
Andrew Pinski 2025-10-14 09:13:51 -07:00
parent 651bf5126d
commit 94ce59ad33
2 changed files with 19 additions and 1 deletions

View File

@ -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" } } */

View File

@ -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: