Posible bug in gcc

From: David (dllorens@lsi.uji.es)
Date: Mon Feb 26 2001 - 11:30:01 EST

I hope you will find this information usefull.

I am not in the linux-kernel list so, if posible, I would like to be
personally CC'ed the answers/comments sent to the list in response to
this posting.

I think I heve found a bug in gcc. I have tried both egcs 1.1.2 (gcc
2.91.66) and gcc 2.95.2 versions.

I am attaching you a simplified test program ('bug.c', a really simple

To generate the faulty program from correct code compile as:
  gcc -O2 -o bug bug.c

You can generate good code in two ways:
1. Compiling with:
  gcc -fno-strength-reduce -O2 -o bug bug.c

   So the problem is with the option -fstrength-reduce which is
   active with the common '-O2' optimization option.

2. Uncomment the printf at line 34. Bugs are surprising.

I have also sent the bug report to the gcc maintainers.

Is it really a bug?

Thank you,

David Llorens.

#include <stdio.h>

#define SMALL_N 2
#define NUM_ELEM 4

int main(void)
  int listElem[NUM_ELEM]={30,2,10,5};
  int listSmall[SMALL_N];
  int i, j;
  int posGreatest=-1, greatest=-1;

  for (i=0; i<SMALL_N; i++) {
    listSmall[i] = listElem[i];
    if (listElem[i] > greatest) {
      posGreatest = i;
      greatest = listElem[i];
  for (i=SMALL_N; i<NUM_ELEM; i++) {
    if (listElem[i] < greatest) {
      listSmall[posGreatest] = listElem[i];
      posGreatest = 0;
      greatest = listSmall[0];
      for (j=1; j<SMALL_N; j++)
        if (listSmall[j] > greatest) {
          posGreatest = j;
          greatest = listSmall[j];
      printf("%d\n", posGreatest);
  printf("Correct output: 5 2\n");
  printf("GCC output: ");
  for (i=0; i<SMALL_N; i++) printf(" %.1d", listSmall[i]);
  return (1);

To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

This archive was generated by hypermail 2b29 : Wed Feb 28 2001 - 21:00:11 EST