This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug c++/30521] New: "if (i == n) ++i;" or "i += i == n;"?


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]