Explorar o código

+ TX86AsmOptimizer.OptPass1VMOVAP for i386 and x86-64
+ new unit aoptutils which helpers for the assembler optimizer

git-svn-id: trunk@33587 -

florian %!s(int64=9) %!d(string=hai) anos
pai
achega
bd54a11f1c
Modificáronse 5 ficheiros con 168 adicións e 8 borrados
  1. 1 0
      .gitattributes
  2. 49 0
      compiler/aoptutils.pas
  3. 5 1
      compiler/i386/aoptcpu.pas
  4. 108 4
      compiler/x86/aoptx86.pas
  5. 5 3
      compiler/x86_64/aoptcpu.pas

+ 1 - 0
.gitattributes

@@ -57,6 +57,7 @@ compiler/aoptbase.pas svneol=native#text/plain
 compiler/aoptcs.pas svneol=native#text/plain
 compiler/aoptda.pas svneol=native#text/plain
 compiler/aoptobj.pas svneol=native#text/plain
+compiler/aoptutils.pas svneol=native#text/pascal
 compiler/arm/aasmcpu.pas svneol=native#text/plain
 compiler/arm/agarmgas.pas svneol=native#text/plain
 compiler/arm/aoptcpu.pas svneol=native#text/plain

+ 49 - 0
compiler/aoptutils.pas

@@ -0,0 +1,49 @@
+{
+    Copyright (c) 1998-2016 by Florian Klaempfl and Jonas Maebe
+
+    This unit contains helper procedures for the assembler peephole optimizer
+
+    This program is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 2 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program; if not, write to the Free Software
+    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+
+ ****************************************************************************
+}
+
+unit aoptutils;
+
+{$i fpcdefs.inc}
+
+  interface
+
+    uses
+      aasmtai,aasmcpu;
+
+    function MatchOpType(const p : taicpu;type0: toptype) : Boolean;
+    function MatchOpType(const p : taicpu;type0,type1 : toptype) : Boolean;
+
+  implementation
+
+    function MatchOpType(const p : taicpu; type0: toptype) : Boolean;
+      begin
+        Result:=(p.oper[0]^.typ=type0);
+      end;
+
+
+    function MatchOpType(const p : taicpu; type0,type1 : toptype) : Boolean;
+      begin
+        Result:=(p.oper[0]^.typ=type0) and (p.oper[0]^.typ=type1);
+      end;
+
+end.
+

+ 5 - 1
compiler/i386/aoptcpu.pas

@@ -2145,6 +2145,10 @@ begin
                         else if DoSubAddOpt(p) then
                           continue
                     end;
+                  A_VMOVAPS,
+                  A_VMOVAPD:
+                    if OptPass1VMOVAP(p) then
+                      continue;
                 end;
             end; { if is_jmp }
           end;
@@ -2603,7 +2607,7 @@ begin
                     end;
                 end;
               A_MOV:
