Main Content

Detect Dead and Constant-Folded Code

During the simulation of your test file, the Fixed-Point Converter app detects dead code or code that is constant folded. 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 dead 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
Green

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 Dead Code?

Dead code is code that does not execute during simulation. Dead 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

  • Inadequate test bench range

  • Constant folding

Detect Dead Code

This example shows how to detect dead code in your algorithm by using the Fixed-Point Converter .

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

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

    u = 1:100;
    v = 101:200;
    
    myFunction(u,v);
    
  3. From the apps gallery, open the Fixed-Point Converter .

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

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

  6. 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 dead code.

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

    The Fixed-Point Converter generates a fixed-point function, myFunction_fixpt. The generated fixed-point code contains comments around the pieces of code identified as dead code. The Validation Results pane proposes that you use a more thorough test bench.

    When the Fixed-Point Converter detects dead 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 dead code.

  8. Close the Fixed-Point Converter .

Fix Dead Code

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

    u = 1:100;
    v = -50:2:149;
    
    myFunction(u,v);
    

  2. Reopen the Fixed-Point Converter .

  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.