avr-gcc size optimization flags

Go To Last Post
8 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi,

I used in an old project some flags that optimized my code.
I can't remember what they were (I tried to search the forum).

 

Some one have any ideas what flags are used for it? (except of course -Os)

 

P.S. I'm building a DFU Bootloader on Xmega.

 

Thanks,

Arkadi

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

There's some words here:

 

https://tty1.net/blog/2008/avr-g...

 

But really the idea is that -Os is just a "catch all" that collects together all the -f switches that help towards the size goal.

 

If you are tight on space I guess that other than things like "relax" (which I sort of assume you'd be using anyway?) the fact is that there isn't much among the compiler options that will have a huge impact beyond -Os. Your best bet is to start studying the .LSS and see what parts it is that are generating "heavy" code and hand optimize the implementation of the worst offenders. For this reason you may find using "avr-nm --size-sort -r project.elf" useful. It produces output such as this:

$ avr-nm --size-sort -r asm.elf
000000c2 T __mulsf3x
000000c0 T __addsf3x
00000050 T __fp_rempio2
0000004a T __fp_powser
00000044 T __fp_split3
00000022 T __fp_sinus
00000022 T __fp_round
00000022 T __fp_powsodd
0000001c T __fp_mpack
0000000e T __fp_zero
0000000e T __fp_pscB
0000000e T __fp_pscA
0000000c T sin
0000000c T __fp_inf
00000006 T __fp_nan
00000004 T __mulsf3

This shows me that the biggest routine in this program (0xC2 bytes = 194) is "__mulsf3x". So that might be the routine I study first to see if there's any room for movement. (except on this occasion it won't work - almost every routine here has a "__" prefix which mean they come from the compiler and its code is already optimal). For your own programs you will see some of your own (or perhaps ASF?s) routines near the top of the list. Those are the ones to study the source of and see if there are any redundant bits that don't apply to the way you are using the code. Or anything that has a sub-optimal (sizewise) implementation.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You might be thinking of "-mrelax" which was originally something like " -mshort-calls" ?  Causes 4-byte jmp and call instructions to get be turned into two-byte rjmp/rcall instructions when they can be.  I don't think this is included by any of the -O options (and it needs a link-time option as well.)

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Just to note that if using relax it is not enough to use -mrelax. That tells the compiler but not the linker. So you have to use -Wl,-relax too so the linker knows as well - otherwise they get out of sync. In Studio 6+ there is a tick box that applies both options.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Just to note that if using relax it is not enough to use -mrelax.

Uh... -mrelax does tell the linker:

       -mrelax
           Shorten some address references at link time, when possible; uses the linker option -relax.

       -mrelax
           Indicate to the linker that it should perform a relaxation optimization pass to shorten branches, calls and absolute memory addresses.  This option only has an effect when used on the command line for the final link step.

The 'm' prefix makes it a 'Machine Dependent Option'.  The liner option is --relax, which can be passed to the linker with -Wl,--relax as you note.  However, use of -mrelax is preferred as it enables the compiler to properly embed debugging information.  Without it, the relationship between source code and machine code can get out of sync (after the linker's --relax pass) when debugging or when examining with objdump.

 

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Then my memory must be going.  Early in AS6 there was an issue where people's debugging was getting "out of step" with the source. It was traced to the use of "relax" (and I think that was -mrelax) and I thought it was determined that it needed to be applied in TWO places and that "-mrelax" alone is not sufficient. Following that a subsequent issue of AS6 then had a tick box added so that the user could select in one place to use it and both the mechanisms to advise the tools would be used.

 

I guess I need to dig out AS7 and remind myself what effect on command line options that tick box is actually having.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
Early in AS6 there was an issue where people's debugging was getting "out of step" with the source.

https://www.avrfreaks.net/forum/r...

 

And I opined in https://www.avrfreaks.net/comment...

theusch wrote:

Relax, John, just relax. At compile time especially. Then, at link time, just relax.

"Doctor, doctor--I keep dreaming about teepees and wigwams. What could be the problem?!?"

"Obviously, you are too tense. That will be $20."

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

It's not to say that you don't need to use -mrelax at both the compile phase and the link phase, merely that -Wl,--relax is not necessary when using -mrelax.

 

Here's what appears to be a relevant thread:

https://www.avrfreaks.net/forum/where-has-mshort-calls-compiler-option-gone

 

Possibly also relevant:

https://www.avrfreaks.net/forum/shifted-debugging-info

https://www.avrfreaks.net/forum/688-bytes-after-changing-target-atmega8-atmega328p

https://www.avrfreaks.net/forum/how-force-jmp-instead-rjmp-assembly-s-file

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"We see a lot of arses on handlebars around here." - [J Ekdahl]