public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/35141]  New: ARM: Constant generation inside a loop: Missed optimization opportunity
@ 2008-02-08 17:07 alexandre dot nunes at gmail dot com
  2008-02-08 17:27 ` [Bug c/35141] " steven at gcc dot gnu dot org
                   ` (8 more replies)
  0 siblings, 9 replies; 10+ messages in thread
From: alexandre dot nunes at gmail dot com @ 2008-02-08 17:07 UTC (permalink / raw)
  To: gcc-bugs

On ARM, the following C code:

void whatever(const char *pqp)
{
  volatile unsigned int *uart_thr = (typeof(uart_thr))0xE000C000;
  unsigned int ch;
  while((ch = *pqp++))
    *uart_thr = ch;
}

Generates this assembler output (by means of -mcpu=arm7tdmi -O2):

whatever:
        @ args = 0, pretend = 0, frame = 0
        @ frame_needed = 0, uses_anonymous_args = 0
        @ link register save eliminated.
        ldrb    r2, [r0, #0]    @ zero_extendqisi2
        cmp     r2, #0
        @ lr needed for prologue
        bxeq    lr
.L4:
        mov     r3, #-536870912
        add     r3, r3, #49152
        str     r2, [r3, #0]
        ldrb    r2, [r0, #1]!   @ zero_extendqisi2
        cmp     r2, #0
        bne     .L4
        bx      lr

The relevant part is the bne .L4 ; since r3 is preserved across the loop, it
could optimize for speed without space penality by generating this instead:

.L4:
        mov     r3, #-536870912
        add     r3, r3, #49152
.L5:
        str     r2, [r3, #0]
        ldrb    r2, [r0, #1]!   @ zero_extendqisi2
        cmp     r2, #0
        bne     .L5
        bx      lr

... or, in other words, generating the constant only once, which saves at least
two cycles per iteration.


-- 
           Summary: ARM: Constant generation inside a loop: Missed
                    optimization opportunity
           Product: gcc
           Version: 4.2.3
            Status: UNCONFIRMED
          Severity: enhancement
          Priority: P3
         Component: c
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: alexandre dot nunes at gmail dot com
  GCC host triplet: i686-unknow-linux
GCC target triplet: arm-*-elf


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


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug c/35141] ARM: Constant generation inside a loop: Missed optimization opportunity
  2008-02-08 17:07 [Bug c/35141] New: ARM: Constant generation inside a loop: Missed optimization opportunity alexandre dot nunes at gmail dot com
@ 2008-02-08 17:27 ` steven at gcc dot gnu dot org
  2008-02-08 18:38 ` rguenth at gcc dot gnu dot org
                   ` (7 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: steven at gcc dot gnu dot org @ 2008-02-08 17:27 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #1 from steven at gcc dot gnu dot org  2008-02-08 17:27 -------
See PR31360.  May be fixed for GCC 4.3.


-- 


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


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug c/35141] ARM: Constant generation inside a loop: Missed optimization opportunity
  2008-02-08 17:07 [Bug c/35141] New: ARM: Constant generation inside a loop: Missed optimization opportunity alexandre dot nunes at gmail dot com
  2008-02-08 17:27 ` [Bug c/35141] " steven at gcc dot gnu dot org
@ 2008-02-08 18:38 ` rguenth at gcc dot gnu dot org
  2008-02-08 20:49 ` [Bug middle-end/35141] " alexandre dot nunes at gmail dot com
                   ` (6 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: rguenth at gcc dot gnu dot org @ 2008-02-08 18:38 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #2 from rguenth at gcc dot gnu dot org  2008-02-08 18:37 -------
Also using a volatile pointer may prevent optimization, so don't use it if
not strictly needed (or at least don't expect optimized code).

Can you try 4.3 as suggested?


-- 

rguenth at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |WAITING


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


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug middle-end/35141] ARM: Constant generation inside a loop: Missed optimization opportunity
  2008-02-08 17:07 [Bug c/35141] New: ARM: Constant generation inside a loop: Missed optimization opportunity alexandre dot nunes at gmail dot com
  2008-02-08 17:27 ` [Bug c/35141] " steven at gcc dot gnu dot org
  2008-02-08 18:38 ` rguenth at gcc dot gnu dot org
@ 2008-02-08 20:49 ` alexandre dot nunes at gmail dot com
  2008-02-11 21:11 ` alexandre dot nunes at gmail dot com
                   ` (5 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: alexandre dot nunes at gmail dot com @ 2008-02-08 20:49 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #3 from alexandre dot nunes at gmail dot com  2008-02-08 20:48 -------
(In reply to comment #2)
> Also using a volatile pointer may prevent optimization, so don't use it if
> not strictly needed (or at least don't expect optimized code).
> 

Sorry for lefting it in there: Tought the above code snippet was from real code
that writes to a hardware register, the compiler generates exactly the same
output with or without the volatile, thus that's irrelevant for this bug
report. 

I hope I don't confuse testing on future compiler versions, where it may end up
 making any difference. 

> Can you try 4.3 as suggested?
> 

Yes, if/when I can get it to compile. I'll post back in a few days.


-- 


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


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug middle-end/35141] ARM: Constant generation inside a loop: Missed optimization opportunity
  2008-02-08 17:07 [Bug c/35141] New: ARM: Constant generation inside a loop: Missed optimization opportunity alexandre dot nunes at gmail dot com
                   ` (2 preceding siblings ...)
  2008-02-08 20:49 ` [Bug middle-end/35141] " alexandre dot nunes at gmail dot com
@ 2008-02-11 21:11 ` alexandre dot nunes at gmail dot com
  2008-02-11 21:32 ` alexandre dot nunes at gmail dot com
                   ` (4 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: alexandre dot nunes at gmail dot com @ 2008-02-11 21:11 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #4 from alexandre dot nunes at gmail dot com  2008-02-11 21:10 -------
(In reply to comment #2)
> Also using a volatile pointer may prevent optimization, so don't use it if
> not strictly needed (or at least don't expect optimized code).
> 
> Can you try 4.3 as suggested?
> 

Ok, PR35071 was the only blocker. I did a bad thing in order to bypass it (it
only affected a non-default [for me] target on my multilib config) and get the
whole thing to compile, and the result is:


whatever:
        @ args = 0, pretend = 0, frame = 0
        @ frame_needed = 0, uses_anonymous_args = 0
        @ link register save eliminated.
        ldrb    r3, [r0, #0]    @ zero_extendqisi2
        cmp     r3, #0
        bxeq    lr
        mov     r2, #-536870912
        add     r2, r2, #49152
.L3:
        str     r3, [r2, #0]
        ldrb    r3, [r0, #1]!   @ zero_extendqisi2
        cmp     r3, #0
        bne     .L3
        bx      lr


... which seems correct to me. (my build was from svn trunk, current date
20080211, unknow revision number [my build system got rid of .svn subdirs]).


No chance of a 4.2.x backport ? :-)


-- 


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


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug middle-end/35141] ARM: Constant generation inside a loop: Missed optimization opportunity
  2008-02-08 17:07 [Bug c/35141] New: ARM: Constant generation inside a loop: Missed optimization opportunity alexandre dot nunes at gmail dot com
                   ` (3 preceding siblings ...)
  2008-02-11 21:11 ` alexandre dot nunes at gmail dot com
@ 2008-02-11 21:32 ` alexandre dot nunes at gmail dot com
  2008-02-11 22:21 ` ubizjak at gmail dot com
                   ` (3 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: alexandre dot nunes at gmail dot com @ 2008-02-11 21:32 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #5 from alexandre dot nunes at gmail dot com  2008-02-11 21:31 -------
I tested on i686 (-march=athlon-xp -O2) with gcc 4.3 revision 132072 (older
than the one I tried at arm), and it seems to misbehave there.

I'm not sure tought of the implications, since that's a superscalar cpu and
these things are too complicated to my mind, perhaps it's faster that way
because of an eventual alignment, etc... But it seems buggy to me :-)


-- 


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


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug middle-end/35141] ARM: Constant generation inside a loop: Missed optimization opportunity
  2008-02-08 17:07 [Bug c/35141] New: ARM: Constant generation inside a loop: Missed optimization opportunity alexandre dot nunes at gmail dot com
                   ` (4 preceding siblings ...)
  2008-02-11 21:32 ` alexandre dot nunes at gmail dot com
@ 2008-02-11 22:21 ` ubizjak at gmail dot com
  2008-02-12  0:46 ` alexandre dot nunes at gmail dot com
                   ` (2 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: ubizjak at gmail dot com @ 2008-02-11 22:21 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #6 from ubizjak at gmail dot com  2008-02-11 22:20 -------
(In reply to comment #5)
> I tested on i686 (-march=athlon-xp -O2) with gcc 4.3 revision 132072 (older
> than the one I tried at arm), and it seems to misbehave there.
> 
> I'm not sure tought of the implications, since that's a superscalar cpu and
> these things are too complicated to my mind, perhaps it's faster that way
> because of an eventual alignment, etc... But it seems buggy to me :-)

It is not what you think it is. ;)

movl    %edx, -536821760

means:

(set (mem:SI (const_int -536821760 [0xffffffffe000c000]))(reg/v:SI 1 dx))

IOW, put %edx to constant address.


-- 


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


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug middle-end/35141] ARM: Constant generation inside a loop: Missed optimization opportunity
  2008-02-08 17:07 [Bug c/35141] New: ARM: Constant generation inside a loop: Missed optimization opportunity alexandre dot nunes at gmail dot com
                   ` (5 preceding siblings ...)
  2008-02-11 22:21 ` ubizjak at gmail dot com
@ 2008-02-12  0:46 ` alexandre dot nunes at gmail dot com
  2009-04-19 21:41 ` ramana at gcc dot gnu dot org
  2009-06-24  7:45 ` steven at gcc dot gnu dot org
  8 siblings, 0 replies; 10+ messages in thread
From: alexandre dot nunes at gmail dot com @ 2008-02-12  0:46 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #7 from alexandre dot nunes at gmail dot com  2008-02-12 00:45 -------
> It is not what you think it is. ;)
> 
> movl    %edx, -536821760
> 
> means:
> 
> (set (mem:SI (const_int -536821760 [0xffffffffe000c000]))(reg/v:SI 1 dx))
> 
> IOW, put %edx to constant address.
> 

It actually makes sense, this arch has no spare registers to play with :-)


-- 


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


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug middle-end/35141] ARM: Constant generation inside a loop: Missed optimization opportunity
  2008-02-08 17:07 [Bug c/35141] New: ARM: Constant generation inside a loop: Missed optimization opportunity alexandre dot nunes at gmail dot com
                   ` (6 preceding siblings ...)
  2008-02-12  0:46 ` alexandre dot nunes at gmail dot com
@ 2009-04-19 21:41 ` ramana at gcc dot gnu dot org
  2009-06-24  7:45 ` steven at gcc dot gnu dot org
  8 siblings, 0 replies; 10+ messages in thread
From: ramana at gcc dot gnu dot org @ 2009-04-19 21:41 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #8 from ramana at gcc dot gnu dot org  2009-04-19 21:41 -------
This appears to be fixed on all release branches. This probably should now be
closed.


-- 


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


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug middle-end/35141] ARM: Constant generation inside a loop: Missed optimization opportunity
  2008-02-08 17:07 [Bug c/35141] New: ARM: Constant generation inside a loop: Missed optimization opportunity alexandre dot nunes at gmail dot com
                   ` (7 preceding siblings ...)
  2009-04-19 21:41 ` ramana at gcc dot gnu dot org
@ 2009-06-24  7:45 ` steven at gcc dot gnu dot org
  8 siblings, 0 replies; 10+ messages in thread
From: steven at gcc dot gnu dot org @ 2009-06-24  7:45 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #9 from steven at gcc dot gnu dot org  2009-06-24 07:45 -------
I agree with Comment #8


-- 

steven at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|WAITING                     |RESOLVED
         Resolution|                            |FIXED


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


^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2009-06-24  7:45 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-02-08 17:07 [Bug c/35141] New: ARM: Constant generation inside a loop: Missed optimization opportunity alexandre dot nunes at gmail dot com
2008-02-08 17:27 ` [Bug c/35141] " steven at gcc dot gnu dot org
2008-02-08 18:38 ` rguenth at gcc dot gnu dot org
2008-02-08 20:49 ` [Bug middle-end/35141] " alexandre dot nunes at gmail dot com
2008-02-11 21:11 ` alexandre dot nunes at gmail dot com
2008-02-11 21:32 ` alexandre dot nunes at gmail dot com
2008-02-11 22:21 ` ubizjak at gmail dot com
2008-02-12  0:46 ` alexandre dot nunes at gmail dot com
2009-04-19 21:41 ` ramana at gcc dot gnu dot org
2009-06-24  7:45 ` steven at gcc dot gnu dot org

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).