Răsfoiți Sursa

m68k: try to optimize realconst operands into B/W/L sized integer operands. these are shorter to encode - which almost always means faster on m68k

git-svn-id: trunk@42923 -
Károly Balogh 5 ani în urmă
părinte
comite
36286441b2
1 a modificat fișierele cu 53 adăugiri și 22 ștergeri
  1. 53 22
      compiler/m68k/aoptcpu.pas

+ 53 - 22
compiler/m68k/aoptcpu.pas

@@ -39,6 +39,7 @@ unit aoptcpu;
         function PeepHoleOptPass1Cpu(var p: tai): boolean; override;
         function PeepHoleOptPass1Cpu(var p: tai): boolean; override;
 
 
         function TryToOptimizeMove(var p: tai): boolean;
         function TryToOptimizeMove(var p: tai): boolean;
+        function MaybeRealConstOperSimplify(var p: tai): boolean;
 
 
         { outputs a debug message into the assembler file }
         { outputs a debug message into the assembler file }
         procedure DebugMsg(const s: string; p: tai);
         procedure DebugMsg(const s: string; p: tai);
@@ -87,6 +88,56 @@ unit aoptcpu;
           end
           end
       end;
       end;
 
 
+    function TCpuAsmOptimizer.MaybeRealConstOperSimplify(var p: tai): boolean;
+      var
+        tmpint64: int64;
+        tmpsingle: single;
+      begin
+        result:=false;
+        if (taicpu(p).oper[0]^.typ = top_realconst) then
+          begin
+            { if we work with actual integers, turn the operand into one }
+            if frac(taicpu(p).oper[0]^.val_real) = 0 then
+              begin
+                tmpint64:=trunc(taicpu(p).oper[0]^.val_real);
+                if (high(shortint) >= tmpint64) and (low(shortint) <= tmpint64) then
+                  begin
+                    taicpu(p).opsize := S_B;
+                    taicpu(p).oper[0]^.typ:=top_const;
+                  end
+                else
+                  if (high(smallint) >= tmpint64) and (low(smallint) <= tmpint64) then
+                    begin
+                      taicpu(p).opsize := S_W;
+                      taicpu(p).oper[0]^.typ:=top_const;
+                    end
+                  else
+                    if (high(longint) >= tmpint64) and (low(longint) <= tmpint64) then
+                      begin
+                        taicpu(p).opsize := S_L;
+                        taicpu(p).oper[0]^.typ:=top_const;
+                      end;
+                if (taicpu(p).oper[0]^.typ) = top_const then
+                  begin
+                    DebugMsg('Optimizer: FPU real const to integer',p);
+                    taicpu(p).oper[0]^.val:=tmpint64;
+                    result:=true;
+                  end;
+              end
+            else
+              begin
+                tmpsingle:=taicpu(p).oper[0]^.val_real;
+                if (taicpu(p).opsize = S_FD) and
+                   ((taicpu(p).oper[0]^.val_real - tmpsingle) = 0.0) then
+                  begin
+                    DebugMsg('Optimizer: FPU real const to lesser precision',p);
+                    taicpu(p).opsize:=S_FS;
+                    result:=true;
+                  end;
+              end;
+          end;
+      end;
+
     function TCpuAsmOptimizer.RegLoadedWithNewValue(reg: tregister; hp: tai): boolean;
     function TCpuAsmOptimizer.RegLoadedWithNewValue(reg: tregister; hp: tai): boolean;
       var
       var
         p: taicpu;
         p: taicpu;
@@ -252,7 +303,6 @@ unit aoptcpu;
     var
     var
       next: tai;
       next: tai;
       tmpref: treference;
       tmpref: treference;
-      tmpsingle: single;
     begin
     begin
       result:=false;
       result:=false;
       case p.typ of
       case p.typ of
@@ -359,16 +409,7 @@ unit aoptcpu;
                         result:=true;
                         result:=true;
                       end
                       end
                     else
                     else
-                      begin
-                        tmpsingle:=taicpu(p).oper[0]^.val_real;
-                        if (taicpu(p).opsize = S_FD) and
-                           ((taicpu(p).oper[0]^.val_real - tmpsingle) = 0.0) then
-                          begin
-                            DebugMsg('Optimizer: FCMP const to lesser precision',p);
-                            taicpu(p).opsize:=S_FS;
-                            result:=true;
-                          end;
-                      end;
+                      result:=result or MaybeRealConstOperSimplify(p);
                   end;
                   end;
               A_FMOVE,A_FMUL,A_FADD,A_FSUB,A_FDIV:
               A_FMOVE,A_FMUL,A_FADD,A_FSUB,A_FDIV:
                   begin
                   begin
@@ -377,17 +418,7 @@ unit aoptcpu;
                         result:=true;
                         result:=true;
                         exit;
                         exit;
                       end;
                       end;
-                    if (taicpu(p).oper[0]^.typ = top_realconst) then
-                      begin
-                        tmpsingle:=taicpu(p).oper[0]^.val_real;
-                        if (taicpu(p).opsize = S_FD) and
-                           ((taicpu(p).oper[0]^.val_real - tmpsingle) = 0.0) then
-                          begin
-                            DebugMsg('Optimizer: FMOVE/FMUL/FADD/FSUB/FDIV const to lesser precision',p);
-                            taicpu(p).opsize:=S_FS;
-                            result:=true;
-                          end;
-                      end;
+                    result:=result or MaybeRealConstOperSimplify(p);
                   end;
                   end;
             end;
             end;
           end;
           end;