Which gcc options reduce code size?

Jens picture Jens · Oct 21, 2013 · Viewed 7.4k times · Source

I'm constrained by a 128Kb limit executable size for an embedded PowerPC system. Unfortunately, using option -Os to optimize for size does not work due to what I believe is a compiler bug (link with -Os fails due to undefined reference to _restgpr_30_x and similar), while with -O[123] everything links fine. This is with gcc 4.8.1 host=i86, target=powerpc-wrs-vxworks.

My next idea is to use the various optimization options selectively. But which of these options would reduce code size instead of execution time? I went on to use some options in isolation and found that -O2 in addition with

-fno-caller-saves
-fno-cse-follow-jumps
-fno-hoist-adjacent-loads
-fno-inline-small-functions
-fno-optimize-sibling-calls
-fno-peephole2
-fno-reorder-functions
-fno-rerun-cse-after-loop
-fno-tree-vrp
-fno-reorder-blocks
-fno-tree-vect-loop-version

reduces code size when used. Is there a more systematic approach than experimenting? The GCC docs describe the various options, but don't say if they are more geared towards execution time speedup or code size reduction.

Answer

Zild picture Zild · Oct 21, 2013

-Os enables all -O2 optimization apart from :

-falign-functions -falign-jumps -falign-loops -falign-labels -freorder-blocks -freorder-blocks-and-partition -fprefetch-loop-arrays -ftree-vect-loop-version

Keep in mind that -Os makes gcc perform other operations to reduce the size so it might not be enough.