* [Bug c/113106] Missing CSE with cast to volatile
2023-12-21 10:38 [Bug c/113106] New: Missing CSE with cast to volatile ubizjak at gmail dot com
@ 2023-12-21 10:41 ` ubizjak at gmail dot com
2023-12-21 10:44 ` ubizjak at gmail dot com
` (6 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: ubizjak at gmail dot com @ 2023-12-21 10:41 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113106
--- Comment #1 from Uroš Bizjak <ubizjak at gmail dot com> ---
Perhaps related,
--cut here--
int a;
int foo(void)
{
return *(volatile int *) &a + *(volatile int *) &a;
}
--cut here--
compiles with -O2 to:
movl a(%rip), %eax
movl a(%rip), %edx
addl %edx, %eax
ret
but may be compiled to:
movl a(%rip), %eax
addl a(%rip), %eax
ret
(the memory read may propagate to the insn)
^ permalink raw reply [flat|nested] 9+ messages in thread
* [Bug c/113106] Missing CSE with cast to volatile
2023-12-21 10:38 [Bug c/113106] New: Missing CSE with cast to volatile ubizjak at gmail dot com
2023-12-21 10:41 ` [Bug c/113106] " ubizjak at gmail dot com
@ 2023-12-21 10:44 ` ubizjak at gmail dot com
2023-12-21 12:50 ` rguenth at gcc dot gnu.org
` (5 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: ubizjak at gmail dot com @ 2023-12-21 10:44 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113106
--- Comment #2 from Uroš Bizjak <ubizjak at gmail dot com> ---
For reference, the same optimization should be applied with address spaces:
--cut here--
int __seg_gs b;
int bar(void)
{
return *(volatile __seg_gs int *) &b + b;
}
--cut here--
the above testcase currently compiles to:
movl %gs:b(%rip), %eax
addl %gs:b(%rip), %eax
ret
but can be compiled to:
movl %gs:b(%rip), %eax
addl %eax, %eax
ret
^ permalink raw reply [flat|nested] 9+ messages in thread
* [Bug c/113106] Missing CSE with cast to volatile
2023-12-21 10:38 [Bug c/113106] New: Missing CSE with cast to volatile ubizjak at gmail dot com
2023-12-21 10:41 ` [Bug c/113106] " ubizjak at gmail dot com
2023-12-21 10:44 ` ubizjak at gmail dot com
@ 2023-12-21 12:50 ` rguenth at gcc dot gnu.org
2023-12-21 12:57 ` ubizjak at gmail dot com
` (4 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: rguenth at gcc dot gnu.org @ 2023-12-21 12:50 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113106
Richard Biener <rguenth at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |rguenth at gcc dot gnu.org
--- Comment #3 from Richard Biener <rguenth at gcc dot gnu.org> ---
The situation with address-spaces isn't valid as we need to preserve the second
load because it's volatile. I think we simply refuse to combine
volatile loads out of caution in the first case.
^ permalink raw reply [flat|nested] 9+ messages in thread
* [Bug c/113106] Missing CSE with cast to volatile
2023-12-21 10:38 [Bug c/113106] New: Missing CSE with cast to volatile ubizjak at gmail dot com
` (2 preceding siblings ...)
2023-12-21 12:50 ` rguenth at gcc dot gnu.org
@ 2023-12-21 12:57 ` ubizjak at gmail dot com
2023-12-21 13:02 ` ubizjak at gmail dot com
` (3 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: ubizjak at gmail dot com @ 2023-12-21 12:57 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113106
--- Comment #4 from Uroš Bizjak <ubizjak at gmail dot com> ---
(In reply to Richard Biener from comment #3)
> The situation with address-spaces isn't valid as we need to preserve the
> second load because it's volatile. I think we simply refuse to combine
> volatile loads out of caution in the first case.
int __seg_gs b;
return *(volatile __seg_gs int *) &b + b;
But the above is the same w.r.t to volatile as:
int a;
return *(volatile int *) &a + a;
?
BTW: I also checked with clang, and it creates expected code in all cases.
^ permalink raw reply [flat|nested] 9+ messages in thread
* [Bug c/113106] Missing CSE with cast to volatile
2023-12-21 10:38 [Bug c/113106] New: Missing CSE with cast to volatile ubizjak at gmail dot com
` (3 preceding siblings ...)
2023-12-21 12:57 ` ubizjak at gmail dot com
@ 2023-12-21 13:02 ` ubizjak at gmail dot com
2023-12-21 13:29 ` [Bug rtl-optimization/113106] " rguenth at gcc dot gnu.org
` (2 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: ubizjak at gmail dot com @ 2023-12-21 13:02 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113106
--- Comment #5 from Uroš Bizjak <ubizjak at gmail dot com> ---
The issue in comment #2 happens in a couple of places when compiling linux
kernel (with named address spaces enabled). However, the issue is not specific
to named AS, I was just more attentive to moves from %gs: prefixed locations.
^ permalink raw reply [flat|nested] 9+ messages in thread
* [Bug rtl-optimization/113106] Missing CSE with cast to volatile
2023-12-21 10:38 [Bug c/113106] New: Missing CSE with cast to volatile ubizjak at gmail dot com
` (4 preceding siblings ...)
2023-12-21 13:02 ` ubizjak at gmail dot com
@ 2023-12-21 13:29 ` rguenth at gcc dot gnu.org
2023-12-21 15:08 ` ubizjak at gmail dot com
2023-12-21 15:11 ` pinskia at gcc dot gnu.org
7 siblings, 0 replies; 9+ messages in thread
From: rguenth at gcc dot gnu.org @ 2023-12-21 13:29 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113106
Richard Biener <rguenth at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Component|c |rtl-optimization
Ever confirmed|0 |1
Keywords| |missed-optimization
Last reconfirmed| |2023-12-21
Status|UNCONFIRMED |NEW
--- Comment #6 from Richard Biener <rguenth at gcc dot gnu.org> ---
(In reply to Uroš Bizjak from comment #4)
> (In reply to Richard Biener from comment #3)
> > The situation with address-spaces isn't valid as we need to preserve the
> > second load because it's volatile. I think we simply refuse to combine
> > volatile loads out of caution in the first case.
>
> int __seg_gs b;
> return *(volatile __seg_gs int *) &b + b;
>
> But the above is the same w.r.t to volatile as:
>
> int a;
> return *(volatile int *) &a + a;
>
> ?
>
> BTW: I also checked with clang, and it creates expected code in all cases.
But you don't get
movl %gs:b(%rip), %eax
addl %eax, %eax
or
movl b(%rip), %eax
addl %eax, %eax
which I think would be wrong. The volatile access doesn't need to yield
the same value as the non-volatile one so we can't value-number them the
same.
The combine issue remains of course. But GCCs point was always that
trying to optimize volatile is wasted time.
^ permalink raw reply [flat|nested] 9+ messages in thread
* [Bug rtl-optimization/113106] Missing CSE with cast to volatile
2023-12-21 10:38 [Bug c/113106] New: Missing CSE with cast to volatile ubizjak at gmail dot com
` (5 preceding siblings ...)
2023-12-21 13:29 ` [Bug rtl-optimization/113106] " rguenth at gcc dot gnu.org
@ 2023-12-21 15:08 ` ubizjak at gmail dot com
2023-12-21 15:11 ` pinskia at gcc dot gnu.org
7 siblings, 0 replies; 9+ messages in thread
From: ubizjak at gmail dot com @ 2023-12-21 15:08 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113106
--- Comment #7 from Uroš Bizjak <ubizjak at gmail dot com> ---
(In reply to Richard Biener from comment #6)
> > BTW: I also checked with clang, and it creates expected code in all cases.
>
> But you don't get
>
> movl %gs:b(%rip), %eax
> addl %eax, %eax
>
> or
>
> movl b(%rip), %eax
> addl %eax, %eax
>
> which I think would be wrong. The volatile access doesn't need to yield
> the same value as the non-volatile one so we can't value-number them the
> same.
The above is the code that clang produces for the testcases in Comment #2 and
Comment #0.
clang version 15.0.7 (Fedora 15.0.7-2.fc37)
^ permalink raw reply [flat|nested] 9+ messages in thread
* [Bug rtl-optimization/113106] Missing CSE with cast to volatile
2023-12-21 10:38 [Bug c/113106] New: Missing CSE with cast to volatile ubizjak at gmail dot com
` (6 preceding siblings ...)
2023-12-21 15:08 ` ubizjak at gmail dot com
@ 2023-12-21 15:11 ` pinskia at gcc dot gnu.org
7 siblings, 0 replies; 9+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-12-21 15:11 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113106
--- Comment #8 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to Uroš Bizjak from comment #1)
> Perhaps related,
>
> --cut here--
> int a;
>
> int foo(void)
> {
> return *(volatile int *) &a + *(volatile int *) &a;
> }
> --cut here--
>
> compiles with -O2 to:
>
> movl a(%rip), %eax
> movl a(%rip), %edx
> addl %edx, %eax
> ret
>
> but may be compiled to:
>
> movl a(%rip), %eax
> addl a(%rip), %eax
> ret
>
> (the memory read may propagate to the insn)
That is pr 3506
^ permalink raw reply [flat|nested] 9+ messages in thread