270 Chapter 8
Ways to Improve Performance
Linker Optimizations
Linker Optimizations
The linker supports the -O option which performs the following
optimizations at link time:
• optimizes references to data by removing unnecessary ADDIL
instructions from the object code.
• removes procedures that can never be reached.
These optimizations can be separately enabled or disabled with the
+O[no]fastaccess and +O[no]procelim options respectively. The -O
linker option simply combines enabling of these into one option. For
example, the following ld command enables linker optimizations and
results in a smaller, faster executable:
$ ld -O -o prog /usr/ccs/lib/crt0.o prog.o -lm -lc
To enable one or the other optimization only, use the appropriate +O
option:
$ ld +Ofastaccess -o prog /usr/ccs/lib/crt0.o prog.o -lm -lc
$ ld +Oprocelim -o prog /usr/ccs/lib/crt0.o prog.o -lm -lc
Invoking Linker Optimizations from the
Compile Line
The compilers automatically call the linker with the +Ofastaccess and
+Oprocelim options if compiler optimization level 4 is selected. For
example, the following cc command invokes full compiler optimization
as well as linker optimization:
$ cc -o prog +O4 prog.c O4 invokes +Ofastaccess and +Oprocelim
If invoked with +O4, the compilers generate object code in such a way
that code optimization is done at link time. Thus, the linker does a better
job of optimizing code that was compiled with +O4.
When the compile and link phases are invoked by separate commands,
specify +O4 on both command lines. For example:
$ cc -c +O4 prog.c invokes compiler optimizations
$ cc -o prog +O4 prog.o invokes linker optimizations