Main Content

Detect Unexecuted and Constant-Folded Code

During the simulation of your test file, the MATLAB® Coder™ app detects unexecuted code or code that is constant folded. Code that is not executed by the test bench may be unreachable code or dead code. The app uses the code coverage information when translating your code from floating-point MATLAB code to fixed-point MATLAB code. Reviewing code coverage results helps you to verify that your test file is exercising the algorithm adequately.

The app inserts inline comments in the fixed-point code to mark the unexecuted and untranslated regions. It includes the code coverage information in the generated fixed-point conversion HTML report. The app editor displays a color-coded bar to the left of the code. This table describes the color coding.

Coverage Bar ColorIndicates

One of the following situations:

  • The entry-point function executes multiple times and the code executes more than one time.

  • The entry-point function executes one time and the code executes one time.

Different shades of green indicate different ranges of line execution counts. The darkest shade of green indicates the highest range.

OrangeThe entry-point function executes multiple times, but the code executes one time.
RedCode does not execute.

What Is Unexecuted Code?

Unexecuted code is code that is not executed by the test bench during simulation. Unexecuted code can result from these scenarios:

  • Defensive code containing intended corner cases that are not reached

  • Human error in the code, resulting in code that cannot be reached by any execution path, sometimes referred to as unreachable code or dead code

  • Inadequate test bench range which does not provide inputs that execute all paths in the code

  • Constant folding

Detect Unexecuted Code

This example shows how to detect code in your algorithm that is not executed by the test bench by using the MATLAB Coder app.

  1. In a local writable folder, create the function myFunction.m.

    function y = myFunction(u,v)
        for i = 1:length(u)
            if u(i) > v(i)
                tmp = u;
                v = tmp;
                y = baz(u,v);
    function y = bar(u,v)
        y = u+v;
    function y = baz(u,v)
        y = u-v;
  2. In the same folder, create a test file, myFunction_tb.

    u = 1:100;
    v = 101:200;
  3. From the apps gallery, open the MATLAB Coder app.

  4. Set Numeric Conversion to Convert to fixed point.

  5. On the Select Source Files page, browse to the myFunction file, and click Open.

  6. Click Next. On the Define Input Types page, browse to select the test file that you created, myFunction_tb. Click Autodefine Input Types.

  7. Click Next. On the Check for Run-Time Issues page, click Check for Issues.

    The app runs the myFunction_tb test file and detects no issues.

  8. Click Next. On the Convert to Fixed-Point page, click Analyze to simulate the entry-point functions, gather range information, and get proposed data types.

    The color-coded bar on the left side of the edit window indicates whether the code executes. The code in the first condition of the if-statement does not execute during simulation because u is never greater than v. The bar function never executes because the if-statement never executes. These parts of the algorithm are marked with a red bar, indicating that they are not executed by the test bench.

  9. To apply the proposed data types to the function, click Convert.

    The MATLAB Coder app generates a fixed-point function, myFunction_fixpt. The generated fixed-point code contains comments around the pieces of code identified as not being executed by the test bench. The Validation Results pane proposes that you use a more thorough test bench.

    When the MATLAB Coder app detects unexecuted code, consider editing your test file so that your algorithm is exercised over its full range. If your test file already reflects the full range of the input variables, consider editing your algorithm to eliminate the unreachable code.

  10. Close the MATLAB Coder app.

Fix Unexecuted Code

  1. Edit the test file myFunction_tb.m to include a wider range of inputs.

    u = 1:100;
    v = -50:2:149;

  2. Reopen the MATLAB Coder app.

  3. Using the same function and the edited test file, go through the conversion process again.

  4. After you click Analyze, this time the code coverage bar shows that all parts of the algorithm execute with the new test file input ranges.

    To finish the conversion process and convert the function to fixed point, click Convert.