Bug 39552 - Unnecessary stack usage with flexible array member
Summary: Unnecessary stack usage with flexible array member
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: middle-end (show other bugs)
Version: 4.4.0
: P3 enhancement
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: missed-optimization
Depends on: 39545
Blocks: argument, return
  Show dependency treegraph
 
Reported: 2009-03-25 00:54 UTC by H.J. Lu
Modified: 2022-01-06 03:33 UTC (History)
2 users (show)

See Also:
Host:
Target: x86_64-linux-gnu
Build:
Known to work:
Known to fail:
Last reconfirmed: 2022-01-06 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description H.J. Lu 2009-03-25 00:54:39 UTC
After fixing PR 39545, I got

[hjl@gnu-34 pr39545]$ cat f-1.i
struct line {
int length;
char contents[];
};

void foo (struct line);

struct line
bar ()
{
 struct line x;
 x.length = sizeof (struct line);
 foo (x);
 return x;
}
[hjl@gnu-34 pr39545]$ make f-1.s
/export/build/gnu/gcc-avx/build-x86_64-linux/stage1-gcc/xgcc -B/export/build/gnu/gcc-avx/build-x86_64-linux/stage1-gcc/ -O2 -fno-asynchronous-unwind-tables -S -o f-1.s f-1.i
f-1.i: In function ‘bar’:
f-1.i:10: note: The ABI of passing struct with a flexible array member has changed in GCC 4.4
[hjl@gnu-34 pr39545]$ cat f-1.s
	.file	"f-1.i"
	.text
	.p2align 4,,15
.globl bar
	.type	bar, @function
bar:
	subq	$40, %rsp
	movl	$4, 16(%rsp)
	movq	16(%rsp), %rdi
	call	foo
	movl	$4, %eax
	addq	$40, %rsp
	ret
	.size	bar, .-bar
[hjl@gnu-34 pr39545]$ cat f-3.i
struct line {
int length;
char contents[0];
};

void foo (struct line);

struct line
bar ()
{
 struct line x;
 x.length = sizeof (struct line);
 foo (x);
 return x;
}
[hjl@gnu-34 pr39545]$ make f-3.s
/export/build/gnu/gcc-avx/build-x86_64-linux/stage1-gcc/xgcc -B/export/build/gnu/gcc-avx/build-x86_64-linux/stage1-gcc/ -O2 -fno-asynchronous-unwind-tables -S -o f-3.s f-3.i
[hjl@gnu-34 pr39545]$ cat f-3.s
	.file	"f-3.i"
	.text
	.p2align 4,,15
.globl bar
	.type	bar, @function
bar:
	subq	$8, %rsp
	movl	$4, %edi
	call	foo
	movl	$4, %eax
	addq	$8, %rsp
	ret
	.size	bar, .-bar
[hjl@gnu-34 pr39545]$ 

With char contents[0], gcc doesn't touch stack. With char contents[],
we put value on stack even if it is passed in register.
Comment 1 Andrew Pinski 2022-01-06 03:24:30 UTC
Confirmed.