Discussion:
Optimizations with avr-gcc
Sandeep K Chaudhary
2014-06-25 15:23:53 UTC
Permalink
Hi guys,

I have a small program with the following statements -

DDRB = 0x1;

DDRB = DDRB<<2;

DDRB += 5;

I am compiling it with "avr-gcc -mmcu=atmega8 -O3 -fdump-tree-all
ledchase.c". As you can see, I am dumping the content after each pass
in avr-gcc.

In the dumps, I see that neither statement 2 nor 3 is optimized i.e. I
don't see "DDRB = 4 (from 2nd statement)" or "DDRB = 5 (from 3rd
statement)".

My question is - Is it possible to achieve such optimizations with
avr-gcc? if not, why?

Please let me know. It would help me a lot. Thanks in advance !

Thanks and regards,
Sandeep.
Niklas Gürtler
2014-06-25 16:57:02 UTC
Permalink
That's because all I/O Registers, including DDRB, are declared as
"volatile". Accesses to it won't be optimized.
For example this code would turn on an I/O pin for one cycle:
PORTB |= 1;
PORTB &= ~1;
If the compiler considered the first instruction as useless and
optimized it out, the pin would never be turned on. So, PORTB and the
others are declared as volatile to prevent this optimization.
Post by Sandeep K Chaudhary
Hi guys,
I have a small program with the following statements -
DDRB = 0x1;
DDRB = DDRB<<2;
DDRB += 5;
I am compiling it with "avr-gcc -mmcu=atmega8 -O3 -fdump-tree-all
ledchase.c". As you can see, I am dumping the content after each pass
in avr-gcc.
In the dumps, I see that neither statement 2 nor 3 is optimized i.e. I
don't see "DDRB = 4 (from 2nd statement)" or "DDRB = 5 (from 3rd
statement)".
My question is - Is it possible to achieve such optimizations with
avr-gcc? if not, why?
Please let me know. It would help me a lot. Thanks in advance !
Thanks and regards,
Sandeep.
Continue reading on narkive:
Search results for 'Optimizations with avr-gcc' (Questions and Answers)
4
replies
Is programming different for AVR and ARM?
started 2012-05-31 04:43:05 UTC
programming & design
Loading...