Browse Source

* better code for ... downto 1 for-loops

florian 8 months ago
parent
commit
798f2ba632
1 changed files with 10 additions and 1 deletions
  1. 10 1
      compiler/nflw.pas

+ 10 - 1
compiler/nflw.pas

@@ -2103,7 +2103,16 @@ implementation
             else
             else
               toexpr:=t1.getcopy;
               toexpr:=t1.getcopy;
 
 
-            addstatement(ifstatements,cwhilerepeatnode.create(caddnode.create_internal(cond,leftcopy,toexpr),loopblock,false,true));
+            { checking against zero might improve the generated assembler,
+              doing this transformation for other values is normally not beneficial }
+            if do_loopvar_at_end and (lnf_backward in loopflags) and is_constintnode(toexpr) and (tordconstnode(toexpr).value=1) and
+              (countermin<tordconstnode(toexpr).value) then
+              begin
+                tordconstnode(toexpr).value:=tordconstnode(toexpr).value-1;
+                addstatement(ifstatements,cwhilerepeatnode.create(caddnode.create_internal(equaln,leftcopy,toexpr),loopblock,false,true))
+              end
+            else
+              addstatement(ifstatements,cwhilerepeatnode.create(caddnode.create_internal(cond,leftcopy,toexpr),loopblock,false,true));
 
 
             if usefromtemp then
             if usefromtemp then
               fromexpr:=ctemprefnode.create(fromtemp)
               fromexpr:=ctemprefnode.create(fromtemp)