[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[avr-gcc-list] Question on optimizing behavior of GCC.
From: |
Hudaidai |
Subject: |
[avr-gcc-list] Question on optimizing behavior of GCC. |
Date: |
Wed, 16 Apr 2003 10:2:28 +0800 |
Hi,
I have a question on optimizing behavior of GCC(tested with WinAVR-20030312).
Here is the C code:
#include <inttypes.h>
#define BUF_LEN 16
uint8_t aucBuf[BUF_LEN];
uint8_t * pucMsgWr;
uint8_t * volatile pucNewLine;
#define INC_PRN_MSG_P(pmsg) \
{ \
pmsg++; \
if (pmsg>=aucBuf+BUF_LEN) \
pmsg=aucBuf; \
}
void fast_func(void)
{
uint8_t *pucTemp;
pucTemp=pucMsgWr;
*pucTemp='\0';
INC_PRN_MSG_P(pucTemp);
pucMsgWr=pucTemp;
pucNewLine=pucTemp;
}
void slow_func(void)
{
*pucMsgWr='\0';
INC_PRN_MSG_P(pucMsgWr);
pucNewLine=pucMsgWr;
}
int main(void)
{
pucMsgWr=aucBuf;
fast_func();
slow_func();
}
Let's look at the list file:
000000a4 <fast_func>:
void fast_func(void)
{
uint8_t *pucTemp;
pucTemp=pucMsgWr;
a4: e0 91 72 00 lds r30, 0x0072
a8: f0 91 73 00 lds r31, 0x0073
*pucTemp='\0';
ac: 11 92 st Z+, r1
INC_PRN_MSG_P(pucTemp);
ae: 80 e0 ldi r24, 0x00 ; 0
b0: e2 37 cpi r30, 0x72 ; 114
b2: f8 07 cpc r31, r24
b4: 10 f0 brcs .+4 ; 0xba
b6: e2 e6 ldi r30, 0x62 ; 98
b8: f0 e0 ldi r31, 0x00 ; 0
pucMsgWr=pucTemp;
ba: f0 93 73 00 sts 0x0073, r31
be: e0 93 72 00 sts 0x0072, r30
pucNewLine=pucTemp;
c2: f0 93 61 00 sts 0x0061, r31
c6: e0 93 60 00 sts 0x0060, r30
}
ca: 08 95 ret
000000cc <slow_func>:
void slow_func(void)
{
*pucMsgWr='\0';
cc: e0 91 72 00 lds r30, 0x0072
d0: f0 91 73 00 lds r31, 0x0073 ;*** first time
accessing "pucMsgWr".
d4: 10 82 st Z, r1
INC_PRN_MSG_P(pucMsgWr);
d6: 80 91 72 00 lds r24, 0x0072
da: 90 91 73 00 lds r25, 0x0073 ;*** why accessing
"pucMsgWr" again?
de: 01 96 adiw r24, 0x01 ; 1
e0: 90 93 73 00 sts 0x0073, r25
e4: 80 93 72 00 sts 0x0072, r24 ;*** waste space(may be
done after compare).
e8: 82 57 subi r24, 0x72 ; 114
ea: 90 40 sbci r25, 0x00 ; 0
ec: 30 f0 brcs .+12 ; 0xfa
ee: 82 e6 ldi r24, 0x62 ; 98
f0: 90 e0 ldi r25, 0x00 ; 0
f2: 90 93 73 00 sts 0x0073, r25
f6: 80 93 72 00 sts 0x0072, r24 ;*** this storeing is
really needed.
pucNewLine=pucMsgWr;
fa: 80 91 72 00 lds r24, 0x0072
fe: 90 91 73 00 lds r25, 0x0073 ;*** but why loading it
again?
102: 90 93 61 00 sts 0x0061, r25
106: 80 93 60 00 sts 0x0060, r24
}
10a: 08 95 ret
I am very surprised that "pucMsgWr" was accessed 4 times in the "slow_func". It
is something like that "pucMsgWr" declared as "uint8_t * volatile". Nothing is
diffrent when either "-O3" or "-Os" option is used.
Who can tell me why?
hudaidai
2003-04-16
- [avr-gcc-list] Question on optimizing behavior of GCC.,
Hudaidai <=