[fpc-devel] Debugging Loop Unroll Optimization

Mattias Gaertner nc-gaertnma at netcologne.de
Thu May 17 23:51:44 CEST 2018

On Thu, 17 May 2018 18:34:04 +0200
Florian Klämpfl <florian at freepascal.org> wrote:


Apparently this mail thread confuses some users:
report https://bugs.freepascal.org/view.php?id=33753:

> ISO7185: "After a for-statement is executed, other than being left by a goto-statement, the
> control-variable shall be undefined"
> http://docwiki.embarcadero.com/RADStudio/Tokyo/en/Declarations_and_Statements_(Delphi)#For_Statements:
> "After the for statement terminates (provided this was not forced by a Break or an Exit procedure),
> the value of counter is undefined."
> So this shall not be unrolled, but is still error prone, if Result is not set after the regular end of the for loop.

To avoid misunderstandings:
The above clearly says, that using break/goto/exit is safe:

  for Result:=low to high do
    if Something(Result) then exit;

  for Result:=low to high do 
    if Something(Result) then break;

OTOH this is not safe:

> > function tfun: integer;
> > begin
> >   for Result:=1 to 10 do
> >     s:=s+1;
> > end;  


More information about the fpc-devel mailing list