Re: GCC 4.3.2 bug (was: Illegal subtraction in tmp-dive_1.s)

2009-04-21 Thread Vincent Lefevre
On 2009-04-20 15:17:44 +0200, Vincent Lefevre wrote:
 On 2009-04-17 12:09:42 -0500, Gabriel Dos Reis wrote:
  At least, let's get it archived on GCC mailing lists.
 
 Is it a bug that has been identified?

FYI, this has been fixed in the 4.3 branch in r143494.
This was PR tree-optimization/36765.

-- 
Vincent Lefèvre vinc...@vinc17.org - Web: http://www.vinc17.org/
100% accessible validated (X)HTML - Blog: http://www.vinc17.org/blog/
Work: CR INRIA - computer arithmetic / Arenaire project (LIP, ENS-Lyon)


Re: GCC 4.3.2 bug (was: Illegal subtraction in tmp-dive_1.s)‏

2009-04-20 Thread James Dennett
2009/4/19 Jason Mancini jayrus...@hotmail.com:

 Vincent Lefevre  writes:
    while ((*(q++))-- == 0) ;

 Is that defined and legal??  Is q incremented before or after *q is 
 decremented?  They are both post operators!
 Jason Mancini

It's defined and legal (so long as q != q, which might well be
guaranteed by the type system for an incrementable q -- it's late, and
I might be missing a counterexample to that).  The order of the
increment/decrement makes no difference except in the pathological
case where they attempt to change the same object (and in that case,
the behavior is undefined).

Note: the decrement is done to *initial_value_of_q, as q++ evaluates
to a copy of q's initial value.  q could even be incremented before
that, so long as the decrement still applies to *initial_value_of_q.
All of this assumes the absence of volatile, of course.

-- James


Re: GCC 4.3.2 bug (was: Illegal subtraction in tmp-dive_1.s)

2009-04-20 Thread Vincent Lefevre
On 2009-04-17 12:09:42 -0500, Gabriel Dos Reis wrote:
 At least, let's get it archived on GCC mailing lists.

Is it a bug that has been identified? If not, perhaps this should
be added to the regression tests.

The program without the quotes:

/* With GCC 4.3.2 and -O2 option: output value is 1 instead of 0.
 * If -fno-strict-aliasing is added, this bug disappears.
 */

#include stdio.h
#include stdlib.h

int test (int n)
{
  unsigned long *p, *q;
  int i;

  q = p = malloc (n * sizeof (unsigned long));
  if (p == NULL)
return 2;
  for (i = 0; i  n - 1; i++)
p[i] = 0;
  p[n - 1] = 1;
  while ((*(q++))-- == 0) ;
  return p[n - 1] == 1;
}

int main (void)
{
  int r;

  r = test (17);
  printf (%d\n, r);
  return r;
}

-- 
Vincent Lefèvre vinc...@vinc17.org - Web: http://www.vinc17.org/
100% accessible validated (X)HTML - Blog: http://www.vinc17.org/blog/
Work: CR INRIA - computer arithmetic / Arenaire project (LIP, ENS-Lyon)


Re: GCC 4.3.2 bug (was: Illegal subtraction in tmp-dive_1.s)?

2009-04-20 Thread Vincent Lefevre
On 2009-04-20 00:30:21 -0700, James Dennett wrote:
 2009/4/19 Jason Mancini jayrus...@hotmail.com:
 
  Vincent Lefevre  writes:
     while ((*(q++))-- == 0) ;
 
  Is that defined and legal??  Is q incremented before or after *q
  is decremented?  They are both post operators!
 
 It's defined and legal (so long as q != q, which might well be
[...]

Yes. BTW, I wondered if this could be due to a pathological case
such as a[a[i]] = ..., which is undefined when a[i] == i, even
though the code looks correct. But this seems to be OK.

As the bug occurs only when malloc is in the tested function, I also
wondered whether the failure was due to the use of uninitialized data
or a buffer overflow (note that in particular, the GMP code was much
more complex than my testcase), but again, this is OK.

