public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Tell GCC functions do not modify struct
@ 2022-03-11  7:03 Yangfl
  2022-03-14 21:43 ` Martin Sebor
  0 siblings, 1 reply; 2+ messages in thread
From: Yangfl @ 2022-03-11  7:03 UTC (permalink / raw)
  To: gcc-help

Hello Everyone,

I'm playing with GCC optimization and come up with a quite common scenario:

struct A { int a; };

void print_a(const struct A *);

int test() {
    const struct A a = {3};
    print_a(&a);
    return a.a == 3;
}

GCC always products a load operation of a.a, that's reasonable. But my
question is, how to tell GCC that the function is guaranteed to not
modify the struct?

I've tried `access` attribute, but that does not help. Of course I can
use __builtin_unreachable around the function call to make a promise,
but that is not a struct-independent solution.

Thanks

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Tell GCC functions do not modify struct
  2022-03-11  7:03 Tell GCC functions do not modify struct Yangfl
@ 2022-03-14 21:43 ` Martin Sebor
  0 siblings, 0 replies; 2+ messages in thread
From: Martin Sebor @ 2022-03-14 21:43 UTC (permalink / raw)
  To: Yangfl, gcc-help

On 3/11/22 00:03, Yangfl via Gcc-help wrote:
> Hello Everyone,
> 
> I'm playing with GCC optimization and come up with a quite common scenario:
> 
> struct A { int a; };
> 
> void print_a(const struct A *);
> 
> int test() {
>      const struct A a = {3};
>      print_a(&a);
>      return a.a == 3;
> }
> 
> GCC always products a load operation of a.a, that's reasonable. But my
> question is, how to tell GCC that the function is guaranteed to not
> modify the struct?
> 
> I've tried `access` attribute, but that does not help. Of course I can
> use __builtin_unreachable around the function call to make a promise,
> but that is not a struct-independent solution.

GCC doesn't yet use the access attribute for optimization.

The solution in standard C is to declare the pointer argument restrict:

   void print_a (const struct A * restrict);

Regrettably, GCC doesn't yet take advantage of this notation for
optimization either.  A request for this enhancement is being
tracked in pr81009:
   https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81009

Martin


> 
> Thanks


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-03-14 21:43 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-03-11  7:03 Tell GCC functions do not modify struct Yangfl
2022-03-14 21:43 ` Martin Sebor

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).