public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: "Stefan Franke" <s.franke@bebbosoft.de>
To: <gcc-help@gcc.gnu.org>
Subject: AW: AW: AW: Correct way to express to the compiler "this does not get clobbered"?
Date: Sun, 6 Dec 2020 12:29:04 +0100	[thread overview]
Message-ID: <028e01d6cbc3$012cc1e0$038645a0$@bebbosoft.de> (raw)
In-Reply-To: <35f7386c-a8db-4c48-19ce-ffc26d8feb23@hesbynett.no>



> -----Ursprüngliche Nachricht-----
> Von: David Brown <david.brown@hesbynett.no>
> Gesendet: Samstag, 5. Dezember 2020 20:03
> An: stefan@franke.ms; gcc-help@gcc.gnu.org
> Betreff: Re: AW: AW: Correct way to express to the compiler "this does not
> get clobbered"?
> 
> On 05/12/2020 15:17, Stefan Franke wrote:
> >
> >
> >> -----Ursprüngliche Nachricht-----
> >> Von: David Brown <david.brown@hesbynett.no>
> >> Gesendet: Samstag, 5. Dezember 2020 15:01
> >> An: Andrea Corallo <andrea.corallo@arm.com>; Stefan Franke
> >> <s.franke@bebbosoft.de>
> >> Cc: gcc-help@gcc.gnu.org; stefan@franke.ms
> >> Betreff: Re: AW: Correct way to express to the compiler "this does
> >> not get clobbered"?
> >>
> >> On 04/12/2020 23:52, Andrea Corallo via Gcc-help wrote:
> >>> "Stefan Franke" <s.franke@bebbosoft.de> writes:
> >>>
> >>>>>> -----Ursprüngliche Nachricht-----
> >>>>>> Von: Gcc-help <gcc-help-bounces@gcc.gnu.org> Im Auftrag von
> >> Segher
> >>>>>> Boessenkool
> >>>>>> Gesendet: Freitag, 4. Dezember 2020 19:33
> >>>>>> An: Andrea Corallo <andrea.corallo@arm.com>
> >>>>>> Cc: stefan@franke.ms; gcc-help@gcc.gnu.org
> >>>>>> Betreff: Re: Correct way to express to the compiler "this does
> >>>>>> not get clobbered"?
> >>>>>>
> >>>>>> On Fri, Dec 04, 2020 at 07:16:45PM +0100, Andrea Corallo via
> >>>>>> Gcc-help
> >>>>> wrote:
> >>>>>>> Segher Boessenkool <segher@kernel.crashing.org> writes:
> >>>>>>>
> >>>>>>>> On Fri, Dec 04, 2020 at 10:52:17AM +0100, Andrea Corallo via
> >>>>> Gcc-help
> >>>>>> wrote:
> >>>>>>>>> stefan@franke.ms writes:
> >>>>>>>>> I might open a bug but my understanding is that const is
> >>>>>>>>> generally not used for optimizations.  Am I wrong?
> >>>>>>>>
> >>>>>>>> extern const int x = 42;
> >>>>>>>> int f(void) { return x; }
> >>>>>>>>
> >>>>>>>> The code generated for f does not load the value for x from
> >> memory:
> >>>>>>>> it returns 42 always.
> >>>>>>
> >>>>>>> Are you suggesting we should treat this as a bug?
> >>>>>>
> >>>>>> Huh?  No, I am just saying that const *is* used for optimisation,
> >>>>>> with a
> >>>>> dumb
> >>>>>> simple example.  Remove const from this code and you get
> >>>>>> different generated machine code (that does load x from memory
> always).
> >>>>>>
> >>>>>> If you think you have found a missing optimisation, please make a
> >>>>>> self- contained demonstrator for that, and a file a PR?
> >>>>>>
> >>>>>>
> >>>>>> Segher
> >>>>
> >>>> IMHO it's the cprop pass which should get enhanced.
> >>>>
> >>>> Stefan
> >>>
> >>> Hi Stefan,
> >>>
> >>> IIUC declaring a pointer to constant prevents the pointer to be used
> >>> in order to modify what is pointing to.
> >>>
> >>> But in this case (the original example) it does not prevent the
> >>> function being called to modify that piece of memory (the one
> >>> pointed by
> >> y).
> >>>
> >>> Therefore I believe this is not a bug.
> >>>
> >>>   Andrea
> >>>
> >>
> >> That is, AFAIUI, correct.
> >>
> >> The case of "extern const int x = 42;" above is very different - here
> >> the object "x" is /defined/ as a const, and therefore the compiler
> >> knows it cannot ever be changed (or rather, any attempts to change it
> >> are undefined behaviour).
> >> But if you make a const pointer to something, you are merely saying
> >> that /you/ won't change the object (via that pointer).
> >>  There is no way in C to say that there can't be anything else that
> >> changes the object.
> >>
> >
> > I disagree:
> >
> > typedef struct {
> >    void (* const fun_ptr)(void);
> >    const long x;
> >  } x_t;
> >
> > Declares that fun_ptr and x both are const and cannot change. So these
> > const value can safely be propagated out of loops if the pointer to is
> > also const and can't change. The current compiler simply ignores that
> > knowledge.
> >
> > I cannot judge if this is a bug or a not implemented feature. But in
> > the current implementation only the c/c++ parser cares about const
> > whereas the compiler passes do not check constness.
> >
> > Regards
> >
> > Stefan
> >
> 
> Pointer declarations cannot tell the compiler that something never changes.
> All you can do with a pointer declaration is promise that /you/ will not change
> the object, at least not via that declaration.
> 
> This is because you can assign a pointer-to-const to point to a non-const
> object.  The "true" constness of an object comes from its definition, not from
> how you use it.


