public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "wvangulik at xs4all dot nl" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug middle-end/33970] Missed optimization using unsigned char loop variable
Date: Tue, 06 Nov 2007 19:35:00 -0000	[thread overview]
Message-ID: <20071106193459.613.qmail@sourceware.org> (raw)
In-Reply-To: <bug-33970-15294@http.gcc.gnu.org/bugzilla/>



------- Comment #10 from wvangulik at xs4all dot nl  2007-11-06 19:34 -------
(In reply to comment #9)
> 
> I think you will also find that if x is changed from ststic to auto the same
> problem appears.
> 

Ok, I tried to find the minimum test case.
And it has nothing todo with static/volatile/inline etc.

==============================================
int sub2(unsigned char); // external function

void foo(void) {
  unsigned char x;
  for(x=0;x<128; x++)
  {
    //sub2(x); //x is becomes a int (16bit)
    sub2(x+1); //x is char (8bit)
  }
}

All is compiled using 4.1.2 and  "avr-gcc -Wall -Os -mmcu=avr5 -S test.c"

The output when compiling with "sub2(x)"
=================================================
/* prologue: frame size=0 */
        push r28
        push r29
/* prologue end (size=2) */
        ldi r28,lo8(0)
        ldi r29,hi8(0)
.L2:
        mov r24,r28 ; << loads as a byte!
        call sub2
        adiw r28,1           ; << increment as a int
        cpi r28,128          ; << compare as a int
        cpc r29,__zero_reg__
        brne .L2
/* epilogue: frame size=0 */
        pop r29
        pop r28
        ret

The output when compiling with "sub2(x+1)"
================================================================
/* prologue: frame size=0 */
        push r17
/* prologue end (size=1) */
        ldi r17,lo8(0)
.L2:
        subi r17,lo8(-(1))
        mov r24,r17
        call sub2
        cpi r17,lo8(-128)
        brne .L2
/* epilogue: frame size=0 */
        pop r17
        ret
===========================================================


>From compiling with x as a int I got a sort of hint. When using x+1, x is
actually loaded with 1 (and not zero). 

So i tried:
======================================================
void foo(void) {
  unsigned char x;
  for(x=1;x<129; x++)
    sub2(x);
}


And it gives the assembler loop check as for the "sub(x+1)" variant of course
the loop is now effectivley the same as "sub2(x+1)". But the the incrementing
of x is now done after the call, so it proofs that this is not the problem. I
also tried different loop lengths, thinking 128 is the nasty one, but it did
not help.

======================================================
/* prologue: frame size=0 */
        push r17
/* prologue end (size=1) */
        ldi r17,lo8(1)
.L2:
        mov r24,r17
        call sub2
        subi r17,lo8(-(1))
        cpi r17,lo8(-127)
        brne .L2
/* epilogue: frame size=0 */
        pop r17
        ret


I am out of knowledge now, I do not know how to further debug GCC on this. Just
hoping this will help someone to pinpoint the problem.


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=33970


  parent reply	other threads:[~2007-11-06 19:35 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-11-01 13:44 [Bug c/33970] New: " henning dot m at insightbb dot com
2007-11-01 14:10 ` [Bug c/33970] " rguenth at gcc dot gnu dot org
2007-11-01 17:09 ` [Bug middle-end/33970] " pinskia at gcc dot gnu dot org
2007-11-01 17:28 ` eweddington at cso dot atmel dot com
2007-11-01 17:45 ` eweddington at cso dot atmel dot com
2007-11-01 17:47 ` eweddington at cso dot atmel dot com
2007-11-01 21:26 ` henning dot m at insightbb dot com
2007-11-04 23:28 ` eweddington at cso dot atmel dot com
2007-11-04 23:28 ` eweddington at cso dot atmel dot com
2007-11-05 22:48 ` wvangulik at xs4all dot nl
2007-11-06 12:37 ` henning dot m at insightbb dot com
2007-11-06 19:35 ` wvangulik at xs4all dot nl [this message]
2007-11-06 21:01 ` wvangulik at xs4all dot nl
2010-09-13 12:09 ` abnikant dot singh at atmel dot com
     [not found] <bug-33970-4@http.gcc.gnu.org/bugzilla/>
2011-07-20 17:48 ` gjl at gcc dot gnu.org

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=20071106193459.613.qmail@sourceware.org \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@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).