public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: Yangfl <mmyangfl@gmail.com>
To: gcc-help@gcc.gnu.org
Subject: Tell GCC functions do not modify struct
Date: Fri, 11 Mar 2022 15:03:47 +0800	[thread overview]
Message-ID: <CAAXyoMPRqbJMo9mV9japafA9HLE68Ze3-KUfex7OMqJKQ9q_tw@mail.gmail.com> (raw)

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

             reply	other threads:[~2022-03-11  7:04 UTC|newest]

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

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=CAAXyoMPRqbJMo9mV9japafA9HLE68Ze3-KUfex7OMqJKQ9q_tw@mail.gmail.com \
    --to=mmyangfl@gmail.com \
    --cc=gcc-help@gcc.gnu.org \
    /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).