Bug 2960

Summary: Duplicate loop conditions even with -Os
Product: gcc Reporter: 94701-quiet
Component: rtl-optimizationAssignee: Richard Henderson <rth>
Status: RESOLVED FIXED    
Severity: normal CC: gcc-bugs, herbert
Priority: P3 Keywords: missed-optimization
Version: 3.0   
Target Milestone: ---   
Host: i386-pc-linux-gnu Target: i386-pc-linux-gnu
Build: i386-pc-linux-gnu Known to work:
Known to fail: Last reconfirmed:

Description 94701-quiet 2001-05-26 11:36:00 UTC
For the following program,

int k;

extern void b();

int a(int i) {
	while (k < i) {
		b();
	}
}

GCC generates this on i386 (gcc -S -Os),

a:
	pushl	%ebp
	movl	%esp, %ebp
	pushl	%ebx
	pushl	%ecx
	movl	8(%ebp), %ebx
	cmpl	%ebx, k
	jge	.L7
	.p2align 2
.L5:
	call	b
	cmpl	%ebx, k
	jl	.L5
.L7:
	popl	%edx
	popl	%ebx
	popl	%ebp
	ret

I don't see the reason why it isn't doing:

	.p2align 2
.L5:
	cmpl	%ebx, k
	jge	.L7
	call	b
	jmp	.L5
.L7:

or

	jmp	.L7
	.p2align 2
.L5:
	call	b
.L7:
	cmpl	%ebx, k
	jl	.L5

Release:
3.0 20010526 (Debian prerelease) (Debian testing/unstable)

Environment:
System: Linux smile 2.2.17 #1 Sun Oct 8 19:26:41 MEST 2000 i686 unknown
Architecture: i686

	
host: i386-pc-linux-gnu
build: i386-pc-linux-gnu
target: i386-pc-linux-gnu
configured with: ../src/configure -v --enable-languages=c,c++,java,f77,proto,objc --prefix=/usr --infodir=/share/info --mandir=/share/man --enable-shared --with-gnu-as --with-gnu-ld --with-system-zlib --enable-long-long --enable-nls --without-x --without-included-gettext --disable-checking --enable-threads=posix --enable-java-gc=boehm --with-cpp-install-dir=bin --enable-objc-gc i386-linux
Comment 1 Richard Henderson 2002-10-06 20:02:57 UTC
Responsible-Changed-From-To: unassigned->rth
Responsible-Changed-Why: .
Comment 2 Richard Henderson 2002-10-06 20:02:57 UTC
State-Changed-From-To: open->closed
State-Changed-Why: http://gcc.gnu.org/ml/gcc-patches/2002-10/msg00338.html
Comment 3 Richard Henderson 2002-10-07 03:01:42 UTC
From: rth@gcc.gnu.org
To: gcc-gnats@gcc.gnu.org
Cc:  
Subject: optimization/2960
Date: 7 Oct 2002 03:01:42 -0000

 CVSROOT:	/cvs/gcc
 Module name:	gcc
 Changes by:	rth@gcc.gnu.org	2002-10-06 20:01:42
 
 Modified files:
 	gcc            : ChangeLog toplev.c 
 
 Log message:
 	PR optimization/2960
 	* toplev.c (rest_of_compilation): Don't copy_loop_headers if
 	optimize_size.
 
 Patches:
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/ChangeLog.diff?cvsroot=gcc&r1=1.15636&r2=1.15637
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/toplev.c.diff?cvsroot=gcc&r1=1.678&r2=1.679
Comment 4 Debian GCC Maintainers 2003-08-10 15:33:57 UTC
Herbert Xu writes:

Although the upstream fix certainly seems to have corrected the 
problem, gcc-3.3 -Os from unstable still generates basically the same 
code with the dupliate test of %ebx against k: 
 
a: 
        pushl   %ebp 
        movl    %esp, %ebp 
        pushl   %ebx 
        movl    8(%ebp), %ebx 
        cmpl    %ebx, k 
        jge     .L7 
.L5: 
        call    b 
        cmpl    %ebx, k 
        jl      .L5 
.L7: 
        popl    %ebx 
        leave 
        ret 
        .size   a, .-a 
        .comm   k,4,4 
        .ident  "GCC: (GNU) 3.3 (Debian)" 
--  
Comment 5 Andrew Pinski 2003-08-10 15:41:58 UTC
This is already fixed on the mainline:
a:
        pushl   %ebp
        movl    %esp, %ebp
        pushl   %ebx
        movl    8(%ebp), %ebx
.L7:
        cmpl    %ebx, k
        jge     .L6
        call    b
        jmp     .L7
.L6:
        popl    %ebx
        leave
        ret