A.7.6 Inline Expansion

The execution speed can be improved by performing inline expansion for functions that are frequently called. A significant effect may be obtained by expanding functions that are particularly called in the loop. However, since the program size is inclined to be increased by inline expansion, inline expansion should be performed when a fast execution speed is preferred at the expense of the program size.

[Example]

The elements of array a and array b are exchanged.

Source code before improvement

int x[10], y[10];
static void sub(int *a, int *b, int I)
{
     int temp;
     temp = a[i];
     a[i] = b[i];
     b[i] = temp;
}
 
void func()
{
     int I;
     for(i=0;i<10;i++)
     {
          sub(x,y,i);
     }
}

 

Assembly-language expansion code before improvement

__$sub:
     SHLL #02H,R3
     ADD R3,R1
     MOV.L [R1],R5
     ADD R3,R2
     MOV.L [R2],[R1]
     MOV.L R5,[R2]
     RTS
_func:
     PUSHM R6-R8
     MOV.L #00000000H,R6
     MOV.L #_x,R7
     MOV.L #_y,R8
L12:
     MOV.L R6,R3
     MOV.L R7,R1
     MOV.L R8,R2
     ADD #01H,R6
     BSR __$sub
     CMP #0AH,R6
     BLT L12
L13:
     RTSD #0CH,R6-R8

 

Source code after improvement

int x[10], y[10];
#pragma inline(sub)
static void sub(int *a, int *b, int I)
{
     int temp;
     temp = a[i];
     a[i] = b[i];
     b[i] = temp;
}
 
void func()
{
     int I;
     for(i=0;i<10;i++)
     {
          sub(x,y,i);
     }
}

 

Assembly-language expansion code after improvement

; The _sub code was reduced as a result of inline expansion
_func:
     MOV.L #0000000AH,R1
     MOV.L #_y,R2
     MOV.L #_x,R3
L11:
     MOV.L [R3],R4
     MOV.L [R2],R5
     MOV.L R4,[R2+]
     MOV.L R5,[R3+]
     SUB #01H,R1
     BNE L11
L12:
     RTS