Lost the return value of assembly code after optimization

OP #1353274
Rate this post
useful
not useful
Hello,

I made a program mixed with C and asm by GCC.  It works fine if no 
optimization but run away after optimization with switch "-Os".

After traced code, I found asm code didn't return r0 value back to C 
routine....

**********My C code**********

    :
  volatile struct iphdr *ip_hdr;
    :
    :
    :
  ip_hdr->check = ip_fast_csum((unsigned char *)ip_hdr,ip_hdr->ihl);
    :
    :

**********My ASM code**********

    .global  ip_fast_csum
    .text
ip_fast_csum:
    ldr    r3, [r0], #4
    ldr    r4, [r0], #4
    sub    r1, r1, #5
    adds  r3, r3, r4
    ldr    r4, [r0], #4
    adcs  r3, r3, r4
    ldr    r4, [r0], #4
    adcs  r3, r3, r4
_l:    ldr    r4, [r0], #4
    adcs  r3, r3, r4
    tst    r1, #15
    subne  r1, r1, #1
    bne    _l
    adc    r3, r3, #0
    adds  r3, r3, r3, lsl #16
    addcs  r3, r3, #0x10000
    mvn    r3, r3
    mov    r3, r3, lsr #16
    mov    r0,  r3
    mov    pc, lr

I found the result of ip_hdr->check always zero although the content of 
r0 is correct at the end of assembly?

How can I control the optimization to keep r0 return correctly to 
ip_hdr->check?

BRs.

Tim
#1354276
Rate this post
useful
not useful
Tim Choi wrote:
> How can I control the optimization to keep r0 return correctly to
> ip_hdr->check?

That was wrong-thinking there. If your code breaks when it is optimised, 
it is most likely that your code was flawed in any case, but that 
without optimisation the error was benign.

Th optimiser may reasonably expect your code to comply with the calling 
convention and will then use available resources with that assumption.

Reply

Please log in before posting.

or

Log in with Google account

Registration is free and takes only a minute.

Register now