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.

C2000, cgtools 6.2.5: loop optimized away?



Compiling the following code

volatile unsigned lim;

void for_loop_u(void) {
  unsigned i;
  for (i=0; i<lim; i++);
}

void for_loop_i(void) {
  int i;
  for (i=0; i<lim; i++);
}

with essentially the following options  -k -q -pdr -pdv -pden -pds880 -pds238 -o2 ... -d"LARGE_MODEL" -d"__TMS320F28XX__"  ...  -mi -ml -v28 --float_support=fpu32 --gen_func_subsections -@"../Debug.lkf"
I get assembly output:


;***************************************************************
;* FNAME: _for_loop_u                   FR SIZE:   0           *
;*                                                             *
;* FUNCTION ENVIRONMENT                                        *
;*                                                             *
;* FUNCTION PROPERTIES                                         *
;*                            0 Parameter,  0 Auto,  0 SOE     *
;***************************************************************

_for_loop_u:
    .dwcfi    cfa_offset, -2
    .dwcfi    save_reg_to_mem, 26, 0
    .dwcfi    save_reg_to_reg, 78, 26
        MOVW      DP,#_lim              ; [CPU_U]
    .dwpsn    file "for_loop.c",line 5,column 13,is_stmt
        MOV       AL,@_lim              ; [CPU_] |5|
        BF        $C$L1,EQ              ; [CPU_] |5|
        ; branchcc occurs ; [] |5|
        MOV       AL,@_lim              ; [CPU_] |5|
        MOV       AL,@_lim              ; [CPU_] |5|
$C$L1:    
$C$DW$3    .dwtag  DW_TAG_TI_branch
    .dwattr $C$DW$3, DW_AT_low_pc(0x00)
    .dwattr $C$DW$3, DW_AT_TI_return
        LRETR     ; [CPU_]
        ; return occurs ; []
    .dwattr $C$DW$2, DW_AT_TI_end_file("for_loop.c")
    .dwattr $C$DW$2, DW_AT_TI_end_line(0x06)
    .dwattr $C$DW$2, DW_AT_TI_end_column(0x01)
    .dwendentry
    .dwendtag $C$DW$2

    .sect    ".text:_for_loop_i"
    .clink
    .global    _for_loop_i

$C$DW$4    .dwtag  DW_TAG_subprogram, DW_AT_name("for_loop_i")
    .dwattr $C$DW$4, DW_AT_low_pc(_for_loop_i)
    .dwattr $C$DW$4, DW_AT_high_pc(0x00)
    .dwattr $C$DW$4, DW_AT_TI_symbol_name("_for_loop_i")
    .dwattr $C$DW$4, DW_AT_external
    .dwattr $C$DW$4, DW_AT_TI_begin_file("for_loop.c")
    .dwattr $C$DW$4, DW_AT_TI_begin_line(0x08)
    .dwattr $C$DW$4, DW_AT_TI_begin_column(0x06)
    .dwattr $C$DW$4, DW_AT_TI_max_frame_size(-2)
    .dwpsn    file "for_loop.c",line 8,column 23,is_stmt,address _for_loop_i

    .dwfde $C$DW$CIE, _for_loop_i

;***************************************************************
;* FNAME: _for_loop_i                   FR SIZE:   0           *
;*                                                             *
;* FUNCTION ENVIRONMENT                                        *
;*                                                             *
;* FUNCTION PROPERTIES                                         *
;*                            0 Parameter,  0 Auto,  0 SOE     *
;***************************************************************

_for_loop_i:
;* AL    assigned to _i
$C$DW$5    .dwtag  DW_TAG_variable, DW_AT_name("i")
    .dwattr $C$DW$5, DW_AT_TI_symbol_name("_i")
    .dwattr $C$DW$5, DW_AT_type(*$C$DW$T$10)
    .dwattr $C$DW$5, DW_AT_location[DW_OP_reg0]
    .dwcfi    cfa_offset, -2
    .dwcfi    save_reg_to_mem, 26, 0
    .dwcfi    save_reg_to_reg, 78, 26
        MOVW      DP,#_lim              ; [CPU_U]
    .dwpsn    file "for_loop.c",line 10,column 13,is_stmt
        MOV       AL,@_lim              ; [CPU_] |10|
        BF        $C$L3,EQ              ; [CPU_] |10|
        ; branchcc occurs ; [] |10|
    .dwpsn    file "for_loop.c",line 10,column 8,is_stmt
        MOVB      AL,#0                 ; [CPU_] |10|
$C$L2:    
    .dwpsn    file "for_loop.c",line 10,column 13,is_stmt
        ADDB      AL,#1                 ; [CPU_] |10|
        CMP       AL,@_lim              ; [CPU_] |10|
        B         $C$L2,LO              ; [CPU_] |10|
        ; branchcc occurs ; [] |10|
$C$L3:    
$C$DW$6    .dwtag  DW_TAG_TI_branch
    .dwattr $C$DW$6, DW_AT_low_pc(0x00)
    .dwattr $C$DW$6, DW_AT_TI_return
        LRETR     ; [CPU_]
        ; return occurs ; []
    .dwattr $C$DW$4, DW_AT_TI_end_file("for_loop.c")
    .dwattr $C$DW$4, DW_AT_TI_end_line(0x0b)
    .dwattr $C$DW$4, DW_AT_TI_end_column(0x01)
    .dwendentry
    .dwendtag $C$DW$4

This looks as if the for loop in for_loop_u() has been optimized away despite lim being declared volatile.

Can someone please comment on this?

Thanks and regards

Johannes

  • It's a bug if it removes either loop.  In C, the loop control expression has to be evaluated every iteration, and if it has side effects or could have side effects (volatile), those need to happen every iteration.  I've submitted SDSCM00049997 to track this issue.