View Issue Details

IDProjectCategoryView StatusLast Update
0025587FPCCompilerpublic2021-04-13 17:15
ReporterMartin Friebe Assigned To 
PrioritynormalSeverityminorReproducibilityalways
Status newResolutionopen 
Platformw32OSwin 
Summary0025587: peephole new opt for: load and immediately overwrite register
DescriptionThe code below compiled with -O3 or -O4 produces

# [13] begin
# Var i located in register eax
# [14] i := 1;
    movl $1,%eax
# Var i located in register eax
# [15] i := i + 1;
    movl $2,%eax

The first movl can be dropped.

Attached patch does this.

It misses the comment about the allocated register though. (any hint how to identify?)
AFTER:
# [13] begin
# Var i located in register eax
# Var i located in register eax
# [15] i := i + 1;
    movl $2,%eax


The 2nd patch can (hopefully) be used if issue 0025586 has been applied first.
Additional Informationprogram project1;
{$mode objfpc}
type
  TFoo = class
    o1,o2,o3,o4,o5: TFoo;
    procedure Abc; virtual;
  end;

procedure TFoo.Abc;
var
  i: Integer;
begin
  i := 1;
  i := i + 1;
end;

begin
end.
TagsNo tags attached.
Fixed in Revision
FPCOldBugId0
FPCTarget
Attached Files

Activities

Martin Friebe

2014-01-23 23:13

manager  

popt386_move_twice_to_same_reg.patch (1,175 bytes)   
Index: compiler/i386/popt386.pas
===================================================================
--- compiler/i386/popt386.pas	(revision 26519)
+++ compiler/i386/popt386.pas	(working copy)
@@ -1369,6 +1369,18 @@
                                 end
                             end
                           else
+                            if (taicpu(p).oper[1]^.typ = top_reg) and
+                               (taicpu(hp1).oper[1]^.typ = top_reg) and
+                               (taicpu(p).opsize = taicpu(hp1).opsize) and
+                               (taicpu(p).oper[1]^.reg = taicpu(hp1).oper[1]^.reg) and
+                               not(RegInOp(getsupreg(taicpu(p).oper[1]^.reg),taicpu(hp1).oper[0]^)) then
+                               { We have mov xxx, reg1  mov yyy, reg1 }
+                              begin
+                                asml.remove(p);
+                                p.free;
+                                p := hp1;
+                              end
+                            else
 (*                          {movl [mem1],reg1
                             movl [mem1],reg2
                             to:

Martin Friebe

2014-01-23 23:13

manager  

popt386_move_twice_to_same_reg__after_0025586.patch (1,267 bytes)   
Index: compiler/i386/popt386.pas
===================================================================
--- compiler/i386/popt386.pas	(revision 26519)
+++ compiler/i386/popt386.pas	(working copy)
@@ -1369,6 +1369,18 @@
                                 end
                             end
                           else
+                            if (taicpu(p).oper[1]^.typ = top_reg) and
+                               (taicpu(hp1).oper[1]^.typ = top_reg) and
+                               (taicpu(p).opsize = taicpu(hp1).opsize) and
+                               (taicpu(p).oper[1]^.reg = taicpu(hp1).oper[1]^.reg) and
+                               not(RegInOp(getsupreg(taicpu(p).oper[1]^.reg),taicpu(hp1).oper[0]^)) then
+                               { We have mov xxx, reg1  mov yyy, reg1 }
+                              begin
+                                asml.remove(p);
+                                p.free;
+                                p := hp1;
+                              end
+                            else
                          {movl [mem1],reg1     to     movl [mem1],reg1
                           movl [mem1],reg2            movl reg1,reg2 }
                             if (taicpu(p).oper[0]^.typ = top_ref) and

Jonas Maebe

2014-01-23 23:45

manager   ~0072622

Last edited: 2014-01-24 00:24

View 2 revisions

This is not for the peephole optimizer, but for the combination of constant propagation and dead store elimination. See http://wiki.freepascal.org/FPC_New_Features_Trunk#Constant_propagation and http://wiki.freepascal.org/FPC_New_Features_Trunk#Dead_store_elimination. That code is moreover architecture-independent.

ravi dion

2021-04-13 17:15

reporter   ~0130348

So could be closed?

Issue History

Date Modified Username Field Change
2014-01-23 23:13 Martin Friebe New Issue
2014-01-23 23:13 Martin Friebe File Added: popt386_move_twice_to_same_reg.patch
2014-01-23 23:13 Martin Friebe File Added: popt386_move_twice_to_same_reg__after_0025586.patch
2014-01-23 23:45 Jonas Maebe Note Added: 0072622
2014-01-24 00:24 Jonas Maebe Note Edited: 0072622 View Revisions
2021-04-13 17:15 ravi dion Note Added: 0130348