Optimization

From Lazarus wiki
Jump to: navigation, search

For an overview of optimization possibilities, see Chapter 11 of the Free Pascal Programmer's Guide.

Free Pascal allows you to use a set of straightforward compiler directives or commandline arguments to considerably power up your programs. (While running debug builds, you may want to keep most code optimizations off to enjoy faster compilation speeds and avoid rare unintended side effects.)


Target Processor

By default, FPC selects a conservative minimum target for code generation, to maximize compatibility. A higher target processor enables the compiler to use different instructions that would not be available on lower processors. How high you want to set the minimum target processor depends on your target audience. To set this, use "-Cp<CPU>". For example, on the x86 platform, -CpPENTIUMM gives the compiler plenty of room and still covers almost all users.

The compiler can also produce code generally favoring a particular processor, but without requiring that processor as a hard minimum. To set this, use "-Op<CPU>".

To check which processors your compiler version supports, run "fpc -ic".


Target FPU

If your 32-bit program uses a lot of single/double type variables, you may gain a significant speed boost from enabling SSE instructions. "-CfSSE" will speed single operations, and "-CfSSE2" will speed single and double operations. On the amd64 platform both are probably already on by default.

To check which FPU instruction sets your compiler version supports, run "fpc -if".


Optimization Switches

General optimizations are not specific to any processor. You can enable optimization groups using "-O1", "-O2", "-O3", and "-O4". Individual switches can be enabled with "-Oo<switch>".

As of 26 March 2017, the optimization groups (defined in /compiler/<arch>/cpuinfo.pas, which references /compiler/globtype.pas) are:

-O1: PEEPHOLE

-O2: O1 + REMOVEEMPTYPROCS + REGVAR + STACKFRAME + TAILREC + CSE

-O3: O2 + CONSTPROP + DFA

-O4: O3 + ORDERFIELDS + DEADVALUES + FASTMATH + USEEBP/USERBP

Ungrouped: UNCERTAIN, SIZE, LOOPUNROLL, STRENGTH, SCHEDULE, AUTOINLINE, DEADSTORE, FORCENOSTACKFRAME

(The -O4 group has potential side effects and may break your code, so use responsibly.)

To check which switches your compiler version supports, run "fpc -io".


Notes

To take optimization a step further, see Whole Program Optimization.

If you are interested in optimizing for size rather than speed, see Size Matters.