2
0

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 ErrorCount<>0 then
  95. exit;
  96. if not(node.nodetype in [forn]) then
  97. exit;
  98. unrolls:=number_unrolls(tfornode(node).t2);
  99. if (unrolls>1) and
  100. ((tfornode(node).left.nodetype<>loadn) or
  101. { the address of the counter variable might be taken if it is passed by constref to a
  102. subroutine, so really check if it is not taken }
  103. ((tfornode(node).left.nodetype=loadn) and (tloadnode(tfornode(node).left).symtableentry is tabstractvarsym) and
  104. not(tabstractvarsym(tloadnode(tfornode(node).left).symtableentry).addr_taken) and
  105. not(tabstractvarsym(tloadnode(tfornode(node).left).symtableentry).different_scope))
  106. ) then
  107. begin
  108. { number of executions known? }
  109. if (tfornode(node).right.nodetype=ordconstn) and (tfornode(node).t1.nodetype=ordconstn) then
  110. begin
  111. if lnf_backward in tfornode(node).loopflags then
  112. counts:=tordconstnode(tfornode(node).right).value-tordconstnode(tfornode(node).t1).value+1
  113. else
  114. counts:=tordconstnode(tfornode(node).t1).value-tordconstnode(tfornode(node).right).value+1;
  115. hascontrollflowstatements:=foreachnodestatic(tfornode(node).t2,@checkcontrollflowstatements,nil);
  116. { don't unroll more than we need,
  117. multiply unroll by two here because we can get rid
  118. of the counter variable completely and replace it by a constant
  119. if unrolls=counts }
  120. if unrolls*2>=counts then
  121. unrolls:=counts;
  122. { create block statement }
  123. unrollblock:=internalstatements(unrollstatement);
  124. { can we get rid completly of the for ? }
  125. getridoffor:=(unrolls=counts) and not(hascontrollflowstatements) and
  126. { TP/Macpas allows assignments to the for-variables, so we cannot get rid of the for }
  127. ([m_tp7,m_mac]*current_settings.modeswitches=[]);
  128. if getridoffor then
  129. begin
  130. replaceinfo.node:=tfornode(node).left;
  131. replaceinfo.value:=tordconstnode(tfornode(node).right).value;
  132. end
  133. else
  134. { we consider currently unrolling not beneficial, if we cannot get rid of the for completely, this
  135. might change if a more sophisticated heuristics is used (FK) }
  136. exit;
  137. { let's unroll (and rock of course) }
  138. for i:=1 to unrolls do
  139. begin
  140. { create and insert copy of the statement block }
  141. addstatement(unrollstatement,tfornode(node).t2.getcopy);
  142. { set and insert entry label? }
  143. if (counts mod unrolls<>0) and
  144. ((counts mod unrolls)=unrolls-i) then
  145. begin
  146. tfornode(node).entrylabel:=clabelnode.create(cnothingnode.create,clabelsym.create('$optunrol'));
  147. addstatement(unrollstatement,tfornode(node).entrylabel);
  148. end;
  149. if getridoffor then
  150. begin
  151. foreachnodestatic(tnode(unrollstatement),@replaceloadnodes,@replaceinfo);
  152. if lnf_backward in tfornode(node).loopflags then
  153. replaceinfo.value:=replaceinfo.value-1
  154. else
  155. replaceinfo.value:=replaceinfo.value+1;
  156. end
  157. else
  158. begin
  159. { for itself increases at the last iteration }
  160. if i<unrolls then
  161. begin
  162. { insert incr/decrementation of counter var }
  163. if lnf_backward in tfornode(node).loopflags then
  164. addstatement(unrollstatement,
  165. geninlinenode(in_dec_x,false,ccallparanode.create(tfornode(node).left.getcopy,nil)))
  166. else
  167. addstatement(unrollstatement,
  168. geninlinenode(in_inc_x,false,ccallparanode.create(tfornode(node).left.getcopy,nil)));
  169. end;
  170. end;
  171. end;
  172. { can we get rid of the for statement? }
  173. if getridoffor then
  174. begin
  175. { create block statement }
  176. result:=internalstatements(newforstatement);
  177. addstatement(newforstatement,unrollblock);
  178. doinlinesimplify(result);
  179. end;
  180. end
  181. else
  182. begin
  183. { unrolling is a little bit more tricky if we don't know the
  184. loop count at compile time, but the solution is to use a jump table
  185. which is indexed by "loop count mod unrolls" at run time and which
  186. jumps then at the appropriate place inside the loop. Because
  187. a module division is expensive, we can use only unroll counts dividable
  188. by 2 }
  189. case unrolls of
  190. 1..2:
  191. ;
  192. 3:
  193. unrolls:=2;
  194. 4..7:
  195. unrolls:=4;
  196. { unrolls>4 already make no sense imo, but who knows (FK) }
  197. 8..15:
  198. unrolls:=8;
  199. 16..31:
  200. unrolls:=16;
  201. 32..63:
  202. unrolls:=32;
  203. 64..$7fff:
  204. unrolls:=64;
  205. else
  206. exit;
  207. end;
  208. { we don't handle this yet }
  209. exit;
  210. end;
  211. if not(assigned(result)) then
  212. begin
  213. tfornode(node).t2.free;
  214. tfornode(node).t2:=unrollblock;
  215. end;
  216. end;
  217. end;
  218. var
  219. initcode,
  220. calccode,
  221. deletecode : tblocknode;
  222. initcodestatements,
  223. calccodestatements,
  224. deletecodestatements: tstatementnode;
  225. templist : tfplist;
  226. inductionexprs : tfplist;
  227. changedforloop,
  228. containsnestedforloop : boolean;
  229. function is_loop_invariant(loop : tnode;expr : tnode) : boolean;
  230. begin
  231. result:=is_constnode(expr);
  232. case expr.nodetype of
  233. loadn:
  234. begin
  235. if (pi_dfaavailable in current_procinfo.flags) and
  236. assigned(loop.optinfo) and
  237. assigned(expr.optinfo) and
  238. not(expr.isequal(tfornode(loop).left)) then
  239. { no aliasing? }
  240. result:=(([nf_write,nf_modify]*expr.flags)=[]) and not(tabstractvarsym(tloadnode(expr).symtableentry).addr_taken) and
  241. { no definition in the loop? }
  242. not(DFASetIn(tfornode(loop).t2.optinfo^.defsum,expr.optinfo^.index));
  243. end;
  244. vecn:
  245. begin
  246. result:=((tvecnode(expr).left.nodetype=loadn) or is_loop_invariant(loop,tvecnode(expr).left)) and
  247. is_loop_invariant(loop,tvecnode(expr).right);
  248. end;
  249. typeconvn:
  250. result:=is_loop_invariant(loop,ttypeconvnode(expr).left);
  251. addn,subn:
  252. result:=is_loop_invariant(loop,taddnode(expr).left) and is_loop_invariant(loop,taddnode(expr).right);
  253. else
  254. ;
  255. end;
  256. end;
  257. { checks if the strength of n can be recuded, arg is the tforloop being considered }
  258. function dostrengthreductiontest(var n: tnode; arg: pointer): foreachnoderesult;
  259. function findpreviousstrengthreduction : boolean;
  260. var
  261. i : longint;
  262. hp : tnode;
  263. begin
  264. result:=false;
  265. for i:=0 to inductionexprs.count-1 do
  266. begin
  267. { do we already maintain one expression? }
  268. if tnode(inductionexprs[i]).isequal(n) then
  269. begin
  270. case n.nodetype of
  271. muln:
  272. hp:=ctemprefnode.create(ttempcreatenode(templist[i]));
  273. vecn:
  274. hp:=ctypeconvnode.create_internal(cderefnode.create(ctemprefnode.create(
  275. ttempcreatenode(templist[i]))),n.resultdef);
  276. else
  277. internalerror(200809211);
  278. end;
  279. n.free;
  280. n:=hp;
  281. result:=true;
  282. exit;
  283. end;
  284. end;
  285. end;
  286. procedure CreateNodes;
  287. begin
  288. if not assigned(initcode) then
  289. begin
  290. initcode:=internalstatements(initcodestatements);
  291. calccode:=internalstatements(calccodestatements);
  292. deletecode:=internalstatements(deletecodestatements);
  293. end;
  294. end;
  295. var
  296. tempnode : ttempcreatenode;
  297. dummy : longint;
  298. begin
  299. result:=fen_false;
  300. case n.nodetype of
  301. forn:
  302. { inform for loop search routine, that it needs to search more deeply }
  303. containsnestedforloop:=true;
  304. muln:
  305. begin
  306. if (taddnode(n).right.nodetype=loadn) and
  307. taddnode(n).right.isequal(tfornode(arg).left) and
  308. { plain read of the loop variable? }
  309. not(nf_write in taddnode(n).right.flags) and
  310. not(nf_modify in taddnode(n).right.flags) and
  311. is_loop_invariant(tfornode(arg),taddnode(n).left) and
  312. { for now, we can handle only constant lower borders }
  313. is_constnode(tfornode(arg).right) then
  314. taddnode(n).swapleftright;
  315. if (taddnode(n).left.nodetype=loadn) and
  316. taddnode(n).left.isequal(tfornode(arg).left) and
  317. { plain read of the loop variable? }
  318. not(nf_write in taddnode(n).left.flags) and
  319. not(nf_modify in taddnode(n).left.flags) and
  320. is_loop_invariant(tfornode(arg),taddnode(n).right) and
  321. { for now, we can handle only constant lower borders }
  322. is_constnode(tfornode(arg).right) then
  323. begin
  324. changedforloop:=true;
  325. { did we use the same expression before already? }
  326. if not(findpreviousstrengthreduction) then
  327. begin
  328. tempnode:=ctempcreatenode.create(n.resultdef,n.resultdef.size,tt_persistent,
  329. tstoreddef(n.resultdef).is_intregable or tstoreddef(n.resultdef).is_fpuregable);
  330. templist.Add(tempnode);
  331. inductionexprs.Add(n);
  332. CreateNodes;
  333. if lnf_backward in tfornode(arg).loopflags then
  334. addstatement(calccodestatements,
  335. geninlinenode(in_dec_x,false,
  336. ccallparanode.create(ctemprefnode.create(tempnode),ccallparanode.create(taddnode(n).right.getcopy,nil))))
  337. else
  338. addstatement(calccodestatements,
  339. geninlinenode(in_inc_x,false,
  340. ccallparanode.create(ctemprefnode.create(tempnode),ccallparanode.create(taddnode(n).right.getcopy,nil))));
  341. addstatement(initcodestatements,tempnode);
  342. addstatement(initcodestatements,cassignmentnode.create(ctemprefnode.create(tempnode),
  343. caddnode.create(muln,tfornode(arg).right.getcopy,
  344. taddnode(n).right.getcopy)
  345. )
  346. );
  347. { finally replace the node by a temp. ref }
  348. n:=ctemprefnode.create(tempnode);
  349. { ... and add a temp. release node }
  350. addstatement(deletecodestatements,ctempdeletenode.create(tempnode));
  351. end;
  352. { set types }
  353. do_firstpass(n);
  354. result:=fen_norecurse_false;
  355. end;
  356. end;
  357. vecn:
  358. begin
  359. { is the index the counter variable? }
  360. if not(is_special_array(tvecnode(n).left.resultdef)) and
  361. not(is_packed_array(tvecnode(n).left.resultdef)) and
  362. (tvecnode(n).right.isequal(tfornode(arg).left) or
  363. { fpc usually creates a type cast to access an array }
  364. ((tvecnode(n).right.nodetype=typeconvn) and
  365. ttypeconvnode(tvecnode(n).right).left.isequal(tfornode(arg).left)
  366. )
  367. ) and
  368. { plain read of the loop variable? }
  369. not(nf_write in tvecnode(n).right.flags) and
  370. not(nf_modify in tvecnode(n).right.flags) and
  371. { direct array access? }
  372. ((tvecnode(n).left.nodetype=loadn) or
  373. { ... or loop invariant expression? }
  374. is_loop_invariant(tfornode(arg),tvecnode(n).right)) and
  375. { removing the multiplication is only worth the
  376. effort if it's not a simple shift }
  377. not(ispowerof2(tcgvecnode(n).get_mul_size,dummy)) then
  378. begin
  379. changedforloop:=true;
  380. { did we use the same expression before already? }
  381. if not(findpreviousstrengthreduction) then
  382. begin
  383. {$ifdef DEBUG_OPTSTRENGTH}
  384. writeln('**********************************************************************************');
  385. writeln('Found expression for strength reduction: ');
  386. printnode(n);
  387. writeln('**********************************************************************************');
  388. {$endif DEBUG_OPTSTRENGTH}
  389. tempnode:=ctempcreatenode.create(voidpointertype,voidpointertype.size,tt_persistent,true);
  390. templist.Add(tempnode);
  391. inductionexprs.Add(n);
  392. CreateNodes;
  393. if lnf_backward in tfornode(arg).loopflags then
  394. addstatement(calccodestatements,
  395. cinlinenode.createintern(in_dec_x,false,
  396. ccallparanode.create(ctemprefnode.create(tempnode),ccallparanode.create(
  397. cordconstnode.create(tcgvecnode(n).get_mul_size,sizeuinttype,false),nil))))
  398. else
  399. addstatement(calccodestatements,
  400. cinlinenode.createintern(in_inc_x,false,
  401. ccallparanode.create(ctemprefnode.create(tempnode),ccallparanode.create(
  402. cordconstnode.create(tcgvecnode(n).get_mul_size,sizeuinttype,false),nil))));
  403. addstatement(initcodestatements,tempnode);
  404. addstatement(initcodestatements,cassignmentnode.create(ctemprefnode.create(tempnode),
  405. caddrnode.create(
  406. cvecnode.create(tvecnode(n).left.getcopy,tfornode(arg).right.getcopy)
  407. )
  408. ));
  409. { finally replace the node by a temp. ref }
  410. n:=ctypeconvnode.create_internal(cderefnode.create(ctemprefnode.create(tempnode)),n.resultdef);
  411. { ... and add a temp. release node }
  412. addstatement(deletecodestatements,ctempdeletenode.create(tempnode));
  413. end;
  414. { set types }
  415. do_firstpass(n);
  416. result:=fen_norecurse_false;
  417. end;
  418. end;
  419. else
  420. ;
  421. end;
  422. end;
  423. function OptimizeInductionVariablesSingleForLoop(node : tnode) : tnode;
  424. var
  425. loopcode : tblocknode;
  426. loopcodestatements,
  427. newcodestatements : tstatementnode;
  428. fornode : tfornode;
  429. begin
  430. result:=nil;
  431. if node.nodetype<>forn then
  432. exit;
  433. templist:=TFPList.Create;
  434. inductionexprs:=TFPList.Create;
  435. initcode:=nil;
  436. calccode:=nil;
  437. deletecode:=nil;
  438. initcodestatements:=nil;
  439. calccodestatements:=nil;
  440. deletecodestatements:=nil;
  441. { find all expressions being candidates for strength reduction
  442. and replace them }
  443. foreachnodestatic(pm_postprocess,node,@dostrengthreductiontest,node);
  444. { clue everything together }
  445. if assigned(initcode) then
  446. begin
  447. do_firstpass(tnode(initcode));
  448. do_firstpass(tnode(calccode));
  449. do_firstpass(tnode(deletecode));
  450. { create a new for node, the old one will be released by the compiler }
  451. with tfornode(node) do
  452. begin
  453. fornode:=cfornode.create(left,right,t1,t2,lnf_backward in loopflags);
  454. left:=nil;
  455. right:=nil;
  456. t1:=nil;
  457. t2:=nil;
  458. end;
  459. node:=fornode;
  460. loopcode:=internalstatements(loopcodestatements);
  461. addstatement(loopcodestatements,tfornode(node).t2);
  462. tfornode(node).t2:=loopcode;
  463. do_firstpass(node);
  464. addstatement(loopcodestatements,calccode);
  465. result:=internalstatements(newcodestatements);
  466. addstatement(newcodestatements,initcode);
  467. initcode:=nil;
  468. addstatement(newcodestatements,node);
  469. addstatement(newcodestatements,deletecode);
  470. end;
  471. templist.Free;
  472. inductionexprs.Free;
  473. end;
  474. function OptimizeInductionVariables_iterforloops(var n: tnode; arg: pointer): foreachnoderesult;
  475. var
  476. hp : tnode;
  477. begin
  478. Result:=fen_false;
  479. if n.nodetype=forn then
  480. begin
  481. { do we have DFA available? }
  482. if pi_dfaavailable in current_procinfo.flags then
  483. begin
  484. CalcDefSum(tfornode(n).t2);
  485. end;
  486. containsnestedforloop:=false;
  487. hp:=OptimizeInductionVariablesSingleForLoop(n);
  488. if assigned(hp) then
  489. begin
  490. n.Free;
  491. n:=hp;
  492. end;
  493. { can we avoid further searching? }
  494. if not(containsnestedforloop) then
  495. Result:=fen_norecurse_false;
  496. end;
  497. end;
  498. function OptimizeInductionVariables(node : tnode) : boolean;
  499. begin
  500. changedforloop:=false;
  501. foreachnodestatic(pm_postprocess,node,@OptimizeInductionVariables_iterforloops,nil);
  502. Result:=changedforloop;
  503. end;
  504. function OptimizeForLoop_iterforloops(var n: tnode; arg: pointer): foreachnoderesult;
  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.