Recent

Author Topic: {$OPTIMIZATION REMOVEEMPTYPROCS} and nested procedures  (Read 1531 times)

440bx

  • Hero Member
  • *****
  • Posts: 3946
{$OPTIMIZATION REMOVEEMPTYPROCS} and nested procedures
« on: April 25, 2020, 10:24:54 pm »
Hello,

{$OPTIMIZATION REMOVEEMPTYPROCS}  seems to work as expected when the empty procedure is _not_ nested.  When the procedure is nested, there does not seem to be a way to convince the compiler to remove it.

consider the following example:
Code: Pascal  [Select][+][-]
  1. {$OPTIMIZATION REMOVEEMPTYPROCS}
  2.  
  3.   // {$DEFINE TRACK_ERROR_LINE}
  4.  
  5. program EmptyProc;
  6.  
  7. procedure OuterProc();
  8.   var
  9.     NotNeeded1, NotNeeded2 : integer;
  10.     p                      : pchar;
  11.  
  12.   procedure TrackErrorLine();
  13.   begin
  14.     {$IFDEF TRACK_ERROR_LINE} writeln('Error line : ', p); {$ENDIF}
  15.   end;
  16.  
  17.  
  18.   procedure Aprocedure(var x : integer);
  19.   var
  20.     a, b, c : integer;
  21.  
  22.   begin
  23.     { give the compiler something to do }
  24.  
  25.     x := a * b + c;
  26.  
  27.     { do plenty of other stuff  }
  28.  
  29.     {$IFDEF TRACK_ERROR_LINE} p := {$I %LINE%}; {$ENDIF}
  30.     TrackErrorLine();
  31.   end;
  32.  
  33. begin
  34.   Aprocedure(NotNeeded1);
  35. end;
  36.  
  37.  
  38. begin
  39.   OuterProc();
  40.  
  41.  
  42.   writeln('press enter/return to end this program');
  43.   readln;
  44. end.
Changing the code to remove OuterProc (and include a call to Aprocedure instead of OuterProc) thereby making the TrackErrorLine() procedure not nested will yield the desired and expected results but, not otherwise.

Suggestions on how to convince the compiler to remove the empty nested proc are most welcome.

Thank you.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

jamie

  • Hero Member
  • *****
  • Posts: 6091
Re: {$OPTIMIZATION REMOVEEMPTYPROCS} and nested procedures
« Reply #1 on: April 26, 2020, 02:56:39 am »
Well as I hear quite often, "Memory is cheap, why worry"  :D
The only true wisdom is knowing you know nothing

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: {$OPTIMIZATION REMOVEEMPTYPROCS} and nested procedures
« Reply #2 on: April 26, 2020, 03:26:48 am »
Hi!

With the memory and the CPU cycles which are today needed to make a moving window semi-transparent they flew 1969 to the moon .....

Winni

PascalDragon

  • Hero Member
  • *****
  • Posts: 5446
  • Compiler Developer
Re: {$OPTIMIZATION REMOVEEMPTYPROCS} and nested procedures
« Reply #3 on: April 26, 2020, 12:33:00 pm »
Suggestions on how to convince the compiler to remove the empty nested proc are most welcome.

By reporting a bug.

The problem is twofold:
- a nested routine is not detected as empty, because it always sets up its frame pointer to the parent's frame
- nested routines are processed after the containing routine, so the compiler has not yet run the detection whether the routine is empty

440bx

  • Hero Member
  • *****
  • Posts: 3946
Re: {$OPTIMIZATION REMOVEEMPTYPROCS} and nested procedures
« Reply #4 on: April 26, 2020, 01:06:07 pm »
By reporting a bug.
Done.  Ticket : 0036977
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

 

TinyPortal © 2005-2018