Browse Source

* optimize tests for a single bit "(a and one_bit_mask_const) = <> one_bit_mask_const" into "(a and one_bit_mask_const) <> = 0" to enable further CPU specific optimizations.

Yuriy Sydorov 4 years ago
parent
commit
6a3f32ba69
1 changed files with 50 additions and 1 deletions
  1. 50 1
      compiler/nadd.pas

+ 50 - 1
compiler/nadd.pas

@@ -590,7 +590,7 @@ implementation
 
 
       var
       var
         t,vl,lefttarget,righttarget: tnode;
         t,vl,lefttarget,righttarget: tnode;
-        lt,rt   : tnodetype;
+        lt,rt,nt : tnodetype;
         hdef,
         hdef,
         rd,ld   , inttype: tdef;
         rd,ld   , inttype: tdef;
         rv,lv,v : tconstexprint;
         rv,lv,v : tconstexprint;
@@ -1726,6 +1726,55 @@ implementation
                  @IsAndOrAndNot,@TransformAndOrAndNot,Result) then
                  @IsAndOrAndNot,@TransformAndOrAndNot,Result) then
                  exit;
                  exit;
              end;
              end;
+            { optimize tests for a single bit:
+              (a and one_bit_mask_const) = <> one_bit_mask_const
+
+              into
+
+              (a and one_bit_mask_const) <> = 0
+            }
+            if (nodetype in [equaln,unequaln]) then
+              begin
+                if (lt=andn) and (rt=ordconstn) then
+                  begin
+                    t:=left;
+                    cr:=tordconstnode(right).value;
+                  end
+                else
+                  if (rt=andn) and (lt=ordconstn) then
+                    begin
+                      t:=right;
+                      cr:=tordconstnode(left).value;
+                    end
+                  else
+                    begin
+                      t:=nil;
+                      cr:=0;
+                    end;
+                if (t<>nil) and (PopCnt(cr) = 1) then
+                  begin
+                    if is_constintnode(taddnode(t).left) then
+                      vl:=taddnode(t).left
+                    else
+                      if is_constintnode(taddnode(t).right) then
+                        vl:=taddnode(t).right
+                      else
+                        vl:=nil;
+                    if (vl<>nil) and (tordconstnode(vl).value=cr) then
+                      begin
+                        if nodetype=equaln then
+                          nt:=unequaln
+                        else
+                          nt:=equaln;
+                        result:=caddnode.create(nt,t,cordconstnode.create(0,vl.resultdef,false));
+                        if t=left then
+                          left:=nil
+                        else
+                          right:=nil;
+                        exit;
+                      end;
+                  end;
+              end;
           end;
           end;
       end;
       end;