(fwd) -O2 bug

Marcel van Kervinck marcelk@stack.nl
Mon Jan 25 15:07:00 GMT 1999

Someone suggested forwarding this to you. He thinks it's
an strength-reduction problem. A problem it is for sure.

--  _   _
  _| |_|_|
 |_    |_     Marcel van Kervinck
         |_|  marcelk@stack.nl

-- forwarded message --
Path: news.tue.nl!not-for-mail
From: Marcel van Kervinck <marcelk@stack.nl>
Newsgroups: gnu.gcc.help
Subject: -O2 bug
Date: 25 Jan 1999 07:48:36 +0100
Organization: M.C.G.V. Stack - Eindhoven, the Netherlands
Lines: 55
Sender: marcelk@turtle.stack.nl
Message-ID: <78h444$4d7$1@turtle.stack.nl>
NNTP-Posting-Host: turtle.stack.nl
User-Agent: tin/pre-1.4-980730 (UNIX) (FreeBSD/2.2.8-STABLE (i386))

[ This is a repost from comp.gcc.bug . I tried posting there
  there twice in a month month, but I got not a single reaction.
  Not sure what is wrong. Perhaps the ditribution of the group
  is broken. Meanwhile the bug has been reproduced with egcs
  as well (--version says 2.91.60). -MvK ]

Try this program with -O2. It initializes a linked list of the
'nodes' in the array. The output is different, depending on whether
the rand() call is in the loop or not. Actually, I think gcc has
it wrong with the rand() call commented out. Compiling with -S
shows that gcc reverses the loop direction, which is clearly giving
wrong initialization of the list. Is there something wrong with
the program, or the compiler? I tried gcc 2.7.2(.1) and gcc 2.8.0.
On Linux, FreeBSD and Solaris. (UltraSparc and Pentium II). All
configurations show the same problem.

Kind regards,

--  _   _
  _| |_|_|
 |_    |_     Marcel van Kervinck
         |_|  marcelk@stack.nl

#include <stdio.h>
#include <stdlib.h>

#define N		8

struct node {
	struct node	*next;
static struct node	*first;
static struct node	nodes[N];

int main (void)
	struct node **p;
	int i;

	p = &first;
	for (i=0; i<N; i++) {
		*p = nodes + i;
		p = &nodes[i].next;
		/* rand (); */ /* uncomment this line and it works */ 
	*p = NULL;

	puts (first==nodes
		? "this is what I expected"
		: "what is going on here?");
	return 0;

-- end of forwarded message --

More information about the Gcc-bugs mailing list