Browse Source

* optimize a-const1-const2 when const1 and const2 are real constants and fast math is on, part of fixing #39782

florian 3 years ago
parent
commit
94665a40d7
1 changed files with 18 additions and 2 deletions
  1. 18 2
      compiler/nadd.pas

+ 18 - 2
compiler/nadd.pas

@@ -1457,7 +1457,7 @@ implementation
         { slow simplifications and/or more sophisticated transformations which might make debugging harder }
         { slow simplifications and/or more sophisticated transformations which might make debugging harder }
         if cs_opt_level2 in current_settings.optimizerswitches then
         if cs_opt_level2 in current_settings.optimizerswitches then
           begin
           begin
-            if nodetype in [addn,muln] then
+            if nodetype in [addn,muln,subn] then
               begin
               begin
                 { try to fold
                 { try to fold
                             op
                             op
@@ -1470,11 +1470,27 @@ implementation
                 }
                 }
                 if (left.nodetype=nodetype) and
                 if (left.nodetype=nodetype) and
                   (((nodetype=addn) and ((rt=stringconstn) or is_constcharnode(right)) and ((taddnode(left).right.nodetype=stringconstn) or is_constcharnode(taddnode(left).right))) or
                   (((nodetype=addn) and ((rt=stringconstn) or is_constcharnode(right)) and ((taddnode(left).right.nodetype=stringconstn) or is_constcharnode(taddnode(left).right))) or
-                   ((nodetype in [addn,muln]) and (cs_opt_fastmath in current_settings.optimizerswitches) and (rt=realconstn) and (taddnode(left).right.nodetype=realconstn))
+                   ((nodetype in [addn,muln,subn]) and (cs_opt_fastmath in current_settings.optimizerswitches) and (rt=realconstn) and (taddnode(left).right.nodetype=realconstn))
                   ) and
                   ) and
                   (compare_defs(resultdef,left.resultdef,nothingn)=te_exact) then
                   (compare_defs(resultdef,left.resultdef,nothingn)=te_exact) then
                   begin
                   begin
+                    { SwapRightWithLeftLeft moves the nodes around in way that we need to insert a minus
+                      on left.right: a-b-c becomes b-c-a so we
+                      need
+                      1) insert a minus bevor b
+                      2) make the current node an add node, see below
+                    }
+                    if nodetype=subn then
+                      begin
+                        taddnode(left).right:=cunaryminusnode.create(taddnode(left).right);
+                        do_typecheckpass(taddnode(left).right);
+                      end;
                     Result:=SwapRightWithLeftLeft;
                     Result:=SwapRightWithLeftLeft;
+                    if nodetype=subn then
+                      begin
+                        Result.nodetype:=addn;
+                        do_typecheckpass(Result);
+                      end;
                     exit;
                     exit;
                   end;
                   end;