Bug 22266 - bad code generation at -O1 (conditional expression and constants)
Summary: bad code generation at -O1 (conditional expression and constants)
Alias: None
Product: gcc
Classification: Unclassified
Component: tree-optimization (show other bugs)
Version: 4.0.0
: P2 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
: 24956 (view as bug list)
Depends on:
Reported: 2005-07-01 10:29 UTC by Aleksander Salwa
Modified: 2005-11-20 15:18 UTC (History)
2 users (show)

See Also:
Host: i386-redhat-linux
Target: i386-redhat-linux
Known to work:
Known to fail:
Last reconfirmed:

full preprocessed source (3.69 KB, text/plain)
2005-07-01 10:31 UTC, Aleksander Salwa
a simple shell script that runs test with various compilation flags (260 bytes, text/plain)
2005-07-01 10:32 UTC, Aleksander Salwa

Note You need to log in before you can comment on or make changes to this bug.
Description Aleksander Salwa 2005-07-01 10:29:39 UTC
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);

        printf(" FAILED\n");
Comment 1 Aleksander Salwa 2005-07-01 10:31:13 UTC
Created attachment 9186 [details]
full preprocessed source
Comment 2 Aleksander Salwa 2005-07-01 10:32:08 UTC
Created attachment 9187 [details]
a simple shell script that runs test with various compilation flags
Comment 3 Aleksander Salwa 2005-07-01 10:44:54 UTC
It looks like a 3.4/4.0 regression - it works fine on gcc 3.4.3.
Comment 4 Andrew Pinski 2005-07-01 15:52:58 UTC
You are using an unitialized variable and putting it together piece wise,  This is undefined hence the 
warning with -Wall.
Comment 5 Andrew Pinski 2005-11-20 15:18:51 UTC
*** Bug 24956 has been marked as a duplicate of this bug. ***