User account creation filtered due to spam.

Bug 6984 - wrong code generated with -O2, -O3, -Os for do-while loop on PowerPC
Summary: wrong code generated with -O2, -O3, -Os for do-while loop on PowerPC
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: rtl-optimization (show other bugs)
Version: 3.1
: P2 critical
Target Milestone: ---
Assignee: Alan Modra
URL:
Keywords: wrong-code
Depends on:
Blocks:
 
Reported: 2002-06-10 08:26 UTC by fshvaige
Modified: 2003-07-25 17:33 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description fshvaige 2002-06-10 08:26:01 UTC
File test1.c:

void f1 (unsigned n) {
        do {
        } while (--n);
}
void f2 (unsigned n) {
l1:
        if (--n) goto l1;
}

When compiled with:
ppc-eabi-gcc -v -save-temps -c -O2 -Wall test1.c

Produces wrong-large-ugly code for function f1().
The same for -O3 and -Os.
For -O1 all Ok.

Note: code for function f2() always Ok.

Release:
3.1

Environment:
../gcc-3.1/configure --with-gcc-version-trigger=/home/install/gcc-3.1/gcc/version.c --host=i686-pc-linux-gnu --with-newlib --enable-target-optspace --target=ppc-eabi --prefix=/home/crossGCC/ppc-eabi --with-local-prefix=/home/crossGCC/ppc-eabi --program-prefix=ppc-eabi- --enable-languages=c,c++ -v --norecursion

How-To-Repeat:
compile test1.c:

void f1 (unsigned n) {
        do {
        } while (--n);
}
void f2 (unsigned n) {
l1:
        if (--n) goto l1;
}

with command line like:
ppc-eabi-gcc -v -save-temps -c -O2 -Wall test1.c

and see file test1.s:
        .file   "test1.c"
        .section        ".text"
        .align 2
        .globl f1
        .type   f1,@function
f1:
        cmpwi 0,3,0
        bne- 0,.L7
        li 3,1
.L7:
        mtctr 3
.L6:
        bdnz .L6
        blr
.Lfe1:
        .size   f1,.Lfe1-f1
        .align 2
        .globl f2
        .type   f2,@function
f2:
        mtctr 3
.L9:
        bdnz .L9
        blr
.Lfe2:
        .size   f2,.Lfe2-f2
        .ident  "GCC: (GNU) 3.1"

