Browse Source

* do_optconstpropagate and do_optdeadstoreelim now set their
"changed" parameter properly and is now an out type.
* Optimisations on calls to said functions

J. Gareth "Curious Kit" Moreton 1 year ago
parent
commit
ffe97bb7d9
3 changed files with 31 additions and 27 deletions
  1. 10 6
      compiler/optconstprop.pas
  2. 3 3
      compiler/optdeadstore.pas
  3. 18 18
      compiler/psub.pas

+ 10 - 6
compiler/optconstprop.pas

@@ -50,7 +50,7 @@ unit optconstprop;
 
       will not result in any constant propagation.
     }
-    function do_optconstpropagate(var rootnode : tnode;var changed: boolean) : tnode;
+    function do_optconstpropagate(var rootnode : tnode;out changed: boolean) : tnode;
 
   implementation
 
@@ -395,23 +395,27 @@ unit optconstprop;
       end;
 
 
-    function do_optconstpropagate(var rootnode: tnode;var changed: boolean): tnode;
+    function do_optconstpropagate(var rootnode: tnode;out changed: boolean): tnode;
+      var
+        iteration_changed: Boolean;
       begin
+        changed:=false;
         repeat
+          iteration_changed:=false;
 {$ifdef DEBUG_CONSTPROP}
           writeln('************************ before constant propagation ***************************');
           printnode(rootnode);
 {$endif DEBUG_CONSTPROP}
-          changed:=false;
-          foreachnodestatic(pm_postandagain, rootnode, @propagate, @changed);
-          if changed then
+          foreachnodestatic(pm_postandagain, rootnode, @propagate, @iteration_changed);
+          changed:=changed or iteration_changed;
+          if iteration_changed then
             doinlinesimplify(rootnode);
 {$ifdef DEBUG_CONSTPROP}
           writeln('************************ after constant propagation ***************************');
           printnode(rootnode);
           writeln('*******************************************************************************');
 {$endif DEBUG_CONSTPROP}
-        until not(cs_opt_level3 in current_settings.optimizerswitches) or not(changed);
+        until not(cs_opt_level3 in current_settings.optimizerswitches) or not(iteration_changed);
         result:=rootnode;
       end;
 

+ 3 - 3
compiler/optdeadstore.pas

@@ -31,7 +31,7 @@ unit optdeadstore;
     uses
       node;
 
-    function do_optdeadstoreelim(var rootnode : tnode;var changed: boolean) : tnode;
+    function do_optdeadstoreelim(var rootnode : tnode;out changed: boolean) : tnode;
 
   implementation
 
@@ -105,8 +105,9 @@ unit optdeadstore;
       end;
 
 
-    function do_optdeadstoreelim(var rootnode: tnode;var changed: boolean): tnode;
+    function do_optdeadstoreelim(var rootnode: tnode;out changed: boolean): tnode;
       begin
+        changed:=false;
 {$ifdef EXTDEBUG_DEADSTORE}
         writeln('******************* Tree before deadstore elimination **********************');
         printnode(rootnode);
@@ -114,7 +115,6 @@ unit optdeadstore;
 {$endif EXTDEBUG_DEADSTORE}
         if not(pi_dfaavailable in current_procinfo.flags) then
           internalerror(2013110201);
-        changed:=false;
         if not current_procinfo.has_nestedprocs then
           foreachnodestatic(pm_postprocess, rootnode, @deadstoreelim, @changed);
 {$ifdef DEBUG_DEADSTORE}

+ 18 - 18
compiler/psub.pas

@@ -1179,8 +1179,7 @@ implementation
       var
         i : integer;
         UserCode : TNode;
-        RedoDFA, changed : Boolean;
-        {RedoDFA : boolean;}
+        RedoDFA : boolean;
       begin
        { do this before adding the entry code else the tail recursion recognition won't work,
          if this causes troubles, it must be if'ed
@@ -1191,10 +1190,9 @@ implementation
 
        if cs_opt_constant_propagate in current_settings.optimizerswitches then
          begin
-           changed:=false;
-           repeat
-             do_optconstpropagate(code,changed);
-           until not(changed);
+           do_optconstpropagate(code,RedoDFA);
+           { RedoDFA value not used here }
+           RedoDFA:=false;
          end;
 
        if (cs_opt_nodedfa in current_settings.optimizerswitches) and
@@ -1208,12 +1206,14 @@ implementation
 
            if cs_opt_constant_propagate in current_settings.optimizerswitches then
              begin
-               changed:=false;
-               repeat
-                 do_optconstpropagate(code,changed);
-                 if changed then
+               do_optconstpropagate(code,RedoDFA);
+               if RedoDFA then
+                 begin
                    dfabuilder.redodfainfo(code);
-               until not(changed);
+                   RedoDFA:=false; { Don't redo it again unless necessary }
+                 end;
+               { Don't re-run constant propagation as redoing DFA info didn't
+                 actually change any nodes }
              end;
 
            if (cs_opt_loopstrength in current_settings.optimizerswitches)
@@ -1225,7 +1225,10 @@ implementation
              end;
 
            if RedoDFA then
-             dfabuilder.redodfainfo(code);
+             begin
+               dfabuilder.redodfainfo(code);
+               RedoDFA:=false; { Don't redo it again unless necessary }
+             end;
 
            if cs_opt_forloop in current_settings.optimizerswitches then
              RedoDFA:=OptimizeForLoop(code);
@@ -1266,12 +1269,9 @@ implementation
 
            if cs_opt_dead_store_eliminate in current_settings.optimizerswitches then
              begin
-               changed:=false;
-               repeat
-                 do_optdeadstoreelim(code,changed);
-                 if changed then
-                   dfabuilder.redodfainfo(code);
-               until not(changed);
+               do_optdeadstoreelim(code,RedoDFA);
+               if RedoDFA then
+                 dfabuilder.redodfainfo(code);
              end;
          end
        else