The attached code is compiled OK at -O0 level, but at -O1 it produces bad 
results.
The bug occurs somewhere in expression:

((a)==INT_MIN && (b)==-1)?(INT_MIN):((a)/(b))

where a and b are ints.
a is not INT_MIN and b is not -1, so division shall be performed. But at -O1
INT_MIN is returned instead.

gcc version 4.0.0 20050519 (Red Hat 4.0.0-8)

Attached is full preprocessed source (*.i file) and a shell script that tries to
detect which compilation flag causes the failure. It shows that the test passes
when compiled with '-O1 -fno-tree-ccp'.

tree-ccp_bug.c file (just for quick reading; I'll attach full *.i file in a few
seconds anyway):

#include <stdio.h>
#include <limits.h>

#define i_div(a, b)  (((a)==INT_MIN && (b)==-1)?(INT_MIN):((a)/(b)))

// 2 macros just to make an assignment look a bit more complicated for compiler:
#define i_low_set(a, b)    \
    ((a)&=(int)0xffff0000UL, (a)|=(int)(unsigned short)(b))

#define i_high_set(a, b)   \
    ((a)&=(int)0x0000ffffL, (a)|=((int)(short)(b))<<16)

int main(int argv, char*argc) {

    int d1;
    int d2;
    int s1, s2;
    int b;

    i_high_set(d1, 0x344);
    i_low_set(d1, 0x4567);
    //d1 = 0x3444567; // this makes test pass

    i_high_set(d2, 0);
    i_low_set(d2, 0x3b9a);

    printf(" dividend >>: %ld\n", d1);
    printf(" divisor  >>: %ld\n", d2);

    // just to check that the subexpression alone is computed correctly:
    b = (d1)==INT_MIN && (d2)==-1;

    s1 = d1/d2;
    s2 = i_div(d1, d2);

    printf(" results >>: %d, %ld, %ld\n", b, s1, s2);

    if(s1!=s2)
        printf(" FAILED\n");
}

-- 
           Summary: bad code generation at -O1 (conditional expression and
                    constants)
           Product: gcc
           Version: 4.0.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: tree-optimization
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: A dot Salwa at osmosys dot tv
                CC: gcc-bugs at gcc dot gnu dot org
  GCC host triplet: i386-redhat-linux
GCC target triplet: i386-redhat-linux


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

Reply via email to