55
Copy propagation
Propagating copies is primarily speed optimization, but since it never
increases the size of your code, it is safe to use if you have enabled -
Og
. Like loop invariant code motion, copy propagation relies on the
analysis performed during common subexpression elimination. Copy
propagation means that the optimizer remembers the values assigned to
expressions and uses those values instead of loading the value of the
assigned expressions. Copies of constants, expressions, and variables
may be propagated.
Pointer aliasing
Pointer aliasing is not an optimization in itself, but it does affect the way
the optimizer performs common subexpression elimination and copy
propagation. When pointer aliasing is turned on, it allows the optimizer
to maintain copy propagation information across function calls and to
maintain common subexpression information across some stores.
Otherwise, the optimizer must discard information about copies and
subexpressions in these situations.
Induction-variable
analysis and
strength reduction
Creating induction variables and performing strength reduction are
speed optimizations performed on loops. The optimizer uses a
mathematical technique called induction to create new variables out of
expressions used inside a loop. These variables are called induction
variables. The optimizer assures that the operations performed on these
new variables are computationally less expensive (reduced in strength)
than those used by the original variables.
Opportunities for these optimizations are common if you use array
indexing inside loops, since a multiplication operation is required to
calculate the position in the array which is indicated by the index.
Loop compaction
Loop compaction takes advantage of the string move instructions on the
80x86 processors by replacing the code for a loop with such an
instruction.
Depending on the complexity of the operands, the compacted loop code
may also be smaller than the corresponding non-compacted loop. You
may wish to experiment with this optimization if you are compiling for
size and have loops of this nature.
Structure copy
inlining
The most visible optimization performed when compiling for speed as
opposed to size is that of inlining structure copies. When you enable
-
Ot
, the compiler determines whether it can safely generate code to
perform a
rep movsw
instruction instead of calling a helper function to
do the copy. For structures and unions of over eight bytes in length,
performing this optimization produces faster structure copies than the
corresponding helper function call.
Code compaction
The most visible optimization performed when compiling for size is code
compaction. In code compaction, the optimizer scans the generated
code for duplicate sequences. When such sequences warrant, the
optimizer replaces one sequence of code with a jump to the other,
thereby eliminating the first piece of code.
switch
statements contain
the most opportunities for code compaction.