A.7.1 Loop Control Variable

Loop expansion cannot be optimized if there is a possibility that the size difference prevents the loop control variable from expressing the data to be compared when determining whether the loop end condition is met. For example, if the loop control variable is signed char while the data to be compared is signed long, loop expansion is not optimized. Thus, compared to signed char and signed short, it is easier to perform optimization of loop expansion for signed long. To optimize loop expansion, specify the loop control variable as a 4-byte integer type.

[Example]

Source code before improvement

signed long array_size=16;
signed char array[16];
 
void func()
{
     signed char I;
     for(i=0;i<array_size;i++)
     {
          array[i]=0;
     }
}

 

Assembly-language expansion code before improvement

<When loop=2 is specified>
_func:
     MOV.L #_array_size,R4
     MOV.L [R4],R2
     MOV.L #00000000H,R5
     BRA L11
L12:
     MOV.L #_array,R14
     MOV.L #00000000H,R3
     MOV.B R3,[R5,R4]
     ADD #01H,R5
L11:
     MOV.B R5,R5
     CMP R2,R5
     BLT L12
L13:
     RTS

 

Source code after improvement

signed long array_size=16;
signed char array[16];
 
void func()
{
     signed long I;
     for(i=0;i<array_size;i++)
     {
          array[i]=0;
     }
}

 

Assembly-language expansion code after improvement

<When loop=2 is specified>
_func: 
     MOV.L #_array_size,R5
     MOV.L [R5],R2
     MOV.L #00000000H,R4
     ADD #0FFFFFFFFH,R2,R3
     CMP R3,R2
     BLE L12
L11:
     MOV.L #_array,R1
     MOV.L R1,R5
     BRA L13
L14:
     MOV.W #0000H,[R5]
     ADD #02H,R5
     ADD #02H,R4
L13:
     CMP R3,R4
     BLT L14
L15:
     CMP R2,R4
     BGE L17 L16:
     MOV.L #00000000H,R5
     MOV.B R5,[R4,R1]
     RTS
L12:
     MOV.L #_array,R5
     MOV.L #00000000H,R3
L19:
     CMPR2,R4
     BGE L17
L20:
     MOV.B R3,[R5+]
     ADD #01H,R4
     BRA L19
L17:
     RTS