浏览代码

+ replace appropriate if statements by min/max intrinsics
+ support min/max intrinsic on xtensa

git-svn-id: trunk@47693 -

florian 4 年之前
父节点
当前提交
57dc416d96
共有 4 个文件被更改,包括 158 次插入0 次删除
  1. 22 0
      compiler/defutil.pas
  2. 44 0
      compiler/nflw.pas
  3. 29 0
      compiler/nutils.pas
  4. 63 0
      compiler/xtensa/ncpuinl.pas

+ 22 - 0
compiler/defutil.pas

@@ -268,6 +268,12 @@ interface
     {# Returns true, if def is a 64 bit type }
     function is_64bit(def : tdef) : boolean;
 
+    { returns true, if def is a longint type }
+    function is_s32bitint(def : tdef) : boolean;
+
+    { returns true, if def is a dword type }
+    function is_u32bitint(def : tdef) : boolean;
+
     { true, if def1 and def2 are both integers of the same bit size and sign }
     function are_equal_ints(def1, def2: tdef): boolean;
 
@@ -1017,6 +1023,22 @@ implementation
       end;
 
 
+    { returns true, if def is a longint type }
+    function is_s32bitint(def : tdef) : boolean;
+      begin
+        result:=(def.typ=orddef) and
+          (torddef(def).ordtype=s32bit);
+      end;
+
+
+    { returns true, if def is a dword type }
+    function is_u32bitint(def : tdef) : boolean;
+      begin
+        result:=(def.typ=orddef) and
+          (torddef(def).ordtype=u32bit);
+      end;
+
+
     { true, if def1 and def2 are both integers of the same bit size and sign }
     function are_equal_ints(def1, def2: tdef): boolean;
       begin

+ 44 - 0
compiler/nflw.pas

@@ -1554,6 +1554,10 @@ implementation
 
 
     function tifnode.internalsimplify(warn: boolean) : tnode;
+      var
+        thenstmnt, elsestmnt: tnode;
+        in_nr: tinlinenumber;
+        paratype: tdef;
       begin
         result:=nil;
         { optimize constant expressions }
@@ -1580,6 +1584,46 @@ implementation
                     CGMessagePos(right.fileinfo,cg_w_unreachable_code);
                end;
           end;
+{$if defined(xtensa)}
+        { use min/max intrinsic? }
+        if (left.nodetype in [gtn,gten,ltn,lten]) and IsSingleStatement(right,thenstmnt) and IsSingleStatement(t1,elsestmnt) and
+          (thenstmnt.nodetype=assignn) and (elsestmnt.nodetype=assignn) and
+          not(might_have_sideeffects(left)) and
+          tassignmentnode(thenstmnt).left.isequal(tassignmentnode(elsestmnt).left) and
+          is_32bitint(tassignmentnode(thenstmnt).right.resultdef) and
+          ((tassignmentnode(thenstmnt).right.isequal(taddnode(left).left) and (tassignmentnode(elsestmnt).right.isequal(taddnode(left).right))) or
+           (tassignmentnode(thenstmnt).right.isequal(taddnode(left).right) and (tassignmentnode(elsestmnt).right.isequal(taddnode(left).left)))) then
+          begin
+            paratype:=tassignmentnode(thenstmnt).left.resultdef;
+            if (left.nodetype in [gtn,gten]) and
+              (tassignmentnode(thenstmnt).right.isequal(taddnode(left).left) and (tassignmentnode(elsestmnt).right.isequal(taddnode(left).right))) then
+              begin
+                if is_double(paratype) then
+                  in_nr:=in_max_double
+                else if is_single(paratype) then
+                  in_nr:=in_max_single
+                else if is_u32bitint(paratype) then
+                  in_nr:=in_max_dword
+                else if is_s32bitint(paratype) then
+                  in_nr:=in_max_longint;
+              end
+            else
+              begin
+                if is_double(paratype) then
+                  in_nr:=in_min_double
+                else if is_single(paratype) then
+                  in_nr:=in_min_single
+                else if is_u32bitint(paratype) then
+                  in_nr:=in_min_dword
+                else if is_s32bitint(paratype) then
+                  in_nr:=in_min_longint;
+              end;
+            Result:=cassignmentnode.create_internal(tassignmentnode(thenstmnt).left.getcopy,
+              cinlinenode.create(in_nr,false,ccallparanode.create(taddnode(left).right.getcopy,
+                    ccallparanode.create(taddnode(left).left.getcopy,nil)))
+              );
+          end;
+{$endif defined(xtensa)}
       end;
 
 

+ 29 - 0
compiler/nutils.pas

@@ -180,6 +180,9 @@ interface
       in a and the function returns true }
     function GetStatements(p : tnode;var a : array of tstatementnode) : Boolean;
 
