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
For the Assertion block, open the block parameters dialog box.
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 24.226s 1 of 1 models built (0 models already up to date) Build duration: 0h 0m 28.864s
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.
For the Assertion block, open the Block Parameters dialog box.
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 9.2354s 1 of 1 models built (0 models already up to date) Build duration: 0h 0m 11.031s
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)