This is the mail archive of the gcc-prs@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]
Other format: [Raw text]

optimization/10562: Code segment optimization incorrectly disappeared with -O2 option


>Number:         10562
>Category:       optimization
>Synopsis:       Code segment optimization incorrectly disappeared with -O2 option
>Confidential:   no
>Severity:       critical
>Priority:       medium
>Responsible:    unassigned
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Wed Apr 30 06:16:01 UTC 2003
>Closed-Date:
>Last-Modified:
>Originator:     fleming.feng@intel.com
>Release:        gcc-3.2 20020903
>Organization:
>Environment:
Red Hat Linux 8.0 3.2-7
>Description:
When I compile the following code with a header file and a C file like following:

test.h

#define cpu_online_map	1

static inline unsigned int generic_hweight32(unsigned int w)
{
	unsigned int res = (w & 0x55555555) + ((w >> 1) & 0x55555555);
	res = (res & 0x33333333) + ((res >> 2) & 0x33333333);
	res = (res & 0x0F0F0F0F) + ((res >> 4) & 0x0F0F0F0F);
	res = (res & 0x00FF00FF) + ((res >> 8) & 0x00FF00FF);
	return (res & 0x0000FFFF) + ((res>>16) & 0x0000FFFFF);
}

#define hweight32(x)	generic_hweight32(x)

static inline unsigned int num_online_cpus(void)
{
	return hweight32(cpu_online_map);
}

test.c


#include <stdio.h>
#include "test.h"

int main(int argc, char* argv[])
{

	int i;

	for(i = 0; i < num_online_cpus(); i++){
		printf("gcc compiled correctly!\n");
	}

	return 0;
	
}

The code in the "for" loop should at least executed one time! Because it equals to:
  for(i = 0; i < 1; i ++){
    ....
  }

but if it is compiled with -O2 option the code segment in "for" loop will not be executed. Using objdump to disassembly the output file, it can be found the code segment disappeared. If only use -O option, the result is correct.
In fact the code in header file comes from linux kernel 2.4.19. And the bug is found during compile a kernel mode driver. 
This also happens on arm-linux-gcc version 3.2.1 which is a cross compile tool chain for ARM platform.

>How-To-Repeat:
Just compile the code in description with -O2 option and execute the output file.
>Fix:

>Release-Note:
>Audit-Trail:
>Unformatted:


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