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

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