Hi,
this code:

unsigned long f (unsigned char  *P)
{
  unsigned long C;
  C  = ((unsigned long)P[1] << 24)
     | ((unsigned long)P[2] << 16)
     | ((unsigned long)P[3] <<  8)
     | ((unsigned long)P[4] <<  0);
  return C;
}

compiles to this:

00000000 <f>:
   0:   f9 2f           mov     r31, r25
   2:   e8 2f           mov     r30, r24
   4:   61 81           ldd     r22, Z+1        ; 0x01
   6:   77 27           eor     r23, r23
   8:   88 27           eor     r24, r24
   a:   99 27           eor     r25, r25
   c:   96 2f           mov     r25, r22
   e:   88 27           eor     r24, r24
  10:   77 27           eor     r23, r23
  12:   66 27           eor     r22, r22
  14:   22 81           ldd     r18, Z+2        ; 0x02
  16:   33 27           eor     r19, r19
  18:   44 27           eor     r20, r20
  1a:   55 27           eor     r21, r21
  1c:   53 2f           mov     r21, r19
  1e:   42 2f           mov     r20, r18
  20:   33 27           eor     r19, r19
  22:   22 27           eor     r18, r18
  24:   62 2b           or      r22, r18
  26:   73 2b           or      r23, r19
  28:   84 2b           or      r24, r20
  2a:   95 2b           or      r25, r21
  2c:   24 81           ldd     r18, Z+4        ; 0x04
  2e:   33 27           eor     r19, r19
  30:   44 27           eor     r20, r20
  32:   55 27           eor     r21, r21
  34:   62 2b           or      r22, r18
  36:   73 2b           or      r23, r19
  38:   84 2b           or      r24, r20
  3a:   95 2b           or      r25, r21
  3c:   23 81           ldd     r18, Z+3        ; 0x03
  3e:   33 27           eor     r19, r19
  40:   44 27           eor     r20, r20
  42:   55 27           eor     r21, r21
  44:   54 2f           mov     r21, r20
  46:   43 2f           mov     r20, r19
  48:   32 2f           mov     r19, r18
  4a:   22 27           eor     r18, r18
  4c:   62 2b           or      r22, r18
  4e:   73 2b           or      r23, r19
  50:   84 2b           or      r24, r20
  52:   95 2b           or      r25, r21
  54:   08 95           ret

using this cmd line:
avr-gcc -c -Os f.c

IMO, most of the or, eor and mov instructions are unnecessary.


-- 
           Summary: missed-optimization transforming a byte array to
                    unsigned long
           Product: gcc
           Version: 4.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: target
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: berndtrog at yahoo dot com
GCC target triplet: avr


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

Reply via email to