Next: , Previous: , Up: Source Optimization   [Contents][Index]


5.4.2 Constant Folding

Constant folding is an optimization that replaces a call of constant arguments with the constant result of that call. Constant folding is done on all standard functions for which it is legal. Inline expansion allows folding of any constant parts of the definition, and can be done even on functions that have side-effects.

It is convenient to rely on constant folding when programming, as in this example:

(defconstant limit 42)

(defun foo ()
  (... (1- limit) ...))

Constant folding is also helpful when writing macros or inline functions, since it usually eliminates the need to write a macro that special-cases constant arguments.

Constant folding of a user defined function is enabled by the extensions:constant-function proclamation. In this example:

(declaim (ext:constant-function myfun))
(defun myexp (x y)
  (declare (single-float x y))
  (exp (* (log x) y)))

 ... (myexp 3.0 1.3) ...

The call to myexp is constant-folded to 4.1711674.


Next: Unused Expression Elimination, Previous: Let Optimization, Up: Source Optimization   [Contents][Index]