+    { checks if p is a single statement, if yes, it is returned in s }
+    function IsSingleStatement(p : tnode;var s : tnode) : Boolean;
+
     type
       TMatchProc2 = function(n1,n2 : tnode) : Boolean is nested;
       TTransformProc2 = function(n1,n2 : tnode) : tnode is nested;
@@ -1643,6 +1646,32 @@ implementation
       end;
 
 
+    function IsSingleStatement(p: tnode; var s: tnode): Boolean;
+      begin
+        Result:=false;
+        if assigned(p) then
+          case p.nodetype of
+            blockn:
+              Result:=IsSingleStatement(tblocknode(p).statements,s);
+            statementn:
+              if not(assigned(tstatementnode(p).next)) then
+                begin
+                  Result:=true;
+                  s:=tstatementnode(p).statement;
+                end;
+            inlinen,
+            assignn,
+            calln:
+              begin
+                s:=p;
+                Result:=true;
+              end
+            else
+              ;
+          end;
+      end;
+
+
     function MatchAndTransformNodesCommutative(n1,n2 : tnode;matchproc : TMatchProc2;transformproc : TTransformProc2;var res : tnode) : Boolean;
       begin
         res:=nil;

+ 63 - 0
compiler/xtensa/ncpuinl.pas

@@ -35,6 +35,8 @@ unit ncpuinl;
         procedure second_abs_real; override;
         function first_fma: tnode; override;
         procedure second_fma; override;
+        function first_minmax: tnode; override;
+        procedure second_minmax; override;
       end;
 
   implementation
@@ -168,6 +170,67 @@ unit ncpuinl;
       end;
 
 
+    function tcpuinlinenode.first_minmax : tnode;
+      begin
+        if is_32bitint(resultdef) then
+          begin
+            expectloc:=LOC_REGISTER;
+            Result:=nil;
+          end
+        else
+          Result:=inherited first_minmax;
+      end;
+
+
+    procedure tcpuinlineNode.second_minmax;
+      var
+        paraarray : array[1..2] of tnode;
+        i: Integer;
+        ai: taicpu;
+        op: TAsmOp;
+      begin
+         if is_32bitint(resultdef) then
+           begin
+             paraarray[1]:=tcallparanode(tcallparanode(parameters).nextpara).paravalue;
+             paraarray[2]:=tcallparanode(parameters).paravalue;
+
+              for i:=low(paraarray) to high(paraarray) do
+               secondpass(paraarray[i]);
+
+             { no memory operand is allowed }
+             for i:=low(paraarray) to high(paraarray) do
+               begin
+                 if not(paraarray[i].location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
+                   hlcg.location_force_reg(current_asmdata.CurrAsmList,paraarray[i].location,
+                     paraarray[i].resultdef,resultdef,true);
+               end;
+
+             location_reset(location,LOC_REGISTER,paraarray[1].location.size);
+             location.register:=cg.getintregister(current_asmdata.CurrAsmList,location.size);
+
+             case inlinenumber of
+               in_min_dword:
+                 op:=A_MINU;
+               in_max_dword:
+                 op:=A_MAXU;
+               in_min_longint:
+                 op:=A_MIN;
+               in_max_longint:
+                 op:=A_MAX;
+               else
+                 Internalerror(2020120505);
+             end;
+
+             current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(op,
+               location.register,paraarray[1].location.register,paraarray[2].location.register));
+
+             cg.maybe_check_for_fpu_exception(current_asmdata.CurrAsmList);
+           end
+         else
+           internalerror(2020120502);
+      end;
+
+
 begin
   cinlinenode:=tcpuinlinenode;
 end.