This is the mail archive of the gcc@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]

serious code optimization bug in 2.8.0 / sparc


I have sent the bug report below to  gnu.g++.bug.
I don't have egcs available. Could someone check if it suffers the same
problem?
Of course, if someone had a patch ...
Thank you,

	-ulrich

the following code snippet extracted from a larger project
produces wrong output when compiled with -O2 on
SunOS gomory 5.5.1 Generic sun4m sparc SUNW,SPARCstation-20

This did not happen with 2.7.2.1 and does not happen under Linux i*86

************************ test.C: ********************************

#include <stdio.h>
typedef unsigned long word;
static const int chunk_length = 4096;
static const int unit_size    = 4;

typedef unsigned char u_char;

class chunk  {
  u_char tags[128];
  static u_char tag_mask[64];
  public:
  static const int offset;
  static const int offset_units;
  inline static u_char* word2tag(const void* p, int& first_bit) {
    chunk* pc = (chunk*) ((word) p & ~(chunk_length-1));
    int off = ((word*) p - (word*) pc) - offset_units; // bit index
    first_bit = off&7;
    return pc->tags + off/8;
  }
  void chunk::set_tags(word* p, int words);

};

void chunk::set_tags(word* p, int words) {
// called after allocation to mark allocated memory
  fprintf(stderr,"set_tags %p (%d) \n",p,words);
  int first_bit;
  // ********************** here is the problem ***********
  // word2tag seems to set first_bit erreneously to 0,
  // so that the index calculation below gives
  // 0*8 + 7 = 7
  // but it should give 2*8 + 7 = 23
  //********************************************************
  u_char* tag  = word2tag(p,first_bit);
  for (int last_bit = first_bit + words - 1; last_bit >= 0; last_bit -= 8) {
    int index = first_bit*8 + (last_bit < 7 ? last_bit : 7);
    fprintf(stderr,"%d\n", index);
    *(tag++) |= tag_mask[first_bit*8 + (last_bit < 7 ? last_bit : 7)];
    fprintf(stderr,"first: %d last: %d tag 0x%2x\n",
	    first_bit,last_bit,*(tag-1));
    first_bit = 0;
  }
} // set_tags

const int     chunk::offset         = 8+128; // first free index in bytes
const int     chunk::offset_units   = chunk::offset/unit_size;
u_char chunk::tag_mask[64];

int main() {
  int last_bit = 14;
  int first_bit;
  chunk* c = (chunk*) memalign(4096,4096);
  word*  p = (word*) ((char*) c + 0x90);
  c->set_tags(p,13);
}

************************************************************************
Wrong output with -O2:

set_tags 22090 (13) 
7
first: 0 last: 14 tag 0x 0
6
first: 0 last: 6 tag 0x 0

************************************************************************
Correct output without optimization:
set_tags 22090 (13) 
23
first: 2 last: 14 tag 0x 0
6
first: 0 last: 6 tag 0x 0

************************************************************************
compile statement:

Reading specs from /sw/local/lib/gcc-lib/sparc-sun-solaris2.5.1/2.8.0/specs
gcc version 2.8.0
 /sw/local/lib/gcc-lib/sparc-sun-solaris2.5.1/2.8.0/cpp -lang-c++ -v -undef -D__GNUC__=2 -D__GNUG__=2 -D__cplusplus -D__GNUC_MINOR__=8 -Dsparc -Dsun -Dunix -D__svr4__ -D__SVR4 -D__sparc__ -D__sun__ -D__unix__ -D__svr4__ -D__SVR4 -D__sparc -D__sun -D__unix -Asystem(unix) -Asystem(svr4) -D__EXCEPTIONS -D__OPTIMIZE__ -D__GCC_NEW_VARARGS__ -Acpu(sparc) -Amachine(sparc) test.C /var/tmp/cca002dS.ii
GNU CPP version 2.8.0 (sparc)
#include "..." search starts here:
#include <...> search starts here:
 /sw/local/include/g++
 /sw/local/sparc-sun-solaris2.5.1/include
 /sw/local/lib/gcc-lib/sparc-sun-solaris2.5.1/2.8.0/include
 /usr/include
End of search list.
 /sw/local/lib/gcc-lib/sparc-sun-solaris2.5.1/2.8.0/cc1plus /var/tmp/cca002dS.ii -quiet -dumpbase test.cc -O2 -version -o /var/tmp/cca002dS.s
GNU C++ version 2.8.0 (sparc-sun-solaris2.5.1) compiled by GNU C version 2.8.0.
test.C: In function `int main()':
test.C:46: warning: implicit declaration of function `int memalign(...)'
 /usr/ccs/bin/as -V -Qy -s -o /var/tmp/cca002dS1.o /var/tmp/cca002dS.s
/usr/ccs/bin/as: SC4.2 dev 30 Nov 1995
 /usr/ccs/bin/ld -V -Y P,/usr/ccs/lib:/usr/lib -Qy -o testopt /sw/local/lib/gcc-lib/sparc-sun-solaris2.5.1/2.8.0/crt1.o /sw/local/lib/gcc-lib/sparc-sun-solaris2.5.1/2.8.0/crti.o /usr/ccs/lib/values-Xa.o /sw/local/lib/gcc-lib/sparc-sun-solaris2.5.1/2.8.0/crtbegin.o -L/sw/local/lib/gcc-lib/sparc-sun-solaris2.5.1/2.8.0 -L/sw/local/sparc-sun-solaris2.5.1/lib -L/usr/ccs/bin -L/usr/ccs/lib -L/sw/local/lib /var/tmp/cca002dS1.o -lstdc++ -lm -lgcc -lc -lgcc /sw/local/lib/gcc-lib/sparc-sun-solaris2.5.1/2.8.0/crtend.o /sw/local/lib/gcc-lib/sparc-sun-solaris2.5.1/2.8.0/crtn.o
ld: Software Generation Utilities (SGU) SunOS/ELF (LK-2.0 (S/I) - versioning)
-- 
	-lauther

----------------------------------------------------------------------------
Ulrich Lauther          ph: +49 89 636 48834 fx: ... 636 42284
Siemens ZT SE 4         Internet: Ulrich.Lauther@mchp.siemens.de
-- 
	-lauther

----------------------------------------------------------------------------
Ulrich Lauther          ph: +49 89 636 48834 fx: ... 636 42284
Siemens ZT SE 4         Internet: Ulrich.Lauther@mchp.siemens.de


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