From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 30892 invoked by alias); 3 Aug 2002 02:36:01 -0000 Mailing-List: contact gcc-prs-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Archive: List-Post: List-Help: Sender: gcc-prs-owner@gcc.gnu.org Received: (qmail 30878 invoked by uid 71); 3 Aug 2002 02:36:01 -0000 Date: Fri, 02 Aug 2002 19:36:00 -0000 Message-ID: <20020803023601.30877.qmail@sources.redhat.com> To: nobody@gcc.gnu.org Cc: gcc-prs@gcc.gnu.org, From: Daniel Berlin Subject: Re: optimization/7476: constant variable tested in loop Reply-To: Daniel Berlin X-SW-Source: 2002-08/txt/msg00046.txt.bz2 List-Id: The following reply was made to PR optimization/7476; it has been noted by GNATS. From: Daniel Berlin To: Kevin Ryde Cc: gcc-gnats@gcc.gnu.org, , , Subject: Re: optimization/7476: constant variable tested in loop Date: Fri, 2 Aug 2002 22:27:50 -0400 (EDT) Fixed by the local cprop stuff Jan added to the mainline. .file "testconst.c" .text .align 2 .p2align 4,,15 .globl foo .type foo, @function foo: pushl %ebp movl $9, %eax movl %esp, %ebp .p2align 4,,15 .L7: decl %eax jns .L7 popl %ebp ret .size foo, .-foo .ident "GCC: (GNU) 3.3 20020729 (experimental) On Sat, 3 Aug 2002, Kevin Ryde wrote: > > >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 > > 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: > --=-=-= > > >