public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug target/40603]  New: unnecessary conversion from unsigned byte load to signed byte load
@ 2009-07-01  6:56 carrot at google dot com
  2009-07-01  6:56 ` [Bug target/40603] " carrot at google dot com
                   ` (9 more replies)
  0 siblings, 10 replies; 11+ messages in thread
From: carrot at google dot com @ 2009-07-01  6:56 UTC (permalink / raw)
  To: gcc-bugs

Compile the following function with options -Os -mthumb -march=armv5te

int ldrb(unsigned char* p)
{
    if (p[8] <= 0x7F)
          return 2;
      else
            return 5;
}

Gcc generates following codes:

        push    {lr}
        mov     r3, #8
        ldrsb   r3, [r0, r3]
        mov     r0, #2
        cmp     r3, #0
        bge     .L2
        mov     r0, #5
.L2:
        @ sp needed for prologue
        pop     {pc}

The source code    if (p[8] <= 0x7F) is translated to:

        mov     r3, #8
        ldrsb   r3, [r0, r3]
        cmp     r3, #0

A better code sequence should be:

        ldrb    r3, [r0, 8]
        cmp     r3, 0x7F

This can save one instruction.

The tree dump shows in a very early pass (ldrb.c.003t.original) the comparison
was transformed to
       if ((signed char) *(p + 8) >= 0)

I guess gcc thinks comparing with 0 is much cheaper than comparing with other
numbers. Am I right?

Unfortunately in thumb mode, loading a signed byte costs more than loading an
unsigned byte and comparing with 0 has same cost as comparing with 0x7F.


-- 
           Summary: unnecessary conversion from unsigned byte load to signed
                    byte load
           Product: gcc
           Version: 4.5.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: target
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: carrot at google dot com
 GCC build triplet: i686-linux
  GCC host triplet: i686-linux
GCC target triplet: arm-eabi


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


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

end of thread, other threads:[~2010-04-16 11:23 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-07-01  6:56 [Bug target/40603] New: unnecessary conversion from unsigned byte load to signed byte load carrot at google dot com
2009-07-01  6:56 ` [Bug target/40603] " carrot at google dot com
2009-07-01  9:13 ` [Bug target/40603] New: " Ramana Radhakrishnan
2009-07-01  9:13 ` [Bug target/40603] " ramana dot radhakrishnan at arm dot com
2009-07-01 10:25 ` carrot at google dot com
2009-07-08 10:50 ` ramana at gcc dot gnu dot org
2009-07-22 21:22 ` rearnsha at gcc dot gnu dot org
2010-03-20 19:29 ` ramana at gcc dot gnu dot org
2010-03-26 12:01 ` bernds at codesourcery dot com
2010-04-16 10:05 ` bernds at gcc dot gnu dot org
2010-04-16 11:23 ` bernds 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).