Bug 38527 - Gcc optimizes code to an endless loop
Summary: Gcc optimizes code to an endless loop
Status: RESOLVED DUPLICATE of bug 21920
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 4.3.2
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2008-12-14 20:11 UTC by Sergey Poznyakoff
Modified: 2008-12-14 20:38 UTC (History)
26 users (show)

See Also:
Host: i686-pc-linux-gnu
Target: i686-pc-linux-gnu
Build: i686-pc-linux-gnu
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Sergey Poznyakoff 2008-12-14 20:11:46 UTC
When compiling the following program with -O2, gcc optimizes the var_unwind_all function to an endless loop. Without -O, the program is compiled and runs correctly. The same error is present with gcc 4.3.0.

The program is (no includes):

struct rlist {
        struct rlist *prev;
};

void
_list_remove(struct rlist **last, struct rlist *obj)
{
	*last = obj->prev;
}


struct var {
	struct var *prev;
};

struct var *var_last;

void
var_unwind_all()
{
	while (var_last)
		_list_remove((struct rlist**)&var_last,
			     (struct rlist*)var_last);
		;
}


/* Implementation */
struct var bucket[2];

int
main()
{
	int i;
	struct var *cur;
	
	/* Create the list */
	for (i = 0; i < sizeof(bucket)/sizeof(bucket[0]); i++) {
		cur = bucket + i;
		cur->prev = var_last;
		var_last = cur;
	}
	
	/* Deallocate it */
	var_unwind_all();
	
	return 0;
}

The generated .s file contains:

.L10:
Comment 1 Sergey Poznyakoff 2008-12-14 20:15:07 UTC
My apologies for the inclomplete last line of the posting.  It should read:
The generated .s code contains:

.L10:
            jmp .L10

Comment 2 Andreas Schwab 2008-12-14 20:36:26 UTC
                _list_remove((struct rlist**)&var_last,
                             (struct rlist*)var_last);

This violates the C aliasing rules.

*** This bug has been marked as a duplicate of 21920 ***