Browse Source

* do constant folding on div <const. int> div <const. int>, resolves #39301

florian 3 years ago
parent
commit
2ce34f7bd3
1 changed files with 20 additions and 1 deletions
  1. 20 1
      compiler/nmat.pas

+ 20 - 1
compiler/nmat.pas

@@ -111,6 +111,7 @@ implementation
     function tmoddivnode.simplify(forinline : boolean):tnode;
     function tmoddivnode.simplify(forinline : boolean):tnode;
       var
       var
         rv,lv : tconstexprint;
         rv,lv : tconstexprint;
+        hp: tnode;
       begin
       begin
         result:=nil;
         result:=nil;
 
 
@@ -162,8 +163,22 @@ implementation
                 left:=nil;
                 left:=nil;
                 exit;
                 exit;
               end;
               end;
+            if (nodetype=divn) and (left.nodetype=divn) and is_constintnode(tmoddivnode(left).right) and
+              { we need a type and the types must be consistent }
+              assigned(resultdef) and
+              (compare_defs(resultdef,left.resultdef,nothingn)=te_exact) then
+              begin
+                { re-use the current node so we get the result type right }
+                right:=caddnode.create_internal(muln,right,tmoddivnode(left).right.getcopy);
+                hp:=tmoddivnode(left).left.getcopy;
+                left.Free;
+                left:=hp;
+                Result:=getcopy;
+                Result.resultdef:=nil;
+                Result:=ctypeconvnode.create_internal(Result,resultdef);
+                exit;
+              end;
           end;
           end;
-
         if is_constintnode(right) and is_constintnode(left) then
         if is_constintnode(right) and is_constintnode(left) then
           begin
           begin
             rv:=tordconstnode(right).value;
             rv:=tordconstnode(right).value;
@@ -380,6 +395,10 @@ implementation
               resultdef:=right.resultdef;
               resultdef:=right.resultdef;
            end;
            end;
 
 
+         result:=simplify(false);
+         if assigned(result) then
+           exit;
+
          { when the result is currency we need some extra code for
          { when the result is currency we need some extra code for
            division. this should not be done when the divn node is
            division. this should not be done when the divn node is
            created internally }
            created internally }