Bug 103920 - No warning for large structs passed by value ?
Summary: No warning for large structs passed by value ?
Status: UNCONFIRMED
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 12.0
: P3 enhancement
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic
Depends on:
Blocks: new-warning, new_warning cppcheck
  Show dependency treegraph
 
Reported: 2022-01-05 16:48 UTC by David Binderman
Modified: 2022-02-04 08:08 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description David Binderman 2022-01-05 16:48:50 UTC
For the following code:

$ more jan5c.cc

struct S
{
	long a;
	long b;
	long c;
	long d;
	long a2;
	long b2;
	long c2;
	long d2;
};

void f1( const S s);
void f2( S s);

gcc has not a lot to say:

$ /home/dcb/gcc/results/bin/gcc -c -O2 -Wall -Wextra -pedantic jan5c.cc
$

cppcheck does a slightly better job:

$ /home/dcb/cppcheck/trunk.git/cppcheck --enable=all jan5c.cc
jan5c.cc:14:18: performance: Function parameter 's' should be passed by const reference. [passedByValue]
void f1( const S s);
                 ^
Also, clang-13 doesn't say anything. 

Judgement call, but "large" might mean four words or more. 

Example code derived from code in the Linux kernel, so even kernel programmers
do this kind of thing in practice.
Comment 1 Andrew Pinski 2022-01-05 20:05:40 UTC
Actually I think the warning is incorrect as there are aliasing implications if passed by reference instead of by value.
Comment 2 Richard Biener 2022-01-10 10:34:25 UTC
Also consider

void f1 (const S s, S * __restrict q);

where obeying the warning would create a false alias when called as

 f1 (x, &x);

whether passing by value or reference is more efficient also depends on the
context - if the aggregate doesn't live in memory at (all) callers and
register passing conventions are in effect then passing by reference requires
materializing the argument on the stack.
Comment 3 Eric Gallager 2022-02-04 08:08:48 UTC
related to bug 59552 and/or bug 70047 perhaps?