The following sections describe the general optimizations done by the compiler, they are not processor specific. Some of these require some compiler switch override while others are done automatically (those which require a switch will be noted as such).
In Free Pascal, if the operand(s) of an operator are constants, they will be evaluated at compile time.
Example
x:=1+2+3+6+5; will generate the same code as x:=17;
Furthermore, if an array index is a constant, the offset will be evaluated at compile time. This means that accessing MyData[5] is as efficient as accessing a normal variable.
Finally, calling Chr, Hi, Lo, Ord, Pred, or Succ functions with constant parameters generates no run-time library calls, instead, the values are evaluated at compile time.
Using the same constant string two or more times generates only one copy of the string constant.
Evaluation of boolean expression stops as soon as the result is known, which makes code execute faster then if all boolean operands were evaluated.
Using the in operator is always more efficient then using the
equivalent <>
, =
, <=
, >=
, <
and >
operators. This is because range comparisons can be done more easily with
in then with normal comparison operators.
Sets which contain less then 33 elements can be directly encoded using a 32-bit value, therefore no run-time library calls to evaluate operands on these sets are required; they are directly encoded by the code generator.
Assignments of constants to variables are range checked at compile time, which removes the need of the generation of runtime range checking code.
Remark: This feature was not implemented before version 0.99.5 of Free Pascal.
When one of the operands in a multiplication is a power of two, they are encoded using arithmetic shift instructions, which generates more efficient code.
Similarly, if the divisor in a div operation is a power of two, it is encoded using arithmetic shift instructions.
The same is true when accessing array indexes which are powers of two, the address is calculated using arithmetic shifts instead of the multiply instruction.
By default all variables larger then a byte are guaranteed to be aligned at least on a word boundary.
Furthermore all pointers allocated using the standard runtime library (New and GetMem among others) are guaranteed to return pointers aligned on a quadword boundary (64-bit alignment).
Alignment of variables on the stack depends on the target processor.
Remark: Two facts about alignment:
This feature removes all unreferenced code in the final executable file, making the executable file much smaller.
Smart linking is switched on with the -Cx command-line switch, or using the {$SMARTLINK ON} global directive.
Remark: Smart linking was implemented starting with version 0.99.6 of Free Pascal.
The following runtime library routines are coded directly into the final executable : Lo, Hi, High, Sizeof, TypeOf, Length, Pred, Succ, Inc, Dec and Assigned.
Remark: Inline Inc and Dec were not completely implemented until version 0.99.6 of Free Pascal.
When using the -O1 (or higher) switch, case statements will be generated using a jump table if appropriate, to make them execute faster.
Under specific conditions, the stack frame (entry and exit code for the routine, see section ) will be omitted, and the variable will directly be accessed via the stack pointer.
Conditions for omission of the stack frame :
When using the -Or switch, local variables or parameters which are used very often will be moved to registers for faster access.
Remark: Register variable allocation is currently an experimental feature, and should be used with caution.
Here follows a listing of the optimizing techniques used in the compiler:
Although you can enable uncertain optimizations in most cases, for people who do not understand the following technical explanation, it might be the safest to leave them off.
If uncertain optimizations are enabled, the CSE algortihm assumes that
The following example will produce bad code when you switch on uncertain optimizations:
Var temp: Longint; Procedure Foo(Var Bar: Longint); Begin If (Bar = temp) Then Begin Inc(Bar); If (Bar <> temp) then Writeln('bug!') End End; Begin Foo(Temp); End.The reason it produces bad code is because you access the global variable Temp both through its name Temp and through a pointer, in this case using the Bar variable parameter, which is nothing but a pointer to Temp in the above code.
On the other hand, you can use the uncertain optimizations if you access global/local variables or parameters through pointers, and only access them through this pointer10.1.
For example:
Type TMyRec = Record a, b: Longint; End; PMyRec = ^TMyRec; TMyRecArray = Array [1..100000] of TMyRec; PMyRecArray = ^TMyRecArray; Var MyRecArrayPtr: PMyRecArray; MyRecPtr: PMyRec; Counter: Longint; Begin New(MyRecArrayPtr); For Counter := 1 to 100000 Do Begin MyRecPtr := @MyRecArrayPtr^[Counter]; MyRecPtr^.a := Counter; MyRecPtr^.b := Counter div 2; End; End.Will produce correct code, because the global variable MyRecArrayPtr is not accessed directly, but only through a pointer (MyRecPtr in this case).
In conclusion, one could say that you can use uncertain optimizations only when you know what you're doing.
Using the -O2 switch does several optimizations in the code produced, the most notable being:
subl $4,%esp
") instead of slower, smaller instructions
("enter $4
"). This is the default setting.
movzbl (mem), %eax|to a combination of simpler instructions
xorl %eax, %eax movb (mem), %alfor the Pentium.
Note: Code blocks which contain an assembler block, are not processed at all by the optimizer at this time. Update: as of version 0.99.11, the Pascal code surrounding the assembler blocks is optimized.
This is where can be found processor specific information on floating point code generated by the compiler.
All normal floating point types map to their real type, including comp and extended.
Early generations of the Motorola 680x0 processors did not have integrated floating point units, so to circumvent this fact, all floating point operations are emulated (with the $E+ switch, which is the default) using the IEEE Single floating point type. In other words when emulation is on, Real, Single, Double and Extended all map to the single floating point type.
When the $E switch is turned off, normal 68882/68881/68040 floating point opcodes are emitted. The Real type still maps to Single but the other types map to their true floating point types. Only basic FPU opcodes are used, which means that it can work on 68040 processors correctly.
Remark: Double and Extended types in true floating point mode have not been extensively tested as of version 0.99.5.
Remark: The comp data type is currently not supported.