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.

Compiler/SIMPLELINK-CC2640R2-SDK: C compiler optimization question

Part Number: SIMPLELINK-CC2640R2-SDK


Tool/software: TI C/C++ Compiler

I am having some C compiler optimization issues.

I am using Code Composer Studio Version: 7.0.0.00022

ARM Compiler Tools 16.9.0.LTS com.ti.cgt.tms470.15.win32.feature.group Texas Instruments

Here is the source code:

typedef uint64_t RF_EventMask;

#define RF_EventLastCmdDone (1<<1)
#define RF_EventRxEntryDone (1<<23)

#pragma FUNCTION_OPTIONS(ull_scanDoneCb, "--opt_level=1")
void ull_scanDoneCb(RF_Handle rfHandle, RF_CmdHandle cmdHandle, RF_EventMask events)
{
if (events & RF_EventRxEntryDone)
{
/* Radio has received a packet */
uble_buildAndPostEvt(UB_EVTDST_LL, ULL_EVT_SCAN_RX_SUCCESS, NULL, 0);
}
else if (events & RF_EventLastCmdDone)
{
...

The "events" should be either "RF_EventRxEntryDone" (0x00800000) or "RF_EventLastCmdDone | RF_EventRxEntryDone" (0x00800002) when the callback is called from SWI.

Here is the assembly for opt_level=0:

519 {
ull_scanDoneCb():
00006f6c: B580 push {r7, lr}
00006f6e: 4618 mov r0, r3
524 if (events & RF_EventRxEntryDone)
00006f70: F44F0100 mov.w r1, #0x800000
00006f74: 2300 movs r3, #0
00006f76: 4011 ands r1, r2
00006f78: EA030700 and.w r7, r3, r0
00006f7c: 2F00 cmp r7, #0
00006f7e: D100 bne $C$L7
00006f80: 2900 cmp r1, #0
$C$L7:
00006f82: D123 bne $C$L10
532 else if (events & RF_EventLastCmdDone)

When "opt_level=0", the code branches to $C$L10 as expected. R2 is either 0x00800000 or 0x00800002 before ull_scanDoneCb() is called.

Here is the assembly for opt_level=1:

519 {
ull_scanDoneCb():
00006d84: 4618 mov r0, r3
524 if (events & RF_EventRxEntryDone)
00006d86: F44F0100 mov.w r1, #0x800000
00006d8a: B580 push {r7, lr}
00006d8c: 2300 movs r3, #0
00006d8e: 4011 ands r1, r2
00006d90: EA030700 and.w r7, r3, r0
00006d94: 2F00 cmp r7, #0
00006d96: D100 bne $C$L7
00006d98: 2900 cmp r1, #0
$C$L7:
00006d9a: D123 bne $C$L10
532 else if (events & RF_EventLastCmdDone)

When "opt_level=1", it is not branched to $C$L10. Strangely, R2 is always 0x00000002 before ull_scanDoneCb() is called.

Setting the function level optimization seems to populate R2 differently before the function is called. Note that the same code without the #pragma works well in IAR (also in highest optimization setting). Let me know if you need any more info on this. Please advise ASAP.

  • Supertrainmegazord said:
    void ull_scanDoneCb(RF_Handle rfHandle, RF_CmdHandle cmdHandle, RF_EventMask events)

    Per the register conventions of the ARM compiler, events is passed in the register R2.  

    Supertrainmegazord said:
    When "opt_level=1", it is not branched to $C$L10. Strangely, R2 is always 0x00000002 before ull_scanDoneCb() is called.

    If R2 contains 0x00000002, then this if ...

    Supertrainmegazord said:
    if (events & RF_EventRxEntryDone)

    ... should not be taken.  So, you need to figure why R2 (events) has the unexpected value.

    Thanks and regards,

    -George

  • Hi George,

    Thanks for your quick response. But from my point of view, turn off the optimization in ull_scanDoneCb() would "fix" the problem. The optimization level in ull_scanDoneCb() somehow affects the R2 input. How is that possible? Note that the ull_scanDoneCb() is called from a library in the SDK. I am not sure how it was built or what optimization level was used.

    Can a mismatch optimization level in the library caused it? What steps should I try to prove it? I would like to avoid carrying the #pragma around or compile the entire module with no optimization.

    Regards,
    Chris
  • Supertrainmegazord said:
    The optimization level in ull_scanDoneCb() somehow affects the R2 input.

    Not directly.  It is likely that you have some problem present in your system all the time.  Building ull_scanDoneCb with different optimization levels causes the problem to appear or be hidden.

    Thanks and regards,

    -George