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]

optimization/4046: redundant comparison introduced with gcc-3.0 (regression from gcc 2)



>Number:         4046
>Category:       optimization
>Synopsis:       redundant comparison introduced with gcc-3.0 (regression from gcc 2)
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    unassigned
>State:          open
>Class:          pessimizes-code
>Submitter-Id:   net
>Arrival-Date:   Fri Aug 17 08:06:01 PDT 2001
>Closed-Date:
>Last-Modified:
>Originator:     Peter Niemayer
>Release:        gcc-3.0, x86
>Organization:
>Environment:
linux (but this doesn't matter)
>Description:
The optimizer wasn't perfect in eliminating
redundant comparisons in gcc-2 (who's perfect anyway? :-) - 
but it seems to have become worse with gcc-3.0...
>How-To-Repeat:
take a look at the assembler output of this code:

#define compi2(x, y) \
( \
 ((x) < (y))? -1 : \
 (((x) > (y))? 1 : 0 ) \
)

int testcomp(void) {
   int r = 0;
   for (int x = 0; x < 10; x++) {
      for (int y = 0; y < 10; y++) {
         if (1 == compi(x,y)) {
            r++;
         }
      }
   }
   return r;
}

You'll find that the resulting assmbler code of the inner
if-clause contains a redundant jump when using gcc-2.x
(with -O3 and any other great optimizer switch I tried :-)
e.g.:

        cmpl %edx,%ecx
        jl .L1137
        jle .L1137
        incl %ebx
.L1137

With gcc-3.0 the same case becomes even worse, now there's
an addition redundant comparison:

    cmpl    %edx, %ecx
    jl      .L13
    cmpl    %edx, %ecx
    jle     .L13
    incl    %eax
.L13

When using an inline function like this

inline int compi(const int x, const int y) {
        if (x < y) return -1;
        if (x > y) return 1;
        return 0;
}

instead of the macro, the code gets even worse.

This is somehow a tragedy as it forbids us to just implement
one comparison function instead of operator<, operator> etc.
for each class...

>Fix:

>Release-Note:
>Audit-Trail:
>Unformatted:


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