Update lfs-uefi.txt
[linux_from_scratch_hints.git] / optimization.txt
blob61063992947f2754d6feb5d0d9ce8a5c052028eb
1 AUTHOR:         Jim Gifford <lfs-hints at jg555.com>
2                 Originally by Gerard Beekmans < gerard at linuxfromscratch.org >
3                 Originally by Thomas -Balu-Walter < tw at itreff.de >
4                 Originally by Eric Olinger <eric at supertux.com> optimization2.txt
6 DATE:           2003-10-30
8 LICENSE:        GNU Free Documentation License Version 1.2
10 SYNOPSIS:       Compiler-optimization
12 DESCRIPTION:    This hint will act as a guide on how-to or not-to use
13                 compiler optimization routines.
15 PREREQUISITES:  None
17 HINT:
19 The origin of this text is the 2.4.3-version of the book - Chapter 6. I
20 modified it a little to create this hint.
22 Most programs and libraries by default are compiled with optimizing level 2
23 (gcc options -g and -O2) and are compiled for a specific CPU. On Intel
24 platforms software is compiled for i386 processors by default. If you don't
25 wish to run software on other machines other than your own, you might want to
26 change the default compiler options so that they will be compiled with a higher
27 optimization level, and generate code for your specific architecture.
29 There are a few ways to change the default compiler options. One way is to edit
30 every Makefile file you can find in a package, look for the CFLAGS and CXXFLAGS
31 variables (a well designed package uses the CFLAGS variable to define gcc
32 compiler options and CXXFLAGS to define g++ compiler options) and change their
33 values. Packages like binutils, gcc, glibc and others have a lot of Makefile
34 files in a lot of subdirectories so this would take a lot of time to do.
35 Instead there's an easier way to do things: create the CFLAGS and CXXFLAGS
36 environment variables. Most configure scripts read the CFLAGS and CXXFLAGS
37 variables and use them in the Makefile files. A few packages don't follow this
38 convention and those package require manual editing.
40 To set those variables you can do the following commands in bash (or in your
41 .bashrc if you want them to be there all the time):
43     export CFLAGS="-O3 -march=<architecture>" &&
44     CXXFLAGS=$CFLAGS
46 This is a minimal set of optimizations that ensures it works on almost all
47 platforms. The option march will compile the binaries with specific
48 instructions for that CPU you have specified. This means you can't copy this
49 binary to a lower class CPU and execute it. It will either work very unreliable
50 or not at all (it will give errors like "Illegal Instruction, core dumped").
51 You'll have to read the GCC Info page to find more possible optimization flags.
52 In the above environment variable you have to replace <architecture> with the
53 appropriate CPU identifiers such as i586, i686, powerpc and others. I suggest
54 to have a look at the gcc-manual at http://gcc.gnu.org/onlinedocs/gcc_toc.html
55 "Hardware Models and Configurations".
58  * Ed. note
59  * "Reboant" dropped a note about how using -Os (optimize for size) showed
60  * incredibly good results. So if you want is small binary size rather than fast
61  * execution time, you might want to take a look at this.
62  */
64 Please keep in mind that if you find that a package doesn't compile and gives
65 errors like "segmentation fault, core dumped" it's most likely got to do with
66 these compiler optimizations. Try lowering the optimizing level by changing -O3
67 to -O2. If that doesn't work try -O or leave it out all together.  Also try
68 changing the -march variable. Compilers are very sensitive to certain hardware
69 too. Bad memory can cause compilation problems when a high level of
70 optimization is used, like the -O3 setting. The fact that I don't have any
71 problems compiling everything with -O3 doesn't mean you won't have any problems
72 either. Another problem can be the Binutils version that's installed on your
73 system which often causes compilation problems in Glibc (most noticable in
74 RedHat because RedHat often uses beta software which aren't always very stable.
76 "RedHat likes living on the bleeding edge, but leaves the bleeding up to you"
77 (quoted from somebody on the lfs-discuss mailinglist).
79 DEFINITIONS FOR FLAGS:
81 For more information on compiler optimization flags see the GCC Command 
82 s page in the Online GCC 3.3.1 docs at: 
84 http://gcc.gnu.org/onlinedocs/gcc-3.3.1/gcc/Optimize-Options.html#Optimize%20Options
85 http://gcc.gnu.org/onlinedocs/gcc-3.3.1/gcc/i386-and-x86-64-Options.html#i386%20and%20x86-64%20Options
88         A linker option that remove all symbol table and relocation 
89         information from the binary.
91 -O3
92         This flag sets the optimizing level for the binary.
93                 3       Highest level, machine specific code is generated.
94                         Auto-magically adds the -finline-functions and 
95                         -frename-registers flags. 
96                 2       Most make files have this set up as Default, performs all  
97                         supported optimizations that do not involve a space-speed 
98                         tradeoff. Adds the -fforce-mem flag auto-magically.
99                 1       Minimal optimizations are performed. Default for the compiler, 
100                         if nothing is given.
101                 0       Don't optimize.
102                 s       Same as O2 but does additional optimizations for size.
104 -fomit-frame-pointer 
105         Tells the compiler not to keep the frame pointer in 
106         a register for functions that don't need one.  This 
107         avoids the instructions to save, set up and restore 
108         frame pointers; it also makes an extra register available 
109         in many functions. It also makes debugging impossible 
110         on some machines.
112 -march=pentium3
113         Defines the instructions set to use when compiling. -mpcu is implied 
114         be the same as -march when only -march is used.
115                 i386                    Intel 386 Prcoessor
116                 i486                    Intel/AMD 486 Processor
117                 pentium                 Intel Pentium Processor
118                 pentiumpro              Intel Pentium Pro Processor
119                 pentium2                Intel PentiumII/Celeron Processor
120                 pentium3                Intel PentiumIII/Celeron Processor
121                 pentium4                Intel Pentium 4/Celeron Processor
122                 k6                      AMD K6 Processor
123                 k6-2                    AMD K6-2 Processor
124                 K6-3                    AMD K6-3 Processor
125                 athlon                  AMD Athlon/Duron Processor
126                 athlon-tbird            AMD Athlon Thunderbird Processor
127                 athlon-4                AMD Athlon Version 4 Processor
128                 athlon-xp               AMD Athlon XP Processor
129                 athlon-mp               AMD Athlon MP Processor
130                 winchip-c6              Winchip C6 Processor
131                 winchip2                Winchip 2 Processor
132                 c3                      VIA C3 Cyrix Processor
134 -mmmx
135 -msse
136 -msse2
137 -m3dnow
138         These switches enable or disable the use of built-in functions
139         that allow direct access to the MMX, SSE and 3Dnow extensions
140         of the instruction set.
141         
142 OPTIMIZATION LINKS:
144 Safe flags to use for gentoo-1.4  
145 http://www.freehackers.org/gentoo/gccflags/flag_gcc3.html
147 Securing & Optimizing Linux: The Ultimate Solution v2.0
148 http://www.openna.com/products/books/sol/solus.php
150 PERSON EXPERIENCE:
152 I have tried using all optimization levels, but to my disappointment, results varied
153 from package to package. Using -O(any number) using GCC 3.3.1 can give unpredictable
154 responses. 
156 Some of those unpredicatable responses can be seen with the following bugs sent to GCC.
157 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=12590
158 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=10655
159 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=8440
161 VERSION:        1.2
163 CHANGELOG:      1.2 Fixed Typos
164                 1.1 Fixed Typos and Cut-n-Paste Errors
165                 1.0 Adopted by Jim Gifford
167  New Version of this document can be viewed from http://cvs.jg555.com/viewcvs.cgi/lfs-hints