Main Content

Eliminate superfluous local variables (Expression folding)

Specify to fold expressions into single expression

Model Configuration Pane: Code Generation / Optimization

Description

The Eliminate superfluous local variables (Expression folding) parameter specifies whether to collapse block computations into single expressions instead of generating separate code statements and storage declarations for each block in the model.

Expression folding optimizes code to minimize the computation of intermediate results at block outputs and the storage of such results in temporary buffers or variables. Expression folding improves 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.

You can use expression folding in your own inlined S-function blocks. For more information, see S-Functions That Support Expression Folding.

Category: Optimization

Dependencies

This parameter is enabled by Signal storage reuse.

Settings

on (default) | off
On
  • Enables expression folding.

  • Eliminates local variables, incorporating the information into the main code statement.

  • Improves code readability and efficiency.

Off

Disables expression folding.

Examples

expand all

This example shows how to generate code that uses expression folding.

Consider the following example model. The two inputs lead to two Gain blocks, which provide the inputs to a Product block.

Model containing two Gain blocks that lead to a Product block.

With expression folding off, in the explfld.c file, the code generator generates this code.

/* Model step function */
void exprfld_step(void)
{
  /* Gain: '<Root>/Gain' incorporates:
   *  Inport: '<Root>/In1'
   */
  exprfld_B.S1 = exprfld_P.Gain_Gain * exprfld_U.i1;
 
  /* Gain: '<Root>/Gain1' incorporates:
   *  Inport: '<Root>/In2'
   */
  exprfld_B.S2 = exprfld_P.Gain1_Gain * exprfld_U.i2;
 
  /* Outport: '<Root>/Out1' incorporates:
   *  Product: '<Root>/Product'
   */
  exprfld_Y.Out1 = exprfld_B.S1 * exprfld_B.S2;
}

There are separate code statements for both Gain blocks. Before final output, these code statements compute temporary results for the Gain blocks.

Turn on expression folding for the model, then re-generate the code. With expression folding, the code generator generates a single-line output computation, as shown in the expfld.c file. The generated comments document the block parameters that appear in the expression.

/* Model step function */
void exprfld_step(void)
{
  /* Outport: '<Root>/Out1' incorporates:
   *  Gain: '<Root>/Gain'
   *  Gain: '<Root>/Gain1'
   *  Inport: '<Root>/In1'
   *  Inport: '<Root>/In2'
   *  Product: '<Root>/Product'
   */
  exprfld_Y.Out1 = 
     exprfld_P.Gain_Gain * 
     exprfld_U.i1 * 
     (exprfld_P.Gain1_Gain * exprfld_U.i2);
}

Recommended Settings

ApplicationSetting
DebuggingOff
TraceabilityNo impact for simulation or during development
Off for production code generation
EfficiencyOn
Safety precautionNo impact

Programmatic Use

Parameter: ExpressionFolding
Type: character vector
Value: 'on' | 'off'
Default: 'on'

Version History

Introduced before R2006a

See Also