optloop.pas 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  1. {
  2. Loop optimization
  3. Copyright (c) 2005 by Florian Klaempfl
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit optloop;
  18. {$i fpcdefs.inc}
  19. { $define DEBUG_OPTSTRENGTH}
  20. { $define DEBUG_OPTFORLOOP}
  21. interface
  22. uses
  23. node;
  24. function unroll_loop(node : tnode) : tnode;
  25. function OptimizeInductionVariables(node : tnode) : boolean;
  26. function OptimizeForLoop(var node : tnode) : boolean;
  27. implementation
  28. uses
  29. cutils,cclasses,compinnr,
  30. globtype,globals,constexp,
  31. verbose,
  32. symdef,symsym,
  33. defutil,
  34. cpuinfo,
  35. nutils,
  36. nadd,nbas,nflw,ncon,ninl,ncal,nld,nmem,ncnv,
  37. ncgmem,
  38. pass_1,
  39. optbase,optutils,
  40. procinfo;
  41. function number_unrolls(node : tnode) : cardinal;
  42. begin
  43. { calculate how often a loop shall be unrolled.
  44. The term (60*ord(node_count_weighted(node)<15)) is used to get small loops unrolled more often as
  45. the counter management takes more time in this case. }
  46. {$ifdef i386}
  47. { multiply by 2 for CPUs with a long pipeline }
  48. if current_settings.optimizecputype in [cpu_Pentium4] then
  49. number_unrolls:=trunc(round((60+(60*ord(node_count_weighted(node)<15)))/max(node_count_weighted(node),1)))
  50. else
  51. {$endif i386}
  52. number_unrolls:=trunc(round((30+(60*ord(node_count_weighted(node)<15)))/max(node_count_weighted(node),1)));
  53. if number_unrolls=0 then
  54. number_unrolls:=1;
  55. end;
  56. type
  57. treplaceinfo = record
  58. node : tnode;
  59. value : Tconstexprint;
  60. end;
  61. preplaceinfo = ^treplaceinfo;
  62. function checkcontrollflowstatements(var n:tnode; arg: pointer): foreachnoderesult;
  63. begin
  64. if n.nodetype in [breakn,continuen,goton,labeln,exitn,raisen] then
  65. result:=fen_norecurse_true
  66. else
  67. result:=fen_false;
  68. end;
  69. function replaceloadnodes(var n: tnode; arg: pointer): foreachnoderesult;
  70. begin
  71. if n.isequal(preplaceinfo(arg)^.node) then
  72. begin
  73. if n.flags*[nf_modify,nf_write,nf_address_taken]<>[] then
  74. internalerror(2012090402);
  75. n.free;
  76. n:=cordconstnode.create(preplaceinfo(arg)^.value,preplaceinfo(arg)^.node.resultdef,false);
  77. do_firstpass(n);
  78. end;
  79. result:=fen_false;
  80. end;
  81. function unroll_loop(node : tnode) : tnode;
  82. var
  83. unrolls,i : cardinal;
  84. counts : qword;
  85. unrollstatement,newforstatement : tstatementnode;
  86. unrollblock : tblocknode;
  87. getridoffor : boolean;
  88. replaceinfo : treplaceinfo;
  89. hascontrollflowstatements : boolean;
  90. begin
  91. result:=nil;
  92. if (cs_opt_size in current_settings.optimizerswitches) then
  93. exit;
  94. if not(node.nodetype in [forn]) then
  95. exit;
  96. unrolls:=number_unrolls(tfornode(node).t2);
  97. if (unrolls>1) and
  98. ((tfornode(node).left.nodetype<>loadn) or
  99. { the address of the counter variable might be taken if it is passed by constref to a
  100. subroutine, so really check if it is not taken }
  101. ((tfornode(node).left.nodetype=loadn) and (tloadnode(tfornode(node).left).symtableentry is tabstractvarsym) and
  102. not(tabstractvarsym(tloadnode(tfornode(node).left).symtableentry).addr_taken) and
  103. not(tabstractvarsym(tloadnode(tfornode(node).left).symtableentry).different_scope))
  104. ) then
  105. begin
  106. { number of executions known? }
  107. if (tfornode(node).right.nodetype=ordconstn) and (tfornode(node).t1.nodetype=ordconstn) then
  108. begin
  109. if lnf_backward in tfornode(node).loopflags then
  110. counts:=tordconstnode(tfornode(node).right).value-tordconstnode(tfornode(node).t1).value+1
  111. else
  112. counts:=tordconstnode(tfornode(node).t1).value-tordconstnode(tfornode(node).right).value+1;
  113. hascontrollflowstatements:=foreachnodestatic(tfornode(node).t2,@checkcontrollflowstatements,nil);
  114. { don't unroll more than we need,
  115. multiply unroll by two here because we can get rid
  116. of the counter variable completely and replace it by a constant
  117. if unrolls=counts }
  118. if unrolls*2>=counts then
  119. unrolls:=counts;
  120. { create block statement }
  121. unrollblock:=internalstatements(unrollstatement);
  122. { can we get rid completly of the for ? }
  123. getridoffor:=(unrolls=counts) and not(hascontrollflowstatements) and
  124. { TP/Macpas allows assignments to the for-variables, so we cannot get rid of the for }
  125. ([m_tp7,m_mac]*current_settings.modeswitches=[]);
  126. if getridoffor then
  127. begin
  128. replaceinfo.node:=tfornode(node).left;
  129. replaceinfo.value:=tordconstnode(tfornode(node).right).value;
  130. end
  131. else
  132. { we consider currently unrolling not beneficial, if we cannot get rid of the for completely, this
  133. might change if a more sophisticated heuristics is used (FK) }
  134. exit;
  135. { let's unroll (and rock of course) }
  136. for i:=1 to unrolls do
  137. begin
  138. { create and insert copy of the statement block }
  139. addstatement(unrollstatement,tfornode(node).t2.getcopy);
  140. { set and insert entry label? }
  141. if (counts mod unrolls<>0) and
  142. ((counts mod unrolls)=unrolls-i) then
  143. begin
  144. tfornode(node).entrylabel:=clabelnode.create(cnothingnode.create,clabelsym.create('$optunrol'));
  145. addstatement(unrollstatement,tfornode(node).entrylabel);
  146. end;
  147. if getridoffor then
  148. begin
  149. foreachnodestatic(tnode(unrollstatement),@replaceloadnodes,@replaceinfo);
  150. if lnf_backward in tfornode(node).loopflags then
  151. replaceinfo.value:=replaceinfo.value-1
  152. else
  153. replaceinfo.value:=replaceinfo.value+1;
  154. end
  155. else
  156. begin
  157. { for itself increases at the last iteration }
  158. if i<unrolls then
  159. begin
  160. { insert incr/decrementation of counter var }
  161. if lnf_backward in tfornode(node).loopflags then
  162. addstatement(unrollstatement,
  163. geninlinenode(in_dec_x,false,ccallparanode.create(tfornode(node).left.getcopy,nil)))
  164. else
  165. addstatement(unrollstatement,
  166. geninlinenode(in_inc_x,false,ccallparanode.create(tfornode(node).left.getcopy,nil)));
  167. end;
  168. end;
  169. end;
  170. { can we get rid of the for statement? }
  171. if getridoffor then
  172. begin
  173. { create block statement }
  174. result:=internalstatements(newforstatement);
  175. addstatement(newforstatement,unrollblock);
  176. doinlinesimplify(result);
  177. end;
  178. end
  179. else
  180. begin
  181. { unrolling is a little bit more tricky if we don't know the
  182. loop count at compile time, but the solution is to use a jump table
  183. which is indexed by "loop count mod unrolls" at run time and which
  184. jumps then at the appropriate place inside the loop. Because
  185. a module division is expensive, we can use only unroll counts dividable
  186. by 2 }
  187. case unrolls of
  188. 1..2:
  189. ;
  190. 3:
  191. unrolls:=2;
  192. 4..7:
  193. unrolls:=4;
  194. { unrolls>4 already make no sense imo, but who knows (FK) }
  195. 8..15:
  196. unrolls:=8;
  197. 16..31:
  198. unrolls:=16;
  199. 32..63:
  200. unrolls:=32;
  201. 64..$7fff:
  202. unrolls:=64;
  203. else
  204. exit;
  205. end;
  206. { we don't handle this yet }
  207. exit;
  208. end;
  209. if not(assigned(result)) then
  210. begin
  211. tfornode(node).t2.free;
  212. tfornode(node).t2:=unrollblock;
  213. end;
  214. end;
  215. end;
  216. var
  217. initcode,
  218. calccode,
  219. deletecode : tblocknode;
  220. initcodestatements,
  221. calccodestatements,
  222. deletecodestatements: tstatementnode;
  223. templist : tfplist;
  224. inductionexprs : tfplist;
  225. changedforloop,
  226. containsnestedforloop : boolean;
  227. function is_loop_invariant(loop : tnode;expr : tnode) : boolean;
  228. begin
  229. result:=is_constnode(expr);
  230. case expr.nodetype of
  231. loadn:
  232. begin
  233. if (pi_dfaavailable in current_procinfo.flags) and
  234. assigned(loop.optinfo) and
  235. assigned(expr.optinfo) and
  236. not(expr.isequal(tfornode(loop).left)) then
  237. { no aliasing? }
  238. result:=(([nf_write,nf_modify]*expr.flags)=[]) and not(tabstractvarsym(tloadnode(expr).symtableentry).addr_taken) and
  239. { no definition in the loop? }
  240. not(DFASetIn(tfornode(loop).t2.optinfo^.defsum,expr.optinfo^.index));
  241. end;
  242. vecn:
  243. begin
  244. result:=((tvecnode(expr).left.nodetype=loadn) or is_loop_invariant(loop,tvecnode(expr).left)) and
  245. is_loop_invariant(loop,tvecnode(expr).right);
  246. end;
  247. typeconvn:
  248. result:=is_loop_invariant(loop,ttypeconvnode(expr).left);
  249. addn,subn:
  250. result:=is_loop_invariant(loop,taddnode(expr).left) and is_loop_invariant(loop,taddnode(expr).right);
  251. else
  252. ;
  253. end;
  254. end;
  255. { checks if the strength of n can be recuded, arg is the tforloop being considered }
  256. function dostrengthreductiontest(var n: tnode; arg: pointer): foreachnoderesult;
  257. function findpreviousstrengthreduction : boolean;
  258. var
  259. i : longint;
  260. hp : tnode;
  261. begin
  262. result:=false;
  263. for i:=0 to inductionexprs.count-1 do
  264. begin
  265. { do we already maintain one expression? }
  266. if tnode(inductionexprs[i]).isequal(n) then
  267. begin
  268. case n.nodetype of
  269. muln:
  270. hp:=ctemprefnode.create(ttempcreatenode(templist[i]));
  271. vecn:
  272. hp:=ctypeconvnode.create_internal(cderefnode.create(ctemprefnode.create(
  273. ttempcreatenode(templist[i]))),n.resultdef);
  274. else
  275. internalerror(200809211);
  276. end;
  277. n.free;
  278. n:=hp;
  279. result:=true;
  280. exit;
  281. end;
  282. end;
  283. end;
  284. procedure CreateNodes;
  285. begin
  286. if not assigned(initcode) then
  287. begin
  288. initcode:=internalstatements(initcodestatements);
  289. calccode:=internalstatements(calccodestatements);
  290. deletecode:=internalstatements(deletecodestatements);
  291. end;
  292. end;
  293. var
  294. tempnode : ttempcreatenode;
  295. dummy : longint;
  296. begin
  297. result:=fen_false;
  298. case n.nodetype of
  299. forn:
  300. { inform for loop search routine, that it needs to search more deeply }
  301. containsnestedforloop:=true;
  302. muln:
  303. begin
  304. if (taddnode(n).right.nodetype=loadn) and
  305. taddnode(n).right.isequal(tfornode(arg).left) and
  306. { plain read of the loop variable? }
  307. not(nf_write in taddnode(n).right.flags) and
  308. not(nf_modify in taddnode(n).right.flags) and
  309. is_loop_invariant(tfornode(arg),taddnode(n).left) and
  310. { for now, we can handle only constant lower borders }
  311. is_constnode(tfornode(arg).right) then
  312. taddnode(n).swapleftright;
  313. if (taddnode(n).left.nodetype=loadn) and
  314. taddnode(n).left.isequal(tfornode(arg).left) and
  315. { plain read of the loop variable? }
  316. not(nf_write in taddnode(n).left.flags) and
  317. not(nf_modify in taddnode(n).left.flags) and
  318. is_loop_invariant(tfornode(arg),taddnode(n).right) and
  319. { for now, we can handle only constant lower borders }
  320. is_constnode(tfornode(arg).right) then
  321. begin
  322. changedforloop:=true;
  323. { did we use the same expression before already? }
  324. if not(findpreviousstrengthreduction) then
  325. begin
  326. tempnode:=ctempcreatenode.create(n.resultdef,n.resultdef.size,tt_persistent,
  327. tstoreddef(n.resultdef).is_intregable or tstoreddef(n.resultdef).is_fpuregable);
  328. templist.Add(tempnode);
  329. inductionexprs.Add(n);
  330. CreateNodes;
  331. if lnf_backward in tfornode(arg).loopflags then
  332. addstatement(calccodestatements,
  333. geninlinenode(in_dec_x,false,
  334. ccallparanode.create(ctemprefnode.create(tempnode),ccallparanode.create(taddnode(n).right.getcopy,nil))))
  335. else
  336. addstatement(calccodestatements,
  337. geninlinenode(in_inc_x,false,
  338. ccallparanode.create(ctemprefnode.create(tempnode),ccallparanode.create(taddnode(n).right.getcopy,nil))));
  339. addstatement(initcodestatements,tempnode);
  340. addstatement(initcodestatements,cassignmentnode.create(ctemprefnode.create(tempnode),
  341. caddnode.create(muln,tfornode(arg).right.getcopy,
  342. taddnode(n).right.getcopy)
  343. )
  344. );
  345. { finally replace the node by a temp. ref }
  346. n:=ctemprefnode.create(tempnode);
  347. { ... and add a temp. release node }
  348. addstatement(deletecodestatements,ctempdeletenode.create(tempnode));
  349. end;
  350. { set types }
  351. do_firstpass(n);
  352. result:=fen_norecurse_false;
  353. end;
  354. end;
  355. vecn:
  356. begin
  357. { is the index the counter variable? }
  358. if not(is_special_array(tvecnode(n).left.resultdef)) and
  359. not(is_packed_array(tvecnode(n).left.resultdef)) and
  360. (tvecnode(n).right.isequal(tfornode(arg).left) or
  361. { fpc usually creates a type cast to access an array }
  362. ((tvecnode(n).right.nodetype=typeconvn) and
  363. ttypeconvnode(tvecnode(n).right).left.isequal(tfornode(arg).left)
  364. )
  365. ) and
  366. { plain read of the loop variable? }
  367. not(nf_write in tvecnode(n).right.flags) and
  368. not(nf_modify in tvecnode(n).right.flags) and
  369. { direct array access? }
  370. ((tvecnode(n).left.nodetype=loadn) or
  371. { ... or loop invariant expression? }
  372. is_loop_invariant(tfornode(arg),tvecnode(n).right)) and
  373. { removing the multiplication is only worth the
  374. effort if it's not a simple shift }
  375. not(ispowerof2(tcgvecnode(n).get_mul_size,dummy)) then
  376. begin
  377. changedforloop:=true;
  378. { did we use the same expression before already? }
  379. if not(findpreviousstrengthreduction) then
  380. begin
  381. {$ifdef DEBUG_OPTSTRENGTH}
  382. writeln('**********************************************************************************');
  383. writeln('Found expression for strength reduction: ');
  384. printnode(n);
  385. writeln('**********************************************************************************');
  386. {$endif DEBUG_OPTSTRENGTH}
  387. tempnode:=ctempcreatenode.create(voidpointertype,voidpointertype.size,tt_persistent,true);
  388. templist.Add(tempnode);
  389. inductionexprs.Add(n);
  390. CreateNodes;
  391. if lnf_backward in tfornode(arg).loopflags then
  392. addstatement(calccodestatements,
  393. cinlinenode.createintern(in_dec_x,false,
  394. ccallparanode.create(ctemprefnode.create(tempnode),ccallparanode.create(
  395. cordconstnode.create(tcgvecnode(n).get_mul_size,sizeuinttype,false),nil))))
  396. else
  397. addstatement(calccodestatements,
  398. cinlinenode.createintern(in_inc_x,false,
  399. ccallparanode.create(ctemprefnode.create(tempnode),ccallparanode.create(
  400. cordconstnode.create(tcgvecnode(n).get_mul_size,sizeuinttype,false),nil))));
  401. addstatement(initcodestatements,tempnode);
  402. addstatement(initcodestatements,cassignmentnode.create(ctemprefnode.create(tempnode),
  403. caddrnode.create(
  404. cvecnode.create(tvecnode(n).left.getcopy,tfornode(arg).right.getcopy)
  405. )
  406. ));
  407. { finally replace the node by a temp. ref }
  408. n:=ctypeconvnode.create_internal(cderefnode.create(ctemprefnode.create(tempnode)),n.resultdef);
  409. { ... and add a temp. release node }
  410. addstatement(deletecodestatements,ctempdeletenode.create(tempnode));
  411. end;
  412. { set types }
  413. do_firstpass(n);
  414. result:=fen_norecurse_false;
  415. end;
  416. end;
  417. else
  418. ;
  419. end;
  420. end;
  421. function OptimizeInductionVariablesSingleForLoop(node : tnode) : tnode;
  422. var
  423. loopcode : tblocknode;
  424. loopcodestatements,
  425. newcodestatements : tstatementnode;
  426. fornode : tfornode;
  427. begin
  428. result:=nil;
  429. if node.nodetype<>forn then
  430. exit;
  431. templist:=TFPList.Create;
  432. inductionexprs:=TFPList.Create;
  433. initcode:=nil;
  434. calccode:=nil;
  435. deletecode:=nil;
  436. initcodestatements:=nil;
  437. calccodestatements:=nil;
  438. deletecodestatements:=nil;
  439. { find all expressions being candidates for strength reduction
  440. and replace them }
  441. foreachnodestatic(pm_postprocess,node,@dostrengthreductiontest,node);
  442. { clue everything together }
  443. if assigned(initcode) then
  444. begin
  445. do_firstpass(tnode(initcode));
  446. do_firstpass(tnode(calccode));
  447. do_firstpass(tnode(deletecode));
  448. { create a new for node, the old one will be released by the compiler }
  449. with tfornode(node) do
  450. begin
  451. fornode:=cfornode.create(left,right,t1,t2,lnf_backward in loopflags);
  452. left:=nil;
  453. right:=nil;
  454. t1:=nil;
  455. t2:=nil;
  456. end;
  457. node:=fornode;
  458. loopcode:=internalstatements(loopcodestatements);
  459. addstatement(loopcodestatements,tfornode(node).t2);
  460. tfornode(node).t2:=loopcode;
  461. do_firstpass(node);
  462. addstatement(loopcodestatements,calccode);
  463. result:=internalstatements(newcodestatements);
  464. addstatement(newcodestatements,initcode);
  465. initcode:=nil;
  466. addstatement(newcodestatements,node);
  467. addstatement(newcodestatements,deletecode);
  468. end;
  469. templist.Free;
  470. inductionexprs.Free;
  471. end;
  472. function OptimizeInductionVariables_iterforloops(var n: tnode; arg: pointer): foreachnoderesult;
  473. var
  474. hp : tnode;
  475. begin
  476. Result:=fen_false;
  477. if n.nodetype=forn then
  478. begin
  479. { do we have DFA available? }
  480. if pi_dfaavailable in current_procinfo.flags then
  481. begin
  482. CalcDefSum(tfornode(n).t2);
  483. end;
  484. containsnestedforloop:=false;
  485. hp:=OptimizeInductionVariablesSingleForLoop(n);
  486. if assigned(hp) then
  487. begin
  488. n.Free;
  489. n:=hp;
  490. end;
  491. { can we avoid further searching? }
  492. if not(containsnestedforloop) then
  493. Result:=fen_norecurse_false;
  494. end;
  495. end;
  496. function OptimizeInductionVariables(node : tnode) : boolean;
  497. begin
  498. changedforloop:=false;
  499. foreachnodestatic(pm_postprocess,node,@OptimizeInductionVariables_iterforloops,nil);
  500. Result:=changedforloop;
  501. end;
  502. function OptimizeForLoop_iterforloops(var n: tnode; arg: pointer): foreachnoderesult;
  503. var
  504. hp : tnode;
  505. begin
  506. Result:=fen_false;
  507. if (n.nodetype=forn) and
  508. not(lnf_backward in tfornode(n).loopflags) and
  509. (lnf_dont_mind_loopvar_on_exit in tfornode(n).loopflags) and
  510. is_constintnode(tfornode(n).right) and
  511. { this is not strictly necessary, but we do it for now }
  512. is_constnode(tfornode(n).t1) and
  513. (([cs_check_overflow,cs_check_range]*n.localswitches)=[]) and
  514. (([cs_check_overflow,cs_check_range]*tfornode(n).left.localswitches)=[]) and
  515. ((tfornode(n).left.nodetype=loadn) and (tloadnode(tfornode(n).left).symtableentry is tabstractvarsym) and
  516. not(tabstractvarsym(tloadnode(tfornode(n).left).symtableentry).addr_taken) and
  517. not(tabstractvarsym(tloadnode(tfornode(n).left).symtableentry).different_scope)) then
  518. begin
  519. { do we have DFA available? }
  520. if pi_dfaavailable in current_procinfo.flags then
  521. begin
  522. CalcUseSum(tfornode(n).t2);
  523. CalcDefSum(tfornode(n).t2);
  524. end
  525. else
  526. Internalerror(2017122801);
  527. if not(assigned(tfornode(n).left.optinfo)) then
  528. exit;
  529. if not(DFASetIn(tfornode(n).t2.optinfo^.usesum,tfornode(n).left.optinfo^.index)) and
  530. not(DFASetIn(tfornode(n).t2.optinfo^.defsum,tfornode(n).left.optinfo^.index)) then
  531. begin
  532. { convert the loop from i:=a to b into i:=b-a+1 to 1 as this simplifies the
  533. abort condition }
  534. {$ifdef DEBUG_OPTFORLOOP}
  535. writeln('**********************************************************************************');
  536. writeln('Found loop for reverting: ');
  537. printnode(n);
  538. writeln('**********************************************************************************');
  539. {$endif DEBUG_OPTFORLOOP}
  540. include(tfornode(n).loopflags,lnf_backward);
  541. tfornode(n).right:=caddnode.create_internal(addn,caddnode.create_internal(subn,tfornode(n).t1,tfornode(n).right),
  542. cordconstnode.create(1,tfornode(n).left.resultdef,false));
  543. tfornode(n).t1:=cordconstnode.create(1,tfornode(n).left.resultdef,false);
  544. include(tfornode(n).loopflags,lnf_counter_not_used);
  545. exclude(n.flags,nf_pass1_done);
  546. do_firstpass(n);
  547. {$ifdef DEBUG_OPTFORLOOP}
  548. writeln('Loop reverted: ');
  549. printnode(n);
  550. writeln('**********************************************************************************');
  551. {$endif DEBUG_OPTFORLOOP}
  552. changedforloop:=true;
  553. end;
  554. end;
  555. end;
  556. function OptimizeForLoop(var node : tnode) : boolean;
  557. begin
  558. Result:=false;
  559. if not(pi_dfaavailable in current_procinfo.flags) then
  560. exit;
  561. changedforloop:=false;
  562. foreachnodestatic(pm_postprocess,node,@OptimizeForLoop_iterforloops,nil);
  563. Result:=changedforloop;
  564. end;
  565. end.