Topic : RC51 - Missed optimisation - pointer addtion.

Forum : 8051

Original Post
Post Information Post
February 15, 2010 - 4:36pm
Guest

RC51 compiles pointer addition less compactly than array indexing, even though the operations are equivalent. In the example below, pointer addition results in a call to _IMULX to scale by sizeof(int), whereas the equivalent operation via an array is optimised to 2 addtions.

extern int getInt(int const code *p);

int getIntAtIdx_ByArray(int code const *base, int idx)
{
return getInt(&base[idx]);
}

Gives....

0000 8E02 MOV AR2,R6
0002 8F03 MOV AR3,R7
; Register R4R5 is assigned to parameter idx
; SOURCE LINE # 7
0004 ED MOV A,R5
0005 2D ADD A,R5
0006 FF MOV R7,A
0007 EC MOV A,R4
0008 3C ADDC A,R4
0009 FE MOV R6,A
000A EF MOV A,R7
000B 2B ADD A,R3
000C FF MOV R7,A
000D EE MOV A,R6
000E 3A ADDC A,R2
000F FE MOV R6,A
0010 120000 R LCALL _getInt
; SOURCE LINE # 8
0013 22 RET

But.......

int getIntAtIdx_AddPtr(int code const *base, int idx)
{
return getInt(base + idx);
}

Gives.....

0000 8E02 MOV AR2,R6
0002 8F03 MOV AR3,R7
0004 900000 R MOV DPTR,#idx
0007 EC MOV A,R4
0008 F0 MOVX @DPTR,A
0009 A3 INC DPTR
000A ED MOV A,R5
000B F0 MOVX @DPTR,A
; SOURCE LINE # 13
000C E4 CLR A
000D FC MOV R4,A
000E 7D02 MOV R5,#002H
0010 900000 R MOV DPTR,#idx
0013 120000 R LCALL ?C_IMULX
0016 EF MOV A,R7
0017 2B ADD A,R3
0018 FF MOV R7,A
0019 EE MOV A,R6
001A 3A ADDC A,R2
001B FE MOV R6,A
001C 120000 R LCALL _getInt
; SOURCE LINE # 14
001F 22 RET