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.
In the model rtwdemo_deadpathElim, 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 = 'rtwdemo_deadpathElim';
open_system(model);
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');
Create a temporary folder for the build and inspection process.
currentDir = pwd; [~,cgDir] = rtwdemodir();
Build the model.
rtwbuild(model)
### Starting build procedure for: rtwdemo_deadpathElim ### Successful completion of build procedure for: rtwdemo_deadpathElim Build Summary Top model targets built: Model Action Rebuild Reason ===================================================================================================== rtwdemo_deadpathElim Code generated and compiled Code generation information file does not exist. 1 of 1 models built (0 models already up to date) Build duration: 0h 0m 6.0566s
Because the Assertion block is enabled, these lines of rtwdemo_deadpathElim.c
include code for the Gain and Assertion blocks.
cfile = fullfile(cgDir,'rtwdemo_deadpathElim_grt_rtw','rtwdemo_deadpathElim.c'); rtwdemodbtype(cfile,'/* Model step', '/* Model initialize function */', 0, 1);
void rtwdemo_deadpathElim_step(void) { /* Sum: '<Root>/Sum1' incorporates: * Constant: '<Root>/Constant1' * Inport: '<Root>/In1' */ rtwdemo_deadpathElim_Y.Out1 = rtwdemo_deadpathElim_U.In1 + 1.0; /* Assertion: '<Root>/Assert1' incorporates: * Gain: '<Root>/G1' */ utAssert(2.0 * rtwdemo_deadpathElim_Y.Out1 != 0.0); }
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.
rtwbuild(model)
### Starting build procedure for: rtwdemo_deadpathElim ### Successful completion of build procedure for: rtwdemo_deadpathElim Build Summary Top model targets built: Model Action Rebuild Reason ==================================================================================== rtwdemo_deadpathElim Code generated and compiled Generated code was out of date. 1 of 1 models built (0 models already up to date) Build duration: 0h 0m 5.5345s
Because the Assertion block is disabled, these lines of rtwdemo_deadpathElim.c
do not include code for the Gain and Assertion blocks.
rtwdemodbtype(cfile,'/* Model step', '/* Model initialize function */', 0, 1);
void rtwdemo_deadpathElim_step(void) { /* Outport: '<Root>/Out1' incorporates: * Constant: '<Root>/Constant1' * Inport: '<Root>/In1' * Sum: '<Root>/Sum1' */ rtwdemo_deadpathElim_Y.Out1 = rtwdemo_deadpathElim_U.In1 + 1.0; }
bdclose(model) rtwdemoclean; cd(currentDir)
For another example of how the code generator eliminates dead code paths in the generated code, see rtwdemo_deadpath.