[fpc-devel] peephole patch - Re: probably incorrect debug-line-info generated by fpc 2.6.2 and trunk

Martin Frb lazarus at mfriebe.de
Wed Jan 22 02:18:17 CET 2014

Starting one by one.

This is the simplest of the changes.  In addition to the patch in this 
message, what is needed to get it committed?

On 10/11/2013 23:52, Jonas Maebe wrote:
>>> On 10 Nov 2013, at 22:42, Martin wrote:
>>>> Why is the
>>>> movl    %eax,-12(%ebp)
>>>> generated at the start of i:=i+1
> The second instruction originally belonged to the second statement and hence has its line number info.
>> I only wonder, if despite the fact that O1 does not guarantee, if in this case it would be worth to consider it a bug (and intend to fix it)?
> If someone wants to change it, they're free to do so. My days of working on the peephole optimisers are over.

Well I gave it a try, and now would appreciate feedback. (patch at end 
of message).

It basically just copies the fileinfo record to the 2nd tai statement. 
(must be the complete info (or maybe at least line and filename), for 
include files). In theory that could lead to the statement in the next 
line generating no code (if other optimizations take away whatever else 
it did. But that would be no damage as far as I can see. (not sure if it 
can even happen in real live)

Appears to work with stabs and dwarf. Also works if the next statement 
is inlined (independent of the inlined proc being from the same or a 
diff unit)

procedure Foo(var x:Integer); inline;
   begin  x := x + 1;  end;
function Bar(var x:Integer): integer; inline;
   begin  result := x + 1;  end;

   i := 1;
   i := i + 1;

   i := 1;
   i := bar(i);

   i := 1;

If the statements are on the same line, the go together anyway (also 
with inlined)
   i := 1;   i := i + 1;

And also ok, if 2nd line is in include file
   i := 1;
   {$i u1.inc}

Currently the ait_label with labeltype = alt_dbgline for debugline-info, 
are generated after the optimization. Can that be relied on? If that 
changes the fix would be completely different (would need to move the 
generated label).
Also the debug-info generating code only looks of line info of 
ait_instruction, so any skipped TAI entries (comments, ...) do not need 
an update.

There may be other similar optimizations, but one step at a time.

Not sure how (or where) to write a test. Are there examples that show, 
how to test that debug info and generated assembler are correctly 
fitting together?
(Existing tests are still running, but since this is for feedback, the 
results can be submitted later)

Index: compiler/i386/popt386.pas
--- compiler/i386/popt386.pas    (revision 26518)
+++ compiler/i386/popt386.pas    (working copy)
@@ -1401,6 +1401,8 @@
+                                  // Storing the value to memory should 
be part of the first instruction
+                                  taicpu(hp1).fileinfo := 
                        if GetNextInstruction(p, hp1) and

More information about the fpc-devel mailing list