This is the mail archive of the gcc-bugs@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/7476: constant variable tested in loop


>Number:         7476
>Category:       optimization
>Synopsis:       constant variable tested in loop
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    unassigned
>State:          open
>Class:          pessimizes-code
>Submitter-Id:   net
>Arrival-Date:   Fri Aug 02 16:06:01 PDT 2002
>Closed-Date:
>Last-Modified:
>Originator:     
>Release:        3.1.1 20020606 (Debian prerelease) (Debian testing/unstable)
>Organization:
>Environment:
System: Linux blah 2.2.15 #1 Tue Apr 25 17:13:48 EST 2000 i586 unknown
Architecture: i586
	<machine, os, target, libraries (multiple lines)>
host: i386-pc-linux-gnu
build: i386-pc-linux-gnu
target: i386-pc-linux-gnu
configured with: /mnt/data/gcc-3.1/gcc-3.1-3.1.1ds1/src/configure -v --enable-languages=c,c++,java,f77,proto,objc,ada --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-gxx-include-dir=/usr/include/g++-v3-3.1 --enable-shared --with-system-zlib --enable-long-long --enable-nls --without-included-gettext --enable-clocale=gnu --enable-threads=posix --enable-java-gc=boehm --enable-objc-gc i386-linux

>Description:
A constant variable tested within a loop doesn't seem to be
recognised as constant.

>How-To-Repeat:
The file foo.c below compiled with

	gcc-3.1 -O9 -S foo.c

produces the foo.s below.  Notice that it still contains a call to
"puts", despite the "cond" variable in the source always being false.

Outside a loop such a construct seems to be recognised as constant and
the dependent code goes dead, but within a loop that doesn't seem to
happen.

I'm not sure if it's too much to expect something like this in a loop
to go dead.  I guess in the presence of gotos or loops it's not always
too easy to be certain something really is constant.  A variable
written only once with a constant value like this might be tractable
though.

For what it's worth this arose within gmp where the "cond" condition
tested was sometimes a compile-time constant, or sometimes a runtime
test, depending on the configuration.  It had seemed like a good idea
just to put the result in a variable in either case, and let the
optimizer drop unused code in the constant case.



--=-=-=
Content-Type: text/x-csrc
Content-Disposition: attachment; filename=foo.c

void
foo (void)
{
  int  cond = 0;
  int  i;

  for (i = 0; i < 10; i++)
    if (cond)
      puts ("hello");
}

--=-=-=
Content-Disposition: attachment; filename=foo.s

	.file	"foo.c"
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"hello"
	.text
	.align 2
	.p2align 2,,3
.globl foo
	.type	foo,@function
foo:
	pushl	%ebp
	movl	%esp, %ebp
	pushl	%esi
	pushl	%ebx
	xorl	%esi, %esi
	movl	$9, %ebx
	.p2align 2,,3
.L7:
	testl	%esi, %esi
	jne	.L10
.L4:
	decl	%ebx
	jns	.L7
	leal	-8(%ebp), %esp
	popl	%ebx
	popl	%esi
	leave
	ret
	.p2align 2,,3
.L10:
	subl	$12, %esp
	pushl	$.LC0
	call	puts
	addl	$16, %esp
	jmp	.L4
.Lfe1:
	.size	foo,.Lfe1-foo
	.ident	"GCC: (GNU) 3.1.1 20020703 (Debian prerelease)"

--=-=-=--
>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]