[fpc-devel] optimizing the peephole optimizer?

Martin Frb lazarus at mfriebe.de
Wed Jan 22 12:47:34 CET 2014

On 22/01/2014 00:46, Ralf Quint wrote:
> On 1/21/2014 4:23 PM, Martin Frb wrote:
> Sorry, but those two code blocks are NOT identical as you claim.
> At least in the snippets below, GetNextInstruction is called at least 
> twice in the OLD snippet, not only once as your supposed NEW snippet 
> does. It is called once for either the ffo1 or the ffo2 case and 
> again, for the the ffo3 case which is executed after either ffo1 or ffo2.

Yes they change the number of invocations.
In the real code, there is at least 1 if block where GetNextInstruction 
is the first condition. I did write:
- Old/ current code between 1 and 5 invocations
- New code exactly 1
GetNextInstruction is side effect free, it is safe to change the amount 
of times it is called

You are however right.

If ffo1 or ffo2 changes the next instruction, then it must update the 
result of  GetNextInstruction  too (unless it exits the block with 
"continue" as some, but not all of the blocks do)

This may be a good reason (unless it is known/remembered) to  check, why 
some of the code that applies changes ends with a continue, and other 
does not (indent or oversight?)

This has at least 2 effects:
- without continue in ffo1/ffo2, it will immediately test for ffo3
- with    continue in ffo1/ffo2, it will test for ffo3 in the NEXT loop 
(since p(the  current instruction) is kept.

- with continue the result of the optimization is tested for ALL 
peephole opts
- without continue it is only tested for ffo3

In case 2, the case "with continue" appears to be preferable. Unless:
- it is known that this would lead to invalid code (but then, that might 
also happen if the code after round 1 occurs in in-optimized code)
- it is known that no other opt can possible apply (and this is 
reviewed, each time a new one is added). But then it would do no harm to 
test anyway.

>> NEW:
>> If GetNextInstruction(p, hp1) then Begin
>>   if ffo1 then
>>     else
>>       if ffo2  then
>>       ;
>>   if ffo3 then
>> end;
>> OLD:
>>   if ffo1 and
>>       GetNextInstruction(p, hp1) then
>>     else
>>       if ffo2   and
>>           GetNextInstruction(p, hp1) then
>>       ;
>>   if ffo3 and
>>      GetNextInstruction(p, hp1) then
>> _______________________________________________
>> fpc-devel maillist  -  fpc-devel at lists.freepascal.org
>> http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel
> ---
> This email is free from viruses and malware because avast! Antivirus 
> protection is active.
> http://www.avast.com
> _______________________________________________
> fpc-devel maillist  -  fpc-devel at lists.freepascal.org
> http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel

More information about the fpc-devel mailing list