public inbox for gcc-prs@sourceware.org
help / color / mirror / Atom feed
From: Kevin Ryde <user42@zip.com.au>
To: gcc-gnats@gcc.gnu.org
Subject: optimization/7476: constant variable tested in loop
Date: Fri, 02 Aug 2002 16:06:00 -0000	[thread overview]
Message-ID: <87eldgeujv.fsf@zip.com.au> (raw)


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


             reply	other threads:[~2002-08-02 23:06 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-08-02 16:06 Kevin Ryde [this message]
2002-08-02 16:16 Andrew Pinski
2002-08-02 19:36 Daniel Berlin
2002-12-06 13:35 bangerth

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87eldgeujv.fsf@zip.com.au \
    --to=user42@zip.com.au \
    --cc=gcc-gnats@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).