Compiler optimization (source code)

= Compiler optimization

<Compiler optimization> transforms a program representation to improve a chosen resource objective while preserving the behavior required by its <programming language semantics>. Objectives include execution time, code size and <computer memory>[memory] traffic. A transformation need not find a globally best program, and improving one objective can worsen another.

For example, <constant folding> replaces `2+3` by `5`, while <common subexpression elimination> may compute a repeated expression once. Reusing a load across a store is valid only if the store cannot change the loaded location; <alias analysis> helps establish that condition. Similarly, replacing floating-point `(a+b)+c` by `a+(b+c)` can change rounding, so an optimizer needs an applicable permission or proof. <Data-flow analysis> supplies facts used by transformations, and <undefined behavior> affects which executions the language requires an implementation to preserve.