A.7.3 Reducing the Number of Loops

Loop expansion is especially effective for inner loops. Since the program size is increased by loop expansion, loop expansion should be performed when a fast execution speed is preferred at the expense of the program size.

[Example]

Array a[] is initialized.

Source code before improvement

extern int a[100];
void func()
{
     int I;
     for( i = 0 ; i < 100 ; i++ ){
          a[i] = 0;
     }
}

 

Assembly-language expansion code before improvement

_func:
     MOV.L #00000064H,R4
     MOV.L #_a,R5
     MOV.L #00000000H,R3
L11:
     MOV.L R3,[R5+]
     SUB #01H,R4
     BNE L11
L12:
     RTS

 

Source code after improvement

extern int a[100];
void func()
{
     int I;
     for( i = 0 ; i < 100 ; i+=2 )
     {
          a[i] = 0;
          a[i+1] = 0;
     }
}

 

Assembly-language expansion code after improvement

_func:
     MOV.L #00000032H,R4
     MOV.L #_a,R5
L11:
     MOV.L #00000000H,[R5]
     MOV.L #00000000H,04H[R5]
     ADD #08H,R5
     SUB #01H,R4
     BNE L11
L12:
     RTS