-- 
Vincent Lefèvre vinc...@vinc17.org - Web: http://www.vinc17.org/
100% accessible validated (X)HTML - Blog: http://www.vinc17.org/blog/
Work: CR INRIA - computer arithmetic / Arenaire project (LIP, ENS-Lyon)


Re: GCC 4.3.2 bug (was: Illegal subtraction in tmp-dive_1.s)?

2009-04-20 Thread Joern Rennecke

As the bug occurs only when malloc is in the tested function,


Note that gcc 'knows' that memory obtained by malloc does not
alias other memory.

You can use a differently named wrapper function for malloc,
or use the malloc attribute for another function, to experiment
how this affects code generation.


Re: GCC 4.3.2 bug (was: Illegal subtraction in tmp-dive_1.s)?

2009-04-20 Thread Vincent Lefevre
On 2009-04-20 10:04:00 -0400, Joern Rennecke wrote:
 As the bug occurs only when malloc is in the tested function,

 Note that gcc 'knows' that memory obtained by malloc does not
 alias other memory.

Yes, in the case of GMP, this was a GMP internal function, not malloc,
but this function is declared with __attribute__ ((malloc)), and the
bug disappears if I remove this attribute. I noticed that when I tried
to simplify the testcase.

 You can use a differently named wrapper function for malloc,
 or use the malloc attribute for another function, to experiment
 how this affects code generation.

I did a test with a wrapper function my_malloc (that just calls
malloc and returns its value), but the bug was still visible,
perhaps due to optimization.

-- 
Vincent Lefèvre vinc...@vinc17.org - Web: http://www.vinc17.org/
100% accessible validated (X)HTML - Blog: http://www.vinc17.org/blog/
Work: CR INRIA - computer arithmetic / Arenaire project (LIP, ENS-Lyon)


Re: GCC 4.3.2 bug (was: Illegal subtraction in tmp-dive_1.s)‏

2009-04-19 Thread Jason Mancini

 Vincent Lefevre  writes:
while ((*(q++))-- == 0) ;

Is that defined and legal??  Is q incremented before or after *q is 
decremented?  They are both post operators!
Jason Mancini
_
Rediscover Hotmail®: Get e-mail storage that grows with you. 
http://windowslive.com/RediscoverHotmail?ocid=TXT_TAGLM_WL_HM_Rediscover_Storage2_042009


Re: GCC 4.3.2 bug (was: Illegal subtraction in tmp-dive_1.s)

2009-04-17 Thread Gabriel Dos Reis
At least, let's get it archived on GCC mailing lists.

On Fri, Apr 17, 2009 at 11:25 AM, Torbjorn Granlund t...@gmplib.org wrote:
 Vincent Lefevre vinc...@vinc17.org writes:

  FYI, here's a simple testcase:

  /* With GCC 4.3.2 and -O2 option: output value is 1 instead of 0.
   * If -fno-strict-aliasing is added, this bug disappears.
   */

  #include stdio.h
  #include stdlib.h

  int test (int n)
  {
unsigned long *p, *q;
int i;

q = p = malloc (n * sizeof (unsigned long));
if (p == NULL)
  return 2;
for (i = 0; i  n - 1; i++)
  p[i] = 0;
p[n - 1] = 1;
while ((*(q++))-- == 0) ;
return p[n - 1] == 1;
  }

  int main (void)
  {
int r;

r = test (17);
printf (%d\n, r);
return r;
  }

  You may want to use it in configure to detect the bug there (as not
  all users run make check). Possibly add -fno-strict-aliasing if
  the bug is detected.

 Nice.  I think that test should go onto the GCC testsuite.

 I suppose a problem is that the code wrt GMP need to be *executed*;
 configure feature tests should work also when cross-compiling, I think.

 --
 Torbjörn
 ___
 gmp-discuss mailing list
 gmp-disc...@gmplib.org
 http://gmplib.org/mailman/listinfo/gmp-discuss