From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 21254 invoked by alias); 2 Aug 2002 23:06:02 -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 21220 invoked by uid 71); 2 Aug 2002 23:06:01 -0000 Resent-Date: 2 Aug 2002 23:06:01 -0000 Resent-Message-ID: <20020802230601.21219.qmail@sources.redhat.com> Resent-From: gcc-gnats@gcc.gnu.org (GNATS Filer) Resent-To: nobody@gcc.gnu.org Resent-Cc: gcc-prs@gcc.gnu.org, gcc-bugs@gcc.gnu.org Resent-Reply-To: gcc-gnats@gcc.gnu.org, Kevin Ryde Received: (qmail 18606 invoked from network); 2 Aug 2002 23:00:53 -0000 Received: from unknown (HELO sunny.pacific.net.au) (203.25.148.40) by sources.redhat.com with SMTP; 2 Aug 2002 23:00:53 -0000 Received: from wisma.pacific.net.au (wisma.pacific.net.au [210.23.129.72]) by sunny.pacific.net.au with ESMTP id g72N0qSs017289 for ; Sat, 3 Aug 2002 09:00:52 +1000 (EST) Received: from localhost (ppp91.dyn228.pacific.net.au [203.143.228.91]) by wisma.pacific.net.au with ESMTP id JAA06324 for ; Sat, 3 Aug 2002 09:00:49 +1000 (EST) Received: from gg by localhost with local (Exim 3.31 #1 (Debian)) id 17alOO-00021r-00; Sat, 03 Aug 2002 08:59:32 +1000 Message-Id: <87eldgeujv.fsf@zip.com.au> Date: Fri, 02 Aug 2002 16:06:00 -0000 From: Kevin Ryde To: gcc-gnats@gcc.gnu.org Subject: optimization/7476: constant variable tested in loop X-SW-Source: 2002-08/txt/msg00043.txt.bz2 List-Id: >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: --=-=-=