public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug middle-end/98817] New: Optimize if (a != b) a = b;
@ 2021-01-25  9:20 antoshkka at gmail dot com
  2021-01-25  9:25 ` [Bug middle-end/98817] " jakub at gcc dot gnu.org
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: antoshkka at gmail dot com @ 2021-01-25  9:20 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98817

            Bug ID: 98817
           Summary: Optimize if (a != b) a = b;
           Product: gcc
           Version: 11.0
            Status: UNCONFIRMED
          Keywords: missed-optimization
          Severity: normal
          Priority: P3
         Component: middle-end
          Assignee: unassigned at gcc dot gnu.org
          Reporter: antoshkka at gmail dot com
  Target Milestone: ---

Consider the example:


void arithmetic(int& result, int value) {
    if (result != value) {
        result = value;
    }
}


GCC generates the following assembly:


arithmetic(int&, int):
  cmp DWORD PTR [rdi], esi
  je .L1
  mov DWORD PTR [rdi], esi
.L1:
  ret


The assembly seems suboptimal, because
1) cmov could be used
2) conditional jump could be totally removed, reducing the binary size and
leaving only one mov instruction:

arithmetic(int&, int):
  mov DWORD PTR [rdi], esi
  ret



Godbolt playground https://godbolt.org/z/Pdz7eP with above sample and
std::vector::clear() sample that would also benefit from the above
optimization.

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

* [Bug middle-end/98817] Optimize if (a != b) a = b;
  2021-01-25  9:20 [Bug middle-end/98817] New: Optimize if (a != b) a = b; antoshkka at gmail dot com
@ 2021-01-25  9:25 ` jakub at gcc dot gnu.org
  2021-01-25  9:34 ` antoshkka at gmail dot com
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: jakub at gcc dot gnu.org @ 2021-01-25  9:25 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98817

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jakub at gcc dot gnu.org

--- Comment #1 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
I'm not sure about this.  Turning it into an unconditional store would mean
that the memory the reference points to must be writable, that might not be
always the case.
E.g. if it refers to a .rodata object, or an object within something mapped
PROT_READ only etc.

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

* [Bug middle-end/98817] Optimize if (a != b) a = b;
  2021-01-25  9:20 [Bug middle-end/98817] New: Optimize if (a != b) a = b; antoshkka at gmail dot com
  2021-01-25  9:25 ` [Bug middle-end/98817] " jakub at gcc dot gnu.org
@ 2021-01-25  9:34 ` antoshkka at gmail dot com
  2021-01-25  9:37 ` pinskia at gcc dot gnu.org
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: antoshkka at gmail dot com @ 2021-01-25  9:34 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98817

--- Comment #2 from Antony Polukhin <antoshkka at gmail dot com> ---
(In reply to Jakub Jelinek from comment #1)
> I'm not sure about this.  Turning it into an unconditional store would mean
> that the memory the reference points to must be writable, that might not be
> always the case.

Fair pint.

How about emitting cmov instead of cmp+je?

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

* [Bug middle-end/98817] Optimize if (a != b) a = b;
  2021-01-25  9:20 [Bug middle-end/98817] New: Optimize if (a != b) a = b; antoshkka at gmail dot com
  2021-01-25  9:25 ` [Bug middle-end/98817] " jakub at gcc dot gnu.org
  2021-01-25  9:34 ` antoshkka at gmail dot com
@ 2021-01-25  9:37 ` pinskia at gcc dot gnu.org
  2021-01-25  9:41 ` jakub at gcc dot gnu.org
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-01-25  9:37 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98817

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
This cannot be done due to race conditions too:
https://gcc.gnu.org/wiki/Atomic/GCCMM/DataRaces

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

* [Bug middle-end/98817] Optimize if (a != b) a = b;
  2021-01-25  9:20 [Bug middle-end/98817] New: Optimize if (a != b) a = b; antoshkka at gmail dot com
                   ` (2 preceding siblings ...)
  2021-01-25  9:37 ` pinskia at gcc dot gnu.org
@ 2021-01-25  9:41 ` jakub at gcc dot gnu.org
  2021-01-25  9:52 ` antoshkka at gmail dot com
  2021-01-25  9:57 ` rguenth at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: jakub at gcc dot gnu.org @ 2021-01-25  9:41 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98817

--- Comment #4 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
cmov has the exact same problem as the unconditional store.  At least on x86 a
cmov for one doesn't have a MEM destination and so the conditionally assigned
register then needs to be unconditionally stored.
You'd need an instruction that would store only if the condition is true
(something like SSE/AVX masked stores, but for scalars).

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

* [Bug middle-end/98817] Optimize if (a != b) a = b;
  2021-01-25  9:20 [Bug middle-end/98817] New: Optimize if (a != b) a = b; antoshkka at gmail dot com
                   ` (3 preceding siblings ...)
  2021-01-25  9:41 ` jakub at gcc dot gnu.org
@ 2021-01-25  9:52 ` antoshkka at gmail dot com
  2021-01-25  9:57 ` rguenth at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: antoshkka at gmail dot com @ 2021-01-25  9:52 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98817

--- Comment #5 from Antony Polukhin <antoshkka at gmail dot com> ---
Please, close as invalid

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

* [Bug middle-end/98817] Optimize if (a != b) a = b;
  2021-01-25  9:20 [Bug middle-end/98817] New: Optimize if (a != b) a = b; antoshkka at gmail dot com
                   ` (4 preceding siblings ...)
  2021-01-25  9:52 ` antoshkka at gmail dot com
@ 2021-01-25  9:57 ` rguenth at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: rguenth at gcc dot gnu.org @ 2021-01-25  9:57 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98817

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |INVALID
             Status|UNCONFIRMED                 |RESOLVED

--- Comment #6 from Richard Biener <rguenth at gcc dot gnu.org> ---
.

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

end of thread, other threads:[~2021-01-25  9:57 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-01-25  9:20 [Bug middle-end/98817] New: Optimize if (a != b) a = b; antoshkka at gmail dot com
2021-01-25  9:25 ` [Bug middle-end/98817] " jakub at gcc dot gnu.org
2021-01-25  9:34 ` antoshkka at gmail dot com
2021-01-25  9:37 ` pinskia at gcc dot gnu.org
2021-01-25  9:41 ` jakub at gcc dot gnu.org
2021-01-25  9:52 ` antoshkka at gmail dot com
2021-01-25  9:57 ` rguenth at gcc dot gnu.org

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).