Code for f1() must be the same as for f2().
Comment 1 Alan Modra 2002-06-17 17:00:07 UTC
From: Alan Modra <amodra@bigpond.net.au>
To: fshvaige@cisco.com
Cc: gcc-gnats@gcc.gnu.org
Subject: Re: optimization/6984: wrong code generated with -O2, -O3, -Os for do-while loop on PowerPC
Date: Mon, 17 Jun 2002 17:00:07 +0930

 Hmm, we get the same sort of bogus code on x86 with -march=k6.
 
 The code that this patch disables looks bogus to me.  Perhaps it's
 relevant when doloop_start is defined??  Anyway this cures your
 testcase on ppc.
 
 Index: gcc/doloop.c
 ===================================================================
 RCS file: /cvs/gcc/gcc/gcc/doloop.c,v
 retrieving revision 1.18
 diff -u -p -r1.18 doloop.c
 --- gcc/doloop.c	11 Jun 2002 12:21:52 -0000	1.18
 +++ gcc/doloop.c	17 Jun 2002 07:23:22 -0000
 @@ -720,7 +720,7 @@ doloop_modify_runtime (loop, iterations_
       not executed before the start of the loop.  We need to determine
       if the loop will terminate after the first pass and to limit the
       iteration count to one if necessary.  */
 -  if (! loop->vtop)
 +  if (0 && ! loop->vtop)
      {
        rtx label;
  
 -- 
 Alan Modra
 IBM OzLabs - Linux Technology Centre

Comment 2 Alan Modra 2002-06-18 00:17:42 UTC
From: Alan Modra <amodra@bigpond.net.au>
To: fshvaige@cisco.com, gcc-gnats@gcc.gnu.org
Cc:  
Subject: Re: optimization/6984: wrong code generated with -O2, -O3, -Os for do-while loop on PowerPC
Date: Tue, 18 Jun 2002 00:17:42 +0930

 Except that disabling that causes failures on powerpc-linux for
 loop-3c and loop-4b.
 
 The following patch passes bootstrap and regression testing on
 powerpc-linux, and I believe is correct.  However, I'm not completely
 happy with it, as this whole chunk of code dealing with do-while loops
 looks like it's just covering up errors in previous calculation of
 "iterations".
 
 	* doloop.c (doloop_modify_runtime <do-while>): Compare against
 	incremented initial value.
 
 Index: gcc/doloop.c
 ===================================================================
 RCS file: /cvs/gcc/gcc/gcc/doloop.c,v
 retrieving revision 1.18
 diff -u -p -r1.18 doloop.c
 --- gcc/doloop.c	11 Jun 2002 12:21:52 -0000	1.18
 +++ gcc/doloop.c	17 Jun 2002 14:30:01 -0000
 @@ -722,8 +722,6 @@ doloop_modify_runtime (loop, iterations_
       iteration count to one if necessary.  */
    if (! loop->vtop)
      {
 -      rtx label;
 -
        if (loop_dump_stream)
  	fprintf (loop_dump_stream, "Doloop: Do-while loop.\n");
  
 @@ -733,10 +731,16 @@ doloop_modify_runtime (loop, iterations_
  	 is guaranteed to execute at least once.  */
        if (loop_info->unroll_number == 1)
  	{
 +	  rtx label;
 +	  rtx after_one;
 +
  	  /*  Emit insns to test if the loop will immediately
  	      terminate and to set the iteration count to 1 if true.  */
  	  label = gen_label_rtx();
 -	  emit_cmp_and_jump_insns (copy_rtx (initial_value),
 +	  after_one = expand_simple_binop (mode, PLUS,
 +					   initial_value, increment, NULL_RTX,
 +					   unsigned_p, OPTAB_LIB_WIDEN);
 +	  emit_cmp_and_jump_insns (after_one,
  				   copy_rtx (loop_info->comparison_value),
  				   comparison_code, NULL_RTX, mode, 0,
  				   label);
 
 -- 
 Alan Modra
 IBM OzLabs - Linux Technology Centre

Comment 3 Alan Modra 2002-06-24 12:02:42 UTC
From: Alan Modra <amodra@bigpond.net.au>
To: gcc-gnats@gcc.gnu.org, gcc-prs@gcc.gnu.org, fshvaige@cisco.com,
  gcc-bugs@gcc.gnu.org, amodra@gcc.gnu.org
Cc:  
Subject: Re: optimization/6984: wrong code generated with -O2, -O3, -Os for do-while loop on PowerPC
Date: Mon, 24 Jun 2002 12:02:42 +0930

 Fixed with http://gcc.gnu.org/ml/gcc-patches/2002-06/msg01487.html
 PR closed.

Comment 4 Alan Modra 2002-09-17 03:25:07 UTC
From: amodra@gcc.gnu.org
To: gcc-gnats@gcc.gnu.org
Cc:  
Subject: optimization/6984
Date: 17 Sep 2002 03:25:07 -0000

 CVSROOT:	/cvs/gcc
 Module name:	gcc
 Branch: 	gcc-3_2-branch
 Changes by:	amodra@gcc.gnu.org	2002-09-16 20:25:07
 
 Modified files:
 	gcc            : ChangeLog doloop.c loop.h unroll.c 
 
 Log message:
 	Merge from mainline.
 	2002-07-20  Alan Modra  <amodra@bigpond.net.au>
 	PR optimization/7130
 	* loop.h (struct loop_info): Add "preconditioned".
 	* unroll.c (unroll_loop): Set it.
 	* doloop.c (doloop_modify_runtime): Correct count for unrolled loops.
 	
 	2002-06-24  Alan Modra  <amodra@bigpond.net.au>
 	PR optimization/6984
 	* doloop.c (doloop_valid_p): Correct comment.
 	(doloop_modify_runtime <abs_inc != 1>): Simplify.
 	(doloop_modify_runtime <do-while>): Don't emit code when NE.
 
 Patches:
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/ChangeLog.diff?cvsroot=gcc&only_with_tag=gcc-3_2-branch&r1=1.13152.2.657.2.48&r2=1.13152.2.657.2.49
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/doloop.c.diff?cvsroot=gcc&only_with_tag=gcc-3_2-branch&r1=1.16&r2=1.16.8.1
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/loop.h.diff?cvsroot=gcc&only_with_tag=gcc-3_2-branch&r1=1.58.6.1&r2=1.58.6.1.4.1
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/unroll.c.diff?cvsroot=gcc&only_with_tag=gcc-3_2-branch&r1=1.160.2.4.2.1&r2=1.160.2.4.2.2