Compiler optimization 2026-11-05
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 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.
Undefined behavior 2026-11-05
Undefined behavior is behavior for which a programming language specification imposes no requirements when a specified erroneous condition occurs. It is not a promise to select one arbitrary but otherwise constrained result. The C programming language uses this category for conditions including signed integer overflow.[ref]
For example, evaluating x+1 as signed int when x is the largest representable int has undefined behavior in that model. A compiler can therefore reason that x+1 exceeds x on executions where the addition is defined, instead of preserving an assumed wraparound case. Unsigned integer arithmetic in C has different rules. Observing a particular result in one build is insufficient to establish a contract; correctness needs range checks, suitable representations or another explicitly defined operation.