Skip to content

Recursion broken recently by `-OoDeadValues` in mainline FPC

Summary

-OoDeadValues causes recursion based counter to not increment.

This is recent, a couple weeks ago in mainline FPC -OoDeadValues was not causing recursion to fail.

System Information

  • Operating system: Linux (Arch Linux)
  • Processor architecture: x86-64
  • Compiler version: 3.3.1-12090-g12aa4860
  • Device: Computer

Steps to reproduce

Given the following:

procedure pr1(const i, final: integer);
  var i0: integer;

  procedure pr2(const i1: integer);
    begin
      if i1 > final then exit;
      if i0 > (final*2) then exit;
      writeln(i0, ' ', i1);
      inc(i0);
      pr2(i1+1);
    end;
  begin
    i0 := i;
    pr2(i);
  end;

begin
  pr1(1, 5);
end.

Compiled as follows:

fpc -gl -gw3 -Xi -Sm -Sg -Os -Mobjfpc -Sh \
        -MfunctionReferences -ManonymousFunctions -MadvancedRecords \
        -McVar -MimplicitFunctionSpecialization -MhintDirective \
        -OoPeepHole -OoTailRec -OoRemoveEmptyProcs -OoConstProp \
        -vnewh -Sewh \
        -OoDFA -OoCSE -OoUnusedPara -OoDeadStore \
        -OoDeadValues \
        ./recurse.pas

With this compiler output:

Hint: Start of reading config file /home/dev/fpc_usr/lib/fpc/etc/fpc.cfg
Hint: End of reading config file /home/dev/fpc_usr/lib/fpc/etc/fpc.cfg
Free Pascal Compiler version 3.3.1 [2022/11/08] for x86_64
Copyright (c) 1993-2022 by Florian Klaempfl and others
Target OS: Linux for x86-64
Compiling ./recurse.pas
Linking recurse
21 lines compiled, 0.1 sec, 156848 bytes code, 1139976 bytes data
2 hint(s) issued

What is the current bug behavior?

When run, produces the following:

1 1
2 1
3 1
4 1
5 1
6 1
7 1
8 1
9 1
10 1

i1 is not incremented.

Terminal condition for the recursion is supposed be based on i1.

Instead it is based on the fallback condition for i0.

What is the expected (correct) behavior?

When compiled the same as above but without the -OoDeadValues, when run the following is produced:

1 1
2 2
3 3
4 4
5 5

i1 is incremented.

Terminal condition for the recursion is correctly based on i1

Fallback terminal condition for the recursion based on i0 is not needed.

When compiled as follows with stable released FPC:

fpc \
        -gl -gw3 -Xi -Sm -Sg -Os -Mobjfpc -Sh \
        -MadvancedRecords \
        -McVar -MhintDirective \
        -OoPeepHole -OoTailRec -OoRemoveEmptyProcs -OoConstProp \
        -OoDFA -OoCSE -OoDeadStore -OoDeadValues \
        -vnewh -Sewh ./recurse.pas

With the following compiler output:

Hint: Start of reading config file /etc/fpc.cfg
Hint: End of reading config file /etc/fpc.cfg
Free Pascal Compiler version 3.2.2 [2022/08/31] for x86_64
Copyright (c) 1993-2021 by Florian Klaempfl and others
Target OS: Linux for x86-64
Compiling ./recurse.pas
Linking recurse
21 lines compiled, 0.1 sec
2 hint(s) issued

Produces the following when run:

1 1
2 2
3 3
4 4
5 5
Edited by bogen85
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information