Main Content

Eliminate Dead Code Paths in Generated Code

This example shows how the code generator eliminates dead (that is, unused) code paths from generated code. This optimization increases execution speed and conserves ROM and RAM consumption.

Example

In the DeadPathElimination model, the signal leaving the Sum block divides into two separate code paths. The top path is not a dead code path. If the user disables the Assertion block, the bottom path becomes a dead code path.

model = 'DeadPathElimination';
open_system(model);

Generate Code with an Enabled Assertion Block

  1. For the Assertion block, open the block parameters dialog box.

  2. Select the Enable assertion box. Alternatively, use the command-line API to enable the Assertion block.

set_param([model '/Assert1'], 'Enabled', 'on');

Build the model.

slbuild(model)
### Starting build procedure for: DeadPathElimination
### Successful completion of build procedure for: DeadPathElimination

Build Summary

Top model targets:

Model                Build Reason                                         Status                        Build Duration
======================================================================================================================
DeadPathElimination  Information cache folder or artifacts were missing.  Code generated and compiled.  0h 0m 20.692s 

1 of 1 models built (0 models already up to date)
Build duration: 0h 0m 25.115s

Because the Assertion block is enabled, these lines of DeadPathElimination.c include code for the Gain and Assertion blocks.

cfile = fullfile('DeadPathElimination_grt_rtw','DeadPathElimination.c');
coder.example.extractLines(cfile,'/* Model step', '/* Model initialize function */', 0, 1);
void DeadPathElimination_step(void)
{
  /* Outport: '<Root>/Out1' incorporates:
   *  Constant: '<Root>/Constant1'
   *  Inport: '<Root>/In1'
   *  Sum: '<Root>/Sum1'
   */
  DeadPathElimination_Y.Out1 = DeadPathElimination_U.In1 + 1.0;

  /* Assertion: '<Root>/Assert1' incorporates:
   *  Constant: '<Root>/Constant1'
   *  Gain: '<Root>/G1'
   *  Inport: '<Root>/In1'
   *  Sum: '<Root>/Sum1'
   */
  utAssert(2.0 * (DeadPathElimination_U.In1 + 1.0) != 0.0);
}

Generate Code with a Disabled Assertion Block

Disable the Assertion block to generate a dead code path. The code generator detects the dead code path and eliminates it from the generated code.

  1. For the Assertion block, open the Block Parameters dialog box.

  2. Deselect the Enable assertion box.

Alternatively, use the command-line API to disable the Assertion block.

set_param([model '/Assert1'], 'Enabled', 'off');

Build the model.

slbuild(model)
### Starting build procedure for: DeadPathElimination
### Successful completion of build procedure for: DeadPathElimination

Build Summary

Top model targets:

Model                Build Reason                     Status                        Build Duration
==================================================================================================
DeadPathElimination  Generated code was out of date.  Code generated and compiled.  0h 0m 10.27s  

1 of 1 models built (0 models already up to date)
Build duration: 0h 0m 11.498s

Because the Assertion block is disabled, these lines of DeadpathElim.c do not include code for the Gain and Assertion blocks.

coder.example.extractLines(cfile,'/* Model step', '/* Model initialize function */', 0, 1);
void DeadPathElimination_step(void)
{
  /* Outport: '<Root>/Out1' incorporates:
   *  Constant: '<Root>/Constant1'
   *  Inport: '<Root>/In1'
   *  Sum: '<Root>/Sum1'
   */
  DeadPathElimination_Y.Out1 = DeadPathElimination_U.In1 + 1.0;
}

Close the Model

bdclose(model)

Related Topics