Bladeren bron

* propagate constants into the header of a for loop
+ test which avoids this so we do not miss regressions on for loops with variable boundaries

git-svn-id: trunk@26339 -

florian 11 jaren geleden
bovenliggende
commit
f393c3ff37
3 gewijzigde bestanden met toevoegingen van 36 en 1 verwijderingen
  1. 1 0
      .gitattributes
  2. 10 1
      compiler/optconstprop.pas
  3. 25 0
      tests/webtbs/tw8883b.pp

+ 1 - 0
.gitattributes

@@ -14370,6 +14370,7 @@ tests/webtbs/tw8847.pp svneol=native#text/plain
 tests/webtbs/tw8861.pp svneol=native#text/plain
 tests/webtbs/tw8870.pp svneol=native#text/plain
 tests/webtbs/tw8883.pp svneol=native#text/plain
+tests/webtbs/tw8883b.pp svneol=native#text/pascal
 tests/webtbs/tw8919.pp svneol=native#text/plain
 tests/webtbs/tw8930.pp svneol=native#text/plain
 tests/webtbs/tw8935.pp svneol=native#text/plain

+ 10 - 1
compiler/optconstprop.pas

@@ -93,7 +93,7 @@ unit optconstprop;
           iterate manually here so we have full controll how all nodes are processed }
 
         { We cannot analyze beyond those nodes, so we terminate to be on the safe side }
-        if (n.nodetype in [addrn,derefn,dataconstn,asmn,withn,casen,whilerepeatn,forn,labeln,continuen,breakn,
+        if (n.nodetype in [addrn,derefn,dataconstn,asmn,withn,casen,whilerepeatn,labeln,continuen,breakn,
                            tryexceptn,raisen,tryfinallyn,onn,loadparentfpn,loadvmtaddrn,guidconstn,rttin,addoptn,asn,goton,
                            objcselectorn,objcprotocoln]) then
           exit(false)
@@ -123,6 +123,15 @@ unit optconstprop;
           end
         else if n.nodetype=statementn then
           result:=replaceBasicAssign(tstatementnode(n).left, arg, tree_modified)
+        else if n.nodetype=forn then
+          begin
+            result:=replaceBasicAssign(tfornode(n).right, arg, tree_modified);
+            if result then
+              replaceBasicAssign(tfornode(n).t1, arg, tree_modified2);
+            tree_modified:=tree_modified or tree_modified2;
+            { after a for node we cannot continue with our simple approach }
+            result:=false;
+          end
         else if n.nodetype=blockn then
           begin
             changed:=false;

+ 25 - 0
tests/webtbs/tw8883b.pp

@@ -0,0 +1,25 @@
+{ %OPT=-Oonoconstprop }
+procedure DoTest;
+var
+  i, j, cnt: longint;
+begin
+  cnt:=0;
+  j:=1;
+  for i:=0 to j do
+  begin
+    Inc(cnt);
+    Dec(j);
+  end;
+
+  writeln(cnt);
+  if cnt <> 2 then
+  begin
+    writeln('Test failed!');
+    Halt(1);
+  end;
+  writeln('Test OK.');
+end;
+
+begin
+  dotest;
+end.