This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

TI-CGT: MSP430 compiler (21.6.0.LTS) produces redundant instructions for some left/right 8-bit shifts.

Part Number: TI-CGT

Hello everyone!

I have just checked the ASM output for the C source below and found out that in some cases where a type conversion from 16- to 8-bit integer or vice versa is performed, the compiler produces a redundant MOV.B instruction. This only seems to be the case when SWPB instruction is used on a register (not a memory location), but not always...

uint8_t shift_right_toU8(uint16_t val)
{
    return val >> 8U;
}

uint16_t shift_right_toU16(uint16_t val)
{
    return val >> 8U;
}

uint16_t shift_left_fromU8(uint8_t val)
{
    return val << 8U;
}

uint16_t shift_left_fromU16(uint16_t val)
{
    return val << 8U;
}

ASM output (stripped of additional info):

shift_right_toU8:
;* --------------------------------------------------------------------------*
        SWPB      r12                   ; [] |46| 
        MOV.B     r12,r12               ; [] |46| 
        MOV.B     r12,r12               ; [] |46| 

        RET       ; [] 

shift_right_toU16:
;* --------------------------------------------------------------------------*
        SWPB      r12                   ; [] |51| 
        MOV.B     r12,r12               ; [] |51| 

        RET       ; [] 

shift_left_fromU8:
;* --------------------------------------------------------------------------*
        MOV.B     r12,r12               ; [] |56| 
        MOV.B     r12,r12               ; [] |56| 
        SWPB      r12                   ; [] |56| 

        RET       ; [] 

shift_left_fromU16:
;* --------------------------------------------------------------------------*
        MOV.B     r12,r12               ; [] |61| 
        SWPB      r12                   ; [] |61| 

        RET       ; [] 
        
        

Cheers!

Michal