If you look at the type you'll notice the const qualifier.

With two changes to expr.c that info can be used and the compiler pulls it out of the loop.

One here to mark the const pointer as unchanging:

      if (exp && exp->ssa_name.typed.type->base.constant_flag)
	decl_rtl->unchanging = 1;
...
      /* Show we haven't gotten RTL for this yet.  */

And another one there :

	if (MEM_P (op0) && exp->base.code == COMPONENT_REF && XEXP (op0, 0)->unchanging
	    && ((exp)->exp.operands[1])->base.readonly_flag)
	  {
	    if (op0 == orig_op0)
	      op0 = copy_rtx (op0);

	    MEM_READONLY_P(op0) = 1;
	  }
...
	/* In cases where an aligned union has an unaligned object

Marks the mem as read only. And the code gets enhanced.

That's not fully correct, and just a hack, but it shows that there is present information which could be used to enhance the code generation.


/Cheers

Stefan



  reply	other threads:[~2020-12-06 11:29 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-12-03 11:47 Andrea Corallo
2020-12-03 12:04 ` Richard Earnshaw
2020-12-03 12:28   ` Andrea Corallo
2020-12-03 13:06     ` Richard Earnshaw
2020-12-03 13:40       ` Andrea Corallo
2020-12-03 18:24 ` David Brown
2020-12-03 23:00   ` Andrea Corallo
2020-12-04  7:23     ` stefan
2020-12-04  9:52       ` Andrea Corallo
2020-12-04 17:27         ` Segher Boessenkool
2020-12-04 18:16           ` Andrea Corallo
2020-12-04 18:33             ` Segher Boessenkool
2020-12-04 18:45               ` Andrea Corallo
     [not found]               ` <017701d6ca6d$7dc8dc90$795a95b0$@bebbosoft.de>
2020-12-04 18:57                 ` AW: " Stefan Franke
2020-12-04 22:52                   ` Andrea Corallo
2020-12-05 14:01                     ` David Brown
2020-12-05 14:17                       ` AW: " Stefan Franke
2020-12-05 18:10                         ` Andrea Corallo
2020-12-07 22:27                           ` Segher Boessenkool
2020-12-05 19:03                         ` David Brown
2020-12-06 11:29                           ` Stefan Franke [this message]
2020-12-05 17:29                       ` Andrea Corallo
2020-12-05 19:07                         ` David Brown
2020-12-06  9:22                           ` Jonathan Wakely
2020-12-04  8:27     ` David Brown
2020-12-04 10:05       ` Andrea Corallo
2020-12-05  8:06 ` Liu Hao

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='028e01d6cbc3$012cc1e0$038645a0$@bebbosoft.de' \
    --to=s.franke@bebbosoft.de \
    --cc=gcc-help@gcc.gnu.org \
    --cc=stefan@franke.ms \
    /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).