Browse Source

+ implement while (simple expr.) do ... to if (simple expr.) then repeat ... until not(simple expr.); optimization as proposed by Rika

florian 3 tuần trước cách đây
mục cha
commit
1f035adf9f
1 tập tin đã thay đổi với 18 bổ sung9 xóa
  1. 18 9
      compiler/nflw.pas

+ 18 - 9
compiler/nflw.pas

@@ -1358,19 +1358,28 @@ implementation
         { convert while i>0 do ... dec(i); to if i>0 then repeat ... dec(i) until i=0; ? }
         if (cs_opt_level2 in current_settings.optimizerswitches) and
           { while loop?}
-          (lnf_testatbegin in loopflags) and not(lnf_checknegate in loopflags) and
-          ((left.nodetype=gtn) and (taddnode(left).left.nodetype=loadn) and is_constintnode(taddnode(left).right) and
-           (tordconstnode(taddnode(left).right).value=0)) then
+          (lnf_testatbegin in loopflags) and not(lnf_checknegate in loopflags) then
           begin
-            p:=GetLastStatement(right);
-            if assigned(p) and (p.nodetype=inlinen) and (tinlinenode(p).inlinenumber=in_dec_x) and
-              taddnode(left).left.isequal(tcallparanode(tinlinenode(p).left).left) and
-              not(assigned(tcallparanode(tinlinenode(p).left).right)) then
+            if ((left.nodetype=gtn) and (taddnode(left).left.nodetype=loadn) and is_constintnode(taddnode(left).right) and
+              (tordconstnode(taddnode(left).right).value=0)) then
+              begin
+                p:=GetLastStatement(right);
+                if assigned(p) and (p.nodetype=inlinen) and (tinlinenode(p).inlinenumber=in_dec_x) and
+                  taddnode(left).left.isequal(tcallparanode(tinlinenode(p).left).left) and
+                  not(assigned(tcallparanode(tinlinenode(p).left).right)) then
+                  begin
+                    result:=cifnode.create_internal(left.getcopy,getcopy,nil);
+                    include(twhilerepeatnode(tifnode(result).right).loopflags,lnf_checknegate);
+                    exclude(twhilerepeatnode(tifnode(result).right).loopflags,lnf_testatbegin);
+                    twhilerepeatnode(tifnode(result).right).left.nodetype:=equaln;
+                  end;
+              end
+            else if not(cs_opt_size in current_settings.optimizerswitches) and
+              (node_complexity(left)<=3) then
               begin
                 result:=cifnode.create_internal(left.getcopy,getcopy,nil);
-                include(twhilerepeatnode(tifnode(result).right).loopflags,lnf_checknegate);
+//                include(twhilerepeatnode(tifnode(result).right).loopflags,lnf_checknegate);
                 exclude(twhilerepeatnode(tifnode(result).right).loopflags,lnf_testatbegin);
-                twhilerepeatnode(tifnode(result).right).left.nodetype:=equaln;
               end;
           end;
       end;