| Contents | Index |
| On this page… |
|---|
Expression folding is a code optimization technique that minimizes the computation of intermediate results at block outputs and the storage of such results in temporary buffers or variables. Eliminate superfluous local variables (expression folding) is used to enable expression folding. When expression folding is on, the Simulink Coder code generator collapses, or "folds," block computations into a single expression, instead of generating separate code statements and storage declarations for each block in the model.
Expression folding can dramatically improve the efficiency of generated code, frequently achieving results that compare favorably to hand-optimized code. In many cases, entire groups of model computations fold into a single highly optimized line of code.
By default, expression folding is on. The Simulink Coder code generation options are configured to use expression folding wherever possible. Most Simulink blocks support expression folding.
You can also take advantage of expression folding in your own inlined S-function blocks. See Write S-Functions That Support Expression Folding for information on how to do this.
In the code generation examples that follow, the Signal storage reuse optimizations (Enable local block outputs, Reuse block outputs, Eliminate superfluous local variables (expression folding) and Minimize data copies between local and global variables) are all turned on.
As a simple example of how expression folding affects the code generated from a model, consider the following model.

With expression folding on, this model generates a single-line output computation, as shown in this model_output function.
static void exprfld_output(int_T tid)
{
/* Outport: '<Root>/Out1' incorporates:
* Gain: '<Root>/k1'
* Gain: '<Root>/k2'
* Inport: '<Root>/In1
* Inport: '<Root>/In2
* Product: '<Root>/Product'
*/
exprfld_Y.Out1 = exprfld_U.i1 * exprfld_P.k1_Gain *
(exprfld_U.i2 * exprfld_P.k2_Gain);
}
The generated comments indicate the block computations that were combined into a single expression. The comments also document the block parameters that appear in the expression.
With expression folding off, the same model computes temporary results for both Gain blocks before the final output, as shown in this output function:
static void exprfld_output(int_T tid)
{
real_T rtb_S2;
/* Gain: '<Root>/k1' incorporates:
* Inport: '<Root>/In1'
*/
exprfld_Y.Out1 = exprfld_U.i1 * exprfld_P.k1_Gain
/* Gain: '<Root>/k2' incorporates:
* Inport: '<Root>/In2'
*/
rtb_S2 = exprfld_U.i2 * exprfld_P.k2_Gain;
/* Product: '<Root>/Product' */
exprfld_Y.Out1 = exprfld_Y.Out1 * rtb_S2;
}
Turn on expression folding, a code optimization technique that minimizes the computation of intermediate results and the use of temporary buffers or variables.
Enable expression folding and regenerate the code as follows:
Change your current working folder to example_codegen if you have not already done so.
In Model Explorer, select Optimization in the center pane and click the Signals and Parameters tab on the right pane. The Signals and Parameters pane appears at the right.
Select the Eliminate superfluous local variables (expression folding) option.

Select Code Generation in the center pane, and click Generate code on the right.
The Simulink Coder software generates code as before.
In the previous examples, the Gain block computation was computed in a separate code statement and the result was stored in a temporary location before the final output computation.
With Eliminate superfluous local variables (expression folding) selected, there is a subtle but significant difference in the generated code: the gain computation is incorporated (or folded) directly into the Outport computation, eliminating the temporary location and separate code statement. This computation is on the last line of the example_output function.
/* Model output function */
static void example_output(int_T tid)
{
/* Outport: '<Root>/Out1' incorporates:
* Gain: '<Root>/Gain'
* Sin: '<Root>/Sine Wave'
*/
example_Y.Out1 = (sin(example_P.SineWave_Freq * example_M->Timing.t[0] +
example_P.SineWave_Phase) * example_P.SineWave_Amp +
example_P.SineWave_Bias) * example_P.Gain_Gain;
/* tid is required for a uniform function interface.
* Argument tid is not used in the function. */
UNUSED_PARAMETER(tid);
}In many cases, expression folding can incorporate entire model computations into a single, highly optimized line of code. Expression folding is turned on by default. Using this option will improve the efficiency of generated code.
For an example of expression folding in the context of a more complex model, click rtwdemo_slexprfold , or type the following command at the MATLAB prompt.
rtwdemo_slexprfold
Expression folding operates only on expressions involving local variables. Expression folding is therefore available only when the Signal storage reuse code generation option is on.
For a new model, default code generation options are set to use expression folding. If you are configuring an existing model, enable expression folding as follows:
Open the Configuration Parameters dialog box and select the Optimization > Signals and Parameters pane.
Select the Reuse block outputs check box.
Select the Minimize data copies between local and global variables check box.
Enable expression folding by selecting Eliminate superfluous local variables (expression folding).
The Optimization > Signals and Parameters pane appears in the next figure. All expression folding related options are selected, as shown.

![]() | Generate Code With Buffer Optimization | Implement Logic Signals as Boolean Data | ![]() |

Learn more about Simulink through this collection of videos, articles, technical literature and the Getting Started with Simulink Guide.
| © 1984-2012- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |