public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: Martin Sebor <msebor@gmail.com>
To: Yangfl <mmyangfl@gmail.com>, gcc-help@gcc.gnu.org
Subject: Re: Tell GCC functions do not modify struct
Date: Mon, 14 Mar 2022 15:43:57 -0600	[thread overview]
Message-ID: <b3c280a0-94c7-fc89-9f7c-4e7b00d6a95e@gmail.com> (raw)
In-Reply-To: <CAAXyoMPRqbJMo9mV9japafA9HLE68Ze3-KUfex7OMqJKQ9q_tw@mail.gmail.com>

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


      reply	other threads:[~2022-03-14 21:43 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-11  7:03 Yangfl
2022-03-14 21:43 ` Martin Sebor [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=b3c280a0-94c7-fc89-9f7c-4e7b00d6a95e@gmail.com \
    --to=msebor@gmail.com \
    --cc=gcc-help@gcc.gnu.org \
    --cc=mmyangfl@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).