From f393c3ff379e0bff0082d788c5a3b038c6531d8b Mon Sep 17 00:00:00 2001 From: florian Date: Tue, 31 Dec 2013 13:16:09 +0000 Subject: [PATCH] * 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 - --- .gitattributes | 1 + compiler/optconstprop.pas | 11 ++++++++++- tests/webtbs/tw8883b.pp | 25 +++++++++++++++++++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 tests/webtbs/tw8883b.pp diff --git a/.gitattributes b/.gitattributes index d9a8105ab3..ac38756d62 100644 --- a/.gitattributes +++ b/.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 diff --git a/compiler/optconstprop.pas b/compiler/optconstprop.pas index 472e4bb6ef..09866dc2c1 100644 --- a/compiler/optconstprop.pas +++ b/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; diff --git a/tests/webtbs/tw8883b.pp b/tests/webtbs/tw8883b.pp new file mode 100644 index 0000000000..551a31055d --- /dev/null +++ b/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.