-                PostPeepholeOpMov(p);
+                PostPeepholeOptMov(p);
               A_MOVZX:
                 { if register vars are on, it's possible there is code like }
                 {   "cmpl $3,%eax; movzbl 8(%ebp),%ebx; je .Lxxx"           }

+ 108 - 4
compiler/x86/aoptx86.pas

@@ -38,12 +38,14 @@ unit aoptx86;
       TX86AsmOptimizer = class(TAsmOptimizer)
         function RegLoadedWithNewValue(reg : tregister; hp : tai) : boolean; override;
       protected
-        procedure PostPeepholeOpMov(const p : tai);
+        procedure PostPeepholeOptMov(const p : tai);
+        function OptPass1VMOVAP(var p : tai) : boolean;
       end;
 
     function MatchInstruction(const instr: tai; const op: TAsmOp; const opsize: topsizes): boolean;
     function MatchInstruction(const instr: tai; const op1,op2: TAsmOp; const opsize: topsizes): boolean;
     function MatchInstruction(const instr: tai; const op1,op2,op3: TAsmOp; const opsize: topsizes): boolean;
+    function MatchInstruction(const instr: tai; const ops: array of TAsmOp; const opsize: topsizes): boolean;
 
     function MatchOperand(const oper: TOper; const reg: TRegister): boolean; inline;
     function MatchOperand(const oper: TOper; const a: tcgint): boolean; inline;
@@ -59,7 +61,8 @@ unit aoptx86;
 
     uses
       verbose,
-      aasmcpu;
+      aasmcpu,
+      aoptobj;
 
     function MatchInstruction(const instr: tai; const op: TAsmOp; const opsize: topsizes): boolean;
       begin
@@ -93,6 +96,25 @@ unit aoptx86;
       end;
 
 
+    function MatchInstruction(const instr : tai;const ops : array of TAsmOp;
+     const opsize : topsizes) : boolean;
+      var
+        op : TAsmOp;
+      begin
+        result:=false;
+        for op in ops do
+          begin
+            if (instr.typ = ait_instruction) and
+               (taicpu(instr).opcode = op) and
+               ((opsize = []) or (taicpu(instr).opsize in opsize)) then
+               begin
+                 result:=true;
+                 exit;
+               end;
+          end;
+      end;
+
+
     function MatchOperand(const oper: TOper; const reg: TRegister): boolean; inline;
       begin
         result := (oper.typ = top_reg) and (oper.reg = reg);
@@ -174,10 +196,14 @@ unit aoptx86;
             (p.opcode = A_LEA) or
             (p.opcode = A_VMOVSS) or
             (p.opcode = A_VMOVSD) or
+            (p.opcode = A_VMOVAPD) or
+            (p.opcode = A_VMOVAPS) or
             (p.opcode = A_VMOVQ) or
             (p.opcode = A_MOVSS) or
             (p.opcode = A_MOVSD) or
-            (p.opcode = A_MOVQ)) and
+            (p.opcode = A_MOVQ) or
+            (p.opcode = A_MOVAPD) or
+            (p.opcode = A_MOVAPS)) and
            (p.oper[1]^.typ = top_reg) and
            (getsupreg(p.oper[1]^.reg) = getsupreg(reg)) and
            ((p.oper[0]^.typ = top_const) or
@@ -190,7 +216,85 @@ unit aoptx86;
       end;
 
 
-    procedure TX86AsmOptimizer.PostPeepholeOpMov(const p : tai);
+    function TX86AsmOptimizer.OptPass1VMOVAP(var p : tai) : boolean;
+      var
+        TmpUsedRegs : TAllUsedRegs;
+        hp1,hp2 : tai;
+      begin
+        result:=false;
+        if MatchOpType(taicpu(p),top_reg,top_reg) then
+          begin
+            { vmova* reg1,reg1
+              =>
+              <nop> }
+            if MatchOperand(taicpu(p).oper[0]^,taicpu(p).oper[1]^) then
+              begin
+                GetNextInstruction(p,hp1);
+                asml.Remove(p);
+                p.Free;
+                p:=hp1;
+                result:=true;
+              end
+            else if GetNextInstruction(p,hp1) then
+              begin
+                if MatchInstruction(hp1,[taicpu(p).opcode],[S_NO]) and
+                  MatchOpType(taicpu(hp1),top_reg,top_reg) and
+                  MatchOperand(taicpu(p).oper[1]^,taicpu(hp1).oper[0]^) then
+                  begin
+                    { vmova* reg1,reg2
+                      vmova* reg2,reg3
+                      dealloc reg2
+                      =>
+                      vmova* reg1,reg3 }
+                    CopyUsedRegs(TmpUsedRegs);
+                    UpdateUsedRegs(TmpUsedRegs, tai(p.next));
+                    if not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,hp1,TmpUsedRegs)) then
+                      begin
+                        taicpu(p).loadoper(1,taicpu(hp1).oper[1]^);
+                        asml.Remove(hp1);
+                        hp1.Free;
+                        result:=true;
+                      end
+                    { special case:
+                      vmova* reg1,reg2
+                      vmova* reg2,reg1
+                      =>
+                      vmova* reg1,reg2 }
+                    else if MatchOperand(taicpu(p).oper[0]^,taicpu(hp1).oper[1]^) then
+                      begin
+                        asml.Remove(hp1);
+                        hp1.Free;
+                        result:=true;
+                      end
+                  end
+                else if MatchInstruction(hp1,[A_VFMADD132PD,A_VFNMADD231SD,A_VFMADD231SD],[S_NO]) and
+                  { we mix single and double opperations here because we assume that the compiler
+                    generates vmovapd only after double operations and vmovaps only after single operations }
+                  MatchOperand(taicpu(p).oper[1]^,taicpu(hp1).oper[2]^) and
+                  GetNextInstruction(hp1,hp2) and
+                  MatchInstruction(hp2,A_VMOVAPD,A_VMOVAPS,[S_NO]) and
+                  MatchOperand(taicpu(p).oper[0]^,taicpu(hp2).oper[1]^) then
+                  begin
+                    CopyUsedRegs(TmpUsedRegs);
+                    UpdateUsedRegs(TmpUsedRegs, tai(p.next));
+                    UpdateUsedRegs(TmpUsedRegs, tai(hp1.next));
+                    if not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,hp2,TmpUsedRegs))
+                     then
+                      begin
+                        taicpu(hp1).loadoper(2,taicpu(p).oper[0]^);
+                        asml.Remove(p);
+                        p.Free;
+                        asml.Remove(hp2);
+                        hp2.Free;
+                        p:=hp1;
+                      end;
+                  end;
+              end;
+          end;
+      end;
+
+
+    procedure TX86AsmOptimizer.PostPeepholeOptMov(const p : tai);
       begin
        if MatchOperand(taicpu(p).oper[0]^,0) and
           (taicpu(p).oper[1]^.typ = Top_Reg) and

+ 5 - 3
compiler/x86_64/aoptcpu.pas

@@ -517,8 +517,7 @@ begin
                 if GetNextInstruction(p, hp1) and
                   (tai(hp1).typ = ait_instruction) and
                   (taicpu(hp1).opcode = A_AND) and
-                  (taicpu(hp1).oper[0]^.typ = Top_Const) and
-                  (taicpu(hp1).oper[1]^.typ = Top_Reg) and
+                  MatchOpType(taicpu(hp1),top_const,top_reg) and
                   (taicpu(hp1).oper[1]^.reg =
                   taicpu(p).oper[1]^.reg) then
                   begin
@@ -577,6 +576,9 @@ begin
                 end;
             end;
           end;
+        A_VMOVAPS,
+        A_VMOVAPD:
+          result:=OptPass1VMOVAP(p);
         A_VDIVSD,
         A_VDIVSS,
         A_VSUBSD,
@@ -617,7 +619,7 @@ end;
             begin
               case taicpu(p).opcode of
                 A_MOV:
-                  PostPeepholeOpMov(p);
+                  PostPeepholeOptMov(p);
               end;
             end;
         end;