public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/30521]  New: "if (i == n) ++i;" or "i += i == n;"?
@ 2007-01-21  0:32 sigra at home dot se
  2007-01-21  8:54 ` [Bug middle-end/30521] " pinskia at gcc dot gnu dot org
                   ` (6 more replies)
  0 siblings, 7 replies; 9+ messages in thread
From: sigra at home dot se @ 2007-01-21  0:32 UTC (permalink / raw)
  To: gcc-bugs

Suppose that we have a function f that can be written in 2 ways with identical
result:
unsigned int f(unsigned int i, unsigned int n) {++i; if (i == n) ++i; return
i;}
unsigned int f(unsigned int i, unsigned int n) {++i; i += i == n; return i;}

g++ -O3 produces different code for the 2 versions:
       pushl   %ebp
.LCFI0:
+       xorl    %edx, %edx
       movl    %esp, %ebp
.LCFI1:
-       movl    8(%ebp), %edx
-       leal    1(%edx), %eax
+       movl    8(%ebp), %eax
+       incl    %eax
       cmpl    12(%ebp), %eax
-       je      .L6
       popl    %ebp
-       ret
-       .p2align 4,,7
-.L6:
-       popl    %ebp
-       leal    2(%edx), %eax
+       sete    %dl
+       addl    %edx, %eax
       ret
.LFE2:

This implies that one of the following 3 statements holds:
1. The 2 versions of f are indeed not identical.
2. The 2 versions of the generated code are equally efficient, so the
difference does not matter.
3. g++ generates suboptimal code for one of the versions of f.

An answer at [http://gcc.gnu.org/ml/gcc-help/2007-01/msg00253.html] suggests
that statement 3 holds.


-- 
           Summary: "if (i == n) ++i;" or "i += i == n;"?
           Product: gcc
           Version: 4.1.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: sigra at home dot se


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=30521


^ permalink raw reply	[flat|nested] 9+ messages in thread
[parent not found: <bug-30521-4@http.gcc.gnu.org/bugzilla/>]

end of thread, other threads:[~2012-03-04  1:45 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-01-21  0:32 [Bug c++/30521] New: "if (i == n) ++i;" or "i += i == n;"? sigra at home dot se
2007-01-21  8:54 ` [Bug middle-end/30521] " pinskia at gcc dot gnu dot org
2007-01-22  0:29 ` pinskia at gcc dot gnu dot org
2008-08-27  4:40 ` bonzini at gnu dot org
2008-08-27  4:40 ` bonzini at gnu dot org
2008-11-22  9:53 ` steven at gcc dot gnu dot org
2008-11-22 10:05 ` steven at gcc dot gnu dot org
2008-11-22 10:17 ` bonzini at gnu dot org
     [not found] <bug-30521-4@http.gcc.gnu.org/bugzilla/>
2012-03-04  1:45 ` pinskia 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).